summaryrefslogtreecommitdiffstats
path: root/t
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-09 13:34:27 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-09 13:34:27 +0000
commit4dbdc42d9e7c3968ff7f690d00680419c9b8cb0f (patch)
tree47c1d492e9c956c1cd2b74dbd3b9d8b0db44dc4e /t
parentInitial commit. (diff)
downloadgit-4dbdc42d9e7c3968ff7f690d00680419c9b8cb0f.tar.xz
git-4dbdc42d9e7c3968ff7f690d00680419c9b8cb0f.zip
Adding upstream version 1:2.43.0.upstream/1%2.43.0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 't')
-rw-r--r--t/.gitattributes25
-rw-r--r--t/.gitignore5
-rwxr-xr-xt/Git-SVN/00compile.t14
-rwxr-xr-xt/Git-SVN/Utils/add_path_to_url.t27
-rwxr-xr-xt/Git-SVN/Utils/can_compress.t11
-rwxr-xr-xt/Git-SVN/Utils/canonicalize_url.t26
-rwxr-xr-xt/Git-SVN/Utils/collapse_dotdot.t23
-rwxr-xr-xt/Git-SVN/Utils/fatal.t34
-rwxr-xr-xt/Git-SVN/Utils/join_paths.t32
-rw-r--r--t/Makefile152
-rw-r--r--t/README1290
-rwxr-xr-xt/aggregate-results.sh63
-rw-r--r--t/annotate-tests.sh677
-rwxr-xr-xt/chainlint.pl832
-rw-r--r--t/chainlint/arithmetic-expansion.expect9
-rw-r--r--t/chainlint/arithmetic-expansion.test11
-rw-r--r--t/chainlint/bash-array.expect10
-rw-r--r--t/chainlint/bash-array.test12
-rw-r--r--t/chainlint/blank-line-before-esac.expect18
-rw-r--r--t/chainlint/blank-line-before-esac.test19
-rw-r--r--t/chainlint/blank-line.expect4
-rw-r--r--t/chainlint/blank-line.test10
-rw-r--r--t/chainlint/block-comment.expect8
-rw-r--r--t/chainlint/block-comment.test8
-rw-r--r--t/chainlint/block.expect23
-rw-r--r--t/chainlint/block.test27
-rw-r--r--t/chainlint/broken-chain.expect6
-rw-r--r--t/chainlint/broken-chain.test8
-rw-r--r--t/chainlint/case-comment.expect11
-rw-r--r--t/chainlint/case-comment.test11
-rw-r--r--t/chainlint/case.expect19
-rw-r--r--t/chainlint/case.test23
-rw-r--r--t/chainlint/chain-break-background.expect9
-rw-r--r--t/chainlint/chain-break-background.test10
-rw-r--r--t/chainlint/chain-break-continue.expect12
-rw-r--r--t/chainlint/chain-break-continue.test13
-rw-r--r--t/chainlint/chain-break-false.expect9
-rw-r--r--t/chainlint/chain-break-false.test10
-rw-r--r--t/chainlint/chain-break-return-exit.expect19
-rw-r--r--t/chainlint/chain-break-return-exit.test23
-rw-r--r--t/chainlint/chain-break-status.expect9
-rw-r--r--t/chainlint/chain-break-status.test11
-rw-r--r--t/chainlint/chained-block.expect9
-rw-r--r--t/chainlint/chained-block.test11
-rw-r--r--t/chainlint/chained-subshell.expect10
-rw-r--r--t/chainlint/chained-subshell.test13
-rw-r--r--t/chainlint/close-nested-and-parent-together.expect3
-rw-r--r--t/chainlint/close-nested-and-parent-together.test3
-rw-r--r--t/chainlint/close-subshell.expect26
-rw-r--r--t/chainlint/close-subshell.test27
-rw-r--r--t/chainlint/command-substitution-subsubshell.expect2
-rw-r--r--t/chainlint/command-substitution-subsubshell.test3
-rw-r--r--t/chainlint/command-substitution.expect9
-rw-r--r--t/chainlint/command-substitution.test11
-rw-r--r--t/chainlint/comment.expect8
-rw-r--r--t/chainlint/comment.test11
-rw-r--r--t/chainlint/complex-if-in-cuddled-loop.expect9
-rw-r--r--t/chainlint/complex-if-in-cuddled-loop.test11
-rw-r--r--t/chainlint/cuddled-if-then-else.expect6
-rw-r--r--t/chainlint/cuddled-if-then-else.test7
-rw-r--r--t/chainlint/cuddled-loop.expect4
-rw-r--r--t/chainlint/cuddled-loop.test7
-rw-r--r--t/chainlint/cuddled.expect17
-rw-r--r--t/chainlint/cuddled.test22
-rw-r--r--t/chainlint/double-here-doc.expect12
-rw-r--r--t/chainlint/double-here-doc.test12
-rw-r--r--t/chainlint/dqstring-line-splice.expect3
-rw-r--r--t/chainlint/dqstring-line-splice.test7
-rw-r--r--t/chainlint/dqstring-no-interpolate.expect11
-rw-r--r--t/chainlint/dqstring-no-interpolate.test15
-rw-r--r--t/chainlint/empty-here-doc.expect4
-rw-r--r--t/chainlint/empty-here-doc.test5
-rw-r--r--t/chainlint/exclamation.expect4
-rw-r--r--t/chainlint/exclamation.test8
-rw-r--r--t/chainlint/exit-loop.expect24
-rw-r--r--t/chainlint/exit-loop.test27
-rw-r--r--t/chainlint/exit-subshell.expect5
-rw-r--r--t/chainlint/exit-subshell.test6
-rw-r--r--t/chainlint/for-loop-abbreviated.expect5
-rw-r--r--t/chainlint/for-loop-abbreviated.test6
-rw-r--r--t/chainlint/for-loop.expect13
-rw-r--r--t/chainlint/for-loop.test19
-rw-r--r--t/chainlint/function.expect11
-rw-r--r--t/chainlint/function.test13
-rw-r--r--t/chainlint/here-doc-close-subshell.expect4
-rw-r--r--t/chainlint/here-doc-close-subshell.test5
-rw-r--r--t/chainlint/here-doc-indent-operator.expect11
-rw-r--r--t/chainlint/here-doc-indent-operator.test13
-rw-r--r--t/chainlint/here-doc-multi-line-command-subst.expect8
-rw-r--r--t/chainlint/here-doc-multi-line-command-subst.test9
-rw-r--r--t/chainlint/here-doc-multi-line-string.expect7
-rw-r--r--t/chainlint/here-doc-multi-line-string.test8
-rw-r--r--t/chainlint/here-doc.expect25
-rw-r--r--t/chainlint/here-doc.test30
-rw-r--r--t/chainlint/if-condition-split.expect7
-rw-r--r--t/chainlint/if-condition-split.test8
-rw-r--r--t/chainlint/if-in-loop.expect12
-rw-r--r--t/chainlint/if-in-loop.test15
-rw-r--r--t/chainlint/if-then-else.expect22
-rw-r--r--t/chainlint/if-then-else.test29
-rw-r--r--t/chainlint/incomplete-line.expect10
-rw-r--r--t/chainlint/incomplete-line.test12
-rw-r--r--t/chainlint/inline-comment.expect8
-rw-r--r--t/chainlint/inline-comment.test12
-rw-r--r--t/chainlint/loop-detect-failure.expect15
-rw-r--r--t/chainlint/loop-detect-failure.test17
-rw-r--r--t/chainlint/loop-detect-status.expect18
-rw-r--r--t/chainlint/loop-detect-status.test19
-rw-r--r--t/chainlint/loop-in-if.expect12
-rw-r--r--t/chainlint/loop-in-if.test15
-rw-r--r--t/chainlint/loop-upstream-pipe.expect10
-rw-r--r--t/chainlint/loop-upstream-pipe.test11
-rw-r--r--t/chainlint/multi-line-nested-command-substitution.expect18
-rw-r--r--t/chainlint/multi-line-nested-command-substitution.test18
-rw-r--r--t/chainlint/multi-line-string.expect14
-rw-r--r--t/chainlint/multi-line-string.test15
-rw-r--r--t/chainlint/negated-one-liner.expect5
-rw-r--r--t/chainlint/negated-one-liner.test7
-rw-r--r--t/chainlint/nested-cuddled-subshell.expect19
-rw-r--r--t/chainlint/nested-cuddled-subshell.test31
-rw-r--r--t/chainlint/nested-here-doc.expect30
-rw-r--r--t/chainlint/nested-here-doc.test33
-rw-r--r--t/chainlint/nested-loop-detect-failure.expect31
-rw-r--r--t/chainlint/nested-loop-detect-failure.test35
-rw-r--r--t/chainlint/nested-subshell-comment.expect11
-rw-r--r--t/chainlint/nested-subshell-comment.test13
-rw-r--r--t/chainlint/nested-subshell.expect12
-rw-r--r--t/chainlint/nested-subshell.test13
-rw-r--r--t/chainlint/not-heredoc.expect14
-rw-r--r--t/chainlint/not-heredoc.test16
-rw-r--r--t/chainlint/one-liner-for-loop.expect9
-rw-r--r--t/chainlint/one-liner-for-loop.test10
-rw-r--r--t/chainlint/one-liner.expect9
-rw-r--r--t/chainlint/one-liner.test12
-rw-r--r--t/chainlint/p4-filespec.expect4
-rw-r--r--t/chainlint/p4-filespec.test5
-rw-r--r--t/chainlint/pipe.expect8
-rw-r--r--t/chainlint/pipe.test12
-rw-r--r--t/chainlint/return-loop.expect5
-rw-r--r--t/chainlint/return-loop.test6
-rw-r--r--t/chainlint/semicolon.expect19
-rw-r--r--t/chainlint/semicolon.test25
-rw-r--r--t/chainlint/sqstring-in-sqstring.expect4
-rw-r--r--t/chainlint/sqstring-in-sqstring.test5
-rw-r--r--t/chainlint/subshell-here-doc.expect30
-rw-r--r--t/chainlint/subshell-here-doc.test35
-rw-r--r--t/chainlint/subshell-one-liner.expect14
-rw-r--r--t/chainlint/subshell-one-liner.test24
-rw-r--r--t/chainlint/t7900-subtree.expect21
-rw-r--r--t/chainlint/t7900-subtree.test22
-rw-r--r--t/chainlint/token-pasting.expect27
-rw-r--r--t/chainlint/token-pasting.test32
-rw-r--r--t/chainlint/unclosed-here-doc-indent.expect4
-rw-r--r--t/chainlint/unclosed-here-doc-indent.test4
-rw-r--r--t/chainlint/unclosed-here-doc.expect7
-rw-r--r--t/chainlint/unclosed-here-doc.test7
-rw-r--r--t/chainlint/while-loop.expect13
-rw-r--r--t/chainlint/while-loop.test19
-rwxr-xr-xt/check-non-portable-shell.pl56
-rw-r--r--t/helper/.gitignore2
-rw-r--r--t/helper/test-advise.c22
-rw-r--r--t/helper/test-bitmap.c33
-rw-r--r--t/helper/test-bloom.c100
-rw-r--r--t/helper/test-bundle-uri.c137
-rw-r--r--t/helper/test-cache-tree.c69
-rw-r--r--t/helper/test-chmtime.c163
-rw-r--r--t/helper/test-config.c237
-rw-r--r--t/helper/test-crontab.c35
-rw-r--r--t/helper/test-csprng.c29
-rw-r--r--t/helper/test-ctype.c70
-rw-r--r--t/helper/test-date.c135
-rw-r--r--t/helper/test-delta.c85
-rw-r--r--t/helper/test-dir-iterator.c63
-rw-r--r--t/helper/test-drop-caches.c162
-rw-r--r--t/helper/test-dump-cache-tree.c78
-rw-r--r--t/helper/test-dump-fsmonitor.c24
-rw-r--r--t/helper/test-dump-split-index.c43
-rw-r--r--t/helper/test-dump-untracked-cache.c71
-rw-r--r--t/helper/test-env-helper.c100
-rw-r--r--t/helper/test-example-decorate.c78
-rw-r--r--t/helper/test-fake-ssh.c32
-rw-r--r--t/helper/test-fast-rebase.c241
-rw-r--r--t/helper/test-find-pack.c50
-rw-r--r--t/helper/test-fsmonitor-client.c224
-rw-r--r--t/helper/test-genrandom.c34
-rw-r--r--t/helper/test-genzeros.c35
-rw-r--r--t/helper/test-getcwd.c26
-rw-r--r--t/helper/test-hash-speed.c61
-rw-r--r--t/helper/test-hash.c59
-rw-r--r--t/helper/test-hashmap.c274
-rw-r--r--t/helper/test-hexdump.c30
-rw-r--r--t/helper/test-json-writer.c592
-rw-r--r--t/helper/test-lazy-init-name-hash.c267
-rw-r--r--t/helper/test-match-trees.c31
-rw-r--r--t/helper/test-mergesort.c406
-rw-r--r--t/helper/test-mktemp.c15
-rw-r--r--t/helper/test-oid-array.c45
-rw-r--r--t/helper/test-oidmap.c123
-rw-r--r--t/helper/test-oidtree.c54
-rw-r--r--t/helper/test-online-cpus.c9
-rw-r--r--t/helper/test-pack-mtimes.c57
-rw-r--r--t/helper/test-parse-options.c340
-rw-r--r--t/helper/test-parse-pathspec-file.c33
-rw-r--r--t/helper/test-partial-clone.c44
-rw-r--r--t/helper/test-path-utils.c506
-rw-r--r--t/helper/test-pcre2-config.c11
-rw-r--r--t/helper/test-pkt-line.c138
-rw-r--r--t/helper/test-prio-queue.c51
-rw-r--r--t/helper/test-proc-receive.c203
-rw-r--r--t/helper/test-progress.c97
-rw-r--r--t/helper/test-reach.c177
-rw-r--r--t/helper/test-read-cache.c42
-rw-r--r--t/helper/test-read-graph.c64
-rw-r--r--t/helper/test-read-midx.c115
-rw-r--r--t/helper/test-ref-store.c361
-rw-r--r--t/helper/test-reftable.c23
-rw-r--r--t/helper/test-regex.c117
-rw-r--r--t/helper/test-repository.c102
-rw-r--r--t/helper/test-revision-walking.c72
-rw-r--r--t/helper/test-rot13-filter.c382
-rw-r--r--t/helper/test-run-command.c462
-rw-r--r--t/helper/test-scrap-cache-tree.c23
-rw-r--r--t/helper/test-serve-v2.c36
-rw-r--r--t/helper/test-sha1.c15
-rwxr-xr-xt/helper/test-sha1.sh83
-rw-r--r--t/helper/test-sha256.c7
-rw-r--r--t/helper/test-sigchain.c23
-rw-r--r--t/helper/test-simple-ipc.c688
-rw-r--r--t/helper/test-strcmp-offset.c23
-rw-r--r--t/helper/test-string-list.c129
-rw-r--r--t/helper/test-submodule-config.c69
-rw-r--r--t/helper/test-submodule-nested-repo-config.c32
-rw-r--r--t/helper/test-submodule.c225
-rw-r--r--t/helper/test-subprocess.c20
-rw-r--r--t/helper/test-tool-utils.h9
-rw-r--r--t/helper/test-tool.c143
-rw-r--r--t/helper/test-tool.h94
-rw-r--r--t/helper/test-trace2.c498
-rw-r--r--t/helper/test-truncate.c25
-rw-r--r--t/helper/test-urlmatch-normalization.c56
-rw-r--r--t/helper/test-userdiff.c48
-rw-r--r--t/helper/test-wildmatch.c24
-rw-r--r--t/helper/test-windows-named-pipe.c72
-rw-r--r--t/helper/test-write-cache.c24
-rw-r--r--t/helper/test-xml-encode.c80
-rw-r--r--t/interop/.gitignore4
-rw-r--r--t/interop/Makefile19
-rw-r--r--t/interop/README85
-rwxr-xr-xt/interop/i0000-basic.sh27
-rwxr-xr-xt/interop/i5500-git-daemon.sh40
-rwxr-xr-xt/interop/i5700-protocol-transition.sh68
-rw-r--r--t/interop/interop-lib.sh92
-rw-r--r--t/lib-bash.sh19
-rw-r--r--t/lib-bitmap.sh451
-rw-r--r--t/lib-bundle-uri-protocol.sh216
-rw-r--r--t/lib-bundle.sh42
-rw-r--r--t/lib-chunk.sh17
-rw-r--r--t/lib-chunk/corrupt-chunk-file.pl90
-rwxr-xr-xt/lib-commit-graph.sh74
-rw-r--r--t/lib-credential.sh546
-rw-r--r--t/lib-cvs.sh78
-rw-r--r--t/lib-diff-alternative.sh227
-rw-r--r--t/lib-diff-data.sh22
-rw-r--r--t/lib-diff.sh41
-rw-r--r--t/lib-encoding.sh25
-rw-r--r--t/lib-gettext.sh63
-rw-r--r--t/lib-git-daemon.sh120
-rw-r--r--t/lib-git-p4.sh225
-rw-r--r--t/lib-git-svn.sh149
-rw-r--r--t/lib-gitweb.sh123
-rw-r--r--t/lib-gpg.sh200
-rw-r--r--t/lib-gpg/gpgsm-gen-key.in8
-rw-r--r--t/lib-gpg/gpgsm_cert.p12bin0 -> 2652 bytes
-rw-r--r--t/lib-gpg/keyring.gpg192
-rw-r--r--t/lib-gpg/ownertrust4
-rw-r--r--t/lib-httpd.sh346
-rw-r--r--t/lib-httpd/apache.conf292
-rw-r--r--t/lib-httpd/apply-one-time-perl.sh27
-rw-r--r--t/lib-httpd/broken-smart-http.sh10
-rw-r--r--t/lib-httpd/error-no-report.sh6
-rw-r--r--t/lib-httpd/error-smart-http.sh3
-rwxr-xr-xt/lib-httpd/error.sh31
-rw-r--r--t/lib-httpd/incomplete-body-upload-pack-v2-http.sh3
-rw-r--r--t/lib-httpd/incomplete-length-upload-pack-v2-http.sh3
-rw-r--r--t/lib-httpd/nph-custom-auth.sh39
-rw-r--r--t/lib-httpd/passwd1
-rw-r--r--t/lib-httpd/proxy-passwd1
-rw-r--r--t/lib-httpd/ssl.cnf8
-rw-r--r--t/lib-log-graph.sh28
-rw-r--r--t/lib-merge.sh13
-rw-r--r--t/lib-midx.sh8
-rw-r--r--t/lib-pack.sh140
-rw-r--r--t/lib-pager.sh15
-rw-r--r--t/lib-parallel-checkout.sh49
-rw-r--r--t/lib-patch-mode.sh55
-rw-r--r--t/lib-perl.sh19
-rw-r--r--t/lib-proto-disable.sh220
-rw-r--r--t/lib-read-tree-m-3way.sh158
-rw-r--r--t/lib-read-tree.sh41
-rw-r--r--t/lib-rebase.sh231
-rw-r--r--t/lib-submodule-update.sh1099
-rw-r--r--t/lib-subtest.sh95
-rw-r--r--t/lib-sudo.sh15
-rw-r--r--t/lib-t3100.sh10
-rw-r--r--t/lib-t6000.sh136
-rw-r--r--t/lib-terminal.sh36
-rwxr-xr-xt/lib-unicode-nfc-nfd.sh162
-rw-r--r--t/lib-unique-files.sh34
-rw-r--r--t/oid-info/README19
-rw-r--r--t/oid-info/hash-info17
-rw-r--r--t/oid-info/oid31
-rw-r--r--t/perf/.gitignore4
-rw-r--r--t/perf/Makefile21
-rw-r--r--t/perf/README210
-rwxr-xr-xt/perf/aggregate.perl357
-rwxr-xr-xt/perf/bisect_regression73
-rwxr-xr-xt/perf/bisect_run_script53
-rw-r--r--t/perf/config2
-rw-r--r--t/perf/lib-bitmap.sh100
-rw-r--r--t/perf/lib-pack.sh25
-rwxr-xr-xt/perf/min_time.perl21
-rwxr-xr-xt/perf/p0000-perf-lib-sanity.sh57
-rwxr-xr-xt/perf/p0001-rev-list.sh48
-rwxr-xr-xt/perf/p0002-read-cache.sh14
-rwxr-xr-xt/perf/p0003-delta-base-cache.sh31
-rwxr-xr-xt/perf/p0004-lazy-init-name-hash.sh56
-rwxr-xr-xt/perf/p0005-status.sh49
-rwxr-xr-xt/perf/p0006-read-tree-checkout.sh75
-rwxr-xr-xt/perf/p0007-write-cache.sh29
-rwxr-xr-xt/perf/p0008-odb-fsync.sh82
-rwxr-xr-xt/perf/p0071-sort.sh52
-rwxr-xr-xt/perf/p0090-cache-tree.sh36
-rwxr-xr-xt/perf/p0100-globbing.sh43
-rwxr-xr-xt/perf/p1006-cat-file.sh12
-rwxr-xr-xt/perf/p1400-update-ref.sh33
-rwxr-xr-xt/perf/p1450-fsck.sh13
-rwxr-xr-xt/perf/p1451-fsck-skip-list.sh40
-rwxr-xr-xt/perf/p1500-graph-walks.sh50
-rwxr-xr-xt/perf/p2000-sparse-operations.sh139
-rwxr-xr-xt/perf/p3400-rebase.sh56
-rwxr-xr-xt/perf/p3404-rebase-interactive.sh36
-rwxr-xr-xt/perf/p4000-diff-algorithms.sh29
-rwxr-xr-xt/perf/p4001-diff-no-index.sh22
-rwxr-xr-xt/perf/p4002-diff-color-moved.sh57
-rwxr-xr-xt/perf/p4205-log-pretty-formats.sh16
-rwxr-xr-xt/perf/p4209-pickaxe.sh70
-rwxr-xr-xt/perf/p4211-line-log.sh42
-rwxr-xr-xt/perf/p4220-log-grep-engines.sh54
-rwxr-xr-xt/perf/p4221-log-grep-engines-fixed.sh45
-rwxr-xr-xt/perf/p5302-pack-index.sh50
-rwxr-xr-xt/perf/p5303-many-packs.sh144
-rwxr-xr-xt/perf/p5304-prune.sh35
-rwxr-xr-xt/perf/p5310-pack-bitmaps.sh40
-rwxr-xr-xt/perf/p5311-pack-bitmaps-fetch.sh54
-rwxr-xr-xt/perf/p5312-pack-bitmaps-revs.sh34
-rwxr-xr-xt/perf/p5326-multi-pack-bitmaps.sh67
-rwxr-xr-xt/perf/p5550-fetch-tags.sh78
-rwxr-xr-xt/perf/p5551-fetch-rescan.sh55
-rwxr-xr-xt/perf/p5600-partial-clone.sh42
-rwxr-xr-xt/perf/p5601-clone-reference.sh27
-rwxr-xr-xt/perf/p7000-filter-branch.sh24
-rwxr-xr-xt/perf/p7102-reset.sh21
-rwxr-xr-xt/perf/p7300-clean.sh35
-rwxr-xr-xt/perf/p7519-fsmonitor.sh314
-rwxr-xr-xt/perf/p7527-builtin-fsmonitor.sh257
-rwxr-xr-xt/perf/p7810-grep.sh23
-rwxr-xr-xt/perf/p7820-grep-engines.sh90
-rwxr-xr-xt/perf/p7821-grep-engines-fixed.sh74
-rwxr-xr-xt/perf/p7822-grep-perl-character.sh42
-rwxr-xr-xt/perf/p9210-scalar.sh39
-rwxr-xr-xt/perf/p9300-fast-import-export.sh23
-rw-r--r--t/perf/perf-lib.sh334
-rw-r--r--t/perf/repos/.gitignore1
-rwxr-xr-xt/perf/repos/inflate-repo.sh85
-rwxr-xr-xt/perf/repos/many-files.sh110
-rwxr-xr-xt/perf/run257
-rwxr-xr-xt/t0000-basic.sh1222
-rwxr-xr-xt/t0001-init.sh611
-rwxr-xr-xt/t0002-gitfile.sh139
-rwxr-xr-xt/t0003-attributes.sh561
-rwxr-xr-xt/t0004-unwritable.sh84
-rwxr-xr-xt/t0005-signals.sh55
-rwxr-xr-xt/t0006-date.sh169
-rwxr-xr-xt/t0007-git-var.sh280
-rwxr-xr-xt/t0008-ignores.sh948
-rwxr-xr-xt/t0009-prio-queue.sh66
-rwxr-xr-xt/t0010-racy-git.sh34
-rwxr-xr-xt/t0011-hashmap.sh260
-rwxr-xr-xt/t0012-help.sh264
-rwxr-xr-xt/t0013-sha1dc.sh23
-rw-r--r--t/t0013/shattered-1.pdfbin0 -> 422435 bytes
-rwxr-xr-xt/t0014-alias.sh47
-rwxr-xr-xt/t0015-hash.sh56
-rwxr-xr-xt/t0016-oidmap.sh112
-rwxr-xr-xt/t0017-env-helper.sh100
-rwxr-xr-xt/t0018-advice.sh33
-rwxr-xr-xt/t0019-json-writer.sh333
-rw-r--r--t/t0019/parse_json.perl55
-rwxr-xr-xt/t0020-crlf.sh401
-rwxr-xr-xt/t0021-conversion.sh1154
-rwxr-xr-xt/t0022-crlf-rename.sh34
-rwxr-xr-xt/t0023-crlf-am.sh45
-rwxr-xr-xt/t0024-crlf-archive.sh39
-rwxr-xr-xt/t0025-crlf-renormalize.sh40
-rwxr-xr-xt/t0026-eol-config.sh104
-rwxr-xr-xt/t0027-auto-crlf.sh632
-rwxr-xr-xt/t0028-working-tree-encoding.sh305
-rwxr-xr-xt/t0029-core-unsetenvvars.sh30
-rwxr-xr-xt/t0030-stripspace.sh455
-rwxr-xr-xt/t0032-reftable-unittest.sh16
-rwxr-xr-xt/t0033-safe-directory.sh83
-rwxr-xr-xt/t0034-root-safe-directory.sh93
-rwxr-xr-xt/t0035-safe-bare-repository.sh81
-rwxr-xr-xt/t0040-parse-options.sh754
-rwxr-xr-xt/t0041-usage.sh111
-rwxr-xr-xt/t0050-filesystem.sh159
-rwxr-xr-xt/t0051-windows-named-pipe.sh22
-rwxr-xr-xt/t0052-simple-ipc.sh123
-rwxr-xr-xt/t0055-beyond-symlinks.sh36
-rwxr-xr-xt/t0056-git-C.sh95
-rwxr-xr-xt/t0060-path-utils.sh613
-rwxr-xr-xt/t0061-run-command.sh272
-rwxr-xr-xt/t0062-revision-walking.sh34
-rwxr-xr-xt/t0063-string-list.sh143
-rwxr-xr-xt/t0064-oid-array.sh104
-rwxr-xr-xt/t0065-strcmp-offset.sh22
-rwxr-xr-xt/t0066-dir-iterator.sh134
-rwxr-xr-xt/t0067-parse_pathspec_file.sh109
-rwxr-xr-xt/t0068-for-each-repo.sh62
-rwxr-xr-xt/t0069-oidtree.sh50
-rwxr-xr-xt/t0070-fundamental.sh56
-rwxr-xr-xt/t0071-sort.sh12
-rwxr-xr-xt/t0081-find-pack.sh82
-rwxr-xr-xt/t0090-cache-tree.sh282
-rwxr-xr-xt/t0091-bugreport.sh157
-rwxr-xr-xt/t0092-diagnose.sh72
-rwxr-xr-xt/t0095-bloom.sh119
-rwxr-xr-xt/t0100-previous.sh76
-rwxr-xr-xt/t0101-at-syntax.sh47
-rwxr-xr-xt/t0110-urlmatch-normalization.sh182
-rw-r--r--t/t0110/README9
-rw-r--r--t/t0110/url-11
-rw-r--r--t/t0110/url-101
-rw-r--r--t/t0110/url-111
-rw-r--r--t/t0110/url-21
-rw-r--r--t/t0110/url-31
-rw-r--r--t/t0110/url-41
-rw-r--r--t/t0110/url-51
-rw-r--r--t/t0110/url-61
-rw-r--r--t/t0110/url-71
-rw-r--r--t/t0110/url-81
-rw-r--r--t/t0110/url-91
-rwxr-xr-xt/t0200-gettext-basic.sh109
-rw-r--r--t/t0200/test.c23
-rw-r--r--t/t0200/test.perl14
-rw-r--r--t/t0200/test.sh14
-rwxr-xr-xt/t0201-gettext-fallbacks.sh68
-rwxr-xr-xt/t0202-gettext-perl.sh18
-rwxr-xr-xt/t0202/test.pl122
-rwxr-xr-xt/t0203-gettext-setlocale-sanity.sh27
-rwxr-xr-xt/t0204-gettext-reencode-sanity.sh88
-rwxr-xr-xt/t0210-trace2-normal.sh286
-rw-r--r--t/t0210/scrub_normal.perl54
-rwxr-xr-xt/t0211-trace2-perf.sh271
-rw-r--r--t/t0211/scrub_perf.perl91
-rwxr-xr-xt/t0212-trace2-event.sh326
-rw-r--r--t/t0212/parse_events.perl261
-rwxr-xr-xt/t0300-credentials.sh833
-rwxr-xr-xt/t0301-credential-cache.sh129
-rwxr-xr-xt/t0302-credential-store.sh212
-rwxr-xr-xt/t0303-credential-external.sh62
-rwxr-xr-xt/t0410-partial-clone.sh707
-rwxr-xr-xt/t0450-txt-doc-vs-help.sh172
-rw-r--r--t/t0450/txt-help-mismatches58
-rwxr-xr-xt/t0500-progress-display.sh378
-rwxr-xr-xt/t1000-read-tree-m-3way.sh513
-rwxr-xr-xt/t1001-read-tree-m-2way.sh420
-rwxr-xr-xt/t1002-read-tree-m-u-2way.sh348
-rwxr-xr-xt/t1003-read-tree-prefix.sh38
-rwxr-xr-xt/t1004-read-tree-m-u-wf.sh242
-rwxr-xr-xt/t1005-read-tree-reset.sh112
-rwxr-xr-xt/t1006-cat-file.sh1204
-rwxr-xr-xt/t1007-hash-object.sh263
-rwxr-xr-xt/t1008-read-tree-overlay.sh36
-rwxr-xr-xt/t1009-read-tree-new-index.sh29
-rwxr-xr-xt/t1010-mktree.sh70
-rwxr-xr-xt/t1011-read-tree-sparse-checkout.sh307
-rwxr-xr-xt/t1012-read-tree-df.sh104
-rwxr-xr-xt/t1013-read-tree-submodule.sh18
-rwxr-xr-xt/t1014-read-tree-confusing.sh65
-rwxr-xr-xt/t1015-read-index-unmerged.sh123
-rwxr-xr-xt/t1020-subdirectory.sh196
-rwxr-xr-xt/t1021-rerere-in-workdir.sh58
-rwxr-xr-xt/t1022-read-tree-partial-clone.sh30
-rwxr-xr-xt/t1050-large.sh215
-rwxr-xr-xt/t1051-large-conversion.sh114
-rwxr-xr-xt/t1060-object-corruption.sh149
-rwxr-xr-xt/t1090-sparse-checkout-scope.sh109
-rwxr-xr-xt/t1091-sparse-checkout-builtin.sh1048
-rwxr-xr-xt/t1092-sparse-checkout-compatibility.sh2334
-rwxr-xr-xt/t1100-commit-tree-options.sh64
-rwxr-xr-xt/t1300-config.sh2593
-rwxr-xr-xt/t1301-shared-repo.sh241
-rwxr-xr-xt/t1302-repo-version.sh118
-rwxr-xr-xt/t1303-wacky-config.sh136
-rwxr-xr-xt/t1304-default-acl.sh66
-rwxr-xr-xt/t1305-config-include.sh360
-rwxr-xr-xt/t1306-xdg-files.sh195
-rwxr-xr-xt/t1307-config-blob.sh80
-rwxr-xr-xt/t1308-config-set.sh400
-rwxr-xr-xt/t1309-early-config.sh103
-rwxr-xr-xt/t1310-config-default.sh37
-rwxr-xr-xt/t1350-config-hooks-path.sh44
-rwxr-xr-xt/t1400-update-ref.sh1647
-rwxr-xr-xt/t1401-symbolic-ref.sh227
-rwxr-xr-xt/t1402-check-ref-format.sh219
-rwxr-xr-xt/t1403-show-ref.sh269
-rwxr-xr-xt/t1404-update-ref-errors.sh636
-rwxr-xr-xt/t1405-main-ref-store.sh140
-rwxr-xr-xt/t1406-submodule-ref-store.sh99
-rwxr-xr-xt/t1407-worktree-ref-store.sh93
-rwxr-xr-xt/t1408-packed-refs.sh46
-rwxr-xr-xt/t1409-avoid-packing-refs.sh119
-rwxr-xr-xt/t1410-reflog.sh481
-rwxr-xr-xt/t1411-reflog-show.sh173
-rwxr-xr-xt/t1412-reflog-loop.sh33
-rwxr-xr-xt/t1413-reflog-detach.sh73
-rwxr-xr-xt/t1414-reflog-walk.sh139
-rwxr-xr-xt/t1415-worktree-refs.sh116
-rwxr-xr-xt/t1416-ref-transaction-hooks.sh137
-rwxr-xr-xt/t1417-reflog-updateref.sh65
-rwxr-xr-xt/t1418-reflog-exists.sh38
-rwxr-xr-xt/t1419-exclude-refs.sh122
-rwxr-xr-xt/t1420-lost-found.sh37
-rwxr-xr-xt/t1430-bad-ref-name.sh393
-rwxr-xr-xt/t1450-fsck.sh1063
-rwxr-xr-xt/t1451-fsck-buffer.sh142
-rwxr-xr-xt/t1500-rev-parse.sh290
-rwxr-xr-xt/t1501-work-tree.sh448
-rwxr-xr-xt/t1502-rev-parse-parseopt.sh325
-rw-r--r--t/t1502/.gitattributes1
-rw-r--r--t/t1502/optionspec-neg8
-rw-r--r--t/t1502/optionspec-neg.help12
-rwxr-xr-xt/t1502/optionspec.help36
-rwxr-xr-xt/t1503-rev-parse-verify.sh165
-rwxr-xr-xt/t1504-ceiling-dirs.sh187
-rwxr-xr-xt/t1505-rev-parse-last.sh65
-rwxr-xr-xt/t1506-rev-parse-diagnosis.sh286
-rwxr-xr-xt/t1507-rev-parse-upstream.sh281
-rwxr-xr-xt/t1508-at-combinations.sh119
-rwxr-xr-xt/t1509-root-work-tree.sh264
-rw-r--r--t/t1509/excludes14
-rwxr-xr-xt/t1509/prepare-chroot.sh58
-rwxr-xr-xt/t1510-repo-setup.sh806
-rwxr-xr-xt/t1511-rev-parse-caret.sh134
-rwxr-xr-xt/t1512-rev-parse-disambiguation.sh479
-rwxr-xr-xt/t1513-rev-parse-prefix.sh100
-rwxr-xr-xt/t1514-rev-parse-push.sh77
-rwxr-xr-xt/t1515-rev-parse-outside-repo.sh47
-rwxr-xr-xt/t1600-index.sh137
-rwxr-xr-xt/t1601-index-bogus.sh22
-rwxr-xr-xt/t1700-split-index.sh547
-rwxr-xr-xt/t1701-racy-split-index.sh215
-rwxr-xr-xt/t1800-hook.sh188
-rwxr-xr-xt/t2000-conflict-when-checking-files-out.sh136
-rwxr-xr-xt/t2002-checkout-cache-u.sh34
-rwxr-xr-xt/t2003-checkout-cache-mkdir.sh120
-rwxr-xr-xt/t2004-checkout-cache-temp.sh250
-rwxr-xr-xt/t2005-checkout-index-symlinks.sh31
-rwxr-xr-xt/t2006-checkout-index-basic.sh104
-rwxr-xr-xt/t2007-checkout-symlink.sh56
-rwxr-xr-xt/t2008-checkout-subdir.sh83
-rwxr-xr-xt/t2009-checkout-statinfo.sh56
-rwxr-xr-xt/t2010-checkout-ambiguous.sh69
-rwxr-xr-xt/t2011-checkout-invalid-head.sh65
-rwxr-xr-xt/t2012-checkout-last.sh170
-rwxr-xr-xt/t2013-checkout-submodule.sh75
-rwxr-xr-xt/t2014-checkout-switch.sh30
-rwxr-xr-xt/t2015-checkout-unborn.sh65
-rwxr-xr-xt/t2016-checkout-patch.sh127
-rwxr-xr-xt/t2017-checkout-orphan.sh138
-rwxr-xr-xt/t2018-checkout-branch.sh289
-rwxr-xr-xt/t2019-checkout-ambiguous-ref.sh61
-rwxr-xr-xt/t2020-checkout-detach.sh336
-rwxr-xr-xt/t2021-checkout-overwrite.sh83
-rwxr-xr-xt/t2022-checkout-paths.sh96
-rwxr-xr-xt/t2023-checkout-m.sh77
-rwxr-xr-xt/t2024-checkout-dwim.sh350
-rwxr-xr-xt/t2025-checkout-no-overlay.sh60
-rwxr-xr-xt/t2026-checkout-pathspec-file.sh164
-rwxr-xr-xt/t2027-checkout-track.sh51
-rwxr-xr-xt/t2030-unresolve-info.sh305
-rwxr-xr-xt/t2050-git-dir-relative.sh56
-rwxr-xr-xt/t2060-switch.sh178
-rwxr-xr-xt/t2070-restore.sh223
-rwxr-xr-xt/t2071-restore-patch.sh118
-rwxr-xr-xt/t2072-restore-pathspec-file.sh177
-rwxr-xr-xt/t2080-parallel-checkout-basics.sh277
-rwxr-xr-xt/t2081-parallel-checkout-collisions.sh163
-rwxr-xr-xt/t2082-parallel-checkout-attributes.sh191
-rwxr-xr-xt/t2100-update-cache-badpath.sh62
-rwxr-xr-xt/t2101-update-index-reupdate.sh92
-rwxr-xr-xt/t2102-update-index-symlinks.sh32
-rwxr-xr-xt/t2103-update-index-ignore-missing.sh90
-rwxr-xr-xt/t2104-update-index-skip-worktree.sh67
-rwxr-xr-xt/t2105-update-index-gitfile.sh39
-rwxr-xr-xt/t2106-update-index-assume-unchanged.sh28
-rwxr-xr-xt/t2107-update-index-basic.sh145
-rwxr-xr-xt/t2108-update-index-refresh-racy.sh64
-rwxr-xr-xt/t2200-add-update.sh211
-rwxr-xr-xt/t2201-add-update-typechange.sh149
-rwxr-xr-xt/t2202-add-addremove.sh55
-rwxr-xr-xt/t2203-add-intent.sh315
-rwxr-xr-xt/t2204-add-ignored.sh93
-rwxr-xr-xt/t2205-add-worktree-config.sh266
-rwxr-xr-xt/t2300-cd-to-toplevel.sh47
-rwxr-xr-xt/t2400-worktree-add.sh1185
-rwxr-xr-xt/t2401-worktree-prune.sh123
-rwxr-xr-xt/t2402-worktree-list.sh289
-rwxr-xr-xt/t2403-worktree-move.sh250
-rwxr-xr-xt/t2404-worktree-config.sh82
-rwxr-xr-xt/t2405-worktree-submodule.sh94
-rwxr-xr-xt/t2406-worktree-repair.sh200
-rwxr-xr-xt/t2407-worktree-heads.sh180
-rwxr-xr-xt/t2500-untracked-overwriting.sh244
-rwxr-xr-xt/t2501-cwd-empty.sh277
-rwxr-xr-xt/t3000-ls-files-others.sh233
-rwxr-xr-xt/t3001-ls-files-others-exclude.sh307
-rwxr-xr-xt/t3002-ls-files-dashpath.sh77
-rwxr-xr-xt/t3003-ls-files-exclude.sh42
-rwxr-xr-xt/t3004-ls-files-basic.sh55
-rwxr-xr-xt/t3005-ls-files-relative.sh63
-rwxr-xr-xt/t3006-ls-files-long.sh41
-rwxr-xr-xt/t3007-ls-files-recurse-submodules.sh348
-rwxr-xr-xt/t3008-ls-files-lazy-init-name-hash.sh28
-rwxr-xr-xt/t3009-ls-files-others-nonsubmodule.sh51
-rwxr-xr-xt/t3010-ls-files-killed-modified.sh128
-rwxr-xr-xt/t3011-common-prefixes-and-directory-traversal.sh209
-rwxr-xr-xt/t3012-ls-files-dedup.sh67
-rwxr-xr-xt/t3013-ls-files-format.sh146
-rwxr-xr-xt/t3020-ls-files-error-unmatch.sh30
-rwxr-xr-xt/t3040-subprojects-basic.sh84
-rwxr-xr-xt/t3050-subprojects-fetch.sh53
-rwxr-xr-xt/t3060-ls-files-with-tree.sh106
-rwxr-xr-xt/t3070-wildmatch.sh445
-rwxr-xr-xt/t3100-ls-tree-restrict.sh167
-rwxr-xr-xt/t3101-ls-tree-dirname.sh242
-rwxr-xr-xt/t3102-ls-tree-wildcards.sh37
-rwxr-xr-xt/t3103-ls-tree-misc.sh41
-rwxr-xr-xt/t3104-ls-tree-format.sh80
-rwxr-xr-xt/t3105-ls-tree-output.sh192
-rwxr-xr-xt/t3200-branch.sh1663
-rwxr-xr-xt/t3201-branch-contains.sh267
-rwxr-xr-xt/t3202-show-branch.sh267
-rwxr-xr-xt/t3203-branch-output.sh459
-rwxr-xr-xt/t3204-branch-name-interpretation.sh171
-rwxr-xr-xt/t3205-branch-color.sh45
-rwxr-xr-xt/t3206-range-diff.sh897
-rw-r--r--t/t3206/history.export709
-rwxr-xr-xt/t3207-branch-submodule.sh329
-rwxr-xr-xt/t3210-pack-refs.sh297
-rwxr-xr-xt/t3211-peel-ref.sh77
-rwxr-xr-xt/t3300-funny-names.sh218
-rwxr-xr-xt/t3301-notes.sh1559
-rwxr-xr-xt/t3302-notes-index-expensive.sh139
-rwxr-xr-xt/t3303-notes-subtrees.sh199
-rwxr-xr-xt/t3304-notes-mixed.sh210
-rwxr-xr-xt/t3305-notes-fanout.sh141
-rwxr-xr-xt/t3306-notes-prune.sh138
-rwxr-xr-xt/t3307-notes-man.sh39
-rwxr-xr-xt/t3308-notes-merge.sh394
-rwxr-xr-xt/t3309-notes-merge-auto-resolve.sh791
-rwxr-xr-xt/t3310-notes-merge-manual-resolve.sh617
-rwxr-xr-xt/t3311-notes-merge-fanout.sh446
-rwxr-xr-xt/t3320-notes-merge-worktrees.sh78
-rwxr-xr-xt/t3321-notes-stripspace.sh578
-rwxr-xr-xt/t3400-rebase.sh458
-rwxr-xr-xt/t3401-rebase-and-am-rename.sh213
-rwxr-xr-xt/t3402-rebase-merge.sh225
-rwxr-xr-xt/t3403-rebase-skip.sh202
-rwxr-xr-xt/t3404-rebase-interactive.sh2211
-rwxr-xr-xt/t3405-rebase-malformed.sh94
-rwxr-xr-xt/t3406-rebase-message.sh258
-rwxr-xr-xt/t3407-rebase-abort.sh116
-rwxr-xr-xt/t3408-rebase-multi-line.sh59
-rwxr-xr-xt/t3409-rebase-environ.sh24
-rwxr-xr-xt/t3412-rebase-root.sh242
-rwxr-xr-xt/t3413-rebase-hook.sh140
-rwxr-xr-xt/t3415-rebase-autosquash.sh469
-rwxr-xr-xt/t3416-rebase-onto-threedots.sh229
-rwxr-xr-xt/t3417-rebase-whitespace-fix.sh124
-rwxr-xr-xt/t3418-rebase-continue.sh357
-rwxr-xr-xt/t3419-rebase-patch-id.sh118
-rwxr-xr-xt/t3420-rebase-autostash.sh336
-rwxr-xr-xt/t3421-rebase-topology-linear.sh359
-rwxr-xr-xt/t3422-rebase-incompatible-options.sh146
-rwxr-xr-xt/t3423-rebase-reword.sh49
-rwxr-xr-xt/t3424-rebase-empty.sh170
-rwxr-xr-xt/t3425-rebase-topology-merges.sh111
-rwxr-xr-xt/t3426-rebase-submodule.sh64
-rwxr-xr-xt/t3427-rebase-subtree.sh93
-rwxr-xr-xt/t3428-rebase-signoff.sh85
-rwxr-xr-xt/t3429-rebase-edit-todo.sh91
-rwxr-xr-xt/t3430-rebase-merges.sh614
-rwxr-xr-xt/t3431-rebase-fork-point.sh116
-rwxr-xr-xt/t3432-rebase-fast-forward.sh142
-rwxr-xr-xt/t3433-rebase-across-mode-change.sh49
-rwxr-xr-xt/t3434-rebase-i18n.sh87
-rw-r--r--t/t3434/ISO8859-1.txt3
-rw-r--r--t/t3434/eucJP.txt4
-rwxr-xr-xt/t3435-rebase-gpg-sign.sh123
-rwxr-xr-xt/t3436-rebase-more-options.sh209
-rwxr-xr-xt/t3437-rebase-fixup-options.sh223
-rw-r--r--t/t3437/expected-combined-message21
-rw-r--r--t/t3437/expected-squash-message51
-rwxr-xr-xt/t3438-rebase-broken-files.sh61
-rwxr-xr-xt/t3500-cherry.sh81
-rwxr-xr-xt/t3501-revert-cherry-pick.sh237
-rwxr-xr-xt/t3502-cherry-pick-merge.sh136
-rwxr-xr-xt/t3503-cherry-pick-root.sh82
-rwxr-xr-xt/t3504-cherry-pick-rerere.sh106
-rwxr-xr-xt/t3505-cherry-pick-empty.sh102
-rwxr-xr-xt/t3506-cherry-pick-ff.sh124
-rwxr-xr-xt/t3507-cherry-pick-conflict.sh588
-rwxr-xr-xt/t3508-cherry-pick-many-commits.sh199
-rwxr-xr-xt/t3509-cherry-pick-merge-df.sh104
-rwxr-xr-xt/t3510-cherry-pick-sequence.sh692
-rwxr-xr-xt/t3511-cherry-pick-x.sh322
-rwxr-xr-xt/t3512-cherry-pick-submodule.sh56
-rwxr-xr-xt/t3513-revert-submodule.sh39
-rwxr-xr-xt/t3514-cherry-pick-revert-gpg.sh86
-rwxr-xr-xt/t3600-rm.sh908
-rwxr-xr-xt/t3601-rm-pathspec-file.sh80
-rwxr-xr-xt/t3602-rm-sparse-checkout.sh139
-rwxr-xr-xt/t3700-add.sh509
-rwxr-xr-xt/t3701-add-interactive.sh1133
-rwxr-xr-xt/t3702-add-edit.sh129
-rwxr-xr-xt/t3703-add-magic-pathspec.sh59
-rwxr-xr-xt/t3704-add-pathspec-file.sh160
-rwxr-xr-xt/t3705-add-sparse-checkout.sh236
-rwxr-xr-xt/t3800-mktag.sh587
-rwxr-xr-xt/t3900-i18n-commit.sh240
-rw-r--r--t/t3900/1-UTF-8.txt3
-rw-r--r--t/t3900/2-UTF-8.txt4
-rw-r--r--t/t3900/ISO-2022-JP.txt4
-rw-r--r--t/t3900/ISO8859-1.txt3
-rw-r--r--t/t3900/UTF-16.txtbin0 -> 146 bytes
-rw-r--r--t/t3900/eucJP.txt4
-rwxr-xr-xt/t3901-i18n-patch.sh319
-rwxr-xr-xt/t3901/8859-1.txt4
-rwxr-xr-xt/t3901/utf8.txt4
-rwxr-xr-xt/t3902-quoted.sh153
-rwxr-xr-xt/t3903-stash.sh1519
-rwxr-xr-xt/t3904-stash-patch.sh115
-rwxr-xr-xt/t3905-stash-include-untracked.sh431
-rwxr-xr-xt/t3906-stash-submodule.sh69
-rwxr-xr-xt/t3907-stash-show-config.sh83
-rwxr-xr-xt/t3908-stash-in-worktree.sh28
-rwxr-xr-xt/t3909-stash-pathspec-file.sh100
-rwxr-xr-xt/t3910-mac-os-precompose.sh223
-rwxr-xr-xt/t3920-crlf-messages.sh128
-rwxr-xr-xt/t4000-diff-format.sh126
-rwxr-xr-xt/t4001-diff-rename.sh289
-rwxr-xr-xt/t4002-diff-basic.sh414
-rwxr-xr-xt/t4003-diff-rename-1.sh130
-rwxr-xr-xt/t4004-diff-rename-symlink.sh71
-rwxr-xr-xt/t4005-diff-rename-2.sh79
-rwxr-xr-xt/t4006-diff-mode.sh68
-rwxr-xr-xt/t4007-rename-3.sh92
-rwxr-xr-xt/t4008-diff-break-rewrite.sh159
-rwxr-xr-xt/t4009-diff-rename-4.sh100
-rwxr-xr-xt/t4010-diff-pathspec.sh154
-rwxr-xr-xt/t4011-diff-symlink.sh177
-rwxr-xr-xt/t4012-diff-binary.sh134
-rwxr-xr-xt/t4013-diff-various.sh669
-rw-r--r--t/t4013/diff.config_format.subjectprefix_DIFFERENT_PREFIX2
-rw-r--r--t/t4013/diff.diff-tree_--cc_--patch-with-stat_--summary_master34
-rw-r--r--t/t4013/diff.diff-tree_--cc_--patch-with-stat_--summary_side39
-rw-r--r--t/t4013/diff.diff-tree_--cc_--patch-with-stat_master34
-rw-r--r--t/t4013/diff.diff-tree_--cc_--shortstat_master4
-rw-r--r--t/t4013/diff.diff-tree_--cc_--stat_--summary_master6
-rw-r--r--t/t4013/diff.diff-tree_--cc_--stat_--summary_side8
-rw-r--r--t/t4013/diff.diff-tree_--cc_--stat_master6
-rw-r--r--t/t4013/diff.diff-tree_--cc_--summary_REVERSE6
-rw-r--r--t/t4013/diff.diff-tree_--cc_master30
-rw-r--r--t/t4013/diff.diff-tree_--format=%N_note6
-rw-r--r--t/t4013/diff.diff-tree_--patch-with-raw_initial2
-rw-r--r--t/t4013/diff.diff-tree_--patch-with-stat_initial2
-rw-r--r--t/t4013/diff.diff-tree_--pretty=oneline_--patch-with-raw_initial2
-rw-r--r--t/t4013/diff.diff-tree_--pretty=oneline_--patch-with-stat_initial2
-rw-r--r--t/t4013/diff.diff-tree_--pretty=oneline_--root_--patch-with-raw_initial33
-rw-r--r--t/t4013/diff.diff-tree_--pretty=oneline_--root_--patch-with-stat_initial34
-rw-r--r--t/t4013/diff.diff-tree_--pretty=oneline_--root_-p_initial29
-rw-r--r--t/t4013/diff.diff-tree_--pretty=oneline_--root_initial6
-rw-r--r--t/t4013/diff.diff-tree_--pretty=oneline_-p_initial2
-rw-r--r--t/t4013/diff.diff-tree_--pretty=oneline_initial2
-rw-r--r--t/t4013/diff.diff-tree_--pretty_--notes_note12
-rw-r--r--t/t4013/diff.diff-tree_--pretty_--patch-with-raw_initial2
-rw-r--r--t/t4013/diff.diff-tree_--pretty_--patch-with-stat_initial2
-rw-r--r--t/t4013/diff.diff-tree_--pretty_--patch-with-stat_side43
-rw-r--r--t/t4013/diff.diff-tree_--pretty_--root_--patch-with-raw_initial38
-rw-r--r--t/t4013/diff.diff-tree_--pretty_--root_--patch-with-stat_initial39
-rw-r--r--t/t4013/diff.diff-tree_--pretty_--root_--stat_--compact-summary_initial12
-rw-r--r--t/t4013/diff.diff-tree_--pretty_--root_--stat_--summary_initial15
-rw-r--r--t/t4013/diff.diff-tree_--pretty_--root_--stat_initial12
-rw-r--r--t/t4013/diff.diff-tree_--pretty_--root_--summary_-r_initial11
-rw-r--r--t/t4013/diff.diff-tree_--pretty_--root_--summary_initial11
-rw-r--r--t/t4013/diff.diff-tree_--pretty_--root_-p_initial34
-rw-r--r--t/t4013/diff.diff-tree_--pretty_--root_initial11
-rw-r--r--t/t4013/diff.diff-tree_--pretty_--stat_--summary_initial2
-rw-r--r--t/t4013/diff.diff-tree_--pretty_--stat_initial2
-rw-r--r--t/t4013/diff.diff-tree_--pretty_--summary_initial2
-rw-r--r--t/t4013/diff.diff-tree_--pretty_-R_--root_--stat_--compact-summary_initial12
-rw-r--r--t/t4013/diff.diff-tree_--pretty_-p_initial2
-rw-r--r--t/t4013/diff.diff-tree_--pretty_-p_side38
-rw-r--r--t/t4013/diff.diff-tree_--pretty_initial2
-rw-r--r--t/t4013/diff.diff-tree_--pretty_note9
-rw-r--r--t/t4013/diff.diff-tree_--pretty_side11
-rw-r--r--t/t4013/diff.diff-tree_--root_--abbrev_initial6
-rw-r--r--t/t4013/diff.diff-tree_--root_--patch-with-raw_initial33
-rw-r--r--t/t4013/diff.diff-tree_--root_--patch-with-stat_initial34
-rw-r--r--t/t4013/diff.diff-tree_--root_-p_--abbrev=10_initial29
-rw-r--r--t/t4013/diff.diff-tree_--root_-p_--full-index_--abbrev=10_initial29
-rw-r--r--t/t4013/diff.diff-tree_--root_-p_--full-index_initial29
-rw-r--r--t/t4013/diff.diff-tree_--root_-p_initial29
-rw-r--r--t/t4013/diff.diff-tree_--root_-r_--abbrev=4_initial6
-rw-r--r--t/t4013/diff.diff-tree_--root_-r_--abbrev_initial6
-rw-r--r--t/t4013/diff.diff-tree_--root_-r_initial6
-rw-r--r--t/t4013/diff.diff-tree_--root_initial6
-rw-r--r--t/t4013/diff.diff-tree_--stat_--compact-summary_initial_mode4
-rw-r--r--t/t4013/diff.diff-tree_--stat_initial_mode4
-rw-r--r--t/t4013/diff.diff-tree_--summary_initial_mode3
-rw-r--r--t/t4013/diff.diff-tree_-R_--stat_--compact-summary_initial_mode4
-rw-r--r--t/t4013/diff.diff-tree_-c_--abbrev_master5
-rw-r--r--t/t4013/diff.diff-tree_-c_--stat_--summary_master6
-rw-r--r--t/t4013/diff.diff-tree_-c_--stat_--summary_side8
-rw-r--r--t/t4013/diff.diff-tree_-c_--stat_master6
-rw-r--r--t/t4013/diff.diff-tree_-c_master5
-rw-r--r--t/t4013/diff.diff-tree_-m_master11
-rw-r--r--t/t4013/diff.diff-tree_-p_-m_master80
-rw-r--r--t/t4013/diff.diff-tree_-p_initial2
-rw-r--r--t/t4013/diff.diff-tree_-p_master2
-rw-r--r--t/t4013/diff.diff-tree_-r_--abbrev=4_initial2
-rw-r--r--t/t4013/diff.diff-tree_-r_--abbrev_initial2
-rw-r--r--t/t4013/diff.diff-tree_-r_initial2
-rw-r--r--t/t4013/diff.diff-tree_initial2
-rw-r--r--t/t4013/diff.diff-tree_initial_mode3
-rw-r--r--t/t4013/diff.diff-tree_master2
-rw-r--r--t/t4013/diff.diff_--abbrev_initial..side32
-rw-r--r--t/t4013/diff.diff_--cached38
-rw-r--r--t/t4013/diff.diff_--cached_--_file015
-rw-r--r--t/t4013/diff.diff_--dirstat-by-file_initial_rearrange3
-rw-r--r--t/t4013/diff.diff_--dirstat_--cc_master~1_master3
-rw-r--r--t/t4013/diff.diff_--dirstat_initial_rearrange3
-rw-r--r--t/t4013/diff.diff_--dirstat_master~1_master~23
-rw-r--r--t/t4013/diff.diff_--line-prefix=abc_master_master^_side29
-rw-r--r--t/t4013/diff.diff_--line-prefix_--cached_--_file015
-rw-r--r--t/t4013/diff.diff_--name-status_dir2_dir2
-rw-r--r--t/t4013/diff.diff_--no-index_--name-status_--_dir2_dir3
-rw-r--r--t/t4013/diff.diff_--no-index_--name-status_dir2_dir3
-rw-r--r--t/t4013/diff.diff_--no-index_--raw_--abbrev=4_dir2_dir3
-rw-r--r--t/t4013/diff.diff_--no-index_--raw_--no-abbrev_dir2_dir3
-rw-r--r--t/t4013/diff.diff_--no-index_--raw_dir2_dir3
-rw-r--r--t/t4013/diff.diff_--no-index_dir_dir32
-rw-r--r--t/t4013/diff.diff_--patch-with-raw_-r_initial..side36
-rw-r--r--t/t4013/diff.diff_--patch-with-raw_initial..side36
-rw-r--r--t/t4013/diff.diff_--patch-with-stat_-r_initial..side37
-rw-r--r--t/t4013/diff.diff_--patch-with-stat_initial..side37
-rw-r--r--t/t4013/diff.diff_--raw_--abbrev=4_initial6
-rw-r--r--t/t4013/diff.diff_--raw_--no-abbrev_initial6
-rw-r--r--t/t4013/diff.diff_--raw_initial6
-rw-r--r--t/t4013/diff.diff_--stat_initial..side6
-rw-r--r--t/t4013/diff.diff_-U1_initial..side29
-rw-r--r--t/t4013/diff.diff_-U2_initial..side31
-rw-r--r--t/t4013/diff.diff_-U_initial..side32
-rw-r--r--t/t4013/diff.diff_-r_--stat_initial..side6
-rw-r--r--t/t4013/diff.diff_-r_initial..side32
-rw-r--r--t/t4013/diff.diff_initial..side32
-rw-r--r--t/t4013/diff.diff_master_master^_side29
-rw-r--r--t/t4013/diff.format-patch_--attach_--stdout_--suffix=.diff_initial..side61
-rw-r--r--t/t4013/diff.format-patch_--attach_--stdout_initial..master170
-rw-r--r--t/t4013/diff.format-patch_--attach_--stdout_initial..master^110
-rw-r--r--t/t4013/diff.format-patch_--attach_--stdout_initial..side61
-rw-r--r--t/t4013/diff.format-patch_--inline_--stdout_--numbered-files_initial..master170
-rw-r--r--t/t4013/diff.format-patch_--inline_--stdout_--subject-prefix=TESTCASE_initial..master170
-rw-r--r--t/t4013/diff.format-patch_--inline_--stdout_initial..master170
-rw-r--r--t/t4013/diff.format-patch_--inline_--stdout_initial..master^110
-rw-r--r--t/t4013/diff.format-patch_--inline_--stdout_initial..master^^62
-rw-r--r--t/t4013/diff.format-patch_--inline_--stdout_initial..side61
-rw-r--r--t/t4013/diff.format-patch_--stdout_--cover-letter_-n_initial..master^103
-rw-r--r--t/t4013/diff.format-patch_--stdout_--no-numbered_initial..master127
-rw-r--r--t/t4013/diff.format-patch_--stdout_--numbered_initial..master127
-rw-r--r--t/t4013/diff.format-patch_--stdout_initial..master127
-rw-r--r--t/t4013/diff.format-patch_--stdout_initial..master^81
-rw-r--r--t/t4013/diff.format-patch_--stdout_initial..side47
-rw-r--r--t/t4013/diff.log_--cc_-m_-p_master200
-rw-r--r--t/t4013/diff.log_--decorate=full_--all61
-rw-r--r--t/t4013/diff.log_--decorate=full_--clear-decorations_--all61
-rw-r--r--t/t4013/diff.log_--decorate=full_--decorate-all_--all61
-rw-r--r--t/t4013/diff.log_--decorate_--all61
-rw-r--r--t/t4013/diff.log_--decorate_--clear-decorations_--all61
-rw-r--r--t/t4013/diff.log_--decorate_--decorate-all_--all61
-rw-r--r--t/t4013/diff.log_--diff-merges=first-parent_master56
-rw-r--r--t/t4013/diff.log_--diff-merges=off_-p_--first-parent_master78
-rw-r--r--t/t4013/diff.log_--first-parent_--diff-merges=off_-p_master78
-rw-r--r--t/t4013/diff.log_--no-diff-merges_-p_--first-parent_master78
-rw-r--r--t/t4013/diff.log_--patch-with-stat_--summary_master_--_dir_74
-rw-r--r--t/t4013/diff.log_--patch-with-stat_master129
-rw-r--r--t/t4013/diff.log_--patch-with-stat_master_--_dir_74
-rw-r--r--t/t4013/diff.log_--root_--cc_--patch-with-stat_--summary_master199
-rw-r--r--t/t4013/diff.log_--root_--patch-with-stat_--summary_master167
-rw-r--r--t/t4013/diff.log_--root_--patch-with-stat_master161
-rw-r--r--t/t4013/diff.log_--root_-c_--patch-with-stat_--summary_master199
-rw-r--r--t/t4013/diff.log_--root_-p_master142
-rw-r--r--t/t4013/diff.log_--root_master34
-rw-r--r--t/t4013/diff.log_-GF_-p_--pickaxe-all_master27
-rw-r--r--t/t4013/diff.log_-GF_-p_master18
-rw-r--r--t/t4013/diff.log_-GF_master7
-rw-r--r--t/t4013/diff.log_-IA_-IB_-I1_-I2_-p_master99
-rw-r--r--t/t4013/diff.log_-SF_-p_master18
-rw-r--r--t/t4013/diff.log_-SF_master7
-rw-r--r--t/t4013/diff.log_-SF_master_--max-count=02
-rw-r--r--t/t4013/diff.log_-SF_master_--max-count=17
-rw-r--r--t/t4013/diff.log_-SF_master_--max-count=27
-rw-r--r--t/t4013/diff.log_-S_F_master7
-rw-r--r--t/t4013/diff.log_-c_-m_-p_master200
-rw-r--r--t/t4013/diff.log_-m_--raw_master61
-rw-r--r--t/t4013/diff.log_-m_--stat_master66
-rw-r--r--t/t4013/diff.log_-m_-p_--first-parent_master100
-rw-r--r--t/t4013/diff.log_-m_-p_master200
-rw-r--r--t/t4013/diff.log_-p_--diff-merges=first-parent_master137
-rw-r--r--t/t4013/diff.log_-p_--first-parent_master100
-rw-r--r--t/t4013/diff.log_-p_master115
-rw-r--r--t/t4013/diff.log_master34
-rw-r--r--t/t4013/diff.noellipses-diff-tree_--root_--abbrev_initial6
-rw-r--r--t/t4013/diff.noellipses-diff-tree_--root_-r_--abbrev=4_initial6
-rw-r--r--t/t4013/diff.noellipses-diff-tree_--root_-r_--abbrev_initial6
-rw-r--r--t/t4013/diff.noellipses-diff-tree_-c_--abbrev_master5
-rw-r--r--t/t4013/diff.noellipses-diff_--no-index_--raw_--abbrev=4_dir2_dir3
-rw-r--r--t/t4013/diff.noellipses-diff_--no-index_--raw_dir2_dir3
-rw-r--r--t/t4013/diff.noellipses-diff_--patch-with-raw_-r_initial..side36
-rw-r--r--t/t4013/diff.noellipses-diff_--patch-with-raw_initial..side36
-rw-r--r--t/t4013/diff.noellipses-diff_--raw_--abbrev=4_initial6
-rw-r--r--t/t4013/diff.noellipses-diff_--raw_initial6
-rw-r--r--t/t4013/diff.noellipses-show_--patch-with-raw_side42
-rw-r--r--t/t4013/diff.noellipses-whatchanged_--root_master42
-rw-r--r--t/t4013/diff.noellipses-whatchanged_-SF_master9
-rw-r--r--t/t4013/diff.noellipses-whatchanged_master32
-rw-r--r--t/t4013/diff.rev-list_--children_HEAD7
-rw-r--r--t/t4013/diff.rev-list_--parents_HEAD7
-rw-r--r--t/t4013/diff.show_--first-parent_master30
-rw-r--r--t/t4013/diff.show_--patch-with-raw_side42
-rw-r--r--t/t4013/diff.show_--patch-with-stat_--summary_side44
-rw-r--r--t/t4013/diff.show_--patch-with-stat_side43
-rw-r--r--t/t4013/diff.show_--root_initial34
-rw-r--r--t/t4013/diff.show_--stat_--summary_side13
-rw-r--r--t/t4013/diff.show_--stat_side12
-rw-r--r--t/t4013/diff.show_-c_master36
-rw-r--r--t/t4013/diff.show_-m_master93
-rw-r--r--t/t4013/diff.show_initial7
-rw-r--r--t/t4013/diff.show_master36
-rw-r--r--t/t4013/diff.show_side38
-rw-r--r--t/t4013/diff.whatchanged_--patch-with-stat_--summary_master_--_dir_61
-rw-r--r--t/t4013/diff.whatchanged_--patch-with-stat_master116
-rw-r--r--t/t4013/diff.whatchanged_--patch-with-stat_master_--_dir_61
-rw-r--r--t/t4013/diff.whatchanged_--root_--cc_--patch-with-stat_--summary_master199
-rw-r--r--t/t4013/diff.whatchanged_--root_--patch-with-stat_--summary_master160
-rw-r--r--t/t4013/diff.whatchanged_--root_--patch-with-stat_master154
-rw-r--r--t/t4013/diff.whatchanged_--root_-c_--patch-with-stat_--summary_master199
-rw-r--r--t/t4013/diff.whatchanged_--root_-p_master135
-rw-r--r--t/t4013/diff.whatchanged_--root_master42
-rw-r--r--t/t4013/diff.whatchanged_-SF_-p_master18
-rw-r--r--t/t4013/diff.whatchanged_-SF_master9
-rw-r--r--t/t4013/diff.whatchanged_-p_master102
-rw-r--r--t/t4013/diff.whatchanged_master32
-rwxr-xr-xt/t4014-format-patch.sh2454
-rwxr-xr-xt/t4015-diff-whitespace.sh2380
-rwxr-xr-xt/t4016-diff-quote.sh89
-rwxr-xr-xt/t4017-diff-retval.sh146
-rwxr-xr-xt/t4018-diff-funcname.sh122
-rw-r--r--t/t4018/README15
-rw-r--r--t/t4018/bash-arithmetic-function4
-rw-r--r--t/t4018/bash-bashism-style-compact6
-rw-r--r--t/t4018/bash-bashism-style-function4
-rw-r--r--t/t4018/bash-bashism-style-whitespace4
-rw-r--r--t/t4018/bash-conditional-function4
-rw-r--r--t/t4018/bash-missing-parentheses6
-rw-r--r--t/t4018/bash-mixed-style-compact4
-rw-r--r--t/t4018/bash-mixed-style-function4
-rw-r--r--t/t4018/bash-nested-functions6
-rw-r--r--t/t4018/bash-other-characters4
-rw-r--r--t/t4018/bash-posix-style-compact4
-rw-r--r--t/t4018/bash-posix-style-function4
-rw-r--r--t/t4018/bash-posix-style-whitespace4
-rw-r--r--t/t4018/bash-subshell-function4
-rw-r--r--t/t4018/bash-trailing-comment4
-rw-r--r--t/t4018/cpp-c++-function4
-rw-r--r--t/t4018/cpp-class-constructor4
-rw-r--r--t/t4018/cpp-class-constructor-mem-init5
-rw-r--r--t/t4018/cpp-class-definition4
-rw-r--r--t/t4018/cpp-class-definition-derived5
-rw-r--r--t/t4018/cpp-class-destructor4
-rw-r--r--t/t4018/cpp-function-returning-global-type4
-rw-r--r--t/t4018/cpp-function-returning-nested5
-rw-r--r--t/t4018/cpp-function-returning-pointer4
-rw-r--r--t/t4018/cpp-function-returning-reference4
-rw-r--r--t/t4018/cpp-gnu-style-function5
-rw-r--r--t/t4018/cpp-namespace-definition4
-rw-r--r--t/t4018/cpp-operator-definition4
-rw-r--r--t/t4018/cpp-skip-access-specifiers8
-rw-r--r--t/t4018/cpp-skip-comment-block9
-rw-r--r--t/t4018/cpp-skip-labels8
-rw-r--r--t/t4018/cpp-struct-definition9
-rw-r--r--t/t4018/cpp-struct-single-line7
-rw-r--r--t/t4018/cpp-template-function-definition4
-rw-r--r--t/t4018/cpp-union-definition4
-rw-r--r--t/t4018/cpp-void-c-function4
-rw-r--r--t/t4018/css-attribute-value-selector4
-rw-r--r--t/t4018/css-block-level-@-statements10
-rw-r--r--t/t4018/css-brace-in-col-15
-rw-r--r--t/t4018/css-class-selector4
-rw-r--r--t/t4018/css-colon-eol4
-rw-r--r--t/t4018/css-colon-selector5
-rw-r--r--t/t4018/css-common4
-rw-r--r--t/t4018/css-id-selector4
-rw-r--r--t/t4018/css-long-selector-list6
-rw-r--r--t/t4018/css-prop-sans-indent5
-rw-r--r--t/t4018/css-root-selector4
-rw-r--r--t/t4018/css-short-selector-list4
-rw-r--r--t/t4018/css-trailing-space5
-rw-r--r--t/t4018/custom1-pattern17
-rw-r--r--t/t4018/custom2-match-to-end-of-line8
-rw-r--r--t/t4018/custom3-alternation-in-pattern17
-rw-r--r--t/t4018/dts-labels9
-rw-r--r--t/t4018/dts-node-unitless8
-rw-r--r--t/t4018/dts-nodes8
-rw-r--r--t/t4018/dts-nodes-boolean-prop9
-rw-r--r--t/t4018/dts-nodes-comment18
-rw-r--r--t/t4018/dts-nodes-comment28
-rw-r--r--t/t4018/dts-nodes-multiline-prop13
-rw-r--r--t/t4018/dts-reference9
-rw-r--r--t/t4018/dts-root5
-rw-r--r--t/t4018/dts-root-comment8
-rw-r--r--t/t4018/elixir-do-not-pick-end5
-rw-r--r--t/t4018/elixir-ex-unit-test6
-rw-r--r--t/t4018/elixir-function5
-rw-r--r--t/t4018/elixir-macro5
-rw-r--r--t/t4018/elixir-module9
-rw-r--r--t/t4018/elixir-module-func8
-rw-r--r--t/t4018/elixir-nested-module9
-rw-r--r--t/t4018/elixir-private-function5
-rw-r--r--t/t4018/elixir-protocol6
-rw-r--r--t/t4018/elixir-protocol-implementation5
-rw-r--r--t/t4018/fortran-block-data5
-rw-r--r--t/t4018/fortran-comment13
-rw-r--r--t/t4018/fortran-comment-keyword14
-rw-r--r--t/t4018/fortran-comment-legacy13
-rw-r--r--t/t4018/fortran-comment-legacy-star13
-rw-r--r--t/t4018/fortran-external-function9
-rw-r--r--t/t4018/fortran-external-subroutine5
-rw-r--r--t/t4018/fortran-module5
-rw-r--r--t/t4018/fortran-module-procedure13
-rw-r--r--t/t4018/fortran-program5
-rw-r--r--t/t4018/fountain-scene4
-rw-r--r--t/t4018/golang-complex-function8
-rw-r--r--t/t4018/golang-func4
-rw-r--r--t/t4018/golang-interface4
-rw-r--r--t/t4018/golang-long-func5
-rw-r--r--t/t4018/golang-struct4
-rw-r--r--t/t4018/java-class-brace-on-separate-line6
-rw-r--r--t/t4018/java-class-member-function12
-rw-r--r--t/t4018/java-class-space-before-type-parameters6
-rw-r--r--t/t4018/java-class-type-parameters6
-rw-r--r--t/t4018/java-class-type-parameters-implements6
-rw-r--r--t/t4018/java-enum-constant6
-rw-r--r--t/t4018/java-interface-type-parameters6
-rw-r--r--t/t4018/java-interface-type-parameters-extends6
-rw-r--r--t/t4018/java-method-return-generic-bounded9
-rw-r--r--t/t4018/java-method-return-generic-wildcard9
-rw-r--r--t/t4018/java-nested-field6
-rw-r--r--t/t4018/java-non-sealed8
-rw-r--r--t/t4018/java-record6
-rw-r--r--t/t4018/java-record-space-before-components6
-rw-r--r--t/t4018/java-record-type-parameters6
-rw-r--r--t/t4018/java-sealed7
-rw-r--r--t/t4018/java-sealed-permits6
-rw-r--r--t/t4018/java-sealed-type-parameters6
-rw-r--r--t/t4018/java-sealed-type-parameters-implements-permits6
-rw-r--r--t/t4018/java-sealed-type-parameters-permits6
-rw-r--r--t/t4018/kotlin-class5
-rw-r--r--t/t4018/kotlin-enum-class5
-rw-r--r--t/t4018/kotlin-fun5
-rw-r--r--t/t4018/kotlin-inheritace-class5
-rw-r--r--t/t4018/kotlin-inline-class5
-rw-r--r--t/t4018/kotlin-interface5
-rw-r--r--t/t4018/kotlin-nested-fun9
-rw-r--r--t/t4018/kotlin-public-class5
-rw-r--r--t/t4018/kotlin-sealed-class5
-rw-r--r--t/t4018/markdown-heading-indented6
-rw-r--r--t/t4018/markdown-heading-non-headings17
-rw-r--r--t/t4018/matlab-class-definition5
-rw-r--r--t/t4018/matlab-function4
-rw-r--r--t/t4018/matlab-octave-section-13
-rw-r--r--t/t4018/matlab-octave-section-23
-rw-r--r--t/t4018/matlab-section3
-rw-r--r--t/t4018/perl-skip-end-of-heredoc8
-rw-r--r--t/t4018/perl-skip-forward-decl10
-rw-r--r--t/t4018/perl-skip-sub-in-pod18
-rw-r--r--t/t4018/perl-sub-definition4
-rw-r--r--t/t4018/perl-sub-definition-kr-brace4
-rw-r--r--t/t4018/php-abstract-class4
-rw-r--r--t/t4018/php-abstract-method7
-rw-r--r--t/t4018/php-class4
-rw-r--r--t/t4018/php-enum4
-rw-r--r--t/t4018/php-final-class4
-rw-r--r--t/t4018/php-final-method7
-rw-r--r--t/t4018/php-function4
-rw-r--r--t/t4018/php-interface4
-rw-r--r--t/t4018/php-method7
-rw-r--r--t/t4018/php-trait7
-rw-r--r--t/t4018/python-async-def4
-rw-r--r--t/t4018/python-class4
-rw-r--r--t/t4018/python-def4
-rw-r--r--t/t4018/python-indented-async-def7
-rw-r--r--t/t4018/python-indented-class5
-rw-r--r--t/t4018/python-indented-def7
-rw-r--r--t/t4018/rust-fn5
-rw-r--r--t/t4018/rust-impl5
-rw-r--r--t/t4018/rust-macro-rules6
-rw-r--r--t/t4018/rust-struct5
-rw-r--r--t/t4018/rust-trait5
-rw-r--r--t/t4018/scheme-class7
-rw-r--r--t/t4018/scheme-def4
-rw-r--r--t/t4018/scheme-def-variant4
-rw-r--r--t/t4018/scheme-define-slash-public7
-rw-r--r--t/t4018/scheme-define-syntax8
-rw-r--r--t/t4018/scheme-define-variant4
-rw-r--r--t/t4018/scheme-library11
-rw-r--r--t/t4018/scheme-local-define4
-rw-r--r--t/t4018/scheme-module6
-rw-r--r--t/t4018/scheme-top-level-define4
-rw-r--r--t/t4018/scheme-user-defined-define6
-rwxr-xr-xt/t4019-diff-wserror.sh298
-rwxr-xr-xt/t4020-diff-external.sh274
-rw-r--r--t/t4020/diff.NULbin0 -> 116 bytes
-rwxr-xr-xt/t4021-format-patch-numbered.sh142
-rwxr-xr-xt/t4022-diff-rewrite.sh101
-rwxr-xr-xt/t4023-diff-rename-typechange.sh88
-rwxr-xr-xt/t4024-diff-optimize-common.sh158
-rwxr-xr-xt/t4025-hunk-header.sh39
-rwxr-xr-xt/t4026-color.sh159
-rwxr-xr-xt/t4027-diff-submodule.sh292
-rwxr-xr-xt/t4028-format-patch-mime-headers.sh32
-rwxr-xr-xt/t4029-diff-trailing-space.sh44
-rwxr-xr-xt/t4030-diff-textconv.sh173
-rwxr-xr-xt/t4031-diff-rewrite-binary.sh80
-rwxr-xr-xt/t4032-diff-inter-hunk-context.sh118
-rwxr-xr-xt/t4033-diff-patience.sh21
-rwxr-xr-xt/t4034-diff-words.sh416
-rw-r--r--t/t4034/ada/expect27
-rw-r--r--t/t4034/ada/post13
-rw-r--r--t/t4034/ada/pre13
-rw-r--r--t/t4034/bibtex/expect15
-rw-r--r--t/t4034/bibtex/post10
-rw-r--r--t/t4034/bibtex/pre9
-rw-r--r--t/t4034/cpp/expect35
-rw-r--r--t/t4034/cpp/post30
-rw-r--r--t/t4034/cpp/pre30
-rw-r--r--t/t4034/csharp/expect35
-rw-r--r--t/t4034/csharp/post18
-rw-r--r--t/t4034/csharp/pre18
-rw-r--r--t/t4034/css/expect16
-rw-r--r--t/t4034/css/post10
-rw-r--r--t/t4034/css/pre10
-rw-r--r--t/t4034/dts/expect37
-rw-r--r--t/t4034/dts/post32
-rw-r--r--t/t4034/dts/pre32
-rw-r--r--t/t4034/fortran/expect10
-rw-r--r--t/t4034/fortran/post5
-rw-r--r--t/t4034/fortran/pre5
-rw-r--r--t/t4034/html/expect8
-rw-r--r--t/t4034/html/post3
-rw-r--r--t/t4034/html/pre3
-rw-r--r--t/t4034/java/expect36
-rw-r--r--t/t4034/java/post19
-rw-r--r--t/t4034/java/pre19
-rw-r--r--t/t4034/kotlin/expect43
-rw-r--r--t/t4034/kotlin/post30
-rw-r--r--t/t4034/kotlin/pre30
-rw-r--r--t/t4034/matlab/expect14
-rw-r--r--t/t4034/matlab/post9
-rw-r--r--t/t4034/matlab/pre9
-rw-r--r--t/t4034/objc/expect35
-rw-r--r--t/t4034/objc/post18
-rw-r--r--t/t4034/objc/pre18
-rw-r--r--t/t4034/pascal/expect35
-rw-r--r--t/t4034/pascal/post18
-rw-r--r--t/t4034/pascal/pre18
-rw-r--r--t/t4034/perl/expect13
-rw-r--r--t/t4034/perl/post22
-rw-r--r--t/t4034/perl/pre22
-rw-r--r--t/t4034/php/expect35
-rw-r--r--t/t4034/php/post18
-rw-r--r--t/t4034/php/pre18
-rw-r--r--t/t4034/python/expect34
-rw-r--r--t/t4034/python/post17
-rw-r--r--t/t4034/python/pre17
-rw-r--r--t/t4034/ruby/expect34
-rw-r--r--t/t4034/ruby/post17
-rw-r--r--t/t4034/ruby/pre17
-rw-r--r--t/t4034/scheme/expect11
-rw-r--r--t/t4034/scheme/post6
-rw-r--r--t/t4034/scheme/pre6
-rw-r--r--t/t4034/tex/expect9
-rw-r--r--t/t4034/tex/post4
-rw-r--r--t/t4034/tex/pre4
-rwxr-xr-xt/t4035-diff-quiet.sh165
-rwxr-xr-xt/t4036-format-patch-signer-mime.sh51
-rwxr-xr-xt/t4037-diff-r-t-dirs.sh54
-rwxr-xr-xt/t4038-diff-combined.sh546
-rwxr-xr-xt/t4039-diff-assume-unchanged.sh43
-rwxr-xr-xt/t4040-whitespace-status.sh76
-rwxr-xr-xt/t4041-diff-submodule-option.sh551
-rwxr-xr-xt/t4042-diff-textconv-caching.sh121
-rwxr-xr-xt/t4043-diff-rename-binary.sh45
-rwxr-xr-xt/t4044-diff-index-unique-abbrev.sh57
-rwxr-xr-xt/t4045-diff-relative.sh249
-rwxr-xr-xt/t4046-diff-unmerged.sh99
-rwxr-xr-xt/t4047-diff-dirstat.sh993
-rwxr-xr-xt/t4048-diff-combined-binary.sh221
-rwxr-xr-xt/t4049-diff-stat-count.sh70
-rwxr-xr-xt/t4050-diff-histogram.sh13
-rwxr-xr-xt/t4051-diff-function-context.sh213
-rw-r--r--t/t4051/appended1.c15
-rw-r--r--t/t4051/appended2.c35
-rw-r--r--t/t4051/dummy.c7
-rw-r--r--t/t4051/hello.c24
-rw-r--r--t/t4051/includes.c20
-rwxr-xr-xt/t4052-stat-output.sh417
-rwxr-xr-xt/t4053-diff-no-index.sh287
-rwxr-xr-xt/t4054-diff-bogus-tree.sh84
-rwxr-xr-xt/t4055-diff-context.sh93
-rwxr-xr-xt/t4056-diff-order.sh200
-rwxr-xr-xt/t4057-diff-combined-paths.sh110
-rwxr-xr-xt/t4058-diff-duplicates.sh189
-rwxr-xr-xt/t4059-diff-submodule-not-initialized.sh127
-rwxr-xr-xt/t4060-diff-submodule-option-diff-format.sh976
-rwxr-xr-xt/t4061-diff-indent.sh371
-rwxr-xr-xt/t4062-diff-pickaxe.sh30
-rwxr-xr-xt/t4063-diff-blobs.sh98
-rwxr-xr-xt/t4064-diff-oidfind.sh123
-rwxr-xr-xt/t4065-diff-anchored.sh94
-rwxr-xr-xt/t4066-diff-emit-delay.sh83
-rwxr-xr-xt/t4067-diff-partial-clone.sh184
-rwxr-xr-xt/t4068-diff-symmetric-merge-base.sh203
-rwxr-xr-xt/t4069-remerge-diff.sh320
-rwxr-xr-xt/t4100-apply-stat.sh42
-rw-r--r--t/t4100/t-apply-1.expect11
-rw-r--r--t/t4100/t-apply-1.patch194
-rw-r--r--t/t4100/t-apply-2.expect5
-rw-r--r--t/t4100/t-apply-2.patch72
-rw-r--r--t/t4100/t-apply-3.expect7
-rw-r--r--t/t4100/t-apply-3.patch567
-rw-r--r--t/t4100/t-apply-4.expect5
-rw-r--r--t/t4100/t-apply-4.patch7
-rw-r--r--t/t4100/t-apply-5.expect19
-rw-r--r--t/t4100/t-apply-5.patch612
-rw-r--r--t/t4100/t-apply-6.expect5
-rw-r--r--t/t4100/t-apply-6.patch101
-rw-r--r--t/t4100/t-apply-7.expect6
-rw-r--r--t/t4100/t-apply-7.patch494
-rw-r--r--t/t4100/t-apply-8.expect2
-rw-r--r--t/t4100/t-apply-8.patch11
-rw-r--r--t/t4100/t-apply-9.expect2
-rw-r--r--t/t4100/t-apply-9.patch11
-rwxr-xr-xt/t4101-apply-nonl.sh33
-rw-r--r--t/t4101/diff.0-16
-rw-r--r--t/t4101/diff.0-27
-rw-r--r--t/t4101/diff.0-38
-rw-r--r--t/t4101/diff.1-06
-rw-r--r--t/t4101/diff.1-28
-rw-r--r--t/t4101/diff.1-38
-rw-r--r--t/t4101/diff.2-07
-rw-r--r--t/t4101/diff.2-18
-rw-r--r--t/t4101/diff.2-37
-rw-r--r--t/t4101/diff.3-08
-rw-r--r--t/t4101/diff.3-18
-rw-r--r--t/t4101/diff.3-27
-rwxr-xr-xt/t4102-apply-rename.sh59
-rwxr-xr-xt/t4103-apply-binary.sh184
-rwxr-xr-xt/t4104-apply-boundary.sh113
-rwxr-xr-xt/t4105-apply-fuzz.sh53
-rwxr-xr-xt/t4106-apply-stdin.sh31
-rwxr-xr-xt/t4107-apply-ignore-whitespace.sh195
-rwxr-xr-xt/t4108-apply-threeway.sh296
-rwxr-xr-xt/t4109-apply-multifrag.sh37
-rw-r--r--t/t4109/expect-131
-rw-r--r--t/t4109/expect-223
-rw-r--r--t/t4109/expect-324
-rw-r--r--t/t4109/patch1.patch28
-rw-r--r--t/t4109/patch2.patch30
-rw-r--r--t/t4109/patch3.patch31
-rw-r--r--t/t4109/patch4.patch30
-rwxr-xr-xt/t4110-apply-scan.sh24
-rw-r--r--t/t4110/expect20
-rw-r--r--t/t4110/patch1.patch17
-rw-r--r--t/t4110/patch2.patch11
-rw-r--r--t/t4110/patch3.patch14
-rw-r--r--t/t4110/patch4.patch11
-rw-r--r--t/t4110/patch5.patch11
-rwxr-xr-xt/t4111-apply-subdir.sh157
-rwxr-xr-xt/t4112-apply-renames.sh146
-rwxr-xr-xt/t4113-apply-ending.sh53
-rwxr-xr-xt/t4114-apply-typechange.sh130
-rwxr-xr-xt/t4115-apply-symlink.sh144
-rwxr-xr-xt/t4116-apply-reverse.sh93
-rwxr-xr-xt/t4117-apply-reject.sh107
-rwxr-xr-xt/t4118-apply-empty-context.sh53
-rwxr-xr-xt/t4119-apply-config.sh181
-rwxr-xr-xt/t4120-apply-popt.sh89
-rwxr-xr-xt/t4121-apply-diffs.sh36
-rwxr-xr-xt/t4122-apply-symlink-inside.sh158
-rwxr-xr-xt/t4123-apply-shrink.sh46
-rwxr-xr-xt/t4124-apply-ws-rule.sh559
-rwxr-xr-xt/t4125-apply-ws-fuzz.sh100
-rwxr-xr-xt/t4126-apply-empty.sh69
-rwxr-xr-xt/t4127-apply-same-fn.sh98
-rwxr-xr-xt/t4128-apply-root.sh123
-rwxr-xr-xt/t4129-apply-samemode.sh104
-rwxr-xr-xt/t4130-apply-criss-cross-rename.sh72
-rwxr-xr-xt/t4131-apply-fake-ancestor.sh42
-rwxr-xr-xt/t4132-apply-removal.sh98
-rwxr-xr-xt/t4133-apply-filenames.sh64
-rwxr-xr-xt/t4134-apply-submodule.sh40
-rwxr-xr-xt/t4135-apply-weird-filenames.sh102
-rw-r--r--t/t4135/.gitignore3
-rw-r--r--t/t4135/add-plain.diff5
-rw-r--r--t/t4135/add-with backslash.diff5
-rw-r--r--t/t4135/add-with quote.diff5
-rw-r--r--t/t4135/add-with spaces.diff5
-rw-r--r--t/t4135/add-with tab.diff5
-rw-r--r--t/t4135/damaged-tz.diff5
-rw-r--r--t/t4135/damaged.diff5
-rw-r--r--t/t4135/diff-plain.diff5
-rw-r--r--t/t4135/diff-with backslash.diff5
-rw-r--r--t/t4135/diff-with quote.diff5
-rw-r--r--t/t4135/diff-with spaces.diff5
-rw-r--r--t/t4135/diff-with tab.diff5
-rw-r--r--t/t4135/funny-tz.diff5
-rw-r--r--t/t4135/git-plain.diff7
-rw-r--r--t/t4135/git-with backslash.diff7
-rw-r--r--t/t4135/git-with quote.diff7
-rw-r--r--t/t4135/git-with spaces.diff7
-rw-r--r--t/t4135/git-with tab.diff7
-rwxr-xr-xt/t4135/make-patches45
-rwxr-xr-xt/t4136-apply-check.sh64
-rwxr-xr-xt/t4137-apply-submodule.sh24
-rwxr-xr-xt/t4138-apply-ws-expansion.sh121
-rwxr-xr-xt/t4139-apply-escape.sh143
-rwxr-xr-xt/t4140-apply-ita.sh57
-rwxr-xr-xt/t4141-apply-too-large.sh23
-rwxr-xr-xt/t4150-am.sh1282
-rwxr-xr-xt/t4151-am-abort.sh227
-rwxr-xr-xt/t4152-am-subjects.sh79
-rwxr-xr-xt/t4153-am-resume-override-opts.sh102
-rwxr-xr-xt/t4200-rerere.sh674
-rwxr-xr-xt/t4201-shortlog.sh401
-rwxr-xr-xt/t4202-log.sh2386
-rwxr-xr-xt/t4203-mailmap.sh1090
-rwxr-xr-xt/t4204-patch-id.sh313
-rwxr-xr-xt/t4205-log-pretty-formats.sh1201
-rwxr-xr-xt/t4206-log-follow-harder-copies.sh58
-rwxr-xr-xt/t4207-log-decoration-colors.sh128
-rwxr-xr-xt/t4208-log-magic-pathspec.sh145
-rwxr-xr-xt/t4209-log-pickaxe.sh226
-rwxr-xr-xt/t4210-log-i18n.sh134
-rwxr-xr-xt/t4211-line-log.sh340
-rw-r--r--t/t4211/history.export406
-rw-r--r--t/t4211/sha1/expect.beginning-of-file43
-rw-r--r--t/t4211/sha1/expect.end-of-file62
-rw-r--r--t/t4211/sha1/expect.move-support-f80
-rw-r--r--t/t4211/sha1/expect.multiple104
-rw-r--r--t/t4211/sha1/expect.multiple-overlapping187
-rw-r--r--t/t4211/sha1/expect.multiple-superset187
-rw-r--r--t/t4211/sha1/expect.parallel-change-f-to-main160
-rw-r--r--t/t4211/sha1/expect.simple-f59
-rw-r--r--t/t4211/sha1/expect.simple-f-to-main100
-rw-r--r--t/t4211/sha1/expect.simple-main68
-rw-r--r--t/t4211/sha1/expect.simple-main-to-end70
-rw-r--r--t/t4211/sha1/expect.two-ranges102
-rw-r--r--t/t4211/sha1/expect.vanishes-early39
-rw-r--r--t/t4211/sha256/expect.beginning-of-file43
-rw-r--r--t/t4211/sha256/expect.end-of-file62
-rw-r--r--t/t4211/sha256/expect.move-support-f80
-rw-r--r--t/t4211/sha256/expect.multiple104
-rw-r--r--t/t4211/sha256/expect.multiple-overlapping187
-rw-r--r--t/t4211/sha256/expect.multiple-superset187
-rw-r--r--t/t4211/sha256/expect.parallel-change-f-to-main160
-rw-r--r--t/t4211/sha256/expect.simple-f59
-rw-r--r--t/t4211/sha256/expect.simple-f-to-main100
-rw-r--r--t/t4211/sha256/expect.simple-main68
-rw-r--r--t/t4211/sha256/expect.simple-main-to-end70
-rw-r--r--t/t4211/sha256/expect.two-ranges102
-rw-r--r--t/t4211/sha256/expect.vanishes-early39
-rwxr-xr-xt/t4212-log-corrupt.sh136
-rwxr-xr-xt/t4213-log-tabexpand.sh106
-rwxr-xr-xt/t4214-log-graph-octopus.sh351
-rwxr-xr-xt/t4215-log-skewed-merges.sh374
-rwxr-xr-xt/t4216-log-bloom.sh457
-rwxr-xr-xt/t4217-log-limit.sh42
-rwxr-xr-xt/t4252-am-options.sh78
-rw-r--r--t/t4252/am-test-1-119
-rw-r--r--t/t4252/am-test-1-221
-rw-r--r--t/t4252/am-test-2-119
-rw-r--r--t/t4252/am-test-2-221
-rw-r--r--t/t4252/am-test-3-119
-rw-r--r--t/t4252/am-test-3-221
-rw-r--r--t/t4252/am-test-4-119
-rw-r--r--t/t4252/am-test-4-222
-rw-r--r--t/t4252/am-test-5-120
-rw-r--r--t/t4252/am-test-5-215
-rw-r--r--t/t4252/am-test-6-121
-rw-r--r--t/t4252/file-1-07
-rw-r--r--t/t4252/file-2-07
-rwxr-xr-xt/t4253-am-keep-cr-dos.sh101
-rwxr-xr-xt/t4254-am-corrupt.sh84
-rwxr-xr-xt/t4255-am-submodule.sh97
-rwxr-xr-xt/t4256-am-format-flowed.sh20
-rw-r--r--t/t4256/1/mailinfo.c1245
-rw-r--r--t/t4256/1/mailinfo.c.orig1185
-rw-r--r--t/t4256/1/patch129
-rwxr-xr-xt/t4257-am-interactive.sh54
-rwxr-xr-xt/t4258-am-quoted-cr.sh37
-rw-r--r--t/t4258/mbox12
-rwxr-xr-xt/t4300-merge-tree.sh382
-rwxr-xr-xt/t4301-merge-tree-write-tree.sh948
-rwxr-xr-xt/t5000-tar-tree.sh533
-rw-r--r--t/t5000/huge-and-future.tarbin0 -> 2048 bytes
-rw-r--r--t/t5000/huge-objectbin0 -> 2048 bytes
-rw-r--r--t/t5000/pax.tarbin0 -> 10240 bytes
-rwxr-xr-xt/t5001-archive-attr.sh165
-rwxr-xr-xt/t5002-archive-attr-pattern.sh88
-rwxr-xr-xt/t5003-archive-zip.sh242
-rw-r--r--t/t5003/infozip-symlinks.zipbin0 -> 328 bytes
-rwxr-xr-xt/t5004-archive-corner-cases.sh229
-rw-r--r--t/t5004/big-pack.zipbin0 -> 7373 bytes
-rw-r--r--t/t5004/empty-with-pax-header.tarbin0 -> 10240 bytes
-rw-r--r--t/t5004/empty.zipbin0 -> 62 bytes
-rwxr-xr-xt/t5100-mailinfo.sh271
-rw-r--r--t/t5100/.gitattributes4
-rw-r--r--t/t5100/0001mboxrd4
-rw-r--r--t/t5100/0002mboxrd5
-rw-r--r--t/t5100/comment.expect5
-rw-r--r--t/t5100/comment.in9
-rw-r--r--t/t5100/embed-from.expect5
-rw-r--r--t/t5100/embed-from.in13
-rw-r--r--t/t5100/empty0
-rw-r--r--t/t5100/info-from.expect5
-rw-r--r--t/t5100/info-from.in8
-rw-r--r--t/t5100/info00015
-rw-r--r--t/t5100/info00025
-rw-r--r--t/t5100/info00035
-rw-r--r--t/t5100/info00045
-rw-r--r--t/t5100/info00055
-rw-r--r--t/t5100/info00065
-rw-r--r--t/t5100/info00075
-rw-r--r--t/t5100/info00085
-rw-r--r--t/t5100/info00095
-rw-r--r--t/t5100/info00105
-rw-r--r--t/t5100/info00115
-rw-r--r--t/t5100/info00125
-rw-r--r--t/t5100/info0012--message-id5
-rw-r--r--t/t5100/info00135
-rw-r--r--t/t5100/info00145
-rw-r--r--t/t5100/info0014--scissors5
-rw-r--r--t/t5100/info00155
-rw-r--r--t/t5100/info0015--no-inbody-headers5
-rw-r--r--t/t5100/info00165
-rw-r--r--t/t5100/info0016--no-inbody-headers5
-rw-r--r--t/t5100/info00175
-rw-r--r--t/t5100/info00185
-rw-r--r--t/t5100/info0018--no-inbody-headers5
-rw-r--r--t/t5100/msg00012
-rw-r--r--t/t5100/msg000221
-rw-r--r--t/t5100/msg00039
-rw-r--r--t/t5100/msg00047
-rw-r--r--t/t5100/msg000513
-rw-r--r--t/t5100/msg00062
-rw-r--r--t/t5100/msg00072
-rw-r--r--t/t5100/msg00084
-rw-r--r--t/t5100/msg00092
-rw-r--r--t/t5100/msg00105
-rw-r--r--t/t5100/msg00112
-rw-r--r--t/t5100/msg00127
-rw-r--r--t/t5100/msg0012--message-id8
-rw-r--r--t/t5100/msg00130
-rw-r--r--t/t5100/msg001418
-rw-r--r--t/t5100/msg0014--scissors4
-rw-r--r--t/t5100/msg00150
-rw-r--r--t/t5100/msg0015--no-inbody-headers3
-rw-r--r--t/t5100/msg00162
-rw-r--r--t/t5100/msg0016--no-inbody-headers4
-rw-r--r--t/t5100/msg00172
-rw-r--r--t/t5100/msg00182
-rw-r--r--t/t5100/msg0018--no-inbody-headers8
-rw-r--r--t/t5100/nul-b64.expectbin0 -> 1672 bytes
-rw-r--r--t/t5100/nul-b64.in37
-rw-r--r--t/t5100/nul-plainbin0 -> 91 bytes
-rw-r--r--t/t5100/patch000114
-rw-r--r--t/t5100/patch000214
-rw-r--r--t/t5100/patch000314
-rw-r--r--t/t5100/patch000493
-rw-r--r--t/t5100/patch000569
-rw-r--r--t/t5100/patch000614
-rw-r--r--t/t5100/patch00070
-rw-r--r--t/t5100/patch00080
-rw-r--r--t/t5100/patch000913
-rw-r--r--t/t5100/patch001020
-rw-r--r--t/t5100/patch001122
-rw-r--r--t/t5100/patch001230
-rw-r--r--t/t5100/patch0012--message-id30
-rw-r--r--t/t5100/patch00130
-rw-r--r--t/t5100/patch001464
-rw-r--r--t/t5100/patch0014--scissors64
-rw-r--r--t/t5100/patch00158
-rw-r--r--t/t5100/patch0015--no-inbody-headers8
-rw-r--r--t/t5100/patch00168
-rw-r--r--t/t5100/patch0016--no-inbody-headers8
-rw-r--r--t/t5100/patch00176
-rw-r--r--t/t5100/patch00186
-rw-r--r--t/t5100/patch0018--no-inbody-headers6
-rw-r--r--t/t5100/quoted-cr-info5
-rw-r--r--t/t5100/quoted-cr-msg2
-rw-r--r--t/t5100/quoted-cr-patch22
-rw-r--r--t/t5100/quoted-cr.mbox47
-rw-r--r--t/t5100/quoted-from.expect3
-rw-r--r--t/t5100/quoted-from.in10
-rw-r--r--t/t5100/quoted-string.expect5
-rw-r--r--t/t5100/quoted-string.in9
-rw-r--r--t/t5100/rfc2047-info-00014
-rw-r--r--t/t5100/rfc2047-info-00024
-rw-r--r--t/t5100/rfc2047-info-00034
-rw-r--r--t/t5100/rfc2047-info-00044
-rw-r--r--t/t5100/rfc2047-info-00052
-rw-r--r--t/t5100/rfc2047-info-00062
-rw-r--r--t/t5100/rfc2047-info-00072
-rw-r--r--t/t5100/rfc2047-info-00082
-rw-r--r--t/t5100/rfc2047-info-00092
-rw-r--r--t/t5100/rfc2047-info-00102
-rw-r--r--t/t5100/rfc2047-info-00112
-rw-r--r--t/t5100/rfc2047-samples.mbox48
-rw-r--r--t/t5100/sample.mbox720
-rw-r--r--t/t5100/sample.mboxrd19
-rwxr-xr-xt/t5150-request-pull.sh309
-rwxr-xr-xt/t5200-update-server-info.sh42
-rwxr-xr-xt/t5300-pack-object.sh597
-rwxr-xr-xt/t5301-sliding-window.sh62
-rwxr-xr-xt/t5302-pack-index.sh297
-rwxr-xr-xt/t5303-pack-corruption-resilience.sh405
-rwxr-xr-xt/t5304-prune.sh367
-rwxr-xr-xt/t5305-include-tag.sh121
-rwxr-xr-xt/t5306-pack-nobase.sh78
-rwxr-xr-xt/t5307-pack-missing-commit.sh40
-rwxr-xr-xt/t5308-pack-detect-duplicates.sh83
-rwxr-xr-xt/t5309-pack-delta-cycles.sh79
-rwxr-xr-xt/t5310-pack-bitmaps.sh530
-rwxr-xr-xt/t5311-pack-bitmaps-shallow.sh58
-rwxr-xr-xt/t5312-prune-corruption.sh140
-rwxr-xr-xt/t5313-pack-bounds-checks.sh196
-rwxr-xr-xt/t5314-pack-cycle-detection.sh115
-rwxr-xr-xt/t5315-pack-objects-compression.sh39
-rwxr-xr-xt/t5316-pack-delta-depth.sh121
-rwxr-xr-xt/t5317-pack-objects-filter-objects.sh482
-rwxr-xr-xt/t5318-commit-graph.sh946
-rwxr-xr-xt/t5319-multi-pack-index.sh1160
-rw-r--r--t/t5319/no-objects.midxbin0 -> 1116 bytes
-rwxr-xr-xt/t5320-delta-islands.sh145
-rwxr-xr-xt/t5321-pack-large-objects.sh34
-rwxr-xr-xt/t5322-pack-objects-sparse.sh142
-rwxr-xr-xt/t5323-pack-redundant.sh498
-rwxr-xr-xt/t5324-split-commit-graph.sh721
-rwxr-xr-xt/t5325-reverse-index.sh208
-rwxr-xr-xt/t5326-multi-pack-bitmaps.sh516
-rwxr-xr-xt/t5327-multi-pack-bitmaps-rev.sh43
-rwxr-xr-xt/t5328-commit-graph-64bit-time.sh87
-rwxr-xr-xt/t5329-pack-objects-cruft.sh947
-rwxr-xr-xt/t5330-no-lazy-fetch-with-commit-graph.sh48
-rwxr-xr-xt/t5331-pack-objects-stdin.sh240
-rwxr-xr-xt/t5351-unpack-large-objects.sh103
-rwxr-xr-xt/t5400-send-pack.sh303
-rwxr-xr-xt/t5401-update-hooks.sh143
-rwxr-xr-xt/t5402-post-merge-hook.sh62
-rwxr-xr-xt/t5403-post-checkout-hook.sh116
-rwxr-xr-xt/t5404-tracking-branches.sh66
-rwxr-xr-xt/t5405-send-pack-rewind.sh46
-rwxr-xr-xt/t5406-remote-rejects.sh25
-rwxr-xr-xt/t5407-post-rewrite-hook.sh313
-rwxr-xr-xt/t5408-send-pack-stdin.sh92
-rwxr-xr-xt/t5409-colorize-remote-messages.sh101
-rwxr-xr-xt/t5410-receive-pack-alternates.sh45
-rwxr-xr-xt/t5411-proc-receive-hook.sh120
-rw-r--r--t/t5411/common-functions.sh73
-rw-r--r--t/t5411/once-0010-report-status-v1.sh91
-rw-r--r--t/t5411/test-0000-standard-git-push.sh134
-rw-r--r--t/t5411/test-0001-standard-git-push--porcelain.sh138
-rw-r--r--t/t5411/test-0002-pre-receive-declined.sh31
-rw-r--r--t/t5411/test-0003-pre-receive-declined--porcelain.sh32
-rw-r--r--t/t5411/test-0010-proc-receive-settings.sh7
-rw-r--r--t/t5411/test-0011-no-hook-error.sh60
-rw-r--r--t/t5411/test-0012-no-hook-error--porcelain.sh62
-rw-r--r--t/t5411/test-0013-bad-protocol.sh302
-rw-r--r--t/t5411/test-0014-bad-protocol--porcelain.sh304
-rw-r--r--t/t5411/test-0020-report-ng.sh63
-rw-r--r--t/t5411/test-0021-report-ng--porcelain.sh65
-rw-r--r--t/t5411/test-0022-report-unexpect-ref.sh43
-rw-r--r--t/t5411/test-0023-report-unexpect-ref--porcelain.sh44
-rw-r--r--t/t5411/test-0024-report-unknown-ref.sh32
-rw-r--r--t/t5411/test-0025-report-unknown-ref--porcelain.sh33
-rw-r--r--t/t5411/test-0026-push-options.sh133
-rw-r--r--t/t5411/test-0027-push-options--porcelain.sh138
-rw-r--r--t/t5411/test-0030-report-ok.sh33
-rw-r--r--t/t5411/test-0031-report-ok--porcelain.sh34
-rw-r--r--t/t5411/test-0032-report-with-options.sh253
-rw-r--r--t/t5411/test-0033-report-with-options--porcelain.sh262
-rw-r--r--t/t5411/test-0034-report-ft.sh42
-rw-r--r--t/t5411/test-0035-report-ft--porcelain.sh43
-rw-r--r--t/t5411/test-0036-report-multi-rewrite-for-one-ref.sh221
-rw-r--r--t/t5411/test-0037-report-multi-rewrite-for-one-ref--porcelain.sh166
-rw-r--r--t/t5411/test-0038-report-mixed-refs.sh87
-rw-r--r--t/t5411/test-0039-report-mixed-refs--porcelain.sh89
-rw-r--r--t/t5411/test-0040-process-all-refs.sh111
-rw-r--r--t/t5411/test-0041-process-all-refs--porcelain.sh112
-rw-r--r--t/t5411/test-0050-proc-receive-refs-with-modifiers.sh129
-rwxr-xr-xt/t5500-fetch-pack.sh1069
-rwxr-xr-xt/t5501-fetch-push-alternates.sh69
-rwxr-xr-xt/t5502-quickfetch.sh146
-rwxr-xr-xt/t5503-tagfollow.sh164
-rwxr-xr-xt/t5504-fetch-receive-strict.sh373
-rwxr-xr-xt/t5505-remote.sh1495
-rwxr-xr-xt/t5506-remote-groups.sh111
-rwxr-xr-xt/t5507-remote-environment.sh36
-rwxr-xr-xt/t5509-fetch-push-namespaces.sh169
-rwxr-xr-xt/t5510-fetch.sh1268
-rwxr-xr-xt/t5511-refspec.sh97
-rwxr-xr-xt/t5512-ls-remote.sh383
-rwxr-xr-xt/t5513-fetch-track.sh31
-rwxr-xr-xt/t5514-fetch-multiple.sh212
-rwxr-xr-xt/t5515-fetch-merge-logic.sh230
-rw-r--r--t/t5515/fetch.br-branches-default8
-rw-r--r--t/t5515/fetch.br-branches-default-merge9
-rw-r--r--t/t5515/fetch.br-branches-default-merge_branches-default9
-rw-r--r--t/t5515/fetch.br-branches-default-octopus10
-rw-r--r--t/t5515/fetch.br-branches-default-octopus_branches-default10
-rw-r--r--t/t5515/fetch.br-branches-default_branches-default8
-rw-r--r--t/t5515/fetch.br-branches-one8
-rw-r--r--t/t5515/fetch.br-branches-one-merge9
-rw-r--r--t/t5515/fetch.br-branches-one-merge_branches-one9
-rw-r--r--t/t5515/fetch.br-branches-one-octopus9
-rw-r--r--t/t5515/fetch.br-branches-one-octopus_branches-one9
-rw-r--r--t/t5515/fetch.br-branches-one_branches-one8
-rw-r--r--t/t5515/fetch.br-config-explicit11
-rw-r--r--t/t5515/fetch.br-config-explicit-merge11
-rw-r--r--t/t5515/fetch.br-config-explicit-merge_config-explicit11
-rw-r--r--t/t5515/fetch.br-config-explicit-octopus11
-rw-r--r--t/t5515/fetch.br-config-explicit-octopus_config-explicit11
-rw-r--r--t/t5515/fetch.br-config-explicit_config-explicit11
-rw-r--r--t/t5515/fetch.br-config-glob11
-rw-r--r--t/t5515/fetch.br-config-glob-merge11
-rw-r--r--t/t5515/fetch.br-config-glob-merge_config-glob11
-rw-r--r--t/t5515/fetch.br-config-glob-octopus11
-rw-r--r--t/t5515/fetch.br-config-glob-octopus_config-glob11
-rw-r--r--t/t5515/fetch.br-config-glob_config-glob11
-rw-r--r--t/t5515/fetch.br-remote-explicit11
-rw-r--r--t/t5515/fetch.br-remote-explicit-merge11
-rw-r--r--t/t5515/fetch.br-remote-explicit-merge_remote-explicit11
-rw-r--r--t/t5515/fetch.br-remote-explicit-octopus11
-rw-r--r--t/t5515/fetch.br-remote-explicit-octopus_remote-explicit11
-rw-r--r--t/t5515/fetch.br-remote-explicit_remote-explicit11
-rw-r--r--t/t5515/fetch.br-remote-glob11
-rw-r--r--t/t5515/fetch.br-remote-glob-merge11
-rw-r--r--t/t5515/fetch.br-remote-glob-merge_remote-glob11
-rw-r--r--t/t5515/fetch.br-remote-glob-octopus11
-rw-r--r--t/t5515/fetch.br-remote-glob-octopus_remote-glob11
-rw-r--r--t/t5515/fetch.br-remote-glob_remote-glob11
-rw-r--r--t/t5515/fetch.br-unconfig11
-rw-r--r--t/t5515/fetch.br-unconfig_--tags_.._.git8
-rw-r--r--t/t5515/fetch.br-unconfig_.._.git2
-rw-r--r--t/t5515/fetch.br-unconfig_.._.git_one2
-rw-r--r--t/t5515/fetch.br-unconfig_.._.git_one_tag_tag-one_tag_tag-three-file8
-rw-r--r--t/t5515/fetch.br-unconfig_.._.git_one_two3
-rw-r--r--t/t5515/fetch.br-unconfig_.._.git_tag_tag-one-tree_tag_tag-three-file7
-rw-r--r--t/t5515/fetch.br-unconfig_.._.git_tag_tag-one_tag_tag-three7
-rw-r--r--t/t5515/fetch.br-unconfig_branches-default8
-rw-r--r--t/t5515/fetch.br-unconfig_branches-one8
-rw-r--r--t/t5515/fetch.br-unconfig_config-explicit11
-rw-r--r--t/t5515/fetch.br-unconfig_config-glob11
-rw-r--r--t/t5515/fetch.br-unconfig_remote-explicit11
-rw-r--r--t/t5515/fetch.br-unconfig_remote-glob11
-rw-r--r--t/t5515/fetch.main11
-rw-r--r--t/t5515/fetch.main_--tags_.._.git8
-rw-r--r--t/t5515/fetch.main_.._.git2
-rw-r--r--t/t5515/fetch.main_.._.git_one2
-rw-r--r--t/t5515/fetch.main_.._.git_one_tag_tag-one_tag_tag-three-file8
-rw-r--r--t/t5515/fetch.main_.._.git_one_two3
-rw-r--r--t/t5515/fetch.main_.._.git_tag_tag-one-tree_tag_tag-three-file7
-rw-r--r--t/t5515/fetch.main_.._.git_tag_tag-one_tag_tag-three7
-rw-r--r--t/t5515/fetch.main_branches-default8
-rw-r--r--t/t5515/fetch.main_branches-one8
-rw-r--r--t/t5515/fetch.main_config-explicit11
-rw-r--r--t/t5515/fetch.main_config-glob11
-rw-r--r--t/t5515/fetch.main_remote-explicit11
-rw-r--r--t/t5515/fetch.main_remote-glob11
-rw-r--r--t/t5515/refs.br-branches-default12
-rw-r--r--t/t5515/refs.br-branches-default-merge12
-rw-r--r--t/t5515/refs.br-branches-default-merge_branches-default12
-rw-r--r--t/t5515/refs.br-branches-default-octopus12
-rw-r--r--t/t5515/refs.br-branches-default-octopus_branches-default12
-rw-r--r--t/t5515/refs.br-branches-default_branches-default12
-rw-r--r--t/t5515/refs.br-branches-one12
-rw-r--r--t/t5515/refs.br-branches-one-merge12
-rw-r--r--t/t5515/refs.br-branches-one-merge_branches-one12
-rw-r--r--t/t5515/refs.br-branches-one-octopus12
-rw-r--r--t/t5515/refs.br-branches-one-octopus_branches-one12
-rw-r--r--t/t5515/refs.br-branches-one_branches-one12
-rw-r--r--t/t5515/refs.br-config-explicit15
-rw-r--r--t/t5515/refs.br-config-explicit-merge15
-rw-r--r--t/t5515/refs.br-config-explicit-merge_config-explicit15
-rw-r--r--t/t5515/refs.br-config-explicit-octopus15
-rw-r--r--t/t5515/refs.br-config-explicit-octopus_config-explicit15
-rw-r--r--t/t5515/refs.br-config-explicit_config-explicit15
-rw-r--r--t/t5515/refs.br-config-glob15
-rw-r--r--t/t5515/refs.br-config-glob-merge15
-rw-r--r--t/t5515/refs.br-config-glob-merge_config-glob15
-rw-r--r--t/t5515/refs.br-config-glob-octopus15
-rw-r--r--t/t5515/refs.br-config-glob-octopus_config-glob15
-rw-r--r--t/t5515/refs.br-config-glob_config-glob15
-rw-r--r--t/t5515/refs.br-remote-explicit15
-rw-r--r--t/t5515/refs.br-remote-explicit-merge15
-rw-r--r--t/t5515/refs.br-remote-explicit-merge_remote-explicit15
-rw-r--r--t/t5515/refs.br-remote-explicit-octopus15
-rw-r--r--t/t5515/refs.br-remote-explicit-octopus_remote-explicit15
-rw-r--r--t/t5515/refs.br-remote-explicit_remote-explicit15
-rw-r--r--t/t5515/refs.br-remote-glob15
-rw-r--r--t/t5515/refs.br-remote-glob-merge15
-rw-r--r--t/t5515/refs.br-remote-glob-merge_remote-glob15
-rw-r--r--t/t5515/refs.br-remote-glob-octopus15
-rw-r--r--t/t5515/refs.br-remote-glob-octopus_remote-glob15
-rw-r--r--t/t5515/refs.br-remote-glob_remote-glob15
-rw-r--r--t/t5515/refs.br-unconfig11
-rw-r--r--t/t5515/refs.br-unconfig_--tags_.._.git11
-rw-r--r--t/t5515/refs.br-unconfig_.._.git5
-rw-r--r--t/t5515/refs.br-unconfig_.._.git_one5
-rw-r--r--t/t5515/refs.br-unconfig_.._.git_one_tag_tag-one_tag_tag-three-file11
-rw-r--r--t/t5515/refs.br-unconfig_.._.git_one_two5
-rw-r--r--t/t5515/refs.br-unconfig_.._.git_tag_tag-one-tree_tag_tag-three-file11
-rw-r--r--t/t5515/refs.br-unconfig_.._.git_tag_tag-one_tag_tag-three11
-rw-r--r--t/t5515/refs.br-unconfig_branches-default12
-rw-r--r--t/t5515/refs.br-unconfig_branches-one12
-rw-r--r--t/t5515/refs.br-unconfig_config-explicit15
-rw-r--r--t/t5515/refs.br-unconfig_config-glob15
-rw-r--r--t/t5515/refs.br-unconfig_remote-explicit15
-rw-r--r--t/t5515/refs.br-unconfig_remote-glob15
-rw-r--r--t/t5515/refs.main11
-rw-r--r--t/t5515/refs.main_--tags_.._.git11
-rw-r--r--t/t5515/refs.main_.._.git5
-rw-r--r--t/t5515/refs.main_.._.git_one5
-rw-r--r--t/t5515/refs.main_.._.git_one_tag_tag-one_tag_tag-three-file11
-rw-r--r--t/t5515/refs.main_.._.git_one_two5
-rw-r--r--t/t5515/refs.main_.._.git_tag_tag-one-tree_tag_tag-three-file11
-rw-r--r--t/t5515/refs.main_.._.git_tag_tag-one_tag_tag-three11
-rw-r--r--t/t5515/refs.main_branches-default12
-rw-r--r--t/t5515/refs.main_branches-one12
-rw-r--r--t/t5515/refs.main_config-explicit15
-rw-r--r--t/t5515/refs.main_config-glob15
-rw-r--r--t/t5515/refs.main_remote-explicit15
-rw-r--r--t/t5515/refs.main_remote-glob15
-rwxr-xr-xt/t5516-fetch-push.sh1901
-rwxr-xr-xt/t5517-push-mirror.sh282
-rwxr-xr-xt/t5518-fetch-exit-status.sh41
-rwxr-xr-xt/t5519-push-alternates.sh146
-rwxr-xr-xt/t5520-pull.sh831
-rwxr-xr-xt/t5521-pull-options.sh256
-rwxr-xr-xt/t5522-pull-symlink.sh87
-rwxr-xr-xt/t5523-push-upstream.sh138
-rwxr-xr-xt/t5524-pull-msg.sh53
-rwxr-xr-xt/t5525-fetch-tagopt.sh55
-rwxr-xr-xt/t5526-fetch-submodules.sh1196
-rwxr-xr-xt/t5527-fetch-odd-refs.sh66
-rwxr-xr-xt/t5528-push-default.sh297
-rwxr-xr-xt/t5529-push-errors.sh50
-rwxr-xr-xt/t5530-upload-pack-error.sh123
-rwxr-xr-xt/t5531-deep-submodule-push.sh640
-rwxr-xr-xt/t5532-fetch-proxy.sh53
-rwxr-xr-xt/t5533-push-cas.sh399
-rwxr-xr-xt/t5534-push-signed.sh392
-rwxr-xr-xt/t5535-fetch-push-symref.sh42
-rwxr-xr-xt/t5536-fetch-conflicts.sh86
-rwxr-xr-xt/t5537-fetch-shallow.sh299
-rwxr-xr-xt/t5538-push-shallow.sh126
-rwxr-xr-xt/t5539-fetch-http-shallow.sh157
-rwxr-xr-xt/t5540-http-push-webdav.sh204
-rwxr-xr-xt/t5541-http-push-smart.sh514
-rwxr-xr-xt/t5542-push-http-shallow.sh96
-rwxr-xr-xt/t5543-atomic-push.sh283
-rwxr-xr-xt/t5544-pack-objects-hook.sh79
-rwxr-xr-xt/t5545-push-options.sh291
-rwxr-xr-xt/t5546-receive-limits.sh86
-rwxr-xr-xt/t5547-push-quarantine.sh74
-rwxr-xr-xt/t5548-push-porcelain.sh278
-rwxr-xr-xt/t5549-fetch-push-http.sh72
-rwxr-xr-xt/t5550-http-fetch-dumb.sh488
-rwxr-xr-xt/t5551-http-fetch-smart.sh734
-rwxr-xr-xt/t5552-skipping-fetch-negotiator.sh225
-rwxr-xr-xt/t5553-set-upstream.sh203
-rwxr-xr-xt/t5554-noop-fetch-negotiator.sh24
-rwxr-xr-xt/t5555-http-smart-common.sh162
-rwxr-xr-xt/t5557-http-get.sh39
-rwxr-xr-xt/t5558-clone-bundle-uri.sh1058
-rwxr-xr-xt/t5559-http-fetch-smart-http2.sh5
-rwxr-xr-xt/t5560-http-backend-noserver.sh78
-rwxr-xr-xt/t5561-http-backend.sh139
-rwxr-xr-xt/t5562-http-backend-content-length.sh175
-rw-r--r--t/t5562/invoke-with-content-length.pl36
-rwxr-xr-xt/t5563-simple-http-auth.sh329
-rwxr-xr-xt/t5564-http-proxy.sh41
-rwxr-xr-xt/t556x_common110
-rwxr-xr-xt/t5570-git-daemon.sh210
-rwxr-xr-xt/t5571-pre-push-hook.sh142
-rwxr-xr-xt/t5572-pull-submodule.sh248
-rwxr-xr-xt/t5573-pull-verify-signatures.sh154
-rwxr-xr-xt/t5574-fetch-output.sh293
-rwxr-xr-xt/t5580-unc-paths.sh93
-rwxr-xr-xt/t5581-http-curl-verbose.sh29
-rwxr-xr-xt/t5582-fetch-negative-refspec.sh285
-rwxr-xr-xt/t5583-push-branches.sh116
-rwxr-xr-xt/t5600-clone-fail-cleanup.sh110
-rwxr-xr-xt/t5601-clone.sh883
-rwxr-xr-xt/t5602-clone-remote-exec.sh28
-rwxr-xr-xt/t5603-clone-dirname.sh110
-rwxr-xr-xt/t5604-clone-reference.sh364
-rwxr-xr-xt/t5605-clone-local.sh169
-rwxr-xr-xt/t5606-clone-options.sh201
-rwxr-xr-xt/t5607-clone-bundle.sh172
-rwxr-xr-xt/t5608-clone-2gb.sh52
-rwxr-xr-xt/t5609-clone-branch.sh74
-rwxr-xr-xt/t5610-clone-detached.sh80
-rwxr-xr-xt/t5611-clone-config.sh137
-rwxr-xr-xt/t5612-clone-refspec.sh238
-rwxr-xr-xt/t5613-info-alternate.sh141
-rwxr-xr-xt/t5614-clone-submodules-shallow.sh131
-rwxr-xr-xt/t5615-alternate-env.sh92
-rwxr-xr-xt/t5616-partial-clone.sh879
-rwxr-xr-xt/t5617-clone-submodules-remote.sh111
-rwxr-xr-xt/t5618-alternate-refs.sh62
-rwxr-xr-xt/t5619-clone-local-ambiguous-transport.sh70
-rwxr-xr-xt/t5700-protocol-v1.sh344
-rwxr-xr-xt/t5701-git-serve.sh383
-rwxr-xr-xt/t5702-protocol-v2.sh1319
-rwxr-xr-xt/t5703-upload-pack-ref-in-want.sh539
-rwxr-xr-xt/t5704-protocol-violations.sh52
-rwxr-xr-xt/t5705-session-id-in-capabilities.sh88
-rwxr-xr-xt/t5730-protocol-v2-bundle-uri-file.sh17
-rwxr-xr-xt/t5731-protocol-v2-bundle-uri-git.sh17
-rwxr-xr-xt/t5732-protocol-v2-bundle-uri-http.sh17
-rwxr-xr-xt/t5750-bundle-uri-parse.sh290
-rwxr-xr-xt/t5801-remote-helpers.sh324
-rwxr-xr-xt/t5801/git-remote-testgit157
-rwxr-xr-xt/t5802-connect-helper.sh101
-rwxr-xr-xt/t5810-proto-disable-local.sh39
-rwxr-xr-xt/t5811-proto-disable-git.sh22
-rwxr-xr-xt/t5812-proto-disable-http.sh37
-rwxr-xr-xt/t5813-proto-disable-ssh.sh45
-rwxr-xr-xt/t5814-proto-disable-ext.sh18
-rwxr-xr-xt/t5815-submodule-protos.sh43
-rwxr-xr-xt/t5900-repo-selection.sh102
-rwxr-xr-xt/t6000-rev-list-misc.sh185
-rwxr-xr-xt/t6001-rev-list-graft.sh127
-rwxr-xr-xt/t6002-rev-list-bisect.sh312
-rwxr-xr-xt/t6003-rev-list-topo-order.sh445
-rwxr-xr-xt/t6004-rev-list-path-optim.sh99
-rwxr-xr-xt/t6005-rev-list-count.sh52
-rwxr-xr-xt/t6006-rev-list-format.sh609
-rwxr-xr-xt/t6007-rev-list-cherry-pick-file.sh284
-rwxr-xr-xt/t6008-rev-list-submodule.sh46
-rwxr-xr-xt/t6009-rev-list-parent.sh153
-rwxr-xr-xt/t6010-merge-base.sh308
-rwxr-xr-xt/t6011-rev-list-with-bad-commit.sh60
-rwxr-xr-xt/t6012-rev-list-simplify.sh288
-rwxr-xr-xt/t6013-rev-list-reverse-parents.sh45
-rwxr-xr-xt/t6014-rev-list-all.sh43
-rwxr-xr-xt/t6016-rev-list-graph-simplify-history.sh246
-rwxr-xr-xt/t6017-rev-list-stdin.sh151
-rwxr-xr-xt/t6018-rev-list-glob.sh429
-rwxr-xr-xt/t6019-rev-list-ancestry-path.sh132
-rwxr-xr-xt/t6020-bundle-misc.sh654
-rwxr-xr-xt/t6021-rev-list-exclude-hidden.sh164
-rwxr-xr-xt/t6022-rev-list-missing.sh74
-rwxr-xr-xt/t6030-bisect-porcelain.sh1256
-rwxr-xr-xt/t6040-tracking-info.sh296
-rwxr-xr-xt/t6041-bisect-submodule.sh37
-rwxr-xr-xt/t6050-replace.sh510
-rwxr-xr-xt/t6060-merge-index.sh99
-rwxr-xr-xt/t6100-rev-list-in-order.sh78
-rwxr-xr-xt/t6101-rev-parse-parents.sh231
-rwxr-xr-xt/t6102-rev-list-unexpected-objects.sh133
-rwxr-xr-xt/t6110-rev-list-sparse.sh24
-rwxr-xr-xt/t6111-rev-list-treesame.sh197
-rwxr-xr-xt/t6112-rev-list-filters-objects.sh704
-rwxr-xr-xt/t6113-rev-list-bitmap-filters.sh157
-rwxr-xr-xt/t6114-keep-packs.sh71
-rwxr-xr-xt/t6115-rev-list-du.sh80
-rwxr-xr-xt/t6120-describe.sh674
-rwxr-xr-xt/t6130-pathspec-noglob.sh160
-rwxr-xr-xt/t6131-pathspec-icase.sh111
-rwxr-xr-xt/t6132-pathspec-exclude.sh428
-rwxr-xr-xt/t6133-pathspec-rev-dwim.sh48
-rwxr-xr-xt/t6134-pathspec-in-submodule.sh33
-rwxr-xr-xt/t6135-pathspec-with-attrs.sh298
-rwxr-xr-xt/t6136-pathspec-in-bare.sh39
-rwxr-xr-xt/t6200-fmt-merge-msg.sh704
-rwxr-xr-xt/t6300-for-each-ref.sh2009
-rwxr-xr-xt/t6301-for-each-ref-errors.sh71
-rwxr-xr-xt/t6302-for-each-ref-filter.sh479
-rwxr-xr-xt/t6400-merge-df.sh163
-rwxr-xr-xt/t6401-merge-criss-cross.sh69
-rwxr-xr-xt/t6402-merge-rename.sh973
-rwxr-xr-xt/t6403-merge-file.sh450
-rwxr-xr-xt/t6404-recursive-merge.sh153
-rwxr-xr-xt/t6405-merge-symlinks.sh66
-rwxr-xr-xt/t6406-merge-attr.sh258
-rwxr-xr-xt/t6407-merge-binary.sh61
-rwxr-xr-xt/t6408-merge-up-to-date.sh93
-rwxr-xr-xt/t6409-merge-subtree.sh155
-rwxr-xr-xt/t6411-merge-filemode.sh104
-rwxr-xr-xt/t6412-merge-large-rename.sh106
-rwxr-xr-xt/t6413-merge-crlf.sh48
-rwxr-xr-xt/t6414-merge-rename-nocruft.sh101
-rwxr-xr-xt/t6415-merge-dir-to-symlink.sh178
-rwxr-xr-xt/t6416-recursive-corner-cases.sh1886
-rwxr-xr-xt/t6417-merge-ours-theirs.sh109
-rwxr-xr-xt/t6418-merge-text-auto.sh233
-rwxr-xr-xt/t6419-merge-ignorecase.sh56
-rwxr-xr-xt/t6421-merge-partial-clone.sh440
-rwxr-xr-xt/t6422-merge-rename-corner-cases.sh1466
-rwxr-xr-xt/t6423-merge-rename-directories.sh5835
-rwxr-xr-xt/t6424-merge-unrelated-index-changes.sh310
-rwxr-xr-xt/t6425-merge-rename-delete.sh28
-rwxr-xr-xt/t6426-merge-skip-unneeded-updates.sh767
-rwxr-xr-xt/t6427-diff3-conflict-markers.sh304
-rwxr-xr-xt/t6428-merge-conflicts-sparse.sh145
-rwxr-xr-xt/t6429-merge-sequence-rename-caching.sh767
-rwxr-xr-xt/t6430-merge-recursive.sh776
-rwxr-xr-xt/t6431-merge-criscross.sh96
-rwxr-xr-xt/t6432-merge-recursive-space-options.sh210
-rwxr-xr-xt/t6433-merge-toplevel.sh178
-rwxr-xr-xt/t6434-merge-recursive-rename-options.sh333
-rwxr-xr-xt/t6435-merge-sparse.sh61
-rwxr-xr-xt/t6436-merge-overwrite.sh207
-rwxr-xr-xt/t6437-submodule-merge.sh546
-rwxr-xr-xt/t6438-submodule-directory-file-conflicts.sh22
-rwxr-xr-xt/t6439-merge-co-error-msgs.sh143
-rwxr-xr-xt/t6500-gc.sh419
-rwxr-xr-xt/t6501-freshen-objects.sh184
-rwxr-xr-xt/t6600-test-reach.sh615
-rwxr-xr-xt/t6700-tree-depth.sh95
-rwxr-xr-xt/t7001-mv.sh552
-rwxr-xr-xt/t7002-mv-sparse-checkout.sh517
-rwxr-xr-xt/t7003-filter-branch.sh543
-rwxr-xr-xt/t7004-tag.sh2210
-rwxr-xr-xt/t7005-editor.sh131
-rwxr-xr-xt/t7006-pager.sh768
-rwxr-xr-xt/t7007-show.sh171
-rwxr-xr-xt/t7008-filter-branch-null-sha1.sh56
-rwxr-xr-xt/t7010-setup.sh161
-rwxr-xr-xt/t7011-skip-worktree-reading.sh148
-rwxr-xr-xt/t7012-skip-worktree-writing.sh198
-rwxr-xr-xt/t7030-verify-tag.sh206
-rwxr-xr-xt/t7031-verify-tag-signed-ssh.sh213
-rwxr-xr-xt/t7060-wtstatus.sh255
-rwxr-xr-xt/t7061-wtstatus-ignore.sh316
-rwxr-xr-xt/t7062-wtstatus-ignorecase.sh21
-rwxr-xr-xt/t7063-status-untracked-cache.sh994
-rwxr-xr-xt/t7064-wtstatus-pv2.sh673
-rwxr-xr-xt/t7101-reset-empty-subdirs.sh66
-rwxr-xr-xt/t7102-reset.sh619
-rwxr-xr-xt/t7103-reset-bare.sh74
-rwxr-xr-xt/t7104-reset-hard.sh47
-rwxr-xr-xt/t7105-reset-patch.sh94
-rwxr-xr-xt/t7106-reset-unborn-branch.sh69
-rwxr-xr-xt/t7107-reset-pathspec-file.sh179
-rwxr-xr-xt/t7110-reset-merge.sh296
-rwxr-xr-xt/t7111-reset-table.sh122
-rwxr-xr-xt/t7112-reset-submodule.sh20
-rwxr-xr-xt/t7113-post-index-change-hook.sh147
-rwxr-xr-xt/t7201-co.sh725
-rwxr-xr-xt/t7300-clean.sh792
-rwxr-xr-xt/t7301-clean-interactive.sh486
-rwxr-xr-xt/t7400-submodule-basic.sh1455
-rwxr-xr-xt/t7401-submodule-summary.sh316
-rwxr-xr-xt/t7402-submodule-rebase.sh135
-rwxr-xr-xt/t7403-submodule-sync.sh356
-rwxr-xr-xt/t7406-submodule-update.sh1205
-rwxr-xr-xt/t7407-submodule-foreach.sh440
-rwxr-xr-xt/t7408-submodule-reference.sh227
-rwxr-xr-xt/t7409-submodule-detached-work-tree.sh92
-rwxr-xr-xt/t7411-submodule-config.sh261
-rwxr-xr-xt/t7412-submodule-absorbgitdirs.sh184
-rwxr-xr-xt/t7413-submodule-is-active.sh128
-rwxr-xr-xt/t7414-submodule-mistakes.sh40
-rwxr-xr-xt/t7416-submodule-dash-url.sh225
-rwxr-xr-xt/t7417-submodule-path-url.sh52
-rwxr-xr-xt/t7418-submodule-sparse-gitmodules.sh130
-rwxr-xr-xt/t7419-submodule-set-branch.sh131
-rwxr-xr-xt/t7420-submodule-set-url.sh81
-rwxr-xr-xt/t7421-submodule-summary-add.sh73
-rwxr-xr-xt/t7422-submodule-output.sh170
-rwxr-xr-xt/t7450-bad-git-dotfiles.sh320
-rwxr-xr-xt/t7500-commit-template-squash-signoff.sh561
-rwxr-xr-xt/t7500/add-comments4
-rwxr-xr-xt/t7500/add-content3
-rwxr-xr-xt/t7500/add-content-and-comment5
-rwxr-xr-xt/t7500/add-signed-off3
-rwxr-xr-xt/t7500/add-whitespaced-content8
-rwxr-xr-xt/t7500/edit-content4
-rwxr-xr-xt/t7501-commit-basic-functionality.sh716
-rwxr-xr-xt/t7502-commit-porcelain.sh962
-rwxr-xr-xt/t7503-pre-commit-and-pre-merge-commit-hooks.sh283
-rwxr-xr-xt/t7504-commit-msg-hook.sh311
-rwxr-xr-xt/t7505-prepare-commit-msg-hook.sh311
-rw-r--r--t/t7505/expected-rebase-i18
-rw-r--r--t/t7505/expected-rebase-p18
-rwxr-xr-xt/t7506-status-submodule.sh415
-rwxr-xr-xt/t7507-commit-verbose.sh160
-rwxr-xr-xt/t7508-status.sh1764
-rwxr-xr-xt/t7509-commit-authorship.sh175
-rwxr-xr-xt/t7510-signed-commit.sh448
-rwxr-xr-xt/t7511-status-index.sh51
-rwxr-xr-xt/t7512-status-help.sh1036
-rwxr-xr-xt/t7513-interpret-trailers.sh1919
-rwxr-xr-xt/t7514-commit-patch.sh34
-rwxr-xr-xt/t7515-status-symlinks.sh44
-rwxr-xr-xt/t7516-commit-races.sh33
-rwxr-xr-xt/t7517-per-repo-email.sh153
-rwxr-xr-xt/t7518-ident-corner-cases.sh58
-rwxr-xr-xt/t7519-status-fsmonitor.sh480
-rwxr-xr-xt/t7519/fsmonitor-all23
-rwxr-xr-xt/t7519/fsmonitor-all-v221
-rwxr-xr-xt/t7519/fsmonitor-env24
-rwxr-xr-xt/t7519/fsmonitor-none22
-rwxr-xr-xt/t7519/fsmonitor-watchman127
-rwxr-xr-xt/t7519/fsmonitor-watchman-v2173
-rwxr-xr-xt/t7520-ignored-hook-warning.sh39
-rwxr-xr-xt/t7521-ignored-mode.sh233
-rwxr-xr-xt/t7524-commit-summary.sh31
-rwxr-xr-xt/t7525-status-rename.sh114
-rwxr-xr-xt/t7526-commit-pathspec-file.sh165
-rwxr-xr-xt/t7527-builtin-fsmonitor.sh1040
-rwxr-xr-xt/t7528-signed-commit-ssh.sh462
-rwxr-xr-xt/t7600-merge.sh1086
-rwxr-xr-xt/t7601-merge-pull-config.sh525
-rwxr-xr-xt/t7602-merge-octopus-many.sh108
-rwxr-xr-xt/t7603-merge-reduce-heads.sh165
-rwxr-xr-xt/t7604-merge-custom-message.sh116
-rwxr-xr-xt/t7605-merge-resolve.sh53
-rwxr-xr-xt/t7606-merge-custom.sh93
-rwxr-xr-xt/t7607-merge-state.sh33
-rwxr-xr-xt/t7608-merge-messages.sh64
-rwxr-xr-xt/t7609-mergetool--lib.sh15
-rwxr-xr-xt/t7610-mergetool.sh901
-rwxr-xr-xt/t7611-merge-abort.sh204
-rwxr-xr-xt/t7612-merge-verify-signatures.sh139
-rwxr-xr-xt/t7614-merge-signoff.sh73
-rwxr-xr-xt/t7700-repack.sh833
-rwxr-xr-xt/t7701-repack-unpack-unreachable.sh199
-rwxr-xr-xt/t7702-repack-cyclic-alternate.sh26
-rwxr-xr-xt/t7703-repack-geometric.sh448
-rwxr-xr-xt/t7704-repack-cruft.sh414
-rwxr-xr-xt/t7800-difftool.sh920
-rwxr-xr-xt/t7810-grep.sh1922
-rwxr-xr-xt/t7811-grep-open.sh159
-rwxr-xr-xt/t7812-grep-icase-non-ascii.sh153
-rwxr-xr-xt/t7813-grep-icase-iso.sh20
-rwxr-xr-xt/t7814-grep-recurse-submodules.sh637
-rwxr-xr-xt/t7815-grep-binary.sh149
-rwxr-xr-xt/t7816-grep-binary-pattern.sh128
-rwxr-xr-xt/t7817-grep-sparse-checkout.sh181
-rwxr-xr-xt/t7900-maintenance.sh865
-rwxr-xr-xt/t8001-annotate.sh20
-rwxr-xr-xt/t8002-blame.sh144
-rwxr-xr-xt/t8003-blame-corner-cases.sh319
-rwxr-xr-xt/t8004-blame-with-conflicts.sh76
-rwxr-xr-xt/t8005-blame-i18n.sh96
-rw-r--r--t/t8005/euc-japan.txt2
-rw-r--r--t/t8005/sjis.txt2
-rw-r--r--t/t8005/utf8.txt2
-rwxr-xr-xt/t8006-blame-textconv.sh157
-rwxr-xr-xt/t8007-cat-file-textconv.sh130
-rwxr-xr-xt/t8008-blame-formats.sh109
-rwxr-xr-xt/t8009-blame-vs-topicbranches.sh36
-rwxr-xr-xt/t8010-cat-file-filters.sh71
-rwxr-xr-xt/t8011-blame-split-file.sh117
-rwxr-xr-xt/t8012-blame-colors.sh52
-rwxr-xr-xt/t8013-blame-ignore-revs.sh291
-rwxr-xr-xt/t8014-blame-ignore-fuzzy.sh437
-rwxr-xr-xt/t9001-send-email.sh2568
-rwxr-xr-xt/t9002-column.sh198
-rwxr-xr-xt/t9003-help-autocorrect.sh71
-rwxr-xr-xt/t9004-example.sh12
-rwxr-xr-xt/t9100-git-svn-basic.sh333
-rwxr-xr-xt/t9101-git-svn-props.sh236
-rwxr-xr-xt/t9102-git-svn-deep-rmdir.sh32
-rwxr-xr-xt/t9103-git-svn-tracked-directory-removed.sh41
-rwxr-xr-xt/t9104-git-svn-follow-parent.sh230
-rwxr-xr-xt/t9105-git-svn-commit-diff.sh44
-rwxr-xr-xt/t9106-git-svn-commit-diff-clobber.sh107
-rwxr-xr-xt/t9107-git-svn-migrate.sh136
-rwxr-xr-xt/t9108-git-svn-glob.sh116
-rwxr-xr-xt/t9109-git-svn-multi-glob.sh167
-rwxr-xr-xt/t9110-git-svn-use-svm-props.sh61
-rw-r--r--t/t9110/svm.dump511
-rwxr-xr-xt/t9111-git-svn-use-svnsync-props.sh51
-rw-r--r--t/t9111/svnsync.dump560
-rwxr-xr-xt/t9112-git-svn-md5less-file.sh47
-rwxr-xr-xt/t9113-git-svn-dcommit-new-file.sh35
-rwxr-xr-xt/t9114-git-svn-dcommit-merge.sh95
-rwxr-xr-xt/t9115-git-svn-dcommit-funky-renames.sh123
-rw-r--r--t/t9115/funky-names.dump103
-rwxr-xr-xt/t9116-git-svn-log.sh150
-rwxr-xr-xt/t9117-git-svn-init-clone.sh122
-rwxr-xr-xt/t9118-git-svn-funky-branch-names.sh90
-rwxr-xr-xt/t9119-git-svn-info.sh391
-rwxr-xr-xt/t9120-git-svn-clone-with-percent-escapes.sh77
-rwxr-xr-xt/t9121-git-svn-fetch-renamed-dir.sh20
-rw-r--r--t/t9121/renamed-dir.dump90
-rwxr-xr-xt/t9122-git-svn-author.sh85
-rwxr-xr-xt/t9123-git-svn-rebuild-with-rewriteroot.sh32
-rwxr-xr-xt/t9124-git-svn-dcommit-auto-props.sh105
-rwxr-xr-xt/t9125-git-svn-multi-glob-branch-names.sh37
-rwxr-xr-xt/t9126-git-svn-follow-deleted-readded-directory.sh22
-rw-r--r--t/t9126/follow-deleted-readded.dump201
-rwxr-xr-xt/t9127-git-svn-partial-rebuild.sh60
-rwxr-xr-xt/t9128-git-svn-cmd-branch.sh79
-rwxr-xr-xt/t9129-git-svn-i18n-commitencoding.sh92
-rwxr-xr-xt/t9130-git-svn-authors-file.sh131
-rwxr-xr-xt/t9131-git-svn-empty-symlink.sh110
-rwxr-xr-xt/t9132-git-svn-broken-symlink.sh102
-rwxr-xr-xt/t9133-git-svn-nested-git-repo.sh101
-rwxr-xr-xt/t9134-git-svn-ignore-paths.sh147
-rwxr-xr-xt/t9135-git-svn-moved-branch-empty-file.sh20
-rw-r--r--t/t9135/svn.dump192
-rwxr-xr-xt/t9136-git-svn-recreated-branch-empty-file.sh12
-rw-r--r--t/t9136/svn.dump192
-rwxr-xr-xt/t9137-git-svn-dcommit-clobber-series.sh63
-rwxr-xr-xt/t9138-git-svn-authors-prog.sh107
-rwxr-xr-xt/t9139-git-svn-non-utf8-commitencoding.sh48
-rwxr-xr-xt/t9140-git-svn-reset.sh66
-rwxr-xr-xt/t9141-git-svn-multiple-branches.sh122
-rwxr-xr-xt/t9142-git-svn-shallow-clone.sh29
-rwxr-xr-xt/t9143-git-svn-gc.sh51
-rwxr-xr-xt/t9144-git-svn-old-rev_map.sh31
-rwxr-xr-xt/t9145-git-svn-master-branch.sh28
-rwxr-xr-xt/t9146-git-svn-empty-dirs.sh160
-rwxr-xr-xt/t9147-git-svn-include-paths.sh149
-rwxr-xr-xt/t9148-git-svn-propset.sh98
-rwxr-xr-xt/t9150-svk-mergetickets.sh25
-rwxr-xr-xt/t9150/make-svk-dump57
-rw-r--r--t/t9150/svk-merge.dump616
-rwxr-xr-xt/t9151-svn-mergeinfo.sh54
-rw-r--r--t/t9151/.gitignore2
-rwxr-xr-xt/t9151/make-svnmerge-dump305
-rw-r--r--t/t9151/svn-mergeinfo.dump2388
-rwxr-xr-xt/t9152-svn-empty-dirs-after-gc.sh40
-rwxr-xr-xt/t9153-git-svn-rewrite-uuid.sh25
-rw-r--r--t/t9153/svn.dump75
-rwxr-xr-xt/t9154-git-svn-fancy-glob.sh51
-rw-r--r--t/t9154/svn.dump222
-rwxr-xr-xt/t9155-git-svn-fetch-deleted-tag.sh45
-rwxr-xr-xt/t9156-git-svn-fetch-deleted-tag-2.sh47
-rwxr-xr-xt/t9157-git-svn-fetch-merge.sh58
-rwxr-xr-xt/t9158-git-svn-mergeinfo.sh52
-rwxr-xr-xt/t9159-git-svn-no-parent-mergeinfo.sh41
-rwxr-xr-xt/t9160-git-svn-preserve-empty-dirs.sh152
-rwxr-xr-xt/t9161-git-svn-mergeinfo-push.sh103
-rw-r--r--t/t9161/branches.dump374
-rwxr-xr-xt/t9162-git-svn-dcommit-interactive.sh65
-rwxr-xr-xt/t9163-git-svn-reset-clears-caches.sh81
-rwxr-xr-xt/t9164-git-svn-dcommit-concurrent.sh217
-rwxr-xr-xt/t9165-git-svn-fetch-merge-branch-of-branch.sh60
-rwxr-xr-xt/t9166-git-svn-fetch-merge-branch-of-branch2.sh53
-rwxr-xr-xt/t9167-git-svn-cmd-branch-subproject.sh49
-rwxr-xr-xt/t9168-git-svn-partially-globbed-names.sh229
-rwxr-xr-xt/t9169-git-svn-dcommit-crlf.sh30
-rwxr-xr-xt/t9200-git-cvsexportcommit.sh341
-rwxr-xr-xt/t9210-scalar.sh234
-rwxr-xr-xt/t9211-scalar-clone.sh195
-rwxr-xr-xt/t9300-fast-import.sh3531
-rwxr-xr-xt/t9301-fast-import-notes.sh728
-rwxr-xr-xt/t9302-fast-import-unpack-limit.sh107
-rwxr-xr-xt/t9303-fast-import-compression.sh63
-rwxr-xr-xt/t9304-fast-import-marks.sh81
-rwxr-xr-xt/t9350-fast-export.sh794
-rw-r--r--t/t9350/broken-iso-8859-7-commit-message.txt1
-rw-r--r--t/t9350/simple-iso-8859-7-commit-message.txt1
-rwxr-xr-xt/t9351-fast-export-anonymize.sh141
-rwxr-xr-xt/t9400-git-cvsserver-server.sh672
-rwxr-xr-xt/t9401-git-cvsserver-crlf.sh372
-rwxr-xr-xt/t9402-git-cvsserver-refs.sh554
-rwxr-xr-xt/t9500-gitweb-standalone-no-errors.sh810
-rwxr-xr-xt/t9501-gitweb-standalone-http-status.sh221
-rwxr-xr-xt/t9502-gitweb-standalone-parse-output.sh237
-rwxr-xr-xt/t9600-cvsimport.sh167
-rwxr-xr-xt/t9601-cvsimport-vendor-branch.sh88
-rw-r--r--t/t9601/cvsroot/.gitattributes1
-rw-r--r--t/t9601/cvsroot/CVSROOT/.gitignore2
-rw-r--r--t/t9601/cvsroot/module/added-imported.txt,v44
-rw-r--r--t/t9601/cvsroot/module/imported-anonymously.txt,v42
-rw-r--r--t/t9601/cvsroot/module/imported-modified-imported.txt,v76
-rw-r--r--t/t9601/cvsroot/module/imported-modified.txt,v59
-rw-r--r--t/t9601/cvsroot/module/imported-once.txt,v43
-rw-r--r--t/t9601/cvsroot/module/imported-twice.txt,v60
-rwxr-xr-xt/t9602-cvsimport-branches-tags.sh81
-rw-r--r--t/t9602/README62
-rw-r--r--t/t9602/cvsroot/.gitattributes1
-rw-r--r--t/t9602/cvsroot/CVSROOT/.gitignore2
-rw-r--r--t/t9602/cvsroot/module/default,v102
-rw-r--r--t/t9602/cvsroot/module/sub1/default,v102
-rw-r--r--t/t9602/cvsroot/module/sub1/subsubA/default,v101
-rw-r--r--t/t9602/cvsroot/module/sub1/subsubB/default,v107
-rw-r--r--t/t9602/cvsroot/module/sub2/Attic/branch_B_MIXED_only,v59
-rw-r--r--t/t9602/cvsroot/module/sub2/default,v102
-rw-r--r--t/t9602/cvsroot/module/sub2/subsubA/default,v102
-rw-r--r--t/t9602/cvsroot/module/sub3/default,v102
-rwxr-xr-xt/t9603-cvsimport-patchsets.sh39
-rw-r--r--t/t9603/cvsroot/.gitattributes1
-rw-r--r--t/t9603/cvsroot/CVSROOT/.gitignore2
-rw-r--r--t/t9603/cvsroot/module/a,v74
-rw-r--r--t/t9603/cvsroot/module/b,v90
-rwxr-xr-xt/t9604-cvsimport-timestamps.sh71
-rw-r--r--t/t9604/cvsroot/.gitattributes1
-rw-r--r--t/t9604/cvsroot/CVSROOT/.gitignore2
-rw-r--r--t/t9604/cvsroot/module/a,v264
-rwxr-xr-xt/t9700-perl-git.sh56
-rwxr-xr-xt/t9700/test.pl160
-rwxr-xr-xt/t9800-git-p4-basic.sh421
-rwxr-xr-xt/t9801-git-p4-branch.sh760
-rwxr-xr-xt/t9802-git-p4-filetype.sh370
-rwxr-xr-xt/t9803-git-p4-shell-metachars.sh108
-rwxr-xr-xt/t9804-git-p4-label.sh111
-rwxr-xr-xt/t9805-git-p4-skip-submit-edit.sh101
-rwxr-xr-xt/t9806-git-p4-options.sh306
-rwxr-xr-xt/t9807-git-p4-submit.sh599
-rwxr-xr-xt/t9808-git-p4-chdir.sh86
-rwxr-xr-xt/t9809-git-p4-client-view.sh839
-rwxr-xr-xt/t9810-git-p4-rcs.sh377
-rwxr-xr-xt/t9811-git-p4-label-import.sh265
-rwxr-xr-xt/t9812-git-p4-wildcards.sh214
-rwxr-xr-xt/t9813-git-p4-preserve-users.sh141
-rwxr-xr-xt/t9814-git-p4-rename.sh245
-rwxr-xr-xt/t9815-git-p4-submit-fail.sh425
-rwxr-xr-xt/t9816-git-p4-locked.sh141
-rwxr-xr-xt/t9817-git-p4-exclude.sh108
-rwxr-xr-xt/t9818-git-p4-block.sh149
-rwxr-xr-xt/t9819-git-p4-case-folding.sh56
-rwxr-xr-xt/t9820-git-p4-editor-handling.sh34
-rwxr-xr-xt/t9821-git-p4-path-variations.sh196
-rwxr-xr-xt/t9822-git-p4-path-encoding.sh77
-rwxr-xr-xt/t9823-git-p4-mock-lfs.sh188
-rwxr-xr-xt/t9824-git-p4-git-lfs.sh290
-rwxr-xr-xt/t9825-git-p4-handle-utf16-without-bom.sh46
-rwxr-xr-xt/t9826-git-p4-keep-empty-commits.sh130
-rwxr-xr-xt/t9827-git-p4-change-filetype.sh62
-rwxr-xr-xt/t9828-git-p4-map-user.sh57
-rwxr-xr-xt/t9829-git-p4-jobs.sh95
-rwxr-xr-xt/t9830-git-p4-symlink-dir.sh39
-rwxr-xr-xt/t9831-git-p4-triggers.sh99
-rwxr-xr-xt/t9832-unshelve.sh188
-rwxr-xr-xt/t9833-errors.sh48
-rwxr-xr-xt/t9834-git-p4-file-dir-bug.sh70
-rwxr-xr-xt/t9835-git-p4-metadata-encoding-python2.sh213
-rwxr-xr-xt/t9836-git-p4-metadata-encoding-python3.sh214
-rwxr-xr-xt/t9850-shell.sh37
-rwxr-xr-xt/t9901-git-web--browse.sh64
-rwxr-xr-xt/t9902-completion.sh2718
-rwxr-xr-xt/t9903-bash-prompt.sh778
-rw-r--r--t/test-binary-1.pngbin0 -> 5660 bytes
-rw-r--r--t/test-binary-2.pngbin0 -> 275 bytes
-rw-r--r--t/test-lib-functions.sh1941
-rw-r--r--t/test-lib-github-workflow-markup.sh56
-rw-r--r--t/test-lib-junit.sh132
-rw-r--r--t/test-lib.sh1951
-rwxr-xr-xt/test-terminal.perl105
-rw-r--r--t/valgrind/.gitignore2
-rwxr-xr-xt/valgrind/analyze.sh127
-rw-r--r--t/valgrind/default.supp51
-rwxr-xr-xt/valgrind/valgrind.sh40
2295 files changed, 330888 insertions, 0 deletions
diff --git a/t/.gitattributes b/t/.gitattributes
new file mode 100644
index 0000000..b9cea17
--- /dev/null
+++ b/t/.gitattributes
@@ -0,0 +1,25 @@
+t[0-9][0-9][0-9][0-9]/* -whitespace
+/chainlint/*.expect eol=lf
+/t0110/url-* binary
+/t3206/* eol=lf
+/t3900/*.txt eol=lf
+/t3901/*.txt eol=lf
+/t4013/* eol=lf
+/t4018/* eol=lf
+/t4034/*/* eol=lf
+/t4051/* eol=lf
+/t4100/* eol=lf
+/t4101/* eol=lf
+/t4109/* eol=lf
+/t4110/* eol=lf
+/t4135/* eol=lf
+/t4211/* eol=lf
+/t4252/* eol=lf
+/t4256/1/* eol=lf
+/t5100/* eol=lf
+/t5515/* eol=lf
+/t556x_common eol=lf
+/t7500/* eol=lf
+/t8005/*.txt eol=lf
+/t9*/*.dump eol=lf
+/t0040*.sh whitespace=-indent-with-non-tab
diff --git a/t/.gitignore b/t/.gitignore
new file mode 100644
index 0000000..91cf577
--- /dev/null
+++ b/t/.gitignore
@@ -0,0 +1,5 @@
+/trash directory*
+/test-results
+/.prove
+/chainlinttmp
+/out/
diff --git a/t/Git-SVN/00compile.t b/t/Git-SVN/00compile.t
new file mode 100755
index 0000000..c92fee4
--- /dev/null
+++ b/t/Git-SVN/00compile.t
@@ -0,0 +1,14 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+
+use Test::More tests => 7;
+
+require_ok 'Git::SVN';
+require_ok 'Git::SVN::Utils';
+require_ok 'Git::SVN::Ra';
+require_ok 'Git::SVN::Log';
+require_ok 'Git::SVN::Migration';
+require_ok 'Git::IndexInfo';
+require_ok 'Git::SVN::GlobSpec';
diff --git a/t/Git-SVN/Utils/add_path_to_url.t b/t/Git-SVN/Utils/add_path_to_url.t
new file mode 100755
index 0000000..bfbd878
--- /dev/null
+++ b/t/Git-SVN/Utils/add_path_to_url.t
@@ -0,0 +1,27 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+
+use Test::More 'no_plan';
+
+use Git::SVN::Utils qw(
+ add_path_to_url
+);
+
+# A reference cannot be a hash key, so we use an array.
+my @tests = (
+ ["http://x.com", "bar"] => 'http://x.com/bar',
+ ["http://x.com", ""] => 'http://x.com',
+ ["http://x.com/foo/", undef] => 'http://x.com/foo/',
+ ["http://x.com/foo/", "/bar/baz/"] => 'http://x.com/foo/bar/baz/',
+ ["http://x.com", 'per%cent'] => 'http://x.com/per%25cent',
+);
+
+while(@tests) {
+ my($have, $want) = splice @tests, 0, 2;
+
+ my $args = join ", ", map { qq['$_'] } map { defined($_) ? $_ : 'undef' } @$have;
+ my $name = "add_path_to_url($args) eq $want";
+ is add_path_to_url(@$have), $want, $name;
+}
diff --git a/t/Git-SVN/Utils/can_compress.t b/t/Git-SVN/Utils/can_compress.t
new file mode 100755
index 0000000..d7b49b8
--- /dev/null
+++ b/t/Git-SVN/Utils/can_compress.t
@@ -0,0 +1,11 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More 'no_plan';
+
+use Git::SVN::Utils qw(can_compress);
+
+# !! is the "convert this to boolean" operator.
+is !!can_compress(), !!eval { require Compress::Zlib };
diff --git a/t/Git-SVN/Utils/canonicalize_url.t b/t/Git-SVN/Utils/canonicalize_url.t
new file mode 100755
index 0000000..05795ab
--- /dev/null
+++ b/t/Git-SVN/Utils/canonicalize_url.t
@@ -0,0 +1,26 @@
+#!/usr/bin/env perl
+
+# Test our own home rolled URL canonicalizer. Test the private one
+# directly because we can't predict what the SVN API is doing to do.
+
+use strict;
+use warnings;
+
+use Test::More 'no_plan';
+
+use Git::SVN::Utils;
+my $canonicalize_url = \&Git::SVN::Utils::_canonicalize_url_ourselves;
+
+my %tests = (
+ "http://x.com" => "http://x.com",
+ "http://x.com/" => "http://x.com",
+ "http://x.com/foo/bar" => "http://x.com/foo/bar",
+ "http://x.com//foo//bar//" => "http://x.com/foo/bar",
+ "http://x.com/ /%/" => "http://x.com/%20%20/%25",
+);
+
+for my $arg (keys %tests) {
+ my $want = $tests{$arg};
+
+ is $canonicalize_url->($arg), $want, "canonicalize_url('$arg') => $want";
+}
diff --git a/t/Git-SVN/Utils/collapse_dotdot.t b/t/Git-SVN/Utils/collapse_dotdot.t
new file mode 100755
index 0000000..1da1cce
--- /dev/null
+++ b/t/Git-SVN/Utils/collapse_dotdot.t
@@ -0,0 +1,23 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+
+use Test::More 'no_plan';
+
+use Git::SVN::Utils;
+my $collapse_dotdot = \&Git::SVN::Utils::_collapse_dotdot;
+
+my %tests = (
+ "foo/bar/baz" => "foo/bar/baz",
+ ".." => "..",
+ "foo/.." => "",
+ "/foo/bar/../../baz" => "/baz",
+ "deeply/.././deeply/nested" => "./deeply/nested",
+);
+
+for my $arg (keys %tests) {
+ my $want = $tests{$arg};
+
+ is $collapse_dotdot->($arg), $want, "_collapse_dotdot('$arg') => $want";
+}
diff --git a/t/Git-SVN/Utils/fatal.t b/t/Git-SVN/Utils/fatal.t
new file mode 100755
index 0000000..49e1438
--- /dev/null
+++ b/t/Git-SVN/Utils/fatal.t
@@ -0,0 +1,34 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More 'no_plan';
+
+BEGIN {
+ # Override exit at BEGIN time before Git::SVN::Utils is loaded
+ # so it will see our local exit later.
+ *CORE::GLOBAL::exit = sub(;$) {
+ return @_ ? CORE::exit($_[0]) : CORE::exit();
+ };
+}
+
+use Git::SVN::Utils qw(fatal);
+
+# fatal()
+{
+ # Capture the exit code and prevent exit.
+ my $exit_status;
+ no warnings 'redefine';
+ local *CORE::GLOBAL::exit = sub { $exit_status = $_[0] || 0 };
+
+ # Trap fatal's message to STDERR
+ my $stderr;
+ close STDERR;
+ ok open STDERR, ">", \$stderr;
+
+ fatal "Some", "Stuff", "Happened";
+
+ is $stderr, "Some Stuff Happened\n";
+ is $exit_status, 1;
+}
diff --git a/t/Git-SVN/Utils/join_paths.t b/t/Git-SVN/Utils/join_paths.t
new file mode 100755
index 0000000..d4488e7
--- /dev/null
+++ b/t/Git-SVN/Utils/join_paths.t
@@ -0,0 +1,32 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+
+use Test::More 'no_plan';
+
+use Git::SVN::Utils qw(
+ join_paths
+);
+
+# A reference cannot be a hash key, so we use an array.
+my @tests = (
+ [] => '',
+ ["/x.com", "bar"] => '/x.com/bar',
+ ["x.com", ""] => 'x.com',
+ ["/x.com/foo/", undef, "bar"] => '/x.com/foo/bar',
+ ["x.com/foo/", "/bar/baz/"] => 'x.com/foo/bar/baz/',
+ ["foo", "bar"] => 'foo/bar',
+ ["/foo/bar", "baz", "/biff"] => '/foo/bar/baz/biff',
+ ["", undef, "."] => '.',
+ [] => '',
+
+);
+
+while(@tests) {
+ my($have, $want) = splice @tests, 0, 2;
+
+ my $args = join ", ", map { qq['$_'] } map { defined($_) ? $_ : 'undef' } @$have;
+ my $name = "join_paths($args) eq '$want'";
+ is join_paths(@$have), $want, $name;
+}
diff --git a/t/Makefile b/t/Makefile
new file mode 100644
index 0000000..3e00cdd
--- /dev/null
+++ b/t/Makefile
@@ -0,0 +1,152 @@
+# Import tree-wide shared Makefile behavior and libraries
+include ../shared.mak
+
+# Run tests
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+-include ../config.mak.autogen
+-include ../config.mak
+
+#GIT_TEST_OPTS = --verbose --debug
+SHELL_PATH ?= $(SHELL)
+TEST_SHELL_PATH ?= $(SHELL_PATH)
+PERL_PATH ?= /usr/bin/perl
+TAR ?= $(TAR)
+RM ?= rm -f
+PROVE ?= prove
+DEFAULT_TEST_TARGET ?= test
+TEST_LINT ?= test-lint
+
+ifdef TEST_OUTPUT_DIRECTORY
+TEST_RESULTS_DIRECTORY = $(TEST_OUTPUT_DIRECTORY)/test-results
+CHAINLINTTMP = $(TEST_OUTPUT_DIRECTORY)/chainlinttmp
+else
+TEST_RESULTS_DIRECTORY = test-results
+CHAINLINTTMP = chainlinttmp
+endif
+
+# Shell quote;
+SHELL_PATH_SQ = $(subst ','\'',$(SHELL_PATH))
+TEST_SHELL_PATH_SQ = $(subst ','\'',$(TEST_SHELL_PATH))
+PERL_PATH_SQ = $(subst ','\'',$(PERL_PATH))
+TEST_RESULTS_DIRECTORY_SQ = $(subst ','\'',$(TEST_RESULTS_DIRECTORY))
+CHAINLINTTMP_SQ = $(subst ','\'',$(CHAINLINTTMP))
+
+T = $(sort $(wildcard t[0-9][0-9][0-9][0-9]-*.sh))
+THELPERS = $(sort $(filter-out $(T),$(wildcard *.sh)))
+TLIBS = $(sort $(wildcard lib-*.sh)) annotate-tests.sh
+TPERF = $(sort $(wildcard perf/p[0-9][0-9][0-9][0-9]-*.sh))
+TINTEROP = $(sort $(wildcard interop/i[0-9][0-9][0-9][0-9]-*.sh))
+CHAINLINTTESTS = $(sort $(patsubst chainlint/%.test,%,$(wildcard chainlint/*.test)))
+CHAINLINT = '$(PERL_PATH_SQ)' chainlint.pl
+
+# `test-chainlint` (which is a dependency of `test-lint`, `test` and `prove`)
+# checks all tests in all scripts via a single invocation, so tell individual
+# scripts not to run the external "chainlint.pl" script themselves
+CHAINLINTSUPPRESS = GIT_TEST_EXT_CHAIN_LINT=0 && export GIT_TEST_EXT_CHAIN_LINT &&
+
+all: $(DEFAULT_TEST_TARGET)
+
+test: pre-clean check-chainlint $(TEST_LINT)
+ $(CHAINLINTSUPPRESS) $(MAKE) aggregate-results-and-cleanup
+
+failed:
+ @failed=$$(cd '$(TEST_RESULTS_DIRECTORY_SQ)' && \
+ grep -l '^failed [1-9]' *.counts | \
+ sed -n 's/\.counts$$/.sh/p') && \
+ test -z "$$failed" || $(MAKE) $$failed
+
+prove: pre-clean check-chainlint $(TEST_LINT)
+ @echo "*** prove ***"; $(CHAINLINTSUPPRESS) $(PROVE) --exec '$(TEST_SHELL_PATH_SQ)' $(GIT_PROVE_OPTS) $(T) :: $(GIT_TEST_OPTS)
+ $(MAKE) clean-except-prove-cache
+
+$(T):
+ @echo "*** $@ ***"; '$(TEST_SHELL_PATH_SQ)' $@ $(GIT_TEST_OPTS)
+
+pre-clean:
+ $(RM) -r '$(TEST_RESULTS_DIRECTORY_SQ)'
+
+clean-except-prove-cache: clean-chainlint
+ $(RM) -r 'trash directory'.*
+ $(RM) -r valgrind/bin
+
+clean: clean-except-prove-cache
+ $(RM) -r '$(TEST_RESULTS_DIRECTORY_SQ)'
+ $(RM) .prove
+
+clean-chainlint:
+ $(RM) -r '$(CHAINLINTTMP_SQ)'
+
+check-chainlint:
+ @mkdir -p '$(CHAINLINTTMP_SQ)' && \
+ for i in $(CHAINLINTTESTS); do \
+ echo "test_expect_success '$$i' '" && \
+ sed -e '/^# LINT: /d' chainlint/$$i.test && \
+ echo "'"; \
+ done >'$(CHAINLINTTMP_SQ)'/tests && \
+ { \
+ echo "# chainlint: $(CHAINLINTTMP_SQ)/tests" && \
+ for i in $(CHAINLINTTESTS); do \
+ echo "# chainlint: $$i" && \
+ sed -e '/^[ ]*$$/d' chainlint/$$i.expect; \
+ done \
+ } >'$(CHAINLINTTMP_SQ)'/expect && \
+ $(CHAINLINT) --emit-all '$(CHAINLINTTMP_SQ)'/tests | \
+ sed -e 's/^[1-9][0-9]* //;/^[ ]*$$/d' >'$(CHAINLINTTMP_SQ)'/actual && \
+ if test -f ../GIT-BUILD-OPTIONS; then \
+ . ../GIT-BUILD-OPTIONS; \
+ fi && \
+ if test -x ../git$$X; then \
+ DIFFW="../git$$X --no-pager diff -w --no-index"; \
+ else \
+ DIFFW="diff -w -u"; \
+ fi && \
+ $$DIFFW '$(CHAINLINTTMP_SQ)'/expect '$(CHAINLINTTMP_SQ)'/actual
+
+test-lint: test-lint-duplicates test-lint-executable test-lint-shell-syntax \
+ test-lint-filenames
+ifneq ($(GIT_TEST_CHAIN_LINT),0)
+test-lint: test-chainlint
+endif
+
+test-lint-duplicates:
+ @dups=`echo $(T) $(TPERF) | tr ' ' '\n' | sed 's/-.*//' | sort | uniq -d` && \
+ test -z "$$dups" || { \
+ echo >&2 "duplicate test numbers:" $$dups; exit 1; }
+
+test-lint-executable:
+ @bad=`for i in $(T) $(TPERF); do test -x "$$i" || echo $$i; done` && \
+ test -z "$$bad" || { \
+ echo >&2 "non-executable tests:" $$bad; exit 1; }
+
+test-lint-shell-syntax:
+ @'$(PERL_PATH_SQ)' check-non-portable-shell.pl $(T) $(THELPERS) $(TPERF)
+
+test-lint-filenames:
+ @# We do *not* pass a glob to ls-files but use grep instead, to catch
+ @# non-ASCII characters (which are quoted within double-quotes)
+ @bad="$$(git -c core.quotepath=true ls-files 2>/dev/null | \
+ grep '["*:<>?\\|]')"; \
+ test -z "$$bad" || { \
+ echo >&2 "non-portable file name(s): $$bad"; exit 1; }
+
+test-chainlint:
+ @$(CHAINLINT) $(T) $(TLIBS) $(TPERF) $(TINTEROP)
+
+aggregate-results-and-cleanup: $(T)
+ $(MAKE) aggregate-results
+ $(MAKE) clean
+
+aggregate-results:
+ @'$(SHELL_PATH_SQ)' ./aggregate-results.sh '$(TEST_RESULTS_DIRECTORY_SQ)'
+
+valgrind:
+ $(MAKE) GIT_TEST_OPTS="$(GIT_TEST_OPTS) --valgrind"
+
+perf:
+ $(MAKE) -C perf/ all
+
+.PHONY: pre-clean $(T) aggregate-results clean valgrind perf \
+ check-chainlint clean-chainlint test-chainlint
diff --git a/t/README b/t/README
new file mode 100644
index 0000000..a0ebe29
--- /dev/null
+++ b/t/README
@@ -0,0 +1,1290 @@
+Core Git Tests
+==============
+
+This directory holds many test scripts for core Git tools. The
+first part of this short document describes how to run the tests
+and read their output.
+
+When fixing the tools or adding enhancements, you are strongly
+encouraged to add tests in this directory to cover what you are
+trying to fix or enhance. The later part of this short document
+describes how your test scripts should be organized.
+
+
+Running Tests
+-------------
+
+The easiest way to run tests is to say "make". This runs all
+the tests.
+
+ *** t0000-basic.sh ***
+ ok 1 - .git/objects should be empty after git init in an empty repo.
+ ok 2 - .git/objects should have 3 subdirectories.
+ ok 3 - success is reported like this
+ ...
+ ok 43 - very long name in the index handled sanely
+ # fixed 1 known breakage(s)
+ # still have 1 known breakage(s)
+ # passed all remaining 42 test(s)
+ 1..43
+ *** t0001-init.sh ***
+ ok 1 - plain
+ ok 2 - plain with GIT_WORK_TREE
+ ok 3 - plain bare
+
+Since the tests all output TAP (see http://testanything.org) they can
+be run with any TAP harness. Here's an example of parallel testing
+powered by a recent version of prove(1):
+
+ $ prove --timer --jobs 15 ./t[0-9]*.sh
+ [19:17:33] ./t0005-signals.sh ................................... ok 36 ms
+ [19:17:33] ./t0022-crlf-rename.sh ............................... ok 69 ms
+ [19:17:33] ./t0024-crlf-archive.sh .............................. ok 154 ms
+ [19:17:33] ./t0004-unwritable.sh ................................ ok 289 ms
+ [19:17:33] ./t0002-gitfile.sh ................................... ok 480 ms
+ ===( 102;0 25/? 6/? 5/? 16/? 1/? 4/? 2/? 1/? 3/? 1... )===
+
+prove and other harnesses come with a lot of useful options. The
+--state option in particular is very useful:
+
+ # Repeat until no more failures
+ $ prove -j 15 --state=failed,save ./t[0-9]*.sh
+
+You can give DEFAULT_TEST_TARGET=prove on the make command (or define it
+in config.mak) to cause "make test" to run tests under prove.
+GIT_PROVE_OPTS can be used to pass additional options, e.g.
+
+ $ make DEFAULT_TEST_TARGET=prove GIT_PROVE_OPTS='--timer --jobs 16' test
+
+You can also run each test individually from command line, like this:
+
+ $ sh ./t3010-ls-files-killed-modified.sh
+ ok 1 - git update-index --add to add various paths.
+ ok 2 - git ls-files -k to show killed files.
+ ok 3 - validate git ls-files -k output.
+ ok 4 - git ls-files -m to show modified files.
+ ok 5 - validate git ls-files -m output.
+ # passed all 5 test(s)
+ 1..5
+
+You can pass --verbose (or -v), --debug (or -d), and --immediate
+(or -i) command line argument to the test, or by setting GIT_TEST_OPTS
+appropriately before running "make". Short options can be bundled, i.e.
+'-d -v' is the same as '-dv'.
+
+-v::
+--verbose::
+ This makes the test more verbose. Specifically, the
+ command being run and their output if any are also
+ output.
+
+--verbose-only=<pattern>::
+ Like --verbose, but the effect is limited to tests with
+ numbers matching <pattern>. The number matched against is
+ simply the running count of the test within the file.
+
+-x::
+ Turn on shell tracing (i.e., `set -x`) during the tests
+ themselves. Implies `--verbose`.
+ Ignored in test scripts that set the variable 'test_untraceable'
+ to a non-empty value, unless it's run with a Bash version
+ supporting BASH_XTRACEFD, i.e. v4.1 or later.
+
+-d::
+--debug::
+ This may help the person who is developing a new test.
+ It causes the command defined with test_debug to run.
+ The "trash" directory (used to store all temporary data
+ during testing) is not deleted even if there are no
+ failed tests so that you can inspect its contents after
+ the test finished.
+
+-i::
+--immediate::
+ This causes the test to immediately exit upon the first
+ failed test. Cleanup commands requested with
+ test_when_finished are not executed if the test failed,
+ in order to keep the state for inspection by the tester
+ to diagnose the bug.
+
+-l::
+--long-tests::
+ This causes additional long-running tests to be run (where
+ available), for more exhaustive testing.
+
+-r::
+--run=<test-selector>::
+ Run only the subset of tests indicated by
+ <test-selector>. See section "Skipping Tests" below for
+ <test-selector> syntax.
+
+--valgrind=<tool>::
+ Execute all Git binaries under valgrind tool <tool> and exit
+ with status 126 on errors (just like regular tests, this will
+ only stop the test script when running under -i).
+
+ Since it makes no sense to run the tests with --valgrind and
+ not see any output, this option implies --verbose. For
+ convenience, it also implies --tee.
+
+ <tool> defaults to 'memcheck', just like valgrind itself.
+ Other particularly useful choices include 'helgrind' and
+ 'drd', but you may use any tool recognized by your valgrind
+ installation.
+
+ As a special case, <tool> can be 'memcheck-fast', which uses
+ memcheck but disables --track-origins. Use this if you are
+ running tests in bulk, to see if there are _any_ memory
+ issues.
+
+ Note that memcheck is run with the option --leak-check=no,
+ as the git process is short-lived and some errors are not
+ interesting. In order to run a single command under the same
+ conditions manually, you should set GIT_VALGRIND to point to
+ the 't/valgrind/' directory and use the commands under
+ 't/valgrind/bin/'.
+
+--valgrind-only=<pattern>::
+ Like --valgrind, but the effect is limited to tests with
+ numbers matching <pattern>. The number matched against is
+ simply the running count of the test within the file.
+
+--tee::
+ In addition to printing the test output to the terminal,
+ write it to files named 't/test-results/$TEST_NAME.out'.
+ As the names depend on the tests' file names, it is safe to
+ run the tests with this option in parallel.
+
+-V::
+--verbose-log::
+ Write verbose output to the same logfile as `--tee`, but do
+ _not_ write it to stdout. Unlike `--tee --verbose`, this option
+ is safe to use when stdout is being consumed by a TAP parser
+ like `prove`. Implies `--tee` and `--verbose`.
+
+--with-dashes::
+ By default tests are run without dashed forms of
+ commands (like git-commit) in the PATH (it only uses
+ wrappers from ../bin-wrappers). Use this option to include
+ the build directory (..) in the PATH, which contains all
+ the dashed forms of commands. This option is currently
+ implied by other options like --valgrind and
+ GIT_TEST_INSTALLED.
+
+--no-bin-wrappers::
+ By default, the test suite uses the wrappers in
+ `../bin-wrappers/` to execute `git` and friends. With this option,
+ `../git` and friends are run directly. This is not recommended
+ in general, as the wrappers contain safeguards to ensure that no
+ files from an installed Git are used, but can speed up test runs
+ especially on platforms where running shell scripts is expensive
+ (most notably, Windows).
+
+--root=<directory>::
+ Create "trash" directories used to store all temporary data during
+ testing under <directory>, instead of the t/ directory.
+ Using this option with a RAM-based filesystem (such as tmpfs)
+ can massively speed up the test suite.
+
+--chain-lint::
+--no-chain-lint::
+ If --chain-lint is enabled, the test harness will check each
+ test to make sure that it properly "&&-chains" all commands (so
+ that a failure in the middle does not go unnoticed by the final
+ exit code of the test). This check is performed in addition to
+ running the tests themselves. You may also enable or disable
+ this feature by setting the GIT_TEST_CHAIN_LINT environment
+ variable to "1" or "0", respectively.
+
+--stress::
+ Run the test script repeatedly in multiple parallel jobs until
+ one of them fails. Useful for reproducing rare failures in
+ flaky tests. The number of parallel jobs is, in order of
+ precedence: the value of the GIT_TEST_STRESS_LOAD
+ environment variable, or twice the number of available
+ processors (as shown by the 'getconf' utility), or 8.
+ Implies `--verbose -x --immediate` to get the most information
+ about the failure. Note that the verbose output of each test
+ job is saved to 't/test-results/$TEST_NAME.stress-<nr>.out',
+ and only the output of the failed test job is shown on the
+ terminal. The names of the trash directories get a
+ '.stress-<nr>' suffix, and the trash directory of the failed
+ test job is renamed to end with a '.stress-failed' suffix.
+
+--stress-jobs=<N>::
+ Override the number of parallel jobs. Implies `--stress`.
+
+--stress-limit=<N>::
+ When combined with --stress run the test script repeatedly
+ this many times in each of the parallel jobs or until one of
+ them fails, whichever comes first. Implies `--stress`.
+
+You can also set the GIT_TEST_INSTALLED environment variable to
+the bindir of an existing git installation to test that installation.
+You still need to have built this git sandbox, from which various
+test-* support programs, templates, and perl libraries are used.
+If your installed git is incomplete, it will silently test parts of
+your built version instead.
+
+When using GIT_TEST_INSTALLED, you can also set GIT_TEST_EXEC_PATH to
+override the location of the dashed-form subcommands (what
+GIT_EXEC_PATH would be used for during normal operation).
+GIT_TEST_EXEC_PATH defaults to `$GIT_TEST_INSTALLED/git --exec-path`.
+
+
+Skipping Tests
+--------------
+
+In some environments, certain tests have no way of succeeding
+due to platform limitation, such as lack of 'unzip' program, or
+filesystem that do not allow arbitrary sequence of non-NUL bytes
+as pathnames.
+
+You should be able to say something like
+
+ $ GIT_SKIP_TESTS=t9200.8 sh ./t9200-git-cvsexport-commit.sh
+
+and even:
+
+ $ GIT_SKIP_TESTS='t[0-4]??? t91?? t9200.8' make
+
+to omit such tests. The value of the environment variable is a
+SP separated list of patterns that tells which tests to skip,
+and either can match the "t[0-9]{4}" part to skip the whole
+test, or t[0-9]{4} followed by ".$number" to say which
+particular test to skip.
+
+For an individual test suite --run could be used to specify that
+only some tests should be run or that some tests should be
+excluded from a run.
+
+The argument for --run, <test-selector>, is a list of description
+substrings or globs or individual test numbers or ranges with an
+optional negation prefix (of '!') that define what tests in a test
+suite to include (or exclude, if negated) in the run. A range is two
+numbers separated with a dash and specifies an inclusive range of tests
+to run. You may omit the first or the second number to
+mean "from the first test" or "up to the very last test" respectively.
+
+The argument to --run is split on commas into separate strings,
+numbers, and ranges, and picks all tests that match any of the
+individual selection criteria. If the substring of the description
+text that you want to match includes a comma, use the glob character
+'?' instead. For example --run='rebase,merge?cherry-pick' would match
+on all tests that match either the glob *rebase* or the glob
+*merge?cherry-pick*.
+
+If --run starts with an unprefixed number or range, the initial
+set of tests to run is empty. If the first item starts with '!',
+all the tests are added to the initial set. After initial set is
+determined, every test number or range is added or excluded from
+the set one by one, from left to right.
+
+For example, to run only tests up to a specific test (21), one
+could do this:
+
+ $ sh ./t9200-git-cvsexport-commit.sh --run='1-21'
+
+or this:
+
+ $ sh ./t9200-git-cvsexport-commit.sh --run='-21'
+
+Common case is to run several setup tests (1, 2, 3) and then a
+specific test (21) that relies on that setup:
+
+ $ sh ./t9200-git-cvsexport-commit.sh --run='1,2,3,21'
+
+or:
+
+ $ sh ./t9200-git-cvsexport-commit.sh --run=1,2,3,21
+
+or:
+
+ $ sh ./t9200-git-cvsexport-commit.sh --run='-3,21'
+
+As noted above, the test set is built by going through the items
+from left to right, so this:
+
+ $ sh ./t9200-git-cvsexport-commit.sh --run='1-4,!3'
+
+will run tests 1, 2, and 4. Items that come later have higher
+precedence. It means that this:
+
+ $ sh ./t9200-git-cvsexport-commit.sh --run='!3,1-4'
+
+would just run tests from 1 to 4, including 3.
+
+You may use negation with ranges. The following will run all
+test in the test suite except from 7 up to 11:
+
+ $ sh ./t9200-git-cvsexport-commit.sh --run='!7-11'
+
+Sometimes there may be multiple tests with e.g. "setup" in their name
+that are needed and rather than figuring out the number for all of them
+we can just use "setup" as a substring/glob to match against the test
+description:
+
+ $ sh ./t0050-filesystem.sh --run=setup,9-11
+
+or one could select both the setup tests and the rename ones (assuming all
+relevant tests had those words in their descriptions):
+
+ $ sh ./t0050-filesystem.sh --run=setup,rename
+
+Some tests in a test suite rely on the previous tests performing
+certain actions, specifically some tests are designated as
+"setup" test, so you cannot _arbitrarily_ disable one test and
+expect the rest to function correctly.
+
+--run is mostly useful when you want to focus on a specific test
+and know what setup is needed for it. Or when you want to run
+everything up to a certain test.
+
+
+Running tests with special setups
+---------------------------------
+
+The whole test suite could be run to test some special features
+that cannot be easily covered by a few specific test cases. These
+could be enabled by running the test suite with correct GIT_TEST_
+environment set.
+
+GIT_TEST_FAIL_PREREQS=<boolean> fails all prerequisites. This is
+useful for discovering issues with the tests where say a later test
+implicitly depends on an optional earlier test.
+
+There's a "FAIL_PREREQS" prerequisite that can be used to test for
+whether this mode is active, and e.g. skip some tests that are hard to
+refactor to deal with it. The "SYMLINKS" prerequisite is currently
+excluded as so much relies on it, but this might change in the future.
+
+GIT_TEST_SPLIT_INDEX=<boolean> forces split-index mode on the whole
+test suite. Accept any boolean values that are accepted by git-config.
+
+GIT_TEST_PASSING_SANITIZE_LEAK=true skips those tests that haven't
+declared themselves as leak-free by setting
+"TEST_PASSES_SANITIZE_LEAK=true" before sourcing "test-lib.sh". This
+test mode is used by the "linux-leaks" CI target.
+
+GIT_TEST_PASSING_SANITIZE_LEAK=check checks that our
+"TEST_PASSES_SANITIZE_LEAK=true" markings are current. Rather than
+skipping those tests that haven't set "TEST_PASSES_SANITIZE_LEAK=true"
+before sourcing "test-lib.sh" this mode runs them with
+"--invert-exit-code". This is used to check that there's a one-to-one
+mapping between "TEST_PASSES_SANITIZE_LEAK=true" and those tests that
+pass under "SANITIZE=leak". This is especially useful when testing a
+series that fixes various memory leaks with "git rebase -x".
+
+GIT_TEST_SANITIZE_LEAK_LOG=true will log memory leaks to
+"test-results/$TEST_NAME.leak/trace.*" files. The logs include a
+"dedup_token" (see +"ASAN_OPTIONS=help=1 ./git") and other options to
+make logs +machine-readable.
+
+With GIT_TEST_SANITIZE_LEAK_LOG=true we'll look at the leak logs
+before exiting and exit on failure if the logs showed that we had a
+memory leak, even if the test itself would have otherwise passed. This
+allows us to catch e.g. missing &&-chaining. This is especially useful
+when combined with "GIT_TEST_PASSING_SANITIZE_LEAK", see below.
+
+GIT_TEST_PASSING_SANITIZE_LEAK=check when combined with "--immediate"
+will run to completion faster, and result in the same failing
+tests. The only practical reason to run
+GIT_TEST_PASSING_SANITIZE_LEAK=check without "--immediate" is to
+combine it with "GIT_TEST_SANITIZE_LEAK_LOG=true". If we stop at the
+first failing test case our leak logs won't show subsequent leaks we
+might have run into.
+
+GIT_TEST_PASSING_SANITIZE_LEAK=(true|check) will not catch all memory
+leaks unless combined with GIT_TEST_SANITIZE_LEAK_LOG=true. Some tests
+run "git" (or "test-tool" etc.) without properly checking the exit
+code, or git will invoke itself and fail to ferry the abort() exit
+code to the original caller. When the two modes are combined we'll
+look at the "test-results/$TEST_NAME.leak/trace.*" files at the end of
+the test run to see if had memory leaks which the test itself didn't
+catch.
+
+GIT_TEST_PROTOCOL_VERSION=<n>, when set, makes 'protocol.version'
+default to n.
+
+GIT_TEST_FULL_IN_PACK_ARRAY=<boolean> exercises the uncommon
+pack-objects code path where there are more than 1024 packs even if
+the actual number of packs in repository is below this limit. Accept
+any boolean values that are accepted by git-config.
+
+GIT_TEST_OE_SIZE=<n> exercises the uncommon pack-objects code path
+where we do not cache object size in memory and read it from existing
+packs on demand. This normally only happens when the object size is
+over 2GB. This variable forces the code path on any object larger than
+<n> bytes.
+
+GIT_TEST_OE_DELTA_SIZE=<n> exercises the uncommon pack-objects code
+path where deltas larger than this limit require extra memory
+allocation for bookkeeping.
+
+GIT_TEST_VALIDATE_INDEX_CACHE_ENTRIES=<boolean> checks that cache-tree
+records are valid when the index is written out or after a merge. This
+is mostly to catch missing invalidation. Default is true.
+
+GIT_TEST_COMMIT_GRAPH=<boolean>, when true, forces the commit-graph to
+be written after every 'git commit' command, and overrides the
+'core.commitGraph' setting to true.
+
+GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS=<boolean>, when true, forces
+commit-graph write to compute and write changed path Bloom filters for
+every 'git commit-graph write', as if the `--changed-paths` option was
+passed in.
+
+GIT_TEST_FSMONITOR=$PWD/t7519/fsmonitor-all exercises the fsmonitor
+code paths for utilizing a (hook based) file system monitor to speed up
+detecting new or changed files.
+
+GIT_TEST_INDEX_VERSION=<n> exercises the index read/write code path
+for the index version specified. Can be set to any valid version
+(currently 2, 3, or 4).
+
+GIT_TEST_PACK_USE_BITMAP_BOUNDARY_TRAVERSAL=<boolean> if enabled will
+use the boundary-based bitmap traversal algorithm. See the documentation
+of `pack.useBitmapBoundaryTraversal` for more details.
+
+GIT_TEST_PACK_SPARSE=<boolean> if disabled will default the pack-objects
+builtin to use the non-sparse object walk. This can still be overridden by
+the --sparse command-line argument.
+
+GIT_TEST_PRELOAD_INDEX=<boolean> exercises the preload-index code path
+by overriding the minimum number of cache entries required per thread.
+
+GIT_TEST_INDEX_THREADS=<n> enables exercising the multi-threaded loading
+of the index for the whole test suite by bypassing the default number of
+cache entries and thread minimums. Setting this to 1 will make the
+index loading single threaded.
+
+GIT_TEST_MULTI_PACK_INDEX=<boolean>, when true, forces the multi-pack-
+index to be written after every 'git repack' command, and overrides the
+'core.multiPackIndex' setting to true.
+
+GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=<boolean>, when true, sets the
+'--bitmap' option on all invocations of 'git multi-pack-index write',
+and ignores pack-objects' '--write-bitmap-index'.
+
+GIT_TEST_SIDEBAND_ALL=<boolean>, when true, overrides the
+'uploadpack.allowSidebandAll' setting to true, and when false, forces
+fetch-pack to not request sideband-all (even if the server advertises
+sideband-all).
+
+GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=<boolean>, when true (which is
+the default when running tests), errors out when an abbreviated option
+is used.
+
+GIT_TEST_DEFAULT_HASH=<hash-algo> specifies which hash algorithm to
+use in the test scripts. Recognized values for <hash-algo> are "sha1"
+and "sha256".
+
+GIT_TEST_NO_WRITE_REV_INDEX=<boolean>, when true disables the
+'pack.writeReverseIndex' setting.
+
+GIT_TEST_SPARSE_INDEX=<boolean>, when true enables index writes to use the
+sparse-index format by default.
+
+GIT_TEST_CHECKOUT_WORKERS=<n> overrides the 'checkout.workers' setting
+to <n> and 'checkout.thresholdForParallelism' to 0, forcing the
+execution of the parallel-checkout code.
+
+GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB=<boolean>, when true, makes
+registering submodule ODBs as alternates a fatal action. Support for
+this environment variable can be removed once the migration to
+explicitly providing repositories when accessing submodule objects is
+complete or needs to be abandoned for whatever reason (in which case the
+migrated codepaths still retain their performance benefits).
+
+GIT_TEST_REQUIRE_PREREQ=<list> allows specifying a space separated list of
+prereqs that are required to succeed. If a prereq in this list is triggered by
+a test and then fails then the whole test run will abort. This can help to make
+sure the expected tests are executed and not silently skipped when their
+dependency breaks or is simply not present in a new environment.
+
+Naming Tests
+------------
+
+The test files are named as:
+
+ tNNNN-commandname-details.sh
+
+where N is a decimal digit.
+
+First digit tells the family:
+
+ 0 - the absolute basics and global stuff
+ 1 - the basic commands concerning database
+ 2 - the basic commands concerning the working tree
+ 3 - the other basic commands (e.g. ls-files)
+ 4 - the diff commands
+ 5 - the pull and exporting commands
+ 6 - the revision tree commands (even e.g. merge-base)
+ 7 - the porcelainish commands concerning the working tree
+ 8 - the porcelainish commands concerning forensics
+ 9 - the git tools
+
+Second digit tells the particular command we are testing.
+
+Third digit (optionally) tells the particular switch or group of switches
+we are testing.
+
+If you create files under t/ directory (i.e. here) that is not
+the top-level test script, never name the file to match the above
+pattern. The Makefile here considers all such files as the
+top-level test script and tries to run all of them. Care is
+especially needed if you are creating a common test library
+file, similar to test-lib.sh, because such a library file may
+not be suitable for standalone execution.
+
+
+Writing Tests
+-------------
+
+The test script is written as a shell script. It should start
+with the standard "#!/bin/sh", and an
+assignment to variable 'test_description', like this:
+
+ #!/bin/sh
+
+ test_description='xxx test (option --frotz)
+
+ This test registers the following structure in the cache
+ and tries to run git-ls-files with option --frotz.'
+
+
+Source 'test-lib.sh'
+--------------------
+
+After assigning test_description, the test script should source
+test-lib.sh like this:
+
+ . ./test-lib.sh
+
+This test harness library does the following things:
+
+ - If the script is invoked with command line argument --help
+ (or -h), it shows the test_description and exits.
+
+ - Creates an empty test directory with an empty .git/objects database
+ and chdir(2) into it. This directory is 't/trash
+ directory.$test_name_without_dotsh', with t/ subject to change by
+ the --root option documented above, and a '.stress-<N>' suffix
+ appended by the --stress option.
+
+ - Defines standard test helper functions for your scripts to
+ use. These functions are designed to make all scripts behave
+ consistently when command line arguments --verbose (or -v),
+ --debug (or -d), and --immediate (or -i) is given.
+
+Recommended style
+-----------------
+
+ - Keep the test_expect_* function call and test title on
+ the same line.
+
+ For example, with test_expect_success, write it like:
+
+ test_expect_success 'test title' '
+ ... test body ...
+ '
+
+ Instead of:
+
+ test_expect_success \
+ 'test title' \
+ '... test body ...'
+
+ - End the line with an opening single quote.
+
+ - Indent here-document bodies, and use "<<-" instead of "<<"
+ to strip leading TABs used for indentation:
+
+ test_expect_success 'test something' '
+ cat >expect <<-\EOF &&
+ one
+ two
+ three
+ EOF
+ test_something > actual &&
+ test_cmp expect actual
+ '
+
+ Instead of:
+
+ test_expect_success 'test something' '
+ cat >expect <<\EOF &&
+ one
+ two
+ three
+ EOF
+ test_something > actual &&
+ test_cmp expect actual
+ '
+
+ - Quote or escape the EOF delimiter that begins a here-document if
+ there is no parameter or other expansion in it, to signal readers
+ that they can skim it more casually:
+
+ cmd <<-\EOF
+ literal here-document text without any expansion
+ EOF
+
+
+Do's & don'ts
+-------------
+
+Here are a few examples of things you probably should and shouldn't do
+when writing tests.
+
+The "do's:"
+
+ - Put all code inside test_expect_success and other assertions.
+
+ Even code that isn't a test per se, but merely some setup code
+ should be inside a test assertion.
+
+ - Chain your test assertions
+
+ Write test code like this:
+
+ git merge foo &&
+ git push bar &&
+ test ...
+
+ Instead of:
+
+ git merge hla
+ git push gh
+ test ...
+
+ That way all of the commands in your tests will succeed or fail. If
+ you must ignore the return value of something, consider using a
+ helper function (e.g. use sane_unset instead of unset, in order
+ to avoid unportable return value for unsetting a variable that was
+ already unset), or prepending the command with test_might_fail or
+ test_must_fail.
+
+ - Check the test coverage for your tests. See the "Test coverage"
+ below.
+
+ Don't blindly follow test coverage metrics; if a new function you added
+ doesn't have any coverage, then you're probably doing something wrong,
+ but having 100% coverage doesn't necessarily mean that you tested
+ everything.
+
+ Tests that are likely to smoke out future regressions are better
+ than tests that just inflate the coverage metrics.
+
+ - When a test checks for an absolute path that a git command generated,
+ construct the expected value using $(pwd) rather than $PWD,
+ $TEST_DIRECTORY, or $TRASH_DIRECTORY. It makes a difference on
+ Windows, where the shell (MSYS bash) mangles absolute path names.
+ For details, see the commit message of 4114156ae9.
+
+ - Remember that inside the <script> part, the standard output and
+ standard error streams are discarded, and the test harness only
+ reports "ok" or "not ok" to the end user running the tests. Under
+ --verbose, they are shown to help debug the tests.
+
+ - Be careful when you loop
+
+ You may need to verify multiple things in a loop, but the
+ following does not work correctly:
+
+ test_expect_success 'test three things' '
+ for i in one two three
+ do
+ test_something "$i"
+ done &&
+ test_something_else
+ '
+
+ Because the status of the loop itself is the exit status of the
+ test_something in the last round, the loop does not fail when
+ "test_something" for "one" or "two" fails. This is not what you
+ want.
+
+ Instead, you can break out of the loop immediately when you see a
+ failure. Because all test_expect_* snippets are executed inside
+ a function, "return 1" can be used to fail the test immediately
+ upon a failure:
+
+ test_expect_success 'test three things' '
+ for i in one two three
+ do
+ test_something "$i" || return 1
+ done &&
+ test_something_else
+ '
+
+ Note that we still &&-chain the loop to propagate failures from
+ earlier commands.
+
+
+And here are the "don'ts:"
+
+ - Don't exit() within a <script> part.
+
+ The harness will catch this as a programming error of the test.
+ Use test_done instead if you need to stop the tests early (see
+ "Skipping tests" below).
+
+ - Don't use '! git cmd' when you want to make sure the git command
+ exits with failure in a controlled way by calling "die()". Instead,
+ use 'test_must_fail git cmd'. This will signal a failure if git
+ dies in an unexpected way (e.g. segfault).
+
+ On the other hand, don't use test_must_fail for running regular
+ platform commands; just use '! cmd'. We are not in the business
+ of verifying that the world given to us sanely works.
+
+ - Don't feed the output of a git command to a pipe, as in:
+
+ git -C repo ls-files |
+ xargs -n 1 basename |
+ grep foo
+
+ which will discard git's exit code and may mask a crash. In the
+ above example, all exit codes are ignored except grep's.
+
+ Instead, write the output of that command to a temporary
+ file with ">" or assign it to a variable with "x=$(git ...)" rather
+ than pipe it.
+
+ - Don't use command substitution in a way that discards git's exit
+ code. When assigning to a variable, the exit code is not discarded,
+ e.g.:
+
+ x=$(git cat-file -p $sha) &&
+ ...
+
+ is OK because a crash in "git cat-file" will cause the "&&" chain
+ to fail, but:
+
+ test "refs/heads/foo" = "$(git symbolic-ref HEAD)"
+
+ is not OK and a crash in git could go undetected.
+
+ - Don't use perl without spelling it as "$PERL_PATH". This is to help
+ our friends on Windows where the platform Perl often adds CR before
+ the end of line, and they bundle Git with a version of Perl that
+ does not do so, whose path is specified with $PERL_PATH. Note that we
+ provide a "perl" function which uses $PERL_PATH under the hood, so
+ you do not need to worry when simply running perl in the test scripts
+ (but you do, for example, on a shebang line or in a sub script
+ created via "write_script").
+
+ - Don't use sh without spelling it as "$SHELL_PATH", when the script
+ can be misinterpreted by broken platform shell (e.g. Solaris).
+
+ - Don't chdir around in tests. It is not sufficient to chdir to
+ somewhere and then chdir back to the original location later in
+ the test, as any intermediate step can fail and abort the test,
+ causing the next test to start in an unexpected directory. Do so
+ inside a subshell if necessary.
+
+ - Don't save and verify the standard error of compound commands, i.e.
+ group commands, subshells, and shell functions (except test helper
+ functions like 'test_must_fail') like this:
+
+ ( cd dir && git cmd ) 2>error &&
+ test_cmp expect error
+
+ When running the test with '-x' tracing, then the trace of commands
+ executed in the compound command will be included in standard error
+ as well, quite possibly throwing off the subsequent checks examining
+ the output. Instead, save only the relevant git command's standard
+ error:
+
+ ( cd dir && git cmd 2>../error ) &&
+ test_cmp expect error
+
+ - Don't break the TAP output
+
+ The raw output from your test may be interpreted by a TAP harness. TAP
+ harnesses will ignore everything they don't know about, but don't step
+ on their toes in these areas:
+
+ - Don't print lines like "$x..$y" where $x and $y are integers.
+
+ - Don't print lines that begin with "ok" or "not ok".
+
+ TAP harnesses expect a line that begins with either "ok" and "not
+ ok" to signal a test passed or failed (and our harness already
+ produces such lines), so your script shouldn't emit such lines to
+ their output.
+
+ You can glean some further possible issues from the TAP grammar
+ (see https://metacpan.org/pod/TAP::Parser::Grammar#TAP-GRAMMAR)
+ but the best indication is to just run the tests with prove(1),
+ it'll complain if anything is amiss.
+
+
+Skipping tests
+--------------
+
+If you need to skip tests you should do so by using the three-arg form
+of the test_* functions (see the "Test harness library" section
+below), e.g.:
+
+ test_expect_success PERL 'I need Perl' '
+ perl -e "hlagh() if unf_unf()"
+ '
+
+The advantage of skipping tests like this is that platforms that don't
+have the PERL and other optional dependencies get an indication of how
+many tests they're missing.
+
+If the test code is too hairy for that (i.e. does a lot of setup work
+outside test assertions) you can also skip all remaining tests by
+setting skip_all and immediately call test_done:
+
+ if ! test_have_prereq PERL
+ then
+ skip_all='skipping perl interface tests, perl not available'
+ test_done
+ fi
+
+The string you give to skip_all will be used as an explanation for why
+the test was skipped.
+
+End with test_done
+------------------
+
+Your script will be a sequence of tests, using helper functions
+from the test harness library. At the end of the script, call
+'test_done'.
+
+
+Test harness library
+--------------------
+
+There are a handful helper functions defined in the test harness
+library for your script to use. Some of them are listed below;
+see test-lib-functions.sh for the full list and their options.
+
+ - test_expect_success [<prereq>] <message> <script>
+
+ Usually takes two strings as parameters, and evaluates the
+ <script>. If it yields success, test is considered
+ successful. <message> should state what it is testing.
+
+ Example:
+
+ test_expect_success \
+ 'git-write-tree should be able to write an empty tree.' \
+ 'tree=$(git-write-tree)'
+
+ If you supply three parameters the first will be taken to be a
+ prerequisite; see the test_set_prereq and test_have_prereq
+ documentation below:
+
+ test_expect_success TTY 'git --paginate rev-list uses a pager' \
+ ' ... '
+
+ You can also supply a comma-separated list of prerequisites, in the
+ rare case where your test depends on more than one:
+
+ test_expect_success PERL,PYTHON 'yo dawg' \
+ ' test $(perl -E '\''print eval "1 +" . qx[python -c "print(2)"]'\'') = "4" '
+
+ - test_expect_failure [<prereq>] <message> <script>
+
+ This is NOT the opposite of test_expect_success, but is used
+ to mark a test that demonstrates a known breakage. Unlike
+ the usual test_expect_success tests, which say "ok" on
+ success and "FAIL" on failure, this will say "FIXED" on
+ success and "still broken" on failure. Failures from these
+ tests won't cause -i (immediate) to stop.
+
+ Like test_expect_success this function can optionally use a three
+ argument invocation with a prerequisite as the first argument.
+
+ - test_debug <script>
+
+ This takes a single argument, <script>, and evaluates it only
+ when the test script is started with --debug command line
+ argument. This is primarily meant for use during the
+ development of a new test script.
+
+ - debug [options] <git-command>
+
+ Run a git command inside a debugger. This is primarily meant for
+ use when debugging a failing test script. With '-t', use your
+ original TERM instead of test-lib.sh's "dumb", so that your
+ debugger interface has colors.
+
+ - test_done
+
+ Your test script must have test_done at the end. Its purpose
+ is to summarize successes and failures in the test script and
+ exit with an appropriate error code.
+
+ - test_tick
+
+ Make commit and tag names consistent by setting the author and
+ committer times to defined state. Subsequent calls will
+ advance the times by a fixed amount.
+
+ - test_commit <message> [<filename> [<contents>]]
+
+ Creates a commit with the given message, committing the given
+ file with the given contents (default for both is to reuse the
+ message string), and adds a tag (again reusing the message
+ string as name). Calls test_tick to make the SHA-1s
+ reproducible.
+
+ - test_merge <message> <commit-or-tag>
+
+ Merges the given rev using the given message. Like test_commit,
+ creates a tag and calls test_tick before committing.
+
+ - test_set_prereq <prereq>
+
+ Set a test prerequisite to be used later with test_have_prereq. The
+ test-lib will set some prerequisites for you, see the
+ "Prerequisites" section below for a full list of these.
+
+ Others you can set yourself and use later with either
+ test_have_prereq directly, or the three argument invocation of
+ test_expect_success and test_expect_failure.
+
+ - test_have_prereq <prereq>
+
+ Check if we have a prerequisite previously set with test_set_prereq.
+ The most common way to use this explicitly (as opposed to the
+ implicit use when an argument is passed to test_expect_*) is to skip
+ all the tests at the start of the test script if we don't have some
+ essential prerequisite:
+
+ if ! test_have_prereq PERL
+ then
+ skip_all='skipping perl interface tests, perl not available'
+ test_done
+ fi
+
+ - test_expect_code <exit-code> <command>
+
+ Run a command and ensure that it exits with the given exit code.
+ For example:
+
+ test_expect_success 'Merge with d/f conflicts' '
+ test_expect_code 1 git merge "merge msg" B master
+ '
+
+ - test_must_fail [<options>] <git-command>
+
+ Run a git command and ensure it fails in a controlled way. Use
+ this instead of "! <git-command>". When git-command dies due to a
+ segfault, test_must_fail diagnoses it as an error; "! <git-command>"
+ treats it as just another expected failure, which would let such a
+ bug go unnoticed.
+
+ Accepts the following options:
+
+ ok=<signal-name>[,<...>]:
+ Don't treat an exit caused by the given signal as error.
+ Multiple signals can be specified as a comma separated list.
+ Currently recognized signal names are: sigpipe, success.
+ (Don't use 'success', use 'test_might_fail' instead.)
+
+ - test_might_fail [<options>] <git-command>
+
+ Similar to test_must_fail, but tolerate success, too. Use this
+ instead of "<git-command> || :" to catch failures due to segv.
+
+ Accepts the same options as test_must_fail.
+
+ - test_cmp <expected> <actual>
+
+ Check whether the content of the <actual> file matches the
+ <expected> file. This behaves like "cmp" but produces more
+ helpful output when the test is run with "-v" option.
+
+ - test_cmp_rev <expected> <actual>
+
+ Check whether the <expected> rev points to the same commit as the
+ <actual> rev.
+
+ - test_line_count (= | -lt | -ge | ...) <length> <file>
+
+ Check whether a file has the length it is expected to.
+
+ - test_path_is_file <path>
+ test_path_is_dir <path>
+ test_path_is_missing <path>
+
+ Check if the named path is a file, if the named path is a
+ directory, or if the named path does not exist, respectively,
+ and fail otherwise.
+
+ - test_when_finished <script>
+
+ Prepend <script> to a list of commands to run to clean up
+ at the end of the current test. If some clean-up command
+ fails, the test will not pass.
+
+ Example:
+
+ test_expect_success 'branch pointing to non-commit' '
+ git rev-parse HEAD^{tree} >.git/refs/heads/invalid &&
+ test_when_finished "git update-ref -d refs/heads/invalid" &&
+ ...
+ '
+
+ - test_atexit <script>
+
+ Prepend <script> to a list of commands to run unconditionally to
+ clean up before the test script exits, e.g. to stop a daemon:
+
+ test_expect_success 'test git daemon' '
+ git daemon &
+ daemon_pid=$! &&
+ test_atexit 'kill $daemon_pid' &&
+ hello world
+ '
+
+ The commands will be executed before the trash directory is removed,
+ i.e. the atexit commands will still be able to access any pidfiles or
+ socket files.
+
+ Note that these commands will be run even when a test script run
+ with '--immediate' fails. Be careful with your atexit commands to
+ minimize any changes to the failed state.
+
+ - test_write_lines <lines>
+
+ Write <lines> on standard output, one line per argument.
+ Useful to prepare multi-line files in a compact form.
+
+ Example:
+
+ test_write_lines a b c d e f g >foo
+
+ Is a more compact equivalent of:
+ cat >foo <<-EOF
+ a
+ b
+ c
+ d
+ e
+ f
+ g
+ EOF
+
+
+ - test_pause [options]
+
+ This command is useful for writing and debugging tests and must be
+ removed before submitting. It halts the execution of the test and
+ spawns a shell in the trash directory. Exit the shell to continue
+ the test. Example:
+
+ test_expect_success 'test' '
+ git do-something >actual &&
+ test_pause &&
+ test_cmp expected actual
+ '
+
+ - test_ln_s_add <path1> <path2>
+
+ This function helps systems whose filesystem does not support symbolic
+ links. Use it to add a symbolic link entry to the index when it is not
+ important that the file system entry is a symbolic link, i.e., instead
+ of the sequence
+
+ ln -s foo bar &&
+ git add bar
+
+ Sometimes it is possible to split a test in a part that does not need
+ the symbolic link in the file system and a part that does; then only
+ the latter part need be protected by a SYMLINKS prerequisite (see below).
+
+ - test_path_is_executable
+
+ This tests whether a file is executable and prints an error message
+ if not. This must be used only under the POSIXPERM prerequisite
+ (see below).
+
+ - test_oid_init
+
+ This function loads facts and useful object IDs related to the hash
+ algorithm(s) in use from the files in t/oid-info.
+
+ - test_oid_cache
+
+ This function reads per-hash algorithm information from standard
+ input (usually a heredoc) in the format described in
+ t/oid-info/README. This is useful for test-specific values, such as
+ object IDs, which must vary based on the hash algorithm.
+
+ Certain fixed values, such as hash sizes and common placeholder
+ object IDs, can be loaded with test_oid_init (described above).
+
+ - test_oid <key>
+
+ This function looks up a value for the hash algorithm in use, based
+ on the key given. The value must have been loaded using
+ test_oid_init or test_oid_cache. Providing an unknown key is an
+ error.
+
+ - yes [<string>]
+
+ This is often seen in modern UNIX but some platforms lack it, so
+ the test harness overrides the platform implementation with a
+ more limited one. Use this only when feeding a handful lines of
+ output to the downstream---unlike the real version, it generates
+ only up to 99 lines.
+
+ - test_bool_env <env-variable-name> <default-value>
+
+ Given the name of an environment variable with a bool value,
+ normalize its value to a 0 (true) or 1 (false or empty string)
+ return code. Return with code corresponding to the given default
+ value if the variable is unset.
+ Abort the test script if either the value of the variable or the
+ default are not valid bool values.
+
+
+Prerequisites
+-------------
+
+These are the prerequisites that the test library predefines with
+test_have_prereq.
+
+See the prereq argument to the test_* functions in the "Test harness
+library" section above and the "test_have_prereq" function for how to
+use these, and "test_set_prereq" for how to define your own.
+
+ - PYTHON
+
+ Git wasn't compiled with NO_PYTHON=YesPlease. Wrap any tests that
+ need Python with this.
+
+ - PERL
+
+ Git wasn't compiled with NO_PERL=YesPlease.
+
+ Even without the PERL prerequisite, tests can assume there is a
+ usable perl interpreter at $PERL_PATH, though it need not be
+ particularly modern.
+
+ - POSIXPERM
+
+ The filesystem supports POSIX style permission bits.
+
+ - BSLASHPSPEC
+
+ Backslashes in pathspec are not directory separators. This is not
+ set on Windows. See 6fd1106a for details.
+
+ - EXECKEEPSPID
+
+ The process retains the same pid across exec(2). See fb9a2bea for
+ details.
+
+ - PIPE
+
+ The filesystem we're on supports creation of FIFOs (named pipes)
+ via mkfifo(1).
+
+ - SYMLINKS
+
+ The filesystem we're on supports symbolic links. E.g. a FAT
+ filesystem doesn't support these. See 704a3143 for details.
+
+ - SANITY
+
+ Test is not run by root user, and an attempt to write to an
+ unwritable file is expected to fail correctly.
+
+ - PCRE
+
+ Git was compiled with support for PCRE. Wrap any tests
+ that use git-grep --perl-regexp or git-grep -P in these.
+
+ - CASE_INSENSITIVE_FS
+
+ Test is run on a case insensitive file system.
+
+ - UTF8_NFD_TO_NFC
+
+ Test is run on a filesystem which converts decomposed utf-8 (nfd)
+ to precomposed utf-8 (nfc).
+
+ - PTHREADS
+
+ Git wasn't compiled with NO_PTHREADS=YesPlease.
+
+ - REFFILES
+
+ Test is specific to packed/loose ref storage, and should be
+ disabled for other ref storage backends
+
+
+Tips for Writing Tests
+----------------------
+
+As with any programming projects, existing programs are the best
+source of the information. However, do _not_ emulate
+t0000-basic.sh when writing your tests. The test is special in
+that it tries to validate the very core of Git. For example, it
+knows that there will be 256 subdirectories under .git/objects/,
+and it knows that the object ID of an empty tree is a certain
+40-byte string. This is deliberately done so in t0000-basic.sh
+because the things the very basic core test tries to achieve is
+to serve as a basis for people who are changing the Git internals
+drastically. For these people, after making certain changes,
+not seeing failures from the basic test _is_ a failure. Any
+Git core changes so drastic that they change even these
+otherwise supposedly stable object IDs should be accompanied by
+an update to t0000-basic.sh.
+
+However, other tests that simply rely on basic parts of the core
+Git working properly should not have that level of intimate
+knowledge of the core Git internals. If all the test scripts
+hardcoded the object IDs like t0000-basic.sh does, that defeats
+the purpose of t0000-basic.sh, which is to isolate that level of
+validation in one place. Your test also ends up needing
+an update whenever the internals change, so do _not_
+do it and leave the low level of validation to t0000-basic.sh.
+
+Test coverage
+-------------
+
+You can use the coverage tests to find code paths that are not being
+used or properly exercised yet.
+
+To do that, run the coverage target at the top-level (not in the t/
+directory):
+
+ make coverage
+
+That'll compile Git with GCC's coverage arguments, and generate a test
+report with gcov after the tests finish. Running the coverage tests
+can take a while, since running the tests in parallel is incompatible
+with GCC's coverage mode.
+
+After the tests have run you can generate a list of untested
+functions:
+
+ make coverage-untested-functions
+
+You can also generate a detailed per-file HTML report using the
+Devel::Cover module. To install it do:
+
+ # On Debian or Ubuntu:
+ sudo aptitude install libdevel-cover-perl
+
+ # From the CPAN with cpanminus
+ curl -L http://cpanmin.us | perl - --sudo --self-upgrade
+ cpanm --sudo Devel::Cover
+
+Then, at the top-level:
+
+ make cover_db_html
+
+That'll generate a detailed cover report in the "cover_db_html"
+directory, which you can then copy to a webserver, or inspect locally
+in a browser.
diff --git a/t/aggregate-results.sh b/t/aggregate-results.sh
new file mode 100755
index 0000000..6e3bcc4
--- /dev/null
+++ b/t/aggregate-results.sh
@@ -0,0 +1,63 @@
+#!/bin/sh
+
+failed_tests=
+fixed=0
+success=0
+failed=0
+broken=0
+total=0
+missing_prereq=
+
+for file in "$1"/t*-*.counts
+do
+ while read type value
+ do
+ case $type in
+ '')
+ continue ;;
+ fixed)
+ fixed=$(($fixed + $value)) ;;
+ success)
+ success=$(($success + $value)) ;;
+ failed)
+ failed=$(($failed + $value))
+ if test $value != 0
+ then
+ testnum=$(expr "$file" : 'test-results/\(t[0-9]*\)-')
+ failed_tests="$failed_tests $testnum"
+ fi
+ ;;
+ broken)
+ broken=$(($broken + $value)) ;;
+ total)
+ total=$(($total + $value)) ;;
+ missing_prereq)
+ missing_prereq="$missing_prereq,$value" ;;
+ esac
+ done <"$file"
+done
+
+if test -n "$missing_prereq"
+then
+ unique_missing_prereq=$(
+ echo $missing_prereq |
+ tr -s "," "\n" |
+ grep -v '^$' |
+ sort -u |
+ paste -s -d ' ')
+ if test -n "$unique_missing_prereq"
+ then
+ printf "\nmissing prereq: $unique_missing_prereq\n\n"
+ fi
+fi
+
+if test -n "$failed_tests"
+then
+ printf "\nfailed test(s):$failed_tests\n\n"
+fi
+
+printf "%-8s%d\n" fixed $fixed
+printf "%-8s%d\n" success $success
+printf "%-8s%d\n" failed $failed
+printf "%-8s%d\n" broken $broken
+printf "%-8s%d\n" total $total
diff --git a/t/annotate-tests.sh b/t/annotate-tests.sh
new file mode 100644
index 0000000..5e21e84
--- /dev/null
+++ b/t/annotate-tests.sh
@@ -0,0 +1,677 @@
+# This file isn't used as a test script directly, instead it is
+# sourced from t8001-annotate.sh and t8002-blame.sh.
+
+if test_have_prereq MINGW
+then
+ sanitize_L () {
+ echo "$1" | sed 'sX\(^-L\|,\)\^\?/X&\\;*Xg'
+ }
+else
+ sanitize_L () {
+ echo "$1"
+ }
+fi
+
+check_count () {
+ head= &&
+ file='file' &&
+ options= &&
+ while :
+ do
+ case "$1" in
+ -h) head="$2"; shift; shift ;;
+ -f) file="$2"; shift; shift ;;
+ -L*) options="$options $(sanitize_L "$1")"; shift ;;
+ -*) options="$options $1"; shift ;;
+ *) break ;;
+ esac
+ done &&
+ echo "$PROG $options $file $head" >&4 &&
+ $PROG $options $file $head >actual &&
+ perl -e '
+ my %expect = (@ARGV);
+ my %count = map { $_ => 0 } keys %expect;
+ while (<STDIN>) {
+ if (/^[0-9a-f]+\t\(([^\t]+)\t/) {
+ my $author = $1;
+ for ($author) { s/^\s*//; s/\s*$//; }
+ $count{$author}++;
+ }
+ }
+ my $bad = 0;
+ while (my ($author, $count) = each %count) {
+ my $ok;
+ my $value = 0;
+ $value = $expect{$author} if defined $expect{$author};
+ if ($value != $count) {
+ $bad = 1;
+ $ok = "bad";
+ }
+ else {
+ $ok = "good";
+ }
+ print STDERR "Author $author (expected $value, attributed $count) $ok\n";
+ }
+ exit($bad);
+ ' "$@" <actual
+}
+
+get_progress_result () {
+ tr '\015' '\012' | tail -n 1
+}
+
+test_expect_success 'setup A lines' '
+ echo "1A quick brown fox jumps over the" >file &&
+ echo "lazy dog" >>file &&
+ git add file &&
+ GIT_AUTHOR_NAME="A" GIT_AUTHOR_EMAIL="A@test.git" \
+ git commit -a -m "Initial."
+'
+
+test_expect_success 'blame 1 author' '
+ check_count A 2
+'
+
+test_expect_success 'blame working copy' '
+ test_when_finished "git restore file" &&
+ echo "1A quick brown fox jumps over the" >file &&
+ echo "another lazy dog" >>file &&
+ check_count A 1 "Not Committed Yet" 1
+'
+
+test_expect_success 'blame with --contents' '
+ check_count --contents=file A 2
+'
+
+test_expect_success 'blame with --contents in a bare repo' '
+ git clone --bare . bare-contents.git &&
+ (
+ cd bare-contents.git &&
+ echo "1A quick brown fox jumps over the" >contents &&
+ check_count --contents=contents A 1
+ )
+'
+
+test_expect_success 'blame with --contents changed' '
+ echo "1A quick brown fox jumps over the" >contents &&
+ echo "another lazy dog" >>contents &&
+ check_count --contents=contents A 1 "External file (--contents)" 1
+'
+
+test_expect_success 'blame in a bare repo without starting commit' '
+ git clone --bare . bare.git &&
+ (
+ cd bare.git &&
+ check_count A 2
+ )
+'
+
+test_expect_success 'blame by tag objects' '
+ git tag -m "test tag" testTag &&
+ git tag -m "test tag #2" testTag2 testTag &&
+ check_count -h testTag A 2 &&
+ check_count -h testTag2 A 2
+'
+
+test_expect_success 'setup B lines' '
+ echo "2A quick brown fox jumps over the" >>file &&
+ echo "lazy dog" >>file &&
+ GIT_AUTHOR_NAME="B" GIT_AUTHOR_EMAIL="B@test.git" \
+ git commit -a -m "Second."
+'
+
+test_expect_success 'blame 2 authors' '
+ check_count A 2 B 2
+'
+
+test_expect_success 'blame with --contents and revision' '
+ check_count -h testTag --contents=file A 2 "External file (--contents)" 2
+'
+
+test_expect_success 'setup B1 lines (branch1)' '
+ git checkout -b branch1 main &&
+ echo "3A slow green fox jumps into the" >>file &&
+ echo "well." >>file &&
+ GIT_AUTHOR_NAME="B1" GIT_AUTHOR_EMAIL="B1@test.git" \
+ git commit -a -m "Branch1-1"
+'
+
+test_expect_success 'blame 2 authors + 1 branch1 author' '
+ check_count A 2 B 2 B1 2
+'
+
+test_expect_success 'setup B2 lines (branch2)' '
+ git checkout -b branch2 main &&
+ sed -e "s/2A quick brown/4A quick brown lazy dog/" <file >file.new &&
+ mv file.new file &&
+ GIT_AUTHOR_NAME="B2" GIT_AUTHOR_EMAIL="B2@test.git" \
+ git commit -a -m "Branch2-1"
+'
+
+test_expect_success 'blame 2 authors + 1 branch2 author' '
+ check_count A 2 B 1 B2 1
+'
+
+test_expect_success 'merge branch1 & branch2' '
+ git merge branch1
+'
+
+test_expect_success 'blame 2 authors + 2 merged-in authors' '
+ check_count A 2 B 1 B1 2 B2 1
+'
+
+test_expect_success 'blame --first-parent blames merge for branch1' '
+ check_count --first-parent A 2 B 1 "A U Thor" 2 B2 1
+'
+
+test_expect_success 'blame ancestor' '
+ check_count -h main A 2 B 2
+'
+
+test_expect_success 'blame great-ancestor' '
+ check_count -h main^ A 2
+'
+
+test_expect_success 'setup evil merge' '
+ echo "evil merge." >>file &&
+ git commit -a --amend
+'
+
+test_expect_success 'blame evil merge' '
+ check_count A 2 B 1 B1 2 B2 1 "A U Thor" 1
+'
+
+test_expect_success 'blame huge graft' '
+ test_when_finished "git checkout branch2" &&
+ test_when_finished "rm -rf .git/info" &&
+ graft= &&
+ for i in 0 1 2
+ do
+ for j in 0 1 2 3 4 5 6 7 8 9
+ do
+ git checkout --orphan "$i$j" &&
+ printf "%s\n" "$i" "$j" >file &&
+ test_tick &&
+ GIT_AUTHOR_NAME=$i$j GIT_AUTHOR_EMAIL=$i$j@test.git \
+ git commit -a -m "$i$j" &&
+ commit=$(git rev-parse --verify HEAD) &&
+ graft="$graft$commit " || return 1
+ done
+ done &&
+ mkdir .git/info &&
+ printf "%s " $graft >.git/info/grafts &&
+ check_count -h 00 01 1 10 1
+'
+
+test_expect_success 'setup incomplete line' '
+ echo "incomplete" | tr -d "\\012" >>file &&
+ GIT_AUTHOR_NAME="C" GIT_AUTHOR_EMAIL="C@test.git" \
+ git commit -a -m "Incomplete"
+'
+
+test_expect_success 'blame incomplete line' '
+ check_count A 2 B 1 B1 2 B2 1 "A U Thor" 1 C 1
+'
+
+test_expect_success 'setup edits' '
+ mv file file.orig &&
+ {
+ cat file.orig &&
+ echo
+ } | sed -e "s/^3A/99/" -e "/^1A/d" -e "/^incomplete/d" >file &&
+ echo "incomplete" | tr -d "\\012" >>file &&
+ GIT_AUTHOR_NAME="D" GIT_AUTHOR_EMAIL="D@test.git" \
+ git commit -a -m "edit"
+'
+
+test_expect_success 'blame edits' '
+ check_count A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1
+'
+
+test_expect_success 'setup obfuscated email' '
+ echo "No robots allowed" >file.new &&
+ cat file >>file.new &&
+ mv file.new file &&
+ GIT_AUTHOR_NAME="E" GIT_AUTHOR_EMAIL="E at test dot git" \
+ git commit -a -m "norobots"
+'
+
+test_expect_success 'blame obfuscated email' '
+ check_count A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1 E 1
+'
+
+test_expect_success 'blame -L 1 (all)' '
+ check_count -L1 A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1 E 1
+'
+
+test_expect_success 'blame -L , (all)' '
+ check_count -L, A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1 E 1
+'
+
+test_expect_success 'blame -L X (X to end)' '
+ check_count -L5 B1 1 C 1 D 1 "A U Thor" 1
+'
+
+test_expect_success 'blame -L X, (X to end)' '
+ check_count -L5, B1 1 C 1 D 1 "A U Thor" 1
+'
+
+test_expect_success 'blame -L ,Y (up to Y)' '
+ check_count -L,3 A 1 B2 1 E 1
+'
+
+test_expect_success 'blame -L X,X' '
+ check_count -L3,3 B2 1
+'
+
+test_expect_success 'blame -L X,Y' '
+ check_count -L3,6 B 1 B1 1 B2 1 D 1
+'
+
+test_expect_success 'blame -L Y,X (undocumented)' '
+ check_count -L6,3 B 1 B1 1 B2 1 D 1
+'
+
+test_expect_success 'blame -L -X' '
+ test_must_fail $PROG -L-1 file
+'
+
+test_expect_success 'blame -L 0' '
+ test_must_fail $PROG -L0 file
+'
+
+test_expect_success 'blame -L ,0' '
+ test_must_fail $PROG -L,0 file
+'
+
+test_expect_success 'blame -L ,+0' '
+ test_must_fail $PROG -L,+0 file
+'
+
+test_expect_success 'blame -L X,+0' '
+ test_must_fail $PROG -L1,+0 file
+'
+
+test_expect_success 'blame -L X,+1' '
+ check_count -L3,+1 B2 1
+'
+
+test_expect_success 'blame -L X,+N' '
+ check_count -L3,+4 B 1 B1 1 B2 1 D 1
+'
+
+test_expect_success 'blame -L ,-0' '
+ test_must_fail $PROG -L,-0 file
+'
+
+test_expect_success 'blame -L X,-0' '
+ test_must_fail $PROG -L1,-0 file
+'
+
+test_expect_success 'blame -L X,-1' '
+ check_count -L3,-1 B2 1
+'
+
+test_expect_success 'blame -L X,-N' '
+ check_count -L6,-4 B 1 B1 1 B2 1 D 1
+'
+
+test_expect_success 'blame -L /RE/ (RE to end)' '
+ check_count -L/evil/ C 1 "A U Thor" 1
+'
+
+test_expect_success 'blame -L /RE/,/RE2/' '
+ check_count -L/robot/,/green/ A 1 B 1 B2 1 D 1 E 1
+'
+
+test_expect_success 'blame -L X,/RE/' '
+ check_count -L5,/evil/ B1 1 D 1 "A U Thor" 1
+'
+
+test_expect_success 'blame -L /RE/,Y' '
+ check_count -L/99/,7 B1 1 D 1 "A U Thor" 1
+'
+
+test_expect_success 'blame -L /RE/,+N' '
+ check_count -L/99/,+3 B1 1 D 1 "A U Thor" 1
+'
+
+test_expect_success 'blame -L /RE/,-N' '
+ check_count -L/99/,-3 B 1 B2 1 D 1
+'
+
+# 'file' ends with an incomplete line, so 'wc' reports one fewer lines than
+# git-blame sees, hence the last line is actually $(wc...)+1.
+test_expect_success 'blame -L X (X == nlines)' '
+ n=$(expr $(wc -l <file) + 1) &&
+ check_count -L$n C 1
+'
+
+test_expect_success 'blame -L X (X == nlines + 1)' '
+ n=$(expr $(wc -l <file) + 2) &&
+ test_must_fail $PROG -L$n file
+'
+
+test_expect_success 'blame -L X (X > nlines)' '
+ test_must_fail $PROG -L12345 file
+'
+
+test_expect_success 'blame -L ,Y (Y == nlines)' '
+ n=$(expr $(wc -l <file) + 1) &&
+ check_count -L,$n A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1 E 1
+'
+
+test_expect_success 'blame -L ,Y (Y == nlines + 1)' '
+ n=$(expr $(wc -l <file) + 2) &&
+ check_count -L,$n A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1 E 1
+'
+
+test_expect_success 'blame -L ,Y (Y > nlines)' '
+ check_count -L,12345 A 1 B 1 B1 1 B2 1 "A U Thor" 1 C 1 D 1 E 1
+'
+
+test_expect_success 'blame -L multiple (disjoint)' '
+ check_count -L2,3 -L6,7 A 1 B1 1 B2 1 "A U Thor" 1
+'
+
+test_expect_success 'blame -L multiple (disjoint: unordered)' '
+ check_count -L6,7 -L2,3 A 1 B1 1 B2 1 "A U Thor" 1
+'
+
+test_expect_success 'blame -L multiple (adjacent)' '
+ check_count -L2,3 -L4,5 A 1 B 1 B2 1 D 1
+'
+
+test_expect_success 'blame -L multiple (adjacent: unordered)' '
+ check_count -L4,5 -L2,3 A 1 B 1 B2 1 D 1
+'
+
+test_expect_success 'blame -L multiple (overlapping)' '
+ check_count -L2,4 -L3,5 A 1 B 1 B2 1 D 1
+'
+
+test_expect_success 'blame -L multiple (overlapping: unordered)' '
+ check_count -L3,5 -L2,4 A 1 B 1 B2 1 D 1
+'
+
+test_expect_success 'blame -L multiple (superset/subset)' '
+ check_count -L2,8 -L3,5 A 1 B 1 B1 1 B2 1 C 1 D 1 "A U Thor" 1
+'
+
+test_expect_success 'blame -L multiple (superset/subset: unordered)' '
+ check_count -L3,5 -L2,8 A 1 B 1 B1 1 B2 1 C 1 D 1 "A U Thor" 1
+'
+
+test_expect_success 'blame -L /RE/ (relative)' '
+ check_count -L3,3 -L/fox/ B1 1 B2 1 C 1 D 1 "A U Thor" 1
+'
+
+test_expect_success 'blame -L /RE/ (relative: no preceding range)' '
+ check_count -L/dog/ A 1 B 1 B1 1 B2 1 C 1 D 1 "A U Thor" 1
+'
+
+test_expect_success 'blame -L /RE/ (relative: adjacent)' '
+ check_count -L1,1 -L/dog/,+1 A 1 E 1
+'
+
+test_expect_success 'blame -L /RE/ (relative: not found)' '
+ test_must_fail $PROG -L4,4 -L/dog/ file
+'
+
+test_expect_success 'blame -L /RE/ (relative: end-of-file)' '
+ test_must_fail $PROG -L, -L/$/ file
+'
+
+test_expect_success 'blame -L ^/RE/ (absolute)' '
+ check_count -L3,3 -L^/dog/,+2 A 1 B2 1
+'
+
+test_expect_success 'blame -L ^/RE/ (absolute: no preceding range)' '
+ check_count -L^/dog/,+2 A 1 B2 1
+'
+
+test_expect_success 'blame -L ^/RE/ (absolute: not found)' '
+ test_must_fail $PROG -L4,4 -L^/tambourine/ file
+'
+
+test_expect_success 'blame -L ^/RE/ (absolute: end-of-file)' '
+ n=$(expr $(wc -l <file) + 1) &&
+ check_count -L$n -L^/$/,+2 A 1 C 1 E 1
+'
+
+test_expect_success 'setup -L :regex' '
+ tr Q "\\t" >hello.c <<-\EOF &&
+ int main(int argc, const char *argv[])
+ {
+ Qputs("hello");
+ }
+ EOF
+ git add hello.c &&
+ GIT_AUTHOR_NAME="F" GIT_AUTHOR_EMAIL="F@test.git" \
+ git commit -m "hello" &&
+
+ mv hello.c hello.orig &&
+ sed -e "/}/ {x; s/$/Qputs(\"goodbye\");/; G;}" <hello.orig |
+ tr Q "\\t" >hello.c &&
+ GIT_AUTHOR_NAME="G" GIT_AUTHOR_EMAIL="G@test.git" \
+ git commit -a -m "goodbye" &&
+
+ mv hello.c hello.orig &&
+ echo "#include <stdio.h>" >hello.c &&
+ cat hello.orig >>hello.c &&
+ tr Q "\\t" >>hello.c <<-\EOF &&
+ void mail()
+ {
+ Qputs("mail");
+ }
+ EOF
+ GIT_AUTHOR_NAME="H" GIT_AUTHOR_EMAIL="H@test.git" \
+ git commit -a -m "mail"
+'
+
+test_expect_success 'blame -L :literal' '
+ check_count -f hello.c -L:main F 4 G 1
+'
+
+test_expect_success 'blame -L :regex' '
+ check_count -f hello.c "-L:m[a-z][a-z]l" H 4
+'
+
+test_expect_success 'blame -L :nomatch' '
+ test_must_fail $PROG -L:nomatch hello.c
+'
+
+test_expect_success 'blame -L :RE (relative)' '
+ check_count -f hello.c -L3,3 -L:ma.. F 1 H 4
+'
+
+test_expect_success 'blame -L :RE (relative: no preceding range)' '
+ check_count -f hello.c -L:ma.. F 4 G 1
+'
+
+test_expect_success 'blame -L :RE (relative: not found)' '
+ test_must_fail $PROG -L3,3 -L:tambourine hello.c
+'
+
+test_expect_success 'blame -L :RE (relative: end-of-file)' '
+ test_must_fail $PROG -L, -L:main hello.c
+'
+
+test_expect_success 'blame -L ^:RE (absolute)' '
+ check_count -f hello.c -L3,3 -L^:ma.. F 4 G 1
+'
+
+test_expect_success 'blame -L ^:RE (absolute: no preceding range)' '
+ check_count -f hello.c -L^:ma.. F 4 G 1
+'
+
+test_expect_success 'blame -L ^:RE (absolute: not found)' '
+ test_must_fail $PROG -L4,4 -L^:tambourine hello.c
+'
+
+test_expect_success 'blame -L ^:RE (absolute: end-of-file)' '
+ n=$(printf "%d" $(wc -l <hello.c)) &&
+ check_count -f hello.c -L$n -L^:ma.. F 4 G 1 H 1
+'
+
+test_expect_success 'blame -L :funcname with userdiff driver' '
+ cat >file.template <<-\EOF &&
+ DO NOT MATCH THIS LINE
+ function RIGHT(a, b) result(c)
+ AS THE DEFAULT DRIVER WOULD
+
+ integer, intent(in) :: ChangeMe
+ EOF
+
+ fortran_file=file.f03 &&
+ test_when_finished "rm .gitattributes" &&
+ echo "$fortran_file diff=fortran" >.gitattributes &&
+
+ test_commit --author "A <A@test.git>" \
+ "add" "$fortran_file" \
+ "$(cat file.template)" &&
+ test_commit --author "B <B@test.git>" \
+ "change" "$fortran_file" \
+ "$(cat file.template | sed -e s/ChangeMe/IWasChanged/)" &&
+ check_count -f "$fortran_file" -L:RIGHT A 3 B 1
+'
+
+test_expect_success 'setup incremental' '
+ (
+ GIT_AUTHOR_NAME=I &&
+ export GIT_AUTHOR_NAME &&
+ GIT_AUTHOR_EMAIL=I@test.git &&
+ export GIT_AUTHOR_EMAIL &&
+ >incremental &&
+ git add incremental &&
+ git commit -m "step 0" &&
+ printf "partial" >>incremental &&
+ git commit -a -m "step 0.5" &&
+ echo >>incremental &&
+ git commit -a -m "step 1"
+ )
+'
+
+test_expect_success 'blame empty' '
+ check_count -h HEAD^^ -f incremental
+'
+
+test_expect_success 'blame -L 0 empty' '
+ test_must_fail $PROG -L0 incremental HEAD^^
+'
+
+test_expect_success 'blame -L 1 empty' '
+ test_must_fail $PROG -L1 incremental HEAD^^
+'
+
+test_expect_success 'blame -L 2 empty' '
+ test_must_fail $PROG -L2 incremental HEAD^^
+'
+
+test_expect_success 'blame half' '
+ check_count -h HEAD^ -f incremental I 1
+'
+
+test_expect_success 'blame -L 0 half' '
+ test_must_fail $PROG -L0 incremental HEAD^
+'
+
+test_expect_success 'blame -L 1 half' '
+ check_count -h HEAD^ -f incremental -L1 I 1
+'
+
+test_expect_success 'blame -L 2 half' '
+ test_must_fail $PROG -L2 incremental HEAD^
+'
+
+test_expect_success 'blame -L 3 half' '
+ test_must_fail $PROG -L3 incremental HEAD^
+'
+
+test_expect_success 'blame full' '
+ check_count -f incremental I 1
+'
+
+test_expect_success 'blame -L 0 full' '
+ test_must_fail $PROG -L0 incremental
+'
+
+test_expect_success 'blame -L 1 full' '
+ check_count -f incremental -L1 I 1
+'
+
+test_expect_success 'blame -L 2 full' '
+ test_must_fail $PROG -L2 incremental
+'
+
+test_expect_success 'blame -L 3 full' '
+ test_must_fail $PROG -L3 incremental
+'
+
+test_expect_success 'blame -L' '
+ test_must_fail $PROG -L file
+'
+
+test_expect_success 'blame -L X,+' '
+ test_must_fail $PROG -L1,+ file
+'
+
+test_expect_success 'blame -L X,-' '
+ test_must_fail $PROG -L1,- file
+'
+
+test_expect_success 'blame -L X (non-numeric X)' '
+ test_must_fail $PROG -LX file
+'
+
+test_expect_success 'blame -L X,Y (non-numeric Y)' '
+ test_must_fail $PROG -L1,Y file
+'
+
+test_expect_success 'blame -L X,+N (non-numeric N)' '
+ test_must_fail $PROG -L1,+N file
+'
+
+test_expect_success 'blame -L X,-N (non-numeric N)' '
+ test_must_fail $PROG -L1,-N file
+'
+
+test_expect_success 'blame -L ,^/RE/' '
+ test_must_fail $PROG -L1,^/99/ file
+'
+
+test_expect_success 'blame progress on a full file' '
+ cat >expect <<-\EOF &&
+ Blaming lines: 100% (10/10), done.
+ EOF
+
+ GIT_PROGRESS_DELAY=0 \
+ git blame --progress hello.c 2>stderr &&
+
+ get_progress_result <stderr >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'blame progress on a single range' '
+ cat >expect <<-\EOF &&
+ Blaming lines: 100% (4/4), done.
+ EOF
+
+ GIT_PROGRESS_DELAY=0 \
+ git blame --progress -L 3,6 hello.c 2>stderr &&
+
+ get_progress_result <stderr >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'blame progress on multiple ranges' '
+ cat >expect <<-\EOF &&
+ Blaming lines: 100% (7/7), done.
+ EOF
+
+ GIT_PROGRESS_DELAY=0 \
+ git blame --progress -L 3,6 -L 8,10 hello.c 2>stderr &&
+
+ get_progress_result <stderr >actual &&
+ test_cmp expect actual
+'
diff --git a/t/chainlint.pl b/t/chainlint.pl
new file mode 100755
index 0000000..556ee91
--- /dev/null
+++ b/t/chainlint.pl
@@ -0,0 +1,832 @@
+#!/usr/bin/env perl
+#
+# Copyright (c) 2021-2022 Eric Sunshine <sunshine@sunshineco.com>
+#
+# This tool scans shell scripts for test definitions and checks those tests for
+# problems, such as broken &&-chains, which might hide bugs in the tests
+# themselves or in behaviors being exercised by the tests.
+#
+# Input arguments are pathnames of shell scripts containing test definitions,
+# or globs referencing a collection of scripts. For each problem discovered,
+# the pathname of the script containing the test is printed along with the test
+# name and the test body with a `?!FOO?!` annotation at the location of each
+# detected problem, where "FOO" is a tag such as "AMP" which indicates a broken
+# &&-chain. Returns zero if no problems are discovered, otherwise non-zero.
+
+use warnings;
+use strict;
+use Config;
+use File::Glob;
+use Getopt::Long;
+
+my $jobs = -1;
+my $show_stats;
+my $emit_all;
+
+# Lexer tokenizes POSIX shell scripts. It is roughly modeled after section 2.3
+# "Token Recognition" of POSIX chapter 2 "Shell Command Language". Although
+# similar to lexical analyzers for other languages, this one differs in a few
+# substantial ways due to quirks of the shell command language.
+#
+# For instance, in many languages, newline is just whitespace like space or
+# TAB, but in shell a newline is a command separator, thus a distinct lexical
+# token. A newline is significant and returned as a distinct token even at the
+# end of a shell comment.
+#
+# In other languages, `1+2` would typically be scanned as three tokens
+# (`1`, `+`, and `2`), but in shell it is a single token. However, the similar
+# `1 + 2`, which embeds whitepace, is scanned as three token in shell, as well.
+# In shell, several characters with special meaning lose that meaning when not
+# surrounded by whitespace. For instance, the negation operator `!` is special
+# when standing alone surrounded by whitespace; whereas in `foo!uucp` it is
+# just a plain character in the longer token "foo!uucp". In many other
+# languages, `"string"/foo:'string'` might be scanned as five tokens ("string",
+# `/`, `foo`, `:`, and 'string'), but in shell, it is just a single token.
+#
+# The lexical analyzer for the shell command language is also somewhat unusual
+# in that it recursively invokes the parser to handle the body of `$(...)`
+# expressions which can contain arbitrary shell code. Such expressions may be
+# encountered both inside and outside of double-quoted strings.
+#
+# The lexical analyzer is responsible for consuming shell here-doc bodies which
+# extend from the line following a `<<TAG` operator until a line consisting
+# solely of `TAG`. Here-doc consumption begins when a newline is encountered.
+# It is legal for multiple here-doc `<<TAG` operators to be present on a single
+# line, in which case their bodies must be present one following the next, and
+# are consumed in the (left-to-right) order the `<<TAG` operators appear on the
+# line. A special complication is that the bodies of all here-docs must be
+# consumed when the newline is encountered even if the parse context depth has
+# changed. For instance, in `cat <<A && x=$(cat <<B &&\n`, bodies of here-docs
+# "A" and "B" must be consumed even though "A" was introduced outside the
+# recursive parse context in which "B" was introduced and in which the newline
+# is encountered.
+package Lexer;
+
+sub new {
+ my ($class, $parser, $s) = @_;
+ bless {
+ parser => $parser,
+ buff => $s,
+ lineno => 1,
+ heretags => []
+ } => $class;
+}
+
+sub scan_heredoc_tag {
+ my $self = shift @_;
+ ${$self->{buff}} =~ /\G(-?)/gc;
+ my $indented = $1;
+ my $token = $self->scan_token();
+ return "<<$indented" unless $token;
+ my $tag = $token->[0];
+ $tag =~ s/['"\\]//g;
+ $$token[0] = $indented ? "\t$tag" : "$tag";
+ push(@{$self->{heretags}}, $token);
+ return "<<$indented$tag";
+}
+
+sub scan_op {
+ my ($self, $c) = @_;
+ my $b = $self->{buff};
+ return $c unless $$b =~ /\G(.)/sgc;
+ my $cc = $c . $1;
+ return scan_heredoc_tag($self) if $cc eq '<<';
+ return $cc if $cc =~ /^(?:&&|\|\||>>|;;|<&|>&|<>|>\|)$/;
+ pos($$b)--;
+ return $c;
+}
+
+sub scan_sqstring {
+ my $self = shift @_;
+ ${$self->{buff}} =~ /\G([^']*'|.*\z)/sgc;
+ my $s = $1;
+ $self->{lineno} += () = $s =~ /\n/sg;
+ return "'" . $s;
+}
+
+sub scan_dqstring {
+ my $self = shift @_;
+ my $b = $self->{buff};
+ my $s = '"';
+ while (1) {
+ # slurp up non-special characters
+ $s .= $1 if $$b =~ /\G([^"\$\\]+)/gc;
+ # handle special characters
+ last unless $$b =~ /\G(.)/sgc;
+ my $c = $1;
+ $s .= '"', last if $c eq '"';
+ $s .= '$' . $self->scan_dollar(), next if $c eq '$';
+ if ($c eq '\\') {
+ $s .= '\\', last unless $$b =~ /\G(.)/sgc;
+ $c = $1;
+ $self->{lineno}++, next if $c eq "\n"; # line splice
+ # backslash escapes only $, `, ", \ in dq-string
+ $s .= '\\' unless $c =~ /^[\$`"\\]$/;
+ $s .= $c;
+ next;
+ }
+ die("internal error scanning dq-string '$c'\n");
+ }
+ $self->{lineno} += () = $s =~ /\n/sg;
+ return $s;
+}
+
+sub scan_balanced {
+ my ($self, $c1, $c2) = @_;
+ my $b = $self->{buff};
+ my $depth = 1;
+ my $s = $c1;
+ while ($$b =~ /\G([^\Q$c1$c2\E]*(?:[\Q$c1$c2\E]|\z))/gc) {
+ $s .= $1;
+ $depth++, next if $s =~ /\Q$c1\E$/;
+ $depth--;
+ last if $depth == 0;
+ }
+ $self->{lineno} += () = $s =~ /\n/sg;
+ return $s;
+}
+
+sub scan_subst {
+ my $self = shift @_;
+ my @tokens = $self->{parser}->parse(qr/^\)$/);
+ $self->{parser}->next_token(); # closing ")"
+ return @tokens;
+}
+
+sub scan_dollar {
+ my $self = shift @_;
+ my $b = $self->{buff};
+ return $self->scan_balanced('(', ')') if $$b =~ /\G\((?=\()/gc; # $((...))
+ return '(' . join(' ', map {$_->[0]} $self->scan_subst()) . ')' if $$b =~ /\G\(/gc; # $(...)
+ return $self->scan_balanced('{', '}') if $$b =~ /\G\{/gc; # ${...}
+ return $1 if $$b =~ /\G(\w+)/gc; # $var
+ return $1 if $$b =~ /\G([@*#?$!0-9-])/gc; # $*, $1, $$, etc.
+ return '';
+}
+
+sub swallow_heredocs {
+ my $self = shift @_;
+ my $b = $self->{buff};
+ my $tags = $self->{heretags};
+ while (my $tag = shift @$tags) {
+ my $start = pos($$b);
+ my $indent = $$tag[0] =~ s/^\t// ? '\\s*' : '';
+ $$b =~ /(?:\G|\n)$indent\Q$$tag[0]\E(?:\n|\z)/gc;
+ if (pos($$b) > $start) {
+ my $body = substr($$b, $start, pos($$b) - $start);
+ $self->{lineno} += () = $body =~ /\n/sg;
+ next;
+ }
+ push(@{$self->{parser}->{problems}}, ['UNCLOSED-HEREDOC', $tag]);
+ $$b =~ /(?:\G|\n).*\z/gc; # consume rest of input
+ my $body = substr($$b, $start, pos($$b) - $start);
+ $self->{lineno} += () = $body =~ /\n/sg;
+ last;
+ }
+}
+
+sub scan_token {
+ my $self = shift @_;
+ my $b = $self->{buff};
+ my $token = '';
+ my ($start, $startln);
+RESTART:
+ $startln = $self->{lineno};
+ $$b =~ /\G[ \t]+/gc; # skip whitespace (but not newline)
+ $start = pos($$b) || 0;
+ $self->{lineno}++, return ["\n", $start, pos($$b), $startln, $startln] if $$b =~ /\G#[^\n]*(?:\n|\z)/gc; # comment
+ while (1) {
+ # slurp up non-special characters
+ $token .= $1 if $$b =~ /\G([^\\;&|<>(){}'"\$\s]+)/gc;
+ # handle special characters
+ last unless $$b =~ /\G(.)/sgc;
+ my $c = $1;
+ pos($$b)--, last if $c =~ /^[ \t]$/; # whitespace ends token
+ pos($$b)--, last if length($token) && $c =~ /^[;&|<>(){}\n]$/;
+ $token .= $self->scan_sqstring(), next if $c eq "'";
+ $token .= $self->scan_dqstring(), next if $c eq '"';
+ $token .= $c . $self->scan_dollar(), next if $c eq '$';
+ $self->{lineno}++, $self->swallow_heredocs(), $token = $c, last if $c eq "\n";
+ $token = $self->scan_op($c), last if $c =~ /^[;&|<>]$/;
+ $token = $c, last if $c =~ /^[(){}]$/;
+ if ($c eq '\\') {
+ $token .= '\\', last unless $$b =~ /\G(.)/sgc;
+ $c = $1;
+ $self->{lineno}++, next if $c eq "\n" && length($token); # line splice
+ $self->{lineno}++, goto RESTART if $c eq "\n"; # line splice
+ $token .= '\\' . $c;
+ next;
+ }
+ die("internal error scanning character '$c'\n");
+ }
+ return length($token) ? [$token, $start, pos($$b), $startln, $self->{lineno}] : undef;
+}
+
+# ShellParser parses POSIX shell scripts (with minor extensions for Bash). It
+# is a recursive descent parser very roughly modeled after section 2.10 "Shell
+# Grammar" of POSIX chapter 2 "Shell Command Language".
+package ShellParser;
+
+sub new {
+ my ($class, $s) = @_;
+ my $self = bless {
+ buff => [],
+ stop => [],
+ output => []
+ } => $class;
+ $self->{lexer} = Lexer->new($self, $s);
+ return $self;
+}
+
+sub next_token {
+ my $self = shift @_;
+ return pop(@{$self->{buff}}) if @{$self->{buff}};
+ return $self->{lexer}->scan_token();
+}
+
+sub untoken {
+ my $self = shift @_;
+ push(@{$self->{buff}}, @_);
+}
+
+sub peek {
+ my $self = shift @_;
+ my $token = $self->next_token();
+ return undef unless defined($token);
+ $self->untoken($token);
+ return $token;
+}
+
+sub stop_at {
+ my ($self, $token) = @_;
+ return 1 unless defined($token);
+ my $stop = ${$self->{stop}}[-1] if @{$self->{stop}};
+ return defined($stop) && $token->[0] =~ $stop;
+}
+
+sub expect {
+ my ($self, $expect) = @_;
+ my $token = $self->next_token();
+ return $token if defined($token) && $token->[0] eq $expect;
+ push(@{$self->{output}}, "?!ERR?! expected '$expect' but found '" . (defined($token) ? $token->[0] : "<end-of-input>") . "'\n");
+ $self->untoken($token) if defined($token);
+ return ();
+}
+
+sub optional_newlines {
+ my $self = shift @_;
+ my @tokens;
+ while (my $token = $self->peek()) {
+ last unless $token->[0] eq "\n";
+ push(@tokens, $self->next_token());
+ }
+ return @tokens;
+}
+
+sub parse_group {
+ my $self = shift @_;
+ return ($self->parse(qr/^}$/),
+ $self->expect('}'));
+}
+
+sub parse_subshell {
+ my $self = shift @_;
+ return ($self->parse(qr/^\)$/),
+ $self->expect(')'));
+}
+
+sub parse_case_pattern {
+ my $self = shift @_;
+ my @tokens;
+ while (defined(my $token = $self->next_token())) {
+ push(@tokens, $token);
+ last if $token->[0] eq ')';
+ }
+ return @tokens;
+}
+
+sub parse_case {
+ my $self = shift @_;
+ my @tokens;
+ push(@tokens,
+ $self->next_token(), # subject
+ $self->optional_newlines(),
+ $self->expect('in'),
+ $self->optional_newlines());
+ while (1) {
+ my $token = $self->peek();
+ last unless defined($token) && $token->[0] ne 'esac';
+ push(@tokens,
+ $self->parse_case_pattern(),
+ $self->optional_newlines(),
+ $self->parse(qr/^(?:;;|esac)$/)); # item body
+ $token = $self->peek();
+ last unless defined($token) && $token->[0] ne 'esac';
+ push(@tokens,
+ $self->expect(';;'),
+ $self->optional_newlines());
+ }
+ push(@tokens, $self->expect('esac'));
+ return @tokens;
+}
+
+sub parse_for {
+ my $self = shift @_;
+ my @tokens;
+ push(@tokens,
+ $self->next_token(), # variable
+ $self->optional_newlines());
+ my $token = $self->peek();
+ if (defined($token) && $token->[0] eq 'in') {
+ push(@tokens,
+ $self->expect('in'),
+ $self->optional_newlines());
+ }
+ push(@tokens,
+ $self->parse(qr/^do$/), # items
+ $self->expect('do'),
+ $self->optional_newlines(),
+ $self->parse_loop_body(),
+ $self->expect('done'));
+ return @tokens;
+}
+
+sub parse_if {
+ my $self = shift @_;
+ my @tokens;
+ while (1) {
+ push(@tokens,
+ $self->parse(qr/^then$/), # if/elif condition
+ $self->expect('then'),
+ $self->optional_newlines(),
+ $self->parse(qr/^(?:elif|else|fi)$/)); # if/elif body
+ my $token = $self->peek();
+ last unless defined($token) && $token->[0] eq 'elif';
+ push(@tokens, $self->expect('elif'));
+ }
+ my $token = $self->peek();
+ if (defined($token) && $token->[0] eq 'else') {
+ push(@tokens,
+ $self->expect('else'),
+ $self->optional_newlines(),
+ $self->parse(qr/^fi$/)); # else body
+ }
+ push(@tokens, $self->expect('fi'));
+ return @tokens;
+}
+
+sub parse_loop_body {
+ my $self = shift @_;
+ return $self->parse(qr/^done$/);
+}
+
+sub parse_loop {
+ my $self = shift @_;
+ return ($self->parse(qr/^do$/), # condition
+ $self->expect('do'),
+ $self->optional_newlines(),
+ $self->parse_loop_body(),
+ $self->expect('done'));
+}
+
+sub parse_func {
+ my $self = shift @_;
+ return ($self->expect('('),
+ $self->expect(')'),
+ $self->optional_newlines(),
+ $self->parse_cmd()); # body
+}
+
+sub parse_bash_array_assignment {
+ my $self = shift @_;
+ my @tokens = $self->expect('(');
+ while (defined(my $token = $self->next_token())) {
+ push(@tokens, $token);
+ last if $token->[0] eq ')';
+ }
+ return @tokens;
+}
+
+my %compound = (
+ '{' => \&parse_group,
+ '(' => \&parse_subshell,
+ 'case' => \&parse_case,
+ 'for' => \&parse_for,
+ 'if' => \&parse_if,
+ 'until' => \&parse_loop,
+ 'while' => \&parse_loop);
+
+sub parse_cmd {
+ my $self = shift @_;
+ my $cmd = $self->next_token();
+ return () unless defined($cmd);
+ return $cmd if $cmd->[0] eq "\n";
+
+ my $token;
+ my @tokens = $cmd;
+ if ($cmd->[0] eq '!') {
+ push(@tokens, $self->parse_cmd());
+ return @tokens;
+ } elsif (my $f = $compound{$cmd->[0]}) {
+ push(@tokens, $self->$f());
+ } elsif (defined($token = $self->peek()) && $token->[0] eq '(') {
+ if ($cmd->[0] !~ /\w=$/) {
+ push(@tokens, $self->parse_func());
+ return @tokens;
+ }
+ my @array = $self->parse_bash_array_assignment();
+ $tokens[-1]->[0] .= join(' ', map {$_->[0]} @array);
+ $tokens[-1]->[2] = $array[$#array][2] if @array;
+ }
+
+ while (defined(my $token = $self->next_token())) {
+ $self->untoken($token), last if $self->stop_at($token);
+ push(@tokens, $token);
+ last if $token->[0] =~ /^(?:[;&\n|]|&&|\|\|)$/;
+ }
+ push(@tokens, $self->next_token()) if $tokens[-1]->[0] ne "\n" && defined($token = $self->peek()) && $token->[0] eq "\n";
+ return @tokens;
+}
+
+sub accumulate {
+ my ($self, $tokens, $cmd) = @_;
+ push(@$tokens, @$cmd);
+}
+
+sub parse {
+ my ($self, $stop) = @_;
+ push(@{$self->{stop}}, $stop);
+ goto DONE if $self->stop_at($self->peek());
+ my @tokens;
+ while (my @cmd = $self->parse_cmd()) {
+ $self->accumulate(\@tokens, \@cmd);
+ last if $self->stop_at($self->peek());
+ }
+DONE:
+ pop(@{$self->{stop}});
+ return @tokens;
+}
+
+# TestParser is a subclass of ShellParser which, beyond parsing shell script
+# code, is also imbued with semantic knowledge of test construction, and checks
+# tests for common problems (such as broken &&-chains) which might hide bugs in
+# the tests themselves or in behaviors being exercised by the tests. As such,
+# TestParser is only called upon to parse test bodies, not the top-level
+# scripts in which the tests are defined.
+package TestParser;
+
+use base 'ShellParser';
+
+sub new {
+ my $class = shift @_;
+ my $self = $class->SUPER::new(@_);
+ $self->{problems} = [];
+ return $self;
+}
+
+sub find_non_nl {
+ my $tokens = shift @_;
+ my $n = shift @_;
+ $n = $#$tokens if !defined($n);
+ $n-- while $n >= 0 && $$tokens[$n]->[0] eq "\n";
+ return $n;
+}
+
+sub ends_with {
+ my ($tokens, $needles) = @_;
+ my $n = find_non_nl($tokens);
+ for my $needle (reverse(@$needles)) {
+ return undef if $n < 0;
+ $n = find_non_nl($tokens, $n), next if $needle eq "\n";
+ return undef if $$tokens[$n]->[0] !~ $needle;
+ $n--;
+ }
+ return 1;
+}
+
+sub match_ending {
+ my ($tokens, $endings) = @_;
+ for my $needles (@$endings) {
+ next if @$tokens < scalar(grep {$_ ne "\n"} @$needles);
+ return 1 if ends_with($tokens, $needles);
+ }
+ return undef;
+}
+
+sub parse_loop_body {
+ my $self = shift @_;
+ my @tokens = $self->SUPER::parse_loop_body(@_);
+ # did loop signal failure via "|| return" or "|| exit"?
+ return @tokens if !@tokens || grep {$_->[0] =~ /^(?:return|exit|\$\?)$/} @tokens;
+ # did loop upstream of a pipe signal failure via "|| echo 'impossible
+ # text'" as the final command in the loop body?
+ return @tokens if ends_with(\@tokens, [qr/^\|\|$/, "\n", qr/^echo$/, qr/^.+$/]);
+ # flag missing "return/exit" handling explicit failure in loop body
+ my $n = find_non_nl(\@tokens);
+ push(@{$self->{problems}}, ['LOOP', $tokens[$n]]);
+ return @tokens;
+}
+
+my @safe_endings = (
+ [qr/^(?:&&|\|\||\||&)$/],
+ [qr/^(?:exit|return)$/, qr/^(?:\d+|\$\?)$/],
+ [qr/^(?:exit|return)$/, qr/^(?:\d+|\$\?)$/, qr/^;$/],
+ [qr/^(?:exit|return|continue)$/],
+ [qr/^(?:exit|return|continue)$/, qr/^;$/]);
+
+sub accumulate {
+ my ($self, $tokens, $cmd) = @_;
+ my $problems = $self->{problems};
+
+ # no previous command to check for missing "&&"
+ goto DONE unless @$tokens;
+
+ # new command is empty line; can't yet check if previous is missing "&&"
+ goto DONE if @$cmd == 1 && $$cmd[0]->[0] eq "\n";
+
+ # did previous command end with "&&", "|", "|| return" or similar?
+ goto DONE if match_ending($tokens, \@safe_endings);
+
+ # if this command handles "$?" specially, then okay for previous
+ # command to be missing "&&"
+ for my $token (@$cmd) {
+ goto DONE if $token->[0] =~ /\$\?/;
+ }
+
+ # if this command is "false", "return 1", or "exit 1" (which signal
+ # failure explicitly), then okay for all preceding commands to be
+ # missing "&&"
+ if ($$cmd[0]->[0] =~ /^(?:false|return|exit)$/) {
+ @$problems = grep {$_->[0] ne 'AMP'} @$problems;
+ goto DONE;
+ }
+
+ # flag missing "&&" at end of previous command
+ my $n = find_non_nl($tokens);
+ push(@$problems, ['AMP', $tokens->[$n]]) unless $n < 0;
+
+DONE:
+ $self->SUPER::accumulate($tokens, $cmd);
+}
+
+# ScriptParser is a subclass of ShellParser which identifies individual test
+# definitions within test scripts, and passes each test body through TestParser
+# to identify possible problems. ShellParser detects test definitions not only
+# at the top-level of test scripts but also within compound commands such as
+# loops and function definitions.
+package ScriptParser;
+
+use base 'ShellParser';
+
+sub new {
+ my $class = shift @_;
+ my $self = $class->SUPER::new(@_);
+ $self->{ntests} = 0;
+ return $self;
+}
+
+# extract the raw content of a token, which may be a single string or a
+# composition of multiple strings and non-string character runs; for instance,
+# `"test body"` unwraps to `test body`; `word"a b"42'c d'` to `worda b42c d`
+sub unwrap {
+ my $token = (@_ ? shift @_ : $_)->[0];
+ # simple case: 'sqstring' or "dqstring"
+ return $token if $token =~ s/^'([^']*)'$/$1/;
+ return $token if $token =~ s/^"([^"]*)"$/$1/;
+
+ # composite case
+ my ($s, $q, $escaped);
+ while (1) {
+ # slurp up non-special characters
+ $s .= $1 if $token =~ /\G([^\\'"]*)/gc;
+ # handle special characters
+ last unless $token =~ /\G(.)/sgc;
+ my $c = $1;
+ $q = undef, next if defined($q) && $c eq $q;
+ $q = $c, next if !defined($q) && $c =~ /^['"]$/;
+ if ($c eq '\\') {
+ last unless $token =~ /\G(.)/sgc;
+ $c = $1;
+ $s .= '\\' if $c eq "\n"; # preserve line splice
+ }
+ $s .= $c;
+ }
+ return $s
+}
+
+sub check_test {
+ my $self = shift @_;
+ my ($title, $body) = map(unwrap, @_);
+ $self->{ntests}++;
+ my $parser = TestParser->new(\$body);
+ my @tokens = $parser->parse();
+ my $problems = $parser->{problems};
+ return unless $emit_all || @$problems;
+ my $c = main::fd_colors(1);
+ my $lineno = $_[1]->[3];
+ my $start = 0;
+ my $checked = '';
+ for (sort {$a->[1]->[2] <=> $b->[1]->[2]} @$problems) {
+ my ($label, $token) = @$_;
+ my $pos = $token->[2];
+ $checked .= substr($body, $start, $pos - $start) . " ?!$label?! ";
+ $start = $pos;
+ }
+ $checked .= substr($body, $start);
+ $checked =~ s/^/$lineno++ . ' '/mge;
+ $checked =~ s/^\d+ \n//;
+ $checked =~ s/(\s) \?!/$1?!/mg;
+ $checked =~ s/\?! (\s)/?!$1/mg;
+ $checked =~ s/(\?![^?]+\?!)/$c->{rev}$c->{red}$1$c->{reset}/mg;
+ $checked =~ s/^\d+/$c->{dim}$&$c->{reset}/mg;
+ $checked .= "\n" unless $checked =~ /\n$/;
+ push(@{$self->{output}}, "$c->{blue}# chainlint: $title$c->{reset}\n$checked");
+}
+
+sub parse_cmd {
+ my $self = shift @_;
+ my @tokens = $self->SUPER::parse_cmd();
+ return @tokens unless @tokens && $tokens[0]->[0] =~ /^test_expect_(?:success|failure)$/;
+ my $n = $#tokens;
+ $n-- while $n >= 0 && $tokens[$n]->[0] =~ /^(?:[;&\n|]|&&|\|\|)$/;
+ $self->check_test($tokens[1], $tokens[2]) if $n == 2; # title body
+ $self->check_test($tokens[2], $tokens[3]) if $n > 2; # prereq title body
+ return @tokens;
+}
+
+# main contains high-level functionality for processing command-line switches,
+# feeding input test scripts to ScriptParser, and reporting results.
+package main;
+
+my $getnow = sub { return time(); };
+my $interval = sub { return time() - shift; };
+if (eval {require Time::HiRes; Time::HiRes->import(); 1;}) {
+ $getnow = sub { return [Time::HiRes::gettimeofday()]; };
+ $interval = sub { return Time::HiRes::tv_interval(shift); };
+}
+
+# Restore TERM if test framework set it to "dumb" so 'tput' will work; do this
+# outside of get_colors() since under 'ithreads' all threads use %ENV of main
+# thread and ignore %ENV changes in subthreads.
+$ENV{TERM} = $ENV{USER_TERM} if $ENV{USER_TERM};
+
+my @NOCOLORS = (bold => '', rev => '', dim => '', reset => '', blue => '', green => '', red => '');
+my %COLORS = ();
+sub get_colors {
+ return \%COLORS if %COLORS;
+ if (exists($ENV{NO_COLOR})) {
+ %COLORS = @NOCOLORS;
+ return \%COLORS;
+ }
+ if ($ENV{TERM} =~ /xterm|xterm-\d+color|xterm-new|xterm-direct|nsterm|nsterm-\d+color|nsterm-direct/) {
+ %COLORS = (bold => "\e[1m",
+ rev => "\e[7m",
+ dim => "\e[2m",
+ reset => "\e[0m",
+ blue => "\e[34m",
+ green => "\e[32m",
+ red => "\e[31m");
+ return \%COLORS;
+ }
+ if (system("tput sgr0 >/dev/null 2>&1") == 0 &&
+ system("tput bold >/dev/null 2>&1") == 0 &&
+ system("tput rev >/dev/null 2>&1") == 0 &&
+ system("tput dim >/dev/null 2>&1") == 0 &&
+ system("tput setaf 1 >/dev/null 2>&1") == 0) {
+ %COLORS = (bold => `tput bold`,
+ rev => `tput rev`,
+ dim => `tput dim`,
+ reset => `tput sgr0`,
+ blue => `tput setaf 4`,
+ green => `tput setaf 2`,
+ red => `tput setaf 1`);
+ return \%COLORS;
+ }
+ %COLORS = @NOCOLORS;
+ return \%COLORS;
+}
+
+my %FD_COLORS = ();
+sub fd_colors {
+ my $fd = shift;
+ return $FD_COLORS{$fd} if exists($FD_COLORS{$fd});
+ $FD_COLORS{$fd} = -t $fd ? get_colors() : {@NOCOLORS};
+ return $FD_COLORS{$fd};
+}
+
+sub ncores {
+ # Windows
+ return $ENV{NUMBER_OF_PROCESSORS} if exists($ENV{NUMBER_OF_PROCESSORS});
+ # Linux / MSYS2 / Cygwin / WSL
+ do { local @ARGV='/proc/cpuinfo'; return scalar(grep(/^processor[\s\d]*:/, <>)); } if -r '/proc/cpuinfo';
+ # macOS & BSD
+ return qx/sysctl -n hw.ncpu/ if $^O =~ /(?:^darwin$|bsd)/;
+ return 1;
+}
+
+sub show_stats {
+ my ($start_time, $stats) = @_;
+ my $walltime = $interval->($start_time);
+ my ($usertime) = times();
+ my ($total_workers, $total_scripts, $total_tests, $total_errs) = (0, 0, 0, 0);
+ my $c = fd_colors(2);
+ print(STDERR $c->{green});
+ for (@$stats) {
+ my ($worker, $nscripts, $ntests, $nerrs) = @$_;
+ print(STDERR "worker $worker: $nscripts scripts, $ntests tests, $nerrs errors\n");
+ $total_workers++;
+ $total_scripts += $nscripts;
+ $total_tests += $ntests;
+ $total_errs += $nerrs;
+ }
+ printf(STDERR "total: %d workers, %d scripts, %d tests, %d errors, %.2fs/%.2fs (wall/user)$c->{reset}\n", $total_workers, $total_scripts, $total_tests, $total_errs, $walltime, $usertime);
+}
+
+sub check_script {
+ my ($id, $next_script, $emit) = @_;
+ my ($nscripts, $ntests, $nerrs) = (0, 0, 0);
+ while (my $path = $next_script->()) {
+ $nscripts++;
+ my $fh;
+ unless (open($fh, "<", $path)) {
+ $emit->("?!ERR?! $path: $!\n");
+ next;
+ }
+ my $s = do { local $/; <$fh> };
+ close($fh);
+ my $parser = ScriptParser->new(\$s);
+ 1 while $parser->parse_cmd();
+ if (@{$parser->{output}}) {
+ my $c = fd_colors(1);
+ my $s = join('', @{$parser->{output}});
+ $emit->("$c->{bold}$c->{blue}# chainlint: $path$c->{reset}\n" . $s);
+ $nerrs += () = $s =~ /\?![^?]+\?!/g;
+ }
+ $ntests += $parser->{ntests};
+ }
+ return [$id, $nscripts, $ntests, $nerrs];
+}
+
+sub exit_code {
+ my $stats = shift @_;
+ for (@$stats) {
+ my ($worker, $nscripts, $ntests, $nerrs) = @$_;
+ return 1 if $nerrs;
+ }
+ return 0;
+}
+
+Getopt::Long::Configure(qw{bundling});
+GetOptions(
+ "emit-all!" => \$emit_all,
+ "jobs|j=i" => \$jobs,
+ "stats|show-stats!" => \$show_stats) or die("option error\n");
+$jobs = ncores() if $jobs < 1;
+
+my $start_time = $getnow->();
+my @stats;
+
+my @scripts;
+push(@scripts, File::Glob::bsd_glob($_)) for (@ARGV);
+unless (@scripts) {
+ show_stats($start_time, \@stats) if $show_stats;
+ exit;
+}
+
+unless ($Config{useithreads} && eval {
+ require threads; threads->import();
+ require Thread::Queue; Thread::Queue->import();
+ 1;
+ }) {
+ push(@stats, check_script(1, sub { shift(@scripts); }, sub { print(@_); }));
+ show_stats($start_time, \@stats) if $show_stats;
+ exit(exit_code(\@stats));
+}
+
+my $script_queue = Thread::Queue->new();
+my $output_queue = Thread::Queue->new();
+
+sub next_script { return $script_queue->dequeue(); }
+sub emit { $output_queue->enqueue(@_); }
+
+sub monitor {
+ while (my $s = $output_queue->dequeue()) {
+ print($s);
+ }
+}
+
+my $mon = threads->create({'context' => 'void'}, \&monitor);
+threads->create({'context' => 'list'}, \&check_script, $_, \&next_script, \&emit) for 1..$jobs;
+
+$script_queue->enqueue(@scripts);
+$script_queue->end();
+
+for (threads->list()) {
+ push(@stats, $_->join()) unless $_ == $mon;
+}
+
+$output_queue->end();
+$mon->join();
+
+show_stats($start_time, \@stats) if $show_stats;
+exit(exit_code(\@stats));
diff --git a/t/chainlint/arithmetic-expansion.expect b/t/chainlint/arithmetic-expansion.expect
new file mode 100644
index 0000000..46ee104
--- /dev/null
+++ b/t/chainlint/arithmetic-expansion.expect
@@ -0,0 +1,9 @@
+(
+ foo &&
+ bar=$((42 + 1)) &&
+ baz
+) &&
+(
+ bar=$((42 + 1)) ?!AMP?!
+ baz
+)
diff --git a/t/chainlint/arithmetic-expansion.test b/t/chainlint/arithmetic-expansion.test
new file mode 100644
index 0000000..1620696
--- /dev/null
+++ b/t/chainlint/arithmetic-expansion.test
@@ -0,0 +1,11 @@
+(
+ foo &&
+# LINT: closing ")" of $((...)) not misinterpreted as subshell-closing ")"
+ bar=$((42 + 1)) &&
+ baz
+) &&
+(
+# LINT: missing "&&" on $((...))
+ bar=$((42 + 1))
+ baz
+)
diff --git a/t/chainlint/bash-array.expect b/t/chainlint/bash-array.expect
new file mode 100644
index 0000000..4c34eae
--- /dev/null
+++ b/t/chainlint/bash-array.expect
@@ -0,0 +1,10 @@
+(
+ foo &&
+ bar=(gumbo stumbo wumbo) &&
+ baz
+) &&
+(
+ foo &&
+ bar=${#bar[@]} &&
+ baz
+)
diff --git a/t/chainlint/bash-array.test b/t/chainlint/bash-array.test
new file mode 100644
index 0000000..92bbb77
--- /dev/null
+++ b/t/chainlint/bash-array.test
@@ -0,0 +1,12 @@
+(
+ foo &&
+# LINT: ")" in Bash array assignment not misinterpreted as subshell-closing ")"
+ bar=(gumbo stumbo wumbo) &&
+ baz
+) &&
+(
+ foo &&
+# LINT: Bash array length operator not misinterpreted as comment
+ bar=${#bar[@]} &&
+ baz
+)
diff --git a/t/chainlint/blank-line-before-esac.expect b/t/chainlint/blank-line-before-esac.expect
new file mode 100644
index 0000000..48ed4eb
--- /dev/null
+++ b/t/chainlint/blank-line-before-esac.expect
@@ -0,0 +1,18 @@
+test_done ( ) {
+ case "$test_failure" in
+ 0 )
+ test_at_end_hook_
+
+ exit 0 ;;
+
+ * )
+ if test $test_external_has_tap -eq 0
+ then
+ say_color error "# failed $test_failure among $msg"
+ say "1..$test_count"
+ fi
+
+ exit 1 ;;
+
+ esac
+}
diff --git a/t/chainlint/blank-line-before-esac.test b/t/chainlint/blank-line-before-esac.test
new file mode 100644
index 0000000..cecccad
--- /dev/null
+++ b/t/chainlint/blank-line-before-esac.test
@@ -0,0 +1,19 @@
+# LINT: blank line before "esac"
+test_done () {
+ case "$test_failure" in
+ 0)
+ test_at_end_hook_
+
+ exit 0 ;;
+
+ *)
+ if test $test_external_has_tap -eq 0
+ then
+ say_color error "# failed $test_failure among $msg"
+ say "1..$test_count"
+ fi
+
+ exit 1 ;;
+
+ esac
+}
diff --git a/t/chainlint/blank-line.expect b/t/chainlint/blank-line.expect
new file mode 100644
index 0000000..f76fde1
--- /dev/null
+++ b/t/chainlint/blank-line.expect
@@ -0,0 +1,4 @@
+(
+ nothing &&
+ something
+)
diff --git a/t/chainlint/blank-line.test b/t/chainlint/blank-line.test
new file mode 100644
index 0000000..0fdf15b
--- /dev/null
+++ b/t/chainlint/blank-line.test
@@ -0,0 +1,10 @@
+(
+
+ nothing &&
+
+ something
+# LINT: ignore blank lines since final _statement_ before subshell end is
+# LINT: significant to "&&"-check, not final _line_ (which might be blank)
+
+
+)
diff --git a/t/chainlint/block-comment.expect b/t/chainlint/block-comment.expect
new file mode 100644
index 0000000..df2beea
--- /dev/null
+++ b/t/chainlint/block-comment.expect
@@ -0,0 +1,8 @@
+(
+ {
+ # show a
+ echo a &&
+ # show b
+ echo b
+ }
+)
diff --git a/t/chainlint/block-comment.test b/t/chainlint/block-comment.test
new file mode 100644
index 0000000..df2beea
--- /dev/null
+++ b/t/chainlint/block-comment.test
@@ -0,0 +1,8 @@
+(
+ {
+ # show a
+ echo a &&
+ # show b
+ echo b
+ }
+)
diff --git a/t/chainlint/block.expect b/t/chainlint/block.expect
new file mode 100644
index 0000000..a3bcea4
--- /dev/null
+++ b/t/chainlint/block.expect
@@ -0,0 +1,23 @@
+(
+ foo &&
+ {
+ echo a ?!AMP?!
+ echo b
+ } &&
+ bar &&
+ {
+ echo c
+ } ?!AMP?!
+ baz
+) &&
+
+{
+ echo a ; ?!AMP?! echo b
+} &&
+{ echo a ; ?!AMP?! echo b ; } &&
+
+{
+ echo "${var}9" &&
+ echo "done"
+} &&
+finis
diff --git a/t/chainlint/block.test b/t/chainlint/block.test
new file mode 100644
index 0000000..4ab69a4
--- /dev/null
+++ b/t/chainlint/block.test
@@ -0,0 +1,27 @@
+(
+# LINT: missing "&&" after first "echo"
+ foo &&
+ {
+ echo a
+ echo b
+ } &&
+ bar &&
+# LINT: missing "&&" at closing "}"
+ {
+ echo c
+ }
+ baz
+) &&
+
+# LINT: ";" not allowed in place of "&&"
+{
+ echo a; echo b
+} &&
+{ echo a; echo b; } &&
+
+# LINT: "}" inside string not mistaken as end of block
+{
+ echo "${var}9" &&
+ echo "done"
+} &&
+finis
diff --git a/t/chainlint/broken-chain.expect b/t/chainlint/broken-chain.expect
new file mode 100644
index 0000000..cfb58fb
--- /dev/null
+++ b/t/chainlint/broken-chain.expect
@@ -0,0 +1,6 @@
+(
+ foo &&
+ bar ?!AMP?!
+ baz &&
+ wop
+)
diff --git a/t/chainlint/broken-chain.test b/t/chainlint/broken-chain.test
new file mode 100644
index 0000000..2a44aa7
--- /dev/null
+++ b/t/chainlint/broken-chain.test
@@ -0,0 +1,8 @@
+(
+ foo &&
+# LINT: missing "&&" from "bar"
+ bar
+ baz &&
+# LINT: final statement before closing ")" legitimately lacks "&&"
+ wop
+)
diff --git a/t/chainlint/case-comment.expect b/t/chainlint/case-comment.expect
new file mode 100644
index 0000000..641c157
--- /dev/null
+++ b/t/chainlint/case-comment.expect
@@ -0,0 +1,11 @@
+(
+ case "$x" in
+ # found foo
+ x) foo ;;
+ # found other
+ *)
+ # treat it as bar
+ bar
+ ;;
+ esac
+)
diff --git a/t/chainlint/case-comment.test b/t/chainlint/case-comment.test
new file mode 100644
index 0000000..641c157
--- /dev/null
+++ b/t/chainlint/case-comment.test
@@ -0,0 +1,11 @@
+(
+ case "$x" in
+ # found foo
+ x) foo ;;
+ # found other
+ *)
+ # treat it as bar
+ bar
+ ;;
+ esac
+)
diff --git a/t/chainlint/case.expect b/t/chainlint/case.expect
new file mode 100644
index 0000000..31f280d
--- /dev/null
+++ b/t/chainlint/case.expect
@@ -0,0 +1,19 @@
+(
+ case "$x" in
+ x) foo ;;
+ *) bar ;;
+ esac &&
+ foobar
+) &&
+(
+ case "$x" in
+ x) foo ;;
+ *) bar ;;
+ esac ?!AMP?!
+ foobar
+) &&
+(
+ case "$x" in 1) true;; esac &&
+ case "$y" in 2) false;; esac ?!AMP?!
+ foobar
+)
diff --git a/t/chainlint/case.test b/t/chainlint/case.test
new file mode 100644
index 0000000..4cb086b
--- /dev/null
+++ b/t/chainlint/case.test
@@ -0,0 +1,23 @@
+(
+# LINT: "...)" arms in "case" not misinterpreted as subshell-closing ")"
+ case "$x" in
+ x) foo ;;
+ *) bar ;;
+ esac &&
+ foobar
+) &&
+(
+# LINT: missing "&&" on "esac"
+ case "$x" in
+ x) foo ;;
+ *) bar ;;
+ esac
+ foobar
+) &&
+(
+# LINT: "...)" arm in one-liner "case" not misinterpreted as closing ")"
+ case "$x" in 1) true;; esac &&
+# LINT: same but missing "&&"
+ case "$y" in 2) false;; esac
+ foobar
+)
diff --git a/t/chainlint/chain-break-background.expect b/t/chainlint/chain-break-background.expect
new file mode 100644
index 0000000..28f9114
--- /dev/null
+++ b/t/chainlint/chain-break-background.expect
@@ -0,0 +1,9 @@
+JGIT_DAEMON_PID= &&
+git init --bare empty.git &&
+> empty.git/git-daemon-export-ok &&
+mkfifo jgit_daemon_output &&
+{
+ jgit daemon --port="$JGIT_DAEMON_PORT" . > jgit_daemon_output &
+ JGIT_DAEMON_PID=$!
+} &&
+test_expect_code 2 git ls-remote --exit-code git://localhost:$JGIT_DAEMON_PORT/empty.git
diff --git a/t/chainlint/chain-break-background.test b/t/chainlint/chain-break-background.test
new file mode 100644
index 0000000..e10f656
--- /dev/null
+++ b/t/chainlint/chain-break-background.test
@@ -0,0 +1,10 @@
+JGIT_DAEMON_PID= &&
+git init --bare empty.git &&
+>empty.git/git-daemon-export-ok &&
+mkfifo jgit_daemon_output &&
+{
+# LINT: exit status of "&" is always 0 so &&-chaining immaterial
+ jgit daemon --port="$JGIT_DAEMON_PORT" . >jgit_daemon_output &
+ JGIT_DAEMON_PID=$!
+} &&
+test_expect_code 2 git ls-remote --exit-code git://localhost:$JGIT_DAEMON_PORT/empty.git
diff --git a/t/chainlint/chain-break-continue.expect b/t/chainlint/chain-break-continue.expect
new file mode 100644
index 0000000..47a3457
--- /dev/null
+++ b/t/chainlint/chain-break-continue.expect
@@ -0,0 +1,12 @@
+git ls-tree --name-only -r refs/notes/many_notes |
+while read path
+do
+ test "$path" = "foobar/non-note.txt" && continue
+ test "$path" = "deadbeef" && continue
+ test "$path" = "de/adbeef" && continue
+
+ if test $(expr length "$path") -ne $hexsz
+ then
+ return 1
+ fi
+done
diff --git a/t/chainlint/chain-break-continue.test b/t/chainlint/chain-break-continue.test
new file mode 100644
index 0000000..f0af71d
--- /dev/null
+++ b/t/chainlint/chain-break-continue.test
@@ -0,0 +1,13 @@
+git ls-tree --name-only -r refs/notes/many_notes |
+while read path
+do
+# LINT: broken &&-chain okay if explicit "continue"
+ test "$path" = "foobar/non-note.txt" && continue
+ test "$path" = "deadbeef" && continue
+ test "$path" = "de/adbeef" && continue
+
+ if test $(expr length "$path") -ne $hexsz
+ then
+ return 1
+ fi
+done
diff --git a/t/chainlint/chain-break-false.expect b/t/chainlint/chain-break-false.expect
new file mode 100644
index 0000000..989766f
--- /dev/null
+++ b/t/chainlint/chain-break-false.expect
@@ -0,0 +1,9 @@
+if condition not satisified
+then
+ echo it did not work...
+ echo failed!
+ false
+else
+ echo it went okay ?!AMP?!
+ congratulate user
+fi
diff --git a/t/chainlint/chain-break-false.test b/t/chainlint/chain-break-false.test
new file mode 100644
index 0000000..a5aaff8
--- /dev/null
+++ b/t/chainlint/chain-break-false.test
@@ -0,0 +1,10 @@
+# LINT: broken &&-chain okay if explicit "false" signals failure
+if condition not satisified
+then
+ echo it did not work...
+ echo failed!
+ false
+else
+ echo it went okay
+ congratulate user
+fi
diff --git a/t/chainlint/chain-break-return-exit.expect b/t/chainlint/chain-break-return-exit.expect
new file mode 100644
index 0000000..1732d22
--- /dev/null
+++ b/t/chainlint/chain-break-return-exit.expect
@@ -0,0 +1,19 @@
+case "$(git ls-files)" in
+one ) echo pass one ;;
+* ) echo bad one ; return 1 ;;
+esac &&
+(
+ case "$(git ls-files)" in
+ two ) echo pass two ;;
+ * ) echo bad two ; exit 1 ;;
+esac
+) &&
+case "$(git ls-files)" in
+dir/two"$LF"one ) echo pass both ;;
+* ) echo bad ; return 1 ;;
+esac &&
+
+for i in 1 2 3 4 ; do
+ git checkout main -b $i || return $?
+ test_commit $i $i $i tag$i || return $?
+done
diff --git a/t/chainlint/chain-break-return-exit.test b/t/chainlint/chain-break-return-exit.test
new file mode 100644
index 0000000..46542ed
--- /dev/null
+++ b/t/chainlint/chain-break-return-exit.test
@@ -0,0 +1,23 @@
+case "$(git ls-files)" in
+one) echo pass one ;;
+# LINT: broken &&-chain okay if explicit "return 1" signals failuire
+*) echo bad one; return 1 ;;
+esac &&
+(
+ case "$(git ls-files)" in
+ two) echo pass two ;;
+# LINT: broken &&-chain okay if explicit "exit 1" signals failuire
+ *) echo bad two; exit 1 ;;
+ esac
+) &&
+case "$(git ls-files)" in
+dir/two"$LF"one) echo pass both ;;
+# LINT: broken &&-chain okay if explicit "return 1" signals failuire
+*) echo bad; return 1 ;;
+esac &&
+
+for i in 1 2 3 4 ; do
+# LINT: broken &&-chain okay if explicit "return $?" signals failure
+ git checkout main -b $i || return $?
+ test_commit $i $i $i tag$i || return $?
+done
diff --git a/t/chainlint/chain-break-status.expect b/t/chainlint/chain-break-status.expect
new file mode 100644
index 0000000..f4bada9
--- /dev/null
+++ b/t/chainlint/chain-break-status.expect
@@ -0,0 +1,9 @@
+OUT=$(( ( large_git ; echo $? 1 >& 3 ) | : ) 3 >& 1) &&
+test_match_signal 13 "$OUT" &&
+
+{ test-tool sigchain > actual ; ret=$? ; } &&
+{
+ test_match_signal 15 "$ret" ||
+ test "$ret" = 3
+} &&
+test_cmp expect actual
diff --git a/t/chainlint/chain-break-status.test b/t/chainlint/chain-break-status.test
new file mode 100644
index 0000000..a6602a7
--- /dev/null
+++ b/t/chainlint/chain-break-status.test
@@ -0,0 +1,11 @@
+# LINT: broken &&-chain okay if next command handles "$?" explicitly
+OUT=$( ((large_git; echo $? 1>&3) | :) 3>&1 ) &&
+test_match_signal 13 "$OUT" &&
+
+# LINT: broken &&-chain okay if next command handles "$?" explicitly
+{ test-tool sigchain >actual; ret=$?; } &&
+{
+ test_match_signal 15 "$ret" ||
+ test "$ret" = 3
+} &&
+test_cmp expect actual
diff --git a/t/chainlint/chained-block.expect b/t/chainlint/chained-block.expect
new file mode 100644
index 0000000..574cdce
--- /dev/null
+++ b/t/chainlint/chained-block.expect
@@ -0,0 +1,9 @@
+echo nobody home && {
+ test the doohicky ?!AMP?!
+ right now
+} &&
+
+GIT_EXTERNAL_DIFF=echo git diff | {
+ read path oldfile oldhex oldmode newfile newhex newmode &&
+ test "z$oh" = "z$oldhex"
+}
diff --git a/t/chainlint/chained-block.test b/t/chainlint/chained-block.test
new file mode 100644
index 0000000..86f81ec
--- /dev/null
+++ b/t/chainlint/chained-block.test
@@ -0,0 +1,11 @@
+# LINT: start of block chained to preceding command
+echo nobody home && {
+ test the doohicky
+ right now
+} &&
+
+# LINT: preceding command pipes to block on same line
+GIT_EXTERNAL_DIFF=echo git diff | {
+ read path oldfile oldhex oldmode newfile newhex newmode &&
+ test "z$oh" = "z$oldhex"
+}
diff --git a/t/chainlint/chained-subshell.expect b/t/chainlint/chained-subshell.expect
new file mode 100644
index 0000000..af0369d
--- /dev/null
+++ b/t/chainlint/chained-subshell.expect
@@ -0,0 +1,10 @@
+mkdir sub && (
+ cd sub &&
+ foo the bar ?!AMP?!
+ nuff said
+) &&
+
+cut "-d " -f actual | ( read s1 s2 s3 &&
+test -f $s1 ?!AMP?!
+test $(cat $s2) = tree2path1 &&
+test $(cat $s3) = tree3path1 )
diff --git a/t/chainlint/chained-subshell.test b/t/chainlint/chained-subshell.test
new file mode 100644
index 0000000..4ff6ddd
--- /dev/null
+++ b/t/chainlint/chained-subshell.test
@@ -0,0 +1,13 @@
+# LINT: start of subshell chained to preceding command
+mkdir sub && (
+ cd sub &&
+ foo the bar
+ nuff said
+) &&
+
+# LINT: preceding command pipes to subshell on same line
+cut "-d " -f actual | (read s1 s2 s3 &&
+test -f $s1
+test $(cat $s2) = tree2path1 &&
+# LINT: closing subshell ")" correctly detected on same line as "$(...)"
+test $(cat $s3) = tree3path1)
diff --git a/t/chainlint/close-nested-and-parent-together.expect b/t/chainlint/close-nested-and-parent-together.expect
new file mode 100644
index 0000000..72d482f
--- /dev/null
+++ b/t/chainlint/close-nested-and-parent-together.expect
@@ -0,0 +1,3 @@
+(cd foo &&
+ (bar &&
+ baz))
diff --git a/t/chainlint/close-nested-and-parent-together.test b/t/chainlint/close-nested-and-parent-together.test
new file mode 100644
index 0000000..72d482f
--- /dev/null
+++ b/t/chainlint/close-nested-and-parent-together.test
@@ -0,0 +1,3 @@
+(cd foo &&
+ (bar &&
+ baz))
diff --git a/t/chainlint/close-subshell.expect b/t/chainlint/close-subshell.expect
new file mode 100644
index 0000000..2192a28
--- /dev/null
+++ b/t/chainlint/close-subshell.expect
@@ -0,0 +1,26 @@
+(
+ foo
+) &&
+(
+ bar
+) >out &&
+(
+ baz
+) 2>err &&
+(
+ boo
+) <input &&
+(
+ bip
+) | wuzzle &&
+(
+ bop
+) | fazz \
+ fozz &&
+(
+ bup
+) |
+fuzzle &&
+(
+ yop
+)
diff --git a/t/chainlint/close-subshell.test b/t/chainlint/close-subshell.test
new file mode 100644
index 0000000..508ca44
--- /dev/null
+++ b/t/chainlint/close-subshell.test
@@ -0,0 +1,27 @@
+# LINT: closing ")" with various decorations ("&&", ">", "|", etc.)
+(
+ foo
+) &&
+(
+ bar
+) >out &&
+(
+ baz
+) 2>err &&
+(
+ boo
+) <input &&
+(
+ bip
+) | wuzzle &&
+(
+ bop
+) | fazz \
+ fozz &&
+(
+ bup
+) |
+fuzzle &&
+(
+ yop
+)
diff --git a/t/chainlint/command-substitution-subsubshell.expect b/t/chainlint/command-substitution-subsubshell.expect
new file mode 100644
index 0000000..ab2f79e
--- /dev/null
+++ b/t/chainlint/command-substitution-subsubshell.expect
@@ -0,0 +1,2 @@
+OUT=$(( ( large_git 1 >& 3 ) | : ) 3 >& 1) &&
+test_match_signal 13 "$OUT"
diff --git a/t/chainlint/command-substitution-subsubshell.test b/t/chainlint/command-substitution-subsubshell.test
new file mode 100644
index 0000000..321de29
--- /dev/null
+++ b/t/chainlint/command-substitution-subsubshell.test
@@ -0,0 +1,3 @@
+# LINT: subshell nested in subshell nested in command substitution
+OUT=$( ((large_git 1>&3) | :) 3>&1 ) &&
+test_match_signal 13 "$OUT"
diff --git a/t/chainlint/command-substitution.expect b/t/chainlint/command-substitution.expect
new file mode 100644
index 0000000..c72e4df
--- /dev/null
+++ b/t/chainlint/command-substitution.expect
@@ -0,0 +1,9 @@
+(
+ foo &&
+ bar=$(gobble) &&
+ baz
+) &&
+(
+ bar=$(gobble blocks) ?!AMP?!
+ baz
+)
diff --git a/t/chainlint/command-substitution.test b/t/chainlint/command-substitution.test
new file mode 100644
index 0000000..3bbb002
--- /dev/null
+++ b/t/chainlint/command-substitution.test
@@ -0,0 +1,11 @@
+(
+ foo &&
+# LINT: closing ")" of $(...) not misinterpreted as subshell-closing ")"
+ bar=$(gobble) &&
+ baz
+) &&
+(
+# LINT: missing "&&" on $(...)
+ bar=$(gobble blocks)
+ baz
+)
diff --git a/t/chainlint/comment.expect b/t/chainlint/comment.expect
new file mode 100644
index 0000000..a68f1f9
--- /dev/null
+++ b/t/chainlint/comment.expect
@@ -0,0 +1,8 @@
+(
+ # comment 1
+ nothing &&
+ # comment 2
+ something
+ # comment 3
+ # comment 4
+)
diff --git a/t/chainlint/comment.test b/t/chainlint/comment.test
new file mode 100644
index 0000000..113c0c4
--- /dev/null
+++ b/t/chainlint/comment.test
@@ -0,0 +1,11 @@
+(
+# LINT: swallow comment lines
+ # comment 1
+ nothing &&
+ # comment 2
+ something
+# LINT: swallow comment lines since final _statement_ before subshell end is
+# LINT: significant to "&&"-check, not final _line_ (which might be comment)
+ # comment 3
+ # comment 4
+)
diff --git a/t/chainlint/complex-if-in-cuddled-loop.expect b/t/chainlint/complex-if-in-cuddled-loop.expect
new file mode 100644
index 0000000..dac2d0f
--- /dev/null
+++ b/t/chainlint/complex-if-in-cuddled-loop.expect
@@ -0,0 +1,9 @@
+(for i in a b c; do
+ if test "$(echo $(waffle bat))" = "eleventeen" &&
+ test "$x" = "$y"; then
+ :
+ else
+ echo >file
+ fi ?!LOOP?!
+ done) &&
+test ! -f file
diff --git a/t/chainlint/complex-if-in-cuddled-loop.test b/t/chainlint/complex-if-in-cuddled-loop.test
new file mode 100644
index 0000000..5efeda5
--- /dev/null
+++ b/t/chainlint/complex-if-in-cuddled-loop.test
@@ -0,0 +1,11 @@
+# LINT: "for" loop cuddled with "(" and ")" and nested "if" with complex
+# LINT: multi-line condition; indented with spaces, not tabs
+(for i in a b c; do
+ if test "$(echo $(waffle bat))" = "eleventeen" &&
+ test "$x" = "$y"; then
+ :
+ else
+ echo >file
+ fi
+ done) &&
+test ! -f file
diff --git a/t/chainlint/cuddled-if-then-else.expect b/t/chainlint/cuddled-if-then-else.expect
new file mode 100644
index 0000000..1d8ed58
--- /dev/null
+++ b/t/chainlint/cuddled-if-then-else.expect
@@ -0,0 +1,6 @@
+(if test -z ""; then
+ echo empty
+ else
+ echo bizzy
+ fi) &&
+echo foobar
diff --git a/t/chainlint/cuddled-if-then-else.test b/t/chainlint/cuddled-if-then-else.test
new file mode 100644
index 0000000..7c53f4e
--- /dev/null
+++ b/t/chainlint/cuddled-if-then-else.test
@@ -0,0 +1,7 @@
+# LINT: "if" cuddled with "(" and ")"; indented with spaces, not tabs
+(if test -z ""; then
+ echo empty
+ else
+ echo bizzy
+ fi) &&
+echo foobar
diff --git a/t/chainlint/cuddled-loop.expect b/t/chainlint/cuddled-loop.expect
new file mode 100644
index 0000000..9cf2607
--- /dev/null
+++ b/t/chainlint/cuddled-loop.expect
@@ -0,0 +1,4 @@
+( while read x
+ do foobar bop || exit 1
+ done <file ) &&
+outside subshell
diff --git a/t/chainlint/cuddled-loop.test b/t/chainlint/cuddled-loop.test
new file mode 100644
index 0000000..3c2a62f
--- /dev/null
+++ b/t/chainlint/cuddled-loop.test
@@ -0,0 +1,7 @@
+# LINT: "while" loop cuddled with "(" and ")", with embedded (allowed)
+# LINT: "|| exit {n}" to exit loop early, and using redirection "<" to feed
+# LINT: loop; indented with spaces, not tabs
+( while read x
+ do foobar bop || exit 1
+ done <file ) &&
+outside subshell
diff --git a/t/chainlint/cuddled.expect b/t/chainlint/cuddled.expect
new file mode 100644
index 0000000..c3e0be4
--- /dev/null
+++ b/t/chainlint/cuddled.expect
@@ -0,0 +1,17 @@
+(cd foo &&
+ bar
+) &&
+
+(cd foo ?!AMP?!
+ bar
+) &&
+
+(
+ cd foo &&
+ bar) &&
+
+(cd foo &&
+ bar) &&
+
+(cd foo ?!AMP?!
+ bar)
diff --git a/t/chainlint/cuddled.test b/t/chainlint/cuddled.test
new file mode 100644
index 0000000..257b5b5
--- /dev/null
+++ b/t/chainlint/cuddled.test
@@ -0,0 +1,22 @@
+# LINT: first subshell statement cuddled with opening "("
+(cd foo &&
+ bar
+) &&
+
+# LINT: same with missing "&&"
+(cd foo
+ bar
+) &&
+
+# LINT: closing ")" cuddled with final subshell statement
+(
+ cd foo &&
+ bar) &&
+
+# LINT: "(" and ")" cuddled with first and final subshell statements
+(cd foo &&
+ bar) &&
+
+# LINT: same with missing "&&"
+(cd foo
+ bar)
diff --git a/t/chainlint/double-here-doc.expect b/t/chainlint/double-here-doc.expect
new file mode 100644
index 0000000..cd584a4
--- /dev/null
+++ b/t/chainlint/double-here-doc.expect
@@ -0,0 +1,12 @@
+run_sub_test_lib_test_err run-inv-range-start \
+ "--run invalid range start" \
+ --run="a-5" <<-\EOF &&
+test_expect_success "passing test #1" "true"
+test_done
+EOF
+check_sub_test_lib_test_err run-inv-range-start \
+ <<-\EOF_OUT 3<<-EOF_ERR
+> FATAL: Unexpected exit with code 1
+EOF_OUT
+> error: --run: invalid non-numeric in range start: ${SQ}a-5${SQ}
+EOF_ERR
diff --git a/t/chainlint/double-here-doc.test b/t/chainlint/double-here-doc.test
new file mode 100644
index 0000000..cd584a4
--- /dev/null
+++ b/t/chainlint/double-here-doc.test
@@ -0,0 +1,12 @@
+run_sub_test_lib_test_err run-inv-range-start \
+ "--run invalid range start" \
+ --run="a-5" <<-\EOF &&
+test_expect_success "passing test #1" "true"
+test_done
+EOF
+check_sub_test_lib_test_err run-inv-range-start \
+ <<-\EOF_OUT 3<<-EOF_ERR
+> FATAL: Unexpected exit with code 1
+EOF_OUT
+> error: --run: invalid non-numeric in range start: ${SQ}a-5${SQ}
+EOF_ERR
diff --git a/t/chainlint/dqstring-line-splice.expect b/t/chainlint/dqstring-line-splice.expect
new file mode 100644
index 0000000..bf9ced6
--- /dev/null
+++ b/t/chainlint/dqstring-line-splice.expect
@@ -0,0 +1,3 @@
+echo 'fatal: reword option of --fixup is mutually exclusive with' '--patch/--interactive/--all/--include/--only' > expect &&
+test_must_fail git commit --fixup=reword:HEAD~ $1 2 > actual &&
+test_cmp expect actual
diff --git a/t/chainlint/dqstring-line-splice.test b/t/chainlint/dqstring-line-splice.test
new file mode 100644
index 0000000..b407144
--- /dev/null
+++ b/t/chainlint/dqstring-line-splice.test
@@ -0,0 +1,7 @@
+# LINT: line-splice within DQ-string
+'"
+echo 'fatal: reword option of --fixup is mutually exclusive with'\
+ '--patch/--interactive/--all/--include/--only' >expect &&
+test_must_fail git commit --fixup=reword:HEAD~ $1 2>actual &&
+test_cmp expect actual
+"'
diff --git a/t/chainlint/dqstring-no-interpolate.expect b/t/chainlint/dqstring-no-interpolate.expect
new file mode 100644
index 0000000..1072498
--- /dev/null
+++ b/t/chainlint/dqstring-no-interpolate.expect
@@ -0,0 +1,11 @@
+grep "^ ! [rejected][ ]*$BRANCH -> $BRANCH (non-fast-forward)$" out &&
+
+grep "^\.git$" output.txt &&
+
+
+(
+ cd client$version &&
+ GIT_TEST_PROTOCOL_VERSION=$version git fetch-pack --no-progress .. $(cat ../input)
+) > output &&
+ cut -d ' ' -f 2 < output | sort > actual &&
+ test_cmp expect actual
diff --git a/t/chainlint/dqstring-no-interpolate.test b/t/chainlint/dqstring-no-interpolate.test
new file mode 100644
index 0000000..d2f4219
--- /dev/null
+++ b/t/chainlint/dqstring-no-interpolate.test
@@ -0,0 +1,15 @@
+# LINT: regex dollar-sign eol anchor in double-quoted string not special
+grep "^ ! \[rejected\][ ]*$BRANCH -> $BRANCH (non-fast-forward)$" out &&
+
+# LINT: escaped "$" not mistaken for variable expansion
+grep "^\\.git\$" output.txt &&
+
+'"
+(
+ cd client$version &&
+# LINT: escaped dollar-sign in double-quoted test body
+ GIT_TEST_PROTOCOL_VERSION=$version git fetch-pack --no-progress .. \$(cat ../input)
+) >output &&
+ cut -d ' ' -f 2 <output | sort >actual &&
+ test_cmp expect actual
+"'
diff --git a/t/chainlint/empty-here-doc.expect b/t/chainlint/empty-here-doc.expect
new file mode 100644
index 0000000..e8733c9
--- /dev/null
+++ b/t/chainlint/empty-here-doc.expect
@@ -0,0 +1,4 @@
+git ls-tree $tree path > current &&
+cat > expected <<\EOF &&
+EOF
+test_output
diff --git a/t/chainlint/empty-here-doc.test b/t/chainlint/empty-here-doc.test
new file mode 100644
index 0000000..24fc165
--- /dev/null
+++ b/t/chainlint/empty-here-doc.test
@@ -0,0 +1,5 @@
+git ls-tree $tree path >current &&
+# LINT: empty here-doc
+cat >expected <<\EOF &&
+EOF
+test_output
diff --git a/t/chainlint/exclamation.expect b/t/chainlint/exclamation.expect
new file mode 100644
index 0000000..2d961a5
--- /dev/null
+++ b/t/chainlint/exclamation.expect
@@ -0,0 +1,4 @@
+if ! condition ; then echo nope ; else yep ; fi &&
+test_prerequisite !MINGW &&
+mail uucp!address &&
+echo !whatever!
diff --git a/t/chainlint/exclamation.test b/t/chainlint/exclamation.test
new file mode 100644
index 0000000..323595b
--- /dev/null
+++ b/t/chainlint/exclamation.test
@@ -0,0 +1,8 @@
+# LINT: "! word" is two tokens
+if ! condition; then echo nope; else yep; fi &&
+# LINT: "!word" is single token, not two tokens "!" and "word"
+test_prerequisite !MINGW &&
+# LINT: "word!word" is single token, not three tokens "word", "!", and "word"
+mail uucp!address &&
+# LINT: "!word!" is single token, not three tokens "!", "word", and "!"
+echo !whatever!
diff --git a/t/chainlint/exit-loop.expect b/t/chainlint/exit-loop.expect
new file mode 100644
index 0000000..f76aa60
--- /dev/null
+++ b/t/chainlint/exit-loop.expect
@@ -0,0 +1,24 @@
+(
+ for i in a b c
+ do
+ foo || exit 1
+ bar &&
+ baz
+ done
+) &&
+(
+ while true
+ do
+ foo || exit 1
+ bar &&
+ baz
+ done
+) &&
+(
+ i=0 &&
+ while test $i -lt 10
+ do
+ echo $i || exit
+ i=$(($i + 1))
+ done
+)
diff --git a/t/chainlint/exit-loop.test b/t/chainlint/exit-loop.test
new file mode 100644
index 0000000..2f03820
--- /dev/null
+++ b/t/chainlint/exit-loop.test
@@ -0,0 +1,27 @@
+(
+ for i in a b c
+ do
+# LINT: "|| exit {n}" valid for-loop escape in subshell; no "&&" needed
+ foo || exit 1
+ bar &&
+ baz
+ done
+) &&
+(
+ while true
+ do
+# LINT: "|| exit {n}" valid while-loop escape in subshell; no "&&" needed
+ foo || exit 1
+ bar &&
+ baz
+ done
+) &&
+(
+ i=0 &&
+ while test $i -lt 10
+ do
+# LINT: "|| exit" (sans exit code) valid escape in subshell; no "&&" needed
+ echo $i || exit
+ i=$(($i + 1))
+ done
+)
diff --git a/t/chainlint/exit-subshell.expect b/t/chainlint/exit-subshell.expect
new file mode 100644
index 0000000..da80339
--- /dev/null
+++ b/t/chainlint/exit-subshell.expect
@@ -0,0 +1,5 @@
+(
+ foo || exit 1
+ bar &&
+ baz
+)
diff --git a/t/chainlint/exit-subshell.test b/t/chainlint/exit-subshell.test
new file mode 100644
index 0000000..4e6ab69
--- /dev/null
+++ b/t/chainlint/exit-subshell.test
@@ -0,0 +1,6 @@
+(
+# LINT: "|| exit {n}" valid subshell escape without hurting &&-chain
+ foo || exit 1
+ bar &&
+ baz
+)
diff --git a/t/chainlint/for-loop-abbreviated.expect b/t/chainlint/for-loop-abbreviated.expect
new file mode 100644
index 0000000..a21007a
--- /dev/null
+++ b/t/chainlint/for-loop-abbreviated.expect
@@ -0,0 +1,5 @@
+for it
+do
+ path=$(expr "$it" : ( [^:]*) ) &&
+ git update-index --add "$path" || exit
+done
diff --git a/t/chainlint/for-loop-abbreviated.test b/t/chainlint/for-loop-abbreviated.test
new file mode 100644
index 0000000..1084ecc
--- /dev/null
+++ b/t/chainlint/for-loop-abbreviated.test
@@ -0,0 +1,6 @@
+# LINT: for-loop lacking optional "in [word...]" before "do"
+for it
+do
+ path=$(expr "$it" : '\([^:]*\)') &&
+ git update-index --add "$path" || exit
+done
diff --git a/t/chainlint/for-loop.expect b/t/chainlint/for-loop.expect
new file mode 100644
index 0000000..d65c821
--- /dev/null
+++ b/t/chainlint/for-loop.expect
@@ -0,0 +1,13 @@
+(
+ for i in a b c
+ do
+ echo $i ?!AMP?!
+ cat <<-\EOF ?!LOOP?!
+ bar
+ EOF
+ done ?!AMP?!
+ for i in a b c; do
+ echo $i &&
+ cat $i ?!LOOP?!
+ done
+)
diff --git a/t/chainlint/for-loop.test b/t/chainlint/for-loop.test
new file mode 100644
index 0000000..6cb3428
--- /dev/null
+++ b/t/chainlint/for-loop.test
@@ -0,0 +1,19 @@
+(
+# LINT: "for", "do", "done" do not need "&&"
+ for i in a b c
+ do
+# LINT: missing "&&" on "echo"
+ echo $i
+# LINT: last statement of while does not need "&&"
+ cat <<-\EOF
+ bar
+ EOF
+# LINT: missing "&&" on "done"
+ done
+
+# LINT: "do" on same line as "for"
+ for i in a b c; do
+ echo $i &&
+ cat $i
+ done
+)
diff --git a/t/chainlint/function.expect b/t/chainlint/function.expect
new file mode 100644
index 0000000..a14388e
--- /dev/null
+++ b/t/chainlint/function.expect
@@ -0,0 +1,11 @@
+sha1_file ( ) {
+ echo "$*" | sed "s#..#.git/objects/&/#"
+} &&
+
+remove_object ( ) {
+ file=$(sha1_file "$*") &&
+ test -e "$file" ?!AMP?!
+ rm -f "$file"
+} ?!AMP?!
+
+sha1_file arg && remove_object arg
diff --git a/t/chainlint/function.test b/t/chainlint/function.test
new file mode 100644
index 0000000..5ee5956
--- /dev/null
+++ b/t/chainlint/function.test
@@ -0,0 +1,13 @@
+# LINT: "()" in function definition not mistaken for subshell
+sha1_file() {
+ echo "$*" | sed "s#..#.git/objects/&/#"
+} &&
+
+# LINT: broken &&-chain in function and after function
+remove_object() {
+ file=$(sha1_file "$*") &&
+ test -e "$file"
+ rm -f "$file"
+}
+
+sha1_file arg && remove_object arg
diff --git a/t/chainlint/here-doc-close-subshell.expect b/t/chainlint/here-doc-close-subshell.expect
new file mode 100644
index 0000000..7d9c2b5
--- /dev/null
+++ b/t/chainlint/here-doc-close-subshell.expect
@@ -0,0 +1,4 @@
+(
+ cat <<-\INPUT)
+ fizz
+ INPUT
diff --git a/t/chainlint/here-doc-close-subshell.test b/t/chainlint/here-doc-close-subshell.test
new file mode 100644
index 0000000..b857ff5
--- /dev/null
+++ b/t/chainlint/here-doc-close-subshell.test
@@ -0,0 +1,5 @@
+(
+# LINT: line contains here-doc and closes nested subshell
+ cat <<-\INPUT)
+ fizz
+ INPUT
diff --git a/t/chainlint/here-doc-indent-operator.expect b/t/chainlint/here-doc-indent-operator.expect
new file mode 100644
index 0000000..f92a7ce
--- /dev/null
+++ b/t/chainlint/here-doc-indent-operator.expect
@@ -0,0 +1,11 @@
+cat >expect <<- EOF &&
+header: 43475048 1 $(test_oid oid_version) $NUM_CHUNKS 0
+num_commits: $1
+chunks: oid_fanout oid_lookup commit_metadata generation_data bloom_indexes bloom_data
+EOF
+
+cat >expect << -EOF ?!AMP?!
+this is not indented
+-EOF
+
+cleanup
diff --git a/t/chainlint/here-doc-indent-operator.test b/t/chainlint/here-doc-indent-operator.test
new file mode 100644
index 0000000..c8a6f18
--- /dev/null
+++ b/t/chainlint/here-doc-indent-operator.test
@@ -0,0 +1,13 @@
+# LINT: whitespace between operator "<<-" and tag legal
+cat >expect <<- EOF &&
+header: 43475048 1 $(test_oid oid_version) $NUM_CHUNKS 0
+num_commits: $1
+chunks: oid_fanout oid_lookup commit_metadata generation_data bloom_indexes bloom_data
+EOF
+
+# LINT: not an indented here-doc; just a plain here-doc with tag named "-EOF"
+cat >expect << -EOF
+this is not indented
+-EOF
+
+cleanup
diff --git a/t/chainlint/here-doc-multi-line-command-subst.expect b/t/chainlint/here-doc-multi-line-command-subst.expect
new file mode 100644
index 0000000..b7364c8
--- /dev/null
+++ b/t/chainlint/here-doc-multi-line-command-subst.expect
@@ -0,0 +1,8 @@
+(
+ x=$(bobble <<-\END &&
+ fossil
+ vegetable
+ END
+ wiffle) ?!AMP?!
+ echo $x
+)
diff --git a/t/chainlint/here-doc-multi-line-command-subst.test b/t/chainlint/here-doc-multi-line-command-subst.test
new file mode 100644
index 0000000..899bc5d
--- /dev/null
+++ b/t/chainlint/here-doc-multi-line-command-subst.test
@@ -0,0 +1,9 @@
+(
+# LINT: line contains here-doc and opens multi-line $(...)
+ x=$(bobble <<-\END &&
+ fossil
+ vegetable
+ END
+ wiffle)
+ echo $x
+)
diff --git a/t/chainlint/here-doc-multi-line-string.expect b/t/chainlint/here-doc-multi-line-string.expect
new file mode 100644
index 0000000..6c13bdc
--- /dev/null
+++ b/t/chainlint/here-doc-multi-line-string.expect
@@ -0,0 +1,7 @@
+(
+ cat <<-\TXT && echo "multi-line
+ string" ?!AMP?!
+ fizzle
+ TXT
+ bap
+)
diff --git a/t/chainlint/here-doc-multi-line-string.test b/t/chainlint/here-doc-multi-line-string.test
new file mode 100644
index 0000000..a53edbc
--- /dev/null
+++ b/t/chainlint/here-doc-multi-line-string.test
@@ -0,0 +1,8 @@
+(
+# LINT: line contains here-doc and opens multi-line string
+ cat <<-\TXT && echo "multi-line
+ string"
+ fizzle
+ TXT
+ bap
+)
diff --git a/t/chainlint/here-doc.expect b/t/chainlint/here-doc.expect
new file mode 100644
index 0000000..1df3f78
--- /dev/null
+++ b/t/chainlint/here-doc.expect
@@ -0,0 +1,25 @@
+boodle wobba \
+ gorgo snoot \
+ wafta snurb <<EOF &&
+quoth the raven,
+nevermore...
+EOF
+
+cat <<-Arbitrary_Tag_42 >foo &&
+snoz
+boz
+woz
+Arbitrary_Tag_42
+
+cat <<"zump" >boo &&
+snoz
+boz
+woz
+zump
+
+horticulture <<\EOF
+gomez
+morticia
+wednesday
+pugsly
+EOF
diff --git a/t/chainlint/here-doc.test b/t/chainlint/here-doc.test
new file mode 100644
index 0000000..3f5f92c
--- /dev/null
+++ b/t/chainlint/here-doc.test
@@ -0,0 +1,30 @@
+# LINT: stitch together incomplete \-ending lines
+# LINT: swallow here-doc to avoid false positives in content
+boodle wobba \
+ gorgo snoot \
+ wafta snurb <<EOF &&
+quoth the raven,
+nevermore...
+EOF
+
+# LINT: swallow here-doc with arbitrary tag
+cat <<-Arbitrary_Tag_42 >foo &&
+snoz
+boz
+woz
+Arbitrary_Tag_42
+
+# LINT: swallow "quoted" here-doc
+cat <<"zump" >boo &&
+snoz
+boz
+woz
+zump
+
+# LINT: swallow here-doc (EOF is last line of test)
+horticulture <<\EOF
+gomez
+morticia
+wednesday
+pugsly
+EOF
diff --git a/t/chainlint/if-condition-split.expect b/t/chainlint/if-condition-split.expect
new file mode 100644
index 0000000..ee745ef
--- /dev/null
+++ b/t/chainlint/if-condition-split.expect
@@ -0,0 +1,7 @@
+if bob &&
+ marcia ||
+ kevin
+then
+ echo "nomads" ?!AMP?!
+ echo "for sure"
+fi
diff --git a/t/chainlint/if-condition-split.test b/t/chainlint/if-condition-split.test
new file mode 100644
index 0000000..240daa9
--- /dev/null
+++ b/t/chainlint/if-condition-split.test
@@ -0,0 +1,8 @@
+# LINT: "if" condition split across multiple lines at "&&" or "||"
+if bob &&
+ marcia ||
+ kevin
+then
+ echo "nomads"
+ echo "for sure"
+fi
diff --git a/t/chainlint/if-in-loop.expect b/t/chainlint/if-in-loop.expect
new file mode 100644
index 0000000..d6514ae
--- /dev/null
+++ b/t/chainlint/if-in-loop.expect
@@ -0,0 +1,12 @@
+(
+ for i in a b c
+ do
+ if false
+ then
+ echo "err"
+ exit 1
+ fi ?!AMP?!
+ foo
+ done ?!AMP?!
+ bar
+)
diff --git a/t/chainlint/if-in-loop.test b/t/chainlint/if-in-loop.test
new file mode 100644
index 0000000..90c2397
--- /dev/null
+++ b/t/chainlint/if-in-loop.test
@@ -0,0 +1,15 @@
+(
+ for i in a b c
+ do
+ if false
+ then
+# LINT: missing "&&" on "echo" okay since "exit 1" signals error explicitly
+ echo "err"
+ exit 1
+# LINT: missing "&&" on "fi"
+ fi
+ foo
+# LINT: missing "&&" on "done"
+ done
+ bar
+)
diff --git a/t/chainlint/if-then-else.expect b/t/chainlint/if-then-else.expect
new file mode 100644
index 0000000..cbaaf85
--- /dev/null
+++ b/t/chainlint/if-then-else.expect
@@ -0,0 +1,22 @@
+(
+ if test -n ""
+ then
+ echo very ?!AMP?!
+ echo empty
+ elif test -z ""
+ then
+ echo foo
+ else
+ echo foo &&
+ cat <<-\EOF
+ bar
+ EOF
+ fi ?!AMP?!
+ echo poodle
+) &&
+(
+ if test -n ""; then
+ echo very &&
+ echo empty
+ fi
+)
diff --git a/t/chainlint/if-then-else.test b/t/chainlint/if-then-else.test
new file mode 100644
index 0000000..2055336
--- /dev/null
+++ b/t/chainlint/if-then-else.test
@@ -0,0 +1,29 @@
+(
+# LINT: "if", "then", "elif", "else", "fi" do not need "&&"
+ if test -n ""
+ then
+# LINT: missing "&&" on "echo"
+ echo very
+# LINT: last statement before "elif" does not need "&&"
+ echo empty
+ elif test -z ""
+ then
+# LINT: last statement before "else" does not need "&&"
+ echo foo
+ else
+ echo foo &&
+# LINT: last statement before "fi" does not need "&&"
+ cat <<-\EOF
+ bar
+ EOF
+# LINT: missing "&&" on "fi"
+ fi
+ echo poodle
+) &&
+(
+# LINT: "then" on same line as "if"
+ if test -n ""; then
+ echo very &&
+ echo empty
+ fi
+)
diff --git a/t/chainlint/incomplete-line.expect b/t/chainlint/incomplete-line.expect
new file mode 100644
index 0000000..134d3a1
--- /dev/null
+++ b/t/chainlint/incomplete-line.expect
@@ -0,0 +1,10 @@
+line 1 \
+line 2 \
+line 3 \
+line 4 &&
+(
+ line 5 \
+ line 6 \
+ line 7 \
+ line 8
+)
diff --git a/t/chainlint/incomplete-line.test b/t/chainlint/incomplete-line.test
new file mode 100644
index 0000000..d856658
--- /dev/null
+++ b/t/chainlint/incomplete-line.test
@@ -0,0 +1,12 @@
+# LINT: stitch together all incomplete \-ending lines
+line 1 \
+line 2 \
+line 3 \
+line 4 &&
+(
+# LINT: stitch together all incomplete \-ending lines (subshell)
+ line 5 \
+ line 6 \
+ line 7 \
+ line 8
+)
diff --git a/t/chainlint/inline-comment.expect b/t/chainlint/inline-comment.expect
new file mode 100644
index 0000000..6bad218
--- /dev/null
+++ b/t/chainlint/inline-comment.expect
@@ -0,0 +1,8 @@
+(
+ foobar && # comment 1
+ barfoo ?!AMP?! # wrong position for &&
+ flibble "not a # comment"
+) &&
+
+(cd foo &&
+ flibble "not a # comment")
diff --git a/t/chainlint/inline-comment.test b/t/chainlint/inline-comment.test
new file mode 100644
index 0000000..8f26856
--- /dev/null
+++ b/t/chainlint/inline-comment.test
@@ -0,0 +1,12 @@
+(
+# LINT: swallow inline comment (leaving command intact)
+ foobar && # comment 1
+# LINT: mispositioned "&&" (correctly) swallowed with comment
+ barfoo # wrong position for &&
+# LINT: "#" in string not misinterpreted as comment
+ flibble "not a # comment"
+) &&
+
+# LINT: "#" in string in cuddled subshell not misinterpreted as comment
+(cd foo &&
+ flibble "not a # comment")
diff --git a/t/chainlint/loop-detect-failure.expect b/t/chainlint/loop-detect-failure.expect
new file mode 100644
index 0000000..a66025c
--- /dev/null
+++ b/t/chainlint/loop-detect-failure.expect
@@ -0,0 +1,15 @@
+git init r1 &&
+for n in 1 2 3 4 5
+do
+ echo "This is file: $n" > r1/file.$n &&
+ git -C r1 add file.$n &&
+ git -C r1 commit -m "$n" || return 1
+done &&
+
+git init r2 &&
+for n in 1000 10000
+do
+ printf "%"$n"s" X > r2/large.$n &&
+ git -C r2 add large.$n &&
+ git -C r2 commit -m "$n" ?!LOOP?!
+done
diff --git a/t/chainlint/loop-detect-failure.test b/t/chainlint/loop-detect-failure.test
new file mode 100644
index 0000000..b9791cc
--- /dev/null
+++ b/t/chainlint/loop-detect-failure.test
@@ -0,0 +1,17 @@
+git init r1 &&
+# LINT: loop handles failure explicitly with "|| return 1"
+for n in 1 2 3 4 5
+do
+ echo "This is file: $n" > r1/file.$n &&
+ git -C r1 add file.$n &&
+ git -C r1 commit -m "$n" || return 1
+done &&
+
+git init r2 &&
+# LINT: loop fails to handle failure explicitly with "|| return 1"
+for n in 1000 10000
+do
+ printf "%"$n"s" X > r2/large.$n &&
+ git -C r2 add large.$n &&
+ git -C r2 commit -m "$n"
+done
diff --git a/t/chainlint/loop-detect-status.expect b/t/chainlint/loop-detect-status.expect
new file mode 100644
index 0000000..24da9e8
--- /dev/null
+++ b/t/chainlint/loop-detect-status.expect
@@ -0,0 +1,18 @@
+( while test $i -le $blobcount
+do
+ printf "Generating blob $i/$blobcount\r" >& 2 &&
+ printf "blob\nmark :$i\ndata $blobsize\n" &&
+ #test-tool genrandom $i $blobsize &&
+ printf "%-${blobsize}s" $i &&
+ echo "M 100644 :$i $i" >> commit &&
+ i=$(($i+1)) ||
+ echo $? > exit-status
+done &&
+echo "commit refs/heads/main" &&
+echo "author A U Thor <author@email.com> 123456789 +0000" &&
+echo "committer C O Mitter <committer@email.com> 123456789 +0000" &&
+echo "data 5" &&
+echo ">2gb" &&
+cat commit ) |
+git fast-import --big-file-threshold=2 &&
+test ! -f exit-status
diff --git a/t/chainlint/loop-detect-status.test b/t/chainlint/loop-detect-status.test
new file mode 100644
index 0000000..1c6c23c
--- /dev/null
+++ b/t/chainlint/loop-detect-status.test
@@ -0,0 +1,19 @@
+# LINT: "$?" handled explicitly within loop body
+(while test $i -le $blobcount
+ do
+ printf "Generating blob $i/$blobcount\r" >&2 &&
+ printf "blob\nmark :$i\ndata $blobsize\n" &&
+ #test-tool genrandom $i $blobsize &&
+ printf "%-${blobsize}s" $i &&
+ echo "M 100644 :$i $i" >> commit &&
+ i=$(($i+1)) ||
+ echo $? > exit-status
+ done &&
+ echo "commit refs/heads/main" &&
+ echo "author A U Thor <author@email.com> 123456789 +0000" &&
+ echo "committer C O Mitter <committer@email.com> 123456789 +0000" &&
+ echo "data 5" &&
+ echo ">2gb" &&
+ cat commit) |
+git fast-import --big-file-threshold=2 &&
+test ! -f exit-status
diff --git a/t/chainlint/loop-in-if.expect b/t/chainlint/loop-in-if.expect
new file mode 100644
index 0000000..6c5d6e5
--- /dev/null
+++ b/t/chainlint/loop-in-if.expect
@@ -0,0 +1,12 @@
+(
+ if true
+ then
+ while true
+ do
+ echo "pop" ?!AMP?!
+ echo "glup" ?!LOOP?!
+ done ?!AMP?!
+ foo
+ fi ?!AMP?!
+ bar
+)
diff --git a/t/chainlint/loop-in-if.test b/t/chainlint/loop-in-if.test
new file mode 100644
index 0000000..dfcc3f9
--- /dev/null
+++ b/t/chainlint/loop-in-if.test
@@ -0,0 +1,15 @@
+(
+ if true
+ then
+ while true
+ do
+# LINT: missing "&&" on "echo"
+ echo "pop"
+ echo "glup"
+# LINT: missing "&&" on "done"
+ done
+ foo
+# LINT: missing "&&" on "fi"
+ fi
+ bar
+)
diff --git a/t/chainlint/loop-upstream-pipe.expect b/t/chainlint/loop-upstream-pipe.expect
new file mode 100644
index 0000000..0b82ecc
--- /dev/null
+++ b/t/chainlint/loop-upstream-pipe.expect
@@ -0,0 +1,10 @@
+(
+ git rev-list --objects --no-object-names base..loose |
+ while read oid
+ do
+ path="$objdir/$(test_oid_to_path "$oid")" &&
+ printf "%s %d\n" "$oid" "$(test-tool chmtime --get "$path")" ||
+ echo "object list generation failed for $oid"
+ done |
+ sort -k1
+) >expect &&
diff --git a/t/chainlint/loop-upstream-pipe.test b/t/chainlint/loop-upstream-pipe.test
new file mode 100644
index 0000000..efb77da
--- /dev/null
+++ b/t/chainlint/loop-upstream-pipe.test
@@ -0,0 +1,11 @@
+(
+ git rev-list --objects --no-object-names base..loose |
+ while read oid
+ do
+# LINT: "|| echo" signals failure in loop upstream of a pipe
+ path="$objdir/$(test_oid_to_path "$oid")" &&
+ printf "%s %d\n" "$oid" "$(test-tool chmtime --get "$path")" ||
+ echo "object list generation failed for $oid"
+ done |
+ sort -k1
+) >expect &&
diff --git a/t/chainlint/multi-line-nested-command-substitution.expect b/t/chainlint/multi-line-nested-command-substitution.expect
new file mode 100644
index 0000000..3000583
--- /dev/null
+++ b/t/chainlint/multi-line-nested-command-substitution.expect
@@ -0,0 +1,18 @@
+(
+ foo &&
+ x=$(
+ echo bar |
+ cat
+ ) &&
+ echo ok
+) |
+sort &&
+(
+ bar &&
+ x=$(echo bar |
+ cat
+ ) &&
+ y=$(echo baz |
+ fip) &&
+ echo fail
+)
diff --git a/t/chainlint/multi-line-nested-command-substitution.test b/t/chainlint/multi-line-nested-command-substitution.test
new file mode 100644
index 0000000..3000583
--- /dev/null
+++ b/t/chainlint/multi-line-nested-command-substitution.test
@@ -0,0 +1,18 @@
+(
+ foo &&
+ x=$(
+ echo bar |
+ cat
+ ) &&
+ echo ok
+) |
+sort &&
+(
+ bar &&
+ x=$(echo bar |
+ cat
+ ) &&
+ y=$(echo baz |
+ fip) &&
+ echo fail
+)
diff --git a/t/chainlint/multi-line-string.expect b/t/chainlint/multi-line-string.expect
new file mode 100644
index 0000000..27ff952
--- /dev/null
+++ b/t/chainlint/multi-line-string.expect
@@ -0,0 +1,14 @@
+(
+ x="line 1
+ line 2
+ line 3" &&
+ y="line 1
+ line2" ?!AMP?!
+ foobar
+) &&
+(
+ echo "xyz" "abc
+ def
+ ghi" &&
+ barfoo
+)
diff --git a/t/chainlint/multi-line-string.test b/t/chainlint/multi-line-string.test
new file mode 100644
index 0000000..4a0af21
--- /dev/null
+++ b/t/chainlint/multi-line-string.test
@@ -0,0 +1,15 @@
+(
+ x="line 1
+ line 2
+ line 3" &&
+# LINT: missing "&&" on assignment
+ y="line 1
+ line2"
+ foobar
+) &&
+(
+ echo "xyz" "abc
+ def
+ ghi" &&
+ barfoo
+)
diff --git a/t/chainlint/negated-one-liner.expect b/t/chainlint/negated-one-liner.expect
new file mode 100644
index 0000000..ad4c2d9
--- /dev/null
+++ b/t/chainlint/negated-one-liner.expect
@@ -0,0 +1,5 @@
+! (foo && bar) &&
+! (foo && bar) >baz &&
+
+! (foo; ?!AMP?! bar) &&
+! (foo; ?!AMP?! bar) >baz
diff --git a/t/chainlint/negated-one-liner.test b/t/chainlint/negated-one-liner.test
new file mode 100644
index 0000000..c9598e9
--- /dev/null
+++ b/t/chainlint/negated-one-liner.test
@@ -0,0 +1,7 @@
+# LINT: top-level one-liner subshell
+! (foo && bar) &&
+! (foo && bar) >baz &&
+
+# LINT: top-level one-liner subshell missing internal "&&"
+! (foo; bar) &&
+! (foo; bar) >baz
diff --git a/t/chainlint/nested-cuddled-subshell.expect b/t/chainlint/nested-cuddled-subshell.expect
new file mode 100644
index 0000000..2a86885
--- /dev/null
+++ b/t/chainlint/nested-cuddled-subshell.expect
@@ -0,0 +1,19 @@
+(
+ (cd foo &&
+ bar
+ ) &&
+ (cd foo &&
+ bar
+ ) ?!AMP?!
+ (
+ cd foo &&
+ bar) &&
+ (
+ cd foo &&
+ bar) ?!AMP?!
+ (cd foo &&
+ bar) &&
+ (cd foo &&
+ bar) ?!AMP?!
+ foobar
+)
diff --git a/t/chainlint/nested-cuddled-subshell.test b/t/chainlint/nested-cuddled-subshell.test
new file mode 100644
index 0000000..8fd656c
--- /dev/null
+++ b/t/chainlint/nested-cuddled-subshell.test
@@ -0,0 +1,31 @@
+(
+# LINT: opening "(" cuddled with first nested subshell statement
+ (cd foo &&
+ bar
+ ) &&
+
+# LINT: same but "&&" missing
+ (cd foo &&
+ bar
+ )
+
+# LINT: closing ")" cuddled with final nested subshell statement
+ (
+ cd foo &&
+ bar) &&
+
+# LINT: same but "&&" missing
+ (
+ cd foo &&
+ bar)
+
+# LINT: "(" and ")" cuddled with first and final subshell statements
+ (cd foo &&
+ bar) &&
+
+# LINT: same but "&&" missing
+ (cd foo &&
+ bar)
+
+ foobar
+)
diff --git a/t/chainlint/nested-here-doc.expect b/t/chainlint/nested-here-doc.expect
new file mode 100644
index 0000000..29b3832
--- /dev/null
+++ b/t/chainlint/nested-here-doc.expect
@@ -0,0 +1,30 @@
+cat <<ARBITRARY >foop &&
+naddle
+fub <<EOF
+ nozzle
+ noodle
+EOF
+formp
+ARBITRARY
+
+(
+ cat <<-\INPUT_END &&
+ fish are mice
+ but geese go slow
+ data <<EOF
+ perl is lerp
+ and nothing else
+ EOF
+ toink
+ INPUT_END
+
+ cat <<-\EOT ?!AMP?!
+ text goes here
+ data <<EOF
+ data goes here
+ EOF
+ more test here
+ EOT
+
+ foobar
+)
diff --git a/t/chainlint/nested-here-doc.test b/t/chainlint/nested-here-doc.test
new file mode 100644
index 0000000..f35404b
--- /dev/null
+++ b/t/chainlint/nested-here-doc.test
@@ -0,0 +1,33 @@
+# LINT: inner "EOF" not misintrepreted as closing ARBITRARY here-doc
+cat <<ARBITRARY >foop &&
+naddle
+fub <<EOF
+ nozzle
+ noodle
+EOF
+formp
+ARBITRARY
+
+(
+# LINT: inner "EOF" not misintrepreted as closing INPUT_END here-doc
+ cat <<-\INPUT_END &&
+ fish are mice
+ but geese go slow
+ data <<EOF
+ perl is lerp
+ and nothing else
+ EOF
+ toink
+ INPUT_END
+
+# LINT: same but missing "&&"
+ cat <<-\EOT
+ text goes here
+ data <<EOF
+ data goes here
+ EOF
+ more test here
+ EOT
+
+ foobar
+)
diff --git a/t/chainlint/nested-loop-detect-failure.expect b/t/chainlint/nested-loop-detect-failure.expect
new file mode 100644
index 0000000..4793a0e
--- /dev/null
+++ b/t/chainlint/nested-loop-detect-failure.expect
@@ -0,0 +1,31 @@
+for i in 0 1 2 3 4 5 6 7 8 9 ;
+do
+ for j in 0 1 2 3 4 5 6 7 8 9 ;
+ do
+ echo "$i$j" > "path$i$j" ?!LOOP?!
+ done ?!LOOP?!
+done &&
+
+for i in 0 1 2 3 4 5 6 7 8 9 ;
+do
+ for j in 0 1 2 3 4 5 6 7 8 9 ;
+ do
+ echo "$i$j" > "path$i$j" || return 1
+ done
+done &&
+
+for i in 0 1 2 3 4 5 6 7 8 9 ;
+do
+ for j in 0 1 2 3 4 5 6 7 8 9 ;
+ do
+ echo "$i$j" > "path$i$j" ?!LOOP?!
+ done || return 1
+done &&
+
+for i in 0 1 2 3 4 5 6 7 8 9 ;
+do
+ for j in 0 1 2 3 4 5 6 7 8 9 ;
+ do
+ echo "$i$j" > "path$i$j" || return 1
+ done || return 1
+done
diff --git a/t/chainlint/nested-loop-detect-failure.test b/t/chainlint/nested-loop-detect-failure.test
new file mode 100644
index 0000000..e6f0c1a
--- /dev/null
+++ b/t/chainlint/nested-loop-detect-failure.test
@@ -0,0 +1,35 @@
+# LINT: neither loop handles failure explicitly with "|| return 1"
+for i in 0 1 2 3 4 5 6 7 8 9;
+do
+ for j in 0 1 2 3 4 5 6 7 8 9;
+ do
+ echo "$i$j" >"path$i$j"
+ done
+done &&
+
+# LINT: inner loop handles failure explicitly with "|| return 1"
+for i in 0 1 2 3 4 5 6 7 8 9;
+do
+ for j in 0 1 2 3 4 5 6 7 8 9;
+ do
+ echo "$i$j" >"path$i$j" || return 1
+ done
+done &&
+
+# LINT: outer loop handles failure explicitly with "|| return 1"
+for i in 0 1 2 3 4 5 6 7 8 9;
+do
+ for j in 0 1 2 3 4 5 6 7 8 9;
+ do
+ echo "$i$j" >"path$i$j"
+ done || return 1
+done &&
+
+# LINT: inner & outer loops handles failure explicitly with "|| return 1"
+for i in 0 1 2 3 4 5 6 7 8 9;
+do
+ for j in 0 1 2 3 4 5 6 7 8 9;
+ do
+ echo "$i$j" >"path$i$j" || return 1
+ done || return 1
+done
diff --git a/t/chainlint/nested-subshell-comment.expect b/t/chainlint/nested-subshell-comment.expect
new file mode 100644
index 0000000..9138cf3
--- /dev/null
+++ b/t/chainlint/nested-subshell-comment.expect
@@ -0,0 +1,11 @@
+(
+ foo &&
+ (
+ bar &&
+ # bottles wobble while fiddles gobble
+ # minor numbers of cows (or do they?)
+ baz &&
+ snaff
+ ) ?!AMP?!
+ fuzzy
+)
diff --git a/t/chainlint/nested-subshell-comment.test b/t/chainlint/nested-subshell-comment.test
new file mode 100644
index 0000000..0215cdb
--- /dev/null
+++ b/t/chainlint/nested-subshell-comment.test
@@ -0,0 +1,13 @@
+(
+ foo &&
+ (
+ bar &&
+# LINT: ")" in comment in nested subshell not misinterpreted as closing ")"
+ # bottles wobble while fiddles gobble
+ # minor numbers of cows (or do they?)
+ baz &&
+ snaff
+# LINT: missing "&&" on ")"
+ )
+ fuzzy
+)
diff --git a/t/chainlint/nested-subshell.expect b/t/chainlint/nested-subshell.expect
new file mode 100644
index 0000000..02e0a9f
--- /dev/null
+++ b/t/chainlint/nested-subshell.expect
@@ -0,0 +1,12 @@
+(
+ cd foo &&
+ (
+ echo a &&
+ echo b
+ ) >file &&
+ cd foo &&
+ (
+ echo a ?!AMP?!
+ echo b
+ ) >file
+)
diff --git a/t/chainlint/nested-subshell.test b/t/chainlint/nested-subshell.test
new file mode 100644
index 0000000..440ee99
--- /dev/null
+++ b/t/chainlint/nested-subshell.test
@@ -0,0 +1,13 @@
+(
+ cd foo &&
+ (
+ echo a &&
+ echo b
+ ) >file &&
+
+ cd foo &&
+ (
+ echo a
+ echo b
+ ) >file
+)
diff --git a/t/chainlint/not-heredoc.expect b/t/chainlint/not-heredoc.expect
new file mode 100644
index 0000000..2e9bb13
--- /dev/null
+++ b/t/chainlint/not-heredoc.expect
@@ -0,0 +1,14 @@
+echo "<<<<<<< ours" &&
+echo ourside &&
+echo "=======" &&
+echo theirside &&
+echo ">>>>>>> theirs" &&
+
+(
+ echo "<<<<<<< ours" &&
+ echo ourside &&
+ echo "=======" &&
+ echo theirside &&
+ echo ">>>>>>> theirs" ?!AMP?!
+ poodle
+) >merged
diff --git a/t/chainlint/not-heredoc.test b/t/chainlint/not-heredoc.test
new file mode 100644
index 0000000..9aa5734
--- /dev/null
+++ b/t/chainlint/not-heredoc.test
@@ -0,0 +1,16 @@
+# LINT: "<< ours" inside string is not here-doc
+echo "<<<<<<< ours" &&
+echo ourside &&
+echo "=======" &&
+echo theirside &&
+echo ">>>>>>> theirs" &&
+
+(
+# LINT: "<< ours" inside string is not here-doc
+ echo "<<<<<<< ours" &&
+ echo ourside &&
+ echo "=======" &&
+ echo theirside &&
+ echo ">>>>>>> theirs"
+ poodle
+) >merged
diff --git a/t/chainlint/one-liner-for-loop.expect b/t/chainlint/one-liner-for-loop.expect
new file mode 100644
index 0000000..51a3dc7
--- /dev/null
+++ b/t/chainlint/one-liner-for-loop.expect
@@ -0,0 +1,9 @@
+git init dir-rename-and-content &&
+(
+ cd dir-rename-and-content &&
+ test_write_lines 1 2 3 4 5 >foo &&
+ mkdir olddir &&
+ for i in a b c; do echo $i >olddir/$i; ?!LOOP?! done ?!AMP?!
+ git add foo olddir &&
+ git commit -m "original" &&
+)
diff --git a/t/chainlint/one-liner-for-loop.test b/t/chainlint/one-liner-for-loop.test
new file mode 100644
index 0000000..4bd8c06
--- /dev/null
+++ b/t/chainlint/one-liner-for-loop.test
@@ -0,0 +1,10 @@
+git init dir-rename-and-content &&
+(
+ cd dir-rename-and-content &&
+ test_write_lines 1 2 3 4 5 >foo &&
+ mkdir olddir &&
+# LINT: one-liner for-loop missing "|| exit"; also broken &&-chain
+ for i in a b c; do echo $i >olddir/$i; done
+ git add foo olddir &&
+ git commit -m "original" &&
+)
diff --git a/t/chainlint/one-liner.expect b/t/chainlint/one-liner.expect
new file mode 100644
index 0000000..57a7a44
--- /dev/null
+++ b/t/chainlint/one-liner.expect
@@ -0,0 +1,9 @@
+(foo && bar) &&
+(foo && bar) |
+(foo && bar) >baz &&
+
+(foo; ?!AMP?! bar) &&
+(foo; ?!AMP?! bar) |
+(foo; ?!AMP?! bar) >baz &&
+
+(foo "bar; baz")
diff --git a/t/chainlint/one-liner.test b/t/chainlint/one-liner.test
new file mode 100644
index 0000000..be9858f
--- /dev/null
+++ b/t/chainlint/one-liner.test
@@ -0,0 +1,12 @@
+# LINT: top-level one-liner subshell
+(foo && bar) &&
+(foo && bar) |
+(foo && bar) >baz &&
+
+# LINT: top-level one-liner subshell missing internal "&&" and broken &&-chain
+(foo; bar) &&
+(foo; bar) |
+(foo; bar) >baz &&
+
+# LINT: ";" in string not misinterpreted as broken &&-chain
+(foo "bar; baz")
diff --git a/t/chainlint/p4-filespec.expect b/t/chainlint/p4-filespec.expect
new file mode 100644
index 0000000..1290fd1
--- /dev/null
+++ b/t/chainlint/p4-filespec.expect
@@ -0,0 +1,4 @@
+(
+ p4 print -1 //depot/fiddle#42 >file &&
+ foobar
+)
diff --git a/t/chainlint/p4-filespec.test b/t/chainlint/p4-filespec.test
new file mode 100644
index 0000000..4fd2d6e
--- /dev/null
+++ b/t/chainlint/p4-filespec.test
@@ -0,0 +1,5 @@
+(
+# LINT: Perforce revspec in filespec not misinterpreted as in-line comment
+ p4 print -1 //depot/fiddle#42 >file &&
+ foobar
+)
diff --git a/t/chainlint/pipe.expect b/t/chainlint/pipe.expect
new file mode 100644
index 0000000..2cfc028
--- /dev/null
+++ b/t/chainlint/pipe.expect
@@ -0,0 +1,8 @@
+(
+ foo |
+ bar |
+ baz &&
+ fish |
+ cow ?!AMP?!
+ sunder
+)
diff --git a/t/chainlint/pipe.test b/t/chainlint/pipe.test
new file mode 100644
index 0000000..dd82534
--- /dev/null
+++ b/t/chainlint/pipe.test
@@ -0,0 +1,12 @@
+(
+# LINT: no "&&" needed on line ending with "|"
+ foo |
+ bar |
+ baz &&
+
+# LINT: final line of pipe sequence ("cow") lacking "&&"
+ fish |
+ cow
+
+ sunder
+)
diff --git a/t/chainlint/return-loop.expect b/t/chainlint/return-loop.expect
new file mode 100644
index 0000000..cfc0549
--- /dev/null
+++ b/t/chainlint/return-loop.expect
@@ -0,0 +1,5 @@
+while test $i -lt $((num - 5))
+do
+ git notes add -m "notes for commit$i" HEAD~$i || return 1
+ i=$((i + 1))
+done
diff --git a/t/chainlint/return-loop.test b/t/chainlint/return-loop.test
new file mode 100644
index 0000000..f90b171
--- /dev/null
+++ b/t/chainlint/return-loop.test
@@ -0,0 +1,6 @@
+while test $i -lt $((num - 5))
+do
+# LINT: "|| return {n}" valid loop escape outside subshell; no "&&" needed
+ git notes add -m "notes for commit$i" HEAD~$i || return 1
+ i=$((i + 1))
+done
diff --git a/t/chainlint/semicolon.expect b/t/chainlint/semicolon.expect
new file mode 100644
index 0000000..3aa2259
--- /dev/null
+++ b/t/chainlint/semicolon.expect
@@ -0,0 +1,19 @@
+(
+ cat foo ; ?!AMP?! echo bar ?!AMP?!
+ cat foo ; ?!AMP?! echo bar
+) &&
+(
+ cat foo ; ?!AMP?! echo bar &&
+ cat foo ; ?!AMP?! echo bar
+) &&
+(
+ echo "foo; bar" &&
+ cat foo; ?!AMP?! echo bar
+) &&
+(
+ foo;
+) &&
+(cd foo &&
+ for i in a b c; do
+ echo; ?!LOOP?!
+ done)
diff --git a/t/chainlint/semicolon.test b/t/chainlint/semicolon.test
new file mode 100644
index 0000000..67e1192
--- /dev/null
+++ b/t/chainlint/semicolon.test
@@ -0,0 +1,25 @@
+(
+# LINT: missing internal "&&" and ending "&&"
+ cat foo ; echo bar
+# LINT: final statement before ")" only missing internal "&&"
+ cat foo ; echo bar
+) &&
+(
+# LINT: missing internal "&&"
+ cat foo ; echo bar &&
+ cat foo ; echo bar
+) &&
+(
+# LINT: not fooled by semicolon in string
+ echo "foo; bar" &&
+ cat foo; echo bar
+) &&
+(
+# LINT: semicolon unnecessary but legitimate
+ foo;
+) &&
+(cd foo &&
+ for i in a b c; do
+# LINT: semicolon unnecessary but legitimate
+ echo;
+ done)
diff --git a/t/chainlint/sqstring-in-sqstring.expect b/t/chainlint/sqstring-in-sqstring.expect
new file mode 100644
index 0000000..cf0b591
--- /dev/null
+++ b/t/chainlint/sqstring-in-sqstring.expect
@@ -0,0 +1,4 @@
+perl -e '
+ defined($_ = -s $_) or die for @ARGV;
+ exit 1 if $ARGV[0] <= $ARGV[1];
+' test-2-$packname_2.pack test-3-$packname_3.pack
diff --git a/t/chainlint/sqstring-in-sqstring.test b/t/chainlint/sqstring-in-sqstring.test
new file mode 100644
index 0000000..77a425e
--- /dev/null
+++ b/t/chainlint/sqstring-in-sqstring.test
@@ -0,0 +1,5 @@
+# LINT: SQ-string Perl code fragment within SQ-string
+perl -e '\''
+ defined($_ = -s $_) or die for @ARGV;
+ exit 1 if $ARGV[0] <= $ARGV[1];
+'\'' test-2-$packname_2.pack test-3-$packname_3.pack
diff --git a/t/chainlint/subshell-here-doc.expect b/t/chainlint/subshell-here-doc.expect
new file mode 100644
index 0000000..5278927
--- /dev/null
+++ b/t/chainlint/subshell-here-doc.expect
@@ -0,0 +1,30 @@
+(
+ echo wobba \
+ gorgo snoot \
+ wafta snurb <<-EOF &&
+ quoth the raven,
+ nevermore...
+ EOF
+
+ cat <<EOF >bip ?!AMP?!
+ fish fly high
+EOF
+
+ echo <<-\EOF >bop
+ gomez
+ morticia
+ wednesday
+ pugsly
+ EOF
+) &&
+(
+ cat <<-\ARBITRARY >bup &&
+ glink
+ FIZZ
+ ARBITRARY
+ cat <<-"ARBITRARY3" >bup3 &&
+ glink
+ FIZZ
+ ARBITRARY3
+ meep
+)
diff --git a/t/chainlint/subshell-here-doc.test b/t/chainlint/subshell-here-doc.test
new file mode 100644
index 0000000..d40eb65
--- /dev/null
+++ b/t/chainlint/subshell-here-doc.test
@@ -0,0 +1,35 @@
+(
+# LINT: stitch together incomplete \-ending lines
+# LINT: swallow here-doc to avoid false positives in content
+ echo wobba \
+ gorgo snoot \
+ wafta snurb <<-EOF &&
+ quoth the raven,
+ nevermore...
+ EOF
+
+# LINT: missing "&&" on "cat"
+ cat <<EOF >bip
+ fish fly high
+EOF
+
+# LINT: swallow here-doc (EOF is last line of subshell)
+ echo <<-\EOF >bop
+ gomez
+ morticia
+ wednesday
+ pugsly
+ EOF
+) &&
+(
+# LINT: swallow here-doc with arbitrary tag
+ cat <<-\ARBITRARY >bup &&
+ glink
+ FIZZ
+ ARBITRARY
+ cat <<-"ARBITRARY3" >bup3 &&
+ glink
+ FIZZ
+ ARBITRARY3
+ meep
+)
diff --git a/t/chainlint/subshell-one-liner.expect b/t/chainlint/subshell-one-liner.expect
new file mode 100644
index 0000000..b701536
--- /dev/null
+++ b/t/chainlint/subshell-one-liner.expect
@@ -0,0 +1,14 @@
+(
+ (foo && bar) &&
+ (foo && bar) |
+ (foo && bar) >baz &&
+ (foo; ?!AMP?! bar) &&
+ (foo; ?!AMP?! bar) |
+ (foo; ?!AMP?! bar) >baz &&
+ (foo || exit 1) &&
+ (foo || exit 1) |
+ (foo || exit 1) >baz &&
+ (foo && bar) ?!AMP?!
+ (foo && bar; ?!AMP?! baz) ?!AMP?!
+ foobar
+)
diff --git a/t/chainlint/subshell-one-liner.test b/t/chainlint/subshell-one-liner.test
new file mode 100644
index 0000000..37fa643
--- /dev/null
+++ b/t/chainlint/subshell-one-liner.test
@@ -0,0 +1,24 @@
+(
+# LINT: nested one-liner subshell
+ (foo && bar) &&
+ (foo && bar) |
+ (foo && bar) >baz &&
+
+# LINT: nested one-liner subshell missing internal "&&"
+ (foo; bar) &&
+ (foo; bar) |
+ (foo; bar) >baz &&
+
+# LINT: nested one-liner subshell with "|| exit"
+ (foo || exit 1) &&
+ (foo || exit 1) |
+ (foo || exit 1) >baz &&
+
+# LINT: nested one-liner subshell lacking ending "&&"
+ (foo && bar)
+
+# LINT: nested one-liner subshell missing internal "&&" and lacking ending "&&"
+ (foo && bar; baz)
+
+ foobar
+)
diff --git a/t/chainlint/t7900-subtree.expect b/t/chainlint/t7900-subtree.expect
new file mode 100644
index 0000000..71b3b3b
--- /dev/null
+++ b/t/chainlint/t7900-subtree.expect
@@ -0,0 +1,21 @@
+(
+ chks="sub1
+sub2
+sub3
+sub4" &&
+ chks_sub=$(cat <<TXT | sed "s,^,sub dir/,"
+$chks
+TXT
+) &&
+ chkms="main-sub1
+main-sub2
+main-sub3
+main-sub4" &&
+ chkms_sub=$(cat <<TXT | sed "s,^,sub dir/,"
+$chkms
+TXT
+) &&
+ subfiles=$(git ls-files) &&
+ check_equal "$subfiles" "$chkms
+$chks"
+)
diff --git a/t/chainlint/t7900-subtree.test b/t/chainlint/t7900-subtree.test
new file mode 100644
index 0000000..02f3129
--- /dev/null
+++ b/t/chainlint/t7900-subtree.test
@@ -0,0 +1,22 @@
+(
+ chks="sub1
+sub2
+sub3
+sub4" &&
+ chks_sub=$(cat <<TXT | sed "s,^,sub dir/,"
+$chks
+TXT
+) &&
+ chkms="main-sub1
+main-sub2
+main-sub3
+main-sub4" &&
+ chkms_sub=$(cat <<TXT | sed "s,^,sub dir/,"
+$chkms
+TXT
+) &&
+
+ subfiles=$(git ls-files) &&
+ check_equal "$subfiles" "$chkms
+$chks"
+)
diff --git a/t/chainlint/token-pasting.expect b/t/chainlint/token-pasting.expect
new file mode 100644
index 0000000..342360b
--- /dev/null
+++ b/t/chainlint/token-pasting.expect
@@ -0,0 +1,27 @@
+git config filter.rot13.smudge ./rot13.sh &&
+git config filter.rot13.clean ./rot13.sh &&
+
+{
+ echo "*.t filter=rot13" ?!AMP?!
+ echo "*.i ident"
+} > .gitattributes &&
+
+{
+ echo a b c d e f g h i j k l m ?!AMP?!
+ echo n o p q r s t u v w x y z ?!AMP?!
+ echo '$Id$'
+} > test &&
+cat test > test.t &&
+cat test > test.o &&
+cat test > test.i &&
+git add test test.t test.i &&
+rm -f test test.t test.i &&
+git checkout -- test test.t test.i &&
+
+echo "content-test2" > test2.o &&
+echo "content-test3 - filename with special characters" > "test3 'sq',$x=.o" ?!AMP?!
+
+downstream_url_for_sed=$(
+ printf "%sn" "$downstream_url" |
+ sed -e 's/\/\\/g' -e 's/[[/.*^$]/\&/g'
+)
diff --git a/t/chainlint/token-pasting.test b/t/chainlint/token-pasting.test
new file mode 100644
index 0000000..b4610ce
--- /dev/null
+++ b/t/chainlint/token-pasting.test
@@ -0,0 +1,32 @@
+# LINT: single token; composite of multiple strings
+git config filter.rot13.smudge ./rot13.sh &&
+git config filter.rot13.clean ./rot13.sh &&
+
+{
+ echo "*.t filter=rot13"
+ echo "*.i ident"
+} >.gitattributes &&
+
+{
+ echo a b c d e f g h i j k l m
+ echo n o p q r s t u v w x y z
+# LINT: exit/enter string context and escaped-quote outside of string
+ echo '\''$Id$'\''
+} >test &&
+cat test >test.t &&
+cat test >test.o &&
+cat test >test.i &&
+git add test test.t test.i &&
+rm -f test test.t test.i &&
+git checkout -- test test.t test.i &&
+
+echo "content-test2" >test2.o &&
+# LINT: exit/enter string context and escaped-quote outside of string
+echo "content-test3 - filename with special characters" >"test3 '\''sq'\'',\$x=.o"
+
+# LINT: single token; composite of multiple strings
+downstream_url_for_sed=$(
+ printf "%s\n" "$downstream_url" |
+# LINT: exit/enter string context; "&" inside string not command terminator
+ sed -e '\''s/\\/\\\\/g'\'' -e '\''s/[[/.*^$]/\\&/g'\''
+)
diff --git a/t/chainlint/unclosed-here-doc-indent.expect b/t/chainlint/unclosed-here-doc-indent.expect
new file mode 100644
index 0000000..7c30a1a
--- /dev/null
+++ b/t/chainlint/unclosed-here-doc-indent.expect
@@ -0,0 +1,4 @@
+command_which_is_run &&
+cat >expect <<-\EOF ?!UNCLOSED-HEREDOC?! &&
+we forget to end the here-doc
+command_which_is_gobbled
diff --git a/t/chainlint/unclosed-here-doc-indent.test b/t/chainlint/unclosed-here-doc-indent.test
new file mode 100644
index 0000000..5c841a9
--- /dev/null
+++ b/t/chainlint/unclosed-here-doc-indent.test
@@ -0,0 +1,4 @@
+command_which_is_run &&
+cat >expect <<-\EOF &&
+we forget to end the here-doc
+command_which_is_gobbled
diff --git a/t/chainlint/unclosed-here-doc.expect b/t/chainlint/unclosed-here-doc.expect
new file mode 100644
index 0000000..d65e50f
--- /dev/null
+++ b/t/chainlint/unclosed-here-doc.expect
@@ -0,0 +1,7 @@
+command_which_is_run &&
+cat >expect <<\EOF ?!UNCLOSED-HEREDOC?! &&
+ we try to end the here-doc below,
+ but the indentation throws us off
+ since the operator is not "<<-".
+ EOF
+command_which_is_gobbled
diff --git a/t/chainlint/unclosed-here-doc.test b/t/chainlint/unclosed-here-doc.test
new file mode 100644
index 0000000..69d3786
--- /dev/null
+++ b/t/chainlint/unclosed-here-doc.test
@@ -0,0 +1,7 @@
+command_which_is_run &&
+cat >expect <<\EOF &&
+ we try to end the here-doc below,
+ but the indentation throws us off
+ since the operator is not "<<-".
+ EOF
+command_which_is_gobbled
diff --git a/t/chainlint/while-loop.expect b/t/chainlint/while-loop.expect
new file mode 100644
index 0000000..1f5eaea
--- /dev/null
+++ b/t/chainlint/while-loop.expect
@@ -0,0 +1,13 @@
+(
+ while true
+ do
+ echo foo ?!AMP?!
+ cat <<-\EOF ?!LOOP?!
+ bar
+ EOF
+ done ?!AMP?!
+ while true; do
+ echo foo &&
+ cat bar ?!LOOP?!
+ done
+)
diff --git a/t/chainlint/while-loop.test b/t/chainlint/while-loop.test
new file mode 100644
index 0000000..d09fb01
--- /dev/null
+++ b/t/chainlint/while-loop.test
@@ -0,0 +1,19 @@
+(
+# LINT: "while", "do", "done" do not need "&&"
+ while true
+ do
+# LINT: missing "&&" on "echo"
+ echo foo
+# LINT: last statement of while does not need "&&"
+ cat <<-\EOF
+ bar
+ EOF
+# LINT: missing "&&" on "done"
+ done
+
+# LINT: "do" on same line as "while"
+ while true; do
+ echo foo &&
+ cat bar
+ done
+)
diff --git a/t/check-non-portable-shell.pl b/t/check-non-portable-shell.pl
new file mode 100755
index 0000000..dd8107c
--- /dev/null
+++ b/t/check-non-portable-shell.pl
@@ -0,0 +1,56 @@
+#!/usr/bin/perl
+
+# Test t0000..t9999.sh for non portable shell scripts
+# This script can be called with one or more filenames as parameters
+
+use strict;
+use warnings;
+
+my $exit_code=0;
+my %func;
+
+sub err {
+ my $msg = shift;
+ s/^\s+//;
+ s/\s+$//;
+ s/\s+/ /g;
+ print "$ARGV:$.: error: $msg: $_\n";
+ $exit_code = 1;
+}
+
+# glean names of shell functions
+for my $i (@ARGV) {
+ open(my $f, '<', $i) or die "$0: $i: $!\n";
+ while (<$f>) {
+ $func{$1} = 1 if /^\s*(\w+)\s*\(\)\s*{\s*$/;
+ }
+ close $f;
+}
+
+my $line = '';
+while (<>) {
+ chomp;
+ $line .= $_;
+ # stitch together incomplete lines (those ending with "\")
+ next if $line =~ s/\\$//;
+
+ $_ = $line;
+ /\bcp\s+-a/ and err 'cp -a is not portable';
+ /\bsed\s+-[^efn]\s+/ and err 'sed option not portable (use only -n, -e, -f)';
+ /\becho\s+-[neE]/ and err 'echo with option is not portable (use printf)';
+ /^\s*declare\s+/ and err 'arrays/declare not portable';
+ /^\s*[^#]\s*which\s/ and err 'which is not portable (use type)';
+ /\btest\s+[^=]*==/ and err '"test a == b" is not portable (use =)';
+ /\bwc -l.*"\s*=/ and err '`"$(wc -l)"` is not portable (use test_line_count)';
+ /\bhead\s+-c\b/ and err 'head -c is not portable (use test_copy_bytes BYTES <file >out)';
+ /(?:\$\(seq|^\s*seq\b)/ and err 'seq is not portable (use test_seq)';
+ /\bgrep\b.*--file\b/ and err 'grep --file FILE is not portable (use grep -f FILE)';
+ /\b[ef]grep\b/ and err 'egrep/fgrep obsolescent (use grep -E/-F)';
+ /\bexport\s+[A-Za-z0-9_]*=/ and err '"export FOO=bar" is not portable (use FOO=bar && export FOO)';
+ /^\s*([A-Z0-9_]+=(\w*|(["']).*?\3)\s+)+(\w+)/ and exists($func{$4}) and
+ err '"FOO=bar shell_func" assignment extends beyond "shell_func"';
+ $line = '';
+ # this resets our $. for each file
+ close ARGV if eof;
+}
+exit $exit_code;
diff --git a/t/helper/.gitignore b/t/helper/.gitignore
new file mode 100644
index 0000000..8c2ddcc
--- /dev/null
+++ b/t/helper/.gitignore
@@ -0,0 +1,2 @@
+/test-tool
+/test-fake-ssh
diff --git a/t/helper/test-advise.c b/t/helper/test-advise.c
new file mode 100644
index 0000000..8a3fd00
--- /dev/null
+++ b/t/helper/test-advise.c
@@ -0,0 +1,22 @@
+#include "test-tool.h"
+#include "advice.h"
+#include "config.h"
+#include "setup.h"
+
+int cmd__advise_if_enabled(int argc, const char **argv)
+{
+ if (argc != 2)
+ die("usage: %s <advice>", argv[0]);
+
+ setup_git_directory();
+ git_config(git_default_config, NULL);
+
+ /*
+ * Any advice type can be used for testing, but NESTED_TAG was
+ * selected here and in t0018 where this command is being
+ * executed.
+ */
+ advise_if_enabled(ADVICE_NESTED_TAG, "%s", argv[1]);
+
+ return 0;
+}
diff --git a/t/helper/test-bitmap.c b/t/helper/test-bitmap.c
new file mode 100644
index 0000000..af43ee1
--- /dev/null
+++ b/t/helper/test-bitmap.c
@@ -0,0 +1,33 @@
+#include "test-tool.h"
+#include "git-compat-util.h"
+#include "pack-bitmap.h"
+#include "setup.h"
+
+static int bitmap_list_commits(void)
+{
+ return test_bitmap_commits(the_repository);
+}
+
+static int bitmap_dump_hashes(void)
+{
+ return test_bitmap_hashes(the_repository);
+}
+
+int cmd__bitmap(int argc, const char **argv)
+{
+ setup_git_directory();
+
+ if (argc != 2)
+ goto usage;
+
+ if (!strcmp(argv[1], "list-commits"))
+ return bitmap_list_commits();
+ if (!strcmp(argv[1], "dump-hashes"))
+ return bitmap_dump_hashes();
+
+usage:
+ usage("\ttest-tool bitmap list-commits\n"
+ "\ttest-tool bitmap dump-hashes");
+
+ return -1;
+}
diff --git a/t/helper/test-bloom.c b/t/helper/test-bloom.c
new file mode 100644
index 0000000..1281e66
--- /dev/null
+++ b/t/helper/test-bloom.c
@@ -0,0 +1,100 @@
+#include "test-tool.h"
+#include "bloom.h"
+#include "hex.h"
+#include "commit.h"
+#include "repository.h"
+#include "setup.h"
+
+static struct bloom_filter_settings settings = DEFAULT_BLOOM_FILTER_SETTINGS;
+
+static void add_string_to_filter(const char *data, struct bloom_filter *filter) {
+ struct bloom_key key;
+ int i;
+
+ fill_bloom_key(data, strlen(data), &key, &settings);
+ printf("Hashes:");
+ for (i = 0; i < settings.num_hashes; i++){
+ printf("0x%08x|", key.hashes[i]);
+ }
+ printf("\n");
+ add_key_to_filter(&key, filter, &settings);
+ clear_bloom_key(&key);
+}
+
+static void print_bloom_filter(struct bloom_filter *filter) {
+ int i;
+
+ if (!filter) {
+ printf("No filter.\n");
+ return;
+ }
+ printf("Filter_Length:%d\n", (int)filter->len);
+ printf("Filter_Data:");
+ for (i = 0; i < filter->len; i++) {
+ printf("%02x|", filter->data[i]);
+ }
+ printf("\n");
+}
+
+static void get_bloom_filter_for_commit(const struct object_id *commit_oid)
+{
+ struct commit *c;
+ struct bloom_filter *filter;
+ c = lookup_commit(the_repository, commit_oid);
+ filter = get_or_compute_bloom_filter(the_repository, c, 1,
+ &settings,
+ NULL);
+ print_bloom_filter(filter);
+}
+
+static const char *bloom_usage = "\n"
+" test-tool bloom get_murmur3 <string>\n"
+" test-tool bloom generate_filter <string> [<string>...]\n"
+" test-tool bloom get_filter_for_commit <commit-hex>\n";
+
+int cmd__bloom(int argc, const char **argv)
+{
+ setup_git_directory();
+
+ if (argc < 2)
+ usage(bloom_usage);
+
+ if (!strcmp(argv[1], "get_murmur3")) {
+ uint32_t hashed;
+ if (argc < 3)
+ usage(bloom_usage);
+ hashed = murmur3_seeded(0, argv[2], strlen(argv[2]));
+ printf("Murmur3 Hash with seed=0:0x%08x\n", hashed);
+ }
+
+ if (!strcmp(argv[1], "generate_filter")) {
+ struct bloom_filter filter;
+ int i = 2;
+ filter.len = (settings.bits_per_entry + BITS_PER_WORD - 1) / BITS_PER_WORD;
+ CALLOC_ARRAY(filter.data, filter.len);
+
+ if (argc - 1 < i)
+ usage(bloom_usage);
+
+ while (argv[i]) {
+ add_string_to_filter(argv[i], &filter);
+ i++;
+ }
+
+ print_bloom_filter(&filter);
+ free(filter.data);
+ }
+
+ if (!strcmp(argv[1], "get_filter_for_commit")) {
+ struct object_id oid;
+ const char *end;
+ if (argc < 3)
+ usage(bloom_usage);
+ if (parse_oid_hex(argv[2], &oid, &end))
+ die("cannot parse oid '%s'", argv[2]);
+ init_bloom_filters();
+ get_bloom_filter_for_commit(&oid);
+ }
+
+ return 0;
+}
diff --git a/t/helper/test-bundle-uri.c b/t/helper/test-bundle-uri.c
new file mode 100644
index 0000000..4750585
--- /dev/null
+++ b/t/helper/test-bundle-uri.c
@@ -0,0 +1,137 @@
+#include "test-tool.h"
+#include "parse-options.h"
+#include "bundle-uri.h"
+#include "gettext.h"
+#include "strbuf.h"
+#include "string-list.h"
+#include "transport.h"
+#include "ref-filter.h"
+#include "remote.h"
+#include "refs.h"
+
+enum input_mode {
+ KEY_VALUE_PAIRS,
+ CONFIG_FILE,
+};
+
+static int cmd__bundle_uri_parse(int argc, const char **argv, enum input_mode mode)
+{
+ const char *key_value_usage[] = {
+ "test-tool bundle-uri parse-key-values <input>",
+ NULL
+ };
+ const char *config_usage[] = {
+ "test-tool bundle-uri parse-config <input>",
+ NULL
+ };
+ const char **usage = key_value_usage;
+ struct option options[] = {
+ OPT_END(),
+ };
+ struct strbuf sb = STRBUF_INIT;
+ struct bundle_list list;
+ int err = 0;
+ FILE *fp;
+
+ if (mode == CONFIG_FILE)
+ usage = config_usage;
+
+ argc = parse_options(argc, argv, NULL, options, usage,
+ PARSE_OPT_STOP_AT_NON_OPTION);
+
+ init_bundle_list(&list);
+
+ list.baseURI = xstrdup("<uri>");
+
+ switch (mode) {
+ case KEY_VALUE_PAIRS:
+ if (argc != 1)
+ goto usage;
+ fp = fopen(argv[0], "r");
+ if (!fp)
+ die("failed to open '%s'", argv[0]);
+ while (strbuf_getline(&sb, fp) != EOF) {
+ if (bundle_uri_parse_line(&list, sb.buf))
+ err = error("bad line: '%s'", sb.buf);
+ }
+ fclose(fp);
+ break;
+
+ case CONFIG_FILE:
+ if (argc != 1)
+ goto usage;
+ err = bundle_uri_parse_config_format("<uri>", argv[0], &list);
+ break;
+ }
+ strbuf_release(&sb);
+
+ print_bundle_list(stdout, &list);
+
+ clear_bundle_list(&list);
+
+ return !!err;
+
+usage:
+ usage_with_options(usage, options);
+}
+
+static int cmd_ls_remote(int argc, const char **argv)
+{
+ const char *dest;
+ struct remote *remote;
+ struct transport *transport;
+ int status = 0;
+
+ dest = argc > 1 ? argv[1] : NULL;
+
+ remote = remote_get(dest);
+ if (!remote) {
+ if (dest)
+ die(_("bad repository '%s'"), dest);
+ die(_("no remote configured to get bundle URIs from"));
+ }
+ if (!remote->url_nr)
+ die(_("remote '%s' has no configured URL"), dest);
+
+ transport = transport_get(remote, NULL);
+ if (transport_get_remote_bundle_uri(transport) < 0) {
+ error(_("could not get the bundle-uri list"));
+ status = 1;
+ goto cleanup;
+ }
+
+ print_bundle_list(stdout, transport->bundles);
+
+cleanup:
+ if (transport_disconnect(transport))
+ return 1;
+ return status;
+}
+
+int cmd__bundle_uri(int argc, const char **argv)
+{
+ const char *usage[] = {
+ "test-tool bundle-uri <subcommand> [<options>]",
+ NULL
+ };
+ struct option options[] = {
+ OPT_END(),
+ };
+
+ argc = parse_options(argc, argv, NULL, options, usage,
+ PARSE_OPT_STOP_AT_NON_OPTION |
+ PARSE_OPT_KEEP_ARGV0);
+ if (argc == 1)
+ goto usage;
+
+ if (!strcmp(argv[1], "parse-key-values"))
+ return cmd__bundle_uri_parse(argc - 1, argv + 1, KEY_VALUE_PAIRS);
+ if (!strcmp(argv[1], "parse-config"))
+ return cmd__bundle_uri_parse(argc - 1, argv + 1, CONFIG_FILE);
+ if (!strcmp(argv[1], "ls-remote"))
+ return cmd_ls_remote(argc - 1, argv + 1);
+ error("there is no test-tool bundle-uri tool '%s'", argv[1]);
+
+usage:
+ usage_with_options(usage, options);
+}
diff --git a/t/helper/test-cache-tree.c b/t/helper/test-cache-tree.c
new file mode 100644
index 0000000..e723639
--- /dev/null
+++ b/t/helper/test-cache-tree.c
@@ -0,0 +1,69 @@
+#define USE_THE_INDEX_VARIABLE
+#include "test-tool.h"
+#include "gettext.h"
+#include "hex.h"
+#include "tree.h"
+#include "cache-tree.h"
+#include "parse-options.h"
+#include "read-cache-ll.h"
+#include "repository.h"
+#include "setup.h"
+
+static char const * const test_cache_tree_usage[] = {
+ N_("test-tool cache-tree <options> (control|prime|update)"),
+ NULL
+};
+
+int cmd__cache_tree(int argc, const char **argv)
+{
+ struct object_id oid;
+ struct tree *tree;
+ int empty = 0;
+ int invalidate_qty = 0;
+ int i;
+
+ struct option options[] = {
+ OPT_BOOL(0, "empty", &empty,
+ N_("clear the cache tree before each iteration")),
+ OPT_INTEGER_F(0, "invalidate", &invalidate_qty,
+ N_("number of entries in the cache tree to invalidate (default 0)"),
+ PARSE_OPT_NONEG),
+ OPT_END()
+ };
+
+ setup_git_directory();
+
+ argc = parse_options(argc, argv, NULL, options, test_cache_tree_usage, 0);
+
+ if (repo_read_index(the_repository) < 0)
+ die(_("unable to read index file"));
+
+ oidcpy(&oid, &the_index.cache_tree->oid);
+ tree = parse_tree_indirect(&oid);
+ if (!tree)
+ die(_("not a tree object: %s"), oid_to_hex(&oid));
+
+ if (empty) {
+ /* clear the cache tree & allocate a new one */
+ cache_tree_free(&the_index.cache_tree);
+ the_index.cache_tree = cache_tree();
+ } else if (invalidate_qty) {
+ /* invalidate the specified number of unique paths */
+ float f_interval = (float)the_index.cache_nr / invalidate_qty;
+ int interval = f_interval < 1.0 ? 1 : (int)f_interval;
+ for (i = 0; i < invalidate_qty && i * interval < the_index.cache_nr; i++)
+ cache_tree_invalidate_path(&the_index, the_index.cache[i * interval]->name);
+ }
+
+ if (argc != 1)
+ usage_with_options(test_cache_tree_usage, options);
+ else if (!strcmp(argv[0], "prime"))
+ prime_cache_tree(the_repository, &the_index, tree);
+ else if (!strcmp(argv[0], "update"))
+ cache_tree_update(&the_index, WRITE_TREE_SILENT | WRITE_TREE_REPAIR);
+ /* use "control" subcommand to specify no-op */
+ else if (!!strcmp(argv[0], "control"))
+ die(_("Unhandled subcommand '%s'"), argv[0]);
+
+ return 0;
+}
diff --git a/t/helper/test-chmtime.c b/t/helper/test-chmtime.c
new file mode 100644
index 0000000..0e55388
--- /dev/null
+++ b/t/helper/test-chmtime.c
@@ -0,0 +1,163 @@
+/*
+ * This program can either change modification time of the given
+ * file(s) or just print it. The program does not change atime or
+ * ctime (their values are explicitly preserved).
+ *
+ * The mtime can be changed to an absolute value:
+ *
+ * test-tool chmtime =<seconds> file...
+ *
+ * Relative to the current time as returned by time(3):
+ *
+ * test-tool chmtime =+<seconds> (or =-<seconds>) file...
+ *
+ * Or relative to the current mtime of the file:
+ *
+ * test-tool chmtime <seconds> file...
+ * test-tool chmtime +<seconds> (or -<seconds>) file...
+ *
+ * Examples:
+ *
+ * To print the mtime and the file name use --verbose and set
+ * the file mtime offset to 0:
+ *
+ * test-tool chmtime -v +0 file
+ *
+ * To print only the mtime use --get:
+ *
+ * test-tool chmtime --get file
+ *
+ * To set the mtime to current time:
+ *
+ * test-tool chmtime =+0 file
+ *
+ * To set the file mtime offset to +1 and print the new value:
+ *
+ * test-tool chmtime --get +1 file
+ *
+ */
+#include "test-tool.h"
+#include "git-compat-util.h"
+#include <utime.h>
+
+static const char usage_str[] =
+ "(-v|--verbose|-g|--get) (+|=|=+|=-|-)<seconds> <file>...";
+
+static int timespec_arg(const char *arg, long int *set_time, int *set_eq)
+{
+ char *test;
+ const char *timespec = arg;
+ *set_eq = (*timespec == '=') ? 1 : 0;
+ if (*set_eq) {
+ timespec++;
+ if (*timespec == '+') {
+ *set_eq = 2; /* relative "in the future" */
+ timespec++;
+ }
+ }
+ *set_time = strtol(timespec, &test, 10);
+ if (*test) {
+ return 0;
+ }
+ if ((*set_eq && *set_time < 0) || *set_eq == 2) {
+ time_t now = time(NULL);
+ *set_time += now;
+ }
+ return 1;
+}
+
+int cmd__chmtime(int argc, const char **argv)
+{
+ static int verbose;
+ static int get;
+
+ int i = 1;
+ /* no mtime change by default */
+ int set_eq = 0;
+ long int set_time = 0;
+
+ if (argc < 3)
+ goto usage;
+
+ if (strcmp(argv[i], "--get") == 0 || strcmp(argv[i], "-g") == 0) {
+ get = 1;
+ ++i;
+ } else if (strcmp(argv[i], "--verbose") == 0 || strcmp(argv[i], "-v") == 0) {
+ verbose = 1;
+ ++i;
+ }
+
+ if (i == argc) {
+ goto usage;
+ }
+
+ if (timespec_arg(argv[i], &set_time, &set_eq)) {
+ ++i;
+ } else {
+ if (get == 0 && verbose == 0) {
+ fprintf(stderr, "Not a base-10 integer: %s\n", argv[i] + 1);
+ goto usage;
+ }
+ }
+
+ if (i == argc)
+ goto usage;
+
+ for (; i < argc; i++) {
+ struct stat sb;
+ struct utimbuf utb;
+ uintmax_t mtime;
+
+ if (stat(argv[i], &sb) < 0) {
+ fprintf(stderr, "Failed to stat %s: %s. Skipping\n",
+ argv[i], strerror(errno));
+ continue;
+ }
+
+#ifdef GIT_WINDOWS_NATIVE
+ if (!(sb.st_mode & S_IWUSR) &&
+ chmod(argv[i], sb.st_mode | S_IWUSR)) {
+ fprintf(stderr, "Could not make user-writable %s: %s",
+ argv[i], strerror(errno));
+ return 1;
+ }
+#endif
+
+ utb.actime = sb.st_atime;
+ utb.modtime = set_eq ? set_time : sb.st_mtime + set_time;
+
+ mtime = utb.modtime < 0 ? 0: utb.modtime;
+ if (get) {
+ printf("%"PRIuMAX"\n", mtime);
+ } else if (verbose) {
+ printf("%"PRIuMAX"\t%s\n", mtime, argv[i]);
+ }
+
+ if (utb.modtime != sb.st_mtime && utime(argv[i], &utb) < 0) {
+#ifdef GIT_WINDOWS_NATIVE
+ if (S_ISDIR(sb.st_mode)) {
+ /*
+ * NEEDSWORK: The Windows version of `utime()`
+ * (aka `mingw_utime()`) does not correctly
+ * handle directory arguments, since it uses
+ * `_wopen()`. Ignore it for now since this
+ * is just a test.
+ */
+ fprintf(stderr,
+ ("Failed to modify time on directory %s. "
+ "Skipping\n"), argv[i]);
+ continue;
+ }
+#endif
+ fprintf(stderr, "Failed to modify time on %s: %s\n",
+ argv[i], strerror(errno));
+ return 1;
+ }
+ }
+
+ return 0;
+
+usage:
+ fprintf(stderr, "usage: %s %s\n", argv[0], usage_str);
+ return 1;
+}
diff --git a/t/helper/test-config.c b/t/helper/test-config.c
new file mode 100644
index 0000000..ed444ca
--- /dev/null
+++ b/t/helper/test-config.c
@@ -0,0 +1,237 @@
+#include "test-tool.h"
+#include "config.h"
+#include "setup.h"
+#include "string-list.h"
+
+/*
+ * This program exposes the C API of the configuration mechanism
+ * as a set of simple commands in order to facilitate testing.
+ *
+ * Reads stdin and prints result of command to stdout:
+ *
+ * get_value -> prints the value with highest priority for the entered key
+ *
+ * get_value_multi -> prints all values for the entered key in increasing order
+ * of priority
+ *
+ * get -> print return value for the entered key
+ *
+ * get_int -> print integer value for the entered key or die
+ *
+ * get_bool -> print bool value for the entered key or die
+ *
+ * get_string -> print string value for the entered key or die
+ *
+ * configset_get_value -> returns value with the highest priority for the entered key
+ * from a config_set constructed from files entered as arguments.
+ *
+ * configset_get_value_multi -> returns value_list for the entered key sorted in
+ * ascending order of priority from a config_set
+ * constructed from files entered as arguments.
+ *
+ * iterate -> iterate over all values using git_config(), and print some
+ * data for each
+ *
+ * git_config_int -> iterate over all values using git_config() and print the
+ * integer value for the entered key or die
+ *
+ * Examples:
+ *
+ * To print the value with highest priority for key "foo.bAr Baz.rock":
+ * test-tool config get_value "foo.bAr Baz.rock"
+ *
+ */
+
+static int iterate_cb(const char *var, const char *value,
+ const struct config_context *ctx,
+ void *data UNUSED)
+{
+ const struct key_value_info *kvi = ctx->kvi;
+ static int nr;
+
+ if (nr++)
+ putchar('\n');
+
+ printf("key=%s\n", var);
+ printf("value=%s\n", value ? value : "(null)");
+ printf("origin=%s\n", config_origin_type_name(kvi->origin_type));
+ printf("name=%s\n", kvi->filename ? kvi->filename : "");
+ printf("lno=%d\n", kvi->linenr);
+ printf("scope=%s\n", config_scope_name(kvi->scope));
+
+ return 0;
+}
+
+static int parse_int_cb(const char *var, const char *value,
+ const struct config_context *ctx, void *data)
+{
+ const char *key_to_match = data;
+
+ if (!strcmp(key_to_match, var)) {
+ int parsed = git_config_int(value, value, ctx->kvi);
+ printf("%d\n", parsed);
+ }
+ return 0;
+}
+
+static int early_config_cb(const char *var, const char *value,
+ const struct config_context *ctx UNUSED,
+ void *vdata)
+{
+ const char *key = vdata;
+
+ if (!strcmp(key, var))
+ printf("%s\n", value);
+
+ return 0;
+}
+
+int cmd__config(int argc, const char **argv)
+{
+ int i, val;
+ const char *v;
+ const struct string_list *strptr;
+ struct config_set cs;
+
+ if (argc == 3 && !strcmp(argv[1], "read_early_config")) {
+ read_early_config(early_config_cb, (void *)argv[2]);
+ return 0;
+ }
+
+ setup_git_directory();
+
+ git_configset_init(&cs);
+
+ if (argc < 2) {
+ fprintf(stderr, "Please, provide a command name on the command-line\n");
+ goto exit1;
+ } else if (argc == 3 && !strcmp(argv[1], "get_value")) {
+ if (!git_config_get_value(argv[2], &v)) {
+ if (!v)
+ printf("(NULL)\n");
+ else
+ printf("%s\n", v);
+ goto exit0;
+ } else {
+ printf("Value not found for \"%s\"\n", argv[2]);
+ goto exit1;
+ }
+ } else if (argc == 3 && !strcmp(argv[1], "get_value_multi")) {
+ if (!git_config_get_value_multi(argv[2], &strptr)) {
+ for (i = 0; i < strptr->nr; i++) {
+ v = strptr->items[i].string;
+ if (!v)
+ printf("(NULL)\n");
+ else
+ printf("%s\n", v);
+ }
+ goto exit0;
+ } else {
+ printf("Value not found for \"%s\"\n", argv[2]);
+ goto exit1;
+ }
+ } else if (argc == 3 && !strcmp(argv[1], "get")) {
+ int ret;
+
+ if (!(ret = git_config_get(argv[2])))
+ goto exit0;
+ else if (ret == 1)
+ printf("Value not found for \"%s\"\n", argv[2]);
+ else if (ret == -CONFIG_INVALID_KEY)
+ printf("Key \"%s\" is invalid\n", argv[2]);
+ else if (ret == -CONFIG_NO_SECTION_OR_NAME)
+ printf("Key \"%s\" has no section\n", argv[2]);
+ else
+ /*
+ * A normal caller should just check "ret <
+ * 0", but for our own tests let's BUG() if
+ * our whitelist of git_config_parse_key()
+ * return values isn't exhaustive.
+ */
+ BUG("Key \"%s\" has unknown return %d", argv[2], ret);
+ goto exit1;
+ } else if (argc == 3 && !strcmp(argv[1], "get_int")) {
+ if (!git_config_get_int(argv[2], &val)) {
+ printf("%d\n", val);
+ goto exit0;
+ } else {
+ printf("Value not found for \"%s\"\n", argv[2]);
+ goto exit1;
+ }
+ } else if (argc == 3 && !strcmp(argv[1], "get_bool")) {
+ if (!git_config_get_bool(argv[2], &val)) {
+ printf("%d\n", val);
+ goto exit0;
+ } else {
+ printf("Value not found for \"%s\"\n", argv[2]);
+ goto exit1;
+ }
+ } else if (argc == 3 && !strcmp(argv[1], "get_string")) {
+ if (!git_config_get_string_tmp(argv[2], &v)) {
+ printf("%s\n", v);
+ goto exit0;
+ } else {
+ printf("Value not found for \"%s\"\n", argv[2]);
+ goto exit1;
+ }
+ } else if (!strcmp(argv[1], "configset_get_value")) {
+ for (i = 3; i < argc; i++) {
+ int err;
+ if ((err = git_configset_add_file(&cs, argv[i]))) {
+ fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
+ goto exit2;
+ }
+ }
+ if (!git_configset_get_value(&cs, argv[2], &v, NULL)) {
+ if (!v)
+ printf("(NULL)\n");
+ else
+ printf("%s\n", v);
+ goto exit0;
+ } else {
+ printf("Value not found for \"%s\"\n", argv[2]);
+ goto exit1;
+ }
+ } else if (!strcmp(argv[1], "configset_get_value_multi")) {
+ for (i = 3; i < argc; i++) {
+ int err;
+ if ((err = git_configset_add_file(&cs, argv[i]))) {
+ fprintf(stderr, "Error (%d) reading configuration file %s.\n", err, argv[i]);
+ goto exit2;
+ }
+ }
+ if (!git_configset_get_value_multi(&cs, argv[2], &strptr)) {
+ for (i = 0; i < strptr->nr; i++) {
+ v = strptr->items[i].string;
+ if (!v)
+ printf("(NULL)\n");
+ else
+ printf("%s\n", v);
+ }
+ goto exit0;
+ } else {
+ printf("Value not found for \"%s\"\n", argv[2]);
+ goto exit1;
+ }
+ } else if (!strcmp(argv[1], "iterate")) {
+ git_config(iterate_cb, NULL);
+ goto exit0;
+ } else if (argc == 3 && !strcmp(argv[1], "git_config_int")) {
+ git_config(parse_int_cb, (void *) argv[2]);
+ goto exit0;
+ }
+
+ die("%s: Please check the syntax and the function name", argv[0]);
+
+exit0:
+ git_configset_clear(&cs);
+ return 0;
+
+exit1:
+ git_configset_clear(&cs);
+ return 1;
+
+exit2:
+ git_configset_clear(&cs);
+ return 2;
+}
diff --git a/t/helper/test-crontab.c b/t/helper/test-crontab.c
new file mode 100644
index 0000000..597027a
--- /dev/null
+++ b/t/helper/test-crontab.c
@@ -0,0 +1,35 @@
+#include "test-tool.h"
+
+/*
+ * Usage: test-tool crontab <file> -l|<input>
+ *
+ * If -l is specified, then write the contents of <file> to stdout.
+ * Otherwise, copy the contents of <input> into <file>.
+ */
+int cmd__crontab(int argc, const char **argv)
+{
+ int a;
+ FILE *from, *to;
+
+ if (argc != 3)
+ usage("test-tool crontab <file> -l|<input>");
+
+ if (!strcmp(argv[2], "-l")) {
+ from = fopen(argv[1], "r");
+ if (!from)
+ return 0;
+ to = stdout;
+ } else {
+ from = xfopen(argv[2], "r");
+ to = xfopen(argv[1], "w");
+ }
+
+ while ((a = fgetc(from)) != EOF)
+ fputc(a, to);
+
+ fclose(from);
+ if (to != stdout)
+ fclose(to);
+
+ return 0;
+}
diff --git a/t/helper/test-csprng.c b/t/helper/test-csprng.c
new file mode 100644
index 0000000..65d1497
--- /dev/null
+++ b/t/helper/test-csprng.c
@@ -0,0 +1,29 @@
+#include "test-tool.h"
+#include "git-compat-util.h"
+
+
+int cmd__csprng(int argc, const char **argv)
+{
+ unsigned long count;
+ unsigned char buf[1024];
+
+ if (argc > 2) {
+ fprintf(stderr, "usage: %s [<size>]\n", argv[0]);
+ return 2;
+ }
+
+ count = (argc == 2) ? strtoul(argv[1], NULL, 0) : -1L;
+
+ while (count) {
+ unsigned long chunk = count < sizeof(buf) ? count : sizeof(buf);
+ if (csprng_bytes(buf, chunk) < 0) {
+ perror("failed to read");
+ return 5;
+ }
+ if (fwrite(buf, chunk, 1, stdout) != chunk)
+ return 1;
+ count -= chunk;
+ }
+
+ return 0;
+}
diff --git a/t/helper/test-ctype.c b/t/helper/test-ctype.c
new file mode 100644
index 0000000..e5659df
--- /dev/null
+++ b/t/helper/test-ctype.c
@@ -0,0 +1,70 @@
+#include "test-tool.h"
+
+static int rc;
+
+static void report_error(const char *class, int ch)
+{
+ printf("%s classifies char %d (0x%02x) wrongly\n", class, ch, ch);
+ rc = 1;
+}
+
+static int is_in(const char *s, int ch)
+{
+ /*
+ * We can't find NUL using strchr. Accept it as the first
+ * character in the spec -- there are no empty classes.
+ */
+ if (ch == '\0')
+ return ch == *s;
+ if (*s == '\0')
+ s++;
+ return !!strchr(s, ch);
+}
+
+#define TEST_CLASS(t,s) { \
+ int i; \
+ for (i = 0; i < 256; i++) { \
+ if (is_in(s, i) != t(i)) \
+ report_error(#t, i); \
+ } \
+ if (t(EOF)) \
+ report_error(#t, EOF); \
+}
+
+#define DIGIT "0123456789"
+#define LOWER "abcdefghijklmnopqrstuvwxyz"
+#define UPPER "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+#define PUNCT "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
+#define ASCII \
+ "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" \
+ "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" \
+ "\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f" \
+ "\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f" \
+ "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f" \
+ "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f" \
+ "\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f" \
+ "\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f"
+#define CNTRL \
+ "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" \
+ "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" \
+ "\x7f"
+
+int cmd__ctype(int argc UNUSED, const char **argv UNUSED)
+{
+ TEST_CLASS(isdigit, DIGIT);
+ TEST_CLASS(isspace, " \n\r\t");
+ TEST_CLASS(isalpha, LOWER UPPER);
+ TEST_CLASS(isalnum, LOWER UPPER DIGIT);
+ TEST_CLASS(is_glob_special, "*?[\\");
+ TEST_CLASS(is_regex_special, "$()*+.?[\\^{|");
+ TEST_CLASS(is_pathspec_magic, "!\"#%&',-/:;<=>@_`~");
+ TEST_CLASS(isascii, ASCII);
+ TEST_CLASS(islower, LOWER);
+ TEST_CLASS(isupper, UPPER);
+ TEST_CLASS(iscntrl, CNTRL);
+ TEST_CLASS(ispunct, PUNCT);
+ TEST_CLASS(isxdigit, DIGIT "abcdefABCDEF");
+ TEST_CLASS(isprint, LOWER UPPER DIGIT PUNCT " ");
+
+ return rc;
+}
diff --git a/t/helper/test-date.c b/t/helper/test-date.c
new file mode 100644
index 0000000..0683d46
--- /dev/null
+++ b/t/helper/test-date.c
@@ -0,0 +1,135 @@
+#include "test-tool.h"
+#include "date.h"
+#include "trace.h"
+
+static const char *usage_msg = "\n"
+" test-tool date relative [time_t]...\n"
+" test-tool date human [time_t]...\n"
+" test-tool date show:<format> [time_t]...\n"
+" test-tool date parse [date]...\n"
+" test-tool date approxidate [date]...\n"
+" test-tool date timestamp [date]...\n"
+" test-tool date getnanos [start-nanos]\n"
+" test-tool date is64bit\n"
+" test-tool date time_t-is64bit\n";
+
+static void show_relative_dates(const char **argv)
+{
+ struct strbuf buf = STRBUF_INIT;
+
+ for (; *argv; argv++) {
+ time_t t = atoi(*argv);
+ show_date_relative(t, &buf);
+ printf("%s -> %s\n", *argv, buf.buf);
+ }
+ strbuf_release(&buf);
+}
+
+static void show_human_dates(const char **argv)
+{
+ for (; *argv; argv++) {
+ time_t t = atoi(*argv);
+ printf("%s -> %s\n", *argv, show_date(t, 0, DATE_MODE(HUMAN)));
+ }
+}
+
+static void show_dates(const char **argv, const char *format)
+{
+ struct date_mode mode = DATE_MODE_INIT;
+
+ parse_date_format(format, &mode);
+ for (; *argv; argv++) {
+ char *arg;
+ timestamp_t t;
+ int tz;
+
+ /*
+ * Do not use our normal timestamp parsing here, as the point
+ * is to test the formatting code in isolation.
+ */
+ t = parse_timestamp(*argv, &arg, 10);
+ while (*arg == ' ')
+ arg++;
+ tz = atoi(arg);
+
+ printf("%s -> %s\n", *argv, show_date(t, tz, &mode));
+ }
+
+ date_mode_release(&mode);
+}
+
+static void parse_dates(const char **argv)
+{
+ struct strbuf result = STRBUF_INIT;
+
+ for (; *argv; argv++) {
+ timestamp_t t;
+ int tz;
+
+ strbuf_reset(&result);
+ parse_date(*argv, &result);
+ if (sscanf(result.buf, "%"PRItime" %d", &t, &tz) == 2)
+ printf("%s -> %s\n",
+ *argv, show_date(t, tz, DATE_MODE(ISO8601)));
+ else
+ printf("%s -> bad\n", *argv);
+ }
+ strbuf_release(&result);
+}
+
+static void parse_approxidate(const char **argv)
+{
+ for (; *argv; argv++) {
+ timestamp_t t;
+ t = approxidate(*argv);
+ printf("%s -> %s\n", *argv, show_date(t, 0, DATE_MODE(ISO8601)));
+ }
+}
+
+static void parse_approx_timestamp(const char **argv)
+{
+ for (; *argv; argv++) {
+ timestamp_t t;
+ t = approxidate(*argv);
+ printf("%s -> %"PRItime"\n", *argv, t);
+ }
+}
+
+static void getnanos(const char **argv)
+{
+ double seconds = getnanotime() / 1.0e9;
+
+ if (*argv)
+ seconds -= strtod(*argv, NULL);
+ printf("%lf\n", seconds);
+}
+
+int cmd__date(int argc UNUSED, const char **argv)
+{
+ const char *x;
+
+ argv++;
+ if (!*argv)
+ usage(usage_msg);
+ if (!strcmp(*argv, "relative"))
+ show_relative_dates(argv+1);
+ else if (!strcmp(*argv, "human"))
+ show_human_dates(argv+1);
+ else if (skip_prefix(*argv, "show:", &x))
+ show_dates(argv+1, x);
+ else if (!strcmp(*argv, "parse"))
+ parse_dates(argv+1);
+ else if (!strcmp(*argv, "approxidate"))
+ parse_approxidate(argv+1);
+ else if (!strcmp(*argv, "timestamp"))
+ parse_approx_timestamp(argv+1);
+ else if (!strcmp(*argv, "getnanos"))
+ getnanos(argv+1);
+ else if (!strcmp(*argv, "is64bit"))
+ return sizeof(timestamp_t) == 8 ? 0 : 1;
+ else if (!strcmp(*argv, "time_t-is64bit"))
+ return sizeof(time_t) == 8 ? 0 : 1;
+ else
+ usage(usage_msg);
+ return 0;
+}
diff --git a/t/helper/test-delta.c b/t/helper/test-delta.c
new file mode 100644
index 0000000..6bc787a
--- /dev/null
+++ b/t/helper/test-delta.c
@@ -0,0 +1,85 @@
+/*
+ * test-delta.c: test code to exercise diff-delta.c and patch-delta.c
+ *
+ * (C) 2005 Nicolas Pitre <nico@fluxnic.net>
+ *
+ * This code is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include "test-tool.h"
+#include "git-compat-util.h"
+#include "delta.h"
+
+static const char usage_str[] =
+ "test-tool delta (-d|-p) <from_file> <data_file> <out_file>";
+
+int cmd__delta(int argc, const char **argv)
+{
+ int fd;
+ struct stat st;
+ void *from_buf = NULL, *data_buf = NULL, *out_buf = NULL;
+ unsigned long from_size, data_size, out_size;
+ int ret = 1;
+
+ if (argc != 5 || (strcmp(argv[1], "-d") && strcmp(argv[1], "-p"))) {
+ fprintf(stderr, "usage: %s\n", usage_str);
+ return 1;
+ }
+
+ fd = open(argv[2], O_RDONLY);
+ if (fd < 0 || fstat(fd, &st)) {
+ perror(argv[2]);
+ return 1;
+ }
+ from_size = st.st_size;
+ from_buf = xmalloc(from_size);
+ if (read_in_full(fd, from_buf, from_size) < 0) {
+ perror(argv[2]);
+ close(fd);
+ goto cleanup;
+ }
+ close(fd);
+
+ fd = open(argv[3], O_RDONLY);
+ if (fd < 0 || fstat(fd, &st)) {
+ perror(argv[3]);
+ goto cleanup;
+ }
+ data_size = st.st_size;
+ data_buf = xmalloc(data_size);
+ if (read_in_full(fd, data_buf, data_size) < 0) {
+ perror(argv[3]);
+ close(fd);
+ goto cleanup;
+ }
+ close(fd);
+
+ if (argv[1][1] == 'd')
+ out_buf = diff_delta(from_buf, from_size,
+ data_buf, data_size,
+ &out_size, 0);
+ else
+ out_buf = patch_delta(from_buf, from_size,
+ data_buf, data_size,
+ &out_size);
+ if (!out_buf) {
+ fprintf(stderr, "delta operation failed (returned NULL)\n");
+ goto cleanup;
+ }
+
+ fd = open (argv[4], O_WRONLY|O_CREAT|O_TRUNC, 0666);
+ if (fd < 0 || write_in_full(fd, out_buf, out_size) < 0) {
+ perror(argv[4]);
+ goto cleanup;
+ }
+
+ ret = 0;
+cleanup:
+ free(from_buf);
+ free(data_buf);
+ free(out_buf);
+
+ return ret;
+}
diff --git a/t/helper/test-dir-iterator.c b/t/helper/test-dir-iterator.c
new file mode 100644
index 0000000..6b297bd
--- /dev/null
+++ b/t/helper/test-dir-iterator.c
@@ -0,0 +1,63 @@
+#include "test-tool.h"
+#include "git-compat-util.h"
+#include "strbuf.h"
+#include "iterator.h"
+#include "dir-iterator.h"
+
+static const char *error_name(int error_number)
+{
+ switch (error_number) {
+ case ENOENT: return "ENOENT";
+ case ENOTDIR: return "ENOTDIR";
+ default: return "ESOMETHINGELSE";
+ }
+}
+
+/*
+ * usage:
+ * tool-test dir-iterator [--pedantic] directory_path
+ */
+int cmd__dir_iterator(int argc, const char **argv)
+{
+ struct dir_iterator *diter;
+ unsigned int flags = 0;
+ int iter_status;
+
+ for (++argv, --argc; *argv && starts_with(*argv, "--"); ++argv, --argc) {
+ if (strcmp(*argv, "--pedantic") == 0)
+ flags |= DIR_ITERATOR_PEDANTIC;
+ else
+ die("invalid option '%s'", *argv);
+ }
+
+ if (!*argv || argc != 1)
+ die("dir-iterator needs exactly one non-option argument");
+
+ diter = dir_iterator_begin(*argv, flags);
+
+ if (!diter) {
+ printf("dir_iterator_begin failure: %s\n", error_name(errno));
+ exit(EXIT_FAILURE);
+ }
+
+ while ((iter_status = dir_iterator_advance(diter)) == ITER_OK) {
+ if (S_ISDIR(diter->st.st_mode))
+ printf("[d] ");
+ else if (S_ISREG(diter->st.st_mode))
+ printf("[f] ");
+ else if (S_ISLNK(diter->st.st_mode))
+ printf("[s] ");
+ else
+ printf("[?] ");
+
+ printf("(%s) [%s] %s\n", diter->relative_path, diter->basename,
+ diter->path.buf);
+ }
+
+ if (iter_status != ITER_DONE) {
+ printf("dir_iterator_advance failure\n");
+ return 1;
+ }
+
+ return 0;
+}
diff --git a/t/helper/test-drop-caches.c b/t/helper/test-drop-caches.c
new file mode 100644
index 0000000..73e551c
--- /dev/null
+++ b/t/helper/test-drop-caches.c
@@ -0,0 +1,162 @@
+#include "test-tool.h"
+#include "git-compat-util.h"
+
+#if defined(GIT_WINDOWS_NATIVE)
+#include "lazyload.h"
+#include <winnt.h>
+
+static int cmd_sync(void)
+{
+ char Buffer[MAX_PATH];
+ DWORD dwRet;
+ char szVolumeAccessPath[] = "\\\\.\\XXXX:";
+ HANDLE hVolWrite;
+ int success = 0, dos_drive_prefix;
+
+ dwRet = GetCurrentDirectory(MAX_PATH, Buffer);
+ if ((0 == dwRet) || (dwRet > MAX_PATH))
+ return error("Error getting current directory");
+
+ dos_drive_prefix = has_dos_drive_prefix(Buffer);
+ if (!dos_drive_prefix)
+ return error("'%s': invalid drive letter", Buffer);
+
+ memcpy(szVolumeAccessPath, Buffer, dos_drive_prefix);
+ szVolumeAccessPath[dos_drive_prefix] = '\0';
+
+ hVolWrite = CreateFile(szVolumeAccessPath, GENERIC_READ | GENERIC_WRITE,
+ FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
+ if (INVALID_HANDLE_VALUE == hVolWrite)
+ return error("Unable to open volume for writing, need admin access");
+
+ success = FlushFileBuffers(hVolWrite);
+ if (!success)
+ error("Unable to flush volume");
+
+ CloseHandle(hVolWrite);
+
+ return !success;
+}
+
+#define STATUS_SUCCESS (0x00000000L)
+#define STATUS_PRIVILEGE_NOT_HELD (0xC0000061L)
+
+typedef enum _SYSTEM_INFORMATION_CLASS {
+ SystemMemoryListInformation = 80,
+} SYSTEM_INFORMATION_CLASS;
+
+typedef enum _SYSTEM_MEMORY_LIST_COMMAND {
+ MemoryCaptureAccessedBits,
+ MemoryCaptureAndResetAccessedBits,
+ MemoryEmptyWorkingSets,
+ MemoryFlushModifiedList,
+ MemoryPurgeStandbyList,
+ MemoryPurgeLowPriorityStandbyList,
+ MemoryCommandMax
+} SYSTEM_MEMORY_LIST_COMMAND;
+
+static BOOL GetPrivilege(HANDLE TokenHandle, LPCSTR lpName, int flags)
+{
+ BOOL bResult;
+ DWORD dwBufferLength;
+ LUID luid;
+ TOKEN_PRIVILEGES tpPreviousState;
+ TOKEN_PRIVILEGES tpNewState;
+
+ dwBufferLength = 16;
+ bResult = LookupPrivilegeValueA(0, lpName, &luid);
+ if (bResult) {
+ tpNewState.PrivilegeCount = 1;
+ tpNewState.Privileges[0].Luid = luid;
+ tpNewState.Privileges[0].Attributes = 0;
+ bResult = AdjustTokenPrivileges(TokenHandle, 0, &tpNewState,
+ (DWORD)((LPBYTE)&(tpNewState.Privileges[1]) - (LPBYTE)&tpNewState),
+ &tpPreviousState, &dwBufferLength);
+ if (bResult) {
+ tpPreviousState.PrivilegeCount = 1;
+ tpPreviousState.Privileges[0].Luid = luid;
+ tpPreviousState.Privileges[0].Attributes = flags != 0 ? 2 : 0;
+ bResult = AdjustTokenPrivileges(TokenHandle, 0, &tpPreviousState,
+ dwBufferLength, 0, 0);
+ }
+ }
+ return bResult;
+}
+
+static int cmd_dropcaches(void)
+{
+ HANDLE hProcess = GetCurrentProcess();
+ HANDLE hToken;
+ DECLARE_PROC_ADDR(ntdll.dll, DWORD, NTAPI, NtSetSystemInformation, INT, PVOID,
+ ULONG);
+ SYSTEM_MEMORY_LIST_COMMAND command;
+ int status;
+
+ if (!OpenProcessToken(hProcess, TOKEN_QUERY | TOKEN_ADJUST_PRIVILEGES, &hToken))
+ return error("Can't open current process token");
+
+ if (!GetPrivilege(hToken, "SeProfileSingleProcessPrivilege", 1))
+ return error("Can't get SeProfileSingleProcessPrivilege");
+
+ CloseHandle(hToken);
+
+ if (!INIT_PROC_ADDR(NtSetSystemInformation))
+ return error("Could not find NtSetSystemInformation() function");
+
+ command = MemoryPurgeStandbyList;
+ status = NtSetSystemInformation(
+ SystemMemoryListInformation,
+ &command,
+ sizeof(SYSTEM_MEMORY_LIST_COMMAND)
+ );
+ if (status == STATUS_PRIVILEGE_NOT_HELD)
+ error("Insufficient privileges to purge the standby list, need admin access");
+ else if (status != STATUS_SUCCESS)
+ error("Unable to execute the memory list command %d", status);
+
+ return status;
+}
+
+#elif defined(__linux__)
+
+static int cmd_sync(void)
+{
+ return system("sync");
+}
+
+static int cmd_dropcaches(void)
+{
+ return system("echo 3 | sudo tee /proc/sys/vm/drop_caches");
+}
+
+#elif defined(__APPLE__)
+
+static int cmd_sync(void)
+{
+ return system("sync");
+}
+
+static int cmd_dropcaches(void)
+{
+ return system("sudo purge");
+}
+
+#else
+
+static int cmd_sync(void)
+{
+ return 0;
+}
+
+static int cmd_dropcaches(void)
+{
+ return error("drop caches not implemented on this platform");
+}
+
+#endif
+
+int cmd__drop_caches(int argc UNUSED, const char **argv UNUSED)
+{
+ cmd_sync();
+ return cmd_dropcaches();
+}
diff --git a/t/helper/test-dump-cache-tree.c b/t/helper/test-dump-cache-tree.c
new file mode 100644
index 0000000..c38f546
--- /dev/null
+++ b/t/helper/test-dump-cache-tree.c
@@ -0,0 +1,78 @@
+#define USE_THE_INDEX_VARIABLE
+#include "test-tool.h"
+#include "hash.h"
+#include "hex.h"
+#include "tree.h"
+#include "cache-tree.h"
+#include "read-cache-ll.h"
+#include "repository.h"
+#include "setup.h"
+
+static void dump_one(struct cache_tree *it, const char *pfx, const char *x)
+{
+ if (it->entry_count < 0)
+ printf("%-40s %s%s (%d subtrees)\n",
+ "invalid", x, pfx, it->subtree_nr);
+ else
+ printf("%s %s%s (%d entries, %d subtrees)\n",
+ oid_to_hex(&it->oid), x, pfx,
+ it->entry_count, it->subtree_nr);
+}
+
+static int dump_cache_tree(struct cache_tree *it,
+ struct cache_tree *ref,
+ const char *pfx)
+{
+ int i;
+ int errs = 0;
+
+ if (!it || !ref)
+ /* missing in either */
+ return 0;
+
+ if (it->entry_count < 0) {
+ /* invalid */
+ dump_one(it, pfx, "");
+ dump_one(ref, pfx, "#(ref) ");
+ }
+ else {
+ dump_one(it, pfx, "");
+ if (!oideq(&it->oid, &ref->oid) ||
+ ref->entry_count != it->entry_count ||
+ ref->subtree_nr != it->subtree_nr) {
+ /* claims to be valid but is lying */
+ dump_one(ref, pfx, "#(ref) ");
+ errs = 1;
+ }
+ }
+
+ for (i = 0; i < it->subtree_nr; i++) {
+ char path[PATH_MAX];
+ struct cache_tree_sub *down = it->down[i];
+ struct cache_tree_sub *rdwn;
+
+ rdwn = cache_tree_sub(ref, down->name);
+ xsnprintf(path, sizeof(path), "%s%.*s/", pfx, down->namelen, down->name);
+ if (dump_cache_tree(down->cache_tree, rdwn->cache_tree, path))
+ errs = 1;
+ }
+ return errs;
+}
+
+int cmd__dump_cache_tree(int ac UNUSED, const char **av UNUSED)
+{
+ struct index_state istate;
+ struct cache_tree *another = cache_tree();
+ int ret;
+
+ setup_git_directory();
+ if (repo_read_index(the_repository) < 0)
+ die("unable to read index file");
+ istate = the_index;
+ istate.cache_tree = another;
+ cache_tree_update(&istate, WRITE_TREE_DRY_RUN);
+ ret = dump_cache_tree(the_index.cache_tree, another, "");
+ cache_tree_free(&another);
+
+ return ret;
+}
diff --git a/t/helper/test-dump-fsmonitor.c b/t/helper/test-dump-fsmonitor.c
new file mode 100644
index 0000000..4f215fe
--- /dev/null
+++ b/t/helper/test-dump-fsmonitor.c
@@ -0,0 +1,24 @@
+#include "test-tool.h"
+#include "read-cache-ll.h"
+#include "repository.h"
+#include "setup.h"
+
+int cmd__dump_fsmonitor(int ac UNUSED, const char **av UNUSED)
+{
+ struct index_state *istate = the_repository->index;
+ int i;
+
+ setup_git_directory();
+ if (do_read_index(istate, the_repository->index_file, 0) < 0)
+ die("unable to read index file");
+ if (!istate->fsmonitor_last_update) {
+ printf("no fsmonitor\n");
+ return 0;
+ }
+ printf("fsmonitor last update %s\n", istate->fsmonitor_last_update);
+
+ for (i = 0; i < istate->cache_nr; i++)
+ printf((istate->cache[i]->ce_flags & CE_FSMONITOR_VALID) ? "+" : "-");
+
+ return 0;
+}
diff --git a/t/helper/test-dump-split-index.c b/t/helper/test-dump-split-index.c
new file mode 100644
index 0000000..f29d18e
--- /dev/null
+++ b/t/helper/test-dump-split-index.c
@@ -0,0 +1,43 @@
+#define USE_THE_INDEX_VARIABLE
+#include "test-tool.h"
+#include "hex.h"
+#include "read-cache-ll.h"
+#include "repository.h"
+#include "setup.h"
+#include "split-index.h"
+#include "ewah/ewok.h"
+
+static void show_bit(size_t pos, void *data UNUSED)
+{
+ printf(" %d", (int)pos);
+}
+
+int cmd__dump_split_index(int ac UNUSED, const char **av)
+{
+ struct split_index *si;
+ int i;
+
+ setup_git_directory();
+
+ do_read_index(&the_index, av[1], 1);
+ printf("own %s\n", oid_to_hex(&the_index.oid));
+ si = the_index.split_index;
+ if (!si) {
+ printf("not a split index\n");
+ return 0;
+ }
+ printf("base %s\n", oid_to_hex(&si->base_oid));
+ for (i = 0; i < the_index.cache_nr; i++) {
+ struct cache_entry *ce = the_index.cache[i];
+ printf("%06o %s %d\t%s\n", ce->ce_mode,
+ oid_to_hex(&ce->oid), ce_stage(ce), ce->name);
+ }
+ printf("replacements:");
+ if (si->replace_bitmap)
+ ewah_each_bit(si->replace_bitmap, show_bit, NULL);
+ printf("\ndeletions:");
+ if (si->delete_bitmap)
+ ewah_each_bit(si->delete_bitmap, show_bit, NULL);
+ printf("\n");
+ return 0;
+}
diff --git a/t/helper/test-dump-untracked-cache.c b/t/helper/test-dump-untracked-cache.c
new file mode 100644
index 0000000..b4af971
--- /dev/null
+++ b/t/helper/test-dump-untracked-cache.c
@@ -0,0 +1,71 @@
+#define USE_THE_INDEX_VARIABLE
+#include "test-tool.h"
+#include "dir.h"
+#include "hex.h"
+#include "read-cache-ll.h"
+#include "repository.h"
+#include "setup.h"
+
+static int compare_untracked(const void *a_, const void *b_)
+{
+ const char *const *a = a_;
+ const char *const *b = b_;
+ return strcmp(*a, *b);
+}
+
+static int compare_dir(const void *a_, const void *b_)
+{
+ const struct untracked_cache_dir *const *a = a_;
+ const struct untracked_cache_dir *const *b = b_;
+ return strcmp((*a)->name, (*b)->name);
+}
+
+static void dump(struct untracked_cache_dir *ucd, struct strbuf *base)
+{
+ int i, len;
+ QSORT(ucd->untracked, ucd->untracked_nr, compare_untracked);
+ QSORT(ucd->dirs, ucd->dirs_nr, compare_dir);
+ len = base->len;
+ strbuf_addf(base, "%s/", ucd->name);
+ printf("%s %s", base->buf,
+ oid_to_hex(&ucd->exclude_oid));
+ if (ucd->recurse)
+ fputs(" recurse", stdout);
+ if (ucd->check_only)
+ fputs(" check_only", stdout);
+ if (ucd->valid)
+ fputs(" valid", stdout);
+ printf("\n");
+ for (i = 0; i < ucd->untracked_nr; i++)
+ printf("%s\n", ucd->untracked[i]);
+ for (i = 0; i < ucd->dirs_nr; i++)
+ dump(ucd->dirs[i], base);
+ strbuf_setlen(base, len);
+}
+
+int cmd__dump_untracked_cache(int ac UNUSED, const char **av UNUSED)
+{
+ struct untracked_cache *uc;
+ struct strbuf base = STRBUF_INIT;
+
+ /* Set core.untrackedCache=keep before setup_git_directory() */
+ xsetenv("GIT_CONFIG_COUNT", "1", 1);
+ xsetenv("GIT_CONFIG_KEY_0", "core.untrackedCache", 1);
+ xsetenv("GIT_CONFIG_VALUE_0", "keep", 1);
+
+ setup_git_directory();
+ if (repo_read_index(the_repository) < 0)
+ die("unable to read index file");
+ uc = the_index.untracked;
+ if (!uc) {
+ printf("no untracked cache\n");
+ return 0;
+ }
+ printf("info/exclude %s\n", oid_to_hex(&uc->ss_info_exclude.oid));
+ printf("core.excludesfile %s\n", oid_to_hex(&uc->ss_excludes_file.oid));
+ printf("exclude_per_dir %s\n", uc->exclude_per_dir);
+ printf("flags %08x\n", uc->dir_flags);
+ if (uc->root)
+ dump(uc->root, &base);
+ return 0;
+}
diff --git a/t/helper/test-env-helper.c b/t/helper/test-env-helper.c
new file mode 100644
index 0000000..1c48688
--- /dev/null
+++ b/t/helper/test-env-helper.c
@@ -0,0 +1,100 @@
+#include "test-tool.h"
+#include "parse.h"
+#include "parse-options.h"
+
+static char const * const env__helper_usage[] = {
+ "test-tool env-helper --type=[bool|ulong] <options> <env-var>",
+ NULL
+};
+
+enum cmdmode {
+ ENV_HELPER_TYPE_BOOL = 1,
+ ENV_HELPER_TYPE_ULONG
+};
+
+static int option_parse_type(const struct option *opt, const char *arg,
+ int unset)
+{
+ enum cmdmode *cmdmode = opt->value;
+
+ BUG_ON_OPT_NEG(unset);
+
+ if (!strcmp(arg, "bool"))
+ *cmdmode = ENV_HELPER_TYPE_BOOL;
+ else if (!strcmp(arg, "ulong"))
+ *cmdmode = ENV_HELPER_TYPE_ULONG;
+ else
+ die("unrecognized --type argument, %s", arg);
+
+ return 0;
+}
+
+int cmd__env_helper(int argc, const char **argv)
+{
+ int exit_code = 0;
+ const char *env_variable = NULL;
+ const char *env_default = NULL;
+ int ret;
+ int ret_int, default_int;
+ unsigned long ret_ulong, default_ulong;
+ enum cmdmode cmdmode = 0;
+ struct option opts[] = {
+ OPT_CALLBACK_F(0, "type", &cmdmode, "type",
+ "value is given this type", PARSE_OPT_NONEG,
+ option_parse_type),
+ OPT_STRING(0, "default", &env_default, "value",
+ "default for git_env_*(...) to fall back on"),
+ OPT_BOOL(0, "exit-code", &exit_code,
+ "be quiet only use git_env_*() value as exit code"),
+ OPT_END(),
+ };
+
+ argc = parse_options(argc, argv, NULL, opts, env__helper_usage,
+ PARSE_OPT_KEEP_UNKNOWN_OPT);
+ if (env_default && !*env_default)
+ usage_with_options(env__helper_usage, opts);
+ if (!cmdmode)
+ usage_with_options(env__helper_usage, opts);
+ if (argc != 1)
+ usage_with_options(env__helper_usage, opts);
+ env_variable = argv[0];
+
+ switch (cmdmode) {
+ case ENV_HELPER_TYPE_BOOL:
+ if (env_default) {
+ default_int = git_parse_maybe_bool(env_default);
+ if (default_int == -1) {
+ error("option `--default' expects a boolean value with `--type=bool`, not `%s`",
+ env_default);
+ usage_with_options(env__helper_usage, opts);
+ }
+ } else {
+ default_int = 0;
+ }
+ ret_int = git_env_bool(env_variable, default_int);
+ if (!exit_code)
+ puts(ret_int ? "true" : "false");
+ ret = ret_int;
+ break;
+ case ENV_HELPER_TYPE_ULONG:
+ if (env_default) {
+ if (!git_parse_ulong(env_default, &default_ulong)) {
+ error("option `--default' expects an unsigned long value with `--type=ulong`, not `%s`",
+ env_default);
+ usage_with_options(env__helper_usage, opts);
+ }
+ } else {
+ default_ulong = 0;
+ }
+ ret_ulong = git_env_ulong(env_variable, default_ulong);
+ if (!exit_code)
+ printf("%lu\n", ret_ulong);
+ ret = ret_ulong;
+ break;
+ default:
+ BUG("unknown <type> value");
+ break;
+ }
+
+ return !ret;
+}
diff --git a/t/helper/test-example-decorate.c b/t/helper/test-example-decorate.c
new file mode 100644
index 0000000..8f59f6b
--- /dev/null
+++ b/t/helper/test-example-decorate.c
@@ -0,0 +1,78 @@
+#include "test-tool.h"
+#include "git-compat-util.h"
+#include "object.h"
+#include "decorate.h"
+#include "repository.h"
+
+int cmd__example_decorate(int argc UNUSED, const char **argv UNUSED)
+{
+ struct decoration n;
+ struct object_id one_oid = { {1} };
+ struct object_id two_oid = { {2} };
+ struct object_id three_oid = { {3} };
+ struct object *one, *two, *three;
+
+ int decoration_a, decoration_b;
+
+ void *ret;
+
+ int i, objects_noticed = 0;
+
+ /*
+ * The struct must be zero-initialized.
+ */
+ memset(&n, 0, sizeof(n));
+
+ /*
+ * Add 2 objects, one with a non-NULL decoration and one with a NULL
+ * decoration.
+ */
+ one = lookup_unknown_object(the_repository, &one_oid);
+ two = lookup_unknown_object(the_repository, &two_oid);
+ ret = add_decoration(&n, one, &decoration_a);
+ if (ret)
+ BUG("when adding a brand-new object, NULL should be returned");
+ ret = add_decoration(&n, two, NULL);
+ if (ret)
+ BUG("when adding a brand-new object, NULL should be returned");
+
+ /*
+ * When re-adding an already existing object, the old decoration is
+ * returned.
+ */
+ ret = add_decoration(&n, one, NULL);
+ if (ret != &decoration_a)
+ BUG("when readding an already existing object, existing decoration should be returned");
+ ret = add_decoration(&n, two, &decoration_b);
+ if (ret)
+ BUG("when readding an already existing object, existing decoration should be returned");
+
+ /*
+ * Lookup returns the added declarations, or NULL if the object was
+ * never added.
+ */
+ ret = lookup_decoration(&n, one);
+ if (ret)
+ BUG("lookup should return added declaration");
+ ret = lookup_decoration(&n, two);
+ if (ret != &decoration_b)
+ BUG("lookup should return added declaration");
+ three = lookup_unknown_object(the_repository, &three_oid);
+ ret = lookup_decoration(&n, three);
+ if (ret)
+ BUG("lookup for unknown object should return NULL");
+
+ /*
+ * The user can also loop through all entries.
+ */
+ for (i = 0; i < n.size; i++) {
+ if (n.entries[i].base)
+ objects_noticed++;
+ }
+ if (objects_noticed != 2)
+ BUG("should have 2 objects");
+
+ clear_decoration(&n, NULL);
+
+ return 0;
+}
diff --git a/t/helper/test-fake-ssh.c b/t/helper/test-fake-ssh.c
new file mode 100644
index 0000000..27323cb
--- /dev/null
+++ b/t/helper/test-fake-ssh.c
@@ -0,0 +1,32 @@
+#include "git-compat-util.h"
+#include "run-command.h"
+#include "strbuf.h"
+
+int cmd_main(int argc, const char **argv)
+{
+ const char *trash_directory = getenv("TRASH_DIRECTORY");
+ struct strbuf buf = STRBUF_INIT;
+ FILE *f;
+ int i;
+ struct child_process cmd = CHILD_PROCESS_INIT;
+
+ /* First, print all parameters into $TRASH_DIRECTORY/ssh-output */
+ if (!trash_directory)
+ die("Need a TRASH_DIRECTORY!");
+ strbuf_addf(&buf, "%s/ssh-output", trash_directory);
+ f = fopen(buf.buf, "w");
+ if (!f)
+ die("Could not write to %s", buf.buf);
+ strbuf_release(&buf);
+ for (i = 0; i < argc; i++)
+ fprintf(f, "%s%s", i > 0 ? " " : "", i > 0 ? argv[i] : "ssh:");
+ fprintf(f, "\n");
+ fclose(f);
+
+ /* Now, evaluate the *last* parameter */
+ if (argc < 2)
+ return 0;
+ cmd.use_shell = 1;
+ strvec_push(&cmd.args, argv[argc - 1]);
+ return run_command(&cmd);
+}
diff --git a/t/helper/test-fast-rebase.c b/t/helper/test-fast-rebase.c
new file mode 100644
index 0000000..cac20a7
--- /dev/null
+++ b/t/helper/test-fast-rebase.c
@@ -0,0 +1,241 @@
+/*
+ * "git fast-rebase" builtin command
+ *
+ * FAST: Forking Any Subprocesses (is) Taboo
+ *
+ * This is meant SOLELY as a demo of what is possible. sequencer.c and
+ * rebase.c should be refactored to use the ideas here, rather than attempting
+ * to extend this file to replace those (unless Phillip or Dscho say that
+ * refactoring is too hard and we need a clean slate, but I'm guessing that
+ * refactoring is the better route).
+ */
+
+#define USE_THE_INDEX_VARIABLE
+#include "test-tool.h"
+#include "cache-tree.h"
+#include "commit.h"
+#include "environment.h"
+#include "gettext.h"
+#include "hash.h"
+#include "hex.h"
+#include "lockfile.h"
+#include "merge-ort.h"
+#include "object-name.h"
+#include "read-cache-ll.h"
+#include "refs.h"
+#include "revision.h"
+#include "sequencer.h"
+#include "setup.h"
+#include "strvec.h"
+#include "tree.h"
+
+static const char *short_commit_name(struct commit *commit)
+{
+ return repo_find_unique_abbrev(the_repository, &commit->object.oid,
+ DEFAULT_ABBREV);
+}
+
+static struct commit *peel_committish(const char *name)
+{
+ struct object *obj;
+ struct object_id oid;
+
+ if (repo_get_oid(the_repository, name, &oid))
+ return NULL;
+ obj = parse_object(the_repository, &oid);
+ return (struct commit *)repo_peel_to_type(the_repository, name, 0, obj,
+ OBJ_COMMIT);
+}
+
+static char *get_author(const char *message)
+{
+ size_t len;
+ const char *a;
+
+ a = find_commit_header(message, "author", &len);
+ if (a)
+ return xmemdupz(a, len);
+
+ return NULL;
+}
+
+static struct commit *create_commit(struct tree *tree,
+ struct commit *based_on,
+ struct commit *parent)
+{
+ struct object_id ret;
+ struct object *obj;
+ struct commit_list *parents = NULL;
+ char *author;
+ char *sign_commit = NULL;
+ struct commit_extra_header *extra;
+ struct strbuf msg = STRBUF_INIT;
+ const char *out_enc = get_commit_output_encoding();
+ const char *message = repo_logmsg_reencode(the_repository, based_on,
+ NULL, out_enc);
+ const char *orig_message = NULL;
+ const char *exclude_gpgsig[] = { "gpgsig", NULL };
+
+ commit_list_insert(parent, &parents);
+ extra = read_commit_extra_headers(based_on, exclude_gpgsig);
+ find_commit_subject(message, &orig_message);
+ strbuf_addstr(&msg, orig_message);
+ author = get_author(message);
+ reset_ident_date();
+ if (commit_tree_extended(msg.buf, msg.len, &tree->object.oid, parents,
+ &ret, author, NULL, sign_commit, extra)) {
+ error(_("failed to write commit object"));
+ return NULL;
+ }
+ free(author);
+ strbuf_release(&msg);
+
+ obj = parse_object(the_repository, &ret);
+ return (struct commit *)obj;
+}
+
+int cmd__fast_rebase(int argc, const char **argv)
+{
+ struct commit *onto;
+ struct commit *last_commit = NULL, *last_picked_commit = NULL;
+ struct object_id head;
+ struct lock_file lock = LOCK_INIT;
+ struct strvec rev_walk_args = STRVEC_INIT;
+ struct rev_info revs;
+ struct commit *commit;
+ struct merge_options merge_opt;
+ struct tree *next_tree, *base_tree, *head_tree;
+ struct merge_result result;
+ struct strbuf reflog_msg = STRBUF_INIT;
+ struct strbuf branch_name = STRBUF_INIT;
+ int ret = 0;
+
+ /*
+ * test-tool stuff doesn't set up the git directory by default; need to
+ * do that manually.
+ */
+ setup_git_directory();
+
+ if (argc == 2 && !strcmp(argv[1], "-h")) {
+ printf("Sorry, I am not a psychiatrist; I can not give you the help you need. Oh, you meant usage...\n");
+ exit(129);
+ }
+
+ if (argc != 5 || strcmp(argv[1], "--onto"))
+ die("usage: read the code, figure out how to use it, then do so");
+
+ onto = peel_committish(argv[2]);
+ strbuf_addf(&branch_name, "refs/heads/%s", argv[4]);
+
+ /* Sanity check */
+ if (repo_get_oid(the_repository, "HEAD", &head))
+ die(_("Cannot read HEAD"));
+ assert(oideq(&onto->object.oid, &head));
+
+ repo_hold_locked_index(the_repository, &lock, LOCK_DIE_ON_ERROR);
+ if (repo_read_index(the_repository) < 0)
+ BUG("Could not read index");
+
+ repo_init_revisions(the_repository, &revs, NULL);
+ revs.verbose_header = 1;
+ revs.max_parents = 1;
+ revs.cherry_mark = 1;
+ revs.limited = 1;
+ revs.reverse = 1;
+ revs.right_only = 1;
+ revs.sort_order = REV_SORT_IN_GRAPH_ORDER;
+ revs.topo_order = 1;
+ strvec_pushl(&rev_walk_args, "", argv[4], "--not", argv[3], NULL);
+
+ if (setup_revisions(rev_walk_args.nr, rev_walk_args.v, &revs, NULL) > 1) {
+ ret = error(_("unhandled options"));
+ goto cleanup;
+ }
+
+ strvec_clear(&rev_walk_args);
+
+ if (prepare_revision_walk(&revs) < 0) {
+ ret = error(_("error preparing revisions"));
+ goto cleanup;
+ }
+
+ init_merge_options(&merge_opt, the_repository);
+ memset(&result, 0, sizeof(result));
+ merge_opt.show_rename_progress = 1;
+ merge_opt.branch1 = "HEAD";
+ head_tree = repo_get_commit_tree(the_repository, onto);
+ result.tree = head_tree;
+ last_commit = onto;
+ while ((commit = get_revision(&revs))) {
+ struct commit *base;
+
+ fprintf(stderr, "Rebasing %s...\r",
+ oid_to_hex(&commit->object.oid));
+ assert(commit->parents && !commit->parents->next);
+ base = commit->parents->item;
+
+ next_tree = repo_get_commit_tree(the_repository, commit);
+ base_tree = repo_get_commit_tree(the_repository, base);
+
+ merge_opt.branch2 = short_commit_name(commit);
+ merge_opt.ancestor = xstrfmt("parent of %s", merge_opt.branch2);
+
+ merge_incore_nonrecursive(&merge_opt,
+ base_tree,
+ result.tree,
+ next_tree,
+ &result);
+
+ free((char*)merge_opt.ancestor);
+ merge_opt.ancestor = NULL;
+ if (!result.clean)
+ break;
+ last_picked_commit = commit;
+ last_commit = create_commit(result.tree, commit, last_commit);
+ }
+
+ merge_switch_to_result(&merge_opt, head_tree, &result, 1, !result.clean);
+
+ if (result.clean < 0)
+ exit(128);
+
+ if (result.clean) {
+ fprintf(stderr, "\nDone.\n");
+ strbuf_addf(&reflog_msg, "finish rebase %s onto %s",
+ oid_to_hex(&last_picked_commit->object.oid),
+ oid_to_hex(&last_commit->object.oid));
+ if (update_ref(reflog_msg.buf, branch_name.buf,
+ &last_commit->object.oid,
+ &last_picked_commit->object.oid,
+ REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) {
+ error(_("could not update %s"), argv[4]);
+ die("Failed to update %s", argv[4]);
+ }
+ if (create_symref("HEAD", branch_name.buf, reflog_msg.buf) < 0)
+ die(_("unable to update HEAD"));
+
+ prime_cache_tree(the_repository, the_repository->index,
+ result.tree);
+ } else {
+ fprintf(stderr, "\nAborting: Hit a conflict.\n");
+ strbuf_addf(&reflog_msg, "rebase progress up to %s",
+ oid_to_hex(&last_picked_commit->object.oid));
+ if (update_ref(reflog_msg.buf, "HEAD",
+ &last_commit->object.oid,
+ &head,
+ REF_NO_DEREF, UPDATE_REFS_MSG_ON_ERR)) {
+ error(_("could not update %s"), argv[4]);
+ die("Failed to update %s", argv[4]);
+ }
+ }
+ if (write_locked_index(&the_index, &lock,
+ COMMIT_LOCK | SKIP_IF_UNCHANGED))
+ die(_("unable to write %s"), get_index_file());
+
+ ret = (result.clean == 0);
+cleanup:
+ strbuf_release(&reflog_msg);
+ strbuf_release(&branch_name);
+ release_revisions(&revs);
+ return ret;
+}
diff --git a/t/helper/test-find-pack.c b/t/helper/test-find-pack.c
new file mode 100644
index 0000000..e8bd793
--- /dev/null
+++ b/t/helper/test-find-pack.c
@@ -0,0 +1,50 @@
+#include "test-tool.h"
+#include "object-name.h"
+#include "object-store.h"
+#include "packfile.h"
+#include "parse-options.h"
+#include "setup.h"
+
+/*
+ * Display the path(s), one per line, of the packfile(s) containing
+ * the given object.
+ *
+ * If '--check-count <n>' is passed, then error out if the number of
+ * packfiles containing the object is not <n>.
+ */
+
+static const char *find_pack_usage[] = {
+ "test-tool find-pack [--check-count <n>] <object>",
+ NULL
+};
+
+int cmd__find_pack(int argc, const char **argv)
+{
+ struct object_id oid;
+ struct packed_git *p;
+ int count = -1, actual_count = 0;
+ const char *prefix = setup_git_directory();
+
+ struct option options[] = {
+ OPT_INTEGER('c', "check-count", &count, "expected number of packs"),
+ OPT_END(),
+ };
+
+ argc = parse_options(argc, argv, prefix, options, find_pack_usage, 0);
+ if (argc != 1)
+ usage(find_pack_usage[0]);
+
+ if (repo_get_oid(the_repository, argv[0], &oid))
+ die("cannot parse %s as an object name", argv[0]);
+
+ for (p = get_all_packs(the_repository); p; p = p->next)
+ if (find_pack_entry_one(oid.hash, p)) {
+ printf("%s\n", p->pack_name);
+ actual_count++;
+ }
+
+ if (count > -1 && count != actual_count)
+ die("bad packfile count %d instead of %d", actual_count, count);
+
+ return 0;
+}
diff --git a/t/helper/test-fsmonitor-client.c b/t/helper/test-fsmonitor-client.c
new file mode 100644
index 0000000..8280984
--- /dev/null
+++ b/t/helper/test-fsmonitor-client.c
@@ -0,0 +1,224 @@
+/*
+ * test-fsmonitor-client.c: client code to send commands/requests to
+ * a `git fsmonitor--daemon` daemon.
+ */
+
+#include "test-tool.h"
+#include "parse-options.h"
+#include "fsmonitor-ipc.h"
+#include "read-cache-ll.h"
+#include "repository.h"
+#include "setup.h"
+#include "thread-utils.h"
+#include "trace2.h"
+
+#ifndef HAVE_FSMONITOR_DAEMON_BACKEND
+int cmd__fsmonitor_client(int argc UNUSED, const char **argv UNUSED)
+{
+ die("fsmonitor--daemon not available on this platform");
+}
+#else
+
+/*
+ * Read the `.git/index` to get the last token written to the
+ * FSMonitor Index Extension.
+ */
+static const char *get_token_from_index(void)
+{
+ struct index_state *istate = the_repository->index;
+
+ if (do_read_index(istate, the_repository->index_file, 0) < 0)
+ die("unable to read index file");
+ if (!istate->fsmonitor_last_update)
+ die("index file does not have fsmonitor extension");
+
+ return istate->fsmonitor_last_update;
+}
+
+/*
+ * Send an IPC query to a `git-fsmonitor--daemon` daemon and
+ * ask for the changes since the given token or from the last
+ * token in the index extension.
+ *
+ * This will implicitly start a daemon process if necessary. The
+ * daemon process will persist after we exit.
+ */
+static int do_send_query(const char *token)
+{
+ struct strbuf answer = STRBUF_INIT;
+ int ret;
+
+ if (!token || !*token)
+ token = get_token_from_index();
+
+ ret = fsmonitor_ipc__send_query(token, &answer);
+ if (ret < 0)
+ die("could not query fsmonitor--daemon");
+
+ write_in_full(1, answer.buf, answer.len);
+ strbuf_release(&answer);
+
+ return 0;
+}
+
+/*
+ * Send a "flush" command to the `git-fsmonitor--daemon` (if running)
+ * and tell it to flush its cache.
+ *
+ * This feature is primarily used by the test suite to simulate a loss of
+ * sync with the filesystem where we miss kernel events.
+ */
+static int do_send_flush(void)
+{
+ struct strbuf answer = STRBUF_INIT;
+ int ret;
+
+ ret = fsmonitor_ipc__send_command("flush", &answer);
+ if (ret)
+ return ret;
+
+ write_in_full(1, answer.buf, answer.len);
+ strbuf_release(&answer);
+
+ return 0;
+}
+
+struct hammer_thread_data
+{
+ pthread_t pthread_id;
+ int thread_nr;
+
+ int nr_requests;
+ const char *token;
+
+ int sum_successful;
+ int sum_errors;
+};
+
+static void *hammer_thread_proc(void *_hammer_thread_data)
+{
+ struct hammer_thread_data *data = _hammer_thread_data;
+ struct strbuf answer = STRBUF_INIT;
+ int k;
+ int ret;
+
+ trace2_thread_start("hammer");
+
+ for (k = 0; k < data->nr_requests; k++) {
+ strbuf_reset(&answer);
+
+ ret = fsmonitor_ipc__send_query(data->token, &answer);
+ if (ret < 0)
+ data->sum_errors++;
+ else
+ data->sum_successful++;
+ }
+
+ strbuf_release(&answer);
+ trace2_thread_exit();
+ return NULL;
+}
+
+/*
+ * Start a pool of client threads that will each send a series of
+ * commands to the daemon.
+ *
+ * The goal is to overload the daemon with a sustained series of
+ * concurrent requests.
+ */
+static int do_hammer(const char *token, int nr_threads, int nr_requests)
+{
+ struct hammer_thread_data *data = NULL;
+ int k;
+ int sum_join_errors = 0;
+ int sum_commands = 0;
+ int sum_errors = 0;
+
+ if (!token || !*token)
+ token = get_token_from_index();
+ if (nr_threads < 1)
+ nr_threads = 1;
+ if (nr_requests < 1)
+ nr_requests = 1;
+
+ CALLOC_ARRAY(data, nr_threads);
+
+ for (k = 0; k < nr_threads; k++) {
+ struct hammer_thread_data *p = &data[k];
+ p->thread_nr = k;
+ p->nr_requests = nr_requests;
+ p->token = token;
+
+ if (pthread_create(&p->pthread_id, NULL, hammer_thread_proc, p)) {
+ warning("failed to create thread[%d] skipping remainder", k);
+ nr_threads = k;
+ break;
+ }
+ }
+
+ for (k = 0; k < nr_threads; k++) {
+ struct hammer_thread_data *p = &data[k];
+
+ if (pthread_join(p->pthread_id, NULL))
+ sum_join_errors++;
+ sum_commands += p->sum_successful;
+ sum_errors += p->sum_errors;
+ }
+
+ fprintf(stderr, "HAMMER: [threads %d][requests %d] [ok %d][err %d][join %d]\n",
+ nr_threads, nr_requests, sum_commands, sum_errors, sum_join_errors);
+
+ free(data);
+
+ /*
+ * Return an error if any of the _send_query requests failed.
+ * We don't care about thread create/join errors.
+ */
+ return sum_errors > 0;
+}
+
+int cmd__fsmonitor_client(int argc, const char **argv)
+{
+ const char *subcmd;
+ const char *token = NULL;
+ int nr_threads = 1;
+ int nr_requests = 1;
+
+ const char * const fsmonitor_client_usage[] = {
+ "test-tool fsmonitor-client query [<token>]",
+ "test-tool fsmonitor-client flush",
+ "test-tool fsmonitor-client hammer [<token>] [<threads>] [<requests>]",
+ NULL,
+ };
+
+ struct option options[] = {
+ OPT_STRING(0, "token", &token, "token",
+ "command token to send to the server"),
+
+ OPT_INTEGER(0, "threads", &nr_threads, "number of client threads"),
+ OPT_INTEGER(0, "requests", &nr_requests, "number of requests per thread"),
+
+ OPT_END()
+ };
+
+ argc = parse_options(argc, argv, NULL, options, fsmonitor_client_usage, 0);
+
+ if (argc != 1)
+ usage_with_options(fsmonitor_client_usage, options);
+
+ subcmd = argv[0];
+
+ setup_git_directory();
+
+ if (!strcmp(subcmd, "query"))
+ return !!do_send_query(token);
+
+ if (!strcmp(subcmd, "flush"))
+ return !!do_send_flush();
+
+ if (!strcmp(subcmd, "hammer"))
+ return !!do_hammer(token, nr_threads, nr_requests);
+
+ die("Unhandled subcommand: '%s'", subcmd);
+}
+#endif
diff --git a/t/helper/test-genrandom.c b/t/helper/test-genrandom.c
new file mode 100644
index 0000000..99b8dc1
--- /dev/null
+++ b/t/helper/test-genrandom.c
@@ -0,0 +1,34 @@
+/*
+ * Simple random data generator used to create reproducible test files.
+ * This is inspired from POSIX.1-2001 implementation example for rand().
+ * Copyright (C) 2007 by Nicolas Pitre, licensed under the GPL version 2.
+ */
+
+#include "test-tool.h"
+#include "git-compat-util.h"
+
+int cmd__genrandom(int argc, const char **argv)
+{
+ unsigned long count, next = 0;
+ unsigned char *c;
+
+ if (argc < 2 || argc > 3) {
+ fprintf(stderr, "usage: %s <seed_string> [<size>]\n", argv[0]);
+ return 1;
+ }
+
+ c = (unsigned char *) argv[1];
+ do {
+ next = next * 11 + *c;
+ } while (*c++);
+
+ count = (argc == 3) ? strtoul(argv[2], NULL, 0) : -1L;
+
+ while (count--) {
+ next = next * 1103515245 + 12345;
+ if (putchar((next >> 16) & 0xff) == EOF)
+ return -1;
+ }
+
+ return 0;
+}
diff --git a/t/helper/test-genzeros.c b/t/helper/test-genzeros.c
new file mode 100644
index 0000000..47af843
--- /dev/null
+++ b/t/helper/test-genzeros.c
@@ -0,0 +1,35 @@
+#include "test-tool.h"
+#include "git-compat-util.h"
+
+int cmd__genzeros(int argc, const char **argv)
+{
+ /* static, so that it is NUL-initialized */
+ static const char zeros[256 * 1024];
+ intmax_t count;
+ ssize_t n;
+
+ if (argc > 2) {
+ fprintf(stderr, "usage: %s [<count>]\n", argv[0]);
+ return 1;
+ }
+
+ count = argc > 1 ? strtoimax(argv[1], NULL, 0) : -1;
+
+ /* Writing out individual NUL bytes is slow... */
+ while (count < 0)
+ if (xwrite(1, zeros, ARRAY_SIZE(zeros)) < 0)
+ die_errno("write error");
+
+ while (count > 0) {
+ n = xwrite(1, zeros,
+ count < ARRAY_SIZE(zeros)
+ ? count : ARRAY_SIZE(zeros));
+
+ if (n < 0)
+ die_errno("write error");
+
+ count -= n;
+ }
+
+ return 0;
+}
diff --git a/t/helper/test-getcwd.c b/t/helper/test-getcwd.c
new file mode 100644
index 0000000..d680038
--- /dev/null
+++ b/t/helper/test-getcwd.c
@@ -0,0 +1,26 @@
+#include "test-tool.h"
+#include "git-compat-util.h"
+#include "parse-options.h"
+
+static const char *getcwd_usage[] = {
+ "test-tool getcwd",
+ NULL
+};
+
+int cmd__getcwd(int argc, const char **argv)
+{
+ struct option options[] = {
+ OPT_END()
+ };
+ char *cwd;
+
+ argc = parse_options(argc, argv, "test-tools", options, getcwd_usage, 0);
+ if (argc > 0)
+ usage_with_options(getcwd_usage, options);
+
+ cwd = xgetcwd();
+ puts(cwd);
+ free(cwd);
+
+ return 0;
+}
diff --git a/t/helper/test-hash-speed.c b/t/helper/test-hash-speed.c
new file mode 100644
index 0000000..b235da5
--- /dev/null
+++ b/t/helper/test-hash-speed.c
@@ -0,0 +1,61 @@
+#include "test-tool.h"
+#include "hash-ll.h"
+
+#define NUM_SECONDS 3
+
+static inline void compute_hash(const struct git_hash_algo *algo, git_hash_ctx *ctx, uint8_t *final, const void *p, size_t len)
+{
+ algo->init_fn(ctx);
+ algo->update_fn(ctx, p, len);
+ algo->final_fn(final, ctx);
+}
+
+int cmd__hash_speed(int ac, const char **av)
+{
+ git_hash_ctx ctx;
+ unsigned char hash[GIT_MAX_RAWSZ];
+ clock_t initial, start, end;
+ unsigned bufsizes[] = { 64, 256, 1024, 8192, 16384 };
+ int i;
+ void *p;
+ const struct git_hash_algo *algo = NULL;
+
+ if (ac == 2) {
+ for (i = 1; i < GIT_HASH_NALGOS; i++) {
+ if (!strcmp(av[1], hash_algos[i].name)) {
+ algo = &hash_algos[i];
+ break;
+ }
+ }
+ }
+ if (!algo)
+ die("usage: test-tool hash-speed algo_name");
+
+ /* Use this as an offset to make overflow less likely. */
+ initial = clock();
+
+ printf("algo: %s\n", algo->name);
+
+ for (i = 0; i < ARRAY_SIZE(bufsizes); i++) {
+ unsigned long j, kb;
+ double kb_per_sec;
+ p = xcalloc(1, bufsizes[i]);
+ start = end = clock() - initial;
+ for (j = 0; ((end - start) / CLOCKS_PER_SEC) < NUM_SECONDS; j++) {
+ compute_hash(algo, &ctx, hash, p, bufsizes[i]);
+
+ /*
+ * Only check elapsed time every 128 iterations to avoid
+ * dominating the runtime with system calls.
+ */
+ if (!(j & 127))
+ end = clock() - initial;
+ }
+ kb = j * bufsizes[i];
+ kb_per_sec = kb / (1024 * ((double)end - start) / CLOCKS_PER_SEC);
+ printf("size %u: %lu iters; %lu KiB; %0.2f KiB/s\n", bufsizes[i], j, kb, kb_per_sec);
+ free(p);
+ }
+
+ return 0;
+}
diff --git a/t/helper/test-hash.c b/t/helper/test-hash.c
new file mode 100644
index 0000000..45d829c
--- /dev/null
+++ b/t/helper/test-hash.c
@@ -0,0 +1,59 @@
+#include "test-tool.h"
+#include "hex.h"
+
+int cmd_hash_impl(int ac, const char **av, int algo)
+{
+ git_hash_ctx ctx;
+ unsigned char hash[GIT_MAX_HEXSZ];
+ unsigned bufsz = 8192;
+ int binary = 0;
+ char *buffer;
+ const struct git_hash_algo *algop = &hash_algos[algo];
+
+ if (ac == 2) {
+ if (!strcmp(av[1], "-b"))
+ binary = 1;
+ else
+ bufsz = strtoul(av[1], NULL, 10) * 1024 * 1024;
+ }
+
+ if (!bufsz)
+ bufsz = 8192;
+
+ while ((buffer = malloc(bufsz)) == NULL) {
+ fprintf(stderr, "bufsz %u is too big, halving...\n", bufsz);
+ bufsz /= 2;
+ if (bufsz < 1024)
+ die("OOPS");
+ }
+
+ algop->init_fn(&ctx);
+
+ while (1) {
+ ssize_t sz, this_sz;
+ char *cp = buffer;
+ unsigned room = bufsz;
+ this_sz = 0;
+ while (room) {
+ sz = xread(0, cp, room);
+ if (sz == 0)
+ break;
+ if (sz < 0)
+ die_errno("test-hash");
+ this_sz += sz;
+ cp += sz;
+ room -= sz;
+ }
+ if (this_sz == 0)
+ break;
+ algop->update_fn(&ctx, buffer, this_sz);
+ }
+ algop->final_fn(hash, &ctx);
+
+ if (binary)
+ fwrite(hash, 1, algop->rawsz, stdout);
+ else
+ puts(hash_to_hex_algop(hash, algop));
+ free(buffer);
+ return 0;
+}
diff --git a/t/helper/test-hashmap.c b/t/helper/test-hashmap.c
new file mode 100644
index 0000000..0eb0b3d
--- /dev/null
+++ b/t/helper/test-hashmap.c
@@ -0,0 +1,274 @@
+#include "test-tool.h"
+#include "git-compat-util.h"
+#include "hashmap.h"
+#include "strbuf.h"
+#include "string-list.h"
+
+struct test_entry
+{
+ int padding; /* hashmap entry no longer needs to be the first member */
+ struct hashmap_entry ent;
+ /* key and value as two \0-terminated strings */
+ char key[FLEX_ARRAY];
+};
+
+static const char *get_value(const struct test_entry *e)
+{
+ return e->key + strlen(e->key) + 1;
+}
+
+static int test_entry_cmp(const void *cmp_data,
+ const struct hashmap_entry *eptr,
+ const struct hashmap_entry *entry_or_key,
+ const void *keydata)
+{
+ const int ignore_case = cmp_data ? *((int *)cmp_data) : 0;
+ const struct test_entry *e1, *e2;
+ const char *key = keydata;
+
+ e1 = container_of(eptr, const struct test_entry, ent);
+ e2 = container_of(entry_or_key, const struct test_entry, ent);
+
+ if (ignore_case)
+ return strcasecmp(e1->key, key ? key : e2->key);
+ else
+ return strcmp(e1->key, key ? key : e2->key);
+}
+
+static struct test_entry *alloc_test_entry(unsigned int hash,
+ char *key, char *value)
+{
+ size_t klen = strlen(key);
+ size_t vlen = strlen(value);
+ struct test_entry *entry = xmalloc(st_add4(sizeof(*entry), klen, vlen, 2));
+ hashmap_entry_init(&entry->ent, hash);
+ memcpy(entry->key, key, klen + 1);
+ memcpy(entry->key + klen + 1, value, vlen + 1);
+ return entry;
+}
+
+#define HASH_METHOD_FNV 0
+#define HASH_METHOD_I 1
+#define HASH_METHOD_IDIV10 2
+#define HASH_METHOD_0 3
+#define HASH_METHOD_X2 4
+#define TEST_SPARSE 8
+#define TEST_ADD 16
+#define TEST_SIZE 100000
+
+static unsigned int hash(unsigned int method, unsigned int i, const char *key)
+{
+ unsigned int hash = 0;
+ switch (method & 3)
+ {
+ case HASH_METHOD_FNV:
+ hash = strhash(key);
+ break;
+ case HASH_METHOD_I:
+ hash = i;
+ break;
+ case HASH_METHOD_IDIV10:
+ hash = i / 10;
+ break;
+ case HASH_METHOD_0:
+ hash = 0;
+ break;
+ }
+
+ if (method & HASH_METHOD_X2)
+ hash = 2 * hash;
+ return hash;
+}
+
+/*
+ * Test performance of hashmap.[ch]
+ * Usage: time echo "perfhashmap method rounds" | test-tool hashmap
+ */
+static void perf_hashmap(unsigned int method, unsigned int rounds)
+{
+ struct hashmap map;
+ char buf[16];
+ struct test_entry **entries;
+ unsigned int *hashes;
+ unsigned int i, j;
+
+ ALLOC_ARRAY(entries, TEST_SIZE);
+ ALLOC_ARRAY(hashes, TEST_SIZE);
+ for (i = 0; i < TEST_SIZE; i++) {
+ xsnprintf(buf, sizeof(buf), "%i", i);
+ entries[i] = alloc_test_entry(0, buf, "");
+ hashes[i] = hash(method, i, entries[i]->key);
+ }
+
+ if (method & TEST_ADD) {
+ /* test adding to the map */
+ for (j = 0; j < rounds; j++) {
+ hashmap_init(&map, test_entry_cmp, NULL, 0);
+
+ /* add entries */
+ for (i = 0; i < TEST_SIZE; i++) {
+ hashmap_entry_init(&entries[i]->ent, hashes[i]);
+ hashmap_add(&map, &entries[i]->ent);
+ }
+
+ hashmap_clear(&map);
+ }
+ } else {
+ /* test map lookups */
+ hashmap_init(&map, test_entry_cmp, NULL, 0);
+
+ /* fill the map (sparsely if specified) */
+ j = (method & TEST_SPARSE) ? TEST_SIZE / 10 : TEST_SIZE;
+ for (i = 0; i < j; i++) {
+ hashmap_entry_init(&entries[i]->ent, hashes[i]);
+ hashmap_add(&map, &entries[i]->ent);
+ }
+
+ for (j = 0; j < rounds; j++) {
+ for (i = 0; i < TEST_SIZE; i++) {
+ hashmap_get_from_hash(&map, hashes[i],
+ entries[i]->key);
+ }
+ }
+
+ hashmap_clear(&map);
+ }
+}
+
+#define DELIM " \t\r\n"
+
+/*
+ * Read stdin line by line and print result of commands to stdout:
+ *
+ * hash key -> strhash(key) memhash(key) strihash(key) memihash(key)
+ * put key value -> NULL / old value
+ * get key -> NULL / value
+ * remove key -> NULL / old value
+ * iterate -> key1 value1\nkey2 value2\n...
+ * size -> tablesize numentries
+ *
+ * perfhashmap method rounds -> test hashmap.[ch] performance
+ */
+int cmd__hashmap(int argc, const char **argv)
+{
+ struct string_list parts = STRING_LIST_INIT_NODUP;
+ struct strbuf line = STRBUF_INIT;
+ int icase;
+ struct hashmap map = HASHMAP_INIT(test_entry_cmp, &icase);
+
+ /* init hash map */
+ icase = argc > 1 && !strcmp("ignorecase", argv[1]);
+
+ /* process commands from stdin */
+ while (strbuf_getline(&line, stdin) != EOF) {
+ char *cmd, *p1, *p2;
+ unsigned int hash = 0;
+ struct test_entry *entry;
+
+ /* break line into command and up to two parameters */
+ string_list_setlen(&parts, 0);
+ string_list_split_in_place(&parts, line.buf, DELIM, 2);
+ string_list_remove_empty_items(&parts, 0);
+
+ /* ignore empty lines */
+ if (!parts.nr)
+ continue;
+ if (!*parts.items[0].string || *parts.items[0].string == '#')
+ continue;
+
+ cmd = parts.items[0].string;
+ p1 = parts.nr >= 1 ? parts.items[1].string : NULL;
+ p2 = parts.nr >= 2 ? parts.items[2].string : NULL;
+ if (p1)
+ hash = icase ? strihash(p1) : strhash(p1);
+
+ if (!strcmp("add", cmd) && p1 && p2) {
+
+ /* create entry with key = p1, value = p2 */
+ entry = alloc_test_entry(hash, p1, p2);
+
+ /* add to hashmap */
+ hashmap_add(&map, &entry->ent);
+
+ } else if (!strcmp("put", cmd) && p1 && p2) {
+
+ /* create entry with key = p1, value = p2 */
+ entry = alloc_test_entry(hash, p1, p2);
+
+ /* add / replace entry */
+ entry = hashmap_put_entry(&map, entry, ent);
+
+ /* print and free replaced entry, if any */
+ puts(entry ? get_value(entry) : "NULL");
+ free(entry);
+
+ } else if (!strcmp("get", cmd) && p1) {
+ /* lookup entry in hashmap */
+ entry = hashmap_get_entry_from_hash(&map, hash, p1,
+ struct test_entry, ent);
+
+ /* print result */
+ if (!entry)
+ puts("NULL");
+ hashmap_for_each_entry_from(&map, entry, ent)
+ puts(get_value(entry));
+
+ } else if (!strcmp("remove", cmd) && p1) {
+
+ /* setup static key */
+ struct hashmap_entry key;
+ struct hashmap_entry *rm;
+ hashmap_entry_init(&key, hash);
+
+ /* remove entry from hashmap */
+ rm = hashmap_remove(&map, &key, p1);
+ entry = rm ? container_of(rm, struct test_entry, ent)
+ : NULL;
+
+ /* print result and free entry*/
+ puts(entry ? get_value(entry) : "NULL");
+ free(entry);
+
+ } else if (!strcmp("iterate", cmd)) {
+ struct hashmap_iter iter;
+
+ hashmap_for_each_entry(&map, &iter, entry,
+ ent /* member name */)
+ printf("%s %s\n", entry->key, get_value(entry));
+
+ } else if (!strcmp("size", cmd)) {
+
+ /* print table sizes */
+ printf("%u %u\n", map.tablesize,
+ hashmap_get_size(&map));
+
+ } else if (!strcmp("intern", cmd) && p1) {
+
+ /* test that strintern works */
+ const char *i1 = strintern(p1);
+ const char *i2 = strintern(p1);
+ if (strcmp(i1, p1))
+ printf("strintern(%s) returns %s\n", p1, i1);
+ else if (i1 == p1)
+ printf("strintern(%s) returns input pointer\n", p1);
+ else if (i1 != i2)
+ printf("strintern(%s) != strintern(%s)", i1, i2);
+ else
+ printf("%s\n", i1);
+
+ } else if (!strcmp("perfhashmap", cmd) && p1 && p2) {
+
+ perf_hashmap(atoi(p1), atoi(p2));
+
+ } else {
+
+ printf("Unknown command %s\n", cmd);
+
+ }
+ }
+
+ string_list_clear(&parts, 0);
+ strbuf_release(&line);
+ hashmap_clear_and_free(&map, struct test_entry, ent);
+ return 0;
+}
diff --git a/t/helper/test-hexdump.c b/t/helper/test-hexdump.c
new file mode 100644
index 0000000..05f55ec
--- /dev/null
+++ b/t/helper/test-hexdump.c
@@ -0,0 +1,30 @@
+#include "test-tool.h"
+#include "git-compat-util.h"
+
+/*
+ * Read stdin and print a hexdump to stdout.
+ */
+int cmd__hexdump(int argc UNUSED, const char **argv UNUSED)
+{
+ char buf[1024];
+ ssize_t i, len;
+ int have_data = 0;
+
+ for (;;) {
+ len = xread(0, buf, sizeof(buf));
+ if (len < 0)
+ die_errno("failure reading stdin");
+ if (!len)
+ break;
+
+ have_data = 1;
+
+ for (i = 0; i < len; i++)
+ printf("%02x ", (unsigned char)buf[i]);
+ }
+
+ if (have_data)
+ putchar('\n');
+
+ return 0;
+}
diff --git a/t/helper/test-json-writer.c b/t/helper/test-json-writer.c
new file mode 100644
index 0000000..afe393f
--- /dev/null
+++ b/t/helper/test-json-writer.c
@@ -0,0 +1,592 @@
+#include "test-tool.h"
+#include "json-writer.h"
+#include "string-list.h"
+
+static const char *expect_obj1 = "{\"a\":\"abc\",\"b\":42,\"c\":true}";
+static const char *expect_obj2 = "{\"a\":-1,\"b\":2147483647,\"c\":0}";
+static const char *expect_obj3 = "{\"a\":0,\"b\":4294967295,\"c\":9223372036854775807}";
+static const char *expect_obj4 = "{\"t\":true,\"f\":false,\"n\":null}";
+static const char *expect_obj5 = "{\"abc\\tdef\":\"abc\\\\def\"}";
+static const char *expect_obj6 = "{\"a\":3.14}";
+
+static const char *pretty_obj1 = ("{\n"
+ " \"a\": \"abc\",\n"
+ " \"b\": 42,\n"
+ " \"c\": true\n"
+ "}");
+static const char *pretty_obj2 = ("{\n"
+ " \"a\": -1,\n"
+ " \"b\": 2147483647,\n"
+ " \"c\": 0\n"
+ "}");
+static const char *pretty_obj3 = ("{\n"
+ " \"a\": 0,\n"
+ " \"b\": 4294967295,\n"
+ " \"c\": 9223372036854775807\n"
+ "}");
+static const char *pretty_obj4 = ("{\n"
+ " \"t\": true,\n"
+ " \"f\": false,\n"
+ " \"n\": null\n"
+ "}");
+
+static struct json_writer obj1 = JSON_WRITER_INIT;
+static struct json_writer obj2 = JSON_WRITER_INIT;
+static struct json_writer obj3 = JSON_WRITER_INIT;
+static struct json_writer obj4 = JSON_WRITER_INIT;
+static struct json_writer obj5 = JSON_WRITER_INIT;
+static struct json_writer obj6 = JSON_WRITER_INIT;
+
+static void make_obj1(int pretty)
+{
+ jw_object_begin(&obj1, pretty);
+ {
+ jw_object_string(&obj1, "a", "abc");
+ jw_object_intmax(&obj1, "b", 42);
+ jw_object_true(&obj1, "c");
+ }
+ jw_end(&obj1);
+}
+
+static void make_obj2(int pretty)
+{
+ jw_object_begin(&obj2, pretty);
+ {
+ jw_object_intmax(&obj2, "a", -1);
+ jw_object_intmax(&obj2, "b", 0x7fffffff);
+ jw_object_intmax(&obj2, "c", 0);
+ }
+ jw_end(&obj2);
+}
+
+static void make_obj3(int pretty)
+{
+ jw_object_begin(&obj3, pretty);
+ {
+ jw_object_intmax(&obj3, "a", 0);
+ jw_object_intmax(&obj3, "b", 0xffffffff);
+ jw_object_intmax(&obj3, "c", 0x7fffffffffffffffULL);
+ }
+ jw_end(&obj3);
+}
+
+static void make_obj4(int pretty)
+{
+ jw_object_begin(&obj4, pretty);
+ {
+ jw_object_true(&obj4, "t");
+ jw_object_false(&obj4, "f");
+ jw_object_null(&obj4, "n");
+ }
+ jw_end(&obj4);
+}
+
+static void make_obj5(int pretty)
+{
+ jw_object_begin(&obj5, pretty);
+ {
+ jw_object_string(&obj5, "abc" "\x09" "def", "abc" "\\" "def");
+ }
+ jw_end(&obj5);
+}
+
+static void make_obj6(int pretty)
+{
+ jw_object_begin(&obj6, pretty);
+ {
+ jw_object_double(&obj6, "a", 2, 3.14159);
+ }
+ jw_end(&obj6);
+}
+
+static const char *expect_arr1 = "[\"abc\",42,true]";
+static const char *expect_arr2 = "[-1,2147483647,0]";
+static const char *expect_arr3 = "[0,4294967295,9223372036854775807]";
+static const char *expect_arr4 = "[true,false,null]";
+
+static const char *pretty_arr1 = ("[\n"
+ " \"abc\",\n"
+ " 42,\n"
+ " true\n"
+ "]");
+static const char *pretty_arr2 = ("[\n"
+ " -1,\n"
+ " 2147483647,\n"
+ " 0\n"
+ "]");
+static const char *pretty_arr3 = ("[\n"
+ " 0,\n"
+ " 4294967295,\n"
+ " 9223372036854775807\n"
+ "]");
+static const char *pretty_arr4 = ("[\n"
+ " true,\n"
+ " false,\n"
+ " null\n"
+ "]");
+
+static struct json_writer arr1 = JSON_WRITER_INIT;
+static struct json_writer arr2 = JSON_WRITER_INIT;
+static struct json_writer arr3 = JSON_WRITER_INIT;
+static struct json_writer arr4 = JSON_WRITER_INIT;
+
+static void make_arr1(int pretty)
+{
+ jw_array_begin(&arr1, pretty);
+ {
+ jw_array_string(&arr1, "abc");
+ jw_array_intmax(&arr1, 42);
+ jw_array_true(&arr1);
+ }
+ jw_end(&arr1);
+}
+
+static void make_arr2(int pretty)
+{
+ jw_array_begin(&arr2, pretty);
+ {
+ jw_array_intmax(&arr2, -1);
+ jw_array_intmax(&arr2, 0x7fffffff);
+ jw_array_intmax(&arr2, 0);
+ }
+ jw_end(&arr2);
+}
+
+static void make_arr3(int pretty)
+{
+ jw_array_begin(&arr3, pretty);
+ {
+ jw_array_intmax(&arr3, 0);
+ jw_array_intmax(&arr3, 0xffffffff);
+ jw_array_intmax(&arr3, 0x7fffffffffffffffULL);
+ }
+ jw_end(&arr3);
+}
+
+static void make_arr4(int pretty)
+{
+ jw_array_begin(&arr4, pretty);
+ {
+ jw_array_true(&arr4);
+ jw_array_false(&arr4);
+ jw_array_null(&arr4);
+ }
+ jw_end(&arr4);
+}
+
+static char *expect_nest1 =
+ "{\"obj1\":{\"a\":\"abc\",\"b\":42,\"c\":true},\"arr1\":[\"abc\",42,true]}";
+
+static struct json_writer nest1 = JSON_WRITER_INIT;
+
+static void make_nest1(int pretty)
+{
+ make_obj1(0);
+ make_arr1(0);
+
+ jw_object_begin(&nest1, pretty);
+ {
+ jw_object_sub_jw(&nest1, "obj1", &obj1);
+ jw_object_sub_jw(&nest1, "arr1", &arr1);
+ }
+ jw_end(&nest1);
+
+ jw_release(&obj1);
+ jw_release(&arr1);
+}
+
+static char *expect_inline1 =
+ "{\"obj1\":{\"a\":\"abc\",\"b\":42,\"c\":true},\"arr1\":[\"abc\",42,true]}";
+
+static char *pretty_inline1 =
+ ("{\n"
+ " \"obj1\": {\n"
+ " \"a\": \"abc\",\n"
+ " \"b\": 42,\n"
+ " \"c\": true\n"
+ " },\n"
+ " \"arr1\": [\n"
+ " \"abc\",\n"
+ " 42,\n"
+ " true\n"
+ " ]\n"
+ "}");
+
+static struct json_writer inline1 = JSON_WRITER_INIT;
+
+static void make_inline1(int pretty)
+{
+ jw_object_begin(&inline1, pretty);
+ {
+ jw_object_inline_begin_object(&inline1, "obj1");
+ {
+ jw_object_string(&inline1, "a", "abc");
+ jw_object_intmax(&inline1, "b", 42);
+ jw_object_true(&inline1, "c");
+ }
+ jw_end(&inline1);
+ jw_object_inline_begin_array(&inline1, "arr1");
+ {
+ jw_array_string(&inline1, "abc");
+ jw_array_intmax(&inline1, 42);
+ jw_array_true(&inline1);
+ }
+ jw_end(&inline1);
+ }
+ jw_end(&inline1);
+}
+
+static char *expect_inline2 =
+ "[[1,2],[3,4],{\"a\":\"abc\"}]";
+
+static char *pretty_inline2 =
+ ("[\n"
+ " [\n"
+ " 1,\n"
+ " 2\n"
+ " ],\n"
+ " [\n"
+ " 3,\n"
+ " 4\n"
+ " ],\n"
+ " {\n"
+ " \"a\": \"abc\"\n"
+ " }\n"
+ "]");
+
+static struct json_writer inline2 = JSON_WRITER_INIT;
+
+static void make_inline2(int pretty)
+{
+ jw_array_begin(&inline2, pretty);
+ {
+ jw_array_inline_begin_array(&inline2);
+ {
+ jw_array_intmax(&inline2, 1);
+ jw_array_intmax(&inline2, 2);
+ }
+ jw_end(&inline2);
+ jw_array_inline_begin_array(&inline2);
+ {
+ jw_array_intmax(&inline2, 3);
+ jw_array_intmax(&inline2, 4);
+ }
+ jw_end(&inline2);
+ jw_array_inline_begin_object(&inline2);
+ {
+ jw_object_string(&inline2, "a", "abc");
+ }
+ jw_end(&inline2);
+ }
+ jw_end(&inline2);
+}
+
+/*
+ * When super is compact, we expect subs to be compacted (even if originally
+ * pretty).
+ */
+static const char *expect_mixed1 =
+ ("{\"obj1\":{\"a\":\"abc\",\"b\":42,\"c\":true},"
+ "\"arr1\":[\"abc\",42,true]}");
+
+/*
+ * When super is pretty, a compact sub (obj1) is kept compact and a pretty
+ * sub (arr1) is re-indented.
+ */
+static const char *pretty_mixed1 =
+ ("{\n"
+ " \"obj1\": {\"a\":\"abc\",\"b\":42,\"c\":true},\n"
+ " \"arr1\": [\n"
+ " \"abc\",\n"
+ " 42,\n"
+ " true\n"
+ " ]\n"
+ "}");
+
+static struct json_writer mixed1 = JSON_WRITER_INIT;
+
+static void make_mixed1(int pretty)
+{
+ jw_init(&obj1);
+ jw_init(&arr1);
+
+ make_obj1(0); /* obj1 is compact */
+ make_arr1(1); /* arr1 is pretty */
+
+ jw_object_begin(&mixed1, pretty);
+ {
+ jw_object_sub_jw(&mixed1, "obj1", &obj1);
+ jw_object_sub_jw(&mixed1, "arr1", &arr1);
+ }
+ jw_end(&mixed1);
+
+ jw_release(&obj1);
+ jw_release(&arr1);
+}
+
+static void cmp(const char *test, const struct json_writer *jw, const char *exp)
+{
+ if (!strcmp(jw->json.buf, exp))
+ return;
+
+ printf("error[%s]: observed '%s' expected '%s'\n",
+ test, jw->json.buf, exp);
+ exit(1);
+}
+
+#define t(v) do { make_##v(0); cmp(#v, &v, expect_##v); jw_release(&v); } while (0)
+#define p(v) do { make_##v(1); cmp(#v, &v, pretty_##v); jw_release(&v); } while (0)
+
+/*
+ * Run some basic regression tests with some known patterns.
+ * These tests also demonstrate how to use the jw_ API.
+ */
+static int unit_tests(void)
+{
+ /* comptact (canonical) forms */
+ t(obj1);
+ t(obj2);
+ t(obj3);
+ t(obj4);
+ t(obj5);
+ t(obj6);
+
+ t(arr1);
+ t(arr2);
+ t(arr3);
+ t(arr4);
+
+ t(nest1);
+
+ t(inline1);
+ t(inline2);
+
+ jw_init(&obj1);
+ jw_init(&obj2);
+ jw_init(&obj3);
+ jw_init(&obj4);
+
+ jw_init(&arr1);
+ jw_init(&arr2);
+ jw_init(&arr3);
+ jw_init(&arr4);
+
+ jw_init(&inline1);
+ jw_init(&inline2);
+
+ /* pretty forms */
+ p(obj1);
+ p(obj2);
+ p(obj3);
+ p(obj4);
+
+ p(arr1);
+ p(arr2);
+ p(arr3);
+ p(arr4);
+
+ p(inline1);
+ p(inline2);
+
+ /* mixed forms */
+ t(mixed1);
+ p(mixed1);
+
+ return 0;
+}
+
+struct line {
+ struct string_list *parts;
+ size_t consumed_nr;
+ int nr;
+};
+
+static void get_s(struct line *line, char **s_in)
+{
+ if (line->consumed_nr > line->parts->nr)
+ die("line[%d]: expected: <s>", line->nr);
+ *s_in = line->parts->items[line->consumed_nr++].string;
+}
+
+static void get_i(struct line *line, intmax_t *s_in)
+{
+ char *s;
+ char *endptr;
+
+ get_s(line, &s);
+
+ *s_in = strtol(s, &endptr, 10);
+ if (*endptr || errno == ERANGE)
+ die("line[%d]: invalid integer value", line->nr);
+}
+
+static void get_d(struct line *line, double *s_in)
+{
+ char *s;
+ char *endptr;
+
+ get_s(line, &s);
+
+ *s_in = strtod(s, &endptr);
+ if (*endptr || errno == ERANGE)
+ die("line[%d]: invalid float value", line->nr);
+}
+
+static int pretty;
+
+#define MAX_LINE_LENGTH (64 * 1024)
+
+static char *get_trimmed_line(char *buf, int buf_size)
+{
+ int len;
+
+ if (!fgets(buf, buf_size, stdin))
+ return NULL;
+
+ len = strlen(buf);
+ while (len > 0) {
+ char c = buf[len - 1];
+ if (c == '\n' || c == '\r' || c == ' ' || c == '\t')
+ buf[--len] = 0;
+ else
+ break;
+ }
+
+ while (*buf == ' ' || *buf == '\t')
+ buf++;
+
+ return buf;
+}
+
+static int scripted(void)
+{
+ struct string_list parts = STRING_LIST_INIT_NODUP;
+ struct json_writer jw = JSON_WRITER_INIT;
+ char buf[MAX_LINE_LENGTH];
+ char *line;
+ int line_nr = 0;
+
+ line = get_trimmed_line(buf, MAX_LINE_LENGTH);
+ if (!line)
+ return 0;
+
+ if (!strcmp(line, "object"))
+ jw_object_begin(&jw, pretty);
+ else if (!strcmp(line, "array"))
+ jw_array_begin(&jw, pretty);
+ else
+ die("expected first line to be 'object' or 'array'");
+
+ while ((line = get_trimmed_line(buf, MAX_LINE_LENGTH)) != NULL) {
+ struct line state = { 0 };
+ char *verb;
+ char *key;
+ char *s_value;
+ intmax_t i_value;
+ double d_value;
+
+ state.parts = &parts;
+ state.nr = ++line_nr;
+
+ /* break line into command and zero or more tokens */
+ string_list_setlen(&parts, 0);
+ string_list_split_in_place(&parts, line, " ", -1);
+ string_list_remove_empty_items(&parts, 0);
+
+ /* ignore empty lines */
+ if (!parts.nr || !*parts.items[0].string)
+ continue;
+
+ verb = parts.items[state.consumed_nr++].string;
+
+ if (!strcmp(verb, "end")) {
+ jw_end(&jw);
+ }
+ else if (!strcmp(verb, "object-string")) {
+ get_s(&state, &key);
+ get_s(&state, &s_value);
+ jw_object_string(&jw, key, s_value);
+ }
+ else if (!strcmp(verb, "object-int")) {
+ get_s(&state, &key);
+ get_i(&state, &i_value);
+ jw_object_intmax(&jw, key, i_value);
+ }
+ else if (!strcmp(verb, "object-double")) {
+ get_s(&state, &key);
+ get_i(&state, &i_value);
+ get_d(&state, &d_value);
+ jw_object_double(&jw, key, i_value, d_value);
+ }
+ else if (!strcmp(verb, "object-true")) {
+ get_s(&state, &key);
+ jw_object_true(&jw, key);
+ }
+ else if (!strcmp(verb, "object-false")) {
+ get_s(&state, &key);
+ jw_object_false(&jw, key);
+ }
+ else if (!strcmp(verb, "object-null")) {
+ get_s(&state, &key);
+ jw_object_null(&jw, key);
+ }
+ else if (!strcmp(verb, "object-object")) {
+ get_s(&state, &key);
+ jw_object_inline_begin_object(&jw, key);
+ }
+ else if (!strcmp(verb, "object-array")) {
+ get_s(&state, &key);
+ jw_object_inline_begin_array(&jw, key);
+ }
+ else if (!strcmp(verb, "array-string")) {
+ get_s(&state, &s_value);
+ jw_array_string(&jw, s_value);
+ }
+ else if (!strcmp(verb, "array-int")) {
+ get_i(&state, &i_value);
+ jw_array_intmax(&jw, i_value);
+ }
+ else if (!strcmp(verb, "array-double")) {
+ get_i(&state, &i_value);
+ get_d(&state, &d_value);
+ jw_array_double(&jw, i_value, d_value);
+ }
+ else if (!strcmp(verb, "array-true"))
+ jw_array_true(&jw);
+ else if (!strcmp(verb, "array-false"))
+ jw_array_false(&jw);
+ else if (!strcmp(verb, "array-null"))
+ jw_array_null(&jw);
+ else if (!strcmp(verb, "array-object"))
+ jw_array_inline_begin_object(&jw);
+ else if (!strcmp(verb, "array-array"))
+ jw_array_inline_begin_array(&jw);
+ else
+ die("unrecognized token: '%s'", verb);
+ }
+
+ if (!jw_is_terminated(&jw))
+ die("json not terminated: '%s'", jw.json.buf);
+
+ printf("%s\n", jw.json.buf);
+
+ jw_release(&jw);
+ string_list_clear(&parts, 0);
+ return 0;
+}
+
+int cmd__json_writer(int argc, const char **argv)
+{
+ argc--; /* skip over "json-writer" arg */
+ argv++;
+
+ if (argc > 0 && argv[0][0] == '-') {
+ if (!strcmp(argv[0], "-u") || !strcmp(argv[0], "--unit"))
+ return unit_tests();
+
+ if (!strcmp(argv[0], "-p") || !strcmp(argv[0], "--pretty"))
+ pretty = 1;
+ }
+
+ return scripted();
+}
diff --git a/t/helper/test-lazy-init-name-hash.c b/t/helper/test-lazy-init-name-hash.c
new file mode 100644
index 0000000..187a115
--- /dev/null
+++ b/t/helper/test-lazy-init-name-hash.c
@@ -0,0 +1,267 @@
+#define USE_THE_INDEX_VARIABLE
+#include "test-tool.h"
+#include "environment.h"
+#include "name-hash.h"
+#include "parse-options.h"
+#include "read-cache-ll.h"
+#include "repository.h"
+#include "setup.h"
+#include "trace.h"
+
+static int single;
+static int multi;
+static int count = 1;
+static int dump;
+static int perf;
+static int analyze;
+static int analyze_step;
+
+/*
+ * Dump the contents of the "dir" and "name" hash tables to stdout.
+ * If you sort the result, you can compare it with the other type
+ * mode and verify that both single and multi produce the same set.
+ */
+static void dump_run(void)
+{
+ struct hashmap_iter iter_dir;
+ struct hashmap_iter iter_cache;
+
+ /* Stolen from name-hash.c */
+ struct dir_entry {
+ struct hashmap_entry ent;
+ struct dir_entry *parent;
+ int nr;
+ unsigned int namelen;
+ char name[FLEX_ARRAY];
+ };
+
+ struct dir_entry *dir;
+ struct cache_entry *ce;
+
+ repo_read_index(the_repository);
+ if (single) {
+ test_lazy_init_name_hash(&the_index, 0);
+ } else {
+ int nr_threads_used = test_lazy_init_name_hash(&the_index, 1);
+ if (!nr_threads_used)
+ die("non-threaded code path used");
+ }
+
+ hashmap_for_each_entry(&the_index.dir_hash, &iter_dir, dir,
+ ent /* member name */)
+ printf("dir %08x %7d %s\n", dir->ent.hash, dir->nr, dir->name);
+
+ hashmap_for_each_entry(&the_index.name_hash, &iter_cache, ce,
+ ent /* member name */)
+ printf("name %08x %s\n", ce->ent.hash, ce->name);
+
+ discard_index(&the_index);
+}
+
+/*
+ * Run the single or multi threaded version "count" times and
+ * report on the time taken.
+ */
+static uint64_t time_runs(int try_threaded)
+{
+ uint64_t t0, t1, t2;
+ uint64_t sum = 0;
+ uint64_t avg;
+ int nr_threads_used;
+ int i;
+
+ for (i = 0; i < count; i++) {
+ t0 = getnanotime();
+ repo_read_index(the_repository);
+ t1 = getnanotime();
+ nr_threads_used = test_lazy_init_name_hash(&the_index, try_threaded);
+ t2 = getnanotime();
+
+ sum += (t2 - t1);
+
+ if (try_threaded && !nr_threads_used)
+ die("non-threaded code path used");
+
+ if (nr_threads_used)
+ printf("%f %f %d multi %d\n",
+ ((double)(t1 - t0))/1000000000,
+ ((double)(t2 - t1))/1000000000,
+ the_index.cache_nr,
+ nr_threads_used);
+ else
+ printf("%f %f %d single\n",
+ ((double)(t1 - t0))/1000000000,
+ ((double)(t2 - t1))/1000000000,
+ the_index.cache_nr);
+ fflush(stdout);
+
+ discard_index(&the_index);
+ }
+
+ avg = sum / count;
+ if (count > 1)
+ printf("avg %f %s\n",
+ (double)avg/1000000000,
+ (try_threaded) ? "multi" : "single");
+
+ return avg;
+}
+
+/*
+ * Try a series of runs varying the "istate->cache_nr" and
+ * try to find a good value for the multi-threaded criteria.
+ */
+static void analyze_run(void)
+{
+ uint64_t t1s, t1m, t2s, t2m;
+ int cache_nr_limit;
+ int nr_threads_used = 0;
+ int i;
+ int nr;
+
+ repo_read_index(the_repository);
+ cache_nr_limit = the_index.cache_nr;
+ discard_index(&the_index);
+
+ nr = analyze;
+ while (1) {
+ uint64_t sum_single = 0;
+ uint64_t sum_multi = 0;
+ uint64_t avg_single;
+ uint64_t avg_multi;
+
+ if (nr > cache_nr_limit)
+ nr = cache_nr_limit;
+
+ for (i = 0; i < count; i++) {
+ repo_read_index(the_repository);
+ the_index.cache_nr = nr; /* cheap truncate of index */
+ t1s = getnanotime();
+ test_lazy_init_name_hash(&the_index, 0);
+ t2s = getnanotime();
+ sum_single += (t2s - t1s);
+ the_index.cache_nr = cache_nr_limit;
+ discard_index(&the_index);
+
+ repo_read_index(the_repository);
+ the_index.cache_nr = nr; /* cheap truncate of index */
+ t1m = getnanotime();
+ nr_threads_used = test_lazy_init_name_hash(&the_index, 1);
+ t2m = getnanotime();
+ sum_multi += (t2m - t1m);
+ the_index.cache_nr = cache_nr_limit;
+ discard_index(&the_index);
+
+ if (!nr_threads_used)
+ printf(" [size %8d] [single %f] non-threaded code path used\n",
+ nr, ((double)(t2s - t1s))/1000000000);
+ else
+ printf(" [size %8d] [single %f] %c [multi %f %d]\n",
+ nr,
+ ((double)(t2s - t1s))/1000000000,
+ (((t2s - t1s) < (t2m - t1m)) ? '<' : '>'),
+ ((double)(t2m - t1m))/1000000000,
+ nr_threads_used);
+ fflush(stdout);
+ }
+ if (count > 1) {
+ avg_single = sum_single / count;
+ avg_multi = sum_multi / count;
+ if (!nr_threads_used)
+ printf("avg [size %8d] [single %f]\n",
+ nr,
+ (double)avg_single/1000000000);
+ else
+ printf("avg [size %8d] [single %f] %c [multi %f %d]\n",
+ nr,
+ (double)avg_single/1000000000,
+ (avg_single < avg_multi ? '<' : '>'),
+ (double)avg_multi/1000000000,
+ nr_threads_used);
+ fflush(stdout);
+ }
+
+ if (nr >= cache_nr_limit)
+ return;
+ nr += analyze_step;
+ }
+}
+
+int cmd__lazy_init_name_hash(int argc, const char **argv)
+{
+ const char *usage[] = {
+ "test-tool lazy-init-name-hash -d (-s | -m)",
+ "test-tool lazy-init-name-hash -p [-c c]",
+ "test-tool lazy-init-name-hash -a a [--step s] [-c c]",
+ "test-tool lazy-init-name-hash (-s | -m) [-c c]",
+ "test-tool lazy-init-name-hash -s -m [-c c]",
+ NULL
+ };
+ struct option options[] = {
+ OPT_BOOL('s', "single", &single, "run single-threaded code"),
+ OPT_BOOL('m', "multi", &multi, "run multi-threaded code"),
+ OPT_INTEGER('c', "count", &count, "number of passes"),
+ OPT_BOOL('d', "dump", &dump, "dump hash tables"),
+ OPT_BOOL('p', "perf", &perf, "compare single vs multi"),
+ OPT_INTEGER('a', "analyze", &analyze, "analyze different multi sizes"),
+ OPT_INTEGER(0, "step", &analyze_step, "analyze step factor"),
+ OPT_END(),
+ };
+ const char *prefix;
+ uint64_t avg_single, avg_multi;
+
+ prefix = setup_git_directory();
+
+ argc = parse_options(argc, argv, prefix, options, usage, 0);
+
+ /*
+ * istate->dir_hash is only created when ignore_case is set.
+ */
+ ignore_case = 1;
+
+ if (dump) {
+ if (perf || analyze > 0)
+ die("cannot combine dump, perf, or analyze");
+ if (count > 1)
+ die("count not valid with dump");
+ if (single && multi)
+ die("cannot use both single and multi with dump");
+ if (!single && !multi)
+ die("dump requires either single or multi");
+ dump_run();
+ return 0;
+ }
+
+ if (perf) {
+ if (analyze > 0)
+ die("cannot combine dump, perf, or analyze");
+ if (single || multi)
+ die("cannot use single or multi with perf");
+ avg_single = time_runs(0);
+ avg_multi = time_runs(1);
+ if (avg_multi > avg_single)
+ die("multi is slower");
+ return 0;
+ }
+
+ if (analyze) {
+ if (analyze < 500)
+ die("analyze must be at least 500");
+ if (!analyze_step)
+ analyze_step = analyze;
+ if (single || multi)
+ die("cannot use single or multi with analyze");
+ analyze_run();
+ return 0;
+ }
+
+ if (!single && !multi)
+ die("require either -s or -m or both");
+
+ if (single)
+ time_runs(0);
+ if (multi)
+ time_runs(1);
+
+ return 0;
+}
diff --git a/t/helper/test-match-trees.c b/t/helper/test-match-trees.c
new file mode 100644
index 0000000..d0db5ff
--- /dev/null
+++ b/t/helper/test-match-trees.c
@@ -0,0 +1,31 @@
+#include "test-tool.h"
+#include "hex.h"
+#include "match-trees.h"
+#include "object-name.h"
+#include "repository.h"
+#include "setup.h"
+#include "tree.h"
+
+int cmd__match_trees(int ac UNUSED, const char **av)
+{
+ struct object_id hash1, hash2, shifted;
+ struct tree *one, *two;
+
+ setup_git_directory();
+
+ if (repo_get_oid(the_repository, av[1], &hash1))
+ die("cannot parse %s as an object name", av[1]);
+ if (repo_get_oid(the_repository, av[2], &hash2))
+ die("cannot parse %s as an object name", av[2]);
+ one = parse_tree_indirect(&hash1);
+ if (!one)
+ die("not a tree-ish %s", av[1]);
+ two = parse_tree_indirect(&hash2);
+ if (!two)
+ die("not a tree-ish %s", av[2]);
+
+ shift_tree(the_repository, &one->object.oid, &two->object.oid, &shifted, -1);
+ printf("shifted: %s\n", oid_to_hex(&shifted));
+
+ return 0;
+}
diff --git a/t/helper/test-mergesort.c b/t/helper/test-mergesort.c
new file mode 100644
index 0000000..42ccc87
--- /dev/null
+++ b/t/helper/test-mergesort.c
@@ -0,0 +1,406 @@
+#include "test-tool.h"
+#include "mem-pool.h"
+#include "mergesort.h"
+#include "strbuf.h"
+
+static uint32_t minstd_rand(uint32_t *state)
+{
+ *state = (uint64_t)*state * 48271 % 2147483647;
+ return *state;
+}
+
+struct line {
+ char *text;
+ struct line *next;
+};
+
+DEFINE_LIST_SORT(static, sort_lines, struct line, next);
+
+static int compare_strings(const struct line *x, const struct line *y)
+{
+ return strcmp(x->text, y->text);
+}
+
+static int sort_stdin(void)
+{
+ struct line *lines;
+ struct line **tail = &lines;
+ struct strbuf sb = STRBUF_INIT;
+ struct mem_pool lines_pool;
+ char *p;
+
+ strbuf_read(&sb, 0, 0);
+
+ /*
+ * Split by newline, but don't create an item
+ * for the empty string after the last separator.
+ */
+ if (sb.len && sb.buf[sb.len - 1] == '\n')
+ strbuf_setlen(&sb, sb.len - 1);
+
+ mem_pool_init(&lines_pool, 0);
+ p = sb.buf;
+ for (;;) {
+ char *eol = strchr(p, '\n');
+ struct line *line = mem_pool_alloc(&lines_pool, sizeof(*line));
+ line->text = p;
+ *tail = line;
+ tail = &line->next;
+ if (!eol)
+ break;
+ *eol = '\0';
+ p = eol + 1;
+ }
+ *tail = NULL;
+
+ sort_lines(&lines, compare_strings);
+
+ while (lines) {
+ puts(lines->text);
+ lines = lines->next;
+ }
+ return 0;
+}
+
+static void dist_sawtooth(int *arr, int n, int m)
+{
+ int i;
+ for (i = 0; i < n; i++)
+ arr[i] = i % m;
+}
+
+static void dist_rand(int *arr, int n, int m)
+{
+ int i;
+ uint32_t seed = 1;
+ for (i = 0; i < n; i++)
+ arr[i] = minstd_rand(&seed) % m;
+}
+
+static void dist_stagger(int *arr, int n, int m)
+{
+ int i;
+ for (i = 0; i < n; i++)
+ arr[i] = (i * m + i) % n;
+}
+
+static void dist_plateau(int *arr, int n, int m)
+{
+ int i;
+ for (i = 0; i < n; i++)
+ arr[i] = (i < m) ? i : m;
+}
+
+static void dist_shuffle(int *arr, int n, int m)
+{
+ int i, j, k;
+ uint32_t seed = 1;
+ for (i = j = 0, k = 1; i < n; i++)
+ arr[i] = minstd_rand(&seed) % m ? (j += 2) : (k += 2);
+}
+
+#define DIST(name) { #name, dist_##name }
+
+static struct dist {
+ const char *name;
+ void (*fn)(int *arr, int n, int m);
+} dist[] = {
+ DIST(sawtooth),
+ DIST(rand),
+ DIST(stagger),
+ DIST(plateau),
+ DIST(shuffle),
+};
+
+static const struct dist *get_dist_by_name(const char *name)
+{
+ int i;
+ for (i = 0; i < ARRAY_SIZE(dist); i++) {
+ if (!strcmp(dist[i].name, name))
+ return &dist[i];
+ }
+ return NULL;
+}
+
+static void mode_copy(int *arr, int n)
+{
+ /* nothing */
+}
+
+static void mode_reverse(int *arr, int n)
+{
+ int i, j;
+ for (i = 0, j = n - 1; i < j; i++, j--)
+ SWAP(arr[i], arr[j]);
+}
+
+static void mode_reverse_1st_half(int *arr, int n)
+{
+ mode_reverse(arr, n / 2);
+}
+
+static void mode_reverse_2nd_half(int *arr, int n)
+{
+ int half = n / 2;
+ mode_reverse(arr + half, n - half);
+}
+
+static int compare_ints(const void *av, const void *bv)
+{
+ const int *ap = av, *bp = bv;
+ int a = *ap, b = *bp;
+ return (a > b) - (a < b);
+}
+
+static void mode_sort(int *arr, int n)
+{
+ QSORT(arr, n, compare_ints);
+}
+
+static void mode_dither(int *arr, int n)
+{
+ int i;
+ for (i = 0; i < n; i++)
+ arr[i] += i % 5;
+}
+
+static void unriffle(int *arr, int n, int *tmp)
+{
+ int i, j;
+ COPY_ARRAY(tmp, arr, n);
+ for (i = j = 0; i < n; i += 2)
+ arr[j++] = tmp[i];
+ for (i = 1; i < n; i += 2)
+ arr[j++] = tmp[i];
+}
+
+static void unriffle_recursively(int *arr, int n, int *tmp)
+{
+ if (n > 1) {
+ int half = n / 2;
+ unriffle(arr, n, tmp);
+ unriffle_recursively(arr, half, tmp);
+ unriffle_recursively(arr + half, n - half, tmp);
+ }
+}
+
+static void mode_unriffle(int *arr, int n)
+{
+ int *tmp;
+ ALLOC_ARRAY(tmp, n);
+ unriffle_recursively(arr, n, tmp);
+ free(tmp);
+}
+
+static unsigned int prev_pow2(unsigned int n)
+{
+ unsigned int pow2 = 1;
+ while (pow2 * 2 < n)
+ pow2 *= 2;
+ return pow2;
+}
+
+static void unriffle_recursively_skewed(int *arr, int n, int *tmp)
+{
+ if (n > 1) {
+ int pow2 = prev_pow2(n);
+ int rest = n - pow2;
+ unriffle(arr + pow2 - rest, rest * 2, tmp);
+ unriffle_recursively_skewed(arr, pow2, tmp);
+ unriffle_recursively_skewed(arr + pow2, rest, tmp);
+ }
+}
+
+static void mode_unriffle_skewed(int *arr, int n)
+{
+ int *tmp;
+ ALLOC_ARRAY(tmp, n);
+ unriffle_recursively_skewed(arr, n, tmp);
+ free(tmp);
+}
+
+#define MODE(name) { #name, mode_##name }
+
+static struct mode {
+ const char *name;
+ void (*fn)(int *arr, int n);
+} mode[] = {
+ MODE(copy),
+ MODE(reverse),
+ MODE(reverse_1st_half),
+ MODE(reverse_2nd_half),
+ MODE(sort),
+ MODE(dither),
+ MODE(unriffle),
+ MODE(unriffle_skewed),
+};
+
+static const struct mode *get_mode_by_name(const char *name)
+{
+ int i;
+ for (i = 0; i < ARRAY_SIZE(mode); i++) {
+ if (!strcmp(mode[i].name, name))
+ return &mode[i];
+ }
+ return NULL;
+}
+
+static int generate(int argc, const char **argv)
+{
+ const struct dist *dist = NULL;
+ const struct mode *mode = NULL;
+ int i, n, m, *arr;
+
+ if (argc != 4)
+ return 1;
+
+ dist = get_dist_by_name(argv[0]);
+ mode = get_mode_by_name(argv[1]);
+ n = strtol(argv[2], NULL, 10);
+ m = strtol(argv[3], NULL, 10);
+ if (!dist || !mode)
+ return 1;
+
+ ALLOC_ARRAY(arr, n);
+ dist->fn(arr, n, m);
+ mode->fn(arr, n);
+ for (i = 0; i < n; i++)
+ printf("%08x\n", arr[i]);
+ free(arr);
+ return 0;
+}
+
+static struct stats {
+ int get_next, set_next, compare;
+} stats;
+
+struct number {
+ int value, rank;
+ struct number *next;
+};
+
+DEFINE_LIST_SORT_DEBUG(static, sort_numbers, struct number, next,
+ stats.get_next++, stats.set_next++);
+
+static int compare_numbers(const struct number *an, const struct number *bn)
+{
+ int a = an->value, b = bn->value;
+ stats.compare++;
+ return (a > b) - (a < b);
+}
+
+static void clear_numbers(struct number *list)
+{
+ while (list) {
+ struct number *next = list->next;
+ free(list);
+ list = next;
+ }
+}
+
+static int test(const struct dist *dist, const struct mode *mode, int n, int m)
+{
+ int *arr;
+ size_t i;
+ struct number *curr, *list, **tail;
+ int is_sorted = 1;
+ int is_stable = 1;
+ const char *verdict;
+ int result = -1;
+
+ ALLOC_ARRAY(arr, n);
+ dist->fn(arr, n, m);
+ mode->fn(arr, n);
+ for (i = 0, tail = &list; i < n; i++) {
+ curr = xmalloc(sizeof(*curr));
+ curr->value = arr[i];
+ curr->rank = i;
+ *tail = curr;
+ tail = &curr->next;
+ }
+ *tail = NULL;
+
+ stats.get_next = stats.set_next = stats.compare = 0;
+ sort_numbers(&list, compare_numbers);
+
+ QSORT(arr, n, compare_ints);
+ for (i = 0, curr = list; i < n && curr; i++, curr = curr->next) {
+ if (arr[i] != curr->value)
+ is_sorted = 0;
+ if (curr->next && curr->value == curr->next->value &&
+ curr->rank >= curr->next->rank)
+ is_stable = 0;
+ }
+ if (i < n) {
+ verdict = "too short";
+ } else if (curr) {
+ verdict = "too long";
+ } else if (!is_sorted) {
+ verdict = "not sorted";
+ } else if (!is_stable) {
+ verdict = "unstable";
+ } else {
+ verdict = "OK";
+ result = 0;
+ }
+
+ printf("%-9s %-16s %8d %8d %8d %8d %8d %s\n",
+ dist->name, mode->name, n, m, stats.get_next, stats.set_next,
+ stats.compare, verdict);
+
+ clear_numbers(list);
+ free(arr);
+
+ return result;
+}
+
+/*
+ * A version of the qsort certification program from "Engineering a Sort
+ * Function" by Bentley and McIlroy, Software—Practice and Experience,
+ * Volume 23, Issue 11, 1249–1265 (November 1993).
+ */
+static int run_tests(int argc, const char **argv)
+{
+ const char *argv_default[] = { "100", "1023", "1024", "1025" };
+ if (!argc)
+ return run_tests(ARRAY_SIZE(argv_default), argv_default);
+ printf("%-9s %-16s %8s %8s %8s %8s %8s %s\n",
+ "distribut", "mode", "n", "m", "get_next", "set_next",
+ "compare", "verdict");
+ while (argc--) {
+ int i, j, m, n = strtol(*argv++, NULL, 10);
+ for (i = 0; i < ARRAY_SIZE(dist); i++) {
+ for (j = 0; j < ARRAY_SIZE(mode); j++) {
+ for (m = 1; m < 2 * n; m *= 2) {
+ if (test(&dist[i], &mode[j], n, m))
+ return 1;
+ }
+ }
+ }
+ }
+ return 0;
+}
+
+int cmd__mergesort(int argc, const char **argv)
+{
+ int i;
+ const char *sep;
+
+ if (argc == 6 && !strcmp(argv[1], "generate"))
+ return generate(argc - 2, argv + 2);
+ if (argc == 2 && !strcmp(argv[1], "sort"))
+ return sort_stdin();
+ if (argc > 1 && !strcmp(argv[1], "test"))
+ return run_tests(argc - 2, argv + 2);
+ fprintf(stderr, "usage: test-tool mergesort generate <distribution> <mode> <n> <m>\n");
+ fprintf(stderr, " or: test-tool mergesort sort\n");
+ fprintf(stderr, " or: test-tool mergesort test [<n>...]\n");
+ fprintf(stderr, "\n");
+ for (i = 0, sep = "distributions: "; i < ARRAY_SIZE(dist); i++, sep = ", ")
+ fprintf(stderr, "%s%s", sep, dist[i].name);
+ fprintf(stderr, "\n");
+ for (i = 0, sep = "modes: "; i < ARRAY_SIZE(mode); i++, sep = ", ")
+ fprintf(stderr, "%s%s", sep, mode[i].name);
+ fprintf(stderr, "\n");
+ return 129;
+}
diff --git a/t/helper/test-mktemp.c b/t/helper/test-mktemp.c
new file mode 100644
index 0000000..2290688
--- /dev/null
+++ b/t/helper/test-mktemp.c
@@ -0,0 +1,15 @@
+/*
+ * test-mktemp.c: code to exercise the creation of temporary files
+ */
+#include "test-tool.h"
+#include "git-compat-util.h"
+
+int cmd__mktemp(int argc, const char **argv)
+{
+ if (argc != 2)
+ usage("Expected 1 parameter defining the temporary file template");
+
+ xmkstemp(xstrdup(argv[1]));
+
+ return 0;
+}
diff --git a/t/helper/test-oid-array.c b/t/helper/test-oid-array.c
new file mode 100644
index 0000000..aafe398
--- /dev/null
+++ b/t/helper/test-oid-array.c
@@ -0,0 +1,45 @@
+#include "test-tool.h"
+#include "hex.h"
+#include "oid-array.h"
+#include "setup.h"
+#include "strbuf.h"
+
+static int print_oid(const struct object_id *oid, void *data UNUSED)
+{
+ puts(oid_to_hex(oid));
+ return 0;
+}
+
+int cmd__oid_array(int argc UNUSED, const char **argv UNUSED)
+{
+ struct oid_array array = OID_ARRAY_INIT;
+ struct strbuf line = STRBUF_INIT;
+ int nongit_ok;
+
+ setup_git_directory_gently(&nongit_ok);
+
+ while (strbuf_getline(&line, stdin) != EOF) {
+ const char *arg;
+ struct object_id oid;
+
+ if (skip_prefix(line.buf, "append ", &arg)) {
+ if (get_oid_hex(arg, &oid))
+ die("not a hexadecimal oid: %s", arg);
+ oid_array_append(&array, &oid);
+ } else if (skip_prefix(line.buf, "lookup ", &arg)) {
+ if (get_oid_hex(arg, &oid))
+ die("not a hexadecimal oid: %s", arg);
+ printf("%d\n", oid_array_lookup(&array, &oid));
+ } else if (!strcmp(line.buf, "clear"))
+ oid_array_clear(&array);
+ else if (!strcmp(line.buf, "for_each_unique"))
+ oid_array_for_each_unique(&array, print_oid, NULL);
+ else
+ die("unknown command: %s", line.buf);
+ }
+
+ strbuf_release(&line);
+ oid_array_clear(&array);
+
+ return 0;
+}
diff --git a/t/helper/test-oidmap.c b/t/helper/test-oidmap.c
new file mode 100644
index 0000000..bd30244
--- /dev/null
+++ b/t/helper/test-oidmap.c
@@ -0,0 +1,123 @@
+#include "test-tool.h"
+#include "hex.h"
+#include "object-name.h"
+#include "oidmap.h"
+#include "repository.h"
+#include "setup.h"
+#include "strbuf.h"
+#include "string-list.h"
+
+/* key is an oid and value is a name (could be a refname for example) */
+struct test_entry {
+ struct oidmap_entry entry;
+ char name[FLEX_ARRAY];
+};
+
+#define DELIM " \t\r\n"
+
+/*
+ * Read stdin line by line and print result of commands to stdout:
+ *
+ * hash oidkey -> sha1hash(oidkey)
+ * put oidkey namevalue -> NULL / old namevalue
+ * get oidkey -> NULL / namevalue
+ * remove oidkey -> NULL / old namevalue
+ * iterate -> oidkey1 namevalue1\noidkey2 namevalue2\n...
+ *
+ */
+int cmd__oidmap(int argc UNUSED, const char **argv UNUSED)
+{
+ struct string_list parts = STRING_LIST_INIT_NODUP;
+ struct strbuf line = STRBUF_INIT;
+ struct oidmap map = OIDMAP_INIT;
+
+ setup_git_directory();
+
+ /* init oidmap */
+ oidmap_init(&map, 0);
+
+ /* process commands from stdin */
+ while (strbuf_getline(&line, stdin) != EOF) {
+ char *cmd, *p1, *p2;
+ struct test_entry *entry;
+ struct object_id oid;
+
+ /* break line into command and up to two parameters */
+ string_list_setlen(&parts, 0);
+ string_list_split_in_place(&parts, line.buf, DELIM, 2);
+ string_list_remove_empty_items(&parts, 0);
+
+ /* ignore empty lines */
+ if (!parts.nr)
+ continue;
+ if (!*parts.items[0].string || *parts.items[0].string == '#')
+ continue;
+
+ cmd = parts.items[0].string;
+ p1 = parts.nr >= 1 ? parts.items[1].string : NULL;
+ p2 = parts.nr >= 2 ? parts.items[2].string : NULL;
+
+ if (!strcmp("put", cmd) && p1 && p2) {
+
+ if (repo_get_oid(the_repository, p1, &oid)) {
+ printf("Unknown oid: %s\n", p1);
+ continue;
+ }
+
+ /* create entry with oid_key = p1, name_value = p2 */
+ FLEX_ALLOC_STR(entry, name, p2);
+ oidcpy(&entry->entry.oid, &oid);
+
+ /* add / replace entry */
+ entry = oidmap_put(&map, entry);
+
+ /* print and free replaced entry, if any */
+ puts(entry ? entry->name : "NULL");
+ free(entry);
+
+ } else if (!strcmp("get", cmd) && p1) {
+
+ if (repo_get_oid(the_repository, p1, &oid)) {
+ printf("Unknown oid: %s\n", p1);
+ continue;
+ }
+
+ /* lookup entry in oidmap */
+ entry = oidmap_get(&map, &oid);
+
+ /* print result */
+ puts(entry ? entry->name : "NULL");
+
+ } else if (!strcmp("remove", cmd) && p1) {
+
+ if (repo_get_oid(the_repository, p1, &oid)) {
+ printf("Unknown oid: %s\n", p1);
+ continue;
+ }
+
+ /* remove entry from oidmap */
+ entry = oidmap_remove(&map, &oid);
+
+ /* print result and free entry*/
+ puts(entry ? entry->name : "NULL");
+ free(entry);
+
+ } else if (!strcmp("iterate", cmd)) {
+
+ struct oidmap_iter iter;
+ oidmap_iter_init(&map, &iter);
+ while ((entry = oidmap_iter_next(&iter)))
+ printf("%s %s\n", oid_to_hex(&entry->entry.oid), entry->name);
+
+ } else {
+
+ printf("Unknown command %s\n", cmd);
+
+ }
+ }
+
+ string_list_clear(&parts, 0);
+ strbuf_release(&line);
+ oidmap_free(&map, 1);
+ return 0;
+}
diff --git a/t/helper/test-oidtree.c b/t/helper/test-oidtree.c
new file mode 100644
index 0000000..c7a1d4c
--- /dev/null
+++ b/t/helper/test-oidtree.c
@@ -0,0 +1,54 @@
+#include "test-tool.h"
+#include "hex.h"
+#include "oidtree.h"
+#include "setup.h"
+#include "strbuf.h"
+
+static enum cb_next print_oid(const struct object_id *oid, void *data UNUSED)
+{
+ puts(oid_to_hex(oid));
+ return CB_CONTINUE;
+}
+
+int cmd__oidtree(int argc UNUSED, const char **argv UNUSED)
+{
+ struct oidtree ot;
+ struct strbuf line = STRBUF_INIT;
+ int nongit_ok;
+ int algo = GIT_HASH_UNKNOWN;
+
+ oidtree_init(&ot);
+ setup_git_directory_gently(&nongit_ok);
+
+ while (strbuf_getline(&line, stdin) != EOF) {
+ const char *arg;
+ struct object_id oid;
+
+ if (skip_prefix(line.buf, "insert ", &arg)) {
+ if (get_oid_hex_any(arg, &oid) == GIT_HASH_UNKNOWN)
+ die("insert not a hexadecimal oid: %s", arg);
+ algo = oid.algo;
+ oidtree_insert(&ot, &oid);
+ } else if (skip_prefix(line.buf, "contains ", &arg)) {
+ if (get_oid_hex(arg, &oid))
+ die("contains not a hexadecimal oid: %s", arg);
+ printf("%d\n", oidtree_contains(&ot, &oid));
+ } else if (skip_prefix(line.buf, "each ", &arg)) {
+ char buf[GIT_MAX_HEXSZ + 1] = { '0' };
+ memset(&oid, 0, sizeof(oid));
+ memcpy(buf, arg, strlen(arg));
+ buf[hash_algos[algo].hexsz] = '\0';
+ get_oid_hex_any(buf, &oid);
+ oid.algo = algo;
+ oidtree_each(&ot, &oid, strlen(arg), print_oid, NULL);
+ } else if (!strcmp(line.buf, "clear")) {
+ oidtree_clear(&ot);
+ } else {
+ die("unknown command: %s", line.buf);
+ }
+ }
+
+ strbuf_release(&line);
+
+ return 0;
+}
diff --git a/t/helper/test-online-cpus.c b/t/helper/test-online-cpus.c
new file mode 100644
index 0000000..47dc211
--- /dev/null
+++ b/t/helper/test-online-cpus.c
@@ -0,0 +1,9 @@
+#include "test-tool.h"
+#include "git-compat-util.h"
+#include "thread-utils.h"
+
+int cmd__online_cpus(int argc UNUSED, const char **argv UNUSED)
+{
+ printf("%d\n", online_cpus());
+ return 0;
+}
diff --git a/t/helper/test-pack-mtimes.c b/t/helper/test-pack-mtimes.c
new file mode 100644
index 0000000..67a964e
--- /dev/null
+++ b/t/helper/test-pack-mtimes.c
@@ -0,0 +1,57 @@
+#include "test-tool.h"
+#include "hex.h"
+#include "strbuf.h"
+#include "object-store-ll.h"
+#include "packfile.h"
+#include "pack-mtimes.h"
+#include "setup.h"
+
+static void dump_mtimes(struct packed_git *p)
+{
+ uint32_t i;
+ if (load_pack_mtimes(p) < 0)
+ die("could not load pack .mtimes");
+
+ for (i = 0; i < p->num_objects; i++) {
+ struct object_id oid;
+ if (nth_packed_object_id(&oid, p, i) < 0)
+ die("could not load object id at position %"PRIu32, i);
+
+ printf("%s %"PRIu32"\n",
+ oid_to_hex(&oid), nth_packed_mtime(p, i));
+ }
+}
+
+static const char *pack_mtimes_usage = "\n"
+" test-tool pack-mtimes <pack-name.mtimes>";
+
+int cmd__pack_mtimes(int argc, const char **argv)
+{
+ struct strbuf buf = STRBUF_INIT;
+ struct packed_git *p;
+
+ setup_git_directory();
+
+ if (argc != 2)
+ usage(pack_mtimes_usage);
+
+ for (p = get_all_packs(the_repository); p; p = p->next) {
+ strbuf_addstr(&buf, basename(p->pack_name));
+ strbuf_strip_suffix(&buf, ".pack");
+ strbuf_addstr(&buf, ".mtimes");
+
+ if (!strcmp(buf.buf, argv[1]))
+ break;
+
+ strbuf_reset(&buf);
+ }
+
+ strbuf_release(&buf);
+
+ if (!p)
+ die("could not find pack '%s'", argv[1]);
+
+ dump_mtimes(p);
+
+ return 0;
+}
diff --git a/t/helper/test-parse-options.c b/t/helper/test-parse-options.c
new file mode 100644
index 0000000..ded8116
--- /dev/null
+++ b/t/helper/test-parse-options.c
@@ -0,0 +1,340 @@
+#include "test-tool.h"
+#include "parse-options.h"
+#include "strbuf.h"
+#include "string-list.h"
+#include "trace2.h"
+
+static int boolean = 0;
+static int integer = 0;
+static unsigned long magnitude = 0;
+static timestamp_t timestamp;
+static int abbrev = 7;
+static int verbose = -1; /* unspecified */
+static int dry_run = 0, quiet = 0;
+static char *string = NULL;
+static char *file = NULL;
+static int ambiguous;
+
+static struct {
+ int called;
+ const char *arg;
+ int unset;
+} length_cb;
+
+static int mode34_callback(const struct option *opt, const char *arg, int unset)
+{
+ if (unset)
+ *(int *)opt->value = 0;
+ else if (!strcmp(arg, "3"))
+ *(int *)opt->value = 3;
+ else if (!strcmp(arg, "4"))
+ *(int *)opt->value = 4;
+ else
+ return error("invalid value for '%s': '%s'", "--mode34", arg);
+ return 0;
+}
+
+static int length_callback(const struct option *opt, const char *arg, int unset)
+{
+ length_cb.called = 1;
+ length_cb.arg = arg;
+ length_cb.unset = unset;
+
+ if (unset)
+ return 1; /* do not support unset */
+
+ *(int *)opt->value = strlen(arg);
+ return 0;
+}
+
+static int number_callback(const struct option *opt, const char *arg, int unset)
+{
+ BUG_ON_OPT_NEG(unset);
+ *(int *)opt->value = strtol(arg, NULL, 10);
+ return 0;
+}
+
+static int collect_expect(const struct option *opt, const char *arg, int unset)
+{
+ struct string_list *expect;
+ struct string_list_item *item;
+ struct strbuf label = STRBUF_INIT;
+ const char *colon;
+
+ if (!arg || unset)
+ die("malformed --expect option");
+
+ expect = (struct string_list *)opt->value;
+ colon = strchr(arg, ':');
+ if (!colon)
+ die("malformed --expect option, lacking a colon");
+ strbuf_add(&label, arg, colon - arg);
+ item = string_list_insert(expect, strbuf_detach(&label, NULL));
+ if (item->util)
+ die("malformed --expect option, duplicate %s", label.buf);
+ item->util = (void *)arg;
+ return 0;
+}
+
+__attribute__((format (printf,3,4)))
+static void show(struct string_list *expect, int *status, const char *fmt, ...)
+{
+ struct string_list_item *item;
+ struct strbuf buf = STRBUF_INIT;
+ va_list args;
+
+ va_start(args, fmt);
+ strbuf_vaddf(&buf, fmt, args);
+ va_end(args);
+
+ if (!expect->nr)
+ printf("%s\n", buf.buf);
+ else {
+ char *colon = strchr(buf.buf, ':');
+ if (!colon)
+ die("malformed output format, output lacking colon: %s", fmt);
+ *colon = '\0';
+ item = string_list_lookup(expect, buf.buf);
+ *colon = ':';
+ if (!item)
+ ; /* not among entries being checked */
+ else {
+ if (strcmp((const char *)item->util, buf.buf)) {
+ printf("-%s\n", (char *)item->util);
+ printf("+%s\n", buf.buf);
+ *status = 1;
+ }
+ }
+ }
+ strbuf_release(&buf);
+}
+
+int cmd__parse_options(int argc, const char **argv)
+{
+ const char *prefix = "prefix/";
+ const char *usage[] = {
+ "test-tool parse-options <options>",
+ "",
+ "A helper function for the parse-options API.",
+ NULL
+ };
+ struct string_list expect = STRING_LIST_INIT_NODUP;
+ struct string_list list = STRING_LIST_INIT_NODUP;
+
+ struct option options[] = {
+ OPT_BOOL(0, "yes", &boolean, "get a boolean"),
+ OPT_BOOL('D', "no-doubt", &boolean, "begins with 'no-'"),
+ { OPTION_SET_INT, 'B', "no-fear", &boolean, NULL,
+ "be brave", PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL, 1 },
+ OPT_COUNTUP('b', "boolean", &boolean, "increment by one"),
+ OPT_BIT('4', "or4", &boolean,
+ "bitwise-or boolean with ...0100", 4),
+ OPT_NEGBIT(0, "neg-or4", &boolean, "same as --no-or4", 4),
+ OPT_GROUP(""),
+ OPT_INTEGER('i', "integer", &integer, "get a integer"),
+ OPT_INTEGER('j', NULL, &integer, "get a integer, too"),
+ OPT_MAGNITUDE('m', "magnitude", &magnitude, "get a magnitude"),
+ OPT_SET_INT(0, "set23", &integer, "set integer to 23", 23),
+ OPT_CMDMODE(0, "mode1", &integer, "set integer to 1 (cmdmode option)", 1),
+ OPT_CMDMODE(0, "mode2", &integer, "set integer to 2 (cmdmode option)", 2),
+ OPT_CALLBACK_F(0, "mode34", &integer, "(3|4)",
+ "set integer to 3 or 4 (cmdmode option)",
+ PARSE_OPT_CMDMODE, mode34_callback),
+ OPT_CALLBACK('L', "length", &integer, "str",
+ "get length of <str>", length_callback),
+ OPT_FILENAME('F', "file", &file, "set file to <file>"),
+ OPT_GROUP("String options"),
+ OPT_STRING('s', "string", &string, "string", "get a string"),
+ OPT_STRING(0, "string2", &string, "str", "get another string"),
+ OPT_STRING(0, "st", &string, "st", "get another string (pervert ordering)"),
+ OPT_STRING('o', NULL, &string, "str", "get another string"),
+ OPT_NOOP_NOARG(0, "obsolete"),
+ OPT_SET_INT_F(0, "longhelp", &integer, "help text of this entry\n"
+ "spans multiple lines", 0, PARSE_OPT_NONEG),
+ OPT_STRING_LIST(0, "list", &list, "str", "add str to list"),
+ OPT_GROUP("Magic arguments"),
+ OPT_NUMBER_CALLBACK(&integer, "set integer to NUM",
+ number_callback),
+ { OPTION_COUNTUP, '+', NULL, &boolean, NULL, "same as -b",
+ PARSE_OPT_NOARG | PARSE_OPT_NONEG | PARSE_OPT_NODASH },
+ { OPTION_COUNTUP, 0, "ambiguous", &ambiguous, NULL,
+ "positive ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG },
+ { OPTION_COUNTUP, 0, "no-ambiguous", &ambiguous, NULL,
+ "negative ambiguity", PARSE_OPT_NOARG | PARSE_OPT_NONEG },
+ OPT_GROUP("Standard options"),
+ OPT__ABBREV(&abbrev),
+ OPT__VERBOSE(&verbose, "be verbose"),
+ OPT__DRY_RUN(&dry_run, "dry run"),
+ OPT__QUIET(&quiet, "be quiet"),
+ OPT_CALLBACK(0, "expect", &expect, "string",
+ "expected output in the variable dump",
+ collect_expect),
+ OPT_GROUP("Alias"),
+ OPT_STRING('A', "alias-source", &string, "string", "get a string"),
+ OPT_ALIAS('Z', "alias-target", "alias-source"),
+ OPT_END(),
+ };
+ int i;
+ int ret = 0;
+
+ trace2_cmd_name("_parse_");
+
+ argc = parse_options(argc, (const char **)argv, prefix, options, usage, 0);
+
+ if (length_cb.called) {
+ const char *arg = length_cb.arg;
+ int unset = length_cb.unset;
+ show(&expect, &ret, "Callback: \"%s\", %d",
+ (arg ? arg : "not set"), unset);
+ }
+ show(&expect, &ret, "boolean: %d", boolean);
+ show(&expect, &ret, "integer: %d", integer);
+ show(&expect, &ret, "magnitude: %lu", magnitude);
+ show(&expect, &ret, "timestamp: %"PRItime, timestamp);
+ show(&expect, &ret, "string: %s", string ? string : "(not set)");
+ show(&expect, &ret, "abbrev: %d", abbrev);
+ show(&expect, &ret, "verbose: %d", verbose);
+ show(&expect, &ret, "quiet: %d", quiet);
+ show(&expect, &ret, "dry run: %s", dry_run ? "yes" : "no");
+ show(&expect, &ret, "file: %s", file ? file : "(not set)");
+
+ for (i = 0; i < list.nr; i++)
+ show(&expect, &ret, "list: %s", list.items[i].string);
+
+ for (i = 0; i < argc; i++)
+ show(&expect, &ret, "arg %02d: %s", i, argv[i]);
+
+ expect.strdup_strings = 1;
+ string_list_clear(&expect, 0);
+ string_list_clear(&list, 0);
+
+ return ret;
+}
+
+static void print_args(int argc, const char **argv)
+{
+ int i;
+ for (i = 0; i < argc; i++)
+ printf("arg %02d: %s\n", i, argv[i]);
+}
+
+static int parse_options_flags__cmd(int argc, const char **argv,
+ enum parse_opt_flags test_flags)
+{
+ const char *usage[] = {
+ "<...> cmd [options]",
+ NULL
+ };
+ int opt = 0;
+ const struct option options[] = {
+ OPT_INTEGER('o', "opt", &opt, "an integer option"),
+ OPT_END()
+ };
+
+ argc = parse_options(argc, argv, NULL, options, usage, test_flags);
+
+ printf("opt: %d\n", opt);
+ print_args(argc, argv);
+
+ return 0;
+}
+
+static enum parse_opt_flags test_flags = 0;
+static const struct option test_flag_options[] = {
+ OPT_GROUP("flag-options:"),
+ OPT_BIT(0, "keep-dashdash", &test_flags,
+ "pass PARSE_OPT_KEEP_DASHDASH to parse_options()",
+ PARSE_OPT_KEEP_DASHDASH),
+ OPT_BIT(0, "stop-at-non-option", &test_flags,
+ "pass PARSE_OPT_STOP_AT_NON_OPTION to parse_options()",
+ PARSE_OPT_STOP_AT_NON_OPTION),
+ OPT_BIT(0, "keep-argv0", &test_flags,
+ "pass PARSE_OPT_KEEP_ARGV0 to parse_options()",
+ PARSE_OPT_KEEP_ARGV0),
+ OPT_BIT(0, "keep-unknown-opt", &test_flags,
+ "pass PARSE_OPT_KEEP_UNKNOWN_OPT to parse_options()",
+ PARSE_OPT_KEEP_UNKNOWN_OPT),
+ OPT_BIT(0, "no-internal-help", &test_flags,
+ "pass PARSE_OPT_NO_INTERNAL_HELP to parse_options()",
+ PARSE_OPT_NO_INTERNAL_HELP),
+ OPT_BIT(0, "subcommand-optional", &test_flags,
+ "pass PARSE_OPT_SUBCOMMAND_OPTIONAL to parse_options()",
+ PARSE_OPT_SUBCOMMAND_OPTIONAL),
+ OPT_END()
+};
+
+int cmd__parse_options_flags(int argc, const char **argv)
+{
+ const char *usage[] = {
+ "test-tool parse-options-flags [flag-options] cmd [options]",
+ NULL
+ };
+
+ argc = parse_options(argc, argv, NULL, test_flag_options, usage,
+ PARSE_OPT_STOP_AT_NON_OPTION);
+
+ if (!argc || strcmp(argv[0], "cmd")) {
+ error("'cmd' is mandatory");
+ usage_with_options(usage, test_flag_options);
+ }
+
+ return parse_options_flags__cmd(argc, argv, test_flags);
+}
+
+static int subcmd_one(int argc, const char **argv, const char *prefix UNUSED)
+{
+ printf("fn: subcmd_one\n");
+ print_args(argc, argv);
+ return 0;
+}
+
+static int subcmd_two(int argc, const char **argv, const char *prefix UNUSED)
+{
+ printf("fn: subcmd_two\n");
+ print_args(argc, argv);
+ return 0;
+}
+
+static int parse_subcommand__cmd(int argc, const char **argv,
+ enum parse_opt_flags test_flags)
+{
+ const char *usage[] = {
+ "<...> cmd subcmd-one",
+ "<...> cmd subcmd-two",
+ NULL
+ };
+ parse_opt_subcommand_fn *fn = NULL;
+ int opt = 0;
+ struct option options[] = {
+ OPT_SUBCOMMAND("subcmd-one", &fn, subcmd_one),
+ OPT_SUBCOMMAND("subcmd-two", &fn, subcmd_two),
+ OPT_INTEGER('o', "opt", &opt, "an integer option"),
+ OPT_END()
+ };
+
+ if (test_flags & PARSE_OPT_SUBCOMMAND_OPTIONAL)
+ fn = subcmd_one;
+ argc = parse_options(argc, argv, NULL, options, usage, test_flags);
+
+ printf("opt: %d\n", opt);
+
+ return fn(argc, argv, NULL);
+}
+
+int cmd__parse_subcommand(int argc, const char **argv)
+{
+ const char *usage[] = {
+ "test-tool parse-subcommand [flag-options] cmd <subcommand>",
+ NULL
+ };
+
+ argc = parse_options(argc, argv, NULL, test_flag_options, usage,
+ PARSE_OPT_STOP_AT_NON_OPTION);
+
+ if (!argc || strcmp(argv[0], "cmd")) {
+ error("'cmd' is mandatory");
+ usage_with_options(usage, test_flag_options);
+ }
+
+ return parse_subcommand__cmd(argc, argv, test_flags);
+}
diff --git a/t/helper/test-parse-pathspec-file.c b/t/helper/test-parse-pathspec-file.c
new file mode 100644
index 0000000..89ecefd
--- /dev/null
+++ b/t/helper/test-parse-pathspec-file.c
@@ -0,0 +1,33 @@
+#include "test-tool.h"
+#include "parse-options.h"
+#include "pathspec.h"
+
+int cmd__parse_pathspec_file(int argc, const char **argv)
+{
+ struct pathspec pathspec;
+ char *pathspec_from_file = NULL;
+ int pathspec_file_nul = 0, i;
+
+ static const char *const usage[] = {
+ "test-tool parse-pathspec-file --pathspec-from-file [--pathspec-file-nul]",
+ NULL
+ };
+
+ struct option options[] = {
+ OPT_PATHSPEC_FROM_FILE(&pathspec_from_file),
+ OPT_PATHSPEC_FILE_NUL(&pathspec_file_nul),
+ OPT_END()
+ };
+
+ parse_options(argc, argv, NULL, options, usage, 0);
+
+ parse_pathspec_file(&pathspec, 0, 0, NULL, pathspec_from_file,
+ pathspec_file_nul);
+
+ for (i = 0; i < pathspec.nr; i++)
+ printf("%s\n", pathspec.items[i].original);
+
+ clear_pathspec(&pathspec);
+ free(pathspec_from_file);
+ return 0;
+}
diff --git a/t/helper/test-partial-clone.c b/t/helper/test-partial-clone.c
new file mode 100644
index 0000000..910a128
--- /dev/null
+++ b/t/helper/test-partial-clone.c
@@ -0,0 +1,44 @@
+#include "test-tool.h"
+#include "hex.h"
+#include "repository.h"
+#include "object-store-ll.h"
+#include "setup.h"
+
+/*
+ * Prints the size of the object corresponding to the given hash in a specific
+ * gitdir. This is similar to "git -C gitdir cat-file -s", except that this
+ * exercises the code that accesses the object of an arbitrary repository that
+ * is not the_repository. ("git -C gitdir" makes it so that the_repository is
+ * the one in gitdir.)
+ */
+static void object_info(const char *gitdir, const char *oid_hex)
+{
+ struct repository r;
+ struct object_id oid;
+ unsigned long size;
+ struct object_info oi = {.sizep = &size};
+ const char *p;
+
+ if (repo_init(&r, gitdir, NULL))
+ die("could not init repo");
+ if (parse_oid_hex(oid_hex, &oid, &p))
+ die("could not parse oid");
+ if (oid_object_info_extended(&r, &oid, &oi, 0))
+ die("could not obtain object info");
+ printf("%d\n", (int) size);
+}
+
+int cmd__partial_clone(int argc, const char **argv)
+{
+ setup_git_directory();
+
+ if (argc < 4)
+ die("too few arguments");
+
+ if (!strcmp(argv[1], "object-info"))
+ object_info(argv[2], argv[3]);
+ else
+ die("invalid argument '%s'", argv[1]);
+
+ return 0;
+}
diff --git a/t/helper/test-path-utils.c b/t/helper/test-path-utils.c
new file mode 100644
index 0000000..70396fa
--- /dev/null
+++ b/t/helper/test-path-utils.c
@@ -0,0 +1,506 @@
+#include "test-tool.h"
+#include "abspath.h"
+#include "environment.h"
+#include "path.h"
+#include "read-cache-ll.h"
+#include "setup.h"
+#include "string-list.h"
+#include "trace.h"
+#include "utf8.h"
+
+/*
+ * A "string_list_each_func_t" function that normalizes an entry from
+ * GIT_CEILING_DIRECTORIES. If the path is unusable for some reason,
+ * die with an explanation.
+ */
+static int normalize_ceiling_entry(struct string_list_item *item,
+ void *data UNUSED)
+{
+ char *ceil = item->string;
+
+ if (!*ceil)
+ die("Empty path is not supported");
+ if (!is_absolute_path(ceil))
+ die("Path \"%s\" is not absolute", ceil);
+ if (normalize_path_copy(ceil, ceil) < 0)
+ die("Path \"%s\" could not be normalized", ceil);
+ return 1;
+}
+
+static void normalize_argv_string(const char **var, const char *input)
+{
+ if (!strcmp(input, "<null>"))
+ *var = NULL;
+ else if (!strcmp(input, "<empty>"))
+ *var = "";
+ else
+ *var = input;
+
+ if (*var && (**var == '<' || **var == '('))
+ die("Bad value: %s\n", input);
+}
+
+struct test_data {
+ const char *from; /* input: transform from this ... */
+ const char *to; /* output: ... to this. */
+ const char *alternative; /* output: ... or this. */
+};
+
+/*
+ * Compatibility wrappers for OpenBSD, whose basename(3) and dirname(3)
+ * have const parameters.
+ */
+static char *posix_basename(char *path)
+{
+ return basename(path);
+}
+
+static char *posix_dirname(char *path)
+{
+ return dirname(path);
+}
+
+static int test_function(struct test_data *data, char *(*func)(char *input),
+ const char *funcname)
+{
+ int failed = 0, i;
+ char buffer[1024];
+ char *to;
+
+ for (i = 0; data[i].to; i++) {
+ if (!data[i].from)
+ to = func(NULL);
+ else {
+ xsnprintf(buffer, sizeof(buffer), "%s", data[i].from);
+ to = func(buffer);
+ }
+ if (!strcmp(to, data[i].to))
+ continue;
+ if (!data[i].alternative)
+ error("FAIL: %s(%s) => '%s' != '%s'\n",
+ funcname, data[i].from, to, data[i].to);
+ else if (!strcmp(to, data[i].alternative))
+ continue;
+ else
+ error("FAIL: %s(%s) => '%s' != '%s', '%s'\n",
+ funcname, data[i].from, to, data[i].to,
+ data[i].alternative);
+ failed = 1;
+ }
+ return failed;
+}
+
+static struct test_data basename_data[] = {
+ /* --- POSIX type paths --- */
+ { NULL, "." },
+ { "", "." },
+ { ".", "." },
+ { "..", ".." },
+ { "/", "/" },
+ { "//", "/", "//" },
+ { "///", "/", "//" },
+ { "////", "/", "//" },
+ { "usr", "usr" },
+ { "/usr", "usr" },
+ { "/usr/", "usr" },
+ { "/usr//", "usr" },
+ { "/usr/lib", "lib" },
+ { "usr/lib", "lib" },
+ { "usr/lib///", "lib" },
+
+#if defined(__MINGW32__) || defined(_MSC_VER)
+ /* --- win32 type paths --- */
+ { "\\usr", "usr" },
+ { "\\usr\\", "usr" },
+ { "\\usr\\\\", "usr" },
+ { "\\usr\\lib", "lib" },
+ { "usr\\lib", "lib" },
+ { "usr\\lib\\\\\\", "lib" },
+ { "C:/usr", "usr" },
+ { "C:/usr", "usr" },
+ { "C:/usr/", "usr" },
+ { "C:/usr//", "usr" },
+ { "C:/usr/lib", "lib" },
+ { "C:usr/lib", "lib" },
+ { "C:usr/lib///", "lib" },
+ { "C:", "." },
+ { "C:a", "a" },
+ { "C:/", "/" },
+ { "C:///", "/" },
+ { "\\", "\\", "/" },
+ { "\\\\", "\\", "/" },
+ { "\\\\\\", "\\", "/" },
+#endif
+ { NULL, NULL }
+};
+
+static struct test_data dirname_data[] = {
+ /* --- POSIX type paths --- */
+ { NULL, "." },
+ { "", "." },
+ { ".", "." },
+ { "..", "." },
+ { "/", "/" },
+ { "//", "/", "//" },
+ { "///", "/", "//" },
+ { "////", "/", "//" },
+ { "usr", "." },
+ { "/usr", "/" },
+ { "/usr/", "/" },
+ { "/usr//", "/" },
+ { "/usr/lib", "/usr" },
+ { "usr/lib", "usr" },
+ { "usr/lib///", "usr" },
+
+#if defined(__MINGW32__) || defined(_MSC_VER)
+ /* --- win32 type paths --- */
+ { "\\", "\\" },
+ { "\\\\", "\\\\" },
+ { "\\usr", "\\" },
+ { "\\usr\\", "\\" },
+ { "\\usr\\\\", "\\" },
+ { "\\usr\\lib", "\\usr" },
+ { "usr\\lib", "usr" },
+ { "usr\\lib\\\\\\", "usr" },
+ { "C:a", "C:." },
+ { "C:/", "C:/" },
+ { "C:///", "C:/" },
+ { "C:/usr", "C:/" },
+ { "C:/usr/", "C:/" },
+ { "C:/usr//", "C:/" },
+ { "C:/usr/lib", "C:/usr" },
+ { "C:usr/lib", "C:usr" },
+ { "C:usr/lib///", "C:usr" },
+ { "\\\\\\", "\\" },
+ { "\\\\\\\\", "\\" },
+ { "C:", "C:.", "." },
+#endif
+ { NULL, NULL }
+};
+
+static int check_dotfile(const char *x, const char **argv,
+ int (*is_hfs)(const char *),
+ int (*is_ntfs)(const char *))
+{
+ int res = 0, expect = 1;
+ for (; *argv; argv++) {
+ if (!strcmp("--not", *argv))
+ expect = !expect;
+ else if (expect != (is_hfs(*argv) || is_ntfs(*argv)))
+ res = error("'%s' is %s.git%s", *argv,
+ expect ? "not " : "", x);
+ else
+ fprintf(stderr, "ok: '%s' is %s.git%s\n",
+ *argv, expect ? "" : "not ", x);
+ }
+ return !!res;
+}
+
+static int cmp_by_st_size(const void *a, const void *b)
+{
+ intptr_t x = (intptr_t)((struct string_list_item *)a)->util;
+ intptr_t y = (intptr_t)((struct string_list_item *)b)->util;
+
+ return x > y ? -1 : (x < y ? +1 : 0);
+}
+
+/*
+ * A very simple, reproducible pseudo-random generator. Copied from
+ * `test-genrandom.c`.
+ */
+static uint64_t my_random_value = 1234;
+
+static uint64_t my_random(void)
+{
+ my_random_value = my_random_value * 1103515245 + 12345;
+ return my_random_value;
+}
+
+/*
+ * A fast approximation of the square root, without requiring math.h.
+ *
+ * It uses Newton's method to approximate the solution of 0 = x^2 - value.
+ */
+static double my_sqrt(double value)
+{
+ const double epsilon = 1e-6;
+ double x = value;
+
+ if (value == 0)
+ return 0;
+
+ for (;;) {
+ double delta = (value / x - x) / 2;
+ if (delta < epsilon && delta > -epsilon)
+ return x + delta;
+ x += delta;
+ }
+}
+
+static int protect_ntfs_hfs_benchmark(int argc, const char **argv)
+{
+ size_t i, j, nr, min_len = 3, max_len = 20;
+ char **names;
+ int repetitions = 15, file_mode = 0100644;
+ uint64_t begin, end;
+ double m[3][2], v[3][2];
+ uint64_t cumul;
+ double cumul2;
+
+ if (argc > 1 && !strcmp(argv[1], "--with-symlink-mode")) {
+ file_mode = 0120000;
+ argc--;
+ argv++;
+ }
+
+ nr = argc > 1 ? strtoul(argv[1], NULL, 0) : 1000000;
+ ALLOC_ARRAY(names, nr);
+
+ if (argc > 2) {
+ min_len = strtoul(argv[2], NULL, 0);
+ if (argc > 3)
+ max_len = strtoul(argv[3], NULL, 0);
+ if (min_len > max_len)
+ die("min_len > max_len");
+ }
+
+ for (i = 0; i < nr; i++) {
+ size_t len = min_len + (my_random() % (max_len + 1 - min_len));
+
+ names[i] = xmallocz(len);
+ while (len > 0)
+ names[i][--len] = (char)(' ' + (my_random() % ('\x7f' - ' ')));
+ }
+
+ for (protect_ntfs = 0; protect_ntfs < 2; protect_ntfs++)
+ for (protect_hfs = 0; protect_hfs < 2; protect_hfs++) {
+ cumul = 0;
+ cumul2 = 0;
+ for (i = 0; i < repetitions; i++) {
+ begin = getnanotime();
+ for (j = 0; j < nr; j++)
+ verify_path(names[j], file_mode);
+ end = getnanotime();
+ printf("protect_ntfs = %d, protect_hfs = %d: %lfms\n", protect_ntfs, protect_hfs, (end-begin) / (double)1e6);
+ cumul += end - begin;
+ cumul2 += (end - begin) * (end - begin);
+ }
+ m[protect_ntfs][protect_hfs] = cumul / (double)repetitions;
+ v[protect_ntfs][protect_hfs] = my_sqrt(cumul2 / (double)repetitions - m[protect_ntfs][protect_hfs] * m[protect_ntfs][protect_hfs]);
+ printf("mean: %lfms, stddev: %lfms\n", m[protect_ntfs][protect_hfs] / (double)1e6, v[protect_ntfs][protect_hfs] / (double)1e6);
+ }
+
+ for (protect_ntfs = 0; protect_ntfs < 2; protect_ntfs++)
+ for (protect_hfs = 0; protect_hfs < 2; protect_hfs++)
+ printf("ntfs=%d/hfs=%d: %lf%% slower\n", protect_ntfs, protect_hfs, (m[protect_ntfs][protect_hfs] - m[0][0]) * 100 / m[0][0]);
+
+ return 0;
+}
+
+int cmd__path_utils(int argc, const char **argv)
+{
+ if (argc == 3 && !strcmp(argv[1], "normalize_path_copy")) {
+ char *buf = xmallocz(strlen(argv[2]));
+ int rv = normalize_path_copy(buf, argv[2]);
+ puts(rv ? "++failed++" : buf);
+ free(buf);
+ return 0;
+ }
+
+ if (argc >= 2 && !strcmp(argv[1], "real_path")) {
+ struct strbuf realpath = STRBUF_INIT;
+ while (argc > 2) {
+ strbuf_realpath(&realpath, argv[2], 1);
+ puts(realpath.buf);
+ argc--;
+ argv++;
+ }
+ strbuf_release(&realpath);
+ return 0;
+ }
+
+ if (argc >= 2 && !strcmp(argv[1], "absolute_path")) {
+ while (argc > 2) {
+ puts(absolute_path(argv[2]));
+ argc--;
+ argv++;
+ }
+ return 0;
+ }
+
+ if (argc == 4 && !strcmp(argv[1], "longest_ancestor_length")) {
+ int len;
+ struct string_list ceiling_dirs = STRING_LIST_INIT_DUP;
+ char *path = xstrdup(argv[2]);
+
+ /*
+ * We have to normalize the arguments because under
+ * Windows, bash mangles arguments that look like
+ * absolute POSIX paths or colon-separate lists of
+ * absolute POSIX paths into DOS paths (e.g.,
+ * "/foo:/foo/bar" might be converted to
+ * "D:\Src\msysgit\foo;D:\Src\msysgit\foo\bar"),
+ * whereas longest_ancestor_length() requires paths
+ * that use forward slashes.
+ */
+ if (normalize_path_copy(path, path))
+ die("Path \"%s\" could not be normalized", argv[2]);
+ string_list_split(&ceiling_dirs, argv[3], PATH_SEP, -1);
+ filter_string_list(&ceiling_dirs, 0,
+ normalize_ceiling_entry, NULL);
+ len = longest_ancestor_length(path, &ceiling_dirs);
+ string_list_clear(&ceiling_dirs, 0);
+ free(path);
+ printf("%d\n", len);
+ return 0;
+ }
+
+ if (argc >= 4 && !strcmp(argv[1], "prefix_path")) {
+ const char *prefix = argv[2];
+ int prefix_len = strlen(prefix);
+ int nongit_ok;
+ setup_git_directory_gently(&nongit_ok);
+ while (argc > 3) {
+ char *pfx = prefix_path(prefix, prefix_len, argv[3]);
+
+ puts(pfx);
+ free(pfx);
+ argc--;
+ argv++;
+ }
+ return 0;
+ }
+
+ if (argc == 4 && !strcmp(argv[1], "strip_path_suffix")) {
+ char *prefix = strip_path_suffix(argv[2], argv[3]);
+ printf("%s\n", prefix ? prefix : "(null)");
+ free(prefix);
+ return 0;
+ }
+
+ if (argc == 3 && !strcmp(argv[1], "print_path")) {
+ puts(argv[2]);
+ return 0;
+ }
+
+ if (argc == 4 && !strcmp(argv[1], "relative_path")) {
+ struct strbuf sb = STRBUF_INIT;
+ const char *in, *prefix, *rel;
+ normalize_argv_string(&in, argv[2]);
+ normalize_argv_string(&prefix, argv[3]);
+ rel = relative_path(in, prefix, &sb);
+ if (!rel)
+ puts("(null)");
+ else
+ puts(strlen(rel) > 0 ? rel : "(empty)");
+ strbuf_release(&sb);
+ return 0;
+ }
+
+ if (argc == 2 && !strcmp(argv[1], "basename"))
+ return test_function(basename_data, posix_basename, argv[1]);
+
+ if (argc == 2 && !strcmp(argv[1], "dirname"))
+ return test_function(dirname_data, posix_dirname, argv[1]);
+
+ if (argc > 2 && !strcmp(argv[1], "is_dotgitmodules")) {
+ return check_dotfile("modules", argv + 2,
+ is_hfs_dotgitmodules,
+ is_ntfs_dotgitmodules);
+ }
+ if (argc > 2 && !strcmp(argv[1], "is_dotgitignore")) {
+ return check_dotfile("ignore", argv + 2,
+ is_hfs_dotgitignore,
+ is_ntfs_dotgitignore);
+ }
+ if (argc > 2 && !strcmp(argv[1], "is_dotgitattributes")) {
+ return check_dotfile("attributes", argv + 2,
+ is_hfs_dotgitattributes,
+ is_ntfs_dotgitattributes);
+ }
+ if (argc > 2 && !strcmp(argv[1], "is_dotmailmap")) {
+ return check_dotfile("mailmap", argv + 2,
+ is_hfs_dotmailmap,
+ is_ntfs_dotmailmap);
+ }
+
+ if (argc > 2 && !strcmp(argv[1], "file-size")) {
+ int res = 0, i;
+ struct stat st;
+
+ for (i = 2; i < argc; i++)
+ if (stat(argv[i], &st))
+ res = error_errno("Cannot stat '%s'", argv[i]);
+ else
+ printf("%"PRIuMAX"\n", (uintmax_t)st.st_size);
+ return !!res;
+ }
+
+ if (argc == 4 && !strcmp(argv[1], "skip-n-bytes")) {
+ int fd = open(argv[2], O_RDONLY), offset = atoi(argv[3]);
+ char buffer[65536];
+
+ if (fd < 0)
+ die_errno("could not open '%s'", argv[2]);
+ if (lseek(fd, offset, SEEK_SET) < 0)
+ die_errno("could not skip %d bytes", offset);
+ for (;;) {
+ ssize_t count = read(fd, buffer, sizeof(buffer));
+ if (count < 0)
+ die_errno("could not read '%s'", argv[2]);
+ if (!count)
+ break;
+ if (write(1, buffer, count) < 0)
+ die_errno("could not write to stdout");
+ }
+ close(fd);
+ return 0;
+ }
+
+ if (argc > 5 && !strcmp(argv[1], "slice-tests")) {
+ int res = 0;
+ long offset, stride, i;
+ struct string_list list = STRING_LIST_INIT_NODUP;
+ struct stat st;
+
+ offset = strtol(argv[2], NULL, 10);
+ stride = strtol(argv[3], NULL, 10);
+ if (stride < 1)
+ stride = 1;
+ for (i = 4; i < argc; i++)
+ if (stat(argv[i], &st))
+ res = error_errno("Cannot stat '%s'", argv[i]);
+ else
+ string_list_append(&list, argv[i])->util =
+ (void *)(intptr_t)st.st_size;
+ QSORT(list.items, list.nr, cmp_by_st_size);
+ for (i = offset; i < list.nr; i+= stride)
+ printf("%s\n", list.items[i].string);
+
+ return !!res;
+ }
+
+ if (argc > 1 && !strcmp(argv[1], "protect_ntfs_hfs"))
+ return !!protect_ntfs_hfs_benchmark(argc - 1, argv + 1);
+
+ if (argc > 1 && !strcmp(argv[1], "is_valid_path")) {
+ int res = 0, expect = 1, i;
+
+ for (i = 2; i < argc; i++)
+ if (!strcmp("--not", argv[i]))
+ expect = 0;
+ else if (expect != is_valid_path(argv[i]))
+ res = error("'%s' is%s a valid path",
+ argv[i], expect ? " not" : "");
+ else
+ fprintf(stderr,
+ "'%s' is%s a valid path\n",
+ argv[i], expect ? "" : " not");
+
+ return !!res;
+ }
+
+ fprintf(stderr, "%s: unknown function name: %s\n", argv[0],
+ argv[1] ? argv[1] : "(there was none)");
+ return 1;
+}
diff --git a/t/helper/test-pcre2-config.c b/t/helper/test-pcre2-config.c
new file mode 100644
index 0000000..5d0b2a2
--- /dev/null
+++ b/t/helper/test-pcre2-config.c
@@ -0,0 +1,11 @@
+#include "test-tool.h"
+#include "grep.h"
+
+int cmd__pcre2_config(int argc, const char **argv)
+{
+ if (argc == 2 && !strcmp(argv[1], "has-PCRE2_MATCH_INVALID_UTF")) {
+ int value = PCRE2_MATCH_INVALID_UTF;
+ return !value;
+ }
+ return 1;
+}
diff --git a/t/helper/test-pkt-line.c b/t/helper/test-pkt-line.c
new file mode 100644
index 0000000..f4d134a
--- /dev/null
+++ b/t/helper/test-pkt-line.c
@@ -0,0 +1,138 @@
+#include "git-compat-util.h"
+#include "test-tool.h"
+#include "pkt-line.h"
+#include "write-or-die.h"
+
+static void pack_line(const char *line)
+{
+ if (!strcmp(line, "0000") || !strcmp(line, "0000\n"))
+ packet_flush(1);
+ else if (!strcmp(line, "0001") || !strcmp(line, "0001\n"))
+ packet_delim(1);
+ else
+ packet_write_fmt(1, "%s", line);
+}
+
+static void pack(int argc, const char **argv)
+{
+ if (argc) { /* read from argv */
+ int i;
+ for (i = 0; i < argc; i++)
+ pack_line(argv[i]);
+ } else { /* read from stdin */
+ char line[LARGE_PACKET_MAX];
+ while (fgets(line, sizeof(line), stdin)) {
+ pack_line(line);
+ }
+ }
+}
+
+static void pack_raw_stdin(void)
+{
+ struct strbuf sb = STRBUF_INIT;
+
+ if (strbuf_read(&sb, 0, 0) < 0)
+ die_errno("failed to read from stdin");
+ packet_write(1, sb.buf, sb.len);
+ strbuf_release(&sb);
+}
+
+static void unpack(void)
+{
+ struct packet_reader reader;
+ packet_reader_init(&reader, 0, NULL, 0,
+ PACKET_READ_GENTLE_ON_EOF |
+ PACKET_READ_CHOMP_NEWLINE);
+
+ while (packet_reader_read(&reader) != PACKET_READ_EOF) {
+ switch (reader.status) {
+ case PACKET_READ_EOF:
+ break;
+ case PACKET_READ_NORMAL:
+ printf("%s\n", reader.line);
+ break;
+ case PACKET_READ_FLUSH:
+ printf("0000\n");
+ break;
+ case PACKET_READ_DELIM:
+ printf("0001\n");
+ break;
+ case PACKET_READ_RESPONSE_END:
+ printf("0002\n");
+ break;
+ }
+ }
+}
+
+static void unpack_sideband(void)
+{
+ struct packet_reader reader;
+ packet_reader_init(&reader, 0, NULL, 0,
+ PACKET_READ_GENTLE_ON_EOF |
+ PACKET_READ_CHOMP_NEWLINE);
+
+ while (packet_reader_read(&reader) != PACKET_READ_EOF) {
+ int band;
+ int fd;
+
+ switch (reader.status) {
+ case PACKET_READ_EOF:
+ break;
+ case PACKET_READ_NORMAL:
+ band = reader.line[0] & 0xff;
+ if (band < 1 || band > 2)
+ continue; /* skip non-sideband packets */
+ fd = band;
+
+ write_or_die(fd, reader.line + 1, reader.pktlen - 1);
+ break;
+ case PACKET_READ_FLUSH:
+ return;
+ case PACKET_READ_DELIM:
+ case PACKET_READ_RESPONSE_END:
+ break;
+ }
+ }
+}
+
+static int send_split_sideband(void)
+{
+ const char *part1 = "Hello,";
+ const char *primary = "\001primary: regular output\n";
+ const char *part2 = " world!\n";
+
+ send_sideband(1, 2, part1, strlen(part1), LARGE_PACKET_MAX);
+ packet_write(1, primary, strlen(primary));
+ send_sideband(1, 2, part2, strlen(part2), LARGE_PACKET_MAX);
+ packet_response_end(1);
+
+ return 0;
+}
+
+static int receive_sideband(void)
+{
+ return recv_sideband("sideband", 0, 1);
+}
+
+int cmd__pkt_line(int argc, const char **argv)
+{
+ if (argc < 2)
+ die("too few arguments");
+
+ if (!strcmp(argv[1], "pack"))
+ pack(argc - 2, argv + 2);
+ else if (!strcmp(argv[1], "pack-raw-stdin"))
+ pack_raw_stdin();
+ else if (!strcmp(argv[1], "unpack"))
+ unpack();
+ else if (!strcmp(argv[1], "unpack-sideband"))
+ unpack_sideband();
+ else if (!strcmp(argv[1], "send-split-sideband"))
+ send_split_sideband();
+ else if (!strcmp(argv[1], "receive-sideband"))
+ receive_sideband();
+ else
+ die("invalid argument '%s'", argv[1]);
+
+ return 0;
+}
diff --git a/t/helper/test-prio-queue.c b/t/helper/test-prio-queue.c
new file mode 100644
index 0000000..f0bf255
--- /dev/null
+++ b/t/helper/test-prio-queue.c
@@ -0,0 +1,51 @@
+#include "test-tool.h"
+#include "prio-queue.h"
+
+static int intcmp(const void *va, const void *vb, void *data UNUSED)
+{
+ const int *a = va, *b = vb;
+ return *a - *b;
+}
+
+static void show(int *v)
+{
+ if (!v)
+ printf("NULL\n");
+ else
+ printf("%d\n", *v);
+ free(v);
+}
+
+int cmd__prio_queue(int argc UNUSED, const char **argv)
+{
+ struct prio_queue pq = { intcmp };
+
+ while (*++argv) {
+ if (!strcmp(*argv, "get")) {
+ void *peek = prio_queue_peek(&pq);
+ void *get = prio_queue_get(&pq);
+ if (peek != get)
+ BUG("peek and get results do not match");
+ show(get);
+ } else if (!strcmp(*argv, "dump")) {
+ void *peek;
+ void *get;
+ while ((peek = prio_queue_peek(&pq))) {
+ get = prio_queue_get(&pq);
+ if (peek != get)
+ BUG("peek and get results do not match");
+ show(get);
+ }
+ } else if (!strcmp(*argv, "stack")) {
+ pq.compare = NULL;
+ } else {
+ int *v = xmalloc(sizeof(*v));
+ *v = atoi(*argv);
+ prio_queue_put(&pq, v);
+ }
+ }
+
+ clear_prio_queue(&pq);
+
+ return 0;
+}
diff --git a/t/helper/test-proc-receive.c b/t/helper/test-proc-receive.c
new file mode 100644
index 0000000..f30022d
--- /dev/null
+++ b/t/helper/test-proc-receive.c
@@ -0,0 +1,203 @@
+#include "test-tool.h"
+#include "connect.h"
+#include "hex.h"
+#include "parse-options.h"
+#include "pkt-line.h"
+#include "setup.h"
+#include "sigchain.h"
+
+static const char *proc_receive_usage[] = {
+ "test-tool proc-receive [<options>]",
+ NULL
+};
+
+static int die_read_version;
+static int die_write_version;
+static int die_read_commands;
+static int die_read_push_options;
+static int die_write_report;
+static int no_push_options;
+static int use_atomic;
+static int use_push_options;
+static int verbose;
+static int version = 1;
+static struct string_list returns = STRING_LIST_INIT_NODUP;
+
+struct command {
+ struct command *next;
+ const char *error_string;
+ unsigned int skip_update:1,
+ did_not_exist:1;
+ int index;
+ struct object_id old_oid;
+ struct object_id new_oid;
+ char ref_name[FLEX_ARRAY]; /* more */
+};
+
+static void proc_receive_verison(struct packet_reader *reader) {
+ int server_version = 0;
+
+ if (die_read_version)
+ die("die with the --die-read-version option");
+
+ for (;;) {
+ int linelen;
+
+ if (packet_reader_read(reader) != PACKET_READ_NORMAL)
+ break;
+
+ /* Ignore version negotiation for version 0 */
+ if (version == 0)
+ continue;
+
+ if (reader->pktlen > 8 && starts_with(reader->line, "version=")) {
+ server_version = atoi(reader->line+8);
+ if (server_version != 1)
+ die("bad protocol version: %d", server_version);
+ linelen = strlen(reader->line);
+ if (linelen < reader->pktlen) {
+ const char *feature_list = reader->line + linelen + 1;
+ if (parse_feature_request(feature_list, "atomic"))
+ use_atomic= 1;
+ if (parse_feature_request(feature_list, "push-options"))
+ use_push_options = 1;
+ }
+ }
+ }
+
+ if (die_write_version)
+ die("die with the --die-write-version option");
+
+ if (version != 0)
+ packet_write_fmt(1, "version=%d%c%s\n",
+ version, '\0',
+ use_push_options && !no_push_options ? "push-options": "");
+ packet_flush(1);
+}
+
+static void proc_receive_read_commands(struct packet_reader *reader,
+ struct command **commands)
+{
+ struct command **tail = commands;
+
+ for (;;) {
+ struct object_id old_oid, new_oid;
+ struct command *cmd;
+ const char *refname;
+ const char *p;
+
+ if (packet_reader_read(reader) != PACKET_READ_NORMAL)
+ break;
+
+ if (die_read_commands)
+ die("die with the --die-read-commands option");
+
+ if (parse_oid_hex(reader->line, &old_oid, &p) ||
+ *p++ != ' ' ||
+ parse_oid_hex(p, &new_oid, &p) ||
+ *p++ != ' ')
+ die("protocol error: expected 'old new ref', got '%s'",
+ reader->line);
+ refname = p;
+ FLEX_ALLOC_STR(cmd, ref_name, refname);
+ oidcpy(&cmd->old_oid, &old_oid);
+ oidcpy(&cmd->new_oid, &new_oid);
+
+ *tail = cmd;
+ tail = &cmd->next;
+ }
+}
+
+static void proc_receive_read_push_options(struct packet_reader *reader,
+ struct string_list *options)
+{
+
+ if (no_push_options || !use_push_options)
+ return;
+
+ if (die_read_push_options)
+ die("die with the --die-read-push-options option");
+
+ while (1) {
+ if (packet_reader_read(reader) != PACKET_READ_NORMAL)
+ break;
+
+ string_list_append(options, reader->line);
+ }
+}
+
+int cmd__proc_receive(int argc, const char **argv)
+{
+ int nongit_ok = 0;
+ struct packet_reader reader;
+ struct command *commands = NULL;
+ struct string_list push_options = STRING_LIST_INIT_DUP;
+ struct string_list_item *item;
+ struct option options[] = {
+ OPT_BOOL(0, "no-push-options", &no_push_options,
+ "disable push options"),
+ OPT_BOOL(0, "die-read-version", &die_read_version,
+ "die when reading version"),
+ OPT_BOOL(0, "die-write-version", &die_write_version,
+ "die when writing version"),
+ OPT_BOOL(0, "die-read-commands", &die_read_commands,
+ "die when reading commands"),
+ OPT_BOOL(0, "die-read-push-options", &die_read_push_options,
+ "die when reading push-options"),
+ OPT_BOOL(0, "die-write-report", &die_write_report,
+ "die when writing report"),
+ OPT_STRING_LIST('r', "return", &returns, "old/new/ref/status/msg",
+ "return of results"),
+ OPT__VERBOSE(&verbose, "be verbose"),
+ OPT_INTEGER('V', "version", &version,
+ "use this protocol version number"),
+ OPT_END()
+ };
+
+ setup_git_directory_gently(&nongit_ok);
+
+ argc = parse_options(argc, argv, "test-tools", options, proc_receive_usage, 0);
+ if (argc > 0)
+ usage_msg_opt("Too many arguments.", proc_receive_usage, options);
+ packet_reader_init(&reader, 0, NULL, 0,
+ PACKET_READ_CHOMP_NEWLINE |
+ PACKET_READ_GENTLE_ON_EOF);
+
+ sigchain_push(SIGPIPE, SIG_IGN);
+ proc_receive_verison(&reader);
+ proc_receive_read_commands(&reader, &commands);
+ proc_receive_read_push_options(&reader, &push_options);
+
+ if (verbose) {
+ struct command *cmd;
+
+ if (use_push_options || use_atomic)
+ fprintf(stderr, "proc-receive:%s%s\n",
+ use_atomic? " atomic": "",
+ use_push_options ? " push_options": "");
+
+ for (cmd = commands; cmd; cmd = cmd->next)
+ fprintf(stderr, "proc-receive< %s %s %s\n",
+ oid_to_hex(&cmd->old_oid),
+ oid_to_hex(&cmd->new_oid),
+ cmd->ref_name);
+
+ if (push_options.nr > 0)
+ for_each_string_list_item(item, &push_options)
+ fprintf(stderr, "proc-receive< %s\n", item->string);
+
+ if (returns.nr)
+ for_each_string_list_item(item, &returns)
+ fprintf(stderr, "proc-receive> %s\n", item->string);
+ }
+
+ if (die_write_report)
+ die("die with the --die-write-report option");
+ if (returns.nr)
+ for_each_string_list_item(item, &returns)
+ packet_write_fmt(1, "%s\n", item->string);
+ packet_flush(1);
+ sigchain_pop(SIGPIPE);
+
+ return 0;
+}
diff --git a/t/helper/test-progress.c b/t/helper/test-progress.c
new file mode 100644
index 0000000..66acb6a
--- /dev/null
+++ b/t/helper/test-progress.c
@@ -0,0 +1,97 @@
+/*
+ * A test helper to exercise the progress display.
+ *
+ * Reads instructions from standard input, one instruction per line:
+ *
+ * "start <total>[ <title>]" - Call start_progress(title, total),
+ * Uses the default title of "Working hard"
+ * if the " <title>" is omitted.
+ * "progress <items>" - Call display_progress() with the given item count
+ * as parameter.
+ * "throughput <bytes> <millis> - Call display_throughput() with the given
+ * byte count as parameter. The 'millis'
+ * specify the time elapsed since the
+ * start_progress() call.
+ * "update" - Set the 'progress_update' flag.
+ * "stop" - Call stop_progress().
+ *
+ * See 't0500-progress-display.sh' for examples.
+ */
+#define GIT_TEST_PROGRESS_ONLY
+#include "test-tool.h"
+#include "parse-options.h"
+#include "progress.h"
+#include "strbuf.h"
+#include "string-list.h"
+
+int cmd__progress(int argc, const char **argv)
+{
+ const char *const default_title = "Working hard";
+ struct string_list titles = STRING_LIST_INIT_DUP;
+ struct strbuf line = STRBUF_INIT;
+ struct progress *progress = NULL;
+
+ const char *usage[] = {
+ "test-tool progress <stdin",
+ NULL
+ };
+ struct option options[] = {
+ OPT_END(),
+ };
+
+ argc = parse_options(argc, argv, NULL, options, usage, 0);
+ if (argc)
+ usage_with_options(usage, options);
+
+ progress_testing = 1;
+ while (strbuf_getline(&line, stdin) != EOF) {
+ char *end;
+
+ if (skip_prefix(line.buf, "start ", (const char **) &end)) {
+ uint64_t total = strtoull(end, &end, 10);
+ const char *title;
+
+ /*
+ * We can't use "end + 1" as an argument to
+ * start_progress(), it doesn't xstrdup() its
+ * "title" argument. We need to hold onto a
+ * valid "char *" for it until the end.
+ */
+ if (!*end)
+ title = default_title;
+ else if (*end == ' ')
+ title = string_list_insert(&titles, end + 1)->string;
+ else
+ die("invalid input: '%s'\n", line.buf);
+
+ progress = start_progress(title, total);
+ } else if (skip_prefix(line.buf, "progress ", (const char **) &end)) {
+ uint64_t item_count = strtoull(end, &end, 10);
+ if (*end != '\0')
+ die("invalid input: '%s'\n", line.buf);
+ display_progress(progress, item_count);
+ } else if (skip_prefix(line.buf, "throughput ",
+ (const char **) &end)) {
+ uint64_t byte_count, test_ms;
+
+ byte_count = strtoull(end, &end, 10);
+ if (*end != ' ')
+ die("invalid input: '%s'\n", line.buf);
+ test_ms = strtoull(end + 1, &end, 10);
+ if (*end != '\0')
+ die("invalid input: '%s'\n", line.buf);
+ progress_test_ns = test_ms * 1000 * 1000;
+ display_throughput(progress, byte_count);
+ } else if (!strcmp(line.buf, "update")) {
+ progress_test_force_update();
+ } else if (!strcmp(line.buf, "stop")) {
+ stop_progress(&progress);
+ } else {
+ die("invalid input: '%s'\n", line.buf);
+ }
+ }
+ strbuf_release(&line);
+ string_list_clear(&titles, 0);
+
+ return 0;
+}
diff --git a/t/helper/test-reach.c b/t/helper/test-reach.c
new file mode 100644
index 0000000..3e17339
--- /dev/null
+++ b/t/helper/test-reach.c
@@ -0,0 +1,177 @@
+#include "test-tool.h"
+#include "commit.h"
+#include "commit-reach.h"
+#include "config.h"
+#include "gettext.h"
+#include "hex.h"
+#include "object-name.h"
+#include "parse-options.h"
+#include "ref-filter.h"
+#include "setup.h"
+#include "string-list.h"
+#include "tag.h"
+
+static void print_sorted_commit_ids(struct commit_list *list)
+{
+ int i;
+ struct string_list s = STRING_LIST_INIT_DUP;
+
+ while (list) {
+ string_list_append(&s, oid_to_hex(&list->item->object.oid));
+ list = list->next;
+ }
+
+ string_list_sort(&s);
+
+ for (i = 0; i < s.nr; i++)
+ printf("%s\n", s.items[i].string);
+
+ string_list_clear(&s, 0);
+}
+
+int cmd__reach(int ac, const char **av)
+{
+ struct object_id oid_A, oid_B;
+ struct commit *A, *B;
+ struct commit_list *X, *Y;
+ struct object_array X_obj = OBJECT_ARRAY_INIT;
+ struct commit **X_array, **Y_array;
+ int X_nr, X_alloc, Y_nr, Y_alloc;
+ struct strbuf buf = STRBUF_INIT;
+ struct repository *r = the_repository;
+
+ setup_git_directory();
+
+ if (ac < 2)
+ exit(1);
+
+ A = B = NULL;
+ X = Y = NULL;
+ X_nr = Y_nr = 0;
+ X_alloc = Y_alloc = 16;
+ ALLOC_ARRAY(X_array, X_alloc);
+ ALLOC_ARRAY(Y_array, Y_alloc);
+
+ while (strbuf_getline(&buf, stdin) != EOF) {
+ struct object_id oid;
+ struct object *orig;
+ struct object *peeled;
+ struct commit *c;
+ if (buf.len < 3)
+ continue;
+
+ if (repo_get_oid_committish(the_repository, buf.buf + 2, &oid))
+ die("failed to resolve %s", buf.buf + 2);
+
+ orig = parse_object(r, &oid);
+ peeled = deref_tag_noverify(orig);
+
+ if (!peeled)
+ die("failed to load commit for input %s resulting in oid %s\n",
+ buf.buf, oid_to_hex(&oid));
+
+ c = object_as_type(peeled, OBJ_COMMIT, 0);
+
+ if (!c)
+ die("failed to load commit for input %s resulting in oid %s\n",
+ buf.buf, oid_to_hex(&oid));
+
+ switch (buf.buf[0]) {
+ case 'A':
+ oidcpy(&oid_A, &oid);
+ A = c;
+ break;
+
+ case 'B':
+ oidcpy(&oid_B, &oid);
+ B = c;
+ break;
+
+ case 'X':
+ commit_list_insert(c, &X);
+ ALLOC_GROW(X_array, X_nr + 1, X_alloc);
+ X_array[X_nr++] = c;
+ add_object_array(orig, NULL, &X_obj);
+ break;
+
+ case 'Y':
+ commit_list_insert(c, &Y);
+ ALLOC_GROW(Y_array, Y_nr + 1, Y_alloc);
+ Y_array[Y_nr++] = c;
+ break;
+
+ default:
+ die("unexpected start of line: %c", buf.buf[0]);
+ }
+ }
+ strbuf_release(&buf);
+
+ if (!strcmp(av[1], "ref_newer"))
+ printf("%s(A,B):%d\n", av[1], ref_newer(&oid_A, &oid_B));
+ else if (!strcmp(av[1], "in_merge_bases"))
+ printf("%s(A,B):%d\n", av[1],
+ repo_in_merge_bases(the_repository, A, B));
+ else if (!strcmp(av[1], "in_merge_bases_many"))
+ printf("%s(A,X):%d\n", av[1],
+ repo_in_merge_bases_many(the_repository, A, X_nr, X_array));
+ else if (!strcmp(av[1], "is_descendant_of"))
+ printf("%s(A,X):%d\n", av[1], repo_is_descendant_of(r, A, X));
+ else if (!strcmp(av[1], "get_merge_bases_many")) {
+ struct commit_list *list = repo_get_merge_bases_many(the_repository,
+ A, X_nr,
+ X_array);
+ printf("%s(A,X):\n", av[1]);
+ print_sorted_commit_ids(list);
+ } else if (!strcmp(av[1], "reduce_heads")) {
+ struct commit_list *list = reduce_heads(X);
+ printf("%s(X):\n", av[1]);
+ print_sorted_commit_ids(list);
+ } else if (!strcmp(av[1], "can_all_from_reach")) {
+ printf("%s(X,Y):%d\n", av[1], can_all_from_reach(X, Y, 1));
+ } else if (!strcmp(av[1], "can_all_from_reach_with_flag")) {
+ struct commit_list *iter = Y;
+
+ while (iter) {
+ iter->item->object.flags |= 2;
+ iter = iter->next;
+ }
+
+ printf("%s(X,_,_,0,0):%d\n", av[1], can_all_from_reach_with_flag(&X_obj, 2, 4, 0, 0));
+ } else if (!strcmp(av[1], "commit_contains")) {
+ struct ref_filter filter = REF_FILTER_INIT;
+ struct contains_cache cache;
+ init_contains_cache(&cache);
+
+ if (ac > 2 && !strcmp(av[2], "--tag"))
+ filter.with_commit_tag_algo = 1;
+ else
+ filter.with_commit_tag_algo = 0;
+
+ printf("%s(_,A,X,_):%d\n", av[1], commit_contains(&filter, A, X, &cache));
+ } else if (!strcmp(av[1], "get_reachable_subset")) {
+ const int reachable_flag = 1;
+ int i, count = 0;
+ struct commit_list *current;
+ struct commit_list *list = get_reachable_subset(X_array, X_nr,
+ Y_array, Y_nr,
+ reachable_flag);
+ printf("get_reachable_subset(X,Y)\n");
+ for (current = list; current; current = current->next) {
+ if (!(list->item->object.flags & reachable_flag))
+ die(_("commit %s is not marked reachable"),
+ oid_to_hex(&list->item->object.oid));
+ count++;
+ }
+ for (i = 0; i < Y_nr; i++) {
+ if (Y_array[i]->object.flags & reachable_flag)
+ count--;
+ }
+
+ if (count < 0)
+ die(_("too many commits marked reachable"));
+
+ print_sorted_commit_ids(list);
+ }
+
+ return 0;
+}
diff --git a/t/helper/test-read-cache.c b/t/helper/test-read-cache.c
new file mode 100644
index 0000000..1acd362
--- /dev/null
+++ b/t/helper/test-read-cache.c
@@ -0,0 +1,42 @@
+#define USE_THE_INDEX_VARIABLE
+#include "test-tool.h"
+#include "config.h"
+#include "read-cache-ll.h"
+#include "repository.h"
+#include "setup.h"
+
+int cmd__read_cache(int argc, const char **argv)
+{
+ int i, cnt = 1;
+ const char *name = NULL;
+
+ initialize_the_repository();
+
+ if (argc > 1 && skip_prefix(argv[1], "--print-and-refresh=", &name)) {
+ argc--;
+ argv++;
+ }
+
+ if (argc == 2)
+ cnt = strtol(argv[1], NULL, 0);
+ setup_git_directory();
+ git_config(git_default_config, NULL);
+
+ for (i = 0; i < cnt; i++) {
+ repo_read_index(the_repository);
+ if (name) {
+ int pos;
+
+ refresh_index(&the_index, REFRESH_QUIET,
+ NULL, NULL, NULL);
+ pos = index_name_pos(&the_index, name, strlen(name));
+ if (pos < 0)
+ die("%s not in index", name);
+ printf("%s is%s up to date\n", name,
+ ce_uptodate(the_index.cache[pos]) ? "" : " not");
+ write_file(name, "%d\n", i);
+ }
+ discard_index(&the_index);
+ }
+ return 0;
+}
diff --git a/t/helper/test-read-graph.c b/t/helper/test-read-graph.c
new file mode 100644
index 0000000..8c7a83f
--- /dev/null
+++ b/t/helper/test-read-graph.c
@@ -0,0 +1,64 @@
+#include "test-tool.h"
+#include "commit-graph.h"
+#include "repository.h"
+#include "object-store-ll.h"
+#include "bloom.h"
+#include "setup.h"
+
+int cmd__read_graph(int argc UNUSED, const char **argv UNUSED)
+{
+ struct commit_graph *graph = NULL;
+ struct object_directory *odb;
+
+ setup_git_directory();
+ odb = the_repository->objects->odb;
+
+ prepare_repo_settings(the_repository);
+
+ graph = read_commit_graph_one(the_repository, odb);
+ if (!graph)
+ return 1;
+
+ printf("header: %08x %d %d %d %d\n",
+ ntohl(*(uint32_t*)graph->data),
+ *(unsigned char*)(graph->data + 4),
+ *(unsigned char*)(graph->data + 5),
+ *(unsigned char*)(graph->data + 6),
+ *(unsigned char*)(graph->data + 7));
+ printf("num_commits: %u\n", graph->num_commits);
+ printf("chunks:");
+
+ if (graph->chunk_oid_fanout)
+ printf(" oid_fanout");
+ if (graph->chunk_oid_lookup)
+ printf(" oid_lookup");
+ if (graph->chunk_commit_data)
+ printf(" commit_metadata");
+ if (graph->chunk_generation_data)
+ printf(" generation_data");
+ if (graph->chunk_generation_data_overflow)
+ printf(" generation_data_overflow");
+ if (graph->chunk_extra_edges)
+ printf(" extra_edges");
+ if (graph->chunk_bloom_indexes)
+ printf(" bloom_indexes");
+ if (graph->chunk_bloom_data)
+ printf(" bloom_data");
+ printf("\n");
+
+ printf("options:");
+ if (graph->bloom_filter_settings)
+ printf(" bloom(%"PRIu32",%"PRIu32",%"PRIu32")",
+ graph->bloom_filter_settings->hash_version,
+ graph->bloom_filter_settings->bits_per_entry,
+ graph->bloom_filter_settings->num_hashes);
+ if (graph->read_generation_data)
+ printf(" read_generation_data");
+ if (graph->topo_levels)
+ printf(" topo_levels");
+ printf("\n");
+
+ UNLEAK(graph);
+
+ return 0;
+}
diff --git a/t/helper/test-read-midx.c b/t/helper/test-read-midx.c
new file mode 100644
index 0000000..e9a444d
--- /dev/null
+++ b/t/helper/test-read-midx.c
@@ -0,0 +1,115 @@
+#include "test-tool.h"
+#include "hex.h"
+#include "midx.h"
+#include "repository.h"
+#include "object-store-ll.h"
+#include "pack-bitmap.h"
+#include "packfile.h"
+#include "setup.h"
+
+static int read_midx_file(const char *object_dir, int show_objects)
+{
+ uint32_t i;
+ struct multi_pack_index *m;
+
+ setup_git_directory();
+ m = load_multi_pack_index(object_dir, 1);
+
+ if (!m)
+ return 1;
+
+ printf("header: %08x %d %d %d %d\n",
+ m->signature,
+ m->version,
+ m->hash_len,
+ m->num_chunks,
+ m->num_packs);
+
+ printf("chunks:");
+
+ if (m->chunk_pack_names)
+ printf(" pack-names");
+ if (m->chunk_oid_fanout)
+ printf(" oid-fanout");
+ if (m->chunk_oid_lookup)
+ printf(" oid-lookup");
+ if (m->chunk_object_offsets)
+ printf(" object-offsets");
+ if (m->chunk_large_offsets)
+ printf(" large-offsets");
+
+ printf("\nnum_objects: %d\n", m->num_objects);
+
+ printf("packs:\n");
+ for (i = 0; i < m->num_packs; i++)
+ printf("%s\n", m->pack_names[i]);
+
+ printf("object-dir: %s\n", m->object_dir);
+
+ if (show_objects) {
+ struct object_id oid;
+ struct pack_entry e;
+
+ for (i = 0; i < m->num_objects; i++) {
+ nth_midxed_object_oid(&oid, m, i);
+ fill_midx_entry(the_repository, &oid, &e, m);
+
+ printf("%s %"PRIu64"\t%s\n",
+ oid_to_hex(&oid), e.offset, e.p->pack_name);
+ }
+ }
+
+ close_midx(m);
+
+ return 0;
+}
+
+static int read_midx_checksum(const char *object_dir)
+{
+ struct multi_pack_index *m;
+
+ setup_git_directory();
+ m = load_multi_pack_index(object_dir, 1);
+ if (!m)
+ return 1;
+ printf("%s\n", hash_to_hex(get_midx_checksum(m)));
+ return 0;
+}
+
+static int read_midx_preferred_pack(const char *object_dir)
+{
+ struct multi_pack_index *midx = NULL;
+ struct bitmap_index *bitmap = NULL;
+
+ setup_git_directory();
+
+ midx = load_multi_pack_index(object_dir, 1);
+ if (!midx)
+ return 1;
+
+ bitmap = prepare_bitmap_git(the_repository);
+ if (!bitmap)
+ return 1;
+ if (!bitmap_is_midx(bitmap)) {
+ free_bitmap_index(bitmap);
+ return 1;
+ }
+
+ printf("%s\n", midx->pack_names[midx_preferred_pack(bitmap)]);
+ free_bitmap_index(bitmap);
+ return 0;
+}
+
+int cmd__read_midx(int argc, const char **argv)
+{
+ if (!(argc == 2 || argc == 3))
+ usage("read-midx [--show-objects|--checksum|--preferred-pack] <object-dir>");
+
+ if (!strcmp(argv[1], "--show-objects"))
+ return read_midx_file(argv[2], 1);
+ else if (!strcmp(argv[1], "--checksum"))
+ return read_midx_checksum(argv[2]);
+ else if (!strcmp(argv[1], "--preferred-pack"))
+ return read_midx_preferred_pack(argv[2]);
+ return read_midx_file(argv[1], 0);
+}
diff --git a/t/helper/test-ref-store.c b/t/helper/test-ref-store.c
new file mode 100644
index 0000000..48552e6
--- /dev/null
+++ b/t/helper/test-ref-store.c
@@ -0,0 +1,361 @@
+#include "test-tool.h"
+#include "hex.h"
+#include "refs.h"
+#include "setup.h"
+#include "worktree.h"
+#include "object-store-ll.h"
+#include "path.h"
+#include "repository.h"
+#include "strbuf.h"
+#include "revision.h"
+
+struct flag_definition {
+ const char *name;
+ uint64_t mask;
+};
+
+#define FLAG_DEF(x) \
+ { \
+#x, (x) \
+ }
+
+static unsigned int parse_flags(const char *str, struct flag_definition *defs)
+{
+ struct string_list masks = STRING_LIST_INIT_DUP;
+ int i = 0;
+ unsigned int result = 0;
+
+ if (!strcmp(str, "0"))
+ return 0;
+
+ string_list_split(&masks, str, ',', 64);
+ for (; i < masks.nr; i++) {
+ const char *name = masks.items[i].string;
+ struct flag_definition *def = defs;
+ int found = 0;
+ while (def->name) {
+ if (!strcmp(def->name, name)) {
+ result |= def->mask;
+ found = 1;
+ break;
+ }
+ def++;
+ }
+ if (!found)
+ die("unknown flag \"%s\"", name);
+ }
+
+ string_list_clear(&masks, 0);
+ return result;
+}
+
+static struct flag_definition empty_flags[] = { { NULL, 0 } };
+
+static const char *notnull(const char *arg, const char *name)
+{
+ if (!arg)
+ die("%s required", name);
+ return arg;
+}
+
+static unsigned int arg_flags(const char *arg, const char *name,
+ struct flag_definition *defs)
+{
+ return parse_flags(notnull(arg, name), defs);
+}
+
+static const char **get_store(const char **argv, struct ref_store **refs)
+{
+ const char *gitdir;
+
+ if (!argv[0]) {
+ die("ref store required");
+ } else if (!strcmp(argv[0], "main")) {
+ *refs = get_main_ref_store(the_repository);
+ } else if (skip_prefix(argv[0], "submodule:", &gitdir)) {
+ struct strbuf sb = STRBUF_INIT;
+ int ret;
+
+ ret = strbuf_git_path_submodule(&sb, gitdir, "objects/");
+ if (ret)
+ die("strbuf_git_path_submodule failed: %d", ret);
+ add_to_alternates_memory(sb.buf);
+ strbuf_release(&sb);
+
+ *refs = get_submodule_ref_store(gitdir);
+ } else if (skip_prefix(argv[0], "worktree:", &gitdir)) {
+ struct worktree **p, **worktrees = get_worktrees();
+
+ for (p = worktrees; *p; p++) {
+ struct worktree *wt = *p;
+
+ if (!wt->id) {
+ /* special case for main worktree */
+ if (!strcmp(gitdir, "main"))
+ break;
+ } else if (!strcmp(gitdir, wt->id))
+ break;
+ }
+ if (!*p)
+ die("no such worktree: %s", gitdir);
+
+ *refs = get_worktree_ref_store(*p);
+ free_worktrees(worktrees);
+ } else
+ die("unknown backend %s", argv[0]);
+
+ if (!*refs)
+ die("no ref store");
+
+ /* consume store-specific optional arguments if needed */
+
+ return argv + 1;
+}
+
+static struct flag_definition pack_flags[] = { FLAG_DEF(PACK_REFS_PRUNE),
+ FLAG_DEF(PACK_REFS_ALL),
+ { NULL, 0 } };
+
+static int cmd_pack_refs(struct ref_store *refs, const char **argv)
+{
+ unsigned int flags = arg_flags(*argv++, "flags", pack_flags);
+ static struct ref_exclusions exclusions = REF_EXCLUSIONS_INIT;
+ static struct string_list included_refs = STRING_LIST_INIT_NODUP;
+ struct pack_refs_opts pack_opts = { .flags = flags,
+ .exclusions = &exclusions,
+ .includes = &included_refs };
+
+ if (pack_opts.flags & PACK_REFS_ALL)
+ string_list_append(pack_opts.includes, "*");
+
+ return refs_pack_refs(refs, &pack_opts);
+}
+
+static int cmd_create_symref(struct ref_store *refs, const char **argv)
+{
+ const char *refname = notnull(*argv++, "refname");
+ const char *target = notnull(*argv++, "target");
+ const char *logmsg = *argv++;
+
+ return refs_create_symref(refs, refname, target, logmsg);
+}
+
+static struct flag_definition transaction_flags[] = {
+ FLAG_DEF(REF_NO_DEREF),
+ FLAG_DEF(REF_FORCE_CREATE_REFLOG),
+ FLAG_DEF(REF_SKIP_OID_VERIFICATION),
+ FLAG_DEF(REF_SKIP_REFNAME_VERIFICATION),
+ { NULL, 0 }
+};
+
+static int cmd_delete_refs(struct ref_store *refs, const char **argv)
+{
+ unsigned int flags = arg_flags(*argv++, "flags", transaction_flags);
+ const char *msg = *argv++;
+ struct string_list refnames = STRING_LIST_INIT_NODUP;
+ int result;
+
+ while (*argv)
+ string_list_append(&refnames, *argv++);
+
+ result = refs_delete_refs(refs, msg, &refnames, flags);
+ string_list_clear(&refnames, 0);
+ return result;
+}
+
+static int cmd_rename_ref(struct ref_store *refs, const char **argv)
+{
+ const char *oldref = notnull(*argv++, "oldref");
+ const char *newref = notnull(*argv++, "newref");
+ const char *logmsg = *argv++;
+
+ return refs_rename_ref(refs, oldref, newref, logmsg);
+}
+
+static int each_ref(const char *refname, const struct object_id *oid,
+ int flags, void *cb_data UNUSED)
+{
+ printf("%s %s 0x%x\n", oid_to_hex(oid), refname, flags);
+ return 0;
+}
+
+static int cmd_for_each_ref(struct ref_store *refs, const char **argv)
+{
+ const char *prefix = notnull(*argv++, "prefix");
+
+ return refs_for_each_ref_in(refs, prefix, each_ref, NULL);
+}
+
+static int cmd_for_each_ref__exclude(struct ref_store *refs, const char **argv)
+{
+ const char *prefix = notnull(*argv++, "prefix");
+ const char **exclude_patterns = argv;
+
+ return refs_for_each_fullref_in(refs, prefix, exclude_patterns, each_ref,
+ NULL);
+}
+
+static int cmd_resolve_ref(struct ref_store *refs, const char **argv)
+{
+ struct object_id oid = *null_oid();
+ const char *refname = notnull(*argv++, "refname");
+ int resolve_flags = arg_flags(*argv++, "resolve-flags", empty_flags);
+ int flags;
+ const char *ref;
+
+ ref = refs_resolve_ref_unsafe(refs, refname, resolve_flags,
+ &oid, &flags);
+ printf("%s %s 0x%x\n", oid_to_hex(&oid), ref ? ref : "(null)", flags);
+ return ref ? 0 : 1;
+}
+
+static int cmd_verify_ref(struct ref_store *refs, const char **argv)
+{
+ const char *refname = notnull(*argv++, "refname");
+ struct strbuf err = STRBUF_INIT;
+ int ret;
+
+ ret = refs_verify_refname_available(refs, refname, NULL, NULL, &err);
+ if (err.len)
+ puts(err.buf);
+ return ret;
+}
+
+static int cmd_for_each_reflog(struct ref_store *refs,
+ const char **argv UNUSED)
+{
+ return refs_for_each_reflog(refs, each_ref, NULL);
+}
+
+static int each_reflog(struct object_id *old_oid, struct object_id *new_oid,
+ const char *committer, timestamp_t timestamp,
+ int tz, const char *msg, void *cb_data UNUSED)
+{
+ printf("%s %s %s %" PRItime " %+05d%s%s", oid_to_hex(old_oid),
+ oid_to_hex(new_oid), committer, timestamp, tz,
+ *msg == '\n' ? "" : "\t", msg);
+ return 0;
+}
+
+static int cmd_for_each_reflog_ent(struct ref_store *refs, const char **argv)
+{
+ const char *refname = notnull(*argv++, "refname");
+
+ return refs_for_each_reflog_ent(refs, refname, each_reflog, refs);
+}
+
+static int cmd_for_each_reflog_ent_reverse(struct ref_store *refs, const char **argv)
+{
+ const char *refname = notnull(*argv++, "refname");
+
+ return refs_for_each_reflog_ent_reverse(refs, refname, each_reflog, refs);
+}
+
+static int cmd_reflog_exists(struct ref_store *refs, const char **argv)
+{
+ const char *refname = notnull(*argv++, "refname");
+
+ return !refs_reflog_exists(refs, refname);
+}
+
+static int cmd_create_reflog(struct ref_store *refs, const char **argv)
+{
+ const char *refname = notnull(*argv++, "refname");
+ struct strbuf err = STRBUF_INIT;
+ int ret;
+
+ ret = refs_create_reflog(refs, refname, &err);
+ if (err.len)
+ puts(err.buf);
+ return ret;
+}
+
+static int cmd_delete_reflog(struct ref_store *refs, const char **argv)
+{
+ const char *refname = notnull(*argv++, "refname");
+
+ return refs_delete_reflog(refs, refname);
+}
+
+static int cmd_delete_ref(struct ref_store *refs, const char **argv)
+{
+ const char *msg = notnull(*argv++, "msg");
+ const char *refname = notnull(*argv++, "refname");
+ const char *sha1_buf = notnull(*argv++, "old-sha1");
+ unsigned int flags = arg_flags(*argv++, "flags", transaction_flags);
+ struct object_id old_oid;
+
+ if (get_oid_hex(sha1_buf, &old_oid))
+ die("cannot parse %s as %s", sha1_buf, the_hash_algo->name);
+
+ return refs_delete_ref(refs, msg, refname, &old_oid, flags);
+}
+
+static int cmd_update_ref(struct ref_store *refs, const char **argv)
+{
+ const char *msg = notnull(*argv++, "msg");
+ const char *refname = notnull(*argv++, "refname");
+ const char *new_sha1_buf = notnull(*argv++, "new-sha1");
+ const char *old_sha1_buf = notnull(*argv++, "old-sha1");
+ unsigned int flags = arg_flags(*argv++, "flags", transaction_flags);
+ struct object_id old_oid;
+ struct object_id new_oid;
+
+ if (get_oid_hex(old_sha1_buf, &old_oid))
+ die("cannot parse %s as %s", old_sha1_buf, the_hash_algo->name);
+ if (get_oid_hex(new_sha1_buf, &new_oid))
+ die("cannot parse %s as %s", new_sha1_buf, the_hash_algo->name);
+
+ return refs_update_ref(refs, msg, refname,
+ &new_oid, &old_oid,
+ flags, UPDATE_REFS_DIE_ON_ERR);
+}
+
+struct command {
+ const char *name;
+ int (*func)(struct ref_store *refs, const char **argv);
+};
+
+static struct command commands[] = {
+ { "pack-refs", cmd_pack_refs },
+ { "create-symref", cmd_create_symref },
+ { "delete-refs", cmd_delete_refs },
+ { "rename-ref", cmd_rename_ref },
+ { "for-each-ref", cmd_for_each_ref },
+ { "for-each-ref--exclude", cmd_for_each_ref__exclude },
+ { "resolve-ref", cmd_resolve_ref },
+ { "verify-ref", cmd_verify_ref },
+ { "for-each-reflog", cmd_for_each_reflog },
+ { "for-each-reflog-ent", cmd_for_each_reflog_ent },
+ { "for-each-reflog-ent-reverse", cmd_for_each_reflog_ent_reverse },
+ { "reflog-exists", cmd_reflog_exists },
+ { "create-reflog", cmd_create_reflog },
+ { "delete-reflog", cmd_delete_reflog },
+ /*
+ * backend transaction functions can't be tested separately
+ */
+ { "delete-ref", cmd_delete_ref },
+ { "update-ref", cmd_update_ref },
+ { NULL, NULL }
+};
+
+int cmd__ref_store(int argc UNUSED, const char **argv)
+{
+ struct ref_store *refs;
+ const char *func;
+ struct command *cmd;
+
+ setup_git_directory();
+
+ argv = get_store(argv + 1, &refs);
+
+ func = *argv++;
+ if (!func)
+ die("ref function required");
+ for (cmd = commands; cmd->name; cmd++) {
+ if (!strcmp(func, cmd->name))
+ return cmd->func(refs, argv);
+ }
+ die("unknown function %s", func);
+ return 0;
+}
diff --git a/t/helper/test-reftable.c b/t/helper/test-reftable.c
new file mode 100644
index 0000000..00237ef
--- /dev/null
+++ b/t/helper/test-reftable.c
@@ -0,0 +1,23 @@
+#include "reftable/system.h"
+#include "reftable/reftable-tests.h"
+#include "test-tool.h"
+
+int cmd__reftable(int argc, const char **argv)
+{
+ /* test from simple to complex. */
+ basics_test_main(argc, argv);
+ record_test_main(argc, argv);
+ block_test_main(argc, argv);
+ tree_test_main(argc, argv);
+ pq_test_main(argc, argv);
+ readwrite_test_main(argc, argv);
+ merged_test_main(argc, argv);
+ stack_test_main(argc, argv);
+ refname_test_main(argc, argv);
+ return 0;
+}
+
+int cmd__dump_reftable(int argc, const char **argv)
+{
+ return reftable_dump_main(argc, (char *const *)argv);
+}
diff --git a/t/helper/test-regex.c b/t/helper/test-regex.c
new file mode 100644
index 0000000..bd871a7
--- /dev/null
+++ b/t/helper/test-regex.c
@@ -0,0 +1,117 @@
+#include "test-tool.h"
+#include "gettext.h"
+
+struct reg_flag {
+ const char *name;
+ int flag;
+};
+
+static struct reg_flag reg_flags[] = {
+ { "EXTENDED", REG_EXTENDED },
+ { "NEWLINE", REG_NEWLINE },
+ { "ICASE", REG_ICASE },
+ { "NOTBOL", REG_NOTBOL },
+ { "NOTEOL", REG_NOTEOL },
+#ifdef REG_STARTEND
+ { "STARTEND", REG_STARTEND },
+#endif
+ { NULL, 0 }
+};
+
+static int test_regex_bug(void)
+{
+ char *pat = "[^={} \t]+";
+ char *str = "={}\nfred";
+ regex_t r;
+ regmatch_t m[1];
+
+ if (regcomp(&r, pat, REG_EXTENDED | REG_NEWLINE))
+ die("failed regcomp() for pattern '%s'", pat);
+ if (regexec(&r, str, 1, m, 0))
+ die("no match of pattern '%s' to string '%s'", pat, str);
+
+ /* http://sourceware.org/bugzilla/show_bug.cgi?id=3957 */
+ if (m[0].rm_so == 3) /* matches '\n' when it should not */
+ die("regex bug confirmed: re-build git with NO_REGEX=1");
+
+ regfree(&r);
+ return 0;
+}
+
+int cmd__regex(int argc, const char **argv)
+{
+ const char *pat;
+ const char *str;
+ int ret, silent = 0, flags = 0;
+ regex_t r;
+ regmatch_t m[1];
+ char errbuf[64];
+
+ argv++;
+ argc--;
+
+ if (!argc)
+ goto usage;
+
+ if (!strcmp(*argv, "--bug")) {
+ if (argc == 1)
+ return test_regex_bug();
+ else
+ goto usage;
+ }
+ if (!strcmp(*argv, "--silent")) {
+ silent = 1;
+ argv++;
+ argc--;
+ }
+ if (!argc)
+ goto usage;
+
+ pat = *argv++;
+ if (argc == 1)
+ str = NULL;
+ else {
+ str = *argv++;
+ while (*argv) {
+ struct reg_flag *rf;
+ for (rf = reg_flags; rf->name; rf++)
+ if (!strcmp(*argv, rf->name)) {
+ flags |= rf->flag;
+ break;
+ }
+ if (!rf->name)
+ die("do not recognize flag %s", *argv);
+ argv++;
+ }
+ }
+ git_setup_gettext();
+
+ ret = regcomp(&r, pat, flags);
+ if (ret) {
+ if (silent)
+ return ret;
+
+ regerror(ret, &r, errbuf, sizeof(errbuf));
+ die("failed regcomp() for pattern '%s' (%s)", pat, errbuf);
+ }
+ if (!str)
+ goto cleanup;
+
+ ret = regexec(&r, str, 1, m, 0);
+ if (ret) {
+ if (silent || ret == REG_NOMATCH)
+ goto cleanup;
+
+ regerror(ret, &r, errbuf, sizeof(errbuf));
+ die("failed regexec() for subject '%s' (%s)", str, errbuf);
+ }
+
+cleanup:
+ regfree(&r);
+ return ret;
+usage:
+ usage("\ttest-tool regex --bug\n"
+ "\ttest-tool regex [--silent] <pattern>\n"
+ "\ttest-tool regex [--silent] <pattern> <string> [<options>]");
+ return -1;
+}
diff --git a/t/helper/test-repository.c b/t/helper/test-repository.c
new file mode 100644
index 0000000..4cd8a95
--- /dev/null
+++ b/t/helper/test-repository.c
@@ -0,0 +1,102 @@
+#include "test-tool.h"
+#include "commit-graph.h"
+#include "commit.h"
+#include "config.h"
+#include "environment.h"
+#include "hex.h"
+#include "object-store-ll.h"
+#include "object.h"
+#include "repository.h"
+#include "setup.h"
+#include "tree.h"
+
+static void test_parse_commit_in_graph(const char *gitdir, const char *worktree,
+ const struct object_id *commit_oid)
+{
+ struct repository r;
+ struct commit *c;
+ struct commit_list *parent;
+
+ setup_git_env(gitdir);
+
+ memset(the_repository, 0, sizeof(*the_repository));
+
+ if (repo_init(&r, gitdir, worktree))
+ die("Couldn't init repo");
+
+ repo_set_hash_algo(the_repository, hash_algo_by_ptr(r.hash_algo));
+
+ c = lookup_commit(&r, commit_oid);
+
+ if (!parse_commit_in_graph(&r, c))
+ die("Couldn't parse commit");
+
+ printf("%"PRItime, c->date);
+ for (parent = c->parents; parent; parent = parent->next)
+ printf(" %s", oid_to_hex(&parent->item->object.oid));
+ printf("\n");
+
+ repo_clear(&r);
+}
+
+static void test_get_commit_tree_in_graph(const char *gitdir,
+ const char *worktree,
+ const struct object_id *commit_oid)
+{
+ struct repository r;
+ struct commit *c;
+ struct tree *tree;
+
+ setup_git_env(gitdir);
+
+ memset(the_repository, 0, sizeof(*the_repository));
+
+ if (repo_init(&r, gitdir, worktree))
+ die("Couldn't init repo");
+
+ repo_set_hash_algo(the_repository, hash_algo_by_ptr(r.hash_algo));
+
+ c = lookup_commit(&r, commit_oid);
+
+ /*
+ * get_commit_tree_in_graph does not automatically parse the commit, so
+ * parse it first.
+ */
+ if (!parse_commit_in_graph(&r, c))
+ die("Couldn't parse commit");
+ tree = get_commit_tree_in_graph(&r, c);
+ if (!tree)
+ die("Couldn't get commit tree");
+
+ printf("%s\n", oid_to_hex(&tree->object.oid));
+
+ repo_clear(&r);
+}
+
+int cmd__repository(int argc, const char **argv)
+{
+ int nongit_ok = 0;
+
+ setup_git_directory_gently(&nongit_ok);
+
+ if (argc < 2)
+ die("must have at least 2 arguments");
+ if (!strcmp(argv[1], "parse_commit_in_graph")) {
+ struct object_id oid;
+ if (argc < 5)
+ die("not enough arguments");
+ if (parse_oid_hex(argv[4], &oid, &argv[4]))
+ die("cannot parse oid '%s'", argv[4]);
+ test_parse_commit_in_graph(argv[2], argv[3], &oid);
+ } else if (!strcmp(argv[1], "get_commit_tree_in_graph")) {
+ struct object_id oid;
+ if (argc < 5)
+ die("not enough arguments");
+ if (parse_oid_hex(argv[4], &oid, &argv[4]))
+ die("cannot parse oid '%s'", argv[4]);
+ test_get_commit_tree_in_graph(argv[2], argv[3], &oid);
+ } else {
+ die("unrecognized '%s'", argv[1]);
+ }
+ return 0;
+}
diff --git a/t/helper/test-revision-walking.c b/t/helper/test-revision-walking.c
new file mode 100644
index 0000000..f346951
--- /dev/null
+++ b/t/helper/test-revision-walking.c
@@ -0,0 +1,72 @@
+/*
+ * test-revision-walking.c: test revision walking API.
+ *
+ * (C) 2012 Heiko Voigt <hvoigt@hvoigt.net>
+ *
+ * This code is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include "test-tool.h"
+#include "commit.h"
+#include "diff.h"
+#include "repository.h"
+#include "revision.h"
+#include "setup.h"
+
+static void print_commit(struct commit *commit)
+{
+ struct strbuf sb = STRBUF_INIT;
+ struct pretty_print_context ctx = {0};
+ ctx.date_mode.type = DATE_NORMAL;
+ repo_format_commit_message(the_repository, commit, " %m %s", &sb,
+ &ctx);
+ printf("%s\n", sb.buf);
+ strbuf_release(&sb);
+}
+
+static int run_revision_walk(void)
+{
+ struct rev_info rev;
+ struct commit *commit;
+ const char *argv[] = {NULL, "--all", NULL};
+ int argc = ARRAY_SIZE(argv) - 1;
+ int got_revision = 0;
+
+ repo_init_revisions(the_repository, &rev, NULL);
+ setup_revisions(argc, argv, &rev, NULL);
+ if (prepare_revision_walk(&rev))
+ die("revision walk setup failed");
+
+ while ((commit = get_revision(&rev)) != NULL) {
+ print_commit(commit);
+ got_revision = 1;
+ }
+
+ reset_revision_walk();
+ release_revisions(&rev);
+ return got_revision;
+}
+
+int cmd__revision_walking(int argc, const char **argv)
+{
+ if (argc < 2)
+ return 1;
+
+ setup_git_directory();
+
+ if (!strcmp(argv[1], "run-twice")) {
+ printf("1st\n");
+ if (!run_revision_walk())
+ return 1;
+ printf("2nd\n");
+ if (!run_revision_walk())
+ return 1;
+
+ return 0;
+ }
+
+ fprintf(stderr, "check usage\n");
+ return 1;
+}
diff --git a/t/helper/test-rot13-filter.c b/t/helper/test-rot13-filter.c
new file mode 100644
index 0000000..f8d564c
--- /dev/null
+++ b/t/helper/test-rot13-filter.c
@@ -0,0 +1,382 @@
+/*
+ * Example implementation for the Git filter protocol version 2
+ * See Documentation/gitattributes.txt, section "Filter Protocol"
+ *
+ * Usage: test-tool rot13-filter [--always-delay] --log=<path> <capabilities>
+ *
+ * Log path defines a debug log file that the script writes to. The
+ * subsequent arguments define a list of supported protocol capabilities
+ * ("clean", "smudge", etc).
+ *
+ * When --always-delay is given all pathnames with the "can-delay" flag
+ * that don't appear on the list bellow are delayed with a count of 1
+ * (see more below).
+ *
+ * This implementation supports special test cases:
+ * (1) If data with the pathname "clean-write-fail.r" is processed with
+ * a "clean" operation then the write operation will die.
+ * (2) If data with the pathname "smudge-write-fail.r" is processed with
+ * a "smudge" operation then the write operation will die.
+ * (3) If data with the pathname "error.r" is processed with any
+ * operation then the filter signals that it cannot or does not want
+ * to process the file.
+ * (4) If data with the pathname "abort.r" is processed with any
+ * operation then the filter signals that it cannot or does not want
+ * to process the file and any file after that is processed with the
+ * same command.
+ * (5) If data with a pathname that is a key in the delay hash is
+ * requested (e.g. "test-delay10.a") then the filter responds with
+ * a "delay" status and sets the "requested" field in the delay hash.
+ * The filter will signal the availability of this object after
+ * "count" (field in delay hash) "list_available_blobs" commands.
+ * (6) If data with the pathname "missing-delay.a" is processed that the
+ * filter will drop the path from the "list_available_blobs" response.
+ * (7) If data with the pathname "invalid-delay.a" is processed that the
+ * filter will add the path "unfiltered" which was not delayed before
+ * to the "list_available_blobs" response.
+ */
+
+#include "test-tool.h"
+#include "pkt-line.h"
+#include "string-list.h"
+#include "strmap.h"
+#include "parse-options.h"
+
+static FILE *logfile;
+static int always_delay, has_clean_cap, has_smudge_cap;
+static struct strmap delay = STRMAP_INIT;
+
+static inline const char *str_or_null(const char *str)
+{
+ return str ? str : "(null)";
+}
+
+static char *rot13(char *str)
+{
+ char *c;
+ for (c = str; *c; c++)
+ if (isalpha(*c))
+ *c += tolower(*c) < 'n' ? 13 : -13;
+ return str;
+}
+
+static char *get_value(char *buf, const char *key)
+{
+ const char *orig_buf = buf;
+ if (!buf ||
+ !skip_prefix((const char *)buf, key, (const char **)&buf) ||
+ !skip_prefix((const char *)buf, "=", (const char **)&buf) ||
+ !*buf)
+ die("expected key '%s', got '%s'", key, str_or_null(orig_buf));
+ return buf;
+}
+
+/*
+ * Read a text packet, expecting that it is in the form "key=value" for
+ * the given key. An EOF does not trigger any error and is reported
+ * back to the caller with NULL. Die if the "key" part of "key=value" does
+ * not match the given key, or the value part is empty.
+ */
+static char *packet_key_val_read(const char *key)
+{
+ char *buf;
+ if (packet_read_line_gently(0, NULL, &buf) < 0)
+ return NULL;
+ return xstrdup(get_value(buf, key));
+}
+
+static inline void assert_remote_capability(struct strset *caps, const char *cap)
+{
+ if (!strset_contains(caps, cap))
+ die("required '%s' capability not available from remote", cap);
+}
+
+static void read_capabilities(struct strset *remote_caps)
+{
+ for (;;) {
+ char *buf = packet_read_line(0, NULL);
+ if (!buf)
+ break;
+ strset_add(remote_caps, get_value(buf, "capability"));
+ }
+
+ assert_remote_capability(remote_caps, "clean");
+ assert_remote_capability(remote_caps, "smudge");
+ assert_remote_capability(remote_caps, "delay");
+}
+
+static void check_and_write_capabilities(struct strset *remote_caps,
+ const char **caps, int nr_caps)
+{
+ int i;
+ for (i = 0; i < nr_caps; i++) {
+ if (!strset_contains(remote_caps, caps[i]))
+ die("our capability '%s' is not available from remote",
+ caps[i]);
+ packet_write_fmt(1, "capability=%s\n", caps[i]);
+ }
+ packet_flush(1);
+}
+
+struct delay_entry {
+ int requested, count;
+ char *output;
+};
+
+static void free_delay_entries(void)
+{
+ struct hashmap_iter iter;
+ struct strmap_entry *ent;
+
+ strmap_for_each_entry(&delay, &iter, ent) {
+ struct delay_entry *delay_entry = ent->value;
+ free(delay_entry->output);
+ free(delay_entry);
+ }
+ strmap_clear(&delay, 0);
+}
+
+static void add_delay_entry(char *pathname, int count, int requested)
+{
+ struct delay_entry *entry = xcalloc(1, sizeof(*entry));
+ entry->count = count;
+ entry->requested = requested;
+ if (strmap_put(&delay, pathname, entry))
+ BUG("adding the same path twice to delay hash?");
+}
+
+static void reply_list_available_blobs_cmd(void)
+{
+ struct hashmap_iter iter;
+ struct strmap_entry *ent;
+ struct string_list_item *str_item;
+ struct string_list paths = STRING_LIST_INIT_NODUP;
+
+ /* flush */
+ if (packet_read_line(0, NULL))
+ die("bad list_available_blobs end");
+
+ strmap_for_each_entry(&delay, &iter, ent) {
+ struct delay_entry *delay_entry = ent->value;
+ if (!delay_entry->requested)
+ continue;
+ delay_entry->count--;
+ if (!strcmp(ent->key, "invalid-delay.a")) {
+ /* Send Git a pathname that was not delayed earlier */
+ packet_write_fmt(1, "pathname=unfiltered");
+ }
+ if (!strcmp(ent->key, "missing-delay.a")) {
+ /* Do not signal Git that this file is available */
+ } else if (!delay_entry->count) {
+ string_list_append(&paths, ent->key);
+ packet_write_fmt(1, "pathname=%s", ent->key);
+ }
+ }
+
+ /* Print paths in sorted order. */
+ string_list_sort(&paths);
+ for_each_string_list_item(str_item, &paths)
+ fprintf(logfile, " %s", str_item->string);
+ string_list_clear(&paths, 0);
+
+ packet_flush(1);
+
+ fprintf(logfile, " [OK]\n");
+ packet_write_fmt(1, "status=success");
+ packet_flush(1);
+}
+
+static void command_loop(void)
+{
+ for (;;) {
+ char *buf, *output;
+ char *pathname;
+ struct delay_entry *entry;
+ struct strbuf input = STRBUF_INIT;
+ char *command = packet_key_val_read("command");
+
+ if (!command) {
+ fprintf(logfile, "STOP\n");
+ break;
+ }
+ fprintf(logfile, "IN: %s", command);
+
+ if (!strcmp(command, "list_available_blobs")) {
+ reply_list_available_blobs_cmd();
+ free(command);
+ continue;
+ }
+
+ pathname = packet_key_val_read("pathname");
+ if (!pathname)
+ die("unexpected EOF while expecting pathname");
+ fprintf(logfile, " %s", pathname);
+
+ /* Read until flush */
+ while ((buf = packet_read_line(0, NULL))) {
+ if (!strcmp(buf, "can-delay=1")) {
+ entry = strmap_get(&delay, pathname);
+ if (entry && !entry->requested)
+ entry->requested = 1;
+ else if (!entry && always_delay)
+ add_delay_entry(pathname, 1, 1);
+ } else if (starts_with(buf, "ref=") ||
+ starts_with(buf, "treeish=") ||
+ starts_with(buf, "blob=")) {
+ fprintf(logfile, " %s", buf);
+ } else {
+ /*
+ * In general, filters need to be graceful about
+ * new metadata, since it's documented that we
+ * can pass any key-value pairs, but for tests,
+ * let's be a little stricter.
+ */
+ die("Unknown message '%s'", buf);
+ }
+ }
+
+ read_packetized_to_strbuf(0, &input, 0);
+ fprintf(logfile, " %"PRIuMAX" [OK] -- ", (uintmax_t)input.len);
+
+ entry = strmap_get(&delay, pathname);
+ if (entry && entry->output) {
+ output = entry->output;
+ } else if (!strcmp(pathname, "error.r") || !strcmp(pathname, "abort.r")) {
+ output = "";
+ } else if (!strcmp(command, "clean") && has_clean_cap) {
+ output = rot13(input.buf);
+ } else if (!strcmp(command, "smudge") && has_smudge_cap) {
+ output = rot13(input.buf);
+ } else {
+ die("bad command '%s'", command);
+ }
+
+ if (!strcmp(pathname, "error.r")) {
+ fprintf(logfile, "[ERROR]\n");
+ packet_write_fmt(1, "status=error");
+ packet_flush(1);
+ } else if (!strcmp(pathname, "abort.r")) {
+ fprintf(logfile, "[ABORT]\n");
+ packet_write_fmt(1, "status=abort");
+ packet_flush(1);
+ } else if (!strcmp(command, "smudge") &&
+ (entry = strmap_get(&delay, pathname)) &&
+ entry->requested == 1) {
+ fprintf(logfile, "[DELAYED]\n");
+ packet_write_fmt(1, "status=delayed");
+ packet_flush(1);
+ entry->requested = 2;
+ if (entry->output != output) {
+ free(entry->output);
+ entry->output = xstrdup(output);
+ }
+ } else {
+ int i, nr_packets = 0;
+ size_t output_len;
+ const char *p;
+ packet_write_fmt(1, "status=success");
+ packet_flush(1);
+
+ if (skip_prefix(pathname, command, &p) &&
+ !strcmp(p, "-write-fail.r")) {
+ fprintf(logfile, "[WRITE FAIL]\n");
+ die("%s write error", command);
+ }
+
+ output_len = strlen(output);
+ fprintf(logfile, "OUT: %"PRIuMAX" ", (uintmax_t)output_len);
+
+ if (write_packetized_from_buf_no_flush_count(output,
+ output_len, 1, &nr_packets))
+ die("failed to write buffer to stdout");
+ packet_flush(1);
+
+ for (i = 0; i < nr_packets; i++)
+ fprintf(logfile, ".");
+ fprintf(logfile, " [OK]\n");
+
+ packet_flush(1);
+ }
+ free(pathname);
+ strbuf_release(&input);
+ free(command);
+ }
+}
+
+static void packet_initialize(void)
+{
+ char *pkt_buf = packet_read_line(0, NULL);
+
+ if (!pkt_buf || strcmp(pkt_buf, "git-filter-client"))
+ die("bad initialize: '%s'", str_or_null(pkt_buf));
+
+ pkt_buf = packet_read_line(0, NULL);
+ if (!pkt_buf || strcmp(pkt_buf, "version=2"))
+ die("bad version: '%s'", str_or_null(pkt_buf));
+
+ pkt_buf = packet_read_line(0, NULL);
+ if (pkt_buf)
+ die("bad version end: '%s'", pkt_buf);
+
+ packet_write_fmt(1, "git-filter-server");
+ packet_write_fmt(1, "version=2");
+ packet_flush(1);
+}
+
+static const char *rot13_usage[] = {
+ "test-tool rot13-filter [--always-delay] --log=<path> <capabilities>",
+ NULL
+};
+
+int cmd__rot13_filter(int argc, const char **argv)
+{
+ int i, nr_caps;
+ struct strset remote_caps = STRSET_INIT;
+ const char *log_path = NULL;
+
+ struct option options[] = {
+ OPT_BOOL(0, "always-delay", &always_delay,
+ "delay all paths with the can-delay flag"),
+ OPT_STRING(0, "log", &log_path, "path",
+ "path to the debug log file"),
+ OPT_END()
+ };
+ nr_caps = parse_options(argc, argv, NULL, options, rot13_usage,
+ PARSE_OPT_STOP_AT_NON_OPTION);
+
+ if (!log_path || !nr_caps)
+ usage_with_options(rot13_usage, options);
+
+ logfile = fopen(log_path, "a");
+ if (!logfile)
+ die_errno("failed to open log file");
+
+ for (i = 0; i < nr_caps; i++) {
+ if (!strcmp(argv[i], "smudge"))
+ has_smudge_cap = 1;
+ if (!strcmp(argv[i], "clean"))
+ has_clean_cap = 1;
+ }
+
+ add_delay_entry("test-delay10.a", 1, 0);
+ add_delay_entry("test-delay11.a", 1, 0);
+ add_delay_entry("test-delay20.a", 2, 0);
+ add_delay_entry("test-delay10.b", 1, 0);
+ add_delay_entry("missing-delay.a", 1, 0);
+ add_delay_entry("invalid-delay.a", 1, 0);
+
+ fprintf(logfile, "START\n");
+ packet_initialize();
+
+ read_capabilities(&remote_caps);
+ check_and_write_capabilities(&remote_caps, argv, nr_caps);
+ fprintf(logfile, "init handshake complete\n");
+ strset_clear(&remote_caps);
+
+ command_loop();
+
+ if (fclose(logfile))
+ die_errno("error closing logfile");
+ free_delay_entries();
+ return 0;
+}
diff --git a/t/helper/test-run-command.c b/t/helper/test-run-command.c
new file mode 100644
index 0000000..c0ed872
--- /dev/null
+++ b/t/helper/test-run-command.c
@@ -0,0 +1,462 @@
+/*
+ * test-run-command.c: test run command API.
+ *
+ * (C) 2009 Ilari Liusvaara <ilari.liusvaara@elisanet.fi>
+ *
+ * This code is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include "test-tool.h"
+#include "run-command.h"
+#include "strvec.h"
+#include "strbuf.h"
+#include "parse-options.h"
+#include "string-list.h"
+#include "thread-utils.h"
+#include "wildmatch.h"
+
+static int number_callbacks;
+static int parallel_next(struct child_process *cp,
+ struct strbuf *err,
+ void *cb,
+ void **task_cb UNUSED)
+{
+ struct child_process *d = cb;
+ if (number_callbacks >= 4)
+ return 0;
+
+ strvec_pushv(&cp->args, d->args.v);
+ if (err)
+ strbuf_addstr(err, "preloaded output of a child\n");
+ else
+ fprintf(stderr, "preloaded output of a child\n");
+
+ number_callbacks++;
+ return 1;
+}
+
+static int no_job(struct child_process *cp UNUSED,
+ struct strbuf *err,
+ void *cb UNUSED,
+ void **task_cb UNUSED)
+{
+ if (err)
+ strbuf_addstr(err, "no further jobs available\n");
+ else
+ fprintf(stderr, "no further jobs available\n");
+ return 0;
+}
+
+static int task_finished(int result UNUSED,
+ struct strbuf *err,
+ void *pp_cb UNUSED,
+ void *pp_task_cb UNUSED)
+{
+ if (err)
+ strbuf_addstr(err, "asking for a quick stop\n");
+ else
+ fprintf(stderr, "asking for a quick stop\n");
+ return 1;
+}
+
+struct testsuite {
+ struct string_list tests, failed;
+ int next;
+ int quiet, immediate, verbose, verbose_log, trace, write_junit_xml;
+};
+#define TESTSUITE_INIT { \
+ .tests = STRING_LIST_INIT_DUP, \
+ .failed = STRING_LIST_INIT_DUP, \
+}
+
+static int next_test(struct child_process *cp, struct strbuf *err, void *cb,
+ void **task_cb)
+{
+ struct testsuite *suite = cb;
+ const char *test;
+ if (suite->next >= suite->tests.nr)
+ return 0;
+
+ test = suite->tests.items[suite->next++].string;
+ strvec_pushl(&cp->args, "sh", test, NULL);
+ if (suite->quiet)
+ strvec_push(&cp->args, "--quiet");
+ if (suite->immediate)
+ strvec_push(&cp->args, "-i");
+ if (suite->verbose)
+ strvec_push(&cp->args, "-v");
+ if (suite->verbose_log)
+ strvec_push(&cp->args, "-V");
+ if (suite->trace)
+ strvec_push(&cp->args, "-x");
+ if (suite->write_junit_xml)
+ strvec_push(&cp->args, "--write-junit-xml");
+
+ strbuf_addf(err, "Output of '%s':\n", test);
+ *task_cb = (void *)test;
+
+ return 1;
+}
+
+static int test_finished(int result, struct strbuf *err, void *cb,
+ void *task_cb)
+{
+ struct testsuite *suite = cb;
+ const char *name = (const char *)task_cb;
+
+ if (result)
+ string_list_append(&suite->failed, name);
+
+ strbuf_addf(err, "%s: '%s'\n", result ? "FAIL" : "SUCCESS", name);
+
+ return 0;
+}
+
+static int test_failed(struct strbuf *out, void *cb, void *task_cb)
+{
+ struct testsuite *suite = cb;
+ const char *name = (const char *)task_cb;
+
+ string_list_append(&suite->failed, name);
+ strbuf_addf(out, "FAILED TO START: '%s'\n", name);
+
+ return 0;
+}
+
+static const char * const testsuite_usage[] = {
+ "test-run-command testsuite [<options>] [<pattern>...]",
+ NULL
+};
+
+static int testsuite(int argc, const char **argv)
+{
+ struct testsuite suite = TESTSUITE_INIT;
+ int max_jobs = 1, i, ret = 0;
+ DIR *dir;
+ struct dirent *d;
+ struct option options[] = {
+ OPT_BOOL('i', "immediate", &suite.immediate,
+ "stop at first failed test case(s)"),
+ OPT_INTEGER('j', "jobs", &max_jobs, "run <N> jobs in parallel"),
+ OPT_BOOL('q', "quiet", &suite.quiet, "be terse"),
+ OPT_BOOL('v', "verbose", &suite.verbose, "be verbose"),
+ OPT_BOOL('V', "verbose-log", &suite.verbose_log,
+ "be verbose, redirected to a file"),
+ OPT_BOOL('x', "trace", &suite.trace, "trace shell commands"),
+ OPT_BOOL(0, "write-junit-xml", &suite.write_junit_xml,
+ "write JUnit-style XML files"),
+ OPT_END()
+ };
+ struct run_process_parallel_opts opts = {
+ .get_next_task = next_test,
+ .start_failure = test_failed,
+ .task_finished = test_finished,
+ .data = &suite,
+ };
+
+ argc = parse_options(argc, argv, NULL, options,
+ testsuite_usage, PARSE_OPT_STOP_AT_NON_OPTION);
+
+ if (max_jobs <= 0)
+ max_jobs = online_cpus();
+
+ dir = opendir(".");
+ if (!dir)
+ die("Could not open the current directory");
+ while ((d = readdir(dir))) {
+ const char *p = d->d_name;
+
+ if (*p != 't' || !isdigit(p[1]) || !isdigit(p[2]) ||
+ !isdigit(p[3]) || !isdigit(p[4]) || p[5] != '-' ||
+ !ends_with(p, ".sh"))
+ continue;
+
+ /* No pattern: match all */
+ if (!argc) {
+ string_list_append(&suite.tests, p);
+ continue;
+ }
+
+ for (i = 0; i < argc; i++)
+ if (!wildmatch(argv[i], p, 0)) {
+ string_list_append(&suite.tests, p);
+ break;
+ }
+ }
+ closedir(dir);
+
+ if (!suite.tests.nr)
+ die("No tests match!");
+ if (max_jobs > suite.tests.nr)
+ max_jobs = suite.tests.nr;
+
+ fprintf(stderr, "Running %"PRIuMAX" tests (%d at a time)\n",
+ (uintmax_t)suite.tests.nr, max_jobs);
+
+ opts.processes = max_jobs;
+ run_processes_parallel(&opts);
+
+ if (suite.failed.nr > 0) {
+ ret = 1;
+ fprintf(stderr, "%"PRIuMAX" tests failed:\n\n",
+ (uintmax_t)suite.failed.nr);
+ for (i = 0; i < suite.failed.nr; i++)
+ fprintf(stderr, "\t%s\n", suite.failed.items[i].string);
+ }
+
+ string_list_clear(&suite.tests, 0);
+ string_list_clear(&suite.failed, 0);
+
+ return ret;
+}
+
+static uint64_t my_random_next = 1234;
+
+static uint64_t my_random(void)
+{
+ uint64_t res = my_random_next;
+ my_random_next = my_random_next * 1103515245 + 12345;
+ return res;
+}
+
+static int quote_stress_test(int argc, const char **argv)
+{
+ /*
+ * We are running a quote-stress test.
+ * spawn a subprocess that runs quote-stress with a
+ * special option that echoes back the arguments that
+ * were passed in.
+ */
+ char special[] = ".?*\\^_\"'`{}()[]<>@~&+:;$%"; // \t\r\n\a";
+ int i, j, k, trials = 100, skip = 0, msys2 = 0;
+ struct strbuf out = STRBUF_INIT;
+ struct strvec args = STRVEC_INIT;
+ struct option options[] = {
+ OPT_INTEGER('n', "trials", &trials, "number of trials"),
+ OPT_INTEGER('s', "skip", &skip, "skip <n> trials"),
+ OPT_BOOL('m', "msys2", &msys2, "test quoting for MSYS2's sh"),
+ OPT_END()
+ };
+ const char * const usage[] = {
+ "test-tool run-command quote-stress-test <options>",
+ NULL
+ };
+
+ argc = parse_options(argc, argv, NULL, options, usage, 0);
+
+ setenv("MSYS_NO_PATHCONV", "1", 0);
+
+ for (i = 0; i < trials; i++) {
+ struct child_process cp = CHILD_PROCESS_INIT;
+ size_t arg_count, arg_offset;
+ int ret = 0;
+
+ strvec_clear(&args);
+ if (msys2)
+ strvec_pushl(&args, "sh", "-c",
+ "printf %s\\\\0 \"$@\"", "skip", NULL);
+ else
+ strvec_pushl(&args, "test-tool", "run-command",
+ "quote-echo", NULL);
+ arg_offset = args.nr;
+
+ if (argc > 0) {
+ trials = 1;
+ arg_count = argc;
+ for (j = 0; j < arg_count; j++)
+ strvec_push(&args, argv[j]);
+ } else {
+ arg_count = 1 + (my_random() % 5);
+ for (j = 0; j < arg_count; j++) {
+ char buf[20];
+ size_t min_len = 1;
+ size_t arg_len = min_len +
+ (my_random() % (ARRAY_SIZE(buf) - min_len));
+
+ for (k = 0; k < arg_len; k++)
+ buf[k] = special[my_random() %
+ ARRAY_SIZE(special)];
+ buf[arg_len] = '\0';
+
+ strvec_push(&args, buf);
+ }
+ }
+
+ if (i < skip)
+ continue;
+
+ strvec_pushv(&cp.args, args.v);
+ strbuf_reset(&out);
+ if (pipe_command(&cp, NULL, 0, &out, 0, NULL, 0) < 0)
+ return error("Failed to spawn child process");
+
+ for (j = 0, k = 0; j < arg_count; j++) {
+ const char *arg = args.v[j + arg_offset];
+
+ if (strcmp(arg, out.buf + k))
+ ret = error("incorrectly quoted arg: '%s', "
+ "echoed back as '%s'",
+ arg, out.buf + k);
+ k += strlen(out.buf + k) + 1;
+ }
+
+ if (k != out.len)
+ ret = error("got %d bytes, but consumed only %d",
+ (int)out.len, (int)k);
+
+ if (ret) {
+ fprintf(stderr, "Trial #%d failed. Arguments:\n", i);
+ for (j = 0; j < arg_count; j++)
+ fprintf(stderr, "arg #%d: '%s'\n",
+ (int)j, args.v[j + arg_offset]);
+
+ strbuf_release(&out);
+ strvec_clear(&args);
+
+ return ret;
+ }
+
+ if (i && (i % 100) == 0)
+ fprintf(stderr, "Trials completed: %d\n", (int)i);
+ }
+
+ strbuf_release(&out);
+ strvec_clear(&args);
+
+ return 0;
+}
+
+static int quote_echo(int argc, const char **argv)
+{
+ while (argc > 1) {
+ fwrite(argv[1], strlen(argv[1]), 1, stdout);
+ fputc('\0', stdout);
+ argv++;
+ argc--;
+ }
+
+ return 0;
+}
+
+static int inherit_handle(const char *argv0)
+{
+ struct child_process cp = CHILD_PROCESS_INIT;
+ char path[PATH_MAX];
+ int tmp;
+
+ /* First, open an inheritable handle */
+ xsnprintf(path, sizeof(path), "out-XXXXXX");
+ tmp = xmkstemp(path);
+
+ strvec_pushl(&cp.args,
+ "test-tool", argv0, "inherited-handle-child", NULL);
+ cp.in = -1;
+ cp.no_stdout = cp.no_stderr = 1;
+ if (start_command(&cp) < 0)
+ die("Could not start child process");
+
+ /* Then close it, and try to delete it. */
+ close(tmp);
+ if (unlink(path))
+ die("Could not delete '%s'", path);
+
+ if (close(cp.in) < 0 || finish_command(&cp) < 0)
+ die("Child did not finish");
+
+ return 0;
+}
+
+static int inherit_handle_child(void)
+{
+ struct strbuf buf = STRBUF_INIT;
+
+ if (strbuf_read(&buf, 0, 0) < 0)
+ die("Could not read stdin");
+ printf("Received %s\n", buf.buf);
+ strbuf_release(&buf);
+
+ return 0;
+}
+
+int cmd__run_command(int argc, const char **argv)
+{
+ struct child_process proc = CHILD_PROCESS_INIT;
+ int jobs;
+ int ret;
+ struct run_process_parallel_opts opts = {
+ .data = &proc,
+ };
+
+ if (argc > 1 && !strcmp(argv[1], "testsuite"))
+ return testsuite(argc - 1, argv + 1);
+ if (!strcmp(argv[1], "inherited-handle"))
+ return inherit_handle(argv[0]);
+ if (!strcmp(argv[1], "inherited-handle-child"))
+ return inherit_handle_child();
+
+ if (argc >= 2 && !strcmp(argv[1], "quote-stress-test"))
+ return !!quote_stress_test(argc - 1, argv + 1);
+
+ if (argc >= 2 && !strcmp(argv[1], "quote-echo"))
+ return !!quote_echo(argc - 1, argv + 1);
+
+ if (argc < 3)
+ return 1;
+ while (!strcmp(argv[1], "env")) {
+ if (!argv[2])
+ die("env specifier without a value");
+ strvec_push(&proc.env, argv[2]);
+ argv += 2;
+ argc -= 2;
+ }
+ if (argc < 3) {
+ ret = 1;
+ goto cleanup;
+ }
+ strvec_pushv(&proc.args, (const char **)argv + 2);
+
+ if (!strcmp(argv[1], "start-command-ENOENT")) {
+ if (start_command(&proc) < 0 && errno == ENOENT) {
+ ret = 0;
+ goto cleanup;
+ }
+ fprintf(stderr, "FAIL %s\n", argv[1]);
+ return 1;
+ }
+ if (!strcmp(argv[1], "run-command")) {
+ ret = run_command(&proc);
+ goto cleanup;
+ }
+
+ if (!strcmp(argv[1], "--ungroup")) {
+ argv += 1;
+ argc -= 1;
+ opts.ungroup = 1;
+ }
+
+ jobs = atoi(argv[2]);
+ strvec_clear(&proc.args);
+ strvec_pushv(&proc.args, (const char **)argv + 3);
+
+ if (!strcmp(argv[1], "run-command-parallel")) {
+ opts.get_next_task = parallel_next;
+ } else if (!strcmp(argv[1], "run-command-abort")) {
+ opts.get_next_task = parallel_next;
+ opts.task_finished = task_finished;
+ } else if (!strcmp(argv[1], "run-command-no-jobs")) {
+ opts.get_next_task = no_job;
+ opts.task_finished = task_finished;
+ } else {
+ ret = 1;
+ fprintf(stderr, "check usage\n");
+ goto cleanup;
+ }
+ opts.processes = jobs;
+ run_processes_parallel(&opts);
+ ret = 0;
+cleanup:
+ child_process_clear(&proc);
+ return ret;
+}
diff --git a/t/helper/test-scrap-cache-tree.c b/t/helper/test-scrap-cache-tree.c
new file mode 100644
index 0000000..0a816a9
--- /dev/null
+++ b/t/helper/test-scrap-cache-tree.c
@@ -0,0 +1,23 @@
+#define USE_THE_INDEX_VARIABLE
+#include "test-tool.h"
+#include "lockfile.h"
+#include "read-cache-ll.h"
+#include "repository.h"
+#include "setup.h"
+#include "tree.h"
+#include "cache-tree.h"
+
+int cmd__scrap_cache_tree(int ac UNUSED, const char **av UNUSED)
+{
+ struct lock_file index_lock = LOCK_INIT;
+
+ setup_git_directory();
+ repo_hold_locked_index(the_repository, &index_lock, LOCK_DIE_ON_ERROR);
+ if (repo_read_index(the_repository) < 0)
+ die("unable to read index file");
+ cache_tree_free(&the_index.cache_tree);
+ the_index.cache_tree = NULL;
+ if (write_locked_index(&the_index, &index_lock, COMMIT_LOCK))
+ die("unable to write index file");
+ return 0;
+}
diff --git a/t/helper/test-serve-v2.c b/t/helper/test-serve-v2.c
new file mode 100644
index 0000000..054cbcf
--- /dev/null
+++ b/t/helper/test-serve-v2.c
@@ -0,0 +1,36 @@
+#include "test-tool.h"
+#include "gettext.h"
+#include "parse-options.h"
+#include "serve.h"
+#include "setup.h"
+
+static char const * const serve_usage[] = {
+ N_("test-tool serve-v2 [<options>]"),
+ NULL
+};
+
+int cmd__serve_v2(int argc, const char **argv)
+{
+ int stateless_rpc = 0;
+ int advertise_capabilities = 0;
+ struct option options[] = {
+ OPT_BOOL(0, "stateless-rpc", &stateless_rpc,
+ N_("quit after a single request/response exchange")),
+ OPT_BOOL(0, "advertise-capabilities", &advertise_capabilities,
+ N_("exit immediately after advertising capabilities")),
+ OPT_END()
+ };
+ const char *prefix = setup_git_directory();
+
+ /* ignore all unknown cmdline switches for now */
+ argc = parse_options(argc, argv, prefix, options, serve_usage,
+ PARSE_OPT_KEEP_DASHDASH |
+ PARSE_OPT_KEEP_UNKNOWN_OPT);
+
+ if (advertise_capabilities)
+ protocol_v2_advertise_capabilities();
+ else
+ protocol_v2_serve_loop(stateless_rpc);
+
+ return 0;
+}
diff --git a/t/helper/test-sha1.c b/t/helper/test-sha1.c
new file mode 100644
index 0000000..dcb7f6c
--- /dev/null
+++ b/t/helper/test-sha1.c
@@ -0,0 +1,15 @@
+#include "test-tool.h"
+#include "hash-ll.h"
+
+int cmd__sha1(int ac, const char **av)
+{
+ return cmd_hash_impl(ac, av, GIT_HASH_SHA1);
+}
+
+int cmd__sha1_is_sha1dc(int argc UNUSED, const char **argv UNUSED)
+{
+#ifdef platform_SHA_IS_SHA1DC
+ return 0;
+#endif
+ return 1;
+}
diff --git a/t/helper/test-sha1.sh b/t/helper/test-sha1.sh
new file mode 100755
index 0000000..8459488
--- /dev/null
+++ b/t/helper/test-sha1.sh
@@ -0,0 +1,83 @@
+#!/bin/sh
+
+dd if=/dev/zero bs=1048576 count=100 2>/dev/null |
+/usr/bin/time t/helper/test-tool sha1 >/dev/null
+
+while read expect cnt pfx
+do
+ case "$expect" in '#'*) continue ;; esac
+ actual=$(
+ {
+ test -z "$pfx" || echo "$pfx"
+ dd if=/dev/zero bs=1048576 count=$cnt 2>/dev/null |
+ perl -pe 'y/\000/g/'
+ } | ./t/helper/test-tool sha1 $cnt
+ )
+ if test "$expect" = "$actual"
+ then
+ echo "OK: $expect $cnt $pfx"
+ else
+ echo >&2 "OOPS: $cnt"
+ echo >&2 "expect: $expect"
+ echo >&2 "actual: $actual"
+ exit 1
+ fi
+done <<EOF
+da39a3ee5e6b4b0d3255bfef95601890afd80709 0
+3f786850e387550fdab836ed7e6dc881de23001b 0 a
+5277cbb45a15902137d332d97e89cf8136545485 0 ab
+03cfd743661f07975fa2f1220c5194cbaff48451 0 abc
+3330b4373640f9e4604991e73c7e86bfd8da2dc3 0 abcd
+ec11312386ad561674f724b8cca7cf1796e26d1d 0 abcde
+bdc37c074ec4ee6050d68bc133c6b912f36474df 0 abcdef
+69bca99b923859f2dc486b55b87f49689b7358c7 0 abcdefg
+e414af7161c9554089f4106d6f1797ef14a73666 0 abcdefgh
+0707f2970043f9f7c22029482db27733deaec029 0 abcdefghi
+a4dd8aa74a5636728fe52451636e2e17726033aa 1
+9986b45e2f4d7086372533bb6953a8652fa3644a 1 frotz
+23d8d4f788e8526b4877548a32577543cbaaf51f 10
+8cd23f822ab44c7f481b8c92d591f6d1fcad431c 10 frotz
+f3b5604a4e604899c1233edb3bf1cc0ede4d8c32 512
+b095bd837a371593048136e429e9ac4b476e1bb3 512 frotz
+08fa81d6190948de5ccca3966340cc48c10cceac 1200 xyzzy
+e33a291f42c30a159733dd98b8b3e4ff34158ca0 4090 4G
+#a3bf783bc20caa958f6cb24dd140a7b21984838d 9999 nitfol
+EOF
+
+exit
+
+# generating test vectors
+# inputs are number of megabytes followed by some random string to prefix.
+
+while read cnt pfx
+do
+ actual=$(
+ {
+ test -z "$pfx" || echo "$pfx"
+ dd if=/dev/zero bs=1048576 count=$cnt 2>/dev/null |
+ perl -pe 'y/\000/g/'
+ } | sha1sum |
+ sed -e 's/ .*//'
+ )
+ echo "$actual $cnt $pfx"
+done <<EOF
+0
+0 a
+0 ab
+0 abc
+0 abcd
+0 abcde
+0 abcdef
+0 abcdefg
+0 abcdefgh
+0 abcdefghi
+1
+1 frotz
+10
+10 frotz
+512
+512 frotz
+1200 xyzzy
+4090 4G
+9999 nitfol
+EOF
diff --git a/t/helper/test-sha256.c b/t/helper/test-sha256.c
new file mode 100644
index 0000000..08cf149
--- /dev/null
+++ b/t/helper/test-sha256.c
@@ -0,0 +1,7 @@
+#include "test-tool.h"
+#include "hash-ll.h"
+
+int cmd__sha256(int ac, const char **av)
+{
+ return cmd_hash_impl(ac, av, GIT_HASH_SHA256);
+}
diff --git a/t/helper/test-sigchain.c b/t/helper/test-sigchain.c
new file mode 100644
index 0000000..2d5ecf7
--- /dev/null
+++ b/t/helper/test-sigchain.c
@@ -0,0 +1,23 @@
+#include "test-tool.h"
+#include "sigchain.h"
+
+#define X(f) \
+static void f(int sig) { \
+ puts(#f); \
+ fflush(stdout); \
+ sigchain_pop(sig); \
+ raise(sig); \
+}
+X(one)
+X(two)
+X(three)
+#undef X
+
+int cmd__sigchain(int argc UNUSED, const char **argv UNUSED)
+{
+ sigchain_push(SIGTERM, one);
+ sigchain_push(SIGTERM, two);
+ sigchain_push(SIGTERM, three);
+ raise(SIGTERM);
+ return 0;
+}
diff --git a/t/helper/test-simple-ipc.c b/t/helper/test-simple-ipc.c
new file mode 100644
index 0000000..941ae7e
--- /dev/null
+++ b/t/helper/test-simple-ipc.c
@@ -0,0 +1,688 @@
+/*
+ * test-simple-ipc.c: verify that the Inter-Process Communication works.
+ */
+
+#include "test-tool.h"
+#include "gettext.h"
+#include "strbuf.h"
+#include "simple-ipc.h"
+#include "parse-options.h"
+#include "thread-utils.h"
+#include "strvec.h"
+#include "run-command.h"
+#include "trace2.h"
+
+#ifndef SUPPORTS_SIMPLE_IPC
+int cmd__simple_ipc(int argc, const char **argv)
+{
+ die("simple IPC not available on this platform");
+}
+#else
+
+/*
+ * The test daemon defines an "application callback" that supports a
+ * series of commands (see `test_app_cb()`).
+ *
+ * Unknown commands are caught here and we send an error message back
+ * to the client process.
+ */
+static int app__unhandled_command(const char *command,
+ ipc_server_reply_cb *reply_cb,
+ struct ipc_server_reply_data *reply_data)
+{
+ struct strbuf buf = STRBUF_INIT;
+ int ret;
+
+ strbuf_addf(&buf, "unhandled command: %s", command);
+ ret = reply_cb(reply_data, buf.buf, buf.len);
+ strbuf_release(&buf);
+
+ return ret;
+}
+
+/*
+ * Reply with a single very large buffer. This is to ensure that
+ * long response are properly handled -- whether the chunking occurs
+ * in the kernel or in the (probably pkt-line) layer.
+ */
+#define BIG_ROWS (10000)
+static int app__big_command(ipc_server_reply_cb *reply_cb,
+ struct ipc_server_reply_data *reply_data)
+{
+ struct strbuf buf = STRBUF_INIT;
+ int row;
+ int ret;
+
+ for (row = 0; row < BIG_ROWS; row++)
+ strbuf_addf(&buf, "big: %.75d\n", row);
+
+ ret = reply_cb(reply_data, buf.buf, buf.len);
+ strbuf_release(&buf);
+
+ return ret;
+}
+
+/*
+ * Reply with a series of lines. This is to ensure that we can incrementally
+ * compute the response and chunk it to the client.
+ */
+#define CHUNK_ROWS (10000)
+static int app__chunk_command(ipc_server_reply_cb *reply_cb,
+ struct ipc_server_reply_data *reply_data)
+{
+ struct strbuf buf = STRBUF_INIT;
+ int row;
+ int ret;
+
+ for (row = 0; row < CHUNK_ROWS; row++) {
+ strbuf_setlen(&buf, 0);
+ strbuf_addf(&buf, "big: %.75d\n", row);
+ ret = reply_cb(reply_data, buf.buf, buf.len);
+ }
+
+ strbuf_release(&buf);
+
+ return ret;
+}
+
+/*
+ * Slowly reply with a series of lines. This is to model an expensive to
+ * compute chunked response (which might happen if this callback is running
+ * in a thread and is fighting for a lock with other threads).
+ */
+#define SLOW_ROWS (1000)
+#define SLOW_DELAY_MS (10)
+static int app__slow_command(ipc_server_reply_cb *reply_cb,
+ struct ipc_server_reply_data *reply_data)
+{
+ struct strbuf buf = STRBUF_INIT;
+ int row;
+ int ret;
+
+ for (row = 0; row < SLOW_ROWS; row++) {
+ strbuf_setlen(&buf, 0);
+ strbuf_addf(&buf, "big: %.75d\n", row);
+ ret = reply_cb(reply_data, buf.buf, buf.len);
+ sleep_millisec(SLOW_DELAY_MS);
+ }
+
+ strbuf_release(&buf);
+
+ return ret;
+}
+
+/*
+ * The client sent a command followed by a (possibly very) large buffer.
+ */
+static int app__sendbytes_command(const char *received, size_t received_len,
+ ipc_server_reply_cb *reply_cb,
+ struct ipc_server_reply_data *reply_data)
+{
+ struct strbuf buf_resp = STRBUF_INIT;
+ const char *p = "?";
+ int len_ballast = 0;
+ int k;
+ int errs = 0;
+ int ret;
+
+ /*
+ * The test is setup to send:
+ * "sendbytes" SP <n * char>
+ */
+ if (received_len < strlen("sendbytes "))
+ BUG("received_len is short in app__sendbytes_command");
+
+ if (skip_prefix(received, "sendbytes ", &p))
+ len_ballast = strlen(p);
+
+ /*
+ * Verify that the ballast is n copies of a single letter.
+ * And that the multi-threaded IO layer didn't cross the streams.
+ */
+ for (k = 1; k < len_ballast; k++)
+ if (p[k] != p[0])
+ errs++;
+
+ if (errs)
+ strbuf_addf(&buf_resp, "errs:%d\n", errs);
+ else
+ strbuf_addf(&buf_resp, "rcvd:%c%08d\n", p[0], len_ballast);
+
+ ret = reply_cb(reply_data, buf_resp.buf, buf_resp.len);
+
+ strbuf_release(&buf_resp);
+
+ return ret;
+}
+
+/*
+ * An arbitrary fixed address to verify that the application instance
+ * data is handled properly.
+ */
+static int my_app_data = 42;
+
+static ipc_server_application_cb test_app_cb;
+
+/*
+ * This is the "application callback" that sits on top of the
+ * "ipc-server". It completely defines the set of commands supported
+ * by this application.
+ */
+static int test_app_cb(void *application_data,
+ const char *command, size_t command_len,
+ ipc_server_reply_cb *reply_cb,
+ struct ipc_server_reply_data *reply_data)
+{
+ /*
+ * Verify that we received the application-data that we passed
+ * when we started the ipc-server. (We have several layers of
+ * callbacks calling callbacks and it's easy to get things mixed
+ * up (especially when some are "void*").)
+ */
+ if (application_data != (void*)&my_app_data)
+ BUG("application_cb: application_data pointer wrong");
+
+ if (command_len == 4 && !strncmp(command, "quit", 4)) {
+ /*
+ * The client sent a "quit" command. This is an async
+ * request for the server to shutdown.
+ *
+ * We DO NOT send the client a response message
+ * (because we have nothing to say and the other
+ * server threads have not yet stopped).
+ *
+ * Tell the ipc-server layer to start shutting down.
+ * This includes: stop listening for new connections
+ * on the socket/pipe and telling all worker threads
+ * to finish/drain their outgoing responses to other
+ * clients.
+ *
+ * This DOES NOT force an immediate sync shutdown.
+ */
+ return SIMPLE_IPC_QUIT;
+ }
+
+ if (command_len == 4 && !strncmp(command, "ping", 4)) {
+ const char *answer = "pong";
+ return reply_cb(reply_data, answer, strlen(answer));
+ }
+
+ if (command_len == 3 && !strncmp(command, "big", 3))
+ return app__big_command(reply_cb, reply_data);
+
+ if (command_len == 5 && !strncmp(command, "chunk", 5))
+ return app__chunk_command(reply_cb, reply_data);
+
+ if (command_len == 4 && !strncmp(command, "slow", 4))
+ return app__slow_command(reply_cb, reply_data);
+
+ if (command_len >= 10 && starts_with(command, "sendbytes "))
+ return app__sendbytes_command(command, command_len,
+ reply_cb, reply_data);
+
+ return app__unhandled_command(command, reply_cb, reply_data);
+}
+
+struct cl_args
+{
+ const char *subcommand;
+ const char *path;
+ const char *token;
+
+ int nr_threads;
+ int max_wait_sec;
+ int bytecount;
+ int batchsize;
+
+ char bytevalue;
+};
+
+static struct cl_args cl_args = {
+ .subcommand = NULL,
+ .path = "ipc-test",
+ .token = NULL,
+
+ .nr_threads = 5,
+ .max_wait_sec = 60,
+ .bytecount = 1024,
+ .batchsize = 10,
+
+ .bytevalue = 'x',
+};
+
+/*
+ * This process will run as a simple-ipc server and listen for IPC commands
+ * from client processes.
+ */
+static int daemon__run_server(void)
+{
+ int ret;
+
+ struct ipc_server_opts opts = {
+ .nr_threads = cl_args.nr_threads,
+ };
+
+ /*
+ * Synchronously run the ipc-server. We don't need any application
+ * instance data, so pass an arbitrary pointer (that we'll later
+ * verify made the round trip).
+ */
+ ret = ipc_server_run(cl_args.path, &opts, test_app_cb, (void*)&my_app_data);
+ if (ret == -2)
+ error("socket/pipe already in use: '%s'", cl_args.path);
+ else if (ret == -1)
+ error_errno("could not start server on: '%s'", cl_args.path);
+
+ return ret;
+}
+
+static start_bg_wait_cb bg_wait_cb;
+
+static int bg_wait_cb(const struct child_process *cp UNUSED,
+ void *cb_data UNUSED)
+{
+ int s = ipc_get_active_state(cl_args.path);
+
+ switch (s) {
+ case IPC_STATE__LISTENING:
+ /* child is "ready" */
+ return 0;
+
+ case IPC_STATE__NOT_LISTENING:
+ case IPC_STATE__PATH_NOT_FOUND:
+ /* give child more time */
+ return 1;
+
+ default:
+ case IPC_STATE__INVALID_PATH:
+ case IPC_STATE__OTHER_ERROR:
+ /* all the time in world won't help */
+ return -1;
+ }
+}
+
+static int daemon__start_server(void)
+{
+ struct child_process cp = CHILD_PROCESS_INIT;
+ enum start_bg_result sbgr;
+
+ strvec_push(&cp.args, "test-tool");
+ strvec_push(&cp.args, "simple-ipc");
+ strvec_push(&cp.args, "run-daemon");
+ strvec_pushf(&cp.args, "--name=%s", cl_args.path);
+ strvec_pushf(&cp.args, "--threads=%d", cl_args.nr_threads);
+
+ cp.no_stdin = 1;
+ cp.no_stdout = 1;
+ cp.no_stderr = 1;
+
+ sbgr = start_bg_command(&cp, bg_wait_cb, NULL, cl_args.max_wait_sec);
+
+ switch (sbgr) {
+ case SBGR_READY:
+ return 0;
+
+ default:
+ case SBGR_ERROR:
+ case SBGR_CB_ERROR:
+ return error("daemon failed to start");
+
+ case SBGR_TIMEOUT:
+ return error("daemon not online yet");
+
+ case SBGR_DIED:
+ return error("daemon terminated");
+ }
+}
+
+/*
+ * This process will run a quick probe to see if a simple-ipc server
+ * is active on this path.
+ *
+ * Returns 0 if the server is alive.
+ */
+static int client__probe_server(void)
+{
+ enum ipc_active_state s;
+
+ s = ipc_get_active_state(cl_args.path);
+ switch (s) {
+ case IPC_STATE__LISTENING:
+ return 0;
+
+ case IPC_STATE__NOT_LISTENING:
+ return error("no server listening at '%s'", cl_args.path);
+
+ case IPC_STATE__PATH_NOT_FOUND:
+ return error("path not found '%s'", cl_args.path);
+
+ case IPC_STATE__INVALID_PATH:
+ return error("invalid pipe/socket name '%s'", cl_args.path);
+
+ case IPC_STATE__OTHER_ERROR:
+ default:
+ return error("other error for '%s'", cl_args.path);
+ }
+}
+
+/*
+ * Send an IPC command token to an already-running server daemon and
+ * print the response.
+ *
+ * This is a simple 1 word command/token that `test_app_cb()` (in the
+ * daemon process) will understand.
+ */
+static int client__send_ipc(void)
+{
+ const char *command = "(no-command)";
+ struct strbuf buf = STRBUF_INIT;
+ struct ipc_client_connect_options options
+ = IPC_CLIENT_CONNECT_OPTIONS_INIT;
+
+ if (cl_args.token && *cl_args.token)
+ command = cl_args.token;
+
+ options.wait_if_busy = 1;
+ options.wait_if_not_found = 0;
+
+ if (!ipc_client_send_command(cl_args.path, &options,
+ command, strlen(command),
+ &buf)) {
+ if (buf.len) {
+ printf("%s\n", buf.buf);
+ fflush(stdout);
+ }
+ strbuf_release(&buf);
+
+ return 0;
+ }
+
+ return error("failed to send '%s' to '%s'", command, cl_args.path);
+}
+
+/*
+ * Send an IPC command to an already-running server and ask it to
+ * shutdown. "send quit" is an async request and queues a shutdown
+ * event in the server, so we spin and wait here for it to actually
+ * shutdown to make the unit tests a little easier to write.
+ */
+static int client__stop_server(void)
+{
+ int ret;
+ time_t time_limit, now;
+ enum ipc_active_state s;
+
+ time(&time_limit);
+ time_limit += cl_args.max_wait_sec;
+
+ cl_args.token = "quit";
+
+ ret = client__send_ipc();
+ if (ret)
+ return ret;
+
+ for (;;) {
+ sleep_millisec(100);
+
+ s = ipc_get_active_state(cl_args.path);
+
+ if (s != IPC_STATE__LISTENING) {
+ /*
+ * The socket/pipe is gone and/or has stopped
+ * responding. Lets assume that the daemon
+ * process has exited too.
+ */
+ return 0;
+ }
+
+ time(&now);
+ if (now > time_limit)
+ return error("daemon has not shutdown yet");
+ }
+}
+
+/*
+ * Send an IPC command followed by ballast to confirm that a large
+ * message can be sent and that the kernel or pkt-line layers will
+ * properly chunk it and that the daemon receives the entire message.
+ */
+static int do_sendbytes(int bytecount, char byte, const char *path,
+ const struct ipc_client_connect_options *options)
+{
+ struct strbuf buf_send = STRBUF_INIT;
+ struct strbuf buf_resp = STRBUF_INIT;
+
+ strbuf_addstr(&buf_send, "sendbytes ");
+ strbuf_addchars(&buf_send, byte, bytecount);
+
+ if (!ipc_client_send_command(path, options,
+ buf_send.buf, buf_send.len,
+ &buf_resp)) {
+ strbuf_rtrim(&buf_resp);
+ printf("sent:%c%08d %s\n", byte, bytecount, buf_resp.buf);
+ fflush(stdout);
+ strbuf_release(&buf_send);
+ strbuf_release(&buf_resp);
+
+ return 0;
+ }
+
+ return error("client failed to sendbytes(%d, '%c') to '%s'",
+ bytecount, byte, path);
+}
+
+/*
+ * Send an IPC command with ballast to an already-running server daemon.
+ */
+static int client__sendbytes(void)
+{
+ struct ipc_client_connect_options options
+ = IPC_CLIENT_CONNECT_OPTIONS_INIT;
+
+ options.wait_if_busy = 1;
+ options.wait_if_not_found = 0;
+ options.uds_disallow_chdir = 0;
+
+ return do_sendbytes(cl_args.bytecount, cl_args.bytevalue, cl_args.path,
+ &options);
+}
+
+struct multiple_thread_data {
+ pthread_t pthread_id;
+ struct multiple_thread_data *next;
+ const char *path;
+ int bytecount;
+ int batchsize;
+ int sum_errors;
+ int sum_good;
+ char letter;
+};
+
+static void *multiple_thread_proc(void *_multiple_thread_data)
+{
+ struct multiple_thread_data *d = _multiple_thread_data;
+ int k;
+ struct ipc_client_connect_options options
+ = IPC_CLIENT_CONNECT_OPTIONS_INIT;
+
+ options.wait_if_busy = 1;
+ options.wait_if_not_found = 0;
+ /*
+ * A multi-threaded client should not be randomly calling chdir().
+ * The test will pass without this restriction because the test is
+ * not otherwise accessing the filesystem, but it makes us honest.
+ */
+ options.uds_disallow_chdir = 1;
+
+ trace2_thread_start("multiple");
+
+ for (k = 0; k < d->batchsize; k++) {
+ if (do_sendbytes(d->bytecount + k, d->letter, d->path, &options))
+ d->sum_errors++;
+ else
+ d->sum_good++;
+ }
+
+ trace2_thread_exit();
+ return NULL;
+}
+
+/*
+ * Start a client-side thread pool. Each thread sends a series of
+ * IPC requests. Each request is on a new connection to the server.
+ */
+static int client__multiple(void)
+{
+ struct multiple_thread_data *list = NULL;
+ int k;
+ int sum_join_errors = 0;
+ int sum_thread_errors = 0;
+ int sum_good = 0;
+
+ for (k = 0; k < cl_args.nr_threads; k++) {
+ struct multiple_thread_data *d = xcalloc(1, sizeof(*d));
+ d->next = list;
+ d->path = cl_args.path;
+ d->bytecount = cl_args.bytecount + cl_args.batchsize*(k/26);
+ d->batchsize = cl_args.batchsize;
+ d->sum_errors = 0;
+ d->sum_good = 0;
+ d->letter = 'A' + (k % 26);
+
+ if (pthread_create(&d->pthread_id, NULL, multiple_thread_proc, d)) {
+ warning("failed to create thread[%d] skipping remainder", k);
+ free(d);
+ break;
+ }
+
+ list = d;
+ }
+
+ while (list) {
+ struct multiple_thread_data *d = list;
+
+ if (pthread_join(d->pthread_id, NULL))
+ sum_join_errors++;
+
+ sum_thread_errors += d->sum_errors;
+ sum_good += d->sum_good;
+
+ list = d->next;
+ free(d);
+ }
+
+ printf("client (good %d) (join %d), (errors %d)\n",
+ sum_good, sum_join_errors, sum_thread_errors);
+
+ return (sum_join_errors + sum_thread_errors) ? 1 : 0;
+}
+
+int cmd__simple_ipc(int argc, const char **argv)
+{
+ const char * const simple_ipc_usage[] = {
+ N_("test-helper simple-ipc is-active [<name>] [<options>]"),
+ N_("test-helper simple-ipc run-daemon [<name>] [<threads>]"),
+ N_("test-helper simple-ipc start-daemon [<name>] [<threads>] [<max-wait>]"),
+ N_("test-helper simple-ipc stop-daemon [<name>] [<max-wait>]"),
+ N_("test-helper simple-ipc send [<name>] [<token>]"),
+ N_("test-helper simple-ipc sendbytes [<name>] [<bytecount>] [<byte>]"),
+ N_("test-helper simple-ipc multiple [<name>] [<threads>] [<bytecount>] [<batchsize>]"),
+ NULL
+ };
+
+ const char *bytevalue = NULL;
+
+ struct option options[] = {
+#ifndef GIT_WINDOWS_NATIVE
+ OPT_STRING(0, "name", &cl_args.path, N_("name"), N_("name or pathname of unix domain socket")),
+#else
+ OPT_STRING(0, "name", &cl_args.path, N_("name"), N_("named-pipe name")),
+#endif
+ OPT_INTEGER(0, "threads", &cl_args.nr_threads, N_("number of threads in server thread pool")),
+ OPT_INTEGER(0, "max-wait", &cl_args.max_wait_sec, N_("seconds to wait for daemon to start or stop")),
+
+ OPT_INTEGER(0, "bytecount", &cl_args.bytecount, N_("number of bytes")),
+ OPT_INTEGER(0, "batchsize", &cl_args.batchsize, N_("number of requests per thread")),
+
+ OPT_STRING(0, "byte", &bytevalue, N_("byte"), N_("ballast character")),
+ OPT_STRING(0, "token", &cl_args.token, N_("token"), N_("command token to send to the server")),
+
+ OPT_END()
+ };
+
+ if (argc < 2)
+ usage_with_options(simple_ipc_usage, options);
+
+ if (argc == 2 && !strcmp(argv[1], "-h"))
+ usage_with_options(simple_ipc_usage, options);
+
+ if (argc == 2 && !strcmp(argv[1], "SUPPORTS_SIMPLE_IPC"))
+ return 0;
+
+ cl_args.subcommand = argv[1];
+
+ argc--;
+ argv++;
+
+ argc = parse_options(argc, argv, NULL, options, simple_ipc_usage, 0);
+
+ if (cl_args.nr_threads < 1)
+ cl_args.nr_threads = 1;
+ if (cl_args.max_wait_sec < 0)
+ cl_args.max_wait_sec = 0;
+ if (cl_args.bytecount < 1)
+ cl_args.bytecount = 1;
+ if (cl_args.batchsize < 1)
+ cl_args.batchsize = 1;
+
+ if (bytevalue && *bytevalue)
+ cl_args.bytevalue = bytevalue[0];
+
+ /*
+ * Use '!!' on all dispatch functions to map from `error()` style
+ * (returns -1) style to `test_must_fail` style (expects 1). This
+ * makes shell error messages less confusing.
+ */
+
+ if (!strcmp(cl_args.subcommand, "is-active"))
+ return !!client__probe_server();
+
+ if (!strcmp(cl_args.subcommand, "run-daemon"))
+ return !!daemon__run_server();
+
+ if (!strcmp(cl_args.subcommand, "start-daemon"))
+ return !!daemon__start_server();
+
+ /*
+ * Client commands follow. Ensure a server is running before
+ * sending any data. This might be overkill, but then again
+ * this is a test harness.
+ */
+
+ if (!strcmp(cl_args.subcommand, "stop-daemon")) {
+ if (client__probe_server())
+ return 1;
+ return !!client__stop_server();
+ }
+
+ if (!strcmp(cl_args.subcommand, "send")) {
+ if (client__probe_server())
+ return 1;
+ return !!client__send_ipc();
+ }
+
+ if (!strcmp(cl_args.subcommand, "sendbytes")) {
+ if (client__probe_server())
+ return 1;
+ return !!client__sendbytes();
+ }
+
+ if (!strcmp(cl_args.subcommand, "multiple")) {
+ if (client__probe_server())
+ return 1;
+ return !!client__multiple();
+ }
+
+ die("Unhandled subcommand: '%s'", cl_args.subcommand);
+}
+#endif
diff --git a/t/helper/test-strcmp-offset.c b/t/helper/test-strcmp-offset.c
new file mode 100644
index 0000000..d8473cf
--- /dev/null
+++ b/t/helper/test-strcmp-offset.c
@@ -0,0 +1,23 @@
+#include "test-tool.h"
+#include "read-cache-ll.h"
+
+int cmd__strcmp_offset(int argc UNUSED, const char **argv)
+{
+ int result;
+ size_t offset;
+
+ if (!argv[1] || !argv[2])
+ die("usage: %s <string1> <string2>", argv[0]);
+
+ result = strcmp_offset(argv[1], argv[2], &offset);
+
+ /*
+ * Because different CRTs behave differently, only rely on signs
+ * of the result values.
+ */
+ result = (result < 0 ? -1 :
+ result > 0 ? 1 :
+ 0);
+ printf("%d %"PRIuMAX"\n", result, (uintmax_t)offset);
+ return 0;
+}
diff --git a/t/helper/test-string-list.c b/t/helper/test-string-list.c
new file mode 100644
index 0000000..e2aad61
--- /dev/null
+++ b/t/helper/test-string-list.c
@@ -0,0 +1,129 @@
+#include "test-tool.h"
+#include "strbuf.h"
+#include "string-list.h"
+
+/*
+ * Parse an argument into a string list. arg should either be a
+ * ':'-separated list of strings, or "-" to indicate an empty string
+ * list (as opposed to "", which indicates a string list containing a
+ * single empty string). list->strdup_strings must be set.
+ */
+static void parse_string_list(struct string_list *list, const char *arg)
+{
+ if (!strcmp(arg, "-"))
+ return;
+
+ (void)string_list_split(list, arg, ':', -1);
+}
+
+static void write_list(const struct string_list *list)
+{
+ int i;
+ for (i = 0; i < list->nr; i++)
+ printf("[%d]: \"%s\"\n", i, list->items[i].string);
+}
+
+static void write_list_compact(const struct string_list *list)
+{
+ int i;
+ if (!list->nr)
+ printf("-\n");
+ else {
+ printf("%s", list->items[0].string);
+ for (i = 1; i < list->nr; i++)
+ printf(":%s", list->items[i].string);
+ printf("\n");
+ }
+}
+
+static int prefix_cb(struct string_list_item *item, void *cb_data)
+{
+ const char *prefix = (const char *)cb_data;
+ return starts_with(item->string, prefix);
+}
+
+int cmd__string_list(int argc, const char **argv)
+{
+ if (argc == 5 && !strcmp(argv[1], "split")) {
+ struct string_list list = STRING_LIST_INIT_DUP;
+ int i;
+ const char *s = argv[2];
+ int delim = *argv[3];
+ int maxsplit = atoi(argv[4]);
+
+ i = string_list_split(&list, s, delim, maxsplit);
+ printf("%d\n", i);
+ write_list(&list);
+ string_list_clear(&list, 0);
+ return 0;
+ }
+
+ if (argc == 5 && !strcmp(argv[1], "split_in_place")) {
+ struct string_list list = STRING_LIST_INIT_NODUP;
+ int i;
+ char *s = xstrdup(argv[2]);
+ const char *delim = argv[3];
+ int maxsplit = atoi(argv[4]);
+
+ i = string_list_split_in_place(&list, s, delim, maxsplit);
+ printf("%d\n", i);
+ write_list(&list);
+ string_list_clear(&list, 0);
+ free(s);
+ return 0;
+ }
+
+ if (argc == 4 && !strcmp(argv[1], "filter")) {
+ /*
+ * Retain only the items that have the specified prefix.
+ * Arguments: list|- prefix
+ */
+ struct string_list list = STRING_LIST_INIT_DUP;
+ const char *prefix = argv[3];
+
+ parse_string_list(&list, argv[2]);
+ filter_string_list(&list, 0, prefix_cb, (void *)prefix);
+ write_list_compact(&list);
+ string_list_clear(&list, 0);
+ return 0;
+ }
+
+ if (argc == 3 && !strcmp(argv[1], "remove_duplicates")) {
+ struct string_list list = STRING_LIST_INIT_DUP;
+
+ parse_string_list(&list, argv[2]);
+ string_list_remove_duplicates(&list, 0);
+ write_list_compact(&list);
+ string_list_clear(&list, 0);
+ return 0;
+ }
+
+ if (argc == 2 && !strcmp(argv[1], "sort")) {
+ struct string_list list = STRING_LIST_INIT_NODUP;
+ struct strbuf sb = STRBUF_INIT;
+ struct string_list_item *item;
+
+ strbuf_read(&sb, 0, 0);
+
+ /*
+ * Split by newline, but don't create a string_list item
+ * for the empty string after the last separator.
+ */
+ if (sb.len && sb.buf[sb.len - 1] == '\n')
+ strbuf_setlen(&sb, sb.len - 1);
+ string_list_split_in_place(&list, sb.buf, "\n", -1);
+
+ string_list_sort(&list);
+
+ for_each_string_list_item(item, &list)
+ puts(item->string);
+
+ string_list_clear(&list, 0);
+ strbuf_release(&sb);
+ return 0;
+ }
+
+ fprintf(stderr, "%s: unknown function name: %s\n", argv[0],
+ argv[1] ? argv[1] : "(there was none)");
+ return 1;
+}
diff --git a/t/helper/test-submodule-config.c b/t/helper/test-submodule-config.c
new file mode 100644
index 0000000..9df2f03
--- /dev/null
+++ b/t/helper/test-submodule-config.c
@@ -0,0 +1,69 @@
+#include "test-tool.h"
+#include "config.h"
+#include "hash.h"
+#include "object-name.h"
+#include "repository.h"
+#include "setup.h"
+#include "submodule-config.h"
+#include "submodule.h"
+
+static void die_usage(int argc UNUSED, const char **argv, const char *msg)
+{
+ fprintf(stderr, "%s\n", msg);
+ fprintf(stderr, "Usage: %s [<commit> <submodulepath>] ...\n", argv[0]);
+ exit(1);
+}
+
+int cmd__submodule_config(int argc, const char **argv)
+{
+ const char **arg = argv;
+ int my_argc = argc;
+ int lookup_name = 0;
+
+ arg++;
+ my_argc--;
+ while (arg[0] && starts_with(arg[0], "--")) {
+ if (!strcmp(arg[0], "--name"))
+ lookup_name = 1;
+ arg++;
+ my_argc--;
+ }
+
+ if (my_argc % 2 != 0)
+ die_usage(argc, argv, "Wrong number of arguments.");
+
+ setup_git_directory();
+
+ while (*arg) {
+ struct object_id commit_oid;
+ const struct submodule *submodule;
+ const char *commit;
+ const char *path_or_name;
+
+ commit = arg[0];
+ path_or_name = arg[1];
+
+ if (commit[0] == '\0')
+ oidclr(&commit_oid);
+ else if (repo_get_oid(the_repository, commit, &commit_oid) < 0)
+ die_usage(argc, argv, "Commit not found.");
+
+ if (lookup_name) {
+ submodule = submodule_from_name(the_repository,
+ &commit_oid, path_or_name);
+ } else
+ submodule = submodule_from_path(the_repository,
+ &commit_oid, path_or_name);
+ if (!submodule)
+ die_usage(argc, argv, "Submodule not found.");
+
+ printf("Submodule name: '%s' for path '%s'\n", submodule->name,
+ submodule->path);
+
+ arg += 2;
+ }
+
+ submodule_free(the_repository);
+
+ return 0;
+}
diff --git a/t/helper/test-submodule-nested-repo-config.c b/t/helper/test-submodule-nested-repo-config.c
new file mode 100644
index 0000000..ecd40de
--- /dev/null
+++ b/t/helper/test-submodule-nested-repo-config.c
@@ -0,0 +1,32 @@
+#include "test-tool.h"
+#include "repository.h"
+#include "setup.h"
+#include "submodule-config.h"
+
+static void die_usage(const char **argv, const char *msg)
+{
+ fprintf(stderr, "%s\n", msg);
+ fprintf(stderr, "Usage: %s <submodulepath> <config name>\n", argv[0]);
+ exit(1);
+}
+
+int cmd__submodule_nested_repo_config(int argc, const char **argv)
+{
+ struct repository subrepo;
+
+ if (argc < 3)
+ die_usage(argv, "Wrong number of arguments.");
+
+ setup_git_directory();
+
+ if (repo_submodule_init(&subrepo, the_repository, argv[1], null_oid())) {
+ die_usage(argv, "Submodule not found.");
+ }
+
+ /* Read the config of _child_ submodules. */
+ print_config_from_gitmodules(&subrepo, argv[2]);
+
+ submodule_free(the_repository);
+
+ return 0;
+}
diff --git a/t/helper/test-submodule.c b/t/helper/test-submodule.c
new file mode 100644
index 0000000..356e0a2
--- /dev/null
+++ b/t/helper/test-submodule.c
@@ -0,0 +1,225 @@
+#include "test-tool.h"
+#include "test-tool-utils.h"
+#include "parse-options.h"
+#include "remote.h"
+#include "repository.h"
+#include "setup.h"
+#include "submodule-config.h"
+#include "submodule.h"
+
+#define TEST_TOOL_CHECK_NAME_USAGE \
+ "test-tool submodule check-name <name>"
+static const char *submodule_check_name_usage[] = {
+ TEST_TOOL_CHECK_NAME_USAGE,
+ NULL
+};
+
+#define TEST_TOOL_IS_ACTIVE_USAGE \
+ "test-tool submodule is-active <name>"
+static const char *submodule_is_active_usage[] = {
+ TEST_TOOL_IS_ACTIVE_USAGE,
+ NULL
+};
+
+#define TEST_TOOL_RESOLVE_RELATIVE_URL_USAGE \
+ "test-tool submodule resolve-relative-url <up_path> <remoteurl> <url>"
+static const char *submodule_resolve_relative_url_usage[] = {
+ TEST_TOOL_RESOLVE_RELATIVE_URL_USAGE,
+ NULL,
+};
+
+static const char *submodule_usage[] = {
+ TEST_TOOL_CHECK_NAME_USAGE,
+ TEST_TOOL_IS_ACTIVE_USAGE,
+ TEST_TOOL_RESOLVE_RELATIVE_URL_USAGE,
+ NULL
+};
+
+/*
+ * Exit non-zero if any of the submodule names given on the command line is
+ * invalid. If no names are given, filter stdin to print only valid names
+ * (which is primarily intended for testing).
+ */
+static int check_name(int argc, const char **argv)
+{
+ if (argc > 1) {
+ while (*++argv) {
+ if (check_submodule_name(*argv) < 0)
+ return 1;
+ }
+ } else {
+ struct strbuf buf = STRBUF_INIT;
+ while (strbuf_getline(&buf, stdin) != EOF) {
+ if (!check_submodule_name(buf.buf))
+ printf("%s\n", buf.buf);
+ }
+ strbuf_release(&buf);
+ }
+ return 0;
+}
+
+static int cmd__submodule_check_name(int argc, const char **argv)
+{
+ struct option options[] = {
+ OPT_END()
+ };
+ argc = parse_options(argc, argv, "test-tools", options,
+ submodule_check_name_usage, 0);
+ if (argc)
+ usage_with_options(submodule_check_name_usage, options);
+
+ return check_name(argc, argv);
+}
+
+static int cmd__submodule_is_active(int argc, const char **argv)
+{
+ struct option options[] = {
+ OPT_END()
+ };
+ argc = parse_options(argc, argv, "test-tools", options,
+ submodule_is_active_usage, 0);
+ if (argc != 1)
+ usage_with_options(submodule_is_active_usage, options);
+
+ setup_git_directory();
+
+ return !is_submodule_active(the_repository, argv[0]);
+}
+
+static int cmd__submodule_resolve_relative_url(int argc, const char **argv)
+{
+ char *remoteurl, *res;
+ const char *up_path, *url;
+ struct option options[] = {
+ OPT_END()
+ };
+ argc = parse_options(argc, argv, "test-tools", options,
+ submodule_resolve_relative_url_usage, 0);
+ if (argc != 3)
+ usage_with_options(submodule_resolve_relative_url_usage, options);
+
+ up_path = argv[0];
+ remoteurl = xstrdup(argv[1]);
+ url = argv[2];
+
+ if (!strcmp(up_path, "(null)"))
+ up_path = NULL;
+
+ res = relative_url(remoteurl, url, up_path);
+ puts(res);
+ free(res);
+ free(remoteurl);
+ return 0;
+}
+
+static int cmd__submodule_config_list(int argc, const char **argv)
+{
+ struct option options[] = {
+ OPT_END()
+ };
+ const char *const usage[] = {
+ "test-tool submodule config-list <key>",
+ NULL
+ };
+ argc = parse_options(argc, argv, "test-tools", options, usage,
+ PARSE_OPT_KEEP_ARGV0);
+
+ setup_git_directory();
+
+ if (argc == 2)
+ return print_config_from_gitmodules(the_repository, argv[1]);
+ usage_with_options(usage, options);
+}
+
+static int cmd__submodule_config_set(int argc, const char **argv)
+{
+ struct option options[] = {
+ OPT_END()
+ };
+ const char *const usage[] = {
+ "test-tool submodule config-set <key> <value>",
+ NULL
+ };
+ argc = parse_options(argc, argv, "test-tools", options, usage,
+ PARSE_OPT_KEEP_ARGV0);
+
+ setup_git_directory();
+
+ /* Equivalent to ACTION_SET in builtin/config.c */
+ if (argc == 3) {
+ if (!is_writing_gitmodules_ok())
+ die("please make sure that the .gitmodules file is in the working tree");
+
+ return config_set_in_gitmodules_file_gently(argv[1], argv[2]);
+ }
+ usage_with_options(usage, options);
+}
+
+static int cmd__submodule_config_unset(int argc, const char **argv)
+{
+ struct option options[] = {
+ OPT_END()
+ };
+ const char *const usage[] = {
+ "test-tool submodule config-unset <key>",
+ NULL
+ };
+
+ setup_git_directory();
+
+ if (argc == 2) {
+ if (!is_writing_gitmodules_ok())
+ die("please make sure that the .gitmodules file is in the working tree");
+ return config_set_in_gitmodules_file_gently(argv[1], NULL);
+ }
+ usage_with_options(usage, options);
+}
+
+static int cmd__submodule_config_writeable(int argc, const char **argv UNUSED)
+{
+ struct option options[] = {
+ OPT_END()
+ };
+ const char *const usage[] = {
+ "test-tool submodule config-writeable",
+ NULL
+ };
+ setup_git_directory();
+
+ if (argc == 1)
+ return is_writing_gitmodules_ok() ? 0 : -1;
+
+ usage_with_options(usage, options);
+}
+
+static struct test_cmd cmds[] = {
+ { "check-name", cmd__submodule_check_name },
+ { "is-active", cmd__submodule_is_active },
+ { "resolve-relative-url", cmd__submodule_resolve_relative_url},
+ { "config-list", cmd__submodule_config_list },
+ { "config-set", cmd__submodule_config_set },
+ { "config-unset", cmd__submodule_config_unset },
+ { "config-writeable", cmd__submodule_config_writeable },
+};
+
+int cmd__submodule(int argc, const char **argv)
+{
+ struct option options[] = {
+ OPT_END()
+ };
+ size_t i;
+
+ argc = parse_options(argc, argv, "test-tools", options, submodule_usage,
+ PARSE_OPT_STOP_AT_NON_OPTION);
+ if (argc < 1)
+ usage_with_options(submodule_usage, options);
+
+ for (i = 0; i < ARRAY_SIZE(cmds); i++)
+ if (!strcmp(cmds[i].name, argv[0]))
+ return cmds[i].fn(argc, argv);
+
+ usage_msg_optf("unknown subcommand '%s'", submodule_usage, options,
+ argv[0]);
+
+ return 0;
+}
diff --git a/t/helper/test-subprocess.c b/t/helper/test-subprocess.c
new file mode 100644
index 0000000..c344f16
--- /dev/null
+++ b/t/helper/test-subprocess.c
@@ -0,0 +1,20 @@
+#include "test-tool.h"
+#include "run-command.h"
+#include "setup.h"
+
+int cmd__subprocess(int argc, const char **argv)
+{
+ struct child_process cp = CHILD_PROCESS_INIT;
+ int nogit = 0;
+
+ setup_git_directory_gently(&nogit);
+ if (nogit)
+ die("No git repo found");
+ if (argc > 1 && !strcmp(argv[1], "--setup-work-tree")) {
+ setup_work_tree();
+ argv++;
+ }
+ cp.git_cmd = 1;
+ strvec_pushv(&cp.args, (const char **)argv + 1);
+ return run_command(&cp);
+}
diff --git a/t/helper/test-tool-utils.h b/t/helper/test-tool-utils.h
new file mode 100644
index 0000000..6a0e5e0
--- /dev/null
+++ b/t/helper/test-tool-utils.h
@@ -0,0 +1,9 @@
+#ifndef TEST_TOOL_UTILS_H
+#define TEST_TOOL_UTILS_H
+
+struct test_cmd {
+ const char *name;
+ int (*fn)(int argc, const char **argv);
+};
+
+#endif
diff --git a/t/helper/test-tool.c b/t/helper/test-tool.c
new file mode 100644
index 0000000..876cd2d
--- /dev/null
+++ b/t/helper/test-tool.c
@@ -0,0 +1,143 @@
+#include "git-compat-util.h"
+#include "test-tool.h"
+#include "test-tool-utils.h"
+#include "trace2.h"
+#include "parse-options.h"
+
+static const char * const test_tool_usage[] = {
+ "test-tool [-C <directory>] <command [<arguments>...]]",
+ NULL
+};
+
+static struct test_cmd cmds[] = {
+ { "advise", cmd__advise_if_enabled },
+ { "bitmap", cmd__bitmap },
+ { "bloom", cmd__bloom },
+ { "bundle-uri", cmd__bundle_uri },
+ { "cache-tree", cmd__cache_tree },
+ { "chmtime", cmd__chmtime },
+ { "config", cmd__config },
+ { "crontab", cmd__crontab },
+ { "csprng", cmd__csprng },
+ { "ctype", cmd__ctype },
+ { "date", cmd__date },
+ { "delta", cmd__delta },
+ { "dir-iterator", cmd__dir_iterator },
+ { "drop-caches", cmd__drop_caches },
+ { "dump-cache-tree", cmd__dump_cache_tree },
+ { "dump-fsmonitor", cmd__dump_fsmonitor },
+ { "dump-split-index", cmd__dump_split_index },
+ { "dump-untracked-cache", cmd__dump_untracked_cache },
+ { "env-helper", cmd__env_helper },
+ { "example-decorate", cmd__example_decorate },
+ { "fast-rebase", cmd__fast_rebase },
+ { "find-pack", cmd__find_pack },
+ { "fsmonitor-client", cmd__fsmonitor_client },
+ { "genrandom", cmd__genrandom },
+ { "genzeros", cmd__genzeros },
+ { "getcwd", cmd__getcwd },
+ { "hashmap", cmd__hashmap },
+ { "hash-speed", cmd__hash_speed },
+ { "hexdump", cmd__hexdump },
+ { "json-writer", cmd__json_writer },
+ { "lazy-init-name-hash", cmd__lazy_init_name_hash },
+ { "match-trees", cmd__match_trees },
+ { "mergesort", cmd__mergesort },
+ { "mktemp", cmd__mktemp },
+ { "oid-array", cmd__oid_array },
+ { "oidmap", cmd__oidmap },
+ { "oidtree", cmd__oidtree },
+ { "online-cpus", cmd__online_cpus },
+ { "pack-mtimes", cmd__pack_mtimes },
+ { "parse-options", cmd__parse_options },
+ { "parse-options-flags", cmd__parse_options_flags },
+ { "parse-pathspec-file", cmd__parse_pathspec_file },
+ { "parse-subcommand", cmd__parse_subcommand },
+ { "partial-clone", cmd__partial_clone },
+ { "path-utils", cmd__path_utils },
+ { "pcre2-config", cmd__pcre2_config },
+ { "pkt-line", cmd__pkt_line },
+ { "prio-queue", cmd__prio_queue },
+ { "proc-receive", cmd__proc_receive },
+ { "progress", cmd__progress },
+ { "reach", cmd__reach },
+ { "read-cache", cmd__read_cache },
+ { "read-graph", cmd__read_graph },
+ { "read-midx", cmd__read_midx },
+ { "ref-store", cmd__ref_store },
+ { "reftable", cmd__reftable },
+ { "rot13-filter", cmd__rot13_filter },
+ { "dump-reftable", cmd__dump_reftable },
+ { "regex", cmd__regex },
+ { "repository", cmd__repository },
+ { "revision-walking", cmd__revision_walking },
+ { "run-command", cmd__run_command },
+ { "scrap-cache-tree", cmd__scrap_cache_tree },
+ { "serve-v2", cmd__serve_v2 },
+ { "sha1", cmd__sha1 },
+ { "sha1-is-sha1dc", cmd__sha1_is_sha1dc },
+ { "sha256", cmd__sha256 },
+ { "sigchain", cmd__sigchain },
+ { "simple-ipc", cmd__simple_ipc },
+ { "strcmp-offset", cmd__strcmp_offset },
+ { "string-list", cmd__string_list },
+ { "submodule", cmd__submodule },
+ { "submodule-config", cmd__submodule_config },
+ { "submodule-nested-repo-config", cmd__submodule_nested_repo_config },
+ { "subprocess", cmd__subprocess },
+ { "trace2", cmd__trace2 },
+ { "truncate", cmd__truncate },
+ { "userdiff", cmd__userdiff },
+ { "urlmatch-normalization", cmd__urlmatch_normalization },
+ { "xml-encode", cmd__xml_encode },
+ { "wildmatch", cmd__wildmatch },
+#ifdef GIT_WINDOWS_NATIVE
+ { "windows-named-pipe", cmd__windows_named_pipe },
+#endif
+ { "write-cache", cmd__write_cache },
+};
+
+static NORETURN void die_usage(void)
+{
+ size_t i;
+
+ fprintf(stderr, "usage: test-tool <toolname> [args]\n");
+ for (i = 0; i < ARRAY_SIZE(cmds); i++)
+ fprintf(stderr, " %s\n", cmds[i].name);
+ exit(128);
+}
+
+int cmd_main(int argc, const char **argv)
+{
+ int i;
+ const char *working_directory = NULL;
+ struct option options[] = {
+ OPT_STRING('C', NULL, &working_directory, "directory",
+ "change the working directory"),
+ OPT_END()
+ };
+
+ BUG_exit_code = 99;
+ argc = parse_options(argc, argv, NULL, options, test_tool_usage,
+ PARSE_OPT_STOP_AT_NON_OPTION |
+ PARSE_OPT_KEEP_ARGV0);
+
+ if (argc < 2)
+ die_usage();
+
+ if (working_directory && chdir(working_directory) < 0)
+ die("Could not cd to '%s'", working_directory);
+
+ for (i = 0; i < ARRAY_SIZE(cmds); i++) {
+ if (!strcmp(cmds[i].name, argv[1])) {
+ argv++;
+ argc--;
+ trace2_cmd_name(cmds[i].name);
+ trace2_cmd_list_config();
+ trace2_cmd_list_env_vars();
+ return cmds[i].fn(argc, argv);
+ }
+ }
+ error("there is no tool named '%s'", argv[1]);
+ die_usage();
+}
diff --git a/t/helper/test-tool.h b/t/helper/test-tool.h
new file mode 100644
index 0000000..70dd4eb
--- /dev/null
+++ b/t/helper/test-tool.h
@@ -0,0 +1,94 @@
+#ifndef TEST_TOOL_H
+#define TEST_TOOL_H
+
+#include "git-compat-util.h"
+
+int cmd__advise_if_enabled(int argc, const char **argv);
+int cmd__bitmap(int argc, const char **argv);
+int cmd__bloom(int argc, const char **argv);
+int cmd__bundle_uri(int argc, const char **argv);
+int cmd__cache_tree(int argc, const char **argv);
+int cmd__chmtime(int argc, const char **argv);
+int cmd__config(int argc, const char **argv);
+int cmd__crontab(int argc, const char **argv);
+int cmd__csprng(int argc, const char **argv);
+int cmd__ctype(int argc, const char **argv);
+int cmd__date(int argc, const char **argv);
+int cmd__delta(int argc, const char **argv);
+int cmd__dir_iterator(int argc, const char **argv);
+int cmd__drop_caches(int argc, const char **argv);
+int cmd__dump_cache_tree(int argc, const char **argv);
+int cmd__dump_fsmonitor(int argc, const char **argv);
+int cmd__dump_split_index(int argc, const char **argv);
+int cmd__dump_untracked_cache(int argc, const char **argv);
+int cmd__dump_reftable(int argc, const char **argv);
+int cmd__env_helper(int argc, const char **argv);
+int cmd__example_decorate(int argc, const char **argv);
+int cmd__fast_rebase(int argc, const char **argv);
+int cmd__find_pack(int argc, const char **argv);
+int cmd__fsmonitor_client(int argc, const char **argv);
+int cmd__genrandom(int argc, const char **argv);
+int cmd__genzeros(int argc, const char **argv);
+int cmd__getcwd(int argc, const char **argv);
+int cmd__hashmap(int argc, const char **argv);
+int cmd__hash_speed(int argc, const char **argv);
+int cmd__hexdump(int argc, const char **argv);
+int cmd__json_writer(int argc, const char **argv);
+int cmd__lazy_init_name_hash(int argc, const char **argv);
+int cmd__match_trees(int argc, const char **argv);
+int cmd__mergesort(int argc, const char **argv);
+int cmd__mktemp(int argc, const char **argv);
+int cmd__oidmap(int argc, const char **argv);
+int cmd__oidtree(int argc, const char **argv);
+int cmd__online_cpus(int argc, const char **argv);
+int cmd__pack_mtimes(int argc, const char **argv);
+int cmd__parse_options(int argc, const char **argv);
+int cmd__parse_options_flags(int argc, const char **argv);
+int cmd__parse_pathspec_file(int argc, const char** argv);
+int cmd__parse_subcommand(int argc, const char **argv);
+int cmd__partial_clone(int argc, const char **argv);
+int cmd__path_utils(int argc, const char **argv);
+int cmd__pcre2_config(int argc, const char **argv);
+int cmd__pkt_line(int argc, const char **argv);
+int cmd__prio_queue(int argc, const char **argv);
+int cmd__proc_receive(int argc, const char **argv);
+int cmd__progress(int argc, const char **argv);
+int cmd__reach(int argc, const char **argv);
+int cmd__read_cache(int argc, const char **argv);
+int cmd__read_graph(int argc, const char **argv);
+int cmd__read_midx(int argc, const char **argv);
+int cmd__ref_store(int argc, const char **argv);
+int cmd__rot13_filter(int argc, const char **argv);
+int cmd__reftable(int argc, const char **argv);
+int cmd__regex(int argc, const char **argv);
+int cmd__repository(int argc, const char **argv);
+int cmd__revision_walking(int argc, const char **argv);
+int cmd__run_command(int argc, const char **argv);
+int cmd__scrap_cache_tree(int argc, const char **argv);
+int cmd__serve_v2(int argc, const char **argv);
+int cmd__sha1(int argc, const char **argv);
+int cmd__sha1_is_sha1dc(int argc, const char **argv);
+int cmd__oid_array(int argc, const char **argv);
+int cmd__sha256(int argc, const char **argv);
+int cmd__sigchain(int argc, const char **argv);
+int cmd__simple_ipc(int argc, const char **argv);
+int cmd__strcmp_offset(int argc, const char **argv);
+int cmd__string_list(int argc, const char **argv);
+int cmd__submodule(int argc, const char **argv);
+int cmd__submodule_config(int argc, const char **argv);
+int cmd__submodule_nested_repo_config(int argc, const char **argv);
+int cmd__subprocess(int argc, const char **argv);
+int cmd__trace2(int argc, const char **argv);
+int cmd__truncate(int argc, const char **argv);
+int cmd__userdiff(int argc, const char **argv);
+int cmd__urlmatch_normalization(int argc, const char **argv);
+int cmd__xml_encode(int argc, const char **argv);
+int cmd__wildmatch(int argc, const char **argv);
+#ifdef GIT_WINDOWS_NATIVE
+int cmd__windows_named_pipe(int argc, const char **argv);
+#endif
+int cmd__write_cache(int argc, const char **argv);
+
+int cmd_hash_impl(int ac, const char **av, int algo);
+
+#endif
diff --git a/t/helper/test-trace2.c b/t/helper/test-trace2.c
new file mode 100644
index 0000000..d5ca004
--- /dev/null
+++ b/t/helper/test-trace2.c
@@ -0,0 +1,498 @@
+#include "test-tool.h"
+#include "strvec.h"
+#include "run-command.h"
+#include "exec-cmd.h"
+#include "config.h"
+#include "repository.h"
+#include "trace2.h"
+
+typedef int(fn_unit_test)(int argc, const char **argv);
+
+struct unit_test {
+ fn_unit_test *ut_fn;
+ const char *ut_name;
+ const char *ut_usage;
+};
+
+#define MyOk 0
+#define MyError 1
+
+static int get_i(int *p_value, const char *data)
+{
+ char *endptr;
+
+ if (!data || !*data)
+ return MyError;
+
+ *p_value = strtol(data, &endptr, 10);
+ if (*endptr || errno == ERANGE)
+ return MyError;
+
+ return MyOk;
+}
+
+/*
+ * Cause process to exit with the requested value via "return".
+ *
+ * Rely on test-tool.c:cmd_main() to call trace2_cmd_exit()
+ * with our result.
+ *
+ * Test harness can confirm:
+ * [] the process-exit value.
+ * [] the "code" field in the "exit" trace2 event.
+ * [] the "code" field in the "atexit" trace2 event.
+ * [] the "name" field in the "cmd_name" trace2 event.
+ * [] "def_param" events for all of the "interesting" pre-defined
+ * config settings.
+ */
+static int ut_001return(int argc UNUSED, const char **argv)
+{
+ int rc;
+
+ if (get_i(&rc, argv[0]))
+ die("expect <exit_code>");
+
+ return rc;
+}
+
+/*
+ * Cause the process to exit with the requested value via "exit()".
+ *
+ * Test harness can confirm:
+ * [] the "code" field in the "exit" trace2 event.
+ * [] the "code" field in the "atexit" trace2 event.
+ * [] the "name" field in the "cmd_name" trace2 event.
+ * [] "def_param" events for all of the "interesting" pre-defined
+ * config settings.
+ */
+static int ut_002exit(int argc UNUSED, const char **argv)
+{
+ int rc;
+
+ if (get_i(&rc, argv[0]))
+ die("expect <exit_code>");
+
+ exit(rc);
+}
+
+/*
+ * Send an "error" event with each value in argv. Normally, git only issues
+ * a single "error" event immediately before issuing an "exit" event (such
+ * as in die() or BUG()), but multiple "error" events are allowed.
+ *
+ * Test harness can confirm:
+ * [] a trace2 "error" event for each value in argv.
+ * [] the "name" field in the "cmd_name" trace2 event.
+ * [] (optional) the file:line in the "exit" event refers to this function.
+ */
+static int ut_003error(int argc, const char **argv)
+{
+ int k;
+
+ if (!argv[0] || !*argv[0])
+ die("expect <error_message>");
+
+ for (k = 0; k < argc; k++)
+ error("%s", argv[k]);
+
+ return 0;
+}
+
+/*
+ * Run a child process and wait for it to finish and exit with its return code.
+ * test-tool trace2 004child [<child-command-line>]
+ *
+ * For example:
+ * test-tool trace2 004child git version
+ * test-tool trace2 004child test-tool trace2 001return 0
+ * test-tool trace2 004child test-tool trace2 004child test-tool trace2 004child
+ * test-tool trace2 004child git -c alias.xyz=version xyz
+ *
+ * Test harness can confirm:
+ * [] the "name" field in the "cmd_name" trace2 event.
+ * [] that the outer process has a single component SID (or depth "d0" in
+ * the PERF stream).
+ * [] that "child_start" and "child_exit" events are generated for the child.
+ * [] if the child process is an instrumented executable:
+ * [] that "version", "start", ..., "exit", and "atexit" events are
+ * generated by the child process.
+ * [] that the child process events have a multiple component SID (or
+ * depth "dN+1" in the PERF stream).
+ * [] that the child exit code is propagated to the parent process "exit"
+ * and "atexit" events..
+ * [] (optional) that the "t_abs" field in the child process "atexit" event
+ * is less than the "t_rel" field in the "child_exit" event of the parent
+ * process.
+ * [] if the child process is like the alias example above,
+ * [] (optional) the child process attempts to run "git-xyx" as a dashed
+ * command.
+ * [] the child process emits an "alias" event with "xyz" => "version"
+ * [] the child process runs "git version" as a child process.
+ * [] the child process has a 3 component SID (or depth "d2" in the PERF
+ * stream).
+ */
+static int ut_004child(int argc, const char **argv)
+{
+ struct child_process cmd = CHILD_PROCESS_INIT;
+ int result;
+
+ /*
+ * Allow empty <child_command_line> so we can do arbitrarily deep
+ * command nesting and let the last one be null.
+ */
+ if (!argc)
+ return 0;
+
+ strvec_pushv(&cmd.args, argv);
+ result = run_command(&cmd);
+ exit(result);
+}
+
+/*
+ * Exec a git command. This may either create a child process (Windows)
+ * or replace the existing process.
+ * test-tool trace2 005exec <git_command_args>
+ *
+ * For example:
+ * test-tool trace2 005exec version
+ *
+ * Test harness can confirm (on Windows):
+ * [] the "name" field in the "cmd_name" trace2 event.
+ * [] that the outer process has a single component SID (or depth "d0" in
+ * the PERF stream).
+ * [] that "exec" and "exec_result" events are generated for the child
+ * process (since the Windows compatibility layer fakes an exec() with
+ * a CreateProcess(), WaitForSingleObject(), and exit()).
+ * [] that the child process has multiple component SID (or depth "dN+1"
+ * in the PERF stream).
+ *
+ * Test harness can confirm (on platforms with a real exec() function):
+ * [] TODO talk about process replacement and how it affects SID.
+ */
+static int ut_005exec(int argc, const char **argv)
+{
+ int result;
+
+ if (!argc)
+ return 0;
+
+ result = execv_git_cmd(argv);
+ return result;
+}
+
+static int ut_006data(int argc, const char **argv)
+{
+ const char *usage_error =
+ "expect <cat0> <k0> <v0> [<cat1> <k1> <v1> [...]]";
+
+ if (argc % 3 != 0)
+ die("%s", usage_error);
+
+ while (argc) {
+ if (!argv[0] || !*argv[0] || !argv[1] || !*argv[1] ||
+ !argv[2] || !*argv[2])
+ die("%s", usage_error);
+
+ trace2_data_string(argv[0], the_repository, argv[1], argv[2]);
+ argv += 3;
+ argc -= 3;
+ }
+
+ return 0;
+}
+
+static int ut_007BUG(int argc UNUSED, const char **argv UNUSED)
+{
+ /*
+ * Exercise BUG() to ensure that the message is printed to trace2.
+ */
+ BUG("the bug message");
+}
+
+static int ut_008bug(int argc UNUSED, const char **argv UNUSED)
+{
+ bug("a bug message");
+ bug("another bug message");
+ BUG_if_bug("an explicit BUG_if_bug() following bug() call(s) is nice, but not required");
+ return 0;
+}
+
+static int ut_009bug_BUG(int argc UNUSED, const char **argv UNUSED)
+{
+ bug("a bug message");
+ bug("another bug message");
+ /* The BUG_if_bug(...) isn't here, but we'll spot bug() calls on exit()! */
+ return 0;
+}
+
+static int ut_010bug_BUG(int argc UNUSED, const char **argv UNUSED)
+{
+ bug("a %s message", "bug");
+ BUG("a %s message", "BUG");
+}
+
+/*
+ * Single-threaded timer test. Create several intervals using the
+ * TEST1 timer. The test script can verify that an aggregate Trace2
+ * "timer" event is emitted indicating that we started+stopped the
+ * timer the requested number of times.
+ */
+static int ut_100timer(int argc, const char **argv)
+{
+ const char *usage_error =
+ "expect <count> <ms_delay>";
+
+ int count = 0;
+ int delay = 0;
+ int k;
+
+ if (argc != 2)
+ die("%s", usage_error);
+ if (get_i(&count, argv[0]))
+ die("%s", usage_error);
+ if (get_i(&delay, argv[1]))
+ die("%s", usage_error);
+
+ for (k = 0; k < count; k++) {
+ trace2_timer_start(TRACE2_TIMER_ID_TEST1);
+ sleep_millisec(delay);
+ trace2_timer_stop(TRACE2_TIMER_ID_TEST1);
+ }
+
+ return 0;
+}
+
+struct ut_101_data {
+ int count;
+ int delay;
+};
+
+static void *ut_101timer_thread_proc(void *_ut_101_data)
+{
+ struct ut_101_data *data = _ut_101_data;
+ int k;
+
+ trace2_thread_start("ut_101");
+
+ for (k = 0; k < data->count; k++) {
+ trace2_timer_start(TRACE2_TIMER_ID_TEST2);
+ sleep_millisec(data->delay);
+ trace2_timer_stop(TRACE2_TIMER_ID_TEST2);
+ }
+
+ trace2_thread_exit();
+ return NULL;
+}
+
+/*
+ * Multi-threaded timer test. Create several threads that each create
+ * several intervals using the TEST2 timer. The test script can verify
+ * that an individual Trace2 "th_timer" events for each thread and an
+ * aggregate "timer" event are generated.
+ */
+static int ut_101timer(int argc, const char **argv)
+{
+ const char *usage_error =
+ "expect <count> <ms_delay> <threads>";
+
+ struct ut_101_data data = { 0, 0 };
+ int nr_threads = 0;
+ int k;
+ pthread_t *pids = NULL;
+
+ if (argc != 3)
+ die("%s", usage_error);
+ if (get_i(&data.count, argv[0]))
+ die("%s", usage_error);
+ if (get_i(&data.delay, argv[1]))
+ die("%s", usage_error);
+ if (get_i(&nr_threads, argv[2]))
+ die("%s", usage_error);
+
+ CALLOC_ARRAY(pids, nr_threads);
+
+ for (k = 0; k < nr_threads; k++) {
+ if (pthread_create(&pids[k], NULL, ut_101timer_thread_proc, &data))
+ die("failed to create thread[%d]", k);
+ }
+
+ for (k = 0; k < nr_threads; k++) {
+ if (pthread_join(pids[k], NULL))
+ die("failed to join thread[%d]", k);
+ }
+
+ free(pids);
+
+ return 0;
+}
+
+/*
+ * Single-threaded counter test. Add several values to the TEST1 counter.
+ * The test script can verify that the final sum is reported in the "counter"
+ * event.
+ */
+static int ut_200counter(int argc, const char **argv)
+{
+ const char *usage_error =
+ "expect <v1> [<v2> [...]]";
+ int value;
+ int k;
+
+ if (argc < 1)
+ die("%s", usage_error);
+
+ for (k = 0; k < argc; k++) {
+ if (get_i(&value, argv[k]))
+ die("invalid value[%s] -- %s",
+ argv[k], usage_error);
+ trace2_counter_add(TRACE2_COUNTER_ID_TEST1, value);
+ }
+
+ return 0;
+}
+
+/*
+ * Multi-threaded counter test. Create seveal threads that each increment
+ * the TEST2 global counter. The test script can verify that an individual
+ * "th_counter" event is generated with a partial sum for each thread and
+ * that a final aggregate "counter" event is generated.
+ */
+
+struct ut_201_data {
+ int v1;
+ int v2;
+};
+
+static void *ut_201counter_thread_proc(void *_ut_201_data)
+{
+ struct ut_201_data *data = _ut_201_data;
+
+ trace2_thread_start("ut_201");
+
+ trace2_counter_add(TRACE2_COUNTER_ID_TEST2, data->v1);
+ trace2_counter_add(TRACE2_COUNTER_ID_TEST2, data->v2);
+
+ trace2_thread_exit();
+ return NULL;
+}
+
+static int ut_201counter(int argc, const char **argv)
+{
+ const char *usage_error =
+ "expect <v1> <v2> <threads>";
+
+ struct ut_201_data data = { 0, 0 };
+ int nr_threads = 0;
+ int k;
+ pthread_t *pids = NULL;
+
+ if (argc != 3)
+ die("%s", usage_error);
+ if (get_i(&data.v1, argv[0]))
+ die("%s", usage_error);
+ if (get_i(&data.v2, argv[1]))
+ die("%s", usage_error);
+ if (get_i(&nr_threads, argv[2]))
+ die("%s", usage_error);
+
+ CALLOC_ARRAY(pids, nr_threads);
+
+ for (k = 0; k < nr_threads; k++) {
+ if (pthread_create(&pids[k], NULL, ut_201counter_thread_proc, &data))
+ die("failed to create thread[%d]", k);
+ }
+
+ for (k = 0; k < nr_threads; k++) {
+ if (pthread_join(pids[k], NULL))
+ die("failed to join thread[%d]", k);
+ }
+
+ free(pids);
+
+ return 0;
+}
+
+/*
+ * Usage:
+ * test-tool trace2 <ut_name_1> <ut_usage_1>
+ * test-tool trace2 <ut_name_2> <ut_usage_2>
+ * ...
+ */
+#define USAGE_PREFIX "test-tool trace2"
+
+/* clang-format off */
+static struct unit_test ut_table[] = {
+ { ut_001return, "001return", "<exit_code>" },
+ { ut_002exit, "002exit", "<exit_code>" },
+ { ut_003error, "003error", "<error_message>+" },
+ { ut_004child, "004child", "[<child_command_line>]" },
+ { ut_005exec, "005exec", "<git_command_args>" },
+ { ut_006data, "006data", "[<category> <key> <value>]+" },
+ { ut_007BUG, "007bug", "" },
+ { ut_008bug, "008bug", "" },
+ { ut_009bug_BUG, "009bug_BUG","" },
+ { ut_010bug_BUG, "010bug_BUG","" },
+
+ { ut_100timer, "100timer", "<count> <ms_delay>" },
+ { ut_101timer, "101timer", "<count> <ms_delay> <threads>" },
+
+ { ut_200counter, "200counter", "<v1> [<v2> [<v3> [...]]]" },
+ { ut_201counter, "201counter", "<v1> <v2> <threads>" },
+};
+/* clang-format on */
+
+/* clang-format off */
+#define for_each_ut(k, ut_k) \
+ for (k = 0, ut_k = &ut_table[k]; \
+ k < ARRAY_SIZE(ut_table); \
+ k++, ut_k = &ut_table[k])
+/* clang-format on */
+
+static int print_usage(void)
+{
+ int k;
+ struct unit_test *ut_k;
+
+ fprintf(stderr, "usage:\n");
+ for_each_ut (k, ut_k)
+ fprintf(stderr, "\t%s %s %s\n", USAGE_PREFIX, ut_k->ut_name,
+ ut_k->ut_usage);
+
+ return 129;
+}
+
+/*
+ * Issue various trace2 events for testing.
+ *
+ * We assume that these trace2 routines has already been called:
+ * [] trace2_initialize() [common-main.c:main()]
+ * [] trace2_cmd_start() [common-main.c:main()]
+ * [] trace2_cmd_name() [test-tool.c:cmd_main()]
+ * [] tracd2_cmd_list_config() [test-tool.c:cmd_main()]
+ * So that:
+ * [] the various trace2 streams are open.
+ * [] the process SID has been created.
+ * [] the "version" event has been generated.
+ * [] the "start" event has been generated.
+ * [] the "cmd_name" event has been generated.
+ * [] this writes various "def_param" events for interesting config values.
+ *
+ * We return from here and let test-tool.c::cmd_main() pass the exit
+ * code to common-main.c::main(), which will use it to call
+ * trace2_cmd_exit().
+ */
+int cmd__trace2(int argc, const char **argv)
+{
+ int k;
+ struct unit_test *ut_k;
+
+ argc--; /* skip over "trace2" arg */
+ argv++;
+
+ if (argc)
+ for_each_ut (k, ut_k)
+ if (!strcmp(argv[0], ut_k->ut_name))
+ return ut_k->ut_fn(argc - 1, argv + 1);
+
+ return print_usage();
+}
diff --git a/t/helper/test-truncate.c b/t/helper/test-truncate.c
new file mode 100644
index 0000000..3931dea
--- /dev/null
+++ b/t/helper/test-truncate.c
@@ -0,0 +1,25 @@
+#include "test-tool.h"
+#include "git-compat-util.h"
+
+/*
+ * Truncate a file to the given size.
+ */
+int cmd__truncate(int argc, const char **argv)
+{
+ char *p = NULL;
+ uintmax_t sz = 0;
+ int fd = -1;
+
+ if (argc != 3)
+ die("expected filename and size");
+
+ sz = strtoumax(argv[2], &p, 0);
+ if (*p)
+ die("invalid size");
+
+ fd = xopen(argv[1], O_WRONLY | O_CREAT, 0600);
+
+ if (ftruncate(fd, (off_t) sz) < 0)
+ die_errno("failed to truncate file");
+ return 0;
+}
diff --git a/t/helper/test-urlmatch-normalization.c b/t/helper/test-urlmatch-normalization.c
new file mode 100644
index 0000000..86edd45
--- /dev/null
+++ b/t/helper/test-urlmatch-normalization.c
@@ -0,0 +1,56 @@
+#include "test-tool.h"
+#include "git-compat-util.h"
+#include "urlmatch.h"
+
+int cmd__urlmatch_normalization(int argc, const char **argv)
+{
+ const char usage[] = "test-tool urlmatch-normalization [-p | -l] <url1> | <url1> <url2>";
+ char *url1 = NULL, *url2 = NULL;
+ int opt_p = 0, opt_l = 0;
+ int ret = 0;
+
+ /*
+ * For one url, succeed if url_normalize succeeds on it, fail otherwise.
+ * For two urls, succeed only if url_normalize succeeds on both and
+ * the results compare equal with strcmp. If -p is given (one url only)
+ * and url_normalize succeeds, print the result followed by "\n". If
+ * -l is given (one url only) and url_normalize succeeds, print the
+ * returned length in decimal followed by "\n".
+ */
+
+ if (argc > 1 && !strcmp(argv[1], "-p")) {
+ opt_p = 1;
+ argc--;
+ argv++;
+ } else if (argc > 1 && !strcmp(argv[1], "-l")) {
+ opt_l = 1;
+ argc--;
+ argv++;
+ }
+
+ if (argc < 2 || argc > 3)
+ die("%s", usage);
+
+ if (argc == 2) {
+ struct url_info info;
+ url1 = url_normalize(argv[1], &info);
+ if (!url1)
+ return 1;
+ if (opt_p)
+ printf("%s\n", url1);
+ if (opt_l)
+ printf("%u\n", (unsigned)info.url_len);
+ goto cleanup;
+ }
+
+ if (opt_p || opt_l)
+ die("%s", usage);
+
+ url1 = url_normalize(argv[1], NULL);
+ url2 = url_normalize(argv[2], NULL);
+ ret = (url1 && url2 && !strcmp(url1, url2)) ? 0 : 1;
+cleanup:
+ free(url1);
+ free(url2);
+ return ret;
+}
diff --git a/t/helper/test-userdiff.c b/t/helper/test-userdiff.c
new file mode 100644
index 0000000..0ce31ce
--- /dev/null
+++ b/t/helper/test-userdiff.c
@@ -0,0 +1,48 @@
+#include "test-tool.h"
+#include "setup.h"
+#include "userdiff.h"
+#include "config.h"
+
+static int driver_cb(struct userdiff_driver *driver,
+ enum userdiff_driver_type type, void *priv)
+{
+ enum userdiff_driver_type *want_type = priv;
+ if (type & *want_type && driver->funcname.pattern)
+ puts(driver->name);
+ return 0;
+}
+
+static int cmd__userdiff_config(const char *var, const char *value,
+ const struct config_context *ctx UNUSED,
+ void *cb UNUSED)
+{
+ if (userdiff_config(var, value) < 0)
+ return -1;
+ return 0;
+}
+
+int cmd__userdiff(int argc, const char **argv)
+{
+ enum userdiff_driver_type want = 0;
+ if (argc != 2)
+ return 1;
+
+ if (!strcmp(argv[1], "list-drivers"))
+ want = (USERDIFF_DRIVER_TYPE_BUILTIN |
+ USERDIFF_DRIVER_TYPE_CUSTOM);
+ else if (!strcmp(argv[1], "list-builtin-drivers"))
+ want = USERDIFF_DRIVER_TYPE_BUILTIN;
+ else if (!strcmp(argv[1], "list-custom-drivers"))
+ want = USERDIFF_DRIVER_TYPE_CUSTOM;
+ else
+ return error("unknown argument %s", argv[1]);
+
+ if (want & USERDIFF_DRIVER_TYPE_CUSTOM) {
+ setup_git_directory();
+ git_config(cmd__userdiff_config, NULL);
+ }
+
+ for_each_userdiff_driver(driver_cb, &want);
+
+ return 0;
+}
diff --git a/t/helper/test-wildmatch.c b/t/helper/test-wildmatch.c
new file mode 100644
index 0000000..b4ff5f9
--- /dev/null
+++ b/t/helper/test-wildmatch.c
@@ -0,0 +1,24 @@
+#include "test-tool.h"
+#include "wildmatch.h"
+
+int cmd__wildmatch(int argc, const char **argv)
+{
+ int i;
+ for (i = 2; i < argc; i++) {
+ if (argv[i][0] == '/')
+ die("Forward slash is not allowed at the beginning of the\n"
+ "pattern because Windows does not like it. Use `XXX/' instead.");
+ else if (!strncmp(argv[i], "XXX/", 4))
+ argv[i] += 3;
+ }
+ if (!strcmp(argv[1], "wildmatch"))
+ return !!wildmatch(argv[3], argv[2], WM_PATHNAME);
+ else if (!strcmp(argv[1], "iwildmatch"))
+ return !!wildmatch(argv[3], argv[2], WM_PATHNAME | WM_CASEFOLD);
+ else if (!strcmp(argv[1], "pathmatch"))
+ return !!wildmatch(argv[3], argv[2], 0);
+ else if (!strcmp(argv[1], "ipathmatch"))
+ return !!wildmatch(argv[3], argv[2], WM_CASEFOLD);
+ else
+ return 1;
+}
diff --git a/t/helper/test-windows-named-pipe.c b/t/helper/test-windows-named-pipe.c
new file mode 100644
index 0000000..ae52183
--- /dev/null
+++ b/t/helper/test-windows-named-pipe.c
@@ -0,0 +1,72 @@
+#include "test-tool.h"
+#include "git-compat-util.h"
+#include "strbuf.h"
+
+#ifdef GIT_WINDOWS_NATIVE
+static const char *usage_string = "<pipe-filename>";
+
+#define TEST_BUFSIZE (4096)
+
+int cmd__windows_named_pipe(int argc, const char **argv)
+{
+ const char *filename;
+ struct strbuf pathname = STRBUF_INIT;
+ int err;
+ HANDLE h;
+ BOOL connected;
+ char buf[TEST_BUFSIZE + 1];
+
+ if (argc < 2)
+ goto print_usage;
+ filename = argv[1];
+ if (strpbrk(filename, "/\\"))
+ goto print_usage;
+ strbuf_addf(&pathname, "//./pipe/%s", filename);
+
+ /*
+ * Create a single instance of the server side of the named pipe.
+ * This will allow exactly one client instance to connect to it.
+ */
+ h = CreateNamedPipeA(
+ pathname.buf,
+ PIPE_ACCESS_INBOUND | FILE_FLAG_FIRST_PIPE_INSTANCE,
+ PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
+ PIPE_UNLIMITED_INSTANCES,
+ TEST_BUFSIZE, TEST_BUFSIZE, 0, NULL);
+ if (h == INVALID_HANDLE_VALUE) {
+ err = err_win_to_posix(GetLastError());
+ fprintf(stderr, "CreateNamedPipe failed: %s\n",
+ strerror(err));
+ return err;
+ }
+
+ connected = ConnectNamedPipe(h, NULL)
+ ? TRUE
+ : (GetLastError() == ERROR_PIPE_CONNECTED);
+ if (!connected) {
+ err = err_win_to_posix(GetLastError());
+ fprintf(stderr, "ConnectNamedPipe failed: %s\n",
+ strerror(err));
+ CloseHandle(h);
+ return err;
+ }
+
+ while (1) {
+ DWORD nbr;
+ BOOL success = ReadFile(h, buf, TEST_BUFSIZE, &nbr, NULL);
+ if (!success || nbr == 0)
+ break;
+ buf[nbr] = 0;
+
+ write(1, buf, nbr);
+ }
+
+ DisconnectNamedPipe(h);
+ CloseHandle(h);
+ return 0;
+
+print_usage:
+ fprintf(stderr, "usage: %s %s\n", argv[0], usage_string);
+ return 1;
+}
+#endif
diff --git a/t/helper/test-write-cache.c b/t/helper/test-write-cache.c
new file mode 100644
index 0000000..f084034
--- /dev/null
+++ b/t/helper/test-write-cache.c
@@ -0,0 +1,24 @@
+#define USE_THE_INDEX_VARIABLE
+#include "test-tool.h"
+#include "lockfile.h"
+#include "read-cache-ll.h"
+#include "repository.h"
+#include "setup.h"
+
+int cmd__write_cache(int argc, const char **argv)
+{
+ struct lock_file index_lock = LOCK_INIT;
+ int i, cnt = 1;
+ if (argc == 2)
+ cnt = strtol(argv[1], NULL, 0);
+ setup_git_directory();
+ repo_read_index(the_repository);
+ for (i = 0; i < cnt; i++) {
+ repo_hold_locked_index(the_repository, &index_lock,
+ LOCK_DIE_ON_ERROR);
+ if (write_locked_index(&the_index, &index_lock, COMMIT_LOCK))
+ die("unable to write index file");
+ }
+
+ return 0;
+}
diff --git a/t/helper/test-xml-encode.c b/t/helper/test-xml-encode.c
new file mode 100644
index 0000000..b2f330d
--- /dev/null
+++ b/t/helper/test-xml-encode.c
@@ -0,0 +1,80 @@
+#include "test-tool.h"
+
+static const char *utf8_replace_character = "&#xfffd;";
+
+/*
+ * Encodes (possibly incorrect) UTF-8 on <stdin> to <stdout>, to be embedded
+ * in an XML file.
+ */
+int cmd__xml_encode(int argc UNUSED, const char **argv UNUSED)
+{
+ unsigned char buf[1024], tmp[4], *tmp2 = NULL;
+ ssize_t cur = 0, len = 1, remaining = 0;
+ unsigned char ch;
+
+ for (;;) {
+ if (++cur == len) {
+ len = xread(0, buf, sizeof(buf));
+ if (!len)
+ return 0;
+ if (len < 0)
+ die_errno("Could not read <stdin>");
+ cur = 0;
+ }
+ ch = buf[cur];
+
+ if (tmp2) {
+ if ((ch & 0xc0) != 0x80) {
+ fputs(utf8_replace_character, stdout);
+ tmp2 = NULL;
+ cur--;
+ continue;
+ }
+ *tmp2 = ch;
+ tmp2++;
+ if (--remaining == 0) {
+ fwrite(tmp, tmp2 - tmp, 1, stdout);
+ tmp2 = NULL;
+ }
+ continue;
+ }
+
+ if (!(ch & 0x80)) {
+ /* 0xxxxxxx */
+ if (ch == '&')
+ fputs("&amp;", stdout);
+ else if (ch == '\'')
+ fputs("&apos;", stdout);
+ else if (ch == '"')
+ fputs("&quot;", stdout);
+ else if (ch == '<')
+ fputs("&lt;", stdout);
+ else if (ch == '>')
+ fputs("&gt;", stdout);
+ else if (ch >= 0x20)
+ fputc(ch, stdout);
+ else if (ch == 0x09 || ch == 0x0a || ch == 0x0d)
+ fprintf(stdout, "&#x%02x;", ch);
+ else
+ fputs(utf8_replace_character, stdout);
+ } else if ((ch & 0xe0) == 0xc0) {
+ /* 110XXXXx 10xxxxxx */
+ tmp[0] = ch;
+ remaining = 1;
+ tmp2 = tmp + 1;
+ } else if ((ch & 0xf0) == 0xe0) {
+ /* 1110XXXX 10Xxxxxx 10xxxxxx */
+ tmp[0] = ch;
+ remaining = 2;
+ tmp2 = tmp + 1;
+ } else if ((ch & 0xf8) == 0xf0) {
+ /* 11110XXX 10XXxxxx 10xxxxxx 10xxxxxx */
+ tmp[0] = ch;
+ remaining = 3;
+ tmp2 = tmp + 1;
+ } else
+ fputs(utf8_replace_character, stdout);
+ }
+
+ return 0;
+}
diff --git a/t/interop/.gitignore b/t/interop/.gitignore
new file mode 100644
index 0000000..49c78d3
--- /dev/null
+++ b/t/interop/.gitignore
@@ -0,0 +1,4 @@
+/trash directory*/
+/test-results/
+/.prove/
+/build/
diff --git a/t/interop/Makefile b/t/interop/Makefile
new file mode 100644
index 0000000..6911c29
--- /dev/null
+++ b/t/interop/Makefile
@@ -0,0 +1,19 @@
+# Import tree-wide shared Makefile behavior and libraries
+include ../../shared.mak
+
+-include ../../config.mak
+export GIT_TEST_OPTIONS
+
+SHELL_PATH ?= $(SHELL)
+SHELL_PATH_SQ = $(subst ','\'',$(SHELL_PATH))
+T = $(sort $(wildcard i[0-9][0-9][0-9][0-9]-*.sh))
+
+all: $(T)
+
+$(T):
+ @echo "*** $@ ***"; '$(SHELL_PATH_SQ)' $@ $(GIT_TEST_OPTS)
+
+clean:
+ rm -rf build "trash directory".* test-results
+
+.PHONY: all clean $(T)
diff --git a/t/interop/README b/t/interop/README
new file mode 100644
index 0000000..72d42bd
--- /dev/null
+++ b/t/interop/README
@@ -0,0 +1,85 @@
+Git version interoperability tests
+==================================
+
+This directory has interoperability tests for git. Each script is
+similar to the normal test scripts found in t/, but with the added twist
+that two special versions of git, "git.a" and "git.b", are available in
+the PATH. Individual tests can then check the interaction between the
+two versions.
+
+When you add a feature that handles backwards compatibility between git
+versions, it's encouraged to add a test here to make sure it behaves as
+you expect.
+
+
+Running Tests
+-------------
+
+The easiest way to run tests is to say "make". This runs all
+the tests against their default versions.
+
+You can run a single test like:
+
+ $ ./i0000-basic.sh
+ ok 1 - bare git is forbidden
+ ok 2 - git.a version (v1.6.6.3)
+ ok 3 - git.b version (v2.11.1)
+ # passed all 3 test(s)
+ 1..3
+
+Each test contains default versions to run against. You may override
+these by setting `GIT_TEST_VERSION_A` and `GIT_TEST_VERSION_B` in the
+environment. Note that not all combinations will give sensible outcomes
+for all tests (e.g., a test checking for a specific old/new interaction
+may want something "old" enough" and something "new" enough; see
+individual tests for details).
+
+Version names should be resolvable as revisions in the current
+repository. They will be exported and built as needed using the
+config.mak files found at the root of your working tree.
+
+The exception is the special version "." which uses the currently-built
+contents of your working tree.
+
+You can set the following variables (in the environment or in your config.mak):
+
+ GIT_INTEROP_MAKE_OPTS
+ Options to pass to `make` when building a git version (e.g.,
+ `-j8`).
+
+You can also pass any command-line options taken by ordinary git tests (e.g.,
+"-v").
+
+
+Naming Tests
+------------
+
+The interop test files are named like:
+
+ iNNNN-short-description.sh
+
+where N is a decimal digit. The same conventions for choosing NNNN as
+for normal tests apply.
+
+
+Writing Tests
+-------------
+
+An interop test script starts like a normal script, declaring a few
+variables and then including interop-lib.sh (which includes test-lib.sh).
+Besides test_description, you should also set the $VERSION_A and $VERSION_B
+variables to give the default versions to test against. See t0000-basic.sh for
+an example.
+
+You can then use test_expect_success as usual, with a few differences:
+
+ 1. The special commands "git.a" and "git.b" correspond to the
+ two versions.
+
+ 2. You cannot call a bare "git". This is to prevent accidents where
+ you meant "git.a" or "git.b".
+
+ 3. The trash directory is _not_ a git repository by default. You
+ should create one with the appropriate version of git.
+
+At the end of the script, call test_done as usual.
diff --git a/t/interop/i0000-basic.sh b/t/interop/i0000-basic.sh
new file mode 100755
index 0000000..903e919
--- /dev/null
+++ b/t/interop/i0000-basic.sh
@@ -0,0 +1,27 @@
+#!/bin/sh
+
+# Note that this test only works on real version numbers,
+# as it depends on matching the output to "git version".
+VERSION_A=v1.6.6.3
+VERSION_B=v2.11.1
+
+test_description='sanity test interop library'
+. ./interop-lib.sh
+
+test_expect_success 'bare git is forbidden' '
+ test_must_fail git version
+'
+
+test_expect_success "git.a version ($VERSION_A)" '
+ echo git version ${VERSION_A#v} >expect &&
+ git.a version >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success "git.b version ($VERSION_B)" '
+ echo git version ${VERSION_B#v} >expect &&
+ git.b version >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/interop/i5500-git-daemon.sh b/t/interop/i5500-git-daemon.sh
new file mode 100755
index 0000000..4d22e42
--- /dev/null
+++ b/t/interop/i5500-git-daemon.sh
@@ -0,0 +1,40 @@
+#!/bin/sh
+
+VERSION_A=.
+VERSION_B=v1.0.0
+
+: ${LIB_GIT_DAEMON_PORT:=5500}
+LIB_GIT_DAEMON_COMMAND='git.a daemon'
+
+test_description='clone and fetch by older client'
+. ./interop-lib.sh
+. "$TEST_DIRECTORY"/lib-git-daemon.sh
+
+start_git_daemon --export-all
+
+repo=$GIT_DAEMON_DOCUMENT_ROOT_PATH/repo
+
+test_expect_success "create repo served by $VERSION_A" '
+ git.a init "$repo" &&
+ git.a -C "$repo" commit --allow-empty -m one
+'
+
+test_expect_success "clone with $VERSION_B" '
+ git.b clone "$GIT_DAEMON_URL/repo" child &&
+ echo one >expect &&
+ git.a -C child log -1 --format=%s >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success "fetch with $VERSION_B" '
+ git.a -C "$repo" commit --allow-empty -m two &&
+ (
+ cd child &&
+ git.b fetch
+ ) &&
+ echo two >expect &&
+ git.a -C child log -1 --format=%s FETCH_HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/interop/i5700-protocol-transition.sh b/t/interop/i5700-protocol-transition.sh
new file mode 100755
index 0000000..97e8e58
--- /dev/null
+++ b/t/interop/i5700-protocol-transition.sh
@@ -0,0 +1,68 @@
+#!/bin/sh
+
+VERSION_A=.
+VERSION_B=v2.0.0
+
+: ${LIB_GIT_DAEMON_PORT:=5700}
+LIB_GIT_DAEMON_COMMAND='git.b daemon'
+
+test_description='clone and fetch by client who is trying to use a new protocol'
+. ./interop-lib.sh
+. "$TEST_DIRECTORY"/lib-git-daemon.sh
+
+start_git_daemon --export-all
+
+repo=$GIT_DAEMON_DOCUMENT_ROOT_PATH/repo
+
+test_expect_success "create repo served by $VERSION_B" '
+ git.b init "$repo" &&
+ git.b -C "$repo" commit --allow-empty -m one
+'
+
+test_expect_success "git:// clone with $VERSION_A and protocol v1" '
+ GIT_TRACE_PACKET=1 git.a -c protocol.version=1 clone "$GIT_DAEMON_URL/repo" child 2>log &&
+ git.a -C child log -1 --format=%s >actual &&
+ git.b -C "$repo" log -1 --format=%s >expect &&
+ test_cmp expect actual &&
+ grep "version=1" log
+'
+
+test_expect_success "git:// fetch with $VERSION_A and protocol v1" '
+ git.b -C "$repo" commit --allow-empty -m two &&
+ git.b -C "$repo" log -1 --format=%s >expect &&
+
+ GIT_TRACE_PACKET=1 git.a -C child -c protocol.version=1 fetch 2>log &&
+ git.a -C child log -1 --format=%s FETCH_HEAD >actual &&
+
+ test_cmp expect actual &&
+ grep "version=1" log &&
+ ! grep "version 1" log
+'
+
+stop_git_daemon
+
+test_expect_success "create repo served by $VERSION_B" '
+ git.b init parent &&
+ git.b -C parent commit --allow-empty -m one
+'
+
+test_expect_success "file:// clone with $VERSION_A and protocol v1" '
+ GIT_TRACE_PACKET=1 git.a -c protocol.version=1 clone --upload-pack="git.b upload-pack" parent child2 2>log &&
+ git.a -C child2 log -1 --format=%s >actual &&
+ git.b -C parent log -1 --format=%s >expect &&
+ test_cmp expect actual &&
+ ! grep "version 1" log
+'
+
+test_expect_success "file:// fetch with $VERSION_A and protocol v1" '
+ git.b -C parent commit --allow-empty -m two &&
+ git.b -C parent log -1 --format=%s >expect &&
+
+ GIT_TRACE_PACKET=1 git.a -C child2 -c protocol.version=1 fetch --upload-pack="git.b upload-pack" 2>log &&
+ git.a -C child2 log -1 --format=%s FETCH_HEAD >actual &&
+
+ test_cmp expect actual &&
+ ! grep "version 1" log
+'
+
+test_done
diff --git a/t/interop/interop-lib.sh b/t/interop/interop-lib.sh
new file mode 100644
index 0000000..62f4481
--- /dev/null
+++ b/t/interop/interop-lib.sh
@@ -0,0 +1,92 @@
+# Interoperability testing framework. Each script should source
+# this after setting default $VERSION_A and $VERSION_B variables.
+
+. ../../GIT-BUILD-OPTIONS
+INTEROP_ROOT=$(pwd)
+BUILD_ROOT=$INTEROP_ROOT/build
+
+build_version () {
+ if test -z "$1"
+ then
+ echo >&2 "error: test script did not set default versions"
+ return 1
+ fi
+
+ if test "$1" = "."
+ then
+ git rev-parse --show-toplevel
+ return 0
+ fi
+
+ sha1=$(git rev-parse "$1^{tree}") || return 1
+ dir=$BUILD_ROOT/$sha1
+
+ if test -e "$dir/.built"
+ then
+ echo "$dir"
+ return 0
+ fi
+
+ echo >&2 "==> Building $1..."
+
+ mkdir -p "$dir" || return 1
+
+ (cd "$(git rev-parse --show-cdup)" && git archive --format=tar "$sha1") |
+ (cd "$dir" && tar x) ||
+ return 1
+
+ for config in config.mak config.mak.autogen config.status
+ do
+ if test -e "$INTEROP_ROOT/../../$config"
+ then
+ cp "$INTEROP_ROOT/../../$config" "$dir/" || return 1
+ fi
+ done
+
+ (
+ cd "$dir" &&
+ make $GIT_INTEROP_MAKE_OPTS >&2 &&
+ touch .built
+ ) || return 1
+
+ echo "$dir"
+}
+
+# Old versions of git don't have bin-wrappers, so let's give a rough emulation.
+wrap_git () {
+ write_script "$1" <<-EOF
+ GIT_EXEC_PATH="$2"
+ export GIT_EXEC_PATH
+ PATH="$2:\$PATH"
+ export GIT_EXEC_PATH
+ exec git "\$@"
+ EOF
+}
+
+generate_wrappers () {
+ mkdir -p .bin &&
+ wrap_git .bin/git.a "$DIR_A" &&
+ wrap_git .bin/git.b "$DIR_B" &&
+ write_script .bin/git <<-\EOF &&
+ echo >&2 fatal: test tried to run generic git: $*
+ exit 1
+ EOF
+ PATH=$(pwd)/.bin:$PATH
+}
+
+VERSION_A=${GIT_TEST_VERSION_A:-$VERSION_A}
+VERSION_B=${GIT_TEST_VERSION_B:-$VERSION_B}
+
+if ! DIR_A=$(build_version "$VERSION_A") ||
+ ! DIR_B=$(build_version "$VERSION_B")
+then
+ echo >&2 "fatal: unable to build git versions"
+ exit 1
+fi
+
+TEST_DIRECTORY=$INTEROP_ROOT/..
+TEST_OUTPUT_DIRECTORY=$INTEROP_ROOT
+TEST_NO_CREATE_REPO=t
+. "$TEST_DIRECTORY"/test-lib.sh
+
+generate_wrappers || die "unable to set up interop test environment"
diff --git a/t/lib-bash.sh b/t/lib-bash.sh
new file mode 100644
index 0000000..b0b6060
--- /dev/null
+++ b/t/lib-bash.sh
@@ -0,0 +1,19 @@
+# Shell library sourced instead of ./test-lib.sh by tests that need
+# to run under Bash; primarily intended for tests of the completion
+# script.
+
+if test -n "$BASH" && test -z "$POSIXLY_CORRECT"
+then
+ # we are in full-on bash mode
+ true
+elif type bash >/dev/null 2>&1
+then
+ # execute in full-on bash mode
+ unset POSIXLY_CORRECT
+ exec bash "$0" "$@"
+else
+ echo '1..0 #SKIP skipping bash completion tests; bash not available'
+ exit 0
+fi
+
+. ./test-lib.sh
diff --git a/t/lib-bitmap.sh b/t/lib-bitmap.sh
new file mode 100644
index 0000000..f595937
--- /dev/null
+++ b/t/lib-bitmap.sh
@@ -0,0 +1,451 @@
+# Helpers for scripts testing bitmap functionality; see t5310 for
+# example usage.
+
+objdir=.git/objects
+midx=$objdir/pack/multi-pack-index
+
+# Compare a file containing rev-list bitmap traversal output to its non-bitmap
+# counterpart. You can't just use test_cmp for this, because the two produce
+# subtly different output:
+#
+# - regular output is in traversal order, whereas bitmap is split by type,
+# with non-packed objects at the end
+#
+# - regular output has a space and the pathname appended to non-commit
+# objects; bitmap output omits this
+#
+# This function normalizes and compares the two. The second file should
+# always be the bitmap output.
+test_bitmap_traversal () {
+ if test "$1" = "--no-confirm-bitmaps"
+ then
+ shift
+ elif cmp "$1" "$2"
+ then
+ echo >&2 "identical raw outputs; are you sure bitmaps were used?"
+ return 1
+ fi &&
+ cut -d' ' -f1 "$1" | sort >"$1.normalized" &&
+ sort "$2" >"$2.normalized" &&
+ test_cmp "$1.normalized" "$2.normalized" &&
+ rm -f "$1.normalized" "$2.normalized"
+}
+
+# To ensure the logic for "maximal commits" is exercised, make
+# the repository a bit more complicated.
+#
+# other second
+# * *
+# (99 commits) (99 commits)
+# * *
+# |\ /|
+# | * octo-other octo-second * |
+# |/|\_________ ____________/|\|
+# | \ \/ __________/ |
+# | | ________/\ / |
+# * |/ * merge-right *
+# | _|__________/ \____________ |
+# |/ | \|
+# (l1) * * merge-left * (r1)
+# | / \________________________ |
+# |/ \|
+# (l2) * * (r2)
+# \___________________________ |
+# \|
+# * (base)
+#
+# We only push bits down the first-parent history, which
+# makes some of these commits unimportant!
+#
+# The important part for the maximal commit algorithm is how
+# the bitmasks are extended. Assuming starting bit positions
+# for second (bit 0) and other (bit 1), the bitmasks at the
+# end should be:
+#
+# second: 1 (maximal, selected)
+# other: 01 (maximal, selected)
+# (base): 11 (maximal)
+#
+# This complicated history was important for a previous
+# version of the walk that guarantees never walking a
+# commit multiple times. That goal might be important
+# again, so preserve this complicated case. For now, this
+# test will guarantee that the bitmaps are computed
+# correctly, even with the repeat calculations.
+setup_bitmap_history() {
+ test_expect_success 'setup repo with moderate-sized history' '
+ test_commit_bulk --id=file 10 &&
+ git branch -M second &&
+ git checkout -b other HEAD~5 &&
+ test_commit_bulk --id=side 10 &&
+
+ # add complicated history setup, including merges and
+ # ambiguous merge-bases
+
+ git checkout -b merge-left other~2 &&
+ git merge second~2 -m "merge-left" &&
+
+ git checkout -b merge-right second~1 &&
+ git merge other~1 -m "merge-right" &&
+
+ git checkout -b octo-second second &&
+ git merge merge-left merge-right -m "octopus-second" &&
+
+ git checkout -b octo-other other &&
+ git merge merge-left merge-right -m "octopus-other" &&
+
+ git checkout other &&
+ git merge octo-other -m "pull octopus" &&
+
+ git checkout second &&
+ git merge octo-second -m "pull octopus" &&
+
+ # Remove these branches so they are not selected
+ # as bitmap tips
+ git branch -D merge-left &&
+ git branch -D merge-right &&
+ git branch -D octo-other &&
+ git branch -D octo-second &&
+
+ # add padding to make these merges less interesting
+ # and avoid having them selected for bitmaps
+ test_commit_bulk --id=file 100 &&
+ git checkout other &&
+ test_commit_bulk --id=side 100 &&
+ git checkout second &&
+
+ bitmaptip=$(git rev-parse second) &&
+ blob=$(echo tagged-blob | git hash-object -w --stdin) &&
+ git tag tagged-blob $blob
+ '
+}
+
+rev_list_tests_head () {
+ test_expect_success "counting commits via bitmap ($state, $branch)" '
+ git rev-list --count $branch >expect &&
+ git rev-list --use-bitmap-index --count $branch >actual &&
+ test_cmp expect actual
+ '
+
+ test_expect_success "counting partial commits via bitmap ($state, $branch)" '
+ git rev-list --count $branch~5..$branch >expect &&
+ git rev-list --use-bitmap-index --count $branch~5..$branch >actual &&
+ test_cmp expect actual
+ '
+
+ test_expect_success "counting commits with limit ($state, $branch)" '
+ git rev-list --count -n 1 $branch >expect &&
+ git rev-list --use-bitmap-index --count -n 1 $branch >actual &&
+ test_cmp expect actual
+ '
+
+ test_expect_success "counting non-linear history ($state, $branch)" '
+ git rev-list --count other...second >expect &&
+ git rev-list --use-bitmap-index --count other...second >actual &&
+ test_cmp expect actual
+ '
+
+ test_expect_success "counting commits with limiting ($state, $branch)" '
+ git rev-list --count $branch -- 1.t >expect &&
+ git rev-list --use-bitmap-index --count $branch -- 1.t >actual &&
+ test_cmp expect actual
+ '
+
+ test_expect_success "counting objects via bitmap ($state, $branch)" '
+ git rev-list --count --objects $branch >expect &&
+ git rev-list --use-bitmap-index --count --objects $branch >actual &&
+ test_cmp expect actual
+ '
+
+ test_expect_success "enumerate commits ($state, $branch)" '
+ git rev-list --use-bitmap-index $branch >actual &&
+ git rev-list $branch >expect &&
+ test_bitmap_traversal --no-confirm-bitmaps expect actual
+ '
+
+ test_expect_success "enumerate --objects ($state, $branch)" '
+ git rev-list --objects --use-bitmap-index $branch >actual &&
+ git rev-list --objects $branch >expect &&
+ test_bitmap_traversal expect actual
+ '
+
+ test_expect_success "bitmap --objects handles non-commit objects ($state, $branch)" '
+ git rev-list --objects --use-bitmap-index $branch tagged-blob >actual &&
+ grep $blob actual
+ '
+}
+
+rev_list_tests () {
+ state=$1
+
+ for branch in "second" "other"
+ do
+ rev_list_tests_head
+ done
+}
+
+basic_bitmap_tests () {
+ tip="$1"
+ test_expect_success 'rev-list --test-bitmap verifies bitmaps' "
+ git rev-list --test-bitmap "${tip:-HEAD}"
+ "
+
+ rev_list_tests 'full bitmap'
+
+ test_expect_success 'clone from bitmapped repository' '
+ rm -fr clone.git &&
+ git clone --no-local --bare . clone.git &&
+ git rev-parse HEAD >expect &&
+ git --git-dir=clone.git rev-parse HEAD >actual &&
+ test_cmp expect actual
+ '
+
+ test_expect_success 'partial clone from bitmapped repository' '
+ test_config uploadpack.allowfilter true &&
+ rm -fr partial-clone.git &&
+ git clone --no-local --bare --filter=blob:none . partial-clone.git &&
+ (
+ cd partial-clone.git &&
+ pack=$(echo objects/pack/*.pack) &&
+ git verify-pack -v "$pack" >have &&
+ awk "/blob/ { print \$1 }" <have >blobs &&
+ # we expect this single blob because of the direct ref
+ git rev-parse refs/tags/tagged-blob >expect &&
+ test_cmp expect blobs
+ )
+ '
+
+ test_expect_success 'setup further non-bitmapped commits' '
+ test_commit_bulk --id=further 10
+ '
+
+ rev_list_tests 'partial bitmap'
+
+ test_expect_success 'fetch (partial bitmap)' '
+ git --git-dir=clone.git fetch origin second:second &&
+ git rev-parse HEAD >expect &&
+ git --git-dir=clone.git rev-parse HEAD >actual &&
+ test_cmp expect actual
+ '
+
+ test_expect_success 'enumerating progress counts pack-reused objects' '
+ count=$(git rev-list --objects --all --count) &&
+ git repack -adb &&
+
+ # check first with only reused objects; confirm that our
+ # progress showed the right number, and also that we did
+ # pack-reuse as expected. Check only the final "done"
+ # line of the meter (there may be an arbitrary number of
+ # intermediate lines ending with CR).
+ GIT_PROGRESS_DELAY=0 \
+ git pack-objects --all --stdout --progress \
+ </dev/null >/dev/null 2>stderr &&
+ grep "Enumerating objects: $count, done" stderr &&
+ grep "pack-reused $count" stderr &&
+
+ # now the same but with one non-reused object
+ git commit --allow-empty -m "an extra commit object" &&
+ GIT_PROGRESS_DELAY=0 \
+ git pack-objects --all --stdout --progress \
+ </dev/null >/dev/null 2>stderr &&
+ grep "Enumerating objects: $((count+1)), done" stderr &&
+ grep "pack-reused $count" stderr
+ '
+}
+
+# have_delta <obj> <expected_base>
+#
+# Note that because this relies on cat-file, it might find _any_ copy of an
+# object in the repository. The caller is responsible for making sure
+# there's only one (e.g., via "repack -ad", or having just fetched a copy).
+have_delta () {
+ echo $2 >expect &&
+ echo $1 | git cat-file --batch-check="%(deltabase)" >actual &&
+ test_cmp expect actual
+}
+
+midx_checksum () {
+ test-tool read-midx --checksum "$1"
+}
+
+# midx_pack_source <obj>
+midx_pack_source () {
+ test-tool read-midx --show-objects .git/objects | grep "^$1 " | cut -f2
+}
+
+test_rev_exists () {
+ commit="$1"
+ kind="$2"
+
+ test_expect_success "reverse index exists ($kind)" '
+ GIT_TRACE2_EVENT=$(pwd)/event.trace \
+ git rev-list --test-bitmap "$commit" &&
+
+ if test "rev" = "$kind"
+ then
+ test_path_is_file $midx-$(midx_checksum $objdir).rev
+ fi &&
+ grep "\"category\":\"load_midx_revindex\",\"key\":\"source\",\"value\":\"$kind\"" event.trace
+ '
+}
+
+midx_bitmap_core () {
+ rev_kind="${1:-midx}"
+
+ setup_bitmap_history
+
+ test_expect_success 'create single-pack midx with bitmaps' '
+ git repack -ad &&
+ git multi-pack-index write --bitmap &&
+ test_path_is_file $midx &&
+ test_path_is_file $midx-$(midx_checksum $objdir).bitmap
+ '
+
+ test_rev_exists HEAD "$rev_kind"
+
+ basic_bitmap_tests
+
+ test_expect_success 'create new additional packs' '
+ for i in $(test_seq 1 16)
+ do
+ test_commit "$i" &&
+ git repack -d || return 1
+ done &&
+
+ git checkout -b other2 HEAD~8 &&
+ for i in $(test_seq 1 8)
+ do
+ test_commit "side-$i" &&
+ git repack -d || return 1
+ done &&
+ git checkout second
+ '
+
+ test_expect_success 'create multi-pack midx with bitmaps' '
+ git multi-pack-index write --bitmap &&
+
+ ls $objdir/pack/pack-*.pack >packs &&
+ test_line_count = 25 packs &&
+
+ test_path_is_file $midx &&
+ test_path_is_file $midx-$(midx_checksum $objdir).bitmap
+ '
+
+ test_rev_exists HEAD "$rev_kind"
+
+ basic_bitmap_tests
+
+ test_expect_success '--no-bitmap is respected when bitmaps exist' '
+ git multi-pack-index write --bitmap &&
+
+ test_commit respect--no-bitmap &&
+ git repack -d &&
+
+ test_path_is_file $midx &&
+ test_path_is_file $midx-$(midx_checksum $objdir).bitmap &&
+
+ git multi-pack-index write --no-bitmap &&
+
+ test_path_is_file $midx &&
+ test_path_is_missing $midx-$(midx_checksum $objdir).bitmap &&
+ test_path_is_missing $midx-$(midx_checksum $objdir).rev
+ '
+
+ test_expect_success 'setup midx with base from later pack' '
+ # Write a and b so that "a" is a delta on top of base "b", since Git
+ # prefers to delete contents out of a base rather than add to a shorter
+ # object.
+ test_seq 1 128 >a &&
+ test_seq 1 130 >b &&
+
+ git add a b &&
+ git commit -m "initial commit" &&
+
+ a=$(git rev-parse HEAD:a) &&
+ b=$(git rev-parse HEAD:b) &&
+
+ # In the first pack, "a" is stored as a delta to "b".
+ p1=$(git pack-objects .git/objects/pack/pack <<-EOF
+ $a
+ $b
+ EOF
+ ) &&
+
+ # In the second pack, "a" is missing, and "b" is not a delta nor base to
+ # any other object.
+ p2=$(git pack-objects .git/objects/pack/pack <<-EOF
+ $b
+ $(git rev-parse HEAD)
+ $(git rev-parse HEAD^{tree})
+ EOF
+ ) &&
+
+ git prune-packed &&
+ # Use the second pack as the preferred source, so that "b" occurs
+ # earlier in the MIDX object order, rendering "a" unusable for pack
+ # reuse.
+ git multi-pack-index write --bitmap --preferred-pack=pack-$p2.idx &&
+
+ have_delta $a $b &&
+ test $(midx_pack_source $a) != $(midx_pack_source $b)
+ '
+
+ rev_list_tests 'full bitmap with backwards delta'
+
+ test_expect_success 'clone with bitmaps enabled' '
+ git clone --no-local --bare . clone-reverse-delta.git &&
+ test_when_finished "rm -fr clone-reverse-delta.git" &&
+
+ git rev-parse HEAD >expect &&
+ git --git-dir=clone-reverse-delta.git rev-parse HEAD >actual &&
+ test_cmp expect actual
+ '
+
+ test_expect_success 'changing the preferred pack does not corrupt bitmaps' '
+ rm -fr repo &&
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+
+ test_commit A &&
+ test_commit B &&
+
+ git rev-list --objects --no-object-names HEAD^ >A.objects &&
+ git rev-list --objects --no-object-names HEAD^.. >B.objects &&
+
+ A=$(git pack-objects $objdir/pack/pack <A.objects) &&
+ B=$(git pack-objects $objdir/pack/pack <B.objects) &&
+
+ cat >indexes <<-EOF &&
+ pack-$A.idx
+ pack-$B.idx
+ EOF
+
+ git multi-pack-index write --bitmap --stdin-packs \
+ --preferred-pack=pack-$A.pack <indexes &&
+ git rev-list --test-bitmap A &&
+
+ git multi-pack-index write --bitmap --stdin-packs \
+ --preferred-pack=pack-$B.pack <indexes &&
+ git rev-list --test-bitmap A
+ )
+ '
+}
+
+midx_bitmap_partial_tests () {
+ rev_kind="${1:-midx}"
+
+ test_expect_success 'setup partial bitmaps' '
+ test_commit packed &&
+ git repack &&
+ test_commit loose &&
+ git multi-pack-index write --bitmap &&
+ test_path_is_file $midx &&
+ test_path_is_file $midx-$(midx_checksum $objdir).bitmap
+ '
+
+ test_rev_exists HEAD~ "$rev_kind"
+
+ basic_bitmap_tests HEAD~
+}
diff --git a/t/lib-bundle-uri-protocol.sh b/t/lib-bundle-uri-protocol.sh
new file mode 100644
index 0000000..a4a1af8
--- /dev/null
+++ b/t/lib-bundle-uri-protocol.sh
@@ -0,0 +1,216 @@
+# Set up and run tests of the 'bundle-uri' command in protocol v2
+#
+# The test that includes this script should set BUNDLE_URI_PROTOCOL
+# to one of "file", "git", or "http".
+
+BUNDLE_URI_TEST_PARENT=
+BUNDLE_URI_TEST_URI=
+BUNDLE_URI_TEST_BUNDLE_URI=
+case "$BUNDLE_URI_PROTOCOL" in
+file)
+ BUNDLE_URI_PARENT=file_parent
+ BUNDLE_URI_REPO_URI="file://$PWD/file_parent"
+ BUNDLE_URI_BUNDLE_URI="$BUNDLE_URI_REPO_URI/fake.bdl"
+ test_set_prereq BUNDLE_URI_FILE
+ ;;
+git)
+ . "$TEST_DIRECTORY"/lib-git-daemon.sh
+ start_git_daemon --export-all --enable=receive-pack
+ BUNDLE_URI_PARENT="$GIT_DAEMON_DOCUMENT_ROOT_PATH/parent"
+ BUNDLE_URI_REPO_URI="$GIT_DAEMON_URL/parent"
+ BUNDLE_URI_BUNDLE_URI="https://example.com/fake.bdl"
+ test_set_prereq BUNDLE_URI_GIT
+ ;;
+http)
+ . "$TEST_DIRECTORY"/lib-httpd.sh
+ start_httpd
+ BUNDLE_URI_PARENT="$HTTPD_DOCUMENT_ROOT_PATH/http_parent"
+ BUNDLE_URI_REPO_URI="$HTTPD_URL/smart/http_parent"
+ BUNDLE_URI_BUNDLE_URI="https://example.com/fake.bdl"
+ test_set_prereq BUNDLE_URI_HTTP
+ ;;
+*)
+ BUG "Need to pass valid BUNDLE_URI_PROTOCOL (was \"$BUNDLE_URI_PROTOCOL\")"
+ ;;
+esac
+
+test_expect_success "setup protocol v2 $BUNDLE_URI_PROTOCOL:// tests" '
+ git init "$BUNDLE_URI_PARENT" &&
+ test_commit -C "$BUNDLE_URI_PARENT" one &&
+ git -C "$BUNDLE_URI_PARENT" config uploadpack.advertiseBundleURIs true
+'
+
+case "$BUNDLE_URI_PROTOCOL" in
+http)
+ test_expect_success "setup config for $BUNDLE_URI_PROTOCOL:// tests" '
+ git -C "$BUNDLE_URI_PARENT" config http.receivepack true
+ '
+ ;;
+*)
+ ;;
+esac
+BUNDLE_URI_BUNDLE_URI_ESCAPED=$(echo "$BUNDLE_URI_BUNDLE_URI" | test_uri_escape)
+
+test_expect_success "connect with $BUNDLE_URI_PROTOCOL:// using protocol v2: no bundle-uri" '
+ test_when_finished "rm -f log" &&
+ test_when_finished "git -C \"$BUNDLE_URI_PARENT\" config uploadpack.advertiseBundleURIs true" &&
+ git -C "$BUNDLE_URI_PARENT" config uploadpack.advertiseBundleURIs false &&
+
+ GIT_TRACE_PACKET="$PWD/log" \
+ git \
+ -c protocol.version=2 \
+ ls-remote --symref "$BUNDLE_URI_REPO_URI" \
+ >actual 2>err &&
+
+ # Server responded using protocol v2
+ grep "< version 2" log &&
+
+ ! grep bundle-uri log
+'
+
+test_expect_success "connect with $BUNDLE_URI_PROTOCOL:// using protocol v2: have bundle-uri" '
+ test_when_finished "rm -f log" &&
+
+ GIT_TRACE_PACKET="$PWD/log" \
+ git \
+ -c protocol.version=2 \
+ ls-remote --symref "$BUNDLE_URI_REPO_URI" \
+ >actual 2>err &&
+
+ # Server responded using protocol v2
+ grep "< version 2" log &&
+
+ # Server advertised bundle-uri capability
+ grep "< bundle-uri" log
+'
+
+test_expect_success "clone with $BUNDLE_URI_PROTOCOL:// using protocol v2: request bundle-uris" '
+ test_when_finished "rm -rf log* cloned*" &&
+
+ GIT_TRACE_PACKET="$PWD/log" \
+ git \
+ -c transfer.bundleURI=false \
+ -c protocol.version=2 \
+ clone "$BUNDLE_URI_REPO_URI" cloned \
+ >actual 2>err &&
+
+ # Server responded using protocol v2
+ grep "< version 2" log &&
+
+ # Server advertised bundle-uri capability
+ grep "< bundle-uri" log &&
+
+ # Client did not issue bundle-uri command
+ ! grep "> command=bundle-uri" log &&
+
+ GIT_TRACE_PACKET="$PWD/log" \
+ git \
+ -c transfer.bundleURI=true \
+ -c protocol.version=2 \
+ clone "$BUNDLE_URI_REPO_URI" cloned2 \
+ >actual 2>err &&
+
+ # Server responded using protocol v2
+ grep "< version 2" log &&
+
+ # Server advertised bundle-uri capability
+ grep "< bundle-uri" log &&
+
+ # Client issued bundle-uri command
+ grep "> command=bundle-uri" log &&
+
+ GIT_TRACE_PACKET="$PWD/log3" \
+ git \
+ -c transfer.bundleURI=true \
+ -c protocol.version=2 \
+ clone --bundle-uri="$BUNDLE_URI_BUNDLE_URI" \
+ "$BUNDLE_URI_REPO_URI" cloned3 \
+ >actual 2>err &&
+
+ # Server responded using protocol v2
+ grep "< version 2" log3 &&
+
+ # Server advertised bundle-uri capability
+ grep "< bundle-uri" log3 &&
+
+ # Client did not issue bundle-uri command (--bundle-uri override)
+ ! grep "> command=bundle-uri" log3
+'
+
+# The remaining tests will all assume transfer.bundleURI=true
+#
+# This test can be removed when transfer.bundleURI is enabled by default.
+test_expect_success 'enable transfer.bundleURI for remaining tests' '
+ git config --global transfer.bundleURI true
+'
+
+test_expect_success "test bundle-uri with $BUNDLE_URI_PROTOCOL:// using protocol v2" '
+ test_config -C "$BUNDLE_URI_PARENT" \
+ bundle.only.uri "$BUNDLE_URI_BUNDLE_URI_ESCAPED" &&
+
+ # All data about bundle URIs
+ cat >expect <<-EOF &&
+ [bundle]
+ version = 1
+ mode = all
+ [bundle "only"]
+ uri = $BUNDLE_URI_BUNDLE_URI_ESCAPED
+ EOF
+
+ test-tool bundle-uri \
+ ls-remote \
+ "$BUNDLE_URI_REPO_URI" \
+ >actual &&
+ test_cmp_config_output expect actual
+'
+
+test_expect_success "test bundle-uri with $BUNDLE_URI_PROTOCOL:// using protocol v2 and extra data" '
+ test_config -C "$BUNDLE_URI_PARENT" \
+ bundle.only.uri "$BUNDLE_URI_BUNDLE_URI_ESCAPED" &&
+
+ # Extra data should be ignored
+ test_config -C "$BUNDLE_URI_PARENT" bundle.only.extra bogus &&
+
+ # All data about bundle URIs
+ cat >expect <<-EOF &&
+ [bundle]
+ version = 1
+ mode = all
+ [bundle "only"]
+ uri = $BUNDLE_URI_BUNDLE_URI_ESCAPED
+ EOF
+
+ test-tool bundle-uri \
+ ls-remote \
+ "$BUNDLE_URI_REPO_URI" \
+ >actual &&
+ test_cmp_config_output expect actual
+'
+
+test_expect_success "test bundle-uri with $BUNDLE_URI_PROTOCOL:// using protocol v2 with list" '
+ test_config -C "$BUNDLE_URI_PARENT" \
+ bundle.bundle1.uri "$BUNDLE_URI_BUNDLE_URI_ESCAPED-1.bdl" &&
+ test_config -C "$BUNDLE_URI_PARENT" \
+ bundle.bundle2.uri "$BUNDLE_URI_BUNDLE_URI_ESCAPED-2.bdl" &&
+ test_config -C "$BUNDLE_URI_PARENT" \
+ bundle.bundle3.uri "$BUNDLE_URI_BUNDLE_URI_ESCAPED-3.bdl" &&
+
+ # All data about bundle URIs
+ cat >expect <<-EOF &&
+ [bundle]
+ version = 1
+ mode = all
+ [bundle "bundle1"]
+ uri = $BUNDLE_URI_BUNDLE_URI_ESCAPED-1.bdl
+ [bundle "bundle2"]
+ uri = $BUNDLE_URI_BUNDLE_URI_ESCAPED-2.bdl
+ [bundle "bundle3"]
+ uri = $BUNDLE_URI_BUNDLE_URI_ESCAPED-3.bdl
+ EOF
+
+ test-tool bundle-uri \
+ ls-remote \
+ "$BUNDLE_URI_REPO_URI" \
+ >actual &&
+ test_cmp_config_output expect actual
+'
diff --git a/t/lib-bundle.sh b/t/lib-bundle.sh
new file mode 100644
index 0000000..cf7ed81
--- /dev/null
+++ b/t/lib-bundle.sh
@@ -0,0 +1,42 @@
+# Library of git-bundle related functions.
+
+# Display the pack data contained in the bundle file, bypassing the
+# header that contains the signature, prerequisites and references.
+convert_bundle_to_pack () {
+ while read x && test -n "$x"
+ do
+ :;
+ done
+ cat
+}
+
+# Check count of objects in a bundle file.
+# We can use "--thin" opiton to check thin pack, which must be fixed by
+# command `git-index-pack --fix-thin --stdin`.
+test_bundle_object_count () {
+ thin=
+ if test "$1" = "--thin"
+ then
+ thin=t
+ shift
+ fi
+ if test $# -ne 2
+ then
+ echo >&2 "args should be: <bundle> <count>"
+ return 1
+ fi
+ bundle=$1
+ pack=$bundle.pack
+ convert_bundle_to_pack <"$bundle" >"$pack" &&
+ if test -n "$thin"
+ then
+ mv "$pack" "$bundle.thin.pack" &&
+ git index-pack --stdin --fix-thin "$pack" <"$bundle.thin.pack"
+ else
+ git index-pack "$pack"
+ fi || return 1
+ count=$(git show-index <"${pack%pack}idx" | wc -l) &&
+ test $2 = $count && return 0
+ echo >&2 "error: object count for $bundle is $count, not $2"
+ return 1
+}
diff --git a/t/lib-chunk.sh b/t/lib-chunk.sh
new file mode 100644
index 0000000..a7cd9c3
--- /dev/null
+++ b/t/lib-chunk.sh
@@ -0,0 +1,17 @@
+# Shell library for working with "chunk" files (commit-graph, midx, etc).
+
+# corrupt_chunk_file <fn> <chunk> <offset> <bytes>
+#
+# Corrupt a chunk-based file (like a commit-graph) by overwriting the bytes
+# found in the chunk specified by the 4-byte <chunk> identifier. If <offset> is
+# "clear", replace the chunk entirely. Otherwise, overwrite data <offset> bytes
+# into the chunk.
+#
+# The <bytes> are interpreted as pairs of hex digits (so "000000FE" would be
+# big-endian 254).
+corrupt_chunk_file () {
+ fn=$1; shift
+ perl "$TEST_DIRECTORY"/lib-chunk/corrupt-chunk-file.pl \
+ "$@" <"$fn" >"$fn.tmp" &&
+ mv "$fn.tmp" "$fn"
+}
diff --git a/t/lib-chunk/corrupt-chunk-file.pl b/t/lib-chunk/corrupt-chunk-file.pl
new file mode 100644
index 0000000..0e11aad
--- /dev/null
+++ b/t/lib-chunk/corrupt-chunk-file.pl
@@ -0,0 +1,90 @@
+#!/usr/bin/perl
+
+my ($chunk, $seek, $bytes) = @ARGV;
+$bytes =~ s/../chr(hex($&))/ge;
+
+binmode STDIN;
+binmode STDOUT;
+
+# A few helpers to read bytes, or read and copy them to the
+# output.
+sub get {
+ my $n = shift;
+ return unless $n;
+ read(STDIN, my $buf, $n)
+ or die "read error or eof: $!\n";
+ return $buf;
+}
+sub copy {
+ my $buf = get(@_);
+ print $buf;
+ return $buf;
+}
+
+# Some platforms' perl builds don't support 64-bit integers, and hence do not
+# allow packing/unpacking quadwords with "Q". The chunk format uses 64-bit file
+# offsets to support files of any size, but in practice our test suite will
+# only use small files. So we can fake it by asking for two 32-bit values and
+# discarding the first (most significant) one, which is equivalent as long as
+# it's just zero.
+sub unpack_quad {
+ my $bytes = shift;
+ my ($n1, $n2) = unpack("NN", $bytes);
+ die "quad value exceeds 32 bits" if $n1;
+ return $n2;
+}
+sub pack_quad {
+ my $n = shift;
+ my $ret = pack("NN", 0, $n);
+ # double check that our original $n did not exceed the 32-bit limit.
+ # This is presumably impossible on a 32-bit system (which would have
+ # truncated much earlier), but would still alert us on a 64-bit build
+ # of a new test that would fail on a 32-bit build (though we'd
+ # presumably see the die() from unpack_quad() in such a case).
+ die "quad round-trip failed" if unpack_quad($ret) != $n;
+ return $ret;
+}
+
+# read until we find table-of-contents entry for chunk;
+# note that we cheat a bit by assuming 4-byte alignment and
+# that no ToC entry will accidentally look like a header.
+#
+# If we don't find the entry, copy() will hit EOF and exit
+# (which should cause the caller to fail the test).
+while (copy(4) ne $chunk) { }
+my $offset = unpack_quad(copy(8));
+
+# In clear mode, our length will change. So figure out
+# the length by comparing to the offset of the next chunk, and
+# then adjust that offset (and all subsequent) ones.
+my $len;
+if ($seek eq "clear") {
+ my $id;
+ do {
+ $id = copy(4);
+ my $next = unpack_quad(get(8));
+ if (!defined $len) {
+ $len = $next - $offset;
+ }
+ print pack_quad($next - $len + length($bytes));
+ } while (unpack("N", $id));
+}
+
+# and now copy up to our existing chunk data
+copy($offset - tell(STDIN));
+if ($seek eq "clear") {
+ # if clearing, skip past existing data
+ get($len);
+} else {
+ # otherwise, copy up to the requested offset,
+ # and skip past the overwritten bytes
+ copy($seek);
+ get(length($bytes));
+}
+
+# now write out the requested bytes, along
+# with any other remaining data
+print $bytes;
+while (read(STDIN, my $buf, 4096)) {
+ print $buf;
+}
diff --git a/t/lib-commit-graph.sh b/t/lib-commit-graph.sh
new file mode 100755
index 0000000..89b2667
--- /dev/null
+++ b/t/lib-commit-graph.sh
@@ -0,0 +1,74 @@
+#!/bin/sh
+
+# Helper functions for testing commit-graphs.
+
+# Initialize OID cache with oid_version
+test_oid_cache <<-EOF
+oid_version sha1:1
+oid_version sha256:2
+EOF
+
+graph_git_two_modes() {
+ git -c core.commitGraph=true $1 >output &&
+ git -c core.commitGraph=false $1 >expect &&
+ test_cmp expect output
+}
+
+# graph_git_behavior <name> <directory> <branch> <compare>
+#
+# Ensures that a handful of traversal operations produce the same
+# results with and without the commit-graph in use.
+#
+# NOTE: it is a bug to call this function with <directory> containing
+# any characters in $IFS.
+graph_git_behavior() {
+ MSG=$1
+ DIR=$2
+ BRANCH=$3
+ COMPARE=$4
+ test_expect_success "check normal git operations: $MSG" '
+ graph_git_two_modes "${DIR:+-C $DIR} log --oneline $BRANCH" &&
+ graph_git_two_modes "${DIR:+-C $DIR} log --topo-order $BRANCH" &&
+ graph_git_two_modes "${DIR:+-C $DIR} log --graph $COMPARE..$BRANCH" &&
+ graph_git_two_modes "${DIR:+-C $DIR} branch -vv" &&
+ graph_git_two_modes "${DIR:+-C $DIR} merge-base -a $BRANCH $COMPARE"
+ '
+}
+
+graph_read_expect() {
+ OPTIONAL=""
+ NUM_CHUNKS=3
+ DIR="."
+ if test "$1" = -C
+ then
+ shift
+ DIR="$1"
+ shift
+ fi
+ if test -n "$2"
+ then
+ OPTIONAL=" $2"
+ NUM_CHUNKS=$((3 + $(echo "$2" | wc -w)))
+ fi
+ GENERATION_VERSION=2
+ if test -n "$3"
+ then
+ GENERATION_VERSION=$3
+ fi
+ OPTIONS=
+ if test $GENERATION_VERSION -gt 1
+ then
+ OPTIONS=" read_generation_data"
+ fi
+ cat >"$DIR/expect" <<-EOF
+ header: 43475048 1 $(test_oid oid_version) $NUM_CHUNKS 0
+ num_commits: $1
+ chunks: oid_fanout oid_lookup commit_metadata$OPTIONAL
+ options:$OPTIONS
+ EOF
+ (
+ cd "$DIR" &&
+ test-tool read-graph >output &&
+ test_cmp expect output
+ )
+}
diff --git a/t/lib-credential.sh b/t/lib-credential.sh
new file mode 100644
index 0000000..15fc9a3
--- /dev/null
+++ b/t/lib-credential.sh
@@ -0,0 +1,546 @@
+# Shell library for testing credential handling including helpers. See t0302
+# for an example of testing a specific helper.
+
+# Try a set of credential helpers; the expected stdin,
+# stdout and stderr should be provided on stdin,
+# separated by "--".
+check() {
+ credential_opts=
+ credential_cmd=$1
+ shift
+ for arg in "$@"; do
+ credential_opts="$credential_opts -c credential.helper='$arg'"
+ done
+ read_chunk >stdin &&
+ read_chunk >expect-stdout &&
+ read_chunk >expect-stderr &&
+ if ! eval "git $credential_opts credential $credential_cmd <stdin >stdout 2>stderr"; then
+ echo "git credential failed with code $?" &&
+ cat stderr &&
+ false
+ fi &&
+ test_cmp expect-stdout stdout &&
+ test_cmp expect-stderr stderr
+}
+
+read_chunk() {
+ while read line; do
+ case "$line" in
+ --) break ;;
+ *) echo "$line" ;;
+ esac
+ done
+}
+
+# Clear any residual data from previous tests. We only
+# need this when testing third-party helpers which read and
+# write outside of our trash-directory sandbox.
+#
+# Don't bother checking for success here, as it is
+# outside the scope of tests and represents a best effort to
+# clean up after ourselves.
+helper_test_clean() {
+ reject $1 https example.com store-user
+ reject $1 https example.com user1
+ reject $1 https example.com user2
+ reject $1 https example.com user-expiry
+ reject $1 https example.com user-expiry-overwrite
+ reject $1 https example.com user4
+ reject $1 https example.com user-distinct-pass
+ reject $1 https example.com user-overwrite
+ reject $1 https example.com user-erase1
+ reject $1 https example.com user-erase2
+ reject $1 http path.tld user
+ reject $1 https timeout.tld user
+ reject $1 https sso.tld
+}
+
+reject() {
+ (
+ echo protocol=$2
+ echo host=$3
+ echo username=$4
+ ) | git -c credential.helper=$1 credential reject
+}
+
+helper_test() {
+ HELPER=$1
+
+ test_expect_success "helper ($HELPER) has no existing data" '
+ check fill $HELPER <<-\EOF
+ protocol=https
+ host=example.com
+ --
+ protocol=https
+ host=example.com
+ username=askpass-username
+ password=askpass-password
+ --
+ askpass: Username for '\''https://example.com'\'':
+ askpass: Password for '\''https://askpass-username@example.com'\'':
+ EOF
+ '
+
+ test_expect_success "helper ($HELPER) stores password" '
+ check approve $HELPER <<-\EOF
+ protocol=https
+ host=example.com
+ username=store-user
+ password=store-pass
+ EOF
+ '
+
+ test_expect_success "helper ($HELPER) can retrieve password" '
+ check fill $HELPER <<-\EOF
+ protocol=https
+ host=example.com
+ --
+ protocol=https
+ host=example.com
+ username=store-user
+ password=store-pass
+ --
+ EOF
+ '
+
+ test_expect_success "helper ($HELPER) requires matching protocol" '
+ check fill $HELPER <<-\EOF
+ protocol=http
+ host=example.com
+ --
+ protocol=http
+ host=example.com
+ username=askpass-username
+ password=askpass-password
+ --
+ askpass: Username for '\''http://example.com'\'':
+ askpass: Password for '\''http://askpass-username@example.com'\'':
+ EOF
+ '
+
+ test_expect_success "helper ($HELPER) requires matching host" '
+ check fill $HELPER <<-\EOF
+ protocol=https
+ host=other.tld
+ --
+ protocol=https
+ host=other.tld
+ username=askpass-username
+ password=askpass-password
+ --
+ askpass: Username for '\''https://other.tld'\'':
+ askpass: Password for '\''https://askpass-username@other.tld'\'':
+ EOF
+ '
+
+ test_expect_success "helper ($HELPER) requires matching username" '
+ check fill $HELPER <<-\EOF
+ protocol=https
+ host=example.com
+ username=other
+ --
+ protocol=https
+ host=example.com
+ username=other
+ password=askpass-password
+ --
+ askpass: Password for '\''https://other@example.com'\'':
+ EOF
+ '
+
+ test_expect_success "helper ($HELPER) requires matching path" '
+ test_config credential.usehttppath true &&
+ check approve $HELPER <<-\EOF &&
+ protocol=http
+ host=path.tld
+ path=foo.git
+ username=user
+ password=pass
+ EOF
+ check fill $HELPER <<-\EOF
+ protocol=http
+ host=path.tld
+ path=bar.git
+ --
+ protocol=http
+ host=path.tld
+ path=bar.git
+ username=askpass-username
+ password=askpass-password
+ --
+ askpass: Username for '\''http://path.tld/bar.git'\'':
+ askpass: Password for '\''http://askpass-username@path.tld/bar.git'\'':
+ EOF
+ '
+
+ test_expect_success "helper ($HELPER) overwrites on store" '
+ check approve $HELPER <<-\EOF &&
+ protocol=https
+ host=example.com
+ username=user-overwrite
+ password=pass1
+ EOF
+ check approve $HELPER <<-\EOF &&
+ protocol=https
+ host=example.com
+ username=user-overwrite
+ password=pass2
+ EOF
+ check fill $HELPER <<-\EOF &&
+ protocol=https
+ host=example.com
+ username=user-overwrite
+ --
+ protocol=https
+ host=example.com
+ username=user-overwrite
+ password=pass2
+ EOF
+ check reject $HELPER <<-\EOF &&
+ protocol=https
+ host=example.com
+ username=user-overwrite
+ password=pass2
+ EOF
+ check fill $HELPER <<-\EOF
+ protocol=https
+ host=example.com
+ username=user-overwrite
+ --
+ protocol=https
+ host=example.com
+ username=user-overwrite
+ password=askpass-password
+ --
+ askpass: Password for '\''https://user-overwrite@example.com'\'':
+ EOF
+ '
+
+ test_expect_success "helper ($HELPER) can forget host" '
+ check reject $HELPER <<-\EOF &&
+ protocol=https
+ host=example.com
+ EOF
+ check fill $HELPER <<-\EOF
+ protocol=https
+ host=example.com
+ --
+ protocol=https
+ host=example.com
+ username=askpass-username
+ password=askpass-password
+ --
+ askpass: Username for '\''https://example.com'\'':
+ askpass: Password for '\''https://askpass-username@example.com'\'':
+ EOF
+ '
+
+ test_expect_success "helper ($HELPER) can store multiple users" '
+ check approve $HELPER <<-\EOF &&
+ protocol=https
+ host=example.com
+ username=user1
+ password=pass1
+ EOF
+ check approve $HELPER <<-\EOF &&
+ protocol=https
+ host=example.com
+ username=user2
+ password=pass2
+ EOF
+ check fill $HELPER <<-\EOF &&
+ protocol=https
+ host=example.com
+ username=user1
+ --
+ protocol=https
+ host=example.com
+ username=user1
+ password=pass1
+ EOF
+ check fill $HELPER <<-\EOF
+ protocol=https
+ host=example.com
+ username=user2
+ --
+ protocol=https
+ host=example.com
+ username=user2
+ password=pass2
+ EOF
+ '
+
+ test_expect_success "helper ($HELPER) does not erase a password distinct from input" '
+ check approve $HELPER <<-\EOF &&
+ protocol=https
+ host=example.com
+ username=user-distinct-pass
+ password=pass1
+ EOF
+ check reject $HELPER <<-\EOF &&
+ protocol=https
+ host=example.com
+ username=user-distinct-pass
+ password=pass2
+ EOF
+ check fill $HELPER <<-\EOF
+ protocol=https
+ host=example.com
+ username=user-distinct-pass
+ --
+ protocol=https
+ host=example.com
+ username=user-distinct-pass
+ password=pass1
+ EOF
+ '
+
+ test_expect_success "helper ($HELPER) can forget user" '
+ check reject $HELPER <<-\EOF &&
+ protocol=https
+ host=example.com
+ username=user1
+ EOF
+ check fill $HELPER <<-\EOF
+ protocol=https
+ host=example.com
+ username=user1
+ --
+ protocol=https
+ host=example.com
+ username=user1
+ password=askpass-password
+ --
+ askpass: Password for '\''https://user1@example.com'\'':
+ EOF
+ '
+
+ test_expect_success "helper ($HELPER) remembers other user" '
+ check fill $HELPER <<-\EOF
+ protocol=https
+ host=example.com
+ username=user2
+ --
+ protocol=https
+ host=example.com
+ username=user2
+ password=pass2
+ EOF
+ '
+
+ test_expect_success "helper ($HELPER) can store empty username" '
+ check approve $HELPER <<-\EOF &&
+ protocol=https
+ host=sso.tld
+ username=
+ password=
+ EOF
+ check fill $HELPER <<-\EOF
+ protocol=https
+ host=sso.tld
+ --
+ protocol=https
+ host=sso.tld
+ username=
+ password=
+ EOF
+ '
+
+ test_expect_success "helper ($HELPER) erases all matching credentials" '
+ check approve $HELPER <<-\EOF &&
+ protocol=https
+ host=example.com
+ username=user-erase1
+ password=pass1
+ EOF
+ check approve $HELPER <<-\EOF &&
+ protocol=https
+ host=example.com
+ username=user-erase2
+ password=pass1
+ EOF
+ check reject $HELPER <<-\EOF &&
+ protocol=https
+ host=example.com
+ EOF
+ check fill $HELPER <<-\EOF
+ protocol=https
+ host=example.com
+ --
+ protocol=https
+ host=example.com
+ username=askpass-username
+ password=askpass-password
+ --
+ askpass: Username for '\''https://example.com'\'':
+ askpass: Password for '\''https://askpass-username@example.com'\'':
+ EOF
+ '
+
+ : ${GIT_TEST_LONG_CRED_BUFFER:=1024}
+ # 23 bytes accounts for "wwwauth[]=basic realm=" plus NUL
+ LONG_VALUE_LEN=$((GIT_TEST_LONG_CRED_BUFFER - 23))
+ LONG_VALUE=$(perl -e 'print "a" x shift' $LONG_VALUE_LEN)
+
+ test_expect_success "helper ($HELPER) not confused by long header" '
+ check approve $HELPER <<-\EOF &&
+ protocol=https
+ host=victim.example.com
+ username=user
+ password=to-be-stolen
+ EOF
+
+ check fill $HELPER <<-EOF
+ protocol=https
+ host=badguy.example.com
+ wwwauth[]=basic realm=${LONG_VALUE}host=victim.example.com
+ --
+ protocol=https
+ host=badguy.example.com
+ username=askpass-username
+ password=askpass-password
+ wwwauth[]=basic realm=${LONG_VALUE}host=victim.example.com
+ --
+ askpass: Username for '\''https://badguy.example.com'\'':
+ askpass: Password for '\''https://askpass-username@badguy.example.com'\'':
+ EOF
+ '
+}
+
+helper_test_timeout() {
+ HELPER="$*"
+
+ test_expect_success "helper ($HELPER) times out" '
+ check approve "$HELPER" <<-\EOF &&
+ protocol=https
+ host=timeout.tld
+ username=user
+ password=pass
+ EOF
+ sleep 2 &&
+ check fill "$HELPER" <<-\EOF
+ protocol=https
+ host=timeout.tld
+ --
+ protocol=https
+ host=timeout.tld
+ username=askpass-username
+ password=askpass-password
+ --
+ askpass: Username for '\''https://timeout.tld'\'':
+ askpass: Password for '\''https://askpass-username@timeout.tld'\'':
+ EOF
+ '
+}
+
+helper_test_password_expiry_utc() {
+ HELPER=$1
+
+ test_expect_success "helper ($HELPER) stores password_expiry_utc" '
+ check approve $HELPER <<-\EOF
+ protocol=https
+ host=example.com
+ username=user-expiry
+ password=pass
+ password_expiry_utc=9999999999
+ EOF
+ '
+
+ test_expect_success "helper ($HELPER) gets password_expiry_utc" '
+ check fill $HELPER <<-\EOF
+ protocol=https
+ host=example.com
+ username=user-expiry
+ --
+ protocol=https
+ host=example.com
+ username=user-expiry
+ password=pass
+ password_expiry_utc=9999999999
+ --
+ EOF
+ '
+
+ test_expect_success "helper ($HELPER) overwrites when password_expiry_utc changes" '
+ check approve $HELPER <<-\EOF &&
+ protocol=https
+ host=example.com
+ username=user-expiry-overwrite
+ password=pass1
+ password_expiry_utc=9999999998
+ EOF
+ check approve $HELPER <<-\EOF &&
+ protocol=https
+ host=example.com
+ username=user-expiry-overwrite
+ password=pass2
+ password_expiry_utc=9999999999
+ EOF
+ check fill $HELPER <<-\EOF &&
+ protocol=https
+ host=example.com
+ username=user-expiry-overwrite
+ --
+ protocol=https
+ host=example.com
+ username=user-expiry-overwrite
+ password=pass2
+ password_expiry_utc=9999999999
+ EOF
+ check reject $HELPER <<-\EOF &&
+ protocol=https
+ host=example.com
+ username=user-expiry-overwrite
+ password=pass2
+ EOF
+ check fill $HELPER <<-\EOF
+ protocol=https
+ host=example.com
+ username=user-expiry-overwrite
+ --
+ protocol=https
+ host=example.com
+ username=user-expiry-overwrite
+ password=askpass-password
+ --
+ askpass: Password for '\''https://user-expiry-overwrite@example.com'\'':
+ EOF
+ '
+}
+
+helper_test_oauth_refresh_token() {
+ HELPER=$1
+
+ test_expect_success "helper ($HELPER) stores oauth_refresh_token" '
+ check approve $HELPER <<-\EOF
+ protocol=https
+ host=example.com
+ username=user4
+ password=pass
+ oauth_refresh_token=xyzzy
+ EOF
+ '
+
+ test_expect_success "helper ($HELPER) gets oauth_refresh_token" '
+ check fill $HELPER <<-\EOF
+ protocol=https
+ host=example.com
+ username=user4
+ --
+ protocol=https
+ host=example.com
+ username=user4
+ password=pass
+ oauth_refresh_token=xyzzy
+ --
+ EOF
+ '
+}
+
+write_script askpass <<\EOF
+echo >&2 askpass: $*
+what=$(echo $1 | cut -d" " -f1 | tr A-Z a-z | tr -cd a-z)
+echo "askpass-$what"
+EOF
+GIT_ASKPASS="$PWD/askpass"
+export GIT_ASKPASS
diff --git a/t/lib-cvs.sh b/t/lib-cvs.sh
new file mode 100644
index 0000000..32b3473
--- /dev/null
+++ b/t/lib-cvs.sh
@@ -0,0 +1,78 @@
+# Shell library sourced instead of ./test-lib.sh by cvsimport tests.
+
+. ./test-lib.sh
+
+unset CVS_SERVER
+
+if ! type cvs >/dev/null 2>&1
+then
+ skip_all='skipping cvsimport tests, cvs not found'
+ test_done
+fi
+
+CVS="cvs -f"
+export CVS
+
+cvsps_version=$(cvsps -h 2>&1 | sed -ne 's/cvsps version //p')
+case "$cvsps_version" in
+2.1 | 2.2*)
+ ;;
+'')
+ skip_all='skipping cvsimport tests, cvsps not found'
+ test_done
+ ;;
+*)
+ skip_all='skipping cvsimport tests, unsupported cvsps version'
+ test_done
+ ;;
+esac
+
+setup_cvs_test_repository () {
+ CVSROOT="$(pwd)/.cvsroot" &&
+ cp -r "$TEST_DIRECTORY/$1/cvsroot" "$CVSROOT" &&
+ export CVSROOT
+}
+
+test_cvs_co () {
+ # Usage: test_cvs_co BRANCH_NAME
+ rm -rf module-cvs-"$1"
+ if [ "$1" = "main" ]
+ then
+ $CVS co -P -d module-cvs-"$1" -A module
+ else
+ $CVS co -P -d module-cvs-"$1" -r "$1" module
+ fi
+}
+
+test_git_co () {
+ # Usage: test_git_co BRANCH_NAME
+ (cd module-git && git checkout "$1")
+}
+
+test_cmp_branch_file () {
+ # Usage: test_cmp_branch_file BRANCH_NAME PATH
+ # The branch must already be checked out of CVS and git.
+ test_cmp module-cvs-"$1"/"$2" module-git/"$2"
+}
+
+test_cmp_branch_tree () {
+ # Usage: test_cmp_branch_tree BRANCH_NAME
+ # Check BRANCH_NAME out of CVS and git and make sure that all
+ # of the files and directories are identical.
+
+ test_cvs_co "$1" &&
+ test_git_co "$1" &&
+ (
+ cd module-cvs-"$1"
+ find . -type d -name CVS -prune -o -type f -print
+ ) | sort >module-cvs-"$1".list &&
+ (
+ cd module-git
+ find . -type d -name .git -prune -o -type f -print
+ ) | sort >module-git-"$1".list &&
+ test_cmp module-cvs-"$1".list module-git-"$1".list &&
+ cat module-cvs-"$1".list | while read f
+ do
+ test_cmp_branch_file "$1" "$f" || return 1
+ done
+}
diff --git a/t/lib-diff-alternative.sh b/t/lib-diff-alternative.sh
new file mode 100644
index 0000000..c4dc2d4
--- /dev/null
+++ b/t/lib-diff-alternative.sh
@@ -0,0 +1,227 @@
+# Helpers shared by the test scripts for diff algorithms (patience,
+# histogram, etc).
+
+test_diff_frobnitz() {
+ cat >file1 <<\EOF
+#include <stdio.h>
+
+// Frobs foo heartily
+int frobnitz(int foo)
+{
+ int i;
+ for(i = 0; i < 10; i++)
+ {
+ printf("Your answer is: ");
+ printf("%d\n", foo);
+ }
+}
+
+int fact(int n)
+{
+ if(n > 1)
+ {
+ return fact(n-1) * n;
+ }
+ return 1;
+}
+
+int main(int argc, char **argv)
+{
+ frobnitz(fact(10));
+}
+EOF
+
+ cat >file2 <<\EOF
+#include <stdio.h>
+
+int fib(int n)
+{
+ if(n > 2)
+ {
+ return fib(n-1) + fib(n-2);
+ }
+ return 1;
+}
+
+// Frobs foo heartily
+int frobnitz(int foo)
+{
+ int i;
+ for(i = 0; i < 10; i++)
+ {
+ printf("%d\n", foo);
+ }
+}
+
+int main(int argc, char **argv)
+{
+ frobnitz(fib(10));
+}
+EOF
+
+ file1=$(git rev-parse --short $(git hash-object file1))
+ file2=$(git rev-parse --short $(git hash-object file2))
+ cat >expect <<EOF
+diff --git a/file1 b/file2
+index $file1..$file2 100644
+--- a/file1
++++ b/file2
+@@ -1,26 +1,25 @@
+ #include <stdio.h>
+
++int fib(int n)
++{
++ if(n > 2)
++ {
++ return fib(n-1) + fib(n-2);
++ }
++ return 1;
++}
++
+ // Frobs foo heartily
+ int frobnitz(int foo)
+ {
+ int i;
+ for(i = 0; i < 10; i++)
+ {
+- printf("Your answer is: ");
+ printf("%d\n", foo);
+ }
+ }
+
+-int fact(int n)
+-{
+- if(n > 1)
+- {
+- return fact(n-1) * n;
+- }
+- return 1;
+-}
+-
+ int main(int argc, char **argv)
+ {
+- frobnitz(fact(10));
++ frobnitz(fib(10));
+ }
+EOF
+
+ cat >expect_diffstat <<EOF
+ file1 => file2 | 21 ++++++++++-----------
+ 1 file changed, 10 insertions(+), 11 deletions(-)
+EOF
+
+ STRATEGY=$1
+
+ test_expect_success "setup attributes files for tests with $STRATEGY" '
+ git checkout -b master &&
+ echo "file* diff=driver" >.gitattributes &&
+ git add file1 file2 .gitattributes &&
+ git commit -m "adding files" &&
+ git checkout -b branchA &&
+ echo "file* diff=driverA" >.gitattributes &&
+ git add .gitattributes &&
+ git commit -m "adding driverA as diff driver" &&
+ git checkout master &&
+ git clone --bare --no-local . bare.git
+ '
+
+ test_expect_success "$STRATEGY diff from attributes" '
+ test_must_fail git -c diff.driver.algorithm=$STRATEGY diff --no-index file1 file2 > output &&
+ test_cmp expect output
+ '
+
+ test_expect_success "diff from attributes with bare repo with source" '
+ git -C bare.git --attr-source=branchA -c diff.driver.algorithm=myers \
+ -c diff.driverA.algorithm=$STRATEGY \
+ diff HEAD:file1 HEAD:file2 >output &&
+ test_cmp expect output
+ '
+
+ test_expect_success "diff from attributes with bare repo with invalid source" '
+ test_must_fail git -C bare.git --attr-source=invalid-branch diff \
+ HEAD:file1 HEAD:file2
+ '
+
+ test_expect_success "$STRATEGY diff from attributes has valid diffstat" '
+ echo "file* diff=driver" >.gitattributes &&
+ git config diff.driver.algorithm "$STRATEGY" &&
+ test_must_fail git diff --stat --no-index file1 file2 > output &&
+ test_cmp expect_diffstat output
+ '
+
+ test_expect_success "$STRATEGY diff" '
+ test_must_fail git diff --no-index "--diff-algorithm=$STRATEGY" file1 file2 > output &&
+ test_cmp expect output
+ '
+
+ test_expect_success "$STRATEGY diff command line precedence before attributes" '
+ echo "file* diff=driver" >.gitattributes &&
+ git config diff.driver.algorithm myers &&
+ test_must_fail git diff --no-index "--diff-algorithm=$STRATEGY" file1 file2 > output &&
+ test_cmp expect output
+ '
+
+ test_expect_success "$STRATEGY diff attributes precedence before config" '
+ git config diff.algorithm default &&
+ echo "file* diff=driver" >.gitattributes &&
+ git config diff.driver.algorithm "$STRATEGY" &&
+ test_must_fail git diff --no-index file1 file2 > output &&
+ test_cmp expect output
+ '
+
+ test_expect_success "$STRATEGY diff output is valid" '
+ mv file2 expect &&
+ git apply < output &&
+ test_cmp expect file2
+ '
+}
+
+test_diff_unique() {
+ cat >uniq1 <<\EOF
+1
+2
+3
+4
+5
+6
+EOF
+
+ cat >uniq2 <<\EOF
+a
+b
+c
+d
+e
+f
+EOF
+
+ uniq1=$(git rev-parse --short $(git hash-object uniq1))
+ uniq2=$(git rev-parse --short $(git hash-object uniq2))
+ cat >expect <<EOF
+diff --git a/uniq1 b/uniq2
+index $uniq1..$uniq2 100644
+--- a/uniq1
++++ b/uniq2
+@@ -1,6 +1,6 @@
+-1
+-2
+-3
+-4
+-5
+-6
++a
++b
++c
++d
++e
++f
+EOF
+
+ STRATEGY=$1
+
+ test_expect_success 'completely different files' '
+ test_must_fail git diff --no-index "--$STRATEGY" uniq1 uniq2 > output &&
+ test_cmp expect output
+ '
+}
+
diff --git a/t/lib-diff-data.sh b/t/lib-diff-data.sh
new file mode 100644
index 0000000..c64ec18
--- /dev/null
+++ b/t/lib-diff-data.sh
@@ -0,0 +1,22 @@
+COPYING_test_data () {
+ cat <<\EOF
+
+ Note that the only valid version of the GPL as far as this project
+ is concerned is _this_ particular version of the license (ie v2, not
+ v2.2 or v3.x or whatever), unless explicitly otherwise stated.
+
+ HOWEVER, in order to allow a migration to GPLv3 if that seems like
+ a good idea, I also ask that people involved with the project make
+ their preferences known. In particular, if you trust me to make that
+ decision, you might note so in your copyright message, ie something
+ like
+
+ This file is licensed under the GPL v2, or a later version
+ at the discretion of Linus.
+
+ might avoid issues. But we can also just decide to synchronize and
+ contact all copyright holders on record if/when the occasion arises.
+
+ Linus Torvalds
+EOF
+}
diff --git a/t/lib-diff.sh b/t/lib-diff.sh
new file mode 100644
index 0000000..c4606bd
--- /dev/null
+++ b/t/lib-diff.sh
@@ -0,0 +1,41 @@
+. "$TEST_DIRECTORY"/lib-diff-data.sh
+
+:
+
+sanitize_diff_raw='/^:/s/ '"\($OID_REGEX\)"' '"\($OID_REGEX\)"' \([A-Z]\)[0-9]* / \1 \2 \3# /'
+compare_diff_raw () {
+ # When heuristics are improved, the score numbers would change.
+ # Ignore them while comparing.
+ # Also we do not check SHA1 hash generation in this test, which
+ # is a job for t0000-basic.sh
+
+ sed -e "$sanitize_diff_raw" <"$1" >.tmp-1
+ sed -e "$sanitize_diff_raw" <"$2" >.tmp-2
+ test_cmp .tmp-1 .tmp-2 && rm -f .tmp-1 .tmp-2
+}
+
+sanitize_diff_raw_z='/^:/s/ '"$OID_REGEX"' '"$OID_REGEX"' \([A-Z]\)[0-9]*$/ X X \1#/'
+compare_diff_raw_z () {
+ # When heuristics are improved, the score numbers would change.
+ # Ignore them while comparing.
+ # Also we do not check SHA1 hash generation in this test, which
+ # is a job for t0000-basic.sh
+
+ perl -pe 'y/\000/\012/' <"$1" | sed -e "$sanitize_diff_raw_z" >.tmp-1
+ perl -pe 'y/\000/\012/' <"$2" | sed -e "$sanitize_diff_raw_z" >.tmp-2
+ test_cmp .tmp-1 .tmp-2 && rm -f .tmp-1 .tmp-2
+}
+
+compare_diff_patch () {
+ # When heuristics are improved, the score numbers would change.
+ # Ignore them while comparing.
+ sed -e '
+ /^[dis]*imilarity index [0-9]*%$/d
+ /^index [0-9a-f]*\.\.[0-9a-f]/d
+ ' <"$1" >.tmp-1
+ sed -e '
+ /^[dis]*imilarity index [0-9]*%$/d
+ /^index [0-9a-f]*\.\.[0-9a-f]/d
+ ' <"$2" >.tmp-2
+ test_cmp .tmp-1 .tmp-2 && rm -f .tmp-1 .tmp-2
+}
diff --git a/t/lib-encoding.sh b/t/lib-encoding.sh
new file mode 100644
index 0000000..2dabc8c
--- /dev/null
+++ b/t/lib-encoding.sh
@@ -0,0 +1,25 @@
+# Encoding helpers
+
+test_lazy_prereq NO_UTF16_BOM '
+ test $(printf abc | iconv -f UTF-8 -t UTF-16 | wc -c) = 6
+'
+
+test_lazy_prereq NO_UTF32_BOM '
+ test $(printf abc | iconv -f UTF-8 -t UTF-32 | wc -c) = 12
+'
+
+write_utf16 () {
+ if test_have_prereq NO_UTF16_BOM
+ then
+ printf '\376\377'
+ fi &&
+ iconv -f UTF-8 -t UTF-16
+}
+
+write_utf32 () {
+ if test_have_prereq NO_UTF32_BOM
+ then
+ printf '\0\0\376\377'
+ fi &&
+ iconv -f UTF-8 -t UTF-32
+}
diff --git a/t/lib-gettext.sh b/t/lib-gettext.sh
new file mode 100644
index 0000000..cc6bb2c
--- /dev/null
+++ b/t/lib-gettext.sh
@@ -0,0 +1,63 @@
+# Initialization and Icelandic locale for basic git i18n tests,
+# which source this scriptlet instead of ./test-lib.sh.
+#
+# Copyright (c) 2010 Ævar Arnfjörð Bjarmason
+#
+
+. ./test-lib.sh
+
+GIT_TEXTDOMAINDIR="$GIT_BUILD_DIR/po/build/locale"
+GIT_PO_PATH="$GIT_BUILD_DIR/po"
+export GIT_TEXTDOMAINDIR GIT_PO_PATH
+
+if test -n "$GIT_TEST_INSTALLED"
+then
+ . "$(git --exec-path)"/git-sh-i18n
+else
+ . "$GIT_BUILD_DIR"/git-sh-i18n
+fi
+
+if test_have_prereq GETTEXT
+then
+ # is_IS.UTF-8 on Solaris and FreeBSD, is_IS.utf8 on Debian
+ is_IS_locale=$(locale -a 2>/dev/null |
+ sed -n '/^is_IS\.[uU][tT][fF]-*8$/{
+ p
+ q
+ }')
+ # is_IS.ISO8859-1 on Solaris and FreeBSD, is_IS.iso88591 on Debian
+ is_IS_iso_locale=$(locale -a 2>/dev/null |
+ sed -n '/^is_IS\.[iI][sS][oO]8859-*1$/{
+ p
+ q
+ }')
+
+ # Export them as an environment variable so the t0202/test.pl Perl
+ # test can use it too
+ export is_IS_locale is_IS_iso_locale
+
+ if test -n "$is_IS_locale" &&
+ test $GIT_INTERNAL_GETTEXT_SH_SCHEME != "fallthrough"
+ then
+ # Some of the tests need the reference Icelandic locale
+ test_set_prereq GETTEXT_LOCALE
+
+ # Exporting for t0202/test.pl
+ GETTEXT_LOCALE=1
+ export GETTEXT_LOCALE
+ say "# lib-gettext: Found '$is_IS_locale' as an is_IS UTF-8 locale"
+ else
+ say "# lib-gettext: No is_IS UTF-8 locale available"
+ fi
+
+ if test -n "$is_IS_iso_locale" &&
+ test $GIT_INTERNAL_GETTEXT_SH_SCHEME != "fallthrough"
+ then
+ # Some of the tests need the reference Icelandic locale
+ test_set_prereq GETTEXT_ISO_LOCALE
+
+ say "# lib-gettext: Found '$is_IS_iso_locale' as an is_IS ISO-8859-1 locale"
+ else
+ say "# lib-gettext: No is_IS ISO-8859-1 locale available"
+ fi
+fi
diff --git a/t/lib-git-daemon.sh b/t/lib-git-daemon.sh
new file mode 100644
index 0000000..e625692
--- /dev/null
+++ b/t/lib-git-daemon.sh
@@ -0,0 +1,120 @@
+# Shell library to run git-daemon in tests. Ends the test early if
+# GIT_TEST_GIT_DAEMON is not set.
+#
+# Usage:
+#
+# . ./test-lib.sh
+# . "$TEST_DIRECTORY"/lib-git-daemon.sh
+# start_git_daemon
+#
+# test_expect_success '...' '
+# ...
+# '
+#
+# test_expect_success ...
+#
+# test_done
+
+if ! test_bool_env GIT_TEST_GIT_DAEMON true
+then
+ skip_all="git-daemon testing disabled (unset GIT_TEST_GIT_DAEMON to enable)"
+ test_done
+fi
+
+if test_have_prereq !PIPE
+then
+ test_skip_or_die GIT_TEST_GIT_DAEMON "file system does not support FIFOs"
+fi
+
+test_set_port LIB_GIT_DAEMON_PORT
+
+GIT_DAEMON_PID=
+GIT_DAEMON_PIDFILE="$PWD"/daemon.pid
+GIT_DAEMON_DOCUMENT_ROOT_PATH="$PWD"/repo
+GIT_DAEMON_HOST_PORT=127.0.0.1:$LIB_GIT_DAEMON_PORT
+GIT_DAEMON_URL=git://$GIT_DAEMON_HOST_PORT
+
+registered_stop_git_daemon_atexit_handler=
+start_git_daemon() {
+ if test -n "$GIT_DAEMON_PID"
+ then
+ error "start_git_daemon already called"
+ fi
+
+ mkdir -p "$GIT_DAEMON_DOCUMENT_ROOT_PATH"
+
+ # One of the test scripts stops and then re-starts 'git daemon'.
+ # Don't register and then run the same atexit handlers several times.
+ if test -z "$registered_stop_git_daemon_atexit_handler"
+ then
+ test_atexit 'stop_git_daemon'
+ registered_stop_git_daemon_atexit_handler=AlreadyDone
+ fi
+
+ say >&3 "Starting git daemon ..."
+ mkfifo git_daemon_output
+ ${LIB_GIT_DAEMON_COMMAND:-git daemon} \
+ --listen=127.0.0.1 --port="$LIB_GIT_DAEMON_PORT" \
+ --reuseaddr --verbose --pid-file="$GIT_DAEMON_PIDFILE" \
+ --base-path="$GIT_DAEMON_DOCUMENT_ROOT_PATH" \
+ "$@" "$GIT_DAEMON_DOCUMENT_ROOT_PATH" \
+ >&3 2>git_daemon_output &
+ GIT_DAEMON_PID=$!
+ {
+ read -r line <&7
+ printf "%s\n" "$line" >&4
+ cat <&7 >&4 &
+ } 7<git_daemon_output &&
+
+ # Check expected output
+ if test x"$(expr "$line" : "\[[0-9]*\] \(.*\)")" != x"Ready to rumble"
+ then
+ kill "$GIT_DAEMON_PID"
+ wait "$GIT_DAEMON_PID"
+ unset GIT_DAEMON_PID
+ test_skip_or_die GIT_TEST_GIT_DAEMON \
+ "git daemon failed to start"
+ fi
+}
+
+stop_git_daemon() {
+ if test -z "$GIT_DAEMON_PID"
+ then
+ return
+ fi
+
+ # kill git-daemon child of git
+ say >&3 "Stopping git daemon ..."
+ kill "$GIT_DAEMON_PID"
+ wait "$GIT_DAEMON_PID" >&3 2>&4
+ ret=$?
+ if ! test_match_signal 15 $ret
+ then
+ error "git daemon exited with status: $ret"
+ fi
+ kill "$(cat "$GIT_DAEMON_PIDFILE")" 2>/dev/null
+ GIT_DAEMON_PID=
+ rm -f git_daemon_output "$GIT_DAEMON_PIDFILE"
+}
+
+# A stripped-down version of a netcat client, that connects to a "host:port"
+# given in $1, sends its stdin followed by EOF, then dumps the response (until
+# EOF) to stdout.
+fake_nc() {
+ if ! test_declared_prereq FAKENC
+ then
+ echo >&4 "fake_nc: need to declare FAKENC prerequisite"
+ return 127
+ fi
+ perl -Mstrict -MIO::Socket::INET -e '
+ my $s = IO::Socket::INET->new(shift)
+ or die "unable to open socket: $!";
+ print $s <STDIN>;
+ $s->shutdown(1);
+ print <$s>;
+ ' "$@"
+}
+
+test_lazy_prereq FAKENC '
+ perl -MIO::Socket::INET -e "exit 0"
+'
diff --git a/t/lib-git-p4.sh b/t/lib-git-p4.sh
new file mode 100644
index 0000000..2a5b873
--- /dev/null
+++ b/t/lib-git-p4.sh
@@ -0,0 +1,225 @@
+#
+# Library code for git p4 tests
+#
+
+# p4 tests never use the top-level repo; always build/clone into
+# a subdirectory called "$git"
+TEST_NO_CREATE_REPO=NoThanks
+
+# Some operations require multiple attempts to be successful. Define
+# here the maximal retry timeout in seconds.
+RETRY_TIMEOUT=60
+
+# Sometimes p4d seems to hang. Terminate the p4d process automatically after
+# the defined timeout in seconds.
+P4D_TIMEOUT=300
+
+. ./test-lib.sh
+
+if ! test_have_prereq PYTHON
+then
+ skip_all='skipping git p4 tests; python not available'
+ test_done
+fi
+( p4 -h && p4d -h ) >/dev/null 2>&1 || {
+ skip_all='skipping git p4 tests; no p4 or p4d'
+ test_done
+}
+
+# On cygwin, the NT version of Perforce can be used. When giving
+# it paths, either on the command-line or in client specifications,
+# be sure to use the native windows form.
+#
+# Older versions of perforce were available compiled natively for
+# cygwin. Those do not accept native windows paths, so make sure
+# not to convert for them.
+native_path () {
+ path="$1" &&
+ if test_have_prereq CYGWIN && ! p4 -V | grep -q CYGWIN
+ then
+ path=$(cygpath --windows "$path")
+ else
+ path=$(test-tool path-utils real_path "$path")
+ fi &&
+ echo "$path"
+}
+
+test_set_port P4DPORT
+
+P4PORT=localhost:$P4DPORT
+P4CLIENT=client
+P4USER=author
+P4EDITOR=true
+unset P4CHARSET
+export P4PORT P4CLIENT P4USER P4EDITOR P4CHARSET
+
+db="$TRASH_DIRECTORY/db"
+cli="$TRASH_DIRECTORY/cli"
+git="$TRASH_DIRECTORY/git"
+pidfile="$TRASH_DIRECTORY/p4d.pid"
+
+stop_p4d_and_watchdog () {
+ kill -9 $p4d_pid $watchdog_pid
+}
+
+# git p4 submit generates a temp file, which will
+# not get cleaned up if the submission fails. Don't
+# clutter up /tmp on the test machine.
+TMPDIR="$TRASH_DIRECTORY"
+export TMPDIR
+
+registered_stop_p4d_atexit_handler=
+start_p4d () {
+ # One of the test scripts stops and then re-starts p4d.
+ # Don't register and then run the same atexit handlers several times.
+ if test -z "$registered_stop_p4d_atexit_handler"
+ then
+ test_atexit 'stop_p4d_and_watchdog'
+ registered_stop_p4d_atexit_handler=AlreadyDone
+ fi
+
+ mkdir -p "$db" "$cli" "$git" &&
+ rm -f "$pidfile" &&
+ (
+ cd "$db" &&
+ {
+ p4d -q -p $P4DPORT "$@" &
+ echo $! >"$pidfile"
+ }
+ ) &&
+ p4d_pid=$(cat "$pidfile")
+
+ # This gives p4d a long time to start up, as it can be
+ # quite slow depending on the machine. Set this environment
+ # variable to something smaller to fail faster in, say,
+ # an automated test setup. If the p4d process dies, that
+ # will be caught with the "kill -0" check below.
+ i=${P4D_START_PATIENCE:-300}
+
+ nr_tries_left=$P4D_TIMEOUT
+ while true
+ do
+ if test $nr_tries_left -eq 0
+ then
+ kill -9 $p4d_pid
+ exit 1
+ fi
+ sleep 1
+ nr_tries_left=$(($nr_tries_left - 1))
+ done 2>/dev/null 4>&2 &
+ watchdog_pid=$!
+
+ ready=
+ while test $i -gt 0
+ do
+ # succeed when p4 client commands start to work
+ if p4 info >/dev/null 2>&1
+ then
+ ready=true
+ break
+ fi
+ # fail if p4d died
+ kill -0 $p4d_pid 2>/dev/null || break
+ echo waiting for p4d to start
+ sleep 1
+ i=$(( $i - 1 ))
+ done
+
+ if test -z "$ready"
+ then
+ # p4d failed to start
+ return 1
+ fi
+
+ # build a p4 user so author@example.com has an entry
+ p4_add_user author
+
+ # build a client
+ client_view "//depot/... //client/..." &&
+
+ return 0
+}
+
+p4_add_user () {
+ name=$1 &&
+ fullname="${2:-Dr. $1}"
+ p4 user -f -i <<-EOF
+ User: $name
+ Email: $name@example.com
+ FullName: $fullname
+ EOF
+}
+
+p4_add_job () {
+ p4 job -f -i <<-EOF
+ Job: $1
+ Status: open
+ User: dummy
+ Description:
+ EOF
+}
+
+retry_until_success () {
+ nr_tries_left=$RETRY_TIMEOUT
+ until "$@" 2>/dev/null || test $nr_tries_left -eq 0
+ do
+ sleep 1
+ nr_tries_left=$(($nr_tries_left - 1))
+ done
+}
+
+stop_and_cleanup_p4d () {
+ kill -9 $p4d_pid $watchdog_pid
+ wait $p4d_pid
+ rm -rf "$db" "$cli" "$pidfile"
+}
+
+cleanup_git () {
+ retry_until_success rm -r "$git"
+ test_path_is_missing "$git" &&
+ retry_until_success mkdir "$git"
+}
+
+marshal_dump () {
+ what=$1 &&
+ line=${2:-1} &&
+ cat >"$TRASH_DIRECTORY/marshal-dump.py" <<-EOF &&
+ import marshal
+ import sys
+ instream = getattr(sys.stdin, 'buffer', sys.stdin)
+ for i in range($line):
+ d = marshal.load(instream)
+ print(d[b'$what'].decode('utf-8'))
+ EOF
+ "$PYTHON_PATH" "$TRASH_DIRECTORY/marshal-dump.py"
+}
+
+#
+# Construct a client with this list of View lines
+#
+client_view () {
+ (
+ cat <<-EOF &&
+ Client: $P4CLIENT
+ Description: $P4CLIENT
+ Root: $cli
+ AltRoots: $(native_path "$cli")
+ LineEnd: unix
+ View:
+ EOF
+ printf "\t%s\n" "$@"
+ ) | p4 client -i
+}
+
+is_cli_file_writeable () {
+ # cygwin version of p4 does not set read-only attr,
+ # will be marked 444 but -w is true
+ file="$1" &&
+ if test_have_prereq CYGWIN && p4 -V | grep -q CYGWIN
+ then
+ stat=$(stat --format=%a "$file") &&
+ test $stat = 644
+ else
+ test -w "$file"
+ fi
+}
diff --git a/t/lib-git-svn.sh b/t/lib-git-svn.sh
new file mode 100644
index 0000000..ea28971
--- /dev/null
+++ b/t/lib-git-svn.sh
@@ -0,0 +1,149 @@
+if test -z "$TEST_FAILS_SANITIZE_LEAK"
+then
+ TEST_PASSES_SANITIZE_LEAK=true
+fi
+. ./test-lib.sh
+
+if test -n "$NO_SVN_TESTS"
+then
+ skip_all='skipping git svn tests, NO_SVN_TESTS defined'
+ test_done
+fi
+if ! test_have_prereq PERL; then
+ skip_all='skipping git svn tests, perl not available'
+ test_done
+fi
+
+GIT_DIR=$PWD/.git
+GIT_SVN_DIR=$GIT_DIR/svn/refs/remotes/git-svn
+SVN_TREE=$GIT_SVN_DIR/svn-tree
+test_set_port SVNSERVE_PORT
+
+svn >/dev/null 2>&1
+if test $? -ne 1
+then
+ skip_all='skipping git svn tests, svn not found'
+ test_done
+fi
+
+svnrepo=$PWD/svnrepo
+export svnrepo
+svnconf=$PWD/svnconf
+export svnconf
+
+perl -w -e "
+use SVN::Core;
+use SVN::Repos;
+\$SVN::Core::VERSION gt '1.1.0' or exit(42);
+system(qw/svnadmin create --fs-type fsfs/, \$ENV{svnrepo}) == 0 or exit(41);
+" >&3 2>&4
+x=$?
+if test $x -ne 0
+then
+ if test $x -eq 42; then
+ skip_all='Perl SVN libraries must be >= 1.1.0'
+ elif test $x -eq 41; then
+ skip_all='svnadmin failed to create fsfs repository'
+ else
+ skip_all='Perl SVN libraries not found or unusable'
+ fi
+ test_done
+fi
+
+rawsvnrepo="$svnrepo"
+svnrepo="file://$svnrepo"
+
+poke() {
+ test-tool chmtime +1 "$1"
+}
+
+# We need this, because we should pass empty configuration directory to
+# the 'svn commit' to avoid automated property changes and other stuff
+# that could be set from user's configuration files in ~/.subversion.
+svn_cmd () {
+ [ -d "$svnconf" ] || mkdir "$svnconf"
+ orig_svncmd="$1"; shift
+ if [ -z "$orig_svncmd" ]; then
+ svn
+ return
+ fi
+ svn "$orig_svncmd" --config-dir "$svnconf" "$@"
+}
+
+maybe_start_httpd () {
+ loc=${1-svn}
+
+ if test_bool_env GIT_TEST_SVN_HTTPD false
+ then
+ . "$TEST_DIRECTORY"/lib-httpd.sh
+ LIB_HTTPD_SVN="$loc"
+ start_httpd
+ fi
+}
+
+convert_to_rev_db () {
+ perl -w -- - "$(test_oid rawsz)" "$@" <<\EOF
+use strict;
+my $oidlen = shift;
+@ARGV == 2 or die "usage: convert_to_rev_db <input> <output>";
+my $record_size = $oidlen + 4;
+my $hexlen = $oidlen * 2;
+open my $wr, '+>', $ARGV[1] or die "$!: couldn't open: $ARGV[1]";
+open my $rd, '<', $ARGV[0] or die "$!: couldn't open: $ARGV[0]";
+my $size = (stat($rd))[7];
+($size % $record_size) == 0 or die "Inconsistent size: $size";
+while (sysread($rd, my $buf, $record_size) == $record_size) {
+ my ($r, $c) = unpack("NH$hexlen", $buf);
+ my $offset = $r * ($hexlen + 1);
+ seek $wr, 0, 2 or die $!;
+ my $pos = tell $wr;
+ if ($pos < $offset) {
+ for (1 .. (($offset - $pos) / ($hexlen + 1))) {
+ print $wr (('0' x $hexlen),"\n") or die $!;
+ }
+ }
+ seek $wr, $offset, 0 or die $!;
+ print $wr $c,"\n" or die $!;
+}
+close $wr or die $!;
+close $rd or die $!;
+EOF
+}
+
+require_svnserve () {
+ if ! test_bool_env GIT_TEST_SVNSERVE false
+ then
+ skip_all='skipping svnserve test. (set $GIT_TEST_SVNSERVE to enable)'
+ test_done
+ fi
+}
+
+start_svnserve () {
+ svnserve --listen-port $SVNSERVE_PORT \
+ --root "$rawsvnrepo" \
+ --listen-once \
+ --listen-host 127.0.0.1 &
+}
+
+prepare_utf8_locale () {
+ if test -z "$GIT_TEST_UTF8_LOCALE"
+ then
+ case "${LC_ALL:-$LANG}" in
+ *.[Uu][Tt][Ff]8 | *.[Uu][Tt][Ff]-8)
+ GIT_TEST_UTF8_LOCALE="${LC_ALL:-$LANG}"
+ ;;
+ *)
+ GIT_TEST_UTF8_LOCALE=$(locale -a | sed -n '/\.[uU][tT][fF]-*8$/{
+ p
+ q
+ }')
+ ;;
+ esac
+ fi
+ if test -n "$GIT_TEST_UTF8_LOCALE"
+ then
+ test_set_prereq UTF8
+ else
+ say "# UTF-8 locale not available, some tests are skipped"
+ fi
+}
diff --git a/t/lib-gitweb.sh b/t/lib-gitweb.sh
new file mode 100644
index 0000000..1f32ca6
--- /dev/null
+++ b/t/lib-gitweb.sh
@@ -0,0 +1,123 @@
+# Initialization and helpers for Gitweb tests, which source this
+# shell library instead of test-lib.sh.
+#
+# Copyright (c) 2007 Jakub Narebski
+#
+
+gitweb_init () {
+ safe_pwd="$(perl -MPOSIX=getcwd -e 'print quotemeta(getcwd)')"
+ cat >gitweb_config.perl <<EOF
+#!/usr/bin/perl
+
+# gitweb configuration for tests
+
+our \$version = 'current';
+our \$GIT = 'git';
+our \$projectroot = "$safe_pwd";
+our \$project_maxdepth = 8;
+our \$home_link_str = 'projects';
+our \$site_name = '[localhost]';
+our \$site_html_head_string = '';
+our \$site_header = '';
+our \$site_footer = '';
+our \$home_text = 'indextext.html';
+our @stylesheets = ('file:///$GIT_BUILD_DIR/gitweb/static/gitweb.css');
+our \$logo = 'file:///$GIT_BUILD_DIR/gitweb/static/git-logo.png';
+our \$favicon = 'file:///$GIT_BUILD_DIR/gitweb/static/git-favicon.png';
+our \$projects_list = '';
+our \$export_ok = '';
+our \$strict_export = '';
+our \$maxload = undef;
+
+EOF
+
+ cat >.git/description <<EOF
+$0 test repository
+EOF
+
+ # You can set the GITWEB_TEST_INSTALLED environment variable to
+ # the gitwebdir (the directory where gitweb is installed / deployed to)
+ # of an existing gitweb installation to test that installation,
+ # or simply to pathname of installed gitweb script.
+ if test -n "$GITWEB_TEST_INSTALLED" ; then
+ if test -d $GITWEB_TEST_INSTALLED; then
+ SCRIPT_NAME="$GITWEB_TEST_INSTALLED/gitweb.cgi"
+ else
+ SCRIPT_NAME="$GITWEB_TEST_INSTALLED"
+ fi
+ test -f "$SCRIPT_NAME" ||
+ error "Cannot find gitweb at $GITWEB_TEST_INSTALLED."
+ say "# Testing $SCRIPT_NAME"
+ else # normal case, use source version of gitweb
+ SCRIPT_NAME="$GIT_BUILD_DIR/gitweb/gitweb.perl"
+ fi
+ export SCRIPT_NAME
+}
+
+gitweb_run () {
+ GATEWAY_INTERFACE='CGI/1.1'
+ HTTP_ACCEPT='*/*'
+ REQUEST_METHOD='GET'
+ QUERY_STRING=$1
+ PATH_INFO=$2
+ REQUEST_URI=/gitweb.cgi$PATH_INFO
+ export GATEWAY_INTERFACE HTTP_ACCEPT REQUEST_METHOD \
+ QUERY_STRING PATH_INFO REQUEST_URI
+
+ GITWEB_CONFIG=$(pwd)/gitweb_config.perl
+ export GITWEB_CONFIG
+
+ # some of git commands write to STDERR on error, but this is not
+ # written to web server logs, so we are not interested in that:
+ # we are interested only in properly formatted errors/warnings
+ rm -f gitweb.log &&
+ perl -- "$SCRIPT_NAME" \
+ >gitweb.output 2>gitweb.log &&
+ perl -w -e '
+ open O, ">gitweb.headers";
+ while (<>) {
+ print O;
+ last if (/^\r$/ || /^$/);
+ }
+ open O, ">gitweb.body";
+ while (<>) {
+ print O;
+ }
+ close O;
+ ' gitweb.output &&
+ if grep '^[[]' gitweb.log >/dev/null 2>&1; then
+ test_debug 'cat gitweb.log >&2' &&
+ false
+ else
+ true
+ fi
+
+ # gitweb.log is left for debugging
+ # gitweb.output is used to parse HTTP output
+ # gitweb.headers contains only HTTP headers
+ # gitweb.body contains body of message, without headers
+}
+
+. ./test-lib.sh
+
+if ! test_have_prereq PERL; then
+ skip_all='skipping gitweb tests, perl not available'
+ test_done
+fi
+
+perl -MEncode -e '$e="";decode_utf8($e, Encode::FB_CROAK)' >/dev/null 2>&1 || {
+ skip_all='skipping gitweb tests, perl version is too old'
+ test_done
+}
+
+perl -MCGI -MCGI::Util -MCGI::Carp -e 0 >/dev/null 2>&1 || {
+ skip_all='skipping gitweb tests, CGI & CGI::Util & CGI::Carp modules not available'
+ test_done
+}
+
+perl -mTime::HiRes -e 0 >/dev/null 2>&1 || {
+ skip_all='skipping gitweb tests, Time::HiRes module not available'
+ test_done
+}
+
+gitweb_init
diff --git a/t/lib-gpg.sh b/t/lib-gpg.sh
new file mode 100644
index 0000000..83b83c9
--- /dev/null
+++ b/t/lib-gpg.sh
@@ -0,0 +1,200 @@
+# We always set GNUPGHOME, even if no usable GPG was found, as
+#
+# - It does not hurt, and
+#
+# - we cannot set global environment variables in lazy prereqs because they are
+# executed in an eval'ed subshell that changes the working directory to a
+# temporary one.
+
+GNUPGHOME="$PWD/gpghome"
+export GNUPGHOME
+
+test_lazy_prereq GPG '
+ gpg_version=$(gpg --version 2>&1)
+ test $? != 127 || exit 1
+
+ # As said here: http://www.gnupg.org/documentation/faqs.html#q6.19
+ # the gpg version 1.0.6 did not parse trust packets correctly, so for
+ # that version, creation of signed tags using the generated key fails.
+ case "$gpg_version" in
+ "gpg (GnuPG) 1.0.6"*)
+ say "Your version of gpg (1.0.6) is too buggy for testing"
+ exit 1
+ ;;
+ *)
+ # Available key info:
+ # * Type DSA and Elgamal, size 2048 bits, no expiration date,
+ # name and email: C O Mitter <committer@example.com>
+ # * Type RSA, size 2048 bits, no expiration date,
+ # name and email: Eris Discordia <discord@example.net>
+ # No password given, to enable non-interactive operation.
+ # To generate new key:
+ # gpg --homedir /tmp/gpghome --gen-key
+ # To write armored exported key to keyring:
+ # gpg --homedir /tmp/gpghome --export-secret-keys \
+ # --armor 0xDEADBEEF >> lib-gpg/keyring.gpg
+ # gpg --homedir /tmp/gpghome --export \
+ # --armor 0xDEADBEEF >> lib-gpg/keyring.gpg
+ # To export ownertrust:
+ # gpg --homedir /tmp/gpghome --export-ownertrust \
+ # > lib-gpg/ownertrust
+ mkdir "$GNUPGHOME" &&
+ chmod 0700 "$GNUPGHOME" &&
+ (gpgconf --kill all || : ) &&
+ gpg --homedir "${GNUPGHOME}" --import \
+ "$TEST_DIRECTORY"/lib-gpg/keyring.gpg &&
+ gpg --homedir "${GNUPGHOME}" --import-ownertrust \
+ "$TEST_DIRECTORY"/lib-gpg/ownertrust &&
+ gpg --homedir "${GNUPGHOME}" --update-trustdb &&
+ gpg --homedir "${GNUPGHOME}" </dev/null >/dev/null \
+ --sign -u committer@example.com
+ ;;
+ esac
+'
+
+test_lazy_prereq GPG2 '
+ gpg_version=$(gpg --version 2>&1)
+ test $? != 127 || exit 1
+
+ case "$gpg_version" in
+ "gpg (GnuPG) "[01].*)
+ say "This test requires a GPG version >= v2.0.0"
+ exit 1
+ ;;
+ *)
+ (gpgconf --kill all || : ) &&
+ gpg --homedir "${GNUPGHOME}" --import \
+ "$TEST_DIRECTORY"/lib-gpg/keyring.gpg &&
+ gpg --homedir "${GNUPGHOME}" --import-ownertrust \
+ "$TEST_DIRECTORY"/lib-gpg/ownertrust &&
+ gpg --homedir "${GNUPGHOME}" </dev/null >/dev/null \
+ --sign -u committer@example.com
+ ;;
+ esac
+'
+
+test_lazy_prereq GPGSM '
+ test_have_prereq GPG &&
+ # Available key info:
+ # * see t/lib-gpg/gpgsm-gen-key.in
+ # To generate new certificate:
+ # * no passphrase
+ # gpgsm --homedir /tmp/gpghome/ \
+ # -o /tmp/gpgsm.crt.user \
+ # --generate-key \
+ # --batch t/lib-gpg/gpgsm-gen-key.in
+ # To import certificate:
+ # gpgsm --homedir /tmp/gpghome/ \
+ # --import /tmp/gpgsm.crt.user
+ # To export into a .p12 we can later import:
+ # gpgsm --homedir /tmp/gpghome/ \
+ # -o t/lib-gpg/gpgsm_cert.p12 \
+ # --export-secret-key-p12 "committer@example.com"
+ echo | gpgsm --homedir "${GNUPGHOME}" \
+ --passphrase-fd 0 --pinentry-mode loopback \
+ --import "$TEST_DIRECTORY"/lib-gpg/gpgsm_cert.p12 &&
+
+ gpgsm --homedir "${GNUPGHOME}" -K --with-colons |
+ awk -F ":" "/^fpr:/ {printf \"%s S relax\\n\", \$10}" \
+ >"${GNUPGHOME}/trustlist.txt" &&
+ (gpgconf --reload all || : ) &&
+
+ echo hello | gpgsm --homedir "${GNUPGHOME}" >/dev/null \
+ -u committer@example.com -o /dev/null --sign -
+'
+
+test_lazy_prereq RFC1991 '
+ test_have_prereq GPG &&
+ echo | gpg --homedir "${GNUPGHOME}" -b --rfc1991 >/dev/null
+'
+
+GPGSSH_KEY_PRIMARY="${GNUPGHOME}/ed25519_ssh_signing_key"
+GPGSSH_KEY_SECONDARY="${GNUPGHOME}/rsa_2048_ssh_signing_key"
+GPGSSH_KEY_UNTRUSTED="${GNUPGHOME}/untrusted_ssh_signing_key"
+GPGSSH_KEY_EXPIRED="${GNUPGHOME}/expired_ssh_signing_key"
+GPGSSH_KEY_NOTYETVALID="${GNUPGHOME}/notyetvalid_ssh_signing_key"
+GPGSSH_KEY_TIMEBOXEDVALID="${GNUPGHOME}/timeboxed_valid_ssh_signing_key"
+GPGSSH_KEY_TIMEBOXEDINVALID="${GNUPGHOME}/timeboxed_invalid_ssh_signing_key"
+GPGSSH_KEY_WITH_PASSPHRASE="${GNUPGHOME}/protected_ssh_signing_key"
+GPGSSH_KEY_ECDSA="${GNUPGHOME}/ecdsa_ssh_signing_key"
+GPGSSH_KEY_PASSPHRASE="super_secret"
+GPGSSH_ALLOWED_SIGNERS="${GNUPGHOME}/ssh.all_valid.allowedSignersFile"
+
+GPGSSH_GOOD_SIGNATURE_TRUSTED='Good "git" signature for'
+GPGSSH_GOOD_SIGNATURE_UNTRUSTED='Good "git" signature with'
+GPGSSH_KEY_NOT_TRUSTED="No principal matched"
+GPGSSH_BAD_SIGNATURE="Signature verification failed"
+
+test_lazy_prereq GPGSSH '
+ ssh_version=$(ssh-keygen -Y find-principals -n "git" 2>&1)
+ test $? != 127 || exit 1
+ echo $ssh_version | grep -q "find-principals:missing signature file"
+ test $? = 0 || exit 1;
+
+ # Setup some keys and an allowed signers file
+ mkdir -p "${GNUPGHOME}" &&
+ chmod 0700 "${GNUPGHOME}" &&
+ (setfacl -k "${GNUPGHOME}" 2>/dev/null || true) &&
+ ssh-keygen -t ed25519 -N "" -C "git ed25519 key" -f "${GPGSSH_KEY_PRIMARY}" >/dev/null &&
+ ssh-keygen -t rsa -b 2048 -N "" -C "git rsa2048 key" -f "${GPGSSH_KEY_SECONDARY}" >/dev/null &&
+ ssh-keygen -t ed25519 -N "${GPGSSH_KEY_PASSPHRASE}" -C "git ed25519 encrypted key" -f "${GPGSSH_KEY_WITH_PASSPHRASE}" >/dev/null &&
+ ssh-keygen -t ecdsa -N "" -f "${GPGSSH_KEY_ECDSA}" >/dev/null &&
+ ssh-keygen -t ed25519 -N "" -C "git ed25519 key" -f "${GPGSSH_KEY_UNTRUSTED}" >/dev/null &&
+
+ cat >"${GPGSSH_ALLOWED_SIGNERS}" <<-EOF &&
+ "principal with number 1" $(cat "${GPGSSH_KEY_PRIMARY}.pub")"
+ "principal with number 2" $(cat "${GPGSSH_KEY_SECONDARY}.pub")"
+ "principal with number 3" $(cat "${GPGSSH_KEY_WITH_PASSPHRASE}.pub")"
+ "principal with number 4" $(cat "${GPGSSH_KEY_ECDSA}.pub")"
+ EOF
+
+ # Verify if at least one key and ssh-keygen works as expected
+ echo "testpayload" |
+ ssh-keygen -Y sign -n "git" -f "${GPGSSH_KEY_PRIMARY}" >gpgssh_prereq.sig &&
+ ssh-keygen -Y find-principals -f "${GPGSSH_ALLOWED_SIGNERS}" -s gpgssh_prereq.sig &&
+ echo "testpayload" |
+ ssh-keygen -Y verify -n "git" -f "${GPGSSH_ALLOWED_SIGNERS}" -I "principal with number 1" -s gpgssh_prereq.sig
+'
+
+test_lazy_prereq GPGSSH_VERIFYTIME '
+ test_have_prereq GPGSSH &&
+ # Check if ssh-keygen has a verify-time option by passing an invalid date to it
+ ssh-keygen -Overify-time=INVALID -Y check-novalidate -n "git" -s doesnotmatter 2>&1 | grep -q -F "Invalid \"verify-time\"" &&
+
+ # Set up keys with key lifetimes
+ ssh-keygen -t ed25519 -N "" -C "timeboxed valid key" -f "${GPGSSH_KEY_TIMEBOXEDVALID}" >/dev/null &&
+ key_valid=$(cat "${GPGSSH_KEY_TIMEBOXEDVALID}.pub") &&
+ ssh-keygen -t ed25519 -N "" -C "timeboxed invalid key" -f "${GPGSSH_KEY_TIMEBOXEDINVALID}" >/dev/null &&
+ key_invalid=$(cat "${GPGSSH_KEY_TIMEBOXEDINVALID}.pub") &&
+ ssh-keygen -t ed25519 -N "" -C "expired key" -f "${GPGSSH_KEY_EXPIRED}" >/dev/null &&
+ key_expired=$(cat "${GPGSSH_KEY_EXPIRED}.pub") &&
+ ssh-keygen -t ed25519 -N "" -C "not yet valid key" -f "${GPGSSH_KEY_NOTYETVALID}" >/dev/null &&
+ key_notyetvalid=$(cat "${GPGSSH_KEY_NOTYETVALID}.pub") &&
+
+ # Timestamps outside of test_tick span
+ ts2005a=20050401000000 ts2005b=200504020000 &&
+ # Timestamps within test_tick span
+ ts2005c=20050407000000 ts2005d=200504100000 &&
+ # Definitely not yet valid / expired timestamps
+ ts2000=20000101000000 ts2999=29990101000000 &&
+
+ cat >>"${GPGSSH_ALLOWED_SIGNERS}" <<-EOF &&
+ "timeboxed valid key" valid-after="$ts2005c",valid-before="$ts2005d" $key_valid"
+ "timeboxed invalid key" valid-after="$ts2005a",valid-before="$ts2005b" $key_invalid"
+ "principal with expired key" valid-before="$ts2000" $key_expired"
+ "principal with not yet valid key" valid-after="$ts2999" $key_notyetvalid"
+ EOF
+
+ # and verify ssh-keygen verifies the key lifetime
+ echo "testpayload" |
+ ssh-keygen -Y sign -n "git" -f "${GPGSSH_KEY_EXPIRED}" >gpgssh_verifytime_prereq.sig &&
+ ! (ssh-keygen -Y verify -n "git" -f "${GPGSSH_ALLOWED_SIGNERS}" -I "principal with expired key" -s gpgssh_verifytime_prereq.sig)
+'
+
+sanitize_pgp() {
+ perl -ne '
+ /^-----END PGP/ and $in_pgp = 0;
+ print unless $in_pgp;
+ /^-----BEGIN PGP/ and $in_pgp = 1;
+ '
+}
diff --git a/t/lib-gpg/gpgsm-gen-key.in b/t/lib-gpg/gpgsm-gen-key.in
new file mode 100644
index 0000000..a7fd87c
--- /dev/null
+++ b/t/lib-gpg/gpgsm-gen-key.in
@@ -0,0 +1,8 @@
+Key-Type: RSA
+Key-Length: 2048
+Key-Usage: sign
+Serial: random
+Name-DN: CN=C O Mitter, O=Example, SN=C O, GN=Mitter
+Name-Email: committer@example.com
+Not-Before: 1970-01-01 00:00:00
+Not-After: 3000-01-01 00:00:00
diff --git a/t/lib-gpg/gpgsm_cert.p12 b/t/lib-gpg/gpgsm_cert.p12
new file mode 100644
index 0000000..94ffad0
--- /dev/null
+++ b/t/lib-gpg/gpgsm_cert.p12
Binary files differ
diff --git a/t/lib-gpg/keyring.gpg b/t/lib-gpg/keyring.gpg
new file mode 100644
index 0000000..918dfce
--- /dev/null
+++ b/t/lib-gpg/keyring.gpg
@@ -0,0 +1,192 @@
+-----BEGIN PGP PRIVATE KEY BLOCK-----
+Version: GnuPG v1
+
+lQG7BEZnyykRBACzCPjIpTYNL7Y2tQqlEGTTDlvZcWNLjF5f7ZzuyOqNOidLUgFD
+36qch1LZLSZkShdR3Gae+bsolyjxrlFuFP0eXRPMtqK20aLw7WZvPFpEV1ThMne+
+PRJjYrvghWw3L0VVIAIZ8GXwrVBuU99uEjHEI0ojYloOvFc2jVPgSaoBvwCg48Tj
+fol2foSoJa7XUu9yAL8szg8D/RUsTzNF+I9hSRHl7MYKFMYoKEY9BDgrgAujp7YY
+8qdGsiUb0Ggyzp2kRjZFt4lpcvKhGfHn5GEjmtk+fRbD5qPfMqKFW+T0NPfYlYmL
+JJ4fs4qZ8Lx7x6iG6X51u+YNwsQuIGjMCC3CeNi3F7or651kkNYASbaQ1NROkCIN
+NudyA/0aasvoZUoNJAc2cP5Ifs6WhXMWLfMR2p2XbfKwKNYneec60usnSComcKqh
+sJVk0Gytvr3FOYVhRkXnKAbx+0W2urFP8OFVBTEKO6Ts2VygWGgneQYoHnqzwlUE
+yjOjlr+lyf7u2s/KAxpKA6jnttEdRZAmzWkhuox1wwAUkr27/QAAn3TEzKR1pxxR
++R3dHuFpnnfatMIDC5O0IkMgTyBNaXR0ZXIgPGNvbW1pdHRlckBleGFtcGxlLmNv
+bT6IXgQTEQIAHgUCRmfLKQIbAwYLCQgHAwIDFQIDAxYCAQIeAQIXgAAKCRATtvUe
+zd5DDXQdAKC92f+wOrTkbmPEf+u+qA/Gv6BxQwCfQ128JXCi3MpMB8tI2Kmo15tY
+gnmdAj0ERmfLThAIAM65eT9T6+gg0fJn+Qxhs3FFDPjxK6AOBS3SieWWmXO6stZZ
+plvb7r2+sXYp8HMHntnOX3TRPolIx1dsdkv3W3w8yUzf9Lmo2XMPsZ3/isWdEbOI
+A0rO3B1xwbQO7vEoWHeB7uyYIF6YsIH0pMqxkImciwB1tnJPB9OxqPHlD/HyyHr2
+voj6nmEGaPQWj8/dkfyenXm6XmNZUZL/slk6tRhNwv4cW3QQLh39nbiz9rqvZMKF
+XX8wkY4FdQkJjCGwqzG+7yJcyHvem29/iq//jRLZgdiN8BwV3MCTJyDp8/Wb/d9y
+jZcUm1RdtwRiwfhfQ+zmpyspm7OxINfH65rf7f8ABA0IALRiMRs/eOD59jrYXmPS
+ZQUbiALlbJJtuP2c9N3WZ5OgrhDiAW+SDIN+hgDynJ9b7C2dE3xNaud4zaXAAF44
+J4J0bAo2ZtZoJajw+GXwaZfh4Z7nPNHwEcbFD4/uXPCj9jPkcLOJqGmUY1aXdygo
+t3Hn5U/zo8JxPQ83YbJQhkzAOZ/HGowLNqKgGkLLHn1X9qay0CxlfTQeEN5RZyl3
+b4qRzGgGALFvoheyZIUw1TbjRpbn3kqlJooEQY02VwXFXfLI/LwzglilH6sSckvs
+0WHKLZ+0L6b3CgJHN2RsZ7QxwCBi1aemsvr65FeEXp/AYxaG5duUbsugG8PgoJ06
+bsEAAVQNQO3cXWpuiJ/nNLLnWuPunBKJUlurkBdf2GD+m+muF0VpwDchhqqbTO4e
+FqOISQQYEQIACQUCRmfLTgIbDAAKCRATtvUezd5DDcHsAKDQcoAtDWJFupVRqleB
+Cezx4Q2khACcCs+/LtE8Lb9hC+2cvr3uH5p82AI=
+=aEiU
+-----END PGP PRIVATE KEY BLOCK-----
+-----BEGIN PGP PRIVATE KEY BLOCK-----
+
+lQOYBFFMlkcBCADJi/xnAF8yI34PHilSCbM7VtOFO17oFMkpu4cgN2QpPuM5MVjy
+cvrzKSguZFvPCDLzeAFJW1uPxL4SHaHSkisCrFhijH7OJWcOPNPSFCwu+inAoAsv
+Hm4ns6pfDZyRjVTHSY4rdMISqKFRozaXu8vHeBRzIhFnubBCepKZW07oKPnrnELV
+TVUSUVI+6el8JFmJIWxxLNLhfRRSPF0v4MDXPF//iCWiZDI+J1pLvQ5V/f7YtfsD
+GV0oPY66J72BFJG555eKBttnNY901LmI3ocn5P5iVnXDaqMElw7FKpnANXucgY3H
+4kLyNkI3s3J0CGbXI7b3MBWtjctuhWv1q2G5ABEBAAEAB/wLiuza/qEfv1Cfj7FQ
+ytAXpz1YoAcrcM/53TeRQhrbvIee5ZNGhLdCkyot81QeuJrSaXO0E9CxRynrjQQ7
+ibYqN7Hy0uu1kAbQQJjmVdQXTKnKJ7Wm7oM4hYhNsVCKNXc+1+5AfDYGg4nZob36
+qqgHtc+Ardl5VfUg7uF+eZrnSMynjZANgikKbPtE09DKVtVOtUE4xTD9ijkpgn65
+glsZDqb7J4QVgTeEiCDKJsQvin3SwrPBqBxBRULF2TIaMbOwe6dHiiaI85rsvAWS
+VGzonUB3IU1470P2SDIVczbXYUK/nDSGx6ZZ0wLu9ZcCyUPvxVEykuh2P4UWHla+
+nHLRBADMLavcfjsCI5CRUsdurYpgE8Y3bEbcDpvzAu5jT5D25p3YPDODOXD3AKTt
+PzVMARVtv8twkbgAyWaoDevJz8OtmoSwsWjdFo4YvsYw9jV7Yf3GwzD3Ya1ZnW32
+JWQr6cX8qcK0AukAD7UZkVyhU2KBvB02t8lKHLbScHXTYVqrywQA/LNUXwmHji+6
+osnSQAC8X9ggMOEs9dGo7Qlk4JgfGAH17CFI3S3ubsaVEdxz3YwzOkD8SNmEbLyW
+a7CZ/RnpdAZU0nB7kSfbfZl7ajhPbgKBMsaV2yvaDdJeor4m5eKdXffRk0SksxjL
+Z/4P1tTIuL8WzetGB/aDcWDFgseSAAsEALzmf579ptlSmDyGRAKQqub+mj4V3EUZ
+1GVGcfBY86w3BZVDsaRiCtcNjk/lcP4AZ1Vbb42RM6jk8nLsENRc7rf2xa7ZPf0T
+6n5F6W+vk7EG76RoFhKVtGZngGKiDGVavxk3FT/yf8lKrT3wYiT03SZDuZ0pWvku
+FiJGEyesAC8WRz60JEVyaXMgRGlzY29yZGlhIDxkaXNjb3JkQGV4YW1wbGUubmV0
+PokBOAQTAQIAIgUCUUyWRwIbAwYLCQgHAwIGFQgCCQoLBBYCAwECHgECF4AACgkQ
+YQkuhbcicYlYowf7B+f+FDcLVfw8XzGlKku1F6PI1yGCt7AMO2/JkmO4LlgHuIgF
+pqe5b/XjKl0IsRcbVLitqiIokc8u+7H8yYU67DDliq7t1gqBy+qThSHcgn6WMKTa
+qCqOE2jzHyqulIAzQsJQ+c5SRofEZAKT4Qa2Dy+nsqWDpIE78aJd0Vnkk9U6H2Vu
+ABvUeN/IMgvxPr525o+rBD7LU4J3CtOzfV+sO6+33da+Bm9UhkR4tC4H/n1dDN1J
+YuxBQbgxTq/h8mKe4/7/Yvy+5WsYd96ZRLE2ZFWeWXtKkwmYbQ42G3SZUXaZ8R8O
+tbTyUrjbFKipO4wvXwhyju1l9cxAsrca6xbSCJ0DmARRTJZHAQgAqTtPFcTXqM+U
+o7bOoo+dcHi8XDf/8XSEmZfMKc/U5pSTBk7h1gSKuGzjF2n3wQm6A8+101vTLaQ6
+PoFDFW8uQB00mjymGrRDYFgz8bjhnaekZnA4XThr1ROjffgMhs3uTpCebdV+lL8K
+0oJTHc39TPLTg23DFcRSDN+3ARJJS7+CRBIbt9L5gObpgA4HUap/o6N7O04rQOPU
+83MAqnwo2JTO/Ded0zoad0Vo31Nmk4F+KvEE52ftGHbd7yqIUGKBt2SeTAh850ac
+LeNZP+V1Y7atBCr7/zm+JpHWq9OH7/NomlEIkxL8WDt8GfAKoqZgqefL+ACEnLbA
+t1du3f0FswARAQABAAf8DclaIQDfPM5kYo3y+YVPoykC11RskmQWpVibdlCLHJm/
+/ISSm1fVYT7lpTOpzl0XfVX/jw9s/cviPtNS/r0G/Iwki+gi9Av5bTDiUm/oWWqd
+1waPYPDGwB4QdKOviY/fOSFI9tOsszt5Czs4wDXWy90AZDWd7fkHYisbgofV1sjK
+Q8bYQPabcepcZ2JyET+EpZBEmUHHqQ76bTiqjN+Vz6k1OFlsEBzGkE+WIakAhkQ2
+57oUrRgFe+h6Ch7meB/v6vVfIRSsLpZe183uc4SigqtfsgjbG9PqOcAJOqovDncB
+Scg3qvpWFOAkTA3Re+yBPUd2HHl9WF/TPa2kBDCT2QQAxcJZeUCuUgDgCizqEgfs
+Kzm6dy4G/OJdW0q9m9psHqD1XWLd7ZLE4+eTS1cxktJiGcGNdGoZD0EtgxkV09uM
+12QYCOBErFJzv4/4oledHeEhTaRR/mFFGRp+kWTz2Ai/zNqUd3D++DYUe8g4mVQJ
+6JP014XhvoRnaCfT8cH9Zd0EANsSL70WGdifcVoWKA9jFJhahc0sSG6IZvMOc7bs
+cSbhBqLEnheObkarBP+A+zgllqIf+sbCassMXjcV52mnl9th3J5RWr7scrQLJ9ZX
+Ivz3uoP85vwlUI98dI9roYK0OpKmG4hNFppAcgiCVNVjnQlhuQ/HoexRHxRmnmcb
+38jPA/sEHPCFbLCGOSB+HQNKx/5Wf6VpFX/4oBNbIUiYoxcRl0jpYT7Lc0zbc8So
+HthjPfWhXhKzYvEDC5YgASEy1cNbGMUJcGyuAInwIQjq44FSwRMkI3ISSHnbv1iH
+0wBVJUzpluMebEAesdZUz1DcZWVf6eVJD0dhZxD6DoG7Xj1m9ThUiQEfBBgBAgAJ
+BQJRTJZHAhsMAAoJEGEJLoW3InGJ7HEIAMXkMf4cOWmnAuvvcSm3KpLghuWik9dy
+fn1sY/IG5atoKK+ypmV/TlBlMZqFQzuPIJQT8VLbmxtLlDhJG04LbI6c8axIZxOO
+ZKLy5nTTSy16ztqEeS7eifHLPZg1UFFyEEIQ1XW0CNDAeuWKh90ERjyl4Cg7PnWS
+Z9Ei+zj6JD5Pcdi3BJhQo9WOLOVEJ0NHmewTYqk9QVXH/0v1Hdl4LMJtgcbdbDWk
+4UTkXbg9pn3umCgkNJ3Vs8fWnIWO9Izdr2/wrFY2JvUT7Yvl+wsNIWatvOEzGy7n
+BOW78WUxzhu0YJTLKy+iKCjg5HS5dx6OC+e4aEEgfhNPCMkbvDsJjtSdA5gEW967
+3AEIAKjseT0sTQjyN39fOn0fzxWp89REMUUKgLigb01MKuuNI3cedBZsz3hpFOKV
+cii5rldw8uf3yS3Okht2DfHPSD4NrGzLGEzSTpQ10S8N2q0DUYwyLU6C0U8HnMZm
+/n+lCGBbUoxvnruohAvKAjpHO3rmJ8D4De9hlWg/fwdAxQQ0Sve0kN8Vwk2p1GuO
+OWQKV1SU9c+kBiou7dewQmbilPRanKmP5ZSU4emhpTOMlJFXF+kmYSODQk1cMvWW
+Ob3ttll2llX0Gul7Sjf+haq/FcRyRk7Tw5MHwZjr5aWiCny0/0+byvfF6SBIfzyE
+qlyWURQ2gHZUqSiG3QPMZiYr04cAEQEAAQAH/Am4rv/oQF6wodgz5y4zc6JJiTDA
+4+nKdIuR7OKqUxk1oo7eZjJML/xvMumygNyUvJ9nodl1SlMKilOhdAswfkKj9gJY
+BdDJLm1OufhW3pJwy6ahbjeqEgwJFVENtSPF0zkuyED9kElrpbD2ZTGfzwdM0e9D
+10ZDFWtODCw8rzOFcijujgI8oilLtxSNrkkTKW+25WJFRNPSHgIkMIm8UlPAG+rj
+3Yj9UqodeXTSvXwG2zceOxjFJadV77sOFJDgwWslN6J8El4+GcgwFVepJxoZEj7e
+cKkmVr0Dc9/Q04D5dWATc1FYcIhZbTu3oImCAh45ep4u9WYLUV5PGyeMviEEAMwo
+mJbYBxWuPjpNa722HQcbvMUiZWWDwHfLCib/SaP0AgfDahid8/PcZwxOPHPByBrm
+GDi0z7ibn/pgJr07kpp1Cic9ntfc2FvkI0QMzG0EuiekzQyPEnzjoDHF+V4nJIj2
+GWVjLYYqlZWEmhsfKt1CnlPXBunKoDJ30ABPcHJ/BADT0WxAIVKF4lO2HlrDVP44
+bufBEG9Ct7dl/G08Qve4Ag3VEZpT82vEFp0LzX0mTCDIUKJUYAYLxAIPhP7IvIfc
+EZXrwyDUxU7YSgKTHMKo9nFC6fIc1GeGPRalIF1gmTY32qlYJC6y5BTDhZNV5ydG
+u8QL2P/orP7XuRrJyeyK+QP/XTekr/DS6Jkct826MPA52ciIkWVgYLatH5fO4HCq
+ssDU8vz7FbbvGs0G1Xn7GA4m9dNYVOZtKwX++3nf2IEOpgPiZVTn/nP2u3HutpJb
+/HMLlcfZGiGdxS6n/vdz6wsEobJoi6STkHkA+VFNOSZmdsw6eKl3X911tpCTYfOG
+2U47/IkCbAQYAQgAIBYhBNS+IjEa0xMeXtoppGEJLoW3InGJBQJb3rvcAhsCAUAJ
+EGEJLoW3InGJwHQgBBkBCAAdFiEE+DZKWeB//p9NYwBaZaDuoC4wytcFAlveu9wA
+CgkQZaDuoC4wytcD9gf/WigtHl7lFyl8RaE/uqROFEelZyM00v1h55fd/IGRG88E
+tN0Lr4FaqBqPkMZjU/LN9UMBaTd+748vHlHaweZqljXJu99CO9Id7Y4w7WzF3C3Y
+yQsGZ92EGxthsPK0+rhHV0MbaINupI1oO9gATFglSxq17o83FJatGRjaXCZau8jr
+57/By1MGtjk+Iq1NkzGkrX778LdRQGLKDw2Qa7lsdHY8d3lUPAH8mbb97ELmIc9t
+PG2aM7ATJL7nBmFuTHo6hmEcIw32Ei9KK1zxM0ZylEYkjBjHAlklWmKb9MiayMC5
+uHW7Iyhjl+NbgbIEr2JTamW/9tL6UrIIxiDEdqaHNfCaB/9D+V31Upcohc9azwB4
+AF8diQwt5nfiVpnVeF/W8+eS1By2W6QrwLNthNRabYFnuSf9USHAY6atDWe+egId
+MLIv4ce0i3ykoczSu0oMoUCMxdl9kQrsNHZCqWX/OiDDLSb05u/P/3he900y6tSB
+15MbIPA6i5Bw/693nHguqxS1ASbBB/LiIu3vCXdFEs9RMvIJ+qkP3xQA96oImQiK
+R3U6OGv593eONKijUINNqHRq6+UxIyJ+OCAi+L2QTidAhJLRCp6EZD96u02cthYq
+8KA8j1+rx9BcbeacVVHepeG1JsgxsXX8BTJ7ZuS5VVndZOjag8URW/9nJMf01w/h
+el64
+=Iv7W
+-----END PGP PRIVATE KEY BLOCK-----
+-----BEGIN PGP PUBLIC KEY BLOCK-----
+
+mQGiBEZnyykRBACzCPjIpTYNL7Y2tQqlEGTTDlvZcWNLjF5f7ZzuyOqNOidLUgFD
+36qch1LZLSZkShdR3Gae+bsolyjxrlFuFP0eXRPMtqK20aLw7WZvPFpEV1ThMne+
+PRJjYrvghWw3L0VVIAIZ8GXwrVBuU99uEjHEI0ojYloOvFc2jVPgSaoBvwCg48Tj
+fol2foSoJa7XUu9yAL8szg8D/RUsTzNF+I9hSRHl7MYKFMYoKEY9BDgrgAujp7YY
+8qdGsiUb0Ggyzp2kRjZFt4lpcvKhGfHn5GEjmtk+fRbD5qPfMqKFW+T0NPfYlYmL
+JJ4fs4qZ8Lx7x6iG6X51u+YNwsQuIGjMCC3CeNi3F7or651kkNYASbaQ1NROkCIN
+NudyA/0aasvoZUoNJAc2cP5Ifs6WhXMWLfMR2p2XbfKwKNYneec60usnSComcKqh
+sJVk0Gytvr3FOYVhRkXnKAbx+0W2urFP8OFVBTEKO6Ts2VygWGgneQYoHnqzwlUE
+yjOjlr+lyf7u2s/KAxpKA6jnttEdRZAmzWkhuox1wwAUkr27/bQiQyBPIE1pdHRl
+ciA8Y29tbWl0dGVyQGV4YW1wbGUuY29tPoheBBMRAgAeBQJGZ8spAhsDBgsJCAcD
+AgMVAgMDFgIBAh4BAheAAAoJEBO29R7N3kMNdB0AoL3Z/7A6tORuY8R/676oD8a/
+oHFDAJ9DXbwlcKLcykwHy0jYqajXm1iCebkCDQRGZ8tOEAgAzrl5P1Pr6CDR8mf5
+DGGzcUUM+PEroA4FLdKJ5ZaZc7qy1lmmW9vuvb6xdinwcwee2c5fdNE+iUjHV2x2
+S/dbfDzJTN/0uajZcw+xnf+KxZ0Rs4gDSs7cHXHBtA7u8ShYd4Hu7JggXpiwgfSk
+yrGQiZyLAHW2ck8H07Go8eUP8fLIeva+iPqeYQZo9BaPz92R/J6debpeY1lRkv+y
+WTq1GE3C/hxbdBAuHf2duLP2uq9kwoVdfzCRjgV1CQmMIbCrMb7vIlzIe96bb3+K
+r/+NEtmB2I3wHBXcwJMnIOnz9Zv933KNlxSbVF23BGLB+F9D7OanKymbs7Eg18fr
+mt/t/wAEDQgAtGIxGz944Pn2OtheY9JlBRuIAuVskm24/Zz03dZnk6CuEOIBb5IM
+g36GAPKcn1vsLZ0TfE1q53jNpcAAXjgngnRsCjZm1mglqPD4ZfBpl+Hhnuc80fAR
+xsUPj+5c8KP2M+Rws4moaZRjVpd3KCi3ceflT/OjwnE9DzdhslCGTMA5n8cajAs2
+oqAaQssefVf2prLQLGV9NB4Q3lFnKXdvipHMaAYAsW+iF7JkhTDVNuNGlufeSqUm
+igRBjTZXBcVd8sj8vDOCWKUfqxJyS+zRYcotn7QvpvcKAkc3ZGxntDHAIGLVp6ay
++vrkV4Ren8BjFobl25Ruy6Abw+CgnTpuwYhJBBgRAgAJBQJGZ8tOAhsMAAoJEBO2
+9R7N3kMNwewAoNBygC0NYkW6lVGqV4EJ7PHhDaSEAJwKz78u0Twtv2EL7Zy+ve4f
+mnzYApkBDQRRTJZHAQgAyYv8ZwBfMiN+Dx4pUgmzO1bThTte6BTJKbuHIDdkKT7j
+OTFY8nL68ykoLmRbzwgy83gBSVtbj8S+Eh2h0pIrAqxYYox+ziVnDjzT0hQsLvop
+wKALLx5uJ7OqXw2ckY1Ux0mOK3TCEqihUaM2l7vLx3gUcyIRZ7mwQnqSmVtO6Cj5
+65xC1U1VElFSPunpfCRZiSFscSzS4X0UUjxdL+DA1zxf/4glomQyPidaS70OVf3+
+2LX7AxldKD2Ouie9gRSRueeXigbbZzWPdNS5iN6HJ+T+YlZ1w2qjBJcOxSqZwDV7
+nIGNx+JC8jZCN7NydAhm1yO29zAVrY3LboVr9athuQARAQABtCRFcmlzIERpc2Nv
+cmRpYSA8ZGlzY29yZEBleGFtcGxlLm5ldD6JATgEEwECACIFAlFMlkcCGwMGCwkI
+BwMCBhUIAgkKCwQWAgMBAh4BAheAAAoJEGEJLoW3InGJWKMH+wfn/hQ3C1X8PF8x
+pSpLtRejyNchgrewDDtvyZJjuC5YB7iIBaanuW/14ypdCLEXG1S4raoiKJHPLvux
+/MmFOuww5Yqu7dYKgcvqk4Uh3IJ+ljCk2qgqjhNo8x8qrpSAM0LCUPnOUkaHxGQC
+k+EGtg8vp7Klg6SBO/GiXdFZ5JPVOh9lbgAb1HjfyDIL8T6+duaPqwQ+y1OCdwrT
+s31frDuvt93WvgZvVIZEeLQuB/59XQzdSWLsQUG4MU6v4fJinuP+/2L8vuVrGHfe
+mUSxNmRVnll7SpMJmG0ONht0mVF2mfEfDrW08lK42xSoqTuML18Ico7tZfXMQLK3
+GusW0gi5AQ0EUUyWRwEIAKk7TxXE16jPlKO2zqKPnXB4vFw3//F0hJmXzCnP1OaU
+kwZO4dYEirhs4xdp98EJugPPtdNb0y2kOj6BQxVvLkAdNJo8phq0Q2BYM/G44Z2n
+pGZwOF04a9UTo334DIbN7k6Qnm3VfpS/CtKCUx3N/Uzy04NtwxXEUgzftwESSUu/
+gkQSG7fS+YDm6YAOB1Gqf6OjeztOK0Dj1PNzAKp8KNiUzvw3ndM6GndFaN9TZpOB
+firxBOdn7Rh23e8qiFBigbdknkwIfOdGnC3jWT/ldWO2rQQq+/85viaR1qvTh+/z
+aJpRCJMS/Fg7fBnwCqKmYKnny/gAhJy2wLdXbt39BbMAEQEAAYkBHwQYAQIACQUC
+UUyWRwIbDAAKCRBhCS6FtyJxiexxCADF5DH+HDlppwLr73EptyqS4IblopPXcn59
+bGPyBuWraCivsqZlf05QZTGahUM7jyCUE/FS25sbS5Q4SRtOC2yOnPGsSGcTjmSi
+8uZ000stes7ahHku3onxyz2YNVBRchBCENV1tAjQwHrliofdBEY8peAoOz51kmfR
+Ivs4+iQ+T3HYtwSYUKPVjizlRCdDR5nsE2KpPUFVx/9L9R3ZeCzCbYHG3Ww1pOFE
+5F24PaZ97pgoJDSd1bPH1pyFjvSM3a9v8KxWNib1E+2L5fsLDSFmrbzhMxsu5wTl
+u/FlMc4btGCUyysvoigo4OR0uXcejgvnuGhBIH4TTwjJG7w7CY7UuQENBFveu9wB
+CACo7Hk9LE0I8jd/Xzp9H88VqfPURDFFCoC4oG9NTCrrjSN3HnQWbM94aRTilXIo
+ua5XcPLn98ktzpIbdg3xz0g+DaxsyxhM0k6UNdEvDdqtA1GMMi1OgtFPB5zGZv5/
+pQhgW1KMb567qIQLygI6Rzt65ifA+A3vYZVoP38HQMUENEr3tJDfFcJNqdRrjjlk
+CldUlPXPpAYqLu3XsEJm4pT0Wpypj+WUlOHpoaUzjJSRVxfpJmEjg0JNXDL1ljm9
+7bZZdpZV9Brpe0o3/oWqvxXEckZO08OTB8GY6+Wlogp8tP9Pm8r3xekgSH88hKpc
+llEUNoB2VKkoht0DzGYmK9OHABEBAAGJAmwEGAEIACAWIQTUviIxGtMTHl7aKaRh
+CS6FtyJxiQUCW9673AIbAgFACRBhCS6FtyJxicB0IAQZAQgAHRYhBPg2Slngf/6f
+TWMAWmWg7qAuMMrXBQJb3rvcAAoJEGWg7qAuMMrXA/YH/1ooLR5e5RcpfEWhP7qk
+ThRHpWcjNNL9YeeX3fyBkRvPBLTdC6+BWqgaj5DGY1PyzfVDAWk3fu+PLx5R2sHm
+apY1ybvfQjvSHe2OMO1sxdwt2MkLBmfdhBsbYbDytPq4R1dDG2iDbqSNaDvYAExY
+JUsate6PNxSWrRkY2lwmWrvI6+e/wctTBrY5PiKtTZMxpK1++/C3UUBiyg8NkGu5
+bHR2PHd5VDwB/Jm2/exC5iHPbTxtmjOwEyS+5wZhbkx6OoZhHCMN9hIvSitc8TNG
+cpRGJIwYxwJZJVpim/TImsjAubh1uyMoY5fjW4GyBK9iU2plv/bS+lKyCMYgxHam
+hzXwmgf/Q/ld9VKXKIXPWs8AeABfHYkMLeZ34laZ1Xhf1vPnktQctlukK8CzbYTU
+Wm2BZ7kn/VEhwGOmrQ1nvnoCHTCyL+HHtIt8pKHM0rtKDKFAjMXZfZEK7DR2Qqll
+/zogwy0m9Obvz/94XvdNMurUgdeTGyDwOouQcP+vd5x4LqsUtQEmwQfy4iLt7wl3
+RRLPUTLyCfqpD98UAPeqCJkIikd1Ojhr+fd3jjSoo1CDTah0auvlMSMifjggIvi9
+kE4nQISS0QqehGQ/ertNnLYWKvCgPI9fq8fQXG3mnFVR3qXhtSbIMbF1/AUye2bk
+uVVZ3WTo2oPFEVv/ZyTH9NcP4XpeuA==
+=KRyT
+-----END PGP PUBLIC KEY BLOCK-----
diff --git a/t/lib-gpg/ownertrust b/t/lib-gpg/ownertrust
new file mode 100644
index 0000000..b3e3c4f
--- /dev/null
+++ b/t/lib-gpg/ownertrust
@@ -0,0 +1,4 @@
+# List of assigned trustvalues, created Thu 11 Dec 2014 01:26:28 PM CET
+# (Use "gpg --import-ownertrust" to restore them)
+73D758744BE721698EC54E8713B6F51ECDDE430D:6:
+D4BE22311AD3131E5EDA29A461092E85B7227189:3:
diff --git a/t/lib-httpd.sh b/t/lib-httpd.sh
new file mode 100644
index 0000000..5fe3c8a
--- /dev/null
+++ b/t/lib-httpd.sh
@@ -0,0 +1,346 @@
+# Shell library to run an HTTP server for use in tests.
+# Ends the test early if httpd tests should not be run,
+# for example because the user has not enabled them.
+#
+# Usage:
+#
+# . ./test-lib.sh
+# . "$TEST_DIRECTORY"/lib-httpd.sh
+# start_httpd
+#
+# test_expect_success '...' '
+# ...
+# '
+#
+# test_expect_success ...
+#
+# test_done
+#
+# Can be configured using the following variables.
+#
+# GIT_TEST_HTTPD enable HTTPD tests
+# LIB_HTTPD_PATH web server path
+# LIB_HTTPD_MODULE_PATH web server modules path
+# LIB_HTTPD_PORT listening port
+# LIB_HTTPD_DAV enable DAV
+# LIB_HTTPD_SVN enable SVN at given location (e.g. "svn")
+# LIB_HTTPD_SSL enable SSL
+# LIB_HTTPD_PROXY enable proxy
+#
+# Copyright (c) 2008 Clemens Buchacher <drizzd@aon.at>
+#
+
+if ! test_have_prereq LIBCURL
+then
+ skip_all='skipping test, git built without http support'
+ test_done
+fi
+
+if test -n "$NO_EXPAT" && test -n "$LIB_HTTPD_DAV"
+then
+ skip_all='skipping test, git built without expat support'
+ test_done
+fi
+
+if ! test_bool_env GIT_TEST_HTTPD true
+then
+ skip_all="Network testing disabled (unset GIT_TEST_HTTPD to enable)"
+ test_done
+fi
+
+if ! test_have_prereq NOT_ROOT; then
+ test_skip_or_die GIT_TEST_HTTPD \
+ "Cannot run httpd tests as root"
+fi
+
+HTTPD_PARA=""
+
+for DEFAULT_HTTPD_PATH in '/usr/sbin/httpd' '/usr/sbin/apache2'
+do
+ if test -x "$DEFAULT_HTTPD_PATH"
+ then
+ break
+ fi
+done
+
+for DEFAULT_HTTPD_MODULE_PATH in '/usr/libexec/apache2' \
+ '/usr/lib/apache2/modules' \
+ '/usr/lib64/httpd/modules' \
+ '/usr/lib/httpd/modules' \
+ '/usr/libexec/httpd'
+do
+ if test -d "$DEFAULT_HTTPD_MODULE_PATH"
+ then
+ break
+ fi
+done
+
+case $(uname) in
+ Darwin)
+ HTTPD_PARA="$HTTPD_PARA -DDarwin"
+ ;;
+esac
+
+LIB_HTTPD_PATH=${LIB_HTTPD_PATH-"$DEFAULT_HTTPD_PATH"}
+test_set_port LIB_HTTPD_PORT
+
+TEST_PATH="$TEST_DIRECTORY"/lib-httpd
+HTTPD_ROOT_PATH="$PWD"/httpd
+HTTPD_DOCUMENT_ROOT_PATH=$HTTPD_ROOT_PATH/www
+
+# hack to suppress apache PassEnv warnings
+GIT_VALGRIND=$GIT_VALGRIND; export GIT_VALGRIND
+GIT_VALGRIND_OPTIONS=$GIT_VALGRIND_OPTIONS; export GIT_VALGRIND_OPTIONS
+GIT_TEST_SIDEBAND_ALL=$GIT_TEST_SIDEBAND_ALL; export GIT_TEST_SIDEBAND_ALL
+GIT_TRACE=$GIT_TRACE; export GIT_TRACE
+
+if ! test -x "$LIB_HTTPD_PATH"
+then
+ test_skip_or_die GIT_TEST_HTTPD "no web server found at '$LIB_HTTPD_PATH'"
+fi
+
+HTTPD_VERSION=$($LIB_HTTPD_PATH -v | \
+ sed -n 's/^Server version: Apache\/\([0-9.]*\).*$/\1/p; q')
+HTTPD_VERSION_MAJOR=$(echo $HTTPD_VERSION | cut -d. -f1)
+HTTPD_VERSION_MINOR=$(echo $HTTPD_VERSION | cut -d. -f2)
+
+if test -n "$HTTPD_VERSION_MAJOR"
+then
+ if test -z "$LIB_HTTPD_MODULE_PATH"
+ then
+ if ! test "$HTTPD_VERSION_MAJOR" -eq 2 ||
+ ! test "$HTTPD_VERSION_MINOR" -ge 4
+ then
+ test_skip_or_die GIT_TEST_HTTPD \
+ "at least Apache version 2.4 is required"
+ fi
+ if ! test -d "$DEFAULT_HTTPD_MODULE_PATH"
+ then
+ test_skip_or_die GIT_TEST_HTTPD \
+ "Apache module directory not found"
+ fi
+
+ LIB_HTTPD_MODULE_PATH="$DEFAULT_HTTPD_MODULE_PATH"
+ fi
+else
+ test_skip_or_die GIT_TEST_HTTPD \
+ "Could not identify web server at '$LIB_HTTPD_PATH'"
+fi
+
+install_script () {
+ write_script "$HTTPD_ROOT_PATH/$1" <"$TEST_PATH/$1"
+}
+
+prepare_httpd() {
+ mkdir -p "$HTTPD_DOCUMENT_ROOT_PATH"
+ cp "$TEST_PATH"/passwd "$HTTPD_ROOT_PATH"
+ cp "$TEST_PATH"/proxy-passwd "$HTTPD_ROOT_PATH"
+ install_script incomplete-length-upload-pack-v2-http.sh
+ install_script incomplete-body-upload-pack-v2-http.sh
+ install_script error-no-report.sh
+ install_script broken-smart-http.sh
+ install_script error-smart-http.sh
+ install_script error.sh
+ install_script apply-one-time-perl.sh
+ install_script nph-custom-auth.sh
+
+ ln -s "$LIB_HTTPD_MODULE_PATH" "$HTTPD_ROOT_PATH/modules"
+
+ if test -n "$LIB_HTTPD_SSL"
+ then
+ HTTPD_PROTO=https
+
+ RANDFILE_PATH="$HTTPD_ROOT_PATH"/.rnd openssl req \
+ -config "$TEST_PATH/ssl.cnf" \
+ -new -x509 -nodes \
+ -out "$HTTPD_ROOT_PATH/httpd.pem" \
+ -keyout "$HTTPD_ROOT_PATH/httpd.pem"
+ GIT_SSL_NO_VERIFY=t
+ export GIT_SSL_NO_VERIFY
+ HTTPD_PARA="$HTTPD_PARA -DSSL"
+ else
+ HTTPD_PROTO=http
+ fi
+ HTTPD_DEST=127.0.0.1:$LIB_HTTPD_PORT
+ HTTPD_URL=$HTTPD_PROTO://$HTTPD_DEST
+ HTTPD_URL_USER=$HTTPD_PROTO://user%40host@$HTTPD_DEST
+ HTTPD_URL_USER_PASS=$HTTPD_PROTO://user%40host:pass%40host@$HTTPD_DEST
+
+ if test -n "$LIB_HTTPD_DAV" || test -n "$LIB_HTTPD_SVN"
+ then
+ HTTPD_PARA="$HTTPD_PARA -DDAV"
+
+ if test -n "$LIB_HTTPD_SVN"
+ then
+ HTTPD_PARA="$HTTPD_PARA -DSVN"
+ LIB_HTTPD_SVNPATH="$rawsvnrepo"
+ svnrepo="http://127.0.0.1:$LIB_HTTPD_PORT/"
+ svnrepo="$svnrepo$LIB_HTTPD_SVN"
+ export LIB_HTTPD_SVN LIB_HTTPD_SVNPATH
+ fi
+ fi
+
+ if test -n "$LIB_HTTPD_PROXY"
+ then
+ HTTPD_PARA="$HTTPD_PARA -DPROXY"
+ fi
+}
+
+enable_http2 () {
+ HTTPD_PARA="$HTTPD_PARA -DHTTP2"
+ test_set_prereq HTTP2
+}
+
+enable_cgipassauth () {
+ # We are looking for 2.4.13 or more recent. Since we only support
+ # 2.4 and up, no need to check for older major/minor.
+ if test "$HTTPD_VERSION_MAJOR" = 2 &&
+ test "$HTTPD_VERSION_MINOR" = 4 &&
+ test "$(echo $HTTPD_VERSION | cut -d. -f3)" -lt 13
+ then
+ echo >&4 "apache $HTTPD_VERSION too old for CGIPassAuth"
+ return
+ fi
+ HTTPD_PARA="$HTTPD_PARA -DUSE_CGIPASSAUTH"
+ test_set_prereq CGIPASSAUTH
+}
+
+start_httpd() {
+ prepare_httpd >&3 2>&4
+
+ test_atexit stop_httpd
+
+ "$LIB_HTTPD_PATH" -d "$HTTPD_ROOT_PATH" \
+ -f "$TEST_PATH/apache.conf" $HTTPD_PARA \
+ -c "Listen 127.0.0.1:$LIB_HTTPD_PORT" -k start \
+ >&3 2>&4
+ if test $? -ne 0
+ then
+ cat "$HTTPD_ROOT_PATH"/error.log >&4 2>/dev/null
+ test_skip_or_die GIT_TEST_HTTPD "web server setup failed"
+ fi
+}
+
+stop_httpd() {
+ "$LIB_HTTPD_PATH" -d "$HTTPD_ROOT_PATH" \
+ -f "$TEST_PATH/apache.conf" $HTTPD_PARA -k stop
+}
+
+test_http_push_nonff () {
+ REMOTE_REPO=$1
+ LOCAL_REPO=$2
+ BRANCH=$3
+ EXPECT_CAS_RESULT=${4-failure}
+
+ test_expect_success 'non-fast-forward push fails' '
+ cd "$REMOTE_REPO" &&
+ HEAD=$(git rev-parse --verify HEAD) &&
+
+ cd "$LOCAL_REPO" &&
+ git checkout $BRANCH &&
+ echo "changed" > path2 &&
+ git commit -a -m path2 --amend &&
+
+ test_must_fail git push -v origin >output 2>&1 &&
+ (
+ cd "$REMOTE_REPO" &&
+ echo "$HEAD" >expect &&
+ git rev-parse --verify HEAD >actual &&
+ test_cmp expect actual
+ )
+ '
+
+ test_expect_success 'non-fast-forward push show ref status' '
+ grep "^ ! \[rejected\][ ]*$BRANCH -> $BRANCH (non-fast-forward)$" output
+ '
+
+ test_expect_success 'non-fast-forward push shows help message' '
+ test_grep "Updates were rejected because" output
+ '
+
+ test_expect_${EXPECT_CAS_RESULT} 'force with lease aka cas' '
+ HEAD=$( cd "$REMOTE_REPO" && git rev-parse --verify HEAD ) &&
+ test_when_finished '\''
+ (cd "$REMOTE_REPO" && git update-ref HEAD "$HEAD")
+ '\'' &&
+ (
+ cd "$LOCAL_REPO" &&
+ git push -v --force-with-lease=$BRANCH:$HEAD origin
+ ) &&
+ git rev-parse --verify "$BRANCH" >expect &&
+ (
+ cd "$REMOTE_REPO" && git rev-parse --verify HEAD
+ ) >actual &&
+ test_cmp expect actual
+ '
+}
+
+setup_askpass_helper() {
+ test_expect_success 'setup askpass helper' '
+ write_script "$TRASH_DIRECTORY/askpass" <<-\EOF &&
+ echo >>"$TRASH_DIRECTORY/askpass-query" "askpass: $*" &&
+ case "$*" in
+ *Username*)
+ what=user
+ ;;
+ *Password*)
+ what=pass
+ ;;
+ esac &&
+ cat "$TRASH_DIRECTORY/askpass-$what"
+ EOF
+ GIT_ASKPASS="$TRASH_DIRECTORY/askpass" &&
+ export GIT_ASKPASS &&
+ export TRASH_DIRECTORY
+ '
+}
+
+set_askpass() {
+ >"$TRASH_DIRECTORY/askpass-query" &&
+ echo "$1" >"$TRASH_DIRECTORY/askpass-user" &&
+ echo "$2" >"$TRASH_DIRECTORY/askpass-pass"
+}
+
+expect_askpass() {
+ dest=$HTTPD_DEST${3+/$3}
+
+ {
+ case "$1" in
+ none)
+ ;;
+ pass)
+ echo "askpass: Password for '$HTTPD_PROTO://$2@$dest': "
+ ;;
+ both)
+ echo "askpass: Username for '$HTTPD_PROTO://$dest': "
+ echo "askpass: Password for '$HTTPD_PROTO://$2@$dest': "
+ ;;
+ *)
+ false
+ ;;
+ esac
+ } >"$TRASH_DIRECTORY/askpass-expect" &&
+ test_cmp "$TRASH_DIRECTORY/askpass-expect" \
+ "$TRASH_DIRECTORY/askpass-query"
+}
+
+strip_access_log() {
+ sed -e "
+ s/^.* \"//
+ s/\"//
+ s/ [1-9][0-9]*\$//
+ s/^GET /GET /
+ " "$HTTPD_ROOT_PATH"/access.log
+}
+
+# Requires one argument: the name of a file containing the expected stripped
+# access log entries.
+check_access_log() {
+ sort "$1" >"$1".sorted &&
+ strip_access_log >access.log.stripped &&
+ sort access.log.stripped >access.log.sorted &&
+ if ! test_cmp "$1".sorted access.log.sorted
+ then
+ test_cmp "$1" access.log.stripped
+ fi
+}
diff --git a/t/lib-httpd/apache.conf b/t/lib-httpd/apache.conf
new file mode 100644
index 0000000..022276a
--- /dev/null
+++ b/t/lib-httpd/apache.conf
@@ -0,0 +1,292 @@
+ServerName dummy
+PidFile httpd.pid
+DocumentRoot www
+LogFormat "%h %l %u %t \"%r\" %>s %b" common
+CustomLog access.log common
+ErrorLog error.log
+<IfModule !mod_log_config.c>
+ LoadModule log_config_module modules/mod_log_config.so
+</IfModule>
+<IfModule !mod_alias.c>
+ LoadModule alias_module modules/mod_alias.so
+</IfModule>
+<IfModule !mod_cgi.c>
+ LoadModule cgi_module modules/mod_cgi.so
+</IfModule>
+<IfModule !mod_env.c>
+ LoadModule env_module modules/mod_env.so
+</IfModule>
+<IfModule !mod_rewrite.c>
+ LoadModule rewrite_module modules/mod_rewrite.so
+</IFModule>
+<IfModule !mod_version.c>
+ LoadModule version_module modules/mod_version.so
+</IfModule>
+<IfModule !mod_headers.c>
+ LoadModule headers_module modules/mod_headers.so
+</IfModule>
+<IfModule !mod_setenvif.c>
+ LoadModule setenvif_module modules/mod_setenvif.so
+</IfModule>
+
+<IfDefine HTTP2>
+LoadModule http2_module modules/mod_http2.so
+Protocols h2 h2c
+</IfDefine>
+
+<IfModule !mod_auth_basic.c>
+ LoadModule auth_basic_module modules/mod_auth_basic.so
+</IfModule>
+<IfModule !mod_authn_file.c>
+ LoadModule authn_file_module modules/mod_authn_file.so
+</IfModule>
+<IfModule !mod_authz_user.c>
+ LoadModule authz_user_module modules/mod_authz_user.so
+</IfModule>
+<IfModule !mod_authz_host.c>
+ LoadModule authz_host_module modules/mod_authz_host.so
+</IfModule>
+
+<IfDefine PROXY>
+<IfModule !mod_proxy.c>
+ LoadModule proxy_module modules/mod_proxy.so
+</IfModule>
+<IfModule !mod_proxy_http.c>
+ LoadModule proxy_http_module modules/mod_proxy_http.so
+</IfModule>
+ProxyRequests On
+<Proxy "*">
+ AuthType Basic
+ AuthName "proxy-auth"
+ AuthUserFile proxy-passwd
+ Require valid-user
+</Proxy>
+</IfDefine>
+
+<IfModule !mod_authn_core.c>
+ LoadModule authn_core_module modules/mod_authn_core.so
+</IfModule>
+<IfModule !mod_authz_core.c>
+ LoadModule authz_core_module modules/mod_authz_core.so
+</IfModule>
+<IfModule !mod_access_compat.c>
+ LoadModule access_compat_module modules/mod_access_compat.so
+</IfModule>
+<IfModule !mod_unixd.c>
+ LoadModule unixd_module modules/mod_unixd.so
+</IfModule>
+
+<IfDefine HTTP2>
+<IfModule !mod_mpm_event.c>
+ LoadModule mpm_event_module modules/mod_mpm_event.so
+</IfModule>
+</IfDefine>
+<IfDefine !HTTP2>
+<IfModule !mod_mpm_prefork.c>
+ LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
+</IfModule>
+</IfDefine>
+
+PassEnv GIT_VALGRIND
+PassEnv GIT_VALGRIND_OPTIONS
+PassEnv GNUPGHOME
+PassEnv ASAN_OPTIONS
+PassEnv LSAN_OPTIONS
+PassEnv UBSAN_OPTIONS
+PassEnv GIT_TRACE
+PassEnv GIT_CONFIG_NOSYSTEM
+PassEnv GIT_TEST_SIDEBAND_ALL
+PassEnv LANG
+PassEnv LC_ALL
+
+Alias /dumb/ www/
+Alias /auth/dumb/ www/auth/dumb/
+
+SetEnv PERL_PATH ${PERL_PATH}
+
+<LocationMatch /smart/>
+ SetEnv GIT_EXEC_PATH ${GIT_EXEC_PATH}
+ SetEnv GIT_HTTP_EXPORT_ALL
+</LocationMatch>
+<LocationMatch /smart_noexport/>
+ SetEnv GIT_EXEC_PATH ${GIT_EXEC_PATH}
+</LocationMatch>
+<LocationMatch /smart_custom_env/>
+ SetEnv GIT_EXEC_PATH ${GIT_EXEC_PATH}
+ SetEnv GIT_HTTP_EXPORT_ALL
+ SetEnv GIT_COMMITTER_NAME "Custom User"
+ SetEnv GIT_COMMITTER_EMAIL custom@example.com
+</LocationMatch>
+<LocationMatch /smart_namespace/>
+ SetEnv GIT_EXEC_PATH ${GIT_EXEC_PATH}
+ SetEnv GIT_HTTP_EXPORT_ALL
+ SetEnv GIT_NAMESPACE ns
+</LocationMatch>
+<LocationMatch /smart_cookies/>
+ SetEnv GIT_EXEC_PATH ${GIT_EXEC_PATH}
+ SetEnv GIT_HTTP_EXPORT_ALL
+ Header set Set-Cookie name=value
+</LocationMatch>
+<LocationMatch /smart_headers/>
+ <RequireAll>
+ Require expr %{HTTP:x-magic-one} == 'abra'
+ Require expr %{HTTP:x-magic-two} == 'cadabra'
+ </RequireAll>
+ SetEnv GIT_EXEC_PATH ${GIT_EXEC_PATH}
+ SetEnv GIT_HTTP_EXPORT_ALL
+</LocationMatch>
+<LocationMatch /one_time_perl/>
+ SetEnv GIT_EXEC_PATH ${GIT_EXEC_PATH}
+ SetEnv GIT_HTTP_EXPORT_ALL
+</LocationMatch>
+<LocationMatch /smart_v0/>
+ SetEnv GIT_EXEC_PATH ${GIT_EXEC_PATH}
+ SetEnv GIT_HTTP_EXPORT_ALL
+ SetEnv GIT_PROTOCOL
+</LocationMatch>
+<LocationMatch /custom_auth/>
+ SetEnv GIT_EXEC_PATH ${GIT_EXEC_PATH}
+ SetEnv GIT_HTTP_EXPORT_ALL
+ <IfDefine USE_CGIPASSAUTH>
+ CGIPassAuth on
+ </IfDefine>
+</LocationMatch>
+ScriptAlias /smart/incomplete_length/git-upload-pack incomplete-length-upload-pack-v2-http.sh/
+ScriptAlias /smart/incomplete_body/git-upload-pack incomplete-body-upload-pack-v2-http.sh/
+ScriptAlias /smart/no_report/git-receive-pack error-no-report.sh/
+ScriptAliasMatch /error_git_upload_pack/(.*)/git-upload-pack error.sh/
+ScriptAliasMatch /smart_*[^/]*/(.*) ${GIT_EXEC_PATH}/git-http-backend/$1
+ScriptAlias /broken_smart/ broken-smart-http.sh/
+ScriptAlias /error_smart/ error-smart-http.sh/
+ScriptAlias /error/ error.sh/
+ScriptAliasMatch /one_time_perl/(.*) apply-one-time-perl.sh/$1
+ScriptAliasMatch /custom_auth/(.*) nph-custom-auth.sh/$1
+<Directory ${GIT_EXEC_PATH}>
+ Options FollowSymlinks
+</Directory>
+<Files incomplete-length-upload-pack-v2-http.sh>
+ Options ExecCGI
+</Files>
+<Files incomplete-body-upload-pack-v2-http.sh>
+ Options ExecCGI
+</Files>
+<Files error-no-report.sh>
+ Options ExecCGI
+</Files>
+<Files broken-smart-http.sh>
+ Options ExecCGI
+</Files>
+<Files error-smart-http.sh>
+ Options ExecCGI
+</Files>
+<Files error.sh>
+ Options ExecCGI
+</Files>
+<Files apply-one-time-perl.sh>
+ Options ExecCGI
+</Files>
+<Files ${GIT_EXEC_PATH}/git-http-backend>
+ Options ExecCGI
+</Files>
+
+RewriteEngine on
+RewriteRule ^/dumb-redir/(.*)$ /dumb/$1 [R=301]
+RewriteRule ^/smart-redir-perm/(.*)$ /smart/$1 [R=301]
+RewriteRule ^/smart-redir-temp/(.*)$ /smart/$1 [R=302]
+RewriteRule ^/smart-redir-auth/(.*)$ /auth/smart/$1 [R=301]
+RewriteRule ^/smart-redir-limited/(.*)/info/refs$ /smart/$1/info/refs [R=301]
+RewriteRule ^/ftp-redir/(.*)$ ftp://localhost:1000/$1 [R=302]
+
+RewriteRule ^/loop-redir/x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-(.*) /$1 [R=302]
+RewriteRule ^/loop-redir/(.*)$ /loop-redir/x-$1 [R=302]
+
+# redir-to/502/x?y -> really-redir-to?path=502/x&qs=y which returns 502
+# redir-to/x?y -> really-redir-to?path=x&qs=y -> x?y
+RewriteCond %{QUERY_STRING} ^(.*)$
+RewriteRule ^/redir-to/(.*)$ /really-redir-to?path=$1&qs=%1 [R=302]
+RewriteCond %{QUERY_STRING} ^path=502/(.*)&qs=(.*)$
+RewriteRule ^/really-redir-to$ - [R=502,L]
+RewriteCond %{QUERY_STRING} ^path=(.*)&qs=(.*)$
+RewriteRule ^/really-redir-to$ /%1?%2 [R=302]
+
+# The first rule issues a client-side redirect to something
+# that _doesn't_ look like a git repo. The second rule is a
+# server-side rewrite, so that it turns out the odd-looking
+# thing _is_ a git repo. The "[PT]" tells Apache to match
+# the usual ScriptAlias rules for /smart.
+RewriteRule ^/insane-redir/(.*)$ /intern-redir/$1/foo [R=301]
+RewriteRule ^/intern-redir/(.*)/foo$ /smart/$1 [PT]
+
+# Serve info/refs internally without redirecting, but
+# issue a redirect for any object requests.
+RewriteRule ^/redir-objects/(.*/info/refs)$ /dumb/$1 [PT]
+RewriteRule ^/redir-objects/(.*/objects/.*)$ /dumb/$1 [R=301]
+
+<IfDefine SSL>
+LoadModule ssl_module modules/mod_ssl.so
+
+SSLCertificateFile httpd.pem
+SSLCertificateKeyFile httpd.pem
+SSLRandomSeed startup file:/dev/urandom 512
+SSLRandomSeed connect file:/dev/urandom 512
+SSLSessionCache none
+SSLEngine On
+</IfDefine>
+
+<Location /auth/>
+ AuthType Basic
+ AuthName "git-auth"
+ AuthUserFile passwd
+ Require valid-user
+</Location>
+
+<LocationMatch "^/auth-push/.*/git-receive-pack$">
+ AuthType Basic
+ AuthName "git-auth"
+ AuthUserFile passwd
+ Require valid-user
+</LocationMatch>
+
+<LocationMatch "^/auth-fetch/.*/git-upload-pack$">
+ AuthType Basic
+ AuthName "git-auth"
+ AuthUserFile passwd
+ Require valid-user
+</LocationMatch>
+
+RewriteCond %{QUERY_STRING} service=git-receive-pack [OR]
+RewriteCond %{REQUEST_URI} /git-receive-pack$
+RewriteRule ^/half-auth-complete/ - [E=AUTHREQUIRED:yes]
+
+<Location /half-auth-complete/>
+ Order Deny,Allow
+ Deny from env=AUTHREQUIRED
+
+ AuthType Basic
+ AuthName "Git Access"
+ AuthUserFile passwd
+ Require valid-user
+ Satisfy Any
+</Location>
+
+<IfDefine DAV>
+ LoadModule dav_module modules/mod_dav.so
+ LoadModule dav_fs_module modules/mod_dav_fs.so
+
+ DAVLockDB DAVLock
+ <Location /dumb/>
+ Dav on
+ </Location>
+ <Location /auth/dumb>
+ Dav on
+ </Location>
+</IfDefine>
+
+<IfDefine SVN>
+ LoadModule dav_svn_module modules/mod_dav_svn.so
+
+ <Location /${LIB_HTTPD_SVN}>
+ DAV svn
+ SVNPath "${LIB_HTTPD_SVNPATH}"
+ </Location>
+</IfDefine>
diff --git a/t/lib-httpd/apply-one-time-perl.sh b/t/lib-httpd/apply-one-time-perl.sh
new file mode 100644
index 0000000..d7f9fed
--- /dev/null
+++ b/t/lib-httpd/apply-one-time-perl.sh
@@ -0,0 +1,27 @@
+#!/bin/sh
+
+# If "one-time-perl" exists in $HTTPD_ROOT_PATH, run perl on the HTTP response,
+# using the contents of "one-time-perl" as the perl command to be run. If the
+# response was modified as a result, delete "one-time-perl" so that subsequent
+# HTTP responses are no longer modified.
+#
+# This can be used to simulate the effects of the repository changing in
+# between HTTP request-response pairs.
+if test -f one-time-perl
+then
+ LC_ALL=C
+ export LC_ALL
+
+ "$GIT_EXEC_PATH/git-http-backend" >out
+ "$PERL_PATH" -pe "$(cat one-time-perl)" out >out_modified
+
+ if cmp -s out out_modified
+ then
+ cat out
+ else
+ cat out_modified
+ rm one-time-perl
+ fi
+else
+ "$GIT_EXEC_PATH/git-http-backend"
+fi
diff --git a/t/lib-httpd/broken-smart-http.sh b/t/lib-httpd/broken-smart-http.sh
new file mode 100644
index 0000000..82cc610
--- /dev/null
+++ b/t/lib-httpd/broken-smart-http.sh
@@ -0,0 +1,10 @@
+printf "Content-Type: text/%s\n" "html"
+echo
+printf "%s\n" "001e# service=git-upload-pack"
+printf "%s" "0000"
+printf "%s%c%s%s\n" \
+ "00a58681d9f286a48b08f37b3a095330da16689e3693 HEAD" \
+ 0 \
+ " include-tag multi_ack_detailed multi_ack ofs-delta" \
+ " side-band side-band-64k thin-pack no-progress shallow no-done "
+printf "%s" "0000"
diff --git a/t/lib-httpd/error-no-report.sh b/t/lib-httpd/error-no-report.sh
new file mode 100644
index 0000000..39ff75b
--- /dev/null
+++ b/t/lib-httpd/error-no-report.sh
@@ -0,0 +1,6 @@
+echo "Content-Type: application/x-git-receive-pack-result"
+echo
+printf '0013\001000eunpack ok\n'
+printf '0015\002skipping report\n'
+printf '0009\0010000'
+printf '0000'
diff --git a/t/lib-httpd/error-smart-http.sh b/t/lib-httpd/error-smart-http.sh
new file mode 100644
index 0000000..e65d447
--- /dev/null
+++ b/t/lib-httpd/error-smart-http.sh
@@ -0,0 +1,3 @@
+echo "Content-Type: application/x-git-upload-pack-advertisement"
+echo
+printf "%s" "0019ERR server-side error"
diff --git a/t/lib-httpd/error.sh b/t/lib-httpd/error.sh
new file mode 100755
index 0000000..a77b8e5
--- /dev/null
+++ b/t/lib-httpd/error.sh
@@ -0,0 +1,31 @@
+#!/bin/sh
+
+printf "Status: 500 Intentional Breakage\n"
+
+printf "Content-Type: "
+charset=iso-8859-1
+case "$PATH_INFO" in
+*html*)
+ printf "text/html"
+ ;;
+*text*)
+ printf "text/plain"
+ ;;
+*charset*)
+ printf "text/plain; charset=utf-8"
+ charset=utf-8
+ ;;
+*utf16*)
+ printf "text/plain; charset=utf-16"
+ charset=utf-16
+ ;;
+*odd-spacing*)
+ printf "text/plain; foo=bar ;charset=utf-16; other=nonsense"
+ charset=utf-16
+ ;;
+esac
+printf "\n"
+
+printf "\n"
+printf "this is the error message\n" |
+iconv -f us-ascii -t $charset
diff --git a/t/lib-httpd/incomplete-body-upload-pack-v2-http.sh b/t/lib-httpd/incomplete-body-upload-pack-v2-http.sh
new file mode 100644
index 0000000..90e73ef
--- /dev/null
+++ b/t/lib-httpd/incomplete-body-upload-pack-v2-http.sh
@@ -0,0 +1,3 @@
+printf "Content-Type: text/%s\n" "application/x-git-upload-pack-result"
+echo
+printf "%s%s" "0079" "45"
diff --git a/t/lib-httpd/incomplete-length-upload-pack-v2-http.sh b/t/lib-httpd/incomplete-length-upload-pack-v2-http.sh
new file mode 100644
index 0000000..dce552e
--- /dev/null
+++ b/t/lib-httpd/incomplete-length-upload-pack-v2-http.sh
@@ -0,0 +1,3 @@
+printf "Content-Type: text/%s\n" "application/x-git-upload-pack-result"
+echo
+printf "%s" "00"
diff --git a/t/lib-httpd/nph-custom-auth.sh b/t/lib-httpd/nph-custom-auth.sh
new file mode 100644
index 0000000..f5345e7
--- /dev/null
+++ b/t/lib-httpd/nph-custom-auth.sh
@@ -0,0 +1,39 @@
+#!/bin/sh
+
+VALID_CREDS_FILE=custom-auth.valid
+CHALLENGE_FILE=custom-auth.challenge
+
+#
+# If $VALID_CREDS_FILE exists in $HTTPD_ROOT_PATH, consider each line as a valid
+# credential for the current request. Each line in the file is considered a
+# valid HTTP Authorization header value. For example:
+#
+# Basic YWxpY2U6c2VjcmV0LXBhc3N3ZA==
+#
+# If $CHALLENGE_FILE exists in $HTTPD_ROOT_PATH, output the contents as headers
+# in a 401 response if no valid authentication credentials were included in the
+# request. For example:
+#
+# WWW-Authenticate: Bearer authorize_uri="id.example.com" p=1 q=0
+# WWW-Authenticate: Basic realm="example.com"
+#
+
+if test -n "$HTTP_AUTHORIZATION" && \
+ grep -Fqsx "${HTTP_AUTHORIZATION}" "$VALID_CREDS_FILE"
+then
+ # Note that although git-http-backend returns a status line, it
+ # does so using a CGI 'Status' header. Because this script is an
+ # No Parsed Headers (NPH) script, we must return a real HTTP
+ # status line.
+ # This is only a test script, so we don't bother to check for
+ # the actual status from git-http-backend and always return 200.
+ echo 'HTTP/1.1 200 OK'
+ exec "$GIT_EXEC_PATH"/git-http-backend
+fi
+
+echo 'HTTP/1.1 401 Authorization Required'
+if test -f "$CHALLENGE_FILE"
+then
+ cat "$CHALLENGE_FILE"
+fi
+echo
diff --git a/t/lib-httpd/passwd b/t/lib-httpd/passwd
new file mode 100644
index 0000000..99a34d6
--- /dev/null
+++ b/t/lib-httpd/passwd
@@ -0,0 +1 @@
+user@host:xb4E8pqD81KQs
diff --git a/t/lib-httpd/proxy-passwd b/t/lib-httpd/proxy-passwd
new file mode 100644
index 0000000..77c2513
--- /dev/null
+++ b/t/lib-httpd/proxy-passwd
@@ -0,0 +1 @@
+proxuser:2x7tAukjAED5M
diff --git a/t/lib-httpd/ssl.cnf b/t/lib-httpd/ssl.cnf
new file mode 100644
index 0000000..812e825
--- /dev/null
+++ b/t/lib-httpd/ssl.cnf
@@ -0,0 +1,8 @@
+RANDFILE = $ENV::RANDFILE_PATH
+
+[ req ]
+default_bits = 2048
+distinguished_name = req_distinguished_name
+prompt = no
+[ req_distinguished_name ]
+commonName = 127.0.0.1
diff --git a/t/lib-log-graph.sh b/t/lib-log-graph.sh
new file mode 100644
index 0000000..bf952ef
--- /dev/null
+++ b/t/lib-log-graph.sh
@@ -0,0 +1,28 @@
+# Helps shared by the test scripts for comparing log graphs.
+
+sanitize_log_output () {
+ sed -e 's/ *$//' \
+ -e 's/commit [0-9a-f]*$/commit COMMIT_OBJECT_NAME/' \
+ -e 's/Merge: [ 0-9a-f]*$/Merge: MERGE_PARENTS/' \
+ -e 's/Merge tag.*/Merge HEADS DESCRIPTION/' \
+ -e 's/Merge commit.*/Merge HEADS DESCRIPTION/' \
+ -e 's/index [0-9a-f]*\.\.[0-9a-f]*/index BEFORE..AFTER/'
+}
+
+lib_test_cmp_graph () {
+ git log --graph "$@" >output &&
+ sed 's/ *$//' >output.sanitized <output &&
+ test_cmp expect output.sanitized
+}
+
+lib_test_cmp_short_graph () {
+ git log --graph --pretty=short "$@" >output &&
+ sanitize_log_output >output.sanitized <output &&
+ test_cmp expect output.sanitized
+}
+
+lib_test_cmp_colored_graph () {
+ git log --graph --color=always "$@" >output.colors.raw &&
+ test_decode_color <output.colors.raw | sed "s/ *\$//" >output.colors &&
+ test_cmp expect.colors output.colors
+}
diff --git a/t/lib-merge.sh b/t/lib-merge.sh
new file mode 100644
index 0000000..8734ebf
--- /dev/null
+++ b/t/lib-merge.sh
@@ -0,0 +1,13 @@
+# Helper functions used by merge tests.
+
+test_expect_merge_algorithm () {
+ status_for_recursive=$1 status_for_ort=$2
+ shift 2
+
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ test_expect_${status_for_ort} "$@"
+ else
+ test_expect_${status_for_recursive} "$@"
+ fi
+}
diff --git a/t/lib-midx.sh b/t/lib-midx.sh
new file mode 100644
index 0000000..1261994
--- /dev/null
+++ b/t/lib-midx.sh
@@ -0,0 +1,8 @@
+# test_midx_consistent <objdir>
+test_midx_consistent () {
+ ls $1/pack/pack-*.idx | xargs -n 1 basename | sort >expect &&
+ test-tool read-midx $1 | grep ^pack-.*\.idx$ | sort >actual &&
+
+ test_cmp expect actual &&
+ git multi-pack-index --object-dir=$1 verify
+}
diff --git a/t/lib-pack.sh b/t/lib-pack.sh
new file mode 100644
index 0000000..bb8938c
--- /dev/null
+++ b/t/lib-pack.sh
@@ -0,0 +1,140 @@
+# Support routines for hand-crafting weird or malicious packs.
+#
+# You can make a complete pack like:
+#
+# pack_header 2 >foo.pack &&
+# pack_obj e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 >>foo.pack &&
+# pack_obj e68fe8129b546b101aee9510c5328e7f21ca1d18 >>foo.pack &&
+# pack_trailer foo.pack
+
+# Print the big-endian 4-byte octal representation of $1
+uint32_octal () {
+ n=$1
+ printf '\\%o' $(($n / 16777216)); n=$((n % 16777216))
+ printf '\\%o' $(($n / 65536)); n=$((n % 65536))
+ printf '\\%o' $(($n / 256)); n=$((n % 256))
+ printf '\\%o' $(($n ));
+}
+
+# Print the big-endian 4-byte binary representation of $1
+uint32_binary () {
+ printf "$(uint32_octal "$1")"
+}
+
+# Print a pack header, version 2, for a pack with $1 objects
+pack_header () {
+ printf 'PACK' &&
+ printf '\0\0\0\2' &&
+ uint32_binary "$1"
+}
+
+# Print the pack data for object $1, as a delta against object $2 (or as a full
+# object if $2 is missing or empty). The output is suitable for including
+# directly in the packfile, and represents the entirety of the object entry.
+# Doing this on the fly (especially picking your deltas) is quite tricky, so we
+# have hardcoded some well-known objects. See the case statements below for the
+# complete list.
+pack_obj () {
+ case "$1" in
+ # empty blob
+ $EMPTY_BLOB)
+ case "$2" in
+ '')
+ printf '\060\170\234\003\0\0\0\0\1'
+ return
+ ;;
+ esac
+ ;;
+
+ # blob containing "\7\76"
+ $(test_oid packlib_7_76))
+ case "$2" in
+ '')
+ printf '\062\170\234\143\267\3\0\0\116\0\106'
+ return
+ ;;
+ 01d7713666f4de822776c7622c10f1b07de280dc)
+ printf '\165\1\327\161\66\146\364\336\202\47\166' &&
+ printf '\307\142\54\20\361\260\175\342\200\334\170' &&
+ printf '\234\143\142\142\142\267\003\0\0\151\0\114'
+ return
+ ;;
+ 37c8e2c15bb22b912e59b43fd51a4f7e9465ed0b5084c5a1411d991cbe630683)
+ printf '\165\67\310\342\301\133\262\53\221\56\131' &&
+ printf '\264\77\325\32\117\176\224\145\355\13\120' &&
+ printf '\204\305\241\101\35\231\34\276\143\6\203\170' &&
+ printf '\234\143\142\142\142\267\003\0\0\151\0\114'
+ return
+ ;;
+ esac
+ ;;
+
+ # blob containing "\7\0"
+ $(test_oid packlib_7_0))
+ case "$2" in
+ '')
+ printf '\062\170\234\143\147\0\0\0\20\0\10'
+ return
+ ;;
+ e68fe8129b546b101aee9510c5328e7f21ca1d18)
+ printf '\165\346\217\350\22\233\124\153\20\32\356' &&
+ printf '\225\20\305\62\216\177\41\312\35\30\170\234' &&
+ printf '\143\142\142\142\147\0\0\0\53\0\16'
+ return
+ ;;
+ 5d8e6fc40f2dab00e6983a48523fe57e621f46434cb58dbd4422fba03380d886)
+ printf '\165\135\216\157\304\17\55\253\0\346\230\72' &&
+ printf '\110\122\77\345\176\142\37\106\103\114\265' &&
+ printf '\215\275\104\42\373\240\63\200\330\206\170\234' &&
+ printf '\143\142\142\142\147\0\0\0\53\0\16'
+ return
+ ;;
+ esac
+ ;;
+ # blob containing "\3\326"
+ 471819e8c52bf11513f100b2810a8aa0622d5cd3d1c913758a071dd4b3bad8fe)
+ case "$2" in
+ '')
+ printf '\062\170\234\143\276\006\000\000\336\000\332'
+ return
+ ;;
+ esac
+ esac
+
+ # If it's not a delta, we can convince pack-objects to generate a pack
+ # with just our entry, and then strip off the header (12 bytes) and
+ # trailer (20 bytes).
+ if test -z "$2"
+ then
+ echo "$1" | git pack-objects --stdout >pack_obj.tmp &&
+ size=$(wc -c <pack_obj.tmp) &&
+ dd if=pack_obj.tmp bs=1 count=$((size - $(test_oid rawsz) - 12)) skip=12 &&
+ rm -f pack_obj.tmp
+ return
+ fi
+
+ echo >&2 "BUG: don't know how to print $1${2:+ (from $2)}"
+ return 1
+}
+
+# Compute and append pack trailer to "$1"
+pack_trailer () {
+ test-tool $(test_oid algo) -b <"$1" >trailer.tmp &&
+ cat trailer.tmp >>"$1" &&
+ rm -f trailer.tmp
+}
+
+# Remove any existing packs to make sure that
+# whatever we index next will be the pack that we
+# actually use.
+clear_packs () {
+ rm -f .git/objects/pack/*
+}
+
+test_oid_cache <<-EOF
+packlib_7_0 sha1:01d7713666f4de822776c7622c10f1b07de280dc
+packlib_7_0 sha256:37c8e2c15bb22b912e59b43fd51a4f7e9465ed0b5084c5a1411d991cbe630683
+
+packlib_7_76 sha1:e68fe8129b546b101aee9510c5328e7f21ca1d18
+packlib_7_76 sha256:5d8e6fc40f2dab00e6983a48523fe57e621f46434cb58dbd4422fba03380d886
+EOF
diff --git a/t/lib-pager.sh b/t/lib-pager.sh
new file mode 100644
index 0000000..e5eb28d
--- /dev/null
+++ b/t/lib-pager.sh
@@ -0,0 +1,15 @@
+# Helpers for tests of git's choice of pager.
+
+test_expect_success 'determine default pager' '
+ test_might_fail git config --unset core.pager &&
+ less=$(
+ sane_unset PAGER GIT_PAGER &&
+ git var GIT_PAGER
+ ) &&
+ test -n "$less"
+'
+
+if expr "$less" : '[a-z][a-z]*$' >/dev/null
+then
+ test_set_prereq SIMPLEPAGER
+fi
diff --git a/t/lib-parallel-checkout.sh b/t/lib-parallel-checkout.sh
new file mode 100644
index 0000000..acaee9c
--- /dev/null
+++ b/t/lib-parallel-checkout.sh
@@ -0,0 +1,49 @@
+# Helpers for tests invoking parallel-checkout
+
+# Parallel checkout tests need full control of the number of workers
+unset GIT_TEST_CHECKOUT_WORKERS
+
+set_checkout_config () {
+ if test $# -ne 2
+ then
+ BUG "usage: set_checkout_config <workers> <threshold>"
+ fi &&
+
+ test_config_global checkout.workers $1 &&
+ test_config_global checkout.thresholdForParallelism $2
+}
+
+# Run "${@:2}" and check that $1 checkout workers were used
+test_checkout_workers () {
+ if test $# -lt 2
+ then
+ BUG "too few arguments to test_checkout_workers"
+ fi &&
+
+ local expected_workers=$1 &&
+ shift &&
+
+ local trace_file=trace-test-checkout-workers &&
+ rm -f "$trace_file" &&
+ (
+ GIT_TRACE2="$(pwd)/$trace_file" &&
+ export GIT_TRACE2 &&
+ "$@" 2>&8
+ ) &&
+
+ local workers="$(grep "child_start\[..*\] git checkout--worker" "$trace_file" | wc -l)" &&
+ test $workers -eq $expected_workers &&
+ rm "$trace_file"
+} 8>&2 2>&4
+
+# Verify that both the working tree and the index were created correctly
+verify_checkout () {
+ if test $# -ne 1
+ then
+ BUG "usage: verify_checkout <repository path>"
+ fi &&
+
+ git -C "$1" diff-index --ignore-submodules=none --exit-code HEAD -- &&
+ git -C "$1" status --porcelain >"$1".status &&
+ test_must_be_empty "$1".status
+}
diff --git a/t/lib-patch-mode.sh b/t/lib-patch-mode.sh
new file mode 100644
index 0000000..89ca1f7
--- /dev/null
+++ b/t/lib-patch-mode.sh
@@ -0,0 +1,55 @@
+: included from t2016 and others
+
+. ./test-lib.sh
+
+# set_state <path> <worktree-content> <index-content>
+#
+# Prepare the content for path in worktree and the index as specified.
+set_state () {
+ echo "$3" > "$1" &&
+ git add "$1" &&
+ echo "$2" > "$1"
+}
+
+# save_state <path>
+#
+# Save index/worktree content of <path> in the files _worktree_<path>
+# and _index_<path>
+save_state () {
+ noslash="$(echo "$1" | tr / _)" &&
+ cat "$1" > _worktree_"$noslash" &&
+ git show :"$1" > _index_"$noslash"
+}
+
+# set_and_save_state <path> <worktree-content> <index-content>
+set_and_save_state () {
+ set_state "$@" &&
+ save_state "$1"
+}
+
+# verify_state <path> <expected-worktree-content> <expected-index-content>
+verify_state () {
+ echo "$2" >expect &&
+ test_cmp expect "$1" &&
+
+ echo "$3" >expect &&
+ git show :"$1" >actual &&
+ test_cmp expect actual
+}
+
+# verify_saved_state <path>
+#
+# Call verify_state with expected contents from the last save_state
+verify_saved_state () {
+ noslash="$(echo "$1" | tr / _)" &&
+ verify_state "$1" "$(cat _worktree_"$noslash")" "$(cat _index_"$noslash")"
+}
+
+save_head () {
+ git rev-parse HEAD > _head
+}
+
+verify_saved_head () {
+ git rev-parse HEAD >actual &&
+ test_cmp _head actual
+}
diff --git a/t/lib-perl.sh b/t/lib-perl.sh
new file mode 100644
index 0000000..d0bf509
--- /dev/null
+++ b/t/lib-perl.sh
@@ -0,0 +1,19 @@
+# Copyright (c) 2022 Ævar Arnfjörð Bjarmason
+
+test_lazy_prereq PERL_TEST_MORE '
+ perl -MTest::More -e 0
+'
+
+skip_all_if_no_Test_More () {
+ if ! test_have_prereq PERL
+ then
+ skip_all='skipping perl interface tests, perl not available'
+ test_done
+ fi
+
+ if ! test_have_prereq PERL_TEST_MORE
+ then
+ skip_all="Perl Test::More unavailable, skipping test"
+ test_done
+ fi
+}
diff --git a/t/lib-proto-disable.sh b/t/lib-proto-disable.sh
new file mode 100644
index 0000000..890622b
--- /dev/null
+++ b/t/lib-proto-disable.sh
@@ -0,0 +1,220 @@
+# Test routines for checking protocol disabling.
+
+# Test clone/fetch/push with GIT_ALLOW_PROTOCOL environment variable
+test_allow_var () {
+ desc=$1
+ proto=$2
+ url=$3
+
+ test_expect_success "clone $desc (enabled)" '
+ rm -rf tmp.git &&
+ (
+ GIT_ALLOW_PROTOCOL=$proto &&
+ export GIT_ALLOW_PROTOCOL &&
+ git clone --bare "$url" tmp.git
+ )
+ '
+
+ test_expect_success "fetch $desc (enabled)" '
+ (
+ cd tmp.git &&
+ GIT_ALLOW_PROTOCOL=$proto &&
+ export GIT_ALLOW_PROTOCOL &&
+ git fetch
+ )
+ '
+
+ test_expect_success "push $desc (enabled)" '
+ (
+ cd tmp.git &&
+ GIT_ALLOW_PROTOCOL=$proto &&
+ export GIT_ALLOW_PROTOCOL &&
+ git push origin HEAD:pushed
+ )
+ '
+
+ test_expect_success "push $desc (disabled)" '
+ (
+ cd tmp.git &&
+ GIT_ALLOW_PROTOCOL=none &&
+ export GIT_ALLOW_PROTOCOL &&
+ test_must_fail git push origin HEAD:pushed
+ )
+ '
+
+ test_expect_success "fetch $desc (disabled)" '
+ (
+ cd tmp.git &&
+ GIT_ALLOW_PROTOCOL=none &&
+ export GIT_ALLOW_PROTOCOL &&
+ test_must_fail git fetch
+ )
+ '
+
+ test_expect_success "clone $desc (disabled)" '
+ rm -rf tmp.git &&
+ (
+ GIT_ALLOW_PROTOCOL=none &&
+ export GIT_ALLOW_PROTOCOL &&
+ test_must_fail git clone --bare "$url" tmp.git
+ )
+ '
+
+ test_expect_success "clone $desc (env var has precedence)" '
+ rm -rf tmp.git &&
+ (
+ GIT_ALLOW_PROTOCOL=none &&
+ export GIT_ALLOW_PROTOCOL &&
+ test_must_fail git -c protocol.allow=always clone --bare "$url" tmp.git &&
+ test_must_fail git -c protocol.$proto.allow=always clone --bare "$url" tmp.git
+ )
+ '
+}
+
+test_config () {
+ desc=$1
+ proto=$2
+ url=$3
+
+ # Test clone/fetch/push with protocol.<type>.allow config
+ test_expect_success "clone $desc (enabled with config)" '
+ rm -rf tmp.git &&
+ git -c protocol.$proto.allow=always clone --bare "$url" tmp.git
+ '
+
+ test_expect_success "fetch $desc (enabled)" '
+ git -C tmp.git -c protocol.$proto.allow=always fetch
+ '
+
+ test_expect_success "push $desc (enabled)" '
+ git -C tmp.git -c protocol.$proto.allow=always push origin HEAD:pushed
+ '
+
+ test_expect_success "push $desc (disabled)" '
+ test_must_fail git -C tmp.git -c protocol.$proto.allow=never push origin HEAD:pushed
+ '
+
+ test_expect_success "fetch $desc (disabled)" '
+ test_must_fail git -C tmp.git -c protocol.$proto.allow=never fetch
+ '
+
+ test_expect_success "clone $desc (disabled)" '
+ rm -rf tmp.git &&
+ test_must_fail git -c protocol.$proto.allow=never clone --bare "$url" tmp.git
+ '
+
+ # Test clone/fetch/push with protocol.user.allow and its env var
+ test_expect_success "clone $desc (enabled)" '
+ rm -rf tmp.git &&
+ git -c protocol.$proto.allow=user clone --bare "$url" tmp.git
+ '
+
+ test_expect_success "fetch $desc (enabled)" '
+ git -C tmp.git -c protocol.$proto.allow=user fetch
+ '
+
+ test_expect_success "push $desc (enabled)" '
+ git -C tmp.git -c protocol.$proto.allow=user push origin HEAD:pushed
+ '
+
+ test_expect_success "push $desc (disabled)" '
+ (
+ cd tmp.git &&
+ GIT_PROTOCOL_FROM_USER=0 &&
+ export GIT_PROTOCOL_FROM_USER &&
+ test_must_fail git -c protocol.$proto.allow=user push origin HEAD:pushed
+ )
+ '
+
+ test_expect_success "fetch $desc (disabled)" '
+ (
+ cd tmp.git &&
+ GIT_PROTOCOL_FROM_USER=0 &&
+ export GIT_PROTOCOL_FROM_USER &&
+ test_must_fail git -c protocol.$proto.allow=user fetch
+ )
+ '
+
+ test_expect_success "clone $desc (disabled)" '
+ rm -rf tmp.git &&
+ (
+ GIT_PROTOCOL_FROM_USER=0 &&
+ export GIT_PROTOCOL_FROM_USER &&
+ test_must_fail git -c protocol.$proto.allow=user clone --bare "$url" tmp.git
+ )
+ '
+
+ # Test clone/fetch/push with protocol.allow user defined default
+ test_expect_success "clone $desc (enabled)" '
+ rm -rf tmp.git &&
+ test_config_global protocol.allow always &&
+ git clone --bare "$url" tmp.git
+ '
+
+ test_expect_success "fetch $desc (enabled)" '
+ test_config_global protocol.allow always &&
+ git -C tmp.git fetch
+ '
+
+ test_expect_success "push $desc (enabled)" '
+ test_config_global protocol.allow always &&
+ git -C tmp.git push origin HEAD:pushed
+ '
+
+ test_expect_success "push $desc (disabled)" '
+ test_config_global protocol.allow never &&
+ test_must_fail git -C tmp.git push origin HEAD:pushed
+ '
+
+ test_expect_success "fetch $desc (disabled)" '
+ test_config_global protocol.allow never &&
+ test_must_fail git -C tmp.git fetch
+ '
+
+ test_expect_success "clone $desc (disabled)" '
+ rm -rf tmp.git &&
+ test_config_global protocol.allow never &&
+ test_must_fail git clone --bare "$url" tmp.git
+ '
+}
+
+# test cloning a particular protocol
+# $1 - description of the protocol
+# $2 - machine-readable name of the protocol
+# $3 - the URL to try cloning
+test_proto () {
+ test_allow_var "$@"
+
+ test_config "$@"
+}
+
+# set up an ssh wrapper that will access $host/$repo in the
+# trash directory, and enable it for subsequent tests.
+setup_ssh_wrapper () {
+ test_expect_success 'setup ssh wrapper' '
+ write_script ssh-wrapper <<-\EOF &&
+ echo >&2 "ssh: $*"
+ host=$1; shift
+ cd "$TRASH_DIRECTORY/$host" &&
+ eval "$*"
+ EOF
+ GIT_SSH="$PWD/ssh-wrapper" &&
+ export GIT_SSH &&
+ export TRASH_DIRECTORY
+ '
+}
+
+# set up a wrapper that can be used with remote-ext to
+# access repositories in the "remote" directory of trash-dir,
+# like "ext::fake-remote %S repo.git"
+setup_ext_wrapper () {
+ test_expect_success 'setup ext wrapper' '
+ write_script fake-remote <<-\EOF &&
+ echo >&2 "fake-remote: $*"
+ cd "$TRASH_DIRECTORY/remote" &&
+ eval "$*"
+ EOF
+ PATH=$TRASH_DIRECTORY:$PATH &&
+ export TRASH_DIRECTORY
+ '
+}
diff --git a/t/lib-read-tree-m-3way.sh b/t/lib-read-tree-m-3way.sh
new file mode 100644
index 0000000..2da25b3
--- /dev/null
+++ b/t/lib-read-tree-m-3way.sh
@@ -0,0 +1,158 @@
+: Included from t1000-read-tree-m-3way.sh and others
+# Original tree.
+mkdir Z
+for a in N D M
+do
+ for b in N D M
+ do
+ p=$a$b
+ echo This is $p from the original tree. >$p
+ echo This is Z/$p from the original tree. >Z/$p
+ test_expect_success "adding test file $p and Z/$p" '
+ git update-index --add $p &&
+ git update-index --add Z/$p
+ '
+ done
+done
+echo This is SS from the original tree. >SS
+test_expect_success 'adding test file SS' '
+ git update-index --add SS
+'
+cat >TT <<\EOF
+This is a trivial merge sample text.
+Branch A is expected to upcase this word, here.
+There are some filler lines to avoid diff context
+conflicts here,
+like this one,
+and this one,
+and this one is yet another one of them.
+At the very end, here comes another line, that is
+the word, expected to be upcased by Branch B.
+This concludes the trivial merge sample file.
+EOF
+test_expect_success 'adding test file TT' '
+ git update-index --add TT
+'
+test_expect_success 'prepare initial tree' '
+ tree_O=$(git write-tree)
+'
+
+################################################################
+# Branch A and B makes the changes according to the above matrix.
+
+################################################################
+# Branch A
+
+to_remove=$(echo D? Z/D?)
+rm -f $to_remove
+test_expect_success 'change in branch A (removal)' '
+ git update-index --remove $to_remove
+'
+
+for p in M? Z/M?
+do
+ echo This is modified $p in the branch A. >$p
+ test_expect_success 'change in branch A (modification)' '
+ git update-index $p
+ '
+done
+
+for p in AN AA Z/AN Z/AA
+do
+ echo This is added $p in the branch A. >$p
+ test_expect_success 'change in branch A (addition)' '
+ git update-index --add $p
+ '
+done
+
+echo This is SS from the modified tree. >SS
+echo This is LL from the modified tree. >LL
+test_expect_success 'change in branch A (addition)' '
+ git update-index --add LL &&
+ git update-index SS
+'
+mv TT TT-
+sed -e '/Branch A/s/word/WORD/g' <TT- >TT
+rm -f TT-
+test_expect_success 'change in branch A (edit)' '
+ git update-index TT
+'
+
+mkdir DF
+echo Branch A makes a file at DF/DF, creating a directory DF. >DF/DF
+test_expect_success 'change in branch A (change file to directory)' '
+ git update-index --add DF/DF
+'
+
+test_expect_success 'recording branch A tree' '
+ tree_A=$(git write-tree)
+'
+
+################################################################
+# Branch B
+# Start from O
+
+rm -rf [NDMASLT][NDMASLT] Z DF
+mkdir Z
+test_expect_success 'reading original tree and checking out' '
+ git read-tree $tree_O &&
+ git checkout-index -a
+'
+
+to_remove=$(echo ?D Z/?D)
+rm -f $to_remove
+test_expect_success 'change in branch B (removal)' '
+ git update-index --remove $to_remove
+'
+
+for p in ?M Z/?M
+do
+ echo This is modified $p in the branch B. >$p
+ test_expect_success 'change in branch B (modification)' '
+ git update-index $p
+ '
+done
+
+for p in NA AA Z/NA Z/AA
+do
+ echo This is added $p in the branch B. >$p
+ test_expect_success 'change in branch B (addition)' '
+ git update-index --add $p
+ '
+done
+echo This is SS from the modified tree. >SS
+echo This is LL from the modified tree. >LL
+test_expect_success 'change in branch B (addition and modification)' '
+ git update-index --add LL &&
+ git update-index SS
+'
+mv TT TT-
+sed -e '/Branch B/s/word/WORD/g' <TT- >TT
+rm -f TT-
+test_expect_success 'change in branch B (modification)' '
+ git update-index TT
+'
+
+echo Branch B makes a file at DF. >DF
+test_expect_success 'change in branch B (addition of a file to conflict with directory)' '
+ git update-index --add DF
+'
+
+test_expect_success 'recording branch B tree' '
+ tree_B=$(git write-tree)
+'
+
+test_expect_success 'keep contents of 3 trees for easy access' '
+ rm -f .git/index &&
+ git read-tree $tree_O &&
+ mkdir .orig-O &&
+ git checkout-index --prefix=.orig-O/ -f -q -a &&
+ rm -f .git/index &&
+ git read-tree $tree_A &&
+ mkdir .orig-A &&
+ git checkout-index --prefix=.orig-A/ -f -q -a &&
+ rm -f .git/index &&
+ git read-tree $tree_B &&
+ mkdir .orig-B &&
+ git checkout-index --prefix=.orig-B/ -f -q -a
+'
diff --git a/t/lib-read-tree.sh b/t/lib-read-tree.sh
new file mode 100644
index 0000000..b95f485
--- /dev/null
+++ b/t/lib-read-tree.sh
@@ -0,0 +1,41 @@
+# Helper functions to check if read-tree would succeed/fail as expected with
+# and without the dry-run option. They also test that the dry-run does not
+# write the index and that together with -u it doesn't touch the work tree.
+#
+read_tree_must_succeed () {
+ git ls-files -s >pre-dry-run &&
+ git read-tree -n "$@" &&
+ git ls-files -s >post-dry-run &&
+ test_cmp pre-dry-run post-dry-run &&
+ git read-tree "$@"
+}
+
+read_tree_must_fail () {
+ git ls-files -s >pre-dry-run &&
+ test_must_fail git read-tree -n "$@" &&
+ git ls-files -s >post-dry-run &&
+ test_cmp pre-dry-run post-dry-run &&
+ test_must_fail git read-tree "$@"
+}
+
+read_tree_u_must_succeed () {
+ git ls-files -s >pre-dry-run &&
+ git diff-files -p >pre-dry-run-wt &&
+ git read-tree -n "$@" &&
+ git ls-files -s >post-dry-run &&
+ git diff-files -p >post-dry-run-wt &&
+ test_cmp pre-dry-run post-dry-run &&
+ test_cmp pre-dry-run-wt post-dry-run-wt &&
+ git read-tree "$@"
+}
+
+read_tree_u_must_fail () {
+ git ls-files -s >pre-dry-run &&
+ git diff-files -p >pre-dry-run-wt &&
+ test_must_fail git read-tree -n "$@" &&
+ git ls-files -s >post-dry-run &&
+ git diff-files -p >post-dry-run-wt &&
+ test_cmp pre-dry-run post-dry-run &&
+ test_cmp pre-dry-run-wt post-dry-run-wt &&
+ test_must_fail git read-tree "$@"
+}
diff --git a/t/lib-rebase.sh b/t/lib-rebase.sh
new file mode 100644
index 0000000..11d2dc9
--- /dev/null
+++ b/t/lib-rebase.sh
@@ -0,0 +1,231 @@
+# Helper functions used by interactive rebase tests.
+
+# After setting the fake editor with this function, you can
+#
+# - override the commit message with $FAKE_COMMIT_MESSAGE
+# - amend the commit message with $FAKE_COMMIT_AMEND
+# - copy the original commit message to a file with $FAKE_MESSAGE_COPY
+# - check that non-commit messages have a certain line count with $EXPECT_COUNT
+# - check the commit count in the commit message header with $EXPECT_HEADER_COUNT
+# - rewrite a rebase -i script as directed by $FAKE_LINES.
+# $FAKE_LINES consists of a sequence of words separated by spaces;
+# spaces inside the words are encoded as underscores.
+# The following words are possible:
+#
+# "<cmd>" -- override the command for the next line specification. Can be
+# "pick", "squash", "fixup[_-(c|C)]", "edit", "reword", "drop",
+# "merge[_-(c|C)_<rev>]", or "bad" for an invalid command.
+#
+# "<lineno>" -- add a command, using the specified line as a template.
+# If the command has not been overridden, the line will be copied
+# verbatim, usually resulting in a "pick" line.
+#
+# "fakesha" -- add a command ("pick" by default), using a fake SHA1.
+#
+# "exec_[command...]", "break" -- add the specified command.
+#
+# "#" -- Add a comment line.
+#
+# ">" -- Add a blank line.
+
+set_fake_editor () {
+ write_script fake-editor.sh <<-\EOF
+ case "$1" in
+ */COMMIT_EDITMSG)
+ test -z "$EXPECT_HEADER_COUNT" ||
+ test "$EXPECT_HEADER_COUNT" = "$(sed -n '1s/^# This is a combination of \(.*\) commits\./\1/p' < "$1")" ||
+ exit
+ test -z "$FAKE_COMMIT_MESSAGE" || echo "$FAKE_COMMIT_MESSAGE" > "$1"
+ test -z "$FAKE_COMMIT_AMEND" || echo "$FAKE_COMMIT_AMEND" >> "$1"
+ test -z "$FAKE_MESSAGE_COPY" || cat "$1" >"$FAKE_MESSAGE_COPY"
+ exit
+ ;;
+ esac
+ test -z "$EXPECT_COUNT" ||
+ test "$EXPECT_COUNT" = $(sed -e '/^#/d' -e '/^$/d' < "$1" | wc -l) ||
+ exit
+ test -z "$FAKE_LINES" && exit
+ grep -v '^#' < "$1" > "$1".tmp
+ rm -f "$1"
+ echo 'rebase -i script before editing:'
+ cat "$1".tmp
+ action=\&
+ for line in $FAKE_LINES; do
+ case $line in
+ pick|p|squash|s|fixup|f|edit|e|reword|r|drop|d|label|l|reset|t|merge|m)
+ action="$line";;
+ exec_*|x_*|break|b)
+ echo "$line" | sed 's/_/ /g' >> "$1";;
+ merge_*|fixup_*)
+ action=$(echo "$line" | sed 's/_/ /g');;
+ "#")
+ echo '# comment' >> "$1";;
+ ">")
+ echo >> "$1";;
+ bad)
+ action="pickled";;
+ fakesha)
+ test \& != "$action" || action=pick
+ echo "$action XXXXXXX False commit" >> "$1"
+ action=\&;;
+ *)
+ sed -n "${line}s/^[a-z][a-z]*/$action/p" < "$1".tmp >> "$1"
+ action=\&;;
+ esac
+ done
+ echo 'rebase -i script after editing:'
+ cat "$1"
+ EOF
+
+ test_set_editor "$(pwd)/fake-editor.sh"
+}
+
+# After set_cat_todo_editor, rebase -i will write the todo list (ignoring
+# blank lines and comments) to stdout, and exit failure (so you should run
+# it with test_must_fail). This can be used to verify the expected user
+# experience, for todo list changes that do not affect the outcome of
+# rebase; or as an extra check in addition to checking the outcome.
+
+set_cat_todo_editor () {
+ write_script fake-editor.sh <<-\EOF
+ grep "^[^#]" "$1"
+ exit 1
+ EOF
+ test_set_editor "$(pwd)/fake-editor.sh"
+}
+
+# checks that the revisions in "$2" represent a linear range with the
+# subjects in "$1"
+test_linear_range () {
+ revlist_merges=$(git rev-list --merges "$2") &&
+ test -z "$revlist_merges" &&
+ expected=$1
+ set -- $(git log --reverse --format=%s "$2")
+ test "$expected" = "$*"
+}
+
+reset_rebase () {
+ test_might_fail git rebase --abort &&
+ git reset --hard &&
+ git clean -f
+}
+
+cherry_pick () {
+ git cherry-pick -n "$2" &&
+ git commit -m "$1" &&
+ git tag "$1"
+}
+
+revert () {
+ git revert -n "$2" &&
+ git commit -m "$1" &&
+ git tag "$1"
+}
+
+make_empty () {
+ git commit --allow-empty -m "$1" &&
+ git tag "$1"
+}
+
+# Call this (inside test_expect_success) at the end of a test file to
+# check that no tests have changed editor related environment
+# variables or config settings
+test_editor_unchanged () {
+ # We're only interested in exported variables hence 'sh -c'
+ sh -c 'cat >actual <<-EOF
+ EDITOR=$EDITOR
+ FAKE_COMMIT_AMEND=$FAKE_COMMIT_AMEND
+ FAKE_COMMIT_MESSAGE=$FAKE_COMMIT_MESSAGE
+ FAKE_LINES=$FAKE_LINES
+ GIT_EDITOR=$GIT_EDITOR
+ GIT_SEQUENCE_EDITOR=$GIT_SEQUENCE_EDITOR
+ core.editor=$(git config core.editor)
+ sequence.editor=$(git config sequence.editor)
+ EOF'
+ cat >expect <<-\EOF
+ EDITOR=:
+ FAKE_COMMIT_AMEND=
+ FAKE_COMMIT_MESSAGE=
+ FAKE_LINES=
+ GIT_EDITOR=
+ GIT_SEQUENCE_EDITOR=
+ core.editor=
+ sequence.editor=
+ EOF
+ test_cmp expect actual
+}
+
+# Set up an editor for testing reword commands
+# Checks that there are no uncommitted changes when rewording and that the
+# todo-list is reread after each
+set_reword_editor () {
+ >reword-actual &&
+ >reword-oid &&
+
+ # Check rewording keeps the original authorship
+ GIT_AUTHOR_NAME="Reword Author"
+ GIT_AUTHOR_EMAIL="reword.author@example.com"
+ GIT_AUTHOR_DATE=@123456
+
+ write_script reword-sequence-editor.sh <<-\EOF &&
+ todo="$(cat "$1")" &&
+ echo "exec git log -1 --pretty=format:'%an <%ae> %at%n%B%n' \
+ >>reword-actual" >"$1" &&
+ printf "%s\n" "$todo" >>"$1"
+ EOF
+
+ write_script reword-editor.sh <<-EOF &&
+ # Save the oid of the first reworded commit so we can check rebase
+ # fast-forwards to it. Also check that we do not write .git/MERGE_MSG
+ # when fast-forwarding
+ if ! test -s reword-oid
+ then
+ git rev-parse HEAD >reword-oid &&
+ if test -f .git/MERGE_MSG
+ then
+ echo 1>&2 "error: .git/MERGE_MSG exists"
+ exit 1
+ fi
+ fi &&
+ # There should be no uncommited changes
+ git diff --exit-code HEAD &&
+ # The todo-list should be re-read after a reword
+ GIT_SEQUENCE_EDITOR="\"$PWD/reword-sequence-editor.sh\"" \
+ git rebase --edit-todo &&
+ echo edited >>"\$1"
+ EOF
+
+ test_set_editor "$PWD/reword-editor.sh"
+}
+
+# Check the results of a rebase after calling set_reword_editor
+# Pass the commits that were reworded in the order that they were picked
+# Expects the first pick to be a fast-forward
+check_reworded_commits () {
+ test_cmp_rev "$(cat reword-oid)" "$1^{commit}" &&
+ git log --format="%an <%ae> %at%n%B%nedited%n" --no-walk=unsorted "$@" \
+ >reword-expected &&
+ test_cmp reword-expected reword-actual &&
+ git log --format="%an <%ae> %at%n%B" -n $# --first-parent --reverse \
+ >reword-log &&
+ test_cmp reword-expected reword-log
+}
+
+# usage: set_replace_editor <file>
+#
+# Replace the todo file with the exact contents of the given file.
+# N.B. sets GIT_SEQUENCE_EDITOR rather than EDITOR so it can be
+# combined with set_fake_editor to reword commits and replace the
+# todo list
+set_replace_editor () {
+ cat >script <<-\EOF &&
+ cat FILENAME >"$1"
+
+ echo 'rebase -i script after editing:'
+ cat "$1"
+ EOF
+
+ sed -e "s/FILENAME/$1/g" script |
+ write_script fake-sequence-editor.sh &&
+ test_set_sequence_editor "$(pwd)/fake-sequence-editor.sh"
+}
diff --git a/t/lib-submodule-update.sh b/t/lib-submodule-update.sh
new file mode 100644
index 0000000..36f767c
--- /dev/null
+++ b/t/lib-submodule-update.sh
@@ -0,0 +1,1099 @@
+# 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)
+# - Updated submodule recursively (add_nested_sub => modify_sub1_recursively)
+# - 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
+# / ^
+# / remove_sub1
+# /
+# add_sub1 /-------O---------O--------O modify_sub1_recursively
+# | / ^ add_nested_sub
+# | / modify_sub1
+# v/
+# O------O-----------O---------O
+# ^ \ ^ replace_directory_with_sub1
+# | \ replace_sub1_with_directory
+# no_submodule \
+# --------O---------O
+# \ ^ replace_file_with_sub1
+# \ replace_sub1_with_file
+# \
+# ----O---------O
+# ^ valid_sub1
+# invalid_sub1
+#
+
+create_lib_submodule_repo () {
+ git init submodule_update_sub1 &&
+ (
+ cd submodule_update_sub1 &&
+ echo "expect" >>.gitignore &&
+ echo "actual" >>.gitignore &&
+ echo "x" >file1 &&
+ echo "y" >file2 &&
+ git add .gitignore file1 file2 &&
+ git commit -m "Base inside first submodule" &&
+ git branch "no_submodule"
+ ) &&
+ git init submodule_update_sub2 &&
+ (
+ cd submodule_update_sub2
+ echo "expect" >>.gitignore &&
+ echo "actual" >>.gitignore &&
+ echo "x" >file1 &&
+ echo "y" >file2 &&
+ git add .gitignore file1 file2 &&
+ git commit -m "nested submodule base" &&
+ git branch "no_submodule"
+ ) &&
+ git init submodule_update_repo &&
+ (
+ cd submodule_update_repo &&
+ branch=$(git symbolic-ref --short HEAD) &&
+ 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 ../submodule_update_sub1 sub1 &&
+ git submodule add ../submodule_update_sub1 uninitialized_sub &&
+ 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 add_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 add_nested_sub modify_sub1 &&
+ git -C sub1 checkout -b "add_nested_sub" &&
+ git -C sub1 submodule add --branch no_submodule ../submodule_update_sub2 sub2 &&
+ git -C sub1 commit -a -m "add a nested submodule" &&
+ git add sub1 &&
+ git commit -a -m "update submodule, that updates a nested submodule" &&
+ git checkout -b modify_sub1_recursively &&
+ git -C sub1 checkout -b modify_sub1_recursively &&
+ git -C sub1/sub2 checkout -b modify_sub1_recursively &&
+ echo change >sub1/sub2/file3 &&
+ git -C sub1/sub2 add file3 &&
+ git -C sub1/sub2 commit -m "make a change in nested sub" &&
+ git -C sub1 add sub2 &&
+ git -C sub1 commit -m "update nested sub" &&
+ git add sub1 &&
+ git commit -m "update sub1, that updates nested sub" &&
+ git -C sub1 push origin modify_sub1_recursively &&
+ git -C sub1/sub2 push origin modify_sub1_recursively &&
+ git -C sub1 submodule deinit -f --all &&
+
+ git checkout -b replace_sub1_with_directory add_sub1 &&
+ git submodule update &&
+ git -C sub1 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 $(test_oid numeric) sub1 &&
+ git commit -m "Invalid sub1 commit" &&
+ git checkout -b valid_sub1 &&
+ git revert HEAD &&
+
+ git checkout "$branch"
+ )
+}
+
+# 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 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.
+#
+# Note that this only supports submodules at the root level of the
+# superproject, with the default name, i.e. same as its path.
+test_git_directory_is_unchanged () {
+ # does core.worktree point at the right place?
+ echo "../../../$1" >expect &&
+ git -C ".git/modules/$1" config core.worktree >actual &&
+ test_cmp expect actual &&
+ # remove it temporarily before comparing, as
+ # "$1/.git/config" lacks it...
+ git -C ".git/modules/$1" config --unset core.worktree &&
+ diff -r ".git/modules/$1" "$1/.git" &&
+ # ... and then restore.
+ git -C ".git/modules/$1" config core.worktree "../../../$1"
+}
+
+test_git_directory_exists () {
+ test -e ".git/modules/$1" &&
+ if test -f sub1/.git
+ then
+ # does core.worktree point at the right place?
+ echo "../../../$1" >expect &&
+ git -C ".git/modules/$1" config core.worktree >actual &&
+ test_cmp expect actual
+ fi
+}
+
+# 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_config_global protocol.file.allow always &&
+ (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 --template= 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 &&
+ hash=$(git rev-parse --revs-only HEAD:sub1) &&
+ if test -n "$hash" &&
+ test $(cd "../submodule_update_sub1" && git rev-parse --verify "$hash^{commit}")
+ then
+ git submodule update --init --recursive "sub1"
+ fi
+ )
+}
+
+reset_work_tree_to_interested () {
+ reset_work_tree_to $1 &&
+ # make the submodule git dirs available
+ if ! test -d submodule_update/.git/modules/sub1
+ then
+ mkdir -p submodule_update/.git/modules &&
+ cp -r submodule_update_repo/.git/modules/sub1 submodule_update/.git/modules/sub1
+ GIT_WORK_TREE=. git -C submodule_update/.git/modules/sub1 config --unset core.worktree
+ fi &&
+ if ! test -d submodule_update/.git/modules/sub1/modules/sub2
+ then
+ mkdir -p submodule_update/.git/modules/sub1/modules &&
+ cp -r submodule_update_repo/.git/modules/sub1/modules/sub2 submodule_update/.git/modules/sub1/modules/sub2
+ # core.worktree is unset for sub2 as it is not checked out
+ fi &&
+ # indicate we are interested in the submodule:
+ git -C submodule_update config submodule.sub1.url "bogus" &&
+ # sub1 might not be checked out, so use the git dir
+ git -C submodule_update/.git/modules/sub1 config submodule.sub2.url "bogus"
+}
+
+# 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 x"$1" = "x-C"
+ then
+ cd "$2"
+ shift; shift;
+ fi
+ 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.
+#
+# The first argument of the callback function will be the name of the submodule.
+#
+# Removing a submodule containing a .git directory must fail even when forced
+# to protect the history! If we are testing this case, the second argument of
+# the callback function will be 'test_must_fail', else it will be the empty
+# string.
+#
+
+# Internal function; use test_submodule_switch_func(), test_submodule_switch(),
+# or test_submodule_forced_switch() instead.
+test_submodule_switch_common () {
+ 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.
+ 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 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
+ )
+ '
+ # Replacing a tracked file with a submodule produces an empty
+ # directory ...
+ test_expect_$RESULT "$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.
+ 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 &&
+ (
+ 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 ...
+ 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 &&
+ (
+ 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_$RESULT "$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 ...
+ 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"
+ elif test "$KNOWN_FAILURE_FORCED_SWITCH_TESTS" = 1
+ then
+ # All existing tests that use test_submodule_forced_switch()
+ # require this.
+ RESULT="failure"
+ else
+ RESULT="success"
+ fi
+ test_expect_$RESULT "$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 &&
+ $command replace_sub1_with_directory test_must_fail &&
+ test_superproject_content origin/add_sub1 &&
+ test_submodule_content sub1 origin/add_sub1
+ )
+ '
+ # ... especially when it contains a .git directory.
+ test_expect_$RESULT "$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 &&
+ $command replace_sub1_with_directory test_must_fail &&
+ 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 &&
+ $command replace_sub1_with_file test_must_fail &&
+ 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 &&
+ $command replace_sub1_with_file test_must_fail &&
+ 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
+ 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 &&
+ (
+ 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_$RESULT "$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_$RESULT "$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
+ )
+ '
+}
+
+# Declares and invokes several tests that, in various situations, checks that
+# the provided transition function:
+# - succeeds in updating the worktree and index of a superproject to a target
+# commit, or fails atomically (depending on the test situation)
+# - if succeeds, the contents of submodule directories are unchanged
+# - if succeeds, once "git submodule update" is invoked, the contents of
+# submodule directories are updated
+#
+# If the command under test is known to not work with submodules in certain
+# conditions, set the appropriate KNOWN_FAILURE_* variable used in the tests
+# below to 1.
+#
+# The first argument of the callback function will be the name of the submodule.
+#
+# Removing a submodule containing a .git directory must fail even when forced
+# to protect the history! If we are testing this case, the second argument of
+# the callback function will be 'test_must_fail', else it will be the empty
+# string.
+#
+# The following example uses `git some-command` as an example command to be
+# tested. It updates the worktree and index to match a target, but not any
+# submodule directories.
+#
+# my_func () {
+# ...prepare for `git some-command` to be run...
+# $2 git some-command "$1" &&
+# if test -n "$2"
+# then
+# return
+# fi &&
+# ...check the state after git some-command is run...
+# }
+# test_submodule_switch_func "my_func"
+test_submodule_switch_func () {
+ command="$1"
+ test_submodule_switch_common "$command"
+
+ # An empty directory does not prevent the creation of a submodule of
+ # the same name, but a file does.
+ 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 &&
+ >sub1 &&
+ $command add_sub1 test_must_fail &&
+ test_superproject_content origin/no_submodule &&
+ test_must_be_empty sub1
+ )
+ '
+}
+
+# Ensures that the that the arg either contains "test_must_fail" or is empty.
+may_only_be_test_must_fail () {
+ test -z "$1" || test "$1" = test_must_fail || die
+}
+
+git_test_func () {
+ may_only_be_test_must_fail "$2" &&
+ $2 git $gitcmd "$1"
+}
+
+test_submodule_switch () {
+ gitcmd="$1"
+ test_submodule_switch_func "git_test_func"
+}
+
+# Same as test_submodule_switch(), except that throwing away local changes in
+# the superproject is allowed.
+test_submodule_forced_switch () {
+ gitcmd="$1"
+ command="git_test_func"
+ KNOWN_FAILURE_FORCED_SWITCH_TESTS=1
+ test_submodule_switch_common "$command"
+
+ # When forced, a file in the superproject does not prevent creating a
+ # submodule of the same name.
+ 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 &&
+ >sub1 &&
+ $command add_sub1 &&
+ test_superproject_content origin/add_sub1 &&
+ test_dir_is_empty sub1
+ )
+ '
+}
+
+# Test that submodule contents are correctly updated when switching
+# between commits that change a submodule.
+# Test that the following transitions are correctly handled:
+# (These tests are also above in the case where we expect no change
+# in the submodule)
+# - 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
+#
+# New test cases
+# - Removing a submodule with a git directory absorbs the submodules
+# git directory first into the superproject.
+# - Switching from no submodule to nested submodules
+# - Switching from nested submodules to no submodule
+
+# Internal function; use test_submodule_switch_recursing_with_args() or
+# test_submodule_forced_switch_recursing_with_args() instead.
+test_submodule_recursing_with_args_common () {
+ command="$1 --recurse-submodules"
+
+ ######################### Appearing submodule #########################
+ # Switching to a commit letting a submodule appear checks it out ...
+ test_expect_success "$command: added submodule is checked out" '
+ prolog &&
+ reset_work_tree_to_interested no_submodule &&
+ (
+ cd submodule_update &&
+ git branch -t add_sub1 origin/add_sub1 &&
+ $command add_sub1 &&
+ test_superproject_content origin/add_sub1 &&
+ test_submodule_content sub1 origin/add_sub1
+ )
+ '
+ # ... ignoring an empty existing directory.
+ test_expect_success "$command: added submodule is checked out in empty dir" '
+ prolog &&
+ reset_work_tree_to_interested 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_submodule_content sub1 origin/add_sub1
+ )
+ '
+
+ # Replacing a tracked file with a submodule produces a checked out submodule
+ test_expect_success "$command: replace tracked file with submodule checks out submodule" '
+ prolog &&
+ reset_work_tree_to_interested 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_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_interested 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_submodule_content sub1 origin/replace_directory_with_sub1
+ )
+ '
+ # Switching to a commit with nested submodules recursively checks them out
+ test_expect_success "$command: nested submodules are checked out" '
+ prolog &&
+ reset_work_tree_to_interested no_submodule &&
+ (
+ cd submodule_update &&
+ git branch -t modify_sub1_recursively origin/modify_sub1_recursively &&
+ $command modify_sub1_recursively &&
+ test_superproject_content origin/modify_sub1_recursively &&
+ test_submodule_content sub1 origin/modify_sub1_recursively &&
+ test_submodule_content -C sub1 sub2 origin/modify_sub1_recursively
+ )
+ '
+
+ ######################## Disappearing submodule #######################
+ # Removing a submodule removes its work tree ...
+ test_expect_success "$command: removed submodule removes submodules working tree" '
+ prolog &&
+ reset_work_tree_to_interested add_sub1 &&
+ (
+ cd submodule_update &&
+ git branch -t remove_sub1 origin/remove_sub1 &&
+ $command remove_sub1 &&
+ test_superproject_content origin/remove_sub1 &&
+ ! test -e sub1 &&
+ test_must_fail git config -f .git/modules/sub1/config core.worktree
+ )
+ '
+ # ... absorbing a .git directory along the way.
+ test_expect_success "$command: removed submodule absorbs submodules .git directory" '
+ prolog &&
+ reset_work_tree_to_interested add_sub1 &&
+ (
+ cd submodule_update &&
+ git branch -t remove_sub1 origin/remove_sub1 &&
+ replace_gitfile_with_git_dir sub1 &&
+ rm -rf .git/modules &&
+ $command remove_sub1 &&
+ test_superproject_content origin/remove_sub1 &&
+ ! test -e sub1 &&
+ test_git_directory_exists sub1
+ )
+ '
+
+ # Replacing it with a file ...
+ test_expect_success "$command: replace submodule with a file" '
+ prolog &&
+ reset_work_tree_to_interested add_sub1 &&
+ (
+ cd submodule_update &&
+ git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
+ $command replace_sub1_with_file &&
+ test_superproject_content origin/replace_sub1_with_file &&
+ test -f sub1
+ )
+ '
+ RESULTDS=success
+ if test "$KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS" = 1
+ then
+ RESULTDS=failure
+ fi
+ # ... must check its local work tree for untracked files
+ test_expect_$RESULTDS "$command: replace submodule with a file must fail with untracked files" '
+ prolog &&
+ reset_work_tree_to_interested add_sub1 &&
+ (
+ cd submodule_update &&
+ git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
+ : >sub1/untrackedfile &&
+ test_must_fail $command replace_sub1_with_file &&
+ test_superproject_content origin/add_sub1 &&
+ test_submodule_content sub1 origin/add_sub1 &&
+ test -f sub1/untracked_file
+ )
+ '
+
+ # Switching to a commit without nested submodules removes their worktrees
+ test_expect_success "$command: worktrees of nested submodules are removed" '
+ prolog &&
+ reset_work_tree_to_interested add_nested_sub &&
+ (
+ cd submodule_update &&
+ git branch -t no_submodule origin/no_submodule &&
+ $command no_submodule &&
+ test_superproject_content origin/no_submodule &&
+ test_path_is_missing sub1 &&
+ test_must_fail git config -f .git/modules/sub1/config core.worktree &&
+ test_must_fail git config -f .git/modules/sub1/modules/sub2/config core.worktree
+ )
+ '
+
+ ########################## Modified submodule #########################
+ # Updating a submodule sha1 updates the submodule's work tree
+ test_expect_success "$command: modified submodule updates submodule work tree" '
+ prolog &&
+ reset_work_tree_to_interested 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/modify_sub1
+ )
+ '
+ # Updating a submodule to an invalid sha1 doesn't update the
+ # superproject nor the submodule's work tree.
+ test_expect_success "$command: updating to a missing submodule commit fails" '
+ prolog &&
+ reset_work_tree_to_interested add_sub1 &&
+ (
+ cd submodule_update &&
+ git branch -t invalid_sub1 origin/invalid_sub1 &&
+ test_must_fail $command invalid_sub1 2>err &&
+ test_grep sub1 err &&
+ test_superproject_content origin/add_sub1 &&
+ test_submodule_content sub1 origin/add_sub1
+ )
+ '
+ # Updating a submodule does not touch the currently checked out branch in the submodule
+ test_expect_success "$command: submodule branch is not changed, detach HEAD instead" '
+ prolog &&
+ reset_work_tree_to_interested add_sub1 &&
+ (
+ cd submodule_update &&
+ git -C sub1 checkout -b keep_branch &&
+ git -C sub1 rev-parse HEAD >expect &&
+ git branch -t modify_sub1 origin/modify_sub1 &&
+ $command modify_sub1 &&
+ test_superproject_content origin/modify_sub1 &&
+ test_submodule_content sub1 origin/modify_sub1 &&
+ git -C sub1 rev-parse keep_branch >actual &&
+ test_cmp expect actual &&
+ test_must_fail git -C sub1 symbolic-ref HEAD
+ )
+ '
+}
+
+# Declares and invokes several tests that, in various situations, checks that
+# the provided Git command, when invoked with --recurse-submodules:
+# - succeeds in updating the worktree and index of a superproject to a target
+# commit, or fails atomically (depending on the test situation)
+# - if succeeds, the contents of submodule directories are updated
+#
+# Specify the Git command so that "git $GIT_COMMAND --recurse-submodules"
+# works.
+#
+# If the command under test is known to not work with submodules in certain
+# conditions, set the appropriate KNOWN_FAILURE_* variable used in the tests
+# below to 1.
+#
+# Use as follows:
+#
+# test_submodule_switch_recursing_with_args "$GIT_COMMAND"
+test_submodule_switch_recursing_with_args () {
+ cmd_args="$1"
+ command="git $cmd_args"
+ test_submodule_recursing_with_args_common "$command"
+
+ RESULTDS=success
+ if test "$KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS" = 1
+ then
+ RESULTDS=failure
+ fi
+ RESULTOI=success
+ if test "$KNOWN_FAILURE_SUBMODULE_OVERWRITE_IGNORED_UNTRACKED" = 1
+ then
+ RESULTOI=failure
+ fi
+ # Switching to a commit letting a submodule appear cannot override an
+ # untracked file.
+ test_expect_success "$command: added submodule doesn't remove untracked file with same name" '
+ prolog &&
+ reset_work_tree_to_interested no_submodule &&
+ (
+ cd submodule_update &&
+ git branch -t add_sub1 origin/add_sub1 &&
+ : >sub1 &&
+ test_must_fail $command add_sub1 &&
+ test_superproject_content origin/no_submodule &&
+ test_must_be_empty sub1
+ )
+ '
+ # ... but an ignored file is fine.
+ test_expect_$RESULTOI "$command: added submodule removes an untracked ignored file" '
+ test_when_finished "rm -rf submodule_update/.git/info" &&
+ prolog &&
+ reset_work_tree_to_interested no_submodule &&
+ (
+ cd submodule_update &&
+ git branch -t add_sub1 origin/add_sub1 &&
+ : >sub1 &&
+ mkdir .git/info &&
+ echo sub1 >.git/info/exclude &&
+ $command add_sub1 &&
+ test_superproject_content origin/add_sub1 &&
+ test_submodule_content sub1 origin/add_sub1
+ )
+ '
+
+ # Replacing a submodule with files in a directory must succeeds
+ # when the submodule is clean
+ test_expect_$RESULTDS "$command: replace submodule with a directory" '
+ prolog &&
+ reset_work_tree_to_interested add_sub1 &&
+ (
+ cd submodule_update &&
+ git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
+ $command replace_sub1_with_directory &&
+ test_superproject_content origin/replace_sub1_with_directory &&
+ test_submodule_content sub1 origin/replace_sub1_with_directory
+ )
+ '
+ # ... absorbing a .git directory.
+ test_expect_$RESULTDS "$command: replace submodule containing a .git directory with a directory must absorb the git dir" '
+ prolog &&
+ reset_work_tree_to_interested add_sub1 &&
+ (
+ cd submodule_update &&
+ git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
+ replace_gitfile_with_git_dir sub1 &&
+ rm -rf .git/modules &&
+ $command replace_sub1_with_directory &&
+ test_superproject_content origin/replace_sub1_with_directory &&
+ test_git_directory_exists sub1
+ )
+ '
+
+ # ... and ignored files are ignored
+ test_expect_success "$command: replace submodule with a file works ignores ignored files in submodule" '
+ test_when_finished "rm submodule_update/.git/modules/sub1/info/exclude" &&
+ prolog &&
+ reset_work_tree_to_interested add_sub1 &&
+ (
+ cd submodule_update &&
+ rm -rf .git/modules/sub1/info &&
+ git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
+ mkdir .git/modules/sub1/info &&
+ echo ignored >.git/modules/sub1/info/exclude &&
+ : >sub1/ignored &&
+ $command replace_sub1_with_file &&
+ test_superproject_content origin/replace_sub1_with_file &&
+ test -f sub1
+ )
+ '
+
+ test_expect_success "git -c submodule.recurse=true $cmd_args: modified submodule updates submodule work tree" '
+ prolog &&
+ reset_work_tree_to_interested add_sub1 &&
+ (
+ cd submodule_update &&
+ git branch -t modify_sub1 origin/modify_sub1 &&
+ git -c submodule.recurse=true $cmd_args modify_sub1 &&
+ test_superproject_content origin/modify_sub1 &&
+ test_submodule_content sub1 origin/modify_sub1
+ )
+ '
+
+ test_expect_success "$command: modified submodule updates submodule recursively" '
+ prolog &&
+ reset_work_tree_to_interested add_nested_sub &&
+ (
+ cd submodule_update &&
+ git branch -t modify_sub1_recursively origin/modify_sub1_recursively &&
+ $command modify_sub1_recursively &&
+ test_superproject_content origin/modify_sub1_recursively &&
+ test_submodule_content sub1 origin/modify_sub1_recursively &&
+ test_submodule_content -C sub1 sub2 origin/modify_sub1_recursively
+ )
+ '
+}
+
+# Same as test_submodule_switch_recursing_with_args(), except that throwing
+# away local changes in the superproject is allowed.
+test_submodule_forced_switch_recursing_with_args () {
+ cmd_args="$1"
+ command="git $cmd_args"
+ test_submodule_recursing_with_args_common "$command"
+
+ RESULT=success
+ if test "$KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS" = 1
+ then
+ RESULT=failure
+ fi
+ # Switching to a commit letting a submodule appear does not care about
+ # an untracked file.
+ test_expect_success "$command: added submodule does remove untracked unignored file with same name when forced" '
+ prolog &&
+ reset_work_tree_to_interested no_submodule &&
+ (
+ cd submodule_update &&
+ git branch -t add_sub1 origin/add_sub1 &&
+ >sub1 &&
+ $command add_sub1 &&
+ test_superproject_content origin/add_sub1 &&
+ test_submodule_content sub1 origin/add_sub1
+ )
+ '
+
+ # Replacing a submodule with files in a directory ...
+ test_expect_success "$command: replace submodule with a directory" '
+ prolog &&
+ reset_work_tree_to_interested add_sub1 &&
+ (
+ cd submodule_update &&
+ git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
+ $command replace_sub1_with_directory &&
+ test_superproject_content origin/replace_sub1_with_directory
+ )
+ '
+ # ... absorbing a .git directory.
+ test_expect_success "$command: replace submodule containing a .git directory with a directory must fail" '
+ prolog &&
+ reset_work_tree_to_interested add_sub1 &&
+ (
+ cd submodule_update &&
+ git branch -t replace_sub1_with_directory origin/replace_sub1_with_directory &&
+ replace_gitfile_with_git_dir sub1 &&
+ rm -rf .git/modules/sub1 &&
+ $command replace_sub1_with_directory &&
+ test_superproject_content origin/replace_sub1_with_directory &&
+ test_git_directory_exists sub1
+ )
+ '
+
+ # ... even if the submodule contains ignored files
+ test_expect_success "$command: replace submodule with a file ignoring ignored files" '
+ prolog &&
+ reset_work_tree_to_interested add_sub1 &&
+ (
+ cd submodule_update &&
+ git branch -t replace_sub1_with_file origin/replace_sub1_with_file &&
+ : >sub1/expect &&
+ $command replace_sub1_with_file &&
+ test_superproject_content origin/replace_sub1_with_file
+ )
+ '
+
+ # Updating a submodule from an invalid sha1 updates
+ test_expect_success "$command: modified submodule does update submodule work tree from invalid commit" '
+ prolog &&
+ reset_work_tree_to_interested invalid_sub1 &&
+ (
+ cd submodule_update &&
+ git branch -t valid_sub1 origin/valid_sub1 &&
+ $command valid_sub1 &&
+ test_superproject_content origin/valid_sub1 &&
+ test_submodule_content sub1 origin/valid_sub1
+ )
+ '
+
+ # Old versions of Git were buggy writing the .git link file
+ # (e.g. before f8eaa0ba98b and then moving the superproject repo
+ # whose submodules contained absolute paths)
+ test_expect_success "$command: updating submodules fixes .git links" '
+ prolog &&
+ reset_work_tree_to_interested add_sub1 &&
+ (
+ cd submodule_update &&
+ git branch -t modify_sub1 origin/modify_sub1 &&
+ echo "gitdir: bogus/path" >sub1/.git &&
+ $command modify_sub1 &&
+ test_superproject_content origin/modify_sub1 &&
+ test_submodule_content sub1 origin/modify_sub1
+ )
+ '
+
+ test_expect_success "$command: changed submodule worktree is reset" '
+ prolog &&
+ reset_work_tree_to_interested add_sub1 &&
+ (
+ cd submodule_update &&
+ rm sub1/file1 &&
+ : >sub1/new_file &&
+ git -C sub1 add new_file &&
+ $command HEAD &&
+ test_path_is_file sub1/file1 &&
+ test_path_is_missing sub1/new_file
+ )
+ '
+}
diff --git a/t/lib-subtest.sh b/t/lib-subtest.sh
new file mode 100644
index 0000000..56ee927
--- /dev/null
+++ b/t/lib-subtest.sh
@@ -0,0 +1,95 @@
+write_sub_test_lib_test () {
+ name="$1" # stdin is the body of the test code
+ mkdir "$name" &&
+ write_script "$name/$name.sh" "$TEST_SHELL_PATH" <<-EOF &&
+ test_description='A test of test-lib.sh itself'
+
+ # Point to the t/test-lib.sh, which isn't in ../ as usual
+ . "\$TEST_DIRECTORY"/test-lib.sh
+ EOF
+ cat >>"$name/$name.sh"
+}
+
+_run_sub_test_lib_test_common () {
+ cmp_op="$1" want_code="$2" name="$3" # stdin is the body of the test code
+ shift 3
+
+ # intercept pseudo-options at the front of the argument list that we
+ # will not pass to child script
+ skip=
+ while test $# -gt 0
+ do
+ case "$1" in
+ --skip=*)
+ skip=${1#--*=}
+ shift
+ ;;
+ *)
+ break
+ ;;
+ esac
+ done
+
+ (
+ cd "$name" &&
+
+ # Pretend we're not running under a test harness, whether we
+ # are or not. The test-lib output depends on the setting of
+ # this variable, so we need a stable setting under which to run
+ # the sub-test.
+ sane_unset HARNESS_ACTIVE &&
+
+ export TEST_DIRECTORY &&
+ # The child test re-sources GIT-BUILD-OPTIONS and may thus
+ # override the test output directory. We thus pass it as an
+ # explicit override to the child.
+ TEST_OUTPUT_DIRECTORY_OVERRIDE=$(pwd) &&
+ export TEST_OUTPUT_DIRECTORY_OVERRIDE &&
+ GIT_SKIP_TESTS=$skip &&
+ export GIT_SKIP_TESTS &&
+ sane_unset GIT_TEST_FAIL_PREREQS &&
+ ./"$name.sh" "$@" >out 2>err;
+ ret=$? &&
+ test "$ret" "$cmp_op" "$want_code"
+ )
+}
+
+write_and_run_sub_test_lib_test () {
+ name="$1" descr="$2" # stdin is the body of the test code
+ write_sub_test_lib_test "$@" || return 1
+ _run_sub_test_lib_test_common -eq 0 "$@"
+}
+
+write_and_run_sub_test_lib_test_err () {
+ name="$1" descr="$2" # stdin is the body of the test code
+ write_sub_test_lib_test "$@" || return 1
+ _run_sub_test_lib_test_common -eq 1 "$@"
+}
+
+run_sub_test_lib_test () {
+ _run_sub_test_lib_test_common -eq 0 "$@"
+}
+
+run_sub_test_lib_test_err () {
+ _run_sub_test_lib_test_common -eq 1 "$@"
+}
+
+_check_sub_test_lib_test_common () {
+ name="$1" &&
+ sed -e 's/^> //' -e 's/Z$//' >"$name"/expect.out &&
+ test_cmp "$name"/expect.out "$name"/out
+}
+
+check_sub_test_lib_test () {
+ name="$1" # stdin is the expected output from the test
+ _check_sub_test_lib_test_common "$name" &&
+ test_must_be_empty "$name"/err
+}
+
+check_sub_test_lib_test_err () {
+ name="$1" # stdin is the expected output from the test
+ _check_sub_test_lib_test_common "$name" &&
+ # expected error output is in descriptor 3
+ sed -e 's/^> //' -e 's/Z$//' <&3 >"$name"/expect.err &&
+ test_cmp "$name"/expect.err "$name"/err
+}
diff --git a/t/lib-sudo.sh b/t/lib-sudo.sh
new file mode 100644
index 0000000..b4d7788
--- /dev/null
+++ b/t/lib-sudo.sh
@@ -0,0 +1,15 @@
+# Helpers for running git commands under sudo.
+
+# Runs a scriplet passed through stdin under sudo.
+run_with_sudo () {
+ local ret
+ local RUN="$TEST_DIRECTORY/$$.sh"
+ write_script "$RUN" "$TEST_SHELL_PATH"
+ # avoid calling "$RUN" directly so sudo doesn't get a chance to
+ # override the shell, add aditional restrictions or even reject
+ # running the script because its security policy deem it unsafe
+ sudo "$TEST_SHELL_PATH" -c "\"$RUN\""
+ ret=$?
+ rm -f "$RUN"
+ return $ret
+}
diff --git a/t/lib-t3100.sh b/t/lib-t3100.sh
new file mode 100644
index 0000000..eabb5fd
--- /dev/null
+++ b/t/lib-t3100.sh
@@ -0,0 +1,10 @@
+#!/bin/sh
+
+setup_basic_ls_tree_data () {
+ mkdir dir &&
+ test_commit dir/sub-file &&
+ test_commit top-file &&
+ git clone . submodule &&
+ git submodule add ./submodule &&
+ git commit -m"add submodule"
+}
diff --git a/t/lib-t6000.sh b/t/lib-t6000.sh
new file mode 100644
index 0000000..fba6778
--- /dev/null
+++ b/t/lib-t6000.sh
@@ -0,0 +1,136 @@
+: included from 6002 and others
+
+>sed.script
+
+# Answer the sha1 has associated with the tag. The tag must exist under refs/tags
+tag () {
+ _tag=$1
+ git rev-parse --verify "refs/tags/$_tag" ||
+ error "tag: \"$_tag\" does not exist"
+}
+
+# Generate a commit using the text specified to make it unique and the tree
+# named by the tag specified.
+unique_commit () {
+ _text=$1
+ _tree=$2
+ shift 2
+ echo "$_text" | git commit-tree $(tag "$_tree") "$@"
+}
+
+# Save the output of a command into the tag specified. Prepend
+# a substitution script for the tag onto the front of sed.script
+save_tag () {
+ _tag=$1
+ test -n "$_tag" || error "usage: save_tag tag commit-args ..."
+ shift 1
+
+ git update-ref "refs/tags/$_tag" $("$@")
+
+ echo "s/$(tag $_tag)/$_tag/g" >sed.script.tmp
+ cat sed.script >>sed.script.tmp
+ rm sed.script
+ mv sed.script.tmp sed.script
+}
+
+# Replace unhelpful sha1 hashes with their symbolic equivalents
+entag () {
+ sed -f sed.script
+}
+
+# Execute a command after first saving, then setting the GIT_AUTHOR_EMAIL
+# tag to a specified value. Restore the original value on return.
+as_author () {
+ _author=$1
+ shift 1
+ _save=$GIT_AUTHOR_EMAIL
+
+ GIT_AUTHOR_EMAIL="$_author"
+ export GIT_AUTHOR_EMAIL
+ "$@"
+ if test -z "$_save"
+ then
+ unset GIT_AUTHOR_EMAIL
+ else
+ GIT_AUTHOR_EMAIL="$_save"
+ export GIT_AUTHOR_EMAIL
+ fi
+}
+
+commit_date () {
+ _commit=$1
+ git cat-file commit $_commit |
+ sed -n "s/^committer .*> \([0-9]*\) .*/\1/p"
+}
+
+# Assign the value of fake date to a variable, but
+# allow fairly common "1971-08-16 00:00" to be omittd
+assign_fake_date () {
+ case "$2" in
+ ??:??:??) eval "$1='1971-08-16 $2'" ;;
+ ??:??) eval "$1='1971-08-16 00:$2'" ;;
+ ??) eval "$1='1971-08-16 00:00:$2'" ;;
+ *) eval "$1='$2'" ;;
+ esac
+}
+
+on_committer_date () {
+ assign_fake_date GIT_COMMITTER_DATE "$1"
+ export GIT_COMMITTER_DATE
+ shift 1
+ "$@"
+}
+
+on_dates () {
+ assign_fake_date GIT_COMMITTER_DATE "$1"
+ assign_fake_date GIT_AUTHOR_DATE "$2"
+ export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
+ shift 2
+ "$@"
+}
+
+# Execute a command and suppress any error output.
+hide_error () {
+ "$@" 2>/dev/null
+}
+
+check_output () {
+ _name=$1
+ shift 1
+ if eval "$*" | entag >"$_name.actual"
+ then
+ test_cmp "$_name.expected" "$_name.actual"
+ else
+ return 1
+ fi
+}
+
+# Turn a reasonable test description into a reasonable test name.
+# All alphanums translated into -'s which are then compressed and stripped
+# from front and back.
+name_from_description () {
+ perl -pe '
+ s/[^A-Za-z0-9.]/-/g;
+ s/-+/-/g;
+ s/-$//;
+ s/^-//;
+ y/A-Z/a-z/;
+ '
+}
+
+
+# Execute the test described by the first argument, by eval'ing
+# command line specified in the 2nd argument. Check the status code
+# is zero and that the output matches the stream read from
+# stdin.
+test_output_expect_success()
+{
+ _description=$1
+ _test=$2
+ test $# -eq 2 ||
+ error "usage: test_output_expect_success description test <<EOF ... EOF"
+
+ _name=$(echo $_description | name_from_description)
+ cat >"$_name.expected"
+ test_expect_success "$_description" "check_output $_name \"$_test\""
+}
diff --git a/t/lib-terminal.sh b/t/lib-terminal.sh
new file mode 100644
index 0000000..e3809dc
--- /dev/null
+++ b/t/lib-terminal.sh
@@ -0,0 +1,36 @@
+# Helpers for terminal output tests.
+
+# Catch tests which should depend on TTY but forgot to. There's no need
+# to additionally check that the TTY prereq is set here. If the test declared
+# it and we are running the test, then it must have been set.
+test_terminal () {
+ if ! test_declared_prereq TTY
+ then
+ echo >&4 "test_terminal: need to declare TTY prerequisite"
+ return 127
+ fi
+ perl "$TEST_DIRECTORY"/test-terminal.perl "$@" 2>&7
+} 7>&2 2>&4
+
+test_lazy_prereq TTY '
+ test_have_prereq PERL &&
+
+ # Reading from the pty master seems to get stuck _sometimes_
+ # on Mac OS X 10.5.0, using Perl 5.10.0 or 5.8.9.
+ #
+ # Reproduction recipe: run
+ #
+ # i=0
+ # while ./test-terminal.perl echo hi $i
+ # do
+ # : $((i = $i + 1))
+ # done
+ #
+ # After 2000 iterations or so it hangs.
+ # https://rt.cpan.org/Ticket/Display.html?id=65692
+ #
+ test "$(uname -s)" != Darwin &&
+
+ perl "$TEST_DIRECTORY"/test-terminal.perl \
+ sh -c "test -t 1 && test -t 2"
+'
diff --git a/t/lib-unicode-nfc-nfd.sh b/t/lib-unicode-nfc-nfd.sh
new file mode 100755
index 0000000..2223224
--- /dev/null
+++ b/t/lib-unicode-nfc-nfd.sh
@@ -0,0 +1,162 @@
+# Help detect how Unicode NFC and NFD are handled on the filesystem.
+
+# A simple character that has a NFD form.
+#
+# NFC: U+00e9 LATIN SMALL LETTER E WITH ACUTE
+# UTF8(NFC): \xc3 \xa9
+#
+# NFD: U+0065 LATIN SMALL LETTER E
+# U+0301 COMBINING ACUTE ACCENT
+# UTF8(NFD): \x65 + \xcc \x81
+#
+utf8_nfc=$(printf "\xc3\xa9")
+utf8_nfd=$(printf "\x65\xcc\x81")
+
+# Is the OS or the filesystem "Unicode composition sensitive"?
+#
+# That is, does the OS or the filesystem allow files to exist with
+# both the NFC and NFD spellings? Or, does the OS/FS lie to us and
+# tell us that the NFC and NFD forms are equivalent.
+#
+# This is or may be independent of what type of filesystem we have,
+# since it might be handled by the OS at a layer above the FS.
+# Testing shows on MacOS using APFS, HFS+, and FAT32 reports a
+# collision, for example.
+#
+# This does not tell us how the Unicode pathname will be spelled
+# on disk, but rather only that the two spelling "collide". We
+# will examine the actual on disk spelling in a later prereq.
+#
+test_lazy_prereq UNICODE_COMPOSITION_SENSITIVE '
+ mkdir trial_${utf8_nfc} &&
+ mkdir trial_${utf8_nfd}
+'
+
+# Is the spelling of an NFC pathname preserved on disk?
+#
+# On MacOS with HFS+ and FAT32, NFC paths are converted into NFD
+# and on APFS, NFC paths are preserved. As we have established
+# above, this is independent of "composition sensitivity".
+#
+test_lazy_prereq UNICODE_NFC_PRESERVED '
+ mkdir c_${utf8_nfc} &&
+ ls | test-tool hexdump >dump &&
+ grep "63 5f c3 a9" dump
+'
+
+# Is the spelling of an NFD pathname preserved on disk?
+#
+test_lazy_prereq UNICODE_NFD_PRESERVED '
+ mkdir d_${utf8_nfd} &&
+ ls | test-tool hexdump >dump &&
+ grep "64 5f 65 cc 81" dump
+'
+
+# The following _DOUBLE_ forms are more for my curiosity,
+# but there may be quirks lurking when there are multiple
+# combining characters in non-canonical order.
+
+# Unicode also allows multiple combining characters
+# that can be decomposed in pieces.
+#
+# NFC: U+1f67 GREEK SMALL LETTER OMEGA WITH DASIA AND PERISPOMENI
+# UTF8(NFC): \xe1 \xbd \xa7
+#
+# NFD1: U+1f61 GREEK SMALL LETTER OMEGA WITH DASIA
+# U+0342 COMBINING GREEK PERISPOMENI
+# UTF8(NFD1): \xe1 \xbd \xa1 + \xcd \x82
+#
+# But U+1f61 decomposes into
+# NFD2: U+03c9 GREEK SMALL LETTER OMEGA
+# U+0314 COMBINING REVERSED COMMA ABOVE
+# UTF8(NFD2): \xcf \x89 + \xcc \x94
+#
+# Yielding: \xcf \x89 + \xcc \x94 + \xcd \x82
+#
+# Note that I've used the canonical ordering of the
+# combinining characters. It is also possible to
+# swap them. My testing shows that that non-standard
+# ordering also causes a collision in mkdir. However,
+# the resulting names don't draw correctly on the
+# terminal (implying that the on-disk format also has
+# them out of order).
+#
+greek_nfc=$(printf "\xe1\xbd\xa7")
+greek_nfd1=$(printf "\xe1\xbd\xa1\xcd\x82")
+greek_nfd2=$(printf "\xcf\x89\xcc\x94\xcd\x82")
+
+# See if a double decomposition also collides.
+#
+test_lazy_prereq UNICODE_DOUBLE_COMPOSITION_SENSITIVE '
+ mkdir trial_${greek_nfc} &&
+ mkdir trial_${greek_nfd2}
+'
+
+# See if the NFC spelling appears on the disk.
+#
+test_lazy_prereq UNICODE_DOUBLE_NFC_PRESERVED '
+ mkdir c_${greek_nfc} &&
+ ls | test-tool hexdump >dump &&
+ grep "63 5f e1 bd a7" dump
+'
+
+# See if the NFD spelling appears on the disk.
+#
+test_lazy_prereq UNICODE_DOUBLE_NFD_PRESERVED '
+ mkdir d_${greek_nfd2} &&
+ ls | test-tool hexdump >dump &&
+ grep "64 5f cf 89 cc 94 cd 82" dump
+'
+
+# The following is for debugging. I found it useful when
+# trying to understand the various (OS, FS) quirks WRT
+# Unicode and how composition/decomposition is handled.
+# For example, when trying to understand how (macOS, APFS)
+# and (macOS, HFS) and (macOS, FAT32) compare.
+#
+# It is rather noisy, so it is disabled by default.
+#
+if test "$unicode_debug" = "true"
+then
+ if test_have_prereq UNICODE_COMPOSITION_SENSITIVE
+ then
+ echo NFC and NFD are distinct on this OS/filesystem.
+ else
+ echo NFC and NFD are aliases on this OS/filesystem.
+ fi
+
+ if test_have_prereq UNICODE_NFC_PRESERVED
+ then
+ echo NFC maintains original spelling.
+ else
+ echo NFC is modified.
+ fi
+
+ if test_have_prereq UNICODE_NFD_PRESERVED
+ then
+ echo NFD maintains original spelling.
+ else
+ echo NFD is modified.
+ fi
+
+ if test_have_prereq UNICODE_DOUBLE_COMPOSITION_SENSITIVE
+ then
+ echo DOUBLE NFC and NFD are distinct on this OS/filesystem.
+ else
+ echo DOUBLE NFC and NFD are aliases on this OS/filesystem.
+ fi
+
+ if test_have_prereq UNICODE_DOUBLE_NFC_PRESERVED
+ then
+ echo Double NFC maintains original spelling.
+ else
+ echo Double NFC is modified.
+ fi
+
+ if test_have_prereq UNICODE_DOUBLE_NFD_PRESERVED
+ then
+ echo Double NFD maintains original spelling.
+ else
+ echo Double NFD is modified.
+ fi
+fi
diff --git a/t/lib-unique-files.sh b/t/lib-unique-files.sh
new file mode 100644
index 0000000..a14080f
--- /dev/null
+++ b/t/lib-unique-files.sh
@@ -0,0 +1,34 @@
+# Helper to create files with unique contents
+
+# Create multiple files with unique contents within this test run. Takes the
+# number of directories, the number of files in each directory, and the base
+# directory.
+#
+# test_create_unique_files 2 3 my_dir -- Creates 2 directories with 3 files
+# each in my_dir, all with contents
+# different from previous invocations
+# of this command in this run.
+
+test_create_unique_files () {
+ test "$#" -ne 3 && BUG "3 param"
+
+ local dirs="$1" &&
+ local files="$2" &&
+ local basedir="$3" &&
+ local counter="0" &&
+ local i &&
+ local j &&
+ test_tick &&
+ local basedata="$basedir$test_tick" &&
+ rm -rf "$basedir" &&
+ for i in $(test_seq $dirs)
+ do
+ local dir="$basedir/dir$i" &&
+ mkdir -p "$dir" &&
+ for j in $(test_seq $files)
+ do
+ counter=$((counter + 1)) &&
+ echo "$basedata.$counter">"$dir/file$j.txt"
+ done
+ done
+}
diff --git a/t/oid-info/README b/t/oid-info/README
new file mode 100644
index 0000000..ca56a74
--- /dev/null
+++ b/t/oid-info/README
@@ -0,0 +1,19 @@
+This directory contains various per-hash values that are used in the testsuite.
+
+Each file contains lines containing a key-value pair; blank lines and lines
+starting with `#` are ignored. The key and value are separated by whitespace
+(specifically, those whitespace in the default `$IFS`). The key consists only
+of shell identifier characters, and the value consists of a hash algorithm,
+colon, and value. The hash algorithm also consists only of shell identifier
+characters; it should match the value in object-file.c.
+
+For example, the following lines map the key "rawsz" to "20" if SHA-1 is in use
+and to "32" if SHA-256 is in use:
+
+----
+rawsz sha1:20
+rawsz sha256:32
+----
+
+The keys and values used here are loaded by `test_oid_init` (see the README file
+in the "t" directory) and are used by calling `test_oid`.
diff --git a/t/oid-info/hash-info b/t/oid-info/hash-info
new file mode 100644
index 0000000..d0736dd
--- /dev/null
+++ b/t/oid-info/hash-info
@@ -0,0 +1,17 @@
+rawsz sha1:20
+rawsz sha256:32
+
+hexsz sha1:40
+hexsz sha256:64
+
+zero sha1:0000000000000000000000000000000000000000
+zero sha256:0000000000000000000000000000000000000000000000000000000000000000
+
+algo sha1:sha1
+algo sha256:sha256
+
+empty_blob sha1:e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
+empty_blob sha256:473a0f4c3be8a93681a267e3b1e9a7dcda1185436fe141f7749120a303721813
+
+empty_tree sha1:4b825dc642cb6eb9a060e54bf8d69288fbee4904
+empty_tree sha256:6ef19b41225c5369f1c104d45d8d85efa9b057b53b14b4b9b939dd74decc5321
diff --git a/t/oid-info/oid b/t/oid-info/oid
new file mode 100644
index 0000000..7547d2c
--- /dev/null
+++ b/t/oid-info/oid
@@ -0,0 +1,31 @@
+# These are some common invalid and partial object IDs used in tests.
+001 sha1:0000000000000000000000000000000000000001
+001 sha256:0000000000000000000000000000000000000000000000000000000000000001
+002 sha1:0000000000000000000000000000000000000002
+002 sha256:0000000000000000000000000000000000000000000000000000000000000002
+003 sha1:0000000000000000000000000000000000000003
+003 sha256:0000000000000000000000000000000000000000000000000000000000000003
+004 sha1:0000000000000000000000000000000000000004
+004 sha256:0000000000000000000000000000000000000000000000000000000000000004
+005 sha1:0000000000000000000000000000000000000005
+005 sha256:0000000000000000000000000000000000000000000000000000000000000005
+006 sha1:0000000000000000000000000000000000000006
+006 sha256:0000000000000000000000000000000000000000000000000000000000000006
+007 sha1:0000000000000000000000000000000000000007
+007 sha256:0000000000000000000000000000000000000000000000000000000000000007
+# All zeros or Fs missing one or two hex segments.
+zero_1 sha1:000000000000000000000000000000000000000
+zero_1 sha256:000000000000000000000000000000000000000000000000000000000000000
+zero_2 sha1:00000000000000000000000000000000000000
+zero_2 sha256:00000000000000000000000000000000000000000000000000000000000000
+ff_1 sha1:fffffffffffffffffffffffffffffffffffffff
+ff_1 sha256:fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
+ff_2 sha1:ffffffffffffffffffffffffffffffffffffff
+ff_2 sha256:ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
+# More various invalid OIDs.
+numeric sha1:0123456789012345678901234567890123456789
+numeric sha256:0123456789012345678901234567890123456789012345678901234567890123
+deadbeef sha1:deadbeefdeadbeefdeadbeefdeadbeefdeadbeef
+deadbeef sha256:deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef
+deadbeef_short sha1:deadbeefdeadbeefdeadbeefdeadbeefdeadbee
+deadbeef_short sha256:deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbee
diff --git a/t/perf/.gitignore b/t/perf/.gitignore
new file mode 100644
index 0000000..72f5d0d
--- /dev/null
+++ b/t/perf/.gitignore
@@ -0,0 +1,4 @@
+/build/
+/test-results/
+/test-trace/
+/trash directory*/
diff --git a/t/perf/Makefile b/t/perf/Makefile
new file mode 100644
index 0000000..e4808ae
--- /dev/null
+++ b/t/perf/Makefile
@@ -0,0 +1,21 @@
+# Import tree-wide shared Makefile behavior and libraries
+include ../../shared.mak
+
+-include ../../config.mak
+export GIT_TEST_OPTIONS
+
+all: test-lint perf
+
+perf: pre-clean
+ ./run
+
+pre-clean:
+ rm -rf test-results test-trace
+
+clean:
+ rm -rf build "trash directory".* test-results test-trace
+
+test-lint:
+ $(MAKE) -C .. test-lint
+
+.PHONY: all perf pre-clean clean
diff --git a/t/perf/README b/t/perf/README
new file mode 100644
index 0000000..8f217d7
--- /dev/null
+++ b/t/perf/README
@@ -0,0 +1,210 @@
+Git performance tests
+=====================
+
+This directory holds performance testing scripts for git tools. The
+first part of this document describes the various ways in which you
+can run them.
+
+When fixing the tools or adding enhancements, you are strongly
+encouraged to add tests in this directory to cover what you are
+trying to fix or enhance. The later part of this short document
+describes how your test scripts should be organized.
+
+
+Running Tests
+-------------
+
+The easiest way to run tests is to say "make". This runs all
+the tests on the current git repository.
+
+ === Running 2 tests in this tree ===
+ [...]
+ Test this tree
+ ---------------------------------------------------------
+ 0001.1: rev-list --all 0.54(0.51+0.02)
+ 0001.2: rev-list --all --objects 6.14(5.99+0.11)
+ 7810.1: grep worktree, cheap regex 0.16(0.16+0.35)
+ 7810.2: grep worktree, expensive regex 7.90(29.75+0.37)
+ 7810.3: grep --cached, cheap regex 3.07(3.02+0.25)
+ 7810.4: grep --cached, expensive regex 9.39(30.57+0.24)
+
+Output format is in seconds "Elapsed(User + System)"
+
+You can compare multiple repositories and even git revisions with the
+'run' script:
+
+ $ ./run . origin/next /path/to/git-tree p0001-rev-list.sh
+
+where . stands for the current git tree. The full invocation is
+
+ ./run [<revision|directory>...] [--] [<test-script>...]
+
+A '.' argument is implied if you do not pass any other
+revisions/directories.
+
+You can also manually test this or another git build tree, and then
+call the aggregation script to summarize the results:
+
+ $ ./p0001-rev-list.sh
+ [...]
+ $ ./run /path/to/other/git -- ./p0001-rev-list.sh
+ [...]
+ $ ./aggregate.perl . /path/to/other/git ./p0001-rev-list.sh
+
+aggregate.perl has the same invocation as 'run', it just does not run
+anything beforehand.
+
+You can set the following variables (also in your config.mak):
+
+ GIT_PERF_REPEAT_COUNT
+ Number of times a test should be repeated for best-of-N
+ measurements. Defaults to 3.
+
+ GIT_PERF_MAKE_OPTS
+ Options to use when automatically building a git tree for
+ performance testing. E.g., -j6 would be useful. Passed
+ directly to make as "make $GIT_PERF_MAKE_OPTS".
+
+ GIT_PERF_MAKE_COMMAND
+ An arbitrary command that'll be run in place of the make
+ command, if set the GIT_PERF_MAKE_OPTS variable is
+ ignored. Useful in cases where source tree changes might
+ require issuing a different make command to different
+ revisions.
+
+ This can be (ab)used to monkeypatch or otherwise change the
+ tree about to be built. Note that the build directory can be
+ re-used for subsequent runs so the make command might get
+ executed multiple times on the same tree, but don't count on
+ any of that, that's an implementation detail that might change
+ in the future.
+
+ GIT_PERF_REPO
+ GIT_PERF_LARGE_REPO
+ Repositories to copy for the performance tests. The normal
+ repo should be at least git.git size. The large repo should
+ probably be about linux.git size for optimal results.
+ Both default to the git.git you are running from.
+
+ GIT_PERF_EXTRA
+ Boolean to enable additional tests. Most test scripts are
+ written to detect regressions between two versions of Git, and
+ the output will compare timings for individual tests between
+ those versions. Some scripts have additional tests which are not
+ run by default, that show patterns within a single version of
+ Git (e.g., performance of index-pack as the number of threads
+ changes). These can be enabled with GIT_PERF_EXTRA.
+
+ GIT_PERF_USE_SCALAR
+ Boolean indicating whether to register test repo(s) with Scalar
+ before executing tests.
+
+You can also pass the options taken by ordinary git tests; the most
+useful one is:
+
+--root=<directory>::
+ Create "trash" directories used to store all temporary data during
+ testing under <directory>, instead of the t/ directory.
+ Using this option with a RAM-based filesystem (such as tmpfs)
+ can massively speed up the test suite.
+
+
+Naming Tests
+------------
+
+The performance test files are named as:
+
+ pNNNN-commandname-details.sh
+
+where N is a decimal digit. The same conventions for choosing NNNN as
+for normal tests apply.
+
+
+Writing Tests
+-------------
+
+The perf script starts much like a normal test script, except it
+sources perf-lib.sh:
+
+ #!/bin/sh
+ #
+ # Copyright (c) 2005 Junio C Hamano
+ #
+
+ test_description='xxx performance test'
+ . ./perf-lib.sh
+
+After that you will want to use some of the following:
+
+ test_perf_fresh_repo # sets up an empty repository
+ test_perf_default_repo # sets up a "normal" repository
+ test_perf_large_repo # sets up a "large" repository
+
+ test_perf_default_repo sub # ditto, in a subdir "sub"
+
+ test_checkout_worktree # if you need the worktree too
+
+At least one of the first two is required!
+
+You can use test_expect_success as usual. In both test_expect_success
+and in test_perf, running "git" points to the version that is being
+perf-tested. The $MODERN_GIT variable points to the git wrapper for the
+currently checked-out version (i.e., the one that matches the t/perf
+scripts you are running). This is useful if your setup uses commands
+that only work with newer versions of git than what you might want to
+test (but obviously your new commands must still create a state that can
+be used by the older version of git you are testing).
+
+For actual performance tests, use
+
+ test_perf 'descriptive string' '
+ command1 &&
+ command2
+ '
+
+test_perf spawns a subshell, for lack of better options. This means
+that
+
+* you _must_ export all variables that you need in the subshell
+
+* you _must_ flag all variables that you want to persist from the
+ subshell with 'test_export':
+
+ test_perf 'descriptive string' '
+ foo=$(git rev-parse HEAD) &&
+ test_export foo
+ '
+
+ The so-exported variables are automatically marked for export in the
+ shell executing the perf test. For your convenience, test_export is
+ the same as export in the main shell.
+
+ This feature relies on a bit of magic using 'set' and 'source'.
+ While we have tried to make sure that it can cope with embedded
+ whitespace and other special characters, it will not work with
+ multi-line data.
+
+Rather than tracking the performance by run-time as `test_perf` does, you
+may also track output size by using `test_size`. The stdout of the
+function should be a single numeric value, which will be captured and
+shown in the aggregated output. For example:
+
+ test_perf 'time foo' '
+ ./foo >foo.out
+ '
+
+ test_size 'output size'
+ wc -c <foo.out
+ '
+
+might produce output like:
+
+ Test origin HEAD
+ -------------------------------------------------------------
+ 1234.1 time foo 0.37(0.79+0.02) 0.26(0.51+0.02) -29.7%
+ 1234.2 output size 4.3M 3.6M -14.7%
+
+The item being measured (and its units) is up to the test; the context
+and the test title should make it clear to the user whether bigger or
+smaller numbers are better. Unlike test_perf, the test code will only be
+run once, since output sizes tend to be more deterministic than timings.
diff --git a/t/perf/aggregate.perl b/t/perf/aggregate.perl
new file mode 100755
index 0000000..575d200
--- /dev/null
+++ b/t/perf/aggregate.perl
@@ -0,0 +1,357 @@
+#!/usr/bin/perl
+
+use lib '../../perl/build/lib';
+use strict;
+use warnings;
+use Getopt::Long;
+use Cwd qw(realpath);
+
+sub get_times {
+ my $name = shift;
+ open my $fh, "<", $name or return undef;
+ my $line = <$fh>;
+ return undef if not defined $line;
+ close $fh or die "cannot close $name: $!";
+ # times
+ if ($line =~ /^(?:(\d+):)?(\d+):(\d+(?:\.\d+)?) (\d+(?:\.\d+)?) (\d+(?:\.\d+)?)$/) {
+ my $rt = ((defined $1 ? $1 : 0.0)*60+$2)*60+$3;
+ return ($rt, $4, $5);
+ # size
+ } elsif ($line =~ /^\s*(\d+)$/) {
+ return $1;
+ } else {
+ die "bad input line: $line";
+ }
+}
+
+sub relative_change {
+ my ($r, $firstr) = @_;
+ if ($firstr > 0) {
+ return sprintf "%+.1f%%", 100.0*($r-$firstr)/$firstr;
+ } elsif ($r == 0) {
+ return "=";
+ } else {
+ return "+inf";
+ }
+}
+
+sub format_times {
+ my ($r, $u, $s, $firstr) = @_;
+ # no value means we did not finish the test
+ if (!defined $r) {
+ return "<missing>";
+ }
+ # a single value means we have a size, not times
+ if (!defined $u) {
+ return format_size($r, $firstr);
+ }
+ # otherwise, we have real/user/system times
+ my $out = sprintf "%.2f(%.2f+%.2f)", $r, $u, $s;
+ $out .= ' ' . relative_change($r, $firstr) if defined $firstr;
+ return $out;
+}
+
+sub usage {
+ print <<EOT;
+./aggregate.perl [options] [--] [<dir_or_rev>...] [--] [<test_script>...] >
+
+ Options:
+ --codespeed * Format output for Codespeed
+ --reponame <str> * Send given reponame to codespeed
+ --results-dir <str> * Directory where test results are located
+ --sort-by <str> * Sort output (only "regression" criteria is supported)
+ --subsection <str> * Use results from given subsection
+
+EOT
+ exit(1);
+}
+
+sub human_size {
+ my $n = shift;
+ my @units = ('', qw(K M G));
+ while ($n > 900 && @units > 1) {
+ $n /= 1000;
+ shift @units;
+ }
+ return $n unless length $units[0];
+ return sprintf '%.1f%s', $n, $units[0];
+}
+
+sub format_size {
+ my ($size, $first) = @_;
+ # match the width of a time: 0.00(0.00+0.00)
+ my $out = sprintf '%15s', human_size($size);
+ $out .= ' ' . relative_change($size, $first) if defined $first;
+ return $out;
+}
+
+sub sane_backticks {
+ open(my $fh, '-|', @_);
+ return <$fh>;
+}
+
+my (@dirs, %dirnames, %dirabbrevs, %prefixes, @tests,
+ $codespeed, $sortby, $subsection, $reponame);
+my $resultsdir = "test-results";
+
+Getopt::Long::Configure qw/ require_order /;
+
+my $rc = GetOptions("codespeed" => \$codespeed,
+ "reponame=s" => \$reponame,
+ "results-dir=s" => \$resultsdir,
+ "sort-by=s" => \$sortby,
+ "subsection=s" => \$subsection);
+usage() unless $rc;
+
+while (scalar @ARGV) {
+ my $arg = $ARGV[0];
+ my $dir;
+ my $prefix = '';
+ last if -f $arg or $arg eq "--";
+ if (! -d $arg) {
+ my $rev = sane_backticks(qw(git rev-parse --verify), $arg);
+ chomp $rev;
+ $dir = "build/".$rev;
+ } elsif ($arg eq '.') {
+ $dir = '.';
+ } else {
+ $dir = realpath($arg);
+ $dirnames{$dir} = $dir;
+ $prefix .= 'bindir';
+ }
+ push @dirs, $dir;
+ $dirnames{$dir} ||= $arg;
+ $prefix .= $dir;
+ $prefix =~ tr/^a-zA-Z0-9/_/c;
+ $prefixes{$dir} = $prefix . '.';
+ shift @ARGV;
+}
+
+if (not @dirs) {
+ @dirs = ('.');
+}
+$dirnames{'.'} = $dirabbrevs{'.'} = "this tree";
+$prefixes{'.'} = '';
+
+shift @ARGV if scalar @ARGV and $ARGV[0] eq "--";
+
+@tests = @ARGV;
+if (not @tests) {
+ @tests = glob "p????-*.sh";
+}
+
+if (! $subsection and
+ exists $ENV{GIT_PERF_SUBSECTION} and
+ $ENV{GIT_PERF_SUBSECTION} ne "") {
+ $subsection = $ENV{GIT_PERF_SUBSECTION};
+}
+
+if ($subsection) {
+ $resultsdir .= "/" . $subsection;
+}
+
+my @subtests;
+my %shorttests;
+for my $t (@tests) {
+ $t =~ s{(?:.*/)?(p(\d+)-[^/]+)\.sh$}{$1} or die "bad test name: $t";
+ my $n = $2;
+ my $fname = "$resultsdir/$t.subtests";
+ open my $fp, "<", $fname or die "cannot open $fname: $!";
+ for (<$fp>) {
+ chomp;
+ /^(\d+)$/ or die "malformed subtest line: $_";
+ push @subtests, "$t.$1";
+ $shorttests{"$t.$1"} = "$n.$1";
+ }
+ close $fp or die "cannot close $fname: $!";
+}
+
+sub read_descr {
+ my $name = shift;
+ open my $fh, "<", $name or return "<error reading description>";
+ binmode $fh, ":utf8" or die "PANIC on binmode: $!";
+ my $line = <$fh>;
+ close $fh or die "cannot close $name";
+ chomp $line;
+ return $line;
+}
+
+sub have_duplicate {
+ my %seen;
+ for (@_) {
+ return 1 if exists $seen{$_};
+ $seen{$_} = 1;
+ }
+ return 0;
+}
+sub have_slash {
+ for (@_) {
+ return 1 if m{/};
+ }
+ return 0;
+}
+
+sub display_dir {
+ my ($d) = @_;
+ return exists $dirabbrevs{$d} ? $dirabbrevs{$d} : $dirnames{$d};
+}
+
+sub print_default_results {
+ my %descrs;
+ my $descrlen = 4; # "Test"
+ for my $t (@subtests) {
+ $descrs{$t} = $shorttests{$t}.": ".read_descr("$resultsdir/$t.descr");
+ $descrlen = length $descrs{$t} if length $descrs{$t}>$descrlen;
+ }
+
+ my %newdirabbrevs = %dirabbrevs;
+ while (!have_duplicate(values %newdirabbrevs)) {
+ %dirabbrevs = %newdirabbrevs;
+ last if !have_slash(values %dirabbrevs);
+ %newdirabbrevs = %dirabbrevs;
+ for (values %newdirabbrevs) {
+ s{^[^/]*/}{};
+ }
+ }
+
+ my %times;
+ my @colwidth = ((0)x@dirs);
+ for my $i (0..$#dirs) {
+ my $w = length display_dir($dirs[$i]);
+ $colwidth[$i] = $w if $w > $colwidth[$i];
+ }
+ for my $t (@subtests) {
+ my $firstr;
+ for my $i (0..$#dirs) {
+ my $d = $dirs[$i];
+ my $base = "$resultsdir/$prefixes{$d}$t";
+ $times{$prefixes{$d}.$t} = [get_times("$base.result")];
+ my ($r,$u,$s) = @{$times{$prefixes{$d}.$t}};
+ my $w = length format_times($r,$u,$s,$firstr);
+ $colwidth[$i] = $w if $w > $colwidth[$i];
+ $firstr = $r unless defined $firstr;
+ }
+ }
+ my $totalwidth = 3*@dirs+$descrlen;
+ $totalwidth += $_ for (@colwidth);
+
+ printf "%-${descrlen}s", "Test";
+ for my $i (0..$#dirs) {
+ printf " %-$colwidth[$i]s", display_dir($dirs[$i]);
+ }
+ print "\n";
+ print "-"x$totalwidth, "\n";
+ for my $t (@subtests) {
+ printf "%-${descrlen}s", $descrs{$t};
+ my $firstr;
+ for my $i (0..$#dirs) {
+ my $d = $dirs[$i];
+ my ($r,$u,$s) = @{$times{$prefixes{$d}.$t}};
+ printf " %-$colwidth[$i]s", format_times($r,$u,$s,$firstr);
+ $firstr = $r unless defined $firstr;
+ }
+ print "\n";
+ }
+}
+
+sub print_sorted_results {
+ my ($sortby) = @_;
+
+ if ($sortby ne "regression") {
+ print "Only 'regression' is supported as '--sort-by' argument\n";
+ usage();
+ }
+
+ my @evolutions;
+ for my $t (@subtests) {
+ my ($prevr, $prevu, $prevs, $prevrev);
+ for my $i (0..$#dirs) {
+ my $d = $dirs[$i];
+ my ($r, $u, $s) = get_times("$resultsdir/$prefixes{$d}$t.result");
+ if ($i > 0 and defined $r and defined $prevr and $prevr > 0) {
+ my $percent = 100.0 * ($r - $prevr) / $prevr;
+ push @evolutions, { "percent" => $percent,
+ "test" => $t,
+ "prevrev" => $prevrev,
+ "rev" => $d,
+ "prevr" => $prevr,
+ "r" => $r,
+ "prevu" => $prevu,
+ "u" => $u,
+ "prevs" => $prevs,
+ "s" => $s};
+ }
+ ($prevr, $prevu, $prevs, $prevrev) = ($r, $u, $s, $d);
+ }
+ }
+
+ my @sorted_evolutions = sort { $b->{percent} <=> $a->{percent} } @evolutions;
+
+ for my $e (@sorted_evolutions) {
+ printf "%+.1f%%", $e->{percent};
+ print " " . $e->{test};
+ print " " . format_times($e->{prevr}, $e->{prevu}, $e->{prevs});
+ print " " . format_times($e->{r}, $e->{u}, $e->{s});
+ print " " . display_dir($e->{prevrev});
+ print " " . display_dir($e->{rev});
+ print "\n";
+ }
+}
+
+sub print_codespeed_results {
+ my ($subsection) = @_;
+
+ my $project = "Git";
+
+ my $executable = `uname -s -m`;
+ chomp $executable;
+
+ if ($subsection) {
+ $executable .= ", " . $subsection;
+ }
+
+ my $environment;
+ if ($reponame) {
+ $environment = $reponame;
+ } elsif (exists $ENV{GIT_PERF_REPO_NAME} and $ENV{GIT_PERF_REPO_NAME} ne "") {
+ $environment = $ENV{GIT_PERF_REPO_NAME};
+ } else {
+ $environment = `uname -r`;
+ chomp $environment;
+ }
+
+ my @data;
+
+ for my $t (@subtests) {
+ for my $d (@dirs) {
+ my $commitid = $prefixes{$d};
+ $commitid =~ s/^build_//;
+ $commitid =~ s/\.$//;
+ my ($result_value, $u, $s) = get_times("$resultsdir/$prefixes{$d}$t.result");
+
+ my %vals = (
+ "commitid" => $commitid,
+ "project" => $project,
+ "branch" => $dirnames{$d},
+ "executable" => $executable,
+ "benchmark" => $shorttests{$t} . " " . read_descr("$resultsdir/$t.descr"),
+ "environment" => $environment,
+ "result_value" => $result_value,
+ );
+ push @data, \%vals;
+ }
+ }
+
+ require JSON;
+ print JSON::to_json(\@data, {utf8 => 1, pretty => 1, canonical => 1}), "\n";
+}
+
+binmode STDOUT, ":utf8" or die "PANIC on binmode: $!";
+
+if ($codespeed) {
+ print_codespeed_results($subsection);
+} elsif (defined $sortby) {
+ print_sorted_results($sortby);
+} else {
+ print_default_results();
+}
diff --git a/t/perf/bisect_regression b/t/perf/bisect_regression
new file mode 100755
index 0000000..ce47e16
--- /dev/null
+++ b/t/perf/bisect_regression
@@ -0,0 +1,73 @@
+#!/bin/sh
+
+# Read a line coming from `./aggregate.perl --sort-by regression ...`
+# and automatically bisect to find the commit responsible for the
+# performance regression.
+#
+# Lines from `./aggregate.perl --sort-by regression ...` look like:
+#
+# +100.0% p7821-grep-engines-fixed.1 0.04(0.10+0.03) 0.08(0.11+0.08) v2.14.3 v2.15.1
+# +33.3% p7820-grep-engines.1 0.03(0.08+0.02) 0.04(0.08+0.02) v2.14.3 v2.15.1
+#
+
+die () {
+ echo >&2 "error: $*"
+ exit 1
+}
+
+while [ $# -gt 0 ]; do
+ arg="$1"
+ case "$arg" in
+ --help)
+ echo "usage: $0 [--config file] [--subsection subsection]"
+ exit 0
+ ;;
+ --config)
+ shift
+ GIT_PERF_CONFIG_FILE=$(cd "$(dirname "$1")"; pwd)/$(basename "$1")
+ export GIT_PERF_CONFIG_FILE
+ shift ;;
+ --subsection)
+ shift
+ GIT_PERF_SUBSECTION="$1"
+ export GIT_PERF_SUBSECTION
+ shift ;;
+ --*)
+ die "unrecognised option: '$arg'" ;;
+ *)
+ die "unknown argument '$arg'"
+ ;;
+ esac
+done
+
+read -r regression subtest oldtime newtime oldrev newrev
+
+test_script=$(echo "$subtest" | sed -e 's/\(.*\)\.[0-9]*$/\1.sh/')
+test_number=$(echo "$subtest" | sed -e 's/.*\.\([0-9]*\)$/\1/')
+
+# oldtime and newtime are decimal number, not integers
+
+oldtime=$(echo "$oldtime" | sed -e 's/^\([0-9]\+\.[0-9]\+\).*$/\1/')
+newtime=$(echo "$newtime" | sed -e 's/^\([0-9]\+\.[0-9]\+\).*$/\1/')
+
+test $(echo "$newtime" "$oldtime" | awk '{ print ($1 > $2) }') = 1 ||
+ die "New time '$newtime' should be greater than old time '$oldtime'"
+
+tmpdir=$(mktemp -d -t bisect_regression_XXXXXX) || die "Failed to create temp directory"
+echo "$oldtime" >"$tmpdir/oldtime" || die "Failed to write to '$tmpdir/oldtime'"
+echo "$newtime" >"$tmpdir/newtime" || die "Failed to write to '$tmpdir/newtime'"
+
+# Bisecting must be performed from the top level directory (even with --no-checkout)
+(
+ toplevel_dir=$(git rev-parse --show-toplevel) || die "Failed to find top level directory"
+ cd "$toplevel_dir" || die "Failed to cd into top level directory '$toplevel_dir'"
+
+ git bisect start --no-checkout "$newrev" "$oldrev" || die "Failed to start bisecting"
+
+ git bisect run t/perf/bisect_run_script "$test_script" "$test_number" "$tmpdir"
+ res="$?"
+
+ git bisect reset
+
+ exit "$res"
+)
diff --git a/t/perf/bisect_run_script b/t/perf/bisect_run_script
new file mode 100755
index 0000000..3ebaf15
--- /dev/null
+++ b/t/perf/bisect_run_script
@@ -0,0 +1,53 @@
+#!/bin/sh
+
+script="$1"
+test_number="$2"
+info_dir="$3"
+
+# This aborts the bisection immediately
+die () {
+ echo >&2 "error: $*"
+ exit 255
+}
+
+bisect_head=$(git rev-parse --verify BISECT_HEAD) || die "Failed to find BISECT_HEAD ref"
+
+script_number=$(echo "$script" | sed -e "s/^p\([0-9]*\).*\$/\1/") || die "Failed to get script number for '$script'"
+
+oldtime=$(cat "$info_dir/oldtime") || die "Failed to access '$info_dir/oldtime'"
+newtime=$(cat "$info_dir/newtime") || die "Failed to access '$info_dir/newtime'"
+
+cd t/perf || die "Failed to cd into 't/perf'"
+
+result_file="$info_dir/perf_${script_number}_${bisect_head}_results.txt"
+
+GIT_PERF_DIRS_OR_REVS="$bisect_head"
+export GIT_PERF_DIRS_OR_REVS
+
+# Don't use codespeed
+GIT_PERF_CODESPEED_OUTPUT=
+GIT_PERF_SEND_TO_CODESPEED=
+export GIT_PERF_CODESPEED_OUTPUT
+export GIT_PERF_SEND_TO_CODESPEED
+
+./run "$script" >"$result_file" 2>&1 || die "Failed to run perf test '$script'"
+
+rtime=$(sed -n "s/^$script_number\.$test_number:.*\([0-9]\+\.[0-9]\+\)(.*).*\$/\1/p" "$result_file")
+
+echo "newtime: $newtime"
+echo "rtime: $rtime"
+echo "oldtime: $oldtime"
+
+# Compare ($newtime - $rtime) with ($rtime - $oldtime)
+# Times are decimal number, not integers
+
+if test $(echo "$newtime" "$rtime" "$oldtime" | awk '{ print ($1 - $2 > $2 - $3) }') = 1
+then
+ # Current commit is considered "good/old"
+ echo "$rtime" >"$info_dir/oldtime"
+ exit 0
+else
+ # Current commit is considered "bad/new"
+ echo "$rtime" >"$info_dir/newtime"
+ exit 1
+fi
diff --git a/t/perf/config b/t/perf/config
new file mode 100644
index 0000000..b92768b
--- /dev/null
+++ b/t/perf/config
@@ -0,0 +1,2 @@
+[gc]
+ auto = 0
diff --git a/t/perf/lib-bitmap.sh b/t/perf/lib-bitmap.sh
new file mode 100644
index 0000000..55a8feb
--- /dev/null
+++ b/t/perf/lib-bitmap.sh
@@ -0,0 +1,100 @@
+# Helper functions for testing bitmap performance; see p5310.
+
+test_full_bitmap () {
+ test_perf 'simulated clone' '
+ git pack-objects --stdout --all </dev/null >/dev/null
+ '
+
+ test_perf 'simulated fetch' '
+ have=$(git rev-list HEAD~100 -1) &&
+ {
+ echo HEAD &&
+ echo ^$have
+ } | git pack-objects --revs --stdout >/dev/null
+ '
+
+ test_perf 'pack to file (bitmap)' '
+ git pack-objects --use-bitmap-index --all pack1b </dev/null >/dev/null
+ '
+
+ test_perf 'rev-list (commits)' '
+ git rev-list --all --use-bitmap-index >/dev/null
+ '
+
+ test_perf 'rev-list (objects)' '
+ git rev-list --all --use-bitmap-index --objects >/dev/null
+ '
+
+ test_perf 'rev-list with tag negated via --not --all (objects)' '
+ git rev-list perf-tag --not --all --use-bitmap-index --objects >/dev/null
+ '
+
+ test_perf 'rev-list with negative tag (objects)' '
+ git rev-list HEAD --not perf-tag --use-bitmap-index --objects >/dev/null
+ '
+
+ test_perf 'rev-list count with blob:none' '
+ git rev-list --use-bitmap-index --count --objects --all \
+ --filter=blob:none >/dev/null
+ '
+
+ test_perf 'rev-list count with blob:limit=1k' '
+ git rev-list --use-bitmap-index --count --objects --all \
+ --filter=blob:limit=1k >/dev/null
+ '
+
+ test_perf 'rev-list count with tree:0' '
+ git rev-list --use-bitmap-index --count --objects --all \
+ --filter=tree:0 >/dev/null
+ '
+
+ test_perf 'simulated partial clone' '
+ git pack-objects --stdout --all --filter=blob:none </dev/null >/dev/null
+ '
+}
+
+test_partial_bitmap () {
+ test_perf 'clone (partial bitmap)' '
+ git pack-objects --stdout --all </dev/null >/dev/null
+ '
+
+ test_perf 'pack to file (partial bitmap)' '
+ git pack-objects --use-bitmap-index --all pack2b </dev/null >/dev/null
+ '
+
+ test_perf 'rev-list with tree filter (partial bitmap)' '
+ git rev-list --use-bitmap-index --count --objects --all \
+ --filter=tree:0 >/dev/null
+ '
+}
+
+test_pack_bitmap () {
+ test_perf "repack to disk" '
+ git repack -ad
+ '
+
+ test_full_bitmap
+
+ test_expect_success "create partial bitmap state" '
+ # pick a commit to represent the repo tip in the past
+ cutoff=$(git rev-list HEAD~100 -1) &&
+ orig_tip=$(git rev-parse HEAD) &&
+
+ # now kill off all of the refs and pretend we had
+ # just the one tip
+ rm -rf .git/logs .git/refs/* .git/packed-refs &&
+ git update-ref HEAD $cutoff &&
+
+ # and then repack, which will leave us with a nice
+ # big bitmap pack of the "old" history, and all of
+ # the new history will be loose, as if it had been pushed
+ # up incrementally and exploded via unpack-objects
+ git repack -Ad &&
+
+ # and now restore our original tip, as if the pushes
+ # had happened
+ git update-ref HEAD $orig_tip
+ '
+
+ test_partial_bitmap
+}
diff --git a/t/perf/lib-pack.sh b/t/perf/lib-pack.sh
new file mode 100644
index 0000000..d3865db
--- /dev/null
+++ b/t/perf/lib-pack.sh
@@ -0,0 +1,25 @@
+# Helpers for dealing with large numbers of packs.
+
+# create $1 nonsense packs, each with a single blob
+create_packs () {
+ perl -le '
+ my ($n) = @ARGV;
+ for (1..$n) {
+ print "blob";
+ print "data <<EOF";
+ print "$_";
+ print "EOF";
+ print "checkpoint"
+ }
+ ' "$@" |
+ git fast-import
+}
+
+# create a large number of packs, disabling any gc which might
+# cause us to repack them
+setup_many_packs () {
+ git config gc.auto 0 &&
+ git config gc.autopacklimit 0 &&
+ git config fastimport.unpacklimit 0 &&
+ create_packs 500
+}
diff --git a/t/perf/min_time.perl b/t/perf/min_time.perl
new file mode 100755
index 0000000..c1a2717
--- /dev/null
+++ b/t/perf/min_time.perl
@@ -0,0 +1,21 @@
+#!/usr/bin/perl
+
+my $minrt = 1e100;
+my $min;
+
+while (<>) {
+ # [h:]m:s.xx U.xx S.xx
+ /^(?:(\d+):)?(\d+):(\d+(?:\.\d+)?) (\d+(?:\.\d+)?) (\d+(?:\.\d+)?)$/
+ or die "bad input line: $_";
+ my $rt = ((defined $1 ? $1 : 0.0)*60+$2)*60+$3;
+ if ($rt < $minrt) {
+ $min = $_;
+ $minrt = $rt;
+ }
+}
+
+if (!defined $min) {
+ die "no input found";
+}
+
+print $min;
diff --git a/t/perf/p0000-perf-lib-sanity.sh b/t/perf/p0000-perf-lib-sanity.sh
new file mode 100755
index 0000000..002c21e
--- /dev/null
+++ b/t/perf/p0000-perf-lib-sanity.sh
@@ -0,0 +1,57 @@
+#!/bin/sh
+
+test_description='Tests whether perf-lib facilities work'
+. ./perf-lib.sh
+
+test_perf_default_repo
+
+test_perf 'test_perf_default_repo works' '
+ foo=$(git rev-parse HEAD) &&
+ test_export foo
+'
+
+test_checkout_worktree
+
+test_perf 'test_checkout_worktree works' '
+ wt=$(find . | wc -l) &&
+ idx=$(git ls-files | wc -l) &&
+ test $wt -gt $idx
+'
+
+baz=baz
+test_export baz
+
+test_expect_success 'test_export works' '
+ echo "$foo" &&
+ test "$foo" = "$(git rev-parse HEAD)" &&
+ echo "$baz" &&
+ test "$baz" = baz
+'
+
+test_perf 'export a weird var' '
+ bar="weird # variable" &&
+ test_export bar
+'
+
+test_perf 'éḿíẗ ńöń-ÃŚĆÃà ćḧáŕáćẗéŕś' 'true'
+
+test_expect_success 'test_export works with weird vars' '
+ echo "$bar" &&
+ test "$bar" = "weird # variable"
+'
+
+test_perf 'important variables available in subshells' '
+ test -n "$HOME" &&
+ test -n "$TEST_DIRECTORY" &&
+ test -n "$TRASH_DIRECTORY" &&
+ test -n "$GIT_BUILD_DIR"
+'
+
+test_perf 'test-lib-functions correctly loaded in subshells' '
+ : >a &&
+ test_path_is_file a &&
+ : >b &&
+ test_cmp a b
+'
+
+test_done
diff --git a/t/perf/p0001-rev-list.sh b/t/perf/p0001-rev-list.sh
new file mode 100755
index 0000000..3042a85
--- /dev/null
+++ b/t/perf/p0001-rev-list.sh
@@ -0,0 +1,48 @@
+#!/bin/sh
+
+test_description="Tests history walking performance"
+
+. ./perf-lib.sh
+
+test_perf_default_repo
+
+test_perf 'rev-list --all' '
+ git rev-list --all >/dev/null
+'
+
+test_perf 'rev-list --all --objects' '
+ git rev-list --all --objects >/dev/null
+'
+
+test_perf 'rev-list --parents' '
+ git rev-list --parents HEAD >/dev/null
+'
+
+test_expect_success 'create dummy file' '
+ echo unlikely-to-already-be-there >dummy &&
+ git add dummy &&
+ git commit -m dummy
+'
+
+test_perf 'rev-list -- dummy' '
+ git rev-list HEAD -- dummy
+'
+
+test_perf 'rev-list --parents -- dummy' '
+ git rev-list --parents HEAD -- dummy
+'
+
+test_expect_success 'create new unreferenced commit' '
+ commit=$(git commit-tree HEAD^{tree} -p HEAD) &&
+ test_export commit
+'
+
+test_perf 'rev-list $commit --not --all' '
+ git rev-list $commit --not --all >/dev/null
+'
+
+test_perf 'rev-list --objects $commit --not --all' '
+ git rev-list --objects $commit --not --all >/dev/null
+'
+
+test_done
diff --git a/t/perf/p0002-read-cache.sh b/t/perf/p0002-read-cache.sh
new file mode 100755
index 0000000..cdd105a
--- /dev/null
+++ b/t/perf/p0002-read-cache.sh
@@ -0,0 +1,14 @@
+#!/bin/sh
+
+test_description="Tests performance of reading the index"
+
+. ./perf-lib.sh
+
+test_perf_default_repo
+
+count=1000
+test_perf "read_cache/discard_cache $count times" "
+ test-tool read-cache $count
+"
+
+test_done
diff --git a/t/perf/p0003-delta-base-cache.sh b/t/perf/p0003-delta-base-cache.sh
new file mode 100755
index 0000000..62369ea
--- /dev/null
+++ b/t/perf/p0003-delta-base-cache.sh
@@ -0,0 +1,31 @@
+#!/bin/sh
+
+test_description='Test operations that emphasize the delta base cache.
+
+We look at both "log --raw", which should put only trees into the delta cache,
+and "log -Sfoo --raw", which should look at both trees and blobs.
+
+Any effects will be emphasized if the test repository is fully packed (loose
+objects obviously do not use the delta base cache at all). It is also
+emphasized if the pack has long delta chains (e.g., as produced by "gc
+--aggressive"), though cache is still quite noticeable even with the default
+depth of 50.
+
+The setting of core.deltaBaseCacheLimit in the source repository is also
+relevant (depending on the size of your test repo), so be sure it is consistent
+between runs.
+'
+. ./perf-lib.sh
+
+test_perf_large_repo
+
+# puts mostly trees into the delta base cache
+test_perf 'log --raw' '
+ git log --raw >/dev/null
+'
+
+test_perf 'log -S' '
+ git log --raw -Sfoo >/dev/null
+'
+
+test_done
diff --git a/t/perf/p0004-lazy-init-name-hash.sh b/t/perf/p0004-lazy-init-name-hash.sh
new file mode 100755
index 0000000..85be14e
--- /dev/null
+++ b/t/perf/p0004-lazy-init-name-hash.sh
@@ -0,0 +1,56 @@
+#!/bin/sh
+
+test_description='Tests multi-threaded lazy_init_name_hash'
+. ./perf-lib.sh
+
+test_perf_large_repo
+test_checkout_worktree
+
+test_expect_success 'verify both methods build the same hashmaps' '
+ test-tool lazy-init-name-hash --dump --single >out.single &&
+ if test-tool lazy-init-name-hash --dump --multi >out.multi
+ then
+ test_set_prereq REPO_BIG_ENOUGH_FOR_MULTI &&
+ sort <out.single >sorted.single &&
+ sort <out.multi >sorted.multi &&
+ test_cmp sorted.single sorted.multi
+ fi
+'
+
+test_expect_success 'calibrate' '
+ entries=$(wc -l <out.single) &&
+
+ case $entries in
+ ?) count=1000000 ;;
+ ??) count=100000 ;;
+ ???) count=10000 ;;
+ ????) count=1000 ;;
+ ?????) count=100 ;;
+ ??????) count=10 ;;
+ *) count=1 ;;
+ esac &&
+ export count &&
+
+ case $entries in
+ 1) entries_desc="1 entry" ;;
+ *) entries_desc="$entries entries" ;;
+ esac &&
+
+ case $count in
+ 1) count_desc="1 round" ;;
+ *) count_desc="$count rounds" ;;
+ esac &&
+
+ desc="$entries_desc, $count_desc" &&
+ export desc
+'
+
+test_perf "single-threaded, $desc" "
+ test-tool lazy-init-name-hash --single --count=$count
+"
+
+test_perf "multi-threaded, $desc" --prereq REPO_BIG_ENOUGH_FOR_MULTI "
+ test-tool lazy-init-name-hash --multi --count=$count
+"
+
+test_done
diff --git a/t/perf/p0005-status.sh b/t/perf/p0005-status.sh
new file mode 100755
index 0000000..ca58d6c
--- /dev/null
+++ b/t/perf/p0005-status.sh
@@ -0,0 +1,49 @@
+#!/bin/sh
+#
+# This test measures the performance of various read-tree
+# and status operations. It is primarily interested in
+# the algorithmic costs of index operations and recursive
+# tree traversal -- and NOT disk I/O on thousands of files.
+
+test_description="Tests performance of read-tree"
+
+. ./perf-lib.sh
+
+test_perf_default_repo
+
+# If the test repo was generated by ./repos/many-files.sh
+# then we know something about the data shape and branches,
+# so we can isolate testing to the ballast-related commits
+# and setup sparse-checkout so we don't have to populate
+# the ballast files and directories.
+#
+# Otherwise, we make some general assumptions about the
+# repo and consider the entire history of the current
+# branch to be the ballast.
+
+test_expect_success "setup repo" '
+ if git rev-parse --verify refs/heads/p0006-ballast^{commit}
+ then
+ echo Assuming synthetic repo from many-files.sh &&
+ git branch br_base master &&
+ git branch br_ballast p0006-ballast &&
+ git config --local core.sparsecheckout 1 &&
+ cat >.git/info/sparse-checkout <<-EOF
+ /*
+ !ballast/*
+ EOF
+ else
+ echo Assuming non-synthetic repo... &&
+ git branch br_base $(git rev-list HEAD | tail -n 1) &&
+ git branch br_ballast HEAD
+ fi &&
+ git checkout -q br_ballast &&
+ nr_files=$(git ls-files | wc -l)
+'
+
+test_perf "read-tree status br_ballast ($nr_files)" '
+ git read-tree HEAD &&
+ git status
+'
+
+test_done
diff --git a/t/perf/p0006-read-tree-checkout.sh b/t/perf/p0006-read-tree-checkout.sh
new file mode 100755
index 0000000..325566e
--- /dev/null
+++ b/t/perf/p0006-read-tree-checkout.sh
@@ -0,0 +1,75 @@
+#!/bin/sh
+#
+# This test measures the performance of various read-tree
+# and checkout operations. It is primarily interested in
+# the algorithmic costs of index operations and recursive
+# tree traversal -- and NOT disk I/O on thousands of files.
+
+test_description="Tests performance of read-tree"
+
+. ./perf-lib.sh
+
+test_perf_default_repo
+
+# If the test repo was generated by ./repos/many-files.sh
+# then we know something about the data shape and branches,
+# so we can isolate testing to the ballast-related commits
+# and setup sparse-checkout so we don't have to populate
+# the ballast files and directories.
+#
+# Otherwise, we make some general assumptions about the
+# repo and consider the entire history of the current
+# branch to be the ballast.
+
+test_expect_success "setup repo" '
+ if git rev-parse --verify refs/heads/p0006-ballast^{commit}
+ then
+ echo Assuming synthetic repo from many-files.sh &&
+ git branch br_base master &&
+ git branch br_ballast p0006-ballast^ &&
+ git branch br_ballast_alias p0006-ballast^ &&
+ git branch br_ballast_plus_1 p0006-ballast &&
+ git config --local core.sparsecheckout 1 &&
+ cat >.git/info/sparse-checkout <<-EOF
+ /*
+ !ballast/*
+ EOF
+ else
+ echo Assuming non-synthetic repo... &&
+ git branch br_base $(git rev-list HEAD | tail -n 1) &&
+ git branch br_ballast HEAD^ || error "no ancestor commit from current head" &&
+ git branch br_ballast_alias HEAD^ &&
+ git branch br_ballast_plus_1 HEAD
+ fi &&
+ git checkout -q br_ballast &&
+ nr_files=$(git ls-files | wc -l)
+'
+
+test_perf "read-tree br_base br_ballast ($nr_files)" '
+ git read-tree -n -m br_base br_ballast
+'
+
+test_perf "read-tree br_ballast_plus_1 ($nr_files)" '
+ # Run read-tree 100 times for clearer performance results & comparisons
+ for i in $(test_seq 100)
+ do
+ git read-tree -n -m br_ballast_plus_1 || return 1
+ done
+'
+
+test_perf "switch between br_base br_ballast ($nr_files)" '
+ git checkout -q br_base &&
+ git checkout -q br_ballast
+'
+
+test_perf "switch between br_ballast br_ballast_plus_1 ($nr_files)" '
+ git checkout -q br_ballast_plus_1 &&
+ git checkout -q br_ballast
+'
+
+test_perf "switch between aliases ($nr_files)" '
+ git checkout -q br_ballast_alias &&
+ git checkout -q br_ballast
+'
+
+test_done
diff --git a/t/perf/p0007-write-cache.sh b/t/perf/p0007-write-cache.sh
new file mode 100755
index 0000000..25d8ff7
--- /dev/null
+++ b/t/perf/p0007-write-cache.sh
@@ -0,0 +1,29 @@
+#!/bin/sh
+
+test_description="Tests performance of writing the index"
+
+. ./perf-lib.sh
+
+test_perf_default_repo
+
+test_expect_success "setup repo" '
+ if git rev-parse --verify refs/heads/p0006-ballast^{commit}
+ then
+ echo Assuming synthetic repo from many-files.sh &&
+ git config --local core.sparsecheckout 1 &&
+ cat >.git/info/sparse-checkout <<-EOF
+ /*
+ !ballast/*
+ EOF
+ else
+ echo Assuming non-synthetic repo...
+ fi &&
+ nr_files=$(git ls-files | wc -l)
+'
+
+count=3
+test_perf "write_locked_index $count times ($nr_files files)" "
+ test-tool write-cache $count
+"
+
+test_done
diff --git a/t/perf/p0008-odb-fsync.sh b/t/perf/p0008-odb-fsync.sh
new file mode 100755
index 0000000..b3a90f3
--- /dev/null
+++ b/t/perf/p0008-odb-fsync.sh
@@ -0,0 +1,82 @@
+#!/bin/sh
+#
+# This test measures the performance of adding new files to the object
+# database. The test was originally added to measure the effect of the
+# core.fsyncMethod=batch mode, which is why we are testing different values of
+# that setting explicitly and creating a lot of unique objects.
+
+test_description="Tests performance of adding things to the object database"
+
+. ./perf-lib.sh
+
+. $TEST_DIRECTORY/lib-unique-files.sh
+
+test_perf_fresh_repo
+test_checkout_worktree
+
+dir_count=10
+files_per_dir=50
+total_files=$((dir_count * files_per_dir))
+
+populate_files () {
+ test_create_unique_files $dir_count $files_per_dir files
+}
+
+setup_repo () {
+ (rm -rf .git || 1) &&
+ git init &&
+ test_commit first &&
+ populate_files
+}
+
+test_perf_fsync_cfgs () {
+ local method &&
+ local cfg &&
+ for method in none fsync batch writeout-only
+ do
+ case $method in
+ none)
+ cfg="-c core.fsync=none"
+ ;;
+ *)
+ cfg="-c core.fsync=loose-object -c core.fsyncMethod=$method"
+ esac &&
+
+ # Set GIT_TEST_FSYNC=1 explicitly since fsync is normally
+ # disabled by t/test-lib.sh.
+ if ! test_perf "$1 (fsyncMethod=$method)" \
+ --setup "$2" \
+ "GIT_TEST_FSYNC=1 git $cfg $3"
+ then
+ break
+ fi
+ done
+}
+
+test_perf_fsync_cfgs "add $total_files files" \
+ "setup_repo" \
+ "add -- files"
+
+test_perf_fsync_cfgs "stash $total_files files" \
+ "setup_repo" \
+ "stash push -u -- files"
+
+test_perf_fsync_cfgs "unpack $total_files files" \
+ "
+ setup_repo &&
+ git -c core.fsync=none add -- files &&
+ git -c core.fsync=none commit -q -m second &&
+ echo HEAD | git pack-objects -q --stdout --revs >test_pack.pack &&
+ setup_repo
+ " \
+ "unpack-objects -q <test_pack.pack"
+
+test_perf_fsync_cfgs "commit $total_files files" \
+ "
+ setup_repo &&
+ git -c core.fsync=none add -- files &&
+ populate_files
+ " \
+ "commit -q -a -m test"
+
+test_done
diff --git a/t/perf/p0071-sort.sh b/t/perf/p0071-sort.sh
new file mode 100755
index 0000000..ae4ddac
--- /dev/null
+++ b/t/perf/p0071-sort.sh
@@ -0,0 +1,52 @@
+#!/bin/sh
+
+test_description='Basic sort performance tests'
+. ./perf-lib.sh
+
+test_perf_default_repo
+
+test_expect_success 'setup' '
+ git ls-files --stage "*.[ch]" "*.sh" |
+ cut -f2 -d" " |
+ git cat-file --batch >unsorted
+'
+
+test_perf 'sort(1) unsorted' '
+ sort <unsorted >sorted
+'
+
+test_expect_success 'reverse' '
+ sort -r <unsorted >reversed
+'
+
+for file in sorted reversed
+do
+ test_perf "sort(1) $file" "
+ sort <$file >actual
+ "
+done
+
+for file in unsorted sorted reversed
+do
+
+ test_perf "string_list_sort() $file" "
+ test-tool string-list sort <$file >actual
+ "
+
+ test_expect_success "string_list_sort() $file sorts like sort(1)" "
+ test_cmp_bin sorted actual
+ "
+done
+
+for file in unsorted sorted reversed
+do
+ test_perf "DEFINE_LIST_SORT $file" "
+ test-tool mergesort sort <$file >actual
+ "
+
+ test_expect_success "DEFINE_LIST_SORT $file sorts like sort(1)" "
+ test_cmp_bin sorted actual
+ "
+done
+
+test_done
diff --git a/t/perf/p0090-cache-tree.sh b/t/perf/p0090-cache-tree.sh
new file mode 100755
index 0000000..a8eabca
--- /dev/null
+++ b/t/perf/p0090-cache-tree.sh
@@ -0,0 +1,36 @@
+#!/bin/sh
+
+test_description="Tests performance of cache tree update operations"
+
+. ./perf-lib.sh
+
+test_perf_large_repo
+test_checkout_worktree
+
+count=100
+
+test_expect_success 'setup cache tree' '
+ git write-tree
+'
+
+test_cache_tree () {
+ test_perf "$1, $3" "
+ for i in \$(test_seq $count)
+ do
+ test-tool cache-tree $4 $2
+ done
+ "
+}
+
+test_cache_tree_update_functions () {
+ test_cache_tree 'no-op' 'control' "$1" "$2"
+ test_cache_tree 'prime_cache_tree' 'prime' "$1" "$2"
+ test_cache_tree 'cache_tree_update' 'update' "$1" "$2"
+}
+
+test_cache_tree_update_functions "clean" ""
+test_cache_tree_update_functions "invalidate 2" "--invalidate 2"
+test_cache_tree_update_functions "invalidate 50" "--invalidate 50"
+test_cache_tree_update_functions "empty" "--empty"
+
+test_done
diff --git a/t/perf/p0100-globbing.sh b/t/perf/p0100-globbing.sh
new file mode 100755
index 0000000..439e9c8
--- /dev/null
+++ b/t/perf/p0100-globbing.sh
@@ -0,0 +1,43 @@
+#!/bin/sh
+
+test_description="Tests pathological globbing performance
+
+Shows how Git's globbing performance performs when given the sort of
+pathological patterns described in at https://research.swtch.com/glob
+"
+
+. ./perf-lib.sh
+
+test_globs_big='10 25 50 75 100'
+test_globs_small='1 2 3 4 5 6'
+
+test_perf_fresh_repo
+
+test_expect_success 'setup' '
+ for i in $(test_seq 1 100)
+ do
+ printf "a" >>refname &&
+ for j in $(test_seq 1 $i)
+ do
+ printf "a*" >>refglob.$i || return 1
+ done &&
+ echo b >>refglob.$i || return 1
+ done &&
+ test_commit test $(cat refname).t "" $(cat refname).t
+'
+
+for i in $test_globs_small
+do
+ test_perf "refglob((a*)^nb) against tag (a^100).t; n = $i" '
+ git for-each-ref "refs/tags/$(cat refglob.'$i')b"
+ '
+done
+
+for i in $test_globs_small
+do
+ test_perf "fileglob((a*)^nb) against file (a^100).t; n = $i" '
+ git ls-files "$(cat refglob.'$i')b"
+ '
+done
+
+test_done
diff --git a/t/perf/p1006-cat-file.sh b/t/perf/p1006-cat-file.sh
new file mode 100755
index 0000000..dcd8015
--- /dev/null
+++ b/t/perf/p1006-cat-file.sh
@@ -0,0 +1,12 @@
+#!/bin/sh
+
+test_description='Tests listing object info performance'
+. ./perf-lib.sh
+
+test_perf_large_repo
+
+test_perf 'cat-file --batch-check' '
+ git cat-file --batch-all-objects --batch-check
+'
+
+test_done
diff --git a/t/perf/p1400-update-ref.sh b/t/perf/p1400-update-ref.sh
new file mode 100755
index 0000000..a75969c
--- /dev/null
+++ b/t/perf/p1400-update-ref.sh
@@ -0,0 +1,33 @@
+#!/bin/sh
+
+test_description="Tests performance of update-ref"
+
+. ./perf-lib.sh
+
+test_perf_fresh_repo
+
+test_expect_success "setup" '
+ test_commit PRE &&
+ test_commit POST &&
+ for i in $(test_seq 5000)
+ do
+ printf "start\ncreate refs/heads/%d PRE\ncommit\n" $i &&
+ printf "start\nupdate refs/heads/%d POST PRE\ncommit\n" $i &&
+ printf "start\ndelete refs/heads/%d POST\ncommit\n" $i || return 1
+ done >instructions
+'
+
+test_perf "update-ref" '
+ for i in $(test_seq 1000)
+ do
+ git update-ref refs/heads/branch PRE &&
+ git update-ref refs/heads/branch POST PRE &&
+ git update-ref -d refs/heads/branch || return 1
+ done
+'
+
+test_perf "update-ref --stdin" '
+ git update-ref --stdin <instructions >/dev/null
+'
+
+test_done
diff --git a/t/perf/p1450-fsck.sh b/t/perf/p1450-fsck.sh
new file mode 100755
index 0000000..ae1b841
--- /dev/null
+++ b/t/perf/p1450-fsck.sh
@@ -0,0 +1,13 @@
+#!/bin/sh
+
+test_description='Test fsck performance'
+
+. ./perf-lib.sh
+
+test_perf_large_repo
+
+test_perf 'fsck' '
+ git fsck
+'
+
+test_done
diff --git a/t/perf/p1451-fsck-skip-list.sh b/t/perf/p1451-fsck-skip-list.sh
new file mode 100755
index 0000000..f767d83
--- /dev/null
+++ b/t/perf/p1451-fsck-skip-list.sh
@@ -0,0 +1,40 @@
+#!/bin/sh
+
+test_description='Test fsck skipList performance'
+
+. ./perf-lib.sh
+
+test_perf_fresh_repo
+
+n=1000000
+
+test_expect_success "setup $n bad commits" '
+ for i in $(test_seq 1 $n)
+ do
+ echo "commit refs/heads/master" &&
+ echo "committer C <c@example.com> 1234567890 +0000" &&
+ echo "data <<EOF" &&
+ echo "$i.Q." &&
+ echo "EOF" || return 1
+ done | q_to_nul | git fast-import
+'
+
+skip=0
+while test $skip -le $n
+do
+ test_expect_success "create skipList for $skip bad commits" '
+ git log --format=%H --max-count=$skip |
+ sort >skiplist
+ '
+
+ test_perf "fsck with $skip skipped bad commits" '
+ git -c fsck.skipList=skiplist fsck
+ '
+
+ case $skip in
+ 0) skip=1 ;;
+ *) skip=${skip}0 ;;
+ esac
+done
+
+test_done
diff --git a/t/perf/p1500-graph-walks.sh b/t/perf/p1500-graph-walks.sh
new file mode 100755
index 0000000..e14e762
--- /dev/null
+++ b/t/perf/p1500-graph-walks.sh
@@ -0,0 +1,50 @@
+#!/bin/sh
+
+test_description='Commit walk performance tests'
+. ./perf-lib.sh
+
+test_perf_large_repo
+
+test_expect_success 'setup' '
+ git for-each-ref --format="%(refname)" "refs/heads/*" "refs/tags/*" >allrefs &&
+ sort -r allrefs | head -n 50 >refs &&
+ for ref in $(cat refs)
+ do
+ git branch -f ref-$ref $ref &&
+ echo ref-$ref ||
+ return 1
+ done >branches &&
+ for ref in $(cat refs)
+ do
+ git tag -f tag-$ref $ref &&
+ echo tag-$ref ||
+ return 1
+ done >tags &&
+ git commit-graph write --reachable
+'
+
+test_perf 'ahead-behind counts: git for-each-ref' '
+ git for-each-ref --format="%(ahead-behind:HEAD)" --stdin <refs
+'
+
+test_perf 'ahead-behind counts: git branch' '
+ xargs git branch -l --format="%(ahead-behind:HEAD)" <branches
+'
+
+test_perf 'ahead-behind counts: git tag' '
+ xargs git tag -l --format="%(ahead-behind:HEAD)" <tags
+'
+
+test_perf 'contains: git for-each-ref --merged' '
+ git for-each-ref --merged=HEAD --stdin <refs
+'
+
+test_perf 'contains: git branch --merged' '
+ xargs git branch --merged=HEAD <branches
+'
+
+test_perf 'contains: git tag --merged' '
+ xargs git tag --merged=HEAD <tags
+'
+
+test_done
diff --git a/t/perf/p2000-sparse-operations.sh b/t/perf/p2000-sparse-operations.sh
new file mode 100755
index 0000000..39e92b0
--- /dev/null
+++ b/t/perf/p2000-sparse-operations.sh
@@ -0,0 +1,139 @@
+#!/bin/sh
+
+test_description="test performance of Git operations using the index"
+
+. ./perf-lib.sh
+
+test_perf_default_repo
+
+SPARSE_CONE=f2/f4
+
+test_expect_success 'setup repo and indexes' '
+ git reset --hard HEAD &&
+
+ # Remove submodules from the example repo, because our
+ # duplication of the entire repo creates an unlikely data shape.
+ if git config --file .gitmodules --get-regexp "submodule.*.path" >modules
+ then
+ git rm $(awk "{print \$2}" modules) &&
+ git commit -m "remove submodules" || return 1
+ fi &&
+
+ echo bogus >a &&
+ cp a b &&
+ git add a b &&
+ git commit -m "level 0" &&
+ BLOB=$(git rev-parse HEAD:a) &&
+ OLD_COMMIT=$(git rev-parse HEAD) &&
+ OLD_TREE=$(git rev-parse HEAD^{tree}) &&
+
+ for i in $(test_seq 1 3)
+ do
+ cat >in <<-EOF &&
+ 100755 blob $BLOB a
+ 040000 tree $OLD_TREE f1
+ 040000 tree $OLD_TREE f2
+ 040000 tree $OLD_TREE f3
+ 040000 tree $OLD_TREE f4
+ EOF
+ NEW_TREE=$(git mktree <in) &&
+ NEW_COMMIT=$(git commit-tree $NEW_TREE -p $OLD_COMMIT -m "level $i") &&
+ OLD_TREE=$NEW_TREE &&
+ OLD_COMMIT=$NEW_COMMIT || return 1
+ done &&
+
+ git sparse-checkout init --cone &&
+ git tag -a v1.0 -m "Final" &&
+ git sparse-checkout set $SPARSE_CONE &&
+ git checkout -b wide $OLD_COMMIT &&
+
+ for l2 in f1 f2 f3 f4
+ do
+ echo more bogus >>$SPARSE_CONE/$l2/a &&
+ git commit -a -m "edit $SPARSE_CONE/$l2/a" || return 1
+ done &&
+
+ git -c core.sparseCheckoutCone=true clone --branch=wide --sparse . full-v3 &&
+ (
+ cd full-v3 &&
+ git sparse-checkout init --cone &&
+ git sparse-checkout set $SPARSE_CONE &&
+ git config index.version 3 &&
+ git update-index --index-version=3 &&
+ git checkout HEAD~4
+ ) &&
+ git -c core.sparseCheckoutCone=true clone --branch=wide --sparse . full-v4 &&
+ (
+ cd full-v4 &&
+ git sparse-checkout init --cone &&
+ git sparse-checkout set $SPARSE_CONE &&
+ git config index.version 4 &&
+ git update-index --index-version=4 &&
+ git checkout HEAD~4
+ ) &&
+ git -c core.sparseCheckoutCone=true clone --branch=wide --sparse . sparse-v3 &&
+ (
+ cd sparse-v3 &&
+ git sparse-checkout init --cone --sparse-index &&
+ git sparse-checkout set $SPARSE_CONE &&
+ git config index.version 3 &&
+ git update-index --index-version=3 &&
+ git checkout HEAD~4
+ ) &&
+ git -c core.sparseCheckoutCone=true clone --branch=wide --sparse . sparse-v4 &&
+ (
+ cd sparse-v4 &&
+ git sparse-checkout init --cone --sparse-index &&
+ git sparse-checkout set $SPARSE_CONE &&
+ git config index.version 4 &&
+ git update-index --index-version=4 &&
+ git checkout HEAD~4
+ )
+'
+
+test_perf_on_all () {
+ command="$@"
+ for repo in full-v3 full-v4 \
+ sparse-v3 sparse-v4
+ do
+ test_perf "$command ($repo)" "
+ (
+ cd $repo &&
+ echo >>$SPARSE_CONE/a &&
+ $command
+ )
+ "
+ done
+}
+
+test_perf_on_all git status
+test_perf_on_all 'git stash && git stash pop'
+test_perf_on_all 'echo >>new && git stash -u && git stash pop'
+test_perf_on_all git add -A
+test_perf_on_all git add .
+test_perf_on_all git commit -a -m A
+test_perf_on_all git checkout -f -
+test_perf_on_all "git sparse-checkout add f2/f3/f1 && git sparse-checkout set $SPARSE_CONE"
+test_perf_on_all git reset
+test_perf_on_all git reset --hard
+test_perf_on_all git reset -- does-not-exist
+test_perf_on_all git diff
+test_perf_on_all git diff --cached
+test_perf_on_all git blame $SPARSE_CONE/a
+test_perf_on_all git blame $SPARSE_CONE/f3/a
+test_perf_on_all git read-tree -mu HEAD
+test_perf_on_all git checkout-index -f --all
+test_perf_on_all git update-index --add --remove $SPARSE_CONE/a
+test_perf_on_all "git rm -f $SPARSE_CONE/a && git checkout HEAD -- $SPARSE_CONE/a"
+test_perf_on_all git grep --cached bogus -- "f2/f1/f1/*"
+test_perf_on_all git write-tree
+test_perf_on_all git describe --dirty
+test_perf_on_all 'echo >>new && git describe --dirty'
+test_perf_on_all git diff-files
+test_perf_on_all git diff-files -- $SPARSE_CONE/a
+test_perf_on_all git diff-tree HEAD
+test_perf_on_all git diff-tree HEAD -- $SPARSE_CONE/a
+test_perf_on_all "git worktree add ../temp && git worktree remove ../temp"
+test_perf_on_all git check-attr -a -- $SPARSE_CONE/a
+
+test_done
diff --git a/t/perf/p3400-rebase.sh b/t/perf/p3400-rebase.sh
new file mode 100755
index 0000000..e6b0277
--- /dev/null
+++ b/t/perf/p3400-rebase.sh
@@ -0,0 +1,56 @@
+#!/bin/sh
+
+test_description='Tests rebase performance'
+. ./perf-lib.sh
+
+test_perf_default_repo
+
+test_expect_success 'setup rebasing on top of a lot of changes' '
+ git checkout -f -B base &&
+ git checkout -B to-rebase &&
+ git checkout -B upstream &&
+ for i in $(test_seq 100)
+ do
+ # simulate huge diffs
+ echo change$i >unrelated-file$i &&
+ test_seq 1000 >>unrelated-file$i &&
+ git add unrelated-file$i &&
+ test_tick &&
+ git commit -m commit$i unrelated-file$i &&
+ echo change$i >unrelated-file$i &&
+ test_seq 1000 | sort -nr >>unrelated-file$i &&
+ git add unrelated-file$i &&
+ test_tick &&
+ git commit -m commit$i-reverse unrelated-file$i ||
+ return 1
+ done &&
+ git checkout to-rebase &&
+ test_commit our-patch interesting-file
+'
+
+test_perf 'rebase on top of a lot of unrelated changes' '
+ git rebase --onto upstream HEAD^ &&
+ git rebase --onto base HEAD^
+'
+
+test_expect_success 'setup rebasing many changes without split-index' '
+ git config core.splitIndex false &&
+ git checkout -B upstream2 to-rebase &&
+ git checkout -B to-rebase2 upstream
+'
+
+test_perf 'rebase a lot of unrelated changes without split-index' '
+ git rebase --onto upstream2 base &&
+ git rebase --onto base upstream2
+'
+
+test_expect_success 'setup rebasing many changes with split-index' '
+ git config core.splitIndex true
+'
+
+test_perf 'rebase a lot of unrelated changes with split-index' '
+ git rebase --onto upstream2 base &&
+ git rebase --onto base upstream2
+'
+
+test_done
diff --git a/t/perf/p3404-rebase-interactive.sh b/t/perf/p3404-rebase-interactive.sh
new file mode 100755
index 0000000..88f47de
--- /dev/null
+++ b/t/perf/p3404-rebase-interactive.sh
@@ -0,0 +1,36 @@
+#!/bin/sh
+
+test_description='Tests rebase -i performance'
+. ./perf-lib.sh
+
+test_perf_default_repo
+
+# This commit merges a sufficiently long topic branch for reasonable
+# performance testing
+branch_merge=ba5312da19c6fdb6c6747d479f58932aae6e900c^{commit}
+export branch_merge
+
+git rev-parse --verify $branch_merge >/dev/null 2>&1 || {
+ skip_all='skipping because $branch_merge was not found'
+ test_done
+}
+
+write_script swap-first-two.sh <<\EOF
+case "$1" in
+*/COMMIT_EDITMSG)
+ mv "$1" "$1".bak &&
+ sed -e '1{h;d}' -e 2G <"$1".bak >"$1"
+ ;;
+esac
+EOF
+
+test_expect_success 'setup' '
+ git config core.editor "\"$PWD"/swap-first-two.sh\" &&
+ git checkout -f $branch_merge^2
+'
+
+test_perf 'rebase -i' '
+ git rebase -i $branch_merge^
+'
+
+test_done
diff --git a/t/perf/p4000-diff-algorithms.sh b/t/perf/p4000-diff-algorithms.sh
new file mode 100755
index 0000000..7e00c9d
--- /dev/null
+++ b/t/perf/p4000-diff-algorithms.sh
@@ -0,0 +1,29 @@
+#!/bin/sh
+
+test_description="Tests diff generation performance"
+
+. ./perf-lib.sh
+
+test_perf_default_repo
+
+test_perf 'log -3000 (baseline)' '
+ git log -3000 >/dev/null
+'
+
+test_perf 'log --raw -3000 (tree-only)' '
+ git log --raw -3000 >/dev/null
+'
+
+test_perf 'log -p -3000 (Myers)' '
+ git log -p -3000 >/dev/null
+'
+
+test_perf 'log -p -3000 --histogram' '
+ git log -p -3000 --histogram >/dev/null
+'
+
+test_perf 'log -p -3000 --patience' '
+ git log -p -3000 --patience >/dev/null
+'
+
+test_done
diff --git a/t/perf/p4001-diff-no-index.sh b/t/perf/p4001-diff-no-index.sh
new file mode 100755
index 0000000..683be69
--- /dev/null
+++ b/t/perf/p4001-diff-no-index.sh
@@ -0,0 +1,22 @@
+#!/bin/sh
+
+test_description="Test diff --no-index performance"
+
+. ./perf-lib.sh
+
+test_perf_large_repo
+test_checkout_worktree
+
+file1=$(git ls-files | tail -n 2 | head -1)
+file2=$(git ls-files | tail -n 1 | head -1)
+
+test_expect_success "empty files, so they take no time to diff" "
+ echo >$file1 &&
+ echo >$file2
+"
+
+test_perf "diff --no-index" "
+ git diff --no-index $file1 $file2 >/dev/null
+"
+
+test_done
diff --git a/t/perf/p4002-diff-color-moved.sh b/t/perf/p4002-diff-color-moved.sh
new file mode 100755
index 0000000..ab2af93
--- /dev/null
+++ b/t/perf/p4002-diff-color-moved.sh
@@ -0,0 +1,57 @@
+#!/bin/sh
+
+test_description='Tests diff --color-moved performance'
+. ./perf-lib.sh
+
+test_perf_default_repo
+
+# The endpoints of the diff can be customized by setting TEST_REV_A
+# and TEST_REV_B in the environment when running this test.
+
+rev="${TEST_REV_A:-v2.28.0}"
+if ! rev_a="$(git rev-parse --quiet --verify "$rev")"
+then
+ skip_all="skipping because '$rev' was not found. \
+ Use TEST_REV_A and TEST_REV_B to set the revs to use"
+ test_done
+fi
+rev="${TEST_REV_B:-v2.29.0}"
+if ! rev_b="$(git rev-parse --quiet --verify "$rev")"
+then
+ skip_all="skipping because '$rev' was not found. \
+ Use TEST_REV_A and TEST_REV_B to set the revs to use"
+ test_done
+fi
+
+GIT_PAGER_IN_USE=1
+test_export GIT_PAGER_IN_USE rev_a rev_b
+
+test_perf 'diff --no-color-moved --no-color-moved-ws large change' '
+ git diff --no-color-moved --no-color-moved-ws $rev_a $rev_b
+'
+
+test_perf 'diff --color-moved --no-color-moved-ws large change' '
+ git diff --color-moved=zebra --no-color-moved-ws $rev_a $rev_b
+'
+
+test_perf 'diff --color-moved-ws=allow-indentation-change large change' '
+ git diff --color-moved=zebra --color-moved-ws=allow-indentation-change \
+ $rev_a $rev_b
+'
+
+test_perf 'log --no-color-moved --no-color-moved-ws' '
+ git log --no-color-moved --no-color-moved-ws --no-merges --patch \
+ -n1000 $rev_b
+'
+
+test_perf 'log --color-moved --no-color-moved-ws' '
+ git log --color-moved=zebra --no-color-moved-ws --no-merges --patch \
+ -n1000 $rev_b
+'
+
+test_perf 'log --color-moved-ws=allow-indentation-change' '
+ git log --color-moved=zebra --color-moved-ws=allow-indentation-change \
+ --no-merges --patch -n1000 $rev_b
+'
+
+test_done
diff --git a/t/perf/p4205-log-pretty-formats.sh b/t/perf/p4205-log-pretty-formats.sh
new file mode 100755
index 0000000..609fecd
--- /dev/null
+++ b/t/perf/p4205-log-pretty-formats.sh
@@ -0,0 +1,16 @@
+#!/bin/sh
+
+test_description='Tests the performance of various pretty format placeholders'
+
+. ./perf-lib.sh
+
+test_perf_default_repo
+
+for format in %H %h %T %t %P %p %h-%h-%h %an-%ae-%s
+do
+ test_perf "log with $format" "
+ git log --format=\"$format\" >/dev/null
+ "
+done
+
+test_done
diff --git a/t/perf/p4209-pickaxe.sh b/t/perf/p4209-pickaxe.sh
new file mode 100755
index 0000000..f585a44
--- /dev/null
+++ b/t/perf/p4209-pickaxe.sh
@@ -0,0 +1,70 @@
+#!/bin/sh
+
+test_description="Test pickaxe performance"
+
+. ./perf-lib.sh
+
+test_perf_default_repo
+
+# Not --max-count, as that's the number of matching commit, so it's
+# unbounded. We want to limit our revision walk here.
+from_rev_desc=
+from_rev=
+max_count=1000
+if test_have_prereq EXPENSIVE
+then
+ max_count=10000
+fi
+from_rev=" $(git rev-list HEAD | head -n $max_count | tail -n 1).."
+from_rev_desc=" <limit-rev>.."
+
+for icase in \
+ '' \
+ '-i '
+do
+ # -S (no regex)
+ for pattern in \
+ 'int main' \
+ 'æ'
+ do
+ for opts in \
+ '-S'
+ do
+ test_perf "git log $icase$opts'$pattern'$from_rev_desc" "
+ git log --pretty=format:%H $icase$opts'$pattern'$from_rev
+ "
+ done
+ done
+
+ # -S (regex)
+ for pattern in \
+ '(int|void|null)' \
+ 'if *\([^ ]+ & ' \
+ '[àáâãäåæñøùúûüýþ]'
+ do
+ for opts in \
+ '--pickaxe-regex -S'
+ do
+ test_perf "git log $icase$opts'$pattern'$from_rev_desc" "
+ git log --pretty=format:%H $icase$opts'$pattern'$from_rev
+ "
+ done
+ done
+
+ # -G
+ for pattern in \
+ '(int|void|null)' \
+ 'if *\([^ ]+ & ' \
+ '[àáâãäåæñøùúûüýþ]'
+ do
+ for opts in \
+ '-G'
+ do
+ test_perf "git log $icase$opts'$pattern'$from_rev_desc" "
+ git log --pretty=format:%H $icase$opts'$pattern'$from_rev
+ "
+ done
+ done
+done
+
+test_done
diff --git a/t/perf/p4211-line-log.sh b/t/perf/p4211-line-log.sh
new file mode 100755
index 0000000..392bcc0
--- /dev/null
+++ b/t/perf/p4211-line-log.sh
@@ -0,0 +1,42 @@
+#!/bin/sh
+
+test_description='Tests log -L performance'
+. ./perf-lib.sh
+
+test_perf_default_repo
+
+# Pick a file to log pseudo-randomly. The sort key is the blob hash,
+# so it is stable.
+test_expect_success 'select a file' '
+ git ls-tree HEAD | grep ^100644 |
+ sort -k 3 | head -1 | cut -f 2 >filelist
+'
+
+file=$(cat filelist)
+export file
+
+test_perf 'git rev-list --topo-order (baseline)' '
+ git rev-list --topo-order HEAD >/dev/null
+'
+
+test_perf 'git log --follow (baseline for -M)' '
+ git log --oneline --follow -- "$file" >/dev/null
+'
+
+test_perf 'git log -L (renames off)' '
+ git log --no-renames -L 1:"$file" >/dev/null
+'
+
+test_perf 'git log -L (renames on)' '
+ git log -M -L 1:"$file" >/dev/null
+'
+
+test_perf 'git log --oneline --raw --parents' '
+ git log --oneline --raw --parents >/dev/null
+'
+
+test_perf 'git log --oneline --raw --parents -1000' '
+ git log --oneline --raw --parents -1000 >/dev/null
+'
+
+test_done
diff --git a/t/perf/p4220-log-grep-engines.sh b/t/perf/p4220-log-grep-engines.sh
new file mode 100755
index 0000000..03fbfbb
--- /dev/null
+++ b/t/perf/p4220-log-grep-engines.sh
@@ -0,0 +1,54 @@
+#!/bin/sh
+
+test_description="Comparison of git-log's --grep regex engines
+
+Set GIT_PERF_4220_LOG_OPTS in the environment to pass options to
+git-grep. Make sure to include a leading space,
+e.g. GIT_PERF_4220_LOG_OPTS=' -i'. Some options to try:
+
+ -i
+ --invert-grep
+ -i --invert-grep
+"
+
+. ./perf-lib.sh
+
+test_perf_large_repo
+test_checkout_worktree
+
+for pattern in \
+ 'how.to' \
+ '^how to' \
+ '[how] to' \
+ '\(e.t[^ ]*\|v.ry\) rare' \
+ 'm\(ú\|u\)lt.b\(æ\|y\)te'
+do
+ for engine in basic extended perl
+ do
+ if test $engine != "basic"
+ then
+ # Poor man's basic -> extended converter.
+ pattern=$(echo $pattern | sed 's/\\//g')
+ fi
+ if test $engine = "perl" && ! test_have_prereq PCRE
+ then
+ prereq="PCRE"
+ else
+ prereq=""
+ fi
+ test_perf "$engine log$GIT_PERF_4220_LOG_OPTS --grep='$pattern'" \
+ --prereq "$prereq" "
+ git -c grep.patternType=$engine log --pretty=format:%h$GIT_PERF_4220_LOG_OPTS --grep='$pattern' >'out.$engine' || :
+ "
+ done
+
+ test_expect_success "assert that all engines found the same for$GIT_PERF_4220_LOG_OPTS '$pattern'" '
+ test_cmp out.basic out.extended &&
+ if test_have_prereq PCRE
+ then
+ test_cmp out.basic out.perl
+ fi
+ '
+done
+
+test_done
diff --git a/t/perf/p4221-log-grep-engines-fixed.sh b/t/perf/p4221-log-grep-engines-fixed.sh
new file mode 100755
index 0000000..0a6d6df
--- /dev/null
+++ b/t/perf/p4221-log-grep-engines-fixed.sh
@@ -0,0 +1,45 @@
+#!/bin/sh
+
+test_description="Comparison of git-log's --grep regex engines with -F
+
+Set GIT_PERF_4221_LOG_OPTS in the environment to pass options to
+git-grep. Make sure to include a leading space,
+e.g. GIT_PERF_4221_LOG_OPTS=' -i'. Some options to try:
+
+ -i
+ --invert-grep
+ -i --invert-grep
+"
+
+. ./perf-lib.sh
+
+test_perf_large_repo
+test_checkout_worktree
+
+for pattern in 'int' 'uncommon' 'æ'
+do
+ for engine in fixed basic extended perl
+ do
+ if test $engine = "perl" && ! test_have_prereq PCRE
+ then
+ prereq="PCRE"
+ else
+ prereq=""
+ fi
+ test_perf "$engine log$GIT_PERF_4221_LOG_OPTS --grep='$pattern'" \
+ --prereq "$prereq" "
+ git -c grep.patternType=$engine log --pretty=format:%h$GIT_PERF_4221_LOG_OPTS --grep='$pattern' >'out.$engine' || :
+ "
+ done
+
+ test_expect_success "assert that all engines found the same for$GIT_PERF_4221_LOG_OPTS '$pattern'" '
+ test_cmp out.fixed out.basic &&
+ test_cmp out.fixed out.extended &&
+ if test_have_prereq PCRE
+ then
+ test_cmp out.fixed out.perl
+ fi
+ '
+done
+
+test_done
diff --git a/t/perf/p5302-pack-index.sh b/t/perf/p5302-pack-index.sh
new file mode 100755
index 0000000..14c601b
--- /dev/null
+++ b/t/perf/p5302-pack-index.sh
@@ -0,0 +1,50 @@
+#!/bin/sh
+
+test_description="Tests index-pack performance"
+
+. ./perf-lib.sh
+
+test_perf_large_repo
+
+test_expect_success 'repack' '
+ git repack -ad &&
+ PACK=$(ls .git/objects/pack/*.pack | head -n1) &&
+ test -f "$PACK" &&
+ export PACK
+'
+
+# Rather than counting up and doubling each time, count down from the endpoint,
+# halving each time. That ensures that our final test uses as many threads as
+# CPUs, even if it isn't a power of 2.
+test_expect_success 'set up thread-counting tests' '
+ t=$(test-tool online-cpus) &&
+ threads= &&
+ while test $t -gt 0
+ do
+ threads="$t $threads" &&
+ t=$((t / 2)) || return 1
+ done
+'
+
+test_perf 'index-pack 0 threads' --prereq PERF_EXTRA \
+ --setup 'rm -rf repo.git && git init --bare repo.git' '
+ GIT_DIR=repo.git git index-pack --threads=1 --stdin < $PACK
+'
+
+for t in $threads
+do
+ THREADS=$t
+ export THREADS
+ test_perf "index-pack $t threads" --prereq PERF_EXTRA \
+ --setup 'rm -rf repo.git && git init --bare repo.git' '
+ GIT_DIR=repo.git GIT_FORCE_THREADS=1 \
+ git index-pack --threads=$THREADS --stdin <$PACK
+ '
+done
+
+test_perf 'index-pack default number of threads' \
+ --setup 'rm -rf repo.git && git init --bare repo.git' '
+ GIT_DIR=repo.git git index-pack --stdin < $PACK
+'
+
+test_done
diff --git a/t/perf/p5303-many-packs.sh b/t/perf/p5303-many-packs.sh
new file mode 100755
index 0000000..af173a7
--- /dev/null
+++ b/t/perf/p5303-many-packs.sh
@@ -0,0 +1,144 @@
+#!/bin/sh
+
+test_description='performance with large numbers of packs'
+. ./perf-lib.sh
+
+test_perf_large_repo
+
+# A real many-pack situation would probably come from having a lot of pushes
+# over time. We don't know how big each push would be, but we can fake it by
+# just walking the first-parent chain and having every 5 commits be their own
+# "push". This isn't _entirely_ accurate, as real pushes would have some
+# duplicate objects due to thin-pack fixing, but it's a reasonable
+# approximation.
+#
+# And then all of the rest of the objects can go in a single packfile that
+# represents the state before any of those pushes (actually, we'll generate
+# that first because in such a setup it would be the oldest pack, and we sort
+# the packs by reverse mtime inside git).
+repack_into_n () {
+ rm -rf staging &&
+ mkdir staging &&
+
+ git rev-list --first-parent HEAD |
+ perl -e '
+ my $n = shift;
+ while (<>) {
+ last unless @commits < $n;
+ push @commits, $_ if $. % 5 == 1;
+ }
+ print reverse @commits;
+ ' "$1" >pushes &&
+
+ # create base packfile
+ base_pack=$(
+ head -n 1 pushes |
+ git pack-objects --delta-base-offset --revs staging/pack
+ ) &&
+ test_export base_pack &&
+
+ # create an empty packfile
+ empty_pack=$(git pack-objects staging/pack </dev/null) &&
+ test_export empty_pack &&
+
+ # and then incrementals between each pair of commits
+ last= &&
+ while read rev
+ do
+ if test -n "$last"; then
+ {
+ echo "$rev" &&
+ echo "^$last"
+ } |
+ git pack-objects --delta-base-offset --revs \
+ staging/pack || return 1
+ fi
+ last=$rev
+ done <pushes &&
+
+ (
+ find staging -type f -name 'pack-*.pack' |
+ xargs -n 1 basename | grep -v "$base_pack" &&
+ printf "^pack-%s.pack\n" $base_pack
+ ) >stdin.packs
+
+ # and install the whole thing
+ rm -f .git/objects/pack/* &&
+ mv staging/* .git/objects/pack/
+}
+
+# Pretend we just have a single branch and no reflogs, and that everything is
+# in objects/pack; that makes our fake pack-building via repack_into_n()
+# much simpler.
+test_expect_success 'simplify reachability' '
+ tip=$(git rev-parse --verify HEAD) &&
+ git for-each-ref --format="option no-deref%0adelete %(refname)" |
+ git update-ref --stdin &&
+ rm -rf .git/logs &&
+ git update-ref refs/heads/master $tip &&
+ git symbolic-ref HEAD refs/heads/master &&
+ git repack -ad
+'
+
+for nr_packs in 1 50 1000
+do
+ test_expect_success "create $nr_packs-pack scenario" '
+ repack_into_n $nr_packs
+ '
+
+ test_perf "rev-list ($nr_packs)" '
+ git rev-list --objects --all >/dev/null
+ '
+
+ test_perf "abbrev-commit ($nr_packs)" '
+ git rev-list --abbrev-commit HEAD >/dev/null
+ '
+
+ # This simulates the interesting part of the repack, which is the
+ # actual pack generation, without smudging the on-disk setup
+ # between trials.
+ test_perf "repack ($nr_packs)" '
+ GIT_TEST_FULL_IN_PACK_ARRAY=1 \
+ git pack-objects --keep-true-parents \
+ --honor-pack-keep --non-empty --all \
+ --reflog --indexed-objects --delta-base-offset \
+ --stdout </dev/null >/dev/null
+ '
+
+ test_perf "repack with kept ($nr_packs)" '
+ git pack-objects --keep-true-parents \
+ --keep-pack=pack-$empty_pack.pack \
+ --honor-pack-keep --non-empty --all \
+ --reflog --indexed-objects --delta-base-offset \
+ --stdout </dev/null >/dev/null
+ '
+
+ test_perf "repack with --stdin-packs ($nr_packs)" '
+ git pack-objects \
+ --keep-true-parents \
+ --stdin-packs \
+ --non-empty \
+ --delta-base-offset \
+ --stdout <stdin.packs >/dev/null
+ '
+done
+
+# Measure pack loading with 10,000 packs.
+test_expect_success 'generate lots of packs' '
+ for i in $(test_seq 10000); do
+ echo "blob" &&
+ echo "data <<EOF" &&
+ echo "blob $i" &&
+ echo "EOF" &&
+ echo "checkpoint" || return 1
+ done |
+ git -c fastimport.unpackLimit=0 fast-import
+'
+
+# The purpose of this test is to evaluate load time for a large number
+# of packs while doing as little other work as possible.
+test_perf "load 10,000 packs" '
+ git rev-parse --verify "HEAD^{commit}"
+'
+
+test_done
diff --git a/t/perf/p5304-prune.sh b/t/perf/p5304-prune.sh
new file mode 100755
index 0000000..83baedb
--- /dev/null
+++ b/t/perf/p5304-prune.sh
@@ -0,0 +1,35 @@
+#!/bin/sh
+
+test_description='performance tests of prune'
+. ./perf-lib.sh
+
+test_perf_default_repo
+
+test_expect_success 'remove reachable loose objects' '
+ git repack -ad
+'
+
+test_expect_success 'remove unreachable loose objects' '
+ git prune
+'
+
+test_expect_success 'confirm there are no loose objects' '
+ git count-objects | grep ^0
+'
+
+test_perf 'prune with no objects' '
+ git prune
+'
+
+test_expect_success 'repack with bitmaps' '
+ git repack -adb
+'
+
+# We have to create the object in each trial run, since otherwise
+# runs after the first see no object and just skip the traversal entirely!
+test_perf 'prune with bitmaps' '
+ echo "probably not present in repo" | git hash-object -w --stdin &&
+ git prune
+'
+
+test_done
diff --git a/t/perf/p5310-pack-bitmaps.sh b/t/perf/p5310-pack-bitmaps.sh
new file mode 100755
index 0000000..b1399f1
--- /dev/null
+++ b/t/perf/p5310-pack-bitmaps.sh
@@ -0,0 +1,40 @@
+#!/bin/sh
+
+test_description='Tests pack performance using bitmaps'
+. ./perf-lib.sh
+. "${TEST_DIRECTORY}/perf/lib-bitmap.sh"
+
+test_lookup_pack_bitmap () {
+ test_expect_success 'start the test from scratch' '
+ rm -rf * .git
+ '
+
+ test_perf_large_repo
+
+ # note that we do everything through config,
+ # since we want to be able to compare bitmap-aware
+ # git versus non-bitmap git
+ #
+ # We intentionally use the deprecated pack.writebitmaps
+ # config so that we can test against older versions of git.
+ test_expect_success 'setup bitmap config' '
+ git config pack.writebitmaps true
+ '
+
+ # we need to create the tag up front such that it is covered by the repack and
+ # thus by generated bitmaps.
+ test_expect_success 'create tags' '
+ git tag --message="tag pointing to HEAD" perf-tag HEAD
+ '
+
+ test_perf "enable lookup table: $1" '
+ git config pack.writeBitmapLookupTable '"$1"'
+ '
+
+ test_pack_bitmap
+}
+
+test_lookup_pack_bitmap false
+test_lookup_pack_bitmap true
+
+test_done
diff --git a/t/perf/p5311-pack-bitmaps-fetch.sh b/t/perf/p5311-pack-bitmaps-fetch.sh
new file mode 100755
index 0000000..426fab8
--- /dev/null
+++ b/t/perf/p5311-pack-bitmaps-fetch.sh
@@ -0,0 +1,54 @@
+#!/bin/sh
+
+test_description='performance of fetches from bitmapped packs'
+. ./perf-lib.sh
+
+test_fetch_bitmaps () {
+ test_expect_success 'setup test directory' '
+ rm -fr * .git
+ '
+
+ test_perf_default_repo
+
+ test_expect_success 'create bitmapped server repo' '
+ git config pack.writebitmaps true &&
+ git config pack.writeBitmapLookupTable '"$1"' &&
+ git repack -ad
+ '
+
+ # simulate a fetch from a repository that last fetched N days ago, for
+ # various values of N. We do so by following the first-parent chain,
+ # and assume the first entry in the chain that is N days older than the current
+ # HEAD is where the HEAD would have been then.
+ for days in 1 2 4 8 16 32 64 128; do
+ title=$(printf '%10s' "($days days)")
+ test_expect_success "setup revs from $days days ago" '
+ now=$(git log -1 --format=%ct HEAD) &&
+ then=$(($now - ($days * 86400))) &&
+ tip=$(git rev-list -1 --first-parent --until=$then HEAD) &&
+ {
+ echo HEAD &&
+ echo ^$tip
+ } >revs
+ '
+
+ test_perf "server $title (lookup=$1)" '
+ git pack-objects --stdout --revs \
+ --thin --delta-base-offset \
+ <revs >tmp.pack
+ '
+
+ test_size "size $title" '
+ wc -c <tmp.pack
+ '
+
+ test_perf "client $title (lookup=$1)" '
+ git index-pack --stdin --fix-thin <tmp.pack
+ '
+ done
+}
+
+test_fetch_bitmaps true
+test_fetch_bitmaps false
+
+test_done
diff --git a/t/perf/p5312-pack-bitmaps-revs.sh b/t/perf/p5312-pack-bitmaps-revs.sh
new file mode 100755
index 0000000..ceec606
--- /dev/null
+++ b/t/perf/p5312-pack-bitmaps-revs.sh
@@ -0,0 +1,34 @@
+#!/bin/sh
+
+test_description='Tests pack performance using bitmaps (rev index enabled)'
+. ./perf-lib.sh
+. "${TEST_DIRECTORY}/perf/lib-bitmap.sh"
+
+test_lookup_pack_bitmap () {
+ test_expect_success 'start the test from scratch' '
+ rm -rf * .git
+ '
+
+ test_perf_large_repo
+
+ test_expect_success 'setup bitmap config' '
+ git config pack.writebitmaps true
+ '
+
+ # we need to create the tag up front such that it is covered by the repack and
+ # thus by generated bitmaps.
+ test_expect_success 'create tags' '
+ git tag --message="tag pointing to HEAD" perf-tag HEAD
+ '
+
+ test_perf "enable lookup table: $1" '
+ git config pack.writeBitmapLookupTable '"$1"'
+ '
+
+ test_pack_bitmap
+}
+
+test_lookup_pack_bitmap false
+test_lookup_pack_bitmap true
+
+test_done
diff --git a/t/perf/p5326-multi-pack-bitmaps.sh b/t/perf/p5326-multi-pack-bitmaps.sh
new file mode 100755
index 0000000..d082e6c
--- /dev/null
+++ b/t/perf/p5326-multi-pack-bitmaps.sh
@@ -0,0 +1,67 @@
+#!/bin/sh
+
+test_description='Tests performance using midx bitmaps'
+. ./perf-lib.sh
+. "${TEST_DIRECTORY}/perf/lib-bitmap.sh"
+
+test_bitmap () {
+ local enabled="$1"
+
+ test_expect_success "remove existing repo (lookup=$enabled)" '
+ rm -fr * .git
+ '
+
+ test_perf_large_repo
+
+ # we need to create the tag up front such that it is covered by the repack and
+ # thus by generated bitmaps.
+ test_expect_success 'create tags' '
+ git tag --message="tag pointing to HEAD" perf-tag HEAD
+ '
+
+ test_expect_success "use lookup table: $enabled" '
+ git config pack.writeBitmapLookupTable '"$enabled"'
+ '
+
+ test_expect_success "start with bitmapped pack (lookup=$enabled)" '
+ git repack -adb
+ '
+
+ test_perf "setup multi-pack index (lookup=$enabled)" '
+ git multi-pack-index write --bitmap
+ '
+
+ test_expect_success "drop pack bitmap (lookup=$enabled)" '
+ rm -f .git/objects/pack/pack-*.bitmap
+ '
+
+ test_full_bitmap
+
+ test_expect_success "create partial bitmap state (lookup=$enabled)" '
+ # pick a commit to represent the repo tip in the past
+ cutoff=$(git rev-list HEAD~100 -1) &&
+ orig_tip=$(git rev-parse HEAD) &&
+
+ # now pretend we have just one tip
+ rm -rf .git/logs .git/refs/* .git/packed-refs &&
+ git update-ref HEAD $cutoff &&
+
+ # and then repack, which will leave us with a nice
+ # big bitmap pack of the "old" history, and all of
+ # the new history will be loose, as if it had been pushed
+ # up incrementally and exploded via unpack-objects
+ git repack -Ad &&
+ git multi-pack-index write --bitmap &&
+
+ # and now restore our original tip, as if the pushes
+ # had happened
+ git update-ref HEAD $orig_tip
+ '
+
+ test_partial_bitmap
+}
+
+test_bitmap false
+test_bitmap true
+
+test_done
diff --git a/t/perf/p5550-fetch-tags.sh b/t/perf/p5550-fetch-tags.sh
new file mode 100755
index 0000000..d0e0e01
--- /dev/null
+++ b/t/perf/p5550-fetch-tags.sh
@@ -0,0 +1,78 @@
+#!/bin/sh
+
+test_description='performance of tag-following with many tags
+
+This tests a fairly pathological case, so rather than rely on a real-world
+case, we will construct our own repository. The situation is roughly as
+follows.
+
+The parent repository has a large number of tags which are disconnected from
+the rest of history. That makes them candidates for tag-following, but we never
+actually grab them (and thus they will impact each subsequent fetch).
+
+The child repository is a clone of parent, without the tags, and is at least
+one commit behind the parent (meaning that we will fetch one object and then
+examine the tags to see if they need followed). Furthermore, it has a large
+number of packs.
+
+The exact values of "large" here are somewhat arbitrary; I picked values that
+start to show a noticeable performance problem on my machine, but without
+taking too long to set up and run the tests.
+'
+. ./perf-lib.sh
+. "$TEST_DIRECTORY/perf/lib-pack.sh"
+
+# make a long nonsense history on branch $1, consisting of $2 commits, each
+# with a unique file pointing to the blob at $2.
+create_history () {
+ perl -le '
+ my ($branch, $n, $blob) = @ARGV;
+ for (1..$n) {
+ print "commit refs/heads/$branch";
+ print "committer nobody <nobody@example.com> now";
+ print "data 4";
+ print "foo";
+ print "M 100644 $blob $_";
+ }
+ ' "$@" |
+ git fast-import --date-format=now
+}
+
+# make a series of tags, one per commit in the revision range given by $@
+create_tags () {
+ git rev-list "$@" |
+ perl -lne 'print "create refs/tags/$. $_"' |
+ git update-ref --stdin
+}
+
+test_expect_success 'create parent and child' '
+ git init parent &&
+ git -C parent commit --allow-empty -m base &&
+ git clone parent child &&
+ git -C parent commit --allow-empty -m trigger-fetch
+'
+
+test_expect_success 'populate parent tags' '
+ (
+ cd parent &&
+ blob=$(echo content | git hash-object -w --stdin) &&
+ create_history cruft 3000 $blob &&
+ create_tags cruft &&
+ git branch -D cruft
+ )
+'
+
+test_expect_success 'create child packs' '
+ (
+ cd child &&
+ setup_many_packs
+ )
+'
+
+test_perf 'fetch' '
+ # make sure there is something to fetch on each iteration
+ git -C child update-ref -d refs/remotes/origin/master &&
+ git -C child fetch
+'
+
+test_done
diff --git a/t/perf/p5551-fetch-rescan.sh b/t/perf/p5551-fetch-rescan.sh
new file mode 100755
index 0000000..b99dc23
--- /dev/null
+++ b/t/perf/p5551-fetch-rescan.sh
@@ -0,0 +1,55 @@
+#!/bin/sh
+
+test_description='fetch performance with many packs
+
+It is common for fetch to consider objects that we might not have, and it is an
+easy mistake for the code to use a function like `parse_object` that might
+give the correct _answer_ on such an object, but do so slowly (due to
+re-scanning the pack directory for lookup failures).
+
+The resulting performance drop can be hard to notice in a real repository, but
+becomes quite large in a repository with a large number of packs. So this
+test creates a more pathological case, since any mistakes would produce a more
+noticeable slowdown.
+'
+. ./perf-lib.sh
+. "$TEST_DIRECTORY"/perf/lib-pack.sh
+
+test_expect_success 'create parent and child' '
+ git init parent &&
+ git clone parent child
+'
+
+
+test_expect_success 'create refs in the parent' '
+ (
+ cd parent &&
+ git commit --allow-empty -m foo &&
+ head=$(git rev-parse HEAD) &&
+ test_seq 1000 |
+ sed "s,.*,update refs/heads/& $head," |
+ $MODERN_GIT update-ref --stdin
+ )
+'
+
+test_expect_success 'create many packs in the child' '
+ (
+ cd child &&
+ setup_many_packs
+ )
+'
+
+test_perf 'fetch' '
+ # start at the same state for each iteration
+ obj=$($MODERN_GIT -C parent rev-parse HEAD) &&
+ (
+ cd child &&
+ $MODERN_GIT for-each-ref --format="delete %(refname)" refs/remotes |
+ $MODERN_GIT update-ref --stdin &&
+ rm -vf .git/objects/$(echo $obj | sed "s|^..|&/|") &&
+
+ git fetch
+ )
+'
+
+test_done
diff --git a/t/perf/p5600-partial-clone.sh b/t/perf/p5600-partial-clone.sh
new file mode 100755
index 0000000..a965f2c
--- /dev/null
+++ b/t/perf/p5600-partial-clone.sh
@@ -0,0 +1,42 @@
+#!/bin/sh
+
+test_description='performance of partial clones'
+. ./perf-lib.sh
+
+test_perf_default_repo
+
+test_expect_success 'enable server-side config' '
+ git config uploadpack.allowFilter true &&
+ git config uploadpack.allowAnySHA1InWant true
+'
+
+test_perf 'clone without blobs' '
+ rm -rf bare.git &&
+ git clone --no-local --bare --filter=blob:none . bare.git
+'
+
+test_perf 'checkout of result' '
+ rm -rf worktree &&
+ mkdir -p worktree/.git &&
+ tar -C bare.git -cf - . | tar -C worktree/.git -xf - &&
+ git -C worktree config core.bare false &&
+ git -C worktree checkout -f
+'
+
+test_perf 'fsck' '
+ git -C bare.git fsck
+'
+
+test_perf 'count commits' '
+ git -C bare.git rev-list --all --count
+'
+
+test_perf 'count non-promisor commits' '
+ git -C bare.git rev-list --all --count --exclude-promisor-objects
+'
+
+test_perf 'gc' '
+ git -C bare.git gc
+'
+
+test_done
diff --git a/t/perf/p5601-clone-reference.sh b/t/perf/p5601-clone-reference.sh
new file mode 100755
index 0000000..68fed66
--- /dev/null
+++ b/t/perf/p5601-clone-reference.sh
@@ -0,0 +1,27 @@
+#!/bin/sh
+
+test_description='speed of clone --reference'
+. ./perf-lib.sh
+
+test_perf_default_repo
+
+test_expect_success 'create shareable repository' '
+ git clone --bare . shared.git
+'
+
+test_expect_success 'advance base repository' '
+ # Do not use test_commit here; its test_tick will
+ # use some ancient hard-coded date. The resulting clock
+ # skew will cause pack-objects to traverse in a very
+ # sub-optimal order, skewing the results.
+ echo content >new-file-that-does-not-exist &&
+ git add new-file-that-does-not-exist &&
+ git commit -m "new commit"
+'
+
+test_perf 'clone --reference' '
+ rm -rf dst.git &&
+ git clone --no-local --bare --reference shared.git . dst.git
+'
+
+test_done
diff --git a/t/perf/p7000-filter-branch.sh b/t/perf/p7000-filter-branch.sh
new file mode 100755
index 0000000..b029586
--- /dev/null
+++ b/t/perf/p7000-filter-branch.sh
@@ -0,0 +1,24 @@
+#!/bin/sh
+
+test_description='performance of filter-branch'
+. ./perf-lib.sh
+
+test_perf_default_repo
+test_checkout_worktree
+
+test_expect_success 'mark bases for tests' '
+ git tag -f tip &&
+ git tag -f base HEAD~100
+'
+
+test_perf 'noop filter' '
+ git checkout --detach tip &&
+ git filter-branch -f base..HEAD
+'
+
+test_perf 'noop prune-empty' '
+ git checkout --detach tip &&
+ git filter-branch -f --prune-empty base..HEAD
+'
+
+test_done
diff --git a/t/perf/p7102-reset.sh b/t/perf/p7102-reset.sh
new file mode 100755
index 0000000..9b039e8
--- /dev/null
+++ b/t/perf/p7102-reset.sh
@@ -0,0 +1,21 @@
+#!/bin/sh
+
+test_description='performance of reset'
+. ./perf-lib.sh
+
+test_perf_default_repo
+test_checkout_worktree
+
+test_perf 'reset --hard with change in tree' '
+ base=$(git rev-parse HEAD) &&
+ test_commit --no-tag A &&
+ new=$(git rev-parse HEAD) &&
+
+ for i in $(test_seq 10)
+ do
+ git reset --hard $new &&
+ git reset --hard $base || return $?
+ done
+'
+
+test_done
diff --git a/t/perf/p7300-clean.sh b/t/perf/p7300-clean.sh
new file mode 100755
index 0000000..7c1888a
--- /dev/null
+++ b/t/perf/p7300-clean.sh
@@ -0,0 +1,35 @@
+#!/bin/sh
+
+test_description="Test git-clean performance"
+
+. ./perf-lib.sh
+
+test_perf_default_repo
+test_checkout_worktree
+
+test_expect_success 'setup untracked directory with many sub dirs' '
+ rm -rf 500_sub_dirs 100000_sub_dirs clean_test_dir &&
+ mkdir 500_sub_dirs 100000_sub_dirs clean_test_dir &&
+ for i in $(test_seq 1 500)
+ do
+ mkdir 500_sub_dirs/dir$i || return $?
+ done &&
+ for i in $(test_seq 1 200)
+ do
+ cp -r 500_sub_dirs 100000_sub_dirs/dir$i || return $?
+ done
+'
+
+test_perf 'clean many untracked sub dirs, check for nested git' '
+ git clean -n -q -f -d 100000_sub_dirs/
+'
+
+test_perf 'clean many untracked sub dirs, ignore nested git' '
+ git clean -n -q -f -f -d 100000_sub_dirs/
+'
+
+test_perf 'ls-files -o' '
+ git ls-files -o
+'
+
+test_done
diff --git a/t/perf/p7519-fsmonitor.sh b/t/perf/p7519-fsmonitor.sh
new file mode 100755
index 0000000..b1cb238
--- /dev/null
+++ b/t/perf/p7519-fsmonitor.sh
@@ -0,0 +1,314 @@
+#!/bin/sh
+
+test_description="Test core.fsmonitor"
+
+. ./perf-lib.sh
+
+#
+# Performance test for the fsmonitor feature which enables git to talk to a
+# file system change monitor and avoid having to scan the working directory
+# for new or modified files.
+#
+# By default, the performance test will utilize the Watchman file system
+# monitor if it is installed. If Watchman is not installed, it will use a
+# dummy integration script that does not report any new or modified files.
+# The dummy script has very little overhead which provides optimistic results.
+#
+# The performance test will also use the untracked cache feature if it is
+# available as fsmonitor uses it to speed up scanning for untracked files.
+#
+# There are 3 environment variables that can be used to alter the default
+# behavior of the performance test:
+#
+# GIT_PERF_7519_UNTRACKED_CACHE: used to configure core.untrackedCache
+# GIT_PERF_7519_SPLIT_INDEX: used to configure core.splitIndex
+# GIT_PERF_7519_FSMONITOR: used to configure core.fsMonitor. May be an
+# absolute path to an integration. May be a space delimited list of
+# absolute paths to integrations.
+#
+# The big win for using fsmonitor is the elimination of the need to scan the
+# working directory looking for changed and untracked files. If the file
+# information is all cached in RAM, the benefits are reduced.
+#
+# GIT_PERF_7519_DROP_CACHE: if set, the OS caches are dropped between tests
+#
+# GIT_PERF_7519_TRACE: if set, enable trace logging during the test.
+# Trace logs will be grouped by fsmonitor provider.
+
+test_perf_large_repo
+test_checkout_worktree
+
+test_lazy_prereq UNTRACKED_CACHE '
+ { git update-index --test-untracked-cache; ret=$?; } &&
+ test $ret -ne 1
+'
+
+test_lazy_prereq WATCHMAN '
+ command -v watchman
+'
+
+if test_have_prereq WATCHMAN
+then
+ # Convert unix style paths to escaped Windows style paths for Watchman
+ case "$(uname -s)" in
+ MSYS_NT*)
+ GIT_WORK_TREE="$(cygpath -aw "$PWD" | sed 's,\\,/,g')"
+ ;;
+ *)
+ GIT_WORK_TREE="$PWD"
+ ;;
+ esac
+fi
+
+trace_start () {
+ if test -n "$GIT_PERF_7519_TRACE"
+ then
+ name="$1"
+ TEST_TRACE_DIR="$TEST_OUTPUT_DIRECTORY/test-trace/p7519/"
+ echo "Writing trace logging to $TEST_TRACE_DIR"
+
+ mkdir -p "$TEST_TRACE_DIR"
+
+ # Start Trace2 logging and any other GIT_TRACE_* logs that you
+ # want for this named test case.
+
+ GIT_TRACE2_PERF="$TEST_TRACE_DIR/$name.trace2perf"
+ export GIT_TRACE2_PERF
+
+ >"$GIT_TRACE2_PERF"
+ fi
+}
+
+trace_stop () {
+ if test -n "$GIT_PERF_7519_TRACE"
+ then
+ unset GIT_TRACE2_PERF
+ fi
+}
+
+touch_files () {
+ n=$1 &&
+ d="$n"_files &&
+
+ (cd $d && test_seq 1 $n | xargs touch )
+}
+
+test_expect_success "one time repo setup" '
+ # set untrackedCache depending on the environment
+ if test -n "$GIT_PERF_7519_UNTRACKED_CACHE"
+ then
+ git config core.untrackedCache "$GIT_PERF_7519_UNTRACKED_CACHE"
+ else
+ if test_have_prereq UNTRACKED_CACHE
+ then
+ git config core.untrackedCache true
+ else
+ git config core.untrackedCache false
+ fi
+ fi &&
+
+ # set core.splitindex depending on the environment
+ if test -n "$GIT_PERF_7519_SPLIT_INDEX"
+ then
+ git config core.splitIndex "$GIT_PERF_7519_SPLIT_INDEX"
+ fi &&
+
+ mkdir 1_file 10_files 100_files 1000_files 10000_files &&
+ : 1_file directory should be left empty &&
+ touch_files 10 &&
+ touch_files 100 &&
+ touch_files 1000 &&
+ touch_files 10000 &&
+ git add 1_file 10_files 100_files 1000_files 10000_files &&
+ git commit -qm "Add files" &&
+
+ # If Watchman exists, watch the work tree and attempt a query.
+ if test_have_prereq WATCHMAN; then
+ watchman watch "$GIT_WORK_TREE" &&
+ watchman watch-list | grep -q -F "p7519-fsmonitor"
+ fi
+'
+
+setup_for_fsmonitor_hook () {
+ # set INTEGRATION_SCRIPT depending on the environment
+ if test -n "$INTEGRATION_PATH"
+ then
+ INTEGRATION_SCRIPT="$INTEGRATION_PATH"
+ else
+ #
+ # Choose integration script based on existence of Watchman.
+ # Fall back to an empty integration script.
+ #
+ mkdir .git/hooks &&
+ if test_have_prereq WATCHMAN
+ then
+ INTEGRATION_SCRIPT=".git/hooks/fsmonitor-watchman" &&
+ cp "$TEST_DIRECTORY/../templates/hooks--fsmonitor-watchman.sample" "$INTEGRATION_SCRIPT"
+ else
+ INTEGRATION_SCRIPT=".git/hooks/fsmonitor-empty" &&
+ write_script "$INTEGRATION_SCRIPT"<<-\EOF
+ EOF
+ fi
+ fi &&
+
+ git config core.fsmonitor "$INTEGRATION_SCRIPT" &&
+ git update-index --fsmonitor 2>error &&
+ if test_have_prereq WATCHMAN
+ then
+ test_must_be_empty error # ensure no silent error
+ else
+ grep "Empty last update token" error
+ fi
+}
+
+test_perf_w_drop_caches () {
+ if test -n "$GIT_PERF_7519_DROP_CACHE"; then
+ test_perf "$1" --setup "test-tool drop-caches" "$2"
+ else
+ test_perf "$@"
+ fi
+}
+
+test_fsmonitor_suite () {
+ if test -n "$USE_FSMONITOR_DAEMON"
+ then
+ DESC="builtin fsmonitor--daemon"
+ elif test -n "$INTEGRATION_SCRIPT"
+ then
+ DESC="fsmonitor=$(basename $INTEGRATION_SCRIPT)"
+ else
+ DESC="fsmonitor=disabled"
+ fi
+
+ test_expect_success "test_initialization" '
+ git reset --hard &&
+ git status # Warm caches
+ '
+
+ test_perf_w_drop_caches "status ($DESC)" '
+ git status
+ '
+
+ test_perf_w_drop_caches "status -uno ($DESC)" '
+ git status -uno
+ '
+
+ test_perf_w_drop_caches "status -uall ($DESC)" '
+ git status -uall
+ '
+
+ # Update the mtimes on upto 100k files to make status think
+ # that they are dirty. For simplicity, omit any files with
+ # LFs (i.e. anything that ls-files thinks it needs to dquote)
+ # and any files with whitespace so that they pass thru xargs
+ # properly.
+ #
+ test_perf_w_drop_caches "status (dirty) ($DESC)" '
+ git ls-files | \
+ head -100000 | \
+ grep -v \" | \
+ grep -v " ." | \
+ xargs test-tool chmtime -300 &&
+ git status
+ '
+
+ test_perf_w_drop_caches "diff ($DESC)" '
+ git diff
+ '
+
+ test_perf_w_drop_caches "diff HEAD ($DESC)" '
+ git diff HEAD
+ '
+
+ test_perf_w_drop_caches "diff -- 0_files ($DESC)" '
+ git diff -- 1_file
+ '
+
+ test_perf_w_drop_caches "diff -- 10_files ($DESC)" '
+ git diff -- 10_files
+ '
+
+ test_perf_w_drop_caches "diff -- 100_files ($DESC)" '
+ git diff -- 100_files
+ '
+
+ test_perf_w_drop_caches "diff -- 1000_files ($DESC)" '
+ git diff -- 1000_files
+ '
+
+ test_perf_w_drop_caches "diff -- 10000_files ($DESC)" '
+ git diff -- 10000_files
+ '
+
+ test_perf_w_drop_caches "add ($DESC)" '
+ git add --all
+ '
+}
+
+#
+# Run a full set of perf tests using each Hook-based fsmonitor provider,
+# such as Watchman.
+#
+
+trace_start fsmonitor-watchman
+if test -n "$GIT_PERF_7519_FSMONITOR"; then
+ for INTEGRATION_PATH in $GIT_PERF_7519_FSMONITOR; do
+ test_expect_success "setup for fsmonitor $INTEGRATION_PATH" 'setup_for_fsmonitor_hook'
+ test_fsmonitor_suite
+ done
+else
+ test_expect_success "setup for fsmonitor hook" 'setup_for_fsmonitor_hook'
+ test_fsmonitor_suite
+fi
+
+if test_have_prereq WATCHMAN
+then
+ watchman watch-del "$GIT_WORK_TREE" >/dev/null 2>&1 &&
+
+ # Work around Watchman bug on Windows where it holds on to handles
+ # preventing the removal of the trash directory
+ watchman shutdown-server >/dev/null 2>&1
+fi
+trace_stop
+
+#
+# Run a full set of perf tests with the fsmonitor feature disabled.
+#
+
+trace_start fsmonitor-disabled
+test_expect_success "setup without fsmonitor" '
+ unset INTEGRATION_SCRIPT &&
+ git config --unset core.fsmonitor &&
+ git update-index --no-fsmonitor
+'
+
+test_fsmonitor_suite
+trace_stop
+
+#
+# Run a full set of perf tests using the built-in fsmonitor--daemon.
+# It does not use the Hook API, so it has a different setup.
+# Explicitly start the daemon here and before we start client commands
+# so that we can later add custom tracing.
+#
+if test_have_prereq FSMONITOR_DAEMON
+then
+ USE_FSMONITOR_DAEMON=t
+
+ test_expect_success "setup for builtin fsmonitor" '
+ trace_start fsmonitor--daemon--server &&
+ git fsmonitor--daemon start &&
+
+ trace_start fsmonitor--daemon--client &&
+
+ git config core.fsmonitor true &&
+ git update-index --fsmonitor
+ '
+
+ test_fsmonitor_suite
+
+ git fsmonitor--daemon stop
+ trace_stop
+fi
+
+test_done
diff --git a/t/perf/p7527-builtin-fsmonitor.sh b/t/perf/p7527-builtin-fsmonitor.sh
new file mode 100755
index 0000000..c3f9a4c
--- /dev/null
+++ b/t/perf/p7527-builtin-fsmonitor.sh
@@ -0,0 +1,257 @@
+#!/bin/sh
+
+test_description="Perf test for the builtin FSMonitor"
+
+. ./perf-lib.sh
+
+if ! test_have_prereq FSMONITOR_DAEMON
+then
+ skip_all="fsmonitor--daemon is not supported on this platform"
+ test_done
+fi
+
+test_lazy_prereq UNTRACKED_CACHE '
+ { git update-index --test-untracked-cache; ret=$?; } &&
+ test $ret -ne 1
+'
+
+# Lie to perf-lib and ask for a new empty repo and avoid
+# the complaints about GIT_PERF_REPO not being big enough
+# the perf hit when GIT_PERF_LARGE_REPO is copied into
+# the trash directory.
+#
+# NEEDSWORK: It would be nice if perf-lib had an option to
+# "borrow" an existing large repo (especially for gigantic
+# monorepos) and use it in-place. For now, fake it here.
+#
+test_perf_fresh_repo
+
+
+# Use a generated synthetic monorepo. If it doesn't exist, we will
+# generate it. If it does exist, we will put it in a known state
+# before we start our timings.
+#
+PARAM_D=5
+PARAM_W=10
+PARAM_F=9
+
+PARAMS="$PARAM_D"."$PARAM_W"."$PARAM_F"
+
+BALLAST_BR=p0006-ballast
+export BALLAST_BR
+
+TMP_BR=tmp_br
+export TMP_BR
+
+REPO=../repos/gen-many-files-"$PARAMS".git
+export REPO
+
+if ! test -d $REPO
+then
+ (cd ../repos; ./many-files.sh -d $PARAM_D -w $PARAM_W -f $PARAM_F)
+fi
+
+
+enable_uc () {
+ git -C $REPO config core.untrackedcache true
+ git -C $REPO update-index --untracked-cache
+ git -C $REPO status >/dev/null 2>&1
+}
+
+disable_uc () {
+ git -C $REPO config core.untrackedcache false
+ git -C $REPO update-index --no-untracked-cache
+ git -C $REPO status >/dev/null 2>&1
+}
+
+start_fsm () {
+ git -C $REPO fsmonitor--daemon start
+ git -C $REPO fsmonitor--daemon status
+ git -C $REPO config core.fsmonitor true
+ git -C $REPO update-index --fsmonitor
+ git -C $REPO status >/dev/null 2>&1
+}
+
+stop_fsm () {
+ git -C $REPO config --unset core.fsmonitor
+ git -C $REPO update-index --no-fsmonitor
+ test_might_fail git -C $REPO fsmonitor--daemon stop 2>/dev/null
+ git -C $REPO status >/dev/null 2>&1
+}
+
+
+# Ensure that FSMonitor is turned off on the borrowed repo.
+#
+test_expect_success "Setup borrowed repo (fsm+uc)" "
+ stop_fsm &&
+ disable_uc
+"
+
+# Also ensure that it starts in a known state.
+#
+# Because we assume that $GIT_PERF_REPEAT_COUNT > 1, we are not going to time
+# the ballast checkout, since only the first invocation does any work and the
+# subsequent ones just print "already on branch" and quit, so the reported
+# time is not useful.
+#
+# Create a temp branch and do all work relative to it so that we don't
+# accidentially alter the real ballast branch.
+#
+test_expect_success "Setup borrowed repo (temp ballast branch)" "
+ test_might_fail git -C $REPO checkout $BALLAST_BR &&
+ test_might_fail git -C $REPO reset --hard &&
+ git -C $REPO clean -d -f &&
+ test_might_fail git -C $REPO branch -D $TMP_BR &&
+ git -C $REPO branch $TMP_BR $BALLAST_BR &&
+ git -C $REPO checkout $TMP_BR
+"
+
+
+echo Data >data.txt
+
+# NEEDSWORK: We assume that $GIT_PERF_REPEAT_COUNT > 1. With
+# FSMonitor enabled, we can get a skewed view of status times, since
+# the index MAY (or may not) be updated after the first invocation
+# which will update the FSMonitor Token, so the subsequent invocations
+# may get a smaller response from the daemon.
+#
+do_status () {
+ msg=$1
+
+ test_perf "$msg" "
+ git -C $REPO status >/dev/null 2>&1
+ "
+}
+
+do_matrix () {
+ uc=$1
+ fsm=$2
+
+ t="[uc $uc][fsm $fsm]"
+ MATRIX_BR="$TMP_BR-$uc-$fsm"
+
+ test_expect_success "$t Setup matrix branch" "
+ git -C $REPO clean -d -f &&
+ git -C $REPO checkout $TMP_BR &&
+ test_might_fail git -C $REPO branch -D $MATRIX_BR &&
+ git -C $REPO branch $MATRIX_BR $TMP_BR &&
+ git -C $REPO checkout $MATRIX_BR
+ "
+
+ if test $uc = true
+ then
+ enable_uc
+ else
+ disable_uc
+ fi
+
+ if test $fsm = true
+ then
+ start_fsm
+ else
+ stop_fsm
+ fi
+
+ do_status "$t status after checkout"
+
+ # Modify many files in the matrix branch.
+ # Stage them.
+ # Commit them.
+ # Rollback.
+ #
+ test_expect_success "$t modify tracked files" "
+ find $REPO -name file1 -exec cp data.txt {} \\;
+ "
+
+ do_status "$t status after big change"
+
+ # Don't bother timing the "add" because _REPEAT_COUNT
+ # issue described above.
+ #
+ test_expect_success "$t add all" "
+ git -C $REPO add -A
+ "
+
+ do_status "$t status after add all"
+
+ test_expect_success "$t add dot" "
+ git -C $REPO add .
+ "
+
+ do_status "$t status after add dot"
+
+ test_expect_success "$t commit staged" "
+ git -C $REPO commit -a -m data
+ "
+
+ do_status "$t status after commit"
+
+ test_expect_success "$t reset HEAD~1 hard" "
+ git -C $REPO reset --hard HEAD~1 >/dev/null 2>&1
+ "
+
+ do_status "$t status after reset hard"
+
+ # Create some untracked files.
+ #
+ test_expect_success "$t create untracked files" "
+ cp -R $REPO/ballast/dir1 $REPO/ballast/xxx1
+ "
+
+ do_status "$t status after create untracked files"
+
+ # Remove the new untracked files.
+ #
+ test_expect_success "$t clean -df" "
+ git -C $REPO clean -d -f
+ "
+
+ do_status "$t status after clean"
+
+ if test $fsm = true
+ then
+ stop_fsm
+ fi
+}
+
+# Begin testing each case in the matrix that we care about.
+#
+uc_values="false"
+test_have_prereq UNTRACKED_CACHE && uc_values="false true"
+
+fsm_values="false true"
+
+for uc_val in $uc_values
+do
+ for fsm_val in $fsm_values
+ do
+ do_matrix $uc_val $fsm_val
+ done
+done
+
+cleanup () {
+ uc=$1
+ fsm=$2
+
+ MATRIX_BR="$TMP_BR-$uc-$fsm"
+
+ test_might_fail git -C $REPO branch -D $MATRIX_BR
+}
+
+
+# We're borrowing this repo. We should leave it in a clean state.
+#
+test_expect_success "Cleanup temp and matrix branches" "
+ git -C $REPO clean -d -f &&
+ test_might_fail git -C $REPO checkout $BALLAST_BR &&
+ test_might_fail git -C $REPO branch -D $TMP_BR &&
+ for uc_val in $uc_values
+ do
+ for fsm_val in $fsm_values
+ do
+ cleanup $uc_val $fsm_val || return 1
+ done
+ done
+"
+
+test_done
diff --git a/t/perf/p7810-grep.sh b/t/perf/p7810-grep.sh
new file mode 100755
index 0000000..9f4ade6
--- /dev/null
+++ b/t/perf/p7810-grep.sh
@@ -0,0 +1,23 @@
+#!/bin/sh
+
+test_description="git-grep performance in various modes"
+
+. ./perf-lib.sh
+
+test_perf_large_repo
+test_checkout_worktree
+
+test_perf 'grep worktree, cheap regex' '
+ git grep some_nonexistent_string || :
+'
+test_perf 'grep worktree, expensive regex' '
+ git grep "^.* *some_nonexistent_string$" || :
+'
+test_perf 'grep --cached, cheap regex' '
+ git grep --cached some_nonexistent_string || :
+'
+test_perf 'grep --cached, expensive regex' '
+ git grep --cached "^.* *some_nonexistent_string$" || :
+'
+
+test_done
diff --git a/t/perf/p7820-grep-engines.sh b/t/perf/p7820-grep-engines.sh
new file mode 100755
index 0000000..9bfb868
--- /dev/null
+++ b/t/perf/p7820-grep-engines.sh
@@ -0,0 +1,90 @@
+#!/bin/sh
+
+test_description="Comparison of git-grep's regex engines
+
+Set GIT_PERF_7820_GREP_OPTS in the environment to pass options to
+git-grep. Make sure to include a leading space,
+e.g. GIT_PERF_7820_GREP_OPTS=' -i'. Some options to try:
+
+ -i
+ -w
+ -v
+ -vi
+ -vw
+ -viw
+
+If GIT_PERF_GREP_THREADS is set to a list of threads (e.g. '1 4 8'
+etc.) we will test the patterns under those numbers of threads.
+"
+
+. ./perf-lib.sh
+
+test_perf_large_repo
+test_checkout_worktree
+
+if test -n "$GIT_PERF_GREP_THREADS"
+then
+ test_set_prereq PERF_GREP_ENGINES_THREADS
+fi
+
+for pattern in \
+ 'how.to' \
+ '^how to' \
+ '[how] to' \
+ '\(e.t[^ ]*\|v.ry\) rare' \
+ 'm\(ú\|u\)lt.b\(æ\|y\)te'
+do
+ for engine in basic extended perl
+ do
+ if test $engine != "basic"
+ then
+ # Poor man's basic -> extended converter.
+ pattern=$(echo "$pattern" | sed 's/\\//g')
+ fi
+ if test $engine = "perl" && ! test_have_prereq PCRE
+ then
+ prereq="PCRE"
+ else
+ prereq=""
+ fi
+ if ! test_have_prereq PERF_GREP_ENGINES_THREADS
+ then
+ test_perf "$engine grep$GIT_PERF_7820_GREP_OPTS '$pattern'" \
+ --prereq "$prereq" "
+ git -c grep.patternType=$engine grep$GIT_PERF_7820_GREP_OPTS -- '$pattern' >'out.$engine' || :
+ "
+ else
+ for threads in $GIT_PERF_GREP_THREADS
+ do
+ test_perf "$engine grep$GIT_PERF_7820_GREP_OPTS '$pattern' with $threads threads"
+ --prereq PTHREADS,$prereq "
+ git -c grep.patternType=$engine -c grep.threads=$threads grep$GIT_PERF_7820_GREP_OPTS -- '$pattern' >'out.$engine.$threads' || :
+ "
+ done
+ fi
+ done
+
+ if ! test_have_prereq PERF_GREP_ENGINES_THREADS
+ then
+ test_expect_success "assert that all engines found the same for$GIT_PERF_7820_GREP_OPTS '$pattern'" '
+ test_cmp out.basic out.extended &&
+ if test_have_prereq PCRE
+ then
+ test_cmp out.basic out.perl
+ fi
+ '
+ else
+ for threads in $GIT_PERF_GREP_THREADS
+ do
+ test_expect_success PTHREADS "assert that all engines found the same for$GIT_PERF_7820_GREP_OPTS '$pattern' under threading" "
+ test_cmp out.basic.$threads out.extended.$threads &&
+ if test_have_prereq PCRE
+ then
+ test_cmp out.basic.$threads out.perl.$threads
+ fi
+ "
+ done
+ fi
+done
+
+test_done
diff --git a/t/perf/p7821-grep-engines-fixed.sh b/t/perf/p7821-grep-engines-fixed.sh
new file mode 100755
index 0000000..61e41b8
--- /dev/null
+++ b/t/perf/p7821-grep-engines-fixed.sh
@@ -0,0 +1,74 @@
+#!/bin/sh
+
+test_description="Comparison of git-grep's regex engines with -F
+
+Set GIT_PERF_7821_GREP_OPTS in the environment to pass options to
+git-grep. Make sure to include a leading space,
+e.g. GIT_PERF_7821_GREP_OPTS=' -w'. See p7820-grep-engines.sh for more
+options to try.
+
+If GIT_PERF_7821_THREADS is set to a list of threads (e.g. '1 4 8'
+etc.) we will test the patterns under those numbers of threads.
+"
+
+. ./perf-lib.sh
+
+test_perf_large_repo
+test_checkout_worktree
+
+if test -n "$GIT_PERF_GREP_THREADS"
+then
+ test_set_prereq PERF_GREP_ENGINES_THREADS
+fi
+
+for pattern in 'int' 'uncommon' 'æ'
+do
+ for engine in fixed basic extended perl
+ do
+ if test $engine = "perl" && ! test_have_prereq PCRE
+ then
+ prereq="PCRE"
+ else
+ prereq=""
+ fi
+ if ! test_have_prereq PERF_GREP_ENGINES_THREADS
+ then
+ test_perf $prereq "$engine grep$GIT_PERF_7821_GREP_OPTS $pattern" "
+ git -c grep.patternType=$engine grep$GIT_PERF_7821_GREP_OPTS $pattern >'out.$engine' || :
+ "
+ else
+ for threads in $GIT_PERF_GREP_THREADS
+ do
+ test_perf PTHREADS,$prereq "$engine grep$GIT_PERF_7821_GREP_OPTS $pattern with $threads threads" "
+ git -c grep.patternType=$engine -c grep.threads=$threads grep$GIT_PERF_7821_GREP_OPTS $pattern >'out.$engine.$threads' || :
+ "
+ done
+ fi
+ done
+
+ if ! test_have_prereq PERF_GREP_ENGINES_THREADS
+ then
+ test_expect_success "assert that all engines found the same for$GIT_PERF_7821_GREP_OPTS $pattern" '
+ test_cmp out.fixed out.basic &&
+ test_cmp out.fixed out.extended &&
+ if test_have_prereq PCRE
+ then
+ test_cmp out.fixed out.perl
+ fi
+ '
+ else
+ for threads in $GIT_PERF_GREP_THREADS
+ do
+ test_expect_success PTHREADS "assert that all engines found the same for$GIT_PERF_7821_GREP_OPTS $pattern under threading" "
+ test_cmp out.fixed.$threads out.basic.$threads &&
+ test_cmp out.fixed.$threads out.extended.$threads &&
+ if test_have_prereq PCRE
+ then
+ test_cmp out.fixed.$threads out.perl.$threads
+ fi
+ "
+ done
+ fi
+done
+
+test_done
diff --git a/t/perf/p7822-grep-perl-character.sh b/t/perf/p7822-grep-perl-character.sh
new file mode 100755
index 0000000..87009c6
--- /dev/null
+++ b/t/perf/p7822-grep-perl-character.sh
@@ -0,0 +1,42 @@
+#!/bin/sh
+
+test_description="git-grep's perl regex
+
+If GIT_PERF_GREP_THREADS is set to a list of threads (e.g. '1 4 8'
+etc.) we will test the patterns under those numbers of threads.
+"
+
+. ./perf-lib.sh
+
+test_perf_large_repo
+test_checkout_worktree
+
+if test -n "$GIT_PERF_GREP_THREADS"
+then
+ test_set_prereq PERF_GREP_ENGINES_THREADS
+fi
+
+for pattern in \
+ '\\bhow' \
+ '\\bÆvar' \
+ '\\d+ \\bÆvar' \
+ '\\bBelón\\b' \
+ '\\w{12}\\b'
+do
+ echo '$pattern' >pat
+ if ! test_have_prereq PERF_GREP_ENGINES_THREADS
+ then
+ test_perf "grep -P '$pattern'" --prereq PCRE "
+ git -P grep -f pat || :
+ "
+ else
+ for threads in $GIT_PERF_GREP_THREADS
+ do
+ test_perf "grep -P '$pattern' with $threads threads" --prereq PTHREADS,PCRE "
+ git -c grep.threads=$threads -P grep -f pat || :
+ "
+ done
+ fi
+done
+
+test_done
diff --git a/t/perf/p9210-scalar.sh b/t/perf/p9210-scalar.sh
new file mode 100755
index 0000000..265f7cd
--- /dev/null
+++ b/t/perf/p9210-scalar.sh
@@ -0,0 +1,39 @@
+#!/bin/sh
+
+test_description='test scalar performance'
+. ./perf-lib.sh
+
+test_perf_large_repo "$TRASH_DIRECTORY/to-clone"
+
+test_expect_success 'enable server-side partial clone' '
+ git -C to-clone config uploadpack.allowFilter true &&
+ git -C to-clone config uploadpack.allowAnySHA1InWant true
+'
+
+test_perf 'scalar clone' '
+ rm -rf scalar-clone &&
+ scalar clone "file://$(pwd)/to-clone" scalar-clone
+'
+
+test_perf 'git clone' '
+ rm -rf git-clone &&
+ git clone "file://$(pwd)/to-clone" git-clone
+'
+
+test_compare_perf () {
+ command=$1
+ shift
+ args=$*
+ test_perf "$command $args (scalar)" "
+ $command -C scalar-clone/src $args
+ "
+
+ test_perf "$command $args (non-scalar)" "
+ $command -C git-clone $args
+ "
+}
+
+test_compare_perf git status
+test_compare_perf test_commit --append --no-tag A
+
+test_done
diff --git a/t/perf/p9300-fast-import-export.sh b/t/perf/p9300-fast-import-export.sh
new file mode 100755
index 0000000..586161e
--- /dev/null
+++ b/t/perf/p9300-fast-import-export.sh
@@ -0,0 +1,23 @@
+#!/bin/sh
+
+test_description='test fast-import and fast-export performance'
+. ./perf-lib.sh
+
+test_perf_default_repo
+
+# Use --no-data here to produce a vastly smaller export file.
+# This is much cheaper to work with but should still exercise
+# fast-import pretty well (we'll still process all commits and
+# trees, which account for 60% or more of objects in most repos).
+#
+# Use --reencode to avoid the default of aborting on non-utf8 commits,
+# which lets this test run against a wider variety of sample repos.
+test_perf 'export (no-blobs)' '
+ git fast-export --reencode=yes --no-data HEAD >export
+'
+
+test_perf 'import (no-blobs)' '
+ git fast-import --force <export
+'
+
+test_done
diff --git a/t/perf/perf-lib.sh b/t/perf/perf-lib.sh
new file mode 100644
index 0000000..e778677
--- /dev/null
+++ b/t/perf/perf-lib.sh
@@ -0,0 +1,334 @@
+# Performance testing framework. Each perf script starts much like
+# a normal test script, except it sources this library instead of
+# test-lib.sh. See t/perf/README for documentation.
+#
+# Copyright (c) 2011 Thomas Rast
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see http://www.gnu.org/licenses/ .
+
+# These variables must be set before the inclusion of test-lib.sh below,
+# because it will change our working directory.
+TEST_DIRECTORY=$(pwd)/..
+TEST_OUTPUT_DIRECTORY=$(pwd)
+
+TEST_NO_CREATE_REPO=t
+TEST_NO_MALLOC_CHECK=t
+
+. ../test-lib.sh
+
+unset GIT_CONFIG_NOSYSTEM
+GIT_CONFIG_SYSTEM="$TEST_DIRECTORY/perf/config"
+export GIT_CONFIG_SYSTEM
+
+if test -n "$GIT_TEST_INSTALLED" -a -z "$PERF_SET_GIT_TEST_INSTALLED"
+then
+ error "Do not use GIT_TEST_INSTALLED with the perf tests.
+
+Instead use:
+
+ ./run <path-to-git> -- <tests>
+
+See t/perf/README for details."
+fi
+
+# Variables from test-lib that are normally internal to the tests; we
+# need to export them for test_perf subshells
+export TEST_DIRECTORY TRASH_DIRECTORY GIT_BUILD_DIR GIT_TEST_CMP
+
+MODERN_GIT=$GIT_BUILD_DIR/bin-wrappers/git
+export MODERN_GIT
+
+MODERN_SCALAR=$GIT_BUILD_DIR/bin-wrappers/scalar
+export MODERN_SCALAR
+
+perf_results_dir=$TEST_RESULTS_DIR
+test -n "$GIT_PERF_SUBSECTION" && perf_results_dir="$perf_results_dir/$GIT_PERF_SUBSECTION"
+mkdir -p "$perf_results_dir"
+rm -f "$perf_results_dir"/$(basename "$0" .sh).subtests
+
+die_if_build_dir_not_repo () {
+ if ! ( cd "$TEST_DIRECTORY/.." &&
+ git rev-parse --build-dir >/dev/null 2>&1 ); then
+ error "No $1 defined, and your build directory is not a repo"
+ fi
+}
+
+if test -z "$GIT_PERF_REPO"; then
+ die_if_build_dir_not_repo '$GIT_PERF_REPO'
+ GIT_PERF_REPO=$TEST_DIRECTORY/..
+fi
+if test -z "$GIT_PERF_LARGE_REPO"; then
+ die_if_build_dir_not_repo '$GIT_PERF_LARGE_REPO'
+ GIT_PERF_LARGE_REPO=$TEST_DIRECTORY/..
+fi
+
+test_perf_do_repo_symlink_config_ () {
+ test_have_prereq SYMLINKS || git config core.symlinks false
+}
+
+test_perf_copy_repo_contents () {
+ for stuff in "$1"/*
+ do
+ case "$stuff" in
+ */objects|*/hooks|*/config|*/commondir|*/gitdir|*/worktrees|*/fsmonitor--daemon*)
+ ;;
+ *)
+ cp -R "$stuff" "$repo/.git/" || exit 1
+ ;;
+ esac
+ done
+}
+
+test_perf_create_repo_from () {
+ test "$#" = 2 ||
+ BUG "not 2 parameters to test-create-repo"
+ repo="$1"
+ source="$2"
+ source_git="$("$MODERN_GIT" -C "$source" rev-parse --git-dir)"
+ objects_dir="$("$MODERN_GIT" -C "$source" rev-parse --git-path objects)"
+ common_dir="$("$MODERN_GIT" -C "$source" rev-parse --git-common-dir)"
+ mkdir -p "$repo/.git"
+ (
+ cd "$source" &&
+ { cp -Rl "$objects_dir" "$repo/.git/" 2>/dev/null ||
+ cp -R "$objects_dir" "$repo/.git/"; } &&
+
+ # common_dir must come first here, since we want source_git to
+ # take precedence and overwrite any overlapping files
+ test_perf_copy_repo_contents "$common_dir"
+ if test "$source_git" != "$common_dir"
+ then
+ test_perf_copy_repo_contents "$source_git"
+ fi
+ ) &&
+ (
+ cd "$repo" &&
+ "$MODERN_GIT" init -q &&
+ test_perf_do_repo_symlink_config_ &&
+ mv .git/hooks .git/hooks-disabled 2>/dev/null &&
+ if test -f .git/index.lock
+ then
+ # We may be copying a repo that can't run "git
+ # status" due to a locked index. Since we have
+ # a copy it's fine to remove the lock.
+ rm .git/index.lock
+ fi &&
+ if test_bool_env GIT_PERF_USE_SCALAR false
+ then
+ "$MODERN_SCALAR" register
+ fi
+ ) || error "failed to copy repository '$source' to '$repo'"
+}
+
+# call at least one of these to establish an appropriately-sized repository
+test_perf_fresh_repo () {
+ repo="${1:-$TRASH_DIRECTORY}"
+ "$MODERN_GIT" init -q "$repo" &&
+ (
+ cd "$repo" &&
+ test_perf_do_repo_symlink_config_ &&
+ if test_bool_env GIT_PERF_USE_SCALAR false
+ then
+ "$MODERN_SCALAR" register
+ fi
+ )
+}
+
+test_perf_default_repo () {
+ test_perf_create_repo_from "${1:-$TRASH_DIRECTORY}" "$GIT_PERF_REPO"
+}
+test_perf_large_repo () {
+ if test "$GIT_PERF_LARGE_REPO" = "$GIT_BUILD_DIR"; then
+ echo "warning: \$GIT_PERF_LARGE_REPO is \$GIT_BUILD_DIR." >&2
+ echo "warning: This will work, but may not be a sufficiently large repo" >&2
+ echo "warning: for representative measurements." >&2
+ fi
+ test_perf_create_repo_from "${1:-$TRASH_DIRECTORY}" "$GIT_PERF_LARGE_REPO"
+}
+test_checkout_worktree () {
+ git checkout-index -u -a ||
+ error "git checkout-index failed"
+}
+
+# Performance tests should never fail. If they do, stop immediately
+immediate=t
+
+# Perf tests require GNU time
+case "$(uname -s)" in Darwin) GTIME="${GTIME:-gtime}";; esac
+GTIME="${GTIME:-/usr/bin/time}"
+
+test_run_perf_ () {
+ test_cleanup=:
+ test_export_="test_cleanup"
+ export test_cleanup test_export_
+ "$GTIME" -f "%E %U %S" -o test_time.$i "$TEST_SHELL_PATH" -c '
+. '"$TEST_DIRECTORY"/test-lib-functions.sh'
+test_export () {
+ test_export_="$test_export_ $*"
+}
+'"$1"'
+ret=$?
+needles=
+for v in $test_export_
+do
+ needles="$needles;s/^$v=/export $v=/p"
+done
+set | sed -n "s'"/'/'\\\\''/g"'$needles" >test_vars
+exit $ret' >&3 2>&4
+ eval_ret=$?
+
+ if test $eval_ret = 0 || test -n "$expecting_failure"
+ then
+ test_eval_ "$test_cleanup"
+ . ./test_vars || error "failed to load updated environment"
+ fi
+ if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"; then
+ echo ""
+ fi
+ return "$eval_ret"
+}
+
+test_wrapper_ () {
+ local test_wrapper_func_="$1"; shift
+ local test_title_="$1"; shift
+ test_start_
+ test_prereq=
+ test_perf_setup_=
+ while test $# != 0
+ do
+ case $1 in
+ --prereq)
+ test_prereq=$2
+ shift
+ ;;
+ --setup)
+ test_perf_setup_=$2
+ shift
+ ;;
+ *)
+ break
+ ;;
+ esac
+ shift
+ done
+ test "$#" = 1 || BUG "test_wrapper_ needs 2 positional parameters"
+ export test_prereq
+ export test_perf_setup_
+
+ if ! test_skip "$test_title_" "$@"
+ then
+ base=$(basename "$0" .sh)
+ echo "$test_count" >>"$perf_results_dir"/$base.subtests
+ echo "$test_title_" >"$perf_results_dir"/$base.$test_count.descr
+ base="$perf_results_dir"/"$PERF_RESULTS_PREFIX$(basename "$0" .sh)"."$test_count"
+ "$test_wrapper_func_" "$test_title_" "$@"
+ fi
+
+ test_finish_
+}
+
+test_perf_ () {
+ if test -z "$verbose"; then
+ printf "%s" "perf $test_count - $1:"
+ else
+ echo "perf $test_count - $1:"
+ fi
+ for i in $(test_seq 1 $GIT_PERF_REPEAT_COUNT); do
+ if test -n "$test_perf_setup_"
+ then
+ say >&3 "setup: $test_perf_setup_"
+ if ! test_eval_ $test_perf_setup_
+ then
+ test_failure_ "$test_perf_setup_"
+ break
+ fi
+
+ fi
+ say >&3 "running: $2"
+ if test_run_perf_ "$2"
+ then
+ if test -z "$verbose"; then
+ printf " %s" "$i"
+ else
+ echo "* timing run $i/$GIT_PERF_REPEAT_COUNT:"
+ fi
+ else
+ test -z "$verbose" && echo
+ test_failure_ "$@"
+ break
+ fi
+ done
+ if test -z "$verbose"; then
+ echo " ok"
+ else
+ test_ok_ "$1"
+ fi
+ "$TEST_DIRECTORY"/perf/min_time.perl test_time.* >"$base".result
+ rm test_time.*
+}
+
+# Usage: test_perf 'title' [options] 'perf-test'
+# Run the performance test script specified in perf-test with
+# optional prerequisite and setup steps.
+# Options:
+# --prereq prerequisites: Skip the test if prequisites aren't met
+# --setup "setup-steps": Run setup steps prior to each measured iteration
+#
+test_perf () {
+ test_wrapper_ test_perf_ "$@"
+}
+
+test_size_ () {
+ if test -n "$test_perf_setup_"
+ then
+ say >&3 "setup: $test_perf_setup_"
+ test_eval_ $test_perf_setup_
+ fi
+
+ say >&3 "running: $2"
+ if test_eval_ "$2" 3>"$base".result; then
+ test_ok_ "$1"
+ else
+ test_failure_ "$@"
+ fi
+}
+
+# Usage: test_size 'title' [options] 'size-test'
+# Run the size test script specified in size-test with optional
+# prerequisites and setup steps. Returns the numeric value
+# returned by size-test.
+# Options:
+# --prereq prerequisites: Skip the test if prequisites aren't met
+# --setup "setup-steps": Run setup steps prior to the size measurement
+
+test_size () {
+ test_wrapper_ test_size_ "$@"
+}
+
+# We extend test_done to print timings at the end (./run disables this
+# and does it after running everything)
+test_at_end_hook_ () {
+ if test -z "$GIT_PERF_AGGREGATING_LATER"; then
+ (
+ cd "$TEST_DIRECTORY"/perf &&
+ ./aggregate.perl --results-dir="$TEST_RESULTS_DIR" $(basename "$0")
+ )
+ fi
+}
+
+test_export () {
+ export "$@"
+}
+
+test_lazy_prereq PERF_EXTRA 'test_bool_env GIT_PERF_EXTRA false'
diff --git a/t/perf/repos/.gitignore b/t/perf/repos/.gitignore
new file mode 100644
index 0000000..72e3dc3
--- /dev/null
+++ b/t/perf/repos/.gitignore
@@ -0,0 +1 @@
+gen-*/
diff --git a/t/perf/repos/inflate-repo.sh b/t/perf/repos/inflate-repo.sh
new file mode 100755
index 0000000..fcfc992
--- /dev/null
+++ b/t/perf/repos/inflate-repo.sh
@@ -0,0 +1,85 @@
+#!/bin/sh
+# Inflate the size of an EXISTING repo.
+#
+# This script should be run inside the worktree of a TEST repo.
+# It will use the contents of the current HEAD to generate a
+# commit containing copies of the current worktree such that the
+# total size of the commit has at least <target_size> files.
+#
+# Usage: [-t target_size] [-b branch_name]
+
+set -e
+
+target_size=10000
+branch_name=p0006-ballast
+ballast=ballast
+
+while test "$#" -ne 0
+do
+ case "$1" in
+ -b)
+ shift;
+ test "$#" -ne 0 || { echo 'error: -b requires an argument' >&2; exit 1; }
+ branch_name=$1;
+ shift ;;
+ -t)
+ shift;
+ test "$#" -ne 0 || { echo 'error: -t requires an argument' >&2; exit 1; }
+ target_size=$1;
+ shift ;;
+ *)
+ echo "error: unknown option '$1'" >&2; exit 1 ;;
+ esac
+done
+
+git ls-tree -r HEAD >GEN_src_list
+nr_src_files=$(cat GEN_src_list | wc -l)
+
+src_branch=$(git symbolic-ref --short HEAD)
+
+echo "Branch $src_branch initially has $nr_src_files files."
+
+if test $target_size -le $nr_src_files
+then
+ echo "Repository already exceeds target size $target_size."
+ rm GEN_src_list
+ exit 1
+fi
+
+# Create well-known branch and add 1 file change to start
+# if off before the ballast.
+git checkout -b $branch_name HEAD
+echo "$target_size" > inflate-repo.params
+git add inflate-repo.params
+git commit -q -m params
+
+# Create ballast for in our branch.
+copy=1
+nr_files=$nr_src_files
+while test $nr_files -lt $target_size
+do
+ sed -e "s| | $ballast/$copy/|" <GEN_src_list |
+ git update-index --index-info
+
+ nr_files=$(expr $nr_files + $nr_src_files)
+ copy=$(expr $copy + 1)
+done
+rm GEN_src_list
+git commit -q -m "ballast"
+
+# Modify 1 file and commit.
+echo "$target_size" >> inflate-repo.params
+git add inflate-repo.params
+git commit -q -m "ballast plus 1"
+
+nr_files=$(git ls-files | wc -l)
+
+# Checkout master to put repo in canonical state (because
+# the perf test may need to clone and enable sparse-checkout
+# before attempting to checkout a commit with the ballast
+# (because it may contain 100K directories and 1M files)).
+git checkout $src_branch
+
+echo "Repository inflated. Branch $branch_name has $nr_files files."
+
+exit 0
diff --git a/t/perf/repos/many-files.sh b/t/perf/repos/many-files.sh
new file mode 100755
index 0000000..28720e4
--- /dev/null
+++ b/t/perf/repos/many-files.sh
@@ -0,0 +1,110 @@
+#!/bin/sh
+# Generate test data repository using the given parameters.
+# When omitted, we create "gen-many-files-d-w-f.git".
+#
+# Usage: [-r repo] [-d depth] [-w width] [-f files]
+#
+# -r repo: path to the new repo to be generated
+# -d depth: the depth of sub-directories
+# -w width: the number of sub-directories at each level
+# -f files: the number of files created in each directory
+#
+# Note that all files will have the same SHA-1 and each
+# directory at a level will have the same SHA-1, so we
+# will potentially have a large index, but not a large
+# ODB.
+#
+# Ballast will be created under "ballast/".
+
+EMPTY_BLOB=e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
+
+set -e
+
+# (5, 10, 9) will create 999,999 ballast files.
+# (4, 10, 9) will create 99,999 ballast files.
+depth=5
+width=10
+files=9
+
+while test "$#" -ne 0
+do
+ case "$1" in
+ -r)
+ shift;
+ test "$#" -ne 0 || { echo 'error: -r requires an argument' >&2; exit 1; }
+ repo=$1;
+ shift ;;
+ -d)
+ shift;
+ test "$#" -ne 0 || { echo 'error: -d requires an argument' >&2; exit 1; }
+ depth=$1;
+ shift ;;
+ -w)
+ shift;
+ test "$#" -ne 0 || { echo 'error: -w requires an argument' >&2; exit 1; }
+ width=$1;
+ shift ;;
+ -f)
+ shift;
+ test "$#" -ne 0 || { echo 'error: -f requires an argument' >&2; exit 1; }
+ files=$1;
+ shift ;;
+ *)
+ echo "error: unknown option '$1'" >&2; exit 1 ;;
+ esac
+done
+
+# Inflate the index with thousands of empty files.
+# usage: dir depth width files
+fill_index() {
+ awk -v arg_dir=$1 -v arg_depth=$2 -v arg_width=$3 -v arg_files=$4 '
+ function make_paths(dir, depth, width, files, f, w) {
+ for (f = 1; f <= files; f++) {
+ print dir "/file" f
+ }
+ if (depth > 0) {
+ for (w = 1; w <= width; w++) {
+ make_paths(dir "/dir" w, depth - 1, width, files)
+ }
+ }
+ }
+ END { make_paths(arg_dir, arg_depth, arg_width, arg_files) }
+ ' </dev/null |
+ sed "s/^/100644 $EMPTY_BLOB /" |
+ git update-index --index-info
+ return 0
+}
+
+[ -z "$repo" ] && repo=gen-many-files-$depth.$width.$files.git
+
+mkdir $repo
+cd $repo
+git init .
+
+# Create an initial commit just to define master.
+touch many-files.empty
+echo "$depth $width $files" >many-files.params
+git add many-files.*
+git commit -q -m params
+
+# Create ballast for p0006 based upon the given params and
+# inflate the index with thousands of empty files and commit.
+git checkout -b p0006-ballast
+fill_index "ballast" $depth $width $files
+git commit -q -m "ballast"
+
+nr_files=$(git ls-files | wc -l)
+
+# Modify 1 file and commit.
+echo "$depth $width $files" >>many-files.params
+git add many-files.params
+git commit -q -m "ballast plus 1"
+
+# Checkout master to put repo in canonical state (because
+# the perf test may need to clone and enable sparse-checkout
+# before attempting to checkout a commit with the ballast
+# (because it may contain 100K directories and 1M files)).
+git checkout master
+
+echo "Repository "$repo" ($depth, $width, $files) created. Ballast $nr_files."
+exit 0
diff --git a/t/perf/run b/t/perf/run
new file mode 100755
index 0000000..34115ed
--- /dev/null
+++ b/t/perf/run
@@ -0,0 +1,257 @@
+#!/bin/sh
+
+die () {
+ echo >&2 "error: $*"
+ exit 1
+}
+
+while [ $# -gt 0 ]; do
+ arg="$1"
+ case "$arg" in
+ --)
+ break ;;
+ --help)
+ echo "usage: $0 [--config file] [--subsection subsec] [other_git_tree...] [--] [test_scripts]"
+ exit 0 ;;
+ --config)
+ shift
+ GIT_PERF_CONFIG_FILE=$(cd "$(dirname "$1")"; pwd)/$(basename "$1")
+ export GIT_PERF_CONFIG_FILE
+ shift ;;
+ --subsection)
+ shift
+ GIT_PERF_SUBSECTION="$1"
+ export GIT_PERF_SUBSECTION
+ shift ;;
+ --*)
+ die "unrecognised option: '$arg'" ;;
+ *)
+ break ;;
+ esac
+done
+
+run_one_dir () {
+ if test $# -eq 0; then
+ set -- p????-*.sh
+ fi
+ echo "=== Running $# tests in ${GIT_TEST_INSTALLED:-this tree} ==="
+ for t in "$@"; do
+ ./$t $GIT_TEST_OPTS
+ done
+}
+
+unpack_git_rev () {
+ rev=$1
+ echo "=== Unpacking $rev in build/$rev ==="
+ mkdir -p build/$rev
+ (cd "$(git rev-parse --show-cdup)" && git archive --format=tar $rev) |
+ (cd build/$rev && tar x)
+}
+
+build_git_rev () {
+ rev=$1
+ name="$2"
+ for config in config.mak config.mak.autogen config.status
+ do
+ if test -e "../../$config"
+ then
+ cp "../../$config" "build/$rev/"
+ fi
+ done
+ echo "=== Building $rev ($name) ==="
+ (
+ cd build/$rev &&
+ if test -n "$GIT_PERF_MAKE_COMMAND"
+ then
+ sh -c "$GIT_PERF_MAKE_COMMAND"
+ else
+ make $GIT_PERF_MAKE_OPTS
+ fi
+ ) || die "failed to build revision '$mydir'"
+}
+
+set_git_test_installed () {
+ mydir=$1
+
+ mydir_abs=$(cd $mydir && pwd)
+ mydir_abs_wrappers="$mydir_abs/bin-wrappers"
+ if test -d "$mydir_abs_wrappers"
+ then
+ GIT_TEST_INSTALLED=$mydir_abs_wrappers
+ else
+ # Older versions of git lacked bin-wrappers;
+ # fallback to the files in the root.
+ GIT_TEST_INSTALLED=$mydir_abs
+ fi
+ export GIT_TEST_INSTALLED
+ PERF_SET_GIT_TEST_INSTALLED=true
+ export PERF_SET_GIT_TEST_INSTALLED
+}
+
+run_dirs_helper () {
+ mydir=${1%/}
+ shift
+ while test $# -gt 0 -a "$1" != -- -a ! -f "$1"; do
+ shift
+ done
+ if test $# -gt 0 -a "$1" = --; then
+ shift
+ fi
+
+ PERF_RESULTS_PREFIX=
+ if test "$mydir" = "."
+ then
+ unset GIT_TEST_INSTALLED
+ elif test -d "$mydir"
+ then
+ PERF_RESULTS_PREFIX=bindir$(cd $mydir && printf "%s" "$(pwd)" | tr -c "[a-zA-Z0-9]" "_").
+ set_git_test_installed "$mydir"
+ else
+ rev=$(git rev-parse --verify "$mydir" 2>/dev/null) ||
+ die "'$mydir' is neither a directory nor a valid revision"
+ if [ ! -d build/$rev ]; then
+ unpack_git_rev $rev
+ fi
+ build_git_rev $rev "$mydir"
+ mydir=build/$rev
+
+ PERF_RESULTS_PREFIX=build_$rev.
+ set_git_test_installed "$mydir"
+ fi
+ export PERF_RESULTS_PREFIX
+
+ run_one_dir "$@"
+}
+
+run_dirs () {
+ while test $# -gt 0 -a "$1" != -- -a ! -f "$1"; do
+ run_dirs_helper "$@"
+ shift
+ done
+}
+
+get_subsections () {
+ section="$1"
+ test -z "$GIT_PERF_CONFIG_FILE" && return
+ git config -f "$GIT_PERF_CONFIG_FILE" --name-only --get-regex "$section\..*\.[^.]+" |
+ sed -e "s/$section\.\(.*\)\..*/\1/" | sort | uniq
+}
+
+get_var_from_env_or_config () {
+ env_var="$1"
+ conf_sec="$2"
+ conf_var="$3"
+ conf_opts="$4" # optional
+
+ # Do nothing if the env variable is already set
+ eval "test -z \"\${$env_var+x}\"" || return
+
+ test -z "$GIT_PERF_CONFIG_FILE" && return
+
+ # Check if the variable is in the config file
+ if test -n "$GIT_PERF_SUBSECTION"
+ then
+ var="$conf_sec.$GIT_PERF_SUBSECTION.$conf_var"
+ conf_value=$(git config $conf_opts -f "$GIT_PERF_CONFIG_FILE" "$var") &&
+ eval "$env_var=\"$conf_value\"" && return
+ fi
+ var="$conf_sec.$conf_var"
+ conf_value=$(git config $conf_opts -f "$GIT_PERF_CONFIG_FILE" "$var") &&
+ eval "$env_var=\"$conf_value\""
+}
+
+run_subsection () {
+ get_var_from_env_or_config "GIT_PERF_REPEAT_COUNT" "perf" "repeatCount" "--int"
+ : ${GIT_PERF_REPEAT_COUNT:=3}
+ export GIT_PERF_REPEAT_COUNT
+
+ get_var_from_env_or_config "GIT_PERF_DIRS_OR_REVS" "perf" "dirsOrRevs"
+ set -- $GIT_PERF_DIRS_OR_REVS "$@"
+
+ get_var_from_env_or_config "GIT_PERF_MAKE_COMMAND" "perf" "makeCommand"
+ get_var_from_env_or_config "GIT_PERF_MAKE_OPTS" "perf" "makeOpts"
+
+ get_var_from_env_or_config "GIT_PERF_USE_SCALAR" "perf" "useScalar" "--bool"
+ export GIT_PERF_USE_SCALAR
+
+ get_var_from_env_or_config "GIT_PERF_REPO_NAME" "perf" "repoName"
+ export GIT_PERF_REPO_NAME
+
+ GIT_PERF_AGGREGATING_LATER=t
+ export GIT_PERF_AGGREGATING_LATER
+
+ if test $# = 0 -o "$1" = -- -o -f "$1"; then
+ set -- . "$@"
+ fi
+
+ codespeed_opt=
+ test "$GIT_PERF_CODESPEED_OUTPUT" = "true" && codespeed_opt="--codespeed"
+
+ run_dirs "$@"
+
+ if test -z "$GIT_PERF_SEND_TO_CODESPEED"
+ then
+ ./aggregate.perl --results-dir="$TEST_RESULTS_DIR" $codespeed_opt "$@"
+ else
+ json_res_file=""$TEST_RESULTS_DIR"/$GIT_PERF_SUBSECTION/aggregate.json"
+ ./aggregate.perl --results-dir="$TEST_RESULTS_DIR" --codespeed "$@" | tee "$json_res_file"
+ send_data_url="$GIT_PERF_SEND_TO_CODESPEED/result/add/json/"
+ curl -v --request POST --data-urlencode "json=$(cat "$json_res_file")" "$send_data_url"
+ fi
+}
+
+get_var_from_env_or_config "GIT_PERF_CODESPEED_OUTPUT" "perf" "codespeedOutput" "--bool"
+get_var_from_env_or_config "GIT_PERF_SEND_TO_CODESPEED" "perf" "sendToCodespeed"
+
+cd "$(dirname $0)"
+. ../../GIT-BUILD-OPTIONS
+
+if test -n "$TEST_OUTPUT_DIRECTORY"
+then
+ TEST_RESULTS_DIR="$TEST_OUTPUT_DIRECTORY/test-results"
+else
+ TEST_RESULTS_DIR=test-results
+fi
+
+mkdir -p "$TEST_RESULTS_DIR"
+get_subsections "perf" >"$TEST_RESULTS_DIR"/run_subsections.names
+
+if test $(wc -l <"$TEST_RESULTS_DIR"/run_subsections.names) -eq 0
+then
+ if test -n "$GIT_PERF_SUBSECTION"
+ then
+ if test -n "$GIT_PERF_CONFIG_FILE"
+ then
+ die "no subsections are defined in config file '$GIT_PERF_CONFIG_FILE'"
+ else
+ die "subsection '$GIT_PERF_SUBSECTION' defined without a config file"
+ fi
+ fi
+ (
+ run_subsection "$@"
+ )
+elif test -n "$GIT_PERF_SUBSECTION"
+then
+ grep -E "^$GIT_PERF_SUBSECTION\$" "$TEST_RESULTS_DIR"/run_subsections.names >/dev/null ||
+ die "subsection '$GIT_PERF_SUBSECTION' not found in '$GIT_PERF_CONFIG_FILE'"
+
+ grep -E "^$GIT_PERF_SUBSECTION\$" "$TEST_RESULTS_DIR"/run_subsections.names | while read -r subsec
+ do
+ (
+ GIT_PERF_SUBSECTION="$subsec"
+ export GIT_PERF_SUBSECTION
+ echo "======== Run for subsection '$GIT_PERF_SUBSECTION' ========"
+ run_subsection "$@"
+ )
+ done
+else
+ while read -r subsec
+ do
+ (
+ GIT_PERF_SUBSECTION="$subsec"
+ export GIT_PERF_SUBSECTION
+ echo "======== Run for subsection '$GIT_PERF_SUBSECTION' ========"
+ run_subsection "$@"
+ )
+ done <"$TEST_RESULTS_DIR"/run_subsections.names
+fi
diff --git a/t/t0000-basic.sh b/t/t0000-basic.sh
new file mode 100755
index 0000000..6e300be
--- /dev/null
+++ b/t/t0000-basic.sh
@@ -0,0 +1,1222 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+test_description='Test the very basics part #1.
+
+The rest of the test suite does not check the basic operation of git
+plumbing commands to work very carefully. Their job is to concentrate
+on tricky features that caused bugs in the past to detect regression.
+
+This test runs very basic features, like registering things in cache,
+writing tree, etc.
+
+Note that this test *deliberately* hard-codes many expected object
+IDs. When object ID computation changes, like in the previous case of
+swapping compression and hashing order, the person who is making the
+modification *should* take notice and update the test vectors here.
+'
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-subtest.sh
+
+try_local_xy () {
+ local x="local" y="alsolocal" &&
+ echo "$x $y"
+}
+
+# Check whether the shell supports the "local" keyword. "local" is not
+# POSIX-standard, but it is very widely supported by POSIX-compliant
+# shells, and we rely on it within Git's test framework.
+#
+# If your shell fails this test, the results of other tests may be
+# unreliable. You may wish to report the problem to the Git mailing
+# list <git@vger.kernel.org>, as it could cause us to reconsider
+# relying on "local".
+test_expect_success 'verify that the running shell supports "local"' '
+ x="notlocal" &&
+ y="alsonotlocal" &&
+ echo "local alsolocal" >expected1 &&
+ try_local_xy >actual1 &&
+ test_cmp expected1 actual1 &&
+ echo "notlocal alsonotlocal" >expected2 &&
+ echo "$x $y" >actual2 &&
+ test_cmp expected2 actual2
+'
+
+################################################################
+# git init has been done in an empty repository.
+# make sure it is empty.
+
+test_expect_success '.git/objects should be empty after git init in an empty repo' '
+ find .git/objects -type f -print >should-be-empty &&
+ test_line_count = 0 should-be-empty
+'
+
+# also it should have 2 subdirectories; no fan-out anymore, pack, and info.
+# 3 is counting "objects" itself
+test_expect_success '.git/objects should have 3 subdirectories' '
+ find .git/objects -type d -print >full-of-directories &&
+ test_line_count = 3 full-of-directories
+'
+
+################################################################
+# Test harness
+test_expect_success 'success is reported like this' '
+ :
+'
+
+test_expect_success 'subtest: 3 passing tests' '
+ write_and_run_sub_test_lib_test full-pass <<-\EOF &&
+ for i in 1 2 3
+ do
+ test_expect_success "passing test #$i" "true"
+ done
+ test_done
+ EOF
+ check_sub_test_lib_test full-pass <<-\EOF
+ > ok 1 - passing test #1
+ > ok 2 - passing test #2
+ > ok 3 - passing test #3
+ > # passed all 3 test(s)
+ > 1..3
+ EOF
+'
+
+test_expect_success 'subtest: 2/3 tests passing' '
+ write_and_run_sub_test_lib_test_err partial-pass <<-\EOF &&
+ test_expect_success "passing test #1" "true"
+ test_expect_success "failing test #2" "false"
+ test_expect_success "passing test #3" "true"
+ test_done
+ EOF
+ check_sub_test_lib_test partial-pass <<-\EOF
+ > ok 1 - passing test #1
+ > not ok 2 - failing test #2
+ # false
+ > ok 3 - passing test #3
+ > # failed 1 among 3 test(s)
+ > 1..3
+ EOF
+'
+
+test_expect_success 'subtest: --immediate' '
+ run_sub_test_lib_test_err partial-pass \
+ --immediate &&
+ check_sub_test_lib_test_err partial-pass \
+ <<-\EOF_OUT 3<<-EOF_ERR
+ > ok 1 - passing test #1
+ > not ok 2 - failing test #2
+ > # false
+ > 1..2
+ EOF_OUT
+ EOF_ERR
+'
+
+test_expect_success 'subtest: a failing TODO test' '
+ write_and_run_sub_test_lib_test failing-todo <<-\EOF &&
+ test_expect_success "passing test" "true"
+ test_expect_failure "pretend we have a known breakage" "false"
+ test_done
+ EOF
+ check_sub_test_lib_test failing-todo <<-\EOF
+ > ok 1 - passing test
+ > not ok 2 - pretend we have a known breakage # TODO known breakage
+ > # still have 1 known breakage(s)
+ > # passed all remaining 1 test(s)
+ > 1..2
+ EOF
+'
+
+test_expect_success 'subtest: a passing TODO test' '
+ write_and_run_sub_test_lib_test passing-todo <<-\EOF &&
+ test_expect_failure "pretend we have fixed a known breakage" "true"
+ test_done
+ EOF
+ check_sub_test_lib_test passing-todo <<-\EOF
+ > ok 1 - pretend we have fixed a known breakage # TODO known breakage vanished
+ > # 1 known breakage(s) vanished; please update test(s)
+ > 1..1
+ EOF
+'
+
+test_expect_success 'subtest: 2 TODO tests, one passin' '
+ write_and_run_sub_test_lib_test partially-passing-todos <<-\EOF &&
+ test_expect_failure "pretend we have a known breakage" "false"
+ test_expect_success "pretend we have a passing test" "true"
+ test_expect_failure "pretend we have fixed another known breakage" "true"
+ test_done
+ EOF
+ check_sub_test_lib_test partially-passing-todos <<-\EOF
+ > not ok 1 - pretend we have a known breakage # TODO known breakage
+ > ok 2 - pretend we have a passing test
+ > ok 3 - pretend we have fixed another known breakage # TODO known breakage vanished
+ > # 1 known breakage(s) vanished; please update test(s)
+ > # still have 1 known breakage(s)
+ > # passed all remaining 1 test(s)
+ > 1..3
+ EOF
+'
+
+test_expect_success 'subtest: mixed results: pass, failure and a TODO test' '
+ write_and_run_sub_test_lib_test_err mixed-results1 <<-\EOF &&
+ test_expect_success "passing test" "true"
+ test_expect_success "failing test" "false"
+ test_expect_failure "pretend we have a known breakage" "false"
+ test_done
+ EOF
+ check_sub_test_lib_test mixed-results1 <<-\EOF
+ > ok 1 - passing test
+ > not ok 2 - failing test
+ > # false
+ > not ok 3 - pretend we have a known breakage # TODO known breakage
+ > # still have 1 known breakage(s)
+ > # failed 1 among remaining 2 test(s)
+ > 1..3
+ EOF
+'
+
+test_expect_success 'subtest: mixed results: a mixture of all possible results' '
+ write_and_run_sub_test_lib_test_err mixed-results2 <<-\EOF &&
+ test_expect_success "passing test" "true"
+ test_expect_success "passing test" "true"
+ test_expect_success "passing test" "true"
+ test_expect_success "passing test" "true"
+ test_expect_success "failing test" "false"
+ test_expect_success "failing test" "false"
+ test_expect_success "failing test" "false"
+ test_expect_failure "pretend we have a known breakage" "false"
+ test_expect_failure "pretend we have a known breakage" "false"
+ test_expect_failure "pretend we have fixed a known breakage" "true"
+ test_done
+ EOF
+ check_sub_test_lib_test mixed-results2 <<-\EOF
+ > ok 1 - passing test
+ > ok 2 - passing test
+ > ok 3 - passing test
+ > ok 4 - passing test
+ > not ok 5 - failing test
+ > # false
+ > not ok 6 - failing test
+ > # false
+ > not ok 7 - failing test
+ > # false
+ > not ok 8 - pretend we have a known breakage # TODO known breakage
+ > not ok 9 - pretend we have a known breakage # TODO known breakage
+ > ok 10 - pretend we have fixed a known breakage # TODO known breakage vanished
+ > # 1 known breakage(s) vanished; please update test(s)
+ > # still have 2 known breakage(s)
+ > # failed 3 among remaining 7 test(s)
+ > 1..10
+ EOF
+'
+
+test_expect_success 'subtest: --verbose option' '
+ write_and_run_sub_test_lib_test_err t1234-verbose --verbose <<-\EOF &&
+ test_expect_success "passing test" true
+ test_expect_success "test with output" "echo foo"
+ test_expect_success "failing test" false
+ test_done
+ EOF
+ mv t1234-verbose/out t1234-verbose/out+ &&
+ grep -v "^Initialized empty" t1234-verbose/out+ >t1234-verbose/out &&
+ check_sub_test_lib_test t1234-verbose <<-\EOF
+ > expecting success of 1234.1 '\''passing test'\'': true
+ > ok 1 - passing test
+ > Z
+ > expecting success of 1234.2 '\''test with output'\'': echo foo
+ > foo
+ > ok 2 - test with output
+ > Z
+ > expecting success of 1234.3 '\''failing test'\'': false
+ > not ok 3 - failing test
+ > # false
+ > Z
+ > # failed 1 among 3 test(s)
+ > 1..3
+ EOF
+'
+
+test_expect_success 'subtest: --verbose-only option' '
+ run_sub_test_lib_test_err \
+ t1234-verbose \
+ --verbose-only=2 &&
+ check_sub_test_lib_test t1234-verbose <<-\EOF
+ > ok 1 - passing test
+ > Z
+ > expecting success of 1234.2 '\''test with output'\'': echo foo
+ > foo
+ > ok 2 - test with output
+ > Z
+ > not ok 3 - failing test
+ > # false
+ > # failed 1 among 3 test(s)
+ > 1..3
+ EOF
+'
+
+test_expect_success 'subtest: skip one with GIT_SKIP_TESTS' '
+ (
+ run_sub_test_lib_test full-pass \
+ --skip="full.2" &&
+ check_sub_test_lib_test full-pass <<-\EOF
+ > ok 1 - passing test #1
+ > ok 2 # skip passing test #2 (GIT_SKIP_TESTS)
+ > ok 3 - passing test #3
+ > # passed all 3 test(s)
+ > 1..3
+ EOF
+ )
+'
+
+test_expect_success 'subtest: skip several with GIT_SKIP_TESTS' '
+ (
+ write_and_run_sub_test_lib_test git-skip-tests-several \
+ --skip="git.2 git.5" <<-\EOF &&
+ for i in 1 2 3 4 5 6
+ do
+ test_expect_success "passing test #$i" "true"
+ done
+ test_done
+ EOF
+ check_sub_test_lib_test git-skip-tests-several <<-\EOF
+ > ok 1 - passing test #1
+ > ok 2 # skip passing test #2 (GIT_SKIP_TESTS)
+ > ok 3 - passing test #3
+ > ok 4 - passing test #4
+ > ok 5 # skip passing test #5 (GIT_SKIP_TESTS)
+ > ok 6 - passing test #6
+ > # passed all 6 test(s)
+ > 1..6
+ EOF
+ )
+'
+
+test_expect_success 'subtest: sh pattern skipping with GIT_SKIP_TESTS' '
+ (
+ run_sub_test_lib_test git-skip-tests-several \
+ --skip="git.[2-5]" &&
+ check_sub_test_lib_test git-skip-tests-several <<-\EOF
+ > ok 1 - passing test #1
+ > ok 2 # skip passing test #2 (GIT_SKIP_TESTS)
+ > ok 3 # skip passing test #3 (GIT_SKIP_TESTS)
+ > ok 4 # skip passing test #4 (GIT_SKIP_TESTS)
+ > ok 5 # skip passing test #5 (GIT_SKIP_TESTS)
+ > ok 6 - passing test #6
+ > # passed all 6 test(s)
+ > 1..6
+ EOF
+ )
+'
+
+test_expect_success 'subtest: skip entire test suite with GIT_SKIP_TESTS' '
+ (
+ GIT_SKIP_TESTS="git" && export GIT_SKIP_TESTS &&
+ run_sub_test_lib_test git-skip-tests-several \
+ --skip="git" &&
+ check_sub_test_lib_test git-skip-tests-several <<-\EOF
+ > 1..0 # SKIP skip all tests in git
+ EOF
+ )
+'
+
+test_expect_success 'subtest: GIT_SKIP_TESTS does not skip unmatched suite' '
+ (
+ GIT_SKIP_TESTS="notgit" && export GIT_SKIP_TESTS &&
+ run_sub_test_lib_test full-pass \
+ --skip="notfull" &&
+ check_sub_test_lib_test full-pass <<-\EOF
+ > ok 1 - passing test #1
+ > ok 2 - passing test #2
+ > ok 3 - passing test #3
+ > # passed all 3 test(s)
+ > 1..3
+ EOF
+ )
+'
+
+test_expect_success 'subtest: --run basic' '
+ run_sub_test_lib_test git-skip-tests-several --run="1,3,5" &&
+ check_sub_test_lib_test git-skip-tests-several <<-\EOF
+ > ok 1 - passing test #1
+ > ok 2 # skip passing test #2 (--run)
+ > ok 3 - passing test #3
+ > ok 4 # skip passing test #4 (--run)
+ > ok 5 - passing test #5
+ > ok 6 # skip passing test #6 (--run)
+ > # passed all 6 test(s)
+ > 1..6
+ EOF
+'
+
+test_expect_success 'subtest: --run with a range' '
+ run_sub_test_lib_test git-skip-tests-several \
+ --run="1-3" &&
+ check_sub_test_lib_test git-skip-tests-several <<-\EOF
+ > ok 1 - passing test #1
+ > ok 2 - passing test #2
+ > ok 3 - passing test #3
+ > ok 4 # skip passing test #4 (--run)
+ > ok 5 # skip passing test #5 (--run)
+ > ok 6 # skip passing test #6 (--run)
+ > # passed all 6 test(s)
+ > 1..6
+ EOF
+'
+
+test_expect_success 'subtest: --run with two ranges' '
+ run_sub_test_lib_test git-skip-tests-several \
+ --run="1-2,5-6" &&
+ check_sub_test_lib_test git-skip-tests-several <<-\EOF
+ > ok 1 - passing test #1
+ > ok 2 - passing test #2
+ > ok 3 # skip passing test #3 (--run)
+ > ok 4 # skip passing test #4 (--run)
+ > ok 5 - passing test #5
+ > ok 6 - passing test #6
+ > # passed all 6 test(s)
+ > 1..6
+ EOF
+'
+
+test_expect_success 'subtest: --run with a left open range' '
+ run_sub_test_lib_test git-skip-tests-several \
+ --run="-3" &&
+ check_sub_test_lib_test git-skip-tests-several <<-\EOF
+ > ok 1 - passing test #1
+ > ok 2 - passing test #2
+ > ok 3 - passing test #3
+ > ok 4 # skip passing test #4 (--run)
+ > ok 5 # skip passing test #5 (--run)
+ > ok 6 # skip passing test #6 (--run)
+ > # passed all 6 test(s)
+ > 1..6
+ EOF
+'
+
+test_expect_success 'subtest: --run with a right open range' '
+ run_sub_test_lib_test git-skip-tests-several \
+ --run="4-" &&
+ check_sub_test_lib_test git-skip-tests-several <<-\EOF
+ > ok 1 # skip passing test #1 (--run)
+ > ok 2 # skip passing test #2 (--run)
+ > ok 3 # skip passing test #3 (--run)
+ > ok 4 - passing test #4
+ > ok 5 - passing test #5
+ > ok 6 - passing test #6
+ > # passed all 6 test(s)
+ > 1..6
+ EOF
+'
+
+test_expect_success 'subtest: --run with basic negation' '
+ run_sub_test_lib_test git-skip-tests-several \
+ --run="!3" &&
+ check_sub_test_lib_test git-skip-tests-several <<-\EOF
+ > ok 1 - passing test #1
+ > ok 2 - passing test #2
+ > ok 3 # skip passing test #3 (--run)
+ > ok 4 - passing test #4
+ > ok 5 - passing test #5
+ > ok 6 - passing test #6
+ > # passed all 6 test(s)
+ > 1..6
+ EOF
+'
+
+test_expect_success 'subtest: --run with two negations' '
+ run_sub_test_lib_test git-skip-tests-several \
+ --run="!3,!6" &&
+ check_sub_test_lib_test git-skip-tests-several <<-\EOF
+ > ok 1 - passing test #1
+ > ok 2 - passing test #2
+ > ok 3 # skip passing test #3 (--run)
+ > ok 4 - passing test #4
+ > ok 5 - passing test #5
+ > ok 6 # skip passing test #6 (--run)
+ > # passed all 6 test(s)
+ > 1..6
+ EOF
+'
+
+test_expect_success 'subtest: --run a range and negation' '
+ run_sub_test_lib_test git-skip-tests-several \
+ --run="-4,!2" &&
+ check_sub_test_lib_test git-skip-tests-several <<-\EOF
+ > ok 1 - passing test #1
+ > ok 2 # skip passing test #2 (--run)
+ > ok 3 - passing test #3
+ > ok 4 - passing test #4
+ > ok 5 # skip passing test #5 (--run)
+ > ok 6 # skip passing test #6 (--run)
+ > # passed all 6 test(s)
+ > 1..6
+ EOF
+'
+
+test_expect_success 'subtest: --run range negation' '
+ run_sub_test_lib_test git-skip-tests-several \
+ --run="!1-3" &&
+ check_sub_test_lib_test git-skip-tests-several <<-\EOF
+ > ok 1 # skip passing test #1 (--run)
+ > ok 2 # skip passing test #2 (--run)
+ > ok 3 # skip passing test #3 (--run)
+ > ok 4 - passing test #4
+ > ok 5 - passing test #5
+ > ok 6 - passing test #6
+ > # passed all 6 test(s)
+ > 1..6
+ EOF
+'
+
+test_expect_success 'subtest: --run include, exclude and include' '
+ run_sub_test_lib_test git-skip-tests-several \
+ --run="1-5,!1-3,2" &&
+ check_sub_test_lib_test git-skip-tests-several <<-\EOF
+ > ok 1 # skip passing test #1 (--run)
+ > ok 2 - passing test #2
+ > ok 3 # skip passing test #3 (--run)
+ > ok 4 - passing test #4
+ > ok 5 - passing test #5
+ > ok 6 # skip passing test #6 (--run)
+ > # passed all 6 test(s)
+ > 1..6
+ EOF
+'
+
+test_expect_success 'subtest: --run include, exclude and include, comma separated' '
+ run_sub_test_lib_test git-skip-tests-several \
+ --run=1-5,!1-3,2 &&
+ check_sub_test_lib_test git-skip-tests-several <<-\EOF
+ > ok 1 # skip passing test #1 (--run)
+ > ok 2 - passing test #2
+ > ok 3 # skip passing test #3 (--run)
+ > ok 4 - passing test #4
+ > ok 5 - passing test #5
+ > ok 6 # skip passing test #6 (--run)
+ > # passed all 6 test(s)
+ > 1..6
+ EOF
+'
+
+test_expect_success 'subtest: --run exclude and include' '
+ run_sub_test_lib_test git-skip-tests-several \
+ --run="!3-,5" &&
+ check_sub_test_lib_test git-skip-tests-several <<-\EOF
+ > ok 1 - passing test #1
+ > ok 2 - passing test #2
+ > ok 3 # skip passing test #3 (--run)
+ > ok 4 # skip passing test #4 (--run)
+ > ok 5 - passing test #5
+ > ok 6 # skip passing test #6 (--run)
+ > # passed all 6 test(s)
+ > 1..6
+ EOF
+'
+
+test_expect_success 'subtest: --run empty selectors' '
+ run_sub_test_lib_test git-skip-tests-several \
+ --run="1,,3,,,5" &&
+ check_sub_test_lib_test git-skip-tests-several <<-\EOF
+ > ok 1 - passing test #1
+ > ok 2 # skip passing test #2 (--run)
+ > ok 3 - passing test #3
+ > ok 4 # skip passing test #4 (--run)
+ > ok 5 - passing test #5
+ > ok 6 # skip passing test #6 (--run)
+ > # passed all 6 test(s)
+ > 1..6
+ EOF
+'
+
+test_expect_success 'subtest: --run substring selector' '
+ write_and_run_sub_test_lib_test run-substring-selector \
+ --run="relevant" <<-\EOF &&
+ test_expect_success "relevant test" "true"
+ for i in 1 2 3 4 5 6
+ do
+ test_expect_success "other test #$i" "true"
+ done
+ test_done
+ EOF
+ check_sub_test_lib_test run-substring-selector <<-\EOF
+ > ok 1 - relevant test
+ > ok 2 # skip other test #1 (--run)
+ > ok 3 # skip other test #2 (--run)
+ > ok 4 # skip other test #3 (--run)
+ > ok 5 # skip other test #4 (--run)
+ > ok 6 # skip other test #5 (--run)
+ > ok 7 # skip other test #6 (--run)
+ > # passed all 7 test(s)
+ > 1..7
+ EOF
+'
+
+test_expect_success 'subtest: --run keyword selection' '
+ write_and_run_sub_test_lib_test_err run-inv-range-start \
+ --run="a-5" <<-\EOF &&
+ test_expect_success "passing test #1" "true"
+ test_done
+ EOF
+ check_sub_test_lib_test_err run-inv-range-start \
+ <<-\EOF_OUT 3<<-EOF_ERR
+ > FATAL: Unexpected exit with code 1
+ EOF_OUT
+ > error: --run: invalid non-numeric in range start: ${SQ}a-5${SQ}
+ EOF_ERR
+'
+
+test_expect_success 'subtest: --run invalid range end' '
+ run_sub_test_lib_test_err run-inv-range-start \
+ --run="1-z" &&
+ check_sub_test_lib_test_err run-inv-range-start \
+ <<-\EOF_OUT 3<<-EOF_ERR
+ > FATAL: Unexpected exit with code 1
+ EOF_OUT
+ > error: --run: invalid non-numeric in range end: ${SQ}1-z${SQ}
+ EOF_ERR
+'
+
+test_expect_success 'subtest: --invert-exit-code without --immediate' '
+ run_sub_test_lib_test_err full-pass \
+ --invert-exit-code &&
+ check_sub_test_lib_test_err full-pass \
+ <<-\EOF_OUT 3<<-EOF_ERR
+ ok 1 - passing test #1
+ ok 2 - passing test #2
+ ok 3 - passing test #3
+ # passed all 3 test(s)
+ 1..3
+ # faking up non-zero exit with --invert-exit-code
+ EOF_OUT
+ EOF_ERR
+'
+
+test_expect_success 'subtest: --invert-exit-code with --immediate: all passed' '
+ run_sub_test_lib_test_err full-pass \
+ --invert-exit-code --immediate &&
+ check_sub_test_lib_test_err full-pass \
+ <<-\EOF_OUT 3<<-EOF_ERR
+ ok 1 - passing test #1
+ ok 2 - passing test #2
+ ok 3 - passing test #3
+ # passed all 3 test(s)
+ 1..3
+ # faking up non-zero exit with --invert-exit-code
+ EOF_OUT
+ EOF_ERR
+'
+
+test_expect_success 'subtest: --invert-exit-code without --immediate: partial pass' '
+ run_sub_test_lib_test partial-pass \
+ --invert-exit-code &&
+ check_sub_test_lib_test partial-pass <<-\EOF
+ ok 1 - passing test #1
+ not ok 2 - # TODO induced breakage (--invert-exit-code): failing test #2
+ # false
+ ok 3 - passing test #3
+ # failed 1 among 3 test(s)
+ 1..3
+ # faked up failures as TODO & now exiting with 0 due to --invert-exit-code
+ EOF
+'
+
+test_expect_success 'subtest: --invert-exit-code with --immediate: partial pass' '
+ run_sub_test_lib_test partial-pass \
+ --invert-exit-code --immediate &&
+ check_sub_test_lib_test partial-pass \
+ <<-\EOF_OUT 3<<-EOF_ERR
+ ok 1 - passing test #1
+ not ok 2 - # TODO induced breakage (--invert-exit-code): failing test #2
+ # false
+ 1..2
+ # faked up failures as TODO & now exiting with 0 due to --invert-exit-code
+ EOF_OUT
+ EOF_ERR
+'
+
+test_expect_success 'subtest: --invert-exit-code --immediate: got a failure' '
+ run_sub_test_lib_test partial-pass \
+ --invert-exit-code --immediate &&
+ check_sub_test_lib_test_err partial-pass \
+ <<-\EOF_OUT 3<<-EOF_ERR
+ ok 1 - passing test #1
+ not ok 2 - # TODO induced breakage (--invert-exit-code): failing test #2
+ # false
+ 1..2
+ # faked up failures as TODO & now exiting with 0 due to --invert-exit-code
+ EOF_OUT
+ EOF_ERR
+'
+
+test_expect_success 'subtest: tests respect prerequisites' '
+ write_and_run_sub_test_lib_test prereqs <<-\EOF &&
+
+ test_set_prereq HAVEIT
+ test_expect_success HAVEIT "prereq is satisfied" "true"
+ test_expect_success "have_prereq works" "
+ test_have_prereq HAVEIT
+ "
+ test_expect_success DONTHAVEIT "prereq not satisfied" "false"
+
+ test_set_prereq HAVETHIS
+ test_expect_success HAVETHIS,HAVEIT "multiple prereqs" "true"
+ test_expect_success HAVEIT,DONTHAVEIT "mixed prereqs (yes,no)" "false"
+ test_expect_success DONTHAVEIT,HAVEIT "mixed prereqs (no,yes)" "false"
+
+ test_done
+ EOF
+
+ check_sub_test_lib_test prereqs <<-\EOF
+ ok 1 - prereq is satisfied
+ ok 2 - have_prereq works
+ ok 3 # skip prereq not satisfied (missing DONTHAVEIT)
+ ok 4 - multiple prereqs
+ ok 5 # skip mixed prereqs (yes,no) (missing DONTHAVEIT of HAVEIT,DONTHAVEIT)
+ ok 6 # skip mixed prereqs (no,yes) (missing DONTHAVEIT of DONTHAVEIT,HAVEIT)
+ # passed all 6 test(s)
+ 1..6
+ EOF
+'
+
+test_expect_success 'subtest: tests respect lazy prerequisites' '
+ write_and_run_sub_test_lib_test lazy-prereqs <<-\EOF &&
+
+ test_lazy_prereq LAZY_TRUE true
+ test_expect_success LAZY_TRUE "lazy prereq is satisifed" "true"
+ test_expect_success !LAZY_TRUE "negative lazy prereq" "false"
+
+ test_lazy_prereq LAZY_FALSE false
+ test_expect_success LAZY_FALSE "lazy prereq not satisfied" "false"
+ test_expect_success !LAZY_FALSE "negative false prereq" "true"
+
+ test_done
+ EOF
+
+ check_sub_test_lib_test lazy-prereqs <<-\EOF
+ ok 1 - lazy prereq is satisifed
+ ok 2 # skip negative lazy prereq (missing !LAZY_TRUE)
+ ok 3 # skip lazy prereq not satisfied (missing LAZY_FALSE)
+ ok 4 - negative false prereq
+ # passed all 4 test(s)
+ 1..4
+ EOF
+'
+
+test_expect_success 'subtest: nested lazy prerequisites' '
+ write_and_run_sub_test_lib_test nested-lazy <<-\EOF &&
+
+ test_lazy_prereq NESTED_INNER "
+ >inner &&
+ rm -f outer
+ "
+ test_lazy_prereq NESTED_PREREQ "
+ >outer &&
+ test_have_prereq NESTED_INNER &&
+ echo can create new file in cwd >file &&
+ test_path_is_file outer &&
+ test_path_is_missing inner
+ "
+ test_expect_success NESTED_PREREQ "evaluate nested prereq" "true"
+
+ test_done
+ EOF
+
+ check_sub_test_lib_test nested-lazy <<-\EOF
+ ok 1 - evaluate nested prereq
+ # passed all 1 test(s)
+ 1..1
+ EOF
+'
+
+test_expect_success 'subtest: lazy prereqs do not turn off tracing' '
+ write_and_run_sub_test_lib_test lazy-prereq-and-tracing \
+ -v -x <<-\EOF &&
+ test_lazy_prereq LAZY true
+
+ test_expect_success lazy "test_have_prereq LAZY && echo trace"
+
+ test_done
+ EOF
+
+ grep "echo trace" lazy-prereq-and-tracing/err
+'
+
+test_expect_success 'subtest: tests clean up after themselves' '
+ write_and_run_sub_test_lib_test cleanup <<-\EOF &&
+ clean=no
+ test_expect_success "do cleanup" "
+ test_when_finished clean=yes
+ "
+ test_expect_success "cleanup happened" "
+ test $clean = yes
+ "
+ test_done
+ EOF
+
+ check_sub_test_lib_test cleanup <<-\EOF
+ ok 1 - do cleanup
+ ok 2 - cleanup happened
+ # passed all 2 test(s)
+ 1..2
+ EOF
+'
+
+test_expect_success 'subtest: tests clean up even on failures' '
+ write_and_run_sub_test_lib_test_err \
+ failing-cleanup <<-\EOF &&
+ test_expect_success "tests clean up even after a failure" "
+ touch clean-after-failure &&
+ test_when_finished rm clean-after-failure &&
+ (exit 1)
+ "
+ test_expect_success "failure to clean up causes the test to fail" "
+ test_when_finished \"(exit 2)\"
+ "
+ test_done
+ EOF
+ check_sub_test_lib_test failing-cleanup <<-\EOF
+ > not ok 1 - tests clean up even after a failure
+ > # Z
+ > # touch clean-after-failure &&
+ > # test_when_finished rm clean-after-failure &&
+ > # (exit 1)
+ > # Z
+ > not ok 2 - failure to clean up causes the test to fail
+ > # Z
+ > # test_when_finished "(exit 2)"
+ > # Z
+ > # failed 2 among 2 test(s)
+ > 1..2
+ EOF
+'
+
+test_expect_success 'subtest: test_atexit is run' '
+ write_and_run_sub_test_lib_test_err \
+ atexit-cleanup -i <<-\EOF &&
+ test_expect_success "tests clean up even after a failure" "
+ > ../../clean-atexit &&
+ test_atexit rm ../../clean-atexit &&
+ > ../../also-clean-atexit &&
+ test_atexit rm ../../also-clean-atexit &&
+ > ../../dont-clean-atexit &&
+ (exit 1)
+ "
+ test_done
+ EOF
+ test_path_is_file dont-clean-atexit &&
+ test_path_is_missing clean-atexit &&
+ test_path_is_missing also-clean-atexit
+'
+
+test_expect_success 'test_oid provides sane info by default' '
+ test_oid zero >actual &&
+ grep "^00*\$" actual &&
+ rawsz="$(test_oid rawsz)" &&
+ hexsz="$(test_oid hexsz)" &&
+ # +1 accounts for the trailing newline
+ test $(( $hexsz + 1)) -eq $(wc -c <actual) &&
+ test $(( $rawsz * 2)) -eq "$hexsz"
+'
+
+test_expect_success 'test_oid can look up data for SHA-1' '
+ test_when_finished "test_detect_hash" &&
+ test_set_hash sha1 &&
+ test_oid zero >actual &&
+ grep "^00*\$" actual &&
+ rawsz="$(test_oid rawsz)" &&
+ hexsz="$(test_oid hexsz)" &&
+ test $(wc -c <actual) -eq 41 &&
+ test "$rawsz" -eq 20 &&
+ test "$hexsz" -eq 40
+'
+
+test_expect_success 'test_oid can look up data for SHA-256' '
+ test_when_finished "test_detect_hash" &&
+ test_set_hash sha256 &&
+ test_oid zero >actual &&
+ grep "^00*\$" actual &&
+ rawsz="$(test_oid rawsz)" &&
+ hexsz="$(test_oid hexsz)" &&
+ test $(wc -c <actual) -eq 65 &&
+ test "$rawsz" -eq 32 &&
+ test "$hexsz" -eq 64
+'
+
+test_expect_success 'test_oid can look up data for a specified algorithm' '
+ rawsz="$(test_oid --hash=sha1 rawsz)" &&
+ hexsz="$(test_oid --hash=sha1 hexsz)" &&
+ test "$rawsz" -eq 20 &&
+ test "$hexsz" -eq 40 &&
+ rawsz="$(test_oid --hash=sha256 rawsz)" &&
+ hexsz="$(test_oid --hash=sha256 hexsz)" &&
+ test "$rawsz" -eq 32 &&
+ test "$hexsz" -eq 64
+'
+
+test_expect_success 'test_bool_env' '
+ (
+ sane_unset envvar &&
+
+ test_bool_env envvar true &&
+ ! test_bool_env envvar false &&
+
+ envvar= &&
+ export envvar &&
+ ! test_bool_env envvar true &&
+ ! test_bool_env envvar false &&
+
+ envvar=true &&
+ test_bool_env envvar true &&
+ test_bool_env envvar false &&
+
+ envvar=false &&
+ ! test_bool_env envvar true &&
+ ! test_bool_env envvar false &&
+
+ envvar=invalid &&
+ # When encountering an invalid bool value, test_bool_env
+ # prints its error message to the original stderr of the
+ # test script, hence the redirection of fd 7, and aborts
+ # with "exit 1", hence the subshell.
+ ! ( test_bool_env envvar true ) 7>err &&
+ grep "error: test_bool_env requires bool values" err &&
+
+ envvar=true &&
+ ! ( test_bool_env envvar invalid ) 7>err &&
+ grep "error: test_bool_env requires bool values" err
+ )
+'
+
+################################################################
+# Basics of the basics
+
+test_oid_cache <<\EOF
+path0f sha1:f87290f8eb2cbbea7857214459a0739927eab154
+path0f sha256:638106af7c38be056f3212cbd7ac65bc1bac74f420ca5a436ff006a9d025d17d
+
+path0s sha1:15a98433ae33114b085f3eb3bb03b832b3180a01
+path0s sha256:3a24cc53cf68edddac490bbf94a418a52932130541361f685df685e41dd6c363
+
+path2f sha1:3feff949ed00a62d9f7af97c15cd8a30595e7ac7
+path2f sha256:2a7f36571c6fdbaf0e3f62751a0b25a3f4c54d2d1137b3f4af9cb794bb498e5f
+
+path2s sha1:d8ce161addc5173867a3c3c730924388daedbc38
+path2s sha256:18fd611b787c2e938ddcc248fabe4d66a150f9364763e9ec133dd01d5bb7c65a
+
+path2d sha1:58a09c23e2ca152193f2786e06986b7b6712bdbe
+path2d sha256:00e4b32b96e7e3d65d79112dcbea53238a22715f896933a62b811377e2650c17
+
+path3f sha1:0aa34cae68d0878578ad119c86ca2b5ed5b28376
+path3f sha256:09f58616b951bd571b8cb9dc76d372fbb09ab99db2393f5ab3189d26c45099ad
+
+path3s sha1:8599103969b43aff7e430efea79ca4636466794f
+path3s sha256:fce1aed087c053306f3f74c32c1a838c662bbc4551a7ac2420f5d6eb061374d0
+
+path3d sha1:21ae8269cacbe57ae09138dcc3a2887f904d02b3
+path3d sha256:9b60497be959cb830bf3f0dc82bcc9ad9e925a24e480837ade46b2295e47efe1
+
+subp3f sha1:00fb5908cb97c2564a9783c0c64087333b3b464f
+subp3f sha256:a1a9e16998c988453f18313d10375ee1d0ddefe757e710dcae0d66aa1e0c58b3
+
+subp3s sha1:6649a1ebe9e9f1c553b66f5a6e74136a07ccc57c
+subp3s sha256:81759d9f5e93c6546ecfcadb560c1ff057314b09f93fe8ec06e2d8610d34ef10
+
+subp3d sha1:3c5e5399f3a333eddecce7a9b9465b63f65f51e2
+subp3d sha256:76b4ef482d4fa1c754390344cf3851c7f883b27cf9bc999c6547928c46aeafb7
+
+root sha1:087704a96baf1c2d1c869a8b084481e121c88b5b
+root sha256:9481b52abab1b2ffeedbf9de63ce422b929f179c1b98ff7bee5f8f1bc0710751
+
+simpletree sha1:7bb943559a305bdd6bdee2cef6e5df2413c3d30a
+simpletree sha256:1710c07a6c86f9a3c7376364df04c47ee39e5a5e221fcdd84b743bc9bb7e2bc5
+EOF
+
+# updating a new file without --add should fail.
+test_expect_success 'git update-index without --add should fail adding' '
+ test_must_fail git update-index should-be-empty
+'
+
+# and with --add it should succeed, even if it is empty (it used to fail).
+test_expect_success 'git update-index with --add should succeed' '
+ git update-index --add should-be-empty
+'
+
+test_expect_success 'writing tree out with git write-tree' '
+ tree=$(git write-tree)
+'
+
+# we know the shape and contents of the tree and know the object ID for it.
+test_expect_success 'validate object ID of a known tree' '
+ test "$tree" = "$(test_oid simpletree)"
+ '
+
+# Removing paths.
+test_expect_success 'git update-index without --remove should fail removing' '
+ rm -f should-be-empty full-of-directories &&
+ test_must_fail git update-index should-be-empty
+'
+
+test_expect_success 'git update-index with --remove should be able to remove' '
+ git update-index --remove should-be-empty
+'
+
+# Empty tree can be written with recent write-tree.
+test_expect_success 'git write-tree should be able to write an empty tree' '
+ tree=$(git write-tree)
+'
+
+test_expect_success 'validate object ID of a known tree' '
+ test "$tree" = $EMPTY_TREE
+'
+
+# Various types of objects
+
+test_expect_success 'adding various types of objects with git update-index --add' '
+ mkdir path2 path3 path3/subp3 &&
+ paths="path0 path2/file2 path3/file3 path3/subp3/file3" &&
+ (
+ for p in $paths
+ do
+ echo "hello $p" >$p || exit 1
+ test_ln_s_add "hello $p" ${p}sym || exit 1
+ done
+ ) &&
+ find path* ! -type d -print | xargs git update-index --add
+'
+
+# Show them and see that matches what we expect.
+test_expect_success 'showing stage with git ls-files --stage' '
+ git ls-files --stage >current
+'
+
+test_expect_success 'validate git ls-files output for a known tree' '
+ cat >expected <<-EOF &&
+ 100644 $(test_oid path0f) 0 path0
+ 120000 $(test_oid path0s) 0 path0sym
+ 100644 $(test_oid path2f) 0 path2/file2
+ 120000 $(test_oid path2s) 0 path2/file2sym
+ 100644 $(test_oid path3f) 0 path3/file3
+ 120000 $(test_oid path3s) 0 path3/file3sym
+ 100644 $(test_oid subp3f) 0 path3/subp3/file3
+ 120000 $(test_oid subp3s) 0 path3/subp3/file3sym
+ EOF
+ test_cmp expected current
+'
+
+test_expect_success 'writing tree out with git write-tree' '
+ tree=$(git write-tree)
+'
+
+test_expect_success 'validate object ID for a known tree' '
+ test "$tree" = "$(test_oid root)"
+'
+
+test_expect_success 'showing tree with git ls-tree' '
+ git ls-tree $tree >current
+'
+
+test_expect_success 'git ls-tree output for a known tree' '
+ cat >expected <<-EOF &&
+ 100644 blob $(test_oid path0f) path0
+ 120000 blob $(test_oid path0s) path0sym
+ 040000 tree $(test_oid path2d) path2
+ 040000 tree $(test_oid path3d) path3
+ EOF
+ test_cmp expected current
+'
+
+# This changed in ls-tree pathspec change -- recursive does
+# not show tree nodes anymore.
+test_expect_success 'showing tree with git ls-tree -r' '
+ git ls-tree -r $tree >current
+'
+
+test_expect_success 'git ls-tree -r output for a known tree' '
+ cat >expected <<-EOF &&
+ 100644 blob $(test_oid path0f) path0
+ 120000 blob $(test_oid path0s) path0sym
+ 100644 blob $(test_oid path2f) path2/file2
+ 120000 blob $(test_oid path2s) path2/file2sym
+ 100644 blob $(test_oid path3f) path3/file3
+ 120000 blob $(test_oid path3s) path3/file3sym
+ 100644 blob $(test_oid subp3f) path3/subp3/file3
+ 120000 blob $(test_oid subp3s) path3/subp3/file3sym
+ EOF
+ test_cmp expected current
+'
+
+# But with -r -t we can have both.
+test_expect_success 'showing tree with git ls-tree -r -t' '
+ git ls-tree -r -t $tree >current
+'
+
+test_expect_success 'git ls-tree -r output for a known tree' '
+ cat >expected <<-EOF &&
+ 100644 blob $(test_oid path0f) path0
+ 120000 blob $(test_oid path0s) path0sym
+ 040000 tree $(test_oid path2d) path2
+ 100644 blob $(test_oid path2f) path2/file2
+ 120000 blob $(test_oid path2s) path2/file2sym
+ 040000 tree $(test_oid path3d) path3
+ 100644 blob $(test_oid path3f) path3/file3
+ 120000 blob $(test_oid path3s) path3/file3sym
+ 040000 tree $(test_oid subp3d) path3/subp3
+ 100644 blob $(test_oid subp3f) path3/subp3/file3
+ 120000 blob $(test_oid subp3s) path3/subp3/file3sym
+ EOF
+ test_cmp expected current
+'
+
+test_expect_success 'writing partial tree out with git write-tree --prefix' '
+ ptree=$(git write-tree --prefix=path3)
+'
+
+test_expect_success 'validate object ID for a known tree' '
+ test "$ptree" = $(test_oid path3d)
+'
+
+test_expect_success 'writing partial tree out with git write-tree --prefix' '
+ ptree=$(git write-tree --prefix=path3/subp3)
+'
+
+test_expect_success 'validate object ID for a known tree' '
+ test "$ptree" = $(test_oid subp3d)
+'
+
+test_expect_success 'put invalid objects into the index' '
+ rm -f .git/index &&
+ suffix=$(echo $ZERO_OID | sed -e "s/^.//") &&
+ cat >badobjects <<-EOF &&
+ 100644 blob $(test_oid 001) dir/file1
+ 100644 blob $(test_oid 002) dir/file2
+ 100644 blob $(test_oid 003) dir/file3
+ 100644 blob $(test_oid 004) dir/file4
+ 100644 blob $(test_oid 005) dir/file5
+ EOF
+ git update-index --index-info <badobjects
+'
+
+test_expect_success 'writing this tree without --missing-ok' '
+ test_must_fail git write-tree
+'
+
+test_expect_success 'writing this tree with --missing-ok' '
+ git write-tree --missing-ok
+'
+
+
+################################################################
+test_expect_success 'git read-tree followed by write-tree should be idempotent' '
+ rm -f .git/index &&
+ git read-tree $tree &&
+ test_path_is_file .git/index &&
+ newtree=$(git write-tree) &&
+ test "$newtree" = "$tree"
+'
+
+test_expect_success 'validate git diff-files output for a know cache/work tree state' '
+ cat >expected <<EOF &&
+:100644 100644 $(test_oid path0f) $ZERO_OID M path0
+:120000 120000 $(test_oid path0s) $ZERO_OID M path0sym
+:100644 100644 $(test_oid path2f) $ZERO_OID M path2/file2
+:120000 120000 $(test_oid path2s) $ZERO_OID M path2/file2sym
+:100644 100644 $(test_oid path3f) $ZERO_OID M path3/file3
+:120000 120000 $(test_oid path3s) $ZERO_OID M path3/file3sym
+:100644 100644 $(test_oid subp3f) $ZERO_OID M path3/subp3/file3
+:120000 120000 $(test_oid subp3s) $ZERO_OID M path3/subp3/file3sym
+EOF
+ git diff-files >current &&
+ test_cmp expected current
+'
+
+test_expect_success 'git update-index --refresh should succeed' '
+ git update-index --refresh
+'
+
+test_expect_success 'no diff after checkout and git update-index --refresh' '
+ git diff-files >current &&
+ cmp -s current /dev/null
+'
+
+################################################################
+P=$(test_oid root)
+
+test_expect_success 'git commit-tree records the correct tree in a commit' '
+ commit0=$(echo NO | git commit-tree $P) &&
+ git show --pretty=raw $commit0 >out &&
+ tree=$(sed -n -e "s/^tree //p" -e "/^author /q" out) &&
+ test "z$tree" = "z$P"
+'
+
+test_expect_success 'git commit-tree records the correct parent in a commit' '
+ commit1=$(echo NO | git commit-tree $P -p $commit0) &&
+ git show --pretty=raw $commit1 >out &&
+ parent=$(sed -n -e "s/^parent //p" -e "/^author /q" out) &&
+ test "z$commit0" = "z$parent"
+'
+
+test_expect_success 'git commit-tree omits duplicated parent in a commit' '
+ commit2=$(echo NO | git commit-tree $P -p $commit0 -p $commit0) &&
+ git show --pretty=raw $commit2 >out &&
+ cat >match.sed <<-\EOF &&
+ s/^parent //p
+ /^author /q
+ EOF
+ parent=$(sed -n -f match.sed out | sort -u) &&
+ test "z$commit0" = "z$parent" &&
+ git show --pretty=raw $commit2 >out &&
+ test_stdout_line_count = 1 sed -n -f match.sed out
+'
+
+test_expect_success 'update-index D/F conflict' '
+ mv path0 tmp &&
+ mv path2 path0 &&
+ mv tmp path2 &&
+ git update-index --add --replace path2 path0/file2 &&
+ git ls-files path0 >tmp &&
+ numpath0=$(wc -l <tmp) &&
+ test $numpath0 = 1
+'
+
+test_expect_success 'very long name in the index handled sanely' '
+
+ a=a && # 1
+ a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 16
+ a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 256
+ a=$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a$a && # 4096
+ a=${a}q &&
+
+ >path4 &&
+ git update-index --add path4 &&
+ git ls-files -s path4 >tmp &&
+ (
+ sed -e "s/ .*/ /" tmp |
+ tr -d "\012" &&
+ echo "$a"
+ ) | git update-index --index-info &&
+ git ls-files "a*" >tmp &&
+ len=$(wc -c <tmp) &&
+ test $len = 4098
+'
+
+test_expect_success 'test_must_fail on a failing git command' '
+ test_must_fail git notacommand
+'
+
+test_expect_success 'test_must_fail on a failing git command with env' '
+ test_must_fail env var1=a var2=b git notacommand
+'
+
+test_expect_success 'test_must_fail rejects a non-git command' '
+ ! test_must_fail grep ^$ notafile 2>err &&
+ grep -F "test_must_fail: only '"'"'git'"'"' is allowed" err
+'
+
+test_expect_success 'test_must_fail rejects a non-git command with env' '
+ ! test_must_fail env var1=a var2=b grep ^$ notafile 2>err &&
+ grep -F "test_must_fail: only '"'"'git'"'"' is allowed" err
+'
+
+test_done
diff --git a/t/t0001-init.sh b/t/t0001-init.sh
new file mode 100755
index 0000000..2b78e3b
--- /dev/null
+++ b/t/t0001-init.sh
@@ -0,0 +1,611 @@
+#!/bin/sh
+
+test_description='git init'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+check_config () {
+ if test_path_is_dir "$1" &&
+ test_path_is_file "$1/config" && test_path_is_dir "$1/refs"
+ then
+ : happy
+ else
+ echo "expected a directory $1, a file $1/config and $1/refs"
+ return 1
+ fi
+
+ if test_have_prereq POSIXPERM && test -x "$1/config"
+ then
+ echo "$1/config is executable?"
+ return 1
+ fi
+
+ bare=$(cd "$1" && git config --bool core.bare)
+ worktree=$(cd "$1" && git config core.worktree) ||
+ worktree=unset
+
+ test "$bare" = "$2" && test "$worktree" = "$3" || {
+ echo "expected bare=$2 worktree=$3"
+ echo " got bare=$bare worktree=$worktree"
+ return 1
+ }
+}
+
+test_expect_success 'plain' '
+ git init plain &&
+ check_config plain/.git false unset
+'
+
+test_expect_success 'plain nested in bare' '
+ (
+ git init --bare bare-ancestor.git &&
+ cd bare-ancestor.git &&
+ mkdir plain-nested &&
+ cd plain-nested &&
+ git init
+ ) &&
+ check_config bare-ancestor.git/plain-nested/.git false unset
+'
+
+test_expect_success 'plain through aliased command, outside any git repo' '
+ (
+ HOME=$(pwd)/alias-config &&
+ export HOME &&
+ mkdir alias-config &&
+ echo "[alias] aliasedinit = init" >alias-config/.gitconfig &&
+
+ GIT_CEILING_DIRECTORIES=$(pwd) &&
+ export GIT_CEILING_DIRECTORIES &&
+
+ mkdir plain-aliased &&
+ cd plain-aliased &&
+ git aliasedinit
+ ) &&
+ check_config plain-aliased/.git false unset
+'
+
+test_expect_success 'plain nested through aliased command' '
+ (
+ git init plain-ancestor-aliased &&
+ cd plain-ancestor-aliased &&
+ echo "[alias] aliasedinit = init" >>.git/config &&
+ mkdir plain-nested &&
+ cd plain-nested &&
+ git aliasedinit
+ ) &&
+ check_config plain-ancestor-aliased/plain-nested/.git false unset
+'
+
+test_expect_success 'plain nested in bare through aliased command' '
+ (
+ git init --bare bare-ancestor-aliased.git &&
+ cd bare-ancestor-aliased.git &&
+ echo "[alias] aliasedinit = init" >>config &&
+ mkdir plain-nested &&
+ cd plain-nested &&
+ git aliasedinit
+ ) &&
+ check_config bare-ancestor-aliased.git/plain-nested/.git false unset
+'
+
+test_expect_success 'No extra GIT_* on alias scripts' '
+ write_script script <<-\EOF &&
+ env |
+ sed -n \
+ -e "/^GIT_PREFIX=/d" \
+ -e "/^GIT_TEXTDOMAINDIR=/d" \
+ -e "/^GIT_TRACE2_PARENT/d" \
+ -e "/^GIT_/s/=.*//p" |
+ sort
+ EOF
+ ./script >expected &&
+ git config alias.script \!./script &&
+ ( mkdir sub && cd sub && git script >../actual ) &&
+ test_cmp expected actual
+'
+
+test_expect_success 'plain with GIT_WORK_TREE' '
+ mkdir plain-wt &&
+ test_must_fail env GIT_WORK_TREE="$(pwd)/plain-wt" git init plain-wt
+'
+
+test_expect_success 'plain bare' '
+ git --bare init plain-bare-1 &&
+ check_config plain-bare-1 true unset
+'
+
+test_expect_success 'plain bare with GIT_WORK_TREE' '
+ mkdir plain-bare-2 &&
+ test_must_fail \
+ env GIT_WORK_TREE="$(pwd)/plain-bare-2" \
+ git --bare init plain-bare-2
+'
+
+test_expect_success 'GIT_DIR bare' '
+ mkdir git-dir-bare.git &&
+ GIT_DIR=git-dir-bare.git git init &&
+ check_config git-dir-bare.git true unset
+'
+
+test_expect_success 'init --bare' '
+ git init --bare init-bare.git &&
+ check_config init-bare.git true unset
+'
+
+test_expect_success 'GIT_DIR non-bare' '
+
+ (
+ mkdir non-bare &&
+ cd non-bare &&
+ GIT_DIR=.git git init
+ ) &&
+ check_config non-bare/.git false unset
+'
+
+test_expect_success 'GIT_DIR & GIT_WORK_TREE (1)' '
+
+ (
+ mkdir git-dir-wt-1.git &&
+ GIT_WORK_TREE=$(pwd) GIT_DIR=git-dir-wt-1.git git init
+ ) &&
+ check_config git-dir-wt-1.git false "$(pwd)"
+'
+
+test_expect_success 'GIT_DIR & GIT_WORK_TREE (2)' '
+ mkdir git-dir-wt-2.git &&
+ test_must_fail env \
+ GIT_WORK_TREE="$(pwd)" \
+ GIT_DIR=git-dir-wt-2.git \
+ git --bare init
+'
+
+test_expect_success 'reinit' '
+
+ (
+ mkdir again &&
+ cd again &&
+ git -c init.defaultBranch=initial init >out1 2>err1 &&
+ git init >out2 2>err2
+ ) &&
+ test_grep "Initialized empty" again/out1 &&
+ test_grep "Reinitialized existing" again/out2 &&
+ test_must_be_empty again/err1 &&
+ test_must_be_empty again/err2
+'
+
+test_expect_success 'init with --template' '
+ mkdir template-source &&
+ echo content >template-source/file &&
+ git init --template=template-source template-custom &&
+ test_cmp template-source/file template-custom/.git/file
+'
+
+test_expect_success 'init with --template (blank)' '
+ git init template-plain &&
+ test_path_is_file template-plain/.git/info/exclude &&
+ git init --template= template-blank &&
+ test_path_is_missing template-blank/.git/info/exclude
+'
+
+init_no_templatedir_env () {
+ (
+ sane_unset GIT_TEMPLATE_DIR &&
+ NO_SET_GIT_TEMPLATE_DIR=t &&
+ export NO_SET_GIT_TEMPLATE_DIR &&
+ git init "$1"
+ )
+}
+
+test_expect_success 'init with init.templatedir set' '
+ mkdir templatedir-source &&
+ echo Content >templatedir-source/file &&
+ test_config_global init.templatedir "${HOME}/templatedir-source" &&
+
+ init_no_templatedir_env templatedir-set &&
+ test_cmp templatedir-source/file templatedir-set/.git/file
+'
+
+test_expect_success 'init with init.templatedir using ~ expansion' '
+ mkdir -p templatedir-source &&
+ echo Content >templatedir-source/file &&
+ test_config_global init.templatedir "~/templatedir-source" &&
+
+ init_no_templatedir_env templatedir-expansion &&
+ test_cmp templatedir-source/file templatedir-expansion/.git/file
+'
+
+test_expect_success 'init --bare/--shared overrides system/global config' '
+ test_config_global core.bare false &&
+ test_config_global core.sharedRepository 0640 &&
+ git init --bare --shared=0666 init-bare-shared-override &&
+ check_config init-bare-shared-override true unset &&
+ test x0666 = \
+ x$(git config -f init-bare-shared-override/config core.sharedRepository)
+'
+
+test_expect_success 'init honors global core.sharedRepository' '
+ test_config_global core.sharedRepository 0666 &&
+ git init shared-honor-global &&
+ test x0666 = \
+ x$(git config -f shared-honor-global/.git/config core.sharedRepository)
+'
+
+test_expect_success 'init allows insanely long --template' '
+ git init --template=$(printf "x%09999dx" 1) test
+'
+
+test_expect_success 'init creates a new directory' '
+ rm -fr newdir &&
+ git init newdir &&
+ test_path_is_dir newdir/.git/refs
+'
+
+test_expect_success 'init creates a new bare directory' '
+ rm -fr newdir &&
+ git init --bare newdir &&
+ test_path_is_dir newdir/refs
+'
+
+test_expect_success 'init recreates a directory' '
+ rm -fr newdir &&
+ mkdir newdir &&
+ git init newdir &&
+ test_path_is_dir newdir/.git/refs
+'
+
+test_expect_success 'init recreates a new bare directory' '
+ rm -fr newdir &&
+ mkdir newdir &&
+ git init --bare newdir &&
+ test_path_is_dir newdir/refs
+'
+
+test_expect_success 'init creates a new deep directory' '
+ rm -fr newdir &&
+ git init newdir/a/b/c &&
+ test_path_is_dir newdir/a/b/c/.git/refs
+'
+
+test_expect_success POSIXPERM 'init creates a new deep directory (umask vs. shared)' '
+ rm -fr newdir &&
+ (
+ # Leading directories should honor umask while
+ # the repository itself should follow "shared"
+ mkdir newdir &&
+ # Remove a default ACL if possible.
+ (setfacl -k newdir 2>/dev/null || true) &&
+ umask 002 &&
+ git init --bare --shared=0660 newdir/a/b/c &&
+ test_path_is_dir newdir/a/b/c/refs &&
+ ls -ld newdir/a newdir/a/b > lsab.out &&
+ ! grep -v "^drwxrw[sx]r-x" lsab.out &&
+ ls -ld newdir/a/b/c > lsc.out &&
+ ! grep -v "^drwxrw[sx]---" lsc.out
+ )
+'
+
+test_expect_success 'init notices EEXIST (1)' '
+ rm -fr newdir &&
+ >newdir &&
+ test_must_fail git init newdir &&
+ test_path_is_file newdir
+'
+
+test_expect_success 'init notices EEXIST (2)' '
+ rm -fr newdir &&
+ mkdir newdir &&
+ >newdir/a &&
+ test_must_fail git init newdir/a/b &&
+ test_path_is_file newdir/a
+'
+
+test_expect_success POSIXPERM,SANITY 'init notices EPERM' '
+ test_when_finished "chmod +w newdir" &&
+ rm -fr newdir &&
+ mkdir newdir &&
+ chmod -w newdir &&
+ test_must_fail git init newdir/a/b
+'
+
+test_expect_success 'init creates a new bare directory with global --bare' '
+ rm -rf newdir &&
+ git --bare init newdir &&
+ test_path_is_dir newdir/refs
+'
+
+test_expect_success 'init prefers command line to GIT_DIR' '
+ rm -rf newdir &&
+ mkdir otherdir &&
+ GIT_DIR=otherdir git --bare init newdir &&
+ test_path_is_dir newdir/refs &&
+ test_path_is_missing otherdir/refs
+'
+
+test_expect_success 'init with separate gitdir' '
+ rm -rf newdir &&
+ git init --separate-git-dir realgitdir newdir &&
+ newdir_git="$(cat newdir/.git)" &&
+ test_cmp_fspath "$(pwd)/realgitdir" "${newdir_git#gitdir: }" &&
+ test_path_is_dir realgitdir/refs
+'
+
+test_expect_success 'explicit bare & --separate-git-dir incompatible' '
+ test_must_fail git init --bare --separate-git-dir goop.git bare.git 2>err &&
+ test_grep "cannot be used together" err
+'
+
+test_expect_success 'implicit bare & --separate-git-dir incompatible' '
+ test_when_finished "rm -rf bare.git" &&
+ mkdir -p bare.git &&
+ test_must_fail env GIT_DIR=. \
+ git -C bare.git init --separate-git-dir goop.git 2>err &&
+ test_grep "incompatible" err
+'
+
+test_expect_success 'bare & --separate-git-dir incompatible within worktree' '
+ test_when_finished "rm -rf bare.git linkwt seprepo" &&
+ test_commit gumby &&
+ git clone --bare . bare.git &&
+ git -C bare.git worktree add --detach ../linkwt &&
+ test_must_fail git -C linkwt init --separate-git-dir seprepo 2>err &&
+ test_grep "incompatible" err
+'
+
+test_lazy_prereq GETCWD_IGNORES_PERMS '
+ base=GETCWD_TEST_BASE_DIR &&
+ mkdir -p $base/dir &&
+ chmod 100 $base ||
+ BUG "cannot prepare $base"
+
+ (
+ cd $base/dir &&
+ test-tool getcwd
+ )
+ status=$?
+
+ chmod 700 $base &&
+ rm -rf $base ||
+ BUG "cannot clean $base"
+ return $status
+'
+
+check_long_base_path () {
+ # exceed initial buffer size of strbuf_getcwd()
+ component=123456789abcdef &&
+ test_when_finished "chmod 0700 $component; rm -rf $component" &&
+ p31=$component/$component &&
+ p127=$p31/$p31/$p31/$p31 &&
+ mkdir -p $p127 &&
+ if test $# = 1
+ then
+ chmod $1 $component
+ fi &&
+ (
+ cd $p127 &&
+ git init newdir
+ )
+}
+
+test_expect_success 'init in long base path' '
+ check_long_base_path
+'
+
+test_expect_success GETCWD_IGNORES_PERMS 'init in long restricted base path' '
+ check_long_base_path 0111
+'
+
+test_expect_success 're-init on .git file' '
+ ( cd newdir && git init )
+'
+
+test_expect_success 're-init to update git link' '
+ git -C newdir init --separate-git-dir ../surrealgitdir &&
+ newdir_git="$(cat newdir/.git)" &&
+ test_cmp_fspath "$(pwd)/surrealgitdir" "${newdir_git#gitdir: }" &&
+ test_path_is_dir surrealgitdir/refs &&
+ test_path_is_missing realgitdir/refs
+'
+
+test_expect_success 're-init to move gitdir' '
+ rm -rf newdir realgitdir surrealgitdir &&
+ git init newdir &&
+ git -C newdir init --separate-git-dir ../realgitdir &&
+ newdir_git="$(cat newdir/.git)" &&
+ test_cmp_fspath "$(pwd)/realgitdir" "${newdir_git#gitdir: }" &&
+ test_path_is_dir realgitdir/refs
+'
+
+test_expect_success SYMLINKS 're-init to move gitdir symlink' '
+ rm -rf newdir realgitdir &&
+ git init newdir &&
+ (
+ cd newdir &&
+ mv .git here &&
+ ln -s here .git &&
+ git init --separate-git-dir ../realgitdir
+ ) &&
+ echo "gitdir: $(pwd)/realgitdir" >expected &&
+ test_cmp expected newdir/.git &&
+ test_cmp expected newdir/here &&
+ test_path_is_dir realgitdir/refs
+'
+
+sep_git_dir_worktree () {
+ test_when_finished "rm -rf mainwt linkwt seprepo" &&
+ git init mainwt &&
+ test_commit -C mainwt gumby &&
+ git -C mainwt worktree add --detach ../linkwt &&
+ git -C "$1" init --separate-git-dir ../seprepo &&
+ git -C mainwt rev-parse --git-common-dir >expect &&
+ git -C linkwt rev-parse --git-common-dir >actual &&
+ test_cmp expect actual
+}
+
+test_expect_success 're-init to move gitdir with linked worktrees' '
+ sep_git_dir_worktree mainwt
+'
+
+test_expect_success 're-init to move gitdir within linked worktree' '
+ sep_git_dir_worktree linkwt
+'
+
+test_expect_success MINGW '.git hidden' '
+ rm -rf newdir &&
+ (
+ sane_unset GIT_DIR GIT_WORK_TREE &&
+ mkdir newdir &&
+ cd newdir &&
+ git init &&
+ test_path_is_hidden .git
+ ) &&
+ check_config newdir/.git false unset
+'
+
+test_expect_success MINGW 'bare git dir not hidden' '
+ rm -rf newdir &&
+ (
+ sane_unset GIT_DIR GIT_WORK_TREE GIT_CONFIG &&
+ mkdir newdir &&
+ cd newdir &&
+ git --bare init
+ ) &&
+ ! is_hidden newdir
+'
+
+test_expect_success 'remote init from does not use config from cwd' '
+ rm -rf newdir &&
+ test_config core.logallrefupdates true &&
+ git init newdir &&
+ echo true >expect &&
+ git -C newdir config --bool core.logallrefupdates >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 're-init from a linked worktree' '
+ git init main-worktree &&
+ (
+ cd main-worktree &&
+ test_commit first &&
+ git worktree add ../linked-worktree &&
+ mv .git/info/exclude expected-exclude &&
+ cp .git/config expected-config &&
+ find .git/worktrees -print | sort >expected &&
+ git -C ../linked-worktree init &&
+ test_cmp expected-exclude .git/info/exclude &&
+ test_cmp expected-config .git/config &&
+ find .git/worktrees -print | sort >actual &&
+ test_cmp expected actual
+ )
+'
+
+test_expect_success 'init honors GIT_DEFAULT_HASH' '
+ GIT_DEFAULT_HASH=sha1 git init sha1 &&
+ git -C sha1 rev-parse --show-object-format >actual &&
+ echo sha1 >expected &&
+ test_cmp expected actual &&
+ GIT_DEFAULT_HASH=sha256 git init sha256 &&
+ git -C sha256 rev-parse --show-object-format >actual &&
+ echo sha256 >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'init honors --object-format' '
+ git init --object-format=sha1 explicit-sha1 &&
+ git -C explicit-sha1 rev-parse --show-object-format >actual &&
+ echo sha1 >expected &&
+ test_cmp expected actual &&
+ git init --object-format=sha256 explicit-sha256 &&
+ git -C explicit-sha256 rev-parse --show-object-format >actual &&
+ echo sha256 >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'extensions.objectFormat is not allowed with repo version 0' '
+ git init --object-format=sha256 explicit-v0 &&
+ git -C explicit-v0 config core.repositoryformatversion 0 &&
+ test_must_fail git -C explicit-v0 rev-parse --show-object-format
+'
+
+test_expect_success 'init rejects attempts to initialize with different hash' '
+ test_must_fail git -C sha1 init --object-format=sha256 &&
+ test_must_fail git -C sha256 init --object-format=sha1
+'
+
+test_expect_success MINGW 'core.hidedotfiles = false' '
+ git config --global core.hidedotfiles false &&
+ rm -rf newdir &&
+ mkdir newdir &&
+ (
+ sane_unset GIT_DIR GIT_WORK_TREE GIT_CONFIG &&
+ git -C newdir init
+ ) &&
+ ! is_hidden newdir/.git
+'
+
+test_expect_success MINGW 'redirect std handles' '
+ GIT_REDIRECT_STDOUT=output.txt git rev-parse --git-dir &&
+ test .git = "$(cat output.txt)" &&
+ test -z "$(GIT_REDIRECT_STDOUT=off git rev-parse --git-dir)" &&
+ test_must_fail env \
+ GIT_REDIRECT_STDOUT=output.txt \
+ GIT_REDIRECT_STDERR="2>&1" \
+ git rev-parse --git-dir --verify refs/invalid &&
+ grep "^\\.git\$" output.txt &&
+ grep "Needed a single revision" output.txt
+'
+
+test_expect_success '--initial-branch' '
+ git init --initial-branch=hello initial-branch-option &&
+ git -C initial-branch-option symbolic-ref HEAD >actual &&
+ echo refs/heads/hello >expect &&
+ test_cmp expect actual &&
+
+ : re-initializing should not change the branch name &&
+ git init --initial-branch=ignore initial-branch-option 2>err &&
+ test_grep "ignored --initial-branch" err &&
+ git -C initial-branch-option symbolic-ref HEAD >actual &&
+ grep hello actual
+'
+
+test_expect_success 'overridden default initial branch name (config)' '
+ test_config_global init.defaultBranch nmb &&
+ GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME= git init initial-branch-config &&
+ git -C initial-branch-config symbolic-ref HEAD >actual &&
+ grep nmb actual
+'
+
+test_expect_success 'advice on unconfigured init.defaultBranch' '
+ GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME= git -c color.advice=always \
+ init unconfigured-default-branch-name 2>err &&
+ test_decode_color <err >decoded &&
+ test_grep "<YELLOW>hint: " decoded
+'
+
+test_expect_success 'overridden default main branch name (env)' '
+ test_config_global init.defaultBranch nmb &&
+ GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=env git init main-branch-env &&
+ git -C main-branch-env symbolic-ref HEAD >actual &&
+ grep env actual
+'
+
+test_expect_success 'invalid default branch name' '
+ test_must_fail env GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME="with space" \
+ git init initial-branch-invalid 2>err &&
+ test_grep "invalid branch name" err
+'
+
+test_expect_success 'branch -m with the initial branch' '
+ git init rename-initial &&
+ git -C rename-initial branch -m renamed &&
+ echo renamed >expect &&
+ git -C rename-initial symbolic-ref --short HEAD >actual &&
+ test_cmp expect actual &&
+
+ git -C rename-initial branch -m renamed again &&
+ echo again >expect &&
+ git -C rename-initial symbolic-ref --short HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t0002-gitfile.sh b/t/t0002-gitfile.sh
new file mode 100755
index 0000000..736516c
--- /dev/null
+++ b/t/t0002-gitfile.sh
@@ -0,0 +1,139 @@
+#!/bin/sh
+
+test_description='.git file
+
+Verify that plumbing commands work when .git is a file
+'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+objpath() {
+ echo "$1" | sed -e 's|\(..\)|\1/|'
+}
+
+test_expect_success 'initial setup' '
+ REAL="$(pwd)/.real" &&
+ mv .git "$REAL"
+'
+
+test_expect_success 'bad setup: invalid .git file format' '
+ echo "gitdir $REAL" >.git &&
+ test_must_fail git rev-parse 2>.err &&
+ test_grep "invalid gitfile format" .err
+'
+
+test_expect_success 'bad setup: invalid .git file path' '
+ echo "gitdir: $REAL.not" >.git &&
+ test_must_fail git rev-parse 2>.err &&
+ test_grep "not a git repository" .err
+'
+
+test_expect_success 'final setup + check rev-parse --git-dir' '
+ echo "gitdir: $REAL" >.git &&
+ echo "$REAL" >expect &&
+ git rev-parse --git-dir >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'check hash-object' '
+ echo "foo" >bar &&
+ SHA=$(cat bar | git hash-object -w --stdin) &&
+ test_path_is_file "$REAL/objects/$(objpath $SHA)"
+'
+
+test_expect_success 'check cat-file' '
+ git cat-file blob $SHA >actual &&
+ test_cmp bar actual
+'
+
+test_expect_success 'check update-index' '
+ test_path_is_missing "$REAL/index" &&
+ rm -f "$REAL/objects/$(objpath $SHA)" &&
+ git update-index --add bar &&
+ test_path_is_file "$REAL/index" &&
+ test_path_is_file "$REAL/objects/$(objpath $SHA)"
+'
+
+test_expect_success 'check write-tree' '
+ SHA=$(git write-tree) &&
+ test_path_is_file "$REAL/objects/$(objpath $SHA)"
+'
+
+test_expect_success 'check commit-tree' '
+ SHA=$(echo "commit bar" | git commit-tree $SHA) &&
+ test_path_is_file "$REAL/objects/$(objpath $SHA)"
+'
+
+test_expect_success 'check rev-list' '
+ git update-ref "HEAD" "$SHA" &&
+ git rev-list HEAD >actual &&
+ echo $SHA >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'setup_git_dir twice in subdir' '
+ git init sgd &&
+ (
+ cd sgd &&
+ git config alias.lsfi ls-files &&
+ mv .git .realgit &&
+ echo "gitdir: .realgit" >.git &&
+ mkdir subdir &&
+ cd subdir &&
+ >foo &&
+ git add foo &&
+ git lsfi >actual &&
+ echo foo >expected &&
+ test_cmp expected actual
+ )
+'
+
+test_expect_success 'enter_repo non-strict mode' '
+ test_create_repo enter_repo &&
+ (
+ cd enter_repo &&
+ test_tick &&
+ test_commit foo &&
+ mv .git .realgit &&
+ echo "gitdir: .realgit" >.git
+ ) &&
+ head=$(git -C enter_repo rev-parse HEAD) &&
+ git ls-remote enter_repo >actual &&
+ cat >expected <<-EOF &&
+ $head HEAD
+ $head refs/heads/main
+ $head refs/tags/foo
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'enter_repo linked checkout' '
+ (
+ cd enter_repo &&
+ git worktree add ../foo refs/tags/foo
+ ) &&
+ head=$(git -C enter_repo rev-parse HEAD) &&
+ git ls-remote foo >actual &&
+ cat >expected <<-EOF &&
+ $head HEAD
+ $head refs/heads/main
+ $head refs/tags/foo
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'enter_repo strict mode' '
+ head=$(git -C enter_repo rev-parse HEAD) &&
+ git ls-remote --upload-pack="git upload-pack --strict" foo/.git >actual &&
+ cat >expected <<-EOF &&
+ $head HEAD
+ $head refs/heads/main
+ $head refs/tags/foo
+ EOF
+ test_cmp expected actual
+'
+
+test_done
diff --git a/t/t0003-attributes.sh b/t/t0003-attributes.sh
new file mode 100755
index 0000000..aee2298
--- /dev/null
+++ b/t/t0003-attributes.sh
@@ -0,0 +1,561 @@
+#!/bin/sh
+
+test_description=gitattributes
+
+TEST_PASSES_SANITIZE_LEAK=true
+TEST_CREATE_REPO_NO_TEMPLATE=1
+. ./test-lib.sh
+
+attr_check_basic () {
+ path="$1" expect="$2" git_opts="$3" &&
+
+ git $git_opts check-attr test -- "$path" >actual 2>err &&
+ echo "$path: test: $expect" >expect &&
+ test_cmp expect actual
+}
+
+attr_check () {
+ attr_check_basic "$@" &&
+ test_must_be_empty err
+}
+
+attr_check_quote () {
+ path="$1" quoted_path="$2" expect="$3" &&
+
+ git check-attr test -- "$path" >actual &&
+ echo "\"$quoted_path\": test: $expect" >expect &&
+ test_cmp expect actual
+}
+
+attr_check_source () {
+ path="$1" expect="$2" source="$3" git_opts="$4" &&
+
+ echo "$path: test: $expect" >expect &&
+
+ git $git_opts check-attr --source $source test -- "$path" >actual 2>err &&
+ test_cmp expect actual &&
+ test_must_be_empty err &&
+
+ git $git_opts --attr-source="$source" check-attr test -- "$path" >actual 2>err &&
+ test_cmp expect actual &&
+ test_must_be_empty err
+
+ git $git_opts -c "attr.tree=$source" check-attr test -- "$path" >actual 2>err &&
+ test_cmp expect actual &&
+ test_must_be_empty err
+
+ GIT_ATTR_SOURCE="$source" git $git_opts check-attr test -- "$path" >actual 2>err &&
+ test_cmp expect actual &&
+ test_must_be_empty err
+}
+
+test_expect_success 'open-quoted pathname' '
+ echo "\"a test=a" >.gitattributes &&
+ attr_check a unspecified
+'
+
+test_expect_success 'setup' '
+ mkdir -p a/b/d a/c b &&
+ (
+ echo "[attr]notest !test" &&
+ echo "\" d \" test=d" &&
+ echo " e test=e" &&
+ echo " e\" test=e" &&
+ echo "f test=f" &&
+ echo "a/i test=a/i" &&
+ echo "onoff test -test" &&
+ echo "offon -test test" &&
+ echo "no notest" &&
+ echo "A/e/F test=A/e/F"
+ ) >.gitattributes &&
+ (
+ echo "g test=a/g" &&
+ echo "b/g test=a/b/g"
+ ) >a/.gitattributes &&
+ (
+ echo "h test=a/b/h" &&
+ echo "d/* test=a/b/d/*" &&
+ echo "d/yes notest"
+ ) >a/b/.gitattributes &&
+ (
+ echo "global test=global"
+ ) >"$HOME"/global-gitattributes &&
+ cat <<-EOF >expect-all
+ f: test: f
+ a/f: test: f
+ a/c/f: test: f
+ a/g: test: a/g
+ a/b/g: test: a/b/g
+ b/g: test: unspecified
+ a/b/h: test: a/b/h
+ a/b/d/g: test: a/b/d/*
+ onoff: test: unset
+ offon: test: set
+ no: notest: set
+ no: test: unspecified
+ a/b/d/no: notest: set
+ a/b/d/no: test: a/b/d/*
+ a/b/d/yes: notest: set
+ a/b/d/yes: test: unspecified
+ EOF
+'
+
+test_expect_success 'setup branches' '
+ mkdir -p foo/bar &&
+ test_commit --printf "add .gitattributes" foo/bar/.gitattributes \
+ "f test=f\na/i test=n\n" tag-1 &&
+ test_commit --printf "add .gitattributes" foo/bar/.gitattributes \
+ "g test=g\na/i test=m\n" tag-2 &&
+ rm foo/bar/.gitattributes
+'
+
+test_expect_success 'command line checks' '
+ test_must_fail git check-attr &&
+ test_must_fail git check-attr -- &&
+ test_must_fail git check-attr test &&
+ test_must_fail git check-attr test -- &&
+ test_must_fail git check-attr -- f &&
+ test_must_fail git check-attr --source &&
+ test_must_fail git check-attr --source not-a-valid-ref &&
+ echo "f" | test_must_fail git check-attr --stdin &&
+ echo "f" | test_must_fail git check-attr --stdin -- f &&
+ echo "f" | test_must_fail git check-attr --stdin test -- f &&
+ test_must_fail git check-attr "" -- f
+'
+
+test_expect_success 'attribute test' '
+
+ attr_check " d " d &&
+ attr_check e e &&
+ attr_check_quote e\" e\\\" e &&
+
+ attr_check f f &&
+ attr_check a/f f &&
+ attr_check a/c/f f &&
+ attr_check a/g a/g &&
+ attr_check a/b/g a/b/g &&
+ attr_check b/g unspecified &&
+ attr_check a/b/h a/b/h &&
+ attr_check a/b/d/g "a/b/d/*" &&
+ attr_check onoff unset &&
+ attr_check offon set &&
+ attr_check no unspecified &&
+ attr_check a/b/d/no "a/b/d/*" &&
+ attr_check a/b/d/yes unspecified
+'
+
+test_expect_success 'attribute matching is case sensitive when core.ignorecase=0' '
+
+ attr_check F unspecified "-c core.ignorecase=0" &&
+ attr_check a/F unspecified "-c core.ignorecase=0" &&
+ attr_check a/c/F unspecified "-c core.ignorecase=0" &&
+ attr_check a/G unspecified "-c core.ignorecase=0" &&
+ attr_check a/B/g a/g "-c core.ignorecase=0" &&
+ attr_check a/b/G unspecified "-c core.ignorecase=0" &&
+ attr_check a/b/H unspecified "-c core.ignorecase=0" &&
+ attr_check a/b/D/g a/g "-c core.ignorecase=0" &&
+ attr_check oNoFf unspecified "-c core.ignorecase=0" &&
+ attr_check oFfOn unspecified "-c core.ignorecase=0" &&
+ attr_check NO unspecified "-c core.ignorecase=0" &&
+ attr_check a/b/D/NO unspecified "-c core.ignorecase=0" &&
+ attr_check a/b/d/YES a/b/d/* "-c core.ignorecase=0" &&
+ attr_check a/E/f f "-c core.ignorecase=0"
+
+'
+
+test_expect_success 'attribute matching is case insensitive when core.ignorecase=1' '
+
+ attr_check F f "-c core.ignorecase=1" &&
+ attr_check a/F f "-c core.ignorecase=1" &&
+ attr_check a/c/F f "-c core.ignorecase=1" &&
+ attr_check a/G a/g "-c core.ignorecase=1" &&
+ attr_check a/B/g a/b/g "-c core.ignorecase=1" &&
+ attr_check a/b/G a/b/g "-c core.ignorecase=1" &&
+ attr_check a/b/H a/b/h "-c core.ignorecase=1" &&
+ attr_check a/b/D/g "a/b/d/*" "-c core.ignorecase=1" &&
+ attr_check oNoFf unset "-c core.ignorecase=1" &&
+ attr_check oFfOn set "-c core.ignorecase=1" &&
+ attr_check NO unspecified "-c core.ignorecase=1" &&
+ attr_check a/b/D/NO "a/b/d/*" "-c core.ignorecase=1" &&
+ attr_check a/b/d/YES unspecified "-c core.ignorecase=1" &&
+ attr_check a/E/f "A/e/F" "-c core.ignorecase=1"
+
+'
+
+test_expect_success CASE_INSENSITIVE_FS 'additional case insensitivity tests' '
+ attr_check a/B/D/g a/g "-c core.ignorecase=0" &&
+ attr_check A/B/D/NO unspecified "-c core.ignorecase=0" &&
+ attr_check A/b/h a/b/h "-c core.ignorecase=1" &&
+ attr_check a/B/D/g "a/b/d/*" "-c core.ignorecase=1" &&
+ attr_check A/B/D/NO "a/b/d/*" "-c core.ignorecase=1"
+'
+
+test_expect_success 'unnormalized paths' '
+ attr_check ./f f &&
+ attr_check ./a/g a/g &&
+ attr_check a/./g a/g &&
+ attr_check a/c/../b/g a/b/g
+'
+
+test_expect_success 'relative paths' '
+ (cd a && attr_check ../f f) &&
+ (cd a && attr_check f f) &&
+ (cd a && attr_check i a/i) &&
+ (cd a && attr_check g a/g) &&
+ (cd a && attr_check b/g a/b/g) &&
+ (cd b && attr_check ../a/f f) &&
+ (cd b && attr_check ../a/g a/g) &&
+ (cd b && attr_check ../a/b/g a/b/g)
+'
+
+test_expect_success 'prefixes are not confused with leading directories' '
+ attr_check a_plus/g unspecified &&
+ cat >expect <<-\EOF &&
+ a/g: test: a/g
+ a_plus/g: test: unspecified
+ EOF
+ git check-attr test a/g a_plus/g >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'core.attributesfile' '
+ attr_check global unspecified &&
+ git config core.attributesfile "$HOME/global-gitattributes" &&
+ attr_check global global &&
+ git config core.attributesfile "~/global-gitattributes" &&
+ attr_check global global &&
+ echo "global test=precedence" >>.gitattributes &&
+ attr_check global precedence
+'
+
+test_expect_success 'attribute test: read paths from stdin' '
+ grep -v notest <expect-all >expect &&
+ sed -e "s/:.*//" <expect | git check-attr --stdin test >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'setup --all option' '
+ grep -v unspecified <expect-all | sort >specified-all &&
+ sed -e "s/:.*//" <expect-all | uniq >stdin-all
+'
+
+test_expect_success 'attribute test: --all option' '
+ git check-attr --stdin --all <stdin-all >tmp &&
+ sort tmp >actual &&
+ test_cmp specified-all actual
+'
+
+test_expect_success 'attribute test: --cached option' '
+ git check-attr --cached --stdin --all <stdin-all >tmp &&
+ sort tmp >actual &&
+ test_must_be_empty actual &&
+ git add .gitattributes a/.gitattributes a/b/.gitattributes &&
+ git check-attr --cached --stdin --all <stdin-all >tmp &&
+ sort tmp >actual &&
+ test_cmp specified-all actual
+'
+
+test_expect_success 'root subdir attribute test' '
+ attr_check a/i a/i &&
+ attr_check subdir/a/i unspecified
+'
+
+test_expect_success 'negative patterns' '
+ echo "!f test=bar" >.gitattributes &&
+ git check-attr test -- '"'"'!f'"'"' 2>errors &&
+ test_grep "Negative patterns are ignored" errors
+'
+
+test_expect_success 'patterns starting with exclamation' '
+ echo "\!f test=foo" >.gitattributes &&
+ attr_check "!f" foo
+'
+
+test_expect_success '"**" test' '
+ echo "**/f foo=bar" >.gitattributes &&
+ cat <<\EOF >expect &&
+f: foo: bar
+a/f: foo: bar
+a/b/f: foo: bar
+a/b/c/f: foo: bar
+EOF
+ git check-attr foo -- "f" >actual 2>err &&
+ git check-attr foo -- "a/f" >>actual 2>>err &&
+ git check-attr foo -- "a/b/f" >>actual 2>>err &&
+ git check-attr foo -- "a/b/c/f" >>actual 2>>err &&
+ test_cmp expect actual &&
+ test_must_be_empty err
+'
+
+test_expect_success '"**" with no slashes test' '
+ echo "a**f foo=bar" >.gitattributes &&
+ git check-attr foo -- "f" >actual &&
+ cat <<\EOF >expect &&
+f: foo: unspecified
+af: foo: bar
+axf: foo: bar
+a/f: foo: unspecified
+a/b/f: foo: unspecified
+a/b/c/f: foo: unspecified
+EOF
+ git check-attr foo -- "f" >actual 2>err &&
+ git check-attr foo -- "af" >>actual 2>err &&
+ git check-attr foo -- "axf" >>actual 2>err &&
+ git check-attr foo -- "a/f" >>actual 2>>err &&
+ git check-attr foo -- "a/b/f" >>actual 2>>err &&
+ git check-attr foo -- "a/b/c/f" >>actual 2>>err &&
+ test_cmp expect actual &&
+ test_must_be_empty err
+'
+
+test_expect_success 'using --git-dir and --work-tree' '
+ mkdir unreal real &&
+ git init real &&
+ echo "file test=in-real" >real/.gitattributes &&
+ (
+ cd unreal &&
+ attr_check file in-real "--git-dir ../real/.git --work-tree ../real"
+ )
+'
+
+test_expect_success 'using --source' '
+ attr_check_source foo/bar/f f tag-1 &&
+ attr_check_source foo/bar/a/i n tag-1 &&
+ attr_check_source foo/bar/f unspecified tag-2 &&
+ attr_check_source foo/bar/a/i m tag-2 &&
+ attr_check_source foo/bar/g g tag-2 &&
+ attr_check_source foo/bar/g unspecified tag-1
+'
+
+test_expect_success 'setup bare' '
+ git clone --template= --bare . bare.git
+'
+
+test_expect_success 'bare repository: check that .gitattribute is ignored' '
+ (
+ cd bare.git &&
+ (
+ echo "f test=f" &&
+ echo "a/i test=a/i"
+ ) >.gitattributes &&
+ attr_check f unspecified &&
+ attr_check a/f unspecified &&
+ attr_check a/c/f unspecified &&
+ attr_check a/i unspecified &&
+ attr_check subdir/a/i unspecified
+ )
+'
+
+bad_attr_source_err="fatal: bad --attr-source or GIT_ATTR_SOURCE"
+
+test_expect_success '--attr-source is bad' '
+ test_when_finished rm -rf empty &&
+ git init empty &&
+ (
+ cd empty &&
+ echo "$bad_attr_source_err" >expect_err &&
+ test_must_fail git --attr-source=HEAD check-attr test -- f/path 2>err &&
+ test_cmp expect_err err
+ )
+'
+
+test_expect_success 'attr.tree when HEAD is unborn' '
+ test_when_finished rm -rf empty &&
+ git init empty &&
+ (
+ cd empty &&
+ echo "f/path: test: unspecified" >expect &&
+ git -c attr.tree=HEAD check-attr test -- f/path >actual 2>err &&
+ test_must_be_empty err &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'bad attr source defaults to reading .gitattributes file' '
+ test_when_finished rm -rf empty &&
+ git init empty &&
+ (
+ cd empty &&
+ echo "f/path test=val" >.gitattributes &&
+ echo "f/path: test: val" >expect &&
+ git -c attr.tree=HEAD check-attr test -- f/path >actual 2>err &&
+ test_must_be_empty err &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'bare repo defaults to reading .gitattributes from HEAD' '
+ test_when_finished rm -rf test bare_with_gitattribute &&
+ git init test &&
+ test_commit -C test gitattributes .gitattributes "f/path test=val" &&
+ git clone --bare test bare_with_gitattribute &&
+ echo "f/path: test: val" >expect &&
+ git -C bare_with_gitattribute check-attr test -- f/path >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'precedence of --attr-source, GIT_ATTR_SOURCE, then attr.tree' '
+ test_when_finished rm -rf empty &&
+ git init empty &&
+ (
+ cd empty &&
+ git checkout -b attr-source &&
+ test_commit "val1" .gitattributes "f/path test=val1" &&
+ git checkout -b attr-tree &&
+ test_commit "val2" .gitattributes "f/path test=val2" &&
+ git checkout attr-source &&
+ echo "f/path: test: val1" >expect &&
+ GIT_ATTR_SOURCE=attr-source git -c attr.tree=attr-tree --attr-source=attr-source \
+ check-attr test -- f/path >actual &&
+ test_cmp expect actual &&
+ GIT_ATTR_SOURCE=attr-source git -c attr.tree=attr-tree \
+ check-attr test -- f/path >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'bare repository: with --source' '
+ (
+ cd bare.git &&
+ attr_check_source foo/bar/f f tag-1 &&
+ attr_check_source foo/bar/a/i n tag-1 &&
+ attr_check_source foo/bar/f unspecified tag-2 &&
+ attr_check_source foo/bar/a/i m tag-2 &&
+ attr_check_source foo/bar/g g tag-2 &&
+ attr_check_source foo/bar/g unspecified tag-1
+ )
+'
+
+test_expect_success 'bare repository: check that --cached honors index' '
+ (
+ cd bare.git &&
+ GIT_INDEX_FILE=../.git/index \
+ git check-attr --cached --stdin --all <../stdin-all |
+ sort >actual &&
+ test_cmp ../specified-all actual
+ )
+'
+
+test_expect_success 'bare repository: test info/attributes' '
+ (
+ cd bare.git &&
+ mkdir info &&
+ (
+ echo "f test=f" &&
+ echo "a/i test=a/i"
+ ) >info/attributes &&
+ attr_check f f &&
+ attr_check a/f f &&
+ attr_check a/c/f f &&
+ attr_check a/i a/i &&
+ attr_check subdir/a/i unspecified
+ )
+'
+
+test_expect_success 'binary macro expanded by -a' '
+ echo "file binary" >.gitattributes &&
+ cat >expect <<-\EOF &&
+ file: binary: set
+ file: diff: unset
+ file: merge: unset
+ file: text: unset
+ EOF
+ git check-attr -a file >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'query binary macro directly' '
+ echo "file binary" >.gitattributes &&
+ echo file: binary: set >expect &&
+ git check-attr binary file >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success SYMLINKS 'set up symlink tests' '
+ echo "* test" >attr &&
+ rm -f .gitattributes
+'
+
+test_expect_success SYMLINKS 'symlinks respected in core.attributesFile' '
+ test_when_finished "rm symlink" &&
+ ln -s attr symlink &&
+ test_config core.attributesFile "$(pwd)/symlink" &&
+ attr_check file set
+'
+
+test_expect_success SYMLINKS 'symlinks respected in info/attributes' '
+ test_when_finished "rm .git/info/attributes" &&
+ mkdir .git/info &&
+ ln -s ../../attr .git/info/attributes &&
+ attr_check file set
+'
+
+test_expect_success SYMLINKS 'symlinks not respected in-tree' '
+ test_when_finished "rm -rf .gitattributes subdir" &&
+ ln -s attr .gitattributes &&
+ mkdir subdir &&
+ ln -s ../attr subdir/.gitattributes &&
+ attr_check_basic subdir/file unspecified &&
+ test_grep "unable to access.*gitattributes" err
+'
+
+test_expect_success 'large attributes line ignored in tree' '
+ test_when_finished "rm .gitattributes" &&
+ printf "path %02043d" 1 >.gitattributes &&
+ git check-attr --all path >actual 2>err &&
+ echo "warning: ignoring overly long attributes line 1" >expect &&
+ test_cmp expect err &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'large attributes line ignores trailing content in tree' '
+ test_when_finished "rm .gitattributes" &&
+ # older versions of Git broke lines at 2048 bytes; the 2045 bytes
+ # of 0-padding here is accounting for the three bytes of "a 1", which
+ # would knock "trailing" to the "next" line, where it would be
+ # erroneously parsed.
+ printf "a %02045dtrailing attribute\n" 1 >.gitattributes &&
+ git check-attr --all trailing >actual 2>err &&
+ echo "warning: ignoring overly long attributes line 1" >expect &&
+ test_cmp expect err &&
+ test_must_be_empty actual
+'
+
+test_expect_success EXPENSIVE 'large attributes file ignored in tree' '
+ test_when_finished "rm .gitattributes" &&
+ dd if=/dev/zero of=.gitattributes bs=1048576 count=101 2>/dev/null &&
+ git check-attr --all path >/dev/null 2>err &&
+ echo "warning: ignoring overly large gitattributes file ${SQ}.gitattributes${SQ}" >expect &&
+ test_cmp expect err
+'
+
+test_expect_success 'large attributes line ignored in index' '
+ test_when_finished "git update-index --remove .gitattributes" &&
+ blob=$(printf "path %02043d" 1 | git hash-object -w --stdin) &&
+ git update-index --add --cacheinfo 100644,$blob,.gitattributes &&
+ git check-attr --cached --all path >actual 2>err &&
+ echo "warning: ignoring overly long attributes line 1" >expect &&
+ test_cmp expect err &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'large attributes line ignores trailing content in index' '
+ test_when_finished "git update-index --remove .gitattributes" &&
+ blob=$(printf "a %02045dtrailing attribute\n" 1 | git hash-object -w --stdin) &&
+ git update-index --add --cacheinfo 100644,$blob,.gitattributes &&
+ git check-attr --cached --all trailing >actual 2>err &&
+ echo "warning: ignoring overly long attributes line 1" >expect &&
+ test_cmp expect err &&
+ test_must_be_empty actual
+'
+
+test_expect_success EXPENSIVE 'large attributes file ignored in index' '
+ test_when_finished "git update-index --remove .gitattributes" &&
+ blob=$(dd if=/dev/zero bs=1048576 count=101 2>/dev/null | git hash-object -w --stdin) &&
+ git update-index --add --cacheinfo 100644,$blob,.gitattributes &&
+ git check-attr --cached --all path >/dev/null 2>err &&
+ echo "warning: ignoring overly large gitattributes blob ${SQ}.gitattributes${SQ}" >expect &&
+ test_cmp expect err
+'
+
+test_done
diff --git a/t/t0004-unwritable.sh b/t/t0004-unwritable.sh
new file mode 100755
index 0000000..8114fac
--- /dev/null
+++ b/t/t0004-unwritable.sh
@@ -0,0 +1,84 @@
+#!/bin/sh
+
+test_description='detect unwritable repository and fail correctly'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+
+ >file &&
+ git add file &&
+ test_tick &&
+ git commit -m initial &&
+ echo >file &&
+ git add file
+
+'
+
+test_expect_success POSIXPERM,SANITY 'write-tree should notice unwritable repository' '
+ test_when_finished "chmod 775 .git/objects .git/objects/??" &&
+ chmod a-w .git/objects .git/objects/?? &&
+ test_must_fail git write-tree 2>out.write-tree
+'
+
+test_lazy_prereq WRITE_TREE_OUT 'test -e "$TRASH_DIRECTORY"/out.write-tree'
+test_expect_success WRITE_TREE_OUT 'write-tree output on unwritable repository' '
+ cat >expect <<-\EOF &&
+ error: insufficient permission for adding an object to repository database .git/objects
+ fatal: git-write-tree: error building trees
+ EOF
+ test_cmp expect out.write-tree
+'
+
+test_expect_success POSIXPERM,SANITY 'commit should notice unwritable repository' '
+ test_when_finished "chmod 775 .git/objects .git/objects/??" &&
+ chmod a-w .git/objects .git/objects/?? &&
+ test_must_fail git commit -m second 2>out.commit
+'
+
+test_lazy_prereq COMMIT_OUT 'test -e "$TRASH_DIRECTORY"/out.commit'
+test_expect_success COMMIT_OUT 'commit output on unwritable repository' '
+ cat >expect <<-\EOF &&
+ error: insufficient permission for adding an object to repository database .git/objects
+ error: Error building trees
+ EOF
+ test_cmp expect out.commit
+'
+
+test_expect_success POSIXPERM,SANITY 'update-index should notice unwritable repository' '
+ test_when_finished "chmod 775 .git/objects .git/objects/??" &&
+ echo 6O >file &&
+ chmod a-w .git/objects .git/objects/?? &&
+ test_must_fail git update-index file 2>out.update-index
+'
+
+test_lazy_prereq UPDATE_INDEX_OUT 'test -e "$TRASH_DIRECTORY"/out.update-index'
+test_expect_success UPDATE_INDEX_OUT 'update-index output on unwritable repository' '
+ cat >expect <<-\EOF &&
+ error: insufficient permission for adding an object to repository database .git/objects
+ error: file: failed to insert into database
+ fatal: Unable to process path file
+ EOF
+ test_cmp expect out.update-index
+'
+
+test_expect_success POSIXPERM,SANITY 'add should notice unwritable repository' '
+ test_when_finished "chmod 775 .git/objects .git/objects/??" &&
+ echo b >file &&
+ chmod a-w .git/objects .git/objects/?? &&
+ test_must_fail git add file 2>out.add
+'
+
+test_lazy_prereq ADD_OUT 'test -e "$TRASH_DIRECTORY"/out.add'
+test_expect_success ADD_OUT 'add output on unwritable repository' '
+ cat >expect <<-\EOF &&
+ error: insufficient permission for adding an object to repository database .git/objects
+ error: file: failed to insert into database
+ error: unable to index file '\''file'\''
+ fatal: updating files failed
+ EOF
+ test_cmp expect out.add
+'
+
+test_done
diff --git a/t/t0005-signals.sh b/t/t0005-signals.sh
new file mode 100755
index 0000000..eba75a2
--- /dev/null
+++ b/t/t0005-signals.sh
@@ -0,0 +1,55 @@
+#!/bin/sh
+
+test_description='signals work as we expect'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+cat >expect <<EOF
+three
+two
+one
+EOF
+
+test_expect_success 'sigchain works' '
+ { test-tool sigchain >actual; ret=$?; } &&
+ {
+ # Signal death by raise() on Windows acts like exit(3),
+ # regardless of the signal number. So we must allow that
+ # as well as the normal signal check.
+ test_match_signal 15 "$ret" ||
+ test "$ret" = 3
+ } &&
+ test_cmp expect actual
+'
+
+test_expect_success !MINGW 'signals are propagated using shell convention' '
+ # we use exec here to avoid any sub-shell interpretation
+ # of the exit code
+ git config alias.sigterm "!exec test-tool sigchain" &&
+ test_expect_code 143 git sigterm
+'
+
+large_git () {
+ for i in $(test_seq 1 100)
+ do
+ git diff --cached --binary || return
+ done
+}
+
+test_expect_success 'create blob' '
+ test-tool genrandom foo 16384 >file &&
+ git add file
+'
+
+test_expect_success !MINGW 'a constipated git dies with SIGPIPE' '
+ OUT=$( ((large_git; echo $? 1>&3) | :) 3>&1 ) &&
+ test_match_signal 13 "$OUT"
+'
+
+test_expect_success !MINGW 'a constipated git dies with SIGPIPE even if parent ignores it' '
+ OUT=$( ((trap "" PIPE && large_git; echo $? 1>&3) | :) 3>&1 ) &&
+ test_match_signal 13 "$OUT"
+'
+
+test_done
diff --git a/t/t0006-date.sh b/t/t0006-date.sh
new file mode 100755
index 0000000..e18b160
--- /dev/null
+++ b/t/t0006-date.sh
@@ -0,0 +1,169 @@
+#!/bin/sh
+
+test_description='test date parsing and printing'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# arbitrary reference time: 2009-08-30 19:20:00
+GIT_TEST_DATE_NOW=1251660000; export GIT_TEST_DATE_NOW
+
+check_relative() {
+ t=$(($GIT_TEST_DATE_NOW - $1))
+ echo "$t -> $2" >expect
+ test_expect_${3:-success} "relative date ($2)" "
+ test-tool date relative $t >actual &&
+ test_cmp expect actual
+ "
+}
+
+check_relative 5 '5 seconds ago'
+check_relative 300 '5 minutes ago'
+check_relative 18000 '5 hours ago'
+check_relative 432000 '5 days ago'
+check_relative 1728000 '3 weeks ago'
+check_relative 13000000 '5 months ago'
+check_relative 37500000 '1 year, 2 months ago'
+check_relative 55188000 '1 year, 9 months ago'
+check_relative 630000000 '20 years ago'
+check_relative 31449600 '12 months ago'
+check_relative 62985600 '2 years ago'
+
+check_show () {
+ format=$1
+ time=$2
+ expect=$3
+ prereqs=$4
+ zone=$5
+ test_expect_success $prereqs "show date ($format:$time)" '
+ echo "$time -> $expect" >expect &&
+ TZ=${zone:-$TZ} test-tool date show:"$format" "$time" >actual &&
+ test_cmp expect actual
+ '
+}
+
+# arbitrary but sensible time for examples
+TIME='1466000000 +0200'
+check_show iso8601 "$TIME" '2016-06-15 16:13:20 +0200'
+check_show iso8601-strict "$TIME" '2016-06-15T16:13:20+02:00'
+check_show rfc2822 "$TIME" 'Wed, 15 Jun 2016 16:13:20 +0200'
+check_show short "$TIME" '2016-06-15'
+check_show default "$TIME" 'Wed Jun 15 16:13:20 2016 +0200'
+check_show raw "$TIME" '1466000000 +0200'
+check_show unix "$TIME" '1466000000'
+check_show iso-local "$TIME" '2016-06-15 14:13:20 +0000'
+check_show raw-local "$TIME" '1466000000 +0000'
+check_show unix-local "$TIME" '1466000000'
+
+check_show 'format:%z' "$TIME" '+0200'
+check_show 'format-local:%z' "$TIME" '+0000'
+check_show 'format:%Z' "$TIME" ''
+check_show 'format-local:%Z' "$TIME" 'UTC'
+check_show 'format:%%z' "$TIME" '%z'
+check_show 'format-local:%%z' "$TIME" '%z'
+
+check_show 'format:%Y-%m-%d %H:%M:%S' "$TIME" '2016-06-15 16:13:20'
+check_show 'format-local:%Y-%m-%d %H:%M:%S' "$TIME" '2016-06-15 09:13:20' '' EST5
+
+check_show 'format:%s' '123456789 +1234' 123456789
+check_show 'format:%s' '123456789 -1234' 123456789
+check_show 'format-local:%s' '123456789 -1234' 123456789
+
+# arbitrary time absurdly far in the future
+FUTURE="5758122296 -0400"
+check_show iso "$FUTURE" "2152-06-19 18:24:56 -0400" TIME_IS_64BIT,TIME_T_IS_64BIT
+check_show iso-local "$FUTURE" "2152-06-19 22:24:56 +0000" TIME_IS_64BIT,TIME_T_IS_64BIT
+
+check_parse() {
+ echo "$1 -> $2" >expect
+ test_expect_${4:-success} "parse date ($1${3:+ TZ=$3})" "
+ TZ=${3:-$TZ} test-tool date parse '$1' >actual &&
+ test_cmp expect actual
+ "
+}
+
+check_parse 2008 bad
+check_parse 2008-02 bad
+check_parse 2008-02-14 bad
+check_parse '2008-02-14 20:30:45' '2008-02-14 20:30:45 +0000'
+check_parse '2008-02-14 20:30:45 -0500' '2008-02-14 20:30:45 -0500'
+check_parse '2008.02.14 20:30:45 -0500' '2008-02-14 20:30:45 -0500'
+check_parse '20080214T20:30:45' '2008-02-14 20:30:45 +0000'
+check_parse '20080214T20:30' '2008-02-14 20:30:00 +0000'
+check_parse '20080214T20' '2008-02-14 20:00:00 +0000'
+check_parse '20080214T203045' '2008-02-14 20:30:45 +0000'
+check_parse '20080214T2030' '2008-02-14 20:30:00 +0000'
+check_parse '20080214T000000.20' '2008-02-14 00:00:00 +0000'
+check_parse '20080214T00:00:00.20' '2008-02-14 00:00:00 +0000'
+check_parse '20080214T203045-04:00' '2008-02-14 20:30:45 -0400'
+check_parse '20080214T203045 -04:00' '2008-02-14 20:30:45 -0400'
+check_parse '20080214T203045.019-04:00' '2008-02-14 20:30:45 -0400'
+check_parse '2008-02-14 20:30:45.019-04:00' '2008-02-14 20:30:45 -0400'
+check_parse '2008-02-14 20:30:45 -0015' '2008-02-14 20:30:45 -0015'
+check_parse '2008-02-14 20:30:45 -5' '2008-02-14 20:30:45 +0000'
+check_parse '2008-02-14 20:30:45 -5:' '2008-02-14 20:30:45 +0000'
+check_parse '2008-02-14 20:30:45 -05' '2008-02-14 20:30:45 -0500'
+check_parse '2008-02-14 20:30:45 -:30' '2008-02-14 20:30:45 +0000'
+check_parse '2008-02-14 20:30:45 -05:00' '2008-02-14 20:30:45 -0500'
+check_parse '2008-02-14 20:30:45' '2008-02-14 20:30:45 -0500' EST5
+check_parse 'Thu, 7 Apr 2005 15:14:13 -0700' '2005-04-07 15:14:13 -0700'
+
+check_approxidate() {
+ echo "$1 -> $2 +0000" >expect
+ test_expect_${3:-success} "parse approxidate ($1)" "
+ test-tool date approxidate '$1' >actual &&
+ test_cmp expect actual
+ "
+}
+
+check_approxidate now '2009-08-30 19:20:00'
+check_approxidate '5 seconds ago' '2009-08-30 19:19:55'
+check_approxidate 5.seconds.ago '2009-08-30 19:19:55'
+check_approxidate 10.minutes.ago '2009-08-30 19:10:00'
+check_approxidate yesterday '2009-08-29 19:20:00'
+check_approxidate 3.days.ago '2009-08-27 19:20:00'
+check_approxidate '12:34:56.3.days.ago' '2009-08-27 12:34:56'
+check_approxidate 3.weeks.ago '2009-08-09 19:20:00'
+check_approxidate 3.months.ago '2009-05-30 19:20:00'
+check_approxidate 2.years.3.months.ago '2007-05-30 19:20:00'
+
+check_approxidate '6am yesterday' '2009-08-29 06:00:00'
+check_approxidate '6pm yesterday' '2009-08-29 18:00:00'
+check_approxidate '3:00' '2009-08-30 03:00:00'
+check_approxidate '15:00' '2009-08-30 15:00:00'
+check_approxidate 'noon today' '2009-08-30 12:00:00'
+check_approxidate 'noon yesterday' '2009-08-29 12:00:00'
+check_approxidate 'January 5th noon pm' '2009-01-05 12:00:00'
+check_approxidate '10am noon' '2009-08-29 12:00:00'
+
+check_approxidate 'last tuesday' '2009-08-25 19:20:00'
+check_approxidate 'July 5th' '2009-07-05 19:20:00'
+check_approxidate '06/05/2009' '2009-06-05 19:20:00'
+check_approxidate '06.05.2009' '2009-05-06 19:20:00'
+
+check_approxidate 'Jun 6, 5AM' '2009-06-06 05:00:00'
+check_approxidate '5AM Jun 6' '2009-06-06 05:00:00'
+check_approxidate '6AM, June 7, 2009' '2009-06-07 06:00:00'
+
+check_approxidate '2008-12-01' '2008-12-01 19:20:00'
+check_approxidate '2009-12-01' '2009-12-01 19:20:00'
+
+check_date_format_human() {
+ t=$(($GIT_TEST_DATE_NOW - $1))
+ echo "$t -> $2" >expect
+ test_expect_success "human date $t" '
+ test-tool date human $t >actual &&
+ test_cmp expect actual
+'
+}
+
+check_date_format_human 18000 "5 hours ago" # 5 hours ago
+check_date_format_human 432000 "Tue Aug 25 19:20" # 5 days ago
+check_date_format_human 1728000 "Mon Aug 10 19:20" # 3 weeks ago
+check_date_format_human 13000000 "Thu Apr 2 08:13" # 5 months ago
+check_date_format_human 31449600 "Aug 31 2008" # 12 months ago
+check_date_format_human 37500000 "Jun 22 2008" # 1 year, 2 months ago
+check_date_format_human 55188000 "Dec 1 2007" # 1 year, 9 months ago
+check_date_format_human 630000000 "Sep 13 1989" # 20 years ago
+
+test_done
diff --git a/t/t0007-git-var.sh b/t/t0007-git-var.sh
new file mode 100755
index 0000000..ff4fd93
--- /dev/null
+++ b/t/t0007-git-var.sh
@@ -0,0 +1,280 @@
+#!/bin/sh
+
+test_description='basic sanity checks for git var'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+sane_unset_all_editors () {
+ sane_unset GIT_EDITOR &&
+ sane_unset VISUAL &&
+ sane_unset EDITOR
+}
+
+test_expect_success 'get GIT_AUTHOR_IDENT' '
+ test_tick &&
+ echo "$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE" >expect &&
+ git var GIT_AUTHOR_IDENT >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'get GIT_COMMITTER_IDENT' '
+ test_tick &&
+ echo "$GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE" >expect &&
+ git var GIT_COMMITTER_IDENT >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success !FAIL_PREREQS,!AUTOIDENT 'requested identities are strict' '
+ (
+ sane_unset GIT_COMMITTER_NAME &&
+ sane_unset GIT_COMMITTER_EMAIL &&
+ test_must_fail git var GIT_COMMITTER_IDENT
+ )
+'
+
+test_expect_success 'get GIT_DEFAULT_BRANCH without configuration' '
+ (
+ sane_unset GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME &&
+ git init defbranch &&
+ git -C defbranch symbolic-ref --short HEAD >expect &&
+ git var GIT_DEFAULT_BRANCH >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'get GIT_DEFAULT_BRANCH with configuration' '
+ test_config init.defaultbranch foo &&
+ (
+ sane_unset GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME &&
+ echo foo >expect &&
+ git var GIT_DEFAULT_BRANCH >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'get GIT_EDITOR without configuration' '
+ (
+ sane_unset_all_editors &&
+ test_expect_code 1 git var GIT_EDITOR >out &&
+ test_must_be_empty out
+ )
+'
+
+test_expect_success 'get GIT_EDITOR with configuration' '
+ test_config core.editor foo &&
+ (
+ sane_unset_all_editors &&
+ echo foo >expect &&
+ git var GIT_EDITOR >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'get GIT_EDITOR with environment variable GIT_EDITOR' '
+ (
+ sane_unset_all_editors &&
+ echo bar >expect &&
+ GIT_EDITOR=bar git var GIT_EDITOR >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'get GIT_EDITOR with environment variable EDITOR' '
+ (
+ sane_unset_all_editors &&
+ echo bar >expect &&
+ EDITOR=bar git var GIT_EDITOR >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'get GIT_EDITOR with configuration and environment variable GIT_EDITOR' '
+ test_config core.editor foo &&
+ (
+ sane_unset_all_editors &&
+ echo bar >expect &&
+ GIT_EDITOR=bar git var GIT_EDITOR >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'get GIT_EDITOR with configuration and environment variable EDITOR' '
+ test_config core.editor foo &&
+ (
+ sane_unset_all_editors &&
+ echo foo >expect &&
+ EDITOR=bar git var GIT_EDITOR >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'get GIT_SEQUENCE_EDITOR without configuration' '
+ (
+ sane_unset GIT_SEQUENCE_EDITOR &&
+ git var GIT_EDITOR >expect &&
+ git var GIT_SEQUENCE_EDITOR >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'get GIT_SEQUENCE_EDITOR with configuration' '
+ test_config sequence.editor foo &&
+ (
+ sane_unset GIT_SEQUENCE_EDITOR &&
+ echo foo >expect &&
+ git var GIT_SEQUENCE_EDITOR >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'get GIT_SEQUENCE_EDITOR with environment variable' '
+ (
+ sane_unset GIT_SEQUENCE_EDITOR &&
+ echo bar >expect &&
+ GIT_SEQUENCE_EDITOR=bar git var GIT_SEQUENCE_EDITOR >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'get GIT_SEQUENCE_EDITOR with configuration and environment variable' '
+ test_config sequence.editor foo &&
+ (
+ sane_unset GIT_SEQUENCE_EDITOR &&
+ echo bar >expect &&
+ GIT_SEQUENCE_EDITOR=bar git var GIT_SEQUENCE_EDITOR >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success POSIXPERM 'GIT_SHELL_PATH points to a valid executable' '
+ shellpath=$(git var GIT_SHELL_PATH) &&
+ test_path_is_executable "$shellpath"
+'
+
+# We know in this environment that our shell will be one of a few fixed values
+# that all end in "sh".
+test_expect_success MINGW 'GIT_SHELL_PATH points to a suitable shell' '
+ shellpath=$(git var GIT_SHELL_PATH) &&
+ case "$shellpath" in
+ *sh) ;;
+ *) return 1;;
+ esac
+'
+
+test_expect_success 'GIT_ATTR_SYSTEM produces expected output' '
+ test_must_fail env GIT_ATTR_NOSYSTEM=1 git var GIT_ATTR_SYSTEM &&
+ (
+ sane_unset GIT_ATTR_NOSYSTEM &&
+ systempath=$(git var GIT_ATTR_SYSTEM) &&
+ test "$systempath" != ""
+ )
+'
+
+test_expect_success 'GIT_ATTR_GLOBAL points to the correct location' '
+ TRASHDIR="$(test-tool path-utils normalize_path_copy "$(pwd)")" &&
+ globalpath=$(XDG_CONFIG_HOME="$TRASHDIR/.config" git var GIT_ATTR_GLOBAL) &&
+ test "$globalpath" = "$TRASHDIR/.config/git/attributes" &&
+ (
+ sane_unset XDG_CONFIG_HOME &&
+ globalpath=$(HOME="$TRASHDIR" git var GIT_ATTR_GLOBAL) &&
+ test "$globalpath" = "$TRASHDIR/.config/git/attributes"
+ )
+'
+
+test_expect_success 'GIT_CONFIG_SYSTEM points to the correct location' '
+ TRASHDIR="$(test-tool path-utils normalize_path_copy "$(pwd)")" &&
+ test_must_fail env GIT_CONFIG_NOSYSTEM=1 git var GIT_CONFIG_SYSTEM &&
+ (
+ sane_unset GIT_CONFIG_NOSYSTEM &&
+ systempath=$(git var GIT_CONFIG_SYSTEM) &&
+ test "$systempath" != "" &&
+ systempath=$(GIT_CONFIG_SYSTEM=/dev/null git var GIT_CONFIG_SYSTEM) &&
+ if test_have_prereq MINGW
+ then
+ test "$systempath" = "nul"
+ else
+ test "$systempath" = "/dev/null"
+ fi &&
+ systempath=$(GIT_CONFIG_SYSTEM="$TRASHDIR/gitconfig" git var GIT_CONFIG_SYSTEM) &&
+ test "$systempath" = "$TRASHDIR/gitconfig"
+ )
+'
+
+test_expect_success 'GIT_CONFIG_GLOBAL points to the correct location' '
+ TRASHDIR="$(test-tool path-utils normalize_path_copy "$(pwd)")" &&
+ HOME="$TRASHDIR" XDG_CONFIG_HOME="$TRASHDIR/foo" git var GIT_CONFIG_GLOBAL >actual &&
+ echo "$TRASHDIR/foo/git/config" >expected &&
+ echo "$TRASHDIR/.gitconfig" >>expected &&
+ test_cmp expected actual &&
+ (
+ sane_unset XDG_CONFIG_HOME &&
+ HOME="$TRASHDIR" git var GIT_CONFIG_GLOBAL >actual &&
+ echo "$TRASHDIR/.config/git/config" >expected &&
+ echo "$TRASHDIR/.gitconfig" >>expected &&
+ test_cmp expected actual &&
+ globalpath=$(GIT_CONFIG_GLOBAL=/dev/null git var GIT_CONFIG_GLOBAL) &&
+ if test_have_prereq MINGW
+ then
+ test "$globalpath" = "nul"
+ else
+ test "$globalpath" = "/dev/null"
+ fi &&
+ globalpath=$(GIT_CONFIG_GLOBAL="$TRASHDIR/gitconfig" git var GIT_CONFIG_GLOBAL) &&
+ test "$globalpath" = "$TRASHDIR/gitconfig"
+ )
+'
+
+# For git var -l, we check only a representative variable;
+# testing the whole output would make our test too brittle with
+# respect to unrelated changes in the test suite's environment.
+test_expect_success 'git var -l lists variables' '
+ git var -l >actual &&
+ echo "$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE" >expect &&
+ sed -n s/GIT_AUTHOR_IDENT=//p <actual >actual.author &&
+ test_cmp expect actual.author
+'
+
+test_expect_success 'git var -l lists config' '
+ git var -l >actual &&
+ echo false >expect &&
+ sed -n s/core\\.bare=//p <actual >actual.bare &&
+ test_cmp expect actual.bare
+'
+
+test_expect_success 'git var -l lists multiple global configs' '
+ TRASHDIR="$(test-tool path-utils normalize_path_copy "$(pwd)")" &&
+ HOME="$TRASHDIR" XDG_CONFIG_HOME="$TRASHDIR/foo" git var -l >actual &&
+ grep "^GIT_CONFIG_GLOBAL=" actual >filtered &&
+ echo "GIT_CONFIG_GLOBAL=$TRASHDIR/foo/git/config" >expected &&
+ echo "GIT_CONFIG_GLOBAL=$TRASHDIR/.gitconfig" >>expected &&
+ test_cmp expected filtered
+'
+
+test_expect_success 'git var -l does not split multiline editors' '
+ (
+ GIT_EDITOR="!f() {
+ echo Hello!
+ }; f" &&
+ export GIT_EDITOR &&
+ echo "GIT_EDITOR=$GIT_EDITOR" >expected &&
+ git var -l >var &&
+ sed -n -e "/^GIT_EDITOR/,\$p" var | head -n 3 >actual &&
+ test_cmp expected actual
+ )
+'
+
+test_expect_success 'listing and asking for variables are exclusive' '
+ test_must_fail git var -l GIT_COMMITTER_IDENT
+'
+
+test_expect_success '`git var -l` works even without HOME' '
+ (
+ XDG_CONFIG_HOME= &&
+ export XDG_CONFIG_HOME &&
+ unset HOME &&
+ git var -l
+ )
+'
+
+test_done
diff --git a/t/t0008-ignores.sh b/t/t0008-ignores.sh
new file mode 100755
index 0000000..361446b
--- /dev/null
+++ b/t/t0008-ignores.sh
@@ -0,0 +1,948 @@
+#!/bin/sh
+
+test_description=check-ignore
+
+TEST_PASSES_SANITIZE_LEAK=true
+TEST_CREATE_REPO_NO_TEMPLATE=1
+. ./test-lib.sh
+
+init_vars () {
+ global_excludes="global-excludes"
+}
+
+enable_global_excludes () {
+ init_vars &&
+ git config core.excludesfile "$global_excludes"
+}
+
+expect_in () {
+ dest="$HOME/expected-$1" text="$2"
+ if test -z "$text"
+ then
+ >"$dest" # avoid newline
+ else
+ echo "$text" >"$dest"
+ fi
+}
+
+expect () {
+ expect_in stdout "$1"
+}
+
+expect_from_stdin () {
+ cat >"$HOME/expected-stdout"
+}
+
+test_stderr () {
+ expected="$1"
+ expect_in stderr "$1" &&
+ test_cmp "$HOME/expected-stderr" "$HOME/stderr"
+}
+
+broken_c_unquote () {
+ "$PERL_PATH" -pe 's/^"//; s/\\//; s/"$//; tr/\n/\0/' "$@"
+}
+
+broken_c_unquote_verbose () {
+ "$PERL_PATH" -pe 's/ "/ /; s/\\//; s/"$//; tr/:\t\n/\0/' "$@"
+}
+
+stderr_contains () {
+ regexp="$1"
+ if test_grep "$regexp" "$HOME/stderr"
+ then
+ return 0
+ else
+ echo "didn't find /$regexp/ in $HOME/stderr"
+ cat "$HOME/stderr"
+ return 1
+ fi
+}
+
+stderr_empty_on_success () {
+ expect_code="$1"
+ if test $expect_code = 0
+ then
+ test_stderr ""
+ else
+ # If we expect failure then stderr might or might not be empty
+ # due to --quiet - the caller can check its contents
+ return 0
+ fi
+}
+
+test_check_ignore () {
+ args="$1" expect_code="${2:-0}" global_args="$3"
+
+ init_vars &&
+ rm -f "$HOME/stdout" "$HOME/stderr" "$HOME/cmd" &&
+ echo git $global_args check-ignore $quiet_opt $verbose_opt $non_matching_opt $no_index_opt $args \
+ >"$HOME/cmd" &&
+ echo "$expect_code" >"$HOME/expected-exit-code" &&
+ test_expect_code "$expect_code" \
+ git $global_args check-ignore $quiet_opt $verbose_opt $non_matching_opt $no_index_opt $args \
+ >"$HOME/stdout" 2>"$HOME/stderr" &&
+ test_cmp "$HOME/expected-stdout" "$HOME/stdout" &&
+ stderr_empty_on_success "$expect_code"
+}
+
+# Runs the same code with 4 different levels of output verbosity:
+#
+# 1. with -q / --quiet
+# 2. with default verbosity
+# 3. with -v / --verbose
+# 4. with -v / --verbose, *and* -n / --non-matching
+#
+# expecting success each time. Takes advantage of the fact that
+# check-ignore --verbose output is the same as normal output except
+# for the extra first column.
+#
+# A parameter is used to determine if the tests are run with the
+# normal case (using the index), or with the --no-index option.
+#
+# Arguments:
+# - (optional) prereqs for this test, e.g. 'SYMLINKS'
+# - test name
+# - output to expect from the fourth verbosity mode (the output
+# from the other verbosity modes is automatically inferred
+# from this value)
+# - code to run (should invoke test_check_ignore)
+# - index option: --index or --no-index
+test_expect_success_multiple () {
+ prereq=
+ if test $# -eq 5
+ then
+ prereq=$1
+ shift
+ fi
+ if test "$4" = "--index"
+ then
+ no_index_opt=
+ else
+ no_index_opt=$4
+ fi
+ testname="$1" expect_all="$2" code="$3"
+
+ expect_verbose=$( echo "$expect_all" | grep -v '^:: ' )
+ expect=$( echo "$expect_verbose" | sed -e 's/.* //' )
+
+ test_expect_success $prereq "$testname${no_index_opt:+ with $no_index_opt}" '
+ expect "$expect" &&
+ eval "$code"
+ '
+
+ # --quiet is only valid when a single pattern is passed
+ if test $( echo "$expect_all" | wc -l ) = 1
+ then
+ for quiet_opt in '-q' '--quiet'
+ do
+ opts="${no_index_opt:+$no_index_opt }$quiet_opt"
+ test_expect_success $prereq "$testname${opts:+ with $opts}" "
+ expect '' &&
+ $code
+ "
+ done
+ quiet_opt=
+ fi
+
+ for verbose_opt in '-v' '--verbose'
+ do
+ for non_matching_opt in '' '-n' '--non-matching'
+ do
+ if test -n "$non_matching_opt"
+ then
+ my_expect="$expect_all"
+ else
+ my_expect="$expect_verbose"
+ fi
+
+ test_code="
+ expect '$my_expect' &&
+ $code
+ "
+ opts="${no_index_opt:+$no_index_opt }$verbose_opt${non_matching_opt:+ $non_matching_opt}"
+ test_expect_success $prereq "$testname${opts:+ with $opts}" "$test_code"
+ done
+ done
+ verbose_opt=
+ non_matching_opt=
+ no_index_opt=
+}
+
+test_expect_success_multi () {
+ test_expect_success_multiple "$@" "--index"
+}
+
+test_expect_success_no_index_multi () {
+ test_expect_success_multiple "$@" "--no-index"
+}
+
+test_expect_success 'setup' '
+ init_vars &&
+ mkdir -p a/b/ignored-dir a/submodule b &&
+ if test_have_prereq SYMLINKS
+ then
+ ln -s b a/symlink
+ fi &&
+ (
+ cd a/submodule &&
+ git init &&
+ echo a >a &&
+ git add a &&
+ git commit -m"commit in submodule"
+ ) &&
+ git add a/submodule &&
+ cat <<-\EOF >.gitignore &&
+ one
+ ignored-*
+ top-level-dir/
+ EOF
+ for dir in . a
+ do
+ : >$dir/not-ignored &&
+ : >$dir/ignored-and-untracked &&
+ : >$dir/ignored-but-in-index || return 1
+ done &&
+ git add -f ignored-but-in-index a/ignored-but-in-index &&
+ cat <<-\EOF >a/.gitignore &&
+ two*
+ *three
+ EOF
+ cat <<-\EOF >a/b/.gitignore &&
+ four
+ five
+ # this comment should affect the line numbers
+ six
+ ignored-dir/
+ # and so should this blank line:
+
+ !on*
+ !two
+ EOF
+ echo "seven" >a/b/ignored-dir/.gitignore &&
+ test -n "$HOME" &&
+ cat <<-\EOF >"$global_excludes" &&
+ globalone
+ !globaltwo
+ globalthree
+ EOF
+ mkdir .git/info &&
+ cat <<-\EOF >.git/info/exclude
+ per-repo
+ EOF
+'
+
+############################################################################
+#
+# test invalid inputs
+
+test_expect_success_multi '. corner-case' ':: .' '
+ test_check_ignore . 1
+'
+
+test_expect_success_multi 'empty command line' '' '
+ test_check_ignore "" 128 &&
+ stderr_contains "fatal: no path specified"
+'
+
+test_expect_success_multi '--stdin with empty STDIN' '' '
+ test_check_ignore "--stdin" 1 </dev/null &&
+ test_stderr ""
+'
+
+test_expect_success '-q with multiple args' '
+ expect "" &&
+ test_check_ignore "-q one two" 128 &&
+ stderr_contains "fatal: --quiet is only valid with a single pathname"
+'
+
+test_expect_success '--quiet with multiple args' '
+ expect "" &&
+ test_check_ignore "--quiet one two" 128 &&
+ stderr_contains "fatal: --quiet is only valid with a single pathname"
+'
+
+for verbose_opt in '-v' '--verbose'
+do
+ for quiet_opt in '-q' '--quiet'
+ do
+ test_expect_success "$quiet_opt $verbose_opt" "
+ expect '' &&
+ test_check_ignore '$quiet_opt $verbose_opt foo' 128 &&
+ stderr_contains 'fatal: cannot have both --quiet and --verbose'
+ "
+ done
+done
+
+test_expect_success '--quiet with multiple args' '
+ expect "" &&
+ test_check_ignore "--quiet one two" 128 &&
+ stderr_contains "fatal: --quiet is only valid with a single pathname"
+'
+
+test_expect_success_multi 'erroneous use of --' '' '
+ test_check_ignore "--" 128 &&
+ stderr_contains "fatal: no path specified"
+'
+
+test_expect_success_multi '--stdin with superfluous arg' '' '
+ test_check_ignore "--stdin foo" 128 &&
+ stderr_contains "fatal: cannot specify pathnames with --stdin"
+'
+
+test_expect_success_multi '--stdin -z with superfluous arg' '' '
+ test_check_ignore "--stdin -z foo" 128 &&
+ stderr_contains "fatal: cannot specify pathnames with --stdin"
+'
+
+test_expect_success_multi '-z without --stdin' '' '
+ test_check_ignore "-z" 128 &&
+ stderr_contains "fatal: -z only makes sense with --stdin"
+'
+
+test_expect_success_multi '-z without --stdin and superfluous arg' '' '
+ test_check_ignore "-z foo" 128 &&
+ stderr_contains "fatal: -z only makes sense with --stdin"
+'
+
+test_expect_success_multi 'needs work tree' '' '
+ (
+ cd .git &&
+ test_check_ignore "foo" 128
+ ) &&
+ stderr_contains "fatal: this operation must be run in a work tree"
+'
+
+############################################################################
+#
+# test standard ignores
+
+# First make sure that the presence of a file in the working tree
+# does not impact results, but that the presence of a file in the
+# index does unless the --no-index option is used.
+
+for subdir in '' 'a/'
+do
+ if test -z "$subdir"
+ then
+ where="at top-level"
+ else
+ where="in subdir $subdir"
+ fi
+
+ test_expect_success_multi "non-existent file $where not ignored" \
+ ":: ${subdir}non-existent" \
+ "test_check_ignore '${subdir}non-existent' 1"
+
+ test_expect_success_no_index_multi "non-existent file $where not ignored" \
+ ":: ${subdir}non-existent" \
+ "test_check_ignore '${subdir}non-existent' 1"
+
+ test_expect_success_multi "non-existent file $where ignored" \
+ ".gitignore:1:one ${subdir}one" \
+ "test_check_ignore '${subdir}one'"
+
+ test_expect_success_no_index_multi "non-existent file $where ignored" \
+ ".gitignore:1:one ${subdir}one" \
+ "test_check_ignore '${subdir}one'"
+
+ test_expect_success_multi "existing untracked file $where not ignored" \
+ ":: ${subdir}not-ignored" \
+ "test_check_ignore '${subdir}not-ignored' 1"
+
+ test_expect_success_no_index_multi "existing untracked file $where not ignored" \
+ ":: ${subdir}not-ignored" \
+ "test_check_ignore '${subdir}not-ignored' 1"
+
+ test_expect_success_multi "existing tracked file $where not ignored" \
+ ":: ${subdir}ignored-but-in-index" \
+ "test_check_ignore '${subdir}ignored-but-in-index' 1"
+
+ test_expect_success_no_index_multi "existing tracked file $where shown as ignored" \
+ ".gitignore:2:ignored-* ${subdir}ignored-but-in-index" \
+ "test_check_ignore '${subdir}ignored-but-in-index'"
+
+ test_expect_success_multi "existing untracked file $where ignored" \
+ ".gitignore:2:ignored-* ${subdir}ignored-and-untracked" \
+ "test_check_ignore '${subdir}ignored-and-untracked'"
+
+ test_expect_success_no_index_multi "existing untracked file $where ignored" \
+ ".gitignore:2:ignored-* ${subdir}ignored-and-untracked" \
+ "test_check_ignore '${subdir}ignored-and-untracked'"
+
+ test_expect_success_multi "mix of file types $where" \
+":: ${subdir}non-existent
+.gitignore:1:one ${subdir}one
+:: ${subdir}not-ignored
+:: ${subdir}ignored-but-in-index
+.gitignore:2:ignored-* ${subdir}ignored-and-untracked" \
+ "test_check_ignore '
+ ${subdir}non-existent
+ ${subdir}one
+ ${subdir}not-ignored
+ ${subdir}ignored-but-in-index
+ ${subdir}ignored-and-untracked'
+ "
+
+ test_expect_success_no_index_multi "mix of file types $where" \
+":: ${subdir}non-existent
+.gitignore:1:one ${subdir}one
+:: ${subdir}not-ignored
+.gitignore:2:ignored-* ${subdir}ignored-but-in-index
+.gitignore:2:ignored-* ${subdir}ignored-and-untracked" \
+ "test_check_ignore '
+ ${subdir}non-existent
+ ${subdir}one
+ ${subdir}not-ignored
+ ${subdir}ignored-but-in-index
+ ${subdir}ignored-and-untracked'
+ "
+done
+
+# Having established the above, from now on we mostly test against
+# files which do not exist in the working tree or index.
+
+test_expect_success 'sub-directory local ignore' '
+ expect "a/3-three" &&
+ test_check_ignore "a/3-three a/three-not-this-one"
+'
+
+test_expect_success 'sub-directory local ignore with --verbose' '
+ expect "a/.gitignore:2:*three a/3-three" &&
+ test_check_ignore "--verbose a/3-three a/three-not-this-one"
+'
+
+test_expect_success 'local ignore inside a sub-directory' '
+ expect "3-three" &&
+ (
+ cd a &&
+ test_check_ignore "3-three three-not-this-one"
+ )
+'
+test_expect_success 'local ignore inside a sub-directory with --verbose' '
+ expect "a/.gitignore:2:*three 3-three" &&
+ (
+ cd a &&
+ test_check_ignore "--verbose 3-three three-not-this-one"
+ )
+'
+
+test_expect_success 'nested include of negated pattern' '
+ expect "" &&
+ test_check_ignore "a/b/one" 1
+'
+
+test_expect_success 'nested include of negated pattern with -q' '
+ expect "" &&
+ test_check_ignore "-q a/b/one" 1
+'
+
+test_expect_success 'nested include of negated pattern with -v' '
+ expect "a/b/.gitignore:8:!on* a/b/one" &&
+ test_check_ignore "-v a/b/one" 0
+'
+
+test_expect_success 'nested include of negated pattern with -v -n' '
+ expect "a/b/.gitignore:8:!on* a/b/one" &&
+ test_check_ignore "-v -n a/b/one" 0
+'
+
+############################################################################
+#
+# test ignored sub-directories
+
+test_expect_success_multi 'ignored sub-directory' \
+ 'a/b/.gitignore:5:ignored-dir/ a/b/ignored-dir' '
+ test_check_ignore "a/b/ignored-dir"
+'
+
+test_expect_success 'multiple files inside ignored sub-directory' '
+ expect_from_stdin <<-\EOF &&
+ a/b/ignored-dir/foo
+ a/b/ignored-dir/twoooo
+ a/b/ignored-dir/seven
+ EOF
+ test_check_ignore "a/b/ignored-dir/foo a/b/ignored-dir/twoooo a/b/ignored-dir/seven"
+'
+
+test_expect_success 'multiple files inside ignored sub-directory with -v' '
+ expect_from_stdin <<-\EOF &&
+ a/b/.gitignore:5:ignored-dir/ a/b/ignored-dir/foo
+ a/b/.gitignore:5:ignored-dir/ a/b/ignored-dir/twoooo
+ a/b/.gitignore:5:ignored-dir/ a/b/ignored-dir/seven
+ EOF
+ test_check_ignore "-v a/b/ignored-dir/foo a/b/ignored-dir/twoooo a/b/ignored-dir/seven"
+'
+
+test_expect_success 'cd to ignored sub-directory' '
+ expect_from_stdin <<-\EOF &&
+ foo
+ twoooo
+ seven
+ ../../one
+ EOF
+ (
+ cd a/b/ignored-dir &&
+ test_check_ignore "foo twoooo ../one seven ../../one"
+ )
+'
+
+test_expect_success 'cd to ignored sub-directory with -v' '
+ expect_from_stdin <<-\EOF &&
+ a/b/.gitignore:5:ignored-dir/ foo
+ a/b/.gitignore:5:ignored-dir/ twoooo
+ a/b/.gitignore:8:!on* ../one
+ a/b/.gitignore:5:ignored-dir/ seven
+ .gitignore:1:one ../../one
+ EOF
+ (
+ cd a/b/ignored-dir &&
+ test_check_ignore "-v foo twoooo ../one seven ../../one"
+ )
+'
+
+############################################################################
+#
+# test handling of symlinks
+
+test_expect_success_multi SYMLINKS 'symlink' ':: a/symlink' '
+ test_check_ignore "a/symlink" 1
+'
+
+test_expect_success_multi SYMLINKS 'beyond a symlink' '' '
+ test_check_ignore "a/symlink/foo" 128 &&
+ test_stderr "fatal: pathspec '\''a/symlink/foo'\'' is beyond a symbolic link"
+'
+
+test_expect_success_multi SYMLINKS 'beyond a symlink from subdirectory' '' '
+ (
+ cd a &&
+ test_check_ignore "symlink/foo" 128
+ ) &&
+ test_stderr "fatal: pathspec '\''symlink/foo'\'' is beyond a symbolic link"
+'
+
+############################################################################
+#
+# test handling of submodules
+
+test_expect_success_multi 'submodule' '' '
+ test_check_ignore "a/submodule/one" 128 &&
+ test_stderr "fatal: Pathspec '\''a/submodule/one'\'' is in submodule '\''a/submodule'\''"
+'
+
+test_expect_success_multi 'submodule from subdirectory' '' '
+ (
+ cd a &&
+ test_check_ignore "submodule/one" 128
+ ) &&
+ test_stderr "fatal: Pathspec '\''submodule/one'\'' is in submodule '\''a/submodule'\''"
+'
+
+############################################################################
+#
+# test handling of global ignore files
+
+test_expect_success 'global ignore not yet enabled' '
+ expect_from_stdin <<-\EOF &&
+ .git/info/exclude:1:per-repo per-repo
+ a/.gitignore:2:*three a/globalthree
+ .git/info/exclude:1:per-repo a/per-repo
+ EOF
+ test_check_ignore "-v globalone per-repo a/globalthree a/per-repo not-ignored a/globaltwo"
+'
+
+test_expect_success 'global ignore' '
+ enable_global_excludes &&
+ expect_from_stdin <<-\EOF &&
+ globalone
+ per-repo
+ globalthree
+ a/globalthree
+ a/per-repo
+ EOF
+ test_check_ignore "globalone per-repo globalthree a/globalthree a/per-repo not-ignored globaltwo"
+'
+
+test_expect_success 'global ignore with -v' '
+ enable_global_excludes &&
+ expect_from_stdin <<-EOF &&
+ $global_excludes:1:globalone globalone
+ .git/info/exclude:1:per-repo per-repo
+ $global_excludes:3:globalthree globalthree
+ a/.gitignore:2:*three a/globalthree
+ .git/info/exclude:1:per-repo a/per-repo
+ $global_excludes:2:!globaltwo globaltwo
+ EOF
+ test_check_ignore "-v globalone per-repo globalthree a/globalthree a/per-repo not-ignored globaltwo"
+'
+
+############################################################################
+#
+# test --stdin
+
+cat <<-\EOF >stdin
+ one
+ not-ignored
+ a/one
+ a/not-ignored
+ a/b/on
+ a/b/one
+ a/b/one one
+ "a/b/one two"
+ "a/b/one\"three"
+ a/b/not-ignored
+ a/b/two
+ a/b/twooo
+ globaltwo
+ a/globaltwo
+ a/b/globaltwo
+ b/globaltwo
+EOF
+cat <<-\EOF >expected-default
+ one
+ a/one
+ a/b/twooo
+EOF
+cat <<-EOF >expected-verbose
+ .gitignore:1:one one
+ .gitignore:1:one a/one
+ a/b/.gitignore:8:!on* a/b/on
+ a/b/.gitignore:8:!on* a/b/one
+ a/b/.gitignore:8:!on* a/b/one one
+ a/b/.gitignore:8:!on* a/b/one two
+ a/b/.gitignore:8:!on* "a/b/one\\"three"
+ a/b/.gitignore:9:!two a/b/two
+ a/.gitignore:1:two* a/b/twooo
+ $global_excludes:2:!globaltwo globaltwo
+ $global_excludes:2:!globaltwo a/globaltwo
+ $global_excludes:2:!globaltwo a/b/globaltwo
+ $global_excludes:2:!globaltwo b/globaltwo
+EOF
+
+broken_c_unquote stdin >stdin0
+
+broken_c_unquote expected-default >expected-default0
+
+broken_c_unquote_verbose expected-verbose >expected-verbose0
+
+test_expect_success '--stdin' '
+ expect_from_stdin <expected-default &&
+ test_check_ignore "--stdin" <stdin
+'
+
+test_expect_success '--stdin -q' '
+ expect "" &&
+ test_check_ignore "-q --stdin" <stdin
+'
+
+test_expect_success '--stdin -v' '
+ expect_from_stdin <expected-verbose &&
+ test_check_ignore "-v --stdin" <stdin
+'
+
+for opts in '--stdin -z' '-z --stdin'
+do
+ test_expect_success "$opts" "
+ expect_from_stdin <expected-default0 &&
+ test_check_ignore '$opts' <stdin0
+ "
+
+ test_expect_success "$opts -q" "
+ expect "" &&
+ test_check_ignore '-q $opts' <stdin0
+ "
+
+ test_expect_success "$opts -v" "
+ expect_from_stdin <expected-verbose0 &&
+ test_check_ignore '-v $opts' <stdin0
+ "
+done
+
+cat <<-\EOF >stdin
+ ../one
+ ../not-ignored
+ one
+ not-ignored
+ b/on
+ b/one
+ b/one one
+ "b/one two"
+ "b/one\"three"
+ b/two
+ b/not-ignored
+ b/twooo
+ ../globaltwo
+ globaltwo
+ b/globaltwo
+ ../b/globaltwo
+ c/not-ignored
+EOF
+# N.B. we deliberately end STDIN with a non-matching pattern in order
+# to test that the exit code indicates that one or more of the
+# provided paths is ignored - in other words, that it represents an
+# aggregation of all the results, not just the final result.
+
+cat <<-EOF >expected-all
+ .gitignore:1:one ../one
+ :: ../not-ignored
+ .gitignore:1:one one
+ :: not-ignored
+ a/b/.gitignore:8:!on* b/on
+ a/b/.gitignore:8:!on* b/one
+ a/b/.gitignore:8:!on* b/one one
+ a/b/.gitignore:8:!on* b/one two
+ a/b/.gitignore:8:!on* "b/one\\"three"
+ a/b/.gitignore:9:!two b/two
+ :: b/not-ignored
+ a/.gitignore:1:two* b/twooo
+ $global_excludes:2:!globaltwo ../globaltwo
+ $global_excludes:2:!globaltwo globaltwo
+ $global_excludes:2:!globaltwo b/globaltwo
+ $global_excludes:2:!globaltwo ../b/globaltwo
+ :: c/not-ignored
+EOF
+cat <<-EOF >expected-default
+../one
+one
+b/twooo
+EOF
+grep -v '^:: ' expected-all >expected-verbose
+
+broken_c_unquote stdin >stdin0
+
+broken_c_unquote expected-default >expected-default0
+
+broken_c_unquote_verbose expected-verbose >expected-verbose0
+
+test_expect_success '--stdin from subdirectory' '
+ expect_from_stdin <expected-default &&
+ (
+ cd a &&
+ test_check_ignore "--stdin" <../stdin
+ )
+'
+
+test_expect_success '--stdin from subdirectory with -v' '
+ expect_from_stdin <expected-verbose &&
+ (
+ cd a &&
+ test_check_ignore "--stdin -v" <../stdin
+ )
+'
+
+test_expect_success '--stdin from subdirectory with -v -n' '
+ expect_from_stdin <expected-all &&
+ (
+ cd a &&
+ test_check_ignore "--stdin -v -n" <../stdin
+ )
+'
+
+for opts in '--stdin -z' '-z --stdin'
+do
+ test_expect_success "$opts from subdirectory" '
+ expect_from_stdin <expected-default0 &&
+ (
+ cd a &&
+ test_check_ignore "'"$opts"'" <../stdin0
+ )
+ '
+
+ test_expect_success "$opts from subdirectory with -v" '
+ expect_from_stdin <expected-verbose0 &&
+ (
+ cd a &&
+ test_check_ignore "'"$opts"' -v" <../stdin0
+ )
+ '
+done
+
+test_expect_success PIPE 'streaming support for --stdin' '
+ mkfifo in out &&
+ (git check-ignore -n -v --stdin <in >out &) &&
+
+ # We cannot just "echo >in" because check-ignore would get EOF
+ # after echo exited; instead we open the descriptor in our
+ # shell, and then echo to the fd. We make sure to close it at
+ # the end, so that the subprocess does get EOF and dies
+ # properly.
+ #
+ # Similarly, we must keep "out" open so that check-ignore does
+ # not ever get SIGPIPE trying to write to us. Not only would that
+ # produce incorrect results, but then there would be no writer on the
+ # other end of the pipe, and we would potentially block forever trying
+ # to open it.
+ exec 9>in &&
+ exec 8<out &&
+ test_when_finished "exec 9>&-" &&
+ test_when_finished "exec 8<&-" &&
+ echo >&9 one &&
+ read response <&8 &&
+ echo "$response" | grep "^\.gitignore:1:one one" &&
+ echo >&9 two &&
+ read response <&8 &&
+ echo "$response" | grep "^:: two"
+'
+
+test_expect_success 'existing file and directory' '
+ test_when_finished "rm one" &&
+ test_when_finished "rmdir top-level-dir" &&
+ >one &&
+ mkdir top-level-dir &&
+ git check-ignore one top-level-dir >actual &&
+ grep one actual &&
+ grep top-level-dir actual
+'
+
+test_expect_success 'existing directory and file' '
+ test_when_finished "rm one" &&
+ test_when_finished "rmdir top-level-dir" &&
+ >one &&
+ mkdir top-level-dir &&
+ git check-ignore top-level-dir one >actual &&
+ grep one actual &&
+ grep top-level-dir actual
+'
+
+test_expect_success 'exact prefix matching (with root)' '
+ test_when_finished rm -r a &&
+ mkdir -p a/git a/git-foo &&
+ touch a/git/foo a/git-foo/bar &&
+ echo /git/ >a/.gitignore &&
+ git check-ignore a/git a/git/foo a/git-foo a/git-foo/bar >actual &&
+ cat >expect <<-\EOF &&
+ a/git
+ a/git/foo
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'exact prefix matching (without root)' '
+ test_when_finished rm -r a &&
+ mkdir -p a/git a/git-foo &&
+ touch a/git/foo a/git-foo/bar &&
+ echo git/ >a/.gitignore &&
+ git check-ignore a/git a/git/foo a/git-foo a/git-foo/bar >actual &&
+ cat >expect <<-\EOF &&
+ a/git
+ a/git/foo
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'directories and ** matches' '
+ cat >.gitignore <<-\EOF &&
+ data/**
+ !data/**/
+ !data/**/*.txt
+ EOF
+ git check-ignore file \
+ data/file data/data1/file1 data/data1/file1.txt \
+ data/data2/file2 data/data2/file2.txt >actual &&
+ cat >expect <<-\EOF &&
+ data/file
+ data/data1/file1
+ data/data2/file2
+ EOF
+ test_cmp expect actual
+'
+
+############################################################################
+#
+# test whitespace handling
+
+test_expect_success 'trailing whitespace is ignored' '
+ mkdir whitespace &&
+ >whitespace/trailing &&
+ >whitespace/untracked &&
+ echo "whitespace/trailing " >ignore &&
+ cat >expect <<EOF &&
+whitespace/untracked
+EOF
+ git ls-files -o -X ignore whitespace >actual 2>err &&
+ test_cmp expect actual &&
+ test_must_be_empty err
+'
+
+test_expect_success !MINGW 'quoting allows trailing whitespace' '
+ rm -rf whitespace &&
+ mkdir whitespace &&
+ >"whitespace/trailing " &&
+ >whitespace/untracked &&
+ echo "whitespace/trailing\\ \\ " >ignore &&
+ echo whitespace/untracked >expect &&
+ git ls-files -o -X ignore whitespace >actual 2>err &&
+ test_cmp expect actual &&
+ test_must_be_empty err
+'
+
+test_expect_success !MINGW,!CYGWIN 'correct handling of backslashes' '
+ rm -rf whitespace &&
+ mkdir whitespace &&
+ >"whitespace/trailing 1 " &&
+ >"whitespace/trailing 2 \\\\" &&
+ >"whitespace/trailing 3 \\\\" &&
+ >"whitespace/trailing 4 \\ " &&
+ >"whitespace/trailing 5 \\ \\ " &&
+ >"whitespace/trailing 6 \\a\\" &&
+ >whitespace/untracked &&
+ sed -e "s/Z$//" >ignore <<-\EOF &&
+ whitespace/trailing 1 \ Z
+ whitespace/trailing 2 \\\\Z
+ whitespace/trailing 3 \\\\ Z
+ whitespace/trailing 4 \\\ Z
+ whitespace/trailing 5 \\ \\\ Z
+ whitespace/trailing 6 \\a\\Z
+ EOF
+ echo whitespace/untracked >expect &&
+ git ls-files -o -X ignore whitespace >actual 2>err &&
+ test_cmp expect actual &&
+ test_must_be_empty err
+'
+
+test_expect_success 'info/exclude trumps core.excludesfile' '
+ echo >>global-excludes usually-ignored &&
+ echo >>.git/info/exclude "!usually-ignored" &&
+ >usually-ignored &&
+ echo "?? usually-ignored" >expect &&
+
+ git status --porcelain usually-ignored >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success SYMLINKS 'set up ignore file for symlink tests' '
+ echo "*" >ignore &&
+ rm -f .gitignore .git/info/exclude
+'
+
+test_expect_success SYMLINKS 'symlinks respected in core.excludesFile' '
+ test_when_finished "rm symlink" &&
+ ln -s ignore symlink &&
+ test_config core.excludesFile "$(pwd)/symlink" &&
+ echo file >expect &&
+ git check-ignore file >actual 2>err &&
+ test_cmp expect actual &&
+ test_must_be_empty err
+'
+
+test_expect_success SYMLINKS 'symlinks respected in info/exclude' '
+ test_when_finished "rm .git/info/exclude" &&
+ ln -s ../../ignore .git/info/exclude &&
+ echo file >expect &&
+ git check-ignore file >actual 2>err &&
+ test_cmp expect actual &&
+ test_must_be_empty err
+'
+
+test_expect_success SYMLINKS 'symlinks not respected in-tree' '
+ test_when_finished "rm .gitignore" &&
+ ln -s ignore .gitignore &&
+ mkdir subdir &&
+ ln -s ignore subdir/.gitignore &&
+ test_must_fail git check-ignore subdir/file >actual 2>err &&
+ test_must_be_empty actual &&
+ test_grep "unable to access.*gitignore" err
+'
+
+test_done
diff --git a/t/t0009-prio-queue.sh b/t/t0009-prio-queue.sh
new file mode 100755
index 0000000..eea9910
--- /dev/null
+++ b/t/t0009-prio-queue.sh
@@ -0,0 +1,66 @@
+#!/bin/sh
+
+test_description='basic tests for priority queue implementation'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+cat >expect <<'EOF'
+1
+2
+3
+4
+5
+5
+6
+7
+8
+9
+10
+EOF
+test_expect_success 'basic ordering' '
+ test-tool prio-queue 2 6 3 10 9 5 7 4 5 8 1 dump >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<'EOF'
+2
+3
+4
+1
+5
+6
+EOF
+test_expect_success 'mixed put and get' '
+ test-tool prio-queue 6 2 4 get 5 3 get get 1 dump >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<'EOF'
+1
+2
+NULL
+1
+2
+NULL
+EOF
+test_expect_success 'notice empty queue' '
+ test-tool prio-queue 1 2 get get get 1 2 get get get >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<'EOF'
+3
+2
+6
+4
+5
+1
+8
+EOF
+test_expect_success 'stack order' '
+ test-tool prio-queue stack 8 1 5 4 6 2 3 dump >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t0010-racy-git.sh b/t/t0010-racy-git.sh
new file mode 100755
index 0000000..837c8b7
--- /dev/null
+++ b/t/t0010-racy-git.sh
@@ -0,0 +1,34 @@
+#!/bin/sh
+
+test_description='racy GIT'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# This test can give false success if your machine is sufficiently
+# slow or your trial happened to happen on second boundary.
+
+for trial in 0 1 2 3 4
+do
+ rm -f .git/index
+ echo frotz >infocom
+ git update-index --add infocom
+ echo xyzzy >infocom
+
+ files=$(git diff-files -p)
+ test_expect_success \
+ "Racy GIT trial #$trial part A" \
+ 'test "" != "$files"'
+
+ sleep 1
+ echo xyzzy >cornerstone
+ git update-index --add cornerstone
+
+ files=$(git diff-files -p)
+ test_expect_success \
+ "Racy GIT trial #$trial part B" \
+ 'test "" != "$files"'
+
+done
+
+test_done
diff --git a/t/t0011-hashmap.sh b/t/t0011-hashmap.sh
new file mode 100755
index 0000000..1cb6aa6
--- /dev/null
+++ b/t/t0011-hashmap.sh
@@ -0,0 +1,260 @@
+#!/bin/sh
+
+test_description='test hashmap and string hash functions'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_hashmap() {
+ echo "$1" | test-tool hashmap $3 > actual &&
+ echo "$2" > expect &&
+ test_cmp expect actual
+}
+
+test_expect_success 'put' '
+
+test_hashmap "put key1 value1
+put key2 value2
+put fooBarFrotz value3
+put foobarfrotz value4
+size" "NULL
+NULL
+NULL
+NULL
+64 4"
+
+'
+
+test_expect_success 'put (case insensitive)' '
+
+test_hashmap "put key1 value1
+put key2 value2
+put fooBarFrotz value3
+size" "NULL
+NULL
+NULL
+64 3" ignorecase
+
+'
+
+test_expect_success 'replace' '
+
+test_hashmap "put key1 value1
+put key1 value2
+put fooBarFrotz value3
+put fooBarFrotz value4
+size" "NULL
+value1
+NULL
+value3
+64 2"
+
+'
+
+test_expect_success 'replace (case insensitive)' '
+
+test_hashmap "put key1 value1
+put Key1 value2
+put fooBarFrotz value3
+put foobarfrotz value4
+size" "NULL
+value1
+NULL
+value3
+64 2" ignorecase
+
+'
+
+test_expect_success 'get' '
+
+test_hashmap "put key1 value1
+put key2 value2
+put fooBarFrotz value3
+put foobarfrotz value4
+get key1
+get key2
+get fooBarFrotz
+get notInMap" "NULL
+NULL
+NULL
+NULL
+value1
+value2
+value3
+NULL"
+
+'
+
+test_expect_success 'get (case insensitive)' '
+
+test_hashmap "put key1 value1
+put key2 value2
+put fooBarFrotz value3
+get Key1
+get keY2
+get foobarfrotz
+get notInMap" "NULL
+NULL
+NULL
+value1
+value2
+value3
+NULL" ignorecase
+
+'
+
+test_expect_success 'add' '
+
+test_hashmap "add key1 value1
+add key1 value2
+add fooBarFrotz value3
+add fooBarFrotz value4
+get key1
+get fooBarFrotz
+get notInMap" "value2
+value1
+value4
+value3
+NULL"
+
+'
+
+test_expect_success 'add (case insensitive)' '
+
+test_hashmap "add key1 value1
+add Key1 value2
+add fooBarFrotz value3
+add foobarfrotz value4
+get key1
+get Foobarfrotz
+get notInMap" "value2
+value1
+value4
+value3
+NULL" ignorecase
+
+'
+
+test_expect_success 'remove' '
+
+test_hashmap "put key1 value1
+put key2 value2
+put fooBarFrotz value3
+remove key1
+remove key2
+remove notInMap
+size" "NULL
+NULL
+NULL
+value1
+value2
+NULL
+64 1"
+
+'
+
+test_expect_success 'remove (case insensitive)' '
+
+test_hashmap "put key1 value1
+put key2 value2
+put fooBarFrotz value3
+remove Key1
+remove keY2
+remove notInMap
+size" "NULL
+NULL
+NULL
+value1
+value2
+NULL
+64 1" ignorecase
+
+'
+
+test_expect_success 'iterate' '
+ test-tool hashmap >actual.raw <<-\EOF &&
+ put key1 value1
+ put key2 value2
+ put fooBarFrotz value3
+ iterate
+ EOF
+
+ cat >expect <<-\EOF &&
+ NULL
+ NULL
+ NULL
+ fooBarFrotz value3
+ key1 value1
+ key2 value2
+ EOF
+
+ sort <actual.raw >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'iterate (case insensitive)' '
+ test-tool hashmap ignorecase >actual.raw <<-\EOF &&
+ put key1 value1
+ put key2 value2
+ put fooBarFrotz value3
+ iterate
+ EOF
+
+ cat >expect <<-\EOF &&
+ NULL
+ NULL
+ NULL
+ fooBarFrotz value3
+ key1 value1
+ key2 value2
+ EOF
+
+ sort <actual.raw >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'grow / shrink' '
+
+ rm -f in &&
+ rm -f expect &&
+ for n in $(test_seq 51)
+ do
+ echo put key$n value$n >> in &&
+ echo NULL >> expect || return 1
+ done &&
+ echo size >> in &&
+ echo 64 51 >> expect &&
+ echo put key52 value52 >> in &&
+ echo NULL >> expect &&
+ echo size >> in &&
+ echo 256 52 >> expect &&
+ for n in $(test_seq 12)
+ do
+ echo remove key$n >> in &&
+ echo value$n >> expect || return 1
+ done &&
+ echo size >> in &&
+ echo 256 40 >> expect &&
+ echo remove key40 >> in &&
+ echo value40 >> expect &&
+ echo size >> in &&
+ echo 64 39 >> expect &&
+ cat in | test-tool hashmap > out &&
+ test_cmp expect out
+
+'
+
+test_expect_success 'string interning' '
+
+test_hashmap "intern value1
+intern Value1
+intern value2
+intern value2
+" "value1
+Value1
+value2
+value2"
+
+'
+
+test_done
diff --git a/t/t0012-help.sh b/t/t0012-help.sh
new file mode 100755
index 0000000..1d273d9
--- /dev/null
+++ b/t/t0012-help.sh
@@ -0,0 +1,264 @@
+#!/bin/sh
+
+test_description='help'
+
+. ./test-lib.sh
+
+configure_help () {
+ test_config help.format html &&
+
+ # Unless the path has "://" in it, Git tries to make sure
+ # the documentation directory locally exists. Avoid it as
+ # we are only interested in seeing an attempt to correctly
+ # invoke a help browser in this test.
+ test_config help.htmlpath test://html &&
+
+ # Name a custom browser
+ test_config browser.test.cmd ./test-browser &&
+ test_config help.browser test
+}
+
+test_expect_success "setup" '
+ # Just write out which page gets requested
+ write_script test-browser <<-\EOF
+ echo "$*" >test-browser.log
+ EOF
+'
+
+# make sure to exercise these code paths, the output is a bit tricky
+# to verify
+test_expect_success 'basic help commands' '
+ git help >/dev/null &&
+ git help -a --no-verbose >/dev/null &&
+ git help -g >/dev/null &&
+ git help -a >/dev/null
+'
+
+test_expect_success 'invalid usage' '
+ test_expect_code 129 git help -a add &&
+ test_expect_code 129 git help --all add &&
+
+ test_expect_code 129 git help -g add &&
+ test_expect_code 129 git help -a -c &&
+
+ test_expect_code 129 git help -g add &&
+ test_expect_code 129 git help -a -g &&
+
+ test_expect_code 129 git help --user-interfaces add &&
+
+ test_expect_code 129 git help -g -c &&
+ test_expect_code 129 git help --config-for-completion add &&
+ test_expect_code 129 git help --config-sections-for-completion add
+'
+
+for opt in '-a' '-g' '-c' '--config-for-completion' '--config-sections-for-completion'
+do
+ test_expect_success "invalid usage of '$opt' with [-i|-m|-w]" '
+ git help $opt &&
+ test_expect_code 129 git help $opt -i &&
+ test_expect_code 129 git help $opt -m &&
+ test_expect_code 129 git help $opt -w
+ '
+
+ if test "$opt" = "-a"
+ then
+ continue
+ fi
+
+ test_expect_success "invalid usage of '$opt' with --no-external-commands" '
+ test_expect_code 129 git help $opt --no-external-commands
+ '
+
+ test_expect_success "invalid usage of '$opt' with --no-aliases" '
+ test_expect_code 129 git help $opt --no-external-commands
+ '
+done
+
+test_expect_success "works for commands and guides by default" '
+ configure_help &&
+ git help status &&
+ echo "test://html/git-status.html" >expect &&
+ test_cmp expect test-browser.log &&
+ git help revisions &&
+ echo "test://html/gitrevisions.html" >expect &&
+ test_cmp expect test-browser.log
+'
+
+test_expect_success "--exclude-guides does not work for guides" '
+ >test-browser.log &&
+ test_must_fail git help --exclude-guides revisions &&
+ test_must_be_empty test-browser.log
+'
+
+test_expect_success "--help does not work for guides" "
+ cat <<-EOF >expect &&
+ git: 'revisions' is not a git command. See 'git --help'.
+ EOF
+ test_must_fail git revisions --help 2>actual &&
+ test_cmp expect actual
+"
+
+test_expect_success 'git help' '
+ git help >help.output &&
+ test_grep "^ clone " help.output &&
+ test_grep "^ add " help.output &&
+ test_grep "^ log " help.output &&
+ test_grep "^ commit " help.output &&
+ test_grep "^ fetch " help.output
+'
+
+test_expect_success 'git help -g' '
+ git help -g >help.output &&
+ test_grep "^ everyday " help.output &&
+ test_grep "^ tutorial " help.output
+'
+
+test_expect_success 'git help fails for non-existing html pages' '
+ configure_help &&
+ mkdir html-empty &&
+ test_must_fail git -c help.htmlpath=html-empty help status &&
+ test_must_be_empty test-browser.log
+'
+
+test_expect_success 'git help succeeds without git.html' '
+ configure_help &&
+ mkdir html-with-docs &&
+ touch html-with-docs/git-status.html &&
+ git -c help.htmlpath=html-with-docs help status &&
+ echo "html-with-docs/git-status.html" >expect &&
+ test_cmp expect test-browser.log
+'
+
+test_expect_success 'git help --user-interfaces' '
+ git help --user-interfaces >help.output &&
+ grep "^ attributes " help.output &&
+ grep "^ mailmap " help.output
+'
+
+test_expect_success 'git help -c' '
+ git help -c >help.output &&
+ cat >expect <<-\EOF &&
+
+ '\''git help config'\'' for more information
+ EOF
+ grep -v -E \
+ -e "^[^.]+\.[^.]+$" \
+ -e "^[^.]+\.[^.]+\.[^.]+$" \
+ help.output >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git help --config-for-completion' '
+ git help -c >human &&
+ grep -E \
+ -e "^[^.]+\.[^.]+$" \
+ -e "^[^.]+\.[^.]+\.[^.]+$" human |
+ sed -e "s/\*.*//" -e "s/<.*//" |
+ sort -u >human.munged &&
+
+ git help --config-for-completion >vars &&
+ test_cmp human.munged vars
+'
+
+test_expect_success 'git help --config-sections-for-completion' '
+ git help -c >human &&
+ grep -E \
+ -e "^[^.]+\.[^.]+$" \
+ -e "^[^.]+\.[^.]+\.[^.]+$" human |
+ sed -e "s/\..*//" |
+ sort -u >human.munged &&
+
+ git help --config-sections-for-completion >sections &&
+ test_cmp human.munged sections
+'
+
+test_section_spacing () {
+ cat >expect &&
+ "$@" >out &&
+ grep -E "(^[^ ]|^$)" out >actual
+}
+
+test_section_spacing_trailer () {
+ test_section_spacing "$@" &&
+ test_expect_code 1 git >out &&
+ sed -n '/list available subcommands/,$p' <out >>expect
+}
+
+
+for cmd in git "git help"
+do
+ test_expect_success "'$cmd' section spacing" '
+ test_section_spacing_trailer git help <<-\EOF &&
+ usage: git [-v | --version] [-h | --help] [-C <path>] [-c <name>=<value>]
+
+ These are common Git commands used in various situations:
+
+ start a working area (see also: git help tutorial)
+
+ work on the current change (see also: git help everyday)
+
+ examine the history and state (see also: git help revisions)
+
+ grow, mark and tweak your common history
+
+ collaborate (see also: git help workflows)
+
+ EOF
+ test_cmp expect actual
+ '
+done
+
+test_expect_success "'git help -a' section spacing" '
+ test_section_spacing \
+ git help -a --no-external-commands --no-aliases <<-\EOF &&
+ See '\''git help <command>'\'' to read about a specific subcommand
+
+ Main Porcelain Commands
+
+ Ancillary Commands / Manipulators
+
+ Ancillary Commands / Interrogators
+
+ Interacting with Others
+
+ Low-level Commands / Manipulators
+
+ Low-level Commands / Interrogators
+
+ Low-level Commands / Syncing Repositories
+
+ Low-level Commands / Internal Helpers
+
+ User-facing repository, command and file interfaces
+
+ Developer-facing file formats, protocols and other interfaces
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success "'git help -g' section spacing" '
+ test_section_spacing_trailer git help -g <<-\EOF &&
+ The Git concept guides are:
+
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'generate builtin list' '
+ mkdir -p sub &&
+ git --list-cmds=builtins >builtins
+'
+
+while read builtin
+do
+ test_expect_success "$builtin can handle -h" '
+ (
+ GIT_CEILING_DIRECTORIES=$(pwd) &&
+ export GIT_CEILING_DIRECTORIES &&
+ test_expect_code 129 git -C sub $builtin -h >output 2>&1
+ ) &&
+ test_grep usage output
+ '
+done <builtins
+
+test_done
diff --git a/t/t0013-sha1dc.sh b/t/t0013-sha1dc.sh
new file mode 100755
index 0000000..0881417
--- /dev/null
+++ b/t/t0013-sha1dc.sh
@@ -0,0 +1,23 @@
+#!/bin/sh
+
+test_description='test sha1 collision detection'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+TEST_DATA="$TEST_DIRECTORY/t0013"
+
+test_lazy_prereq SHA1_IS_SHA1DC 'test-tool sha1-is-sha1dc'
+
+if ! test_have_prereq SHA1_IS_SHA1DC
+then
+ skip_all='skipping sha1 collision tests, not using sha1collisiondetection'
+ test_done
+fi
+
+test_expect_success 'test-sha1 detects shattered pdf' '
+ test_must_fail test-tool sha1 <"$TEST_DATA/shattered-1.pdf" 2>err &&
+ test_grep collision err &&
+ grep 38762cf7f55934b34d179ae6a4c80cadccbb7f0a err
+'
+
+test_done
diff --git a/t/t0013/shattered-1.pdf b/t/t0013/shattered-1.pdf
new file mode 100644
index 0000000..ba9aaa1
--- /dev/null
+++ b/t/t0013/shattered-1.pdf
Binary files differ
diff --git a/t/t0014-alias.sh b/t/t0014-alias.sh
new file mode 100755
index 0000000..9556834
--- /dev/null
+++ b/t/t0014-alias.sh
@@ -0,0 +1,47 @@
+#!/bin/sh
+
+test_description='git command aliasing'
+
+. ./test-lib.sh
+
+test_expect_success 'nested aliases - internal execution' '
+ git config alias.nested-internal-1 nested-internal-2 &&
+ git config alias.nested-internal-2 status &&
+ git nested-internal-1 >output &&
+ test_grep "^On branch " output
+'
+
+test_expect_success 'nested aliases - mixed execution' '
+ git config alias.nested-external-1 nested-external-2 &&
+ git config alias.nested-external-2 "!git nested-external-3" &&
+ git config alias.nested-external-3 status &&
+ git nested-external-1 >output &&
+ test_grep "^On branch " output
+'
+
+test_expect_success 'looping aliases - internal execution' '
+ git config alias.loop-internal-1 loop-internal-2 &&
+ git config alias.loop-internal-2 loop-internal-3 &&
+ git config alias.loop-internal-3 loop-internal-2 &&
+ test_must_fail git loop-internal-1 2>output &&
+ test_grep "^fatal: alias loop detected: expansion of" output
+'
+
+# This test is disabled until external loops are fixed, because would block
+# the test suite for a full minute.
+#
+#test_expect_failure 'looping aliases - mixed execution' '
+# git config alias.loop-mixed-1 loop-mixed-2 &&
+# git config alias.loop-mixed-2 "!git loop-mixed-1" &&
+# test_must_fail git loop-mixed-1 2>output &&
+# test_grep "^fatal: alias loop detected: expansion of" output
+#'
+
+test_expect_success 'run-command formats empty args properly' '
+ test_must_fail env GIT_TRACE=1 git frotz a "" b " " c 2>actual.raw &&
+ sed -ne "/run_command:/s/.*trace: run_command: //p" actual.raw >actual &&
+ echo "git-frotz a '\'''\'' b '\'' '\'' c" >expect &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t0015-hash.sh b/t/t0015-hash.sh
new file mode 100755
index 0000000..0a087a1
--- /dev/null
+++ b/t/t0015-hash.sh
@@ -0,0 +1,56 @@
+#!/bin/sh
+
+test_description='test basic hash implementation'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'test basic SHA-1 hash values' '
+ test-tool sha1 </dev/null >actual &&
+ grep da39a3ee5e6b4b0d3255bfef95601890afd80709 actual &&
+ printf "a" | test-tool sha1 >actual &&
+ grep 86f7e437faa5a7fce15d1ddcb9eaeaea377667b8 actual &&
+ printf "abc" | test-tool sha1 >actual &&
+ grep a9993e364706816aba3e25717850c26c9cd0d89d actual &&
+ printf "message digest" | test-tool sha1 >actual &&
+ grep c12252ceda8be8994d5fa0290a47231c1d16aae3 actual &&
+ printf "abcdefghijklmnopqrstuvwxyz" | test-tool sha1 >actual &&
+ grep 32d10c7b8cf96570ca04ce37f2a19d84240d3a89 actual &&
+ perl -e "$| = 1; print q{aaaaaaaaaa} for 1..100000;" |
+ test-tool sha1 >actual &&
+ grep 34aa973cd4c4daa4f61eeb2bdbad27316534016f actual &&
+ printf "blob 0\0" | test-tool sha1 >actual &&
+ grep e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 actual &&
+ printf "blob 3\0abc" | test-tool sha1 >actual &&
+ grep f2ba8f84ab5c1bce84a7b441cb1959cfc7093b7f actual &&
+ printf "tree 0\0" | test-tool sha1 >actual &&
+ grep 4b825dc642cb6eb9a060e54bf8d69288fbee4904 actual
+'
+
+test_expect_success 'test basic SHA-256 hash values' '
+ test-tool sha256 </dev/null >actual &&
+ grep e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 actual &&
+ printf "a" | test-tool sha256 >actual &&
+ grep ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb actual &&
+ printf "abc" | test-tool sha256 >actual &&
+ grep ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad actual &&
+ printf "message digest" | test-tool sha256 >actual &&
+ grep f7846f55cf23e14eebeab5b4e1550cad5b509e3348fbc4efa3a1413d393cb650 actual &&
+ printf "abcdefghijklmnopqrstuvwxyz" | test-tool sha256 >actual &&
+ grep 71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73 actual &&
+ # Try to exercise the chunking code by turning autoflush on.
+ perl -e "$| = 1; print q{aaaaaaaaaa} for 1..100000;" |
+ test-tool sha256 >actual &&
+ grep cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0 actual &&
+ perl -e "$| = 1; print q{abcdefghijklmnopqrstuvwxyz} for 1..100000;" |
+ test-tool sha256 >actual &&
+ grep e406ba321ca712ad35a698bf0af8d61fc4dc40eca6bdcea4697962724ccbde35 actual &&
+ printf "blob 0\0" | test-tool sha256 >actual &&
+ grep 473a0f4c3be8a93681a267e3b1e9a7dcda1185436fe141f7749120a303721813 actual &&
+ printf "blob 3\0abc" | test-tool sha256 >actual &&
+ grep c1cf6e465077930e88dc5136641d402f72a229ddd996f627d60e9639eaba35a6 actual &&
+ printf "tree 0\0" | test-tool sha256 >actual &&
+ grep 6ef19b41225c5369f1c104d45d8d85efa9b057b53b14b4b9b939dd74decc5321 actual
+'
+
+test_done
diff --git a/t/t0016-oidmap.sh b/t/t0016-oidmap.sh
new file mode 100755
index 0000000..0faef1f
--- /dev/null
+++ b/t/t0016-oidmap.sh
@@ -0,0 +1,112 @@
+#!/bin/sh
+
+test_description='test oidmap'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# This purposefully is very similar to t0011-hashmap.sh
+
+test_oidmap () {
+ echo "$1" | test-tool oidmap $3 >actual &&
+ echo "$2" >expect &&
+ test_cmp expect actual
+}
+
+
+test_expect_success 'setup' '
+
+ test_commit one &&
+ test_commit two &&
+ test_commit three &&
+ test_commit four
+
+'
+
+test_expect_success 'put' '
+
+test_oidmap "put one 1
+put two 2
+put invalidOid 4
+put three 3" "NULL
+NULL
+Unknown oid: invalidOid
+NULL"
+
+'
+
+test_expect_success 'replace' '
+
+test_oidmap "put one 1
+put two 2
+put three 3
+put invalidOid 4
+put two deux
+put one un" "NULL
+NULL
+NULL
+Unknown oid: invalidOid
+2
+1"
+
+'
+
+test_expect_success 'get' '
+
+test_oidmap "put one 1
+put two 2
+put three 3
+get two
+get four
+get invalidOid
+get one" "NULL
+NULL
+NULL
+2
+NULL
+Unknown oid: invalidOid
+1"
+
+'
+
+test_expect_success 'remove' '
+
+test_oidmap "put one 1
+put two 2
+put three 3
+remove one
+remove two
+remove invalidOid
+remove four" "NULL
+NULL
+NULL
+1
+2
+Unknown oid: invalidOid
+NULL"
+
+'
+
+test_expect_success 'iterate' '
+ test-tool oidmap >actual.raw <<-\EOF &&
+ put one 1
+ put two 2
+ put three 3
+ iterate
+ EOF
+
+ # sort "expect" too so we do not rely on the order of particular oids
+ sort >expect <<-EOF &&
+ NULL
+ NULL
+ NULL
+ $(git rev-parse one) 1
+ $(git rev-parse two) 2
+ $(git rev-parse three) 3
+ EOF
+
+ sort <actual.raw >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t0017-env-helper.sh b/t/t0017-env-helper.sh
new file mode 100755
index 0000000..fc14ba0
--- /dev/null
+++ b/t/t0017-env-helper.sh
@@ -0,0 +1,100 @@
+#!/bin/sh
+
+test_description='test test-tool env-helper'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+
+test_expect_success 'test-tool env-helper usage' '
+ test_must_fail test-tool env-helper &&
+ test_must_fail test-tool env-helper --type=bool &&
+ test_must_fail test-tool env-helper --type=ulong &&
+ test_must_fail test-tool env-helper --type=bool &&
+ test_must_fail test-tool env-helper --type=bool --default &&
+ test_must_fail test-tool env-helper --type=bool --default= &&
+ test_must_fail test-tool env-helper --defaultxyz
+'
+
+test_expect_success 'test-tool env-helper bad default values' '
+ test_must_fail test-tool env-helper --type=bool --default=1xyz MISSING &&
+ test_must_fail test-tool env-helper --type=ulong --default=1xyz MISSING
+'
+
+test_expect_success 'test-tool env-helper --type=bool' '
+ # Test various --default bool values
+ echo true >expected &&
+ test-tool env-helper --type=bool --default=1 MISSING >actual &&
+ test_cmp expected actual &&
+ test-tool env-helper --type=bool --default=yes MISSING >actual &&
+ test_cmp expected actual &&
+ test-tool env-helper --type=bool --default=true MISSING >actual &&
+ test_cmp expected actual &&
+ echo false >expected &&
+ test_must_fail test-tool env-helper --type=bool --default=0 MISSING >actual &&
+ test_cmp expected actual &&
+ test_must_fail test-tool env-helper --type=bool --default=no MISSING >actual &&
+ test_cmp expected actual &&
+ test_must_fail test-tool env-helper --type=bool --default=false MISSING >actual &&
+ test_cmp expected actual &&
+
+ # No output with --exit-code
+ test-tool env-helper --type=bool --default=true --exit-code MISSING >actual.out 2>actual.err &&
+ test_must_be_empty actual.out &&
+ test_must_be_empty actual.err &&
+ test_must_fail test-tool env-helper --type=bool --default=false --exit-code MISSING >actual.out 2>actual.err &&
+ test_must_be_empty actual.out &&
+ test_must_be_empty actual.err &&
+
+ # Existing variable
+ EXISTS=true test-tool env-helper --type=bool --default=false --exit-code EXISTS >actual.out 2>actual.err &&
+ test_must_be_empty actual.out &&
+ test_must_be_empty actual.err &&
+ test_must_fail \
+ env EXISTS=false \
+ test-tool env-helper --type=bool --default=true --exit-code EXISTS >actual.out 2>actual.err &&
+ test_must_be_empty actual.out &&
+ test_must_be_empty actual.err
+'
+
+test_expect_success 'test-tool env-helper --type=ulong' '
+ echo 1234567890 >expected &&
+ test-tool env-helper --type=ulong --default=1234567890 MISSING >actual.out 2>actual.err &&
+ test_cmp expected actual.out &&
+ test_must_be_empty actual.err &&
+
+ echo 0 >expected &&
+ test_must_fail test-tool env-helper --type=ulong --default=0 MISSING >actual &&
+ test_cmp expected actual &&
+
+ test-tool env-helper --type=ulong --default=1234567890 --exit-code MISSING >actual.out 2>actual.err &&
+ test_must_be_empty actual.out &&
+ test_must_be_empty actual.err &&
+
+ EXISTS=1234567890 test-tool env-helper --type=ulong --default=0 EXISTS --exit-code >actual.out 2>actual.err &&
+ test_must_be_empty actual.out &&
+ test_must_be_empty actual.err &&
+
+ echo 1234567890 >expected &&
+ EXISTS=1234567890 test-tool env-helper --type=ulong --default=0 EXISTS >actual.out 2>actual.err &&
+ test_cmp expected actual.out &&
+ test_must_be_empty actual.err
+'
+
+test_expect_success 'test-tool env-helper reads config thanks to trace2' '
+ mkdir home &&
+ git config -f home/.gitconfig include.path cycle &&
+ git config -f home/cycle include.path .gitconfig &&
+
+ test_must_fail \
+ env HOME="$(pwd)/home" \
+ git config -l 2>err &&
+ grep "exceeded maximum include depth" err &&
+
+ test_must_fail \
+ env HOME="$(pwd)/home" GIT_TEST_ENV_HELPER=true \
+ test-tool -C cycle env-helper --type=bool --default=0 --exit-code GIT_TEST_ENV_HELPER 2>err &&
+ grep "exceeded maximum include depth" err
+'
+
+test_done
diff --git a/t/t0018-advice.sh b/t/t0018-advice.sh
new file mode 100755
index 0000000..c13057a
--- /dev/null
+++ b/t/t0018-advice.sh
@@ -0,0 +1,33 @@
+#!/bin/sh
+
+test_description='Test advise_if_enabled functionality'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'advice should be printed when config variable is unset' '
+ cat >expect <<-\EOF &&
+ hint: This is a piece of advice
+ hint: Disable this message with "git config advice.nestedTag false"
+ EOF
+ test-tool advise "This is a piece of advice" 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'advice should be printed when config variable is set to true' '
+ cat >expect <<-\EOF &&
+ hint: This is a piece of advice
+ hint: Disable this message with "git config advice.nestedTag false"
+ EOF
+ test_config advice.nestedTag true &&
+ test-tool advise "This is a piece of advice" 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'advice should not be printed when config variable is set to false' '
+ test_config advice.nestedTag false &&
+ test-tool advise "This is a piece of advice" 2>actual &&
+ test_must_be_empty actual
+'
+
+test_done
diff --git a/t/t0019-json-writer.sh b/t/t0019-json-writer.sh
new file mode 100755
index 0000000..19a730c
--- /dev/null
+++ b/t/t0019-json-writer.sh
@@ -0,0 +1,333 @@
+#!/bin/sh
+
+test_description='test json-writer JSON generation'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'unit test of json-writer routines' '
+ test-tool json-writer -u
+'
+
+test_expect_success 'trivial object' '
+ cat >expect <<-\EOF &&
+ {}
+ EOF
+ cat >input <<-\EOF &&
+ object
+ end
+ EOF
+ test-tool json-writer <input >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'trivial array' '
+ cat >expect <<-\EOF &&
+ []
+ EOF
+ cat >input <<-\EOF &&
+ array
+ end
+ EOF
+ test-tool json-writer <input >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'simple object' '
+ cat >expect <<-\EOF &&
+ {"a":"abc","b":42,"c":3.14,"d":true,"e":false,"f":null}
+ EOF
+ cat >input <<-\EOF &&
+ object
+ object-string a abc
+ object-int b 42
+ object-double c 2 3.140
+ object-true d
+ object-false e
+ object-null f
+ end
+ EOF
+ test-tool json-writer <input >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'simple array' '
+ cat >expect <<-\EOF &&
+ ["abc",42,3.14,true,false,null]
+ EOF
+ cat >input <<-\EOF &&
+ array
+ array-string abc
+ array-int 42
+ array-double 2 3.140
+ array-true
+ array-false
+ array-null
+ end
+ EOF
+ test-tool json-writer <input >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'escape quoting string' '
+ cat >expect <<-\EOF &&
+ {"a":"abc\\def"}
+ EOF
+ cat >input <<-\EOF &&
+ object
+ object-string a abc\def
+ end
+ EOF
+ test-tool json-writer <input >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'escape quoting string 2' '
+ cat >expect <<-\EOF &&
+ {"a":"abc\"def"}
+ EOF
+ cat >input <<-\EOF &&
+ object
+ object-string a abc"def
+ end
+ EOF
+ test-tool json-writer <input >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'nested inline object' '
+ cat >expect <<-\EOF &&
+ {"a":"abc","b":42,"sub1":{"c":3.14,"d":true,"sub2":{"e":false,"f":null}}}
+ EOF
+ cat >input <<-\EOF &&
+ object
+ object-string a abc
+ object-int b 42
+ object-object sub1
+ object-double c 2 3.140
+ object-true d
+ object-object sub2
+ object-false e
+ object-null f
+ end
+ end
+ end
+ EOF
+ test-tool json-writer <input >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'nested inline array' '
+ cat >expect <<-\EOF &&
+ ["abc",42,[3.14,true,[false,null]]]
+ EOF
+ cat >input <<-\EOF &&
+ array
+ array-string abc
+ array-int 42
+ array-array
+ array-double 2 3.140
+ array-true
+ array-array
+ array-false
+ array-null
+ end
+ end
+ end
+ EOF
+ test-tool json-writer <input >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'nested inline object and array' '
+ cat >expect <<-\EOF &&
+ {"a":"abc","b":42,"sub1":{"c":3.14,"d":true,"sub2":[false,null]}}
+ EOF
+ cat >input <<-\EOF &&
+ object
+ object-string a abc
+ object-int b 42
+ object-object sub1
+ object-double c 2 3.140
+ object-true d
+ object-array sub2
+ array-false
+ array-null
+ end
+ end
+ end
+ EOF
+ test-tool json-writer <input >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'nested inline object and array 2' '
+ cat >expect <<-\EOF &&
+ {"a":"abc","b":42,"sub1":{"c":3.14,"d":true,"sub2":[false,{"g":0,"h":1},null]}}
+ EOF
+ cat >input <<-\EOF &&
+ object
+ object-string a abc
+ object-int b 42
+ object-object sub1
+ object-double c 2 3.140
+ object-true d
+ object-array sub2
+ array-false
+ array-object
+ object-int g 0
+ object-int h 1
+ end
+ array-null
+ end
+ end
+ end
+ EOF
+ test-tool json-writer <input >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'pretty nested inline object and array 2' '
+ sed -e "s/^|//" >expect <<-\EOF &&
+ |{
+ | "a": "abc",
+ | "b": 42,
+ | "sub1": {
+ | "c": 3.14,
+ | "d": true,
+ | "sub2": [
+ | false,
+ | {
+ | "g": 0,
+ | "h": 1
+ | },
+ | null
+ | ]
+ | }
+ |}
+ EOF
+ cat >input <<-\EOF &&
+ object
+ object-string a abc
+ object-int b 42
+ object-object sub1
+ object-double c 2 3.140
+ object-true d
+ object-array sub2
+ array-false
+ array-object
+ object-int g 0
+ object-int h 1
+ end
+ array-null
+ end
+ end
+ end
+ EOF
+ test-tool json-writer -p <input >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'inline object with no members' '
+ cat >expect <<-\EOF &&
+ {"a":"abc","empty":{},"b":42}
+ EOF
+ cat >input <<-\EOF &&
+ object
+ object-string a abc
+ object-object empty
+ end
+ object-int b 42
+ end
+ EOF
+ test-tool json-writer <input >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'inline array with no members' '
+ cat >expect <<-\EOF &&
+ {"a":"abc","empty":[],"b":42}
+ EOF
+ cat >input <<-\EOF &&
+ object
+ object-string a abc
+ object-array empty
+ end
+ object-int b 42
+ end
+ EOF
+ test-tool json-writer <input >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'larger empty example' '
+ cat >expect <<-\EOF &&
+ {"a":"abc","empty":[{},{},{},[],{}],"b":42}
+ EOF
+ cat >input <<-\EOF &&
+ object
+ object-string a abc
+ object-array empty
+ array-object
+ end
+ array-object
+ end
+ array-object
+ end
+ array-array
+ end
+ array-object
+ end
+ end
+ object-int b 42
+ end
+ EOF
+ test-tool json-writer <input >actual &&
+ test_cmp expect actual
+'
+
+test_lazy_prereq PERLJSON '
+ perl -MJSON -e "exit 0"
+'
+
+# As a sanity check, ask Perl to parse our generated JSON and recursively
+# dump the resulting data in sorted order. Confirm that that matches our
+# expectations.
+test_expect_success PERLJSON 'parse JSON using Perl' '
+ cat >expect <<-\EOF &&
+ row[0].a abc
+ row[0].b 42
+ row[0].sub1 hash
+ row[0].sub1.c 3.14
+ row[0].sub1.d 1
+ row[0].sub1.sub2 array
+ row[0].sub1.sub2[0] 0
+ row[0].sub1.sub2[1] hash
+ row[0].sub1.sub2[1].g 0
+ row[0].sub1.sub2[1].h 1
+ row[0].sub1.sub2[2] null
+ EOF
+ cat >input <<-\EOF &&
+ object
+ object-string a abc
+ object-int b 42
+ object-object sub1
+ object-double c 2 3.140
+ object-true d
+ object-array sub2
+ array-false
+ array-object
+ object-int g 0
+ object-int h 1
+ end
+ array-null
+ end
+ end
+ end
+ EOF
+ test-tool json-writer <input >output.json &&
+ perl "$TEST_DIRECTORY"/t0019/parse_json.perl <output.json >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t0019/parse_json.perl b/t/t0019/parse_json.perl
new file mode 100644
index 0000000..fea87fb
--- /dev/null
+++ b/t/t0019/parse_json.perl
@@ -0,0 +1,55 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+use JSON;
+
+sub dump_array {
+ my ($label_in, $ary_ref) = @_;
+ my @ary = @$ary_ref;
+
+ for ( my $i = 0; $i <= $#{ $ary_ref }; $i++ )
+ {
+ my $label = "$label_in\[$i\]";
+ dump_item($label, $ary[$i]);
+ }
+}
+
+sub dump_hash {
+ my ($label_in, $obj_ref) = @_;
+ my %obj = %$obj_ref;
+
+ foreach my $k (sort keys %obj) {
+ my $label = (length($label_in) > 0) ? "$label_in.$k" : "$k";
+ my $value = $obj{$k};
+
+ dump_item($label, $value);
+ }
+}
+
+sub dump_item {
+ my ($label_in, $value) = @_;
+ if (ref($value) eq 'ARRAY') {
+ print "$label_in array\n";
+ dump_array($label_in, $value);
+ } elsif (ref($value) eq 'HASH') {
+ print "$label_in hash\n";
+ dump_hash($label_in, $value);
+ } elsif (ref $value) {
+ my $bool = $value ? 1 : 0;
+ print "$label_in $bool\n";
+ } elsif (defined $value) {
+ print "$label_in $value\n";
+ } else {
+ print "$label_in null\n";
+ }
+}
+
+my $row = 0;
+while (<>) {
+ my $data = decode_json( $_ );
+ my $label = "row[$row]";
+
+ dump_hash($label, $data);
+ $row++;
+}
+
diff --git a/t/t0020-crlf.sh b/t/t0020-crlf.sh
new file mode 100755
index 0000000..81946e8
--- /dev/null
+++ b/t/t0020-crlf.sh
@@ -0,0 +1,401 @@
+#!/bin/sh
+
+test_description='CRLF conversion'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+has_cr() {
+ tr '\015' Q <"$1" | grep Q >/dev/null
+}
+
+# add or remove CRs to disk file in-place
+# usage: munge_cr <append|remove> <file>
+munge_cr () {
+ "${1}_cr" <"$2" >tmp &&
+ mv tmp "$2"
+}
+
+test_expect_success setup '
+
+ git config core.autocrlf false &&
+
+ test_write_lines Hello world how are you >one &&
+ mkdir dir &&
+ test_write_lines I am very very fine thank you >dir/two &&
+ test_write_lines Oh here is NULQin text here | q_to_nul >three &&
+ git add . &&
+
+ git commit -m initial &&
+
+ one=$(git rev-parse HEAD:one) &&
+ dir=$(git rev-parse HEAD:dir) &&
+ two=$(git rev-parse HEAD:dir/two) &&
+ three=$(git rev-parse HEAD:three) &&
+
+ test_write_lines Some extra lines here >>one &&
+ git diff >patch.file &&
+ patched=$(git hash-object --stdin <one) &&
+ git read-tree --reset -u HEAD
+'
+
+test_expect_success 'safecrlf: autocrlf=input, all CRLF' '
+
+ git config core.autocrlf input &&
+ git config core.safecrlf true &&
+
+ test_write_lines I am all CRLF | append_cr >allcrlf &&
+ test_must_fail git add allcrlf
+'
+
+test_expect_success 'safecrlf: autocrlf=input, mixed LF/CRLF' '
+
+ git config core.autocrlf input &&
+ git config core.safecrlf true &&
+
+ test_write_lines Oh here is CRLFQ in text | q_to_cr >mixed &&
+ test_must_fail git add mixed
+'
+
+test_expect_success 'safecrlf: autocrlf=true, all LF' '
+
+ git config core.autocrlf true &&
+ git config core.safecrlf true &&
+
+ test_write_lines I am all LF >alllf &&
+ test_must_fail git add alllf
+'
+
+test_expect_success 'safecrlf: autocrlf=true mixed LF/CRLF' '
+
+ git config core.autocrlf true &&
+ git config core.safecrlf true &&
+
+ test_write_lines Oh here is CRLFQ in text | q_to_cr >mixed &&
+ test_must_fail git add mixed
+'
+
+test_expect_success 'safecrlf: print warning only once' '
+
+ git config core.autocrlf input &&
+ git config core.safecrlf warn &&
+
+ test_write_lines I am all LF >doublewarn &&
+ git add doublewarn &&
+ git commit -m "nowarn" &&
+ test_write_lines Oh here is CRLFQ in text | q_to_cr >doublewarn &&
+ git add doublewarn 2>err &&
+ grep "CRLF will be replaced by LF" err >err.warnings &&
+ test_line_count = 1 err.warnings
+'
+
+
+test_expect_success 'safecrlf: git diff demotes safecrlf=true to warn' '
+ git config core.autocrlf input &&
+ git config core.safecrlf true &&
+ git diff HEAD
+'
+
+
+test_expect_success 'safecrlf: no warning with safecrlf=false' '
+ git config core.autocrlf input &&
+ git config core.safecrlf false &&
+
+ test_write_lines I am all CRLF | append_cr >allcrlf &&
+ git add allcrlf 2>err &&
+ test_must_be_empty err
+'
+
+
+test_expect_success 'switch off autocrlf, safecrlf, reset HEAD' '
+ git config core.autocrlf false &&
+ git config core.safecrlf false &&
+ git reset --hard HEAD^
+'
+
+test_expect_success 'update with autocrlf=input' '
+
+ rm -f tmp one dir/two three &&
+ git read-tree --reset -u HEAD &&
+ git config core.autocrlf input &&
+ munge_cr append one &&
+ munge_cr append dir/two &&
+ git update-index -- one dir/two &&
+ differs=$(git diff-index --cached HEAD) &&
+ test -z "$differs"
+
+'
+
+test_expect_success 'update with autocrlf=true' '
+
+ rm -f tmp one dir/two three &&
+ git read-tree --reset -u HEAD &&
+ git config core.autocrlf true &&
+ munge_cr append one &&
+ munge_cr append dir/two &&
+ git update-index -- one dir/two &&
+ differs=$(git diff-index --cached HEAD) &&
+ test -z "$differs"
+
+'
+
+test_expect_success 'checkout with autocrlf=true' '
+
+ rm -f tmp one dir/two three &&
+ git config core.autocrlf true &&
+ git read-tree --reset -u HEAD &&
+ munge_cr remove one &&
+ munge_cr remove dir/two &&
+ git update-index -- one dir/two &&
+ test "$one" = $(git hash-object --stdin <one) &&
+ test "$two" = $(git hash-object --stdin <dir/two) &&
+ differs=$(git diff-index --cached HEAD) &&
+ test -z "$differs"
+'
+
+test_expect_success 'checkout with autocrlf=input' '
+
+ rm -f tmp one dir/two three &&
+ git config core.autocrlf input &&
+ git read-tree --reset -u HEAD &&
+ ! has_cr one &&
+ ! has_cr dir/two &&
+ git update-index -- one dir/two &&
+ test "$one" = $(git hash-object --stdin <one) &&
+ test "$two" = $(git hash-object --stdin <dir/two) &&
+ differs=$(git diff-index --cached HEAD) &&
+ test -z "$differs"
+'
+
+test_expect_success 'apply patch (autocrlf=input)' '
+
+ rm -f tmp one dir/two three &&
+ git config core.autocrlf input &&
+ git read-tree --reset -u HEAD &&
+
+ git apply patch.file &&
+ test "$patched" = "$(git hash-object --stdin <one)"
+'
+
+test_expect_success 'apply patch --cached (autocrlf=input)' '
+
+ rm -f tmp one dir/two three &&
+ git config core.autocrlf input &&
+ git read-tree --reset -u HEAD &&
+
+ git apply --cached patch.file &&
+ test "$patched" = $(git rev-parse :one)
+'
+
+test_expect_success 'apply patch --index (autocrlf=input)' '
+
+ rm -f tmp one dir/two three &&
+ git config core.autocrlf input &&
+ git read-tree --reset -u HEAD &&
+
+ git apply --index patch.file &&
+ test "$patched" = $(git rev-parse :one) &&
+ test "$patched" = $(git hash-object --stdin <one)
+'
+
+test_expect_success 'apply patch (autocrlf=true)' '
+
+ rm -f tmp one dir/two three &&
+ git config core.autocrlf true &&
+ git read-tree --reset -u HEAD &&
+
+ git apply patch.file &&
+ test "$patched" = "$(remove_cr <one | git hash-object --stdin)"
+'
+
+test_expect_success 'apply patch --cached (autocrlf=true)' '
+
+ rm -f tmp one dir/two three &&
+ git config core.autocrlf true &&
+ git read-tree --reset -u HEAD &&
+
+ git apply --cached patch.file &&
+ test "$patched" = $(git rev-parse :one)
+'
+
+test_expect_success 'apply patch --index (autocrlf=true)' '
+
+ rm -f tmp one dir/two three &&
+ git config core.autocrlf true &&
+ git read-tree --reset -u HEAD &&
+
+ git apply --index patch.file &&
+ test "$patched" = $(git rev-parse :one) &&
+ test "$patched" = "$(remove_cr <one | git hash-object --stdin)"
+'
+
+test_expect_success '.gitattributes says two is binary' '
+
+ rm -f tmp one dir/two three &&
+ echo "two -crlf" >.gitattributes &&
+ git config core.autocrlf true &&
+ git read-tree --reset -u HEAD &&
+
+ ! has_cr dir/two &&
+ has_cr one &&
+ ! has_cr three
+'
+
+test_expect_success '.gitattributes says two is input' '
+
+ rm -f tmp one dir/two three &&
+ echo "two crlf=input" >.gitattributes &&
+ git read-tree --reset -u HEAD &&
+
+ ! has_cr dir/two
+'
+
+test_expect_success '.gitattributes says two and three are text' '
+
+ rm -f tmp one dir/two three &&
+ echo "t* crlf" >.gitattributes &&
+ git read-tree --reset -u HEAD &&
+
+ has_cr dir/two &&
+ has_cr three
+'
+
+test_expect_success 'in-tree .gitattributes (1)' '
+
+ echo "one -crlf" >>.gitattributes &&
+ git add .gitattributes &&
+ git commit -m "Add .gitattributes" &&
+
+ rm -rf tmp one dir .gitattributes patch.file three &&
+ git read-tree --reset -u HEAD &&
+
+ ! has_cr one &&
+ has_cr three
+'
+
+test_expect_success 'in-tree .gitattributes (2)' '
+
+ rm -rf tmp one dir .gitattributes patch.file three &&
+ git read-tree --reset HEAD &&
+ git checkout-index -f -q -u -a &&
+
+ ! has_cr one &&
+ has_cr three
+'
+
+test_expect_success 'in-tree .gitattributes (3)' '
+
+ rm -rf tmp one dir .gitattributes patch.file three &&
+ git read-tree --reset HEAD &&
+ git checkout-index -u .gitattributes &&
+ git checkout-index -u one dir/two three &&
+
+ ! has_cr one &&
+ has_cr three
+'
+
+test_expect_success 'in-tree .gitattributes (4)' '
+
+ rm -rf tmp one dir .gitattributes patch.file three &&
+ git read-tree --reset HEAD &&
+ git checkout-index -u one dir/two three &&
+ git checkout-index -u .gitattributes &&
+
+ ! has_cr one &&
+ has_cr three
+'
+
+test_expect_success 'checkout with existing .gitattributes' '
+
+ git config core.autocrlf true &&
+ git config --unset core.safecrlf &&
+ echo ".file2 -crlfQ" | q_to_cr >> .gitattributes &&
+ git add .gitattributes &&
+ git commit -m initial &&
+ echo ".file -crlfQ" | q_to_cr >> .gitattributes &&
+ echo "contents" > .file &&
+ git add .gitattributes .file &&
+ git commit -m second &&
+
+ git checkout main~1 &&
+ git checkout main &&
+ test "$(git diff-files --raw)" = ""
+
+'
+
+test_expect_success 'checkout when deleting .gitattributes' '
+
+ git rm .gitattributes &&
+ echo "contentsQ" | q_to_cr > .file2 &&
+ git add .file2 &&
+ git commit -m third &&
+
+ git checkout main~1 &&
+ git checkout main &&
+ has_cr .file2
+
+'
+
+test_expect_success 'invalid .gitattributes (must not crash)' '
+
+ echo "three +crlf" >>.gitattributes &&
+ git diff
+
+'
+# Some more tests here to add new autocrlf functionality.
+# We want to have a known state here, so start a bit from scratch
+
+test_expect_success 'setting up for new autocrlf tests' '
+ git config core.autocrlf false &&
+ git config core.safecrlf false &&
+ rm -rf .????* * &&
+ test_write_lines I am all LF >alllf &&
+ test_write_lines Oh here is CRLFQ in text | q_to_cr >mixed &&
+ test_write_lines I am all CRLF | append_cr >allcrlf &&
+ git add -A . &&
+ git commit -m "alllf, allcrlf and mixed only" &&
+ git tag -a -m "message" autocrlf-checkpoint
+'
+
+test_expect_success 'report no change after setting autocrlf' '
+ git config core.autocrlf true &&
+ touch * &&
+ git diff --exit-code
+'
+
+test_expect_success 'files are clean after checkout' '
+ rm * &&
+ git checkout -f &&
+ git diff --exit-code
+'
+
+cr_to_Q_no_NL () {
+ tr '\015' Q | tr -d '\012'
+}
+
+test_expect_success 'LF only file gets CRLF with autocrlf' '
+ test "$(cr_to_Q_no_NL < alllf)" = "IQamQallQLFQ"
+'
+
+test_expect_success 'Mixed file is still mixed with autocrlf' '
+ test "$(cr_to_Q_no_NL < mixed)" = "OhhereisCRLFQintext"
+'
+
+test_expect_success 'CRLF only file has CRLF with autocrlf' '
+ test "$(cr_to_Q_no_NL < allcrlf)" = "IQamQallQCRLFQ"
+'
+
+test_expect_success 'New CRLF file gets LF in repo' '
+ tr -d "\015" < alllf | append_cr > alllf2 &&
+ git add alllf2 &&
+ git commit -m "alllf2 added" &&
+ git config core.autocrlf false &&
+ rm * &&
+ git checkout -f &&
+ test_cmp alllf alllf2
+'
+
+test_done
diff --git a/t/t0021-conversion.sh b/t/t0021-conversion.sh
new file mode 100755
index 0000000..0b49970
--- /dev/null
+++ b/t/t0021-conversion.sh
@@ -0,0 +1,1154 @@
+#!/bin/sh
+
+test_description='blob conversion via gitattributes'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-terminal.sh
+
+PATH=$PWD:$PATH
+TEST_ROOT="$(pwd)"
+
+write_script <<\EOF "$TEST_ROOT/rot13.sh"
+tr \
+ 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' \
+ 'nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM'
+EOF
+
+generate_random_characters () {
+ LEN=$1
+ NAME=$2
+ test-tool genrandom some-seed $LEN |
+ perl -pe "s/./chr((ord($&) % 26) + ord('a'))/sge" >"$TEST_ROOT/$NAME"
+}
+
+filter_git () {
+ rm -f *.log &&
+ git "$@"
+}
+
+# Compare two files and ensure that `clean` and `smudge` respectively are
+# called at least once if specified in the `expect` file. The actual
+# invocation count is not relevant because their number can vary.
+# c.f. https://lore.kernel.org/git/xmqqshv18i8i.fsf@gitster.mtv.corp.google.com/
+test_cmp_count () {
+ expect=$1
+ actual=$2
+ for FILE in "$expect" "$actual"
+ do
+ sort "$FILE" | uniq -c |
+ sed -e "s/^ *[0-9][0-9]*[ ]*IN: /x IN: /" >"$FILE.tmp"
+ done &&
+ test_cmp "$expect.tmp" "$actual.tmp" &&
+ rm "$expect.tmp" "$actual.tmp"
+}
+
+# Compare two files but exclude all `clean` invocations because Git can
+# call `clean` zero or more times.
+# c.f. https://lore.kernel.org/git/xmqqshv18i8i.fsf@gitster.mtv.corp.google.com/
+test_cmp_exclude_clean () {
+ expect=$1
+ actual=$2
+ for FILE in "$expect" "$actual"
+ do
+ grep -v "IN: clean" "$FILE" >"$FILE.tmp"
+ done &&
+ test_cmp "$expect.tmp" "$actual.tmp" &&
+ rm "$expect.tmp" "$actual.tmp"
+}
+
+# Check that the contents of two files are equal and that their rot13 version
+# is equal to the committed content.
+test_cmp_committed_rot13 () {
+ test_cmp "$1" "$2" &&
+ rot13.sh <"$1" >expected &&
+ git cat-file blob :"$2" >actual &&
+ test_cmp expected actual
+}
+
+test_expect_success setup '
+ git config filter.rot13.smudge ./rot13.sh &&
+ git config filter.rot13.clean ./rot13.sh &&
+
+ {
+ echo "*.t filter=rot13" &&
+ echo "*.i ident"
+ } >.gitattributes &&
+
+ {
+ echo a b c d e f g h i j k l m &&
+ echo n o p q r s t u v w x y z &&
+ echo '\''$Id$'\''
+ } >test &&
+ cat test >test.t &&
+ cat test >test.o &&
+ cat test >test.i &&
+ git add test test.t test.i &&
+ rm -f test test.t test.i &&
+ git checkout -- test test.t test.i &&
+
+ echo "content-test2" >test2.o &&
+ echo "content-test3 - filename with special characters" >"test3 '\''sq'\'',\$x=.o"
+'
+
+script='s/^\$Id: \([0-9a-f]*\) \$/\1/p'
+
+test_expect_success check '
+
+ test_cmp test.o test &&
+ test_cmp test.o test.t &&
+
+ # ident should be stripped in the repository
+ git diff --raw --exit-code :test :test.i &&
+ id=$(git rev-parse --verify :test) &&
+ embedded=$(sed -ne "$script" test.i) &&
+ test "z$id" = "z$embedded" &&
+
+ git cat-file blob :test.t >test.r &&
+
+ ./rot13.sh <test.o >test.t &&
+ test_cmp test.r test.t
+'
+
+# If an expanded ident ever gets into the repository, we want to make sure that
+# it is collapsed before being expanded again on checkout
+test_expect_success expanded_in_repo '
+ cat >expanded-keywords.0 <<-\EOF &&
+ File with expanded keywords
+ $Id$
+ $Id:$
+ $Id: 0000000000000000000000000000000000000000 $
+ $Id: NoSpaceAtEnd$
+ $Id:NoSpaceAtFront $
+ $Id:NoSpaceAtEitherEnd$
+ $Id: NoTerminatingSymbol
+ $Id: Foreign Commit With Spaces $
+ EOF
+
+ {
+ cat expanded-keywords.0 &&
+ printf "\$Id: NoTerminatingSymbolAtEOF"
+ } >expanded-keywords &&
+ cat expanded-keywords >expanded-keywords-crlf &&
+ git add expanded-keywords expanded-keywords-crlf &&
+ git commit -m "File with keywords expanded" &&
+ id=$(git rev-parse --verify :expanded-keywords) &&
+
+ cat >expected-output.0 <<-EOF &&
+ File with expanded keywords
+ \$Id: $id \$
+ \$Id: $id \$
+ \$Id: $id \$
+ \$Id: $id \$
+ \$Id: $id \$
+ \$Id: $id \$
+ \$Id: NoTerminatingSymbol
+ \$Id: Foreign Commit With Spaces \$
+ EOF
+ {
+ cat expected-output.0 &&
+ printf "\$Id: NoTerminatingSymbolAtEOF"
+ } >expected-output &&
+ {
+ append_cr <expected-output.0 &&
+ printf "\$Id: NoTerminatingSymbolAtEOF"
+ } >expected-output-crlf &&
+ {
+ echo "expanded-keywords ident" &&
+ echo "expanded-keywords-crlf ident text eol=crlf"
+ } >>.gitattributes &&
+
+ rm -f expanded-keywords expanded-keywords-crlf &&
+
+ git checkout -- expanded-keywords &&
+ test_cmp expected-output expanded-keywords &&
+
+ git checkout -- expanded-keywords-crlf &&
+ test_cmp expected-output-crlf expanded-keywords-crlf
+'
+
+# The use of %f in a filter definition is expanded to the path to
+# the filename being smudged or cleaned. It must be shell escaped.
+# First, set up some interesting file names and pet them in
+# .gitattributes.
+test_expect_success 'filter shell-escaped filenames' '
+ cat >argc.sh <<-EOF &&
+ #!$SHELL_PATH
+ cat >/dev/null
+ echo argc: \$# "\$@"
+ EOF
+ normal=name-no-magic &&
+ special="name with '\''sq'\'' and \$x" &&
+ echo some test text >"$normal" &&
+ echo some test text >"$special" &&
+ git add "$normal" "$special" &&
+ git commit -q -m "add files" &&
+ echo "name* filter=argc" >.gitattributes &&
+
+ # delete the files and check them out again, using a smudge filter
+ # that will count the args and echo the command-line back to us
+ test_config filter.argc.smudge "sh ./argc.sh %f" &&
+ rm "$normal" "$special" &&
+ git checkout -- "$normal" "$special" &&
+
+ # make sure argc.sh counted the right number of args
+ echo "argc: 1 $normal" >expect &&
+ test_cmp expect "$normal" &&
+ echo "argc: 1 $special" >expect &&
+ test_cmp expect "$special" &&
+
+ # do the same thing, but with more args in the filter expression
+ test_config filter.argc.smudge "sh ./argc.sh %f --my-extra-arg" &&
+ rm "$normal" "$special" &&
+ git checkout -- "$normal" "$special" &&
+
+ # make sure argc.sh counted the right number of args
+ echo "argc: 2 $normal --my-extra-arg" >expect &&
+ test_cmp expect "$normal" &&
+ echo "argc: 2 $special --my-extra-arg" >expect &&
+ test_cmp expect "$special" &&
+ :
+'
+
+test_expect_success 'required filter should filter data' '
+ test_config filter.required.smudge ./rot13.sh &&
+ test_config filter.required.clean ./rot13.sh &&
+ test_config filter.required.required true &&
+
+ echo "*.r filter=required" >.gitattributes &&
+
+ cat test.o >test.r &&
+ git add test.r &&
+
+ rm -f test.r &&
+ git checkout -- test.r &&
+ test_cmp test.o test.r &&
+
+ ./rot13.sh <test.o >expected &&
+ git cat-file blob :test.r >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'required filter smudge failure' '
+ test_config filter.failsmudge.smudge false &&
+ test_config filter.failsmudge.clean cat &&
+ test_config filter.failsmudge.required true &&
+
+ echo "*.fs filter=failsmudge" >.gitattributes &&
+
+ echo test >test.fs &&
+ git add test.fs &&
+ rm -f test.fs &&
+ test_must_fail git checkout -- test.fs
+'
+
+test_expect_success 'required filter clean failure' '
+ test_config filter.failclean.smudge cat &&
+ test_config filter.failclean.clean false &&
+ test_config filter.failclean.required true &&
+
+ echo "*.fc filter=failclean" >.gitattributes &&
+
+ echo test >test.fc &&
+ test_must_fail git add test.fc
+'
+
+test_expect_success 'required filter with absent clean field' '
+ test_config filter.absentclean.smudge cat &&
+ test_config filter.absentclean.required true &&
+
+ echo "*.ac filter=absentclean" >.gitattributes &&
+
+ echo test >test.ac &&
+ test_must_fail git add test.ac 2>stderr &&
+ test_grep "fatal: test.ac: clean filter .absentclean. failed" stderr
+'
+
+test_expect_success 'required filter with absent smudge field' '
+ test_config filter.absentsmudge.clean cat &&
+ test_config filter.absentsmudge.required true &&
+
+ echo "*.as filter=absentsmudge" >.gitattributes &&
+
+ echo test >test.as &&
+ git add test.as &&
+ rm -f test.as &&
+ test_must_fail git checkout -- test.as 2>stderr &&
+ test_grep "fatal: test.as: smudge filter absentsmudge failed" stderr
+'
+
+test_expect_success 'filtering large input to small output should use little memory' '
+ test_config filter.devnull.clean "cat >/dev/null" &&
+ test_config filter.devnull.required true &&
+ for i in $(test_seq 1 30); do printf "%1048576d" 1 || return 1; done >30MB &&
+ echo "30MB filter=devnull" >.gitattributes &&
+ GIT_MMAP_LIMIT=1m GIT_ALLOC_LIMIT=1m git add 30MB
+'
+
+test_expect_success 'filter that does not read is fine' '
+ test-tool genrandom foo $((128 * 1024 + 1)) >big &&
+ echo "big filter=epipe" >.gitattributes &&
+ test_config filter.epipe.clean "echo xyzzy" &&
+ git add big &&
+ git cat-file blob :big >actual &&
+ echo xyzzy >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success EXPENSIVE 'filter large file' '
+ test_config filter.largefile.smudge cat &&
+ test_config filter.largefile.clean cat &&
+ for i in $(test_seq 1 2048); do printf "%1048576d" 1 || return 1; done >2GB &&
+ echo "2GB filter=largefile" >.gitattributes &&
+ git add 2GB 2>err &&
+ test_must_be_empty err &&
+ rm -f 2GB &&
+ git checkout -- 2GB 2>err &&
+ test_must_be_empty err
+'
+
+test_expect_success "filter: clean empty file" '
+ test_config filter.in-repo-header.clean "echo cleaned && cat" &&
+ test_config filter.in-repo-header.smudge "sed 1d" &&
+
+ echo "empty-in-worktree filter=in-repo-header" >>.gitattributes &&
+ >empty-in-worktree &&
+
+ echo cleaned >expected &&
+ git add empty-in-worktree &&
+ git show :empty-in-worktree >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success "filter: smudge empty file" '
+ test_config filter.empty-in-repo.clean "cat >/dev/null" &&
+ test_config filter.empty-in-repo.smudge "echo smudged && cat" &&
+
+ echo "empty-in-repo filter=empty-in-repo" >>.gitattributes &&
+ echo dead data walking >empty-in-repo &&
+ git add empty-in-repo &&
+
+ echo smudged >expected &&
+ git checkout-index --prefix=filtered- empty-in-repo &&
+ test_cmp expected filtered-empty-in-repo
+'
+
+test_expect_success 'disable filter with empty override' '
+ test_config_global filter.disable.smudge false &&
+ test_config_global filter.disable.clean false &&
+ test_config filter.disable.smudge false &&
+ test_config filter.disable.clean false &&
+
+ echo "*.disable filter=disable" >.gitattributes &&
+
+ echo test >test.disable &&
+ git -c filter.disable.clean= add test.disable 2>err &&
+ test_must_be_empty err &&
+ rm -f test.disable &&
+ git -c filter.disable.smudge= checkout -- test.disable 2>err &&
+ test_must_be_empty err
+'
+
+test_expect_success 'diff does not reuse worktree files that need cleaning' '
+ test_config filter.counter.clean "echo . >>count; sed s/^/clean:/" &&
+ echo "file filter=counter" >.gitattributes &&
+ test_commit one file &&
+ test_commit two file &&
+
+ >count &&
+ git diff-tree -p HEAD &&
+ test_line_count = 0 count
+'
+
+test_expect_success 'required process filter should filter data' '
+ test_config_global filter.protocol.process "test-tool rot13-filter --log=debug.log clean smudge" &&
+ test_config_global filter.protocol.required true &&
+ rm -rf repo &&
+ mkdir repo &&
+ (
+ cd repo &&
+ git init &&
+
+ echo "*.r filter=protocol" >.gitattributes &&
+ git add . &&
+ git commit -m "test commit 1" &&
+ git branch empty-branch &&
+
+ cp "$TEST_ROOT/test.o" test.r &&
+ cp "$TEST_ROOT/test2.o" test2.r &&
+ mkdir testsubdir &&
+ cp "$TEST_ROOT/test3 '\''sq'\'',\$x=.o" "testsubdir/test3 '\''sq'\'',\$x=.r" &&
+ >test4-empty.r &&
+
+ S=$(test_file_size test.r) &&
+ S2=$(test_file_size test2.r) &&
+ S3=$(test_file_size "testsubdir/test3 '\''sq'\'',\$x=.r") &&
+ M=$(git hash-object test.r) &&
+ M2=$(git hash-object test2.r) &&
+ M3=$(git hash-object "testsubdir/test3 '\''sq'\'',\$x=.r") &&
+ EMPTY=$(git hash-object /dev/null) &&
+
+ filter_git add . &&
+ cat >expected.log <<-EOF &&
+ START
+ init handshake complete
+ IN: clean test.r $S [OK] -- OUT: $S . [OK]
+ IN: clean test2.r $S2 [OK] -- OUT: $S2 . [OK]
+ IN: clean test4-empty.r 0 [OK] -- OUT: 0 [OK]
+ IN: clean testsubdir/test3 '\''sq'\'',\$x=.r $S3 [OK] -- OUT: $S3 . [OK]
+ STOP
+ EOF
+ test_cmp_count expected.log debug.log &&
+
+ git commit -m "test commit 2" &&
+ MAIN=$(git rev-parse --verify main) &&
+ META="ref=refs/heads/main treeish=$MAIN" &&
+ rm -f test2.r "testsubdir/test3 '\''sq'\'',\$x=.r" &&
+
+ filter_git checkout --quiet --no-progress . &&
+ cat >expected.log <<-EOF &&
+ START
+ init handshake complete
+ IN: smudge test2.r blob=$M2 $S2 [OK] -- OUT: $S2 . [OK]
+ IN: smudge testsubdir/test3 '\''sq'\'',\$x=.r blob=$M3 $S3 [OK] -- OUT: $S3 . [OK]
+ STOP
+ EOF
+ test_cmp_exclude_clean expected.log debug.log &&
+
+ # Make sure that the file appears dirty, so checkout below has to
+ # run the configured filter.
+ touch test.r &&
+ filter_git checkout --quiet --no-progress empty-branch &&
+ cat >expected.log <<-EOF &&
+ START
+ init handshake complete
+ IN: clean test.r $S [OK] -- OUT: $S . [OK]
+ STOP
+ EOF
+ test_cmp_exclude_clean expected.log debug.log &&
+
+ filter_git checkout --quiet --no-progress main &&
+ cat >expected.log <<-EOF &&
+ START
+ init handshake complete
+ IN: smudge test.r $META blob=$M $S [OK] -- OUT: $S . [OK]
+ IN: smudge test2.r $META blob=$M2 $S2 [OK] -- OUT: $S2 . [OK]
+ IN: smudge test4-empty.r $META blob=$EMPTY 0 [OK] -- OUT: 0 [OK]
+ IN: smudge testsubdir/test3 '\''sq'\'',\$x=.r $META blob=$M3 $S3 [OK] -- OUT: $S3 . [OK]
+ STOP
+ EOF
+ test_cmp_exclude_clean expected.log debug.log &&
+
+ test_cmp_committed_rot13 "$TEST_ROOT/test.o" test.r &&
+ test_cmp_committed_rot13 "$TEST_ROOT/test2.o" test2.r &&
+ test_cmp_committed_rot13 "$TEST_ROOT/test3 '\''sq'\'',\$x=.o" "testsubdir/test3 '\''sq'\'',\$x=.r"
+ )
+'
+
+test_expect_success 'required process filter should filter data for various subcommands' '
+ test_config_global filter.protocol.process "test-tool rot13-filter --log=debug.log clean smudge" &&
+ test_config_global filter.protocol.required true &&
+ (
+ cd repo &&
+
+ S=$(test_file_size test.r) &&
+ S2=$(test_file_size test2.r) &&
+ S3=$(test_file_size "testsubdir/test3 '\''sq'\'',\$x=.r") &&
+ M=$(git hash-object test.r) &&
+ M2=$(git hash-object test2.r) &&
+ M3=$(git hash-object "testsubdir/test3 '\''sq'\'',\$x=.r") &&
+ EMPTY=$(git hash-object /dev/null) &&
+
+ MAIN=$(git rev-parse --verify main) &&
+
+ cp "$TEST_ROOT/test.o" test5.r &&
+ git add test5.r &&
+ git commit -m "test commit 3" &&
+ git checkout empty-branch &&
+ filter_git rebase --onto empty-branch main^^ main &&
+ MAIN2=$(git rev-parse --verify main) &&
+ META="ref=refs/heads/main treeish=$MAIN2" &&
+ cat >expected.log <<-EOF &&
+ START
+ init handshake complete
+ IN: smudge test.r $META blob=$M $S [OK] -- OUT: $S . [OK]
+ IN: smudge test2.r $META blob=$M2 $S2 [OK] -- OUT: $S2 . [OK]
+ IN: smudge test4-empty.r $META blob=$EMPTY 0 [OK] -- OUT: 0 [OK]
+ IN: smudge test5.r $META blob=$M $S [OK] -- OUT: $S . [OK]
+ IN: smudge testsubdir/test3 '\''sq'\'',\$x=.r $META blob=$M3 $S3 [OK] -- OUT: $S3 . [OK]
+ STOP
+ EOF
+ test_cmp_exclude_clean expected.log debug.log &&
+
+ git reset --hard empty-branch &&
+ filter_git reset --hard $MAIN &&
+ META="treeish=$MAIN" &&
+ cat >expected.log <<-EOF &&
+ START
+ init handshake complete
+ IN: smudge test.r $META blob=$M $S [OK] -- OUT: $S . [OK]
+ IN: smudge test2.r $META blob=$M2 $S2 [OK] -- OUT: $S2 . [OK]
+ IN: smudge test4-empty.r $META blob=$EMPTY 0 [OK] -- OUT: 0 [OK]
+ IN: smudge testsubdir/test3 '\''sq'\'',\$x=.r $META blob=$M3 $S3 [OK] -- OUT: $S3 . [OK]
+ STOP
+ EOF
+ test_cmp_exclude_clean expected.log debug.log &&
+
+ git branch old-main $MAIN &&
+ git reset --hard empty-branch &&
+ filter_git reset --hard old-main &&
+ META="ref=refs/heads/old-main treeish=$MAIN" &&
+ cat >expected.log <<-EOF &&
+ START
+ init handshake complete
+ IN: smudge test.r $META blob=$M $S [OK] -- OUT: $S . [OK]
+ IN: smudge test2.r $META blob=$M2 $S2 [OK] -- OUT: $S2 . [OK]
+ IN: smudge test4-empty.r $META blob=$EMPTY 0 [OK] -- OUT: 0 [OK]
+ IN: smudge testsubdir/test3 '\''sq'\'',\$x=.r $META blob=$M3 $S3 [OK] -- OUT: $S3 . [OK]
+ STOP
+ EOF
+ test_cmp_exclude_clean expected.log debug.log &&
+
+ git checkout -b merge empty-branch &&
+ git branch -f main $MAIN2 &&
+ filter_git merge main &&
+ META="treeish=$MAIN2" &&
+ cat >expected.log <<-EOF &&
+ START
+ init handshake complete
+ IN: smudge test.r $META blob=$M $S [OK] -- OUT: $S . [OK]
+ IN: smudge test2.r $META blob=$M2 $S2 [OK] -- OUT: $S2 . [OK]
+ IN: smudge test4-empty.r $META blob=$EMPTY 0 [OK] -- OUT: 0 [OK]
+ IN: smudge test5.r $META blob=$M $S [OK] -- OUT: $S . [OK]
+ IN: smudge testsubdir/test3 '\''sq'\'',\$x=.r $META blob=$M3 $S3 [OK] -- OUT: $S3 . [OK]
+ STOP
+ EOF
+ test_cmp_exclude_clean expected.log debug.log &&
+
+ filter_git archive main >/dev/null &&
+ META="ref=refs/heads/main treeish=$MAIN2" &&
+ cat >expected.log <<-EOF &&
+ START
+ init handshake complete
+ IN: smudge test.r $META blob=$M $S [OK] -- OUT: $S . [OK]
+ IN: smudge test2.r $META blob=$M2 $S2 [OK] -- OUT: $S2 . [OK]
+ IN: smudge test4-empty.r $META blob=$EMPTY 0 [OK] -- OUT: 0 [OK]
+ IN: smudge test5.r $META blob=$M $S [OK] -- OUT: $S . [OK]
+ IN: smudge testsubdir/test3 '\''sq'\'',\$x=.r $META blob=$M3 $S3 [OK] -- OUT: $S3 . [OK]
+ STOP
+ EOF
+ test_cmp_exclude_clean expected.log debug.log &&
+
+ TREE="$(git rev-parse $MAIN2^{tree})" &&
+ filter_git archive $TREE >/dev/null &&
+ META="treeish=$TREE" &&
+ cat >expected.log <<-EOF &&
+ START
+ init handshake complete
+ IN: smudge test.r $META blob=$M $S [OK] -- OUT: $S . [OK]
+ IN: smudge test2.r $META blob=$M2 $S2 [OK] -- OUT: $S2 . [OK]
+ IN: smudge test4-empty.r $META blob=$EMPTY 0 [OK] -- OUT: 0 [OK]
+ IN: smudge test5.r $META blob=$M $S [OK] -- OUT: $S . [OK]
+ IN: smudge testsubdir/test3 '\''sq'\'',\$x=.r $META blob=$M3 $S3 [OK] -- OUT: $S3 . [OK]
+ STOP
+ EOF
+ test_cmp_exclude_clean expected.log debug.log
+ )
+'
+
+test_expect_success 'required process filter takes precedence' '
+ test_config_global filter.protocol.clean false &&
+ test_config_global filter.protocol.process "test-tool rot13-filter --log=debug.log clean" &&
+ test_config_global filter.protocol.required true &&
+ rm -rf repo &&
+ mkdir repo &&
+ (
+ cd repo &&
+ git init &&
+
+ echo "*.r filter=protocol" >.gitattributes &&
+ cp "$TEST_ROOT/test.o" test.r &&
+ S=$(test_file_size test.r) &&
+
+ # Check that the process filter is invoked here
+ filter_git add . &&
+ cat >expected.log <<-EOF &&
+ START
+ init handshake complete
+ IN: clean test.r $S [OK] -- OUT: $S . [OK]
+ STOP
+ EOF
+ test_cmp_count expected.log debug.log
+ )
+'
+
+test_expect_success 'required process filter should be used only for "clean" operation only' '
+ test_config_global filter.protocol.process "test-tool rot13-filter --log=debug.log clean" &&
+ rm -rf repo &&
+ mkdir repo &&
+ (
+ cd repo &&
+ git init &&
+
+ echo "*.r filter=protocol" >.gitattributes &&
+ cp "$TEST_ROOT/test.o" test.r &&
+ S=$(test_file_size test.r) &&
+
+ filter_git add . &&
+ cat >expected.log <<-EOF &&
+ START
+ init handshake complete
+ IN: clean test.r $S [OK] -- OUT: $S . [OK]
+ STOP
+ EOF
+ test_cmp_count expected.log debug.log &&
+
+ rm test.r &&
+
+ filter_git checkout --quiet --no-progress . &&
+ # If the filter would be used for "smudge", too, we would see
+ # "IN: smudge test.r 57 [OK] -- OUT: 57 . [OK]" here
+ cat >expected.log <<-EOF &&
+ START
+ init handshake complete
+ STOP
+ EOF
+ test_cmp_exclude_clean expected.log debug.log
+ )
+'
+
+test_expect_success 'required process filter should process multiple packets' '
+ test_config_global filter.protocol.process "test-tool rot13-filter --log=debug.log clean smudge" &&
+ test_config_global filter.protocol.required true &&
+
+ rm -rf repo &&
+ mkdir repo &&
+ (
+ cd repo &&
+ git init &&
+
+ # Generate data requiring 1, 2, 3 packets
+ S=65516 && # PKTLINE_DATA_MAXLEN -> Maximal size of a packet
+ generate_random_characters $(($S )) 1pkt_1__.file &&
+ generate_random_characters $(($S +1)) 2pkt_1+1.file &&
+ generate_random_characters $(($S*2-1)) 2pkt_2-1.file &&
+ generate_random_characters $(($S*2 )) 2pkt_2__.file &&
+ generate_random_characters $(($S*2+1)) 3pkt_2+1.file &&
+
+ for FILE in "$TEST_ROOT"/*.file
+ do
+ cp "$FILE" . &&
+ rot13.sh <"$FILE" >"$FILE.rot13" || return 1
+ done &&
+
+ echo "*.file filter=protocol" >.gitattributes &&
+ filter_git add *.file .gitattributes &&
+ cat >expected.log <<-EOF &&
+ START
+ init handshake complete
+ IN: clean 1pkt_1__.file $(($S )) [OK] -- OUT: $(($S )) . [OK]
+ IN: clean 2pkt_1+1.file $(($S +1)) [OK] -- OUT: $(($S +1)) .. [OK]
+ IN: clean 2pkt_2-1.file $(($S*2-1)) [OK] -- OUT: $(($S*2-1)) .. [OK]
+ IN: clean 2pkt_2__.file $(($S*2 )) [OK] -- OUT: $(($S*2 )) .. [OK]
+ IN: clean 3pkt_2+1.file $(($S*2+1)) [OK] -- OUT: $(($S*2+1)) ... [OK]
+ STOP
+ EOF
+ test_cmp_count expected.log debug.log &&
+
+ M1="blob=$(git hash-object 1pkt_1__.file)" &&
+ M2="blob=$(git hash-object 2pkt_1+1.file)" &&
+ M3="blob=$(git hash-object 2pkt_2-1.file)" &&
+ M4="blob=$(git hash-object 2pkt_2__.file)" &&
+ M5="blob=$(git hash-object 3pkt_2+1.file)" &&
+ rm -f *.file debug.log &&
+
+ filter_git checkout --quiet --no-progress -- *.file &&
+ cat >expected.log <<-EOF &&
+ START
+ init handshake complete
+ IN: smudge 1pkt_1__.file $M1 $(($S )) [OK] -- OUT: $(($S )) . [OK]
+ IN: smudge 2pkt_1+1.file $M2 $(($S +1)) [OK] -- OUT: $(($S +1)) .. [OK]
+ IN: smudge 2pkt_2-1.file $M3 $(($S*2-1)) [OK] -- OUT: $(($S*2-1)) .. [OK]
+ IN: smudge 2pkt_2__.file $M4 $(($S*2 )) [OK] -- OUT: $(($S*2 )) .. [OK]
+ IN: smudge 3pkt_2+1.file $M5 $(($S*2+1)) [OK] -- OUT: $(($S*2+1)) ... [OK]
+ STOP
+ EOF
+ test_cmp_exclude_clean expected.log debug.log &&
+
+ for FILE in *.file
+ do
+ test_cmp_committed_rot13 "$TEST_ROOT/$FILE" $FILE || return 1
+ done
+ )
+'
+
+test_expect_success 'required process filter with clean error should fail' '
+ test_config_global filter.protocol.process "test-tool rot13-filter --log=debug.log clean smudge" &&
+ test_config_global filter.protocol.required true &&
+ rm -rf repo &&
+ mkdir repo &&
+ (
+ cd repo &&
+ git init &&
+
+ echo "*.r filter=protocol" >.gitattributes &&
+
+ cp "$TEST_ROOT/test.o" test.r &&
+ echo "this is going to fail" >clean-write-fail.r &&
+ echo "content-test3-subdir" >test3.r &&
+
+ test_must_fail git add .
+ )
+'
+
+test_expect_success 'process filter should restart after unexpected write failure' '
+ test_config_global filter.protocol.process "test-tool rot13-filter --log=debug.log clean smudge" &&
+ rm -rf repo &&
+ mkdir repo &&
+ (
+ cd repo &&
+ git init &&
+
+ echo "*.r filter=protocol" >.gitattributes &&
+
+ cp "$TEST_ROOT/test.o" test.r &&
+ cp "$TEST_ROOT/test2.o" test2.r &&
+ echo "this is going to fail" >smudge-write-fail.o &&
+ cp smudge-write-fail.o smudge-write-fail.r &&
+
+ S=$(test_file_size test.r) &&
+ S2=$(test_file_size test2.r) &&
+ SF=$(test_file_size smudge-write-fail.r) &&
+ M=$(git hash-object test.r) &&
+ M2=$(git hash-object test2.r) &&
+ MF=$(git hash-object smudge-write-fail.r) &&
+ rm -f debug.log &&
+
+ git add . &&
+ rm -f *.r &&
+
+ rm -f debug.log &&
+ git checkout --quiet --no-progress . 2>git-stderr.log &&
+
+ grep "smudge write error" git-stderr.log &&
+ test_grep "error: external filter" git-stderr.log &&
+
+ cat >expected.log <<-EOF &&
+ START
+ init handshake complete
+ IN: smudge smudge-write-fail.r blob=$MF $SF [OK] -- [WRITE FAIL]
+ START
+ init handshake complete
+ IN: smudge test.r blob=$M $S [OK] -- OUT: $S . [OK]
+ IN: smudge test2.r blob=$M2 $S2 [OK] -- OUT: $S2 . [OK]
+ STOP
+ EOF
+ test_cmp_exclude_clean expected.log debug.log &&
+
+ test_cmp_committed_rot13 "$TEST_ROOT/test.o" test.r &&
+ test_cmp_committed_rot13 "$TEST_ROOT/test2.o" test2.r &&
+
+ # Smudge failed
+ ! test_cmp smudge-write-fail.o smudge-write-fail.r &&
+ rot13.sh <smudge-write-fail.o >expected &&
+ git cat-file blob :smudge-write-fail.r >actual &&
+ test_cmp expected actual
+ )
+'
+
+test_expect_success 'process filter should not be restarted if it signals an error' '
+ test_config_global filter.protocol.process "test-tool rot13-filter --log=debug.log clean smudge" &&
+ rm -rf repo &&
+ mkdir repo &&
+ (
+ cd repo &&
+ git init &&
+
+ echo "*.r filter=protocol" >.gitattributes &&
+
+ cp "$TEST_ROOT/test.o" test.r &&
+ cp "$TEST_ROOT/test2.o" test2.r &&
+ echo "this will cause an error" >error.o &&
+ cp error.o error.r &&
+
+ S=$(test_file_size test.r) &&
+ S2=$(test_file_size test2.r) &&
+ SE=$(test_file_size error.r) &&
+ M=$(git hash-object test.r) &&
+ M2=$(git hash-object test2.r) &&
+ ME=$(git hash-object error.r) &&
+ rm -f debug.log &&
+
+ git add . &&
+ rm -f *.r &&
+
+ filter_git checkout --quiet --no-progress . &&
+ cat >expected.log <<-EOF &&
+ START
+ init handshake complete
+ IN: smudge error.r blob=$ME $SE [OK] -- [ERROR]
+ IN: smudge test.r blob=$M $S [OK] -- OUT: $S . [OK]
+ IN: smudge test2.r blob=$M2 $S2 [OK] -- OUT: $S2 . [OK]
+ STOP
+ EOF
+ test_cmp_exclude_clean expected.log debug.log &&
+
+ test_cmp_committed_rot13 "$TEST_ROOT/test.o" test.r &&
+ test_cmp_committed_rot13 "$TEST_ROOT/test2.o" test2.r &&
+ test_cmp error.o error.r
+ )
+'
+
+test_expect_success 'process filter abort stops processing of all further files' '
+ test_config_global filter.protocol.process "test-tool rot13-filter --log=debug.log clean smudge" &&
+ rm -rf repo &&
+ mkdir repo &&
+ (
+ cd repo &&
+ git init &&
+
+ echo "*.r filter=protocol" >.gitattributes &&
+
+ cp "$TEST_ROOT/test.o" test.r &&
+ cp "$TEST_ROOT/test2.o" test2.r &&
+ echo "error this blob and all future blobs" >abort.o &&
+ cp abort.o abort.r &&
+
+ M="blob=$(git hash-object abort.r)" &&
+ rm -f debug.log &&
+ SA=$(test_file_size abort.r) &&
+
+ git add . &&
+ rm -f *.r &&
+
+
+ # Note: This test assumes that Git filters files in alphabetical
+ # order ("abort.r" before "test.r").
+ filter_git checkout --quiet --no-progress . &&
+ cat >expected.log <<-EOF &&
+ START
+ init handshake complete
+ IN: smudge abort.r $M $SA [OK] -- [ABORT]
+ STOP
+ EOF
+ test_cmp_exclude_clean expected.log debug.log &&
+
+ test_cmp "$TEST_ROOT/test.o" test.r &&
+ test_cmp "$TEST_ROOT/test2.o" test2.r &&
+ test_cmp abort.o abort.r
+ )
+'
+
+test_expect_success PERL 'invalid process filter must fail (and not hang!)' '
+ test_config_global filter.protocol.process cat &&
+ test_config_global filter.protocol.required true &&
+ rm -rf repo &&
+ mkdir repo &&
+ (
+ cd repo &&
+ git init &&
+
+ echo "*.r filter=protocol" >.gitattributes &&
+
+ cp "$TEST_ROOT/test.o" test.r &&
+ test_must_fail git add . 2>git-stderr.log &&
+ grep "expected git-filter-server" git-stderr.log
+ )
+'
+
+test_expect_success 'delayed checkout in process filter' '
+ test_config_global filter.a.process "test-tool rot13-filter --log=a.log clean smudge delay" &&
+ test_config_global filter.a.required true &&
+ test_config_global filter.b.process "test-tool rot13-filter --log=b.log clean smudge delay" &&
+ test_config_global filter.b.required true &&
+
+ rm -rf repo &&
+ mkdir repo &&
+ (
+ cd repo &&
+ git init &&
+ echo "*.a filter=a" >.gitattributes &&
+ echo "*.b filter=b" >>.gitattributes &&
+ cp "$TEST_ROOT/test.o" test.a &&
+ cp "$TEST_ROOT/test.o" test-delay10.a &&
+ cp "$TEST_ROOT/test.o" test-delay11.a &&
+ cp "$TEST_ROOT/test.o" test-delay20.a &&
+ cp "$TEST_ROOT/test.o" test-delay10.b &&
+ git add . &&
+ git commit -m "test commit"
+ ) &&
+
+ S=$(test_file_size "$TEST_ROOT/test.o") &&
+ PM="ref=refs/heads/main treeish=$(git -C repo rev-parse --verify main) " &&
+ M="${PM}blob=$(git -C repo rev-parse --verify main:test.a)" &&
+ cat >a.exp <<-EOF &&
+ START
+ init handshake complete
+ IN: smudge test.a $M $S [OK] -- OUT: $S . [OK]
+ IN: smudge test-delay10.a $M $S [OK] -- [DELAYED]
+ IN: smudge test-delay11.a $M $S [OK] -- [DELAYED]
+ IN: smudge test-delay20.a $M $S [OK] -- [DELAYED]
+ IN: list_available_blobs test-delay10.a test-delay11.a [OK]
+ IN: smudge test-delay10.a $M 0 [OK] -- OUT: $S . [OK]
+ IN: smudge test-delay11.a $M 0 [OK] -- OUT: $S . [OK]
+ IN: list_available_blobs test-delay20.a [OK]
+ IN: smudge test-delay20.a $M 0 [OK] -- OUT: $S . [OK]
+ IN: list_available_blobs [OK]
+ STOP
+ EOF
+ cat >b.exp <<-EOF &&
+ START
+ init handshake complete
+ IN: smudge test-delay10.b $M $S [OK] -- [DELAYED]
+ IN: list_available_blobs test-delay10.b [OK]
+ IN: smudge test-delay10.b $M 0 [OK] -- OUT: $S . [OK]
+ IN: list_available_blobs [OK]
+ STOP
+ EOF
+
+ rm -rf repo-cloned &&
+ filter_git clone repo repo-cloned &&
+ test_cmp_count a.exp repo-cloned/a.log &&
+ test_cmp_count b.exp repo-cloned/b.log &&
+
+ (
+ cd repo-cloned &&
+ test_cmp_committed_rot13 "$TEST_ROOT/test.o" test.a &&
+ test_cmp_committed_rot13 "$TEST_ROOT/test.o" test-delay10.a &&
+ test_cmp_committed_rot13 "$TEST_ROOT/test.o" test-delay11.a &&
+ test_cmp_committed_rot13 "$TEST_ROOT/test.o" test-delay20.a &&
+ test_cmp_committed_rot13 "$TEST_ROOT/test.o" test-delay10.b &&
+
+ rm *.a *.b &&
+ filter_git checkout . &&
+ # We are not checking out a ref here, so filter out ref metadata.
+ sed -e "s!$PM!!" ../a.exp >a.exp.filtered &&
+ sed -e "s!$PM!!" ../b.exp >b.exp.filtered &&
+ test_cmp_count a.exp.filtered a.log &&
+ test_cmp_count b.exp.filtered b.log &&
+
+ test_cmp_committed_rot13 "$TEST_ROOT/test.o" test.a &&
+ test_cmp_committed_rot13 "$TEST_ROOT/test.o" test-delay10.a &&
+ test_cmp_committed_rot13 "$TEST_ROOT/test.o" test-delay11.a &&
+ test_cmp_committed_rot13 "$TEST_ROOT/test.o" test-delay20.a &&
+ test_cmp_committed_rot13 "$TEST_ROOT/test.o" test-delay10.b
+ )
+'
+
+test_expect_success 'missing file in delayed checkout' '
+ test_config_global filter.bug.process "test-tool rot13-filter --log=bug.log clean smudge delay" &&
+ test_config_global filter.bug.required true &&
+
+ rm -rf repo &&
+ mkdir repo &&
+ (
+ cd repo &&
+ git init &&
+ echo "*.a filter=bug" >.gitattributes &&
+ cp "$TEST_ROOT/test.o" missing-delay.a &&
+ git add . &&
+ git commit -m "test commit"
+ ) &&
+
+ rm -rf repo-cloned &&
+ test_must_fail git clone repo repo-cloned 2>git-stderr.log &&
+ grep "error: .missing-delay\.a. was not filtered properly" git-stderr.log
+'
+
+test_expect_success 'invalid file in delayed checkout' '
+ test_config_global filter.bug.process "test-tool rot13-filter --log=bug.log clean smudge delay" &&
+ test_config_global filter.bug.required true &&
+
+ rm -rf repo &&
+ mkdir repo &&
+ (
+ cd repo &&
+ git init &&
+ echo "*.a filter=bug" >.gitattributes &&
+ cp "$TEST_ROOT/test.o" invalid-delay.a &&
+ cp "$TEST_ROOT/test.o" unfiltered &&
+ git add . &&
+ git commit -m "test commit"
+ ) &&
+
+ rm -rf repo-cloned &&
+ test_must_fail git clone repo repo-cloned 2>git-stderr.log &&
+ grep "error: external filter .* signaled that .unfiltered. is now available although it has not been delayed earlier" git-stderr.log
+'
+
+for mode in 'case' 'utf-8'
+do
+ case "$mode" in
+ case) dir='A' symlink='a' mode_prereq='CASE_INSENSITIVE_FS' ;;
+ utf-8)
+ dir=$(printf "\141\314\210") symlink=$(printf "\303\244")
+ mode_prereq='UTF8_NFD_TO_NFC' ;;
+ esac
+
+ test_expect_success SYMLINKS,$mode_prereq \
+ "delayed checkout with $mode-collision don't write to the wrong place" '
+ test_config_global filter.delay.process \
+ "test-tool rot13-filter --always-delay --log=delayed.log clean smudge delay" &&
+ test_config_global filter.delay.required true &&
+
+ git init $mode-collision &&
+ (
+ cd $mode-collision &&
+ mkdir target-dir &&
+
+ empty_oid=$(printf "" | git hash-object -w --stdin) &&
+ symlink_oid=$(printf "%s" "$PWD/target-dir" | git hash-object -w --stdin) &&
+ attr_oid=$(echo "$dir/z filter=delay" | git hash-object -w --stdin) &&
+
+ cat >objs <<-EOF &&
+ 100644 blob $empty_oid $dir/x
+ 100644 blob $empty_oid $dir/y
+ 100644 blob $empty_oid $dir/z
+ 120000 blob $symlink_oid $symlink
+ 100644 blob $attr_oid .gitattributes
+ EOF
+
+ git update-index --index-info <objs &&
+ git commit -m "test commit"
+ ) &&
+
+ git clone $mode-collision $mode-collision-cloned &&
+ # Make sure z was really delayed
+ grep "IN: smudge $dir/z .* \\[DELAYED\\]" $mode-collision-cloned/delayed.log &&
+
+ # Should not create $dir/z at $symlink/z
+ test_path_is_missing $mode-collision/target-dir/z
+ '
+done
+
+test_expect_success SYMLINKS,CASE_INSENSITIVE_FS \
+"delayed checkout with submodule collision don't write to the wrong place" '
+ git init collision-with-submodule &&
+ (
+ cd collision-with-submodule &&
+ git config filter.delay.process "test-tool rot13-filter --always-delay --log=delayed.log clean smudge delay" &&
+ git config filter.delay.required true &&
+
+ # We need Git to treat the submodule "a" and the
+ # leading dir "A" as different paths in the index.
+ git config --local core.ignoreCase false &&
+
+ empty_oid=$(printf "" | git hash-object -w --stdin) &&
+ attr_oid=$(echo "A/B/y filter=delay" | git hash-object -w --stdin) &&
+ cat >objs <<-EOF &&
+ 100644 blob $empty_oid A/B/x
+ 100644 blob $empty_oid A/B/y
+ 100644 blob $attr_oid .gitattributes
+ EOF
+ git update-index --index-info <objs &&
+
+ git init a &&
+ mkdir target-dir &&
+ symlink_oid=$(printf "%s" "$PWD/target-dir" | git -C a hash-object -w --stdin) &&
+ echo "120000 blob $symlink_oid b" >objs &&
+ git -C a update-index --index-info <objs &&
+ git -C a commit -m sub &&
+ git submodule add ./a &&
+ git commit -m super &&
+
+ git checkout --recurse-submodules . &&
+ grep "IN: smudge A/B/y .* \\[DELAYED\\]" delayed.log &&
+ test_path_is_missing target-dir/y
+ )
+'
+
+test_expect_success 'setup for progress tests' '
+ git init progress &&
+ (
+ cd progress &&
+ git config filter.delay.process "test-tool rot13-filter --log=delay-progress.log clean smudge delay" &&
+ git config filter.delay.required true &&
+
+ echo "*.a filter=delay" >.gitattributes &&
+ touch test-delay10.a &&
+ git add . &&
+ git commit -m files
+ )
+'
+
+test_delayed_checkout_progress () {
+ if test "$1" = "!"
+ then
+ local expect_progress=N &&
+ shift
+ else
+ local expect_progress=
+ fi &&
+
+ if test $# -lt 1
+ then
+ BUG "no command given to test_delayed_checkout_progress"
+ fi &&
+
+ (
+ cd progress &&
+ GIT_PROGRESS_DELAY=0 &&
+ export GIT_PROGRESS_DELAY &&
+ rm -f *.a delay-progress.log &&
+
+ "$@" 2>err &&
+ grep "IN: smudge test-delay10.a .* \\[DELAYED\\]" delay-progress.log &&
+ if test "$expect_progress" = N
+ then
+ ! grep "Filtering content" err
+ else
+ grep "Filtering content" err
+ fi
+ )
+}
+
+for mode in pathspec branch
+do
+ case "$mode" in
+ pathspec) opt='.' ;;
+ branch) opt='-f HEAD' ;;
+ esac
+
+ test_expect_success PERL,TTY "delayed checkout shows progress by default on tty ($mode checkout)" '
+ test_delayed_checkout_progress test_terminal git checkout $opt
+ '
+
+ test_expect_success PERL "delayed checkout ommits progress on non-tty ($mode checkout)" '
+ test_delayed_checkout_progress ! git checkout $opt
+ '
+
+ test_expect_success PERL,TTY "delayed checkout ommits progress with --quiet ($mode checkout)" '
+ test_delayed_checkout_progress ! test_terminal git checkout --quiet $opt
+ '
+
+ test_expect_success PERL,TTY "delayed checkout honors --[no]-progress ($mode checkout)" '
+ test_delayed_checkout_progress ! test_terminal git checkout --no-progress $opt &&
+ test_delayed_checkout_progress test_terminal git checkout --quiet --progress $opt
+ '
+done
+
+test_expect_success 'delayed checkout correctly reports the number of updated entries' '
+ rm -rf repo &&
+ git init repo &&
+ (
+ cd repo &&
+ git config filter.delay.process "test-tool rot13-filter --log=delayed.log clean smudge delay" &&
+ git config filter.delay.required true &&
+
+ echo "*.a filter=delay" >.gitattributes &&
+ echo a >test-delay10.a &&
+ echo a >test-delay11.a &&
+ git add . &&
+ git commit -m files &&
+
+ rm *.a &&
+ git checkout . 2>err &&
+ grep "IN: smudge test-delay10.a .* \\[DELAYED\\]" delayed.log &&
+ grep "IN: smudge test-delay11.a .* \\[DELAYED\\]" delayed.log &&
+ grep "Updated 2 paths from the index" err
+ )
+'
+
+test_done
diff --git a/t/t0022-crlf-rename.sh b/t/t0022-crlf-rename.sh
new file mode 100755
index 0000000..9fe9891
--- /dev/null
+++ b/t/t0022-crlf-rename.sh
@@ -0,0 +1,34 @@
+#!/bin/sh
+
+test_description='ignore CR in CRLF sequence while computing similiarity'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+
+ cat "$TEST_DIRECTORY"/t0022-crlf-rename.sh >sample &&
+ git add sample &&
+
+ test_tick &&
+ git commit -m Initial &&
+
+ append_cr <"$TEST_DIRECTORY"/t0022-crlf-rename.sh >elpmas &&
+ git add elpmas &&
+ rm -f sample &&
+
+ test_tick &&
+ git commit -a -m Second
+
+'
+
+test_expect_success 'diff -M' '
+
+ git diff-tree -M -r --name-status HEAD^ HEAD >tmp &&
+ sed -e "s/R[0-9]*/RNUM/" tmp >actual &&
+ echo "RNUM sample elpmas" >expect &&
+ test_cmp expect actual
+
+'
+
+test_done
diff --git a/t/t0023-crlf-am.sh b/t/t0023-crlf-am.sh
new file mode 100755
index 0000000..5758055
--- /dev/null
+++ b/t/t0023-crlf-am.sh
@@ -0,0 +1,45 @@
+#!/bin/sh
+
+test_description='Test am with auto.crlf'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+cat >patchfile <<\EOF
+From 38be10072e45dd6b08ce40851e3fca60a31a340b Mon Sep 17 00:00:00 2001
+From: Marius Storm-Olsen <x@y.com>
+Date: Thu, 23 Aug 2007 13:00:00 +0200
+Subject: test1
+
+---
+ foo | 1 +
+ 1 files changed, 1 insertions(+), 0 deletions(-)
+ create mode 100644 foo
+
+diff --git a/foo b/foo
+new file mode 100644
+index 0000000000000000000000000000000000000000..5716ca5987cbf97d6bb54920bea6adde242d87e6
+--- /dev/null
++++ b/foo
+@@ -0,0 +1 @@
++bar
+EOF
+
+test_expect_success 'setup' '
+
+ git config core.autocrlf true &&
+ echo foo >bar &&
+ git add bar &&
+ test_tick &&
+ git commit -m initial
+
+'
+
+test_expect_success 'am' '
+
+ git am -3 <patchfile &&
+ git diff-files --name-status --exit-code
+
+'
+
+test_done
diff --git a/t/t0024-crlf-archive.sh b/t/t0024-crlf-archive.sh
new file mode 100755
index 0000000..a34de56
--- /dev/null
+++ b/t/t0024-crlf-archive.sh
@@ -0,0 +1,39 @@
+#!/bin/sh
+
+test_description='respect crlf in git archive'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+
+ git config core.autocrlf true &&
+
+ printf "CRLF line ending\r\nAnd another\r\n" > sample &&
+ git add sample &&
+
+ test_tick &&
+ git commit -m Initial
+
+'
+
+test_expect_success 'tar archive' '
+
+ git archive --format=tar HEAD |
+ ( mkdir untarred && cd untarred && "$TAR" -xf - ) &&
+
+ test_cmp sample untarred/sample
+
+'
+
+test_expect_success UNZIP 'zip archive' '
+
+ git archive --format=zip HEAD >test.zip &&
+
+ ( mkdir unzipped && cd unzipped && "$GIT_UNZIP" ../test.zip ) &&
+
+ test_cmp sample unzipped/sample
+
+'
+
+test_done
diff --git a/t/t0025-crlf-renormalize.sh b/t/t0025-crlf-renormalize.sh
new file mode 100755
index 0000000..f7202c1
--- /dev/null
+++ b/t/t0025-crlf-renormalize.sh
@@ -0,0 +1,40 @@
+#!/bin/sh
+
+test_description='CRLF renormalization'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ git config core.autocrlf false &&
+ printf "LINEONE\nLINETWO\nLINETHREE\n" >LF.txt &&
+ printf "LINEONE\r\nLINETWO\r\nLINETHREE\r\n" >CRLF.txt &&
+ printf "LINEONE\r\nLINETWO\nLINETHREE\n" >CRLF_mix_LF.txt &&
+ git add . &&
+ git commit -m initial
+'
+
+test_expect_success 'renormalize CRLF in repo' '
+ echo "*.txt text=auto" >.gitattributes &&
+ git add --renormalize "*.txt" &&
+ cat >expect <<-\EOF &&
+ i/lf w/crlf attr/text=auto CRLF.txt
+ i/lf w/lf attr/text=auto LF.txt
+ i/lf w/mixed attr/text=auto CRLF_mix_LF.txt
+ EOF
+ git ls-files --eol >tmp &&
+ sed -e "s/ / /g" -e "s/ */ /g" tmp |
+ sort >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'ignore-errors not mistaken for renormalize' '
+ git reset --hard &&
+ echo "*.txt text=auto" >.gitattributes &&
+ git ls-files --eol >expect &&
+ git add --ignore-errors "*.txt" &&
+ git ls-files --eol >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t0026-eol-config.sh b/t/t0026-eol-config.sh
new file mode 100755
index 0000000..f426a18
--- /dev/null
+++ b/t/t0026-eol-config.sh
@@ -0,0 +1,104 @@
+#!/bin/sh
+
+test_description='CRLF conversion'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+has_cr() {
+ tr '\015' Q <"$1" | grep Q >/dev/null
+}
+
+test_expect_success setup '
+
+ git config core.autocrlf false &&
+
+ echo "one text" > .gitattributes &&
+
+ test_write_lines Hello world how are you >one &&
+ test_write_lines I am very very fine thank you >two &&
+ git add . &&
+
+ git commit -m initial &&
+
+ one=$(git rev-parse HEAD:one) &&
+ two=$(git rev-parse HEAD:two) &&
+
+ echo happy.
+'
+
+test_expect_success 'eol=lf puts LFs in normalized file' '
+
+ rm -f .gitattributes tmp one two &&
+ git config core.eol lf &&
+ git read-tree --reset -u HEAD &&
+
+ ! has_cr one &&
+ ! has_cr two &&
+ onediff=$(git diff one) &&
+ twodiff=$(git diff two) &&
+ test -z "$onediff" && test -z "$twodiff"
+'
+
+test_expect_success 'eol=crlf puts CRLFs in normalized file' '
+
+ rm -f .gitattributes tmp one two &&
+ git config core.eol crlf &&
+ git read-tree --reset -u HEAD &&
+
+ has_cr one &&
+ ! has_cr two &&
+ onediff=$(git diff one) &&
+ twodiff=$(git diff two) &&
+ test -z "$onediff" && test -z "$twodiff"
+'
+
+test_expect_success 'autocrlf=true overrides eol=lf' '
+
+ rm -f .gitattributes tmp one two &&
+ git config core.eol lf &&
+ git config core.autocrlf true &&
+ git read-tree --reset -u HEAD &&
+
+ has_cr one &&
+ has_cr two &&
+ onediff=$(git diff one) &&
+ twodiff=$(git diff two) &&
+ test -z "$onediff" && test -z "$twodiff"
+'
+
+test_expect_success 'autocrlf=true overrides unset eol' '
+
+ rm -f .gitattributes tmp one two &&
+ git config --unset-all core.eol &&
+ git config core.autocrlf true &&
+ git read-tree --reset -u HEAD &&
+
+ has_cr one &&
+ has_cr two &&
+ onediff=$(git diff one) &&
+ twodiff=$(git diff two) &&
+ test -z "$onediff" && test -z "$twodiff"
+'
+
+test_expect_success NATIVE_CRLF 'eol native is crlf' '
+
+ rm -rf native_eol && mkdir native_eol &&
+ (
+ cd native_eol &&
+ printf "*.txt text\n" >.gitattributes &&
+ printf "one\r\ntwo\r\nthree\r\n" >filedos.txt &&
+ printf "one\ntwo\nthree\n" >fileunix.txt &&
+ git init &&
+ git config core.autocrlf false &&
+ git config core.eol native &&
+ git add filedos.txt fileunix.txt &&
+ git commit -m "first" &&
+ rm file*.txt &&
+ git reset --hard HEAD &&
+ has_cr filedos.txt &&
+ has_cr fileunix.txt
+ )
+'
+
+test_done
diff --git a/t/t0027-auto-crlf.sh b/t/t0027-auto-crlf.sh
new file mode 100755
index 0000000..2f57c86
--- /dev/null
+++ b/t/t0027-auto-crlf.sh
@@ -0,0 +1,632 @@
+#!/bin/sh
+
+test_description='CRLF conversion all combinations'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+compare_files () {
+ tr '\015\000' QN <"$1" >"$1".expect &&
+ tr '\015\000' QN <"$2" | tr -d 'Z' >"$2".actual &&
+ test_cmp "$1".expect "$2".actual &&
+ rm "$1".expect "$2".actual
+}
+
+compare_ws_file () {
+ pfx=$1
+ exp=$2.expect
+ act=$pfx.actual.$3
+ tr '\015\000abcdef0123456789' QN00000000000000000 <"$2" |
+ sed -e "s/0000*/$ZERO_OID/" >"$exp" &&
+ tr '\015\000abcdef0123456789' QN00000000000000000 <"$3" |
+ sed -e "s/0000*/$ZERO_OID/" >"$act" &&
+ test_cmp "$exp" "$act" &&
+ rm "$exp" "$act"
+}
+
+create_gitattributes () {
+ {
+ while test "$#" != 0
+ do
+ case "$1" in
+ auto) echo '*.txt text=auto' ;;
+ ident) echo '*.txt ident' ;;
+ text) echo '*.txt text' ;;
+ -text) echo '*.txt -text' ;;
+ crlf) echo '*.txt eol=crlf' ;;
+ lf) echo '*.txt eol=lf' ;;
+ "") ;;
+ *)
+ echo >&2 invalid attribute: "$1"
+ exit 1
+ ;;
+ esac &&
+ shift
+ done
+ } >.gitattributes
+}
+
+# Create 2 sets of files:
+# The NNO files are "Not NOrmalized in the repo. We use CRLF_mix_LF and store
+# it under different names for the different test cases, see ${pfx}
+# Depending on .gitattributes they are normalized at the next commit (or not)
+# The MIX files have different contents in the repo.
+# Depending on its contents, the "new safer autocrlf" may kick in.
+create_NNO_MIX_files () {
+ for crlf in false true input
+ do
+ for attr in "" auto text -text
+ do
+ for aeol in "" lf crlf
+ do
+ pfx=NNO_attr_${attr}_aeol_${aeol}_${crlf} &&
+ cp CRLF_mix_LF ${pfx}_LF.txt &&
+ cp CRLF_mix_LF ${pfx}_CRLF.txt &&
+ cp CRLF_mix_LF ${pfx}_CRLF_mix_LF.txt &&
+ cp CRLF_mix_LF ${pfx}_LF_mix_CR.txt &&
+ cp CRLF_mix_LF ${pfx}_CRLF_nul.txt &&
+ pfx=MIX_attr_${attr}_aeol_${aeol}_${crlf} &&
+ cp LF ${pfx}_LF.txt &&
+ cp CRLF ${pfx}_CRLF.txt &&
+ cp CRLF_mix_LF ${pfx}_CRLF_mix_LF.txt &&
+ cp LF_mix_CR ${pfx}_LF_mix_CR.txt &&
+ cp CRLF_nul ${pfx}_CRLF_nul.txt ||
+ return 1
+ done
+ done
+ done
+}
+
+check_warning () {
+ case "$1" in
+ LF_CRLF) echo "LF will be replaced by CRLF" >"$2".expect ;;
+ CRLF_LF) echo "CRLF will be replaced by LF" >"$2".expect ;;
+ '') >"$2".expect ;;
+ *) echo >&2 "Illegal 1": "$1" ; return false ;;
+ esac
+ sed -e "s/^.* \([^ ]* will be replaced by [^ ]*\) .*$/\1/" "$2" | uniq >"$2".actual
+ test_cmp "$2".expect "$2".actual
+}
+
+commit_check_warn () {
+ crlf=$1
+ attr=$2
+ lfname=$3
+ crlfname=$4
+ lfmixcrlf=$5
+ lfmixcr=$6
+ crlfnul=$7
+ pfx=crlf_${crlf}_attr_${attr}
+ create_gitattributes "$attr" &&
+ for f in LF CRLF LF_mix_CR CRLF_mix_LF LF_nul CRLF_nul
+ do
+ fname=${pfx}_$f.txt &&
+ cp $f $fname &&
+ git -c core.autocrlf=$crlf add $fname 2>"${pfx}_$f.err" ||
+ return 1
+ done &&
+ git commit -m "core.autocrlf $crlf" &&
+ check_warning "$lfname" ${pfx}_LF.err &&
+ check_warning "$crlfname" ${pfx}_CRLF.err &&
+ check_warning "$lfmixcrlf" ${pfx}_CRLF_mix_LF.err &&
+ check_warning "$lfmixcr" ${pfx}_LF_mix_CR.err &&
+ check_warning "$crlfnul" ${pfx}_CRLF_nul.err
+}
+
+commit_chk_wrnNNO () {
+ attr=$1 ; shift
+ aeol=$1 ; shift
+ crlf=$1 ; shift
+ lfwarn=$1 ; shift
+ crlfwarn=$1 ; shift
+ lfmixcrlf=$1 ; shift
+ lfmixcr=$1 ; shift
+ crlfnul=$1 ; shift
+ pfx=NNO_attr_${attr}_aeol_${aeol}_${crlf}
+
+ test_expect_success 'setup commit NNO files' '
+ #Commit files on top of existing file
+ create_gitattributes "$attr" $aeol &&
+ for f in LF CRLF CRLF_mix_LF LF_mix_CR CRLF_nul
+ do
+ fname=${pfx}_$f.txt &&
+ cp $f $fname &&
+ printf Z >>"$fname" &&
+ git -c core.autocrlf=$crlf add $fname 2>"${pfx}_$f.err" ||
+ return 1
+ done
+ '
+
+ test_expect_success "commit NNO files crlf=$crlf attr=$attr LF" '
+ check_warning "$lfwarn" ${pfx}_LF.err
+ '
+ test_expect_success "commit NNO files attr=$attr aeol=$aeol crlf=$crlf CRLF" '
+ check_warning "$crlfwarn" ${pfx}_CRLF.err
+ '
+
+ test_expect_success "commit NNO files attr=$attr aeol=$aeol crlf=$crlf CRLF_mix_LF" '
+ check_warning "$lfmixcrlf" ${pfx}_CRLF_mix_LF.err
+ '
+
+ test_expect_success "commit NNO files attr=$attr aeol=$aeol crlf=$crlf LF_mix_cr" '
+ check_warning "$lfmixcr" ${pfx}_LF_mix_CR.err
+ '
+
+ test_expect_success "commit NNO files attr=$attr aeol=$aeol crlf=$crlf CRLF_nul" '
+ check_warning "$crlfnul" ${pfx}_CRLF_nul.err
+ '
+}
+
+# Commit a file with mixed line endings on top of different files
+# in the index. Check for warnings
+commit_MIX_chkwrn () {
+ attr=$1 ; shift
+ aeol=$1 ; shift
+ crlf=$1 ; shift
+ lfwarn=$1 ; shift
+ crlfwarn=$1 ; shift
+ lfmixcrlf=$1 ; shift
+ lfmixcr=$1 ; shift
+ crlfnul=$1 ; shift
+ pfx=MIX_attr_${attr}_aeol_${aeol}_${crlf}
+
+ test_expect_success 'setup commit file with mixed EOL' '
+ #Commit file with CLRF_mix_LF on top of existing file
+ create_gitattributes "$attr" $aeol &&
+ for f in LF CRLF CRLF_mix_LF LF_mix_CR CRLF_nul
+ do
+ fname=${pfx}_$f.txt &&
+ cp CRLF_mix_LF $fname &&
+ printf Z >>"$fname" &&
+ git -c core.autocrlf=$crlf add $fname 2>"${pfx}_$f.err" ||
+ return 1
+ done
+ '
+
+ test_expect_success "commit file with mixed EOL onto LF crlf=$crlf attr=$attr" '
+ check_warning "$lfwarn" ${pfx}_LF.err
+ '
+ test_expect_success "commit file with mixed EOL onto CLRF attr=$attr aeol=$aeol crlf=$crlf" '
+ check_warning "$crlfwarn" ${pfx}_CRLF.err
+ '
+
+ test_expect_success "commit file with mixed EOL onto CRLF_mix_LF attr=$attr aeol=$aeol crlf=$crlf" '
+ check_warning "$lfmixcrlf" ${pfx}_CRLF_mix_LF.err
+ '
+
+ test_expect_success "commit file with mixed EOL onto LF_mix_cr attr=$attr aeol=$aeol crlf=$crlf " '
+ check_warning "$lfmixcr" ${pfx}_LF_mix_CR.err
+ '
+
+ test_expect_success "commit file with mixed EOL onto CRLF_nul attr=$attr aeol=$aeol crlf=$crlf" '
+ check_warning "$crlfnul" ${pfx}_CRLF_nul.err
+ '
+}
+
+
+stats_ascii () {
+ case "$1" in
+ LF)
+ echo lf
+ ;;
+ CRLF)
+ echo crlf
+ ;;
+ CRLF_mix_LF)
+ echo mixed
+ ;;
+ LF_mix_CR|CRLF_nul|LF_nul|CRLF_mix_CR)
+ echo "-text"
+ ;;
+ *)
+ echo error_invalid $1
+ ;;
+ esac
+
+}
+
+
+# construct the attr/ returned by git ls-files --eol
+# Take none (=empty), one or two args
+# convert.c: eol=XX overrides text=auto
+attr_ascii () {
+ case $1,$2 in
+ -text,*) echo "-text" ;;
+ text,) echo "text" ;;
+ text,lf) echo "text eol=lf" ;;
+ text,crlf) echo "text eol=crlf" ;;
+ auto,) echo "text=auto" ;;
+ auto,lf) echo "text=auto eol=lf" ;;
+ auto,crlf) echo "text=auto eol=crlf" ;;
+ lf,) echo "text eol=lf" ;;
+ crlf,) echo "text eol=crlf" ;;
+ ,) echo "" ;;
+ *) echo invalid_attr "$1,$2" ;;
+ esac
+}
+
+check_files_in_repo () {
+ crlf=$1
+ attr=$2
+ lfname=$3
+ crlfname=$4
+ lfmixcrlf=$5
+ lfmixcr=$6
+ crlfnul=$7
+ pfx=crlf_${crlf}_attr_${attr}_ &&
+ compare_files $lfname ${pfx}LF.txt &&
+ compare_files $crlfname ${pfx}CRLF.txt &&
+ compare_files $lfmixcrlf ${pfx}CRLF_mix_LF.txt &&
+ compare_files $lfmixcr ${pfx}LF_mix_CR.txt &&
+ compare_files $crlfnul ${pfx}CRLF_nul.txt
+}
+
+check_in_repo_NNO () {
+ attr=$1 ; shift
+ aeol=$1 ; shift
+ crlf=$1 ; shift
+ lfname=$1 ; shift
+ crlfname=$1 ; shift
+ lfmixcrlf=$1 ; shift
+ lfmixcr=$1 ; shift
+ crlfnul=$1 ; shift
+ pfx=NNO_attr_${attr}_aeol_${aeol}_${crlf}
+ test_expect_success "compare_files $lfname ${pfx}_LF.txt" '
+ compare_files $lfname ${pfx}_LF.txt
+ '
+ test_expect_success "compare_files $crlfname ${pfx}_CRLF.txt" '
+ compare_files $crlfname ${pfx}_CRLF.txt
+ '
+ test_expect_success "compare_files $lfmixcrlf ${pfx}_CRLF_mix_LF.txt" '
+ compare_files $lfmixcrlf ${pfx}_CRLF_mix_LF.txt
+ '
+ test_expect_success "compare_files $lfmixcr ${pfx}_LF_mix_CR.txt" '
+ compare_files $lfmixcr ${pfx}_LF_mix_CR.txt
+ '
+ test_expect_success "compare_files $crlfnul ${pfx}_CRLF_nul.txt" '
+ compare_files $crlfnul ${pfx}_CRLF_nul.txt
+ '
+}
+
+checkout_files () {
+ attr=$1 ; shift
+ ident=$1; shift
+ aeol=$1 ; shift
+ crlf=$1 ; shift
+ ceol=$1 ; shift
+ lfname=$1 ; shift
+ crlfname=$1 ; shift
+ lfmixcrlf=$1 ; shift
+ lfmixcr=$1 ; shift
+ crlfnul=$1 ; shift
+ test_expect_success "setup config for checkout attr=$attr ident=$ident aeol=$aeol core.autocrlf=$crlf" '
+ create_gitattributes "$attr" $ident $aeol &&
+ git config core.autocrlf $crlf
+ '
+ pfx=eol_${ceol}_crlf_${crlf}_attr_${attr}_ &&
+ for f in LF CRLF LF_mix_CR CRLF_mix_LF LF_nul
+ do
+ test_expect_success "setup $f checkout ${ceol:+ with -c core.eol=$ceol}" '
+ rm -f crlf_false_attr__$f.txt &&
+ git ${ceol:+-c core.eol=$ceol} checkout -- crlf_false_attr__$f.txt
+ '
+ done
+
+ test_expect_success "ls-files --eol attr=$attr $ident aeol=$aeol core.autocrlf=$crlf core.eol=$ceol" '
+ test_when_finished "rm expect actual" &&
+ sort <<-EOF >expect &&
+ i/crlf w/$(stats_ascii $crlfname) attr/$(attr_ascii $attr $aeol) crlf_false_attr__CRLF.txt
+ i/mixed w/$(stats_ascii $lfmixcrlf) attr/$(attr_ascii $attr $aeol) crlf_false_attr__CRLF_mix_LF.txt
+ i/lf w/$(stats_ascii $lfname) attr/$(attr_ascii $attr $aeol) crlf_false_attr__LF.txt
+ i/-text w/$(stats_ascii $lfmixcr) attr/$(attr_ascii $attr $aeol) crlf_false_attr__LF_mix_CR.txt
+ i/-text w/$(stats_ascii $crlfnul) attr/$(attr_ascii $attr $aeol) crlf_false_attr__CRLF_nul.txt
+ i/-text w/$(stats_ascii $crlfnul) attr/$(attr_ascii $attr $aeol) crlf_false_attr__LF_nul.txt
+ EOF
+ git ls-files --eol crlf_false_attr__* >tmp &&
+ sed -e "s/ / /g" -e "s/ */ /g" tmp |
+ sort >actual &&
+ test_cmp expect actual
+ '
+ test_expect_success "checkout attr=$attr $ident aeol=$aeol core.autocrlf=$crlf core.eol=$ceol file=LF" "
+ compare_ws_file $pfx $lfname crlf_false_attr__LF.txt
+ "
+ test_expect_success "checkout attr=$attr $ident aeol=$aeol core.autocrlf=$crlf core.eol=$ceol file=CRLF" "
+ compare_ws_file $pfx $crlfname crlf_false_attr__CRLF.txt
+ "
+ test_expect_success "checkout attr=$attr $ident aeol=$aeol core.autocrlf=$crlf core.eol=$ceol file=CRLF_mix_LF" "
+ compare_ws_file $pfx $lfmixcrlf crlf_false_attr__CRLF_mix_LF.txt
+ "
+ test_expect_success "checkout attr=$attr $ident aeol=$aeol core.autocrlf=$crlf core.eol=$ceol file=LF_mix_CR" "
+ compare_ws_file $pfx $lfmixcr crlf_false_attr__LF_mix_CR.txt
+ "
+ test_expect_success "checkout attr=$attr $ident aeol=$aeol core.autocrlf=$crlf core.eol=$ceol file=LF_nul" "
+ compare_ws_file $pfx $crlfnul crlf_false_attr__LF_nul.txt
+ "
+}
+
+# Test control characters
+# NUL SOH CR EOF==^Z
+test_expect_success 'ls-files --eol -o Text/Binary' '
+ test_when_finished "rm expect actual TeBi_*" &&
+ STRT=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA &&
+ STR=$STRT$STRT$STRT$STRT &&
+ printf "${STR}BBB\001" >TeBi_127_S &&
+ printf "${STR}BBBB\001">TeBi_128_S &&
+ printf "${STR}BBB\032" >TeBi_127_E &&
+ printf "\032${STR}BBB" >TeBi_E_127 &&
+ printf "${STR}BBBB\000">TeBi_128_N &&
+ printf "${STR}BBB\012">TeBi_128_L &&
+ printf "${STR}BBB\015">TeBi_127_C &&
+ printf "${STR}BB\015\012" >TeBi_126_CL &&
+ printf "${STR}BB\015\012\015" >TeBi_126_CLC &&
+ sort <<-\EOF >expect &&
+ i/ w/-text TeBi_127_S
+ i/ w/none TeBi_128_S
+ i/ w/none TeBi_127_E
+ i/ w/-text TeBi_E_127
+ i/ w/-text TeBi_128_N
+ i/ w/lf TeBi_128_L
+ i/ w/-text TeBi_127_C
+ i/ w/crlf TeBi_126_CL
+ i/ w/-text TeBi_126_CLC
+ EOF
+ git ls-files --eol -o >tmp &&
+ sed -n -e "/TeBi_/{s!attr/[ ]*!!g
+ s! ! !g
+ s! *! !g
+ p
+ }" tmp | sort >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'setup main' '
+ echo >.gitattributes &&
+ git checkout -b main &&
+ git add .gitattributes &&
+ git commit -m "add .gitattributes" . &&
+ printf "\$Id: 0000000000000000000000000000000000000000 \$\nLINEONE\nLINETWO\nLINETHREE" >LF &&
+ printf "\$Id: 0000000000000000000000000000000000000000 \$\r\nLINEONE\r\nLINETWO\r\nLINETHREE" >CRLF &&
+ printf "\$Id: 0000000000000000000000000000000000000000 \$\nLINEONE\r\nLINETWO\nLINETHREE" >CRLF_mix_LF &&
+ printf "\$Id: 0000000000000000000000000000000000000000 \$\nLINEONE\nLINETWO\rLINETHREE" >LF_mix_CR &&
+ printf "\$Id: 0000000000000000000000000000000000000000 \$\r\nLINEONE\r\nLINETWO\rLINETHREE" >CRLF_mix_CR &&
+ printf "\$Id: 0000000000000000000000000000000000000000 \$\r\nLINEONEQ\r\nLINETWO\r\nLINETHREE" | q_to_nul >CRLF_nul &&
+ printf "\$Id: 0000000000000000000000000000000000000000 \$\nLINEONEQ\nLINETWO\nLINETHREE" | q_to_nul >LF_nul &&
+ create_NNO_MIX_files &&
+ git -c core.autocrlf=false add NNO_*.txt MIX_*.txt &&
+ git commit -m "mixed line endings" &&
+ test_tick
+'
+
+
+
+warn_LF_CRLF="LF will be replaced by CRLF"
+warn_CRLF_LF="CRLF will be replaced by LF"
+
+# WILC stands for "Warn if (this OS) converts LF into CRLF".
+# WICL: Warn if CRLF becomes LF
+# WAMIX: Mixed line endings: either CRLF->LF or LF->CRLF
+if test_have_prereq NATIVE_CRLF
+then
+ WILC=LF_CRLF
+ WICL=
+ WAMIX=LF_CRLF
+else
+ WILC=
+ WICL=CRLF_LF
+ WAMIX=CRLF_LF
+fi
+
+# attr LF CRLF CRLFmixLF LFmixCR CRLFNUL
+test_expect_success 'commit files empty attr' '
+ commit_check_warn false "" "" "" "" "" "" &&
+ commit_check_warn true "" "LF_CRLF" "" "LF_CRLF" "" "" &&
+ commit_check_warn input "" "" "CRLF_LF" "CRLF_LF" "" ""
+'
+
+test_expect_success 'commit files attr=auto' '
+ commit_check_warn false "auto" "$WILC" "$WICL" "$WAMIX" "" "" &&
+ commit_check_warn true "auto" "LF_CRLF" "" "LF_CRLF" "" "" &&
+ commit_check_warn input "auto" "" "CRLF_LF" "CRLF_LF" "" ""
+'
+
+test_expect_success 'commit files attr=text' '
+ commit_check_warn false "text" "$WILC" "$WICL" "$WAMIX" "$WILC" "$WICL" &&
+ commit_check_warn true "text" "LF_CRLF" "" "LF_CRLF" "LF_CRLF" "" &&
+ commit_check_warn input "text" "" "CRLF_LF" "CRLF_LF" "" "CRLF_LF"
+'
+
+test_expect_success 'commit files attr=-text' '
+ commit_check_warn false "-text" "" "" "" "" "" &&
+ commit_check_warn true "-text" "" "" "" "" "" &&
+ commit_check_warn input "-text" "" "" "" "" ""
+'
+
+test_expect_success 'commit files attr=lf' '
+ commit_check_warn false "lf" "" "CRLF_LF" "CRLF_LF" "" "CRLF_LF" &&
+ commit_check_warn true "lf" "" "CRLF_LF" "CRLF_LF" "" "CRLF_LF" &&
+ commit_check_warn input "lf" "" "CRLF_LF" "CRLF_LF" "" "CRLF_LF"
+'
+
+test_expect_success 'commit files attr=crlf' '
+ commit_check_warn false "crlf" "LF_CRLF" "" "LF_CRLF" "LF_CRLF" "" &&
+ commit_check_warn true "crlf" "LF_CRLF" "" "LF_CRLF" "LF_CRLF" "" &&
+ commit_check_warn input "crlf" "LF_CRLF" "" "LF_CRLF" "LF_CRLF" ""
+'
+
+# Commit "CRLFmixLF" on top of these files already in the repo:
+# mixed mixed mixed mixed mixed
+# onto onto onto onto onto
+# attr LF CRLF CRLFmixLF LF_mix_CR CRLFNUL
+commit_MIX_chkwrn "" "" false "" "" "" "" ""
+commit_MIX_chkwrn "" "" true "LF_CRLF" "" "" "LF_CRLF" "LF_CRLF"
+commit_MIX_chkwrn "" "" input "CRLF_LF" "" "" "CRLF_LF" "CRLF_LF"
+
+commit_MIX_chkwrn "auto" "" false "$WAMIX" "" "" "$WAMIX" "$WAMIX"
+commit_MIX_chkwrn "auto" "" true "LF_CRLF" "" "" "LF_CRLF" "LF_CRLF"
+commit_MIX_chkwrn "auto" "" input "CRLF_LF" "" "" "CRLF_LF" "CRLF_LF"
+
+# attr LF CRLF CRLFmixLF LF_mix_CR CRLFNUL
+commit_chk_wrnNNO "" "" false "" "" "" "" ""
+commit_chk_wrnNNO "" "" true LF_CRLF "" "" "" ""
+commit_chk_wrnNNO "" "" input "" "" "" "" ""
+
+commit_chk_wrnNNO "auto" "" false "$WILC" "" "" "" ""
+commit_chk_wrnNNO "auto" "" true LF_CRLF "" "" "" ""
+commit_chk_wrnNNO "auto" "" input "" "" "" "" ""
+for crlf in true false input
+do
+ commit_chk_wrnNNO -text "" $crlf "" "" "" "" ""
+ commit_chk_wrnNNO -text lf $crlf "" "" "" "" ""
+ commit_chk_wrnNNO -text crlf $crlf "" "" "" "" ""
+ commit_chk_wrnNNO "" lf $crlf "" CRLF_LF CRLF_LF "" CRLF_LF
+ commit_chk_wrnNNO "" crlf $crlf LF_CRLF "" LF_CRLF LF_CRLF ""
+ commit_chk_wrnNNO auto lf $crlf "" "" "" "" ""
+ commit_chk_wrnNNO auto crlf $crlf LF_CRLF "" "" "" ""
+ commit_chk_wrnNNO text lf $crlf "" CRLF_LF CRLF_LF "" CRLF_LF
+ commit_chk_wrnNNO text crlf $crlf LF_CRLF "" LF_CRLF LF_CRLF ""
+done
+
+commit_chk_wrnNNO "text" "" false "$WILC" "$WICL" "$WAMIX" "$WILC" "$WICL"
+commit_chk_wrnNNO "text" "" true LF_CRLF "" LF_CRLF LF_CRLF ""
+commit_chk_wrnNNO "text" "" input "" CRLF_LF CRLF_LF "" CRLF_LF
+
+test_expect_success 'commit NNO and cleanup' '
+ git commit -m "commit files on top of NNO" &&
+ rm -f *.txt &&
+ git -c core.autocrlf=false reset --hard
+'
+
+test_expect_success 'commit empty gitattribues' '
+ check_files_in_repo false "" LF CRLF CRLF_mix_LF LF_mix_CR CRLF_nul &&
+ check_files_in_repo true "" LF LF LF LF_mix_CR CRLF_nul &&
+ check_files_in_repo input "" LF LF LF LF_mix_CR CRLF_nul
+'
+
+test_expect_success 'commit text=auto' '
+ check_files_in_repo false "auto" LF LF LF LF_mix_CR CRLF_nul &&
+ check_files_in_repo true "auto" LF LF LF LF_mix_CR CRLF_nul &&
+ check_files_in_repo input "auto" LF LF LF LF_mix_CR CRLF_nul
+'
+
+test_expect_success 'commit text' '
+ check_files_in_repo false "text" LF LF LF LF_mix_CR LF_nul &&
+ check_files_in_repo true "text" LF LF LF LF_mix_CR LF_nul &&
+ check_files_in_repo input "text" LF LF LF LF_mix_CR LF_nul
+'
+
+test_expect_success 'commit -text' '
+ check_files_in_repo false "-text" LF CRLF CRLF_mix_LF LF_mix_CR CRLF_nul &&
+ check_files_in_repo true "-text" LF CRLF CRLF_mix_LF LF_mix_CR CRLF_nul &&
+ check_files_in_repo input "-text" LF CRLF CRLF_mix_LF LF_mix_CR CRLF_nul
+'
+
+for crlf in true false input
+do
+ # attr aeol LF CRLF CRLF_mix_LF LF_mix_CR CRLFNUL
+ check_in_repo_NNO "" "" $crlf LF CRLF CRLF_mix_LF LF_mix_CR CRLF_nul
+ check_in_repo_NNO -text "" $crlf LF CRLF CRLF_mix_LF LF_mix_CR CRLF_nul
+ check_in_repo_NNO -text lf $crlf LF CRLF CRLF_mix_LF LF_mix_CR CRLF_nul
+ check_in_repo_NNO -text crlf $crlf LF CRLF CRLF_mix_LF LF_mix_CR CRLF_nul
+ check_in_repo_NNO auto "" $crlf LF CRLF CRLF_mix_LF LF_mix_CR CRLF_nul
+ check_in_repo_NNO auto lf $crlf LF CRLF CRLF_mix_LF LF_mix_CR CRLF_nul
+ check_in_repo_NNO auto crlf $crlf LF CRLF CRLF_mix_LF LF_mix_CR CRLF_nul
+ check_in_repo_NNO text "" $crlf LF LF LF LF_mix_CR LF_nul
+ check_in_repo_NNO text lf $crlf LF LF LF LF_mix_CR LF_nul
+ check_in_repo_NNO text crlf $crlf LF LF LF LF_mix_CR LF_nul
+done
+################################################################################
+# Check how files in the repo are changed when they are checked out
+# How to read the table below:
+# - checkout_files will check multiple files with a combination of settings
+# and attributes (core.autocrlf=input is forbidden with core.eol=crlf)
+#
+# - parameter $1 : text in .gitattributs "" (empty) | auto | text | -text
+# - parameter $2 : ident "" | i (i == ident)
+# - parameter $3 : eol in .gitattributs "" (empty) | lf | crlf
+# - parameter $4 : core.autocrlf false | true | input
+# - parameter $5 : core.eol "" | lf | crlf | "native"
+# - parameter $6 : reference for a file with only LF in the repo
+# - parameter $7 : reference for a file with only CRLF in the repo
+# - parameter $8 : reference for a file with mixed LF and CRLF in the repo
+# - parameter $9 : reference for a file with LF and CR in the repo
+# - parameter $10 : reference for a file with CRLF and a NUL (should be handled as binary when auto)
+
+if test_have_prereq NATIVE_CRLF
+then
+MIX_CRLF_LF=CRLF
+MIX_LF_CR=CRLF_mix_CR
+NL=CRLF
+LFNUL=CRLF_nul
+else
+MIX_CRLF_LF=CRLF_mix_LF
+MIX_LF_CR=LF_mix_CR
+NL=LF
+LFNUL=LF_nul
+fi
+export CRLF_MIX_LF_CR MIX NL
+
+# Same handling with and without ident
+for id in "" ident
+do
+ for ceol in lf crlf native
+ do
+ for crlf in true false input
+ do
+ # -text overrides core.autocrlf and core.eol
+ # text and eol=crlf or eol=lf override core.autocrlf and core.eol
+ checkout_files -text "$id" "" "$crlf" "$ceol" LF CRLF CRLF_mix_LF LF_mix_CR LF_nul
+ checkout_files -text "$id" "lf" "$crlf" "$ceol" LF CRLF CRLF_mix_LF LF_mix_CR LF_nul
+ checkout_files -text "$id" "crlf" "$crlf" "$ceol" LF CRLF CRLF_mix_LF LF_mix_CR LF_nul
+ # text
+ checkout_files text "$id" "lf" "$crlf" "$ceol" LF CRLF CRLF_mix_LF LF_mix_CR LF_nul
+ checkout_files text "$id" "crlf" "$crlf" "$ceol" CRLF CRLF CRLF CRLF_mix_CR CRLF_nul
+ # currently the same as text, eol=XXX
+ checkout_files auto "$id" "lf" "$crlf" "$ceol" LF CRLF CRLF_mix_LF LF_mix_CR LF_nul
+ checkout_files auto "$id" "crlf" "$crlf" "$ceol" CRLF CRLF CRLF_mix_LF LF_mix_CR LF_nul
+ done
+
+ # core.autocrlf false, different core.eol
+ checkout_files "" "$id" "" false "$ceol" LF CRLF CRLF_mix_LF LF_mix_CR LF_nul
+ # core.autocrlf true
+ checkout_files "" "$id" "" true "$ceol" CRLF CRLF CRLF_mix_LF LF_mix_CR LF_nul
+ # text: core.autocrlf = true overrides core.eol
+ checkout_files auto "$id" "" true "$ceol" CRLF CRLF CRLF_mix_LF LF_mix_CR LF_nul
+ checkout_files text "$id" "" true "$ceol" CRLF CRLF CRLF CRLF_mix_CR CRLF_nul
+ # text: core.autocrlf = input overrides core.eol
+ checkout_files text "$id" "" input "$ceol" LF CRLF CRLF_mix_LF LF_mix_CR LF_nul
+ checkout_files auto "$id" "" input "$ceol" LF CRLF CRLF_mix_LF LF_mix_CR LF_nul
+ # text=auto + eol=XXX
+ done
+ # text: core.autocrlf=false uses core.eol
+ checkout_files text "$id" "" false crlf CRLF CRLF CRLF CRLF_mix_CR CRLF_nul
+ checkout_files text "$id" "" false lf LF CRLF CRLF_mix_LF LF_mix_CR LF_nul
+ # text: core.autocrlf=false and core.eol unset(or native) uses native eol
+ checkout_files text "$id" "" false "" $NL CRLF $MIX_CRLF_LF $MIX_LF_CR $LFNUL
+ checkout_files text "$id" "" false native $NL CRLF $MIX_CRLF_LF $MIX_LF_CR $LFNUL
+ # auto: core.autocrlf=false and core.eol unset(or native) uses native eol
+ checkout_files auto "$id" "" false "" $NL CRLF CRLF_mix_LF LF_mix_CR LF_nul
+ checkout_files auto "$id" "" false native $NL CRLF CRLF_mix_LF LF_mix_CR LF_nul
+ # core.autocrlf false, .gitattributes sets eol
+ checkout_files "" "$id" "lf" false "" LF CRLF CRLF_mix_LF LF_mix_CR LF_nul
+ checkout_files "" "$id" "crlf" false "" CRLF CRLF CRLF CRLF_mix_CR CRLF_nul
+ # core.autocrlf true, .gitattributes sets eol
+ checkout_files "" "$id" "lf" true "" LF CRLF CRLF_mix_LF LF_mix_CR LF_nul
+ checkout_files "" "$id" "crlf" true "" CRLF CRLF CRLF CRLF_mix_CR CRLF_nul
+done
+
+# Should be the last test case: remove some files from the worktree
+test_expect_success 'ls-files --eol -d -z' '
+ rm crlf_false_attr__CRLF.txt crlf_false_attr__CRLF_mix_LF.txt crlf_false_attr__LF.txt .gitattributes &&
+ cat >expect <<-\EOF &&
+ i/crlf w/ crlf_false_attr__CRLF.txt
+ i/lf w/ .gitattributes
+ i/lf w/ crlf_false_attr__LF.txt
+ i/mixed w/ crlf_false_attr__CRLF_mix_LF.txt
+ EOF
+ git ls-files --eol -d >tmp &&
+ sed -e "s!attr/[^ ]*!!g" -e "s/ / /g" -e "s/ */ /g" tmp |
+ sort >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t0028-working-tree-encoding.sh b/t/t0028-working-tree-encoding.sh
new file mode 100755
index 0000000..1b55f59
--- /dev/null
+++ b/t/t0028-working-tree-encoding.sh
@@ -0,0 +1,305 @@
+#!/bin/sh
+
+test_description='working-tree-encoding conversion via gitattributes'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+TEST_CREATE_REPO_NO_TEMPLATE=1
+. ./test-lib.sh
+. "$TEST_DIRECTORY/lib-encoding.sh"
+
+GIT_TRACE_WORKING_TREE_ENCODING=1 && export GIT_TRACE_WORKING_TREE_ENCODING
+
+test_expect_success 'setup test files' '
+ git config core.eol lf &&
+
+ text="hallo there!\ncan you read me?" &&
+ echo "*.utf16 text working-tree-encoding=utf-16" >.gitattributes &&
+ echo "*.utf16lebom text working-tree-encoding=UTF-16LE-BOM" >>.gitattributes &&
+ printf "$text" >test.utf8.raw &&
+ printf "$text" | write_utf16 >test.utf16.raw &&
+ printf "$text" | write_utf32 >test.utf32.raw &&
+ printf "\377\376" >test.utf16lebom.raw &&
+ printf "$text" | iconv -f UTF-8 -t UTF-16LE >>test.utf16lebom.raw &&
+
+ # Line ending tests
+ printf "one\ntwo\nthree\n" >lf.utf8.raw &&
+ printf "one\r\ntwo\r\nthree\r\n" >crlf.utf8.raw &&
+
+ # BOM tests
+ printf "\0a\0b\0c" >nobom.utf16be.raw &&
+ printf "a\0b\0c\0" >nobom.utf16le.raw &&
+ printf "\376\377\0a\0b\0c" >bebom.utf16be.raw &&
+ printf "\377\376a\0b\0c\0" >lebom.utf16le.raw &&
+ printf "\0\0\0a\0\0\0b\0\0\0c" >nobom.utf32be.raw &&
+ printf "a\0\0\0b\0\0\0c\0\0\0" >nobom.utf32le.raw &&
+ printf "\0\0\376\377\0\0\0a\0\0\0b\0\0\0c" >bebom.utf32be.raw &&
+ printf "\377\376\0\0a\0\0\0b\0\0\0c\0\0\0" >lebom.utf32le.raw &&
+
+ # Add only UTF-16 file, we will add the UTF-32 file later
+ cp test.utf16.raw test.utf16 &&
+ cp test.utf32.raw test.utf32 &&
+ cp test.utf16lebom.raw test.utf16lebom &&
+ git add .gitattributes test.utf16 test.utf16lebom &&
+ git commit -m initial
+'
+
+test_expect_success 'ensure UTF-8 is stored in Git' '
+ test_when_finished "rm -f test.utf16.git" &&
+
+ git cat-file -p :test.utf16 >test.utf16.git &&
+ test_cmp_bin test.utf8.raw test.utf16.git
+'
+
+test_expect_success 're-encode to UTF-16 on checkout' '
+ test_when_finished "rm -f test.utf16.raw" &&
+
+ rm test.utf16 &&
+ git checkout test.utf16 &&
+ test_cmp_bin test.utf16.raw test.utf16
+'
+
+test_expect_success 're-encode to UTF-16-LE-BOM on checkout' '
+ rm test.utf16lebom &&
+ git checkout test.utf16lebom &&
+ test_cmp_bin test.utf16lebom.raw test.utf16lebom
+'
+
+test_expect_success 'check $GIT_DIR/info/attributes support' '
+ test_when_finished "rm -f test.utf32.git" &&
+ test_when_finished "git reset --hard HEAD" &&
+
+ mkdir .git/info &&
+ echo "*.utf32 text working-tree-encoding=utf-32" >.git/info/attributes &&
+ git add test.utf32 &&
+
+ git cat-file -p :test.utf32 >test.utf32.git &&
+ test_cmp_bin test.utf8.raw test.utf32.git
+'
+
+for i in 16 32
+do
+ test_expect_success "check prohibited UTF-${i} BOM" '
+ test_when_finished "git reset --hard HEAD" &&
+
+ echo "*.utf${i}be text working-tree-encoding=utf-${i}be" >>.gitattributes &&
+ echo "*.utf${i}le text working-tree-encoding=utf-${i}LE" >>.gitattributes &&
+
+ # Here we add a UTF-16 (resp. UTF-32) files with BOM (big/little-endian)
+ # but we tell Git to treat it as UTF-16BE/UTF-16LE (resp. UTF-32).
+ # In these cases the BOM is prohibited.
+ cp bebom.utf${i}be.raw bebom.utf${i}be &&
+ test_must_fail git add bebom.utf${i}be 2>err.out &&
+ test_grep "fatal: BOM is prohibited .* utf-${i}be" err.out &&
+ test_grep "use UTF-${i} as working-tree-encoding" err.out &&
+
+ cp lebom.utf${i}le.raw lebom.utf${i}be &&
+ test_must_fail git add lebom.utf${i}be 2>err.out &&
+ test_grep "fatal: BOM is prohibited .* utf-${i}be" err.out &&
+ test_grep "use UTF-${i} as working-tree-encoding" err.out &&
+
+ cp bebom.utf${i}be.raw bebom.utf${i}le &&
+ test_must_fail git add bebom.utf${i}le 2>err.out &&
+ test_grep "fatal: BOM is prohibited .* utf-${i}LE" err.out &&
+ test_grep "use UTF-${i} as working-tree-encoding" err.out &&
+
+ cp lebom.utf${i}le.raw lebom.utf${i}le &&
+ test_must_fail git add lebom.utf${i}le 2>err.out &&
+ test_grep "fatal: BOM is prohibited .* utf-${i}LE" err.out &&
+ test_grep "use UTF-${i} as working-tree-encoding" err.out
+ '
+
+ test_expect_success "check required UTF-${i} BOM" '
+ test_when_finished "git reset --hard HEAD" &&
+
+ echo "*.utf${i} text working-tree-encoding=utf-${i}" >>.gitattributes &&
+
+ cp nobom.utf${i}be.raw nobom.utf${i} &&
+ test_must_fail git add nobom.utf${i} 2>err.out &&
+ test_grep "fatal: BOM is required .* utf-${i}" err.out &&
+ test_grep "use UTF-${i}BE or UTF-${i}LE" err.out &&
+
+ cp nobom.utf${i}le.raw nobom.utf${i} &&
+ test_must_fail git add nobom.utf${i} 2>err.out &&
+ test_grep "fatal: BOM is required .* utf-${i}" err.out &&
+ test_grep "use UTF-${i}BE or UTF-${i}LE" err.out
+ '
+
+ test_expect_success "eol conversion for UTF-${i} encoded files on checkout" '
+ test_when_finished "rm -f crlf.utf${i}.raw lf.utf${i}.raw" &&
+ test_when_finished "git reset --hard HEAD^" &&
+
+ cat lf.utf8.raw | write_utf${i} >lf.utf${i}.raw &&
+ cat crlf.utf8.raw | write_utf${i} >crlf.utf${i}.raw &&
+ cp crlf.utf${i}.raw eol.utf${i} &&
+
+ cat >expectIndexLF <<-EOF &&
+ i/lf w/-text attr/text eol.utf${i}
+ EOF
+
+ git add eol.utf${i} &&
+ git commit -m eol &&
+
+ # UTF-${i} with CRLF (Windows line endings)
+ rm eol.utf${i} &&
+ git -c core.eol=crlf checkout eol.utf${i} &&
+ test_cmp_bin crlf.utf${i}.raw eol.utf${i} &&
+
+ # Although the file has CRLF in the working tree,
+ # ensure LF in the index
+ git ls-files --eol eol.utf${i} >actual &&
+ test_cmp expectIndexLF actual &&
+
+ # UTF-${i} with LF (Unix line endings)
+ rm eol.utf${i} &&
+ git -c core.eol=lf checkout eol.utf${i} &&
+ test_cmp_bin lf.utf${i}.raw eol.utf${i} &&
+
+ # The file LF in the working tree, ensure LF in the index
+ git ls-files --eol eol.utf${i} >actual &&
+ test_cmp expectIndexLF actual
+ '
+done
+
+test_expect_success 'check unsupported encodings' '
+ test_when_finished "git reset --hard HEAD" &&
+
+ echo "*.set text working-tree-encoding" >.gitattributes &&
+ printf "set" >t.set &&
+ test_must_fail git add t.set 2>err.out &&
+ test_grep "true/false are no valid working-tree-encodings" err.out &&
+
+ echo "*.unset text -working-tree-encoding" >.gitattributes &&
+ printf "unset" >t.unset &&
+ git add t.unset &&
+
+ echo "*.empty text working-tree-encoding=" >.gitattributes &&
+ printf "empty" >t.empty &&
+ git add t.empty &&
+
+ echo "*.garbage text working-tree-encoding=garbage" >.gitattributes &&
+ printf "garbage" >t.garbage &&
+ test_must_fail git add t.garbage 2>err.out &&
+ test_grep "failed to encode" err.out
+'
+
+test_expect_success 'error if encoding round trip is not the same during refresh' '
+ BEFORE_STATE=$(git rev-parse HEAD) &&
+ test_when_finished "git reset --hard $BEFORE_STATE" &&
+
+ # Add and commit a UTF-16 file but skip the "working-tree-encoding"
+ # filter. Consequently, the in-repo representation is UTF-16 and not
+ # UTF-8. This simulates a Git version that has no working tree encoding
+ # support.
+ echo "*.utf16le text working-tree-encoding=utf-16le" >.gitattributes &&
+ echo "hallo" >nonsense.utf16le &&
+ TEST_HASH=$(git hash-object --no-filters -w nonsense.utf16le) &&
+ git update-index --add --cacheinfo 100644 $TEST_HASH nonsense.utf16le &&
+ COMMIT=$(git commit-tree -p $(git rev-parse HEAD) -m "plain commit" $(git write-tree)) &&
+ git update-ref refs/heads/main $COMMIT &&
+
+ test_must_fail git checkout HEAD^ 2>err.out &&
+ test_grep "error: .* overwritten by checkout:" err.out
+'
+
+test_expect_success 'error if encoding garbage is already in Git' '
+ BEFORE_STATE=$(git rev-parse HEAD) &&
+ test_when_finished "git reset --hard $BEFORE_STATE" &&
+
+ # Skip the UTF-16 filter for the added file
+ # This simulates a Git version that has no checkoutEncoding support
+ cp nobom.utf16be.raw nonsense.utf16 &&
+ TEST_HASH=$(git hash-object --no-filters -w nonsense.utf16) &&
+ git update-index --add --cacheinfo 100644 $TEST_HASH nonsense.utf16 &&
+ COMMIT=$(git commit-tree -p $(git rev-parse HEAD) -m "plain commit" $(git write-tree)) &&
+ git update-ref refs/heads/main $COMMIT &&
+
+ git diff 2>err.out &&
+ test_grep "error: BOM is required" err.out
+'
+
+test_lazy_prereq ICONV_SHIFT_JIS '
+ iconv -f UTF-8 -t SHIFT-JIS </dev/null
+'
+
+test_expect_success ICONV_SHIFT_JIS 'check roundtrip encoding' '
+ test_when_finished "rm -f roundtrip.shift roundtrip.utf16" &&
+ test_when_finished "git reset --hard HEAD" &&
+
+ text="hallo there!\nroundtrip test here!" &&
+ printf "$text" | iconv -f UTF-8 -t SHIFT-JIS >roundtrip.shift &&
+ printf "$text" | write_utf16 >roundtrip.utf16 &&
+ echo "*.shift text working-tree-encoding=SHIFT-JIS" >>.gitattributes &&
+
+ # SHIFT-JIS encoded files are round-trip checked by default...
+ GIT_TRACE=1 git add .gitattributes roundtrip.shift 2>&1 |
+ grep "Checking roundtrip encoding for SHIFT-JIS" &&
+ git reset &&
+
+ # ... unless we overwrite the Git config!
+ ! GIT_TRACE=1 git -c core.checkRoundtripEncoding=garbage \
+ add .gitattributes roundtrip.shift 2>&1 |
+ grep "Checking roundtrip encoding for SHIFT-JIS" &&
+ git reset &&
+
+ # UTF-16 encoded files should not be round-trip checked by default...
+ ! GIT_TRACE=1 git add roundtrip.utf16 2>&1 |
+ grep "Checking roundtrip encoding for UTF-16" &&
+ git reset &&
+
+ # ... unless we tell Git to check it!
+ GIT_TRACE=1 git -c core.checkRoundtripEncoding="UTF-16, UTF-32" \
+ add roundtrip.utf16 2>&1 |
+ grep "Checking roundtrip encoding for utf-16" &&
+ git reset &&
+
+ # ... unless we tell Git to check it!
+ # (here we also check that the casing of the encoding is irrelevant)
+ GIT_TRACE=1 git -c core.checkRoundtripEncoding="UTF-32, utf-16" \
+ add roundtrip.utf16 2>&1 |
+ grep "Checking roundtrip encoding for utf-16" &&
+ git reset
+'
+
+# $1: checkout encoding
+# $2: test string
+# $3: binary test string in checkout encoding
+test_commit_utf8_checkout_other () {
+ encoding="$1"
+ orig_string="$2"
+ expect_bytes="$3"
+
+ test_expect_success "Commit UTF-8, checkout $encoding" '
+ test_when_finished "git checkout HEAD -- .gitattributes" &&
+
+ test_ext="commit_utf8_checkout_$encoding" &&
+ test_file="test.$test_ext" &&
+
+ # Commit as UTF-8
+ echo "*.$test_ext text working-tree-encoding=UTF-8" >.gitattributes &&
+ printf "$orig_string" >$test_file &&
+ git add $test_file &&
+ git commit -m "Test data" &&
+
+ # Checkout in tested encoding
+ rm $test_file &&
+ echo "*.$test_ext text working-tree-encoding=$encoding" >.gitattributes &&
+ git checkout HEAD -- $test_file &&
+
+ # Test
+ printf $expect_bytes >$test_file.raw &&
+ test_cmp_bin $test_file.raw $test_file
+ '
+}
+
+test_commit_utf8_checkout_other "UTF-8" "Test ТеÑÑ‚" "\124\145\163\164\040\320\242\320\265\321\201\321\202"
+test_commit_utf8_checkout_other "UTF-16LE" "Test ТеÑÑ‚" "\124\000\145\000\163\000\164\000\040\000\042\004\065\004\101\004\102\004"
+test_commit_utf8_checkout_other "UTF-16BE" "Test ТеÑÑ‚" "\000\124\000\145\000\163\000\164\000\040\004\042\004\065\004\101\004\102"
+test_commit_utf8_checkout_other "UTF-16LE-BOM" "Test ТеÑÑ‚" "\377\376\124\000\145\000\163\000\164\000\040\000\042\004\065\004\101\004\102\004"
+test_commit_utf8_checkout_other "UTF-16BE-BOM" "Test ТеÑÑ‚" "\376\377\000\124\000\145\000\163\000\164\000\040\004\042\004\065\004\101\004\102"
+test_commit_utf8_checkout_other "UTF-32LE" "Test ТеÑÑ‚" "\124\000\000\000\145\000\000\000\163\000\000\000\164\000\000\000\040\000\000\000\042\004\000\000\065\004\000\000\101\004\000\000\102\004\000\000"
+test_commit_utf8_checkout_other "UTF-32BE" "Test ТеÑÑ‚" "\000\000\000\124\000\000\000\145\000\000\000\163\000\000\000\164\000\000\000\040\000\000\004\042\000\000\004\065\000\000\004\101\000\000\004\102"
+
+test_done
diff --git a/t/t0029-core-unsetenvvars.sh b/t/t0029-core-unsetenvvars.sh
new file mode 100755
index 0000000..4e8e90d
--- /dev/null
+++ b/t/t0029-core-unsetenvvars.sh
@@ -0,0 +1,30 @@
+#!/bin/sh
+
+test_description='test the Windows-only core.unsetenvvars setting'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+if ! test_have_prereq MINGW
+then
+ skip_all='skipping Windows-specific tests'
+ test_done
+fi
+
+test_expect_success 'setup' '
+ test_hook --setup pre-commit <<-\EOF
+ echo $HOBBES >&2
+ EOF
+'
+
+test_expect_success 'core.unsetenvvars works' '
+ HOBBES=Calvin &&
+ export HOBBES &&
+ git commit --allow-empty -m with 2>err &&
+ grep Calvin err &&
+ git -c core.unsetenvvars=FINDUS,HOBBES,CALVIN \
+ commit --allow-empty -m without 2>err &&
+ ! grep Calvin err
+'
+
+test_done
diff --git a/t/t0030-stripspace.sh b/t/t0030-stripspace.sh
new file mode 100755
index 0000000..d1b3be8
--- /dev/null
+++ b/t/t0030-stripspace.sh
@@ -0,0 +1,455 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Carlos Rica
+#
+
+test_description='git stripspace'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+t40='A quick brown fox jumps over the lazy do'
+s40=' '
+sss="$s40$s40$s40$s40$s40$s40$s40$s40$s40$s40" # 400
+ttt="$t40$t40$t40$t40$t40$t40$t40$t40$t40$t40" # 400
+
+printf_git_stripspace () {
+ printf "$1" | git stripspace
+}
+
+test_expect_success 'long lines without spaces should be unchanged' '
+ echo "$ttt" >expect &&
+ git stripspace <expect >actual &&
+ test_cmp expect actual &&
+
+ echo "$ttt$ttt" >expect &&
+ git stripspace <expect >actual &&
+ test_cmp expect actual &&
+
+ echo "$ttt$ttt$ttt" >expect &&
+ git stripspace <expect >actual &&
+ test_cmp expect actual &&
+
+ echo "$ttt$ttt$ttt$ttt" >expect &&
+ git stripspace <expect >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'lines with spaces at the beginning should be unchanged' '
+ echo "$sss$ttt" >expect &&
+ git stripspace <expect >actual &&
+ test_cmp expect actual &&
+
+ echo "$sss$sss$ttt" >expect &&
+ git stripspace <expect >actual &&
+ test_cmp expect actual &&
+
+ echo "$sss$sss$sss$ttt" >expect &&
+ git stripspace <expect >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'lines with intermediate spaces should be unchanged' '
+ echo "$ttt$sss$ttt" >expect &&
+ git stripspace <expect >actual &&
+ test_cmp expect actual &&
+
+ echo "$ttt$sss$sss$ttt" >expect &&
+ git stripspace <expect >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'consecutive blank lines should be unified' '
+ printf "$ttt\n\n$ttt\n" > expect &&
+ printf "$ttt\n\n\n\n\n$ttt\n" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ printf "$ttt$ttt\n\n$ttt\n" > expect &&
+ printf "$ttt$ttt\n\n\n\n\n$ttt\n" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ printf "$ttt$ttt$ttt\n\n$ttt\n" > expect &&
+ printf "$ttt$ttt$ttt\n\n\n\n\n$ttt\n" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ printf "$ttt\n\n$ttt\n" > expect &&
+ printf "$ttt\n\n\n\n\n$ttt\n" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ printf "$ttt\n\n$ttt$ttt\n" > expect &&
+ printf "$ttt\n\n\n\n\n$ttt$ttt\n" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ printf "$ttt\n\n$ttt$ttt$ttt\n" > expect &&
+ printf "$ttt\n\n\n\n\n$ttt$ttt$ttt\n" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ printf "$ttt\n\n$ttt\n" > expect &&
+ printf "$ttt\n\t\n \n\n \t\t\n$ttt\n" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ printf "$ttt$ttt\n\n$ttt\n" > expect &&
+ printf "$ttt$ttt\n\t\n \n\n \t\t\n$ttt\n" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ printf "$ttt$ttt$ttt\n\n$ttt\n" > expect &&
+ printf "$ttt$ttt$ttt\n\t\n \n\n \t\t\n$ttt\n" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ printf "$ttt\n\n$ttt\n" > expect &&
+ printf "$ttt\n\t\n \n\n \t\t\n$ttt\n" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ printf "$ttt\n\n$ttt$ttt\n" > expect &&
+ printf "$ttt\n\t\n \n\n \t\t\n$ttt$ttt\n" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ printf "$ttt\n\n$ttt$ttt$ttt\n" > expect &&
+ printf "$ttt\n\t\n \n\n \t\t\n$ttt$ttt$ttt\n" | git stripspace >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'only consecutive blank lines should be completely removed' '
+ printf "\n" | git stripspace >actual &&
+ test_must_be_empty actual &&
+
+ printf "\n\n\n" | git stripspace >actual &&
+ test_must_be_empty actual &&
+
+ printf "$sss\n$sss\n$sss\n" | git stripspace >actual &&
+ test_must_be_empty actual &&
+
+ printf "$sss$sss\n$sss\n\n" | git stripspace >actual &&
+ test_must_be_empty actual &&
+
+ printf "\n$sss\n$sss$sss\n" | git stripspace >actual &&
+ test_must_be_empty actual &&
+
+ printf "$sss$sss$sss$sss\n\n\n" | git stripspace >actual &&
+ test_must_be_empty actual &&
+
+ printf "\n$sss$sss$sss$sss\n\n" | git stripspace >actual &&
+ test_must_be_empty actual &&
+
+ printf "\n\n$sss$sss$sss$sss\n" | git stripspace >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'consecutive blank lines at the beginning should be removed' '
+ printf "$ttt\n" > expect &&
+ printf "\n$ttt\n" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ printf "$ttt\n" > expect &&
+ printf "\n\n\n$ttt\n" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ printf "$ttt$ttt\n" > expect &&
+ printf "\n\n\n$ttt$ttt\n" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ printf "$ttt$ttt$ttt\n" > expect &&
+ printf "\n\n\n$ttt$ttt$ttt\n" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ printf "$ttt$ttt$ttt$ttt\n" > expect &&
+ printf "\n\n\n$ttt$ttt$ttt$ttt\n" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ printf "$ttt\n" > expect &&
+
+ printf "$sss\n$sss\n$sss\n$ttt\n" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ printf "\n$sss\n$sss$sss\n$ttt\n" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ printf "$sss$sss\n$sss\n\n$ttt\n" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ printf "$sss$sss$sss\n\n\n$ttt\n" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ printf "\n$sss$sss$sss\n\n$ttt\n" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ printf "\n\n$sss$sss$sss\n$ttt\n" | git stripspace >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'consecutive blank lines at the end should be removed' '
+ printf "$ttt\n" > expect &&
+ printf "$ttt\n\n" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ printf "$ttt\n" > expect &&
+ printf "$ttt\n\n\n\n" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ printf "$ttt$ttt\n" > expect &&
+ printf "$ttt$ttt\n\n\n\n" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ printf "$ttt$ttt$ttt\n" > expect &&
+ printf "$ttt$ttt$ttt\n\n\n\n" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ printf "$ttt$ttt$ttt$ttt\n" > expect &&
+ printf "$ttt$ttt$ttt$ttt\n\n\n\n" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ printf "$ttt\n" > expect &&
+
+ printf "$ttt\n$sss\n$sss\n$sss\n" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ printf "$ttt\n\n$sss\n$sss$sss\n" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ printf "$ttt\n$sss$sss\n$sss\n\n" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ printf "$ttt\n$sss$sss$sss\n\n\n" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ printf "$ttt\n\n$sss$sss$sss\n\n" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ printf "$ttt\n\n\n$sss$sss$sss\n" | git stripspace >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'text without newline at end should end with newline' '
+ test_stdout_line_count -gt 0 printf_git_stripspace "$ttt" &&
+ test_stdout_line_count -gt 0 printf_git_stripspace "$ttt$ttt" &&
+ test_stdout_line_count -gt 0 printf_git_stripspace "$ttt$ttt$ttt" &&
+ test_stdout_line_count -gt 0 printf_git_stripspace "$ttt$ttt$ttt$ttt"
+'
+
+# text plus spaces at the end:
+
+test_expect_success 'text plus spaces without newline at end should end with newline' '
+ test_stdout_line_count -gt 0 printf_git_stripspace "$ttt$sss" &&
+ test_stdout_line_count -gt 0 printf_git_stripspace "$ttt$ttt$sss" &&
+ test_stdout_line_count -gt 0 printf_git_stripspace "$ttt$ttt$ttt$sss" &&
+ test_stdout_line_count -gt 0 printf_git_stripspace "$ttt$sss$sss" &&
+ test_stdout_line_count -gt 0 printf_git_stripspace "$ttt$ttt$sss$sss" &&
+ test_stdout_line_count -gt 0 printf_git_stripspace "$ttt$sss$sss$sss"
+'
+
+test_expect_success 'text plus spaces without newline at end should not show spaces' '
+ printf "$ttt$sss" | git stripspace >tmp &&
+ ! grep " " tmp >/dev/null &&
+ printf "$ttt$ttt$sss" | git stripspace >tmp &&
+ ! grep " " tmp >/dev/null &&
+ printf "$ttt$ttt$ttt$sss" | git stripspace >tmp &&
+ ! grep " " tmp >/dev/null &&
+ printf "$ttt$sss$sss" | git stripspace >tmp &&
+ ! grep " " tmp >/dev/null &&
+ printf "$ttt$ttt$sss$sss" | git stripspace >tmp &&
+ ! grep " " tmp >/dev/null &&
+ printf "$ttt$sss$sss$sss" | git stripspace >tmp &&
+ ! grep " " tmp >/dev/null
+'
+
+test_expect_success 'text plus spaces without newline should show the correct lines' '
+ printf "$ttt\n" >expect &&
+ printf "$ttt$sss" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ printf "$ttt\n" >expect &&
+ printf "$ttt$sss$sss" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ printf "$ttt\n" >expect &&
+ printf "$ttt$sss$sss$sss" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ printf "$ttt$ttt\n" >expect &&
+ printf "$ttt$ttt$sss" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ printf "$ttt$ttt\n" >expect &&
+ printf "$ttt$ttt$sss$sss" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ printf "$ttt$ttt$ttt\n" >expect &&
+ printf "$ttt$ttt$ttt$sss" | git stripspace >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'text plus spaces at end should not show spaces' '
+ echo "$ttt$sss" | git stripspace >tmp &&
+ ! grep " " tmp >/dev/null &&
+ echo "$ttt$ttt$sss" | git stripspace >tmp &&
+ ! grep " " tmp >/dev/null &&
+ echo "$ttt$ttt$ttt$sss" | git stripspace >tmp &&
+ ! grep " " tmp >/dev/null &&
+ echo "$ttt$sss$sss" | git stripspace >tmp &&
+ ! grep " " tmp >/dev/null &&
+ echo "$ttt$ttt$sss$sss" | git stripspace >tmp &&
+ ! grep " " tmp >/dev/null &&
+ echo "$ttt$sss$sss$sss" | git stripspace >tmp &&
+ ! grep " " tmp >/dev/null
+'
+
+test_expect_success 'text plus spaces at end should be cleaned and newline must remain' '
+ echo "$ttt" >expect &&
+ echo "$ttt$sss" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ echo "$ttt" >expect &&
+ echo "$ttt$sss$sss" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ echo "$ttt" >expect &&
+ echo "$ttt$sss$sss$sss" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ echo "$ttt$ttt" >expect &&
+ echo "$ttt$ttt$sss" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ echo "$ttt$ttt" >expect &&
+ echo "$ttt$ttt$sss$sss" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ echo "$ttt$ttt$ttt" >expect &&
+ echo "$ttt$ttt$ttt$sss" | git stripspace >actual &&
+ test_cmp expect actual
+'
+
+# spaces only:
+
+test_expect_success 'spaces with newline at end should be replaced with empty string' '
+ echo | git stripspace >actual &&
+ test_must_be_empty actual &&
+
+ echo "$sss" | git stripspace >actual &&
+ test_must_be_empty actual &&
+
+ echo "$sss$sss" | git stripspace >actual &&
+ test_must_be_empty actual &&
+
+ echo "$sss$sss$sss" | git stripspace >actual &&
+ test_must_be_empty actual &&
+
+ echo "$sss$sss$sss$sss" | git stripspace >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'spaces without newline at end should not show spaces' '
+ printf "" | git stripspace >tmp &&
+ ! grep " " tmp >/dev/null &&
+ printf "$sss" | git stripspace >tmp &&
+ ! grep " " tmp >/dev/null &&
+ printf "$sss$sss" | git stripspace >tmp &&
+ ! grep " " tmp >/dev/null &&
+ printf "$sss$sss$sss" | git stripspace >tmp &&
+ ! grep " " tmp >/dev/null &&
+ printf "$sss$sss$sss$sss" | git stripspace >tmp &&
+ ! grep " " tmp >/dev/null
+'
+
+test_expect_success 'spaces without newline at end should be replaced with empty string' '
+ printf "" | git stripspace >actual &&
+ test_must_be_empty actual &&
+
+ printf "$sss$sss" | git stripspace >actual &&
+ test_must_be_empty actual &&
+
+ printf "$sss$sss$sss" | git stripspace >actual &&
+ test_must_be_empty actual &&
+
+ printf "$sss$sss$sss$sss" | git stripspace >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'consecutive text lines should be unchanged' '
+ printf "$ttt$ttt\n$ttt\n" >expect &&
+ printf "$ttt$ttt\n$ttt\n" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ printf "$ttt\n$ttt$ttt\n$ttt\n" >expect &&
+ printf "$ttt\n$ttt$ttt\n$ttt\n" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ printf "$ttt\n$ttt\n$ttt\n$ttt$ttt\n" >expect &&
+ printf "$ttt\n$ttt\n$ttt\n$ttt$ttt\n" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ printf "$ttt\n$ttt\n\n$ttt$ttt\n$ttt\n" >expect &&
+ printf "$ttt\n$ttt\n\n$ttt$ttt\n$ttt\n" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ printf "$ttt$ttt\n\n$ttt\n$ttt$ttt\n" >expect &&
+ printf "$ttt$ttt\n\n$ttt\n$ttt$ttt\n" | git stripspace >actual &&
+ test_cmp expect actual &&
+
+ printf "$ttt\n$ttt$ttt\n\n$ttt\n" >expect &&
+ printf "$ttt\n$ttt$ttt\n\n$ttt\n" | git stripspace >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'strip comments, too' '
+ test ! -z "$(echo "# comment" | git stripspace)" &&
+ test -z "$(echo "# comment" | git stripspace -s)"
+'
+
+test_expect_success 'strip comments with changed comment char' '
+ test ! -z "$(echo "; comment" | git -c core.commentchar=";" stripspace)" &&
+ test -z "$(echo "; comment" | git -c core.commentchar=";" stripspace -s)"
+'
+
+test_expect_success '-c with single line' '
+ printf "# foo\n" >expect &&
+ printf "foo" | git stripspace -c >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '-c with single line followed by empty line' '
+ printf "# foo\n#\n" >expect &&
+ printf "foo\n\n" | git stripspace -c >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '-c with newline only' '
+ printf "#\n" >expect &&
+ printf "\n" | git stripspace -c >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--comment-lines with single line' '
+ printf "# foo\n" >expect &&
+ printf "foo" | git stripspace -c >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '-c with changed comment char' '
+ printf "; foo\n" >expect &&
+ printf "foo" | git -c core.commentchar=";" stripspace -c >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '-c with comment char defined in .git/config' '
+ test_config core.commentchar = &&
+ printf "= foo\n" >expect &&
+ rm -fr sub &&
+ mkdir sub &&
+ printf "foo" | git -C sub stripspace -c >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '-c outside git repository' '
+ printf "# foo\n" >expect &&
+ printf "foo" | nongit git stripspace -c >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'avoid SP-HT sequence in commented line' '
+ printf "#\tone\n#\n# two\n" >expect &&
+ printf "\tone\n\ntwo\n" | git stripspace -c >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t0032-reftable-unittest.sh b/t/t0032-reftable-unittest.sh
new file mode 100755
index 0000000..471cb37
--- /dev/null
+++ b/t/t0032-reftable-unittest.sh
@@ -0,0 +1,16 @@
+#!/bin/sh
+#
+# Copyright (c) 2020 Google LLC
+#
+
+test_description='reftable unittests'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'unittests' '
+ TMPDIR=$(pwd) && export TMPDIR &&
+ test-tool reftable
+'
+
+test_done
diff --git a/t/t0033-safe-directory.sh b/t/t0033-safe-directory.sh
new file mode 100755
index 0000000..dc34968
--- /dev/null
+++ b/t/t0033-safe-directory.sh
@@ -0,0 +1,83 @@
+#!/bin/sh
+
+test_description='verify safe.directory checks'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+GIT_TEST_ASSUME_DIFFERENT_OWNER=1
+export GIT_TEST_ASSUME_DIFFERENT_OWNER
+
+expect_rejected_dir () {
+ test_must_fail git status 2>err &&
+ grep "dubious ownership" err
+}
+
+test_expect_success 'safe.directory is not set' '
+ expect_rejected_dir
+'
+
+test_expect_success 'safe.directory on the command line' '
+ git -c safe.directory="$(pwd)" status
+'
+
+test_expect_success 'safe.directory in the environment' '
+ env GIT_CONFIG_COUNT=1 \
+ GIT_CONFIG_KEY_0="safe.directory" \
+ GIT_CONFIG_VALUE_0="$(pwd)" \
+ git status
+'
+
+test_expect_success 'safe.directory in GIT_CONFIG_PARAMETERS' '
+ env GIT_CONFIG_PARAMETERS="${SQ}safe.directory${SQ}=${SQ}$(pwd)${SQ}" \
+ git status
+'
+
+test_expect_success 'ignoring safe.directory in repo config' '
+ (
+ unset GIT_TEST_ASSUME_DIFFERENT_OWNER &&
+ git config safe.directory "$(pwd)"
+ ) &&
+ expect_rejected_dir
+'
+
+test_expect_success 'safe.directory does not match' '
+ git config --global safe.directory bogus &&
+ expect_rejected_dir
+'
+
+test_expect_success 'path exist as different key' '
+ git config --global foo.bar "$(pwd)" &&
+ expect_rejected_dir
+'
+
+test_expect_success 'safe.directory matches' '
+ git config --global --add safe.directory "$(pwd)" &&
+ git status
+'
+
+test_expect_success 'safe.directory matches, but is reset' '
+ git config --global --add safe.directory "" &&
+ expect_rejected_dir
+'
+
+test_expect_success 'safe.directory=*' '
+ git config --global --add safe.directory "*" &&
+ git status
+'
+
+test_expect_success 'safe.directory=*, but is reset' '
+ git config --global --add safe.directory "" &&
+ expect_rejected_dir
+'
+
+test_expect_success 'safe.directory in included file' '
+ cat >gitconfig-include <<-EOF &&
+ [safe]
+ directory = "$(pwd)"
+ EOF
+ git config --global --add include.path "$(pwd)/gitconfig-include" &&
+ git status
+'
+
+test_done
diff --git a/t/t0034-root-safe-directory.sh b/t/t0034-root-safe-directory.sh
new file mode 100755
index 0000000..ff31176
--- /dev/null
+++ b/t/t0034-root-safe-directory.sh
@@ -0,0 +1,93 @@
+#!/bin/sh
+
+test_description='verify safe.directory checks while running as root'
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-sudo.sh
+
+if [ "$GIT_TEST_ALLOW_SUDO" != "YES" ]
+then
+ skip_all="You must set env var GIT_TEST_ALLOW_SUDO=YES in order to run this test"
+ test_done
+fi
+
+if ! test_have_prereq NOT_ROOT
+then
+ skip_all="These tests do not support running as root"
+ test_done
+fi
+
+test_lazy_prereq SUDO '
+ sudo -n id -u >u &&
+ id -u root >r &&
+ test_cmp u r &&
+ command -v git >u &&
+ sudo command -v git >r &&
+ test_cmp u r
+'
+
+if ! test_have_prereq SUDO
+then
+ skip_all="Your sudo/system configuration is either too strict or unsupported"
+ test_done
+fi
+
+test_expect_success SUDO 'setup' '
+ sudo rm -rf root &&
+ mkdir -p root/r &&
+ (
+ cd root/r &&
+ git init
+ )
+'
+
+test_expect_success SUDO 'sudo git status as original owner' '
+ (
+ cd root/r &&
+ git status &&
+ sudo git status
+ )
+'
+
+test_expect_success SUDO 'setup root owned repository' '
+ sudo mkdir -p root/p &&
+ sudo git init root/p
+'
+
+test_expect_success 'cannot access if owned by root' '
+ (
+ cd root/p &&
+ test_must_fail git status
+ )
+'
+
+test_expect_success 'can access if addressed explicitly' '
+ (
+ cd root/p &&
+ GIT_DIR=.git GIT_WORK_TREE=. git status
+ )
+'
+
+test_expect_success SUDO 'can access with sudo if root' '
+ (
+ cd root/p &&
+ sudo git status
+ )
+'
+
+test_expect_success SUDO 'can access with sudo if root by removing SUDO_UID' '
+ (
+ cd root/p &&
+ run_with_sudo <<-END
+ unset SUDO_UID &&
+ git status
+ END
+ )
+'
+
+# this MUST be always the last test
+test_expect_success SUDO 'cleanup' '
+ sudo rm -rf root
+'
+
+test_done
diff --git a/t/t0035-safe-bare-repository.sh b/t/t0035-safe-bare-repository.sh
new file mode 100755
index 0000000..038b8b7
--- /dev/null
+++ b/t/t0035-safe-bare-repository.sh
@@ -0,0 +1,81 @@
+#!/bin/sh
+
+test_description='verify safe.bareRepository checks'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+pwd="$(pwd)"
+
+expect_accepted_implicit () {
+ test_when_finished 'rm "$pwd/trace.perf"' &&
+ GIT_TRACE2_PERF="$pwd/trace.perf" git "$@" rev-parse --git-dir &&
+ # Note: we're intentionally only checking that the bare repo has a
+ # directory *prefix* of $pwd
+ grep -F "implicit-bare-repository:$pwd" "$pwd/trace.perf"
+}
+
+expect_accepted_explicit () {
+ test_when_finished 'rm "$pwd/trace.perf"' &&
+ GIT_DIR="$1" GIT_TRACE2_PERF="$pwd/trace.perf" git rev-parse --git-dir &&
+ ! grep -F "implicit-bare-repository:$pwd" "$pwd/trace.perf"
+}
+
+expect_rejected () {
+ test_when_finished 'rm "$pwd/trace.perf"' &&
+ test_env GIT_TRACE2_PERF="$pwd/trace.perf" \
+ test_must_fail git "$@" rev-parse --git-dir 2>err &&
+ grep -F "cannot use bare repository" err &&
+ grep -F "implicit-bare-repository:$pwd" "$pwd/trace.perf"
+}
+
+test_expect_success 'setup bare repo in worktree' '
+ git init outer-repo &&
+ git init --bare outer-repo/bare-repo
+'
+
+test_expect_success 'safe.bareRepository unset' '
+ test_unconfig --global safe.bareRepository &&
+ expect_accepted_implicit -C outer-repo/bare-repo
+'
+
+test_expect_success 'safe.bareRepository=all' '
+ test_config_global safe.bareRepository all &&
+ expect_accepted_implicit -C outer-repo/bare-repo
+'
+
+test_expect_success 'safe.bareRepository=explicit' '
+ test_config_global safe.bareRepository explicit &&
+ expect_rejected -C outer-repo/bare-repo
+'
+
+test_expect_success 'safe.bareRepository in the repository' '
+ # safe.bareRepository must not be "explicit", otherwise
+ # git config fails with "fatal: not in a git directory" (like
+ # safe.directory)
+ test_config -C outer-repo/bare-repo safe.bareRepository \
+ all &&
+ test_config_global safe.bareRepository explicit &&
+ expect_rejected -C outer-repo/bare-repo
+'
+
+test_expect_success 'safe.bareRepository on the command line' '
+ test_config_global safe.bareRepository explicit &&
+ expect_accepted_implicit -C outer-repo/bare-repo \
+ -c safe.bareRepository=all
+'
+
+test_expect_success 'safe.bareRepository in included file' '
+ cat >gitconfig-include <<-\EOF &&
+ [safe]
+ bareRepository = explicit
+ EOF
+ git config --global --add include.path "$(pwd)/gitconfig-include" &&
+ expect_rejected -C outer-repo/bare-repo
+'
+
+test_expect_success 'no trace when GIT_DIR is explicitly provided' '
+ expect_accepted_explicit "$pwd/outer-repo/bare-repo"
+'
+
+test_done
diff --git a/t/t0040-parse-options.sh b/t/t0040-parse-options.sh
new file mode 100755
index 0000000..8fdef88
--- /dev/null
+++ b/t/t0040-parse-options.sh
@@ -0,0 +1,754 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Johannes Schindelin
+#
+
+test_description='our own option parser'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+cat >expect <<\EOF
+usage: test-tool parse-options <options>
+
+ A helper function for the parse-options API.
+
+ --[no-]yes get a boolean
+ -D, --no-doubt begins with 'no-'
+ --doubt opposite of --no-doubt
+ -B, --no-fear be brave
+ -b, --[no-]boolean increment by one
+ -4, --[no-]or4 bitwise-or boolean with ...0100
+ --[no-]neg-or4 same as --no-or4
+
+ -i, --[no-]integer <n>
+ get a integer
+ -j <n> get a integer, too
+ -m, --magnitude <n> get a magnitude
+ --[no-]set23 set integer to 23
+ --mode1 set integer to 1 (cmdmode option)
+ --mode2 set integer to 2 (cmdmode option)
+ --[no-]mode34 (3|4) set integer to 3 or 4 (cmdmode option)
+ -L, --[no-]length <str>
+ get length of <str>
+ -F, --[no-]file <file>
+ set file to <file>
+
+String options
+ -s, --[no-]string <string>
+ get a string
+ --[no-]string2 <str> get another string
+ --[no-]st <st> get another string (pervert ordering)
+ -o <str> get another string
+ --longhelp help text of this entry
+ spans multiple lines
+ --[no-]list <str> add str to list
+
+Magic arguments
+ -NUM set integer to NUM
+ + same as -b
+ --ambiguous positive ambiguity
+ --no-ambiguous negative ambiguity
+
+Standard options
+ --[no-]abbrev[=<n>] use <n> digits to display object names
+ -v, --[no-]verbose be verbose
+ -n, --[no-]dry-run dry run
+ -q, --[no-]quiet be quiet
+ --[no-]expect <string>
+ expected output in the variable dump
+
+Alias
+ -A, --[no-]alias-source <string>
+ get a string
+ -Z, --[no-]alias-target <string>
+ alias of --alias-source
+
+EOF
+
+test_expect_success 'test help' '
+ test_must_fail test-tool parse-options -h >output 2>output.err &&
+ test_must_be_empty output.err &&
+ test_cmp expect output
+'
+
+mv expect expect.err
+
+check () {
+ what="$1" &&
+ shift &&
+ expect="$1" &&
+ shift &&
+ test-tool parse-options --expect="$what $expect" "$@"
+}
+
+check_unknown_i18n() {
+ case "$1" in
+ --*)
+ echo error: unknown option \`${1#--}\' >expect ;;
+ -*)
+ echo error: unknown switch \`${1#-}\' >expect ;;
+ esac &&
+ cat expect.err >>expect &&
+ test_must_fail test-tool parse-options $* >output 2>output.err &&
+ test_must_be_empty output &&
+ test_cmp expect output.err
+}
+
+test_expect_success 'OPT_BOOL() #1' 'check boolean: 1 --yes'
+test_expect_success 'OPT_BOOL() #2' 'check boolean: 1 --no-doubt'
+test_expect_success 'OPT_BOOL() #3' 'check boolean: 1 -D'
+test_expect_success 'OPT_BOOL() #4' 'check boolean: 1 --no-fear'
+test_expect_success 'OPT_BOOL() #5' 'check boolean: 1 -B'
+
+test_expect_success 'OPT_BOOL() is idempotent #1' 'check boolean: 1 --yes --yes'
+test_expect_success 'OPT_BOOL() is idempotent #2' 'check boolean: 1 -DB'
+
+test_expect_success 'OPT_BOOL() negation #1' 'check boolean: 0 -D --no-yes'
+test_expect_success 'OPT_BOOL() negation #2' 'check boolean: 0 -D --no-no-doubt'
+
+test_expect_success 'OPT_BOOL() no negation #1' 'check_unknown_i18n --fear'
+test_expect_success 'OPT_BOOL() no negation #2' 'check_unknown_i18n --no-no-fear'
+
+test_expect_success 'OPT_BOOL() positivation' 'check boolean: 0 -D --doubt'
+
+test_expect_success 'OPT_INT() negative' 'check integer: -2345 -i -2345'
+
+test_expect_success 'OPT_MAGNITUDE() simple' '
+ check magnitude: 2345678 -m 2345678
+'
+
+test_expect_success 'OPT_MAGNITUDE() kilo' '
+ check magnitude: 239616 -m 234k
+'
+
+test_expect_success 'OPT_MAGNITUDE() mega' '
+ check magnitude: 104857600 -m 100m
+'
+
+test_expect_success 'OPT_MAGNITUDE() giga' '
+ check magnitude: 1073741824 -m 1g
+'
+
+test_expect_success 'OPT_MAGNITUDE() 3giga' '
+ check magnitude: 3221225472 -m 3g
+'
+
+cat >expect <<\EOF
+boolean: 2
+integer: 1729
+magnitude: 16384
+timestamp: 0
+string: 123
+abbrev: 7
+verbose: 2
+quiet: 0
+dry run: yes
+file: prefix/my.file
+EOF
+
+test_expect_success 'short options' '
+ test-tool parse-options -s123 -b -i 1729 -m 16k -b -vv -n -F my.file \
+ >output 2>output.err &&
+ test_cmp expect output &&
+ test_must_be_empty output.err
+'
+
+cat >expect <<\EOF
+boolean: 2
+integer: 1729
+magnitude: 16384
+timestamp: 0
+string: 321
+abbrev: 10
+verbose: 2
+quiet: 0
+dry run: no
+file: prefix/fi.le
+EOF
+
+test_expect_success 'long options' '
+ test-tool parse-options --boolean --integer 1729 --magnitude 16k \
+ --boolean --string2=321 --verbose --verbose --no-dry-run \
+ --abbrev=10 --file fi.le --obsolete \
+ >output 2>output.err &&
+ test_must_be_empty output.err &&
+ test_cmp expect output
+'
+
+test_expect_success 'missing required value' '
+ cat >expect <<-\EOF &&
+ error: switch `s'\'' requires a value
+ EOF
+ test_expect_code 129 test-tool parse-options -s 2>actual &&
+ test_cmp expect actual &&
+
+ cat >expect <<-\EOF &&
+ error: option `string'\'' requires a value
+ EOF
+ test_expect_code 129 test-tool parse-options --string 2>actual &&
+ test_cmp expect actual &&
+
+ cat >expect <<-\EOF &&
+ error: option `file'\'' requires a value
+ EOF
+ test_expect_code 129 test-tool parse-options --file 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'superfluous value provided: boolean' '
+ cat >expect <<-\EOF &&
+ error: option `yes'\'' takes no value
+ EOF
+ test_expect_code 129 test-tool parse-options --yes=hi 2>actual &&
+ test_cmp expect actual &&
+
+ cat >expect <<-\EOF &&
+ error: option `no-yes'\'' takes no value
+ EOF
+ test_expect_code 129 test-tool parse-options --no-yes=hi 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'superfluous value provided: cmdmode' '
+ cat >expect <<-\EOF &&
+ error: option `mode1'\'' takes no value
+ EOF
+ test_expect_code 129 test-tool parse-options --mode1=hi 2>actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<\EOF
+boolean: 1
+integer: 13
+magnitude: 0
+timestamp: 0
+string: 123
+abbrev: 7
+verbose: -1
+quiet: 0
+dry run: no
+file: (not set)
+arg 00: a1
+arg 01: b1
+arg 02: --boolean
+EOF
+
+test_expect_success 'intermingled arguments' '
+ test-tool parse-options a1 --string 123 b1 --boolean -j 13 -- --boolean \
+ >output 2>output.err &&
+ test_must_be_empty output.err &&
+ test_cmp expect output
+'
+
+cat >expect <<\EOF
+boolean: 0
+integer: 2
+magnitude: 0
+timestamp: 0
+string: (not set)
+abbrev: 7
+verbose: -1
+quiet: 0
+dry run: no
+file: (not set)
+EOF
+
+test_expect_success 'unambiguously abbreviated option' '
+ GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
+ test-tool parse-options --int 2 --boolean --no-bo >output 2>output.err &&
+ test_must_be_empty output.err &&
+ test_cmp expect output
+'
+
+test_expect_success 'unambiguously abbreviated option with "="' '
+ GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
+ test-tool parse-options --expect="integer: 2" --int=2
+'
+
+test_expect_success 'ambiguously abbreviated option' '
+ test_expect_code 129 env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
+ test-tool parse-options --strin 123
+'
+
+test_expect_success 'non ambiguous option (after two options it abbreviates)' '
+ GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
+ test-tool parse-options --expect="string: 123" --st 123
+'
+
+test_expect_success 'Alias options do not contribute to abbreviation' '
+ test-tool parse-options --alias-source 123 >output &&
+ grep "^string: 123" output &&
+ test-tool parse-options --alias-target 123 >output &&
+ grep "^string: 123" output &&
+ test_must_fail test-tool parse-options --alias &&
+ GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
+ test-tool parse-options --alias 123 >output &&
+ grep "^string: 123" output
+'
+
+cat >typo.err <<\EOF
+error: did you mean `--boolean` (with two dashes)?
+EOF
+
+test_expect_success 'detect possible typos' '
+ test_must_fail test-tool parse-options -boolean >output 2>output.err &&
+ test_must_be_empty output &&
+ test_cmp typo.err output.err
+'
+
+cat >typo.err <<\EOF
+error: did you mean `--ambiguous` (with two dashes)?
+EOF
+
+test_expect_success 'detect possible typos' '
+ test_must_fail test-tool parse-options -ambiguous >output 2>output.err &&
+ test_must_be_empty output &&
+ test_cmp typo.err output.err
+'
+
+cat >expect <<\EOF
+Callback: "four", 0
+boolean: 5
+integer: 4
+magnitude: 0
+timestamp: 0
+string: (not set)
+abbrev: 7
+verbose: -1
+quiet: 0
+dry run: no
+file: (not set)
+EOF
+
+test_expect_success 'OPT_CALLBACK() and OPT_BIT() work' '
+ test-tool parse-options --length=four -b -4 >output 2>output.err &&
+ test_must_be_empty output.err &&
+ test_cmp expect output
+'
+
+test_expect_success 'OPT_CALLBACK() and callback errors work' '
+ test_must_fail test-tool parse-options --no-length >output 2>output.err &&
+ test_must_be_empty output &&
+ test_must_be_empty output.err
+'
+
+cat >expect <<\EOF
+boolean: 1
+integer: 23
+magnitude: 0
+timestamp: 0
+string: (not set)
+abbrev: 7
+verbose: -1
+quiet: 0
+dry run: no
+file: (not set)
+EOF
+
+test_expect_success 'OPT_BIT() and OPT_SET_INT() work' '
+ test-tool parse-options --set23 -bbbbb --no-or4 >output 2>output.err &&
+ test_must_be_empty output.err &&
+ test_cmp expect output
+'
+
+test_expect_success 'OPT_NEGBIT() and OPT_SET_INT() work' '
+ test-tool parse-options --set23 -bbbbb --neg-or4 >output 2>output.err &&
+ test_must_be_empty output.err &&
+ test_cmp expect output
+'
+
+test_expect_success 'OPT_BIT() works' '
+ test-tool parse-options --expect="boolean: 6" -bb --or4
+'
+
+test_expect_success 'OPT_NEGBIT() works' '
+ test-tool parse-options --expect="boolean: 6" -bb --no-neg-or4
+'
+
+test_expect_success 'OPT_CMDMODE() works' '
+ test-tool parse-options --expect="integer: 1" --mode1 &&
+ test-tool parse-options --expect="integer: 3" --mode34=3
+'
+
+test_expect_success 'OPT_CMDMODE() detects incompatibility (1)' '
+ test_must_fail test-tool parse-options --mode1 --mode2 >output 2>output.err &&
+ test_must_be_empty output &&
+ test_grep "mode1" output.err &&
+ test_grep "mode2" output.err &&
+ test_grep "is incompatible with" output.err
+'
+
+test_expect_success 'OPT_CMDMODE() detects incompatibility (2)' '
+ test_must_fail test-tool parse-options --set23 --mode2 >output 2>output.err &&
+ test_must_be_empty output &&
+ test_grep "mode2" output.err &&
+ test_grep "set23" output.err &&
+ test_grep "is incompatible with" output.err
+'
+
+test_expect_success 'OPT_CMDMODE() detects incompatibility (3)' '
+ test_must_fail test-tool parse-options --mode2 --set23 >output 2>output.err &&
+ test_must_be_empty output &&
+ test_grep "mode2" output.err &&
+ test_grep "set23" output.err &&
+ test_grep "is incompatible with" output.err
+'
+
+test_expect_success 'OPT_CMDMODE() detects incompatibility (4)' '
+ test_must_fail test-tool parse-options --mode2 --mode34=3 \
+ >output 2>output.err &&
+ test_must_be_empty output &&
+ test_grep "mode2" output.err &&
+ test_grep "mode34.3" output.err &&
+ test_grep "is incompatible with" output.err
+'
+
+test_expect_success 'OPT_COUNTUP() with PARSE_OPT_NODASH works' '
+ test-tool parse-options --expect="boolean: 6" + + + + + +
+'
+
+test_expect_success 'OPT_NUMBER_CALLBACK() works' '
+ test-tool parse-options --expect="integer: 12345" -12345
+'
+
+cat >expect <<\EOF
+boolean: 0
+integer: 0
+magnitude: 0
+timestamp: 0
+string: (not set)
+abbrev: 7
+verbose: -1
+quiet: 0
+dry run: no
+file: (not set)
+EOF
+
+test_expect_success 'negation of OPT_NONEG flags is not ambiguous' '
+ GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
+ test-tool parse-options --no-ambig >output 2>output.err &&
+ test_must_be_empty output.err &&
+ test_cmp expect output
+'
+
+cat >>expect <<\EOF
+list: foo
+list: bar
+list: baz
+EOF
+test_expect_success '--list keeps list of strings' '
+ test-tool parse-options --list foo --list=bar --list=baz >output &&
+ test_cmp expect output
+'
+
+test_expect_success '--no-list resets list' '
+ test-tool parse-options --list=other --list=irrelevant --list=options \
+ --no-list --list=foo --list=bar --list=baz >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'multiple quiet levels' '
+ test-tool parse-options --expect="quiet: 3" -q -q -q
+'
+
+test_expect_success 'multiple verbose levels' '
+ test-tool parse-options --expect="verbose: 3" -v -v -v
+'
+
+test_expect_success '--no-quiet sets --quiet to 0' '
+ test-tool parse-options --expect="quiet: 0" --no-quiet
+'
+
+test_expect_success '--no-quiet resets multiple -q to 0' '
+ test-tool parse-options --expect="quiet: 0" -q -q -q --no-quiet
+'
+
+test_expect_success '--no-verbose sets verbose to 0' '
+ test-tool parse-options --expect="verbose: 0" --no-verbose
+'
+
+test_expect_success '--no-verbose resets multiple verbose to 0' '
+ test-tool parse-options --expect="verbose: 0" -v -v -v --no-verbose
+'
+
+test_expect_success 'GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS works' '
+ GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=false \
+ test-tool parse-options --ye &&
+ test_must_fail env GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=true \
+ test-tool parse-options --ye
+'
+
+test_expect_success '--end-of-options treats remainder as args' '
+ test-tool parse-options \
+ --expect="verbose: -1" \
+ --expect="arg 00: --verbose" \
+ --end-of-options --verbose
+'
+
+test_expect_success 'KEEP_DASHDASH works' '
+ test-tool parse-options-flags --keep-dashdash cmd --opt=1 -- --opt=2 --unknown >actual &&
+ cat >expect <<-\EOF &&
+ opt: 1
+ arg 00: --
+ arg 01: --opt=2
+ arg 02: --unknown
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'KEEP_ARGV0 works' '
+ test-tool parse-options-flags --keep-argv0 cmd arg0 --opt=3 >actual &&
+ cat >expect <<-\EOF &&
+ opt: 3
+ arg 00: cmd
+ arg 01: arg0
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'STOP_AT_NON_OPTION works' '
+ test-tool parse-options-flags --stop-at-non-option cmd --opt=4 arg0 --opt=5 --unknown >actual &&
+ cat >expect <<-\EOF &&
+ opt: 4
+ arg 00: arg0
+ arg 01: --opt=5
+ arg 02: --unknown
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'KEEP_UNKNOWN_OPT works' '
+ test-tool parse-options-flags --keep-unknown-opt cmd --unknown=1 --opt=6 -u2 >actual &&
+ cat >expect <<-\EOF &&
+ opt: 6
+ arg 00: --unknown=1
+ arg 01: -u2
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'NO_INTERNAL_HELP works for -h' '
+ test_expect_code 129 test-tool parse-options-flags --no-internal-help cmd -h 2>err &&
+ grep "^error: unknown switch \`h$SQ" err &&
+ grep "^usage: " err
+'
+
+for help_opt in help help-all
+do
+ test_expect_success "NO_INTERNAL_HELP works for --$help_opt" "
+ test_expect_code 129 test-tool parse-options-flags --no-internal-help cmd --$help_opt 2>err &&
+ grep '^error: unknown option \`'$help_opt\' err &&
+ grep '^usage: ' err
+ "
+done
+
+test_expect_success 'KEEP_UNKNOWN_OPT | NO_INTERNAL_HELP works' '
+ test-tool parse-options-flags --keep-unknown-opt --no-internal-help cmd -h --help --help-all >actual &&
+ cat >expect <<-\EOF &&
+ opt: 0
+ arg 00: -h
+ arg 01: --help
+ arg 02: --help-all
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'subcommand - no subcommand shows error and usage' '
+ test_expect_code 129 test-tool parse-subcommand cmd 2>err &&
+ grep "^error: need a subcommand" err &&
+ grep ^usage: err
+'
+
+test_expect_success 'subcommand - subcommand after -- shows error and usage' '
+ test_expect_code 129 test-tool parse-subcommand cmd -- subcmd-one 2>err &&
+ grep "^error: need a subcommand" err &&
+ grep ^usage: err
+'
+
+test_expect_success 'subcommand - subcommand after --end-of-options shows error and usage' '
+ test_expect_code 129 test-tool parse-subcommand cmd --end-of-options subcmd-one 2>err &&
+ grep "^error: need a subcommand" err &&
+ grep ^usage: err
+'
+
+test_expect_success 'subcommand - unknown subcommand shows error and usage' '
+ test_expect_code 129 test-tool parse-subcommand cmd nope 2>err &&
+ grep "^error: unknown subcommand: \`nope$SQ" err &&
+ grep ^usage: err
+'
+
+test_expect_success 'subcommand - subcommands cannot be abbreviated' '
+ test_expect_code 129 test-tool parse-subcommand cmd subcmd-o 2>err &&
+ grep "^error: unknown subcommand: \`subcmd-o$SQ$" err &&
+ grep ^usage: err
+'
+
+test_expect_success 'subcommand - no negated subcommands' '
+ test_expect_code 129 test-tool parse-subcommand cmd no-subcmd-one 2>err &&
+ grep "^error: unknown subcommand: \`no-subcmd-one$SQ" err &&
+ grep ^usage: err
+'
+
+test_expect_success 'subcommand - simple' '
+ test-tool parse-subcommand cmd subcmd-two >actual &&
+ cat >expect <<-\EOF &&
+ opt: 0
+ fn: subcmd_two
+ arg 00: subcmd-two
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'subcommand - stop parsing at the first subcommand' '
+ test-tool parse-subcommand cmd --opt=1 subcmd-two subcmd-one --opt=2 >actual &&
+ cat >expect <<-\EOF &&
+ opt: 1
+ fn: subcmd_two
+ arg 00: subcmd-two
+ arg 01: subcmd-one
+ arg 02: --opt=2
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'subcommand - KEEP_ARGV0' '
+ test-tool parse-subcommand --keep-argv0 cmd subcmd-two >actual &&
+ cat >expect <<-\EOF &&
+ opt: 0
+ fn: subcmd_two
+ arg 00: cmd
+ arg 01: subcmd-two
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + subcommand not given' '
+ test-tool parse-subcommand --subcommand-optional cmd >actual &&
+ cat >expect <<-\EOF &&
+ opt: 0
+ fn: subcmd_one
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + given subcommand' '
+ test-tool parse-subcommand --subcommand-optional cmd subcmd-two branch file >actual &&
+ cat >expect <<-\EOF &&
+ opt: 0
+ fn: subcmd_two
+ arg 00: subcmd-two
+ arg 01: branch
+ arg 02: file
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + subcommand not given + unknown dashless args' '
+ test-tool parse-subcommand --subcommand-optional cmd branch file >actual &&
+ cat >expect <<-\EOF &&
+ opt: 0
+ fn: subcmd_one
+ arg 00: branch
+ arg 01: file
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL + subcommand not given + unknown option' '
+ test_expect_code 129 test-tool parse-subcommand --subcommand-optional cmd --subcommand-opt 2>err &&
+ grep "^error: unknown option" err &&
+ grep ^usage: err
+'
+
+test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT + subcommand not given + unknown option' '
+ test-tool parse-subcommand --subcommand-optional --keep-unknown-opt cmd --subcommand-opt >actual &&
+ cat >expect <<-\EOF &&
+ opt: 0
+ fn: subcmd_one
+ arg 00: --subcommand-opt
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT + subcommand ignored after unknown option' '
+ test-tool parse-subcommand --subcommand-optional --keep-unknown-opt cmd --subcommand-opt subcmd-two >actual &&
+ cat >expect <<-\EOF &&
+ opt: 0
+ fn: subcmd_one
+ arg 00: --subcommand-opt
+ arg 01: subcmd-two
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT + command and subcommand options cannot be mixed' '
+ test-tool parse-subcommand --subcommand-optional --keep-unknown-opt cmd --subcommand-opt branch --opt=1 >actual &&
+ cat >expect <<-\EOF &&
+ opt: 0
+ fn: subcmd_one
+ arg 00: --subcommand-opt
+ arg 01: branch
+ arg 02: --opt=1
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT | KEEP_ARGV0' '
+ test-tool parse-subcommand --subcommand-optional --keep-unknown-opt --keep-argv0 cmd --subcommand-opt branch >actual &&
+ cat >expect <<-\EOF &&
+ opt: 0
+ fn: subcmd_one
+ arg 00: cmd
+ arg 01: --subcommand-opt
+ arg 02: branch
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'subcommand - SUBCOMMAND_OPTIONAL | KEEP_UNKNOWN_OPT | KEEP_DASHDASH' '
+ test-tool parse-subcommand --subcommand-optional --keep-unknown-opt --keep-dashdash cmd -- --subcommand-opt file >actual &&
+ cat >expect <<-\EOF &&
+ opt: 0
+ fn: subcmd_one
+ arg 00: --
+ arg 01: --subcommand-opt
+ arg 02: file
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'subcommand - completion helper' '
+ test-tool parse-subcommand cmd --git-completion-helper >actual &&
+ echo "subcmd-one subcmd-two --opt= --no-opt" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'subcommands are incompatible with STOP_AT_NON_OPTION' '
+ test_must_fail test-tool parse-subcommand --stop-at-non-option cmd subcmd-one 2>err &&
+ grep ^BUG err
+'
+
+test_expect_success 'subcommands are incompatible with KEEP_UNKNOWN_OPT unless in combination with SUBCOMMAND_OPTIONAL' '
+ test_must_fail test-tool parse-subcommand --keep-unknown-opt cmd subcmd-two 2>err &&
+ grep ^BUG err
+'
+
+test_expect_success 'subcommands are incompatible with KEEP_DASHDASH unless in combination with SUBCOMMAND_OPTIONAL' '
+ test_must_fail test-tool parse-subcommand --keep-dashdash cmd subcmd-two 2>err &&
+ grep ^BUG err
+'
+
+test_expect_success 'negative magnitude' '
+ test_must_fail test-tool parse-options --magnitude -1 >out 2>err &&
+ grep "non-negative integer" err &&
+ test_must_be_empty out
+'
+
+test_expect_success 'magnitude with units but no numbers' '
+ test_must_fail test-tool parse-options --magnitude m >out 2>err &&
+ grep "non-negative integer" err &&
+ test_must_be_empty out
+'
+
+test_done
diff --git a/t/t0041-usage.sh b/t/t0041-usage.sh
new file mode 100755
index 0000000..1464294
--- /dev/null
+++ b/t/t0041-usage.sh
@@ -0,0 +1,111 @@
+#!/bin/sh
+
+test_description='Test commands behavior when given invalid argument value'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup ' '
+ test_commit "v1.0"
+'
+
+test_expect_success 'tag --contains <existent_tag>' '
+ git tag --contains "v1.0" >actual 2>actual.err &&
+ grep "v1.0" actual &&
+ test_line_count = 0 actual.err
+'
+
+test_expect_success 'tag --contains <inexistent_tag>' '
+ test_must_fail git tag --contains "notag" >actual 2>actual.err &&
+ test_line_count = 0 actual &&
+ test_grep "error" actual.err &&
+ test_grep ! "usage" actual.err
+'
+
+test_expect_success 'tag --no-contains <existent_tag>' '
+ git tag --no-contains "v1.0" >actual 2>actual.err &&
+ test_line_count = 0 actual &&
+ test_line_count = 0 actual.err
+'
+
+test_expect_success 'tag --no-contains <inexistent_tag>' '
+ test_must_fail git tag --no-contains "notag" >actual 2>actual.err &&
+ test_line_count = 0 actual &&
+ test_grep "error" actual.err &&
+ test_grep ! "usage" actual.err
+'
+
+test_expect_success 'tag usage error' '
+ test_must_fail git tag --noopt >actual 2>actual.err &&
+ test_line_count = 0 actual &&
+ test_grep "usage" actual.err
+'
+
+test_expect_success 'branch --contains <existent_commit>' '
+ git branch --contains "main" >actual 2>actual.err &&
+ test_grep "main" actual &&
+ test_line_count = 0 actual.err
+'
+
+test_expect_success 'branch --contains <inexistent_commit>' '
+ test_must_fail git branch --no-contains "nocommit" >actual 2>actual.err &&
+ test_line_count = 0 actual &&
+ test_grep "error" actual.err &&
+ test_grep ! "usage" actual.err
+'
+
+test_expect_success 'branch --no-contains <existent_commit>' '
+ git branch --no-contains "main" >actual 2>actual.err &&
+ test_line_count = 0 actual &&
+ test_line_count = 0 actual.err
+'
+
+test_expect_success 'branch --no-contains <inexistent_commit>' '
+ test_must_fail git branch --no-contains "nocommit" >actual 2>actual.err &&
+ test_line_count = 0 actual &&
+ test_grep "error" actual.err &&
+ test_grep ! "usage" actual.err
+'
+
+test_expect_success 'branch usage error' '
+ test_must_fail git branch --noopt >actual 2>actual.err &&
+ test_line_count = 0 actual &&
+ test_grep "usage" actual.err
+'
+
+test_expect_success 'for-each-ref --contains <existent_object>' '
+ git for-each-ref --contains "main" >actual 2>actual.err &&
+ test_line_count = 2 actual &&
+ test_line_count = 0 actual.err
+'
+
+test_expect_success 'for-each-ref --contains <inexistent_object>' '
+ test_must_fail git for-each-ref --no-contains "noobject" >actual 2>actual.err &&
+ test_line_count = 0 actual &&
+ test_grep "error" actual.err &&
+ test_grep ! "usage" actual.err
+'
+
+test_expect_success 'for-each-ref --no-contains <existent_object>' '
+ git for-each-ref --no-contains "main" >actual 2>actual.err &&
+ test_line_count = 0 actual &&
+ test_line_count = 0 actual.err
+'
+
+test_expect_success 'for-each-ref --no-contains <inexistent_object>' '
+ test_must_fail git for-each-ref --no-contains "noobject" >actual 2>actual.err &&
+ test_line_count = 0 actual &&
+ test_grep "error" actual.err &&
+ test_grep ! "usage" actual.err
+'
+
+test_expect_success 'for-each-ref usage error' '
+ test_must_fail git for-each-ref --noopt >actual 2>actual.err &&
+ test_line_count = 0 actual &&
+ test_grep "usage" actual.err
+'
+
+test_done
diff --git a/t/t0050-filesystem.sh b/t/t0050-filesystem.sh
new file mode 100755
index 0000000..325eb1c
--- /dev/null
+++ b/t/t0050-filesystem.sh
@@ -0,0 +1,159 @@
+#!/bin/sh
+
+test_description='Various filesystem issues'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+auml=$(printf '\303\244')
+aumlcdiar=$(printf '\141\314\210')
+
+if test_have_prereq CASE_INSENSITIVE_FS
+then
+ say "will test on a case insensitive filesystem"
+ test_case=test_expect_failure
+else
+ test_case=test_expect_success
+fi
+
+if test_have_prereq UTF8_NFD_TO_NFC
+then
+ say "will test on a unicode corrupting filesystem"
+ test_unicode=test_expect_failure
+else
+ test_unicode=test_expect_success
+fi
+
+test_have_prereq SYMLINKS ||
+ say "will test on a filesystem lacking symbolic links"
+
+if test_have_prereq CASE_INSENSITIVE_FS
+then
+test_expect_success "detection of case insensitive filesystem during repo init" '
+ test $(git config --bool core.ignorecase) = true
+'
+else
+test_expect_success "detection of case insensitive filesystem during repo init" '
+ {
+ test_must_fail git config --bool core.ignorecase >/dev/null ||
+ test $(git config --bool core.ignorecase) = false
+ }
+'
+fi
+
+if test_have_prereq SYMLINKS
+then
+test_expect_success "detection of filesystem w/o symlink support during repo init" '
+ {
+ test_must_fail git config --bool core.symlinks ||
+ test "$(git config --bool core.symlinks)" = true
+ }
+'
+else
+test_expect_success "detection of filesystem w/o symlink support during repo init" '
+ v=$(git config --bool core.symlinks) &&
+ test "$v" = false
+'
+fi
+
+test_expect_success "setup case tests" '
+ git config core.ignorecase true &&
+ touch camelcase &&
+ git add camelcase &&
+ git commit -m "initial" &&
+ git tag initial &&
+ git checkout -b topic &&
+ git mv camelcase tmp &&
+ git mv tmp CamelCase &&
+ git commit -m "rename" &&
+ git checkout -f main
+'
+
+test_expect_success 'rename (case change)' '
+ git mv camelcase CamelCase &&
+ git commit -m "rename"
+'
+
+test_expect_success 'merge (case change)' '
+ rm -f CamelCase &&
+ rm -f camelcase &&
+ git reset --hard initial &&
+ git merge topic
+'
+
+test_expect_success CASE_INSENSITIVE_FS 'add directory (with different case)' '
+ git reset --hard initial &&
+ mkdir -p dir1/dir2 &&
+ echo >dir1/dir2/a &&
+ echo >dir1/dir2/b &&
+ git add dir1/dir2/a &&
+ git add dir1/DIR2/b &&
+ git ls-files >actual &&
+ cat >expected <<-\EOF &&
+ camelcase
+ dir1/dir2/a
+ dir1/dir2/b
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_failure CASE_INSENSITIVE_FS 'add (with different case)' '
+ git reset --hard initial &&
+ rm camelcase &&
+ echo 1 >CamelCase &&
+ git add CamelCase &&
+ git ls-files >tmp &&
+ camel=$(grep -i camelcase tmp) &&
+ test $(echo "$camel" | wc -l) = 1 &&
+ test "z$(git cat-file blob :$camel)" = z1
+'
+
+test_expect_success "setup unicode normalization tests" '
+ test_create_repo unicode &&
+ cd unicode &&
+ git config core.precomposeunicode false &&
+ touch "$aumlcdiar" &&
+ git add "$aumlcdiar" &&
+ git commit -m initial &&
+ git tag initial &&
+ git checkout -b topic &&
+ git mv $aumlcdiar tmp &&
+ git mv tmp "$auml" &&
+ git commit -m rename &&
+ git checkout -f main
+'
+
+$test_unicode 'rename (silent unicode normalization)' '
+ git mv "$aumlcdiar" "$auml" &&
+ git commit -m rename
+'
+
+$test_unicode 'merge (silent unicode normalization)' '
+ git reset --hard initial &&
+ git merge topic
+'
+
+test_expect_success CASE_INSENSITIVE_FS 'checkout with no pathspec and a case insensitive fs' '
+ git init repo &&
+ (
+ cd repo &&
+
+ >Gitweb &&
+ git add Gitweb &&
+ git commit -m "add Gitweb" &&
+
+ git checkout --orphan todo &&
+ git reset --hard &&
+ mkdir -p gitweb/subdir &&
+ >gitweb/subdir/file &&
+ git add gitweb &&
+ git commit -m "add gitweb/subdir/file" &&
+
+ git checkout main
+ )
+'
+
+test_done
diff --git a/t/t0051-windows-named-pipe.sh b/t/t0051-windows-named-pipe.sh
new file mode 100755
index 0000000..412f413
--- /dev/null
+++ b/t/t0051-windows-named-pipe.sh
@@ -0,0 +1,22 @@
+#!/bin/sh
+
+test_description='Windows named pipes'
+
+. ./test-lib.sh
+if ! test_have_prereq MINGW
+then
+ skip_all='skipping Windows-specific tests'
+ test_done
+fi
+
+test_expect_success 'o_append write to named pipe' '
+ GIT_TRACE="$(pwd)/expect" git status >/dev/null 2>&1 &&
+ { test-tool windows-named-pipe t0051 >actual 2>&1 & } &&
+ pid=$! &&
+ sleep 1 &&
+ GIT_TRACE=//./pipe/t0051 git status >/dev/null 2>warning &&
+ wait $pid &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t0052-simple-ipc.sh b/t/t0052-simple-ipc.sh
new file mode 100755
index 0000000..1a36a53
--- /dev/null
+++ b/t/t0052-simple-ipc.sh
@@ -0,0 +1,123 @@
+#!/bin/sh
+
+test_description='simple command server'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test-tool simple-ipc SUPPORTS_SIMPLE_IPC || {
+ skip_all='simple IPC not supported on this platform'
+ test_done
+}
+
+stop_simple_IPC_server () {
+ test-tool simple-ipc stop-daemon
+}
+
+test_expect_success 'start simple command server' '
+ test_atexit stop_simple_IPC_server &&
+ test-tool simple-ipc start-daemon --threads=8 &&
+ test-tool simple-ipc is-active
+'
+
+test_expect_success 'simple command server' '
+ test-tool simple-ipc send --token=ping >actual &&
+ echo pong >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'servers cannot share the same path' '
+ test_must_fail test-tool simple-ipc run-daemon &&
+ test-tool simple-ipc is-active
+'
+
+test_expect_success 'big response' '
+ test-tool simple-ipc send --token=big >actual &&
+ test_line_count -ge 10000 actual &&
+ grep -q "big: [0]*9999\$" actual
+'
+
+test_expect_success 'chunk response' '
+ test-tool simple-ipc send --token=chunk >actual &&
+ test_line_count -ge 10000 actual &&
+ grep -q "big: [0]*9999\$" actual
+'
+
+test_expect_success 'slow response' '
+ test-tool simple-ipc send --token=slow >actual &&
+ test_line_count -ge 100 actual &&
+ grep -q "big: [0]*99\$" actual
+'
+
+# Send an IPC with n=100,000 bytes of ballast. This should be large enough
+# to force both the kernel and the pkt-line layer to chunk the message to the
+# daemon and for the daemon to receive it in chunks.
+#
+test_expect_success 'sendbytes' '
+ test-tool simple-ipc sendbytes --bytecount=100000 --byte=A >actual &&
+ grep "sent:A00100000 rcvd:A00100000" actual
+'
+
+# Start a series of <threads> client threads that each make <batchsize>
+# IPC requests to the server. Each (<threads> * <batchsize>) request
+# will open a new connection to the server and randomly bind to a server
+# thread. Each client thread exits after completing its batch. So the
+# total number of live client threads will be smaller than the total.
+# Each request will send a message containing at least <bytecount> bytes
+# of ballast. (Responses are small.)
+#
+# The purpose here is to test threading in the server and responding to
+# many concurrent client requests (regardless of whether they come from
+# 1 client process or many). And to test that the server side of the
+# named pipe/socket is stable. (On Windows this means that the server
+# pipe is properly recycled.)
+#
+# On Windows it also lets us adjust the connection timeout in the
+# `ipc_client_send_command()`.
+#
+# Note it is easy to drive the system into failure by requesting an
+# insane number of threads on client or server and/or increasing the
+# per-thread batchsize or the per-request bytecount (ballast).
+# On Windows these failures look like "pipe is busy" errors.
+# So I've chosen fairly conservative values for now.
+#
+# We expect output of the form "sent:<letter><length> ..."
+# With terms (7, 19, 13) we expect:
+# <letter> in [A-G]
+# <length> in [19+0 .. 19+(13-1)]
+# and (7 * 13) successful responses.
+#
+test_expect_success 'stress test threads' '
+ test-tool simple-ipc multiple \
+ --threads=7 \
+ --bytecount=19 \
+ --batchsize=13 \
+ >actual &&
+ test_line_count = 92 actual &&
+ grep "good 91" actual &&
+ grep "sent:A" <actual >actual_a &&
+ cat >expect_a <<-EOF &&
+ sent:A00000019 rcvd:A00000019
+ sent:A00000020 rcvd:A00000020
+ sent:A00000021 rcvd:A00000021
+ sent:A00000022 rcvd:A00000022
+ sent:A00000023 rcvd:A00000023
+ sent:A00000024 rcvd:A00000024
+ sent:A00000025 rcvd:A00000025
+ sent:A00000026 rcvd:A00000026
+ sent:A00000027 rcvd:A00000027
+ sent:A00000028 rcvd:A00000028
+ sent:A00000029 rcvd:A00000029
+ sent:A00000030 rcvd:A00000030
+ sent:A00000031 rcvd:A00000031
+ EOF
+ test_cmp expect_a actual_a
+'
+
+test_expect_success 'stop-daemon works' '
+ test-tool simple-ipc stop-daemon &&
+ test_must_fail test-tool simple-ipc is-active &&
+ test_must_fail test-tool simple-ipc send --token=ping
+'
+
+test_done
diff --git a/t/t0055-beyond-symlinks.sh b/t/t0055-beyond-symlinks.sh
new file mode 100755
index 0000000..c3eb115
--- /dev/null
+++ b/t/t0055-beyond-symlinks.sh
@@ -0,0 +1,36 @@
+#!/bin/sh
+
+test_description='update-index and add refuse to add beyond symlinks'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success SYMLINKS setup '
+ >a &&
+ mkdir b &&
+ ln -s b c &&
+ >c/d &&
+ git update-index --add a b/d
+'
+
+test_expect_success SYMLINKS 'update-index --add beyond symlinks' '
+ test_must_fail git update-index --add c/d &&
+ cat >expect <<-\EOF &&
+ a
+ b/d
+ EOF
+ git ls-files >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success SYMLINKS 'add beyond symlinks' '
+ test_must_fail git add c/d &&
+ cat >expect <<-\EOF &&
+ a
+ b/d
+ EOF
+ git ls-files >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t0056-git-C.sh b/t/t0056-git-C.sh
new file mode 100755
index 0000000..752aa8c
--- /dev/null
+++ b/t/t0056-git-C.sh
@@ -0,0 +1,95 @@
+#!/bin/sh
+
+test_description='"-C <path>" option and its effects on other path-related options'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success '"git -C <path>" runs git from the directory <path>' '
+ test_create_repo dir1 &&
+ echo 1 >dir1/a.txt &&
+ msg="initial in dir1" &&
+ (cd dir1 && git add a.txt && git commit -m "$msg") &&
+ echo "$msg" >expected &&
+ git -C dir1 log --format=%s >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success '"git -C <path>" with an empty <path> is a no-op' '
+ (
+ mkdir -p dir1/subdir &&
+ cd dir1/subdir &&
+ git -C "" rev-parse --show-prefix >actual &&
+ echo subdir/ >expect &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'Multiple -C options: "-C dir1 -C dir2" is equivalent to "-C dir1/dir2"' '
+ test_create_repo dir1/dir2 &&
+ echo 1 >dir1/dir2/b.txt &&
+ git -C dir1/dir2 add b.txt &&
+ msg="initial in dir1/dir2" &&
+ echo "$msg" >expected &&
+ git -C dir1/dir2 commit -m "$msg" &&
+ git -C dir1 -C dir2 log --format=%s >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'Effect on --git-dir option: "-C c --git-dir=a.git" is equivalent to "--git-dir c/a.git"' '
+ mkdir c &&
+ mkdir c/a &&
+ mkdir c/a.git &&
+ (cd c/a.git && git init --bare) &&
+ echo 1 >c/a/a.txt &&
+ git --git-dir c/a.git --work-tree=c/a add a.txt &&
+ git --git-dir c/a.git --work-tree=c/a commit -m "initial" &&
+ git --git-dir=c/a.git log -1 --format=%s >expected &&
+ git -C c --git-dir=a.git log -1 --format=%s >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'Order should not matter: "--git-dir=a.git -C c" is equivalent to "-C c --git-dir=a.git"' '
+ git -C c --git-dir=a.git log -1 --format=%s >expected &&
+ git --git-dir=a.git -C c log -1 --format=%s >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'Effect on --work-tree option: "-C c/a.git --work-tree=../a" is equivalent to "--work-tree=c/a --git-dir=c/a.git"' '
+ rm c/a/a.txt &&
+ git --git-dir=c/a.git --work-tree=c/a status >expected &&
+ git -C c/a.git --work-tree=../a status >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'Order should not matter: "--work-tree=../a -C c/a.git" is equivalent to "-C c/a.git --work-tree=../a"' '
+ git -C c/a.git --work-tree=../a status >expected &&
+ git --work-tree=../a -C c/a.git status >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'Effect on --git-dir and --work-tree options - "-C c --git-dir=a.git --work-tree=a" is equivalent to "--git-dir=c/a.git --work-tree=c/a"' '
+ git --git-dir=c/a.git --work-tree=c/a status >expected &&
+ git -C c --git-dir=a.git --work-tree=a status >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'Order should not matter: "-C c --git-dir=a.git --work-tree=a" is equivalent to "--git-dir=a.git -C c --work-tree=a"' '
+ git -C c --git-dir=a.git --work-tree=a status >expected &&
+ git --git-dir=a.git -C c --work-tree=a status >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'Order should not matter: "-C c --git-dir=a.git --work-tree=a" is equivalent to "--git-dir=a.git --work-tree=a -C c"' '
+ git -C c --git-dir=a.git --work-tree=a status >expected &&
+ git --git-dir=a.git --work-tree=a -C c status >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'Relative followed by fullpath: "-C ./here -C /there" is equivalent to "-C /there"' '
+ echo "initial in dir1/dir2" >expected &&
+ git -C dir1 -C "$(pwd)/dir1/dir2" log --format=%s >actual &&
+ test_cmp expected actual
+'
+
+test_done
diff --git a/t/t0060-path-utils.sh b/t/t0060-path-utils.sh
new file mode 100755
index 0000000..0afa3d0
--- /dev/null
+++ b/t/t0060-path-utils.sh
@@ -0,0 +1,613 @@
+#!/bin/sh
+#
+# Copyright (c) 2008 David Reiss
+#
+
+test_description='Test various path utilities'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+norm_path() {
+ expected=$(test-tool path-utils print_path "$2")
+ test_expect_success $3 "normalize path: $1 => $2" "
+ echo '$expected' >expect &&
+ test-tool path-utils normalize_path_copy '$1' >actual &&
+ test_cmp expect actual
+ "
+}
+
+relative_path() {
+ expected=$(test-tool path-utils print_path "$3")
+ test_expect_success $4 "relative path: $1 $2 => $3" "
+ echo '$expected' >expect &&
+ test-tool path-utils relative_path '$1' '$2' >actual &&
+ test_cmp expect actual
+ "
+}
+
+test_submodule_relative_url() {
+ test_expect_success "test_submodule_relative_url: $1 $2 $3 => $4" "
+ echo '$4' >expect &&
+ test-tool submodule resolve-relative-url '$1' '$2' '$3' >actual &&
+ test_cmp expect actual
+ "
+}
+
+test_git_path() {
+ test_expect_success "git-path $1 $2 => $3" "
+ $1 git rev-parse --git-path $2 >actual &&
+ echo $3 >expect &&
+ test_cmp expect actual
+ "
+}
+
+# On Windows, we are using MSYS's bash, which mangles the paths.
+# Absolute paths are anchored at the MSYS installation directory,
+# which means that the path / accounts for this many characters:
+rootoff=$(test-tool path-utils normalize_path_copy / | wc -c)
+# Account for the trailing LF:
+if test $rootoff = 2; then
+ rootoff= # we are on Unix
+else
+ rootoff=$(($rootoff-1))
+ # In MSYS2, the root directory "/" is translated into a Windows
+ # directory *with* trailing slash. Let's test for that and adjust
+ # our expected longest ancestor length accordingly.
+ case "$(test-tool path-utils print_path /)" in
+ */) rootslash=1;;
+ *) rootslash=0;;
+ esac
+fi
+
+ancestor() {
+ # We do some math with the expected ancestor length.
+ expected=$3
+ case "$rootoff,$expected,$2" in
+ *,*,//*) ;; # leave UNC paths alone
+ [0-9]*,[0-9]*,/*)
+ # On Windows, expect MSYS2 pseudo root translation for
+ # Unix-style absolute paths
+ expected=$(($expected-$rootslash+$rootoff))
+ ;;
+ esac
+ test_expect_success $4 "longest ancestor: $1 $2 => $expected" "
+ echo '$expected' >expect &&
+ test-tool path-utils longest_ancestor_length '$1' '$2' >actual &&
+ test_cmp expect actual
+ "
+}
+
+# Some absolute path tests should be skipped on Windows due to path mangling
+# on POSIX-style absolute paths
+case $(uname -s) in
+*MINGW*)
+ ;;
+*CYGWIN*)
+ ;;
+*)
+ test_set_prereq POSIX
+ ;;
+esac
+
+test_expect_success basename 'test-tool path-utils basename'
+test_expect_success dirname 'test-tool path-utils dirname'
+
+norm_path "" ""
+norm_path . ""
+norm_path ./ ""
+norm_path ./. ""
+norm_path ./.. ++failed++
+norm_path ../. ++failed++
+norm_path ./../.// ++failed++
+norm_path dir/.. ""
+norm_path dir/sub/../.. ""
+norm_path dir/sub/../../.. ++failed++
+norm_path dir dir
+norm_path dir// dir/
+norm_path ./dir dir
+norm_path dir/. dir/
+norm_path dir///./ dir/
+norm_path dir//sub/.. dir/
+norm_path dir/sub/../ dir/
+norm_path dir/sub/../. dir/
+norm_path dir/s1/../s2/ dir/s2/
+norm_path d1/s1///s2/..//../s3/ d1/s3/
+norm_path d1/s1//../s2/../../d2 d2
+norm_path d1/.../d2 d1/.../d2
+norm_path d1/..././../d2 d1/d2
+
+norm_path / /
+norm_path // / POSIX
+norm_path /// / POSIX
+norm_path /. /
+norm_path /./ / POSIX
+norm_path /./.. ++failed++ POSIX
+norm_path /../. ++failed++
+norm_path /./../.// ++failed++ POSIX
+norm_path /dir/.. / POSIX
+norm_path /dir/sub/../.. / POSIX
+norm_path /dir/sub/../../.. ++failed++ POSIX
+norm_path /dir /dir
+norm_path /dir// /dir/
+norm_path /./dir /dir
+norm_path /dir/. /dir/
+norm_path /dir///./ /dir/
+norm_path /dir//sub/.. /dir/
+norm_path /dir/sub/../ /dir/
+norm_path //dir/sub/../. /dir/ POSIX
+norm_path /dir/s1/../s2/ /dir/s2/
+norm_path /d1/s1///s2/..//../s3/ /d1/s3/
+norm_path /d1/s1//../s2/../../d2 /d2
+norm_path /d1/.../d2 /d1/.../d2
+norm_path /d1/..././../d2 /d1/d2
+
+ancestor / / -1
+ancestor /foo / 0
+ancestor /foo /fo -1
+ancestor /foo /foo -1
+ancestor /foo /bar -1
+ancestor /foo /foo/bar -1
+ancestor /foo /foo:/bar -1
+ancestor /foo /:/foo:/bar 0
+ancestor /foo /foo:/:/bar 0
+ancestor /foo /:/bar:/foo 0
+ancestor /foo/bar / 0
+ancestor /foo/bar /fo -1
+ancestor /foo/bar /foo 4
+ancestor /foo/bar /foo/ba -1
+ancestor /foo/bar /:/fo 0
+ancestor /foo/bar /foo:/foo/ba 4
+ancestor /foo/bar /bar -1
+ancestor /foo/bar /fo -1
+ancestor /foo/bar /foo:/bar 4
+ancestor /foo/bar /:/foo:/bar 4
+ancestor /foo/bar /foo:/:/bar 4
+ancestor /foo/bar /:/bar:/fo 0
+ancestor /foo/bar /:/bar 0
+ancestor /foo/bar /foo 4
+ancestor /foo/bar /foo:/bar 4
+ancestor /foo/bar /bar -1
+
+# Windows-specific: DOS drives, network shares
+ancestor C:/Users/me C:/ 2 MINGW
+ancestor D:/Users/me C:/ -1 MINGW
+ancestor //server/share/my-directory //server/share/ 14 MINGW
+
+test_expect_success 'strip_path_suffix' '
+ echo c:/msysgit >expect &&
+ test-tool path-utils strip_path_suffix \
+ c:/msysgit/libexec//git-core libexec/git-core >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'absolute path rejects the empty string' '
+ test_must_fail test-tool path-utils absolute_path ""
+'
+
+test_expect_success MINGW '<drive-letter>:\\abc is an absolute path' '
+ for letter in : \" C Z 1 ä
+ do
+ path=$letter:\\abc &&
+ absolute="$(test-tool path-utils absolute_path "$path")" &&
+ test "$path" = "$absolute" || return 1
+ done
+'
+
+test_expect_success 'real path rejects the empty string' '
+ test_must_fail test-tool path-utils real_path ""
+'
+
+test_expect_success POSIX 'real path works on absolute paths 1' '
+ echo / >expect &&
+ test-tool path-utils real_path "/" >actual &&
+ test_cmp expect actual &&
+
+ nopath="hopefully-absent-path" &&
+ echo "/$nopath" >expect &&
+ test-tool path-utils real_path "/$nopath" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'real path works on absolute paths 2' '
+ # Find an existing top-level directory for the remaining tests:
+ d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
+ echo "$d" >expect &&
+ test-tool path-utils real_path "$d" >actual &&
+ test_cmp expect actual &&
+
+ nopath="hopefully-absent-path" &&
+ echo "$d/$nopath" >expect &&
+ test-tool path-utils real_path "$d/$nopath" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success POSIX 'real path removes extra leading slashes' '
+ echo "/" >expect &&
+ test-tool path-utils real_path "///" >actual &&
+ test_cmp expect actual &&
+
+ nopath="hopefully-absent-path" &&
+ echo "/$nopath" >expect &&
+ test-tool path-utils real_path "///$nopath" >actual &&
+ test_cmp expect actual &&
+
+ # Find an existing top-level directory for the remaining tests:
+ d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
+ echo "$d" >expect &&
+ test-tool path-utils real_path "//$d" >actual &&
+ test_cmp expect actual &&
+
+ echo "$d/$nopath" >expect &&
+ test-tool path-utils real_path "//$d/$nopath" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'real path removes other extra slashes' '
+ # Find an existing top-level directory for the remaining tests:
+ d=$(pwd -P | sed -e "s|^\([^/]*/[^/]*\)/.*|\1|") &&
+ echo "$d" >expect &&
+ test-tool path-utils real_path "$d///" >actual &&
+ test_cmp expect actual &&
+
+ nopath="hopefully-absent-path" &&
+ echo "$d/$nopath" >expect &&
+ test-tool path-utils real_path "$d///$nopath" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success SYMLINKS 'real path works on symlinks' '
+ mkdir first &&
+ ln -s ../.git first/.git &&
+ mkdir second &&
+ ln -s ../first second/other &&
+ mkdir third &&
+ dir="$(cd .git && pwd -P)" &&
+ dir2=third/../second/other/.git &&
+ echo "$dir" >expect &&
+ test-tool path-utils real_path $dir2 >actual &&
+ test_cmp expect actual &&
+ file="$dir"/index &&
+ echo "$file" >expect &&
+ test-tool path-utils real_path $dir2/index >actual &&
+ test_cmp expect actual &&
+ basename=blub &&
+ echo "$dir/$basename" >expect &&
+ test-tool -C .git path-utils real_path "$basename" >actual &&
+ test_cmp expect actual &&
+ ln -s ../first/file .git/syml &&
+ sym="$(cd first && pwd -P)"/file &&
+ echo "$sym" >expect &&
+ test-tool path-utils real_path "$dir2/syml" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success SYMLINKS 'prefix_path works with absolute paths to work tree symlinks' '
+ ln -s target symlink &&
+ echo "symlink" >expect &&
+ test-tool path-utils prefix_path prefix "$(pwd)/symlink" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'prefix_path works with only absolute path to work tree' '
+ echo "" >expected &&
+ test-tool path-utils prefix_path prefix "$(pwd)" >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'prefix_path rejects absolute path to dir with same beginning as work tree' '
+ test_must_fail test-tool path-utils prefix_path prefix "$(pwd)a"
+'
+
+test_expect_success SYMLINKS 'prefix_path works with absolute path to a symlink to work tree having same beginning as work tree' '
+ git init repo &&
+ ln -s repo repolink &&
+ echo "a" >expect &&
+ repo_path="$(cd repo && pwd)" &&
+ test-tool -C repo path-utils prefix_path prefix "$repo_path/../repolink/a" >actual &&
+ test_cmp expect actual
+'
+
+relative_path /foo/a/b/c/ /foo/a/b/ c/
+relative_path /foo/a/b/c/ /foo/a/b c/
+relative_path /foo/a//b//c/ ///foo/a/b// c/ POSIX
+relative_path /foo/a/b /foo/a/b ./
+relative_path /foo/a/b/ /foo/a/b ./
+relative_path /foo/a /foo/a/b ../
+relative_path / /foo/a/b/ ../../../
+relative_path /foo/a/c /foo/a/b/ ../c
+relative_path /foo/a/c /foo/a/b ../c
+relative_path /foo/x/y /foo/a/b/ ../../x/y
+relative_path /foo/a/b "<empty>" /foo/a/b
+relative_path /foo/a/b "<null>" /foo/a/b
+relative_path foo/a/b/c/ foo/a/b/ c/
+relative_path foo/a/b/c/ foo/a/b c/
+relative_path foo/a/b//c foo/a//b c
+relative_path foo/a/b/ foo/a/b/ ./
+relative_path foo/a/b/ foo/a/b ./
+relative_path foo/a foo/a/b ../
+relative_path foo/x/y foo/a/b ../../x/y
+relative_path foo/a/c foo/a/b ../c
+relative_path foo/a/b /foo/x/y foo/a/b
+relative_path /foo/a/b foo/x/y /foo/a/b
+relative_path d:/a/b D:/a/c ../b MINGW
+relative_path C:/a/b D:/a/c C:/a/b MINGW
+relative_path foo/a/b "<empty>" foo/a/b
+relative_path foo/a/b "<null>" foo/a/b
+relative_path "<empty>" /foo/a/b ./
+relative_path "<empty>" "<empty>" ./
+relative_path "<empty>" "<null>" ./
+relative_path "<null>" "<empty>" ./
+relative_path "<null>" "<null>" ./
+relative_path "<null>" /foo/a/b ./
+
+test_git_path A=B info/grafts .git/info/grafts
+test_git_path GIT_GRAFT_FILE=foo info/grafts foo
+test_git_path GIT_GRAFT_FILE=foo info/////grafts foo
+test_git_path GIT_INDEX_FILE=foo index foo
+test_git_path GIT_INDEX_FILE=foo index/foo .git/index/foo
+test_git_path GIT_INDEX_FILE=foo index2 .git/index2
+test_expect_success 'setup fake objects directory foo' 'mkdir foo'
+test_git_path GIT_OBJECT_DIRECTORY=foo objects foo
+test_git_path GIT_OBJECT_DIRECTORY=foo objects/foo foo/foo
+test_git_path GIT_OBJECT_DIRECTORY=foo objects2 .git/objects2
+test_expect_success 'setup common repository' 'git --git-dir=bar init'
+test_git_path GIT_COMMON_DIR=bar index .git/index
+test_git_path GIT_COMMON_DIR=bar index.lock .git/index.lock
+test_git_path GIT_COMMON_DIR=bar HEAD .git/HEAD
+test_git_path GIT_COMMON_DIR=bar logs/HEAD .git/logs/HEAD
+test_git_path GIT_COMMON_DIR=bar logs/HEAD.lock .git/logs/HEAD.lock
+test_git_path GIT_COMMON_DIR=bar logs/refs/bisect/foo .git/logs/refs/bisect/foo
+test_git_path GIT_COMMON_DIR=bar logs/refs bar/logs/refs
+test_git_path GIT_COMMON_DIR=bar logs/refs/ bar/logs/refs/
+test_git_path GIT_COMMON_DIR=bar logs/refs/bisec/foo bar/logs/refs/bisec/foo
+test_git_path GIT_COMMON_DIR=bar logs/refs/bisec bar/logs/refs/bisec
+test_git_path GIT_COMMON_DIR=bar logs/refs/bisectfoo bar/logs/refs/bisectfoo
+test_git_path GIT_COMMON_DIR=bar objects bar/objects
+test_git_path GIT_COMMON_DIR=bar objects/bar bar/objects/bar
+test_git_path GIT_COMMON_DIR=bar info/exclude bar/info/exclude
+test_git_path GIT_COMMON_DIR=bar info/grafts bar/info/grafts
+test_git_path GIT_COMMON_DIR=bar info/sparse-checkout .git/info/sparse-checkout
+test_git_path GIT_COMMON_DIR=bar info//sparse-checkout .git/info//sparse-checkout
+test_git_path GIT_COMMON_DIR=bar remotes/bar bar/remotes/bar
+test_git_path GIT_COMMON_DIR=bar branches/bar bar/branches/bar
+test_git_path GIT_COMMON_DIR=bar logs/refs/heads/main bar/logs/refs/heads/main
+test_git_path GIT_COMMON_DIR=bar refs/heads/main bar/refs/heads/main
+test_git_path GIT_COMMON_DIR=bar refs/bisect/foo .git/refs/bisect/foo
+test_git_path GIT_COMMON_DIR=bar hooks/me bar/hooks/me
+test_git_path GIT_COMMON_DIR=bar config bar/config
+test_git_path GIT_COMMON_DIR=bar packed-refs bar/packed-refs
+test_git_path GIT_COMMON_DIR=bar shallow bar/shallow
+test_git_path GIT_COMMON_DIR=bar common bar/common
+test_git_path GIT_COMMON_DIR=bar common/file bar/common/file
+
+# In the tests below, $(pwd) must be used because it is a native path on
+# Windows and avoids MSYS's path mangling (which simplifies "foo/../bar" and
+# strips the dot from trailing "/.").
+
+test_submodule_relative_url "../" "../foo" "../submodule" "../../submodule"
+test_submodule_relative_url "../" "../foo/bar" "../submodule" "../../foo/submodule"
+test_submodule_relative_url "../" "../foo/submodule" "../submodule" "../../foo/submodule"
+test_submodule_relative_url "../" "./foo" "../submodule" "../submodule"
+test_submodule_relative_url "../" "./foo/bar" "../submodule" "../foo/submodule"
+test_submodule_relative_url "../../../" "../foo/bar" "../sub/a/b/c" "../../../../foo/sub/a/b/c"
+test_submodule_relative_url "../" "$(pwd)/addtest" "../repo" "$(pwd)/repo"
+test_submodule_relative_url "../" "foo/bar" "../submodule" "../foo/submodule"
+test_submodule_relative_url "../" "foo" "../submodule" "../submodule"
+
+test_submodule_relative_url "(null)" "../foo/bar" "../sub/a/b/c" "../foo/sub/a/b/c"
+test_submodule_relative_url "(null)" "../foo/bar" "../sub/a/b/c/" "../foo/sub/a/b/c"
+test_submodule_relative_url "(null)" "../foo/bar/" "../sub/a/b/c" "../foo/sub/a/b/c"
+test_submodule_relative_url "(null)" "../foo/bar" "../submodule" "../foo/submodule"
+test_submodule_relative_url "(null)" "../foo/submodule" "../submodule" "../foo/submodule"
+test_submodule_relative_url "(null)" "../foo" "../submodule" "../submodule"
+test_submodule_relative_url "(null)" "./foo/bar" "../submodule" "foo/submodule"
+test_submodule_relative_url "(null)" "./foo" "../submodule" "submodule"
+test_submodule_relative_url "(null)" "//somewhere else/repo" "../subrepo" "//somewhere else/subrepo"
+test_submodule_relative_url "(null)" "//somewhere else/repo" "../../subrepo" "//subrepo"
+test_submodule_relative_url "(null)" "//somewhere else/repo" "../../../subrepo" "/subrepo"
+test_submodule_relative_url "(null)" "//somewhere else/repo" "../../../../subrepo" "subrepo"
+test_submodule_relative_url "(null)" "$(pwd)/subsuper_update_r" "../subsubsuper_update_r" "$(pwd)/subsubsuper_update_r"
+test_submodule_relative_url "(null)" "$(pwd)/super_update_r2" "../subsuper_update_r" "$(pwd)/subsuper_update_r"
+test_submodule_relative_url "(null)" "$(pwd)/." "../." "$(pwd)/."
+test_submodule_relative_url "(null)" "$(pwd)" "./." "$(pwd)/."
+test_submodule_relative_url "(null)" "$(pwd)/addtest" "../repo" "$(pwd)/repo"
+test_submodule_relative_url "(null)" "$(pwd)" "./å äö" "$(pwd)/å äö"
+test_submodule_relative_url "(null)" "$(pwd)/." "../submodule" "$(pwd)/submodule"
+test_submodule_relative_url "(null)" "$(pwd)/submodule" "../submodule" "$(pwd)/submodule"
+test_submodule_relative_url "(null)" "$(pwd)/home2/../remote" "../bundle1" "$(pwd)/home2/../bundle1"
+test_submodule_relative_url "(null)" "$(pwd)/submodule_update_repo" "./." "$(pwd)/submodule_update_repo/."
+test_submodule_relative_url "(null)" "file:///tmp/repo" "../subrepo" "file:///tmp/subrepo"
+test_submodule_relative_url "(null)" "foo/bar" "../submodule" "foo/submodule"
+test_submodule_relative_url "(null)" "foo" "../submodule" "submodule"
+test_submodule_relative_url "(null)" "helper:://hostname/repo" "../subrepo" "helper:://hostname/subrepo"
+test_submodule_relative_url "(null)" "helper:://hostname/repo" "../../subrepo" "helper:://subrepo"
+test_submodule_relative_url "(null)" "helper:://hostname/repo" "../../../subrepo" "helper::/subrepo"
+test_submodule_relative_url "(null)" "helper:://hostname/repo" "../../../../subrepo" "helper::subrepo"
+test_submodule_relative_url "(null)" "helper:://hostname/repo" "../../../../../subrepo" "helper:subrepo"
+test_submodule_relative_url "(null)" "helper:://hostname/repo" "../../../../../../subrepo" ".:subrepo"
+test_submodule_relative_url "(null)" "ssh://hostname/repo" "../subrepo" "ssh://hostname/subrepo"
+test_submodule_relative_url "(null)" "ssh://hostname/repo" "../../subrepo" "ssh://subrepo"
+test_submodule_relative_url "(null)" "ssh://hostname/repo" "../../../subrepo" "ssh:/subrepo"
+test_submodule_relative_url "(null)" "ssh://hostname/repo" "../../../../subrepo" "ssh:subrepo"
+test_submodule_relative_url "(null)" "ssh://hostname/repo" "../../../../../subrepo" ".:subrepo"
+test_submodule_relative_url "(null)" "ssh://hostname:22/repo" "../subrepo" "ssh://hostname:22/subrepo"
+test_submodule_relative_url "(null)" "user@host:path/to/repo" "../subrepo" "user@host:path/to/subrepo"
+test_submodule_relative_url "(null)" "user@host:repo" "../subrepo" "user@host:subrepo"
+test_submodule_relative_url "(null)" "user@host:repo" "../../subrepo" ".:subrepo"
+
+test_expect_success 'match .gitmodules' '
+ test-tool path-utils is_dotgitmodules \
+ .gitmodules \
+ \
+ .git${u200c}modules \
+ \
+ .Gitmodules \
+ .gitmoduleS \
+ \
+ ".gitmodules " \
+ ".gitmodules." \
+ ".gitmodules " \
+ ".gitmodules. " \
+ ".gitmodules ." \
+ ".gitmodules.." \
+ ".gitmodules " \
+ ".gitmodules. " \
+ ".gitmodules . " \
+ ".gitmodules ." \
+ \
+ ".Gitmodules " \
+ ".Gitmodules." \
+ ".Gitmodules " \
+ ".Gitmodules. " \
+ ".Gitmodules ." \
+ ".Gitmodules.." \
+ ".Gitmodules " \
+ ".Gitmodules. " \
+ ".Gitmodules . " \
+ ".Gitmodules ." \
+ \
+ GITMOD~1 \
+ gitmod~1 \
+ GITMOD~2 \
+ gitmod~3 \
+ GITMOD~4 \
+ \
+ "GITMOD~1 " \
+ "gitmod~2." \
+ "GITMOD~3 " \
+ "gitmod~4. " \
+ "GITMOD~1 ." \
+ "gitmod~2 " \
+ "GITMOD~3. " \
+ "gitmod~4 . " \
+ \
+ GI7EBA~1 \
+ gi7eba~9 \
+ \
+ GI7EB~10 \
+ GI7EB~11 \
+ GI7EB~99 \
+ GI7EB~10 \
+ GI7E~100 \
+ GI7E~101 \
+ GI7E~999 \
+ ~1000000 \
+ ~9999999 \
+ \
+ .gitmodules:\$DATA \
+ "gitmod~4 . :\$DATA" \
+ \
+ --not \
+ ".gitmodules x" \
+ ".gitmodules .x" \
+ \
+ " .gitmodules" \
+ \
+ ..gitmodules \
+ \
+ gitmodules \
+ \
+ .gitmodule \
+ \
+ ".gitmodules x " \
+ ".gitmodules .x" \
+ \
+ GI7EBA~ \
+ GI7EBA~0 \
+ GI7EBA~~1 \
+ GI7EBA~X \
+ Gx7EBA~1 \
+ GI7EBX~1 \
+ \
+ GI7EB~1 \
+ GI7EB~01 \
+ GI7EB~1X \
+ \
+ .gitmodules,:\$DATA
+'
+
+test_expect_success 'match .gitattributes' '
+ test-tool path-utils is_dotgitattributes \
+ .gitattributes \
+ .git${u200c}attributes \
+ .Gitattributes \
+ .gitattributeS \
+ GITATT~1 \
+ GI7D29~1
+'
+
+test_expect_success 'match .gitignore' '
+ test-tool path-utils is_dotgitignore \
+ .gitignore \
+ .git${u200c}ignore \
+ .Gitignore \
+ .gitignorE \
+ GITIGN~1 \
+ GI250A~1
+'
+
+test_expect_success 'match .mailmap' '
+ test-tool path-utils is_dotmailmap \
+ .mailmap \
+ .mail${u200c}map \
+ .Mailmap \
+ .mailmaP \
+ MAILMA~1 \
+ MABA30~1
+'
+
+test_expect_success MINGW 'is_valid_path() on Windows' '
+ test-tool path-utils is_valid_path \
+ win32 \
+ "win32 x" \
+ ../hello.txt \
+ C:\\git \
+ comm \
+ conout.c \
+ com0.c \
+ lptN \
+ \
+ --not \
+ "win32 " \
+ "win32 /x " \
+ "win32." \
+ "win32 . ." \
+ .../hello.txt \
+ colon:test \
+ "AUX.c" \
+ "abc/conOut\$ .xyz/test" \
+ lpt8 \
+ com9.c \
+ "lpt*" \
+ Nul \
+ "PRN./abc"
+'
+
+test_lazy_prereq RUNTIME_PREFIX '
+ test true = "$RUNTIME_PREFIX"
+'
+
+test_lazy_prereq CAN_EXEC_IN_PWD '
+ cp "$GIT_EXEC_PATH"/git$X ./ &&
+ ./git rev-parse
+'
+
+test_expect_success !VALGRIND,RUNTIME_PREFIX,CAN_EXEC_IN_PWD 'RUNTIME_PREFIX works' '
+ mkdir -p pretend/bin pretend/libexec/git-core &&
+ echo "echo HERE" | write_script pretend/libexec/git-core/git-here &&
+ cp "$GIT_EXEC_PATH"/git$X pretend/bin/ &&
+ GIT_EXEC_PATH= ./pretend/bin/git here >actual &&
+ echo HERE >expect &&
+ test_cmp expect actual'
+
+test_expect_success !VALGRIND,RUNTIME_PREFIX,CAN_EXEC_IN_PWD '%(prefix)/ works' '
+ mkdir -p pretend/bin &&
+ cp "$GIT_EXEC_PATH"/git$X pretend/bin/ &&
+ git config yes.path "%(prefix)/yes" &&
+ GIT_EXEC_PATH= ./pretend/bin/git config --path yes.path >actual &&
+ echo "$(pwd)/pretend/yes" >expect &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t0061-run-command.sh b/t/t0061-run-command.sh
new file mode 100755
index 0000000..20986b6
--- /dev/null
+++ b/t/t0061-run-command.sh
@@ -0,0 +1,272 @@
+#!/bin/sh
+#
+# Copyright (c) 2009 Ilari Liusvaara
+#
+
+test_description='Test run command'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+cat >hello-script <<-EOF
+ #!$SHELL_PATH
+ cat hello-script
+EOF
+
+test_expect_success MINGW 'subprocess inherits only std handles' '
+ test-tool run-command inherited-handle
+'
+
+test_expect_success 'start_command reports ENOENT (slash)' '
+ test-tool run-command start-command-ENOENT ./does-not-exist 2>err &&
+ test_grep "\./does-not-exist" err
+'
+
+test_expect_success 'start_command reports ENOENT (no slash)' '
+ test-tool run-command start-command-ENOENT does-not-exist 2>err &&
+ test_grep "does-not-exist" err
+'
+
+test_expect_success 'run_command can run a command' '
+ cat hello-script >hello.sh &&
+ chmod +x hello.sh &&
+ test-tool run-command run-command ./hello.sh >actual 2>err &&
+
+ test_cmp hello-script actual &&
+ test_must_be_empty err
+'
+
+
+test_lazy_prereq RUNS_COMMANDS_FROM_PWD '
+ write_script runs-commands-from-pwd <<-\EOF &&
+ true
+ EOF
+ runs-commands-from-pwd >/dev/null 2>&1
+'
+
+test_expect_success !RUNS_COMMANDS_FROM_PWD 'run_command is restricted to PATH' '
+ write_script should-not-run <<-\EOF &&
+ echo yikes
+ EOF
+ test_must_fail test-tool run-command run-command should-not-run 2>err &&
+ test_grep "should-not-run" err
+'
+
+test_expect_success !MINGW 'run_command can run a script without a #! line' '
+ cat >hello <<-\EOF &&
+ cat hello-script
+ EOF
+ chmod +x hello &&
+ test-tool run-command run-command ./hello >actual 2>err &&
+
+ test_cmp hello-script actual &&
+ test_must_be_empty err
+'
+
+test_expect_success 'run_command does not try to execute a directory' '
+ test_when_finished "rm -rf bin1 bin2" &&
+ mkdir -p bin1/greet bin2 &&
+ write_script bin2/greet <<-\EOF &&
+ cat bin2/greet
+ EOF
+
+ PATH=$PWD/bin1:$PWD/bin2:$PATH \
+ test-tool run-command run-command greet >actual 2>err &&
+ test_cmp bin2/greet actual &&
+ test_must_be_empty err
+'
+
+test_expect_success POSIXPERM 'run_command passes over non-executable file' '
+ test_when_finished "rm -rf bin1 bin2" &&
+ mkdir -p bin1 bin2 &&
+ write_script bin1/greet <<-\EOF &&
+ cat bin1/greet
+ EOF
+ chmod -x bin1/greet &&
+ write_script bin2/greet <<-\EOF &&
+ cat bin2/greet
+ EOF
+
+ PATH=$PWD/bin1:$PWD/bin2:$PATH \
+ test-tool run-command run-command greet >actual 2>err &&
+ test_cmp bin2/greet actual &&
+ test_must_be_empty err
+'
+
+test_expect_success POSIXPERM 'run_command reports EACCES' '
+ cat hello-script >hello.sh &&
+ chmod -x hello.sh &&
+ test_must_fail test-tool run-command run-command ./hello.sh 2>err &&
+
+ grep "fatal: cannot exec.*hello.sh" err
+'
+
+test_expect_success POSIXPERM,SANITY 'unreadable directory in PATH' '
+ mkdir local-command &&
+ test_when_finished "chmod u+rwx local-command && rm -fr local-command" &&
+ git config alias.nitfol "!echo frotz" &&
+ chmod a-rx local-command &&
+ (
+ PATH=./local-command:$PATH &&
+ git nitfol >actual
+ ) &&
+ echo frotz >expect &&
+ test_cmp expect actual
+'
+
+cat >expect <<-EOF
+preloaded output of a child
+Hello
+World
+preloaded output of a child
+Hello
+World
+preloaded output of a child
+Hello
+World
+preloaded output of a child
+Hello
+World
+EOF
+
+test_expect_success 'run_command runs in parallel with more jobs available than tasks' '
+ test-tool run-command run-command-parallel 5 sh -c "printf \"%s\n%s\n\" Hello World" >out 2>actual &&
+ test_must_be_empty out &&
+ test_cmp expect actual
+'
+
+test_expect_success 'run_command runs ungrouped in parallel with more jobs available than tasks' '
+ test-tool run-command --ungroup run-command-parallel 5 sh -c "printf \"%s\n%s\n\" Hello World" >out 2>err &&
+ test_line_count = 8 out &&
+ test_line_count = 4 err
+'
+
+test_expect_success 'run_command runs in parallel with as many jobs as tasks' '
+ test-tool run-command run-command-parallel 4 sh -c "printf \"%s\n%s\n\" Hello World" >out 2>actual &&
+ test_must_be_empty out &&
+ test_cmp expect actual
+'
+
+test_expect_success 'run_command runs ungrouped in parallel with as many jobs as tasks' '
+ test-tool run-command --ungroup run-command-parallel 4 sh -c "printf \"%s\n%s\n\" Hello World" >out 2>err &&
+ test_line_count = 8 out &&
+ test_line_count = 4 err
+'
+
+test_expect_success 'run_command runs in parallel with more tasks than jobs available' '
+ test-tool run-command run-command-parallel 3 sh -c "printf \"%s\n%s\n\" Hello World" >out 2>actual &&
+ test_must_be_empty out &&
+ test_cmp expect actual
+'
+
+test_expect_success 'run_command runs ungrouped in parallel with more tasks than jobs available' '
+ test-tool run-command --ungroup run-command-parallel 3 sh -c "printf \"%s\n%s\n\" Hello World" >out 2>err &&
+ test_line_count = 8 out &&
+ test_line_count = 4 err
+'
+
+cat >expect <<-EOF
+preloaded output of a child
+asking for a quick stop
+preloaded output of a child
+asking for a quick stop
+preloaded output of a child
+asking for a quick stop
+EOF
+
+test_expect_success 'run_command is asked to abort gracefully' '
+ test-tool run-command run-command-abort 3 false >out 2>actual &&
+ test_must_be_empty out &&
+ test_cmp expect actual
+'
+
+test_expect_success 'run_command is asked to abort gracefully (ungroup)' '
+ test-tool run-command --ungroup run-command-abort 3 false >out 2>err &&
+ test_must_be_empty out &&
+ test_line_count = 6 err
+'
+
+cat >expect <<-EOF
+no further jobs available
+EOF
+
+test_expect_success 'run_command outputs ' '
+ test-tool run-command run-command-no-jobs 3 sh -c "printf \"%s\n%s\n\" Hello World" >out 2>actual &&
+ test_must_be_empty out &&
+ test_cmp expect actual
+'
+
+test_expect_success 'run_command outputs (ungroup) ' '
+ test-tool run-command --ungroup run-command-no-jobs 3 sh -c "printf \"%s\n%s\n\" Hello World" >out 2>err &&
+ test_must_be_empty out &&
+ test_cmp expect err
+'
+
+test_trace () {
+ expect="$1"
+ shift
+ GIT_TRACE=1 test-tool run-command "$@" run-command true 2>&1 >/dev/null | \
+ sed -e 's/.* run_command: //' -e '/trace: .*/d' \
+ -e '/RUNTIME_PREFIX requested/d' >actual &&
+ echo "$expect true" >expect &&
+ test_cmp expect actual
+}
+
+test_expect_success 'GIT_TRACE with environment variables' '
+ test_trace "abc=1 def=2" env abc=1 env def=2 &&
+ test_trace "abc=2" env abc env abc=1 env abc=2 &&
+ test_trace "abc=2" env abc env abc=2 &&
+ (
+ abc=1 && export abc &&
+ test_trace "def=1" env abc=1 env def=1
+ ) &&
+ (
+ abc=1 && export abc &&
+ test_trace "def=1" env abc env abc=1 env def=1
+ ) &&
+ test_trace "def=1" env non-exist env def=1 &&
+ test_trace "abc=2" env abc=1 env abc env abc=2 &&
+ (
+ abc=1 def=2 && export abc def &&
+ test_trace "unset abc def;" env abc env def
+ ) &&
+ (
+ abc=1 def=2 && export abc def &&
+ test_trace "unset def; abc=3" env abc env def env abc=3
+ ) &&
+ (
+ abc=1 && export abc &&
+ test_trace "unset abc;" env abc=2 env abc
+ )
+'
+
+test_expect_success MINGW 'verify curlies are quoted properly' '
+ : force the rev-parse through the MSYS2 Bash &&
+ git -c alias.r="!git rev-parse" r -- a{b}c >actual &&
+ cat >expect <<-\EOF &&
+ --
+ a{b}c
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success MINGW 'can spawn .bat with argv[0] containing spaces' '
+ bat="$TRASH_DIRECTORY/bat with spaces in name.bat" &&
+
+ # Every .bat invocation will log its arguments to file "out"
+ rm -f out &&
+ echo "echo %* >>out" >"$bat" &&
+
+ # Ask git to invoke .bat; clone will fail due to fake SSH helper
+ test_must_fail env GIT_SSH="$bat" git clone myhost:src ssh-clone &&
+
+ # Spawning .bat can fail if there are two quoted cmd.exe arguments.
+ # .bat itself is first (due to spaces in name), so just one more is
+ # needed to verify. GIT_SSH will invoke .bat multiple times:
+ # 1) -G myhost
+ # 2) myhost "git-upload-pack src"
+ # First invocation will always succeed. Test the second one.
+ grep "git-upload-pack" out
+'
+
+test_done
diff --git a/t/t0062-revision-walking.sh b/t/t0062-revision-walking.sh
new file mode 100755
index 0000000..b9480c8
--- /dev/null
+++ b/t/t0062-revision-walking.sh
@@ -0,0 +1,34 @@
+#!/bin/sh
+#
+# Copyright (c) 2012 Heiko Voigt
+#
+
+test_description='Test revision walking api'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+cat >run_twice_expected <<-EOF
+1st
+ > add b
+ > add a
+2nd
+ > add b
+ > add a
+EOF
+
+test_expect_success 'setup' '
+ echo a > a &&
+ git add a &&
+ git commit -m "add a" &&
+ echo b > b &&
+ git add b &&
+ git commit -m "add b"
+'
+
+test_expect_success 'revision walking can be done twice' '
+ test-tool revision-walking run-twice >run_twice_actual &&
+ test_cmp run_twice_expected run_twice_actual
+'
+
+test_done
diff --git a/t/t0063-string-list.sh b/t/t0063-string-list.sh
new file mode 100755
index 0000000..1fee6d9
--- /dev/null
+++ b/t/t0063-string-list.sh
@@ -0,0 +1,143 @@
+#!/bin/sh
+#
+# Copyright (c) 2012 Michael Haggerty
+#
+
+test_description='Test string list functionality'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_split () {
+ cat >expected &&
+ test_expect_success "split $1 at $2, max $3" "
+ test-tool string-list split '$1' '$2' '$3' >actual &&
+ test_cmp expected actual &&
+ test-tool string-list split_in_place '$1' '$2' '$3' >actual &&
+ test_cmp expected actual
+ "
+}
+
+test_split_in_place() {
+ cat >expected &&
+ test_expect_success "split (in place) $1 at $2, max $3" "
+ test-tool string-list split_in_place '$1' '$2' '$3' >actual &&
+ test_cmp expected actual
+ "
+}
+
+test_split "foo:bar:baz" ":" "-1" <<EOF
+3
+[0]: "foo"
+[1]: "bar"
+[2]: "baz"
+EOF
+
+test_split "foo:bar:baz" ":" "0" <<EOF
+1
+[0]: "foo:bar:baz"
+EOF
+
+test_split "foo:bar:baz" ":" "1" <<EOF
+2
+[0]: "foo"
+[1]: "bar:baz"
+EOF
+
+test_split "foo:bar:baz" ":" "2" <<EOF
+3
+[0]: "foo"
+[1]: "bar"
+[2]: "baz"
+EOF
+
+test_split "foo:bar:" ":" "-1" <<EOF
+3
+[0]: "foo"
+[1]: "bar"
+[2]: ""
+EOF
+
+test_split "" ":" "-1" <<EOF
+1
+[0]: ""
+EOF
+
+test_split ":" ":" "-1" <<EOF
+2
+[0]: ""
+[1]: ""
+EOF
+
+test_split_in_place "foo:;:bar:;:baz:;:" ":;" "-1" <<EOF
+10
+[0]: "foo"
+[1]: ""
+[2]: ""
+[3]: "bar"
+[4]: ""
+[5]: ""
+[6]: "baz"
+[7]: ""
+[8]: ""
+[9]: ""
+EOF
+
+test_split_in_place "foo:;:bar:;:baz" ":;" "0" <<EOF
+1
+[0]: "foo:;:bar:;:baz"
+EOF
+
+test_split_in_place "foo:;:bar:;:baz" ":;" "1" <<EOF
+2
+[0]: "foo"
+[1]: ";:bar:;:baz"
+EOF
+
+test_split_in_place "foo:;:bar:;:baz" ":;" "2" <<EOF
+3
+[0]: "foo"
+[1]: ""
+[2]: ":bar:;:baz"
+EOF
+
+test_split_in_place "foo:;:bar:;:" ":;" "-1" <<EOF
+7
+[0]: "foo"
+[1]: ""
+[2]: ""
+[3]: "bar"
+[4]: ""
+[5]: ""
+[6]: ""
+EOF
+
+test_expect_success "test filter_string_list" '
+ test "x-" = "x$(test-tool string-list filter - y)" &&
+ test "x-" = "x$(test-tool string-list filter no y)" &&
+ test yes = "$(test-tool string-list filter yes y)" &&
+ test yes = "$(test-tool string-list filter no:yes y)" &&
+ test yes = "$(test-tool string-list filter yes:no y)" &&
+ test y1:y2 = "$(test-tool string-list filter y1:y2 y)" &&
+ test y2:y1 = "$(test-tool string-list filter y2:y1 y)" &&
+ test "x-" = "x$(test-tool string-list filter x1:x2 y)"
+'
+
+test_expect_success "test remove_duplicates" '
+ test "x-" = "x$(test-tool string-list remove_duplicates -)" &&
+ test "x" = "x$(test-tool string-list remove_duplicates "")" &&
+ test a = "$(test-tool string-list remove_duplicates a)" &&
+ test a = "$(test-tool string-list remove_duplicates a:a)" &&
+ test a = "$(test-tool string-list remove_duplicates a:a:a:a:a)" &&
+ test a:b = "$(test-tool string-list remove_duplicates a:b)" &&
+ test a:b = "$(test-tool string-list remove_duplicates a:a:b)" &&
+ test a:b = "$(test-tool string-list remove_duplicates a:b:b)" &&
+ test a:b:c = "$(test-tool string-list remove_duplicates a:b:c)" &&
+ test a:b:c = "$(test-tool string-list remove_duplicates a:a:b:c)" &&
+ test a:b:c = "$(test-tool string-list remove_duplicates a:b:b:c)" &&
+ test a:b:c = "$(test-tool string-list remove_duplicates a:b:c:c)" &&
+ test a:b:c = "$(test-tool string-list remove_duplicates a:a:b:b:c:c)" &&
+ test a:b:c = "$(test-tool string-list remove_duplicates a:a:a:b:b:b:c:c:c)"
+'
+
+test_done
diff --git a/t/t0064-oid-array.sh b/t/t0064-oid-array.sh
new file mode 100755
index 0000000..88c89e8
--- /dev/null
+++ b/t/t0064-oid-array.sh
@@ -0,0 +1,104 @@
+#!/bin/sh
+
+test_description='basic tests for the oid array implementation'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+echoid () {
+ prefix="${1:+$1 }"
+ shift
+ while test $# -gt 0
+ do
+ echo "$prefix$ZERO_OID" | sed -e "s/00/$1/g"
+ shift
+ done
+}
+
+test_expect_success 'ordered enumeration' '
+ echoid "" 44 55 88 aa >expect &&
+ {
+ echoid append 88 44 aa 55 &&
+ echo for_each_unique
+ } | test-tool oid-array >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'ordered enumeration with duplicate suppression' '
+ echoid "" 44 55 88 aa >expect &&
+ {
+ echoid append 88 44 aa 55 &&
+ echoid append 88 44 aa 55 &&
+ echoid append 88 44 aa 55 &&
+ echo for_each_unique
+ } | test-tool oid-array >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'lookup' '
+ {
+ echoid append 88 44 aa 55 &&
+ echoid lookup 55
+ } | test-tool oid-array >actual &&
+ n=$(cat actual) &&
+ test "$n" -eq 1
+'
+
+test_expect_success 'lookup non-existing entry' '
+ {
+ echoid append 88 44 aa 55 &&
+ echoid lookup 33
+ } | test-tool oid-array >actual &&
+ n=$(cat actual) &&
+ test "$n" -lt 0
+'
+
+test_expect_success 'lookup with duplicates' '
+ {
+ echoid append 88 44 aa 55 &&
+ echoid append 88 44 aa 55 &&
+ echoid append 88 44 aa 55 &&
+ echoid lookup 55
+ } | test-tool oid-array >actual &&
+ n=$(cat actual) &&
+ test "$n" -ge 3 &&
+ test "$n" -le 5
+'
+
+test_expect_success 'lookup non-existing entry with duplicates' '
+ {
+ echoid append 88 44 aa 55 &&
+ echoid append 88 44 aa 55 &&
+ echoid append 88 44 aa 55 &&
+ echoid lookup 66
+ } | test-tool oid-array >actual &&
+ n=$(cat actual) &&
+ test "$n" -lt 0
+'
+
+test_expect_success 'lookup with almost duplicate values' '
+ # n-1 5s
+ root=$(echoid "" 55) &&
+ root=${root%5} &&
+ {
+ id1="${root}5" &&
+ id2="${root}f" &&
+ echo "append $id1" &&
+ echo "append $id2" &&
+ echoid lookup 55
+ } | test-tool oid-array >actual &&
+ n=$(cat actual) &&
+ test "$n" -eq 0
+'
+
+test_expect_success 'lookup with single duplicate value' '
+ {
+ echoid append 55 55 &&
+ echoid lookup 55
+ } | test-tool oid-array >actual &&
+ n=$(cat actual) &&
+ test "$n" -ge 0 &&
+ test "$n" -le 1
+'
+
+test_done
diff --git a/t/t0065-strcmp-offset.sh b/t/t0065-strcmp-offset.sh
new file mode 100755
index 0000000..94e34c8
--- /dev/null
+++ b/t/t0065-strcmp-offset.sh
@@ -0,0 +1,22 @@
+#!/bin/sh
+
+test_description='Test strcmp_offset functionality'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+while read s1 s2 expect
+do
+ test_expect_success "strcmp_offset($s1, $s2)" '
+ echo "$expect" >expect &&
+ test-tool strcmp-offset "$s1" "$s2" >actual &&
+ test_cmp expect actual
+ '
+done <<-EOF
+abc abc 0 3
+abc def -1 0
+abc abz -1 2
+abc abcdef -1 3
+EOF
+
+test_done
diff --git a/t/t0066-dir-iterator.sh b/t/t0066-dir-iterator.sh
new file mode 100755
index 0000000..7d0a0da
--- /dev/null
+++ b/t/t0066-dir-iterator.sh
@@ -0,0 +1,134 @@
+#!/bin/sh
+
+test_description='Test the dir-iterator functionality'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ mkdir -p dir &&
+ mkdir -p dir/a/b/c/ &&
+ >dir/b &&
+ >dir/c &&
+ mkdir -p dir/d/e/d/ &&
+ >dir/a/b/c/d &&
+ >dir/a/e &&
+ >dir/d/e/d/a &&
+
+ mkdir -p dir2/a/b/c/ &&
+ >dir2/a/b/c/d
+'
+
+test_expect_success 'dir-iterator should iterate through all files' '
+ cat >expected-iteration-sorted-output <<-EOF &&
+ [d] (a) [a] ./dir/a
+ [d] (a/b) [b] ./dir/a/b
+ [d] (a/b/c) [c] ./dir/a/b/c
+ [d] (d) [d] ./dir/d
+ [d] (d/e) [e] ./dir/d/e
+ [d] (d/e/d) [d] ./dir/d/e/d
+ [f] (a/b/c/d) [d] ./dir/a/b/c/d
+ [f] (a/e) [e] ./dir/a/e
+ [f] (b) [b] ./dir/b
+ [f] (c) [c] ./dir/c
+ [f] (d/e/d/a) [a] ./dir/d/e/d/a
+ EOF
+
+ test-tool dir-iterator ./dir >out &&
+ sort out >./actual-iteration-sorted-output &&
+
+ test_cmp expected-iteration-sorted-output actual-iteration-sorted-output
+'
+
+test_expect_success 'dir-iterator should list files in the correct order' '
+ cat >expected-pre-order-output <<-EOF &&
+ [d] (a) [a] ./dir2/a
+ [d] (a/b) [b] ./dir2/a/b
+ [d] (a/b/c) [c] ./dir2/a/b/c
+ [f] (a/b/c/d) [d] ./dir2/a/b/c/d
+ EOF
+
+ test-tool dir-iterator ./dir2 >actual-pre-order-output &&
+
+ test_cmp expected-pre-order-output actual-pre-order-output
+'
+
+test_expect_success 'begin should fail upon inexistent paths' '
+ test_must_fail test-tool dir-iterator ./inexistent-path \
+ >actual-inexistent-path-output &&
+ echo "dir_iterator_begin failure: ENOENT" >expected-inexistent-path-output &&
+ test_cmp expected-inexistent-path-output actual-inexistent-path-output
+'
+
+test_expect_success 'begin should fail upon non directory paths' '
+ test_must_fail test-tool dir-iterator ./dir/b >actual-non-dir-output &&
+ echo "dir_iterator_begin failure: ENOTDIR" >expected-non-dir-output &&
+ test_cmp expected-non-dir-output actual-non-dir-output
+'
+
+test_expect_success POSIXPERM,SANITY 'advance should not fail on errors by default' '
+ cat >expected-no-permissions-output <<-EOF &&
+ [d] (a) [a] ./dir3/a
+ EOF
+
+ mkdir -p dir3/a &&
+ >dir3/a/b &&
+ chmod 0 dir3/a &&
+
+ test-tool dir-iterator ./dir3 >actual-no-permissions-output &&
+ test_cmp expected-no-permissions-output actual-no-permissions-output &&
+ chmod 755 dir3/a &&
+ rm -rf dir3
+'
+
+test_expect_success POSIXPERM,SANITY 'advance should fail on errors, w/ pedantic flag' '
+ cat >expected-no-permissions-pedantic-output <<-EOF &&
+ [d] (a) [a] ./dir3/a
+ dir_iterator_advance failure
+ EOF
+
+ mkdir -p dir3/a &&
+ >dir3/a/b &&
+ chmod 0 dir3/a &&
+
+ test_must_fail test-tool dir-iterator --pedantic ./dir3 \
+ >actual-no-permissions-pedantic-output &&
+ test_cmp expected-no-permissions-pedantic-output \
+ actual-no-permissions-pedantic-output &&
+ chmod 755 dir3/a &&
+ rm -rf dir3
+'
+
+test_expect_success SYMLINKS 'setup dirs with symlinks' '
+ mkdir -p dir4/a &&
+ mkdir -p dir4/b/c &&
+ >dir4/a/d &&
+ ln -s d dir4/a/e &&
+ ln -s ../b dir4/a/f &&
+
+ ln -s dir4 dir5
+'
+
+test_expect_success SYMLINKS 'dir-iterator should not follow symlinks by default' '
+ cat >expected-no-follow-sorted-output <<-EOF &&
+ [d] (a) [a] ./dir4/a
+ [d] (b) [b] ./dir4/b
+ [d] (b/c) [c] ./dir4/b/c
+ [f] (a/d) [d] ./dir4/a/d
+ [s] (a/e) [e] ./dir4/a/e
+ [s] (a/f) [f] ./dir4/a/f
+ EOF
+
+ test-tool dir-iterator ./dir4 >out &&
+ sort out >actual-no-follow-sorted-output &&
+
+ test_cmp expected-no-follow-sorted-output actual-no-follow-sorted-output
+'
+
+test_expect_success SYMLINKS 'dir-iterator does not resolve top-level symlinks' '
+ test_must_fail test-tool dir-iterator ./dir5 >out &&
+
+ grep "ENOTDIR" out
+'
+
+test_done
diff --git a/t/t0067-parse_pathspec_file.sh b/t/t0067-parse_pathspec_file.sh
new file mode 100755
index 0000000..0188d04
--- /dev/null
+++ b/t/t0067-parse_pathspec_file.sh
@@ -0,0 +1,109 @@
+#!/bin/sh
+
+test_description='Test parse_pathspec_file()'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'one item from stdin' '
+ cat >expect <<-\EOF &&
+ fileA.t
+ EOF
+
+ echo fileA.t |
+ test-tool parse-pathspec-file --pathspec-from-file=- >actual &&
+
+ test_cmp expect actual
+'
+
+test_expect_success 'one item from file' '
+ cat >expect <<-\EOF &&
+ fileA.t
+ EOF
+
+ echo fileA.t >list &&
+ test-tool parse-pathspec-file --pathspec-from-file=list >actual &&
+
+ test_cmp expect actual
+'
+
+test_expect_success 'NUL delimiters' '
+ cat >expect <<-\EOF &&
+ fileA.t
+ fileB.t
+ EOF
+
+ printf "fileA.t\0fileB.t\0" |
+ test-tool parse-pathspec-file --pathspec-from-file=- --pathspec-file-nul >actual &&
+
+ test_cmp expect actual
+'
+
+test_expect_success 'LF delimiters' '
+ cat >expect <<-\EOF &&
+ fileA.t
+ fileB.t
+ EOF
+
+ printf "fileA.t\nfileB.t\n" |
+ test-tool parse-pathspec-file --pathspec-from-file=- >actual &&
+
+ test_cmp expect actual
+'
+
+test_expect_success 'no trailing delimiter' '
+ cat >expect <<-\EOF &&
+ fileA.t
+ fileB.t
+ EOF
+
+ printf "fileA.t\nfileB.t" |
+ test-tool parse-pathspec-file --pathspec-from-file=- >actual &&
+
+ test_cmp expect actual
+'
+
+test_expect_success 'CRLF delimiters' '
+ cat >expect <<-\EOF &&
+ fileA.t
+ fileB.t
+ EOF
+
+ printf "fileA.t\r\nfileB.t\r\n" |
+ test-tool parse-pathspec-file --pathspec-from-file=- >actual &&
+
+ test_cmp expect actual
+'
+
+test_expect_success 'quotes' '
+ cat >expect <<-\EOF &&
+ fileA.t
+ EOF
+
+ cat >list <<-\EOF &&
+ "file\101.t"
+ EOF
+
+ test-tool parse-pathspec-file --pathspec-from-file=list >actual &&
+
+ test_cmp expect actual
+'
+
+test_expect_success '--pathspec-file-nul takes quotes literally' '
+ # Note: there is an extra newline because --pathspec-file-nul takes
+ # input \n literally, too
+ cat >expect <<-\EOF &&
+ "file\101.t"
+
+ EOF
+
+ cat >list <<-\EOF &&
+ "file\101.t"
+ EOF
+
+ test-tool parse-pathspec-file --pathspec-from-file=list --pathspec-file-nul >actual &&
+
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t0068-for-each-repo.sh b/t/t0068-for-each-repo.sh
new file mode 100755
index 0000000..4b90b74
--- /dev/null
+++ b/t/t0068-for-each-repo.sh
@@ -0,0 +1,62 @@
+#!/bin/sh
+
+test_description='git for-each-repo builtin'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'run based on configured value' '
+ git init one &&
+ git init two &&
+ git init three &&
+ git init ~/four &&
+ git -C two commit --allow-empty -m "DID NOT RUN" &&
+ git config run.key "$TRASH_DIRECTORY/one" &&
+ git config --add run.key "$TRASH_DIRECTORY/three" &&
+ git config --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
+'
+
+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 >>.git/config <<-\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_done
diff --git a/t/t0069-oidtree.sh b/t/t0069-oidtree.sh
new file mode 100755
index 0000000..889db50
--- /dev/null
+++ b/t/t0069-oidtree.sh
@@ -0,0 +1,50 @@
+#!/bin/sh
+
+test_description='basic tests for the oidtree implementation'
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+maxhexsz=$(test_oid hexsz)
+echoid () {
+ prefix="${1:+$1 }"
+ shift
+ while test $# -gt 0
+ do
+ shortoid="$1"
+ shift
+ difference=$(($maxhexsz - ${#shortoid}))
+ printf "%s%s%0${difference}d\\n" "$prefix" "$shortoid" "0"
+ done
+}
+
+test_expect_success 'oidtree insert and contains' '
+ cat >expect <<-\EOF &&
+ 0
+ 0
+ 0
+ 1
+ 1
+ 0
+ EOF
+ {
+ echoid insert 444 1 2 3 4 5 a b c d e &&
+ echoid contains 44 441 440 444 4440 4444 &&
+ echo clear
+ } | test-tool oidtree >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'oidtree each' '
+ echoid "" 123 321 321 >expect &&
+ {
+ echoid insert f 9 8 123 321 a b c d e &&
+ echo each 12300 &&
+ echo each 3211 &&
+ echo each 3210 &&
+ echo each 32100 &&
+ echo clear
+ } | test-tool oidtree >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t0070-fundamental.sh b/t/t0070-fundamental.sh
new file mode 100755
index 0000000..487bc8d
--- /dev/null
+++ b/t/t0070-fundamental.sh
@@ -0,0 +1,56 @@
+#!/bin/sh
+
+test_description='check that the most basic functions work
+
+
+Verify wrappers and compatibility functions.
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'character classes (isspace, isalpha etc.)' '
+ test-tool ctype
+'
+
+test_expect_success 'mktemp to nonexistent directory prints filename' '
+ test_must_fail test-tool mktemp doesnotexist/testXXXXXX 2>err &&
+ grep "doesnotexist/test" err
+'
+
+test_expect_success POSIXPERM,SANITY 'mktemp to unwritable directory prints filename' '
+ mkdir cannotwrite &&
+ test_when_finished "chmod +w cannotwrite" &&
+ chmod -w cannotwrite &&
+ test_must_fail test-tool mktemp cannotwrite/testXXXXXX 2>err &&
+ grep "cannotwrite/test" err
+'
+
+test_expect_success 'git_mkstemps_mode does not fail if fd 0 is not open' '
+ git commit --allow-empty -m message <&-
+'
+
+test_expect_success 'check for a bug in the regex routines' '
+ # if this test fails, re-build git with NO_REGEX=1
+ test-tool regex --bug
+'
+
+test_expect_success 'incomplete sideband messages are reassembled' '
+ test-tool pkt-line send-split-sideband >split-sideband &&
+ test-tool pkt-line receive-sideband <split-sideband 2>err &&
+ grep "Hello, world" err
+'
+
+test_expect_success 'eof on sideband message is reported' '
+ printf 1234 >input &&
+ test-tool pkt-line receive-sideband <input 2>err &&
+ test_grep "unexpected disconnect" err
+'
+
+test_expect_success 'missing sideband designator is reported' '
+ printf 0004 >input &&
+ test-tool pkt-line receive-sideband <input 2>err &&
+ test_grep "missing sideband" err
+'
+
+test_done
diff --git a/t/t0071-sort.sh b/t/t0071-sort.sh
new file mode 100755
index 0000000..ba8ad1d
--- /dev/null
+++ b/t/t0071-sort.sh
@@ -0,0 +1,12 @@
+#!/bin/sh
+
+test_description='verify sort functions'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'DEFINE_LIST_SORT_DEBUG' '
+ test-tool mergesort test
+'
+
+test_done
diff --git a/t/t0081-find-pack.sh b/t/t0081-find-pack.sh
new file mode 100755
index 0000000..67b1121
--- /dev/null
+++ b/t/t0081-find-pack.sh
@@ -0,0 +1,82 @@
+#!/bin/sh
+
+test_description='test `test-tool find-pack`'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ test_commit one &&
+ test_commit two &&
+ test_commit three &&
+ test_commit four &&
+ test_commit five
+'
+
+test_expect_success 'repack everything into a single packfile' '
+ git repack -a -d --no-write-bitmap-index &&
+
+ head_commit_pack=$(test-tool find-pack HEAD) &&
+ head_tree_pack=$(test-tool find-pack HEAD^{tree}) &&
+ one_pack=$(test-tool find-pack HEAD:one.t) &&
+ three_pack=$(test-tool find-pack HEAD:three.t) &&
+ old_commit_pack=$(test-tool find-pack HEAD~4) &&
+
+ test-tool find-pack --check-count 1 HEAD &&
+ test-tool find-pack --check-count=1 HEAD^{tree} &&
+ ! test-tool find-pack --check-count=0 HEAD:one.t &&
+ ! test-tool find-pack -c 2 HEAD:one.t &&
+ test-tool find-pack -c 1 HEAD:three.t &&
+
+ # Packfile exists at the right path
+ case "$head_commit_pack" in
+ ".git/objects/pack/pack-"*".pack") true ;;
+ *) false ;;
+ esac &&
+ test -f "$head_commit_pack" &&
+
+ # Everything is in the same pack
+ test "$head_commit_pack" = "$head_tree_pack" &&
+ test "$head_commit_pack" = "$one_pack" &&
+ test "$head_commit_pack" = "$three_pack" &&
+ test "$head_commit_pack" = "$old_commit_pack"
+'
+
+test_expect_success 'add more packfiles' '
+ git rev-parse HEAD^{tree} HEAD:two.t HEAD:four.t >objects &&
+ git pack-objects .git/objects/pack/mypackname1 >packhash1 <objects &&
+
+ git rev-parse HEAD~ HEAD~^{tree} HEAD:five.t >objects &&
+ git pack-objects .git/objects/pack/mypackname2 >packhash2 <objects &&
+
+ head_commit_pack=$(test-tool find-pack HEAD) &&
+
+ # HEAD^{tree} is in 2 packfiles
+ test-tool find-pack HEAD^{tree} >head_tree_packs &&
+ grep "$head_commit_pack" head_tree_packs &&
+ grep mypackname1 head_tree_packs &&
+ ! grep mypackname2 head_tree_packs &&
+ test-tool find-pack --check-count 2 HEAD^{tree} &&
+ ! test-tool find-pack --check-count 1 HEAD^{tree} &&
+
+ # HEAD:five.t is also in 2 packfiles
+ test-tool find-pack HEAD:five.t >five_packs &&
+ grep "$head_commit_pack" five_packs &&
+ ! grep mypackname1 five_packs &&
+ grep mypackname2 five_packs &&
+ test-tool find-pack -c 2 HEAD:five.t &&
+ ! test-tool find-pack --check-count=0 HEAD:five.t
+'
+
+test_expect_success 'add more commits (as loose objects)' '
+ test_commit six &&
+ test_commit seven &&
+
+ test -z "$(test-tool find-pack HEAD)" &&
+ test -z "$(test-tool find-pack HEAD:six.t)" &&
+ test-tool find-pack --check-count 0 HEAD &&
+ test-tool find-pack -c 0 HEAD:six.t &&
+ ! test-tool find-pack -c 1 HEAD:seven.t
+'
+
+test_done
diff --git a/t/t0090-cache-tree.sh b/t/t0090-cache-tree.sh
new file mode 100755
index 0000000..d8e2fc4
--- /dev/null
+++ b/t/t0090-cache-tree.sh
@@ -0,0 +1,282 @@
+#!/bin/sh
+
+test_description="Test whether cache-tree is properly updated
+
+Tests whether various commands properly update and/or rewrite the
+cache-tree extension.
+"
+
+TEST_PASSES_SANITIZE_LEAK=true
+ . ./test-lib.sh
+
+cmp_cache_tree () {
+ test-tool dump-cache-tree | sed -e '/#(ref)/d' >actual &&
+ sed "s/$OID_REGEX/SHA/" <actual >filtered &&
+ test_cmp "$1" filtered &&
+ rm filtered
+}
+
+# We don't bother with actually checking the SHA1:
+# test-tool dump-cache-tree already verifies that all existing data is
+# correct.
+generate_expected_cache_tree () {
+ pathspec="$1" &&
+ dir="$2${2:+/}" &&
+ git ls-tree --name-only HEAD -- "$pathspec" >files &&
+ git ls-tree --name-only -d HEAD -- "$pathspec" >subtrees &&
+ printf "SHA %s (%d entries, %d subtrees)\n" "$dir" $(wc -l <files) $(wc -l <subtrees) &&
+ while read subtree
+ do
+ generate_expected_cache_tree "$pathspec/$subtree/" "$subtree" || return 1
+ done <subtrees
+}
+
+test_cache_tree () {
+ generate_expected_cache_tree "." >expect &&
+ cmp_cache_tree expect &&
+ rm expect actual files subtrees &&
+ git status --porcelain -- ':!status' ':!expected.status' >status &&
+ if test -n "$1"
+ then
+ test_cmp "$1" status
+ else
+ test_must_be_empty status
+ fi
+}
+
+test_invalid_cache_tree () {
+ printf "invalid %s ()\n" "" "$@" >expect &&
+ test-tool dump-cache-tree |
+ sed -n -e "s/[0-9]* subtrees//" -e '/#(ref)/d' -e '/^invalid /p' >actual &&
+ test_cmp expect actual
+}
+
+test_no_cache_tree () {
+ >expect &&
+ cmp_cache_tree expect
+}
+
+test_expect_success 'initial commit has cache-tree' '
+ test_commit foo &&
+ test_cache_tree
+'
+
+test_expect_success 'read-tree HEAD establishes cache-tree' '
+ git read-tree HEAD &&
+ test_cache_tree
+'
+
+test_expect_success 'git-add invalidates cache-tree' '
+ test_when_finished "git reset --hard; git read-tree HEAD" &&
+ echo "I changed this file" >foo &&
+ git add foo &&
+ test_invalid_cache_tree
+'
+
+test_expect_success 'git-add in subdir invalidates cache-tree' '
+ test_when_finished "git reset --hard; git read-tree HEAD" &&
+ mkdir dirx &&
+ echo "I changed this file" >dirx/foo &&
+ git add dirx/foo &&
+ test_invalid_cache_tree
+'
+
+test_expect_success 'git-add in subdir does not invalidate sibling cache-tree' '
+ git tag no-children &&
+ test_when_finished "git reset --hard no-children; git read-tree HEAD" &&
+ mkdir dir1 dir2 &&
+ test_commit dir1/a &&
+ test_commit dir2/b &&
+ echo "I changed this file" >dir1/a &&
+ test_when_finished "rm before" &&
+ cat >before <<-\EOF &&
+ SHA (3 entries, 2 subtrees)
+ SHA dir1/ (1 entries, 0 subtrees)
+ SHA dir2/ (1 entries, 0 subtrees)
+ EOF
+ cmp_cache_tree before &&
+ echo "I changed this file" >dir1/a &&
+ git add dir1/a &&
+ cat >expect <<-\EOF &&
+ invalid (2 subtrees)
+ invalid dir1/ (0 subtrees)
+ SHA dir2/ (1 entries, 0 subtrees)
+ EOF
+ cmp_cache_tree expect
+'
+
+test_expect_success 'update-index invalidates cache-tree' '
+ test_when_finished "git reset --hard; git read-tree HEAD" &&
+ echo "I changed this file" >foo &&
+ git update-index --add foo &&
+ test_invalid_cache_tree
+'
+
+test_expect_success 'write-tree establishes cache-tree' '
+ test-tool scrap-cache-tree &&
+ git write-tree &&
+ test_cache_tree
+'
+
+test_expect_success 'test-tool scrap-cache-tree works' '
+ git read-tree HEAD &&
+ test-tool scrap-cache-tree &&
+ test_no_cache_tree
+'
+
+test_expect_success 'second commit has cache-tree' '
+ test_commit bar &&
+ test_cache_tree
+'
+
+test_expect_success PERL 'commit --interactive gives cache-tree on partial commit' '
+ test_when_finished "git reset --hard" &&
+ cat <<-\EOT >foo.c &&
+ int foo()
+ {
+ return 42;
+ }
+ int bar()
+ {
+ return 42;
+ }
+ EOT
+ git add foo.c &&
+ test_invalid_cache_tree &&
+ git commit -m "add a file" &&
+ test_cache_tree &&
+ cat <<-\EOT >foo.c &&
+ int foo()
+ {
+ return 43;
+ }
+ int bar()
+ {
+ return 44;
+ }
+ EOT
+ test_write_lines p 1 "" s n y q |
+ git commit --interactive -m foo &&
+ cat <<-\EOF >expected.status &&
+ M foo.c
+ EOF
+ test_cache_tree expected.status
+'
+
+test_expect_success PERL 'commit -p with shrinking cache-tree' '
+ mkdir -p deep/very-long-subdir &&
+ echo content >deep/very-long-subdir/file &&
+ git add deep &&
+ git commit -m add &&
+ git rm -r deep &&
+
+ before=$(wc -c <.git/index) &&
+ git commit -m delete -p &&
+ after=$(wc -c <.git/index) &&
+
+ # double check that the index shrank
+ test $before -gt $after &&
+
+ # and that our index was not corrupted
+ git fsck
+'
+
+test_expect_success 'commit in child dir has cache-tree' '
+ mkdir dir &&
+ >dir/child.t &&
+ git add dir/child.t &&
+ git commit -m dir/child.t &&
+ test_cache_tree
+'
+
+test_expect_success 'reset --hard gives cache-tree' '
+ test-tool scrap-cache-tree &&
+ git reset --hard &&
+ test_cache_tree
+'
+
+test_expect_success 'reset --hard without index gives cache-tree' '
+ rm -f .git/index &&
+ git clean -fd &&
+ git reset --hard &&
+ test_cache_tree
+'
+
+test_expect_success 'checkout gives cache-tree' '
+ git tag current &&
+ git checkout HEAD^ &&
+ test_cache_tree
+'
+
+test_expect_success 'checkout -b gives cache-tree' '
+ git checkout current &&
+ git checkout -b prev HEAD^ &&
+ test_cache_tree
+'
+
+test_expect_success 'checkout -B gives cache-tree' '
+ git checkout current &&
+ git checkout -B prev HEAD^ &&
+ test_cache_tree
+'
+
+test_expect_success 'merge --ff-only maintains cache-tree' '
+ git checkout current &&
+ git checkout -b changes &&
+ test_commit llamas &&
+ test_commit pachyderm &&
+ test_cache_tree &&
+ git checkout current &&
+ test_cache_tree &&
+ git merge --ff-only changes &&
+ test_cache_tree
+'
+
+test_expect_success 'merge maintains cache-tree' '
+ git checkout current &&
+ git checkout -b changes2 &&
+ test_commit alpacas &&
+ test_cache_tree &&
+ git checkout current &&
+ test_commit struthio &&
+ test_cache_tree &&
+ git merge changes2 &&
+ test_cache_tree
+'
+
+test_expect_success 'partial commit gives cache-tree' '
+ git checkout -b partial no-children &&
+ test_commit one &&
+ test_commit two &&
+ echo "some change" >one.t &&
+ git add one.t &&
+ echo "some other change" >two.t &&
+ git commit two.t -m partial &&
+ cat <<-\EOF >expected.status &&
+ M one.t
+ EOF
+ test_cache_tree expected.status
+'
+
+test_expect_success 'no phantom error when switching trees' '
+ mkdir newdir &&
+ >newdir/one &&
+ git add newdir/one &&
+ git checkout 2>errors &&
+ test_must_be_empty errors
+'
+
+test_expect_success 'switching trees does not invalidate shared index' '
+ (
+ sane_unset GIT_TEST_SPLIT_INDEX &&
+ git update-index --split-index &&
+ >split &&
+ git add split &&
+ test-tool dump-split-index .git/index | grep -v ^own >before &&
+ git commit -m "as-is" &&
+ test-tool dump-split-index .git/index | grep -v ^own >after &&
+ test_cmp before after
+ )
+'
+
+test_done
diff --git a/t/t0091-bugreport.sh b/t/t0091-bugreport.sh
new file mode 100755
index 0000000..8798fee
--- /dev/null
+++ b/t/t0091-bugreport.sh
@@ -0,0 +1,157 @@
+#!/bin/sh
+
+test_description='git bugreport'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'create a report' '
+ git bugreport -s format &&
+ test_file_not_empty git-bugreport-format.txt
+'
+
+test_expect_success 'report contains wanted template (before first section)' '
+ sed -ne "/^\[/q;p" git-bugreport-format.txt >actual &&
+ cat >expect <<-\EOF &&
+ Thank you for filling out a Git bug report!
+ Please answer the following questions to help us understand your issue.
+
+ What did you do before the bug happened? (Steps to reproduce your issue)
+
+ What did you expect to happen? (Expected behavior)
+
+ What happened instead? (Actual behavior)
+
+ What'\''s different between what you expected and what actually happened?
+
+ Anything else you want to add:
+
+ Please review the rest of the bug report below.
+ You can delete any lines you don'\''t wish to share.
+
+
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'sanity check "System Info" section' '
+ test_when_finished rm -f git-bugreport-format.txt &&
+
+ sed -ne "/^\[System Info\]$/,/^$/p" <git-bugreport-format.txt >system &&
+
+ # The beginning should match "git version --build-info" verbatim,
+ # but rather than checking bit-for-bit equality, just test some basics.
+ grep "git version [0-9]." system &&
+ grep "shell-path: ." system &&
+
+ # After the version, there should be some more info.
+ # This is bound to differ from environment to environment,
+ # so we just do some rather high-level checks.
+ grep "uname: ." system &&
+ grep "compiler info: ." system
+'
+
+test_expect_success 'dies if file with same name as report already exists' '
+ test_when_finished rm git-bugreport-duplicate.txt &&
+ >>git-bugreport-duplicate.txt &&
+ test_must_fail git bugreport --suffix duplicate
+'
+
+test_expect_success '--output-directory puts the report in the provided dir' '
+ test_when_finished rm -fr foo/ &&
+ git bugreport -o foo/ &&
+ test_path_is_file foo/git-bugreport-*
+'
+
+test_expect_success 'incorrect arguments abort with usage' '
+ test_must_fail git bugreport --false 2>output &&
+ test_grep usage output &&
+ test_path_is_missing git-bugreport-*
+'
+
+test_expect_success 'incorrect positional arguments abort with usage and hint' '
+ test_must_fail git bugreport false 2>output &&
+ test_grep usage output &&
+ test_grep false output &&
+ test_path_is_missing git-bugreport-*
+'
+
+test_expect_success 'runs outside of a git dir' '
+ test_when_finished rm non-repo/git-bugreport-* &&
+ nongit git bugreport
+'
+
+test_expect_success 'can create leading directories outside of a git dir' '
+ test_when_finished rm -fr foo/bar/baz &&
+ nongit git bugreport -o foo/bar/baz
+'
+
+test_expect_success 'indicates populated hooks' '
+ test_when_finished rm git-bugreport-hooks.txt &&
+
+ test_hook applypatch-msg <<-\EOF &&
+ true
+ EOF
+ test_hook unknown-hook <<-\EOF &&
+ true
+ EOF
+ git bugreport -s hooks &&
+
+ sort >expect <<-\EOF &&
+ [Enabled Hooks]
+ applypatch-msg
+ EOF
+
+ sed -ne "/^\[Enabled Hooks\]$/,/^$/p" <git-bugreport-hooks.txt >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success UNZIP '--diagnose creates diagnostics zip archive' '
+ test_when_finished rm -rf report &&
+
+ git bugreport --diagnose -o report -s test >out &&
+
+ zip_path=report/git-diagnostics-test.zip &&
+ grep "Available space" out &&
+ test_path_is_file "$zip_path" &&
+
+ # Check zipped archive content
+ "$GIT_UNZIP" -p "$zip_path" diagnostics.log >out &&
+ test_file_not_empty out &&
+
+ "$GIT_UNZIP" -p "$zip_path" packs-local.txt >out &&
+ grep ".git/objects" out &&
+
+ "$GIT_UNZIP" -p "$zip_path" objects-local.txt >out &&
+ grep "^Total: [0-9][0-9]*" out &&
+
+ # Should not include .git directory contents by default
+ ! "$GIT_UNZIP" -l "$zip_path" | grep ".git/"
+'
+
+test_expect_success UNZIP '--diagnose=stats excludes .git dir contents' '
+ test_when_finished rm -rf report &&
+
+ git bugreport --diagnose=stats -o report -s test >out &&
+
+ # Includes pack quantity/size info
+ "$GIT_UNZIP" -p "$zip_path" packs-local.txt >out &&
+ grep ".git/objects" out &&
+
+ # Does not include .git directory contents
+ ! "$GIT_UNZIP" -l "$zip_path" | grep ".git/"
+'
+
+test_expect_success UNZIP '--diagnose=all includes .git dir contents' '
+ test_when_finished rm -rf report &&
+
+ git bugreport --diagnose=all -o report -s test >out &&
+
+ # Includes .git directory contents
+ "$GIT_UNZIP" -l "$zip_path" | grep ".git/" &&
+
+ "$GIT_UNZIP" -p "$zip_path" .git/HEAD >out &&
+ test_file_not_empty out
+'
+
+test_done
diff --git a/t/t0092-diagnose.sh b/t/t0092-diagnose.sh
new file mode 100755
index 0000000..133e574
--- /dev/null
+++ b/t/t0092-diagnose.sh
@@ -0,0 +1,72 @@
+#!/bin/sh
+
+test_description='git diagnose'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success UNZIP 'creates diagnostics zip archive' '
+ test_when_finished rm -rf report &&
+
+ git diagnose -o report -s test >out &&
+ grep "Available space" out &&
+
+ zip_path=report/git-diagnostics-test.zip &&
+ test_path_is_file "$zip_path" &&
+
+ # Check zipped archive content
+ "$GIT_UNZIP" -p "$zip_path" diagnostics.log >out &&
+ test_file_not_empty out &&
+
+ "$GIT_UNZIP" -p "$zip_path" packs-local.txt >out &&
+ grep ".git/objects" out &&
+
+ "$GIT_UNZIP" -p "$zip_path" objects-local.txt >out &&
+ grep "^Total: [0-9][0-9]*" out &&
+
+ # Should not include .git directory contents by default
+ ! "$GIT_UNZIP" -l "$zip_path" | grep ".git/"
+'
+
+test_expect_success UNZIP 'counts loose objects' '
+ test_commit A &&
+
+ # After committing, should have non-zero loose objects
+ git diagnose -o test-count -s 1 >out &&
+ zip_path=test-count/git-diagnostics-1.zip &&
+ "$GIT_UNZIP" -p "$zip_path" objects-local.txt >out &&
+ grep "^Total: [1-9][0-9]* loose objects" out
+'
+
+test_expect_success UNZIP '--mode=stats excludes .git dir contents' '
+ test_when_finished rm -rf report &&
+
+ git diagnose -o report -s test --mode=stats >out &&
+
+ # Includes pack quantity/size info
+ zip_path=report/git-diagnostics-test.zip &&
+ "$GIT_UNZIP" -p "$zip_path" packs-local.txt >out &&
+ grep ".git/objects" out &&
+
+ # Does not include .git directory contents
+ ! "$GIT_UNZIP" -l "$zip_path" | grep ".git/"
+'
+
+test_expect_success UNZIP '--mode=all includes .git dir contents' '
+ test_when_finished rm -rf report &&
+
+ git diagnose -o report -s test --mode=all >out &&
+
+ # Includes pack quantity/size info
+ zip_path=report/git-diagnostics-test.zip &&
+ "$GIT_UNZIP" -p "$zip_path" packs-local.txt >out &&
+ grep ".git/objects" out &&
+
+ # Includes .git directory contents
+ "$GIT_UNZIP" -l "$zip_path" | grep ".git/" &&
+
+ "$GIT_UNZIP" -p "$zip_path" .git/HEAD >out &&
+ test_file_not_empty out
+'
+
+test_done
diff --git a/t/t0095-bloom.sh b/t/t0095-bloom.sh
new file mode 100755
index 0000000..b567383
--- /dev/null
+++ b/t/t0095-bloom.sh
@@ -0,0 +1,119 @@
+#!/bin/sh
+
+test_description='Testing the various Bloom filter computations in bloom.c'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'compute unseeded murmur3 hash for empty string' '
+ cat >expect <<-\EOF &&
+ Murmur3 Hash with seed=0:0x00000000
+ EOF
+ test-tool bloom get_murmur3 "" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'compute unseeded murmur3 hash for test string 1' '
+ cat >expect <<-\EOF &&
+ Murmur3 Hash with seed=0:0x627b0c2c
+ EOF
+ test-tool bloom get_murmur3 "Hello world!" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'compute unseeded murmur3 hash for test string 2' '
+ cat >expect <<-\EOF &&
+ Murmur3 Hash with seed=0:0x2e4ff723
+ EOF
+ test-tool bloom get_murmur3 "The quick brown fox jumps over the lazy dog" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'compute bloom key for empty string' '
+ cat >expect <<-\EOF &&
+ Hashes:0x5615800c|0x5b966560|0x61174ab4|0x66983008|0x6c19155c|0x7199fab0|0x771ae004|
+ Filter_Length:2
+ Filter_Data:11|11|
+ EOF
+ test-tool bloom generate_filter "" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'compute bloom key for whitespace' '
+ cat >expect <<-\EOF &&
+ Hashes:0xf178874c|0x5f3d6eb6|0xcd025620|0x3ac73d8a|0xa88c24f4|0x16510c5e|0x8415f3c8|
+ Filter_Length:2
+ Filter_Data:51|55|
+ EOF
+ test-tool bloom generate_filter " " >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'compute bloom key for test string 1' '
+ cat >expect <<-\EOF &&
+ Hashes:0xb270de9b|0x1bb6f26e|0x84fd0641|0xee431a14|0x57892de7|0xc0cf41ba|0x2a15558d|
+ Filter_Length:2
+ Filter_Data:92|6c|
+ EOF
+ test-tool bloom generate_filter "Hello world!" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'compute bloom key for test string 2' '
+ cat >expect <<-\EOF &&
+ Hashes:0x20ab385b|0xf5237fe2|0xc99bc769|0x9e140ef0|0x728c5677|0x47049dfe|0x1b7ce585|
+ Filter_Length:2
+ Filter_Data:a5|4a|
+ EOF
+ test-tool bloom generate_filter "file.txt" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success !SANITIZE_LEAK 'get bloom filters for commit with no changes' '
+ git init &&
+ git commit --allow-empty -m "c0" &&
+ cat >expect <<-\EOF &&
+ Filter_Length:1
+ Filter_Data:00|
+ EOF
+ test-tool bloom get_filter_for_commit "$(git rev-parse HEAD)" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'get bloom filter for commit with 10 changes' '
+ rm actual &&
+ rm expect &&
+ mkdir smallDir &&
+ for i in $(test_seq 0 9)
+ do
+ echo $i >smallDir/$i || return 1
+ done &&
+ git add smallDir &&
+ git commit -m "commit with 10 changes" &&
+ cat >expect <<-\EOF &&
+ Filter_Length:14
+ Filter_Data:02|b3|c4|a0|34|e7|fe|eb|cb|47|fe|a0|e8|72|
+ EOF
+ test-tool bloom get_filter_for_commit "$(git rev-parse HEAD)" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success EXPENSIVE 'get bloom filter for commit with 513 changes' '
+ rm actual &&
+ rm expect &&
+ mkdir bigDir &&
+ for i in $(test_seq 0 511)
+ do
+ echo $i >bigDir/$i || return 1
+ done &&
+ git add bigDir &&
+ git commit -m "commit with 513 changes" &&
+ cat >expect <<-\EOF &&
+ Filter_Length:1
+ Filter_Data:ff|
+ EOF
+ test-tool bloom get_filter_for_commit "$(git rev-parse HEAD)" >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t0100-previous.sh b/t/t0100-previous.sh
new file mode 100755
index 0000000..70a3223
--- /dev/null
+++ b/t/t0100-previous.sh
@@ -0,0 +1,76 @@
+#!/bin/sh
+
+test_description='previous branch syntax @{-n}'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'branch -d @{-1}' '
+ test_commit A &&
+ git checkout -b junk &&
+ git checkout - &&
+ echo refs/heads/main >expect &&
+ git symbolic-ref HEAD >actual &&
+ test_cmp expect actual &&
+ git branch -d @{-1} &&
+ test_must_fail git rev-parse --verify refs/heads/junk
+'
+
+test_expect_success 'branch -d @{-12} when there is not enough switches yet' '
+ git reflog expire --expire=now &&
+ git checkout -b junk2 &&
+ git checkout - &&
+ echo refs/heads/main >expect &&
+ git symbolic-ref HEAD >actual &&
+ test_cmp expect actual &&
+ test_must_fail git branch -d @{-12} &&
+ git rev-parse --verify refs/heads/main
+'
+
+test_expect_success 'merge @{-1}' '
+ git checkout A &&
+ test_commit B &&
+ git checkout A &&
+ test_commit C &&
+ test_commit D &&
+ git branch -f main B &&
+ git branch -f other &&
+ git checkout other &&
+ git checkout main &&
+ git merge @{-1} &&
+ git cat-file commit HEAD | grep "Merge branch '\''other'\''"
+'
+
+test_expect_success 'merge @{-1}~1' '
+ git checkout main &&
+ git reset --hard B &&
+ git checkout other &&
+ git checkout main &&
+ git merge @{-1}~1 &&
+ git cat-file commit HEAD >actual &&
+ grep "Merge branch '\''other'\''" actual
+'
+
+test_expect_success 'merge @{-100} before checking out that many branches yet' '
+ git reflog expire --expire=now &&
+ git checkout -f main &&
+ git reset --hard B &&
+ git branch -f other C &&
+ git checkout other &&
+ git checkout main &&
+ test_must_fail git merge @{-100}
+'
+
+test_expect_success 'log -g @{-1}' '
+ git checkout -b last_branch &&
+ git checkout -b new_branch &&
+ echo "last_branch@{0}" >expect &&
+ git log -g --format=%gd @{-1} >actual &&
+ test_cmp expect actual
+'
+
+test_done
+
diff --git a/t/t0101-at-syntax.sh b/t/t0101-at-syntax.sh
new file mode 100755
index 0000000..878aadd
--- /dev/null
+++ b/t/t0101-at-syntax.sh
@@ -0,0 +1,47 @@
+#!/bin/sh
+
+test_description='various @{whatever} syntax tests'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ test_commit one &&
+ test_commit two
+'
+
+check_at() {
+ echo "$2" >expect &&
+ git log -1 --format=%s "$1" >actual &&
+ test_cmp expect actual
+}
+
+test_expect_success '@{0} shows current' '
+ check_at @{0} two
+'
+
+test_expect_success '@{1} shows old' '
+ check_at @{1} one
+'
+
+test_expect_success '@{now} shows current' '
+ check_at @{now} two
+'
+
+test_expect_success '@{2001-09-17} (before the first commit) shows old' '
+ check_at @{2001-09-17} one
+'
+
+test_expect_success 'silly approxidates work' '
+ check_at @{3.hot.dogs.on.2001-09-17} one
+'
+
+test_expect_success 'notice misspelled upstream' '
+ test_must_fail git log -1 --format=%s @{usptream}
+'
+
+test_expect_success 'complain about total nonsense' '
+ test_must_fail git log -1 --format=%s @{utter.bogosity}
+'
+
+test_done
diff --git a/t/t0110-urlmatch-normalization.sh b/t/t0110-urlmatch-normalization.sh
new file mode 100755
index 0000000..12d817f
--- /dev/null
+++ b/t/t0110-urlmatch-normalization.sh
@@ -0,0 +1,182 @@
+#!/bin/sh
+
+test_description='urlmatch URL normalization'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# The base name of the test url files
+tu="$TEST_DIRECTORY/t0110/url"
+
+# Note that only file: URLs should be allowed without a host
+
+test_expect_success 'url scheme' '
+ ! test-tool urlmatch-normalization "" &&
+ ! test-tool urlmatch-normalization "_" &&
+ ! test-tool urlmatch-normalization "scheme" &&
+ ! test-tool urlmatch-normalization "scheme:" &&
+ ! test-tool urlmatch-normalization "scheme:/" &&
+ ! test-tool urlmatch-normalization "scheme://" &&
+ ! test-tool urlmatch-normalization "file" &&
+ ! test-tool urlmatch-normalization "file:" &&
+ ! test-tool urlmatch-normalization "file:/" &&
+ test-tool urlmatch-normalization "file://" &&
+ ! test-tool urlmatch-normalization "://acme.co" &&
+ ! test-tool urlmatch-normalization "x_test://acme.co" &&
+ ! test-tool urlmatch-normalization "-test://acme.co" &&
+ ! test-tool urlmatch-normalization "0test://acme.co" &&
+ ! test-tool urlmatch-normalization "+test://acme.co" &&
+ ! test-tool urlmatch-normalization ".test://acme.co" &&
+ ! test-tool urlmatch-normalization "schem%6e://" &&
+ test-tool urlmatch-normalization "x-Test+v1.0://acme.co" &&
+ test "$(test-tool urlmatch-normalization -p "AbCdeF://x.Y")" = "abcdef://x.y/"
+'
+
+test_expect_success 'url authority' '
+ ! test-tool urlmatch-normalization "scheme://user:pass@" &&
+ ! test-tool urlmatch-normalization "scheme://?" &&
+ ! test-tool urlmatch-normalization "scheme://#" &&
+ ! test-tool urlmatch-normalization "scheme:///" &&
+ ! test-tool urlmatch-normalization "scheme://:" &&
+ ! test-tool urlmatch-normalization "scheme://:555" &&
+ test-tool urlmatch-normalization "file://user:pass@" &&
+ test-tool urlmatch-normalization "file://?" &&
+ test-tool urlmatch-normalization "file://#" &&
+ test-tool urlmatch-normalization "file:///" &&
+ test-tool urlmatch-normalization "file://:" &&
+ ! test-tool urlmatch-normalization "file://:555" &&
+ test-tool urlmatch-normalization "scheme://user:pass@host" &&
+ test-tool urlmatch-normalization "scheme://@host" &&
+ test-tool urlmatch-normalization "scheme://%00@host" &&
+ ! test-tool urlmatch-normalization "scheme://%%@host" &&
+ test-tool urlmatch-normalization "scheme://host_" &&
+ test-tool urlmatch-normalization "scheme://user:pass@host/" &&
+ test-tool urlmatch-normalization "scheme://@host/" &&
+ test-tool urlmatch-normalization "scheme://host/" &&
+ test-tool urlmatch-normalization "scheme://host?x" &&
+ test-tool urlmatch-normalization "scheme://host#x" &&
+ test-tool urlmatch-normalization "scheme://host/@" &&
+ test-tool urlmatch-normalization "scheme://host?@x" &&
+ test-tool urlmatch-normalization "scheme://host#@x" &&
+ test-tool urlmatch-normalization "scheme://[::1]" &&
+ test-tool urlmatch-normalization "scheme://[::1]/" &&
+ ! test-tool urlmatch-normalization "scheme://hos%41/" &&
+ test-tool urlmatch-normalization "scheme://[invalid....:/" &&
+ test-tool urlmatch-normalization "scheme://invalid....:]/" &&
+ ! test-tool urlmatch-normalization "scheme://invalid....:[/" &&
+ ! test-tool urlmatch-normalization "scheme://invalid....:["
+'
+
+test_expect_success 'url port checks' '
+ test-tool urlmatch-normalization "xyz://q@some.host:" &&
+ test-tool urlmatch-normalization "xyz://q@some.host:456/" &&
+ ! test-tool urlmatch-normalization "xyz://q@some.host:0" &&
+ ! test-tool urlmatch-normalization "xyz://q@some.host:0000000" &&
+ test-tool urlmatch-normalization "xyz://q@some.host:0000001?" &&
+ test-tool urlmatch-normalization "xyz://q@some.host:065535#" &&
+ test-tool urlmatch-normalization "xyz://q@some.host:65535" &&
+ ! test-tool urlmatch-normalization "xyz://q@some.host:65536" &&
+ ! test-tool urlmatch-normalization "xyz://q@some.host:99999" &&
+ ! test-tool urlmatch-normalization "xyz://q@some.host:100000" &&
+ ! test-tool urlmatch-normalization "xyz://q@some.host:100001" &&
+ test-tool urlmatch-normalization "http://q@some.host:80" &&
+ test-tool urlmatch-normalization "https://q@some.host:443" &&
+ test-tool urlmatch-normalization "http://q@some.host:80/" &&
+ test-tool urlmatch-normalization "https://q@some.host:443?" &&
+ ! test-tool urlmatch-normalization "http://q@:8008" &&
+ ! test-tool urlmatch-normalization "http://:8080" &&
+ ! test-tool urlmatch-normalization "http://:" &&
+ test-tool urlmatch-normalization "xyz://q@some.host:456/" &&
+ test-tool urlmatch-normalization "xyz://[::1]:456/" &&
+ test-tool urlmatch-normalization "xyz://[::1]:/" &&
+ ! test-tool urlmatch-normalization "xyz://[::1]:000/" &&
+ ! test-tool urlmatch-normalization "xyz://[::1]:0%300/" &&
+ ! test-tool urlmatch-normalization "xyz://[::1]:0x80/" &&
+ ! test-tool urlmatch-normalization "xyz://[::1]:4294967297/" &&
+ ! test-tool urlmatch-normalization "xyz://[::1]:030f/"
+'
+
+test_expect_success 'url port normalization' '
+ test "$(test-tool urlmatch-normalization -p "http://x:800")" = "http://x:800/" &&
+ test "$(test-tool urlmatch-normalization -p "http://x:0800")" = "http://x:800/" &&
+ test "$(test-tool urlmatch-normalization -p "http://x:00000800")" = "http://x:800/" &&
+ test "$(test-tool urlmatch-normalization -p "http://x:065535")" = "http://x:65535/" &&
+ test "$(test-tool urlmatch-normalization -p "http://x:1")" = "http://x:1/" &&
+ test "$(test-tool urlmatch-normalization -p "http://x:80")" = "http://x/" &&
+ test "$(test-tool urlmatch-normalization -p "http://x:080")" = "http://x/" &&
+ test "$(test-tool urlmatch-normalization -p "http://x:000000080")" = "http://x/" &&
+ test "$(test-tool urlmatch-normalization -p "https://x:443")" = "https://x/" &&
+ test "$(test-tool urlmatch-normalization -p "https://x:0443")" = "https://x/" &&
+ test "$(test-tool urlmatch-normalization -p "https://x:000000443")" = "https://x/"
+'
+
+test_expect_success 'url general escapes' '
+ ! test-tool urlmatch-normalization "http://x.y?%fg" &&
+ test "$(test-tool urlmatch-normalization -p "X://W/%7e%41^%3a")" = "x://w/~A%5E%3A" &&
+ test "$(test-tool urlmatch-normalization -p "X://W/:/?#[]@")" = "x://w/:/?#[]@" &&
+ test "$(test-tool urlmatch-normalization -p "X://W/$&()*+,;=")" = "x://w/$&()*+,;=" &&
+ test "$(test-tool urlmatch-normalization -p "X://W/'\''")" = "x://w/'\''" &&
+ test "$(test-tool urlmatch-normalization -p "X://W?'\!'")" = "x://w/?'\!'"
+'
+
+test_expect_success !MINGW 'url high-bit escapes' '
+ test "$(test-tool urlmatch-normalization -p "$(cat "$tu-1")")" = "x://q/%01%02%03%04%05%06%07%08%0E%0F%10%11%12" &&
+ test "$(test-tool urlmatch-normalization -p "$(cat "$tu-2")")" = "x://q/%13%14%15%16%17%18%19%1B%1C%1D%1E%1F%7F" &&
+ test "$(test-tool urlmatch-normalization -p "$(cat "$tu-3")")" = "x://q/%80%81%82%83%84%85%86%87%88%89%8A%8B%8C%8D%8E%8F" &&
+ test "$(test-tool urlmatch-normalization -p "$(cat "$tu-4")")" = "x://q/%90%91%92%93%94%95%96%97%98%99%9A%9B%9C%9D%9E%9F" &&
+ test "$(test-tool urlmatch-normalization -p "$(cat "$tu-5")")" = "x://q/%A0%A1%A2%A3%A4%A5%A6%A7%A8%A9%AA%AB%AC%AD%AE%AF" &&
+ test "$(test-tool urlmatch-normalization -p "$(cat "$tu-6")")" = "x://q/%B0%B1%B2%B3%B4%B5%B6%B7%B8%B9%BA%BB%BC%BD%BE%BF" &&
+ test "$(test-tool urlmatch-normalization -p "$(cat "$tu-7")")" = "x://q/%C0%C1%C2%C3%C4%C5%C6%C7%C8%C9%CA%CB%CC%CD%CE%CF" &&
+ test "$(test-tool urlmatch-normalization -p "$(cat "$tu-8")")" = "x://q/%D0%D1%D2%D3%D4%D5%D6%D7%D8%D9%DA%DB%DC%DD%DE%DF" &&
+ test "$(test-tool urlmatch-normalization -p "$(cat "$tu-9")")" = "x://q/%E0%E1%E2%E3%E4%E5%E6%E7%E8%E9%EA%EB%EC%ED%EE%EF" &&
+ test "$(test-tool urlmatch-normalization -p "$(cat "$tu-10")")" = "x://q/%F0%F1%F2%F3%F4%F5%F6%F7%F8%F9%FA%FB%FC%FD%FE%FF"
+'
+
+test_expect_success 'url utf-8 escapes' '
+ test "$(test-tool urlmatch-normalization -p "$(cat "$tu-11")")" = "x://q/%C2%80%DF%BF%E0%A0%80%EF%BF%BD%F0%90%80%80%F0%AF%BF%BD"
+'
+
+test_expect_success 'url username/password escapes' '
+ test "$(test-tool urlmatch-normalization -p "x://%41%62(^):%70+d@foo")" = "x://Ab(%5E):p+d@foo/"
+'
+
+test_expect_success 'url normalized lengths' '
+ test "$(test-tool urlmatch-normalization -l "Http://%4d%65:%4d^%70@The.Host")" = 25 &&
+ test "$(test-tool urlmatch-normalization -l "http://%41:%42@x.y/%61/")" = 17 &&
+ test "$(test-tool urlmatch-normalization -l "http://@x.y/^")" = 15
+'
+
+test_expect_success 'url . and .. segments' '
+ test "$(test-tool urlmatch-normalization -p "x://y/.")" = "x://y/" &&
+ test "$(test-tool urlmatch-normalization -p "x://y/./")" = "x://y/" &&
+ test "$(test-tool urlmatch-normalization -p "x://y/a/.")" = "x://y/a" &&
+ test "$(test-tool urlmatch-normalization -p "x://y/a/./")" = "x://y/a/" &&
+ test "$(test-tool urlmatch-normalization -p "x://y/.?")" = "x://y/?" &&
+ test "$(test-tool urlmatch-normalization -p "x://y/./?")" = "x://y/?" &&
+ test "$(test-tool urlmatch-normalization -p "x://y/a/.?")" = "x://y/a?" &&
+ test "$(test-tool urlmatch-normalization -p "x://y/a/./?")" = "x://y/a/?" &&
+ test "$(test-tool urlmatch-normalization -p "x://y/a/./b/.././../c")" = "x://y/c" &&
+ test "$(test-tool urlmatch-normalization -p "x://y/a/./b/../.././c/")" = "x://y/c/" &&
+ test "$(test-tool urlmatch-normalization -p "x://y/a/./b/.././../c/././.././.")" = "x://y/" &&
+ ! test-tool urlmatch-normalization "x://y/a/./b/.././../c/././.././.." &&
+ test "$(test-tool urlmatch-normalization -p "x://y/a/./?/././..")" = "x://y/a/?/././.." &&
+ test "$(test-tool urlmatch-normalization -p "x://y/%2e/")" = "x://y/" &&
+ test "$(test-tool urlmatch-normalization -p "x://y/%2E/")" = "x://y/" &&
+ test "$(test-tool urlmatch-normalization -p "x://y/a/%2e./")" = "x://y/" &&
+ test "$(test-tool urlmatch-normalization -p "x://y/b/.%2E/")" = "x://y/" &&
+ test "$(test-tool urlmatch-normalization -p "x://y/c/%2e%2E/")" = "x://y/"
+'
+
+# http://@foo specifies an empty user name but does not specify a password
+# http://foo specifies neither a user name nor a password
+# So they should not be equivalent
+test_expect_success 'url equivalents' '
+ test-tool urlmatch-normalization "httP://x" "Http://X/" &&
+ test-tool urlmatch-normalization "Http://%4d%65:%4d^%70@The.Host" "hTTP://Me:%4D^p@the.HOST:80/" &&
+ ! test-tool urlmatch-normalization "https://@x.y/^" "httpS://x.y:443/^" &&
+ test-tool urlmatch-normalization "https://@x.y/^" "httpS://@x.y:0443/^" &&
+ test-tool urlmatch-normalization "https://@x.y/^/../abc" "httpS://@x.y:0443/abc" &&
+ test-tool urlmatch-normalization "https://@x.y/^/.." "httpS://@x.y:0443/"
+'
+
+test_done
diff --git a/t/t0110/README b/t/t0110/README
new file mode 100644
index 0000000..ad4a50e
--- /dev/null
+++ b/t/t0110/README
@@ -0,0 +1,9 @@
+The url data files in this directory contain URLs with characters
+in the range 0x01-0x1f and 0x7f-0xff to test the proper normalization
+of unprintable characters.
+
+A select few characters in the 0x01-0x1f range are skipped to help
+avoid problems running the test itself.
+
+The urls are in test files in this directory rather than being
+embedded in the test script for portability.
diff --git a/t/t0110/url-1 b/t/t0110/url-1
new file mode 100644
index 0000000..519019c
--- /dev/null
+++ b/t/t0110/url-1
@@ -0,0 +1 @@
+x://q/
diff --git a/t/t0110/url-10 b/t/t0110/url-10
new file mode 100644
index 0000000..b9965de
--- /dev/null
+++ b/t/t0110/url-10
@@ -0,0 +1 @@
+x://q/ðñòóôõö÷øùúûüýþÿ
diff --git a/t/t0110/url-11 b/t/t0110/url-11
new file mode 100644
index 0000000..f0a50f1
--- /dev/null
+++ b/t/t0110/url-11
@@ -0,0 +1 @@
+x://q/€߿ࠀ�ð€€ð¯¿½
diff --git a/t/t0110/url-2 b/t/t0110/url-2
new file mode 100644
index 0000000..43334b0
--- /dev/null
+++ b/t/t0110/url-2
@@ -0,0 +1 @@
+x://q/
diff --git a/t/t0110/url-3 b/t/t0110/url-3
new file mode 100644
index 0000000..7378c7b
--- /dev/null
+++ b/t/t0110/url-3
@@ -0,0 +1 @@
+x://q/€‚ƒ„…†‡ˆ‰Š‹ŒŽ
diff --git a/t/t0110/url-4 b/t/t0110/url-4
new file mode 100644
index 0000000..220b198
--- /dev/null
+++ b/t/t0110/url-4
@@ -0,0 +1 @@
+x://q/‘’“”•–—˜™š›œžŸ
diff --git a/t/t0110/url-5 b/t/t0110/url-5
new file mode 100644
index 0000000..1ccd927
--- /dev/null
+++ b/t/t0110/url-5
@@ -0,0 +1 @@
+x://q/ ¡¢£¤¥¦§¨©ª«¬­®¯
diff --git a/t/t0110/url-6 b/t/t0110/url-6
new file mode 100644
index 0000000..e8283aa
--- /dev/null
+++ b/t/t0110/url-6
@@ -0,0 +1 @@
+x://q/°±²³´µ¶·¸¹º»¼½¾¿
diff --git a/t/t0110/url-7 b/t/t0110/url-7
new file mode 100644
index 0000000..fa7c10b
--- /dev/null
+++ b/t/t0110/url-7
@@ -0,0 +1 @@
+x://q/ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏ
diff --git a/t/t0110/url-8 b/t/t0110/url-8
new file mode 100644
index 0000000..79a0ba8
--- /dev/null
+++ b/t/t0110/url-8
@@ -0,0 +1 @@
+x://q/ÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞß
diff --git a/t/t0110/url-9 b/t/t0110/url-9
new file mode 100644
index 0000000..8b44bec
--- /dev/null
+++ b/t/t0110/url-9
@@ -0,0 +1 @@
+x://q/àáâãäåæçèéêëìíîï
diff --git a/t/t0200-gettext-basic.sh b/t/t0200-gettext-basic.sh
new file mode 100755
index 0000000..522fb2a
--- /dev/null
+++ b/t/t0200-gettext-basic.sh
@@ -0,0 +1,109 @@
+#!/bin/sh
+#
+# Copyright (c) 2010 Ævar Arnfjörð Bjarmason
+#
+
+test_description='Gettext support for Git'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./lib-gettext.sh
+
+test_expect_success "sanity: \$GIT_INTERNAL_GETTEXT_SH_SCHEME is set (to $GIT_INTERNAL_GETTEXT_SH_SCHEME)" '
+ test -n "$GIT_INTERNAL_GETTEXT_SH_SCHEME"
+'
+
+test_expect_success 'sanity: $TEXTDOMAIN is git' '
+ test $TEXTDOMAIN = "git"
+'
+
+test_expect_success 'xgettext sanity: Perl _() strings are not extracted' '
+ ! grep "A Perl string xgettext will not get" "$GIT_PO_PATH"/is.po
+'
+
+test_expect_success 'xgettext sanity: Comment extraction with --add-comments' '
+ grep "TRANSLATORS: This is a test" "$TEST_DIRECTORY"/t0200/* | wc -l >expect &&
+ grep "TRANSLATORS: This is a test" "$GIT_PO_PATH"/is.po | wc -l >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'xgettext sanity: Comment extraction with --add-comments stops at statements' '
+ ! grep "This is a phony" "$GIT_PO_PATH"/is.po &&
+ ! grep "the above comment" "$GIT_PO_PATH"/is.po
+'
+
+test_expect_success GETTEXT 'sanity: $TEXTDOMAINDIR exists without NO_GETTEXT=YesPlease' '
+ test -d "$TEXTDOMAINDIR" &&
+ test "$TEXTDOMAINDIR" = "$GIT_TEXTDOMAINDIR"
+'
+
+test_expect_success GETTEXT 'sanity: Icelandic locale was compiled' '
+ test -f "$TEXTDOMAINDIR/is/LC_MESSAGES/git.mo"
+'
+
+# TODO: When we have more locales, generalize this to test them
+# all. Maybe we'll need a dir->locale map for that.
+test_expect_success GETTEXT_LOCALE 'sanity: gettext("") metadata is OK' '
+ # Return value may be non-zero
+ LANGUAGE=is LC_ALL="$is_IS_locale" gettext "" >zero-expect &&
+ grep "Project-Id-Version: Git" zero-expect &&
+ grep "Git Mailing List <git@vger.kernel.org>" zero-expect &&
+ grep "Content-Type: text/plain; charset=UTF-8" zero-expect &&
+ grep "Content-Transfer-Encoding: 8bit" zero-expect
+'
+
+test_expect_success GETTEXT_LOCALE 'sanity: gettext(unknown) is passed through' '
+ printf "This is not a translation string" >expect &&
+ gettext "This is not a translation string" >actual &&
+ eval_gettext "This is not a translation string" >actual &&
+ test_cmp expect actual
+'
+
+# xgettext from C
+test_expect_success GETTEXT_LOCALE 'xgettext: C extraction of _() and N_() strings' '
+ printf "TILRAUN: C tilraunastrengur" >expect &&
+ printf "\n" >>expect &&
+ printf "Sjá '\''git help SKIPUN'\'' til að sjá hjálp fyrir tiltekna skipun." >>expect &&
+ LANGUAGE=is LC_ALL="$is_IS_locale" gettext "TEST: A C test string" >actual &&
+ printf "\n" >>actual &&
+ LANGUAGE=is LC_ALL="$is_IS_locale" gettext "See '\''git help COMMAND'\'' for more information on a specific command." >>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success GETTEXT_LOCALE 'xgettext: C extraction with %s' '
+ printf "TILRAUN: C tilraunastrengur %%s" >expect &&
+ LANGUAGE=is LC_ALL="$is_IS_locale" gettext "TEST: A C test string %s" >actual &&
+ test_cmp expect actual
+'
+
+# xgettext from Shell
+test_expect_success GETTEXT_LOCALE 'xgettext: Shell extraction' '
+ printf "TILRAUN: Skeljartilraunastrengur" >expect &&
+ LANGUAGE=is LC_ALL="$is_IS_locale" gettext "TEST: A Shell test string" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success GETTEXT_LOCALE 'xgettext: Shell extraction with $variable' '
+ printf "TILRAUN: Skeljartilraunastrengur með breytunni a var i able" >x-expect &&
+ LANGUAGE=is LC_ALL="$is_IS_locale" variable="a var i able" eval_gettext "TEST: A Shell test \$variable" >x-actual &&
+ test_cmp x-expect x-actual
+'
+
+# xgettext from Perl
+test_expect_success GETTEXT_LOCALE 'xgettext: Perl extraction' '
+ printf "TILRAUN: Perl tilraunastrengur" >expect &&
+ LANGUAGE=is LC_ALL="$is_IS_locale" gettext "TEST: A Perl test string" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success GETTEXT_LOCALE 'xgettext: Perl extraction with %s' '
+ printf "TILRAUN: Perl tilraunastrengur með breytunni %%s" >expect &&
+ LANGUAGE=is LC_ALL="$is_IS_locale" gettext "TEST: A Perl test variable %s" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success GETTEXT_LOCALE 'sanity: Some gettext("") data for real locale' '
+ LANGUAGE=is LC_ALL="$is_IS_locale" gettext "" >real-locale &&
+ test -s real-locale
+'
+
+test_done
diff --git a/t/t0200/test.c b/t/t0200/test.c
new file mode 100644
index 0000000..584d45c
--- /dev/null
+++ b/t/t0200/test.c
@@ -0,0 +1,23 @@
+/* This is a phony C program that's only here to test xgettext message extraction */
+
+const char help[] =
+ /* TRANSLATORS: This is a test. You don't need to translate it. */
+ N_("See 'git help COMMAND' for more information on a specific command.");
+
+int main(void)
+{
+ /* TRANSLATORS: This is a test. You don't need to translate it. */
+ puts(_("TEST: A C test string"));
+
+ /* TRANSLATORS: This is a test. You don't need to translate it. */
+ printf(_("TEST: A C test string %s"), "variable");
+
+ /* TRANSLATORS: This is a test. You don't need to translate it. */
+ printf(_("TEST: Hello World!"));
+
+ /* TRANSLATORS: This is a test. You don't need to translate it. */
+ printf(_("TEST: Old English Runes"));
+
+ /* TRANSLATORS: This is a test. You don't need to translate it. */
+ printf(_("TEST: ‘single’ and “double†quotes"));
+}
diff --git a/t/t0200/test.perl b/t/t0200/test.perl
new file mode 100644
index 0000000..36fba34
--- /dev/null
+++ b/t/t0200/test.perl
@@ -0,0 +1,14 @@
+# This is a phony Perl program that's only here to test xgettext
+# message extraction
+
+# so the above comment won't be folded into the next one by xgettext
+1;
+
+# TRANSLATORS: This is a test. You don't need to translate it.
+print __("TEST: A Perl test string");
+
+# TRANSLATORS: This is a test. You don't need to translate it.
+printf __("TEST: A Perl test variable %s"), "moo";
+
+# TRANSLATORS: If you see this, Git has a bug
+print _"TEST: A Perl string xgettext will not get";
diff --git a/t/t0200/test.sh b/t/t0200/test.sh
new file mode 100644
index 0000000..022d607
--- /dev/null
+++ b/t/t0200/test.sh
@@ -0,0 +1,14 @@
+# This is a phony Shell program that's only here to test xgettext
+# message extraction
+
+# so the above comment won't be folded into the next one by xgettext
+echo
+
+# TRANSLATORS: This is a test. You don't need to translate it.
+gettext "TEST: A Shell test string"
+
+# TRANSLATORS: This is a test. You don't need to translate it.
+eval_gettext "TEST: A Shell test \$variable"
+
+# TRANSLATORS: If you see this, Git has a bug
+_("TEST: A Shell string xgettext won't get")
diff --git a/t/t0201-gettext-fallbacks.sh b/t/t0201-gettext-fallbacks.sh
new file mode 100755
index 0000000..8724ce1
--- /dev/null
+++ b/t/t0201-gettext-fallbacks.sh
@@ -0,0 +1,68 @@
+#!/bin/sh
+#
+# Copyright (c) 2010 Ævar Arnfjörð Bjarmason
+#
+
+test_description='Gettext Shell fallbacks'
+
+GIT_INTERNAL_GETTEXT_TEST_FALLBACKS=YesPlease
+export GIT_INTERNAL_GETTEXT_TEST_FALLBACKS
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./lib-gettext.sh
+
+test_expect_success "sanity: \$GIT_INTERNAL_GETTEXT_SH_SCHEME is set (to $GIT_INTERNAL_GETTEXT_SH_SCHEME)" '
+ test -n "$GIT_INTERNAL_GETTEXT_SH_SCHEME"
+'
+
+test_expect_success 'sanity: $GIT_INTERNAL_GETTEXT_TEST_FALLBACKS is set' '
+ test -n "$GIT_INTERNAL_GETTEXT_TEST_FALLBACKS"
+'
+
+test_expect_success 'sanity: $GIT_INTERNAL_GETTEXT_SH_SCHEME" is fallthrough' '
+ echo fallthrough >expect &&
+ echo $GIT_INTERNAL_GETTEXT_SH_SCHEME >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'gettext: our gettext() fallback has pass-through semantics' '
+ printf "test" >expect &&
+ gettext "test" >actual &&
+ test_cmp expect actual &&
+ printf "test more words" >expect &&
+ gettext "test more words" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'eval_gettext: our eval_gettext() fallback has pass-through semantics' '
+ printf "test" >expect &&
+ eval_gettext "test" >actual &&
+ test_cmp expect actual &&
+ printf "test more words" >expect &&
+ eval_gettext "test more words" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'eval_gettext: our eval_gettext() fallback can interpolate variables' '
+ printf "test YesPlease" >expect &&
+ GIT_INTERNAL_GETTEXT_TEST_FALLBACKS=YesPlease eval_gettext "test \$GIT_INTERNAL_GETTEXT_TEST_FALLBACKS" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'eval_gettext: our eval_gettext() fallback can interpolate variables with spaces' '
+ cmdline="git am" &&
+ export cmdline &&
+ printf "When you have resolved this problem, run git am --resolved." >expect &&
+ eval_gettext "When you have resolved this problem, run \$cmdline --resolved." >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'eval_gettext: our eval_gettext() fallback can interpolate variables with spaces and quotes' '
+ cmdline="git am" &&
+ export cmdline &&
+ printf "When you have resolved this problem, run \"git am --resolved\"." >expect &&
+ eval_gettext "When you have resolved this problem, run \"\$cmdline --resolved\"." >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t0202-gettext-perl.sh b/t/t0202-gettext-perl.sh
new file mode 100755
index 0000000..5a6f280
--- /dev/null
+++ b/t/t0202-gettext-perl.sh
@@ -0,0 +1,18 @@
+#!/bin/sh
+#
+# Copyright (c) 2010 Ævar Arnfjörð Bjarmason
+#
+
+test_description='Perl gettext interface (Git::I18N)'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./lib-gettext.sh
+. "$TEST_DIRECTORY"/lib-perl.sh
+skip_all_if_no_Test_More
+
+test_expect_success 'run t0202/test.pl to test Git::I18N.pm' '
+ "$PERL_PATH" "$TEST_DIRECTORY"/t0202/test.pl 2>stderr &&
+ test_must_be_empty stderr
+'
+
+test_done
diff --git a/t/t0202/test.pl b/t/t0202/test.pl
new file mode 100755
index 0000000..2cbf7b9
--- /dev/null
+++ b/t/t0202/test.pl
@@ -0,0 +1,122 @@
+#!/usr/bin/perl
+use 5.008;
+use lib (split(/:/, $ENV{GITPERLLIB}));
+use strict;
+use warnings;
+use POSIX qw(:locale_h);
+use Test::More tests => 13;
+use Git::I18N;
+
+my $has_gettext_library = $Git::I18N::__HAS_LIBRARY;
+
+ok(1, "Testing Git::I18N with " .
+ ($has_gettext_library
+ ? (defined $Locale::Messages::VERSION
+ ? "Locale::Messages version $Locale::Messages::VERSION"
+ # Versions of Locale::Messages before 1.17 didn't have a
+ # $VERSION variable.
+ : "Locale::Messages version <1.17")
+ : "NO Perl gettext library"));
+ok(1, "Git::I18N is located at $INC{'Git/I18N.pm'}");
+
+{
+ my $exports = @Git::I18N::EXPORT;
+ ok($exports, "sanity: Git::I18N has $exports export(s)");
+}
+is_deeply(\@Git::I18N::EXPORT, \@Git::I18N::EXPORT_OK, "sanity: Git::I18N exports everything by default");
+
+# prototypes
+{
+ # Add prototypes here when modifying the public interface to add
+ # more gettext wrapper functions.
+ my %prototypes = (qw(
+ __ $
+ __n $$$
+ N__ $
+ ));
+ while (my ($sub, $proto) = each %prototypes) {
+ is(prototype(\&{"Git::I18N::$sub"}), $proto, "sanity: $sub has a $proto prototype");
+ }
+}
+
+# Test basic passthrough in the C locale
+{
+ local $ENV{LANGUAGE} = 'C';
+ local $ENV{LC_ALL} = 'C';
+ local $ENV{LANG} = 'C';
+
+ my ($got, $expect) = (('TEST: A Perl test string') x 2);
+
+ is(__($got), $expect, "Passing a string through __() in the C locale works");
+
+ my ($got_singular, $got_plural, $expect_singular, $expect_plural) =
+ (('TEST: 1 file', 'TEST: n files') x 2);
+
+ is(__n($got_singular, $got_plural, 1), $expect_singular,
+ "Get singular string through __n() in C locale");
+ is(__n($got_singular, $got_plural, 2), $expect_plural,
+ "Get plural string through __n() in C locale");
+
+ is(N__($got), $expect, "Passing a string through N__() in the C locale works");
+}
+
+# Test a basic message on different locales
+SKIP: {
+ unless ($ENV{GETTEXT_LOCALE}) {
+ # Can't reliably test __() with a non-C locales because the
+ # required locales may not be installed on the system.
+ #
+ # We test for these anyway as part of the shell
+ # tests. Skipping these here will eliminate failures on odd
+ # platforms with incomplete locale data.
+
+ skip "GETTEXT_LOCALE must be set by lib-gettext.sh for exhaustive Git::I18N tests", 2;
+ }
+
+ # The is_IS UTF-8 locale passed from lib-gettext.sh
+ my $is_IS_locale = $ENV{is_IS_locale};
+
+ my $test = sub {
+ my ($got, $expect, $msg, $locale) = @_;
+ # Maybe this system doesn't have the locale we're trying to
+ # test.
+ my $locale_ok = setlocale(LC_ALL, $locale);
+ is(__($got), $expect, "$msg a gettext library + <$locale> locale <$got> turns into <$expect>");
+ };
+
+ my $env_C = sub {
+ $ENV{LANGUAGE} = 'C';
+ $ENV{LC_ALL} = 'C';
+ };
+
+ my $env_is = sub {
+ $ENV{LANGUAGE} = 'is';
+ $ENV{LC_ALL} = $is_IS_locale;
+ };
+
+ # Translation's the same as the original
+ my ($got, $expect) = (('TEST: A Perl test string') x 2);
+
+ if ($has_gettext_library) {
+ {
+ local %ENV; $env_C->();
+ $test->($got, $expect, "With", 'C');
+ }
+
+ {
+ my ($got, $expect) = ($got, 'TILRAUN: Perl tilraunastrengur');
+ local %ENV; $env_is->();
+ $test->($got, $expect, "With", $is_IS_locale);
+ }
+ } else {
+ {
+ local %ENV; $env_C->();
+ $test->($got, $expect, "Without", 'C');
+ }
+
+ {
+ local %ENV; $env_is->();
+ $test->($got, $expect, "Without", 'is');
+ }
+ }
+}
diff --git a/t/t0203-gettext-setlocale-sanity.sh b/t/t0203-gettext-setlocale-sanity.sh
new file mode 100755
index 0000000..86cff32
--- /dev/null
+++ b/t/t0203-gettext-setlocale-sanity.sh
@@ -0,0 +1,27 @@
+#!/bin/sh
+#
+# Copyright (c) 2010 Ævar Arnfjörð Bjarmason
+#
+
+test_description="The Git C functions aren't broken by setlocale(3)"
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./lib-gettext.sh
+
+test_expect_success 'git show a ISO-8859-1 commit under C locale' '
+ . "$TEST_DIRECTORY"/t3901/8859-1.txt &&
+ test_commit "iso-c-commit" iso-under-c &&
+ git show >out 2>err &&
+ test_must_be_empty err &&
+ grep -q "iso-c-commit" out
+'
+
+test_expect_success GETTEXT_LOCALE 'git show a ISO-8859-1 commit under a UTF-8 locale' '
+ . "$TEST_DIRECTORY"/t3901/8859-1.txt &&
+ test_commit "iso-utf8-commit" iso-under-utf8 &&
+ LANGUAGE=is LC_ALL="$is_IS_locale" git show >out 2>err &&
+ test_must_be_empty err &&
+ grep -q "iso-utf8-commit" out
+'
+
+test_done
diff --git a/t/t0204-gettext-reencode-sanity.sh b/t/t0204-gettext-reencode-sanity.sh
new file mode 100755
index 0000000..4f2e0dc
--- /dev/null
+++ b/t/t0204-gettext-reencode-sanity.sh
@@ -0,0 +1,88 @@
+#!/bin/sh
+#
+# Copyright (c) 2010 Ævar Arnfjörð Bjarmason
+#
+
+test_description="Gettext reencoding of our *.po/*.mo files works"
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./lib-gettext.sh
+
+# The constants used in a tricky observation for undefined behaviour
+RUNES="TILRAUN: ᚻᛖ ᚳᚹᚫᚦ ᚦᚫᛠᚻᛖ ᛒᚢᛞᛖ áš©áš¾ ᚦᚫᛗ ᛚᚪᚾᛞᛖ ᚾᚩᚱᚦᚹᛖᚪᚱᛞᚢᛗ áš¹á›áš¦ ᚦᚪ ᚹᛖᛥᚫ"
+PUNTS="TILRAUN: ?? ???? ??? ?? ???? ?? ??? ????? ??????????? ??? ?? ????"
+MSGKEY="TEST: Old English Runes"
+
+test_expect_success GETTEXT_LOCALE 'gettext: Emitting UTF-8 from our UTF-8 *.mo files / Icelandic' '
+ printf "TILRAUN: Halló Heimur!" >expect &&
+ LANGUAGE=is LC_ALL="$is_IS_locale" gettext "TEST: Hello World!" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success GETTEXT_LOCALE 'gettext: Emitting UTF-8 from our UTF-8 *.mo files / Runes' '
+ printf "%s" "$RUNES" >expect &&
+ LANGUAGE=is LC_ALL="$is_IS_locale" gettext "$MSGKEY" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success GETTEXT_ISO_LOCALE 'gettext: Emitting ISO-8859-1 from our UTF-8 *.mo files / Icelandic' '
+ printf "TILRAUN: Halló Heimur!" | iconv -f UTF-8 -t ISO8859-1 >expect &&
+ LANGUAGE=is LC_ALL="$is_IS_iso_locale" gettext "TEST: Hello World!" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success GETTEXT_ISO_LOCALE 'gettext: impossible ISO-8859-1 output' '
+ LANGUAGE=is LC_ALL="$is_IS_iso_locale" gettext "$MSGKEY" >runes &&
+ case "$(cat runes)" in
+ "$MSGKEY")
+ say "Your system gives back the key to message catalog"
+ ;;
+ "$PUNTS")
+ say "Your system replaces an impossible character with ?"
+ ;;
+ "$RUNES")
+ say "Your system gives back the raw message for an impossible request"
+ ;;
+ *)
+ say "We never saw the error behaviour your system exhibits"
+ false
+ ;;
+ esac
+'
+
+test_expect_success GETTEXT_LOCALE 'gettext: Fetching a UTF-8 msgid -> UTF-8' '
+ printf "TILRAUN: ‚einfaldar‘ og „tvöfaldar“ gæsalappir" >expect &&
+ LANGUAGE=is LC_ALL="$is_IS_locale" gettext "TEST: ‘single’ and “double†quotes" >actual &&
+ test_cmp expect actual
+'
+
+# How these quotes get transliterated depends on the gettext implementation:
+#
+# Debian: ,einfaldar' og ,,tvöfaldar" [GNU libintl]
+# FreeBSD: `einfaldar` og "tvöfaldar" [GNU libintl]
+# Solaris: ?einfaldar? og ?tvöfaldar? [Solaris libintl]
+#
+# Just make sure the contents are transliterated, and don't use grep -q
+# so that these differences are emitted under --verbose for curious
+# eyes.
+test_expect_success GETTEXT_ISO_LOCALE 'gettext: Fetching a UTF-8 msgid -> ISO-8859-1' '
+ LANGUAGE=is LC_ALL="$is_IS_iso_locale" gettext "TEST: ‘single’ and “double†quotes" >actual &&
+ grep "einfaldar" actual &&
+ grep "$(echo tvöfaldar | iconv -f UTF-8 -t ISO8859-1)" actual
+'
+
+test_expect_success GETTEXT_LOCALE 'gettext.c: git init UTF-8 -> UTF-8' '
+ printf "Bjó til tóma Git lind" >expect &&
+ LANGUAGE=is LC_ALL="$is_IS_locale" git init repo >actual &&
+ test_when_finished "rm -rf repo" &&
+ grep "^$(cat expect) " actual
+'
+
+test_expect_success GETTEXT_ISO_LOCALE 'gettext.c: git init UTF-8 -> ISO-8859-1' '
+ printf "Bjó til tóma Git lind" >expect &&
+ LANGUAGE=is LC_ALL="$is_IS_iso_locale" git init repo >actual &&
+ test_when_finished "rm -rf repo" &&
+ grep "^$(cat expect | iconv -f UTF-8 -t ISO8859-1) " actual
+'
+
+test_done
diff --git a/t/t0210-trace2-normal.sh b/t/t0210-trace2-normal.sh
new file mode 100755
index 0000000..80e76a4
--- /dev/null
+++ b/t/t0210-trace2-normal.sh
@@ -0,0 +1,286 @@
+#!/bin/sh
+
+test_description='test trace2 facility (normal target)'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# Turn off any inherited trace2 settings for this test.
+sane_unset GIT_TRACE2 GIT_TRACE2_PERF GIT_TRACE2_EVENT
+sane_unset GIT_TRACE2_BRIEF
+sane_unset GIT_TRACE2_CONFIG_PARAMS
+
+# Add t/helper directory to PATH so that we can use a relative
+# path to run nested instances of test-tool.exe (see 004child).
+# This helps with HEREDOC comparisons later.
+TTDIR="$GIT_BUILD_DIR/t/helper/" && export TTDIR
+PATH="$TTDIR:$PATH" && export PATH
+
+# Warning: use of 'test_cmp' may run test-tool.exe and/or git.exe
+# Warning: to do the actual diff/comparison, so the HEREDOCs here
+# Warning: only cover our actual calls to test-tool and/or git.
+# Warning: So you may see extra lines in artifact files when
+# Warning: interactively debugging.
+
+V=$(git version | sed -e 's/^git version //') && export V
+
+# There are multiple trace2 targets: normal, perf, and event.
+# Trace2 events will/can be written to each active target (subject
+# to whatever filtering that target decides to do).
+# This script tests the normal target in isolation.
+#
+# Defer setting GIT_TRACE2 until the actual command line we want to test
+# because hidden git and test-tool commands run by the test harness
+# can contaminate our output.
+
+# Enable "brief" feature which turns off "<clock> <file>:<line> " prefix.
+GIT_TRACE2_BRIEF=1 && export GIT_TRACE2_BRIEF
+
+# Basic tests of the trace2 normal stream. Since this stream is used
+# primarily with printf-style debugging/tracing, we do limited testing
+# here.
+#
+# We do confirm the following API features:
+# [] the 'version <v>' event
+# [] the 'start <argv>' event
+# [] the 'cmd_name <name>' event
+# [] the 'exit <time> code:<code>' event
+# [] the 'atexit <time> code:<code>' event
+#
+# Fields of the form _FIELD_ are tokens that have been replaced (such
+# as the elapsed time).
+
+# Verb 001return
+#
+# Implicit return from cmd_<verb> function propagates <code>.
+
+test_expect_success 'normal stream, return code 0' '
+ test_when_finished "rm trace.normal actual expect" &&
+ GIT_TRACE2="$(pwd)/trace.normal" test-tool trace2 001return 0 &&
+ perl "$TEST_DIRECTORY/t0210/scrub_normal.perl" <trace.normal >actual &&
+ cat >expect <<-EOF &&
+ version $V
+ start _EXE_ trace2 001return 0
+ cmd_name trace2 (trace2)
+ exit elapsed:_TIME_ code:0
+ atexit elapsed:_TIME_ code:0
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'normal stream, return code 1' '
+ test_when_finished "rm trace.normal actual expect" &&
+ test_must_fail env GIT_TRACE2="$(pwd)/trace.normal" test-tool trace2 001return 1 &&
+ perl "$TEST_DIRECTORY/t0210/scrub_normal.perl" <trace.normal >actual &&
+ cat >expect <<-EOF &&
+ version $V
+ start _EXE_ trace2 001return 1
+ cmd_name trace2 (trace2)
+ exit elapsed:_TIME_ code:1
+ atexit elapsed:_TIME_ code:1
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'automatic filename' '
+ test_when_finished "rm -r traces actual expect" &&
+ mkdir traces &&
+ GIT_TRACE2="$(pwd)/traces" test-tool trace2 001return 0 &&
+ perl "$TEST_DIRECTORY/t0210/scrub_normal.perl" <"$(ls traces/*)" >actual &&
+ cat >expect <<-EOF &&
+ version $V
+ start _EXE_ trace2 001return 0
+ cmd_name trace2 (trace2)
+ exit elapsed:_TIME_ code:0
+ atexit elapsed:_TIME_ code:0
+ EOF
+ test_cmp expect actual
+'
+
+# Verb 002exit
+#
+# Explicit exit(code) from within cmd_<verb> propagates <code>.
+
+test_expect_success 'normal stream, exit code 0' '
+ test_when_finished "rm trace.normal actual expect" &&
+ GIT_TRACE2="$(pwd)/trace.normal" test-tool trace2 002exit 0 &&
+ perl "$TEST_DIRECTORY/t0210/scrub_normal.perl" <trace.normal >actual &&
+ cat >expect <<-EOF &&
+ version $V
+ start _EXE_ trace2 002exit 0
+ cmd_name trace2 (trace2)
+ exit elapsed:_TIME_ code:0
+ atexit elapsed:_TIME_ code:0
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'normal stream, exit code 1' '
+ test_when_finished "rm trace.normal actual expect" &&
+ test_must_fail env GIT_TRACE2="$(pwd)/trace.normal" test-tool trace2 002exit 1 &&
+ perl "$TEST_DIRECTORY/t0210/scrub_normal.perl" <trace.normal >actual &&
+ cat >expect <<-EOF &&
+ version $V
+ start _EXE_ trace2 002exit 1
+ cmd_name trace2 (trace2)
+ exit elapsed:_TIME_ code:1
+ atexit elapsed:_TIME_ code:1
+ EOF
+ test_cmp expect actual
+'
+
+# Verb 003error
+#
+# To the above, add multiple 'error <msg>' events
+
+test_expect_success 'normal stream, error event' '
+ test_when_finished "rm trace.normal actual expect" &&
+ GIT_TRACE2="$(pwd)/trace.normal" test-tool trace2 003error "hello world" "this is a test" &&
+ perl "$TEST_DIRECTORY/t0210/scrub_normal.perl" <trace.normal >actual &&
+ cat >expect <<-EOF &&
+ version $V
+ start _EXE_ trace2 003error '\''hello world'\'' '\''this is a test'\''
+ cmd_name trace2 (trace2)
+ error hello world
+ error this is a test
+ exit elapsed:_TIME_ code:0
+ atexit elapsed:_TIME_ code:0
+ EOF
+ test_cmp expect actual
+'
+
+# Verb 007bug
+#
+# Check that BUG writes to trace2
+
+test_expect_success 'BUG messages are written to trace2' '
+ test_when_finished "rm trace.normal actual expect" &&
+ test_must_fail env GIT_TRACE2="$(pwd)/trace.normal" test-tool trace2 007bug &&
+ perl "$TEST_DIRECTORY/t0210/scrub_normal.perl" <trace.normal >actual &&
+ cat >expect <<-EOF &&
+ version $V
+ start _EXE_ trace2 007bug
+ cmd_name trace2 (trace2)
+ error the bug message
+ exit elapsed:_TIME_ code:99
+ atexit elapsed:_TIME_ code:99
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'bug messages with BUG_if_bug() are written to trace2' '
+ test_when_finished "rm trace.normal actual expect" &&
+ test_expect_code 99 env GIT_TRACE2="$(pwd)/trace.normal" \
+ test-tool trace2 008bug 2>err &&
+ cat >expect <<-\EOF &&
+ a bug message
+ another bug message
+ an explicit BUG_if_bug() following bug() call(s) is nice, but not required
+ EOF
+ sed "s/^.*: //" <err >actual &&
+ test_cmp expect actual &&
+
+ perl "$TEST_DIRECTORY/t0210/scrub_normal.perl" <trace.normal >actual &&
+ cat >expect <<-EOF &&
+ version $V
+ start _EXE_ trace2 008bug
+ cmd_name trace2 (trace2)
+ error a bug message
+ error another bug message
+ error an explicit BUG_if_bug() following bug() call(s) is nice, but not required
+ exit elapsed:_TIME_ code:99
+ atexit elapsed:_TIME_ code:99
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'bug messages without explicit BUG_if_bug() are written to trace2' '
+ test_when_finished "rm trace.normal actual expect" &&
+ test_expect_code 99 env GIT_TRACE2="$(pwd)/trace.normal" \
+ test-tool trace2 009bug_BUG 2>err &&
+ cat >expect <<-\EOF &&
+ a bug message
+ another bug message
+ had bug() call(s) in this process without explicit BUG_if_bug()
+ EOF
+ sed "s/^.*: //" <err >actual &&
+ test_cmp expect actual &&
+
+ perl "$TEST_DIRECTORY/t0210/scrub_normal.perl" <trace.normal >actual &&
+ cat >expect <<-EOF &&
+ version $V
+ start _EXE_ trace2 009bug_BUG
+ cmd_name trace2 (trace2)
+ error a bug message
+ error another bug message
+ error on exit(): had bug() call(s) in this process without explicit BUG_if_bug()
+ exit elapsed:_TIME_ code:99
+ atexit elapsed:_TIME_ code:99
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'bug messages followed by BUG() are written to trace2' '
+ test_when_finished "rm trace.normal actual expect" &&
+ test_expect_code 99 env GIT_TRACE2="$(pwd)/trace.normal" \
+ test-tool trace2 010bug_BUG 2>err &&
+ cat >expect <<-\EOF &&
+ a bug message
+ a BUG message
+ EOF
+ sed "s/^.*: //" <err >actual &&
+ test_cmp expect actual &&
+
+ perl "$TEST_DIRECTORY/t0210/scrub_normal.perl" <trace.normal >actual &&
+ cat >expect <<-EOF &&
+ version $V
+ start _EXE_ trace2 010bug_BUG
+ cmd_name trace2 (trace2)
+ error a bug message
+ error a BUG message
+ exit elapsed:_TIME_ code:99
+ atexit elapsed:_TIME_ code:99
+ EOF
+ test_cmp expect actual
+'
+
+sane_unset GIT_TRACE2_BRIEF
+
+# Now test without environment variables and get all Trace2 settings
+# from the global config.
+
+test_expect_success 'using global config, normal stream, return code 0' '
+ test_when_finished "rm trace.normal actual expect" &&
+ test_config_global trace2.normalBrief 1 &&
+ test_config_global trace2.normalTarget "$(pwd)/trace.normal" &&
+ test-tool trace2 001return 0 &&
+ perl "$TEST_DIRECTORY/t0210/scrub_normal.perl" <trace.normal >actual &&
+ cat >expect <<-EOF &&
+ version $V
+ start _EXE_ trace2 001return 0
+ cmd_name trace2 (trace2)
+ exit elapsed:_TIME_ code:0
+ atexit elapsed:_TIME_ code:0
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'using global config with include' '
+ test_when_finished "rm trace.normal actual expect real.gitconfig" &&
+ test_config_global trace2.normalBrief 1 &&
+ test_config_global trace2.normalTarget "$(pwd)/trace.normal" &&
+ mv "$(pwd)/.gitconfig" "$(pwd)/real.gitconfig" &&
+ test_config_global include.path "$(pwd)/real.gitconfig" &&
+ test-tool trace2 001return 0 &&
+ perl "$TEST_DIRECTORY/t0210/scrub_normal.perl" <trace.normal >actual &&
+ cat >expect <<-EOF &&
+ version $V
+ start _EXE_ trace2 001return 0
+ cmd_name trace2 (trace2)
+ exit elapsed:_TIME_ code:0
+ atexit elapsed:_TIME_ code:0
+ EOF
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t0210/scrub_normal.perl b/t/t0210/scrub_normal.perl
new file mode 100644
index 0000000..7cc4de3
--- /dev/null
+++ b/t/t0210/scrub_normal.perl
@@ -0,0 +1,54 @@
+#!/usr/bin/perl
+#
+# Scrub the variable fields from the normal trace2 output to
+# make testing easier.
+
+use strict;
+use warnings;
+
+my $float = '[0-9]*\.[0-9]+([eE][-+]?[0-9]+)?';
+
+# This code assumes that the trace2 data was written with bare
+# turned on (which omits the "<clock> <file>:<line>" prefix.
+
+while (<>) {
+ # Various messages include an elapsed time in the middle
+ # of the message. Replace the time with a placeholder to
+ # simplify our HEREDOC in the test script.
+ s/elapsed:$float/elapsed:_TIME_/g;
+
+ my $line = $_;
+
+ # we expect:
+ # start <argv0> [<argv1> [<argv2> [...]]]
+ #
+ # where argv0 might be a relative or absolute path, with
+ # or without quotes, and platform dependent. Replace argv0
+ # with a token for HEREDOC matching in the test script.
+
+ if ($line =~ m/^start/) {
+ $line =~ /^start\s+(.*)/;
+ my $argv = $1;
+ $argv =~ m/(\'[^\']*\'|[^ ]+)\s+(.*)/;
+ my $argv_0 = $1;
+ my $argv_rest = $2;
+
+ print "start _EXE_ $argv_rest\n";
+ }
+ elsif ($line =~ m/^cmd_path/) {
+ # Likewise, the 'cmd_path' message breaks out argv[0].
+ #
+ # This line is only emitted when RUNTIME_PREFIX is defined,
+ # so just omit it for testing purposes.
+ # print "cmd_path _EXE_\n";
+ }
+ elsif ($line =~ m/^cmd_ancestry/) {
+ # 'cmd_ancestry' is not implemented everywhere, so for portability's
+ # sake, skip it when parsing normal.
+ #
+ # print "$line";
+ }
+ else {
+ print "$line";
+ }
+}
diff --git a/t/t0211-trace2-perf.sh b/t/t0211-trace2-perf.sh
new file mode 100755
index 0000000..cfba686
--- /dev/null
+++ b/t/t0211-trace2-perf.sh
@@ -0,0 +1,271 @@
+#!/bin/sh
+
+test_description='test trace2 facility (perf target)'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# Turn off any inherited trace2 settings for this test.
+sane_unset GIT_TRACE2 GIT_TRACE2_PERF GIT_TRACE2_EVENT
+sane_unset GIT_TRACE2_PERF_BRIEF
+sane_unset GIT_TRACE2_CONFIG_PARAMS
+
+# Add t/helper directory to PATH so that we can use a relative
+# path to run nested instances of test-tool.exe (see 004child).
+# This helps with HEREDOC comparisons later.
+TTDIR="$GIT_BUILD_DIR/t/helper/" && export TTDIR
+PATH="$TTDIR:$PATH" && export PATH
+
+# Warning: use of 'test_cmp' may run test-tool.exe and/or git.exe
+# Warning: to do the actual diff/comparison, so the HEREDOCs here
+# Warning: only cover our actual calls to test-tool and/or git.
+# Warning: So you may see extra lines in artifact files when
+# Warning: interactively debugging.
+
+V=$(git version | sed -e 's/^git version //') && export V
+
+# There are multiple trace2 targets: normal, perf, and event.
+# Trace2 events will/can be written to each active target (subject
+# to whatever filtering that target decides to do).
+# Test each target independently.
+#
+# Defer setting GIT_TRACE2_PERF until the actual command we want to
+# test because hidden git and test-tool commands in the test
+# harness can contaminate our output.
+
+# Enable "brief" feature which turns off the prefix:
+# "<clock> <file>:<line> | <nr_parents> | "
+GIT_TRACE2_PERF_BRIEF=1 && export GIT_TRACE2_PERF_BRIEF
+
+# Repeat some of the t0210 tests using the perf target stream instead of
+# the normal stream.
+#
+# Tokens here of the form _FIELD_ have been replaced in the observed output.
+
+# Verb 001return
+#
+# Implicit return from cmd_<verb> function propagates <code>.
+
+test_expect_success 'perf stream, return code 0' '
+ test_when_finished "rm trace.perf actual expect" &&
+ GIT_TRACE2_PERF="$(pwd)/trace.perf" test-tool trace2 001return 0 &&
+ perl "$TEST_DIRECTORY/t0211/scrub_perf.perl" <trace.perf >actual &&
+ cat >expect <<-EOF &&
+ d0|main|version|||||$V
+ d0|main|start||_T_ABS_|||_EXE_ trace2 001return 0
+ d0|main|cmd_name|||||trace2 (trace2)
+ d0|main|exit||_T_ABS_|||code:0
+ d0|main|atexit||_T_ABS_|||code:0
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'perf stream, return code 1' '
+ test_when_finished "rm trace.perf actual expect" &&
+ test_must_fail env GIT_TRACE2_PERF="$(pwd)/trace.perf" test-tool trace2 001return 1 &&
+ perl "$TEST_DIRECTORY/t0211/scrub_perf.perl" <trace.perf >actual &&
+ cat >expect <<-EOF &&
+ d0|main|version|||||$V
+ d0|main|start||_T_ABS_|||_EXE_ trace2 001return 1
+ d0|main|cmd_name|||||trace2 (trace2)
+ d0|main|exit||_T_ABS_|||code:1
+ d0|main|atexit||_T_ABS_|||code:1
+ EOF
+ test_cmp expect actual
+'
+
+# Verb 003error
+#
+# To the above, add multiple 'error <msg>' events
+
+test_expect_success 'perf stream, error event' '
+ test_when_finished "rm trace.perf actual expect" &&
+ GIT_TRACE2_PERF="$(pwd)/trace.perf" test-tool trace2 003error "hello world" "this is a test" &&
+ perl "$TEST_DIRECTORY/t0211/scrub_perf.perl" <trace.perf >actual &&
+ cat >expect <<-EOF &&
+ d0|main|version|||||$V
+ d0|main|start||_T_ABS_|||_EXE_ trace2 003error '\''hello world'\'' '\''this is a test'\''
+ d0|main|cmd_name|||||trace2 (trace2)
+ d0|main|error|||||hello world
+ d0|main|error|||||this is a test
+ d0|main|exit||_T_ABS_|||code:0
+ d0|main|atexit||_T_ABS_|||code:0
+ EOF
+ test_cmp expect actual
+'
+
+# Verb 004child
+#
+# Test nested spawning of child processes.
+#
+# Conceptually, this looks like:
+# P1: TT trace2 004child
+# P2: |--- TT trace2 004child
+# P3: |--- TT trace2 001return 0
+#
+# Which should generate events:
+# P1: version
+# P1: start
+# P1: cmd_name
+# P1: child_start
+# P2: version
+# P2: start
+# P2: cmd_name
+# P2: child_start
+# P3: version
+# P3: start
+# P3: cmd_name
+# P3: exit
+# P3: atexit
+# P2: child_exit
+# P2: exit
+# P2: atexit
+# P1: child_exit
+# P1: exit
+# P1: atexit
+
+test_expect_success 'perf stream, child processes' '
+ test_when_finished "rm trace.perf actual expect" &&
+ GIT_TRACE2_PERF="$(pwd)/trace.perf" test-tool trace2 004child test-tool trace2 004child test-tool trace2 001return 0 &&
+ perl "$TEST_DIRECTORY/t0211/scrub_perf.perl" <trace.perf >actual &&
+ cat >expect <<-EOF &&
+ d0|main|version|||||$V
+ d0|main|start||_T_ABS_|||_EXE_ trace2 004child test-tool trace2 004child test-tool trace2 001return 0
+ d0|main|cmd_name|||||trace2 (trace2)
+ d0|main|child_start||_T_ABS_|||[ch0] class:? argv:[test-tool trace2 004child test-tool trace2 001return 0]
+ d1|main|version|||||$V
+ d1|main|start||_T_ABS_|||_EXE_ trace2 004child test-tool trace2 001return 0
+ d1|main|cmd_name|||||trace2 (trace2/trace2)
+ d1|main|child_start||_T_ABS_|||[ch0] class:? argv:[test-tool trace2 001return 0]
+ d2|main|version|||||$V
+ d2|main|start||_T_ABS_|||_EXE_ trace2 001return 0
+ d2|main|cmd_name|||||trace2 (trace2/trace2/trace2)
+ d2|main|exit||_T_ABS_|||code:0
+ d2|main|atexit||_T_ABS_|||code:0
+ d1|main|child_exit||_T_ABS_|_T_REL_||[ch0] pid:_PID_ code:0
+ d1|main|exit||_T_ABS_|||code:0
+ d1|main|atexit||_T_ABS_|||code:0
+ d0|main|child_exit||_T_ABS_|_T_REL_||[ch0] pid:_PID_ code:0
+ d0|main|exit||_T_ABS_|||code:0
+ d0|main|atexit||_T_ABS_|||code:0
+ EOF
+ test_cmp expect actual
+'
+
+sane_unset GIT_TRACE2_PERF_BRIEF
+
+# Now test without environment variables and get all Trace2 settings
+# from the global config.
+
+test_expect_success 'using global config, perf stream, return code 0' '
+ test_when_finished "rm trace.perf actual expect" &&
+ test_config_global trace2.perfBrief 1 &&
+ test_config_global trace2.perfTarget "$(pwd)/trace.perf" &&
+ test-tool trace2 001return 0 &&
+ perl "$TEST_DIRECTORY/t0211/scrub_perf.perl" <trace.perf >actual &&
+ cat >expect <<-EOF &&
+ d0|main|version|||||$V
+ d0|main|start||_T_ABS_|||_EXE_ trace2 001return 0
+ d0|main|cmd_name|||||trace2 (trace2)
+ d0|main|exit||_T_ABS_|||code:0
+ d0|main|atexit||_T_ABS_|||code:0
+ EOF
+ test_cmp expect actual
+'
+
+# Exercise the stopwatch timers in a loop and confirm that we have
+# as many start/stop intervals as expected. We cannot really test the
+# actual (total, min, max) timer values, so we have to assume that they
+# are good, but we can verify the interval count.
+#
+# The timer "test/test1" should only emit a global summary "timer" event.
+# The timer "test/test2" should emit per-thread "th_timer" events and a
+# global summary "timer" event.
+
+have_timer_event () {
+ thread=$1 event=$2 category=$3 name=$4 intervals=$5 file=$6 &&
+
+ pattern="d0|${thread}|${event}||||${category}|name:${name} intervals:${intervals}" &&
+
+ grep "${pattern}" ${file}
+}
+
+test_expect_success 'stopwatch timer test/test1' '
+ test_when_finished "rm trace.perf actual" &&
+ test_config_global trace2.perfBrief 1 &&
+ test_config_global trace2.perfTarget "$(pwd)/trace.perf" &&
+
+ # Use the timer "test1" 5 times from "main".
+ test-tool trace2 100timer 5 10 &&
+
+ perl "$TEST_DIRECTORY/t0211/scrub_perf.perl" <trace.perf >actual &&
+
+ have_timer_event "main" "timer" "test" "test1" 5 actual
+'
+
+test_expect_success PTHREADS 'stopwatch timer test/test2' '
+ test_when_finished "rm trace.perf actual" &&
+ test_config_global trace2.perfBrief 1 &&
+ test_config_global trace2.perfTarget "$(pwd)/trace.perf" &&
+
+ # Use the timer "test2" 5 times each in 3 threads.
+ test-tool trace2 101timer 5 10 3 &&
+
+ perl "$TEST_DIRECTORY/t0211/scrub_perf.perl" <trace.perf >actual &&
+
+ # So we should have 3 per-thread events of 5 each.
+ have_timer_event "th01:ut_101" "th_timer" "test" "test2" 5 actual &&
+ have_timer_event "th02:ut_101" "th_timer" "test" "test2" 5 actual &&
+ have_timer_event "th03:ut_101" "th_timer" "test" "test2" 5 actual &&
+
+ # And we should have 15 total uses.
+ have_timer_event "main" "timer" "test" "test2" 15 actual
+'
+
+# Exercise the global counters and confirm that we get the expected values.
+#
+# The counter "test/test1" should only emit a global summary "counter" event.
+# The counter "test/test2" could emit per-thread "th_counter" events and a
+# global summary "counter" event.
+
+have_counter_event () {
+ thread=$1 event=$2 category=$3 name=$4 value=$5 file=$6 &&
+
+ pattern="d0|${thread}|${event}||||${category}|name:${name} value:${value}" &&
+
+ grep "${patern}" ${file}
+}
+
+test_expect_success 'global counter test/test1' '
+ test_when_finished "rm trace.perf actual" &&
+ test_config_global trace2.perfBrief 1 &&
+ test_config_global trace2.perfTarget "$(pwd)/trace.perf" &&
+
+ # Use the counter "test1" and add n integers.
+ test-tool trace2 200counter 1 2 3 4 5 &&
+
+ perl "$TEST_DIRECTORY/t0211/scrub_perf.perl" <trace.perf >actual &&
+
+ have_counter_event "main" "counter" "test" "test1" 15 actual
+'
+
+test_expect_success PTHREADS 'global counter test/test2' '
+ test_when_finished "rm trace.perf actual" &&
+ test_config_global trace2.perfBrief 1 &&
+ test_config_global trace2.perfTarget "$(pwd)/trace.perf" &&
+
+ # Add 2 integers to the counter "test2" in each of 3 threads.
+ test-tool trace2 201counter 7 13 3 &&
+
+ perl "$TEST_DIRECTORY/t0211/scrub_perf.perl" <trace.perf >actual &&
+
+ # So we should have 3 per-thread events of 5 each.
+ have_counter_event "th01:ut_201" "th_counter" "test" "test2" 20 actual &&
+ have_counter_event "th02:ut_201" "th_counter" "test" "test2" 20 actual &&
+ have_counter_event "th03:ut_201" "th_counter" "test" "test2" 20 actual &&
+
+ # And we should have a single event with the total across all threads.
+ have_counter_event "main" "counter" "test" "test2" 60 actual
+'
+
+test_done
diff --git a/t/t0211/scrub_perf.perl b/t/t0211/scrub_perf.perl
new file mode 100644
index 0000000..7a50bae
--- /dev/null
+++ b/t/t0211/scrub_perf.perl
@@ -0,0 +1,91 @@
+#!/usr/bin/perl
+#
+# Scrub the variable fields from the perf trace2 output to
+# make testing easier.
+
+use strict;
+use warnings;
+
+my $qpath = '\'[^\']*\'|[^ ]*';
+
+my $col_depth=0;
+my $col_thread=1;
+my $col_event=2;
+my $col_repo=3;
+my $col_t_abs=4;
+my $col_t_rel=5;
+my $col_category=6;
+my $col_rest=7;
+
+# This code assumes that the trace2 data was written with bare
+# turned on (which omits the "<clock> <file>:<line> | <parents>"
+# prefix.
+
+while (<>) {
+ my @tokens = split /\|/;
+
+ foreach my $col (@tokens) { $col =~ s/^\s+|\s+$//g; }
+
+ if ($tokens[$col_event] =~ m/^start/) {
+ # The 'start' message lists the contents of argv in $col_rest.
+ # On some platforms (Windows), argv[0] is *sometimes* a canonical
+ # absolute path to the EXE rather than the value passed in the
+ # shell script. Replace it with a placeholder to simplify our
+ # HEREDOC in the test script.
+ my $argv0;
+ my $argvRest;
+ $tokens[$col_rest] =~ s/^($qpath)\W*(.*)/_EXE_ $2/;
+ }
+ elsif ($tokens[$col_event] =~ m/cmd_path/) {
+ # Likewise, the 'cmd_path' message breaks out argv[0].
+ #
+ # This line is only emitted when RUNTIME_PREFIX is defined,
+ # so just omit it for testing purposes.
+ # $tokens[$col_rest] = "_EXE_";
+ goto SKIP_LINE;
+ }
+ elsif ($tokens[$col_event] =~ m/cmd_ancestry/) {
+ # 'cmd_ancestry' is platform-specific and not implemented everywhere,
+ # so skip it.
+ goto SKIP_LINE;
+ }
+ elsif ($tokens[$col_event] =~ m/child_exit/) {
+ $tokens[$col_rest] =~ s/ pid:\d* / pid:_PID_ /;
+ }
+ elsif ($tokens[$col_event] =~ m/data/) {
+ if ($tokens[$col_category] =~ m/process/) {
+ # 'data' and 'data_json' events containing 'process'
+ # category data are assumed to be platform-specific
+ # and highly variable. Just omit them.
+ goto SKIP_LINE;
+ }
+ if ($tokens[$col_category] =~ m/fsync/) {
+ # fsync events aren't interesting for the test
+ goto SKIP_LINE;
+ }
+ }
+ elsif ($tokens[$col_event] =~ m/timer/) {
+ # This also captures "th_timer" events
+ $tokens[$col_rest] =~ s/ total:\d+\.\d*/ total:_T_TOTAL_/;
+ $tokens[$col_rest] =~ s/ min:\d+\.\d*/ min:_T_MIN_/;
+ $tokens[$col_rest] =~ s/ max:\d+\.\d*/ max:_T_MAX_/;
+ }
+
+ # t_abs and t_rel are either blank or a float. Replace the float
+ # with a constant for matching the HEREDOC in the test script.
+ if ($tokens[$col_t_abs] =~ m/\d/) {
+ $tokens[$col_t_abs] = "_T_ABS_";
+ }
+ if ($tokens[$col_t_rel] =~ m/\d/) {
+ $tokens[$col_t_rel] = "_T_REL_";
+ }
+
+ my $out;
+
+ $out = join('|', @tokens);
+ print "$out\n";
+
+ SKIP_LINE:
+}
+
+
diff --git a/t/t0212-trace2-event.sh b/t/t0212-trace2-event.sh
new file mode 100755
index 0000000..6d3374f
--- /dev/null
+++ b/t/t0212-trace2-event.sh
@@ -0,0 +1,326 @@
+#!/bin/sh
+
+test_description='test trace2 facility'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# Turn off any inherited trace2 settings for this test.
+sane_unset GIT_TRACE2 GIT_TRACE2_PERF GIT_TRACE2_EVENT
+sane_unset GIT_TRACE2_BARE
+sane_unset GIT_TRACE2_CONFIG_PARAMS
+
+perl -MJSON::PP -e 0 >/dev/null 2>&1 && test_set_prereq JSON_PP
+
+# Add t/helper directory to PATH so that we can use a relative
+# path to run nested instances of test-tool.exe (see 004child).
+# This helps with HEREDOC comparisons later.
+TTDIR="$GIT_BUILD_DIR/t/helper/" && export TTDIR
+PATH="$TTDIR:$PATH" && export PATH
+
+# Warning: use of 'test_cmp' may run test-tool.exe and/or git.exe
+# Warning: to do the actual diff/comparison, so the HEREDOCs here
+# Warning: only cover our actual calls to test-tool and/or git.
+# Warning: So you may see extra lines in artifact files when
+# Warning: interactively debugging.
+
+V=$(git version | sed -e 's/^git version //') && export V
+
+# There are multiple trace2 targets: normal, perf, and event.
+# Trace2 events will/can be written to each active target (subject
+# to whatever filtering that target decides to do).
+# Test each target independently.
+#
+# Defer setting GIT_TRACE2_PERF until the actual command we want to
+# test because hidden git and test-tool commands in the test
+# harness can contaminate our output.
+
+# We don't bother repeating the 001return and 002exit tests, since they
+# have coverage in the normal and perf targets.
+
+# Verb 003error
+#
+# To the above, add multiple 'error <msg>' events
+
+test_expect_success JSON_PP 'event stream, error event' '
+ test_when_finished "rm trace.event actual expect" &&
+ GIT_TRACE2_EVENT="$(pwd)/trace.event" test-tool trace2 003error "hello world" "this is a test" &&
+ perl "$TEST_DIRECTORY/t0212/parse_events.perl" <trace.event >actual &&
+ sed -e "s/^|//" >expect <<-EOF &&
+ |VAR1 = {
+ | "_SID0_":{
+ | "argv":[
+ | "_EXE_",
+ | "trace2",
+ | "003error",
+ | "hello world",
+ | "this is a test"
+ | ],
+ | "errors":[
+ | "%s",
+ | "%s"
+ | ],
+ | "exit_code":0,
+ | "hierarchy":"trace2",
+ | "name":"trace2",
+ | "version":"$V"
+ | }
+ |};
+ EOF
+ test_cmp expect actual
+'
+
+# Verb 004child
+#
+# Test nested spawning of child processes.
+#
+# Conceptually, this looks like:
+# P1: TT trace2 004child
+# P2: |--- TT trace2 004child
+# P3: |--- TT trace2 001return 0
+
+test_expect_success JSON_PP 'event stream, return code 0' '
+ test_when_finished "rm trace.event actual expect" &&
+ GIT_TRACE2_EVENT="$(pwd)/trace.event" test-tool trace2 004child test-tool trace2 004child test-tool trace2 001return 0 &&
+ perl "$TEST_DIRECTORY/t0212/parse_events.perl" <trace.event >actual &&
+ sed -e "s/^|//" >expect <<-EOF &&
+ |VAR1 = {
+ | "_SID0_":{
+ | "argv":[
+ | "_EXE_",
+ | "trace2",
+ | "004child",
+ | "test-tool",
+ | "trace2",
+ | "004child",
+ | "test-tool",
+ | "trace2",
+ | "001return",
+ | "0"
+ | ],
+ | "child":{
+ | "0":{
+ | "child_argv":[
+ | "_EXE_",
+ | "trace2",
+ | "004child",
+ | "test-tool",
+ | "trace2",
+ | "001return",
+ | "0"
+ | ],
+ | "child_class":"?",
+ | "child_code":0,
+ | "use_shell":0
+ | }
+ | },
+ | "exit_code":0,
+ | "hierarchy":"trace2",
+ | "name":"trace2",
+ | "version":"$V"
+ | },
+ | "_SID0_/_SID1_":{
+ | "argv":[
+ | "_EXE_",
+ | "trace2",
+ | "004child",
+ | "test-tool",
+ | "trace2",
+ | "001return",
+ | "0"
+ | ],
+ | "child":{
+ | "0":{
+ | "child_argv":[
+ | "_EXE_",
+ | "trace2",
+ | "001return",
+ | "0"
+ | ],
+ | "child_class":"?",
+ | "child_code":0,
+ | "use_shell":0
+ | }
+ | },
+ | "exit_code":0,
+ | "hierarchy":"trace2/trace2",
+ | "name":"trace2",
+ | "version":"$V"
+ | },
+ | "_SID0_/_SID1_/_SID2_":{
+ | "argv":[
+ | "_EXE_",
+ | "trace2",
+ | "001return",
+ | "0"
+ | ],
+ | "exit_code":0,
+ | "hierarchy":"trace2/trace2/trace2",
+ | "name":"trace2",
+ | "version":"$V"
+ | }
+ |};
+ EOF
+ test_cmp expect actual
+'
+
+# Test listing of all "interesting" config settings.
+
+test_expect_success JSON_PP 'event stream, list config' '
+ test_when_finished "rm trace.event actual expect" &&
+ git config --local t0212.abc 1 &&
+ git config --local t0212.def "hello world" &&
+ GIT_TRACE2_EVENT="$(pwd)/trace.event" GIT_TRACE2_CONFIG_PARAMS="t0212.*" test-tool trace2 001return 0 &&
+ perl "$TEST_DIRECTORY/t0212/parse_events.perl" <trace.event >actual &&
+ sed -e "s/^|//" >expect <<-EOF &&
+ |VAR1 = {
+ | "_SID0_":{
+ | "argv":[
+ | "_EXE_",
+ | "trace2",
+ | "001return",
+ | "0"
+ | ],
+ | "exit_code":0,
+ | "hierarchy":"trace2",
+ | "name":"trace2",
+ | "params":[
+ | {
+ | "param":"t0212.abc",
+ | "value":"1"
+ | },
+ | {
+ | "param":"t0212.def",
+ | "value":"hello world"
+ | }
+ | ],
+ | "version":"$V"
+ | }
+ |};
+ EOF
+ test_cmp expect actual
+'
+
+# Test listing of all "interesting" environment variables.
+
+test_expect_success JSON_PP 'event stream, list env vars' '
+ test_when_finished "rm trace.event actual expect" &&
+ GIT_TRACE2_EVENT="$(pwd)/trace.event" \
+ GIT_TRACE2_ENV_VARS="A_VAR,OTHER_VAR,MISSING" \
+ A_VAR=1 OTHER_VAR="hello world" test-tool trace2 001return 0 &&
+ perl "$TEST_DIRECTORY/t0212/parse_events.perl" <trace.event >actual &&
+ sed -e "s/^|//" >expect <<-EOF &&
+ |VAR1 = {
+ | "_SID0_":{
+ | "argv":[
+ | "_EXE_",
+ | "trace2",
+ | "001return",
+ | "0"
+ | ],
+ | "exit_code":0,
+ | "hierarchy":"trace2",
+ | "name":"trace2",
+ | "params":[
+ | {
+ | "param":"A_VAR",
+ | "value":"1"
+ | },
+ | {
+ | "param":"OTHER_VAR",
+ | "value":"hello world"
+ | }
+ | ],
+ | "version":"$V"
+ | }
+ |};
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success JSON_PP 'basic trace2_data' '
+ test_when_finished "rm trace.event actual expect" &&
+ GIT_TRACE2_EVENT="$(pwd)/trace.event" test-tool trace2 006data test_category k1 v1 test_category k2 v2 &&
+ perl "$TEST_DIRECTORY/t0212/parse_events.perl" <trace.event >actual &&
+ sed -e "s/^|//" >expect <<-EOF &&
+ |VAR1 = {
+ | "_SID0_":{
+ | "argv":[
+ | "_EXE_",
+ | "trace2",
+ | "006data",
+ | "test_category",
+ | "k1",
+ | "v1",
+ | "test_category",
+ | "k2",
+ | "v2"
+ | ],
+ | "data":{
+ | "test_category":{
+ | "k1":"v1",
+ | "k2":"v2"
+ | }
+ | },
+ | "exit_code":0,
+ | "hierarchy":"trace2",
+ | "name":"trace2",
+ | "version":"$V"
+ | }
+ |};
+ EOF
+ test_cmp expect actual
+'
+
+# Now test without environment variables and get all Trace2 settings
+# from the global config.
+
+test_expect_success JSON_PP 'using global config, event stream, error event' '
+ test_when_finished "rm trace.event actual expect" &&
+ test_config_global trace2.eventTarget "$(pwd)/trace.event" &&
+ test-tool trace2 003error "hello world" "this is a test" &&
+ perl "$TEST_DIRECTORY/t0212/parse_events.perl" <trace.event >actual &&
+ sed -e "s/^|//" >expect <<-EOF &&
+ |VAR1 = {
+ | "_SID0_":{
+ | "argv":[
+ | "_EXE_",
+ | "trace2",
+ | "003error",
+ | "hello world",
+ | "this is a test"
+ | ],
+ | "errors":[
+ | "%s",
+ | "%s"
+ | ],
+ | "exit_code":0,
+ | "hierarchy":"trace2",
+ | "name":"trace2",
+ | "version":"$V"
+ | }
+ |};
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'discard traces when there are too many files' '
+ mkdir trace_target_dir &&
+ test_when_finished "rm -r trace_target_dir" &&
+ (
+ GIT_TRACE2_MAX_FILES=5 &&
+ export GIT_TRACE2_MAX_FILES &&
+ cd trace_target_dir &&
+ test_seq $GIT_TRACE2_MAX_FILES >../expected_filenames.txt &&
+ xargs touch <../expected_filenames.txt &&
+ cd .. &&
+ GIT_TRACE2_EVENT="$(pwd)/trace_target_dir" test-tool trace2 001return 0
+ ) &&
+ echo git-trace2-discard >>expected_filenames.txt &&
+ ls trace_target_dir >ls_output.txt &&
+ test_cmp expected_filenames.txt ls_output.txt &&
+ head -n1 trace_target_dir/git-trace2-discard | grep \"event\":\"version\" &&
+ head -n2 trace_target_dir/git-trace2-discard | tail -n1 | grep \"event\":\"too_many_files\"
+'
+
+test_done
diff --git a/t/t0212/parse_events.perl b/t/t0212/parse_events.perl
new file mode 100644
index 0000000..30a9f51
--- /dev/null
+++ b/t/t0212/parse_events.perl
@@ -0,0 +1,261 @@
+#!/usr/bin/perl
+#
+# Parse event stream and convert individual events into a summary
+# record for the process.
+#
+# Git.exe generates one or more "event" records for each API method,
+# such as "start <argv>" and "exit <code>", during the life of the git
+# process. Additionally, the input may contain interleaved events
+# from multiple concurrent git processes and/or multiple threads from
+# within a git process.
+#
+# Accumulate events for each process (based on its unique SID) in a
+# dictionary and emit process summary records.
+#
+# Convert some of the variable fields (such as elapsed time) into
+# placeholders (or omit them) to make HEREDOC comparisons easier in
+# the test scripts.
+#
+# We may also omit fields not (currently) useful for testing purposes.
+
+use strict;
+use warnings;
+use JSON::PP;
+use Data::Dumper;
+use Getopt::Long;
+
+# The version of the trace2 event target format that we understand.
+# This is reported in the 'version' event in the 'evt' field.
+# It comes from the GIT_TRACE2_EVENT_VERSION macro in trace2/tr2_tgt_event.c
+my $evt_version = '1';
+
+my $show_children = 1;
+my $show_exec = 1;
+my $show_threads = 1;
+
+# A hack to generate test HEREDOC data for pasting into the test script.
+# Usage:
+# cd "t/trash directory.t0212-trace2-event"
+# $TT trace ... >trace.event
+# VV=$(../../git.exe version | sed -e 's/^git version //')
+# perl ../t0212/parse_events.perl --HEREDOC --VERSION=$VV <trace.event >heredoc
+# Then paste heredoc into your new test.
+
+my $gen_heredoc = 0;
+my $gen_version = '';
+
+GetOptions("children!" => \$show_children,
+ "exec!" => \$show_exec,
+ "threads!" => \$show_threads,
+ "HEREDOC!" => \$gen_heredoc,
+ "VERSION=s" => \$gen_version )
+ or die("Error in command line arguments\n");
+
+
+# SIDs contains timestamps and PIDs of the process and its parents.
+# This makes it difficult to match up in a HEREDOC in the test script.
+# Build a map from actual SIDs to predictable constant values and yet
+# keep the parent/child relationships. For example:
+# {..., "sid":"1539706952458276-8652", ...}
+# {..., "sid":"1539706952458276-8652/1539706952649493-15452", ...}
+# becomes:
+# {..., "sid":"_SID1_", ...}
+# {..., "sid":"_SID1_/_SID2_", ...}
+my $sid_map;
+my $sid_count = 0;
+
+my $processes;
+
+while (<>) {
+ my $line = decode_json( $_ );
+
+ my $sid = "";
+ my $sid_sep = "";
+
+ my $raw_sid = $line->{'sid'};
+ my @raw_sid_parts = split /\//, $raw_sid;
+ foreach my $raw_sid_k (@raw_sid_parts) {
+ if (!exists $sid_map->{$raw_sid_k}) {
+ $sid_map->{$raw_sid_k} = '_SID' . $sid_count . '_';
+ $sid_count++;
+ }
+ $sid = $sid . $sid_sep . $sid_map->{$raw_sid_k};
+ $sid_sep = '/';
+ }
+
+ my $event = $line->{'event'};
+
+ if ($event eq 'version') {
+ $processes->{$sid}->{'version'} = $line->{'exe'};
+ if ($gen_heredoc == 1 && $gen_version eq $line->{'exe'}) {
+ # If we are generating data FOR the test script, replace
+ # the reported git.exe version with a reference to an
+ # environment variable. When our output is pasted into
+ # the test script, it will then be expanded in future
+ # test runs to the THEN current version of git.exe.
+ # We assume that the test script uses env var $V.
+ $processes->{$sid}->{'version'} = "\$V";
+ }
+ }
+
+ elsif ($event eq 'start') {
+ $processes->{$sid}->{'argv'} = $line->{'argv'};
+ $processes->{$sid}->{'argv'}[0] = "_EXE_";
+ }
+
+ elsif ($event eq 'exit') {
+ $processes->{$sid}->{'exit_code'} = $line->{'code'};
+ }
+
+ elsif ($event eq 'atexit') {
+ $processes->{$sid}->{'exit_code'} = $line->{'code'};
+ }
+
+ elsif ($event eq 'error') {
+ # For HEREDOC purposes, use the error message format string if
+ # available, rather than the formatted message (which probably
+ # has an absolute pathname).
+ if (exists $line->{'fmt'}) {
+ push( @{$processes->{$sid}->{'errors'}}, $line->{'fmt'} );
+ }
+ elsif (exists $line->{'msg'}) {
+ push( @{$processes->{$sid}->{'errors'}}, $line->{'msg'} );
+ }
+ }
+
+ elsif ($event eq 'cmd_path') {
+ ## $processes->{$sid}->{'path'} = $line->{'path'};
+ #
+ # Like in the 'start' event, we need to replace the value of
+ # argv[0] with a token for HEREDOC purposes. However, the
+ # event is only emitted when RUNTIME_PREFIX is defined, so
+ # just omit it for testing purposes.
+ # $processes->{$sid}->{'path'} = "_EXE_";
+ }
+ elsif ($event eq 'cmd_ancestry') {
+ # 'cmd_ancestry' is platform-specific and not implemented everywhere, so
+ # just skip it for testing purposes.
+ }
+ elsif ($event eq 'cmd_name') {
+ $processes->{$sid}->{'name'} = $line->{'name'};
+ $processes->{$sid}->{'hierarchy'} = $line->{'hierarchy'};
+ }
+
+ elsif ($event eq 'alias') {
+ $processes->{$sid}->{'alias'}->{'key'} = $line->{'alias'};
+ $processes->{$sid}->{'alias'}->{'argv'} = $line->{'argv'};
+ }
+
+ elsif ($event eq 'def_param') {
+ my $kv;
+ $kv->{'param'} = $line->{'param'};
+ $kv->{'value'} = $line->{'value'};
+ push( @{$processes->{$sid}->{'params'}}, $kv );
+ }
+
+ elsif ($event eq 'child_start') {
+ if ($show_children == 1) {
+ $processes->{$sid}->{'child'}->{$line->{'child_id'}}->{'child_class'} = $line->{'child_class'};
+ $processes->{$sid}->{'child'}->{$line->{'child_id'}}->{'child_argv'} = $line->{'argv'};
+ $processes->{$sid}->{'child'}->{$line->{'child_id'}}->{'child_argv'}[0] = "_EXE_";
+ $processes->{$sid}->{'child'}->{$line->{'child_id'}}->{'use_shell'} = $line->{'use_shell'} ? 1 : 0;
+ }
+ }
+
+ elsif ($event eq 'child_exit') {
+ if ($show_children == 1) {
+ $processes->{$sid}->{'child'}->{$line->{'child_id'}}->{'child_code'} = $line->{'code'};
+ }
+ }
+
+ # TODO decide what information we want to test from thread events.
+
+ elsif ($event eq 'thread_start') {
+ if ($show_threads == 1) {
+ }
+ }
+
+ elsif ($event eq 'thread_exit') {
+ if ($show_threads == 1) {
+ }
+ }
+
+ # TODO decide what information we want to test from exec events.
+
+ elsif ($event eq 'exec') {
+ if ($show_exec == 1) {
+ }
+ }
+
+ elsif ($event eq 'exec_result') {
+ if ($show_exec == 1) {
+ }
+ }
+
+ elsif ($event eq 'def_param') {
+ # Accumulate parameter key/value pairs by key rather than in an array
+ # so that we get overwrite (last one wins) effects.
+ $processes->{$sid}->{'params'}->{$line->{'param'}} = $line->{'value'};
+ }
+
+ elsif ($event eq 'def_repo') {
+ # $processes->{$sid}->{'repos'}->{$line->{'repo'}} = $line->{'worktree'};
+ $processes->{$sid}->{'repos'}->{$line->{'repo'}} = "_WORKTREE_";
+ }
+
+ # A series of potentially nested and threaded region and data events
+ # is fundamentally incompatibile with the type of summary record we
+ # are building in this script. Since they are intended for
+ # perf-trace-like analysis rather than a result summary, we ignore
+ # most of them here.
+
+ # elsif ($event eq 'region_enter') {
+ # }
+ # elsif ($event eq 'region_leave') {
+ # }
+
+ elsif ($event eq 'data') {
+ my $cat = $line->{'category'};
+ my $key = $line->{'key'};
+ my $value = $line->{'value'};
+ $processes->{$sid}->{'data'}->{$cat}->{$key} = $value;
+ }
+
+ elsif ($event eq 'data_json') {
+ # NEEDSWORK: Ignore due to
+ # compat/win32/trace2_win32_process_info.c, which should log a
+ # "cmd_ancestry" event instead.
+ }
+
+ else {
+ push @{$processes->{$sid}->{$event}} => $line->{value};
+ }
+
+ # This trace2 target does not emit 'printf' events.
+ #
+ # elsif ($event eq 'printf') {
+ # }
+}
+
+# Dump the resulting hash into something that we can compare against
+# in the test script. These options make Dumper output look a little
+# bit like JSON. Also convert variable references of the form "$VAR*"
+# so that the matching HEREDOC doesn't need to escape it.
+
+$Data::Dumper::Sortkeys = 1;
+$Data::Dumper::Indent = 1;
+$Data::Dumper::Purity = 1;
+$Data::Dumper::Pair = ':';
+
+my $out = Dumper($processes);
+$out =~ s/'/"/g;
+$out =~ s/\$VAR/VAR/g;
+
+# Finally, if we're running this script to generate (manually confirmed)
+# data to add to the test script, guard the indentation.
+
+if ($gen_heredoc == 1) {
+ $out =~ s/^/\t\|/gms;
+}
+
+print $out;
diff --git a/t/t0300-credentials.sh b/t/t0300-credentials.sh
new file mode 100755
index 0000000..400f6bd
--- /dev/null
+++ b/t/t0300-credentials.sh
@@ -0,0 +1,833 @@
+#!/bin/sh
+
+test_description='basic credential helper tests'
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-credential.sh
+
+test_expect_success 'setup helper scripts' '
+ cat >dump <<-\EOF &&
+ whoami=$(echo $0 | sed s/.*git-credential-//)
+ echo >&2 "$whoami: $*"
+ OIFS=$IFS
+ IFS==
+ while read key value; do
+ echo >&2 "$whoami: $key=$value"
+ eval "$key=$value"
+ done
+ IFS=$OIFS
+ EOF
+
+ write_script git-credential-useless <<-\EOF &&
+ . ./dump
+ exit 0
+ EOF
+
+ write_script git-credential-quit <<-\EOF &&
+ . ./dump
+ echo quit=1
+ EOF
+
+ write_script git-credential-verbatim <<-\EOF &&
+ user=$1; shift
+ pass=$1; shift
+ . ./dump
+ test -z "$user" || echo username=$user
+ test -z "$pass" || echo password=$pass
+ EOF
+
+ write_script git-credential-verbatim-with-expiry <<-\EOF &&
+ user=$1; shift
+ pass=$1; shift
+ pexpiry=$1; shift
+ . ./dump
+ test -z "$user" || echo username=$user
+ test -z "$pass" || echo password=$pass
+ test -z "$pexpiry" || echo password_expiry_utc=$pexpiry
+ EOF
+
+ PATH="$PWD:$PATH"
+'
+
+test_expect_success 'credential_fill invokes helper' '
+ check fill "verbatim foo bar" <<-\EOF
+ protocol=http
+ host=example.com
+ --
+ protocol=http
+ host=example.com
+ username=foo
+ password=bar
+ --
+ verbatim: get
+ verbatim: protocol=http
+ verbatim: host=example.com
+ EOF
+'
+
+test_expect_success 'credential_fill invokes multiple helpers' '
+ check fill useless "verbatim foo bar" <<-\EOF
+ protocol=http
+ host=example.com
+ --
+ protocol=http
+ host=example.com
+ username=foo
+ password=bar
+ --
+ useless: get
+ useless: protocol=http
+ useless: host=example.com
+ verbatim: get
+ verbatim: protocol=http
+ verbatim: host=example.com
+ EOF
+'
+
+test_expect_success 'credential_fill stops when we get a full response' '
+ check fill "verbatim one two" "verbatim three four" <<-\EOF
+ protocol=http
+ host=example.com
+ --
+ protocol=http
+ host=example.com
+ username=one
+ password=two
+ --
+ verbatim: get
+ verbatim: protocol=http
+ verbatim: host=example.com
+ EOF
+'
+
+test_expect_success 'credential_fill continues through partial response' '
+ check fill "verbatim one \"\"" "verbatim two three" <<-\EOF
+ protocol=http
+ host=example.com
+ --
+ protocol=http
+ host=example.com
+ username=two
+ password=three
+ --
+ verbatim: get
+ verbatim: protocol=http
+ verbatim: host=example.com
+ verbatim: get
+ verbatim: protocol=http
+ verbatim: host=example.com
+ verbatim: username=one
+ EOF
+'
+
+test_expect_success 'credential_fill populates password_expiry_utc' '
+ check fill "verbatim-with-expiry one two 9999999999" <<-\EOF
+ protocol=http
+ host=example.com
+ --
+ protocol=http
+ host=example.com
+ username=one
+ password=two
+ password_expiry_utc=9999999999
+ --
+ verbatim-with-expiry: get
+ verbatim-with-expiry: protocol=http
+ verbatim-with-expiry: host=example.com
+ EOF
+'
+
+test_expect_success 'credential_fill ignores expired password' '
+ check fill "verbatim-with-expiry one two 5" "verbatim three four" <<-\EOF
+ protocol=http
+ host=example.com
+ --
+ protocol=http
+ host=example.com
+ username=three
+ password=four
+ --
+ verbatim-with-expiry: get
+ verbatim-with-expiry: protocol=http
+ verbatim-with-expiry: host=example.com
+ verbatim: get
+ verbatim: protocol=http
+ verbatim: host=example.com
+ verbatim: username=one
+ EOF
+'
+
+test_expect_success 'credential_fill passes along metadata' '
+ check fill "verbatim one two" <<-\EOF
+ protocol=ftp
+ host=example.com
+ path=foo.git
+ --
+ protocol=ftp
+ host=example.com
+ path=foo.git
+ username=one
+ password=two
+ --
+ verbatim: get
+ verbatim: protocol=ftp
+ verbatim: host=example.com
+ verbatim: path=foo.git
+ EOF
+'
+
+test_expect_success 'credential_approve calls all helpers' '
+ check approve useless "verbatim one two" <<-\EOF
+ protocol=http
+ host=example.com
+ username=foo
+ password=bar
+ --
+ --
+ useless: store
+ useless: protocol=http
+ useless: host=example.com
+ useless: username=foo
+ useless: password=bar
+ verbatim: store
+ verbatim: protocol=http
+ verbatim: host=example.com
+ verbatim: username=foo
+ verbatim: password=bar
+ EOF
+'
+
+test_expect_success 'credential_approve stores password expiry' '
+ check approve useless <<-\EOF
+ protocol=http
+ host=example.com
+ username=foo
+ password=bar
+ password_expiry_utc=9999999999
+ --
+ --
+ useless: store
+ useless: protocol=http
+ useless: host=example.com
+ useless: username=foo
+ useless: password=bar
+ useless: password_expiry_utc=9999999999
+ EOF
+'
+
+test_expect_success 'credential_approve stores oauth refresh token' '
+ check approve useless <<-\EOF
+ protocol=http
+ host=example.com
+ username=foo
+ password=bar
+ oauth_refresh_token=xyzzy
+ --
+ --
+ useless: store
+ useless: protocol=http
+ useless: host=example.com
+ useless: username=foo
+ useless: password=bar
+ useless: oauth_refresh_token=xyzzy
+ EOF
+'
+
+test_expect_success 'do not bother storing password-less credential' '
+ check approve useless <<-\EOF
+ protocol=http
+ host=example.com
+ username=foo
+ --
+ --
+ EOF
+'
+
+test_expect_success 'credential_approve does not store expired password' '
+ check approve useless <<-\EOF
+ protocol=http
+ host=example.com
+ username=foo
+ password=bar
+ password_expiry_utc=5
+ --
+ --
+ EOF
+'
+
+test_expect_success 'credential_reject calls all helpers' '
+ check reject useless "verbatim one two" <<-\EOF
+ protocol=http
+ host=example.com
+ username=foo
+ password=bar
+ --
+ --
+ useless: erase
+ useless: protocol=http
+ useless: host=example.com
+ useless: username=foo
+ useless: password=bar
+ verbatim: erase
+ verbatim: protocol=http
+ verbatim: host=example.com
+ verbatim: username=foo
+ verbatim: password=bar
+ EOF
+'
+
+test_expect_success 'credential_reject erases credential regardless of expiry' '
+ check reject useless <<-\EOF
+ protocol=http
+ host=example.com
+ username=foo
+ password=bar
+ password_expiry_utc=5
+ --
+ --
+ useless: erase
+ useless: protocol=http
+ useless: host=example.com
+ useless: username=foo
+ useless: password=bar
+ useless: password_expiry_utc=5
+ EOF
+'
+
+test_expect_success 'usernames can be preserved' '
+ check fill "verbatim \"\" three" <<-\EOF
+ protocol=http
+ host=example.com
+ username=one
+ --
+ protocol=http
+ host=example.com
+ username=one
+ password=three
+ --
+ verbatim: get
+ verbatim: protocol=http
+ verbatim: host=example.com
+ verbatim: username=one
+ EOF
+'
+
+test_expect_success 'usernames can be overridden' '
+ check fill "verbatim two three" <<-\EOF
+ protocol=http
+ host=example.com
+ username=one
+ --
+ protocol=http
+ host=example.com
+ username=two
+ password=three
+ --
+ verbatim: get
+ verbatim: protocol=http
+ verbatim: host=example.com
+ verbatim: username=one
+ EOF
+'
+
+test_expect_success 'do not bother completing already-full credential' '
+ check fill "verbatim three four" <<-\EOF
+ protocol=http
+ host=example.com
+ username=one
+ password=two
+ --
+ protocol=http
+ host=example.com
+ username=one
+ password=two
+ --
+ EOF
+'
+
+# We can't test the basic terminal password prompt here because
+# getpass() tries too hard to find the real terminal. But if our
+# askpass helper is run, we know the internal getpass is working.
+test_expect_success 'empty helper list falls back to internal getpass' '
+ check fill <<-\EOF
+ protocol=http
+ host=example.com
+ --
+ protocol=http
+ host=example.com
+ username=askpass-username
+ password=askpass-password
+ --
+ askpass: Username for '\''http://example.com'\'':
+ askpass: Password for '\''http://askpass-username@example.com'\'':
+ EOF
+'
+
+test_expect_success 'internal getpass does not ask for known username' '
+ check fill <<-\EOF
+ protocol=http
+ host=example.com
+ username=foo
+ --
+ protocol=http
+ host=example.com
+ username=foo
+ password=askpass-password
+ --
+ askpass: Password for '\''http://foo@example.com'\'':
+ EOF
+'
+
+test_expect_success 'git-credential respects core.askPass' '
+ write_script alternate-askpass <<-\EOF &&
+ echo >&2 "alternate askpass invoked"
+ echo alternate-value
+ EOF
+ test_config core.askpass "$PWD/alternate-askpass" &&
+ (
+ # unset GIT_ASKPASS set by lib-credential.sh which would
+ # override our config, but do so in a subshell so that we do
+ # not interfere with other tests
+ sane_unset GIT_ASKPASS &&
+ check fill <<-\EOF
+ protocol=http
+ host=example.com
+ --
+ protocol=http
+ host=example.com
+ username=alternate-value
+ password=alternate-value
+ --
+ alternate askpass invoked
+ alternate askpass invoked
+ EOF
+ )
+'
+
+HELPER="!f() {
+ cat >/dev/null
+ echo username=foo
+ echo password=bar
+ }; f"
+test_expect_success 'respect configured credentials' '
+ test_config credential.helper "$HELPER" &&
+ check fill <<-\EOF
+ protocol=http
+ host=example.com
+ --
+ protocol=http
+ host=example.com
+ username=foo
+ password=bar
+ --
+ EOF
+'
+
+test_expect_success 'match configured credential' '
+ test_config credential.https://example.com.helper "$HELPER" &&
+ check fill <<-\EOF
+ protocol=https
+ host=example.com
+ path=repo.git
+ --
+ protocol=https
+ host=example.com
+ username=foo
+ password=bar
+ --
+ EOF
+'
+
+test_expect_success 'do not match configured credential' '
+ test_config credential.https://foo.helper "$HELPER" &&
+ check fill <<-\EOF
+ protocol=https
+ host=bar
+ --
+ protocol=https
+ host=bar
+ username=askpass-username
+ password=askpass-password
+ --
+ askpass: Username for '\''https://bar'\'':
+ askpass: Password for '\''https://askpass-username@bar'\'':
+ EOF
+'
+
+test_expect_success 'match multiple configured helpers' '
+ test_config credential.helper "verbatim \"\" \"\"" &&
+ test_config credential.https://example.com.helper "$HELPER" &&
+ check fill <<-\EOF
+ protocol=https
+ host=example.com
+ path=repo.git
+ --
+ protocol=https
+ host=example.com
+ username=foo
+ password=bar
+ --
+ verbatim: get
+ verbatim: protocol=https
+ verbatim: host=example.com
+ EOF
+'
+
+test_expect_success 'match multiple configured helpers with URLs' '
+ test_config credential.https://example.com/repo.git.helper "verbatim \"\" \"\"" &&
+ test_config credential.https://example.com.helper "$HELPER" &&
+ check fill <<-\EOF
+ protocol=https
+ host=example.com
+ path=repo.git
+ --
+ protocol=https
+ host=example.com
+ username=foo
+ password=bar
+ --
+ verbatim: get
+ verbatim: protocol=https
+ verbatim: host=example.com
+ EOF
+'
+
+test_expect_success 'match percent-encoded values' '
+ test_config credential.https://example.com/%2566.git.helper "$HELPER" &&
+ check fill <<-\EOF
+ url=https://example.com/%2566.git
+ --
+ protocol=https
+ host=example.com
+ username=foo
+ password=bar
+ --
+ EOF
+'
+
+test_expect_success 'match percent-encoded UTF-8 values in path' '
+ test_config credential.https://example.com.useHttpPath true &&
+ test_config credential.https://example.com/perú.git.helper "$HELPER" &&
+ check fill <<-\EOF
+ url=https://example.com/per%C3%BA.git
+ --
+ protocol=https
+ host=example.com
+ path=perú.git
+ username=foo
+ password=bar
+ --
+ EOF
+'
+
+test_expect_success 'match percent-encoded values in username' '
+ test_config credential.https://user%2fname@example.com/foo/bar.git.helper "$HELPER" &&
+ check fill <<-\EOF
+ url=https://user%2fname@example.com/foo/bar.git
+ --
+ protocol=https
+ host=example.com
+ username=foo
+ password=bar
+ --
+ EOF
+'
+
+test_expect_success 'fetch with multiple path components' '
+ test_unconfig credential.helper &&
+ test_config credential.https://example.com/foo/repo.git.helper "verbatim foo bar" &&
+ check fill <<-\EOF
+ url=https://example.com/foo/repo.git
+ --
+ protocol=https
+ host=example.com
+ username=foo
+ password=bar
+ --
+ verbatim: get
+ verbatim: protocol=https
+ verbatim: host=example.com
+ EOF
+'
+
+test_expect_success 'pull username from config' '
+ test_config credential.https://example.com.username foo &&
+ check fill <<-\EOF
+ protocol=https
+ host=example.com
+ --
+ protocol=https
+ host=example.com
+ username=foo
+ password=askpass-password
+ --
+ askpass: Password for '\''https://foo@example.com'\'':
+ EOF
+'
+
+test_expect_success 'honors username from URL over helper (URL)' '
+ test_config credential.https://example.com.username bob &&
+ test_config credential.https://example.com.helper "verbatim \"\" bar" &&
+ check fill <<-\EOF
+ url=https://alice@example.com
+ --
+ protocol=https
+ host=example.com
+ username=alice
+ password=bar
+ --
+ verbatim: get
+ verbatim: protocol=https
+ verbatim: host=example.com
+ verbatim: username=alice
+ EOF
+'
+
+test_expect_success 'honors username from URL over helper (components)' '
+ test_config credential.https://example.com.username bob &&
+ test_config credential.https://example.com.helper "verbatim \"\" bar" &&
+ check fill <<-\EOF
+ protocol=https
+ host=example.com
+ username=alice
+ --
+ protocol=https
+ host=example.com
+ username=alice
+ password=bar
+ --
+ verbatim: get
+ verbatim: protocol=https
+ verbatim: host=example.com
+ verbatim: username=alice
+ EOF
+'
+
+test_expect_success 'last matching username wins' '
+ test_config credential.https://example.com/path.git.username bob &&
+ test_config credential.https://example.com.username alice &&
+ test_config credential.https://example.com.helper "verbatim \"\" bar" &&
+ check fill <<-\EOF
+ url=https://example.com/path.git
+ --
+ protocol=https
+ host=example.com
+ username=alice
+ password=bar
+ --
+ verbatim: get
+ verbatim: protocol=https
+ verbatim: host=example.com
+ verbatim: username=alice
+ EOF
+'
+
+test_expect_success 'http paths can be part of context' '
+ check fill "verbatim foo bar" <<-\EOF &&
+ protocol=https
+ host=example.com
+ path=foo.git
+ --
+ protocol=https
+ host=example.com
+ username=foo
+ password=bar
+ --
+ verbatim: get
+ verbatim: protocol=https
+ verbatim: host=example.com
+ EOF
+ test_config credential.https://example.com.useHttpPath true &&
+ check fill "verbatim foo bar" <<-\EOF
+ protocol=https
+ host=example.com
+ path=foo.git
+ --
+ protocol=https
+ host=example.com
+ path=foo.git
+ username=foo
+ password=bar
+ --
+ verbatim: get
+ verbatim: protocol=https
+ verbatim: host=example.com
+ verbatim: path=foo.git
+ EOF
+'
+
+test_expect_success 'context uses urlmatch' '
+ test_config "credential.https://*.org.useHttpPath" true &&
+ check fill "verbatim foo bar" <<-\EOF
+ protocol=https
+ host=example.org
+ path=foo.git
+ --
+ protocol=https
+ host=example.org
+ path=foo.git
+ username=foo
+ password=bar
+ --
+ verbatim: get
+ verbatim: protocol=https
+ verbatim: host=example.org
+ verbatim: path=foo.git
+ EOF
+'
+
+test_expect_success 'helpers can abort the process' '
+ test_must_fail git \
+ -c credential.helper=quit \
+ -c credential.helper="verbatim foo bar" \
+ credential fill >stdout 2>stderr <<-\EOF &&
+ protocol=http
+ host=example.com
+ EOF
+ test_must_be_empty stdout &&
+ cat >expect <<-\EOF &&
+ quit: get
+ quit: protocol=http
+ quit: host=example.com
+ fatal: credential helper '\''quit'\'' told us to quit
+ EOF
+ test_cmp expect stderr
+'
+
+test_expect_success 'empty helper spec resets helper list' '
+ test_config credential.helper "verbatim file file" &&
+ check fill "" "verbatim cmdline cmdline" <<-\EOF
+ protocol=http
+ host=example.com
+ --
+ protocol=http
+ host=example.com
+ username=cmdline
+ password=cmdline
+ --
+ verbatim: get
+ verbatim: protocol=http
+ verbatim: host=example.com
+ EOF
+'
+
+test_expect_success 'url parser rejects embedded newlines' '
+ test_must_fail git credential fill 2>stderr <<-\EOF &&
+ url=https://one.example.com?%0ahost=two.example.com/
+ EOF
+ cat >expect <<-\EOF &&
+ warning: url contains a newline in its path component: https://one.example.com?%0ahost=two.example.com/
+ fatal: credential url cannot be parsed: https://one.example.com?%0ahost=two.example.com/
+ EOF
+ test_cmp expect stderr
+'
+
+test_expect_success 'host-less URLs are parsed as empty host' '
+ check fill "verbatim foo bar" <<-\EOF
+ url=cert:///path/to/cert.pem
+ --
+ protocol=cert
+ host=
+ path=path/to/cert.pem
+ username=foo
+ password=bar
+ --
+ verbatim: get
+ verbatim: protocol=cert
+ verbatim: host=
+ verbatim: path=path/to/cert.pem
+ EOF
+'
+
+test_expect_success 'credential system refuses to work with missing host' '
+ test_must_fail git credential fill 2>stderr <<-\EOF &&
+ protocol=http
+ EOF
+ cat >expect <<-\EOF &&
+ fatal: refusing to work with credential missing host field
+ EOF
+ test_cmp expect stderr
+'
+
+test_expect_success 'credential system refuses to work with missing protocol' '
+ test_must_fail git credential fill 2>stderr <<-\EOF &&
+ host=example.com
+ EOF
+ cat >expect <<-\EOF &&
+ fatal: refusing to work with credential missing protocol field
+ EOF
+ test_cmp expect stderr
+'
+
+# usage: check_host_and_path <url> <expected-host> <expected-path>
+check_host_and_path () {
+ # we always parse the path component, but we need this to make sure it
+ # is passed to the helper
+ test_config credential.useHTTPPath true &&
+ check fill "verbatim user pass" <<-EOF
+ url=$1
+ --
+ protocol=https
+ host=$2
+ path=$3
+ username=user
+ password=pass
+ --
+ verbatim: get
+ verbatim: protocol=https
+ verbatim: host=$2
+ verbatim: path=$3
+ EOF
+}
+
+test_expect_success 'url parser handles bare query marker' '
+ check_host_and_path https://example.com?foo.git example.com ?foo.git
+'
+
+test_expect_success 'url parser handles bare fragment marker' '
+ check_host_and_path https://example.com#foo.git example.com "#foo.git"
+'
+
+test_expect_success 'url parser not confused by encoded markers' '
+ check_host_and_path https://example.com%23%3f%2f/foo.git \
+ "example.com#?/" foo.git
+'
+
+test_expect_success 'credential config with partial URLs' '
+ echo "echo password=yep" | write_script git-credential-yep &&
+ test_write_lines url=https://user@example.com/repo.git >stdin &&
+ for partial in \
+ example.com \
+ user@example.com \
+ https:// \
+ https://example.com \
+ https://example.com/ \
+ https://user@example.com \
+ https://user@example.com/ \
+ https://example.com/repo.git \
+ https://user@example.com/repo.git \
+ /repo.git
+ do
+ git -c credential.$partial.helper=yep \
+ credential fill <stdin >stdout &&
+ grep yep stdout ||
+ return 1
+ done &&
+
+ for partial in \
+ dont.use.this \
+ http:// \
+ /repo
+ do
+ git -c credential.$partial.helper=yep \
+ credential fill <stdin >stdout &&
+ ! grep yep stdout ||
+ return 1
+ done &&
+
+ git -c credential.$partial.helper=yep \
+ -c credential.with%0anewline.username=uh-oh \
+ credential fill <stdin 2>stderr &&
+ test_grep "skipping credential lookup for key" stderr
+'
+
+test_done
diff --git a/t/t0301-credential-cache.sh b/t/t0301-credential-cache.sh
new file mode 100755
index 0000000..8300faa
--- /dev/null
+++ b/t/t0301-credential-cache.sh
@@ -0,0 +1,129 @@
+#!/bin/sh
+
+test_description='credential-cache tests'
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-credential.sh
+
+test -z "$NO_UNIX_SOCKETS" || {
+ skip_all='skipping credential-cache tests, unix sockets not available'
+ test_done
+}
+
+uname_s=$(uname -s)
+case $uname_s in
+*MINGW*)
+ test_path_is_socket () {
+ # `test -S` cannot detect Win10's Unix sockets
+ test_path_exists "$1"
+ }
+ ;;
+*)
+ test_path_is_socket () {
+ test -S "$1"
+ }
+ ;;
+esac
+
+# don't leave a stale daemon running
+test_atexit 'git credential-cache exit'
+
+# test that the daemon works with no special setup
+helper_test cache
+helper_test_password_expiry_utc cache
+helper_test_oauth_refresh_token cache
+
+test_expect_success 'socket defaults to ~/.cache/git/credential/socket' '
+ test_when_finished "
+ git credential-cache exit &&
+ rmdir -p .cache/git/credential/
+ " &&
+ test_path_is_missing "$HOME/.git-credential-cache" &&
+ test_path_is_socket "$HOME/.cache/git/credential/socket"
+'
+
+XDG_CACHE_HOME="$HOME/xdg"
+export XDG_CACHE_HOME
+# test behavior when XDG_CACHE_HOME is set
+helper_test cache
+
+test_expect_success "use custom XDG_CACHE_HOME if set and default sockets are not created" '
+ test_when_finished "git credential-cache exit" &&
+ test_path_is_socket "$XDG_CACHE_HOME/git/credential/socket" &&
+ test_path_is_missing "$HOME/.git-credential-cache/socket" &&
+ test_path_is_missing "$HOME/.cache/git/credential/socket"
+'
+unset XDG_CACHE_HOME
+
+test_expect_success 'credential-cache --socket option overrides default location' '
+ test_when_finished "
+ git credential-cache exit --socket \"\$HOME/dir/socket\" &&
+ rmdir \"\$HOME/dir\"
+ " &&
+ check approve "cache --socket \"\$HOME/dir/socket\"" <<-\EOF &&
+ protocol=https
+ host=example.com
+ username=store-user
+ password=store-pass
+ EOF
+ test_path_is_socket "$HOME/dir/socket"
+'
+
+test_expect_success "use custom XDG_CACHE_HOME even if xdg socket exists" '
+ test_when_finished "
+ git credential-cache exit &&
+ sane_unset XDG_CACHE_HOME
+ " &&
+ check approve cache <<-\EOF &&
+ protocol=https
+ host=example.com
+ username=store-user
+ password=store-pass
+ EOF
+ test_path_is_socket "$HOME/.cache/git/credential/socket" &&
+ XDG_CACHE_HOME="$HOME/xdg" &&
+ export XDG_CACHE_HOME &&
+ check approve cache <<-\EOF &&
+ protocol=https
+ host=example.com
+ username=store-user
+ password=store-pass
+ EOF
+ test_path_is_socket "$XDG_CACHE_HOME/git/credential/socket"
+'
+
+test_expect_success 'use user socket if user directory exists' '
+ test_when_finished "
+ git credential-cache exit &&
+ rmdir \"\$HOME/.git-credential-cache/\"
+ " &&
+ mkdir -p "$HOME/.git-credential-cache/" &&
+ chmod 700 "$HOME/.git-credential-cache/" &&
+ check approve cache <<-\EOF &&
+ protocol=https
+ host=example.com
+ username=store-user
+ password=store-pass
+ EOF
+ test_path_is_socket "$HOME/.git-credential-cache/socket"
+'
+
+test_expect_success SYMLINKS 'use user socket if user directory is a symlink to a directory' '
+ test_when_finished "
+ git credential-cache exit &&
+ rmdir \"\$HOME/dir/\" &&
+ rm \"\$HOME/.git-credential-cache\"
+ " &&
+ mkdir -p -m 700 "$HOME/dir/" &&
+ ln -s "$HOME/dir" "$HOME/.git-credential-cache" &&
+ check approve cache <<-\EOF &&
+ protocol=https
+ host=example.com
+ username=store-user
+ password=store-pass
+ EOF
+ test_path_is_socket "$HOME/.git-credential-cache/socket"
+'
+
+helper_test_timeout cache --timeout=1
+
+test_done
diff --git a/t/t0302-credential-store.sh b/t/t0302-credential-store.sh
new file mode 100755
index 0000000..716bf1a
--- /dev/null
+++ b/t/t0302-credential-store.sh
@@ -0,0 +1,212 @@
+#!/bin/sh
+
+test_description='credential-store tests'
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-credential.sh
+
+helper_test store
+
+test_expect_success 'when xdg file does not exist, xdg file not created' '
+ test_path_is_missing "$HOME/.config/git/credentials" &&
+ test -s "$HOME/.git-credentials"
+'
+
+test_expect_success 'setup xdg file' '
+ rm -f "$HOME/.git-credentials" &&
+ mkdir -p "$HOME/.config/git" &&
+ >"$HOME/.config/git/credentials"
+'
+
+helper_test store
+
+test_expect_success 'when xdg file exists, home file not created' '
+ test -s "$HOME/.config/git/credentials" &&
+ test_path_is_missing "$HOME/.git-credentials"
+'
+
+test_expect_success 'setup custom xdg file' '
+ rm -f "$HOME/.git-credentials" &&
+ rm -f "$HOME/.config/git/credentials" &&
+ mkdir -p "$HOME/xdg/git" &&
+ >"$HOME/xdg/git/credentials"
+'
+
+XDG_CONFIG_HOME="$HOME/xdg"
+export XDG_CONFIG_HOME
+helper_test store
+unset XDG_CONFIG_HOME
+
+test_expect_success 'if custom xdg file exists, home and xdg files not created' '
+ test_when_finished "rm -f \"$HOME/xdg/git/credentials\"" &&
+ test -s "$HOME/xdg/git/credentials" &&
+ test_path_is_missing "$HOME/.git-credentials" &&
+ test_path_is_missing "$HOME/.config/git/credentials"
+'
+
+test_expect_success 'get: use home file if both home and xdg files have matches' '
+ echo "https://home-user:home-pass@example.com" >"$HOME/.git-credentials" &&
+ mkdir -p "$HOME/.config/git" &&
+ echo "https://xdg-user:xdg-pass@example.com" >"$HOME/.config/git/credentials" &&
+ check fill store <<-\EOF
+ protocol=https
+ host=example.com
+ --
+ protocol=https
+ host=example.com
+ username=home-user
+ password=home-pass
+ --
+ EOF
+'
+
+test_expect_success 'get: use xdg file if home file has no matches' '
+ >"$HOME/.git-credentials" &&
+ mkdir -p "$HOME/.config/git" &&
+ echo "https://xdg-user:xdg-pass@example.com" >"$HOME/.config/git/credentials" &&
+ check fill store <<-\EOF
+ protocol=https
+ host=example.com
+ --
+ protocol=https
+ host=example.com
+ username=xdg-user
+ password=xdg-pass
+ --
+ EOF
+'
+
+test_expect_success POSIXPERM,SANITY 'get: use xdg file if home file is unreadable' '
+ echo "https://home-user:home-pass@example.com" >"$HOME/.git-credentials" &&
+ chmod -r "$HOME/.git-credentials" &&
+ mkdir -p "$HOME/.config/git" &&
+ echo "https://xdg-user:xdg-pass@example.com" >"$HOME/.config/git/credentials" &&
+ check fill store <<-\EOF
+ protocol=https
+ host=example.com
+ --
+ protocol=https
+ host=example.com
+ username=xdg-user
+ password=xdg-pass
+ --
+ EOF
+'
+
+test_expect_success 'store: if both xdg and home files exist, only store in home file' '
+ >"$HOME/.git-credentials" &&
+ mkdir -p "$HOME/.config/git" &&
+ >"$HOME/.config/git/credentials" &&
+ check approve store <<-\EOF &&
+ protocol=https
+ host=example.com
+ username=store-user
+ password=store-pass
+ EOF
+ echo "https://store-user:store-pass@example.com" >expected &&
+ test_cmp expected "$HOME/.git-credentials" &&
+ test_must_be_empty "$HOME/.config/git/credentials"
+'
+
+test_expect_success 'erase: erase matching credentials from both xdg and home files' '
+ echo "https://home-user:home-pass@example.com" >"$HOME/.git-credentials" &&
+ mkdir -p "$HOME/.config/git" &&
+ echo "https://xdg-user:xdg-pass@example.com" >"$HOME/.config/git/credentials" &&
+ check reject store <<-\EOF &&
+ protocol=https
+ host=example.com
+ EOF
+ test_must_be_empty "$HOME/.git-credentials" &&
+ test_must_be_empty "$HOME/.config/git/credentials"
+'
+
+invalid_credential_test() {
+ test_expect_success "get: ignore credentials without $1 as invalid" '
+ echo "$2" >"$HOME/.git-credentials" &&
+ check fill store <<-\EOF
+ protocol=https
+ host=example.com
+ --
+ protocol=https
+ host=example.com
+ username=askpass-username
+ password=askpass-password
+ --
+ askpass: Username for '\''https://example.com'\'':
+ askpass: Password for '\''https://askpass-username@example.com'\'':
+ --
+ EOF
+ '
+}
+
+invalid_credential_test "scheme" ://user:pass@example.com
+invalid_credential_test "valid host/path" https://user:pass@
+invalid_credential_test "username/password" https://pass@example.com
+
+test_expect_success 'get: credentials with DOS line endings are invalid' '
+ printf "https://user:pass@example.com\r\n" >"$HOME/.git-credentials" &&
+ check fill store <<-\EOF
+ protocol=https
+ host=example.com
+ --
+ protocol=https
+ host=example.com
+ username=askpass-username
+ password=askpass-password
+ --
+ askpass: Username for '\''https://example.com'\'':
+ askpass: Password for '\''https://askpass-username@example.com'\'':
+ --
+ EOF
+'
+
+test_expect_success 'get: credentials with path and DOS line endings are valid' '
+ printf "https://user:pass@example.com/repo.git\r\n" >"$HOME/.git-credentials" &&
+ check fill store <<-\EOF
+ url=https://example.com/repo.git
+ --
+ protocol=https
+ host=example.com
+ username=user
+ password=pass
+ --
+ EOF
+'
+
+test_expect_success 'get: credentials with DOS line endings are invalid if path is relevant' '
+ printf "https://user:pass@example.com/repo.git\r\n" >"$HOME/.git-credentials" &&
+ test_config credential.useHttpPath true &&
+ check fill store <<-\EOF
+ url=https://example.com/repo.git
+ --
+ protocol=https
+ host=example.com
+ path=repo.git
+ username=askpass-username
+ password=askpass-password
+ --
+ askpass: Username for '\''https://example.com/repo.git'\'':
+ askpass: Password for '\''https://askpass-username@example.com/repo.git'\'':
+ --
+ EOF
+'
+
+test_expect_success 'get: store file can contain empty/bogus lines' '
+ echo "" >"$HOME/.git-credentials" &&
+ q_to_tab <<-\CREDENTIAL >>"$HOME/.git-credentials" &&
+ #comment
+ Q
+ https://user:pass@example.com
+ CREDENTIAL
+ check fill store <<-\EOF
+ protocol=https
+ host=example.com
+ --
+ protocol=https
+ host=example.com
+ username=user
+ password=pass
+ --
+ EOF
+'
+
+test_done
diff --git a/t/t0303-credential-external.sh b/t/t0303-credential-external.sh
new file mode 100755
index 0000000..095574b
--- /dev/null
+++ b/t/t0303-credential-external.sh
@@ -0,0 +1,62 @@
+#!/bin/sh
+
+test_description='external credential helper tests
+
+This is a tool for authors of external helper tools to sanity-check
+their helpers. If you have written the "git-credential-foo" helper,
+you check it with:
+
+ make GIT_TEST_CREDENTIAL_HELPER=foo t0303-credential-external.sh
+
+This assumes that your helper is capable of both storing and
+retrieving credentials (some helpers may be read-only, and they will
+fail these tests).
+
+Please note that the individual tests do not verify all of the
+preconditions themselves, but rather build on each other. A failing
+test means that tests later in the sequence can return false "OK"
+results.
+
+If your helper supports time-based expiration with a configurable
+timeout, you can test that feature with:
+
+ make GIT_TEST_CREDENTIAL_HELPER=foo \
+ GIT_TEST_CREDENTIAL_HELPER_TIMEOUT="foo --timeout=1" \
+ t0303-credential-external.sh
+
+If your helper requires additional setup before the tests are started,
+you can set GIT_TEST_CREDENTIAL_HELPER_SETUP to a sequence of shell
+commands.
+'
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-credential.sh
+
+if test -z "$GIT_TEST_CREDENTIAL_HELPER"; then
+ skip_all="used to test external credential helpers"
+ test_done
+fi
+
+test -z "$GIT_TEST_CREDENTIAL_HELPER_SETUP" ||
+ eval "$GIT_TEST_CREDENTIAL_HELPER_SETUP"
+
+# clean before the test in case there is cruft left
+# over from a previous run that would impact results
+helper_test_clean "$GIT_TEST_CREDENTIAL_HELPER"
+
+helper_test "$GIT_TEST_CREDENTIAL_HELPER"
+helper_test_password_expiry_utc "$GIT_TEST_CREDENTIAL_HELPER"
+helper_test_oauth_refresh_token "$GIT_TEST_CREDENTIAL_HELPER"
+
+if test -z "$GIT_TEST_CREDENTIAL_HELPER_TIMEOUT"; then
+ say "# skipping timeout tests (GIT_TEST_CREDENTIAL_HELPER_TIMEOUT not set)"
+else
+ helper_test_timeout "$GIT_TEST_CREDENTIAL_HELPER_TIMEOUT"
+fi
+
+# clean afterwards so that we are good citizens
+# and don't leave cruft in the helper's storage, which
+# might be long-term system storage
+helper_test_clean "$GIT_TEST_CREDENTIAL_HELPER"
+
+test_done
diff --git a/t/t0410-partial-clone.sh b/t/t0410-partial-clone.sh
new file mode 100755
index 0000000..5b7bee8
--- /dev/null
+++ b/t/t0410-partial-clone.sh
@@ -0,0 +1,707 @@
+#!/bin/sh
+
+test_description='partial clone'
+
+. ./test-lib.sh
+
+# missing promisor objects cause repacks which write bitmaps to fail
+GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0
+# When enabled, some commands will write commit-graphs. This causes fsck
+# to fail when delete_object() is called because fsck will attempt to
+# verify the out-of-sync commit graph.
+GIT_TEST_COMMIT_GRAPH=0
+
+delete_object () {
+ rm $1/.git/objects/$(echo $2 | sed -e 's|^..|&/|')
+}
+
+pack_as_from_promisor () {
+ HASH=$(git -C repo pack-objects .git/objects/pack/pack) &&
+ >repo/.git/objects/pack/pack-$HASH.promisor &&
+ echo $HASH
+}
+
+promise_and_delete () {
+ HASH=$(git -C repo rev-parse "$1") &&
+ git -C repo tag -a -m message my_annotated_tag "$HASH" &&
+ git -C repo rev-parse my_annotated_tag | pack_as_from_promisor &&
+ # tag -d prints a message to stdout, so redirect it
+ git -C repo tag -d my_annotated_tag >/dev/null &&
+ delete_object repo "$HASH"
+}
+
+test_expect_success 'extensions.partialclone without filter' '
+ test_create_repo server &&
+ git clone --filter="blob:none" "file://$(pwd)/server" client &&
+ git -C client config --unset remote.origin.partialclonefilter &&
+ git -C client fetch origin
+'
+
+test_expect_success 'convert shallow clone to partial clone' '
+ rm -fr server client &&
+ test_create_repo server &&
+ test_commit -C server my_commit 1 &&
+ test_commit -C server my_commit2 1 &&
+ git clone --depth=1 "file://$(pwd)/server" client &&
+ git -C client fetch --unshallow --filter="blob:none" &&
+ test_cmp_config -C client true remote.origin.promisor &&
+ test_cmp_config -C client blob:none remote.origin.partialclonefilter &&
+ test_cmp_config -C client 1 core.repositoryformatversion
+'
+
+test_expect_success SHA1 'convert to partial clone with noop extension' '
+ rm -fr server client &&
+ test_create_repo server &&
+ test_commit -C server my_commit 1 &&
+ test_commit -C server my_commit2 1 &&
+ git clone --depth=1 "file://$(pwd)/server" client &&
+ test_cmp_config -C client 0 core.repositoryformatversion &&
+ git -C client config extensions.noop true &&
+ git -C client fetch --unshallow --filter="blob:none"
+'
+
+test_expect_success SHA1 'converting to partial clone fails with unrecognized extension' '
+ rm -fr server client &&
+ test_create_repo server &&
+ test_commit -C server my_commit 1 &&
+ test_commit -C server my_commit2 1 &&
+ git clone --depth=1 "file://$(pwd)/server" client &&
+ test_cmp_config -C client 0 core.repositoryformatversion &&
+ git -C client config extensions.nonsense true &&
+ test_must_fail git -C client fetch --unshallow --filter="blob:none"
+'
+
+test_expect_success 'missing reflog object, but promised by a commit, passes fsck' '
+ rm -rf repo &&
+ test_create_repo repo &&
+ test_commit -C repo my_commit &&
+
+ A=$(git -C repo commit-tree -m a HEAD^{tree}) &&
+ C=$(git -C repo commit-tree -m c -p $A HEAD^{tree}) &&
+
+ # Reference $A only from reflog, and delete it
+ git -C repo branch my_branch "$A" &&
+ git -C repo branch -f my_branch my_commit &&
+ delete_object repo "$A" &&
+
+ # State that we got $C, which refers to $A, from promisor
+ printf "$C\n" | pack_as_from_promisor &&
+
+ # Normally, it fails
+ test_must_fail git -C repo fsck &&
+
+ # But with the extension, it succeeds
+ git -C repo config core.repositoryformatversion 1 &&
+ git -C repo config extensions.partialclone "arbitrary string" &&
+ git -C repo fsck
+'
+
+test_expect_success 'missing reflog object, but promised by a tag, passes fsck' '
+ rm -rf repo &&
+ test_create_repo repo &&
+ test_commit -C repo my_commit &&
+
+ A=$(git -C repo commit-tree -m a HEAD^{tree}) &&
+ git -C repo tag -a -m d my_tag_name $A &&
+ T=$(git -C repo rev-parse my_tag_name) &&
+ git -C repo tag -d my_tag_name &&
+
+ # Reference $A only from reflog, and delete it
+ git -C repo branch my_branch "$A" &&
+ git -C repo branch -f my_branch my_commit &&
+ delete_object repo "$A" &&
+
+ # State that we got $T, which refers to $A, from promisor
+ printf "$T\n" | pack_as_from_promisor &&
+
+ git -C repo config core.repositoryformatversion 1 &&
+ git -C repo config extensions.partialclone "arbitrary string" &&
+ git -C repo fsck
+'
+
+test_expect_success 'missing reflog object alone fails fsck, even with extension set' '
+ rm -rf repo &&
+ test_create_repo repo &&
+ test_commit -C repo my_commit &&
+
+ A=$(git -C repo commit-tree -m a HEAD^{tree}) &&
+ B=$(git -C repo commit-tree -m b HEAD^{tree}) &&
+
+ # Reference $A only from reflog, and delete it
+ git -C repo branch my_branch "$A" &&
+ git -C repo branch -f my_branch my_commit &&
+ delete_object repo "$A" &&
+
+ git -C repo config core.repositoryformatversion 1 &&
+ git -C repo config extensions.partialclone "arbitrary string" &&
+ test_must_fail git -C repo fsck
+'
+
+test_expect_success 'missing ref object, but promised, passes fsck' '
+ rm -rf repo &&
+ test_create_repo repo &&
+ test_commit -C repo my_commit &&
+
+ A=$(git -C repo commit-tree -m a HEAD^{tree}) &&
+
+ # Reference $A only from ref
+ git -C repo branch my_branch "$A" &&
+ promise_and_delete "$A" &&
+
+ git -C repo config core.repositoryformatversion 1 &&
+ git -C repo config extensions.partialclone "arbitrary string" &&
+ git -C repo fsck
+'
+
+test_expect_success 'missing object, but promised, passes fsck' '
+ rm -rf repo &&
+ test_create_repo repo &&
+ test_commit -C repo 1 &&
+ test_commit -C repo 2 &&
+ test_commit -C repo 3 &&
+ git -C repo tag -a annotated_tag -m "annotated tag" &&
+
+ C=$(git -C repo rev-parse 1) &&
+ T=$(git -C repo rev-parse 2^{tree}) &&
+ B=$(git hash-object repo/3.t) &&
+ AT=$(git -C repo rev-parse annotated_tag) &&
+
+ promise_and_delete "$C" &&
+ promise_and_delete "$T" &&
+ promise_and_delete "$B" &&
+ promise_and_delete "$AT" &&
+
+ git -C repo config core.repositoryformatversion 1 &&
+ git -C repo config extensions.partialclone "arbitrary string" &&
+ git -C repo fsck
+'
+
+test_expect_success 'missing CLI object, but promised, passes fsck' '
+ rm -rf repo &&
+ test_create_repo repo &&
+ test_commit -C repo my_commit &&
+
+ A=$(git -C repo commit-tree -m a HEAD^{tree}) &&
+ promise_and_delete "$A" &&
+
+ git -C repo config core.repositoryformatversion 1 &&
+ git -C repo config extensions.partialclone "arbitrary string" &&
+ git -C repo fsck "$A"
+'
+
+test_expect_success 'fetching of missing objects' '
+ rm -rf repo err &&
+ test_create_repo server &&
+ test_commit -C server foo &&
+ git -C server repack -a -d --write-bitmap-index &&
+
+ git clone "file://$(pwd)/server" repo &&
+ HASH=$(git -C repo rev-parse foo) &&
+ rm -rf repo/.git/objects/* &&
+
+ git -C repo config core.repositoryformatversion 1 &&
+ git -C repo config extensions.partialclone "origin" &&
+ git -C repo cat-file -p "$HASH" 2>err &&
+
+ # Ensure that no spurious FETCH_HEAD messages are written
+ ! grep FETCH_HEAD err &&
+
+ # Ensure that the .promisor file is written, and check that its
+ # associated packfile contains the object
+ ls repo/.git/objects/pack/pack-*.promisor >promisorlist &&
+ test_line_count = 1 promisorlist &&
+ IDX=$(sed "s/promisor$/idx/" promisorlist) &&
+ git verify-pack --verbose "$IDX" >out &&
+ grep "$HASH" out
+'
+
+test_expect_success 'fetching of a promised object that promisor remote no longer has' '
+ rm -f err &&
+ test_create_repo unreliable-server &&
+ git -C unreliable-server config uploadpack.allowanysha1inwant 1 &&
+ git -C unreliable-server config uploadpack.allowfilter 1 &&
+ test_commit -C unreliable-server foo &&
+
+ git clone --filter=blob:none --no-checkout "file://$(pwd)/unreliable-server" unreliable-client &&
+
+ rm -rf unreliable-server/.git/objects/* &&
+ test_must_fail git -C unreliable-client checkout HEAD 2>err &&
+ grep "could not fetch.*from promisor remote" err
+'
+
+test_expect_success 'fetching of missing objects works with ref-in-want enabled' '
+ # ref-in-want requires protocol version 2
+ git -C server config protocol.version 2 &&
+ git -C server config uploadpack.allowrefinwant 1 &&
+ git -C repo config protocol.version 2 &&
+
+ rm -rf repo/.git/objects/* &&
+ rm -f trace &&
+ GIT_TRACE_PACKET="$(pwd)/trace" git -C repo cat-file -p "$HASH" &&
+ grep "fetch< fetch=.*ref-in-want" trace
+'
+
+test_expect_success 'fetching of missing objects from another promisor remote' '
+ git clone "file://$(pwd)/server" server2 &&
+ test_commit -C server2 bar &&
+ git -C server2 repack -a -d --write-bitmap-index &&
+ HASH2=$(git -C server2 rev-parse bar) &&
+
+ git -C repo remote add server2 "file://$(pwd)/server2" &&
+ git -C repo config remote.server2.promisor true &&
+ git -C repo cat-file -p "$HASH2" &&
+
+ git -C repo fetch server2 &&
+ rm -rf repo/.git/objects/* &&
+ git -C repo cat-file -p "$HASH2" &&
+
+ # Ensure that the .promisor file is written, and check that its
+ # associated packfile contains the object
+ ls repo/.git/objects/pack/pack-*.promisor >promisorlist &&
+ test_line_count = 1 promisorlist &&
+ IDX=$(sed "s/promisor$/idx/" promisorlist) &&
+ git verify-pack --verbose "$IDX" >out &&
+ grep "$HASH2" out
+'
+
+test_expect_success 'fetching of missing objects configures a promisor remote' '
+ git clone "file://$(pwd)/server" server3 &&
+ test_commit -C server3 baz &&
+ git -C server3 repack -a -d --write-bitmap-index &&
+ HASH3=$(git -C server3 rev-parse baz) &&
+ git -C server3 config uploadpack.allowfilter 1 &&
+
+ rm repo/.git/objects/pack/pack-*.promisor &&
+
+ git -C repo remote add server3 "file://$(pwd)/server3" &&
+ git -C repo fetch --filter="blob:none" server3 $HASH3 &&
+
+ test_cmp_config -C repo true remote.server3.promisor &&
+
+ # Ensure that the .promisor file is written, and check that its
+ # associated packfile contains the object
+ ls repo/.git/objects/pack/pack-*.promisor >promisorlist &&
+ test_line_count = 1 promisorlist &&
+ IDX=$(sed "s/promisor$/idx/" promisorlist) &&
+ git verify-pack --verbose "$IDX" >out &&
+ grep "$HASH3" out
+'
+
+test_expect_success 'fetching of missing blobs works' '
+ rm -rf server server2 repo &&
+ rm -rf server server3 repo &&
+ test_create_repo server &&
+ test_commit -C server foo &&
+ git -C server repack -a -d --write-bitmap-index &&
+
+ git clone "file://$(pwd)/server" repo &&
+ git hash-object repo/foo.t >blobhash &&
+ rm -rf repo/.git/objects/* &&
+
+ git -C server config uploadpack.allowanysha1inwant 1 &&
+ git -C server config uploadpack.allowfilter 1 &&
+ git -C repo config core.repositoryformatversion 1 &&
+ git -C repo config extensions.partialclone "origin" &&
+
+ git -C repo cat-file -p $(cat blobhash)
+'
+
+test_expect_success 'fetching of missing trees does not fetch blobs' '
+ rm -rf server repo &&
+ test_create_repo server &&
+ test_commit -C server foo &&
+ git -C server repack -a -d --write-bitmap-index &&
+
+ git clone "file://$(pwd)/server" repo &&
+ git -C repo rev-parse foo^{tree} >treehash &&
+ git hash-object repo/foo.t >blobhash &&
+ rm -rf repo/.git/objects/* &&
+
+ git -C server config uploadpack.allowanysha1inwant 1 &&
+ git -C server config uploadpack.allowfilter 1 &&
+ git -C repo config core.repositoryformatversion 1 &&
+ git -C repo config extensions.partialclone "origin" &&
+ git -C repo cat-file -p $(cat treehash) &&
+
+ # Ensure that the tree, but not the blob, is fetched
+ git -C repo rev-list --objects --missing=print $(cat treehash) >objects &&
+ grep "^$(cat treehash)" objects &&
+ grep "^[?]$(cat blobhash)" objects
+'
+
+test_expect_success 'rev-list stops traversal at missing and promised commit' '
+ rm -rf repo &&
+ test_create_repo repo &&
+ test_commit -C repo foo &&
+ test_commit -C repo bar &&
+
+ FOO=$(git -C repo rev-parse foo) &&
+ promise_and_delete "$FOO" &&
+
+ git -C repo config core.repositoryformatversion 1 &&
+ git -C repo config extensions.partialclone "arbitrary string" &&
+ git -C repo rev-list --exclude-promisor-objects --objects bar >out &&
+ grep $(git -C repo rev-parse bar) out &&
+ ! grep $FOO out
+'
+
+test_expect_success 'missing tree objects with --missing=allow-promisor and --exclude-promisor-objects' '
+ rm -rf repo &&
+ test_create_repo repo &&
+ test_commit -C repo foo &&
+ test_commit -C repo bar &&
+ test_commit -C repo baz &&
+
+ promise_and_delete $(git -C repo rev-parse bar^{tree}) &&
+ promise_and_delete $(git -C repo rev-parse foo^{tree}) &&
+
+ git -C repo config core.repositoryformatversion 1 &&
+ git -C repo config extensions.partialclone "arbitrary string" &&
+
+ git -C repo rev-list --missing=allow-promisor --objects HEAD >objs 2>rev_list_err &&
+ test_must_be_empty rev_list_err &&
+ # 3 commits, 3 blobs, and 1 tree
+ test_line_count = 7 objs &&
+
+ # Do the same for --exclude-promisor-objects, but with all trees gone.
+ promise_and_delete $(git -C repo rev-parse baz^{tree}) &&
+ git -C repo rev-list --exclude-promisor-objects --objects HEAD >objs 2>rev_list_err &&
+ test_must_be_empty rev_list_err &&
+ # 3 commits, no blobs or trees
+ test_line_count = 3 objs
+'
+
+test_expect_success 'missing non-root tree object and rev-list' '
+ rm -rf repo &&
+ test_create_repo repo &&
+ mkdir repo/dir &&
+ echo foo >repo/dir/foo &&
+ git -C repo add dir/foo &&
+ git -C repo commit -m "commit dir/foo" &&
+
+ promise_and_delete $(git -C repo rev-parse HEAD:dir) &&
+
+ git -C repo config core.repositoryformatversion 1 &&
+ git -C repo config extensions.partialclone "arbitrary string" &&
+
+ git -C repo rev-list --missing=allow-any --objects HEAD >objs 2>rev_list_err &&
+ test_must_be_empty rev_list_err &&
+ # 1 commit and 1 tree
+ test_line_count = 2 objs
+'
+
+test_expect_success 'rev-list stops traversal at missing and promised tree' '
+ rm -rf repo &&
+ test_create_repo repo &&
+ test_commit -C repo foo &&
+ mkdir repo/a_dir &&
+ echo something >repo/a_dir/something &&
+ git -C repo add a_dir/something &&
+ git -C repo commit -m bar &&
+
+ # foo^{tree} (tree referenced from commit)
+ TREE=$(git -C repo rev-parse foo^{tree}) &&
+
+ # a tree referenced by HEAD^{tree} (tree referenced from tree)
+ TREE2=$(git -C repo ls-tree HEAD^{tree} | grep " tree " | head -1 | cut -b13-52) &&
+
+ promise_and_delete "$TREE" &&
+ promise_and_delete "$TREE2" &&
+
+ git -C repo config core.repositoryformatversion 1 &&
+ git -C repo config extensions.partialclone "arbitrary string" &&
+ git -C repo rev-list --exclude-promisor-objects --objects HEAD >out &&
+ grep $(git -C repo rev-parse foo) out &&
+ ! grep $TREE out &&
+ grep $(git -C repo rev-parse HEAD) out &&
+ ! grep $TREE2 out
+'
+
+test_expect_success 'rev-list stops traversal at missing and promised blob' '
+ rm -rf repo &&
+ test_create_repo repo &&
+ echo something >repo/something &&
+ git -C repo add something &&
+ git -C repo commit -m foo &&
+
+ BLOB=$(git -C repo hash-object -w something) &&
+ promise_and_delete "$BLOB" &&
+
+ git -C repo config core.repositoryformatversion 1 &&
+ git -C repo config extensions.partialclone "arbitrary string" &&
+ git -C repo rev-list --exclude-promisor-objects --objects HEAD >out &&
+ grep $(git -C repo rev-parse HEAD) out &&
+ ! grep $BLOB out
+'
+
+test_expect_success 'rev-list stops traversal at promisor commit, tree, and blob' '
+ rm -rf repo &&
+ test_create_repo repo &&
+ test_commit -C repo foo &&
+ test_commit -C repo bar &&
+ test_commit -C repo baz &&
+
+ COMMIT=$(git -C repo rev-parse foo) &&
+ TREE=$(git -C repo rev-parse bar^{tree}) &&
+ BLOB=$(git hash-object repo/baz.t) &&
+ printf "%s\n%s\n%s\n" $COMMIT $TREE $BLOB | pack_as_from_promisor &&
+
+ git -C repo config core.repositoryformatversion 1 &&
+ git -C repo config extensions.partialclone "arbitrary string" &&
+ git -C repo rev-list --exclude-promisor-objects --objects HEAD >out &&
+ ! grep $COMMIT out &&
+ ! grep $TREE out &&
+ ! grep $BLOB out &&
+ grep $(git -C repo rev-parse bar) out # sanity check that some walking was done
+'
+
+test_expect_success 'rev-list dies for missing objects on cmd line' '
+ rm -rf repo &&
+ test_create_repo repo &&
+ test_commit -C repo foo &&
+ test_commit -C repo bar &&
+ test_commit -C repo baz &&
+
+ COMMIT=$(git -C repo rev-parse foo) &&
+ TREE=$(git -C repo rev-parse bar^{tree}) &&
+ BLOB=$(git hash-object repo/baz.t) &&
+
+ promise_and_delete $COMMIT &&
+ promise_and_delete $TREE &&
+ promise_and_delete $BLOB &&
+
+ git -C repo config core.repositoryformatversion 1 &&
+ git -C repo config extensions.partialclone "arbitrary string" &&
+
+ for OBJ in "$COMMIT" "$TREE" "$BLOB"; do
+ test_must_fail git -C repo rev-list --objects \
+ --exclude-promisor-objects "$OBJ" &&
+ test_must_fail git -C repo rev-list --objects-edge-aggressive \
+ --exclude-promisor-objects "$OBJ" &&
+
+ # Do not die or crash when --ignore-missing is passed.
+ git -C repo rev-list --ignore-missing --objects \
+ --exclude-promisor-objects "$OBJ" &&
+ git -C repo rev-list --ignore-missing --objects-edge-aggressive \
+ --exclude-promisor-objects "$OBJ" || return 1
+ done
+'
+
+test_expect_success 'single promisor remote can be re-initialized gracefully' '
+ # ensure one promisor is in the promisors list
+ rm -rf repo &&
+ test_create_repo repo &&
+ test_create_repo other &&
+ git -C repo remote add foo "file://$(pwd)/other" &&
+ git -C repo config remote.foo.promisor true &&
+ git -C repo config extensions.partialclone foo &&
+
+ # reinitialize the promisors list
+ git -C repo fetch --filter=blob:none foo
+'
+
+test_expect_success 'gc repacks promisor objects separately from non-promisor objects' '
+ rm -rf repo &&
+ test_create_repo repo &&
+ test_commit -C repo one &&
+ test_commit -C repo two &&
+
+ TREE_ONE=$(git -C repo rev-parse one^{tree}) &&
+ printf "$TREE_ONE\n" | pack_as_from_promisor &&
+ TREE_TWO=$(git -C repo rev-parse two^{tree}) &&
+ printf "$TREE_TWO\n" | pack_as_from_promisor &&
+
+ git -C repo config core.repositoryformatversion 1 &&
+ git -C repo config extensions.partialclone "arbitrary string" &&
+ git -C repo gc &&
+
+ # Ensure that exactly one promisor packfile exists, and that it
+ # contains the trees but not the commits
+ ls repo/.git/objects/pack/pack-*.promisor >promisorlist &&
+ test_line_count = 1 promisorlist &&
+ PROMISOR_PACKFILE=$(sed "s/.promisor/.pack/" <promisorlist) &&
+ git verify-pack $PROMISOR_PACKFILE -v >out &&
+ grep "$TREE_ONE" out &&
+ grep "$TREE_TWO" out &&
+ ! grep "$(git -C repo rev-parse one)" out &&
+ ! grep "$(git -C repo rev-parse two)" out &&
+
+ # Remove the promisor packfile and associated files
+ rm $(sed "s/.promisor//" <promisorlist).* &&
+
+ # Ensure that the single other pack contains the commits, but not the
+ # trees
+ ls repo/.git/objects/pack/pack-*.pack >packlist &&
+ test_line_count = 1 packlist &&
+ git verify-pack repo/.git/objects/pack/pack-*.pack -v >out &&
+ grep "$(git -C repo rev-parse one)" out &&
+ grep "$(git -C repo rev-parse two)" out &&
+ ! grep "$TREE_ONE" out &&
+ ! grep "$TREE_TWO" out
+'
+
+test_expect_success 'gc does not repack promisor objects if there are none' '
+ rm -rf repo &&
+ test_create_repo repo &&
+ test_commit -C repo one &&
+
+ git -C repo config core.repositoryformatversion 1 &&
+ git -C repo config extensions.partialclone "arbitrary string" &&
+ git -C repo gc &&
+
+ # Ensure that only one pack exists
+ ls repo/.git/objects/pack/pack-*.pack >packlist &&
+ test_line_count = 1 packlist
+'
+
+repack_and_check () {
+ rm -rf repo2 &&
+ cp -r repo repo2 &&
+ if test x"$1" = "x--must-fail"
+ then
+ shift
+ test_must_fail git -C repo2 repack $1 -d
+ else
+ git -C repo2 repack $1 -d
+ fi &&
+ git -C repo2 fsck &&
+
+ git -C repo2 cat-file -e $2 &&
+ git -C repo2 cat-file -e $3
+}
+
+test_expect_success 'repack -d does not irreversibly delete promisor objects' '
+ rm -rf repo &&
+ test_create_repo repo &&
+ git -C repo config core.repositoryformatversion 1 &&
+ git -C repo config extensions.partialclone "arbitrary string" &&
+
+ git -C repo commit --allow-empty -m one &&
+ git -C repo commit --allow-empty -m two &&
+ git -C repo commit --allow-empty -m three &&
+ git -C repo commit --allow-empty -m four &&
+ ONE=$(git -C repo rev-parse HEAD^^^) &&
+ TWO=$(git -C repo rev-parse HEAD^^) &&
+ THREE=$(git -C repo rev-parse HEAD^) &&
+
+ printf "$TWO\n" | pack_as_from_promisor &&
+ printf "$THREE\n" | pack_as_from_promisor &&
+ delete_object repo "$ONE" &&
+
+ repack_and_check --must-fail -ab "$TWO" "$THREE" &&
+ repack_and_check -a "$TWO" "$THREE" &&
+ repack_and_check -A "$TWO" "$THREE" &&
+ repack_and_check -l "$TWO" "$THREE"
+'
+
+test_expect_success 'gc stops traversal when a missing but promised object is reached' '
+ rm -rf repo &&
+ test_create_repo repo &&
+ test_commit -C repo my_commit &&
+
+ TREE_HASH=$(git -C repo rev-parse HEAD^{tree}) &&
+ HASH=$(promise_and_delete $TREE_HASH) &&
+
+ git -C repo config core.repositoryformatversion 1 &&
+ git -C repo config extensions.partialclone "arbitrary string" &&
+ git -C repo gc &&
+
+ # Ensure that the promisor packfile still exists, and remove it
+ test -e repo/.git/objects/pack/pack-$HASH.pack &&
+ rm repo/.git/objects/pack/pack-$HASH.* &&
+
+ # Ensure that the single other pack contains the commit, but not the tree
+ ls repo/.git/objects/pack/pack-*.pack >packlist &&
+ test_line_count = 1 packlist &&
+ git verify-pack repo/.git/objects/pack/pack-*.pack -v >out &&
+ grep "$(git -C repo rev-parse HEAD)" out &&
+ ! grep "$TREE_HASH" out
+'
+
+test_expect_success 'do not fetch when checking existence of tree we construct ourselves' '
+ rm -rf repo &&
+ test_create_repo repo &&
+ test_commit -C repo base &&
+ test_commit -C repo side1 &&
+ git -C repo checkout base &&
+ test_commit -C repo side2 &&
+
+ git -C repo config core.repositoryformatversion 1 &&
+ git -C repo config extensions.partialclone "arbitrary string" &&
+
+ git -C repo cherry-pick side1
+'
+
+test_expect_success 'exact rename does not need to fetch the blob lazily' '
+ rm -rf repo partial.git &&
+ test_create_repo repo &&
+ content="some dummy content" &&
+ test_commit -C repo create-a-file file.txt "$content" &&
+ git -C repo mv file.txt new-file.txt &&
+ git -C repo commit -m rename-the-file &&
+ FILE_HASH=$(git -C repo rev-parse HEAD:new-file.txt) &&
+ test_config -C repo uploadpack.allowfilter 1 &&
+ test_config -C repo uploadpack.allowanysha1inwant 1 &&
+
+ git clone --filter=blob:none --bare "file://$(pwd)/repo" partial.git &&
+ git -C partial.git rev-list --objects --missing=print HEAD >out &&
+ grep "[?]$FILE_HASH" out &&
+ git -C partial.git log --follow -- new-file.txt &&
+ git -C partial.git rev-list --objects --missing=print HEAD >out &&
+ grep "[?]$FILE_HASH" out
+'
+
+test_expect_success 'lazy-fetch when accessing object not in the_repository' '
+ rm -rf full partial.git &&
+ test_create_repo full &&
+ test_commit -C full create-a-file file.txt &&
+
+ test_config -C full uploadpack.allowfilter 1 &&
+ test_config -C full uploadpack.allowanysha1inwant 1 &&
+ git clone --filter=blob:none --bare "file://$(pwd)/full" partial.git &&
+ FILE_HASH=$(git -C full rev-parse HEAD:file.txt) &&
+
+ # Sanity check that the file is missing
+ git -C partial.git rev-list --objects --missing=print HEAD >out &&
+ grep "[?]$FILE_HASH" out &&
+
+ git -C full cat-file -s "$FILE_HASH" >expect &&
+ test-tool partial-clone object-info partial.git "$FILE_HASH" >actual &&
+ test_cmp expect actual &&
+
+ # Sanity check that the file is now present
+ git -C partial.git rev-list --objects --missing=print HEAD >out &&
+ ! grep "[?]$FILE_HASH" out
+'
+
+. "$TEST_DIRECTORY"/lib-httpd.sh
+start_httpd
+
+test_expect_success 'fetching of missing objects from an HTTP server' '
+ rm -rf repo &&
+ SERVER="$HTTPD_DOCUMENT_ROOT_PATH/server" &&
+ test_create_repo "$SERVER" &&
+ test_commit -C "$SERVER" foo &&
+ git -C "$SERVER" repack -a -d --write-bitmap-index &&
+
+ git clone $HTTPD_URL/smart/server repo &&
+ HASH=$(git -C repo rev-parse foo) &&
+ rm -rf repo/.git/objects/* &&
+
+ git -C repo config core.repositoryformatversion 1 &&
+ git -C repo config extensions.partialclone "origin" &&
+ git -C repo cat-file -p "$HASH" &&
+
+ # Ensure that the .promisor file is written, and check that its
+ # associated packfile contains the object
+ ls repo/.git/objects/pack/pack-*.promisor >promisorlist &&
+ test_line_count = 1 promisorlist &&
+ IDX=$(sed "s/promisor$/idx/" promisorlist) &&
+ git verify-pack --verbose "$IDX" >out &&
+ grep "$HASH" out
+'
+
+# DO NOT add non-httpd-specific tests here, because the last part of this
+# test script is only executed when httpd is available and enabled.
+
+test_done
diff --git a/t/t0450-txt-doc-vs-help.sh b/t/t0450-txt-doc-vs-help.sh
new file mode 100755
index 0000000..cd3969e
--- /dev/null
+++ b/t/t0450-txt-doc-vs-help.sh
@@ -0,0 +1,172 @@
+#!/bin/sh
+
+test_description='assert (unbuilt) Documentation/*.txt and -h output
+
+Run this with --debug to see a summary of where we still fail to make
+the two versions consistent with one another.'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup: list of builtins' '
+ git --list-cmds=builtins >builtins
+'
+
+test_expect_success 'list of txt and help mismatches is sorted' '
+ sort -u "$TEST_DIRECTORY"/t0450/txt-help-mismatches >expect &&
+ if ! test_cmp expect "$TEST_DIRECTORY"/t0450/txt-help-mismatches
+ then
+ BUG "please keep the list of txt and help mismatches sorted"
+ fi
+'
+
+help_to_synopsis () {
+ builtin="$1" &&
+ out_dir="out/$builtin" &&
+ out="$out_dir/help.synopsis" &&
+ if test -f "$out"
+ then
+ echo "$out" &&
+ return 0
+ fi &&
+ mkdir -p "$out_dir" &&
+ test_expect_code 129 git $builtin -h >"$out.raw" 2>&1 &&
+ sed -n \
+ -e '1,/^$/ {
+ /^$/d;
+ s/^usage: //;
+ s/^ *or: //;
+ p;
+ }' <"$out.raw" >"$out" &&
+ echo "$out"
+}
+
+builtin_to_txt () {
+ echo "$GIT_BUILD_DIR/Documentation/git-$1.txt"
+}
+
+txt_to_synopsis () {
+ builtin="$1" &&
+ out_dir="out/$builtin" &&
+ out="$out_dir/txt.synopsis" &&
+ if test -f "$out"
+ then
+ echo "$out" &&
+ return 0
+ fi &&
+ b2t="$(builtin_to_txt "$builtin")" &&
+ sed -n \
+ -e '/^\[verse\]$/,/^$/ {
+ /^$/d;
+ /^\[verse\]$/d;
+
+ s/{litdd}/--/g;
+ s/'\''\(git[ a-z-]*\)'\''/\1/g;
+
+ p;
+ }' \
+ <"$b2t" >"$out" &&
+ echo "$out"
+}
+
+check_dashed_labels () {
+ ! grep -E "<[^>_-]+_" "$1"
+}
+
+HT=" "
+
+align_after_nl () {
+ builtin="$1" &&
+ len=$(printf "git %s " "$builtin" | wc -c) &&
+ pad=$(printf "%${len}s" "") &&
+
+ sed "s/^[ $HT][ $HT]*/$pad/"
+}
+
+test_debug '>failing'
+while read builtin
+do
+ # -h output assertions
+ test_expect_success "$builtin -h output has no \t" '
+ h2s="$(help_to_synopsis "$builtin")" &&
+ ! grep "$HT" "$h2s"
+ '
+
+ test_expect_success "$builtin -h output has dashed labels" '
+ check_dashed_labels "$(help_to_synopsis "$builtin")"
+ '
+
+ test_expect_success "$builtin -h output has consistent spacing" '
+ h2s="$(help_to_synopsis "$builtin")" &&
+ sed -n \
+ -e "/^ / {
+ s/[^ ].*//;
+ p;
+ }" \
+ <"$h2s" >help &&
+ sort -u help >help.ws &&
+ if test -s help.ws
+ then
+ test_line_count = 1 help.ws
+ fi
+ '
+
+ txt="$(builtin_to_txt "$builtin")" &&
+ preq="$(echo BUILTIN_TXT_$builtin | tr '[:lower:]-' '[:upper:]_')" &&
+
+ if test -f "$txt"
+ then
+ test_set_prereq "$preq"
+ fi &&
+
+ # *.txt output assertions
+ test_expect_success "$preq" "$builtin *.txt SYNOPSIS has dashed labels" '
+ check_dashed_labels "$(txt_to_synopsis "$builtin")"
+ '
+
+ # *.txt output consistency assertions
+ result=
+ if grep -q "^$builtin$" "$TEST_DIRECTORY"/t0450/txt-help-mismatches
+ then
+ result=failure
+ else
+ result=success
+ fi &&
+ test_expect_$result "$preq" "$builtin -h output and SYNOPSIS agree" '
+ t2s="$(txt_to_synopsis "$builtin")" &&
+ if test "$builtin" = "merge-tree"
+ then
+ test_when_finished "rm -f t2s.new" &&
+ sed -e '\''s/ (deprecated)$//g'\'' <"$t2s" >t2s.new
+ t2s=t2s.new
+ fi &&
+ h2s="$(help_to_synopsis "$builtin")" &&
+
+ # The *.txt and -h use different spacing for the
+ # alignment of continued usage output, normalize it.
+ align_after_nl "$builtin" <"$t2s" >txt &&
+ align_after_nl "$builtin" <"$h2s" >help &&
+ test_cmp txt help
+ '
+
+ if test_have_prereq "$preq" && test -e txt && test -e help
+ then
+ test_debug '
+ if test_cmp txt help >cmp 2>/dev/null
+ then
+ echo "=== DONE: $builtin ==="
+ else
+ echo "=== TODO: $builtin ===" &&
+ cat cmp
+ fi >>failing
+ '
+
+ # Not in test_expect_success in case --run is being
+ # used with --debug
+ rm -f txt help tmp 2>/dev/null
+ fi
+done <builtins
+
+test_debug 'say "$(cat failing)"'
+
+test_done
diff --git a/t/t0450/txt-help-mismatches b/t/t0450/txt-help-mismatches
new file mode 100644
index 0000000..a0777ac
--- /dev/null
+++ b/t/t0450/txt-help-mismatches
@@ -0,0 +1,58 @@
+add
+am
+apply
+archive
+bisect
+blame
+branch
+check-ref-format
+checkout
+checkout-index
+clone
+column
+config
+credential
+credential-cache
+credential-store
+fast-export
+fast-import
+fetch-pack
+fmt-merge-msg
+for-each-ref
+format-patch
+fsck-objects
+fsmonitor--daemon
+gc
+grep
+index-pack
+init-db
+log
+ls-files
+ls-tree
+mailinfo
+mailsplit
+maintenance
+merge
+merge-file
+merge-index
+merge-one-file
+multi-pack-index
+name-rev
+notes
+pack-objects
+push
+range-diff
+rebase
+remote
+remote-ext
+remote-fd
+repack
+reset
+restore
+rev-parse
+show
+stage
+switch
+update-index
+update-ref
+whatchanged
diff --git a/t/t0500-progress-display.sh b/t/t0500-progress-display.sh
new file mode 100755
index 0000000..1eb3a83
--- /dev/null
+++ b/t/t0500-progress-display.sh
@@ -0,0 +1,378 @@
+#!/bin/sh
+
+test_description='progress display'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+show_cr () {
+ tr '\015' Q | sed -e "s/Q/<CR>\\$LF/g"
+}
+
+test_expect_success 'simple progress display' '
+ cat >expect <<-\EOF &&
+ Working hard: 1<CR>
+ Working hard: 2<CR>
+ Working hard: 5<CR>
+ Working hard: 5, done.
+ EOF
+
+ cat >in <<-\EOF &&
+ start 0
+ update
+ progress 1
+ update
+ progress 2
+ progress 3
+ progress 4
+ update
+ progress 5
+ stop
+ EOF
+ test-tool progress <in 2>stderr &&
+
+ show_cr <stderr >out &&
+ test_cmp expect out
+'
+
+test_expect_success 'progress display with total' '
+ cat >expect <<-\EOF &&
+ Working hard: 33% (1/3)<CR>
+ Working hard: 66% (2/3)<CR>
+ Working hard: 100% (3/3)<CR>
+ Working hard: 100% (3/3), done.
+ EOF
+
+ cat >in <<-\EOF &&
+ start 3
+ progress 1
+ progress 2
+ progress 3
+ stop
+ EOF
+ test-tool progress <in 2>stderr &&
+
+ show_cr <stderr >out &&
+ test_cmp expect out
+'
+
+test_expect_success 'progress display breaks long lines #1' '
+ sed -e "s/Z$//" >expect <<\EOF &&
+Working hard.......2.........3.........4.........5.........6: 0% (100/100000)<CR>
+Working hard.......2.........3.........4.........5.........6: 1% (1000/100000)<CR>
+Working hard.......2.........3.........4.........5.........6: Z
+ 10% (10000/100000)<CR>
+ 100% (100000/100000)<CR>
+ 100% (100000/100000), done.
+EOF
+
+ cat >in <<-\EOF &&
+ start 100000 Working hard.......2.........3.........4.........5.........6
+ progress 100
+ progress 1000
+ progress 10000
+ progress 100000
+ stop
+ EOF
+ test-tool progress <in 2>stderr &&
+
+ show_cr <stderr >out &&
+ test_cmp expect out
+'
+
+test_expect_success 'progress display breaks long lines #2' '
+ # Note: we do not need that many spaces after the title to cover up
+ # the last line before breaking the progress line.
+ sed -e "s/Z$//" >expect <<\EOF &&
+Working hard.......2.........3.........4.........5.........6: 0% (1/100000)<CR>
+Working hard.......2.........3.........4.........5.........6: 0% (2/100000)<CR>
+Working hard.......2.........3.........4.........5.........6: Z
+ 10% (10000/100000)<CR>
+ 100% (100000/100000)<CR>
+ 100% (100000/100000), done.
+EOF
+
+ cat >in <<-\EOF &&
+ start 100000 Working hard.......2.........3.........4.........5.........6
+ update
+ progress 1
+ update
+ progress 2
+ progress 10000
+ progress 100000
+ stop
+ EOF
+ test-tool progress <in 2>stderr &&
+
+ show_cr <stderr >out &&
+ test_cmp expect out
+'
+
+test_expect_success 'progress display breaks long lines #3 - even the first is too long' '
+ # Note: we do not actually need any spaces at the end of the title
+ # line, because there is no previous progress line to cover up.
+ sed -e "s/Z$//" >expect <<\EOF &&
+Working hard.......2.........3.........4.........5.........6: Z
+ 25% (25000/100000)<CR>
+ 50% (50000/100000)<CR>
+ 75% (75000/100000)<CR>
+ 100% (100000/100000)<CR>
+ 100% (100000/100000), done.
+EOF
+
+ cat >in <<-\EOF &&
+ start 100000 Working hard.......2.........3.........4.........5.........6
+ progress 25000
+ progress 50000
+ progress 75000
+ progress 100000
+ stop
+ EOF
+ test-tool progress <in 2>stderr &&
+
+ show_cr <stderr >out &&
+ test_cmp expect out
+'
+
+test_expect_success 'progress display breaks long lines #4 - title line matches terminal width' '
+ cat >expect <<\EOF &&
+Working hard.......2.........3.........4.........5.........6.........7.........:
+ 25% (25000/100000)<CR>
+ 50% (50000/100000)<CR>
+ 75% (75000/100000)<CR>
+ 100% (100000/100000)<CR>
+ 100% (100000/100000), done.
+EOF
+
+ cat >in <<-\EOF &&
+ start 100000 Working hard.......2.........3.........4.........5.........6.........7.........
+ progress 25000
+ progress 50000
+ progress 75000
+ progress 100000
+ stop
+ EOF
+ test-tool progress <in 2>stderr &&
+
+ show_cr <stderr >out &&
+ test_cmp expect out
+'
+
+# Progress counter goes backwards, this should not happen in practice.
+test_expect_success 'progress shortens - crazy caller' '
+ cat >expect <<-\EOF &&
+ Working hard: 10% (100/1000)<CR>
+ Working hard: 20% (200/1000)<CR>
+ Working hard: 0% (1/1000) <CR>
+ Working hard: 100% (1000/1000)<CR>
+ Working hard: 100% (1000/1000), done.
+ EOF
+
+ cat >in <<-\EOF &&
+ start 1000
+ progress 100
+ progress 200
+ progress 1
+ progress 1000
+ stop
+ EOF
+ test-tool progress <in 2>stderr &&
+
+ show_cr <stderr >out &&
+ test_cmp expect out
+'
+
+test_expect_success 'progress display with throughput' '
+ cat >expect <<-\EOF &&
+ Working hard: 10<CR>
+ Working hard: 20, 200.00 KiB | 100.00 KiB/s<CR>
+ Working hard: 30, 300.00 KiB | 100.00 KiB/s<CR>
+ Working hard: 40, 400.00 KiB | 100.00 KiB/s<CR>
+ Working hard: 40, 400.00 KiB | 100.00 KiB/s, done.
+ EOF
+
+ cat >in <<-\EOF &&
+ start 0
+ throughput 102400 1000
+ update
+ progress 10
+ throughput 204800 2000
+ update
+ progress 20
+ throughput 307200 3000
+ update
+ progress 30
+ throughput 409600 4000
+ update
+ progress 40
+ stop
+ EOF
+ test-tool progress <in 2>stderr &&
+
+ show_cr <stderr >out &&
+ test_cmp expect out
+'
+
+test_expect_success 'progress display with throughput and total' '
+ cat >expect <<-\EOF &&
+ Working hard: 25% (10/40)<CR>
+ Working hard: 50% (20/40), 200.00 KiB | 100.00 KiB/s<CR>
+ Working hard: 75% (30/40), 300.00 KiB | 100.00 KiB/s<CR>
+ Working hard: 100% (40/40), 400.00 KiB | 100.00 KiB/s<CR>
+ Working hard: 100% (40/40), 400.00 KiB | 100.00 KiB/s, done.
+ EOF
+
+ cat >in <<-\EOF &&
+ start 40
+ throughput 102400 1000
+ progress 10
+ throughput 204800 2000
+ progress 20
+ throughput 307200 3000
+ progress 30
+ throughput 409600 4000
+ progress 40
+ stop
+ EOF
+ test-tool progress <in 2>stderr &&
+
+ show_cr <stderr >out &&
+ test_cmp expect out
+'
+
+test_expect_success 'cover up after throughput shortens' '
+ cat >expect <<-\EOF &&
+ Working hard: 1<CR>
+ Working hard: 2, 800.00 KiB | 400.00 KiB/s<CR>
+ Working hard: 3, 1.17 MiB | 400.00 KiB/s <CR>
+ Working hard: 4, 1.56 MiB | 400.00 KiB/s<CR>
+ Working hard: 4, 1.56 MiB | 400.00 KiB/s, done.
+ EOF
+
+ cat >in <<-\EOF &&
+ start 0
+ throughput 409600 1000
+ update
+ progress 1
+ throughput 819200 2000
+ update
+ progress 2
+ throughput 1228800 3000
+ update
+ progress 3
+ throughput 1638400 4000
+ update
+ progress 4
+ stop
+ EOF
+ test-tool progress <in 2>stderr &&
+
+ show_cr <stderr >out &&
+ test_cmp expect out
+'
+
+test_expect_success 'cover up after throughput shortens a lot' '
+ cat >expect <<-\EOF &&
+ Working hard: 1<CR>
+ Working hard: 2, 1000.00 KiB | 1000.00 KiB/s<CR>
+ Working hard: 3, 3.00 MiB | 1.50 MiB/s <CR>
+ Working hard: 3, 3.00 MiB | 1024.00 KiB/s, done.
+ EOF
+
+ cat >in <<-\EOF &&
+ start 0
+ throughput 1 1000
+ update
+ progress 1
+ throughput 1024000 2000
+ update
+ progress 2
+ throughput 3145728 3000
+ update
+ progress 3
+ stop
+ EOF
+ test-tool progress <in 2>stderr &&
+
+ show_cr <stderr >out &&
+ test_cmp expect out
+'
+
+test_expect_success 'progress generates traces' '
+ cat >in <<-\EOF &&
+ start 40
+ throughput 102400 1000
+ update
+ progress 10
+ throughput 204800 2000
+ update
+ progress 20
+ throughput 307200 3000
+ update
+ progress 30
+ throughput 409600 4000
+ update
+ progress 40
+ stop
+ EOF
+
+ GIT_TRACE2_EVENT="$(pwd)/trace.event" test-tool progress \
+ <in 2>stderr &&
+
+ # t0212/parse_events.perl intentionally omits regions and data.
+ test_region progress "Working hard" trace.event &&
+ grep "\"key\":\"total_objects\",\"value\":\"40\"" trace.event &&
+ grep "\"key\":\"total_bytes\",\"value\":\"409600\"" trace.event
+'
+
+test_expect_success 'progress generates traces: stop / start' '
+ cat >in <<-\EOF &&
+ start 0
+ stop
+ EOF
+
+ GIT_TRACE2_EVENT="$PWD/trace-startstop.event" test-tool progress \
+ <in 2>stderr &&
+ test_region progress "Working hard" trace-startstop.event
+'
+
+test_expect_success 'progress generates traces: start without stop' '
+ cat >in <<-\EOF &&
+ start 0
+ EOF
+
+ GIT_TRACE2_EVENT="$PWD/trace-start.event" \
+ LSAN_OPTIONS=detect_leaks=0 \
+ test-tool progress \
+ <in 2>stderr &&
+ grep region_enter.*progress trace-start.event &&
+ ! grep region_leave.*progress trace-start.event
+'
+
+test_expect_success 'progress generates traces: stop without start' '
+ cat >in <<-\EOF &&
+ stop
+ EOF
+
+ GIT_TRACE2_EVENT="$PWD/trace-stop.event" test-tool progress \
+ <in 2>stderr &&
+ ! grep region_enter.*progress trace-stop.event &&
+ ! grep region_leave.*progress trace-stop.event
+'
+
+test_expect_success 'progress generates traces: start with active progress bar (no stops)' '
+ cat >in <<-\EOF &&
+ start 0 One
+ start 0 Two
+ EOF
+
+ GIT_TRACE2_EVENT="$PWD/trace-2start.event" \
+ LSAN_OPTIONS=detect_leaks=0 \
+ test-tool progress \
+ <in 2>stderr &&
+ grep region_enter.*progress.*One trace-2start.event &&
+ grep region_enter.*progress.*Two trace-2start.event &&
+ ! grep region_leave trace-2start.event
+'
+
+test_done
diff --git a/t/t1000-read-tree-m-3way.sh b/t/t1000-read-tree-m-3way.sh
new file mode 100755
index 0000000..0e8c0df
--- /dev/null
+++ b/t/t1000-read-tree-m-3way.sh
@@ -0,0 +1,513 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+test_description='Three way merge with read-tree -m
+
+This test tries three-way merge with read-tree -m
+
+There is one ancestor (called O for Original) and two branches A
+and B derived from it. We want to do a 3-way merge between A and
+B, using O as the common ancestor.
+
+ merge A O B
+
+Decisions are made by comparing contents of O, A and B pathname
+by pathname. The result is determined by the following guiding
+principle:
+
+ - If only A does something to it and B does not touch it, take
+ whatever A does.
+
+ - If only B does something to it and A does not touch it, take
+ whatever B does.
+
+ - If both A and B does something but in the same way, take
+ whatever they do.
+
+ - If A and B does something but different things, we need a
+ 3-way merge:
+
+ - We cannot do anything about the following cases:
+
+ * O does not have it. A and B both must be adding to the
+ same path independently.
+
+ * A deletes it. B must be modifying.
+
+ - Otherwise, A and B are modifying. Run 3-way merge.
+
+First, the case matrix.
+
+ - Vertical axis is for A'\''s actions.
+ - Horizontal axis is for B'\''s actions.
+
+.----------------------------------------------------------------.
+| A B | No Action | Delete | Modify | Add |
+|------------+------------+------------+------------+------------|
+| No Action | | | | |
+| | select O | delete | select B | select B |
+| | | | | |
+|------------+------------+------------+------------+------------|
+| Delete | | | ********** | can |
+| | delete | delete | merge | not |
+| | | | | happen |
+|------------+------------+------------+------------+------------|
+| Modify | | ********** | ?????????? | can |
+| | select A | merge | select A=B | not |
+| | | | merge | happen |
+|------------+------------+------------+------------+------------|
+| Add | | can | can | ?????????? |
+| | select A | not | not | select A=B |
+| | | happen | happen | merge |
+.----------------------------------------------------------------.
+
+In addition:
+
+ SS: a special case of MM, where A and B makes the same modification.
+ LL: a special case of AA, where A and B creates the same file.
+ TT: a special case of MM, where A and B makes mergeable changes.
+ DF: a special case, where A makes a directory and B makes a file.
+
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-read-tree.sh
+. "$TEST_DIRECTORY"/lib-read-tree-m-3way.sh
+
+################################################################
+# Trivial "majority when 3 stages exist" merge plus #2ALT, #3ALT
+# and #5ALT trivial merges.
+
+cat >expected <<\EOF
+100644 X 2 AA
+100644 X 3 AA
+100644 X 0 AN
+100644 X 1 DD
+100644 X 3 DF
+100644 X 2 DF/DF
+100644 X 1 DM
+100644 X 3 DM
+100644 X 1 DN
+100644 X 3 DN
+100644 X 0 LL
+100644 X 1 MD
+100644 X 2 MD
+100644 X 1 MM
+100644 X 2 MM
+100644 X 3 MM
+100644 X 0 MN
+100644 X 0 NA
+100644 X 1 ND
+100644 X 2 ND
+100644 X 0 NM
+100644 X 0 NN
+100644 X 0 SS
+100644 X 1 TT
+100644 X 2 TT
+100644 X 3 TT
+100644 X 2 Z/AA
+100644 X 3 Z/AA
+100644 X 0 Z/AN
+100644 X 1 Z/DD
+100644 X 1 Z/DM
+100644 X 3 Z/DM
+100644 X 1 Z/DN
+100644 X 3 Z/DN
+100644 X 1 Z/MD
+100644 X 2 Z/MD
+100644 X 1 Z/MM
+100644 X 2 Z/MM
+100644 X 3 Z/MM
+100644 X 0 Z/MN
+100644 X 0 Z/NA
+100644 X 1 Z/ND
+100644 X 2 Z/ND
+100644 X 0 Z/NM
+100644 X 0 Z/NN
+EOF
+
+check_result () {
+ git ls-files --stage | sed -e 's/ '"$OID_REGEX"' / X /' >current &&
+ test_cmp expected current
+}
+
+# This is done on an empty work directory, which is the normal
+# merge person behaviour.
+test_expect_success '3-way merge with git read-tree -m, empty cache' '
+ rm -fr [NDMALTS][NDMALTSF] Z &&
+ rm .git/index &&
+ read_tree_must_succeed -m $tree_O $tree_A $tree_B &&
+ check_result
+'
+
+# This starts out with the first head, which is the normal
+# patch submitter behaviour.
+test_expect_success '3-way merge with git read-tree -m, match H' '
+ rm -fr [NDMALTS][NDMALTSF] Z &&
+ rm .git/index &&
+ read_tree_must_succeed $tree_A &&
+ git checkout-index -f -u -a &&
+ read_tree_must_succeed -m $tree_O $tree_A $tree_B &&
+ check_result
+'
+
+: <<\END_OF_CASE_TABLE
+
+We have so far tested only empty index and clean-and-matching-A index
+case which are trivial. Make sure index requirements are also
+checked.
+
+"git read-tree -m O A B"
+
+ O A B result index requirements
+-------------------------------------------------------------------
+ 1 missing missing missing - must not exist.
+ ------------------------------------------------------------------
+ 2 missing missing exists take B* must match B, if exists.
+ ------------------------------------------------------------------
+ 3 missing exists missing take A* must match A, if exists.
+ ------------------------------------------------------------------
+ 4 missing exists A!=B no merge must match A and be
+ up-to-date, if exists.
+ ------------------------------------------------------------------
+ 5 missing exists A==B take A must match A, if exists.
+ ------------------------------------------------------------------
+ 6 exists missing missing remove must not exist.
+ ------------------------------------------------------------------
+ 7 exists missing O!=B no merge must not exist.
+ ------------------------------------------------------------------
+ 8 exists missing O==B remove must not exist.
+ ------------------------------------------------------------------
+ 9 exists O!=A missing no merge must match A and be
+ up-to-date, if exists.
+ ------------------------------------------------------------------
+ 10 exists O==A missing no merge must match A
+ ------------------------------------------------------------------
+ 11 exists O!=A O!=B no merge must match A and be
+ A!=B up-to-date, if exists.
+ ------------------------------------------------------------------
+ 12 exists O!=A O!=B take A must match A, if exists.
+ A==B
+ ------------------------------------------------------------------
+ 13 exists O!=A O==B take A must match A, if exists.
+ ------------------------------------------------------------------
+ 14 exists O==A O!=B take B if exists, must either (1)
+ match A and be up-to-date,
+ or (2) match B.
+ ------------------------------------------------------------------
+ 15 exists O==A O==B take B must match A if exists.
+ ------------------------------------------------------------------
+ 16 exists O==A O==B barf must match A if exists.
+ *multi* in one in another
+-------------------------------------------------------------------
+
+Note: we need to be careful in case 2 and 3. The tree A may contain
+DF (file) when tree B require DF to be a directory by having DF/DF
+(file).
+
+END_OF_CASE_TABLE
+
+test_expect_success '1 - must not have an entry not in A.' '
+ rm -f .git/index XX &&
+ echo XX >XX &&
+ git update-index --add XX &&
+ read_tree_must_fail -m $tree_O $tree_A $tree_B
+'
+
+test_expect_success '2 - must match B in !O && !A && B case.' '
+ rm -f .git/index NA &&
+ cp .orig-B/NA NA &&
+ git update-index --add NA &&
+ read_tree_must_succeed -m $tree_O $tree_A $tree_B
+'
+
+test_expect_success '2 - matching B alone is OK in !O && !A && B case.' '
+ rm -f .git/index NA &&
+ cp .orig-B/NA NA &&
+ git update-index --add NA &&
+ echo extra >>NA &&
+ read_tree_must_succeed -m $tree_O $tree_A $tree_B
+'
+
+test_expect_success '3 - must match A in !O && A && !B case.' '
+ rm -f .git/index AN &&
+ cp .orig-A/AN AN &&
+ git update-index --add AN &&
+ read_tree_must_succeed -m $tree_O $tree_A $tree_B &&
+ check_result
+'
+
+test_expect_success '3 - matching A alone is OK in !O && A && !B case.' '
+ rm -f .git/index AN &&
+ cp .orig-A/AN AN &&
+ git update-index --add AN &&
+ echo extra >>AN &&
+ read_tree_must_succeed -m $tree_O $tree_A $tree_B
+'
+
+test_expect_success '3 (fail) - must match A in !O && A && !B case.' '
+ rm -f .git/index AN &&
+ cp .orig-A/AN AN &&
+ echo extra >>AN &&
+ git update-index --add AN &&
+ read_tree_must_fail -m $tree_O $tree_A $tree_B
+'
+
+test_expect_success '4 - must match and be up-to-date in !O && A && B && A!=B case.' '
+ rm -f .git/index AA &&
+ cp .orig-A/AA AA &&
+ git update-index --add AA &&
+ read_tree_must_succeed -m $tree_O $tree_A $tree_B &&
+ check_result
+'
+
+test_expect_success '4 (fail) - must match and be up-to-date in !O && A && B && A!=B case.' '
+ rm -f .git/index AA &&
+ cp .orig-A/AA AA &&
+ git update-index --add AA &&
+ echo extra >>AA &&
+ read_tree_must_fail -m $tree_O $tree_A $tree_B
+'
+
+test_expect_success '4 (fail) - must match and be up-to-date in !O && A && B && A!=B case.' '
+ rm -f .git/index AA &&
+ cp .orig-A/AA AA &&
+ echo extra >>AA &&
+ git update-index --add AA &&
+ read_tree_must_fail -m $tree_O $tree_A $tree_B
+'
+
+test_expect_success '5 - must match in !O && A && B && A==B case.' '
+ rm -f .git/index LL &&
+ cp .orig-A/LL LL &&
+ git update-index --add LL &&
+ read_tree_must_succeed -m $tree_O $tree_A $tree_B &&
+ check_result
+'
+
+test_expect_success '5 - must match in !O && A && B && A==B case.' '
+ rm -f .git/index LL &&
+ cp .orig-A/LL LL &&
+ git update-index --add LL &&
+ echo extra >>LL &&
+ read_tree_must_succeed -m $tree_O $tree_A $tree_B &&
+ check_result
+'
+
+test_expect_success '5 (fail) - must match A in !O && A && B && A==B case.' '
+ rm -f .git/index LL &&
+ cp .orig-A/LL LL &&
+ echo extra >>LL &&
+ git update-index --add LL &&
+ read_tree_must_fail -m $tree_O $tree_A $tree_B
+'
+
+test_expect_success '6 - must not exist in O && !A && !B case' '
+ rm -f .git/index DD &&
+ echo DD >DD &&
+ git update-index --add DD &&
+ read_tree_must_fail -m $tree_O $tree_A $tree_B
+'
+
+test_expect_success '7 - must not exist in O && !A && B && O!=B case' '
+ rm -f .git/index DM &&
+ cp .orig-B/DM DM &&
+ git update-index --add DM &&
+ read_tree_must_fail -m $tree_O $tree_A $tree_B
+'
+
+test_expect_success '8 - must not exist in O && !A && B && O==B case' '
+ rm -f .git/index DN &&
+ cp .orig-B/DN DN &&
+ git update-index --add DN &&
+ read_tree_must_fail -m $tree_O $tree_A $tree_B
+'
+
+test_expect_success '9 - must match and be up-to-date in O && A && !B && O!=A case' '
+ rm -f .git/index MD &&
+ cp .orig-A/MD MD &&
+ git update-index --add MD &&
+ read_tree_must_succeed -m $tree_O $tree_A $tree_B &&
+ check_result
+'
+
+test_expect_success '9 (fail) - must match and be up-to-date in O && A && !B && O!=A case' '
+ rm -f .git/index MD &&
+ cp .orig-A/MD MD &&
+ git update-index --add MD &&
+ echo extra >>MD &&
+ read_tree_must_fail -m $tree_O $tree_A $tree_B
+'
+
+test_expect_success '9 (fail) - must match and be up-to-date in O && A && !B && O!=A case' '
+ rm -f .git/index MD &&
+ cp .orig-A/MD MD &&
+ echo extra >>MD &&
+ git update-index --add MD &&
+ read_tree_must_fail -m $tree_O $tree_A $tree_B
+'
+
+test_expect_success '10 - must match and be up-to-date in O && A && !B && O==A case' '
+ rm -f .git/index ND &&
+ cp .orig-A/ND ND &&
+ git update-index --add ND &&
+ read_tree_must_succeed -m $tree_O $tree_A $tree_B &&
+ check_result
+'
+
+test_expect_success '10 (fail) - must match and be up-to-date in O && A && !B && O==A case' '
+ rm -f .git/index ND &&
+ cp .orig-A/ND ND &&
+ git update-index --add ND &&
+ echo extra >>ND &&
+ read_tree_must_fail -m $tree_O $tree_A $tree_B
+'
+
+test_expect_success '10 (fail) - must match and be up-to-date in O && A && !B && O==A case' '
+ rm -f .git/index ND &&
+ cp .orig-A/ND ND &&
+ echo extra >>ND &&
+ git update-index --add ND &&
+ read_tree_must_fail -m $tree_O $tree_A $tree_B
+'
+
+test_expect_success '11 - must match and be up-to-date in O && A && B && O!=A && O!=B && A!=B case' '
+ rm -f .git/index MM &&
+ cp .orig-A/MM MM &&
+ git update-index --add MM &&
+ read_tree_must_succeed -m $tree_O $tree_A $tree_B &&
+ check_result
+'
+
+test_expect_success '11 (fail) - must match and be up-to-date in O && A && B && O!=A && O!=B && A!=B case' '
+ rm -f .git/index MM &&
+ cp .orig-A/MM MM &&
+ git update-index --add MM &&
+ echo extra >>MM &&
+ read_tree_must_fail -m $tree_O $tree_A $tree_B
+'
+
+test_expect_success '11 (fail) - must match and be up-to-date in O && A && B && O!=A && O!=B && A!=B case' '
+ rm -f .git/index MM &&
+ cp .orig-A/MM MM &&
+ echo extra >>MM &&
+ git update-index --add MM &&
+ read_tree_must_fail -m $tree_O $tree_A $tree_B
+'
+
+test_expect_success '12 - must match A in O && A && B && O!=A && A==B case' '
+ rm -f .git/index SS &&
+ cp .orig-A/SS SS &&
+ git update-index --add SS &&
+ read_tree_must_succeed -m $tree_O $tree_A $tree_B &&
+ check_result
+'
+
+test_expect_success '12 - must match A in O && A && B && O!=A && A==B case' '
+ rm -f .git/index SS &&
+ cp .orig-A/SS SS &&
+ git update-index --add SS &&
+ echo extra >>SS &&
+ read_tree_must_succeed -m $tree_O $tree_A $tree_B &&
+ check_result
+'
+
+test_expect_success '12 (fail) - must match A in O && A && B && O!=A && A==B case' '
+ rm -f .git/index SS &&
+ cp .orig-A/SS SS &&
+ echo extra >>SS &&
+ git update-index --add SS &&
+ read_tree_must_fail -m $tree_O $tree_A $tree_B
+'
+
+test_expect_success '13 - must match A in O && A && B && O!=A && O==B case' '
+ rm -f .git/index MN &&
+ cp .orig-A/MN MN &&
+ git update-index --add MN &&
+ read_tree_must_succeed -m $tree_O $tree_A $tree_B &&
+ check_result
+'
+
+test_expect_success '13 - must match A in O && A && B && O!=A && O==B case' '
+ rm -f .git/index MN &&
+ cp .orig-A/MN MN &&
+ git update-index --add MN &&
+ echo extra >>MN &&
+ read_tree_must_succeed -m $tree_O $tree_A $tree_B &&
+ check_result
+'
+
+test_expect_success '14 - must match and be up-to-date in O && A && B && O==A && O!=B case' '
+ rm -f .git/index NM &&
+ cp .orig-A/NM NM &&
+ git update-index --add NM &&
+ read_tree_must_succeed -m $tree_O $tree_A $tree_B &&
+ check_result
+'
+
+test_expect_success '14 - may match B in O && A && B && O==A && O!=B case' '
+ rm -f .git/index NM &&
+ cp .orig-B/NM NM &&
+ git update-index --add NM &&
+ echo extra >>NM &&
+ read_tree_must_succeed -m $tree_O $tree_A $tree_B &&
+ check_result
+'
+
+test_expect_success '14 (fail) - must match and be up-to-date in O && A && B && O==A && O!=B case' '
+ rm -f .git/index NM &&
+ cp .orig-A/NM NM &&
+ git update-index --add NM &&
+ echo extra >>NM &&
+ read_tree_must_fail -m $tree_O $tree_A $tree_B
+'
+
+test_expect_success '14 (fail) - must match and be up-to-date in O && A && B && O==A && O!=B case' '
+ rm -f .git/index NM &&
+ cp .orig-A/NM NM &&
+ echo extra >>NM &&
+ git update-index --add NM &&
+ read_tree_must_fail -m $tree_O $tree_A $tree_B
+'
+
+test_expect_success '15 - must match A in O && A && B && O==A && O==B case' '
+ rm -f .git/index NN &&
+ cp .orig-A/NN NN &&
+ git update-index --add NN &&
+ read_tree_must_succeed -m $tree_O $tree_A $tree_B &&
+ check_result
+'
+
+test_expect_success '15 - must match A in O && A && B && O==A && O==B case' '
+ rm -f .git/index NN &&
+ cp .orig-A/NN NN &&
+ git update-index --add NN &&
+ echo extra >>NN &&
+ read_tree_must_succeed -m $tree_O $tree_A $tree_B &&
+ check_result
+'
+
+test_expect_success '15 (fail) - must match A in O && A && B && O==A && O==B case' '
+ rm -f .git/index NN &&
+ cp .orig-A/NN NN &&
+ echo extra >>NN &&
+ git update-index --add NN &&
+ read_tree_must_fail -m $tree_O $tree_A $tree_B
+'
+
+test_expect_success '16 - A matches in one and B matches in another.' '
+ rm -f .git/index F16 &&
+ echo F16 >F16 &&
+ git update-index --add F16 &&
+ tree0=$(git write-tree) &&
+ echo E16 >F16 &&
+ git update-index F16 &&
+ tree1=$(git write-tree) &&
+ read_tree_must_succeed -m $tree0 $tree1 $tree1 $tree0 &&
+ git ls-files --stage
+'
+
+test_done
diff --git a/t/t1001-read-tree-m-2way.sh b/t/t1001-read-tree-m-2way.sh
new file mode 100755
index 0000000..88c524f
--- /dev/null
+++ b/t/t1001-read-tree-m-2way.sh
@@ -0,0 +1,420 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+test_description='Two way merge with read-tree -m $H $M
+
+This test tries two-way merge (aka fast-forward with carry forward).
+
+There is the head (called H) and another commit (called M), which is
+simply ahead of H. The index and the work tree contains a state that
+is derived from H, but may also have local changes. This test checks
+all the combinations described in the two-tree merge "carry forward"
+rules, found in <Documentation/git read-tree.txt>.
+
+In the test, these paths are used:
+ bozbar - in H, stays in M, modified from bozbar to gnusto
+ frotz - not in H added in M
+ nitfol - in H, stays in M unmodified
+ rezrov - in H, deleted in M
+ yomin - not in H or M
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-read-tree.sh
+
+read_tree_twoway () {
+ git read-tree -m "$1" "$2" && git ls-files --stage
+}
+
+compare_change () {
+ sed -n >current \
+ -e '/^--- /d; /^+++ /d; /^@@ /d;' \
+ -e 's/^\([-+][0-7][0-7][0-7][0-7][0-7][0-7]\) '"$OID_REGEX"' /\1 X /p' \
+ "$1"
+ test_cmp expected current
+}
+
+check_cache_at () {
+ git diff-files -- "$1" >out &&
+ clean_if_empty=$(cat out) &&
+ case "$clean_if_empty" in
+ '') echo "$1: clean" ;;
+ ?*) echo "$1: dirty" ;;
+ esac &&
+ case "$2,$clean_if_empty" in
+ clean,) : ;;
+ clean,?*) false ;;
+ dirty,) false ;;
+ dirty,?*) : ;;
+ esac
+}
+
+cat >bozbar-old <<\EOF
+This is a sample file used in two-way fast-forward merge
+tests. Its second line ends with a magic word bozbar
+which will be modified by the merged head to gnusto.
+It has some extra lines so that external tools can
+successfully merge independent changes made to later
+lines (such as this one), avoiding line conflicts.
+EOF
+
+sed -e 's/bozbar/gnusto (earlier bozbar)/' bozbar-old >bozbar-new
+
+test_expect_success 'setup' '
+ echo frotz >frotz &&
+ echo nitfol >nitfol &&
+ cat bozbar-old >bozbar &&
+ echo rezrov >rezrov &&
+ echo yomin >yomin &&
+ git update-index --add nitfol bozbar rezrov &&
+ treeH=$(git write-tree) &&
+ echo treeH $treeH &&
+ git ls-tree $treeH &&
+
+ cat bozbar-new >bozbar &&
+ git update-index --add frotz bozbar --force-remove rezrov &&
+ git ls-files --stage >M.out &&
+ treeM=$(git write-tree) &&
+ echo treeM $treeM &&
+ git ls-tree $treeM &&
+ git diff-tree $treeH $treeM
+'
+
+test_expect_success '1, 2, 3 - no carry forward' '
+ rm -f .git/index &&
+ read_tree_twoway $treeH $treeM &&
+ git ls-files --stage >1-3.out &&
+ test_cmp M.out 1-3.out &&
+ check_cache_at bozbar dirty &&
+ check_cache_at frotz dirty &&
+ check_cache_at nitfol dirty
+'
+echo '+100644 X 0 yomin' >expected
+
+test_expect_success '4 - carry forward local addition.' '
+ rm -f .git/index &&
+ read_tree_must_succeed $treeH &&
+ git checkout-index -u -f -q -a &&
+ git update-index --add yomin &&
+ read_tree_twoway $treeH $treeM &&
+ git ls-files --stage >4.out &&
+ test_must_fail git diff --no-index M.out 4.out >4diff.out &&
+ compare_change 4diff.out expected &&
+ check_cache_at yomin clean
+'
+
+test_expect_success '5 - carry forward local addition.' '
+ rm -f .git/index &&
+ read_tree_must_succeed $treeH &&
+ git checkout-index -u -f -q -a &&
+ echo yomin >yomin &&
+ git update-index --add yomin &&
+ echo yomin yomin >yomin &&
+ read_tree_twoway $treeH $treeM &&
+ git ls-files --stage >5.out &&
+ test_must_fail git diff --no-index M.out 5.out >5diff.out &&
+ compare_change 5diff.out expected &&
+ check_cache_at yomin dirty
+'
+
+test_expect_success '6 - local addition already has the same.' '
+ rm -f .git/index &&
+ read_tree_must_succeed $treeH &&
+ git checkout-index -u -f -q -a &&
+ git update-index --add frotz &&
+ read_tree_twoway $treeH $treeM &&
+ git ls-files --stage >6.out &&
+ test_cmp M.out 6.out &&
+ check_cache_at frotz clean
+'
+
+test_expect_success '7 - local addition already has the same.' '
+ rm -f .git/index &&
+ read_tree_must_succeed $treeH &&
+ git checkout-index -u -f -q -a &&
+ echo frotz >frotz &&
+ git update-index --add frotz &&
+ echo frotz frotz >frotz &&
+ read_tree_twoway $treeH $treeM &&
+ git ls-files --stage >7.out &&
+ test_cmp M.out 7.out &&
+ check_cache_at frotz dirty
+'
+
+test_expect_success '8 - conflicting addition.' '
+ rm -f .git/index &&
+ read_tree_must_succeed $treeH &&
+ git checkout-index -u -f -q -a &&
+ echo frotz frotz >frotz &&
+ git update-index --add frotz &&
+ if read_tree_twoway $treeH $treeM; then false; else :; fi
+'
+
+test_expect_success '9 - conflicting addition.' '
+ rm -f .git/index &&
+ read_tree_must_succeed $treeH &&
+ git checkout-index -u -f -q -a &&
+ echo frotz frotz >frotz &&
+ git update-index --add frotz &&
+ echo frotz >frotz &&
+ if read_tree_twoway $treeH $treeM; then false; else :; fi
+'
+
+test_expect_success '10 - path removed.' '
+ rm -f .git/index &&
+ read_tree_must_succeed $treeH &&
+ git checkout-index -u -f -q -a &&
+ echo rezrov >rezrov &&
+ git update-index --add rezrov &&
+ read_tree_twoway $treeH $treeM &&
+ git ls-files --stage >10.out &&
+ test_cmp M.out 10.out
+'
+
+test_expect_success '11 - dirty path removed.' '
+ rm -f .git/index &&
+ read_tree_must_succeed $treeH &&
+ git checkout-index -u -f -q -a &&
+ echo rezrov >rezrov &&
+ git update-index --add rezrov &&
+ echo rezrov rezrov >rezrov &&
+ if read_tree_twoway $treeH $treeM; then false; else :; fi
+'
+
+test_expect_success '12 - unmatching local changes being removed.' '
+ rm -f .git/index &&
+ read_tree_must_succeed $treeH &&
+ git checkout-index -u -f -q -a &&
+ echo rezrov rezrov >rezrov &&
+ git update-index --add rezrov &&
+ if read_tree_twoway $treeH $treeM; then false; else :; fi
+'
+
+test_expect_success '13 - unmatching local changes being removed.' '
+ rm -f .git/index &&
+ read_tree_must_succeed $treeH &&
+ git checkout-index -u -f -q -a &&
+ echo rezrov rezrov >rezrov &&
+ git update-index --add rezrov &&
+ echo rezrov >rezrov &&
+ if read_tree_twoway $treeH $treeM; then false; else :; fi
+'
+
+cat >expected <<EOF
+-100644 X 0 nitfol
++100644 X 0 nitfol
+EOF
+
+test_expect_success '14 - unchanged in two heads.' '
+ rm -f .git/index &&
+ read_tree_must_succeed $treeH &&
+ git checkout-index -u -f -q -a &&
+ echo nitfol nitfol >nitfol &&
+ git update-index --add nitfol &&
+ read_tree_twoway $treeH $treeM &&
+ git ls-files --stage >14.out &&
+ test_must_fail git diff --no-index M.out 14.out >14diff.out &&
+ compare_change 14diff.out expected &&
+ check_cache_at nitfol clean
+'
+
+test_expect_success '15 - unchanged in two heads.' '
+ rm -f .git/index &&
+ read_tree_must_succeed $treeH &&
+ git checkout-index -u -f -q -a &&
+ echo nitfol nitfol >nitfol &&
+ git update-index --add nitfol &&
+ echo nitfol nitfol nitfol >nitfol &&
+ read_tree_twoway $treeH $treeM &&
+ git ls-files --stage >15.out &&
+ test_must_fail git diff --no-index M.out 15.out >15diff.out &&
+ compare_change 15diff.out expected &&
+ check_cache_at nitfol dirty
+'
+
+test_expect_success '16 - conflicting local change.' '
+ rm -f .git/index &&
+ read_tree_must_succeed $treeH &&
+ git checkout-index -u -f -q -a &&
+ echo bozbar bozbar >bozbar &&
+ git update-index --add bozbar &&
+ if read_tree_twoway $treeH $treeM; then false; else :; fi
+'
+
+test_expect_success '17 - conflicting local change.' '
+ rm -f .git/index &&
+ read_tree_must_succeed $treeH &&
+ git checkout-index -u -f -q -a &&
+ echo bozbar bozbar >bozbar &&
+ git update-index --add bozbar &&
+ echo bozbar bozbar bozbar >bozbar &&
+ if read_tree_twoway $treeH $treeM; then false; else :; fi
+'
+
+test_expect_success '18 - local change already having a good result.' '
+ rm -f .git/index &&
+ read_tree_must_succeed $treeH &&
+ git checkout-index -u -f -q -a &&
+ cat bozbar-new >bozbar &&
+ git update-index --add bozbar &&
+ read_tree_twoway $treeH $treeM &&
+ git ls-files --stage >18.out &&
+ test_cmp M.out 18.out &&
+ check_cache_at bozbar clean
+'
+
+test_expect_success '19 - local change already having a good result, further modified.' '
+ rm -f .git/index &&
+ read_tree_must_succeed $treeH &&
+ git checkout-index -u -f -q -a &&
+ cat bozbar-new >bozbar &&
+ git update-index --add bozbar &&
+ echo gnusto gnusto >bozbar &&
+ read_tree_twoway $treeH $treeM &&
+ git ls-files --stage >19.out &&
+ test_cmp M.out 19.out &&
+ check_cache_at bozbar dirty
+'
+
+test_expect_success '20 - no local change, use new tree.' '
+ rm -f .git/index &&
+ read_tree_must_succeed $treeH &&
+ git checkout-index -u -f -q -a &&
+ cat bozbar-old >bozbar &&
+ git update-index --add bozbar &&
+ read_tree_twoway $treeH $treeM &&
+ git ls-files --stage >20.out &&
+ test_cmp M.out 20.out &&
+ check_cache_at bozbar dirty
+'
+
+test_expect_success '21 - no local change, dirty cache.' '
+ rm -f .git/index &&
+ read_tree_must_succeed $treeH &&
+ git checkout-index -u -f -q -a &&
+ cat bozbar-old >bozbar &&
+ git update-index --add bozbar &&
+ echo gnusto gnusto >bozbar &&
+ if read_tree_twoway $treeH $treeM; then false; else :; fi
+'
+
+# This fails with straight two-way fast-forward.
+test_expect_success '22 - local change cache updated.' '
+ rm -f .git/index &&
+ read_tree_must_succeed $treeH &&
+ git checkout-index -u -f -q -a &&
+ sed -e "s/such as/SUCH AS/" bozbar-old >bozbar &&
+ git update-index --add bozbar &&
+ if read_tree_twoway $treeH $treeM; then false; else :; fi
+'
+
+# Also make sure we did not break DF vs DF/DF case.
+test_expect_success 'DF vs DF/DF case setup.' '
+ rm -f .git/index &&
+ echo DF >DF &&
+ git update-index --add DF &&
+ treeDF=$(git write-tree) &&
+ echo treeDF $treeDF &&
+ git ls-tree $treeDF &&
+
+ rm -f DF &&
+ mkdir DF &&
+ echo DF/DF >DF/DF &&
+ git update-index --add --remove DF DF/DF &&
+ treeDFDF=$(git write-tree) &&
+ echo treeDFDF $treeDFDF &&
+ git ls-tree $treeDFDF &&
+ git ls-files --stage >DFDF.out
+'
+
+test_expect_success 'DF vs DF/DF case test.' '
+ rm -f .git/index &&
+ rm -fr DF &&
+ echo DF >DF &&
+ git update-index --add DF &&
+ read_tree_twoway $treeDF $treeDFDF &&
+ git ls-files --stage >DFDFcheck.out &&
+ test_cmp DFDF.out DFDFcheck.out &&
+ check_cache_at DF/DF dirty &&
+ :
+'
+
+test_expect_success 'a/b (untracked) vs a case setup.' '
+ rm -f .git/index &&
+ : >a &&
+ git update-index --add a &&
+ treeM=$(git write-tree) &&
+ echo treeM $treeM &&
+ git ls-tree $treeM &&
+ git ls-files --stage >treeM.out &&
+
+ rm -f a &&
+ git update-index --remove a &&
+ mkdir a &&
+ : >a/b &&
+ treeH=$(git write-tree) &&
+ echo treeH $treeH &&
+ git ls-tree $treeH
+'
+
+test_expect_success 'a/b (untracked) vs a, plus c/d case test.' '
+ read_tree_u_must_fail -u -m "$treeH" "$treeM" &&
+ git ls-files --stage &&
+ test -f a/b
+'
+
+test_expect_success 'read-tree supports the super-prefix' '
+ cat <<-EOF >expect &&
+ error: Updating '\''fictional/a'\'' would lose untracked files in it
+ EOF
+ test_must_fail git read-tree --super-prefix fictional/ -u -m "$treeH" "$treeM" 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'a/b vs a, plus c/d case setup.' '
+ rm -f .git/index &&
+ rm -fr a &&
+ : >a &&
+ mkdir c &&
+ : >c/d &&
+ git update-index --add a c/d &&
+ treeM=$(git write-tree) &&
+ echo treeM $treeM &&
+ git ls-tree $treeM &&
+ git ls-files --stage >treeM.out &&
+
+ rm -f a &&
+ mkdir a &&
+ : >a/b &&
+ git update-index --add --remove a a/b &&
+ treeH=$(git write-tree) &&
+ echo treeH $treeH &&
+ git ls-tree $treeH
+'
+
+test_expect_success 'a/b vs a, plus c/d case test.' '
+ read_tree_u_must_succeed -u -m "$treeH" "$treeM" &&
+ git ls-files --stage | tee >treeMcheck.out &&
+ test_cmp treeM.out treeMcheck.out
+'
+
+test_expect_success '-m references the correct modified tree' '
+ echo >file-a &&
+ echo >file-b &&
+ git add file-a file-b &&
+ git commit -a -m "test for correct modified tree" &&
+ git branch initial-mod &&
+ echo b >file-b &&
+ git commit -a -m "B" &&
+ echo a >file-a &&
+ git add file-a &&
+ git ls-tree $(git write-tree) file-a >expect &&
+ read_tree_must_succeed -m HEAD initial-mod &&
+ git ls-tree $(git write-tree) file-a >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t1002-read-tree-m-u-2way.sh b/t/t1002-read-tree-m-u-2way.sh
new file mode 100755
index 0000000..a7c2ed0
--- /dev/null
+++ b/t/t1002-read-tree-m-u-2way.sh
@@ -0,0 +1,348 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+test_description='Two way merge with read-tree -m -u $H $M
+
+This is identical to t1001, but uses -u to update the work tree as well.
+
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-read-tree.sh
+
+compare_change () {
+ sed >current \
+ -e '1{/^diff --git /d;}' \
+ -e '2{/^index /d;}' \
+ -e '/^--- /d; /^+++ /d; /^@@ /d;' \
+ -e 's/^\(.[0-7][0-7][0-7][0-7][0-7][0-7]\) '"$OID_REGEX"' /\1 X /' "$1"
+ test_cmp expected current
+}
+
+check_cache_at () {
+ git diff-files -- "$1" >out &&
+ clean_if_empty=$(cat out) &&
+ case "$clean_if_empty" in
+ '') echo "$1: clean" ;;
+ ?*) echo "$1: dirty" ;;
+ esac &&
+ case "$2,$clean_if_empty" in
+ clean,) : ;;
+ clean,?*) false ;;
+ dirty,) false ;;
+ dirty,?*) : ;;
+ esac
+}
+
+test_expect_success setup '
+ echo frotz >frotz &&
+ echo nitfol >nitfol &&
+ echo bozbar >bozbar &&
+ echo rezrov >rezrov &&
+ git update-index --add nitfol bozbar rezrov &&
+ treeH=$(git write-tree) &&
+ echo treeH $treeH &&
+ git ls-tree $treeH &&
+
+ echo gnusto >bozbar &&
+ git update-index --add frotz bozbar --force-remove rezrov &&
+ git ls-files --stage >M.out &&
+ treeM=$(git write-tree) &&
+ echo treeM $treeM &&
+ git ls-tree $treeM &&
+ cp bozbar bozbar.M &&
+ cp frotz frotz.M &&
+ cp nitfol nitfol.M &&
+ git diff-tree $treeH $treeM
+'
+
+test_expect_success '1, 2, 3 - no carry forward' '
+ rm -f .git/index nitfol bozbar rezrov frotz &&
+ read_tree_u_must_succeed --reset -u $treeH &&
+ read_tree_u_must_succeed -m -u $treeH $treeM &&
+ git ls-files --stage >1-3.out &&
+ cmp M.out 1-3.out &&
+ test_cmp bozbar.M bozbar &&
+ test_cmp frotz.M frotz &&
+ test_cmp nitfol.M nitfol &&
+ check_cache_at bozbar clean &&
+ check_cache_at frotz clean &&
+ check_cache_at nitfol clean
+'
+
+test_expect_success '4 - carry forward local addition.' '
+ rm -f .git/index nitfol bozbar rezrov frotz &&
+ read_tree_u_must_succeed --reset -u $treeH &&
+ echo "+100644 X 0 yomin" >expected &&
+ echo yomin >yomin &&
+ git update-index --add yomin &&
+ read_tree_u_must_succeed -m -u $treeH $treeM &&
+ git ls-files --stage >4.out &&
+ test_might_fail git diff -U0 --no-index M.out 4.out >4diff.out &&
+ compare_change 4diff.out expected &&
+ check_cache_at yomin clean &&
+ test_cmp bozbar.M bozbar &&
+ test_cmp frotz.M frotz &&
+ test_cmp nitfol.M nitfol &&
+ echo yomin >yomin1 &&
+ diff yomin yomin1 &&
+ rm -f yomin1
+'
+
+test_expect_success '5 - carry forward local addition.' '
+ rm -f .git/index nitfol bozbar rezrov frotz &&
+ read_tree_u_must_succeed --reset -u $treeH &&
+ read_tree_u_must_succeed -m -u $treeH &&
+ echo yomin >yomin &&
+ git update-index --add yomin &&
+ echo yomin yomin >yomin &&
+ read_tree_u_must_succeed -m -u $treeH $treeM &&
+ git ls-files --stage >5.out &&
+ test_might_fail git diff -U0 --no-index M.out 5.out >5diff.out &&
+ compare_change 5diff.out expected &&
+ check_cache_at yomin dirty &&
+ test_cmp bozbar.M bozbar &&
+ test_cmp frotz.M frotz &&
+ test_cmp nitfol.M nitfol &&
+ : dirty index should have prevented -u from checking it out. &&
+ echo yomin yomin >yomin1 &&
+ diff yomin yomin1 &&
+ rm -f yomin1
+'
+
+test_expect_success '6 - local addition already has the same.' '
+ rm -f .git/index nitfol bozbar rezrov frotz &&
+ read_tree_u_must_succeed --reset -u $treeH &&
+ echo frotz >frotz &&
+ git update-index --add frotz &&
+ read_tree_u_must_succeed -m -u $treeH $treeM &&
+ git ls-files --stage >6.out &&
+ test_cmp M.out 6.out &&
+ check_cache_at frotz clean &&
+ test_cmp bozbar.M bozbar &&
+ test_cmp frotz.M frotz &&
+ test_cmp nitfol.M nitfol &&
+ echo frotz >frotz1 &&
+ diff frotz frotz1 &&
+ rm -f frotz1
+'
+
+test_expect_success '7 - local addition already has the same.' '
+ rm -f .git/index nitfol bozbar rezrov frotz &&
+ read_tree_u_must_succeed --reset -u $treeH &&
+ echo frotz >frotz &&
+ git update-index --add frotz &&
+ echo frotz frotz >frotz &&
+ read_tree_u_must_succeed -m -u $treeH $treeM &&
+ git ls-files --stage >7.out &&
+ test_cmp M.out 7.out &&
+ check_cache_at frotz dirty &&
+ test_cmp bozbar.M bozbar &&
+ test_cmp nitfol.M nitfol &&
+ : dirty index should have prevented -u from checking it out. &&
+ echo frotz frotz >frotz1 &&
+ diff frotz frotz1 &&
+ rm -f frotz1
+'
+
+test_expect_success '8 - conflicting addition.' '
+ rm -f .git/index nitfol bozbar rezrov frotz &&
+ read_tree_u_must_succeed --reset -u $treeH &&
+ echo frotz frotz >frotz &&
+ git update-index --add frotz &&
+ ! read_tree_u_must_succeed -m -u $treeH $treeM
+'
+
+test_expect_success '9 - conflicting addition.' '
+ rm -f .git/index nitfol bozbar rezrov frotz &&
+ read_tree_u_must_succeed --reset -u $treeH &&
+ echo frotz frotz >frotz &&
+ git update-index --add frotz &&
+ echo frotz >frotz &&
+ ! read_tree_u_must_succeed -m -u $treeH $treeM
+'
+
+test_expect_success '10 - path removed.' '
+ rm -f .git/index nitfol bozbar rezrov frotz &&
+ read_tree_u_must_succeed --reset -u $treeH &&
+ echo rezrov >rezrov &&
+ git update-index --add rezrov &&
+ read_tree_u_must_succeed -m -u $treeH $treeM &&
+ git ls-files --stage >10.out &&
+ cmp M.out 10.out &&
+ test_cmp bozbar.M bozbar &&
+ test_cmp frotz.M frotz &&
+ test_cmp nitfol.M nitfol
+'
+
+test_expect_success '11 - dirty path removed.' '
+ rm -f .git/index nitfol bozbar rezrov frotz &&
+ read_tree_u_must_succeed --reset -u $treeH &&
+ echo rezrov >rezrov &&
+ git update-index --add rezrov &&
+ echo rezrov rezrov >rezrov &&
+ ! read_tree_u_must_succeed -m -u $treeH $treeM
+'
+
+test_expect_success '12 - unmatching local changes being removed.' '
+ rm -f .git/index nitfol bozbar rezrov frotz &&
+ read_tree_u_must_succeed --reset -u $treeH &&
+ echo rezrov rezrov >rezrov &&
+ git update-index --add rezrov &&
+ ! read_tree_u_must_succeed -m -u $treeH $treeM
+'
+
+test_expect_success '13 - unmatching local changes being removed.' '
+ rm -f .git/index nitfol bozbar rezrov frotz &&
+ read_tree_u_must_succeed --reset -u $treeH &&
+ echo rezrov rezrov >rezrov &&
+ git update-index --add rezrov &&
+ echo rezrov >rezrov &&
+ ! read_tree_u_must_succeed -m -u $treeH $treeM
+'
+
+cat >expected <<EOF
+-100644 X 0 nitfol
++100644 X 0 nitfol
+EOF
+
+test_expect_success '14 - unchanged in two heads.' '
+ rm -f .git/index nitfol bozbar rezrov frotz &&
+ read_tree_u_must_succeed --reset -u $treeH &&
+ echo nitfol nitfol >nitfol &&
+ git update-index --add nitfol &&
+ read_tree_u_must_succeed -m -u $treeH $treeM &&
+ git ls-files --stage >14.out &&
+ test_must_fail git diff -U0 --no-index M.out 14.out >14diff.out &&
+ compare_change 14diff.out expected &&
+ test_cmp bozbar.M bozbar &&
+ test_cmp frotz.M frotz &&
+ check_cache_at nitfol clean &&
+ echo nitfol nitfol >nitfol1 &&
+ diff nitfol nitfol1 &&
+ rm -f nitfol1
+'
+
+test_expect_success '15 - unchanged in two heads.' '
+ rm -f .git/index nitfol bozbar rezrov frotz &&
+ read_tree_u_must_succeed --reset -u $treeH &&
+ echo nitfol nitfol >nitfol &&
+ git update-index --add nitfol &&
+ echo nitfol nitfol nitfol >nitfol &&
+ read_tree_u_must_succeed -m -u $treeH $treeM &&
+ git ls-files --stage >15.out &&
+ test_must_fail git diff -U0 --no-index M.out 15.out >15diff.out &&
+ compare_change 15diff.out expected &&
+ check_cache_at nitfol dirty &&
+ test_cmp bozbar.M bozbar &&
+ test_cmp frotz.M frotz &&
+ echo nitfol nitfol nitfol >nitfol1 &&
+ diff nitfol nitfol1 &&
+ rm -f nitfol1
+'
+
+test_expect_success '16 - conflicting local change.' '
+ rm -f .git/index nitfol bozbar rezrov frotz &&
+ read_tree_u_must_succeed --reset -u $treeH &&
+ echo bozbar bozbar >bozbar &&
+ git update-index --add bozbar &&
+ ! read_tree_u_must_succeed -m -u $treeH $treeM
+'
+
+test_expect_success '17 - conflicting local change.' '
+ rm -f .git/index nitfol bozbar rezrov frotz &&
+ read_tree_u_must_succeed --reset -u $treeH &&
+ echo bozbar bozbar >bozbar &&
+ git update-index --add bozbar &&
+ echo bozbar bozbar bozbar >bozbar &&
+ ! read_tree_u_must_succeed -m -u $treeH $treeM
+'
+
+test_expect_success '18 - local change already having a good result.' '
+ rm -f .git/index nitfol bozbar rezrov frotz &&
+ read_tree_u_must_succeed --reset -u $treeH &&
+ echo gnusto >bozbar &&
+ git update-index --add bozbar &&
+ read_tree_u_must_succeed -m -u $treeH $treeM &&
+ git ls-files --stage >18.out &&
+ test_cmp M.out 18.out &&
+ check_cache_at bozbar clean &&
+ test_cmp bozbar.M bozbar &&
+ test_cmp frotz.M frotz &&
+ test_cmp nitfol.M nitfol
+'
+
+test_expect_success '19 - local change already having a good result, further modified.' '
+ rm -f .git/index nitfol bozbar rezrov frotz &&
+ read_tree_u_must_succeed --reset -u $treeH &&
+ echo gnusto >bozbar &&
+ git update-index --add bozbar &&
+ echo gnusto gnusto >bozbar &&
+ read_tree_u_must_succeed -m -u $treeH $treeM &&
+ git ls-files --stage >19.out &&
+ test_cmp M.out 19.out &&
+ check_cache_at bozbar dirty &&
+ test_cmp frotz.M frotz &&
+ test_cmp nitfol.M nitfol &&
+ echo gnusto gnusto >bozbar1 &&
+ diff bozbar bozbar1 &&
+ rm -f bozbar1
+'
+
+test_expect_success '20 - no local change, use new tree.' '
+ rm -f .git/index nitfol bozbar rezrov frotz &&
+ read_tree_u_must_succeed --reset -u $treeH &&
+ echo bozbar >bozbar &&
+ git update-index --add bozbar &&
+ read_tree_u_must_succeed -m -u $treeH $treeM &&
+ git ls-files --stage >20.out &&
+ test_cmp M.out 20.out &&
+ check_cache_at bozbar clean &&
+ test_cmp bozbar.M bozbar &&
+ test_cmp frotz.M frotz &&
+ test_cmp nitfol.M nitfol
+'
+
+test_expect_success '21 - no local change, dirty cache.' '
+ rm -f .git/index nitfol bozbar rezrov frotz &&
+ read_tree_u_must_succeed --reset -u $treeH &&
+ echo bozbar >bozbar &&
+ git update-index --add bozbar &&
+ echo gnusto gnusto >bozbar &&
+ ! read_tree_u_must_succeed -m -u $treeH $treeM
+'
+
+# Also make sure we did not break DF vs DF/DF case.
+test_expect_success 'DF vs DF/DF case setup.' '
+ rm -f .git/index &&
+ echo DF >DF &&
+ git update-index --add DF &&
+ treeDF=$(git write-tree) &&
+ echo treeDF $treeDF &&
+ git ls-tree $treeDF &&
+
+ rm -f DF &&
+ mkdir DF &&
+ echo DF/DF >DF/DF &&
+ git update-index --add --remove DF DF/DF &&
+ treeDFDF=$(git write-tree) &&
+ echo treeDFDF $treeDFDF &&
+ git ls-tree $treeDFDF &&
+ git ls-files --stage >DFDF.out
+'
+
+test_expect_success 'DF vs DF/DF case test.' '
+ rm -f .git/index &&
+ rm -fr DF &&
+ echo DF >DF &&
+ git update-index --add DF &&
+ read_tree_u_must_succeed -m -u $treeDF $treeDFDF &&
+ git ls-files --stage >DFDFcheck.out &&
+ test_cmp DFDF.out DFDFcheck.out &&
+ check_cache_at DF/DF clean
+'
+
+test_done
diff --git a/t/t1003-read-tree-prefix.sh b/t/t1003-read-tree-prefix.sh
new file mode 100755
index 0000000..c860c08
--- /dev/null
+++ b/t/t1003-read-tree-prefix.sh
@@ -0,0 +1,38 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Junio C Hamano
+#
+
+test_description='git read-tree --prefix test.
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ echo hello >one &&
+ git update-index --add one &&
+ tree=$(git write-tree) &&
+ echo tree is $tree
+'
+
+echo 'one
+two/one' >expect
+
+test_expect_success 'read-tree --prefix' '
+ git read-tree --prefix=two/ $tree &&
+ git ls-files >actual &&
+ cmp expect actual
+'
+
+test_expect_success 'read-tree --prefix with leading slash exits with error' '
+ git rm -rf . &&
+ test_must_fail git read-tree --prefix=/two/ $tree &&
+ git read-tree --prefix=two/ $tree &&
+
+ git rm -rf . &&
+ test_must_fail git read-tree --prefix=/ $tree &&
+ git read-tree --prefix= $tree
+'
+
+test_done
diff --git a/t/t1004-read-tree-m-u-wf.sh b/t/t1004-read-tree-m-u-wf.sh
new file mode 100755
index 0000000..11bf104
--- /dev/null
+++ b/t/t1004-read-tree-m-u-wf.sh
@@ -0,0 +1,242 @@
+#!/bin/sh
+
+test_description='read-tree -m -u checks working tree files'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-read-tree.sh
+
+# two-tree test
+
+test_expect_success 'two-way setup' '
+
+ mkdir subdir &&
+ echo >file1 file one &&
+ echo >file2 file two &&
+ echo >subdir/file1 file one in subdirectory &&
+ echo >subdir/file2 file two in subdirectory &&
+ git update-index --add file1 file2 subdir/file1 subdir/file2 &&
+ git commit -m initial &&
+
+ git branch side &&
+ git tag -f branch-point &&
+
+ echo file2 is not tracked on the main branch anymore &&
+ rm -f file2 subdir/file2 &&
+ git update-index --remove file2 subdir/file2 &&
+ git commit -a -m "main removes file2 and subdir/file2"
+'
+
+test_expect_success 'two-way not clobbering' '
+
+ echo >file2 main creates untracked file2 &&
+ echo >subdir/file2 main creates untracked subdir/file2 &&
+ if err=$(read_tree_u_must_succeed -m -u main side 2>&1)
+ then
+ echo should have complained
+ false
+ else
+ echo "happy to see $err"
+ fi
+'
+
+echo file2 >.gitignore
+
+test_expect_success 'two-way with incorrect --exclude-per-directory (1)' '
+
+ if err=$(read_tree_u_must_succeed -m --exclude-per-directory=.gitignore main side 2>&1)
+ then
+ echo should have complained
+ false
+ else
+ echo "happy to see $err"
+ fi
+'
+
+test_expect_success 'two-way with incorrect --exclude-per-directory (2)' '
+
+ if err=$(read_tree_u_must_succeed -m -u --exclude-per-directory=foo --exclude-per-directory=.gitignore main side 2>&1)
+ then
+ echo should have complained
+ false
+ else
+ echo "happy to see $err"
+ fi
+'
+
+test_expect_success 'two-way clobbering a ignored file' '
+
+ read_tree_u_must_succeed -m -u --exclude-per-directory=.gitignore main side
+'
+
+rm -f .gitignore
+
+# three-tree test
+
+test_expect_success 'three-way not complaining on an untracked path in both' '
+
+ rm -f file2 subdir/file2 &&
+ git checkout side &&
+ echo >file3 file three &&
+ echo >subdir/file3 file three &&
+ git update-index --add file3 subdir/file3 &&
+ git commit -a -m "side adds file3 and removes file2" &&
+
+ git checkout main &&
+ echo >file2 file two is untracked on the main side &&
+ echo >subdir/file2 file two is untracked on the main side &&
+
+ read_tree_u_must_succeed -m -u branch-point main side
+'
+
+test_expect_success 'three-way not clobbering a working tree file' '
+
+ git reset --hard &&
+ rm -f file2 subdir/file2 file3 subdir/file3 &&
+ git checkout main &&
+ echo >file3 file three created in main, untracked &&
+ echo >subdir/file3 file three created in main, untracked &&
+ if err=$(read_tree_u_must_succeed -m -u branch-point main side 2>&1)
+ then
+ echo should have complained
+ false
+ else
+ echo "happy to see $err"
+ fi
+'
+
+echo >.gitignore file3
+
+test_expect_success 'three-way not complaining on an untracked file' '
+
+ git reset --hard &&
+ rm -f file2 subdir/file2 file3 subdir/file3 &&
+ git checkout main &&
+ echo >file3 file three created in main, untracked &&
+ echo >subdir/file3 file three created in main, untracked &&
+
+ read_tree_u_must_succeed -m -u --exclude-per-directory=.gitignore branch-point main side
+'
+
+test_expect_success '3-way not overwriting local changes (setup)' '
+
+ git reset --hard &&
+ git checkout -b side-a branch-point &&
+ echo >>file1 "new line to be kept in the merge result" &&
+ git commit -a -m "side-a changes file1" &&
+ git checkout -b side-b branch-point &&
+ echo >>file2 "new line to be kept in the merge result" &&
+ git commit -a -m "side-b changes file2" &&
+ git checkout side-a
+
+'
+
+test_expect_success '3-way not overwriting local changes (our side)' '
+
+ # At this point, file1 from side-a should be kept as side-b
+ # did not touch it.
+
+ git reset --hard &&
+
+ echo >>file1 "local changes" &&
+ read_tree_u_must_succeed -m -u branch-point side-a side-b &&
+ grep "new line to be kept" file1 &&
+ grep "local changes" file1
+
+'
+
+test_expect_success '3-way not overwriting local changes (their side)' '
+
+ # At this point, file2 from side-b should be taken as side-a
+ # did not touch it.
+
+ git reset --hard &&
+
+ echo >>file2 "local changes" &&
+ read_tree_u_must_fail -m -u branch-point side-a side-b &&
+ ! grep "new line to be kept" file2 &&
+ grep "local changes" file2
+
+'
+
+test_expect_success 'funny symlink in work tree' '
+
+ git reset --hard &&
+ git checkout -b sym-b side-b &&
+ mkdir -p a &&
+ >a/b &&
+ git add a/b &&
+ git commit -m "side adds a/b" &&
+
+ rm -fr a &&
+ git checkout -b sym-a side-a &&
+ mkdir -p a &&
+ test_ln_s_add ../b a/b &&
+ git commit -m "we add a/b" &&
+
+ read_tree_u_must_succeed -m -u sym-a sym-a sym-b
+
+'
+
+test_expect_success SANITY 'funny symlink in work tree, un-unlink-able' '
+
+ test_when_finished "chmod u+w a 2>/dev/null; rm -fr a b" &&
+
+ rm -fr a b &&
+ git reset --hard &&
+
+ git checkout sym-a &&
+ chmod a-w a &&
+ test_must_fail git read-tree -m -u sym-a sym-a sym-b
+
+'
+
+test_expect_success 'D/F setup' '
+
+ git reset --hard &&
+
+ git checkout side-a &&
+ rm -f subdir/file2 &&
+ mkdir subdir/file2 &&
+ echo qfwfq >subdir/file2/another &&
+ git add subdir/file2/another &&
+ test_tick &&
+ git commit -m "side-a changes file2 to directory"
+
+'
+
+test_expect_success 'D/F' '
+
+ git checkout side-b &&
+ read_tree_u_must_succeed -m -u branch-point side-b side-a &&
+ git ls-files -u >actual &&
+ (
+ a=$(git rev-parse branch-point:subdir/file2) &&
+ b=$(git rev-parse side-a:subdir/file2/another) &&
+ echo "100644 $a 1 subdir/file2" &&
+ echo "100644 $a 2 subdir/file2" &&
+ echo "100644 $b 3 subdir/file2/another"
+ ) >expect &&
+ test_cmp expect actual
+
+'
+
+test_expect_success 'D/F resolve' '
+
+ git reset --hard &&
+ git checkout side-b &&
+ git merge-resolve branch-point -- side-b side-a
+
+'
+
+test_expect_success 'D/F recursive' '
+
+ git reset --hard &&
+ git checkout side-b &&
+ git merge-recursive branch-point -- side-b side-a
+
+'
+
+test_done
diff --git a/t/t1005-read-tree-reset.sh b/t/t1005-read-tree-reset.sh
new file mode 100755
index 0000000..26be4a2
--- /dev/null
+++ b/t/t1005-read-tree-reset.sh
@@ -0,0 +1,112 @@
+#!/bin/sh
+
+test_description='read-tree -u --reset'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-read-tree.sh
+
+# two-tree test
+
+test_expect_success 'setup' '
+ git init &&
+ mkdir df &&
+ echo content >df/file &&
+ git add df/file &&
+ git commit -m one &&
+ git ls-files >expect &&
+ rm -rf df &&
+ echo content >df &&
+ git add df &&
+ echo content >new &&
+ git add new &&
+ git commit -m two
+'
+
+test_expect_success 'reset should work' '
+ read_tree_u_must_succeed -u --reset HEAD^ &&
+ git ls-files >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'reset should remove remnants from a failed merge' '
+ read_tree_u_must_succeed --reset -u HEAD &&
+ git ls-files -s >expect &&
+ sha1=$(git rev-parse :new) &&
+ (
+ echo "100644 $sha1 1 old" &&
+ echo "100644 $sha1 3 old"
+ ) | git update-index --index-info &&
+ >old &&
+ git ls-files -s &&
+ read_tree_u_must_succeed --reset -u HEAD &&
+ git ls-files -s >actual &&
+ ! test -f old &&
+ test_cmp expect actual
+'
+
+test_expect_success 'two-way reset should remove remnants too' '
+ read_tree_u_must_succeed --reset -u HEAD &&
+ git ls-files -s >expect &&
+ sha1=$(git rev-parse :new) &&
+ (
+ echo "100644 $sha1 1 old" &&
+ echo "100644 $sha1 3 old"
+ ) | git update-index --index-info &&
+ >old &&
+ git ls-files -s &&
+ read_tree_u_must_succeed --reset -u HEAD HEAD &&
+ git ls-files -s >actual &&
+ ! test -f old &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Porcelain reset should remove remnants too' '
+ read_tree_u_must_succeed --reset -u HEAD &&
+ git ls-files -s >expect &&
+ sha1=$(git rev-parse :new) &&
+ (
+ echo "100644 $sha1 1 old" &&
+ echo "100644 $sha1 3 old"
+ ) | git update-index --index-info &&
+ >old &&
+ git ls-files -s &&
+ git reset --hard &&
+ git ls-files -s >actual &&
+ ! test -f old &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Porcelain checkout -f should remove remnants too' '
+ read_tree_u_must_succeed --reset -u HEAD &&
+ git ls-files -s >expect &&
+ sha1=$(git rev-parse :new) &&
+ (
+ echo "100644 $sha1 1 old" &&
+ echo "100644 $sha1 3 old"
+ ) | git update-index --index-info &&
+ >old &&
+ git ls-files -s &&
+ git checkout -f &&
+ git ls-files -s >actual &&
+ ! test -f old &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Porcelain checkout -f HEAD should remove remnants too' '
+ read_tree_u_must_succeed --reset -u HEAD &&
+ git ls-files -s >expect &&
+ sha1=$(git rev-parse :new) &&
+ (
+ echo "100644 $sha1 1 old" &&
+ echo "100644 $sha1 3 old"
+ ) | git update-index --index-info &&
+ >old &&
+ git ls-files -s &&
+ git checkout -f HEAD &&
+ git ls-files -s >actual &&
+ ! test -f old &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t1006-cat-file.sh b/t/t1006-cat-file.sh
new file mode 100755
index 0000000..d73a0be
--- /dev/null
+++ b/t/t1006-cat-file.sh
@@ -0,0 +1,1204 @@
+#!/bin/sh
+
+test_description='git cat-file'
+
+. ./test-lib.sh
+
+test_cmdmode_usage () {
+ test_expect_code 129 "$@" 2>err &&
+ grep "^error:.*is incompatible with" err
+}
+
+for switches in \
+ '-e -p' \
+ '-p -t' \
+ '-t -s' \
+ '-s --textconv' \
+ '--textconv --filters' \
+ '--batch-all-objects -e'
+do
+ test_expect_success "usage: cmdmode $switches" '
+ test_cmdmode_usage git cat-file $switches
+ '
+done
+
+test_incompatible_usage () {
+ test_expect_code 129 "$@" 2>err &&
+ grep -E "^(fatal|error):.*(requires|incompatible with|needs)" err
+}
+
+for opt in --batch --batch-check
+do
+ test_expect_success "usage: incompatible options: --path with $opt" '
+ test_incompatible_usage git cat-file --path=foo $opt
+ '
+done
+
+test_missing_usage () {
+ test_expect_code 129 "$@" 2>err &&
+ grep -E "^fatal:.*required" err
+}
+
+short_modes="-e -p -t -s"
+cw_modes="--textconv --filters"
+
+for opt in $cw_modes
+do
+ test_expect_success "usage: $opt requires another option" '
+ test_missing_usage git cat-file $opt
+ '
+done
+
+for opt in $short_modes
+do
+ test_expect_success "usage: $opt requires another option" '
+ test_missing_usage git cat-file $opt
+ '
+
+ for opt2 in --batch \
+ --batch-check \
+ --follow-symlinks \
+ "--path=foo HEAD:some-path.txt"
+ do
+ test_expect_success "usage: incompatible options: $opt and $opt2" '
+ test_incompatible_usage git cat-file $opt $opt2
+ '
+ done
+done
+
+test_too_many_arguments () {
+ test_expect_code 129 "$@" 2>err &&
+ grep -E "^fatal: too many arguments$" err
+}
+
+for opt in $short_modes $cw_modes
+do
+ args="one two three"
+ test_expect_success "usage: too many arguments: $opt $args" '
+ test_too_many_arguments git cat-file $opt $args
+ '
+
+ for opt2 in --buffer --follow-symlinks
+ do
+ test_expect_success "usage: incompatible arguments: $opt with batch option $opt2" '
+ test_incompatible_usage git cat-file $opt $opt2
+ '
+ done
+done
+
+for opt in --buffer \
+ --follow-symlinks \
+ --batch-all-objects \
+ -z \
+ -Z
+do
+ test_expect_success "usage: bad option combination: $opt without batch mode" '
+ test_incompatible_usage git cat-file $opt &&
+ test_incompatible_usage git cat-file $opt commit HEAD
+ '
+done
+
+echo_without_newline () {
+ printf '%s' "$*"
+}
+
+echo_without_newline_nul () {
+ echo_without_newline "$@" | tr '\n' '\0'
+}
+
+strlen () {
+ echo_without_newline "$1" | wc -c | sed -e 's/^ *//'
+}
+
+run_tests () {
+ type=$1
+ sha1=$2
+ size=$3
+ content=$4
+ pretty_content=$5
+
+ batch_output="$sha1 $type $size
+$content"
+
+ test_expect_success "$type exists" '
+ git cat-file -e $sha1
+ '
+
+ test_expect_success "Type of $type is correct" '
+ echo $type >expect &&
+ git cat-file -t $sha1 >actual &&
+ test_cmp expect actual
+ '
+
+ test_expect_success "Size of $type is correct" '
+ echo $size >expect &&
+ git cat-file -s $sha1 >actual &&
+ test_cmp expect actual
+ '
+
+ test_expect_success "Type of $type is correct using --allow-unknown-type" '
+ echo $type >expect &&
+ git cat-file -t --allow-unknown-type $sha1 >actual &&
+ test_cmp expect actual
+ '
+
+ test_expect_success "Size of $type is correct using --allow-unknown-type" '
+ echo $size >expect &&
+ git cat-file -s --allow-unknown-type $sha1 >actual &&
+ test_cmp expect actual
+ '
+
+ test -z "$content" ||
+ test_expect_success "Content of $type is correct" '
+ echo_without_newline "$content" >expect &&
+ git cat-file $type $sha1 >actual &&
+ test_cmp expect actual
+ '
+
+ test_expect_success "Pretty content of $type is correct" '
+ echo_without_newline "$pretty_content" >expect &&
+ git cat-file -p $sha1 >actual &&
+ test_cmp expect actual
+ '
+
+ test -z "$content" ||
+ test_expect_success "--batch output of $type is correct" '
+ echo "$batch_output" >expect &&
+ echo $sha1 | git cat-file --batch >actual &&
+ test_cmp expect actual
+ '
+
+ test_expect_success "--batch-check output of $type is correct" '
+ echo "$sha1 $type $size" >expect &&
+ echo_without_newline $sha1 | git cat-file --batch-check >actual &&
+ test_cmp expect actual
+ '
+
+ for opt in --buffer --no-buffer
+ do
+ test -z "$content" ||
+ test_expect_success "--batch-command $opt output of $type content is correct" '
+ echo "$batch_output" >expect &&
+ test_write_lines "contents $sha1" | git cat-file --batch-command $opt >actual &&
+ test_cmp expect actual
+ '
+
+ test_expect_success "--batch-command $opt output of $type info is correct" '
+ echo "$sha1 $type $size" >expect &&
+ test_write_lines "info $sha1" |
+ git cat-file --batch-command $opt >actual &&
+ test_cmp expect actual
+ '
+ done
+
+ test_expect_success "custom --batch-check format" '
+ echo "$type $sha1" >expect &&
+ echo $sha1 | git cat-file --batch-check="%(objecttype) %(objectname)" >actual &&
+ test_cmp expect actual
+ '
+
+ test_expect_success "custom --batch-command format" '
+ echo "$type $sha1" >expect &&
+ echo "info $sha1" | git cat-file --batch-command="%(objecttype) %(objectname)" >actual &&
+ test_cmp expect actual
+ '
+
+ test_expect_success '--batch-check with %(rest)' '
+ echo "$type this is some extra content" >expect &&
+ echo "$sha1 this is some extra content" |
+ git cat-file --batch-check="%(objecttype) %(rest)" >actual &&
+ test_cmp expect actual
+ '
+
+ test -z "$content" ||
+ test_expect_success "--batch without type ($type)" '
+ {
+ echo "$size" &&
+ echo "$content"
+ } >expect &&
+ echo $sha1 | git cat-file --batch="%(objectsize)" >actual &&
+ test_cmp expect actual
+ '
+
+ test -z "$content" ||
+ test_expect_success "--batch without size ($type)" '
+ {
+ echo "$type" &&
+ echo "$content"
+ } >expect &&
+ echo $sha1 | git cat-file --batch="%(objecttype)" >actual &&
+ test_cmp expect actual
+ '
+}
+
+hello_content="Hello World"
+hello_size=$(strlen "$hello_content")
+hello_sha1=$(echo_without_newline "$hello_content" | git hash-object --stdin)
+
+test_expect_success "setup" '
+ echo_without_newline "$hello_content" > hello &&
+ git update-index --add hello
+'
+
+run_tests 'blob' $hello_sha1 $hello_size "$hello_content" "$hello_content"
+
+test_expect_success '--batch-command --buffer with flush for blob info' '
+ echo "$hello_sha1 blob $hello_size" >expect &&
+ test_write_lines "info $hello_sha1" "flush" |
+ GIT_TEST_CAT_FILE_NO_FLUSH_ON_EXIT=1 \
+ git cat-file --batch-command --buffer >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--batch-command --buffer without flush for blob info' '
+ touch output &&
+ test_write_lines "info $hello_sha1" |
+ GIT_TEST_CAT_FILE_NO_FLUSH_ON_EXIT=1 \
+ git cat-file --batch-command --buffer >>output &&
+ test_must_be_empty output
+'
+
+test_expect_success '--batch-check without %(rest) considers whole line' '
+ echo "$hello_sha1 blob $hello_size" >expect &&
+ git update-index --add --cacheinfo 100644 $hello_sha1 "white space" &&
+ test_when_finished "git update-index --remove \"white space\"" &&
+ echo ":white space" | git cat-file --batch-check >actual &&
+ test_cmp expect actual
+'
+
+tree_sha1=$(git write-tree)
+tree_size=$(($(test_oid rawsz) + 13))
+tree_pretty_content="100644 blob $hello_sha1 hello${LF}"
+
+run_tests 'tree' $tree_sha1 $tree_size "" "$tree_pretty_content"
+
+commit_message="Initial commit"
+commit_sha1=$(echo_without_newline "$commit_message" | git commit-tree $tree_sha1)
+commit_size=$(($(test_oid hexsz) + 137))
+commit_content="tree $tree_sha1
+author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE
+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+
+$commit_message"
+
+run_tests 'commit' $commit_sha1 $commit_size "$commit_content" "$commit_content"
+
+tag_header_without_timestamp="object $hello_sha1
+type blob
+tag hellotag
+tagger $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
+tag_description="This is a tag"
+tag_content="$tag_header_without_timestamp 0 +0000
+
+$tag_description"
+
+tag_sha1=$(echo_without_newline "$tag_content" | git hash-object -t tag --stdin -w)
+tag_size=$(strlen "$tag_content")
+
+run_tests 'tag' $tag_sha1 $tag_size "$tag_content" "$tag_content"
+
+test_expect_success "Reach a blob from a tag pointing to it" '
+ echo_without_newline "$hello_content" >expect &&
+ git cat-file blob $tag_sha1 >actual &&
+ test_cmp expect actual
+'
+
+for batch in batch batch-check batch-command
+do
+ for opt in t s e p
+ do
+ test_expect_success "Passing -$opt with --$batch fails" '
+ test_must_fail git cat-file --$batch -$opt $hello_sha1
+ '
+
+ test_expect_success "Passing --$batch with -$opt fails" '
+ test_must_fail git cat-file -$opt --$batch $hello_sha1
+ '
+ done
+
+ test_expect_success "Passing <type> with --$batch fails" '
+ test_must_fail git cat-file --$batch blob $hello_sha1
+ '
+
+ test_expect_success "Passing --$batch with <type> fails" '
+ test_must_fail git cat-file blob --$batch $hello_sha1
+ '
+
+ test_expect_success "Passing sha1 with --$batch fails" '
+ test_must_fail git cat-file --$batch $hello_sha1
+ '
+done
+
+for opt in t s e p
+do
+ test_expect_success "Passing -$opt with --follow-symlinks fails" '
+ test_must_fail git cat-file --follow-symlinks -$opt $hello_sha1
+ '
+done
+
+test_expect_success "--batch-check for a non-existent named object" '
+ cat >expect <<-EOF &&
+ foobar42 missing
+ foobar84 missing
+ EOF
+
+ printf "foobar42\nfoobar84" >in &&
+ git cat-file --batch-check <in >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success "--batch-check for a non-existent hash" '
+ cat >expect <<-EOF &&
+ 0000000000000000000000000000000000000042 missing
+ 0000000000000000000000000000000000000084 missing
+ EOF
+
+ printf "0000000000000000000000000000000000000042\n0000000000000000000000000000000000000084" >in &&
+ git cat-file --batch-check <in >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success "--batch for an existent and a non-existent hash" '
+ cat >expect <<-EOF &&
+ $tag_sha1 tag $tag_size
+ $tag_content
+ 0000000000000000000000000000000000000000 missing
+ EOF
+
+ printf "$tag_sha1\n0000000000000000000000000000000000000000" >in &&
+ git cat-file --batch <in >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success "--batch-check for an empty line" '
+ cat >expect <<-EOF &&
+ missing
+ EOF
+
+ echo >in &&
+ git cat-file --batch-check <in >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'empty --batch-check notices missing object' '
+ echo "$ZERO_OID missing" >expect &&
+ echo "$ZERO_OID" | git cat-file --batch-check="" >actual &&
+ test_cmp expect actual
+'
+
+batch_input="$hello_sha1
+$commit_sha1
+$tag_sha1
+deadbeef
+
+"
+
+printf "%s\0" \
+ "$hello_sha1 blob $hello_size" \
+ "$hello_content" \
+ "$commit_sha1 commit $commit_size" \
+ "$commit_content" \
+ "$tag_sha1 tag $tag_size" \
+ "$tag_content" \
+ "deadbeef missing" \
+ " missing" >batch_output
+
+test_expect_success '--batch with multiple sha1s gives correct format' '
+ tr "\0" "\n" <batch_output >expect &&
+ echo_without_newline "$batch_input" >in &&
+ git cat-file --batch <in >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--batch, -z with multiple sha1s gives correct format' '
+ echo_without_newline_nul "$batch_input" >in &&
+ tr "\0" "\n" <batch_output >expect &&
+ git cat-file --batch -z <in >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--batch, -Z with multiple sha1s gives correct format' '
+ echo_without_newline_nul "$batch_input" >in &&
+ git cat-file --batch -Z <in >actual &&
+ test_cmp batch_output actual
+'
+
+batch_check_input="$hello_sha1
+$tree_sha1
+$commit_sha1
+$tag_sha1
+deadbeef
+
+"
+
+printf "%s\0" \
+ "$hello_sha1 blob $hello_size" \
+ "$tree_sha1 tree $tree_size" \
+ "$commit_sha1 commit $commit_size" \
+ "$tag_sha1 tag $tag_size" \
+ "deadbeef missing" \
+ " missing" >batch_check_output
+
+test_expect_success "--batch-check with multiple sha1s gives correct format" '
+ tr "\0" "\n" <batch_check_output >expect &&
+ echo_without_newline "$batch_check_input" >in &&
+ git cat-file --batch-check <in >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success "--batch-check, -z with multiple sha1s gives correct format" '
+ tr "\0" "\n" <batch_check_output >expect &&
+ echo_without_newline_nul "$batch_check_input" >in &&
+ git cat-file --batch-check -z <in >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success "--batch-check, -Z with multiple sha1s gives correct format" '
+ echo_without_newline_nul "$batch_check_input" >in &&
+ git cat-file --batch-check -Z <in >actual &&
+ test_cmp batch_check_output actual
+'
+
+test_expect_success FUNNYNAMES 'setup with newline in input' '
+ touch -- "newline${LF}embedded" &&
+ git add -- "newline${LF}embedded" &&
+ git commit -m "file with newline embedded" &&
+ test_tick &&
+
+ printf "HEAD:newline${LF}embedded" >in
+'
+
+test_expect_success FUNNYNAMES '--batch-check, -z with newline in input' '
+ git cat-file --batch-check -z <in >actual &&
+ echo "$(git rev-parse "HEAD:newline${LF}embedded") blob 0" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success FUNNYNAMES '--batch-check, -Z with newline in input' '
+ git cat-file --batch-check -Z <in >actual &&
+ printf "%s\0" "$(git rev-parse "HEAD:newline${LF}embedded") blob 0" >expect &&
+ test_cmp expect actual
+'
+
+batch_command_multiple_info="info $hello_sha1
+info $tree_sha1
+info $commit_sha1
+info $tag_sha1
+info deadbeef"
+
+test_expect_success '--batch-command with multiple info calls gives correct format' '
+ cat >expect <<-EOF &&
+ $hello_sha1 blob $hello_size
+ $tree_sha1 tree $tree_size
+ $commit_sha1 commit $commit_size
+ $tag_sha1 tag $tag_size
+ deadbeef missing
+ EOF
+
+ echo "$batch_command_multiple_info" >in &&
+ git cat-file --batch-command --buffer <in >actual &&
+
+ test_cmp expect actual &&
+
+ echo "$batch_command_multiple_info" | tr "\n" "\0" >in &&
+ git cat-file --batch-command --buffer -z <in >actual &&
+
+ test_cmp expect actual &&
+
+ echo "$batch_command_multiple_info" | tr "\n" "\0" >in &&
+ tr "\n" "\0" <expect >expect_nul &&
+ git cat-file --batch-command --buffer -Z <in >actual &&
+
+ test_cmp expect_nul actual
+'
+
+batch_command_multiple_contents="contents $hello_sha1
+contents $commit_sha1
+contents $tag_sha1
+contents deadbeef
+flush"
+
+test_expect_success '--batch-command with multiple command calls gives correct format' '
+ printf "%s\0" \
+ "$hello_sha1 blob $hello_size" \
+ "$hello_content" \
+ "$commit_sha1 commit $commit_size" \
+ "$commit_content" \
+ "$tag_sha1 tag $tag_size" \
+ "$tag_content" \
+ "deadbeef missing" >expect_nul &&
+ tr "\0" "\n" <expect_nul >expect &&
+
+ echo "$batch_command_multiple_contents" >in &&
+ git cat-file --batch-command --buffer <in >actual &&
+
+ test_cmp expect actual &&
+
+ echo "$batch_command_multiple_contents" | tr "\n" "\0" >in &&
+ git cat-file --batch-command --buffer -z <in >actual &&
+
+ test_cmp expect actual &&
+
+ echo "$batch_command_multiple_contents" | tr "\n" "\0" >in &&
+ git cat-file --batch-command --buffer -Z <in >actual &&
+
+ test_cmp expect_nul actual
+'
+
+test_expect_success 'setup blobs which are likely to delta' '
+ test-tool genrandom foo 10240 >foo &&
+ { cat foo && echo plus; } >foo-plus &&
+ git add foo foo-plus &&
+ git commit -m foo &&
+ cat >blobs <<-\EOF
+ HEAD:foo
+ HEAD:foo-plus
+ EOF
+'
+
+test_expect_success 'confirm that neither loose blob is a delta' '
+ cat >expect <<-EOF &&
+ $ZERO_OID
+ $ZERO_OID
+ EOF
+ git cat-file --batch-check="%(deltabase)" <blobs >actual &&
+ test_cmp expect actual
+'
+
+# To avoid relying too much on the current delta heuristics,
+# we will check only that one of the two objects is a delta
+# against the other, but not the order. We can do so by just
+# asking for the base of both, and checking whether either
+# sha1 appears in the output.
+test_expect_success '%(deltabase) reports packed delta bases' '
+ git repack -ad &&
+ git cat-file --batch-check="%(deltabase)" <blobs >actual &&
+ {
+ grep "$(git rev-parse HEAD:foo)" actual ||
+ grep "$(git rev-parse HEAD:foo-plus)" actual
+ }
+'
+
+test_expect_success 'setup bogus data' '
+ bogus_short_type="bogus" &&
+ bogus_short_content="bogus" &&
+ bogus_short_size=$(strlen "$bogus_short_content") &&
+ bogus_short_sha1=$(echo_without_newline "$bogus_short_content" | git hash-object -t $bogus_short_type --literally -w --stdin) &&
+
+ bogus_long_type="abcdefghijklmnopqrstuvwxyz1234679" &&
+ bogus_long_content="bogus" &&
+ bogus_long_size=$(strlen "$bogus_long_content") &&
+ bogus_long_sha1=$(echo_without_newline "$bogus_long_content" | git hash-object -t $bogus_long_type --literally -w --stdin)
+'
+
+for arg1 in '' --allow-unknown-type
+do
+ for arg2 in -s -t -p
+ do
+ if test "$arg1" = "--allow-unknown-type" && test "$arg2" = "-p"
+ then
+ continue
+ fi
+
+
+ test_expect_success "cat-file $arg1 $arg2 error on bogus short OID" '
+ cat >expect <<-\EOF &&
+ fatal: invalid object type
+ EOF
+
+ if test "$arg1" = "--allow-unknown-type"
+ then
+ git cat-file $arg1 $arg2 $bogus_short_sha1
+ else
+ test_must_fail git cat-file $arg1 $arg2 $bogus_short_sha1 >out 2>actual &&
+ test_must_be_empty out &&
+ test_cmp expect actual
+ fi
+ '
+
+ test_expect_success "cat-file $arg1 $arg2 error on bogus full OID" '
+ if test "$arg2" = "-p"
+ then
+ cat >expect <<-EOF
+ error: header for $bogus_long_sha1 too long, exceeds 32 bytes
+ fatal: Not a valid object name $bogus_long_sha1
+ EOF
+ else
+ cat >expect <<-EOF
+ error: header for $bogus_long_sha1 too long, exceeds 32 bytes
+ fatal: git cat-file: could not get object info
+ EOF
+ fi &&
+
+ if test "$arg1" = "--allow-unknown-type"
+ then
+ git cat-file $arg1 $arg2 $bogus_short_sha1
+ else
+ test_must_fail git cat-file $arg1 $arg2 $bogus_long_sha1 >out 2>actual &&
+ test_must_be_empty out &&
+ test_cmp expect actual
+ fi
+ '
+
+ test_expect_success "cat-file $arg1 $arg2 error on missing short OID" '
+ cat >expect.err <<-EOF &&
+ fatal: Not a valid object name $(test_oid deadbeef_short)
+ EOF
+ test_must_fail git cat-file $arg1 $arg2 $(test_oid deadbeef_short) >out 2>err.actual &&
+ test_must_be_empty out &&
+ test_cmp expect.err err.actual
+ '
+
+ test_expect_success "cat-file $arg1 $arg2 error on missing full OID" '
+ if test "$arg2" = "-p"
+ then
+ cat >expect.err <<-EOF
+ fatal: Not a valid object name $(test_oid deadbeef)
+ EOF
+ else
+ cat >expect.err <<-\EOF
+ fatal: git cat-file: could not get object info
+ EOF
+ fi &&
+ test_must_fail git cat-file $arg1 $arg2 $(test_oid deadbeef) >out 2>err.actual &&
+ test_must_be_empty out &&
+ test_cmp expect.err err.actual
+ '
+ done
+done
+
+test_expect_success '-e is OK with a broken object without --allow-unknown-type' '
+ git cat-file -e $bogus_short_sha1
+'
+
+test_expect_success '-e can not be combined with --allow-unknown-type' '
+ test_expect_code 128 git cat-file -e --allow-unknown-type $bogus_short_sha1
+'
+
+test_expect_success '-p cannot print a broken object even with --allow-unknown-type' '
+ test_must_fail git cat-file -p $bogus_short_sha1 &&
+ test_expect_code 128 git cat-file -p --allow-unknown-type $bogus_short_sha1
+'
+
+test_expect_success '<type> <hash> does not work with objects of broken types' '
+ cat >err.expect <<-\EOF &&
+ fatal: invalid object type "bogus"
+ EOF
+ test_must_fail git cat-file $bogus_short_type $bogus_short_sha1 2>err.actual &&
+ test_cmp err.expect err.actual
+'
+
+test_expect_success 'broken types combined with --batch and --batch-check' '
+ echo $bogus_short_sha1 >bogus-oid &&
+
+ cat >err.expect <<-\EOF &&
+ fatal: invalid object type
+ EOF
+
+ test_must_fail git cat-file --batch <bogus-oid 2>err.actual &&
+ test_cmp err.expect err.actual &&
+
+ test_must_fail git cat-file --batch-check <bogus-oid 2>err.actual &&
+ test_cmp err.expect err.actual
+'
+
+test_expect_success 'the --batch and --batch-check options do not combine with --allow-unknown-type' '
+ test_expect_code 128 git cat-file --batch --allow-unknown-type <bogus-oid &&
+ test_expect_code 128 git cat-file --batch-check --allow-unknown-type <bogus-oid
+'
+
+test_expect_success 'the --allow-unknown-type option does not consider replacement refs' '
+ cat >expect <<-EOF &&
+ $bogus_short_type
+ EOF
+ git cat-file -t --allow-unknown-type $bogus_short_sha1 >actual &&
+ test_cmp expect actual &&
+
+ # Create it manually, as "git replace" will die on bogus
+ # types.
+ head=$(git rev-parse --verify HEAD) &&
+ test_when_finished "test-tool ref-store main delete-refs 0 msg refs/replace/$bogus_short_sha1" &&
+ test-tool ref-store main update-ref msg "refs/replace/$bogus_short_sha1" $head $ZERO_OID REF_SKIP_OID_VERIFICATION &&
+
+ cat >expect <<-EOF &&
+ commit
+ EOF
+ git cat-file -t --allow-unknown-type $bogus_short_sha1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success "Type of broken object is correct" '
+ echo $bogus_short_type >expect &&
+ git cat-file -t --allow-unknown-type $bogus_short_sha1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success "Size of broken object is correct" '
+ echo $bogus_short_size >expect &&
+ git cat-file -s --allow-unknown-type $bogus_short_sha1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'clean up broken object' '
+ rm .git/objects/$(test_oid_to_path $bogus_short_sha1)
+'
+
+test_expect_success "Type of broken object is correct when type is large" '
+ echo $bogus_long_type >expect &&
+ git cat-file -t --allow-unknown-type $bogus_long_sha1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success "Size of large broken object is correct when type is large" '
+ echo $bogus_long_size >expect &&
+ git cat-file -s --allow-unknown-type $bogus_long_sha1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'clean up broken object' '
+ rm .git/objects/$(test_oid_to_path $bogus_long_sha1)
+'
+
+test_expect_success 'cat-file -t and -s on corrupt loose object' '
+ git init --bare corrupt-loose.git &&
+ (
+ cd corrupt-loose.git &&
+
+ # Setup and create the empty blob and its path
+ empty_path=$(git rev-parse --git-path objects/$(test_oid_to_path "$EMPTY_BLOB")) &&
+ empty_blob=$(git hash-object -w --stdin </dev/null) &&
+
+ # Create another blob and its path
+ echo other >other.blob &&
+ other_blob=$(git hash-object -w --stdin <other.blob) &&
+ other_path=$(git rev-parse --git-path objects/$(test_oid_to_path "$other_blob")) &&
+
+ # Before the swap the size is 0
+ cat >out.expect <<-EOF &&
+ 0
+ EOF
+ git cat-file -s "$EMPTY_BLOB" >out.actual 2>err.actual &&
+ test_must_be_empty err.actual &&
+ test_cmp out.expect out.actual &&
+
+ # Swap the two to corrupt the repository
+ mv -f "$other_path" "$empty_path" &&
+ test_must_fail git fsck 2>err.fsck &&
+ grep "hash-path mismatch" err.fsck &&
+
+ # confirm that cat-file is reading the new swapped-in
+ # blob...
+ cat >out.expect <<-EOF &&
+ blob
+ EOF
+ git cat-file -t "$EMPTY_BLOB" >out.actual 2>err.actual &&
+ test_must_be_empty err.actual &&
+ test_cmp out.expect out.actual &&
+
+ # ... since it has a different size now.
+ cat >out.expect <<-EOF &&
+ 6
+ EOF
+ git cat-file -s "$EMPTY_BLOB" >out.actual 2>err.actual &&
+ test_must_be_empty err.actual &&
+ test_cmp out.expect out.actual &&
+
+ # So far "cat-file" has been happy to spew the found
+ # content out as-is. Try to make it zlib-invalid.
+ mv -f other.blob "$empty_path" &&
+ test_must_fail git fsck 2>err.fsck &&
+ cat >expect <<-EOF &&
+ error: inflate: data stream error (incorrect header check)
+ error: unable to unpack header of ./$empty_path
+ error: $empty_blob: object corrupt or missing: ./$empty_path
+ EOF
+ grep "^error: " err.fsck >actual &&
+ test_cmp expect actual
+ )
+'
+
+# Tests for git cat-file --follow-symlinks
+test_expect_success 'prep for symlink tests' '
+ echo_without_newline "$hello_content" >morx &&
+ test_ln_s_add morx same-dir-link &&
+ test_ln_s_add dir link-to-dir &&
+ test_ln_s_add ../fleem out-of-repo-link &&
+ test_ln_s_add .. out-of-repo-link-dir &&
+ test_ln_s_add same-dir-link link-to-link &&
+ test_ln_s_add nope broken-same-dir-link &&
+ mkdir dir &&
+ test_ln_s_add ../morx dir/parent-dir-link &&
+ test_ln_s_add .. dir/link-dir &&
+ test_ln_s_add ../../escape dir/out-of-repo-link &&
+ test_ln_s_add ../.. dir/out-of-repo-link-dir &&
+ test_ln_s_add nope dir/broken-link-in-dir &&
+ mkdir dir/subdir &&
+ test_ln_s_add ../../morx dir/subdir/grandparent-dir-link &&
+ test_ln_s_add ../../../great-escape dir/subdir/out-of-repo-link &&
+ test_ln_s_add ../../.. dir/subdir/out-of-repo-link-dir &&
+ test_ln_s_add ../../../ dir/subdir/out-of-repo-link-dir-trailing &&
+ test_ln_s_add ../parent-dir-link dir/subdir/parent-dir-link-to-link &&
+ echo_without_newline "$hello_content" >dir/subdir/ind2 &&
+ echo_without_newline "$hello_content" >dir/ind1 &&
+ test_ln_s_add dir dirlink &&
+ test_ln_s_add dir/subdir subdirlink &&
+ test_ln_s_add subdir/ind2 dir/link-to-child &&
+ test_ln_s_add dir/link-to-child link-to-down-link &&
+ test_ln_s_add dir/.. up-down &&
+ test_ln_s_add dir/../ up-down-trailing &&
+ test_ln_s_add dir/../morx up-down-file &&
+ test_ln_s_add dir/../../morx up-up-down-file &&
+ test_ln_s_add subdirlink/../../morx up-two-down-file &&
+ test_ln_s_add loop1 loop2 &&
+ test_ln_s_add loop2 loop1 &&
+ git add morx dir/subdir/ind2 dir/ind1 &&
+ git commit -am "test" &&
+ echo $hello_sha1 blob $hello_size >found
+'
+
+test_expect_success 'git cat-file --batch-check --follow-symlinks works for non-links' '
+ echo HEAD:morx | git cat-file --batch-check --follow-symlinks >actual &&
+ test_cmp found actual &&
+ echo HEAD:nope missing >expect &&
+ echo HEAD:nope | git cat-file --batch-check --follow-symlinks >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git cat-file --batch-check --follow-symlinks works for in-repo, same-dir links' '
+ echo HEAD:same-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
+ test_cmp found actual
+'
+
+test_expect_success 'git cat-file --batch-check --follow-symlinks works for in-repo, links to dirs' '
+ echo HEAD:link-to-dir/ind1 | git cat-file --batch-check --follow-symlinks >actual &&
+ test_cmp found actual
+'
+
+
+test_expect_success 'git cat-file --batch-check --follow-symlinks works for broken in-repo, same-dir links' '
+ echo dangling 25 >expect &&
+ echo HEAD:broken-same-dir-link >>expect &&
+ echo HEAD:broken-same-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git cat-file --batch-check --follow-symlinks -Z works for broken in-repo, same-dir links' '
+ printf "HEAD:broken-same-dir-link\0" >in &&
+ printf "dangling 25\0HEAD:broken-same-dir-link\0" >expect &&
+ git cat-file --batch-check --follow-symlinks -Z <in >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git cat-file --batch-check --follow-symlinks works for same-dir links-to-links' '
+ echo HEAD:link-to-link | git cat-file --batch-check --follow-symlinks >actual &&
+ test_cmp found actual
+'
+
+test_expect_success 'git cat-file --batch-check --follow-symlinks works for parent-dir links' '
+ echo HEAD:dir/parent-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
+ test_cmp found actual &&
+ echo notdir 29 >expect &&
+ echo HEAD:dir/parent-dir-link/nope >>expect &&
+ echo HEAD:dir/parent-dir-link/nope | git cat-file --batch-check --follow-symlinks >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git cat-file --batch-check --follow-symlinks -Z works for parent-dir links' '
+ echo HEAD:dir/parent-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
+ test_cmp found actual &&
+ printf "notdir 29\0HEAD:dir/parent-dir-link/nope\0" >expect &&
+ printf "HEAD:dir/parent-dir-link/nope\0" >in &&
+ git cat-file --batch-check --follow-symlinks -Z <in >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git cat-file --batch-check --follow-symlinks works for .. links' '
+ echo dangling 22 >expect &&
+ echo HEAD:dir/link-dir/nope >>expect &&
+ echo HEAD:dir/link-dir/nope | git cat-file --batch-check --follow-symlinks >actual &&
+ test_cmp expect actual &&
+ echo HEAD:dir/link-dir/morx | git cat-file --batch-check --follow-symlinks >actual &&
+ test_cmp found actual &&
+ echo dangling 27 >expect &&
+ echo HEAD:dir/broken-link-in-dir >>expect &&
+ echo HEAD:dir/broken-link-in-dir | git cat-file --batch-check --follow-symlinks >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git cat-file --batch-check --follow-symlinks works for ../.. links' '
+ echo notdir 41 >expect &&
+ echo HEAD:dir/subdir/grandparent-dir-link/nope >>expect &&
+ echo HEAD:dir/subdir/grandparent-dir-link/nope | git cat-file --batch-check --follow-symlinks >actual &&
+ test_cmp expect actual &&
+ echo HEAD:dir/subdir/grandparent-dir-link | git cat-file --batch-check --follow-symlinks >actual &&
+ test_cmp found actual &&
+ echo HEAD:dir/subdir/parent-dir-link-to-link | git cat-file --batch-check --follow-symlinks >actual &&
+ test_cmp found actual
+'
+
+test_expect_success 'git cat-file --batch-check --follow-symlinks works for dir/ links' '
+ echo dangling 17 >expect &&
+ echo HEAD:dirlink/morx >>expect &&
+ echo HEAD:dirlink/morx | git cat-file --batch-check --follow-symlinks >actual &&
+ test_cmp expect actual &&
+ echo $hello_sha1 blob $hello_size >expect &&
+ echo HEAD:dirlink/ind1 | git cat-file --batch-check --follow-symlinks >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git cat-file --batch-check --follow-symlinks works for dir/subdir links' '
+ echo dangling 20 >expect &&
+ echo HEAD:subdirlink/morx >>expect &&
+ echo HEAD:subdirlink/morx | git cat-file --batch-check --follow-symlinks >actual &&
+ test_cmp expect actual &&
+ echo HEAD:subdirlink/ind2 | git cat-file --batch-check --follow-symlinks >actual &&
+ test_cmp found actual
+'
+
+test_expect_success 'git cat-file --batch-check --follow-symlinks works for dir ->subdir links' '
+ echo notdir 27 >expect &&
+ echo HEAD:dir/link-to-child/morx >>expect &&
+ echo HEAD:dir/link-to-child/morx | git cat-file --batch-check --follow-symlinks >actual &&
+ test_cmp expect actual &&
+ echo HEAD:dir/link-to-child | git cat-file --batch-check --follow-symlinks >actual &&
+ test_cmp found actual &&
+ echo HEAD:link-to-down-link | git cat-file --batch-check --follow-symlinks >actual &&
+ test_cmp found actual
+'
+
+test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks' '
+ echo symlink 8 >expect &&
+ echo ../fleem >>expect &&
+ echo HEAD:out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual &&
+ test_cmp expect actual &&
+ echo symlink 2 >expect &&
+ echo .. >>expect &&
+ echo HEAD:out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks in dirs' '
+ echo symlink 9 >expect &&
+ echo ../escape >>expect &&
+ echo HEAD:dir/out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual &&
+ test_cmp expect actual &&
+ echo symlink 2 >expect &&
+ echo .. >>expect &&
+ echo HEAD:dir/out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git cat-file --batch-check --follow-symlinks works for out-of-repo symlinks in subdirs' '
+ echo symlink 15 >expect &&
+ echo ../great-escape >>expect &&
+ echo HEAD:dir/subdir/out-of-repo-link | git cat-file --batch-check --follow-symlinks >actual &&
+ test_cmp expect actual &&
+ echo symlink 2 >expect &&
+ echo .. >>expect &&
+ echo HEAD:dir/subdir/out-of-repo-link-dir | git cat-file --batch-check --follow-symlinks >actual &&
+ test_cmp expect actual &&
+ echo symlink 3 >expect &&
+ echo ../ >>expect &&
+ echo HEAD:dir/subdir/out-of-repo-link-dir-trailing | git cat-file --batch-check --follow-symlinks >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git cat-file --batch-check --follow-symlinks works for symlinks with internal ..' '
+ echo HEAD: | git cat-file --batch-check >expect &&
+ echo HEAD:up-down | git cat-file --batch-check --follow-symlinks >actual &&
+ test_cmp expect actual &&
+ echo HEAD:up-down-trailing | git cat-file --batch-check --follow-symlinks >actual &&
+ test_cmp expect actual &&
+ echo HEAD:up-down-file | git cat-file --batch-check --follow-symlinks >actual &&
+ test_cmp found actual &&
+ echo symlink 7 >expect &&
+ echo ../morx >>expect &&
+ echo HEAD:up-up-down-file | git cat-file --batch-check --follow-symlinks >actual &&
+ test_cmp expect actual &&
+ echo HEAD:up-two-down-file | git cat-file --batch-check --follow-symlinks >actual &&
+ test_cmp found actual
+'
+
+test_expect_success 'git cat-file --batch-check --follow-symlink breaks loops' '
+ echo loop 10 >expect &&
+ echo HEAD:loop1 >>expect &&
+ echo HEAD:loop1 | git cat-file --batch-check --follow-symlinks >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git cat-file --batch-check --follow-symlink -Z breaks loops' '
+ printf "loop 10\0HEAD:loop1\0" >expect &&
+ printf "HEAD:loop1\0" >in &&
+ git cat-file --batch-check --follow-symlinks -Z <in >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git cat-file --batch --follow-symlink returns correct sha and mode' '
+ echo HEAD:morx | git cat-file --batch >expect &&
+ echo HEAD:morx | git cat-file --batch --follow-symlinks >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'cat-file --batch-all-objects shows all objects' '
+ # make new repos so we know the full set of objects; we will
+ # also make sure that there are some packed and some loose
+ # objects, some referenced and some not, some duplicates, and that
+ # there are some available only via alternates.
+ git init all-one &&
+ (
+ cd all-one &&
+ echo content >file &&
+ git add file &&
+ git commit -qm base &&
+ git rev-parse HEAD HEAD^{tree} HEAD:file &&
+ git repack -ad &&
+ echo not-cloned | git hash-object -w --stdin
+ ) >expect.unsorted &&
+ git clone -s all-one all-two &&
+ (
+ cd all-two &&
+ echo local-unref | git hash-object -w --stdin
+ ) >>expect.unsorted &&
+ git -C all-two rev-parse HEAD:file |
+ git -C all-two pack-objects .git/objects/pack/pack &&
+ sort <expect.unsorted >expect &&
+ git -C all-two cat-file --batch-all-objects \
+ --batch-check="%(objectname)" >actual &&
+ test_cmp expect actual
+'
+
+# The only user-visible difference is that the objects are no longer sorted,
+# and the resulting sort order is undefined. So we can only check that it
+# produces the same objects as the ordered case, but that at least exercises
+# the code.
+test_expect_success 'cat-file --unordered works' '
+ git -C all-two cat-file --batch-all-objects --unordered \
+ --batch-check="%(objectname)" >actual.unsorted &&
+ sort <actual.unsorted >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'set up object list for --batch-all-objects tests' '
+ git -C all-two cat-file --batch-all-objects --batch-check="%(objectname)" >objects
+'
+
+test_expect_success 'cat-file --batch="%(objectname)" with --batch-all-objects will work' '
+ git -C all-two cat-file --batch="%(objectname)" <objects >expect &&
+ git -C all-two cat-file --batch-all-objects --batch="%(objectname)" >actual &&
+ cmp expect actual
+'
+
+test_expect_success 'cat-file --batch="%(rest)" with --batch-all-objects will work' '
+ git -C all-two cat-file --batch="%(rest)" <objects >expect &&
+ git -C all-two cat-file --batch-all-objects --batch="%(rest)" >actual &&
+ cmp expect actual
+'
+
+test_expect_success 'cat-file --batch="batman" with --batch-all-objects will work' '
+ git -C all-two cat-file --batch="batman" <objects >expect &&
+ git -C all-two cat-file --batch-all-objects --batch="batman" >actual &&
+ cmp expect actual
+'
+
+test_expect_success 'set up replacement object' '
+ orig=$(git rev-parse HEAD) &&
+ git cat-file commit $orig >orig &&
+ {
+ cat orig &&
+ echo extra
+ } >fake &&
+ fake=$(git hash-object -t commit -w fake) &&
+ orig_size=$(git cat-file -s $orig) &&
+ fake_size=$(git cat-file -s $fake) &&
+ git replace $orig $fake
+'
+
+test_expect_success 'cat-file --batch respects replace objects' '
+ git cat-file --batch >actual <<-EOF &&
+ $orig
+ EOF
+ {
+ echo "$orig commit $fake_size" &&
+ cat fake &&
+ echo
+ } >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'cat-file --batch-check respects replace objects' '
+ git cat-file --batch-check >actual <<-EOF &&
+ $orig
+ EOF
+ echo "$orig commit $fake_size" >expect &&
+ test_cmp expect actual
+'
+
+# Pull the entry for object with oid "$1" out of the output of
+# "cat-file --batch", including its object content (which requires
+# parsing and reading a set amount of bytes, hence perl).
+extract_batch_output () {
+ perl -ne '
+ BEGIN { $oid = shift }
+ if (/^$oid \S+ (\d+)$/) {
+ print;
+ read STDIN, my $buf, $1;
+ print $buf;
+ print "\n";
+ }
+ ' "$@"
+}
+
+test_expect_success 'cat-file --batch-all-objects --batch ignores replace' '
+ git cat-file --batch-all-objects --batch >actual.raw &&
+ extract_batch_output $orig <actual.raw >actual &&
+ {
+ echo "$orig commit $orig_size" &&
+ cat orig &&
+ echo
+ } >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'cat-file --batch-all-objects --batch-check ignores replace' '
+ git cat-file --batch-all-objects --batch-check >actual.raw &&
+ grep ^$orig actual.raw >actual &&
+ echo "$orig commit $orig_size" >expect &&
+ test_cmp expect actual
+'
+test_expect_success 'batch-command empty command' '
+ echo "" >cmd &&
+ test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
+ grep "^fatal:.*empty command in input.*" err
+'
+
+test_expect_success 'batch-command whitespace before command' '
+ echo " info deadbeef" >cmd &&
+ test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
+ grep "^fatal:.*whitespace before command.*" err
+'
+
+test_expect_success 'batch-command unknown command' '
+ echo unknown_command >cmd &&
+ test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
+ grep "^fatal:.*unknown command.*" err
+'
+
+test_expect_success 'batch-command missing arguments' '
+ echo "info" >cmd &&
+ test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
+ grep "^fatal:.*info requires arguments.*" err
+'
+
+test_expect_success 'batch-command flush with arguments' '
+ echo "flush arg" >cmd &&
+ test_expect_code 128 git cat-file --batch-command --buffer <cmd 2>err &&
+ grep "^fatal:.*flush takes no arguments.*" err
+'
+
+test_expect_success 'batch-command flush without --buffer' '
+ echo "flush" >cmd &&
+ test_expect_code 128 git cat-file --batch-command <cmd 2>err &&
+ grep "^fatal:.*flush is only for --buffer mode.*" err
+'
+
+test_done
diff --git a/t/t1007-hash-object.sh b/t/t1007-hash-object.sh
new file mode 100755
index 0000000..ac3d173
--- /dev/null
+++ b/t/t1007-hash-object.sh
@@ -0,0 +1,263 @@
+#!/bin/sh
+
+test_description="git hash-object"
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+echo_without_newline() {
+ printf '%s' "$*"
+}
+
+test_blob_does_not_exist() {
+ test_expect_success 'blob does not exist in database' "
+ test_must_fail git cat-file blob $1
+ "
+}
+
+test_blob_exists() {
+ test_expect_success 'blob exists in database' "
+ git cat-file blob $1
+ "
+}
+
+hello_content="Hello World"
+example_content="This is an example"
+
+setup_repo() {
+ echo_without_newline "$hello_content" > hello
+ echo_without_newline "$example_content" > example
+}
+
+test_repo=test
+push_repo() {
+ test_create_repo $test_repo
+ cd $test_repo
+
+ setup_repo
+}
+
+pop_repo() {
+ cd ..
+ rm -rf $test_repo
+}
+
+test_expect_success 'setup' '
+ setup_repo &&
+ test_oid_cache <<-EOF
+ hello sha1:5e1c309dae7f45e0f39b1bf3ac3cd9db12e7d689
+ hello sha256:1e3b6c04d2eeb2b3e45c8a330445404c0b7cc7b257e2b097167d26f5230090c4
+
+ example sha1:ddd3f836d3e3fbb7ae289aa9ae83536f76956399
+ example sha256:b44fe1fe65589848253737db859bd490453510719d7424daab03daf0767b85ae
+ EOF
+'
+
+# Argument checking
+
+test_expect_success "multiple '--stdin's are rejected" '
+ echo example | test_must_fail git hash-object --stdin --stdin
+'
+
+test_expect_success "Can't use --stdin and --stdin-paths together" '
+ echo example | test_must_fail git hash-object --stdin --stdin-paths &&
+ echo example | test_must_fail git hash-object --stdin-paths --stdin
+'
+
+test_expect_success "Can't pass filenames as arguments with --stdin-paths" '
+ echo example | test_must_fail git hash-object --stdin-paths hello
+'
+
+test_expect_success "Can't use --path with --stdin-paths" '
+ echo example | test_must_fail git hash-object --stdin-paths --path=foo
+'
+
+test_expect_success "Can't use --path with --no-filters" '
+ test_must_fail git hash-object --no-filters --path=foo
+'
+
+# Behavior
+
+push_repo
+
+test_expect_success 'hash a file' '
+ test "$(test_oid hello)" = $(git hash-object hello)
+'
+
+test_blob_does_not_exist "$(test_oid hello)"
+
+test_expect_success 'hash from stdin' '
+ test "$(test_oid example)" = $(git hash-object --stdin < example)
+'
+
+test_blob_does_not_exist "$(test_oid example)"
+
+test_expect_success 'hash a file and write to database' '
+ test "$(test_oid hello)" = $(git hash-object -w hello)
+'
+
+test_blob_exists "$(test_oid hello)"
+
+test_expect_success 'git hash-object --stdin file1 <file0 first operates on file0, then file1' '
+ echo foo > file1 &&
+ obname0=$(echo bar | git hash-object --stdin) &&
+ obname1=$(git hash-object file1) &&
+ obname0new=$(echo bar | git hash-object --stdin file1 | sed -n -e 1p) &&
+ obname1new=$(echo bar | git hash-object --stdin file1 | sed -n -e 2p) &&
+ test "$obname0" = "$obname0new" &&
+ test "$obname1" = "$obname1new"
+'
+
+test_expect_success 'set up crlf tests' '
+ echo fooQ | tr Q "\\015" >file0 &&
+ cp file0 file1 &&
+ echo "file0 -crlf" >.gitattributes &&
+ echo "file1 crlf" >>.gitattributes &&
+ git config core.autocrlf true &&
+ file0_sha=$(git hash-object file0) &&
+ file1_sha=$(git hash-object file1) &&
+ test "$file0_sha" != "$file1_sha"
+'
+
+test_expect_success 'check that appropriate filter is invoke when --path is used' '
+ path1_sha=$(git hash-object --path=file1 file0) &&
+ path0_sha=$(git hash-object --path=file0 file1) &&
+ test "$file0_sha" = "$path0_sha" &&
+ test "$file1_sha" = "$path1_sha" &&
+ path1_sha=$(cat file0 | git hash-object --path=file1 --stdin) &&
+ path0_sha=$(cat file1 | git hash-object --path=file0 --stdin) &&
+ test "$file0_sha" = "$path0_sha" &&
+ test "$file1_sha" = "$path1_sha"
+'
+
+test_expect_success 'gitattributes also work in a subdirectory' '
+ mkdir subdir &&
+ (
+ cd subdir &&
+ subdir_sha0=$(git hash-object ../file0) &&
+ subdir_sha1=$(git hash-object ../file1) &&
+ test "$file0_sha" = "$subdir_sha0" &&
+ test "$file1_sha" = "$subdir_sha1"
+ )
+'
+
+test_expect_success '--path works in a subdirectory' '
+ (
+ cd subdir &&
+ path1_sha=$(git hash-object --path=../file1 ../file0) &&
+ path0_sha=$(git hash-object --path=../file0 ../file1) &&
+ test "$file0_sha" = "$path0_sha" &&
+ test "$file1_sha" = "$path1_sha"
+ )
+'
+
+test_expect_success 'check that --no-filters option works' '
+ nofilters_file1=$(git hash-object --no-filters file1) &&
+ test "$file0_sha" = "$nofilters_file1" &&
+ nofilters_file1=$(cat file1 | git hash-object --stdin) &&
+ test "$file0_sha" = "$nofilters_file1"
+'
+
+test_expect_success 'check that --no-filters option works with --stdin-paths' '
+ nofilters_file1=$(echo "file1" | git hash-object --stdin-paths --no-filters) &&
+ test "$file0_sha" = "$nofilters_file1"
+'
+
+pop_repo
+
+for args in "-w --stdin" "--stdin -w"; do
+ push_repo
+
+ test_expect_success "hash from stdin and write to database ($args)" '
+ test "$(test_oid example)" = $(git hash-object $args < example)
+ '
+
+ test_blob_exists "$(test_oid example)"
+
+ pop_repo
+done
+
+filenames="hello
+example"
+
+oids="$(test_oid hello)
+$(test_oid example)"
+
+test_expect_success "hash two files with names on stdin" '
+ test "$oids" = "$(echo_without_newline "$filenames" | git hash-object --stdin-paths)"
+'
+
+for args in "-w --stdin-paths" "--stdin-paths -w"; do
+ push_repo
+
+ test_expect_success "hash two files with names on stdin and write to database ($args)" '
+ test "$oids" = "$(echo_without_newline "$filenames" | git hash-object $args)"
+ '
+
+ test_blob_exists "$(test_oid hello)"
+ test_blob_exists "$(test_oid example)"
+
+ pop_repo
+done
+
+test_expect_success 'too-short tree' '
+ echo abc >malformed-tree &&
+ test_must_fail git hash-object -t tree malformed-tree 2>err &&
+ grep "too-short tree object" err
+'
+
+test_expect_success 'malformed mode in tree' '
+ hex_oid=$(echo foo | git hash-object --stdin -w) &&
+ bin_oid=$(echo $hex_oid | hex2oct) &&
+ printf "9100644 \0$bin_oid" >tree-with-malformed-mode &&
+ test_must_fail git hash-object -t tree tree-with-malformed-mode 2>err &&
+ grep "malformed mode in tree entry" err
+'
+
+test_expect_success 'empty filename in tree' '
+ hex_oid=$(echo foo | git hash-object --stdin -w) &&
+ bin_oid=$(echo $hex_oid | hex2oct) &&
+ printf "100644 \0$bin_oid" >tree-with-empty-filename &&
+ test_must_fail git hash-object -t tree tree-with-empty-filename 2>err &&
+ grep "empty filename in tree entry" err
+'
+
+test_expect_success 'duplicate filename in tree' '
+ hex_oid=$(echo foo | git hash-object --stdin -w) &&
+ bin_oid=$(echo $hex_oid | hex2oct) &&
+ {
+ printf "100644 file\0$bin_oid" &&
+ printf "100644 file\0$bin_oid"
+ } >tree-with-duplicate-filename &&
+ test_must_fail git hash-object -t tree tree-with-duplicate-filename 2>err &&
+ grep "duplicateEntries" err
+'
+
+test_expect_success 'corrupt commit' '
+ test_must_fail git hash-object -t commit --stdin </dev/null
+'
+
+test_expect_success 'corrupt tag' '
+ test_must_fail git hash-object -t tag --stdin </dev/null
+'
+
+test_expect_success 'hash-object complains about bogus type name' '
+ test_must_fail git hash-object -t bogus --stdin </dev/null
+'
+
+test_expect_success 'hash-object complains about truncated type name' '
+ test_must_fail git hash-object -t bl --stdin </dev/null
+'
+
+test_expect_success '--literally' '
+ t=1234567890 &&
+ echo example | git hash-object -t $t --literally --stdin
+'
+
+test_expect_success '--literally with extra-long type' '
+ t=12345678901234567890123456789012345678901234567890 &&
+ t="$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t$t" &&
+ echo example | git hash-object -t $t --literally --stdin
+'
+
+test_done
diff --git a/t/t1008-read-tree-overlay.sh b/t/t1008-read-tree-overlay.sh
new file mode 100755
index 0000000..ad5936e
--- /dev/null
+++ b/t/t1008-read-tree-overlay.sh
@@ -0,0 +1,36 @@
+#!/bin/sh
+
+test_description='test multi-tree read-tree without merging'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-read-tree.sh
+
+test_expect_success setup '
+ echo one >a &&
+ git add a &&
+ git commit -m initial &&
+ git tag initial &&
+ echo two >b &&
+ git add b &&
+ git commit -m second &&
+ git checkout -b side initial &&
+ echo three >a &&
+ mkdir b &&
+ echo four >b/c &&
+ git add b/c &&
+ git commit -m third
+'
+
+test_expect_success 'multi-read' '
+ read_tree_must_succeed initial main side &&
+ test_write_lines a b/c >expect &&
+ git ls-files >actual &&
+ test_cmp expect actual
+'
+
+test_done
+
diff --git a/t/t1009-read-tree-new-index.sh b/t/t1009-read-tree-new-index.sh
new file mode 100755
index 0000000..fc179ac
--- /dev/null
+++ b/t/t1009-read-tree-new-index.sh
@@ -0,0 +1,29 @@
+#!/bin/sh
+
+test_description='test read-tree into a fresh index file'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ echo one >a &&
+ git add a &&
+ git commit -m initial
+'
+
+test_expect_success 'non-existent index file' '
+ rm -f new-index &&
+ GIT_INDEX_FILE=new-index git read-tree main
+'
+
+test_expect_success 'empty index file' '
+ rm -f new-index &&
+ > new-index &&
+ GIT_INDEX_FILE=new-index git read-tree main
+'
+
+test_done
+
diff --git a/t/t1010-mktree.sh b/t/t1010-mktree.sh
new file mode 100755
index 0000000..22875ba
--- /dev/null
+++ b/t/t1010-mktree.sh
@@ -0,0 +1,70 @@
+#!/bin/sh
+
+test_description='git mktree'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ for d in a a- a0
+ do
+ mkdir "$d" && echo "$d/one" >"$d/one" &&
+ git add "$d" || return 1
+ done &&
+ echo zero >one &&
+ git update-index --add --info-only one &&
+ git write-tree --missing-ok >tree.missing &&
+ git ls-tree $(cat tree.missing) >top.missing &&
+ git ls-tree -r $(cat tree.missing) >all.missing &&
+ echo one >one &&
+ git add one &&
+ git write-tree >tree &&
+ git ls-tree $(cat tree) >top &&
+ git ls-tree -r $(cat tree) >all &&
+ test_tick &&
+ git commit -q -m one &&
+ H=$(git rev-parse HEAD) &&
+ git update-index --add --cacheinfo 160000 $H sub &&
+ test_tick &&
+ git commit -q -m two &&
+ git rev-parse HEAD^{tree} >tree.withsub &&
+ git ls-tree HEAD >top.withsub &&
+ git ls-tree -r HEAD >all.withsub
+'
+
+test_expect_success 'ls-tree piped to mktree (1)' '
+ git mktree <top >actual &&
+ test_cmp tree actual
+'
+
+test_expect_success 'ls-tree piped to mktree (2)' '
+ git mktree <top.withsub >actual &&
+ test_cmp tree.withsub actual
+'
+
+test_expect_success 'ls-tree output in wrong order given to mktree (1)' '
+ perl -e "print reverse <>" <top |
+ git mktree >actual &&
+ test_cmp tree actual
+'
+
+test_expect_success 'ls-tree output in wrong order given to mktree (2)' '
+ perl -e "print reverse <>" <top.withsub |
+ git mktree >actual &&
+ test_cmp tree.withsub actual
+'
+
+test_expect_success 'allow missing object with --missing' '
+ git mktree --missing <top.missing >actual &&
+ test_cmp tree.missing actual
+'
+
+test_expect_success 'mktree refuses to read ls-tree -r output (1)' '
+ test_must_fail git mktree <all
+'
+
+test_expect_success 'mktree refuses to read ls-tree -r output (2)' '
+ test_must_fail git mktree <all.withsub
+'
+
+test_done
diff --git a/t/t1011-read-tree-sparse-checkout.sh b/t/t1011-read-tree-sparse-checkout.sh
new file mode 100755
index 0000000..595b24c
--- /dev/null
+++ b/t/t1011-read-tree-sparse-checkout.sh
@@ -0,0 +1,307 @@
+#!/bin/sh
+
+test_description='sparse checkout tests
+
+* (tag: removed, main) removed
+| D sub/added
+* (HEAD, tag: top) modified and added
+| M init.t
+| A sub/added
+* (tag: init) init
+ A init.t
+'
+
+TEST_CREATE_REPO_NO_TEMPLATE=1
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-read-tree.sh
+
+test_expect_success 'setup' '
+ test_commit init &&
+ echo modified >>init.t &&
+
+ cat >expected <<-EOF &&
+ 100644 $(git hash-object init.t) 0 init.t
+ 100644 $EMPTY_BLOB 0 sub/added
+ 100644 $EMPTY_BLOB 0 sub/addedtoo
+ 100644 $EMPTY_BLOB 0 subsub/added
+ EOF
+ cat >expected.swt <<-\EOF &&
+ H init.t
+ H sub/added
+ H sub/addedtoo
+ H subsub/added
+ EOF
+
+ mkdir sub subsub &&
+ touch sub/added sub/addedtoo subsub/added &&
+ git add init.t sub/added sub/addedtoo subsub/added &&
+ git commit -m "modified and added" &&
+ git tag top &&
+ git rm sub/added &&
+ git commit -m removed &&
+ git tag removed &&
+ git checkout top &&
+ git ls-files --stage >result &&
+ test_cmp expected result
+'
+
+test_expect_success 'read-tree without .git/info/sparse-checkout' '
+ read_tree_u_must_succeed -m -u HEAD &&
+ git ls-files --stage >result &&
+ test_cmp expected result &&
+ git ls-files -t >result &&
+ test_cmp expected.swt result
+'
+
+test_expect_success 'read-tree with .git/info/sparse-checkout but disabled' '
+ mkdir .git/info &&
+ echo >.git/info/sparse-checkout &&
+ read_tree_u_must_succeed -m -u HEAD &&
+ git ls-files -t >result &&
+ test_cmp expected.swt result &&
+ test_path_is_file init.t &&
+ test_path_is_file sub/added
+'
+
+test_expect_success 'read-tree --no-sparse-checkout with empty .git/info/sparse-checkout and enabled' '
+ git config core.sparsecheckout true &&
+ echo >.git/info/sparse-checkout &&
+ read_tree_u_must_succeed --no-sparse-checkout -m -u HEAD &&
+ git ls-files -t >result &&
+ test_cmp expected.swt result &&
+ test_path_is_file init.t &&
+ test_path_is_file sub/added
+'
+
+test_expect_success 'read-tree with empty .git/info/sparse-checkout' '
+ git config core.sparsecheckout true &&
+ echo >.git/info/sparse-checkout &&
+ read_tree_u_must_succeed -m -u HEAD &&
+ git ls-files --stage >result &&
+ test_cmp expected result &&
+ git ls-files -t >result &&
+ cat >expected.swt <<-\EOF &&
+ S init.t
+ S sub/added
+ S sub/addedtoo
+ S subsub/added
+ EOF
+ test_cmp expected.swt result &&
+ test_path_is_missing init.t &&
+ test_path_is_missing sub/added
+'
+
+test_expect_success 'match directories with trailing slash' '
+ cat >expected.swt-noinit <<-\EOF &&
+ S init.t
+ H sub/added
+ H sub/addedtoo
+ S subsub/added
+ EOF
+
+ echo sub/ > .git/info/sparse-checkout &&
+ read_tree_u_must_succeed -m -u HEAD &&
+ git ls-files -t > result &&
+ test_cmp expected.swt-noinit result &&
+ test_path_is_missing init.t &&
+ test_path_is_file sub/added
+'
+
+test_expect_success 'match directories without trailing slash' '
+ echo sub >.git/info/sparse-checkout &&
+ read_tree_u_must_succeed -m -u HEAD &&
+ git ls-files -t >result &&
+ test_cmp expected.swt-noinit result &&
+ test_path_is_missing init.t &&
+ test_path_is_file sub/added
+'
+
+test_expect_success 'match directories with negated patterns' '
+ cat >expected.swt-negation <<\EOF &&
+S init.t
+S sub/added
+H sub/addedtoo
+S subsub/added
+EOF
+
+ cat >.git/info/sparse-checkout <<\EOF &&
+sub
+!sub/added
+EOF
+ git read-tree -m -u HEAD &&
+ git ls-files -t >result &&
+ test_cmp expected.swt-negation result &&
+ test_path_is_missing init.t &&
+ test_path_is_missing sub/added &&
+ test_path_is_file sub/addedtoo
+'
+
+test_expect_success 'match directories with negated patterns (2)' '
+ cat >expected.swt-negation2 <<\EOF &&
+H init.t
+H sub/added
+S sub/addedtoo
+H subsub/added
+EOF
+
+ cat >.git/info/sparse-checkout <<\EOF &&
+/*
+!sub
+sub/added
+EOF
+ git read-tree -m -u HEAD &&
+ git ls-files -t >result &&
+ test_cmp expected.swt-negation2 result &&
+ test_path_is_file init.t &&
+ test_path_is_file sub/added &&
+ test_path_is_missing sub/addedtoo
+'
+
+test_expect_success 'match directory pattern' '
+ echo "s?b" >.git/info/sparse-checkout &&
+ read_tree_u_must_succeed -m -u HEAD &&
+ git ls-files -t >result &&
+ test_cmp expected.swt-noinit result &&
+ test_path_is_missing init.t &&
+ test_path_is_file sub/added
+'
+
+test_expect_success 'checkout area changes' '
+ cat >expected.swt-nosub <<-\EOF &&
+ H init.t
+ S sub/added
+ S sub/addedtoo
+ S subsub/added
+ EOF
+
+ echo init.t >.git/info/sparse-checkout &&
+ read_tree_u_must_succeed -m -u HEAD &&
+ git ls-files -t >result &&
+ test_cmp expected.swt-nosub result &&
+ test_path_is_file init.t &&
+ test_path_is_missing sub/added
+'
+
+test_expect_success 'read-tree updates worktree, absent case' '
+ echo sub/added >.git/info/sparse-checkout &&
+ git checkout -f top &&
+ read_tree_u_must_succeed -m -u HEAD^ &&
+ test_path_is_missing init.t
+'
+
+test_expect_success 'read-tree will not throw away dirty changes, non-sparse' '
+ echo "/*" >.git/info/sparse-checkout &&
+ read_tree_u_must_succeed -m -u HEAD &&
+
+ echo dirty >init.t &&
+ read_tree_u_must_fail -m -u HEAD^ &&
+ test_path_is_file init.t &&
+ grep -q dirty init.t
+'
+
+test_expect_success 'read-tree will not throw away dirty changes, sparse' '
+ echo "/*" >.git/info/sparse-checkout &&
+ read_tree_u_must_succeed -m -u HEAD &&
+
+ echo dirty >init.t &&
+ echo sub/added >.git/info/sparse-checkout &&
+ read_tree_u_must_fail -m -u HEAD^ &&
+ test_path_is_file init.t &&
+ grep -q dirty init.t
+'
+
+test_expect_success 'read-tree updates worktree, dirty case' '
+ echo sub/added >.git/info/sparse-checkout &&
+ git checkout -f top &&
+ echo dirty >init.t &&
+ read_tree_u_must_fail -m -u HEAD^ &&
+ grep -q dirty init.t &&
+ rm init.t
+'
+
+test_expect_success 'read-tree removes worktree, dirty case' '
+ echo init.t >.git/info/sparse-checkout &&
+ git checkout -f top &&
+ echo dirty >added &&
+ read_tree_u_must_succeed -m -u HEAD^ &&
+ grep -q dirty added
+'
+
+test_expect_success 'read-tree adds to worktree, absent case' '
+ echo init.t >.git/info/sparse-checkout &&
+ git checkout -f removed &&
+ read_tree_u_must_succeed -u -m HEAD^ &&
+ test_path_is_missing sub/added
+'
+
+test_expect_success 'read-tree adds to worktree, dirty case' '
+ echo init.t >.git/info/sparse-checkout &&
+ git checkout -f removed &&
+ mkdir sub &&
+ echo dirty >sub/added &&
+ read_tree_u_must_succeed -u -m HEAD^ &&
+ grep -q dirty sub/added
+'
+
+test_expect_success 'index removal and worktree narrowing at the same time' '
+ echo init.t >.git/info/sparse-checkout &&
+ echo sub/added >>.git/info/sparse-checkout &&
+ git checkout -f top &&
+ echo init.t >.git/info/sparse-checkout &&
+ git checkout removed &&
+ git ls-files sub/added >result &&
+ test_path_is_missing sub/added &&
+ test_must_be_empty result
+'
+
+test_expect_success 'read-tree --reset removes outside worktree' '
+ echo init.t >.git/info/sparse-checkout &&
+ git checkout -f top &&
+ git reset --hard removed &&
+ git ls-files sub/added >result &&
+ test_must_be_empty result
+'
+
+test_expect_success 'print warnings when some worktree updates disabled' '
+ echo sub >.git/info/sparse-checkout &&
+ git checkout -f init &&
+ mkdir sub &&
+ touch sub/added sub/addedtoo &&
+ # Use -q to suppress "Previous HEAD position" and "Head is now at" msgs
+ git checkout -q top 2>actual &&
+ cat >expected <<\EOF &&
+warning: The following paths were already present and thus not updated despite sparse patterns:
+ sub/added
+ sub/addedtoo
+
+After fixing the above paths, you may want to run `git sparse-checkout reapply`.
+EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'checkout without --ignore-skip-worktree-bits' '
+ echo "*" >.git/info/sparse-checkout &&
+ git checkout -f top &&
+ test_path_is_file init.t &&
+ echo sub >.git/info/sparse-checkout &&
+ git checkout &&
+ echo modified >> sub/added &&
+ git checkout . &&
+ test_path_is_missing init.t &&
+ git diff --exit-code HEAD
+'
+
+test_expect_success 'checkout with --ignore-skip-worktree-bits' '
+ echo "*" >.git/info/sparse-checkout &&
+ git checkout -f top &&
+ test_path_is_file init.t &&
+ echo sub >.git/info/sparse-checkout &&
+ git checkout &&
+ echo modified >> sub/added &&
+ git checkout --ignore-skip-worktree-bits . &&
+ test_path_is_file init.t &&
+ git diff --exit-code HEAD
+'
+
+test_done
diff --git a/t/t1012-read-tree-df.sh b/t/t1012-read-tree-df.sh
new file mode 100755
index 0000000..cde93d2
--- /dev/null
+++ b/t/t1012-read-tree-df.sh
@@ -0,0 +1,104 @@
+#!/bin/sh
+
+test_description='read-tree D/F conflict corner cases'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-read-tree.sh
+
+maketree () {
+ (
+ rm -f .git/index .git/index.lock &&
+ git clean -d -f -f -q -x &&
+ name="$1" &&
+ shift &&
+ for it
+ do
+ path=$(expr "$it" : '\([^:]*\)') &&
+ mkdir -p $(dirname "$path") &&
+ echo "$it" >"$path" &&
+ git update-index --add "$path" || exit
+ done &&
+ git tag "$name" $(git write-tree)
+ )
+}
+
+settree () {
+ rm -f .git/index .git/index.lock &&
+ git clean -d -f -f -q -x &&
+ git read-tree "$1" &&
+ git checkout-index -f -q -u -a &&
+ git update-index --refresh
+}
+
+checkindex () {
+ git ls-files -s |
+ sed "s|^[0-7][0-7]* $OID_REGEX \([0-3]\) |\1 |" >current &&
+ cat >expect &&
+ test_cmp expect current
+}
+
+test_expect_success setup '
+ maketree O-000 a/b-2/c/d a/b/c/d a/x &&
+ maketree A-000 a/b-2/c/d a/b/c/d a/x &&
+ maketree A-001 a/b-2/c/d a/b/c/d a/b/c/e a/x &&
+ maketree B-000 a/b-2/c/d a/b a/x &&
+
+ maketree O-010 t-0 t/1 t/2 t=3 &&
+ maketree A-010 t-0 t t=3 &&
+ maketree B-010 t/1: t=3: &&
+
+ maketree O-020 ds/dma/ioat.c ds/dma/ioat_dca.c &&
+ maketree A-020 ds/dma/ioat/Makefile ds/dma/ioat/registers.h &&
+ :
+'
+
+test_expect_success '3-way (1)' '
+ settree A-000 &&
+ read_tree_u_must_succeed -m -u O-000 A-000 B-000 &&
+ checkindex <<-EOF
+ 3 a/b
+ 0 a/b-2/c/d
+ 1 a/b/c/d
+ 2 a/b/c/d
+ 0 a/x
+ EOF
+'
+
+test_expect_success '3-way (2)' '
+ settree A-001 &&
+ read_tree_u_must_succeed -m -u O-000 A-001 B-000 &&
+ checkindex <<-EOF
+ 3 a/b
+ 0 a/b-2/c/d
+ 1 a/b/c/d
+ 2 a/b/c/d
+ 2 a/b/c/e
+ 0 a/x
+ EOF
+'
+
+test_expect_success '3-way (3)' '
+ settree A-010 &&
+ read_tree_u_must_succeed -m -u O-010 A-010 B-010 &&
+ checkindex <<-EOF
+ 2 t
+ 1 t-0
+ 2 t-0
+ 1 t/1
+ 3 t/1
+ 1 t/2
+ 0 t=3
+ EOF
+'
+
+test_expect_success '2-way (1)' '
+ settree O-020 &&
+ read_tree_u_must_succeed -m -u O-020 A-020 &&
+ checkindex <<-EOF
+ 0 ds/dma/ioat/Makefile
+ 0 ds/dma/ioat/registers.h
+ EOF
+'
+
+test_done
diff --git a/t/t1013-read-tree-submodule.sh b/t/t1013-read-tree-submodule.sh
new file mode 100755
index 0000000..bfc90d4
--- /dev/null
+++ b/t/t1013-read-tree-submodule.sh
@@ -0,0 +1,18 @@
+#!/bin/sh
+
+test_description='read-tree can handle submodules'
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-submodule-update.sh
+
+KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS=1
+
+test_submodule_switch_recursing_with_args "read-tree -u -m"
+
+test_submodule_forced_switch_recursing_with_args "read-tree -u --reset"
+
+test_submodule_switch "read-tree -u -m"
+
+test_submodule_forced_switch "read-tree -u --reset"
+
+test_done
diff --git a/t/t1014-read-tree-confusing.sh b/t/t1014-read-tree-confusing.sh
new file mode 100755
index 0000000..8ea8d36
--- /dev/null
+++ b/t/t1014-read-tree-confusing.sh
@@ -0,0 +1,65 @@
+#!/bin/sh
+
+test_description='check that read-tree rejects confusing paths'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'create base tree' '
+ echo content >file &&
+ git add file &&
+ git commit -m base &&
+ blob=$(git rev-parse HEAD:file) &&
+ tree=$(git rev-parse HEAD^{tree})
+'
+
+test_expect_success 'enable core.protectHFS for rejection tests' '
+ git config core.protectHFS true
+'
+
+test_expect_success 'enable core.protectNTFS for rejection tests' '
+ git config core.protectNTFS true
+'
+
+while read path pretty; do
+ : ${pretty:=$path}
+ case "$path" in
+ *SPACE)
+ path="${path%SPACE} "
+ ;;
+ esac
+ test_expect_success "reject $pretty at end of path" '
+ printf "100644 blob %s\t%s" "$blob" "$path" >tree &&
+ bogus=$(git mktree <tree) &&
+ test_must_fail git read-tree $bogus
+ '
+
+ test_expect_success "reject $pretty as subtree" '
+ printf "040000 tree %s\t%s" "$tree" "$path" >tree &&
+ bogus=$(git mktree <tree) &&
+ test_must_fail git read-tree $bogus
+ '
+done <<-EOF
+.
+..
+.git
+.GIT
+${u200c}.Git {u200c}.Git
+.gI${u200c}T .gI{u200c}T
+.GiT${u200c} .GiT{u200c}
+git~1
+.git.SPACE .git.{space}
+.\\\\.GIT\\\\foobar backslashes
+.git\\\\foobar backslashes2
+.git...:alternate-stream
+EOF
+
+test_expect_success 'utf-8 paths allowed with core.protectHFS off' '
+ test_when_finished "git read-tree HEAD" &&
+ test_config core.protectHFS false &&
+ printf "100644 blob %s\t%s" "$blob" ".gi${u200c}t" >tree &&
+ ok=$(git mktree <tree) &&
+ git read-tree $ok
+'
+
+test_done
diff --git a/t/t1015-read-index-unmerged.sh b/t/t1015-read-index-unmerged.sh
new file mode 100755
index 0000000..55d22da
--- /dev/null
+++ b/t/t1015-read-index-unmerged.sh
@@ -0,0 +1,123 @@
+#!/bin/sh
+
+test_description='Test various callers of read_index_unmerged'
+. ./test-lib.sh
+
+test_expect_success 'setup modify/delete + directory/file conflict' '
+ test_create_repo df_plus_modify_delete &&
+ (
+ cd df_plus_modify_delete &&
+
+ test_write_lines a b c d e f g h >letters &&
+ git add letters &&
+ git commit -m initial &&
+
+ git checkout -b modify &&
+ # Throw in letters.txt for sorting order fun
+ # ("letters.txt" sorts between "letters" and "letters/file")
+ echo i >>letters &&
+ echo "version 2" >letters.txt &&
+ git add letters letters.txt &&
+ git commit -m modified &&
+
+ git checkout -b delete HEAD^ &&
+ git rm letters &&
+ mkdir letters &&
+ >letters/file &&
+ echo "version 1" >letters.txt &&
+ git add letters letters.txt &&
+ git commit -m deleted
+ )
+'
+
+test_expect_success 'read-tree --reset cleans unmerged entries' '
+ test_when_finished "git -C df_plus_modify_delete clean -f" &&
+ test_when_finished "git -C df_plus_modify_delete reset --hard" &&
+ (
+ cd df_plus_modify_delete &&
+
+ git checkout delete^0 &&
+ test_must_fail git merge modify &&
+
+ git read-tree --reset HEAD &&
+ git ls-files -u >conflicts &&
+ test_must_be_empty conflicts
+ )
+'
+
+test_expect_success 'One reset --hard cleans unmerged entries' '
+ test_when_finished "git -C df_plus_modify_delete clean -f" &&
+ test_when_finished "git -C df_plus_modify_delete reset --hard" &&
+ (
+ cd df_plus_modify_delete &&
+
+ git checkout delete^0 &&
+ test_must_fail git merge modify &&
+
+ git reset --hard &&
+ test_path_is_missing .git/MERGE_HEAD &&
+ git ls-files -u >conflicts &&
+ test_must_be_empty conflicts
+ )
+'
+
+test_expect_success 'setup directory/file conflict + simple edit/edit' '
+ test_create_repo df_plus_edit_edit &&
+ (
+ cd df_plus_edit_edit &&
+
+ test_seq 1 10 >numbers &&
+ git add numbers &&
+ git commit -m initial &&
+
+ git checkout -b d-edit &&
+ mkdir foo &&
+ echo content >foo/bar &&
+ git add foo &&
+ echo 11 >>numbers &&
+ git add numbers &&
+ git commit -m "directory and edit" &&
+
+ git checkout -b f-edit d-edit^1 &&
+ echo content >foo &&
+ git add foo &&
+ echo eleven >>numbers &&
+ git add numbers &&
+ git commit -m "file and edit"
+ )
+'
+
+test_expect_success 'git merge --abort succeeds despite D/F conflict' '
+ test_when_finished "git -C df_plus_edit_edit clean -f" &&
+ test_when_finished "git -C df_plus_edit_edit reset --hard" &&
+ (
+ cd df_plus_edit_edit &&
+
+ git checkout f-edit^0 &&
+ test_must_fail git merge d-edit^0 &&
+
+ git merge --abort &&
+ test_path_is_missing .git/MERGE_HEAD &&
+ git ls-files -u >conflicts &&
+ test_must_be_empty conflicts
+ )
+'
+
+test_expect_success 'git am --skip succeeds despite D/F conflict' '
+ test_when_finished "git -C df_plus_edit_edit clean -f" &&
+ test_when_finished "git -C df_plus_edit_edit reset --hard" &&
+ (
+ cd df_plus_edit_edit &&
+
+ git checkout f-edit^0 &&
+ git format-patch -1 d-edit &&
+ test_must_fail git am -3 0001*.patch &&
+
+ git am --skip &&
+ test_path_is_missing .git/rebase-apply &&
+ git ls-files -u >conflicts &&
+ test_must_be_empty conflicts
+ )
+'
+
+test_done
diff --git a/t/t1020-subdirectory.sh b/t/t1020-subdirectory.sh
new file mode 100755
index 0000000..45eef94
--- /dev/null
+++ b/t/t1020-subdirectory.sh
@@ -0,0 +1,196 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Junio C Hamano
+#
+
+test_description='Try various core-level commands in subdirectory.
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-read-tree.sh
+
+test_expect_success setup '
+ long="a b c d e f g h i j k l m n o p q r s t u v w x y z" &&
+ test_write_lines $long >one &&
+ mkdir dir &&
+ test_write_lines x y z $long a b c >dir/two &&
+ cp one original.one &&
+ cp dir/two original.two
+'
+
+test_expect_success 'update-index and ls-files' '
+ git update-index --add one &&
+ case "$(git ls-files)" in
+ one) echo pass one ;;
+ *) echo bad one; return 1 ;;
+ esac &&
+ (
+ cd dir &&
+ git update-index --add two &&
+ case "$(git ls-files)" in
+ two) echo pass two ;;
+ *) echo bad two; exit 1 ;;
+ esac
+ ) &&
+ case "$(git ls-files)" in
+ dir/two"$LF"one) echo pass both ;;
+ *) echo bad; return 1 ;;
+ esac
+'
+
+test_expect_success 'cat-file' '
+ two=$(git ls-files -s dir/two) &&
+ two=$(expr "$two" : "[0-7]* \\([0-9a-f]*\\)") &&
+ echo "$two" &&
+ git cat-file -p "$two" >actual &&
+ cmp dir/two actual &&
+ (
+ cd dir &&
+ git cat-file -p "$two" >actual &&
+ cmp two actual
+ )
+'
+rm -f actual dir/actual
+
+test_expect_success 'diff-files' '
+ echo a >>one &&
+ echo d >>dir/two &&
+ case "$(git diff-files --name-only)" in
+ dir/two"$LF"one) echo pass top ;;
+ *) echo bad top; return 1 ;;
+ esac &&
+ # diff should not omit leading paths
+ (
+ cd dir &&
+ case "$(git diff-files --name-only)" in
+ dir/two"$LF"one) echo pass subdir ;;
+ *) echo bad subdir; exit 1 ;;
+ esac &&
+ case "$(git diff-files --name-only .)" in
+ dir/two) echo pass subdir limited ;;
+ *) echo bad subdir limited; exit 1 ;;
+ esac
+ )
+'
+
+test_expect_success 'write-tree' '
+ top=$(git write-tree) &&
+ echo $top &&
+ (
+ cd dir &&
+ sub=$(git write-tree) &&
+ echo $sub &&
+ test "z$top" = "z$sub"
+ )
+'
+
+test_expect_success 'checkout-index' '
+ git checkout-index -f -u one &&
+ cmp one original.one &&
+ (
+ cd dir &&
+ git checkout-index -f -u two &&
+ cmp two ../original.two
+ )
+'
+
+test_expect_success 'read-tree' '
+ rm -f one dir/two &&
+ tree=$(git write-tree) &&
+ read_tree_u_must_succeed --reset -u "$tree" &&
+ cmp one original.one &&
+ cmp dir/two original.two &&
+ (
+ cd dir &&
+ rm -f two &&
+ read_tree_u_must_succeed --reset -u "$tree" &&
+ cmp two ../original.two &&
+ cmp ../one ../original.one
+ )
+'
+
+test_expect_success 'alias expansion' '
+ (
+ git config alias.test-status-alias status &&
+ cd dir &&
+ git status &&
+ git test-status-alias
+ )
+'
+
+test_expect_success !MINGW '!alias expansion' '
+ pwd >expect &&
+ (
+ git config alias.test-alias-directory !pwd &&
+ cd dir &&
+ git test-alias-directory >../actual
+ ) &&
+ test_cmp expect actual
+'
+
+test_expect_success 'GIT_PREFIX for !alias' '
+ printf "dir/" >expect &&
+ (
+ git config alias.test-alias-directory "!sh -c \"printf \$GIT_PREFIX\"" &&
+ cd dir &&
+ git test-alias-directory >../actual
+ ) &&
+ test_cmp expect actual
+'
+
+test_expect_success 'GIT_PREFIX for built-ins' '
+ # Use GIT_EXTERNAL_DIFF to test that the "diff" built-in
+ # receives the GIT_PREFIX variable.
+ echo "dir/" >expect &&
+ write_script diff <<-\EOF &&
+ printf "%s\n" "$GIT_PREFIX"
+ EOF
+ (
+ cd dir &&
+ echo "change" >two &&
+ GIT_EXTERNAL_DIFF=./diff git diff >../actual &&
+ git checkout -- two
+ ) &&
+ test_cmp expect actual
+'
+
+test_expect_success 'no file/rev ambiguity check inside .git' '
+ git commit -a -m 1 &&
+ (
+ cd .git &&
+ git show -s HEAD
+ )
+'
+
+test_expect_success 'no file/rev ambiguity check inside a bare repo (explicit GIT_DIR)' '
+ test_when_finished "rm -fr foo.git" &&
+ git clone -s --bare .git foo.git &&
+ (
+ cd foo.git &&
+ # older Git needed help by exporting GIT_DIR=.
+ # to realize that it is inside a bare repository.
+ # We keep this test around for regression testing.
+ GIT_DIR=. git show -s HEAD
+ )
+'
+
+test_expect_success 'no file/rev ambiguity check inside a bare repo' '
+ test_when_finished "rm -fr foo.git" &&
+ git clone -s --bare .git foo.git &&
+ (
+ cd foo.git &&
+ git show -s HEAD
+ )
+'
+
+test_expect_success SYMLINKS 'detection should not be fooled by a symlink' '
+ git clone -s .git another &&
+ ln -s another yetanother &&
+ (
+ cd yetanother/.git &&
+ git show -s HEAD
+ )
+'
+
+test_done
diff --git a/t/t1021-rerere-in-workdir.sh b/t/t1021-rerere-in-workdir.sh
new file mode 100755
index 0000000..0b89289
--- /dev/null
+++ b/t/t1021-rerere-in-workdir.sh
@@ -0,0 +1,58 @@
+#!/bin/sh
+
+test_description='rerere run in a workdir'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success SYMLINKS setup '
+ git config rerere.enabled true &&
+ >world &&
+ git add world &&
+ test_tick &&
+ git commit -m initial &&
+
+ echo hello >world &&
+ test_tick &&
+ git commit -a -m hello &&
+
+ git checkout -b side HEAD^ &&
+ echo goodbye >world &&
+ test_tick &&
+ git commit -a -m goodbye &&
+
+ git checkout main
+'
+
+test_expect_success SYMLINKS 'rerere in workdir' '
+ rm -rf .git/rr-cache &&
+ "$SHELL_PATH" "$TEST_DIRECTORY/../contrib/workdir/git-new-workdir" . work &&
+ (
+ cd work &&
+ test_must_fail git merge side &&
+ git rerere status >actual &&
+ echo world >expect &&
+ test_cmp expect actual
+ )
+'
+
+# This fails because we don't resolve relative symlink in mkdir_in_gitdir()
+# For the purpose of helping contrib/workdir/git-new-workdir users, we do not
+# have to support relative symlinks, but it might be nicer to make this work
+# with a relative symbolic link someday.
+test_expect_failure SYMLINKS 'rerere in workdir (relative)' '
+ rm -rf .git/rr-cache &&
+ "$SHELL_PATH" "$TEST_DIRECTORY/../contrib/workdir/git-new-workdir" . krow &&
+ (
+ cd krow &&
+ rm -f .git/rr-cache &&
+ ln -s ../.git/rr-cache .git/rr-cache &&
+ test_must_fail git merge side &&
+ git rerere status >actual &&
+ echo world >expect &&
+ test_cmp expect actual
+ )
+'
+
+test_done
diff --git a/t/t1022-read-tree-partial-clone.sh b/t/t1022-read-tree-partial-clone.sh
new file mode 100755
index 0000000..cca4380
--- /dev/null
+++ b/t/t1022-read-tree-partial-clone.sh
@@ -0,0 +1,30 @@
+#!/bin/sh
+
+test_description='git read-tree in partial clones'
+
+TEST_NO_CREATE_REPO=1
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'read-tree in partial clone prefetches in one batch' '
+ test_when_finished "rm -rf server client trace" &&
+
+ git init server &&
+ echo foo >server/one &&
+ echo bar >server/two &&
+ git -C server add one two &&
+ git -C server commit -m "initial commit" &&
+ TREE=$(git -C server rev-parse HEAD^{tree}) &&
+
+ git -C server config uploadpack.allowfilter 1 &&
+ git -C server config uploadpack.allowanysha1inwant 1 &&
+ git clone --bare --filter=blob:none "file://$(pwd)/server" client &&
+ GIT_TRACE_PACKET="$(pwd)/trace" git -C client read-tree $TREE $TREE &&
+
+ # "done" marks the end of negotiation (once per fetch). Expect that
+ # only one fetch occurs.
+ grep "fetch> done" trace >donelines &&
+ test_line_count = 1 donelines
+'
+
+test_done
diff --git a/t/t1050-large.sh b/t/t1050-large.sh
new file mode 100755
index 0000000..c71932b
--- /dev/null
+++ b/t/t1050-large.sh
@@ -0,0 +1,215 @@
+#!/bin/sh
+# Copyright (c) 2011, Google Inc.
+
+test_description='adding and checking out large blobs'
+
+. ./test-lib.sh
+
+test_expect_success 'core.bigFileThreshold must be non-negative' '
+ test_must_fail git -c core.bigFileThreshold=-1 rev-parse >out 2>err &&
+ grep "bad numeric config value" err &&
+ test_must_be_empty out
+'
+
+test_expect_success setup '
+ # clone does not allow us to pass core.bigfilethreshold to
+ # new repos, so set core.bigfilethreshold globally
+ git config --global core.bigfilethreshold 200k &&
+ printf "%2000000s" X >large1 &&
+ cp large1 large2 &&
+ cp large1 large3 &&
+ printf "%2500000s" Y >huge &&
+ GIT_ALLOC_LIMIT=1500k &&
+ export GIT_ALLOC_LIMIT
+'
+
+test_expect_success 'enter "large" codepath, with small core.bigFileThreshold' '
+ test_when_finished "rm -rf repo" &&
+
+ git init --bare repo &&
+ echo large | git -C repo hash-object -w --stdin &&
+ git -C repo -c core.bigfilethreshold=4 fsck
+'
+
+# add a large file with different settings
+while read expect config
+do
+ test_expect_success "add with $config" '
+ test_when_finished "rm -f .git/objects/pack/pack-*.* .git/index" &&
+ git $config add large1 &&
+ sz=$(test_file_size .git/objects/pack/pack-*.pack) &&
+ case "$expect" in
+ small) test "$sz" -le 100000 ;;
+ large) test "$sz" -ge 100000 ;;
+ esac
+ '
+done <<\EOF
+large -c core.compression=0
+small -c core.compression=9
+large -c core.compression=0 -c pack.compression=0
+large -c core.compression=9 -c pack.compression=0
+small -c core.compression=0 -c pack.compression=9
+small -c core.compression=9 -c pack.compression=9
+large -c pack.compression=0
+small -c pack.compression=9
+EOF
+
+test_expect_success 'add a large file or two' '
+ git add large1 huge large2 &&
+ # make sure we got a single packfile and no loose objects
+ count=0 idx= &&
+ for p in .git/objects/pack/pack-*.pack
+ do
+ count=$(( $count + 1 )) &&
+ test_path_is_file "$p" &&
+ idx=${p%.pack}.idx &&
+ test_path_is_file "$idx" || return 1
+ done &&
+ test $count = 1 &&
+ cnt=$(git show-index <"$idx" | wc -l) &&
+ test $cnt = 2 &&
+ for l in .git/objects/$OIDPATH_REGEX
+ do
+ test_path_is_missing "$l" || return 1
+ done &&
+
+ # attempt to add another copy of the same
+ git add large3 &&
+ bad= count=0 &&
+ for p in .git/objects/pack/pack-*.pack
+ do
+ count=$(( $count + 1 )) &&
+ test_path_is_file "$p" &&
+ idx=${p%.pack}.idx &&
+ test_path_is_file "$idx" || return 1
+ done &&
+ test $count = 1
+'
+
+test_expect_success 'checkout a large file' '
+ large1=$(git rev-parse :large1) &&
+ git update-index --add --cacheinfo 100644 $large1 another &&
+ git checkout another &&
+ test_cmp large1 another
+'
+
+test_expect_success 'packsize limit' '
+ test_create_repo mid &&
+ (
+ cd mid &&
+ git config core.bigfilethreshold 64k &&
+ git config pack.packsizelimit 256k &&
+
+ # mid1 and mid2 will fit within 256k limit but
+ # appending mid3 will bust the limit and will
+ # result in a separate packfile.
+ test-tool genrandom "a" $(( 66 * 1024 )) >mid1 &&
+ test-tool genrandom "b" $(( 80 * 1024 )) >mid2 &&
+ test-tool genrandom "c" $(( 128 * 1024 )) >mid3 &&
+ git add mid1 mid2 mid3 &&
+
+ count=0 &&
+ for pi in .git/objects/pack/pack-*.idx
+ do
+ test_path_is_file "$pi" && count=$(( $count + 1 )) || return 1
+ done &&
+ test $count = 2 &&
+
+ (
+ git hash-object --stdin <mid1 &&
+ git hash-object --stdin <mid2 &&
+ git hash-object --stdin <mid3
+ ) |
+ sort >expect &&
+
+ for pi in .git/objects/pack/pack-*.idx
+ do
+ git show-index <"$pi" || return 1
+ done |
+ sed -e "s/^[0-9]* \([0-9a-f]*\) .*/\1/" |
+ sort >actual &&
+
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'diff --raw' '
+ git commit -q -m initial &&
+ echo modified >>large1 &&
+ git add large1 &&
+ git commit -q -m modified &&
+ git diff --raw HEAD^
+'
+
+test_expect_success 'diff --stat' '
+ git diff --stat HEAD^ HEAD
+'
+
+test_expect_success 'diff' '
+ git diff HEAD^ HEAD >actual &&
+ grep "Binary files.*differ" actual
+'
+
+test_expect_success 'diff --cached' '
+ git diff --cached HEAD^ >actual &&
+ grep "Binary files.*differ" actual
+'
+
+test_expect_success 'hash-object' '
+ git hash-object large1
+'
+
+test_expect_success 'cat-file a large file' '
+ git cat-file blob :large1 >/dev/null
+'
+
+test_expect_success 'cat-file a large file from a tag' '
+ git tag -m largefile largefiletag :large1 &&
+ git cat-file blob largefiletag >/dev/null
+'
+
+test_expect_success 'git-show a large file' '
+ git show :large1 >/dev/null
+
+'
+
+test_expect_success 'index-pack' '
+ git clone file://"$(pwd)"/.git foo &&
+ GIT_DIR=non-existent git index-pack --object-format=$(test_oid algo) \
+ --strict --verify foo/.git/objects/pack/*.pack
+'
+
+test_expect_success 'repack' '
+ git repack -ad
+'
+
+test_expect_success 'pack-objects with large loose object' '
+ SHA1=$(git hash-object huge) &&
+ test_create_repo loose &&
+ echo $SHA1 | git pack-objects --stdout |
+ GIT_ALLOC_LIMIT=0 GIT_DIR=loose/.git git unpack-objects &&
+ echo $SHA1 | GIT_DIR=loose/.git git pack-objects pack &&
+ test_create_repo packed &&
+ mv pack-* packed/.git/objects/pack &&
+ GIT_DIR=packed/.git git cat-file blob $SHA1 >actual &&
+ test_cmp huge actual
+'
+
+test_expect_success 'tar archiving' '
+ git archive --format=tar HEAD >/dev/null
+'
+
+test_expect_success 'zip archiving, store only' '
+ git archive --format=zip -0 HEAD >/dev/null
+'
+
+test_expect_success 'zip archiving, deflate' '
+ git archive --format=zip HEAD >/dev/null
+'
+
+test_expect_success 'fsck large blobs' '
+ git fsck 2>err &&
+ test_must_be_empty err
+'
+
+test_done
diff --git a/t/t1051-large-conversion.sh b/t/t1051-large-conversion.sh
new file mode 100755
index 0000000..f6709c9
--- /dev/null
+++ b/t/t1051-large-conversion.sh
@@ -0,0 +1,114 @@
+#!/bin/sh
+
+test_description='test conversion filters on large files'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+set_attr() {
+ test_when_finished 'rm -f .gitattributes' &&
+ echo "* $*" >.gitattributes
+}
+
+check_input() {
+ git read-tree --empty &&
+ git add small large &&
+ git cat-file blob :small >small.index &&
+ git cat-file blob :large | head -n 1 >large.index &&
+ test_cmp small.index large.index
+}
+
+check_output() {
+ rm -f small large &&
+ git checkout small large &&
+ head -n 1 large >large.head &&
+ test_cmp small large.head
+}
+
+test_expect_success 'setup input tests' '
+ printf "\$Id: foo\$\\r\\n" >small &&
+ cat small small >large &&
+ git config core.bigfilethreshold 20 &&
+ git config filter.test.clean "sed s/.*/CLEAN/"
+'
+
+test_expect_success 'autocrlf=true converts on input' '
+ test_config core.autocrlf true &&
+ check_input
+'
+
+test_expect_success 'eol=crlf converts on input' '
+ set_attr eol=crlf &&
+ check_input
+'
+
+test_expect_success 'ident converts on input' '
+ set_attr ident &&
+ check_input
+'
+
+test_expect_success 'user-defined filters convert on input' '
+ set_attr filter=test &&
+ check_input
+'
+
+test_expect_success 'setup output tests' '
+ echo "\$Id\$" >small &&
+ cat small small >large &&
+ git add small large &&
+ git config core.bigfilethreshold 7 &&
+ git config filter.test.smudge "sed s/.*/SMUDGE/"
+'
+
+test_expect_success 'autocrlf=true converts on output' '
+ test_config core.autocrlf true &&
+ check_output
+'
+
+test_expect_success 'eol=crlf converts on output' '
+ set_attr eol=crlf &&
+ check_output
+'
+
+test_expect_success 'user-defined filters convert on output' '
+ set_attr filter=test &&
+ check_output
+'
+
+test_expect_success 'ident converts on output' '
+ set_attr ident &&
+ rm -f small large &&
+ git checkout small large &&
+ sed -n "s/Id: .*/Id: SHA/p" <small >small.clean &&
+ head -n 1 large >large.head &&
+ sed -n "s/Id: .*/Id: SHA/p" <large.head >large.clean &&
+ test_cmp small.clean large.clean
+'
+
+# This smudge filter prepends 5GB of zeros to the file it checks out. This
+# ensures that smudging doesn't mangle large files on 64-bit Windows.
+test_expect_success EXPENSIVE,SIZE_T_IS_64BIT,!LONG_IS_64BIT \
+ 'files over 4GB convert on output' '
+ test_commit test small "a small file" &&
+ small_size=$(test_file_size small) &&
+ test_config filter.makelarge.smudge \
+ "test-tool genzeros $((5*1024*1024*1024)) && cat" &&
+ echo "small filter=makelarge" >.gitattributes &&
+ rm small &&
+ git checkout -- small &&
+ size=$(test_file_size small) &&
+ test "$size" -eq $((5 * 1024 * 1024 * 1024 + $small_size))
+'
+
+# This clean filter writes down the size of input it receives. By checking against
+# the actual size, we ensure that cleaning doesn't mangle large files on 64-bit Windows.
+test_expect_success EXPENSIVE,SIZE_T_IS_64BIT,!LONG_IS_64BIT \
+ 'files over 4GB convert on input' '
+ test-tool genzeros $((5*1024*1024*1024)) >big &&
+ test_config filter.checklarge.clean "wc -c >big.size" &&
+ echo "big filter=checklarge" >.gitattributes &&
+ git add big &&
+ test $(test_file_size big) -eq $(cat big.size)
+'
+
+test_done
diff --git a/t/t1060-object-corruption.sh b/t/t1060-object-corruption.sh
new file mode 100755
index 0000000..5e0f0a3
--- /dev/null
+++ b/t/t1060-object-corruption.sh
@@ -0,0 +1,149 @@
+#!/bin/sh
+
+test_description='see how we handle various forms of corruption'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# convert "1234abcd" to ".git/objects/12/34abcd"
+obj_to_file() {
+ echo "$(git rev-parse --git-dir)/objects/$(git rev-parse "$1" | sed 's,..,&/,')"
+}
+
+# Convert byte at offset "$2" of object "$1" into '\0'
+corrupt_byte() {
+ obj_file=$(obj_to_file "$1") &&
+ chmod +w "$obj_file" &&
+ printf '\0' | dd of="$obj_file" bs=1 seek="$2" conv=notrunc
+}
+
+test_expect_success 'setup corrupt repo' '
+ git init bit-error &&
+ (
+ cd bit-error &&
+ test_commit content &&
+ corrupt_byte HEAD:content.t 10
+ ) &&
+ git init no-bit-error &&
+ (
+ # distinct commit from bit-error, but containing a
+ # non-corrupted version of the same blob
+ cd no-bit-error &&
+ test_tick &&
+ test_commit content
+ )
+'
+
+test_expect_success 'setup repo with missing object' '
+ git init missing &&
+ (
+ cd missing &&
+ test_commit content &&
+ rm -f "$(obj_to_file HEAD:content.t)"
+ )
+'
+
+test_expect_success 'setup repo with misnamed object' '
+ git init misnamed &&
+ (
+ cd misnamed &&
+ test_commit content &&
+ good=$(obj_to_file HEAD:content.t) &&
+ blob=$(echo corrupt | git hash-object -w --stdin) &&
+ bad=$(obj_to_file $blob) &&
+ rm -f "$good" &&
+ mv "$bad" "$good"
+ )
+'
+
+test_expect_success 'streaming a corrupt blob fails' '
+ (
+ cd bit-error &&
+ test_must_fail git cat-file blob HEAD:content.t
+ )
+'
+
+test_expect_success 'getting type of a corrupt blob fails' '
+ (
+ cd bit-error &&
+ test_must_fail git cat-file -s HEAD:content.t
+ )
+'
+
+test_expect_success 'read-tree -u detects bit-errors in blobs' '
+ (
+ cd bit-error &&
+ rm -f content.t &&
+ test_must_fail git read-tree --reset -u HEAD
+ )
+'
+
+test_expect_success 'read-tree -u detects missing objects' '
+ (
+ cd missing &&
+ rm -f content.t &&
+ test_must_fail git read-tree --reset -u HEAD
+ )
+'
+
+# We use --bare to make sure that the transport detects it, not the checkout
+# phase.
+test_expect_success 'clone --no-local --bare detects corruption' '
+ test_must_fail git clone --no-local --bare bit-error corrupt-transport
+'
+
+test_expect_success 'clone --no-local --bare detects missing object' '
+ test_must_fail git clone --no-local --bare missing missing-transport
+'
+
+test_expect_success 'clone --no-local --bare detects misnamed object' '
+ test_must_fail git clone --no-local --bare misnamed misnamed-transport
+'
+
+# We do not expect --local to detect corruption at the transport layer,
+# so we are really checking the checkout() code path.
+test_expect_success 'clone --local detects corruption' '
+ test_must_fail git clone --local bit-error corrupt-checkout
+'
+
+test_expect_success 'error detected during checkout leaves repo intact' '
+ test_path_is_dir corrupt-checkout/.git
+'
+
+test_expect_success 'clone --local detects missing objects' '
+ test_must_fail git clone --local missing missing-checkout
+'
+
+test_expect_failure 'clone --local detects misnamed objects' '
+ test_must_fail git clone --local misnamed misnamed-checkout
+'
+
+test_expect_success 'fetch into corrupted repo with index-pack' '
+ cp -R bit-error bit-error-cp &&
+ test_when_finished "rm -rf bit-error-cp" &&
+ (
+ cd bit-error-cp &&
+ test_must_fail git -c transfer.unpackLimit=1 \
+ fetch ../no-bit-error 2>stderr &&
+ test_grep ! -i collision stderr
+ )
+'
+
+test_expect_success 'internal tree objects are not "missing"' '
+ git init missing-empty &&
+ (
+ cd missing-empty &&
+ empty_tree=$(git hash-object -t tree /dev/null) &&
+ commit=$(echo foo | git commit-tree $empty_tree) &&
+ git rev-list --objects $commit
+ )
+'
+
+test_expect_success 'partial clone of corrupted repository' '
+ test_config -C misnamed uploadpack.allowFilter true &&
+ git clone --no-local --no-checkout --filter=blob:none \
+ misnamed corrupt-partial && \
+ test_must_fail git -C corrupt-partial checkout --force
+'
+
+test_done
diff --git a/t/t1090-sparse-checkout-scope.sh b/t/t1090-sparse-checkout-scope.sh
new file mode 100755
index 0000000..3a14218
--- /dev/null
+++ b/t/t1090-sparse-checkout-scope.sh
@@ -0,0 +1,109 @@
+#!/bin/sh
+
+test_description='sparse checkout scope tests'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_CREATE_REPO_NO_TEMPLATE=1
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ echo "initial" >a &&
+ echo "initial" >b &&
+ echo "initial" >c &&
+ git add a b c &&
+ git commit -m "initial commit"
+'
+
+test_expect_success 'create feature branch' '
+ git checkout -b feature &&
+ echo "modified" >b &&
+ echo "modified" >c &&
+ git add b c &&
+ git commit -m "modification"
+'
+
+test_expect_success 'perform sparse checkout of main' '
+ git config --local --bool core.sparsecheckout true &&
+ mkdir .git/info &&
+ echo "!/*" >.git/info/sparse-checkout &&
+ echo "/a" >>.git/info/sparse-checkout &&
+ echo "/c" >>.git/info/sparse-checkout &&
+ git checkout main &&
+ test_path_is_file a &&
+ test_path_is_missing b &&
+ test_path_is_file c
+'
+
+test_expect_success 'merge feature branch into sparse checkout of main' '
+ git merge feature &&
+ test_path_is_file a &&
+ test_path_is_missing b &&
+ test_path_is_file c &&
+ test "$(cat c)" = "modified"
+'
+
+test_expect_success 'return to full checkout of main' '
+ git checkout feature &&
+ echo "/*" >.git/info/sparse-checkout &&
+ git checkout main &&
+ test_path_is_file a &&
+ test_path_is_file b &&
+ test_path_is_file c &&
+ test "$(cat b)" = "modified"
+'
+
+test_expect_success 'skip-worktree on files outside sparse patterns' '
+ git sparse-checkout disable &&
+ git sparse-checkout set --no-cone "a*" &&
+ git checkout-index --all --ignore-skip-worktree-bits &&
+
+ git ls-files -t >output &&
+ ! grep ^S output >actual &&
+ test_must_be_empty actual &&
+
+ test_config sparse.expectFilesOutsideOfPatterns true &&
+ cat <<-\EOF >expect &&
+ S b
+ S c
+ EOF
+ git ls-files -t >output &&
+ grep ^S output >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'in partial clone, sparse checkout only fetches needed blobs' '
+ test_create_repo server &&
+ git clone --template= "file://$(pwd)/server" client &&
+
+ test_config -C server uploadpack.allowfilter 1 &&
+ test_config -C server uploadpack.allowanysha1inwant 1 &&
+ echo a >server/a &&
+ echo bb >server/b &&
+ mkdir server/c &&
+ echo ccc >server/c/c &&
+ git -C server add a b c/c &&
+ git -C server commit -m message &&
+
+ test_config -C client core.sparsecheckout 1 &&
+ mkdir client/.git/info &&
+ echo "!/*" >client/.git/info/sparse-checkout &&
+ echo "/a" >>client/.git/info/sparse-checkout &&
+ git -C client fetch --filter=blob:none origin &&
+ git -C client checkout FETCH_HEAD &&
+
+ git -C client rev-list HEAD \
+ --quiet --objects --missing=print >unsorted_actual &&
+ (
+ printf "?" &&
+ git hash-object server/b &&
+ printf "?" &&
+ git hash-object server/c/c
+ ) >unsorted_expect &&
+ sort unsorted_actual >actual &&
+ sort unsorted_expect >expect &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t1091-sparse-checkout-builtin.sh b/t/t1091-sparse-checkout-builtin.sh
new file mode 100755
index 0000000..f67611d
--- /dev/null
+++ b/t/t1091-sparse-checkout-builtin.sh
@@ -0,0 +1,1048 @@
+#!/bin/sh
+
+test_description='sparse checkout builtin tests'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+GIT_TEST_SPLIT_INDEX=false
+export GIT_TEST_SPLIT_INDEX
+
+. ./test-lib.sh
+
+list_files() {
+ # Do not replace this with 'ls "$1"', as "ls" with BSD-lineage
+ # enables "-A" by default for root and ends up including ".git" and
+ # such in its output. (Note, though, that running the test suite as
+ # root is generally not recommended.)
+ (cd "$1" && printf '%s\n' *)
+}
+
+check_files() {
+ list_files "$1" >actual &&
+ shift &&
+ printf "%s\n" $@ >expect &&
+ test_cmp expect actual
+}
+
+test_expect_success 'setup' '
+ git init repo &&
+ (
+ cd repo &&
+ echo "initial" >a &&
+ mkdir folder1 folder2 deep &&
+ mkdir deep/deeper1 deep/deeper2 &&
+ mkdir deep/deeper1/deepest &&
+ cp a folder1 &&
+ cp a folder2 &&
+ cp a deep &&
+ cp a deep/deeper1 &&
+ cp a deep/deeper2 &&
+ cp a deep/deeper1/deepest &&
+ git add . &&
+ git commit -m "initial commit"
+ )
+'
+
+test_expect_success 'git sparse-checkout list (not sparse)' '
+ test_must_fail git -C repo sparse-checkout list >list 2>err &&
+ test_must_be_empty list &&
+ test_grep "this worktree is not sparse" err
+'
+
+test_expect_success 'git sparse-checkout list (not sparse)' '
+ git -C repo sparse-checkout set &&
+ rm repo/.git/info/sparse-checkout &&
+ git -C repo sparse-checkout list >list 2>err &&
+ test_must_be_empty list &&
+ test_grep "this worktree is not sparse (sparse-checkout file may not exist)" err
+'
+
+test_expect_success 'git sparse-checkout list (populated)' '
+ test_when_finished rm -f repo/.git/info/sparse-checkout &&
+ cat >repo/.git/info/sparse-checkout <<-\EOF &&
+ /folder1/*
+ /deep/
+ **/a
+ !*bin*
+ EOF
+ cp repo/.git/info/sparse-checkout expect &&
+ git -C repo sparse-checkout list >list &&
+ test_cmp expect list
+'
+
+test_expect_success 'git sparse-checkout init' '
+ git -C repo sparse-checkout init --no-cone &&
+ cat >expect <<-\EOF &&
+ /*
+ !/*/
+ EOF
+ test_cmp expect repo/.git/info/sparse-checkout &&
+ test_cmp_config -C repo true core.sparsecheckout &&
+ check_files repo a
+'
+
+test_expect_success 'git sparse-checkout init in empty repo' '
+ test_when_finished rm -rf empty-repo blank-template &&
+ git init --template= empty-repo &&
+ git -C empty-repo sparse-checkout init
+'
+
+test_expect_success 'git sparse-checkout list after init' '
+ git -C repo sparse-checkout list >actual &&
+ cat >expect <<-\EOF &&
+ /*
+ !/*/
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'init with existing sparse-checkout' '
+ echo "*folder*" >> repo/.git/info/sparse-checkout &&
+ git -C repo sparse-checkout init &&
+ cat >expect <<-\EOF &&
+ /*
+ !/*/
+ *folder*
+ EOF
+ test_cmp expect repo/.git/info/sparse-checkout &&
+ check_files repo a folder1 folder2
+'
+
+test_expect_success 'clone --sparse' '
+ git clone --sparse "file://$(pwd)/repo" clone &&
+ git -C clone sparse-checkout reapply --no-cone &&
+ git -C clone sparse-checkout list >actual &&
+ cat >expect <<-\EOF &&
+ /*
+ !/*/
+ EOF
+ test_cmp expect actual &&
+ check_files clone a
+'
+
+test_expect_success 'switching to cone mode with non-cone mode patterns' '
+ git init bad-patterns &&
+ (
+ cd bad-patterns &&
+ git sparse-checkout init --no-cone &&
+ git sparse-checkout add dir &&
+ git config --worktree core.sparseCheckoutCone true &&
+ test_must_fail git sparse-checkout add dir 2>err &&
+ grep "existing sparse-checkout patterns do not use cone mode" err
+ )
+'
+
+test_expect_success 'interaction with clone --no-checkout (unborn index)' '
+ git clone --no-checkout "file://$(pwd)/repo" clone_no_checkout &&
+ git -C clone_no_checkout sparse-checkout init --cone &&
+ git -C clone_no_checkout sparse-checkout set folder1 &&
+
+ git -C clone_no_checkout sparse-checkout list >actual &&
+ cat >expect <<-\EOF &&
+ folder1
+ EOF
+ test_cmp expect actual &&
+
+ # nothing checked out, expect "No such file or directory"
+ ! ls clone_no_checkout/* >actual &&
+ test_must_be_empty actual &&
+ test_path_is_missing clone_no_checkout/.git/index &&
+
+ # No branch is checked out until we manually switch to one
+ git -C clone_no_checkout switch main &&
+ test_path_is_file clone_no_checkout/.git/index &&
+ check_files clone_no_checkout a folder1
+'
+
+test_expect_success 'set enables config' '
+ git init worktree-config &&
+ (
+ cd worktree-config &&
+ test_commit test file &&
+ test_path_is_missing .git/config.worktree &&
+ git sparse-checkout set nothing &&
+ test_path_is_file .git/config.worktree &&
+ test_cmp_config true core.sparseCheckout
+ )
+'
+
+test_expect_success 'set sparse-checkout using builtin' '
+ git -C repo sparse-checkout set "/*" "!/*/" "*folder*" &&
+ cat >expect <<-\EOF &&
+ /*
+ !/*/
+ *folder*
+ EOF
+ git -C repo sparse-checkout list >actual &&
+ test_cmp expect actual &&
+ test_cmp expect repo/.git/info/sparse-checkout &&
+ check_files repo a folder1 folder2
+'
+
+test_expect_success 'set sparse-checkout using --stdin' '
+ cat >expect <<-\EOF &&
+ /*
+ !/*/
+ /folder1/
+ /folder2/
+ EOF
+ git -C repo sparse-checkout set --stdin <expect &&
+ git -C repo sparse-checkout list >actual &&
+ test_cmp expect actual &&
+ test_cmp expect repo/.git/info/sparse-checkout &&
+ check_files repo "a folder1 folder2"
+'
+
+test_expect_success 'add to sparse-checkout' '
+ cat repo/.git/info/sparse-checkout >old &&
+ test_when_finished cp old repo/.git/info/sparse-checkout &&
+ cat >add <<-\EOF &&
+ pattern1
+ /folder1/
+ pattern2
+ EOF
+ cat old >expect &&
+ cat add >>expect &&
+ git -C repo sparse-checkout add --stdin <add &&
+ git -C repo sparse-checkout list >actual &&
+ test_cmp expect actual &&
+ test_cmp expect repo/.git/info/sparse-checkout &&
+ check_files repo "a folder1 folder2"
+'
+
+test_expect_success 'worktree: add copies sparse-checkout patterns' '
+ cat repo/.git/info/sparse-checkout >old &&
+ test_when_finished cp old repo/.git/info/sparse-checkout &&
+ test_when_finished git -C repo worktree remove ../worktree &&
+ git -C repo sparse-checkout set --no-cone "/*" &&
+ git -C repo worktree add --quiet ../worktree 2>err &&
+ test_must_be_empty err &&
+ new="$(git -C worktree rev-parse --git-path info/sparse-checkout)" &&
+ test_path_is_file "$new" &&
+ test_cmp repo/.git/info/sparse-checkout "$new" &&
+ git -C worktree sparse-checkout set --cone &&
+ test_cmp_config -C worktree true core.sparseCheckoutCone &&
+ test_must_fail git -C repo core.sparseCheckoutCone
+'
+
+test_expect_success 'cone mode: match patterns' '
+ git -C repo config --worktree core.sparseCheckoutCone true &&
+ rm -rf repo/a repo/folder1 repo/folder2 &&
+ git -C repo read-tree -mu HEAD 2>err &&
+ test_grep ! "disabling cone patterns" err &&
+ git -C repo reset --hard &&
+ check_files repo a folder1 folder2
+'
+
+test_expect_success 'cone mode: warn on bad pattern' '
+ test_when_finished mv sparse-checkout repo/.git/info/ &&
+ cp repo/.git/info/sparse-checkout . &&
+ echo "!/deep/deeper/*/" >>repo/.git/info/sparse-checkout &&
+ git -C repo read-tree -mu HEAD 2>err &&
+ test_grep "unrecognized negative pattern" err
+'
+
+test_expect_success 'sparse-checkout disable' '
+ test_when_finished rm -rf repo/.git/info/sparse-checkout &&
+ git -C repo sparse-checkout disable &&
+ test_path_is_file repo/.git/info/sparse-checkout &&
+ git -C repo config --list >config &&
+ test_must_fail git config core.sparseCheckout &&
+ check_files repo a deep folder1 folder2
+'
+
+test_expect_success 'sparse-index enabled and disabled' '
+ git -C repo sparse-checkout init --cone --sparse-index &&
+ test_cmp_config -C repo true index.sparse &&
+ git -C repo ls-files --sparse >sparse &&
+ git -C repo sparse-checkout disable &&
+ git -C repo ls-files --sparse >full &&
+
+ cat >expect <<-\EOF &&
+ @@ -1,4 +1,7 @@
+ a
+ -deep/
+ -folder1/
+ -folder2/
+ +deep/a
+ +deep/deeper1/a
+ +deep/deeper1/deepest/a
+ +deep/deeper2/a
+ +folder1/a
+ +folder2/a
+ EOF
+
+ diff -u sparse full | tail -n +3 >actual &&
+ test_cmp expect actual &&
+
+ git -C repo config --list >config &&
+ test_cmp_config -C repo false index.sparse
+'
+
+test_expect_success 'cone mode: init and set' '
+ git -C repo sparse-checkout init --cone &&
+ git -C repo config --list >config &&
+ test_grep "core.sparsecheckoutcone=true" config &&
+ list_files repo >dir &&
+ echo a >expect &&
+ test_cmp expect dir &&
+ git -C repo sparse-checkout set deep/deeper1/deepest/ 2>err &&
+ test_must_be_empty err &&
+ check_files repo a deep &&
+ check_files repo/deep a deeper1 &&
+ check_files repo/deep/deeper1 a deepest &&
+ cat >expect <<-\EOF &&
+ /*
+ !/*/
+ /deep/
+ !/deep/*/
+ /deep/deeper1/
+ !/deep/deeper1/*/
+ /deep/deeper1/deepest/
+ EOF
+ test_cmp expect repo/.git/info/sparse-checkout &&
+ git -C repo sparse-checkout set --stdin 2>err <<-\EOF &&
+ folder1
+ folder2
+ EOF
+ test_must_be_empty err &&
+ check_files repo a folder1 folder2
+'
+
+test_expect_success 'cone mode: list' '
+ cat >expect <<-\EOF &&
+ folder1
+ folder2
+ EOF
+ git -C repo sparse-checkout set --stdin <expect &&
+ git -C repo sparse-checkout list >actual 2>err &&
+ test_must_be_empty err &&
+ test_cmp expect actual
+'
+
+test_expect_success 'cone mode: set with nested folders' '
+ git -C repo sparse-checkout set deep deep/deeper1/deepest 2>err &&
+ test_line_count = 0 err &&
+ cat >expect <<-\EOF &&
+ /*
+ !/*/
+ /deep/
+ EOF
+ test_cmp repo/.git/info/sparse-checkout expect
+'
+
+test_expect_success 'cone mode: add independent path' '
+ git -C repo sparse-checkout set deep/deeper1 &&
+ git -C repo sparse-checkout add folder1 &&
+ cat >expect <<-\EOF &&
+ /*
+ !/*/
+ /deep/
+ !/deep/*/
+ /deep/deeper1/
+ /folder1/
+ EOF
+ test_cmp expect repo/.git/info/sparse-checkout &&
+ check_files repo a deep folder1
+'
+
+test_expect_success 'cone mode: add sibling path' '
+ git -C repo sparse-checkout set deep/deeper1 &&
+ git -C repo sparse-checkout add deep/deeper2 &&
+ cat >expect <<-\EOF &&
+ /*
+ !/*/
+ /deep/
+ !/deep/*/
+ /deep/deeper1/
+ /deep/deeper2/
+ EOF
+ test_cmp expect repo/.git/info/sparse-checkout &&
+ check_files repo a deep
+'
+
+test_expect_success 'cone mode: add parent path' '
+ git -C repo sparse-checkout set deep/deeper1 folder1 &&
+ git -C repo sparse-checkout add deep &&
+ cat >expect <<-\EOF &&
+ /*
+ !/*/
+ /deep/
+ /folder1/
+ EOF
+ test_cmp expect repo/.git/info/sparse-checkout &&
+ check_files repo a deep folder1
+'
+
+test_expect_success 'not-up-to-date does not block rest of sparsification' '
+ test_when_finished git -C repo sparse-checkout disable &&
+ test_when_finished git -C repo reset --hard &&
+ git -C repo sparse-checkout set deep &&
+
+ echo update >repo/deep/deeper2/a &&
+ cp repo/.git/info/sparse-checkout expect &&
+ test_write_lines "!/deep/*/" "/deep/deeper1/" >>expect &&
+
+ git -C repo sparse-checkout set deep/deeper1 2>err &&
+
+ test_grep "The following paths are not up to date" err &&
+ test_cmp expect repo/.git/info/sparse-checkout &&
+ check_files repo/deep a deeper1 deeper2 &&
+ check_files repo/deep/deeper1 a deepest &&
+ check_files repo/deep/deeper1/deepest a &&
+ check_files repo/deep/deeper2 a
+'
+
+test_expect_success 'revert to old sparse-checkout on empty update' '
+ git init empty-test &&
+ (
+ echo >file &&
+ git add file &&
+ git commit -m "test" &&
+ git sparse-checkout set nothing 2>err &&
+ test_grep ! "Sparse checkout leaves no entry on working directory" err &&
+ test_grep ! ".git/index.lock" err &&
+ git sparse-checkout set --no-cone file
+ )
+'
+
+test_expect_success 'fail when lock is taken' '
+ test_when_finished rm -rf repo/.git/info/sparse-checkout.lock &&
+ touch repo/.git/info/sparse-checkout.lock &&
+ test_must_fail git -C repo sparse-checkout set deep 2>err &&
+ test_grep "Unable to create .*\.lock" err
+'
+
+test_expect_success '.gitignore should not warn about cone mode' '
+ git -C repo config --worktree core.sparseCheckoutCone true &&
+ echo "**/bin/*" >repo/.gitignore &&
+ git -C repo reset --hard 2>err &&
+ test_grep ! "disabling cone patterns" err
+'
+
+test_expect_success 'sparse-checkout (init|set|disable) warns with dirty status' '
+ git clone repo dirty &&
+ echo dirty >dirty/folder1/a &&
+
+ git -C dirty sparse-checkout init --no-cone 2>err &&
+ test_grep "warning.*The following paths are not up to date" err &&
+
+ git -C dirty sparse-checkout set /folder2/* /deep/deeper1/* 2>err &&
+ test_grep "warning.*The following paths are not up to date" err &&
+ test_path_is_file dirty/folder1/a &&
+
+ git -C dirty sparse-checkout disable 2>err &&
+ test_must_be_empty err &&
+
+ git -C dirty reset --hard &&
+ git -C dirty sparse-checkout init --no-cone &&
+ git -C dirty sparse-checkout set /folder2/* /deep/deeper1/* &&
+ test_path_is_missing dirty/folder1/a &&
+ git -C dirty sparse-checkout disable &&
+ test_path_is_file dirty/folder1/a
+'
+
+test_expect_success 'sparse-checkout (init|set|disable) warns with unmerged status' '
+ git clone repo unmerged &&
+
+ cat >input <<-EOF &&
+ 0 $ZERO_OID folder1/a
+ 100644 $(git -C unmerged rev-parse HEAD:folder1/a) 1 folder1/a
+ EOF
+ git -C unmerged update-index --index-info <input &&
+
+ git -C unmerged sparse-checkout init --no-cone 2>err &&
+ test_grep "warning.*The following paths are unmerged" err &&
+
+ git -C unmerged sparse-checkout set /folder2/* /deep/deeper1/* 2>err &&
+ test_grep "warning.*The following paths are unmerged" err &&
+ test_path_is_file dirty/folder1/a &&
+
+ git -C unmerged sparse-checkout disable 2>err &&
+ test_grep "warning.*The following paths are unmerged" err &&
+
+ git -C unmerged reset --hard &&
+ git -C unmerged sparse-checkout init --no-cone &&
+ git -C unmerged sparse-checkout set /folder2/* /deep/deeper1/* &&
+ git -C unmerged sparse-checkout disable
+'
+
+test_expect_failure 'sparse-checkout reapply' '
+ git clone repo tweak &&
+
+ echo dirty >tweak/deep/deeper2/a &&
+
+ cat >input <<-EOF &&
+ 0 $ZERO_OID folder1/a
+ 100644 $(git -C tweak rev-parse HEAD:folder1/a) 1 folder1/a
+ EOF
+ git -C tweak update-index --index-info <input &&
+
+ git -C tweak sparse-checkout init --cone 2>err &&
+ test_grep "warning.*The following paths are not up to date" err &&
+ test_grep "warning.*The following paths are unmerged" err &&
+
+ git -C tweak sparse-checkout set folder2 deep/deeper1 2>err &&
+ test_grep "warning.*The following paths are not up to date" err &&
+ test_grep "warning.*The following paths are unmerged" err &&
+
+ git -C tweak sparse-checkout reapply 2>err &&
+ test_grep "warning.*The following paths are not up to date" err &&
+ test_path_is_file tweak/deep/deeper2/a &&
+ test_grep "warning.*The following paths are unmerged" err &&
+ test_path_is_file tweak/folder1/a &&
+
+ git -C tweak checkout HEAD deep/deeper2/a &&
+ git -C tweak sparse-checkout reapply 2>err &&
+ test_grep ! "warning.*The following paths are not up to date" err &&
+ test_path_is_missing tweak/deep/deeper2/a &&
+ test_grep "warning.*The following paths are unmerged" err &&
+ test_path_is_file tweak/folder1/a &&
+
+ # NEEDSWORK: We are asking to update a file outside of the
+ # sparse-checkout cone, but this is no longer allowed.
+ git -C tweak add folder1/a &&
+ git -C tweak sparse-checkout reapply 2>err &&
+ test_must_be_empty err &&
+ test_path_is_missing tweak/deep/deeper2/a &&
+ test_path_is_missing tweak/folder1/a &&
+
+ git -C tweak sparse-checkout disable
+'
+
+test_expect_success 'reapply can handle config options' '
+ git -C repo sparse-checkout init --cone --no-sparse-index &&
+ git -C repo config --worktree --list >actual &&
+ cat >expect <<-\EOF &&
+ core.sparsecheckout=true
+ core.sparsecheckoutcone=true
+ index.sparse=false
+ EOF
+ test_cmp expect actual &&
+
+ git -C repo sparse-checkout reapply --no-cone --no-sparse-index &&
+ git -C repo config --worktree --list >actual &&
+ cat >expect <<-\EOF &&
+ core.sparsecheckout=true
+ core.sparsecheckoutcone=false
+ index.sparse=false
+ EOF
+ test_cmp expect actual &&
+
+ git -C repo sparse-checkout reapply --cone --sparse-index &&
+ git -C repo config --worktree --list >actual &&
+ cat >expect <<-\EOF &&
+ core.sparsecheckout=true
+ core.sparsecheckoutcone=true
+ index.sparse=true
+ EOF
+ test_cmp expect actual &&
+
+ git -C repo sparse-checkout disable
+'
+
+test_expect_success 'cone mode: set with core.ignoreCase=true' '
+ rm repo/.git/info/sparse-checkout &&
+ git -C repo sparse-checkout init --cone &&
+ git -C repo -c core.ignoreCase=true sparse-checkout set folder1 &&
+ cat >expect <<-\EOF &&
+ /*
+ !/*/
+ /folder1/
+ EOF
+ test_cmp expect repo/.git/info/sparse-checkout &&
+ check_files repo a folder1
+'
+
+test_expect_success 'setup submodules' '
+ git clone repo super &&
+ (
+ cd super &&
+ mkdir modules &&
+ git -c protocol.file.allow=always \
+ submodule add ../repo modules/child &&
+ git add . &&
+ git commit -m "add submodule" &&
+ git sparse-checkout init --cone &&
+ git sparse-checkout set folder1
+ )
+'
+
+test_expect_success 'interaction with submodules' '
+ check_files super a folder1 modules &&
+ check_files super/modules/child a deep folder1 folder2
+'
+
+test_expect_success 'check-rules interaction with submodules' '
+ git -C super ls-tree --name-only -r HEAD >all-files &&
+ git -C super sparse-checkout check-rules >check-rules-matches <all-files &&
+
+ test_grep ! "modules/" check-rules-matches &&
+ test_grep "folder1/" check-rules-matches
+'
+
+test_expect_success 'different sparse-checkouts with worktrees' '
+ git -C repo sparse-checkout set --cone deep folder1 &&
+ git -C repo worktree add --detach ../worktree &&
+ check_files worktree "a deep folder1" &&
+ git -C repo sparse-checkout set --cone folder1 &&
+ git -C worktree sparse-checkout set --cone deep/deeper1 &&
+ check_files repo "a folder1" &&
+ check_files worktree "a deep"
+'
+
+test_expect_success 'set using filename keeps file on-disk' '
+ git -C repo sparse-checkout set --skip-checks a deep &&
+ cat >expect <<-\EOF &&
+ /*
+ !/*/
+ /a/
+ /deep/
+ EOF
+ test_cmp expect repo/.git/info/sparse-checkout &&
+ check_files repo a deep
+'
+
+check_read_tree_errors () {
+ REPO=$1
+ FILES=$2
+ ERRORS=$3
+ git -C $REPO -c core.sparseCheckoutCone=false read-tree -mu HEAD 2>err &&
+ test_must_be_empty err &&
+ check_files $REPO "$FILES" &&
+ git -C $REPO read-tree -mu HEAD 2>err &&
+ if test -z "$ERRORS"
+ then
+ test_must_be_empty err
+ else
+ test_grep "$ERRORS" err
+ fi &&
+ check_files $REPO $FILES
+}
+
+test_expect_success 'pattern-checks: /A/**' '
+ cat >repo/.git/info/sparse-checkout <<-\EOF &&
+ /*
+ !/*/
+ /folder1/**
+ EOF
+ check_read_tree_errors repo "a folder1" "disabling cone pattern matching"
+'
+
+test_expect_success 'pattern-checks: /A/**/B/' '
+ cat >repo/.git/info/sparse-checkout <<-\EOF &&
+ /*
+ !/*/
+ /deep/**/deepest
+ EOF
+ check_read_tree_errors repo "a deep" "disabling cone pattern matching" &&
+ check_files repo/deep "deeper1" &&
+ check_files repo/deep/deeper1 "deepest"
+'
+
+test_expect_success 'pattern-checks: too short' '
+ cat >repo/.git/info/sparse-checkout <<-\EOF &&
+ /*
+ !/*/
+ /
+ EOF
+ check_read_tree_errors repo "a" "disabling cone pattern matching"
+'
+test_expect_success 'pattern-checks: not too short' '
+ cat >repo/.git/info/sparse-checkout <<-\EOF &&
+ /*
+ !/*/
+ /b/
+ EOF
+ git -C repo read-tree -mu HEAD 2>err &&
+ test_must_be_empty err &&
+ check_files repo a
+'
+
+test_expect_success 'pattern-checks: trailing "*"' '
+ cat >repo/.git/info/sparse-checkout <<-\EOF &&
+ /*
+ !/*/
+ /a*
+ EOF
+ check_read_tree_errors repo "a" "disabling cone pattern matching"
+'
+
+test_expect_success 'pattern-checks: starting "*"' '
+ cat >repo/.git/info/sparse-checkout <<-\EOF &&
+ /*
+ !/*/
+ *eep/
+ EOF
+ check_read_tree_errors repo "a deep" "disabling cone pattern matching"
+'
+
+test_expect_success 'pattern-checks: non directory pattern' '
+ cat >repo/.git/info/sparse-checkout <<-\EOF &&
+ /deep/deeper1/a
+ EOF
+ check_read_tree_errors repo deep "disabling cone pattern matching" &&
+ check_files repo/deep deeper1 &&
+ check_files repo/deep/deeper1 a
+'
+
+test_expect_success 'pattern-checks: contained glob characters' '
+ for c in "[a]" "\\" "?" "*"
+ do
+ cat >repo/.git/info/sparse-checkout <<-EOF &&
+ /*
+ !/*/
+ something$c-else/
+ EOF
+ check_read_tree_errors repo "a" "disabling cone pattern matching" || return 1
+ done
+'
+
+test_expect_success BSLASHPSPEC 'pattern-checks: escaped characters' '
+ git clone repo escaped &&
+ TREEOID=$(git -C escaped rev-parse HEAD:folder1) &&
+ NEWTREE=$(git -C escaped mktree <<-EOF
+ $(git -C escaped ls-tree HEAD)
+ 040000 tree $TREEOID zbad\\dir
+ 040000 tree $TREEOID zdoes*exist
+ 040000 tree $TREEOID zglob[!a]?
+ EOF
+ ) &&
+ COMMIT=$(git -C escaped commit-tree $NEWTREE -p HEAD) &&
+ git -C escaped reset --hard $COMMIT &&
+ check_files escaped "a deep folder1 folder2 zbad\\dir zdoes*exist" zglob[!a]? &&
+ git -C escaped sparse-checkout init --cone &&
+ git -C escaped sparse-checkout set --skip-checks zbad\\dir/bogus "zdoes*not*exist" "zdoes*exist" "zglob[!a]?" &&
+ cat >expect <<-\EOF &&
+ /*
+ !/*/
+ /zbad\\dir/
+ !/zbad\\dir/*/
+ /zbad\\dir/bogus/
+ /zdoes\*exist/
+ /zdoes\*not\*exist/
+ /zglob\[!a]\?/
+ EOF
+ test_cmp expect escaped/.git/info/sparse-checkout &&
+ check_read_tree_errors escaped "a zbad\\dir zdoes*exist zglob[!a]?" &&
+ git -C escaped ls-tree -d --name-only HEAD >list-expect &&
+ git -C escaped sparse-checkout set --stdin <list-expect &&
+ cat >expect <<-\EOF &&
+ /*
+ !/*/
+ /deep/
+ /folder1/
+ /folder2/
+ /zbad\\dir/
+ /zdoes\*exist/
+ /zglob\[!a]\?/
+ EOF
+ test_cmp expect escaped/.git/info/sparse-checkout &&
+ check_files escaped "a deep folder1 folder2 zbad\\dir zdoes*exist" zglob[!a]? &&
+ git -C escaped sparse-checkout list >list-actual &&
+ test_cmp list-expect list-actual
+'
+
+test_expect_success MINGW 'cone mode replaces backslashes with slashes' '
+ git -C repo sparse-checkout set deep\\deeper1 &&
+ cat >expect <<-\EOF &&
+ /*
+ !/*/
+ /deep/
+ !/deep/*/
+ /deep/deeper1/
+ EOF
+ test_cmp expect repo/.git/info/sparse-checkout &&
+ check_files repo a deep &&
+ check_files repo/deep a deeper1
+'
+
+test_expect_success 'cone mode clears ignored subdirectories' '
+ rm repo/.git/info/sparse-checkout &&
+
+ git -C repo sparse-checkout init --cone &&
+ git -C repo sparse-checkout set deep/deeper1 &&
+
+ cat >repo/.gitignore <<-\EOF &&
+ obj/
+ *.o
+ EOF
+
+ git -C repo add .gitignore &&
+ git -C repo commit -m ".gitignore" &&
+
+ mkdir -p repo/obj repo/folder1/obj repo/deep/deeper2/obj &&
+ for file in folder1/obj/a obj/a folder1/file.o folder1.o \
+ deep/deeper2/obj/a deep/deeper2/file.o file.o
+ do
+ echo ignored >repo/$file || return 1
+ done &&
+
+ git -C repo status --porcelain=v2 >out &&
+ test_must_be_empty out &&
+
+ git -C repo sparse-checkout reapply &&
+ test_path_is_missing repo/folder1 &&
+ test_path_is_missing repo/deep/deeper2 &&
+ test_path_is_dir repo/obj &&
+ test_path_is_file repo/file.o &&
+
+ git -C repo status --porcelain=v2 >out &&
+ test_must_be_empty out &&
+
+ git -C repo sparse-checkout set deep/deeper2 &&
+ test_path_is_missing repo/deep/deeper1 &&
+ test_path_is_dir repo/deep/deeper2 &&
+ test_path_is_dir repo/obj &&
+ test_path_is_file repo/file.o &&
+
+ >repo/deep/deeper2/ignored.o &&
+ >repo/deep/deeper2/untracked &&
+
+ # When an untracked file is in the way, all untracked files
+ # (even ignored files) are preserved.
+ git -C repo sparse-checkout set folder1 2>err &&
+ grep "contains untracked files" err &&
+ test_path_is_file repo/deep/deeper2/ignored.o &&
+ test_path_is_file repo/deep/deeper2/untracked &&
+
+ # The rest of the cone matches expectation
+ test_path_is_missing repo/deep/deeper1 &&
+ test_path_is_dir repo/obj &&
+ test_path_is_file repo/file.o &&
+
+ git -C repo status --porcelain=v2 >out &&
+ echo "? deep/deeper2/untracked" >expect &&
+ test_cmp expect out
+'
+
+test_expect_success 'malformed cone-mode patterns' '
+ git -C repo sparse-checkout init --cone &&
+ mkdir -p repo/foo/bar &&
+ touch repo/foo/bar/x repo/foo/y &&
+ cat >repo/.git/info/sparse-checkout <<-\EOF &&
+ /*
+ !/*/
+ /foo/
+ !/foo/*/
+ /foo/\*/
+ EOF
+
+ # Listing the patterns will notice the duplicate pattern and
+ # emit a warning. It will list the patterns directly instead
+ # of using the cone-mode translation to a set of directories.
+ git -C repo sparse-checkout list >actual 2>err &&
+ test_cmp repo/.git/info/sparse-checkout actual &&
+ grep "warning: your sparse-checkout file may have issues: pattern .* is repeated" err &&
+ grep "warning: disabling cone pattern matching" err
+'
+
+test_expect_success 'set from subdir pays attention to prefix' '
+ git -C repo sparse-checkout disable &&
+ git -C repo/deep sparse-checkout set --cone deeper2 ../folder1 &&
+
+ git -C repo sparse-checkout list >actual &&
+
+ cat >expect <<-\EOF &&
+ deep/deeper2
+ folder1
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'add from subdir pays attention to prefix' '
+ git -C repo sparse-checkout set --cone deep/deeper2 &&
+ git -C repo/deep sparse-checkout add deeper1/deepest ../folder1 &&
+
+ git -C repo sparse-checkout list >actual &&
+
+ cat >expect <<-\EOF &&
+ deep/deeper1/deepest
+ deep/deeper2
+ folder1
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'set from subdir in non-cone mode throws an error' '
+ git -C repo sparse-checkout disable &&
+ test_must_fail git -C repo/deep sparse-checkout set --no-cone deeper2 ../folder1 2>error &&
+
+ grep "run from the toplevel directory in non-cone mode" error
+'
+
+test_expect_success 'set from subdir in non-cone mode throws an error' '
+ git -C repo sparse-checkout set --no-cone deep/deeper2 &&
+ test_must_fail git -C repo/deep sparse-checkout add deeper1/deepest ../folder1 2>error &&
+
+ grep "run from the toplevel directory in non-cone mode" error
+'
+
+test_expect_success 'by default, cone mode will error out when passed files' '
+ git -C repo sparse-checkout reapply --cone &&
+ test_must_fail git -C repo sparse-checkout add .gitignore 2>error &&
+
+ grep ".gitignore.*is not a directory" error
+'
+
+test_expect_success 'by default, non-cone mode will warn on individual files' '
+ git -C repo sparse-checkout reapply --no-cone &&
+ git -C repo sparse-checkout add .gitignore 2>warning &&
+
+ grep "pass a leading slash before paths.*if you want a single file" warning
+'
+
+test_expect_success 'setup bare repo' '
+ git clone --bare "file://$(pwd)/repo" bare
+'
+test_expect_success 'list fails outside work tree' '
+ test_must_fail git -C bare sparse-checkout list 2>err &&
+ test_grep "this operation must be run in a work tree" err
+'
+
+test_expect_success 'add fails outside work tree' '
+ test_must_fail git -C bare sparse-checkout add deeper 2>err &&
+ test_grep "this operation must be run in a work tree" err
+'
+
+test_expect_success 'set fails outside work tree' '
+ test_must_fail git -C bare sparse-checkout set deeper 2>err &&
+ test_grep "this operation must be run in a work tree" err
+'
+
+test_expect_success 'init fails outside work tree' '
+ test_must_fail git -C bare sparse-checkout init 2>err &&
+ test_grep "this operation must be run in a work tree" err
+'
+
+test_expect_success 'reapply fails outside work tree' '
+ test_must_fail git -C bare sparse-checkout reapply 2>err &&
+ test_grep "this operation must be run in a work tree" err
+'
+
+test_expect_success 'disable fails outside work tree' '
+ test_must_fail git -C bare sparse-checkout disable 2>err &&
+ test_grep "this operation must be run in a work tree" err
+'
+
+test_expect_success 'setup clean' '
+ git -C repo clean -fdx
+'
+
+test_expect_success 'check-rules cone mode' '
+ cat >rules <<-\EOF &&
+ folder1
+ deep/deeper1/deepest
+ EOF
+
+ git -C bare ls-tree -r --name-only HEAD >all-files &&
+ git -C bare sparse-checkout check-rules --cone \
+ --rules-file ../rules >check-rules-file <all-files &&
+
+ git -C repo sparse-checkout set --cone --stdin <rules&&
+ git -C repo ls-files -t >out &&
+ sed -n "/^S /!s/^. //p" out >ls-files &&
+
+ git -C repo sparse-checkout check-rules >check-rules-default <all-files &&
+
+ test_grep "deep/deeper1/deepest/a" check-rules-file &&
+ test_grep ! "deep/deeper2" check-rules-file &&
+
+ test_cmp check-rules-file ls-files &&
+ test_cmp check-rules-file check-rules-default
+'
+
+test_expect_success 'check-rules non-cone mode' '
+ cat >rules <<-\EOF &&
+ deep/deeper1/deepest/a
+ EOF
+
+ git -C bare ls-tree -r --name-only HEAD >all-files &&
+ git -C bare sparse-checkout check-rules --no-cone --rules-file ../rules\
+ >check-rules-file <all-files &&
+
+ cat rules | git -C repo sparse-checkout set --no-cone --stdin &&
+ git -C repo ls-files -t >out &&
+ sed -n "/^S /!s/^. //p" out >ls-files &&
+
+ git -C repo sparse-checkout check-rules >check-rules-default <all-files &&
+
+ cat >expect <<-\EOF &&
+ deep/deeper1/deepest/a
+ EOF
+
+ test_cmp expect check-rules-file &&
+ test_cmp check-rules-file ls-files &&
+ test_cmp check-rules-file check-rules-default
+'
+
+test_expect_success 'check-rules cone mode is default' '
+ cat >rules <<-\EOF &&
+ folder1
+ EOF
+
+ cat >all-files <<-\EOF &&
+ toplevel
+ folder2/file
+ folder1/file
+ EOF
+
+ cat >expect <<-\EOF &&
+ toplevel
+ folder1/file
+ EOF
+
+ git -C repo sparse-checkout set --no-cone &&
+ git -C repo sparse-checkout check-rules \
+ --rules-file ../rules >actual <all-files &&
+
+ git -C bare sparse-checkout check-rules \
+ --rules-file ../rules >actual-bare <all-files &&
+
+ test_cmp expect actual &&
+ test_cmp expect actual-bare
+'
+
+test_expect_success 'check-rules quoting' '
+ cat >rules <<-EOF &&
+ "folder\" a"
+ EOF
+ cat >files <<-EOF &&
+ "folder\" a/file"
+ "folder\" b/file"
+ EOF
+ cat >expect <<-EOF &&
+ "folder\" a/file"
+ EOF
+ git sparse-checkout check-rules --cone \
+ --rules-file rules >actual <files &&
+
+ test_cmp expect actual
+'
+
+test_expect_success 'check-rules null termination' '
+ cat >rules <<-EOF &&
+ "folder\" a"
+ EOF
+
+ lf_to_nul >files <<-EOF &&
+ folder" a/a
+ folder" a/b
+ folder" b/fileQ
+ EOF
+
+ cat >expect <<-EOF &&
+ folder" a/aQfolder" a/bQ
+ EOF
+
+ git sparse-checkout check-rules --cone -z \
+ --rules-file rules >actual.nul <files &&
+ nul_to_q <actual.nul >actual &&
+ echo >>actual &&
+
+ test_cmp expect actual
+'
+
+
+test_done
diff --git a/t/t1092-sparse-checkout-compatibility.sh b/t/t1092-sparse-checkout-compatibility.sh
new file mode 100755
index 0000000..2f1ae5f
--- /dev/null
+++ b/t/t1092-sparse-checkout-compatibility.sh
@@ -0,0 +1,2334 @@
+#!/bin/sh
+
+test_description='compare full workdir to sparse workdir'
+
+GIT_TEST_SPLIT_INDEX=0
+GIT_TEST_SPARSE_INDEX=
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ git init initial-repo &&
+ (
+ GIT_TEST_SPARSE_INDEX=0 &&
+ cd initial-repo &&
+ echo a >a &&
+ echo "after deep" >e &&
+ echo "after folder1" >g &&
+ echo "after x" >z &&
+ mkdir folder1 folder2 deep before x &&
+ echo "before deep" >before/a &&
+ echo "before deep again" >before/b &&
+ mkdir deep/deeper1 deep/deeper2 deep/before deep/later &&
+ mkdir deep/deeper1/deepest &&
+ mkdir deep/deeper1/deepest2 &&
+ mkdir deep/deeper1/deepest3 &&
+ echo "after deeper1" >deep/e &&
+ echo "after deepest" >deep/deeper1/e &&
+ cp a folder1 &&
+ cp a folder2 &&
+ cp a x &&
+ cp a deep &&
+ cp a deep/before &&
+ cp a deep/deeper1 &&
+ cp a deep/deeper2 &&
+ cp a deep/later &&
+ cp a deep/deeper1/deepest &&
+ cp a deep/deeper1/deepest2 &&
+ cp a deep/deeper1/deepest3 &&
+ cp -r deep/deeper1/ deep/deeper2 &&
+ mkdir deep/deeper1/0 &&
+ mkdir deep/deeper1/0/0 &&
+ touch deep/deeper1/0/1 &&
+ touch deep/deeper1/0/0/0 &&
+ >folder1- &&
+ >folder1.x &&
+ >folder10 &&
+ cp -r deep/deeper1/0 folder1 &&
+ cp -r deep/deeper1/0 folder2 &&
+ echo >>folder1/0/0/0 &&
+ echo >>folder2/0/1 &&
+ git add . &&
+ git commit -m "initial commit" &&
+ git checkout -b base &&
+ for dir in folder1 folder2 deep
+ do
+ git checkout -b update-$dir base &&
+ echo "updated $dir" >$dir/a &&
+ git commit -a -m "update $dir" || return 1
+ done &&
+
+ git checkout -b rename-base base &&
+ cat >folder1/larger-content <<-\EOF &&
+ matching
+ lines
+ help
+ inexact
+ renames
+ EOF
+ cp folder1/larger-content folder2/ &&
+ cp folder1/larger-content deep/deeper1/ &&
+ git add . &&
+ git commit -m "add interesting rename content" &&
+
+ git checkout -b rename-out-to-out rename-base &&
+ mv folder1/a folder2/b &&
+ mv folder1/larger-content folder2/edited-content &&
+ echo >>folder2/edited-content &&
+ echo >>folder2/0/1 &&
+ echo stuff >>deep/deeper1/a &&
+ git add . &&
+ git commit -m "rename folder1/... to folder2/..." &&
+
+ git checkout -b rename-out-to-in rename-base &&
+ mv folder1/a deep/deeper1/b &&
+ echo more stuff >>deep/deeper1/a &&
+ rm folder2/0/1 &&
+ mkdir folder2/0/1 &&
+ echo >>folder2/0/1/1 &&
+ mv folder1/larger-content deep/deeper1/edited-content &&
+ echo >>deep/deeper1/edited-content &&
+ git add . &&
+ git commit -m "rename folder1/... to deep/deeper1/..." &&
+
+ git checkout -b rename-in-to-out rename-base &&
+ mv deep/deeper1/a folder1/b &&
+ echo >>folder2/0/1 &&
+ rm -rf folder1/0/0 &&
+ echo >>folder1/0/0 &&
+ mv deep/deeper1/larger-content folder1/edited-content &&
+ echo >>folder1/edited-content &&
+ git add . &&
+ git commit -m "rename deep/deeper1/... to folder1/..." &&
+
+ git checkout -b df-conflict-1 base &&
+ rm -rf folder1 &&
+ echo content >folder1 &&
+ git add . &&
+ git commit -m "dir to file" &&
+
+ git checkout -b df-conflict-2 base &&
+ rm -rf folder2 &&
+ echo content >folder2 &&
+ git add . &&
+ git commit -m "dir to file" &&
+
+ git checkout -b fd-conflict base &&
+ rm a &&
+ mkdir a &&
+ echo content >a/a &&
+ git add . &&
+ git commit -m "file to dir" &&
+
+ for side in left right
+ do
+ git checkout -b merge-$side base &&
+ echo $side >>deep/deeper2/a &&
+ echo $side >>folder1/a &&
+ echo $side >>folder2/a &&
+ git add . &&
+ git commit -m "$side" || return 1
+ done &&
+
+ git checkout -b deepest base &&
+ echo "updated deepest" >deep/deeper1/deepest/a &&
+ echo "updated deepest2" >deep/deeper1/deepest2/a &&
+ echo "updated deepest3" >deep/deeper1/deepest3/a &&
+ git commit -a -m "update deepest" &&
+
+ git checkout -f base &&
+ git reset --hard
+ )
+'
+
+init_repos () {
+ rm -rf full-checkout sparse-checkout sparse-index &&
+
+ # create repos in initial state
+ cp -r initial-repo full-checkout &&
+ git -C full-checkout reset --hard &&
+
+ cp -r initial-repo sparse-checkout &&
+ git -C sparse-checkout reset --hard &&
+
+ cp -r initial-repo sparse-index &&
+ git -C sparse-index reset --hard &&
+
+ # initialize sparse-checkout definitions
+ git -C sparse-checkout sparse-checkout init --cone &&
+ git -C sparse-checkout sparse-checkout set deep &&
+ git -C sparse-index sparse-checkout init --cone --sparse-index &&
+ test_cmp_config -C sparse-index true index.sparse &&
+ git -C sparse-index sparse-checkout set deep
+}
+
+init_repos_as_submodules () {
+ git reset --hard &&
+ init_repos &&
+ git submodule add ./full-checkout &&
+ git submodule add ./sparse-checkout &&
+ git submodule add ./sparse-index &&
+
+ git submodule status >actual &&
+ grep full-checkout actual &&
+ grep sparse-checkout actual &&
+ grep sparse-index actual
+}
+
+run_on_sparse () {
+ (
+ cd sparse-checkout &&
+ GIT_PROGRESS_DELAY=100000 "$@" >../sparse-checkout-out 2>../sparse-checkout-err
+ ) &&
+ (
+ cd sparse-index &&
+ GIT_PROGRESS_DELAY=100000 "$@" >../sparse-index-out 2>../sparse-index-err
+ )
+}
+
+run_on_all () {
+ (
+ cd full-checkout &&
+ GIT_PROGRESS_DELAY=100000 "$@" >../full-checkout-out 2>../full-checkout-err
+ ) &&
+ run_on_sparse "$@"
+}
+
+test_all_match () {
+ run_on_all "$@" &&
+ test_cmp full-checkout-out sparse-checkout-out &&
+ test_cmp full-checkout-out sparse-index-out &&
+ test_cmp full-checkout-err sparse-checkout-err &&
+ test_cmp full-checkout-err sparse-index-err
+}
+
+test_sparse_match () {
+ run_on_sparse "$@" &&
+ test_cmp sparse-checkout-out sparse-index-out &&
+ test_cmp sparse-checkout-err sparse-index-err
+}
+
+test_sparse_unstaged () {
+ file=$1 &&
+ for repo in sparse-checkout sparse-index
+ do
+ # Skip "unmerged" paths
+ git -C $repo diff --staged --diff-filter=u -- "$file" >diff &&
+ test_must_be_empty diff || return 1
+ done
+}
+
+# Usage: test_sprase_checkout_set "<c1> ... <cN>" "<s1> ... <sM>"
+# Verifies that "git sparse-checkout set <c1> ... <cN>" succeeds and
+# leaves the sparse index in a state where <s1> ... <sM> are sparse
+# directories (and <c1> ... <cN> are not).
+test_sparse_checkout_set () {
+ CONE_DIRS=$1 &&
+ SPARSE_DIRS=$2 &&
+ git -C sparse-index sparse-checkout set --skip-checks $CONE_DIRS &&
+ git -C sparse-index ls-files --sparse --stage >cache &&
+
+ # Check that the directories outside of the sparse-checkout cone
+ # have sparse directory entries.
+ for dir in $SPARSE_DIRS
+ do
+ TREE=$(git -C sparse-index rev-parse HEAD:$dir) &&
+ grep "040000 $TREE 0 $dir/" cache \
+ || return 1
+ done &&
+
+ # Check that the directories in the sparse-checkout cone
+ # are not sparse directory entries.
+ for dir in $CONE_DIRS
+ do
+ # Allow TREE to not exist because
+ # $dir does not exist at HEAD.
+ TREE=$(git -C sparse-index rev-parse HEAD:$dir) ||
+ ! grep "040000 $TREE 0 $dir/" cache \
+ || return 1
+ done
+}
+
+test_expect_success 'sparse-index contents' '
+ init_repos &&
+
+ # Remove deep, add three other directories.
+ test_sparse_checkout_set \
+ "folder1 folder2 x" \
+ "before deep" &&
+
+ # Remove folder1, add deep
+ test_sparse_checkout_set \
+ "deep folder2 x" \
+ "before folder1" &&
+
+ # Replace deep with deep/deeper2 (dropping deep/deeper1)
+ # Add folder1
+ test_sparse_checkout_set \
+ "deep/deeper2 folder1 folder2 x" \
+ "before deep/deeper1" &&
+
+ # Replace deep/deeper2 with deep/deeper1
+ # Replace folder1 with folder1/0/0
+ # Replace folder2 with non-existent folder2/2/3
+ # Add non-existent "bogus"
+ test_sparse_checkout_set \
+ "bogus deep/deeper1 folder1/0/0 folder2/2/3 x" \
+ "before deep/deeper2 folder2/0" &&
+
+ # Drop down to only files at root
+ test_sparse_checkout_set \
+ "" \
+ "before deep folder1 folder2 x" &&
+
+ # Disabling the sparse-index replaces tree entries with full ones
+ git -C sparse-index sparse-checkout init --no-sparse-index &&
+ test_sparse_match git ls-files --stage --sparse
+'
+
+test_expect_success 'expanded in-memory index matches full index' '
+ init_repos &&
+ test_sparse_match git ls-files --stage
+'
+
+test_expect_success 'root directory cannot be sparse' '
+ init_repos &&
+
+ # Remove all in-cone files and directories from the index, collapse index
+ # with `git sparse-checkout reapply`
+ git -C sparse-index rm -r . &&
+ git -C sparse-index sparse-checkout reapply &&
+
+ # Verify sparse directories still present, root directory is not sparse
+ cat >expect <<-EOF &&
+ before/
+ folder1/
+ folder2/
+ x/
+ EOF
+ git -C sparse-index ls-files --sparse >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'status with options' '
+ init_repos &&
+ test_sparse_match ls &&
+ test_all_match git status --porcelain=v2 &&
+ test_all_match git status --porcelain=v2 -z -u &&
+ test_all_match git status --porcelain=v2 -uno &&
+ run_on_all touch README.md &&
+ test_all_match git status --porcelain=v2 &&
+ test_all_match git status --porcelain=v2 -z -u &&
+ test_all_match git status --porcelain=v2 -uno &&
+ test_all_match git add README.md &&
+ test_all_match git status --porcelain=v2 &&
+ test_all_match git status --porcelain=v2 -z -u &&
+ test_all_match git status --porcelain=v2 -uno
+'
+
+test_expect_success 'status with diff in unexpanded sparse directory' '
+ init_repos &&
+ test_all_match git checkout rename-base &&
+ test_all_match git reset --soft rename-out-to-out &&
+ test_all_match git status --porcelain=v2
+'
+
+test_expect_success 'status reports sparse-checkout' '
+ init_repos &&
+ git -C sparse-checkout status >full &&
+ git -C sparse-index status >sparse &&
+ test_grep "You are in a sparse checkout with " full &&
+ test_grep "You are in a sparse checkout." sparse
+'
+
+test_expect_success 'add, commit, checkout' '
+ init_repos &&
+
+ write_script edit-contents <<-\EOF &&
+ echo text >>$1
+ EOF
+ run_on_all ../edit-contents README.md &&
+
+ test_all_match git add README.md &&
+ test_all_match git status --porcelain=v2 &&
+ test_all_match git commit -m "Add README.md" &&
+
+ test_all_match git checkout HEAD~1 &&
+ test_all_match git checkout - &&
+
+ run_on_all ../edit-contents README.md &&
+
+ test_all_match git add -A &&
+ test_all_match git status --porcelain=v2 &&
+ test_all_match git commit -m "Extend README.md" &&
+
+ test_all_match git checkout HEAD~1 &&
+ test_all_match git checkout - &&
+
+ run_on_all ../edit-contents deep/newfile &&
+
+ test_all_match git status --porcelain=v2 -uno &&
+ test_all_match git status --porcelain=v2 &&
+ test_all_match git add . &&
+ test_all_match git status --porcelain=v2 &&
+ test_all_match git commit -m "add deep/newfile" &&
+
+ test_all_match git checkout HEAD~1 &&
+ test_all_match git checkout -
+'
+
+test_expect_success 'deep changes during checkout' '
+ init_repos &&
+
+ test_sparse_match git sparse-checkout set deep/deeper1/deepest &&
+ test_all_match git checkout deepest &&
+ test_all_match git checkout base
+'
+
+test_expect_success 'checkout with modified sparse directory' '
+ init_repos &&
+
+ test_all_match git checkout rename-in-to-out -- . &&
+ test_sparse_match git sparse-checkout reapply &&
+ test_all_match git checkout base
+'
+
+test_expect_success 'checkout orphan then non-orphan' '
+ init_repos &&
+
+ test_all_match git checkout --orphan test-orphan &&
+ test_all_match git status --porcelain=v2 &&
+ test_all_match git checkout base &&
+ test_all_match git status --porcelain=v2
+'
+
+test_expect_success 'add outside sparse cone' '
+ init_repos &&
+
+ run_on_sparse mkdir folder1 &&
+ run_on_sparse ../edit-contents folder1/a &&
+ run_on_sparse ../edit-contents folder1/newfile &&
+ test_sparse_match test_must_fail git add folder1/a &&
+ grep "Disable or modify the sparsity rules" sparse-checkout-err &&
+ test_sparse_unstaged folder1/a &&
+ test_sparse_match test_must_fail git add folder1/newfile &&
+ grep "Disable or modify the sparsity rules" sparse-checkout-err &&
+ test_sparse_unstaged folder1/newfile
+'
+
+test_expect_success 'commit including unstaged changes' '
+ init_repos &&
+
+ write_script edit-file <<-\EOF &&
+ echo $1 >$2
+ EOF
+
+ run_on_all ../edit-file 1 a &&
+ run_on_all ../edit-file 1 deep/a &&
+
+ test_all_match git commit -m "-a" -a &&
+ test_all_match git status --porcelain=v2 &&
+
+ run_on_all ../edit-file 2 a &&
+ run_on_all ../edit-file 2 deep/a &&
+
+ test_all_match git commit -m "--include" --include deep/a &&
+ test_all_match git status --porcelain=v2 &&
+ test_all_match git commit -m "--include" --include a &&
+ test_all_match git status --porcelain=v2 &&
+
+ run_on_all ../edit-file 3 a &&
+ run_on_all ../edit-file 3 deep/a &&
+
+ test_all_match git commit -m "--amend" -a --amend &&
+ test_all_match git status --porcelain=v2
+'
+
+test_expect_success 'status/add: outside sparse cone' '
+ init_repos &&
+
+ # folder1 is at HEAD, but outside the sparse cone
+ run_on_sparse mkdir folder1 &&
+ cp initial-repo/folder1/a sparse-checkout/folder1/a &&
+ cp initial-repo/folder1/a sparse-index/folder1/a &&
+
+ test_sparse_match git status &&
+
+ write_script edit-contents <<-\EOF &&
+ echo text >>$1
+ EOF
+ run_on_all ../edit-contents folder1/a &&
+ run_on_all ../edit-contents folder1/new &&
+
+ test_sparse_match git status --porcelain=v2 &&
+
+ # Adding the path outside of the sparse-checkout cone should fail.
+ test_sparse_match test_must_fail git add folder1/a &&
+ grep "Disable or modify the sparsity rules" sparse-checkout-err &&
+ test_sparse_unstaged folder1/a &&
+ test_all_match git add --refresh folder1/a &&
+ test_must_be_empty sparse-checkout-err &&
+ test_sparse_unstaged folder1/a &&
+ test_sparse_match test_must_fail git add folder1/new &&
+ grep "Disable or modify the sparsity rules" sparse-checkout-err &&
+ test_sparse_unstaged folder1/new &&
+ test_sparse_match git add --sparse folder1/a &&
+ test_sparse_match git add --sparse folder1/new &&
+
+ test_all_match git add --sparse . &&
+ test_all_match git status --porcelain=v2 &&
+ test_all_match git commit -m folder1/new &&
+ test_all_match git rev-parse HEAD^{tree} &&
+
+ run_on_all ../edit-contents folder1/newer &&
+ test_all_match git add --sparse folder1/ &&
+ test_all_match git status --porcelain=v2 &&
+ test_all_match git commit -m folder1/newer &&
+ test_all_match git rev-parse HEAD^{tree}
+'
+
+test_expect_success 'checkout and reset --hard' '
+ init_repos &&
+
+ test_all_match git checkout update-folder1 &&
+ test_all_match git status --porcelain=v2 &&
+
+ test_all_match git checkout update-deep &&
+ test_all_match git status --porcelain=v2 &&
+
+ test_all_match git checkout -b reset-test &&
+ test_all_match git reset --hard deepest &&
+ test_all_match git reset --hard update-folder1 &&
+ test_all_match git reset --hard update-folder2
+'
+
+test_expect_success 'diff --cached' '
+ init_repos &&
+
+ write_script edit-contents <<-\EOF &&
+ echo text >>README.md
+ EOF
+ run_on_all ../edit-contents &&
+
+ test_all_match git diff &&
+ test_all_match git diff --cached &&
+ test_all_match git add README.md &&
+ test_all_match git diff &&
+ test_all_match git diff --cached
+'
+
+# NEEDSWORK: sparse-checkout behaves differently from full-checkout when
+# running this test with 'df-conflict-2' after 'df-conflict-1'.
+test_expect_success 'diff with renames and conflicts' '
+ init_repos &&
+
+ for branch in rename-out-to-out \
+ rename-out-to-in \
+ rename-in-to-out \
+ df-conflict-1 \
+ fd-conflict
+ do
+ test_all_match git checkout rename-base &&
+ test_all_match git checkout $branch -- . &&
+ test_all_match git status --porcelain=v2 &&
+ test_all_match git diff --cached --no-renames &&
+ test_all_match git diff --cached --find-renames || return 1
+ done
+'
+
+test_expect_success 'diff with directory/file conflicts' '
+ init_repos &&
+
+ for branch in rename-out-to-out \
+ rename-out-to-in \
+ rename-in-to-out \
+ df-conflict-1 \
+ df-conflict-2 \
+ fd-conflict
+ do
+ git -C full-checkout reset --hard &&
+ test_sparse_match git reset --hard &&
+ test_all_match git checkout $branch &&
+ test_all_match git checkout rename-base -- . &&
+ test_all_match git status --porcelain=v2 &&
+ test_all_match git diff --cached --no-renames &&
+ test_all_match git diff --cached --find-renames || return 1
+ done
+'
+
+test_expect_success 'log with pathspec outside sparse definition' '
+ init_repos &&
+
+ test_all_match git log -- a &&
+ test_all_match git log -- folder1/a &&
+ test_all_match git log -- folder2/a &&
+ test_all_match git log -- deep/a &&
+ test_all_match git log -- deep/deeper1/a &&
+ test_all_match git log -- deep/deeper1/deepest/a &&
+
+ test_all_match git checkout update-folder1 &&
+ test_all_match git log -- folder1/a
+'
+
+test_expect_success 'blame with pathspec inside sparse definition' '
+ init_repos &&
+
+ for file in a \
+ deep/a \
+ deep/deeper1/a \
+ deep/deeper1/deepest/a
+ do
+ test_all_match git blame $file || return 1
+ done
+'
+
+# Without a revision specified, blame will error if passed any file that
+# is not present in the working directory (even if the file is tracked).
+# Here we just verify that this is also true with sparse checkouts.
+test_expect_success 'blame with pathspec outside sparse definition' '
+ init_repos &&
+ test_sparse_match git sparse-checkout set &&
+
+ for file in \
+ deep/a \
+ deep/deeper1/a \
+ deep/deeper1/deepest/a
+ do
+ test_sparse_match test_must_fail git blame $file &&
+ cat >expect <<-EOF &&
+ fatal: Cannot lstat '"'"'$file'"'"': No such file or directory
+ EOF
+ # We compare sparse-checkout-err and sparse-index-err in
+ # `test_sparse_match`. Given we know they are the same, we
+ # only check the content of sparse-index-err here.
+ test_cmp expect sparse-index-err || return 1
+ done
+'
+
+test_expect_success 'checkout and reset (mixed)' '
+ init_repos &&
+
+ test_all_match git checkout -b reset-test update-deep &&
+ test_all_match git reset deepest &&
+
+ # Because skip-worktree is preserved, resetting to update-folder1
+ # will show worktree changes for folder1/a in full-checkout, but not
+ # in sparse-checkout or sparse-index.
+ git -C full-checkout reset update-folder1 >full-checkout-out &&
+ test_sparse_match git reset update-folder1 &&
+ grep "M folder1/a" full-checkout-out &&
+ ! grep "M folder1/a" sparse-checkout-out &&
+ run_on_sparse test_path_is_missing folder1
+'
+
+test_expect_success 'checkout and reset (merge)' '
+ init_repos &&
+
+ write_script edit-contents <<-\EOF &&
+ echo text >>$1
+ EOF
+
+ test_all_match git checkout -b reset-test update-deep &&
+ run_on_all ../edit-contents a &&
+ test_all_match git reset --merge deepest &&
+ test_all_match git status --porcelain=v2 &&
+
+ test_all_match git reset --hard update-deep &&
+ run_on_all ../edit-contents deep/a &&
+ test_all_match test_must_fail git reset --merge deepest
+'
+
+test_expect_success 'checkout and reset (keep)' '
+ init_repos &&
+
+ write_script edit-contents <<-\EOF &&
+ echo text >>$1
+ EOF
+
+ test_all_match git checkout -b reset-test update-deep &&
+ run_on_all ../edit-contents a &&
+ test_all_match git reset --keep deepest &&
+ test_all_match git status --porcelain=v2 &&
+
+ test_all_match git reset --hard update-deep &&
+ run_on_all ../edit-contents deep/a &&
+ test_all_match test_must_fail git reset --keep deepest
+'
+
+test_expect_success 'reset with pathspecs inside sparse definition' '
+ init_repos &&
+
+ write_script edit-contents <<-\EOF &&
+ echo text >>$1
+ EOF
+
+ test_all_match git checkout -b reset-test update-deep &&
+ run_on_all ../edit-contents deep/a &&
+
+ test_all_match git reset base -- deep/a &&
+ test_all_match git status --porcelain=v2 &&
+
+ test_all_match git reset base -- nonexistent-file &&
+ test_all_match git status --porcelain=v2 &&
+
+ test_all_match git reset deepest -- deep &&
+ test_all_match git status --porcelain=v2
+'
+
+# Although the working tree differs between full and sparse checkouts after
+# reset, the state of the index is the same.
+test_expect_success 'reset with pathspecs outside sparse definition' '
+ init_repos &&
+ test_all_match git checkout -b reset-test base &&
+
+ test_sparse_match git reset update-folder1 -- folder1 &&
+ git -C full-checkout reset update-folder1 -- folder1 &&
+ test_all_match git ls-files -s -- folder1 &&
+
+ test_sparse_match git reset update-folder2 -- folder2/a &&
+ git -C full-checkout reset update-folder2 -- folder2/a &&
+ test_all_match git ls-files -s -- folder2/a
+'
+
+test_expect_success 'reset with wildcard pathspec' '
+ init_repos &&
+
+ test_all_match git reset update-deep -- deep\* &&
+ test_all_match git ls-files -s -- deep &&
+
+ test_all_match git reset deepest -- deep\*\*\* &&
+ test_all_match git ls-files -s -- deep &&
+
+ # The following `git reset`s result in updating the index on files with
+ # `skip-worktree` enabled. To avoid failing due to discrepencies in reported
+ # "modified" files, `test_sparse_match` reset is performed separately from
+ # "full-checkout" reset, then the index contents of all repos are verified.
+
+ test_sparse_match git reset update-folder1 -- \*/a &&
+ git -C full-checkout reset update-folder1 -- \*/a &&
+ test_all_match git ls-files -s -- deep/a folder1/a &&
+
+ test_sparse_match git reset update-folder2 -- folder\* &&
+ git -C full-checkout reset update-folder2 -- folder\* &&
+ test_all_match git ls-files -s -- folder10 folder1 folder2 &&
+
+ test_sparse_match git reset base -- folder1/\* &&
+ git -C full-checkout reset base -- folder1/\* &&
+ test_all_match git ls-files -s -- folder1
+'
+
+test_expect_success 'reset hard with removed sparse dir' '
+ init_repos &&
+
+ run_on_all git rm -r --sparse folder1 &&
+ test_all_match git status --porcelain=v2 &&
+
+ test_all_match git reset --hard &&
+ test_all_match git status --porcelain=v2 &&
+
+ cat >expect <<-\EOF &&
+ folder1/
+ EOF
+
+ git -C sparse-index ls-files --sparse folder1 >out &&
+ test_cmp expect out
+'
+
+test_expect_success 'update-index modify outside sparse definition' '
+ init_repos &&
+
+ write_script edit-contents <<-\EOF &&
+ echo text >>$1
+ EOF
+
+ # Create & modify folder1/a
+ # Note that this setup is a manual way of reaching the erroneous
+ # condition in which a `skip-worktree` enabled, outside-of-cone file
+ # exists on disk. It is used here to ensure `update-index` is stable
+ # and behaves predictably if such a condition occurs.
+ run_on_sparse mkdir -p folder1 &&
+ run_on_sparse cp ../initial-repo/folder1/a folder1/a &&
+ run_on_all ../edit-contents folder1/a &&
+
+ # If file has skip-worktree enabled, but the file is present, it is
+ # treated the same as if skip-worktree is disabled
+ test_all_match git status --porcelain=v2 &&
+ test_all_match git update-index folder1/a &&
+ test_all_match git status --porcelain=v2 &&
+
+ # When skip-worktree is disabled (even on files outside sparse cone), file
+ # is updated in the index
+ test_sparse_match git update-index --no-skip-worktree folder1/a &&
+ test_all_match git status --porcelain=v2 &&
+ test_all_match git update-index folder1/a &&
+ test_all_match git status --porcelain=v2
+'
+
+test_expect_success 'update-index --add outside sparse definition' '
+ init_repos &&
+
+ write_script edit-contents <<-\EOF &&
+ echo text >>$1
+ EOF
+
+ # Create folder1, add new file
+ run_on_sparse mkdir -p folder1 &&
+ run_on_all ../edit-contents folder1/b &&
+
+ # The *untracked* out-of-cone file is added to the index because it does
+ # not have a `skip-worktree` bit to signal that it should be ignored
+ # (unlike in `git add`, which will fail due to the file being outside
+ # the sparse checkout definition).
+ test_all_match git update-index --add folder1/b &&
+ test_all_match git status --porcelain=v2
+'
+
+# NEEDSWORK: `--remove`, unlike the rest of `update-index`, does not ignore
+# `skip-worktree` entries by default and will remove them from the index.
+# The `--ignore-skip-worktree-entries` flag must be used in conjunction with
+# `--remove` to ignore the `skip-worktree` entries and prevent their removal
+# from the index.
+test_expect_success 'update-index --remove outside sparse definition' '
+ init_repos &&
+
+ # When --ignore-skip-worktree-entries is _not_ specified:
+ # out-of-cone, not-on-disk files are removed from the index
+ test_sparse_match git update-index --remove folder1/a &&
+ cat >expect <<-EOF &&
+ D folder1/a
+ EOF
+ test_sparse_match git diff --cached --name-status &&
+ test_cmp expect sparse-checkout-out &&
+
+ # Reset the state
+ test_all_match git reset --hard &&
+
+ # When --ignore-skip-worktree-entries is specified, out-of-cone
+ # (skip-worktree) files are ignored
+ test_sparse_match git update-index --remove --ignore-skip-worktree-entries folder1/a &&
+ test_sparse_match git diff --cached --name-status &&
+ test_must_be_empty sparse-checkout-out &&
+
+ # Reset the state
+ test_all_match git reset --hard &&
+
+ # --force-remove supercedes --ignore-skip-worktree-entries, removing
+ # a skip-worktree file from the index (and disk) when both are specified
+ # with --remove
+ test_sparse_match git update-index --force-remove --ignore-skip-worktree-entries folder1/a &&
+ cat >expect <<-EOF &&
+ D folder1/a
+ EOF
+ test_sparse_match git diff --cached --name-status &&
+ test_cmp expect sparse-checkout-out
+'
+
+test_expect_success 'update-index with directories' '
+ init_repos &&
+
+ # update-index will exit silently when provided with a directory name
+ # containing a trailing slash
+ test_all_match git update-index deep/ folder1/ &&
+ grep "Ignoring path deep/" sparse-checkout-err &&
+ grep "Ignoring path folder1/" sparse-checkout-err &&
+
+ # When update-index is given a directory name WITHOUT a trailing slash, it will
+ # behave in different ways depending on the status of the directory on disk:
+ # * if it exists, the command exits with an error ("add individual files instead")
+ # * if it does NOT exist (e.g., in a sparse-checkout), it is assumed to be a
+ # file and either triggers an error ("does not exist and --remove not passed")
+ # or is ignored completely (when using --remove)
+ test_all_match test_must_fail git update-index deep &&
+ run_on_all test_must_fail git update-index folder1 &&
+ test_must_fail git -C full-checkout update-index --remove folder1 &&
+ test_sparse_match git update-index --remove folder1 &&
+ test_all_match git status --porcelain=v2
+'
+
+test_expect_success 'update-index --again file outside sparse definition' '
+ init_repos &&
+
+ test_all_match git checkout -b test-reupdate &&
+
+ # Update HEAD without modifying the index to introduce a difference in
+ # folder1/a
+ test_sparse_match git reset --soft update-folder1 &&
+
+ # Because folder1/a differs in the index vs HEAD,
+ # `git update-index --no-skip-worktree --again` will effectively perform
+ # `git update-index --no-skip-worktree folder1/a` and remove the skip-worktree
+ # flag from folder1/a
+ test_sparse_match git update-index --no-skip-worktree --again &&
+ test_sparse_match git status --porcelain=v2 &&
+
+ cat >expect <<-EOF &&
+ D folder1/a
+ EOF
+ test_sparse_match git diff --name-status &&
+ test_cmp expect sparse-checkout-out
+'
+
+test_expect_success 'update-index --cacheinfo' '
+ init_repos &&
+
+ deep_a_oid=$(git -C full-checkout rev-parse update-deep:deep/a) &&
+ folder2_oid=$(git -C full-checkout rev-parse update-folder2:folder2) &&
+ folder1_a_oid=$(git -C full-checkout rev-parse update-folder1:folder1/a) &&
+
+ test_all_match git update-index --cacheinfo 100644 $deep_a_oid deep/a &&
+ test_all_match git status --porcelain=v2 &&
+
+ # Cannot add sparse directory, even in sparse index case
+ test_all_match test_must_fail git update-index --add --cacheinfo 040000 $folder2_oid folder2/ &&
+
+ # Sparse match only: the new outside-of-cone entry is added *without* skip-worktree,
+ # so `git status` reports it as "deleted" in the worktree
+ test_sparse_match git update-index --add --cacheinfo 100644 $folder1_a_oid folder1/a &&
+ test_sparse_match git status --porcelain=v2 &&
+ cat >expect <<-EOF &&
+ MD folder1/a
+ EOF
+ test_sparse_match git status --short -- folder1/a &&
+ test_cmp expect sparse-checkout-out &&
+
+ # To return folder1/a to "normal" for a sparse checkout (ignored &
+ # outside-of-cone), add the skip-worktree flag.
+ test_sparse_match git update-index --skip-worktree folder1/a &&
+ cat >expect <<-EOF &&
+ S folder1/a
+ EOF
+ test_sparse_match git ls-files -t -- folder1/a &&
+ test_cmp expect sparse-checkout-out
+'
+
+for MERGE_TREES in "base HEAD update-folder2" \
+ "update-folder1 update-folder2" \
+ "update-folder2"
+do
+ test_expect_success "'read-tree -mu $MERGE_TREES' with files outside sparse definition" '
+ init_repos &&
+
+ # Although the index matches, without --no-sparse-checkout, outside-of-
+ # definition files will not exist on disk for sparse checkouts
+ test_all_match git read-tree -mu $MERGE_TREES &&
+ test_all_match git status --porcelain=v2 &&
+ test_path_is_missing sparse-checkout/folder2 &&
+ test_path_is_missing sparse-index/folder2 &&
+
+ test_all_match git read-tree --reset -u HEAD &&
+ test_all_match git status --porcelain=v2 &&
+
+ test_all_match git read-tree -mu --no-sparse-checkout $MERGE_TREES &&
+ test_all_match git status --porcelain=v2 &&
+ test_cmp sparse-checkout/folder2/a sparse-index/folder2/a &&
+ test_cmp sparse-checkout/folder2/a full-checkout/folder2/a
+
+ '
+done
+
+test_expect_success 'read-tree --merge with edit/edit conflicts in sparse directories' '
+ init_repos &&
+
+ # Merge of multiple changes to same directory (but not same files) should
+ # succeed
+ test_all_match git read-tree -mu base rename-base update-folder1 &&
+ test_all_match git status --porcelain=v2 &&
+
+ test_all_match git reset --hard &&
+
+ test_all_match git read-tree -mu rename-base update-folder2 &&
+ test_all_match git status --porcelain=v2 &&
+
+ test_all_match git reset --hard &&
+
+ test_all_match test_must_fail git read-tree -mu base update-folder1 rename-out-to-in &&
+ test_all_match test_must_fail git read-tree -mu rename-out-to-in update-folder1
+'
+
+test_expect_success 'read-tree --prefix' '
+ init_repos &&
+
+ # If files differing between the index and target <commit-ish> exist
+ # inside the prefix, `read-tree --prefix` should fail
+ test_all_match test_must_fail git read-tree --prefix=deep/ deepest &&
+ test_all_match test_must_fail git read-tree --prefix=folder1/ update-folder1 &&
+
+ # If no differing index entries exist matching the prefix,
+ # `read-tree --prefix` updates the index successfully
+ test_all_match git rm -rf deep/deeper1/deepest/ &&
+ test_all_match git read-tree --prefix=deep/deeper1/deepest -u deepest &&
+ test_all_match git status --porcelain=v2 &&
+
+ run_on_all git rm -rf --sparse folder1/ &&
+ test_all_match git read-tree --prefix=folder1/ -u update-folder1 &&
+ test_all_match git status --porcelain=v2 &&
+
+ test_all_match git rm -rf --sparse folder2/0 &&
+ test_all_match git read-tree --prefix=folder2/0/ -u rename-out-to-out &&
+ test_all_match git status --porcelain=v2
+'
+
+test_expect_success 'read-tree --merge with directory-file conflicts' '
+ init_repos &&
+
+ test_all_match git checkout -b test-branch rename-base &&
+
+ # Although the index matches, without --no-sparse-checkout, outside-of-
+ # definition files will not exist on disk for sparse checkouts
+ test_sparse_match git read-tree -mu rename-out-to-out &&
+ test_sparse_match git status --porcelain=v2 &&
+ test_path_is_missing sparse-checkout/folder2 &&
+ test_path_is_missing sparse-index/folder2 &&
+
+ test_sparse_match git read-tree --reset -u HEAD &&
+ test_sparse_match git status --porcelain=v2 &&
+
+ test_sparse_match git read-tree -mu --no-sparse-checkout rename-out-to-out &&
+ test_sparse_match git status --porcelain=v2 &&
+ test_cmp sparse-checkout/folder2/0/1 sparse-index/folder2/0/1
+'
+
+test_expect_success 'merge, cherry-pick, and rebase' '
+ init_repos &&
+
+ for OPERATION in "merge -m merge" cherry-pick "rebase --apply" "rebase --merge"
+ do
+ test_all_match git checkout -B temp update-deep &&
+ test_all_match git $OPERATION update-folder1 &&
+ test_all_match git rev-parse HEAD^{tree} &&
+ test_all_match git $OPERATION update-folder2 &&
+ test_all_match git rev-parse HEAD^{tree} || return 1
+ done
+'
+
+test_expect_success 'merge with conflict outside cone' '
+ init_repos &&
+
+ test_all_match git checkout -b merge-tip merge-left &&
+ test_all_match git status --porcelain=v2 &&
+ test_all_match test_must_fail git merge -m merge merge-right &&
+ test_all_match git status --porcelain=v2 &&
+
+ # Resolve the conflict in different ways:
+ # 1. Revert to the base
+ test_all_match git checkout base -- deep/deeper2/a &&
+ test_all_match git status --porcelain=v2 &&
+
+ # 2. Add the file with conflict markers
+ test_sparse_match test_must_fail git add folder1/a &&
+ grep "Disable or modify the sparsity rules" sparse-checkout-err &&
+ test_sparse_unstaged folder1/a &&
+ test_all_match git add --sparse folder1/a &&
+ test_all_match git status --porcelain=v2 &&
+
+ # 3. Rename the file to another sparse filename and
+ # accept conflict markers as resolved content.
+ run_on_all mv folder2/a folder2/z &&
+ test_sparse_match test_must_fail git add folder2 &&
+ grep "Disable or modify the sparsity rules" sparse-checkout-err &&
+ test_sparse_unstaged folder2/z &&
+ test_all_match git add --sparse folder2 &&
+ test_all_match git status --porcelain=v2 &&
+
+ test_all_match git merge --continue &&
+ test_all_match git status --porcelain=v2 &&
+ test_all_match git rev-parse HEAD^{tree}
+'
+
+test_expect_success 'cherry-pick/rebase with conflict outside cone' '
+ init_repos &&
+
+ for OPERATION in cherry-pick rebase
+ do
+ test_all_match git checkout -B tip &&
+ test_all_match git reset --hard merge-left &&
+ test_all_match git status --porcelain=v2 &&
+ test_all_match test_must_fail git $OPERATION merge-right &&
+ test_all_match git status --porcelain=v2 &&
+
+ # Resolve the conflict in different ways:
+ # 1. Revert to the base
+ test_all_match git checkout base -- deep/deeper2/a &&
+ test_all_match git status --porcelain=v2 &&
+
+ # 2. Add the file with conflict markers
+ # NEEDSWORK: Even though the merge conflict removed the
+ # SKIP_WORKTREE bit from the index entry for folder1/a, we should
+ # warn that this is a problematic add.
+ test_sparse_match test_must_fail git add folder1/a &&
+ grep "Disable or modify the sparsity rules" sparse-checkout-err &&
+ test_sparse_unstaged folder1/a &&
+ test_all_match git add --sparse folder1/a &&
+ test_all_match git status --porcelain=v2 &&
+
+ # 3. Rename the file to another sparse filename and
+ # accept conflict markers as resolved content.
+ # NEEDSWORK: This mode now fails, because folder2/z is
+ # outside of the sparse-checkout cone and does not match an
+ # existing index entry with the SKIP_WORKTREE bit cleared.
+ run_on_all mv folder2/a folder2/z &&
+ test_sparse_match test_must_fail git add folder2 &&
+ grep "Disable or modify the sparsity rules" sparse-checkout-err &&
+ test_sparse_unstaged folder2/z &&
+ test_all_match git add --sparse folder2 &&
+ test_all_match git status --porcelain=v2 &&
+
+ test_all_match git $OPERATION --continue &&
+ test_all_match git status --porcelain=v2 &&
+ test_all_match git rev-parse HEAD^{tree} || return 1
+ done
+'
+
+test_expect_success 'merge with outside renames' '
+ init_repos &&
+
+ for type in out-to-out out-to-in in-to-out
+ do
+ test_all_match git reset --hard &&
+ test_all_match git checkout -f -b merge-$type update-deep &&
+ test_all_match git merge -m "$type" rename-$type &&
+ test_all_match git rev-parse HEAD^{tree} || return 1
+ done
+'
+
+# Sparse-index fails to convert the index in the
+# final 'git cherry-pick' command.
+test_expect_success 'cherry-pick with conflicts' '
+ init_repos &&
+
+ write_script edit-conflict <<-\EOF &&
+ echo $1 >conflict
+ EOF
+
+ test_all_match git checkout -b to-cherry-pick &&
+ run_on_all ../edit-conflict ABC &&
+ test_all_match git add conflict &&
+ test_all_match git commit -m "conflict to pick" &&
+
+ test_all_match git checkout -B base HEAD~1 &&
+ run_on_all ../edit-conflict DEF &&
+ test_all_match git add conflict &&
+ test_all_match git commit -m "conflict in base" &&
+
+ test_all_match test_must_fail git cherry-pick to-cherry-pick
+'
+
+test_expect_success 'stash' '
+ init_repos &&
+
+ write_script edit-contents <<-\EOF &&
+ echo text >>$1
+ EOF
+
+ # Stash a sparse directory (folder1)
+ test_all_match git checkout -b test-branch rename-base &&
+ test_all_match git reset --soft rename-out-to-out &&
+ test_all_match git stash &&
+ test_all_match git status --porcelain=v2 &&
+
+ # Apply the sparse directory stash without reinstating the index
+ test_all_match git stash apply -q &&
+ test_all_match git status --porcelain=v2 &&
+
+ # Reset to state where stash can be applied
+ test_sparse_match git sparse-checkout reapply &&
+ test_all_match git reset --hard rename-out-to-out &&
+
+ # Apply the sparse directory stash *with* reinstating the index
+ test_all_match git stash apply --index -q &&
+ test_all_match git status --porcelain=v2 &&
+
+ # Reset to state where we will get a conflict applying the stash
+ test_sparse_match git sparse-checkout reapply &&
+ test_all_match git reset --hard update-folder1 &&
+
+ # Apply the sparse directory stash with conflicts
+ test_all_match test_must_fail git stash apply --index -q &&
+ test_all_match test_must_fail git stash apply -q &&
+ test_all_match git status --porcelain=v2 &&
+
+ # Reset to base branch
+ test_sparse_match git sparse-checkout reapply &&
+ test_all_match git reset --hard base &&
+
+ # Stash & unstash an untracked file outside of the sparse checkout
+ # definition.
+ run_on_sparse mkdir -p folder1 &&
+ run_on_all ../edit-contents folder1/new &&
+ test_all_match git stash -u &&
+ test_all_match git status --porcelain=v2 &&
+
+ test_all_match git stash pop -q &&
+ test_all_match git status --porcelain=v2
+'
+
+test_expect_success 'checkout-index inside sparse definition' '
+ init_repos &&
+
+ run_on_all rm -f deep/a &&
+ test_all_match git checkout-index -- deep/a &&
+ test_all_match git status --porcelain=v2 &&
+
+ echo test >>new-a &&
+ run_on_all cp ../new-a a &&
+ test_all_match test_must_fail git checkout-index -- a &&
+ test_all_match git checkout-index -f -- a &&
+ test_all_match git status --porcelain=v2
+'
+
+test_expect_success 'checkout-index outside sparse definition' '
+ init_repos &&
+
+ # Without --ignore-skip-worktree-bits, outside-of-cone files will trigger
+ # an error
+ test_sparse_match test_must_fail git checkout-index -- folder1/a &&
+ test_grep "folder1/a has skip-worktree enabled" sparse-checkout-err &&
+ test_path_is_missing folder1/a &&
+
+ # With --ignore-skip-worktree-bits, outside-of-cone files are checked out
+ test_sparse_match git checkout-index --ignore-skip-worktree-bits -- folder1/a &&
+ test_cmp sparse-checkout/folder1/a sparse-index/folder1/a &&
+ test_cmp sparse-checkout/folder1/a full-checkout/folder1/a &&
+
+ run_on_sparse rm -rf folder1 &&
+ echo test >new-a &&
+ run_on_sparse mkdir -p folder1 &&
+ run_on_all cp ../new-a folder1/a &&
+
+ test_all_match test_must_fail git checkout-index --ignore-skip-worktree-bits -- folder1/a &&
+ test_all_match git checkout-index -f --ignore-skip-worktree-bits -- folder1/a &&
+ test_cmp sparse-checkout/folder1/a sparse-index/folder1/a &&
+ test_cmp sparse-checkout/folder1/a full-checkout/folder1/a
+'
+
+test_expect_success 'checkout-index with folders' '
+ init_repos &&
+
+ # Inside checkout definition
+ test_all_match test_must_fail git checkout-index -f -- deep/ &&
+
+ # Outside checkout definition
+ # Note: although all tests fail (as expected), the messaging differs. For
+ # non-sparse index checkouts, the error is that the "file" does not appear
+ # in the index; for sparse checkouts, the error is explicitly that the
+ # entry is a sparse directory.
+ run_on_all test_must_fail git checkout-index -f -- folder1/ &&
+ test_cmp full-checkout-err sparse-checkout-err &&
+ ! test_cmp full-checkout-err sparse-index-err &&
+ grep "is a sparse directory" sparse-index-err
+'
+
+test_expect_success 'checkout-index --all' '
+ init_repos &&
+
+ test_all_match git checkout-index --all &&
+ test_sparse_match test_path_is_missing folder1 &&
+
+ # --ignore-skip-worktree-bits will cause `skip-worktree` files to be
+ # checked out, causing the outside-of-cone `folder1` to exist on-disk
+ test_all_match git checkout-index --ignore-skip-worktree-bits --all &&
+ test_all_match test_path_exists folder1
+'
+
+test_expect_success 'clean' '
+ init_repos &&
+
+ echo bogus >>.gitignore &&
+ run_on_all cp ../.gitignore . &&
+ test_all_match git add .gitignore &&
+ test_all_match git commit -m "ignore bogus files" &&
+
+ run_on_sparse mkdir folder1 &&
+ run_on_all mkdir -p deep/untracked-deep &&
+ run_on_all touch folder1/bogus &&
+ run_on_all touch folder1/untracked &&
+ run_on_all touch deep/untracked-deep/bogus &&
+ run_on_all touch deep/untracked-deep/untracked &&
+
+ test_all_match git status --porcelain=v2 &&
+ test_all_match git clean -f &&
+ test_all_match git status --porcelain=v2 &&
+ test_sparse_match ls &&
+ test_sparse_match ls folder1 &&
+ run_on_all test_path_exists folder1/bogus &&
+ run_on_all test_path_is_missing folder1/untracked &&
+ run_on_all test_path_exists deep/untracked-deep/bogus &&
+ run_on_all test_path_exists deep/untracked-deep/untracked &&
+
+ test_all_match git clean -fd &&
+ test_all_match git status --porcelain=v2 &&
+ test_sparse_match ls &&
+ test_sparse_match ls folder1 &&
+ run_on_all test_path_exists folder1/bogus &&
+ run_on_all test_path_exists deep/untracked-deep/bogus &&
+ run_on_all test_path_is_missing deep/untracked-deep/untracked &&
+
+ test_all_match git clean -xf &&
+ test_all_match git status --porcelain=v2 &&
+ test_sparse_match ls &&
+ test_sparse_match ls folder1 &&
+ run_on_all test_path_is_missing folder1/bogus &&
+ run_on_all test_path_exists deep/untracked-deep/bogus &&
+
+ test_all_match git clean -xdf &&
+ test_all_match git status --porcelain=v2 &&
+ test_sparse_match ls &&
+ test_sparse_match ls folder1 &&
+ run_on_all test_path_is_missing deep/untracked-deep/bogus &&
+
+ test_sparse_match test_path_is_dir folder1
+'
+
+for builtin in show rev-parse
+do
+ test_expect_success "$builtin (cached blobs/trees)" "
+ init_repos &&
+
+ test_all_match git $builtin :a &&
+ test_all_match git $builtin :deep/a &&
+ test_sparse_match git $builtin :folder1/a &&
+
+ # The error message differs depending on whether
+ # the directory exists in the worktree.
+ test_all_match test_must_fail git $builtin :deep/ &&
+ test_must_fail git -C full-checkout $builtin :folder1/ &&
+ test_sparse_match test_must_fail git $builtin :folder1/ &&
+
+ # Change the sparse cone for an extra case:
+ run_on_sparse git sparse-checkout set deep/deeper1 &&
+
+ # deep/deeper2 is a sparse directory in the sparse index.
+ test_sparse_match test_must_fail git $builtin :deep/deeper2/ &&
+
+ # deep/deeper2/deepest is not in the sparse index, but
+ # will trigger an index expansion.
+ test_sparse_match test_must_fail git $builtin :deep/deeper2/deepest/
+ "
+done
+
+test_expect_success 'submodule handling' '
+ init_repos &&
+
+ test_sparse_match git sparse-checkout add modules &&
+ test_all_match mkdir modules &&
+ test_all_match touch modules/a &&
+ test_all_match git add modules &&
+ test_all_match git commit -m "add modules directory" &&
+
+ test_config_global protocol.file.allow always &&
+
+ run_on_all git submodule add "$(pwd)/initial-repo" modules/sub &&
+ test_all_match git commit -m "add submodule" &&
+
+ # having a submodule prevents "modules" from collapse
+ test_sparse_match git sparse-checkout set deep/deeper1 &&
+ git -C sparse-index ls-files --sparse --stage >cache &&
+ grep "100644 .* modules/a" cache &&
+ grep "160000 $(git -C initial-repo rev-parse HEAD) 0 modules/sub" cache
+'
+
+# When working with a sparse index, some commands will need to expand the
+# index to operate properly. If those commands also write the index back
+# to disk, they need to convert the index to sparse before writing.
+# This test verifies that both of these events are logged in trace2 logs.
+test_expect_success 'sparse-index is expanded and converted back' '
+ init_repos &&
+
+ GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
+ git -C sparse-index reset -- folder1/a &&
+ test_region index convert_to_sparse trace2.txt &&
+ test_region index ensure_full_index trace2.txt &&
+
+ # ls-files expands on read, but does not write.
+ rm trace2.txt &&
+ GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \
+ git -C sparse-index ls-files &&
+ test_region index ensure_full_index trace2.txt
+'
+
+test_expect_success 'index.sparse disabled inline uses full index' '
+ init_repos &&
+
+ # When index.sparse is disabled inline with `git status`, the
+ # index is expanded at the beginning of the execution then never
+ # converted back to sparse. It is then written to disk as a full index.
+ rm -f trace2.txt &&
+ GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \
+ git -C sparse-index -c index.sparse=false status &&
+ ! test_region index convert_to_sparse trace2.txt &&
+ test_region index ensure_full_index trace2.txt &&
+
+ # Since index.sparse is set to true at a repo level, the index
+ # is converted from full to sparse when read, then never expanded
+ # over the course of `git status`. It is written to disk as a sparse
+ # index.
+ rm -f trace2.txt &&
+ GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \
+ git -C sparse-index status &&
+ test_region index convert_to_sparse trace2.txt &&
+ ! test_region index ensure_full_index trace2.txt &&
+
+ # Now that the index has been written to disk as sparse, it is not
+ # converted to sparse (or expanded to full) when read by `git status`.
+ rm -f trace2.txt &&
+ GIT_TRACE2_EVENT="$(pwd)/trace2.txt" GIT_TRACE2_EVENT_NESTING=10 \
+ git -C sparse-index status &&
+ ! test_region index convert_to_sparse trace2.txt &&
+ ! test_region index ensure_full_index trace2.txt
+'
+
+run_sparse_index_trace2 () {
+ rm -f trace2.txt &&
+ if test -z "$WITHOUT_UNTRACKED_TXT"
+ then
+ echo >>sparse-index/untracked.txt
+ fi &&
+
+ if test "$1" = "!"
+ then
+ shift &&
+ test_must_fail env \
+ GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
+ git -C sparse-index "$@" \
+ >sparse-index-out \
+ 2>sparse-index-error || return 1
+ else
+ GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
+ git -C sparse-index "$@" \
+ >sparse-index-out \
+ 2>sparse-index-error || return 1
+ fi
+}
+
+ensure_expanded () {
+ run_sparse_index_trace2 "$@" &&
+ test_region index ensure_full_index trace2.txt
+}
+
+ensure_not_expanded () {
+ run_sparse_index_trace2 "$@" &&
+ test_region ! index ensure_full_index trace2.txt
+}
+
+test_expect_success 'sparse-index is not expanded' '
+ init_repos &&
+
+ ensure_not_expanded status &&
+ ensure_not_expanded ls-files --sparse &&
+ ensure_not_expanded commit --allow-empty -m empty &&
+ echo >>sparse-index/a &&
+ ensure_not_expanded commit -a -m a &&
+ echo >>sparse-index/a &&
+ ensure_not_expanded commit --include a -m a &&
+ echo >>sparse-index/deep/deeper1/a &&
+ ensure_not_expanded commit --include deep/deeper1/a -m deeper &&
+ ensure_not_expanded checkout rename-out-to-out &&
+ ensure_not_expanded checkout - &&
+ ensure_not_expanded switch rename-out-to-out &&
+ ensure_not_expanded switch - &&
+ ensure_not_expanded reset --hard &&
+ ensure_not_expanded checkout rename-out-to-out -- deep/deeper1 &&
+ ensure_not_expanded reset --hard &&
+ ensure_not_expanded restore -s rename-out-to-out -- deep/deeper1 &&
+
+ echo >>sparse-index/README.md &&
+ ensure_not_expanded add -A &&
+ echo >>sparse-index/extra.txt &&
+ ensure_not_expanded add extra.txt &&
+ echo >>sparse-index/untracked.txt &&
+ ensure_not_expanded add . &&
+
+ ensure_not_expanded checkout-index -f a &&
+ ensure_not_expanded checkout-index -f --all &&
+ for ref in update-deep update-folder1 update-folder2 update-deep
+ do
+ echo >>sparse-index/README.md &&
+ ensure_not_expanded reset --hard $ref || return 1
+ done &&
+
+ ensure_not_expanded reset --mixed base &&
+ ensure_not_expanded reset --hard update-deep &&
+ ensure_not_expanded reset --keep base &&
+ ensure_not_expanded reset --merge update-deep &&
+ ensure_not_expanded reset --hard &&
+
+ ensure_not_expanded reset base -- deep/a &&
+ ensure_not_expanded reset base -- nonexistent-file &&
+ ensure_not_expanded reset deepest -- deep &&
+
+ # Although folder1 is outside the sparse definition, it exists as a
+ # directory entry in the index, so the pathspec will not force the
+ # index to be expanded.
+ ensure_not_expanded reset deepest -- folder1 &&
+ ensure_not_expanded reset deepest -- folder1/ &&
+
+ # Wildcard identifies only in-cone files, no index expansion
+ ensure_not_expanded reset deepest -- deep/\* &&
+
+ # Wildcard identifies only full sparse directories, no index expansion
+ ensure_not_expanded reset deepest -- folder\* &&
+
+ ensure_not_expanded clean -fd &&
+
+ ensure_not_expanded checkout -f update-deep &&
+ test_config -C sparse-index pull.twohead ort &&
+ (
+ sane_unset GIT_TEST_MERGE_ALGORITHM &&
+ for OPERATION in "merge -m merge" cherry-pick rebase
+ do
+ ensure_not_expanded merge -m merge update-folder1 &&
+ ensure_not_expanded merge -m merge update-folder2 || return 1
+ done
+ )
+'
+
+test_expect_success 'sparse-index is not expanded: merge conflict in cone' '
+ init_repos &&
+
+ for side in right left
+ do
+ git -C sparse-index checkout -b expand-$side base &&
+ echo $side >sparse-index/deep/a &&
+ git -C sparse-index commit -a -m "$side" || return 1
+ done &&
+
+ (
+ sane_unset GIT_TEST_MERGE_ALGORITHM &&
+ git -C sparse-index config pull.twohead ort &&
+ ensure_not_expanded ! merge -m merged expand-right
+ )
+'
+
+test_expect_success 'sparse-index is not expanded: stash' '
+ init_repos &&
+
+ echo >>sparse-index/a &&
+ ensure_not_expanded stash &&
+ ensure_not_expanded stash list &&
+ ensure_not_expanded stash show stash@{0} &&
+ ensure_not_expanded stash apply stash@{0} &&
+ ensure_not_expanded stash drop stash@{0} &&
+
+ echo >>sparse-index/deep/new &&
+ ensure_not_expanded stash -u &&
+ (
+ WITHOUT_UNTRACKED_TXT=1 &&
+ ensure_not_expanded stash pop
+ ) &&
+
+ ensure_not_expanded stash create &&
+ oid=$(git -C sparse-index stash create) &&
+ ensure_not_expanded stash store -m "test" $oid &&
+ ensure_not_expanded reset --hard &&
+ ensure_not_expanded stash pop
+'
+
+test_expect_success 'describe tested on all' '
+ init_repos &&
+
+ # Add tag to be read by describe
+
+ run_on_all git tag -a v1.0 -m "Version 1" &&
+ test_all_match git describe --dirty &&
+ run_on_all rm g &&
+ test_all_match git describe --dirty
+'
+
+
+test_expect_success 'sparse-index is not expanded: describe' '
+ init_repos &&
+
+ # Add tag to be read by describe
+
+ git -C sparse-index tag -a v1.0 -m "Version 1" &&
+
+ ensure_not_expanded describe --dirty &&
+ echo "test" >>sparse-index/g &&
+ ensure_not_expanded describe --dirty &&
+ ensure_not_expanded describe
+'
+
+test_expect_success 'sparse index is not expanded: diff' '
+ init_repos &&
+
+ write_script edit-contents <<-\EOF &&
+ echo text >>$1
+ EOF
+
+ # Add file within cone
+ test_sparse_match git sparse-checkout set deep &&
+ run_on_all ../edit-contents deep/testfile &&
+ test_all_match git add deep/testfile &&
+ run_on_all ../edit-contents deep/testfile &&
+
+ test_all_match git diff &&
+ test_all_match git diff --cached &&
+ ensure_not_expanded diff &&
+ ensure_not_expanded diff --cached &&
+
+ # Add file outside cone
+ test_all_match git reset --hard &&
+ run_on_all mkdir newdirectory &&
+ run_on_all ../edit-contents newdirectory/testfile &&
+ test_sparse_match git sparse-checkout set newdirectory &&
+ test_all_match git add newdirectory/testfile &&
+ run_on_all ../edit-contents newdirectory/testfile &&
+ test_sparse_match git sparse-checkout set &&
+
+ test_all_match git diff &&
+ test_all_match git diff --cached &&
+ ensure_not_expanded diff &&
+ ensure_not_expanded diff --cached &&
+
+ # Merge conflict outside cone
+ # The sparse checkout will report a warning that is not in the
+ # full checkout, so we use `run_on_all` instead of
+ # `test_all_match`
+ run_on_all git reset --hard &&
+ test_all_match git checkout merge-left &&
+ test_all_match test_must_fail git merge merge-right &&
+
+ test_all_match git diff &&
+ test_all_match git diff --cached &&
+ ensure_not_expanded diff &&
+ ensure_not_expanded diff --cached
+'
+
+test_expect_success 'sparse index is not expanded: show and rev-parse' '
+ init_repos &&
+
+ ensure_not_expanded show :a &&
+ ensure_not_expanded show :deep/a &&
+ ensure_not_expanded rev-parse :a &&
+ ensure_not_expanded rev-parse :deep/a
+'
+
+test_expect_success 'sparse index is not expanded: update-index' '
+ init_repos &&
+
+ deep_a_oid=$(git -C full-checkout rev-parse update-deep:deep/a) &&
+ ensure_not_expanded update-index --cacheinfo 100644 $deep_a_oid deep/a &&
+
+ echo "test" >sparse-index/README.md &&
+ echo "test2" >sparse-index/a &&
+ rm -f sparse-index/deep/a &&
+
+ ensure_not_expanded update-index --add README.md &&
+ ensure_not_expanded update-index a &&
+ ensure_not_expanded update-index --remove deep/a &&
+
+ ensure_not_expanded reset --soft update-deep &&
+ ensure_not_expanded update-index --add --remove --again
+'
+
+test_expect_success 'sparse index is not expanded: blame' '
+ init_repos &&
+
+ for file in a \
+ deep/a \
+ deep/deeper1/a \
+ deep/deeper1/deepest/a
+ do
+ ensure_not_expanded blame $file || return 1
+ done
+'
+
+test_expect_success 'sparse index is not expanded: fetch/pull' '
+ init_repos &&
+
+ git -C sparse-index remote add full "file://$(pwd)/full-checkout" &&
+ ensure_not_expanded fetch full &&
+ git -C full-checkout commit --allow-empty -m "for pull merge" &&
+ git -C sparse-index commit --allow-empty -m "for pull merge" &&
+ ensure_not_expanded pull full base
+'
+
+test_expect_success 'sparse index is not expanded: read-tree' '
+ init_repos &&
+
+ ensure_not_expanded checkout -b test-branch update-folder1 &&
+ for MERGE_TREES in "base HEAD update-folder2" \
+ "base HEAD rename-base" \
+ "base update-folder2" \
+ "base rename-base" \
+ "update-folder2"
+ do
+ ensure_not_expanded read-tree -mu $MERGE_TREES &&
+ ensure_not_expanded reset --hard || return 1
+ done &&
+
+ rm -rf sparse-index/deep/deeper2 &&
+ ensure_not_expanded add . &&
+ ensure_not_expanded commit -m "test" &&
+
+ ensure_not_expanded read-tree --prefix=deep/deeper2 -u deepest
+'
+
+test_expect_success 'ls-files' '
+ init_repos &&
+
+ # Use a smaller sparse-checkout for reduced output
+ test_sparse_match git sparse-checkout set &&
+
+ # Behavior agrees by default. Sparse index is expanded.
+ test_all_match git ls-files &&
+
+ # With --sparse, the sparse index data changes behavior.
+ git -C sparse-index ls-files --sparse >actual &&
+
+ cat >expect <<-\EOF &&
+ a
+ before/
+ deep/
+ e
+ folder1-
+ folder1.x
+ folder1/
+ folder10
+ folder2/
+ g
+ x/
+ z
+ EOF
+
+ test_cmp expect actual &&
+
+ # With --sparse and no sparse index, nothing changes.
+ git -C sparse-checkout ls-files >dense &&
+ git -C sparse-checkout ls-files --sparse >sparse &&
+ test_cmp dense sparse &&
+
+ # Set up a strange condition of having a file edit
+ # outside of the sparse-checkout cone. We want to verify
+ # that all modes handle this the same, and detect the
+ # modification.
+ write_script edit-content <<-\EOF &&
+ mkdir -p folder1 &&
+ echo content >>folder1/a
+ EOF
+ run_on_all ../edit-content &&
+
+ test_all_match git ls-files --modified &&
+
+ git -C sparse-index ls-files --sparse --modified >sparse-index-out &&
+ cat >expect <<-\EOF &&
+ folder1/a
+ EOF
+ test_cmp expect sparse-index-out &&
+
+ # Add folder1 to the sparse-checkout cone and
+ # check that ls-files shows the expanded files.
+ test_sparse_match git sparse-checkout add folder1 &&
+ test_all_match git ls-files --modified &&
+
+ test_all_match git ls-files &&
+ git -C sparse-index ls-files --sparse >actual &&
+
+ cat >expect <<-\EOF &&
+ a
+ before/
+ deep/
+ e
+ folder1-
+ folder1.x
+ folder1/0/0/0
+ folder1/0/1
+ folder1/a
+ folder10
+ folder2/
+ g
+ x/
+ z
+ EOF
+
+ test_cmp expect actual &&
+
+ # Double-check index expansion is avoided
+ ensure_not_expanded ls-files --sparse
+'
+
+test_expect_success 'sparse index is not expanded: sparse-checkout' '
+ init_repos &&
+
+ ensure_not_expanded sparse-checkout set deep/deeper2 &&
+ ensure_not_expanded sparse-checkout set deep/deeper1 &&
+ ensure_not_expanded sparse-checkout set deep &&
+ ensure_not_expanded sparse-checkout add folder1 &&
+ ensure_not_expanded sparse-checkout set deep/deeper1 &&
+ ensure_not_expanded sparse-checkout set folder2 &&
+
+ # Demonstrate that the checks that "folder1/a" is a file
+ # do not cause a sparse-index expansion (since it is in the
+ # sparse-checkout cone).
+ echo >>sparse-index/folder2/a &&
+ git -C sparse-index add folder2/a &&
+
+ ensure_not_expanded sparse-checkout add folder1 &&
+
+ # Skip checks here, since deep/deeper1 is inside a sparse directory
+ # that must be expanded to check whether `deep/deeper1` is a file
+ # or not.
+ ensure_not_expanded sparse-checkout set --skip-checks deep/deeper1 &&
+ ensure_not_expanded sparse-checkout set
+'
+
+# NEEDSWORK: a sparse-checkout behaves differently from a full checkout
+# in this scenario, but it shouldn't.
+test_expect_success 'reset mixed and checkout orphan' '
+ init_repos &&
+
+ test_all_match git checkout rename-out-to-in &&
+
+ # Sparse checkouts do not agree with full checkouts about
+ # how to report a directory/file conflict during a reset.
+ # This command would fail with test_all_match because the
+ # full checkout reports "T folder1/0/1" while a sparse
+ # checkout reports "D folder1/0/1". This matches because
+ # the sparse checkouts skip "adding" the other side of
+ # the conflict.
+ test_sparse_match git reset --mixed HEAD~1 &&
+ test_sparse_match git ls-files --stage &&
+ test_sparse_match git status --porcelain=v2 &&
+
+ # At this point, sparse-checkouts behave differently
+ # from the full-checkout.
+ test_sparse_match git checkout --orphan new-branch &&
+ test_sparse_match git ls-files --stage &&
+ test_sparse_match git status --porcelain=v2
+'
+
+test_expect_success 'add everything with deep new file' '
+ init_repos &&
+
+ run_on_sparse git sparse-checkout set deep/deeper1/deepest &&
+
+ run_on_all touch deep/deeper1/x &&
+ test_all_match git add . &&
+ test_all_match git status --porcelain=v2
+'
+
+# NEEDSWORK: 'git checkout' behaves incorrectly in the case of
+# directory/file conflicts, even without sparse-checkout. Use this
+# test only as a documentation of the incorrect behavior, not a
+# measure of how it _should_ behave.
+test_expect_success 'checkout behaves oddly with df-conflict-1' '
+ init_repos &&
+
+ test_sparse_match git sparse-checkout disable &&
+
+ write_script edit-content <<-\EOF &&
+ echo content >>folder1/larger-content
+ git add folder1
+ EOF
+
+ run_on_all ../edit-content &&
+ test_all_match git status --porcelain=v2 &&
+
+ git -C sparse-checkout sparse-checkout init --cone &&
+ git -C sparse-index sparse-checkout init --cone --sparse-index &&
+
+ test_all_match git status --porcelain=v2 &&
+
+ # This checkout command should fail, because we have a staged
+ # change to folder1/larger-content, but the destination changes
+ # folder1 to a file.
+ git -C full-checkout checkout df-conflict-1 \
+ 1>full-checkout-out \
+ 2>full-checkout-err &&
+ git -C sparse-checkout checkout df-conflict-1 \
+ 1>sparse-checkout-out \
+ 2>sparse-checkout-err &&
+ git -C sparse-index checkout df-conflict-1 \
+ 1>sparse-index-out \
+ 2>sparse-index-err &&
+
+ # Instead, the checkout deletes the folder1 file and adds the
+ # folder1/larger-content file, leaving all other paths that were
+ # in folder1/ as deleted (without any warning).
+ cat >expect <<-EOF &&
+ D folder1
+ A folder1/larger-content
+ EOF
+ test_cmp expect full-checkout-out &&
+ test_cmp expect sparse-checkout-out &&
+
+ # The sparse-index reports no output
+ test_must_be_empty sparse-index-out &&
+
+ # stderr: Switched to branch df-conflict-1
+ test_cmp full-checkout-err sparse-checkout-err &&
+ test_cmp full-checkout-err sparse-checkout-err
+'
+
+# NEEDSWORK: 'git checkout' behaves incorrectly in the case of
+# directory/file conflicts, even without sparse-checkout. Use this
+# test only as a documentation of the incorrect behavior, not a
+# measure of how it _should_ behave.
+test_expect_success 'checkout behaves oddly with df-conflict-2' '
+ init_repos &&
+
+ test_sparse_match git sparse-checkout disable &&
+
+ write_script edit-content <<-\EOF &&
+ echo content >>folder2/larger-content
+ git add folder2
+ EOF
+
+ run_on_all ../edit-content &&
+ test_all_match git status --porcelain=v2 &&
+
+ git -C sparse-checkout sparse-checkout init --cone &&
+ git -C sparse-index sparse-checkout init --cone --sparse-index &&
+
+ test_all_match git status --porcelain=v2 &&
+
+ # This checkout command should fail, because we have a staged
+ # change to folder1/larger-content, but the destination changes
+ # folder1 to a file.
+ git -C full-checkout checkout df-conflict-2 \
+ 1>full-checkout-out \
+ 2>full-checkout-err &&
+ git -C sparse-checkout checkout df-conflict-2 \
+ 1>sparse-checkout-out \
+ 2>sparse-checkout-err &&
+ git -C sparse-index checkout df-conflict-2 \
+ 1>sparse-index-out \
+ 2>sparse-index-err &&
+
+ # The full checkout deviates from the df-conflict-1 case here!
+ # It drops the change to folder1/larger-content and leaves the
+ # folder1 path as-is on disk. The sparse-index behaves the same.
+ test_must_be_empty full-checkout-out &&
+ test_must_be_empty sparse-index-out &&
+
+ # In the sparse-checkout case, the checkout deletes the folder1
+ # file and adds the folder1/larger-content file, leaving all other
+ # paths that were in folder1/ as deleted (without any warning).
+ cat >expect <<-EOF &&
+ D folder2
+ A folder2/larger-content
+ EOF
+ test_cmp expect sparse-checkout-out &&
+
+ # Switched to branch df-conflict-1
+ test_cmp full-checkout-err sparse-checkout-err &&
+ test_cmp full-checkout-err sparse-index-err
+'
+
+test_expect_success 'mv directory from out-of-cone to in-cone' '
+ init_repos &&
+
+ # <source> as a sparse directory (or SKIP_WORKTREE_DIR without enabling
+ # sparse index).
+ test_all_match git mv --sparse folder1 deep &&
+ test_all_match git status --porcelain=v2 &&
+ test_sparse_match git ls-files -t &&
+ git -C sparse-checkout ls-files -t >actual &&
+ grep -e "H deep/folder1/0/0/0" actual &&
+ grep -e "H deep/folder1/0/1" actual &&
+ grep -e "H deep/folder1/a" actual &&
+
+ test_all_match git reset --hard &&
+
+ # <source> as a directory deeper than sparse index boundary (where
+ # sparse index will expand).
+ test_sparse_match git mv --sparse folder1/0 deep &&
+ test_sparse_match git status --porcelain=v2 &&
+ test_sparse_match git ls-files -t &&
+ git -C sparse-checkout ls-files -t >actual &&
+ grep -e "H deep/0/0/0" actual &&
+ grep -e "H deep/0/1" actual
+'
+
+test_expect_success 'rm pathspec inside sparse definition' '
+ init_repos &&
+
+ test_all_match git rm deep/a &&
+ test_all_match git status --porcelain=v2 &&
+
+ # test wildcard
+ run_on_all git reset --hard &&
+ test_all_match git rm deep/* &&
+ test_all_match git status --porcelain=v2 &&
+
+ # test recursive rm
+ run_on_all git reset --hard &&
+ test_all_match git rm -r deep &&
+ test_all_match git status --porcelain=v2
+'
+
+test_expect_success 'rm pathspec outside sparse definition' '
+ init_repos &&
+
+ for file in folder1/a folder1/0/1
+ do
+ test_sparse_match test_must_fail git rm $file &&
+ test_sparse_match test_must_fail git rm --cached $file &&
+ test_sparse_match git rm --sparse $file &&
+ test_sparse_match git status --porcelain=v2 || return 1
+ done &&
+
+ cat >folder1-full <<-EOF &&
+ rm ${SQ}folder1/0/0/0${SQ}
+ rm ${SQ}folder1/0/1${SQ}
+ rm ${SQ}folder1/a${SQ}
+ EOF
+
+ cat >folder1-sparse <<-EOF &&
+ rm ${SQ}folder1/${SQ}
+ EOF
+
+ # test wildcard
+ run_on_sparse git reset --hard &&
+ run_on_sparse git sparse-checkout reapply &&
+ test_sparse_match test_must_fail git rm folder1/* &&
+ run_on_sparse git rm --sparse folder1/* &&
+ test_cmp folder1-full sparse-checkout-out &&
+ test_cmp folder1-sparse sparse-index-out &&
+ test_sparse_match git status --porcelain=v2 &&
+
+ # test recursive rm
+ run_on_sparse git reset --hard &&
+ run_on_sparse git sparse-checkout reapply &&
+ test_sparse_match test_must_fail git rm --sparse folder1 &&
+ run_on_sparse git rm --sparse -r folder1 &&
+ test_cmp folder1-full sparse-checkout-out &&
+ test_cmp folder1-sparse sparse-index-out &&
+ test_sparse_match git status --porcelain=v2
+'
+
+test_expect_success 'rm pathspec expands index when necessary' '
+ init_repos &&
+
+ # in-cone pathspec (do not expand)
+ ensure_not_expanded rm "deep/deep*" &&
+ test_must_be_empty sparse-index-err &&
+
+ # out-of-cone pathspec (expand)
+ ! ensure_not_expanded rm --sparse "folder1/a*" &&
+ test_must_be_empty sparse-index-err &&
+
+ # pathspec that should expand index
+ ! ensure_not_expanded rm "*/a" &&
+ test_must_be_empty sparse-index-err &&
+
+ ! ensure_not_expanded rm "**a" &&
+ test_must_be_empty sparse-index-err
+'
+
+test_expect_success 'sparse index is not expanded: rm' '
+ init_repos &&
+
+ ensure_not_expanded rm deep/a &&
+
+ # test in-cone wildcard
+ git -C sparse-index reset --hard &&
+ ensure_not_expanded rm deep/* &&
+
+ # test recursive rm
+ git -C sparse-index reset --hard &&
+ ensure_not_expanded rm -r deep
+'
+
+test_expect_success 'grep with and --cached' '
+ init_repos &&
+
+ test_all_match git grep --cached a &&
+ test_all_match git grep --cached a -- "folder1/*"
+'
+
+test_expect_success 'grep is not expanded' '
+ init_repos &&
+
+ ensure_not_expanded grep a &&
+ ensure_not_expanded grep a -- deep/* &&
+
+ # All files within the folder1/* pathspec are sparse,
+ # so this command does not find any matches
+ ensure_not_expanded ! grep a -- folder1/* &&
+
+ # test out-of-cone pathspec with or without wildcard
+ ensure_not_expanded grep --cached a -- "folder1/a" &&
+ ensure_not_expanded grep --cached a -- "folder1/*" &&
+
+ # test in-cone pathspec with or without wildcard
+ ensure_not_expanded grep --cached a -- "deep/a" &&
+ ensure_not_expanded grep --cached a -- "deep/*"
+'
+
+# NEEDSWORK: when running `grep` in the superproject with --recurse-submodules,
+# Git expands the index of the submodules unexpectedly. Even though `grep`
+# builtin is marked as "command_requires_full_index = 0", this config is only
+# useful for the superproject. Namely, the submodules have their own configs,
+# which are _not_ populated by the one-time sparse-index feature switch.
+test_expect_failure 'grep within submodules is not expanded' '
+ init_repos_as_submodules &&
+
+ # do not use ensure_not_expanded() here, becasue `grep` should be
+ # run in the superproject, not in "./sparse-index"
+ GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
+ git grep --cached --recurse-submodules a -- "*/folder1/*" &&
+ test_region ! index ensure_full_index trace2.txt
+'
+
+# NEEDSWORK: this test is not actually testing the code. The design purpose
+# of this test is to verify the grep result when the submodules are using a
+# sparse-index. Namely, we want "folder1/" as a tree (a sparse directory); but
+# because of the index expansion, we are now grepping the "folder1/a" blob.
+# Because of the problem stated above 'grep within submodules is not expanded',
+# we don't have the ideal test environment yet.
+test_expect_success 'grep sparse directory within submodules' '
+ init_repos_as_submodules &&
+
+ cat >expect <<-\EOF &&
+ full-checkout/folder1/a:a
+ sparse-checkout/folder1/a:a
+ sparse-index/folder1/a:a
+ EOF
+ git grep --cached --recurse-submodules a -- "*/folder1/*" >actual &&
+ test_cmp actual expect
+'
+
+test_expect_success 'write-tree' '
+ init_repos &&
+
+ test_all_match git write-tree &&
+
+ write_script edit-contents <<-\EOF &&
+ echo text >>"$1"
+ EOF
+
+ # make a change inside the sparse cone
+ run_on_all ../edit-contents deep/a &&
+ test_all_match git update-index deep/a &&
+ test_all_match git write-tree &&
+ test_all_match git status --porcelain=v2 &&
+
+ # make a change outside the sparse cone
+ run_on_all mkdir -p folder1 &&
+ run_on_all cp a folder1/a &&
+ run_on_all ../edit-contents folder1/a &&
+ test_all_match git update-index folder1/a &&
+ test_all_match git write-tree &&
+ test_all_match git status --porcelain=v2 &&
+
+ # check that SKIP_WORKTREE files are not materialized
+ test_path_is_missing sparse-checkout/folder2/a &&
+ test_path_is_missing sparse-index/folder2/a
+'
+
+test_expect_success 'sparse-index is not expanded: write-tree' '
+ init_repos &&
+
+ ensure_not_expanded write-tree &&
+
+ echo "test1" >>sparse-index/a &&
+ git -C sparse-index update-index a &&
+ ensure_not_expanded write-tree
+'
+
+test_expect_success 'diff-files with pathspec inside sparse definition' '
+ init_repos &&
+
+ write_script edit-contents <<-\EOF &&
+ echo text >>"$1"
+ EOF
+
+ run_on_all ../edit-contents deep/a &&
+
+ test_all_match git diff-files &&
+
+ test_all_match git diff-files -- deep/a &&
+
+ # test wildcard
+ test_all_match git diff-files -- "deep/*"
+'
+
+test_expect_success 'diff-files with pathspec outside sparse definition' '
+ init_repos &&
+
+ test_sparse_match git diff-files -- folder2/a &&
+
+ write_script edit-contents <<-\EOF &&
+ echo text >>"$1"
+ EOF
+
+ # The directory "folder1" is outside the cone of interest
+ # and will not exist in the sparse checkout repositories.
+ # Create it as needed, add file "folder1/a" there with
+ # contents that is different from the staged version.
+ run_on_all mkdir -p folder1 &&
+ run_on_all cp a folder1/a &&
+
+ run_on_all ../edit-contents folder1/a &&
+ test_all_match git diff-files &&
+ test_all_match git diff-files -- folder1/a &&
+ test_all_match git diff-files -- "folder*/a"
+'
+
+test_expect_success 'sparse index is not expanded: diff-files' '
+ init_repos &&
+
+ write_script edit-contents <<-\EOF &&
+ echo text >>"$1"
+ EOF
+
+ run_on_all ../edit-contents deep/a &&
+
+ ensure_not_expanded diff-files &&
+ ensure_not_expanded diff-files -- deep/a &&
+ ensure_not_expanded diff-files -- "deep/*"
+'
+
+test_expect_success 'diff-tree' '
+ init_repos &&
+
+ # Test change inside sparse cone
+ tree1=$(git -C sparse-index rev-parse HEAD^{tree}) &&
+ tree2=$(git -C sparse-index rev-parse update-deep^{tree}) &&
+ test_all_match git diff-tree $tree1 $tree2 &&
+ test_all_match git diff-tree $tree1 $tree2 -- deep/a &&
+ test_all_match git diff-tree HEAD update-deep &&
+ test_all_match git diff-tree HEAD update-deep -- deep/a &&
+
+ # Test change outside sparse cone
+ tree3=$(git -C sparse-index rev-parse update-folder1^{tree}) &&
+ test_all_match git diff-tree $tree1 $tree3 &&
+ test_all_match git diff-tree $tree1 $tree3 -- folder1/a &&
+ test_all_match git diff-tree HEAD update-folder1 &&
+ test_all_match git diff-tree HEAD update-folder1 -- folder1/a &&
+
+ # Check that SKIP_WORKTREE files are not materialized
+ test_path_is_missing sparse-checkout/folder1/a &&
+ test_path_is_missing sparse-index/folder1/a &&
+ test_path_is_missing sparse-checkout/folder2/a &&
+ test_path_is_missing sparse-index/folder2/a
+'
+
+test_expect_success 'sparse-index is not expanded: diff-tree' '
+ init_repos &&
+
+ tree1=$(git -C sparse-index rev-parse HEAD^{tree}) &&
+ tree2=$(git -C sparse-index rev-parse update-deep^{tree}) &&
+ tree3=$(git -C sparse-index rev-parse update-folder1^{tree}) &&
+
+ ensure_not_expanded diff-tree $tree1 $tree2 &&
+ ensure_not_expanded diff-tree $tree1 $tree2 -- deep/a &&
+ ensure_not_expanded diff-tree HEAD update-deep &&
+ ensure_not_expanded diff-tree HEAD update-deep -- deep/a &&
+ ensure_not_expanded diff-tree $tree1 $tree3 &&
+ ensure_not_expanded diff-tree $tree1 $tree3 -- folder1/a &&
+ ensure_not_expanded diff-tree HEAD update-folder1 &&
+ ensure_not_expanded diff-tree HEAD update-folder1 -- folder1/a
+'
+
+test_expect_success 'worktree' '
+ init_repos &&
+
+ write_script edit-contents <<-\EOF &&
+ echo text >>"$1"
+ EOF
+
+ for repo in full-checkout sparse-checkout sparse-index
+ do
+ worktree=${repo}-wt &&
+ git -C $repo worktree add ../$worktree &&
+
+ # Compare worktree content with "ls"
+ (cd $repo && ls) >worktree_contents &&
+ (cd $worktree && ls) >new_worktree_contents &&
+ test_cmp worktree_contents new_worktree_contents &&
+
+ # Compare index content with "ls-files --sparse"
+ git -C $repo ls-files --sparse >index_contents &&
+ git -C $worktree ls-files --sparse >new_index_contents &&
+ test_cmp index_contents new_index_contents &&
+
+ git -C $repo worktree remove ../$worktree || return 1
+ done &&
+
+ test_all_match git worktree add .worktrees/hotfix &&
+ run_on_all ../edit-contents .worktrees/hotfix/deep/a &&
+ test_all_match test_must_fail git worktree remove .worktrees/hotfix
+'
+
+test_expect_success 'worktree is not expanded' '
+ init_repos &&
+
+ ensure_not_expanded worktree add .worktrees/hotfix &&
+ ensure_not_expanded worktree remove .worktrees/hotfix
+'
+
+test_expect_success 'check-attr with pathspec inside sparse definition' '
+ init_repos &&
+
+ echo "a -crlf myAttr" >>.gitattributes &&
+ run_on_all cp ../.gitattributes ./deep &&
+
+ test_all_match git check-attr -a -- deep/a &&
+
+ test_all_match git add deep/.gitattributes &&
+ test_all_match git check-attr -a --cached -- deep/a
+'
+
+test_expect_success 'check-attr with pathspec outside sparse definition' '
+ init_repos &&
+
+ echo "a -crlf myAttr" >>.gitattributes &&
+ run_on_sparse mkdir folder1 &&
+ run_on_all cp ../.gitattributes ./folder1 &&
+ run_on_all cp a folder1/a &&
+
+ test_all_match git check-attr -a -- folder1/a &&
+
+ git -C full-checkout add folder1/.gitattributes &&
+ test_sparse_match git add --sparse folder1/.gitattributes &&
+ test_all_match git commit -m "add .gitattributes" &&
+ test_sparse_match git sparse-checkout reapply &&
+ test_all_match git check-attr -a --cached -- folder1/a
+'
+
+# NEEDSWORK: The 'diff --check' test is left as 'test_expect_failure' due
+# to an underlying issue in oneway_diff() within diff-lib.c.
+# 'do_oneway_diff()' is not called as expected for paths that could match
+# inside of a sparse directory. Specifically, the 'ce_path_match()' function
+# fails to recognize files inside a sparse directory (e.g., when 'folder1/'
+# is a sparse directory, 'folder1/a' cannot be recognized). The goal is to
+# proceed with 'do_oneway_diff()' if the pathspec could match inside of a
+# sparse directory.
+test_expect_failure 'diff --check with pathspec outside sparse definition' '
+ init_repos &&
+
+ write_script edit-contents <<-\EOF &&
+ echo "a " >"$1"
+ EOF
+
+ test_all_match git config core.whitespace -trailing-space,-space-before-tab &&
+
+ echo "a whitespace=trailing-space,space-before-tab" >>.gitattributes &&
+ run_on_all mkdir -p folder1 &&
+ run_on_all cp ../.gitattributes ./folder1 &&
+ test_all_match git add --sparse folder1/.gitattributes &&
+ run_on_all ../edit-contents folder1/a &&
+ test_all_match git add --sparse folder1/a &&
+
+ test_sparse_match git sparse-checkout reapply &&
+ test_all_match test_must_fail git diff --check --cached -- folder1/a
+'
+
+test_expect_success 'sparse-index is not expanded: check-attr' '
+ init_repos &&
+
+ echo "a -crlf myAttr" >>.gitattributes &&
+ mkdir ./sparse-index/folder1 &&
+ cp ./sparse-index/a ./sparse-index/folder1/a &&
+ cp .gitattributes ./sparse-index/deep &&
+ cp .gitattributes ./sparse-index/folder1 &&
+
+ git -C sparse-index add deep/.gitattributes &&
+ git -C sparse-index add --sparse folder1/.gitattributes &&
+ ensure_not_expanded check-attr -a --cached -- deep/a &&
+ ensure_not_expanded check-attr -a --cached -- folder1/a
+'
+
+test_done
diff --git a/t/t1100-commit-tree-options.sh b/t/t1100-commit-tree-options.sh
new file mode 100755
index 0000000..0f37a43
--- /dev/null
+++ b/t/t1100-commit-tree-options.sh
@@ -0,0 +1,64 @@
+#!/bin/sh
+#
+# Copyright (C) 2005 Rene Scharfe
+#
+
+test_description='git commit-tree options test
+
+This test checks that git commit-tree can create a specific commit
+object by defining all environment variables that it understands.
+
+Also make sure that command line parser understands the normal
+"flags first and then non flag arguments" command line.
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+cat >expected <<EOF
+tree $EMPTY_TREE
+author Author Name <author@email> 1117148400 +0000
+committer Committer Name <committer@email> 1117150200 +0000
+
+comment text
+EOF
+
+test_expect_success \
+ 'test preparation: write empty tree' \
+ 'git write-tree >treeid'
+
+test_expect_success \
+ 'construct commit' \
+ 'echo comment text |
+ GIT_AUTHOR_NAME="Author Name" \
+ GIT_AUTHOR_EMAIL="author@email" \
+ GIT_AUTHOR_DATE="2005-05-26 23:00" \
+ GIT_COMMITTER_NAME="Committer Name" \
+ GIT_COMMITTER_EMAIL="committer@email" \
+ GIT_COMMITTER_DATE="2005-05-26 23:30" \
+ TZ=GMT git commit-tree $(cat treeid) >commitid 2>/dev/null'
+
+test_expect_success \
+ 'read commit' \
+ 'git cat-file commit $(cat commitid) >commit'
+
+test_expect_success \
+ 'compare commit' \
+ 'test_cmp expected commit'
+
+
+test_expect_success 'flags and then non flags' '
+ test_tick &&
+ echo comment text |
+ git commit-tree $(cat treeid) >commitid &&
+ echo comment text |
+ git commit-tree $(cat treeid) -p $(cat commitid) >childid-1 &&
+ echo comment text |
+ git commit-tree -p $(cat commitid) $(cat treeid) >childid-2 &&
+ test_cmp childid-1 childid-2 &&
+ git commit-tree $(cat treeid) -m foo >childid-3 &&
+ git commit-tree -m foo $(cat treeid) >childid-4 &&
+ test_cmp childid-3 childid-4
+'
+
+test_done
diff --git a/t/t1300-config.sh b/t/t1300-config.sh
new file mode 100755
index 0000000..f4e2752
--- /dev/null
+++ b/t/t1300-config.sh
@@ -0,0 +1,2593 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Johannes Schindelin
+#
+
+test_description='Test git config in different settings'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'clear default config' '
+ rm -f .git/config
+'
+
+cat > expect << EOF
+[section]
+ penguin = little blue
+EOF
+test_expect_success 'initial' '
+ git config section.penguin "little blue" &&
+ test_cmp expect .git/config
+'
+
+cat > expect << EOF
+[section]
+ penguin = little blue
+ Movie = BadPhysics
+EOF
+test_expect_success 'mixed case' '
+ git config Section.Movie BadPhysics &&
+ test_cmp expect .git/config
+'
+
+cat > expect << EOF
+[section]
+ penguin = little blue
+ Movie = BadPhysics
+[Sections]
+ WhatEver = Second
+EOF
+test_expect_success 'similar section' '
+ git config Sections.WhatEver Second &&
+ test_cmp expect .git/config
+'
+
+cat > expect << EOF
+[section]
+ penguin = little blue
+ Movie = BadPhysics
+ UPPERCASE = true
+[Sections]
+ WhatEver = Second
+EOF
+test_expect_success 'uppercase section' '
+ git config SECTION.UPPERCASE true &&
+ test_cmp expect .git/config
+'
+
+test_expect_success 'replace with non-match' '
+ git config section.penguin kingpin !blue
+'
+
+test_expect_success 'replace with non-match (actually matching)' '
+ git config section.penguin "very blue" !kingpin
+'
+
+cat > expect << EOF
+[section]
+ penguin = very blue
+ Movie = BadPhysics
+ UPPERCASE = true
+ penguin = kingpin
+[Sections]
+ WhatEver = Second
+EOF
+
+test_expect_success 'non-match result' 'test_cmp expect .git/config'
+
+test_expect_success 'find mixed-case key by canonical name' '
+ test_cmp_config Second sections.whatever
+'
+
+test_expect_success 'find mixed-case key by non-canonical name' '
+ test_cmp_config Second SeCtIoNs.WhAtEvEr
+'
+
+test_expect_success 'subsections are not canonicalized by git-config' '
+ cat >>.git/config <<-\EOF &&
+ [section.SubSection]
+ key = one
+ [section "SubSection"]
+ key = two
+ EOF
+ test_cmp_config one section.subsection.key &&
+ test_cmp_config two section.SubSection.key
+'
+
+test_missing_key () {
+ local key="$1" &&
+ local title="$2" &&
+ test_expect_success "value for $title is not printed" '
+ test_must_fail git config "$key" >out 2>err &&
+ test_must_be_empty out &&
+ test_must_be_empty err
+ '
+}
+
+test_missing_key 'missingsection.missingkey' 'missing section and missing key'
+test_missing_key 'missingsection.penguin' 'missing section and existing key'
+test_missing_key 'section.missingkey' 'existing section and missing key'
+test_missing_key 'section.MissingSubSection.missingkey' 'missing subsection and missing key'
+test_missing_key 'section.SubSection.missingkey' 'existing subsection and missing key'
+test_missing_key 'section.MissingSubSection.key' 'missing subsection and existing key'
+
+cat > .git/config <<\EOF
+[alpha]
+bar = foo
+[beta]
+baz = multiple \
+lines
+foo = bar
+EOF
+
+test_expect_success 'unset with cont. lines' '
+ git config --unset beta.baz
+'
+
+cat > expect <<\EOF
+[alpha]
+bar = foo
+[beta]
+foo = bar
+EOF
+
+test_expect_success 'unset with cont. lines is correct' 'test_cmp expect .git/config'
+
+cat > .git/config << EOF
+[beta] ; silly comment # another comment
+noIndent= sillyValue ; 'nother silly comment
+
+# empty line
+ ; comment
+ haha ="beta" # last silly comment
+haha = hello
+ haha = bello
+[nextSection] noNewline = ouch
+EOF
+
+cp .git/config .git/config2
+
+test_expect_success 'multiple unset' '
+ git config --unset-all beta.haha
+'
+
+cat > expect << EOF
+[beta] ; silly comment # another comment
+noIndent= sillyValue ; 'nother silly comment
+
+# empty line
+ ; comment
+[nextSection] noNewline = ouch
+EOF
+
+test_expect_success 'multiple unset is correct' '
+ test_cmp expect .git/config
+'
+
+cp .git/config2 .git/config
+
+test_expect_success '--replace-all missing value' '
+ test_must_fail git config --replace-all beta.haha &&
+ test_cmp .git/config2 .git/config
+'
+
+rm .git/config2
+
+test_expect_success '--replace-all' '
+ git config --replace-all beta.haha gamma
+'
+
+cat > expect << EOF
+[beta] ; silly comment # another comment
+noIndent= sillyValue ; 'nother silly comment
+
+# empty line
+ ; comment
+ haha = gamma
+[nextSection] noNewline = ouch
+EOF
+
+test_expect_success 'all replaced' '
+ test_cmp expect .git/config
+'
+
+cat > expect << EOF
+[beta] ; silly comment # another comment
+noIndent= sillyValue ; 'nother silly comment
+
+# empty line
+ ; comment
+ haha = alpha
+[nextSection] noNewline = ouch
+EOF
+test_expect_success 'really mean test' '
+ git config beta.haha alpha &&
+ test_cmp expect .git/config
+'
+
+cat > expect << EOF
+[beta] ; silly comment # another comment
+noIndent= sillyValue ; 'nother silly comment
+
+# empty line
+ ; comment
+ haha = alpha
+[nextSection]
+ nonewline = wow
+EOF
+test_expect_success 'really really mean test' '
+ git config nextsection.nonewline wow &&
+ test_cmp expect .git/config
+'
+
+test_expect_success 'get value' '
+ test_cmp_config alpha beta.haha
+'
+
+cat > expect << EOF
+[beta] ; silly comment # another comment
+noIndent= sillyValue ; 'nother silly comment
+
+# empty line
+ ; comment
+[nextSection]
+ nonewline = wow
+EOF
+test_expect_success 'unset' '
+ git config --unset beta.haha &&
+ test_cmp expect .git/config
+'
+
+cat > expect << EOF
+[beta] ; silly comment # another comment
+noIndent= sillyValue ; 'nother silly comment
+
+# empty line
+ ; comment
+[nextSection]
+ nonewline = wow
+ NoNewLine = wow2 for me
+EOF
+test_expect_success 'multivar' '
+ git config nextsection.NoNewLine "wow2 for me" "for me$" &&
+ test_cmp expect .git/config
+'
+
+test_expect_success 'non-match' '
+ git config --get nextsection.nonewline !for
+'
+
+test_expect_success 'non-match value' '
+ test_cmp_config wow --get nextsection.nonewline !for
+'
+
+test_expect_success 'multi-valued get returns final one' '
+ test_cmp_config "wow2 for me" --get nextsection.nonewline
+'
+
+test_expect_success 'multi-valued get-all returns all' '
+ cat >expect <<-\EOF &&
+ wow
+ wow2 for me
+ EOF
+ git config --get-all nextsection.nonewline >actual &&
+ test_cmp expect actual
+'
+
+cat > expect << EOF
+[beta] ; silly comment # another comment
+noIndent= sillyValue ; 'nother silly comment
+
+# empty line
+ ; comment
+[nextSection]
+ nonewline = wow3
+ NoNewLine = wow2 for me
+EOF
+test_expect_success 'multivar replace' '
+ git config nextsection.nonewline "wow3" "wow$" &&
+ test_cmp expect .git/config
+'
+
+test_expect_success 'ambiguous unset' '
+ test_must_fail git config --unset nextsection.nonewline
+'
+
+test_expect_success 'invalid unset' '
+ test_must_fail git config --unset somesection.nonewline
+'
+
+cat > expect << EOF
+[beta] ; silly comment # another comment
+noIndent= sillyValue ; 'nother silly comment
+
+# empty line
+ ; comment
+[nextSection]
+ NoNewLine = wow2 for me
+EOF
+
+test_expect_success 'multivar unset' '
+ git config --unset nextsection.nonewline "wow3$" &&
+ test_cmp expect .git/config
+'
+
+test_expect_success 'invalid key' 'test_must_fail git config inval.2key blabla'
+
+test_expect_success 'correct key' 'git config 123456.a123 987'
+
+test_expect_success 'hierarchical section' '
+ git config Version.1.2.3eX.Alpha beta
+'
+
+cat > expect << EOF
+[beta] ; silly comment # another comment
+noIndent= sillyValue ; 'nother silly comment
+
+# empty line
+ ; comment
+[nextSection]
+ NoNewLine = wow2 for me
+[123456]
+ a123 = 987
+[Version "1.2.3eX"]
+ Alpha = beta
+EOF
+
+test_expect_success 'hierarchical section value' '
+ test_cmp expect .git/config
+'
+
+cat > expect << EOF
+beta.noindent=sillyValue
+nextsection.nonewline=wow2 for me
+123456.a123=987
+version.1.2.3eX.alpha=beta
+EOF
+
+test_expect_success 'working --list' '
+ git config --list > output &&
+ test_cmp expect output
+'
+test_expect_success '--list without repo produces empty output' '
+ git --git-dir=nonexistent config --list >output &&
+ test_must_be_empty output
+'
+
+cat > expect << EOF
+beta.noindent
+nextsection.nonewline
+123456.a123
+version.1.2.3eX.alpha
+EOF
+
+test_expect_success '--name-only --list' '
+ git config --name-only --list >output &&
+ test_cmp expect output
+'
+
+cat > expect << EOF
+beta.noindent sillyValue
+nextsection.nonewline wow2 for me
+EOF
+
+test_expect_success '--get-regexp' '
+ git config --get-regexp in >output &&
+ test_cmp expect output
+'
+
+cat > expect << EOF
+beta.noindent
+nextsection.nonewline
+EOF
+
+test_expect_success '--name-only --get-regexp' '
+ git config --name-only --get-regexp in >output &&
+ test_cmp expect output
+'
+
+cat > expect << EOF
+wow2 for me
+wow4 for you
+EOF
+
+test_expect_success '--add' '
+ git config --add nextsection.nonewline "wow4 for you" &&
+ git config --get-all nextsection.nonewline > output &&
+ test_cmp expect output
+'
+
+cat > .git/config << EOF
+[novalue]
+ variable
+[emptyvalue]
+ variable =
+EOF
+
+test_expect_success 'get variable with no value' '
+ git config --get novalue.variable ^$
+'
+
+test_expect_success 'get variable with empty value' '
+ git config --get emptyvalue.variable ^$
+'
+
+echo novalue.variable > expect
+
+test_expect_success 'get-regexp variable with no value' '
+ git config --get-regexp novalue > output &&
+ test_cmp expect output
+'
+
+echo 'novalue.variable true' > expect
+
+test_expect_success 'get-regexp --bool variable with no value' '
+ git config --bool --get-regexp novalue > output &&
+ test_cmp expect output
+'
+
+echo 'emptyvalue.variable ' > expect
+
+test_expect_success 'get-regexp variable with empty value' '
+ git config --get-regexp emptyvalue > output &&
+ test_cmp expect output
+'
+
+echo true > expect
+
+test_expect_success 'get bool variable with no value' '
+ git config --bool novalue.variable > output &&
+ test_cmp expect output
+'
+
+echo false > expect
+
+test_expect_success 'get bool variable with empty value' '
+ git config --bool emptyvalue.variable > output &&
+ test_cmp expect output
+'
+
+test_expect_success 'no arguments, but no crash' '
+ test_must_fail git config >output 2>&1 &&
+ test_grep usage output
+'
+
+cat > .git/config << EOF
+[a.b]
+ c = d
+EOF
+
+cat > expect << EOF
+[a.b]
+ c = d
+[a]
+ x = y
+EOF
+
+test_expect_success 'new section is partial match of another' '
+ git config a.x y &&
+ test_cmp expect .git/config
+'
+
+cat > expect << EOF
+[a.b]
+ c = d
+[a]
+ x = y
+ b = c
+[b]
+ x = y
+EOF
+
+test_expect_success 'new variable inserts into proper section' '
+ git config b.x y &&
+ git config a.b c &&
+ test_cmp expect .git/config
+'
+
+test_expect_success 'alternative --file (non-existing file should fail)' '
+ test_must_fail git config --file non-existing-config -l &&
+ test_must_fail git config --file non-existing-config test.xyzzy
+'
+
+cat > other-config << EOF
+[ein]
+ bahn = strasse
+EOF
+
+cat > expect << EOF
+ein.bahn=strasse
+EOF
+
+test_expect_success 'alternative GIT_CONFIG' '
+ GIT_CONFIG=other-config git config --list >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'alternative GIT_CONFIG (--file)' '
+ git config --file other-config --list >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'alternative GIT_CONFIG (--file=-)' '
+ git config --file - --list <other-config >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'setting a value in stdin is an error' '
+ test_must_fail git config --file - some.value foo
+'
+
+test_expect_success 'editing stdin is an error' '
+ test_must_fail git config --file - --edit
+'
+
+test_expect_success 'refer config from subdirectory' '
+ mkdir x &&
+ test_cmp_config -C x strasse --file=../other-config --get ein.bahn
+'
+
+cat > expect << EOF
+[ein]
+ bahn = strasse
+[anwohner]
+ park = ausweis
+EOF
+
+test_expect_success '--set in alternative file' '
+ git config --file=other-config anwohner.park ausweis &&
+ test_cmp expect other-config
+'
+
+cat > .git/config << EOF
+# Hallo
+ #Bello
+[branch "eins"]
+ x = 1
+[branch.eins]
+ y = 1
+ [branch "1 234 blabl/a"]
+weird
+EOF
+
+test_expect_success 'rename section' '
+ git config --rename-section branch.eins branch.zwei
+'
+
+cat > expect << EOF
+# Hallo
+ #Bello
+[branch "zwei"]
+ x = 1
+[branch "zwei"]
+ y = 1
+ [branch "1 234 blabl/a"]
+weird
+EOF
+
+test_expect_success 'rename succeeded' '
+ test_cmp expect .git/config
+'
+
+test_expect_success 'rename non-existing section' '
+ test_must_fail git config --rename-section \
+ branch."world domination" branch.drei
+'
+
+test_expect_success 'rename succeeded' '
+ test_cmp expect .git/config
+'
+
+test_expect_success 'rename another section' '
+ git config --rename-section branch."1 234 blabl/a" branch.drei
+'
+
+cat > expect << EOF
+# Hallo
+ #Bello
+[branch "zwei"]
+ x = 1
+[branch "zwei"]
+ y = 1
+[branch "drei"]
+weird
+EOF
+
+test_expect_success 'rename succeeded' '
+ test_cmp expect .git/config
+'
+
+cat >> .git/config << EOF
+[branch "vier"] z = 1
+EOF
+
+test_expect_success 'rename a section with a var on the same line' '
+ git config --rename-section branch.vier branch.zwei
+'
+
+cat > expect << EOF
+# Hallo
+ #Bello
+[branch "zwei"]
+ x = 1
+[branch "zwei"]
+ y = 1
+[branch "drei"]
+weird
+[branch "zwei"]
+ z = 1
+EOF
+
+test_expect_success 'rename succeeded' '
+ test_cmp expect .git/config
+'
+
+test_expect_success 'renaming empty section name is rejected' '
+ test_must_fail git config --rename-section branch.zwei ""
+'
+
+test_expect_success 'renaming to bogus section is rejected' '
+ test_must_fail git config --rename-section branch.zwei "bogus name"
+'
+
+test_expect_success 'renaming a section with a long line' '
+ {
+ printf "[b]\\n" &&
+ printf " c = d %1024s [a] e = f\\n" " " &&
+ printf "[a] g = h\\n"
+ } >y &&
+ git config -f y --rename-section a xyz &&
+ test_must_fail git config -f y b.e
+'
+
+test_expect_success 'renaming an embedded section with a long line' '
+ {
+ printf "[b]\\n" &&
+ printf " c = d %1024s [a] [foo] e = f\\n" " " &&
+ printf "[a] g = h\\n"
+ } >y &&
+ git config -f y --rename-section a xyz &&
+ test_must_fail git config -f y foo.e
+'
+
+test_expect_success 'renaming a section with an overly-long line' '
+ {
+ printf "[b]\\n" &&
+ printf " c = d %525000s e" " " &&
+ printf "[a] g = h\\n"
+ } >y &&
+ test_must_fail git config -f y --rename-section a xyz 2>err &&
+ grep "refusing to work with overly long line in .y. on line 2" err
+'
+
+cat >> .git/config << EOF
+ [branch "zwei"] a = 1 [branch "vier"]
+EOF
+
+test_expect_success 'remove section' '
+ git config --remove-section branch.zwei
+'
+
+cat > expect << EOF
+# Hallo
+ #Bello
+[branch "drei"]
+weird
+EOF
+
+test_expect_success 'section was removed properly' '
+ test_cmp expect .git/config
+'
+
+cat > expect << EOF
+[gitcvs]
+ enabled = true
+ dbname = %Ggitcvs2.%a.%m.sqlite
+[gitcvs "ext"]
+ dbname = %Ggitcvs1.%a.%m.sqlite
+EOF
+
+test_expect_success 'section ending' '
+ rm -f .git/config &&
+ git config gitcvs.enabled true &&
+ git config gitcvs.ext.dbname %Ggitcvs1.%a.%m.sqlite &&
+ git config gitcvs.dbname %Ggitcvs2.%a.%m.sqlite &&
+ test_cmp expect .git/config
+
+'
+
+test_expect_success numbers '
+ git config kilo.gram 1k &&
+ git config mega.ton 1m &&
+ echo 1024 >expect &&
+ echo 1048576 >>expect &&
+ git config --int --get kilo.gram >actual &&
+ git config --int --get mega.ton >>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--int is at least 64 bits' '
+ git config giga.watts 121g &&
+ echo >expect &&
+ test_cmp_config 129922760704 --int --get giga.watts
+'
+
+test_expect_success 'invalid unit' '
+ git config aninvalid.unit "1auto" &&
+ test_cmp_config 1auto aninvalid.unit &&
+ test_must_fail git config --int --get aninvalid.unit 2>actual &&
+ test_grep "bad numeric config value .1auto. for .aninvalid.unit. in file .git/config: invalid unit" actual
+'
+
+test_expect_success 'invalid unit boolean' '
+ git config commit.gpgsign "1true" &&
+ test_cmp_config 1true commit.gpgsign &&
+ test_must_fail git config --bool --get commit.gpgsign 2>actual &&
+ test_grep "bad boolean config value .1true. for .commit.gpgsign." actual
+'
+
+test_expect_success 'line number is reported correctly' '
+ printf "[bool]\n\tvar\n" >invalid &&
+ test_must_fail git config -f invalid --path bool.var 2>actual &&
+ test_grep "line 2" actual
+'
+
+test_expect_success 'invalid stdin config' '
+ echo "[broken" | test_must_fail git config --list --file - >output 2>&1 &&
+ test_grep "bad config line 1 in standard input" output
+'
+
+cat > expect << EOF
+true
+false
+true
+false
+true
+false
+true
+false
+EOF
+
+test_expect_success bool '
+
+ git config bool.true1 01 &&
+ git config bool.true2 -1 &&
+ git config bool.true3 YeS &&
+ git config bool.true4 true &&
+ git config bool.false1 000 &&
+ git config bool.false2 "" &&
+ git config bool.false3 nO &&
+ git config bool.false4 FALSE &&
+ rm -f result &&
+ for i in 1 2 3 4
+ do
+ git config --bool --get bool.true$i >>result &&
+ git config --bool --get bool.false$i >>result || return 1
+ done &&
+ test_cmp expect result'
+
+test_expect_success 'invalid bool (--get)' '
+
+ git config bool.nobool foobar &&
+ test_must_fail git config --bool --get bool.nobool'
+
+test_expect_success 'invalid bool (set)' '
+
+ test_must_fail git config --bool bool.nobool foobar'
+
+cat > expect <<\EOF
+[bool]
+ true1 = true
+ true2 = true
+ true3 = true
+ true4 = true
+ false1 = false
+ false2 = false
+ false3 = false
+ false4 = false
+EOF
+
+test_expect_success 'set --bool' '
+
+ rm -f .git/config &&
+ git config --bool bool.true1 01 &&
+ git config --bool bool.true2 -1 &&
+ git config --bool bool.true3 YeS &&
+ git config --bool bool.true4 true &&
+ git config --bool bool.false1 000 &&
+ git config --bool bool.false2 "" &&
+ git config --bool bool.false3 nO &&
+ git config --bool bool.false4 FALSE &&
+ test_cmp expect .git/config'
+
+cat > expect <<\EOF
+[int]
+ val1 = 1
+ val2 = -1
+ val3 = 5242880
+EOF
+
+test_expect_success 'set --int' '
+
+ rm -f .git/config &&
+ git config --int int.val1 01 &&
+ git config --int int.val2 -1 &&
+ git config --int int.val3 5m &&
+ test_cmp expect .git/config
+'
+
+test_expect_success 'get --bool-or-int' '
+ cat >.git/config <<-\EOF &&
+ [bool]
+ true1
+ true2 = true
+ false = false
+ [int]
+ int1 = 0
+ int2 = 1
+ int3 = -1
+ EOF
+ cat >expect <<-\EOF &&
+ true
+ true
+ false
+ 0
+ 1
+ -1
+ EOF
+ {
+ git config --bool-or-int bool.true1 &&
+ git config --bool-or-int bool.true2 &&
+ git config --bool-or-int bool.false &&
+ git config --bool-or-int int.int1 &&
+ git config --bool-or-int int.int2 &&
+ git config --bool-or-int int.int3
+ } >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<\EOF
+[bool]
+ true1 = true
+ false1 = false
+ true2 = true
+ false2 = false
+[int]
+ int1 = 0
+ int2 = 1
+ int3 = -1
+EOF
+
+test_expect_success 'set --bool-or-int' '
+ rm -f .git/config &&
+ git config --bool-or-int bool.true1 true &&
+ git config --bool-or-int bool.false1 false &&
+ git config --bool-or-int bool.true2 yes &&
+ git config --bool-or-int bool.false2 no &&
+ git config --bool-or-int int.int1 0 &&
+ git config --bool-or-int int.int2 1 &&
+ git config --bool-or-int int.int3 -1 &&
+ test_cmp expect .git/config
+'
+
+cat >expect <<\EOF
+[path]
+ home = ~/
+ normal = /dev/null
+ trailingtilde = foo~
+EOF
+
+test_expect_success !MINGW 'set --path' '
+ rm -f .git/config &&
+ git config --path path.home "~/" &&
+ git config --path path.normal "/dev/null" &&
+ git config --path path.trailingtilde "foo~" &&
+ test_cmp expect .git/config'
+
+if test_have_prereq !MINGW && test "${HOME+set}"
+then
+ test_set_prereq HOMEVAR
+fi
+
+cat >expect <<EOF
+$HOME/
+/dev/null
+foo~
+EOF
+
+test_expect_success HOMEVAR 'get --path' '
+ git config --get --path path.home > result &&
+ git config --get --path path.normal >> result &&
+ git config --get --path path.trailingtilde >> result &&
+ test_cmp expect result
+'
+
+cat >expect <<\EOF
+/dev/null
+foo~
+EOF
+
+test_expect_success !MINGW 'get --path copes with unset $HOME' '
+ (
+ sane_unset HOME &&
+ test_must_fail git config --get --path path.home \
+ >result 2>msg &&
+ git config --get --path path.normal >>result &&
+ git config --get --path path.trailingtilde >>result
+ ) &&
+ test_grep "[Ff]ailed to expand.*~/" msg &&
+ test_cmp expect result
+'
+
+test_expect_success 'get --path barfs on boolean variable' '
+ echo "[path]bool" >.git/config &&
+ test_must_fail git config --get --path path.bool
+'
+
+test_expect_success 'get --expiry-date' '
+ rel="3.weeks.5.days.00:00" &&
+ rel_out="$rel ->" &&
+ cat >.git/config <<-\EOF &&
+ [date]
+ valid1 = "3.weeks.5.days 00:00"
+ valid2 = "Fri Jun 4 15:46:55 2010"
+ valid3 = "2017/11/11 11:11:11PM"
+ valid4 = "2017/11/10 09:08:07 PM"
+ valid5 = "never"
+ invalid1 = "abc"
+ EOF
+ cat >expect <<-EOF &&
+ $(test-tool date timestamp $rel)
+ 1275666415
+ 1510441871
+ 1510348087
+ 0
+ EOF
+ : "work around heredoc parsing bug fixed in dash 0.5.7 (in ec2c84d)" &&
+ {
+ echo "$rel_out $(git config --expiry-date date.valid1)" &&
+ git config --expiry-date date.valid2 &&
+ git config --expiry-date date.valid3 &&
+ git config --expiry-date date.valid4 &&
+ git config --expiry-date date.valid5
+ } >actual &&
+ test_cmp expect actual &&
+ test_must_fail git config --expiry-date date.invalid1
+'
+
+test_expect_success 'get --type=color' '
+ rm .git/config &&
+ git config foo.color "red" &&
+ git config --get --type=color foo.color >actual.raw &&
+ test_decode_color <actual.raw >actual &&
+ echo "<RED>" >expect &&
+ test_cmp expect actual
+'
+
+cat >expect << EOF
+[foo]
+ color = red
+EOF
+
+test_expect_success 'set --type=color' '
+ rm .git/config &&
+ git config --type=color foo.color "red" &&
+ test_cmp expect .git/config
+'
+
+test_expect_success 'get --type=color barfs on non-color' '
+ echo "[foo]bar=not-a-color" >.git/config &&
+ test_must_fail git config --get --type=color foo.bar
+'
+
+test_expect_success 'set --type=color barfs on non-color' '
+ test_must_fail git config --type=color foo.color "not-a-color" 2>error &&
+ test_grep "cannot parse color" error
+'
+
+cat > expect << EOF
+[quote]
+ leading = " test"
+ ending = "test "
+ semicolon = "test;test"
+ hash = "test#test"
+EOF
+test_expect_success 'quoting' '
+ rm -f .git/config &&
+ git config quote.leading " test" &&
+ git config quote.ending "test " &&
+ git config quote.semicolon "test;test" &&
+ git config quote.hash "test#test" &&
+ test_cmp expect .git/config
+'
+
+test_expect_success 'key with newline' '
+ test_must_fail git config "key.with
+newline" 123'
+
+test_expect_success 'value with newline' 'git config key.sub value.with\\\
+newline'
+
+cat > .git/config <<\EOF
+[section]
+ ; comment \
+ continued = cont\
+inued
+ noncont = not continued ; \
+ quotecont = "cont;\
+inued"
+EOF
+
+cat > expect <<\EOF
+section.continued=continued
+section.noncont=not continued
+section.quotecont=cont;inued
+EOF
+
+test_expect_success 'value continued on next line' '
+ git config --list > result &&
+ test_cmp expect result
+'
+
+cat > .git/config <<\EOF
+[section "sub=section"]
+ val1 = foo=bar
+ val2 = foo\nbar
+ val3 = \n\n
+ val4 =
+ val5
+EOF
+
+cat > expect <<\EOF
+section.sub=section.val1
+foo=barQsection.sub=section.val2
+foo
+barQsection.sub=section.val3
+
+
+Qsection.sub=section.val4
+Qsection.sub=section.val5Q
+EOF
+test_expect_success '--null --list' '
+ git config --null --list >result.raw &&
+ nul_to_q <result.raw >result &&
+ echo >>result &&
+ test_cmp expect result
+'
+
+test_expect_success '--null --get-regexp' '
+ git config --null --get-regexp "val[0-9]" >result.raw &&
+ nul_to_q <result.raw >result &&
+ echo >>result &&
+ test_cmp expect result
+'
+
+test_expect_success 'inner whitespace kept verbatim' '
+ git config section.val "foo bar" &&
+ test_cmp_config "foo bar" section.val
+'
+
+test_expect_success SYMLINKS 'symlinked configuration' '
+ ln -s notyet myconfig &&
+ git config --file=myconfig test.frotz nitfol &&
+ test -h myconfig &&
+ test -f notyet &&
+ test "z$(git config --file=notyet test.frotz)" = znitfol &&
+ git config --file=myconfig test.xyzzy rezrov &&
+ test -h myconfig &&
+ test -f notyet &&
+ cat >expect <<-\EOF &&
+ nitfol
+ rezrov
+ EOF
+ {
+ git config --file=notyet test.frotz &&
+ git config --file=notyet test.xyzzy
+ } >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success SYMLINKS 'symlink to nonexistent configuration' '
+ ln -s doesnotexist linktonada &&
+ ln -s linktonada linktolinktonada &&
+ test_must_fail git config --file=linktonada --list &&
+ test_must_fail git config --file=linktolinktonada --list
+'
+
+test_expect_success 'check split_cmdline return' "
+ git config alias.split-cmdline-fix 'echo \"' &&
+ test_must_fail git split-cmdline-fix &&
+ echo foo > foo &&
+ git add foo &&
+ git commit -m 'initial commit' &&
+ git config branch.main.mergeoptions 'echo \"' &&
+ test_must_fail git merge main
+"
+
+test_expect_success 'git -c "key=value" support' '
+ cat >expect <<-\EOF &&
+ value
+ value
+ true
+ EOF
+ {
+ git -c section.name=value config section.name &&
+ git -c foo.CamelCase=value config foo.camelcase &&
+ git -c foo.flag config --bool foo.flag
+ } >actual &&
+ test_cmp expect actual &&
+ test_must_fail git -c name=value config section.name
+'
+
+# We just need a type-specifier here that cares about the
+# distinction internally between a NULL boolean and a real
+# string (because most of git's internal parsers do care).
+# Using "--path" works, but we do not otherwise care about
+# its semantics.
+test_expect_success 'git -c can represent empty string' '
+ echo >expect &&
+ git -c foo.empty= config --path foo.empty >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'key sanity-checking' '
+ test_must_fail git config foo=bar &&
+ test_must_fail git config foo=.bar &&
+ test_must_fail git config foo.ba=r &&
+ test_must_fail git config foo.1bar &&
+ test_must_fail git config foo."ba
+ z".bar &&
+ test_must_fail git config . false &&
+ test_must_fail git config .foo false &&
+ test_must_fail git config foo. false &&
+ test_must_fail git config .foo. false &&
+ git config foo.bar true &&
+ git config foo."ba =z".bar false
+'
+
+test_expect_success 'git -c works with aliases of builtins' '
+ git config alias.checkconfig "-c foo.check=bar config foo.check" &&
+ echo bar >expect &&
+ git checkconfig >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'aliases can be CamelCased' '
+ test_config alias.CamelCased "rev-parse HEAD" &&
+ git CamelCased >out &&
+ git rev-parse HEAD >expect &&
+ test_cmp expect out
+'
+
+test_expect_success 'git -c does not split values on equals' '
+ echo "value with = in it" >expect &&
+ git -c section.foo="value with = in it" config section.foo >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git -c dies on bogus config' '
+ test_must_fail git -c core.bare=foo rev-parse
+'
+
+test_expect_success 'git -c complains about empty key' '
+ test_must_fail git -c "=foo" rev-parse
+'
+
+test_expect_success 'git -c complains about empty key and value' '
+ test_must_fail git -c "" rev-parse
+'
+
+test_expect_success 'multiple git -c appends config' '
+ test_config alias.x "!git -c x.two=2 config --get-regexp ^x\.*" &&
+ cat >expect <<-\EOF &&
+ x.one 1
+ x.two 2
+ EOF
+ git -c x.one=1 x >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'last one wins: two level vars' '
+
+ # sec.var and sec.VAR are the same variable, as the first
+ # and the last level of a configuration variable name is
+ # case insensitive.
+
+ echo VAL >expect &&
+
+ git -c sec.var=val -c sec.VAR=VAL config --get sec.var >actual &&
+ test_cmp expect actual &&
+ git -c SEC.var=val -c sec.var=VAL config --get sec.var >actual &&
+ test_cmp expect actual &&
+
+ git -c sec.var=val -c sec.VAR=VAL config --get SEC.var >actual &&
+ test_cmp expect actual &&
+ git -c SEC.var=val -c sec.var=VAL config --get sec.VAR >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'last one wins: three level vars' '
+
+ # v.a.r and v.A.r are not the same variable, as the middle
+ # level of a three-level configuration variable name is
+ # case sensitive.
+
+ echo val >expect &&
+ git -c v.a.r=val -c v.A.r=VAL config --get v.a.r >actual &&
+ test_cmp expect actual &&
+ git -c v.a.r=val -c v.A.r=VAL config --get V.a.R >actual &&
+ test_cmp expect actual &&
+
+ # v.a.r and V.a.R are the same variable, as the first
+ # and the last level of a configuration variable name is
+ # case insensitive.
+
+ echo VAL >expect &&
+ git -c v.a.r=val -c v.a.R=VAL config --get v.a.r >actual &&
+ test_cmp expect actual &&
+ git -c v.a.r=val -c V.a.r=VAL config --get v.a.r >actual &&
+ test_cmp expect actual &&
+ git -c v.a.r=val -c v.a.R=VAL config --get V.a.R >actual &&
+ test_cmp expect actual &&
+ git -c v.a.r=val -c V.a.r=VAL config --get V.a.R >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'old-fashioned settings are case insensitive' '
+ test_when_finished "rm -f testConfig testConfig_expect testConfig_actual" &&
+
+ cat >testConfig_actual <<-EOF &&
+ [V.A]
+ r = value1
+ EOF
+ q_to_tab >testConfig_expect <<-EOF &&
+ [V.A]
+ Qr = value2
+ EOF
+ git config -f testConfig_actual "v.a.r" value2 &&
+ test_cmp testConfig_expect testConfig_actual &&
+
+ cat >testConfig_actual <<-EOF &&
+ [V.A]
+ r = value1
+ EOF
+ q_to_tab >testConfig_expect <<-EOF &&
+ [V.A]
+ QR = value2
+ EOF
+ git config -f testConfig_actual "V.a.R" value2 &&
+ test_cmp testConfig_expect testConfig_actual &&
+
+ cat >testConfig_actual <<-EOF &&
+ [V.A]
+ r = value1
+ EOF
+ q_to_tab >testConfig_expect <<-EOF &&
+ [V.A]
+ r = value1
+ Qr = value2
+ EOF
+ git config -f testConfig_actual "V.A.r" value2 &&
+ test_cmp testConfig_expect testConfig_actual &&
+
+ cat >testConfig_actual <<-EOF &&
+ [V.A]
+ r = value1
+ EOF
+ q_to_tab >testConfig_expect <<-EOF &&
+ [V.A]
+ r = value1
+ Qr = value2
+ EOF
+ git config -f testConfig_actual "v.A.r" value2 &&
+ test_cmp testConfig_expect testConfig_actual
+'
+
+test_expect_success 'setting different case sensitive subsections ' '
+ test_when_finished "rm -f testConfig testConfig_expect testConfig_actual" &&
+
+ cat >testConfig_actual <<-EOF &&
+ [V "A"]
+ R = v1
+ [K "E"]
+ Y = v1
+ [a "b"]
+ c = v1
+ [d "e"]
+ f = v1
+ EOF
+ q_to_tab >testConfig_expect <<-EOF &&
+ [V "A"]
+ Qr = v2
+ [K "E"]
+ Qy = v2
+ [a "b"]
+ Qc = v2
+ [d "e"]
+ f = v1
+ [d "E"]
+ Qf = v2
+ EOF
+ # exact match
+ git config -f testConfig_actual a.b.c v2 &&
+ # match section and subsection, key is cased differently.
+ git config -f testConfig_actual K.E.y v2 &&
+ # section and key are matched case insensitive, but subsection needs
+ # to match; When writing out new values only the key is adjusted
+ git config -f testConfig_actual v.A.r v2 &&
+ # subsection is not matched:
+ git config -f testConfig_actual d.E.f v2 &&
+ test_cmp testConfig_expect testConfig_actual
+'
+
+for VAR in a .a a. a.0b a."b c". a."b c".0d
+do
+ test_expect_success "git -c $VAR=VAL rejects invalid '$VAR'" '
+ test_must_fail git -c "$VAR=VAL" config -l
+ '
+done
+
+for VAR in a.b a."b c".d
+do
+ test_expect_success "git -c $VAR=VAL works with valid '$VAR'" '
+ echo VAL >expect &&
+ git -c "$VAR=VAL" config --get "$VAR" >actual &&
+ test_cmp expect actual
+ '
+done
+
+test_expect_success 'git -c is not confused by empty environment' '
+ GIT_CONFIG_PARAMETERS="" git -c x.one=1 config --list
+'
+
+test_expect_success 'GIT_CONFIG_PARAMETERS handles old-style entries' '
+ v="${SQ}key.one=foo${SQ}" &&
+ v="$v ${SQ}key.two=bar${SQ}" &&
+ v="$v ${SQ}key.ambiguous=section.whatever=value${SQ}" &&
+ GIT_CONFIG_PARAMETERS=$v git config --get-regexp "key.*" >actual &&
+ cat >expect <<-EOF &&
+ key.one foo
+ key.two bar
+ key.ambiguous section.whatever=value
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'GIT_CONFIG_PARAMETERS handles new-style entries' '
+ v="${SQ}key.one${SQ}=${SQ}foo${SQ}" &&
+ v="$v ${SQ}key.two${SQ}=${SQ}bar${SQ}" &&
+ v="$v ${SQ}key.ambiguous=section.whatever${SQ}=${SQ}value${SQ}" &&
+ GIT_CONFIG_PARAMETERS=$v git config --get-regexp "key.*" >actual &&
+ cat >expect <<-EOF &&
+ key.one foo
+ key.two bar
+ key.ambiguous=section.whatever value
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'old and new-style entries can mix' '
+ v="${SQ}key.oldone=oldfoo${SQ}" &&
+ v="$v ${SQ}key.newone${SQ}=${SQ}newfoo${SQ}" &&
+ v="$v ${SQ}key.oldtwo=oldbar${SQ}" &&
+ v="$v ${SQ}key.newtwo${SQ}=${SQ}newbar${SQ}" &&
+ GIT_CONFIG_PARAMETERS=$v git config --get-regexp "key.*" >actual &&
+ cat >expect <<-EOF &&
+ key.oldone oldfoo
+ key.newone newfoo
+ key.oldtwo oldbar
+ key.newtwo newbar
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'old and new bools with ambiguous subsection' '
+ v="${SQ}key.with=equals.oldbool${SQ}" &&
+ v="$v ${SQ}key.with=equals.newbool${SQ}=" &&
+ GIT_CONFIG_PARAMETERS=$v git config --get-regexp "key.*" >actual &&
+ cat >expect <<-EOF &&
+ key.with equals.oldbool
+ key.with=equals.newbool
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'detect bogus GIT_CONFIG_PARAMETERS' '
+ cat >expect <<-\EOF &&
+ env.one one
+ env.two two
+ EOF
+ GIT_CONFIG_PARAMETERS="${SQ}env.one=one${SQ} ${SQ}env.two=two${SQ}" \
+ git config --get-regexp "env.*" >actual &&
+ test_cmp expect actual &&
+
+ cat >expect <<-EOF &&
+ env.one one${SQ}
+ env.two two
+ EOF
+ GIT_CONFIG_PARAMETERS="${SQ}env.one=one${SQ}\\$SQ$SQ$SQ ${SQ}env.two=two${SQ}" \
+ git config --get-regexp "env.*" >actual &&
+ test_cmp expect actual &&
+
+ test_must_fail env \
+ GIT_CONFIG_PARAMETERS="${SQ}env.one=one${SQ}\\$SQ ${SQ}env.two=two${SQ}" \
+ git config --get-regexp "env.*"
+'
+
+test_expect_success 'git --config-env=key=envvar support' '
+ cat >expect <<-\EOF &&
+ value
+ value
+ value
+ value
+ false
+ false
+ EOF
+ {
+ ENVVAR=value git --config-env=core.name=ENVVAR config core.name &&
+ ENVVAR=value git --config-env core.name=ENVVAR config core.name &&
+ ENVVAR=value git --config-env=foo.CamelCase=ENVVAR config foo.camelcase &&
+ ENVVAR=value git --config-env foo.CamelCase=ENVVAR config foo.camelcase &&
+ ENVVAR= git --config-env=foo.flag=ENVVAR config --bool foo.flag &&
+ ENVVAR= git --config-env foo.flag=ENVVAR config --bool foo.flag
+ } >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git --config-env with missing value' '
+ test_must_fail env ENVVAR=value git --config-env 2>error &&
+ grep "no config key given for --config-env" error &&
+ test_must_fail env ENVVAR=value git --config-env config core.name 2>error &&
+ grep "invalid config format: config" error
+'
+
+test_expect_success 'git --config-env fails with invalid parameters' '
+ test_must_fail git --config-env=foo.flag config --bool foo.flag 2>error &&
+ test_grep "invalid config format: foo.flag" error &&
+ test_must_fail git --config-env=foo.flag= config --bool foo.flag 2>error &&
+ test_grep "missing environment variable name for configuration ${SQ}foo.flag${SQ}" error &&
+ sane_unset NONEXISTENT &&
+ test_must_fail git --config-env=foo.flag=NONEXISTENT config --bool foo.flag 2>error &&
+ test_grep "missing environment variable ${SQ}NONEXISTENT${SQ} for configuration ${SQ}foo.flag${SQ}" error
+'
+
+test_expect_success 'git -c and --config-env work together' '
+ cat >expect <<-\EOF &&
+ bar.cmd cmd-value
+ bar.env env-value
+ EOF
+ ENVVAR=env-value git \
+ -c bar.cmd=cmd-value \
+ --config-env=bar.env=ENVVAR \
+ config --get-regexp "^bar.*" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git -c and --config-env override each other' '
+ cat >expect <<-\EOF &&
+ env
+ cmd
+ EOF
+ {
+ ENVVAR=env git -c bar.bar=cmd --config-env=bar.bar=ENVVAR config bar.bar &&
+ ENVVAR=env git --config-env=bar.bar=ENVVAR -c bar.bar=cmd config bar.bar
+ } >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--config-env handles keys with equals' '
+ echo value=with=equals >expect &&
+ ENVVAR=value=with=equals git \
+ --config-env=section.subsection=with=equals.key=ENVVAR \
+ config section.subsection=with=equals.key >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git config handles environment config pairs' '
+ GIT_CONFIG_COUNT=2 \
+ GIT_CONFIG_KEY_0="pair.one" GIT_CONFIG_VALUE_0="foo" \
+ GIT_CONFIG_KEY_1="pair.two" GIT_CONFIG_VALUE_1="bar" \
+ git config --get-regexp "pair.*" >actual &&
+ cat >expect <<-EOF &&
+ pair.one foo
+ pair.two bar
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'git config ignores pairs without count' '
+ test_must_fail env GIT_CONFIG_KEY_0="pair.one" GIT_CONFIG_VALUE_0="value" \
+ git config pair.one 2>error &&
+ test_must_be_empty error
+'
+
+test_expect_success 'git config ignores pairs exceeding count' '
+ GIT_CONFIG_COUNT=1 \
+ GIT_CONFIG_KEY_0="pair.one" GIT_CONFIG_VALUE_0="value" \
+ GIT_CONFIG_KEY_1="pair.two" GIT_CONFIG_VALUE_1="value" \
+ git config --get-regexp "pair.*" >actual 2>error &&
+ cat >expect <<-EOF &&
+ pair.one value
+ EOF
+ test_cmp expect actual &&
+ test_must_be_empty error
+'
+
+test_expect_success 'git config ignores pairs with zero count' '
+ test_must_fail env \
+ GIT_CONFIG_COUNT=0 GIT_CONFIG_KEY_0="pair.one" GIT_CONFIG_VALUE_0="value" \
+ git config pair.one 2>error &&
+ test_must_be_empty error
+'
+
+test_expect_success 'git config ignores pairs with empty count' '
+ test_must_fail env \
+ GIT_CONFIG_COUNT= GIT_CONFIG_KEY_0="pair.one" GIT_CONFIG_VALUE_0="value" \
+ git config pair.one 2>error &&
+ test_must_be_empty error
+'
+
+test_expect_success 'git config fails with invalid count' '
+ test_must_fail env GIT_CONFIG_COUNT=10a git config --list 2>error &&
+ test_grep "bogus count" error &&
+ test_must_fail env GIT_CONFIG_COUNT=9999999999999999 git config --list 2>error &&
+ test_grep "too many entries" error
+'
+
+test_expect_success 'git config fails with missing config key' '
+ test_must_fail env GIT_CONFIG_COUNT=1 GIT_CONFIG_VALUE_0="value" \
+ git config --list 2>error &&
+ test_grep "missing config key" error
+'
+
+test_expect_success 'git config fails with missing config value' '
+ test_must_fail env GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0="pair.one" \
+ git config --list 2>error &&
+ test_grep "missing config value" error
+'
+
+test_expect_success 'git config fails with invalid config pair key' '
+ test_must_fail env GIT_CONFIG_COUNT=1 \
+ GIT_CONFIG_KEY_0= GIT_CONFIG_VALUE_0=value \
+ git config --list &&
+ test_must_fail env GIT_CONFIG_COUNT=1 \
+ GIT_CONFIG_KEY_0=missing-section GIT_CONFIG_VALUE_0=value \
+ git config --list
+'
+
+test_expect_success 'environment overrides config file' '
+ test_when_finished "rm -f .git/config" &&
+ cat >.git/config <<-EOF &&
+ [pair]
+ one = value
+ EOF
+ GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=pair.one GIT_CONFIG_VALUE_0=override \
+ git config pair.one >actual &&
+ cat >expect <<-EOF &&
+ override
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'GIT_CONFIG_PARAMETERS overrides environment config' '
+ GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=pair.one GIT_CONFIG_VALUE_0=value \
+ GIT_CONFIG_PARAMETERS="${SQ}pair.one=override${SQ}" \
+ git config pair.one >actual &&
+ cat >expect <<-EOF &&
+ override
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'command line overrides environment config' '
+ GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=pair.one GIT_CONFIG_VALUE_0=value \
+ git -c pair.one=override config pair.one >actual &&
+ cat >expect <<-EOF &&
+ override
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'git config --edit works' '
+ git config -f tmp test.value no &&
+ echo test.value=yes >expect &&
+ GIT_EDITOR="echo [test]value=yes >" git config -f tmp --edit &&
+ git config -f tmp --list >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git config --edit respects core.editor' '
+ git config -f tmp test.value no &&
+ echo test.value=yes >expect &&
+ test_config core.editor "echo [test]value=yes >" &&
+ git config -f tmp --edit &&
+ git config -f tmp --list >actual &&
+ test_cmp expect actual
+'
+
+# malformed configuration files
+test_expect_success 'barf on syntax error' '
+ cat >.git/config <<-\EOF &&
+ # broken key=value
+ [section]
+ key garbage
+ EOF
+ test_must_fail git config --get section.key 2>error &&
+ test_grep " line 3 " error
+'
+
+test_expect_success 'barf on incomplete section header' '
+ cat >.git/config <<-\EOF &&
+ # broken section line
+ [section
+ key = value
+ EOF
+ test_must_fail git config --get section.key 2>error &&
+ test_grep " line 2 " error
+'
+
+test_expect_success 'barf on incomplete string' '
+ cat >.git/config <<-\EOF &&
+ # broken value string
+ [section]
+ key = "value string
+ EOF
+ test_must_fail git config --get section.key 2>error &&
+ test_grep " line 3 " error
+'
+
+test_expect_success 'urlmatch' '
+ cat >.git/config <<-\EOF &&
+ [http]
+ sslVerify
+ [http "https://weak.example.com"]
+ sslVerify = false
+ cookieFile = /tmp/cookie.txt
+ EOF
+
+ test_expect_code 1 git config --bool --get-urlmatch doesnt.exist https://good.example.com >actual &&
+ test_must_be_empty actual &&
+
+ echo true >expect &&
+ git config --bool --get-urlmatch http.SSLverify https://good.example.com >actual &&
+ test_cmp expect actual &&
+
+ echo false >expect &&
+ git config --bool --get-urlmatch http.sslverify https://weak.example.com >actual &&
+ test_cmp expect actual &&
+
+ {
+ echo http.cookiefile /tmp/cookie.txt &&
+ echo http.sslverify false
+ } >expect &&
+ git config --get-urlmatch HTTP https://weak.example.com >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'urlmatch with --show-scope' '
+ cat >.git/config <<-\EOF &&
+ [http "https://weak.example.com"]
+ sslVerify = false
+ cookieFile = /tmp/cookie.txt
+ EOF
+
+ cat >expect <<-EOF &&
+ local http.cookiefile /tmp/cookie.txt
+ local http.sslverify false
+ EOF
+ git config --get-urlmatch --show-scope HTTP https://weak.example.com >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'urlmatch favors more specific URLs' '
+ cat >.git/config <<-\EOF &&
+ [http "https://example.com/"]
+ cookieFile = /tmp/root.txt
+ [http "https://example.com/subdirectory"]
+ cookieFile = /tmp/subdirectory.txt
+ [http "https://user@example.com/"]
+ cookieFile = /tmp/user.txt
+ [http "https://averylonguser@example.com/"]
+ cookieFile = /tmp/averylonguser.txt
+ [http "https://preceding.example.com"]
+ cookieFile = /tmp/preceding.txt
+ [http "https://*.example.com"]
+ cookieFile = /tmp/wildcard.txt
+ [http "https://*.example.com/wildcardwithsubdomain"]
+ cookieFile = /tmp/wildcardwithsubdomain.txt
+ [http "https://*.example.*"]
+ cookieFile = /tmp/multiwildcard.txt
+ [http "https://trailing.example.com"]
+ cookieFile = /tmp/trailing.txt
+ [http "https://user@*.example.com/"]
+ cookieFile = /tmp/wildcardwithuser.txt
+ [http "https://sub.example.com/"]
+ cookieFile = /tmp/sub.txt
+ EOF
+
+ echo http.cookiefile /tmp/root.txt >expect &&
+ git config --get-urlmatch HTTP https://example.com >actual &&
+ test_cmp expect actual &&
+
+ echo http.cookiefile /tmp/subdirectory.txt >expect &&
+ git config --get-urlmatch HTTP https://example.com/subdirectory >actual &&
+ test_cmp expect actual &&
+
+ echo http.cookiefile /tmp/subdirectory.txt >expect &&
+ git config --get-urlmatch HTTP https://example.com/subdirectory/nested >actual &&
+ test_cmp expect actual &&
+
+ echo http.cookiefile /tmp/user.txt >expect &&
+ git config --get-urlmatch HTTP https://user@example.com/ >actual &&
+ test_cmp expect actual &&
+
+ echo http.cookiefile /tmp/subdirectory.txt >expect &&
+ git config --get-urlmatch HTTP https://averylonguser@example.com/subdirectory >actual &&
+ test_cmp expect actual &&
+
+ echo http.cookiefile /tmp/preceding.txt >expect &&
+ git config --get-urlmatch HTTP https://preceding.example.com >actual &&
+ test_cmp expect actual &&
+
+ echo http.cookiefile /tmp/wildcard.txt >expect &&
+ git config --get-urlmatch HTTP https://wildcard.example.com >actual &&
+ test_cmp expect actual &&
+
+ echo http.cookiefile /tmp/sub.txt >expect &&
+ git config --get-urlmatch HTTP https://sub.example.com/wildcardwithsubdomain >actual &&
+ test_cmp expect actual &&
+
+ echo http.cookiefile /tmp/trailing.txt >expect &&
+ git config --get-urlmatch HTTP https://trailing.example.com >actual &&
+ test_cmp expect actual &&
+
+ echo http.cookiefile /tmp/sub.txt >expect &&
+ git config --get-urlmatch HTTP https://user@sub.example.com >actual &&
+ test_cmp expect actual &&
+
+ echo http.cookiefile /tmp/multiwildcard.txt >expect &&
+ git config --get-urlmatch HTTP https://wildcard.example.org >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'urlmatch with wildcard' '
+ cat >.git/config <<-\EOF &&
+ [http]
+ sslVerify
+ [http "https://*.example.com"]
+ sslVerify = false
+ cookieFile = /tmp/cookie.txt
+ EOF
+
+ test_expect_code 1 git config --bool --get-urlmatch doesnt.exist https://good.example.com >actual &&
+ test_must_be_empty actual &&
+
+ echo true >expect &&
+ git config --bool --get-urlmatch http.SSLverify https://example.com >actual &&
+ test_cmp expect actual &&
+
+ echo true >expect &&
+ git config --bool --get-urlmatch http.SSLverify https://good-example.com >actual &&
+ test_cmp expect actual &&
+
+ echo true >expect &&
+ git config --bool --get-urlmatch http.sslverify https://deep.nested.example.com >actual &&
+ test_cmp expect actual &&
+
+ echo false >expect &&
+ git config --bool --get-urlmatch http.sslverify https://good.example.com >actual &&
+ test_cmp expect actual &&
+
+ {
+ echo http.cookiefile /tmp/cookie.txt &&
+ echo http.sslverify false
+ } >expect &&
+ git config --get-urlmatch HTTP https://good.example.com >actual &&
+ test_cmp expect actual &&
+
+ echo http.sslverify >expect &&
+ git config --get-urlmatch HTTP https://more.example.com.au >actual &&
+ test_cmp expect actual
+'
+
+# good section hygiene
+test_expect_success '--unset last key removes section (except if commented)' '
+ cat >.git/config <<-\EOF &&
+ # some generic comment on the configuration file itself
+ # a comment specific to this "section" section.
+ [section]
+ # some intervening lines
+ # that should also be dropped
+
+ key = value
+ # please be careful when you update the above variable
+ EOF
+
+ cat >expect <<-\EOF &&
+ # some generic comment on the configuration file itself
+ # a comment specific to this "section" section.
+ [section]
+ # some intervening lines
+ # that should also be dropped
+
+ # please be careful when you update the above variable
+ EOF
+
+ git config --unset section.key &&
+ test_cmp expect .git/config &&
+
+ cat >.git/config <<-\EOF &&
+ [section]
+ key = value
+ [next-section]
+ EOF
+
+ cat >expect <<-\EOF &&
+ [next-section]
+ EOF
+
+ git config --unset section.key &&
+ test_cmp expect .git/config &&
+
+ q_to_tab >.git/config <<-\EOF &&
+ [one]
+ Qkey = "multiline \
+ QQ# with comment"
+ [two]
+ key = true
+ EOF
+ git config --unset two.key &&
+ ! grep two .git/config &&
+
+ q_to_tab >.git/config <<-\EOF &&
+ [one]
+ Qkey = "multiline \
+ QQ# with comment"
+ [one]
+ key = true
+ EOF
+ git config --unset-all one.key &&
+ test_line_count = 0 .git/config &&
+
+ q_to_tab >.git/config <<-\EOF &&
+ [one]
+ Qkey = true
+ Q# a comment not at the start
+ [two]
+ Qkey = true
+ EOF
+ git config --unset two.key &&
+ grep two .git/config &&
+
+ q_to_tab >.git/config <<-\EOF &&
+ [one]
+ Qkey = not [two "subsection"]
+ [two "subsection"]
+ [two "subsection"]
+ Qkey = true
+ [TWO "subsection"]
+ [one]
+ EOF
+ git config --unset two.subsection.key &&
+ test "not [two subsection]" = "$(git config one.key)" &&
+ test_line_count = 3 .git/config
+'
+
+test_expect_success '--unset-all removes section if empty & uncommented' '
+ cat >.git/config <<-\EOF &&
+ [section]
+ key = value1
+ key = value2
+ EOF
+
+ git config --unset-all section.key &&
+ test_line_count = 0 .git/config
+'
+
+test_expect_success 'adding a key into an empty section reuses header' '
+ cat >.git/config <<-\EOF &&
+ [section]
+ EOF
+
+ q_to_tab >expect <<-\EOF &&
+ [section]
+ Qkey = value
+ EOF
+
+ git config section.key value &&
+ test_cmp expect .git/config
+'
+
+test_expect_success POSIXPERM,PERL 'preserves existing permissions' '
+ chmod 0600 .git/config &&
+ git config imap.pass Hunter2 &&
+ perl -e \
+ "die q(badset) if ((stat(q(.git/config)))[2] & 07777) != 0600" &&
+ git config --rename-section imap pop &&
+ perl -e \
+ "die q(badrename) if ((stat(q(.git/config)))[2] & 07777) != 0600"
+'
+
+! test_have_prereq MINGW ||
+HOME="$(pwd)" # convert to Windows path
+
+test_expect_success 'set up --show-origin tests' '
+ INCLUDE_DIR="$HOME/include" &&
+ mkdir -p "$INCLUDE_DIR" &&
+ cat >"$INCLUDE_DIR"/absolute.include <<-\EOF &&
+ [user]
+ absolute = include
+ EOF
+ cat >"$INCLUDE_DIR"/relative.include <<-\EOF &&
+ [user]
+ relative = include
+ EOF
+ cat >"$HOME"/.gitconfig <<-EOF &&
+ [user]
+ global = true
+ override = global
+ [include]
+ path = "$INCLUDE_DIR/absolute.include"
+ EOF
+ cat >.git/config <<-\EOF
+ [user]
+ local = true
+ override = local
+ [include]
+ path = ../include/relative.include
+ EOF
+'
+
+test_expect_success '--show-origin with --list' '
+ cat >expect <<-EOF &&
+ file:$HOME/.gitconfig user.global=true
+ file:$HOME/.gitconfig user.override=global
+ file:$HOME/.gitconfig include.path=$INCLUDE_DIR/absolute.include
+ file:$INCLUDE_DIR/absolute.include user.absolute=include
+ file:.git/config user.local=true
+ file:.git/config user.override=local
+ file:.git/config include.path=../include/relative.include
+ file:.git/../include/relative.include user.relative=include
+ command line: user.environ=true
+ command line: user.cmdline=true
+ EOF
+ GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=user.environ GIT_CONFIG_VALUE_0=true\
+ git -c user.cmdline=true config --list --show-origin >output &&
+ test_cmp expect output
+'
+
+test_expect_success '--show-origin with --list --null' '
+ cat >expect <<-EOF &&
+ file:$HOME/.gitconfigQuser.global
+ trueQfile:$HOME/.gitconfigQuser.override
+ globalQfile:$HOME/.gitconfigQinclude.path
+ $INCLUDE_DIR/absolute.includeQfile:$INCLUDE_DIR/absolute.includeQuser.absolute
+ includeQfile:.git/configQuser.local
+ trueQfile:.git/configQuser.override
+ localQfile:.git/configQinclude.path
+ ../include/relative.includeQfile:.git/../include/relative.includeQuser.relative
+ includeQcommand line:Quser.cmdline
+ trueQ
+ EOF
+ git -c user.cmdline=true config --null --list --show-origin >output.raw &&
+ nul_to_q <output.raw >output &&
+ # The here-doc above adds a newline that the --null output would not
+ # include. Add it here to make the two comparable.
+ echo >>output &&
+ test_cmp expect output
+'
+
+test_expect_success '--show-origin with single file' '
+ cat >expect <<-\EOF &&
+ file:.git/config user.local=true
+ file:.git/config user.override=local
+ file:.git/config include.path=../include/relative.include
+ EOF
+ git config --local --list --show-origin >output &&
+ test_cmp expect output
+'
+
+test_expect_success '--show-origin with --get-regexp' '
+ cat >expect <<-EOF &&
+ file:$HOME/.gitconfig user.global true
+ file:.git/config user.local true
+ EOF
+ git config --show-origin --get-regexp "user\.[g|l].*" >output &&
+ test_cmp expect output
+'
+
+test_expect_success '--show-origin getting a single key' '
+ cat >expect <<-\EOF &&
+ file:.git/config local
+ EOF
+ git config --show-origin user.override >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'set up custom config file' '
+ CUSTOM_CONFIG_FILE="custom.conf" &&
+ cat >"$CUSTOM_CONFIG_FILE" <<-\EOF
+ [user]
+ custom = true
+ EOF
+'
+
+test_expect_success !MINGW 'set up custom config file with special name characters' '
+ WEIRDLY_NAMED_FILE="file\" (dq) and spaces.conf" &&
+ cp "$CUSTOM_CONFIG_FILE" "$WEIRDLY_NAMED_FILE"
+'
+
+test_expect_success !MINGW '--show-origin escape special file name characters' '
+ cat >expect <<-\EOF &&
+ file:"file\" (dq) and spaces.conf" user.custom=true
+ EOF
+ git config --file "$WEIRDLY_NAMED_FILE" --show-origin --list >output &&
+ test_cmp expect output
+'
+
+test_expect_success '--show-origin stdin' '
+ cat >expect <<-\EOF &&
+ standard input: user.custom=true
+ EOF
+ git config --file - --show-origin --list <"$CUSTOM_CONFIG_FILE" >output &&
+ test_cmp expect output
+'
+
+test_expect_success '--show-origin stdin with file include' '
+ cat >"$INCLUDE_DIR"/stdin.include <<-EOF &&
+ [user]
+ stdin = include
+ EOF
+ cat >expect <<-EOF &&
+ file:$INCLUDE_DIR/stdin.include include
+ EOF
+ echo "[include]path=\"$INCLUDE_DIR\"/stdin.include" |
+ git config --show-origin --includes --file - user.stdin >output &&
+
+ test_cmp expect output
+'
+
+test_expect_success '--show-origin blob' '
+ blob=$(git hash-object -w "$CUSTOM_CONFIG_FILE") &&
+ cat >expect <<-EOF &&
+ blob:$blob user.custom=true
+ EOF
+ git config --blob=$blob --show-origin --list >output &&
+ test_cmp expect output
+'
+
+test_expect_success '--show-origin blob ref' '
+ cat >expect <<-\EOF &&
+ blob:main:custom.conf user.custom=true
+ EOF
+ git add "$CUSTOM_CONFIG_FILE" &&
+ git commit -m "new config file" &&
+ git config --blob=main:"$CUSTOM_CONFIG_FILE" --show-origin --list >output &&
+ test_cmp expect output
+'
+
+test_expect_success '--show-origin with --default' '
+ git config --show-origin --default foo some.key >actual &&
+ echo "command line: foo" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success '--show-scope with --list' '
+ cat >expect <<-EOF &&
+ global user.global=true
+ global user.override=global
+ global include.path=$INCLUDE_DIR/absolute.include
+ global user.absolute=include
+ local user.local=true
+ local user.override=local
+ local include.path=../include/relative.include
+ local user.relative=include
+ local core.repositoryformatversion=1
+ local extensions.worktreeconfig=true
+ worktree user.worktree=true
+ command user.cmdline=true
+ EOF
+ git worktree add wt1 &&
+ # We need these to test for worktree scope, but outside of this
+ # test, this is just noise
+ test_config core.repositoryformatversion 1 &&
+ test_config extensions.worktreeConfig true &&
+ git config --worktree user.worktree true &&
+ git -c user.cmdline=true config --list --show-scope >output &&
+ test_cmp expect output
+'
+
+test_expect_success !MINGW '--show-scope with --blob' '
+ blob=$(git hash-object -w "$CUSTOM_CONFIG_FILE") &&
+ cat >expect <<-EOF &&
+ command user.custom=true
+ EOF
+ git config --blob=$blob --show-scope --list >output &&
+ test_cmp expect output
+'
+
+test_expect_success '--show-scope with --local' '
+ cat >expect <<-\EOF &&
+ local user.local=true
+ local user.override=local
+ local include.path=../include/relative.include
+ EOF
+ git config --local --list --show-scope >output &&
+ test_cmp expect output
+'
+
+test_expect_success '--show-scope getting a single value' '
+ cat >expect <<-\EOF &&
+ local true
+ EOF
+ git config --show-scope --get user.local >output &&
+ test_cmp expect output
+'
+
+test_expect_success '--show-scope with --show-origin' '
+ cat >expect <<-EOF &&
+ global file:$HOME/.gitconfig user.global=true
+ global file:$HOME/.gitconfig user.override=global
+ global file:$HOME/.gitconfig include.path=$INCLUDE_DIR/absolute.include
+ global file:$INCLUDE_DIR/absolute.include user.absolute=include
+ local file:.git/config user.local=true
+ local file:.git/config user.override=local
+ local file:.git/config include.path=../include/relative.include
+ local file:.git/../include/relative.include user.relative=include
+ command command line: user.cmdline=true
+ EOF
+ git -c user.cmdline=true config --list --show-origin --show-scope >output &&
+ test_cmp expect output
+'
+
+test_expect_success '--show-scope with --default' '
+ git config --show-scope --default foo some.key >actual &&
+ echo "command foo" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'override global and system config' '
+ test_when_finished rm -f \"\$HOME\"/.gitconfig &&
+ cat >"$HOME"/.gitconfig <<-EOF &&
+ [home]
+ config = true
+ EOF
+
+ test_when_finished rm -rf \"\$HOME\"/.config/git &&
+ mkdir -p "$HOME"/.config/git &&
+ cat >"$HOME"/.config/git/config <<-EOF &&
+ [xdg]
+ config = true
+ EOF
+ cat >.git/config <<-EOF &&
+ [local]
+ config = true
+ EOF
+ cat >custom-global-config <<-EOF &&
+ [global]
+ config = true
+ EOF
+ cat >custom-system-config <<-EOF &&
+ [system]
+ config = true
+ EOF
+
+ cat >expect <<-EOF &&
+ global xdg.config=true
+ global home.config=true
+ local local.config=true
+ EOF
+ git config --show-scope --list >output &&
+ test_cmp expect output &&
+
+ cat >expect <<-EOF &&
+ system system.config=true
+ global global.config=true
+ local local.config=true
+ EOF
+ GIT_CONFIG_NOSYSTEM=false GIT_CONFIG_SYSTEM=custom-system-config GIT_CONFIG_GLOBAL=custom-global-config \
+ git config --show-scope --list >output &&
+ test_cmp expect output &&
+
+ cat >expect <<-EOF &&
+ local local.config=true
+ EOF
+ GIT_CONFIG_NOSYSTEM=false GIT_CONFIG_SYSTEM=/dev/null GIT_CONFIG_GLOBAL=/dev/null \
+ git config --show-scope --list >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'override global and system config with missing file' '
+ test_must_fail env GIT_CONFIG_GLOBAL=does-not-exist GIT_CONFIG_SYSTEM=/dev/null git config --global --list &&
+ test_must_fail env GIT_CONFIG_GLOBAL=/dev/null GIT_CONFIG_SYSTEM=does-not-exist git config --system --list &&
+ GIT_CONFIG_GLOBAL=does-not-exist GIT_CONFIG_SYSTEM=does-not-exist git version
+'
+
+test_expect_success 'system override has no effect with GIT_CONFIG_NOSYSTEM' '
+ # `git config --system` has different semantics compared to other
+ # commands as it ignores GIT_CONFIG_NOSYSTEM. We thus test whether the
+ # variable has an effect via a different proxy.
+ cat >alias-config <<-EOF &&
+ [alias]
+ hello-world = !echo "hello world"
+ EOF
+ test_must_fail env GIT_CONFIG_NOSYSTEM=true GIT_CONFIG_SYSTEM=alias-config \
+ git hello-world &&
+ GIT_CONFIG_NOSYSTEM=false GIT_CONFIG_SYSTEM=alias-config \
+ git hello-world >actual &&
+ echo "hello world" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'write to overridden global and system config' '
+ cat >expect <<EOF &&
+[config]
+ key = value
+EOF
+
+ GIT_CONFIG_GLOBAL=write-to-global git config --global config.key value &&
+ test_cmp expect write-to-global &&
+
+ GIT_CONFIG_SYSTEM=write-to-system git config --system config.key value &&
+ test_cmp expect write-to-system
+'
+
+for opt in --local --worktree
+do
+ test_expect_success "$opt requires a repo" '
+ # we expect 128 to ensure that we do not simply
+ # fail to find anything and return code "1"
+ test_expect_code 128 nongit git config $opt foo.bar
+ '
+done
+
+cat >.git/config <<-\EOF &&
+[section]
+foo = true
+number = 10
+big = 1M
+EOF
+
+test_expect_success 'identical modern --type specifiers are allowed' '
+ test_cmp_config 1048576 --type=int --type=int section.big
+'
+
+test_expect_success 'identical legacy --type specifiers are allowed' '
+ test_cmp_config 1048576 --int --int section.big
+'
+
+test_expect_success 'identical mixed --type specifiers are allowed' '
+ test_cmp_config 1048576 --int --type=int section.big
+'
+
+test_expect_success 'non-identical modern --type specifiers are not allowed' '
+ test_must_fail git config --type=int --type=bool section.big 2>error &&
+ test_grep "only one type at a time" error
+'
+
+test_expect_success 'non-identical legacy --type specifiers are not allowed' '
+ test_must_fail git config --int --bool section.big 2>error &&
+ test_grep "only one type at a time" error
+'
+
+test_expect_success 'non-identical mixed --type specifiers are not allowed' '
+ test_must_fail git config --type=int --bool section.big 2>error &&
+ test_grep "only one type at a time" error
+'
+
+test_expect_success '--type allows valid type specifiers' '
+ test_cmp_config true --type=bool section.foo
+'
+
+test_expect_success '--no-type unsets type specifiers' '
+ test_cmp_config 10 --type=bool --no-type section.number
+'
+
+test_expect_success 'unset type specifiers may be reset to conflicting ones' '
+ test_cmp_config 1048576 --type=bool --no-type --type=int section.big
+'
+
+test_expect_success '--type rejects unknown specifiers' '
+ test_must_fail git config --type=nonsense section.foo 2>error &&
+ test_grep "unrecognized --type argument" error
+'
+
+test_expect_success '--type=int requires at least one digit' '
+ test_must_fail git config --type int --default m some.key >out 2>error &&
+ grep "bad numeric config value" error &&
+ test_must_be_empty out
+'
+
+test_expect_success '--replace-all does not invent newlines' '
+ q_to_tab >.git/config <<-\EOF &&
+ [abc]key
+ QkeepSection
+ [xyz]
+ Qkey = 1
+ [abc]
+ Qkey = a
+ EOF
+ q_to_tab >expect <<-\EOF &&
+ [abc]
+ QkeepSection
+ [xyz]
+ Qkey = 1
+ [abc]
+ Qkey = b
+ EOF
+ git config --replace-all abc.key b &&
+ test_cmp expect .git/config
+'
+
+test_expect_success 'set all config with value-pattern' '
+ test_when_finished rm -f config initial &&
+ git config --file=initial abc.key one &&
+
+ # no match => add new entry
+ cp initial config &&
+ git config --file=config abc.key two a+ &&
+ git config --file=config --list >actual &&
+ cat >expect <<-\EOF &&
+ abc.key=one
+ abc.key=two
+ EOF
+ test_cmp expect actual &&
+
+ # multiple matches => failure
+ test_must_fail git config --file=config abc.key three o+ 2>err &&
+ test_grep "has multiple values" err &&
+
+ # multiple values, no match => add
+ git config --file=config abc.key three a+ &&
+ git config --file=config --list >actual &&
+ cat >expect <<-\EOF &&
+ abc.key=one
+ abc.key=two
+ abc.key=three
+ EOF
+ test_cmp expect actual &&
+
+ # single match => replace
+ git config --file=config abc.key four h+ &&
+ git config --file=config --list >actual &&
+ cat >expect <<-\EOF &&
+ abc.key=one
+ abc.key=two
+ abc.key=four
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success '--replace-all and value-pattern' '
+ test_when_finished rm -f config &&
+ git config --file=config --add abc.key one &&
+ git config --file=config --add abc.key two &&
+ git config --file=config --add abc.key three &&
+ git config --file=config --replace-all abc.key four "o+" &&
+ git config --file=config --list >actual &&
+ cat >expect <<-\EOF &&
+ abc.key=four
+ abc.key=three
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'refuse --fixed-value for incompatible actions' '
+ test_when_finished rm -f config &&
+ git config --file=config dev.null bogus &&
+
+ # These modes do not allow --fixed-value at all
+ test_must_fail git config --file=config --fixed-value --add dev.null bogus &&
+ test_must_fail git config --file=config --fixed-value --get-urlmatch dev.null bogus &&
+ test_must_fail git config --file=config --fixed-value --get-urlmatch dev.null bogus &&
+ test_must_fail git config --file=config --fixed-value --rename-section dev null &&
+ test_must_fail git config --file=config --fixed-value --remove-section dev &&
+ test_must_fail git config --file=config --fixed-value --list &&
+ test_must_fail git config --file=config --fixed-value --get-color dev.null &&
+ test_must_fail git config --file=config --fixed-value --get-colorbool dev.null &&
+
+ # These modes complain when --fixed-value has no value-pattern
+ test_must_fail git config --file=config --fixed-value dev.null bogus &&
+ test_must_fail git config --file=config --fixed-value --replace-all dev.null bogus &&
+ test_must_fail git config --file=config --fixed-value --get dev.null &&
+ test_must_fail git config --file=config --fixed-value --get-all dev.null &&
+ test_must_fail git config --file=config --fixed-value --get-regexp "dev.*" &&
+ test_must_fail git config --file=config --fixed-value --unset dev.null &&
+ test_must_fail git config --file=config --fixed-value --unset-all dev.null
+'
+
+test_expect_success '--fixed-value uses exact string matching' '
+ test_when_finished rm -f config initial &&
+ META="a+b*c?d[e]f.g" &&
+ git config --file=initial fixed.test "$META" &&
+
+ cp initial config &&
+ git config --file=config fixed.test bogus "$META" &&
+ git config --file=config --list >actual &&
+ cat >expect <<-EOF &&
+ fixed.test=$META
+ fixed.test=bogus
+ EOF
+ test_cmp expect actual &&
+
+ cp initial config &&
+ git config --file=config --fixed-value fixed.test bogus "$META" &&
+ git config --file=config --list >actual &&
+ cat >expect <<-\EOF &&
+ fixed.test=bogus
+ EOF
+ test_cmp expect actual &&
+
+ cp initial config &&
+ test_must_fail git config --file=config --unset fixed.test "$META" &&
+ git config --file=config --fixed-value --unset fixed.test "$META" &&
+ test_must_fail git config --file=config fixed.test &&
+
+ cp initial config &&
+ test_must_fail git config --file=config --unset-all fixed.test "$META" &&
+ git config --file=config --fixed-value --unset-all fixed.test "$META" &&
+ test_must_fail git config --file=config fixed.test &&
+
+ cp initial config &&
+ git config --file=config --replace-all fixed.test bogus "$META" &&
+ git config --file=config --list >actual &&
+ cat >expect <<-EOF &&
+ fixed.test=$META
+ fixed.test=bogus
+ EOF
+ test_cmp expect actual &&
+
+ git config --file=config --fixed-value --replace-all fixed.test bogus "$META" &&
+ git config --file=config --list >actual &&
+ cat >expect <<-EOF &&
+ fixed.test=bogus
+ fixed.test=bogus
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success '--get and --get-all with --fixed-value' '
+ test_when_finished rm -f config &&
+ META="a+b*c?d[e]f.g" &&
+ git config --file=config fixed.test bogus &&
+ git config --file=config --add fixed.test "$META" &&
+
+ git config --file=config --get fixed.test bogus &&
+ test_must_fail git config --file=config --get fixed.test "$META" &&
+ git config --file=config --get --fixed-value fixed.test "$META" &&
+ test_must_fail git config --file=config --get --fixed-value fixed.test non-existent &&
+
+ git config --file=config --get-all fixed.test bogus &&
+ test_must_fail git config --file=config --get-all fixed.test "$META" &&
+ git config --file=config --get-all --fixed-value fixed.test "$META" &&
+ test_must_fail git config --file=config --get-all --fixed-value fixed.test non-existent &&
+
+ git config --file=config --get-regexp fixed+ bogus &&
+ test_must_fail git config --file=config --get-regexp fixed+ "$META" &&
+ git config --file=config --get-regexp --fixed-value fixed+ "$META" &&
+ test_must_fail git config --file=config --get-regexp --fixed-value fixed+ non-existent
+'
+
+test_expect_success 'includeIf.hasconfig:remote.*.url' '
+ git init hasremoteurlTest &&
+ test_when_finished "rm -rf hasremoteurlTest" &&
+
+ cat >include-this <<-\EOF &&
+ [user]
+ this = this-is-included
+ EOF
+ cat >dont-include-that <<-\EOF &&
+ [user]
+ that = that-is-not-included
+ EOF
+ cat >>hasremoteurlTest/.git/config <<-EOF &&
+ [includeIf "hasconfig:remote.*.url:foourl"]
+ path = "$(pwd)/include-this"
+ [includeIf "hasconfig:remote.*.url:barurl"]
+ path = "$(pwd)/dont-include-that"
+ [remote "foo"]
+ url = foourl
+ EOF
+
+ echo this-is-included >expect-this &&
+ git -C hasremoteurlTest config --get user.this >actual-this &&
+ test_cmp expect-this actual-this &&
+
+ test_must_fail git -C hasremoteurlTest config --get user.that
+'
+
+test_expect_success 'includeIf.hasconfig:remote.*.url respects last-config-wins' '
+ git init hasremoteurlTest &&
+ test_when_finished "rm -rf hasremoteurlTest" &&
+
+ cat >include-two-three <<-\EOF &&
+ [user]
+ two = included-config
+ three = included-config
+ EOF
+ cat >>hasremoteurlTest/.git/config <<-EOF &&
+ [remote "foo"]
+ url = foourl
+ [user]
+ one = main-config
+ two = main-config
+ [includeIf "hasconfig:remote.*.url:foourl"]
+ path = "$(pwd)/include-two-three"
+ [user]
+ three = main-config
+ EOF
+
+ echo main-config >expect-main-config &&
+ echo included-config >expect-included-config &&
+
+ git -C hasremoteurlTest config --get user.one >actual &&
+ test_cmp expect-main-config actual &&
+
+ git -C hasremoteurlTest config --get user.two >actual &&
+ test_cmp expect-included-config actual &&
+
+ git -C hasremoteurlTest config --get user.three >actual &&
+ test_cmp expect-main-config actual
+'
+
+test_expect_success 'includeIf.hasconfig:remote.*.url globs' '
+ git init hasremoteurlTest &&
+ test_when_finished "rm -rf hasremoteurlTest" &&
+
+ printf "[user]\ndss = yes\n" >double-star-start &&
+ printf "[user]\ndse = yes\n" >double-star-end &&
+ printf "[user]\ndsm = yes\n" >double-star-middle &&
+ printf "[user]\nssm = yes\n" >single-star-middle &&
+ printf "[user]\nno = no\n" >no &&
+
+ cat >>hasremoteurlTest/.git/config <<-EOF &&
+ [remote "foo"]
+ url = https://foo/bar/baz
+ [includeIf "hasconfig:remote.*.url:**/baz"]
+ path = "$(pwd)/double-star-start"
+ [includeIf "hasconfig:remote.*.url:**/nomatch"]
+ path = "$(pwd)/no"
+ [includeIf "hasconfig:remote.*.url:https:/**"]
+ path = "$(pwd)/double-star-end"
+ [includeIf "hasconfig:remote.*.url:nomatch:/**"]
+ path = "$(pwd)/no"
+ [includeIf "hasconfig:remote.*.url:https:/**/baz"]
+ path = "$(pwd)/double-star-middle"
+ [includeIf "hasconfig:remote.*.url:https:/**/nomatch"]
+ path = "$(pwd)/no"
+ [includeIf "hasconfig:remote.*.url:https://*/bar/baz"]
+ path = "$(pwd)/single-star-middle"
+ [includeIf "hasconfig:remote.*.url:https://*/baz"]
+ path = "$(pwd)/no"
+ EOF
+
+ git -C hasremoteurlTest config --get user.dss &&
+ git -C hasremoteurlTest config --get user.dse &&
+ git -C hasremoteurlTest config --get user.dsm &&
+ git -C hasremoteurlTest config --get user.ssm &&
+ test_must_fail git -C hasremoteurlTest config --get user.no
+'
+
+test_expect_success 'includeIf.hasconfig:remote.*.url forbids remote url in such included files' '
+ git init hasremoteurlTest &&
+ test_when_finished "rm -rf hasremoteurlTest" &&
+
+ cat >include-with-url <<-\EOF &&
+ [remote "bar"]
+ url = barurl
+ EOF
+ cat >>hasremoteurlTest/.git/config <<-EOF &&
+ [includeIf "hasconfig:remote.*.url:foourl"]
+ path = "$(pwd)/include-with-url"
+ EOF
+
+ # test with any Git command
+ test_must_fail git -C hasremoteurlTest status 2>err &&
+ grep "fatal: remote URLs cannot be configured in file directly or indirectly included by includeIf.hasconfig:remote.*.url" err
+'
+
+test_done
diff --git a/t/t1301-shared-repo.sh b/t/t1301-shared-repo.sh
new file mode 100755
index 0000000..e5a0d65
--- /dev/null
+++ b/t/t1301-shared-repo.sh
@@ -0,0 +1,241 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Johannes Schindelin
+#
+
+test_description='Test shared repository initialization'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_CREATE_REPO_NO_TEMPLATE=1
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# Remove a default ACL from the test dir if possible.
+setfacl -k . 2>/dev/null
+
+# User must have read permissions to the repo -> failure on --shared=0400
+test_expect_success 'shared = 0400 (faulty permission u-w)' '
+ test_when_finished "rm -rf sub" &&
+ mkdir sub && (
+ cd sub &&
+ test_must_fail git init --shared=0400
+ )
+'
+
+for u in 002 022
+do
+ test_expect_success POSIXPERM "shared=1 does not clear bits preset by umask $u" '
+ test_when_finished "rm -rf sub" &&
+ mkdir sub && (
+ cd sub &&
+ umask $u &&
+ git init --shared=1 &&
+ test 1 = "$(git config core.sharedrepository)"
+ ) &&
+ actual=$(ls -l sub/.git/HEAD) &&
+ case "$actual" in
+ -rw-rw-r--*)
+ : happy
+ ;;
+ *)
+ echo Oops, .git/HEAD is not 0664 but $actual
+ false
+ ;;
+ esac
+ '
+done
+
+test_expect_success 'shared=all' '
+ git init --template= --shared=all &&
+ test 2 = $(git config core.sharedrepository)
+'
+
+test_expect_failure 'template can set core.bare' '
+ test_when_finished "rm -rf subdir" &&
+ test_when_finished "rm -rf templates" &&
+ test_config core.bare true &&
+ umask 0022 &&
+ mkdir -p templates/ &&
+ cp .git/config templates/config &&
+ git init --template=templates subdir &&
+ test_path_exists subdir/HEAD
+'
+
+test_expect_success 'template can set core.bare but overridden by command line' '
+ test_when_finished "rm -rf subdir" &&
+ test_when_finished "rm -rf templates" &&
+ test_config core.bare true &&
+ umask 0022 &&
+ mkdir -p templates/ &&
+ cp .git/config templates/config &&
+ git init --no-bare --template=templates subdir &&
+ test_path_exists subdir/.git/HEAD
+'
+
+test_expect_success POSIXPERM 'update-server-info honors core.sharedRepository' '
+ : > a1 &&
+ git add a1 &&
+ test_tick &&
+ git commit -m a1 &&
+ mkdir .git/info &&
+ umask 0277 &&
+ git update-server-info &&
+ actual="$(ls -l .git/info/refs)" &&
+ case "$actual" in
+ -r--r--r--*)
+ : happy
+ ;;
+ *)
+ echo Oops, .git/info/refs is not 0444
+ false
+ ;;
+ esac
+'
+
+for u in 0660:rw-rw---- \
+ 0640:rw-r----- \
+ 0600:rw------- \
+ 0666:rw-rw-rw- \
+ 0664:rw-rw-r--
+do
+ x=$(expr "$u" : ".*:\([rw-]*\)") &&
+ y=$(echo "$x" | sed -e "s/w/-/g") &&
+ u=$(expr "$u" : "\([0-7]*\)") &&
+ git config core.sharedrepository "$u" &&
+ umask 0277 &&
+
+ test_expect_success POSIXPERM "shared = $u ($y) ro" '
+
+ rm -f .git/info/refs &&
+ git update-server-info &&
+ actual="$(test_modebits .git/info/refs)" &&
+ test "x$actual" = "x-$y"
+
+ '
+
+ umask 077 &&
+ test_expect_success POSIXPERM "shared = $u ($x) rw" '
+
+ rm -f .git/info/refs &&
+ git update-server-info &&
+ actual="$(test_modebits .git/info/refs)" &&
+ test "x$actual" = "x-$x"
+
+ '
+
+done
+
+test_expect_success POSIXPERM 'info/refs respects umask in unshared repo' '
+ rm -f .git/info/refs &&
+ test_unconfig core.sharedrepository &&
+ umask 002 &&
+ git update-server-info &&
+ echo "-rw-rw-r--" >expect &&
+ test_modebits .git/info/refs >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success POSIXPERM 'git reflog expire honors core.sharedRepository' '
+ umask 077 &&
+ git config core.sharedRepository group &&
+ git reflog expire --all &&
+ actual="$(ls -l .git/logs/refs/heads/main)" &&
+ case "$actual" in
+ -rw-rw-*)
+ : happy
+ ;;
+ *)
+ echo Ooops, .git/logs/refs/heads/main is not 066x [$actual]
+ false
+ ;;
+ esac
+'
+
+test_expect_success POSIXPERM 'forced modes' '
+ test_when_finished "rm -rf new" &&
+ mkdir -p templates/hooks &&
+ echo update-server-info >templates/hooks/post-update &&
+ chmod +x templates/hooks/post-update &&
+ echo : >random-file &&
+ mkdir new &&
+ (
+ cd new &&
+ umask 002 &&
+ git init --shared=0660 --template=../templates &&
+ test_path_is_file .git/hooks/post-update &&
+ >frotz &&
+ git add frotz &&
+ git commit -a -m initial &&
+ git repack
+ ) &&
+ # List repository files meant to be protected; note that
+ # COMMIT_EDITMSG does not matter---0mode is not about a
+ # repository with a work tree.
+ find new/.git -type f -name COMMIT_EDITMSG -prune -o -print |
+ xargs ls -ld >actual &&
+
+ # Everything must be unaccessible to others
+ test -z "$(sed -e "/^.......---/d" actual)" &&
+
+ # All directories must have either 2770 or 770
+ test -z "$(sed -n -e "/^drwxrw[sx]---/d" -e "/^d/p" actual)" &&
+
+ # post-update hook must be 0770
+ test -z "$(sed -n -e "/post-update/{
+ /^-rwxrwx---/d
+ p
+ }" actual)" &&
+
+ # All files inside objects must be accessible by us
+ test -z "$(sed -n -e "/objects\//{
+ /^d/d
+ /^-r.-r.----/d
+ p
+ }" actual)"
+'
+
+test_expect_success POSIXPERM 'remote init does not use config from cwd' '
+ test_when_finished "rm -rf child.git" &&
+ git config core.sharedrepository 0666 &&
+ umask 0022 &&
+ git init --bare child.git &&
+ echo "-rw-r--r--" >expect &&
+ test_modebits child.git/config >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success POSIXPERM 're-init respects core.sharedrepository (local)' '
+ git config core.sharedrepository 0666 &&
+ umask 0022 &&
+ echo whatever >templates/foo &&
+ git init --template=templates &&
+ echo "-rw-rw-rw-" >expect &&
+ test_modebits .git/foo >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success POSIXPERM 're-init respects core.sharedrepository (remote)' '
+ test_when_finished "rm -rf child.git" &&
+ umask 0022 &&
+ git init --bare --shared=0666 child.git &&
+ test_path_is_missing child.git/foo &&
+ git init --bare --template=templates child.git &&
+ echo "-rw-rw-rw-" >expect &&
+ test_modebits child.git/foo >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success POSIXPERM 'template can set core.sharedrepository' '
+ test_when_finished "rm -rf child.git" &&
+ umask 0022 &&
+ git config core.sharedrepository 0666 &&
+ cp .git/config templates/config &&
+ git init --bare --template=templates child.git &&
+ echo "-rw-rw-rw-" >expect &&
+ test_modebits child.git/HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t1302-repo-version.sh b/t/t1302-repo-version.sh
new file mode 100755
index 0000000..179474f
--- /dev/null
+++ b/t/t1302-repo-version.sh
@@ -0,0 +1,118 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Nguyá»…n Thái Ngá»c Duy
+#
+
+test_description='Test repository version check'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ test_oid_cache <<-\EOF &&
+ version sha1:0
+ version sha256:1
+ EOF
+ cat >test.patch <<-\EOF &&
+ diff --git a/test.txt b/test.txt
+ new file mode 100644
+ --- /dev/null
+ +++ b/test.txt
+ @@ -0,0 +1 @@
+ +123
+ EOF
+
+ test_create_repo "test" &&
+ test_create_repo "test2" &&
+ git config --file=test2/.git/config core.repositoryformatversion 99
+'
+
+test_expect_success 'gitdir selection on normal repos' '
+ test_oid version >expect &&
+ git config core.repositoryformatversion >actual &&
+ git -C test config core.repositoryformatversion >actual2 &&
+ test_cmp expect actual &&
+ test_cmp expect actual2
+'
+
+test_expect_success 'gitdir selection on unsupported repo' '
+ # Make sure it would stop at test2, not trash
+ test_expect_code 1 git -C test2 config core.repositoryformatversion
+'
+
+test_expect_success 'gitdir not required mode' '
+ git apply --stat test.patch &&
+ git -C test apply --stat ../test.patch &&
+ git -C test2 apply --stat ../test.patch
+'
+
+test_expect_success 'gitdir required mode' '
+ git apply --check --index test.patch &&
+ git -C test apply --check --index ../test.patch &&
+ test_must_fail git -C test2 apply --check --index ../test.patch
+'
+
+check_allow () {
+ git rev-parse --git-dir >actual &&
+ echo .git >expect &&
+ test_cmp expect actual
+}
+
+check_abort () {
+ test_must_fail git rev-parse --git-dir
+}
+
+# avoid git-config, since it cannot be trusted to run
+# in a repository with a broken version
+mkconfig () {
+ echo '[core]' &&
+ echo "repositoryformatversion = $1" &&
+ shift &&
+
+ if test $# -gt 0; then
+ echo '[extensions]' &&
+ for i in "$@"; do
+ echo "$i"
+ done
+ fi
+}
+
+while read outcome version extensions; do
+ test_expect_success "$outcome version=$version $extensions" "
+ mkconfig $version $extensions >.git/config &&
+ check_${outcome}
+ "
+done <<\EOF
+allow 0
+allow 1
+allow 1 noop
+abort 1 no-such-extension
+allow 0 no-such-extension
+allow 0 noop
+abort 0 noop-v1
+allow 1 noop-v1
+EOF
+
+test_expect_success 'precious-objects allowed' '
+ mkconfig 1 preciousObjects >.git/config &&
+ check_allow
+'
+
+test_expect_success 'precious-objects blocks destructive repack' '
+ test_must_fail git repack -ad
+'
+
+test_expect_success 'other repacks are OK' '
+ test_commit foo &&
+ git repack
+'
+
+test_expect_success 'precious-objects blocks prune' '
+ test_must_fail git prune
+'
+
+test_expect_success 'gc runs without complaint' '
+ git gc
+'
+
+test_done
diff --git a/t/t1303-wacky-config.sh b/t/t1303-wacky-config.sh
new file mode 100755
index 0000000..0506f3d
--- /dev/null
+++ b/t/t1303-wacky-config.sh
@@ -0,0 +1,136 @@
+#!/bin/sh
+
+test_description='Test wacky input to git config'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# Leaving off the newline is intentional!
+setup() {
+ (printf "[section]\n" &&
+ printf " key = foo") >.git/config
+}
+
+# 'check section.key value' verifies that the entry for section.key is
+# 'value'
+check() {
+ echo "$2" >expected
+ git config --get "$1" >actual 2>&1
+ test_cmp expected actual
+}
+
+# 'check section.key regex value' verifies that the entry for
+# section.key *that matches 'regex'* is 'value'
+check_regex() {
+ echo "$3" >expected
+ git config --get "$1" "$2" >actual 2>&1
+ test_cmp expected actual
+}
+
+test_expect_success 'modify same key' '
+ setup &&
+ git config section.key bar &&
+ check section.key bar
+'
+
+test_expect_success 'add key in same section' '
+ setup &&
+ git config section.other bar &&
+ check section.key foo &&
+ check section.other bar
+'
+
+test_expect_success 'add key in different section' '
+ setup &&
+ git config section2.key bar &&
+ check section.key foo &&
+ check section2.key bar
+'
+
+SECTION="test.q\"s\\sq'sp e.key"
+test_expect_success 'make sure git config escapes section names properly' '
+ git config "$SECTION" bar &&
+ check "$SECTION" bar
+'
+
+LONG_VALUE=$(printf "x%01021dx a" 7)
+test_expect_success 'do not crash on special long config line' '
+ setup &&
+ git config section.key "$LONG_VALUE" &&
+ check section.key "$LONG_VALUE"
+'
+
+setup_many() {
+ setup &&
+ # This time we want the newline so that we can tack on more
+ # entries.
+ echo >>.git/config &&
+ # Semi-efficient way of concatenating 5^5 = 3125 lines. Note
+ # that because 'setup' already put one line, this means 3126
+ # entries for section.key in the config file.
+ cat >5to1 <<-\EOF &&
+ key = foo
+ key = foo
+ key = foo
+ key = foo
+ key = foo
+ EOF
+ cat 5to1 5to1 5to1 5to1 5to1 >5to2 && # 25
+ cat 5to2 5to2 5to2 5to2 5to2 >5to3 && # 125
+ cat 5to3 5to3 5to3 5to3 5to3 >5to4 && # 635
+ cat 5to4 5to4 5to4 5to4 5to4 >>.git/config # 3125
+}
+
+test_expect_success 'get many entries' '
+ setup_many &&
+ git config --get-all section.key >actual &&
+ test_line_count = 3126 actual
+'
+
+test_expect_success 'get many entries by regex' '
+ setup_many &&
+ git config --get-regexp "sec.*ke." >actual &&
+ test_line_count = 3126 actual
+'
+
+test_expect_success 'add and replace one of many entries' '
+ setup_many &&
+ git config --add section.key bar &&
+ check_regex section.key "b.*r" bar &&
+ git config section.key beer "b.*r" &&
+ check_regex section.key "b.*r" beer
+'
+
+test_expect_success 'replace many entries' '
+ setup_many &&
+ git config --replace-all section.key bar &&
+ check section.key bar
+'
+
+test_expect_success 'unset many entries' '
+ setup_many &&
+ git config --unset-all section.key &&
+ test_must_fail git config section.key
+'
+
+test_expect_success '--add appends new value after existing empty value' '
+ cat >expect <<-\EOF &&
+
+
+ fool
+ roll
+ EOF
+ cp .git/config .git/config.old &&
+ test_when_finished "mv .git/config.old .git/config" &&
+ cat >.git/config <<-\EOF &&
+ [foo]
+ baz
+ baz =
+ baz = fool
+ EOF
+ git config --add foo.baz roll &&
+ git config --get-all foo.baz >output &&
+ test_cmp expect output
+'
+
+test_done
diff --git a/t/t1304-default-acl.sh b/t/t1304-default-acl.sh
new file mode 100755
index 0000000..31b89dd
--- /dev/null
+++ b/t/t1304-default-acl.sh
@@ -0,0 +1,66 @@
+#!/bin/sh
+#
+# Copyright (c) 2010 Matthieu Moy
+#
+
+test_description='Test repository with default ACL'
+
+# Create the test repo with restrictive umask
+# => this must come before . ./test-lib.sh
+umask 077
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# We need an arbitrary other user give permission to using ACLs. root
+# is a good candidate: exists on all unices, and it has permission
+# anyway, so we don't create a security hole running the testsuite.
+test_expect_success 'checking for a working acl setup' '
+ if setfacl -m d:m:rwx -m u:root:rwx . &&
+ getfacl . | grep user:root:rwx &&
+ touch should-have-readable-acl &&
+ getfacl should-have-readable-acl | grep -E "mask::?rw-"
+ then
+ test_set_prereq SETFACL
+ fi
+'
+
+if test -z "$LOGNAME"
+then
+ LOGNAME="${USER:-$(id -u -n)}"
+fi
+
+check_perms_and_acl () {
+ test -r "$1" &&
+ getfacl "$1" > actual &&
+ grep -q "user:root:rwx" actual &&
+ grep -q "user:${LOGNAME}:rwx" actual &&
+ grep -E "mask::?r--" actual > /dev/null 2>&1 &&
+ grep -q "group::---" actual || false
+}
+
+dirs_to_set="./ .git/ .git/objects/ .git/objects/pack/"
+
+test_expect_success SETFACL 'Setup test repo' '
+ setfacl -m d:u::rwx,d:g::---,d:o:---,d:m:rwx $dirs_to_set &&
+ setfacl -m m:rwx $dirs_to_set &&
+ setfacl -m u:root:rwx $dirs_to_set &&
+ setfacl -m d:u:"$LOGNAME":rwx $dirs_to_set &&
+ setfacl -m d:u:root:rwx $dirs_to_set &&
+
+ touch file.txt &&
+ git add file.txt &&
+ git commit -m "init"
+'
+
+test_expect_success SETFACL 'Objects creation does not break ACLs with restrictive umask' '
+ # SHA1 for empty blob
+ check_perms_and_acl .git/objects/$(echo $EMPTY_BLOB | sed -e "s,^\(..\),\1/,")
+'
+
+test_expect_success SETFACL 'git gc does not break ACLs with restrictive umask' '
+ git gc &&
+ check_perms_and_acl .git/objects/pack/*.pack
+'
+
+test_done
diff --git a/t/t1305-config-include.sh b/t/t1305-config-include.sh
new file mode 100755
index 0000000..5cde79e
--- /dev/null
+++ b/t/t1305-config-include.sh
@@ -0,0 +1,360 @@
+#!/bin/sh
+
+test_description='test config file include directives'
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# Force setup_explicit_git_dir() to run until the end. This is needed
+# by some tests to make sure real_path() is called on $GIT_DIR. The
+# caller needs to make sure git commands are run from a subdirectory
+# though or real_path() will not be called.
+force_setup_explicit_git_dir() {
+ GIT_DIR="$(pwd)/.git"
+ GIT_WORK_TREE="$(pwd)"
+ export GIT_DIR GIT_WORK_TREE
+}
+
+test_expect_success 'include file by absolute path' '
+ echo "[test]one = 1" >one &&
+ echo "[include]path = \"$(pwd)/one\"" >.gitconfig &&
+ echo 1 >expect &&
+ git config test.one >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'include file by relative path' '
+ echo "[test]one = 1" >one &&
+ echo "[include]path = one" >.gitconfig &&
+ echo 1 >expect &&
+ git config test.one >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'chained relative paths' '
+ mkdir subdir &&
+ echo "[test]three = 3" >subdir/three &&
+ echo "[include]path = three" >subdir/two &&
+ echo "[include]path = subdir/two" >.gitconfig &&
+ echo 3 >expect &&
+ git config test.three >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'include paths get tilde-expansion' '
+ echo "[test]one = 1" >one &&
+ echo "[include]path = ~/one" >.gitconfig &&
+ echo 1 >expect &&
+ git config test.one >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'include options can still be examined' '
+ echo "[test]one = 1" >one &&
+ echo "[include]path = one" >.gitconfig &&
+ echo one >expect &&
+ git config include.path >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'listing includes option and expansion' '
+ echo "[test]one = 1" >one &&
+ echo "[include]path = one" >.gitconfig &&
+ cat >expect <<-\EOF &&
+ include.path=one
+ test.one=1
+ EOF
+ git config --list >actual.full &&
+ grep -v -e ^core -e ^extensions actual.full >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'single file lookup does not expand includes by default' '
+ echo "[test]one = 1" >one &&
+ echo "[include]path = one" >.gitconfig &&
+ test_must_fail git config -f .gitconfig test.one &&
+ test_must_fail git config --global test.one &&
+ echo 1 >expect &&
+ git config --includes -f .gitconfig test.one >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'single file list does not expand includes by default' '
+ echo "[test]one = 1" >one &&
+ echo "[include]path = one" >.gitconfig &&
+ echo "include.path=one" >expect &&
+ git config -f .gitconfig --list >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'writing config file does not expand includes' '
+ echo "[test]one = 1" >one &&
+ echo "[include]path = one" >.gitconfig &&
+ git config test.two 2 &&
+ echo 2 >expect &&
+ git config --no-includes test.two >actual &&
+ test_cmp expect actual &&
+ test_must_fail git config --no-includes test.one
+'
+
+test_expect_success 'config modification does not affect includes' '
+ echo "[test]one = 1" >one &&
+ echo "[include]path = one" >.gitconfig &&
+ git config test.one 2 &&
+ echo 1 >expect &&
+ git config -f one test.one >actual &&
+ test_cmp expect actual &&
+ cat >expect <<-\EOF &&
+ 1
+ 2
+ EOF
+ git config --get-all test.one >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'missing include files are ignored' '
+ cat >.gitconfig <<-\EOF &&
+ [include]path = non-existent
+ [test]value = yes
+ EOF
+ echo yes >expect &&
+ git config test.value >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'absolute includes from command line work' '
+ echo "[test]one = 1" >one &&
+ echo 1 >expect &&
+ git -c include.path="$(pwd)/one" config test.one >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'relative includes from command line fail' '
+ echo "[test]one = 1" >one &&
+ test_must_fail git -c include.path=one config test.one
+'
+
+test_expect_success 'absolute includes from blobs work' '
+ echo "[test]one = 1" >one &&
+ echo "[include]path=$(pwd)/one" >blob &&
+ blob=$(git hash-object -w blob) &&
+ echo 1 >expect &&
+ git config --blob=$blob test.one >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'relative includes from blobs fail' '
+ echo "[test]one = 1" >one &&
+ echo "[include]path=one" >blob &&
+ blob=$(git hash-object -w blob) &&
+ test_must_fail git config --blob=$blob test.one
+'
+
+test_expect_success 'absolute includes from stdin work' '
+ echo "[test]one = 1" >one &&
+ echo 1 >expect &&
+ echo "[include]path=\"$(pwd)/one\"" |
+ git config --file - test.one >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'relative includes from stdin line fail' '
+ echo "[test]one = 1" >one &&
+ echo "[include]path=one" |
+ test_must_fail git config --file - test.one
+'
+
+test_expect_success 'conditional include, both unanchored' '
+ git init foo &&
+ (
+ cd foo &&
+ echo "[includeIf \"gitdir:foo/\"]path=bar" >>.git/config &&
+ echo "[test]one=1" >.git/bar &&
+ echo 1 >expect &&
+ git config test.one >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'conditional include, $HOME expansion' '
+ (
+ cd foo &&
+ echo "[includeIf \"gitdir:~/foo/\"]path=bar2" >>.git/config &&
+ echo "[test]two=2" >.git/bar2 &&
+ echo 2 >expect &&
+ git config test.two >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'conditional include, full pattern' '
+ (
+ cd foo &&
+ echo "[includeIf \"gitdir:**/foo/**\"]path=bar3" >>.git/config &&
+ echo "[test]three=3" >.git/bar3 &&
+ echo 3 >expect &&
+ git config test.three >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'conditional include, relative path' '
+ echo "[includeIf \"gitdir:./foo/.git\"]path=bar4" >>.gitconfig &&
+ echo "[test]four=4" >bar4 &&
+ (
+ cd foo &&
+ echo 4 >expect &&
+ git config test.four >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'conditional include, both unanchored, icase' '
+ (
+ cd foo &&
+ echo "[includeIf \"gitdir/i:FOO/\"]path=bar5" >>.git/config &&
+ echo "[test]five=5" >.git/bar5 &&
+ echo 5 >expect &&
+ git config test.five >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'conditional include, early config reading' '
+ (
+ cd foo &&
+ echo "[includeIf \"gitdir:foo/\"]path=bar6" >>.git/config &&
+ echo "[test]six=6" >.git/bar6 &&
+ echo 6 >expect &&
+ test-tool config read_early_config test.six >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'conditional include with /**/' '
+ REPO=foo/bar/repo &&
+ git init $REPO &&
+ cat >>$REPO/.git/config <<-\EOF &&
+ [includeIf "gitdir:**/foo/**/bar/**"]
+ path=bar7
+ EOF
+ echo "[test]seven=7" >$REPO/.git/bar7 &&
+ echo 7 >expect &&
+ git -C $REPO config test.seven >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success SYMLINKS 'conditional include, set up symlinked $HOME' '
+ mkdir real-home &&
+ ln -s real-home home &&
+ (
+ HOME="$TRASH_DIRECTORY/home" &&
+ export HOME &&
+ cd "$HOME" &&
+
+ git init foo &&
+ cd foo &&
+ mkdir sub
+ )
+'
+
+test_expect_success SYMLINKS 'conditional include, $HOME expansion with symlinks' '
+ (
+ HOME="$TRASH_DIRECTORY/home" &&
+ export HOME &&
+ cd "$HOME"/foo &&
+
+ echo "[includeIf \"gitdir:~/foo/\"]path=bar2" >>.git/config &&
+ echo "[test]two=2" >.git/bar2 &&
+ echo 2 >expect &&
+ force_setup_explicit_git_dir &&
+ git -C sub config test.two >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success SYMLINKS 'conditional include, relative path with symlinks' '
+ echo "[includeIf \"gitdir:./foo/.git\"]path=bar4" >home/.gitconfig &&
+ echo "[test]four=4" >home/bar4 &&
+ (
+ HOME="$TRASH_DIRECTORY/home" &&
+ export HOME &&
+ cd "$HOME"/foo &&
+
+ echo 4 >expect &&
+ force_setup_explicit_git_dir &&
+ git -C sub config test.four >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success SYMLINKS 'conditional include, gitdir matching symlink' '
+ ln -s foo bar &&
+ (
+ cd bar &&
+ echo "[includeIf \"gitdir:bar/\"]path=bar7" >>.git/config &&
+ echo "[test]seven=7" >.git/bar7 &&
+ echo 7 >expect &&
+ git config test.seven >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success SYMLINKS 'conditional include, gitdir matching symlink, icase' '
+ (
+ cd bar &&
+ echo "[includeIf \"gitdir/i:BAR/\"]path=bar8" >>.git/config &&
+ echo "[test]eight=8" >.git/bar8 &&
+ echo 8 >expect &&
+ git config test.eight >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'conditional include, onbranch' '
+ echo "[includeIf \"onbranch:foo-branch\"]path=bar9" >>.git/config &&
+ echo "[test]nine=9" >.git/bar9 &&
+ git checkout -b main &&
+ test_must_fail git config test.nine &&
+ git checkout -b foo-branch &&
+ echo 9 >expect &&
+ git config test.nine >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'conditional include, onbranch, wildcard' '
+ echo "[includeIf \"onbranch:?oo-*/**\"]path=bar10" >>.git/config &&
+ echo "[test]ten=10" >.git/bar10 &&
+ git checkout -b not-foo-branch/a &&
+ test_must_fail git config test.ten &&
+
+ echo 10 >expect &&
+ git checkout -b foo-branch/a/b/c &&
+ git config test.ten >actual &&
+ test_cmp expect actual &&
+
+ git checkout -b moo-bar/a &&
+ git config test.ten >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'conditional include, onbranch, implicit /** for /' '
+ echo "[includeIf \"onbranch:foo-dir/\"]path=bar11" >>.git/config &&
+ echo "[test]eleven=11" >.git/bar11 &&
+ git checkout -b not-foo-dir/a &&
+ test_must_fail git config test.eleven &&
+
+ echo 11 >expect &&
+ git checkout -b foo-dir/a/b/c &&
+ git config test.eleven >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'include cycles are detected' '
+ git init --bare cycle &&
+ git -C cycle config include.path cycle &&
+ git config -f cycle/cycle include.path config &&
+ test_must_fail git -C cycle config --get-all test.value 2>stderr &&
+ grep "exceeded maximum include depth" stderr
+'
+
+test_done
diff --git a/t/t1306-xdg-files.sh b/t/t1306-xdg-files.sh
new file mode 100755
index 0000000..40d3c42
--- /dev/null
+++ b/t/t1306-xdg-files.sh
@@ -0,0 +1,195 @@
+#!/bin/sh
+#
+# Copyright (c) 2012 Valentin Duperray, Lucien Kong, Franck Jonas,
+# Thomas Nguy, Khoi Nguyen
+# Grenoble INP Ensimag
+#
+
+test_description='Compatibility with $XDG_CONFIG_HOME/git/ files'
+
+. ./test-lib.sh
+
+test_expect_success 'read config: xdg file exists and ~/.gitconfig doesn'\''t' '
+ mkdir -p .config/git &&
+ echo "[alias]" >.config/git/config &&
+ echo " myalias = !echo in_config" >>.config/git/config &&
+ echo in_config >expected &&
+ git myalias >actual &&
+ test_cmp expected actual
+'
+
+
+test_expect_success 'read config: xdg file exists and ~/.gitconfig exists' '
+ >.gitconfig &&
+ echo "[alias]" >.gitconfig &&
+ echo " myalias = !echo in_gitconfig" >>.gitconfig &&
+ echo in_gitconfig >expected &&
+ git myalias >actual &&
+ test_cmp expected actual
+'
+
+
+test_expect_success 'read with --get: xdg file exists and ~/.gitconfig doesn'\''t' '
+ rm .gitconfig &&
+ echo "[user]" >.config/git/config &&
+ echo " name = read_config" >>.config/git/config &&
+ echo read_config >expected &&
+ git config --get user.name >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success '"$XDG_CONFIG_HOME overrides $HOME/.config/git' '
+ mkdir -p "$HOME"/xdg/git &&
+ echo "[user]name = in_xdg" >"$HOME"/xdg/git/config &&
+ echo in_xdg >expected &&
+ XDG_CONFIG_HOME="$HOME"/xdg git config --get-all user.name >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'read with --get: xdg file exists and ~/.gitconfig exists' '
+ >.gitconfig &&
+ echo "[user]" >.gitconfig &&
+ echo " name = read_gitconfig" >>.gitconfig &&
+ echo read_gitconfig >expected &&
+ git config --get user.name >actual &&
+ test_cmp expected actual
+'
+
+
+test_expect_success 'read with --list: xdg file exists and ~/.gitconfig doesn'\''t' '
+ rm .gitconfig &&
+ echo user.name=read_config >expected &&
+ git config --global --list >actual &&
+ test_cmp expected actual
+'
+
+
+test_expect_success 'read with --list: xdg file exists and ~/.gitconfig exists' '
+ >.gitconfig &&
+ echo "[user]" >.gitconfig &&
+ echo " name = read_gitconfig" >>.gitconfig &&
+ echo user.name=read_gitconfig >expected &&
+ git config --global --list >actual &&
+ test_cmp expected actual
+'
+
+
+test_expect_success 'Setup' '
+ git init git &&
+ cd git &&
+ echo foo >to_be_excluded
+'
+
+
+test_expect_success 'Exclusion of a file in the XDG ignore file' '
+ mkdir -p "$HOME"/.config/git/ &&
+ echo to_be_excluded >"$HOME"/.config/git/ignore &&
+ test_must_fail git add to_be_excluded
+'
+
+test_expect_success '$XDG_CONFIG_HOME overrides $HOME/.config/git/ignore' '
+ mkdir -p "$HOME"/xdg/git &&
+ echo content >excluded_by_xdg_only &&
+ echo excluded_by_xdg_only >"$HOME"/xdg/git/ignore &&
+ test_when_finished "git read-tree --empty" &&
+ (XDG_CONFIG_HOME="$HOME/xdg" &&
+ export XDG_CONFIG_HOME &&
+ git add to_be_excluded &&
+ test_must_fail git add excluded_by_xdg_only
+ )
+'
+
+test_expect_success 'Exclusion in both XDG and local ignore files' '
+ echo to_be_excluded >.gitignore &&
+ test_must_fail git add to_be_excluded
+'
+
+
+test_expect_success 'Exclusion in a non-XDG global ignore file' '
+ rm .gitignore &&
+ echo >"$HOME"/.config/git/ignore &&
+ echo to_be_excluded >"$HOME"/my_gitignore &&
+ git config core.excludesfile "$HOME"/my_gitignore &&
+ test_must_fail git add to_be_excluded
+'
+
+test_expect_success 'Checking XDG ignore file when HOME is unset' '
+ (sane_unset HOME &&
+ git config --unset core.excludesfile &&
+ git ls-files --exclude-standard --ignored --others >actual) &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'Checking attributes in the XDG attributes file' '
+ echo foo >f &&
+ git check-attr -a f >actual &&
+ test_line_count -eq 0 actual &&
+ echo "f attr_f" >"$HOME"/.config/git/attributes &&
+ echo "f: attr_f: set" >expected &&
+ git check-attr -a f >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'Checking XDG attributes when HOME is unset' '
+ (sane_unset HOME &&
+ git check-attr -a f >actual) &&
+ test_must_be_empty actual
+'
+
+test_expect_success '$XDG_CONFIG_HOME overrides $HOME/.config/git/attributes' '
+ mkdir -p "$HOME"/xdg/git &&
+ echo "f attr_f=xdg" >"$HOME"/xdg/git/attributes &&
+ echo "f: attr_f: xdg" >expected &&
+ XDG_CONFIG_HOME="$HOME/xdg" git check-attr -a f >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'Checking attributes in both XDG and local attributes files' '
+ echo "f -attr_f" >.gitattributes &&
+ echo "f: attr_f: unset" >expected &&
+ git check-attr -a f >actual &&
+ test_cmp expected actual
+'
+
+
+test_expect_success 'Checking attributes in a non-XDG global attributes file' '
+ rm -f .gitattributes &&
+ echo "f attr_f=test" >"$HOME"/my_gitattributes &&
+ git config core.attributesfile "$HOME"/my_gitattributes &&
+ echo "f: attr_f: test" >expected &&
+ git check-attr -a f >actual &&
+ test_cmp expected actual
+'
+
+
+test_expect_success 'write: xdg file exists and ~/.gitconfig doesn'\''t' '
+ mkdir -p "$HOME"/.config/git &&
+ >"$HOME"/.config/git/config &&
+ rm -f "$HOME"/.gitconfig &&
+ git config --global user.name "write_config" &&
+ echo "[user]" >expected &&
+ echo " name = write_config" >>expected &&
+ test_cmp expected "$HOME"/.config/git/config
+'
+
+
+test_expect_success 'write: xdg file exists and ~/.gitconfig exists' '
+ >"$HOME"/.gitconfig &&
+ git config --global user.name "write_gitconfig" &&
+ echo "[user]" >expected &&
+ echo " name = write_gitconfig" >>expected &&
+ test_cmp expected "$HOME"/.gitconfig
+'
+
+
+test_expect_success 'write: ~/.config/git/ exists and config file doesn'\''t' '
+ rm -f "$HOME"/.gitconfig &&
+ rm -f "$HOME"/.config/git/config &&
+ git config --global user.name "write_gitconfig" &&
+ echo "[user]" >expected &&
+ echo " name = write_gitconfig" >>expected &&
+ test_cmp expected "$HOME"/.gitconfig
+'
+
+
+test_done
diff --git a/t/t1307-config-blob.sh b/t/t1307-config-blob.sh
new file mode 100755
index 0000000..b9852fe
--- /dev/null
+++ b/t/t1307-config-blob.sh
@@ -0,0 +1,80 @@
+#!/bin/sh
+
+test_description='support for reading config from a blob'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'create config blob' '
+ cat >config <<-\EOF &&
+ [some]
+ value = 1
+ EOF
+ git add config &&
+ git commit -m foo
+'
+
+test_expect_success 'list config blob contents' '
+ echo some.value=1 >expect &&
+ git config --blob=HEAD:config --list >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'fetch value from blob' '
+ echo true >expect &&
+ git config --blob=HEAD:config --bool some.value >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'reading non-existing value from blob is an error' '
+ test_must_fail git config --blob=HEAD:config non.existing
+'
+
+test_expect_success 'reading from blob and file is an error' '
+ test_must_fail git config --blob=HEAD:config --system --list
+'
+
+test_expect_success 'reading from missing ref is an error' '
+ test_must_fail git config --blob=HEAD:doesnotexist --list
+'
+
+test_expect_success 'reading from non-blob is an error' '
+ test_must_fail git config --blob=HEAD --list
+'
+
+test_expect_success 'setting a value in a blob is an error' '
+ test_must_fail git config --blob=HEAD:config some.value foo
+'
+
+test_expect_success 'deleting a value in a blob is an error' '
+ test_must_fail git config --blob=HEAD:config --unset some.value
+'
+
+test_expect_success 'editing a blob is an error' '
+ test_must_fail git config --blob=HEAD:config --edit
+'
+
+test_expect_success 'parse errors in blobs are properly attributed' '
+ cat >config <<-\EOF &&
+ [some]
+ value = "
+ EOF
+ git add config &&
+ git commit -m broken &&
+
+ test_must_fail git config --blob=HEAD:config some.value 2>err &&
+ test_grep "HEAD:config" err
+'
+
+test_expect_success 'can parse blob ending with CR' '
+ test_commit --printf CR config "[some]key = value\\r" &&
+ echo value >expect &&
+ git config --blob=HEAD:config some.key >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'config --blob outside of a repository is an error' '
+ nongit test_must_fail git config --blob=foo --list
+'
+
+test_done
diff --git a/t/t1308-config-set.sh b/t/t1308-config-set.sh
new file mode 100755
index 0000000..3bfec07
--- /dev/null
+++ b/t/t1308-config-set.sh
@@ -0,0 +1,400 @@
+#!/bin/sh
+
+test_description='Test git config-set API in different settings'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# 'check_config get_* section.key value' verifies that the entry for
+# section.key is 'value'
+check_config () {
+ if test "$1" = expect_code
+ then
+ expect_code="$2" && shift && shift
+ else
+ expect_code=0
+ fi &&
+ op=$1 key=$2 && shift && shift &&
+ if test $# != 0
+ then
+ printf "%s\n" "$@"
+ fi >expect &&
+ test_expect_code $expect_code test-tool config "$op" "$key" >actual &&
+ test_cmp expect actual
+}
+
+test_expect_success 'setup default config' '
+ cat >.git/config <<-\EOF
+ [case]
+ penguin = very blue
+ Movie = BadPhysics
+ UPPERCASE = true
+ MixedCase = true
+ my =
+ foo
+ baz = sam
+ [Cores]
+ WhatEver = Second
+ baz = bar
+ [cores]
+ baz = bat
+ [CORES]
+ baz = ball
+ [my "Foo bAr"]
+ hi = mixed-case
+ [my "FOO BAR"]
+ hi = upper-case
+ [my "foo bar"]
+ hi = lower-case
+ [case]
+ baz = bat
+ baz = hask
+ [lamb]
+ chop = 65
+ head = none
+ [goat]
+ legs = 4
+ head = true
+ skin = false
+ nose = 1
+ horns
+ [value]
+ less
+ EOF
+'
+
+test_expect_success 'get value for a simple key' '
+ check_config get_value case.penguin "very blue"
+'
+
+test_expect_success 'get value for a key with value as an empty string' '
+ check_config get_value case.my ""
+'
+
+test_expect_success 'get value for a key with value as NULL' '
+ check_config get_value case.foo "(NULL)"
+'
+
+test_expect_success 'upper case key' '
+ check_config get_value case.UPPERCASE "true" &&
+ check_config get_value case.uppercase "true"
+'
+
+test_expect_success 'mixed case key' '
+ check_config get_value case.MixedCase "true" &&
+ check_config get_value case.MIXEDCASE "true" &&
+ check_config get_value case.mixedcase "true"
+'
+
+test_expect_success 'key and value with mixed case' '
+ check_config get_value case.Movie "BadPhysics"
+'
+
+test_expect_success 'key with case sensitive subsection' '
+ check_config get_value "my.Foo bAr.hi" "mixed-case" &&
+ check_config get_value "my.FOO BAR.hi" "upper-case" &&
+ check_config get_value "my.foo bar.hi" "lower-case"
+'
+
+test_expect_success 'key with case insensitive section header' '
+ check_config get_value cores.baz "ball" &&
+ check_config get_value Cores.baz "ball" &&
+ check_config get_value CORES.baz "ball" &&
+ check_config get_value coreS.baz "ball"
+'
+
+test_expect_success 'key with case insensitive section header & variable' '
+ check_config get_value CORES.BAZ "ball" &&
+ check_config get_value cores.baz "ball" &&
+ check_config get_value cores.BaZ "ball" &&
+ check_config get_value cOreS.bAz "ball"
+'
+
+test_expect_success 'find value with misspelled key' '
+ check_config expect_code 1 get_value "my.fOo Bar.hi" "Value not found for \"my.fOo Bar.hi\""
+'
+
+test_expect_success 'find value with the highest priority' '
+ check_config get_value case.baz "hask"
+'
+
+test_expect_success 'return value for an existing key' '
+ test-tool config get lamb.chop >out 2>err &&
+ test_must_be_empty out &&
+ test_must_be_empty err
+'
+
+test_expect_success 'return value for value-less key' '
+ test-tool config get value.less >out 2>err &&
+ test_must_be_empty out &&
+ test_must_be_empty err
+'
+
+test_expect_success 'return value for a missing key' '
+ cat >expect <<-\EOF &&
+ Value not found for "missing.key"
+ EOF
+ test_expect_code 1 test-tool config get missing.key >actual 2>err &&
+ test_cmp actual expect &&
+ test_must_be_empty err
+'
+
+test_expect_success 'return value for a bad key: CONFIG_INVALID_KEY' '
+ cat >expect <<-\EOF &&
+ Key "fails.iskeychar.-" is invalid
+ EOF
+ test_expect_code 1 test-tool config get fails.iskeychar.- >actual 2>err &&
+ test_cmp actual expect &&
+ test_must_be_empty out
+'
+
+test_expect_success 'return value for a bad key: CONFIG_NO_SECTION_OR_NAME' '
+ cat >expect <<-\EOF &&
+ Key "keynosection" has no section
+ EOF
+ test_expect_code 1 test-tool config get keynosection >actual 2>err &&
+ test_cmp actual expect &&
+ test_must_be_empty out
+'
+
+test_expect_success 'find integer value for a key' '
+ check_config get_int lamb.chop 65
+'
+
+test_expect_success 'parse integer value during iteration' '
+ check_config git_config_int lamb.chop 65
+'
+
+test_expect_success 'find string value for a key' '
+ check_config get_string case.baz hask &&
+ check_config expect_code 1 get_string case.ba "Value not found for \"case.ba\""
+'
+
+test_expect_success 'check line error when NULL string is queried' '
+ test_expect_code 128 test-tool config get_string case.foo 2>result &&
+ test_grep "fatal: .*case\.foo.*\.git/config.*line 7" result
+'
+
+test_expect_success 'find integer if value is non parse-able' '
+ check_config expect_code 128 get_int lamb.head
+'
+
+test_expect_success 'non parse-able integer value during iteration' '
+ check_config expect_code 128 git_config_int lamb.head 2>result &&
+ grep "fatal: bad numeric config value .* in file \.git/config" result
+'
+
+test_expect_success 'find bool value for the entered key' '
+ check_config get_bool goat.head 1 &&
+ check_config get_bool goat.skin 0 &&
+ check_config get_bool goat.nose 1 &&
+ check_config get_bool goat.horns 1 &&
+ check_config get_bool goat.legs 1
+'
+
+test_expect_success 'find multiple values' '
+ check_config get_value_multi case.baz sam bat hask
+'
+
+test_NULL_in_multi () {
+ local op="$1" &&
+ local file="$2" &&
+
+ test_expect_success "$op: NULL value in config${file:+ in $file}" '
+ config="$file" &&
+ if test -z "$config"
+ then
+ config=.git/config &&
+ test_when_finished "mv $config.old $config" &&
+ mv "$config" "$config".old
+ fi &&
+
+ # Value-less in the middle of a list
+ cat >"$config" <<-\EOF &&
+ [a]key=x
+ [a]key
+ [a]key=y
+ EOF
+ case "$op" in
+ *_multi)
+ cat >expect <<-\EOF
+ x
+ (NULL)
+ y
+ EOF
+ ;;
+ *)
+ cat >expect <<-\EOF
+ y
+ EOF
+ ;;
+ esac &&
+ test-tool config "$op" a.key $file >actual &&
+ test_cmp expect actual &&
+
+ # Value-less at the end of a least
+ cat >"$config" <<-\EOF &&
+ [a]key=x
+ [a]key=y
+ [a]key
+ EOF
+ case "$op" in
+ *_multi)
+ cat >expect <<-\EOF
+ x
+ y
+ (NULL)
+ EOF
+ ;;
+ *)
+ cat >expect <<-\EOF
+ (NULL)
+ EOF
+ ;;
+ esac &&
+ test-tool config "$op" a.key $file >actual &&
+ test_cmp expect actual
+ '
+}
+
+test_NULL_in_multi "get_value_multi"
+test_NULL_in_multi "configset_get_value" "my.config"
+test_NULL_in_multi "configset_get_value_multi" "my.config"
+
+test_expect_success 'find value from a configset' '
+ cat >config2 <<-\EOF &&
+ [case]
+ baz = lama
+ [my]
+ new = silk
+ [case]
+ baz = ball
+ EOF
+ echo silk >expect &&
+ test-tool config configset_get_value my.new config2 .git/config >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'find value with highest priority from a configset' '
+ echo hask >expect &&
+ test-tool config configset_get_value case.baz config2 .git/config >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'find value_list for a key from a configset' '
+ cat >expect <<-\EOF &&
+ lama
+ ball
+ sam
+ bat
+ hask
+ EOF
+ test-tool config configset_get_value_multi case.baz config2 .git/config >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'proper error on non-existent files' '
+ echo "Error (-1) reading configuration file non-existent-file." >expect &&
+ test_expect_code 2 test-tool config configset_get_value foo.bar non-existent-file 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'proper error on directory "files"' '
+ echo "Error (-1) reading configuration file a-directory." >expect &&
+ mkdir a-directory &&
+ test_expect_code 2 test-tool config configset_get_value foo.bar a-directory 2>output &&
+ grep "^warning:" output &&
+ grep "^Error" output >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success POSIXPERM,SANITY 'proper error on non-accessible files' '
+ chmod -r .git/config &&
+ test_when_finished "chmod +r .git/config" &&
+ echo "Error (-1) reading configuration file .git/config." >expect &&
+ test_expect_code 2 test-tool config configset_get_value foo.bar .git/config 2>output &&
+ grep "^warning:" output &&
+ grep "^Error" output >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'proper error on error in default config files' '
+ cp .git/config .git/config.old &&
+ test_when_finished "mv .git/config.old .git/config" &&
+ echo "[" >>.git/config &&
+ echo "fatal: bad config line 36 in file .git/config" >expect &&
+ test_expect_code 128 test-tool config get_value foo.bar 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'proper error on error in custom config files' '
+ echo "[" >>syntax-error &&
+ echo "fatal: bad config line 1 in file syntax-error" >expect &&
+ test_expect_code 128 test-tool config configset_get_value foo.bar syntax-error 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'check line errors for malformed values' '
+ mv .git/config .git/config.old &&
+ test_when_finished "mv .git/config.old .git/config" &&
+ cat >.git/config <<-\EOF &&
+ [alias]
+ br
+ EOF
+ test_expect_code 128 git br 2>result &&
+ test_grep "missing value for .alias\.br" result &&
+ test_grep "fatal: .*\.git/config" result &&
+ test_grep "fatal: .*line 2" result
+'
+
+test_expect_success 'error on modifying repo config without repo' '
+ nongit test_must_fail git config a.b c 2>err &&
+ test_grep "not in a git directory" err
+'
+
+cmdline_config="'foo.bar=from-cmdline'"
+test_expect_success 'iteration shows correct origins' '
+ printf "[ignore]\n\tthis = please\n[foo]bar = from-repo\n" >.git/config &&
+ printf "[foo]\n\tbar = from-home\n" >.gitconfig &&
+ if test_have_prereq MINGW
+ then
+ # Use Windows path (i.e. *not* $HOME)
+ HOME_GITCONFIG=$(pwd)/.gitconfig
+ else
+ # Do not get fooled by symbolic links, i.e. $HOME != $(pwd)
+ HOME_GITCONFIG=$HOME/.gitconfig
+ fi &&
+ cat >expect <<-EOF &&
+ key=foo.bar
+ value=from-home
+ origin=file
+ name=$HOME_GITCONFIG
+ lno=2
+ scope=global
+
+ key=ignore.this
+ value=please
+ origin=file
+ name=.git/config
+ lno=2
+ scope=local
+
+ key=foo.bar
+ value=from-repo
+ origin=file
+ name=.git/config
+ lno=3
+ scope=local
+
+ key=foo.bar
+ value=from-cmdline
+ origin=command line
+ name=
+ lno=-1
+ scope=command
+ EOF
+ GIT_CONFIG_PARAMETERS=$cmdline_config test-tool config iterate >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t1309-early-config.sh b/t/t1309-early-config.sh
new file mode 100755
index 0000000..523aa99
--- /dev/null
+++ b/t/t1309-early-config.sh
@@ -0,0 +1,103 @@
+#!/bin/sh
+
+test_description='Test read_early_config()'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'read early config' '
+ test_config early.config correct &&
+ test-tool config read_early_config early.config >output &&
+ test correct = "$(cat output)"
+'
+
+test_expect_success 'in a sub-directory' '
+ test_config early.config sub &&
+ mkdir -p sub &&
+ (
+ cd sub &&
+ test-tool config read_early_config early.config
+ ) >output &&
+ test sub = "$(cat output)"
+'
+
+test_expect_success 'ceiling' '
+ test_config early.config ceiling &&
+ mkdir -p sub &&
+ (
+ GIT_CEILING_DIRECTORIES="$PWD" &&
+ export GIT_CEILING_DIRECTORIES &&
+ cd sub &&
+ test-tool config read_early_config early.config
+ ) >output &&
+ test_must_be_empty output
+'
+
+test_expect_success 'ceiling #2' '
+ mkdir -p xdg/git &&
+ git config -f xdg/git/config early.config xdg &&
+ test_config early.config ceiling &&
+ mkdir -p sub &&
+ (
+ XDG_CONFIG_HOME="$PWD"/xdg &&
+ GIT_CEILING_DIRECTORIES="$PWD" &&
+ export GIT_CEILING_DIRECTORIES XDG_CONFIG_HOME &&
+ cd sub &&
+ test-tool config read_early_config early.config
+ ) >output &&
+ test xdg = "$(cat output)"
+'
+
+cmdline_config="'test.source=cmdline'"
+test_expect_success 'read config file in right order' '
+ echo "[test]source = home" >>.gitconfig &&
+ git init foo &&
+ (
+ cd foo &&
+ echo "[test]source = repo" >>.git/config &&
+ GIT_CONFIG_PARAMETERS=$cmdline_config test-tool config \
+ read_early_config test.source >actual &&
+ cat >expected <<-\EOF &&
+ home
+ repo
+ cmdline
+ EOF
+ test_cmp expected actual
+ )
+'
+
+test_with_config () {
+ rm -rf throwaway &&
+ git init throwaway &&
+ (
+ cd throwaway &&
+ echo "$*" >.git/config &&
+ test-tool config read_early_config early.config
+ )
+}
+
+test_expect_success 'ignore .git/ with incompatible repository version' '
+ test_with_config "[core]repositoryformatversion = 999999" 2>err &&
+ test_grep "warning:.* Expected git repo version <= [1-9]" err
+'
+
+test_expect_failure 'ignore .git/ with invalid repository version' '
+ test_with_config "[core]repositoryformatversion = invalid"
+'
+
+
+test_expect_failure 'ignore .git/ with invalid config' '
+ test_with_config "["
+'
+
+test_expect_success 'early config and onbranch' '
+ echo "[broken" >broken &&
+ test_with_config "[includeif \"onbranch:topic\"]path=../broken"
+'
+
+test_expect_success 'onbranch config outside of git repo' '
+ test_config_global includeIf.onbranch:topic.path non-existent &&
+ nongit git help
+'
+
+test_done
diff --git a/t/t1310-config-default.sh b/t/t1310-config-default.sh
new file mode 100755
index 0000000..1a90d31
--- /dev/null
+++ b/t/t1310-config-default.sh
@@ -0,0 +1,37 @@
+#!/bin/sh
+
+test_description='Test git config in different settings (with --default)'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'uses --default when entry missing' '
+ echo quux >expect &&
+ git config -f config --default=quux core.foo >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'does not use --default when entry present' '
+ echo bar >expect &&
+ git -c core.foo=bar config --default=baz core.foo >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'canonicalizes --default with appropriate type' '
+ echo true >expect &&
+ git config -f config --default=yes --bool core.foo >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'dies when --default cannot be parsed' '
+ test_must_fail git config -f config --type=expiry-date --default=x --get \
+ not.a.section 2>error &&
+ test_grep "failed to format default config value" error
+'
+
+test_expect_success 'does not allow --default without --get' '
+ test_must_fail git config --default=quux --unset a.section >output 2>&1 &&
+ test_grep "\-\-default is only applicable to" output
+'
+
+test_done
diff --git a/t/t1350-config-hooks-path.sh b/t/t1350-config-hooks-path.sh
new file mode 100755
index 0000000..f6dc83e
--- /dev/null
+++ b/t/t1350-config-hooks-path.sh
@@ -0,0 +1,44 @@
+#!/bin/sh
+
+test_description='Test the core.hooksPath configuration variable'
+
+. ./test-lib.sh
+
+test_expect_success 'set up a pre-commit hook in core.hooksPath' '
+ >actual &&
+ mkdir -p .git/custom-hooks &&
+ write_script .git/custom-hooks/pre-commit <<-\EOF &&
+ echo CUSTOM >>actual
+ EOF
+ test_hook --setup pre-commit <<-\EOF
+ echo NORMAL >>actual
+ EOF
+'
+
+test_expect_success 'Check that various forms of specifying core.hooksPath work' '
+ test_commit no_custom_hook &&
+ git config core.hooksPath .git/custom-hooks &&
+ test_commit have_custom_hook &&
+ git config core.hooksPath .git/custom-hooks/ &&
+ test_commit have_custom_hook_trailing_slash &&
+ git config core.hooksPath "$PWD/.git/custom-hooks" &&
+ test_commit have_custom_hook_abs_path &&
+ git config core.hooksPath "$PWD/.git/custom-hooks/" &&
+ test_commit have_custom_hook_abs_path_trailing_slash &&
+ cat >expect <<-\EOF &&
+ NORMAL
+ CUSTOM
+ CUSTOM
+ CUSTOM
+ CUSTOM
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'git rev-parse --git-path hooks' '
+ git config core.hooksPath .git/custom-hooks &&
+ git rev-parse --git-path hooks/abc >actual &&
+ test .git/custom-hooks/abc = "$(cat actual)"
+'
+
+test_done
diff --git a/t/t1400-update-ref.sh b/t/t1400-update-ref.sh
new file mode 100755
index 0000000..9ac4b70
--- /dev/null
+++ b/t/t1400-update-ref.sh
@@ -0,0 +1,1647 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Shawn Pearce
+#
+
+test_description='Test git update-ref and basic ref logging'
+. ./test-lib.sh
+
+Z=$ZERO_OID
+
+m=refs/heads/main
+n_dir=refs/heads/gu
+n=$n_dir/fixes
+outside=refs/foo
+bare=bare-repo
+
+create_test_commits ()
+{
+ prfx="$1"
+ for name in A B C D E F
+ do
+ test_tick &&
+ T=$(git write-tree) &&
+ sha1=$(echo $name | git commit-tree $T) &&
+ eval $prfx$name=$sha1
+ done
+}
+
+test_expect_success setup '
+ git checkout --orphan main &&
+ create_test_commits "" &&
+ mkdir $bare &&
+ cd $bare &&
+ git init --bare -b main &&
+ create_test_commits "bare" &&
+ cd -
+'
+
+test_expect_success "create $m" '
+ git update-ref $m $A &&
+ test $A = $(git show-ref -s --verify $m)
+'
+test_expect_success "create $m with oldvalue verification" '
+ git update-ref $m $B $A &&
+ test $B = $(git show-ref -s --verify $m)
+'
+test_expect_success "fail to delete $m with stale ref" '
+ test_must_fail git update-ref -d $m $A &&
+ test $B = "$(git show-ref -s --verify $m)"
+'
+test_expect_success "delete $m" '
+ test_when_finished "git update-ref -d $m" &&
+ git update-ref -d $m $B &&
+ test_must_fail git show-ref --verify -q $m
+'
+
+test_expect_success "delete $m without oldvalue verification" '
+ test_when_finished "git update-ref -d $m" &&
+ git update-ref $m $A &&
+ test $A = $(git show-ref -s --verify $m) &&
+ git update-ref -d $m &&
+ test_must_fail git show-ref --verify -q $m
+'
+
+test_expect_success "fail to create $n" '
+ test_when_finished "rm -f .git/$n_dir" &&
+ touch .git/$n_dir &&
+ test_must_fail git update-ref $n $A
+'
+
+test_expect_success "create $m (by HEAD)" '
+ git update-ref HEAD $A &&
+ test $A = $(git show-ref -s --verify $m)
+'
+test_expect_success "create $m (by HEAD) with oldvalue verification" '
+ git update-ref HEAD $B $A &&
+ test $B = $(git show-ref -s --verify $m)
+'
+test_expect_success "fail to delete $m (by HEAD) with stale ref" '
+ test_must_fail git update-ref -d HEAD $A &&
+ test $B = $(git show-ref -s --verify $m)
+'
+test_expect_success "delete $m (by HEAD)" '
+ test_when_finished "git update-ref -d $m" &&
+ git update-ref -d HEAD $B &&
+ test_must_fail git show-ref --verify -q $m
+'
+
+test_expect_success "deleting current branch adds message to HEAD's log" '
+ test_when_finished "git update-ref -d $m" &&
+ git update-ref $m $A &&
+ git symbolic-ref HEAD $m &&
+ git update-ref -m delete-$m -d $m &&
+ test_must_fail git show-ref --verify -q $m &&
+ grep "delete-$m$" .git/logs/HEAD
+'
+
+test_expect_success "deleting by HEAD adds message to HEAD's log" '
+ test_when_finished "git update-ref -d $m" &&
+ git update-ref $m $A &&
+ git symbolic-ref HEAD $m &&
+ git update-ref -m delete-by-head -d HEAD &&
+ test_must_fail git show-ref --verify -q $m &&
+ grep "delete-by-head$" .git/logs/HEAD
+'
+
+test_expect_success 'update-ref does not create reflogs by default' '
+ test_when_finished "git update-ref -d $outside" &&
+ git update-ref $outside $A &&
+ git rev-parse $A >expect &&
+ git rev-parse $outside >actual &&
+ test_cmp expect actual &&
+ test_must_fail git reflog exists $outside
+'
+
+test_expect_success 'update-ref creates reflogs with --create-reflog' '
+ test_when_finished "git update-ref -d $outside" &&
+ git update-ref --create-reflog $outside $A &&
+ git rev-parse $A >expect &&
+ git rev-parse $outside >actual &&
+ test_cmp expect actual &&
+ git reflog exists $outside
+'
+
+test_expect_success 'creates no reflog in bare repository' '
+ git -C $bare update-ref $m $bareA &&
+ git -C $bare rev-parse $bareA >expect &&
+ git -C $bare rev-parse $m >actual &&
+ test_cmp expect actual &&
+ test_must_fail git -C $bare reflog exists $m
+'
+
+test_expect_success 'core.logAllRefUpdates=true creates reflog in bare repository' '
+ test_when_finished "git -C $bare config --unset core.logAllRefUpdates && \
+ rm $bare/logs/$m" &&
+ git -C $bare config core.logAllRefUpdates true &&
+ git -C $bare update-ref $m $bareB &&
+ git -C $bare rev-parse $bareB >expect &&
+ git -C $bare rev-parse $m >actual &&
+ test_cmp expect actual &&
+ git -C $bare reflog exists $m
+'
+
+test_expect_success 'core.logAllRefUpdates=true does not create reflog by default' '
+ test_config core.logAllRefUpdates true &&
+ test_when_finished "git update-ref -d $outside" &&
+ git update-ref $outside $A &&
+ git rev-parse $A >expect &&
+ git rev-parse $outside >actual &&
+ test_cmp expect actual &&
+ test_must_fail git reflog exists $outside
+'
+
+test_expect_success 'core.logAllRefUpdates=always creates reflog by default' '
+ test_config core.logAllRefUpdates always &&
+ test_when_finished "git update-ref -d $outside" &&
+ git update-ref $outside $A &&
+ git rev-parse $A >expect &&
+ git rev-parse $outside >actual &&
+ test_cmp expect actual &&
+ git reflog exists $outside
+'
+
+test_expect_success 'core.logAllRefUpdates=always creates reflog for ORIG_HEAD' '
+ test_config core.logAllRefUpdates always &&
+ git update-ref ORIG_HEAD $A &&
+ git reflog exists ORIG_HEAD
+'
+
+test_expect_success '--no-create-reflog overrides core.logAllRefUpdates=always' '
+ test_config core.logAllRefUpdates true &&
+ test_when_finished "git update-ref -d $outside" &&
+ git update-ref --no-create-reflog $outside $A &&
+ git rev-parse $A >expect &&
+ git rev-parse $outside >actual &&
+ test_cmp expect actual &&
+ test_must_fail git reflog exists $outside
+'
+
+test_expect_success "create $m (by HEAD)" '
+ git update-ref HEAD $A &&
+ test $A = $(git show-ref -s --verify $m)
+'
+test_expect_success 'pack refs' '
+ git pack-refs --all
+'
+test_expect_success "move $m (by HEAD)" '
+ git update-ref HEAD $B $A &&
+ test $B = $(git show-ref -s --verify $m)
+'
+test_expect_success "delete $m (by HEAD) should remove both packed and loose $m" '
+ test_when_finished "git update-ref -d $m" &&
+ git update-ref -d HEAD $B &&
+ ! grep "$m" .git/packed-refs &&
+ test_must_fail git show-ref --verify -q $m
+'
+
+test_expect_success 'delete symref without dereference' '
+ test_when_finished "git update-ref -d $m" &&
+ echo foo >foo.c &&
+ git add foo.c &&
+ git commit -m foo &&
+ git symbolic-ref SYMREF $m &&
+ git update-ref --no-deref -d SYMREF &&
+ git show-ref --verify -q $m &&
+ test_must_fail git show-ref --verify -q SYMREF &&
+ test_must_fail git symbolic-ref SYMREF
+'
+
+test_expect_success 'delete symref without dereference when the referred ref is packed' '
+ test_when_finished "git update-ref -d $m" &&
+ echo foo >foo.c &&
+ git add foo.c &&
+ git commit -m foo &&
+ git symbolic-ref SYMREF $m &&
+ git pack-refs --all &&
+ git update-ref --no-deref -d SYMREF &&
+ git show-ref --verify -q $m &&
+ test_must_fail git show-ref --verify -q SYMREF &&
+ test_must_fail git symbolic-ref SYMREF
+'
+
+test_expect_success 'update-ref -d is not confused by self-reference' '
+ git symbolic-ref refs/heads/self refs/heads/self &&
+ test_when_finished "rm -f .git/refs/heads/self" &&
+ test_path_is_file .git/refs/heads/self &&
+ test_must_fail git update-ref -d refs/heads/self &&
+ test_path_is_file .git/refs/heads/self
+'
+
+test_expect_success 'update-ref --no-deref -d can delete self-reference' '
+ git symbolic-ref refs/heads/self refs/heads/self &&
+ test_when_finished "rm -f .git/refs/heads/self" &&
+ test_path_is_file .git/refs/heads/self &&
+ git update-ref --no-deref -d refs/heads/self &&
+ test_must_fail git show-ref --verify -q refs/heads/self
+'
+
+test_expect_success 'update-ref --no-deref -d can delete reference to bad ref' '
+ >.git/refs/heads/bad &&
+ test_when_finished "rm -f .git/refs/heads/bad" &&
+ git symbolic-ref refs/heads/ref-to-bad refs/heads/bad &&
+ test_when_finished "git update-ref -d refs/heads/ref-to-bad" &&
+ test_path_is_file .git/refs/heads/ref-to-bad &&
+ git update-ref --no-deref -d refs/heads/ref-to-bad &&
+ test_must_fail git show-ref --verify -q refs/heads/ref-to-bad
+'
+
+test_expect_success '(not) create HEAD with old sha1' '
+ test_must_fail git update-ref HEAD $A $B
+'
+test_expect_success "(not) prior created .git/$m" '
+ test_when_finished "git update-ref -d $m" &&
+ test_must_fail git show-ref --verify -q $m
+'
+
+test_expect_success 'create HEAD' '
+ git update-ref HEAD $A
+'
+test_expect_success '(not) change HEAD with wrong SHA1' '
+ test_must_fail git update-ref HEAD $B $Z
+'
+test_expect_success "(not) changed .git/$m" '
+ test_when_finished "git update-ref -d $m" &&
+ ! test $B = $(git show-ref -s --verify $m)
+'
+
+rm -f .git/logs/refs/heads/main
+test_expect_success "create $m (logged by touch)" '
+ test_config core.logAllRefUpdates false &&
+ GIT_COMMITTER_DATE="2005-05-26 23:30" \
+ git update-ref --create-reflog HEAD $A -m "Initial Creation" &&
+ test $A = $(git show-ref -s --verify $m)
+'
+test_expect_success "update $m (logged by touch)" '
+ test_config core.logAllRefUpdates false &&
+ GIT_COMMITTER_DATE="2005-05-26 23:31" \
+ git update-ref HEAD $B $A -m "Switch" &&
+ test $B = $(git show-ref -s --verify $m)
+'
+test_expect_success "set $m (logged by touch)" '
+ test_config core.logAllRefUpdates false &&
+ GIT_COMMITTER_DATE="2005-05-26 23:41" \
+ git update-ref HEAD $A &&
+ test $A = $(git show-ref -s --verify $m)
+'
+
+test_expect_success 'empty directory removal' '
+ git branch d1/d2/r1 HEAD &&
+ git branch d1/r2 HEAD &&
+ test_path_is_file .git/refs/heads/d1/d2/r1 &&
+ test_path_is_file .git/logs/refs/heads/d1/d2/r1 &&
+ git branch -d d1/d2/r1 &&
+ test_must_fail git show-ref --verify -q refs/heads/d1/d2 &&
+ test_must_fail git show-ref --verify -q logs/refs/heads/d1/d2 &&
+ test_path_is_file .git/refs/heads/d1/r2 &&
+ test_path_is_file .git/logs/refs/heads/d1/r2
+'
+
+test_expect_success 'symref empty directory removal' '
+ git branch e1/e2/r1 HEAD &&
+ git branch e1/r2 HEAD &&
+ git checkout e1/e2/r1 &&
+ test_when_finished "git checkout main" &&
+ test_path_is_file .git/refs/heads/e1/e2/r1 &&
+ test_path_is_file .git/logs/refs/heads/e1/e2/r1 &&
+ git update-ref -d HEAD &&
+ test_must_fail git show-ref --verify -q refs/heads/e1/e2 &&
+ test_must_fail git show-ref --verify -q logs/refs/heads/e1/e2 &&
+ test_path_is_file .git/refs/heads/e1/r2 &&
+ test_path_is_file .git/logs/refs/heads/e1/r2 &&
+ test_path_is_file .git/logs/HEAD
+'
+
+cat >expect <<EOF
+$Z $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 Initial Creation
+$A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150260 +0000 Switch
+$B $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150860 +0000
+EOF
+test_expect_success "verifying $m's log (logged by touch)" '
+ test_when_finished "git update-ref -d $m && rm -rf .git/logs actual expect" &&
+ test-tool ref-store main for-each-reflog-ent $m >actual &&
+ test_cmp actual expect
+'
+
+test_expect_success "create $m (logged by config)" '
+ test_config core.logAllRefUpdates true &&
+ GIT_COMMITTER_DATE="2005-05-26 23:32" \
+ git update-ref HEAD $A -m "Initial Creation" &&
+ test $A = $(git show-ref -s --verify $m)
+'
+test_expect_success "update $m (logged by config)" '
+ test_config core.logAllRefUpdates true &&
+ GIT_COMMITTER_DATE="2005-05-26 23:33" \
+ git update-ref HEAD $B $A -m "Switch" &&
+ test $B = $(git show-ref -s --verify $m)
+'
+test_expect_success "set $m (logged by config)" '
+ test_config core.logAllRefUpdates true &&
+ GIT_COMMITTER_DATE="2005-05-26 23:43" \
+ git update-ref HEAD $A &&
+ test $A = $(git show-ref -s --verify $m)
+'
+
+cat >expect <<EOF
+$Z $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150320 +0000 Initial Creation
+$A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150380 +0000 Switch
+$B $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150980 +0000
+EOF
+test_expect_success "verifying $m's log (logged by config)" '
+ test_when_finished "git update-ref -d $m && rm -rf .git/logs actual expect" &&
+ test-tool ref-store main for-each-reflog-ent $m >actual &&
+ test_cmp actual expect
+'
+
+test_expect_success 'set up for querying the reflog' '
+ git update-ref $m $D &&
+ cat >.git/logs/$m <<-EOF
+ $Z $C $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150320 -0500
+ $C $A $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150350 -0500
+ $A $B $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150380 -0500
+ $F $Z $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150680 -0500
+ $Z $E $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150980 -0500
+ EOF
+'
+
+ed="Thu, 26 May 2005 18:32:00 -0500"
+gd="Thu, 26 May 2005 18:33:00 -0500"
+ld="Thu, 26 May 2005 18:43:00 -0500"
+test_expect_success 'Query "main@{May 25 2005}" (before history)' '
+ test_when_finished "rm -f o e" &&
+ git rev-parse --verify "main@{May 25 2005}" >o 2>e &&
+ echo "$C" >expect &&
+ test_cmp expect o &&
+ echo "warning: log for '\''main'\'' only goes back to $ed" >expect &&
+ test_cmp expect e
+'
+test_expect_success 'Query main@{2005-05-25} (before history)' '
+ test_when_finished "rm -f o e" &&
+ git rev-parse --verify main@{2005-05-25} >o 2>e &&
+ echo "$C" >expect &&
+ test_cmp expect o &&
+ echo "warning: log for '\''main'\'' only goes back to $ed" >expect &&
+ test_cmp expect e
+'
+test_expect_success 'Query "main@{May 26 2005 23:31:59}" (1 second before history)' '
+ test_when_finished "rm -f o e" &&
+ git rev-parse --verify "main@{May 26 2005 23:31:59}" >o 2>e &&
+ echo "$C" >expect &&
+ test_cmp expect o &&
+ echo "warning: log for '\''main'\'' only goes back to $ed" >expect &&
+ test_cmp expect e
+'
+test_expect_success 'Query "main@{May 26 2005 23:32:00}" (exactly history start)' '
+ test_when_finished "rm -f o e" &&
+ git rev-parse --verify "main@{May 26 2005 23:32:00}" >o 2>e &&
+ echo "$C" >expect &&
+ test_cmp expect o &&
+ test_must_be_empty e
+'
+test_expect_success 'Query "main@{May 26 2005 23:32:30}" (first non-creation change)' '
+ test_when_finished "rm -f o e" &&
+ git rev-parse --verify "main@{May 26 2005 23:32:30}" >o 2>e &&
+ echo "$A" >expect &&
+ test_cmp expect o &&
+ test_must_be_empty e
+'
+test_expect_success 'Query "main@{2005-05-26 23:33:01}" (middle of history with gap)' '
+ test_when_finished "rm -f o e" &&
+ git rev-parse --verify "main@{2005-05-26 23:33:01}" >o 2>e &&
+ echo "$B" >expect &&
+ test_cmp expect o &&
+ test_grep -F "warning: log for ref $m has gap after $gd" e
+'
+test_expect_success 'Query "main@{2005-05-26 23:38:00}" (middle of history)' '
+ test_when_finished "rm -f o e" &&
+ git rev-parse --verify "main@{2005-05-26 23:38:00}" >o 2>e &&
+ echo "$Z" >expect &&
+ test_cmp expect o &&
+ test_must_be_empty e
+'
+test_expect_success 'Query "main@{2005-05-26 23:43:00}" (exact end of history)' '
+ test_when_finished "rm -f o e" &&
+ git rev-parse --verify "main@{2005-05-26 23:43:00}" >o 2>e &&
+ echo "$E" >expect &&
+ test_cmp expect o &&
+ test_must_be_empty e
+'
+test_expect_success 'Query "main@{2005-05-28}" (past end of history)' '
+ test_when_finished "rm -f o e" &&
+ git rev-parse --verify "main@{2005-05-28}" >o 2>e &&
+ echo "$D" >expect &&
+ test_cmp expect o &&
+ test_grep -F "warning: log for ref $m unexpectedly ended on $ld" e
+'
+
+rm -f .git/$m .git/logs/$m expect
+
+test_expect_success 'creating initial files' '
+ test_when_finished rm -f M &&
+ echo TEST >F &&
+ git add F &&
+ GIT_AUTHOR_DATE="2005-05-26 23:30" \
+ GIT_COMMITTER_DATE="2005-05-26 23:30" git commit -m add -a &&
+ h_TEST=$(git rev-parse --verify HEAD) &&
+ echo The other day this did not work. >M &&
+ echo And then Bob told me how to fix it. >>M &&
+ echo OTHER >F &&
+ GIT_AUTHOR_DATE="2005-05-26 23:41" \
+ GIT_COMMITTER_DATE="2005-05-26 23:41" git commit -F M -a &&
+ h_OTHER=$(git rev-parse --verify HEAD) &&
+ GIT_AUTHOR_DATE="2005-05-26 23:44" \
+ GIT_COMMITTER_DATE="2005-05-26 23:44" git commit --amend &&
+ h_FIXED=$(git rev-parse --verify HEAD) &&
+ echo Merged initial commit and a later commit. >M &&
+ echo $h_TEST >.git/MERGE_HEAD &&
+ GIT_AUTHOR_DATE="2005-05-26 23:45" \
+ GIT_COMMITTER_DATE="2005-05-26 23:45" git commit -F M &&
+ h_MERGED=$(git rev-parse --verify HEAD)
+'
+
+cat >expect <<EOF
+$Z $h_TEST $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 commit (initial): add
+$h_TEST $h_OTHER $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150860 +0000 commit: The other day this did not work.
+$h_OTHER $h_FIXED $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117151040 +0000 commit (amend): The other day this did not work.
+$h_FIXED $h_MERGED $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117151100 +0000 commit (merge): Merged initial commit and a later commit.
+EOF
+test_expect_success 'git commit logged updates' '
+ test-tool ref-store main for-each-reflog-ent $m >actual &&
+ test_cmp expect actual
+'
+unset h_TEST h_OTHER h_FIXED h_MERGED
+
+test_expect_success 'git cat-file blob main:F (expect OTHER)' '
+ test OTHER = $(git cat-file blob main:F)
+'
+test_expect_success 'git cat-file blob main@{2005-05-26 23:30}:F (expect TEST)' '
+ test TEST = $(git cat-file blob "main@{2005-05-26 23:30}:F")
+'
+test_expect_success 'git cat-file blob main@{2005-05-26 23:42}:F (expect OTHER)' '
+ test OTHER = $(git cat-file blob "main@{2005-05-26 23:42}:F")
+'
+
+# Test adding and deleting pseudorefs
+
+test_expect_success 'given old value for missing pseudoref, do not create' '
+ test_must_fail git update-ref PSEUDOREF $A $B 2>err &&
+ test_must_fail git rev-parse PSEUDOREF &&
+ test_grep "unable to resolve reference" err
+'
+
+test_expect_success 'create pseudoref' '
+ git update-ref PSEUDOREF $A &&
+ test $A = $(git rev-parse PSEUDOREF)
+'
+
+test_expect_success 'overwrite pseudoref with no old value given' '
+ git update-ref PSEUDOREF $B &&
+ test $B = $(git rev-parse PSEUDOREF)
+'
+
+test_expect_success 'overwrite pseudoref with correct old value' '
+ git update-ref PSEUDOREF $C $B &&
+ test $C = $(git rev-parse PSEUDOREF)
+'
+
+test_expect_success 'do not overwrite pseudoref with wrong old value' '
+ test_must_fail git update-ref PSEUDOREF $D $E 2>err &&
+ test $C = $(git rev-parse PSEUDOREF) &&
+ test_grep "cannot lock ref.*expected" err
+'
+
+test_expect_success 'delete pseudoref' '
+ git update-ref -d PSEUDOREF &&
+ test_must_fail git rev-parse PSEUDOREF
+'
+
+test_expect_success 'do not delete pseudoref with wrong old value' '
+ git update-ref PSEUDOREF $A &&
+ test_must_fail git update-ref -d PSEUDOREF $B 2>err &&
+ test $A = $(git rev-parse PSEUDOREF) &&
+ test_grep "cannot lock ref.*expected" err
+'
+
+test_expect_success 'delete pseudoref with correct old value' '
+ git update-ref -d PSEUDOREF $A &&
+ test_must_fail git rev-parse PSEUDOREF
+'
+
+test_expect_success 'create pseudoref with old OID zero' '
+ git update-ref PSEUDOREF $A $Z &&
+ test $A = $(git rev-parse PSEUDOREF)
+'
+
+test_expect_success 'do not overwrite pseudoref with old OID zero' '
+ test_when_finished git update-ref -d PSEUDOREF &&
+ test_must_fail git update-ref PSEUDOREF $B $Z 2>err &&
+ test $A = $(git rev-parse PSEUDOREF) &&
+ test_grep "already exists" err
+'
+
+# Test --stdin
+
+a=refs/heads/a
+b=refs/heads/b
+c=refs/heads/c
+E='""'
+F='%s\0'
+pws='path with space'
+
+test_expect_success 'stdin test setup' '
+ echo "$pws" >"$pws" &&
+ git add -- "$pws" &&
+ git commit -m "$pws"
+'
+
+test_expect_success '-z fails without --stdin' '
+ test_must_fail git update-ref -z $m $m $m 2>err &&
+ test_grep "usage: git update-ref" err
+'
+
+test_expect_success 'stdin works with no input' '
+ >stdin &&
+ git update-ref --stdin <stdin &&
+ git rev-parse --verify -q $m
+'
+
+test_expect_success 'stdin fails on empty line' '
+ echo "" >stdin &&
+ test_must_fail git update-ref --stdin <stdin 2>err &&
+ grep "fatal: empty command in input" err
+'
+
+test_expect_success 'stdin fails on only whitespace' '
+ echo " " >stdin &&
+ test_must_fail git update-ref --stdin <stdin 2>err &&
+ grep "fatal: whitespace before command: " err
+'
+
+test_expect_success 'stdin fails on leading whitespace' '
+ echo " create $a $m" >stdin &&
+ test_must_fail git update-ref --stdin <stdin 2>err &&
+ grep "fatal: whitespace before command: create $a $m" err
+'
+
+test_expect_success 'stdin fails on unknown command' '
+ echo "unknown $a" >stdin &&
+ test_must_fail git update-ref --stdin <stdin 2>err &&
+ grep "fatal: unknown command: unknown $a" err
+'
+
+test_expect_success 'stdin fails on unbalanced quotes' '
+ echo "create $a \"main" >stdin &&
+ test_must_fail git update-ref --stdin <stdin 2>err &&
+ grep "fatal: badly quoted argument: \\\"main" err
+'
+
+test_expect_success 'stdin fails on invalid escape' '
+ echo "create $a \"ma\zn\"" >stdin &&
+ test_must_fail git update-ref --stdin <stdin 2>err &&
+ grep "fatal: badly quoted argument: \\\"ma\\\\zn\\\"" err
+'
+
+test_expect_success 'stdin fails on junk after quoted argument' '
+ echo "create \"$a\"main" >stdin &&
+ test_must_fail git update-ref --stdin <stdin 2>err &&
+ grep "fatal: unexpected character after quoted argument: \\\"$a\\\"main" err
+'
+
+test_expect_success 'stdin fails create with no ref' '
+ echo "create " >stdin &&
+ test_must_fail git update-ref --stdin <stdin 2>err &&
+ grep "fatal: create: missing <ref>" err
+'
+
+test_expect_success 'stdin fails create with no new value' '
+ echo "create $a" >stdin &&
+ test_must_fail git update-ref --stdin <stdin 2>err &&
+ grep "fatal: create $a: missing <newvalue>" err
+'
+
+test_expect_success 'stdin fails create with too many arguments' '
+ echo "create $a $m $m" >stdin &&
+ test_must_fail git update-ref --stdin <stdin 2>err &&
+ grep "fatal: create $a: extra input: $m" err
+'
+
+test_expect_success 'stdin fails update with no ref' '
+ echo "update " >stdin &&
+ test_must_fail git update-ref --stdin <stdin 2>err &&
+ grep "fatal: update: missing <ref>" err
+'
+
+test_expect_success 'stdin fails update with no new value' '
+ echo "update $a" >stdin &&
+ test_must_fail git update-ref --stdin <stdin 2>err &&
+ grep "fatal: update $a: missing <newvalue>" err
+'
+
+test_expect_success 'stdin fails update with too many arguments' '
+ echo "update $a $m $m $m" >stdin &&
+ test_must_fail git update-ref --stdin <stdin 2>err &&
+ grep "fatal: update $a: extra input: $m" err
+'
+
+test_expect_success 'stdin fails delete with no ref' '
+ echo "delete " >stdin &&
+ test_must_fail git update-ref --stdin <stdin 2>err &&
+ grep "fatal: delete: missing <ref>" err
+'
+
+test_expect_success 'stdin fails delete with too many arguments' '
+ echo "delete $a $m $m" >stdin &&
+ test_must_fail git update-ref --stdin <stdin 2>err &&
+ grep "fatal: delete $a: extra input: $m" err
+'
+
+test_expect_success 'stdin fails verify with too many arguments' '
+ echo "verify $a $m $m" >stdin &&
+ test_must_fail git update-ref --stdin <stdin 2>err &&
+ grep "fatal: verify $a: extra input: $m" err
+'
+
+test_expect_success 'stdin fails option with unknown name' '
+ echo "option unknown" >stdin &&
+ test_must_fail git update-ref --stdin <stdin 2>err &&
+ grep "fatal: option unknown: unknown" err
+'
+
+test_expect_success 'stdin fails with duplicate refs' '
+ cat >stdin <<-EOF &&
+ create $a $m
+ create $b $m
+ create $a $m
+ EOF
+ test_must_fail git update-ref --stdin <stdin 2>err &&
+ test_grep "fatal: multiple updates for ref '"'"'$a'"'"' not allowed" err
+'
+
+test_expect_success 'stdin create ref works' '
+ echo "create $a $m" >stdin &&
+ git update-ref --stdin <stdin &&
+ git rev-parse $m >expect &&
+ git rev-parse $a >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'stdin does not create reflogs by default' '
+ test_when_finished "git update-ref -d $outside" &&
+ echo "create $outside $m" >stdin &&
+ git update-ref --stdin <stdin &&
+ git rev-parse $m >expect &&
+ git rev-parse $outside >actual &&
+ test_cmp expect actual &&
+ test_must_fail git reflog exists $outside
+'
+
+test_expect_success 'stdin creates reflogs with --create-reflog' '
+ test_when_finished "git update-ref -d $outside" &&
+ echo "create $outside $m" >stdin &&
+ git update-ref --create-reflog --stdin <stdin &&
+ git rev-parse $m >expect &&
+ git rev-parse $outside >actual &&
+ test_cmp expect actual &&
+ git reflog exists $outside
+'
+
+test_expect_success 'stdin succeeds with quoted argument' '
+ git update-ref -d $a &&
+ echo "create $a \"$m\"" >stdin &&
+ git update-ref --stdin <stdin &&
+ git rev-parse $m >expect &&
+ git rev-parse $a >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'stdin succeeds with escaped character' '
+ git update-ref -d $a &&
+ echo "create $a \"ma\\151n\"" >stdin &&
+ git update-ref --stdin <stdin &&
+ git rev-parse $m >expect &&
+ git rev-parse $a >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'stdin update ref creates with zero old value' '
+ echo "update $b $m $Z" >stdin &&
+ git update-ref --stdin <stdin &&
+ git rev-parse $m >expect &&
+ git rev-parse $b >actual &&
+ test_cmp expect actual &&
+ git update-ref -d $b
+'
+
+test_expect_success 'stdin update ref creates with empty old value' '
+ echo "update $b $m $E" >stdin &&
+ git update-ref --stdin <stdin &&
+ git rev-parse $m >expect &&
+ git rev-parse $b >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'stdin create ref works with path with space to blob' '
+ echo "create refs/blobs/pws \"$m:$pws\"" >stdin &&
+ git update-ref --stdin <stdin &&
+ git rev-parse "$m:$pws" >expect &&
+ git rev-parse refs/blobs/pws >actual &&
+ test_cmp expect actual &&
+ git update-ref -d refs/blobs/pws
+'
+
+test_expect_success 'stdin update ref fails with wrong old value' '
+ echo "update $c $m $m~1" >stdin &&
+ test_must_fail git update-ref --stdin <stdin 2>err &&
+ grep "fatal: cannot lock ref '"'"'$c'"'"'" err &&
+ test_must_fail git rev-parse --verify -q $c
+'
+
+test_expect_success 'stdin update ref fails with bad old value' '
+ echo "update $c $m does-not-exist" >stdin &&
+ test_must_fail git update-ref --stdin <stdin 2>err &&
+ grep "fatal: update $c: invalid <oldvalue>: does-not-exist" err &&
+ test_must_fail git rev-parse --verify -q $c
+'
+
+test_expect_success 'stdin create ref fails with bad new value' '
+ echo "create $c does-not-exist" >stdin &&
+ test_must_fail git update-ref --stdin <stdin 2>err &&
+ grep "fatal: create $c: invalid <newvalue>: does-not-exist" err &&
+ test_must_fail git rev-parse --verify -q $c
+'
+
+test_expect_success 'stdin create ref fails with zero new value' '
+ echo "create $c " >stdin &&
+ test_must_fail git update-ref --stdin <stdin 2>err &&
+ grep "fatal: create $c: zero <newvalue>" err &&
+ test_must_fail git rev-parse --verify -q $c
+'
+
+test_expect_success 'stdin update ref works with right old value' '
+ echo "update $b $m~1 $m" >stdin &&
+ git update-ref --stdin <stdin &&
+ git rev-parse $m~1 >expect &&
+ git rev-parse $b >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'stdin delete ref fails with wrong old value' '
+ echo "delete $a $m~1" >stdin &&
+ test_must_fail git update-ref --stdin <stdin 2>err &&
+ grep "fatal: cannot lock ref '"'"'$a'"'"'" err &&
+ git rev-parse $m >expect &&
+ git rev-parse $a >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'stdin delete ref fails with zero old value' '
+ echo "delete $a " >stdin &&
+ test_must_fail git update-ref --stdin <stdin 2>err &&
+ grep "fatal: delete $a: zero <oldvalue>" err &&
+ git rev-parse $m >expect &&
+ git rev-parse $a >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'stdin update symref works option no-deref' '
+ git symbolic-ref TESTSYMREF $b &&
+ cat >stdin <<-EOF &&
+ option no-deref
+ update TESTSYMREF $a $b
+ EOF
+ git update-ref --stdin <stdin &&
+ git rev-parse TESTSYMREF >expect &&
+ git rev-parse $a >actual &&
+ test_cmp expect actual &&
+ git rev-parse $m~1 >expect &&
+ git rev-parse $b >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'stdin delete symref works option no-deref' '
+ git symbolic-ref TESTSYMREF $b &&
+ cat >stdin <<-EOF &&
+ option no-deref
+ delete TESTSYMREF $b
+ EOF
+ git update-ref --stdin <stdin &&
+ test_must_fail git rev-parse --verify -q TESTSYMREF &&
+ git rev-parse $m~1 >expect &&
+ git rev-parse $b >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'stdin update symref works flag --no-deref' '
+ git symbolic-ref TESTSYMREFONE $b &&
+ git symbolic-ref TESTSYMREFTWO $b &&
+ cat >stdin <<-EOF &&
+ update TESTSYMREFONE $a $b
+ update TESTSYMREFTWO $a $b
+ EOF
+ git update-ref --no-deref --stdin <stdin &&
+ git rev-parse TESTSYMREFONE TESTSYMREFTWO >expect &&
+ git rev-parse $a $a >actual &&
+ test_cmp expect actual &&
+ git rev-parse $m~1 >expect &&
+ git rev-parse $b >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'stdin delete symref works flag --no-deref' '
+ git symbolic-ref TESTSYMREFONE $b &&
+ git symbolic-ref TESTSYMREFTWO $b &&
+ cat >stdin <<-EOF &&
+ delete TESTSYMREFONE $b
+ delete TESTSYMREFTWO $b
+ EOF
+ git update-ref --no-deref --stdin <stdin &&
+ test_must_fail git rev-parse --verify -q TESTSYMREFONE &&
+ test_must_fail git rev-parse --verify -q TESTSYMREFTWO &&
+ git rev-parse $m~1 >expect &&
+ git rev-parse $b >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'stdin delete ref works with right old value' '
+ echo "delete $b $m~1" >stdin &&
+ git update-ref --stdin <stdin &&
+ test_must_fail git rev-parse --verify -q $b
+'
+
+test_expect_success 'stdin update/create/verify combination works' '
+ cat >stdin <<-EOF &&
+ update $a $m
+ create $b $m
+ verify $c
+ EOF
+ git update-ref --stdin <stdin &&
+ git rev-parse $m >expect &&
+ git rev-parse $a >actual &&
+ test_cmp expect actual &&
+ git rev-parse $b >actual &&
+ test_cmp expect actual &&
+ test_must_fail git rev-parse --verify -q $c
+'
+
+test_expect_success 'stdin verify succeeds for correct value' '
+ git rev-parse $m >expect &&
+ echo "verify $m $m" >stdin &&
+ git update-ref --stdin <stdin &&
+ git rev-parse $m >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'stdin verify succeeds for missing reference' '
+ echo "verify refs/heads/missing $Z" >stdin &&
+ git update-ref --stdin <stdin &&
+ test_must_fail git rev-parse --verify -q refs/heads/missing
+'
+
+test_expect_success 'stdin verify treats no value as missing' '
+ echo "verify refs/heads/missing" >stdin &&
+ git update-ref --stdin <stdin &&
+ test_must_fail git rev-parse --verify -q refs/heads/missing
+'
+
+test_expect_success 'stdin verify fails for wrong value' '
+ git rev-parse $m >expect &&
+ echo "verify $m $m~1" >stdin &&
+ test_must_fail git update-ref --stdin <stdin &&
+ git rev-parse $m >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'stdin verify fails for mistaken null value' '
+ git rev-parse $m >expect &&
+ echo "verify $m $Z" >stdin &&
+ test_must_fail git update-ref --stdin <stdin &&
+ git rev-parse $m >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'stdin verify fails for mistaken empty value' '
+ M=$(git rev-parse $m) &&
+ test_when_finished "git update-ref $m $M" &&
+ git rev-parse $m >expect &&
+ echo "verify $m" >stdin &&
+ test_must_fail git update-ref --stdin <stdin &&
+ git rev-parse $m >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'stdin update refs works with identity updates' '
+ cat >stdin <<-EOF &&
+ update $a $m $m
+ update $b $m $m
+ update $c $Z $E
+ EOF
+ git update-ref --stdin <stdin &&
+ git rev-parse $m >expect &&
+ git rev-parse $a >actual &&
+ test_cmp expect actual &&
+ git rev-parse $b >actual &&
+ test_cmp expect actual &&
+ test_must_fail git rev-parse --verify -q $c
+'
+
+test_expect_success 'stdin update refs fails with wrong old value' '
+ git update-ref $c $m &&
+ cat >stdin <<-EOF &&
+ update $a $m $m
+ update $b $m $m
+ update $c ''
+ EOF
+ test_must_fail git update-ref --stdin <stdin 2>err &&
+ grep "fatal: cannot lock ref '"'"'$c'"'"'" err &&
+ git rev-parse $m >expect &&
+ git rev-parse $a >actual &&
+ test_cmp expect actual &&
+ git rev-parse $b >actual &&
+ test_cmp expect actual &&
+ git rev-parse $c >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'stdin delete refs works with packed and loose refs' '
+ git pack-refs --all &&
+ git update-ref $c $m~1 &&
+ cat >stdin <<-EOF &&
+ delete $a $m
+ update $b $Z $m
+ update $c $E $m~1
+ EOF
+ git update-ref --stdin <stdin &&
+ test_must_fail git rev-parse --verify -q $a &&
+ test_must_fail git rev-parse --verify -q $b &&
+ test_must_fail git rev-parse --verify -q $c
+'
+
+test_expect_success 'stdin -z works on empty input' '
+ >stdin &&
+ git update-ref -z --stdin <stdin &&
+ git rev-parse --verify -q $m
+'
+
+test_expect_success 'stdin -z fails on empty line' '
+ echo "" >stdin &&
+ test_must_fail git update-ref -z --stdin <stdin 2>err &&
+ grep "fatal: whitespace before command: " err
+'
+
+test_expect_success 'stdin -z fails on empty command' '
+ printf $F "" >stdin &&
+ test_must_fail git update-ref -z --stdin <stdin 2>err &&
+ grep "fatal: empty command in input" err
+'
+
+test_expect_success 'stdin -z fails on only whitespace' '
+ printf $F " " >stdin &&
+ test_must_fail git update-ref -z --stdin <stdin 2>err &&
+ grep "fatal: whitespace before command: " err
+'
+
+test_expect_success 'stdin -z fails on leading whitespace' '
+ printf $F " create $a" "$m" >stdin &&
+ test_must_fail git update-ref -z --stdin <stdin 2>err &&
+ grep "fatal: whitespace before command: create $a" err
+'
+
+test_expect_success 'stdin -z fails on unknown command' '
+ printf $F "unknown $a" >stdin &&
+ test_must_fail git update-ref -z --stdin <stdin 2>err &&
+ grep "fatal: unknown command: unknown $a" err
+'
+
+test_expect_success 'stdin -z fails create with no ref' '
+ printf $F "create " >stdin &&
+ test_must_fail git update-ref -z --stdin <stdin 2>err &&
+ grep "fatal: create: missing <ref>" err
+'
+
+test_expect_success 'stdin -z fails create with no new value' '
+ printf $F "create $a" >stdin &&
+ test_must_fail git update-ref -z --stdin <stdin 2>err &&
+ grep "fatal: create $a: unexpected end of input when reading <newvalue>" err
+'
+
+test_expect_success 'stdin -z fails create with too many arguments' '
+ printf $F "create $a" "$m" "$m" >stdin &&
+ test_must_fail git update-ref -z --stdin <stdin 2>err &&
+ grep "fatal: unknown command: $m" err
+'
+
+test_expect_success 'stdin -z fails update with no ref' '
+ printf $F "update " >stdin &&
+ test_must_fail git update-ref -z --stdin <stdin 2>err &&
+ grep "fatal: update: missing <ref>" err
+'
+
+test_expect_success 'stdin -z fails update with too few args' '
+ printf $F "update $a" "$m" >stdin &&
+ test_must_fail git update-ref -z --stdin <stdin 2>err &&
+ grep "fatal: update $a: unexpected end of input when reading <oldvalue>" err
+'
+
+test_expect_success 'stdin -z emits warning with empty new value' '
+ git update-ref $a $m &&
+ printf $F "update $a" "" "" >stdin &&
+ git update-ref -z --stdin <stdin 2>err &&
+ grep "warning: update $a: missing <newvalue>, treating as zero" err &&
+ test_must_fail git rev-parse --verify -q $a
+'
+
+test_expect_success 'stdin -z fails update with no new value' '
+ printf $F "update $a" >stdin &&
+ test_must_fail git update-ref -z --stdin <stdin 2>err &&
+ grep "fatal: update $a: unexpected end of input when reading <newvalue>" err
+'
+
+test_expect_success 'stdin -z fails update with no old value' '
+ printf $F "update $a" "$m" >stdin &&
+ test_must_fail git update-ref -z --stdin <stdin 2>err &&
+ grep "fatal: update $a: unexpected end of input when reading <oldvalue>" err
+'
+
+test_expect_success 'stdin -z fails update with too many arguments' '
+ printf $F "update $a" "$m" "$m" "$m" >stdin &&
+ test_must_fail git update-ref -z --stdin <stdin 2>err &&
+ grep "fatal: unknown command: $m" err
+'
+
+test_expect_success 'stdin -z fails delete with no ref' '
+ printf $F "delete " >stdin &&
+ test_must_fail git update-ref -z --stdin <stdin 2>err &&
+ grep "fatal: delete: missing <ref>" err
+'
+
+test_expect_success 'stdin -z fails delete with no old value' '
+ printf $F "delete $a" >stdin &&
+ test_must_fail git update-ref -z --stdin <stdin 2>err &&
+ grep "fatal: delete $a: unexpected end of input when reading <oldvalue>" err
+'
+
+test_expect_success 'stdin -z fails delete with too many arguments' '
+ printf $F "delete $a" "$m" "$m" >stdin &&
+ test_must_fail git update-ref -z --stdin <stdin 2>err &&
+ grep "fatal: unknown command: $m" err
+'
+
+test_expect_success 'stdin -z fails verify with too many arguments' '
+ printf $F "verify $a" "$m" "$m" >stdin &&
+ test_must_fail git update-ref -z --stdin <stdin 2>err &&
+ grep "fatal: unknown command: $m" err
+'
+
+test_expect_success 'stdin -z fails verify with no old value' '
+ printf $F "verify $a" >stdin &&
+ test_must_fail git update-ref -z --stdin <stdin 2>err &&
+ grep "fatal: verify $a: unexpected end of input when reading <oldvalue>" err
+'
+
+test_expect_success 'stdin -z fails option with unknown name' '
+ printf $F "option unknown" >stdin &&
+ test_must_fail git update-ref -z --stdin <stdin 2>err &&
+ grep "fatal: option unknown: unknown" err
+'
+
+test_expect_success 'stdin -z fails with duplicate refs' '
+ printf $F "create $a" "$m" "create $b" "$m" "create $a" "$m" >stdin &&
+ test_must_fail git update-ref -z --stdin <stdin 2>err &&
+ test_grep "fatal: multiple updates for ref '"'"'$a'"'"' not allowed" err
+'
+
+test_expect_success 'stdin -z create ref works' '
+ printf $F "create $a" "$m" >stdin &&
+ git update-ref -z --stdin <stdin &&
+ git rev-parse $m >expect &&
+ git rev-parse $a >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'stdin -z update ref creates with zero old value' '
+ printf $F "update $b" "$m" "$Z" >stdin &&
+ git update-ref -z --stdin <stdin &&
+ git rev-parse $m >expect &&
+ git rev-parse $b >actual &&
+ test_cmp expect actual &&
+ git update-ref -d $b
+'
+
+test_expect_success 'stdin -z update ref creates with empty old value' '
+ printf $F "update $b" "$m" "" >stdin &&
+ git update-ref -z --stdin <stdin &&
+ git rev-parse $m >expect &&
+ git rev-parse $b >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'stdin -z create ref works with path with space to blob' '
+ printf $F "create refs/blobs/pws" "$m:$pws" >stdin &&
+ git update-ref -z --stdin <stdin &&
+ git rev-parse "$m:$pws" >expect &&
+ git rev-parse refs/blobs/pws >actual &&
+ test_cmp expect actual &&
+ git update-ref -d refs/blobs/pws
+'
+
+test_expect_success 'stdin -z update ref fails with wrong old value' '
+ printf $F "update $c" "$m" "$m~1" >stdin &&
+ test_must_fail git update-ref -z --stdin <stdin 2>err &&
+ grep "fatal: cannot lock ref '"'"'$c'"'"'" err &&
+ test_must_fail git rev-parse --verify -q $c
+'
+
+test_expect_success 'stdin -z update ref fails with bad old value' '
+ printf $F "update $c" "$m" "does-not-exist" >stdin &&
+ test_must_fail git update-ref -z --stdin <stdin 2>err &&
+ grep "fatal: update $c: invalid <oldvalue>: does-not-exist" err &&
+ test_must_fail git rev-parse --verify -q $c
+'
+
+test_expect_success 'stdin -z create ref fails when ref exists' '
+ git update-ref $c $m &&
+ git rev-parse "$c" >expect &&
+ printf $F "create $c" "$m~1" >stdin &&
+ test_must_fail git update-ref -z --stdin <stdin 2>err &&
+ grep "fatal: cannot lock ref '"'"'$c'"'"'" err &&
+ git rev-parse "$c" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'stdin -z create ref fails with bad new value' '
+ git update-ref -d "$c" &&
+ printf $F "create $c" "does-not-exist" >stdin &&
+ test_must_fail git update-ref -z --stdin <stdin 2>err &&
+ grep "fatal: create $c: invalid <newvalue>: does-not-exist" err &&
+ test_must_fail git rev-parse --verify -q $c
+'
+
+test_expect_success 'stdin -z create ref fails with empty new value' '
+ printf $F "create $c" "" >stdin &&
+ test_must_fail git update-ref -z --stdin <stdin 2>err &&
+ grep "fatal: create $c: missing <newvalue>" err &&
+ test_must_fail git rev-parse --verify -q $c
+'
+
+test_expect_success 'stdin -z update ref works with right old value' '
+ printf $F "update $b" "$m~1" "$m" >stdin &&
+ git update-ref -z --stdin <stdin &&
+ git rev-parse $m~1 >expect &&
+ git rev-parse $b >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'stdin -z delete ref fails with wrong old value' '
+ printf $F "delete $a" "$m~1" >stdin &&
+ test_must_fail git update-ref -z --stdin <stdin 2>err &&
+ grep "fatal: cannot lock ref '"'"'$a'"'"'" err &&
+ git rev-parse $m >expect &&
+ git rev-parse $a >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'stdin -z delete ref fails with zero old value' '
+ printf $F "delete $a" "$Z" >stdin &&
+ test_must_fail git update-ref -z --stdin <stdin 2>err &&
+ grep "fatal: delete $a: zero <oldvalue>" err &&
+ git rev-parse $m >expect &&
+ git rev-parse $a >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'stdin -z update symref works option no-deref' '
+ git symbolic-ref TESTSYMREF $b &&
+ printf $F "option no-deref" "update TESTSYMREF" "$a" "$b" >stdin &&
+ git update-ref -z --stdin <stdin &&
+ git rev-parse TESTSYMREF >expect &&
+ git rev-parse $a >actual &&
+ test_cmp expect actual &&
+ git rev-parse $m~1 >expect &&
+ git rev-parse $b >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'stdin -z delete symref works option no-deref' '
+ git symbolic-ref TESTSYMREF $b &&
+ printf $F "option no-deref" "delete TESTSYMREF" "$b" >stdin &&
+ git update-ref -z --stdin <stdin &&
+ test_must_fail git rev-parse --verify -q TESTSYMREF &&
+ git rev-parse $m~1 >expect &&
+ git rev-parse $b >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'stdin -z delete ref works with right old value' '
+ printf $F "delete $b" "$m~1" >stdin &&
+ git update-ref -z --stdin <stdin &&
+ test_must_fail git rev-parse --verify -q $b
+'
+
+test_expect_success 'stdin -z update/create/verify combination works' '
+ printf $F "update $a" "$m" "" "create $b" "$m" "verify $c" "" >stdin &&
+ git update-ref -z --stdin <stdin &&
+ git rev-parse $m >expect &&
+ git rev-parse $a >actual &&
+ test_cmp expect actual &&
+ git rev-parse $b >actual &&
+ test_cmp expect actual &&
+ test_must_fail git rev-parse --verify -q $c
+'
+
+test_expect_success 'stdin -z verify succeeds for correct value' '
+ git rev-parse $m >expect &&
+ printf $F "verify $m" "$m" >stdin &&
+ git update-ref -z --stdin <stdin &&
+ git rev-parse $m >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'stdin -z verify succeeds for missing reference' '
+ printf $F "verify refs/heads/missing" "$Z" >stdin &&
+ git update-ref -z --stdin <stdin &&
+ test_must_fail git rev-parse --verify -q refs/heads/missing
+'
+
+test_expect_success 'stdin -z verify treats no value as missing' '
+ printf $F "verify refs/heads/missing" "" >stdin &&
+ git update-ref -z --stdin <stdin &&
+ test_must_fail git rev-parse --verify -q refs/heads/missing
+'
+
+test_expect_success 'stdin -z verify fails for wrong value' '
+ git rev-parse $m >expect &&
+ printf $F "verify $m" "$m~1" >stdin &&
+ test_must_fail git update-ref -z --stdin <stdin &&
+ git rev-parse $m >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'stdin -z verify fails for mistaken null value' '
+ git rev-parse $m >expect &&
+ printf $F "verify $m" "$Z" >stdin &&
+ test_must_fail git update-ref -z --stdin <stdin &&
+ git rev-parse $m >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'stdin -z verify fails for mistaken empty value' '
+ M=$(git rev-parse $m) &&
+ test_when_finished "git update-ref $m $M" &&
+ git rev-parse $m >expect &&
+ printf $F "verify $m" "" >stdin &&
+ test_must_fail git update-ref -z --stdin <stdin &&
+ git rev-parse $m >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'stdin -z update refs works with identity updates' '
+ printf $F "update $a" "$m" "$m" "update $b" "$m" "$m" "update $c" "$Z" "" >stdin &&
+ git update-ref -z --stdin <stdin &&
+ git rev-parse $m >expect &&
+ git rev-parse $a >actual &&
+ test_cmp expect actual &&
+ git rev-parse $b >actual &&
+ test_cmp expect actual &&
+ test_must_fail git rev-parse --verify -q $c
+'
+
+test_expect_success 'stdin -z update refs fails with wrong old value' '
+ git update-ref $c $m &&
+ printf $F "update $a" "$m" "$m" "update $b" "$m" "$m" "update $c" "$m" "$Z" >stdin &&
+ test_must_fail git update-ref -z --stdin <stdin 2>err &&
+ grep "fatal: cannot lock ref '"'"'$c'"'"'" err &&
+ git rev-parse $m >expect &&
+ git rev-parse $a >actual &&
+ test_cmp expect actual &&
+ git rev-parse $b >actual &&
+ test_cmp expect actual &&
+ git rev-parse $c >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'stdin -z delete refs works with packed and loose refs' '
+ git pack-refs --all &&
+ git update-ref $c $m~1 &&
+ printf $F "delete $a" "$m" "update $b" "$Z" "$m" "update $c" "" "$m~1" >stdin &&
+ git update-ref -z --stdin <stdin &&
+ test_must_fail git rev-parse --verify -q $a &&
+ test_must_fail git rev-parse --verify -q $b &&
+ test_must_fail git rev-parse --verify -q $c
+'
+
+test_expect_success 'fails with duplicate HEAD update' '
+ git branch target1 $A &&
+ git checkout target1 &&
+ cat >stdin <<-EOF &&
+ update refs/heads/target1 $C
+ option no-deref
+ update HEAD $B
+ EOF
+ test_must_fail git update-ref --stdin <stdin 2>err &&
+ test_grep "fatal: multiple updates for '\''HEAD'\'' (including one via its referent .refs/heads/target1.) are not allowed" err &&
+ echo "refs/heads/target1" >expect &&
+ git symbolic-ref HEAD >actual &&
+ test_cmp expect actual &&
+ echo "$A" >expect &&
+ git rev-parse refs/heads/target1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'fails with duplicate ref update via symref' '
+ git branch target2 $A &&
+ git symbolic-ref refs/heads/symref2 refs/heads/target2 &&
+ cat >stdin <<-EOF &&
+ update refs/heads/target2 $C
+ update refs/heads/symref2 $B
+ EOF
+ test_must_fail git update-ref --stdin <stdin 2>err &&
+ test_grep "fatal: multiple updates for '\''refs/heads/target2'\'' (including one via symref .refs/heads/symref2.) are not allowed" err &&
+ echo "refs/heads/target2" >expect &&
+ git symbolic-ref refs/heads/symref2 >actual &&
+ test_cmp expect actual &&
+ echo "$A" >expect &&
+ git rev-parse refs/heads/target2 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success ULIMIT_FILE_DESCRIPTORS 'large transaction creating branches does not burst open file limit' '
+(
+ for i in $(test_seq 33)
+ do
+ echo "create refs/heads/$i HEAD" || exit 1
+ done >large_input &&
+ run_with_limited_open_files git update-ref --stdin <large_input &&
+ git rev-parse --verify -q refs/heads/33
+)
+'
+
+test_expect_success ULIMIT_FILE_DESCRIPTORS 'large transaction deleting branches does not burst open file limit' '
+(
+ for i in $(test_seq 33)
+ do
+ echo "delete refs/heads/$i HEAD" || exit 1
+ done >large_input &&
+ run_with_limited_open_files git update-ref --stdin <large_input &&
+ test_must_fail git rev-parse --verify -q refs/heads/33
+)
+'
+
+test_expect_success 'handle per-worktree refs in refs/bisect' '
+ git commit --allow-empty -m "initial commit" &&
+ git worktree add -b branch worktree &&
+ (
+ cd worktree &&
+ git commit --allow-empty -m "test commit" &&
+ git for-each-ref >for-each-ref.out &&
+ ! grep refs/bisect for-each-ref.out &&
+ git update-ref refs/bisect/something HEAD &&
+ git rev-parse refs/bisect/something >../worktree-head &&
+ git for-each-ref | grep refs/bisect/something
+ ) &&
+ git show-ref >actual &&
+ ! grep 'refs/bisect' actual &&
+ test_must_fail git rev-parse refs/bisect/something &&
+ git update-ref refs/bisect/something HEAD &&
+ git rev-parse refs/bisect/something >main-head &&
+ ! test_cmp main-head worktree-head
+'
+
+test_expect_success 'transaction handles empty commit' '
+ cat >stdin <<-EOF &&
+ start
+ prepare
+ commit
+ EOF
+ git update-ref --stdin <stdin >actual &&
+ printf "%s: ok\n" start prepare commit >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'transaction handles empty commit with missing prepare' '
+ cat >stdin <<-EOF &&
+ start
+ commit
+ EOF
+ git update-ref --stdin <stdin >actual &&
+ printf "%s: ok\n" start commit >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'transaction handles sole commit' '
+ cat >stdin <<-EOF &&
+ commit
+ EOF
+ git update-ref --stdin <stdin >actual &&
+ printf "%s: ok\n" commit >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'transaction handles empty abort' '
+ cat >stdin <<-EOF &&
+ start
+ prepare
+ abort
+ EOF
+ git update-ref --stdin <stdin >actual &&
+ printf "%s: ok\n" start prepare abort >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'transaction exits on multiple aborts' '
+ cat >stdin <<-EOF &&
+ abort
+ abort
+ EOF
+ test_must_fail git update-ref --stdin <stdin >actual 2>err &&
+ printf "%s: ok\n" abort >expect &&
+ test_cmp expect actual &&
+ grep "fatal: transaction is closed" err
+'
+
+test_expect_success 'transaction exits on start after prepare' '
+ cat >stdin <<-EOF &&
+ prepare
+ start
+ EOF
+ test_must_fail git update-ref --stdin <stdin 2>err >actual &&
+ printf "%s: ok\n" prepare >expect &&
+ test_cmp expect actual &&
+ grep "fatal: prepared transactions can only be closed" err
+'
+
+test_expect_success 'transaction handles empty abort with missing prepare' '
+ cat >stdin <<-EOF &&
+ start
+ abort
+ EOF
+ git update-ref --stdin <stdin >actual &&
+ printf "%s: ok\n" start abort >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'transaction handles sole abort' '
+ cat >stdin <<-EOF &&
+ abort
+ EOF
+ git update-ref --stdin <stdin >actual &&
+ printf "%s: ok\n" abort >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'transaction can handle commit' '
+ cat >stdin <<-EOF &&
+ start
+ create $a HEAD
+ commit
+ EOF
+ git update-ref --stdin <stdin >actual &&
+ printf "%s: ok\n" start commit >expect &&
+ test_cmp expect actual &&
+ git rev-parse HEAD >expect &&
+ git rev-parse $a >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'transaction can handle abort' '
+ cat >stdin <<-EOF &&
+ start
+ create $b HEAD
+ abort
+ EOF
+ git update-ref --stdin <stdin >actual &&
+ printf "%s: ok\n" start abort >expect &&
+ test_cmp expect actual &&
+ test_must_fail git show-ref --verify -q $b
+'
+
+test_expect_success 'transaction aborts by default' '
+ cat >stdin <<-EOF &&
+ start
+ create $b HEAD
+ EOF
+ git update-ref --stdin <stdin >actual &&
+ printf "%s: ok\n" start >expect &&
+ test_cmp expect actual &&
+ test_must_fail git show-ref --verify -q $b
+'
+
+test_expect_success 'transaction with prepare aborts by default' '
+ cat >stdin <<-EOF &&
+ start
+ create $b HEAD
+ prepare
+ EOF
+ git update-ref --stdin <stdin >actual &&
+ printf "%s: ok\n" start prepare >expect &&
+ test_cmp expect actual &&
+ test_must_fail git show-ref --verify -q $b
+'
+
+test_expect_success 'transaction can commit multiple times' '
+ cat >stdin <<-EOF &&
+ start
+ create refs/heads/branch-1 $A
+ commit
+ start
+ create refs/heads/branch-2 $B
+ commit
+ EOF
+ git update-ref --stdin <stdin >actual &&
+ printf "%s: ok\n" start commit start commit >expect &&
+ test_cmp expect actual &&
+ echo "$A" >expect &&
+ git rev-parse refs/heads/branch-1 >actual &&
+ test_cmp expect actual &&
+ echo "$B" >expect &&
+ git rev-parse refs/heads/branch-2 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'transaction can create and delete' '
+ cat >stdin <<-EOF &&
+ start
+ create refs/heads/create-and-delete $A
+ commit
+ start
+ delete refs/heads/create-and-delete $A
+ commit
+ EOF
+ git update-ref --stdin <stdin >actual &&
+ printf "%s: ok\n" start commit start commit >expect &&
+ test_cmp expect actual &&
+ test_must_fail git show-ref --verify refs/heads/create-and-delete
+'
+
+test_expect_success 'transaction can commit after abort' '
+ cat >stdin <<-EOF &&
+ start
+ create refs/heads/abort $A
+ abort
+ start
+ create refs/heads/abort $A
+ commit
+ EOF
+ git update-ref --stdin <stdin >actual &&
+ printf "%s: ok\n" start abort start commit >expect &&
+ echo "$A" >expect &&
+ git rev-parse refs/heads/abort >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'transaction cannot restart ongoing transaction' '
+ cat >stdin <<-EOF &&
+ start
+ create refs/heads/restart $A
+ start
+ commit
+ EOF
+ test_must_fail git update-ref --stdin <stdin >actual &&
+ printf "%s: ok\n" start >expect &&
+ test_cmp expect actual &&
+ test_must_fail git show-ref --verify refs/heads/restart
+'
+
+test_expect_success PIPE 'transaction flushes status updates' '
+ mkfifo in out &&
+ (git update-ref --stdin <in >out &) &&
+
+ exec 9>in &&
+ exec 8<out &&
+ test_when_finished "exec 9>&-" &&
+ test_when_finished "exec 8<&-" &&
+
+ echo "start" >&9 &&
+ echo "start: ok" >expected &&
+ read line <&8 &&
+ echo "$line" >actual &&
+ test_cmp expected actual &&
+
+ echo "create refs/heads/flush $A" >&9 &&
+
+ echo prepare >&9 &&
+ echo "prepare: ok" >expected &&
+ read line <&8 &&
+ echo "$line" >actual &&
+ test_cmp expected actual &&
+
+ # This must now fail given that we have locked the ref.
+ test_must_fail git update-ref refs/heads/flush $B 2>stderr &&
+ grep "fatal: update_ref failed for ref ${SQ}refs/heads/flush${SQ}: cannot lock ref" stderr &&
+
+ echo commit >&9 &&
+ echo "commit: ok" >expected &&
+ read line <&8 &&
+ echo "$line" >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'directory not created deleting packed ref' '
+ git branch d1/d2/r1 HEAD &&
+ git pack-refs --all &&
+ test_path_is_missing .git/refs/heads/d1/d2 &&
+ git update-ref -d refs/heads/d1/d2/r1 &&
+ test_path_is_missing .git/refs/heads/d1/d2 &&
+ test_path_is_missing .git/refs/heads/d1
+'
+
+test_done
diff --git a/t/t1401-symbolic-ref.sh b/t/t1401-symbolic-ref.sh
new file mode 100755
index 0000000..c7745e1
--- /dev/null
+++ b/t/t1401-symbolic-ref.sh
@@ -0,0 +1,227 @@
+#!/bin/sh
+
+test_description='basic symbolic-ref tests'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# If the tests munging HEAD fail, they can break detection of
+# the git repo, meaning that further tests will operate on
+# the surrounding git repo instead of the trash directory.
+reset_to_sane() {
+ rm -rf .git &&
+ "$TAR" xf .git.tar
+}
+
+test_expect_success 'setup' '
+ git symbolic-ref HEAD refs/heads/foo &&
+ test_commit file &&
+ "$TAR" cf .git.tar .git/
+'
+
+test_expect_success 'symbolic-ref read/write roundtrip' '
+ git symbolic-ref HEAD refs/heads/read-write-roundtrip &&
+ echo refs/heads/read-write-roundtrip >expect &&
+ git symbolic-ref HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'symbolic-ref refuses non-ref for HEAD' '
+ test_must_fail git symbolic-ref HEAD foo
+'
+
+reset_to_sane
+
+test_expect_success 'symbolic-ref refuses bare sha1' '
+ rev=$(git rev-parse HEAD) &&
+ test_must_fail git symbolic-ref HEAD "$rev"
+'
+
+reset_to_sane
+
+test_expect_success 'HEAD cannot be removed' '
+ test_must_fail git symbolic-ref -d HEAD
+'
+
+reset_to_sane
+
+test_expect_success 'symbolic-ref can be deleted' '
+ git symbolic-ref NOTHEAD refs/heads/foo &&
+ git symbolic-ref -d NOTHEAD &&
+ git rev-parse refs/heads/foo &&
+ test_must_fail git symbolic-ref NOTHEAD
+'
+reset_to_sane
+
+test_expect_success 'symbolic-ref can delete dangling symref' '
+ git symbolic-ref NOTHEAD refs/heads/missing &&
+ git symbolic-ref -d NOTHEAD &&
+ test_must_fail git rev-parse refs/heads/missing &&
+ test_must_fail git symbolic-ref NOTHEAD
+'
+reset_to_sane
+
+test_expect_success 'symbolic-ref fails to delete missing FOO' '
+ echo "fatal: Cannot delete FOO, not a symbolic ref" >expect &&
+ test_must_fail git symbolic-ref -d FOO >actual 2>&1 &&
+ test_cmp expect actual
+'
+reset_to_sane
+
+test_expect_success 'symbolic-ref fails to delete real ref' '
+ echo "fatal: Cannot delete refs/heads/foo, not a symbolic ref" >expect &&
+ test_must_fail git symbolic-ref -d refs/heads/foo >actual 2>&1 &&
+ git rev-parse --verify refs/heads/foo &&
+ test_cmp expect actual
+'
+reset_to_sane
+
+test_expect_success 'create large ref name' '
+ # make 256+ character ref; some systems may not handle that,
+ # so be gentle
+ long=0123456789abcdef &&
+ long=$long/$long/$long/$long &&
+ long=$long/$long/$long/$long &&
+ long_ref=refs/heads/$long &&
+ tree=$(git write-tree) &&
+ commit=$(echo foo | git commit-tree $tree) &&
+ if git update-ref $long_ref $commit; then
+ test_set_prereq LONG_REF
+ else
+ echo >&2 "long refs not supported"
+ fi
+'
+
+test_expect_success LONG_REF 'symbolic-ref can point to large ref name' '
+ git symbolic-ref HEAD $long_ref &&
+ echo $long_ref >expect &&
+ git symbolic-ref HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success LONG_REF 'we can parse long symbolic ref' '
+ echo $commit >expect &&
+ git rev-parse --verify HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'symbolic-ref reports failure in exit code' '
+ test_when_finished "rm -f .git/HEAD.lock" &&
+ >.git/HEAD.lock &&
+ test_must_fail git symbolic-ref HEAD refs/heads/whatever
+'
+
+test_expect_success 'symbolic-ref writes reflog entry' '
+ git checkout -b log1 &&
+ test_commit one &&
+ git checkout -b log2 &&
+ test_commit two &&
+ git checkout --orphan orphan &&
+ git symbolic-ref -m create HEAD refs/heads/log1 &&
+ git symbolic-ref -m update HEAD refs/heads/log2 &&
+ cat >expect <<-\EOF &&
+ update
+ create
+ EOF
+ git log --format=%gs -g -2 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'symbolic-ref does not create ref d/f conflicts' '
+ git checkout -b df &&
+ test_commit df &&
+ test_must_fail git symbolic-ref refs/heads/df/conflict refs/heads/df &&
+ git pack-refs --all --prune &&
+ test_must_fail git symbolic-ref refs/heads/df/conflict refs/heads/df
+'
+
+test_expect_success 'symbolic-ref can overwrite pointer to invalid name' '
+ test_when_finished reset_to_sane &&
+ head=$(git rev-parse HEAD) &&
+ git symbolic-ref HEAD refs/heads/outer &&
+ test_when_finished "git update-ref -d refs/heads/outer/inner" &&
+ git update-ref refs/heads/outer/inner $head &&
+ git symbolic-ref HEAD refs/heads/unrelated
+'
+
+test_expect_success 'symbolic-ref can resolve d/f name (EISDIR)' '
+ test_when_finished reset_to_sane &&
+ head=$(git rev-parse HEAD) &&
+ git symbolic-ref HEAD refs/heads/outer/inner &&
+ test_when_finished "git update-ref -d refs/heads/outer" &&
+ git update-ref refs/heads/outer $head &&
+ echo refs/heads/outer/inner >expect &&
+ git symbolic-ref HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'symbolic-ref can resolve d/f name (ENOTDIR)' '
+ test_when_finished reset_to_sane &&
+ head=$(git rev-parse HEAD) &&
+ git symbolic-ref HEAD refs/heads/outer &&
+ test_when_finished "git update-ref -d refs/heads/outer/inner" &&
+ git update-ref refs/heads/outer/inner $head &&
+ echo refs/heads/outer >expect &&
+ git symbolic-ref HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'symbolic-ref refuses invalid target for non-HEAD' '
+ test_must_fail git symbolic-ref refs/heads/invalid foo..bar
+'
+
+test_expect_success 'symbolic-ref allows top-level target for non-HEAD' '
+ git symbolic-ref refs/heads/top-level FETCH_HEAD &&
+ git update-ref FETCH_HEAD HEAD &&
+ test_cmp_rev top-level HEAD
+'
+
+test_expect_success 'symbolic-ref pointing at another' '
+ git update-ref refs/heads/maint-2.37 HEAD &&
+ git symbolic-ref refs/heads/maint refs/heads/maint-2.37 &&
+ git checkout maint &&
+
+ git symbolic-ref HEAD >actual &&
+ echo refs/heads/maint-2.37 >expect &&
+ test_cmp expect actual &&
+
+ git symbolic-ref --no-recurse HEAD >actual &&
+ echo refs/heads/maint >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'symbolic-ref --short handles complex utf8 case' '
+ name="测试-加-增加-加-增加" &&
+ git symbolic-ref TEST_SYMREF "refs/heads/$name" &&
+ # In the real world, we saw problems with this case only
+ # when the locale includes UTF-8. Set it here to try to make things as
+ # hard as possible for us to pass, but in practice we should do the
+ # right thing regardless (and of course some platforms may not even
+ # have this locale).
+ LC_ALL=en_US.UTF-8 git symbolic-ref --short TEST_SYMREF >actual &&
+ echo "$name" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'symbolic-ref --short handles name with suffix' '
+ git symbolic-ref TEST_SYMREF "refs/remotes/origin/HEAD" &&
+ git symbolic-ref --short TEST_SYMREF >actual &&
+ echo "origin" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'symbolic-ref --short handles almost-matching name' '
+ git symbolic-ref TEST_SYMREF "refs/headsXfoo" &&
+ git symbolic-ref --short TEST_SYMREF >actual &&
+ echo "headsXfoo" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'symbolic-ref --short handles name with percent' '
+ git symbolic-ref TEST_SYMREF "refs/heads/%foo" &&
+ git symbolic-ref --short TEST_SYMREF >actual &&
+ echo "%foo" >expect &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t1402-check-ref-format.sh b/t/t1402-check-ref-format.sh
new file mode 100755
index 0000000..5ed9d73
--- /dev/null
+++ b/t/t1402-check-ref-format.sh
@@ -0,0 +1,219 @@
+#!/bin/sh
+
+test_description='Test git check-ref-format'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+valid_ref() {
+ prereq=
+ case $1 in
+ [A-Z!]*)
+ prereq=$1
+ shift
+ esac
+ desc="ref name '$1' is valid${2:+ with options $2}"
+ test_expect_success $prereq "$desc" "
+ git check-ref-format $2 '$1'
+ "
+}
+invalid_ref() {
+ prereq=
+ case $1 in
+ [A-Z!]*)
+ prereq=$1
+ shift
+ esac
+ desc="ref name '$1' is invalid${2:+ with options $2}"
+ test_expect_success $prereq "$desc" "
+ test_must_fail git check-ref-format $2 '$1'
+ "
+}
+
+invalid_ref ''
+invalid_ref !MINGW '/'
+invalid_ref !MINGW '/' --allow-onelevel
+invalid_ref !MINGW '/' --normalize
+invalid_ref !MINGW '/' '--allow-onelevel --normalize'
+valid_ref 'foo/bar/baz'
+valid_ref 'foo/bar/baz' --normalize
+invalid_ref 'refs///heads/foo'
+valid_ref 'refs///heads/foo' --normalize
+invalid_ref 'heads/foo/'
+invalid_ref !MINGW '/heads/foo'
+valid_ref !MINGW '/heads/foo' --normalize
+invalid_ref '///heads/foo'
+valid_ref '///heads/foo' --normalize
+invalid_ref './foo'
+invalid_ref './foo/bar'
+invalid_ref 'foo/./bar'
+invalid_ref 'foo/bar/.'
+invalid_ref '.refs/foo'
+invalid_ref 'refs/heads/foo.'
+invalid_ref 'heads/foo..bar'
+invalid_ref 'heads/foo?bar'
+valid_ref 'foo./bar'
+invalid_ref 'heads/foo.lock'
+invalid_ref 'heads///foo.lock'
+invalid_ref 'foo.lock/bar'
+invalid_ref 'foo.lock///bar'
+valid_ref 'heads/foo@bar'
+invalid_ref 'heads/v@{ation'
+invalid_ref 'heads/foo\bar'
+invalid_ref "$(printf 'heads/foo\t')"
+invalid_ref "$(printf 'heads/foo\177')"
+valid_ref "$(printf 'heads/fu\303\237')"
+valid_ref 'heads/*foo/bar' --refspec-pattern
+valid_ref 'heads/foo*/bar' --refspec-pattern
+valid_ref 'heads/f*o/bar' --refspec-pattern
+invalid_ref 'heads/f*o*/bar' --refspec-pattern
+invalid_ref 'heads/foo*/bar*' --refspec-pattern
+
+ref='foo'
+invalid_ref "$ref"
+valid_ref "$ref" --allow-onelevel
+invalid_ref "$ref" --refspec-pattern
+valid_ref "$ref" '--refspec-pattern --allow-onelevel'
+invalid_ref "$ref" --normalize
+valid_ref "$ref" '--allow-onelevel --normalize'
+
+ref='foo/bar'
+valid_ref "$ref"
+valid_ref "$ref" --allow-onelevel
+valid_ref "$ref" --refspec-pattern
+valid_ref "$ref" '--refspec-pattern --allow-onelevel'
+valid_ref "$ref" --normalize
+
+ref='foo/*'
+invalid_ref "$ref"
+invalid_ref "$ref" --allow-onelevel
+valid_ref "$ref" --refspec-pattern
+valid_ref "$ref" '--refspec-pattern --allow-onelevel'
+
+ref='*/foo'
+invalid_ref "$ref"
+invalid_ref "$ref" --allow-onelevel
+valid_ref "$ref" --refspec-pattern
+valid_ref "$ref" '--refspec-pattern --allow-onelevel'
+invalid_ref "$ref" --normalize
+valid_ref "$ref" '--refspec-pattern --normalize'
+
+ref='foo/*/bar'
+invalid_ref "$ref"
+invalid_ref "$ref" --allow-onelevel
+valid_ref "$ref" --refspec-pattern
+valid_ref "$ref" '--refspec-pattern --allow-onelevel'
+
+ref='*'
+invalid_ref "$ref"
+invalid_ref "$ref" --allow-onelevel
+invalid_ref "$ref" --refspec-pattern
+valid_ref "$ref" '--refspec-pattern --allow-onelevel'
+
+ref='foo/*/*'
+invalid_ref "$ref" --refspec-pattern
+invalid_ref "$ref" '--refspec-pattern --allow-onelevel'
+
+ref='*/foo/*'
+invalid_ref "$ref" --refspec-pattern
+invalid_ref "$ref" '--refspec-pattern --allow-onelevel'
+
+ref='*/*/foo'
+invalid_ref "$ref" --refspec-pattern
+invalid_ref "$ref" '--refspec-pattern --allow-onelevel'
+
+ref='/foo'
+invalid_ref !MINGW "$ref"
+invalid_ref !MINGW "$ref" --allow-onelevel
+invalid_ref !MINGW "$ref" --refspec-pattern
+invalid_ref !MINGW "$ref" '--refspec-pattern --allow-onelevel'
+invalid_ref !MINGW "$ref" --normalize
+valid_ref !MINGW "$ref" '--allow-onelevel --normalize'
+invalid_ref !MINGW "$ref" '--refspec-pattern --normalize'
+valid_ref !MINGW "$ref" '--refspec-pattern --allow-onelevel --normalize'
+
+test_expect_success "check-ref-format --branch @{-1}" '
+ T=$(git write-tree) &&
+ sha1=$(echo A | git commit-tree $T) &&
+ git update-ref refs/heads/main $sha1 &&
+ git update-ref refs/remotes/origin/main $sha1 &&
+ git checkout main &&
+ git checkout origin/main &&
+ git checkout main &&
+ refname=$(git check-ref-format --branch @{-1}) &&
+ test "$refname" = "$sha1" &&
+ refname2=$(git check-ref-format --branch @{-2}) &&
+ test "$refname2" = main'
+
+test_expect_success 'check-ref-format --branch -nain' '
+ test_must_fail git check-ref-format --branch -nain >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'check-ref-format --branch from subdir' '
+ mkdir subdir &&
+
+ T=$(git write-tree) &&
+ sha1=$(echo A | git commit-tree $T) &&
+ git update-ref refs/heads/main $sha1 &&
+ git update-ref refs/remotes/origin/main $sha1 &&
+ git checkout main &&
+ git checkout origin/main &&
+ git checkout main &&
+ refname=$(
+ cd subdir &&
+ git check-ref-format --branch @{-1}
+ ) &&
+ test "$refname" = "$sha1"
+'
+
+test_expect_success 'check-ref-format --branch @{-1} from non-repo' '
+ nongit test_must_fail git check-ref-format --branch @{-1} >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'check-ref-format --branch main from non-repo' '
+ echo main >expect &&
+ nongit git check-ref-format --branch main >actual &&
+ test_cmp expect actual
+'
+
+valid_ref_normalized() {
+ prereq=
+ case $1 in
+ [A-Z!]*)
+ prereq=$1
+ shift
+ esac
+ test_expect_success $prereq "ref name '$1' simplifies to '$2'" "
+ refname=\$(git check-ref-format --normalize '$1') &&
+ test \"\$refname\" = '$2'
+ "
+}
+invalid_ref_normalized() {
+ prereq=
+ case $1 in
+ [A-Z!]*)
+ prereq=$1
+ shift
+ esac
+ test_expect_success $prereq "check-ref-format --normalize rejects '$1'" "
+ test_must_fail git check-ref-format --normalize '$1'
+ "
+}
+
+valid_ref_normalized 'heads/foo' 'heads/foo'
+valid_ref_normalized 'refs///heads/foo' 'refs/heads/foo'
+valid_ref_normalized !MINGW '/heads/foo' 'heads/foo'
+valid_ref_normalized '///heads/foo' 'heads/foo'
+invalid_ref_normalized 'foo'
+invalid_ref_normalized !MINGW '/foo'
+invalid_ref_normalized 'heads/foo/../bar'
+invalid_ref_normalized 'heads/./foo'
+invalid_ref_normalized 'heads\foo'
+invalid_ref_normalized 'heads/foo.lock'
+invalid_ref_normalized 'heads///foo.lock'
+invalid_ref_normalized 'foo.lock/bar'
+invalid_ref_normalized 'foo.lock///bar'
+
+test_done
diff --git a/t/t1403-show-ref.sh b/t/t1403-show-ref.sh
new file mode 100755
index 0000000..b50ae6f
--- /dev/null
+++ b/t/t1403-show-ref.sh
@@ -0,0 +1,269 @@
+#!/bin/sh
+
+test_description='show-ref'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ test_commit --annotate A &&
+ git checkout -b side &&
+ test_commit --annotate B &&
+ git checkout main &&
+ test_commit C &&
+ git branch B A^0
+'
+
+test_expect_success 'show-ref' '
+ echo $(git rev-parse refs/tags/A) refs/tags/A >expect &&
+
+ git show-ref A >actual &&
+ test_cmp expect actual &&
+
+ git show-ref tags/A >actual &&
+ test_cmp expect actual &&
+
+ git show-ref refs/tags/A >actual &&
+ test_cmp expect actual &&
+
+ test_must_fail git show-ref D >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'show-ref -q' '
+ git show-ref -q A >actual &&
+ test_must_be_empty actual &&
+
+ git show-ref -q tags/A >actual &&
+ test_must_be_empty actual &&
+
+ git show-ref -q refs/tags/A >actual &&
+ test_must_be_empty actual &&
+
+ test_must_fail git show-ref -q D >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'show-ref --verify' '
+ echo $(git rev-parse refs/tags/A) refs/tags/A >expect &&
+
+ git show-ref --verify refs/tags/A >actual &&
+ test_cmp expect actual &&
+
+ test_must_fail git show-ref --verify A >actual &&
+ test_must_be_empty actual &&
+
+ test_must_fail git show-ref --verify tags/A >actual &&
+ test_must_be_empty actual &&
+
+ test_must_fail git show-ref --verify D >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'show-ref --verify -q' '
+ git show-ref --verify -q refs/tags/A >actual &&
+ test_must_be_empty actual &&
+
+ test_must_fail git show-ref --verify -q A >actual &&
+ test_must_be_empty actual &&
+
+ test_must_fail git show-ref --verify -q tags/A >actual &&
+ test_must_be_empty actual &&
+
+ test_must_fail git show-ref --verify -q D >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'show-ref -d' '
+ {
+ echo $(git rev-parse refs/tags/A) refs/tags/A &&
+ echo $(git rev-parse refs/tags/A^0) "refs/tags/A^{}" &&
+ echo $(git rev-parse refs/tags/C) refs/tags/C
+ } >expect &&
+ git show-ref -d A C >actual &&
+ test_cmp expect actual &&
+
+ git show-ref -d tags/A tags/C >actual &&
+ test_cmp expect actual &&
+
+ git show-ref -d refs/tags/A refs/tags/C >actual &&
+ test_cmp expect actual &&
+
+ git show-ref --verify -d refs/tags/A refs/tags/C >actual &&
+ test_cmp expect actual &&
+
+ echo $(git rev-parse refs/heads/main) refs/heads/main >expect &&
+ git show-ref -d main >actual &&
+ test_cmp expect actual &&
+
+ git show-ref -d heads/main >actual &&
+ test_cmp expect actual &&
+
+ git show-ref -d refs/heads/main >actual &&
+ test_cmp expect actual &&
+
+ git show-ref -d --verify refs/heads/main >actual &&
+ test_cmp expect actual &&
+
+ test_must_fail git show-ref -d --verify main >actual &&
+ test_must_be_empty actual &&
+
+ test_must_fail git show-ref -d --verify heads/main >actual &&
+ test_must_be_empty actual &&
+
+ test_must_fail git show-ref --verify -d A C >actual &&
+ test_must_be_empty actual &&
+
+ test_must_fail git show-ref --verify -d tags/A tags/C >actual &&
+ test_must_be_empty actual
+
+'
+
+test_expect_success 'show-ref --heads, --tags, --head, pattern' '
+ for branch in B main side
+ do
+ echo $(git rev-parse refs/heads/$branch) refs/heads/$branch || return 1
+ done >expect.heads &&
+ git show-ref --heads >actual &&
+ test_cmp expect.heads actual &&
+
+ for tag in A B C
+ do
+ echo $(git rev-parse refs/tags/$tag) refs/tags/$tag || return 1
+ done >expect.tags &&
+ git show-ref --tags >actual &&
+ test_cmp expect.tags actual &&
+
+ cat expect.heads expect.tags >expect &&
+ git show-ref --heads --tags >actual &&
+ test_cmp expect actual &&
+
+ {
+ echo $(git rev-parse HEAD) HEAD &&
+ cat expect.heads expect.tags
+ } >expect &&
+ git show-ref --heads --tags --head >actual &&
+ test_cmp expect actual &&
+
+ {
+ echo $(git rev-parse HEAD) HEAD &&
+ echo $(git rev-parse refs/heads/B) refs/heads/B &&
+ echo $(git rev-parse refs/tags/B) refs/tags/B
+ } >expect &&
+ git show-ref --head B >actual &&
+ test_cmp expect actual &&
+
+ {
+ echo $(git rev-parse HEAD) HEAD &&
+ echo $(git rev-parse refs/heads/B) refs/heads/B &&
+ echo $(git rev-parse refs/tags/B) refs/tags/B &&
+ echo $(git rev-parse refs/tags/B^0) "refs/tags/B^{}"
+ } >expect &&
+ git show-ref --head -d B >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'show-ref --verify HEAD' '
+ echo $(git rev-parse HEAD) HEAD >expect &&
+ git show-ref --verify HEAD >actual &&
+ test_cmp expect actual &&
+
+ git show-ref --verify -q HEAD >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'show-ref --verify with dangling ref' '
+ sha1_file() {
+ echo "$*" | sed "s#..#.git/objects/&/#"
+ } &&
+
+ remove_object() {
+ file=$(sha1_file "$*") &&
+ test -e "$file" &&
+ rm -f "$file"
+ } &&
+
+ test_when_finished "rm -rf dangling" &&
+ (
+ git init dangling &&
+ cd dangling &&
+ test_commit dangling &&
+ sha=$(git rev-parse refs/tags/dangling) &&
+ remove_object $sha &&
+ test_must_fail git show-ref --verify refs/tags/dangling
+ )
+'
+
+test_expect_success 'show-ref sub-modes are mutually exclusive' '
+ cat >expect <<-EOF &&
+ fatal: only one of ${SQ}--exclude-existing${SQ}, ${SQ}--verify${SQ} or ${SQ}--exists${SQ} can be given
+ EOF
+
+ test_must_fail git show-ref --verify --exclude-existing 2>err &&
+ test_cmp expect err &&
+
+ test_must_fail git show-ref --verify --exists 2>err &&
+ test_cmp expect err &&
+
+ test_must_fail git show-ref --exclude-existing --exists 2>err &&
+ test_cmp expect err
+'
+
+test_expect_success '--exists with existing reference' '
+ git show-ref --exists refs/heads/$GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+'
+
+test_expect_success '--exists with missing reference' '
+ test_expect_code 2 git show-ref --exists refs/heads/does-not-exist
+'
+
+test_expect_success '--exists does not use DWIM' '
+ test_expect_code 2 git show-ref --exists $GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME 2>err &&
+ grep "reference does not exist" err
+'
+
+test_expect_success '--exists with HEAD' '
+ git show-ref --exists HEAD
+'
+
+test_expect_success '--exists with bad reference name' '
+ test_when_finished "git update-ref -d refs/heads/bad...name" &&
+ new_oid=$(git rev-parse HEAD) &&
+ test-tool ref-store main update-ref msg refs/heads/bad...name $new_oid $ZERO_OID REF_SKIP_REFNAME_VERIFICATION &&
+ git show-ref --exists refs/heads/bad...name
+'
+
+test_expect_success '--exists with arbitrary symref' '
+ test_when_finished "git symbolic-ref -d refs/symref" &&
+ git symbolic-ref refs/symref refs/heads/$GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME &&
+ git show-ref --exists refs/symref
+'
+
+test_expect_success '--exists with dangling symref' '
+ test_when_finished "git symbolic-ref -d refs/heads/dangling" &&
+ git symbolic-ref refs/heads/dangling refs/heads/does-not-exist &&
+ git show-ref --exists refs/heads/dangling
+'
+
+test_expect_success '--exists with nonexistent object ID' '
+ test-tool ref-store main update-ref msg refs/heads/missing-oid $(test_oid 001) $ZERO_OID REF_SKIP_OID_VERIFICATION &&
+ git show-ref --exists refs/heads/missing-oid
+'
+
+test_expect_success '--exists with non-commit object' '
+ tree_oid=$(git rev-parse HEAD^{tree}) &&
+ test-tool ref-store main update-ref msg refs/heads/tree ${tree_oid} $ZERO_OID REF_SKIP_OID_VERIFICATION &&
+ git show-ref --exists refs/heads/tree
+'
+
+test_expect_success '--exists with directory fails with generic error' '
+ cat >expect <<-EOF &&
+ error: failed to look up reference: Is a directory
+ EOF
+ test_expect_code 1 git show-ref --exists refs/heads 2>err &&
+ test_cmp expect err
+'
+
+test_done
diff --git a/t/t1404-update-ref-errors.sh b/t/t1404-update-ref-errors.sh
new file mode 100755
index 0000000..0369bee
--- /dev/null
+++ b/t/t1404-update-ref-errors.sh
@@ -0,0 +1,636 @@
+#!/bin/sh
+
+test_description='Test git update-ref error handling'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# Create some references, perhaps run pack-refs --all, then try to
+# create some more references. Ensure that the second creation fails
+# with the correct error message.
+# Usage: test_update_rejected <before> <pack> <create> <error>
+# <before> is a ws-separated list of refs to create before the test
+# <pack> (true or false) tells whether to pack the refs before the test
+# <create> is a list of variables to attempt creating
+# <error> is a string to look for in the stderr of update-ref.
+# All references are created in the namespace specified by the current
+# value of $prefix.
+test_update_rejected () {
+ before="$1" &&
+ pack="$2" &&
+ create="$3" &&
+ error="$4" &&
+ printf "create $prefix/%s $C\n" $before |
+ git update-ref --stdin &&
+ git for-each-ref $prefix >unchanged &&
+ if $pack
+ then
+ git pack-refs --all
+ fi &&
+ printf "create $prefix/%s $C\n" $create >input &&
+ test_must_fail git update-ref --stdin <input 2>output.err &&
+ test_grep -F "$error" output.err &&
+ git for-each-ref $prefix >actual &&
+ test_cmp unchanged actual
+}
+
+# Test adding and deleting D/F-conflicting references in a single
+# transaction.
+df_test() {
+ prefix="$1"
+ pack=: symadd=false symdel=false add_del=false addref= delref=
+ shift
+ while test $# -gt 0
+ do
+ case "$1" in
+ --pack)
+ pack="git pack-refs --all"
+ shift
+ ;;
+ --sym-add)
+ # Perform the add via a symbolic reference
+ symadd=true
+ shift
+ ;;
+ --sym-del)
+ # Perform the del via a symbolic reference
+ symdel=true
+ shift
+ ;;
+ --del-add)
+ # Delete first reference then add second
+ add_del=false
+ delref="$prefix/r/$2"
+ addref="$prefix/r/$3"
+ shift 3
+ ;;
+ --add-del)
+ # Add first reference then delete second
+ add_del=true
+ addref="$prefix/r/$2"
+ delref="$prefix/r/$3"
+ shift 3
+ ;;
+ *)
+ echo 1>&2 "Extra args to df_test: $*"
+ return 1
+ ;;
+ esac
+ done
+ git update-ref "$delref" $C &&
+ if $symadd
+ then
+ addname="$prefix/s/symadd" &&
+ git symbolic-ref "$addname" "$addref"
+ else
+ addname="$addref"
+ fi &&
+ if $symdel
+ then
+ delname="$prefix/s/symdel" &&
+ git symbolic-ref "$delname" "$delref"
+ else
+ delname="$delref"
+ fi &&
+ cat >expected-err <<-EOF &&
+ fatal: cannot lock ref $SQ$addname$SQ: $SQ$delref$SQ exists; cannot create $SQ$addref$SQ
+ EOF
+ $pack &&
+ if $add_del
+ then
+ printf "%s\n" "create $addname $D" "delete $delname"
+ else
+ printf "%s\n" "delete $delname" "create $addname $D"
+ fi >commands &&
+ test_must_fail git update-ref --stdin <commands 2>output.err &&
+ test_cmp expected-err output.err &&
+ printf "%s\n" "$C $delref" >expected-refs &&
+ git for-each-ref --format="%(objectname) %(refname)" $prefix/r >actual-refs &&
+ test_cmp expected-refs actual-refs
+}
+
+test_expect_success 'setup' '
+
+ git commit --allow-empty -m Initial &&
+ C=$(git rev-parse HEAD) &&
+ git commit --allow-empty -m Second &&
+ D=$(git rev-parse HEAD) &&
+ git commit --allow-empty -m Third &&
+ E=$(git rev-parse HEAD)
+'
+
+test_expect_success 'existing loose ref is a simple prefix of new' '
+
+ prefix=refs/1l &&
+ test_update_rejected "a c e" false "b c/x d" \
+ "$SQ$prefix/c$SQ exists; cannot create $SQ$prefix/c/x$SQ"
+
+'
+
+test_expect_success 'existing packed ref is a simple prefix of new' '
+
+ prefix=refs/1p &&
+ test_update_rejected "a c e" true "b c/x d" \
+ "$SQ$prefix/c$SQ exists; cannot create $SQ$prefix/c/x$SQ"
+
+'
+
+test_expect_success 'existing loose ref is a deeper prefix of new' '
+
+ prefix=refs/2l &&
+ test_update_rejected "a c e" false "b c/x/y d" \
+ "$SQ$prefix/c$SQ exists; cannot create $SQ$prefix/c/x/y$SQ"
+
+'
+
+test_expect_success 'existing packed ref is a deeper prefix of new' '
+
+ prefix=refs/2p &&
+ test_update_rejected "a c e" true "b c/x/y d" \
+ "$SQ$prefix/c$SQ exists; cannot create $SQ$prefix/c/x/y$SQ"
+
+'
+
+test_expect_success 'new ref is a simple prefix of existing loose' '
+
+ prefix=refs/3l &&
+ test_update_rejected "a c/x e" false "b c d" \
+ "$SQ$prefix/c/x$SQ exists; cannot create $SQ$prefix/c$SQ"
+
+'
+
+test_expect_success 'new ref is a simple prefix of existing packed' '
+
+ prefix=refs/3p &&
+ test_update_rejected "a c/x e" true "b c d" \
+ "$SQ$prefix/c/x$SQ exists; cannot create $SQ$prefix/c$SQ"
+
+'
+
+test_expect_success 'new ref is a deeper prefix of existing loose' '
+
+ prefix=refs/4l &&
+ test_update_rejected "a c/x/y e" false "b c d" \
+ "$SQ$prefix/c/x/y$SQ exists; cannot create $SQ$prefix/c$SQ"
+
+'
+
+test_expect_success 'new ref is a deeper prefix of existing packed' '
+
+ prefix=refs/4p &&
+ test_update_rejected "a c/x/y e" true "b c d" \
+ "$SQ$prefix/c/x/y$SQ exists; cannot create $SQ$prefix/c$SQ"
+
+'
+
+test_expect_success 'one new ref is a simple prefix of another' '
+
+ prefix=refs/5 &&
+ test_update_rejected "a e" false "b c c/x d" \
+ "cannot process $SQ$prefix/c$SQ and $SQ$prefix/c/x$SQ at the same time"
+
+'
+
+test_expect_success REFFILES 'empty directory should not fool rev-parse' '
+ prefix=refs/e-rev-parse &&
+ git update-ref $prefix/foo $C &&
+ git pack-refs --all &&
+ mkdir -p .git/$prefix/foo/bar/baz &&
+ echo "$C" >expected &&
+ git rev-parse $prefix/foo >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success REFFILES 'empty directory should not fool for-each-ref' '
+ prefix=refs/e-for-each-ref &&
+ git update-ref $prefix/foo $C &&
+ git for-each-ref $prefix >expected &&
+ git pack-refs --all &&
+ mkdir -p .git/$prefix/foo/bar/baz &&
+ git for-each-ref $prefix >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success REFFILES 'empty directory should not fool create' '
+ prefix=refs/e-create &&
+ mkdir -p .git/$prefix/foo/bar/baz &&
+ printf "create %s $C\n" $prefix/foo |
+ git update-ref --stdin
+'
+
+test_expect_success REFFILES 'empty directory should not fool verify' '
+ prefix=refs/e-verify &&
+ git update-ref $prefix/foo $C &&
+ git pack-refs --all &&
+ mkdir -p .git/$prefix/foo/bar/baz &&
+ printf "verify %s $C\n" $prefix/foo |
+ git update-ref --stdin
+'
+
+test_expect_success REFFILES 'empty directory should not fool 1-arg update' '
+ prefix=refs/e-update-1 &&
+ git update-ref $prefix/foo $C &&
+ git pack-refs --all &&
+ mkdir -p .git/$prefix/foo/bar/baz &&
+ printf "update %s $D\n" $prefix/foo |
+ git update-ref --stdin
+'
+
+test_expect_success REFFILES 'empty directory should not fool 2-arg update' '
+ prefix=refs/e-update-2 &&
+ git update-ref $prefix/foo $C &&
+ git pack-refs --all &&
+ mkdir -p .git/$prefix/foo/bar/baz &&
+ printf "update %s $D $C\n" $prefix/foo |
+ git update-ref --stdin
+'
+
+test_expect_success REFFILES 'empty directory should not fool 0-arg delete' '
+ prefix=refs/e-delete-0 &&
+ git update-ref $prefix/foo $C &&
+ git pack-refs --all &&
+ mkdir -p .git/$prefix/foo/bar/baz &&
+ printf "delete %s\n" $prefix/foo |
+ git update-ref --stdin
+'
+
+test_expect_success REFFILES 'empty directory should not fool 1-arg delete' '
+ prefix=refs/e-delete-1 &&
+ git update-ref $prefix/foo $C &&
+ git pack-refs --all &&
+ mkdir -p .git/$prefix/foo/bar/baz &&
+ printf "delete %s $C\n" $prefix/foo |
+ git update-ref --stdin
+'
+
+test_expect_success REFFILES 'D/F conflict prevents add long + delete short' '
+ df_test refs/df-al-ds --add-del foo/bar foo
+'
+
+test_expect_success REFFILES 'D/F conflict prevents add short + delete long' '
+ df_test refs/df-as-dl --add-del foo foo/bar
+'
+
+test_expect_success REFFILES 'D/F conflict prevents delete long + add short' '
+ df_test refs/df-dl-as --del-add foo/bar foo
+'
+
+test_expect_success REFFILES 'D/F conflict prevents delete short + add long' '
+ df_test refs/df-ds-al --del-add foo foo/bar
+'
+
+test_expect_success REFFILES 'D/F conflict prevents add long + delete short packed' '
+ df_test refs/df-al-dsp --pack --add-del foo/bar foo
+'
+
+test_expect_success REFFILES 'D/F conflict prevents add short + delete long packed' '
+ df_test refs/df-as-dlp --pack --add-del foo foo/bar
+'
+
+test_expect_success REFFILES 'D/F conflict prevents delete long packed + add short' '
+ df_test refs/df-dlp-as --pack --del-add foo/bar foo
+'
+
+test_expect_success REFFILES 'D/F conflict prevents delete short packed + add long' '
+ df_test refs/df-dsp-al --pack --del-add foo foo/bar
+'
+
+# Try some combinations involving symbolic refs...
+
+test_expect_success REFFILES 'D/F conflict prevents indirect add long + delete short' '
+ df_test refs/df-ial-ds --sym-add --add-del foo/bar foo
+'
+
+test_expect_success REFFILES 'D/F conflict prevents indirect add long + indirect delete short' '
+ df_test refs/df-ial-ids --sym-add --sym-del --add-del foo/bar foo
+'
+
+test_expect_success REFFILES 'D/F conflict prevents indirect add short + indirect delete long' '
+ df_test refs/df-ias-idl --sym-add --sym-del --add-del foo foo/bar
+'
+
+test_expect_success REFFILES 'D/F conflict prevents indirect delete long + indirect add short' '
+ df_test refs/df-idl-ias --sym-add --sym-del --del-add foo/bar foo
+'
+
+test_expect_success REFFILES 'D/F conflict prevents indirect add long + delete short packed' '
+ df_test refs/df-ial-dsp --sym-add --pack --add-del foo/bar foo
+'
+
+test_expect_success REFFILES 'D/F conflict prevents indirect add long + indirect delete short packed' '
+ df_test refs/df-ial-idsp --sym-add --sym-del --pack --add-del foo/bar foo
+'
+
+test_expect_success REFFILES 'D/F conflict prevents add long + indirect delete short packed' '
+ df_test refs/df-al-idsp --sym-del --pack --add-del foo/bar foo
+'
+
+test_expect_success REFFILES 'D/F conflict prevents indirect delete long packed + indirect add short' '
+ df_test refs/df-idlp-ias --sym-add --sym-del --pack --del-add foo/bar foo
+'
+
+# Test various errors when reading the old values of references...
+
+test_expect_success 'missing old value blocks update' '
+ prefix=refs/missing-update &&
+ cat >expected <<-EOF &&
+ fatal: cannot lock ref $SQ$prefix/foo$SQ: unable to resolve reference $SQ$prefix/foo$SQ
+ EOF
+ printf "%s\n" "update $prefix/foo $E $D" |
+ test_must_fail git update-ref --stdin 2>output.err &&
+ test_cmp expected output.err
+'
+
+test_expect_success 'incorrect old value blocks update' '
+ prefix=refs/incorrect-update &&
+ git update-ref $prefix/foo $C &&
+ cat >expected <<-EOF &&
+ fatal: cannot lock ref $SQ$prefix/foo$SQ: is at $C but expected $D
+ EOF
+ printf "%s\n" "update $prefix/foo $E $D" |
+ test_must_fail git update-ref --stdin 2>output.err &&
+ test_cmp expected output.err
+'
+
+test_expect_success 'existing old value blocks create' '
+ prefix=refs/existing-create &&
+ git update-ref $prefix/foo $C &&
+ cat >expected <<-EOF &&
+ fatal: cannot lock ref $SQ$prefix/foo$SQ: reference already exists
+ EOF
+ printf "%s\n" "create $prefix/foo $E" |
+ test_must_fail git update-ref --stdin 2>output.err &&
+ test_cmp expected output.err
+'
+
+test_expect_success 'incorrect old value blocks delete' '
+ prefix=refs/incorrect-delete &&
+ git update-ref $prefix/foo $C &&
+ cat >expected <<-EOF &&
+ fatal: cannot lock ref $SQ$prefix/foo$SQ: is at $C but expected $D
+ EOF
+ printf "%s\n" "delete $prefix/foo $D" |
+ test_must_fail git update-ref --stdin 2>output.err &&
+ test_cmp expected output.err
+'
+
+test_expect_success 'missing old value blocks indirect update' '
+ prefix=refs/missing-indirect-update &&
+ git symbolic-ref $prefix/symref $prefix/foo &&
+ cat >expected <<-EOF &&
+ fatal: cannot lock ref $SQ$prefix/symref$SQ: unable to resolve reference $SQ$prefix/foo$SQ
+ EOF
+ printf "%s\n" "update $prefix/symref $E $D" |
+ test_must_fail git update-ref --stdin 2>output.err &&
+ test_cmp expected output.err
+'
+
+test_expect_success 'incorrect old value blocks indirect update' '
+ prefix=refs/incorrect-indirect-update &&
+ git symbolic-ref $prefix/symref $prefix/foo &&
+ git update-ref $prefix/foo $C &&
+ cat >expected <<-EOF &&
+ fatal: cannot lock ref $SQ$prefix/symref$SQ: is at $C but expected $D
+ EOF
+ printf "%s\n" "update $prefix/symref $E $D" |
+ test_must_fail git update-ref --stdin 2>output.err &&
+ test_cmp expected output.err
+'
+
+test_expect_success 'existing old value blocks indirect create' '
+ prefix=refs/existing-indirect-create &&
+ git symbolic-ref $prefix/symref $prefix/foo &&
+ git update-ref $prefix/foo $C &&
+ cat >expected <<-EOF &&
+ fatal: cannot lock ref $SQ$prefix/symref$SQ: reference already exists
+ EOF
+ printf "%s\n" "create $prefix/symref $E" |
+ test_must_fail git update-ref --stdin 2>output.err &&
+ test_cmp expected output.err
+'
+
+test_expect_success 'incorrect old value blocks indirect delete' '
+ prefix=refs/incorrect-indirect-delete &&
+ git symbolic-ref $prefix/symref $prefix/foo &&
+ git update-ref $prefix/foo $C &&
+ cat >expected <<-EOF &&
+ fatal: cannot lock ref $SQ$prefix/symref$SQ: is at $C but expected $D
+ EOF
+ printf "%s\n" "delete $prefix/symref $D" |
+ test_must_fail git update-ref --stdin 2>output.err &&
+ test_cmp expected output.err
+'
+
+test_expect_success 'missing old value blocks indirect no-deref update' '
+ prefix=refs/missing-noderef-update &&
+ git symbolic-ref $prefix/symref $prefix/foo &&
+ cat >expected <<-EOF &&
+ fatal: cannot lock ref $SQ$prefix/symref$SQ: reference is missing but expected $D
+ EOF
+ printf "%s\n" "option no-deref" "update $prefix/symref $E $D" |
+ test_must_fail git update-ref --stdin 2>output.err &&
+ test_cmp expected output.err
+'
+
+test_expect_success 'incorrect old value blocks indirect no-deref update' '
+ prefix=refs/incorrect-noderef-update &&
+ git symbolic-ref $prefix/symref $prefix/foo &&
+ git update-ref $prefix/foo $C &&
+ cat >expected <<-EOF &&
+ fatal: cannot lock ref $SQ$prefix/symref$SQ: is at $C but expected $D
+ EOF
+ printf "%s\n" "option no-deref" "update $prefix/symref $E $D" |
+ test_must_fail git update-ref --stdin 2>output.err &&
+ test_cmp expected output.err
+'
+
+test_expect_success 'existing old value blocks indirect no-deref create' '
+ prefix=refs/existing-noderef-create &&
+ git symbolic-ref $prefix/symref $prefix/foo &&
+ git update-ref $prefix/foo $C &&
+ cat >expected <<-EOF &&
+ fatal: cannot lock ref $SQ$prefix/symref$SQ: reference already exists
+ EOF
+ printf "%s\n" "option no-deref" "create $prefix/symref $E" |
+ test_must_fail git update-ref --stdin 2>output.err &&
+ test_cmp expected output.err
+'
+
+test_expect_success 'incorrect old value blocks indirect no-deref delete' '
+ prefix=refs/incorrect-noderef-delete &&
+ git symbolic-ref $prefix/symref $prefix/foo &&
+ git update-ref $prefix/foo $C &&
+ cat >expected <<-EOF &&
+ fatal: cannot lock ref $SQ$prefix/symref$SQ: is at $C but expected $D
+ EOF
+ printf "%s\n" "option no-deref" "delete $prefix/symref $D" |
+ test_must_fail git update-ref --stdin 2>output.err &&
+ test_cmp expected output.err
+'
+
+test_expect_success REFFILES 'non-empty directory blocks create' '
+ prefix=refs/ne-create &&
+ mkdir -p .git/$prefix/foo/bar &&
+ : >.git/$prefix/foo/bar/baz.lock &&
+ test_when_finished "rm -f .git/$prefix/foo/bar/baz.lock" &&
+ cat >expected <<-EOF &&
+ fatal: cannot lock ref $SQ$prefix/foo$SQ: there is a non-empty directory $SQ.git/$prefix/foo$SQ blocking reference $SQ$prefix/foo$SQ
+ EOF
+ printf "%s\n" "update $prefix/foo $C" |
+ test_must_fail git update-ref --stdin 2>output.err &&
+ test_cmp expected output.err &&
+ cat >expected <<-EOF &&
+ fatal: cannot lock ref $SQ$prefix/foo$SQ: unable to resolve reference $SQ$prefix/foo$SQ
+ EOF
+ printf "%s\n" "update $prefix/foo $D $C" |
+ test_must_fail git update-ref --stdin 2>output.err &&
+ test_cmp expected output.err
+'
+
+test_expect_success REFFILES 'broken reference blocks create' '
+ prefix=refs/broken-create &&
+ mkdir -p .git/$prefix &&
+ echo "gobbledigook" >.git/$prefix/foo &&
+ test_when_finished "rm -f .git/$prefix/foo" &&
+ cat >expected <<-EOF &&
+ fatal: cannot lock ref $SQ$prefix/foo$SQ: unable to resolve reference $SQ$prefix/foo$SQ: reference broken
+ EOF
+ printf "%s\n" "update $prefix/foo $C" |
+ test_must_fail git update-ref --stdin 2>output.err &&
+ test_cmp expected output.err &&
+ cat >expected <<-EOF &&
+ fatal: cannot lock ref $SQ$prefix/foo$SQ: unable to resolve reference $SQ$prefix/foo$SQ: reference broken
+ EOF
+ printf "%s\n" "update $prefix/foo $D $C" |
+ test_must_fail git update-ref --stdin 2>output.err &&
+ test_cmp expected output.err
+'
+
+test_expect_success REFFILES 'non-empty directory blocks indirect create' '
+ prefix=refs/ne-indirect-create &&
+ git symbolic-ref $prefix/symref $prefix/foo &&
+ mkdir -p .git/$prefix/foo/bar &&
+ : >.git/$prefix/foo/bar/baz.lock &&
+ test_when_finished "rm -f .git/$prefix/foo/bar/baz.lock" &&
+ cat >expected <<-EOF &&
+ fatal: cannot lock ref $SQ$prefix/symref$SQ: there is a non-empty directory $SQ.git/$prefix/foo$SQ blocking reference $SQ$prefix/foo$SQ
+ EOF
+ printf "%s\n" "update $prefix/symref $C" |
+ test_must_fail git update-ref --stdin 2>output.err &&
+ test_cmp expected output.err &&
+ cat >expected <<-EOF &&
+ fatal: cannot lock ref $SQ$prefix/symref$SQ: unable to resolve reference $SQ$prefix/foo$SQ
+ EOF
+ printf "%s\n" "update $prefix/symref $D $C" |
+ test_must_fail git update-ref --stdin 2>output.err &&
+ test_cmp expected output.err
+'
+
+test_expect_success REFFILES 'broken reference blocks indirect create' '
+ prefix=refs/broken-indirect-create &&
+ git symbolic-ref $prefix/symref $prefix/foo &&
+ echo "gobbledigook" >.git/$prefix/foo &&
+ test_when_finished "rm -f .git/$prefix/foo" &&
+ cat >expected <<-EOF &&
+ fatal: cannot lock ref $SQ$prefix/symref$SQ: unable to resolve reference $SQ$prefix/foo$SQ: reference broken
+ EOF
+ printf "%s\n" "update $prefix/symref $C" |
+ test_must_fail git update-ref --stdin 2>output.err &&
+ test_cmp expected output.err &&
+ cat >expected <<-EOF &&
+ fatal: cannot lock ref $SQ$prefix/symref$SQ: unable to resolve reference $SQ$prefix/foo$SQ: reference broken
+ EOF
+ printf "%s\n" "update $prefix/symref $D $C" |
+ test_must_fail git update-ref --stdin 2>output.err &&
+ test_cmp expected output.err
+'
+
+test_expect_success REFFILES 'no bogus intermediate values during delete' '
+ prefix=refs/slow-transaction &&
+ # Set up a reference with differing loose and packed versions:
+ git update-ref $prefix/foo $C &&
+ git pack-refs --all &&
+ git update-ref $prefix/foo $D &&
+ # Now try to update the reference, but hold the `packed-refs` lock
+ # for a while to see what happens while the process is blocked:
+ : >.git/packed-refs.lock &&
+ test_when_finished "rm -f .git/packed-refs.lock" &&
+ {
+ # Note: the following command is intentionally run in the
+ # background. We increase the timeout so that `update-ref`
+ # attempts to acquire the `packed-refs` lock for much longer
+ # than it takes for us to do the check then delete it:
+ git -c core.packedrefstimeout=30000 update-ref -d $prefix/foo &
+ } &&
+ pid2=$! &&
+ # Give update-ref plenty of time to get to the point where it tries
+ # to lock packed-refs:
+ sleep 1 &&
+ # Make sure that update-ref did not complete despite the lock:
+ kill -0 $pid2 &&
+ # Verify that the reference still has its old value:
+ sha1=$(git rev-parse --verify --quiet $prefix/foo || echo undefined) &&
+ case "$sha1" in
+ $D)
+ # This is what we hope for; it means that nothing
+ # user-visible has changed yet.
+ : ;;
+ undefined)
+ # This is not correct; it means the deletion has happened
+ # already even though update-ref should not have been
+ # able to acquire the lock yet.
+ echo "$prefix/foo deleted prematurely" &&
+ break
+ ;;
+ $C)
+ # This value should never be seen. Probably the loose
+ # reference has been deleted but the packed reference
+ # is still there:
+ echo "$prefix/foo incorrectly observed to be C" &&
+ break
+ ;;
+ *)
+ # WTF?
+ echo "unexpected value observed for $prefix/foo: $sha1" &&
+ break
+ ;;
+ esac >out &&
+ rm -f .git/packed-refs.lock &&
+ wait $pid2 &&
+ test_must_be_empty out &&
+ test_must_fail git rev-parse --verify --quiet $prefix/foo
+'
+
+test_expect_success REFFILES 'delete fails cleanly if packed-refs file is locked' '
+ prefix=refs/locked-packed-refs &&
+ # Set up a reference with differing loose and packed versions:
+ git update-ref $prefix/foo $C &&
+ git pack-refs --all &&
+ git update-ref $prefix/foo $D &&
+ git for-each-ref $prefix >unchanged &&
+ # Now try to delete it while the `packed-refs` lock is held:
+ : >.git/packed-refs.lock &&
+ test_when_finished "rm -f .git/packed-refs.lock" &&
+ test_must_fail git update-ref -d $prefix/foo >out 2>err &&
+ git for-each-ref $prefix >actual &&
+ test_grep "Unable to create $SQ.*packed-refs.lock$SQ: " err &&
+ test_cmp unchanged actual
+'
+
+test_expect_success REFFILES 'delete fails cleanly if packed-refs.new write fails' '
+ # Setup and expectations are similar to the test above.
+ prefix=refs/failed-packed-refs &&
+ git update-ref $prefix/foo $C &&
+ git pack-refs --all &&
+ git update-ref $prefix/foo $D &&
+ git for-each-ref $prefix >unchanged &&
+ # This should not happen in practice, but it is an easy way to get a
+ # reliable error (we open with create_tempfile(), which uses O_EXCL).
+ : >.git/packed-refs.new &&
+ test_when_finished "rm -f .git/packed-refs.new" &&
+ test_must_fail git update-ref -d $prefix/foo &&
+ git for-each-ref $prefix >actual &&
+ test_cmp unchanged actual
+'
+
+test_done
diff --git a/t/t1405-main-ref-store.sh b/t/t1405-main-ref-store.sh
new file mode 100755
index 0000000..e4627cf
--- /dev/null
+++ b/t/t1405-main-ref-store.sh
@@ -0,0 +1,140 @@
+#!/bin/sh
+
+test_description='test main ref store api'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+RUN="test-tool ref-store main"
+
+
+test_expect_success 'setup' '
+ test_commit one
+'
+
+test_expect_success REFFILES 'pack_refs(PACK_REFS_ALL | PACK_REFS_PRUNE)' '
+ N=`find .git/refs -type f | wc -l` &&
+ test "$N" != 0 &&
+ $RUN pack-refs PACK_REFS_PRUNE,PACK_REFS_ALL &&
+ N=`find .git/refs -type f` &&
+ test -z "$N"
+'
+
+test_expect_success 'create_symref(FOO, refs/heads/main)' '
+ $RUN create-symref FOO refs/heads/main nothing &&
+ echo refs/heads/main >expected &&
+ git symbolic-ref FOO >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'delete_refs(FOO, refs/tags/new-tag)' '
+ git tag -a -m new-tag new-tag HEAD &&
+ git rev-parse FOO -- &&
+ git rev-parse refs/tags/new-tag -- &&
+ m=$(git rev-parse main) &&
+ $RUN delete-refs REF_NO_DEREF nothing FOO refs/tags/new-tag &&
+ test_must_fail git rev-parse --symbolic-full-name FOO &&
+ test_must_fail git rev-parse FOO -- &&
+ test_must_fail git rev-parse refs/tags/new-tag --
+'
+
+# In reftable, we keep the reflogs around for deleted refs.
+test_expect_success !REFFILES 'delete-reflog(FOO, refs/tags/new-tag)' '
+ $RUN delete-reflog FOO &&
+ $RUN delete-reflog refs/tags/new-tag
+'
+
+test_expect_success 'rename_refs(main, new-main)' '
+ git rev-parse main >expected &&
+ $RUN rename-ref refs/heads/main refs/heads/new-main &&
+ git rev-parse new-main >actual &&
+ test_cmp expected actual &&
+ test_commit recreate-main
+'
+
+test_expect_success 'for_each_ref(refs/heads/)' '
+ $RUN for-each-ref refs/heads/ | cut -d" " -f 2- >actual &&
+ cat >expected <<-\EOF &&
+ main 0x0
+ new-main 0x0
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'for_each_ref() is sorted' '
+ $RUN for-each-ref refs/heads/ | cut -d" " -f 2- >actual &&
+ sort actual > expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'resolve_ref(new-main)' '
+ SHA1=`git rev-parse new-main` &&
+ echo "$SHA1 refs/heads/new-main 0x0" >expected &&
+ $RUN resolve-ref refs/heads/new-main 0 >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'verify_ref(new-main)' '
+ $RUN verify-ref refs/heads/new-main
+'
+
+test_expect_success 'for_each_reflog()' '
+ $RUN for-each-reflog | sort -k2 | cut -d" " -f 2- >actual &&
+ cat >expected <<-\EOF &&
+ HEAD 0x1
+ refs/heads/main 0x0
+ refs/heads/new-main 0x0
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'for_each_reflog_ent()' '
+ $RUN for-each-reflog-ent HEAD >actual &&
+ head -n1 actual | grep one &&
+ tail -n1 actual | grep recreate-main
+'
+
+test_expect_success 'for_each_reflog_ent_reverse()' '
+ $RUN for-each-reflog-ent-reverse HEAD >actual &&
+ head -n1 actual | grep recreate-main &&
+ tail -n1 actual | grep one
+'
+
+test_expect_success 'reflog_exists(HEAD)' '
+ $RUN reflog-exists HEAD
+'
+
+test_expect_success 'delete_reflog(HEAD)' '
+ $RUN delete-reflog HEAD &&
+ test_must_fail git reflog exists HEAD
+'
+
+test_expect_success REFFILES 'create-reflog(HEAD)' '
+ $RUN create-reflog HEAD &&
+ git reflog exists HEAD
+'
+
+test_expect_success 'delete_ref(refs/heads/foo)' '
+ git checkout -b foo &&
+ FOO_SHA1=`git rev-parse foo` &&
+ git checkout --detach &&
+ test_commit bar-commit &&
+ git checkout -b bar &&
+ BAR_SHA1=`git rev-parse bar` &&
+ $RUN update-ref updating refs/heads/foo $BAR_SHA1 $FOO_SHA1 0 &&
+ echo $BAR_SHA1 >expected &&
+ git rev-parse refs/heads/foo >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'delete_ref(refs/heads/foo)' '
+ SHA1=`git rev-parse foo` &&
+ git checkout --detach &&
+ $RUN delete-ref msg refs/heads/foo $SHA1 0 &&
+ test_must_fail git rev-parse refs/heads/foo --
+'
+
+test_done
diff --git a/t/t1406-submodule-ref-store.sh b/t/t1406-submodule-ref-store.sh
new file mode 100755
index 0000000..e6a7f73
--- /dev/null
+++ b/t/t1406-submodule-ref-store.sh
@@ -0,0 +1,99 @@
+#!/bin/sh
+
+test_description='test submodule ref store api'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+RUN="test-tool ref-store submodule:sub"
+
+test_expect_success 'setup' '
+ git init sub &&
+ (
+ cd sub &&
+ test_commit first &&
+ git checkout -b new-main &&
+ git tag -a -m new-tag new-tag HEAD
+ )
+'
+
+test_expect_success 'pack_refs() not allowed' '
+ test_must_fail $RUN pack-refs 3
+'
+
+test_expect_success 'create_symref() not allowed' '
+ test_must_fail $RUN create-symref FOO refs/heads/main nothing
+'
+
+test_expect_success 'delete_refs() not allowed' '
+ test_must_fail $RUN delete-refs 0 nothing FOO refs/tags/new-tag
+'
+
+test_expect_success 'rename_refs() not allowed' '
+ test_must_fail $RUN rename-ref refs/heads/main refs/heads/new-main
+'
+
+test_expect_success 'for_each_ref(refs/heads/)' '
+ $RUN for-each-ref refs/heads/ | cut -d" " -f 2- >actual &&
+ cat >expected <<-\EOF &&
+ main 0x0
+ new-main 0x0
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'for_each_ref() is sorted' '
+ $RUN for-each-ref refs/heads/ | cut -d" " -f 2- >actual &&
+ sort actual > expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'resolve_ref(main)' '
+ SHA1=`git -C sub rev-parse main` &&
+ echo "$SHA1 refs/heads/main 0x0" >expected &&
+ $RUN resolve-ref refs/heads/main 0 >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'verify_ref(new-main)' '
+ $RUN verify-ref refs/heads/new-main
+'
+
+test_expect_success 'for_each_reflog()' '
+ $RUN for-each-reflog | sort | cut -d" " -f 2- >actual &&
+ cat >expected <<-\EOF &&
+ HEAD 0x1
+ refs/heads/main 0x0
+ refs/heads/new-main 0x0
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'for_each_reflog_ent()' '
+ $RUN for-each-reflog-ent HEAD >actual &&
+ head -n1 actual | grep first &&
+ tail -n1 actual | grep main.to.new
+'
+
+test_expect_success 'for_each_reflog_ent_reverse()' '
+ $RUN for-each-reflog-ent-reverse HEAD >actual &&
+ head -n1 actual | grep main.to.new &&
+ tail -n1 actual | grep first
+'
+
+test_expect_success 'reflog_exists(HEAD)' '
+ $RUN reflog-exists HEAD
+'
+
+test_expect_success 'delete_reflog() not allowed' '
+ test_must_fail $RUN delete-reflog HEAD
+'
+
+test_expect_success 'create-reflog() not allowed' '
+ test_must_fail $RUN create-reflog HEAD
+'
+
+test_done
diff --git a/t/t1407-worktree-ref-store.sh b/t/t1407-worktree-ref-store.sh
new file mode 100755
index 0000000..05b1881
--- /dev/null
+++ b/t/t1407-worktree-ref-store.sh
@@ -0,0 +1,93 @@
+#!/bin/sh
+
+test_description='test worktree ref store api'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+RWT="test-tool ref-store worktree:wt"
+RMAIN="test-tool ref-store worktree:main"
+
+test_expect_success 'setup' '
+ test_commit first &&
+ git worktree add -b wt-main wt &&
+ (
+ cd wt &&
+ test_commit second
+ )
+'
+
+test_expect_success 'resolve_ref(<shared-ref>)' '
+ SHA1=`git rev-parse main` &&
+ echo "$SHA1 refs/heads/main 0x0" >expected &&
+ $RWT resolve-ref refs/heads/main 0 >actual &&
+ test_cmp expected actual &&
+ $RMAIN resolve-ref refs/heads/main 0 >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'resolve_ref(<per-worktree-ref>)' '
+ SHA1=`git -C wt rev-parse HEAD` &&
+ echo "$SHA1 refs/heads/wt-main 0x1" >expected &&
+ $RWT resolve-ref HEAD 0 >actual &&
+ test_cmp expected actual &&
+
+ SHA1=`git rev-parse HEAD` &&
+ echo "$SHA1 refs/heads/main 0x1" >expected &&
+ $RMAIN resolve-ref HEAD 0 >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'create_symref(FOO, refs/heads/main)' '
+ $RWT create-symref FOO refs/heads/main nothing &&
+ echo refs/heads/main >expected &&
+ git -C wt symbolic-ref FOO >actual &&
+ test_cmp expected actual &&
+
+ $RMAIN create-symref FOO refs/heads/wt-main nothing &&
+ echo refs/heads/wt-main >expected &&
+ git symbolic-ref FOO >actual &&
+ test_cmp expected actual
+'
+
+# Some refs (refs/bisect/*, pseudorefs) are kept per worktree, so they should
+# only appear in the for-each-reflog output if it is called from the correct
+# worktree, which is exercised in this test. This test is poorly written (and
+# therefore marked REFFILES) for mulitple reasons: 1) it creates invalidly
+# formatted log entres. 2) it uses direct FS access for creating the reflogs. 3)
+# PSEUDO-WT and refs/bisect/random do not create reflogs by default, so it is
+# not testing a realistic scenario.
+test_expect_success REFFILES 'for_each_reflog()' '
+ echo $ZERO_OID > .git/logs/PSEUDO-MAIN &&
+ mkdir -p .git/logs/refs/bisect &&
+ echo $ZERO_OID > .git/logs/refs/bisect/random &&
+
+ echo $ZERO_OID > .git/worktrees/wt/logs/PSEUDO-WT &&
+ mkdir -p .git/worktrees/wt/logs/refs/bisect &&
+ echo $ZERO_OID > .git/worktrees/wt/logs/refs/bisect/wt-random &&
+
+ $RWT for-each-reflog | cut -d" " -f 2- | sort >actual &&
+ cat >expected <<-\EOF &&
+ HEAD 0x1
+ PSEUDO-WT 0x0
+ refs/bisect/wt-random 0x0
+ refs/heads/main 0x0
+ refs/heads/wt-main 0x0
+ EOF
+ test_cmp expected actual &&
+
+ $RMAIN for-each-reflog | cut -d" " -f 2- | sort >actual &&
+ cat >expected <<-\EOF &&
+ HEAD 0x1
+ PSEUDO-MAIN 0x0
+ refs/bisect/random 0x0
+ refs/heads/main 0x0
+ refs/heads/wt-main 0x0
+ EOF
+ test_cmp expected actual
+'
+
+test_done
diff --git a/t/t1408-packed-refs.sh b/t/t1408-packed-refs.sh
new file mode 100755
index 0000000..9469c79
--- /dev/null
+++ b/t/t1408-packed-refs.sh
@@ -0,0 +1,46 @@
+#!/bin/sh
+
+test_description='packed-refs entries are covered by loose refs'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ test_tick &&
+ git commit --allow-empty -m one &&
+ one=$(git rev-parse HEAD) &&
+ git for-each-ref >actual &&
+ echo "$one commit refs/heads/main" >expect &&
+ test_cmp expect actual &&
+
+ git pack-refs --all &&
+ git for-each-ref >actual &&
+ echo "$one commit refs/heads/main" >expect &&
+ test_cmp expect actual &&
+
+ git checkout --orphan another &&
+ test_tick &&
+ git commit --allow-empty -m two &&
+ two=$(git rev-parse HEAD) &&
+ git checkout -B main &&
+ git branch -D another &&
+
+ git for-each-ref >actual &&
+ echo "$two commit refs/heads/main" >expect &&
+ test_cmp expect actual &&
+
+ git reflog expire --expire=now --all &&
+ git prune &&
+ git tag -m v1.0 v1.0 main
+'
+
+test_expect_success 'no error from stale entry in packed-refs' '
+ git describe main >actual 2>&1 &&
+ echo "v1.0" >expect &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t1409-avoid-packing-refs.sh b/t/t1409-avoid-packing-refs.sh
new file mode 100755
index 0000000..f23c015
--- /dev/null
+++ b/t/t1409-avoid-packing-refs.sh
@@ -0,0 +1,119 @@
+#!/bin/sh
+
+test_description='avoid rewriting packed-refs unnecessarily'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# Add an identifying mark to the packed-refs file header line. This
+# shouldn't upset readers, and it should be omitted if the file is
+# ever rewritten.
+mark_packed_refs () {
+ sed -e "s/^\(#.*\)/\1 t1409 /" .git/packed-refs >.git/packed-refs.new &&
+ mv .git/packed-refs.new .git/packed-refs
+}
+
+# Verify that the packed-refs file is still marked.
+check_packed_refs_marked () {
+ grep -q '^#.* t1409 ' .git/packed-refs
+}
+
+test_expect_success 'setup' '
+ git commit --allow-empty -m "Commit A" &&
+ A=$(git rev-parse HEAD) &&
+ git commit --allow-empty -m "Commit B" &&
+ B=$(git rev-parse HEAD) &&
+ git commit --allow-empty -m "Commit C" &&
+ C=$(git rev-parse HEAD)
+'
+
+test_expect_success 'do not create packed-refs file gratuitously' '
+ test_path_is_missing .git/packed-refs &&
+ git update-ref refs/heads/foo $A &&
+ test_path_is_missing .git/packed-refs &&
+ git update-ref refs/heads/foo $B &&
+ test_path_is_missing .git/packed-refs &&
+ git update-ref refs/heads/foo $C $B &&
+ test_path_is_missing .git/packed-refs &&
+ git update-ref -d refs/heads/foo &&
+ test_path_is_missing .git/packed-refs
+'
+
+test_expect_success 'check that marking the packed-refs file works' '
+ git for-each-ref >expected &&
+ git pack-refs --all &&
+ mark_packed_refs &&
+ check_packed_refs_marked &&
+ git for-each-ref >actual &&
+ test_cmp expected actual &&
+ git pack-refs --all &&
+ ! check_packed_refs_marked &&
+ git for-each-ref >actual2 &&
+ test_cmp expected actual2
+'
+
+test_expect_success 'leave packed-refs untouched on update of packed' '
+ git update-ref refs/heads/packed-update $A &&
+ git pack-refs --all &&
+ mark_packed_refs &&
+ git update-ref refs/heads/packed-update $B &&
+ check_packed_refs_marked
+'
+
+test_expect_success 'leave packed-refs untouched on checked update of packed' '
+ git update-ref refs/heads/packed-checked-update $A &&
+ git pack-refs --all &&
+ mark_packed_refs &&
+ git update-ref refs/heads/packed-checked-update $B $A &&
+ check_packed_refs_marked
+'
+
+test_expect_success 'leave packed-refs untouched on verify of packed' '
+ git update-ref refs/heads/packed-verify $A &&
+ git pack-refs --all &&
+ mark_packed_refs &&
+ echo "verify refs/heads/packed-verify $A" | git update-ref --stdin &&
+ check_packed_refs_marked
+'
+
+test_expect_success 'touch packed-refs on delete of packed' '
+ git update-ref refs/heads/packed-delete $A &&
+ git pack-refs --all &&
+ mark_packed_refs &&
+ git update-ref -d refs/heads/packed-delete &&
+ ! check_packed_refs_marked
+'
+
+test_expect_success 'leave packed-refs untouched on update of loose' '
+ git pack-refs --all &&
+ git update-ref refs/heads/loose-update $A &&
+ mark_packed_refs &&
+ git update-ref refs/heads/loose-update $B &&
+ check_packed_refs_marked
+'
+
+test_expect_success 'leave packed-refs untouched on checked update of loose' '
+ git pack-refs --all &&
+ git update-ref refs/heads/loose-checked-update $A &&
+ mark_packed_refs &&
+ git update-ref refs/heads/loose-checked-update $B $A &&
+ check_packed_refs_marked
+'
+
+test_expect_success 'leave packed-refs untouched on verify of loose' '
+ git pack-refs --all &&
+ git update-ref refs/heads/loose-verify $A &&
+ mark_packed_refs &&
+ echo "verify refs/heads/loose-verify $A" | git update-ref --stdin &&
+ check_packed_refs_marked
+'
+
+test_expect_success 'leave packed-refs untouched on delete of loose' '
+ git pack-refs --all &&
+ git update-ref refs/heads/loose-delete $A &&
+ mark_packed_refs &&
+ git update-ref -d refs/heads/loose-delete &&
+ check_packed_refs_marked
+'
+
+test_done
diff --git a/t/t1410-reflog.sh b/t/t1410-reflog.sh
new file mode 100755
index 0000000..aeddc2f
--- /dev/null
+++ b/t/t1410-reflog.sh
@@ -0,0 +1,481 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Junio C Hamano
+#
+
+test_description='Test prune and reflog expiration'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+check_have () {
+ gaah= &&
+ for N in "$@"
+ do
+ eval "o=\$$N" && git cat-file -t $o || {
+ echo Gaah $N
+ gaah=$N
+ break
+ }
+ done &&
+ test -z "$gaah"
+}
+
+check_fsck () {
+ git fsck --full >fsck.output
+ case "$1" in
+ '')
+ test_must_be_empty fsck.output ;;
+ *)
+ test_grep "$1" fsck.output ;;
+ esac
+}
+
+corrupt () {
+ mv .git/objects/$(test_oid_to_path $1) .git/$1
+}
+
+recover () {
+ aa=$(echo $1 | cut -c 1-2)
+ mkdir -p .git/objects/$aa
+ mv .git/$1 .git/objects/$(test_oid_to_path $1)
+}
+
+check_dont_have () {
+ gaah= &&
+ for N in "$@"
+ do
+ eval "o=\$$N"
+ git cat-file -t $o && {
+ echo Gaah $N
+ gaah=$N
+ break
+ }
+ done
+ test -z "$gaah"
+}
+
+test_expect_success setup '
+ mkdir -p A/B &&
+ echo rat >C &&
+ echo ox >A/D &&
+ echo tiger >A/B/E &&
+ git add . &&
+
+ test_tick && git commit -m rabbit &&
+ H=$(git rev-parse --verify HEAD) &&
+ A=$(git rev-parse --verify HEAD:A) &&
+ B=$(git rev-parse --verify HEAD:A/B) &&
+ C=$(git rev-parse --verify HEAD:C) &&
+ D=$(git rev-parse --verify HEAD:A/D) &&
+ E=$(git rev-parse --verify HEAD:A/B/E) &&
+ check_fsck &&
+
+ test_chmod +x C &&
+ git add C &&
+ test_tick && git commit -m dragon &&
+ L=$(git rev-parse --verify HEAD) &&
+ check_fsck &&
+
+ rm -f C A/B/E &&
+ echo snake >F &&
+ echo horse >A/G &&
+ git add F A/G &&
+ test_tick && git commit -a -m sheep &&
+ F=$(git rev-parse --verify HEAD:F) &&
+ G=$(git rev-parse --verify HEAD:A/G) &&
+ I=$(git rev-parse --verify HEAD:A) &&
+ J=$(git rev-parse --verify HEAD) &&
+ check_fsck &&
+
+ rm -f A/G &&
+ test_tick && git commit -a -m monkey &&
+ K=$(git rev-parse --verify HEAD) &&
+ check_fsck &&
+
+ check_have A B C D E F G H I J K L &&
+
+ git prune &&
+
+ check_have A B C D E F G H I J K L &&
+
+ check_fsck &&
+
+ git reflog refs/heads/main >output &&
+ test_line_count = 4 output
+'
+
+test_expect_success 'correct usage on sub-command -h' '
+ test_expect_code 129 git reflog expire -h >err &&
+ grep "git reflog expire" err
+'
+
+test_expect_success 'correct usage on "git reflog show -h"' '
+ test_expect_code 129 git reflog show -h >err &&
+ grep -F "git reflog [show]" err
+'
+
+test_expect_success 'pass through -- to sub-command' '
+ test_when_finished "rm -rf repo" &&
+ git init repo &&
+ test_commit -C repo message --a-file contents dash-tag &&
+
+ git -C repo reflog show -- --does-not-exist >out &&
+ test_must_be_empty out &&
+ git -C repo reflog show >expect &&
+ git -C repo reflog show -- --a-file >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success rewind '
+ test_tick && git reset --hard HEAD~2 &&
+ test -f C &&
+ test -f A/B/E &&
+ ! test -f F &&
+ ! test -f A/G &&
+
+ check_have A B C D E F G H I J K L &&
+
+ git prune &&
+
+ check_have A B C D E F G H I J K L &&
+
+ git reflog refs/heads/main >output &&
+ test_line_count = 5 output
+'
+
+test_expect_success 'corrupt and check' '
+
+ corrupt $F &&
+ check_fsck "missing blob $F"
+
+'
+
+test_expect_success 'reflog expire --dry-run should not touch reflog' '
+
+ git reflog expire --dry-run \
+ --expire=$(($test_tick - 10000)) \
+ --expire-unreachable=$(($test_tick - 10000)) \
+ --stale-fix \
+ --all &&
+
+ git reflog refs/heads/main >output &&
+ test_line_count = 5 output &&
+
+ check_fsck "missing blob $F"
+'
+
+test_expect_success 'reflog expire' '
+
+ git reflog expire --verbose \
+ --expire=$(($test_tick - 10000)) \
+ --expire-unreachable=$(($test_tick - 10000)) \
+ --stale-fix \
+ --all &&
+
+ git reflog refs/heads/main >output &&
+ test_line_count = 2 output &&
+
+ check_fsck "dangling commit $K"
+'
+
+test_expect_success '--stale-fix handles missing objects generously' '
+ git -c core.logAllRefUpdates=false fast-import --date-format=now <<-EOS &&
+ commit refs/heads/stale-fix
+ mark :1
+ committer Author <a@uth.or> now
+ data <<EOF
+ start stale fix
+ EOF
+ M 100644 inline file
+ data <<EOF
+ contents
+ EOF
+ commit refs/heads/stale-fix
+ committer Author <a@uth.or> now
+ data <<EOF
+ stale fix branch tip
+ EOF
+ from :1
+ EOS
+
+ parent_oid=$(git rev-parse stale-fix^) &&
+ test_when_finished "recover $parent_oid" &&
+ corrupt $parent_oid &&
+ git reflog expire --stale-fix
+'
+
+test_expect_success 'prune and fsck' '
+
+ git prune &&
+ check_fsck &&
+
+ check_have A B C D E H L &&
+ check_dont_have F G I J K
+
+'
+
+test_expect_success 'recover and check' '
+
+ recover $F &&
+ check_fsck "dangling blob $F"
+
+'
+
+test_expect_success 'delete' '
+ echo 1 > C &&
+ test_tick &&
+ git commit -m rat C &&
+
+ echo 2 > C &&
+ test_tick &&
+ git commit -m ox C &&
+
+ echo 3 > C &&
+ test_tick &&
+ git commit -m tiger C &&
+
+ HEAD_entry_count=$(git reflog | wc -l) &&
+ main_entry_count=$(git reflog show main | wc -l) &&
+
+ test $HEAD_entry_count = 5 &&
+ test $main_entry_count = 5 &&
+
+
+ git reflog delete main@{1} &&
+ git reflog show main > output &&
+ test_line_count = $(($main_entry_count - 1)) output &&
+ test $HEAD_entry_count = $(git reflog | wc -l) &&
+ ! grep ox < output &&
+
+ main_entry_count=$(wc -l < output) &&
+
+ git reflog delete HEAD@{1} &&
+ test $(($HEAD_entry_count -1)) = $(git reflog | wc -l) &&
+ test $main_entry_count = $(git reflog show main | wc -l) &&
+
+ HEAD_entry_count=$(git reflog | wc -l) &&
+
+ git reflog delete main@{07.04.2005.15:15:00.-0700} &&
+ git reflog show main > output &&
+ test_line_count = $(($main_entry_count - 1)) output &&
+ ! grep dragon < output
+
+'
+
+test_expect_success 'rewind2' '
+
+ test_tick && git reset --hard HEAD~2 &&
+ git reflog refs/heads/main >output &&
+ test_line_count = 4 output
+'
+
+test_expect_success '--expire=never' '
+
+ git reflog expire --verbose \
+ --expire=never \
+ --expire-unreachable=never \
+ --all &&
+ git reflog refs/heads/main >output &&
+ test_line_count = 4 output
+'
+
+test_expect_success 'gc.reflogexpire=never' '
+ test_config gc.reflogexpire never &&
+ test_config gc.reflogexpireunreachable never &&
+
+ git reflog expire --verbose --all >output &&
+ test_line_count = 9 output &&
+
+ git reflog refs/heads/main >output &&
+ test_line_count = 4 output
+'
+
+test_expect_success 'gc.reflogexpire=false' '
+ test_config gc.reflogexpire false &&
+ test_config gc.reflogexpireunreachable false &&
+
+ git reflog expire --verbose --all &&
+ git reflog refs/heads/main >output &&
+ test_line_count = 4 output
+
+'
+
+test_expect_success 'git reflog expire unknown reference' '
+ test_config gc.reflogexpire never &&
+ test_config gc.reflogexpireunreachable never &&
+
+ test_must_fail git reflog expire main@{123} 2>stderr &&
+ test_grep "points nowhere" stderr &&
+ test_must_fail git reflog expire does-not-exist 2>stderr &&
+ test_grep "points nowhere" stderr
+'
+
+test_expect_success 'checkout should not delete log for packed ref' '
+ test $(git reflog main | wc -l) = 4 &&
+ git branch foo &&
+ git pack-refs --all &&
+ git checkout foo &&
+ test $(git reflog main | wc -l) = 4
+'
+
+test_expect_success 'stale dirs do not cause d/f conflicts (reflogs on)' '
+ test_when_finished "git branch -d one || git branch -d one/two" &&
+
+ git branch one/two main &&
+ echo "one/two@{0} branch: Created from main" >expect &&
+ git log -g --format="%gd %gs" one/two >actual &&
+ test_cmp expect actual &&
+ git branch -d one/two &&
+
+ # now logs/refs/heads/one is a stale directory, but
+ # we should move it out of the way to create "one" reflog
+ git branch one main &&
+ echo "one@{0} branch: Created from main" >expect &&
+ git log -g --format="%gd %gs" one >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'stale dirs do not cause d/f conflicts (reflogs off)' '
+ test_when_finished "git branch -d one || git branch -d one/two" &&
+
+ git branch one/two main &&
+ echo "one/two@{0} branch: Created from main" >expect &&
+ git log -g --format="%gd %gs" one/two >actual &&
+ test_cmp expect actual &&
+ git branch -d one/two &&
+
+ # same as before, but we only create a reflog for "one" if
+ # it already exists, which it does not
+ git -c core.logallrefupdates=false branch one main &&
+ git log -g --format="%gd %gs" one >actual &&
+ test_must_be_empty actual
+'
+
+# Triggering the bug detected by this test requires a newline to fall
+# exactly BUFSIZ-1 bytes from the end of the file. We don't know
+# what that value is, since it's platform dependent. However, if
+# we choose some value N, we also catch any D which divides N evenly
+# (since we will read backwards in chunks of D). So we choose 8K,
+# which catches glibc (with an 8K BUFSIZ) and *BSD (1K).
+#
+# Each line is 114 characters, so we need 75 to still have a few before the
+# last 8K. The 89-character padding on the final entry lines up our
+# newline exactly.
+test_expect_success REFFILES,SHA1 'parsing reverse reflogs at BUFSIZ boundaries' '
+ git checkout -b reflogskip &&
+ zf=$(test_oid zero_2) &&
+ ident="abc <xyz> 0000000001 +0000" &&
+ for i in $(test_seq 1 75); do
+ printf "$zf%02d $zf%02d %s\t" $i $(($i+1)) "$ident" &&
+ if test $i = 75; then
+ for j in $(test_seq 1 89); do
+ printf X || return 1
+ done
+ else
+ printf X
+ fi &&
+ printf "\n" || return 1
+ done >.git/logs/refs/heads/reflogskip &&
+ git rev-parse reflogskip@{73} >actual &&
+ echo ${zf}03 >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'no segfaults for reflog containing non-commit sha1s' '
+ git update-ref --create-reflog -m "Creating ref" \
+ refs/tests/tree-in-reflog HEAD &&
+ git update-ref -m "Forcing tree" refs/tests/tree-in-reflog HEAD^{tree} &&
+ git update-ref -m "Restoring to commit" refs/tests/tree-in-reflog HEAD &&
+ git reflog refs/tests/tree-in-reflog
+'
+
+test_expect_failure 'reflog with non-commit entries displays all entries' '
+ git reflog refs/tests/tree-in-reflog >actual &&
+ test_line_count = 3 actual
+'
+
+# This test takes a lock on an individual ref; this is not supported in
+# reftable.
+test_expect_success REFFILES 'reflog expire operates on symref not referrent' '
+ git branch --create-reflog the_symref &&
+ git branch --create-reflog referrent &&
+ git update-ref referrent HEAD &&
+ git symbolic-ref refs/heads/the_symref refs/heads/referrent &&
+ test_when_finished "rm -f .git/refs/heads/referrent.lock" &&
+ touch .git/refs/heads/referrent.lock &&
+ git reflog expire --expire=all the_symref
+'
+
+test_expect_success 'continue walking past root commits' '
+ git init orphanage &&
+ (
+ cd orphanage &&
+ cat >expect <<-\EOF &&
+ HEAD@{0} commit (initial): orphan2-1
+ HEAD@{1} commit: orphan1-2
+ HEAD@{2} commit (initial): orphan1-1
+ HEAD@{3} commit (initial): initial
+ EOF
+ test_commit initial &&
+ git checkout --orphan orphan1 &&
+ test_commit orphan1-1 &&
+ test_commit orphan1-2 &&
+ git checkout --orphan orphan2 &&
+ test_commit orphan2-1 &&
+ git log -g --format="%gd %gs" >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'expire with multiple worktrees' '
+ git init main-wt &&
+ (
+ cd main-wt &&
+ test_tick &&
+ test_commit foo &&
+ git worktree add link-wt &&
+ test_tick &&
+ test_commit -C link-wt foobar &&
+ test_tick &&
+ git reflog expire --verbose --all --expire=$test_tick &&
+ test-tool ref-store worktree:link-wt for-each-reflog-ent HEAD >actual &&
+ test_must_be_empty actual
+ )
+'
+
+test_expect_success 'expire one of multiple worktrees' '
+ git init main-wt2 &&
+ (
+ cd main-wt2 &&
+ test_tick &&
+ test_commit foo &&
+ git worktree add link-wt &&
+ test_tick &&
+ test_commit -C link-wt foobar &&
+ test_tick &&
+ test-tool ref-store worktree:link-wt for-each-reflog-ent HEAD \
+ >expect-link-wt &&
+ git reflog expire --verbose --all --expire=$test_tick \
+ --single-worktree &&
+ test-tool ref-store worktree:main for-each-reflog-ent HEAD \
+ >actual-main &&
+ test-tool ref-store worktree:link-wt for-each-reflog-ent HEAD \
+ >actual-link-wt &&
+ test_must_be_empty actual-main &&
+ test_cmp expect-link-wt actual-link-wt
+ )
+'
+
+test_expect_success REFFILES 'empty reflog' '
+ test_when_finished "rm -rf empty" &&
+ git init empty &&
+ test_commit -C empty A &&
+ >empty/.git/logs/refs/heads/foo &&
+ git -C empty reflog expire --all 2>err &&
+ test_must_be_empty err
+'
+
+test_done
diff --git a/t/t1411-reflog-show.sh b/t/t1411-reflog-show.sh
new file mode 100755
index 0000000..da581ec
--- /dev/null
+++ b/t/t1411-reflog-show.sh
@@ -0,0 +1,173 @@
+#!/bin/sh
+
+test_description='Test reflog display routines'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ echo content >file &&
+ git add file &&
+ test_tick &&
+ git commit -m one
+'
+
+commit=$(git rev-parse --short HEAD)
+cat >expect <<'EOF'
+Reflog: HEAD@{0} (C O Mitter <committer@example.com>)
+Reflog message: commit (initial): one
+EOF
+test_expect_success 'log -g shows reflog headers' '
+ git log -g -1 >tmp &&
+ grep ^Reflog <tmp >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<EOF
+$commit HEAD@{0}: commit (initial): one
+EOF
+test_expect_success 'oneline reflog format' '
+ git log -g -1 --oneline >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'reflog default format' '
+ git reflog -1 >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<EOF
+commit $commit
+Reflog: HEAD@{0} (C O Mitter <committer@example.com>)
+Reflog message: commit (initial): one
+Author: A U Thor <author@example.com>
+
+ one
+EOF
+test_expect_success 'override reflog default format' '
+ git reflog --format=short -1 >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<'EOF'
+Reflog: HEAD@{Thu Apr 7 15:13:13 2005 -0700} (C O Mitter <committer@example.com>)
+Reflog message: commit (initial): one
+EOF
+test_expect_success 'using @{now} syntax shows reflog date (multiline)' '
+ git log -g -1 HEAD@{now} >tmp &&
+ grep ^Reflog <tmp >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<EOF
+$commit HEAD@{Thu Apr 7 15:13:13 2005 -0700}: commit (initial): one
+EOF
+test_expect_success 'using @{now} syntax shows reflog date (oneline)' '
+ git log -g -1 --oneline HEAD@{now} >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<'EOF'
+HEAD@{Thu Apr 7 15:13:13 2005 -0700}
+EOF
+test_expect_success 'using @{now} syntax shows reflog date (format=%gd)' '
+ git log -g -1 --format=%gd HEAD@{now} >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<'EOF'
+Reflog: HEAD@{Thu Apr 7 15:13:13 2005 -0700} (C O Mitter <committer@example.com>)
+Reflog message: commit (initial): one
+EOF
+test_expect_success 'using --date= shows reflog date (multiline)' '
+ git log -g -1 --date=default >tmp &&
+ grep ^Reflog <tmp >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<EOF
+$commit HEAD@{Thu Apr 7 15:13:13 2005 -0700}: commit (initial): one
+EOF
+test_expect_success 'using --date= shows reflog date (oneline)' '
+ git log -g -1 --oneline --date=default >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<'EOF'
+HEAD@{1112911993 -0700}
+EOF
+test_expect_success 'using --date= shows reflog date (format=%gd)' '
+ git log -g -1 --format=%gd --date=raw >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<'EOF'
+Reflog: HEAD@{0} (C O Mitter <committer@example.com>)
+Reflog message: commit (initial): one
+EOF
+test_expect_success 'log.date does not invoke "--date" magic (multiline)' '
+ test_config log.date raw &&
+ git log -g -1 >tmp &&
+ grep ^Reflog <tmp >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<EOF
+$commit HEAD@{0}: commit (initial): one
+EOF
+test_expect_success 'log.date does not invoke "--date" magic (oneline)' '
+ test_config log.date raw &&
+ git log -g -1 --oneline >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<'EOF'
+HEAD@{0}
+EOF
+test_expect_success 'log.date does not invoke "--date" magic (format=%gd)' '
+ test_config log.date raw &&
+ git log -g -1 --format=%gd >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<'EOF'
+HEAD@{0}
+EOF
+test_expect_success '--date magic does not override explicit @{0} syntax' '
+ git log -g -1 --format=%gd --date=raw HEAD@{0} >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'empty reflog file' '
+ git branch empty &&
+ git reflog expire --expire=all refs/heads/empty &&
+
+ git log -g empty >actual &&
+ test_must_be_empty actual
+'
+
+# This guards against the alternative of showing the diffs vs. the
+# reflog ancestor. The reflog used is designed to list the commits
+# more than once, so as to exercise the corresponding logic.
+test_expect_success 'git log -g -p shows diffs vs. parents' '
+ test_commit two &&
+ git branch flipflop &&
+ git update-ref refs/heads/flipflop -m flip1 HEAD^ &&
+ git update-ref refs/heads/flipflop -m flop1 HEAD &&
+ git update-ref refs/heads/flipflop -m flip2 HEAD^ &&
+ git log -g -p flipflop >reflog &&
+ grep -v ^Reflog reflog >actual &&
+ git log -1 -p HEAD^ >log.one &&
+ git log -1 -p HEAD >log.two &&
+ (
+ cat log.one && echo &&
+ cat log.two && echo &&
+ cat log.one && echo &&
+ cat log.two
+ ) >expect &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t1412-reflog-loop.sh b/t/t1412-reflog-loop.sh
new file mode 100755
index 0000000..ff30874
--- /dev/null
+++ b/t/t1412-reflog-loop.sh
@@ -0,0 +1,33 @@
+#!/bin/sh
+
+test_description='reflog walk shows repeated commits again'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup commits' '
+ test_commit one file content &&
+ test_commit --append two file content
+'
+
+test_expect_success 'setup reflog with alternating commits' '
+ git checkout -b topic &&
+ git reset one &&
+ git reset two &&
+ git reset one &&
+ git reset two
+'
+
+test_expect_success 'reflog shows all entries' '
+ cat >expect <<-\EOF &&
+ topic@{0} reset: moving to two
+ topic@{1} reset: moving to one
+ topic@{2} reset: moving to two
+ topic@{3} reset: moving to one
+ topic@{4} branch: Created from HEAD
+ EOF
+ git log -g --format="%gd %gs" topic >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t1413-reflog-detach.sh b/t/t1413-reflog-detach.sh
new file mode 100755
index 0000000..d2a4822
--- /dev/null
+++ b/t/t1413-reflog-detach.sh
@@ -0,0 +1,73 @@
+#!/bin/sh
+
+test_description='Test reflog interaction with detached HEAD'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+reset_state () {
+ rm -rf .git && "$TAR" xf .git-saved.tar
+}
+
+test_expect_success setup '
+ test_tick &&
+ git commit --allow-empty -m initial &&
+ git branch side &&
+ test_tick &&
+ git commit --allow-empty -m second &&
+ "$TAR" cf .git-saved.tar .git
+'
+
+test_expect_success baseline '
+ reset_state &&
+ git rev-parse main main^ >expect &&
+ git log -g --format=%H >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'switch to branch' '
+ reset_state &&
+ git rev-parse side main main^ >expect &&
+ git checkout side &&
+ git log -g --format=%H >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'detach to other' '
+ reset_state &&
+ git rev-parse main side main main^ >expect &&
+ git checkout side &&
+ git checkout main^0 &&
+ git log -g --format=%H >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'detach to self' '
+ reset_state &&
+ git rev-parse main main main^ >expect &&
+ git checkout main^0 &&
+ git log -g --format=%H >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'attach to self' '
+ reset_state &&
+ git rev-parse main main main main^ >expect &&
+ git checkout main^0 &&
+ git checkout main &&
+ git log -g --format=%H >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'attach to other' '
+ reset_state &&
+ git rev-parse side main main main^ >expect &&
+ git checkout main^0 &&
+ git checkout side &&
+ git log -g --format=%H >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t1414-reflog-walk.sh b/t/t1414-reflog-walk.sh
new file mode 100755
index 0000000..ea64cec
--- /dev/null
+++ b/t/t1414-reflog-walk.sh
@@ -0,0 +1,139 @@
+#!/bin/sh
+
+test_description='various tests of reflog walk (log -g) behavior'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success 'set up some reflog entries' '
+ test_commit one &&
+ test_commit two &&
+ git checkout -b side HEAD^ &&
+ test_commit three &&
+ git merge --no-commit main &&
+ echo evil-merge-content >>one.t &&
+ test_tick &&
+ git commit --no-edit -a
+'
+
+do_walk () {
+ git log -g --format="%gd %gs" "$@"
+}
+
+test_expect_success 'set up expected reflog' '
+ cat >expect.all <<-EOF
+ HEAD@{0} commit (merge): Merge branch ${SQ}main${SQ} into side
+ HEAD@{1} commit: three
+ HEAD@{2} checkout: moving from main to side
+ HEAD@{3} commit: two
+ HEAD@{4} commit (initial): one
+ EOF
+'
+
+test_expect_success 'reflog walk shows expected logs' '
+ do_walk >actual &&
+ test_cmp expect.all actual
+'
+
+test_expect_success 'reflog can limit with --no-merges' '
+ grep -v merge expect.all >expect &&
+ do_walk --no-merges >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'reflog can limit with pathspecs' '
+ grep two expect.all >expect &&
+ do_walk -- two.t >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'pathspec limiting handles merges' '
+ # we pick up:
+ # - the initial commit of one
+ # - the checkout back to commit one
+ # - the evil merge which touched one
+ sed -n "1p;3p;5p" expect.all >expect &&
+ do_walk -- one.t >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--parents shows true parents' '
+ # convert newlines to spaces
+ echo $(git rev-parse HEAD HEAD^1 HEAD^2) >expect &&
+ git rev-list -g --parents -1 HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'walking multiple reflogs shows all' '
+ # We expect to see all entries for all reflogs, but interleaved by
+ # date, with order on the command line breaking ties. We
+ # can use "sort" on the separate lists to generate this,
+ # but note two tricks:
+ #
+ # 1. We use "{" as the delimiter, which lets us skip to the reflog
+ # date specifier as our second field, and then our "-n" numeric
+ # sort ignores the bits after the timestamp.
+ #
+ # 2. POSIX leaves undefined whether this is a stable sort or not. So
+ # we use "-k 1" to ensure that we see HEAD before main before
+ # side when breaking ties.
+ {
+ do_walk --date=unix HEAD &&
+ do_walk --date=unix side &&
+ do_walk --date=unix main
+ } >expect.raw &&
+ sort -t "{" -k 2nr -k 1 <expect.raw >expect &&
+ do_walk --date=unix HEAD main side >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'date-limiting does not interfere with other logs' '
+ do_walk HEAD@{1979-01-01} HEAD >actual &&
+ test_cmp expect.all actual
+'
+
+test_expect_success 'min/max age uses entry date to limit' '
+ # Flip between commits one and two so each ref update actually
+ # does something (and does not get optimized out). We know
+ # that the timestamps of those commits will be before our "min".
+
+ git update-ref -m before refs/heads/minmax one &&
+
+ test_tick &&
+ min=$test_tick &&
+ git update-ref -m min refs/heads/minmax two &&
+
+ test_tick &&
+ max=$test_tick &&
+ git update-ref -m max refs/heads/minmax one &&
+
+ test_tick &&
+ git update-ref -m after refs/heads/minmax two &&
+
+ cat >expect <<-\EOF &&
+ max
+ min
+ EOF
+ git log -g --since=$min --until=$max --format=%gs minmax >actual &&
+ test_cmp expect actual
+'
+
+# Create a situation where the reflog and ref database disagree about the latest
+# state of HEAD.
+test_expect_success REFFILES 'walk prefers reflog to ref tip' '
+ head=$(git rev-parse HEAD) &&
+ one=$(git rev-parse one) &&
+ ident="$GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE" &&
+ echo "$head $one $ident broken reflog entry" >>.git/logs/HEAD &&
+
+ echo $one >expect &&
+ git log -g --format=%H -1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rev-list -g complains when there are no reflogs' '
+ test_must_fail git rev-list -g
+'
+
+test_done
diff --git a/t/t1415-worktree-refs.sh b/t/t1415-worktree-refs.sh
new file mode 100755
index 0000000..3b53184
--- /dev/null
+++ b/t/t1415-worktree-refs.sh
@@ -0,0 +1,116 @@
+#!/bin/sh
+
+test_description='per-worktree refs'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ test_commit initial &&
+ test_commit wt1 &&
+ test_commit wt2 &&
+ git worktree add wt1 wt1 &&
+ git worktree add wt2 wt2 &&
+ git checkout initial &&
+ git update-ref refs/worktree/foo HEAD &&
+ git -C wt1 update-ref refs/worktree/foo HEAD &&
+ git -C wt2 update-ref refs/worktree/foo HEAD
+'
+
+# The 'packed-refs' file is stored directly in .git/. This means it is global
+# to the repository, and can only contain refs that are shared across all
+# worktrees.
+test_expect_success REFFILES 'refs/worktree must not be packed' '
+ git pack-refs --all &&
+ test_path_is_missing .git/refs/tags/wt1 &&
+ test_path_is_file .git/refs/worktree/foo &&
+ test_path_is_file .git/worktrees/wt1/refs/worktree/foo &&
+ test_path_is_file .git/worktrees/wt2/refs/worktree/foo
+'
+
+test_expect_success 'refs/worktree are per-worktree' '
+ test_cmp_rev worktree/foo initial &&
+ ( cd wt1 && test_cmp_rev worktree/foo wt1 ) &&
+ ( cd wt2 && test_cmp_rev worktree/foo wt2 )
+'
+
+test_expect_success 'resolve main-worktree/HEAD' '
+ test_cmp_rev main-worktree/HEAD initial &&
+ ( cd wt1 && test_cmp_rev main-worktree/HEAD initial ) &&
+ ( cd wt2 && test_cmp_rev main-worktree/HEAD initial )
+'
+
+test_expect_success 'ambiguous main-worktree/HEAD' '
+ test_when_finished git update-ref -d refs/heads/main-worktree/HEAD &&
+ git update-ref refs/heads/main-worktree/HEAD $(git rev-parse HEAD) &&
+ git rev-parse main-worktree/HEAD 2>warn &&
+ grep "main-worktree/HEAD.*ambiguous" warn
+'
+
+test_expect_success 'resolve worktrees/xx/HEAD' '
+ test_cmp_rev worktrees/wt1/HEAD wt1 &&
+ ( cd wt1 && test_cmp_rev worktrees/wt1/HEAD wt1 ) &&
+ ( cd wt2 && test_cmp_rev worktrees/wt1/HEAD wt1 )
+'
+
+test_expect_success 'ambiguous worktrees/xx/HEAD' '
+ git update-ref refs/heads/worktrees/wt1/HEAD $(git rev-parse HEAD) &&
+ test_when_finished git update-ref -d refs/heads/worktrees/wt1/HEAD &&
+ git rev-parse worktrees/wt1/HEAD 2>warn &&
+ grep "worktrees/wt1/HEAD.*ambiguous" warn
+'
+
+test_expect_success 'reflog of main-worktree/HEAD' '
+ git reflog HEAD | sed "s/HEAD/main-worktree\/HEAD/" >expected &&
+ git reflog main-worktree/HEAD >actual &&
+ test_cmp expected actual &&
+ git -C wt1 reflog main-worktree/HEAD >actual.wt1 &&
+ test_cmp expected actual.wt1
+'
+
+test_expect_success 'reflog of worktrees/xx/HEAD' '
+ git -C wt2 reflog HEAD | sed "s/HEAD/worktrees\/wt2\/HEAD/" >expected &&
+ git reflog worktrees/wt2/HEAD >actual &&
+ test_cmp expected actual &&
+ git -C wt1 reflog worktrees/wt2/HEAD >actual.wt1 &&
+ test_cmp expected actual.wt1 &&
+ git -C wt2 reflog worktrees/wt2/HEAD >actual.wt2 &&
+ test_cmp expected actual.wt2
+'
+
+test_expect_success 'for-each-ref from main worktree' '
+ mkdir fer1 &&
+ git -C fer1 init repo &&
+ test_commit -C fer1/repo initial &&
+ git -C fer1/repo worktree add ../second &&
+ git -C fer1/repo update-ref refs/bisect/first HEAD &&
+ git -C fer1/repo update-ref refs/rewritten/first HEAD &&
+ git -C fer1/repo update-ref refs/worktree/first HEAD &&
+ git -C fer1/repo for-each-ref --format="%(refname)" | grep first >actual &&
+ cat >expected <<-\EOF &&
+ refs/bisect/first
+ refs/rewritten/first
+ refs/worktree/first
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'for-each-ref from linked worktree' '
+ mkdir fer2 &&
+ git -C fer2 init repo &&
+ test_commit -C fer2/repo initial &&
+ git -C fer2/repo worktree add ../second &&
+ git -C fer2/second update-ref refs/bisect/second HEAD &&
+ git -C fer2/second update-ref refs/rewritten/second HEAD &&
+ git -C fer2/second update-ref refs/worktree/second HEAD &&
+ git -C fer2/second for-each-ref --format="%(refname)" | grep second >actual &&
+ cat >expected <<-\EOF &&
+ refs/bisect/second
+ refs/heads/second
+ refs/rewritten/second
+ refs/worktree/second
+ EOF
+ test_cmp expected actual
+'
+
+test_done
diff --git a/t/t1416-ref-transaction-hooks.sh b/t/t1416-ref-transaction-hooks.sh
new file mode 100755
index 0000000..2092488
--- /dev/null
+++ b/t/t1416-ref-transaction-hooks.sh
@@ -0,0 +1,137 @@
+#!/bin/sh
+
+test_description='reference transaction hooks'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ test_commit PRE &&
+ PRE_OID=$(git rev-parse PRE) &&
+ test_commit POST &&
+ POST_OID=$(git rev-parse POST)
+'
+
+test_expect_success 'hook allows updating ref if successful' '
+ git reset --hard PRE &&
+ test_hook reference-transaction <<-\EOF &&
+ echo "$*" >>actual
+ EOF
+ cat >expect <<-EOF &&
+ prepared
+ committed
+ EOF
+ git update-ref HEAD POST &&
+ test_cmp expect actual
+'
+
+test_expect_success 'hook aborts updating ref in prepared state' '
+ git reset --hard PRE &&
+ test_hook reference-transaction <<-\EOF &&
+ if test "$1" = prepared
+ then
+ exit 1
+ fi
+ EOF
+ test_must_fail git update-ref HEAD POST 2>err &&
+ test_grep "ref updates aborted by hook" err
+'
+
+test_expect_success 'hook gets all queued updates in prepared state' '
+ test_when_finished "rm actual" &&
+ git reset --hard PRE &&
+ test_hook reference-transaction <<-\EOF &&
+ if test "$1" = prepared
+ then
+ while read -r line
+ do
+ printf "%s\n" "$line"
+ done >actual
+ fi
+ EOF
+ cat >expect <<-EOF &&
+ $ZERO_OID $POST_OID HEAD
+ $ZERO_OID $POST_OID refs/heads/main
+ EOF
+ git update-ref HEAD POST <<-EOF &&
+ update HEAD $ZERO_OID $POST_OID
+ update refs/heads/main $ZERO_OID $POST_OID
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'hook gets all queued updates in committed state' '
+ test_when_finished "rm actual" &&
+ git reset --hard PRE &&
+ test_hook reference-transaction <<-\EOF &&
+ if test "$1" = committed
+ then
+ while read -r line
+ do
+ printf "%s\n" "$line"
+ done >actual
+ fi
+ EOF
+ cat >expect <<-EOF &&
+ $ZERO_OID $POST_OID HEAD
+ $ZERO_OID $POST_OID refs/heads/main
+ EOF
+ git update-ref HEAD POST &&
+ test_cmp expect actual
+'
+
+test_expect_success 'hook gets all queued updates in aborted state' '
+ test_when_finished "rm actual" &&
+ git reset --hard PRE &&
+ test_hook reference-transaction <<-\EOF &&
+ if test "$1" = aborted
+ then
+ while read -r line
+ do
+ printf "%s\n" "$line"
+ done >actual
+ fi
+ EOF
+ cat >expect <<-EOF &&
+ $ZERO_OID $POST_OID HEAD
+ $ZERO_OID $POST_OID refs/heads/main
+ EOF
+ git update-ref --stdin <<-EOF &&
+ start
+ update HEAD POST $ZERO_OID
+ update refs/heads/main POST $ZERO_OID
+ abort
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'interleaving hook calls succeed' '
+ test_when_finished "rm -r target-repo.git" &&
+
+ git init --bare target-repo.git &&
+
+ test_hook -C target-repo.git reference-transaction <<-\EOF &&
+ echo $0 "$@" >>actual
+ EOF
+
+ test_hook -C target-repo.git update <<-\EOF &&
+ echo $0 "$@" >>actual
+ EOF
+
+ cat >expect <<-EOF &&
+ hooks/update refs/tags/PRE $ZERO_OID $PRE_OID
+ hooks/reference-transaction prepared
+ hooks/reference-transaction committed
+ hooks/update refs/tags/POST $ZERO_OID $POST_OID
+ hooks/reference-transaction prepared
+ hooks/reference-transaction committed
+ EOF
+
+ git push ./target-repo.git PRE POST &&
+ test_cmp expect target-repo.git/actual
+'
+
+test_done
diff --git a/t/t1417-reflog-updateref.sh b/t/t1417-reflog-updateref.sh
new file mode 100755
index 0000000..14f13b5
--- /dev/null
+++ b/t/t1417-reflog-updateref.sh
@@ -0,0 +1,65 @@
+#!/bin/sh
+
+test_description='git reflog --updateref'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ git init -b main repo &&
+ (
+ cd repo &&
+
+ test_commit A &&
+ test_commit B &&
+ test_commit C &&
+
+ cp .git/logs/HEAD HEAD.old &&
+ git reset --hard HEAD~ &&
+ cp HEAD.old .git/logs/HEAD
+ )
+'
+
+test_reflog_updateref () {
+ exp=$1
+ shift
+ args="$@"
+
+ test_expect_success REFFILES "get '$exp' with '$args'" '
+ test_when_finished "rm -rf copy" &&
+ cp -R repo copy &&
+
+ (
+ cd copy &&
+
+ $args &&
+ git rev-parse $exp >expect &&
+ git rev-parse HEAD >actual &&
+
+ test_cmp expect actual
+ )
+ '
+}
+
+test_reflog_updateref B git reflog delete --updateref HEAD@{0}
+test_reflog_updateref B git reflog delete --updateref HEAD@{1}
+test_reflog_updateref C git reflog delete --updateref main@{0}
+test_reflog_updateref B git reflog delete --updateref main@{1}
+test_reflog_updateref B git reflog delete --updateref --rewrite HEAD@{0}
+test_reflog_updateref B git reflog delete --updateref --rewrite HEAD@{1}
+test_reflog_updateref C git reflog delete --updateref --rewrite main@{0}
+test_reflog_updateref B git reflog delete --updateref --rewrite main@{1}
+test_reflog_updateref B test_must_fail git reflog expire HEAD@{0}
+test_reflog_updateref B test_must_fail git reflog expire HEAD@{1}
+test_reflog_updateref B test_must_fail git reflog expire main@{0}
+test_reflog_updateref B test_must_fail git reflog expire main@{1}
+test_reflog_updateref B test_must_fail git reflog expire --updateref HEAD@{0}
+test_reflog_updateref B test_must_fail git reflog expire --updateref HEAD@{1}
+test_reflog_updateref B test_must_fail git reflog expire --updateref main@{0}
+test_reflog_updateref B test_must_fail git reflog expire --updateref main@{1}
+test_reflog_updateref B test_must_fail git reflog expire --updateref --rewrite HEAD@{0}
+test_reflog_updateref B test_must_fail git reflog expire --updateref --rewrite HEAD@{1}
+test_reflog_updateref B test_must_fail git reflog expire --updateref --rewrite main@{0}
+test_reflog_updateref B test_must_fail git reflog expire --updateref --rewrite main@{1}
+
+test_done
diff --git a/t/t1418-reflog-exists.sh b/t/t1418-reflog-exists.sh
new file mode 100755
index 0000000..2268bca
--- /dev/null
+++ b/t/t1418-reflog-exists.sh
@@ -0,0 +1,38 @@
+#!/bin/sh
+
+test_description='Test reflog display routines'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ test_commit A
+'
+
+test_expect_success 'usage' '
+ test_expect_code 129 git reflog exists &&
+ test_expect_code 129 git reflog exists -h
+'
+
+test_expect_success 'usage: unknown option' '
+ test_expect_code 129 git reflog exists --unknown-option
+'
+
+test_expect_success 'reflog exists works' '
+ git reflog exists refs/heads/main &&
+ test_must_fail git reflog exists refs/heads/nonexistent
+'
+
+test_expect_success 'reflog exists works with a "--" delimiter' '
+ git reflog exists -- refs/heads/main &&
+ test_must_fail git reflog exists -- refs/heads/nonexistent
+'
+
+test_expect_success 'reflog exists works with a "--end-of-options" delimiter' '
+ git reflog exists --end-of-options refs/heads/main &&
+ test_must_fail git reflog exists --end-of-options refs/heads/nonexistent
+'
+
+test_done
diff --git a/t/t1419-exclude-refs.sh b/t/t1419-exclude-refs.sh
new file mode 100755
index 0000000..5d8c86b
--- /dev/null
+++ b/t/t1419-exclude-refs.sh
@@ -0,0 +1,122 @@
+#!/bin/sh
+
+test_description='test exclude_patterns functionality in main ref store'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+for_each_ref__exclude () {
+ GIT_TRACE2_PERF=1 test-tool ref-store main \
+ for-each-ref--exclude "$@" >actual.raw
+ cut -d ' ' -f 2 actual.raw
+}
+
+for_each_ref () {
+ git for-each-ref --format='%(refname)' "$@"
+}
+
+assert_jumps () {
+ local nr="$1"
+ local trace="$2"
+
+ grep -q "name:jumps_made value:$nr$" $trace
+}
+
+assert_no_jumps () {
+ ! assert_jumps ".*" "$1"
+}
+
+test_expect_success 'setup' '
+ test_commit --no-tag base &&
+ base="$(git rev-parse HEAD)" &&
+
+ for name in foo bar baz quux
+ do
+ for i in 1 2 3
+ do
+ echo "create refs/heads/$name/$i $base" || return 1
+ done || return 1
+ done >in &&
+ echo "delete refs/heads/main" >>in &&
+
+ git update-ref --stdin <in &&
+ git pack-refs --all
+'
+
+test_expect_success 'excluded region in middle' '
+ for_each_ref__exclude refs/heads refs/heads/foo >actual 2>perf &&
+ for_each_ref refs/heads/bar refs/heads/baz refs/heads/quux >expect &&
+
+ test_cmp expect actual &&
+ assert_jumps 1 perf
+'
+
+test_expect_success 'excluded region at beginning' '
+ for_each_ref__exclude refs/heads refs/heads/bar >actual 2>perf &&
+ for_each_ref refs/heads/baz refs/heads/foo refs/heads/quux >expect &&
+
+ test_cmp expect actual &&
+ assert_jumps 1 perf
+'
+
+test_expect_success 'excluded region at end' '
+ for_each_ref__exclude refs/heads refs/heads/quux >actual 2>perf &&
+ for_each_ref refs/heads/foo refs/heads/bar refs/heads/baz >expect &&
+
+ test_cmp expect actual &&
+ assert_jumps 1 perf
+'
+
+test_expect_success 'disjoint excluded regions' '
+ for_each_ref__exclude refs/heads refs/heads/bar refs/heads/quux >actual 2>perf &&
+ for_each_ref refs/heads/baz refs/heads/foo >expect &&
+
+ test_cmp expect actual &&
+ assert_jumps 2 perf
+'
+
+test_expect_success 'adjacent, non-overlapping excluded regions' '
+ for_each_ref__exclude refs/heads refs/heads/bar refs/heads/baz >actual 2>perf &&
+ for_each_ref refs/heads/foo refs/heads/quux >expect &&
+
+ test_cmp expect actual &&
+ assert_jumps 1 perf
+'
+
+test_expect_success 'overlapping excluded regions' '
+ for_each_ref__exclude refs/heads refs/heads/ba refs/heads/baz >actual 2>perf &&
+ for_each_ref refs/heads/foo refs/heads/quux >expect &&
+
+ test_cmp expect actual &&
+ assert_jumps 1 perf
+'
+
+test_expect_success 'several overlapping excluded regions' '
+ for_each_ref__exclude refs/heads \
+ refs/heads/bar refs/heads/baz refs/heads/foo >actual 2>perf &&
+ for_each_ref refs/heads/quux >expect &&
+
+ test_cmp expect actual &&
+ assert_jumps 1 perf
+'
+
+test_expect_success 'non-matching excluded section' '
+ for_each_ref__exclude refs/heads refs/heads/does/not/exist >actual 2>perf &&
+ for_each_ref >expect &&
+
+ test_cmp expect actual &&
+ assert_no_jumps perf
+'
+
+test_expect_success 'meta-characters are discarded' '
+ for_each_ref__exclude refs/heads "refs/heads/ba*" >actual 2>perf &&
+ for_each_ref >expect &&
+
+ test_cmp expect actual &&
+ assert_no_jumps perf
+'
+
+test_done
diff --git a/t/t1420-lost-found.sh b/t/t1420-lost-found.sh
new file mode 100755
index 0000000..dbe15a0
--- /dev/null
+++ b/t/t1420-lost-found.sh
@@ -0,0 +1,37 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Johannes E. Schindelin
+#
+
+test_description='Test fsck --lost-found'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ git config core.logAllRefUpdates 0 &&
+ : > file1 &&
+ git add file1 &&
+ test_tick &&
+ git commit -m initial &&
+ echo 1 > file1 &&
+ echo 2 > file2 &&
+ git add file1 file2 &&
+ test_tick &&
+ git commit -m second &&
+ echo 3 > file3 &&
+ git add file3
+'
+
+test_expect_success 'lost and found something' '
+ git rev-parse HEAD > lost-commit &&
+ git rev-parse :file3 > lost-other &&
+ test_tick &&
+ git reset --hard HEAD^ &&
+ git fsck --lost-found &&
+ test 2 = $(ls .git/lost-found/*/* | wc -l) &&
+ test -f .git/lost-found/commit/$(cat lost-commit) &&
+ test -f .git/lost-found/other/$(cat lost-other)
+'
+
+test_done
diff --git a/t/t1430-bad-ref-name.sh b/t/t1430-bad-ref-name.sh
new file mode 100755
index 0000000..68cc9e7
--- /dev/null
+++ b/t/t1430-bad-ref-name.sh
@@ -0,0 +1,393 @@
+#!/bin/sh
+
+test_description='Test handling of ref names that check-ref-format rejects'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ test_commit one &&
+ test_commit two &&
+ main_sha1=$(git rev-parse refs/heads/main)
+'
+
+test_expect_success 'fast-import: fail on invalid branch name ".badbranchname"' '
+ test_when_finished "rm -f .git/objects/pack_* .git/objects/index_*" &&
+ cat >input <<-INPUT_END &&
+ commit .badbranchname
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ corrupt
+ COMMIT
+
+ from refs/heads/main
+
+ INPUT_END
+ test_must_fail git fast-import <input
+'
+
+test_expect_success 'fast-import: fail on invalid branch name "bad[branch]name"' '
+ test_when_finished "rm -f .git/objects/pack_* .git/objects/index_*" &&
+ cat >input <<-INPUT_END &&
+ commit bad[branch]name
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ corrupt
+ COMMIT
+
+ from refs/heads/main
+
+ INPUT_END
+ test_must_fail git fast-import <input
+'
+
+test_expect_success 'git branch shows badly named ref as warning' '
+ test-tool ref-store main update-ref msg "refs/heads/broken...ref" $main_sha1 $ZERO_OID REF_SKIP_REFNAME_VERIFICATION &&
+ test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...ref" &&
+ git branch >output 2>error &&
+ test_grep -e "ignoring ref with broken name refs/heads/broken\.\.\.ref" error &&
+ ! grep -e "broken\.\.\.ref" output
+'
+
+test_expect_success 'branch -d can delete badly named ref' '
+ test-tool ref-store main update-ref msg "refs/heads/broken...ref" $main_sha1 $ZERO_OID REF_SKIP_REFNAME_VERIFICATION &&
+ test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...ref" &&
+ git branch -d broken...ref &&
+ git branch >output 2>error &&
+ ! grep -e "broken\.\.\.ref" error &&
+ ! grep -e "broken\.\.\.ref" output
+'
+
+test_expect_success 'branch -D can delete badly named ref' '
+ test-tool ref-store main update-ref msg "refs/heads/broken...ref" $main_sha1 $ZERO_OID REF_SKIP_REFNAME_VERIFICATION &&
+ test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...ref" &&
+ git branch -D broken...ref &&
+ git branch >output 2>error &&
+ ! grep -e "broken\.\.\.ref" error &&
+ ! grep -e "broken\.\.\.ref" output
+'
+
+test_expect_success 'branch -D cannot delete non-ref in .git dir' '
+ echo precious >.git/my-private-file &&
+ echo precious >expect &&
+ test_must_fail git branch -D ../../my-private-file &&
+ test_cmp expect .git/my-private-file
+'
+
+test_expect_success 'branch -D cannot delete ref in .git dir' '
+ git rev-parse HEAD >.git/my-private-file &&
+ git rev-parse HEAD >expect &&
+ git branch foo/legit &&
+ test_must_fail git branch -D foo////./././../../../my-private-file &&
+ test_cmp expect .git/my-private-file
+'
+
+test_expect_success 'branch -D cannot delete absolute path' '
+ git branch -f extra &&
+ test_must_fail git branch -D "$(pwd)/.git/refs/heads/extra" &&
+ test_cmp_rev HEAD extra
+'
+
+test_expect_success 'git branch cannot create a badly named ref' '
+ test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...ref" &&
+ test_must_fail git branch broken...ref &&
+ git branch >output 2>error &&
+ ! grep -e "broken\.\.\.ref" error &&
+ ! grep -e "broken\.\.\.ref" output
+'
+
+test_expect_success 'branch -m cannot rename to a bad ref name' '
+ test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...ref" &&
+ test_might_fail git branch -D goodref &&
+ git branch goodref &&
+ test_must_fail git branch -m goodref broken...ref &&
+ test_cmp_rev main goodref &&
+ git branch >output 2>error &&
+ ! grep -e "broken\.\.\.ref" error &&
+ ! grep -e "broken\.\.\.ref" output
+'
+
+test_expect_failure 'branch -m can rename from a bad ref name' '
+ test-tool ref-store main update-ref msg "refs/heads/broken...ref" $main_sha1 $ZERO_OID REF_SKIP_REFNAME_VERIFICATION &&
+
+ test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...ref" &&
+ git branch -m broken...ref renamed &&
+ test_cmp_rev main renamed &&
+ git branch >output 2>error &&
+ ! grep -e "broken\.\.\.ref" error &&
+ ! grep -e "broken\.\.\.ref" output
+'
+
+test_expect_success 'push cannot create a badly named ref' '
+ test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...ref" &&
+ test_must_fail git push "file://$(pwd)" HEAD:refs/heads/broken...ref &&
+ git branch >output 2>error &&
+ ! grep -e "broken\.\.\.ref" error &&
+ ! grep -e "broken\.\.\.ref" output
+'
+
+test_expect_failure 'push --mirror can delete badly named ref' '
+ top=$(pwd) &&
+ git init src &&
+ git init dest &&
+
+ (
+ cd src &&
+ test_commit one
+ ) &&
+ (
+ cd dest &&
+ test_commit two &&
+ git checkout --detach &&
+ test-tool ref-store main update-ref msg "refs/heads/broken...ref" $main_sha1 $ZERO_OID REF_SKIP_REFNAME_VERIFICATION
+ ) &&
+ git -C src push --mirror "file://$top/dest" &&
+ git -C dest branch >output 2>error &&
+ ! grep -e "broken\.\.\.ref" error &&
+ ! grep -e "broken\.\.\.ref" output
+'
+
+test_expect_success 'rev-parse skips symref pointing to broken name' '
+ test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...ref" &&
+ git branch shadow one &&
+ test-tool ref-store main update-ref msg "refs/heads/broken...ref" $main_sha1 $ZERO_OID REF_SKIP_REFNAME_VERIFICATION &&
+ test-tool ref-store main create-symref refs/tags/shadow refs/heads/broken...ref msg &&
+ test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/tags/shadow" &&
+ git rev-parse --verify one >expect &&
+ git rev-parse --verify shadow >actual 2>err &&
+ test_cmp expect actual &&
+ test_grep "ignoring dangling symref refs/tags/shadow" err
+'
+
+test_expect_success 'for-each-ref emits warnings for broken names' '
+ test-tool ref-store main update-ref msg "refs/heads/broken...ref" $main_sha1 $ZERO_OID REF_SKIP_REFNAME_VERIFICATION &&
+ test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...ref" &&
+ printf "ref: refs/heads/broken...ref\n" >.git/refs/heads/badname &&
+ test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/badname" &&
+ printf "ref: refs/heads/main\n" >.git/refs/heads/broken...symref &&
+ test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...symref" &&
+ git for-each-ref >output 2>error &&
+ ! grep -e "broken\.\.\.ref" output &&
+ ! grep -e "badname" output &&
+ ! grep -e "broken\.\.\.symref" output &&
+ test_grep "ignoring ref with broken name refs/heads/broken\.\.\.ref" error &&
+ test_grep ! "ignoring broken ref refs/heads/badname" error &&
+ test_grep "ignoring ref with broken name refs/heads/broken\.\.\.symref" error
+'
+
+test_expect_success 'update-ref -d can delete broken name' '
+ test-tool ref-store main update-ref msg "refs/heads/broken...ref" $main_sha1 $ZERO_OID REF_SKIP_REFNAME_VERIFICATION &&
+ test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...ref" &&
+ git update-ref -d refs/heads/broken...ref >output 2>error &&
+ test_must_be_empty output &&
+ test_must_be_empty error &&
+ git branch >output 2>error &&
+ ! grep -e "broken\.\.\.ref" error &&
+ ! grep -e "broken\.\.\.ref" output
+'
+
+test_expect_success 'branch -d can delete broken name' '
+ test-tool ref-store main update-ref msg "refs/heads/broken...ref" $main_sha1 $ZERO_OID REF_SKIP_REFNAME_VERIFICATION &&
+ test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...ref" &&
+ git branch -d broken...ref >output 2>error &&
+ test_grep "Deleted branch broken...ref (was broken)" output &&
+ test_must_be_empty error &&
+ git branch >output 2>error &&
+ ! grep -e "broken\.\.\.ref" error &&
+ ! grep -e "broken\.\.\.ref" output
+'
+
+test_expect_success 'update-ref --no-deref -d can delete symref to broken name' '
+ test-tool ref-store main update-ref msg "refs/heads/broken...ref" $main_sha1 $ZERO_OID REF_SKIP_REFNAME_VERIFICATION &&
+
+ test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...ref" &&
+ test-tool ref-store main create-symref refs/heads/badname refs/heads/broken...ref msg &&
+ test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/badname" &&
+ test_ref_exists refs/heads/badname &&
+ git update-ref --no-deref -d refs/heads/badname >output 2>error &&
+ test_ref_missing refs/heads/badname &&
+ test_must_be_empty output &&
+ test_must_be_empty error
+'
+
+test_expect_success 'branch -d can delete symref to broken name' '
+ test-tool ref-store main update-ref msg "refs/heads/broken...ref" $main_sha1 $ZERO_OID REF_SKIP_REFNAME_VERIFICATION &&
+ test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...ref" &&
+ test-tool ref-store main create-symref refs/heads/badname refs/heads/broken...ref msg &&
+ test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/badname" &&
+ test_ref_exists refs/heads/badname &&
+ git branch -d badname >output 2>error &&
+ test_ref_missing refs/heads/badname &&
+ test_grep "Deleted branch badname (was refs/heads/broken\.\.\.ref)" output &&
+ test_must_be_empty error
+'
+
+test_expect_success 'update-ref --no-deref -d can delete dangling symref to broken name' '
+ test-tool ref-store main create-symref refs/heads/badname refs/heads/broken...ref msg &&
+ test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/badname" &&
+ test_ref_exists refs/heads/badname &&
+ git update-ref --no-deref -d refs/heads/badname >output 2>error &&
+ test_ref_missing refs/heads/badname &&
+ test_must_be_empty output &&
+ test_must_be_empty error
+'
+
+test_expect_success 'branch -d can delete dangling symref to broken name' '
+ test-tool ref-store main create-symref refs/heads/badname refs/heads/broken...ref msg &&
+ test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/badname" &&
+ test_ref_exists refs/heads/badname &&
+ git branch -d badname >output 2>error &&
+ test_ref_missing refs/heads/badname &&
+ test_grep "Deleted branch badname (was refs/heads/broken\.\.\.ref)" output &&
+ test_must_be_empty error
+'
+
+test_expect_success 'update-ref -d can delete broken name through symref' '
+ test-tool ref-store main update-ref msg "refs/heads/broken...ref" $main_sha1 $ZERO_OID REF_SKIP_REFNAME_VERIFICATION &&
+ test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...ref" &&
+ test-tool ref-store main create-symref refs/heads/badname refs/heads/broken...ref msg &&
+ test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/badname" &&
+ test_ref_exists refs/heads/broken...ref &&
+ git update-ref -d refs/heads/badname >output 2>error &&
+ test_ref_missing refs/heads/broken...ref &&
+ test_must_be_empty output &&
+ test_must_be_empty error
+'
+
+test_expect_success 'update-ref --no-deref -d can delete symref with broken name' '
+ printf "ref: refs/heads/main\n" >.git/refs/heads/broken...symref &&
+ test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...symref" &&
+ test_ref_exists refs/heads/broken...symref &&
+ git update-ref --no-deref -d refs/heads/broken...symref >output 2>error &&
+ test_ref_missing refs/heads/broken...symref &&
+ test_must_be_empty output &&
+ test_must_be_empty error
+'
+
+test_expect_success 'branch -d can delete symref with broken name' '
+ printf "ref: refs/heads/main\n" >.git/refs/heads/broken...symref &&
+ test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...symref" &&
+ test_ref_exists refs/heads/broken...symref &&
+ git branch -d broken...symref >output 2>error &&
+ test_ref_missing refs/heads/broken...symref &&
+ test_grep "Deleted branch broken...symref (was refs/heads/main)" output &&
+ test_must_be_empty error
+'
+
+test_expect_success 'update-ref --no-deref -d can delete dangling symref with broken name' '
+ printf "ref: refs/heads/idonotexist\n" >.git/refs/heads/broken...symref &&
+ test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...symref" &&
+ test_ref_exists refs/heads/broken...symref &&
+ git update-ref --no-deref -d refs/heads/broken...symref >output 2>error &&
+ test_ref_missing refs/heads/broken...symref &&
+ test_must_be_empty output &&
+ test_must_be_empty error
+'
+
+test_expect_success 'branch -d can delete dangling symref with broken name' '
+ printf "ref: refs/heads/idonotexist\n" >.git/refs/heads/broken...symref &&
+ test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/broken...symref" &&
+ test_ref_exists refs/heads/broken...symref &&
+ git branch -d broken...symref >output 2>error &&
+ test_ref_missing refs/heads/broken...symref &&
+ test_grep "Deleted branch broken...symref (was refs/heads/idonotexist)" output &&
+ test_must_be_empty error
+'
+
+test_expect_success 'update-ref -d cannot delete non-ref in .git dir' '
+ echo precious >.git/my-private-file &&
+ echo precious >expect &&
+ test_must_fail git update-ref -d my-private-file >output 2>error &&
+ test_must_be_empty output &&
+ test_grep -e "refusing to update ref with bad name" error &&
+ test_cmp expect .git/my-private-file
+'
+
+test_expect_success 'update-ref -d cannot delete absolute path' '
+ git branch -f extra &&
+ test_must_fail git update-ref -d "$(pwd)/.git/refs/heads/extra" &&
+ test_cmp_rev HEAD extra
+'
+
+test_expect_success 'update-ref --stdin fails create with bad ref name' '
+ echo "create ~a refs/heads/main" >stdin &&
+ test_must_fail git update-ref --stdin <stdin 2>err &&
+ grep "fatal: invalid ref format: ~a" err
+'
+
+test_expect_success 'update-ref --stdin fails update with bad ref name' '
+ echo "update ~a refs/heads/main" >stdin &&
+ test_must_fail git update-ref --stdin <stdin 2>err &&
+ grep "fatal: invalid ref format: ~a" err
+'
+
+test_expect_success 'update-ref --stdin fails delete with bad ref name' '
+ echo "delete ~a refs/heads/main" >stdin &&
+ test_must_fail git update-ref --stdin <stdin 2>err &&
+ grep "fatal: invalid ref format: ~a" err
+'
+
+test_expect_success 'update-ref --stdin -z fails create with bad ref name' '
+ printf "%s\0" "create ~a " refs/heads/main >stdin &&
+ test_must_fail git update-ref -z --stdin <stdin 2>err &&
+ grep "fatal: invalid ref format: ~a " err
+'
+
+test_expect_success 'update-ref --stdin -z fails update with bad ref name' '
+ printf "%s\0" "update ~a" refs/heads/main "" >stdin &&
+ test_must_fail git update-ref -z --stdin <stdin 2>err &&
+ grep "fatal: invalid ref format: ~a" err
+'
+
+test_expect_success 'update-ref --stdin -z fails delete with bad ref name' '
+ printf "%s\0" "delete ~a" refs/heads/main >stdin &&
+ test_must_fail git update-ref -z --stdin <stdin 2>err &&
+ grep "fatal: invalid ref format: ~a" err
+'
+
+test_expect_success 'branch rejects HEAD as a branch name' '
+ test_must_fail git branch HEAD HEAD^ &&
+ test_must_fail git show-ref refs/heads/HEAD
+'
+
+test_expect_success 'checkout -b rejects HEAD as a branch name' '
+ test_must_fail git checkout -B HEAD HEAD^ &&
+ test_must_fail git show-ref refs/heads/HEAD
+'
+
+test_expect_success 'update-ref can operate on refs/heads/HEAD' '
+ git update-ref refs/heads/HEAD HEAD^ &&
+ git show-ref refs/heads/HEAD &&
+ git update-ref -d refs/heads/HEAD &&
+ test_must_fail git show-ref refs/heads/HEAD
+'
+
+test_expect_success 'branch -d can remove refs/heads/HEAD' '
+ git update-ref refs/heads/HEAD HEAD^ &&
+ git branch -d HEAD &&
+ test_must_fail git show-ref refs/heads/HEAD
+'
+
+test_expect_success 'branch -m can rename refs/heads/HEAD' '
+ git update-ref refs/heads/HEAD HEAD^ &&
+ git branch -m HEAD tail &&
+ test_must_fail git show-ref refs/heads/HEAD &&
+ git show-ref refs/heads/tail
+'
+
+test_expect_success 'branch -d can remove refs/heads/-dash' '
+ git update-ref refs/heads/-dash HEAD^ &&
+ git branch -d -- -dash &&
+ test_must_fail git show-ref refs/heads/-dash
+'
+
+test_expect_success 'branch -m can rename refs/heads/-dash' '
+ git update-ref refs/heads/-dash HEAD^ &&
+ git branch -m -- -dash dash &&
+ test_must_fail git show-ref refs/heads/-dash &&
+ git show-ref refs/heads/dash
+'
+
+test_done
diff --git a/t/t1450-fsck.sh b/t/t1450-fsck.sh
new file mode 100755
index 0000000..0e3e87d
--- /dev/null
+++ b/t/t1450-fsck.sh
@@ -0,0 +1,1063 @@
+#!/bin/sh
+
+test_description='git fsck random collection of tests
+
+* (HEAD) B
+* (main) A
+'
+
+. ./test-lib.sh
+
+test_expect_success setup '
+ git config gc.auto 0 &&
+ git config i18n.commitencoding ISO-8859-1 &&
+ test_commit A fileA one &&
+ git config --unset i18n.commitencoding &&
+ git checkout HEAD^0 &&
+ test_commit B fileB two &&
+ git tag -d A B &&
+ git reflog expire --expire=now --all
+'
+
+test_expect_success 'loose objects borrowed from alternate are not missing' '
+ mkdir another &&
+ (
+ cd another &&
+ git init &&
+ echo ../../../.git/objects >.git/objects/info/alternates &&
+ test_commit C fileC one &&
+ git fsck --no-dangling >../actual 2>&1
+ ) &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'HEAD is part of refs, valid objects appear valid' '
+ git fsck >actual 2>&1 &&
+ test_must_be_empty actual
+'
+
+# Corruption tests follow. Make sure to remove all traces of the
+# specific corruption you test afterwards, lest a later test trip over
+# it.
+
+sha1_file () {
+ git rev-parse --git-path objects/$(test_oid_to_path "$1")
+}
+
+remove_object () {
+ rm "$(sha1_file "$1")"
+}
+
+test_expect_success 'object with hash mismatch' '
+ git init --bare hash-mismatch &&
+ (
+ cd hash-mismatch &&
+
+ oid=$(echo blob | git hash-object -w --stdin) &&
+ oldoid=$oid &&
+ old=$(test_oid_to_path "$oid") &&
+ new=$(dirname $old)/$(test_oid ff_2) &&
+ oid="$(dirname $new)$(basename $new)" &&
+
+ mv objects/$old objects/$new &&
+ git update-index --add --cacheinfo 100644 $oid foo &&
+ tree=$(git write-tree) &&
+ cmt=$(echo bogus | git commit-tree $tree) &&
+ git update-ref refs/heads/bogus $cmt &&
+
+ test_must_fail git fsck 2>out &&
+ grep "$oldoid: hash-path mismatch, found at: .*$new" out
+ )
+'
+
+test_expect_success 'object with hash and type mismatch' '
+ git init --bare hash-type-mismatch &&
+ (
+ cd hash-type-mismatch &&
+
+ oid=$(echo blob | git hash-object -w --stdin -t garbage --literally) &&
+ oldoid=$oid &&
+ old=$(test_oid_to_path "$oid") &&
+ new=$(dirname $old)/$(test_oid ff_2) &&
+ oid="$(dirname $new)$(basename $new)" &&
+
+ mv objects/$old objects/$new &&
+ git update-index --add --cacheinfo 100644 $oid foo &&
+ tree=$(git write-tree) &&
+ cmt=$(echo bogus | git commit-tree $tree) &&
+ git update-ref refs/heads/bogus $cmt &&
+
+
+ test_must_fail git fsck 2>out &&
+ grep "^error: $oldoid: hash-path mismatch, found at: .*$new" out &&
+ grep "^error: $oldoid: object is of unknown type '"'"'garbage'"'"'" out
+ )
+'
+
+test_expect_success 'zlib corrupt loose object output ' '
+ git init --bare corrupt-loose-output &&
+ (
+ cd corrupt-loose-output &&
+ oid=$(git hash-object -w --stdin --literally </dev/null) &&
+ oidf=objects/$(test_oid_to_path "$oid") &&
+ chmod +w $oidf &&
+ echo extra garbage >>$oidf &&
+
+ cat >expect.error <<-EOF &&
+ error: garbage at end of loose object '\''$oid'\''
+ error: unable to unpack contents of ./$oidf
+ error: $oid: object corrupt or missing: ./$oidf
+ EOF
+ test_must_fail git fsck 2>actual &&
+ grep ^error: actual >error &&
+ test_cmp expect.error error
+ )
+'
+
+test_expect_success 'branch pointing to non-commit' '
+ git rev-parse HEAD^{tree} >.git/refs/heads/invalid &&
+ test_when_finished "git update-ref -d refs/heads/invalid" &&
+ test_must_fail git fsck 2>out &&
+ test_grep "not a commit" out
+'
+
+test_expect_success 'HEAD link pointing at a funny object' '
+ test_when_finished "mv .git/SAVED_HEAD .git/HEAD" &&
+ mv .git/HEAD .git/SAVED_HEAD &&
+ echo $ZERO_OID >.git/HEAD &&
+ # avoid corrupt/broken HEAD from interfering with repo discovery
+ test_must_fail env GIT_DIR=.git git fsck 2>out &&
+ test_grep "detached HEAD points" out
+'
+
+test_expect_success 'HEAD link pointing at a funny place' '
+ test_when_finished "mv .git/SAVED_HEAD .git/HEAD" &&
+ mv .git/HEAD .git/SAVED_HEAD &&
+ echo "ref: refs/funny/place" >.git/HEAD &&
+ # avoid corrupt/broken HEAD from interfering with repo discovery
+ test_must_fail env GIT_DIR=.git git fsck 2>out &&
+ test_grep "HEAD points to something strange" out
+'
+
+test_expect_success 'HEAD link pointing at a funny object (from different wt)' '
+ test_when_finished "mv .git/SAVED_HEAD .git/HEAD" &&
+ test_when_finished "rm -rf .git/worktrees wt" &&
+ git worktree add wt &&
+ mv .git/HEAD .git/SAVED_HEAD &&
+ echo $ZERO_OID >.git/HEAD &&
+ # avoid corrupt/broken HEAD from interfering with repo discovery
+ test_must_fail git -C wt fsck 2>out &&
+ test_grep "main-worktree/HEAD: detached HEAD points" out
+'
+
+test_expect_success 'other worktree HEAD link pointing at a funny object' '
+ test_when_finished "rm -rf .git/worktrees other" &&
+ git worktree add other &&
+ echo $ZERO_OID >.git/worktrees/other/HEAD &&
+ test_must_fail git fsck 2>out &&
+ test_grep "worktrees/other/HEAD: detached HEAD points" out
+'
+
+test_expect_success 'other worktree HEAD link pointing at missing object' '
+ test_when_finished "rm -rf .git/worktrees other" &&
+ git worktree add other &&
+ echo "Contents missing from repo" | git hash-object --stdin >.git/worktrees/other/HEAD &&
+ test_must_fail git fsck 2>out &&
+ test_grep "worktrees/other/HEAD: invalid sha1 pointer" out
+'
+
+test_expect_success 'other worktree HEAD link pointing at a funny place' '
+ test_when_finished "rm -rf .git/worktrees other" &&
+ git worktree add other &&
+ echo "ref: refs/funny/place" >.git/worktrees/other/HEAD &&
+ test_must_fail git fsck 2>out &&
+ test_grep "worktrees/other/HEAD points to something strange" out
+'
+
+test_expect_success 'commit with multiple signatures is okay' '
+ git cat-file commit HEAD >basis &&
+ cat >sigs <<-EOF &&
+ gpgsig -----BEGIN PGP SIGNATURE-----
+ VGhpcyBpcyBub3QgcmVhbGx5IGEgc2lnbmF0dXJlLg==
+ -----END PGP SIGNATURE-----
+ gpgsig-sha256 -----BEGIN PGP SIGNATURE-----
+ VGhpcyBpcyBub3QgcmVhbGx5IGEgc2lnbmF0dXJlLg==
+ -----END PGP SIGNATURE-----
+ EOF
+ sed -e "/^committer/q" basis >okay &&
+ cat sigs >>okay &&
+ echo >>okay &&
+ sed -e "1,/^$/d" basis >>okay &&
+ cat okay &&
+ new=$(git hash-object -t commit -w --stdin <okay) &&
+ test_when_finished "remove_object $new" &&
+ git update-ref refs/heads/bogus "$new" &&
+ test_when_finished "git update-ref -d refs/heads/bogus" &&
+ git fsck 2>out &&
+ cat out &&
+ ! grep "commit $new" out
+'
+
+test_expect_success 'email without @ is okay' '
+ git cat-file commit HEAD >basis &&
+ sed "s/@/AT/" basis >okay &&
+ new=$(git hash-object -t commit -w --stdin <okay) &&
+ test_when_finished "remove_object $new" &&
+ git update-ref refs/heads/bogus "$new" &&
+ test_when_finished "git update-ref -d refs/heads/bogus" &&
+ git fsck 2>out &&
+ ! grep "commit $new" out
+'
+
+test_expect_success 'email with embedded > is not okay' '
+ git cat-file commit HEAD >basis &&
+ sed "s/@[a-z]/&>/" basis >bad-email &&
+ new=$(git hash-object --literally -t commit -w --stdin <bad-email) &&
+ test_when_finished "remove_object $new" &&
+ git update-ref refs/heads/bogus "$new" &&
+ test_when_finished "git update-ref -d refs/heads/bogus" &&
+ test_must_fail git fsck 2>out &&
+ test_grep "error in commit $new" out
+'
+
+test_expect_success 'missing < email delimiter is reported nicely' '
+ git cat-file commit HEAD >basis &&
+ sed "s/<//" basis >bad-email-2 &&
+ new=$(git hash-object --literally -t commit -w --stdin <bad-email-2) &&
+ test_when_finished "remove_object $new" &&
+ git update-ref refs/heads/bogus "$new" &&
+ test_when_finished "git update-ref -d refs/heads/bogus" &&
+ test_must_fail git fsck 2>out &&
+ test_grep "error in commit $new.* - bad name" out
+'
+
+test_expect_success 'missing email is reported nicely' '
+ git cat-file commit HEAD >basis &&
+ sed "s/[a-z]* <[^>]*>//" basis >bad-email-3 &&
+ new=$(git hash-object --literally -t commit -w --stdin <bad-email-3) &&
+ test_when_finished "remove_object $new" &&
+ git update-ref refs/heads/bogus "$new" &&
+ test_when_finished "git update-ref -d refs/heads/bogus" &&
+ test_must_fail git fsck 2>out &&
+ test_grep "error in commit $new.* - missing email" out
+'
+
+test_expect_success '> in name is reported' '
+ git cat-file commit HEAD >basis &&
+ sed "s/ </> </" basis >bad-email-4 &&
+ new=$(git hash-object --literally -t commit -w --stdin <bad-email-4) &&
+ test_when_finished "remove_object $new" &&
+ git update-ref refs/heads/bogus "$new" &&
+ test_when_finished "git update-ref -d refs/heads/bogus" &&
+ test_must_fail git fsck 2>out &&
+ test_grep "error in commit $new" out
+'
+
+# date is 2^64 + 1
+test_expect_success 'integer overflow in timestamps is reported' '
+ git cat-file commit HEAD >basis &&
+ sed "s/^\\(author .*>\\) [0-9]*/\\1 18446744073709551617/" \
+ <basis >bad-timestamp &&
+ new=$(git hash-object --literally -t commit -w --stdin <bad-timestamp) &&
+ test_when_finished "remove_object $new" &&
+ git update-ref refs/heads/bogus "$new" &&
+ test_when_finished "git update-ref -d refs/heads/bogus" &&
+ test_must_fail git fsck 2>out &&
+ test_grep "error in commit $new.*integer overflow" out
+'
+
+test_expect_success 'commit with NUL in header' '
+ git cat-file commit HEAD >basis &&
+ sed "s/author ./author Q/" <basis | q_to_nul >commit-NUL-header &&
+ new=$(git hash-object --literally -t commit -w --stdin <commit-NUL-header) &&
+ test_when_finished "remove_object $new" &&
+ git update-ref refs/heads/bogus "$new" &&
+ test_when_finished "git update-ref -d refs/heads/bogus" &&
+ test_must_fail git fsck 2>out &&
+ test_grep "error in commit $new.*unterminated header: NUL at offset" out
+'
+
+test_expect_success 'tree object with duplicate entries' '
+ test_when_finished "for i in \$T; do remove_object \$i; done" &&
+ T=$(
+ GIT_INDEX_FILE=test-index &&
+ export GIT_INDEX_FILE &&
+ rm -f test-index &&
+ >x &&
+ git add x &&
+ git rev-parse :x &&
+ T=$(git write-tree) &&
+ echo $T &&
+ (
+ git cat-file tree $T &&
+ git cat-file tree $T
+ ) |
+ git hash-object --literally -w -t tree --stdin
+ ) &&
+ test_must_fail git fsck 2>out &&
+ test_grep "error in tree .*contains duplicate file entries" out
+'
+
+check_duplicate_names () {
+ expect=$1 &&
+ shift &&
+ names=$@ &&
+ test_expect_$expect "tree object with duplicate names: $names" '
+ test_when_finished "remove_object \$blob" &&
+ test_when_finished "remove_object \$tree" &&
+ test_when_finished "remove_object \$badtree" &&
+ blob=$(echo blob | git hash-object -w --stdin) &&
+ printf "100644 blob %s\t%s\n" $blob x.2 >tree &&
+ tree=$(git mktree <tree) &&
+ for name in $names
+ do
+ case "$name" in
+ */) printf "040000 tree %s\t%s\n" $tree "${name%/}" ;;
+ *) printf "100644 blob %s\t%s\n" $blob "$name" ;;
+ esac
+ done >badtree &&
+ badtree=$(git mktree <badtree) &&
+ test_must_fail git fsck 2>out &&
+ test_grep "$badtree" out &&
+ test_grep "error in tree .*contains duplicate file entries" out
+ '
+}
+
+check_duplicate_names success x x.1 x/
+check_duplicate_names success x x.1.2 x.1/ x/
+check_duplicate_names success x x.1 x.1.2 x/
+
+test_expect_success 'unparseable tree object' '
+ test_oid_cache <<-\EOF &&
+ junk sha1:twenty-bytes-of-junk
+ junk sha256:twenty-bytes-of-junk-twelve-more
+ EOF
+
+ test_when_finished "git update-ref -d refs/heads/wrong" &&
+ test_when_finished "remove_object \$tree_sha1" &&
+ test_when_finished "remove_object \$commit_sha1" &&
+ junk=$(test_oid junk) &&
+ tree_sha1=$(printf "100644 \0$junk" | git hash-object -t tree --stdin -w --literally) &&
+ commit_sha1=$(git commit-tree $tree_sha1) &&
+ git update-ref refs/heads/wrong $commit_sha1 &&
+ test_must_fail git fsck 2>out &&
+ test_grep "error: empty filename in tree entry" out &&
+ test_grep "$tree_sha1" out &&
+ test_grep ! "fatal: empty filename in tree entry" out
+'
+
+test_expect_success 'tree entry with type mismatch' '
+ test_when_finished "remove_object \$blob" &&
+ test_when_finished "remove_object \$tree" &&
+ test_when_finished "remove_object \$commit" &&
+ test_when_finished "git update-ref -d refs/heads/type_mismatch" &&
+ blob=$(echo blob | git hash-object -w --stdin) &&
+ blob_bin=$(echo $blob | hex2oct) &&
+ tree=$(
+ printf "40000 dir\0${blob_bin}100644 file\0${blob_bin}" |
+ git hash-object -t tree --stdin -w --literally
+ ) &&
+ commit=$(git commit-tree $tree) &&
+ git update-ref refs/heads/type_mismatch $commit &&
+ test_must_fail git fsck >out 2>&1 &&
+ test_grep "is a blob, not a tree" out &&
+ test_grep ! "dangling blob" out
+'
+
+test_expect_success 'tree entry with bogus mode' '
+ test_when_finished "remove_object \$blob" &&
+ test_when_finished "remove_object \$tree" &&
+ blob=$(echo blob | git hash-object -w --stdin) &&
+ blob_oct=$(echo $blob | hex2oct) &&
+ tree=$(printf "100000 foo\0${blob_oct}" |
+ git hash-object -t tree --stdin -w --literally) &&
+ git fsck 2>err &&
+ cat >expect <<-EOF &&
+ warning in tree $tree: badFilemode: contains bad file modes
+ EOF
+ test_cmp expect err
+'
+
+test_expect_success 'tag pointing to nonexistent' '
+ badoid=$(test_oid deadbeef) &&
+ cat >invalid-tag <<-EOF &&
+ object $badoid
+ type commit
+ tag invalid
+ tagger T A Gger <tagger@example.com> 1234567890 -0000
+
+ This is an invalid tag.
+ EOF
+
+ tag=$(git hash-object -t tag -w --stdin <invalid-tag) &&
+ test_when_finished "remove_object $tag" &&
+ echo $tag >.git/refs/tags/invalid &&
+ test_when_finished "git update-ref -d refs/tags/invalid" &&
+ test_must_fail git fsck --tags >out &&
+ test_grep "broken link" out
+'
+
+test_expect_success 'tag pointing to something else than its type' '
+ sha=$(echo blob | git hash-object -w --stdin) &&
+ test_when_finished "remove_object $sha" &&
+ cat >wrong-tag <<-EOF &&
+ object $sha
+ type commit
+ tag wrong
+ tagger T A Gger <tagger@example.com> 1234567890 -0000
+
+ This is an invalid tag.
+ EOF
+
+ tag=$(git hash-object -t tag -w --stdin <wrong-tag) &&
+ test_when_finished "remove_object $tag" &&
+ echo $tag >.git/refs/tags/wrong &&
+ test_when_finished "git update-ref -d refs/tags/wrong" &&
+ test_must_fail git fsck --tags
+'
+
+test_expect_success 'tag with incorrect tag name & missing tagger' '
+ sha=$(git rev-parse HEAD) &&
+ cat >wrong-tag <<-EOF &&
+ object $sha
+ type commit
+ tag wrong name format
+
+ This is an invalid tag.
+ EOF
+
+ tag=$(git hash-object --literally -t tag -w --stdin <wrong-tag) &&
+ test_when_finished "remove_object $tag" &&
+ echo $tag >.git/refs/tags/wrong &&
+ test_when_finished "git update-ref -d refs/tags/wrong" &&
+ git fsck --tags 2>out &&
+
+ cat >expect <<-EOF &&
+ warning in tag $tag: badTagName: invalid '\''tag'\'' name: wrong name format
+ warning in tag $tag: missingTaggerEntry: invalid format - expected '\''tagger'\'' line
+ EOF
+ test_cmp expect out
+'
+
+test_expect_success 'tag with bad tagger' '
+ sha=$(git rev-parse HEAD) &&
+ cat >wrong-tag <<-EOF &&
+ object $sha
+ type commit
+ tag not-quite-wrong
+ tagger Bad Tagger Name
+
+ This is an invalid tag.
+ EOF
+
+ tag=$(git hash-object --literally -t tag -w --stdin <wrong-tag) &&
+ test_when_finished "remove_object $tag" &&
+ echo $tag >.git/refs/tags/wrong &&
+ test_when_finished "git update-ref -d refs/tags/wrong" &&
+ test_must_fail git fsck --tags 2>out &&
+ test_grep "error in tag .*: invalid author/committer" out
+'
+
+test_expect_success 'tag with NUL in header' '
+ sha=$(git rev-parse HEAD) &&
+ q_to_nul >tag-NUL-header <<-EOF &&
+ object $sha
+ type commit
+ tag contains-Q-in-header
+ tagger T A Gger <tagger@example.com> 1234567890 -0000
+
+ This is an invalid tag.
+ EOF
+
+ tag=$(git hash-object --literally -t tag -w --stdin <tag-NUL-header) &&
+ test_when_finished "remove_object $tag" &&
+ echo $tag >.git/refs/tags/wrong &&
+ test_when_finished "git update-ref -d refs/tags/wrong" &&
+ test_must_fail git fsck --tags 2>out &&
+ test_grep "error in tag $tag.*unterminated header: NUL at offset" out
+'
+
+test_expect_success 'cleaned up' '
+ git fsck >actual 2>&1 &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'rev-list --verify-objects' '
+ git rev-list --verify-objects --all >/dev/null 2>out &&
+ test_must_be_empty out
+'
+
+test_expect_success 'rev-list --verify-objects with bad sha1' '
+ sha=$(echo blob | git hash-object -w --stdin) &&
+ old=$(test_oid_to_path $sha) &&
+ new=$(dirname $old)/$(test_oid ff_2) &&
+ sha="$(dirname $new)$(basename $new)" &&
+ mv .git/objects/$old .git/objects/$new &&
+ test_when_finished "remove_object $sha" &&
+ git update-index --add --cacheinfo 100644 $sha foo &&
+ test_when_finished "git read-tree -u --reset HEAD" &&
+ tree=$(git write-tree) &&
+ test_when_finished "remove_object $tree" &&
+ cmt=$(echo bogus | git commit-tree $tree) &&
+ test_when_finished "remove_object $cmt" &&
+ git update-ref refs/heads/bogus $cmt &&
+ test_when_finished "git update-ref -d refs/heads/bogus" &&
+
+ test_might_fail git rev-list --verify-objects refs/heads/bogus >/dev/null 2>out &&
+ test_grep -q "error: hash mismatch $(dirname $new)$(test_oid ff_2)" out
+'
+
+# An actual bit corruption is more likely than swapped commits, but
+# this provides an easy way to have commits which don't match their purported
+# hashes, but which aren't so broken we can't read them at all.
+test_expect_success 'rev-list --verify-objects notices swapped commits' '
+ git init swapped-commits &&
+ (
+ cd swapped-commits &&
+ test_commit one &&
+ test_commit two &&
+ one_oid=$(git rev-parse HEAD) &&
+ two_oid=$(git rev-parse HEAD^) &&
+ one=.git/objects/$(test_oid_to_path $one_oid) &&
+ two=.git/objects/$(test_oid_to_path $two_oid) &&
+ mv $one tmp &&
+ mv $two $one &&
+ mv tmp $two &&
+ test_must_fail git rev-list --verify-objects HEAD
+ )
+'
+
+test_expect_success 'set up repository with commit-graph' '
+ git init corrupt-graph &&
+ (
+ cd corrupt-graph &&
+ test_commit one &&
+ test_commit two &&
+ git commit-graph write --reachable
+ )
+'
+
+corrupt_graph_obj () {
+ oid=$(git -C corrupt-graph rev-parse "$1") &&
+ obj=corrupt-graph/.git/objects/$(test_oid_to_path $oid) &&
+ test_when_finished 'mv backup $obj' &&
+ mv $obj backup &&
+ echo garbage >$obj
+}
+
+test_expect_success 'rev-list --verify-objects with commit graph (tip)' '
+ corrupt_graph_obj HEAD &&
+ test_must_fail git -C corrupt-graph rev-list --verify-objects HEAD
+'
+
+test_expect_success 'rev-list --verify-objects with commit graph (parent)' '
+ corrupt_graph_obj HEAD^ &&
+ test_must_fail git -C corrupt-graph rev-list --verify-objects HEAD
+'
+
+test_expect_success 'force fsck to ignore double author' '
+ git cat-file commit HEAD >basis &&
+ sed "s/^author .*/&,&/" <basis | tr , \\n >multiple-authors &&
+ new=$(git hash-object --literally -t commit -w --stdin <multiple-authors) &&
+ test_when_finished "remove_object $new" &&
+ git update-ref refs/heads/bogus "$new" &&
+ test_when_finished "git update-ref -d refs/heads/bogus" &&
+ test_must_fail git fsck &&
+ git -c fsck.multipleAuthors=ignore fsck
+'
+
+_bz='\0'
+_bzoid=$(printf $ZERO_OID | sed -e 's/00/\\0/g')
+
+test_expect_success 'fsck notices blob entry pointing to null sha1' '
+ (git init null-blob &&
+ cd null-blob &&
+ sha=$(printf "100644 file$_bz$_bzoid" |
+ git hash-object --literally -w --stdin -t tree) &&
+ git fsck 2>out &&
+ test_grep "warning.*null sha1" out
+ )
+'
+
+test_expect_success 'fsck notices submodule entry pointing to null sha1' '
+ (git init null-commit &&
+ cd null-commit &&
+ sha=$(printf "160000 submodule$_bz$_bzoid" |
+ git hash-object --literally -w --stdin -t tree) &&
+ git fsck 2>out &&
+ test_grep "warning.*null sha1" out
+ )
+'
+
+test_expect_success 'fsck notices excessively large tree entry name' '
+ git init large-name &&
+ (
+ cd large-name &&
+ test_commit a-long-name &&
+ git -c fsck.largePathname=warn:10 fsck 2>out &&
+ grep "warning.*large pathname" out
+ )
+'
+
+while read name path pretty; do
+ while read mode type; do
+ : ${pretty:=$path}
+ test_expect_success "fsck notices $pretty as $type" '
+ (
+ git init $name-$type &&
+ cd $name-$type &&
+ git config core.protectNTFS false &&
+ echo content >file &&
+ git add file &&
+ git commit -m base &&
+ blob=$(git rev-parse :file) &&
+ tree=$(git rev-parse HEAD^{tree}) &&
+ value=$(eval "echo \$$type") &&
+ printf "$mode $type %s\t%s" "$value" "$path" >bad &&
+ bad_tree=$(git mktree <bad) &&
+ git fsck 2>out &&
+ test_grep "warning.*tree $bad_tree" out
+ )'
+ done <<-\EOF
+ 100644 blob
+ 040000 tree
+ EOF
+done <<-EOF
+dot .
+dotdot ..
+dotgit .git
+dotgit-case .GIT
+dotgit-unicode .gI${u200c}T .gI{u200c}T
+dotgit-case2 .Git
+git-tilde1 git~1
+dotgitdot .git.
+dot-backslash-case .\\\\.GIT\\\\foobar
+dotgit-case-backslash .git\\\\foobar
+EOF
+
+test_expect_success 'fsck allows .Ňit' '
+ (
+ git init not-dotgit &&
+ cd not-dotgit &&
+ echo content >file &&
+ git add file &&
+ git commit -m base &&
+ blob=$(git rev-parse :file) &&
+ printf "100644 blob $blob\t.\\305\\207it" >tree &&
+ tree=$(git mktree <tree) &&
+ git fsck 2>err &&
+ test_line_count = 0 err
+ )
+'
+
+test_expect_success 'NUL in commit' '
+ rm -fr nul-in-commit &&
+ git init nul-in-commit &&
+ (
+ cd nul-in-commit &&
+ git commit --allow-empty -m "initial commitQNUL after message" &&
+ git cat-file commit HEAD >original &&
+ q_to_nul <original >munged &&
+ git hash-object --literally -w -t commit --stdin <munged >name &&
+ git branch bad $(cat name) &&
+
+ test_must_fail git -c fsck.nulInCommit=error fsck 2>warn.1 &&
+ test_grep nulInCommit warn.1 &&
+ git fsck 2>warn.2 &&
+ test_grep nulInCommit warn.2
+ )
+'
+
+# create a static test repo which is broken by omitting
+# one particular object ($1, which is looked up via rev-parse
+# in the new repository).
+create_repo_missing () {
+ rm -rf missing &&
+ git init missing &&
+ (
+ cd missing &&
+ git commit -m one --allow-empty &&
+ mkdir subdir &&
+ echo content >subdir/file &&
+ git add subdir/file &&
+ git commit -m two &&
+ unrelated=$(echo unrelated | git hash-object --stdin -w) &&
+ git tag -m foo tag $unrelated &&
+ sha1=$(git rev-parse --verify "$1") &&
+ path=$(echo $sha1 | sed 's|..|&/|') &&
+ rm .git/objects/$path
+ )
+}
+
+test_expect_success 'fsck notices missing blob' '
+ create_repo_missing HEAD:subdir/file &&
+ test_must_fail git -C missing fsck
+'
+
+test_expect_success 'fsck notices missing subtree' '
+ create_repo_missing HEAD:subdir &&
+ test_must_fail git -C missing fsck
+'
+
+test_expect_success 'fsck notices missing root tree' '
+ create_repo_missing HEAD^{tree} &&
+ test_must_fail git -C missing fsck
+'
+
+test_expect_success 'fsck notices missing parent' '
+ create_repo_missing HEAD^ &&
+ test_must_fail git -C missing fsck
+'
+
+test_expect_success 'fsck notices missing tagged object' '
+ create_repo_missing tag^{blob} &&
+ test_must_fail git -C missing fsck
+'
+
+test_expect_success 'fsck notices ref pointing to missing commit' '
+ create_repo_missing HEAD &&
+ test_must_fail git -C missing fsck
+'
+
+test_expect_success 'fsck notices ref pointing to missing tag' '
+ create_repo_missing tag &&
+ test_must_fail git -C missing fsck
+'
+
+test_expect_success 'fsck --connectivity-only' '
+ rm -rf connectivity-only &&
+ git init connectivity-only &&
+ (
+ cd connectivity-only &&
+ touch empty &&
+ git add empty &&
+ test_commit empty &&
+
+ # Drop the index now; we want to be sure that we
+ # recursively notice the broken objects
+ # because they are reachable from refs, not because
+ # they are in the index.
+ rm -f .git/index &&
+
+ # corrupt the blob, but in a way that we can still identify
+ # its type. That lets us see that --connectivity-only is
+ # not actually looking at the contents, but leaves it
+ # free to examine the type if it chooses.
+ empty=.git/objects/$(test_oid_to_path $EMPTY_BLOB) &&
+ blob=$(echo unrelated | git hash-object -w --stdin) &&
+ mv -f $(sha1_file $blob) $empty &&
+
+ test_must_fail git fsck --strict &&
+ git fsck --strict --connectivity-only &&
+ tree=$(git rev-parse HEAD:) &&
+ suffix=${tree#??} &&
+ tree=.git/objects/${tree%$suffix}/$suffix &&
+ rm -f $tree &&
+ echo invalid >$tree &&
+ test_must_fail git fsck --strict --connectivity-only
+ )
+'
+
+test_expect_success 'fsck --connectivity-only with explicit head' '
+ rm -rf connectivity-only &&
+ git init connectivity-only &&
+ (
+ cd connectivity-only &&
+ test_commit foo &&
+ rm -f .git/index &&
+ tree=$(git rev-parse HEAD^{tree}) &&
+ remove_object $(git rev-parse HEAD:foo.t) &&
+ test_must_fail git fsck --connectivity-only $tree
+ )
+'
+
+test_expect_success 'fsck --name-objects' '
+ rm -rf name-objects &&
+ git init name-objects &&
+ (
+ cd name-objects &&
+ git config core.logAllRefUpdates false &&
+ test_commit julius caesar.t &&
+ test_commit augustus44 &&
+ test_commit caesar &&
+ remove_object $(git rev-parse julius:caesar.t) &&
+ tree=$(git rev-parse --verify julius:) &&
+ git tag -d julius &&
+ test_must_fail git fsck --name-objects >out &&
+ test_grep "$tree (refs/tags/augustus44\\^:" out
+ )
+'
+
+test_expect_success 'alternate objects are correctly blamed' '
+ test_when_finished "rm -rf alt.git .git/objects/info/alternates" &&
+ name=$(test_oid numeric) &&
+ path=$(test_oid_to_path "$name") &&
+ git init --bare alt.git &&
+ echo "../../alt.git/objects" >.git/objects/info/alternates &&
+ mkdir alt.git/objects/$(dirname $path) &&
+ >alt.git/objects/$(dirname $path)/$(basename $path) &&
+ test_must_fail git fsck >out 2>&1 &&
+ test_grep alt.git out
+'
+
+test_expect_success 'fsck errors in packed objects' '
+ git cat-file commit HEAD >basis &&
+ sed "s/</one/" basis >one &&
+ sed "s/</foo/" basis >two &&
+ one=$(git hash-object --literally -t commit -w one) &&
+ two=$(git hash-object --literally -t commit -w two) &&
+ pack=$(
+ {
+ echo $one &&
+ echo $two
+ } | git pack-objects .git/objects/pack/pack
+ ) &&
+ test_when_finished "rm -f .git/objects/pack/pack-$pack.*" &&
+ remove_object $one &&
+ remove_object $two &&
+ test_must_fail git fsck 2>out &&
+ test_grep "error in commit $one.* - bad name" out &&
+ test_grep "error in commit $two.* - bad name" out &&
+ ! grep corrupt out
+'
+
+test_expect_success 'fsck fails on corrupt packfile' '
+ hsh=$(git commit-tree -m mycommit HEAD^{tree}) &&
+ pack=$(echo $hsh | git pack-objects .git/objects/pack/pack) &&
+
+ # Corrupt the first byte of the first object. (It contains 3 type bits,
+ # at least one of which is not zero, so setting the first byte to 0 is
+ # sufficient.)
+ chmod a+w .git/objects/pack/pack-$pack.pack &&
+ printf "\0" | dd of=.git/objects/pack/pack-$pack.pack bs=1 conv=notrunc seek=12 &&
+
+ test_when_finished "rm -f .git/objects/pack/pack-$pack.*" &&
+ remove_object $hsh &&
+ test_must_fail git fsck 2>out &&
+ test_grep "checksum mismatch" out
+'
+
+test_expect_success 'fsck finds problems in duplicate loose objects' '
+ rm -rf broken-duplicate &&
+ git init broken-duplicate &&
+ (
+ cd broken-duplicate &&
+ test_commit duplicate &&
+ # no "-d" here, so we end up with duplicates
+ git repack &&
+ # now corrupt the loose copy
+ oid="$(git rev-parse HEAD)" &&
+ file=$(sha1_file "$oid") &&
+ rm "$file" &&
+ echo broken >"$file" &&
+ test_must_fail git fsck 2>err &&
+
+ cat >expect <<-EOF &&
+ error: inflate: data stream error (incorrect header check)
+ error: unable to unpack header of $file
+ error: $oid: object corrupt or missing: $file
+ EOF
+ grep "^error: " err >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'fsck detects trailing loose garbage (commit)' '
+ git cat-file commit HEAD >basis &&
+ echo bump-commit-sha1 >>basis &&
+ commit=$(git hash-object -w -t commit basis) &&
+ file=$(sha1_file $commit) &&
+ test_when_finished "remove_object $commit" &&
+ chmod +w "$file" &&
+ echo garbage >>"$file" &&
+ test_must_fail git fsck 2>out &&
+ test_grep "garbage.*$commit" out
+'
+
+test_expect_success 'fsck detects trailing loose garbage (large blob)' '
+ blob=$(echo trailing | git hash-object -w --stdin) &&
+ file=$(sha1_file $blob) &&
+ test_when_finished "remove_object $blob" &&
+ chmod +w "$file" &&
+ echo garbage >>"$file" &&
+ test_must_fail git -c core.bigfilethreshold=5 fsck 2>out &&
+ test_grep "garbage.*$blob" out
+'
+
+test_expect_success 'fsck detects truncated loose object' '
+ # make it big enough that we know we will truncate in the data
+ # portion, not the header
+ test-tool genrandom truncate 4096 >file &&
+ blob=$(git hash-object -w file) &&
+ file=$(sha1_file $blob) &&
+ test_when_finished "remove_object $blob" &&
+ test_copy_bytes 1024 <"$file" >tmp &&
+ rm "$file" &&
+ mv -f tmp "$file" &&
+
+ # check both regular and streaming code paths
+ test_must_fail git fsck 2>out &&
+ test_grep corrupt.*$blob out &&
+
+ test_must_fail git -c core.bigfilethreshold=128 fsck 2>out &&
+ test_grep corrupt.*$blob out
+'
+
+# for each of type, we have one version which is referenced by another object
+# (and so while unreachable, not dangling), and another variant which really is
+# dangling.
+test_expect_success 'create dangling-object repository' '
+ git init dangling &&
+ (
+ cd dangling &&
+ blob=$(echo not-dangling | git hash-object -w --stdin) &&
+ dblob=$(echo dangling | git hash-object -w --stdin) &&
+ tree=$(printf "100644 blob %s\t%s\n" $blob one | git mktree) &&
+ dtree=$(printf "100644 blob %s\t%s\n" $blob two | git mktree) &&
+ commit=$(git commit-tree $tree) &&
+ dcommit=$(git commit-tree -p $commit $tree) &&
+
+ cat >expect <<-EOF
+ dangling blob $dblob
+ dangling commit $dcommit
+ dangling tree $dtree
+ EOF
+ )
+'
+
+test_expect_success 'fsck notices dangling objects' '
+ (
+ cd dangling &&
+ git fsck >actual &&
+ # the output order is non-deterministic, as it comes from a hash
+ sort <actual >actual.sorted &&
+ test_cmp expect actual.sorted
+ )
+'
+
+test_expect_success 'fsck --connectivity-only notices dangling objects' '
+ (
+ cd dangling &&
+ git fsck --connectivity-only >actual &&
+ # the output order is non-deterministic, as it comes from a hash
+ sort <actual >actual.sorted &&
+ test_cmp expect actual.sorted
+ )
+'
+
+test_expect_success 'fsck $name notices bogus $name' '
+ test_must_fail git fsck bogus &&
+ test_must_fail git fsck $ZERO_OID
+'
+
+test_expect_success 'bogus head does not fallback to all heads' '
+ # set up a case that will cause a reachability complaint
+ echo to-be-deleted >foo &&
+ git add foo &&
+ blob=$(git rev-parse :foo) &&
+ test_when_finished "git rm --cached foo" &&
+ remove_object $blob &&
+ test_must_fail git fsck $ZERO_OID >out 2>&1 &&
+ ! grep $blob out
+'
+
+# Corrupt the checksum on the index.
+# Add 1 to the last byte in the SHA.
+corrupt_index_checksum () {
+ perl -w -e '
+ use Fcntl ":seek";
+ open my $fh, "+<", ".git/index" or die "open: $!";
+ binmode $fh;
+ seek $fh, -1, SEEK_END or die "seek: $!";
+ read $fh, my $in_byte, 1 or die "read: $!";
+
+ $in_value = unpack("C", $in_byte);
+ $out_value = ($in_value + 1) & 255;
+
+ $out_byte = pack("C", $out_value);
+
+ seek $fh, -1, SEEK_END or die "seek: $!";
+ print $fh $out_byte;
+ close $fh or die "close: $!";
+ '
+}
+
+# Corrupt the checksum on the index and then
+# verify that only fsck notices.
+test_expect_success 'detect corrupt index file in fsck' '
+ cp .git/index .git/index.backup &&
+ test_when_finished "mv .git/index.backup .git/index" &&
+ corrupt_index_checksum &&
+ test_must_fail git fsck --cache 2>errors &&
+ test_grep "bad index file" errors
+'
+
+test_expect_success 'fsck error and recovery on invalid object type' '
+ git init --bare garbage-type &&
+ (
+ cd garbage-type &&
+
+ garbage_blob=$(git hash-object --stdin -w -t garbage --literally </dev/null) &&
+
+ test_must_fail git fsck 2>err &&
+ grep -e "^error" -e "^fatal" err >errors &&
+ test_line_count = 1 errors &&
+ grep "$garbage_blob: object is of unknown type '"'"'garbage'"'"':" err
+ )
+'
+
+test_expect_success 'fsck error on gitattributes with excessive line lengths' '
+ blob=$(printf "pattern %02048d" 1 | git hash-object -w --stdin) &&
+ test_when_finished "remove_object $blob" &&
+ tree=$(printf "100644 blob %s\t%s\n" $blob .gitattributes | git mktree) &&
+ test_when_finished "remove_object $tree" &&
+ cat >expected <<-EOF &&
+ error in blob $blob: gitattributesLineLength: .gitattributes has too long lines to parse
+ EOF
+ test_must_fail git fsck --no-dangling >actual 2>&1 &&
+ test_cmp expected actual
+'
+
+test_expect_success 'fsck error on gitattributes with excessive size' '
+ blob=$(test-tool genzeros $((100 * 1024 * 1024 + 1)) | git hash-object -w --stdin) &&
+ test_when_finished "remove_object $blob" &&
+ tree=$(printf "100644 blob %s\t%s\n" $blob .gitattributes | git mktree) &&
+ test_when_finished "remove_object $tree" &&
+ cat >expected <<-EOF &&
+ error in blob $blob: gitattributesLarge: .gitattributes too large to parse
+ EOF
+ test_must_fail git fsck --no-dangling >actual 2>&1 &&
+ test_cmp expected actual
+'
+
+test_expect_success 'fsck detects problems in worktree index' '
+ test_when_finished "git worktree remove -f wt" &&
+ git worktree add wt &&
+
+ echo "this will be removed to break the worktree index" >wt/file &&
+ git -C wt add file &&
+ blob=$(git -C wt rev-parse :file) &&
+ remove_object $blob &&
+
+ test_must_fail git fsck --name-objects >actual 2>&1 &&
+ cat >expect <<-EOF &&
+ missing blob $blob (.git/worktrees/wt/index:file)
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'fsck reports problems in current worktree index without filename' '
+ test_when_finished "rm -f .git/index && git read-tree HEAD" &&
+ echo "this object will be removed to break current worktree index" >file &&
+ git add file &&
+ blob=$(git rev-parse :file) &&
+ remove_object $blob &&
+
+ test_must_fail git fsck --name-objects >actual 2>&1 &&
+ cat >expect <<-EOF &&
+ missing blob $blob (:file)
+ EOF
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t1451-fsck-buffer.sh b/t/t1451-fsck-buffer.sh
new file mode 100755
index 0000000..3413da4
--- /dev/null
+++ b/t/t1451-fsck-buffer.sh
@@ -0,0 +1,142 @@
+#!/bin/sh
+
+test_description='fsck on buffers without NUL termination
+
+The goal here is to make sure that the various fsck parsers never look
+past the end of the buffer they are given, even when encountering broken
+or truncated objects.
+
+We have to use "hash-object" for this because most code paths that read objects
+append an extra NUL for safety after the buffer. But hash-object, since it is
+reading straight from a file (and possibly even mmap-ing it) cannot always do
+so.
+
+These tests _might_ catch such overruns in normal use, but should be run with
+ASan or valgrind for more confidence.
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# the general idea for tags and commits is to build up the "base" file
+# progressively, and then test new truncations on top of it.
+reset () {
+ test_expect_success 'reset input to empty' '
+ >base
+ '
+}
+
+add () {
+ content="$1"
+ type=${content%% *}
+ test_expect_success "add $type line" '
+ echo "$content" >>base
+ '
+}
+
+check () {
+ type=$1
+ fsck=$2
+ content=$3
+ test_expect_success "truncated $type ($fsck, \"$content\")" '
+ # do not pipe into hash-object here; we want to increase
+ # the chance that it uses a fixed-size buffer or mmap,
+ # and a pipe would be read into a strbuf.
+ {
+ cat base &&
+ echo "$content"
+ } >input &&
+ test_must_fail git hash-object -t "$type" input 2>err &&
+ grep "$fsck" err
+ '
+}
+
+test_expect_success 'create valid objects' '
+ git commit --allow-empty -m foo &&
+ commit=$(git rev-parse --verify HEAD) &&
+ tree=$(git rev-parse --verify HEAD^{tree})
+'
+
+reset
+check commit missingTree ""
+check commit missingTree "tr"
+check commit missingTree "tree"
+check commit badTreeSha1 "tree "
+check commit badTreeSha1 "tree 1234"
+add "tree $tree"
+
+# these expect missingAuthor because "parent" is optional
+check commit missingAuthor ""
+check commit missingAuthor "par"
+check commit missingAuthor "parent"
+check commit badParentSha1 "parent "
+check commit badParentSha1 "parent 1234"
+add "parent $commit"
+
+check commit missingAuthor ""
+check commit missingAuthor "au"
+check commit missingAuthor "author"
+ident_checks () {
+ check $1 missingEmail "$2 "
+ check $1 missingEmail "$2 name"
+ check $1 badEmail "$2 name <"
+ check $1 badEmail "$2 name <email"
+ check $1 missingSpaceBeforeDate "$2 name <email>"
+ check $1 badDate "$2 name <email> "
+ check $1 badDate "$2 name <email> 1234"
+ check $1 badTimezone "$2 name <email> 1234 "
+ check $1 badTimezone "$2 name <email> 1234 +"
+}
+ident_checks commit author
+add "author name <email> 1234 +0000"
+
+check commit missingCommitter ""
+check commit missingCommitter "co"
+check commit missingCommitter "committer"
+ident_checks commit committer
+add "committer name <email> 1234 +0000"
+
+reset
+check tag missingObject ""
+check tag missingObject "obj"
+check tag missingObject "object"
+check tag badObjectSha1 "object "
+check tag badObjectSha1 "object 1234"
+add "object $commit"
+
+check tag missingType ""
+check tag missingType "ty"
+check tag missingType "type"
+check tag badType "type "
+check tag badType "type com"
+add "type commit"
+
+check tag missingTagEntry ""
+check tag missingTagEntry "ta"
+check tag missingTagEntry "tag"
+check tag badTagName "tag "
+add "tag foo"
+
+check tag missingTagger ""
+check tag missingTagger "ta"
+check tag missingTagger "tagger"
+ident_checks tag tagger
+
+# trees are a binary format and can't use our earlier helpers
+test_expect_success 'truncated tree (short hash)' '
+ printf "100644 foo\0\1\1\1\1" >input &&
+ test_must_fail git hash-object -t tree input 2>err &&
+ grep badTree err
+'
+
+test_expect_success 'truncated tree (missing nul)' '
+ # these two things are indistinguishable to the parser. The important
+ # thing about this is example is that there are enough bytes to
+ # make up a hash, and that there is no NUL (and we confirm that the
+ # parser does not walk past the end of the buffer).
+ printf "100644 a long filename, or a hash with missing nul?" >input &&
+ test_must_fail git hash-object -t tree input 2>err &&
+ grep badTree err
+'
+
+test_done
diff --git a/t/t1500-rev-parse.sh b/t/t1500-rev-parse.sh
new file mode 100755
index 0000000..3f9e7f6
--- /dev/null
+++ b/t/t1500-rev-parse.sh
@@ -0,0 +1,290 @@
+#!/bin/sh
+
+test_description='test git rev-parse'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_one () {
+ dir="$1" &&
+ expect="$2" &&
+ shift &&
+ shift &&
+ echo "$expect" >expect &&
+ git -C "$dir" rev-parse "$@" >actual &&
+ test_cmp expect actual
+}
+
+# usage: [options] label is-bare is-inside-git is-inside-work prefix git-dir absolute-git-dir
+test_rev_parse () {
+ d=
+ bare=
+ gitdir=
+ while :
+ do
+ case "$1" in
+ -C) d="$2"; shift; shift ;;
+ -b) case "$2" in
+ [tfu]*) bare="$2"; shift; shift ;;
+ *) error "test_rev_parse: bogus core.bare value '$2'" ;;
+ esac ;;
+ -g) gitdir="$2"; shift; shift ;;
+ -*) error "test_rev_parse: unrecognized option '$1'" ;;
+ *) break ;;
+ esac
+ done
+
+ name=$1
+ shift
+
+ for o in --is-bare-repository \
+ --is-inside-git-dir \
+ --is-inside-work-tree \
+ --show-prefix \
+ --git-dir \
+ --absolute-git-dir
+ do
+ test $# -eq 0 && break
+ expect="$1"
+ test_expect_success "$name: $o" '
+ if test -n "$gitdir"
+ then
+ test_when_finished "unset GIT_DIR" &&
+ GIT_DIR="$gitdir" &&
+ export GIT_DIR
+ fi &&
+
+ case "$bare" in
+ t*) test_config ${d:+-C} ${d:+"$d"} core.bare true ;;
+ f*) test_config ${d:+-C} ${d:+"$d"} core.bare false ;;
+ u*) test_unconfig ${d:+-C} ${d:+"$d"} core.bare ;;
+ esac &&
+
+ echo "$expect" >expect &&
+ git ${d:+-C} ${d:+"$d"} rev-parse $o >actual &&
+ test_cmp expect actual
+ '
+ shift
+ done
+}
+
+ROOT=$(pwd)
+
+test_expect_success 'setup' '
+ mkdir -p sub/dir work &&
+ cp -R .git repo.git &&
+ git checkout -B main &&
+ test_commit abc &&
+ git checkout -b side &&
+ test_commit def &&
+ git checkout main &&
+ git worktree add worktree side
+'
+
+test_rev_parse toplevel false false true '' .git "$ROOT/.git"
+
+test_rev_parse -C .git .git/ false true false '' . "$ROOT/.git"
+test_rev_parse -C .git/objects .git/objects/ false true false '' "$ROOT/.git" "$ROOT/.git"
+
+test_rev_parse -C sub/dir subdirectory false false true sub/dir/ "$ROOT/.git" "$ROOT/.git"
+
+test_rev_parse -b t 'core.bare = true' true false false
+
+test_rev_parse -b u 'core.bare undefined' false false true
+
+
+test_rev_parse -C work -g ../.git -b f 'GIT_DIR=../.git, core.bare = false' false false true '' "../.git" "$ROOT/.git"
+
+test_rev_parse -C work -g ../.git -b t 'GIT_DIR=../.git, core.bare = true' true false false ''
+
+test_rev_parse -C work -g ../.git -b u 'GIT_DIR=../.git, core.bare undefined' false false true ''
+
+
+test_rev_parse -C work -g ../repo.git -b f 'GIT_DIR=../repo.git, core.bare = false' false false true '' "../repo.git" "$ROOT/repo.git"
+
+test_rev_parse -C work -g ../repo.git -b t 'GIT_DIR=../repo.git, core.bare = true' true false false ''
+
+test_rev_parse -C work -g ../repo.git -b u 'GIT_DIR=../repo.git, core.bare undefined' false false true ''
+
+test_expect_success 'rev-parse --path-format=absolute' '
+ test_one "." "$ROOT/.git" --path-format=absolute --git-dir &&
+ test_one "." "$ROOT/.git" --path-format=absolute --git-common-dir &&
+ test_one "sub/dir" "$ROOT/.git" --path-format=absolute --git-dir &&
+ test_one "sub/dir" "$ROOT/.git" --path-format=absolute --git-common-dir &&
+ test_one "worktree" "$ROOT/.git/worktrees/worktree" --path-format=absolute --git-dir &&
+ test_one "worktree" "$ROOT/.git" --path-format=absolute --git-common-dir &&
+ test_one "." "$ROOT" --path-format=absolute --show-toplevel &&
+ test_one "." "$ROOT/.git/objects" --path-format=absolute --git-path objects &&
+ test_one "." "$ROOT/.git/objects/foo/bar/baz" --path-format=absolute --git-path objects/foo/bar/baz
+'
+
+test_expect_success 'rev-parse --path-format=relative' '
+ test_one "." ".git" --path-format=relative --git-dir &&
+ test_one "." ".git" --path-format=relative --git-common-dir &&
+ test_one "sub/dir" "../../.git" --path-format=relative --git-dir &&
+ test_one "sub/dir" "../../.git" --path-format=relative --git-common-dir &&
+ test_one "worktree" "../.git/worktrees/worktree" --path-format=relative --git-dir &&
+ test_one "worktree" "../.git" --path-format=relative --git-common-dir &&
+ test_one "." "./" --path-format=relative --show-toplevel &&
+ test_one "." ".git/objects" --path-format=relative --git-path objects &&
+ test_one "." ".git/objects/foo/bar/baz" --path-format=relative --git-path objects/foo/bar/baz
+'
+
+test_expect_success '--path-format=relative does not affect --absolute-git-dir' '
+ git rev-parse --path-format=relative --absolute-git-dir >actual &&
+ echo "$ROOT/.git" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success '--path-format can change in the middle of the command line' '
+ git rev-parse --path-format=absolute --git-dir --path-format=relative --git-path objects/foo/bar >actual &&
+ cat >expect <<-EOF &&
+ $ROOT/.git
+ .git/objects/foo/bar
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success '--path-format does not segfault without an argument' '
+ test_must_fail git rev-parse --path-format
+'
+
+test_expect_success 'git-common-dir from worktree root' '
+ echo .git >expect &&
+ git rev-parse --git-common-dir >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git-common-dir inside sub-dir' '
+ mkdir -p path/to/child &&
+ test_when_finished "rm -rf path" &&
+ echo "$(git -C path/to/child rev-parse --show-cdup).git" >expect &&
+ git -C path/to/child rev-parse --git-common-dir >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git-path from worktree root' '
+ echo .git/objects >expect &&
+ git rev-parse --git-path objects >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git-path inside sub-dir' '
+ mkdir -p path/to/child &&
+ test_when_finished "rm -rf path" &&
+ echo "$(git -C path/to/child rev-parse --show-cdup).git/objects" >expect &&
+ git -C path/to/child rev-parse --git-path objects >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rev-parse --is-shallow-repository in shallow repo' '
+ test_commit test_commit &&
+ echo true >expect &&
+ git clone --depth 1 --no-local . shallow &&
+ test_when_finished "rm -rf shallow" &&
+ git -C shallow rev-parse --is-shallow-repository >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rev-parse --is-shallow-repository in non-shallow repo' '
+ echo false >expect &&
+ git rev-parse --is-shallow-repository >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rev-parse --show-object-format in repo' '
+ test_oid algo >expect &&
+ git rev-parse --show-object-format >actual &&
+ test_cmp expect actual &&
+ git rev-parse --show-object-format=storage >actual &&
+ test_cmp expect actual &&
+ git rev-parse --show-object-format=input >actual &&
+ test_cmp expect actual &&
+ git rev-parse --show-object-format=output >actual &&
+ test_cmp expect actual &&
+ test_must_fail git rev-parse --show-object-format=squeamish-ossifrage 2>err &&
+ grep "unknown mode for --show-object-format: squeamish-ossifrage" err
+'
+
+test_expect_success '--show-toplevel from subdir of working tree' '
+ pwd >expect &&
+ git -C sub/dir rev-parse --show-toplevel >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--show-toplevel from inside .git' '
+ test_must_fail git -C .git rev-parse --show-toplevel
+'
+
+test_expect_success 'showing the superproject correctly' '
+ git rev-parse --show-superproject-working-tree >out &&
+ test_must_be_empty out &&
+
+ test_create_repo super &&
+ test_commit -C super test_commit &&
+ test_create_repo sub &&
+ test_commit -C sub test_commit &&
+ git -c protocol.file.allow=always \
+ -C super submodule add ../sub dir/sub &&
+ echo $(pwd)/super >expect &&
+ git -C super/dir/sub rev-parse --show-superproject-working-tree >out &&
+ test_cmp expect out &&
+
+ test_commit -C super submodule_add &&
+ git -C super checkout -b branch1 &&
+ git -C super/dir/sub checkout -b branch1 &&
+ test_commit -C super/dir/sub branch1_commit &&
+ git -C super add dir/sub &&
+ test_commit -C super branch1_commit &&
+ git -C super checkout -b branch2 main &&
+ git -C super/dir/sub checkout -b branch2 main &&
+ test_commit -C super/dir/sub branch2_commit &&
+ git -C super add dir/sub &&
+ test_commit -C super branch2_commit &&
+ test_must_fail git -C super merge branch1 &&
+
+ git -C super/dir/sub rev-parse --show-superproject-working-tree >out &&
+ test_cmp expect out
+'
+
+# at least one external project depends on this behavior:
+test_expect_success 'rev-parse --since= unsqueezed ordering' '
+ x1=--since=1970-01-01T00:00:01Z &&
+ x2=--since=1970-01-01T00:00:02Z &&
+ x3=--since=1970-01-01T00:00:03Z &&
+ git rev-parse $x1 $x1 $x3 $x2 >actual &&
+ cat >expect <<-EOF &&
+ --max-age=1
+ --max-age=1
+ --max-age=3
+ --max-age=2
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'rev-parse --bisect includes bad, excludes good' '
+ test_commit_bulk 6 &&
+
+ git update-ref refs/bisect/bad-1 HEAD~1 &&
+ git update-ref refs/bisect/b HEAD~2 &&
+ git update-ref refs/bisect/bad-3 HEAD~3 &&
+ git update-ref refs/bisect/good-3 HEAD~3 &&
+ git update-ref refs/bisect/bad-4 HEAD~4 &&
+ git update-ref refs/bisect/go HEAD~4 &&
+
+ # Note: refs/bisect/b and refs/bisect/go should be ignored because they
+ # do not match the refs/bisect/bad or refs/bisect/good prefixes.
+ cat >expect <<-EOF &&
+ refs/bisect/bad-1
+ refs/bisect/bad-3
+ refs/bisect/bad-4
+ ^refs/bisect/good-3
+ EOF
+
+ git rev-parse --symbolic-full-name --bisect >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t1501-work-tree.sh b/t/t1501-work-tree.sh
new file mode 100755
index 0000000..ae6528a
--- /dev/null
+++ b/t/t1501-work-tree.sh
@@ -0,0 +1,448 @@
+#!/bin/sh
+
+test_description='test separate work tree'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ EMPTY_TREE=$(git write-tree) &&
+ EMPTY_BLOB=$(git hash-object -t blob --stdin </dev/null) &&
+ CHANGED_BLOB=$(echo changed | git hash-object -t blob --stdin) &&
+ EMPTY_BLOB7=$(echo $EMPTY_BLOB | sed "s/\(.......\).*/\1/") &&
+ CHANGED_BLOB7=$(echo $CHANGED_BLOB | sed "s/\(.......\).*/\1/") &&
+
+ mkdir -p work/sub/dir &&
+ mkdir -p work2 &&
+ mv .git repo.git
+'
+
+test_expect_success 'setup: helper for testing rev-parse' '
+ test_rev_parse() {
+ echo $1 >expected.bare &&
+ echo $2 >expected.inside-git &&
+ echo $3 >expected.inside-worktree &&
+ if test $# -ge 4
+ then
+ echo $4 >expected.prefix
+ fi &&
+
+ git rev-parse --is-bare-repository >actual.bare &&
+ git rev-parse --is-inside-git-dir >actual.inside-git &&
+ git rev-parse --is-inside-work-tree >actual.inside-worktree &&
+ if test $# -ge 4
+ then
+ git rev-parse --show-prefix >actual.prefix
+ fi &&
+
+ test_cmp expected.bare actual.bare &&
+ test_cmp expected.inside-git actual.inside-git &&
+ test_cmp expected.inside-worktree actual.inside-worktree &&
+ if test $# -ge 4
+ then
+ # rev-parse --show-prefix should output
+ # a single newline when at the top of the work tree,
+ # but we test for that separately.
+ test -z "$4" && test_must_be_empty actual.prefix ||
+ test_cmp expected.prefix actual.prefix
+ fi
+ }
+'
+
+test_expect_success 'setup: core.worktree = relative path' '
+ sane_unset GIT_WORK_TREE &&
+ GIT_DIR=repo.git &&
+ GIT_CONFIG="$(pwd)"/$GIT_DIR/config &&
+ export GIT_DIR GIT_CONFIG &&
+ git config core.worktree ../work
+'
+
+test_expect_success 'outside' '
+ test_rev_parse false false false
+'
+
+test_expect_success 'inside work tree' '
+ (
+ cd work &&
+ GIT_DIR=../repo.git &&
+ GIT_CONFIG="$(pwd)"/$GIT_DIR/config &&
+ test_rev_parse false false true ""
+ )
+'
+
+test_expect_success 'empty prefix is actually written out' '
+ echo >expected &&
+ (
+ cd work &&
+ GIT_DIR=../repo.git &&
+ GIT_CONFIG="$(pwd)"/$GIT_DIR/config &&
+ git rev-parse --show-prefix >../actual
+ ) &&
+ test_cmp expected actual
+'
+
+test_expect_success 'subdir of work tree' '
+ (
+ cd work/sub/dir &&
+ GIT_DIR=../../../repo.git &&
+ GIT_CONFIG="$(pwd)"/$GIT_DIR/config &&
+ test_rev_parse false false true sub/dir/
+ )
+'
+
+test_expect_success 'setup: core.worktree = absolute path' '
+ sane_unset GIT_WORK_TREE &&
+ GIT_DIR=$(pwd)/repo.git &&
+ GIT_CONFIG=$GIT_DIR/config &&
+ export GIT_DIR GIT_CONFIG &&
+ git config core.worktree "$(pwd)/work"
+'
+
+test_expect_success 'outside' '
+ test_rev_parse false false false &&
+ (
+ cd work2 &&
+ test_rev_parse false false false
+ )
+'
+
+test_expect_success 'inside work tree' '
+ (
+ cd work &&
+ test_rev_parse false false true ""
+ )
+'
+
+test_expect_success 'subdir of work tree' '
+ (
+ cd work/sub/dir &&
+ test_rev_parse false false true sub/dir/
+ )
+'
+
+test_expect_success 'setup: GIT_WORK_TREE=relative (override core.worktree)' '
+ GIT_DIR=$(pwd)/repo.git &&
+ GIT_CONFIG=$GIT_DIR/config &&
+ git config core.worktree non-existent &&
+ GIT_WORK_TREE=work &&
+ export GIT_DIR GIT_CONFIG GIT_WORK_TREE
+'
+
+test_expect_success 'outside' '
+ test_rev_parse false false false &&
+ (
+ cd work2 &&
+ test_rev_parse false false false
+ )
+'
+
+test_expect_success 'inside work tree' '
+ (
+ cd work &&
+ GIT_WORK_TREE=. &&
+ test_rev_parse false false true ""
+ )
+'
+
+test_expect_success 'subdir of work tree' '
+ (
+ cd work/sub/dir &&
+ GIT_WORK_TREE=../.. &&
+ test_rev_parse false false true sub/dir/
+ )
+'
+
+test_expect_success 'setup: GIT_WORK_TREE=absolute, below git dir' '
+ mv work repo.git/work &&
+ mv work2 repo.git/work2 &&
+ GIT_DIR=$(pwd)/repo.git &&
+ GIT_CONFIG=$GIT_DIR/config &&
+ GIT_WORK_TREE=$(pwd)/repo.git/work &&
+ export GIT_DIR GIT_CONFIG GIT_WORK_TREE
+'
+
+test_expect_success 'outside' '
+ echo outside &&
+ test_rev_parse false false false
+'
+
+test_expect_success 'in repo.git' '
+ (
+ cd repo.git &&
+ test_rev_parse false true false
+ ) &&
+ (
+ cd repo.git/objects &&
+ test_rev_parse false true false
+ ) &&
+ (
+ cd repo.git/work2 &&
+ test_rev_parse false true false
+ )
+'
+
+test_expect_success 'inside work tree' '
+ (
+ cd repo.git/work &&
+ test_rev_parse false true true ""
+ )
+'
+
+test_expect_success 'subdir of work tree' '
+ (
+ cd repo.git/work/sub/dir &&
+ test_rev_parse false true true sub/dir/
+ )
+'
+
+test_expect_success 'find work tree from repo' '
+ echo sub/dir/untracked >expected &&
+ cat <<-\EOF >repo.git/work/.gitignore &&
+ expected.*
+ actual.*
+ .gitignore
+ EOF
+ >repo.git/work/sub/dir/untracked &&
+ (
+ cd repo.git &&
+ git ls-files --others --exclude-standard >../actual
+ ) &&
+ test_cmp expected actual
+'
+
+test_expect_success 'find work tree from work tree' '
+ echo sub/dir/tracked >expected &&
+ >repo.git/work/sub/dir/tracked &&
+ (
+ cd repo.git/work/sub/dir &&
+ git --git-dir=../../.. add tracked
+ ) &&
+ (
+ cd repo.git &&
+ git ls-files >../actual
+ ) &&
+ test_cmp expected actual
+'
+
+test_expect_success '_gently() groks relative GIT_DIR & GIT_WORK_TREE' '
+ (
+ cd repo.git/work/sub/dir &&
+ GIT_DIR=../../.. &&
+ GIT_WORK_TREE=../.. &&
+ GIT_PAGER= &&
+ export GIT_DIR GIT_WORK_TREE GIT_PAGER &&
+
+ git diff --exit-code tracked &&
+ echo changed >tracked &&
+ test_must_fail git diff --exit-code tracked
+ )
+'
+
+test_expect_success 'diff-index respects work tree under .git dir' '
+ cat >diff-index-cached.expected <<-EOF &&
+ :000000 100644 $ZERO_OID $EMPTY_BLOB A sub/dir/tracked
+ EOF
+ cat >diff-index.expected <<-EOF &&
+ :000000 100644 $ZERO_OID $ZERO_OID A sub/dir/tracked
+ EOF
+
+ (
+ GIT_DIR=repo.git &&
+ GIT_WORK_TREE=repo.git/work &&
+ export GIT_DIR GIT_WORK_TREE &&
+ git diff-index $EMPTY_TREE >diff-index.actual &&
+ git diff-index --cached $EMPTY_TREE >diff-index-cached.actual
+ ) &&
+ test_cmp diff-index.expected diff-index.actual &&
+ test_cmp diff-index-cached.expected diff-index-cached.actual
+'
+
+test_expect_success 'diff-files respects work tree under .git dir' '
+ cat >diff-files.expected <<-EOF &&
+ :100644 100644 $EMPTY_BLOB $ZERO_OID M sub/dir/tracked
+ EOF
+
+ (
+ GIT_DIR=repo.git &&
+ GIT_WORK_TREE=repo.git/work &&
+ export GIT_DIR GIT_WORK_TREE &&
+ git diff-files >diff-files.actual
+ ) &&
+ test_cmp diff-files.expected diff-files.actual
+'
+
+test_expect_success 'git diff respects work tree under .git dir' '
+ cat >diff-TREE.expected <<-EOF &&
+ diff --git a/sub/dir/tracked b/sub/dir/tracked
+ new file mode 100644
+ index 0000000..$CHANGED_BLOB7
+ --- /dev/null
+ +++ b/sub/dir/tracked
+ @@ -0,0 +1 @@
+ +changed
+ EOF
+ cat >diff-TREE-cached.expected <<-EOF &&
+ diff --git a/sub/dir/tracked b/sub/dir/tracked
+ new file mode 100644
+ index 0000000..$EMPTY_BLOB7
+ EOF
+ cat >diff-FILES.expected <<-EOF &&
+ diff --git a/sub/dir/tracked b/sub/dir/tracked
+ index $EMPTY_BLOB7..$CHANGED_BLOB7 100644
+ --- a/sub/dir/tracked
+ +++ b/sub/dir/tracked
+ @@ -0,0 +1 @@
+ +changed
+ EOF
+
+ (
+ GIT_DIR=repo.git &&
+ GIT_WORK_TREE=repo.git/work &&
+ export GIT_DIR GIT_WORK_TREE &&
+ git diff $EMPTY_TREE >diff-TREE.actual &&
+ git diff --cached $EMPTY_TREE >diff-TREE-cached.actual &&
+ git diff >diff-FILES.actual
+ ) &&
+ test_cmp diff-TREE.expected diff-TREE.actual &&
+ test_cmp diff-TREE-cached.expected diff-TREE-cached.actual &&
+ test_cmp diff-FILES.expected diff-FILES.actual
+'
+
+test_expect_success 'git grep' '
+ echo dir/tracked >expected.grep &&
+ (
+ cd repo.git/work/sub &&
+ GIT_DIR=../.. &&
+ GIT_WORK_TREE=.. &&
+ export GIT_DIR GIT_WORK_TREE &&
+ git grep -l changed >../../../actual.grep
+ ) &&
+ test_cmp expected.grep actual.grep
+'
+
+test_expect_success 'git commit' '
+ (
+ cd repo.git &&
+ GIT_DIR=. GIT_WORK_TREE=work git commit -a -m done
+ )
+'
+
+test_expect_success 'absolute pathspec should fail gracefully' '
+ (
+ cd repo.git &&
+ test_might_fail git config --unset core.worktree &&
+ test_must_fail git log HEAD -- /home
+ )
+'
+
+test_expect_success 'make_relative_path handles double slashes in GIT_DIR' '
+ >dummy_file &&
+ echo git --git-dir="$(pwd)//repo.git" --work-tree="$(pwd)" add dummy_file &&
+ git --git-dir="$(pwd)//repo.git" --work-tree="$(pwd)" add dummy_file
+'
+
+test_expect_success 'relative $GIT_WORK_TREE and git subprocesses' '
+ GIT_DIR=repo.git GIT_WORK_TREE=repo.git/work \
+ test-tool subprocess --setup-work-tree rev-parse --show-toplevel >actual &&
+ echo "$(pwd)/repo.git/work" >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'Multi-worktree setup' '
+ mkdir work &&
+ mkdir -p repo.git/repos/foo &&
+ cp repo.git/HEAD repo.git/index repo.git/repos/foo &&
+ { cp repo.git/sharedindex.* repo.git/repos/foo || :; } &&
+ sane_unset GIT_DIR GIT_CONFIG GIT_WORK_TREE
+'
+
+test_expect_success 'GIT_DIR set (1)' '
+ echo "gitdir: repo.git/repos/foo" >gitfile &&
+ echo ../.. >repo.git/repos/foo/commondir &&
+ (
+ cd work &&
+ GIT_DIR=../gitfile git rev-parse --git-common-dir >actual &&
+ test-tool path-utils real_path "$TRASH_DIRECTORY/repo.git" >expect &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'GIT_DIR set (2)' '
+ echo "gitdir: repo.git/repos/foo" >gitfile &&
+ echo "$(pwd)/repo.git" >repo.git/repos/foo/commondir &&
+ (
+ cd work &&
+ GIT_DIR=../gitfile git rev-parse --git-common-dir >actual &&
+ test-tool path-utils real_path "$TRASH_DIRECTORY/repo.git" >expect &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'Auto discovery' '
+ echo "gitdir: repo.git/repos/foo" >.git &&
+ echo ../.. >repo.git/repos/foo/commondir &&
+ (
+ cd work &&
+ git rev-parse --git-common-dir >actual &&
+ test-tool path-utils real_path "$TRASH_DIRECTORY/repo.git" >expect &&
+ test_cmp expect actual &&
+ echo haha >data1 &&
+ git add data1 &&
+ git ls-files --full-name :/ | grep data1 >actual &&
+ echo work/data1 >expect &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success '$GIT_DIR/common overrides core.worktree' '
+ mkdir elsewhere &&
+ git --git-dir=repo.git config core.worktree "$TRASH_DIRECTORY/elsewhere" &&
+ echo "gitdir: repo.git/repos/foo" >.git &&
+ echo ../.. >repo.git/repos/foo/commondir &&
+ (
+ cd work &&
+ git rev-parse --git-common-dir >actual &&
+ test-tool path-utils real_path "$TRASH_DIRECTORY/repo.git" >expect &&
+ test_cmp expect actual &&
+ echo haha >data2 &&
+ git add data2 &&
+ git ls-files --full-name :/ | grep data2 >actual &&
+ echo work/data2 >expect &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success '$GIT_WORK_TREE overrides $GIT_DIR/common' '
+ echo "gitdir: repo.git/repos/foo" >.git &&
+ echo ../.. >repo.git/repos/foo/commondir &&
+ (
+ cd work &&
+ echo haha >data3 &&
+ git --git-dir=../.git --work-tree=. add data3 &&
+ git ls-files --full-name -- :/ | grep data3 >actual &&
+ echo data3 >expect &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'error out gracefully on invalid $GIT_WORK_TREE' '
+ (
+ GIT_WORK_TREE=/.invalid/work/tree &&
+ export GIT_WORK_TREE &&
+ test_expect_code 128 git rev-parse
+ )
+'
+
+test_expect_success 'refs work with relative gitdir and work tree' '
+ git init relative &&
+ git -C relative commit --allow-empty -m one &&
+ git -C relative commit --allow-empty -m two &&
+
+ GIT_DIR=relative/.git GIT_WORK_TREE=relative git reset HEAD^ &&
+
+ git -C relative log -1 --format=%s >actual &&
+ echo one >expect &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t1502-rev-parse-parseopt.sh b/t/t1502-rev-parse-parseopt.sh
new file mode 100755
index 0000000..f073759
--- /dev/null
+++ b/t/t1502-rev-parse-parseopt.sh
@@ -0,0 +1,325 @@
+#!/bin/sh
+
+test_description='test git rev-parse --parseopt'
+. ./test-lib.sh
+
+check_invalid_long_option () {
+ spec="$1"
+ opt="$2"
+ test_expect_success "test --parseopt invalid switch $opt help output for $spec" '
+ {
+ cat <<-\EOF &&
+ error: unknown option `'${opt#--}\''
+ EOF
+ sed -e 1d -e \$d <"$TEST_DIRECTORY/t1502/$spec.help"
+ } >expect &&
+ test_expect_code 129 git rev-parse --parseopt -- $opt \
+ 2>output <"$TEST_DIRECTORY/t1502/$spec" &&
+ test_cmp expect output
+ '
+}
+
+test_expect_success 'setup optionspec' '
+ sed -e "s/^|//" >optionspec <<\EOF
+|some-command [options] <args>...
+|
+|some-command does foo and bar!
+|--
+|h,help! show the help
+|
+|foo some nifty option --foo
+|bar= some cool option --bar with an argument
+|b,baz a short and long option
+|
+| An option group Header
+|C? option C with an optional argument
+|d,data? short and long option with an optional argument
+|
+| Argument hints
+|B=arg short option required argument
+|bar2=arg long option required argument
+|e,fuz=with-space short and long option required argument
+|s?some short option optional argument
+|long?data long option optional argument
+|g,fluf?path short and long option optional argument
+|longest=very-long-argument-hint a very long argument hint
+|pair=key=value with an equals sign in the hint
+|aswitch help te=t contains? fl*g characters!`
+|bswitch=hint hint has trailing tab character
+|cswitch switch has trailing tab character
+|short-hint=a with a one symbol hint
+|
+|Extras
+|extra1 line above used to cause a segfault but no longer does
+EOF
+'
+
+test_expect_success 'setup optionspec-no-switches' '
+ sed -e "s/^|//" >optionspec_no_switches <<\EOF
+|some-command [options] <args>...
+|
+|some-command does foo and bar!
+|--
+EOF
+'
+
+test_expect_success 'setup optionspec-only-hidden-switches' '
+ sed -e "s/^|//" >optionspec_only_hidden_switches <<\EOF
+|some-command [options] <args>...
+|
+|some-command does foo and bar!
+|--
+|hidden1* A hidden switch
+EOF
+'
+
+test_expect_success 'test --parseopt help output' '
+ test_expect_code 129 git rev-parse --parseopt -- -h > output < optionspec &&
+ test_cmp "$TEST_DIRECTORY/t1502/optionspec.help" output
+'
+
+test_expect_success 'test --parseopt help output no switches' '
+ sed -e "s/^|//" >expect <<\END_EXPECT &&
+|cat <<\EOF
+|usage: some-command [options] <args>...
+|
+| some-command does foo and bar!
+|
+|EOF
+END_EXPECT
+ test_expect_code 129 git rev-parse --parseopt -- -h > output < optionspec_no_switches &&
+ test_cmp expect output
+'
+
+test_expect_success 'test --parseopt help output hidden switches' '
+ sed -e "s/^|//" >expect <<\END_EXPECT &&
+|cat <<\EOF
+|usage: some-command [options] <args>...
+|
+| some-command does foo and bar!
+|
+|EOF
+END_EXPECT
+ test_expect_code 129 git rev-parse --parseopt -- -h > output < optionspec_only_hidden_switches &&
+ test_cmp expect output
+'
+
+test_expect_success 'test --parseopt help-all output hidden switches' '
+ sed -e "s/^|//" >expect <<\END_EXPECT &&
+|cat <<\EOF
+|usage: some-command [options] <args>...
+|
+| some-command does foo and bar!
+|
+| --[no-]hidden1 A hidden switch
+|
+|EOF
+END_EXPECT
+ test_expect_code 129 git rev-parse --parseopt -- --help-all > output < optionspec_only_hidden_switches &&
+ test_cmp expect output
+'
+
+test_expect_success 'test --parseopt invalid switch help output' '
+ {
+ cat <<-\EOF &&
+ error: unknown option `does-not-exist'\''
+ EOF
+ sed -e 1d -e \$d <"$TEST_DIRECTORY/t1502/optionspec.help"
+ } >expect &&
+ test_expect_code 129 git rev-parse --parseopt -- --does-not-exist 1>/dev/null 2>output < optionspec &&
+ test_cmp expect output
+'
+
+test_expect_success 'setup expect.1' "
+ cat > expect <<EOF
+set -- --foo --bar 'ham' -b --aswitch -- 'arg'
+EOF
+"
+
+test_expect_success 'test --parseopt' '
+ git rev-parse --parseopt -- --foo --bar=ham --baz --aswitch arg < optionspec > output &&
+ test_cmp expect output
+'
+
+test_expect_success 'test --parseopt with mixed options and arguments' '
+ git rev-parse --parseopt -- --foo arg --bar=ham --baz --aswitch < optionspec > output &&
+ test_cmp expect output
+'
+
+test_expect_success 'setup expect.2' "
+ cat > expect <<EOF
+set -- --foo -- 'arg' '--bar=ham'
+EOF
+"
+
+test_expect_success 'test --parseopt with --' '
+ git rev-parse --parseopt -- --foo -- arg --bar=ham < optionspec > output &&
+ test_cmp expect output
+'
+
+test_expect_success 'test --parseopt --stop-at-non-option' '
+ git rev-parse --parseopt --stop-at-non-option -- --foo arg --bar=ham < optionspec > output &&
+ test_cmp expect output
+'
+
+test_expect_success 'setup expect.3' "
+ cat > expect <<EOF
+set -- --foo -- '--' 'arg' '--bar=ham'
+EOF
+"
+
+test_expect_success 'test --parseopt --keep-dashdash' '
+ git rev-parse --parseopt --keep-dashdash -- --foo -- arg --bar=ham < optionspec > output &&
+ test_cmp expect output
+'
+
+test_expect_success 'setup expect.4' "
+ cat >expect <<EOF
+set -- --foo -- '--' 'arg' '--spam=ham'
+EOF
+"
+
+test_expect_success 'test --parseopt --keep-dashdash --stop-at-non-option with --' '
+ git rev-parse --parseopt --keep-dashdash --stop-at-non-option -- --foo -- arg --spam=ham <optionspec >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'setup expect.5' "
+ cat > expect <<EOF
+set -- --foo -- 'arg' '--spam=ham'
+EOF
+"
+
+test_expect_success 'test --parseopt --keep-dashdash --stop-at-non-option without --' '
+ git rev-parse --parseopt --keep-dashdash --stop-at-non-option -- --foo arg --spam=ham <optionspec >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'setup expect.6' "
+ cat > expect <<EOF
+set -- --foo --bar='z' --baz -C'Z' --data='A' -- 'arg'
+EOF
+"
+
+test_expect_success 'test --parseopt --stuck-long' '
+ git rev-parse --parseopt --stuck-long -- --foo --bar=z -b arg -CZ -dA <optionspec >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'setup expect.7' "
+ cat > expect <<EOF
+set -- --data='' -C --baz -- 'arg'
+EOF
+"
+
+test_expect_success 'test --parseopt --stuck-long and empty optional argument' '
+ git rev-parse --parseopt --stuck-long -- --data= arg -C -b <optionspec >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'setup expect.8' "
+ cat > expect <<EOF
+set -- --data --baz -- 'arg'
+EOF
+"
+
+test_expect_success 'test --parseopt --stuck-long and long option with unset optional argument' '
+ git rev-parse --parseopt --stuck-long -- --data arg -b <optionspec >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'test --parseopt --stuck-long and short option with unset optional argument' '
+ git rev-parse --parseopt --stuck-long -- -d arg -b <optionspec >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'test --parseopt help output: "wrapped" options normal "or:" lines' '
+ sed -e "s/^|//" >spec <<-\EOF &&
+ |cmd [--some-option]
+ | [--another-option]
+ |cmd [--yet-another-option]
+ |--
+ |h,help! show the help
+ EOF
+
+ sed -e "s/^|//" >expect <<-\END_EXPECT &&
+ |cat <<\EOF
+ |usage: cmd [--some-option]
+ | or: [--another-option]
+ | or: cmd [--yet-another-option]
+ |
+ | -h, --help show the help
+ |
+ |EOF
+ END_EXPECT
+
+ test_must_fail git rev-parse --parseopt -- -h <spec >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'test --parseopt invalid opt-spec' '
+ test_write_lines x -- "=, x" >spec &&
+ echo "fatal: missing opt-spec before option flags" >expect &&
+ test_must_fail git rev-parse --parseopt -- <spec 2>err &&
+ test_cmp expect err
+'
+
+test_expect_success 'test --parseopt help output: multi-line blurb after empty line' '
+ sed -e "s/^|//" >spec <<-\EOF &&
+ |cmd [--some-option]
+ | [--another-option]
+ |
+ |multi
+ |line
+ |blurb
+ |--
+ |h,help! show the help
+ EOF
+
+ sed -e "s/^|//" >expect <<-\END_EXPECT &&
+ |cat <<\EOF
+ |usage: cmd [--some-option]
+ | or: [--another-option]
+ |
+ | multi
+ | line
+ | blurb
+ |
+ | -h, --help show the help
+ |
+ |EOF
+ END_EXPECT
+
+ test_must_fail git rev-parse --parseopt -- -h <spec >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'test --parseopt help output for optionspec-neg' '
+ test_expect_code 129 git rev-parse --parseopt -- \
+ -h >output <"$TEST_DIRECTORY/t1502/optionspec-neg" &&
+ test_cmp "$TEST_DIRECTORY/t1502/optionspec-neg.help" output
+'
+
+test_expect_success 'test --parseopt valid options for optionspec-neg' '
+ cat >expect <<-\EOF &&
+ set -- --foo --no-foo --no-bar --positive-only --no-negative --
+ EOF
+ git rev-parse --parseopt -- <"$TEST_DIRECTORY/t1502/optionspec-neg" >output \
+ --foo --no-foo --no-bar --positive-only --no-negative &&
+ test_cmp expect output
+'
+
+test_expect_success 'test --parseopt positivated option for optionspec-neg' '
+ cat >expect <<-\EOF &&
+ set -- --no-no-bar --no-no-bar --
+ EOF
+ git rev-parse --parseopt -- <"$TEST_DIRECTORY/t1502/optionspec-neg" >output \
+ --no-no-bar --bar &&
+ test_cmp expect output
+'
+
+check_invalid_long_option optionspec-neg --no-positive-only
+check_invalid_long_option optionspec-neg --negative
+check_invalid_long_option optionspec-neg --no-no-negative
+
+test_done
diff --git a/t/t1502/.gitattributes b/t/t1502/.gitattributes
new file mode 100644
index 0000000..562b12e
--- /dev/null
+++ b/t/t1502/.gitattributes
@@ -0,0 +1 @@
+* -whitespace
diff --git a/t/t1502/optionspec-neg b/t/t1502/optionspec-neg
new file mode 100644
index 0000000..392f43e
--- /dev/null
+++ b/t/t1502/optionspec-neg
@@ -0,0 +1,8 @@
+some-command [options] <args>...
+
+some-command does foo and bar!
+--
+foo can be negated
+no-bar can be positivated
+positive-only! cannot be negated
+no-negative! cannot be positivated
diff --git a/t/t1502/optionspec-neg.help b/t/t1502/optionspec-neg.help
new file mode 100644
index 0000000..7a29f8c
--- /dev/null
+++ b/t/t1502/optionspec-neg.help
@@ -0,0 +1,12 @@
+cat <<\EOF
+usage: some-command [options] <args>...
+
+ some-command does foo and bar!
+
+ --[no-]foo can be negated
+ --no-bar can be positivated
+ --bar opposite of --no-bar
+ --positive-only cannot be negated
+ --no-negative cannot be positivated
+
+EOF
diff --git a/t/t1502/optionspec.help b/t/t1502/optionspec.help
new file mode 100755
index 0000000..cbdd54d
--- /dev/null
+++ b/t/t1502/optionspec.help
@@ -0,0 +1,36 @@
+cat <<\EOF
+usage: some-command [options] <args>...
+
+ some-command does foo and bar!
+
+ -h, --help show the help
+ --[no-]foo some nifty option --foo
+ --[no-]bar ... some cool option --bar with an argument
+ -b, --[no-]baz a short and long option
+
+An option group Header
+ -C[...] option C with an optional argument
+ -d, --[no-]data[=...] short and long option with an optional argument
+
+Argument hints
+ -B <arg> short option required argument
+ --[no-]bar2 <arg> long option required argument
+ -e, --[no-]fuz <with-space>
+ short and long option required argument
+ -s[<some>] short option optional argument
+ --[no-]long[=<data>] long option optional argument
+ -g, --[no-]fluf[=<path>]
+ short and long option optional argument
+ --[no-]longest <very-long-argument-hint>
+ a very long argument hint
+ --[no-]pair <key=value>
+ with an equals sign in the hint
+ --[no-]aswitch help te=t contains? fl*g characters!`
+ --[no-]bswitch <hint> hint has trailing tab character
+ --[no-]cswitch switch has trailing tab character
+ --[no-]short-hint <a> with a one symbol hint
+
+Extras
+ --[no-]extra1 line above used to cause a segfault but no longer does
+
+EOF
diff --git a/t/t1503-rev-parse-verify.sh b/t/t1503-rev-parse-verify.sh
new file mode 100755
index 0000000..bc13683
--- /dev/null
+++ b/t/t1503-rev-parse-verify.sh
@@ -0,0 +1,165 @@
+#!/bin/sh
+#
+# Copyright (c) 2008 Christian Couder
+#
+test_description='test git rev-parse --verify'
+
+exec </dev/null
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+add_line_into_file()
+{
+ _line=$1
+ _file=$2
+
+ if [ -f "$_file" ]; then
+ echo "$_line" >> $_file || return $?
+ MSG="Add <$_line> into <$_file>."
+ else
+ echo "$_line" > $_file || return $?
+ git add $_file || return $?
+ MSG="Create file <$_file> with <$_line> inside."
+ fi
+
+ test_tick
+ git commit --quiet -m "$MSG" $_file
+}
+
+HASH1=
+HASH2=
+HASH3=
+HASH4=
+
+test_expect_success 'set up basic repo with 1 file (hello) and 4 commits' '
+ add_line_into_file "1: Hello World" hello &&
+ HASH1=$(git rev-parse --verify HEAD) &&
+ add_line_into_file "2: A new day for git" hello &&
+ HASH2=$(git rev-parse --verify HEAD) &&
+ add_line_into_file "3: Another new day for git" hello &&
+ HASH3=$(git rev-parse --verify HEAD) &&
+ add_line_into_file "4: Ciao for now" hello &&
+ HASH4=$(git rev-parse --verify HEAD)
+'
+
+test_expect_success 'works with one good rev' '
+ rev_hash1=$(git rev-parse --verify $HASH1) &&
+ test "$rev_hash1" = "$HASH1" &&
+ rev_hash2=$(git rev-parse --verify $HASH2) &&
+ test "$rev_hash2" = "$HASH2" &&
+ rev_hash3=$(git rev-parse --verify $HASH3) &&
+ test "$rev_hash3" = "$HASH3" &&
+ rev_hash4=$(git rev-parse --verify $HASH4) &&
+ test "$rev_hash4" = "$HASH4" &&
+ rev_main=$(git rev-parse --verify main) &&
+ test "$rev_main" = "$HASH4" &&
+ rev_head=$(git rev-parse --verify HEAD) &&
+ test "$rev_head" = "$HASH4"
+'
+
+test_expect_success 'fails with any bad rev or many good revs' '
+ test_must_fail git rev-parse --verify 2>error &&
+ grep "single revision" error &&
+ test_must_fail git rev-parse --verify foo 2>error &&
+ grep "single revision" error &&
+ test_must_fail git rev-parse --verify HEAD bar 2>error &&
+ grep "single revision" error &&
+ test_must_fail git rev-parse --verify baz HEAD 2>error &&
+ grep "single revision" error &&
+ test_must_fail git rev-parse --verify $HASH2 HEAD 2>error &&
+ grep "single revision" error
+'
+
+test_expect_success 'fails silently when using -q' '
+ test_must_fail git rev-parse --verify --quiet 2>error &&
+ test_must_be_empty error &&
+ test_must_fail git rev-parse -q --verify foo 2>error &&
+ test_must_be_empty error &&
+ test_must_fail git rev-parse --verify -q HEAD bar 2>error &&
+ test_must_be_empty error &&
+ test_must_fail git rev-parse --quiet --verify baz HEAD 2>error &&
+ test_must_be_empty error &&
+ test_must_fail git rev-parse -q --verify $HASH2 HEAD 2>error &&
+ test_must_be_empty error
+'
+
+test_expect_success 'fails silently when using -q with deleted reflogs' '
+ ref=$(git rev-parse HEAD) &&
+ git update-ref --create-reflog -m "message for refs/test" refs/test "$ref" &&
+ git reflog delete --updateref --rewrite refs/test@{1} &&
+ test_must_fail git rev-parse -q --verify refs/test@{1} >error 2>&1 &&
+ test_must_be_empty error
+'
+
+test_expect_success 'fails silently when using -q with not enough reflogs' '
+ ref=$(git rev-parse HEAD) &&
+ git update-ref --create-reflog -m "message for refs/test2" refs/test2 "$ref" &&
+ test_must_fail git rev-parse -q --verify refs/test2@{999} >error 2>&1 &&
+ test_must_be_empty error
+'
+
+test_expect_success 'succeeds silently with -q and reflogs that do not go far back enough in time' '
+ ref=$(git rev-parse HEAD) &&
+ git update-ref --create-reflog -m "message for refs/test3" refs/test3 "$ref" &&
+ git rev-parse -q --verify refs/test3@{1.year.ago} >actual 2>error &&
+ test_must_be_empty error &&
+ echo "$ref" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'no stdout output on error' '
+ test -z "$(git rev-parse --verify)" &&
+ test -z "$(git rev-parse --verify foo)" &&
+ test -z "$(git rev-parse --verify baz HEAD)" &&
+ test -z "$(git rev-parse --verify HEAD bar)" &&
+ test -z "$(git rev-parse --verify $HASH2 HEAD)"
+'
+
+test_expect_success 'use --default' '
+ git rev-parse --verify --default main &&
+ git rev-parse --verify --default main HEAD &&
+ git rev-parse --default main --verify &&
+ git rev-parse --default main --verify HEAD &&
+ git rev-parse --verify HEAD --default main &&
+ test_must_fail git rev-parse --verify foo --default main &&
+ test_must_fail git rev-parse --default HEAD --verify bar &&
+ test_must_fail git rev-parse --verify --default HEAD baz &&
+ test_must_fail git rev-parse --default foo --verify &&
+ test_must_fail git rev-parse --verify --default bar
+'
+
+test_expect_success 'main@{n} for various n' '
+ git reflog >out &&
+ N=$(wc -l <out) &&
+ Nm1=$(($N-1)) &&
+ Np1=$(($N+1)) &&
+ git rev-parse --verify main@{0} &&
+ git rev-parse --verify main@{1} &&
+ git rev-parse --verify main@{$Nm1} &&
+ test_must_fail git rev-parse --verify main@{$N} &&
+ test_must_fail git rev-parse --verify main@{$Np1}
+'
+
+test_expect_success SYMLINKS,REFFILES 'ref resolution not confused by broken symlinks' '
+ ln -s does-not-exist .git/refs/heads/broken &&
+ test_must_fail git rev-parse --verify broken
+'
+
+test_expect_success 'options can appear after --verify' '
+ git rev-parse --verify HEAD >expect &&
+ git rev-parse --verify -q HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'verify respects --end-of-options' '
+ git update-ref refs/heads/-tricky HEAD &&
+ git rev-parse --verify HEAD >expect &&
+ git rev-parse --verify --end-of-options -tricky >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t1504-ceiling-dirs.sh b/t/t1504-ceiling-dirs.sh
new file mode 100755
index 0000000..c1679e3
--- /dev/null
+++ b/t/t1504-ceiling-dirs.sh
@@ -0,0 +1,187 @@
+#!/bin/sh
+
+test_description='test GIT_CEILING_DIRECTORIES'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_prefix() {
+ local expect="$2" &&
+ test_expect_success "$1: git rev-parse --show-prefix is '$2'" '
+ echo "$expect" >expect &&
+ git rev-parse --show-prefix >actual &&
+ test_cmp expect actual
+ '
+}
+
+test_fail() {
+ test_expect_success "$1: prefix" '
+ test_expect_code 128 git rev-parse --show-prefix
+ '
+}
+
+TRASH_ROOT="$PWD"
+ROOT_PARENT=$(dirname "$TRASH_ROOT")
+
+
+unset GIT_CEILING_DIRECTORIES
+test_prefix no_ceil ""
+
+export GIT_CEILING_DIRECTORIES
+
+GIT_CEILING_DIRECTORIES=""
+test_prefix ceil_empty ""
+
+GIT_CEILING_DIRECTORIES="$ROOT_PARENT"
+test_prefix ceil_at_parent ""
+
+GIT_CEILING_DIRECTORIES="$ROOT_PARENT/"
+test_prefix ceil_at_parent_slash ""
+
+GIT_CEILING_DIRECTORIES="$TRASH_ROOT"
+test_prefix ceil_at_trash ""
+
+GIT_CEILING_DIRECTORIES="$TRASH_ROOT/"
+test_prefix ceil_at_trash_slash ""
+
+GIT_CEILING_DIRECTORIES="$TRASH_ROOT/sub"
+test_prefix ceil_at_sub ""
+
+GIT_CEILING_DIRECTORIES="$TRASH_ROOT/sub/"
+test_prefix ceil_at_sub_slash ""
+
+if test_have_prereq SYMLINKS
+then
+ ln -s sub top
+fi
+
+mkdir -p sub/dir || exit 1
+cd sub/dir || exit 1
+
+unset GIT_CEILING_DIRECTORIES
+test_prefix subdir_no_ceil "sub/dir/"
+
+export GIT_CEILING_DIRECTORIES
+
+GIT_CEILING_DIRECTORIES=""
+test_prefix subdir_ceil_empty "sub/dir/"
+
+GIT_CEILING_DIRECTORIES="$TRASH_ROOT"
+test_fail subdir_ceil_at_trash
+
+GIT_CEILING_DIRECTORIES="$TRASH_ROOT/"
+test_fail subdir_ceil_at_trash_slash
+
+GIT_CEILING_DIRECTORIES="$TRASH_ROOT/sub"
+test_fail subdir_ceil_at_sub
+
+GIT_CEILING_DIRECTORIES="$TRASH_ROOT/sub/"
+test_fail subdir_ceil_at_sub_slash
+
+if test_have_prereq SYMLINKS
+then
+ GIT_CEILING_DIRECTORIES="$TRASH_ROOT/top"
+ test_fail subdir_ceil_at_top
+ GIT_CEILING_DIRECTORIES="$TRASH_ROOT/top/"
+ test_fail subdir_ceil_at_top_slash
+
+ GIT_CEILING_DIRECTORIES=":$TRASH_ROOT/top"
+ test_prefix subdir_ceil_at_top_no_resolve "sub/dir/"
+ GIT_CEILING_DIRECTORIES=":$TRASH_ROOT/top/"
+ test_prefix subdir_ceil_at_top_slash_no_resolve "sub/dir/"
+fi
+
+GIT_CEILING_DIRECTORIES="$TRASH_ROOT/sub/dir"
+test_prefix subdir_ceil_at_subdir "sub/dir/"
+
+GIT_CEILING_DIRECTORIES="$TRASH_ROOT/sub/dir/"
+test_prefix subdir_ceil_at_subdir_slash "sub/dir/"
+
+
+GIT_CEILING_DIRECTORIES="$TRASH_ROOT/su"
+test_prefix subdir_ceil_at_su "sub/dir/"
+
+GIT_CEILING_DIRECTORIES="$TRASH_ROOT/su/"
+test_prefix subdir_ceil_at_su_slash "sub/dir/"
+
+GIT_CEILING_DIRECTORIES="$TRASH_ROOT/sub/di"
+test_prefix subdir_ceil_at_sub_di "sub/dir/"
+
+GIT_CEILING_DIRECTORIES="$TRASH_ROOT/sub/di"
+test_prefix subdir_ceil_at_sub_di_slash "sub/dir/"
+
+GIT_CEILING_DIRECTORIES="$TRASH_ROOT/subdi"
+test_prefix subdir_ceil_at_subdi "sub/dir/"
+
+GIT_CEILING_DIRECTORIES="$TRASH_ROOT/subdi"
+test_prefix subdir_ceil_at_subdi_slash "sub/dir/"
+
+
+GIT_CEILING_DIRECTORIES="/foo:$TRASH_ROOT/sub"
+test_fail second_of_two
+
+GIT_CEILING_DIRECTORIES="$TRASH_ROOT/sub:/bar"
+test_fail first_of_two
+
+GIT_CEILING_DIRECTORIES="/foo:$TRASH_ROOT/sub:/bar"
+test_fail second_of_three
+
+
+GIT_CEILING_DIRECTORIES="$TRASH_ROOT/sub"
+GIT_DIR=../../.git
+export GIT_DIR
+test_prefix git_dir_specified ""
+unset GIT_DIR
+
+
+cd ../.. || exit 1
+mkdir -p s/d || exit 1
+cd s/d || exit 1
+
+unset GIT_CEILING_DIRECTORIES
+test_prefix sd_no_ceil "s/d/"
+
+export GIT_CEILING_DIRECTORIES
+
+GIT_CEILING_DIRECTORIES=""
+test_prefix sd_ceil_empty "s/d/"
+
+GIT_CEILING_DIRECTORIES="$TRASH_ROOT"
+test_fail sd_ceil_at_trash
+
+GIT_CEILING_DIRECTORIES="$TRASH_ROOT/"
+test_fail sd_ceil_at_trash_slash
+
+GIT_CEILING_DIRECTORIES="$TRASH_ROOT/s"
+test_fail sd_ceil_at_s
+
+GIT_CEILING_DIRECTORIES="$TRASH_ROOT/s/"
+test_fail sd_ceil_at_s_slash
+
+GIT_CEILING_DIRECTORIES="$TRASH_ROOT/s/d"
+test_prefix sd_ceil_at_sd "s/d/"
+
+GIT_CEILING_DIRECTORIES="$TRASH_ROOT/s/d/"
+test_prefix sd_ceil_at_sd_slash "s/d/"
+
+
+GIT_CEILING_DIRECTORIES="$TRASH_ROOT/su"
+test_prefix sd_ceil_at_su "s/d/"
+
+GIT_CEILING_DIRECTORIES="$TRASH_ROOT/su/"
+test_prefix sd_ceil_at_su_slash "s/d/"
+
+GIT_CEILING_DIRECTORIES="$TRASH_ROOT/s/di"
+test_prefix sd_ceil_at_s_di "s/d/"
+
+GIT_CEILING_DIRECTORIES="$TRASH_ROOT/s/di"
+test_prefix sd_ceil_at_s_di_slash "s/d/"
+
+GIT_CEILING_DIRECTORIES="$TRASH_ROOT/sdi"
+test_prefix sd_ceil_at_sdi "s/d/"
+
+GIT_CEILING_DIRECTORIES="$TRASH_ROOT/sdi"
+test_prefix sd_ceil_at_sdi_slash "s/d/"
+
+
+test_done
diff --git a/t/t1505-rev-parse-last.sh b/t/t1505-rev-parse-last.sh
new file mode 100755
index 0000000..4a5758f
--- /dev/null
+++ b/t/t1505-rev-parse-last.sh
@@ -0,0 +1,65 @@
+#!/bin/sh
+
+test_description='test @{-N} syntax'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+
+make_commit () {
+ echo "$1" > "$1" &&
+ git add "$1" &&
+ git commit -m "$1"
+}
+
+
+test_expect_success 'setup' '
+
+ make_commit 1 &&
+ git branch side &&
+ make_commit 2 &&
+ make_commit 3 &&
+ git checkout side &&
+ make_commit 4 &&
+ git merge main &&
+ git checkout main
+
+'
+
+# 1 -- 2 -- 3 main
+# \ \
+# \ \
+# --- 4 --- 5 side
+#
+# and 'side' should be the last branch
+
+test_expect_success '@{-1} works' '
+ test_cmp_rev side @{-1}
+'
+
+test_expect_success '@{-1}~2 works' '
+ test_cmp_rev side~2 @{-1}~2
+'
+
+test_expect_success '@{-1}^2 works' '
+ test_cmp_rev side^2 @{-1}^2
+'
+
+test_expect_success '@{-1}@{1} works' '
+ test_cmp_rev side@{1} @{-1}@{1}
+'
+
+test_expect_success '@{-2} works' '
+ test_cmp_rev main @{-2}
+'
+
+test_expect_success '@{-3} fails' '
+ test_must_fail git rev-parse @{-3}
+'
+
+test_done
+
+
diff --git a/t/t1506-rev-parse-diagnosis.sh b/t/t1506-rev-parse-diagnosis.sh
new file mode 100755
index 0000000..ef40511
--- /dev/null
+++ b/t/t1506-rev-parse-diagnosis.sh
@@ -0,0 +1,286 @@
+#!/bin/sh
+
+test_description='test git rev-parse diagnosis for invalid argument'
+
+exec </dev/null
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_did_you_mean ()
+{
+ cat >expected <<-EOF &&
+ fatal: path '$2$3' $4, but not ${5:-$SQ$3$SQ}
+ hint: Did you mean '$1:$2$3'${2:+ aka $SQ$1:./$3$SQ}?
+ EOF
+ test_cmp expected error
+}
+
+HASH_file=
+
+test_expect_success 'set up basic repo' '
+ echo one > file.txt &&
+ mkdir subdir &&
+ echo two > subdir/file.txt &&
+ echo three > subdir/file2.txt &&
+ git add . &&
+ git commit -m init &&
+ echo four > index-only.txt &&
+ git add index-only.txt &&
+ echo five > disk-only.txt
+'
+
+test_expect_success 'correct file objects' '
+ HASH_file=$(git rev-parse HEAD:file.txt) &&
+ git rev-parse HEAD:subdir/file.txt &&
+ git rev-parse :index-only.txt &&
+ (cd subdir &&
+ git rev-parse HEAD:subdir/file2.txt &&
+ test $HASH_file = $(git rev-parse HEAD:file.txt) &&
+ test $HASH_file = $(git rev-parse :file.txt) &&
+ test $HASH_file = $(git rev-parse :0:file.txt) )
+'
+
+test_expect_success 'correct relative file objects (0)' '
+ git rev-parse :file.txt >expected &&
+ git rev-parse :./file.txt >result &&
+ test_cmp expected result &&
+ git rev-parse :0:./file.txt >result &&
+ test_cmp expected result
+'
+
+test_expect_success 'correct relative file objects (1)' '
+ git rev-parse HEAD:file.txt >expected &&
+ git rev-parse HEAD:./file.txt >result &&
+ test_cmp expected result
+'
+
+test_expect_success 'correct relative file objects (2)' '
+ (
+ cd subdir &&
+ git rev-parse HEAD:../file.txt >result &&
+ test_cmp ../expected result
+ )
+'
+
+test_expect_success 'correct relative file objects (3)' '
+ (
+ cd subdir &&
+ git rev-parse HEAD:../subdir/../file.txt >result &&
+ test_cmp ../expected result
+ )
+'
+
+test_expect_success 'correct relative file objects (4)' '
+ git rev-parse HEAD:subdir/file.txt >expected &&
+ (
+ cd subdir &&
+ git rev-parse HEAD:./file.txt >result &&
+ test_cmp ../expected result
+ )
+'
+
+test_expect_success 'correct relative file objects (5)' '
+ git rev-parse :subdir/file.txt >expected &&
+ (
+ cd subdir &&
+ git rev-parse :./file.txt >result &&
+ test_cmp ../expected result &&
+ git rev-parse :0:./file.txt >result &&
+ test_cmp ../expected result
+ )
+'
+
+test_expect_success 'correct relative file objects (6)' '
+ git rev-parse :file.txt >expected &&
+ (
+ cd subdir &&
+ git rev-parse :../file.txt >result &&
+ test_cmp ../expected result &&
+ git rev-parse :0:../file.txt >result &&
+ test_cmp ../expected result
+ )
+'
+
+test_expect_success 'incorrect revision id' '
+ test_must_fail git rev-parse foobar:file.txt 2>error &&
+ test_grep "invalid object name .foobar." error &&
+ test_must_fail git rev-parse foobar 2>error &&
+ test_grep "unknown revision or path not in the working tree." error
+'
+
+test_expect_success 'incorrect file in sha1:path' '
+ test_must_fail git rev-parse HEAD:nothing.txt 2>error &&
+ test_grep "path .nothing.txt. does not exist in .HEAD." error &&
+ test_must_fail git rev-parse HEAD:index-only.txt 2>error &&
+ test_grep "path .index-only.txt. exists on disk, but not in .HEAD." error &&
+ (cd subdir &&
+ test_must_fail git rev-parse HEAD:file2.txt 2>error &&
+ test_did_you_mean HEAD subdir/ file2.txt exists )
+'
+
+test_expect_success 'incorrect file in :path and :N:path' '
+ test_must_fail git rev-parse :nothing.txt 2>error &&
+ test_grep "path .nothing.txt. does not exist (neither on disk nor in the index)" error &&
+ test_must_fail git rev-parse :1:nothing.txt 2>error &&
+ test_grep "path .nothing.txt. does not exist (neither on disk nor in the index)" error &&
+ test_must_fail git rev-parse :1:file.txt 2>error &&
+ test_did_you_mean ":0" "" file.txt "is in the index" "at stage 1" &&
+ (cd subdir &&
+ test_must_fail git rev-parse :1:file.txt 2>error &&
+ test_did_you_mean ":0" "" file.txt "is in the index" "at stage 1" &&
+ test_must_fail git rev-parse :file2.txt 2>error &&
+ test_did_you_mean ":0" subdir/ file2.txt "is in the index" &&
+ test_must_fail git rev-parse :2:file2.txt 2>error &&
+ test_did_you_mean :0 subdir/ file2.txt "is in the index") &&
+ test_must_fail git rev-parse :disk-only.txt 2>error &&
+ test_grep "path .disk-only.txt. exists on disk, but not in the index" error
+'
+
+test_expect_success 'invalid @{n} reference' '
+ test_must_fail git rev-parse main@{99999} >output 2>error &&
+ test_must_be_empty output &&
+ test_grep "log for [^ ]* only has [0-9][0-9]* entries" error &&
+ test_must_fail git rev-parse --verify main@{99999} >output 2>error &&
+ test_must_be_empty output &&
+ test_grep "log for [^ ]* only has [0-9][0-9]* entries" error
+'
+
+test_expect_success 'relative path not found' '
+ (
+ cd subdir &&
+ test_must_fail git rev-parse HEAD:./nonexistent.txt 2>error &&
+ test_grep subdir/nonexistent.txt error
+ )
+'
+
+test_expect_success 'relative path outside worktree' '
+ test_must_fail git rev-parse HEAD:../file.txt >output 2>error &&
+ test_must_be_empty output &&
+ test_grep "outside repository" error
+'
+
+test_expect_success 'relative path when cwd is outside worktree' '
+ test_must_fail git --git-dir=.git --work-tree=subdir rev-parse HEAD:./file.txt >output 2>error &&
+ test_must_be_empty output &&
+ test_grep "relative path syntax can.t be used outside working tree" error
+'
+
+test_expect_success '<commit>:file correctly diagnosed after a pathname' '
+ test_must_fail git rev-parse file.txt HEAD:file.txt 1>actual 2>error &&
+ test_grep ! "exists on disk" error &&
+ test_grep "no such path in the working tree" error &&
+ cat >expect <<-\EOF &&
+ file.txt
+ HEAD:file.txt
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'dotdot is not an empty set' '
+ ( H=$(git rev-parse HEAD) && echo $H && echo ^$H ) >expect &&
+
+ git rev-parse HEAD.. >actual &&
+ test_cmp expect actual &&
+
+ git rev-parse ..HEAD >actual &&
+ test_cmp expect actual &&
+
+ echo .. >expect &&
+ git rev-parse .. >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'dotdot does not peel endpoints' '
+ git tag -a -m "annote" annotated HEAD &&
+ A=$(git rev-parse annotated) &&
+ H=$(git rev-parse annotated^0) &&
+ {
+ echo $A && echo ^$A
+ } >expect-with-two-dots &&
+ {
+ echo $A && echo $A && echo ^$H
+ } >expect-with-merge-base &&
+
+ git rev-parse annotated..annotated >actual-with-two-dots &&
+ test_cmp expect-with-two-dots actual-with-two-dots &&
+
+ git rev-parse annotated...annotated >actual-with-merge-base &&
+ test_cmp expect-with-merge-base actual-with-merge-base
+'
+
+test_expect_success 'arg before dashdash must be a revision (missing)' '
+ test_must_fail git rev-parse foobar -- 2>stderr &&
+ test_grep "bad revision" stderr
+'
+
+test_expect_success 'arg before dashdash must be a revision (file)' '
+ >foobar &&
+ test_must_fail git rev-parse foobar -- 2>stderr &&
+ test_grep "bad revision" stderr
+'
+
+test_expect_success 'arg before dashdash must be a revision (ambiguous)' '
+ >foobar &&
+ git update-ref refs/heads/foobar HEAD &&
+ {
+ # we do not want to use rev-parse here, because
+ # we are testing it
+ git show-ref -s refs/heads/foobar &&
+ printf "%s\n" --
+ } >expect &&
+ git rev-parse foobar -- >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'reject Nth parent if N is too high' '
+ test_must_fail git rev-parse HEAD^100000000000000000000000000000000
+'
+
+test_expect_success 'reject Nth ancestor if N is too high' '
+ test_must_fail git rev-parse HEAD~100000000000000000000000000000000
+'
+
+test_expect_success 'pathspecs with wildcards are not ambiguous' '
+ echo "*.c" >expect &&
+ git rev-parse "*.c" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'backslash does not trigger wildcard rule' '
+ test_must_fail git rev-parse "foo\\bar"
+'
+
+test_expect_success 'escaped char does not trigger wildcard rule' '
+ test_must_fail git rev-parse "foo\\*bar"
+'
+
+test_expect_success 'arg after dashdash not interpreted as option' '
+ cat >expect <<-\EOF &&
+ --
+ --local-env-vars
+ EOF
+ git rev-parse -- --local-env-vars >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'arg after end-of-options not interpreted as option' '
+ test_must_fail git rev-parse --end-of-options --not-real -- 2>err &&
+ test_grep bad.revision.*--not-real err
+'
+
+test_expect_success 'end-of-options still allows --' '
+ cat >expect <<-EOF &&
+ --end-of-options
+ $(git rev-parse --verify HEAD)
+ --
+ path
+ EOF
+ git rev-parse --end-of-options HEAD -- path >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t1507-rev-parse-upstream.sh b/t/t1507-rev-parse-upstream.sh
new file mode 100755
index 0000000..b9af6b3
--- /dev/null
+++ b/t/t1507-rev-parse-upstream.sh
@@ -0,0 +1,281 @@
+#!/bin/sh
+
+test_description='test <branch>@{upstream} syntax'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+
+test_expect_success 'setup' '
+
+ test_commit 1 &&
+ git checkout -b side &&
+ test_commit 2 &&
+ git checkout main &&
+ git clone . clone &&
+ test_commit 3 &&
+ (cd clone &&
+ test_commit 4 &&
+ git branch --track my-side origin/side &&
+ git branch --track local-main main &&
+ git branch --track fun@ny origin/side &&
+ git branch --track @funny origin/side &&
+ git branch --track funny@ origin/side &&
+ git remote add -t main main-only .. &&
+ git fetch main-only &&
+ git branch bad-upstream &&
+ git config branch.bad-upstream.remote main-only &&
+ git config branch.bad-upstream.merge refs/heads/side
+ )
+'
+
+commit_subject () {
+ (cd clone &&
+ git show -s --pretty=tformat:%s "$@")
+}
+
+error_message () {
+ (cd clone &&
+ test_must_fail git rev-parse --verify "$@" 2>../error)
+}
+
+test_expect_success '@{upstream} resolves to correct full name' '
+ echo refs/remotes/origin/main >expect &&
+ git -C clone rev-parse --symbolic-full-name @{upstream} >actual &&
+ test_cmp expect actual &&
+ git -C clone rev-parse --symbolic-full-name @{UPSTREAM} >actual &&
+ test_cmp expect actual &&
+ git -C clone rev-parse --symbolic-full-name @{UpSTReam} >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '@{u} resolves to correct full name' '
+ echo refs/remotes/origin/main >expect &&
+ git -C clone rev-parse --symbolic-full-name @{u} >actual &&
+ test_cmp expect actual &&
+ git -C clone rev-parse --symbolic-full-name @{U} >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'my-side@{upstream} resolves to correct full name' '
+ echo refs/remotes/origin/side >expect &&
+ git -C clone rev-parse --symbolic-full-name my-side@{u} >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'upstream of branch with @ in middle' '
+ git -C clone rev-parse --symbolic-full-name fun@ny@{u} >actual &&
+ echo refs/remotes/origin/side >expect &&
+ test_cmp expect actual &&
+ git -C clone rev-parse --symbolic-full-name fun@ny@{U} >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'upstream of branch with @ at start' '
+ git -C clone rev-parse --symbolic-full-name @funny@{u} >actual &&
+ echo refs/remotes/origin/side >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'upstream of branch with @ at end' '
+ git -C clone rev-parse --symbolic-full-name funny@@{u} >actual &&
+ echo refs/remotes/origin/side >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'refs/heads/my-side@{upstream} does not resolve to my-side{upstream}' '
+ test_must_fail git -C clone rev-parse --symbolic-full-name refs/heads/my-side@{upstream}
+'
+
+test_expect_success 'my-side@{u} resolves to correct commit' '
+ git checkout side &&
+ test_commit 5 &&
+ (cd clone && git fetch) &&
+ echo 2 >expect &&
+ commit_subject my-side >actual &&
+ test_cmp expect actual &&
+ echo 5 >expect &&
+ commit_subject my-side@{u} >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'not-tracking@{u} fails' '
+ test_must_fail git -C clone rev-parse --symbolic-full-name non-tracking@{u} &&
+ (cd clone && git checkout --no-track -b non-tracking) &&
+ test_must_fail git -C clone rev-parse --symbolic-full-name non-tracking@{u}
+'
+
+test_expect_success '<branch>@{u}@{1} resolves correctly' '
+ test_commit 6 &&
+ (cd clone && git fetch) &&
+ echo 5 >expect &&
+ commit_subject my-side@{u}@{1} >actual &&
+ test_cmp expect actual &&
+ commit_subject my-side@{U}@{1} >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '@{u} without specifying branch fails on a detached HEAD' '
+ git checkout HEAD^0 &&
+ test_must_fail git rev-parse @{u} &&
+ test_must_fail git rev-parse @{U}
+'
+
+test_expect_success 'checkout -b new my-side@{u} forks from the same' '
+(
+ cd clone &&
+ git checkout -b new my-side@{u} &&
+ git rev-parse --symbolic-full-name my-side@{u} >expect &&
+ git rev-parse --symbolic-full-name new@{u} >actual &&
+ test_cmp expect actual
+)
+'
+
+test_expect_success 'merge my-side@{u} records the correct name' '
+(
+ cd clone &&
+ git checkout main &&
+ test_might_fail git branch -D new &&
+ git branch -t new my-side@{u} &&
+ git merge -s ours new@{u} &&
+ git show -s --pretty=tformat:%s >actual &&
+ echo "Merge remote-tracking branch ${SQ}origin/side${SQ}" >expect &&
+ test_cmp expect actual
+)
+'
+
+test_expect_success 'branch -d other@{u}' '
+ git checkout -t -b other main &&
+ git branch -d @{u} &&
+ git for-each-ref refs/heads/main >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'checkout other@{u}' '
+ git branch -f main HEAD &&
+ git checkout -t -b another main &&
+ git checkout @{u} &&
+ git symbolic-ref HEAD >actual &&
+ echo refs/heads/main >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'branch@{u} works when tracking a local branch' '
+ echo refs/heads/main >expect &&
+ git -C clone rev-parse --symbolic-full-name local-main@{u} >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'branch@{u} error message when no upstream' '
+ cat >expect <<-EOF &&
+ fatal: no upstream configured for branch ${SQ}non-tracking${SQ}
+ EOF
+ error_message non-tracking@{u} &&
+ test_cmp expect error
+'
+
+test_expect_success '@{u} error message when no upstream' '
+ cat >expect <<-EOF &&
+ fatal: no upstream configured for branch ${SQ}main${SQ}
+ EOF
+ test_must_fail git rev-parse --verify @{u} 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '@{u} silent error when no upstream' '
+ test_must_fail git rev-parse --verify --quiet @{u} 2>actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'branch@{u} error message with misspelt branch' '
+ cat >expect <<-EOF &&
+ fatal: no such branch: ${SQ}no-such-branch${SQ}
+ EOF
+ error_message no-such-branch@{u} &&
+ test_cmp expect error
+'
+
+test_expect_success '@{u} error message when not on a branch' '
+ cat >expect <<-EOF &&
+ fatal: HEAD does not point to a branch
+ EOF
+ git checkout HEAD^0 &&
+ test_must_fail git rev-parse --verify @{u} 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'branch@{u} error message if upstream branch not fetched' '
+ cat >expect <<-EOF &&
+ fatal: upstream branch ${SQ}refs/heads/side${SQ} not stored as a remote-tracking branch
+ EOF
+ error_message bad-upstream@{u} &&
+ test_cmp expect error
+'
+
+test_expect_success 'pull works when tracking a local branch' '
+(
+ cd clone &&
+ git checkout local-main &&
+ git pull
+)
+'
+
+# makes sense if the previous one succeeded
+test_expect_success '@{u} works when tracking a local branch' '
+ echo refs/heads/main >expect &&
+ git -C clone rev-parse --symbolic-full-name @{u} >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log -g other@{u}' '
+ commit=$(git rev-parse HEAD) &&
+ cat >expect <<-EOF &&
+ commit $commit
+ Reflog: main@{0} (C O Mitter <committer@example.com>)
+ Reflog message: branch: Created from HEAD
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:15:13 2005 -0700
+
+ 3
+ EOF
+ git log -1 -g other@{u} >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log -g other@{u}@{now}' '
+ commit=$(git rev-parse HEAD) &&
+ cat >expect <<-EOF &&
+ commit $commit
+ Reflog: main@{Thu Apr 7 15:17:13 2005 -0700} (C O Mitter <committer@example.com>)
+ Reflog message: branch: Created from HEAD
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:15:13 2005 -0700
+
+ 3
+ EOF
+ git log -1 -g other@{u}@{now} >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '@{reflog}-parsing does not look beyond colon' '
+ echo content >@{yesterday} &&
+ git add @{yesterday} &&
+ git commit -m "funny reflog file" &&
+ git hash-object @{yesterday} >expect &&
+ git rev-parse HEAD:@{yesterday} >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '@{upstream}-parsing does not look beyond colon' '
+ echo content >@{upstream} &&
+ git add @{upstream} &&
+ git commit -m "funny upstream file" &&
+ git hash-object @{upstream} >expect &&
+ git rev-parse HEAD:@{upstream} >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t1508-at-combinations.sh b/t/t1508-at-combinations.sh
new file mode 100755
index 0000000..e841309
--- /dev/null
+++ b/t/t1508-at-combinations.sh
@@ -0,0 +1,119 @@
+#!/bin/sh
+
+test_description='test various @{X} syntax combinations together'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+check() {
+ test_expect_${4:-success} "$1 = $3" "
+ echo '$3' >expect &&
+ if test '$2' = 'commit'
+ then
+ git log -1 --format=%s '$1' >actual
+ elif test '$2' = 'ref'
+ then
+ git rev-parse --symbolic-full-name '$1' >actual
+ else
+ git cat-file -p '$1' >actual
+ fi &&
+ test_cmp expect actual
+ "
+}
+
+nonsense() {
+ test_expect_${2:-success} "$1 is nonsensical" "
+ test_must_fail git rev-parse --verify '$1'
+ "
+}
+
+fail() {
+ "$@" failure
+}
+
+test_expect_success 'setup' '
+ test_commit main-one &&
+ test_commit main-two &&
+ git checkout -b upstream-branch &&
+ test_commit upstream-one &&
+ test_commit upstream-two &&
+ if test_have_prereq !MINGW
+ then
+ git checkout -b @/at-test
+ fi &&
+ git checkout -b @@/at-test &&
+ git checkout -b @at-test &&
+ git checkout -b old-branch &&
+ test_commit old-one &&
+ test_commit old-two &&
+ git checkout -b new-branch &&
+ test_commit new-one &&
+ test_commit new-two &&
+ git branch -u main old-branch &&
+ git branch -u upstream-branch new-branch
+'
+
+check HEAD ref refs/heads/new-branch
+check "@{1}" commit new-one
+check "HEAD@{1}" commit new-one
+check "@{now}" commit new-two
+check "HEAD@{now}" commit new-two
+check "@{-1}" ref refs/heads/old-branch
+check "@{-1}@{0}" commit old-two
+check "@{-1}@{1}" commit old-one
+check "@{u}" ref refs/heads/upstream-branch
+check "HEAD@{u}" ref refs/heads/upstream-branch
+check "@{u}@{1}" commit upstream-one
+check "@{-1}@{u}" ref refs/heads/main
+check "@{-1}@{u}@{1}" commit main-one
+check "@" commit new-two
+check "@@{u}" ref refs/heads/upstream-branch
+check "@@/at-test" ref refs/heads/@@/at-test
+test_have_prereq MINGW ||
+check "@/at-test" ref refs/heads/@/at-test
+check "@at-test" ref refs/heads/@at-test
+nonsense "@{u}@{-1}"
+nonsense "@{0}@{0}"
+nonsense "@{1}@{u}"
+nonsense "HEAD@{-1}"
+nonsense "@{-1}@{-1}"
+
+# @{N} versus HEAD@{N}
+
+check "HEAD@{3}" commit old-two
+nonsense "@{3}"
+
+test_expect_success 'switch to old-branch' '
+ git checkout old-branch
+'
+
+check HEAD ref refs/heads/old-branch
+check "HEAD@{1}" commit new-two
+check "@{1}" commit old-one
+
+test_expect_success 'create path with @' '
+ echo content >normal &&
+ echo content >fun@ny &&
+ git add normal fun@ny &&
+ git commit -m "funny path"
+'
+
+check "@:normal" blob content
+check "@:fun@ny" blob content
+
+test_expect_success '@{1} works with only one reflog entry' '
+ git checkout -B newbranch main &&
+ git reflog expire --expire=now refs/heads/newbranch &&
+ git commit --allow-empty -m "first after expiration" &&
+ test_cmp_rev newbranch~ newbranch@{1}
+'
+
+test_expect_success '@{0} works with empty reflog' '
+ git checkout -B newbranch main &&
+ git reflog expire --expire=now refs/heads/newbranch &&
+ test_cmp_rev newbranch newbranch@{0}
+'
+
+test_done
diff --git a/t/t1509-root-work-tree.sh b/t/t1509-root-work-tree.sh
new file mode 100755
index 0000000..c799f5b
--- /dev/null
+++ b/t/t1509-root-work-tree.sh
@@ -0,0 +1,264 @@
+#!/bin/sh
+
+test_description='Test Git when git repository is located at root
+
+This test requires write access in root. Do not bother if you do not
+have a throwaway chroot or VM.
+
+Script t1509/prepare-chroot.sh may help you setup chroot, then you
+can chroot in and execute this test from there.
+'
+
+. ./test-lib.sh
+
+test_cmp_val() {
+ echo "$1" > expected
+ echo "$2" > result
+ test_cmp expected result
+}
+
+test_vars() {
+ test_expect_success "$1: gitdir" '
+ test_cmp_val "'"$2"'" "$(git rev-parse --git-dir)"
+ '
+
+ test_expect_success "$1: worktree" '
+ test_cmp_val "'"$3"'" "$(git rev-parse --show-toplevel)"
+ '
+
+ test_expect_success "$1: prefix" '
+ test_cmp_val "'"$4"'" "$(git rev-parse --show-prefix)"
+ '
+}
+
+test_foobar_root() {
+ test_expect_success 'add relative' '
+ test -z "$(cd / && git ls-files)" &&
+ git add foo/foome &&
+ git add foo/bar/barme &&
+ git add me &&
+ ( cd / && git ls-files --stage ) > result &&
+ test_cmp /ls.expected result &&
+ rm "$(git rev-parse --git-dir)/index"
+ '
+
+ test_expect_success 'add absolute' '
+ test -z "$(cd / && git ls-files)" &&
+ git add /foo/foome &&
+ git add /foo/bar/barme &&
+ git add /me &&
+ ( cd / && git ls-files --stage ) > result &&
+ test_cmp /ls.expected result &&
+ rm "$(git rev-parse --git-dir)/index"
+ '
+
+}
+
+test_foobar_foo() {
+ test_expect_success 'add relative' '
+ test -z "$(cd / && git ls-files)" &&
+ git add foome &&
+ git add bar/barme &&
+ git add ../me &&
+ ( cd / && git ls-files --stage ) > result &&
+ test_cmp /ls.expected result &&
+ rm "$(git rev-parse --git-dir)/index"
+ '
+
+ test_expect_success 'add absolute' '
+ test -z "$(cd / && git ls-files)" &&
+ git add /foo/foome &&
+ git add /foo/bar/barme &&
+ git add /me &&
+ ( cd / && git ls-files --stage ) > result &&
+ test_cmp /ls.expected result &&
+ rm "$(git rev-parse --git-dir)/index"
+ '
+}
+
+test_foobar_foobar() {
+ test_expect_success 'add relative' '
+ test -z "$(cd / && git ls-files)" &&
+ git add ../foome &&
+ git add barme &&
+ git add ../../me &&
+ ( cd / && git ls-files --stage ) > result &&
+ test_cmp /ls.expected result &&
+ rm "$(git rev-parse --git-dir)/index"
+ '
+
+ test_expect_success 'add absolute' '
+ test -z "$(cd / && git ls-files)" &&
+ git add /foo/foome &&
+ git add /foo/bar/barme &&
+ git add /me &&
+ ( cd / && git ls-files --stage ) > result &&
+ test_cmp /ls.expected result &&
+ rm "$(git rev-parse --git-dir)/index"
+ '
+}
+
+if ! test -w /
+then
+ skip_all="Test requiring writable / skipped. Read this test if you want to run it"
+ test_done
+fi
+
+if test -e /refs || test -e /objects || test -e /info || test -e /hooks ||
+ test -e /.git || test -e /foo || test -e /me
+then
+ skip_all="Skip test that clobbers existing files in /"
+ test_done
+fi
+
+if [ "$IKNOWWHATIAMDOING" != "YES" ]; then
+ skip_all="You must set env var IKNOWWHATIAMDOING=YES in order to run this test"
+ test_done
+fi
+
+if ! test_have_prereq NOT_ROOT
+then
+ skip_all="No you can't run this as root"
+ test_done
+fi
+
+ONE_SHA1=d00491fd7e5bb6fa28c517a0bb32b8b506539d4d
+
+test_expect_success 'setup' '
+ rm -rf /foo &&
+ mkdir /foo &&
+ mkdir /foo/bar &&
+ echo 1 > /foo/foome &&
+ echo 1 > /foo/bar/barme &&
+ echo 1 > /me
+'
+
+say "GIT_DIR absolute, GIT_WORK_TREE set"
+
+test_expect_success 'go to /' 'cd /'
+
+cat >ls.expected <<EOF
+100644 $ONE_SHA1 0 foo/bar/barme
+100644 $ONE_SHA1 0 foo/foome
+100644 $ONE_SHA1 0 me
+EOF
+
+GIT_DIR="$TRASH_DIRECTORY/.git" && export GIT_DIR
+GIT_WORK_TREE=/ && export GIT_WORK_TREE
+
+test_vars 'abs gitdir, root' "$GIT_DIR" "/" ""
+test_foobar_root
+
+test_expect_success 'go to /foo' 'cd /foo'
+
+test_vars 'abs gitdir, foo' "$GIT_DIR" "/" "foo/"
+test_foobar_foo
+
+test_expect_success 'go to /foo/bar' 'cd /foo/bar'
+
+test_vars 'abs gitdir, foo/bar' "$GIT_DIR" "/" "foo/bar/"
+test_foobar_foobar
+
+say "GIT_DIR relative, GIT_WORK_TREE set"
+
+test_expect_success 'go to /' 'cd /'
+
+GIT_DIR="$(echo $TRASH_DIRECTORY|sed 's,^/,,')/.git" && export GIT_DIR
+GIT_WORK_TREE=/ && export GIT_WORK_TREE
+
+test_vars 'rel gitdir, root' "$GIT_DIR" "/" ""
+test_foobar_root
+
+test_expect_success 'go to /foo' 'cd /foo'
+
+GIT_DIR="../$TRASH_DIRECTORY/.git" && export GIT_DIR
+GIT_WORK_TREE=/ && export GIT_WORK_TREE
+
+test_vars 'rel gitdir, foo' "$TRASH_DIRECTORY/.git" "/" "foo/"
+test_foobar_foo
+
+test_expect_success 'go to /foo/bar' 'cd /foo/bar'
+
+GIT_DIR="../../$TRASH_DIRECTORY/.git" && export GIT_DIR
+GIT_WORK_TREE=/ && export GIT_WORK_TREE
+
+test_vars 'rel gitdir, foo/bar' "$TRASH_DIRECTORY/.git" "/" "foo/bar/"
+test_foobar_foobar
+
+say "GIT_DIR relative, GIT_WORK_TREE relative"
+
+test_expect_success 'go to /' 'cd /'
+
+GIT_DIR="$(echo $TRASH_DIRECTORY|sed 's,^/,,')/.git" && export GIT_DIR
+GIT_WORK_TREE=. && export GIT_WORK_TREE
+
+test_vars 'rel gitdir, root' "$GIT_DIR" "/" ""
+test_foobar_root
+
+test_expect_success 'go to /' 'cd /foo'
+
+GIT_DIR="../$TRASH_DIRECTORY/.git" && export GIT_DIR
+GIT_WORK_TREE=.. && export GIT_WORK_TREE
+
+test_vars 'rel gitdir, foo' "$TRASH_DIRECTORY/.git" "/" "foo/"
+test_foobar_foo
+
+test_expect_success 'go to /foo/bar' 'cd /foo/bar'
+
+GIT_DIR="../../$TRASH_DIRECTORY/.git" && export GIT_DIR
+GIT_WORK_TREE=../.. && export GIT_WORK_TREE
+
+test_vars 'rel gitdir, foo/bar' "$TRASH_DIRECTORY/.git" "/" "foo/bar/"
+test_foobar_foobar
+
+say ".git at root"
+
+unset GIT_DIR
+unset GIT_WORK_TREE
+
+test_expect_success 'go to /' 'cd /'
+test_expect_success 'setup' '
+ rm -rf /.git &&
+ echo "Initialized empty Git repository in /.git/" > expected &&
+ git init > result &&
+ test_cmp expected result &&
+ git config --global --add safe.directory /
+'
+
+test_vars 'auto gitdir, root' ".git" "/" ""
+test_foobar_root
+
+test_expect_success 'go to /foo' 'cd /foo'
+test_vars 'auto gitdir, foo' "/.git" "/" "foo/"
+test_foobar_foo
+
+test_expect_success 'go to /foo/bar' 'cd /foo/bar'
+test_vars 'auto gitdir, foo/bar' "/.git" "/" "foo/bar/"
+test_foobar_foobar
+
+test_expect_success 'cleanup' 'rm -rf /.git'
+
+say "auto bare gitdir"
+
+# DESTROYYYYY!!!!!
+test_expect_success 'setup' '
+ rm -rf /refs /objects /info /hooks &&
+ rm -f /HEAD /expected /ls.expected /me /result &&
+ cd / &&
+ echo "Initialized empty Git repository in /" > expected &&
+ git init --bare > result &&
+ test_cmp expected result
+'
+
+test_vars 'auto gitdir, root' "." "" ""
+
+test_expect_success 'go to /foo' 'cd /foo'
+
+test_vars 'auto gitdir, root' "/" "" ""
+
+test_expect_success 'cleanup root' '
+ rm -rf /.git /refs /objects /info /hooks /branches /foo &&
+ rm -f /HEAD /config /description /expected /ls.expected /me /result
+'
+
+test_done
diff --git a/t/t1509/excludes b/t/t1509/excludes
new file mode 100644
index 0000000..d4d21d3
--- /dev/null
+++ b/t/t1509/excludes
@@ -0,0 +1,14 @@
+*.o
+*~
+*.bak
+*.c
+*.h
+.git
+contrib
+Documentation
+git-gui
+gitk-git
+gitweb
+t/t4013
+t/t5100
+t/t5515
diff --git a/t/t1509/prepare-chroot.sh b/t/t1509/prepare-chroot.sh
new file mode 100755
index 0000000..6d47e2c
--- /dev/null
+++ b/t/t1509/prepare-chroot.sh
@@ -0,0 +1,58 @@
+#!/bin/sh
+
+die() {
+ echo >&2 "$@"
+ exit 1
+}
+
+xmkdir() {
+ while [ -n "$1" ]; do
+ [ -d "$1" ] || mkdir "$1" || die "Unable to mkdir $1"
+ shift
+ done
+}
+
+R="$1"
+
+[ "$(id -u)" -eq 0 ] && die "This script should not be run as root, what if it does rm -rf /?"
+[ -n "$R" ] || die "usage: prepare-chroot.sh <root>"
+[ -x git ] || die "This script needs to be executed at git source code's top directory"
+if [ -x /bin/busybox ]; then
+ BB=/bin/busybox
+elif [ -x /usr/bin/busybox ]; then
+ BB=/usr/bin/busybox
+else
+ die "You need busybox"
+fi
+
+xmkdir "$R" "$R/bin" "$R/etc" "$R/lib" "$R/dev"
+touch "$R/dev/null"
+echo "root:x:0:0:root:/:/bin/sh" > "$R/etc/passwd"
+echo "$(id -nu):x:$(id -u):$(id -g)::$(pwd)/t:/bin/sh" >> "$R/etc/passwd"
+echo "root::0:root" > "$R/etc/group"
+echo "$(id -ng)::$(id -g):$(id -nu)" >> "$R/etc/group"
+
+[ -x "$R$BB" ] || cp $BB "$R/bin/busybox"
+for cmd in sh su ls expr tr basename rm mkdir mv id uname dirname cat true sed diff; do
+ ln -f -s /bin/busybox "$R/bin/$cmd"
+done
+
+mkdir -p "$R$(pwd)"
+rsync --exclude-from t/t1509/excludes -Ha . "$R$(pwd)"
+# Fake perl to reduce dependency, t1509 does not use perl, but some
+# env might slip through, see test-lib.sh, unset.*PERL_PATH
+sed 's|^PERL_PATH=.*|PERL_PATH=/bin/true|' GIT-BUILD-OPTIONS > "$R$(pwd)/GIT-BUILD-OPTIONS"
+for cmd in git $BB;do
+ ldd $cmd | grep '/' | sed 's,.*\s\(/[^ ]*\).*,\1,' | while read i; do
+ mkdir -p "$R$(dirname $i)"
+ cp "$i" "$R/$i"
+ done
+done
+cat <<EOF
+All is set up in $R, execute t1509 with the following commands:
+
+sudo chroot $R /bin/su - $(id -nu)
+IKNOWWHATIAMDOING=YES ./t1509-root-worktree.sh -v -i
+
+When you are done, simply delete $R to clean up
+EOF
diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh
new file mode 100755
index 0000000..591505a
--- /dev/null
+++ b/t/t1510-repo-setup.sh
@@ -0,0 +1,806 @@
+#!/bin/sh
+
+test_description="Tests of cwd/prefix/worktree/gitdir setup in all cases
+
+A few rules for repo setup:
+
+1. GIT_DIR is relative to user's cwd. --git-dir is equivalent to
+ GIT_DIR.
+
+2. .git file is relative to parent directory. .git file is basically
+ symlink in disguise. The directory where .git file points to will
+ become new git_dir.
+
+3. core.worktree is relative to git_dir.
+
+4. GIT_WORK_TREE is relative to user's cwd. --work-tree is
+ equivalent to GIT_WORK_TREE.
+
+5. GIT_WORK_TREE/core.worktree was originally meant to work only if
+ GIT_DIR is set, but earlier git didn't enforce it, and some scripts
+ depend on the implementation that happened to first discover .git by
+ going up from the users $cwd and then using the specified working tree
+ that may or may not have any relation to where .git was found in. This
+ historical behaviour must be kept.
+
+6. Effective GIT_WORK_TREE overrides core.worktree and core.bare
+
+7. Effective core.worktree conflicts with core.bare
+
+8. If GIT_DIR is set but neither worktree nor bare setting is given,
+ original cwd becomes worktree.
+
+9. If .git discovery is done inside a repo, the repo becomes a bare
+ repo. .git discovery is performed if GIT_DIR is not set.
+
+10. If no worktree is available, cwd remains unchanged, prefix is
+ NULL.
+
+11. When user's cwd is outside worktree, cwd remains unchanged,
+ prefix is NULL.
+"
+
+# This test heavily relies on the standard error of nested function calls.
+test_untraceable=UnfortunatelyYes
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+here=$(pwd)
+
+test_repo () {
+ (
+ cd "$1" &&
+ if test -n "$2"
+ then
+ GIT_DIR="$2" &&
+ export GIT_DIR
+ fi &&
+ if test -n "$3"
+ then
+ GIT_WORK_TREE="$3" &&
+ export GIT_WORK_TREE
+ fi &&
+ rm -f trace &&
+ GIT_TRACE_SETUP="$(pwd)/trace" git symbolic-ref HEAD >/dev/null &&
+ grep '^setup: ' trace >result &&
+ test_cmp expected result
+ )
+}
+
+maybe_config () {
+ file=$1 var=$2 value=$3 &&
+ if test "$value" != unset
+ then
+ git config --file="$file" "$var" "$value"
+ fi
+}
+
+setup_repo () {
+ name=$1 worktreecfg=$2 gitfile=$3 barecfg=$4 &&
+ sane_unset GIT_DIR GIT_WORK_TREE &&
+
+ git -c init.defaultBranch=initial init "$name" &&
+ maybe_config "$name/.git/config" core.worktree "$worktreecfg" &&
+ maybe_config "$name/.git/config" core.bare "$barecfg" &&
+ mkdir -p "$name/sub/sub" &&
+
+ if test "${gitfile:+set}"
+ then
+ mv "$name/.git" "$name.git" &&
+ echo "gitdir: ../$name.git" >"$name/.git"
+ fi
+}
+
+maybe_set () {
+ var=$1 value=$2 &&
+ if test "$value" != unset
+ then
+ eval "$var=\$value" &&
+ export $var
+ fi
+}
+
+setup_env () {
+ worktreenv=$1 gitdirenv=$2 &&
+ sane_unset GIT_DIR GIT_WORK_TREE &&
+ maybe_set GIT_DIR "$gitdirenv" &&
+ maybe_set GIT_WORK_TREE "$worktreeenv"
+}
+
+expect () {
+ cat >"$1/expected" <<-EOF
+ setup: git_dir: $2
+ setup: git_common_dir: $2
+ setup: worktree: $3
+ setup: cwd: $4
+ setup: prefix: $5
+ EOF
+}
+
+try_case () {
+ name=$1 worktreeenv=$2 gitdirenv=$3 &&
+ setup_env "$worktreeenv" "$gitdirenv" &&
+ expect "$name" "$4" "$5" "$6" "$7" &&
+ test_repo "$name"
+}
+
+run_wt_tests () {
+ N=$1 gitfile=$2
+
+ absgit="$here/$N/.git"
+ dotgit=.git
+ dotdotgit=../../.git
+
+ if test "$gitfile"
+ then
+ absgit="$here/$N.git"
+ dotgit=$absgit dotdotgit=$absgit
+ fi
+
+ test_expect_success "#$N: explicit GIT_WORK_TREE and GIT_DIR at toplevel" '
+ try_case $N "$here/$N" .git \
+ "$dotgit" "$here/$N" "$here/$N" "(null)" &&
+ try_case $N . .git \
+ "$dotgit" "$here/$N" "$here/$N" "(null)" &&
+ try_case $N "$here/$N" "$here/$N/.git" \
+ "$absgit" "$here/$N" "$here/$N" "(null)" &&
+ try_case $N . "$here/$N/.git" \
+ "$absgit" "$here/$N" "$here/$N" "(null)"
+ '
+
+ test_expect_success "#$N: explicit GIT_WORK_TREE and GIT_DIR in subdir" '
+ try_case $N/sub/sub "$here/$N" ../../.git \
+ "$absgit" "$here/$N" "$here/$N" sub/sub/ &&
+ try_case $N/sub/sub ../.. ../../.git \
+ "$absgit" "$here/$N" "$here/$N" sub/sub/ &&
+ try_case $N/sub/sub "$here/$N" "$here/$N/.git" \
+ "$absgit" "$here/$N" "$here/$N" sub/sub/ &&
+ try_case $N/sub/sub ../.. "$here/$N/.git" \
+ "$absgit" "$here/$N" "$here/$N" sub/sub/
+ '
+
+ test_expect_success "#$N: explicit GIT_WORK_TREE from parent of worktree" '
+ try_case $N "$here/$N/wt" .git \
+ "$dotgit" "$here/$N/wt" "$here/$N" "(null)" &&
+ try_case $N wt .git \
+ "$dotgit" "$here/$N/wt" "$here/$N" "(null)" &&
+ try_case $N wt "$here/$N/.git" \
+ "$absgit" "$here/$N/wt" "$here/$N" "(null)" &&
+ try_case $N "$here/$N/wt" "$here/$N/.git" \
+ "$absgit" "$here/$N/wt" "$here/$N" "(null)"
+ '
+
+ test_expect_success "#$N: explicit GIT_WORK_TREE from nephew of worktree" '
+ try_case $N/sub/sub "$here/$N/wt" ../../.git \
+ "$dotdotgit" "$here/$N/wt" "$here/$N/sub/sub" "(null)" &&
+ try_case $N/sub/sub ../../wt ../../.git \
+ "$dotdotgit" "$here/$N/wt" "$here/$N/sub/sub" "(null)" &&
+ try_case $N/sub/sub ../../wt "$here/$N/.git" \
+ "$absgit" "$here/$N/wt" "$here/$N/sub/sub" "(null)" &&
+ try_case $N/sub/sub "$here/$N/wt" "$here/$N/.git" \
+ "$absgit" "$here/$N/wt" "$here/$N/sub/sub" "(null)"
+ '
+
+ test_expect_success "#$N: chdir_to_toplevel uses worktree, not git dir" '
+ try_case $N "$here" .git \
+ "$absgit" "$here" "$here" $N/ &&
+ try_case $N .. .git \
+ "$absgit" "$here" "$here" $N/ &&
+ try_case $N .. "$here/$N/.git" \
+ "$absgit" "$here" "$here" $N/ &&
+ try_case $N "$here" "$here/$N/.git" \
+ "$absgit" "$here" "$here" $N/
+ '
+
+ test_expect_success "#$N: chdir_to_toplevel uses worktree (from subdir)" '
+ try_case $N/sub/sub "$here" ../../.git \
+ "$absgit" "$here" "$here" $N/sub/sub/ &&
+ try_case $N/sub/sub ../../.. ../../.git \
+ "$absgit" "$here" "$here" $N/sub/sub/ &&
+ try_case $N/sub/sub ../../../ "$here/$N/.git" \
+ "$absgit" "$here" "$here" $N/sub/sub/ &&
+ try_case $N/sub/sub "$here" "$here/$N/.git" \
+ "$absgit" "$here" "$here" $N/sub/sub/
+ '
+}
+
+# try_repo #c GIT_WORK_TREE GIT_DIR core.worktree .gitfile? core.bare \
+# (git dir) (work tree) (cwd) (prefix) \ <-- at toplevel
+# (git dir) (work tree) (cwd) (prefix) <-- from subdir
+try_repo () {
+ name=$1 worktreeenv=$2 gitdirenv=$3 &&
+ setup_repo "$name" "$4" "$5" "$6" &&
+ shift 6 &&
+ try_case "$name" "$worktreeenv" "$gitdirenv" \
+ "$1" "$2" "$3" "$4" &&
+ shift 4 &&
+ case "$gitdirenv" in
+ /* | ?:/* | unset) ;;
+ *)
+ gitdirenv=../$gitdirenv ;;
+ esac &&
+ try_case "$name/sub" "$worktreeenv" "$gitdirenv" \
+ "$1" "$2" "$3" "$4"
+}
+
+# Bit 0 = GIT_WORK_TREE
+# Bit 1 = GIT_DIR
+# Bit 2 = core.worktree
+# Bit 3 = .git is a file
+# Bit 4 = bare repo
+# Case# = encoding of the above 5 bits
+
+test_expect_success '#0: nonbare repo, no explicit configuration' '
+ try_repo 0 unset unset unset "" unset \
+ .git "$here/0" "$here/0" "(null)" \
+ .git "$here/0" "$here/0" sub/ 2>message &&
+ test_must_be_empty message
+'
+
+test_expect_success '#1: GIT_WORK_TREE without explicit GIT_DIR is accepted' '
+ try_repo 1 "$here" unset unset "" unset \
+ "$here/1/.git" "$here" "$here" 1/ \
+ "$here/1/.git" "$here" "$here" 1/sub/ 2>message &&
+ test_must_be_empty message
+'
+
+test_expect_success '#2: worktree defaults to cwd with explicit GIT_DIR' '
+ try_repo 2 unset "$here/2/.git" unset "" unset \
+ "$here/2/.git" "$here/2" "$here/2" "(null)" \
+ "$here/2/.git" "$here/2/sub" "$here/2/sub" "(null)"
+'
+
+test_expect_success '#2b: relative GIT_DIR' '
+ try_repo 2b unset ".git" unset "" unset \
+ ".git" "$here/2b" "$here/2b" "(null)" \
+ "../.git" "$here/2b/sub" "$here/2b/sub" "(null)"
+'
+
+test_expect_success '#3: setup' '
+ setup_repo 3 unset "" unset &&
+ mkdir -p 3/sub/sub 3/wt/sub
+'
+run_wt_tests 3
+
+test_expect_success '#4: core.worktree without GIT_DIR set is accepted' '
+ setup_repo 4 ../sub "" unset &&
+ mkdir -p 4/sub sub &&
+ try_case 4 unset unset \
+ .git "$here/4/sub" "$here/4" "(null)" \
+ "$here/4/.git" "$here/4/sub" "$here/4/sub" "(null)" 2>message &&
+ test_must_be_empty message
+'
+
+test_expect_success '#5: core.worktree + GIT_WORK_TREE is accepted' '
+ # or: you cannot intimidate away the lack of GIT_DIR setting
+ try_repo 5 "$here" unset "$here/5" "" unset \
+ "$here/5/.git" "$here" "$here" 5/ \
+ "$here/5/.git" "$here" "$here" 5/sub/ 2>message &&
+ try_repo 5a .. unset "$here/5a" "" unset \
+ "$here/5a/.git" "$here" "$here" 5a/ \
+ "$here/5a/.git" "$here/5a" "$here/5a" sub/ &&
+ test_must_be_empty message
+'
+
+test_expect_success '#6: setting GIT_DIR brings core.worktree to life' '
+ setup_repo 6 "$here/6" "" unset &&
+ try_case 6 unset .git \
+ .git "$here/6" "$here/6" "(null)" &&
+ try_case 6 unset "$here/6/.git" \
+ "$here/6/.git" "$here/6" "$here/6" "(null)" &&
+ try_case 6/sub/sub unset ../../.git \
+ "$here/6/.git" "$here/6" "$here/6" sub/sub/ &&
+ try_case 6/sub/sub unset "$here/6/.git" \
+ "$here/6/.git" "$here/6" "$here/6" sub/sub/
+'
+
+test_expect_success '#6b: GIT_DIR set, core.worktree relative' '
+ setup_repo 6b .. "" unset &&
+ try_case 6b unset .git \
+ .git "$here/6b" "$here/6b" "(null)" &&
+ try_case 6b unset "$here/6b/.git" \
+ "$here/6b/.git" "$here/6b" "$here/6b" "(null)" &&
+ try_case 6b/sub/sub unset ../../.git \
+ "$here/6b/.git" "$here/6b" "$here/6b" sub/sub/ &&
+ try_case 6b/sub/sub unset "$here/6b/.git" \
+ "$here/6b/.git" "$here/6b" "$here/6b" sub/sub/
+'
+
+test_expect_success '#6c: GIT_DIR set, core.worktree=../wt (absolute)' '
+ setup_repo 6c "$here/6c/wt" "" unset &&
+ mkdir -p 6c/wt/sub &&
+
+ try_case 6c unset .git \
+ .git "$here/6c/wt" "$here/6c" "(null)" &&
+ try_case 6c unset "$here/6c/.git" \
+ "$here/6c/.git" "$here/6c/wt" "$here/6c" "(null)" &&
+ try_case 6c/sub/sub unset ../../.git \
+ ../../.git "$here/6c/wt" "$here/6c/sub/sub" "(null)" &&
+ try_case 6c/sub/sub unset "$here/6c/.git" \
+ "$here/6c/.git" "$here/6c/wt" "$here/6c/sub/sub" "(null)"
+'
+
+test_expect_success '#6d: GIT_DIR set, core.worktree=../wt (relative)' '
+ setup_repo 6d "$here/6d/wt" "" unset &&
+ mkdir -p 6d/wt/sub &&
+
+ try_case 6d unset .git \
+ .git "$here/6d/wt" "$here/6d" "(null)" &&
+ try_case 6d unset "$here/6d/.git" \
+ "$here/6d/.git" "$here/6d/wt" "$here/6d" "(null)" &&
+ try_case 6d/sub/sub unset ../../.git \
+ ../../.git "$here/6d/wt" "$here/6d/sub/sub" "(null)" &&
+ try_case 6d/sub/sub unset "$here/6d/.git" \
+ "$here/6d/.git" "$here/6d/wt" "$here/6d/sub/sub" "(null)"
+'
+
+test_expect_success '#6e: GIT_DIR set, core.worktree=../.. (absolute)' '
+ setup_repo 6e "$here" "" unset &&
+ try_case 6e unset .git \
+ "$here/6e/.git" "$here" "$here" 6e/ &&
+ try_case 6e unset "$here/6e/.git" \
+ "$here/6e/.git" "$here" "$here" 6e/ &&
+ try_case 6e/sub/sub unset ../../.git \
+ "$here/6e/.git" "$here" "$here" 6e/sub/sub/ &&
+ try_case 6e/sub/sub unset "$here/6e/.git" \
+ "$here/6e/.git" "$here" "$here" 6e/sub/sub/
+'
+
+test_expect_success '#6f: GIT_DIR set, core.worktree=../.. (relative)' '
+ setup_repo 6f ../../ "" unset &&
+ try_case 6f unset .git \
+ "$here/6f/.git" "$here" "$here" 6f/ &&
+ try_case 6f unset "$here/6f/.git" \
+ "$here/6f/.git" "$here" "$here" 6f/ &&
+ try_case 6f/sub/sub unset ../../.git \
+ "$here/6f/.git" "$here" "$here" 6f/sub/sub/ &&
+ try_case 6f/sub/sub unset "$here/6f/.git" \
+ "$here/6f/.git" "$here" "$here" 6f/sub/sub/
+'
+
+# case #7: GIT_WORK_TREE overrides core.worktree.
+test_expect_success '#7: setup' '
+ setup_repo 7 non-existent "" unset &&
+ mkdir -p 7/sub/sub 7/wt/sub
+'
+run_wt_tests 7
+
+test_expect_success '#8: gitfile, easy case' '
+ try_repo 8 unset unset unset gitfile unset \
+ "$here/8.git" "$here/8" "$here/8" "(null)" \
+ "$here/8.git" "$here/8" "$here/8" sub/
+'
+
+test_expect_success '#9: GIT_WORK_TREE accepted with gitfile' '
+ mkdir -p 9/wt &&
+ try_repo 9 wt unset unset gitfile unset \
+ "$here/9.git" "$here/9/wt" "$here/9" "(null)" \
+ "$here/9.git" "$here/9/sub/wt" "$here/9/sub" "(null)" 2>message &&
+ test_must_be_empty message
+'
+
+test_expect_success '#10: GIT_DIR can point to gitfile' '
+ try_repo 10 unset "$here/10/.git" unset gitfile unset \
+ "$here/10.git" "$here/10" "$here/10" "(null)" \
+ "$here/10.git" "$here/10/sub" "$here/10/sub" "(null)"
+'
+
+test_expect_success '#10b: relative GIT_DIR can point to gitfile' '
+ try_repo 10b unset .git unset gitfile unset \
+ "$here/10b.git" "$here/10b" "$here/10b" "(null)" \
+ "$here/10b.git" "$here/10b/sub" "$here/10b/sub" "(null)"
+'
+
+# case #11: GIT_WORK_TREE works, gitfile case.
+test_expect_success '#11: setup' '
+ setup_repo 11 unset gitfile unset &&
+ mkdir -p 11/sub/sub 11/wt/sub
+'
+run_wt_tests 11 gitfile
+
+test_expect_success '#12: core.worktree with gitfile is accepted' '
+ try_repo 12 unset unset "$here/12" gitfile unset \
+ "$here/12.git" "$here/12" "$here/12" "(null)" \
+ "$here/12.git" "$here/12" "$here/12" sub/ 2>message &&
+ test_must_be_empty message
+'
+
+test_expect_success '#13: core.worktree+GIT_WORK_TREE accepted (with gitfile)' '
+ # or: you cannot intimidate away the lack of GIT_DIR setting
+ try_repo 13 non-existent-too unset non-existent gitfile unset \
+ "$here/13.git" "$here/13/non-existent-too" "$here/13" "(null)" \
+ "$here/13.git" "$here/13/sub/non-existent-too" "$here/13/sub" "(null)" 2>message &&
+ test_must_be_empty message
+'
+
+# case #14.
+# If this were more table-driven, it could share code with case #6.
+
+test_expect_success '#14: core.worktree with GIT_DIR pointing to gitfile' '
+ setup_repo 14 "$here/14" gitfile unset &&
+ try_case 14 unset .git \
+ "$here/14.git" "$here/14" "$here/14" "(null)" &&
+ try_case 14 unset "$here/14/.git" \
+ "$here/14.git" "$here/14" "$here/14" "(null)" &&
+ try_case 14/sub/sub unset ../../.git \
+ "$here/14.git" "$here/14" "$here/14" sub/sub/ &&
+ try_case 14/sub/sub unset "$here/14/.git" \
+ "$here/14.git" "$here/14" "$here/14" sub/sub/ &&
+
+ setup_repo 14c "$here/14c/wt" gitfile unset &&
+ mkdir -p 14c/wt/sub &&
+
+ try_case 14c unset .git \
+ "$here/14c.git" "$here/14c/wt" "$here/14c" "(null)" &&
+ try_case 14c unset "$here/14c/.git" \
+ "$here/14c.git" "$here/14c/wt" "$here/14c" "(null)" &&
+ try_case 14c/sub/sub unset ../../.git \
+ "$here/14c.git" "$here/14c/wt" "$here/14c/sub/sub" "(null)" &&
+ try_case 14c/sub/sub unset "$here/14c/.git" \
+ "$here/14c.git" "$here/14c/wt" "$here/14c/sub/sub" "(null)" &&
+
+ setup_repo 14d "$here/14d/wt" gitfile unset &&
+ mkdir -p 14d/wt/sub &&
+
+ try_case 14d unset .git \
+ "$here/14d.git" "$here/14d/wt" "$here/14d" "(null)" &&
+ try_case 14d unset "$here/14d/.git" \
+ "$here/14d.git" "$here/14d/wt" "$here/14d" "(null)" &&
+ try_case 14d/sub/sub unset ../../.git \
+ "$here/14d.git" "$here/14d/wt" "$here/14d/sub/sub" "(null)" &&
+ try_case 14d/sub/sub unset "$here/14d/.git" \
+ "$here/14d.git" "$here/14d/wt" "$here/14d/sub/sub" "(null)" &&
+
+ setup_repo 14e "$here" gitfile unset &&
+ try_case 14e unset .git \
+ "$here/14e.git" "$here" "$here" 14e/ &&
+ try_case 14e unset "$here/14e/.git" \
+ "$here/14e.git" "$here" "$here" 14e/ &&
+ try_case 14e/sub/sub unset ../../.git \
+ "$here/14e.git" "$here" "$here" 14e/sub/sub/ &&
+ try_case 14e/sub/sub unset "$here/14e/.git" \
+ "$here/14e.git" "$here" "$here" 14e/sub/sub/
+'
+
+test_expect_success '#14b: core.worktree is relative to actual git dir' '
+ setup_repo 14b ../14b gitfile unset &&
+ try_case 14b unset .git \
+ "$here/14b.git" "$here/14b" "$here/14b" "(null)" &&
+ try_case 14b unset "$here/14b/.git" \
+ "$here/14b.git" "$here/14b" "$here/14b" "(null)" &&
+ try_case 14b/sub/sub unset ../../.git \
+ "$here/14b.git" "$here/14b" "$here/14b" sub/sub/ &&
+ try_case 14b/sub/sub unset "$here/14b/.git" \
+ "$here/14b.git" "$here/14b" "$here/14b" sub/sub/ &&
+
+ setup_repo 14f ../ gitfile unset &&
+ try_case 14f unset .git \
+ "$here/14f.git" "$here" "$here" 14f/ &&
+ try_case 14f unset "$here/14f/.git" \
+ "$here/14f.git" "$here" "$here" 14f/ &&
+ try_case 14f/sub/sub unset ../../.git \
+ "$here/14f.git" "$here" "$here" 14f/sub/sub/ &&
+ try_case 14f/sub/sub unset "$here/14f/.git" \
+ "$here/14f.git" "$here" "$here" 14f/sub/sub/
+'
+
+# case #15: GIT_WORK_TREE overrides core.worktree (gitfile case).
+test_expect_success '#15: setup' '
+ setup_repo 15 non-existent gitfile unset &&
+ mkdir -p 15/sub/sub 15/wt/sub
+'
+run_wt_tests 15 gitfile
+
+test_expect_success '#16a: implicitly bare repo (cwd inside .git dir)' '
+ setup_repo 16a unset "" unset &&
+ mkdir -p 16a/.git/wt/sub &&
+
+ try_case 16a/.git unset unset \
+ . "(null)" "$here/16a/.git" "(null)" &&
+ try_case 16a/.git/wt unset unset \
+ "$here/16a/.git" "(null)" "$here/16a/.git/wt" "(null)" &&
+ try_case 16a/.git/wt/sub unset unset \
+ "$here/16a/.git" "(null)" "$here/16a/.git/wt/sub" "(null)"
+'
+
+test_expect_success '#16b: bare .git (cwd inside .git dir)' '
+ setup_repo 16b unset "" true &&
+ mkdir -p 16b/.git/wt/sub &&
+
+ try_case 16b/.git unset unset \
+ . "(null)" "$here/16b/.git" "(null)" &&
+ try_case 16b/.git/wt unset unset \
+ "$here/16b/.git" "(null)" "$here/16b/.git/wt" "(null)" &&
+ try_case 16b/.git/wt/sub unset unset \
+ "$here/16b/.git" "(null)" "$here/16b/.git/wt/sub" "(null)"
+'
+
+test_expect_success '#16c: bare .git has no worktree' '
+ try_repo 16c unset unset unset "" true \
+ .git "(null)" "$here/16c" "(null)" \
+ "$here/16c/.git" "(null)" "$here/16c/sub" "(null)"
+'
+
+test_expect_success '#16d: bareness preserved across alias' '
+ setup_repo 16d unset "" unset &&
+ (
+ cd 16d/.git &&
+ test_must_fail git status &&
+ git config alias.st status &&
+ test_must_fail git st
+ )
+'
+
+test_expect_success '#16e: bareness preserved by --bare' '
+ setup_repo 16e unset "" unset &&
+ (
+ cd 16e/.git &&
+ test_must_fail git status &&
+ test_must_fail git --bare status
+ )
+'
+
+test_expect_success '#17: GIT_WORK_TREE without explicit GIT_DIR is accepted (bare case)' '
+ # Just like #16.
+ setup_repo 17a unset "" true &&
+ setup_repo 17b unset "" true &&
+ mkdir -p 17a/.git/wt/sub &&
+ mkdir -p 17b/.git/wt/sub &&
+
+ try_case 17a/.git "$here/17a" unset \
+ "$here/17a/.git" "$here/17a" "$here/17a" .git/ \
+ 2>message &&
+ try_case 17a/.git/wt "$here/17a" unset \
+ "$here/17a/.git" "$here/17a" "$here/17a" .git/wt/ &&
+ try_case 17a/.git/wt/sub "$here/17a" unset \
+ "$here/17a/.git" "$here/17a" "$here/17a" .git/wt/sub/ &&
+
+ try_case 17b/.git "$here/17b" unset \
+ "$here/17b/.git" "$here/17b" "$here/17b" .git/ &&
+ try_case 17b/.git/wt "$here/17b" unset \
+ "$here/17b/.git" "$here/17b" "$here/17b" .git/wt/ &&
+ try_case 17b/.git/wt/sub "$here/17b" unset \
+ "$here/17b/.git" "$here/17b" "$here/17b" .git/wt/sub/ &&
+
+ try_repo 17c "$here/17c" unset unset "" true \
+ .git "$here/17c" "$here/17c" "(null)" \
+ "$here/17c/.git" "$here/17c" "$here/17c" sub/ 2>message &&
+ test_must_be_empty message
+'
+
+test_expect_success '#18: bare .git named by GIT_DIR has no worktree' '
+ try_repo 18 unset .git unset "" true \
+ .git "(null)" "$here/18" "(null)" \
+ ../.git "(null)" "$here/18/sub" "(null)" &&
+ try_repo 18b unset "$here/18b/.git" unset "" true \
+ "$here/18b/.git" "(null)" "$here/18b" "(null)" \
+ "$here/18b/.git" "(null)" "$here/18b/sub" "(null)"
+'
+
+# Case #19: GIT_DIR + GIT_WORK_TREE suppresses bareness.
+test_expect_success '#19: setup' '
+ setup_repo 19 unset "" true &&
+ mkdir -p 19/sub/sub 19/wt/sub
+'
+run_wt_tests 19
+
+test_expect_success '#20a: core.worktree without GIT_DIR accepted (inside .git)' '
+ # Unlike case #16a.
+ setup_repo 20a "$here/20a" "" unset &&
+ mkdir -p 20a/.git/wt/sub &&
+ try_case 20a/.git unset unset \
+ "$here/20a/.git" "$here/20a" "$here/20a" .git/ 2>message &&
+ try_case 20a/.git/wt unset unset \
+ "$here/20a/.git" "$here/20a" "$here/20a" .git/wt/ &&
+ try_case 20a/.git/wt/sub unset unset \
+ "$here/20a/.git" "$here/20a" "$here/20a" .git/wt/sub/ &&
+ test_must_be_empty message
+'
+
+test_expect_success '#20b/c: core.worktree and core.bare conflict' '
+ setup_repo 20b non-existent "" true &&
+ mkdir -p 20b/.git/wt/sub &&
+ (
+ cd 20b/.git &&
+ test_must_fail git status >/dev/null
+ ) 2>message &&
+ grep "core.bare and core.worktree" message
+'
+
+test_expect_success '#20d: core.worktree and core.bare OK when working tree not needed' '
+ setup_repo 20d non-existent "" true &&
+ mkdir -p 20d/.git/wt/sub &&
+ (
+ cd 20d/.git &&
+ git config foo.bar value
+ )
+'
+
+# Case #21: core.worktree/GIT_WORK_TREE overrides core.bare' '
+test_expect_success '#21: setup, core.worktree warns before overriding core.bare' '
+ setup_repo 21 non-existent "" unset &&
+ mkdir -p 21/.git/wt/sub &&
+ (
+ cd 21/.git &&
+ GIT_WORK_TREE="$here/21" &&
+ export GIT_WORK_TREE &&
+ git status >/dev/null
+ ) 2>message &&
+ test_must_be_empty message
+
+'
+run_wt_tests 21
+
+test_expect_success '#22a: core.worktree = GIT_DIR = .git dir' '
+ # like case #6.
+
+ setup_repo 22a "$here/22a/.git" "" unset &&
+ setup_repo 22ab . "" unset &&
+ mkdir -p 22a/.git/sub 22a/sub &&
+ mkdir -p 22ab/.git/sub 22ab/sub &&
+ try_case 22a/.git unset . \
+ . "$here/22a/.git" "$here/22a/.git" "(null)" &&
+ try_case 22a/.git unset "$here/22a/.git" \
+ "$here/22a/.git" "$here/22a/.git" "$here/22a/.git" "(null)" &&
+ try_case 22a/.git/sub unset .. \
+ "$here/22a/.git" "$here/22a/.git" "$here/22a/.git" sub/ &&
+ try_case 22a/.git/sub unset "$here/22a/.git" \
+ "$here/22a/.git" "$here/22a/.git" "$here/22a/.git" sub/ &&
+
+ try_case 22ab/.git unset . \
+ . "$here/22ab/.git" "$here/22ab/.git" "(null)" &&
+ try_case 22ab/.git unset "$here/22ab/.git" \
+ "$here/22ab/.git" "$here/22ab/.git" "$here/22ab/.git" "(null)" &&
+ try_case 22ab/.git/sub unset .. \
+ "$here/22ab/.git" "$here/22ab/.git" "$here/22ab/.git" sub/ &&
+ try_case 22ab/.git unset "$here/22ab/.git" \
+ "$here/22ab/.git" "$here/22ab/.git" "$here/22ab/.git" "(null)"
+'
+
+test_expect_success '#22b: core.worktree child of .git, GIT_DIR=.git' '
+ setup_repo 22b "$here/22b/.git/wt" "" unset &&
+ setup_repo 22bb wt "" unset &&
+ mkdir -p 22b/.git/sub 22b/sub 22b/.git/wt/sub 22b/wt/sub &&
+ mkdir -p 22bb/.git/sub 22bb/sub 22bb/.git/wt 22bb/wt &&
+
+ try_case 22b/.git unset . \
+ . "$here/22b/.git/wt" "$here/22b/.git" "(null)" &&
+ try_case 22b/.git unset "$here/22b/.git" \
+ "$here/22b/.git" "$here/22b/.git/wt" "$here/22b/.git" "(null)" &&
+ try_case 22b/.git/sub unset .. \
+ .. "$here/22b/.git/wt" "$here/22b/.git/sub" "(null)" &&
+ try_case 22b/.git/sub unset "$here/22b/.git" \
+ "$here/22b/.git" "$here/22b/.git/wt" "$here/22b/.git/sub" "(null)" &&
+
+ try_case 22bb/.git unset . \
+ . "$here/22bb/.git/wt" "$here/22bb/.git" "(null)" &&
+ try_case 22bb/.git unset "$here/22bb/.git" \
+ "$here/22bb/.git" "$here/22bb/.git/wt" "$here/22bb/.git" "(null)" &&
+ try_case 22bb/.git/sub unset .. \
+ .. "$here/22bb/.git/wt" "$here/22bb/.git/sub" "(null)" &&
+ try_case 22bb/.git/sub unset "$here/22bb/.git" \
+ "$here/22bb/.git" "$here/22bb/.git/wt" "$here/22bb/.git/sub" "(null)"
+'
+
+test_expect_success '#22c: core.worktree = .git/.., GIT_DIR=.git' '
+ setup_repo 22c "$here/22c" "" unset &&
+ setup_repo 22cb .. "" unset &&
+ mkdir -p 22c/.git/sub 22c/sub &&
+ mkdir -p 22cb/.git/sub 22cb/sub &&
+
+ try_case 22c/.git unset . \
+ "$here/22c/.git" "$here/22c" "$here/22c" .git/ &&
+ try_case 22c/.git unset "$here/22c/.git" \
+ "$here/22c/.git" "$here/22c" "$here/22c" .git/ &&
+ try_case 22c/.git/sub unset .. \
+ "$here/22c/.git" "$here/22c" "$here/22c" .git/sub/ &&
+ try_case 22c/.git/sub unset "$here/22c/.git" \
+ "$here/22c/.git" "$here/22c" "$here/22c" .git/sub/ &&
+
+ try_case 22cb/.git unset . \
+ "$here/22cb/.git" "$here/22cb" "$here/22cb" .git/ &&
+ try_case 22cb/.git unset "$here/22cb/.git" \
+ "$here/22cb/.git" "$here/22cb" "$here/22cb" .git/ &&
+ try_case 22cb/.git/sub unset .. \
+ "$here/22cb/.git" "$here/22cb" "$here/22cb" .git/sub/ &&
+ try_case 22cb/.git/sub unset "$here/22cb/.git" \
+ "$here/22cb/.git" "$here/22cb" "$here/22cb" .git/sub/
+'
+
+test_expect_success '#22.2: core.worktree and core.bare conflict' '
+ setup_repo 22 "$here/22" "" true &&
+ (
+ cd 22/.git &&
+ GIT_DIR=. &&
+ export GIT_DIR &&
+ test_must_fail git status 2>result
+ ) &&
+ (
+ cd 22 &&
+ GIT_DIR=.git &&
+ export GIT_DIR &&
+ test_must_fail git status 2>result
+ ) &&
+ grep "core.bare and core.worktree" 22/.git/result &&
+ grep "core.bare and core.worktree" 22/result
+'
+
+# Case #23: GIT_DIR + GIT_WORK_TREE(+core.worktree) suppresses bareness.
+test_expect_success '#23: setup' '
+ setup_repo 23 non-existent "" true &&
+ mkdir -p 23/sub/sub 23/wt/sub
+'
+run_wt_tests 23
+
+test_expect_success '#24: bare repo has no worktree (gitfile case)' '
+ try_repo 24 unset unset unset gitfile true \
+ "$here/24.git" "(null)" "$here/24" "(null)" \
+ "$here/24.git" "(null)" "$here/24/sub" "(null)"
+'
+
+test_expect_success '#25: GIT_WORK_TREE accepted if GIT_DIR unset (bare gitfile case)' '
+ try_repo 25 "$here/25" unset unset gitfile true \
+ "$here/25.git" "$here/25" "$here/25" "(null)" \
+ "$here/25.git" "$here/25" "$here/25" "sub/" 2>message &&
+ test_must_be_empty message
+'
+
+test_expect_success '#26: bare repo has no worktree (GIT_DIR -> gitfile case)' '
+ try_repo 26 unset "$here/26/.git" unset gitfile true \
+ "$here/26.git" "(null)" "$here/26" "(null)" \
+ "$here/26.git" "(null)" "$here/26/sub" "(null)" &&
+ try_repo 26b unset .git unset gitfile true \
+ "$here/26b.git" "(null)" "$here/26b" "(null)" \
+ "$here/26b.git" "(null)" "$here/26b/sub" "(null)"
+'
+
+# Case #27: GIT_DIR + GIT_WORK_TREE suppresses bareness (with gitfile).
+test_expect_success '#27: setup' '
+ setup_repo 27 unset gitfile true &&
+ mkdir -p 27/sub/sub 27/wt/sub
+'
+run_wt_tests 27 gitfile
+
+test_expect_success '#28: core.worktree and core.bare conflict (gitfile case)' '
+ setup_repo 28 "$here/28" gitfile true &&
+ (
+ cd 28 &&
+ test_must_fail git status
+ ) 2>message &&
+ grep "core.bare and core.worktree" message
+'
+
+# Case #29: GIT_WORK_TREE(+core.worktree) overrides core.bare (gitfile case).
+test_expect_success '#29: setup' '
+ setup_repo 29 non-existent gitfile true &&
+ mkdir -p 29/sub/sub 29/wt/sub &&
+ (
+ cd 29 &&
+ GIT_WORK_TREE="$here/29" &&
+ export GIT_WORK_TREE &&
+ git status
+ ) 2>message &&
+ test_must_be_empty message
+'
+run_wt_tests 29 gitfile
+
+test_expect_success '#30: core.worktree and core.bare conflict (gitfile version)' '
+ # Just like case #22.
+ setup_repo 30 "$here/30" gitfile true &&
+ (
+ cd 30 &&
+ test_must_fail env GIT_DIR=.git git status 2>result
+ ) &&
+ grep "core.bare and core.worktree" 30/result
+'
+
+# Case #31: GIT_DIR + GIT_WORK_TREE(+core.worktree) suppresses
+# bareness (gitfile version).
+test_expect_success '#31: setup' '
+ setup_repo 31 non-existent gitfile true &&
+ mkdir -p 31/sub/sub 31/wt/sub
+'
+run_wt_tests 31 gitfile
+
+test_done
diff --git a/t/t1511-rev-parse-caret.sh b/t/t1511-rev-parse-caret.sh
new file mode 100755
index 0000000..6ecfed8
--- /dev/null
+++ b/t/t1511-rev-parse-caret.sh
@@ -0,0 +1,134 @@
+#!/bin/sh
+
+test_description='tests for ref^{stuff}'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ echo blob >a-blob &&
+ git tag -a -m blob blob-tag $(git hash-object -w a-blob) &&
+ mkdir a-tree &&
+ echo moreblobs >a-tree/another-blob &&
+ git add . &&
+ TREE_SHA1=$(git write-tree) &&
+ git tag -a -m tree tree-tag "$TREE_SHA1" &&
+ git commit -m Initial &&
+ git tag -a -m commit commit-tag &&
+ git branch ref &&
+ git checkout main &&
+ echo modified >>a-blob &&
+ git add -u &&
+ git commit -m Modified &&
+ git branch modref &&
+ echo changed! >>a-blob &&
+ git add -u &&
+ git commit -m !Exp &&
+ git branch expref &&
+ echo changed >>a-blob &&
+ git add -u &&
+ git commit -m Changed &&
+ echo changed-again >>a-blob &&
+ git add -u &&
+ git commit -m Changed-again
+'
+
+test_expect_success 'ref^{non-existent}' '
+ test_must_fail git rev-parse ref^{non-existent}
+'
+
+test_expect_success 'ref^{}' '
+ git rev-parse ref >expected &&
+ git rev-parse ref^{} >actual &&
+ test_cmp expected actual &&
+ git rev-parse commit-tag^{} >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'ref^{commit}' '
+ git rev-parse ref >expected &&
+ git rev-parse ref^{commit} >actual &&
+ test_cmp expected actual &&
+ git rev-parse commit-tag^{commit} >actual &&
+ test_cmp expected actual &&
+ test_must_fail git rev-parse tree-tag^{commit} &&
+ test_must_fail git rev-parse blob-tag^{commit}
+'
+
+test_expect_success 'ref^{tree}' '
+ echo $TREE_SHA1 >expected &&
+ git rev-parse ref^{tree} >actual &&
+ test_cmp expected actual &&
+ git rev-parse commit-tag^{tree} >actual &&
+ test_cmp expected actual &&
+ git rev-parse tree-tag^{tree} >actual &&
+ test_cmp expected actual &&
+ test_must_fail git rev-parse blob-tag^{tree}
+'
+
+test_expect_success 'ref^{tag}' '
+ test_must_fail git rev-parse HEAD^{tag} &&
+ git rev-parse commit-tag >expected &&
+ git rev-parse commit-tag^{tag} >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'ref^{/.}' '
+ git rev-parse main >expected &&
+ git rev-parse main^{/.} >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'ref^{/non-existent}' '
+ test_must_fail git rev-parse main^{/non-existent}
+'
+
+test_expect_success 'ref^{/Initial}' '
+ git rev-parse ref >expected &&
+ git rev-parse main^{/Initial} >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'ref^{/!Exp}' '
+ test_must_fail git rev-parse main^{/!Exp}
+'
+
+test_expect_success 'ref^{/!}' '
+ test_must_fail git rev-parse main^{/!}
+'
+
+test_expect_success 'ref^{/!!Exp}' '
+ git rev-parse expref >expected &&
+ git rev-parse main^{/!!Exp} >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'ref^{/!-}' '
+ test_must_fail git rev-parse main^{/!-}
+'
+
+test_expect_success 'ref^{/!-.}' '
+ test_must_fail git rev-parse main^{/!-.}
+'
+
+test_expect_success 'ref^{/!-non-existent}' '
+ git rev-parse main >expected &&
+ git rev-parse main^{/!-non-existent} >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'ref^{/!-Changed}' '
+ git rev-parse expref >expected &&
+ git rev-parse main^{/!-Changed} >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'ref^{/!-!Exp}' '
+ git rev-parse modref >expected &&
+ git rev-parse expref^{/!-!Exp} >actual &&
+ test_cmp expected actual
+'
+
+test_done
diff --git a/t/t1512-rev-parse-disambiguation.sh b/t/t1512-rev-parse-disambiguation.sh
new file mode 100755
index 0000000..70f1e0a
--- /dev/null
+++ b/t/t1512-rev-parse-disambiguation.sh
@@ -0,0 +1,479 @@
+#!/bin/sh
+
+test_description='object name disambiguation
+
+Create blobs, trees, commits and a tag that all share the same
+prefix, and make sure "git rev-parse" can take advantage of
+type information to disambiguate short object names that are
+not necessarily unique.
+
+The final history used in the test has five commits, with the bottom
+one tagged as v1.0.0. They all have one regular file each.
+
+ +-------------------------------------------+
+ | |
+ | .-------b3wettvi---- ad2uee |
+ | / / |
+ | a2onsxbvj---czy8f73t--ioiley5o |
+ | |
+ +-------------------------------------------+
+
+'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_cmp_failed_rev_parse () {
+ dir=$1
+ rev=$2
+
+ cat >expect &&
+ test_must_fail git -C "$dir" rev-parse "$rev" 2>actual.raw &&
+ sed "s/\($rev\)[0-9a-f]*/\1.../" <actual.raw >actual &&
+ test_cmp expect actual
+}
+
+test_expect_success 'ambiguous blob output' '
+ git init --bare blob.prefix &&
+ (
+ cd blob.prefix &&
+
+ # Both start with "dead..", under both SHA-1 and SHA-256
+ echo brocdnra | git hash-object -w --stdin &&
+ echo brigddsv | git hash-object -w --stdin &&
+
+ # Both start with "beef.."
+ echo 1agllotbh | git hash-object -w --stdin &&
+ echo 1bbfctrkc | git hash-object -w --stdin
+ ) &&
+
+ test_must_fail git -C blob.prefix rev-parse dead &&
+ test_cmp_failed_rev_parse blob.prefix beef <<-\EOF
+ error: short object ID beef... is ambiguous
+ hint: The candidates are:
+ hint: beef... blob
+ hint: beef... blob
+ fatal: ambiguous argument '\''beef...'\'': unknown revision or path not in the working tree.
+ Use '\''--'\'' to separate paths from revisions, like this:
+ '\''git <command> [<revision>...] -- [<file>...]'\''
+ EOF
+'
+
+test_expect_success 'ambiguous loose bad object parsed as OBJ_BAD' '
+ git init --bare blob.bad &&
+ (
+ cd blob.bad &&
+
+ # Both have the prefix "bad0"
+ echo xyzfaowcoh | git hash-object -t bad -w --stdin --literally &&
+ echo xyzhjpyvwl | git hash-object -t bad -w --stdin --literally
+ ) &&
+
+ test_cmp_failed_rev_parse blob.bad bad0 <<-\EOF
+ error: short object ID bad0... is ambiguous
+ fatal: invalid object type
+ EOF
+'
+
+test_expect_success POSIXPERM 'ambigous zlib corrupt loose blob' '
+ git init --bare blob.corrupt &&
+ (
+ cd blob.corrupt &&
+
+ # Both have the prefix "cafe"
+ echo bnkxmdwz | git hash-object -w --stdin &&
+ oid=$(echo bmwsjxzi | git hash-object -w --stdin) &&
+
+ oidf=objects/$(test_oid_to_path "$oid") &&
+ chmod 755 $oidf &&
+ echo broken >$oidf
+ ) &&
+
+ test_cmp_failed_rev_parse blob.corrupt cafe <<-\EOF
+ error: short object ID cafe... is ambiguous
+ error: inflate: data stream error (incorrect header check)
+ error: unable to unpack cafe... header
+ error: inflate: data stream error (incorrect header check)
+ error: unable to unpack cafe... header
+ hint: The candidates are:
+ hint: cafe... [bad object]
+ hint: cafe... blob
+ fatal: ambiguous argument '\''cafe...'\'': unknown revision or path not in the working tree.
+ Use '\''--'\'' to separate paths from revisions, like this:
+ '\''git <command> [<revision>...] -- [<file>...]'\''
+ EOF
+'
+
+if ! test_have_prereq SHA1
+then
+ skip_all='not using SHA-1 for objects'
+ test_done
+fi
+
+test_expect_success 'blob and tree' '
+ test_tick &&
+ (
+ test_write_lines 0 1 2 3 4 5 6 7 8 9 &&
+ echo &&
+ echo b1rwzyc3
+ ) >a0blgqsjc &&
+
+ # create one blob 0000000000b36
+ git add a0blgqsjc &&
+
+ # create one tree 0000000000cdc
+ git write-tree
+'
+
+test_expect_success 'warn ambiguity when no candidate matches type hint' '
+ test_must_fail git rev-parse --verify 000000000^{commit} 2>actual &&
+ test_grep "short object ID 000000000 is ambiguous" actual
+'
+
+test_expect_success 'disambiguate tree-ish' '
+ # feed tree-ish in an unambiguous way
+ git rev-parse --verify 0000000000cdc:a0blgqsjc &&
+
+ # ambiguous at the object name level, but there is only one
+ # such tree-ish (the other is a blob)
+ git rev-parse --verify 000000000:a0blgqsjc
+'
+
+test_expect_success 'disambiguate blob' '
+ sed -e "s/|$//" >patch <<-EOF &&
+ diff --git a/frotz b/frotz
+ index 000000000..ffffff 100644
+ --- a/frotz
+ +++ b/frotz
+ @@ -10,3 +10,4 @@
+ 9
+ |
+ b1rwzyc3
+ +irwry
+ EOF
+ (
+ GIT_INDEX_FILE=frotz &&
+ export GIT_INDEX_FILE &&
+ git apply --build-fake-ancestor frotz patch &&
+ git cat-file blob :frotz >actual
+ ) &&
+ test_cmp a0blgqsjc actual
+'
+
+test_expect_success 'disambiguate tree' '
+ commit=$(echo "d7xm" | git commit-tree 000000000) &&
+ # this commit is fffff2e and not ambiguous with the 00000* objects
+ test $(git rev-parse $commit^{tree}) = $(git rev-parse 0000000000cdc)
+'
+
+test_expect_success 'first commit' '
+ # create one commit 0000000000e4f
+ git commit -m a2onsxbvj
+'
+
+test_expect_success 'disambiguate commit-ish' '
+ # feed commit-ish in an unambiguous way
+ git rev-parse --verify 0000000000e4f^{commit} &&
+
+ # ambiguous at the object name level, but there is only one
+ # such commit (the others are tree and blob)
+ git rev-parse --verify 000000000^{commit} &&
+
+ # likewise
+ git rev-parse --verify 000000000^0
+'
+
+test_expect_success 'disambiguate commit' '
+ commit=$(echo "hoaxj" | git commit-tree 0000000000cdc -p 000000000) &&
+ # this commit is ffffffd8 and not ambiguous with the 00000* objects
+ test $(git rev-parse $commit^) = $(git rev-parse 0000000000e4f)
+'
+
+test_expect_success 'log name1..name2 takes only commit-ishes on both ends' '
+ # These are underspecified from the prefix-length point of view
+ # to disambiguate the commit with other objects, but there is only
+ # one commit that has 00000* prefix at this point.
+ git log 000000000..000000000 &&
+ git log ..000000000 &&
+ git log 000000000.. &&
+ git log 000000000...000000000 &&
+ git log ...000000000 &&
+ git log 000000000...
+'
+
+test_expect_success 'rev-parse name1..name2 takes only commit-ishes on both ends' '
+ # Likewise.
+ git rev-parse 000000000..000000000 &&
+ git rev-parse ..000000000 &&
+ git rev-parse 000000000..
+'
+
+test_expect_success 'git log takes only commit-ish' '
+ # Likewise.
+ git log 000000000
+'
+
+test_expect_success 'git reset takes only commit-ish' '
+ # Likewise.
+ git reset 000000000
+'
+
+test_expect_success 'first tag' '
+ # create one tag 0000000000f8f
+ git tag -a -m j7cp83um v1.0.0
+'
+
+test_expect_failure 'two semi-ambiguous commit-ish' '
+ # At this point, we have a tag 0000000000f8f that points
+ # at a commit 0000000000e4f, and a tree and a blob that
+ # share 0000000000 prefix with these tag and commit.
+ #
+ # Once the parser becomes ultra-smart, it could notice that
+ # 0000000000 before ^{commit} name many different objects, but
+ # that only two (HEAD and v1.0.0 tag) can be peeled to commit,
+ # and that peeling them down to commit yield the same commit
+ # without ambiguity.
+ git rev-parse --verify 0000000000^{commit} &&
+
+ # likewise
+ git log 0000000000..0000000000 &&
+ git log ..0000000000 &&
+ git log 0000000000.. &&
+ git log 0000000000...0000000000 &&
+ git log ...0000000000 &&
+ git log 0000000000...
+'
+
+test_expect_failure 'three semi-ambiguous tree-ish' '
+ # Likewise for tree-ish. HEAD, v1.0.0 and HEAD^{tree} share
+ # the prefix but peeling them to tree yields the same thing
+ git rev-parse --verify 0000000000^{tree}
+'
+
+test_expect_success 'parse describe name' '
+ # feed an unambiguous describe name
+ git rev-parse --verify v1.0.0-0-g0000000000e4f &&
+
+ # ambiguous at the object name level, but there is only one
+ # such commit (others are blob, tree and tag)
+ git rev-parse --verify v1.0.0-0-g000000000
+'
+
+test_expect_success 'more history' '
+ # commit 0000000000043
+ git mv a0blgqsjc d12cr3h8t &&
+ echo h62xsjeu >>d12cr3h8t &&
+ git add d12cr3h8t &&
+
+ test_tick &&
+ git commit -m czy8f73t &&
+
+ # commit 00000000008ec
+ git mv d12cr3h8t j000jmpzn &&
+ echo j08bekfvt >>j000jmpzn &&
+ git add j000jmpzn &&
+
+ test_tick &&
+ git commit -m ioiley5o &&
+
+ # commit 0000000005b0
+ git checkout v1.0.0^0 &&
+ git mv a0blgqsjc f5518nwu &&
+
+ test_write_lines h62xsjeu j08bekfvt kg7xflhm >>f5518nwu &&
+ git add f5518nwu &&
+
+ test_tick &&
+ git commit -m b3wettvi &&
+ side=$(git rev-parse HEAD) &&
+
+ # commit 000000000066
+ git checkout main &&
+
+ # If you use recursive, merge will fail and you will need to
+ # clean up a0blgqsjc as well. If you use resolve, merge will
+ # succeed.
+ test_might_fail git merge --no-commit -s recursive $side &&
+ git rm -f f5518nwu j000jmpzn &&
+
+ test_might_fail git rm -f a0blgqsjc &&
+ (
+ git cat-file blob $side:f5518nwu &&
+ echo j3l0i9s6
+ ) >ab2gs879 &&
+ git add ab2gs879 &&
+
+ test_tick &&
+ git commit -m ad2uee
+
+'
+
+test_expect_failure 'parse describe name taking advantage of generation' '
+ # ambiguous at the object name level, but there is only one
+ # such commit at generation 0
+ git rev-parse --verify v1.0.0-0-g000000000 &&
+
+ # likewise for generation 2 and 4
+ git rev-parse --verify v1.0.0-2-g000000000 &&
+ git rev-parse --verify v1.0.0-4-g000000000
+'
+
+# Note: because rev-parse does not even try to disambiguate based on
+# the generation number, this test currently succeeds for a wrong
+# reason. When it learns to use the generation number, the previous
+# test should succeed, and also this test should fail because the
+# describe name used in the test with generation number can name two
+# commits. Make sure that such a future enhancement does not randomly
+# pick one.
+test_expect_success 'parse describe name not ignoring ambiguity' '
+ # ambiguous at the object name level, and there are two such
+ # commits at generation 1
+ test_must_fail git rev-parse --verify v1.0.0-1-g000000000
+'
+
+test_expect_success 'ambiguous commit-ish' '
+ # Now there are many commits that begin with the
+ # common prefix, none of these should pick one at
+ # random. They all should result in ambiguity errors.
+ test_must_fail git rev-parse --verify 00000000^{commit} &&
+
+ # likewise
+ test_must_fail git log 000000000..000000000 &&
+ test_must_fail git log ..000000000 &&
+ test_must_fail git log 000000000.. &&
+ test_must_fail git log 000000000...000000000 &&
+ test_must_fail git log ...000000000 &&
+ test_must_fail git log 000000000...
+'
+
+# There are three objects with this prefix: a blob, a tree, and a tag. We know
+# the blob will not pass as a treeish, but the tree and tag should (and thus
+# cause an error).
+test_expect_success 'ambiguous tags peel to treeish' '
+ test_must_fail git rev-parse 0000000000f^{tree}
+'
+
+test_expect_success 'rev-parse --disambiguate' '
+ # The test creates 16 objects that share the prefix and two
+ # commits created by commit-tree in earlier tests share a
+ # different prefix.
+ git rev-parse --disambiguate=000000000 >actual &&
+ test_line_count = 16 actual &&
+ test "$(sed -e "s/^\(.........\).*/\1/" actual | sort -u)" = 000000000
+'
+
+test_expect_success 'rev-parse --disambiguate drops duplicates' '
+ git rev-parse --disambiguate=000000000 >expect &&
+ git pack-objects .git/objects/pack/pack <expect &&
+ git rev-parse --disambiguate=000000000 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'ambiguous 40-hex ref' '
+ TREE=$(git mktree </dev/null) &&
+ REF=$(git rev-parse HEAD) &&
+ VAL=$(git commit-tree $TREE </dev/null) &&
+ git update-ref refs/heads/$REF $VAL &&
+ test $(git rev-parse $REF 2>err) = $REF &&
+ grep "refname.*${REF}.*ambiguous" err
+'
+
+test_expect_success 'ambiguous short sha1 ref' '
+ TREE=$(git mktree </dev/null) &&
+ REF=$(git rev-parse --short HEAD) &&
+ VAL=$(git commit-tree $TREE </dev/null) &&
+ git update-ref refs/heads/$REF $VAL &&
+ test $(git rev-parse $REF 2>err) = $VAL &&
+ grep "refname.*${REF}.*ambiguous" err
+'
+
+test_expect_success 'ambiguity errors are not repeated (raw)' '
+ test_must_fail git rev-parse 00000 2>stderr &&
+ grep "is ambiguous" stderr >errors &&
+ test_line_count = 1 errors
+'
+
+test_expect_success 'ambiguity errors are not repeated (treeish)' '
+ test_must_fail git rev-parse 00000:foo 2>stderr &&
+ grep "is ambiguous" stderr >errors &&
+ test_line_count = 1 errors
+'
+
+test_expect_success 'ambiguity errors are not repeated (peel)' '
+ test_must_fail git rev-parse 00000^{commit} 2>stderr &&
+ grep "is ambiguous" stderr >errors &&
+ test_line_count = 1 errors
+'
+
+test_expect_success 'ambiguity hints' '
+ test_must_fail git rev-parse 000000000 2>stderr &&
+ grep ^hint: stderr >hints &&
+ # 16 candidates, plus one intro line
+ test_line_count = 17 hints
+'
+
+test_expect_success 'ambiguity hints respect type' '
+ test_must_fail git rev-parse 000000000^{commit} 2>stderr &&
+ grep ^hint: stderr >hints &&
+ # 5 commits, 1 tag (which is a committish), plus intro line
+ test_line_count = 7 hints
+'
+
+test_expect_success 'failed type-selector still shows hint' '
+ # these two blobs share the same prefix "ee3d", but neither
+ # will pass for a commit
+ echo 851 | git hash-object --stdin -w &&
+ echo 872 | git hash-object --stdin -w &&
+ test_must_fail git rev-parse ee3d^{commit} 2>stderr &&
+ grep ^hint: stderr >hints &&
+ test_line_count = 3 hints
+'
+
+test_expect_success 'core.disambiguate config can prefer types' '
+ # ambiguous between tree and tag
+ sha1=0000000000f &&
+ test_must_fail git rev-parse $sha1 &&
+ git rev-parse $sha1^{commit} &&
+ git -c core.disambiguate=committish rev-parse $sha1
+'
+
+test_expect_success 'core.disambiguate does not override context' '
+ # treeish ambiguous between tag and tree
+ test_must_fail \
+ git -c core.disambiguate=committish rev-parse $sha1^{tree}
+'
+
+test_expect_success 'ambiguous commits are printed by type first, then hash order' '
+ test_must_fail git rev-parse 0000 2>stderr &&
+ grep ^hint: stderr >hints &&
+ grep 0000 hints >objects &&
+ cat >expected <<-\EOF &&
+ tag
+ commit
+ tree
+ blob
+ EOF
+ awk "{print \$3}" <objects >objects.types &&
+ uniq <objects.types >objects.types.uniq &&
+ test_cmp expected objects.types.uniq &&
+ for type in tag commit tree blob
+ do
+ grep $type objects >$type.objects &&
+ sort $type.objects >$type.objects.sorted &&
+ test_cmp $type.objects.sorted $type.objects || return 1
+ done
+'
+
+test_expect_success 'cat-file --batch and --batch-check show ambiguous' '
+ echo "0000 ambiguous" >expect &&
+ echo 0000 | git cat-file --batch-check >actual 2>err &&
+ test_cmp expect actual &&
+ test_grep hint: err &&
+ echo 0000 | git cat-file --batch >actual 2>err &&
+ test_cmp expect actual &&
+ test_grep hint: err
+'
+
+test_done
diff --git a/t/t1513-rev-parse-prefix.sh b/t/t1513-rev-parse-prefix.sh
new file mode 100755
index 0000000..ba43387
--- /dev/null
+++ b/t/t1513-rev-parse-prefix.sh
@@ -0,0 +1,100 @@
+#!/bin/sh
+
+test_description='Tests for rev-parse --prefix'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ mkdir -p sub1/sub2 &&
+ echo top >top &&
+ echo file1 >sub1/file1 &&
+ echo file2 >sub1/sub2/file2 &&
+ git add top sub1/file1 sub1/sub2/file2 &&
+ git commit -m commit
+'
+
+test_expect_success 'empty prefix -- file' '
+ git rev-parse --prefix "" -- top sub1/file1 >actual &&
+ cat <<-\EOF >expected &&
+ --
+ top
+ sub1/file1
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'valid prefix -- file' '
+ git rev-parse --prefix sub1/ -- file1 sub2/file2 >actual &&
+ cat <<-\EOF >expected &&
+ --
+ sub1/file1
+ sub1/sub2/file2
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'valid prefix -- ../file' '
+ git rev-parse --prefix sub1/ -- ../top sub2/file2 >actual &&
+ cat <<-\EOF >expected &&
+ --
+ sub1/../top
+ sub1/sub2/file2
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'empty prefix HEAD:./path' '
+ git rev-parse --prefix "" HEAD:./top >actual &&
+ git rev-parse HEAD:top >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'valid prefix HEAD:./path' '
+ git rev-parse --prefix sub1/ HEAD:./file1 >actual &&
+ git rev-parse HEAD:sub1/file1 >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'valid prefix HEAD:../path' '
+ git rev-parse --prefix sub1/ HEAD:../top >actual &&
+ git rev-parse HEAD:top >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'prefix ignored with HEAD:top' '
+ git rev-parse --prefix sub1/ HEAD:top >actual &&
+ git rev-parse HEAD:top >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'disambiguate path with valid prefix' '
+ git rev-parse --prefix sub1/ file1 >actual &&
+ cat <<-\EOF >expected &&
+ sub1/file1
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'file and refs with prefix' '
+ git rev-parse --prefix sub1/ main file1 >actual &&
+ cat <<-EOF >expected &&
+ $(git rev-parse main)
+ sub1/file1
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'two-levels deep' '
+ git rev-parse --prefix sub1/sub2/ -- file2 >actual &&
+ cat <<-\EOF >expected &&
+ --
+ sub1/sub2/file2
+ EOF
+ test_cmp expected actual
+'
+
+test_done
diff --git a/t/t1514-rev-parse-push.sh b/t/t1514-rev-parse-push.sh
new file mode 100755
index 0000000..a835a19
--- /dev/null
+++ b/t/t1514-rev-parse-push.sh
@@ -0,0 +1,77 @@
+#!/bin/sh
+
+test_description='test <branch>@{push} syntax'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+resolve () {
+ echo "$2" >expect &&
+ git rev-parse --symbolic-full-name "$1" >actual &&
+ test_cmp expect actual
+}
+
+test_expect_success 'setup' '
+ git init --bare parent.git &&
+ git init --bare other.git &&
+ git remote add origin parent.git &&
+ git remote add other other.git &&
+ test_commit base &&
+ git push origin HEAD &&
+ git branch --set-upstream-to=origin/main main &&
+ git branch --track topic origin/main &&
+ git push origin topic &&
+ git push other topic
+'
+
+test_expect_success '@{push} with default=nothing' '
+ test_config push.default nothing &&
+ test_must_fail git rev-parse main@{push} &&
+ test_must_fail git rev-parse main@{PUSH} &&
+ test_must_fail git rev-parse main@{PuSH}
+'
+
+test_expect_success '@{push} with default=simple' '
+ test_config push.default simple &&
+ resolve main@{push} refs/remotes/origin/main &&
+ resolve main@{PUSH} refs/remotes/origin/main &&
+ resolve main@{pUSh} refs/remotes/origin/main
+'
+
+test_expect_success 'triangular @{push} fails with default=simple' '
+ test_config push.default simple &&
+ test_must_fail git rev-parse topic@{push}
+'
+
+test_expect_success '@{push} with default=current' '
+ test_config push.default current &&
+ resolve topic@{push} refs/remotes/origin/topic
+'
+
+test_expect_success '@{push} with default=matching' '
+ test_config push.default matching &&
+ resolve topic@{push} refs/remotes/origin/topic
+'
+
+test_expect_success '@{push} with pushremote defined' '
+ test_config push.default current &&
+ test_config branch.topic.pushremote other &&
+ resolve topic@{push} refs/remotes/other/topic
+'
+
+test_expect_success '@{push} with push refspecs' '
+ test_config push.default nothing &&
+ test_config remote.origin.push refs/heads/*:refs/heads/magic/* &&
+ git push &&
+ resolve topic@{push} refs/remotes/origin/magic/topic
+'
+
+test_expect_success 'resolving @{push} fails with a detached HEAD' '
+ git checkout HEAD^0 &&
+ test_when_finished "git checkout -" &&
+ test_must_fail git rev-parse @{push}
+'
+
+test_done
diff --git a/t/t1515-rev-parse-outside-repo.sh b/t/t1515-rev-parse-outside-repo.sh
new file mode 100755
index 0000000..cdb26a3
--- /dev/null
+++ b/t/t1515-rev-parse-outside-repo.sh
@@ -0,0 +1,47 @@
+#!/bin/sh
+
+test_description='check that certain rev-parse options work outside repo'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'set up non-repo directory' '
+ GIT_CEILING_DIRECTORIES=$(pwd) &&
+ export GIT_CEILING_DIRECTORIES &&
+ mkdir non-repo &&
+ cd non-repo &&
+ # confirm that git does not find a repo
+ test_must_fail git rev-parse --git-dir
+'
+
+# Rather than directly test the output of sq-quote directly,
+# make sure the shell can read back a tricky case, since
+# that's what we really care about anyway.
+tricky="really tricky with \\ and \" and '"
+dump_args () {
+ for i in "$@"; do
+ echo "arg: $i"
+ done
+}
+test_expect_success 'rev-parse --sq-quote' '
+ dump_args "$tricky" easy >expect &&
+ eval "dump_args $(git rev-parse --sq-quote "$tricky" easy)" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rev-parse --local-env-vars' '
+ git rev-parse --local-env-vars >actual &&
+ # we do not want to depend on the complete list here,
+ # so just look for something plausible
+ grep ^GIT_DIR actual
+'
+
+test_expect_success 'rev-parse --resolve-git-dir' '
+ git init --separate-git-dir repo dir &&
+ test_must_fail git rev-parse --resolve-git-dir . &&
+ echo "$(pwd)/repo" >expect &&
+ git rev-parse --resolve-git-dir dir/.git >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t1600-index.sh b/t/t1600-index.sh
new file mode 100755
index 0000000..62e7fd1
--- /dev/null
+++ b/t/t1600-index.sh
@@ -0,0 +1,137 @@
+#!/bin/sh
+
+test_description='index file specific tests'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+sane_unset GIT_TEST_SPLIT_INDEX
+
+test_expect_success 'setup' '
+ echo 1 >a
+'
+
+test_expect_success 'bogus GIT_INDEX_VERSION issues warning' '
+ (
+ rm -f .git/index &&
+ GIT_INDEX_VERSION=2bogus &&
+ export GIT_INDEX_VERSION &&
+ git add a 2>err &&
+ sed "s/[0-9]//" err >actual.err &&
+ sed -e "s/ Z$/ /" <<-\EOF >expect.err &&
+ warning: GIT_INDEX_VERSION set, but the value is invalid.
+ Using version Z
+ EOF
+ test_cmp expect.err actual.err
+ )
+'
+
+test_expect_success 'out of bounds GIT_INDEX_VERSION issues warning' '
+ (
+ rm -f .git/index &&
+ GIT_INDEX_VERSION=1 &&
+ export GIT_INDEX_VERSION &&
+ git add a 2>err &&
+ sed "s/[0-9]//" err >actual.err &&
+ sed -e "s/ Z$/ /" <<-\EOF >expect.err &&
+ warning: GIT_INDEX_VERSION set, but the value is invalid.
+ Using version Z
+ EOF
+ test_cmp expect.err actual.err
+ )
+'
+
+test_expect_success 'no warning with bogus GIT_INDEX_VERSION and existing index' '
+ (
+ GIT_INDEX_VERSION=1 &&
+ export GIT_INDEX_VERSION &&
+ git add a 2>actual.err &&
+ test_must_be_empty actual.err
+ )
+'
+
+test_expect_success 'out of bounds index.version issues warning' '
+ (
+ sane_unset GIT_INDEX_VERSION &&
+ rm -f .git/index &&
+ git config --add index.version 1 &&
+ git add a 2>err &&
+ sed "s/[0-9]//" err >actual.err &&
+ sed -e "s/ Z$/ /" <<-\EOF >expect.err &&
+ warning: index.version set, but the value is invalid.
+ Using version Z
+ EOF
+ test_cmp expect.err actual.err
+ )
+'
+
+test_expect_success 'index.skipHash config option' '
+ rm -f .git/index &&
+ git -c index.skipHash=true add a &&
+ test_trailing_hash .git/index >hash &&
+ echo $(test_oid zero) >expect &&
+ test_cmp expect hash &&
+ git fsck &&
+
+ rm -f .git/index &&
+ git -c feature.manyFiles=true add a &&
+ test_trailing_hash .git/index >hash &&
+ cmp expect hash &&
+
+ rm -f .git/index &&
+ git -c feature.manyFiles=true \
+ -c index.skipHash=false add a &&
+ test_trailing_hash .git/index >hash &&
+ ! cmp expect hash &&
+
+ test_commit start &&
+ git -c protocol.file.allow=always submodule add ./ sub &&
+ git config index.skipHash false &&
+ git -C sub config index.skipHash true &&
+ rm -f .git/modules/sub/index &&
+ >sub/file &&
+ git -C sub add a &&
+ test_trailing_hash .git/modules/sub/index >hash &&
+ test_cmp expect hash &&
+ git -C sub fsck
+'
+
+test_index_version () {
+ INDEX_VERSION_CONFIG=$1 &&
+ FEATURE_MANY_FILES=$2 &&
+ ENV_VAR_VERSION=$3
+ EXPECTED_OUTPUT_VERSION=$4 &&
+ (
+ rm -f .git/index &&
+ rm -f .git/config &&
+ if test "$INDEX_VERSION_CONFIG" -ne 0
+ then
+ git config --add index.version $INDEX_VERSION_CONFIG
+ fi &&
+ git config --add feature.manyFiles $FEATURE_MANY_FILES
+ if test "$ENV_VAR_VERSION" -ne 0
+ then
+ GIT_INDEX_VERSION=$ENV_VAR_VERSION &&
+ export GIT_INDEX_VERSION
+ else
+ unset GIT_INDEX_VERSION
+ fi &&
+ git add a &&
+ echo $EXPECTED_OUTPUT_VERSION >expect &&
+ git update-index --show-index-version >actual &&
+ test_cmp expect actual
+ )
+}
+
+test_expect_success 'index version config precedence' '
+ test_index_version 0 false 0 2 &&
+ test_index_version 2 false 0 2 &&
+ test_index_version 3 false 0 2 &&
+ test_index_version 4 false 0 4 &&
+ test_index_version 2 false 4 4 &&
+ test_index_version 2 true 0 2 &&
+ test_index_version 0 true 0 4 &&
+ test_index_version 0 true 2 2
+'
+
+test_done
diff --git a/t/t1601-index-bogus.sh b/t/t1601-index-bogus.sh
new file mode 100755
index 0000000..4171f1e
--- /dev/null
+++ b/t/t1601-index-bogus.sh
@@ -0,0 +1,22 @@
+#!/bin/sh
+
+test_description='test handling of bogus index entries'
+. ./test-lib.sh
+
+test_expect_success 'create tree with null sha1' '
+ tree=$(printf "160000 commit $ZERO_OID\\tbroken\\n" | git mktree)
+'
+
+test_expect_success 'read-tree refuses to read null sha1' '
+ test_must_fail git read-tree $tree
+'
+
+test_expect_success 'GIT_ALLOW_NULL_SHA1 overrides refusal' '
+ GIT_ALLOW_NULL_SHA1=1 git read-tree $tree
+'
+
+test_expect_success 'git write-tree refuses to write null sha1' '
+ test_must_fail git write-tree
+'
+
+test_done
diff --git a/t/t1700-split-index.sh b/t/t1700-split-index.sh
new file mode 100755
index 0000000..a7b7263
--- /dev/null
+++ b/t/t1700-split-index.sh
@@ -0,0 +1,547 @@
+#!/bin/sh
+
+test_description='split index mode tests'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+# We need total control of index splitting here
+sane_unset GIT_TEST_SPLIT_INDEX
+
+# Testing a hard coded SHA against an index with an extension
+# that can vary from run to run is problematic so we disable
+# those extensions.
+sane_unset GIT_TEST_FSMONITOR
+sane_unset GIT_TEST_INDEX_THREADS
+
+# Create a file named as $1 with content read from stdin.
+# Set the file's mtime to a few seconds in the past to avoid racy situations.
+create_non_racy_file () {
+ cat >"$1" &&
+ test-tool chmtime =-5 "$1"
+}
+
+test_expect_success 'setup' '
+ test_oid_cache <<-EOF
+ own_v3 sha1:8299b0bcd1ac364e5f1d7768efb62fa2da79a339
+ own_v3 sha256:38a6d2925e3eceec33ad7b34cbff4e0086caa0daf28f31e51f5bd94b4a7af86b
+
+ base_v3 sha1:39d890139ee5356c7ef572216cebcd27aa41f9df
+ base_v3 sha256:c9baeadf905112bf6c17aefbd7d02267afd70ded613c30cafed2d40cb506e1ed
+
+ own_v4 sha1:432ef4b63f32193984f339431fd50ca796493569
+ own_v4 sha256:6738ac6319c25b694afa7bcc313deb182d1a59b68bf7a47b4296de83478c0420
+
+ base_v4 sha1:508851a7f0dfa8691e9f69c7f055865389012491
+ base_v4 sha256:3177d4adfdd4b6904f7e921d91d715a471c0dde7cf6a4bba574927f02b699508
+ EOF
+'
+
+test_expect_success 'enable split index' '
+ git config splitIndex.maxPercentChange 100 &&
+ git update-index --split-index &&
+ test-tool dump-split-index .git/index >actual &&
+ indexversion=$(git update-index --show-index-version) &&
+
+ # NEEDSWORK: Stop hard-coding checksums.
+ if test "$indexversion" = "4"
+ then
+ own=$(test_oid own_v4) &&
+ base=$(test_oid base_v4)
+ else
+ own=$(test_oid own_v3) &&
+ base=$(test_oid base_v3)
+ fi &&
+
+ cat >expect <<-EOF &&
+ own $own
+ base $base
+ replacements:
+ deletions:
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'add one file' '
+ create_non_racy_file one &&
+ git update-index --add one &&
+ git ls-files --stage >ls-files.actual &&
+ cat >ls-files.expect <<-EOF &&
+ 100644 $EMPTY_BLOB 0 one
+ EOF
+ test_cmp ls-files.expect ls-files.actual &&
+
+ test-tool dump-split-index .git/index | sed "/^own/d" >actual &&
+ cat >expect <<-EOF &&
+ base $base
+ 100644 $EMPTY_BLOB 0 one
+ replacements:
+ deletions:
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'disable split index' '
+ git update-index --no-split-index &&
+ git ls-files --stage >ls-files.actual &&
+ cat >ls-files.expect <<-EOF &&
+ 100644 $EMPTY_BLOB 0 one
+ EOF
+ test_cmp ls-files.expect ls-files.actual &&
+
+ BASE=$(test-tool dump-split-index .git/index | sed -n "s/^own/base/p") &&
+ test-tool dump-split-index .git/index | sed "/^own/d" >actual &&
+ cat >expect <<-EOF &&
+ not a split index
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'enable split index again, "one" now belongs to base index"' '
+ git update-index --split-index &&
+ git ls-files --stage >ls-files.actual &&
+ cat >ls-files.expect <<-EOF &&
+ 100644 $EMPTY_BLOB 0 one
+ EOF
+ test_cmp ls-files.expect ls-files.actual &&
+
+ test-tool dump-split-index .git/index | sed "/^own/d" >actual &&
+ cat >expect <<-EOF &&
+ $BASE
+ replacements:
+ deletions:
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'modify original file, base index untouched' '
+ echo modified | create_non_racy_file one &&
+ file1_blob=$(git hash-object one) &&
+ git update-index one &&
+ git ls-files --stage >ls-files.actual &&
+ cat >ls-files.expect <<-EOF &&
+ 100644 $file1_blob 0 one
+ EOF
+ test_cmp ls-files.expect ls-files.actual &&
+
+ test-tool dump-split-index .git/index | sed "/^own/d" >actual &&
+ q_to_tab >expect <<-EOF &&
+ $BASE
+ 100644 $file1_blob 0Q
+ replacements: 0
+ deletions:
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'add another file, which stays index' '
+ create_non_racy_file two &&
+ git update-index --add two &&
+ git ls-files --stage >ls-files.actual &&
+ cat >ls-files.expect <<-EOF &&
+ 100644 $file1_blob 0 one
+ 100644 $EMPTY_BLOB 0 two
+ EOF
+ test_cmp ls-files.expect ls-files.actual &&
+
+ test-tool dump-split-index .git/index | sed "/^own/d" >actual &&
+ q_to_tab >expect <<-EOF &&
+ $BASE
+ 100644 $file1_blob 0Q
+ 100644 $EMPTY_BLOB 0 two
+ replacements: 0
+ deletions:
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'remove file not in base index' '
+ git update-index --force-remove two &&
+ git ls-files --stage >ls-files.actual &&
+ cat >ls-files.expect <<-EOF &&
+ 100644 $file1_blob 0 one
+ EOF
+ test_cmp ls-files.expect ls-files.actual &&
+
+ test-tool dump-split-index .git/index | sed "/^own/d" >actual &&
+ q_to_tab >expect <<-EOF &&
+ $BASE
+ 100644 $file1_blob 0Q
+ replacements: 0
+ deletions:
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'remove file in base index' '
+ git update-index --force-remove one &&
+ git ls-files --stage >ls-files.actual &&
+ test_must_be_empty ls-files.actual &&
+
+ test-tool dump-split-index .git/index | sed "/^own/d" >actual &&
+ cat >expect <<-EOF &&
+ $BASE
+ replacements:
+ deletions: 0
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'add original file back' '
+ create_non_racy_file one &&
+ git update-index --add one &&
+ git ls-files --stage >ls-files.actual &&
+ cat >ls-files.expect <<-EOF &&
+ 100644 $EMPTY_BLOB 0 one
+ EOF
+ test_cmp ls-files.expect ls-files.actual &&
+
+ test-tool dump-split-index .git/index | sed "/^own/d" >actual &&
+ cat >expect <<-EOF &&
+ $BASE
+ 100644 $EMPTY_BLOB 0 one
+ replacements:
+ deletions: 0
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'add new file' '
+ create_non_racy_file two &&
+ git update-index --add two &&
+ git ls-files --stage >actual &&
+ cat >expect <<-EOF &&
+ 100644 $EMPTY_BLOB 0 one
+ 100644 $EMPTY_BLOB 0 two
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'unify index, two files remain' '
+ git update-index --no-split-index &&
+ git ls-files --stage >ls-files.actual &&
+ cat >ls-files.expect <<-EOF &&
+ 100644 $EMPTY_BLOB 0 one
+ 100644 $EMPTY_BLOB 0 two
+ EOF
+ test_cmp ls-files.expect ls-files.actual &&
+
+ test-tool dump-split-index .git/index | sed "/^own/d" >actual &&
+ cat >expect <<-EOF &&
+ not a split index
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'rev-parse --shared-index-path' '
+ test_create_repo split-index &&
+ (
+ cd split-index &&
+ git update-index --split-index &&
+ echo .git/sharedindex* >expect &&
+ git rev-parse --shared-index-path >actual &&
+ test_cmp expect actual &&
+ mkdir subdirectory &&
+ cd subdirectory &&
+ echo ../.git/sharedindex* >expect &&
+ git rev-parse --shared-index-path >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'set core.splitIndex config variable to true' '
+ git config core.splitIndex true &&
+ create_non_racy_file three &&
+ git update-index --add three &&
+ git ls-files --stage >ls-files.actual &&
+ cat >ls-files.expect <<-EOF &&
+ 100644 $EMPTY_BLOB 0 one
+ 100644 $EMPTY_BLOB 0 three
+ 100644 $EMPTY_BLOB 0 two
+ EOF
+ test_cmp ls-files.expect ls-files.actual &&
+ BASE=$(test-tool dump-split-index .git/index | grep "^base") &&
+ test-tool dump-split-index .git/index | sed "/^own/d" >actual &&
+ cat >expect <<-EOF &&
+ $BASE
+ replacements:
+ deletions:
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'set core.splitIndex config variable to false' '
+ git config core.splitIndex false &&
+ git update-index --force-remove three &&
+ git ls-files --stage >ls-files.actual &&
+ cat >ls-files.expect <<-EOF &&
+ 100644 $EMPTY_BLOB 0 one
+ 100644 $EMPTY_BLOB 0 two
+ EOF
+ test_cmp ls-files.expect ls-files.actual &&
+ test-tool dump-split-index .git/index | sed "/^own/d" >actual &&
+ cat >expect <<-EOF &&
+ not a split index
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'set core.splitIndex config variable back to true' '
+ git config core.splitIndex true &&
+ create_non_racy_file three &&
+ git update-index --add three &&
+ BASE=$(test-tool dump-split-index .git/index | grep "^base") &&
+ test-tool dump-split-index .git/index | sed "/^own/d" >actual &&
+ cat >expect <<-EOF &&
+ $BASE
+ replacements:
+ deletions:
+ EOF
+ test_cmp expect actual &&
+ create_non_racy_file four &&
+ git update-index --add four &&
+ test-tool dump-split-index .git/index | sed "/^own/d" >actual &&
+ cat >expect <<-EOF &&
+ $BASE
+ 100644 $EMPTY_BLOB 0 four
+ replacements:
+ deletions:
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'check behavior with splitIndex.maxPercentChange unset' '
+ git config --unset splitIndex.maxPercentChange &&
+ create_non_racy_file five &&
+ git update-index --add five &&
+ BASE=$(test-tool dump-split-index .git/index | grep "^base") &&
+ test-tool dump-split-index .git/index | sed "/^own/d" >actual &&
+ cat >expect <<-EOF &&
+ $BASE
+ replacements:
+ deletions:
+ EOF
+ test_cmp expect actual &&
+ create_non_racy_file six &&
+ git update-index --add six &&
+ test-tool dump-split-index .git/index | sed "/^own/d" >actual &&
+ cat >expect <<-EOF &&
+ $BASE
+ 100644 $EMPTY_BLOB 0 six
+ replacements:
+ deletions:
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'check splitIndex.maxPercentChange set to 0' '
+ git config splitIndex.maxPercentChange 0 &&
+ create_non_racy_file seven &&
+ git update-index --add seven &&
+ BASE=$(test-tool dump-split-index .git/index | grep "^base") &&
+ test-tool dump-split-index .git/index | sed "/^own/d" >actual &&
+ cat >expect <<-EOF &&
+ $BASE
+ replacements:
+ deletions:
+ EOF
+ test_cmp expect actual &&
+ create_non_racy_file eight &&
+ git update-index --add eight &&
+ BASE=$(test-tool dump-split-index .git/index | grep "^base") &&
+ test-tool dump-split-index .git/index | sed "/^own/d" >actual &&
+ cat >expect <<-EOF &&
+ $BASE
+ replacements:
+ deletions:
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'shared index files expire after 2 weeks by default' '
+ create_non_racy_file ten &&
+ git update-index --add ten &&
+ test $(ls .git/sharedindex.* | wc -l) -gt 2 &&
+ just_under_2_weeks_ago=$((5-14*86400)) &&
+ test-tool chmtime =$just_under_2_weeks_ago .git/sharedindex.* &&
+ create_non_racy_file eleven &&
+ git update-index --add eleven &&
+ test $(ls .git/sharedindex.* | wc -l) -gt 2 &&
+ just_over_2_weeks_ago=$((-1-14*86400)) &&
+ test-tool chmtime =$just_over_2_weeks_ago .git/sharedindex.* &&
+ create_non_racy_file twelve &&
+ git update-index --add twelve &&
+ test $(ls .git/sharedindex.* | wc -l) -le 2
+'
+
+test_expect_success 'check splitIndex.sharedIndexExpire set to 16 days' '
+ git config splitIndex.sharedIndexExpire "16.days.ago" &&
+ test-tool chmtime =$just_over_2_weeks_ago .git/sharedindex.* &&
+ create_non_racy_file thirteen &&
+ git update-index --add thirteen &&
+ test $(ls .git/sharedindex.* | wc -l) -gt 2 &&
+ just_over_16_days_ago=$((-1-16*86400)) &&
+ test-tool chmtime =$just_over_16_days_ago .git/sharedindex.* &&
+ create_non_racy_file fourteen &&
+ git update-index --add fourteen &&
+ test $(ls .git/sharedindex.* | wc -l) -le 2
+'
+
+test_expect_success 'check splitIndex.sharedIndexExpire set to "never" and "now"' '
+ git config splitIndex.sharedIndexExpire never &&
+ just_10_years_ago=$((-365*10*86400)) &&
+ test-tool chmtime =$just_10_years_ago .git/sharedindex.* &&
+ create_non_racy_file fifteen &&
+ git update-index --add fifteen &&
+ test $(ls .git/sharedindex.* | wc -l) -gt 2 &&
+ git config splitIndex.sharedIndexExpire now &&
+ just_1_second_ago=-1 &&
+ test-tool chmtime =$just_1_second_ago .git/sharedindex.* &&
+ create_non_racy_file sixteen &&
+ git update-index --add sixteen &&
+ test $(ls .git/sharedindex.* | wc -l) -le 2
+'
+
+test_expect_success POSIXPERM 'same mode for index & split index' '
+ git init same-mode &&
+ (
+ cd same-mode &&
+ test_commit A &&
+ test_modebits .git/index >index_mode &&
+ test_must_fail git config core.sharedRepository &&
+ git -c core.splitIndex=true status &&
+ shared=$(ls .git/sharedindex.*) &&
+ case "$shared" in
+ *" "*)
+ # we have more than one???
+ false ;;
+ *)
+ test_modebits "$shared" >split_index_mode &&
+ test_cmp index_mode split_index_mode ;;
+ esac
+ )
+'
+
+while read -r mode modebits
+do
+ test_expect_success POSIXPERM "split index respects core.sharedrepository $mode" '
+ # Remove existing shared index files
+ git config core.splitIndex false &&
+ git update-index --force-remove one &&
+ rm -f .git/sharedindex.* &&
+ # Create one new shared index file
+ git config core.sharedrepository "$mode" &&
+ git config core.splitIndex true &&
+ create_non_racy_file one &&
+ git update-index --add one &&
+ echo "$modebits" >expect &&
+ test_modebits .git/index >actual &&
+ test_cmp expect actual &&
+ shared=$(ls .git/sharedindex.*) &&
+ case "$shared" in
+ *" "*)
+ # we have more than one???
+ false ;;
+ *)
+ test_modebits "$shared" >actual &&
+ test_cmp expect actual ;;
+ esac
+ '
+done <<\EOF
+0666 -rw-rw-rw-
+0642 -rw-r---w-
+EOF
+
+test_expect_success POSIXPERM,SANITY 'graceful handling when splitting index is not allowed' '
+ test_create_repo ro &&
+ (
+ cd ro &&
+ test_commit initial &&
+ git update-index --split-index &&
+ test -f .git/sharedindex.*
+ ) &&
+ cp ro/.git/index new-index &&
+ test_when_finished "chmod u+w ro/.git" &&
+ chmod u-w ro/.git &&
+ GIT_INDEX_FILE="$(pwd)/new-index" git -C ro update-index --split-index &&
+ chmod u+w ro/.git &&
+ rm ro/.git/sharedindex.* &&
+ GIT_INDEX_FILE=new-index git ls-files >actual &&
+ echo initial.t >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'writing split index with null sha1 does not write cache tree' '
+ git config core.splitIndex true &&
+ git config splitIndex.maxPercentChange 0 &&
+ git commit -m "commit" &&
+ {
+ git ls-tree HEAD &&
+ printf "160000 commit $ZERO_OID\\tbroken\\n"
+ } >broken-tree &&
+ echo "add broken entry" >msg &&
+
+ tree=$(git mktree <broken-tree) &&
+ test_tick &&
+ commit=$(git commit-tree $tree -p HEAD <msg) &&
+ git update-ref HEAD "$commit" &&
+ GIT_ALLOW_NULL_SHA1=1 git reset --hard &&
+ test_might_fail test-tool dump-cache-tree >cache-tree.out &&
+ test_line_count = 0 cache-tree.out
+'
+
+test_expect_success 'do not refresh null base index' '
+ test_create_repo merge &&
+ (
+ cd merge &&
+ test_commit initial &&
+ git checkout -b side-branch &&
+ test_commit extra &&
+ git checkout main &&
+ git update-index --split-index &&
+ test_commit more &&
+ # must not write a new shareindex, or we wont catch the problem
+ git -c splitIndex.maxPercentChange=100 merge --no-edit side-branch 2>err &&
+ # i.e. do not expect warnings like
+ # could not freshen shared index .../shareindex.00000...
+ test_must_be_empty err
+ )
+'
+
+test_expect_success 'reading split index at alternate location' '
+ git init reading-alternate-location &&
+ (
+ cd reading-alternate-location &&
+ >file-in-alternate &&
+ git update-index --split-index --add file-in-alternate
+ ) &&
+ echo file-in-alternate >expect &&
+
+ # Should be able to find the shared index both right next to
+ # the specified split index file ...
+ GIT_INDEX_FILE=./reading-alternate-location/.git/index \
+ git ls-files --cached >actual &&
+ test_cmp expect actual &&
+
+ # ... and, for backwards compatibility, in the current GIT_DIR
+ # as well.
+ mv -v ./reading-alternate-location/.git/sharedindex.* .git &&
+ GIT_INDEX_FILE=./reading-alternate-location/.git/index \
+ git ls-files --cached >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'GIT_TEST_SPLIT_INDEX works' '
+ git init git-test-split-index &&
+ (
+ cd git-test-split-index &&
+ >file &&
+ GIT_TEST_SPLIT_INDEX=1 git update-index --add file &&
+ ls -l .git/sharedindex.* >actual &&
+ test_line_count = 1 actual
+ )
+'
+
+test_done
diff --git a/t/t1701-racy-split-index.sh b/t/t1701-racy-split-index.sh
new file mode 100755
index 0000000..d8fa489
--- /dev/null
+++ b/t/t1701-racy-split-index.sh
@@ -0,0 +1,215 @@
+#!/bin/sh
+
+# This test can give false success if your machine is sufficiently
+# slow or all trials happened to happen on second boundaries.
+
+test_description='racy split index'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ # Only split the index when the test explicitly says so.
+ sane_unset GIT_TEST_SPLIT_INDEX &&
+ git config splitIndex.maxPercentChange 100 &&
+
+ echo "cached content" >racy-file &&
+ git add racy-file &&
+ git commit -m initial &&
+
+ echo something >other-file &&
+ # No raciness with this file.
+ test-tool chmtime =-20 other-file &&
+
+ echo "+cached content" >expect
+'
+
+check_cached_diff () {
+ git diff-index --patch --cached $EMPTY_TREE racy-file >diff &&
+ tail -1 diff >actual &&
+ test_cmp expect actual
+}
+
+trials="0 1 2 3 4"
+for trial in $trials
+do
+ test_expect_success "split the index while adding a racily clean file #$trial" '
+ rm -f .git/index .git/sharedindex.* &&
+
+ # The next three commands must be run within the same
+ # second (so both writes to racy-file result in the same
+ # mtime) to create the interesting racy situation.
+ echo "cached content" >racy-file &&
+
+ # Update and split the index. The cache entry of
+ # racy-file will be stored only in the shared index.
+ git update-index --split-index --add racy-file &&
+
+ # File size must stay the same.
+ echo "dirty worktree" >racy-file &&
+
+ # Subsequent git commands should notice that racy-file
+ # and the split index have the same mtime, and check
+ # the content of the file to see if it is actually
+ # clean.
+ check_cached_diff
+ '
+done
+
+for trial in $trials
+do
+ test_expect_success "add a racily clean file to an already split index #$trial" '
+ rm -f .git/index .git/sharedindex.* &&
+
+ git update-index --split-index &&
+
+ # The next three commands must be run within the same
+ # second.
+ echo "cached content" >racy-file &&
+
+ # Update the split index. The cache entry of racy-file
+ # will be stored only in the split index.
+ git update-index --add racy-file &&
+
+ # File size must stay the same.
+ echo "dirty worktree" >racy-file &&
+
+ # Subsequent git commands should notice that racy-file
+ # and the split index have the same mtime, and check
+ # the content of the file to see if it is actually
+ # clean.
+ check_cached_diff
+ '
+done
+
+for trial in $trials
+do
+ test_expect_success "split the index when the index contains a racily clean cache entry #$trial" '
+ rm -f .git/index .git/sharedindex.* &&
+
+ # The next three commands must be run within the same
+ # second.
+ echo "cached content" >racy-file &&
+
+ git update-index --add racy-file &&
+
+ # File size must stay the same.
+ echo "dirty worktree" >racy-file &&
+
+ # Now wait a bit to ensure that the split index written
+ # below will get a more recent mtime than racy-file.
+ sleep 1 &&
+
+ # Update and split the index when the index contains
+ # the racily clean cache entry of racy-file.
+ # A corresponding replacement cache entry with smudged
+ # stat data should be added to the new split index.
+ git update-index --split-index --add other-file &&
+
+ # Subsequent git commands should notice the smudged
+ # stat data in the replacement cache entry and that it
+ # doesnt match with the file the worktree.
+ check_cached_diff
+ '
+done
+
+for trial in $trials
+do
+ test_expect_success "update the split index when it contains a new racily clean cache entry #$trial" '
+ rm -f .git/index .git/sharedindex.* &&
+
+ git update-index --split-index &&
+
+ # The next three commands must be run within the same
+ # second.
+ echo "cached content" >racy-file &&
+
+ # Update the split index. The cache entry of racy-file
+ # will be stored only in the split index.
+ git update-index --add racy-file &&
+
+ # File size must stay the same.
+ echo "dirty worktree" >racy-file &&
+
+ # Now wait a bit to ensure that the split index written
+ # below will get a more recent mtime than racy-file.
+ sleep 1 &&
+
+ # Update the split index when the racily clean cache
+ # entry of racy-file is only stored in the split index.
+ # An updated cache entry with smudged stat data should
+ # be added to the new split index.
+ git update-index --add other-file &&
+
+ # Subsequent git commands should notice the smudged
+ # stat data.
+ check_cached_diff
+ '
+done
+
+for trial in $trials
+do
+ test_expect_success "update the split index when a racily clean cache entry is stored only in the shared index #$trial" '
+ rm -f .git/index .git/sharedindex.* &&
+
+ # The next three commands must be run within the same
+ # second.
+ echo "cached content" >racy-file &&
+
+ # Update and split the index. The cache entry of
+ # racy-file will be stored only in the shared index.
+ git update-index --split-index --add racy-file &&
+
+ # File size must stay the same.
+ echo "dirty worktree" >racy-file &&
+
+ # Now wait a bit to ensure that the split index written
+ # below will get a more recent mtime than racy-file.
+ sleep 1 &&
+
+ # Update the split index when the racily clean cache
+ # entry of racy-file is only stored in the shared index.
+ # A corresponding replacement cache entry with smudged
+ # stat data should be added to the new split index.
+ git update-index --add other-file &&
+
+ # Subsequent git commands should notice the smudged
+ # stat data.
+ check_cached_diff
+ '
+done
+
+for trial in $trials
+do
+ test_expect_success "update the split index after unpack trees() copied a racily clean cache entry from the shared index #$trial" '
+ rm -f .git/index .git/sharedindex.* &&
+
+ # The next three commands must be run within the same
+ # second.
+ echo "cached content" >racy-file &&
+
+ # Update and split the index. The cache entry of
+ # racy-file will be stored only in the shared index.
+ git update-index --split-index --add racy-file &&
+
+ # File size must stay the same.
+ echo "dirty worktree" >racy-file &&
+
+ # Now wait a bit to ensure that the split index written
+ # below will get a more recent mtime than racy-file.
+ sleep 1 &&
+
+ # Update the split index after unpack_trees() copied the
+ # racily clean cache entry of racy-file from the shared
+ # index. A corresponding replacement cache entry
+ # with smudged stat data should be added to the new
+ # split index.
+ git read-tree -m HEAD &&
+
+ # Subsequent git commands should notice the smudged
+ # stat data.
+ check_cached_diff
+ '
+done
+
+test_done
diff --git a/t/t1800-hook.sh b/t/t1800-hook.sh
new file mode 100755
index 0000000..8b0234c
--- /dev/null
+++ b/t/t1800-hook.sh
@@ -0,0 +1,188 @@
+#!/bin/sh
+
+test_description='git-hook command'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-terminal.sh
+
+test_expect_success 'git hook usage' '
+ test_expect_code 129 git hook &&
+ test_expect_code 129 git hook run &&
+ test_expect_code 129 git hook run -h &&
+ test_expect_code 129 git hook run --unknown 2>err &&
+ grep "unknown option" err
+'
+
+test_expect_success 'git hook run: nonexistent hook' '
+ cat >stderr.expect <<-\EOF &&
+ error: cannot find a hook named test-hook
+ EOF
+ test_expect_code 1 git hook run test-hook 2>stderr.actual &&
+ test_cmp stderr.expect stderr.actual
+'
+
+test_expect_success 'git hook run: nonexistent hook with --ignore-missing' '
+ git hook run --ignore-missing does-not-exist 2>stderr.actual &&
+ test_must_be_empty stderr.actual
+'
+
+test_expect_success 'git hook run: basic' '
+ test_hook test-hook <<-EOF &&
+ echo Test hook
+ EOF
+
+ cat >expect <<-\EOF &&
+ Test hook
+ EOF
+ git hook run test-hook 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git hook run: stdout and stderr both write to our stderr' '
+ test_hook test-hook <<-EOF &&
+ echo >&1 Will end up on stderr
+ echo >&2 Will end up on stderr
+ EOF
+
+ cat >stderr.expect <<-\EOF &&
+ Will end up on stderr
+ Will end up on stderr
+ EOF
+ git hook run test-hook >stdout.actual 2>stderr.actual &&
+ test_cmp stderr.expect stderr.actual &&
+ test_must_be_empty stdout.actual
+'
+
+for code in 1 2 128 129
+do
+ test_expect_success "git hook run: exit code $code is passed along" '
+ test_hook test-hook <<-EOF &&
+ exit $code
+ EOF
+
+ test_expect_code $code git hook run test-hook
+ '
+done
+
+test_expect_success 'git hook run arg u ments without -- is not allowed' '
+ test_expect_code 129 git hook run test-hook arg u ments
+'
+
+test_expect_success 'git hook run -- pass arguments' '
+ test_hook test-hook <<-\EOF &&
+ echo $1
+ echo $2
+ EOF
+
+ cat >expect <<-EOF &&
+ arg
+ u ments
+ EOF
+
+ git hook run test-hook -- arg "u ments" 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git hook run -- out-of-repo runs excluded' '
+ test_hook test-hook <<-EOF &&
+ echo Test hook
+ EOF
+
+ nongit test_must_fail git hook run test-hook
+'
+
+test_expect_success 'git -c core.hooksPath=<PATH> hook run' '
+ mkdir my-hooks &&
+ write_script my-hooks/test-hook <<-\EOF &&
+ echo Hook ran $1
+ EOF
+
+ cat >expect <<-\EOF &&
+ Test hook
+ Hook ran one
+ Hook ran two
+ Hook ran three
+ Hook ran four
+ EOF
+
+ test_hook test-hook <<-EOF &&
+ echo Test hook
+ EOF
+
+ # Test various ways of specifying the path. See also
+ # t1350-config-hooks-path.sh
+ >actual &&
+ git hook run test-hook -- ignored 2>>actual &&
+ git -c core.hooksPath=my-hooks hook run test-hook -- one 2>>actual &&
+ git -c core.hooksPath=my-hooks/ hook run test-hook -- two 2>>actual &&
+ git -c core.hooksPath="$PWD/my-hooks" hook run test-hook -- three 2>>actual &&
+ git -c core.hooksPath="$PWD/my-hooks/" hook run test-hook -- four 2>>actual &&
+ test_cmp expect actual
+'
+
+test_hook_tty () {
+ cat >expect <<-\EOF
+ STDOUT TTY
+ STDERR TTY
+ EOF
+
+ test_when_finished "rm -rf repo" &&
+ git init repo &&
+
+ test_commit -C repo A &&
+ test_commit -C repo B &&
+ git -C repo reset --soft HEAD^ &&
+
+ test_hook -C repo pre-commit <<-EOF &&
+ test -t 1 && echo STDOUT TTY >>actual || echo STDOUT NO TTY >>actual &&
+ test -t 2 && echo STDERR TTY >>actual || echo STDERR NO TTY >>actual
+ EOF
+
+ test_terminal git -C repo "$@" &&
+ test_cmp expect repo/actual
+}
+
+test_expect_success TTY 'git hook run: stdout and stderr are connected to a TTY' '
+ test_hook_tty hook run pre-commit
+'
+
+test_expect_success TTY 'git commit: stdout and stderr are connected to a TTY' '
+ test_hook_tty commit -m"B.new"
+'
+
+test_expect_success 'git hook run a hook with a bad shebang' '
+ test_when_finished "rm -rf bad-hooks" &&
+ mkdir bad-hooks &&
+ write_script bad-hooks/test-hook "/bad/path/no/spaces" </dev/null &&
+
+ test_expect_code 1 git \
+ -c core.hooksPath=bad-hooks \
+ hook run test-hook >out 2>err &&
+ test_must_be_empty out &&
+
+ # TODO: We should emit the same (or at least a more similar)
+ # error on MINGW (essentially Git for Windows) and all other
+ # platforms.. See the OS-specific code in start_command()
+ grep -E "^(error|fatal): cannot (exec|spawn) .*bad-hooks/test-hook" err
+'
+
+test_expect_success 'stdin to hooks' '
+ write_script .git/hooks/test-hook <<-\EOF &&
+ echo BEGIN stdin
+ cat
+ echo END stdin
+ EOF
+
+ cat >expect <<-EOF &&
+ BEGIN stdin
+ hello
+ END stdin
+ EOF
+
+ echo hello >input &&
+ git hook run --to-stdin=input test-hook 2>actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t2000-conflict-when-checking-files-out.sh b/t/t2000-conflict-when-checking-files-out.sh
new file mode 100755
index 0000000..79fc97f
--- /dev/null
+++ b/t/t2000-conflict-when-checking-files-out.sh
@@ -0,0 +1,136 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+test_description='git conflicts when checking files out test.'
+
+# The first test registers the following filesystem structure in the
+# cache:
+#
+# path0 - a file
+# path1/file1 - a file in a directory
+#
+# And then tries to checkout in a work tree that has the following:
+#
+# path0/file0 - a file in a directory
+# path1 - a file
+#
+# The git checkout-index command should fail when attempting to checkout
+# path0, finding it is occupied by a directory, and path1/file1, finding
+# path1 is occupied by a non-directory. With "-f" flag, it should remove
+# the conflicting paths and succeed.
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+show_files() {
+ # show filesystem files, just [-dl] for type and name
+ find path? -ls |
+ sed -e 's/^[0-9]* * [0-9]* * \([-bcdl]\)[^ ]* *[0-9]* *[^ ]* *[^ ]* *[0-9]* [A-Z][a-z][a-z] [0-9][0-9] [^ ]* /fs: \1 /'
+ # what's in the cache, just mode and name
+ git ls-files --stage |
+ sed -e 's/^\([0-9]*\) [0-9a-f]* [0-3] /ca: \1 /'
+ # what's in the tree, just mode and name.
+ git ls-tree -r "$1" |
+ sed -e 's/^\([0-9]*\) [^ ]* [0-9a-f]* /tr: \1 /'
+}
+
+date >path0
+mkdir path1
+date >path1/file1
+
+test_expect_success \
+ 'git update-index --add various paths.' \
+ 'git update-index --add path0 path1/file1'
+
+rm -fr path0 path1
+mkdir path0
+date >path0/file0
+date >path1
+
+test_expect_success \
+ 'git checkout-index without -f should fail on conflicting work tree.' \
+ 'test_must_fail git checkout-index -a'
+
+test_expect_success \
+ 'git checkout-index with -f should succeed.' \
+ 'git checkout-index -f -a'
+
+test_expect_success \
+ 'git checkout-index conflicting paths.' \
+ 'test -f path0 && test -d path1 && test -f path1/file1'
+
+test_expect_success SYMLINKS 'checkout-index -f twice with --prefix' '
+ mkdir -p tar/get &&
+ ln -s tar/get there &&
+ echo first &&
+ git checkout-index -a -f --prefix=there/ &&
+ echo second &&
+ git checkout-index -a -f --prefix=there/
+'
+
+# The second test registers the following filesystem structure in the cache:
+#
+# path2/file0 - a file in a directory
+# path3/file1 - a file in a directory
+#
+# and attempts to check it out when the work tree has:
+#
+# path2/file0 - a file in a directory
+# path3 - a symlink pointing at "path2"
+#
+# Checkout cache should fail to extract path3/file1 because the leading
+# path path3 is occupied by a non-directory. With "-f" it should remove
+# the symlink path3 and create directory path3 and file path3/file1.
+
+mkdir path2
+date >path2/file0
+test_expect_success \
+ 'git update-index --add path2/file0' \
+ 'git update-index --add path2/file0'
+test_expect_success \
+ 'writing tree out with git write-tree' \
+ 'tree1=$(git write-tree)'
+test_debug 'show_files $tree1'
+
+mkdir path3
+date >path3/file1
+test_expect_success \
+ 'git update-index --add path3/file1' \
+ 'git update-index --add path3/file1'
+test_expect_success \
+ 'writing tree out with git write-tree' \
+ 'tree2=$(git write-tree)'
+test_debug 'show_files $tree2'
+
+rm -fr path3
+test_expect_success \
+ 'read previously written tree and checkout.' \
+ 'git read-tree -m $tree1 && git checkout-index -f -a'
+test_debug 'show_files $tree1'
+
+test_expect_success \
+ 'add a symlink' \
+ 'test_ln_s_add path2 path3'
+test_expect_success \
+ 'writing tree out with git write-tree' \
+ 'tree3=$(git write-tree)'
+test_debug 'show_files $tree3'
+
+# Morten says "Got that?" here.
+# Test begins.
+
+test_expect_success \
+ 'read previously written tree and checkout.' \
+ 'git read-tree $tree2 && git checkout-index -f -a'
+test_debug 'show_files $tree2'
+
+test_expect_success \
+ 'checking out conflicting path with -f' \
+ 'test ! -h path2 && test -d path2 &&
+ test ! -h path3 && test -d path3 &&
+ test ! -h path2/file0 && test -f path2/file0 &&
+ test ! -h path3/file1 && test -f path3/file1'
+
+test_done
diff --git a/t/t2002-checkout-cache-u.sh b/t/t2002-checkout-cache-u.sh
new file mode 100755
index 0000000..fc95cf9
--- /dev/null
+++ b/t/t2002-checkout-cache-u.sh
@@ -0,0 +1,34 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+test_description='git checkout-index -u test.
+
+With -u flag, git checkout-index internally runs the equivalent of
+git update-index --refresh on the checked out entry.'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success \
+'preparation' '
+echo frotz >path0 &&
+git update-index --add path0 &&
+t=$(git write-tree)'
+
+test_expect_success \
+'without -u, git checkout-index smudges stat information.' '
+rm -f path0 &&
+git read-tree $t &&
+git checkout-index -f -a &&
+test_must_fail git diff-files --exit-code'
+
+test_expect_success \
+'with -u, git checkout-index picks up stat information from new files.' '
+rm -f path0 &&
+git read-tree $t &&
+git checkout-index -u -f -a &&
+git diff-files --exit-code'
+
+test_done
diff --git a/t/t2003-checkout-cache-mkdir.sh b/t/t2003-checkout-cache-mkdir.sh
new file mode 100755
index 0000000..f0fd441
--- /dev/null
+++ b/t/t2003-checkout-cache-mkdir.sh
@@ -0,0 +1,120 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+test_description='git checkout-index --prefix test.
+
+This test makes sure that --prefix option works as advertised, and
+also verifies that such leading path may contain symlinks, unlike
+the GIT controlled paths.
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ mkdir path1 &&
+ echo frotz >path0 &&
+ echo rezrov >path1/file1 &&
+ git update-index --add path0 path1/file1
+'
+
+test_expect_success SYMLINKS 'have symlink in place where dir is expected.' '
+ rm -fr path0 path1 &&
+ mkdir path2 &&
+ ln -s path2 path1 &&
+ git checkout-index -f -a &&
+ test ! -h path1 && test -d path1 &&
+ test -f path1/file1 && test ! -f path2/file1
+'
+
+test_expect_success 'use --prefix=path2/' '
+ rm -fr path0 path1 path2 &&
+ mkdir path2 &&
+ git checkout-index --prefix=path2/ -f -a &&
+ test -f path2/path0 &&
+ test -f path2/path1/file1 &&
+ test ! -f path0 &&
+ test ! -f path1/file1
+'
+
+test_expect_success 'use --prefix=tmp-' '
+ rm -fr path0 path1 path2 tmp* &&
+ git checkout-index --prefix=tmp- -f -a &&
+ test -f tmp-path0 &&
+ test -f tmp-path1/file1 &&
+ test ! -f path0 &&
+ test ! -f path1/file1
+'
+
+test_expect_success 'use --prefix=tmp- but with a conflicting file and dir' '
+ rm -fr path0 path1 path2 tmp* &&
+ echo nitfol >tmp-path1 &&
+ mkdir tmp-path0 &&
+ git checkout-index --prefix=tmp- -f -a &&
+ test -f tmp-path0 &&
+ test -f tmp-path1/file1 &&
+ test ! -f path0 &&
+ test ! -f path1/file1
+'
+
+test_expect_success SYMLINKS 'use --prefix=tmp/orary/ where tmp is a symlink' '
+ rm -fr path0 path1 path2 tmp* &&
+ mkdir tmp1 tmp1/orary &&
+ ln -s tmp1 tmp &&
+ git checkout-index --prefix=tmp/orary/ -f -a &&
+ test -d tmp1/orary &&
+ test -f tmp1/orary/path0 &&
+ test -f tmp1/orary/path1/file1 &&
+ test -h tmp
+'
+
+test_expect_success SYMLINKS 'use --prefix=tmp/orary- where tmp is a symlink' '
+ rm -fr path0 path1 path2 tmp* &&
+ mkdir tmp1 &&
+ ln -s tmp1 tmp &&
+ git checkout-index --prefix=tmp/orary- -f -a &&
+ test -f tmp1/orary-path0 &&
+ test -f tmp1/orary-path1/file1 &&
+ test -h tmp
+'
+
+test_expect_success SYMLINKS 'use --prefix=tmp- where tmp-path1 is a symlink' '
+ rm -fr path0 path1 path2 tmp* &&
+ mkdir tmp1 &&
+ ln -s tmp1 tmp-path1 &&
+ git checkout-index --prefix=tmp- -f -a &&
+ test -f tmp-path0 &&
+ test ! -h tmp-path1 &&
+ test -d tmp-path1 &&
+ test -f tmp-path1/file1
+'
+
+test_expect_success 'apply filter from working tree .gitattributes with --prefix' '
+ rm -fr path0 path1 path2 tmp* &&
+ mkdir path1 &&
+ mkdir tmp &&
+ git config filter.replace-all.smudge "sed -e s/./,/g" &&
+ git config filter.replace-all.clean cat &&
+ git config filter.replace-all.required true &&
+ echo "file1 filter=replace-all" >path1/.gitattributes &&
+ git checkout-index --prefix=tmp/ -f -a &&
+ echo frotz >expected &&
+ test_cmp expected tmp/path0 &&
+ echo ,,,,,, >expected &&
+ test_cmp expected tmp/path1/file1
+'
+
+test_expect_success 'apply CRLF filter from working tree .gitattributes with --prefix' '
+ rm -fr path0 path1 path2 tmp* &&
+ mkdir path1 &&
+ mkdir tmp &&
+ echo "file1 eol=crlf" >path1/.gitattributes &&
+ git checkout-index --prefix=tmp/ -f -a &&
+ echo rezrovQ >expected &&
+ tr \\015 Q <tmp/path1/file1 >actual &&
+ test_cmp expected actual
+'
+
+test_done
diff --git a/t/t2004-checkout-cache-temp.sh b/t/t2004-checkout-cache-temp.sh
new file mode 100755
index 0000000..98e818f
--- /dev/null
+++ b/t/t2004-checkout-cache-temp.sh
@@ -0,0 +1,250 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Shawn Pearce
+#
+
+test_description='git checkout-index --temp test.
+
+With --temp flag, git checkout-index writes to temporary merge files
+rather than the tracked path.'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ mkdir asubdir &&
+ echo tree1path0 >path0 &&
+ echo tree1path1 >path1 &&
+ echo tree1path3 >path3 &&
+ echo tree1path4 >path4 &&
+ echo tree1asubdir/path5 >asubdir/path5 &&
+ git update-index --add path0 path1 path3 path4 asubdir/path5 &&
+ t1=$(git write-tree) &&
+ rm -f path* .merge_* actual .git/index &&
+ echo tree2path0 >path0 &&
+ echo tree2path1 >path1 &&
+ echo tree2path2 >path2 &&
+ echo tree2path4 >path4 &&
+ git update-index --add path0 path1 path2 path4 &&
+ t2=$(git write-tree) &&
+ rm -f path* .merge_* actual .git/index &&
+ echo tree2path0 >path0 &&
+ echo tree3path1 >path1 &&
+ echo tree3path2 >path2 &&
+ echo tree3path3 >path3 &&
+ git update-index --add path0 path1 path2 path3 &&
+ t3=$(git write-tree)
+'
+
+test_expect_success 'checkout one stage 0 to temporary file' '
+ rm -f path* .merge_* actual .git/index &&
+ git read-tree $t1 &&
+ git checkout-index --temp -- path1 >actual &&
+ test_line_count = 1 actual &&
+ test $(cut "-d " -f2 actual) = path1 &&
+ p=$(cut "-d " -f1 actual) &&
+ test -f $p &&
+ test $(cat $p) = tree1path1
+'
+
+test_expect_success 'checkout all stage 0 to temporary files' '
+ rm -f path* .merge_* actual .git/index &&
+ git read-tree $t1 &&
+ git checkout-index -a --temp >actual &&
+ test_line_count = 5 actual &&
+ for f in path0 path1 path3 path4 asubdir/path5
+ do
+ test $(grep $f actual | cut "-d " -f2) = $f &&
+ p=$(grep $f actual | cut "-d " -f1) &&
+ test -f $p &&
+ test $(cat $p) = tree1$f || return 1
+ done
+'
+
+test_expect_success 'setup 3-way merge' '
+ rm -f path* .merge_* actual .git/index &&
+ git read-tree -m $t1 $t2 $t3
+'
+
+test_expect_success 'checkout one stage 2 to temporary file' '
+ rm -f path* .merge_* actual &&
+ git checkout-index --stage=2 --temp -- path1 >actual &&
+ test_line_count = 1 actual &&
+ test $(cut "-d " -f2 actual) = path1 &&
+ p=$(cut "-d " -f1 actual) &&
+ test -f $p &&
+ test $(cat $p) = tree2path1
+'
+
+test_expect_success 'checkout all stage 2 to temporary files' '
+ rm -f path* .merge_* actual &&
+ git checkout-index --all --stage=2 --temp >actual &&
+ test_line_count = 3 actual &&
+ for f in path1 path2 path4
+ do
+ test $(grep $f actual | cut "-d " -f2) = $f &&
+ p=$(grep $f actual | cut "-d " -f1) &&
+ test -f $p &&
+ test $(cat $p) = tree2$f || return 1
+ done
+'
+
+test_expect_success 'checkout all stages of unknown path' '
+ rm -f path* .merge_* actual &&
+ test_must_fail git checkout-index --stage=all --temp \
+ -- does-not-exist 2>stderr &&
+ test_grep not.in.the.cache stderr
+'
+
+test_expect_success 'checkout all stages/one file to nothing' '
+ rm -f path* .merge_* actual &&
+ git checkout-index --stage=all --temp -- path0 >actual 2>stderr &&
+ test_must_be_empty stderr &&
+ test_line_count = 0 actual
+'
+
+test_expect_success 'checkout all stages/one file to temporary files' '
+ rm -f path* .merge_* actual &&
+ git checkout-index --stage=all --temp -- path1 >actual &&
+ test_line_count = 1 actual &&
+ test $(cut "-d " -f2 actual) = path1 &&
+ cut "-d " -f1 actual | (read s1 s2 s3 &&
+ test -f $s1 &&
+ test -f $s2 &&
+ test -f $s3 &&
+ test $(cat $s1) = tree1path1 &&
+ test $(cat $s2) = tree2path1 &&
+ test $(cat $s3) = tree3path1)
+'
+
+test_expect_success '--stage=all implies --temp' '
+ rm -f path* .merge_* actual &&
+ git checkout-index --stage=all -- path1 &&
+ test_path_is_missing path1
+'
+
+test_expect_success 'overriding --stage=all resets implied --temp' '
+ rm -f path* .merge_* actual &&
+ git checkout-index --stage=all --stage=2 -- path1 &&
+ echo tree2path1 >expect &&
+ test_cmp expect path1
+'
+
+test_expect_success '--stage=all --no-temp is rejected' '
+ rm -f path* .merge_* actual &&
+ test_must_fail git checkout-index --stage=all --no-temp -- path1 2>err &&
+ grep -v "already exists" err &&
+ grep "options .--stage=all. and .--no-temp. cannot be used together" err
+'
+
+test_expect_success 'checkout some stages/one file to temporary files' '
+ rm -f path* .merge_* actual &&
+ git checkout-index --stage=all --temp -- path2 >actual &&
+ test_line_count = 1 actual &&
+ test $(cut "-d " -f2 actual) = path2 &&
+ cut "-d " -f1 actual | (read s1 s2 s3 &&
+ test $s1 = . &&
+ test -f $s2 &&
+ test -f $s3 &&
+ test $(cat $s2) = tree2path2 &&
+ test $(cat $s3) = tree3path2)
+'
+
+test_expect_success 'checkout all stages/all files to temporary files' '
+ rm -f path* .merge_* actual &&
+ git checkout-index -a --stage=all --temp >actual &&
+ test_line_count = 5 actual
+'
+
+test_expect_success '-- path0: no entry' '
+ test x$(grep path0 actual | cut "-d " -f2) = x
+'
+
+test_expect_success '-- path1: all 3 stages' '
+ test $(grep path1 actual | cut "-d " -f2) = path1 &&
+ grep path1 actual | cut "-d " -f1 | (read s1 s2 s3 &&
+ test -f $s1 &&
+ test -f $s2 &&
+ test -f $s3 &&
+ test $(cat $s1) = tree1path1 &&
+ test $(cat $s2) = tree2path1 &&
+ test $(cat $s3) = tree3path1)
+'
+
+test_expect_success '-- path2: no stage 1, have stage 2 and 3' '
+ test $(grep path2 actual | cut "-d " -f2) = path2 &&
+ grep path2 actual | cut "-d " -f1 | (read s1 s2 s3 &&
+ test $s1 = . &&
+ test -f $s2 &&
+ test -f $s3 &&
+ test $(cat $s2) = tree2path2 &&
+ test $(cat $s3) = tree3path2)
+'
+
+test_expect_success '-- path3: no stage 2, have stage 1 and 3' '
+ test $(grep path3 actual | cut "-d " -f2) = path3 &&
+ grep path3 actual | cut "-d " -f1 | (read s1 s2 s3 &&
+ test -f $s1 &&
+ test $s2 = . &&
+ test -f $s3 &&
+ test $(cat $s1) = tree1path3 &&
+ test $(cat $s3) = tree3path3)
+'
+
+test_expect_success '-- path4: no stage 3, have stage 1 and 3' '
+ test $(grep path4 actual | cut "-d " -f2) = path4 &&
+ grep path4 actual | cut "-d " -f1 | (read s1 s2 s3 &&
+ test -f $s1 &&
+ test -f $s2 &&
+ test $s3 = . &&
+ test $(cat $s1) = tree1path4 &&
+ test $(cat $s2) = tree2path4)
+'
+
+test_expect_success '-- asubdir/path5: no stage 2 and 3 have stage 1' '
+ test $(grep asubdir/path5 actual | cut "-d " -f2) = asubdir/path5 &&
+ grep asubdir/path5 actual | cut "-d " -f1 | (read s1 s2 s3 &&
+ test -f $s1 &&
+ test $s2 = . &&
+ test $s3 = . &&
+ test $(cat $s1) = tree1asubdir/path5)
+'
+
+test_expect_success 'checkout --temp within subdir' '
+ (
+ cd asubdir &&
+ git checkout-index -a --stage=all >actual &&
+ test_line_count = 1 actual &&
+ test $(grep path5 actual | cut "-d " -f2) = path5 &&
+ grep path5 actual | cut "-d " -f1 | (read s1 s2 s3 &&
+ test -f ../$s1 &&
+ test $s2 = . &&
+ test $s3 = . &&
+ test $(cat ../$s1) = tree1asubdir/path5)
+ )
+'
+
+test_expect_success 'checkout --temp symlink' '
+ rm -f path* .merge_* actual .git/index &&
+ test_ln_s_add path7 path6 &&
+ git checkout-index --temp -a >actual &&
+ test_line_count = 1 actual &&
+ test $(cut "-d " -f2 actual) = path6 &&
+ p=$(cut "-d " -f1 actual) &&
+ test -f $p &&
+ test $(cat $p) = path7
+'
+
+test_expect_success 'emit well-formed relative path' '
+ rm -f path* .merge_* actual .git/index &&
+ >path0123456789 &&
+ git update-index --add path0123456789 &&
+ (
+ cd asubdir &&
+ git checkout-index --temp -- ../path0123456789 >actual &&
+ test_line_count = 1 actual &&
+ test $(cut "-d " -f2 actual) = ../path0123456789
+ )
+'
+
+test_done
diff --git a/t/t2005-checkout-index-symlinks.sh b/t/t2005-checkout-index-symlinks.sh
new file mode 100755
index 0000000..67d18cf
--- /dev/null
+++ b/t/t2005-checkout-index-symlinks.sh
@@ -0,0 +1,31 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Johannes Sixt
+#
+
+test_description='git checkout-index on filesystem w/o symlinks test.
+
+This tests that git checkout-index creates a symbolic link as a plain
+file if core.symlinks is false.'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success \
+'preparation' '
+git config core.symlinks false &&
+l=$(printf file | git hash-object -t blob -w --stdin) &&
+echo "120000 $l symlink" | git update-index --index-info'
+
+test_expect_success \
+'the checked-out symlink must be a file' '
+git checkout-index symlink &&
+test -f symlink'
+
+test_expect_success 'the file must be the blob we added during the setup' '
+ echo "$l" >expect &&
+ git hash-object -t blob symlink >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t2006-checkout-index-basic.sh b/t/t2006-checkout-index-basic.sh
new file mode 100755
index 0000000..570ba38
--- /dev/null
+++ b/t/t2006-checkout-index-basic.sh
@@ -0,0 +1,104 @@
+#!/bin/sh
+
+test_description='basic checkout-index tests
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'checkout-index --gobbledegook' '
+ test_expect_code 129 git checkout-index --gobbledegook 2>err &&
+ test_grep "[Uu]sage" err
+'
+
+test_expect_success 'checkout-index -h in broken repository' '
+ mkdir broken &&
+ (
+ cd broken &&
+ git init &&
+ >.git/index &&
+ test_expect_code 129 git checkout-index -h >usage 2>&1
+ ) &&
+ test_grep "[Uu]sage" broken/usage
+'
+
+test_expect_success 'checkout-index reports errors (cmdline)' '
+ test_must_fail git checkout-index -- does-not-exist 2>stderr &&
+ test_grep not.in.the.cache stderr
+'
+
+test_expect_success 'checkout-index reports errors (stdin)' '
+ echo does-not-exist |
+ test_must_fail git checkout-index --stdin 2>stderr &&
+ test_grep not.in.the.cache stderr
+'
+for mode in 'case' 'utf-8'
+do
+ case "$mode" in
+ case) dir='A' symlink='a' mode_prereq='CASE_INSENSITIVE_FS' ;;
+ utf-8)
+ dir=$(printf "\141\314\210") symlink=$(printf "\303\244")
+ mode_prereq='UTF8_NFD_TO_NFC' ;;
+ esac
+
+ test_expect_success SYMLINKS,$mode_prereq \
+ "checkout-index with $mode-collision don't write to the wrong place" '
+ git init $mode-collision &&
+ (
+ cd $mode-collision &&
+ mkdir target-dir &&
+
+ empty_obj_hex=$(git hash-object -w --stdin </dev/null) &&
+ symlink_hex=$(printf "%s" "$PWD/target-dir" | git hash-object -w --stdin) &&
+
+ cat >objs <<-EOF &&
+ 100644 blob ${empty_obj_hex} ${dir}/x
+ 100644 blob ${empty_obj_hex} ${dir}/y
+ 100644 blob ${empty_obj_hex} ${dir}/z
+ 120000 blob ${symlink_hex} ${symlink}
+ EOF
+
+ git update-index --index-info <objs &&
+
+ # Note: the order is important here to exercise the
+ # case where the file at ${dir} has its type changed by
+ # the time Git tries to check out ${dir}/z.
+ #
+ # Also, we use core.precomposeUnicode=false because we
+ # want Git to treat the UTF-8 paths transparently on
+ # Mac OS, matching what is in the index.
+ #
+ git -c core.precomposeUnicode=false checkout-index -f \
+ ${dir}/x ${dir}/y ${symlink} ${dir}/z &&
+
+ # Should not create ${dir}/z at ${symlink}/z
+ test_path_is_missing target-dir/z
+
+ )
+ '
+done
+
+test_expect_success 'checkout-index --temp correctly reports error on missing blobs' '
+ test_when_finished git reset --hard &&
+ missing_blob=$(echo "no such blob here" | git hash-object --stdin) &&
+ cat >objs <<-EOF &&
+ 100644 $missing_blob file
+ 120000 $missing_blob symlink
+ EOF
+ git update-index --index-info <objs &&
+
+ test_must_fail git checkout-index --temp symlink file 2>stderr &&
+ test_grep "unable to read sha1 file of file ($missing_blob)" stderr &&
+ test_grep "unable to read sha1 file of symlink ($missing_blob)" stderr
+'
+
+test_expect_success 'checkout-index --temp correctly reports error for submodules' '
+ git init sub &&
+ test_commit -C sub file &&
+ git submodule add ./sub &&
+ git commit -m sub &&
+ test_must_fail git checkout-index --temp sub 2>stderr &&
+ test_grep "cannot create temporary submodule sub" stderr
+'
+
+test_done
diff --git a/t/t2007-checkout-symlink.sh b/t/t2007-checkout-symlink.sh
new file mode 100755
index 0000000..bd9e9e7
--- /dev/null
+++ b/t/t2007-checkout-symlink.sh
@@ -0,0 +1,56 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Junio C Hamano
+
+test_description='git checkout to switch between branches with symlink<->dir'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+
+ mkdir frotz &&
+ echo hello >frotz/filfre &&
+ git add frotz/filfre &&
+ test_tick &&
+ git commit -m "main has file frotz/filfre" &&
+
+ git branch side &&
+
+ echo goodbye >nitfol &&
+ git add nitfol &&
+ test_tick &&
+ git commit -m "main adds file nitfol" &&
+
+ git checkout side &&
+
+ git rm --cached frotz/filfre &&
+ mv frotz xyzzy &&
+ test_ln_s_add xyzzy frotz &&
+ git add xyzzy/filfre &&
+ test_tick &&
+ git commit -m "side moves frotz/ to xyzzy/ and adds frotz->xyzzy/"
+
+'
+
+test_expect_success 'switch from symlink to dir' '
+
+ git checkout main
+
+'
+
+test_expect_success 'Remove temporary directories & switch to main' '
+ rm -fr frotz xyzzy nitfol &&
+ git checkout -f main
+'
+
+test_expect_success 'switch from dir to symlink' '
+
+ git checkout side
+
+'
+
+test_done
diff --git a/t/t2008-checkout-subdir.sh b/t/t2008-checkout-subdir.sh
new file mode 100755
index 0000000..8a518a4
--- /dev/null
+++ b/t/t2008-checkout-subdir.sh
@@ -0,0 +1,83 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 David Symonds
+
+test_description='git checkout from subdirectories'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+
+ echo "base" > file0 &&
+ git add file0 &&
+ mkdir dir1 &&
+ echo "hello" > dir1/file1 &&
+ git add dir1/file1 &&
+ mkdir dir2 &&
+ echo "bonjour" > dir2/file2 &&
+ git add dir2/file2 &&
+ test_tick &&
+ git commit -m "populate tree"
+
+'
+
+test_expect_success 'remove and restore with relative path' '
+
+ (
+ cd dir1 &&
+ rm ../file0 &&
+ git checkout HEAD -- ../file0 &&
+ test "base" = "$(cat ../file0)" &&
+ rm ../dir2/file2 &&
+ git checkout HEAD -- ../dir2/file2 &&
+ test "bonjour" = "$(cat ../dir2/file2)" &&
+ rm ../file0 ./file1 &&
+ git checkout HEAD -- .. &&
+ test "base" = "$(cat ../file0)" &&
+ test "hello" = "$(cat file1)"
+ )
+
+'
+
+test_expect_success 'checkout with empty prefix' '
+
+ rm file0 &&
+ git checkout HEAD -- file0 &&
+ test "base" = "$(cat file0)"
+
+'
+
+test_expect_success 'checkout with simple prefix' '
+
+ rm dir1/file1 &&
+ git checkout HEAD -- dir1 &&
+ test "hello" = "$(cat dir1/file1)" &&
+ rm dir1/file1 &&
+ git checkout HEAD -- dir1/file1 &&
+ test "hello" = "$(cat dir1/file1)"
+
+'
+
+test_expect_success 'checkout with complex relative path' '
+ (
+ cd dir1 &&
+ rm file1 &&
+ git checkout HEAD -- ../dir1/../dir1/file1 &&
+ test "hello" = "$(cat file1)"
+ )
+'
+
+test_expect_success 'relative path outside tree should fail' \
+ 'test_must_fail git checkout HEAD -- ../../Makefile'
+
+test_expect_success 'incorrect relative path to file should fail (1)' \
+ 'test_must_fail git checkout HEAD -- ../file0'
+
+test_expect_success 'incorrect relative path should fail (2)' \
+ '( cd dir1 && test_must_fail git checkout HEAD -- ./file0 )'
+
+test_expect_success 'incorrect relative path should fail (3)' \
+ '( cd dir1 && test_must_fail git checkout HEAD -- ../../file0 )'
+
+test_done
diff --git a/t/t2009-checkout-statinfo.sh b/t/t2009-checkout-statinfo.sh
new file mode 100755
index 0000000..71195dd
--- /dev/null
+++ b/t/t2009-checkout-statinfo.sh
@@ -0,0 +1,56 @@
+#!/bin/sh
+
+test_description='checkout should leave clean stat info'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+
+ echo hello >world &&
+ git update-index --add world &&
+ git commit -m initial &&
+ git branch side &&
+ echo goodbye >world &&
+ git update-index --add world &&
+ git commit -m second
+
+'
+
+test_expect_success 'branch switching' '
+
+ git reset --hard &&
+ test "$(git diff-files --raw)" = "" &&
+
+ git checkout main &&
+ test "$(git diff-files --raw)" = "" &&
+
+ git checkout side &&
+ test "$(git diff-files --raw)" = "" &&
+
+ git checkout main &&
+ test "$(git diff-files --raw)" = ""
+
+'
+
+test_expect_success 'path checkout' '
+
+ git reset --hard &&
+ test "$(git diff-files --raw)" = "" &&
+
+ git checkout main world &&
+ test "$(git diff-files --raw)" = "" &&
+
+ git checkout side world &&
+ test "$(git diff-files --raw)" = "" &&
+
+ git checkout main world &&
+ test "$(git diff-files --raw)" = ""
+
+'
+
+test_done
+
diff --git a/t/t2010-checkout-ambiguous.sh b/t/t2010-checkout-ambiguous.sh
new file mode 100755
index 0000000..82c3bfe
--- /dev/null
+++ b/t/t2010-checkout-ambiguous.sh
@@ -0,0 +1,69 @@
+#!/bin/sh
+
+test_description='checkout and pathspecs/refspecs ambiguities'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ echo hello >world &&
+ echo hello >all &&
+ git add all world &&
+ git commit -m initial &&
+ git branch world
+'
+
+test_expect_success 'reference must be a tree' '
+ test_must_fail git checkout $(git hash-object ./all) --
+'
+
+test_expect_success 'branch switching' '
+ test "refs/heads/main" = "$(git symbolic-ref HEAD)" &&
+ git checkout world -- &&
+ test "refs/heads/world" = "$(git symbolic-ref HEAD)"
+'
+
+test_expect_success 'checkout world from the index' '
+ echo bye > world &&
+ git checkout -- world &&
+ git diff --exit-code --quiet
+'
+
+test_expect_success 'non ambiguous call' '
+ git checkout all
+'
+
+test_expect_success 'allow the most common case' '
+ git checkout world &&
+ test "refs/heads/world" = "$(git symbolic-ref HEAD)"
+'
+
+test_expect_success 'check ambiguity' '
+ test_must_fail git checkout world all
+'
+
+test_expect_success 'check ambiguity in subdir' '
+ mkdir sub &&
+ # not ambiguous because sub/world does not exist
+ git -C sub checkout world ../all &&
+ echo hello >sub/world &&
+ # ambiguous because sub/world does exist
+ test_must_fail git -C sub checkout world ../all
+'
+
+test_expect_success 'disambiguate checking out from a tree-ish' '
+ echo bye > world &&
+ git checkout world -- world &&
+ git diff --exit-code --quiet
+'
+
+test_expect_success 'accurate error message with more than one ref' '
+ test_must_fail git checkout HEAD main -- 2>actual &&
+ test_grep 2 actual &&
+ test_grep "one reference expected, 2 given" actual
+'
+
+test_done
diff --git a/t/t2011-checkout-invalid-head.sh b/t/t2011-checkout-invalid-head.sh
new file mode 100755
index 0000000..d9997e7
--- /dev/null
+++ b/t/t2011-checkout-invalid-head.sh
@@ -0,0 +1,65 @@
+#!/bin/sh
+
+test_description='checkout switching away from an invalid branch'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ echo hello >world &&
+ git add world &&
+ git commit -m initial
+'
+
+test_expect_success 'checkout should not start branch from a tree' '
+ test_must_fail git checkout -b newbranch main^{tree}
+'
+
+test_expect_success 'checkout main from invalid HEAD' '
+ echo $ZERO_OID >.git/HEAD &&
+ git checkout main --
+'
+
+test_expect_success 'checkout notices failure to lock HEAD' '
+ test_when_finished "rm -f .git/HEAD.lock" &&
+ >.git/HEAD.lock &&
+ test_must_fail git checkout -b other
+'
+
+test_expect_success 'create ref directory/file conflict scenario' '
+ git update-ref refs/heads/outer/inner main &&
+
+ # do not rely on symbolic-ref to get a known state,
+ # as it may use the same code we are testing
+ reset_to_df () {
+ echo "ref: refs/heads/outer" >.git/HEAD
+ }
+'
+
+test_expect_success 'checkout away from d/f HEAD (unpacked, to branch)' '
+ reset_to_df &&
+ git checkout main
+'
+
+test_expect_success 'checkout away from d/f HEAD (unpacked, to detached)' '
+ reset_to_df &&
+ git checkout --detach main
+'
+
+test_expect_success 'pack refs' '
+ git pack-refs --all --prune
+'
+
+test_expect_success 'checkout away from d/f HEAD (packed, to branch)' '
+ reset_to_df &&
+ git checkout main
+'
+
+test_expect_success 'checkout away from d/f HEAD (packed, to detached)' '
+ reset_to_df &&
+ git checkout --detach main
+'
+test_done
diff --git a/t/t2012-checkout-last.sh b/t/t2012-checkout-last.sh
new file mode 100755
index 0000000..4b6372f
--- /dev/null
+++ b/t/t2012-checkout-last.sh
@@ -0,0 +1,170 @@
+#!/bin/sh
+
+test_description='checkout can switch to last branch and merge base'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ test_commit initial world hello &&
+ git branch other &&
+ test_commit --append second world "hello again"
+'
+
+test_expect_success '"checkout -" does not work initially' '
+ test_must_fail git checkout -
+'
+
+test_expect_success 'first branch switch' '
+ git checkout other
+'
+
+test_cmp_symbolic_HEAD_ref () {
+ echo refs/heads/"$1" >expect &&
+ git symbolic-ref HEAD >actual &&
+ test_cmp expect actual
+}
+
+test_expect_success '"checkout -" switches back' '
+ git checkout - &&
+ test_cmp_symbolic_HEAD_ref main
+'
+
+test_expect_success '"checkout -" switches forth' '
+ git checkout - &&
+ test_cmp_symbolic_HEAD_ref other
+'
+
+test_expect_success 'detach HEAD' '
+ git checkout $(git rev-parse HEAD)
+'
+
+test_expect_success '"checkout -" attaches again' '
+ git checkout - &&
+ test_cmp_symbolic_HEAD_ref other
+'
+
+test_expect_success '"checkout -" detaches again' '
+ git checkout - &&
+
+ git rev-parse other >expect &&
+ git rev-parse HEAD >actual &&
+ test_cmp expect actual &&
+
+ test_must_fail git symbolic-ref HEAD
+'
+
+test_expect_success 'more switches' '
+ for i in 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
+ do
+ git checkout -b branch$i || return 1
+ done
+'
+
+more_switches () {
+ for i in 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
+ do
+ git checkout branch$i || return 1
+ done
+}
+
+test_expect_success 'switch to the last' '
+ more_switches &&
+ git checkout @{-1} &&
+ test_cmp_symbolic_HEAD_ref branch2
+'
+
+test_expect_success 'switch to second from the last' '
+ more_switches &&
+ git checkout @{-2} &&
+ test_cmp_symbolic_HEAD_ref branch3
+'
+
+test_expect_success 'switch to third from the last' '
+ more_switches &&
+ git checkout @{-3} &&
+ test_cmp_symbolic_HEAD_ref branch4
+'
+
+test_expect_success 'switch to fourth from the last' '
+ more_switches &&
+ git checkout @{-4} &&
+ test_cmp_symbolic_HEAD_ref branch5
+'
+
+test_expect_success 'switch to twelfth from the last' '
+ more_switches &&
+ git checkout @{-12} &&
+ test_cmp_symbolic_HEAD_ref branch13
+'
+
+test_expect_success 'merge base test setup' '
+ git checkout -b another other &&
+ test_commit --append third world "hello again"
+'
+
+test_expect_success 'another...main' '
+ git checkout another &&
+ git checkout another...main &&
+
+ git rev-parse --verify main^ >expect &&
+ git rev-parse --verify HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '...main' '
+ git checkout another &&
+ git checkout ...main &&
+
+ git rev-parse --verify main^ >expect &&
+ git rev-parse --verify HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'main...' '
+ git checkout another &&
+ git checkout main... &&
+
+ git rev-parse --verify main^ >expect &&
+ git rev-parse --verify HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '"checkout -" works after a rebase A' '
+ git checkout main &&
+ git checkout other &&
+ git rebase main &&
+ git checkout - &&
+ test_cmp_symbolic_HEAD_ref main
+'
+
+test_expect_success '"checkout -" works after a rebase A B' '
+ git branch moodle main~1 &&
+ git checkout main &&
+ git checkout other &&
+ git rebase main moodle &&
+ git checkout - &&
+ test_cmp_symbolic_HEAD_ref main
+'
+
+test_expect_success '"checkout -" works after a rebase -i A' '
+ git checkout main &&
+ git checkout other &&
+ git rebase -i main &&
+ git checkout - &&
+ test_cmp_symbolic_HEAD_ref main
+'
+
+test_expect_success '"checkout -" works after a rebase -i A B' '
+ git branch foodle main~1 &&
+ git checkout main &&
+ git checkout other &&
+ git rebase main foodle &&
+ git checkout - &&
+ test_cmp_symbolic_HEAD_ref main
+'
+
+test_done
diff --git a/t/t2013-checkout-submodule.sh b/t/t2013-checkout-submodule.sh
new file mode 100755
index 0000000..b2bdd1f
--- /dev/null
+++ b/t/t2013-checkout-submodule.sh
@@ -0,0 +1,75 @@
+#!/bin/sh
+
+test_description='checkout can handle submodules'
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-submodule-update.sh
+
+test_expect_success 'setup' '
+ mkdir submodule &&
+ (cd submodule &&
+ git init &&
+ test_commit first) &&
+ git add submodule &&
+ test_tick &&
+ git commit -m superproject &&
+ (cd submodule &&
+ test_commit second) &&
+ git add submodule &&
+ test_tick &&
+ git commit -m updated.superproject
+'
+
+test_expect_success '"reset <submodule>" updates the index' '
+ git update-index --refresh &&
+ git diff-files --quiet &&
+ git diff-index --quiet --cached HEAD &&
+ git reset HEAD^ submodule &&
+ test_must_fail git diff-files --quiet &&
+ git reset submodule &&
+ git diff-files --quiet
+'
+
+test_expect_success '"checkout <submodule>" updates the index only' '
+ git update-index --refresh &&
+ git diff-files --quiet &&
+ git diff-index --quiet --cached HEAD &&
+ git checkout HEAD^ submodule &&
+ test_must_fail git diff-files --quiet &&
+ git checkout HEAD submodule &&
+ git diff-files --quiet
+'
+
+test_expect_success '"checkout <submodule>" honors diff.ignoreSubmodules' '
+ git config diff.ignoreSubmodules dirty &&
+ echo x> submodule/untracked &&
+ git checkout HEAD >actual 2>&1 &&
+ test_must_be_empty actual
+'
+
+test_expect_success '"checkout <submodule>" honors submodule.*.ignore from .gitmodules' '
+ git config diff.ignoreSubmodules none &&
+ git config -f .gitmodules submodule.submodule.path submodule &&
+ git config -f .gitmodules submodule.submodule.ignore untracked &&
+ git checkout HEAD >actual 2>&1 &&
+ test_must_be_empty actual
+'
+
+test_expect_success '"checkout <submodule>" honors submodule.*.ignore from .git/config' '
+ git config -f .gitmodules submodule.submodule.ignore none &&
+ git config submodule.submodule.path submodule &&
+ git config submodule.submodule.ignore all &&
+ git checkout HEAD >actual 2>&1 &&
+ test_must_be_empty actual
+'
+
+KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS=1
+test_submodule_switch_recursing_with_args "checkout"
+
+test_submodule_forced_switch_recursing_with_args "checkout -f"
+
+test_submodule_switch "checkout"
+
+test_submodule_forced_switch "checkout -f"
+
+test_done
diff --git a/t/t2014-checkout-switch.sh b/t/t2014-checkout-switch.sh
new file mode 100755
index 0000000..c138bdd
--- /dev/null
+++ b/t/t2014-checkout-switch.sh
@@ -0,0 +1,30 @@
+#!/bin/sh
+
+test_description='Peter MacMillan'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ echo Hello >file &&
+ git add file &&
+ test_tick &&
+ git commit -m V1 &&
+ echo Hello world >file &&
+ git add file &&
+ git checkout -b other
+'
+
+test_expect_success 'check all changes are staged' '
+ git diff --exit-code
+'
+
+test_expect_success 'second commit' '
+ git commit -m V2
+'
+
+test_expect_success 'check' '
+ git diff --cached --exit-code
+'
+
+test_done
diff --git a/t/t2015-checkout-unborn.sh b/t/t2015-checkout-unborn.sh
new file mode 100755
index 0000000..fb0e138
--- /dev/null
+++ b/t/t2015-checkout-unborn.sh
@@ -0,0 +1,65 @@
+#!/bin/sh
+
+test_description='checkout from unborn branch'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ mkdir parent &&
+ (
+ cd parent &&
+ git init &&
+ echo content >file &&
+ git add file &&
+ git commit -m base
+ ) &&
+ git fetch parent main:origin
+'
+
+test_expect_success 'checkout from unborn preserves untracked files' '
+ echo precious >expect &&
+ echo precious >file &&
+ test_must_fail git checkout -b new origin &&
+ test_cmp expect file
+'
+
+test_expect_success 'checkout from unborn preserves index contents' '
+ echo precious >expect &&
+ echo precious >file &&
+ git add file &&
+ test_must_fail git checkout -b new origin &&
+ test_cmp expect file &&
+ git show :file >file &&
+ test_cmp expect file
+'
+
+test_expect_success 'checkout from unborn merges identical index contents' '
+ echo content >file &&
+ git add file &&
+ git checkout -b new origin
+'
+
+test_expect_success 'checking out another branch from unborn state' '
+ git checkout --orphan newroot &&
+ git checkout -b anothername &&
+ test_must_fail git show-ref --verify refs/heads/newroot &&
+ git symbolic-ref HEAD >actual &&
+ echo refs/heads/anothername >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'checking out in a newly created repo' '
+ test_create_repo empty &&
+ (
+ cd empty &&
+ git symbolic-ref HEAD >expect &&
+ test_must_fail git checkout &&
+ git symbolic-ref HEAD >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_done
diff --git a/t/t2016-checkout-patch.sh b/t/t2016-checkout-patch.sh
new file mode 100755
index 0000000..747eb55
--- /dev/null
+++ b/t/t2016-checkout-patch.sh
@@ -0,0 +1,127 @@
+#!/bin/sh
+
+test_description='git checkout --patch'
+
+. ./lib-patch-mode.sh
+
+test_expect_success 'setup' '
+ mkdir dir &&
+ echo parent > dir/foo &&
+ echo dummy > bar &&
+ git add bar dir/foo &&
+ git commit -m initial &&
+ test_tick &&
+ test_commit second dir/foo head &&
+ set_and_save_state bar bar_work bar_index &&
+ save_head
+'
+
+# note: bar sorts before dir/foo, so the first 'n' is always to skip 'bar'
+
+test_expect_success 'saying "n" does nothing' '
+ set_and_save_state dir/foo work head &&
+ test_write_lines n n | git checkout -p &&
+ verify_saved_state bar &&
+ verify_saved_state dir/foo
+'
+
+test_expect_success 'git checkout -p' '
+ test_write_lines n y | git checkout -p &&
+ verify_saved_state bar &&
+ verify_state dir/foo head head
+'
+
+test_expect_success 'git checkout -p with staged changes' '
+ set_state dir/foo work index &&
+ test_write_lines n y | git checkout -p &&
+ verify_saved_state bar &&
+ verify_state dir/foo index index
+'
+
+test_expect_success 'git checkout -p HEAD with NO staged changes: abort' '
+ set_and_save_state dir/foo work head &&
+ test_write_lines n y n | git checkout -p HEAD &&
+ verify_saved_state bar &&
+ verify_saved_state dir/foo
+'
+
+test_expect_success 'git checkout -p HEAD with NO staged changes: apply' '
+ test_write_lines n y y | git checkout -p HEAD &&
+ verify_saved_state bar &&
+ verify_state dir/foo head head
+'
+
+test_expect_success 'git checkout -p HEAD with change already staged' '
+ set_state dir/foo index index &&
+ # the third n is to get out in case it mistakenly does not apply
+ test_write_lines n y n | git checkout -p HEAD &&
+ verify_saved_state bar &&
+ verify_state dir/foo head head
+'
+
+test_expect_success 'git checkout -p HEAD^...' '
+ # the third n is to get out in case it mistakenly does not apply
+ test_write_lines n y n | git checkout -p HEAD^... &&
+ verify_saved_state bar &&
+ verify_state dir/foo parent parent
+'
+
+test_expect_success 'git checkout -p HEAD^' '
+ # the third n is to get out in case it mistakenly does not apply
+ test_write_lines n y n | git checkout -p HEAD^ &&
+ verify_saved_state bar &&
+ verify_state dir/foo parent parent
+'
+
+test_expect_success 'git checkout -p handles deletion' '
+ set_state dir/foo work index &&
+ rm dir/foo &&
+ test_write_lines n y | git checkout -p &&
+ verify_saved_state bar &&
+ verify_state dir/foo index index
+'
+
+# The idea in the rest is that bar sorts first, so we always say 'y'
+# first and if the path limiter fails it'll apply to bar instead of
+# dir/foo. There's always an extra 'n' to reject edits to dir/foo in
+# the failure case (and thus get out of the loop).
+
+test_expect_success 'path limiting works: dir' '
+ set_state dir/foo work head &&
+ test_write_lines y n | git checkout -p dir &&
+ verify_saved_state bar &&
+ verify_state dir/foo head head
+'
+
+test_expect_success 'path limiting works: -- dir' '
+ set_state dir/foo work head &&
+ test_write_lines y n | git checkout -p -- dir &&
+ verify_saved_state bar &&
+ verify_state dir/foo head head
+'
+
+test_expect_success 'path limiting works: HEAD^ -- dir' '
+ # the third n is to get out in case it mistakenly does not apply
+ test_write_lines y n n | git checkout -p HEAD^ -- dir &&
+ verify_saved_state bar &&
+ verify_state dir/foo parent parent
+'
+
+test_expect_success 'path limiting works: foo inside dir' '
+ set_state dir/foo work head &&
+ # the third n is to get out in case it mistakenly does not apply
+ test_write_lines y n n | (cd dir && git checkout -p foo) &&
+ verify_saved_state bar &&
+ verify_state dir/foo head head
+'
+
+test_expect_success 'none of this moved HEAD' '
+ verify_saved_head
+'
+
+test_expect_success 'empty tree can be handled' '
+ test_when_finished "git reset --hard" &&
+ git checkout -p $(test_oid empty_tree) --
+'
+
+test_done
diff --git a/t/t2017-checkout-orphan.sh b/t/t2017-checkout-orphan.sh
new file mode 100755
index 0000000..947d158
--- /dev/null
+++ b/t/t2017-checkout-orphan.sh
@@ -0,0 +1,138 @@
+#!/bin/sh
+#
+# Copyright (c) 2010 Erick Mattos
+#
+
+test_description='git checkout --orphan
+
+Main Tests for --orphan functionality.'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+TEST_FILE=foo
+
+test_expect_success 'Setup' '
+ echo "Initial" >"$TEST_FILE" &&
+ git add "$TEST_FILE" &&
+ git commit -m "First Commit" &&
+ test_tick &&
+ echo "State 1" >>"$TEST_FILE" &&
+ git add "$TEST_FILE" &&
+ test_tick &&
+ git commit -m "Second Commit"
+'
+
+test_expect_success '--orphan creates a new orphan branch from HEAD' '
+ git checkout --orphan alpha &&
+ test_must_fail git rev-parse --verify HEAD &&
+ test "refs/heads/alpha" = "$(git symbolic-ref HEAD)" &&
+ test_tick &&
+ git commit -m "Third Commit" &&
+ test_must_fail git rev-parse --verify HEAD^ &&
+ git diff-tree --quiet main alpha
+'
+
+test_expect_success '--orphan creates a new orphan branch from <start_point>' '
+ git checkout main &&
+ git checkout --orphan beta main^ &&
+ test_must_fail git rev-parse --verify HEAD &&
+ test "refs/heads/beta" = "$(git symbolic-ref HEAD)" &&
+ test_tick &&
+ git commit -m "Fourth Commit" &&
+ test_must_fail git rev-parse --verify HEAD^ &&
+ git diff-tree --quiet main^ beta
+'
+
+test_expect_success '--orphan must be rejected with -b' '
+ git checkout main &&
+ test_must_fail git checkout --orphan new -b newer &&
+ test refs/heads/main = "$(git symbolic-ref HEAD)"
+'
+
+test_expect_success '--orphan must be rejected with -t' '
+ git checkout main &&
+ test_must_fail git checkout --orphan new -t main &&
+ test refs/heads/main = "$(git symbolic-ref HEAD)"
+'
+
+test_expect_success '--orphan ignores branch.autosetupmerge' '
+ git checkout main &&
+ git config branch.autosetupmerge always &&
+ git checkout --orphan gamma &&
+ test_cmp_config "" --default "" branch.gamma.merge &&
+ test refs/heads/gamma = "$(git symbolic-ref HEAD)" &&
+ test_must_fail git rev-parse --verify HEAD^ &&
+ git checkout main &&
+ git config branch.autosetupmerge inherit &&
+ git checkout --orphan eta &&
+ test_cmp_config "" --default "" branch.eta.merge &&
+ test_cmp_config "" --default "" branch.eta.remote &&
+ echo refs/heads/eta >expected &&
+ git symbolic-ref HEAD >actual &&
+ test_cmp expected actual &&
+ test_must_fail git rev-parse --verify HEAD^
+'
+
+test_expect_success '--orphan makes reflog by default' '
+ git checkout main &&
+ git config --unset core.logAllRefUpdates &&
+ git checkout --orphan delta &&
+ test_must_fail git rev-parse --verify delta@{0} &&
+ git commit -m Delta &&
+ git rev-parse --verify delta@{0}
+'
+
+test_expect_success REFFILES '--orphan does not make reflog when core.logAllRefUpdates = false' '
+ git checkout main &&
+ git config core.logAllRefUpdates false &&
+ git checkout --orphan epsilon &&
+ test_must_fail git rev-parse --verify epsilon@{0} &&
+ git commit -m Epsilon &&
+ test_must_fail git rev-parse --verify epsilon@{0}
+'
+
+test_expect_success '--orphan with -l makes reflog when core.logAllRefUpdates = false' '
+ git checkout main &&
+ git checkout -l --orphan zeta &&
+ test_must_fail git rev-parse --verify zeta@{0} &&
+ git commit -m Zeta &&
+ git rev-parse --verify zeta@{0}
+'
+
+test_expect_success 'giving up --orphan not committed when -l and core.logAllRefUpdates = false deletes reflog' '
+ git checkout main &&
+ git checkout -l --orphan eta &&
+ test_must_fail git rev-parse --verify eta@{0} &&
+ git checkout main &&
+ test_must_fail git rev-parse --verify eta@{0}
+'
+
+test_expect_success '--orphan is rejected with an existing name' '
+ git checkout main &&
+ test_must_fail git checkout --orphan main &&
+ test refs/heads/main = "$(git symbolic-ref HEAD)"
+'
+
+test_expect_success '--orphan refuses to switch if a merge is needed' '
+ git checkout main &&
+ git reset --hard &&
+ echo local >>"$TEST_FILE" &&
+ cat "$TEST_FILE" >"$TEST_FILE.saved" &&
+ test_must_fail git checkout --orphan new main^ &&
+ test refs/heads/main = "$(git symbolic-ref HEAD)" &&
+ test_cmp "$TEST_FILE" "$TEST_FILE.saved" &&
+ git diff-index --quiet --cached HEAD &&
+ git reset --hard
+'
+
+test_expect_success 'cannot --detach on an unborn branch' '
+ git checkout main &&
+ git checkout --orphan new &&
+ test_must_fail git checkout --detach
+'
+
+test_done
diff --git a/t/t2018-checkout-branch.sh b/t/t2018-checkout-branch.sh
new file mode 100755
index 0000000..43551cc
--- /dev/null
+++ b/t/t2018-checkout-branch.sh
@@ -0,0 +1,289 @@
+#!/bin/sh
+
+test_description='checkout'
+
+TEST_CREATE_REPO_NO_TEMPLATE=1
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# Arguments: [!] <branch> <oid> [<checkout options>]
+#
+# Runs "git checkout" to switch to <branch>, testing that
+#
+# 1) we are on the specified branch, <branch>;
+# 2) HEAD is <oid>; if <oid> is not specified, the old HEAD is used.
+#
+# If <checkout options> is not specified, "git checkout" is run with -b.
+#
+# If the first argument is `!`, "git checkout" is expected to fail when
+# it is run.
+do_checkout () {
+ should_fail= &&
+ if test "x$1" = "x!"
+ then
+ should_fail=yes &&
+ shift
+ fi &&
+ exp_branch=$1 &&
+ exp_ref="refs/heads/$exp_branch" &&
+
+ # if <oid> is not specified, use HEAD.
+ exp_oid=${2:-$(git rev-parse --verify HEAD)} &&
+
+ # default options for git checkout: -b
+ if test -z "$3"
+ then
+ opts="-b"
+ else
+ opts="$3"
+ fi
+
+ if test -n "$should_fail"
+ then
+ test_must_fail git checkout $opts $exp_branch $exp_oid
+ else
+ git checkout $opts $exp_branch $exp_oid &&
+ echo "$exp_ref" >ref.expect &&
+ git rev-parse --symbolic-full-name HEAD >ref.actual &&
+ test_cmp ref.expect ref.actual &&
+ echo "$exp_oid" >oid.expect &&
+ git rev-parse --verify HEAD >oid.actual &&
+ test_cmp oid.expect oid.actual
+ fi
+}
+
+test_dirty_unmergeable () {
+ test_expect_code 1 git diff --exit-code
+}
+
+test_dirty_unmergeable_discards_changes () {
+ git diff --exit-code
+}
+
+setup_dirty_unmergeable () {
+ echo >>file1 change2
+}
+
+test_dirty_mergeable () {
+ test_expect_code 1 git diff --cached --exit-code
+}
+
+test_dirty_mergeable_discards_changes () {
+ git diff --cached --exit-code
+}
+
+setup_dirty_mergeable () {
+ echo >file2 file2 &&
+ git add file2
+}
+
+test_expect_success 'setup' '
+ test_commit initial file1 &&
+ HEAD1=$(git rev-parse --verify HEAD) &&
+
+ test_commit change1 file1 &&
+ HEAD2=$(git rev-parse --verify HEAD) &&
+
+ git branch -m branch1
+'
+
+test_expect_success 'checkout a branch without refs/heads/* prefix' '
+ git clone --no-tags . repo-odd-prefix &&
+ (
+ cd repo-odd-prefix &&
+
+ origin=$(git symbolic-ref refs/remotes/origin/HEAD) &&
+ git symbolic-ref refs/heads/a-branch "$origin" &&
+
+ git checkout -f a-branch &&
+ git checkout -f a-branch
+ )
+'
+
+test_expect_success 'checkout -b to a new branch, set to HEAD' '
+ test_when_finished "
+ git checkout branch1 &&
+ test_might_fail git branch -D branch2" &&
+ do_checkout branch2
+'
+
+test_expect_success 'checkout -b to a merge base' '
+ test_when_finished "
+ git checkout branch1 &&
+ test_might_fail git branch -D branch2" &&
+ git checkout -b branch2 branch1...
+'
+
+test_expect_success 'checkout -b to a new branch, set to an explicit ref' '
+ test_when_finished "
+ git checkout branch1 &&
+ test_might_fail git branch -D branch2" &&
+ do_checkout branch2 $HEAD1
+'
+
+test_expect_success 'checkout -b to a new branch with unmergeable changes fails' '
+ setup_dirty_unmergeable &&
+ do_checkout ! branch2 $HEAD1 &&
+ test_dirty_unmergeable
+'
+
+test_expect_success 'checkout -f -b to a new branch with unmergeable changes discards changes' '
+ test_when_finished "
+ git checkout branch1 &&
+ test_might_fail git branch -D branch2" &&
+
+ # still dirty and on branch1
+ do_checkout branch2 $HEAD1 "-f -b" &&
+ test_dirty_unmergeable_discards_changes
+'
+
+test_expect_success 'checkout -b to a new branch preserves mergeable changes' '
+ test_when_finished "
+ git reset --hard &&
+ git checkout branch1 &&
+ test_might_fail git branch -D branch2" &&
+
+ setup_dirty_mergeable &&
+ do_checkout branch2 $HEAD1 &&
+ test_dirty_mergeable
+'
+
+test_expect_success 'checkout -f -b to a new branch with mergeable changes discards changes' '
+ test_when_finished git reset --hard HEAD &&
+ setup_dirty_mergeable &&
+ do_checkout branch2 $HEAD1 "-f -b" &&
+ test_dirty_mergeable_discards_changes
+'
+
+test_expect_success 'checkout -b to an existing branch fails' '
+ test_when_finished git reset --hard HEAD &&
+ do_checkout ! branch2 $HEAD2
+'
+
+test_expect_success 'checkout -b to @{-1} fails with the right branch name' '
+ git checkout branch1 &&
+ git checkout branch2 &&
+ echo >expect "fatal: a branch named '\''branch1'\'' already exists" &&
+ test_must_fail git checkout -b @{-1} 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'checkout -B to an existing branch resets branch to HEAD' '
+ git checkout branch1 &&
+
+ do_checkout branch2 "" -B
+'
+
+test_expect_success 'checkout -B to a merge base' '
+ git checkout branch1 &&
+
+ git checkout -B branch2 branch1...
+'
+
+test_expect_success 'checkout -B to an existing branch from detached HEAD resets branch to HEAD' '
+ head=$(git rev-parse --verify HEAD) &&
+ git checkout "$head" &&
+
+ do_checkout branch2 "" -B
+'
+
+test_expect_success 'checkout -B to an existing branch with an explicit ref resets branch to that ref' '
+ git checkout branch1 &&
+
+ do_checkout branch2 $HEAD1 -B
+'
+
+test_expect_success 'checkout -B to an existing branch with unmergeable changes fails' '
+ git checkout branch1 &&
+
+ setup_dirty_unmergeable &&
+ do_checkout ! branch2 $HEAD1 -B &&
+ test_dirty_unmergeable
+'
+
+test_expect_success 'checkout -f -B to an existing branch with unmergeable changes discards changes' '
+ # still dirty and on branch1
+ do_checkout branch2 $HEAD1 "-f -B" &&
+ test_dirty_unmergeable_discards_changes
+'
+
+test_expect_success 'checkout -B to an existing branch preserves mergeable changes' '
+ test_when_finished git reset --hard &&
+ git checkout branch1 &&
+
+ setup_dirty_mergeable &&
+ do_checkout branch2 $HEAD1 -B &&
+ test_dirty_mergeable
+'
+
+test_expect_success 'checkout -f -B to an existing branch with mergeable changes discards changes' '
+ git checkout branch1 &&
+
+ setup_dirty_mergeable &&
+ do_checkout branch2 $HEAD1 "-f -B" &&
+ test_dirty_mergeable_discards_changes
+'
+
+test_expect_success 'checkout -b <describe>' '
+ git tag -f -m "First commit" initial initial &&
+ git checkout -f change1 &&
+ name=$(git describe) &&
+ git checkout -b $name &&
+ git diff --exit-code change1 &&
+ echo "refs/heads/$name" >expect &&
+ git symbolic-ref HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'checkout -B to the current branch works' '
+ git checkout branch1 &&
+ git checkout -B branch1-scratch &&
+
+ setup_dirty_mergeable &&
+ git checkout -B branch1-scratch initial &&
+ test_dirty_mergeable
+'
+
+test_expect_success 'checkout -b after clone --no-checkout does a checkout of HEAD' '
+ git init src &&
+ test_commit -C src a &&
+ rev="$(git -C src rev-parse HEAD)" &&
+ git clone --no-checkout src dest &&
+ git -C dest checkout "$rev" -b branch &&
+ test_path_is_file dest/a.t
+'
+
+test_expect_success 'checkout -b to a new branch preserves mergeable changes despite sparse-checkout' '
+ test_when_finished "
+ git reset --hard &&
+ git checkout branch1-scratch &&
+ test_might_fail git branch -D branch3 &&
+ git config core.sparseCheckout false &&
+ rm -rf .git/info" &&
+
+ test_commit file2 &&
+
+ echo stuff >>file1 &&
+ mkdir .git/info &&
+ echo file2 >.git/info/sparse-checkout &&
+ git config core.sparseCheckout true &&
+
+ CURHEAD=$(git rev-parse HEAD) &&
+ do_checkout branch3 $CURHEAD &&
+
+ echo file1 >expect &&
+ git diff --name-only >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'checkout -b rejects an invalid start point' '
+ test_must_fail git checkout -b branch4 file1 2>err &&
+ test_grep "is not a commit" err
+'
+
+test_expect_success 'checkout -b rejects an extra path argument' '
+ test_must_fail git checkout -b branch5 branch1 file1 2>err &&
+ test_grep "Cannot update paths and switch to branch" err
+'
+
+test_done
diff --git a/t/t2019-checkout-ambiguous-ref.sh b/t/t2019-checkout-ambiguous-ref.sh
new file mode 100755
index 0000000..c67261e
--- /dev/null
+++ b/t/t2019-checkout-ambiguous-ref.sh
@@ -0,0 +1,61 @@
+#!/bin/sh
+
+test_description='checkout handling of ambiguous (branch/tag) refs'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup ambiguous refs' '
+ test_commit branch file &&
+ git branch ambiguity &&
+ git branch vagueness &&
+ test_commit tag file &&
+ git tag ambiguity &&
+ git tag vagueness HEAD:file &&
+ test_commit other file
+'
+
+test_expect_success 'checkout ambiguous ref succeeds' '
+ git checkout ambiguity 2>stderr
+'
+
+test_expect_success 'checkout produces ambiguity warning' '
+ grep "warning.*ambiguous" stderr
+'
+
+test_expect_success 'checkout chooses branch over tag' '
+ echo refs/heads/ambiguity >expect &&
+ git symbolic-ref HEAD >actual &&
+ test_cmp expect actual &&
+ echo branch >expect &&
+ test_cmp expect file
+'
+
+test_expect_success 'checkout reports switch to branch' '
+ test_grep "Switched to branch" stderr &&
+ test_grep ! "^HEAD is now at" stderr
+'
+
+test_expect_success 'checkout vague ref succeeds' '
+ git checkout vagueness 2>stderr &&
+ test_set_prereq VAGUENESS_SUCCESS
+'
+
+test_expect_success VAGUENESS_SUCCESS 'checkout produces ambiguity warning' '
+ grep "warning.*ambiguous" stderr
+'
+
+test_expect_success VAGUENESS_SUCCESS 'checkout chooses branch over tag' '
+ echo refs/heads/vagueness >expect &&
+ git symbolic-ref HEAD >actual &&
+ test_cmp expect actual &&
+ echo branch >expect &&
+ test_cmp expect file
+'
+
+test_expect_success VAGUENESS_SUCCESS 'checkout reports switch to branch' '
+ test_grep "Switched to branch" stderr &&
+ test_grep ! "^HEAD is now at" stderr
+'
+
+test_done
diff --git a/t/t2020-checkout-detach.sh b/t/t2020-checkout-detach.sh
new file mode 100755
index 0000000..8202ef8
--- /dev/null
+++ b/t/t2020-checkout-detach.sh
@@ -0,0 +1,336 @@
+#!/bin/sh
+
+test_description='checkout into detached HEAD state'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+check_detached () {
+ test_must_fail git symbolic-ref -q HEAD >/dev/null
+}
+
+check_not_detached () {
+ git symbolic-ref -q HEAD >/dev/null
+}
+
+PREV_HEAD_DESC='Previous HEAD position was'
+check_orphan_warning() {
+ test_grep "you are leaving $2 behind" "$1" &&
+ test_grep ! "$PREV_HEAD_DESC" "$1"
+}
+check_no_orphan_warning() {
+ test_grep ! "you are leaving .* commit.*behind" "$1" &&
+ test_grep "$PREV_HEAD_DESC" "$1"
+}
+
+reset () {
+ git checkout main &&
+ check_not_detached
+}
+
+test_expect_success 'setup' '
+ test_commit one &&
+ test_commit two &&
+ test_commit three && git tag -d three &&
+ test_commit four && git tag -d four &&
+ git branch branch &&
+ git tag tag
+'
+
+test_expect_success 'checkout branch does not detach' '
+ reset &&
+ git checkout branch &&
+ check_not_detached
+'
+
+test_expect_success 'checkout tag detaches' '
+ reset &&
+ git checkout tag &&
+ check_detached
+'
+
+test_expect_success 'checkout branch by full name detaches' '
+ reset &&
+ git checkout refs/heads/branch &&
+ check_detached
+'
+
+test_expect_success 'checkout non-ref detaches' '
+ reset &&
+ git checkout branch^ &&
+ check_detached
+'
+
+test_expect_success 'checkout ref^0 detaches' '
+ reset &&
+ git checkout branch^0 &&
+ check_detached
+'
+
+test_expect_success 'checkout --detach detaches' '
+ reset &&
+ git checkout --detach branch &&
+ check_detached
+'
+
+test_expect_success 'checkout --detach without branch name' '
+ reset &&
+ git checkout --detach &&
+ check_detached
+'
+
+test_expect_success 'checkout --detach errors out for non-commit' '
+ reset &&
+ test_must_fail git checkout --detach one^{tree} &&
+ check_not_detached
+'
+
+test_expect_success 'checkout --detach errors out for extra argument' '
+ reset &&
+ git checkout main &&
+ test_must_fail git checkout --detach tag one.t &&
+ check_not_detached
+'
+
+test_expect_success 'checkout --detached and -b are incompatible' '
+ reset &&
+ test_must_fail git checkout --detach -b newbranch tag &&
+ check_not_detached
+'
+
+test_expect_success 'checkout --detach moves HEAD' '
+ reset &&
+ git checkout one &&
+ git checkout --detach two &&
+ git diff --exit-code HEAD &&
+ git diff --exit-code two
+'
+
+test_expect_success 'checkout warns on orphan commits' '
+ reset &&
+ git checkout --detach two &&
+ echo content >orphan &&
+ git add orphan &&
+ git commit -a -m orphan1 &&
+ echo new content >orphan &&
+ git commit -a -m orphan2 &&
+ orphan2=$(git rev-parse HEAD) &&
+ git checkout main 2>stderr
+'
+
+test_expect_success 'checkout warns on orphan commits: output' '
+ check_orphan_warning stderr "2 commits"
+'
+
+test_expect_success 'checkout warns orphaning 1 of 2 commits' '
+ git checkout "$orphan2" &&
+ git checkout HEAD^ 2>stderr
+'
+
+test_expect_success 'checkout warns orphaning 1 of 2 commits: output' '
+ check_orphan_warning stderr "1 commit"
+'
+
+test_expect_success 'checkout does not warn leaving ref tip' '
+ reset &&
+ git checkout --detach two &&
+ git checkout main 2>stderr
+'
+
+test_expect_success 'checkout does not warn leaving ref tip' '
+ check_no_orphan_warning stderr
+'
+
+test_expect_success 'checkout does not warn leaving reachable commit' '
+ reset &&
+ git checkout --detach HEAD^ &&
+ git checkout main 2>stderr
+'
+
+test_expect_success 'checkout does not warn leaving reachable commit' '
+ check_no_orphan_warning stderr
+'
+
+cat >expect <<'EOF'
+Your branch is behind 'main' by 1 commit, and can be fast-forwarded.
+ (use "git pull" to update your local branch)
+EOF
+test_expect_success 'tracking count is accurate after orphan check' '
+ reset &&
+ git branch child main^ &&
+ git config branch.child.remote . &&
+ git config branch.child.merge refs/heads/main &&
+ git checkout child^ &&
+ git checkout child >stdout &&
+ test_cmp expect stdout
+'
+
+test_expect_success 'no advice given for explicit detached head state' '
+ # baseline
+ test_config advice.detachedHead true &&
+ git checkout child && git checkout HEAD^0 >expect.advice 2>&1 &&
+ test_config advice.detachedHead false &&
+ git checkout child && git checkout HEAD^0 >expect.no-advice 2>&1 &&
+ test_unconfig advice.detachedHead &&
+ # without configuration, the advice.* variables default to true
+ git checkout child && git checkout HEAD^0 >actual 2>&1 &&
+ test_cmp expect.advice actual &&
+
+ # with explicit --detach
+ # no configuration
+ test_unconfig advice.detachedHead &&
+ git checkout child && git checkout --detach HEAD^0 >actual 2>&1 &&
+ test_cmp expect.no-advice actual &&
+
+ # explicitly decline advice
+ test_config advice.detachedHead false &&
+ git checkout child && git checkout --detach HEAD^0 >actual 2>&1 &&
+ test_cmp expect.no-advice actual
+'
+
+# Detached HEAD tests for GIT_PRINT_SHA1_ELLIPSIS (new format)
+test_expect_success 'describe_detached_head prints no SHA-1 ellipsis when not asked to' "
+
+ commit=$(git rev-parse --short=12 main^) &&
+ commit2=$(git rev-parse --short=12 main~2) &&
+ commit3=$(git rev-parse --short=12 main~3) &&
+
+ # The first detach operation is more chatty than the following ones.
+ cat >1st_detach <<-EOF &&
+ Note: switching to 'HEAD^'.
+
+ You are in 'detached HEAD' state. You can look around, make experimental
+ changes and commit them, and you can discard any commits you make in this
+ state without impacting any branches by switching back to a branch.
+
+ If you want to create a new branch to retain commits you create, you may
+ do so (now or later) by using -c with the switch command. Example:
+
+ git switch -c <new-branch-name>
+
+ Or undo this operation with:
+
+ git switch -
+
+ Turn off this advice by setting config variable advice.detachedHead to false
+
+ HEAD is now at \$commit three
+ EOF
+
+ # The remaining ones just show info about previous and current HEADs.
+ cat >2nd_detach <<-EOF &&
+ Previous HEAD position was \$commit three
+ HEAD is now at \$commit2 two
+ EOF
+
+ cat >3rd_detach <<-EOF &&
+ Previous HEAD position was \$commit2 two
+ HEAD is now at \$commit3 one
+ EOF
+
+ reset &&
+ check_not_detached &&
+
+ # Various ways of *not* asking for ellipses
+
+ sane_unset GIT_PRINT_SHA1_ELLIPSIS &&
+ git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
+ check_detached &&
+ test_cmp 1st_detach actual &&
+
+ GIT_PRINT_SHA1_ELLIPSIS="no" git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
+ check_detached &&
+ test_cmp 2nd_detach actual &&
+
+ GIT_PRINT_SHA1_ELLIPSIS= git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
+ check_detached &&
+ test_cmp 3rd_detach actual &&
+
+ sane_unset GIT_PRINT_SHA1_ELLIPSIS &&
+
+ # We only have four commits, but we can re-use them
+ reset &&
+ check_not_detached &&
+
+ # Make no mention of the env var at all
+ git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
+ check_detached &&
+ test_cmp 1st_detach actual &&
+
+ GIT_PRINT_SHA1_ELLIPSIS='nope' &&
+ git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
+ check_detached &&
+ test_cmp 2nd_detach actual &&
+
+ GIT_PRINT_SHA1_ELLIPSIS=nein &&
+ git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
+ check_detached &&
+ test_cmp 3rd_detach actual &&
+
+ true
+"
+
+# Detached HEAD tests for GIT_PRINT_SHA1_ELLIPSIS (old format)
+test_expect_success 'describe_detached_head does print SHA-1 ellipsis when asked to' "
+
+ commit=$(git rev-parse --short=12 main^) &&
+ commit2=$(git rev-parse --short=12 main~2) &&
+ commit3=$(git rev-parse --short=12 main~3) &&
+
+ # The first detach operation is more chatty than the following ones.
+ cat >1st_detach <<-EOF &&
+ Note: switching to 'HEAD^'.
+
+ You are in 'detached HEAD' state. You can look around, make experimental
+ changes and commit them, and you can discard any commits you make in this
+ state without impacting any branches by switching back to a branch.
+
+ If you want to create a new branch to retain commits you create, you may
+ do so (now or later) by using -c with the switch command. Example:
+
+ git switch -c <new-branch-name>
+
+ Or undo this operation with:
+
+ git switch -
+
+ Turn off this advice by setting config variable advice.detachedHead to false
+
+ HEAD is now at \$commit... three
+ EOF
+
+ # The remaining ones just show info about previous and current HEADs.
+ cat >2nd_detach <<-EOF &&
+ Previous HEAD position was \$commit... three
+ HEAD is now at \$commit2... two
+ EOF
+
+ cat >3rd_detach <<-EOF &&
+ Previous HEAD position was \$commit2... two
+ HEAD is now at \$commit3... one
+ EOF
+
+ reset &&
+ check_not_detached &&
+
+ # Various ways of asking for ellipses...
+ # The user can just use any kind of quoting (including none).
+
+ GIT_PRINT_SHA1_ELLIPSIS=yes git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
+ check_detached &&
+ test_cmp 1st_detach actual &&
+
+ GIT_PRINT_SHA1_ELLIPSIS=Yes git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
+ check_detached &&
+ test_cmp 2nd_detach actual &&
+
+ GIT_PRINT_SHA1_ELLIPSIS=YES git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
+ check_detached &&
+ test_cmp 3rd_detach actual &&
+
+ true
+"
+
+test_done
diff --git a/t/t2021-checkout-overwrite.sh b/t/t2021-checkout-overwrite.sh
new file mode 100755
index 0000000..ecfacf0
--- /dev/null
+++ b/t/t2021-checkout-overwrite.sh
@@ -0,0 +1,83 @@
+#!/bin/sh
+
+test_description='checkout must not overwrite an untracked objects'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+
+ mkdir -p a/b/c &&
+ >a/b/c/d &&
+ git add -A &&
+ git commit -m base &&
+ git tag start
+'
+
+test_expect_success 'create a commit where dir a/b changed to file' '
+
+ git checkout -b file &&
+ rm -rf a/b &&
+ >a/b &&
+ git add -A &&
+ git commit -m "dir to file"
+'
+
+test_expect_success 'checkout commit with dir must not remove untracked a/b' '
+
+ git rm --cached a/b &&
+ git commit -m "un-track the file" &&
+ test_must_fail git checkout start &&
+ test -f a/b
+'
+
+test_expect_success 'create a commit where dir a/b changed to symlink' '
+
+ rm -rf a/b && # cleanup if previous test failed
+ git checkout -f -b symlink start &&
+ rm -rf a/b &&
+ git add -A &&
+ test_ln_s_add foo a/b &&
+ git commit -m "dir to symlink"
+'
+
+test_expect_success 'checkout commit with dir must not remove untracked a/b' '
+
+ git rm --cached a/b &&
+ git commit -m "un-track the symlink" &&
+ test_must_fail git checkout start
+'
+
+test_expect_success SYMLINKS 'the symlink remained' '
+
+ test -h a/b
+'
+
+test_expect_success 'cleanup after previous symlink tests' '
+ rm a/b
+'
+
+test_expect_success SYMLINKS 'checkout -f must not follow symlinks when removing entries' '
+ git checkout -f start &&
+ mkdir dir &&
+ >dir/f &&
+ git add dir/f &&
+ git commit -m "add dir/f" &&
+ mv dir untracked &&
+ ln -s untracked dir &&
+ git checkout -f HEAD~ &&
+ test_path_is_file untracked/f
+'
+
+test_expect_success 'checkout --overwrite-ignore should succeed if only ignored files in the way' '
+ git checkout -b df_conflict &&
+ test_commit contents some_dir &&
+ git checkout start &&
+ mkdir some_dir &&
+ echo autogenerated information >some_dir/ignore &&
+ echo ignore >.git/info/exclude &&
+ git checkout --overwrite-ignore df_conflict &&
+ test_path_is_file some_dir
+'
+
+test_done
diff --git a/t/t2022-checkout-paths.sh b/t/t2022-checkout-paths.sh
new file mode 100755
index 0000000..f1b709d
--- /dev/null
+++ b/t/t2022-checkout-paths.sh
@@ -0,0 +1,96 @@
+#!/bin/sh
+
+test_description='checkout $tree -- $paths'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ mkdir dir &&
+ >dir/main &&
+ echo common >dir/common &&
+ git add dir/main dir/common &&
+ test_tick && git commit -m "main has dir/main" &&
+ git checkout -b next &&
+ git mv dir/main dir/next0 &&
+ echo next >dir/next1 &&
+ git add dir &&
+ test_tick && git commit -m "next has dir/next but not dir/main"
+'
+
+test_expect_success 'checking out paths out of a tree does not clobber unrelated paths' '
+ git checkout next &&
+ git reset --hard &&
+ rm dir/next0 &&
+ cat dir/common >expect.common &&
+ echo modified >expect.next1 &&
+ cat expect.next1 >dir/next1 &&
+ echo untracked >expect.next2 &&
+ cat expect.next2 >dir/next2 &&
+
+ git checkout main dir &&
+
+ test_cmp expect.common dir/common &&
+ test_path_is_file dir/main &&
+ git diff --exit-code main dir/main &&
+
+ test_path_is_missing dir/next0 &&
+ test_cmp expect.next1 dir/next1 &&
+ test_path_is_file dir/next2 &&
+ test_must_fail git ls-files --error-unmatch dir/next2 &&
+ test_cmp expect.next2 dir/next2
+'
+
+test_expect_success 'do not touch unmerged entries matching $path but not in $tree' '
+ git checkout next &&
+ git reset --hard &&
+
+ cat dir/common >expect.common &&
+ EMPTY_SHA1=$(git hash-object -w --stdin </dev/null) &&
+ git rm dir/next0 &&
+ cat >expect.next0 <<-EOF &&
+ 100644 $EMPTY_SHA1 1 dir/next0
+ 100644 $EMPTY_SHA1 2 dir/next0
+ EOF
+ git update-index --index-info <expect.next0 &&
+
+ git checkout main dir &&
+
+ test_cmp expect.common dir/common &&
+ test_path_is_file dir/main &&
+ git diff --exit-code main dir/main &&
+ git ls-files -s dir/next0 >actual.next0 &&
+ test_cmp expect.next0 actual.next0
+'
+
+test_expect_success 'do not touch files that are already up-to-date' '
+ git reset --hard &&
+ echo one >file1 &&
+ echo two >file2 &&
+ git add file1 file2 &&
+ git commit -m base &&
+ echo modified >file1 &&
+ test-tool chmtime =1000000000 file2 &&
+ git update-index -q --refresh &&
+ git checkout HEAD -- file1 file2 &&
+ echo one >expect &&
+ test_cmp expect file1 &&
+ echo "1000000000" >expect &&
+ test-tool chmtime --get file2 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'checkout HEAD adds deleted intent-to-add file back to index' '
+ echo "nonempty" >nonempty &&
+ >empty &&
+ git add nonempty empty &&
+ git commit -m "create files to be deleted" &&
+ git rm --cached nonempty empty &&
+ git add -N nonempty empty &&
+ git checkout HEAD nonempty empty &&
+ git diff --cached --exit-code
+'
+
+test_done
diff --git a/t/t2023-checkout-m.sh b/t/t2023-checkout-m.sh
new file mode 100755
index 0000000..81e772f
--- /dev/null
+++ b/t/t2023-checkout-m.sh
@@ -0,0 +1,77 @@
+#!/bin/sh
+
+test_description='checkout -m -- <conflicted path>
+
+Ensures that checkout -m on a resolved file restores the conflicted file'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ test_tick &&
+ test_commit both.txt both.txt initial &&
+ git branch topic &&
+ test_commit modified_in_main both.txt in_main &&
+ test_commit added_in_main each.txt in_main &&
+ git checkout topic &&
+ test_commit modified_in_topic both.txt in_topic &&
+ test_commit added_in_topic each.txt in_topic
+'
+
+test_expect_success 'git merge main' '
+ test_must_fail git merge main
+'
+
+clean_branchnames () {
+ # Remove branch names after conflict lines
+ sed 's/^\([<>]\{5,\}\) .*$/\1/'
+}
+
+test_expect_success '-m restores 2-way conflicted+resolved file' '
+ cp each.txt each.txt.conflicted &&
+ echo resolved >each.txt &&
+ git add each.txt &&
+ git checkout -m -- each.txt &&
+ clean_branchnames <each.txt >each.txt.cleaned &&
+ clean_branchnames <each.txt.conflicted >each.txt.conflicted.cleaned &&
+ test_cmp each.txt.conflicted.cleaned each.txt.cleaned
+'
+
+test_expect_success '-m restores 3-way conflicted+resolved file' '
+ cp both.txt both.txt.conflicted &&
+ echo resolved >both.txt &&
+ git add both.txt &&
+ git checkout -m -- both.txt &&
+ clean_branchnames <both.txt >both.txt.cleaned &&
+ clean_branchnames <both.txt.conflicted >both.txt.conflicted.cleaned &&
+ test_cmp both.txt.conflicted.cleaned both.txt.cleaned
+'
+
+test_expect_success 'force checkout a conflict file creates stage zero entry' '
+ git init co-force &&
+ (
+ cd co-force &&
+ echo a >a &&
+ git add a &&
+ git commit -ama &&
+ A_OBJ=$(git rev-parse :a) &&
+ git branch topic &&
+ echo b >a &&
+ git commit -amb &&
+ B_OBJ=$(git rev-parse :a) &&
+ git checkout topic &&
+ echo c >a &&
+ C_OBJ=$(git hash-object a) &&
+ git checkout -m main &&
+ test_cmp_rev :1:a $A_OBJ &&
+ test_cmp_rev :2:a $B_OBJ &&
+ test_cmp_rev :3:a $C_OBJ &&
+ git checkout -f topic &&
+ test_cmp_rev :0:a $A_OBJ
+ )
+'
+
+test_done
diff --git a/t/t2024-checkout-dwim.sh b/t/t2024-checkout-dwim.sh
new file mode 100755
index 0000000..a97416c
--- /dev/null
+++ b/t/t2024-checkout-dwim.sh
@@ -0,0 +1,350 @@
+#!/bin/sh
+
+test_description='checkout <branch>
+
+Ensures that checkout on an unborn branch does what the user expects'
+
+. ./test-lib.sh
+
+# Is the current branch "refs/heads/$1"?
+test_branch () {
+ printf "%s\n" "refs/heads/$1" >expect.HEAD &&
+ git symbolic-ref HEAD >actual.HEAD &&
+ test_cmp expect.HEAD actual.HEAD
+}
+
+# Is branch "refs/heads/$1" set to pull from "$2/$3"?
+test_branch_upstream () {
+ printf "%s\n" "$2" "refs/heads/$3" >expect.upstream &&
+ {
+ git config "branch.$1.remote" &&
+ git config "branch.$1.merge"
+ } >actual.upstream &&
+ test_cmp expect.upstream actual.upstream
+}
+
+status_uno_is_clean () {
+ git status -uno --porcelain >status.actual &&
+ test_must_be_empty status.actual
+}
+
+test_expect_success 'setup' '
+ test_commit my_main &&
+ git init repo_a &&
+ (
+ cd repo_a &&
+ test_commit a_main &&
+ git checkout -b foo &&
+ test_commit a_foo &&
+ git checkout -b bar &&
+ test_commit a_bar &&
+ git checkout -b ambiguous_branch_and_file &&
+ test_commit a_ambiguous_branch_and_file
+ ) &&
+ git init repo_b &&
+ (
+ cd repo_b &&
+ test_commit b_main &&
+ git checkout -b foo &&
+ test_commit b_foo &&
+ git checkout -b baz &&
+ test_commit b_baz &&
+ git checkout -b ambiguous_branch_and_file &&
+ test_commit b_ambiguous_branch_and_file
+ ) &&
+ git remote add repo_a repo_a &&
+ git remote add repo_b repo_b &&
+ git config remote.repo_b.fetch \
+ "+refs/heads/*:refs/remotes/other_b/*" &&
+ git fetch --all
+'
+
+test_expect_success 'checkout of non-existing branch fails' '
+ git checkout -B main &&
+ test_might_fail git branch -D xyzzy &&
+
+ test_must_fail git checkout xyzzy &&
+ status_uno_is_clean &&
+ test_must_fail git rev-parse --verify refs/heads/xyzzy &&
+ test_branch main
+'
+
+test_expect_success 'checkout of branch from multiple remotes fails #1' '
+ git checkout -B main &&
+ test_might_fail git branch -D foo &&
+
+ test_must_fail git checkout foo &&
+ status_uno_is_clean &&
+ test_must_fail git rev-parse --verify refs/heads/foo &&
+ test_branch main
+'
+
+test_expect_success 'when arg matches multiple remotes, do not fallback to interpreting as pathspec' '
+ # create a file with name matching remote branch name
+ git checkout -b t_ambiguous_branch_and_file &&
+ >ambiguous_branch_and_file &&
+ git add ambiguous_branch_and_file &&
+ git commit -m "ambiguous_branch_and_file" &&
+
+ # modify file to verify that it will not be touched by checkout
+ test_when_finished "git checkout -- ambiguous_branch_and_file" &&
+ echo "file contents" >ambiguous_branch_and_file &&
+ cp ambiguous_branch_and_file expect &&
+
+ test_must_fail git checkout ambiguous_branch_and_file 2>err &&
+
+ test_grep "matched multiple (2) remote tracking branches" err &&
+
+ # file must not be altered
+ test_cmp expect ambiguous_branch_and_file
+'
+
+test_expect_success 'checkout of branch from multiple remotes fails with advice' '
+ git checkout -B main &&
+ test_might_fail git branch -D foo &&
+ test_must_fail git checkout foo 2>stderr &&
+ test_branch main &&
+ status_uno_is_clean &&
+ test_grep "^hint: " stderr &&
+ test_must_fail git -c advice.checkoutAmbiguousRemoteBranchName=false \
+ checkout foo 2>stderr &&
+ test_branch main &&
+ status_uno_is_clean &&
+ test_grep ! "^hint: " stderr
+'
+
+test_expect_success PERL 'checkout -p with multiple remotes does not print advice' '
+ git checkout -B main &&
+ test_might_fail git branch -D foo &&
+
+ git checkout -p foo 2>stderr &&
+ test_grep ! "^hint: " stderr &&
+ status_uno_is_clean
+'
+
+test_expect_success 'checkout of branch from multiple remotes succeeds with checkout.defaultRemote #1' '
+ git checkout -B main &&
+ status_uno_is_clean &&
+ test_might_fail git branch -D foo &&
+
+ git -c checkout.defaultRemote=repo_a checkout foo &&
+ status_uno_is_clean &&
+ test_branch foo &&
+ test_cmp_rev remotes/repo_a/foo HEAD &&
+ test_branch_upstream foo repo_a foo
+'
+
+test_expect_success 'checkout of branch from a single remote succeeds #1' '
+ git checkout -B main &&
+ test_might_fail git branch -D bar &&
+
+ git checkout bar &&
+ status_uno_is_clean &&
+ test_branch bar &&
+ test_cmp_rev remotes/repo_a/bar HEAD &&
+ test_branch_upstream bar repo_a bar
+'
+
+test_expect_success 'checkout of branch from a single remote succeeds #2' '
+ git checkout -B main &&
+ test_might_fail git branch -D baz &&
+
+ git checkout baz &&
+ status_uno_is_clean &&
+ test_branch baz &&
+ test_cmp_rev remotes/other_b/baz HEAD &&
+ test_branch_upstream baz repo_b baz
+'
+
+test_expect_success '--no-guess suppresses branch auto-vivification' '
+ git checkout -B main &&
+ status_uno_is_clean &&
+ test_might_fail git branch -D bar &&
+
+ test_must_fail git checkout --no-guess bar &&
+ test_must_fail git rev-parse --verify refs/heads/bar &&
+ test_branch main
+'
+
+test_expect_success 'checkout.guess = false suppresses branch auto-vivification' '
+ git checkout -B main &&
+ status_uno_is_clean &&
+ test_might_fail git branch -D bar &&
+
+ test_config checkout.guess false &&
+ test_must_fail git checkout bar &&
+ test_must_fail git rev-parse --verify refs/heads/bar &&
+ test_branch main
+'
+
+test_expect_success 'setup more remotes with unconventional refspecs' '
+ git checkout -B main &&
+ status_uno_is_clean &&
+ git init repo_c &&
+ (
+ cd repo_c &&
+ test_commit c_main &&
+ git checkout -b bar &&
+ test_commit c_bar &&
+ git checkout -b spam &&
+ test_commit c_spam
+ ) &&
+ git init repo_d &&
+ (
+ cd repo_d &&
+ test_commit d_main &&
+ git checkout -b baz &&
+ test_commit d_baz &&
+ git checkout -b eggs &&
+ test_commit d_eggs
+ ) &&
+ git remote add repo_c repo_c &&
+ git config remote.repo_c.fetch \
+ "+refs/heads/*:refs/remotes/extra_dir/repo_c/extra_dir/*" &&
+ git remote add repo_d repo_d &&
+ git config remote.repo_d.fetch \
+ "+refs/heads/*:refs/repo_d/*" &&
+ git fetch --all
+'
+
+test_expect_success 'checkout of branch from multiple remotes fails #2' '
+ git checkout -B main &&
+ status_uno_is_clean &&
+ test_might_fail git branch -D bar &&
+
+ test_must_fail git checkout bar &&
+ status_uno_is_clean &&
+ test_must_fail git rev-parse --verify refs/heads/bar &&
+ test_branch main
+'
+
+test_expect_success 'checkout of branch from multiple remotes fails #3' '
+ git checkout -B main &&
+ status_uno_is_clean &&
+ test_might_fail git branch -D baz &&
+
+ test_must_fail git checkout baz &&
+ status_uno_is_clean &&
+ test_must_fail git rev-parse --verify refs/heads/baz &&
+ test_branch main
+'
+
+test_expect_success 'checkout of branch from a single remote succeeds #3' '
+ git checkout -B main &&
+ status_uno_is_clean &&
+ test_might_fail git branch -D spam &&
+
+ git checkout spam &&
+ status_uno_is_clean &&
+ test_branch spam &&
+ test_cmp_rev refs/remotes/extra_dir/repo_c/extra_dir/spam HEAD &&
+ test_branch_upstream spam repo_c spam
+'
+
+test_expect_success 'checkout of branch from a single remote succeeds #4' '
+ git checkout -B main &&
+ status_uno_is_clean &&
+ test_might_fail git branch -D eggs &&
+
+ git checkout eggs &&
+ status_uno_is_clean &&
+ test_branch eggs &&
+ test_cmp_rev refs/repo_d/eggs HEAD &&
+ test_branch_upstream eggs repo_d eggs
+'
+
+test_expect_success 'checkout of branch with a file having the same name fails' '
+ git checkout -B main &&
+ status_uno_is_clean &&
+ test_might_fail git branch -D spam &&
+
+ >spam &&
+ test_must_fail git checkout spam &&
+ status_uno_is_clean &&
+ test_must_fail git rev-parse --verify refs/heads/spam &&
+ test_branch main
+'
+
+test_expect_success 'checkout of branch with a file in subdir having the same name fails' '
+ git checkout -B main &&
+ status_uno_is_clean &&
+ test_might_fail git branch -D spam &&
+
+ >spam &&
+ mkdir sub &&
+ mv spam sub/spam &&
+ test_must_fail git -C sub checkout spam &&
+ status_uno_is_clean &&
+ test_must_fail git rev-parse --verify refs/heads/spam &&
+ test_branch main
+'
+
+test_expect_success 'checkout <branch> -- succeeds, even if a file with the same name exists' '
+ git checkout -B main &&
+ status_uno_is_clean &&
+ test_might_fail git branch -D spam &&
+
+ >spam &&
+ git checkout spam -- &&
+ status_uno_is_clean &&
+ test_branch spam &&
+ test_cmp_rev refs/remotes/extra_dir/repo_c/extra_dir/spam HEAD &&
+ test_branch_upstream spam repo_c spam
+'
+
+test_expect_success 'loosely defined local base branch is reported correctly' '
+
+ git checkout main &&
+ status_uno_is_clean &&
+ git branch strict &&
+ git branch loose &&
+ git commit --allow-empty -m "a bit more" &&
+
+ test_config branch.strict.remote . &&
+ test_config branch.loose.remote . &&
+ test_config branch.strict.merge refs/heads/main &&
+ test_config branch.loose.merge main &&
+
+ git checkout strict >expect.raw 2>&1 &&
+ sed -e "s/strict/BRANCHNAME/g" <expect.raw >expect &&
+ status_uno_is_clean &&
+ git checkout loose >actual.raw 2>&1 &&
+ sed -e "s/loose/BRANCHNAME/g" <actual.raw >actual &&
+ status_uno_is_clean &&
+ grep BRANCHNAME actual &&
+
+ test_cmp expect actual
+'
+
+test_expect_success 'reject when arg could be part of dwim branch' '
+ git remote add foo file://non-existent-place &&
+ git update-ref refs/remotes/foo/dwim-arg HEAD &&
+ echo foo >dwim-arg &&
+ git add dwim-arg &&
+ echo bar >dwim-arg &&
+ test_must_fail git checkout dwim-arg &&
+ test_must_fail git rev-parse refs/heads/dwim-arg -- &&
+ grep bar dwim-arg
+'
+
+test_expect_success 'disambiguate dwim branch and checkout path (1)' '
+ git update-ref refs/remotes/foo/dwim-arg1 HEAD &&
+ echo foo >dwim-arg1 &&
+ git add dwim-arg1 &&
+ echo bar >dwim-arg1 &&
+ git checkout -- dwim-arg1 &&
+ test_must_fail git rev-parse refs/heads/dwim-arg1 -- &&
+ grep foo dwim-arg1
+'
+
+test_expect_success 'disambiguate dwim branch and checkout path (2)' '
+ git update-ref refs/remotes/foo/dwim-arg2 HEAD &&
+ echo foo >dwim-arg2 &&
+ git add dwim-arg2 &&
+ echo bar >dwim-arg2 &&
+ git checkout dwim-arg2 -- &&
+ git rev-parse refs/heads/dwim-arg2 -- &&
+ grep bar dwim-arg2
+'
+
+test_done
diff --git a/t/t2025-checkout-no-overlay.sh b/t/t2025-checkout-no-overlay.sh
new file mode 100755
index 0000000..246609d
--- /dev/null
+++ b/t/t2025-checkout-no-overlay.sh
@@ -0,0 +1,60 @@
+#!/bin/sh
+
+test_description='checkout --no-overlay <tree-ish> -- <pathspec>'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ git commit --allow-empty -m "initial"
+'
+
+test_expect_success 'checkout --no-overlay deletes files not in <tree-ish>' '
+ >file &&
+ mkdir dir &&
+ >dir/file1 &&
+ git add file dir/file1 &&
+ git checkout --no-overlay HEAD -- file &&
+ test_path_is_missing file &&
+ test_path_is_file dir/file1
+'
+
+test_expect_success 'checkout --no-overlay removing last file from directory' '
+ git checkout --no-overlay HEAD -- dir/file1 &&
+ test_path_is_missing dir
+'
+
+test_expect_success 'checkout -p --overlay is disallowed' '
+ test_must_fail git checkout -p --overlay HEAD 2>actual &&
+ test_grep "fatal: options .-p. and .--overlay. cannot be used together" actual
+'
+
+test_expect_success '--no-overlay --theirs with D/F conflict deletes file' '
+ test_commit file1 file1 &&
+ test_commit file2 file2 &&
+ git rm --cached file1 &&
+ echo 1234 >file1 &&
+ F1=$(git rev-parse HEAD:file1) &&
+ F2=$(git rev-parse HEAD:file2) &&
+ {
+ echo "100644 $F1 1 file1" &&
+ echo "100644 $F2 2 file1"
+ } | git update-index --index-info &&
+ test_path_is_file file1 &&
+ git checkout --theirs --no-overlay -- file1 &&
+ test_path_is_missing file1
+'
+
+test_expect_success 'wildcard pathspec matches file in subdirectory' '
+ git reset --hard &&
+ mkdir subdir &&
+ test_commit file3-1 subdir/file3 &&
+ test_commit file3-2 subdir/file3 &&
+
+ git checkout --no-overlay file3-1 "*file3" &&
+ echo file3-1 >expect &&
+ test_path_is_file subdir/file3 &&
+ test_cmp expect subdir/file3
+'
+
+test_done
diff --git a/t/t2026-checkout-pathspec-file.sh b/t/t2026-checkout-pathspec-file.sh
new file mode 100755
index 0000000..acd5521
--- /dev/null
+++ b/t/t2026-checkout-pathspec-file.sh
@@ -0,0 +1,164 @@
+#!/bin/sh
+
+test_description='checkout --pathspec-from-file'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_tick
+
+test_expect_success setup '
+ test_commit file0 &&
+
+ echo 1 >fileA.t &&
+ echo 1 >fileB.t &&
+ echo 1 >fileC.t &&
+ echo 1 >fileD.t &&
+ git add fileA.t fileB.t fileC.t fileD.t &&
+ git commit -m "files 1" &&
+
+ echo 2 >fileA.t &&
+ echo 2 >fileB.t &&
+ echo 2 >fileC.t &&
+ echo 2 >fileD.t &&
+ git add fileA.t fileB.t fileC.t fileD.t &&
+ git commit -m "files 2" &&
+
+ git tag checkpoint
+'
+
+restore_checkpoint () {
+ git reset --hard checkpoint
+}
+
+verify_expect () {
+ git status --porcelain --untracked-files=no -- fileA.t fileB.t fileC.t fileD.t >actual &&
+ test_cmp expect actual
+}
+
+test_expect_success '--pathspec-from-file from stdin' '
+ restore_checkpoint &&
+
+ echo fileA.t | git checkout --pathspec-from-file=- HEAD^1 &&
+
+ cat >expect <<-\EOF &&
+ M fileA.t
+ EOF
+ verify_expect
+'
+
+test_expect_success '--pathspec-from-file from file' '
+ restore_checkpoint &&
+
+ echo fileA.t >list &&
+ git checkout --pathspec-from-file=list HEAD^1 &&
+
+ cat >expect <<-\EOF &&
+ M fileA.t
+ EOF
+ verify_expect
+'
+
+test_expect_success 'NUL delimiters' '
+ restore_checkpoint &&
+
+ printf "fileA.t\0fileB.t\0" | git checkout --pathspec-from-file=- --pathspec-file-nul HEAD^1 &&
+
+ cat >expect <<-\EOF &&
+ M fileA.t
+ M fileB.t
+ EOF
+ verify_expect
+'
+
+test_expect_success 'LF delimiters' '
+ restore_checkpoint &&
+
+ printf "fileA.t\nfileB.t\n" | git checkout --pathspec-from-file=- HEAD^1 &&
+
+ cat >expect <<-\EOF &&
+ M fileA.t
+ M fileB.t
+ EOF
+ verify_expect
+'
+
+test_expect_success 'no trailing delimiter' '
+ restore_checkpoint &&
+
+ printf "fileA.t\nfileB.t" | git checkout --pathspec-from-file=- HEAD^1 &&
+
+ cat >expect <<-\EOF &&
+ M fileA.t
+ M fileB.t
+ EOF
+ verify_expect
+'
+
+test_expect_success 'CRLF delimiters' '
+ restore_checkpoint &&
+
+ printf "fileA.t\r\nfileB.t\r\n" | git checkout --pathspec-from-file=- HEAD^1 &&
+
+ cat >expect <<-\EOF &&
+ M fileA.t
+ M fileB.t
+ EOF
+ verify_expect
+'
+
+test_expect_success 'quotes' '
+ restore_checkpoint &&
+
+ cat >list <<-\EOF &&
+ "file\101.t"
+ EOF
+
+ git checkout --pathspec-from-file=list HEAD^1 &&
+
+ cat >expect <<-\EOF &&
+ M fileA.t
+ EOF
+ verify_expect
+'
+
+test_expect_success 'quotes not compatible with --pathspec-file-nul' '
+ restore_checkpoint &&
+
+ cat >list <<-\EOF &&
+ "file\101.t"
+ EOF
+
+ test_must_fail git checkout --pathspec-from-file=list --pathspec-file-nul HEAD^1
+'
+
+test_expect_success 'only touches what was listed' '
+ restore_checkpoint &&
+
+ printf "fileB.t\nfileC.t\n" | git checkout --pathspec-from-file=- HEAD^1 &&
+
+ cat >expect <<-\EOF &&
+ M fileB.t
+ M fileC.t
+ EOF
+ verify_expect
+'
+
+test_expect_success 'error conditions' '
+ restore_checkpoint &&
+ echo fileA.t >list &&
+
+ test_must_fail git checkout --pathspec-from-file=list --detach 2>err &&
+ test_grep -e "options .--pathspec-from-file. and .--detach. cannot be used together" err &&
+
+ test_must_fail git checkout --pathspec-from-file=list --patch 2>err &&
+ test_grep -e "options .--pathspec-from-file. and .--patch. cannot be used together" err &&
+
+ test_must_fail git checkout --pathspec-from-file=list -- fileA.t 2>err &&
+ test_grep -e ".--pathspec-from-file. and pathspec arguments cannot be used together" err &&
+
+ test_must_fail git checkout --pathspec-file-nul 2>err &&
+ test_grep -e "the option .--pathspec-file-nul. requires .--pathspec-from-file." err
+'
+
+test_done
diff --git a/t/t2027-checkout-track.sh b/t/t2027-checkout-track.sh
new file mode 100755
index 0000000..98f16c7
--- /dev/null
+++ b/t/t2027-checkout-track.sh
@@ -0,0 +1,51 @@
+#!/bin/sh
+
+test_description='tests for git branch --track'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ test_commit one &&
+ test_commit two
+'
+
+test_expect_success 'checkout --track -b creates a new tracking branch' '
+ git checkout --track -b branch1 main &&
+ test $(git rev-parse --abbrev-ref HEAD) = branch1 &&
+ test $(git config --get branch.branch1.remote) = . &&
+ test $(git config --get branch.branch1.merge) = refs/heads/main
+'
+
+test_expect_success 'checkout --track -b rejects an extra path argument' '
+ test_must_fail git checkout --track -b branch2 main one.t 2>err &&
+ test_grep "cannot be used with updating paths" err
+'
+
+test_expect_success 'checkout --track -b overrides autoSetupMerge=inherit' '
+ # Set up tracking config on main
+ test_config branch.main.remote origin &&
+ test_config branch.main.merge refs/heads/some-branch &&
+ test_config branch.autoSetupMerge inherit &&
+ # With --track=inherit, we copy the tracking config from main
+ git checkout --track=inherit -b b1 main &&
+ test_cmp_config origin branch.b1.remote &&
+ test_cmp_config refs/heads/some-branch branch.b1.merge &&
+ # With branch.autoSetupMerge=inherit, we do the same
+ git checkout -b b2 main &&
+ test_cmp_config origin branch.b2.remote &&
+ test_cmp_config refs/heads/some-branch branch.b2.merge &&
+ # But --track overrides this
+ git checkout --track -b b3 main &&
+ test_cmp_config . branch.b3.remote &&
+ test_cmp_config refs/heads/main branch.b3.merge &&
+ # And --track=direct does as well
+ git checkout --track=direct -b b4 main &&
+ test_cmp_config . branch.b4.remote &&
+ test_cmp_config refs/heads/main branch.b4.merge
+'
+
+test_done
diff --git a/t/t2030-unresolve-info.sh b/t/t2030-unresolve-info.sh
new file mode 100755
index 0000000..be3fcdd
--- /dev/null
+++ b/t/t2030-unresolve-info.sh
@@ -0,0 +1,305 @@
+#!/bin/sh
+
+test_description='undoing resolution'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+check_resolve_undo () {
+ msg=$1
+ shift
+ while case $# in
+ 0) break ;;
+ 1|2|3) die "Bug in check-resolve-undo test" ;;
+ esac
+ do
+ path=$1
+ shift
+ for stage in 1 2 3
+ do
+ sha1=$1
+ shift
+ case "$sha1" in
+ '') continue ;;
+ esac
+ sha1=$(git rev-parse --verify "$sha1")
+ printf "100644 %s %s\t%s\n" $sha1 $stage $path
+ done
+ done >"$msg.expect" &&
+ git ls-files --resolve-undo >"$msg.actual" &&
+ test_cmp "$msg.expect" "$msg.actual"
+}
+
+prime_resolve_undo () {
+ git reset --hard &&
+ git checkout second^0 &&
+ test_tick &&
+ test_must_fail git merge third^0 &&
+ check_resolve_undo empty &&
+
+ # how should the conflict be resolved?
+ case "$1" in
+ remove)
+ rm -f file/le && git rm fi/le
+ ;;
+ *) # modify
+ echo different >fi/le && git add fi/le
+ ;;
+ esac
+ check_resolve_undo recorded fi/le initial:fi/le second:fi/le third:fi/le
+}
+
+test_expect_success setup '
+ mkdir fi &&
+ printf "a\0a" >binary &&
+ git add binary &&
+ test_commit initial fi/le first &&
+ git branch side &&
+ git branch another &&
+ printf "a\0b" >binary &&
+ git add binary &&
+ test_commit second fi/le second &&
+ git checkout side &&
+ test_commit third fi/le third &&
+ git branch add-add &&
+ git checkout another &&
+ test_commit fourth fi/le fourth &&
+ git checkout add-add &&
+ test_commit fifth add-differently &&
+ git checkout main
+'
+
+test_expect_success 'add records switch clears' '
+ prime_resolve_undo &&
+ test_tick &&
+ git commit -m merged &&
+ echo committing keeps &&
+ check_resolve_undo kept fi/le initial:fi/le second:fi/le third:fi/le &&
+ git checkout second^0 &&
+ echo switching clears &&
+ check_resolve_undo cleared
+'
+
+test_expect_success 'rm records reset clears' '
+ prime_resolve_undo &&
+ test_tick &&
+ git commit -m merged &&
+ echo committing keeps &&
+ check_resolve_undo kept fi/le initial:fi/le second:fi/le third:fi/le &&
+
+ echo merge clears upfront &&
+ test_must_fail git merge fourth^0 &&
+ check_resolve_undo nuked &&
+
+ git rm -f fi/le &&
+ echo resolving records &&
+ check_resolve_undo recorded fi/le initial:fi/le HEAD:fi/le fourth:fi/le &&
+
+ git reset --hard &&
+ echo resetting discards &&
+ check_resolve_undo discarded
+'
+
+test_expect_success 'plumbing clears' '
+ prime_resolve_undo &&
+ test_tick &&
+ git commit -m merged &&
+ echo committing keeps &&
+ check_resolve_undo kept fi/le initial:fi/le second:fi/le third:fi/le &&
+
+ echo plumbing clear &&
+ git update-index --clear-resolve-undo &&
+ check_resolve_undo cleared
+'
+
+test_expect_success 'add records checkout -m undoes' '
+ prime_resolve_undo &&
+ git diff HEAD &&
+ git checkout --conflict=merge fi/le &&
+ echo checkout used the record and removed it &&
+ check_resolve_undo removed &&
+ echo the index and the work tree is unmerged again &&
+ git diff >actual &&
+ grep "^++<<<<<<<" actual
+'
+
+test_expect_success 'unmerge with plumbing' '
+ prime_resolve_undo &&
+ git update-index --unresolve fi/le &&
+ git ls-files --resolve-undo fi/le >actual &&
+ test_must_be_empty actual &&
+ git ls-files -u >actual &&
+ test_line_count = 3 actual
+'
+
+test_expect_success 'unmerge can be done even after committing' '
+ prime_resolve_undo &&
+ git commit -m "record to nuke MERGE_HEAD" &&
+ git update-index --unresolve fi/le &&
+ git ls-files --resolve-undo fi/le >actual &&
+ test_must_be_empty actual &&
+ git ls-files -u >actual &&
+ test_line_count = 3 actual
+'
+
+test_expect_success 'unmerge removal' '
+ prime_resolve_undo remove &&
+ git update-index --unresolve fi/le &&
+ git ls-files --resolve-undo fi/le >actual &&
+ test_must_be_empty actual &&
+ git ls-files -u >actual &&
+ test_line_count = 3 actual
+'
+
+test_expect_success 'unmerge removal after committing' '
+ prime_resolve_undo remove &&
+ git commit -m "record to nuke MERGE_HEAD" &&
+ git update-index --unresolve fi/le &&
+ git ls-files --resolve-undo fi/le >actual &&
+ test_must_be_empty actual &&
+ git ls-files -u >actual &&
+ test_line_count = 3 actual
+'
+
+test_expect_success 'rerere and rerere forget' '
+ mkdir .git/rr-cache &&
+ prime_resolve_undo &&
+ echo record the resolution &&
+ git rerere &&
+ rerere_id=$(cd .git/rr-cache && echo */postimage) &&
+ rerere_id=${rerere_id%/postimage} &&
+ test -f .git/rr-cache/$rerere_id/postimage &&
+ git checkout -m fi/le &&
+ echo resurrect the conflict &&
+ grep "^=======" fi/le &&
+ echo reresolve the conflict &&
+ git rerere &&
+ test "z$(cat fi/le)" = zdifferent &&
+ echo register the resolution again &&
+ git add fi/le &&
+ check_resolve_undo kept fi/le initial:fi/le second:fi/le third:fi/le &&
+ test -z "$(git ls-files -u)" &&
+ git rerere forget fi/le &&
+ ! test -f .git/rr-cache/$rerere_id/postimage &&
+ tr "\0" "\n" <.git/MERGE_RR >actual &&
+ echo "$rerere_id fi/le" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rerere and rerere forget (subdirectory)' '
+ rm -fr .git/rr-cache &&
+ mkdir .git/rr-cache &&
+ prime_resolve_undo &&
+ echo record the resolution &&
+ (cd fi && git rerere) &&
+ rerere_id=$(cd .git/rr-cache && echo */postimage) &&
+ rerere_id=${rerere_id%/postimage} &&
+ test -f .git/rr-cache/$rerere_id/postimage &&
+ (cd fi && git checkout -m le) &&
+ echo resurrect the conflict &&
+ grep "^=======" fi/le &&
+ echo reresolve the conflict &&
+ (cd fi && git rerere) &&
+ test "z$(cat fi/le)" = zdifferent &&
+ echo register the resolution again &&
+ (cd fi && git add le) &&
+ check_resolve_undo kept fi/le initial:fi/le second:fi/le third:fi/le &&
+ test -z "$(git ls-files -u)" &&
+ (cd fi && git rerere forget le) &&
+ ! test -f .git/rr-cache/$rerere_id/postimage &&
+ tr "\0" "\n" <.git/MERGE_RR >actual &&
+ echo "$rerere_id fi/le" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rerere forget (binary)' '
+ git checkout -f side &&
+ test_commit --printf binary binary "a\0c" &&
+ test_must_fail git merge second &&
+ git rerere forget binary
+'
+
+test_expect_success 'rerere forget (add-add conflict)' '
+ git checkout -f main &&
+ echo main >add-differently &&
+ git add add-differently &&
+ git commit -m "add differently" &&
+ test_must_fail git merge fifth &&
+ git rerere forget add-differently 2>actual &&
+ test_grep "no remembered" actual
+'
+
+test_expect_success 'resolve-undo keeps blobs from gc' '
+ git checkout -f main &&
+
+ # First make sure we do not have any cruft left in the object store
+ git repack -a -d &&
+ git prune --expire=now &&
+ git prune-packed &&
+ git gc --prune=now &&
+ git fsck --unreachable >cruft &&
+ test_must_be_empty cruft &&
+
+ # Now add three otherwise unreferenced blob objects to the index
+ git reset --hard &&
+ B1=$(echo "resolve undo test data 1" | git hash-object -w --stdin) &&
+ B2=$(echo "resolve undo test data 2" | git hash-object -w --stdin) &&
+ B3=$(echo "resolve undo test data 3" | git hash-object -w --stdin) &&
+ git update-index --add --index-info <<-EOF &&
+ 100644 $B1 1 frotz
+ 100644 $B2 2 frotz
+ 100644 $B3 3 frotz
+ EOF
+
+ # These three blob objects are reachable (only) from the index
+ git fsck --unreachable >cruft &&
+ test_must_be_empty cruft &&
+ # and they should be protected from GC
+ git gc --prune=now &&
+ git cat-file -e $B1 &&
+ git cat-file -e $B2 &&
+ git cat-file -e $B3 &&
+
+ # Now resolve the conflicted path
+ B0=$(echo "resolve undo test data 0" | git hash-object -w --stdin) &&
+ git update-index --add --cacheinfo 100644,$B0,frotz &&
+
+ # These three blob objects are now reachable only from the resolve-undo
+ git fsck --unreachable >cruft &&
+ test_must_be_empty cruft &&
+
+ # and they should survive GC
+ git gc --prune=now &&
+ git cat-file -e $B0 &&
+ git cat-file -e $B1 &&
+ git cat-file -e $B2 &&
+ git cat-file -e $B3 &&
+
+ # Now we switch away, which nukes resolve-undo, and
+ # blobs B0..B3 would become dangling. fsck should
+ # notice that they are now unreachable.
+ git checkout -f side &&
+ git fsck --unreachable >cruft &&
+ sort cruft >actual &&
+ sort <<-EOF >expect &&
+ unreachable blob $B0
+ unreachable blob $B1
+ unreachable blob $B2
+ unreachable blob $B3
+ EOF
+ test_cmp expect actual &&
+
+ # And they should go away when gc runs.
+ git gc --prune=now &&
+ git fsck --unreachable >cruft &&
+ test_must_be_empty cruft &&
+
+ test_must_fail git cat-file -e $B0 &&
+ test_must_fail git cat-file -e $B1 &&
+ test_must_fail git cat-file -e $B2 &&
+ test_must_fail git cat-file -e $B3
+'
+
+test_done
diff --git a/t/t2050-git-dir-relative.sh b/t/t2050-git-dir-relative.sh
new file mode 100755
index 0000000..1f193cd
--- /dev/null
+++ b/t/t2050-git-dir-relative.sh
@@ -0,0 +1,56 @@
+#!/bin/sh
+
+test_description='check problems with relative GIT_DIR
+
+This test creates a working tree state with a file and subdir:
+
+ top (committed several times)
+ subdir (a subdirectory)
+
+It creates a commit-hook and tests it, then moves .git
+into the subdir while keeping the worktree location,
+and tries commits from the top and the subdir, checking
+that the commit-hook still gets called.'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+COMMIT_FILE="$(pwd)/output"
+export COMMIT_FILE
+
+test_expect_success 'Setting up post-commit hook' '
+mkdir -p .git/hooks &&
+echo >.git/hooks/post-commit "#!/bin/sh
+touch \"\${COMMIT_FILE}\"
+echo Post commit hook was called." &&
+chmod +x .git/hooks/post-commit'
+
+test_expect_success 'post-commit hook used ordinarily' '
+echo initial >top &&
+git add top &&
+git commit -m initial &&
+test -r "${COMMIT_FILE}"
+'
+
+rm -rf "${COMMIT_FILE}"
+mkdir subdir
+mv .git subdir
+
+test_expect_success 'post-commit-hook created and used from top dir' '
+echo changed >top &&
+git --git-dir subdir/.git add top &&
+git --git-dir subdir/.git commit -m topcommit &&
+test -r "${COMMIT_FILE}"
+'
+
+rm -rf "${COMMIT_FILE}"
+
+test_expect_success 'post-commit-hook from sub dir' '
+echo changed again >top &&
+cd subdir &&
+git --git-dir .git --work-tree .. add ../top &&
+git --git-dir .git --work-tree .. commit -m subcommit &&
+test -r "${COMMIT_FILE}"
+'
+
+test_done
diff --git a/t/t2060-switch.sh b/t/t2060-switch.sh
new file mode 100755
index 0000000..e247a47
--- /dev/null
+++ b/t/t2060-switch.sh
@@ -0,0 +1,178 @@
+#!/bin/sh
+
+test_description='switch basic functionality'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ test_commit first &&
+ git branch first-branch &&
+ test_commit second &&
+ test_commit third &&
+ git remote add origin nohost:/nopath &&
+ git update-ref refs/remotes/origin/foo first-branch
+'
+
+test_expect_success 'switch branch no arguments' '
+ test_must_fail git switch
+'
+
+test_expect_success 'switch branch' '
+ git switch first-branch &&
+ test_path_is_missing second.t
+'
+
+test_expect_success 'switch and detach' '
+ test_when_finished git switch main &&
+ test_must_fail git switch main^{commit} &&
+ git switch --detach main^{commit} &&
+ test_must_fail git symbolic-ref HEAD
+'
+
+test_expect_success 'suggestion to detach' '
+ test_must_fail git switch main^{commit} 2>stderr &&
+ grep "try again with the --detach option" stderr
+'
+
+test_expect_success 'suggestion to detach is suppressed with advice.suggestDetachingHead=false' '
+ test_config advice.suggestDetachingHead false &&
+ test_must_fail git switch main^{commit} 2>stderr &&
+ ! grep "try again with the --detach option" stderr
+'
+
+test_expect_success 'switch and detach current branch' '
+ test_when_finished git switch main &&
+ git switch main &&
+ git switch --detach &&
+ test_must_fail git symbolic-ref HEAD
+'
+
+test_expect_success 'switch and create branch' '
+ test_when_finished git switch main &&
+ git switch -c temp main^ &&
+ test_cmp_rev main^ refs/heads/temp &&
+ echo refs/heads/temp >expected-branch &&
+ git symbolic-ref HEAD >actual-branch &&
+ test_cmp expected-branch actual-branch
+'
+
+test_expect_success 'force create branch from HEAD' '
+ test_when_finished git switch main &&
+ git switch --detach main &&
+ test_must_fail git switch -c temp &&
+ git switch -C temp &&
+ test_cmp_rev main refs/heads/temp &&
+ echo refs/heads/temp >expected-branch &&
+ git symbolic-ref HEAD >actual-branch &&
+ test_cmp expected-branch actual-branch
+'
+
+test_expect_success 'new orphan branch from empty' '
+ test_when_finished git switch main &&
+ test_must_fail git switch --orphan new-orphan HEAD &&
+ git switch --orphan new-orphan &&
+ test_commit orphan &&
+ git cat-file commit refs/heads/new-orphan >commit &&
+ ! grep ^parent commit &&
+ git ls-files >tracked-files &&
+ echo orphan.t >expected &&
+ test_cmp expected tracked-files
+'
+
+test_expect_success 'orphan branch works with --discard-changes' '
+ test_when_finished git switch main &&
+ echo foo >foo.txt &&
+ git switch --discard-changes --orphan new-orphan2 &&
+ git ls-files >tracked-files &&
+ test_must_be_empty tracked-files
+'
+
+test_expect_success 'switching ignores file of same branch name' '
+ test_when_finished git switch main &&
+ : >first-branch &&
+ git switch first-branch &&
+ echo refs/heads/first-branch >expected &&
+ git symbolic-ref HEAD >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'guess and create branch' '
+ test_when_finished git switch main &&
+ test_must_fail git switch --no-guess foo &&
+ test_config checkout.guess false &&
+ test_must_fail git switch foo &&
+ test_config checkout.guess true &&
+ git switch foo &&
+ echo refs/heads/foo >expected &&
+ git symbolic-ref HEAD >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'not switching when something is in progress' '
+ test_when_finished rm -f .git/MERGE_HEAD &&
+ # fake a merge-in-progress
+ cp .git/HEAD .git/MERGE_HEAD &&
+ test_must_fail git switch -d @^
+'
+
+test_expect_success 'tracking info copied with autoSetupMerge=inherit' '
+ # default config does not copy tracking info
+ git switch -c foo-no-inherit foo &&
+ test_cmp_config "" --default "" branch.foo-no-inherit.remote &&
+ test_cmp_config "" --default "" branch.foo-no-inherit.merge &&
+ # with --track=inherit, we copy tracking info from foo
+ git switch --track=inherit -c foo2 foo &&
+ test_cmp_config origin branch.foo2.remote &&
+ test_cmp_config refs/heads/foo branch.foo2.merge &&
+ # with autoSetupMerge=inherit, we do the same
+ test_config branch.autoSetupMerge inherit &&
+ git switch -c foo3 foo &&
+ test_cmp_config origin branch.foo3.remote &&
+ test_cmp_config refs/heads/foo branch.foo3.merge &&
+ # with --track, we override autoSetupMerge
+ git switch --track -c foo4 foo &&
+ test_cmp_config . branch.foo4.remote &&
+ test_cmp_config refs/heads/foo branch.foo4.merge &&
+ # and --track=direct does as well
+ git switch --track=direct -c foo5 foo &&
+ test_cmp_config . branch.foo5.remote &&
+ test_cmp_config refs/heads/foo branch.foo5.merge &&
+ # no tracking info to inherit from main
+ git switch -c main2 main &&
+ test_cmp_config "" --default "" branch.main2.remote &&
+ test_cmp_config "" --default "" branch.main2.merge
+'
+
+test_expect_success 'switch back when temporarily detached and checked out elsewhere ' '
+ test_when_finished "
+ git worktree remove wt1 ||:
+ git worktree remove wt2 ||:
+ git checkout - ||:
+ git branch -D shared ||:
+ " &&
+ git checkout -b shared &&
+ test_commit shared-first &&
+ HASH1=$(git rev-parse --verify HEAD) &&
+ test_commit shared-second &&
+ test_commit shared-third &&
+ HASH2=$(git rev-parse --verify HEAD) &&
+ git worktree add wt1 -f shared &&
+ git -C wt1 bisect start &&
+ git -C wt1 bisect good $HASH1 &&
+ git -C wt1 bisect bad $HASH2 &&
+ git worktree add wt2 -f shared &&
+ git -C wt2 bisect start &&
+ git -C wt2 bisect good $HASH1 &&
+ git -C wt2 bisect bad $HASH2 &&
+ # we test in both worktrees to ensure that works
+ # as expected with "first" and "next" worktrees
+ test_must_fail git -C wt1 switch shared &&
+ git -C wt1 switch --ignore-other-worktrees shared &&
+ test_must_fail git -C wt2 switch shared &&
+ git -C wt2 switch --ignore-other-worktrees shared
+'
+
+test_done
diff --git a/t/t2070-restore.sh b/t/t2070-restore.sh
new file mode 100755
index 0000000..16d6348
--- /dev/null
+++ b/t/t2070-restore.sh
@@ -0,0 +1,223 @@
+#!/bin/sh
+
+test_description='restore basic functionality'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ test_commit first &&
+ echo first-and-a-half >>first.t &&
+ git add first.t &&
+ test_commit second &&
+ echo one >one &&
+ echo two >two &&
+ echo untracked >untracked &&
+ echo ignored >ignored &&
+ echo /ignored >.gitignore &&
+ git add one two .gitignore &&
+ git update-ref refs/heads/one main
+'
+
+test_expect_success 'restore without pathspec is not ok' '
+ test_must_fail git restore &&
+ test_must_fail git restore --source=first
+'
+
+test_expect_success 'restore a file, ignoring branch of same name' '
+ cat one >expected &&
+ echo dirty >>one &&
+ git restore one &&
+ test_cmp expected one
+'
+
+test_expect_success 'restore a file on worktree from another ref' '
+ test_when_finished git reset --hard &&
+ git cat-file blob first:./first.t >expected &&
+ git restore --source=first first.t &&
+ test_cmp expected first.t &&
+ git cat-file blob HEAD:./first.t >expected &&
+ git show :first.t >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'restore a file in the index from another ref' '
+ test_when_finished git reset --hard &&
+ git cat-file blob first:./first.t >expected &&
+ git restore --source=first --staged first.t &&
+ git show :first.t >actual &&
+ test_cmp expected actual &&
+ git cat-file blob HEAD:./first.t >expected &&
+ test_cmp expected first.t
+'
+
+test_expect_success 'restore a file in both the index and worktree from another ref' '
+ test_when_finished git reset --hard &&
+ git cat-file blob first:./first.t >expected &&
+ git restore --source=first --staged --worktree first.t &&
+ git show :first.t >actual &&
+ test_cmp expected actual &&
+ test_cmp expected first.t
+'
+
+test_expect_success 'restore --staged uses HEAD as source' '
+ test_when_finished git reset --hard &&
+ git cat-file blob :./first.t >expected &&
+ echo index-dirty >>first.t &&
+ git add first.t &&
+ git restore --staged first.t &&
+ git cat-file blob :./first.t >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'restore --worktree --staged uses HEAD as source' '
+ test_when_finished git reset --hard &&
+ git show HEAD:./first.t >expected &&
+ echo dirty >>first.t &&
+ git add first.t &&
+ git restore --worktree --staged first.t &&
+ git show :./first.t >actual &&
+ test_cmp expected actual &&
+ test_cmp expected first.t
+'
+
+test_expect_success 'restore --ignore-unmerged ignores unmerged entries' '
+ git init unmerged &&
+ (
+ cd unmerged &&
+ echo one >unmerged &&
+ echo one >common &&
+ git add unmerged common &&
+ git commit -m common &&
+ git switch -c first &&
+ echo first >unmerged &&
+ git commit -am first &&
+ git switch -c second main &&
+ echo second >unmerged &&
+ git commit -am second &&
+ test_must_fail git merge first &&
+
+ echo dirty >>common &&
+ test_must_fail git restore . &&
+
+ git restore --ignore-unmerged --quiet . >output 2>&1 &&
+ git diff common >diff-output &&
+ test_must_be_empty output &&
+ test_must_be_empty diff-output
+ )
+'
+
+test_expect_success 'restore --staged adds deleted intent-to-add file back to index' '
+ echo "nonempty" >nonempty &&
+ >empty &&
+ git add nonempty empty &&
+ git commit -m "create files to be deleted" &&
+ git rm --cached nonempty empty &&
+ git add -N nonempty empty &&
+ git restore --staged nonempty empty &&
+ git diff --cached --exit-code
+'
+
+test_expect_success 'restore --staged invalidates cache tree for deletions' '
+ test_when_finished git reset --hard &&
+ >new1 &&
+ >new2 &&
+ git add new1 new2 &&
+
+ # It is important to commit and then reset here, so that the index
+ # contains a valid cache-tree for the "both" tree.
+ git commit -m both &&
+ git reset --soft HEAD^ &&
+
+ git restore --staged new1 &&
+ git commit -m "just new2" &&
+ git rev-parse HEAD:new2 &&
+ test_must_fail git rev-parse HEAD:new1
+'
+
+test_expect_success 'restore --merge to unresolve' '
+ O=$(echo original | git hash-object -w --stdin) &&
+ A=$(echo ourside | git hash-object -w --stdin) &&
+ B=$(echo theirside | git hash-object -w --stdin) &&
+ {
+ echo "100644 $O 1 file" &&
+ echo "100644 $A 2 file" &&
+ echo "100644 $B 3 file"
+ } | git update-index --index-info &&
+ echo nothing >file &&
+ git restore --worktree --merge file &&
+ cat >expect <<-\EOF &&
+ <<<<<<< ours
+ ourside
+ =======
+ theirside
+ >>>>>>> theirs
+ EOF
+ test_cmp expect file
+'
+
+test_expect_success 'restore --merge to unresolve after (mistaken) resolution' '
+ O=$(echo original | git hash-object -w --stdin) &&
+ A=$(echo ourside | git hash-object -w --stdin) &&
+ B=$(echo theirside | git hash-object -w --stdin) &&
+ {
+ echo "100644 $O 1 file" &&
+ echo "100644 $A 2 file" &&
+ echo "100644 $B 3 file"
+ } | git update-index --index-info &&
+ echo nothing >file &&
+ git add file &&
+ git restore --worktree --merge file &&
+ cat >expect <<-\EOF &&
+ <<<<<<< ours
+ ourside
+ =======
+ theirside
+ >>>>>>> theirs
+ EOF
+ test_cmp expect file
+'
+
+test_expect_success 'restore --merge to unresolve after (mistaken) resolution' '
+ O=$(echo original | git hash-object -w --stdin) &&
+ A=$(echo ourside | git hash-object -w --stdin) &&
+ B=$(echo theirside | git hash-object -w --stdin) &&
+ {
+ echo "100644 $O 1 file" &&
+ echo "100644 $A 2 file" &&
+ echo "100644 $B 3 file"
+ } | git update-index --index-info &&
+ git rm -f file &&
+ git restore --worktree --merge file &&
+ cat >expect <<-\EOF &&
+ <<<<<<< ours
+ ourside
+ =======
+ theirside
+ >>>>>>> theirs
+ EOF
+ test_cmp expect file
+'
+
+test_expect_success 'restore with merge options are incompatible with certain options' '
+ for opts in \
+ "--staged --ours" \
+ "--staged --theirs" \
+ "--staged --merge" \
+ "--source=HEAD --ours" \
+ "--source=HEAD --theirs" \
+ "--source=HEAD --merge" \
+ "--staged --conflict=diff3" \
+ "--staged --worktree --ours" \
+ "--staged --worktree --theirs" \
+ "--staged --worktree --merge" \
+ "--staged --worktree --conflict=zdiff3"
+ do
+ test_must_fail git restore $opts . 2>err &&
+ grep "cannot be used" err || return
+ done
+'
+
+test_done
diff --git a/t/t2071-restore-patch.sh b/t/t2071-restore-patch.sh
new file mode 100755
index 0000000..b5c5c0f
--- /dev/null
+++ b/t/t2071-restore-patch.sh
@@ -0,0 +1,118 @@
+#!/bin/sh
+
+test_description='git restore --patch'
+
+. ./lib-patch-mode.sh
+
+test_expect_success PERL 'setup' '
+ mkdir dir &&
+ echo parent >dir/foo &&
+ echo dummy >bar &&
+ git add bar dir/foo &&
+ git commit -m initial &&
+ test_tick &&
+ test_commit second dir/foo head &&
+ set_and_save_state bar bar_work bar_index &&
+ save_head
+'
+
+test_expect_success PERL 'restore -p without pathspec is fine' '
+ echo q >cmd &&
+ git restore -p <cmd
+'
+
+# note: bar sorts before dir/foo, so the first 'n' is always to skip 'bar'
+
+test_expect_success PERL 'saying "n" does nothing' '
+ set_and_save_state dir/foo work head &&
+ test_write_lines n n | git restore -p &&
+ verify_saved_state bar &&
+ verify_saved_state dir/foo
+'
+
+test_expect_success PERL 'git restore -p' '
+ set_and_save_state dir/foo work head &&
+ test_write_lines n y | git restore -p &&
+ verify_saved_state bar &&
+ verify_state dir/foo head head
+'
+
+test_expect_success PERL 'git restore -p with staged changes' '
+ set_state dir/foo work index &&
+ test_write_lines n y | git restore -p &&
+ verify_saved_state bar &&
+ verify_state dir/foo index index
+'
+
+test_expect_success PERL 'git restore -p --source=HEAD' '
+ set_state dir/foo work index &&
+ # the third n is to get out in case it mistakenly does not apply
+ test_write_lines n y n | git restore -p --source=HEAD &&
+ verify_saved_state bar &&
+ verify_state dir/foo head index
+'
+
+test_expect_success PERL 'git restore -p --source=HEAD^' '
+ set_state dir/foo work index &&
+ # the third n is to get out in case it mistakenly does not apply
+ test_write_lines n y n | git restore -p --source=HEAD^ &&
+ verify_saved_state bar &&
+ verify_state dir/foo parent index
+'
+
+test_expect_success PERL 'git restore -p --source=HEAD^...' '
+ set_state dir/foo work index &&
+ # the third n is to get out in case it mistakenly does not apply
+ test_write_lines n y n | git restore -p --source=HEAD^... &&
+ verify_saved_state bar &&
+ verify_state dir/foo parent index
+'
+
+test_expect_success PERL 'git restore -p handles deletion' '
+ set_state dir/foo work index &&
+ rm dir/foo &&
+ test_write_lines n y | git restore -p &&
+ verify_saved_state bar &&
+ verify_state dir/foo index index
+'
+
+# The idea in the rest is that bar sorts first, so we always say 'y'
+# first and if the path limiter fails it'll apply to bar instead of
+# dir/foo. There's always an extra 'n' to reject edits to dir/foo in
+# the failure case (and thus get out of the loop).
+
+test_expect_success PERL 'path limiting works: dir' '
+ set_state dir/foo work head &&
+ test_write_lines y n | git restore -p dir &&
+ verify_saved_state bar &&
+ verify_state dir/foo head head
+'
+
+test_expect_success PERL 'path limiting works: -- dir' '
+ set_state dir/foo work head &&
+ test_write_lines y n | git restore -p -- dir &&
+ verify_saved_state bar &&
+ verify_state dir/foo head head
+'
+
+test_expect_success PERL 'path limiting works: HEAD^ -- dir' '
+ set_state dir/foo work head &&
+ # the third n is to get out in case it mistakenly does not apply
+ test_write_lines y n n | git restore -p --source=HEAD^ -- dir &&
+ verify_saved_state bar &&
+ verify_state dir/foo parent head
+'
+
+test_expect_success PERL 'path limiting works: foo inside dir' '
+ set_state dir/foo work head &&
+ # the third n is to get out in case it mistakenly does not apply
+ test_write_lines y n n | (cd dir && git restore -p foo) &&
+ verify_saved_state bar &&
+ verify_state dir/foo head head
+'
+
+test_expect_success PERL 'none of this moved HEAD' '
+ verify_saved_head
+'
+
+test_done
diff --git a/t/t2072-restore-pathspec-file.sh b/t/t2072-restore-pathspec-file.sh
new file mode 100755
index 0000000..8198a1e
--- /dev/null
+++ b/t/t2072-restore-pathspec-file.sh
@@ -0,0 +1,177 @@
+#!/bin/sh
+
+test_description='restore --pathspec-from-file'
+
+. ./test-lib.sh
+
+test_tick
+
+test_expect_success setup '
+ test_commit file0 &&
+
+ mkdir dir1 &&
+ echo 1 >dir1/file &&
+ echo 1 >fileA.t &&
+ echo 1 >fileB.t &&
+ echo 1 >fileC.t &&
+ echo 1 >fileD.t &&
+ git add dir1 fileA.t fileB.t fileC.t fileD.t &&
+ git commit -m "files 1" &&
+
+ echo 2 >dir1/file &&
+ echo 2 >fileA.t &&
+ echo 2 >fileB.t &&
+ echo 2 >fileC.t &&
+ echo 2 >fileD.t &&
+ git add dir1 fileA.t fileB.t fileC.t fileD.t &&
+ git commit -m "files 2" &&
+
+ git tag checkpoint
+'
+
+restore_checkpoint () {
+ git reset --hard checkpoint
+}
+
+verify_expect () {
+ git status --porcelain --untracked-files=no -- dir1 fileA.t fileB.t fileC.t fileD.t >actual &&
+ test_cmp expect actual
+}
+
+test_expect_success '--pathspec-from-file from stdin' '
+ restore_checkpoint &&
+
+ echo fileA.t | git restore --pathspec-from-file=- --source=HEAD^1 &&
+
+ cat >expect <<-\EOF &&
+ M fileA.t
+ EOF
+ verify_expect
+'
+
+test_expect_success '--pathspec-from-file from file' '
+ restore_checkpoint &&
+
+ echo fileA.t >list &&
+ git restore --pathspec-from-file=list --source=HEAD^1 &&
+
+ cat >expect <<-\EOF &&
+ M fileA.t
+ EOF
+ verify_expect
+'
+
+test_expect_success 'NUL delimiters' '
+ restore_checkpoint &&
+
+ printf "fileA.t\0fileB.t\0" | git restore --pathspec-from-file=- --pathspec-file-nul --source=HEAD^1 &&
+
+ cat >expect <<-\EOF &&
+ M fileA.t
+ M fileB.t
+ EOF
+ verify_expect
+'
+
+test_expect_success 'LF delimiters' '
+ restore_checkpoint &&
+
+ printf "fileA.t\nfileB.t\n" | git restore --pathspec-from-file=- --source=HEAD^1 &&
+
+ cat >expect <<-\EOF &&
+ M fileA.t
+ M fileB.t
+ EOF
+ verify_expect
+'
+
+test_expect_success 'no trailing delimiter' '
+ restore_checkpoint &&
+
+ printf "fileA.t\nfileB.t" | git restore --pathspec-from-file=- --source=HEAD^1 &&
+
+ cat >expect <<-\EOF &&
+ M fileA.t
+ M fileB.t
+ EOF
+ verify_expect
+'
+
+test_expect_success 'CRLF delimiters' '
+ restore_checkpoint &&
+
+ printf "fileA.t\r\nfileB.t\r\n" | git restore --pathspec-from-file=- --source=HEAD^1 &&
+
+ cat >expect <<-\EOF &&
+ M fileA.t
+ M fileB.t
+ EOF
+ verify_expect
+'
+
+test_expect_success 'quotes' '
+ restore_checkpoint &&
+
+ cat >list <<-\EOF &&
+ "file\101.t"
+ EOF
+
+ git restore --pathspec-from-file=list --source=HEAD^1 &&
+
+ cat >expect <<-\EOF &&
+ M fileA.t
+ EOF
+ verify_expect
+'
+
+test_expect_success 'quotes not compatible with --pathspec-file-nul' '
+ restore_checkpoint &&
+
+ cat >list <<-\EOF &&
+ "file\101.t"
+ EOF
+
+ test_must_fail git restore --pathspec-from-file=list --pathspec-file-nul --source=HEAD^1
+'
+
+test_expect_success 'only touches what was listed' '
+ restore_checkpoint &&
+
+ printf "fileB.t\nfileC.t\n" | git restore --pathspec-from-file=- --source=HEAD^1 &&
+
+ cat >expect <<-\EOF &&
+ M fileB.t
+ M fileC.t
+ EOF
+ verify_expect
+'
+
+test_expect_success 'error conditions' '
+ restore_checkpoint &&
+ echo fileA.t >list &&
+ >empty_list &&
+
+ test_must_fail git restore --pathspec-from-file=list --patch --source=HEAD^1 2>err &&
+ test_grep -e "options .--pathspec-from-file. and .--patch. cannot be used together" err &&
+
+ test_must_fail git restore --pathspec-from-file=list --source=HEAD^1 -- fileA.t 2>err &&
+ test_grep -e ".--pathspec-from-file. and pathspec arguments cannot be used together" err &&
+
+ test_must_fail git restore --pathspec-file-nul --source=HEAD^1 2>err &&
+ test_grep -e "the option .--pathspec-file-nul. requires .--pathspec-from-file." err &&
+
+ test_must_fail git restore --pathspec-from-file=empty_list --source=HEAD^1 2>err &&
+ test_grep -e "you must specify path(s) to restore" err
+'
+
+test_expect_success 'wildcard pathspec matches file in subdirectory' '
+ restore_checkpoint &&
+
+ echo "*file" | git restore --pathspec-from-file=- --source=HEAD^1 &&
+ cat >expect <<-\EOF &&
+ M dir1/file
+ EOF
+ verify_expect
+'
+
+test_done
diff --git a/t/t2080-parallel-checkout-basics.sh b/t/t2080-parallel-checkout-basics.sh
new file mode 100755
index 0000000..5ffe1a4
--- /dev/null
+++ b/t/t2080-parallel-checkout-basics.sh
@@ -0,0 +1,277 @@
+#!/bin/sh
+
+test_description='parallel-checkout basics
+
+Ensure that parallel-checkout basically works on clone and checkout, spawning
+the required number of workers and correctly populating both the index and the
+working tree.
+'
+
+TEST_NO_CREATE_REPO=1
+. ./test-lib.sh
+. "$TEST_DIRECTORY/lib-parallel-checkout.sh"
+
+# Test parallel-checkout with a branch switch containing a variety of file
+# creations, deletions, and modifications, involving different entry types.
+# The branches B1 and B2 have the following paths:
+#
+# B1 B2
+# a/a (file) a (file)
+# b (file) b/b (file)
+#
+# c/c (file) c (symlink)
+# d (symlink) d/d (file)
+#
+# e/e (file) e (submodule)
+# f (submodule) f/f (file)
+#
+# g (submodule) g (symlink)
+# h (symlink) h (submodule)
+#
+# Additionally, the following paths are present on both branches, but with
+# different contents:
+#
+# i (file) i (file)
+# j (symlink) j (symlink)
+# k (submodule) k (submodule)
+#
+# And the following paths are only present in one of the branches:
+#
+# l/l (file) -
+# - m/m (file)
+#
+test_expect_success 'setup repo for checkout with various types of changes' '
+ test_config_global protocol.file.allow always &&
+
+ git init sub &&
+ (
+ cd sub &&
+ git checkout -b B2 &&
+ echo B2 >file &&
+ git add file &&
+ git commit -m file &&
+
+ git checkout -b B1 &&
+ echo B1 >file &&
+ git add file &&
+ git commit -m file
+ ) &&
+
+ git init various &&
+ (
+ cd various &&
+
+ git checkout -b B1 &&
+ mkdir a c e &&
+ echo a/a >a/a &&
+ echo b >b &&
+ echo c/c >c/c &&
+ test_ln_s_add c d &&
+ echo e/e >e/e &&
+ git submodule add ../sub f &&
+ git submodule add ../sub g &&
+ test_ln_s_add c h &&
+
+ echo "B1 i" >i &&
+ test_ln_s_add c j &&
+ git submodule add -b B1 ../sub k &&
+ mkdir l &&
+ echo l/l >l/l &&
+
+ git add . &&
+ git commit -m B1 &&
+
+ git checkout -b B2 &&
+ git rm -rf :^.gitmodules :^k &&
+ mkdir b d f &&
+ echo a >a &&
+ echo b/b >b/b &&
+ test_ln_s_add b c &&
+ echo d/d >d/d &&
+ git submodule add ../sub e &&
+ echo f/f >f/f &&
+ test_ln_s_add b g &&
+ git submodule add ../sub h &&
+
+ echo "B2 i" >i &&
+ test_ln_s_add b j &&
+ git -C k checkout B2 &&
+ mkdir m &&
+ echo m/m >m/m &&
+
+ git add . &&
+ git commit -m B2 &&
+
+ git checkout --recurse-submodules B1
+ )
+'
+
+for mode in sequential parallel sequential-fallback
+do
+ case $mode in
+ sequential) workers=1 threshold=0 expected_workers=0 ;;
+ parallel) workers=2 threshold=0 expected_workers=2 ;;
+ sequential-fallback) workers=2 threshold=100 expected_workers=0 ;;
+ esac
+
+ test_expect_success "$mode checkout" '
+ repo=various_$mode &&
+ cp -R -P various $repo &&
+
+ # The just copied files have more recent timestamps than their
+ # associated index entries. So refresh the cached timestamps
+ # to avoid an "entry not up-to-date" error from `git checkout`.
+ # We only have to do this for the submodules as `git checkout`
+ # will already refresh the superproject index before performing
+ # the up-to-date check.
+ #
+ git -C $repo submodule foreach "git update-index --refresh" &&
+
+ set_checkout_config $workers $threshold &&
+ test_checkout_workers $expected_workers \
+ git -C $repo checkout --recurse-submodules B2 &&
+ verify_checkout $repo
+ '
+done
+
+for mode in parallel sequential-fallback
+do
+ case $mode in
+ parallel) workers=2 threshold=0 expected_workers=2 ;;
+ sequential-fallback) workers=2 threshold=100 expected_workers=0 ;;
+ esac
+
+ test_expect_success "$mode checkout on clone" '
+ test_config_global protocol.file.allow always &&
+ repo=various_${mode}_clone &&
+ set_checkout_config $workers $threshold &&
+ test_checkout_workers $expected_workers \
+ git clone --recurse-submodules --branch B2 various $repo &&
+ verify_checkout $repo
+ '
+done
+
+# Just to be paranoid, actually compare the working trees' contents directly.
+test_expect_success 'compare the working trees' '
+ rm -rf various_*/.git &&
+ rm -rf various_*/*/.git &&
+
+ # We use `git diff` instead of `diff -r` because the latter would
+ # follow symlinks, and not all `diff` implementations support the
+ # `--no-dereference` option.
+ #
+ git diff --no-index various_sequential various_parallel &&
+ git diff --no-index various_sequential various_parallel_clone &&
+ git diff --no-index various_sequential various_sequential-fallback &&
+ git diff --no-index various_sequential various_sequential-fallback_clone
+'
+
+# Currently, each submodule is checked out in a separated child process, but
+# these subprocesses must also be able to use parallel checkout workers to
+# write the submodules' entries.
+test_expect_success 'submodules can use parallel checkout' '
+ set_checkout_config 2 0 &&
+ git init super &&
+ (
+ cd super &&
+ git init sub &&
+ test_commit -C sub A &&
+ test_commit -C sub B &&
+ git submodule add ./sub &&
+ git commit -m sub &&
+ rm sub/* &&
+ test_checkout_workers 2 git checkout --recurse-submodules .
+ )
+'
+
+test_expect_success 'parallel checkout respects --[no]-force' '
+ set_checkout_config 2 0 &&
+ git init dirty &&
+ (
+ cd dirty &&
+ mkdir D &&
+ test_commit D/F &&
+ test_commit F &&
+
+ rm -rf D &&
+ echo changed >D &&
+ echo changed >F.t &&
+
+ # We expect 0 workers because there is nothing to be done
+ test_checkout_workers 0 git checkout HEAD &&
+ test_path_is_file D &&
+ grep changed D &&
+ grep changed F.t &&
+
+ test_checkout_workers 2 git checkout --force HEAD &&
+ test_path_is_dir D &&
+ grep D/F D/F.t &&
+ grep F F.t
+ )
+'
+
+test_expect_success SYMLINKS 'parallel checkout checks for symlinks in leading dirs' '
+ set_checkout_config 2 0 &&
+ git init symlinks &&
+ (
+ cd symlinks &&
+ mkdir D untracked &&
+ # Commit 2 files to have enough work for 2 parallel workers
+ test_commit D/A &&
+ test_commit D/B &&
+ rm -rf D &&
+ ln -s untracked D &&
+
+ test_checkout_workers 2 git checkout --force HEAD &&
+ ! test -h D &&
+ grep D/A D/A.t &&
+ grep D/B D/B.t
+ )
+'
+
+# This test is here (and not in e.g. t2022-checkout-paths.sh), because we
+# check the final report including sequential, parallel, and delayed entries
+# all at the same time. So we must have finer control of the parallel checkout
+# variables.
+test_expect_success '"git checkout ." report should not include failed entries' '
+ test_config_global filter.delay.process \
+ "test-tool rot13-filter --always-delay --log=delayed.log clean smudge delay" &&
+ test_config_global filter.delay.required true &&
+ test_config_global filter.cat.clean cat &&
+ test_config_global filter.cat.smudge cat &&
+ test_config_global filter.cat.required true &&
+
+ set_checkout_config 2 0 &&
+ git init failed_entries &&
+ (
+ cd failed_entries &&
+ cat >.gitattributes <<-EOF &&
+ *delay* filter=delay
+ parallel-ineligible* filter=cat
+ EOF
+ echo a >missing-delay.a &&
+ echo a >parallel-ineligible.a &&
+ echo a >parallel-eligible.a &&
+ echo b >success-delay.b &&
+ echo b >parallel-ineligible.b &&
+ echo b >parallel-eligible.b &&
+ git add -A &&
+ git commit -m files &&
+
+ a_blob="$(git rev-parse :parallel-ineligible.a)" &&
+ rm .git/objects/$(test_oid_to_path $a_blob) &&
+ rm *.a *.b &&
+
+ test_checkout_workers 2 test_must_fail git checkout . 2>err &&
+
+ # All *.b entries should succeed and all *.a entries should fail:
+ # - missing-delay.a: the delay filter will drop this path
+ # - parallel-*.a: the blob will be missing
+ #
+ grep "Updated 3 paths from the index" err &&
+ test_stdout_line_count = 3 ls *.b &&
+ ! ls *.a
+ )
+'
+
+test_done
diff --git a/t/t2081-parallel-checkout-collisions.sh b/t/t2081-parallel-checkout-collisions.sh
new file mode 100755
index 0000000..6acdb89
--- /dev/null
+++ b/t/t2081-parallel-checkout-collisions.sh
@@ -0,0 +1,163 @@
+#!/bin/sh
+
+test_description="path collisions during parallel checkout
+
+Parallel checkout must detect path collisions to:
+
+1) Avoid racily writing to different paths that represent the same file on disk.
+2) Report the colliding entries on clone.
+
+The tests in this file exercise parallel checkout's collision detection code in
+both these mechanics.
+"
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY/lib-parallel-checkout.sh"
+
+TEST_ROOT="$PWD"
+
+test_expect_success CASE_INSENSITIVE_FS 'setup' '
+ empty_oid=$(git hash-object -w --stdin </dev/null) &&
+ cat >objs <<-EOF &&
+ 100644 $empty_oid FILE_X
+ 100644 $empty_oid FILE_x
+ 100644 $empty_oid file_X
+ 100644 $empty_oid file_x
+ EOF
+ git update-index --index-info <objs &&
+ git commit -m "colliding files" &&
+ git tag basename_collision &&
+
+ write_script "$TEST_ROOT"/logger_script <<-\EOF
+ echo "$@" >>filter.log
+ EOF
+'
+
+test_workers_in_event_trace ()
+{
+ test $1 -eq $(grep ".event.:.child_start..*checkout--worker" $2 | wc -l)
+}
+
+test_expect_success CASE_INSENSITIVE_FS 'worker detects basename collision' '
+ GIT_TRACE2_EVENT="$(pwd)/trace" git \
+ -c checkout.workers=2 -c checkout.thresholdForParallelism=0 \
+ checkout . &&
+
+ test_workers_in_event_trace 2 trace &&
+ collisions=$(grep -i "category.:.pcheckout.,.key.:.collision/basename.,.value.:.file_x.}" trace | wc -l) &&
+ test $collisions -eq 3
+'
+
+test_expect_success CASE_INSENSITIVE_FS 'worker detects dirname collision' '
+ test_config filter.logger.smudge "\"$TEST_ROOT/logger_script\" %f" &&
+ empty_oid=$(git hash-object -w --stdin </dev/null) &&
+
+ # By setting a filter command to "a", we make it ineligible for parallel
+ # checkout, and thus it is checked out *first*. This way we can ensure
+ # that "A/B" and "A/C" will both collide with the regular file "a".
+ #
+ attr_oid=$(echo "a filter=logger" | git hash-object -w --stdin) &&
+
+ cat >objs <<-EOF &&
+ 100644 $empty_oid A/B
+ 100644 $empty_oid A/C
+ 100644 $empty_oid a
+ 100644 $attr_oid .gitattributes
+ EOF
+ git rm -rf . &&
+ git update-index --index-info <objs &&
+
+ rm -f trace filter.log &&
+ GIT_TRACE2_EVENT="$(pwd)/trace" git \
+ -c checkout.workers=2 -c checkout.thresholdForParallelism=0 \
+ checkout . &&
+
+ # Check that "a" (and only "a") was filtered
+ echo a >expected.log &&
+ test_cmp filter.log expected.log &&
+
+ # Check that it used the right number of workers and detected the collisions
+ test_workers_in_event_trace 2 trace &&
+ grep "category.:.pcheckout.,.key.:.collision/dirname.,.value.:.A/B.}" trace &&
+ grep "category.:.pcheckout.,.key.:.collision/dirname.,.value.:.A/C.}" trace
+'
+
+test_expect_success SYMLINKS,CASE_INSENSITIVE_FS 'do not follow symlinks colliding with leading dir' '
+ empty_oid=$(git hash-object -w --stdin </dev/null) &&
+ symlink_oid=$(echo "./e" | git hash-object -w --stdin) &&
+ mkdir e &&
+
+ cat >objs <<-EOF &&
+ 120000 $symlink_oid D
+ 100644 $empty_oid d/x
+ 100644 $empty_oid e/y
+ EOF
+ git rm -rf . &&
+ git update-index --index-info <objs &&
+
+ set_checkout_config 2 0 &&
+ test_checkout_workers 2 git checkout . &&
+ test_path_is_dir e &&
+ test_path_is_missing e/x
+'
+
+# The two following tests check that parallel checkout correctly reports
+# colliding entries on clone. The sequential code detects a collision by
+# calling lstat() before trying to open(O_CREAT) a file. (Note that this only
+# works for clone.) Then, to find the pair of a colliding item k, it searches
+# cache_entry[0, k-1]. This is not sufficient in parallel checkout because:
+#
+# - A colliding file may be created between the lstat() and open() calls;
+# - A colliding entry might appear in the second half of the cache_entry array.
+#
+test_expect_success CASE_INSENSITIVE_FS 'collision report on clone (w/ racy file creation)' '
+ git reset --hard basename_collision &&
+ set_checkout_config 2 0 &&
+ test_checkout_workers 2 git clone . clone-repo 2>stderr &&
+
+ grep FILE_X stderr &&
+ grep FILE_x stderr &&
+ grep file_X stderr &&
+ grep file_x stderr &&
+ grep "the following paths have collided" stderr
+'
+
+# This test ensures that the collision report code is correctly looking for
+# colliding peers in the second half of the cache_entry array. This is done by
+# defining a smudge command for the *last* array entry, which makes it
+# non-eligible for parallel-checkout. Thus, it is checked out *first*, before
+# spawning the workers.
+#
+# Note: this test doesn't work on Windows because, on this system, the
+# collision report code uses strcmp() to find the colliding pairs when
+# core.ignoreCase is false. And we need this setting for this test so that only
+# 'file_x' matches the pattern of the filter attribute. But the test works on
+# OSX, where the colliding pairs are found using inode.
+#
+test_expect_success CASE_INSENSITIVE_FS,!MINGW,!CYGWIN \
+ 'collision report on clone (w/ colliding peer after the detected entry)' '
+
+ test_config_global filter.logger.smudge "\"$TEST_ROOT/logger_script\" %f" &&
+ git reset --hard basename_collision &&
+ echo "file_x filter=logger" >.gitattributes &&
+ git add .gitattributes &&
+ git commit -m "filter for file_x" &&
+
+ rm -rf clone-repo &&
+ set_checkout_config 2 0 &&
+ test_checkout_workers 2 \
+ git -c core.ignoreCase=false clone . clone-repo 2>stderr &&
+
+ grep FILE_X stderr &&
+ grep FILE_x stderr &&
+ grep file_X stderr &&
+ grep file_x stderr &&
+ grep "the following paths have collided" stderr &&
+
+ # Check that only "file_x" was filtered
+ echo file_x >expected.log &&
+ test_cmp clone-repo/filter.log expected.log
+'
+
+test_done
diff --git a/t/t2082-parallel-checkout-attributes.sh b/t/t2082-parallel-checkout-attributes.sh
new file mode 100755
index 0000000..f3511cd
--- /dev/null
+++ b/t/t2082-parallel-checkout-attributes.sh
@@ -0,0 +1,191 @@
+#!/bin/sh
+
+test_description='parallel-checkout: attributes
+
+Verify that parallel-checkout correctly creates files that require
+conversions, as specified in .gitattributes. The main point here is
+to check that the conv_attr data is correctly sent to the workers
+and that it contains sufficient information to smudge files
+properly (without access to the index or attribute stack).
+'
+
+TEST_NO_CREATE_REPO=1
+. ./test-lib.sh
+. "$TEST_DIRECTORY/lib-parallel-checkout.sh"
+. "$TEST_DIRECTORY/lib-encoding.sh"
+
+test_expect_success 'parallel-checkout with ident' '
+ set_checkout_config 2 0 &&
+ git init ident &&
+ (
+ cd ident &&
+ echo "A ident" >.gitattributes &&
+ echo "\$Id\$" >A &&
+ echo "\$Id\$" >B &&
+ git add -A &&
+ git commit -m id &&
+
+ rm A B &&
+ test_checkout_workers 2 git reset --hard &&
+ hexsz=$(test_oid hexsz) &&
+ grep -E "\\\$Id: [0-9a-f]{$hexsz} \\\$" A &&
+ grep "\\\$Id\\\$" B
+ )
+'
+
+test_expect_success 'parallel-checkout with re-encoding' '
+ set_checkout_config 2 0 &&
+ git init encoding &&
+ (
+ cd encoding &&
+ echo text >utf8-text &&
+ write_utf16 <utf8-text >utf16-text &&
+
+ echo "A working-tree-encoding=UTF-16" >.gitattributes &&
+ cp utf16-text A &&
+ cp utf8-text B &&
+ git add A B .gitattributes &&
+ git commit -m encoding &&
+
+ # Check that A is stored in UTF-8
+ git cat-file -p :A >A.internal &&
+ test_cmp_bin utf8-text A.internal &&
+
+ rm A B &&
+ test_checkout_workers 2 git checkout A B &&
+
+ # Check that A (and only A) is re-encoded during checkout
+ test_cmp_bin utf16-text A &&
+ test_cmp_bin utf8-text B
+ )
+'
+
+test_expect_success 'parallel-checkout with eol conversions' '
+ set_checkout_config 2 0 &&
+ git init eol &&
+ (
+ cd eol &&
+ printf "multi\r\nline\r\ntext" >crlf-text &&
+ printf "multi\nline\ntext" >lf-text &&
+
+ git config core.autocrlf false &&
+ echo "A eol=crlf" >.gitattributes &&
+ cp crlf-text A &&
+ cp lf-text B &&
+ git add A B .gitattributes &&
+ git commit -m eol &&
+
+ # Check that A is stored with LF format
+ git cat-file -p :A >A.internal &&
+ test_cmp_bin lf-text A.internal &&
+
+ rm A B &&
+ test_checkout_workers 2 git checkout A B &&
+
+ # Check that A (and only A) is converted to CRLF during checkout
+ test_cmp_bin crlf-text A &&
+ test_cmp_bin lf-text B
+ )
+'
+
+# Entries that require an external filter are not eligible for parallel
+# checkout. Check that both the parallel-eligible and non-eligible entries are
+# properly writen in a single checkout operation.
+#
+test_expect_success 'parallel-checkout and external filter' '
+ set_checkout_config 2 0 &&
+ git init filter &&
+ (
+ cd filter &&
+ write_script <<-\EOF rot13.sh &&
+ tr \
+ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" \
+ "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM"
+ EOF
+
+ git config filter.rot13.clean "\"$(pwd)/rot13.sh\"" &&
+ git config filter.rot13.smudge "\"$(pwd)/rot13.sh\"" &&
+ git config filter.rot13.required true &&
+
+ echo abcd >original &&
+ echo nopq >rot13 &&
+
+ echo "A filter=rot13" >.gitattributes &&
+ cp original A &&
+ cp original B &&
+ cp original C &&
+ git add A B C .gitattributes &&
+ git commit -m filter &&
+
+ # Check that A (and only A) was cleaned
+ git cat-file -p :A >A.internal &&
+ test_cmp rot13 A.internal &&
+ git cat-file -p :B >B.internal &&
+ test_cmp original B.internal &&
+ git cat-file -p :C >C.internal &&
+ test_cmp original C.internal &&
+
+ rm A B C *.internal &&
+ test_checkout_workers 2 git checkout A B C &&
+
+ # Check that A (and only A) was smudged during checkout
+ test_cmp original A &&
+ test_cmp original B &&
+ test_cmp original C
+ )
+'
+
+# The delayed queue is independent from the parallel queue, and they should be
+# able to work together in the same checkout process.
+#
+test_expect_success 'parallel-checkout and delayed checkout' '
+ test_config_global filter.delay.process \
+ "test-tool rot13-filter --always-delay --log=\"$(pwd)/delayed.log\" clean smudge delay" &&
+ test_config_global filter.delay.required true &&
+
+ echo "abcd" >original &&
+ echo "nopq" >rot13 &&
+
+ git init delayed &&
+ (
+ cd delayed &&
+ echo "*.d filter=delay" >.gitattributes &&
+ cp ../original W.d &&
+ cp ../original X.d &&
+ cp ../original Y &&
+ cp ../original Z &&
+ git add -A &&
+ git commit -m delayed &&
+
+ # Check that *.d files were cleaned
+ git cat-file -p :W.d >W.d.internal &&
+ test_cmp W.d.internal ../rot13 &&
+ git cat-file -p :X.d >X.d.internal &&
+ test_cmp X.d.internal ../rot13 &&
+ git cat-file -p :Y >Y.internal &&
+ test_cmp Y.internal ../original &&
+ git cat-file -p :Z >Z.internal &&
+ test_cmp Z.internal ../original &&
+
+ rm *
+ ) &&
+
+ set_checkout_config 2 0 &&
+ test_checkout_workers 2 git -C delayed checkout -f &&
+ verify_checkout delayed &&
+
+ # Check that the *.d files got to the delay queue and were filtered
+ grep "smudge W.d .* \[DELAYED\]" delayed.log &&
+ grep "smudge X.d .* \[DELAYED\]" delayed.log &&
+ test_cmp delayed/W.d original &&
+ test_cmp delayed/X.d original &&
+
+ # Check that the parallel-eligible entries went to the right queue and
+ # were not filtered
+ ! grep "smudge Y .* \[DELAYED\]" delayed.log &&
+ ! grep "smudge Z .* \[DELAYED\]" delayed.log &&
+ test_cmp delayed/Y original &&
+ test_cmp delayed/Z original
+'
+
+test_done
diff --git a/t/t2100-update-cache-badpath.sh b/t/t2100-update-cache-badpath.sh
new file mode 100755
index 0000000..7915e7b
--- /dev/null
+++ b/t/t2100-update-cache-badpath.sh
@@ -0,0 +1,62 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+test_description='git update-index nonsense-path test.
+
+This test creates the following structure in the cache:
+
+ path0 - a file
+ path1 - a symlink
+ path2/file2 - a file in a directory
+ path3/file3 - a file in a directory
+
+and tries to git update-index --add the following:
+
+ path0/file0 - a file in a directory
+ path1/file1 - a file in a directory
+ path2 - a file
+ path3 - a symlink
+
+All of the attempts should fail.
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+mkdir path2 path3
+date >path0
+if test_have_prereq SYMLINKS
+then
+ ln -s xyzzy path1
+else
+ date > path1
+fi
+date >path2/file2
+date >path3/file3
+
+test_expect_success \
+ 'git update-index --add to add various paths.' \
+ 'git update-index --add -- path0 path1 path2/file2 path3/file3'
+
+rm -fr path?
+
+mkdir path0 path1
+date >path2
+if test_have_prereq SYMLINKS
+then
+ ln -s frotz path3
+else
+ date > path3
+fi
+date >path0/file0
+date >path1/file1
+
+for p in path0/file0 path1/file1 path2 path3
+do
+ test_expect_success \
+ "git update-index to add conflicting path $p should fail." \
+ "test_must_fail git update-index --add -- $p"
+done
+test_done
diff --git a/t/t2101-update-index-reupdate.sh b/t/t2101-update-index-reupdate.sh
new file mode 100755
index 0000000..e3c7acd
--- /dev/null
+++ b/t/t2101-update-index-reupdate.sh
@@ -0,0 +1,92 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Junio C Hamano
+#
+
+test_description='git update-index --again test.
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'update-index --add' '
+ echo hello world >file1 &&
+ echo goodbye people >file2 &&
+ git update-index --add file1 file2 &&
+ git ls-files -s >current &&
+ cat >expected <<-EOF &&
+ 100644 $(git hash-object file1) 0 file1
+ 100644 $(git hash-object file2) 0 file2
+ EOF
+ cmp current expected
+'
+
+test_expect_success 'update-index --again' '
+ rm -f file1 &&
+ echo hello everybody >file2 &&
+ if git update-index --again
+ then
+ echo should have refused to remove file1
+ exit 1
+ else
+ echo happy - failed as expected
+ fi &&
+ git ls-files -s >current &&
+ cmp current expected
+'
+
+test_expect_success 'update-index --remove --again' '
+ git update-index --remove --again &&
+ git ls-files -s >current &&
+ cat >expected <<-EOF &&
+ 100644 $(git hash-object file2) 0 file2
+ EOF
+ cmp current expected
+'
+
+test_expect_success 'first commit' 'git commit -m initial'
+
+test_expect_success 'update-index again' '
+ mkdir -p dir1 &&
+ echo hello world >dir1/file3 &&
+ echo goodbye people >file2 &&
+ git update-index --add file2 dir1/file3 &&
+ echo hello everybody >file2 &&
+ echo happy >dir1/file3 &&
+ git update-index --again &&
+ git ls-files -s >current &&
+ cat >expected <<-EOF &&
+ 100644 $(git hash-object dir1/file3) 0 dir1/file3
+ 100644 $(git hash-object file2) 0 file2
+ EOF
+ cmp current expected
+'
+
+file2=$(git hash-object file2)
+test_expect_success 'update-index --update from subdir' '
+ echo not so happy >file2 &&
+ (cd dir1 &&
+ cat ../file2 >file3 &&
+ git update-index --again
+ ) &&
+ git ls-files -s >current &&
+ cat >expected <<-EOF &&
+ 100644 $(git hash-object dir1/file3) 0 dir1/file3
+ 100644 $file2 0 file2
+ EOF
+ test_cmp expected current
+'
+
+test_expect_success 'update-index --update with pathspec' '
+ echo very happy >file2 &&
+ cat file2 >dir1/file3 &&
+ git update-index --again dir1/ &&
+ git ls-files -s >current &&
+ cat >expected <<-EOF &&
+ 100644 $(git hash-object dir1/file3) 0 dir1/file3
+ 100644 $file2 0 file2
+ EOF
+ cmp current expected
+'
+
+test_done
diff --git a/t/t2102-update-index-symlinks.sh b/t/t2102-update-index-symlinks.sh
new file mode 100755
index 0000000..c49cdfb
--- /dev/null
+++ b/t/t2102-update-index-symlinks.sh
@@ -0,0 +1,32 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Johannes Sixt
+#
+
+test_description='git update-index on filesystem w/o symlinks test.
+
+This tests that git update-index keeps the symbolic link property
+even if a plain file is in the working tree if core.symlinks is false.'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success \
+'preparation' '
+git config core.symlinks false &&
+l=$(printf file | git hash-object -t blob -w --stdin) &&
+echo "120000 $l symlink" | git update-index --index-info'
+
+test_expect_success \
+'modify the symbolic link' '
+printf new-file > symlink &&
+git update-index symlink'
+
+test_expect_success \
+'the index entry must still be a symbolic link' '
+case "$(git ls-files --stage --cached symlink)" in
+120000" "*symlink) echo pass;;
+*) echo fail; git ls-files --stage --cached symlink; false;;
+esac'
+
+test_done
diff --git a/t/t2103-update-index-ignore-missing.sh b/t/t2103-update-index-ignore-missing.sh
new file mode 100755
index 0000000..e9451cd
--- /dev/null
+++ b/t/t2103-update-index-ignore-missing.sh
@@ -0,0 +1,90 @@
+#!/bin/sh
+
+test_description='update-index with options'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success basics '
+ >one &&
+ >two &&
+ >three &&
+
+ # need --add when adding
+ test_must_fail git update-index one &&
+ test -z "$(git ls-files)" &&
+ git update-index --add one &&
+ test zone = "z$(git ls-files)" &&
+
+ # update-index is atomic
+ echo 1 >one &&
+ test_must_fail git update-index one two &&
+ echo "M one" >expect &&
+ git diff-files --name-status >actual &&
+ test_cmp expect actual &&
+
+ git update-index --add one two three &&
+ test_write_lines one three two >expect &&
+ git ls-files >actual &&
+ test_cmp expect actual &&
+
+ test_tick &&
+ (
+ test_create_repo xyzzy &&
+ cd xyzzy &&
+ >file &&
+ git add file &&
+ git commit -m "sub initial"
+ ) &&
+ git add xyzzy &&
+
+ test_tick &&
+ git commit -m initial &&
+ git tag initial
+'
+
+test_expect_success '--ignore-missing --refresh' '
+ git reset --hard initial &&
+ echo 2 >one &&
+ test_must_fail git update-index --refresh &&
+ echo 1 >one &&
+ git update-index --refresh &&
+ rm -f two &&
+ test_must_fail git update-index --refresh &&
+ git update-index --ignore-missing --refresh
+
+'
+
+test_expect_success '--unmerged --refresh' '
+ git reset --hard initial &&
+ info=$(git ls-files -s one | sed -e "s/ 0 / 1 /") &&
+ git rm --cached one &&
+ echo "$info" | git update-index --index-info &&
+ test_must_fail git update-index --refresh &&
+ git update-index --unmerged --refresh &&
+ echo 2 >two &&
+ test_must_fail git update-index --unmerged --refresh >actual &&
+ grep two actual &&
+ ! grep one actual &&
+ ! grep three actual
+'
+
+test_expect_success '--ignore-submodules --refresh (1)' '
+ git reset --hard initial &&
+ rm -f two &&
+ test_must_fail git update-index --ignore-submodules --refresh
+'
+
+test_expect_success '--ignore-submodules --refresh (2)' '
+ git reset --hard initial &&
+ test_tick &&
+ (
+ cd xyzzy &&
+ git commit -m "sub second" --allow-empty
+ ) &&
+ test_must_fail git update-index --refresh &&
+ test_must_fail git update-index --ignore-missing --refresh &&
+ git update-index --ignore-submodules --refresh
+'
+
+test_done
diff --git a/t/t2104-update-index-skip-worktree.sh b/t/t2104-update-index-skip-worktree.sh
new file mode 100755
index 0000000..0bab134
--- /dev/null
+++ b/t/t2104-update-index-skip-worktree.sh
@@ -0,0 +1,67 @@
+#!/bin/sh
+#
+# Copyright (c) 2008 Nguyá»…n Thái Ngá»c Duy
+#
+
+test_description='skip-worktree bit test'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+sane_unset GIT_TEST_SPLIT_INDEX
+
+test_set_index_version () {
+ GIT_INDEX_VERSION="$1"
+ export GIT_INDEX_VERSION
+}
+
+test_set_index_version 3
+
+cat >expect.full <<EOF
+H 1
+H 2
+H sub/1
+H sub/2
+EOF
+
+cat >expect.skip <<EOF
+S 1
+H 2
+S sub/1
+H sub/2
+EOF
+
+test_expect_success 'setup' '
+ mkdir sub &&
+ touch ./1 ./2 sub/1 sub/2 &&
+ git add 1 2 sub/1 sub/2 &&
+ git ls-files -t | test_cmp expect.full -
+'
+
+test_expect_success 'index is at version 2' '
+ test "$(git update-index --show-index-version)" = 2
+'
+
+test_expect_success 'update-index --skip-worktree' '
+ git update-index --skip-worktree 1 sub/1 &&
+ git ls-files -t | test_cmp expect.skip -
+'
+
+test_expect_success 'index is at version 3 after having some skip-worktree entries' '
+ test "$(git update-index --show-index-version)" = 3
+'
+
+test_expect_success 'ls-files -t' '
+ git ls-files -t | test_cmp expect.skip -
+'
+
+test_expect_success 'update-index --no-skip-worktree' '
+ git update-index --no-skip-worktree 1 sub/1 &&
+ git ls-files -t | test_cmp expect.full -
+'
+
+test_expect_success 'index version is back to 2 when there is no skip-worktree entry' '
+ test "$(git update-index --show-index-version)" = 2
+'
+
+test_done
diff --git a/t/t2105-update-index-gitfile.sh b/t/t2105-update-index-gitfile.sh
new file mode 100755
index 0000000..963ebe7
--- /dev/null
+++ b/t/t2105-update-index-gitfile.sh
@@ -0,0 +1,39 @@
+#!/bin/sh
+#
+# Copyright (c) 2010 Brad King
+#
+
+test_description='git update-index for gitlink to .git file.
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'submodule with absolute .git file' '
+ mkdir sub1 &&
+ (cd sub1 &&
+ git init &&
+ REAL="$(pwd)/.real" &&
+ mv .git "$REAL" &&
+ echo "gitdir: $REAL" >.git &&
+ test_commit first)
+'
+
+test_expect_success 'add gitlink to absolute .git file' '
+ git update-index --add -- sub1
+'
+
+test_expect_success 'submodule with relative .git file' '
+ mkdir sub2 &&
+ (cd sub2 &&
+ git init &&
+ mv .git .real &&
+ echo "gitdir: .real" >.git &&
+ test_commit first)
+'
+
+test_expect_success 'add gitlink to relative .git file' '
+ git update-index --add -- sub2
+'
+
+test_done
diff --git a/t/t2106-update-index-assume-unchanged.sh b/t/t2106-update-index-assume-unchanged.sh
new file mode 100755
index 0000000..95c004d
--- /dev/null
+++ b/t/t2106-update-index-assume-unchanged.sh
@@ -0,0 +1,28 @@
+#!/bin/sh
+
+test_description='git update-index --assume-unchanged test.
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ : >file &&
+ git add file &&
+ git commit -m initial &&
+ git branch other &&
+ echo upstream >file &&
+ git add file &&
+ git commit -m upstream
+'
+
+test_expect_success 'do not switch branches with dirty file' '
+ git reset --hard &&
+ git checkout other &&
+ echo dirt >file &&
+ git update-index --assume-unchanged file &&
+ test_must_fail git checkout - 2>err &&
+ test_grep overwritten err
+'
+
+test_done
diff --git a/t/t2107-update-index-basic.sh b/t/t2107-update-index-basic.sh
new file mode 100755
index 0000000..cc72ead
--- /dev/null
+++ b/t/t2107-update-index-basic.sh
@@ -0,0 +1,145 @@
+#!/bin/sh
+
+test_description='basic update-index tests
+
+Tests for command-line parsing and basic operation.
+'
+
+. ./test-lib.sh
+
+test_expect_success 'update-index --nonsense fails' '
+ test_must_fail git update-index --nonsense 2>msg &&
+ test -s msg
+'
+
+test_expect_success 'update-index --nonsense dumps usage' '
+ test_expect_code 129 git update-index --nonsense 2>err &&
+ test_grep "[Uu]sage: git update-index" err
+'
+
+test_expect_success 'update-index -h with corrupt index' '
+ mkdir broken &&
+ (
+ cd broken &&
+ git init &&
+ >.git/index &&
+ test_expect_code 129 git update-index -h >usage 2>&1
+ ) &&
+ test_grep "[Uu]sage: git update-index" broken/usage
+'
+
+test_expect_success '--cacheinfo complains of missing arguments' '
+ test_must_fail git update-index --cacheinfo
+'
+
+test_expect_success '--cacheinfo does not accept blob null sha1' '
+ echo content >file &&
+ git add file &&
+ git rev-parse :file >expect &&
+ test_must_fail git update-index --verbose --cacheinfo 100644 $ZERO_OID file >out &&
+ git rev-parse :file >actual &&
+ test_cmp expect actual &&
+
+ cat >expect <<-\EOF &&
+ add '\''file'\''
+ EOF
+ test_cmp expect out
+'
+
+test_expect_success '--cacheinfo does not accept gitlink null sha1' '
+ git init submodule &&
+ (cd submodule && test_commit foo) &&
+ git add submodule &&
+ git rev-parse :submodule >expect &&
+ test_must_fail git update-index --cacheinfo 160000 $ZERO_OID submodule &&
+ git rev-parse :submodule >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--cacheinfo mode,sha1,path (new syntax)' '
+ echo content >file &&
+ git hash-object -w --stdin <file >expect &&
+
+ git update-index --add --cacheinfo 100644 "$(cat expect)" file &&
+ git rev-parse :file >actual &&
+ test_cmp expect actual &&
+
+ git update-index --add --verbose --cacheinfo "100644,$(cat expect),elif" >out &&
+ git rev-parse :elif >actual &&
+ test_cmp expect actual &&
+
+ cat >expect <<-\EOF &&
+ add '\''elif'\''
+ EOF
+ test_cmp expect out
+'
+
+test_expect_success '.lock files cleaned up' '
+ mkdir cleanup &&
+ (
+ cd cleanup &&
+ mkdir worktree &&
+ git init repo &&
+ cd repo &&
+ git config core.worktree ../../worktree &&
+ # --refresh triggers late setup_work_tree,
+ # the_index.cache_changed is zero, rollback_lock_file fails
+ git update-index --refresh --verbose >out &&
+ test_must_be_empty out &&
+ ! test -f .git/index.lock
+ )
+'
+
+test_expect_success '--chmod=+x and chmod=-x in the same argument list' '
+ >A &&
+ >B &&
+ git add A B &&
+ git update-index --verbose --chmod=+x A --chmod=-x B >out &&
+ cat >expect <<-\EOF &&
+ add '\''A'\''
+ chmod +x '\''A'\''
+ add '\''B'\''
+ chmod -x '\''B'\''
+ EOF
+ test_cmp expect out &&
+
+ cat >expect <<-EOF &&
+ 100755 $EMPTY_BLOB 0 A
+ 100644 $EMPTY_BLOB 0 B
+ EOF
+ git ls-files --stage A B >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--index-version' '
+ git commit --allow-empty -m snap &&
+ git reset --hard &&
+ git rm -f -r --cached . &&
+
+ # The default index version is 2 --- update this test
+ # when you change it in the code
+ git update-index --show-index-version >actual &&
+ echo 2 >expect &&
+ test_cmp expect actual &&
+
+ # The next test wants us to be using version 2
+ git update-index --index-version 2 &&
+
+ git update-index --index-version 4 --verbose >actual &&
+ echo "index-version: was 2, set to 4" >expect &&
+ test_cmp expect actual &&
+
+ git update-index --index-version 4 --verbose >actual &&
+ echo "index-version: was 4, set to 4" >expect &&
+ test_cmp expect actual &&
+
+ git update-index --index-version 2 --verbose >actual &&
+ echo "index-version: was 4, set to 2" >expect &&
+ test_cmp expect actual &&
+
+ # non-verbose should be silent
+ git update-index --index-version 4 >actual &&
+ test_must_be_empty actual
+'
+
+test_done
diff --git a/t/t2108-update-index-refresh-racy.sh b/t/t2108-update-index-refresh-racy.sh
new file mode 100755
index 0000000..bc5f288
--- /dev/null
+++ b/t/t2108-update-index-refresh-racy.sh
@@ -0,0 +1,64 @@
+#!/bin/sh
+
+test_description='update-index refresh tests related to racy timestamps'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+reset_files () {
+ echo content >file &&
+ echo content >other &&
+ test_set_magic_mtime file &&
+ test_set_magic_mtime other
+}
+
+update_assert_changed () {
+ test_set_magic_mtime .git/index &&
+ test_might_fail git update-index "$1" &&
+ ! test_is_magic_mtime .git/index
+}
+
+test_expect_success 'setup' '
+ reset_files &&
+ # we are calling reset_files() a couple of times during tests;
+ # test-tool chmtime does not change the ctime; to not weaken
+ # or even break our tests, disable ctime-checks entirely
+ git config core.trustctime false &&
+ git add file other &&
+ git commit -m "initial import"
+'
+
+test_expect_success '--refresh has no racy timestamps to fix' '
+ reset_files &&
+ # set the index time far enough to the future;
+ # it must be at least 3 seconds for VFAT
+ test_set_magic_mtime .git/index +60 &&
+ git update-index --refresh &&
+ test_is_magic_mtime .git/index +60
+'
+
+test_expect_success '--refresh should fix racy timestamp' '
+ reset_files &&
+ update_assert_changed --refresh
+'
+
+test_expect_success '--really-refresh should fix racy timestamp' '
+ reset_files &&
+ update_assert_changed --really-refresh
+'
+
+test_expect_success '--refresh should fix racy timestamp if other file needs update' '
+ reset_files &&
+ echo content2 >other &&
+ test_set_magic_mtime other &&
+ update_assert_changed --refresh
+'
+
+test_expect_success '--refresh should fix racy timestamp if racy file needs update' '
+ reset_files &&
+ echo content2 >file &&
+ test_set_magic_mtime file &&
+ update_assert_changed --refresh
+'
+
+test_done
diff --git a/t/t2200-add-update.sh b/t/t2200-add-update.sh
new file mode 100755
index 0000000..c01492f
--- /dev/null
+++ b/t/t2200-add-update.sh
@@ -0,0 +1,211 @@
+#!/bin/sh
+
+test_description='git add -u
+
+This test creates a working tree state with three files:
+
+ top (previously committed, modified)
+ dir/sub (previously committed, modified)
+ dir/other (untracked)
+
+and issues a git add -u with path limiting on "dir" to add
+only the updates to dir/sub.
+
+Also tested are "git add -u" without limiting, and "git add -u"
+without contents changes, and other conditions'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ echo initial >check &&
+ echo initial >top &&
+ echo initial >foo &&
+ mkdir dir1 dir2 &&
+ echo initial >dir1/sub1 &&
+ echo initial >dir1/sub2 &&
+ echo initial >dir2/sub3 &&
+ git add check dir1 dir2 top foo &&
+ test_tick &&
+ git commit -m initial &&
+
+ echo changed >check &&
+ echo changed >top &&
+ echo changed >dir2/sub3 &&
+ rm -f dir1/sub1 &&
+ echo other >dir2/other
+'
+
+test_expect_success update '
+ git add -u dir1 dir2
+'
+
+test_expect_success 'update noticed a removal' '
+ git ls-files dir1/sub1 >out &&
+ test_must_be_empty out
+'
+
+test_expect_success 'update touched correct path' '
+ git diff-files --name-status dir2/sub3 >out &&
+ test_must_be_empty out
+'
+
+test_expect_success 'update did not touch other tracked files' '
+ echo "M check" >expect &&
+ git diff-files --name-status check >actual &&
+ test_cmp expect actual &&
+
+ echo "M top" >expect &&
+ git diff-files --name-status top >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'update did not touch untracked files' '
+ git ls-files dir2/other >out &&
+ test_must_be_empty out
+'
+
+test_expect_success 'cache tree has not been corrupted' '
+
+ git ls-files -s |
+ sed -e "s/ 0 / /" >expect &&
+ git ls-tree -r $(git write-tree) |
+ sed -e "s/ blob / /" >current &&
+ test_cmp expect current
+
+'
+
+test_expect_success 'update from a subdirectory' '
+ (
+ cd dir1 &&
+ echo more >sub2 &&
+ git add -u sub2
+ )
+'
+
+test_expect_success 'change gets noticed' '
+ git diff-files --name-status dir1 >out &&
+ test_must_be_empty out
+'
+
+test_expect_success 'non-qualified update in subdir updates from the root' '
+ (
+ cd dir1 &&
+ echo even more >>sub2 &&
+ git --literal-pathspecs add -u &&
+ echo even more >>sub2 &&
+ git add -u
+ ) &&
+ git diff-files --name-only >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'replace a file with a symlink' '
+
+ rm foo &&
+ test_ln_s_add top foo
+
+'
+
+test_expect_success 'add everything changed' '
+
+ git add -u &&
+ git diff-files >out &&
+ test_must_be_empty out
+
+'
+
+test_expect_success 'touch and then add -u' '
+
+ touch check &&
+ git add -u &&
+ git diff-files >out &&
+ test_must_be_empty out
+
+'
+
+test_expect_success 'touch and then add explicitly' '
+
+ touch check &&
+ git add check &&
+ git diff-files >out &&
+ test_must_be_empty out
+
+'
+
+test_expect_success 'add -n -u should not add but just report' '
+
+ (
+ echo "add '\''check'\''" &&
+ echo "remove '\''top'\''"
+ ) >expect &&
+ before=$(git ls-files -s check top) &&
+ git count-objects -v >objects_before &&
+ echo changed >>check &&
+ rm -f top &&
+ git add -n -u >actual &&
+ after=$(git ls-files -s check top) &&
+ git count-objects -v >objects_after &&
+
+ test "$before" = "$after" &&
+ test_cmp objects_before objects_after &&
+ test_cmp expect actual
+
+'
+
+test_expect_success 'add -u resolves unmerged paths' '
+ git reset --hard &&
+ one=$(echo 1 | git hash-object -w --stdin) &&
+ two=$(echo 2 | git hash-object -w --stdin) &&
+ three=$(echo 3 | git hash-object -w --stdin) &&
+ {
+ for path in path1 path2
+ do
+ echo "100644 $one 1 $path" &&
+ echo "100644 $two 2 $path" &&
+ echo "100644 $three 3 $path" || return 1
+ done &&
+ echo "100644 $one 1 path3" &&
+ echo "100644 $one 1 path4" &&
+ echo "100644 $one 3 path5" &&
+ echo "100644 $one 3 path6"
+ } |
+ git update-index --index-info &&
+ echo 3 >path1 &&
+ echo 2 >path3 &&
+ echo 2 >path5 &&
+
+ # Fail to explicitly resolve removed paths with "git add"
+ test_must_fail git add --no-all path4 &&
+ test_must_fail git add --no-all path6 &&
+
+ # "add -u" should notice removals no matter what stages
+ # the index entries are in.
+ git add -u &&
+ git ls-files -s path1 path2 path3 path4 path5 path6 >actual &&
+ {
+ echo "100644 $three 0 path1" &&
+ echo "100644 $two 0 path3" &&
+ echo "100644 $two 0 path5"
+ } >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success '"add -u non-existent" should fail' '
+ test_must_fail git add -u non-existent &&
+ git ls-files >actual &&
+ ! grep "non-existent" actual
+'
+
+test_expect_success '"commit -a" implies "add -u" if index becomes empty' '
+ git rm -rf \* &&
+ git commit -m clean-slate &&
+ test_commit file1 &&
+ rm file1.t &&
+ test_tick &&
+ git commit -a -m remove &&
+ git ls-tree HEAD: >out &&
+ test_must_be_empty out
+'
+
+test_done
diff --git a/t/t2201-add-update-typechange.sh b/t/t2201-add-update-typechange.sh
new file mode 100755
index 0000000..dba62d6
--- /dev/null
+++ b/t/t2201-add-update-typechange.sh
@@ -0,0 +1,149 @@
+#!/bin/sh
+
+test_description='more git add -u'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ >xyzzy &&
+ _empty=$(git hash-object --stdin <xyzzy) &&
+ >yomin &&
+ >caskly &&
+ if test_have_prereq SYMLINKS; then
+ ln -s frotz nitfol &&
+ T_letter=T
+ else
+ printf %s frotz > nitfol &&
+ T_letter=M
+ fi &&
+ mkdir rezrov &&
+ >rezrov/bozbar &&
+ git add caskly xyzzy yomin nitfol rezrov/bozbar &&
+
+ test_tick &&
+ git commit -m initial
+
+'
+
+test_expect_success modify '
+ rm -f xyzzy yomin nitfol caskly &&
+ # caskly disappears (not a submodule)
+ mkdir caskly &&
+ # nitfol changes from symlink to regular
+ >nitfol &&
+ # rezrov/bozbar disappears
+ rm -fr rezrov &&
+ if test_have_prereq SYMLINKS; then
+ ln -s xyzzy rezrov
+ else
+ printf %s xyzzy > rezrov
+ fi &&
+ # xyzzy disappears (not a submodule)
+ mkdir xyzzy &&
+ echo gnusto >xyzzy/bozbar &&
+ # yomin gets replaced with a submodule
+ mkdir yomin &&
+ >yomin/yomin &&
+ (
+ cd yomin &&
+ git init &&
+ git add yomin &&
+ git commit -m "sub initial"
+ ) &&
+ yomin=$(GIT_DIR=yomin/.git git rev-parse HEAD) &&
+ # yonk is added and then turned into a submodule
+ # this should appear as T in diff-files and as A in diff-index
+ >yonk &&
+ git add yonk &&
+ rm -f yonk &&
+ mkdir yonk &&
+ >yonk/yonk &&
+ (
+ cd yonk &&
+ git init &&
+ git add yonk &&
+ git commit -m "sub initial"
+ ) &&
+ yonk=$(GIT_DIR=yonk/.git git rev-parse HEAD) &&
+ # zifmia is added and then removed
+ # this should appear in diff-files but not in diff-index.
+ >zifmia &&
+ git add zifmia &&
+ rm -f zifmia &&
+ mkdir zifmia &&
+ {
+ git ls-tree -r HEAD |
+ sed -e "s/^/:/" -e "
+ / caskly/{
+ s/ caskly/ $ZERO_OID D&/
+ s/blob/000000/
+ }
+ / nitfol/{
+ s/ nitfol/ $ZERO_OID $T_letter&/
+ s/blob/100644/
+ }
+ / rezrov.bozbar/{
+ s/ rezrov.bozbar/ $ZERO_OID D&/
+ s/blob/000000/
+ }
+ / xyzzy/{
+ s/ xyzzy/ $ZERO_OID D&/
+ s/blob/000000/
+ }
+ / yomin/{
+ s/ yomin/ $ZERO_OID T&/
+ s/blob/160000/
+ }
+ "
+ } >expect &&
+ {
+ cat expect &&
+ echo ":100644 160000 $_empty $ZERO_OID T yonk" &&
+ echo ":100644 000000 $_empty $ZERO_OID D zifmia"
+ } >expect-files &&
+ {
+ cat expect &&
+ echo ":000000 160000 $ZERO_OID $ZERO_OID A yonk"
+ } >expect-index &&
+ {
+ echo "100644 $_empty 0 nitfol" &&
+ echo "160000 $yomin 0 yomin" &&
+ echo "160000 $yonk 0 yonk"
+ } >expect-final
+'
+
+test_expect_success diff-files '
+ git diff-files --raw >actual &&
+ test_cmp expect-files actual
+'
+
+test_expect_success diff-index '
+ git diff-index --raw HEAD -- >actual &&
+ test_cmp expect-index actual
+'
+
+test_expect_success 'add -u' '
+ rm -f ".git/saved-index" &&
+ cp -p ".git/index" ".git/saved-index" &&
+ git add -u &&
+ git ls-files -s >actual &&
+ test_cmp expect-final actual
+'
+
+test_expect_success 'commit -a' '
+ if test -f ".git/saved-index"
+ then
+ rm -f ".git/index" &&
+ mv ".git/saved-index" ".git/index"
+ fi &&
+ git commit -m "second" -a &&
+ git ls-files -s >actual &&
+ test_cmp expect-final actual &&
+ rm -f .git/index &&
+ git read-tree HEAD &&
+ git ls-files -s >actual &&
+ test_cmp expect-final actual
+'
+
+test_done
diff --git a/t/t2202-add-addremove.sh b/t/t2202-add-addremove.sh
new file mode 100755
index 0000000..24c60bf
--- /dev/null
+++ b/t/t2202-add-addremove.sh
@@ -0,0 +1,55 @@
+#!/bin/sh
+
+test_description='git add --all'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ (
+ echo .gitignore &&
+ echo will-remove
+ ) >expect &&
+ (
+ echo actual &&
+ echo expect &&
+ echo ignored
+ ) >.gitignore &&
+ git --literal-pathspecs add --all &&
+ >will-remove &&
+ git add --all &&
+ test_tick &&
+ git commit -m initial &&
+ git ls-files >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git add --all' '
+ (
+ echo .gitignore &&
+ echo not-ignored &&
+ echo "M .gitignore" &&
+ echo "A not-ignored" &&
+ echo "D will-remove"
+ ) >expect &&
+ >ignored &&
+ >not-ignored &&
+ echo modification >>.gitignore &&
+ rm -f will-remove &&
+ git add --all &&
+ git update-index --refresh &&
+ git ls-files >actual &&
+ git diff-index --name-status --cached HEAD >>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Just "git add" is a no-op' '
+ git reset --hard &&
+ echo >will-remove &&
+ >will-not-be-added &&
+ git add &&
+ git diff-index --name-status --cached HEAD >actual &&
+ test_must_be_empty actual
+'
+
+test_done
diff --git a/t/t2203-add-intent.sh b/t/t2203-add-intent.sh
new file mode 100755
index 0000000..8fa44a9
--- /dev/null
+++ b/t/t2203-add-intent.sh
@@ -0,0 +1,315 @@
+#!/bin/sh
+
+test_description='Intent to add'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'intent to add' '
+ test_commit 1 &&
+ git rm 1.t &&
+ echo hello >1.t &&
+ echo hello >file &&
+ echo hello >elif &&
+ git add -N file &&
+ git add elif &&
+ git add -N 1.t
+'
+
+test_expect_success 'git status' '
+ git status --porcelain | grep -v actual >actual &&
+ cat >expect <<-\EOF &&
+ DA 1.t
+ A elif
+ A file
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'git status with porcelain v2' '
+ git status --porcelain=v2 | grep -v "^?" >actual &&
+ nam1=$(echo 1 | git hash-object --stdin) &&
+ nam2=$(git hash-object elif) &&
+ cat >expect <<-EOF &&
+ 1 DA N... 100644 000000 100644 $nam1 $ZERO_OID 1.t
+ 1 A. N... 000000 100644 100644 $ZERO_OID $nam2 elif
+ 1 .A N... 000000 000000 100644 $ZERO_OID $ZERO_OID file
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'check result of "add -N"' '
+ git ls-files -s file >actual &&
+ empty=$(git hash-object --stdin </dev/null) &&
+ echo "100644 $empty 0 file" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'intent to add is just an ordinary empty blob' '
+ git add -u &&
+ git ls-files -s file >actual &&
+ git ls-files -s elif | sed -e "s/elif/file/" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'intent to add does not clobber existing paths' '
+ git add -N file elif &&
+ empty=$(git hash-object --stdin </dev/null) &&
+ git ls-files -s >actual &&
+ ! grep "$empty" actual
+'
+
+test_expect_success 'i-t-a entry is simply ignored' '
+ test_tick &&
+ git commit -a -m initial &&
+ git reset --hard &&
+
+ echo xyzzy >rezrov &&
+ echo frotz >nitfol &&
+ git add rezrov &&
+ git add -N nitfol &&
+ git commit -m second &&
+ test $(git ls-tree HEAD -- nitfol | wc -l) = 0 &&
+ test $(git diff --name-only HEAD -- nitfol | wc -l) = 1 &&
+ test $(git diff --name-only -- nitfol | wc -l) = 1
+'
+
+test_expect_success 'can commit with an unrelated i-t-a entry in index' '
+ git reset --hard &&
+ echo bozbar >rezrov &&
+ echo frotz >nitfol &&
+ git add rezrov &&
+ git add -N nitfol &&
+ git commit -m partial rezrov
+'
+
+test_expect_success 'can "commit -a" with an i-t-a entry' '
+ git reset --hard &&
+ : >nitfol &&
+ git add -N nitfol &&
+ git commit -a -m all
+'
+
+test_expect_success 'cache-tree invalidates i-t-a paths' '
+ git reset --hard &&
+ mkdir dir &&
+ : >dir/foo &&
+ git add dir/foo &&
+ git commit -m foo &&
+
+ : >dir/bar &&
+ git add -N dir/bar &&
+ git diff --name-only >actual &&
+ echo dir/bar >expect &&
+ test_cmp expect actual &&
+
+ git write-tree >/dev/null &&
+
+ git diff --name-only >actual &&
+ echo dir/bar >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'cache-tree does not ignore dir that has i-t-a entries' '
+ git init ita-in-dir &&
+ (
+ cd ita-in-dir &&
+ mkdir 2 &&
+ for f in 1 2/1 2/2 3
+ do
+ echo "$f" >"$f" || return 1
+ done &&
+ git add 1 2/2 3 &&
+ git add -N 2/1 &&
+ git commit -m committed &&
+ git ls-tree -r HEAD >actual &&
+ grep 2/2 actual
+ )
+'
+
+test_expect_success 'cache-tree does skip dir that becomes empty' '
+ rm -fr ita-in-dir &&
+ git init ita-in-dir &&
+ (
+ cd ita-in-dir &&
+ mkdir -p 1/2/3 &&
+ echo 4 >1/2/3/4 &&
+ git add -N 1/2/3/4 &&
+ git write-tree >actual &&
+ echo $EMPTY_TREE >expected &&
+ test_cmp expected actual
+ )
+'
+
+test_expect_success 'commit: ita entries ignored in empty initial commit check' '
+ git init empty-initial-commit &&
+ (
+ cd empty-initial-commit &&
+ : >one &&
+ git add -N one &&
+ test_must_fail git commit -m nothing-new-here
+ )
+'
+
+test_expect_success 'commit: ita entries ignored in empty commit check' '
+ git init empty-subsequent-commit &&
+ (
+ cd empty-subsequent-commit &&
+ test_commit one &&
+ : >two &&
+ git add -N two &&
+ test_must_fail git commit -m nothing-new-here
+ )
+'
+
+test_expect_success 'rename detection finds the right names' '
+ git init rename-detection &&
+ (
+ cd rename-detection &&
+ echo contents >first &&
+ git add first &&
+ git commit -m first &&
+ mv first third &&
+ git add -N third &&
+
+ git status | grep -v "^?" >actual.1 &&
+ test_grep "renamed: *first -> third" actual.1 &&
+
+ git status --porcelain | grep -v "^?" >actual.2 &&
+ cat >expected.2 <<-\EOF &&
+ R first -> third
+ EOF
+ test_cmp expected.2 actual.2 &&
+
+ hash=$(git hash-object third) &&
+ git status --porcelain=v2 | grep -v "^?" >actual.3 &&
+ cat >expected.3 <<-EOF &&
+ 2 .R N... 100644 100644 100644 $hash $hash R100 third first
+ EOF
+ test_cmp expected.3 actual.3 &&
+
+ git diff --stat >actual.4 &&
+ cat >expected.4 <<-EOF &&
+ first => third | 0
+ 1 file changed, 0 insertions(+), 0 deletions(-)
+ EOF
+ test_cmp expected.4 actual.4 &&
+
+ git diff --cached --stat >actual.5 &&
+ test_must_be_empty actual.5
+
+ )
+'
+
+test_expect_success 'double rename detection in status' '
+ git init rename-detection-2 &&
+ (
+ cd rename-detection-2 &&
+ echo contents >first &&
+ git add first &&
+ git commit -m first &&
+ git mv first second &&
+ mv second third &&
+ git add -N third &&
+
+ git status | grep -v "^?" >actual.1 &&
+ test_grep "renamed: *first -> second" actual.1 &&
+ test_grep "renamed: *second -> third" actual.1 &&
+
+ git status --porcelain | grep -v "^?" >actual.2 &&
+ cat >expected.2 <<-\EOF &&
+ R first -> second
+ R second -> third
+ EOF
+ test_cmp expected.2 actual.2 &&
+
+ hash=$(git hash-object third) &&
+ git status --porcelain=v2 | grep -v "^?" >actual.3 &&
+ cat >expected.3 <<-EOF &&
+ 2 R. N... 100644 100644 100644 $hash $hash R100 second first
+ 2 .R N... 100644 100644 100644 $hash $hash R100 third second
+ EOF
+ test_cmp expected.3 actual.3
+ )
+'
+
+test_expect_success 'i-t-a files shown as new for "diff", "diff-files"; not-new for "diff --cached"' '
+ git reset --hard &&
+ : >empty &&
+ content="foo" &&
+ echo "$content" >not-empty &&
+
+ hash_e=$(git hash-object empty) &&
+ hash_n=$(git hash-object not-empty) &&
+
+ cat >expect.diff_p <<-EOF &&
+ diff --git a/empty b/empty
+ new file mode 100644
+ index 0000000..$(git rev-parse --short $hash_e)
+ diff --git a/not-empty b/not-empty
+ new file mode 100644
+ index 0000000..$(git rev-parse --short $hash_n)
+ --- /dev/null
+ +++ b/not-empty
+ @@ -0,0 +1 @@
+ +$content
+ EOF
+ cat >expect.diff_s <<-EOF &&
+ create mode 100644 empty
+ create mode 100644 not-empty
+ EOF
+ cat >expect.diff_a <<-EOF &&
+ :000000 100644 0000000 0000000 A$(printf "\t")empty
+ :000000 100644 0000000 0000000 A$(printf "\t")not-empty
+ EOF
+
+ git add -N empty not-empty &&
+
+ git diff >actual &&
+ test_cmp expect.diff_p actual &&
+
+ git diff --summary >actual &&
+ test_cmp expect.diff_s actual &&
+
+ git diff-files -p >actual &&
+ test_cmp expect.diff_p actual &&
+
+ git diff-files --abbrev >actual &&
+ test_cmp expect.diff_a actual &&
+
+ git diff --cached >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success '"diff HEAD" includes ita as new files' '
+ git reset --hard &&
+ echo new >new-ita &&
+ oid=$(git hash-object new-ita) &&
+ oid=$(git rev-parse --short $oid) &&
+ git add -N new-ita &&
+ git diff HEAD >actual &&
+ cat >expected <<-EOF &&
+ diff --git a/new-ita b/new-ita
+ new file mode 100644
+ index 0000000..$oid
+ --- /dev/null
+ +++ b/new-ita
+ @@ -0,0 +1 @@
+ +new
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'apply --intent-to-add' '
+ git reset --hard &&
+ echo new >new-ita &&
+ git add -N new-ita &&
+ git diff >expected &&
+ grep "new file" expected &&
+ git reset --hard &&
+ git apply --intent-to-add expected &&
+ git diff >actual &&
+ test_cmp expected actual
+'
+
+test_done
diff --git a/t/t2204-add-ignored.sh b/t/t2204-add-ignored.sh
new file mode 100755
index 0000000..b7cf1e4
--- /dev/null
+++ b/t/t2204-add-ignored.sh
@@ -0,0 +1,93 @@
+#!/bin/sh
+
+test_description='giving ignored paths to git add'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ mkdir sub dir dir/sub &&
+ echo sub >.gitignore &&
+ echo ign >>.gitignore &&
+ for p in . sub dir dir/sub
+ do
+ >"$p/ign" &&
+ >"$p/file" || exit 1
+ done
+'
+
+for i in file dir/file dir 'd*'
+do
+ test_expect_success "no complaints for unignored $i" '
+ rm -f .git/index &&
+ git add "$i" &&
+ git ls-files "$i" >out &&
+ test -s out
+ '
+done
+
+for i in ign dir/ign dir/sub dir/sub/*ign sub/file sub sub/*
+do
+ test_expect_success "complaints for ignored $i" '
+ rm -f .git/index &&
+ test_must_fail git add "$i" 2>err &&
+ git ls-files "$i" >out &&
+ test_must_be_empty out
+ '
+
+ test_expect_success "complaints for ignored $i output" '
+ test_grep -e "Use -f if" err
+ '
+
+ test_expect_success "complaints for ignored $i with unignored file" '
+ rm -f .git/index &&
+ test_must_fail git add "$i" file 2>err &&
+ git ls-files "$i" >out &&
+ test_must_be_empty out
+ '
+ test_expect_success "complaints for ignored $i with unignored file output" '
+ test_grep -e "Use -f if" err
+ '
+done
+
+for i in sub sub/*
+do
+ test_expect_success "complaints for ignored $i in dir" '
+ rm -f .git/index &&
+ (
+ cd dir &&
+ test_must_fail git add "$i" 2>err &&
+ git ls-files "$i" >out &&
+ test_must_be_empty out
+ )
+ '
+
+ test_expect_success "complaints for ignored $i in dir output" '
+ (
+ cd dir &&
+ test_grep -e "Use -f if" err
+ )
+ '
+done
+
+for i in ign file
+do
+ test_expect_success "complaints for ignored $i in sub" '
+ rm -f .git/index &&
+ (
+ cd sub &&
+ test_must_fail git add "$i" 2>err &&
+ git ls-files "$i" >out &&
+ test_must_be_empty out
+ )
+ '
+
+ test_expect_success "complaints for ignored $i in sub output" '
+ (
+ cd sub &&
+ test_grep -e "Use -f if" err
+ )
+ '
+done
+
+test_done
diff --git a/t/t2205-add-worktree-config.sh b/t/t2205-add-worktree-config.sh
new file mode 100755
index 0000000..98265ba
--- /dev/null
+++ b/t/t2205-add-worktree-config.sh
@@ -0,0 +1,266 @@
+#!/bin/sh
+
+test_description='directory traversal respects user config
+
+This test verifies the traversal of the directory tree when the traversal begins
+outside the repository. Two instances for which this can occur are tested:
+
+ 1) The user manually sets the worktree. For this instance, the test sets
+ the worktree two levels above the `.git` directory and checks whether we
+ are able to add to the index those files that are in either (1) the
+ manually configured worktree directory or (2) the standard worktree
+ location with respect to the `.git` directory (i.e. ensuring that the
+ encountered `.git` directory is not treated as belonging to a foreign
+ nested repository).
+ 2) The user manually sets the `git_dir` while the working directory is
+ outside the repository. The test checks that files inside the
+ repository can be added to the index.
+ '
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success '1a: setup--config worktree' '
+ mkdir test1 &&
+ (
+ cd test1 &&
+ test_create_repo repo &&
+ git --git-dir="repo/.git" config core.worktree "$(pwd)" &&
+
+ mkdir -p outside-tracked outside-untracked &&
+ mkdir -p repo/inside-tracked repo/inside-untracked &&
+ >file-tracked &&
+ >file-untracked &&
+ >outside-tracked/file &&
+ >outside-untracked/file &&
+ >repo/file-tracked &&
+ >repo/file-untracked &&
+ >repo/inside-tracked/file &&
+ >repo/inside-untracked/file &&
+
+ cat >expect-tracked-unsorted <<-EOF &&
+ ../file-tracked
+ ../outside-tracked/file
+ file-tracked
+ inside-tracked/file
+ EOF
+
+ cat >expect-untracked-unsorted <<-EOF &&
+ ../file-untracked
+ ../outside-untracked/file
+ file-untracked
+ inside-untracked/file
+ EOF
+
+ cat >expect-all-dir-unsorted <<-EOF &&
+ ../file-untracked
+ ../file-tracked
+ ../outside-untracked/
+ ../outside-tracked/
+ ./
+ EOF
+
+ cat expect-tracked-unsorted expect-untracked-unsorted >expect-all-unsorted &&
+
+ cat >.gitignore <<-EOF
+ .gitignore
+ actual-*
+ expect-*
+ EOF
+ )
+'
+
+test_expect_success '1b: pre-add all' '
+ (
+ cd test1 &&
+ local parent_dir="$(pwd)" &&
+ git -C repo ls-files -o --exclude-standard "$parent_dir" >actual-all-unsorted &&
+ sort actual-all-unsorted >actual-all &&
+ sort expect-all-unsorted >expect-all &&
+ test_cmp expect-all actual-all
+ )
+'
+
+test_expect_success '1c: pre-add dir all' '
+ (
+ cd test1 &&
+ local parent_dir="$(pwd)" &&
+ git -C repo ls-files -o --directory --exclude-standard "$parent_dir" >actual-all-dir-unsorted &&
+ sort actual-all-dir-unsorted >actual-all &&
+ sort expect-all-dir-unsorted >expect-all &&
+ test_cmp expect-all actual-all
+ )
+'
+
+test_expect_success '1d: post-add tracked' '
+ (
+ cd test1 &&
+ local parent_dir="$(pwd)" &&
+ (
+ cd repo &&
+ git add file-tracked &&
+ git add inside-tracked &&
+ git add ../outside-tracked &&
+ git add "$parent_dir/file-tracked" &&
+ git ls-files "$parent_dir" >../actual-tracked-unsorted
+ ) &&
+ sort actual-tracked-unsorted >actual-tracked &&
+ sort expect-tracked-unsorted >expect-tracked &&
+ test_cmp expect-tracked actual-tracked
+ )
+'
+
+test_expect_success '1e: post-add untracked' '
+ (
+ cd test1 &&
+ local parent_dir="$(pwd)" &&
+ git -C repo ls-files -o --exclude-standard "$parent_dir" >actual-untracked-unsorted &&
+ sort actual-untracked-unsorted >actual-untracked &&
+ sort expect-untracked-unsorted >expect-untracked &&
+ test_cmp expect-untracked actual-untracked
+ )
+'
+
+test_expect_success '2a: setup--set git-dir' '
+ mkdir test2 &&
+ (
+ cd test2 &&
+ test_create_repo repo &&
+ # create two foreign repositories that should remain untracked
+ test_create_repo repo-outside &&
+ test_create_repo repo/repo-inside &&
+
+ mkdir -p repo/inside-tracked repo/inside-untracked &&
+ >repo/file-tracked &&
+ >repo/file-untracked &&
+ >repo/inside-tracked/file &&
+ >repo/inside-untracked/file &&
+ >repo-outside/file &&
+ >repo/repo-inside/file &&
+
+ cat >expect-tracked-unsorted <<-EOF &&
+ repo/file-tracked
+ repo/inside-tracked/file
+ EOF
+
+ cat >expect-untracked-unsorted <<-EOF &&
+ repo/file-untracked
+ repo/inside-untracked/file
+ repo/repo-inside/
+ repo-outside/
+ EOF
+
+ cat >expect-all-dir-unsorted <<-EOF &&
+ repo/
+ repo-outside/
+ EOF
+
+ cat expect-tracked-unsorted expect-untracked-unsorted >expect-all-unsorted &&
+
+ cat >.gitignore <<-EOF
+ .gitignore
+ actual-*
+ expect-*
+ EOF
+ )
+'
+
+test_expect_success '2b: pre-add all' '
+ (
+ cd test2 &&
+ git --git-dir=repo/.git ls-files -o --exclude-standard >actual-all-unsorted &&
+ sort actual-all-unsorted >actual-all &&
+ sort expect-all-unsorted >expect-all &&
+ test_cmp expect-all actual-all
+ )
+'
+
+test_expect_success '2c: pre-add dir all' '
+ (
+ cd test2 &&
+ git --git-dir=repo/.git ls-files -o --directory --exclude-standard >actual-all-dir-unsorted &&
+ sort actual-all-dir-unsorted >actual-all &&
+ sort expect-all-dir-unsorted >expect-all &&
+ test_cmp expect-all actual-all
+ )
+'
+
+test_expect_success '2d: post-add tracked' '
+ (
+ cd test2 &&
+ git --git-dir=repo/.git add repo/file-tracked &&
+ git --git-dir=repo/.git add repo/inside-tracked &&
+ git --git-dir=repo/.git ls-files >actual-tracked-unsorted &&
+ sort actual-tracked-unsorted >actual-tracked &&
+ sort expect-tracked-unsorted >expect-tracked &&
+ test_cmp expect-tracked actual-tracked
+ )
+'
+
+test_expect_success '2e: post-add untracked' '
+ (
+ cd test2 &&
+ git --git-dir=repo/.git ls-files -o --exclude-standard >actual-untracked-unsorted &&
+ sort actual-untracked-unsorted >actual-untracked &&
+ sort expect-untracked-unsorted >expect-untracked &&
+ test_cmp expect-untracked actual-untracked
+ )
+'
+
+test_expect_success '3a: setup--add repo dir' '
+ mkdir test3 &&
+ (
+ cd test3 &&
+ test_create_repo repo &&
+
+ mkdir -p repo/inside-tracked repo/inside-ignored &&
+ >repo/file-tracked &&
+ >repo/file-ignored &&
+ >repo/inside-tracked/file &&
+ >repo/inside-ignored/file &&
+
+ cat >.gitignore <<-EOF &&
+ .gitignore
+ actual-*
+ expect-*
+ *ignored
+ EOF
+
+ cat >expect-tracked-unsorted <<-EOF &&
+ repo/file-tracked
+ repo/inside-tracked/file
+ EOF
+
+ cat >expect-ignored-unsorted <<-EOF
+ repo/file-ignored
+ repo/inside-ignored/
+ .gitignore
+ actual-ignored-unsorted
+ expect-ignored-unsorted
+ expect-tracked-unsorted
+ EOF
+ )
+'
+
+test_expect_success '3b: ignored' '
+ (
+ cd test3 &&
+ git --git-dir=repo/.git ls-files -io --directory --exclude-standard >actual-ignored-unsorted &&
+ sort actual-ignored-unsorted >actual-ignored &&
+ sort expect-ignored-unsorted >expect-ignored &&
+ test_cmp expect-ignored actual-ignored
+ )
+'
+
+test_expect_success '3c: add repo' '
+ (
+ cd test3 &&
+ git --git-dir=repo/.git add repo &&
+ git --git-dir=repo/.git ls-files >actual-tracked-unsorted &&
+ sort actual-tracked-unsorted >actual-tracked &&
+ sort expect-tracked-unsorted >expect-tracked &&
+ test_cmp expect-tracked actual-tracked
+ )
+'
+
+test_done
diff --git a/t/t2300-cd-to-toplevel.sh b/t/t2300-cd-to-toplevel.sh
new file mode 100755
index 0000000..b40eeb2
--- /dev/null
+++ b/t/t2300-cd-to-toplevel.sh
@@ -0,0 +1,47 @@
+#!/bin/sh
+
+test_description='cd_to_toplevel'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+EXEC_PATH="$(git --exec-path)"
+test_have_prereq !MINGW ||
+case "$EXEC_PATH" in
+[A-Za-z]:/*)
+ EXEC_PATH="/${EXEC_PATH%%:*}${EXEC_PATH#?:}"
+ ;;
+esac
+
+test_cd_to_toplevel () {
+ test_expect_success $3 "$2" '
+ (
+ cd '"'$1'"' &&
+ PATH="$EXEC_PATH:$PATH" &&
+ . git-sh-setup &&
+ cd_to_toplevel &&
+ [ "$(pwd -P)" = "$TOPLEVEL" ]
+ )
+ '
+}
+
+TOPLEVEL="$(pwd -P)/repo"
+mkdir -p repo/sub/dir
+mv .git repo/
+SUBDIRECTORY_OK=1
+
+test_cd_to_toplevel repo 'at physical root'
+
+test_cd_to_toplevel repo/sub/dir 'at physical subdir'
+
+ln -s repo symrepo 2>/dev/null
+test_cd_to_toplevel symrepo 'at symbolic root' SYMLINKS
+
+ln -s repo/sub/dir subdir-link 2>/dev/null
+test_cd_to_toplevel subdir-link 'at symbolic subdir' SYMLINKS
+
+cd repo
+ln -s sub/dir internal-link 2>/dev/null
+test_cd_to_toplevel internal-link 'at internal symbolic subdir' SYMLINKS
+
+test_done
diff --git a/t/t2400-worktree-add.sh b/t/t2400-worktree-add.sh
new file mode 100755
index 0000000..df4aff7
--- /dev/null
+++ b/t/t2400-worktree-add.sh
@@ -0,0 +1,1185 @@
+#!/bin/sh
+
+test_description='test git worktree add'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_CREATE_REPO_NO_TEMPLATE=1
+. ./test-lib.sh
+
+. "$TEST_DIRECTORY"/lib-rebase.sh
+
+test_expect_success 'setup' '
+ test_commit init
+'
+
+test_expect_success '"add" an existing worktree' '
+ mkdir -p existing/subtree &&
+ test_must_fail git worktree add --detach existing main
+'
+
+test_expect_success '"add" an existing empty worktree' '
+ mkdir existing_empty &&
+ git worktree add --detach existing_empty main
+'
+
+test_expect_success '"add" using shorthand - fails when no previous branch' '
+ test_must_fail git worktree add existing_short -
+'
+
+test_expect_success '"add" using - shorthand' '
+ git checkout -b newbranch &&
+ echo hello >myworld &&
+ git add myworld &&
+ git commit -m myworld &&
+ git checkout main &&
+ git worktree add short-hand - &&
+ echo refs/heads/newbranch >expect &&
+ git -C short-hand rev-parse --symbolic-full-name HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '"add" refuses to checkout locked branch' '
+ test_must_fail git worktree add zere main &&
+ ! test -d zere &&
+ ! test -d .git/worktrees/zere
+'
+
+test_expect_success 'checking out paths not complaining about linked checkouts' '
+ (
+ cd existing_empty &&
+ echo dirty >>init.t &&
+ git checkout main -- init.t
+ )
+'
+
+test_expect_success '"add" worktree' '
+ git rev-parse HEAD >expect &&
+ git worktree add --detach here main &&
+ (
+ cd here &&
+ test_cmp ../init.t init.t &&
+ test_must_fail git symbolic-ref HEAD &&
+ git rev-parse HEAD >actual &&
+ test_cmp ../expect actual &&
+ git fsck
+ )
+'
+
+test_expect_success '"add" worktree with lock' '
+ git worktree add --detach --lock here-with-lock main &&
+ test_when_finished "git worktree unlock here-with-lock || :" &&
+ test -f .git/worktrees/here-with-lock/locked
+'
+
+test_expect_success '"add" worktree with lock and reason' '
+ lock_reason="why not" &&
+ git worktree add --detach --lock --reason "$lock_reason" here-with-lock-reason main &&
+ test_when_finished "git worktree unlock here-with-lock-reason || :" &&
+ test -f .git/worktrees/here-with-lock-reason/locked &&
+ echo "$lock_reason" >expect &&
+ test_cmp expect .git/worktrees/here-with-lock-reason/locked
+'
+
+test_expect_success '"add" worktree with reason but no lock' '
+ test_must_fail git worktree add --detach --reason "why not" here-with-reason-only main &&
+ test_path_is_missing .git/worktrees/here-with-reason-only/locked
+'
+
+test_expect_success '"add" worktree from a subdir' '
+ (
+ mkdir sub &&
+ cd sub &&
+ git worktree add --detach here main &&
+ cd here &&
+ test_cmp ../../init.t init.t
+ )
+'
+
+test_expect_success '"add" from a linked checkout' '
+ (
+ cd here &&
+ git worktree add --detach nested-here main &&
+ cd nested-here &&
+ git fsck
+ )
+'
+
+test_expect_success '"add" worktree creating new branch' '
+ git worktree add -b newmain there main &&
+ (
+ cd there &&
+ test_cmp ../init.t init.t &&
+ git symbolic-ref HEAD >actual &&
+ echo refs/heads/newmain >expect &&
+ test_cmp expect actual &&
+ git fsck
+ )
+'
+
+test_expect_success 'die the same branch is already checked out' '
+ (
+ cd here &&
+ test_must_fail git checkout newmain 2>actual &&
+ grep "already used by worktree at" actual
+ )
+'
+
+test_expect_success SYMLINKS 'die the same branch is already checked out (symlink)' '
+ head=$(git -C there rev-parse --git-path HEAD) &&
+ ref=$(git -C there symbolic-ref HEAD) &&
+ rm "$head" &&
+ ln -s "$ref" "$head" &&
+ test_must_fail git -C here checkout newmain
+'
+
+test_expect_success 'not die the same branch is already checked out' '
+ (
+ cd here &&
+ git worktree add --force anothernewmain newmain
+ )
+'
+
+test_expect_success 'not die on re-checking out current branch' '
+ (
+ cd there &&
+ git checkout newmain
+ )
+'
+
+test_expect_success '"add" from a bare repo' '
+ (
+ git clone --bare . bare &&
+ cd bare &&
+ git worktree add -b bare-main ../there2 main
+ )
+'
+
+test_expect_success 'checkout from a bare repo without "add"' '
+ (
+ cd bare &&
+ test_must_fail git checkout main
+ )
+'
+
+test_expect_success '"add" default branch of a bare repo' '
+ (
+ git clone --bare . bare2 &&
+ cd bare2 &&
+ git worktree add ../there3 main &&
+ cd ../there3 &&
+ # Simple check that a Git command does not
+ # immediately fail with the current setup
+ git status
+ ) &&
+ cat >expect <<-EOF &&
+ init.t
+ EOF
+ ls there3 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '"add" to bare repo with worktree config' '
+ (
+ git clone --bare . bare3 &&
+ cd bare3 &&
+ git config extensions.worktreeconfig true &&
+
+ # Add config values that are erroneous to have in
+ # a config.worktree file outside of the main
+ # working tree, to check that Git filters them out
+ # when copying config during "git worktree add".
+ git config --worktree core.bare true &&
+ git config --worktree core.worktree "$(pwd)" &&
+
+ # We want to check that bogus.key is copied
+ git config --worktree bogus.key value &&
+ git config --unset core.bare &&
+ git worktree add ../there4 main &&
+ cd ../there4 &&
+
+ # Simple check that a Git command does not
+ # immediately fail with the current setup
+ git status &&
+ git worktree add --detach ../there5 &&
+ cd ../there5 &&
+ git status
+ ) &&
+
+ # the worktree has the arbitrary value copied.
+ test_cmp_config -C there4 value bogus.key &&
+ test_cmp_config -C there5 value bogus.key &&
+
+ # however, core.bare and core.worktree were removed.
+ test_must_fail git -C there4 config core.bare &&
+ test_must_fail git -C there4 config core.worktree &&
+
+ cat >expect <<-EOF &&
+ init.t
+ EOF
+
+ ls there4 >actual &&
+ test_cmp expect actual &&
+ ls there5 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'checkout with grafts' '
+ test_when_finished rm .git/info/grafts &&
+ test_commit abc &&
+ SHA1=$(git rev-parse HEAD) &&
+ test_commit def &&
+ test_commit xyz &&
+ mkdir .git/info &&
+ echo "$(git rev-parse HEAD) $SHA1" >.git/info/grafts &&
+ cat >expected <<-\EOF &&
+ xyz
+ abc
+ EOF
+ git log --format=%s -2 >actual &&
+ test_cmp expected actual &&
+ git worktree add --detach grafted main &&
+ git --git-dir=grafted/.git log --format=%s -2 >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success '"add" from relative HEAD' '
+ test_commit a &&
+ test_commit b &&
+ test_commit c &&
+ git rev-parse HEAD~1 >expected &&
+ git worktree add relhead HEAD~1 &&
+ git -C relhead rev-parse HEAD >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success '"add -b" with <branch> omitted' '
+ git worktree add -b burble flornk &&
+ test_cmp_rev HEAD burble
+'
+
+test_expect_success '"add --detach" with <branch> omitted' '
+ git worktree add --detach fishhook &&
+ git rev-parse HEAD >expected &&
+ git -C fishhook rev-parse HEAD >actual &&
+ test_cmp expected actual &&
+ test_must_fail git -C fishhook symbolic-ref HEAD
+'
+
+test_expect_success '"add" with <branch> omitted' '
+ git worktree add wiffle/bat &&
+ test_cmp_rev HEAD bat
+'
+
+test_expect_success '"add" checks out existing branch of dwimd name' '
+ git branch dwim HEAD~1 &&
+ git worktree add dwim &&
+ test_cmp_rev HEAD~1 dwim &&
+ (
+ cd dwim &&
+ test_cmp_rev HEAD dwim
+ )
+'
+
+test_expect_success '"add <path>" dwim fails with checked out branch' '
+ git checkout -b test-branch &&
+ test_must_fail git worktree add test-branch &&
+ test_path_is_missing test-branch
+'
+
+test_expect_success '"add --force" with existing dwimd name doesnt die' '
+ git checkout test-branch &&
+ git worktree add --force test-branch
+'
+
+test_expect_success '"add" no auto-vivify with --detach and <branch> omitted' '
+ git worktree add --detach mish/mash &&
+ test_must_fail git rev-parse mash -- &&
+ test_must_fail git -C mish/mash symbolic-ref HEAD
+'
+
+# Helper function to test mutually exclusive options.
+#
+# Note: Quoted arguments containing spaces are not supported.
+test_wt_add_excl () {
+ local opts="$*" &&
+ test_expect_success "'worktree add' with '$opts' has mutually exclusive options" '
+ test_must_fail git worktree add $opts 2>actual &&
+ grep -E "fatal:( options)? .* cannot be used together" actual
+ '
+}
+
+test_wt_add_excl -b poodle -B poodle bamboo main
+test_wt_add_excl -b poodle --detach bamboo main
+test_wt_add_excl -B poodle --detach bamboo main
+test_wt_add_excl --orphan --detach bamboo
+test_wt_add_excl --orphan --no-checkout bamboo
+test_wt_add_excl --orphan bamboo main
+test_wt_add_excl --orphan -b bamboo wtdir/ main
+
+test_expect_success '"add -B" fails if the branch is checked out' '
+ git rev-parse newmain >before &&
+ test_must_fail git worktree add -B newmain bamboo main &&
+ git rev-parse newmain >after &&
+ test_cmp before after
+'
+
+test_expect_success 'add -B' '
+ git worktree add -B poodle bamboo2 main^ &&
+ git -C bamboo2 symbolic-ref HEAD >actual &&
+ echo refs/heads/poodle >expected &&
+ test_cmp expected actual &&
+ test_cmp_rev main^ poodle
+'
+
+test_expect_success 'add --quiet' '
+ test_when_finished "git worktree remove -f -f another-worktree" &&
+ git worktree add --quiet another-worktree main 2>actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'add --quiet -b' '
+ test_when_finished "git branch -D quietnewbranch" &&
+ test_when_finished "git worktree remove -f -f another-worktree" &&
+ git worktree add --quiet -b quietnewbranch another-worktree 2>actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success '"add --orphan"' '
+ test_when_finished "git worktree remove -f -f orphandir" &&
+ git worktree add --orphan -b neworphan orphandir &&
+ echo refs/heads/neworphan >expected &&
+ git -C orphandir symbolic-ref HEAD >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success '"add --orphan (no -b)"' '
+ test_when_finished "git worktree remove -f -f neworphan" &&
+ git worktree add --orphan neworphan &&
+ echo refs/heads/neworphan >expected &&
+ git -C neworphan symbolic-ref HEAD >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success '"add --orphan --quiet"' '
+ test_when_finished "git worktree remove -f -f orphandir" &&
+ git worktree add --quiet --orphan -b neworphan orphandir 2>log.actual &&
+ test_must_be_empty log.actual &&
+ echo refs/heads/neworphan >expected &&
+ git -C orphandir symbolic-ref HEAD >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success '"add --orphan" fails if the branch already exists' '
+ test_when_finished "git branch -D existingbranch" &&
+ git worktree add -b existingbranch orphandir main &&
+ git worktree remove orphandir &&
+ test_must_fail git worktree add --orphan -b existingbranch orphandir
+'
+
+test_expect_success '"add --orphan" with empty repository' '
+ test_when_finished "rm -rf empty_repo" &&
+ echo refs/heads/newbranch >expected &&
+ GIT_DIR="empty_repo" git init --bare &&
+ git -C empty_repo worktree add --orphan -b newbranch worktreedir &&
+ git -C empty_repo/worktreedir symbolic-ref HEAD >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success '"add" worktree with orphan branch and lock' '
+ git worktree add --lock --orphan -b orphanbr orphan-with-lock &&
+ test_when_finished "git worktree unlock orphan-with-lock || :" &&
+ test -f .git/worktrees/orphan-with-lock/locked
+'
+
+test_expect_success '"add" worktree with orphan branch, lock, and reason' '
+ lock_reason="why not" &&
+ git worktree add --detach --lock --reason "$lock_reason" orphan-with-lock-reason main &&
+ test_when_finished "git worktree unlock orphan-with-lock-reason || :" &&
+ test -f .git/worktrees/orphan-with-lock-reason/locked &&
+ echo "$lock_reason" >expect &&
+ test_cmp expect .git/worktrees/orphan-with-lock-reason/locked
+'
+
+# Note: Quoted arguments containing spaces are not supported.
+test_wt_add_orphan_hint () {
+ local context="$1" &&
+ local use_branch=$2 &&
+ shift 2 &&
+ local opts="$*" &&
+ test_expect_success "'worktree add' show orphan hint in bad/orphan HEAD w/ $context" '
+ test_when_finished "rm -rf repo" &&
+ git init repo &&
+ (cd repo && test_commit commit) &&
+ git -C repo switch --orphan noref &&
+ test_must_fail git -C repo worktree add $opts foobar/ 2>actual &&
+ ! grep "error: unknown switch" actual &&
+ grep "hint: If you meant to create a worktree containing a new orphan branch" actual &&
+ if [ $use_branch -eq 1 ]
+ then
+ grep -E "^hint: +git worktree add --orphan -b [^ ]+ [^ ]+$" actual
+ else
+ grep -E "^hint: +git worktree add --orphan [^ ]+$" actual
+ fi
+
+ '
+}
+
+test_wt_add_orphan_hint 'no opts' 0
+test_wt_add_orphan_hint '-b' 1 -b foobar_branch
+test_wt_add_orphan_hint '-B' 1 -B foobar_branch
+
+test_expect_success "'worktree add' doesn't show orphan hint in bad/orphan HEAD w/ --quiet" '
+ test_when_finished "rm -rf repo" &&
+ git init repo &&
+ (cd repo && test_commit commit) &&
+ test_must_fail git -C repo worktree add --quiet foobar_branch foobar/ 2>actual &&
+ ! grep "error: unknown switch" actual &&
+ ! grep "hint: If you meant to create a worktree containing a new orphan branch" actual
+'
+
+test_expect_success 'local clone from linked checkout' '
+ git clone --local here here-clone &&
+ ( cd here-clone && git fsck )
+'
+
+test_expect_success 'local clone --shared from linked checkout' '
+ git -C bare worktree add --detach ../baretree &&
+ git clone --local --shared baretree bare-clone &&
+ grep /bare/ bare-clone/.git/objects/info/alternates
+'
+
+test_expect_success '"add" worktree with --no-checkout' '
+ git worktree add --no-checkout -b swamp swamp &&
+ ! test -e swamp/init.t &&
+ git -C swamp reset --hard &&
+ test_cmp init.t swamp/init.t
+'
+
+test_expect_success '"add" worktree with --checkout' '
+ git worktree add --checkout -b swmap2 swamp2 &&
+ test_cmp init.t swamp2/init.t
+'
+
+test_expect_success 'put a worktree under rebase' '
+ git worktree add under-rebase &&
+ (
+ cd under-rebase &&
+ set_fake_editor &&
+ FAKE_LINES="edit 1" git rebase -i HEAD^ &&
+ git worktree list | grep "under-rebase.*detached HEAD"
+ )
+'
+
+test_expect_success 'add a worktree, checking out a rebased branch' '
+ test_must_fail git worktree add new-rebase under-rebase &&
+ ! test -d new-rebase
+'
+
+test_expect_success 'checking out a rebased branch from another worktree' '
+ git worktree add new-place &&
+ test_must_fail git -C new-place checkout under-rebase
+'
+
+test_expect_success 'not allow to delete a branch under rebase' '
+ (
+ cd under-rebase &&
+ test_must_fail git branch -D under-rebase
+ )
+'
+
+test_expect_success 'rename a branch under rebase not allowed' '
+ test_must_fail git branch -M under-rebase rebase-with-new-name
+'
+
+test_expect_success 'check out from current worktree branch ok' '
+ (
+ cd under-rebase &&
+ git checkout under-rebase &&
+ git checkout - &&
+ git rebase --abort
+ )
+'
+
+test_expect_success 'checkout a branch under bisect' '
+ git worktree add under-bisect &&
+ (
+ cd under-bisect &&
+ git bisect start &&
+ git bisect bad &&
+ git bisect good HEAD~2 &&
+ git worktree list | grep "under-bisect.*detached HEAD" &&
+ test_must_fail git worktree add new-bisect under-bisect &&
+ ! test -d new-bisect
+ )
+'
+
+test_expect_success 'rename a branch under bisect not allowed' '
+ test_must_fail git branch -M under-bisect bisect-with-new-name
+'
+# Is branch "refs/heads/$1" set to pull from "$2/$3"?
+test_branch_upstream () {
+ printf "%s\n" "$2" "refs/heads/$3" >expect.upstream &&
+ {
+ git config "branch.$1.remote" &&
+ git config "branch.$1.merge"
+ } >actual.upstream &&
+ test_cmp expect.upstream actual.upstream
+}
+
+test_expect_success '--track sets up tracking' '
+ test_when_finished rm -rf track &&
+ git worktree add --track -b track track main &&
+ test_branch_upstream track . main
+'
+
+# setup remote repository $1 and repository $2 with $1 set up as
+# remote. The remote has two branches, main and foo.
+setup_remote_repo () {
+ git init $1 &&
+ (
+ cd $1 &&
+ test_commit $1_main &&
+ git checkout -b foo &&
+ test_commit upstream_foo
+ ) &&
+ git init $2 &&
+ (
+ cd $2 &&
+ test_commit $2_main &&
+ git remote add $1 ../$1 &&
+ git config remote.$1.fetch \
+ "refs/heads/*:refs/remotes/$1/*" &&
+ git fetch --all
+ )
+}
+
+test_expect_success '"add" <path> <remote/branch> w/ no HEAD' '
+ test_when_finished rm -rf repo_upstream repo_local foo &&
+ setup_remote_repo repo_upstream repo_local &&
+ git -C repo_local config --bool core.bare true &&
+ git -C repo_local branch -D main &&
+ git -C repo_local worktree add ./foo repo_upstream/foo
+'
+
+test_expect_success '--no-track avoids setting up tracking' '
+ test_when_finished rm -rf repo_upstream repo_local foo &&
+ setup_remote_repo repo_upstream repo_local &&
+ (
+ cd repo_local &&
+ git worktree add --no-track -b foo ../foo repo_upstream/foo
+ ) &&
+ (
+ cd foo &&
+ test_must_fail git config "branch.foo.remote" &&
+ test_must_fail git config "branch.foo.merge" &&
+ test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo
+ )
+'
+
+test_expect_success '"add" <path> <non-existent-branch> fails' '
+ test_must_fail git worktree add foo non-existent
+'
+
+test_expect_success '"add" <path> <branch> dwims' '
+ test_when_finished rm -rf repo_upstream repo_dwim foo &&
+ setup_remote_repo repo_upstream repo_dwim &&
+ git init repo_dwim &&
+ (
+ cd repo_dwim &&
+ git worktree add ../foo foo
+ ) &&
+ (
+ cd foo &&
+ test_branch_upstream foo repo_upstream foo &&
+ test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo
+ )
+'
+
+test_expect_success '"add" <path> <branch> dwims with checkout.defaultRemote' '
+ test_when_finished rm -rf repo_upstream repo_dwim foo &&
+ setup_remote_repo repo_upstream repo_dwim &&
+ git init repo_dwim &&
+ (
+ cd repo_dwim &&
+ git remote add repo_upstream2 ../repo_upstream &&
+ git fetch repo_upstream2 &&
+ test_must_fail git worktree add ../foo foo &&
+ git -c checkout.defaultRemote=repo_upstream worktree add ../foo foo &&
+ git status -uno --porcelain >status.actual &&
+ test_must_be_empty status.actual
+ ) &&
+ (
+ cd foo &&
+ test_branch_upstream foo repo_upstream foo &&
+ test_cmp_rev refs/remotes/repo_upstream/foo refs/heads/foo
+ )
+'
+
+test_expect_success 'git worktree add does not match remote' '
+ test_when_finished rm -rf repo_a repo_b foo &&
+ setup_remote_repo repo_a repo_b &&
+ (
+ cd repo_b &&
+ git worktree add ../foo
+ ) &&
+ (
+ cd foo &&
+ test_must_fail git config "branch.foo.remote" &&
+ test_must_fail git config "branch.foo.merge" &&
+ test_cmp_rev ! refs/remotes/repo_a/foo refs/heads/foo
+ )
+'
+
+test_expect_success 'git worktree add --guess-remote sets up tracking' '
+ test_when_finished rm -rf repo_a repo_b foo &&
+ setup_remote_repo repo_a repo_b &&
+ (
+ cd repo_b &&
+ git worktree add --guess-remote ../foo
+ ) &&
+ (
+ cd foo &&
+ test_branch_upstream foo repo_a foo &&
+ test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
+ )
+'
+test_expect_success 'git worktree add --guess-remote sets up tracking (quiet)' '
+ test_when_finished rm -rf repo_a repo_b foo &&
+ setup_remote_repo repo_a repo_b &&
+ (
+ cd repo_b &&
+ git worktree add --quiet --guess-remote ../foo 2>actual &&
+ test_must_be_empty actual
+ ) &&
+ (
+ cd foo &&
+ test_branch_upstream foo repo_a foo &&
+ test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
+ )
+'
+
+test_expect_success 'git worktree --no-guess-remote (quiet)' '
+ test_when_finished rm -rf repo_a repo_b foo &&
+ setup_remote_repo repo_a repo_b &&
+ (
+ cd repo_b &&
+ git worktree add --quiet --no-guess-remote ../foo
+ ) &&
+ (
+ cd foo &&
+ test_must_fail git config "branch.foo.remote" &&
+ test_must_fail git config "branch.foo.merge" &&
+ test_cmp_rev ! refs/remotes/repo_a/foo refs/heads/foo
+ )
+'
+
+test_expect_success 'git worktree add with worktree.guessRemote sets up tracking' '
+ test_when_finished rm -rf repo_a repo_b foo &&
+ setup_remote_repo repo_a repo_b &&
+ (
+ cd repo_b &&
+ git config worktree.guessRemote true &&
+ git worktree add ../foo
+ ) &&
+ (
+ cd foo &&
+ test_branch_upstream foo repo_a foo &&
+ test_cmp_rev refs/remotes/repo_a/foo refs/heads/foo
+ )
+'
+
+test_expect_success 'git worktree --no-guess-remote option overrides config' '
+ test_when_finished rm -rf repo_a repo_b foo &&
+ setup_remote_repo repo_a repo_b &&
+ (
+ cd repo_b &&
+ git config worktree.guessRemote true &&
+ git worktree add --no-guess-remote ../foo
+ ) &&
+ (
+ cd foo &&
+ test_must_fail git config "branch.foo.remote" &&
+ test_must_fail git config "branch.foo.merge" &&
+ test_cmp_rev ! refs/remotes/repo_a/foo refs/heads/foo
+ )
+'
+
+test_dwim_orphan () {
+ local info_text="No possible source branch, inferring '--orphan'" &&
+ local fetch_error_text="fatal: No local or remote refs exist despite at least one remote" &&
+ local orphan_hint="hint: If you meant to create a worktree containing a new orphan branch" &&
+ local invalid_ref_regex="^fatal: invalid reference: " &&
+ local bad_combo_regex="^fatal: '[-a-z]*' and '[-a-z]*' cannot be used together" &&
+
+ local git_ns="repo" &&
+ local dashc_args="-C $git_ns" &&
+ local use_cd=0 &&
+
+ local bad_head=0 &&
+ local empty_repo=1 &&
+ local local_ref=0 &&
+ local use_quiet=0 &&
+ local remote=0 &&
+ local remote_ref=0 &&
+ local use_detach=0 &&
+ local use_new_branch=0 &&
+
+ local outcome="$1" &&
+ local outcome_text &&
+ local success &&
+ shift &&
+ local args="" &&
+ local context="" &&
+ case "$outcome" in
+ "infer")
+ success=1 &&
+ outcome_text='"add" DWIM infer --orphan'
+ ;;
+ "no_infer")
+ success=1 &&
+ outcome_text='"add" DWIM doesnt infer --orphan'
+ ;;
+ "fetch_error")
+ success=0 &&
+ outcome_text='"add" error need fetch'
+ ;;
+ "fatal_orphan_bad_combo")
+ success=0 &&
+ outcome_text='"add" error inferred "--orphan" gives illegal opts combo'
+ ;;
+ "warn_bad_head")
+ success=0 &&
+ outcome_text='"add" error, warn on bad HEAD, hint use orphan'
+ ;;
+ *)
+ echo "test_dwim_orphan(): invalid outcome: '$outcome'" >&2 &&
+ return 1
+ ;;
+ esac &&
+ while [ $# -gt 0 ]
+ do
+ case "$1" in
+ # How and from where to create the worktree
+ "-C_repo")
+ use_cd=0 &&
+ git_ns="repo" &&
+ dashc_args="-C $git_ns" &&
+ context="$context, 'git -C repo'"
+ ;;
+ "-C_wt")
+ use_cd=0 &&
+ git_ns="wt" &&
+ dashc_args="-C $git_ns" &&
+ context="$context, 'git -C wt'"
+ ;;
+ "cd_repo")
+ use_cd=1 &&
+ git_ns="repo" &&
+ dashc_args="" &&
+ context="$context, 'cd repo && git'"
+ ;;
+ "cd_wt")
+ use_cd=1 &&
+ git_ns="wt" &&
+ dashc_args="" &&
+ context="$context, 'cd wt && git'"
+ ;;
+
+ # Bypass the "pull first" warning
+ "force")
+ args="$args --force" &&
+ context="$context, --force"
+ ;;
+
+ # Try to use remote refs when DWIM
+ "guess_remote")
+ args="$args --guess-remote" &&
+ context="$context, --guess-remote"
+ ;;
+ "no_guess_remote")
+ args="$args --no-guess-remote" &&
+ context="$context, --no-guess-remote"
+ ;;
+
+ # Whether there is at least one local branch present
+ "local_ref")
+ empty_repo=0 &&
+ local_ref=1 &&
+ context="$context, >=1 local branches"
+ ;;
+ "no_local_ref")
+ empty_repo=0 &&
+ context="$context, 0 local branches"
+ ;;
+
+ # Whether the HEAD points at a valid ref (skip this opt when no refs)
+ "good_head")
+ # requires: local_ref
+ context="$context, valid HEAD"
+ ;;
+ "bad_head")
+ bad_head=1 &&
+ context="$context, invalid (or orphan) HEAD"
+ ;;
+
+ # Whether the code path is tested with the base add command, -b, or --detach
+ "no_-b")
+ use_new_branch=0 &&
+ context="$context, no --branch"
+ ;;
+ "-b")
+ use_new_branch=1 &&
+ context="$context, --branch"
+ ;;
+ "detach")
+ use_detach=1 &&
+ context="$context, --detach"
+ ;;
+
+ # Whether to check that all output is suppressed (except errors)
+ # or that the output is as expected
+ "quiet")
+ use_quiet=1 &&
+ args="$args --quiet" &&
+ context="$context, --quiet"
+ ;;
+ "no_quiet")
+ use_quiet=0 &&
+ context="$context, no --quiet (expect output)"
+ ;;
+
+ # Whether there is at least one remote attached to the repo
+ "remote")
+ empty_repo=0 &&
+ remote=1 &&
+ context="$context, >=1 remotes"
+ ;;
+ "no_remote")
+ empty_repo=0 &&
+ remote=0 &&
+ context="$context, 0 remotes"
+ ;;
+
+ # Whether there is at least one valid remote ref
+ "remote_ref")
+ # requires: remote
+ empty_repo=0 &&
+ remote_ref=1 &&
+ context="$context, >=1 fetched remote branches"
+ ;;
+ "no_remote_ref")
+ empty_repo=0 &&
+ remote_ref=0 &&
+ context="$context, 0 fetched remote branches"
+ ;;
+
+ # Options or flags that become illegal when --orphan is inferred
+ "no_checkout")
+ args="$args --no-checkout" &&
+ context="$context, --no-checkout"
+ ;;
+ "track")
+ args="$args --track" &&
+ context="$context, --track"
+ ;;
+
+ # All other options are illegal
+ *)
+ echo "test_dwim_orphan(): invalid arg: '$1'" >&2 &&
+ return 1
+ ;;
+ esac &&
+ shift
+ done &&
+ context="${context#', '}" &&
+ if [ $use_new_branch -eq 1 ]
+ then
+ args="$args -b foo"
+ elif [ $use_detach -eq 1 ]
+ then
+ args="$args --detach"
+ else
+ context="DWIM (no --branch), $context"
+ fi &&
+ if [ $empty_repo -eq 1 ]
+ then
+ context="empty repo, $context"
+ fi &&
+ args="$args ../foo" &&
+ context="${context%', '}" &&
+ test_expect_success "$outcome_text w/ $context" '
+ test_when_finished "rm -rf repo" &&
+ git init repo &&
+ if [ $local_ref -eq 1 ] && [ "$git_ns" = "repo" ]
+ then
+ (cd repo && test_commit commit) &&
+ if [ $bad_head -eq 1 ]
+ then
+ git -C repo symbolic-ref HEAD refs/heads/badbranch
+ fi
+ elif [ $local_ref -eq 1 ] && [ "$git_ns" = "wt" ]
+ then
+ test_when_finished "git -C repo worktree remove -f ../wt" &&
+ git -C repo worktree add --orphan -b main ../wt &&
+ (cd wt && test_commit commit) &&
+ if [ $bad_head -eq 1 ]
+ then
+ git -C wt symbolic-ref HEAD refs/heads/badbranch
+ fi
+ elif [ $local_ref -eq 0 ] && [ "$git_ns" = "wt" ]
+ then
+ test_when_finished "git -C repo worktree remove -f ../wt" &&
+ git -C repo worktree add --orphan -b orphanbranch ../wt
+ fi &&
+
+ if [ $remote -eq 1 ]
+ then
+ test_when_finished "rm -rf upstream" &&
+ git init upstream &&
+ (cd upstream && test_commit commit) &&
+ git -C upstream switch -c foo &&
+ git -C repo remote add upstream ../upstream
+ fi &&
+
+ if [ $remote_ref -eq 1 ]
+ then
+ git -C repo fetch
+ fi &&
+ if [ $success -eq 1 ]
+ then
+ test_when_finished git -C repo worktree remove ../foo
+ fi &&
+ (
+ if [ $use_cd -eq 1 ]
+ then
+ cd $git_ns
+ fi &&
+ if [ "$outcome" = "infer" ]
+ then
+ git $dashc_args worktree add $args 2>actual &&
+ if [ $use_quiet -eq 1 ]
+ then
+ test_must_be_empty actual
+ else
+ grep "$info_text" actual
+ fi
+ elif [ "$outcome" = "no_infer" ]
+ then
+ git $dashc_args worktree add $args 2>actual &&
+ if [ $use_quiet -eq 1 ]
+ then
+ test_must_be_empty actual
+ else
+ ! grep "$info_text" actual
+ fi
+ elif [ "$outcome" = "fetch_error" ]
+ then
+ test_must_fail git $dashc_args worktree add $args 2>actual &&
+ grep "$fetch_error_text" actual
+ elif [ "$outcome" = "fatal_orphan_bad_combo" ]
+ then
+ test_must_fail git $dashc_args worktree add $args 2>actual &&
+ if [ $use_quiet -eq 1 ]
+ then
+ ! grep "$info_text" actual
+ else
+ grep "$info_text" actual
+ fi &&
+ grep "$bad_combo_regex" actual
+ elif [ "$outcome" = "warn_bad_head" ]
+ then
+ test_must_fail git $dashc_args worktree add $args 2>actual &&
+ if [ $use_quiet -eq 1 ]
+ then
+ grep "$invalid_ref_regex" actual &&
+ ! grep "$orphan_hint" actual
+ else
+ headpath=$(git $dashc_args rev-parse --path-format=absolute --git-path HEAD) &&
+ headcontents=$(cat "$headpath") &&
+ grep "HEAD points to an invalid (or orphaned) reference" actual &&
+ grep "HEAD path: .$headpath." actual &&
+ grep "HEAD contents: .$headcontents." actual &&
+ grep "$orphan_hint" actual &&
+ ! grep "$info_text" actual
+ fi &&
+ grep "$invalid_ref_regex" actual
+ else
+ # Unreachable
+ false
+ fi
+ ) &&
+ if [ $success -ne 1 ]
+ then
+ test_path_is_missing foo
+ fi
+ '
+}
+
+for quiet_mode in "no_quiet" "quiet"
+do
+ for changedir_type in "cd_repo" "cd_wt" "-C_repo" "-C_wt"
+ do
+ dwim_test_args="$quiet_mode $changedir_type"
+ test_dwim_orphan 'infer' $dwim_test_args no_-b
+ test_dwim_orphan 'no_infer' $dwim_test_args no_-b local_ref good_head
+ test_dwim_orphan 'infer' $dwim_test_args no_-b no_local_ref no_remote no_remote_ref no_guess_remote
+ test_dwim_orphan 'infer' $dwim_test_args no_-b no_local_ref remote no_remote_ref no_guess_remote
+ test_dwim_orphan 'fetch_error' $dwim_test_args no_-b no_local_ref remote no_remote_ref guess_remote
+ test_dwim_orphan 'infer' $dwim_test_args no_-b no_local_ref remote no_remote_ref guess_remote force
+ test_dwim_orphan 'no_infer' $dwim_test_args no_-b no_local_ref remote remote_ref guess_remote
+
+ test_dwim_orphan 'infer' $dwim_test_args -b
+ test_dwim_orphan 'no_infer' $dwim_test_args -b local_ref good_head
+ test_dwim_orphan 'infer' $dwim_test_args -b no_local_ref no_remote no_remote_ref no_guess_remote
+ test_dwim_orphan 'infer' $dwim_test_args -b no_local_ref remote no_remote_ref no_guess_remote
+ test_dwim_orphan 'infer' $dwim_test_args -b no_local_ref remote no_remote_ref guess_remote
+ test_dwim_orphan 'infer' $dwim_test_args -b no_local_ref remote remote_ref guess_remote
+
+ test_dwim_orphan 'warn_bad_head' $dwim_test_args no_-b local_ref bad_head
+ test_dwim_orphan 'warn_bad_head' $dwim_test_args -b local_ref bad_head
+ test_dwim_orphan 'warn_bad_head' $dwim_test_args detach local_ref bad_head
+ done
+
+ test_dwim_orphan 'fatal_orphan_bad_combo' $quiet_mode no_-b no_checkout
+ test_dwim_orphan 'fatal_orphan_bad_combo' $quiet_mode no_-b track
+ test_dwim_orphan 'fatal_orphan_bad_combo' $quiet_mode -b no_checkout
+ test_dwim_orphan 'fatal_orphan_bad_combo' $quiet_mode -b track
+done
+
+post_checkout_hook () {
+ test_when_finished "rm -rf .git/hooks" &&
+ mkdir .git/hooks &&
+ test_hook -C "$1" post-checkout <<-\EOF
+ {
+ echo $*
+ git rev-parse --git-dir --show-toplevel
+ } >hook.actual
+ EOF
+}
+
+test_expect_success '"add" invokes post-checkout hook (branch)' '
+ post_checkout_hook &&
+ {
+ echo $ZERO_OID $(git rev-parse HEAD) 1 &&
+ echo $(pwd)/.git/worktrees/gumby &&
+ echo $(pwd)/gumby
+ } >hook.expect &&
+ git worktree add gumby &&
+ test_cmp hook.expect gumby/hook.actual
+'
+
+test_expect_success '"add" invokes post-checkout hook (detached)' '
+ post_checkout_hook &&
+ {
+ echo $ZERO_OID $(git rev-parse HEAD) 1 &&
+ echo $(pwd)/.git/worktrees/grumpy &&
+ echo $(pwd)/grumpy
+ } >hook.expect &&
+ git worktree add --detach grumpy &&
+ test_cmp hook.expect grumpy/hook.actual
+'
+
+test_expect_success '"add --no-checkout" suppresses post-checkout hook' '
+ post_checkout_hook &&
+ rm -f hook.actual &&
+ git worktree add --no-checkout gloopy &&
+ test_path_is_missing gloopy/hook.actual
+'
+
+test_expect_success '"add" in other worktree invokes post-checkout hook' '
+ post_checkout_hook &&
+ {
+ echo $ZERO_OID $(git rev-parse HEAD) 1 &&
+ echo $(pwd)/.git/worktrees/guppy &&
+ echo $(pwd)/guppy
+ } >hook.expect &&
+ git -C gloopy worktree add --detach ../guppy &&
+ test_cmp hook.expect guppy/hook.actual
+'
+
+test_expect_success '"add" in bare repo invokes post-checkout hook' '
+ rm -rf bare &&
+ git clone --bare . bare &&
+ {
+ echo $ZERO_OID $(git --git-dir=bare rev-parse HEAD) 1 &&
+ echo $(pwd)/bare/worktrees/goozy &&
+ echo $(pwd)/goozy
+ } >hook.expect &&
+ post_checkout_hook bare &&
+ git -C bare worktree add --detach ../goozy &&
+ test_cmp hook.expect goozy/hook.actual
+'
+
+test_expect_success '"add" an existing but missing worktree' '
+ git worktree add --detach pneu &&
+ test_must_fail git worktree add --detach pneu &&
+ rm -fr pneu &&
+ test_must_fail git worktree add --detach pneu &&
+ git worktree add --force --detach pneu
+'
+
+test_expect_success '"add" an existing locked but missing worktree' '
+ git worktree add --detach gnoo &&
+ git worktree lock gnoo &&
+ test_when_finished "git worktree unlock gnoo || :" &&
+ rm -fr gnoo &&
+ test_must_fail git worktree add --detach gnoo &&
+ test_must_fail git worktree add --force --detach gnoo &&
+ git worktree add --force --force --detach gnoo
+'
+
+test_expect_success '"add" not tripped up by magic worktree matching"' '
+ # if worktree "sub1/bar" exists, "git worktree add bar" in distinct
+ # directory `sub2` should not mistakenly complain that `bar` is an
+ # already-registered worktree
+ mkdir sub1 sub2 &&
+ git -C sub1 --git-dir=../.git worktree add --detach bozo &&
+ git -C sub2 --git-dir=../.git worktree add --detach bozo
+'
+
+test_expect_success FUNNYNAMES 'sanitize generated worktree name' '
+ git worktree add --detach ". weird*..?.lock.lock" &&
+ test -d .git/worktrees/---weird-.-
+'
+
+test_expect_success '"add" should not fail because of another bad worktree' '
+ git init add-fail &&
+ (
+ cd add-fail &&
+ test_commit first &&
+ mkdir sub &&
+ git worktree add sub/to-be-deleted &&
+ rm -rf sub &&
+ git worktree add second
+ )
+'
+
+test_expect_success '"add" with uninitialized submodule, with submodule.recurse unset' '
+ test_config_global protocol.file.allow always &&
+ test_create_repo submodule &&
+ test_commit -C submodule first &&
+ test_create_repo project &&
+ git -C project submodule add ../submodule &&
+ git -C project add submodule &&
+ test_tick &&
+ git -C project commit -m add_sub &&
+ git clone project project-clone &&
+ git -C project-clone worktree add ../project-2
+'
+test_expect_success '"add" with uninitialized submodule, with submodule.recurse set' '
+ git -C project-clone -c submodule.recurse worktree add ../project-3
+'
+
+test_expect_success '"add" with initialized submodule, with submodule.recurse unset' '
+ test_config_global protocol.file.allow always &&
+ git -C project-clone submodule update --init &&
+ git -C project-clone worktree add ../project-4
+'
+
+test_expect_success '"add" with initialized submodule, with submodule.recurse set' '
+ git -C project-clone -c submodule.recurse worktree add ../project-5
+'
+
+test_done
diff --git a/t/t2401-worktree-prune.sh b/t/t2401-worktree-prune.sh
new file mode 100755
index 0000000..71aa9bc
--- /dev/null
+++ b/t/t2401-worktree-prune.sh
@@ -0,0 +1,123 @@
+#!/bin/sh
+
+test_description='prune $GIT_DIR/worktrees'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success initialize '
+ git commit --allow-empty -m init
+'
+
+test_expect_success 'worktree prune on normal repo' '
+ git worktree prune &&
+ test_must_fail git worktree prune abc
+'
+
+test_expect_success 'prune files inside $GIT_DIR/worktrees' '
+ mkdir .git/worktrees &&
+ : >.git/worktrees/abc &&
+ git worktree prune --verbose 2>actual &&
+ cat >expect <<EOF &&
+Removing worktrees/abc: not a valid directory
+EOF
+ test_cmp expect actual &&
+ ! test -f .git/worktrees/abc &&
+ ! test -d .git/worktrees
+'
+
+test_expect_success 'prune directories without gitdir' '
+ mkdir -p .git/worktrees/def/abc &&
+ : >.git/worktrees/def/def &&
+ cat >expect <<EOF &&
+Removing worktrees/def: gitdir file does not exist
+EOF
+ git worktree prune --verbose 2>actual &&
+ test_cmp expect actual &&
+ ! test -d .git/worktrees/def &&
+ ! test -d .git/worktrees
+'
+
+test_expect_success SANITY 'prune directories with unreadable gitdir' '
+ mkdir -p .git/worktrees/def/abc &&
+ : >.git/worktrees/def/def &&
+ : >.git/worktrees/def/gitdir &&
+ chmod u-r .git/worktrees/def/gitdir &&
+ git worktree prune --verbose 2>actual &&
+ test_grep "Removing worktrees/def: unable to read gitdir file" actual &&
+ ! test -d .git/worktrees/def &&
+ ! test -d .git/worktrees
+'
+
+test_expect_success 'prune directories with invalid gitdir' '
+ mkdir -p .git/worktrees/def/abc &&
+ : >.git/worktrees/def/def &&
+ : >.git/worktrees/def/gitdir &&
+ git worktree prune --verbose 2>actual &&
+ test_grep "Removing worktrees/def: invalid gitdir file" actual &&
+ ! test -d .git/worktrees/def &&
+ ! test -d .git/worktrees
+'
+
+test_expect_success 'prune directories with gitdir pointing to nowhere' '
+ mkdir -p .git/worktrees/def/abc &&
+ : >.git/worktrees/def/def &&
+ echo "$(pwd)"/nowhere >.git/worktrees/def/gitdir &&
+ git worktree prune --verbose 2>actual &&
+ test_grep "Removing worktrees/def: gitdir file points to non-existent location" actual &&
+ ! test -d .git/worktrees/def &&
+ ! test -d .git/worktrees
+'
+
+test_expect_success 'not prune locked checkout' '
+ test_when_finished rm -r .git/worktrees &&
+ mkdir -p .git/worktrees/ghi &&
+ : >.git/worktrees/ghi/locked &&
+ git worktree prune &&
+ test -d .git/worktrees/ghi
+'
+
+test_expect_success 'not prune recent checkouts' '
+ test_when_finished rm -r .git/worktrees &&
+ git worktree add jlm HEAD &&
+ test -d .git/worktrees/jlm &&
+ rm -rf jlm &&
+ git worktree prune --verbose --expire=2.days.ago &&
+ test -d .git/worktrees/jlm
+'
+
+test_expect_success 'not prune proper checkouts' '
+ test_when_finished rm -r .git/worktrees &&
+ git worktree add --detach "$PWD/nop" main &&
+ git worktree prune &&
+ test -d .git/worktrees/nop
+'
+
+test_expect_success 'prune duplicate (linked/linked)' '
+ test_when_finished rm -fr .git/worktrees w1 w2 &&
+ git worktree add --detach w1 &&
+ git worktree add --detach w2 &&
+ sed "s/w2/w1/" .git/worktrees/w2/gitdir >.git/worktrees/w2/gitdir.new &&
+ mv .git/worktrees/w2/gitdir.new .git/worktrees/w2/gitdir &&
+ git worktree prune --verbose 2>actual &&
+ test_grep "duplicate entry" actual &&
+ test -d .git/worktrees/w1 &&
+ ! test -d .git/worktrees/w2
+'
+
+test_expect_success 'prune duplicate (main/linked)' '
+ test_when_finished rm -fr repo wt &&
+ test_create_repo repo &&
+ test_commit -C repo x &&
+ git -C repo worktree add --detach ../wt &&
+ rm -fr wt &&
+ mv repo wt &&
+ git -C wt worktree prune --verbose 2>actual &&
+ test_grep "duplicate entry" actual &&
+ ! test -d .git/worktrees/wt
+'
+
+test_done
diff --git a/t/t2402-worktree-list.sh b/t/t2402-worktree-list.sh
new file mode 100755
index 0000000..33ea9cb
--- /dev/null
+++ b/t/t2402-worktree-list.sh
@@ -0,0 +1,289 @@
+#!/bin/sh
+
+test_description='test git worktree list'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ test_commit init
+'
+
+test_expect_success 'rev-parse --git-common-dir on main worktree' '
+ git rev-parse --git-common-dir >actual &&
+ echo .git >expected &&
+ test_cmp expected actual &&
+ mkdir sub &&
+ git -C sub rev-parse --git-common-dir >actual2 &&
+ echo ../.git >expected2 &&
+ test_cmp expected2 actual2
+'
+
+test_expect_success 'rev-parse --git-path objects linked worktree' '
+ echo "$(git rev-parse --show-toplevel)/.git/objects" >expect &&
+ test_when_finished "rm -rf linked-tree actual expect && git worktree prune" &&
+ git worktree add --detach linked-tree main &&
+ git -C linked-tree rev-parse --git-path objects >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '"list" all worktrees from main' '
+ echo "$(git rev-parse --show-toplevel) $(git rev-parse --short HEAD) [$(git symbolic-ref --short HEAD)]" >expect &&
+ test_when_finished "rm -rf here out actual expect && git worktree prune" &&
+ git worktree add --detach here main &&
+ echo "$(git -C here rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >>expect &&
+ git worktree list >out &&
+ sed "s/ */ /g" <out >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '"list" all worktrees from linked' '
+ echo "$(git rev-parse --show-toplevel) $(git rev-parse --short HEAD) [$(git symbolic-ref --short HEAD)]" >expect &&
+ test_when_finished "rm -rf here out actual expect && git worktree prune" &&
+ git worktree add --detach here main &&
+ echo "$(git -C here rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >>expect &&
+ git -C here worktree list >out &&
+ sed "s/ */ /g" <out >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '"list" all worktrees --porcelain' '
+ echo "worktree $(git rev-parse --show-toplevel)" >expect &&
+ echo "HEAD $(git rev-parse HEAD)" >>expect &&
+ echo "branch $(git symbolic-ref HEAD)" >>expect &&
+ echo >>expect &&
+ test_when_finished "rm -rf here actual expect && git worktree prune" &&
+ git worktree add --detach here main &&
+ echo "worktree $(git -C here rev-parse --show-toplevel)" >>expect &&
+ echo "HEAD $(git rev-parse HEAD)" >>expect &&
+ echo "detached" >>expect &&
+ echo >>expect &&
+ git worktree list --porcelain >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '"list" all worktrees --porcelain -z' '
+ test_when_finished "rm -rf here _actual actual expect &&
+ git worktree prune" &&
+ printf "worktree %sQHEAD %sQbranch %sQQ" \
+ "$(git rev-parse --show-toplevel)" \
+ $(git rev-parse HEAD --symbolic-full-name HEAD) >expect &&
+ git worktree add --detach here main &&
+ printf "worktree %sQHEAD %sQdetachedQQ" \
+ "$(git -C here rev-parse --show-toplevel)" \
+ "$(git rev-parse HEAD)" >>expect &&
+ git worktree list --porcelain -z >_actual &&
+ nul_to_q <_actual >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '"list" -z fails without --porcelain' '
+ test_must_fail git worktree list -z
+'
+
+test_expect_success '"list" all worktrees with locked annotation' '
+ test_when_finished "rm -rf locked unlocked out && git worktree prune" &&
+ git worktree add --detach locked main &&
+ git worktree add --detach unlocked main &&
+ git worktree lock locked &&
+ test_when_finished "git worktree unlock locked" &&
+ git worktree list >out &&
+ grep "/locked *[0-9a-f].* locked$" out &&
+ ! grep "/unlocked *[0-9a-f].* locked$" out
+'
+
+test_expect_success '"list" all worktrees --porcelain with locked' '
+ test_when_finished "rm -rf locked1 locked2 unlocked out actual expect && git worktree prune" &&
+ echo "locked" >expect &&
+ echo "locked with reason" >>expect &&
+ git worktree add --detach locked1 &&
+ git worktree add --detach locked2 &&
+ # unlocked worktree should not be annotated with "locked"
+ git worktree add --detach unlocked &&
+ git worktree lock locked1 &&
+ test_when_finished "git worktree unlock locked1" &&
+ git worktree lock locked2 --reason "with reason" &&
+ test_when_finished "git worktree unlock locked2" &&
+ git worktree list --porcelain >out &&
+ grep "^locked" out >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '"list" all worktrees --porcelain with locked reason newline escaped' '
+ test_when_finished "rm -rf locked_lf locked_crlf out actual expect && git worktree prune" &&
+ printf "locked \"locked\\\\r\\\\nreason\"\n" >expect &&
+ printf "locked \"locked\\\\nreason\"\n" >>expect &&
+ git worktree add --detach locked_lf &&
+ git worktree add --detach locked_crlf &&
+ git worktree lock locked_lf --reason "$(printf "locked\nreason")" &&
+ test_when_finished "git worktree unlock locked_lf" &&
+ git worktree lock locked_crlf --reason "$(printf "locked\r\nreason")" &&
+ test_when_finished "git worktree unlock locked_crlf" &&
+ git worktree list --porcelain >out &&
+ grep "^locked" out >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '"list" all worktrees with prunable annotation' '
+ test_when_finished "rm -rf prunable unprunable out && git worktree prune" &&
+ git worktree add --detach prunable &&
+ git worktree add --detach unprunable &&
+ rm -rf prunable &&
+ git worktree list >out &&
+ grep "/prunable *[0-9a-f].* prunable$" out &&
+ ! grep "/unprunable *[0-9a-f].* prunable$"
+'
+
+test_expect_success '"list" all worktrees --porcelain with prunable' '
+ test_when_finished "rm -rf prunable out && git worktree prune" &&
+ git worktree add --detach prunable &&
+ rm -rf prunable &&
+ git worktree list --porcelain >out &&
+ sed -n "/^worktree .*\/prunable$/,/^$/p" <out >only_prunable &&
+ test_grep "^prunable gitdir file points to non-existent location$" only_prunable
+'
+
+test_expect_success '"list" all worktrees with prunable consistent with "prune"' '
+ test_when_finished "rm -rf prunable unprunable out && git worktree prune" &&
+ git worktree add --detach prunable &&
+ git worktree add --detach unprunable &&
+ rm -rf prunable &&
+ git worktree list >out &&
+ grep "/prunable *[0-9a-f].* prunable$" out &&
+ ! grep "/unprunable *[0-9a-f].* unprunable$" out &&
+ git worktree prune --verbose 2>out &&
+ test_grep "^Removing worktrees/prunable" out &&
+ test_grep ! "^Removing worktrees/unprunable" out
+'
+
+test_expect_success '"list" --verbose and --porcelain mutually exclusive' '
+ test_must_fail git worktree list --verbose --porcelain
+'
+
+test_expect_success '"list" all worktrees --verbose with locked' '
+ test_when_finished "rm -rf locked1 locked2 out actual expect && git worktree prune" &&
+ git worktree add locked1 --detach &&
+ git worktree add locked2 --detach &&
+ git worktree lock locked1 &&
+ test_when_finished "git worktree unlock locked1" &&
+ git worktree lock locked2 --reason "with reason" &&
+ test_when_finished "git worktree unlock locked2" &&
+ echo "$(git -C locked2 rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >expect &&
+ printf "\tlocked: with reason\n" >>expect &&
+ git worktree list --verbose >out &&
+ grep "/locked1 *[0-9a-f].* locked$" out &&
+ sed -n "s/ */ /g;/\/locked2 *[0-9a-f].*$/,/locked: .*$/p" <out >actual &&
+ test_cmp actual expect
+'
+
+test_expect_success '"list" all worktrees --verbose with prunable' '
+ test_when_finished "rm -rf prunable out actual expect && git worktree prune" &&
+ git worktree add prunable --detach &&
+ echo "$(git -C prunable rev-parse --show-toplevel) $(git rev-parse --short HEAD) (detached HEAD)" >expect &&
+ printf "\tprunable: gitdir file points to non-existent location\n" >>expect &&
+ rm -rf prunable &&
+ git worktree list --verbose >out &&
+ sed -n "s/ */ /g;/\/prunable *[0-9a-f].*$/,/prunable: .*$/p" <out >actual &&
+ test_cmp actual expect
+'
+
+test_expect_success 'bare repo setup' '
+ git init --bare bare1 &&
+ echo "data" >file1 &&
+ git add file1 &&
+ git commit -m"File1: add data" &&
+ git push bare1 main &&
+ git reset --hard HEAD^
+'
+
+test_expect_success '"list" all worktrees from bare main' '
+ test_when_finished "rm -rf there out actual expect && git -C bare1 worktree prune" &&
+ git -C bare1 worktree add --detach ../there main &&
+ echo "$(pwd)/bare1 (bare)" >expect &&
+ echo "$(git -C there rev-parse --show-toplevel) $(git -C there rev-parse --short HEAD) (detached HEAD)" >>expect &&
+ git -C bare1 worktree list >out &&
+ sed "s/ */ /g" <out >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '"list" all worktrees --porcelain from bare main' '
+ test_when_finished "rm -rf there actual expect && git -C bare1 worktree prune" &&
+ git -C bare1 worktree add --detach ../there main &&
+ echo "worktree $(pwd)/bare1" >expect &&
+ echo "bare" >>expect &&
+ echo >>expect &&
+ echo "worktree $(git -C there rev-parse --show-toplevel)" >>expect &&
+ echo "HEAD $(git -C there rev-parse HEAD)" >>expect &&
+ echo "detached" >>expect &&
+ echo >>expect &&
+ git -C bare1 worktree list --porcelain >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '"list" all worktrees from linked with a bare main' '
+ test_when_finished "rm -rf there out actual expect && git -C bare1 worktree prune" &&
+ git -C bare1 worktree add --detach ../there main &&
+ echo "$(pwd)/bare1 (bare)" >expect &&
+ echo "$(git -C there rev-parse --show-toplevel) $(git -C there rev-parse --short HEAD) (detached HEAD)" >>expect &&
+ git -C there worktree list >out &&
+ sed "s/ */ /g" <out >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'bare repo cleanup' '
+ rm -rf bare1
+'
+
+test_expect_success 'broken main worktree still at the top' '
+ git init broken-main &&
+ (
+ cd broken-main &&
+ test_commit new &&
+ git worktree add linked &&
+ cat >expected <<-EOF &&
+ worktree $(pwd)
+ HEAD $ZERO_OID
+
+ EOF
+ cd linked &&
+ echo "worktree $(pwd)" >expected &&
+ (cd ../ && test-tool ref-store main create-symref HEAD .broken ) &&
+ git worktree list --porcelain >out &&
+ head -n 3 out >actual &&
+ test_cmp ../expected actual &&
+ git worktree list >out &&
+ head -n 1 out >actual.2 &&
+ grep -F "(error)" actual.2
+ )
+'
+
+test_expect_success 'linked worktrees are sorted' '
+ mkdir sorted &&
+ git init sorted/main &&
+ (
+ cd sorted/main &&
+ test_tick &&
+ test_commit new &&
+ git worktree add ../first &&
+ git worktree add ../second &&
+ git worktree list --porcelain >out &&
+ grep ^worktree out >actual
+ ) &&
+ cat >expected <<-EOF &&
+ worktree $(pwd)/sorted/main
+ worktree $(pwd)/sorted/first
+ worktree $(pwd)/sorted/second
+ EOF
+ test_cmp expected sorted/main/actual
+'
+
+test_expect_success 'worktree path when called in .git directory' '
+ git worktree list >list1 &&
+ git -C .git worktree list >list2 &&
+ test_cmp list1 list2
+'
+
+test_done
diff --git a/t/t2403-worktree-move.sh b/t/t2403-worktree-move.sh
new file mode 100755
index 0000000..901342e
--- /dev/null
+++ b/t/t2403-worktree-move.sh
@@ -0,0 +1,250 @@
+#!/bin/sh
+
+test_description='test git worktree move, remove, lock and unlock'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ test_commit init &&
+ git worktree add source &&
+ git worktree list --porcelain >out &&
+ grep "^worktree" out >actual &&
+ cat <<-EOF >expected &&
+ worktree $(pwd)
+ worktree $(pwd)/source
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'lock main worktree' '
+ test_must_fail git worktree lock .
+'
+
+test_expect_success 'lock linked worktree' '
+ git worktree lock --reason hahaha source &&
+ echo hahaha >expected &&
+ test_cmp expected .git/worktrees/source/locked
+'
+
+test_expect_success 'lock linked worktree from another worktree' '
+ rm .git/worktrees/source/locked &&
+ git worktree add elsewhere &&
+ git -C elsewhere worktree lock --reason hahaha ../source &&
+ echo hahaha >expected &&
+ test_cmp expected .git/worktrees/source/locked
+'
+
+test_expect_success 'lock worktree twice' '
+ test_must_fail git worktree lock source &&
+ echo hahaha >expected &&
+ test_cmp expected .git/worktrees/source/locked
+'
+
+test_expect_success 'lock worktree twice (from the locked worktree)' '
+ test_must_fail git -C source worktree lock . &&
+ echo hahaha >expected &&
+ test_cmp expected .git/worktrees/source/locked
+'
+
+test_expect_success 'unlock main worktree' '
+ test_must_fail git worktree unlock .
+'
+
+test_expect_success 'unlock linked worktree' '
+ git worktree unlock source &&
+ test_path_is_missing .git/worktrees/source/locked
+'
+
+test_expect_success 'unlock worktree twice' '
+ test_must_fail git worktree unlock source &&
+ test_path_is_missing .git/worktrees/source/locked
+'
+
+test_expect_success 'move non-worktree' '
+ mkdir abc &&
+ test_must_fail git worktree move abc def
+'
+
+test_expect_success 'move locked worktree' '
+ git worktree lock source &&
+ test_when_finished "git worktree unlock source" &&
+ test_must_fail git worktree move source destination
+'
+
+test_expect_success 'move worktree' '
+ git worktree move source destination &&
+ test_path_is_missing source &&
+ git worktree list --porcelain >out &&
+ grep "^worktree.*/destination$" out &&
+ ! grep "^worktree.*/source$" out &&
+ git -C destination log --format=%s >actual2 &&
+ echo init >expected2 &&
+ test_cmp expected2 actual2
+'
+
+test_expect_success 'move main worktree' '
+ test_must_fail git worktree move . def
+'
+
+test_expect_success 'move worktree to another dir' '
+ mkdir some-dir &&
+ git worktree move destination some-dir &&
+ test_when_finished "git worktree move some-dir/destination destination" &&
+ test_path_is_missing destination &&
+ git worktree list --porcelain >out &&
+ grep "^worktree.*/some-dir/destination$" out &&
+ git -C some-dir/destination log --format=%s >actual2 &&
+ echo init >expected2 &&
+ test_cmp expected2 actual2
+'
+
+test_expect_success 'move locked worktree (force)' '
+ test_when_finished "
+ git worktree unlock flump || :
+ git worktree remove flump || :
+ git worktree unlock ploof || :
+ git worktree remove ploof || :
+ " &&
+ git worktree add --detach flump &&
+ git worktree lock flump &&
+ test_must_fail git worktree move flump ploof" &&
+ test_must_fail git worktree move --force flump ploof" &&
+ git worktree move --force --force flump ploof
+'
+
+test_expect_success 'refuse to move worktree atop existing path' '
+ >bobble &&
+ git worktree add --detach beeble &&
+ test_must_fail git worktree move beeble bobble
+'
+
+test_expect_success 'move atop existing but missing worktree' '
+ git worktree add --detach gnoo &&
+ git worktree add --detach pneu &&
+ rm -fr pneu &&
+ test_must_fail git worktree move gnoo pneu &&
+ git worktree move --force gnoo pneu &&
+
+ git worktree add --detach nu &&
+ git worktree lock nu &&
+ rm -fr nu &&
+ test_must_fail git worktree move pneu nu &&
+ test_must_fail git worktree --force move pneu nu &&
+ git worktree move --force --force pneu nu
+'
+
+test_expect_success 'move a repo with uninitialized submodule' '
+ git init withsub &&
+ (
+ cd withsub &&
+ test_commit initial &&
+ git -c protocol.file.allow=always \
+ submodule add "$PWD"/.git sub &&
+ git commit -m withsub &&
+ git worktree add second HEAD &&
+ git worktree move second third
+ )
+'
+
+test_expect_success 'not move a repo with initialized submodule' '
+ (
+ cd withsub &&
+ git -c protocol.file.allow=always -C third submodule update &&
+ test_must_fail git worktree move third forth
+ )
+'
+
+test_expect_success 'remove main worktree' '
+ test_must_fail git worktree remove .
+'
+
+test_expect_success 'remove locked worktree' '
+ git worktree lock destination &&
+ test_when_finished "git worktree unlock destination" &&
+ test_must_fail git worktree remove destination
+'
+
+test_expect_success 'remove worktree with dirty tracked file' '
+ echo dirty >>destination/init.t &&
+ test_when_finished "git -C destination checkout init.t" &&
+ test_must_fail git worktree remove destination
+'
+
+test_expect_success 'remove worktree with untracked file' '
+ : >destination/untracked &&
+ test_must_fail git worktree remove destination
+'
+
+test_expect_success 'force remove worktree with untracked file' '
+ git worktree remove --force destination &&
+ test_path_is_missing destination
+'
+
+test_expect_success 'remove missing worktree' '
+ git worktree add to-be-gone &&
+ test -d .git/worktrees/to-be-gone &&
+ mv to-be-gone gone &&
+ git worktree remove to-be-gone &&
+ test_path_is_missing .git/worktrees/to-be-gone
+'
+
+test_expect_success 'NOT remove missing-but-locked worktree' '
+ git worktree add gone-but-locked &&
+ git worktree lock gone-but-locked &&
+ test -d .git/worktrees/gone-but-locked &&
+ mv gone-but-locked really-gone-now &&
+ test_must_fail git worktree remove gone-but-locked &&
+ test_path_is_dir .git/worktrees/gone-but-locked
+'
+
+test_expect_success 'proper error when worktree not found' '
+ for i in noodle noodle/bork
+ do
+ test_must_fail git worktree lock $i 2>err &&
+ test_grep "not a working tree" err || return 1
+ done
+'
+
+test_expect_success 'remove locked worktree (force)' '
+ git worktree add --detach gumby &&
+ test_when_finished "git worktree remove gumby || :" &&
+ git worktree lock gumby &&
+ test_when_finished "git worktree unlock gumby || :" &&
+ test_must_fail git worktree remove gumby &&
+ test_must_fail git worktree remove --force gumby &&
+ git worktree remove --force --force gumby
+'
+
+test_expect_success 'remove cleans up .git/worktrees when empty' '
+ git init moog &&
+ (
+ cd moog &&
+ test_commit bim &&
+ git worktree add --detach goom &&
+ test_path_exists .git/worktrees &&
+ git worktree remove goom &&
+ test_path_is_missing .git/worktrees
+ )
+'
+
+test_expect_success 'remove a repo with uninitialized submodule' '
+ test_config_global protocol.file.allow always &&
+ (
+ cd withsub &&
+ git worktree add to-remove HEAD &&
+ git worktree remove to-remove
+ )
+'
+
+test_expect_success 'not remove a repo with initialized submodule' '
+ test_config_global protocol.file.allow always &&
+ (
+ cd withsub &&
+ git worktree add to-remove HEAD &&
+ git -C to-remove submodule update &&
+ test_must_fail git worktree remove to-remove
+ )
+'
+
+test_done
diff --git a/t/t2404-worktree-config.sh b/t/t2404-worktree-config.sh
new file mode 100755
index 0000000..842937b
--- /dev/null
+++ b/t/t2404-worktree-config.sh
@@ -0,0 +1,82 @@
+#!/bin/sh
+
+test_description="config file in multi worktree"
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ test_commit start
+'
+
+test_expect_success 'config --worktree in single worktree' '
+ git config --worktree foo.bar true &&
+ test_cmp_config true foo.bar
+'
+
+test_expect_success 'add worktrees' '
+ git worktree add wt1 &&
+ git worktree add wt2
+'
+
+test_expect_success 'config --worktree without extension' '
+ test_must_fail git config --worktree foo.bar false
+'
+
+test_expect_success 'enable worktreeConfig extension' '
+ git config core.repositoryformatversion 1 &&
+ git config extensions.worktreeConfig true &&
+ test_cmp_config true extensions.worktreeConfig &&
+ test_cmp_config 1 core.repositoryformatversion
+'
+
+test_expect_success 'config is shared as before' '
+ git config this.is shared &&
+ test_cmp_config shared this.is &&
+ test_cmp_config -C wt1 shared this.is &&
+ test_cmp_config -C wt2 shared this.is
+'
+
+test_expect_success 'config is shared (set from another worktree)' '
+ git -C wt1 config that.is also-shared &&
+ test_cmp_config also-shared that.is &&
+ test_cmp_config -C wt1 also-shared that.is &&
+ test_cmp_config -C wt2 also-shared that.is
+'
+
+test_expect_success 'config private to main worktree' '
+ git config --worktree this.is for-main &&
+ test_cmp_config for-main this.is &&
+ test_cmp_config -C wt1 shared this.is &&
+ test_cmp_config -C wt2 shared this.is
+'
+
+test_expect_success 'config private to linked worktree' '
+ git -C wt1 config --worktree this.is for-wt1 &&
+ test_cmp_config for-main this.is &&
+ test_cmp_config -C wt1 for-wt1 this.is &&
+ test_cmp_config -C wt2 shared this.is
+'
+
+test_expect_success 'core.bare no longer for main only' '
+ test_config core.bare true &&
+ test "$(git rev-parse --is-bare-repository)" = true &&
+ test "$(git -C wt1 rev-parse --is-bare-repository)" = true &&
+ test "$(git -C wt2 rev-parse --is-bare-repository)" = true
+'
+
+test_expect_success 'per-worktree core.bare is picked up' '
+ git -C wt1 config --worktree core.bare true &&
+ test "$(git rev-parse --is-bare-repository)" = false &&
+ test "$(git -C wt1 rev-parse --is-bare-repository)" = true &&
+ test "$(git -C wt2 rev-parse --is-bare-repository)" = false
+'
+
+test_expect_success 'config.worktree no longer read without extension' '
+ git config --unset extensions.worktreeConfig &&
+ test_cmp_config shared this.is &&
+ test_cmp_config -C wt1 shared this.is &&
+ test_cmp_config -C wt2 shared this.is
+'
+
+test_done
diff --git a/t/t2405-worktree-submodule.sh b/t/t2405-worktree-submodule.sh
new file mode 100755
index 0000000..11018f3
--- /dev/null
+++ b/t/t2405-worktree-submodule.sh
@@ -0,0 +1,94 @@
+#!/bin/sh
+
+test_description='Combination of submodules and multiple worktrees'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+base_path=$(pwd -P)
+
+test_expect_success 'setup: create origin repos' '
+ git config --global protocol.file.allow always &&
+ git init origin/sub &&
+ test_commit -C origin/sub file1 &&
+ git init origin/main &&
+ test_commit -C origin/main first &&
+ git -C origin/main submodule add ../sub &&
+ git -C origin/main commit -m "add sub" &&
+ test_commit -C origin/sub "file1 updated" file1 file1updated file1updated &&
+ git -C origin/main/sub pull &&
+ git -C origin/main add sub &&
+ git -C origin/main commit -m "sub updated"
+'
+
+test_expect_success 'setup: clone superproject to create main worktree' '
+ git clone --recursive "$base_path/origin/main" main
+'
+
+rev1_hash_main=$(git --git-dir=origin/main/.git show --pretty=format:%h -q "HEAD~1")
+rev1_hash_sub=$(git --git-dir=origin/sub/.git show --pretty=format:%h -q "HEAD~1")
+
+test_expect_success 'add superproject worktree' '
+ git -C main worktree add "$base_path/worktree" "$rev1_hash_main"
+'
+
+test_expect_failure 'submodule is checked out just after worktree add' '
+ git -C worktree diff --submodule main"^!" >out &&
+ grep "file1 updated" out
+'
+
+test_expect_success 'add superproject worktree and initialize submodules' '
+ git -C main worktree add "$base_path/worktree-submodule-update" "$rev1_hash_main" &&
+ git -C worktree-submodule-update submodule update
+'
+
+test_expect_success 'submodule is checked out just after submodule update in linked worktree' '
+ git -C worktree-submodule-update diff --submodule main"^!" >out &&
+ grep "file1 updated" out
+'
+
+test_expect_success 'add superproject worktree and manually add submodule worktree' '
+ git -C main worktree add "$base_path/linked_submodule" "$rev1_hash_main" &&
+ git -C main/sub worktree add "$base_path/linked_submodule/sub" "$rev1_hash_sub"
+'
+
+test_expect_success 'submodule is checked out after manually adding submodule worktree' '
+ git -C linked_submodule diff --submodule main"^!" >out &&
+ grep "file1 updated" out
+'
+
+test_expect_success 'checkout --recurse-submodules uses $GIT_DIR for submodules in a linked worktree' '
+ git -C main worktree add "$base_path/checkout-recurse" --detach &&
+ git -C checkout-recurse submodule update --init &&
+ echo "gitdir: ../../main/.git/worktrees/checkout-recurse/modules/sub" >expect-gitfile &&
+ cat checkout-recurse/sub/.git >actual-gitfile &&
+ test_cmp expect-gitfile actual-gitfile &&
+ git -C main/sub rev-parse HEAD >expect-head-main &&
+ git -C checkout-recurse checkout --recurse-submodules HEAD~1 &&
+ cat checkout-recurse/sub/.git >actual-gitfile &&
+ git -C main/sub rev-parse HEAD >actual-head-main &&
+ test_cmp expect-gitfile actual-gitfile &&
+ test_cmp expect-head-main actual-head-main
+'
+
+test_expect_success 'core.worktree is removed in $GIT_DIR/modules/<name>/config, not in $GIT_COMMON_DIR/modules/<name>/config' '
+ echo "../../../sub" >expect-main &&
+ git -C main/sub config --get core.worktree >actual-main &&
+ test_cmp expect-main actual-main &&
+ echo "../../../../../../checkout-recurse/sub" >expect-linked &&
+ git -C checkout-recurse/sub config --get core.worktree >actual-linked &&
+ test_cmp expect-linked actual-linked &&
+ git -C checkout-recurse checkout --recurse-submodules first &&
+ test_expect_code 1 git -C main/.git/worktrees/checkout-recurse/modules/sub config --get core.worktree >linked-config &&
+ test_must_be_empty linked-config &&
+ git -C main/sub config --get core.worktree >actual-main &&
+ test_cmp expect-main actual-main
+'
+
+test_expect_success 'unsetting core.worktree does not prevent running commands directly against the submodule repository' '
+ git -C main/.git/worktrees/checkout-recurse/modules/sub log
+'
+
+test_done
diff --git a/t/t2406-worktree-repair.sh b/t/t2406-worktree-repair.sh
new file mode 100755
index 0000000..edbf502
--- /dev/null
+++ b/t/t2406-worktree-repair.sh
@@ -0,0 +1,200 @@
+#!/bin/sh
+
+test_description='test git worktree repair'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ test_commit init
+'
+
+test_expect_success 'skip missing worktree' '
+ test_when_finished "git worktree prune" &&
+ git worktree add --detach missing &&
+ rm -rf missing &&
+ git worktree repair >out 2>err &&
+ test_must_be_empty out &&
+ test_must_be_empty err
+'
+
+test_expect_success 'worktree path not directory' '
+ test_when_finished "git worktree prune" &&
+ git worktree add --detach notdir &&
+ rm -rf notdir &&
+ >notdir &&
+ test_must_fail git worktree repair >out 2>err &&
+ test_must_be_empty out &&
+ test_grep "not a directory" err
+'
+
+test_expect_success "don't clobber .git repo" '
+ test_when_finished "rm -rf repo && git worktree prune" &&
+ git worktree add --detach repo &&
+ rm -rf repo &&
+ test_create_repo repo &&
+ test_must_fail git worktree repair >out 2>err &&
+ test_must_be_empty out &&
+ test_grep ".git is not a file" err
+'
+
+test_corrupt_gitfile () {
+ butcher=$1 &&
+ problem=$2 &&
+ repairdir=${3:-.} &&
+ test_when_finished 'rm -rf corrupt && git worktree prune' &&
+ git worktree add --detach corrupt &&
+ git -C corrupt rev-parse --absolute-git-dir >expect &&
+ eval "$butcher" &&
+ git -C "$repairdir" worktree repair 2>err &&
+ test_grep "$problem" err &&
+ git -C corrupt rev-parse --absolute-git-dir >actual &&
+ test_cmp expect actual
+}
+
+test_expect_success 'repair missing .git file' '
+ test_corrupt_gitfile "rm -f corrupt/.git" ".git file broken"
+'
+
+test_expect_success 'repair bogus .git file' '
+ test_corrupt_gitfile "echo \"gitdir: /nowhere\" >corrupt/.git" \
+ ".git file broken"
+'
+
+test_expect_success 'repair incorrect .git file' '
+ test_when_finished "rm -rf other && git worktree prune" &&
+ test_create_repo other &&
+ other=$(git -C other rev-parse --absolute-git-dir) &&
+ test_corrupt_gitfile "echo \"gitdir: $other\" >corrupt/.git" \
+ ".git file incorrect"
+'
+
+test_expect_success 'repair .git file from main/.git' '
+ test_corrupt_gitfile "rm -f corrupt/.git" ".git file broken" .git
+'
+
+test_expect_success 'repair .git file from linked worktree' '
+ test_when_finished "rm -rf other && git worktree prune" &&
+ git worktree add --detach other &&
+ test_corrupt_gitfile "rm -f corrupt/.git" ".git file broken" other
+'
+
+test_expect_success 'repair .git file from bare.git' '
+ test_when_finished "rm -rf bare.git corrupt && git worktree prune" &&
+ git clone --bare . bare.git &&
+ git -C bare.git worktree add --detach ../corrupt &&
+ git -C corrupt rev-parse --absolute-git-dir >expect &&
+ rm -f corrupt/.git &&
+ git -C bare.git worktree repair &&
+ git -C corrupt rev-parse --absolute-git-dir >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'invalid worktree path' '
+ test_must_fail git worktree repair /notvalid >out 2>err &&
+ test_must_be_empty out &&
+ test_grep "not a valid path" err
+'
+
+test_expect_success 'repo not found; .git not file' '
+ test_when_finished "rm -rf not-a-worktree" &&
+ test_create_repo not-a-worktree &&
+ test_must_fail git worktree repair not-a-worktree >out 2>err &&
+ test_must_be_empty out &&
+ test_grep ".git is not a file" err
+'
+
+test_expect_success 'repo not found; .git not referencing repo' '
+ test_when_finished "rm -rf side not-a-repo && git worktree prune" &&
+ git worktree add --detach side &&
+ sed s,\.git/worktrees/side$,not-a-repo, side/.git >side/.newgit &&
+ mv side/.newgit side/.git &&
+ mkdir not-a-repo &&
+ test_must_fail git worktree repair side 2>err &&
+ test_grep ".git file does not reference a repository" err
+'
+
+test_expect_success 'repo not found; .git file broken' '
+ test_when_finished "rm -rf orig moved && git worktree prune" &&
+ git worktree add --detach orig &&
+ echo /invalid >orig/.git &&
+ mv orig moved &&
+ test_must_fail git worktree repair moved >out 2>err &&
+ test_must_be_empty out &&
+ test_grep ".git file broken" err
+'
+
+test_expect_success 'repair broken gitdir' '
+ test_when_finished "rm -rf orig moved && git worktree prune" &&
+ git worktree add --detach orig &&
+ sed s,orig/\.git$,moved/.git, .git/worktrees/orig/gitdir >expect &&
+ rm .git/worktrees/orig/gitdir &&
+ mv orig moved &&
+ git worktree repair moved 2>err &&
+ test_cmp expect .git/worktrees/orig/gitdir &&
+ test_grep "gitdir unreadable" err
+'
+
+test_expect_success 'repair incorrect gitdir' '
+ test_when_finished "rm -rf orig moved && git worktree prune" &&
+ git worktree add --detach orig &&
+ sed s,orig/\.git$,moved/.git, .git/worktrees/orig/gitdir >expect &&
+ mv orig moved &&
+ git worktree repair moved 2>err &&
+ test_cmp expect .git/worktrees/orig/gitdir &&
+ test_grep "gitdir incorrect" err
+'
+
+test_expect_success 'repair gitdir (implicit) from linked worktree' '
+ test_when_finished "rm -rf orig moved && git worktree prune" &&
+ git worktree add --detach orig &&
+ sed s,orig/\.git$,moved/.git, .git/worktrees/orig/gitdir >expect &&
+ mv orig moved &&
+ git -C moved worktree repair 2>err &&
+ test_cmp expect .git/worktrees/orig/gitdir &&
+ test_grep "gitdir incorrect" err
+'
+
+test_expect_success 'unable to repair gitdir (implicit) from main worktree' '
+ test_when_finished "rm -rf orig moved && git worktree prune" &&
+ git worktree add --detach orig &&
+ cat .git/worktrees/orig/gitdir >expect &&
+ mv orig moved &&
+ git worktree repair 2>err &&
+ test_cmp expect .git/worktrees/orig/gitdir &&
+ test_must_be_empty err
+'
+
+test_expect_success 'repair multiple gitdir files' '
+ test_when_finished "rm -rf orig1 orig2 moved1 moved2 &&
+ git worktree prune" &&
+ git worktree add --detach orig1 &&
+ git worktree add --detach orig2 &&
+ sed s,orig1/\.git$,moved1/.git, .git/worktrees/orig1/gitdir >expect1 &&
+ sed s,orig2/\.git$,moved2/.git, .git/worktrees/orig2/gitdir >expect2 &&
+ mv orig1 moved1 &&
+ mv orig2 moved2 &&
+ git worktree repair moved1 moved2 2>err &&
+ test_cmp expect1 .git/worktrees/orig1/gitdir &&
+ test_cmp expect2 .git/worktrees/orig2/gitdir &&
+ test_grep "gitdir incorrect:.*orig1/gitdir$" err &&
+ test_grep "gitdir incorrect:.*orig2/gitdir$" err
+'
+
+test_expect_success 'repair moved main and linked worktrees' '
+ test_when_finished "rm -rf main side mainmoved sidemoved" &&
+ test_create_repo main &&
+ test_commit -C main init &&
+ git -C main worktree add --detach ../side &&
+ sed "s,side/\.git$,sidemoved/.git," \
+ main/.git/worktrees/side/gitdir >expect-gitdir &&
+ sed "s,main/.git/worktrees/side$,mainmoved/.git/worktrees/side," \
+ side/.git >expect-gitfile &&
+ mv main mainmoved &&
+ mv side sidemoved &&
+ git -C mainmoved worktree repair ../sidemoved &&
+ test_cmp expect-gitdir mainmoved/.git/worktrees/side/gitdir &&
+ test_cmp expect-gitfile sidemoved/.git
+'
+
+test_done
diff --git a/t/t2407-worktree-heads.sh b/t/t2407-worktree-heads.sh
new file mode 100755
index 0000000..f6835c9
--- /dev/null
+++ b/t/t2407-worktree-heads.sh
@@ -0,0 +1,180 @@
+#!/bin/sh
+
+test_description='test operations trying to overwrite refs at worktree HEAD'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ test_commit init &&
+
+ for i in 1 2 3 4
+ do
+ git checkout -b conflict-$i &&
+ echo "not I" >$i.t &&
+ git add $i.t &&
+ git commit -m "will conflict" &&
+
+ git checkout - &&
+ test_commit $i &&
+ git branch wt-$i &&
+ git branch fake-$i &&
+ git worktree add wt-$i wt-$i || return 1
+ done &&
+
+ # Create a server that updates each branch by one commit
+ git init server &&
+ test_commit -C server initial &&
+ git remote add server ./server &&
+ for i in 1 2 3 4
+ do
+ git -C server checkout -b wt-$i &&
+ test_commit -C server A-$i || return 1
+ done &&
+ for i in 1 2
+ do
+ git -C server checkout -b fake-$i &&
+ test_commit -C server f-$i || return 1
+ done
+'
+
+test_expect_success 'refuse to overwrite: checked out in worktree' '
+ for i in 1 2 3 4
+ do
+ test_must_fail git branch -f wt-$i HEAD 2>err &&
+ grep "cannot force update the branch" err &&
+
+ test_must_fail git branch -D wt-$i 2>err &&
+ grep "cannot delete branch" err || return 1
+ done
+'
+
+test_expect_success !SANITIZE_LEAK 'refuse to overwrite: worktree in bisect' '
+ test_when_finished git -C wt-4 bisect reset &&
+
+ # Set up a bisect so HEAD no longer points to wt-4.
+ git -C wt-4 bisect start &&
+ git -C wt-4 bisect bad wt-4 &&
+ git -C wt-4 bisect good wt-1 &&
+
+ test_must_fail git branch -f wt-4 HEAD 2>err &&
+ grep "cannot force update the branch '\''wt-4'\'' used by worktree at.*wt-4" err
+'
+
+test_expect_success !SANITIZE_LEAK 'refuse to overwrite: worktree in rebase (apply)' '
+ test_when_finished git -C wt-2 rebase --abort &&
+
+ # This will fail part-way through due to a conflict.
+ test_must_fail git -C wt-2 rebase --apply conflict-2 &&
+
+ test_must_fail git branch -f wt-2 HEAD 2>err &&
+ grep "cannot force update the branch '\''wt-2'\'' used by worktree at.*wt-2" err
+'
+
+test_expect_success !SANITIZE_LEAK 'refuse to overwrite: worktree in rebase (merge)' '
+ test_when_finished git -C wt-2 rebase --abort &&
+
+ # This will fail part-way through due to a conflict.
+ test_must_fail git -C wt-2 rebase conflict-2 &&
+
+ test_must_fail git branch -f wt-2 HEAD 2>err &&
+ grep "cannot force update the branch '\''wt-2'\'' used by worktree at.*wt-2" err
+'
+
+test_expect_success !SANITIZE_LEAK 'refuse to overwrite: worktree in rebase with --update-refs' '
+ test_when_finished git -C wt-3 rebase --abort &&
+
+ git branch -f can-be-updated wt-3 &&
+ test_must_fail git -C wt-3 rebase --update-refs conflict-3 &&
+
+ for i in 3 4
+ do
+ test_must_fail git branch -f can-be-updated HEAD 2>err &&
+ grep "cannot force update the branch '\''can-be-updated'\'' used by worktree at.*wt-3" err ||
+ return 1
+ done
+'
+
+test_expect_success !SANITIZE_LEAK 'refuse to fetch over ref: checked out' '
+ test_must_fail git fetch server +refs/heads/wt-3:refs/heads/wt-3 2>err &&
+ grep "refusing to fetch into branch '\''refs/heads/wt-3'\''" err &&
+
+ # General fetch into refs/heads/ will fail on first ref,
+ # so use a generic error message check.
+ test_must_fail git fetch server +refs/heads/*:refs/heads/* 2>err &&
+ grep "refusing to fetch into branch" err
+'
+
+test_expect_success !SANITIZE_LEAK 'refuse to fetch over ref: worktree in bisect' '
+ test_when_finished git -C wt-4 bisect reset &&
+
+ # Set up a bisect so HEAD no longer points to wt-4.
+ git -C wt-4 bisect start &&
+ git -C wt-4 bisect bad wt-4 &&
+ git -C wt-4 bisect good wt-1 &&
+
+ test_must_fail git fetch server +refs/heads/wt-4:refs/heads/wt-4 2>err &&
+ grep "refusing to fetch into branch" err
+'
+
+test_expect_success !SANITIZE_LEAK 'refuse to fetch over ref: worktree in rebase' '
+ test_when_finished git -C wt-3 rebase --abort &&
+
+ # This will fail part-way through due to a conflict.
+ test_must_fail git -C wt-3 rebase conflict-3 &&
+
+ test_must_fail git fetch server +refs/heads/wt-3:refs/heads/wt-3 2>err &&
+ grep "refusing to fetch into branch" err
+'
+
+test_expect_success 'refuse to overwrite when in error states' '
+ test_when_finished rm -rf .git/worktrees/wt-*/rebase-merge &&
+ test_when_finished rm -rf .git/worktrees/wt-*/BISECT_* &&
+
+ # Both branches are currently under rebase.
+ mkdir -p .git/worktrees/wt-3/rebase-merge &&
+ touch .git/worktrees/wt-3/rebase-merge/interactive &&
+ echo refs/heads/fake-1 >.git/worktrees/wt-3/rebase-merge/head-name &&
+ echo refs/heads/fake-2 >.git/worktrees/wt-3/rebase-merge/onto &&
+ mkdir -p .git/worktrees/wt-4/rebase-merge &&
+ touch .git/worktrees/wt-4/rebase-merge/interactive &&
+ echo refs/heads/fake-2 >.git/worktrees/wt-4/rebase-merge/head-name &&
+ echo refs/heads/fake-1 >.git/worktrees/wt-4/rebase-merge/onto &&
+
+ # Both branches are currently under bisect.
+ touch .git/worktrees/wt-4/BISECT_LOG &&
+ echo refs/heads/fake-2 >.git/worktrees/wt-4/BISECT_START &&
+ touch .git/worktrees/wt-1/BISECT_LOG &&
+ echo refs/heads/fake-1 >.git/worktrees/wt-1/BISECT_START &&
+
+ for i in 1 2
+ do
+ test_must_fail git branch -f fake-$i HEAD 2>err &&
+ grep "cannot force update the branch '\''fake-$i'\'' used by worktree at" err ||
+ return 1
+ done
+'
+
+. "$TEST_DIRECTORY"/lib-rebase.sh
+
+test_expect_success !SANITIZE_LEAK 'refuse to overwrite during rebase with --update-refs' '
+ git commit --fixup HEAD~2 --allow-empty &&
+ (
+ set_cat_todo_editor &&
+ test_must_fail git rebase -i --update-refs HEAD~3 >todo &&
+ ! grep "update-refs" todo
+ ) &&
+ git branch -f allow-update HEAD~2 &&
+ (
+ set_cat_todo_editor &&
+ test_must_fail git rebase -i --update-refs HEAD~3 >todo &&
+ grep "update-ref refs/heads/allow-update" todo
+ )
+'
+
+# This must be the last test in this file
+test_expect_success '$EDITOR and friends are unchanged' '
+ test_editor_unchanged
+'
+
+test_done
diff --git a/t/t2500-untracked-overwriting.sh b/t/t2500-untracked-overwriting.sh
new file mode 100755
index 0000000..5c0bf4d
--- /dev/null
+++ b/t/t2500-untracked-overwriting.sh
@@ -0,0 +1,244 @@
+#!/bin/sh
+
+test_description='Test handling of overwriting untracked files'
+
+. ./test-lib.sh
+
+test_setup_reset () {
+ git init reset_$1 &&
+ (
+ cd reset_$1 &&
+ test_commit init &&
+
+ git branch stable &&
+ git branch work &&
+
+ git checkout work &&
+ test_commit foo &&
+
+ git checkout stable
+ )
+}
+
+test_expect_success 'reset --hard will nuke untracked files/dirs' '
+ test_setup_reset hard &&
+ (
+ cd reset_hard &&
+ git ls-tree -r stable &&
+ git log --all --name-status --oneline &&
+ git ls-tree -r work &&
+
+ mkdir foo.t &&
+ echo precious >foo.t/file &&
+ echo foo >expect &&
+
+ git reset --hard work &&
+
+ # check that untracked directory foo.t/ was nuked
+ test_path_is_file foo.t &&
+ test_cmp expect foo.t
+ )
+'
+
+test_expect_success 'reset --merge will preserve untracked files/dirs' '
+ test_setup_reset merge &&
+ (
+ cd reset_merge &&
+
+ mkdir foo.t &&
+ echo precious >foo.t/file &&
+ cp foo.t/file expect &&
+
+ test_must_fail git reset --merge work 2>error &&
+ test_cmp expect foo.t/file &&
+ grep "Updating .foo.t. would lose untracked files" error
+ )
+'
+
+test_expect_success 'reset --keep will preserve untracked files/dirs' '
+ test_setup_reset keep &&
+ (
+ cd reset_keep &&
+
+ mkdir foo.t &&
+ echo precious >foo.t/file &&
+ cp foo.t/file expect &&
+
+ test_must_fail git reset --merge work 2>error &&
+ test_cmp expect foo.t/file &&
+ grep "Updating.*foo.t.*would lose untracked files" error
+ )
+'
+
+test_setup_checkout_m () {
+ git init checkout &&
+ (
+ cd checkout &&
+ test_commit init &&
+
+ test_write_lines file has some >filler &&
+ git add filler &&
+ git commit -m filler &&
+
+ git branch stable &&
+
+ git switch -c work &&
+ echo stuff >notes.txt &&
+ test_write_lines file has some words >filler &&
+ git add notes.txt filler &&
+ git commit -m filler &&
+
+ git checkout stable
+ )
+}
+
+test_expect_success 'checkout -m does not nuke untracked file' '
+ test_setup_checkout_m &&
+ (
+ cd checkout &&
+
+ # Tweak filler
+ test_write_lines this file has some >filler &&
+ # Make an untracked file, save its contents in "expect"
+ echo precious >notes.txt &&
+ cp notes.txt expect &&
+
+ test_must_fail git checkout -m work &&
+ test_cmp expect notes.txt
+ )
+'
+
+test_setup_sequencing () {
+ git init sequencing_$1 &&
+ (
+ cd sequencing_$1 &&
+ test_commit init &&
+
+ test_write_lines this file has some words >filler &&
+ git add filler &&
+ git commit -m filler &&
+
+ mkdir -p foo/bar &&
+ test_commit foo/bar/baz &&
+
+ git branch simple &&
+ git branch fooey &&
+
+ git checkout fooey &&
+ git rm foo/bar/baz.t &&
+ echo stuff >>filler &&
+ git add -u &&
+ git commit -m "changes" &&
+
+ git checkout simple &&
+ echo items >>filler &&
+ echo newstuff >>newfile &&
+ git add filler newfile &&
+ git commit -m another
+ )
+}
+
+test_expect_success 'git rebase --abort and untracked files' '
+ test_setup_sequencing rebase_abort_and_untracked &&
+ (
+ cd sequencing_rebase_abort_and_untracked &&
+ git checkout fooey &&
+ test_must_fail git rebase simple &&
+
+ cat init.t &&
+ git rm init.t &&
+ echo precious >init.t &&
+ cp init.t expect &&
+ git status --porcelain &&
+ test_must_fail git rebase --abort &&
+ test_cmp expect init.t
+ )
+'
+
+test_expect_success 'git rebase fast forwarding and untracked files' '
+ test_setup_sequencing rebase_fast_forward_and_untracked &&
+ (
+ cd sequencing_rebase_fast_forward_and_untracked &&
+ git checkout init &&
+ echo precious >filler &&
+ cp filler expect &&
+ test_must_fail git rebase init simple &&
+ test_cmp expect filler
+ )
+'
+
+test_expect_failure 'git rebase --autostash and untracked files' '
+ test_setup_sequencing rebase_autostash_and_untracked &&
+ (
+ cd sequencing_rebase_autostash_and_untracked &&
+ git checkout simple &&
+ git rm filler &&
+ mkdir filler &&
+ echo precious >filler/file &&
+ cp filler/file expect &&
+ git rebase --autostash init &&
+ test_path_is_file filler/file
+ )
+'
+
+test_expect_failure 'git stash and untracked files' '
+ test_setup_sequencing stash_and_untracked_files &&
+ (
+ cd sequencing_stash_and_untracked_files &&
+ git checkout simple &&
+ git rm filler &&
+ mkdir filler &&
+ echo precious >filler/file &&
+ cp filler/file expect &&
+ git status --porcelain &&
+ git stash push &&
+ git status --porcelain &&
+ test_path_is_file filler/file
+ )
+'
+
+test_expect_success 'git am --abort and untracked dir vs. unmerged file' '
+ test_setup_sequencing am_abort_and_untracked &&
+ (
+ cd sequencing_am_abort_and_untracked &&
+ git format-patch -1 --stdout fooey >changes.mbox &&
+ test_must_fail git am --3way changes.mbox &&
+
+ # Delete the conflicted file; we will stage and commit it later
+ rm filler &&
+
+ # Put an unrelated untracked directory there
+ mkdir filler &&
+ echo foo >filler/file1 &&
+ echo bar >filler/file2 &&
+
+ test_must_fail git am --abort 2>errors &&
+ test_path_is_dir filler &&
+ grep "Updating .filler. would lose untracked files in it" errors
+ )
+'
+
+test_expect_success 'git am --skip and untracked dir vs deleted file' '
+ test_setup_sequencing am_skip_and_untracked &&
+ (
+ cd sequencing_am_skip_and_untracked &&
+ git checkout fooey &&
+ git format-patch -1 --stdout simple >changes.mbox &&
+ test_must_fail git am --3way changes.mbox &&
+
+ # Delete newfile
+ rm newfile &&
+
+ # Put an unrelated untracked directory there
+ mkdir newfile &&
+ echo foo >newfile/file1 &&
+ echo bar >newfile/file2 &&
+
+ # Change our mind about resolutions, just skip this patch
+ test_must_fail git am --skip 2>errors &&
+ test_path_is_dir newfile &&
+ grep "Updating .newfile. would lose untracked files in it" errors
+ )
+'
+
+test_done
diff --git a/t/t2501-cwd-empty.sh b/t/t2501-cwd-empty.sh
new file mode 100755
index 0000000..f6d8d7d
--- /dev/null
+++ b/t/t2501-cwd-empty.sh
@@ -0,0 +1,277 @@
+#!/bin/sh
+
+test_description='Test handling of the current working directory becoming empty'
+
+. ./test-lib.sh
+
+test_expect_success setup '
+ test_commit init &&
+
+ git branch fd_conflict &&
+
+ mkdir -p foo/bar &&
+ test_commit foo/bar/baz &&
+
+ git revert HEAD &&
+ git tag reverted &&
+
+ git checkout fd_conflict &&
+ mkdir dirORfile &&
+ test_commit dirORfile/foo &&
+
+ git rm -r dirORfile &&
+ echo not-a-directory >dirORfile &&
+ git add dirORfile &&
+ git commit -m dirORfile &&
+
+ git switch -c df_conflict HEAD~1 &&
+ test_commit random_file &&
+
+ git switch -c undo_fd_conflict fd_conflict &&
+ git revert HEAD
+'
+
+test_incidental_dir_removal () {
+ test_when_finished "git reset --hard" &&
+
+ git checkout foo/bar/baz^{commit} &&
+ test_path_is_dir foo/bar &&
+
+ (
+ cd foo &&
+ "$@" &&
+
+ # Make sure foo still exists, and commands needing it work
+ test-tool getcwd &&
+ git status --porcelain
+ ) &&
+ test_path_is_missing foo/bar/baz &&
+ test_path_is_missing foo/bar &&
+
+ test_path_is_dir foo
+}
+
+test_required_dir_removal () {
+ git checkout df_conflict^{commit} &&
+ test_when_finished "git clean -fdx" &&
+
+ (
+ cd dirORfile &&
+
+ # Ensure command refuses to run
+ test_must_fail "$@" 2>../error &&
+ grep "Refusing to remove.*current working directory" ../error &&
+
+ # ...and that the index and working tree are left clean
+ git diff --exit-code HEAD &&
+
+ # Ensure that getcwd and git status do not error out (which
+ # they might if the current working directory had been removed)
+ test-tool getcwd &&
+ git status --porcelain
+ ) &&
+
+ test_path_is_dir dirORfile
+}
+
+test_expect_success 'checkout does not clean cwd incidentally' '
+ test_incidental_dir_removal git checkout init
+'
+
+test_expect_success 'checkout fails if cwd needs to be removed' '
+ test_required_dir_removal git checkout fd_conflict
+'
+
+test_expect_success 'reset --hard does not clean cwd incidentally' '
+ test_incidental_dir_removal git reset --hard init
+'
+
+test_expect_success 'reset --hard fails if cwd needs to be removed' '
+ test_required_dir_removal git reset --hard fd_conflict
+'
+
+test_expect_success 'merge does not clean cwd incidentally' '
+ test_incidental_dir_removal git merge reverted
+'
+
+# This file uses some simple merges where
+# Base: 'dirORfile/' exists
+# Side1: random other file changed
+# Side2: 'dirORfile/' removed, 'dirORfile' added
+# this should resolve cleanly, but merge-recursive throws merge conflicts
+# because it's dumb. Add a special test for checking merge-recursive (and
+# merge-ort), then after this just hard require ort for all remaining tests.
+#
+test_expect_success 'merge fails if cwd needs to be removed; recursive friendly' '
+ git checkout foo/bar/baz &&
+ test_when_finished "git clean -fdx" &&
+
+ mkdir dirORfile &&
+ (
+ cd dirORfile &&
+
+ test_must_fail git merge fd_conflict 2>../error
+ ) &&
+
+ test_path_is_dir dirORfile &&
+ grep "Refusing to remove the current working directory" error
+'
+
+GIT_TEST_MERGE_ALGORITHM=ort
+
+test_expect_success 'merge fails if cwd needs to be removed' '
+ test_required_dir_removal git merge fd_conflict
+'
+
+test_expect_success 'cherry-pick does not clean cwd incidentally' '
+ test_incidental_dir_removal git cherry-pick reverted
+'
+
+test_expect_success 'cherry-pick fails if cwd needs to be removed' '
+ test_required_dir_removal git cherry-pick fd_conflict
+'
+
+test_expect_success 'rebase does not clean cwd incidentally' '
+ test_incidental_dir_removal git rebase reverted
+'
+
+test_expect_success 'rebase fails if cwd needs to be removed' '
+ test_required_dir_removal git rebase fd_conflict
+'
+
+test_expect_success 'revert does not clean cwd incidentally' '
+ test_incidental_dir_removal git revert HEAD
+'
+
+test_expect_success 'revert fails if cwd needs to be removed' '
+ test_required_dir_removal git revert undo_fd_conflict
+'
+
+test_expect_success 'rm does not clean cwd incidentally' '
+ test_incidental_dir_removal git rm bar/baz.t
+'
+
+test_expect_success 'apply does not remove cwd incidentally' '
+ git diff HEAD HEAD~1 >patch &&
+ test_incidental_dir_removal git apply ../patch
+'
+
+test_incidental_untracked_dir_removal () {
+ test_when_finished "git reset --hard" &&
+
+ git checkout foo/bar/baz^{commit} &&
+ mkdir -p untracked &&
+ mkdir empty
+ >untracked/random &&
+
+ (
+ cd untracked &&
+ "$@" &&
+
+ # Make sure untracked still exists, and commands needing it work
+ test-tool getcwd &&
+ git status --porcelain
+ ) &&
+ test_path_is_missing empty &&
+ test_path_is_missing untracked/random &&
+
+ test_path_is_dir untracked
+}
+
+test_expect_success 'clean does not remove cwd incidentally' '
+ test_incidental_untracked_dir_removal \
+ git -C .. clean -fd -e warnings . >warnings &&
+ grep "Refusing to remove current working directory" warnings
+'
+
+test_expect_success 'stash does not remove cwd incidentally' '
+ test_incidental_untracked_dir_removal \
+ git stash --include-untracked
+'
+
+test_expect_success '`rm -rf dir` only removes a subset of dir' '
+ test_when_finished "rm -rf a/" &&
+
+ mkdir -p a/b/c &&
+ >a/b/c/untracked &&
+ >a/b/c/tracked &&
+ git add a/b/c/tracked &&
+
+ (
+ cd a/b &&
+ git rm -rf ../b
+ ) &&
+
+ test_path_is_dir a/b &&
+ test_path_is_missing a/b/c/tracked &&
+ test_path_is_file a/b/c/untracked
+'
+
+test_expect_success '`rm -rf dir` even with only tracked files will remove something else' '
+ test_when_finished "rm -rf a/" &&
+
+ mkdir -p a/b/c &&
+ >a/b/c/tracked &&
+ git add a/b/c/tracked &&
+
+ (
+ cd a/b &&
+ git rm -rf ../b
+ ) &&
+
+ test_path_is_missing a/b/c/tracked &&
+ test_path_is_missing a/b/c &&
+ test_path_is_dir a/b
+'
+
+test_expect_success 'git version continues working from a deleted dir' '
+ mkdir tmp &&
+ (
+ cd tmp &&
+ rm -rf ../tmp &&
+ git version
+ )
+'
+
+test_submodule_removal () {
+ path_status=$1 &&
+ shift &&
+
+ test_status=
+ test "$path_status" = dir && test_status=test_must_fail
+
+ test_when_finished "git reset --hard HEAD~1" &&
+ test_when_finished "rm -rf .git/modules/my_submodule" &&
+
+ git checkout foo/bar/baz &&
+
+ git init my_submodule &&
+ touch my_submodule/file &&
+ git -C my_submodule add file &&
+ git -C my_submodule commit -m "initial commit" &&
+ git submodule add ./my_submodule &&
+ git commit -m "Add the submodule" &&
+
+ (
+ cd my_submodule &&
+ $test_status "$@"
+ ) &&
+
+ test_path_is_${path_status} my_submodule
+}
+
+test_expect_success 'rm -r with -C leaves submodule if cwd inside' '
+ test_submodule_removal dir git -C .. rm -r my_submodule/
+'
+
+test_expect_success 'rm -r leaves submodule if cwd inside' '
+ test_submodule_removal dir \
+ git --git-dir=../.git --work-tree=.. rm -r ../my_submodule/
+'
+
+test_expect_success 'rm -rf removes submodule even if cwd inside' '
+ test_submodule_removal missing \
+ git --git-dir=../.git --work-tree=.. rm -rf ../my_submodule/
+'
+
+test_done
diff --git a/t/t3000-ls-files-others.sh b/t/t3000-ls-files-others.sh
new file mode 100755
index 0000000..11af455
--- /dev/null
+++ b/t/t3000-ls-files-others.sh
@@ -0,0 +1,233 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+test_description='basic tests for ls-files --others
+
+This test runs git ls-files --others with the following on the
+filesystem.
+
+ path0 - a file
+ path1 - a symlink
+ path2/file2 - a file in a directory
+ path3-junk - a file to confuse things
+ path3/file3 - a file in a directory
+ path4 - an empty directory
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup ' '
+ date >path0 &&
+ if test_have_prereq SYMLINKS
+ then
+ ln -s xyzzy path1
+ else
+ date >path1
+ fi &&
+ mkdir path2 path3 path4 &&
+ date >path2/file2 &&
+ date >path2-junk &&
+ date >path3/file3 &&
+ date >path3-junk &&
+ git update-index --add path3-junk path3/file3
+'
+
+test_expect_success 'setup: expected output' '
+ cat >expected1 <<-\EOF &&
+ expected1
+ expected2
+ expected3
+ output
+ path0
+ path1
+ path2-junk
+ path2/file2
+ EOF
+
+ sed -e "s|path2/file2|path2/|" <expected1 >expected2 &&
+ cp expected2 expected3 &&
+ echo path4/ >>expected2
+'
+
+test_expect_success 'ls-files --others' '
+ git ls-files --others >output &&
+ test_cmp expected1 output
+'
+
+test_expect_success 'ls-files --others --directory' '
+ git ls-files --others --directory >output &&
+ test_cmp expected2 output
+'
+
+test_expect_success '--no-empty-directory hides empty directory' '
+ git ls-files --others --directory --no-empty-directory >output &&
+ test_cmp expected3 output
+'
+
+test_expect_success 'ls-files --others handles non-submodule .git' '
+ mkdir not-a-submodule &&
+ echo foo >not-a-submodule/.git &&
+ git ls-files -o >output &&
+ test_cmp expected1 output
+'
+
+test_expect_success SYMLINKS 'ls-files --others with symlinked submodule' '
+ git init super &&
+ git init sub &&
+ (
+ cd sub &&
+ >a &&
+ git add a &&
+ git commit -m sub &&
+ git pack-refs --all
+ ) &&
+ (
+ cd super &&
+ "$SHELL_PATH" "$TEST_DIRECTORY/../contrib/workdir/git-new-workdir" ../sub sub &&
+ git ls-files --others --exclude-standard >../actual
+ ) &&
+ echo sub/ >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'setup nested pathspec search' '
+ test_create_repo nested &&
+ (
+ cd nested &&
+
+ mkdir -p partially_tracked/untracked_dir &&
+ > partially_tracked/content &&
+ > partially_tracked/untracked_dir/file &&
+
+ mkdir -p untracked/deep &&
+ > untracked/deep/path &&
+ > untracked/deep/foo.c &&
+
+ git add partially_tracked/content
+ )
+'
+
+test_expect_success 'ls-files -o --directory with single deep dir pathspec' '
+ (
+ cd nested &&
+
+ git ls-files -o --directory untracked/deep/ >actual &&
+
+ cat <<-EOF >expect &&
+ untracked/deep/
+ EOF
+
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'ls-files -o --directory with multiple dir pathspecs' '
+ (
+ cd nested &&
+
+ git ls-files -o --directory partially_tracked/ untracked/ >actual &&
+
+ cat <<-EOF >expect &&
+ partially_tracked/untracked_dir/
+ untracked/
+ EOF
+
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'ls-files -o --directory with mix dir/file pathspecs' '
+ (
+ cd nested &&
+
+ git ls-files -o --directory partially_tracked/ untracked/deep/path >actual &&
+
+ cat <<-EOF >expect &&
+ partially_tracked/untracked_dir/
+ untracked/deep/path
+ EOF
+
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'ls-files -o --directory with glob filetype match' '
+ (
+ cd nested &&
+
+ # globs kinda defeat --directory, but only for that pathspec
+ git ls-files --others --directory partially_tracked "untracked/*.c" >actual &&
+
+ cat <<-EOF >expect &&
+ partially_tracked/untracked_dir/
+ untracked/deep/foo.c
+ EOF
+
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'ls-files -o --directory with mix of tracked states' '
+ (
+ cd nested &&
+
+ # globs kinda defeat --directory, but only for that pathspec
+ git ls-files --others --directory partially_tracked/ "untracked/?*" >actual &&
+
+ cat <<-EOF >expect &&
+ partially_tracked/untracked_dir/
+ untracked/deep/
+ EOF
+
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'ls-files -o --directory with glob filetype match only' '
+ (
+ cd nested &&
+
+ git ls-files --others --directory "untracked/*.c" >actual &&
+
+ cat <<-EOF >expect &&
+ untracked/deep/foo.c
+ EOF
+
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'ls-files -o --directory to get immediate paths under one dir only' '
+ (
+ cd nested &&
+
+ git ls-files --others --directory "untracked/?*" >actual &&
+
+ cat <<-EOF >expect &&
+ untracked/deep/
+ EOF
+
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'ls-files -o avoids listing untracked non-matching gitdir' '
+ test_when_finished "rm -rf nested/untracked/deep/empty" &&
+ (
+ cd nested &&
+
+ git init untracked/deep/empty &&
+ git ls-files --others "untracked/*.c" >actual &&
+
+ cat <<-EOF >expect &&
+ untracked/deep/foo.c
+ EOF
+
+ test_cmp expect actual
+ )
+'
+
+test_done
diff --git a/t/t3001-ls-files-others-exclude.sh b/t/t3001-ls-files-others-exclude.sh
new file mode 100755
index 0000000..1ed0aa9
--- /dev/null
+++ b/t/t3001-ls-files-others-exclude.sh
@@ -0,0 +1,307 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+test_description='git ls-files --others --exclude
+
+This test runs git ls-files --others and tests --exclude patterns.
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+rm -fr one three
+for dir in . one one/two three
+do
+ mkdir -p $dir &&
+ for i in 1 2 3 4 5 6 7 8
+ do
+ >$dir/a.$i
+ done
+done
+>"#ignore1"
+>"#ignore2"
+>"#hidden"
+
+cat >expect <<EOF
+a.2
+a.4
+a.5
+a.8
+one/a.3
+one/a.4
+one/a.5
+one/a.7
+one/two/a.2
+one/two/a.3
+one/two/a.5
+one/two/a.7
+one/two/a.8
+three/a.2
+three/a.3
+three/a.4
+three/a.5
+three/a.8
+EOF
+
+echo '.gitignore
+\#ignore1
+\#ignore2*
+\#hid*n
+output
+expect
+.gitignore
+*.7
+!*.8' >.git/ignore
+
+echo '*.1
+/*.3
+!*.6' >.gitignore
+echo '*.2
+two/*.4
+!*.7
+*.8' >one/.gitignore
+echo '!*.2
+!*.8' >one/two/.gitignore
+
+allignores='.gitignore one/.gitignore one/two/.gitignore'
+
+test_expect_success 'git ls-files --others with various exclude options.' '
+ git ls-files --others \
+ --exclude=\*.6 \
+ --exclude-per-directory=.gitignore \
+ --exclude-from=.git/ignore \
+ >output &&
+ test_cmp expect output
+'
+
+# Test \r\n (MSDOS-like systems)
+printf '*.1\r\n/*.3\r\n!*.6\r\n' >.gitignore
+
+test_expect_success 'git ls-files --others with \r\n line endings.' '
+ git ls-files --others \
+ --exclude=\*.6 \
+ --exclude-per-directory=.gitignore \
+ --exclude-from=.git/ignore \
+ >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'setup skip-worktree gitignore' '
+ git add $allignores &&
+ git update-index --skip-worktree $allignores &&
+ rm $allignores
+'
+
+test_expect_success 'git ls-files --others with various exclude options.' '
+ git ls-files --others \
+ --exclude=\*.6 \
+ --exclude-per-directory=.gitignore \
+ --exclude-from=.git/ignore \
+ >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'restore gitignore' '
+ git checkout --ignore-skip-worktree-bits $allignores &&
+ rm .git/index
+'
+
+cat > excludes-file <<\EOF
+*.[1-8]
+e*
+\#*
+EOF
+
+git config core.excludesFile excludes-file
+
+git -c status.displayCommentPrefix=true status | grep "^# " > output
+
+cat > expect << EOF
+# .gitignore
+# a.6
+# one/
+# output
+# three/
+EOF
+
+test_expect_success 'git status honors core.excludesfile' \
+ 'test_cmp expect output'
+
+test_expect_success 'trailing slash in exclude allows directory match(1)' '
+
+ git ls-files --others --exclude=one/ >output &&
+ if grep "^one/" output
+ then
+ echo Ooops
+ false
+ else
+ : happy
+ fi
+
+'
+
+test_expect_success 'trailing slash in exclude allows directory match (2)' '
+
+ git ls-files --others --exclude=one/two/ >output &&
+ if grep "^one/two/" output
+ then
+ echo Ooops
+ false
+ else
+ : happy
+ fi
+
+'
+
+test_expect_success 'trailing slash in exclude forces directory match (1)' '
+
+ >two &&
+ git ls-files --others --exclude=two/ >output &&
+ grep "^two" output
+
+'
+
+test_expect_success 'trailing slash in exclude forces directory match (2)' '
+
+ git ls-files --others --exclude=one/a.1/ >output &&
+ grep "^one/a.1" output
+
+'
+
+test_expect_success 'negated exclude matches can override previous ones' '
+
+ git ls-files --others --exclude="a.*" --exclude="!a.1" >output &&
+ grep "^a.1" output
+'
+
+test_expect_success 'excluded directory overrides content patterns' '
+
+ git ls-files --others --exclude="one" --exclude="!one/a.1" >output &&
+ if grep "^one/a.1" output
+ then
+ false
+ fi
+'
+
+test_expect_success 'negated directory doesn'\''t affect content patterns' '
+
+ git ls-files --others --exclude="!one" --exclude="one/a.1" >output &&
+ if grep "^one/a.1" output
+ then
+ false
+ fi
+'
+
+test_expect_success 'subdirectory ignore (setup)' '
+ mkdir -p top/l1/l2 &&
+ (
+ cd top &&
+ git init &&
+ echo /.gitignore >.gitignore &&
+ echo l1 >>.gitignore &&
+ echo l2 >l1/.gitignore &&
+ >l1/l2/l1
+ )
+'
+
+test_expect_success 'subdirectory ignore (toplevel)' '
+ (
+ cd top &&
+ git ls-files -o --exclude-standard
+ ) >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'subdirectory ignore (l1/l2)' '
+ (
+ cd top/l1/l2 &&
+ git ls-files -o --exclude-standard
+ ) >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'subdirectory ignore (l1)' '
+ (
+ cd top/l1 &&
+ git ls-files -o --exclude-standard
+ ) >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'show/hide empty ignored directory (setup)' '
+ rm top/l1/l2/l1 &&
+ rm top/l1/.gitignore
+'
+
+test_expect_success 'show empty ignored directory with --directory' '
+ (
+ cd top &&
+ git ls-files -o -i --exclude l1 --directory
+ ) >actual &&
+ echo l1/ >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'hide empty ignored directory with --no-empty-directory' '
+ (
+ cd top &&
+ git ls-files -o -i --exclude l1 --directory --no-empty-directory
+ ) >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'show/hide empty ignored sub-directory (setup)' '
+ > top/l1/tracked &&
+ (
+ cd top &&
+ git add -f l1/tracked
+ )
+'
+
+test_expect_success 'show empty ignored sub-directory with --directory' '
+ (
+ cd top &&
+ git ls-files -o -i --exclude l1 --directory
+ ) >actual &&
+ echo l1/l2/ >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'hide empty ignored sub-directory with --no-empty-directory' '
+ (
+ cd top &&
+ git ls-files -o -i --exclude l1 --directory --no-empty-directory
+ ) >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'pattern matches prefix completely' '
+ git ls-files -i -o --exclude "/three/a.3[abc]" >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'ls-files with "**" patterns' '
+ cat <<-\EOF >expect &&
+ a.1
+ one/a.1
+ one/two/a.1
+ three/a.1
+ EOF
+ git ls-files -o -i --exclude "**/a.1" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'ls-files with "**" patterns and --directory' '
+ # Expectation same as previous test
+ git ls-files --directory -o -i --exclude "**/a.1" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'ls-files with "**" patterns and no slashes' '
+ git ls-files -o -i --exclude "one**a.1" >actual &&
+ test_must_be_empty actual
+'
+
+test_done
diff --git a/t/t3002-ls-files-dashpath.sh b/t/t3002-ls-files-dashpath.sh
new file mode 100755
index 0000000..4dd2455
--- /dev/null
+++ b/t/t3002-ls-files-dashpath.sh
@@ -0,0 +1,77 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+test_description='git ls-files test (-- to terminate the path list).
+
+This test runs git ls-files --others with the following on the
+filesystem.
+
+ path0 - a file
+ -foo - a file with a funny name.
+ -- - another file with a funny name.
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ echo frotz >path0 &&
+ echo frotz >./-foo &&
+ echo frotz >./--
+'
+
+test_expect_success 'git ls-files without path restriction.' '
+ test_when_finished "rm -f expect" &&
+ git ls-files --others >output &&
+ cat >expect <<-\EOF &&
+ --
+ -foo
+ output
+ path0
+ EOF
+ test_cmp output expect
+'
+
+test_expect_success 'git ls-files with path restriction.' '
+ test_when_finished "rm -f expect" &&
+ git ls-files --others path0 >output &&
+ cat >expect <<-\EOF &&
+ path0
+ EOF
+ test_cmp output expect
+'
+
+test_expect_success 'git ls-files with path restriction with --.' '
+ test_when_finished "rm -f expect" &&
+ git ls-files --others -- path0 >output &&
+ cat >expect <<-\EOF &&
+ path0
+ EOF
+ test_cmp output expect
+'
+
+test_expect_success 'git ls-files with path restriction with -- --.' '
+ test_when_finished "rm -f expect" &&
+ git ls-files --others -- -- >output &&
+ cat >expect <<-\EOF &&
+ --
+ EOF
+ test_cmp output expect
+'
+
+test_expect_success 'git ls-files with no path restriction.' '
+ test_when_finished "rm -f expect" &&
+ git ls-files --others -- >output &&
+ cat >expect <<-\EOF &&
+ --
+ -foo
+ output
+ path0
+ EOF
+ test_cmp output expect
+
+'
+
+test_done
diff --git a/t/t3003-ls-files-exclude.sh b/t/t3003-ls-files-exclude.sh
new file mode 100755
index 0000000..7933dff
--- /dev/null
+++ b/t/t3003-ls-files-exclude.sh
@@ -0,0 +1,42 @@
+#!/bin/sh
+
+test_description='ls-files --exclude does not affect index files'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'create repo with file' '
+ echo content >file &&
+ git add file &&
+ git commit -m file &&
+ echo modification >file
+'
+
+check_output() {
+test_expect_success "ls-files output contains file ($1)" "
+ echo '$2' >expect &&
+ git ls-files --exclude-standard --$1 >output &&
+ test_cmp expect output
+"
+}
+
+check_all_output() {
+ check_output 'cached' 'file'
+ check_output 'modified' 'file'
+}
+
+check_all_output
+test_expect_success 'add file to gitignore' '
+ echo file >.gitignore
+'
+check_all_output
+
+test_expect_success 'ls-files -i -c lists only tracked-but-ignored files' '
+ echo content >other-file &&
+ git add other-file &&
+ echo file >expect &&
+ git ls-files -i -c --exclude-standard >output &&
+ test_cmp expect output
+'
+
+test_done
diff --git a/t/t3004-ls-files-basic.sh b/t/t3004-ls-files-basic.sh
new file mode 100755
index 0000000..12e41a7
--- /dev/null
+++ b/t/t3004-ls-files-basic.sh
@@ -0,0 +1,55 @@
+#!/bin/sh
+
+test_description='basic ls-files tests
+
+This test runs git ls-files with various unusual or malformed
+command-line arguments.
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'ls-files in empty repository' '
+ git ls-files >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'ls-files with nonexistent path' '
+ git ls-files doesnotexist >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'ls-files with nonsense option' '
+ test_expect_code 129 git ls-files --nonsense 2>actual &&
+ test_grep "[Uu]sage: git ls-files" actual
+'
+
+test_expect_success 'ls-files -h in corrupt repository' '
+ mkdir broken &&
+ (
+ cd broken &&
+ git init &&
+ >.git/index &&
+ test_expect_code 129 git ls-files -h >usage 2>&1
+ ) &&
+ test_grep "[Uu]sage: git ls-files " broken/usage
+'
+
+test_expect_success SYMLINKS 'ls-files with absolute paths to symlinks' '
+ mkdir subs &&
+ ln -s nosuch link &&
+ ln -s ../nosuch subs/link &&
+ git add link subs/link &&
+ git ls-files -s link subs/link >expect &&
+ git ls-files -s "$(pwd)/link" "$(pwd)/subs/link" >actual &&
+ test_cmp expect actual &&
+
+ (
+ cd subs &&
+ git ls-files -s link >../expect &&
+ git ls-files -s "$(pwd)/link" >../actual
+ ) &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t3005-ls-files-relative.sh b/t/t3005-ls-files-relative.sh
new file mode 100755
index 0000000..fbfa210
--- /dev/null
+++ b/t/t3005-ls-files-relative.sh
@@ -0,0 +1,63 @@
+#!/bin/sh
+
+test_description='ls-files tests with relative paths
+
+This test runs git ls-files with various relative path arguments.
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'prepare' '
+ : >never-mind-me &&
+ git add never-mind-me &&
+ mkdir top &&
+ (
+ cd top &&
+ mkdir sub &&
+ x="x xa xbc xdef xghij xklmno" &&
+ y=$(echo "$x" | tr x y) &&
+ touch $x &&
+ touch $y &&
+ cd sub &&
+ git add ../x*
+ )
+'
+
+test_expect_success 'ls-files with mixed levels' '
+ (
+ cd top/sub &&
+ cat >expect <<-EOF &&
+ ../../never-mind-me
+ ../x
+ EOF
+ git ls-files $(cat expect) >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'ls-files -c' '
+ (
+ cd top/sub &&
+ printf "error: pathspec $SQ%s$SQ did not match any file(s) known to git\n" ../y* >expect.err &&
+ echo "Did you forget to ${SQ}git add${SQ}?" >>expect.err &&
+ ls ../x* >expect.out &&
+ test_must_fail git ls-files -c --error-unmatch ../[xy]* >actual.out 2>actual.err &&
+ test_cmp expect.out actual.out &&
+ test_cmp expect.err actual.err
+ )
+'
+
+test_expect_success 'ls-files -o' '
+ (
+ cd top/sub &&
+ printf "error: pathspec $SQ%s$SQ did not match any file(s) known to git\n" ../x* >expect.err &&
+ echo "Did you forget to ${SQ}git add${SQ}?" >>expect.err &&
+ ls ../y* >expect.out &&
+ test_must_fail git ls-files -o --error-unmatch ../[xy]* >actual.out 2>actual.err &&
+ test_cmp expect.out actual.out &&
+ test_cmp expect.err actual.err
+ )
+'
+
+test_done
diff --git a/t/t3006-ls-files-long.sh b/t/t3006-ls-files-long.sh
new file mode 100755
index 0000000..2aaf91e
--- /dev/null
+++ b/t/t3006-ls-files-long.sh
@@ -0,0 +1,41 @@
+#!/bin/sh
+
+test_description='overly long paths'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ p=filefilefilefilefilefilefilefile &&
+ p=$p$p$p$p$p$p$p$p$p$p$p$p$p$p$p$p &&
+ p=$p$p$p$p$p$p$p$p$p$p$p$p$p$p$p$p &&
+
+ path_a=${p}_a &&
+ path_z=${p}_z &&
+
+ blob_a=$(echo frotz | git hash-object -w --stdin) &&
+ blob_z=$(echo nitfol | git hash-object -w --stdin) &&
+
+ pat="100644 %s 0\t%s\n"
+'
+
+test_expect_success 'overly-long path by itself is not a problem' '
+ printf "$pat" "$blob_a" "$path_a" |
+ git update-index --add --index-info &&
+ echo "$path_a" >expect &&
+ git ls-files >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'overly-long path does not replace another by mistake' '
+ printf "$pat" "$blob_a" "$path_a" "$blob_z" "$path_z" |
+ git update-index --add --index-info &&
+ (
+ echo "$path_a" &&
+ echo "$path_z"
+ ) >expect &&
+ git ls-files >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t3007-ls-files-recurse-submodules.sh b/t/t3007-ls-files-recurse-submodules.sh
new file mode 100755
index 0000000..61771ee
--- /dev/null
+++ b/t/t3007-ls-files-recurse-submodules.sh
@@ -0,0 +1,348 @@
+#!/bin/sh
+
+test_description='Test ls-files recurse-submodules feature
+
+This test verifies the recurse-submodules feature correctly lists files from
+submodules.
+'
+
+. ./test-lib.sh
+
+test_expect_success 'setup directory structure and submodules' '
+ echo a >a &&
+ mkdir b &&
+ echo b >b/b &&
+ git add a b &&
+ git commit -m "add a and b" &&
+ git init submodule &&
+ echo c >submodule/c &&
+ git -C submodule add c &&
+ git -C submodule commit -m "add c" &&
+ git submodule add ./submodule &&
+ git commit -m "added submodule"
+'
+
+test_expect_success 'ls-files correctly outputs files in submodule' '
+ cat >expect <<-\EOF &&
+ .gitmodules
+ a
+ b/b
+ submodule/c
+ EOF
+
+ git ls-files --recurse-submodules >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--stage' '
+ GITMODULES_HASH=$(git rev-parse HEAD:.gitmodules) &&
+ A_HASH=$(git rev-parse HEAD:a) &&
+ B_HASH=$(git rev-parse HEAD:b/b) &&
+ C_HASH=$(git -C submodule rev-parse HEAD:c) &&
+
+ cat >expect <<-EOF &&
+ 100644 $GITMODULES_HASH 0 .gitmodules
+ 100644 $A_HASH 0 a
+ 100644 $B_HASH 0 b/b
+ 100644 $C_HASH 0 submodule/c
+ EOF
+
+ git ls-files --stage --recurse-submodules >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'ls-files correctly outputs files in submodule with -z' '
+ lf_to_nul >expect <<-\EOF &&
+ .gitmodules
+ a
+ b/b
+ submodule/c
+ EOF
+
+ git ls-files --recurse-submodules -z >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'ls-files does not output files not added to a repo' '
+ cat >expect <<-\EOF &&
+ .gitmodules
+ a
+ b/b
+ submodule/c
+ EOF
+
+ echo a >not_added &&
+ echo b >b/not_added &&
+ echo c >submodule/not_added &&
+ git ls-files --recurse-submodules >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'ls-files recurses more than 1 level' '
+ cat >expect <<-\EOF &&
+ .gitmodules
+ a
+ b/b
+ submodule/.gitmodules
+ submodule/c
+ submodule/subsub/d
+ EOF
+
+ git init submodule/subsub &&
+ echo d >submodule/subsub/d &&
+ git -C submodule/subsub add d &&
+ git -C submodule/subsub commit -m "add d" &&
+ git -C submodule submodule add ./subsub &&
+ git -C submodule commit -m "added subsub" &&
+ git submodule absorbgitdirs &&
+ git ls-files --recurse-submodules >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'ls-files works with GIT_DIR' '
+ cat >expect <<-\EOF &&
+ .gitmodules
+ c
+ subsub/d
+ EOF
+
+ git --git-dir=submodule/.git ls-files --recurse-submodules >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--recurse-submodules and pathspecs setup' '
+ echo e >submodule/subsub/e.txt &&
+ git -C submodule/subsub add e.txt &&
+ git -C submodule/subsub commit -m "adding e.txt" &&
+ echo f >submodule/f.TXT &&
+ echo g >submodule/g.txt &&
+ git -C submodule add f.TXT g.txt &&
+ git -C submodule commit -m "add f and g" &&
+ echo h >h.txt &&
+ mkdir sib &&
+ echo sib >sib/file &&
+ git add h.txt sib/file &&
+ git commit -m "add h and sib/file" &&
+ git init sub &&
+ echo sub >sub/file &&
+ git -C sub add file &&
+ git -C sub commit -m "add file" &&
+ git submodule add ./sub &&
+ git commit -m "added sub" &&
+
+ cat >expect <<-\EOF &&
+ .gitmodules
+ a
+ b/b
+ h.txt
+ sib/file
+ sub/file
+ submodule/.gitmodules
+ submodule/c
+ submodule/f.TXT
+ submodule/g.txt
+ submodule/subsub/d
+ submodule/subsub/e.txt
+ EOF
+
+ git ls-files --recurse-submodules >actual &&
+ test_cmp expect actual &&
+ git ls-files --recurse-submodules "*" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'inactive submodule' '
+ test_when_finished "git config --bool submodule.submodule.active true" &&
+ test_when_finished "git -C submodule config --bool submodule.subsub.active true" &&
+ git config --bool submodule.submodule.active "false" &&
+
+ cat >expect <<-\EOF &&
+ .gitmodules
+ a
+ b/b
+ h.txt
+ sib/file
+ sub/file
+ submodule
+ EOF
+
+ git ls-files --recurse-submodules >actual &&
+ test_cmp expect actual &&
+
+ git config --bool submodule.submodule.active "true" &&
+ git -C submodule config --bool submodule.subsub.active "false" &&
+
+ cat >expect <<-\EOF &&
+ .gitmodules
+ a
+ b/b
+ h.txt
+ sib/file
+ sub/file
+ submodule/.gitmodules
+ submodule/c
+ submodule/f.TXT
+ submodule/g.txt
+ submodule/subsub
+ EOF
+
+ git ls-files --recurse-submodules >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--recurse-submodules and pathspecs' '
+ cat >expect <<-\EOF &&
+ h.txt
+ submodule/g.txt
+ submodule/subsub/e.txt
+ EOF
+
+ git ls-files --recurse-submodules "*.txt" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--recurse-submodules and pathspecs' '
+ cat >expect <<-\EOF &&
+ h.txt
+ submodule/f.TXT
+ submodule/g.txt
+ submodule/subsub/e.txt
+ EOF
+
+ git ls-files --recurse-submodules ":(icase)*.txt" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--recurse-submodules and pathspecs' '
+ cat >expect <<-\EOF &&
+ h.txt
+ submodule/f.TXT
+ submodule/g.txt
+ EOF
+
+ git ls-files --recurse-submodules ":(icase)*.txt" ":(exclude)submodule/subsub/*" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--recurse-submodules and pathspecs' '
+ cat >expect <<-\EOF &&
+ sub/file
+ EOF
+
+ git ls-files --recurse-submodules "sub" >actual &&
+ test_cmp expect actual &&
+ git ls-files --recurse-submodules "sub/" >actual &&
+ test_cmp expect actual &&
+ git ls-files --recurse-submodules "sub/file" >actual &&
+ test_cmp expect actual &&
+ git ls-files --recurse-submodules "su*/file" >actual &&
+ test_cmp expect actual &&
+ git ls-files --recurse-submodules "su?/file" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--recurse-submodules and pathspecs' '
+ cat >expect <<-\EOF &&
+ sib/file
+ sub/file
+ EOF
+
+ git ls-files --recurse-submodules "s??/file" >actual &&
+ test_cmp expect actual &&
+ git ls-files --recurse-submodules "s???file" >actual &&
+ test_cmp expect actual &&
+ git ls-files --recurse-submodules "s*file" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--recurse-submodules and relative paths' '
+ # From subdir
+ cat >expect <<-\EOF &&
+ b
+ EOF
+ git -C b ls-files --recurse-submodules >actual &&
+ test_cmp expect actual &&
+
+ # Relative path to top
+ cat >expect <<-\EOF &&
+ ../.gitmodules
+ ../a
+ b
+ ../h.txt
+ ../sib/file
+ ../sub/file
+ ../submodule/.gitmodules
+ ../submodule/c
+ ../submodule/f.TXT
+ ../submodule/g.txt
+ ../submodule/subsub/d
+ ../submodule/subsub/e.txt
+ EOF
+ git -C b ls-files --recurse-submodules -- .. >actual &&
+ test_cmp expect actual &&
+
+ # Relative path to submodule
+ cat >expect <<-\EOF &&
+ ../submodule/.gitmodules
+ ../submodule/c
+ ../submodule/f.TXT
+ ../submodule/g.txt
+ ../submodule/subsub/d
+ ../submodule/subsub/e.txt
+ EOF
+ git -C b ls-files --recurse-submodules -- ../submodule >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--recurse-submodules does not support --error-unmatch' '
+ test_must_fail git ls-files --recurse-submodules --error-unmatch 2>actual &&
+ test_grep "does not support --error-unmatch" actual
+'
+
+test_expect_success '--recurse-submodules parses submodule repo config' '
+ test_config -C submodule index.sparse "invalid non-boolean value" &&
+ test_must_fail git ls-files --recurse-submodules 2>err &&
+ grep "bad boolean config value" err
+'
+
+test_expect_success '--recurse-submodules parses submodule worktree config' '
+ test_config -C submodule extensions.worktreeConfig true &&
+ test_config -C submodule --worktree index.sparse "invalid non-boolean value" &&
+
+ test_must_fail git ls-files --recurse-submodules 2>err &&
+ grep "bad boolean config value" err
+'
+
+test_expect_success '--recurse-submodules submodules ignore super project worktreeConfig extension' '
+ # Enable worktree config in both super project & submodule, set an
+ # invalid config in the submodule worktree config
+ test_config extensions.worktreeConfig true &&
+ test_config -C submodule extensions.worktreeConfig true &&
+ test_config -C submodule --worktree index.sparse "invalid non-boolean value" &&
+
+ # Now, disable the worktree config in the submodule. Note that we need
+ # to manually re-enable extensions.worktreeConfig when the test is
+ # finished, otherwise the test_unconfig of index.sparse will not work.
+ test_unconfig -C submodule extensions.worktreeConfig &&
+ test_when_finished "git -C submodule config extensions.worktreeConfig true" &&
+
+ # With extensions.worktreeConfig disabled in the submodule, the invalid
+ # worktree config is not picked up.
+ git ls-files --recurse-submodules 2>err &&
+ ! grep "bad boolean config value" err
+'
+
+test_incompatible_with_recurse_submodules () {
+ test_expect_success "--recurse-submodules and $1 are incompatible" "
+ test_must_fail git ls-files --recurse-submodules $1 2>actual &&
+ test_grep 'unsupported mode' actual
+ "
+}
+
+test_incompatible_with_recurse_submodules --deleted
+test_incompatible_with_recurse_submodules --modified
+test_incompatible_with_recurse_submodules --others
+test_incompatible_with_recurse_submodules --killed
+test_incompatible_with_recurse_submodules --unmerged
+
+test_done
diff --git a/t/t3008-ls-files-lazy-init-name-hash.sh b/t/t3008-ls-files-lazy-init-name-hash.sh
new file mode 100755
index 0000000..51d3dff
--- /dev/null
+++ b/t/t3008-ls-files-lazy-init-name-hash.sh
@@ -0,0 +1,28 @@
+#!/bin/sh
+
+test_description='Test the lazy init name hash with various folder structures'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+if test 1 -eq $(test-tool online-cpus)
+then
+ skip_all='skipping lazy-init tests, single cpu'
+ test_done
+fi
+
+LAZY_THREAD_COST=2000
+
+test_expect_success 'no buffer overflow in lazy_init_name_hash' '
+ (
+ test_seq $LAZY_THREAD_COST | sed "s/^/a_/" &&
+ echo b/b/b &&
+ test_seq $LAZY_THREAD_COST | sed "s/^/c_/" &&
+ test_seq 50 | sed "s/^/d_/" | tr "\n" "/" && echo d
+ ) |
+ sed "s/^/100644 $EMPTY_BLOB /" |
+ git update-index --index-info &&
+ test-tool lazy-init-name-hash -m
+'
+
+test_done
diff --git a/t/t3009-ls-files-others-nonsubmodule.sh b/t/t3009-ls-files-others-nonsubmodule.sh
new file mode 100755
index 0000000..14218b3
--- /dev/null
+++ b/t/t3009-ls-files-others-nonsubmodule.sh
@@ -0,0 +1,51 @@
+#!/bin/sh
+
+test_description='test git ls-files --others with non-submodule repositories
+
+This test runs git ls-files --others with the following working tree:
+
+ nonrepo-no-files/
+ plain directory with no files
+ nonrepo-untracked-file/
+ plain directory with an untracked file
+ repo-no-commit-no-files/
+ git repository without a commit or a file
+ repo-no-commit-untracked-file/
+ git repository without a commit but with an untracked file
+ repo-with-commit-no-files/
+ git repository with a commit and no untracked files
+ repo-with-commit-untracked-file/
+ git repository with a commit and an untracked file
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup: directories' '
+ mkdir nonrepo-no-files/ &&
+ mkdir nonrepo-untracked-file &&
+ : >nonrepo-untracked-file/untracked &&
+ git init repo-no-commit-no-files &&
+ git init repo-no-commit-untracked-file &&
+ : >repo-no-commit-untracked-file/untracked &&
+ git init repo-with-commit-no-files &&
+ git -C repo-with-commit-no-files commit --allow-empty -mmsg &&
+ git init repo-with-commit-untracked-file &&
+ test_commit -C repo-with-commit-untracked-file msg &&
+ : >repo-with-commit-untracked-file/untracked
+'
+
+test_expect_success 'ls-files --others handles untracked git repositories' '
+ git ls-files -o >output &&
+ cat >expect <<-EOF &&
+ nonrepo-untracked-file/untracked
+ output
+ repo-no-commit-no-files/
+ repo-no-commit-untracked-file/
+ repo-with-commit-no-files/
+ repo-with-commit-untracked-file/
+ EOF
+ test_cmp expect output
+'
+
+test_done
diff --git a/t/t3010-ls-files-killed-modified.sh b/t/t3010-ls-files-killed-modified.sh
new file mode 100755
index 0000000..0541787
--- /dev/null
+++ b/t/t3010-ls-files-killed-modified.sh
@@ -0,0 +1,128 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+test_description='git ls-files -k and -m flags test.
+
+This test prepares the following in the cache:
+
+ path0 - a file
+ path1 - a symlink
+ path2/file2 - a file in a directory
+ path3/file3 - a file in a directory
+ pathx/ju - a file in a directory
+ submod1/ - a submodule
+ submod2/ - another submodule
+
+and the following on the filesystem:
+
+ path0/file0 - a file in a directory
+ path1/file1 - a file in a directory
+ path2 - a file
+ path3 - a symlink
+ path4 - a file
+ path5 - a symlink
+ path6/file6 - a file in a directory
+ pathx/ju/nk - a file in a directory to be killed
+ submod1/ - a submodule (modified from the cache)
+ submod2/ - a submodule (matches the cache)
+
+git ls-files -k should report that existing filesystem objects
+path0/*, path1/*, path2 and path3 to be killed.
+
+Also for modification test, the cache and working tree have:
+
+ path7 - an empty file, modified to a non-empty file.
+ path8 - a non-empty file, modified to an empty file.
+ path9 - an empty file, cache dirtied.
+ path10 - a non-empty file, cache dirtied.
+
+We should report path0, path1, path2/file2, path3/file3, path7 and path8
+modified without reporting path9 and path10. submod1 is also modified.
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'git update-index --add to add various paths.' '
+ date >path0 &&
+ test_ln_s_add xyzzy path1 &&
+ mkdir path2 path3 pathx &&
+ date >path2/file2 &&
+ date >path3/file3 &&
+ >pathx/ju &&
+ : >path7 &&
+ date >path8 &&
+ : >path9 &&
+ date >path10 &&
+ git update-index --add -- path0 path?/file? pathx/ju path7 path8 path9 path10 &&
+ git init submod1 &&
+ git -C submod1 commit --allow-empty -m "empty 1" &&
+ git init submod2 &&
+ git -C submod2 commit --allow-empty -m "empty 2" &&
+ git update-index --add submod[12] &&
+ (
+ cd submod1 &&
+ git commit --allow-empty -m "empty 1 (updated)"
+ ) &&
+ rm -fr path? # leave path10 alone
+'
+
+test_expect_success 'git ls-files -k to show killed files.' '
+ date >path2 &&
+ if test_have_prereq SYMLINKS
+ then
+ ln -s frotz path3 &&
+ ln -s nitfol path5
+ else
+ date >path3 &&
+ date >path5
+ fi &&
+ mkdir -p path0 path1 path6 pathx/ju &&
+ date >path0/file0 &&
+ date >path1/file1 &&
+ date >path6/file6 &&
+ date >path7 &&
+ : >path8 &&
+ : >path9 &&
+ touch path10 &&
+ >pathx/ju/nk &&
+ cat >.expected <<-\EOF
+ path0/file0
+ path1/file1
+ path2
+ path3
+ pathx/ju/nk
+ EOF
+'
+
+test_expect_success 'git ls-files -k output (w/o icase)' '
+ git ls-files -k >.output &&
+ test_cmp .expected .output
+'
+
+test_expect_success 'git ls-files -k output (w/ icase)' '
+ git -c core.ignorecase=true ls-files -k >.output &&
+ test_cmp .expected .output
+'
+
+test_expect_success 'git ls-files -m to show modified files.' '
+ git ls-files -m >.output
+'
+
+test_expect_success 'validate git ls-files -m output.' '
+ cat >.expected <<-\EOF &&
+ path0
+ path1
+ path2/file2
+ path3/file3
+ path7
+ path8
+ pathx/ju
+ submod1
+ EOF
+ test_cmp .expected .output
+'
+
+test_done
diff --git a/t/t3011-common-prefixes-and-directory-traversal.sh b/t/t3011-common-prefixes-and-directory-traversal.sh
new file mode 100755
index 0000000..3da5b2b
--- /dev/null
+++ b/t/t3011-common-prefixes-and-directory-traversal.sh
@@ -0,0 +1,209 @@
+#!/bin/sh
+
+test_description='directory traversal handling, especially with common prefixes'
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ test_commit hello &&
+
+ >empty &&
+ mkdir untracked_dir &&
+ >untracked_dir/empty &&
+ git init untracked_repo &&
+ >untracked_repo/empty &&
+
+ cat <<-EOF >.gitignore &&
+ ignored
+ an_ignored_dir/
+ EOF
+ mkdir an_ignored_dir &&
+ mkdir an_untracked_dir &&
+ >an_ignored_dir/ignored &&
+ >an_ignored_dir/untracked &&
+ >an_untracked_dir/ignored &&
+ >an_untracked_dir/untracked
+'
+
+test_expect_success 'git ls-files -o shows the right entries' '
+ cat <<-EOF >expect &&
+ .gitignore
+ actual
+ an_ignored_dir/ignored
+ an_ignored_dir/untracked
+ an_untracked_dir/ignored
+ an_untracked_dir/untracked
+ empty
+ expect
+ untracked_dir/empty
+ untracked_repo/
+ EOF
+ git ls-files -o >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git ls-files -o --exclude-standard shows the right entries' '
+ cat <<-EOF >expect &&
+ .gitignore
+ actual
+ an_untracked_dir/untracked
+ empty
+ expect
+ untracked_dir/empty
+ untracked_repo/
+ EOF
+ git ls-files -o --exclude-standard >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git ls-files -o untracked_dir recurses' '
+ echo untracked_dir/empty >expect &&
+ git ls-files -o untracked_dir >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git ls-files -o untracked_dir/ recurses' '
+ echo untracked_dir/empty >expect &&
+ git ls-files -o untracked_dir/ >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git ls-files -o --directory untracked_dir does not recurse' '
+ echo untracked_dir/ >expect &&
+ git ls-files -o --directory untracked_dir >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git ls-files -o --directory untracked_dir/ does not recurse' '
+ echo untracked_dir/ >expect &&
+ git ls-files -o --directory untracked_dir/ >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git ls-files -o untracked_repo does not recurse' '
+ echo untracked_repo/ >expect &&
+ git ls-files -o untracked_repo >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git ls-files -o untracked_repo/ does not recurse' '
+ echo untracked_repo/ >expect &&
+ git ls-files -o untracked_repo/ >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git ls-files -o untracked_dir untracked_repo recurses into untracked_dir only' '
+ cat <<-EOF >expect &&
+ untracked_dir/empty
+ untracked_repo/
+ EOF
+ git ls-files -o untracked_dir untracked_repo >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git ls-files -o untracked_dir/ untracked_repo/ recurses into untracked_dir only' '
+ cat <<-EOF >expect &&
+ untracked_dir/empty
+ untracked_repo/
+ EOF
+ git ls-files -o untracked_dir/ untracked_repo/ >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git ls-files -o --directory untracked_dir untracked_repo does not recurse' '
+ cat <<-EOF >expect &&
+ untracked_dir/
+ untracked_repo/
+ EOF
+ git ls-files -o --directory untracked_dir untracked_repo >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git ls-files -o --directory untracked_dir/ untracked_repo/ does not recurse' '
+ cat <<-EOF >expect &&
+ untracked_dir/
+ untracked_repo/
+ EOF
+ git ls-files -o --directory untracked_dir/ untracked_repo/ >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git ls-files -o .git shows nothing' '
+ git ls-files -o .git >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'git ls-files -o .git/ shows nothing' '
+ git ls-files -o .git/ >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success FUNNYNAMES 'git ls-files -o untracked_* recurses appropriately' '
+ mkdir "untracked_*" &&
+ >"untracked_*/empty" &&
+
+ cat <<-EOF >expect &&
+ untracked_*/empty
+ untracked_dir/empty
+ untracked_repo/
+ EOF
+ git ls-files -o "untracked_*" >actual &&
+ test_cmp expect actual
+'
+
+# It turns out fill_directory returns the right paths, but ls-files' post-call
+# filtering in show_dir_entry() via calling dir_path_match() which ends up
+# in git_fnmatch() has logic for PATHSPEC_ONESTAR that assumes the pathspec
+# must match the full path; it doesn't check it for matching a leading
+# directory.
+test_expect_failure FUNNYNAMES 'git ls-files -o untracked_*/ recurses appropriately' '
+ cat <<-EOF >expect &&
+ untracked_*/empty
+ untracked_dir/empty
+ untracked_repo/
+ EOF
+ git ls-files -o "untracked_*/" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success FUNNYNAMES 'git ls-files -o --directory untracked_* does not recurse' '
+ cat <<-EOF >expect &&
+ untracked_*/
+ untracked_dir/
+ untracked_repo/
+ EOF
+ git ls-files -o --directory "untracked_*" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success FUNNYNAMES 'git ls-files -o --directory untracked_*/ does not recurse' '
+ cat <<-EOF >expect &&
+ untracked_*/
+ untracked_dir/
+ untracked_repo/
+ EOF
+ git ls-files -o --directory "untracked_*/" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git ls-files -o consistent between one or two dirs' '
+ git ls-files -o --exclude-standard an_ignored_dir/ an_untracked_dir/ >tmp &&
+ ! grep ^an_ignored_dir/ tmp >expect &&
+ git ls-files -o --exclude-standard an_ignored_dir/ >actual &&
+ test_cmp expect actual
+'
+
+# ls-files doesn't have a way to request showing both untracked and ignored
+# files at the same time, so use `git status --ignored`
+test_expect_success 'git status --ignored shows same files under dir with or without pathspec' '
+ cat <<-EOF >expect &&
+ ?? an_untracked_dir/
+ !! an_untracked_dir/ignored
+ EOF
+ git status --porcelain --ignored >output &&
+ grep an_untracked_dir output >expect &&
+ git status --porcelain --ignored an_untracked_dir/ >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t3012-ls-files-dedup.sh b/t/t3012-ls-files-dedup.sh
new file mode 100755
index 0000000..190e2f6
--- /dev/null
+++ b/t/t3012-ls-files-dedup.sh
@@ -0,0 +1,67 @@
+#!/bin/sh
+
+test_description='git ls-files --deduplicate test'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ >a.txt &&
+ >b.txt &&
+ >delete.txt &&
+ git add a.txt b.txt delete.txt &&
+ git commit -m base &&
+ echo a >a.txt &&
+ echo b >b.txt &&
+ echo delete >delete.txt &&
+ git add a.txt b.txt delete.txt &&
+ git commit -m tip &&
+ git tag tip &&
+ git reset --hard HEAD^ &&
+ echo change >a.txt &&
+ git commit -a -m side &&
+ git tag side
+'
+
+test_expect_success 'git ls-files --deduplicate to show unique unmerged path' '
+ test_must_fail git merge tip &&
+ git ls-files --deduplicate >actual &&
+ cat >expect <<-\EOF &&
+ a.txt
+ b.txt
+ delete.txt
+ EOF
+ test_cmp expect actual &&
+ git merge --abort
+'
+
+test_expect_success 'git ls-files -d -m --deduplicate with different display options' '
+ git reset --hard side &&
+ test_must_fail git merge tip &&
+ rm delete.txt &&
+ git ls-files -d -m --deduplicate >actual &&
+ cat >expect <<-\EOF &&
+ a.txt
+ delete.txt
+ EOF
+ test_cmp expect actual &&
+ git ls-files -d -m -t --deduplicate >actual &&
+ cat >expect <<-\EOF &&
+ C a.txt
+ C a.txt
+ C a.txt
+ R delete.txt
+ C delete.txt
+ EOF
+ test_cmp expect actual &&
+ git ls-files -d -m -c --deduplicate >actual &&
+ cat >expect <<-\EOF &&
+ a.txt
+ b.txt
+ delete.txt
+ EOF
+ test_cmp expect actual &&
+ git merge --abort
+'
+
+test_done
diff --git a/t/t3013-ls-files-format.sh b/t/t3013-ls-files-format.sh
new file mode 100755
index 0000000..6e6ea0b
--- /dev/null
+++ b/t/t3013-ls-files-format.sh
@@ -0,0 +1,146 @@
+#!/bin/sh
+
+test_description='git ls-files --format test'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+for flag in -s -o -k -t --resolve-undo --deduplicate --eol
+do
+ test_expect_success "usage: --format is incompatible with $flag" '
+ test_expect_code 129 git ls-files --format="%(objectname)" $flag
+ '
+done
+
+test_expect_success 'setup' '
+ printf "LINEONE\nLINETWO\nLINETHREE\n" >o1.txt &&
+ printf "LINEONE\r\nLINETWO\r\nLINETHREE\r\n" >o2.txt &&
+ printf "LINEONE\r\nLINETWO\nLINETHREE\n" >o3.txt &&
+ git add o?.txt &&
+ oid=$(git hash-object o1.txt) &&
+ git update-index --add --cacheinfo 120000 $oid o4.txt &&
+ git update-index --add --cacheinfo 160000 $oid o5.txt &&
+ git update-index --add --cacheinfo 100755 $oid o6.txt &&
+ git commit -m base
+'
+
+test_expect_success 'git ls-files --format objectmode v.s. -s' '
+ git ls-files -s >files &&
+ cut -d" " -f1 files >expect &&
+ git ls-files --format="%(objectmode)" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git ls-files --format objectname v.s. -s' '
+ git ls-files -s >files &&
+ cut -d" " -f2 files >expect &&
+ git ls-files --format="%(objectname)" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git ls-files --format objecttype' '
+ git ls-files --format="%(objectname)" o1.txt o4.txt o6.txt >objectname &&
+ git cat-file --batch-check="%(objecttype)" >expect <objectname &&
+ git ls-files --format="%(objecttype)" o1.txt o4.txt o6.txt >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git ls-files --format objectsize' '
+ cat>expect <<-\EOF &&
+26
+29
+27
+26
+-
+26
+ EOF
+ git ls-files --format="%(objectsize)" >actual &&
+
+ test_cmp expect actual
+'
+
+test_expect_success 'git ls-files --format objectsize:padded' '
+ cat>expect <<-\EOF &&
+ 26
+ 29
+ 27
+ 26
+ -
+ 26
+ EOF
+ git ls-files --format="%(objectsize:padded)" >actual &&
+
+ test_cmp expect actual
+'
+
+test_expect_success 'git ls-files --format v.s. --eol' '
+ git ls-files --eol >tmp &&
+ sed -e "s/ / /g" -e "s/ */ /g" tmp >expect 2>err &&
+ test_must_be_empty err &&
+ git ls-files --format="i/%(eolinfo:index) w/%(eolinfo:worktree) attr/%(eolattr) %(path)" >actual 2>err &&
+ test_must_be_empty err &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git ls-files --format path v.s. -s' '
+ git ls-files -s >files &&
+ cut -f2 files >expect &&
+ git ls-files --format="%(path)" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git ls-files --format with relative path' '
+ cat >expect <<-\EOF &&
+ ../o1.txt
+ ../o2.txt
+ ../o3.txt
+ ../o4.txt
+ ../o5.txt
+ ../o6.txt
+ EOF
+ mkdir sub &&
+ cd sub &&
+ git ls-files --format="%(path)" ":/" >../actual &&
+ cd .. &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git ls-files --format with -m' '
+ echo change >o1.txt &&
+ cat >expect <<-\EOF &&
+ o1.txt
+ o4.txt
+ o5.txt
+ o6.txt
+ EOF
+ git ls-files --format="%(path)" -m >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git ls-files --format with -d' '
+ echo o7 >o7.txt &&
+ git add o7.txt &&
+ rm o7.txt &&
+ cat >expect <<-\EOF &&
+ o4.txt
+ o5.txt
+ o6.txt
+ o7.txt
+ EOF
+ git ls-files --format="%(path)" -d >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git ls-files --format v.s -s' '
+ git ls-files --stage >expect &&
+ git ls-files --format="%(objectmode) %(objectname) %(stage)%x09%(path)" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git ls-files --format with --debug' '
+ git ls-files --debug >expect &&
+ git ls-files --format="%(path)" --debug >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t3020-ls-files-error-unmatch.sh b/t/t3020-ls-files-error-unmatch.sh
new file mode 100755
index 0000000..133593d
--- /dev/null
+++ b/t/t3020-ls-files-error-unmatch.sh
@@ -0,0 +1,30 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Carl D. Worth
+#
+
+test_description='git ls-files test for --error-unmatch option
+
+This test runs git ls-files --error-unmatch to ensure it correctly
+returns an error when a non-existent path is provided on the command
+line.
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ touch foo bar &&
+ git update-index --add foo bar &&
+ git commit -m "add foo bar"
+'
+
+test_expect_success 'git ls-files --error-unmatch should fail with unmatched path.' '
+ test_must_fail git ls-files --error-unmatch foo bar-does-not-match
+'
+
+test_expect_success 'git ls-files --error-unmatch should succeed with matched paths.' '
+ git ls-files --error-unmatch foo bar
+'
+
+test_done
diff --git a/t/t3040-subprojects-basic.sh b/t/t3040-subprojects-basic.sh
new file mode 100755
index 0000000..bd65dfc
--- /dev/null
+++ b/t/t3040-subprojects-basic.sh
@@ -0,0 +1,84 @@
+#!/bin/sh
+
+test_description='Basic subproject functionality'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup: create superproject' '
+ : >Makefile &&
+ git add Makefile &&
+ git commit -m "Superproject created"
+'
+
+test_expect_success 'setup: create subprojects' '
+ mkdir sub1 &&
+ ( cd sub1 && git init && : >Makefile && git add * &&
+ git commit -q -m "subproject 1" ) &&
+ mkdir sub2 &&
+ ( cd sub2 && git init && : >Makefile && git add * &&
+ git commit -q -m "subproject 2" ) &&
+ git update-index --add sub1 &&
+ git add sub2 &&
+ git commit -q -m "subprojects added" &&
+ GIT_PRINT_SHA1_ELLIPSIS="yes" git diff-tree --abbrev=5 HEAD^ HEAD |cut -d" " -f-3,5- >current &&
+ git branch save HEAD &&
+ cat >expected <<-\EOF &&
+ :000000 160000 00000... A sub1
+ :000000 160000 00000... A sub2
+ EOF
+ test_cmp expected current
+'
+
+test_expect_success 'check if fsck ignores the subprojects' '
+ git fsck --full
+'
+
+test_expect_success 'check if commit in a subproject detected' '
+ ( cd sub1 &&
+ echo "all:" >>Makefile &&
+ echo " true" >>Makefile &&
+ git commit -q -a -m "make all" ) &&
+ test_expect_code 1 git diff-files --exit-code
+'
+
+test_expect_success 'check if a changed subproject HEAD can be committed' '
+ git commit -q -a -m "sub1 changed" &&
+ test_expect_code 1 git diff-tree --exit-code HEAD^ HEAD
+'
+
+test_expect_success 'check if diff-index works for subproject elements' '
+ test_expect_code 1 git diff-index --exit-code --cached save -- sub1
+'
+
+test_expect_success 'check if diff-tree works for subproject elements' '
+ test_expect_code 1 git diff-tree --exit-code HEAD^ HEAD -- sub1
+'
+
+test_expect_success 'check if git diff works for subproject elements' '
+ test_expect_code 1 git diff --exit-code HEAD^ HEAD
+'
+
+test_expect_success 'check if clone works' '
+ git ls-files -s >expected &&
+ git clone -l -s . cloned &&
+ ( cd cloned && git ls-files -s ) >current &&
+ test_cmp expected current
+'
+
+test_expect_success 'removing and adding subproject' '
+ git update-index --force-remove -- sub2 &&
+ mv sub2 sub3 &&
+ git add sub3 &&
+ git commit -q -m "renaming a subproject" &&
+ test_expect_code 1 git diff -M --name-status --exit-code HEAD^ HEAD
+'
+
+# the index must contain the object name the HEAD of the
+# subproject sub1 was at the point "save"
+test_expect_success 'checkout in superproject' '
+ git checkout save &&
+ git diff-index --exit-code --raw --cached save -- sub1
+'
+
+test_done
diff --git a/t/t3050-subprojects-fetch.sh b/t/t3050-subprojects-fetch.sh
new file mode 100755
index 0000000..3884694
--- /dev/null
+++ b/t/t3050-subprojects-fetch.sh
@@ -0,0 +1,53 @@
+#!/bin/sh
+
+test_description='fetching and pushing project with subproject'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ test_tick &&
+ mkdir -p sub && (
+ cd sub &&
+ git init &&
+ >subfile &&
+ git add subfile &&
+ git commit -m "subproject commit #1"
+ ) &&
+ >mainfile &&
+ git add sub mainfile &&
+ test_tick &&
+ git commit -m "superproject commit #1"
+'
+
+test_expect_success clone '
+ git clone "file://$(pwd)/.git" cloned &&
+ (git rev-parse HEAD && git ls-files -s) >expected &&
+ (
+ cd cloned &&
+ (git rev-parse HEAD && git ls-files -s) >../actual
+ ) &&
+ test_cmp expected actual
+'
+
+test_expect_success advance '
+ echo more >mainfile &&
+ git update-index --force-remove sub &&
+ mv sub/.git sub/.git-disabled &&
+ git add sub/subfile mainfile &&
+ mv sub/.git-disabled sub/.git &&
+ test_tick &&
+ git commit -m "superproject commit #2"
+'
+
+test_expect_success fetch '
+ (git rev-parse HEAD && git ls-files -s) >expected &&
+ (
+ cd cloned &&
+ git pull &&
+ (git rev-parse HEAD && git ls-files -s) >../actual
+ ) &&
+ test_cmp expected actual
+'
+
+test_done
diff --git a/t/t3060-ls-files-with-tree.sh b/t/t3060-ls-files-with-tree.sh
new file mode 100755
index 0000000..5a06732
--- /dev/null
+++ b/t/t3060-ls-files-with-tree.sh
@@ -0,0 +1,106 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Carl D. Worth
+#
+
+test_description='git ls-files test (--with-tree).
+
+This test runs git ls-files --with-tree and in particular in
+a scenario known to trigger a crash with some versions of git.
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+
+ # The bug we are exercising requires a fair number of entries
+ # in a sub-directory so that add_index_entry will trigger a
+ # realloc.
+
+ echo file >expected &&
+ mkdir sub &&
+ for n in 0 1 2 3 4 5
+ do
+ for m in 0 1 2 3 4 5 6 7 8 9
+ do
+ num=00$n$m &&
+ >sub/file-$num &&
+ echo file-$num >>expected ||
+ return 1
+ done
+ done &&
+ git add . &&
+ git commit -m "add a bunch of files" &&
+
+ # We remove them all so that we will have something to add
+ # back with --with-tree and so that we will definitely be
+ # under the realloc size to trigger the bug.
+ rm -rf sub &&
+ git commit -a -m "remove them all" &&
+
+ # The bug also requires some entry before our directory so that
+ # prune_index will modify the_repository->index.cache
+
+ mkdir a_directory_that_sorts_before_sub &&
+ >a_directory_that_sorts_before_sub/file &&
+ mkdir sub &&
+ >sub/file &&
+ git add .
+'
+
+test_expect_success 'usage' '
+ test_expect_code 128 git ls-files --with-tree=HEAD -u &&
+ test_expect_code 128 git ls-files --with-tree=HEAD -s &&
+ test_expect_code 128 git ls-files --recurse-submodules --with-tree=HEAD
+'
+
+test_expect_success 'git ls-files --with-tree should succeed from subdir' '
+ # We have to run from a sub-directory to trigger prune_index
+ # Then we finally get to run our --with-tree test
+ (
+ cd sub &&
+ git ls-files --with-tree=HEAD~1 >../output
+ )
+'
+
+test_expect_success 'git ls-files --with-tree should add entries from named tree.' '
+ test_cmp expected output
+'
+
+test_expect_success 'no duplicates in --with-tree output' '
+ git ls-files --with-tree=HEAD >actual &&
+ sort -u actual >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'setup: output in a conflict' '
+ test_create_repo conflict &&
+ test_commit -C conflict BASE file &&
+ test_commit -C conflict A file foo &&
+ git -C conflict reset --hard BASE &&
+ test_commit -C conflict B file bar
+'
+
+test_expect_success 'output in a conflict' '
+ test_must_fail git -C conflict merge A B &&
+ cat >expected <<-\EOF &&
+ file
+ file
+ file
+ file
+ EOF
+ git -C conflict ls-files --with-tree=HEAD >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'output with removed .git/index' '
+ cat >expected <<-\EOF &&
+ file
+ EOF
+ rm conflict/.git/index &&
+ git -C conflict ls-files --with-tree=HEAD >actual &&
+ test_cmp expected actual
+'
+
+test_done
diff --git a/t/t3070-wildmatch.sh b/t/t3070-wildmatch.sh
new file mode 100755
index 0000000..4dd42df
--- /dev/null
+++ b/t/t3070-wildmatch.sh
@@ -0,0 +1,445 @@
+#!/bin/sh
+
+test_description='wildmatch tests'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+should_create_test_file() {
+ file=$1
+
+ case $file in
+ # `touch .` will succeed but obviously not do what we intend
+ # here.
+ ".")
+ return 1
+ ;;
+ # We cannot create a file with an empty filename.
+ "")
+ return 1
+ ;;
+ # The tests that are testing that e.g. foo//bar is matched by
+ # foo/*/bar can't be tested on filesystems since there's no
+ # way we're getting a double slash.
+ *//*)
+ return 1
+ ;;
+ # When testing the difference between foo/bar and foo/bar/ we
+ # can't test the latter.
+ */)
+ return 1
+ ;;
+ # On Windows, \ in paths is silently converted to /, which
+ # would result in the "touch" below working, but the test
+ # itself failing. See 6fd1106aa4 ("t3700: Skip a test with
+ # backslashes in pathspec", 2009-03-13) for prior art and
+ # details.
+ *\\*)
+ if ! test_have_prereq BSLASHPSPEC
+ then
+ return 1
+ fi
+ # NOTE: The ;;& bash extension is not portable, so
+ # this test needs to be at the end of the pattern
+ # list.
+ #
+ # If we want to add more conditional returns we either
+ # need a new case statement, or turn this whole thing
+ # into a series of "if" tests.
+ ;;
+ esac
+
+
+ # On Windows proper (i.e. not Cygwin) many file names which
+ # under Cygwin would be emulated don't work.
+ if test_have_prereq MINGW
+ then
+ case $file in
+ " ")
+ # Files called " " are forbidden on Windows
+ return 1
+ ;;
+ *\<*|*\>*|*:*|*\"*|*\|*|*\?*|*\**)
+ # Files with various special characters aren't
+ # allowed on Windows. Sourced from
+ # https://stackoverflow.com/a/31976060
+ return 1
+ ;;
+ esac
+ fi
+
+ return 0
+}
+
+match_with_function() {
+ text=$1
+ pattern=$2
+ match_expect=$3
+ match_function=$4
+
+ if test "$match_expect" = 1
+ then
+ test_expect_success "$match_function: match '$text' '$pattern'" "
+ test-tool wildmatch $match_function '$text' '$pattern'
+ "
+ elif test "$match_expect" = 0
+ then
+ test_expect_success "$match_function: no match '$text' '$pattern'" "
+ test_must_fail test-tool wildmatch $match_function '$text' '$pattern'
+ "
+ else
+ test_expect_success "PANIC: Test framework error. Unknown matches value $match_expect" 'false'
+ fi
+
+}
+
+match_with_ls_files() {
+ text=$1
+ pattern=$2
+ match_expect=$3
+ match_function=$4
+ ls_files_args=$5
+
+ match_stdout_stderr_cmp="
+ tr -d '\0' <actual.raw >actual &&
+ test_must_be_empty actual.err &&
+ test_cmp expect actual"
+
+ if test "$match_expect" = 'E'
+ then
+ if test -e .git/created_test_file
+ then
+ test_expect_success EXPENSIVE_ON_WINDOWS "$match_function (via ls-files): match dies on '$pattern' '$text'" "
+ printf '%s' '$text' >expect &&
+ test_must_fail git$ls_files_args ls-files -z -- '$pattern'
+ "
+ else
+ test_expect_failure EXPENSIVE_ON_WINDOWS "$match_function (via ls-files): match skip '$pattern' '$text'" 'false'
+ fi
+ elif test "$match_expect" = 1
+ then
+ if test -e .git/created_test_file
+ then
+ test_expect_success EXPENSIVE_ON_WINDOWS "$match_function (via ls-files): match '$pattern' '$text'" "
+ printf '%s' '$text' >expect &&
+ git$ls_files_args ls-files -z -- '$pattern' >actual.raw 2>actual.err &&
+ $match_stdout_stderr_cmp
+ "
+ else
+ test_expect_failure EXPENSIVE_ON_WINDOWS "$match_function (via ls-files): match skip '$pattern' '$text'" 'false'
+ fi
+ elif test "$match_expect" = 0
+ then
+ if test -e .git/created_test_file
+ then
+ test_expect_success EXPENSIVE_ON_WINDOWS "$match_function (via ls-files): no match '$pattern' '$text'" "
+ >expect &&
+ git$ls_files_args ls-files -z -- '$pattern' >actual.raw 2>actual.err &&
+ $match_stdout_stderr_cmp
+ "
+ else
+ test_expect_failure EXPENSIVE_ON_WINDOWS "$match_function (via ls-files): no match skip '$pattern' '$text'" 'false'
+ fi
+ else
+ test_expect_success "PANIC: Test framework error. Unknown matches value $match_expect" 'false'
+ fi
+}
+
+match() {
+ if test "$#" = 6
+ then
+ # When test-tool wildmatch and git ls-files produce the same
+ # result.
+ match_glob=$1
+ match_file_glob=$match_glob
+ match_iglob=$2
+ match_file_iglob=$match_iglob
+ match_pathmatch=$3
+ match_file_pathmatch=$match_pathmatch
+ match_pathmatchi=$4
+ match_file_pathmatchi=$match_pathmatchi
+ text=$5
+ pattern=$6
+ elif test "$#" = 10
+ then
+ match_glob=$1
+ match_iglob=$2
+ match_pathmatch=$3
+ match_pathmatchi=$4
+ match_file_glob=$5
+ match_file_iglob=$6
+ match_file_pathmatch=$7
+ match_file_pathmatchi=$8
+ text=$9
+ pattern=${10}
+ fi
+
+ test_expect_success EXPENSIVE_ON_WINDOWS 'cleanup after previous file test' '
+ if test -e .git/created_test_file
+ then
+ git reset &&
+ git clean -df
+ fi
+ '
+
+ printf '%s' "$text" >.git/expected_test_file
+
+ test_expect_success EXPENSIVE_ON_WINDOWS "setup match file test for $text" '
+ file=$(cat .git/expected_test_file) &&
+ if should_create_test_file "$file"
+ then
+ dirs=${file%/*} &&
+ if test "$file" != "$dirs"
+ then
+ mkdir -p -- "$dirs" &&
+ touch -- "./$text"
+ else
+ touch -- "./$file"
+ fi &&
+ git add -A &&
+ printf "%s" "$file" >.git/created_test_file
+ elif test -e .git/created_test_file
+ then
+ rm .git/created_test_file
+ fi
+ '
+
+ # $1: Case sensitive glob match: test-tool wildmatch & ls-files
+ match_with_function "$text" "$pattern" $match_glob "wildmatch"
+ match_with_ls_files "$text" "$pattern" $match_file_glob "wildmatch" " --glob-pathspecs"
+
+ # $2: Case insensitive glob match: test-tool wildmatch & ls-files
+ match_with_function "$text" "$pattern" $match_iglob "iwildmatch"
+ match_with_ls_files "$text" "$pattern" $match_file_iglob "iwildmatch" " --glob-pathspecs --icase-pathspecs"
+
+ # $3: Case sensitive path match: test-tool wildmatch & ls-files
+ match_with_function "$text" "$pattern" $match_pathmatch "pathmatch"
+ match_with_ls_files "$text" "$pattern" $match_file_pathmatch "pathmatch" ""
+
+ # $4: Case insensitive path match: test-tool wildmatch & ls-files
+ match_with_function "$text" "$pattern" $match_pathmatchi "ipathmatch"
+ match_with_ls_files "$text" "$pattern" $match_file_pathmatchi "ipathmatch" " --icase-pathspecs"
+}
+
+# Basic wildmatch features
+match 1 1 1 1 foo foo
+match 0 0 0 0 foo bar
+match 1 1 1 1 '' ""
+match 1 1 1 1 foo '???'
+match 0 0 0 0 foo '??'
+match 1 1 1 1 foo '*'
+match 1 1 1 1 foo 'f*'
+match 0 0 0 0 foo '*f'
+match 1 1 1 1 foo '*foo*'
+match 1 1 1 1 foobar '*ob*a*r*'
+match 1 1 1 1 aaaaaaabababab '*ab'
+match 1 1 1 1 'foo*' 'foo\*'
+match 0 0 0 0 foobar 'foo\*bar'
+match 1 1 1 1 'f\oo' 'f\\oo'
+match 1 1 1 1 ball '*[al]?'
+match 0 0 0 0 ten '[ten]'
+match 1 1 1 1 ten '**[!te]'
+match 0 0 0 0 ten '**[!ten]'
+match 1 1 1 1 ten 't[a-g]n'
+match 0 0 0 0 ten 't[!a-g]n'
+match 1 1 1 1 ton 't[!a-g]n'
+match 1 1 1 1 ton 't[^a-g]n'
+match 1 1 1 1 'a]b' 'a[]]b'
+match 1 1 1 1 a-b 'a[]-]b'
+match 1 1 1 1 'a]b' 'a[]-]b'
+match 0 0 0 0 aab 'a[]-]b'
+match 1 1 1 1 aab 'a[]a-]b'
+match 1 1 1 1 ']' ']'
+
+# Extended slash-matching features
+match 0 0 1 1 'foo/baz/bar' 'foo*bar'
+match 0 0 1 1 'foo/baz/bar' 'foo**bar'
+match 1 1 1 1 'foobazbar' 'foo**bar'
+match 1 1 1 1 'foo/baz/bar' 'foo/**/bar'
+match 1 1 0 0 'foo/baz/bar' 'foo/**/**/bar'
+match 1 1 1 1 'foo/b/a/z/bar' 'foo/**/bar'
+match 1 1 1 1 'foo/b/a/z/bar' 'foo/**/**/bar'
+match 1 1 0 0 'foo/bar' 'foo/**/bar'
+match 1 1 0 0 'foo/bar' 'foo/**/**/bar'
+match 0 0 1 1 'foo/bar' 'foo?bar'
+match 0 0 1 1 'foo/bar' 'foo[/]bar'
+match 0 0 1 1 'foo/bar' 'foo[^a-z]bar'
+match 0 0 1 1 'foo/bar' 'f[^eiu][^eiu][^eiu][^eiu][^eiu]r'
+match 1 1 1 1 'foo-bar' 'f[^eiu][^eiu][^eiu][^eiu][^eiu]r'
+match 1 1 0 0 'foo' '**/foo'
+match 1 1 1 1 'XXX/foo' '**/foo'
+match 1 1 1 1 'bar/baz/foo' '**/foo'
+match 0 0 1 1 'bar/baz/foo' '*/foo'
+match 0 0 1 1 'foo/bar/baz' '**/bar*'
+match 1 1 1 1 'deep/foo/bar/baz' '**/bar/*'
+match 0 0 1 1 'deep/foo/bar/baz/' '**/bar/*'
+match 1 1 1 1 'deep/foo/bar/baz/' '**/bar/**'
+match 0 0 0 0 'deep/foo/bar' '**/bar/*'
+match 1 1 1 1 'deep/foo/bar/' '**/bar/**'
+match 0 0 1 1 'foo/bar/baz' '**/bar**'
+match 1 1 1 1 'foo/bar/baz/x' '*/bar/**'
+match 0 0 1 1 'deep/foo/bar/baz/x' '*/bar/**'
+match 1 1 1 1 'deep/foo/bar/baz/x' '**/bar/*/*'
+
+# Various additional tests
+match 0 0 0 0 'acrt' 'a[c-c]st'
+match 1 1 1 1 'acrt' 'a[c-c]rt'
+match 0 0 0 0 ']' '[!]-]'
+match 1 1 1 1 'a' '[!]-]'
+match 0 0 0 0 '' '\'
+match 0 0 0 0 \
+ 1 1 1 1 '\' '\'
+match 0 0 0 0 'XXX/\' '*/\'
+match 1 1 1 1 'XXX/\' '*/\\'
+match 1 1 1 1 'foo' 'foo'
+match 1 1 1 1 '@foo' '@foo'
+match 0 0 0 0 'foo' '@foo'
+match 1 1 1 1 '[ab]' '\[ab]'
+match 1 1 1 1 '[ab]' '[[]ab]'
+match 1 1 1 1 '[ab]' '[[:]ab]'
+match 0 0 0 0 '[ab]' '[[::]ab]'
+match 1 1 1 1 '[ab]' '[[:digit]ab]'
+match 1 1 1 1 '[ab]' '[\[:]ab]'
+match 1 1 1 1 '?a?b' '\??\?b'
+match 1 1 1 1 'abc' '\a\b\c'
+match 0 0 0 0 \
+ E E E E 'foo' ''
+match 1 1 1 1 'foo/bar/baz/to' '**/t[o]'
+
+# Character class tests
+match 1 1 1 1 'a1B' '[[:alpha:]][[:digit:]][[:upper:]]'
+match 0 1 0 1 'a' '[[:digit:][:upper:][:space:]]'
+match 1 1 1 1 'A' '[[:digit:][:upper:][:space:]]'
+match 1 1 1 1 '1' '[[:digit:][:upper:][:space:]]'
+match 0 0 0 0 '1' '[[:digit:][:upper:][:spaci:]]'
+match 1 1 1 1 ' ' '[[:digit:][:upper:][:space:]]'
+match 0 0 0 0 '.' '[[:digit:][:upper:][:space:]]'
+match 1 1 1 1 '.' '[[:digit:][:punct:][:space:]]'
+match 1 1 1 1 '5' '[[:xdigit:]]'
+match 1 1 1 1 'f' '[[:xdigit:]]'
+match 1 1 1 1 'D' '[[:xdigit:]]'
+match 1 1 1 1 '_' '[[:alnum:][:alpha:][:blank:][:cntrl:][:digit:][:graph:][:lower:][:print:][:punct:][:space:][:upper:][:xdigit:]]'
+match 1 1 1 1 '.' '[^[:alnum:][:alpha:][:blank:][:cntrl:][:digit:][:lower:][:space:][:upper:][:xdigit:]]'
+match 1 1 1 1 '5' '[a-c[:digit:]x-z]'
+match 1 1 1 1 'b' '[a-c[:digit:]x-z]'
+match 1 1 1 1 'y' '[a-c[:digit:]x-z]'
+match 0 0 0 0 'q' '[a-c[:digit:]x-z]'
+
+# Additional tests, including some malformed wildmatch patterns
+match 1 1 1 1 ']' '[\\-^]'
+match 0 0 0 0 '[' '[\\-^]'
+match 1 1 1 1 '-' '[\-_]'
+match 1 1 1 1 ']' '[\]]'
+match 0 0 0 0 '\]' '[\]]'
+match 0 0 0 0 '\' '[\]]'
+match 0 0 0 0 'ab' 'a[]b'
+match 0 0 0 0 \
+ 1 1 1 1 'a[]b' 'a[]b'
+match 0 0 0 0 \
+ 1 1 1 1 'ab[' 'ab['
+match 0 0 0 0 'ab' '[!'
+match 0 0 0 0 'ab' '[-'
+match 1 1 1 1 '-' '[-]'
+match 0 0 0 0 '-' '[a-'
+match 0 0 0 0 '-' '[!a-'
+match 1 1 1 1 '-' '[--A]'
+match 1 1 1 1 '5' '[--A]'
+match 1 1 1 1 ' ' '[ --]'
+match 1 1 1 1 '$' '[ --]'
+match 1 1 1 1 '-' '[ --]'
+match 0 0 0 0 '0' '[ --]'
+match 1 1 1 1 '-' '[---]'
+match 1 1 1 1 '-' '[------]'
+match 0 0 0 0 'j' '[a-e-n]'
+match 1 1 1 1 '-' '[a-e-n]'
+match 1 1 1 1 'a' '[!------]'
+match 0 0 0 0 '[' '[]-a]'
+match 1 1 1 1 '^' '[]-a]'
+match 0 0 0 0 '^' '[!]-a]'
+match 1 1 1 1 '[' '[!]-a]'
+match 1 1 1 1 '^' '[a^bc]'
+match 1 1 1 1 '-b]' '[a-]b]'
+match 0 0 0 0 '\' '[\]'
+match 1 1 1 1 '\' '[\\]'
+match 0 0 0 0 '\' '[!\\]'
+match 1 1 1 1 'G' '[A-\\]'
+match 0 0 0 0 'aaabbb' 'b*a'
+match 0 0 0 0 'aabcaa' '*ba*'
+match 1 1 1 1 ',' '[,]'
+match 1 1 1 1 ',' '[\\,]'
+match 1 1 1 1 '\' '[\\,]'
+match 1 1 1 1 '-' '[,-.]'
+match 0 0 0 0 '+' '[,-.]'
+match 0 0 0 0 '-.]' '[,-.]'
+match 1 1 1 1 '2' '[\1-\3]'
+match 1 1 1 1 '3' '[\1-\3]'
+match 0 0 0 0 '4' '[\1-\3]'
+match 1 1 1 1 '\' '[[-\]]'
+match 1 1 1 1 '[' '[[-\]]'
+match 1 1 1 1 ']' '[[-\]]'
+match 0 0 0 0 '-' '[[-\]]'
+
+# Test recursion
+match 1 1 1 1 '-adobe-courier-bold-o-normal--12-120-75-75-m-70-iso8859-1' '-*-*-*-*-*-*-12-*-*-*-m-*-*-*'
+match 0 0 0 0 '-adobe-courier-bold-o-normal--12-120-75-75-X-70-iso8859-1' '-*-*-*-*-*-*-12-*-*-*-m-*-*-*'
+match 0 0 0 0 '-adobe-courier-bold-o-normal--12-120-75-75-/-70-iso8859-1' '-*-*-*-*-*-*-12-*-*-*-m-*-*-*'
+match 1 1 1 1 'XXX/adobe/courier/bold/o/normal//12/120/75/75/m/70/iso8859/1' 'XXX/*/*/*/*/*/*/12/*/*/*/m/*/*/*'
+match 0 0 0 0 'XXX/adobe/courier/bold/o/normal//12/120/75/75/X/70/iso8859/1' 'XXX/*/*/*/*/*/*/12/*/*/*/m/*/*/*'
+match 1 1 1 1 'abcd/abcdefg/abcdefghijk/abcdefghijklmnop.txt' '**/*a*b*g*n*t'
+match 0 0 0 0 'abcd/abcdefg/abcdefghijk/abcdefghijklmnop.txtz' '**/*a*b*g*n*t'
+match 0 0 0 0 foo '*/*/*'
+match 0 0 0 0 foo/bar '*/*/*'
+match 1 1 1 1 foo/bba/arr '*/*/*'
+match 0 0 1 1 foo/bb/aa/rr '*/*/*'
+match 1 1 1 1 foo/bb/aa/rr '**/**/**'
+match 1 1 1 1 abcXdefXghi '*X*i'
+match 0 0 1 1 ab/cXd/efXg/hi '*X*i'
+match 1 1 1 1 ab/cXd/efXg/hi '*/*X*/*/*i'
+match 1 1 1 1 ab/cXd/efXg/hi '**/*X*/**/*i'
+
+# Extra pathmatch tests
+match 0 0 0 0 foo fo
+match 1 1 1 1 foo/bar foo/bar
+match 1 1 1 1 foo/bar 'foo/*'
+match 0 0 1 1 foo/bba/arr 'foo/*'
+match 1 1 1 1 foo/bba/arr 'foo/**'
+match 0 0 1 1 foo/bba/arr 'foo*'
+match 0 0 1 1 \
+ 1 1 1 1 foo/bba/arr 'foo**'
+match 0 0 1 1 foo/bba/arr 'foo/*arr'
+match 0 0 1 1 foo/bba/arr 'foo/**arr'
+match 0 0 0 0 foo/bba/arr 'foo/*z'
+match 0 0 0 0 foo/bba/arr 'foo/**z'
+match 0 0 1 1 foo/bar 'foo?bar'
+match 0 0 1 1 foo/bar 'foo[/]bar'
+match 0 0 1 1 foo/bar 'foo[^a-z]bar'
+match 0 0 1 1 ab/cXd/efXg/hi '*Xg*i'
+
+# Extra case-sensitivity tests
+match 0 1 0 1 'a' '[A-Z]'
+match 1 1 1 1 'A' '[A-Z]'
+match 0 1 0 1 'A' '[a-z]'
+match 1 1 1 1 'a' '[a-z]'
+match 0 1 0 1 'a' '[[:upper:]]'
+match 1 1 1 1 'A' '[[:upper:]]'
+match 0 1 0 1 'A' '[[:lower:]]'
+match 1 1 1 1 'a' '[[:lower:]]'
+match 0 1 0 1 'A' '[B-Za]'
+match 1 1 1 1 'a' '[B-Za]'
+match 0 1 0 1 'A' '[B-a]'
+match 1 1 1 1 'a' '[B-a]'
+match 0 1 0 1 'z' '[Z-y]'
+match 1 1 1 1 'Z' '[Z-y]'
+
+test_expect_success 'matching does not exhibit exponential behavior' '
+ {
+ test-tool wildmatch wildmatch \
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab \
+ "*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a*a" &
+ pid=$!
+ } &&
+ sleep 2 &&
+ ! kill $!
+'
+
+test_done
diff --git a/t/t3100-ls-tree-restrict.sh b/t/t3100-ls-tree-restrict.sh
new file mode 100755
index 0000000..436de44
--- /dev/null
+++ b/t/t3100-ls-tree-restrict.sh
@@ -0,0 +1,167 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+test_description='git ls-tree test.
+
+This test runs git ls-tree with the following in a tree.
+
+ path0 - a file
+ path1 - a symlink
+ path2/foo - a file in a directory
+ path2/bazbo - a symlink in a directory
+ path2/baz/b - a file in a directory in a directory
+
+The new path restriction code should do the right thing for path2 and
+path2/baz. Also path0/ should snow nothing.
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success \
+ 'setup' \
+ 'mkdir path2 path2/baz &&
+ echo Hi >path0 &&
+ test_ln_s_add path0 path1 &&
+ test_ln_s_add ../path1 path2/bazbo &&
+ echo Lo >path2/foo &&
+ echo Mi >path2/baz/b &&
+ find path? \( -type f -o -type l \) -print |
+ xargs git update-index --add &&
+ tree=$(git write-tree) &&
+ echo $tree'
+
+test_output () {
+ sed -e "s/ $OID_REGEX / X /" <current >check
+ test_cmp expected check
+}
+
+test_expect_success \
+ 'ls-tree plain' \
+ 'git ls-tree $tree >current &&
+ cat >expected <<\EOF &&
+100644 blob X path0
+120000 blob X path1
+040000 tree X path2
+EOF
+ test_output'
+
+test_expect_success \
+ 'ls-tree recursive' \
+ 'git ls-tree -r $tree >current &&
+ cat >expected <<\EOF &&
+100644 blob X path0
+120000 blob X path1
+100644 blob X path2/baz/b
+120000 blob X path2/bazbo
+100644 blob X path2/foo
+EOF
+ test_output'
+
+test_expect_success \
+ 'ls-tree recursive with -t' \
+ 'git ls-tree -r -t $tree >current &&
+ cat >expected <<\EOF &&
+100644 blob X path0
+120000 blob X path1
+040000 tree X path2
+040000 tree X path2/baz
+100644 blob X path2/baz/b
+120000 blob X path2/bazbo
+100644 blob X path2/foo
+EOF
+ test_output'
+
+test_expect_success \
+ 'ls-tree recursive with -d' \
+ 'git ls-tree -r -d $tree >current &&
+ cat >expected <<\EOF &&
+040000 tree X path2
+040000 tree X path2/baz
+EOF
+ test_output'
+
+test_expect_success \
+ 'ls-tree filtered with path' \
+ 'git ls-tree $tree path >current &&
+ cat >expected <<\EOF &&
+EOF
+ test_output'
+
+
+# it used to be path1 and then path0, but with pathspec semantics
+# they are shown in canonical order.
+test_expect_success \
+ 'ls-tree filtered with path1 path0' \
+ 'git ls-tree $tree path1 path0 >current &&
+ cat >expected <<\EOF &&
+100644 blob X path0
+120000 blob X path1
+EOF
+ test_output'
+
+test_expect_success \
+ 'ls-tree filtered with path0/' \
+ 'git ls-tree $tree path0/ >current &&
+ cat >expected <<\EOF &&
+EOF
+ test_output'
+
+# It used to show path2 and its immediate children but
+# with pathspec semantics it shows only path2
+test_expect_success \
+ 'ls-tree filtered with path2' \
+ 'git ls-tree $tree path2 >current &&
+ cat >expected <<\EOF &&
+040000 tree X path2
+EOF
+ test_output'
+
+# ... and path2/ shows the children.
+test_expect_success \
+ 'ls-tree filtered with path2/' \
+ 'git ls-tree $tree path2/ >current &&
+ cat >expected <<\EOF &&
+040000 tree X path2/baz
+120000 blob X path2/bazbo
+100644 blob X path2/foo
+EOF
+ test_output'
+
+# The same change -- exact match does not show children of
+# path2/baz
+test_expect_success \
+ 'ls-tree filtered with path2/baz' \
+ 'git ls-tree $tree path2/baz >current &&
+ cat >expected <<\EOF &&
+040000 tree X path2/baz
+EOF
+ test_output'
+
+test_expect_success \
+ 'ls-tree filtered with path2/bak' \
+ 'git ls-tree $tree path2/bak >current &&
+ cat >expected <<\EOF &&
+EOF
+ test_output'
+
+test_expect_success \
+ 'ls-tree -t filtered with path2/bak' \
+ 'git ls-tree -t $tree path2/bak >current &&
+ cat >expected <<\EOF &&
+040000 tree X path2
+EOF
+ test_output'
+
+test_expect_success \
+ 'ls-tree with one path a prefix of the other' \
+ 'git ls-tree $tree path2/baz path2/bazbo >current &&
+ cat >expected <<\EOF &&
+040000 tree X path2/baz
+120000 blob X path2/bazbo
+EOF
+ test_output'
+
+test_done
diff --git a/t/t3101-ls-tree-dirname.sh b/t/t3101-ls-tree-dirname.sh
new file mode 100755
index 0000000..5af2dac
--- /dev/null
+++ b/t/t3101-ls-tree-dirname.sh
@@ -0,0 +1,242 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+# Copyright (c) 2005 Robert Fitzsimons
+#
+
+test_description='git ls-tree directory and filenames handling.
+
+This test runs git ls-tree with the following in a tree.
+
+ 1.txt - a file
+ 2.txt - a file
+ path0/a/b/c/1.txt - a file in a directory
+ path1/b/c/1.txt - a file in a directory
+ path2/1.txt - a file in a directory
+ path3/1.txt - a file in a directory
+ path3/2.txt - a file in a directory
+
+Test the handling of multiple directories which have matching file
+entries. Also test odd filename and missing entries handling.
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ echo 111 >1.txt &&
+ echo 222 >2.txt &&
+ mkdir path0 path0/a path0/a/b path0/a/b/c &&
+ echo 111 >path0/a/b/c/1.txt &&
+ mkdir path1 path1/b path1/b/c &&
+ echo 111 >path1/b/c/1.txt &&
+ mkdir path2 &&
+ echo 111 >path2/1.txt &&
+ mkdir path3 &&
+ echo 111 >path3/1.txt &&
+ echo 222 >path3/2.txt &&
+ find *.txt path* \( -type f -o -type l \) -print |
+ xargs git update-index --add &&
+ tree=$(git write-tree) &&
+ echo $tree
+'
+
+test_output () {
+ sed -e "s/ $OID_REGEX / X /" <current >check &&
+ test_cmp expected check
+}
+
+test_expect_success 'ls-tree plain' '
+ git ls-tree $tree >current &&
+ cat >expected <<\EOF &&
+100644 blob X 1.txt
+100644 blob X 2.txt
+040000 tree X path0
+040000 tree X path1
+040000 tree X path2
+040000 tree X path3
+EOF
+ test_output
+'
+
+# Recursive does not show tree nodes anymore...
+test_expect_success 'ls-tree recursive' '
+ git ls-tree -r $tree >current &&
+ cat >expected <<\EOF &&
+100644 blob X 1.txt
+100644 blob X 2.txt
+100644 blob X path0/a/b/c/1.txt
+100644 blob X path1/b/c/1.txt
+100644 blob X path2/1.txt
+100644 blob X path3/1.txt
+100644 blob X path3/2.txt
+EOF
+ test_output
+'
+
+test_expect_success 'ls-tree filter 1.txt' '
+ git ls-tree $tree 1.txt >current &&
+ cat >expected <<\EOF &&
+100644 blob X 1.txt
+EOF
+ test_output
+'
+
+test_expect_success 'ls-tree filter path1/b/c/1.txt' '
+ git ls-tree $tree path1/b/c/1.txt >current &&
+ cat >expected <<\EOF &&
+100644 blob X path1/b/c/1.txt
+EOF
+ test_output
+'
+
+test_expect_success 'ls-tree filter all 1.txt files' '
+ git ls-tree $tree 1.txt path0/a/b/c/1.txt \
+ path1/b/c/1.txt path2/1.txt path3/1.txt >current &&
+ cat >expected <<\EOF &&
+100644 blob X 1.txt
+100644 blob X path0/a/b/c/1.txt
+100644 blob X path1/b/c/1.txt
+100644 blob X path2/1.txt
+100644 blob X path3/1.txt
+EOF
+ test_output
+'
+
+# I am not so sure about this one after ls-tree doing pathspec match.
+# Having both path0/a and path0/a/b/c makes path0/a redundant, and
+# it behaves as if path0/a/b/c, path1/b/c, path2 and path3 are specified.
+test_expect_success 'ls-tree filter directories' '
+ git ls-tree $tree path3 path2 path0/a/b/c path1/b/c path0/a >current &&
+ cat >expected <<\EOF &&
+040000 tree X path0/a/b/c
+040000 tree X path1/b/c
+040000 tree X path2
+040000 tree X path3
+EOF
+ test_output
+'
+
+# Again, duplicates are filtered away so this is equivalent to
+# having 1.txt and path3
+test_expect_success 'ls-tree filter odd names' '
+ git ls-tree $tree 1.txt ./1.txt .//1.txt \
+ path3/1.txt path3/./1.txt path3 path3// >current &&
+ cat >expected <<\EOF &&
+100644 blob X 1.txt
+100644 blob X path3/1.txt
+100644 blob X path3/2.txt
+EOF
+ test_output
+'
+
+test_expect_success 'ls-tree filter missing files and extra slashes' '
+ git ls-tree $tree 1.txt/ abc.txt \
+ path3//23.txt path3/2.txt/// >current &&
+ >expected &&
+ test_output
+'
+
+test_expect_success 'ls-tree filter is leading path match' '
+ git ls-tree $tree pa path3/a >current &&
+ >expected &&
+ test_output
+'
+
+test_expect_success 'ls-tree --full-name' '
+ (
+ cd path0 &&
+ git ls-tree --full-name $tree a
+ ) >current &&
+ cat >expected <<\EOF &&
+040000 tree X path0/a
+EOF
+ test_output
+'
+
+test_expect_success 'ls-tree --no-full-name' '
+ git -C path0 ls-tree --no-full-name $tree a >current &&
+ cat >expected <<-EOF &&
+ 040000 tree X a
+ EOF
+ test_output
+'
+
+test_expect_success 'ls-tree --full-tree' '
+ (
+ cd path1/b/c &&
+ git ls-tree --full-tree $tree
+ ) >current &&
+ cat >expected <<\EOF &&
+100644 blob X 1.txt
+100644 blob X 2.txt
+040000 tree X path0
+040000 tree X path1
+040000 tree X path2
+040000 tree X path3
+EOF
+ test_output
+'
+
+test_expect_success 'ls-tree --full-tree -r' '
+ (
+ cd path3/ &&
+ git ls-tree --full-tree -r $tree
+ ) >current &&
+ cat >expected <<\EOF &&
+100644 blob X 1.txt
+100644 blob X 2.txt
+100644 blob X path0/a/b/c/1.txt
+100644 blob X path1/b/c/1.txt
+100644 blob X path2/1.txt
+100644 blob X path3/1.txt
+100644 blob X path3/2.txt
+EOF
+ test_output
+'
+
+test_expect_success 'ls-tree --abbrev=5' '
+ git ls-tree --abbrev=5 $tree >current &&
+ sed -e "s/ $_x05[0-9a-f]* / X /" <current >check &&
+ cat >expected <<\EOF &&
+100644 blob X 1.txt
+100644 blob X 2.txt
+040000 tree X path0
+040000 tree X path1
+040000 tree X path2
+040000 tree X path3
+EOF
+ test_cmp expected check
+'
+
+for opt in --name-only --name-status
+do
+ test_expect_success "ls-tree $opt" '
+ git ls-tree $opt $tree >current &&
+ cat >expected <<-\EOF &&
+ 1.txt
+ 2.txt
+ path0
+ path1
+ path2
+ path3
+ EOF
+ test_output
+ '
+
+ test_expect_success "ls-tree $opt -r" '
+ git ls-tree $opt -r $tree >current &&
+ cat >expected <<-\EOF &&
+ 1.txt
+ 2.txt
+ path0/a/b/c/1.txt
+ path1/b/c/1.txt
+ path2/1.txt
+ path3/1.txt
+ path3/2.txt
+ EOF
+ test_output
+ '
+done
+
+test_done
diff --git a/t/t3102-ls-tree-wildcards.sh b/t/t3102-ls-tree-wildcards.sh
new file mode 100755
index 0000000..3942db2
--- /dev/null
+++ b/t/t3102-ls-tree-wildcards.sh
@@ -0,0 +1,37 @@
+#!/bin/sh
+
+test_description='ls-tree with(out) globs'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ mkdir a aa "a[a]" &&
+ touch a/one aa/two "a[a]/three" &&
+ git add a/one aa/two "a[a]/three" &&
+ git commit -m test
+'
+
+test_expect_success 'ls-tree a[a] matches literally' '
+ cat >expect <<-EOF &&
+ 100644 blob $EMPTY_BLOB a[a]/three
+ EOF
+ git ls-tree -r HEAD "a[a]" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'ls-tree outside prefix' '
+ cat >expect <<-EOF &&
+ 100644 blob $EMPTY_BLOB ../a[a]/three
+ EOF
+ ( cd aa && git ls-tree -r HEAD "../a[a]" ) >actual &&
+ test_cmp expect actual
+'
+
+test_expect_failure 'ls-tree does not yet support negated pathspec' '
+ git ls-files ":(exclude)a" "a*" >expect &&
+ git ls-tree --name-only -r HEAD ":(exclude)a" "a*" >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t3103-ls-tree-misc.sh b/t/t3103-ls-tree-misc.sh
new file mode 100755
index 0000000..81c6343
--- /dev/null
+++ b/t/t3103-ls-tree-misc.sh
@@ -0,0 +1,41 @@
+#!/bin/sh
+
+test_description='
+Miscellaneous tests for git ls-tree.
+
+ 1. git ls-tree fails in presence of tree damage.
+
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ mkdir a &&
+ touch a/one &&
+ git add a/one &&
+ git commit -m test
+'
+
+test_expect_success 'ls-tree fails with non-zero exit code on broken tree' '
+ tree=$(git rev-parse HEAD:a) &&
+ rm -f .git/objects/$(echo $tree | sed -e "s,^\(..\),\1/,") &&
+ test_must_fail git ls-tree -r HEAD
+'
+
+for opts in \
+ "--long --name-only" \
+ "--name-only --name-status" \
+ "--name-status --object-only" \
+ "--object-only --long"
+do
+ test_expect_success "usage: incompatible options: $opts" '
+ test_expect_code 129 git ls-tree $opts $tree
+ '
+
+ one_opt=$(echo "$opts" | cut -d' ' -f1)
+ test_expect_success "usage: incompatible options: $one_opt and --format" '
+ test_expect_code 129 git ls-tree $one_opt --format=fmt $tree
+ '
+done
+test_done
diff --git a/t/t3104-ls-tree-format.sh b/t/t3104-ls-tree-format.sh
new file mode 100755
index 0000000..3adb206
--- /dev/null
+++ b/t/t3104-ls-tree-format.sh
@@ -0,0 +1,80 @@
+#!/bin/sh
+
+test_description='ls-tree --format'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-t3100.sh
+
+test_expect_success 'ls-tree --format usage' '
+ test_expect_code 129 git ls-tree --format=fmt -l HEAD &&
+ test_expect_code 129 git ls-tree --format=fmt --name-only HEAD &&
+ test_expect_code 129 git ls-tree --format=fmt --name-status HEAD
+'
+
+test_expect_success 'setup' '
+ setup_basic_ls_tree_data
+'
+
+test_ls_tree_format () {
+ format=$1 &&
+ opts=$2 &&
+ fmtopts=$3 &&
+
+ test_expect_success "ls-tree '--format=<$format>' is like options '$opts $fmtopts'" '
+ git ls-tree $opts -r HEAD >expect &&
+ git ls-tree --format="$format" -r $fmtopts HEAD >actual &&
+ test_cmp expect actual
+ '
+
+ test_expect_success "ls-tree '--format=<$format>' on optimized v.s. non-optimized path" '
+ git ls-tree --format="$format" -r $fmtopts HEAD >expect &&
+ git ls-tree --format="> $format" -r $fmtopts HEAD >actual.raw &&
+ sed "s/^> //" >actual <actual.raw &&
+ test_cmp expect actual
+ '
+}
+
+test_expect_success "ls-tree --format='%(path) %(path) %(path)' HEAD top-file" '
+ git ls-tree --format="%(path) %(path) %(path)" HEAD top-file.t >actual &&
+ echo top-file.t top-file.t top-file.t >expect &&
+ test_cmp expect actual
+'
+
+test_ls_tree_format \
+ "%(objectmode) %(objecttype) %(objectname)%x09%(path)" \
+ ""
+
+test_ls_tree_format \
+ "%(objectmode) %(objecttype) %(objectname) %(objectsize:padded)%x09%(path)" \
+ "--long"
+
+test_ls_tree_format \
+ "%(path)" \
+ "--name-only"
+
+test_ls_tree_format \
+ "%(objectname)" \
+ "--object-only"
+
+test_ls_tree_format \
+ "%(objectname)" \
+ "--object-only --abbrev" \
+ "--abbrev"
+
+test_ls_tree_format \
+ "%(objectmode) %(objecttype) %(objectname)%x09%(path)" \
+ "-t" \
+ "-t"
+
+test_ls_tree_format \
+ "%(objectmode) %(objecttype) %(objectname)%x09%(path)" \
+ "--full-name" \
+ "--full-name"
+
+test_ls_tree_format \
+ "%(objectmode) %(objecttype) %(objectname)%x09%(path)" \
+ "--full-tree" \
+ "--full-tree"
+
+test_done
diff --git a/t/t3105-ls-tree-output.sh b/t/t3105-ls-tree-output.sh
new file mode 100755
index 0000000..ce2391e
--- /dev/null
+++ b/t/t3105-ls-tree-output.sh
@@ -0,0 +1,192 @@
+#!/bin/sh
+
+test_description='ls-tree output'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-t3100.sh
+
+test_expect_success 'ls-tree --format usage' '
+ test_expect_code 129 git ls-tree --format=fmt -l HEAD &&
+ test_expect_code 129 git ls-tree --format=fmt --name-only HEAD &&
+ test_expect_code 129 git ls-tree --format=fmt --name-status HEAD
+'
+
+test_expect_success 'setup' '
+ setup_basic_ls_tree_data
+'
+
+test_ls_tree_format_mode_output () {
+ local opts="$1" &&
+ shift &&
+ cat >expect &&
+
+ while test $# -gt 0
+ do
+ local mode="$1" &&
+ shift &&
+
+ test_expect_success "'ls-tree $opts${mode:+ $mode}' output" '
+ git ls-tree ${mode:+$mode }$opts HEAD >actual &&
+ test_cmp expect actual
+ '
+
+ case "$opts" in
+ --full-tree)
+ test_expect_success "'ls-tree $opts${mode:+ $mode}' output (via subdir, fails)" '
+ test_must_fail git -C dir ls-tree --full-name ${mode:+$mode }$opts HEAD -- ../
+ '
+ ;;
+ *)
+ test_expect_success "'ls-tree $opts${mode:+ $mode}' output (via subdir)" '
+ git -C dir ls-tree --full-name ${mode:+$mode }$opts HEAD -- ../ >actual &&
+ test_cmp expect actual
+ '
+ ;;
+ esac
+ done
+}
+
+# test exact output of option (none, --long, ...) and mode (none and
+# -d, -r -t) and combinations
+test_expect_success 'setup: HEAD_* variables' '
+ HEAD_gitmodules=$(git rev-parse HEAD:.gitmodules) &&
+ HEAD_dir=$(git rev-parse HEAD:dir) &&
+ HEAD_top_file=$(git rev-parse HEAD:top-file.t) &&
+ HEAD_submodule=$(git rev-parse HEAD:submodule) &&
+ HEAD_dir_sub_file=$(git rev-parse HEAD:dir/sub-file.t)
+'
+## opt =
+test_ls_tree_format_mode_output "" "" "-t" <<-EOF
+ 100644 blob $HEAD_gitmodules .gitmodules
+ 040000 tree $HEAD_dir dir
+ 160000 commit $HEAD_submodule submodule
+ 100644 blob $HEAD_top_file top-file.t
+ EOF
+test_ls_tree_format_mode_output "" "-d" <<-EOF
+ 040000 tree $HEAD_dir dir
+ 160000 commit $HEAD_submodule submodule
+ EOF
+test_ls_tree_format_mode_output "" "-r" <<-EOF
+ 100644 blob $HEAD_gitmodules .gitmodules
+ 100644 blob $HEAD_dir_sub_file dir/sub-file.t
+ 160000 commit $HEAD_submodule submodule
+ 100644 blob $HEAD_top_file top-file.t
+ EOF
+## opt = --long
+test_ls_tree_format_mode_output "--long" "" "-t" <<-EOF
+ 100644 blob $HEAD_gitmodules 61 .gitmodules
+ 040000 tree $HEAD_dir - dir
+ 160000 commit $HEAD_submodule - submodule
+ 100644 blob $HEAD_top_file 9 top-file.t
+ EOF
+test_ls_tree_format_mode_output "--long" "-d" <<-EOF
+ 040000 tree $HEAD_dir - dir
+ 160000 commit $HEAD_submodule - submodule
+ EOF
+test_ls_tree_format_mode_output "--long" "-r" <<-EOF
+ 100644 blob $HEAD_gitmodules 61 .gitmodules
+ 100644 blob $HEAD_dir_sub_file 13 dir/sub-file.t
+ 160000 commit $HEAD_submodule - submodule
+ 100644 blob $HEAD_top_file 9 top-file.t
+ EOF
+## opt = --name-only
+test_ls_tree_format_mode_output "--name-only" "" "-t" <<-EOF
+ .gitmodules
+ dir
+ submodule
+ top-file.t
+ EOF
+test_ls_tree_format_mode_output "--name-only" "-d" <<-EOF
+ dir
+ submodule
+ EOF
+test_ls_tree_format_mode_output "--name-only" "-r" <<-EOF
+ .gitmodules
+ dir/sub-file.t
+ submodule
+ top-file.t
+ EOF
+## opt = --object-only
+test_ls_tree_format_mode_output "--object-only" "" "-t" <<-EOF
+ $HEAD_gitmodules
+ $HEAD_dir
+ $HEAD_submodule
+ $HEAD_top_file
+ EOF
+test_ls_tree_format_mode_output "--object-only" "-d" <<-EOF
+ $HEAD_dir
+ $HEAD_submodule
+ EOF
+test_ls_tree_format_mode_output "--object-only" "-r" <<-EOF
+ $HEAD_gitmodules
+ $HEAD_dir_sub_file
+ $HEAD_submodule
+ $HEAD_top_file
+ EOF
+## opt = --object-only --abbrev
+test_expect_success 'setup: HEAD_short_* variables' '
+ HEAD_short_gitmodules=$(git rev-parse --short HEAD:.gitmodules) &&
+ HEAD_short_dir=$(git rev-parse --short HEAD:dir) &&
+ HEAD_short_top_file=$(git rev-parse --short HEAD:top-file.t) &&
+ HEAD_short_submodule=$(git rev-parse --short HEAD:submodule) &&
+ HEAD_short_dir_sub_file=$(git rev-parse --short HEAD:dir/sub-file.t)
+'
+test_ls_tree_format_mode_output "--object-only --abbrev" "" "-t" <<-EOF
+ $HEAD_short_gitmodules
+ $HEAD_short_dir
+ $HEAD_short_submodule
+ $HEAD_short_top_file
+ EOF
+test_ls_tree_format_mode_output "--object-only --abbrev" "-d" <<-EOF
+ $HEAD_short_dir
+ $HEAD_short_submodule
+ EOF
+test_ls_tree_format_mode_output "--object-only --abbrev" "-r" <<-EOF
+ $HEAD_short_gitmodules
+ $HEAD_short_dir_sub_file
+ $HEAD_short_submodule
+ $HEAD_short_top_file
+ EOF
+## opt = --full-name
+test_ls_tree_format_mode_output "--full-name" "" <<-EOF
+ 100644 blob $HEAD_gitmodules .gitmodules
+ 040000 tree $HEAD_dir dir
+ 160000 commit $HEAD_submodule submodule
+ 100644 blob $HEAD_top_file top-file.t
+ EOF
+test_ls_tree_format_mode_output "--full-name" "-d" <<-EOF
+ 040000 tree $HEAD_dir dir
+ 160000 commit $HEAD_submodule submodule
+ EOF
+test_ls_tree_format_mode_output "--full-name" "-r" <<-EOF
+ 100644 blob $HEAD_gitmodules .gitmodules
+ 100644 blob $HEAD_dir_sub_file dir/sub-file.t
+ 160000 commit $HEAD_submodule submodule
+ 100644 blob $HEAD_top_file top-file.t
+ EOF
+test_ls_tree_format_mode_output "--full-name" "-t" <<-EOF
+ 100644 blob $HEAD_gitmodules .gitmodules
+ 040000 tree $HEAD_dir dir
+ 160000 commit $HEAD_submodule submodule
+ 100644 blob $HEAD_top_file top-file.t
+ EOF
+## opt = --full-tree
+test_ls_tree_format_mode_output "--full-tree" "" "-t" <<-EOF
+ 100644 blob $HEAD_gitmodules .gitmodules
+ 040000 tree $HEAD_dir dir
+ 160000 commit $HEAD_submodule submodule
+ 100644 blob $HEAD_top_file top-file.t
+ EOF
+test_ls_tree_format_mode_output "--full-tree" "-d" <<-EOF
+ 040000 tree $HEAD_dir dir
+ 160000 commit $HEAD_submodule submodule
+ EOF
+test_ls_tree_format_mode_output "--full-tree" "-r" <<-EOF
+ 100644 blob $HEAD_gitmodules .gitmodules
+ 100644 blob $HEAD_dir_sub_file dir/sub-file.t
+ 160000 commit $HEAD_submodule submodule
+ 100644 blob $HEAD_top_file top-file.t
+ EOF
+
+test_done
diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
new file mode 100755
index 0000000..c54fd9e
--- /dev/null
+++ b/t/t3200-branch.sh
@@ -0,0 +1,1663 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Amos Waterland
+#
+
+test_description='git branch assorted tests'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-rebase.sh
+
+test_expect_success 'prepare a trivial repository' '
+ echo Hello >A &&
+ git update-index --add A &&
+ git commit -m "Initial commit." &&
+ git branch -M main &&
+ echo World >>A &&
+ git update-index --add A &&
+ git commit -m "Second commit." &&
+ HEAD=$(git rev-parse --verify HEAD)
+'
+
+test_expect_success 'git branch --help should not have created a bogus branch' '
+ test_might_fail git branch --man --help </dev/null >/dev/null 2>&1 &&
+ test_ref_missing refs/heads/--help
+'
+
+test_expect_success 'branch -h in broken repository' '
+ mkdir broken &&
+ (
+ cd broken &&
+ git init -b main &&
+ >.git/refs/heads/main &&
+ test_expect_code 129 git branch -h >usage 2>&1
+ ) &&
+ test_grep "[Uu]sage" broken/usage
+'
+
+test_expect_success 'git branch abc should create a branch' '
+ git branch abc &&
+ test_ref_exists refs/heads/abc
+'
+
+test_expect_success 'git branch abc should fail when abc exists' '
+ test_must_fail git branch abc
+'
+
+test_expect_success 'git branch --force abc should fail when abc is checked out' '
+ test_when_finished git switch main &&
+ git switch abc &&
+ test_must_fail git branch --force abc HEAD~1
+'
+
+test_expect_success 'git branch --force abc should succeed when abc exists' '
+ git rev-parse HEAD~1 >expect &&
+ git branch --force abc HEAD~1 &&
+ git rev-parse abc >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git branch a/b/c should create a branch' '
+ git branch a/b/c &&
+ test_ref_exists refs/heads/a/b/c
+'
+
+test_expect_success 'git branch mb main... should create a branch' '
+ git branch mb main... &&
+ test_ref_exists refs/heads/mb
+'
+
+test_expect_success 'git branch HEAD should fail' '
+ test_must_fail git branch HEAD
+'
+
+cat >expect <<EOF
+$ZERO_OID $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from main
+EOF
+test_expect_success 'git branch --create-reflog d/e/f should create a branch and a log' '
+ GIT_COMMITTER_DATE="2005-05-26 23:30" \
+ git -c core.logallrefupdates=false branch --create-reflog d/e/f &&
+ test_ref_exists refs/heads/d/e/f &&
+ test_path_is_file .git/logs/refs/heads/d/e/f &&
+ test_cmp expect .git/logs/refs/heads/d/e/f
+'
+
+test_expect_success 'git branch -d d/e/f should delete a branch and a log' '
+ git branch -d d/e/f &&
+ test_ref_missing refs/heads/d/e/f &&
+ test_must_fail git reflog exists refs/heads/d/e/f
+'
+
+test_expect_success 'git branch j/k should work after branch j has been deleted' '
+ git branch j &&
+ git branch -d j &&
+ git branch j/k
+'
+
+test_expect_success 'git branch l should work after branch l/m has been deleted' '
+ git branch l/m &&
+ git branch -d l/m &&
+ git branch l
+'
+
+test_expect_success 'git branch -m dumps usage' '
+ test_expect_code 128 git branch -m 2>err &&
+ test_grep "branch name required" err
+'
+
+test_expect_success 'git branch -m m broken_symref should work' '
+ test_when_finished "git branch -D broken_symref" &&
+ git branch --create-reflog m &&
+ git symbolic-ref refs/heads/broken_symref refs/heads/i_am_broken &&
+ git branch -m m broken_symref &&
+ git reflog exists refs/heads/broken_symref &&
+ test_must_fail git reflog exists refs/heads/i_am_broken
+'
+
+test_expect_success 'git branch -m m m/m should work' '
+ git branch --create-reflog m &&
+ git branch -m m m/m &&
+ git reflog exists refs/heads/m/m
+'
+
+test_expect_success 'git branch -m n/n n should work' '
+ git branch --create-reflog n/n &&
+ git branch -m n/n n &&
+ git reflog exists refs/heads/n
+'
+
+# The topmost entry in reflog for branch bbb is about branch creation.
+# Hence, we compare bbb@{1} (instead of bbb@{0}) with aaa@{0}.
+
+test_expect_success 'git branch -m bbb should rename checked out branch' '
+ test_when_finished git branch -D bbb &&
+ test_when_finished git checkout main &&
+ git checkout -b aaa &&
+ git commit --allow-empty -m "a new commit" &&
+ git rev-parse aaa@{0} >expect &&
+ git branch -m bbb &&
+ git rev-parse bbb@{1} >actual &&
+ test_cmp expect actual &&
+ git symbolic-ref HEAD >actual &&
+ echo refs/heads/bbb >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'renaming checked out branch works with d/f conflict' '
+ test_when_finished "git branch -D foo/bar || git branch -D foo" &&
+ test_when_finished git checkout main &&
+ git checkout -b foo &&
+ git branch -m foo/bar &&
+ git symbolic-ref HEAD >actual &&
+ echo refs/heads/foo/bar >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git branch -m o/o o should fail when o/p exists' '
+ git branch o/o &&
+ git branch o/p &&
+ test_must_fail git branch -m o/o o
+'
+
+test_expect_success 'git branch -m o/q o/p should fail when o/p exists' '
+ git branch o/q &&
+ test_must_fail git branch -m o/q o/p
+'
+
+test_expect_success 'git branch -M o/q o/p should work when o/p exists' '
+ git branch -M o/q o/p
+'
+
+test_expect_success 'git branch -m -f o/q o/p should work when o/p exists' '
+ git branch o/q &&
+ git branch -m -f o/q o/p
+'
+
+test_expect_success 'git branch -m q r/q should fail when r exists' '
+ git branch q &&
+ git branch r &&
+ test_must_fail git branch -m q r/q
+'
+
+test_expect_success 'git branch -M foo bar should fail when bar is checked out' '
+ git branch bar &&
+ git checkout -b foo &&
+ test_must_fail git branch -M bar foo
+'
+
+test_expect_success 'git branch -M foo bar should fail when bar is checked out in worktree' '
+ git branch -f bar &&
+ test_when_finished "git worktree remove wt && git branch -D wt" &&
+ git worktree add wt &&
+ test_must_fail git branch -M bar wt
+'
+
+test_expect_success 'git branch -M baz bam should succeed when baz is checked out' '
+ git checkout -b baz &&
+ git branch bam &&
+ git branch -M baz bam &&
+ test $(git rev-parse --abbrev-ref HEAD) = bam
+'
+
+test_expect_success 'git branch -M baz bam should add entries to .git/logs/HEAD' '
+ msg="Branch: renamed refs/heads/baz to refs/heads/bam" &&
+ grep " $ZERO_OID.*$msg$" .git/logs/HEAD &&
+ grep "^$ZERO_OID.*$msg$" .git/logs/HEAD
+'
+
+test_expect_success 'git branch -M should leave orphaned HEAD alone' '
+ git init -b main orphan &&
+ (
+ cd orphan &&
+ test_commit initial &&
+ git checkout --orphan lonely &&
+ grep lonely .git/HEAD &&
+ test_ref_missing refs/head/lonely &&
+ git branch -M main mistress &&
+ grep lonely .git/HEAD
+ )
+'
+
+test_expect_success 'resulting reflog can be shown by log -g' '
+ oid=$(git rev-parse HEAD) &&
+ cat >expect <<-EOF &&
+ HEAD@{0} $oid $msg
+ HEAD@{2} $oid checkout: moving from foo to baz
+ EOF
+ git log -g --format="%gd %H %gs" -2 HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git branch -M baz bam should succeed when baz is checked out as linked working tree' '
+ git checkout main &&
+ git worktree add -b baz bazdir &&
+ git worktree add -f bazdir2 baz &&
+ git branch -M baz bam &&
+ test $(git -C bazdir rev-parse --abbrev-ref HEAD) = bam &&
+ test $(git -C bazdir2 rev-parse --abbrev-ref HEAD) = bam &&
+ rm -r bazdir bazdir2 &&
+ git worktree prune
+'
+
+test_expect_success 'git branch -M fails if updating any linked working tree fails' '
+ git worktree add -b baz bazdir1 &&
+ git worktree add -f bazdir2 baz &&
+ touch .git/worktrees/bazdir1/HEAD.lock &&
+ test_must_fail git branch -M baz bam &&
+ test $(git -C bazdir2 rev-parse --abbrev-ref HEAD) = bam &&
+ git branch -M bam baz &&
+ rm .git/worktrees/bazdir1/HEAD.lock &&
+ touch .git/worktrees/bazdir2/HEAD.lock &&
+ test_must_fail git branch -M baz bam &&
+ test $(git -C bazdir1 rev-parse --abbrev-ref HEAD) = bam &&
+ rm -rf bazdir1 bazdir2 &&
+ git worktree prune
+'
+
+test_expect_success 'git branch -M baz bam should succeed within a worktree in which baz is checked out' '
+ git checkout -b baz &&
+ git worktree add -f bazdir baz &&
+ (
+ cd bazdir &&
+ git branch -M baz bam &&
+ echo bam >expect &&
+ git rev-parse --abbrev-ref HEAD >actual &&
+ test_cmp expect actual
+ ) &&
+ echo bam >expect &&
+ git rev-parse --abbrev-ref HEAD >actual &&
+ test_cmp expect actual &&
+ rm -r bazdir &&
+ git worktree prune
+'
+
+test_expect_success 'git branch -M main should work when main is checked out' '
+ git checkout main &&
+ git branch -M main
+'
+
+test_expect_success 'git branch -M main main should work when main is checked out' '
+ git checkout main &&
+ git branch -M main main
+'
+
+test_expect_success 'git branch -M topic topic should work when main is checked out' '
+ git checkout main &&
+ git branch topic &&
+ git branch -M topic topic
+'
+
+test_expect_success 'git branch -M and -C fail on detached HEAD' '
+ git checkout HEAD^{} &&
+ test_when_finished git checkout - &&
+ echo "fatal: cannot rename the current branch while not on any" >expect &&
+ test_must_fail git branch -M must-fail 2>err &&
+ test_cmp expect err &&
+ echo "fatal: cannot copy the current branch while not on any" >expect &&
+ test_must_fail git branch -C must-fail 2>err &&
+ test_cmp expect err
+'
+
+test_expect_success 'git branch -m should work with orphan branches' '
+ test_when_finished git checkout - &&
+ test_when_finished git worktree remove -f wt &&
+ git worktree add wt --detach &&
+ # rename orphan in another worktreee
+ git -C wt checkout --orphan orphan-foo-wt &&
+ git branch -m orphan-foo-wt orphan-bar-wt &&
+ test orphan-bar-wt=$(git -C orphan-worktree branch --show-current) &&
+ # rename orphan in the current worktree
+ git checkout --orphan orphan-foo &&
+ git branch -m orphan-foo orphan-bar &&
+ test orphan-bar=$(git branch --show-current)
+'
+
+test_expect_success 'git branch -d on orphan HEAD (merged)' '
+ test_when_finished git checkout main &&
+ git checkout --orphan orphan &&
+ test_when_finished "rm -rf .git/objects/commit-graph*" &&
+ git commit-graph write --reachable &&
+ git branch --track to-delete main &&
+ git branch -d to-delete
+'
+
+test_expect_success 'git branch -d on orphan HEAD (merged, graph)' '
+ test_when_finished git checkout main &&
+ git checkout --orphan orphan &&
+ git branch --track to-delete main &&
+ git branch -d to-delete
+'
+
+test_expect_success 'git branch -d on orphan HEAD (unmerged)' '
+ test_when_finished git checkout main &&
+ git checkout --orphan orphan &&
+ test_when_finished "git branch -D to-delete" &&
+ git branch to-delete main &&
+ test_must_fail git branch -d to-delete 2>err &&
+ grep "not fully merged" err
+'
+
+test_expect_success 'git branch -d on orphan HEAD (unmerged, graph)' '
+ test_when_finished git checkout main &&
+ git checkout --orphan orphan &&
+ test_when_finished "git branch -D to-delete" &&
+ git branch to-delete main &&
+ test_when_finished "rm -rf .git/objects/commit-graph*" &&
+ git commit-graph write --reachable &&
+ test_must_fail git branch -d to-delete 2>err &&
+ grep "not fully merged" err
+'
+
+test_expect_success 'git branch -v -d t should work' '
+ git branch t &&
+ git rev-parse --verify refs/heads/t &&
+ git branch -v -d t &&
+ test_must_fail git rev-parse --verify refs/heads/t
+'
+
+test_expect_success 'git branch -v -m t s should work' '
+ git branch t &&
+ git rev-parse --verify refs/heads/t &&
+ git branch -v -m t s &&
+ test_must_fail git rev-parse --verify refs/heads/t &&
+ git rev-parse --verify refs/heads/s &&
+ git branch -d s
+'
+
+test_expect_success 'git branch -m -d t s should fail' '
+ git branch t &&
+ git rev-parse refs/heads/t &&
+ test_must_fail git branch -m -d t s &&
+ git branch -d t &&
+ test_must_fail git rev-parse refs/heads/t
+'
+
+test_expect_success 'git branch --list -d t should fail' '
+ git branch t &&
+ git rev-parse refs/heads/t &&
+ test_must_fail git branch --list -d t &&
+ git branch -d t &&
+ test_must_fail git rev-parse refs/heads/t
+'
+
+test_expect_success 'deleting checked-out branch from repo that is a submodule' '
+ test_when_finished "rm -rf repo1 repo2" &&
+
+ git init repo1 &&
+ git init repo1/sub &&
+ test_commit -C repo1/sub x &&
+ test_config_global protocol.file.allow always &&
+ git -C repo1 submodule add ./sub &&
+ git -C repo1 commit -m "adding sub" &&
+
+ git clone --recurse-submodules repo1 repo2 &&
+ git -C repo2/sub checkout -b work &&
+ test_must_fail git -C repo2/sub branch -D work
+'
+
+test_expect_success 'bare main worktree has HEAD at branch deleted by secondary worktree' '
+ test_when_finished "rm -rf nonbare base secondary" &&
+
+ git init -b main nonbare &&
+ test_commit -C nonbare x &&
+ git clone --bare nonbare bare &&
+ git -C bare worktree add --detach ../secondary main &&
+ git -C secondary branch -D main
+'
+
+test_expect_success 'git branch --list -v with --abbrev' '
+ test_when_finished "git branch -D t" &&
+ git branch t &&
+ git branch -v --list t >actual.default &&
+ git branch -v --list --abbrev t >actual.abbrev &&
+ test_cmp actual.default actual.abbrev &&
+
+ git branch -v --list --no-abbrev t >actual.noabbrev &&
+ git branch -v --list --abbrev=0 t >actual.0abbrev &&
+ git -c core.abbrev=no branch -v --list t >actual.noabbrev-conf &&
+ test_cmp actual.noabbrev actual.0abbrev &&
+ test_cmp actual.noabbrev actual.noabbrev-conf &&
+
+ git branch -v --list --abbrev=36 t >actual.36abbrev &&
+ # how many hexdigits are used?
+ read name objdefault rest <actual.abbrev &&
+ read name obj36 rest <actual.36abbrev &&
+ objfull=$(git rev-parse --verify t) &&
+
+ # are we really getting abbreviations?
+ test "$obj36" != "$objdefault" &&
+ expr "$obj36" : "$objdefault" >/dev/null &&
+ test "$objfull" != "$obj36" &&
+ expr "$objfull" : "$obj36" >/dev/null
+
+'
+
+test_expect_success 'git branch --column' '
+ COLUMNS=81 git branch --column=column >actual &&
+ cat >expect <<\EOF &&
+ a/b/c bam foo l * main n o/p r
+ abc bar j/k m/m mb o/o q topic
+EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'git branch --column with an extremely long branch name' '
+ long=this/is/a/part/of/long/branch/name &&
+ long=z$long/$long/$long/$long &&
+ test_when_finished "git branch -d $long" &&
+ git branch $long &&
+ COLUMNS=80 git branch --column=column >actual &&
+ cat >expect <<EOF &&
+ a/b/c
+ abc
+ bam
+ bar
+ foo
+ j/k
+ l
+ m/m
+* main
+ mb
+ n
+ o/o
+ o/p
+ q
+ r
+ topic
+ $long
+EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'git branch with column.*' '
+ git config column.ui column &&
+ git config column.branch "dense" &&
+ COLUMNS=80 git branch >actual &&
+ git config --unset column.branch &&
+ git config --unset column.ui &&
+ cat >expect <<\EOF &&
+ a/b/c bam foo l * main n o/p r
+ abc bar j/k m/m mb o/o q topic
+EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'git branch --column -v should fail' '
+ test_must_fail git branch --column -v
+'
+
+test_expect_success 'git branch -v with column.ui ignored' '
+ git config column.ui column &&
+ COLUMNS=80 git branch -v | cut -c -8 | sed "s/ *$//" >actual &&
+ git config --unset column.ui &&
+ cat >expect <<\EOF &&
+ a/b/c
+ abc
+ bam
+ bar
+ foo
+ j/k
+ l
+ m/m
+* main
+ mb
+ n
+ o/o
+ o/p
+ q
+ r
+ topic
+EOF
+ test_cmp expect actual
+'
+
+mv .git/config .git/config-saved
+
+test_expect_success SHA1 'git branch -m q q2 without config should succeed' '
+ git branch -m q q2 &&
+ git branch -m q2 q
+'
+
+mv .git/config-saved .git/config
+
+git config branch.s/s.dummy Hello
+
+test_expect_success 'git branch -m s/s s should work when s/t is deleted' '
+ git branch --create-reflog s/s &&
+ git reflog exists refs/heads/s/s &&
+ git branch --create-reflog s/t &&
+ git reflog exists refs/heads/s/t &&
+ git branch -d s/t &&
+ git branch -m s/s s &&
+ git reflog exists refs/heads/s
+'
+
+test_expect_success 'config information was renamed, too' '
+ test $(git config branch.s.dummy) = Hello &&
+ test_must_fail git config branch.s/s.dummy
+'
+
+test_expect_success 'git branch -m correctly renames multiple config sections' '
+ test_when_finished "git checkout main" &&
+ git checkout -b source main &&
+
+ # Assert that a config file with multiple config sections has
+ # those sections preserved...
+ cat >expect <<-\EOF &&
+ branch.dest.key1=value1
+ some.gar.b=age
+ branch.dest.key2=value2
+ EOF
+ cat >config.branch <<\EOF &&
+;; Note the lack of -\EOF above & mixed indenting here. This is
+;; intentional, we are also testing that the formatting of copied
+;; sections is preserved.
+
+;; Comment for source. Tabs
+[branch "source"]
+ ;; Comment for the source value
+ key1 = value1
+;; Comment for some.gar. Spaces
+[some "gar"]
+ ;; Comment for the some.gar value
+ b = age
+;; Comment for source, again. Mixed tabs/spaces.
+[branch "source"]
+ ;; Comment for the source value, again
+ key2 = value2
+EOF
+ cat config.branch >>.git/config &&
+ git branch -m source dest &&
+ git config -f .git/config -l | grep -F -e source -e dest -e some.gar >actual &&
+ test_cmp expect actual &&
+
+ # ...and that the comments for those sections are also
+ # preserved.
+ cat config.branch | sed "s/\"source\"/\"dest\"/" >expect &&
+ sed -n -e "/Note the lack/,\$p" .git/config >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git branch -c dumps usage' '
+ test_expect_code 128 git branch -c 2>err &&
+ test_grep "branch name required" err
+'
+
+test_expect_success 'git branch --copy dumps usage' '
+ test_expect_code 128 git branch --copy 2>err &&
+ test_grep "branch name required" err
+'
+
+test_expect_success 'git branch -c d e should work' '
+ git branch --create-reflog d &&
+ git reflog exists refs/heads/d &&
+ git config branch.d.dummy Hello &&
+ git branch -c d e &&
+ git reflog exists refs/heads/d &&
+ git reflog exists refs/heads/e &&
+ echo Hello >expect &&
+ git config branch.e.dummy >actual &&
+ test_cmp expect actual &&
+ echo Hello >expect &&
+ git config branch.d.dummy >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git branch --copy is a synonym for -c' '
+ git branch --create-reflog copy &&
+ git reflog exists refs/heads/copy &&
+ git config branch.copy.dummy Hello &&
+ git branch --copy copy copy-to &&
+ git reflog exists refs/heads/copy &&
+ git reflog exists refs/heads/copy-to &&
+ echo Hello >expect &&
+ git config branch.copy.dummy >actual &&
+ test_cmp expect actual &&
+ echo Hello >expect &&
+ git config branch.copy-to.dummy >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git branch -c ee ef should copy ee to create branch ef' '
+ git checkout -b ee &&
+ git reflog exists refs/heads/ee &&
+ git config branch.ee.dummy Hello &&
+ git branch -c ee ef &&
+ git reflog exists refs/heads/ee &&
+ git reflog exists refs/heads/ef &&
+ test $(git config branch.ee.dummy) = Hello &&
+ test $(git config branch.ef.dummy) = Hello &&
+ test $(git rev-parse --abbrev-ref HEAD) = ee
+'
+
+test_expect_success 'git branch -c f/f g/g should work' '
+ git branch --create-reflog f/f &&
+ git reflog exists refs/heads/f/f &&
+ git config branch.f/f.dummy Hello &&
+ git branch -c f/f g/g &&
+ git reflog exists refs/heads/f/f &&
+ git reflog exists refs/heads/g/g &&
+ test $(git config branch.f/f.dummy) = Hello &&
+ test $(git config branch.g/g.dummy) = Hello
+'
+
+test_expect_success 'git branch -c m2 m2 should work' '
+ git branch --create-reflog m2 &&
+ git reflog exists refs/heads/m2 &&
+ git config branch.m2.dummy Hello &&
+ git branch -c m2 m2 &&
+ git reflog exists refs/heads/m2 &&
+ test $(git config branch.m2.dummy) = Hello
+'
+
+test_expect_success 'git branch -c zz zz/zz should fail' '
+ git branch --create-reflog zz &&
+ git reflog exists refs/heads/zz &&
+ test_must_fail git branch -c zz zz/zz
+'
+
+test_expect_success 'git branch -c b/b b should fail' '
+ git branch --create-reflog b/b &&
+ test_must_fail git branch -c b/b b
+'
+
+test_expect_success 'git branch -C o/q o/p should work when o/p exists' '
+ git branch --create-reflog o/q &&
+ git reflog exists refs/heads/o/q &&
+ git reflog exists refs/heads/o/p &&
+ git branch -C o/q o/p
+'
+
+test_expect_success 'git branch -c -f o/q o/p should work when o/p exists' '
+ git reflog exists refs/heads/o/q &&
+ git reflog exists refs/heads/o/p &&
+ git branch -c -f o/q o/p
+'
+
+test_expect_success 'git branch -c qq rr/qq should fail when rr exists' '
+ git branch qq &&
+ git branch rr &&
+ test_must_fail git branch -c qq rr/qq
+'
+
+test_expect_success 'git branch -C b1 b2 should fail when b2 is checked out' '
+ git branch b1 &&
+ git checkout -b b2 &&
+ test_must_fail git branch -C b1 b2
+'
+
+test_expect_success 'git branch -C c1 c2 should succeed when c1 is checked out' '
+ git checkout -b c1 &&
+ git branch c2 &&
+ git branch -C c1 c2 &&
+ test $(git rev-parse --abbrev-ref HEAD) = c1
+'
+
+test_expect_success 'git branch -C c1 c2 should never touch HEAD' '
+ msg="Branch: copied refs/heads/c1 to refs/heads/c2" &&
+ ! grep "$msg$" .git/logs/HEAD
+'
+
+test_expect_success 'git branch -C main should work when main is checked out' '
+ git checkout main &&
+ git branch -C main
+'
+
+test_expect_success 'git branch -C main main should work when main is checked out' '
+ git checkout main &&
+ git branch -C main main
+'
+
+test_expect_success 'git branch -C main5 main5 should work when main is checked out' '
+ git checkout main &&
+ git branch main5 &&
+ git branch -C main5 main5
+'
+
+test_expect_success 'git branch -C ab cd should overwrite existing config for cd' '
+ git branch --create-reflog cd &&
+ git reflog exists refs/heads/cd &&
+ git config branch.cd.dummy CD &&
+ git branch --create-reflog ab &&
+ git reflog exists refs/heads/ab &&
+ git config branch.ab.dummy AB &&
+ git branch -C ab cd &&
+ git reflog exists refs/heads/ab &&
+ git reflog exists refs/heads/cd &&
+ test $(git config branch.ab.dummy) = AB &&
+ test $(git config branch.cd.dummy) = AB
+'
+
+test_expect_success 'git branch -c correctly copies multiple config sections' '
+ FOO=1 &&
+ export FOO &&
+ test_when_finished "git checkout main" &&
+ git checkout -b source2 main &&
+
+ # Assert that a config file with multiple config sections has
+ # those sections preserved...
+ cat >expect <<-\EOF &&
+ branch.source2.key1=value1
+ branch.dest2.key1=value1
+ more.gar.b=age
+ branch.source2.key2=value2
+ branch.dest2.key2=value2
+ EOF
+ cat >config.branch <<\EOF &&
+;; Note the lack of -\EOF above & mixed indenting here. This is
+;; intentional, we are also testing that the formatting of copied
+;; sections is preserved.
+
+;; Comment for source2. Tabs
+[branch "source2"]
+ ;; Comment for the source2 value
+ key1 = value1
+;; Comment for more.gar. Spaces
+[more "gar"]
+ ;; Comment for the more.gar value
+ b = age
+;; Comment for source2, again. Mixed tabs/spaces.
+[branch "source2"]
+ ;; Comment for the source2 value, again
+ key2 = value2
+EOF
+ cat config.branch >>.git/config &&
+ git branch -c source2 dest2 &&
+ git config -f .git/config -l | grep -F -e source2 -e dest2 -e more.gar >actual &&
+ test_cmp expect actual &&
+
+ # ...and that the comments and formatting for those sections
+ # is also preserved.
+ cat >expect <<\EOF &&
+;; Comment for source2. Tabs
+[branch "source2"]
+ ;; Comment for the source2 value
+ key1 = value1
+;; Comment for more.gar. Spaces
+[branch "dest2"]
+ ;; Comment for the source2 value
+ key1 = value1
+;; Comment for more.gar. Spaces
+[more "gar"]
+ ;; Comment for the more.gar value
+ b = age
+;; Comment for source2, again. Mixed tabs/spaces.
+[branch "source2"]
+ ;; Comment for the source2 value, again
+ key2 = value2
+[branch "dest2"]
+ ;; Comment for the source2 value, again
+ key2 = value2
+EOF
+ sed -n -e "/Comment for source2/,\$p" .git/config >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'deleting a symref' '
+ git branch target &&
+ git symbolic-ref refs/heads/symref refs/heads/target &&
+ echo "Deleted branch symref (was refs/heads/target)." >expect &&
+ git branch -d symref >actual &&
+ test_ref_exists refs/heads/target &&
+ test_ref_missing refs/heads/symref &&
+ test_cmp expect actual
+'
+
+test_expect_success 'deleting a dangling symref' '
+ git symbolic-ref refs/heads/dangling-symref nowhere &&
+ test_path_is_file .git/refs/heads/dangling-symref &&
+ echo "Deleted branch dangling-symref (was nowhere)." >expect &&
+ git branch -d dangling-symref >actual &&
+ test_ref_missing refs/heads/dangling-symref &&
+ test_cmp expect actual
+'
+
+test_expect_success 'deleting a self-referential symref' '
+ git symbolic-ref refs/heads/self-reference refs/heads/self-reference &&
+ test_ref_exists refs/heads/self-reference &&
+ echo "Deleted branch self-reference (was refs/heads/self-reference)." >expect &&
+ git branch -d self-reference >actual &&
+ test_ref_missing refs/heads/self-reference &&
+ test_cmp expect actual
+'
+
+test_expect_success 'renaming a symref is not allowed' '
+ git symbolic-ref refs/heads/topic refs/heads/main &&
+ test_must_fail git branch -m topic new-topic &&
+ git symbolic-ref refs/heads/topic &&
+ test_ref_exists refs/heads/main &&
+ test_ref_missing refs/heads/new-topic
+'
+
+test_expect_success SYMLINKS 'git branch -m u v should fail when the reflog for u is a symlink' '
+ git branch --create-reflog u &&
+ mv .git/logs/refs/heads/u real-u &&
+ ln -s real-u .git/logs/refs/heads/u &&
+ test_must_fail git branch -m u v
+'
+
+test_expect_success SYMLINKS 'git branch -m with symlinked .git/refs' '
+ test_when_finished "rm -rf subdir" &&
+ git init --bare subdir &&
+
+ rm -rfv subdir/refs subdir/objects subdir/packed-refs &&
+ ln -s ../.git/refs subdir/refs &&
+ ln -s ../.git/objects subdir/objects &&
+ ln -s ../.git/packed-refs subdir/packed-refs &&
+
+ git -C subdir rev-parse --absolute-git-dir >subdir.dir &&
+ git rev-parse --absolute-git-dir >our.dir &&
+ ! test_cmp subdir.dir our.dir &&
+
+ git -C subdir log &&
+ git -C subdir branch rename-src &&
+ git rev-parse rename-src >expect &&
+ git -C subdir branch -m rename-src rename-dest &&
+ git rev-parse rename-dest >actual &&
+ test_cmp expect actual &&
+ git branch -D rename-dest
+'
+
+test_expect_success 'test tracking setup via --track' '
+ git config remote.local.url . &&
+ git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+ (git show-ref -q refs/remotes/local/main || git fetch local) &&
+ git branch --track my1 local/main &&
+ test $(git config branch.my1.remote) = local &&
+ test $(git config branch.my1.merge) = refs/heads/main
+'
+
+test_expect_success 'test tracking setup (non-wildcard, matching)' '
+ git config remote.local.url . &&
+ git config remote.local.fetch refs/heads/main:refs/remotes/local/main &&
+ (git show-ref -q refs/remotes/local/main || git fetch local) &&
+ git branch --track my4 local/main &&
+ test $(git config branch.my4.remote) = local &&
+ test $(git config branch.my4.merge) = refs/heads/main
+'
+
+test_expect_success 'tracking setup fails on non-matching refspec' '
+ git config remote.local.url . &&
+ git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+ (git show-ref -q refs/remotes/local/main || git fetch local) &&
+ git config remote.local.fetch refs/heads/s:refs/remotes/local/s &&
+ test_must_fail git branch --track my5 local/main &&
+ test_must_fail git config branch.my5.remote &&
+ test_must_fail git config branch.my5.merge
+'
+
+test_expect_success 'test tracking setup via config' '
+ git config branch.autosetupmerge true &&
+ git config remote.local.url . &&
+ git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+ (git show-ref -q refs/remotes/local/main || git fetch local) &&
+ git branch my3 local/main &&
+ test $(git config branch.my3.remote) = local &&
+ test $(git config branch.my3.merge) = refs/heads/main
+'
+
+test_expect_success 'test overriding tracking setup via --no-track' '
+ git config branch.autosetupmerge true &&
+ git config remote.local.url . &&
+ git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+ (git show-ref -q refs/remotes/local/main || git fetch local) &&
+ git branch --no-track my2 local/main &&
+ git config branch.autosetupmerge false &&
+ ! test "$(git config branch.my2.remote)" = local &&
+ ! test "$(git config branch.my2.merge)" = refs/heads/main
+'
+
+test_expect_success 'no tracking without .fetch entries' '
+ git config branch.autosetupmerge true &&
+ git branch my6 s &&
+ git config branch.autosetupmerge false &&
+ test -z "$(git config branch.my6.remote)" &&
+ test -z "$(git config branch.my6.merge)"
+'
+
+test_expect_success 'test tracking setup via --track but deeper' '
+ git config remote.local.url . &&
+ git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+ (git show-ref -q refs/remotes/local/o/o || git fetch local) &&
+ git branch --track my7 local/o/o &&
+ test "$(git config branch.my7.remote)" = local &&
+ test "$(git config branch.my7.merge)" = refs/heads/o/o
+'
+
+test_expect_success 'test deleting branch deletes branch config' '
+ git branch -d my7 &&
+ test -z "$(git config branch.my7.remote)" &&
+ test -z "$(git config branch.my7.merge)"
+'
+
+test_expect_success 'test deleting branch without config' '
+ git branch my7 s &&
+ sha1=$(git rev-parse my7 | cut -c 1-7) &&
+ echo "Deleted branch my7 (was $sha1)." >expect &&
+ git branch -d my7 >actual 2>&1 &&
+ test_cmp expect actual
+'
+
+test_expect_success 'deleting currently checked out branch fails' '
+ git worktree add -b my7 my7 &&
+ test_must_fail git -C my7 branch -d my7 &&
+ test_must_fail git branch -d my7 2>actual &&
+ grep "^error: cannot delete branch .my7. used by worktree at " actual &&
+ rm -r my7 &&
+ git worktree prune
+'
+
+test_expect_success 'deleting in-use branch fails' '
+ git worktree add my7 &&
+ test_commit -C my7 bt7 &&
+ git -C my7 bisect start HEAD HEAD~2 &&
+ test_must_fail git -C my7 branch -d my7 &&
+ test_must_fail git branch -d my7 2>actual &&
+ grep "^error: cannot delete branch .my7. used by worktree at " actual &&
+ rm -r my7 &&
+ git worktree prune
+'
+
+test_expect_success 'test --track without .fetch entries' '
+ git branch --track my8 &&
+ test "$(git config branch.my8.remote)" &&
+ test "$(git config branch.my8.merge)"
+'
+
+test_expect_success 'branch from non-branch HEAD w/autosetupmerge=always' '
+ git config branch.autosetupmerge always &&
+ git branch my9 HEAD^ &&
+ git config branch.autosetupmerge false
+'
+
+test_expect_success 'branch from non-branch HEAD w/--track causes failure' '
+ test_must_fail git branch --track my10 HEAD^
+'
+
+test_expect_success 'branch from tag w/--track causes failure' '
+ git tag foobar &&
+ test_must_fail git branch --track my11 foobar
+'
+
+test_expect_success 'simple tracking works when remote branch name matches' '
+ test_when_finished "rm -rf otherserver" &&
+ git init otherserver &&
+ test_commit -C otherserver my_commit 1 &&
+ git -C otherserver branch feature &&
+ test_config branch.autosetupmerge simple &&
+ test_config remote.otherserver.url otherserver &&
+ test_config remote.otherserver.fetch refs/heads/*:refs/remotes/otherserver/* &&
+ git fetch otherserver &&
+ git branch feature otherserver/feature &&
+ test_cmp_config otherserver branch.feature.remote &&
+ test_cmp_config refs/heads/feature branch.feature.merge
+'
+
+test_expect_success 'simple tracking skips when remote branch name does not match' '
+ test_config branch.autosetupmerge simple &&
+ test_config remote.local.url . &&
+ test_config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+ git fetch local &&
+ git branch my-other local/main &&
+ test_cmp_config "" --default "" branch.my-other.remote &&
+ test_cmp_config "" --default "" branch.my-other.merge
+'
+
+test_expect_success 'simple tracking skips when remote ref is not a branch' '
+ test_config branch.autosetupmerge simple &&
+ test_config remote.localtags.url . &&
+ test_config remote.localtags.fetch refs/tags/*:refs/remotes/localtags/* &&
+ git tag mytag12 main &&
+ git fetch localtags &&
+ git branch mytag12 localtags/mytag12 &&
+ test_cmp_config "" --default "" branch.mytag12.remote &&
+ test_cmp_config "" --default "" branch.mytag12.merge
+'
+
+test_expect_success '--set-upstream-to fails on multiple branches' '
+ echo "fatal: too many arguments to set new upstream" >expect &&
+ test_must_fail git branch --set-upstream-to main a b c 2>err &&
+ test_cmp expect err
+'
+
+test_expect_success '--set-upstream-to fails on detached HEAD' '
+ git checkout HEAD^{} &&
+ test_when_finished git checkout - &&
+ echo "fatal: could not set upstream of HEAD to main when it does not point to any branch" >expect &&
+ test_must_fail git branch --set-upstream-to main 2>err &&
+ test_cmp expect err
+'
+
+test_expect_success '--set-upstream-to fails on a missing dst branch' '
+ echo "fatal: branch '"'"'does-not-exist'"'"' does not exist" >expect &&
+ test_must_fail git branch --set-upstream-to main does-not-exist 2>err &&
+ test_cmp expect err
+'
+
+test_expect_success '--set-upstream-to fails on a missing src branch' '
+ test_must_fail git branch --set-upstream-to does-not-exist main 2>err &&
+ test_grep "the requested upstream branch '"'"'does-not-exist'"'"' does not exist" err
+'
+
+test_expect_success '--set-upstream-to fails on a non-ref' '
+ echo "fatal: cannot set up tracking information; starting point '"'"'HEAD^{}'"'"' is not a branch" >expect &&
+ test_must_fail git branch --set-upstream-to HEAD^{} 2>err &&
+ test_cmp expect err
+'
+
+test_expect_success '--set-upstream-to fails on locked config' '
+ test_when_finished "rm -f .git/config.lock" &&
+ >.git/config.lock &&
+ git branch locked &&
+ test_must_fail git branch --set-upstream-to locked 2>err &&
+ test_grep "could not lock config file .git/config" err
+'
+
+test_expect_success 'use --set-upstream-to modify HEAD' '
+ test_config branch.main.remote foo &&
+ test_config branch.main.merge foo &&
+ git branch my12 &&
+ git branch --set-upstream-to my12 &&
+ test "$(git config branch.main.remote)" = "." &&
+ test "$(git config branch.main.merge)" = "refs/heads/my12"
+'
+
+test_expect_success 'use --set-upstream-to modify a particular branch' '
+ git branch my13 &&
+ git branch --set-upstream-to main my13 &&
+ test_when_finished "git branch --unset-upstream my13" &&
+ test "$(git config branch.my13.remote)" = "." &&
+ test "$(git config branch.my13.merge)" = "refs/heads/main"
+'
+
+test_expect_success '--unset-upstream should fail if given a non-existent branch' '
+ echo "fatal: branch '"'"'i-dont-exist'"'"' has no upstream information" >expect &&
+ test_must_fail git branch --unset-upstream i-dont-exist 2>err &&
+ test_cmp expect err
+'
+
+test_expect_success '--unset-upstream should fail if config is locked' '
+ test_when_finished "rm -f .git/config.lock" &&
+ git branch --set-upstream-to locked &&
+ >.git/config.lock &&
+ test_must_fail git branch --unset-upstream 2>err &&
+ test_grep "could not lock config file .git/config" err
+'
+
+test_expect_success 'test --unset-upstream on HEAD' '
+ git branch my14 &&
+ test_config branch.main.remote foo &&
+ test_config branch.main.merge foo &&
+ git branch --set-upstream-to my14 &&
+ git branch --unset-upstream &&
+ test_must_fail git config branch.main.remote &&
+ test_must_fail git config branch.main.merge &&
+ # fail for a branch without upstream set
+ echo "fatal: branch '"'"'main'"'"' has no upstream information" >expect &&
+ test_must_fail git branch --unset-upstream 2>err &&
+ test_cmp expect err
+'
+
+test_expect_success '--unset-upstream should fail on multiple branches' '
+ echo "fatal: too many arguments to unset upstream" >expect &&
+ test_must_fail git branch --unset-upstream a b c 2>err &&
+ test_cmp expect err
+'
+
+test_expect_success '--unset-upstream should fail on detached HEAD' '
+ git checkout HEAD^{} &&
+ test_when_finished git checkout - &&
+ echo "fatal: could not unset upstream of HEAD when it does not point to any branch" >expect &&
+ test_must_fail git branch --unset-upstream 2>err &&
+ test_cmp expect err
+'
+
+test_expect_success 'test --unset-upstream on a particular branch' '
+ git branch my15 &&
+ git branch --set-upstream-to main my14 &&
+ git branch --unset-upstream my14 &&
+ test_must_fail git config branch.my14.remote &&
+ test_must_fail git config branch.my14.merge
+'
+
+test_expect_success 'disabled option --set-upstream fails' '
+ test_must_fail git branch --set-upstream origin/main
+'
+
+test_expect_success '--set-upstream-to notices an error to set branch as own upstream' "
+ git branch --set-upstream-to refs/heads/my13 my13 2>actual &&
+ cat >expect <<-\EOF &&
+ warning: not setting branch 'my13' as its own upstream
+ EOF
+ test_expect_code 1 git config branch.my13.remote &&
+ test_expect_code 1 git config branch.my13.merge &&
+ test_cmp expect actual
+"
+
+# Keep this test last, as it changes the current branch
+cat >expect <<EOF
+$ZERO_OID $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from main
+EOF
+test_expect_success 'git checkout -b g/h/i -l should create a branch and a log' '
+ GIT_COMMITTER_DATE="2005-05-26 23:30" \
+ git checkout -b g/h/i -l main &&
+ test_ref_exists refs/heads/g/h/i &&
+ test_path_is_file .git/logs/refs/heads/g/h/i &&
+ test_cmp expect .git/logs/refs/heads/g/h/i
+'
+
+test_expect_success 'checkout -b makes reflog by default' '
+ git checkout main &&
+ git config --unset core.logAllRefUpdates &&
+ git checkout -b alpha &&
+ git rev-parse --verify alpha@{0}
+'
+
+test_expect_success 'checkout -b does not make reflog when core.logAllRefUpdates = false' '
+ git checkout main &&
+ git config core.logAllRefUpdates false &&
+ git checkout -b beta &&
+ test_must_fail git rev-parse --verify beta@{0}
+'
+
+test_expect_success 'checkout -b with -l makes reflog when core.logAllRefUpdates = false' '
+ git checkout main &&
+ git checkout -lb gamma &&
+ git config --unset core.logAllRefUpdates &&
+ git rev-parse --verify gamma@{0}
+'
+
+test_expect_success 'avoid ambiguous track and advise' '
+ git config branch.autosetupmerge true &&
+ git config remote.ambi1.url lalala &&
+ git config remote.ambi1.fetch refs/heads/lalala:refs/heads/main &&
+ git config remote.ambi2.url lilili &&
+ git config remote.ambi2.fetch refs/heads/lilili:refs/heads/main &&
+ cat <<-EOF >expected &&
+ fatal: not tracking: ambiguous information for ref '\''refs/heads/main'\''
+ hint: There are multiple remotes whose fetch refspecs map to the remote
+ hint: tracking ref '\''refs/heads/main'\'':
+ hint: ambi1
+ hint: ambi2
+ hint: ''
+ hint: This is typically a configuration error.
+ hint: ''
+ hint: To support setting up tracking branches, ensure that
+ hint: different remotes'\'' fetch refspecs map into different
+ hint: tracking namespaces.
+ EOF
+ test_must_fail git branch all1 main 2>actual &&
+ test_cmp expected actual &&
+ test -z "$(git config branch.all1.merge)"
+'
+
+test_expect_success 'autosetuprebase local on a tracked local branch' '
+ git config remote.local.url . &&
+ git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+ git config branch.autosetuprebase local &&
+ (git show-ref -q refs/remotes/local/o || git fetch local) &&
+ git branch mybase &&
+ git branch --track myr1 mybase &&
+ test "$(git config branch.myr1.remote)" = . &&
+ test "$(git config branch.myr1.merge)" = refs/heads/mybase &&
+ test "$(git config branch.myr1.rebase)" = true
+'
+
+test_expect_success 'autosetuprebase always on a tracked local branch' '
+ git config remote.local.url . &&
+ git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+ git config branch.autosetuprebase always &&
+ (git show-ref -q refs/remotes/local/o || git fetch local) &&
+ git branch mybase2 &&
+ git branch --track myr2 mybase &&
+ test "$(git config branch.myr2.remote)" = . &&
+ test "$(git config branch.myr2.merge)" = refs/heads/mybase &&
+ test "$(git config branch.myr2.rebase)" = true
+'
+
+test_expect_success 'autosetuprebase remote on a tracked local branch' '
+ git config remote.local.url . &&
+ git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+ git config branch.autosetuprebase remote &&
+ (git show-ref -q refs/remotes/local/o || git fetch local) &&
+ git branch mybase3 &&
+ git branch --track myr3 mybase2 &&
+ test "$(git config branch.myr3.remote)" = . &&
+ test "$(git config branch.myr3.merge)" = refs/heads/mybase2 &&
+ ! test "$(git config branch.myr3.rebase)" = true
+'
+
+test_expect_success 'autosetuprebase never on a tracked local branch' '
+ git config remote.local.url . &&
+ git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+ git config branch.autosetuprebase never &&
+ (git show-ref -q refs/remotes/local/o || git fetch local) &&
+ git branch mybase4 &&
+ git branch --track myr4 mybase2 &&
+ test "$(git config branch.myr4.remote)" = . &&
+ test "$(git config branch.myr4.merge)" = refs/heads/mybase2 &&
+ ! test "$(git config branch.myr4.rebase)" = true
+'
+
+test_expect_success 'autosetuprebase local on a tracked remote branch' '
+ git config remote.local.url . &&
+ git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+ git config branch.autosetuprebase local &&
+ (git show-ref -q refs/remotes/local/main || git fetch local) &&
+ git branch --track myr5 local/main &&
+ test "$(git config branch.myr5.remote)" = local &&
+ test "$(git config branch.myr5.merge)" = refs/heads/main &&
+ ! test "$(git config branch.myr5.rebase)" = true
+'
+
+test_expect_success 'autosetuprebase never on a tracked remote branch' '
+ git config remote.local.url . &&
+ git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+ git config branch.autosetuprebase never &&
+ (git show-ref -q refs/remotes/local/main || git fetch local) &&
+ git branch --track myr6 local/main &&
+ test "$(git config branch.myr6.remote)" = local &&
+ test "$(git config branch.myr6.merge)" = refs/heads/main &&
+ ! test "$(git config branch.myr6.rebase)" = true
+'
+
+test_expect_success 'autosetuprebase remote on a tracked remote branch' '
+ git config remote.local.url . &&
+ git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+ git config branch.autosetuprebase remote &&
+ (git show-ref -q refs/remotes/local/main || git fetch local) &&
+ git branch --track myr7 local/main &&
+ test "$(git config branch.myr7.remote)" = local &&
+ test "$(git config branch.myr7.merge)" = refs/heads/main &&
+ test "$(git config branch.myr7.rebase)" = true
+'
+
+test_expect_success 'autosetuprebase always on a tracked remote branch' '
+ git config remote.local.url . &&
+ git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+ git config branch.autosetuprebase remote &&
+ (git show-ref -q refs/remotes/local/main || git fetch local) &&
+ git branch --track myr8 local/main &&
+ test "$(git config branch.myr8.remote)" = local &&
+ test "$(git config branch.myr8.merge)" = refs/heads/main &&
+ test "$(git config branch.myr8.rebase)" = true
+'
+
+test_expect_success 'autosetuprebase unconfigured on a tracked remote branch' '
+ git config --unset branch.autosetuprebase &&
+ git config remote.local.url . &&
+ git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+ (git show-ref -q refs/remotes/local/main || git fetch local) &&
+ git branch --track myr9 local/main &&
+ test "$(git config branch.myr9.remote)" = local &&
+ test "$(git config branch.myr9.merge)" = refs/heads/main &&
+ test "z$(git config branch.myr9.rebase)" = z
+'
+
+test_expect_success 'autosetuprebase unconfigured on a tracked local branch' '
+ git config remote.local.url . &&
+ git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+ (git show-ref -q refs/remotes/local/o || git fetch local) &&
+ git branch mybase10 &&
+ git branch --track myr10 mybase2 &&
+ test "$(git config branch.myr10.remote)" = . &&
+ test "$(git config branch.myr10.merge)" = refs/heads/mybase2 &&
+ test "z$(git config branch.myr10.rebase)" = z
+'
+
+test_expect_success 'autosetuprebase unconfigured on untracked local branch' '
+ git config remote.local.url . &&
+ git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+ (git show-ref -q refs/remotes/local/main || git fetch local) &&
+ git branch --no-track myr11 mybase2 &&
+ test "z$(git config branch.myr11.remote)" = z &&
+ test "z$(git config branch.myr11.merge)" = z &&
+ test "z$(git config branch.myr11.rebase)" = z
+'
+
+test_expect_success 'autosetuprebase unconfigured on untracked remote branch' '
+ git config remote.local.url . &&
+ git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+ (git show-ref -q refs/remotes/local/main || git fetch local) &&
+ git branch --no-track myr12 local/main &&
+ test "z$(git config branch.myr12.remote)" = z &&
+ test "z$(git config branch.myr12.merge)" = z &&
+ test "z$(git config branch.myr12.rebase)" = z
+'
+
+test_expect_success 'autosetuprebase never on an untracked local branch' '
+ git config branch.autosetuprebase never &&
+ git config remote.local.url . &&
+ git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+ (git show-ref -q refs/remotes/local/main || git fetch local) &&
+ git branch --no-track myr13 mybase2 &&
+ test "z$(git config branch.myr13.remote)" = z &&
+ test "z$(git config branch.myr13.merge)" = z &&
+ test "z$(git config branch.myr13.rebase)" = z
+'
+
+test_expect_success 'autosetuprebase local on an untracked local branch' '
+ git config branch.autosetuprebase local &&
+ git config remote.local.url . &&
+ git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+ (git show-ref -q refs/remotes/local/main || git fetch local) &&
+ git branch --no-track myr14 mybase2 &&
+ test "z$(git config branch.myr14.remote)" = z &&
+ test "z$(git config branch.myr14.merge)" = z &&
+ test "z$(git config branch.myr14.rebase)" = z
+'
+
+test_expect_success 'autosetuprebase remote on an untracked local branch' '
+ git config branch.autosetuprebase remote &&
+ git config remote.local.url . &&
+ git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+ (git show-ref -q refs/remotes/local/main || git fetch local) &&
+ git branch --no-track myr15 mybase2 &&
+ test "z$(git config branch.myr15.remote)" = z &&
+ test "z$(git config branch.myr15.merge)" = z &&
+ test "z$(git config branch.myr15.rebase)" = z
+'
+
+test_expect_success 'autosetuprebase always on an untracked local branch' '
+ git config branch.autosetuprebase always &&
+ git config remote.local.url . &&
+ git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+ (git show-ref -q refs/remotes/local/main || git fetch local) &&
+ git branch --no-track myr16 mybase2 &&
+ test "z$(git config branch.myr16.remote)" = z &&
+ test "z$(git config branch.myr16.merge)" = z &&
+ test "z$(git config branch.myr16.rebase)" = z
+'
+
+test_expect_success 'autosetuprebase never on an untracked remote branch' '
+ git config branch.autosetuprebase never &&
+ git config remote.local.url . &&
+ git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+ (git show-ref -q refs/remotes/local/main || git fetch local) &&
+ git branch --no-track myr17 local/main &&
+ test "z$(git config branch.myr17.remote)" = z &&
+ test "z$(git config branch.myr17.merge)" = z &&
+ test "z$(git config branch.myr17.rebase)" = z
+'
+
+test_expect_success 'autosetuprebase local on an untracked remote branch' '
+ git config branch.autosetuprebase local &&
+ git config remote.local.url . &&
+ git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+ (git show-ref -q refs/remotes/local/main || git fetch local) &&
+ git branch --no-track myr18 local/main &&
+ test "z$(git config branch.myr18.remote)" = z &&
+ test "z$(git config branch.myr18.merge)" = z &&
+ test "z$(git config branch.myr18.rebase)" = z
+'
+
+test_expect_success 'autosetuprebase remote on an untracked remote branch' '
+ git config branch.autosetuprebase remote &&
+ git config remote.local.url . &&
+ git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+ (git show-ref -q refs/remotes/local/main || git fetch local) &&
+ git branch --no-track myr19 local/main &&
+ test "z$(git config branch.myr19.remote)" = z &&
+ test "z$(git config branch.myr19.merge)" = z &&
+ test "z$(git config branch.myr19.rebase)" = z
+'
+
+test_expect_success 'autosetuprebase always on an untracked remote branch' '
+ git config branch.autosetuprebase always &&
+ git config remote.local.url . &&
+ git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
+ (git show-ref -q refs/remotes/local/main || git fetch local) &&
+ git branch --no-track myr20 local/main &&
+ test "z$(git config branch.myr20.remote)" = z &&
+ test "z$(git config branch.myr20.merge)" = z &&
+ test "z$(git config branch.myr20.rebase)" = z
+'
+
+test_expect_success 'autosetuprebase always on detached HEAD' '
+ git config branch.autosetupmerge always &&
+ test_when_finished git checkout main &&
+ git checkout HEAD^0 &&
+ git branch my11 &&
+ test -z "$(git config branch.my11.remote)" &&
+ test -z "$(git config branch.my11.merge)"
+'
+
+test_expect_success 'detect misconfigured autosetuprebase (bad value)' '
+ git config branch.autosetuprebase garbage &&
+ test_must_fail git branch
+'
+
+test_expect_success 'detect misconfigured autosetuprebase (no value)' '
+ git config --unset branch.autosetuprebase &&
+ echo "[branch] autosetuprebase" >>.git/config &&
+ test_must_fail git branch &&
+ git config --unset branch.autosetuprebase
+'
+
+test_expect_success 'attempt to delete a branch without base and unmerged to HEAD' '
+ git checkout my9 &&
+ git config --unset branch.my8.merge &&
+ test_must_fail git branch -d my8
+'
+
+test_expect_success 'attempt to delete a branch merged to its base' '
+ # we are on my9 which is the initial commit; traditionally
+ # we would not have allowed deleting my8 that is not merged
+ # to my9, but it is set to track main that already has my8
+ git config branch.my8.merge refs/heads/main &&
+ git branch -d my8
+'
+
+test_expect_success 'attempt to delete a branch merged to its base' '
+ git checkout main &&
+ echo Third >>A &&
+ git commit -m "Third commit" A &&
+ git branch -t my10 my9 &&
+ git branch -f my10 HEAD^ &&
+ # we are on main which is at the third commit, and my10
+ # is behind us, so traditionally we would have allowed deleting
+ # it; but my10 is set to track my9 that is further behind.
+ test_must_fail git branch -d my10
+'
+
+test_expect_success 'branch --delete --force removes dangling branch' '
+ git checkout main &&
+ test_commit unstable &&
+ hash=$(git rev-parse HEAD) &&
+ objpath=$(echo $hash | sed -e "s|^..|.git/objects/&/|") &&
+ git branch --no-track dangling &&
+ mv $objpath $objpath.x &&
+ test_when_finished "mv $objpath.x $objpath" &&
+ git branch --delete --force dangling &&
+ git for-each-ref refs/heads/dangling >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'use --edit-description' '
+ EDITOR=: git branch --edit-description &&
+ test_expect_code 1 git config branch.main.description &&
+
+ write_script editor <<-\EOF &&
+ echo "New contents" >"$1"
+ EOF
+ EDITOR=./editor git branch --edit-description &&
+ write_script editor <<-\EOF &&
+ git stripspace -s <"$1" >"EDITOR_OUTPUT"
+ EOF
+ EDITOR=./editor git branch --edit-description &&
+ echo "New contents" >expect &&
+ test_cmp expect EDITOR_OUTPUT
+'
+
+test_expect_success 'detect typo in branch name when using --edit-description' '
+ write_script editor <<-\EOF &&
+ echo "New contents" >"$1"
+ EOF
+ test_must_fail env EDITOR=./editor git branch --edit-description no-such-branch
+'
+
+test_expect_success 'refuse --edit-description on unborn branch for now' '
+ test_when_finished "git checkout main" &&
+ write_script editor <<-\EOF &&
+ echo "New contents" >"$1"
+ EOF
+ git checkout --orphan unborn &&
+ test_must_fail env EDITOR=./editor git branch --edit-description
+'
+
+test_expect_success '--merged catches invalid object names' '
+ test_must_fail git branch --merged 0000000000000000000000000000000000000000
+'
+
+test_expect_success '--list during rebase' '
+ test_when_finished "reset_rebase" &&
+ git checkout main &&
+ FAKE_LINES="1 edit 2" &&
+ export FAKE_LINES &&
+ set_fake_editor &&
+ git rebase -i HEAD~2 &&
+ git branch --list >actual &&
+ test_grep "rebasing main" actual
+'
+
+test_expect_success '--list during rebase from detached HEAD' '
+ test_when_finished "reset_rebase && git checkout main" &&
+ git checkout main^0 &&
+ oid=$(git rev-parse --short HEAD) &&
+ FAKE_LINES="1 edit 2" &&
+ export FAKE_LINES &&
+ set_fake_editor &&
+ git rebase -i HEAD~2 &&
+ git branch --list >actual &&
+ test_grep "rebasing detached HEAD $oid" actual
+'
+
+test_expect_success 'tracking with unexpected .fetch refspec' '
+ rm -rf a b c d &&
+ git init -b main a &&
+ (
+ cd a &&
+ test_commit a
+ ) &&
+ git init -b main b &&
+ (
+ cd b &&
+ test_commit b
+ ) &&
+ git init -b main c &&
+ (
+ cd c &&
+ test_commit c &&
+ git remote add a ../a &&
+ git remote add b ../b &&
+ git fetch --all
+ ) &&
+ git init -b main d &&
+ (
+ cd d &&
+ git remote add c ../c &&
+ git config remote.c.fetch "+refs/remotes/*:refs/remotes/*" &&
+ git fetch c &&
+ git branch --track local/a/main remotes/a/main &&
+ test "$(git config branch.local/a/main.remote)" = "c" &&
+ test "$(git config branch.local/a/main.merge)" = "refs/remotes/a/main" &&
+ git rev-parse --verify a >expect &&
+ git rev-parse --verify local/a/main >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'configured committerdate sort' '
+ git init -b main sort &&
+ (
+ cd sort &&
+ git config branch.sort committerdate &&
+ test_commit initial &&
+ git checkout -b a &&
+ test_commit a &&
+ git checkout -b c &&
+ test_commit c &&
+ git checkout -b b &&
+ test_commit b &&
+ git branch >actual &&
+ cat >expect <<-\EOF &&
+ main
+ a
+ c
+ * b
+ EOF
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'option override configured sort' '
+ (
+ cd sort &&
+ git config branch.sort committerdate &&
+ git branch --sort=refname >actual &&
+ cat >expect <<-\EOF &&
+ a
+ * b
+ c
+ main
+ EOF
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'invalid sort parameter in configuration' '
+ (
+ cd sort &&
+ git config branch.sort "v:notvalid" &&
+
+ # this works in the "listing" mode, so bad sort key
+ # is a dying offence.
+ test_must_fail git branch &&
+
+ # these do not need to use sorting, and should all
+ # succeed
+ git branch newone main &&
+ git branch -c newone newerone &&
+ git branch -m newone newestone &&
+ git branch -d newerone newestone
+ )
+'
+
+test_expect_success 'tracking info copied with --track=inherit' '
+ git branch --track=inherit foo2 my1 &&
+ test_cmp_config local branch.foo2.remote &&
+ test_cmp_config refs/heads/main branch.foo2.merge
+'
+
+test_expect_success 'tracking info copied with autoSetupMerge=inherit' '
+ test_unconfig branch.autoSetupMerge &&
+ # default config does not copy tracking info
+ git branch foo-no-inherit my1 &&
+ test_cmp_config "" --default "" branch.foo-no-inherit.remote &&
+ test_cmp_config "" --default "" branch.foo-no-inherit.merge &&
+ # with autoSetupMerge=inherit, we copy tracking info from my1
+ test_config branch.autoSetupMerge inherit &&
+ git branch foo3 my1 &&
+ test_cmp_config local branch.foo3.remote &&
+ test_cmp_config refs/heads/main branch.foo3.merge &&
+ # no tracking info to inherit from main
+ git branch main2 main &&
+ test_cmp_config "" --default "" branch.main2.remote &&
+ test_cmp_config "" --default "" branch.main2.merge
+'
+
+test_expect_success '--track overrides branch.autoSetupMerge' '
+ test_config branch.autoSetupMerge inherit &&
+ git branch --track=direct foo4 my1 &&
+ test_cmp_config . branch.foo4.remote &&
+ test_cmp_config refs/heads/my1 branch.foo4.merge &&
+ git branch --no-track foo5 my1 &&
+ test_cmp_config "" --default "" branch.foo5.remote &&
+ test_cmp_config "" --default "" branch.foo5.merge
+'
+
+test_done
diff --git a/t/t3201-branch-contains.sh b/t/t3201-branch-contains.sh
new file mode 100755
index 0000000..800fc33
--- /dev/null
+++ b/t/t3201-branch-contains.sh
@@ -0,0 +1,267 @@
+#!/bin/sh
+
+test_description='branch --contains <commit>, --no-contains <commit> --merged, and --no-merged'
+
+. ./test-lib.sh
+
+test_expect_success setup '
+
+ >file &&
+ git add file &&
+ test_tick &&
+ git commit -m initial &&
+ git branch -M main &&
+ git branch side &&
+
+ echo 1 >file &&
+ test_tick &&
+ git commit -a -m "second on main" &&
+
+ git checkout side &&
+ echo 1 >file &&
+ test_tick &&
+ git commit -a -m "second on side" &&
+
+ git merge main
+
+'
+
+test_expect_success 'branch --contains=main' '
+
+ git branch --contains=main >actual &&
+ {
+ echo " main" && echo "* side"
+ } >expect &&
+ test_cmp expect actual
+
+'
+
+test_expect_success 'branch --contains main' '
+
+ git branch --contains main >actual &&
+ {
+ echo " main" && echo "* side"
+ } >expect &&
+ test_cmp expect actual
+
+'
+
+test_expect_success 'branch --no-contains=main' '
+
+ git branch --no-contains=main >actual &&
+ test_must_be_empty actual
+
+'
+
+test_expect_success 'branch --no-contains main' '
+
+ git branch --no-contains main >actual &&
+ test_must_be_empty actual
+
+'
+
+test_expect_success 'branch --contains=side' '
+
+ git branch --contains=side >actual &&
+ {
+ echo "* side"
+ } >expect &&
+ test_cmp expect actual
+
+'
+
+test_expect_success 'branch --no-contains=side' '
+
+ git branch --no-contains=side >actual &&
+ {
+ echo " main"
+ } >expect &&
+ test_cmp expect actual
+
+'
+
+test_expect_success 'branch --contains with pattern implies --list' '
+
+ git branch --contains=main main >actual &&
+ {
+ echo " main"
+ } >expect &&
+ test_cmp expect actual
+
+'
+
+test_expect_success 'branch --no-contains with pattern implies --list' '
+
+ git branch --no-contains=main main >actual &&
+ test_must_be_empty actual
+
+'
+
+test_expect_success 'side: branch --merged' '
+
+ git branch --merged >actual &&
+ {
+ echo " main" &&
+ echo "* side"
+ } >expect &&
+ test_cmp expect actual
+
+'
+
+test_expect_success 'branch --merged with pattern implies --list' '
+
+ git branch --merged=side main >actual &&
+ {
+ echo " main"
+ } >expect &&
+ test_cmp expect actual
+
+'
+
+test_expect_success 'side: branch --no-merged' '
+
+ git branch --no-merged >actual &&
+ test_must_be_empty actual
+
+'
+
+test_expect_success 'main: branch --merged' '
+
+ git checkout main &&
+ git branch --merged >actual &&
+ {
+ echo "* main"
+ } >expect &&
+ test_cmp expect actual
+
+'
+
+test_expect_success 'main: branch --no-merged' '
+
+ git branch --no-merged >actual &&
+ {
+ echo " side"
+ } >expect &&
+ test_cmp expect actual
+
+'
+
+test_expect_success 'branch --no-merged with pattern implies --list' '
+
+ git branch --no-merged=main main >actual &&
+ test_must_be_empty actual
+
+'
+
+test_expect_success 'implicit --list conflicts with modification options' '
+
+ test_must_fail git branch --contains=main -d &&
+ test_must_fail git branch --contains=main -m foo &&
+ test_must_fail git branch --no-contains=main -d &&
+ test_must_fail git branch --no-contains=main -m foo
+
+'
+
+test_expect_success 'Assert that --contains only works on commits, not trees & blobs' '
+ test_must_fail git branch --contains main^{tree} &&
+ blob=$(git hash-object -w --stdin <<-\EOF
+ Some blob
+ EOF
+ ) &&
+ test_must_fail git branch --contains $blob &&
+ test_must_fail git branch --no-contains $blob
+'
+
+test_expect_success 'multiple branch --contains' '
+ git checkout -b side2 main &&
+ >feature &&
+ git add feature &&
+ git commit -m "add feature" &&
+ git checkout -b next main &&
+ git merge side &&
+ git branch --contains side --contains side2 >actual &&
+ cat >expect <<-\EOF &&
+ * next
+ side
+ side2
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'multiple branch --merged' '
+ git branch --merged next --merged main >actual &&
+ cat >expect <<-\EOF &&
+ main
+ * next
+ side
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'multiple branch --no-contains' '
+ git branch --no-contains side --no-contains side2 >actual &&
+ cat >expect <<-\EOF &&
+ main
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'multiple branch --no-merged' '
+ git branch --no-merged next --no-merged main >actual &&
+ cat >expect <<-\EOF &&
+ side2
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'branch --contains combined with --no-contains' '
+ git checkout -b seen main &&
+ git merge side &&
+ git merge side2 &&
+ git branch --contains side --no-contains side2 >actual &&
+ cat >expect <<-\EOF &&
+ next
+ side
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'branch --merged combined with --no-merged' '
+ git branch --merged seen --no-merged next >actual &&
+ cat >expect <<-\EOF &&
+ * seen
+ side2
+ EOF
+ test_cmp expect actual
+'
+
+# We want to set up a case where the walk for the tracking info
+# of one branch crosses the tip of another branch (and make sure
+# that the latter walk does not mess up our flag to see if it was
+# merged).
+#
+# Here "topic" tracks "main" with one extra commit, and "zzz" points to the
+# same tip as main The name "zzz" must come alphabetically after "topic"
+# as we process them in that order.
+test_expect_success 'branch --merged with --verbose' '
+ git branch --track topic main &&
+ git branch zzz topic &&
+ git checkout topic &&
+ test_commit foo &&
+ git branch --merged topic >actual &&
+ cat >expect <<-\EOF &&
+ main
+ * topic
+ zzz
+ EOF
+ test_cmp expect actual &&
+ git branch --verbose --merged topic >actual &&
+ cat >expect <<-EOF &&
+ main $(git rev-parse --short main) second on main
+ * topic $(git rev-parse --short topic ) [ahead 1] foo
+ zzz $(git rev-parse --short zzz ) second on main
+ EOF
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t3202-show-branch.sh b/t/t3202-show-branch.sh
new file mode 100755
index 0000000..6a98b2d
--- /dev/null
+++ b/t/t3202-show-branch.sh
@@ -0,0 +1,267 @@
+#!/bin/sh
+
+test_description='test show-branch'
+
+. ./test-lib.sh
+
+# arbitrary reference time: 2009-08-30 19:20:00
+GIT_TEST_DATE_NOW=1251660000; export GIT_TEST_DATE_NOW
+
+test_expect_success 'error descriptions on empty repository' '
+ current=$(git branch --show-current) &&
+ cat >expect <<-EOF &&
+ error: no commit on branch '\''$current'\'' yet
+ EOF
+ test_must_fail git branch --edit-description 2>actual &&
+ test_cmp expect actual &&
+ test_must_fail git branch --edit-description $current 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'fatal descriptions on empty repository' '
+ current=$(git branch --show-current) &&
+ cat >expect <<-EOF &&
+ fatal: no commit on branch '\''$current'\'' yet
+ EOF
+ test_must_fail git branch --set-upstream-to=non-existent 2>actual &&
+ test_cmp expect actual &&
+ test_must_fail git branch -c new-branch 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'setup' '
+ test_commit initial &&
+ for i in $(test_seq 1 10)
+ do
+ git checkout -b branch$i initial &&
+ test_commit --no-tag branch$i || return 1
+ done &&
+ git for-each-ref \
+ --sort=version:refname \
+ --format="%(refname:strip=2)" \
+ "refs/heads/branch*" >branches.sorted &&
+ sed "s/^> //" >expect <<-\EOF
+ > ! [branch1] branch1
+ > ! [branch2] branch2
+ > ! [branch3] branch3
+ > ! [branch4] branch4
+ > ! [branch5] branch5
+ > ! [branch6] branch6
+ > ! [branch7] branch7
+ > ! [branch8] branch8
+ > ! [branch9] branch9
+ > * [branch10] branch10
+ > ----------
+ > * [branch10] branch10
+ > + [branch9] branch9
+ > + [branch8] branch8
+ > + [branch7] branch7
+ > + [branch6] branch6
+ > + [branch5] branch5
+ > + [branch4] branch4
+ > + [branch3] branch3
+ > + [branch2] branch2
+ > + [branch1] branch1
+ > +++++++++* [branch10^] initial
+ EOF
+'
+
+test_expect_success 'show-branch with more than 8 branches' '
+ git show-branch $(cat branches.sorted) >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'show-branch with showbranch.default' '
+ for branch in $(cat branches.sorted)
+ do
+ test_config showbranch.default $branch --add || return 1
+ done &&
+ git show-branch >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'show-branch --color output' '
+ sed "s/^> //" >expect <<-\EOF &&
+ > <RED>!<RESET> [branch1] branch1
+ > <GREEN>!<RESET> [branch2] branch2
+ > <YELLOW>!<RESET> [branch3] branch3
+ > <BLUE>!<RESET> [branch4] branch4
+ > <MAGENTA>!<RESET> [branch5] branch5
+ > <CYAN>!<RESET> [branch6] branch6
+ > <BOLD;RED>!<RESET> [branch7] branch7
+ > <BOLD;GREEN>!<RESET> [branch8] branch8
+ > <BOLD;YELLOW>!<RESET> [branch9] branch9
+ > <BOLD;BLUE>*<RESET> [branch10] branch10
+ > ----------
+ > <BOLD;BLUE>*<RESET> [branch10] branch10
+ > <BOLD;YELLOW>+<RESET> [branch9] branch9
+ > <BOLD;GREEN>+<RESET> [branch8] branch8
+ > <BOLD;RED>+<RESET> [branch7] branch7
+ > <CYAN>+<RESET> [branch6] branch6
+ > <MAGENTA>+<RESET> [branch5] branch5
+ > <BLUE>+<RESET> [branch4] branch4
+ > <YELLOW>+<RESET> [branch3] branch3
+ > <GREEN>+<RESET> [branch2] branch2
+ > <RED>+<RESET> [branch1] branch1
+ > <RED>+<RESET><GREEN>+<RESET><YELLOW>+<RESET><BLUE>+<RESET><MAGENTA>+<RESET><CYAN>+<RESET><BOLD;RED>+<RESET><BOLD;GREEN>+<RESET><BOLD;YELLOW>+<RESET><BOLD;BLUE>*<RESET> [branch10^] initial
+ EOF
+ git show-branch --color=always $(cat branches.sorted) >actual.raw &&
+ test_decode_color <actual.raw >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'show branch --remotes' '
+ cat >expect.err <<-\EOF &&
+ No revs to be shown.
+ EOF
+ git show-branch -r 2>actual.err >actual.out &&
+ test_cmp expect.err actual.err &&
+ test_must_be_empty actual.out
+'
+
+test_expect_success 'show-branch --sparse' '
+ test_when_finished "git checkout branch10 && git branch -D branchA" &&
+ git checkout -b branchA branch10 &&
+ git merge -s ours -m "merge 1 and 10 to make A" branch1 &&
+ git commit --allow-empty -m "another" &&
+
+ git show-branch --sparse >out &&
+ grep "merge 1 and 10 to make A" out &&
+
+ git show-branch >out &&
+ ! grep "merge 1 and 10 to make A" out &&
+
+ git show-branch --no-sparse >out &&
+ ! grep "merge 1 and 10 to make A" out
+'
+
+test_expect_success 'setup show branch --list' '
+ sed "s/^> //" >expect <<-\EOF
+ > [branch1] branch1
+ > [branch2] branch2
+ > [branch3] branch3
+ > [branch4] branch4
+ > [branch5] branch5
+ > [branch6] branch6
+ > [branch7] branch7
+ > [branch8] branch8
+ > [branch9] branch9
+ > * [branch10] branch10
+ EOF
+'
+
+test_expect_success 'show branch --list' '
+ git show-branch --list $(cat branches.sorted) >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'show branch --list has no --color output' '
+ git show-branch --color=always --list $(cat branches.sorted) >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'show branch --merge-base with one argument' '
+ for branch in $(cat branches.sorted)
+ do
+ git rev-parse $branch >expect &&
+ git show-branch --merge-base $branch >actual &&
+ test_cmp expect actual || return 1
+ done
+'
+
+test_expect_success 'show branch --merge-base with two arguments' '
+ for branch in $(cat branches.sorted)
+ do
+ git rev-parse initial >expect &&
+ git show-branch --merge-base initial $branch >actual &&
+ test_cmp expect actual || return 1
+ done
+'
+
+test_expect_success 'show branch --merge-base with N arguments' '
+ git rev-parse initial >expect &&
+ git show-branch --merge-base $(cat branches.sorted) >actual &&
+ test_cmp expect actual &&
+
+ git merge-base $(cat branches.sorted) >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'show branch --reflog=2' '
+ sed "s/^> //" >expect <<-\EOF &&
+ > ! [refs/heads/branch10@{0}] (4 years, 5 months ago) commit: branch10
+ > ! [refs/heads/branch10@{1}] (4 years, 5 months ago) commit: branch10
+ > --
+ > + [refs/heads/branch10@{0}] branch10
+ > ++ [refs/heads/branch10@{1}] initial
+ EOF
+ git show-branch --reflog=2 >actual &&
+ test_cmp actual expect
+'
+
+# incompatible options
+while read combo
+do
+ test_expect_success "show-branch $combo (should fail)" '
+ test_must_fail git show-branch $combo 2>error &&
+ grep -e "cannot be used together" -e "usage:" error
+ '
+done <<\EOF
+--all --reflog
+--merge-base --reflog
+--list --merge-base
+--reflog --current
+EOF
+
+# unnegatable options
+for opt in topo-order date-order reflog
+do
+ test_expect_success "show-branch --no-$opt (should fail)" '
+ test_must_fail git show-branch --no-$opt 2>err &&
+ grep "unknown option .no-$opt." err
+ '
+done
+
+test_expect_success 'error descriptions on non-existent branch' '
+ cat >expect <<-EOF &&
+ error: no branch named '\''non-existent'\''
+ EOF
+ test_must_fail git branch --edit-description non-existent 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'fatal descriptions on non-existent branch' '
+ cat >expect <<-EOF &&
+ fatal: branch '\''non-existent'\'' does not exist
+ EOF
+ test_must_fail git branch --set-upstream-to=non-existent non-existent 2>actual &&
+ test_cmp expect actual &&
+
+ cat >expect <<-EOF &&
+ fatal: no branch named '\''non-existent'\''
+ EOF
+ test_must_fail git branch -c non-existent new-branch 2>actual &&
+ test_cmp expect actual &&
+ test_must_fail git branch -m non-existent new-branch 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'error descriptions on orphan branch' '
+ test_when_finished git worktree remove -f wt &&
+ git worktree add wt --detach &&
+ git -C wt checkout --orphan orphan-branch &&
+ test_branch_op_in_wt() {
+ test_orphan_error() {
+ test_must_fail git $* 2>actual &&
+ test_grep "no commit on branch .orphan-branch. yet$" actual
+ } &&
+ test_orphan_error -C wt branch $1 $2 && # implicit branch
+ test_orphan_error -C wt branch $1 orphan-branch $2 && # explicit branch
+ test_orphan_error branch $1 orphan-branch $2 # different worktree
+ } &&
+ test_branch_op_in_wt --edit-description &&
+ test_branch_op_in_wt --set-upstream-to=ne &&
+ test_branch_op_in_wt -c new-branch
+'
+
+test_done
diff --git a/t/t3203-branch-output.sh b/t/t3203-branch-output.sh
new file mode 100755
index 0000000..758963b
--- /dev/null
+++ b/t/t3203-branch-output.sh
@@ -0,0 +1,459 @@
+#!/bin/sh
+
+test_description='git branch display tests'
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-terminal.sh
+
+test_expect_success 'make commits' '
+ echo content >file &&
+ git add file &&
+ git commit -m one &&
+ git branch -M main &&
+ echo content >>file &&
+ git commit -a -m two
+'
+
+test_expect_success 'make branches' '
+ git branch branch-one &&
+ git branch branch-two HEAD^
+'
+
+test_expect_success 'make remote branches' '
+ git update-ref refs/remotes/origin/branch-one branch-one &&
+ git update-ref refs/remotes/origin/branch-two branch-two &&
+ git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/branch-one
+'
+
+cat >expect <<'EOF'
+ branch-one
+ branch-two
+* main
+EOF
+test_expect_success 'git branch shows local branches' '
+ git branch >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git branch --list shows local branches' '
+ git branch --list >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<'EOF'
+ branch-one
+ branch-two
+EOF
+test_expect_success 'git branch --list pattern shows matching local branches' '
+ git branch --list branch* >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<'EOF'
+ origin/HEAD -> origin/branch-one
+ origin/branch-one
+ origin/branch-two
+EOF
+test_expect_success 'git branch -r shows remote branches' '
+ git branch -r >actual &&
+ test_cmp expect actual &&
+
+ git branch --remotes >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git branch --no-remotes is rejected' '
+ test_must_fail git branch --no-remotes 2>err &&
+ grep "unknown option .no-remotes." err
+'
+
+cat >expect <<'EOF'
+ branch-one
+ branch-two
+* main
+ remotes/origin/HEAD -> origin/branch-one
+ remotes/origin/branch-one
+ remotes/origin/branch-two
+EOF
+test_expect_success 'git branch -a shows local and remote branches' '
+ git branch -a >actual &&
+ test_cmp expect actual &&
+
+ git branch --all >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git branch --no-all is rejected' '
+ test_must_fail git branch --no-all 2>err &&
+ grep "unknown option .no-all." err
+'
+
+cat >expect <<'EOF'
+two
+one
+two
+EOF
+test_expect_success 'git branch -v shows branch summaries' '
+ git branch -v >tmp &&
+ awk "{print \$NF}" <tmp >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<'EOF'
+two
+one
+EOF
+test_expect_success 'git branch --list -v pattern shows branch summaries' '
+ git branch --list -v branch* >tmp &&
+ awk "{print \$NF}" <tmp >actual &&
+ test_cmp expect actual
+'
+test_expect_success 'git branch --ignore-case --list -v pattern shows branch summaries' '
+ git branch --list --ignore-case -v BRANCH* >tmp &&
+ awk "{print \$NF}" <tmp >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git branch -v pattern does not show branch summaries' '
+ test_must_fail git branch -v branch*
+'
+
+test_expect_success 'git branch `--show-current` shows current branch' '
+ cat >expect <<-\EOF &&
+ branch-two
+ EOF
+ git checkout branch-two &&
+ git branch --show-current >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git branch `--show-current` is silent when detached HEAD' '
+ git checkout HEAD^0 &&
+ git branch --show-current >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'git branch `--show-current` works properly when tag exists' '
+ cat >expect <<-\EOF &&
+ branch-and-tag-name
+ EOF
+ test_when_finished "
+ git checkout branch-one
+ git branch -D branch-and-tag-name
+ " &&
+ git checkout -b branch-and-tag-name &&
+ test_when_finished "git tag -d branch-and-tag-name" &&
+ git tag branch-and-tag-name &&
+ git branch --show-current >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git branch `--show-current` works properly with worktrees' '
+ cat >expect <<-\EOF &&
+ branch-one
+ branch-two
+ EOF
+ git checkout branch-one &&
+ test_when_finished "
+ git worktree remove worktree_dir
+ " &&
+ git worktree add worktree_dir branch-two &&
+ {
+ git branch --show-current &&
+ git -C worktree_dir branch --show-current
+ } >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git branch shows detached HEAD properly' '
+ cat >expect <<EOF &&
+* (HEAD detached at $(git rev-parse --short HEAD^0))
+ branch-one
+ branch-two
+ main
+EOF
+ git checkout HEAD^0 &&
+ git branch >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git branch shows detached HEAD properly after checkout --detach' '
+ git checkout main &&
+ cat >expect <<EOF &&
+* (HEAD detached at $(git rev-parse --short HEAD^0))
+ branch-one
+ branch-two
+ main
+EOF
+ git checkout --detach &&
+ git branch >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git branch shows detached HEAD properly after moving' '
+ cat >expect <<EOF &&
+* (HEAD detached from $(git rev-parse --short HEAD))
+ branch-one
+ branch-two
+ main
+EOF
+ git reset --hard HEAD^1 &&
+ git branch >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git branch shows detached HEAD properly from tag' '
+ cat >expect <<EOF &&
+* (HEAD detached at fromtag)
+ branch-one
+ branch-two
+ main
+EOF
+ git tag fromtag main &&
+ git checkout fromtag &&
+ git branch >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git branch shows detached HEAD properly after moving from tag' '
+ cat >expect <<EOF &&
+* (HEAD detached from fromtag)
+ branch-one
+ branch-two
+ main
+EOF
+ git reset --hard HEAD^1 &&
+ git branch >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git branch `--sort=[-]objectsize` option' '
+ cat >expect <<-\EOF &&
+ * (HEAD detached from fromtag)
+ branch-two
+ branch-one
+ main
+ EOF
+ git branch --sort=objectsize >actual &&
+ test_cmp expect actual &&
+
+ cat >expect <<-\EOF &&
+ * (HEAD detached from fromtag)
+ branch-one
+ main
+ branch-two
+ EOF
+ git branch --sort=-objectsize >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git branch `--sort=[-]type` option' '
+ cat >expect <<-\EOF &&
+ * (HEAD detached from fromtag)
+ branch-one
+ branch-two
+ main
+ EOF
+ git branch --sort=type >actual &&
+ test_cmp expect actual &&
+
+ cat >expect <<-\EOF &&
+ * (HEAD detached from fromtag)
+ branch-one
+ branch-two
+ main
+ EOF
+ git branch --sort=-type >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git branch `--sort=[-]version:refname` option' '
+ cat >expect <<-\EOF &&
+ * (HEAD detached from fromtag)
+ branch-one
+ branch-two
+ main
+ EOF
+ git branch --sort=version:refname >actual &&
+ test_cmp expect actual &&
+
+ cat >expect <<-\EOF &&
+ * (HEAD detached from fromtag)
+ main
+ branch-two
+ branch-one
+ EOF
+ git branch --sort=-version:refname >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git branch --points-at option' '
+ cat >expect <<-\EOF &&
+ branch-one
+ main
+ EOF
+ git branch --points-at=branch-one >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'ambiguous branch/tag not marked' '
+ git tag ambiguous &&
+ git branch ambiguous &&
+ echo " ambiguous" >expect &&
+ git branch --list ambiguous >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'local-branch symrefs shortened properly' '
+ git symbolic-ref refs/heads/ref-to-branch refs/heads/branch-one &&
+ git symbolic-ref refs/heads/ref-to-remote refs/remotes/origin/branch-one &&
+ cat >expect <<-\EOF &&
+ ref-to-branch -> branch-one
+ ref-to-remote -> origin/branch-one
+ EOF
+ git branch >actual.raw &&
+ grep ref-to <actual.raw >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'sort branches, ignore case' '
+ (
+ git init -b main sort-icase &&
+ cd sort-icase &&
+ test_commit initial &&
+ git branch branch-one &&
+ git branch BRANCH-two &&
+ git branch --list | awk "{print \$NF}" >actual &&
+ cat >expected <<-\EOF &&
+ BRANCH-two
+ branch-one
+ main
+ EOF
+ test_cmp expected actual &&
+ git branch --list -i | awk "{print \$NF}" >actual &&
+ cat >expected <<-\EOF &&
+ branch-one
+ BRANCH-two
+ main
+ EOF
+ test_cmp expected actual
+ )
+'
+
+test_expect_success 'git branch --format option' '
+ cat >expect <<-\EOF &&
+ Refname is (HEAD detached from fromtag)
+ Refname is refs/heads/ambiguous
+ Refname is refs/heads/branch-one
+ Refname is refs/heads/branch-two
+ Refname is refs/heads/main
+ Refname is refs/heads/ref-to-branch
+ Refname is refs/heads/ref-to-remote
+ EOF
+ git branch --format="Refname is %(refname)" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git branch --format with ahead-behind' '
+ cat >expect <<-\EOF &&
+ (HEAD detached from fromtag) 0 0
+ refs/heads/ambiguous 0 0
+ refs/heads/branch-one 1 0
+ refs/heads/branch-two 0 0
+ refs/heads/main 1 0
+ refs/heads/ref-to-branch 1 0
+ refs/heads/ref-to-remote 1 0
+ EOF
+ git branch --format="%(refname) %(ahead-behind:HEAD)" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git branch with --format=%(rest) must fail' '
+ test_must_fail git branch --format="%(rest)" >actual
+'
+
+test_expect_success 'git branch --format --omit-empty' '
+ cat >expect <<-\EOF &&
+ Refname is (HEAD detached from fromtag)
+ Refname is refs/heads/ambiguous
+ Refname is refs/heads/branch-one
+ Refname is refs/heads/branch-two
+
+ Refname is refs/heads/ref-to-branch
+ Refname is refs/heads/ref-to-remote
+ EOF
+ git branch --format="%(if:notequals=refs/heads/main)%(refname)%(then)Refname is %(refname)%(end)" >actual &&
+ test_cmp expect actual &&
+ cat >expect <<-\EOF &&
+ Refname is (HEAD detached from fromtag)
+ Refname is refs/heads/ambiguous
+ Refname is refs/heads/branch-one
+ Refname is refs/heads/branch-two
+ Refname is refs/heads/ref-to-branch
+ Refname is refs/heads/ref-to-remote
+ EOF
+ git branch --omit-empty --format="%(if:notequals=refs/heads/main)%(refname)%(then)Refname is %(refname)%(end)" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'worktree colors correct' '
+ cat >expect <<-EOF &&
+ * <GREEN>(HEAD detached from fromtag)<RESET>
+ ambiguous<RESET>
+ branch-one<RESET>
+ + <CYAN>branch-two<RESET>
+ main<RESET>
+ ref-to-branch<RESET> -> branch-one
+ ref-to-remote<RESET> -> origin/branch-one
+ EOF
+ git worktree add worktree_dir branch-two &&
+ git branch --color >actual.raw &&
+ rm -r worktree_dir &&
+ git worktree prune &&
+ test_decode_color <actual.raw >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success "set up color tests" '
+ echo "<RED>main<RESET>" >expect.color &&
+ echo "main" >expect.bare &&
+ color_args="--format=%(color:red)%(refname:short) --list main"
+'
+
+test_expect_success '%(color) omitted without tty' '
+ TERM=vt100 git branch $color_args >actual.raw &&
+ test_decode_color <actual.raw >actual &&
+ test_cmp expect.bare actual
+'
+
+test_expect_success TTY '%(color) present with tty' '
+ test_terminal git branch $color_args >actual.raw &&
+ test_decode_color <actual.raw >actual &&
+ test_cmp expect.color actual
+'
+
+test_expect_success '--color overrides auto-color' '
+ git branch --color $color_args >actual.raw &&
+ test_decode_color <actual.raw >actual &&
+ test_cmp expect.color actual
+'
+
+test_expect_success 'verbose output lists worktree path' '
+ one=$(git rev-parse --short HEAD) &&
+ two=$(git rev-parse --short main) &&
+ cat >expect <<-EOF &&
+ * (HEAD detached from fromtag) $one one
+ ambiguous $one one
+ branch-one $two two
+ + branch-two $one ($(pwd)/worktree_dir) one
+ main $two two
+ ref-to-branch $two two
+ ref-to-remote $two two
+ EOF
+ git worktree add worktree_dir branch-two &&
+ git branch -vv >actual &&
+ rm -r worktree_dir &&
+ git worktree prune &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t3204-branch-name-interpretation.sh b/t/t3204-branch-name-interpretation.sh
new file mode 100755
index 0000000..594e3e4
--- /dev/null
+++ b/t/t3204-branch-name-interpretation.sh
@@ -0,0 +1,171 @@
+#!/bin/sh
+
+test_description='interpreting exotic branch name arguments
+
+Branch name arguments are usually names which are taken to be inside of
+refs/heads/, but we interpret some magic syntax like @{-1}, @{upstream}, etc.
+This script aims to check the behavior of those corner cases.
+'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+expect_branch() {
+ git log -1 --format=%s "$1" >actual &&
+ echo "$2" >expect &&
+ test_cmp expect actual
+}
+
+expect_deleted() {
+ test_must_fail git rev-parse --verify "$1"
+}
+
+test_expect_success 'set up repo' '
+ test_commit one &&
+ test_commit two &&
+ git remote add origin foo.git
+'
+
+test_expect_success 'update branch via @{-1}' '
+ git branch previous one &&
+
+ git checkout previous &&
+ git checkout main &&
+
+ git branch -f @{-1} two &&
+ expect_branch previous two
+'
+
+test_expect_success 'update branch via local @{upstream}' '
+ git branch local one &&
+ git branch --set-upstream-to=local &&
+
+ git branch -f @{upstream} two &&
+ expect_branch local two
+'
+
+test_expect_success 'disallow updating branch via remote @{upstream}' '
+ git update-ref refs/remotes/origin/remote one &&
+ git branch --set-upstream-to=origin/remote &&
+
+ test_must_fail git branch -f @{upstream} two
+'
+
+test_expect_success 'create branch with pseudo-qualified name' '
+ git branch refs/heads/qualified two &&
+ expect_branch refs/heads/refs/heads/qualified two
+'
+
+test_expect_success 'force-copy a branch to itself via @{-1} is no-op' '
+ git branch -t copiable main &&
+ git checkout copiable &&
+ git checkout - &&
+ git branch -C @{-1} copiable &&
+ git config --get-all branch.copiable.merge >actual &&
+ echo refs/heads/main >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'delete branch via @{-1}' '
+ git branch previous-del &&
+
+ git checkout previous-del &&
+ git checkout main &&
+
+ git branch -D @{-1} &&
+ expect_deleted previous-del
+'
+
+test_expect_success 'delete branch via local @{upstream}' '
+ git branch local-del &&
+ git branch --set-upstream-to=local-del &&
+
+ git branch -D @{upstream} &&
+ expect_deleted local-del
+'
+
+test_expect_success 'delete branch via remote @{upstream}' '
+ git update-ref refs/remotes/origin/remote-del two &&
+ git branch --set-upstream-to=origin/remote-del &&
+
+ git branch -r -D @{upstream} &&
+ expect_deleted origin/remote-del
+'
+
+# Note that we create two oddly named local branches here. We want to make
+# sure that we do not accidentally delete either of them, even if
+# shorten_unambiguous_ref() tweaks the name to avoid ambiguity.
+test_expect_success 'delete @{upstream} expansion matches -r option' '
+ git update-ref refs/remotes/origin/remote-del two &&
+ git branch --set-upstream-to=origin/remote-del &&
+ git update-ref refs/heads/origin/remote-del two &&
+ git update-ref refs/heads/remotes/origin/remote-del two &&
+
+ test_must_fail git branch -D @{upstream} &&
+ expect_branch refs/heads/origin/remote-del two &&
+ expect_branch refs/heads/remotes/origin/remote-del two
+'
+
+test_expect_success 'disallow deleting remote branch via @{-1}' '
+ git update-ref refs/remotes/origin/previous one &&
+
+ git checkout -b origin/previous two &&
+ git checkout main &&
+
+ test_must_fail git branch -r -D @{-1} &&
+ expect_branch refs/remotes/origin/previous one &&
+ expect_branch refs/heads/origin/previous two
+'
+
+# The thing we are testing here is that "@" is the real branch refs/heads/@,
+# and not refs/heads/HEAD. These tests should not imply that refs/heads/@ is a
+# sane thing, but it _is_ technically allowed for now. If we disallow it, these
+# can be switched to test_must_fail.
+test_expect_success 'create branch named "@"' '
+ git branch -f @ one &&
+ expect_branch refs/heads/@ one
+'
+
+test_expect_success 'delete branch named "@"' '
+ git update-ref refs/heads/@ two &&
+ git branch -D @ &&
+ expect_deleted refs/heads/@
+'
+
+test_expect_success 'checkout does not treat remote @{upstream} as a branch' '
+ git update-ref refs/remotes/origin/checkout one &&
+ git branch --set-upstream-to=origin/checkout &&
+ git update-ref refs/heads/origin/checkout two &&
+ git update-ref refs/heads/remotes/origin/checkout two &&
+
+ git checkout @{upstream} &&
+ expect_branch HEAD one
+'
+
+test_expect_success 'edit-description via @{-1}' '
+ git checkout -b desc-branch &&
+ git checkout -b non-desc-branch &&
+ write_script editor <<-\EOF &&
+ echo "Branch description" >"$1"
+ EOF
+ EDITOR=./editor git branch --edit-description @{-1} &&
+ test_must_fail git config branch.non-desc-branch.description &&
+ git config branch.desc-branch.description >actual &&
+ printf "Branch description\n\n" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'modify branch upstream via "@{-1}" and "@{-1}@{upstream}"' '
+ git checkout -b upstream-branch &&
+ git checkout -b upstream-other -t upstream-branch &&
+ git branch --set-upstream-to upstream-other @{-1} &&
+ git config branch.upstream-branch.merge >actual &&
+ echo "refs/heads/upstream-other" >expect &&
+ test_cmp expect actual &&
+ git branch --unset-upstream @{-1}@{upstream} &&
+ test_must_fail git config branch.upstream-other.merge
+'
+
+test_done
diff --git a/t/t3205-branch-color.sh b/t/t3205-branch-color.sh
new file mode 100755
index 0000000..0b61da9
--- /dev/null
+++ b/t/t3205-branch-color.sh
@@ -0,0 +1,45 @@
+#!/bin/sh
+
+test_description='basic branch output coloring'
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'set up some sample branches' '
+ test_commit foo &&
+ git branch -M main &&
+ git update-ref refs/remotes/origin/main HEAD &&
+ git update-ref refs/heads/other HEAD
+'
+
+# choose non-default colors to make sure config
+# is taking effect
+test_expect_success 'set up some color config' '
+ git config color.branch.local blue &&
+ git config color.branch.remote yellow &&
+ git config color.branch.current cyan
+'
+
+test_expect_success 'regular output shows colors' '
+ cat >expect <<-\EOF &&
+ * <CYAN>main<RESET>
+ <BLUE>other<RESET>
+ <YELLOW>remotes/origin/main<RESET>
+ EOF
+ git branch --color -a >actual.raw &&
+ test_decode_color <actual.raw >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'verbose output shows colors' '
+ oid=$(git rev-parse --short HEAD) &&
+ cat >expect <<-EOF &&
+ * <CYAN>main <RESET> $oid foo
+ <BLUE>other <RESET> $oid foo
+ <YELLOW>remotes/origin/main<RESET> $oid foo
+ EOF
+ git branch --color -v -a >actual.raw &&
+ test_decode_color <actual.raw >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t3206-range-diff.sh b/t/t3206-range-diff.sh
new file mode 100755
index 0000000..7b05bf3
--- /dev/null
+++ b/t/t3206-range-diff.sh
@@ -0,0 +1,897 @@
+#!/bin/sh
+
+test_description='range-diff tests'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+# Note that because of the range-diff's heuristics, test_commit does more
+# harm than good. We need some real history.
+
+test_expect_success 'setup' '
+ git fast-import <"$TEST_DIRECTORY"/t3206/history.export &&
+ test_oid_cache <<-\EOF
+ # topic
+ t1 sha1:4de457d
+ t2 sha1:fccce22
+ t3 sha1:147e64e
+ t4 sha1:a63e992
+ t1 sha256:b89f8b9
+ t2 sha256:5f12aad
+ t3 sha256:ea8b273
+ t4 sha256:14b7336
+
+ # unmodified
+ u1 sha1:35b9b25
+ u2 sha1:de345ab
+ u3 sha1:9af6654
+ u4 sha1:2901f77
+ u1 sha256:e3731be
+ u2 sha256:14fadf8
+ u3 sha256:736c4bc
+ u4 sha256:673e77d
+
+ # topic (abbrev=10)
+ t1_abbrev sha1:4de457d2c0
+ t2_abbrev sha1:fccce22f8c
+ t3_abbrev sha1:147e64ef53
+ t4_abbrev sha1:a63e992599
+ t1_abbrev sha256:b89f8b9092
+ t2_abbrev sha256:5f12aadf34
+ t3_abbrev sha256:ea8b273a6c
+ t4_abbrev sha256:14b73361fc
+
+ # unmodified (abbrev=10)
+ u1_abbrev sha1:35b9b25f76
+ u2_abbrev sha1:de345ab3de
+ u3_abbrev sha1:9af6654000
+ u4_abbrev sha1:2901f773f3
+ u1_abbrev sha256:e3731be242
+ u2_abbrev sha256:14fadf8cee
+ u3_abbrev sha256:736c4bcb44
+ u4_abbrev sha256:673e77d589
+
+ # reordered
+ r1 sha1:aca177a
+ r2 sha1:14ad629
+ r3 sha1:ee58208
+ r4 sha1:307b27a
+ r1 sha256:f59d3aa
+ r2 sha256:fb261a8
+ r3 sha256:cb2649b
+ r4 sha256:958577e
+
+ # removed (deleted)
+ d1 sha1:7657159
+ d2 sha1:43d84d3
+ d3 sha1:a740396
+ d1 sha256:e312513
+ d2 sha256:eb19258
+ d3 sha256:1ccb3c1
+
+ # added
+ a1 sha1:2716022
+ a2 sha1:b62accd
+ a3 sha1:df46cfa
+ a4 sha1:3e64548
+ a5 sha1:12b4063
+ a1 sha256:d724f4d
+ a2 sha256:1de7762
+ a3 sha256:e159431
+ a4 sha256:b3e483c
+ a5 sha256:90866a7
+
+ # rebased
+ b1 sha1:cc9c443
+ b2 sha1:c5d9641
+ b3 sha1:28cc2b6
+ b4 sha1:5628ab7
+ b5 sha1:a31b12e
+ b1 sha256:a1a8717
+ b2 sha256:20a5862
+ b3 sha256:587172a
+ b4 sha256:2721c5d
+ b5 sha256:7b57864
+
+ # changed
+ c1 sha1:a4b3333
+ c2 sha1:f51d370
+ c3 sha1:0559556
+ c4 sha1:d966c5c
+ c1 sha256:f8c2b9d
+ c2 sha256:3fb6318
+ c3 sha256:168ab68
+ c4 sha256:3526539
+
+ # changed-message
+ m1 sha1:f686024
+ m2 sha1:4ab067d
+ m3 sha1:b9cb956
+ m4 sha1:8add5f1
+ m1 sha256:31e6281
+ m2 sha256:a06bf1b
+ m3 sha256:82dc654
+ m4 sha256:48470c5
+
+ # renamed
+ n1 sha1:f258d75
+ n2 sha1:017b62d
+ n3 sha1:3ce7af6
+ n4 sha1:1e6226b
+ n1 sha256:ad52114
+ n2 sha256:3b54c8f
+ n3 sha256:3b0a644
+ n4 sha256:e461653
+
+ # mode change
+ o1 sha1:4d39cb3
+ o2 sha1:26c107f
+ o3 sha1:4c1e0f5
+ o1 sha256:d0dd598
+ o2 sha256:c4a279e
+ o3 sha256:78459d7
+
+ # added and removed
+ s1 sha1:096b1ba
+ s2 sha1:d92e698
+ s3 sha1:9a1db4d
+ s4 sha1:fea3b5c
+ s1 sha256:a7f9134
+ s2 sha256:b4c2580
+ s3 sha256:1d62aa2
+ s4 sha256:48160e8
+
+ # Empty delimiter (included so lines match neatly)
+ __ sha1:-------
+ __ sha256:-------
+ EOF
+'
+
+test_expect_success 'simple A..B A..C (unmodified)' '
+ git range-diff --no-color main..topic main..unmodified \
+ >actual &&
+ cat >expect <<-EOF &&
+ 1: $(test_oid t1) = 1: $(test_oid u1) s/5/A/
+ 2: $(test_oid t2) = 2: $(test_oid u2) s/4/A/
+ 3: $(test_oid t3) = 3: $(test_oid u3) s/11/B/
+ 4: $(test_oid t4) = 4: $(test_oid u4) s/12/B/
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'simple B...C (unmodified)' '
+ git range-diff --no-color topic...unmodified >actual &&
+ # same "expect" as above
+ test_cmp expect actual
+'
+
+test_expect_success 'simple A B C (unmodified)' '
+ git range-diff --no-color main topic unmodified >actual &&
+ # same "expect" as above
+ test_cmp expect actual
+'
+
+test_expect_success 'simple A..B A..C (unmodified) with --abbrev' '
+ git range-diff --no-color --abbrev=10 main..topic main..unmodified \
+ >actual &&
+ cat >expect <<-EOF &&
+ 1: $(test_oid t1_abbrev) = 1: $(test_oid u1_abbrev) s/5/A/
+ 2: $(test_oid t2_abbrev) = 2: $(test_oid u2_abbrev) s/4/A/
+ 3: $(test_oid t3_abbrev) = 3: $(test_oid u3_abbrev) s/11/B/
+ 4: $(test_oid t4_abbrev) = 4: $(test_oid u4_abbrev) s/12/B/
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'A^! and A^-<n> (unmodified)' '
+ git range-diff --no-color topic^! unmodified^-1 >actual &&
+ cat >expect <<-EOF &&
+ 1: $(test_oid t4) = 1: $(test_oid u4) s/12/B/
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'A^{/..} is not mistaken for a range' '
+ test_must_fail git range-diff topic^.. topic^{/..} -- 2>error &&
+ test_grep "not a commit range" error
+'
+
+test_expect_success 'trivial reordering' '
+ git range-diff --no-color main topic reordered >actual &&
+ cat >expect <<-EOF &&
+ 1: $(test_oid t1) = 1: $(test_oid r1) s/5/A/
+ 3: $(test_oid t3) = 2: $(test_oid r2) s/11/B/
+ 4: $(test_oid t4) = 3: $(test_oid r3) s/12/B/
+ 2: $(test_oid t2) = 4: $(test_oid r4) s/4/A/
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'removed a commit' '
+ git range-diff --no-color main topic removed >actual &&
+ cat >expect <<-EOF &&
+ 1: $(test_oid t1) = 1: $(test_oid d1) s/5/A/
+ 2: $(test_oid t2) < -: $(test_oid __) s/4/A/
+ 3: $(test_oid t3) = 2: $(test_oid d2) s/11/B/
+ 4: $(test_oid t4) = 3: $(test_oid d3) s/12/B/
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'added a commit' '
+ git range-diff --no-color main topic added >actual &&
+ cat >expect <<-EOF &&
+ 1: $(test_oid t1) = 1: $(test_oid a1) s/5/A/
+ 2: $(test_oid t2) = 2: $(test_oid a2) s/4/A/
+ -: $(test_oid __) > 3: $(test_oid a3) s/6/A/
+ 3: $(test_oid t3) = 4: $(test_oid a4) s/11/B/
+ 4: $(test_oid t4) = 5: $(test_oid a5) s/12/B/
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'new base, A B C' '
+ git range-diff --no-color main topic rebased >actual &&
+ cat >expect <<-EOF &&
+ 1: $(test_oid t1) = 1: $(test_oid b1) s/5/A/
+ 2: $(test_oid t2) = 2: $(test_oid b2) s/4/A/
+ 3: $(test_oid t3) = 3: $(test_oid b3) s/11/B/
+ 4: $(test_oid t4) = 4: $(test_oid b4) s/12/B/
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'new base, B...C' '
+ # this syntax includes the commits from main!
+ git range-diff --no-color topic...rebased >actual &&
+ cat >expect <<-EOF &&
+ -: $(test_oid __) > 1: $(test_oid b5) unrelated
+ 1: $(test_oid t1) = 2: $(test_oid b1) s/5/A/
+ 2: $(test_oid t2) = 3: $(test_oid b2) s/4/A/
+ 3: $(test_oid t3) = 4: $(test_oid b3) s/11/B/
+ 4: $(test_oid t4) = 5: $(test_oid b4) s/12/B/
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'changed commit' '
+ git range-diff --no-color topic...changed >actual &&
+ cat >expect <<-EOF &&
+ 1: $(test_oid t1) = 1: $(test_oid c1) s/5/A/
+ 2: $(test_oid t2) = 2: $(test_oid c2) s/4/A/
+ 3: $(test_oid t3) ! 3: $(test_oid c3) s/11/B/
+ @@ file: A
+ 9
+ 10
+ -11
+ -+B
+ ++BB
+ 12
+ 13
+ 14
+ 4: $(test_oid t4) ! 4: $(test_oid c4) s/12/B/
+ @@ file
+ @@ file: A
+ 9
+ 10
+ - B
+ + BB
+ -12
+ +B
+ 13
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'changed commit with --no-patch diff option' '
+ git range-diff --no-color --no-patch topic...changed >actual &&
+ cat >expect <<-EOF &&
+ 1: $(test_oid t1) = 1: $(test_oid c1) s/5/A/
+ 2: $(test_oid t2) = 2: $(test_oid c2) s/4/A/
+ 3: $(test_oid t3) ! 3: $(test_oid c3) s/11/B/
+ 4: $(test_oid t4) ! 4: $(test_oid c4) s/12/B/
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'changed commit with --stat diff option' '
+ git range-diff --no-color --stat topic...changed >actual &&
+ cat >expect <<-EOF &&
+ 1: $(test_oid t1) = 1: $(test_oid c1) s/5/A/
+ 2: $(test_oid t2) = 2: $(test_oid c2) s/4/A/
+ 3: $(test_oid t3) ! 3: $(test_oid c3) s/11/B/
+ a => b | 2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
+ 4: $(test_oid t4) ! 4: $(test_oid c4) s/12/B/
+ a => b | 2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'changed commit with sm config' '
+ git range-diff --no-color --submodule=log topic...changed >actual &&
+ cat >expect <<-EOF &&
+ 1: $(test_oid t1) = 1: $(test_oid c1) s/5/A/
+ 2: $(test_oid t2) = 2: $(test_oid c2) s/4/A/
+ 3: $(test_oid t3) ! 3: $(test_oid c3) s/11/B/
+ @@ file: A
+ 9
+ 10
+ -11
+ -+B
+ ++BB
+ 12
+ 13
+ 14
+ 4: $(test_oid t4) ! 4: $(test_oid c4) s/12/B/
+ @@ file
+ @@ file: A
+ 9
+ 10
+ - B
+ + BB
+ -12
+ +B
+ 13
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'renamed file' '
+ git range-diff --no-color --submodule=log topic...renamed-file >actual &&
+ sed s/Z/\ /g >expect <<-EOF &&
+ 1: $(test_oid t1) = 1: $(test_oid n1) s/5/A/
+ 2: $(test_oid t2) ! 2: $(test_oid n2) s/4/A/
+ @@ Metadata
+ ZAuthor: Thomas Rast <trast@inf.ethz.ch>
+ Z
+ Z ## Commit message ##
+ - s/4/A/
+ + s/4/A/ + rename file
+ Z
+ - ## file ##
+ + ## file => renamed-file ##
+ Z@@
+ Z 1
+ Z 2
+ 3: $(test_oid t3) ! 3: $(test_oid n3) s/11/B/
+ @@ Metadata
+ Z ## Commit message ##
+ Z s/11/B/
+ Z
+ - ## file ##
+ -@@ file: A
+ + ## renamed-file ##
+ +@@ renamed-file: A
+ Z 8
+ Z 9
+ Z 10
+ 4: $(test_oid t4) ! 4: $(test_oid n4) s/12/B/
+ @@ Metadata
+ Z ## Commit message ##
+ Z s/12/B/
+ Z
+ - ## file ##
+ -@@ file: A
+ + ## renamed-file ##
+ +@@ renamed-file: A
+ Z 9
+ Z 10
+ Z B
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'file with mode only change' '
+ git range-diff --no-color --submodule=log topic...mode-only-change >actual &&
+ sed s/Z/\ /g >expect <<-EOF &&
+ 1: $(test_oid t2) ! 1: $(test_oid o1) s/4/A/
+ @@ Metadata
+ ZAuthor: Thomas Rast <trast@inf.ethz.ch>
+ Z
+ Z ## Commit message ##
+ - s/4/A/
+ + s/4/A/ + add other-file
+ Z
+ Z ## file ##
+ Z@@
+ @@ file
+ Z A
+ Z 6
+ Z 7
+ +
+ + ## other-file (new) ##
+ 2: $(test_oid t3) ! 2: $(test_oid o2) s/11/B/
+ @@ Metadata
+ ZAuthor: Thomas Rast <trast@inf.ethz.ch>
+ Z
+ Z ## Commit message ##
+ - s/11/B/
+ + s/11/B/ + mode change other-file
+ Z
+ Z ## file ##
+ Z@@ file: A
+ @@ file: A
+ Z 12
+ Z 13
+ Z 14
+ +
+ + ## other-file (mode change 100644 => 100755) ##
+ 3: $(test_oid t4) = 3: $(test_oid o3) s/12/B/
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'file added and later removed' '
+ git range-diff --no-color --submodule=log topic...added-removed >actual &&
+ sed s/Z/\ /g >expect <<-EOF &&
+ 1: $(test_oid t1) = 1: $(test_oid s1) s/5/A/
+ 2: $(test_oid t2) ! 2: $(test_oid s2) s/4/A/
+ @@ Metadata
+ ZAuthor: Thomas Rast <trast@inf.ethz.ch>
+ Z
+ Z ## Commit message ##
+ - s/4/A/
+ + s/4/A/ + new-file
+ Z
+ Z ## file ##
+ Z@@
+ @@ file
+ Z A
+ Z 6
+ Z 7
+ +
+ + ## new-file (new) ##
+ 3: $(test_oid t3) ! 3: $(test_oid s3) s/11/B/
+ @@ Metadata
+ ZAuthor: Thomas Rast <trast@inf.ethz.ch>
+ Z
+ Z ## Commit message ##
+ - s/11/B/
+ + s/11/B/ + remove file
+ Z
+ Z ## file ##
+ Z@@ file: A
+ @@ file: A
+ Z 12
+ Z 13
+ Z 14
+ +
+ + ## new-file (deleted) ##
+ 4: $(test_oid t4) = 4: $(test_oid s4) s/12/B/
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'no commits on one side' '
+ git commit --amend -m "new message" &&
+ git range-diff main HEAD@{1} HEAD
+'
+
+test_expect_success 'changed message' '
+ git range-diff --no-color topic...changed-message >actual &&
+ sed s/Z/\ /g >expect <<-EOF &&
+ 1: $(test_oid t1) = 1: $(test_oid m1) s/5/A/
+ 2: $(test_oid t2) ! 2: $(test_oid m2) s/4/A/
+ @@ Metadata
+ Z ## Commit message ##
+ Z s/4/A/
+ Z
+ + Also a silly comment here!
+ +
+ Z ## file ##
+ Z@@
+ Z 1
+ 3: $(test_oid t3) = 3: $(test_oid m3) s/11/B/
+ 4: $(test_oid t4) = 4: $(test_oid m4) s/12/B/
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'dual-coloring' '
+ sed -e "s|^:||" >expect <<-EOF &&
+ :<YELLOW>1: $(test_oid c1) = 1: $(test_oid m1) s/5/A/<RESET>
+ :<RED>2: $(test_oid c2) <RESET><YELLOW>!<RESET><GREEN> 2: $(test_oid m2)<RESET><YELLOW> s/4/A/<RESET>
+ : <REVERSE><CYAN>@@<RESET> <RESET>Metadata<RESET>
+ : ## Commit message ##<RESET>
+ : s/4/A/<RESET>
+ : <RESET>
+ : <REVERSE><GREEN>+<RESET><BOLD> Also a silly comment here!<RESET>
+ : <REVERSE><GREEN>+<RESET>
+ : ## file ##<RESET>
+ : <CYAN> @@<RESET>
+ : 1<RESET>
+ :<RED>3: $(test_oid c3) <RESET><YELLOW>!<RESET><GREEN> 3: $(test_oid m3)<RESET><YELLOW> s/11/B/<RESET>
+ : <REVERSE><CYAN>@@<RESET> <RESET>file: A<RESET>
+ : 9<RESET>
+ : 10<RESET>
+ : <RED> -11<RESET>
+ : <REVERSE><RED>-<RESET><FAINT;GREEN>+BB<RESET>
+ : <REVERSE><GREEN>+<RESET><BOLD;GREEN>+B<RESET>
+ : 12<RESET>
+ : 13<RESET>
+ : 14<RESET>
+ :<RED>4: $(test_oid c4) <RESET><YELLOW>!<RESET><GREEN> 4: $(test_oid m4)<RESET><YELLOW> s/12/B/<RESET>
+ : <REVERSE><CYAN>@@<RESET> <RESET>file<RESET>
+ : <CYAN> @@ file: A<RESET>
+ : 9<RESET>
+ : 10<RESET>
+ : <REVERSE><RED>-<RESET><FAINT> BB<RESET>
+ : <REVERSE><GREEN>+<RESET><BOLD> B<RESET>
+ : <RED> -12<RESET>
+ : <GREEN> +B<RESET>
+ : 13<RESET>
+ EOF
+ git range-diff changed...changed-message --color --dual-color >actual.raw &&
+ test_decode_color >actual <actual.raw &&
+ test_cmp expect actual
+'
+
+for prev in topic main..topic
+do
+ test_expect_success "format-patch --range-diff=$prev" '
+ git format-patch --cover-letter --range-diff=$prev \
+ main..unmodified >actual &&
+ test_when_finished "rm 000?-*" &&
+ test_line_count = 5 actual &&
+ test_grep "^Range-diff:$" 0000-* &&
+ grep "= 1: .* s/5/A" 0000-* &&
+ grep "= 2: .* s/4/A" 0000-* &&
+ grep "= 3: .* s/11/B" 0000-* &&
+ grep "= 4: .* s/12/B" 0000-*
+ '
+done
+
+test_expect_success 'format-patch --range-diff as commentary' '
+ git format-patch --range-diff=HEAD~1 HEAD~1 >actual &&
+ test_when_finished "rm 0001-*" &&
+ test_line_count = 1 actual &&
+ test_grep "^Range-diff:$" 0001-* &&
+ grep "> 1: .* new message" 0001-*
+'
+
+test_expect_success 'format-patch --range-diff reroll-count with a non-integer' '
+ git format-patch --range-diff=HEAD~1 -v2.9 HEAD~1 >actual &&
+ test_when_finished "rm v2.9-0001-*" &&
+ test_line_count = 1 actual &&
+ test_grep "^Range-diff:$" v2.9-0001-* &&
+ grep "> 1: .* new message" v2.9-0001-*
+'
+
+test_expect_success 'format-patch --range-diff reroll-count with a integer' '
+ git format-patch --range-diff=HEAD~1 -v2 HEAD~1 >actual &&
+ test_when_finished "rm v2-0001-*" &&
+ test_line_count = 1 actual &&
+ test_grep "^Range-diff ..* v1:$" v2-0001-* &&
+ grep "> 1: .* new message" v2-0001-*
+'
+
+test_expect_success 'format-patch --range-diff with v0' '
+ git format-patch --range-diff=HEAD~1 -v0 HEAD~1 >actual &&
+ test_when_finished "rm v0-0001-*" &&
+ test_line_count = 1 actual &&
+ test_grep "^Range-diff:$" v0-0001-* &&
+ grep "> 1: .* new message" v0-0001-*
+'
+
+test_expect_success 'range-diff overrides diff.noprefix internally' '
+ git -c diff.noprefix=true range-diff HEAD^...
+'
+
+test_expect_success 'basic with modified format.pretty with suffix' '
+ git -c format.pretty="format:commit %H%d%n" range-diff \
+ main..topic main..unmodified
+'
+
+test_expect_success 'basic with modified format.pretty without "commit "' '
+ git -c format.pretty="format:%H%n" range-diff \
+ main..topic main..unmodified
+'
+
+test_expect_success 'range-diff compares notes by default' '
+ git notes add -m "topic note" topic &&
+ git notes add -m "unmodified note" unmodified &&
+ test_when_finished git notes remove topic unmodified &&
+ git range-diff --no-color main..topic main..unmodified \
+ >actual &&
+ sed s/Z/\ /g >expect <<-EOF &&
+ 1: $(test_oid t1) = 1: $(test_oid u1) s/5/A/
+ 2: $(test_oid t2) = 2: $(test_oid u2) s/4/A/
+ 3: $(test_oid t3) = 3: $(test_oid u3) s/11/B/
+ 4: $(test_oid t4) ! 4: $(test_oid u4) s/12/B/
+ @@ Commit message
+ Z
+ Z
+ Z ## Notes ##
+ - topic note
+ + unmodified note
+ Z
+ Z ## file ##
+ Z@@ file: A
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'range-diff with --no-notes' '
+ git notes add -m "topic note" topic &&
+ git notes add -m "unmodified note" unmodified &&
+ test_when_finished git notes remove topic unmodified &&
+ git range-diff --no-color --no-notes main..topic main..unmodified \
+ >actual &&
+ cat >expect <<-EOF &&
+ 1: $(test_oid t1) = 1: $(test_oid u1) s/5/A/
+ 2: $(test_oid t2) = 2: $(test_oid u2) s/4/A/
+ 3: $(test_oid t3) = 3: $(test_oid u3) s/11/B/
+ 4: $(test_oid t4) = 4: $(test_oid u4) s/12/B/
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'range-diff with multiple --notes' '
+ git notes --ref=note1 add -m "topic note1" topic &&
+ git notes --ref=note1 add -m "unmodified note1" unmodified &&
+ test_when_finished git notes --ref=note1 remove topic unmodified &&
+ git notes --ref=note2 add -m "topic note2" topic &&
+ git notes --ref=note2 add -m "unmodified note2" unmodified &&
+ test_when_finished git notes --ref=note2 remove topic unmodified &&
+ git range-diff --no-color --notes=note1 --notes=note2 main..topic main..unmodified \
+ >actual &&
+ sed s/Z/\ /g >expect <<-EOF &&
+ 1: $(test_oid t1) = 1: $(test_oid u1) s/5/A/
+ 2: $(test_oid t2) = 2: $(test_oid u2) s/4/A/
+ 3: $(test_oid t3) = 3: $(test_oid u3) s/11/B/
+ 4: $(test_oid t4) ! 4: $(test_oid u4) s/12/B/
+ @@ Commit message
+ Z
+ Z
+ Z ## Notes (note1) ##
+ - topic note1
+ + unmodified note1
+ Z
+ Z
+ Z ## Notes (note2) ##
+ - topic note2
+ + unmodified note2
+ Z
+ Z ## file ##
+ Z@@ file: A
+ EOF
+ test_cmp expect actual
+'
+
+# `range-diff` should act like `log` with regards to notes
+test_expect_success 'range-diff with --notes=custom does not show default notes' '
+ git notes add -m "topic note" topic &&
+ git notes add -m "unmodified note" unmodified &&
+ git notes --ref=custom add -m "topic note" topic &&
+ git notes --ref=custom add -m "unmodified note" unmodified &&
+ test_when_finished git notes remove topic unmodified &&
+ test_when_finished git notes --ref=custom remove topic unmodified &&
+ git range-diff --notes=custom main..topic main..unmodified \
+ >actual &&
+ ! grep "## Notes ##" actual &&
+ grep "## Notes (custom) ##" actual
+'
+
+test_expect_success 'format-patch --range-diff does not compare notes by default' '
+ git notes add -m "topic note" topic &&
+ git notes add -m "unmodified note" unmodified &&
+ test_when_finished git notes remove topic unmodified &&
+ git format-patch --cover-letter --range-diff=$prev \
+ main..unmodified >actual &&
+ test_when_finished "rm 000?-*" &&
+ test_line_count = 5 actual &&
+ test_grep "^Range-diff:$" 0000-* &&
+ grep "= 1: .* s/5/A" 0000-* &&
+ grep "= 2: .* s/4/A" 0000-* &&
+ grep "= 3: .* s/11/B" 0000-* &&
+ grep "= 4: .* s/12/B" 0000-* &&
+ ! grep "Notes" 0000-* &&
+ ! grep "note" 0000-*
+'
+
+test_expect_success 'format-patch --notes=custom --range-diff only compares custom notes' '
+ git notes add -m "topic note" topic &&
+ git notes --ref=custom add -m "topic note (custom)" topic &&
+ git notes add -m "unmodified note" unmodified &&
+ git notes --ref=custom add -m "unmodified note (custom)" unmodified &&
+ test_when_finished git notes remove topic unmodified &&
+ test_when_finished git notes --ref=custom remove topic unmodified &&
+ git format-patch --notes=custom --cover-letter --range-diff=$prev \
+ main..unmodified >actual &&
+ test_when_finished "rm 000?-*" &&
+ grep "## Notes (custom) ##" 0000-* &&
+ ! grep "## Notes ##" 0000-*
+'
+
+test_expect_success 'format-patch --range-diff with --no-notes' '
+ git notes add -m "topic note" topic &&
+ git notes add -m "unmodified note" unmodified &&
+ test_when_finished git notes remove topic unmodified &&
+ git format-patch --no-notes --cover-letter --range-diff=$prev \
+ main..unmodified >actual &&
+ test_when_finished "rm 000?-*" &&
+ test_line_count = 5 actual &&
+ test_grep "^Range-diff:$" 0000-* &&
+ grep "= 1: .* s/5/A" 0000-* &&
+ grep "= 2: .* s/4/A" 0000-* &&
+ grep "= 3: .* s/11/B" 0000-* &&
+ grep "= 4: .* s/12/B" 0000-* &&
+ ! grep "Notes" 0000-* &&
+ ! grep "note" 0000-*
+'
+
+test_expect_success 'format-patch --range-diff with --notes' '
+ git notes add -m "topic note" topic &&
+ git notes add -m "unmodified note" unmodified &&
+ test_when_finished git notes remove topic unmodified &&
+ git format-patch --notes --cover-letter --range-diff=$prev \
+ main..unmodified >actual &&
+ test_when_finished "rm 000?-*" &&
+ test_line_count = 5 actual &&
+ test_grep "^Range-diff:$" 0000-* &&
+ grep "= 1: .* s/5/A" 0000-* &&
+ grep "= 2: .* s/4/A" 0000-* &&
+ grep "= 3: .* s/11/B" 0000-* &&
+ grep "! 4: .* s/12/B" 0000-* &&
+ sed s/Z/\ /g >expect <<-EOF &&
+ @@ Commit message
+ Z
+ Z
+ Z ## Notes ##
+ - topic note
+ + unmodified note
+ Z
+ Z ## file ##
+ Z@@ file: A
+ EOF
+ sed "/@@ Commit message/,/@@ file: A/!d" 0000-* >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'format-patch --range-diff with format.notes config' '
+ git notes add -m "topic note" topic &&
+ git notes add -m "unmodified note" unmodified &&
+ test_when_finished git notes remove topic unmodified &&
+ test_config format.notes true &&
+ git format-patch --cover-letter --range-diff=$prev \
+ main..unmodified >actual &&
+ test_when_finished "rm 000?-*" &&
+ test_line_count = 5 actual &&
+ test_grep "^Range-diff:$" 0000-* &&
+ grep "= 1: .* s/5/A" 0000-* &&
+ grep "= 2: .* s/4/A" 0000-* &&
+ grep "= 3: .* s/11/B" 0000-* &&
+ grep "! 4: .* s/12/B" 0000-* &&
+ sed s/Z/\ /g >expect <<-EOF &&
+ @@ Commit message
+ Z
+ Z
+ Z ## Notes ##
+ - topic note
+ + unmodified note
+ Z
+ Z ## file ##
+ Z@@ file: A
+ EOF
+ sed "/@@ Commit message/,/@@ file: A/!d" 0000-* >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'format-patch --range-diff with multiple notes' '
+ git notes --ref=note1 add -m "topic note1" topic &&
+ git notes --ref=note1 add -m "unmodified note1" unmodified &&
+ test_when_finished git notes --ref=note1 remove topic unmodified &&
+ git notes --ref=note2 add -m "topic note2" topic &&
+ git notes --ref=note2 add -m "unmodified note2" unmodified &&
+ test_when_finished git notes --ref=note2 remove topic unmodified &&
+ git format-patch --notes=note1 --notes=note2 --cover-letter --range-diff=$prev \
+ main..unmodified >actual &&
+ test_when_finished "rm 000?-*" &&
+ test_line_count = 5 actual &&
+ test_grep "^Range-diff:$" 0000-* &&
+ grep "= 1: .* s/5/A" 0000-* &&
+ grep "= 2: .* s/4/A" 0000-* &&
+ grep "= 3: .* s/11/B" 0000-* &&
+ grep "! 4: .* s/12/B" 0000-* &&
+ sed s/Z/\ /g >expect <<-EOF &&
+ @@ Commit message
+ Z
+ Z
+ Z ## Notes (note1) ##
+ - topic note1
+ + unmodified note1
+ Z
+ Z
+ Z ## Notes (note2) ##
+ - topic note2
+ + unmodified note2
+ Z
+ Z ## file ##
+ Z@@ file: A
+ EOF
+ sed "/@@ Commit message/,/@@ file: A/!d" 0000-* >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--left-only/--right-only' '
+ git switch --orphan left-right &&
+ test_commit first &&
+ test_commit unmatched &&
+ test_commit common &&
+ git switch -C left-right first &&
+ git cherry-pick common &&
+
+ git range-diff -s --left-only ...common >actual &&
+ head_oid=$(git rev-parse --short HEAD) &&
+ common_oid=$(git rev-parse --short common) &&
+ echo "1: $head_oid = 2: $common_oid common" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'ranges with pathspecs' '
+ git range-diff topic...mode-only-change -- other-file >actual &&
+ test_line_count = 2 actual &&
+ topic_oid=$(git rev-parse --short topic) &&
+ mode_change_oid=$(git rev-parse --short mode-only-change^) &&
+ file_change_oid=$(git rev-parse --short mode-only-change) &&
+ grep "$mode_change_oid" actual &&
+ ! grep "$file_change_oid" actual &&
+ ! grep "$topic_oid" actual
+'
+
+test_expect_success 'submodule changes are shown irrespective of diff.submodule' '
+ git init sub-repo &&
+ test_commit -C sub-repo sub-first &&
+ sub_oid1=$(git -C sub-repo rev-parse HEAD) &&
+ test_commit -C sub-repo sub-second &&
+ sub_oid2=$(git -C sub-repo rev-parse HEAD) &&
+ test_commit -C sub-repo sub-third &&
+ sub_oid3=$(git -C sub-repo rev-parse HEAD) &&
+
+ git checkout -b main-sub topic &&
+ git -c protocol.file.allow=always submodule add ./sub-repo sub &&
+ git -C sub checkout --detach sub-first &&
+ git commit -m "add sub" sub &&
+ sup_oid1=$(git rev-parse --short HEAD) &&
+ git checkout -b topic-sub &&
+ git -C sub checkout sub-second &&
+ git commit -m "change sub" sub &&
+ sup_oid2=$(git rev-parse --short HEAD) &&
+ git checkout -b modified-sub main-sub &&
+ git -C sub checkout sub-third &&
+ git commit -m "change sub" sub &&
+ sup_oid3=$(git rev-parse --short HEAD) &&
+ sup_oid0=$(test_oid __) &&
+
+ test_config diff.submodule log &&
+ git range-diff topic topic-sub modified-sub >actual &&
+ cat >expect <<-EOF &&
+ 1: $sup_oid1 = 1: $sup_oid1 add sub
+ 2: $sup_oid2 < -: $sup_oid0 change sub
+ -: $sup_oid0 > 2: $sup_oid3 change sub
+ EOF
+ test_cmp expect actual &&
+ test_config diff.submodule diff &&
+ git range-diff topic topic-sub modified-sub >actual &&
+ git range-diff --creation-factor=100 topic topic-sub modified-sub >actual &&
+ cat >expect <<-EOF &&
+ 1: $sup_oid1 = 1: $sup_oid1 add sub
+ 2: $sup_oid2 ! 2: $sup_oid3 change sub
+ @@ Commit message
+ ## sub ##
+ @@
+ -Subproject commit $sub_oid1
+ -+Subproject commit $sub_oid2
+ ++Subproject commit $sub_oid3
+ EOF
+ test_cmp expect actual &&
+ test_config diff.submodule diff &&
+ git range-diff --creation-factor=100 topic topic-sub modified-sub >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t3206/history.export b/t/t3206/history.export
new file mode 100644
index 0000000..4485adc
--- /dev/null
+++ b/t/t3206/history.export
@@ -0,0 +1,709 @@
+blob
+mark :1
+data 51
+1
+2
+3
+4
+5
+6
+7
+8
+9
+10
+11
+12
+13
+14
+15
+16
+17
+18
+19
+20
+
+reset refs/heads/renamed-file
+commit refs/heads/renamed-file
+mark :2
+author Thomas Rast <trast@inf.ethz.ch> 1374424921 +0200
+committer Thomas Rast <trast@inf.ethz.ch> 1374484724 +0200
+data 8
+initial
+M 100644 :1 file
+
+blob
+mark :3
+data 51
+1
+2
+3
+4
+A
+6
+7
+8
+9
+10
+11
+12
+13
+14
+15
+16
+17
+18
+19
+20
+
+commit refs/heads/mode-only-change
+mark :4
+author Thomas Rast <trast@inf.ethz.ch> 1374485014 +0200
+committer Thomas Rast <trast@inf.ethz.ch> 1374485014 +0200
+data 7
+s/5/A/
+from :2
+M 100644 :3 file
+
+blob
+mark :5
+data 51
+1
+2
+3
+A
+A
+6
+7
+8
+9
+10
+11
+12
+13
+14
+15
+16
+17
+18
+19
+20
+
+commit refs/heads/topic
+mark :6
+author Thomas Rast <trast@inf.ethz.ch> 1374485024 +0200
+committer Thomas Rast <trast@inf.ethz.ch> 1374485024 +0200
+data 7
+s/4/A/
+from :4
+M 100644 :5 file
+
+blob
+mark :7
+data 50
+1
+2
+3
+A
+A
+6
+7
+8
+9
+10
+B
+12
+13
+14
+15
+16
+17
+18
+19
+20
+
+commit refs/heads/topic
+mark :8
+author Thomas Rast <trast@inf.ethz.ch> 1374485036 +0200
+committer Thomas Rast <trast@inf.ethz.ch> 1374485036 +0200
+data 8
+s/11/B/
+from :6
+M 100644 :7 file
+
+blob
+mark :9
+data 49
+1
+2
+3
+A
+A
+6
+7
+8
+9
+10
+B
+B
+13
+14
+15
+16
+17
+18
+19
+20
+
+commit refs/heads/topic
+mark :10
+author Thomas Rast <trast@inf.ethz.ch> 1374485044 +0200
+committer Thomas Rast <trast@inf.ethz.ch> 1374485044 +0200
+data 8
+s/12/B/
+from :8
+M 100644 :9 file
+
+blob
+mark :11
+data 10
+unrelated
+
+commit refs/heads/main
+mark :12
+author Thomas Rast <trast@inf.ethz.ch> 1374485127 +0200
+committer Thomas Rast <trast@inf.ethz.ch> 1374485127 +0200
+data 10
+unrelated
+from :2
+M 100644 :11 otherfile
+
+commit refs/heads/rebased
+mark :13
+author Thomas Rast <trast@inf.ethz.ch> 1374485014 +0200
+committer Thomas Rast <trast@inf.ethz.ch> 1374485137 +0200
+data 7
+s/5/A/
+from :12
+M 100644 :3 file
+
+commit refs/heads/rebased
+mark :14
+author Thomas Rast <trast@inf.ethz.ch> 1374485024 +0200
+committer Thomas Rast <trast@inf.ethz.ch> 1374485138 +0200
+data 7
+s/4/A/
+from :13
+M 100644 :5 file
+
+commit refs/heads/rebased
+mark :15
+author Thomas Rast <trast@inf.ethz.ch> 1374485036 +0200
+committer Thomas Rast <trast@inf.ethz.ch> 1374485138 +0200
+data 8
+s/11/B/
+from :14
+M 100644 :7 file
+
+commit refs/heads/rebased
+mark :16
+author Thomas Rast <trast@inf.ethz.ch> 1374485044 +0200
+committer Thomas Rast <trast@inf.ethz.ch> 1374485138 +0200
+data 8
+s/12/B/
+from :15
+M 100644 :9 file
+
+commit refs/heads/added
+mark :17
+author Thomas Rast <trast@inf.ethz.ch> 1374485014 +0200
+committer Thomas Rast <trast@inf.ethz.ch> 1374485341 +0200
+data 7
+s/5/A/
+from :2
+M 100644 :3 file
+
+commit refs/heads/added
+mark :18
+author Thomas Rast <trast@inf.ethz.ch> 1374485024 +0200
+committer Thomas Rast <trast@inf.ethz.ch> 1374485341 +0200
+data 7
+s/4/A/
+from :17
+M 100644 :5 file
+
+blob
+mark :19
+data 51
+1
+2
+3
+A
+A
+A
+7
+8
+9
+10
+11
+12
+13
+14
+15
+16
+17
+18
+19
+20
+
+commit refs/heads/added
+mark :20
+author Thomas Rast <trast@inf.ethz.ch> 1374485186 +0200
+committer Thomas Rast <trast@inf.ethz.ch> 1374485341 +0200
+data 7
+s/6/A/
+from :18
+M 100644 :19 file
+
+blob
+mark :21
+data 50
+1
+2
+3
+A
+A
+A
+7
+8
+9
+10
+B
+12
+13
+14
+15
+16
+17
+18
+19
+20
+
+commit refs/heads/added
+mark :22
+author Thomas Rast <trast@inf.ethz.ch> 1374485036 +0200
+committer Thomas Rast <trast@inf.ethz.ch> 1374485341 +0200
+data 8
+s/11/B/
+from :20
+M 100644 :21 file
+
+blob
+mark :23
+data 49
+1
+2
+3
+A
+A
+A
+7
+8
+9
+10
+B
+B
+13
+14
+15
+16
+17
+18
+19
+20
+
+commit refs/heads/added
+mark :24
+author Thomas Rast <trast@inf.ethz.ch> 1374485044 +0200
+committer Thomas Rast <trast@inf.ethz.ch> 1374485341 +0200
+data 8
+s/12/B/
+from :22
+M 100644 :23 file
+
+commit refs/heads/reordered
+mark :25
+author Thomas Rast <trast@inf.ethz.ch> 1374485014 +0200
+committer Thomas Rast <trast@inf.ethz.ch> 1374485350 +0200
+data 7
+s/5/A/
+from :2
+M 100644 :3 file
+
+blob
+mark :26
+data 50
+1
+2
+3
+4
+A
+6
+7
+8
+9
+10
+B
+12
+13
+14
+15
+16
+17
+18
+19
+20
+
+commit refs/heads/reordered
+mark :27
+author Thomas Rast <trast@inf.ethz.ch> 1374485036 +0200
+committer Thomas Rast <trast@inf.ethz.ch> 1374485350 +0200
+data 8
+s/11/B/
+from :25
+M 100644 :26 file
+
+blob
+mark :28
+data 49
+1
+2
+3
+4
+A
+6
+7
+8
+9
+10
+B
+B
+13
+14
+15
+16
+17
+18
+19
+20
+
+commit refs/heads/reordered
+mark :29
+author Thomas Rast <trast@inf.ethz.ch> 1374485044 +0200
+committer Thomas Rast <trast@inf.ethz.ch> 1374485350 +0200
+data 8
+s/12/B/
+from :27
+M 100644 :28 file
+
+commit refs/heads/reordered
+mark :30
+author Thomas Rast <trast@inf.ethz.ch> 1374485024 +0200
+committer Thomas Rast <trast@inf.ethz.ch> 1374485350 +0200
+data 7
+s/4/A/
+from :29
+M 100644 :9 file
+
+commit refs/heads/changed
+mark :31
+author Thomas Rast <trast@inf.ethz.ch> 1374485014 +0200
+committer Thomas Rast <trast@inf.ethz.ch> 1374485507 +0200
+data 7
+s/5/A/
+from :2
+M 100644 :3 file
+
+commit refs/heads/changed
+mark :32
+author Thomas Rast <trast@inf.ethz.ch> 1374485024 +0200
+committer Thomas Rast <trast@inf.ethz.ch> 1374485507 +0200
+data 7
+s/4/A/
+from :31
+M 100644 :5 file
+
+blob
+mark :33
+data 51
+1
+2
+3
+A
+A
+6
+7
+8
+9
+10
+BB
+12
+13
+14
+15
+16
+17
+18
+19
+20
+
+commit refs/heads/changed
+mark :34
+author Thomas Rast <trast@inf.ethz.ch> 1374485036 +0200
+committer Thomas Rast <trast@inf.ethz.ch> 1374485507 +0200
+data 8
+s/11/B/
+from :32
+M 100644 :33 file
+
+blob
+mark :35
+data 50
+1
+2
+3
+A
+A
+6
+7
+8
+9
+10
+BB
+B
+13
+14
+15
+16
+17
+18
+19
+20
+
+commit refs/heads/changed
+mark :36
+author Thomas Rast <trast@inf.ethz.ch> 1374485044 +0200
+committer Thomas Rast <trast@inf.ethz.ch> 1374485507 +0200
+data 8
+s/12/B/
+from :34
+M 100644 :35 file
+
+commit refs/heads/changed-message
+mark :37
+author Thomas Rast <trast@inf.ethz.ch> 1374485014 +0200
+committer Thomas Rast <trast@inf.ethz.ch> 1374485530 +0200
+data 7
+s/5/A/
+from :2
+M 100644 :3 file
+
+commit refs/heads/changed-message
+mark :38
+author Thomas Rast <trast@inf.ethz.ch> 1374485024 +0200
+committer Thomas Rast <trast@inf.ethz.ch> 1374485530 +0200
+data 35
+s/4/A/
+
+Also a silly comment here!
+from :37
+M 100644 :5 file
+
+commit refs/heads/changed-message
+mark :39
+author Thomas Rast <trast@inf.ethz.ch> 1374485036 +0200
+committer Thomas Rast <trast@inf.ethz.ch> 1374485536 +0200
+data 8
+s/11/B/
+from :38
+M 100644 :7 file
+
+commit refs/heads/changed-message
+mark :40
+author Thomas Rast <trast@inf.ethz.ch> 1374485044 +0200
+committer Thomas Rast <trast@inf.ethz.ch> 1374485536 +0200
+data 8
+s/12/B/
+from :39
+M 100644 :9 file
+
+commit refs/heads/unmodified
+mark :41
+author Thomas Rast <trast@inf.ethz.ch> 1374485014 +0200
+committer Thomas Rast <trast@inf.ethz.ch> 1374485631 +0200
+data 7
+s/5/A/
+from :2
+M 100644 :3 file
+
+commit refs/heads/unmodified
+mark :42
+author Thomas Rast <trast@inf.ethz.ch> 1374485024 +0200
+committer Thomas Rast <trast@inf.ethz.ch> 1374485631 +0200
+data 7
+s/4/A/
+from :41
+M 100644 :5 file
+
+commit refs/heads/unmodified
+mark :43
+author Thomas Rast <trast@inf.ethz.ch> 1374485036 +0200
+committer Thomas Rast <trast@inf.ethz.ch> 1374485632 +0200
+data 8
+s/11/B/
+from :42
+M 100644 :7 file
+
+commit refs/heads/unmodified
+mark :44
+author Thomas Rast <trast@inf.ethz.ch> 1374485044 +0200
+committer Thomas Rast <trast@inf.ethz.ch> 1374485632 +0200
+data 8
+s/12/B/
+from :43
+M 100644 :9 file
+
+commit refs/heads/removed
+mark :45
+author Thomas Rast <trast@inf.ethz.ch> 1374485014 +0200
+committer Thomas Rast <trast@inf.ethz.ch> 1374486061 +0200
+data 7
+s/5/A/
+from :2
+M 100644 :3 file
+
+commit refs/heads/removed
+mark :46
+author Thomas Rast <trast@inf.ethz.ch> 1374485036 +0200
+committer Thomas Rast <trast@inf.ethz.ch> 1374486061 +0200
+data 8
+s/11/B/
+from :45
+M 100644 :26 file
+
+commit refs/heads/removed
+mark :47
+author Thomas Rast <trast@inf.ethz.ch> 1374485044 +0200
+committer Thomas Rast <trast@inf.ethz.ch> 1374486061 +0200
+data 8
+s/12/B/
+from :46
+M 100644 :28 file
+
+commit refs/heads/added-removed
+mark :48
+author Thomas Rast <trast@inf.ethz.ch> 1374485014 +0200
+committer Thomas Gummerer <t.gummerer@gmail.com> 1556574151 +0100
+data 7
+s/5/A/
+from :2
+M 100644 :3 file
+
+blob
+mark :49
+data 0
+
+commit refs/heads/added-removed
+mark :50
+author Thomas Rast <trast@inf.ethz.ch> 1374485024 +0200
+committer Thomas Gummerer <t.gummerer@gmail.com> 1556574177 +0100
+data 18
+s/4/A/ + new-file
+from :48
+M 100644 :5 file
+M 100644 :49 new-file
+
+commit refs/heads/added-removed
+mark :51
+author Thomas Rast <trast@inf.ethz.ch> 1374485036 +0200
+committer Thomas Gummerer <t.gummerer@gmail.com> 1556574177 +0100
+data 22
+s/11/B/ + remove file
+from :50
+M 100644 :7 file
+D new-file
+
+commit refs/heads/added-removed
+mark :52
+author Thomas Rast <trast@inf.ethz.ch> 1374485044 +0200
+committer Thomas Gummerer <t.gummerer@gmail.com> 1556574177 +0100
+data 8
+s/12/B/
+from :51
+M 100644 :9 file
+
+commit refs/heads/renamed-file
+mark :53
+author Thomas Rast <trast@inf.ethz.ch> 1374485014 +0200
+committer Thomas Gummerer <t.gummerer@gmail.com> 1556574309 +0100
+data 7
+s/5/A/
+from :2
+M 100644 :3 file
+
+commit refs/heads/renamed-file
+mark :54
+author Thomas Rast <trast@inf.ethz.ch> 1374485024 +0200
+committer Thomas Gummerer <t.gummerer@gmail.com> 1556574312 +0100
+data 21
+s/4/A/ + rename file
+from :53
+D file
+M 100644 :5 renamed-file
+
+commit refs/heads/renamed-file
+mark :55
+author Thomas Rast <trast@inf.ethz.ch> 1374485036 +0200
+committer Thomas Gummerer <t.gummerer@gmail.com> 1556574319 +0100
+data 8
+s/11/B/
+from :54
+M 100644 :7 renamed-file
+
+commit refs/heads/renamed-file
+mark :56
+author Thomas Rast <trast@inf.ethz.ch> 1374485044 +0200
+committer Thomas Gummerer <t.gummerer@gmail.com> 1556574319 +0100
+data 8
+s/12/B/
+from :55
+M 100644 :9 renamed-file
+
+commit refs/heads/mode-only-change
+mark :57
+author Thomas Rast <trast@inf.ethz.ch> 1374485024 +0200
+committer Thomas Gummerer <t.gummerer@gmail.com> 1570473767 +0100
+data 24
+s/4/A/ + add other-file
+from :4
+M 100644 :5 file
+M 100644 :49 other-file
+
+commit refs/heads/mode-only-change
+mark :58
+author Thomas Rast <trast@inf.ethz.ch> 1374485036 +0200
+committer Thomas Gummerer <t.gummerer@gmail.com> 1570473768 +0100
+data 33
+s/11/B/ + mode change other-file
+from :57
+M 100644 :7 file
+M 100755 :49 other-file
+
+commit refs/heads/mode-only-change
+mark :59
+author Thomas Rast <trast@inf.ethz.ch> 1374485044 +0200
+committer Thomas Gummerer <t.gummerer@gmail.com> 1570473768 +0100
+data 8
+s/12/B/
+from :58
+M 100644 :9 file
+
diff --git a/t/t3207-branch-submodule.sh b/t/t3207-branch-submodule.sh
new file mode 100755
index 0000000..fe72b24
--- /dev/null
+++ b/t/t3207-branch-submodule.sh
@@ -0,0 +1,329 @@
+#!/bin/sh
+
+test_description='git branch submodule tests'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-rebase.sh
+
+pwd=$(pwd)
+
+# Creates a clean test environment in "pwd" by copying the repo setup
+# from test_dirs.
+reset_test () {
+ rm -fr super &&
+ rm -fr sub-sub-upstream &&
+ rm -fr sub-upstream &&
+ cp -r test_dirs/* .
+}
+
+# Tests that the expected branch does not exist
+test_no_branch () {
+ DIR=$1 &&
+ BRANCH_NAME=$2 &&
+ test_must_fail git -C "$DIR" rev-parse "$BRANCH_NAME" 2>err &&
+ grep "ambiguous argument .$BRANCH_NAME." err
+}
+
+test_expect_success 'setup superproject and submodule' '
+ git config --global protocol.file.allow always &&
+ mkdir test_dirs &&
+ (
+ cd test_dirs &&
+ git init super &&
+ test_commit -C super foo &&
+ git init sub-sub-upstream &&
+ test_commit -C sub-sub-upstream foo &&
+ git init sub-upstream &&
+ # Submodule in a submodule
+ git -C sub-upstream submodule add "${pwd}/test_dirs/sub-sub-upstream" sub-sub &&
+ git -C sub-upstream commit -m "add submodule" &&
+ # Regular submodule
+ git -C super submodule add "${pwd}/test_dirs/sub-upstream" sub &&
+ # Submodule in a subdirectory
+ git -C super submodule add "${pwd}/test_dirs/sub-sub-upstream" second/sub &&
+ git -C super commit -m "add submodule" &&
+ git -C super config submodule.propagateBranches true &&
+ git -C super/sub submodule update --init
+ ) &&
+ reset_test
+'
+
+# Test the argument parsing
+test_expect_success '--recurse-submodules should create branches' '
+ test_when_finished "reset_test" &&
+ (
+ cd super &&
+ git branch --recurse-submodules branch-a &&
+ git rev-parse branch-a &&
+ git -C sub rev-parse branch-a &&
+ git -C sub/sub-sub rev-parse branch-a &&
+ git -C second/sub rev-parse branch-a
+ )
+'
+
+test_expect_success '--recurse-submodules should die if submodule.propagateBranches is false' '
+ test_when_finished "reset_test" &&
+ (
+ cd super &&
+ echo "fatal: branch with --recurse-submodules can only be used if submodule.propagateBranches is enabled" >expected &&
+ test_must_fail git -c submodule.propagateBranches=false branch --recurse-submodules branch-a 2>actual &&
+ test_cmp expected actual
+ )
+'
+
+test_expect_success '--recurse-submodules should fail when not creating branches' '
+ test_when_finished "reset_test" &&
+ (
+ cd super &&
+ git branch --recurse-submodules branch-a &&
+ echo "fatal: --recurse-submodules can only be used to create branches" >expected &&
+ test_must_fail git branch --recurse-submodules -D branch-a 2>actual &&
+ test_cmp expected actual &&
+ # Assert that the branches were not deleted
+ git rev-parse branch-a &&
+ git -C sub rev-parse branch-a
+ )
+'
+
+test_expect_success 'should respect submodule.recurse when creating branches' '
+ test_when_finished "reset_test" &&
+ (
+ cd super &&
+ git -c submodule.recurse=true branch branch-a &&
+ git rev-parse branch-a &&
+ git -C sub rev-parse branch-a
+ )
+'
+
+test_expect_success 'should ignore submodule.recurse when not creating branches' '
+ test_when_finished "reset_test" &&
+ (
+ cd super &&
+ git branch --recurse-submodules branch-a &&
+ git -c submodule.recurse=true branch -D branch-a &&
+ test_no_branch . branch-a &&
+ git -C sub rev-parse branch-a
+ )
+'
+
+# Test branch creation behavior
+test_expect_success 'should create branches based off commit id in superproject' '
+ test_when_finished "reset_test" &&
+ (
+ cd super &&
+ git branch --recurse-submodules branch-a &&
+ git checkout --recurse-submodules branch-a &&
+ git -C sub rev-parse HEAD >expected &&
+ # Move the tip of sub:branch-a so that it no longer matches the commit in super:branch-a
+ git -C sub checkout branch-a &&
+ test_commit -C sub bar &&
+ # Create a new branch-b branch with start-point=branch-a
+ git branch --recurse-submodules branch-b branch-a &&
+ git rev-parse branch-b &&
+ git -C sub rev-parse branch-b >actual &&
+ # Assert that the commit id of sub:second-branch matches super:branch-a and not sub:branch-a
+ test_cmp expected actual
+ )
+'
+
+test_expect_success 'should not create any branches if branch is not valid for all repos' '
+ test_when_finished "reset_test" &&
+ (
+ cd super &&
+ git -C sub branch branch-a &&
+ test_must_fail git branch --recurse-submodules branch-a 2>actual &&
+ test_no_branch . branch-a &&
+ grep "submodule .sub.: fatal: a branch named .branch-a. already exists" actual
+ )
+'
+
+test_expect_success 'should create branches if branch exists and --force is given' '
+ test_when_finished "reset_test" &&
+ (
+ cd super &&
+ git -C sub rev-parse HEAD >expected &&
+ test_commit -C sub baz &&
+ # branch-a in sub now points to a newer commit.
+ git -C sub branch branch-a HEAD &&
+ git -C sub rev-parse branch-a >actual-old-branch-a &&
+ git branch --recurse-submodules --force branch-a &&
+ git rev-parse branch-a &&
+ git -C sub rev-parse branch-a >actual-new-branch-a &&
+ test_cmp expected actual-new-branch-a &&
+ # assert that branch --force actually moved the sub
+ # branch
+ ! test_cmp expected actual-old-branch-a
+ )
+'
+
+test_expect_success 'should create branch when submodule is not in HEAD:.gitmodules' '
+ test_when_finished "reset_test" &&
+ (
+ cd super &&
+ git branch branch-a &&
+ git checkout -b branch-b &&
+ git submodule add ../sub-upstream sub2 &&
+ git -C sub2 submodule update --init &&
+ # branch-b now has a committed submodule not in branch-a
+ git commit -m "add second submodule" &&
+ git checkout branch-a &&
+ git branch --recurse-submodules branch-c branch-b &&
+ git checkout --recurse-submodules branch-c &&
+ git -C sub2 rev-parse branch-c &&
+ git -C sub2/sub-sub rev-parse branch-c
+ )
+'
+
+test_expect_success 'should not create branches in inactive submodules' '
+ test_when_finished "reset_test" &&
+ test_config -C super submodule.sub.active false &&
+ (
+ cd super &&
+ git branch --recurse-submodules branch-a &&
+ git rev-parse branch-a &&
+ test_no_branch sub branch-a
+ )
+'
+
+test_expect_success 'should set up tracking of local branches with track=always' '
+ test_when_finished "reset_test" &&
+ (
+ cd super &&
+ git -c branch.autoSetupMerge=always branch --recurse-submodules branch-a main &&
+ git -C sub rev-parse main &&
+ test_cmp_config -C sub . branch.branch-a.remote &&
+ test_cmp_config -C sub refs/heads/main branch.branch-a.merge
+ )
+'
+
+test_expect_success 'should set up tracking of local branches with explicit track' '
+ test_when_finished "reset_test" &&
+ (
+ cd super &&
+ git branch --track --recurse-submodules branch-a main &&
+ git -C sub rev-parse main &&
+ test_cmp_config -C sub . branch.branch-a.remote &&
+ test_cmp_config -C sub refs/heads/main branch.branch-a.merge
+ )
+'
+
+test_expect_success 'should not set up unnecessary tracking of local branches' '
+ test_when_finished "reset_test" &&
+ (
+ cd super &&
+ git branch --recurse-submodules branch-a main &&
+ git -C sub rev-parse main &&
+ test_cmp_config -C sub "" --default "" branch.branch-a.remote &&
+ test_cmp_config -C sub "" --default "" branch.branch-a.merge
+ )
+'
+
+reset_remote_test () {
+ rm -fr super-clone &&
+ reset_test
+}
+
+test_expect_success 'setup tests with remotes' '
+ (
+ cd test_dirs &&
+ (
+ cd super &&
+ git branch branch-a &&
+ git checkout -b branch-b &&
+ git submodule add ../sub-upstream sub2 &&
+ # branch-b now has a committed submodule not in branch-a
+ git commit -m "add second submodule"
+ ) &&
+ git clone --branch main --recurse-submodules super super-clone &&
+ git -C super-clone config submodule.propagateBranches true
+ ) &&
+ reset_remote_test
+'
+
+test_expect_success 'should get fatal error upon branch creation when submodule is not in .git/modules' '
+ test_when_finished "reset_remote_test" &&
+ (
+ cd super-clone &&
+ # This should succeed because super-clone has sub in .git/modules
+ git branch --recurse-submodules branch-a origin/branch-a &&
+ # This should fail because super-clone does not have sub2 .git/modules
+ test_must_fail git branch --recurse-submodules branch-b origin/branch-b 2>actual &&
+ grep "fatal: submodule .sub2.: unable to find submodule" actual &&
+ test_no_branch . branch-b &&
+ test_no_branch sub branch-b &&
+ # User can fix themselves by initializing the submodule
+ git checkout origin/branch-b &&
+ git submodule update --init --recursive &&
+ git branch --recurse-submodules branch-b origin/branch-b
+ )
+'
+
+test_expect_success 'should set up tracking of remote-tracking branches by default' '
+ test_when_finished "reset_remote_test" &&
+ (
+ cd super-clone &&
+ git branch --recurse-submodules branch-a origin/branch-a &&
+ test_cmp_config origin branch.branch-a.remote &&
+ test_cmp_config refs/heads/branch-a branch.branch-a.merge &&
+ # "origin/branch-a" does not exist for "sub", but it matches the refspec
+ # so tracking should be set up
+ test_cmp_config -C sub origin branch.branch-a.remote &&
+ test_cmp_config -C sub refs/heads/branch-a branch.branch-a.merge &&
+ test_cmp_config -C sub/sub-sub origin branch.branch-a.remote &&
+ test_cmp_config -C sub/sub-sub refs/heads/branch-a branch.branch-a.merge
+ )
+'
+
+test_expect_success 'should not fail when unable to set up tracking in submodule' '
+ test_when_finished "reset_remote_test" &&
+ (
+ cd super-clone &&
+ git remote rename origin ex-origin &&
+ git branch --recurse-submodules branch-a ex-origin/branch-a &&
+ test_cmp_config ex-origin branch.branch-a.remote &&
+ test_cmp_config refs/heads/branch-a branch.branch-a.merge &&
+ test_cmp_config -C sub "" --default "" branch.branch-a.remote &&
+ test_cmp_config -C sub "" --default "" branch.branch-a.merge
+ )
+'
+
+test_expect_success '--track=inherit should set up tracking correctly' '
+ test_when_finished "reset_remote_test" &&
+ (
+ cd super-clone &&
+ git branch --recurse-submodules branch-a origin/branch-a &&
+ # Set this manually instead of using branch --set-upstream-to
+ # to circumvent the "nonexistent upstream" check.
+ git -C sub config branch.branch-a.remote origin &&
+ git -C sub config branch.branch-a.merge refs/heads/sub-branch-a &&
+ git -C sub/sub-sub config branch.branch-a.remote other &&
+ git -C sub/sub-sub config branch.branch-a.merge refs/heads/sub-sub-branch-a &&
+
+ git branch --recurse-submodules --track=inherit branch-b branch-a &&
+ test_cmp_config origin branch.branch-b.remote &&
+ test_cmp_config refs/heads/branch-a branch.branch-b.merge &&
+ test_cmp_config -C sub origin branch.branch-b.remote &&
+ test_cmp_config -C sub refs/heads/sub-branch-a branch.branch-b.merge &&
+ test_cmp_config -C sub/sub-sub other branch.branch-b.remote &&
+ test_cmp_config -C sub/sub-sub refs/heads/sub-sub-branch-a branch.branch-b.merge
+ )
+'
+
+test_expect_success '--no-track should not set up tracking' '
+ test_when_finished "reset_remote_test" &&
+ (
+ cd super-clone &&
+ git branch --recurse-submodules --no-track branch-a origin/branch-a &&
+ test_cmp_config "" --default "" branch.branch-a.remote &&
+ test_cmp_config "" --default "" branch.branch-a.merge &&
+ test_cmp_config -C sub "" --default "" branch.branch-a.remote &&
+ test_cmp_config -C sub "" --default "" branch.branch-a.merge &&
+ test_cmp_config -C sub/sub-sub "" --default "" branch.branch-a.remote &&
+ test_cmp_config -C sub/sub-sub "" --default "" branch.branch-a.merge
+ )
+'
+
+test_done
diff --git a/t/t3210-pack-refs.sh b/t/t3210-pack-refs.sh
new file mode 100755
index 0000000..7f4e98d
--- /dev/null
+++ b/t/t3210-pack-refs.sh
@@ -0,0 +1,297 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Amos Waterland
+# Copyright (c) 2006 Christian Couder
+#
+
+test_description='git pack-refs should not change the branch semantic
+
+This test runs git pack-refs and git show-ref and checks that the branch
+semantic is still the same.
+'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'enable reflogs' '
+ git config core.logallrefupdates true
+'
+
+test_expect_success 'prepare a trivial repository' '
+ echo Hello > A &&
+ git update-index --add A &&
+ git commit -m "Initial commit." &&
+ HEAD=$(git rev-parse --verify HEAD)
+'
+
+SHA1=
+
+test_expect_success 'see if git show-ref works as expected' '
+ git branch a &&
+ SHA1=$(cat .git/refs/heads/a) &&
+ echo "$SHA1 refs/heads/a" >expect &&
+ git show-ref a >result &&
+ test_cmp expect result
+'
+
+test_expect_success 'see if a branch still exists when packed' '
+ git branch b &&
+ git pack-refs --all &&
+ rm -f .git/refs/heads/b &&
+ echo "$SHA1 refs/heads/b" >expect &&
+ git show-ref b >result &&
+ test_cmp expect result
+'
+
+test_expect_success 'git branch c/d should barf if branch c exists' '
+ git branch c &&
+ git pack-refs --all &&
+ rm -f .git/refs/heads/c &&
+ test_must_fail git branch c/d
+'
+
+test_expect_success 'see if a branch still exists after git pack-refs --prune' '
+ git branch e &&
+ git pack-refs --all --prune &&
+ echo "$SHA1 refs/heads/e" >expect &&
+ git show-ref e >result &&
+ test_cmp expect result
+'
+
+test_expect_success 'see if git pack-refs --prune remove ref files' '
+ git branch f &&
+ git pack-refs --all --prune &&
+ ! test -f .git/refs/heads/f
+'
+
+test_expect_success 'see if git pack-refs --prune removes empty dirs' '
+ git branch r/s/t &&
+ git pack-refs --all --prune &&
+ ! test -e .git/refs/heads/r
+'
+
+test_expect_success 'git branch g should work when git branch g/h has been deleted' '
+ git branch g/h &&
+ git pack-refs --all --prune &&
+ git branch -d g/h &&
+ git branch g &&
+ git pack-refs --all &&
+ git branch -d g
+'
+
+test_expect_success 'git branch i/j/k should barf if branch i exists' '
+ git branch i &&
+ git pack-refs --all --prune &&
+ test_must_fail git branch i/j/k
+'
+
+test_expect_success 'test git branch k after branch k/l/m and k/lm have been deleted' '
+ git branch k/l &&
+ git branch k/lm &&
+ git branch -d k/l &&
+ git branch k/l/m &&
+ git branch -d k/l/m &&
+ git branch -d k/lm &&
+ git branch k
+'
+
+test_expect_success 'test git branch n after some branch deletion and pruning' '
+ git branch n/o &&
+ git branch n/op &&
+ git branch -d n/o &&
+ git branch n/o/p &&
+ git branch -d n/op &&
+ git pack-refs --all --prune &&
+ git branch -d n/o/p &&
+ git branch n
+'
+
+test_expect_success 'test excluded refs are not packed' '
+ git branch dont_pack1 &&
+ git branch dont_pack2 &&
+ git branch pack_this &&
+ git pack-refs --all --exclude "refs/heads/dont_pack*" &&
+ test -f .git/refs/heads/dont_pack1 &&
+ test -f .git/refs/heads/dont_pack2 &&
+ ! test -f .git/refs/heads/pack_this'
+
+test_expect_success 'test --no-exclude refs clears excluded refs' '
+ git branch dont_pack3 &&
+ git branch dont_pack4 &&
+ git pack-refs --all --exclude "refs/heads/dont_pack*" --no-exclude &&
+ ! test -f .git/refs/heads/dont_pack3 &&
+ ! test -f .git/refs/heads/dont_pack4'
+
+test_expect_success 'test only included refs are packed' '
+ git branch pack_this1 &&
+ git branch pack_this2 &&
+ git tag dont_pack5 &&
+ git pack-refs --include "refs/heads/pack_this*" &&
+ test -f .git/refs/tags/dont_pack5 &&
+ ! test -f .git/refs/heads/pack_this1 &&
+ ! test -f .git/refs/heads/pack_this2'
+
+test_expect_success 'test --no-include refs clears included refs' '
+ git branch pack1 &&
+ git branch pack2 &&
+ git pack-refs --include "refs/heads/pack*" --no-include &&
+ test -f .git/refs/heads/pack1 &&
+ test -f .git/refs/heads/pack2'
+
+test_expect_success 'test --exclude takes precedence over --include' '
+ git branch dont_pack5 &&
+ git pack-refs --include "refs/heads/pack*" --exclude "refs/heads/pack*" &&
+ test -f .git/refs/heads/dont_pack5'
+
+test_expect_success 'see if up-to-date packed refs are preserved' '
+ git branch q &&
+ git pack-refs --all --prune &&
+ git update-ref refs/heads/q refs/heads/q &&
+ ! test -f .git/refs/heads/q
+'
+
+test_expect_success 'pack, prune and repack' '
+ git tag foo &&
+ git pack-refs --all --prune &&
+ git show-ref >all-of-them &&
+ git pack-refs &&
+ git show-ref >again &&
+ test_cmp all-of-them again
+'
+
+test_expect_success 'explicit pack-refs with dangling packed reference' '
+ git commit --allow-empty -m "soon to be garbage-collected" &&
+ git pack-refs --all &&
+ git reset --hard HEAD^ &&
+ git reflog expire --expire=all --all &&
+ git prune --expire=all &&
+ git pack-refs --all 2>result &&
+ test_must_be_empty result
+'
+
+test_expect_success 'delete ref with dangling packed version' '
+ git checkout -b lamb &&
+ git commit --allow-empty -m "future garbage" &&
+ git pack-refs --all &&
+ git reset --hard HEAD^ &&
+ git checkout main &&
+ git reflog expire --expire=all --all &&
+ git prune --expire=all &&
+ git branch -d lamb 2>result &&
+ test_must_be_empty result
+'
+
+test_expect_success 'delete ref while another dangling packed ref' '
+ git branch lamb &&
+ git commit --allow-empty -m "future garbage" &&
+ git pack-refs --all &&
+ git reset --hard HEAD^ &&
+ git reflog expire --expire=all --all &&
+ git prune --expire=all &&
+ git branch -d lamb 2>result &&
+ test_must_be_empty result
+'
+
+test_expect_success 'pack ref directly below refs/' '
+ git update-ref refs/top HEAD &&
+ git pack-refs --all --prune &&
+ grep refs/top .git/packed-refs &&
+ test_path_is_missing .git/refs/top
+'
+
+test_expect_success 'do not pack ref in refs/bisect' '
+ git update-ref refs/bisect/local HEAD &&
+ git pack-refs --all --prune &&
+ ! grep refs/bisect/local .git/packed-refs >/dev/null &&
+ test_path_is_file .git/refs/bisect/local
+'
+
+test_expect_success 'disable reflogs' '
+ git config core.logallrefupdates false &&
+ rm -rf .git/logs
+'
+
+test_expect_success 'create packed foo/bar/baz branch' '
+ git branch foo/bar/baz &&
+ git pack-refs --all --prune &&
+ test_path_is_missing .git/refs/heads/foo/bar/baz &&
+ test_must_fail git reflog exists refs/heads/foo/bar/baz
+'
+
+test_expect_success 'notice d/f conflict with existing directory' '
+ test_must_fail git branch foo &&
+ test_must_fail git branch foo/bar
+'
+
+test_expect_success 'existing directory reports concrete ref' '
+ test_must_fail git branch foo 2>stderr &&
+ test_grep refs/heads/foo/bar/baz stderr
+'
+
+test_expect_success 'notice d/f conflict with existing ref' '
+ test_must_fail git branch foo/bar/baz/extra &&
+ test_must_fail git branch foo/bar/baz/lots/of/extra/components
+'
+
+test_expect_success 'reject packed-refs with unterminated line' '
+ cp .git/packed-refs .git/packed-refs.bak &&
+ test_when_finished "mv .git/packed-refs.bak .git/packed-refs" &&
+ printf "%s" "$HEAD refs/zzzzz" >>.git/packed-refs &&
+ echo "fatal: unterminated line in .git/packed-refs: $HEAD refs/zzzzz" >expected_err &&
+ test_must_fail git for-each-ref >out 2>err &&
+ test_cmp expected_err err
+'
+
+test_expect_success 'reject packed-refs containing junk' '
+ cp .git/packed-refs .git/packed-refs.bak &&
+ test_when_finished "mv .git/packed-refs.bak .git/packed-refs" &&
+ printf "%s\n" "bogus content" >>.git/packed-refs &&
+ echo "fatal: unexpected line in .git/packed-refs: bogus content" >expected_err &&
+ test_must_fail git for-each-ref >out 2>err &&
+ test_cmp expected_err err
+'
+
+test_expect_success 'reject packed-refs with a short SHA-1' '
+ cp .git/packed-refs .git/packed-refs.bak &&
+ test_when_finished "mv .git/packed-refs.bak .git/packed-refs" &&
+ printf "%.7s %s\n" $HEAD refs/zzzzz >>.git/packed-refs &&
+ printf "fatal: unexpected line in .git/packed-refs: %.7s %s\n" $HEAD refs/zzzzz >expected_err &&
+ test_must_fail git for-each-ref >out 2>err &&
+ test_cmp expected_err err
+'
+
+test_expect_success 'timeout if packed-refs.lock exists' '
+ LOCK=.git/packed-refs.lock &&
+ >"$LOCK" &&
+ test_when_finished "rm -f $LOCK" &&
+ test_must_fail git pack-refs --all --prune
+'
+
+test_expect_success 'retry acquiring packed-refs.lock' '
+ LOCK=.git/packed-refs.lock &&
+ >"$LOCK" &&
+ test_when_finished "wait && rm -f $LOCK" &&
+ {
+ ( sleep 1 && rm -f $LOCK ) &
+ } &&
+ git -c core.packedrefstimeout=3000 pack-refs --all --prune
+'
+
+test_expect_success SYMLINKS 'pack symlinked packed-refs' '
+ # First make sure that symlinking works when reading:
+ git update-ref refs/heads/lossy refs/heads/main &&
+ git for-each-ref >all-refs-before &&
+ mv .git/packed-refs .git/my-deviant-packed-refs &&
+ ln -s my-deviant-packed-refs .git/packed-refs &&
+ git for-each-ref >all-refs-linked &&
+ test_cmp all-refs-before all-refs-linked &&
+ git pack-refs --all --prune &&
+ git for-each-ref >all-refs-packed &&
+ test_cmp all-refs-before all-refs-packed &&
+ test -h .git/packed-refs &&
+ test "$(test_readlink .git/packed-refs)" = "my-deviant-packed-refs"
+'
+
+test_done
diff --git a/t/t3211-peel-ref.sh b/t/t3211-peel-ref.sh
new file mode 100755
index 0000000..9cbc34f
--- /dev/null
+++ b/t/t3211-peel-ref.sh
@@ -0,0 +1,77 @@
+#!/bin/sh
+
+test_description='tests for the peel_ref optimization of packed-refs'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'create annotated tag in refs/tags' '
+ test_commit base &&
+ git tag -m annotated foo
+'
+
+test_expect_success 'create annotated tag outside of refs/tags' '
+ git update-ref refs/outside/foo refs/tags/foo
+'
+
+# This matches show-ref's output
+print_ref() {
+ echo "$(git rev-parse "$1") $1"
+}
+
+test_expect_success 'set up expected show-ref output' '
+ {
+ print_ref "refs/heads/main" &&
+ print_ref "refs/outside/foo" &&
+ print_ref "refs/outside/foo^{}" &&
+ print_ref "refs/tags/base" &&
+ print_ref "refs/tags/foo" &&
+ print_ref "refs/tags/foo^{}"
+ } >expect
+'
+
+test_expect_success 'refs are peeled outside of refs/tags (loose)' '
+ git show-ref -d >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'refs are peeled outside of refs/tags (packed)' '
+ git pack-refs --all &&
+ git show-ref -d >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'create old-style pack-refs without fully-peeled' '
+ # Git no longer writes without fully-peeled, so we just write our own
+ # from scratch; we could also munge the existing file to remove the
+ # fully-peeled bits, but that seems even more prone to failure,
+ # especially if the format ever changes again. At least this way we
+ # know we are emulating exactly what an older git would have written.
+ {
+ echo "# pack-refs with: peeled " &&
+ print_ref "refs/heads/main" &&
+ print_ref "refs/outside/foo" &&
+ print_ref "refs/tags/base" &&
+ print_ref "refs/tags/foo" &&
+ echo "^$(git rev-parse "refs/tags/foo^{}")"
+ } >tmp &&
+ mv tmp .git/packed-refs
+'
+
+test_expect_success 'refs are peeled outside of refs/tags (old packed)' '
+ git show-ref -d >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'peeled refs survive deletion of packed ref' '
+ git pack-refs --all &&
+ cp .git/packed-refs fully-peeled &&
+ git branch yadda &&
+ git pack-refs --all &&
+ git branch -d yadda &&
+ test_cmp fully-peeled .git/packed-refs
+'
+
+test_done
diff --git a/t/t3300-funny-names.sh b/t/t3300-funny-names.sh
new file mode 100755
index 0000000..d3ac826
--- /dev/null
+++ b/t/t3300-funny-names.sh
@@ -0,0 +1,218 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+test_description='Pathnames with funny characters.
+
+This test tries pathnames with funny characters in the working
+tree, index, and tree objects.
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+HT=' '
+
+test_have_prereq MINGW ||
+echo 2>/dev/null > "Name with an${HT}HT"
+if ! test -f "Name with an${HT}HT"
+then
+ # since FAT/NTFS does not allow tabs in filenames, skip this test
+ skip_all='Your filesystem does not allow tabs in filenames'
+ test_done
+fi
+
+p0='no-funny'
+p1='tabs ," (dq) and spaces'
+p2='just space'
+
+test_expect_success 'setup' '
+ cat >"$p0" <<-\EOF &&
+ 1. A quick brown fox jumps over the lazy cat, oops dog.
+ 2. A quick brown fox jumps over the lazy cat, oops dog.
+ 3. A quick brown fox jumps over the lazy cat, oops dog.
+ EOF
+
+ { cat "$p0" >"$p1" || :; } &&
+ { echo "Foo Bar Baz" >"$p2" || :; }
+'
+
+test_expect_success 'setup: populate index and tree' '
+ git update-index --add "$p0" "$p2" &&
+ t0=$(git write-tree)
+'
+
+test_expect_success 'ls-files prints space in filename verbatim' '
+ printf "%s\n" "just space" no-funny >expected &&
+ git ls-files >current &&
+ test_cmp expected current
+'
+
+test_expect_success 'setup: add funny filename' '
+ git update-index --add "$p1" &&
+ t1=$(git write-tree)
+'
+
+test_expect_success 'ls-files quotes funny filename' '
+ cat >expected <<-\EOF &&
+ just space
+ no-funny
+ "tabs\t,\" (dq) and spaces"
+ EOF
+ git ls-files >current &&
+ test_cmp expected current
+'
+
+test_expect_success 'ls-files -z does not quote funny filename' '
+ cat >expected <<-\EOF &&
+ just space
+ no-funny
+ tabs ," (dq) and spaces
+ EOF
+ git ls-files -z >ls-files.z &&
+ perl -pe "y/\000/\012/" <ls-files.z >current &&
+ test_cmp expected current
+'
+
+test_expect_success 'ls-tree quotes funny filename' '
+ cat >expected <<-\EOF &&
+ just space
+ no-funny
+ "tabs\t,\" (dq) and spaces"
+ EOF
+ git ls-tree -r $t1 >ls-tree &&
+ sed -e "s/^[^ ]* //" <ls-tree >current &&
+ test_cmp expected current
+'
+
+test_expect_success 'diff-index --name-status quotes funny filename' '
+ cat >expected <<-\EOF &&
+ A "tabs\t,\" (dq) and spaces"
+ EOF
+ git diff-index --name-status $t0 >current &&
+ test_cmp expected current
+'
+
+test_expect_success 'diff-tree --name-status quotes funny filename' '
+ cat >expected <<-\EOF &&
+ A "tabs\t,\" (dq) and spaces"
+ EOF
+ git diff-tree --name-status $t0 $t1 >current &&
+ test_cmp expected current
+'
+
+test_expect_success 'diff-index -z does not quote funny filename' '
+ cat >expected <<-\EOF &&
+ A
+ tabs ," (dq) and spaces
+ EOF
+ git diff-index -z --name-status $t0 >diff-index.z &&
+ perl -pe "y/\000/\012/" <diff-index.z >current &&
+ test_cmp expected current
+'
+
+test_expect_success 'diff-tree -z does not quote funny filename' '
+ cat >expected <<-\EOF &&
+ A
+ tabs ," (dq) and spaces
+ EOF
+ git diff-tree -z --name-status $t0 $t1 >diff-tree.z &&
+ perl -pe y/\\000/\\012/ <diff-tree.z >current &&
+ test_cmp expected current
+'
+
+test_expect_success 'diff-tree --find-copies-harder quotes funny filename' '
+ cat >expected <<-\EOF &&
+ CNUM no-funny "tabs\t,\" (dq) and spaces"
+ EOF
+ git diff-tree -C --find-copies-harder --name-status $t0 $t1 >out &&
+ sed -e "s/^C[0-9]*/CNUM/" <out >current &&
+ test_cmp expected current
+'
+
+test_expect_success 'setup: remove unfunny index entry' '
+ git update-index --force-remove "$p0"
+'
+
+test_expect_success 'diff-tree -M quotes funny filename' '
+ cat >expected <<-\EOF &&
+ RNUM no-funny "tabs\t,\" (dq) and spaces"
+ EOF
+ git diff-index -M --name-status $t0 >out &&
+ sed -e "s/^R[0-9]*/RNUM/" <out >current &&
+ test_cmp expected current
+'
+
+test_expect_success 'diff-index -M -p quotes funny filename' '
+ cat >expected <<-\EOF &&
+ diff --git a/no-funny "b/tabs\t,\" (dq) and spaces"
+ similarity index NUM%
+ rename from no-funny
+ rename to "tabs\t,\" (dq) and spaces"
+ EOF
+ git diff-index -M -p $t0 >diff &&
+ sed -e "s/index [0-9]*%/index NUM%/" <diff >current &&
+ test_cmp expected current
+'
+
+test_expect_success 'setup: mode change' '
+ chmod +x "$p1"
+'
+
+test_expect_success 'diff-index -M -p with mode change quotes funny filename' '
+ cat >expected <<-\EOF &&
+ diff --git a/no-funny "b/tabs\t,\" (dq) and spaces"
+ old mode 100644
+ new mode 100755
+ similarity index NUM%
+ rename from no-funny
+ rename to "tabs\t,\" (dq) and spaces"
+ EOF
+ git diff-index -M -p $t0 >diff &&
+ sed -e "s/index [0-9]*%/index NUM%/" <diff >current &&
+ test_cmp expected current
+'
+
+test_expect_success 'diffstat for rename quotes funny filename' '
+ cat >expected <<-\EOF &&
+ "tabs\t,\" (dq) and spaces"
+ 1 file changed, 0 insertions(+), 0 deletions(-)
+ EOF
+ git diff-index -M -p $t0 >diff &&
+ git apply --stat <diff >diffstat &&
+ sed -e "s/|.*//" -e "s/ *\$//" <diffstat >current &&
+ test_cmp expected current
+'
+
+test_expect_success 'numstat for rename quotes funny filename' '
+ cat >expected <<-\EOF &&
+ 0 0 "tabs\t,\" (dq) and spaces"
+ EOF
+ git diff-index -M -p $t0 >diff &&
+ git apply --numstat <diff >current &&
+ test_cmp expected current
+'
+
+test_expect_success 'numstat without -M quotes funny filename' '
+ cat >expected <<-\EOF &&
+ 0 3 no-funny
+ 3 0 "tabs\t,\" (dq) and spaces"
+ EOF
+ git diff-index -p $t0 >diff &&
+ git apply --numstat <diff >current &&
+ test_cmp expected current
+'
+
+test_expect_success 'numstat for non-git rename diff quotes funny filename' '
+ cat >expected <<-\EOF &&
+ 0 3 no-funny
+ 3 0 "tabs\t,\" (dq) and spaces"
+ EOF
+ git diff-index -p $t0 >git-diff &&
+ sed -ne "/^[-+@]/p" <git-diff >diff &&
+ git apply --numstat <diff >current &&
+ test_cmp expected current
+'
+
+test_done
diff --git a/t/t3301-notes.sh b/t/t3301-notes.sh
new file mode 100755
index 0000000..cf23c06
--- /dev/null
+++ b/t/t3301-notes.sh
@@ -0,0 +1,1559 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Johannes E. Schindelin
+#
+
+test_description='Test commit notes'
+
+. ./test-lib.sh
+
+write_script fake_editor <<\EOF
+echo "$MSG" >"$1"
+echo "$MSG" >&2
+EOF
+GIT_EDITOR=./fake_editor
+export GIT_EDITOR
+
+indent=" "
+
+test_expect_success 'cannot annotate non-existing HEAD' '
+ test_must_fail env MSG=3 git notes add
+'
+
+test_expect_success 'setup' '
+ test_commit 1st &&
+ test_commit 2nd
+'
+
+test_expect_success 'need valid notes ref' '
+ test_must_fail env MSG=1 GIT_NOTES_REF=/ git notes show &&
+ test_must_fail env MSG=2 GIT_NOTES_REF=/ git notes show
+'
+
+test_expect_success 'refusing to add notes in refs/heads/' '
+ test_must_fail env MSG=1 GIT_NOTES_REF=refs/heads/bogus git notes add
+'
+
+test_expect_success 'refusing to edit notes in refs/remotes/' '
+ test_must_fail env MSG=1 GIT_NOTES_REF=refs/heads/bogus git notes edit
+'
+
+# 1 indicates caught gracefully by die, 128 means git-show barked
+test_expect_success 'handle empty notes gracefully' '
+ test_expect_code 1 git notes show
+'
+
+test_expect_success 'show non-existent notes entry with %N' '
+ test_write_lines A B >expect &&
+ git show -s --format="A%n%NB" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'create notes' '
+ MSG=b4 git notes add &&
+ test_path_is_missing .git/NOTES_EDITMSG &&
+ git ls-tree -r refs/notes/commits >actual &&
+ test_line_count = 1 actual &&
+ echo b4 >expect &&
+ git notes show >actual &&
+ test_cmp expect actual &&
+ git show HEAD^ &&
+ test_must_fail git notes show HEAD^
+'
+
+test_expect_success 'show notes entry with %N' '
+ test_write_lines A b4 B >expect &&
+ git show -s --format="A%n%NB" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'create reflog entry' '
+ ref=$(git rev-parse --short refs/notes/commits) &&
+ cat <<-EOF >expect &&
+ $ref refs/notes/commits@{0}: notes: Notes added by '\''git notes add'\''
+ EOF
+ git reflog show refs/notes/commits >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'edit existing notes' '
+ MSG=b3 git notes edit &&
+ test_path_is_missing .git/NOTES_EDITMSG &&
+ git ls-tree -r refs/notes/commits >actual &&
+ test_line_count = 1 actual &&
+ echo b3 >expect &&
+ git notes show >actual &&
+ test_cmp expect actual &&
+ git show HEAD^ &&
+ test_must_fail git notes show HEAD^
+'
+
+test_expect_success 'show notes from treeish' '
+ echo b3 >expect &&
+ git notes --ref commits^{tree} show >actual &&
+ test_cmp expect actual &&
+
+ echo b4 >expect &&
+ git notes --ref commits@{1} show >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'cannot edit notes from non-ref' '
+ test_must_fail git notes --ref commits^{tree} edit &&
+ test_must_fail git notes --ref commits@{1} edit
+'
+
+test_expect_success 'cannot "git notes add -m" where notes already exists' '
+ test_must_fail git notes add -m "b2" &&
+ test_path_is_missing .git/NOTES_EDITMSG &&
+ git ls-tree -r refs/notes/commits >actual &&
+ test_line_count = 1 actual &&
+ echo b3 >expect &&
+ git notes show >actual &&
+ test_cmp expect actual &&
+ git show HEAD^ &&
+ test_must_fail git notes show HEAD^
+'
+
+test_expect_success 'can overwrite existing note with "git notes add -f -m"' '
+ git notes add -f -m "b1" &&
+ test_path_is_missing .git/NOTES_EDITMSG &&
+ git ls-tree -r refs/notes/commits >actual &&
+ test_line_count = 1 actual &&
+ echo b1 >expect &&
+ git notes show >actual &&
+ test_cmp expect actual &&
+ git show HEAD^ &&
+ test_must_fail git notes show HEAD^
+'
+
+test_expect_success 'add w/no options on existing note morphs into edit' '
+ MSG=b2 git notes add &&
+ test_path_is_missing .git/NOTES_EDITMSG &&
+ git ls-tree -r refs/notes/commits >actual &&
+ test_line_count = 1 actual &&
+ echo b2 >expect &&
+ git notes show >actual &&
+ test_cmp expect actual &&
+ git show HEAD^ &&
+ test_must_fail git notes show HEAD^
+'
+
+test_expect_success 'can overwrite existing note with "git notes add -f"' '
+ MSG=b1 git notes add -f &&
+ test_path_is_missing .git/NOTES_EDITMSG &&
+ git ls-tree -r refs/notes/commits >actual &&
+ test_line_count = 1 actual &&
+ echo b1 >expect &&
+ git notes show >actual &&
+ test_cmp expect actual &&
+ git show HEAD^ &&
+ test_must_fail git notes show HEAD^
+'
+
+test_expect_success 'show notes' '
+ commit=$(git rev-parse HEAD) &&
+ cat >expect <<-EOF &&
+ commit $commit
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:14:13 2005 -0700
+
+ ${indent}2nd
+
+ Notes:
+ ${indent}b1
+ EOF
+ git cat-file commit HEAD >commits &&
+ ! grep b1 commits &&
+ git log -1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'show multi-line notes' '
+ test_commit 3rd &&
+ MSG="b3${LF}c3c3c3c3${LF}d3d3d3" git notes add &&
+ commit=$(git rev-parse HEAD) &&
+ cat >expect-multiline <<-EOF &&
+ commit $commit
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:15:13 2005 -0700
+
+ ${indent}3rd
+
+ Notes:
+ ${indent}b3
+ ${indent}c3c3c3c3
+ ${indent}d3d3d3
+
+ EOF
+ cat expect >>expect-multiline &&
+ git log -2 >actual &&
+ test_cmp expect-multiline actual
+'
+
+test_expect_success 'show -F notes' '
+ test_commit 4th &&
+ echo "xyzzy" >note5 &&
+ git notes add -F note5 &&
+ commit=$(git rev-parse HEAD) &&
+ cat >expect-F <<-EOF &&
+ commit $commit
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:16:13 2005 -0700
+
+ ${indent}4th
+
+ Notes:
+ ${indent}xyzzy
+
+ EOF
+ cat expect-multiline >>expect-F &&
+ git log -3 >actual &&
+ test_cmp expect-F actual
+'
+
+test_expect_success 'Re-adding -F notes without -f fails' '
+ echo "zyxxy" >note5 &&
+ test_must_fail git notes add -F note5 &&
+ git log -3 >actual &&
+ test_cmp expect-F actual
+'
+
+test_expect_success 'git log --pretty=raw does not show notes' '
+ commit=$(git rev-parse HEAD) &&
+ tree=$(git rev-parse HEAD^{tree}) &&
+ parent=$(git rev-parse HEAD^) &&
+ cat >expect <<-EOF &&
+ commit $commit
+ tree $tree
+ parent $parent
+ author A U Thor <author@example.com> 1112912173 -0700
+ committer C O Mitter <committer@example.com> 1112912173 -0700
+
+ ${indent}4th
+ EOF
+ git log -1 --pretty=raw >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git log --show-notes' '
+ cat >>expect <<-EOF &&
+
+ Notes:
+ ${indent}xyzzy
+ EOF
+ git log -1 --pretty=raw --show-notes >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git log --no-notes' '
+ git log -1 --no-notes >actual &&
+ ! grep xyzzy actual
+'
+
+test_expect_success 'git format-patch does not show notes' '
+ git format-patch -1 --stdout >actual &&
+ ! grep xyzzy actual
+'
+
+test_expect_success 'git format-patch --show-notes does show notes' '
+ git format-patch --show-notes -1 --stdout >actual &&
+ grep xyzzy actual
+'
+
+for pretty in \
+ "" --pretty --pretty=raw --pretty=short --pretty=medium \
+ --pretty=full --pretty=fuller --pretty=format:%s --oneline
+do
+ case "$pretty" in
+ "") p= not= negate="" ;;
+ ?*) p="$pretty" not=" not" negate="!" ;;
+ esac
+ test_expect_success "git show $pretty does$not show notes" '
+ git show $p >actual &&
+ eval "$negate grep xyzzy actual"
+ '
+done
+
+test_expect_success 'setup alternate notes ref' '
+ git notes --ref=alternate add -m alternate
+'
+
+test_expect_success 'git log --notes shows default notes' '
+ git log -1 --notes >actual &&
+ grep xyzzy actual &&
+ ! grep alternate actual
+'
+
+test_expect_success 'git log --notes=X shows only X' '
+ git log -1 --notes=alternate >actual &&
+ ! grep xyzzy actual &&
+ grep alternate actual
+'
+
+test_expect_success 'git log --notes --notes=X shows both' '
+ git log -1 --notes --notes=alternate >actual &&
+ grep xyzzy actual &&
+ grep alternate actual
+'
+
+test_expect_success 'git log --no-notes resets default state' '
+ git log -1 --notes --notes=alternate \
+ --no-notes --notes=alternate \
+ >actual &&
+ ! grep xyzzy actual &&
+ grep alternate actual
+'
+
+test_expect_success 'git log --no-notes resets ref list' '
+ git log -1 --notes --notes=alternate \
+ --no-notes --notes \
+ >actual &&
+ grep xyzzy actual &&
+ ! grep alternate actual
+'
+
+test_expect_success 'show -m notes' '
+ test_commit 5th &&
+ git notes add -m spam -m "foo${LF}bar${LF}baz" &&
+ commit=$(git rev-parse HEAD) &&
+ cat >expect-m <<-EOF &&
+ commit $commit
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:17:13 2005 -0700
+
+ ${indent}5th
+
+ Notes:
+ ${indent}spam
+ ${indent}
+ ${indent}foo
+ ${indent}bar
+ ${indent}baz
+
+ EOF
+ cat expect-F >>expect-m &&
+ git log -4 >actual &&
+ test_cmp expect-m actual
+'
+
+test_expect_success 'remove note with add -f -F /dev/null' '
+ git notes add -f -F /dev/null &&
+ commit=$(git rev-parse HEAD) &&
+ cat >expect-rm-F <<-EOF &&
+ commit $commit
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:17:13 2005 -0700
+
+ ${indent}5th
+
+ EOF
+ cat expect-F >>expect-rm-F &&
+ git log -4 >actual &&
+ test_cmp expect-rm-F actual &&
+ test_must_fail git notes show
+'
+
+test_expect_success 'do not create empty note with -m ""' '
+ git notes add -m "" &&
+ git log -4 >actual &&
+ test_cmp expect-rm-F actual &&
+ test_must_fail git notes show
+'
+
+test_expect_success 'create note with combination of -m and -F' '
+ test_when_finished git notes remove HEAD &&
+ cat >expect-combine_m_and_F <<-EOF &&
+ foo
+
+ xyzzy
+
+ bar
+
+ zyxxy
+
+ baz
+ EOF
+ echo "xyzzy" >note_a &&
+ echo "zyxxy" >note_b &&
+ git notes add -m "foo" -F note_a -m "bar" -F note_b -m "baz" &&
+ git notes show >actual &&
+ test_cmp expect-combine_m_and_F actual
+'
+
+test_expect_success 'create note with combination of -m and -F and --separator' '
+ test_when_finished git notes remove HEAD &&
+ cat >expect-combine_m_and_F <<-\EOF &&
+ foo
+ -------
+ xyzzy
+ -------
+ bar
+ -------
+ zyxxy
+ -------
+ baz
+ EOF
+ echo "xyzzy" >note_a &&
+ echo "zyxxy" >note_b &&
+ git notes add -m "foo" -F note_a -m "bar" -F note_b -m "baz" --separator="-------" &&
+ git notes show >actual &&
+ test_cmp expect-combine_m_and_F actual
+'
+
+test_expect_success 'create note with combination of -m and -F and --no-separator' '
+ cat >expect-combine_m_and_F <<-\EOF &&
+ foo
+ xyzzy
+ bar
+ zyxxy
+ baz
+ EOF
+ echo "xyzzy" >note_a &&
+ echo "zyxxy" >note_b &&
+ git notes add -m "foo" -F note_a -m "bar" -F note_b -m "baz" --no-separator &&
+ git notes show >actual &&
+ test_cmp expect-combine_m_and_F actual
+'
+
+test_expect_success 'remove note with "git notes remove"' '
+ git notes remove HEAD^ &&
+ git notes remove &&
+ commit=$(git rev-parse HEAD) &&
+ parent=$(git rev-parse HEAD^) &&
+ cat >expect-rm-remove <<-EOF &&
+ commit $commit
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:17:13 2005 -0700
+
+ ${indent}5th
+
+ commit $parent
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:16:13 2005 -0700
+
+ ${indent}4th
+
+ EOF
+ cat expect-multiline >>expect-rm-remove &&
+ git log -4 >actual &&
+ test_cmp expect-rm-remove actual &&
+ test_must_fail git notes show HEAD^
+'
+
+test_expect_success 'removing non-existing note should not create new commit' '
+ git rev-parse --verify refs/notes/commits >before_commit &&
+ test_must_fail git notes remove HEAD^ &&
+ git rev-parse --verify refs/notes/commits >after_commit &&
+ test_cmp before_commit after_commit
+'
+
+test_expect_success 'removing more than one' '
+ before=$(git rev-parse --verify refs/notes/commits) &&
+ test_when_finished "git update-ref refs/notes/commits $before" &&
+
+ # We have only two -- add another and make sure it stays
+ git notes add -m "extra" &&
+ git notes list HEAD >after-removal-expect &&
+ git notes remove HEAD^^ HEAD^^^ &&
+ git notes list | sed -e "s/ .*//" >actual &&
+ test_cmp after-removal-expect actual
+'
+
+test_expect_success 'removing is atomic' '
+ before=$(git rev-parse --verify refs/notes/commits) &&
+ test_when_finished "git update-ref refs/notes/commits $before" &&
+ test_must_fail git notes remove HEAD^^ HEAD^^^ HEAD^ &&
+ after=$(git rev-parse --verify refs/notes/commits) &&
+ test "$before" = "$after"
+'
+
+test_expect_success 'removing with --ignore-missing' '
+ before=$(git rev-parse --verify refs/notes/commits) &&
+ test_when_finished "git update-ref refs/notes/commits $before" &&
+
+ # We have only two -- add another and make sure it stays
+ git notes add -m "extra" &&
+ git notes list HEAD >after-removal-expect &&
+ git notes remove --ignore-missing HEAD^^ HEAD^^^ HEAD^ &&
+ git notes list | sed -e "s/ .*//" >actual &&
+ test_cmp after-removal-expect actual
+'
+
+test_expect_success 'removing with --ignore-missing but bogus ref' '
+ before=$(git rev-parse --verify refs/notes/commits) &&
+ test_when_finished "git update-ref refs/notes/commits $before" &&
+ test_must_fail git notes remove --ignore-missing HEAD^^ HEAD^^^ NO-SUCH-COMMIT &&
+ after=$(git rev-parse --verify refs/notes/commits) &&
+ test "$before" = "$after"
+'
+
+test_expect_success 'remove reads from --stdin' '
+ before=$(git rev-parse --verify refs/notes/commits) &&
+ test_when_finished "git update-ref refs/notes/commits $before" &&
+
+ # We have only two -- add another and make sure it stays
+ git notes add -m "extra" &&
+ git notes list HEAD >after-removal-expect &&
+ git rev-parse HEAD^^ HEAD^^^ >input &&
+ git notes remove --stdin <input &&
+ git notes list | sed -e "s/ .*//" >actual &&
+ test_cmp after-removal-expect actual
+'
+
+test_expect_success 'remove --stdin is also atomic' '
+ before=$(git rev-parse --verify refs/notes/commits) &&
+ test_when_finished "git update-ref refs/notes/commits $before" &&
+ git rev-parse HEAD^^ HEAD^^^ HEAD^ >input &&
+ test_must_fail git notes remove --stdin <input &&
+ after=$(git rev-parse --verify refs/notes/commits) &&
+ test "$before" = "$after"
+'
+
+test_expect_success 'removing with --stdin --ignore-missing' '
+ before=$(git rev-parse --verify refs/notes/commits) &&
+ test_when_finished "git update-ref refs/notes/commits $before" &&
+
+ # We have only two -- add another and make sure it stays
+ git notes add -m "extra" &&
+ git notes list HEAD >after-removal-expect &&
+ git rev-parse HEAD^^ HEAD^^^ HEAD^ >input &&
+ git notes remove --ignore-missing --stdin <input &&
+ git notes list | sed -e "s/ .*//" >actual &&
+ test_cmp after-removal-expect actual
+'
+
+test_expect_success 'list notes with "git notes list"' '
+ commit_2=$(git rev-parse 2nd) &&
+ commit_3=$(git rev-parse 3rd) &&
+ note_2=$(git rev-parse refs/notes/commits:$commit_2) &&
+ note_3=$(git rev-parse refs/notes/commits:$commit_3) &&
+ sort -t" " -k2 >expect <<-EOF &&
+ $note_2 $commit_2
+ $note_3 $commit_3
+ EOF
+ git notes list >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'list notes with "git notes"' '
+ git notes >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '"git notes" without subcommand does not take arguments' '
+ test_expect_code 129 git notes HEAD^^ 2>err &&
+ grep "^error: unknown subcommand" err
+'
+
+test_expect_success 'list specific note with "git notes list <object>"' '
+ git rev-parse refs/notes/commits:$commit_3 >expect &&
+ git notes list HEAD^^ >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'listing non-existing notes fails' '
+ test_must_fail git notes list HEAD >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'append: specify a separator with an empty arg' '
+ test_when_finished git notes remove HEAD &&
+ cat >expect <<-\EOF &&
+ notes-1
+
+ notes-2
+ EOF
+
+ git notes add -m "notes-1" &&
+ git notes append --separator="" -m "notes-2" &&
+ git notes show >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'append: specify a separator without arg' '
+ test_when_finished git notes remove HEAD &&
+ cat >expect <<-\EOF &&
+ notes-1
+
+ notes-2
+ EOF
+
+ git notes add -m "notes-1" &&
+ git notes append --separator -m "notes-2" &&
+ git notes show >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'append: specify as --no-separator' '
+ test_when_finished git notes remove HEAD &&
+ cat >expect <<-\EOF &&
+ notes-1
+ notes-2
+ EOF
+
+ git notes add -m "notes-1" &&
+ git notes append --no-separator -m "notes-2" &&
+ git notes show >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'append: specify separator with line break' '
+ test_when_finished git notes remove HEAD &&
+ cat >expect <<-\EOF &&
+ notes-1
+ -------
+ notes-2
+ EOF
+
+ git notes add -m "notes-1" &&
+ git notes append --separator="-------$LF" -m "notes-2" &&
+ git notes show >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'append: specify separator without line break' '
+ test_when_finished git notes remove HEAD &&
+ cat >expect <<-\EOF &&
+ notes-1
+ -------
+ notes-2
+ EOF
+
+ git notes add -m "notes-1" &&
+ git notes append --separator="-------" -m "notes-2" &&
+ git notes show >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'append: specify separator with multiple messages' '
+ test_when_finished git notes remove HEAD &&
+ cat >expect <<-\EOF &&
+ notes-1
+ -------
+ notes-2
+ -------
+ notes-3
+ EOF
+
+ git notes add -m "notes-1" &&
+ git notes append --separator="-------" -m "notes-2" -m "notes-3" &&
+ git notes show >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'append note with combination of -m and -F and --separator' '
+ test_when_finished git notes remove HEAD &&
+ cat >expect-combine_m_and_F <<-\EOF &&
+ m-notes-1
+ -------
+ f-notes-1
+ -------
+ m-notes-2
+ -------
+ f-notes-2
+ -------
+ m-notes-3
+ EOF
+
+ echo "f-notes-1" >note_a &&
+ echo "f-notes-2" >note_b &&
+ git notes append -m "m-notes-1" -F note_a -m "m-notes-2" -F note_b -m "m-notes-3" --separator="-------" &&
+ git notes show >actual &&
+ test_cmp expect-combine_m_and_F actual
+'
+
+test_expect_success 'append to existing note with "git notes append"' '
+ cat >expect <<-EOF &&
+ Initial set of notes
+
+ More notes appended with git notes append
+ EOF
+ git notes add -m "Initial set of notes" &&
+ git notes append -m "More notes appended with git notes append" &&
+ git notes show >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '"git notes list" does not expand to "git notes list HEAD"' '
+ commit_5=$(git rev-parse 5th) &&
+ note_5=$(git rev-parse refs/notes/commits:$commit_5) &&
+ sort -t" " -k2 >expect_list <<-EOF &&
+ $note_2 $commit_2
+ $note_3 $commit_3
+ $note_5 $commit_5
+ EOF
+ git notes list >actual &&
+ test_cmp expect_list actual
+'
+
+test_expect_success 'appending empty string does not change existing note' '
+ git notes append -m "" &&
+ git notes show >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git notes append == add when there is no existing note' '
+ git notes remove HEAD &&
+ test_must_fail git notes list HEAD &&
+ git notes append -m "Initial set of notes${LF}${LF}More notes appended with git notes append" &&
+ git notes show >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'appending empty string to non-existing note does not create note' '
+ git notes remove HEAD &&
+ test_must_fail git notes list HEAD &&
+ git notes append -m "" &&
+ test_must_fail git notes list HEAD
+'
+
+test_expect_success 'create other note on a different notes ref (setup)' '
+ test_commit 6th &&
+ GIT_NOTES_REF="refs/notes/other" git notes add -m "other note" &&
+ commit=$(git rev-parse HEAD) &&
+ cat >expect-not-other <<-EOF &&
+ commit $commit
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:18:13 2005 -0700
+
+ ${indent}6th
+ EOF
+ cp expect-not-other expect-other &&
+ cat >>expect-other <<-EOF
+
+ Notes (other):
+ ${indent}other note
+ EOF
+'
+
+test_expect_success 'Do not show note on other ref by default' '
+ git log -1 >actual &&
+ test_cmp expect-not-other actual
+'
+
+test_expect_success 'Do show note when ref is given in GIT_NOTES_REF' '
+ GIT_NOTES_REF="refs/notes/other" git log -1 >actual &&
+ test_cmp expect-other actual
+'
+
+test_expect_success 'Do show note when ref is given in core.notesRef config' '
+ test_config core.notesRef "refs/notes/other" &&
+ git log -1 >actual &&
+ test_cmp expect-other actual
+'
+
+test_expect_success 'Do not show note when core.notesRef is overridden' '
+ test_config core.notesRef "refs/notes/other" &&
+ GIT_NOTES_REF="refs/notes/wrong" git log -1 >actual &&
+ test_cmp expect-not-other actual
+'
+
+test_expect_success 'Show all notes when notes.displayRef=refs/notes/*' '
+ commit=$(git rev-parse HEAD) &&
+ parent=$(git rev-parse HEAD^) &&
+ cat >expect-both <<-EOF &&
+ commit $commit
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:18:13 2005 -0700
+
+ ${indent}6th
+
+ Notes:
+ ${indent}order test
+
+ Notes (other):
+ ${indent}other note
+
+ commit $parent
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:17:13 2005 -0700
+
+ ${indent}5th
+
+ Notes:
+ ${indent}replacement for deleted note
+ EOF
+ GIT_NOTES_REF=refs/notes/commits git notes add \
+ -m"replacement for deleted note" HEAD^ &&
+ GIT_NOTES_REF=refs/notes/commits git notes add -m"order test" &&
+ test_unconfig core.notesRef &&
+ test_config notes.displayRef "refs/notes/*" &&
+ git log -2 >actual &&
+ test_cmp expect-both actual
+'
+
+test_expect_success 'core.notesRef is implicitly in notes.displayRef' '
+ test_config core.notesRef refs/notes/commits &&
+ test_config notes.displayRef refs/notes/other &&
+ git log -2 >actual &&
+ test_cmp expect-both actual
+'
+
+test_expect_success 'notes.displayRef can be given more than once' '
+ test_unconfig core.notesRef &&
+ test_config notes.displayRef refs/notes/commits &&
+ git config --add notes.displayRef refs/notes/other &&
+ git log -2 >actual &&
+ test_cmp expect-both actual
+'
+
+test_expect_success 'notes.displayRef respects order' '
+ commit=$(git rev-parse HEAD) &&
+ cat >expect-both-reversed <<-EOF &&
+ commit $commit
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:18:13 2005 -0700
+
+ ${indent}6th
+
+ Notes (other):
+ ${indent}other note
+
+ Notes:
+ ${indent}order test
+ EOF
+ test_config core.notesRef refs/notes/other &&
+ test_config notes.displayRef refs/notes/commits &&
+ git log -1 >actual &&
+ test_cmp expect-both-reversed actual
+'
+
+test_expect_success 'notes.displayRef with no value handled gracefully' '
+ test_must_fail git -c notes.displayRef log -0 --notes &&
+ test_must_fail git -c notes.displayRef diff-tree --notes HEAD
+'
+
+test_expect_success 'GIT_NOTES_DISPLAY_REF works' '
+ GIT_NOTES_DISPLAY_REF=refs/notes/commits:refs/notes/other \
+ git log -2 >actual &&
+ test_cmp expect-both actual
+'
+
+test_expect_success 'GIT_NOTES_DISPLAY_REF overrides config' '
+ commit=$(git rev-parse HEAD) &&
+ parent=$(git rev-parse HEAD^) &&
+ cat >expect-none <<-EOF &&
+ commit $commit
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:18:13 2005 -0700
+
+ ${indent}6th
+
+ commit $parent
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:17:13 2005 -0700
+
+ ${indent}5th
+ EOF
+ test_config notes.displayRef "refs/notes/*" &&
+ GIT_NOTES_REF= GIT_NOTES_DISPLAY_REF= git log -2 >actual &&
+ test_cmp expect-none actual
+'
+
+test_expect_success '--show-notes=* adds to GIT_NOTES_DISPLAY_REF' '
+ GIT_NOTES_REF= GIT_NOTES_DISPLAY_REF= git log --show-notes=* -2 >actual &&
+ test_cmp expect-both actual
+'
+
+test_expect_success '--no-standard-notes' '
+ commit=$(git rev-parse HEAD) &&
+ cat >expect-commits <<-EOF &&
+ commit $commit
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:18:13 2005 -0700
+
+ ${indent}6th
+
+ Notes:
+ ${indent}order test
+ EOF
+ git log --no-standard-notes --show-notes=commits -1 >actual &&
+ test_cmp expect-commits actual
+'
+
+test_expect_success '--standard-notes' '
+ test_config notes.displayRef "refs/notes/*" &&
+ git log --no-standard-notes --show-notes=commits \
+ --standard-notes -2 >actual &&
+ test_cmp expect-both actual
+'
+
+test_expect_success '--show-notes=ref accumulates' '
+ git log --show-notes=other --show-notes=commits \
+ --no-standard-notes -1 >actual &&
+ test_cmp expect-both-reversed actual
+'
+
+test_expect_success 'Allow notes on non-commits (trees, blobs, tags)' '
+ test_config core.notesRef refs/notes/other &&
+ echo "Note on a tree" >expect &&
+ git notes add -m "Note on a tree" HEAD: &&
+ git notes show HEAD: >actual &&
+ test_cmp expect actual &&
+ echo "Note on a blob" >expect &&
+ git ls-tree --name-only HEAD >files &&
+ filename=$(head -n1 files) &&
+ git notes add -m "Note on a blob" HEAD:$filename &&
+ git notes show HEAD:$filename >actual &&
+ test_cmp expect actual &&
+ echo "Note on a tag" >expect &&
+ git tag -a -m "This is an annotated tag" foobar HEAD^ &&
+ git notes add -m "Note on a tag" foobar &&
+ git notes show foobar >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'create note from other note with "git notes add -C"' '
+ test_commit 7th &&
+ commit=$(git rev-parse HEAD) &&
+ cat >expect <<-EOF &&
+ commit $commit
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:19:13 2005 -0700
+
+ ${indent}7th
+
+ Notes:
+ ${indent}order test
+ EOF
+ note=$(git notes list HEAD^) &&
+ git notes add -C $note &&
+ git log -1 >actual &&
+ test_cmp expect actual &&
+ git notes list HEAD^ >expect &&
+ git notes list HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'create note from non-existing note with "git notes add -C" fails' '
+ test_commit 8th &&
+ test_must_fail git notes add -C deadbeef &&
+ test_must_fail git notes list HEAD
+'
+
+test_expect_success 'create note from non-blob with "git notes add -C" fails' '
+ commit=$(git rev-parse --verify HEAD) &&
+ tree=$(git rev-parse --verify HEAD:) &&
+ test_must_fail git notes add -C $commit &&
+ test_must_fail git notes add -C $tree &&
+ test_must_fail git notes list HEAD
+'
+
+test_expect_success 'create note from blob with "git notes add -C" reuses blob id' '
+ commit=$(git rev-parse HEAD) &&
+ cat >expect <<-EOF &&
+ commit $commit
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:20:13 2005 -0700
+
+ ${indent}8th
+
+ Notes:
+ ${indent}This is a blob object
+ EOF
+ echo "This is a blob object" | git hash-object -w --stdin >blob &&
+ git notes add -C $(cat blob) &&
+ git log -1 >actual &&
+ test_cmp expect actual &&
+ git notes list HEAD >actual &&
+ test_cmp blob actual
+'
+
+test_expect_success 'create note from blob with "-C", also specify "-m", "-F" and "--separator"' '
+ # 8th will be reuseed in following tests, so rollback when the test is done
+ test_when_finished "git notes remove && git notes add -C $(cat blob)" &&
+ commit=$(git rev-parse HEAD) &&
+ cat >expect <<-EOF &&
+ commit $commit
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:20:13 2005 -0700
+
+ ${indent}8th
+
+ Notes:
+ ${indent}This is a blob object
+ ${indent}-------
+ ${indent}This is created by -m
+ ${indent}-------
+ ${indent}This is created by -F
+ EOF
+
+ git notes remove &&
+ echo "This is a blob object" | git hash-object -w --stdin >blob &&
+ echo "This is created by -F" >note_a &&
+ git notes add -C $(cat blob) -m "This is created by -m" -F note_a --separator="-------" &&
+ git log -1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'create note from other note with "git notes add -c"' '
+ test_commit 9th &&
+ commit=$(git rev-parse HEAD) &&
+ cat >expect <<-EOF &&
+ commit $commit
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:21:13 2005 -0700
+
+ ${indent}9th
+
+ Notes:
+ ${indent}yet another note
+ EOF
+ note=$(git notes list HEAD^^) &&
+ MSG="yet another note" git notes add -c $note &&
+ git log -1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'create note from non-existing note with "git notes add -c" fails' '
+ test_commit 10th &&
+ test_must_fail env MSG="yet another note" git notes add -c deadbeef &&
+ test_must_fail git notes list HEAD
+'
+
+test_expect_success 'append to note from other note with "git notes append -C"' '
+ commit=$(git rev-parse HEAD^) &&
+ cat >expect <<-EOF &&
+ commit $commit
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:21:13 2005 -0700
+
+ ${indent}9th
+
+ Notes:
+ ${indent}yet another note
+ ${indent}
+ ${indent}yet another note
+ EOF
+ note=$(git notes list HEAD^) &&
+ git notes append -C $note HEAD^ &&
+ git log -1 HEAD^ >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'create note from other note with "git notes append -c"' '
+ commit=$(git rev-parse HEAD) &&
+ cat >expect <<-EOF &&
+ commit $commit
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:22:13 2005 -0700
+
+ ${indent}10th
+
+ Notes:
+ ${indent}other note
+ EOF
+ note=$(git notes list HEAD^) &&
+ MSG="other note" git notes append -c $note &&
+ git log -1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'append to note from other note with "git notes append -c"' '
+ commit=$(git rev-parse HEAD) &&
+ cat >expect <<-EOF &&
+ commit $commit
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:22:13 2005 -0700
+
+ ${indent}10th
+
+ Notes:
+ ${indent}other note
+ ${indent}
+ ${indent}yet another note
+ EOF
+ note=$(git notes list HEAD) &&
+ MSG="yet another note" git notes append -c $note &&
+ git log -1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'copy note with "git notes copy"' '
+ commit=$(git rev-parse 4th) &&
+ cat >expect <<-EOF &&
+ commit $commit
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:16:13 2005 -0700
+
+ ${indent}4th
+
+ Notes:
+ ${indent}This is a blob object
+ EOF
+ git notes copy 8th 4th &&
+ git log 3rd..4th >actual &&
+ test_cmp expect actual &&
+ git notes list 4th >expect &&
+ git notes list 8th >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'copy note with "git notes copy" with default' '
+ test_commit 11th &&
+ commit=$(git rev-parse HEAD) &&
+ cat >expect <<-EOF &&
+ commit $commit
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:23:13 2005 -0700
+
+ ${indent}11th
+
+ Notes:
+ ${indent}other note
+ ${indent}
+ ${indent}yet another note
+ EOF
+ git notes copy HEAD^ &&
+ git log -1 >actual &&
+ test_cmp expect actual &&
+ git notes list HEAD^ >expect &&
+ git notes list HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'prevent overwrite with "git notes copy"' '
+ test_must_fail git notes copy HEAD~2 HEAD &&
+ cat >expect <<-EOF &&
+ commit $commit
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:23:13 2005 -0700
+
+ ${indent}11th
+
+ Notes:
+ ${indent}other note
+ ${indent}
+ ${indent}yet another note
+ EOF
+ git log -1 >actual &&
+ test_cmp expect actual &&
+ git notes list HEAD^ >expect &&
+ git notes list HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'allow overwrite with "git notes copy -f"' '
+ commit=$(git rev-parse HEAD) &&
+ cat >expect <<-EOF &&
+ commit $commit
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:23:13 2005 -0700
+
+ ${indent}11th
+
+ Notes:
+ ${indent}This is a blob object
+ EOF
+ git notes copy -f HEAD~3 HEAD &&
+ git log -1 >actual &&
+ test_cmp expect actual &&
+ git notes list HEAD~3 >expect &&
+ git notes list HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'allow overwrite with "git notes copy -f" with default' '
+ commit=$(git rev-parse HEAD) &&
+ cat >expect <<-EOF &&
+ commit $commit
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:23:13 2005 -0700
+
+ ${indent}11th
+
+ Notes:
+ ${indent}yet another note
+ ${indent}
+ ${indent}yet another note
+ EOF
+ git notes copy -f HEAD~2 &&
+ git log -1 >actual &&
+ test_cmp expect actual &&
+ git notes list HEAD~2 >expect &&
+ git notes list HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'cannot copy note from object without notes' '
+ test_commit 12th &&
+ test_commit 13th &&
+ test_must_fail git notes copy HEAD^ HEAD
+'
+
+test_expect_success 'git notes copy --stdin' '
+ commit=$(git rev-parse HEAD) &&
+ parent=$(git rev-parse HEAD^) &&
+ cat >expect <<-EOF &&
+ commit $commit
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:25:13 2005 -0700
+
+ ${indent}13th
+
+ Notes:
+ ${indent}yet another note
+ ${indent}
+ ${indent}yet another note
+
+ commit $parent
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:24:13 2005 -0700
+
+ ${indent}12th
+
+ Notes:
+ ${indent}other note
+ ${indent}
+ ${indent}yet another note
+ EOF
+ from=$(git rev-parse HEAD~3) &&
+ to=$(git rev-parse HEAD^) &&
+ echo "$from" "$to" >copy &&
+ from=$(git rev-parse HEAD~2) &&
+ to=$(git rev-parse HEAD) &&
+ echo "$from" "$to" >>copy &&
+ git notes copy --stdin <copy &&
+ git log -2 >actual &&
+ test_cmp expect actual &&
+ git notes list HEAD~2 >expect &&
+ git notes list HEAD >actual &&
+ test_cmp expect actual &&
+ git notes list HEAD~3 >expect &&
+ git notes list HEAD^ >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git notes copy --for-rewrite (unconfigured)' '
+ test_commit 14th &&
+ test_commit 15th &&
+ commit=$(git rev-parse HEAD) &&
+ parent=$(git rev-parse HEAD^) &&
+ cat >expect <<-EOF &&
+ commit $commit
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:27:13 2005 -0700
+
+ ${indent}15th
+
+ commit $parent
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:26:13 2005 -0700
+
+ ${indent}14th
+ EOF
+ from=$(git rev-parse HEAD~3) &&
+ to=$(git rev-parse HEAD^) &&
+ echo "$from" "$to" >copy &&
+ from=$(git rev-parse HEAD~2) &&
+ to=$(git rev-parse HEAD) &&
+ echo "$from" "$to" >>copy &&
+ git notes copy --for-rewrite=foo <copy &&
+ git log -2 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git notes copy --for-rewrite (enabled)' '
+ commit=$(git rev-parse HEAD) &&
+ parent=$(git rev-parse HEAD^) &&
+ cat >expect <<-EOF &&
+ commit $commit
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:27:13 2005 -0700
+
+ ${indent}15th
+
+ Notes:
+ ${indent}yet another note
+ ${indent}
+ ${indent}yet another note
+
+ commit $parent
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:26:13 2005 -0700
+
+ ${indent}14th
+
+ Notes:
+ ${indent}other note
+ ${indent}
+ ${indent}yet another note
+ EOF
+ test_config notes.rewriteMode overwrite &&
+ test_config notes.rewriteRef "refs/notes/*" &&
+ from=$(git rev-parse HEAD~3) &&
+ to=$(git rev-parse HEAD^) &&
+ echo "$from" "$to" >copy &&
+ from=$(git rev-parse HEAD~2) &&
+ to=$(git rev-parse HEAD) &&
+ echo "$from" "$to" >>copy &&
+ git notes copy --for-rewrite=foo <copy &&
+ git log -2 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git notes copy --for-rewrite (disabled)' '
+ test_config notes.rewrite.bar false &&
+ from=$(git rev-parse HEAD~3) &&
+ to=$(git rev-parse HEAD) &&
+ echo "$from" "$to" >copy &&
+ git notes copy --for-rewrite=bar <copy &&
+ git log -2 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git notes copy --for-rewrite (overwrite)' '
+ commit=$(git rev-parse HEAD) &&
+ cat >expect <<-EOF &&
+ commit $commit
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:27:13 2005 -0700
+
+ ${indent}15th
+
+ Notes:
+ ${indent}a fresh note
+ EOF
+ git notes add -f -m"a fresh note" HEAD^ &&
+ test_config notes.rewriteMode overwrite &&
+ test_config notes.rewriteRef "refs/notes/*" &&
+ from=$(git rev-parse HEAD^) &&
+ to=$(git rev-parse HEAD) &&
+ echo "$from" "$to" >copy &&
+ git notes copy --for-rewrite=foo <copy &&
+ git log -1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git notes copy --for-rewrite (ignore)' '
+ test_config notes.rewriteMode ignore &&
+ test_config notes.rewriteRef "refs/notes/*" &&
+ from=$(git rev-parse HEAD^) &&
+ to=$(git rev-parse HEAD) &&
+ echo "$from" "$to" >copy &&
+ git notes copy --for-rewrite=foo <copy &&
+ git log -1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git notes copy --for-rewrite (append)' '
+ commit=$(git rev-parse HEAD) &&
+ cat >expect <<-EOF &&
+ commit $commit
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:27:13 2005 -0700
+
+ ${indent}15th
+
+ Notes:
+ ${indent}a fresh note
+ ${indent}
+ ${indent}another fresh note
+ EOF
+ git notes add -f -m"another fresh note" HEAD^ &&
+ test_config notes.rewriteMode concatenate &&
+ test_config notes.rewriteRef "refs/notes/*" &&
+ from=$(git rev-parse HEAD^) &&
+ to=$(git rev-parse HEAD) &&
+ echo "$from" "$to" >copy &&
+ git notes copy --for-rewrite=foo <copy &&
+ git log -1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git notes copy --for-rewrite (append two to one)' '
+ commit=$(git rev-parse HEAD) &&
+ cat >expect <<-EOF &&
+ commit $commit
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:27:13 2005 -0700
+
+ ${indent}15th
+
+ Notes:
+ ${indent}a fresh note
+ ${indent}
+ ${indent}another fresh note
+ ${indent}
+ ${indent}append 1
+ ${indent}
+ ${indent}append 2
+ EOF
+ git notes add -f -m"append 1" HEAD^ &&
+ git notes add -f -m"append 2" HEAD^^ &&
+ test_config notes.rewriteMode concatenate &&
+ test_config notes.rewriteRef "refs/notes/*" &&
+ from=$(git rev-parse HEAD^) &&
+ to=$(git rev-parse HEAD) &&
+ echo "$from" "$to" >copy &&
+ from=$(git rev-parse HEAD^^) &&
+ to=$(git rev-parse HEAD) &&
+ echo "$from" "$to" >>copy &&
+ git notes copy --for-rewrite=foo <copy &&
+ git log -1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git notes copy --for-rewrite (append empty)' '
+ git notes remove HEAD^ &&
+ test_config notes.rewriteMode concatenate &&
+ test_config notes.rewriteRef "refs/notes/*" &&
+ from=$(git rev-parse HEAD^) &&
+ to=$(git rev-parse HEAD) &&
+ echo "$from" "$to" >copy &&
+ git notes copy --for-rewrite=foo <copy &&
+ git log -1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'GIT_NOTES_REWRITE_MODE works' '
+ commit=$(git rev-parse HEAD) &&
+ cat >expect <<-EOF &&
+ commit $commit
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:27:13 2005 -0700
+
+ ${indent}15th
+
+ Notes:
+ ${indent}replacement note 1
+ EOF
+ test_config notes.rewriteMode concatenate &&
+ test_config notes.rewriteRef "refs/notes/*" &&
+ git notes add -f -m"replacement note 1" HEAD^ &&
+ from=$(git rev-parse HEAD^) &&
+ to=$(git rev-parse HEAD) &&
+ echo "$from" "$to" >copy &&
+ GIT_NOTES_REWRITE_MODE=overwrite git notes copy --for-rewrite=foo <copy &&
+ git log -1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'GIT_NOTES_REWRITE_REF works' '
+ commit=$(git rev-parse HEAD) &&
+ cat >expect <<-EOF &&
+ commit $commit
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:27:13 2005 -0700
+
+ ${indent}15th
+
+ Notes:
+ ${indent}replacement note 2
+ EOF
+ git notes add -f -m"replacement note 2" HEAD^ &&
+ test_config notes.rewriteMode overwrite &&
+ test_unconfig notes.rewriteRef &&
+ from=$(git rev-parse HEAD^) &&
+ to=$(git rev-parse HEAD) &&
+ echo "$from" "$to" >copy &&
+ GIT_NOTES_REWRITE_REF=refs/notes/commits:refs/notes/other \
+ git notes copy --for-rewrite=foo <copy &&
+ git log -1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'GIT_NOTES_REWRITE_REF overrides config' '
+ git notes add -f -m"replacement note 3" HEAD^ &&
+ test_config notes.rewriteMode overwrite &&
+ test_config notes.rewriteRef refs/notes/other &&
+ from=$(git rev-parse HEAD^) &&
+ to=$(git rev-parse HEAD) &&
+ echo "$from" "$to" >copy &&
+ GIT_NOTES_REWRITE_REF=refs/notes/commits \
+ git notes copy --for-rewrite=foo <copy &&
+ git log -1 >actual &&
+ grep "replacement note 3" actual
+'
+
+test_expect_success 'git notes copy diagnoses too many or too few arguments' '
+ test_must_fail git notes copy 2>error &&
+ test_grep "too few arguments" error &&
+ test_must_fail git notes copy one two three 2>error &&
+ test_grep "too many arguments" error
+'
+
+test_expect_success 'git notes get-ref expands refs/heads/main to refs/notes/refs/heads/main' '
+ test_unconfig core.notesRef &&
+ sane_unset GIT_NOTES_REF &&
+ echo refs/notes/refs/heads/main >expect &&
+ git notes --ref=refs/heads/main get-ref >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git notes get-ref (no overrides)' '
+ test_unconfig core.notesRef &&
+ sane_unset GIT_NOTES_REF &&
+ echo refs/notes/commits >expect &&
+ git notes get-ref >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git notes get-ref (core.notesRef)' '
+ test_config core.notesRef refs/notes/foo &&
+ echo refs/notes/foo >expect &&
+ git notes get-ref >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git notes get-ref (GIT_NOTES_REF)' '
+ echo refs/notes/bar >expect &&
+ GIT_NOTES_REF=refs/notes/bar git notes get-ref >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git notes get-ref (--ref)' '
+ echo refs/notes/baz >expect &&
+ GIT_NOTES_REF=refs/notes/bar git notes --ref=baz get-ref >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'setup testing of empty notes' '
+ test_unconfig core.notesRef &&
+ test_commit 16th &&
+ empty_blob=$(git hash-object -w /dev/null) &&
+ echo "$empty_blob" >expect_empty
+'
+
+while read cmd
+do
+ test_expect_success "'git notes $cmd' removes empty note" "
+ test_might_fail git notes remove HEAD &&
+ MSG= git notes $cmd &&
+ test_must_fail git notes list HEAD
+ "
+
+ test_expect_success "'git notes $cmd --allow-empty' stores empty note" "
+ test_might_fail git notes remove HEAD &&
+ MSG= git notes $cmd --allow-empty &&
+ git notes list HEAD >actual &&
+ test_cmp expect_empty actual
+ "
+done <<\EOF
+add
+add -F /dev/null
+add -m ""
+add -c "$empty_blob"
+add -C "$empty_blob"
+append
+append -F /dev/null
+append -m ""
+append -c "$empty_blob"
+append -C "$empty_blob"
+edit
+EOF
+
+test_expect_success 'empty notes are displayed by git log' '
+ test_commit 17th &&
+ git log -1 >expect &&
+ cat >>expect <<-EOF &&
+
+ Notes:
+ EOF
+ git notes add -C "$empty_blob" --allow-empty &&
+ git log -1 >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t3302-notes-index-expensive.sh b/t/t3302-notes-index-expensive.sh
new file mode 100755
index 0000000..d0c4d38
--- /dev/null
+++ b/t/t3302-notes-index-expensive.sh
@@ -0,0 +1,139 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Johannes E. Schindelin
+#
+
+test_description='Test commit notes index (expensive!)'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+create_repo () {
+ number_of_commits=$1
+ nr=0
+ test -d .git || {
+ git init &&
+ (
+ while test $nr -lt $number_of_commits
+ do
+ nr=$(($nr+1))
+ mark=$(($nr+$nr))
+ notemark=$(($mark+1))
+ test_tick &&
+ cat <<-INPUT_END &&
+ commit refs/heads/main
+ mark :$mark
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ commit #$nr
+ COMMIT
+
+ M 644 inline file
+ data <<EOF
+ file in commit #$nr
+ EOF
+
+ blob
+ mark :$notemark
+ data <<EOF
+ note for commit #$nr
+ EOF
+
+ INPUT_END
+ echo "N :$notemark :$mark" >>note_commit
+ done &&
+ test_tick &&
+ cat <<-INPUT_END &&
+ commit refs/notes/commits
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ notes
+ COMMIT
+
+ INPUT_END
+
+ cat note_commit
+ ) |
+ git fast-import --quiet &&
+ git config core.notesRef refs/notes/commits
+ }
+}
+
+test_notes () {
+ count=$1 &&
+ git config core.notesRef refs/notes/commits &&
+ git log >tmp &&
+ grep "^ " tmp >output &&
+ i=$count &&
+ while test $i -gt 0
+ do
+ echo " commit #$i" &&
+ echo " note for commit #$i" &&
+ i=$(($i-1))
+ done >expect &&
+ test_cmp expect output
+}
+
+write_script time_notes <<\EOF
+ mode=$1
+ i=1
+ while test $i -lt $2
+ do
+ case $1 in
+ no-notes)
+ GIT_NOTES_REF=non-existing
+ export GIT_NOTES_REF
+ ;;
+ notes)
+ unset GIT_NOTES_REF
+ ;;
+ esac
+ git log || exit $?
+ i=$(($i+1))
+ done >/dev/null
+EOF
+
+time_notes () {
+ for mode in no-notes notes
+ do
+ echo $mode
+ /usr/bin/time ../time_notes $mode $1
+ done
+}
+
+do_tests () {
+ count=$1 pr=${2-}
+
+ test_expect_success $pr "setup $count" '
+ mkdir "$count" &&
+ (
+ cd "$count" &&
+ create_repo "$count"
+ )
+ '
+
+ test_expect_success $pr 'notes work' '
+ (
+ cd "$count" &&
+ test_notes "$count"
+ )
+ '
+
+ test_expect_success "USR_BIN_TIME${pr:+,$pr}" 'notes timing with /usr/bin/time' '
+ (
+ cd "$count" &&
+ time_notes 100
+ )
+ '
+}
+
+do_tests 10
+for count in 100 1000 10000
+do
+ do_tests "$count" EXPENSIVE
+done
+
+test_done
diff --git a/t/t3303-notes-subtrees.sh b/t/t3303-notes-subtrees.sh
new file mode 100755
index 0000000..bc9b791
--- /dev/null
+++ b/t/t3303-notes-subtrees.sh
@@ -0,0 +1,199 @@
+#!/bin/sh
+
+test_description='Test commit notes organized in subtrees'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+number_of_commits=100
+
+start_note_commit () {
+ test_tick &&
+ cat <<INPUT_END
+commit refs/notes/commits
+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+data <<COMMIT
+notes
+COMMIT
+
+from refs/notes/commits^0
+deleteall
+INPUT_END
+
+}
+
+verify_notes () {
+ git log | grep "^ " > output &&
+ i=$number_of_commits &&
+ while [ $i -gt 0 ]; do
+ echo " commit #$i" &&
+ echo " note for commit #$i" &&
+ i=$(($i-1)) || return 1
+ done > expect &&
+ test_cmp expect output
+}
+
+test_expect_success "setup: create $number_of_commits commits" '
+
+ (
+ nr=0 &&
+ while [ $nr -lt $number_of_commits ]; do
+ nr=$(($nr+1)) &&
+ test_tick &&
+ cat <<INPUT_END || return 1
+commit refs/heads/main
+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+data <<COMMIT
+commit #$nr
+COMMIT
+
+M 644 inline file
+data <<EOF
+file in commit #$nr
+EOF
+
+INPUT_END
+
+ done &&
+ test_tick &&
+ cat <<INPUT_END
+commit refs/notes/commits
+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+data <<COMMIT
+no notes
+COMMIT
+
+deleteall
+
+INPUT_END
+
+ ) |
+ git fast-import --quiet &&
+ git config core.notesRef refs/notes/commits
+'
+
+test_sha1_based () {
+ (
+ start_note_commit &&
+ nr=$number_of_commits &&
+ git rev-list refs/heads/main >out &&
+ while read sha1; do
+ note_path=$(echo "$sha1" | sed "$1")
+ cat <<INPUT_END &&
+M 100644 inline $note_path
+data <<EOF
+note for commit #$nr
+EOF
+
+INPUT_END
+
+ nr=$(($nr-1))
+ done <out
+ ) >gfi &&
+ git fast-import --quiet <gfi
+}
+
+test_expect_success 'test notes in 2/38-fanout' 'test_sha1_based "s|^..|&/|"'
+test_expect_success 'verify notes in 2/38-fanout' 'verify_notes'
+
+test_expect_success 'test notes in 2/2/36-fanout' 'test_sha1_based "s|^\(..\)\(..\)|\1/\2/|"'
+test_expect_success 'verify notes in 2/2/36-fanout' 'verify_notes'
+
+test_expect_success 'test notes in 2/2/2/34-fanout' 'test_sha1_based "s|^\(..\)\(..\)\(..\)|\1/\2/\3/|"'
+test_expect_success 'verify notes in 2/2/2/34-fanout' 'verify_notes'
+
+test_same_notes () {
+ (
+ start_note_commit &&
+ nr=$number_of_commits &&
+ git rev-list refs/heads/main |
+ while read sha1; do
+ first_note_path=$(echo "$sha1" | sed "$1")
+ second_note_path=$(echo "$sha1" | sed "$2")
+ cat <<INPUT_END &&
+M 100644 inline $second_note_path
+data <<EOF
+note for commit #$nr
+EOF
+
+M 100644 inline $first_note_path
+data <<EOF
+note for commit #$nr
+EOF
+
+INPUT_END
+
+ nr=$(($nr-1))
+ done
+ ) |
+ git fast-import --quiet
+}
+
+test_expect_success 'test same notes in no fanout and 2/38-fanout' 'test_same_notes "s|^..|&/|" ""'
+test_expect_success 'verify same notes in no fanout and 2/38-fanout' 'verify_notes'
+
+test_expect_success 'test same notes in no fanout and 2/2/36-fanout' 'test_same_notes "s|^\(..\)\(..\)|\1/\2/|" ""'
+test_expect_success 'verify same notes in no fanout and 2/2/36-fanout' 'verify_notes'
+
+test_expect_success 'test same notes in 2/38-fanout and 2/2/36-fanout' 'test_same_notes "s|^\(..\)\(..\)|\1/\2/|" "s|^..|&/|"'
+test_expect_success 'verify same notes in 2/38-fanout and 2/2/36-fanout' 'verify_notes'
+
+test_expect_success 'test same notes in 2/2/2/34-fanout and 2/2/36-fanout' 'test_same_notes "s|^\(..\)\(..\)|\1/\2/|" "s|^\(..\)\(..\)\(..\)|\1/\2/\3/|"'
+test_expect_success 'verify same notes in 2/2/2/34-fanout and 2/2/36-fanout' 'verify_notes'
+
+test_concatenated_notes () {
+ (
+ start_note_commit &&
+ nr=$number_of_commits &&
+ git rev-list refs/heads/main |
+ while read sha1; do
+ first_note_path=$(echo "$sha1" | sed "$1")
+ second_note_path=$(echo "$sha1" | sed "$2")
+ cat <<INPUT_END &&
+M 100644 inline $second_note_path
+data <<EOF
+second note for commit #$nr
+EOF
+
+M 100644 inline $first_note_path
+data <<EOF
+first note for commit #$nr
+EOF
+
+INPUT_END
+
+ nr=$(($nr-1))
+ done
+ ) |
+ git fast-import --quiet
+}
+
+verify_concatenated_notes () {
+ git log | grep "^ " > output &&
+ i=$number_of_commits &&
+ while [ $i -gt 0 ]; do
+ echo " commit #$i" &&
+ echo " first note for commit #$i" &&
+ echo " " &&
+ echo " second note for commit #$i" &&
+ i=$(($i-1)) || return 1
+ done > expect &&
+ test_cmp expect output
+}
+
+test_expect_success 'test notes in no fanout concatenated with 2/38-fanout' 'test_concatenated_notes "s|^..|&/|" ""'
+test_expect_success 'verify notes in no fanout concatenated with 2/38-fanout' 'verify_concatenated_notes'
+
+test_expect_success 'test notes in no fanout concatenated with 2/2/36-fanout' 'test_concatenated_notes "s|^\(..\)\(..\)|\1/\2/|" ""'
+test_expect_success 'verify notes in no fanout concatenated with 2/2/36-fanout' 'verify_concatenated_notes'
+
+test_expect_success 'test notes in 2/38-fanout concatenated with 2/2/36-fanout' 'test_concatenated_notes "s|^\(..\)\(..\)|\1/\2/|" "s|^..|&/|"'
+test_expect_success 'verify notes in 2/38-fanout concatenated with 2/2/36-fanout' 'verify_concatenated_notes'
+
+test_expect_success 'test notes in 2/2/36-fanout concatenated with 2/2/2/34-fanout' 'test_concatenated_notes "s|^\(..\)\(..\)\(..\)|\1/\2/\3/|" "s|^\(..\)\(..\)|\1/\2/|"'
+test_expect_success 'verify notes in 2/2/36-fanout concatenated with 2/2/2/34-fanout' 'verify_concatenated_notes'
+
+test_done
diff --git a/t/t3304-notes-mixed.sh b/t/t3304-notes-mixed.sh
new file mode 100755
index 0000000..2c3a245
--- /dev/null
+++ b/t/t3304-notes-mixed.sh
@@ -0,0 +1,210 @@
+#!/bin/sh
+
+test_description='Test notes trees that also contain non-notes'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+number_of_commits=100
+
+start_note_commit () {
+ test_tick &&
+ cat <<INPUT_END
+commit refs/notes/commits
+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+data <<COMMIT
+notes
+COMMIT
+
+from refs/notes/commits^0
+deleteall
+INPUT_END
+
+}
+
+verify_notes () {
+ git log | grep "^ " > output &&
+ i=$number_of_commits &&
+ while [ $i -gt 0 ]; do
+ echo " commit #$i" &&
+ echo " note for commit #$i" &&
+ i=$(($i-1));
+ done > expect &&
+ test_cmp expect output
+}
+
+test_expect_success "setup: create a couple of commits" '
+
+ test_tick &&
+ cat <<INPUT_END >input &&
+commit refs/heads/main
+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+data <<COMMIT
+commit #1
+COMMIT
+
+M 644 inline file
+data <<EOF
+file in commit #1
+EOF
+
+INPUT_END
+
+ test_tick &&
+ cat <<INPUT_END >>input &&
+commit refs/heads/main
+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+data <<COMMIT
+commit #2
+COMMIT
+
+M 644 inline file
+data <<EOF
+file in commit #2
+EOF
+
+INPUT_END
+ git fast-import --quiet <input
+'
+
+test_expect_success "create a notes tree with both notes and non-notes" '
+
+ commit1=$(git rev-parse refs/heads/main^) &&
+ commit2=$(git rev-parse refs/heads/main) &&
+ test_tick &&
+ cat <<INPUT_END >input &&
+commit refs/notes/commits
+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+data <<COMMIT
+notes commit #1
+COMMIT
+
+N inline $commit1
+data <<EOF
+note for commit #1
+EOF
+
+N inline $commit2
+data <<EOF
+note for commit #2
+EOF
+
+INPUT_END
+ test_tick &&
+ cat <<INPUT_END >>input &&
+commit refs/notes/commits
+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+data <<COMMIT
+notes commit #2
+COMMIT
+
+M 644 inline foobar/non-note.txt
+data <<EOF
+A non-note in a notes tree
+EOF
+
+N inline $commit2
+data <<EOF
+edited note for commit #2
+EOF
+
+INPUT_END
+ test_tick &&
+ cat <<INPUT_END >>input &&
+commit refs/notes/commits
+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+data <<COMMIT
+notes commit #3
+COMMIT
+
+N inline $commit1
+data <<EOF
+edited note for commit #1
+EOF
+
+M 644 inline deadbeef
+data <<EOF
+non-note with SHA1-like name
+EOF
+
+M 644 inline de/adbeef
+data <<EOF
+another non-note with SHA1-like name
+EOF
+
+M 644 inline de/adbeefdeadbeefdeadbeefdeadbeefdeadbeef
+data <<EOF
+This is actually a valid note, albeit to a non-existing object.
+It is needed in order to trigger the "mishandling" of the dead/beef non-note.
+EOF
+
+M 644 inline dead/beef
+data <<EOF
+yet another non-note with SHA1-like name
+EOF
+
+INPUT_END
+ git fast-import --quiet <input &&
+ git config core.notesRef refs/notes/commits
+'
+
+cat >expect <<EXPECT_END
+ commit #2
+ edited note for commit #2
+ commit #1
+ edited note for commit #1
+EXPECT_END
+
+test_expect_success "verify contents of notes" '
+
+ git log | grep "^ " > actual &&
+ test_cmp expect actual
+'
+
+cat >expect_nn1 <<EXPECT_END
+A non-note in a notes tree
+EXPECT_END
+cat >expect_nn2 <<EXPECT_END
+non-note with SHA1-like name
+EXPECT_END
+cat >expect_nn3 <<EXPECT_END
+another non-note with SHA1-like name
+EXPECT_END
+cat >expect_nn4 <<EXPECT_END
+yet another non-note with SHA1-like name
+EXPECT_END
+
+test_expect_success "verify contents of non-notes" '
+
+ git cat-file -p refs/notes/commits:foobar/non-note.txt > actual_nn1 &&
+ test_cmp expect_nn1 actual_nn1 &&
+ git cat-file -p refs/notes/commits:deadbeef > actual_nn2 &&
+ test_cmp expect_nn2 actual_nn2 &&
+ git cat-file -p refs/notes/commits:de/adbeef > actual_nn3 &&
+ test_cmp expect_nn3 actual_nn3 &&
+ git cat-file -p refs/notes/commits:dead/beef > actual_nn4 &&
+ test_cmp expect_nn4 actual_nn4
+'
+
+test_expect_success "git-notes preserves non-notes" '
+
+ test_tick &&
+ git notes add -f -m "foo bar"
+'
+
+test_expect_success "verify contents of non-notes after git-notes" '
+
+ git cat-file -p refs/notes/commits:foobar/non-note.txt > actual_nn1 &&
+ test_cmp expect_nn1 actual_nn1 &&
+ git cat-file -p refs/notes/commits:deadbeef > actual_nn2 &&
+ test_cmp expect_nn2 actual_nn2 &&
+ git cat-file -p refs/notes/commits:de/adbeef > actual_nn3 &&
+ test_cmp expect_nn3 actual_nn3 &&
+ git cat-file -p refs/notes/commits:dead/beef > actual_nn4 &&
+ test_cmp expect_nn4 actual_nn4
+'
+
+test_done
diff --git a/t/t3305-notes-fanout.sh b/t/t3305-notes-fanout.sh
new file mode 100755
index 0000000..1ec1fb6
--- /dev/null
+++ b/t/t3305-notes-fanout.sh
@@ -0,0 +1,141 @@
+#!/bin/sh
+
+test_description='Test that adding/removing many notes triggers automatic fanout restructuring'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+path_has_fanout() {
+ path=$1 &&
+ fanout=$2 &&
+ after_last_slash=$(($(test_oid hexsz) - $fanout * 2)) &&
+ echo $path | grep -q -E "^([0-9a-f]{2}/){$fanout}[0-9a-f]{$after_last_slash}$"
+}
+
+touched_one_note_with_fanout() {
+ notes_commit=$1 &&
+ modification=$2 && # 'A' for addition, 'D' for deletion
+ fanout=$3 &&
+ diff=$(git diff-tree --no-commit-id --name-status --root -r $notes_commit) &&
+ path=$(echo $diff | sed -e "s/^$modification[\t ]//") &&
+ path_has_fanout "$path" $fanout;
+}
+
+all_notes_have_fanout() {
+ notes_commit=$1 &&
+ fanout=$2 &&
+ git ls-tree -r --name-only $notes_commit |
+ while read path
+ do
+ path_has_fanout $path $fanout || return 1
+ done
+}
+
+test_expect_success 'tweak test environment' '
+ git checkout -b nondeterminism &&
+ test_commit A &&
+ git checkout --orphan with_notes;
+'
+
+test_expect_success 'creating many notes with git-notes' '
+ num_notes=300 &&
+ i=0 &&
+ while test $i -lt $num_notes
+ do
+ i=$(($i + 1)) &&
+ test_tick &&
+ echo "file for commit #$i" > file &&
+ git add file &&
+ git commit -q -m "commit #$i" &&
+ git notes add -m "note #$i" || return 1
+ done
+'
+
+test_expect_success 'many notes created correctly with git-notes' '
+ git log >output.raw &&
+ grep "^ " output.raw >output &&
+ i=$num_notes &&
+ while test $i -gt 0
+ do
+ echo " commit #$i" &&
+ echo " note #$i" &&
+ i=$(($i - 1)) || return 1
+ done > expect &&
+ test_cmp expect output
+'
+
+test_expect_success 'stable fanout 0 is followed by stable fanout 1' '
+ i=$num_notes &&
+ fanout=0 &&
+ while test $i -gt 0
+ do
+ i=$(($i - 1)) &&
+ if touched_one_note_with_fanout refs/notes/commits~$i A $fanout
+ then
+ continue
+ elif test $fanout -eq 0
+ then
+ fanout=1 &&
+ if all_notes_have_fanout refs/notes/commits~$i $fanout
+ then
+ echo "Fanout 0 -> 1 at refs/notes/commits~$i" &&
+ continue
+ fi
+ fi &&
+ echo "Failed fanout=$fanout check at refs/notes/commits~$i" &&
+ git ls-tree -r --name-only refs/notes/commits~$i &&
+ return 1
+ done &&
+ all_notes_have_fanout refs/notes/commits 1
+'
+
+test_expect_success 'deleting most notes with git-notes' '
+ remove_notes=285 &&
+ i=0 &&
+ git rev-list HEAD >revs &&
+ while test $i -lt $remove_notes && read sha1
+ do
+ i=$(($i + 1)) &&
+ test_tick &&
+ git notes remove "$sha1" || return 1
+ done <revs
+'
+
+test_expect_success 'most notes deleted correctly with git-notes' '
+ git log HEAD~$remove_notes | grep "^ " > output &&
+ i=$(($num_notes - $remove_notes)) &&
+ while test $i -gt 0
+ do
+ echo " commit #$i" &&
+ echo " note #$i" &&
+ i=$(($i - 1)) || return 1
+ done > expect &&
+ test_cmp expect output
+'
+
+test_expect_success 'stable fanout 1 is followed by stable fanout 0' '
+ i=$remove_notes &&
+ fanout=1 &&
+ while test $i -gt 0
+ do
+ i=$(($i - 1)) &&
+ if touched_one_note_with_fanout refs/notes/commits~$i D $fanout
+ then
+ continue
+ elif test $fanout -eq 1
+ then
+ fanout=0 &&
+ if all_notes_have_fanout refs/notes/commits~$i $fanout
+ then
+ echo "Fanout 1 -> 0 at refs/notes/commits~$i" &&
+ continue
+ fi
+ fi &&
+ echo "Failed fanout=$fanout check at refs/notes/commits~$i" &&
+ git ls-tree -r --name-only refs/notes/commits~$i &&
+ return 1
+ done &&
+ all_notes_have_fanout refs/notes/commits 0
+'
+
+test_done
diff --git a/t/t3306-notes-prune.sh b/t/t3306-notes-prune.sh
new file mode 100755
index 0000000..8f4102f
--- /dev/null
+++ b/t/t3306-notes-prune.sh
@@ -0,0 +1,138 @@
+#!/bin/sh
+
+test_description='Test git notes prune'
+
+. ./test-lib.sh
+
+test_expect_success 'setup: create a few commits with notes' '
+
+ : > file1 &&
+ git add file1 &&
+ test_tick &&
+ git commit -m 1st &&
+ git notes add -m "Note #1" &&
+ first=$(git rev-parse HEAD) &&
+ : > file2 &&
+ git add file2 &&
+ test_tick &&
+ git commit -m 2nd &&
+ git notes add -m "Note #2" &&
+ second=$(git rev-parse HEAD) &&
+ : > file3 &&
+ git add file3 &&
+ test_tick &&
+ git commit -m 3rd &&
+ third=$(git rev-parse HEAD) &&
+ COMMIT_FILE=$(echo $third | sed "s!^..!.git/objects/&/!") &&
+ test -f $COMMIT_FILE &&
+ test-tool chmtime =+0 $COMMIT_FILE &&
+ git notes add -m "Note #3"
+'
+
+cat > expect <<END_OF_LOG
+commit $third
+Author: A U Thor <author@example.com>
+Date: Thu Apr 7 15:15:13 2005 -0700
+
+ 3rd
+
+Notes:
+ Note #3
+
+commit $second
+Author: A U Thor <author@example.com>
+Date: Thu Apr 7 15:14:13 2005 -0700
+
+ 2nd
+
+Notes:
+ Note #2
+
+commit $first
+Author: A U Thor <author@example.com>
+Date: Thu Apr 7 15:13:13 2005 -0700
+
+ 1st
+
+Notes:
+ Note #1
+END_OF_LOG
+
+test_expect_success 'verify commits and notes' '
+
+ git log > actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'remove some commits' '
+
+ git reset --hard HEAD~1 &&
+ git reflog expire --expire=now HEAD &&
+ git gc --prune=now
+'
+
+test_expect_success 'verify that commits are gone' '
+
+ test_must_fail git cat-file -p $third &&
+ git cat-file -p $second &&
+ git cat-file -p $first
+'
+
+test_expect_success 'verify that notes are still present' '
+
+ git notes show $third &&
+ git notes show $second &&
+ git notes show $first
+'
+
+test_expect_success 'prune -n does not remove notes' '
+
+ git notes list > expect &&
+ git notes prune -n &&
+ git notes list > actual &&
+ test_cmp expect actual
+'
+
+
+test_expect_success 'prune -n lists prunable notes' '
+
+ echo $third >expect &&
+ git notes prune -n > actual &&
+ test_cmp expect actual
+'
+
+
+test_expect_success 'prune notes' '
+
+ git notes prune
+'
+
+test_expect_success 'verify that notes are gone' '
+
+ test_must_fail git notes show $third &&
+ git notes show $second &&
+ git notes show $first
+'
+
+test_expect_success 'remove some commits' '
+
+ git reset --hard HEAD~1 &&
+ git reflog expire --expire=now HEAD &&
+ git gc --prune=now
+'
+
+test_expect_success 'prune -v notes' '
+
+ echo $second >expect &&
+ git notes prune -v > actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'verify that notes are gone' '
+
+ test_must_fail git notes show $third &&
+ test_must_fail git notes show $second &&
+ git notes show $first
+'
+
+test_done
diff --git a/t/t3307-notes-man.sh b/t/t3307-notes-man.sh
new file mode 100755
index 0000000..ae31650
--- /dev/null
+++ b/t/t3307-notes-man.sh
@@ -0,0 +1,39 @@
+#!/bin/sh
+
+test_description='Examples from the git-notes man page
+
+Make sure the manual is not full of lies.'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ test_commit A &&
+ test_commit B &&
+ test_commit C
+'
+
+test_expect_success 'example 1: notes to add an Acked-by line' '
+ cat <<-\EOF >expect &&
+ B
+
+ Notes:
+ Acked-by: A C Ker <acker@example.com>
+ EOF
+ git notes add -m "Acked-by: A C Ker <acker@example.com>" B &&
+ git show -s B^{commit} >log &&
+ tail -n 4 log >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'example 2: binary notes' '
+ cp "$TEST_DIRECTORY"/test-binary-1.png . &&
+ git checkout B &&
+ blob=$(git hash-object -w test-binary-1.png) &&
+ git notes --ref=logo add -C "$blob" &&
+ git notes --ref=logo copy B C &&
+ git notes --ref=logo show C >actual &&
+ test_cmp test-binary-1.png actual
+'
+
+test_done
diff --git a/t/t3308-notes-merge.sh b/t/t3308-notes-merge.sh
new file mode 100755
index 0000000..202702b
--- /dev/null
+++ b/t/t3308-notes-merge.sh
@@ -0,0 +1,394 @@
+#!/bin/sh
+#
+# Copyright (c) 2010 Johan Herland
+#
+
+test_description='Test merging of notes trees'
+
+. ./test-lib.sh
+
+test_expect_success setup '
+ test_commit 1st &&
+ test_commit 2nd &&
+ test_commit 3rd &&
+ test_commit 4th &&
+ test_commit 5th &&
+ # Create notes on 4 first commits
+ git config core.notesRef refs/notes/x &&
+ git notes add -m "Notes on 1st commit" 1st &&
+ git notes add -m "Notes on 2nd commit" 2nd &&
+ git notes add -m "Notes on 3rd commit" 3rd &&
+ git notes add -m "Notes on 4th commit" 4th &&
+ # Copy notes to remote-notes
+ git fetch . refs/notes/*:refs/remote-notes/origin/* &&
+
+ test_oid_cache <<-EOF
+ hash4a sha1:5e93d24084d32e1cb61f7070505b9d2530cca987
+ hash3a sha1:8366731eeee53787d2bdf8fc1eff7d94757e8da0
+ hash2a sha1:eede89064cd42441590d6afec6c37b321ada3389
+ hash1a sha1:daa55ffad6cb99bf64226532147ffcaf5ce8bdd1
+ hash5b sha1:0f2efbd00262f2fd41dfae33df8765618eeacd99
+ hash4b sha1:dec2502dac3ea161543f71930044deff93fa945c
+ hash3b sha1:4069cdb399fd45463ec6eef8e051a16a03592d91
+ hash2c sha1:d000d30e6ddcfce3a8122c403226a2ce2fd04d9d
+ hash1c sha1:43add6bd0c8c0bc871ac7991e0f5573cfba27804
+ hash4d sha1:1f257a3a90328557c452f0817d6cc50c89d315d4
+ hash3d sha1:05a4927951bcef347f51486575b878b2b60137f2
+
+ hash4a sha256:eef876be1d32ac2e2e42240e0429325cec116e55e88cb2969899fac695aa762f
+ hash3a sha256:cf7cd1bc091d7ba4166a86df864110e42087cd893a5ae96bc50d637e0290939d
+ hash2a sha256:21ddde7ebce2c285213898cb04deca0fd3209610cf7aaf8222e4e2f45262fae2
+ hash1a sha256:f9fe0eda16c6027732ed9d4295689a03abd16f893be69b3dcbf4037ddb191921
+ hash5b sha256:20046f2244577797a9e3d3f790ea9eca4d8a6bafb2a5570bcb0e03aa02ce100b
+ hash4b sha256:f90563d134c61a95bb88afbd45d48ccc9e919c62aa6fbfcd483302b3e4d8dbcb
+ hash3b sha256:988f2aca9f2d87e93e6a73197c2bb99560cc44a2f92d18653968f956f01221e0
+ hash2c sha256:84153b777b4d42827a756c6578dcdb59d8ae5d1360b874fb37c430150c825c26
+ hash1c sha256:9beb2bc4eef72e4c4087be168a20573e34d993d9ab1883055f23e322afa06567
+ hash4d sha256:32de39dc06e679a7abb2d4a55ede7709b3124340a4a90aa305971b1c72ac319d
+ hash3d sha256:fa73b20e41cbb7541c4c81d1535016131dbfbeb05bf6a71f6115e9cad31c7af5
+ EOF
+'
+
+commit_sha1=$(git rev-parse 1st^{commit})
+commit_sha2=$(git rev-parse 2nd^{commit})
+commit_sha3=$(git rev-parse 3rd^{commit})
+commit_sha4=$(git rev-parse 4th^{commit})
+commit_sha5=$(git rev-parse 5th^{commit})
+
+verify_notes () {
+ notes_ref="$1"
+ git -c core.notesRef="refs/notes/$notes_ref" notes |
+ sort >"output_notes_$notes_ref" &&
+ test_cmp "expect_notes_$notes_ref" "output_notes_$notes_ref" &&
+ git -c core.notesRef="refs/notes/$notes_ref" log --format="%H %s%n%N" \
+ >"output_log_$notes_ref" &&
+ test_cmp "expect_log_$notes_ref" "output_log_$notes_ref"
+}
+
+cat <<EOF | sort >expect_notes_x
+$(test_oid hash4a) $commit_sha4
+$(test_oid hash3a) $commit_sha3
+$(test_oid hash2a) $commit_sha2
+$(test_oid hash1a) $commit_sha1
+EOF
+
+cat >expect_log_x <<EOF
+$commit_sha5 5th
+
+$commit_sha4 4th
+Notes on 4th commit
+
+$commit_sha3 3rd
+Notes on 3rd commit
+
+$commit_sha2 2nd
+Notes on 2nd commit
+
+$commit_sha1 1st
+Notes on 1st commit
+
+EOF
+
+test_expect_success 'verify initial notes (x)' '
+ verify_notes x
+'
+
+cp expect_notes_x expect_notes_y
+cp expect_notes_x expect_notes_v
+cp expect_log_x expect_log_y
+cp expect_log_x expect_log_v
+
+test_expect_success 'fail to merge empty notes ref into empty notes ref (z => y)' '
+ test_must_fail git -c "core.notesRef=refs/notes/y" notes merge z
+'
+
+test_expect_success 'fail to merge into various non-notes refs' '
+ test_must_fail git -c "core.notesRef=refs/notes" notes merge x &&
+ test_must_fail git -c "core.notesRef=refs/notes/" notes merge x &&
+ git update-ref refs/notes/dir/foo HEAD &&
+ test_must_fail git -c "core.notesRef=refs/notes/dir" notes merge x &&
+ test_must_fail git -c "core.notesRef=refs/notes/dir/" notes merge x &&
+ test_must_fail git -c "core.notesRef=refs/heads/main" notes merge x &&
+ test_must_fail git -c "core.notesRef=refs/notes/y:" notes merge x &&
+ test_must_fail git -c "core.notesRef=refs/notes/y:foo" notes merge x &&
+ test_must_fail git -c "core.notesRef=refs/notes/foo^{bar" notes merge x
+'
+
+test_expect_success 'merge non-notes ref into empty notes ref (remote-notes/origin/x => v)' '
+ git config core.notesRef refs/notes/v &&
+ git notes merge refs/remote-notes/origin/x &&
+ verify_notes v &&
+ # refs/remote-notes/origin/x and v should point to the same notes commit
+ test "$(git rev-parse refs/remote-notes/origin/x)" = "$(git rev-parse refs/notes/v)"
+'
+
+test_expect_success 'merge notes into empty notes ref (x => y)' '
+ git config core.notesRef refs/notes/y &&
+ git notes merge x &&
+ verify_notes y &&
+ # x and y should point to the same notes commit
+ test "$(git rev-parse refs/notes/x)" = "$(git rev-parse refs/notes/y)"
+'
+
+test_expect_success 'merge empty notes ref (z => y)' '
+ git notes merge z &&
+ # y should not change (still == x)
+ test "$(git rev-parse refs/notes/x)" = "$(git rev-parse refs/notes/y)"
+'
+
+test_expect_success 'change notes on other notes ref (y)' '
+ # Not touching notes to 1st commit
+ git notes remove 2nd &&
+ git notes append -m "More notes on 3rd commit" 3rd &&
+ git notes add -f -m "New notes on 4th commit" 4th &&
+ git notes add -m "Notes on 5th commit" 5th
+'
+
+test_expect_success 'merge previous notes commit (y^ => y) => No-op' '
+ pre_state="$(git rev-parse refs/notes/y)" &&
+ git notes merge y^ &&
+ # y should not move
+ test "$pre_state" = "$(git rev-parse refs/notes/y)"
+'
+
+cat <<EOF | sort >expect_notes_y
+$(test_oid hash5b) $commit_sha5
+$(test_oid hash4b) $commit_sha4
+$(test_oid hash3b) $commit_sha3
+$(test_oid hash1a) $commit_sha1
+EOF
+
+cat >expect_log_y <<EOF
+$commit_sha5 5th
+Notes on 5th commit
+
+$commit_sha4 4th
+New notes on 4th commit
+
+$commit_sha3 3rd
+Notes on 3rd commit
+
+More notes on 3rd commit
+
+$commit_sha2 2nd
+
+$commit_sha1 1st
+Notes on 1st commit
+
+EOF
+
+test_expect_success 'verify changed notes on other notes ref (y)' '
+ verify_notes y
+'
+
+test_expect_success 'verify unchanged notes on original notes ref (x)' '
+ verify_notes x
+'
+
+test_expect_success 'merge original notes (x) into changed notes (y) => No-op' '
+ git notes merge -vvv x &&
+ verify_notes y &&
+ verify_notes x
+'
+
+cp expect_notes_y expect_notes_x
+cp expect_log_y expect_log_x
+
+test_expect_success 'merge changed (y) into original (x) => Fast-forward' '
+ git config core.notesRef refs/notes/x &&
+ git notes merge y &&
+ verify_notes x &&
+ verify_notes y &&
+ # x and y should point to same the notes commit
+ test "$(git rev-parse refs/notes/x)" = "$(git rev-parse refs/notes/y)"
+'
+
+test_expect_success 'merge empty notes ref (z => y)' '
+ # Prepare empty (but valid) notes ref (z)
+ git config core.notesRef refs/notes/z &&
+ git notes add -m "foo" &&
+ git notes remove &&
+ git notes >output_notes_z &&
+ test_must_be_empty output_notes_z &&
+ # Do the merge (z => y)
+ git config core.notesRef refs/notes/y &&
+ git notes merge z &&
+ verify_notes y &&
+ # y should no longer point to the same notes commit as x
+ test "$(git rev-parse refs/notes/x)" != "$(git rev-parse refs/notes/y)"
+'
+
+cat <<EOF | sort >expect_notes_y
+$(test_oid hash5b) $commit_sha5
+$(test_oid hash4b) $commit_sha4
+$(test_oid hash3b) $commit_sha3
+$(test_oid hash2c) $commit_sha2
+$(test_oid hash1c) $commit_sha1
+EOF
+
+cat >expect_log_y <<EOF
+$commit_sha5 5th
+Notes on 5th commit
+
+$commit_sha4 4th
+New notes on 4th commit
+
+$commit_sha3 3rd
+Notes on 3rd commit
+
+More notes on 3rd commit
+
+$commit_sha2 2nd
+New notes on 2nd commit
+
+$commit_sha1 1st
+Notes on 1st commit
+
+More notes on 1st commit
+
+EOF
+
+test_expect_success 'change notes on other notes ref (y)' '
+ # Append to 1st commit notes
+ git notes append -m "More notes on 1st commit" 1st &&
+ # Add new notes to 2nd commit
+ git notes add -m "New notes on 2nd commit" 2nd &&
+ verify_notes y
+'
+
+cat <<EOF | sort >expect_notes_x
+$(test_oid hash5b) $commit_sha5
+$(test_oid hash4d) $commit_sha4
+$(test_oid hash1a) $commit_sha1
+EOF
+
+cat >expect_log_x <<EOF
+$commit_sha5 5th
+Notes on 5th commit
+
+$commit_sha4 4th
+New notes on 4th commit
+
+More notes on 4th commit
+
+$commit_sha3 3rd
+
+$commit_sha2 2nd
+
+$commit_sha1 1st
+Notes on 1st commit
+
+EOF
+
+test_expect_success 'change notes on notes ref (x)' '
+ git config core.notesRef refs/notes/x &&
+ git notes remove 3rd &&
+ git notes append -m "More notes on 4th commit" 4th &&
+ verify_notes x
+'
+
+cat <<EOF | sort >expect_notes_x
+$(test_oid hash5b) $commit_sha5
+$(test_oid hash4d) $commit_sha4
+$(test_oid hash2c) $commit_sha2
+$(test_oid hash1c) $commit_sha1
+EOF
+
+cat >expect_log_x <<EOF
+$commit_sha5 5th
+Notes on 5th commit
+
+$commit_sha4 4th
+New notes on 4th commit
+
+More notes on 4th commit
+
+$commit_sha3 3rd
+
+$commit_sha2 2nd
+New notes on 2nd commit
+
+$commit_sha1 1st
+Notes on 1st commit
+
+More notes on 1st commit
+
+EOF
+
+test_expect_success 'merge y into x => Non-conflicting 3-way merge' '
+ git notes merge y &&
+ verify_notes x &&
+ verify_notes y
+'
+
+cat <<EOF | sort >expect_notes_w
+$(test_oid hash3d) $commit_sha3
+$(test_oid hash2c) $commit_sha2
+EOF
+
+cat >expect_log_w <<EOF
+$commit_sha5 5th
+
+$commit_sha4 4th
+
+$commit_sha3 3rd
+New notes on 3rd commit
+
+$commit_sha2 2nd
+New notes on 2nd commit
+
+$commit_sha1 1st
+
+EOF
+
+test_expect_success 'create notes on new, separate notes ref (w)' '
+ git config core.notesRef refs/notes/w &&
+ # Add same note as refs/notes/y on 2nd commit
+ git notes add -m "New notes on 2nd commit" 2nd &&
+ # Add new note on 3rd commit (non-conflicting)
+ git notes add -m "New notes on 3rd commit" 3rd &&
+ # Verify state of notes on new, separate notes ref (w)
+ verify_notes w
+'
+
+cat <<EOF | sort >expect_notes_x
+$(test_oid hash5b) $commit_sha5
+$(test_oid hash4d) $commit_sha4
+$(test_oid hash3d) $commit_sha3
+$(test_oid hash2c) $commit_sha2
+$(test_oid hash1c) $commit_sha1
+EOF
+
+cat >expect_log_x <<EOF
+$commit_sha5 5th
+Notes on 5th commit
+
+$commit_sha4 4th
+New notes on 4th commit
+
+More notes on 4th commit
+
+$commit_sha3 3rd
+New notes on 3rd commit
+
+$commit_sha2 2nd
+New notes on 2nd commit
+
+$commit_sha1 1st
+Notes on 1st commit
+
+More notes on 1st commit
+
+EOF
+
+test_expect_success 'merge w into x => Non-conflicting history-less merge' '
+ git config core.notesRef refs/notes/x &&
+ git notes merge w &&
+ # Verify new state of notes on other notes ref (x)
+ verify_notes x &&
+ # Also verify that nothing changed on other notes refs (y and w)
+ verify_notes y &&
+ verify_notes w
+'
+
+test_done
diff --git a/t/t3309-notes-merge-auto-resolve.sh b/t/t3309-notes-merge-auto-resolve.sh
new file mode 100755
index 0000000..9bd5dbf
--- /dev/null
+++ b/t/t3309-notes-merge-auto-resolve.sh
@@ -0,0 +1,791 @@
+#!/bin/sh
+#
+# Copyright (c) 2010 Johan Herland
+#
+
+test_description='Test notes merging with auto-resolving strategies'
+
+. ./test-lib.sh
+
+# Set up a notes merge scenario with all kinds of potential conflicts
+test_expect_success 'setup commits' '
+ test_commit 1st &&
+ test_commit 2nd &&
+ test_commit 3rd &&
+ test_commit 4th &&
+ test_commit 5th &&
+ test_commit 6th &&
+ test_commit 7th &&
+ test_commit 8th &&
+ test_commit 9th &&
+ test_commit 10th &&
+ test_commit 11th &&
+ test_commit 12th &&
+ test_commit 13th &&
+ test_commit 14th &&
+ test_commit 15th &&
+
+ test_oid_cache <<-EOF
+ hash15a sha1:457a85d6c814ea208550f15fcc48f804ac8dc023
+ hash14a sha1:b0c95b954301d69da2bc3723f4cb1680d355937c
+ hash13a sha1:5d30216a129eeffa97d9694ffe8c74317a560315
+ hash12a sha1:dd161bc149470fd890dd4ab52a4cbd79bbd18c36
+ hash11a sha1:7abbc45126d680336fb24294f013a7cdfa3ed545
+ hash10a sha1:b8d03e173f67f6505a76f6e00cf93440200dd9be
+ hash09a sha1:20c613c835011c48a5abe29170a2402ca6354910
+ hash08a sha1:a3daf8a1e4e5dc3409a303ad8481d57bfea7f5d6
+ hash07a sha1:897003322b53bc6ca098e9324ee508362347e734
+ hash06a sha1:11d97fdebfa5ceee540a3da07bce6fa0222bc082
+ hash15b sha1:68b8630d25516028bed862719855b3d6768d7833
+ hash14b sha1:5de7ea7ad4f47e7ff91989fb82234634730f75df
+ hash13b sha1:3a631fdb6f41b05b55d8f4baf20728ba8f6fccbc
+ hash12b sha1:a66055fa82f7a03fe0c02a6aba3287a85abf7c62
+ hash05b sha1:154508c7a0bcad82b6fe4b472bc4c26b3bf0825b
+ hash04b sha1:e2bfd06a37dd2031684a59a6e2b033e212239c78
+ hash03b sha1:5772f42408c0dd6f097a7ca2d24de0e78d1c46b1
+ hash15c sha1:9b4b2c61f0615412da3c10f98ff85b57c04ec765
+ hash11c sha1:7e3c53503a3db8dd996cb62e37c66e070b44b54d
+ hash08c sha1:851e1638784a884c7dd26c5d41f3340f6387413a
+ hash05c sha1:99fc34adfc400b95c67b013115e37e31aa9a6d23
+ hash02c sha1:283b48219aee9a4105f6cab337e789065c82c2b9
+ hash15d sha1:7c4e546efd0fe939f876beb262ece02797880b54
+ hash05d sha1:6c841cc36ea496027290967ca96bd2bef54dbb47
+ hash15e sha1:d682107b8bf7a7aea1e537a8d5cb6a12b60135f1
+ hash05e sha1:357b6ca14c7afd59b7f8b8aaaa6b8b723771135b
+ hash15f sha1:6be90240b5f54594203e25d9f2f64b7567175aee
+ hash05f sha1:660311d7f78dc53db12ac373a43fca7465381a7e
+
+ hash15a sha256:45b1558e5c1b75f570010fa48aaa67bb2289fcd431b34ad81cb4c8b95f4f872a
+ hash14a sha256:6e7af179ea4dd28afdc83ae6912ba0098cdeff764b26a8b750b157dd81749092
+ hash13a sha256:7353089961baf555388e1bac68c67c8ea94b08ccbd97532201cf7f6790703052
+ hash12a sha256:5863e4521689ee1879ceab3b38d39e93ab5b51ec70aaf6a96ad388fbdedfa25e
+ hash11a sha256:82a0ec0338b4ecf8b44304badf4ad38d7469dc41827f38d7ba6c42e3bae3ee98
+ hash10a sha256:e84f2564e92de9792c93b8d197262c735d7ccb1de6025cef8759af8f6c3308eb
+ hash09a sha256:4dd07764bcec696f195c0ea71ae89e174876403af1637e4642b8f4453fd23028
+ hash08a sha256:02132c4546cd88a1d0aa5854dd55da120927f7904ba16afe36fe03e91a622067
+ hash07a sha256:369baf7d00c6720efdc10273493555f943051f84a4706fb24caeb353fa4789db
+ hash06a sha256:52d32c10353583b2d96a5849b1f1f43c8018e76f3e8ef1b0d46eb5cff7cdefaf
+ hash15b sha256:345e6660b345fa174738a31a7a59423c394bdf414804e200bc510c65d971ae96
+ hash14b sha256:7653a6596021c52e405cba979eea15a729993e7102b9a61ba4667e34f0ead4a1
+ hash13b sha256:0f202a0b6b9690de2349c173dfd766a37e82744f61c14f1c389306f1d69f470b
+ hash12b sha256:eb00f219c026136ea6535b16ff8ec3efa510e6bf50098ca041e1a2a1d4b79840
+ hash05b sha256:993b2290cd0c24c27c849d99f1904f3b590f77af0f539932734ad05679ac5a2f
+ hash04b sha256:c7fba0d6104917fbf35258f40b9fa4fc697cfa992deecd1570a3b08d0a5587a9
+ hash03b sha256:7287a2d78a3766c181b08df38951d784b08b72a44f571ed6d855bd0be22c70f6
+ hash15c sha256:62316660a22bf97857dc4a16709ec4d93a224e8c9f37d661ef91751e1f4c4166
+ hash11c sha256:51c3763de9b08309370adc5036d58debb331980e73097902957c444602551daa
+ hash08c sha256:22cf1fa29599898a7218c51135d66ed85d22aad584f77db3305dedce4c3d4798
+ hash05c sha256:2508fd86db980f0508893a1c1571bdf3b2ee113dc25ddb1a3a2fb94bd6cd0d58
+ hash02c sha256:63bb527e0b4e1c8e1dd0d54dd778ca7c3718689fd6e37c473044cfbcf1cacfdb
+ hash15d sha256:667acb4e2d5f8df15e5aea4506dfd16d25bc7feca70fdb0d965a7222f983bb88
+ hash05d sha256:09e6b5a6fe666c4a027674b6611a254b7d2528cd211c6b5288d1b4db6c741dfa
+ hash15e sha256:e8cbf52f6fcadc6de3c7761e64a89e9fe38d19a03d3e28ef6ca8596d93fc4f3a
+ hash05e sha256:cdb1e19f7ba1539f95af51a57edeb88a7ecc97d3c2f52da8c4c86af308595607
+ hash15f sha256:29c14cb92da448a923963b8a43994268b19c2e57913de73f3667421fd2c0eeec
+ hash05f sha256:14a6e641b2c0a9f398ebac6b4d34afa5efea4c52d2631382f45f8f662266903b
+ EOF
+'
+
+commit_sha1=$(git rev-parse 1st^{commit})
+commit_sha2=$(git rev-parse 2nd^{commit})
+commit_sha3=$(git rev-parse 3rd^{commit})
+commit_sha4=$(git rev-parse 4th^{commit})
+commit_sha5=$(git rev-parse 5th^{commit})
+commit_sha6=$(git rev-parse 6th^{commit})
+commit_sha7=$(git rev-parse 7th^{commit})
+commit_sha8=$(git rev-parse 8th^{commit})
+commit_sha9=$(git rev-parse 9th^{commit})
+commit_sha10=$(git rev-parse 10th^{commit})
+commit_sha11=$(git rev-parse 11th^{commit})
+commit_sha12=$(git rev-parse 12th^{commit})
+commit_sha13=$(git rev-parse 13th^{commit})
+commit_sha14=$(git rev-parse 14th^{commit})
+commit_sha15=$(git rev-parse 15th^{commit})
+
+verify_notes () {
+ notes_ref="$1"
+ suffix="$2"
+ git -c core.notesRef="refs/notes/$notes_ref" notes |
+ sort >"output_notes_$suffix" &&
+ test_cmp "expect_notes_$suffix" "output_notes_$suffix" &&
+ git -c core.notesRef="refs/notes/$notes_ref" log --format="%H %s%n%N" \
+ >"output_log_$suffix" &&
+ test_cmp "expect_log_$suffix" "output_log_$suffix"
+}
+
+test_expect_success 'setup merge base (x)' '
+ git config core.notesRef refs/notes/x &&
+ git notes add -m "x notes on 6th commit" 6th &&
+ git notes add -m "x notes on 7th commit" 7th &&
+ git notes add -m "x notes on 8th commit" 8th &&
+ git notes add -m "x notes on 9th commit" 9th &&
+ git notes add -m "x notes on 10th commit" 10th &&
+ git notes add -m "x notes on 11th commit" 11th &&
+ git notes add -m "x notes on 12th commit" 12th &&
+ git notes add -m "x notes on 13th commit" 13th &&
+ git notes add -m "x notes on 14th commit" 14th &&
+ git notes add -m "x notes on 15th commit" 15th
+'
+
+cat <<EOF | sort >expect_notes_x
+$(test_oid hash15a) $commit_sha15
+$(test_oid hash14a) $commit_sha14
+$(test_oid hash13a) $commit_sha13
+$(test_oid hash12a) $commit_sha12
+$(test_oid hash11a) $commit_sha11
+$(test_oid hash10a) $commit_sha10
+$(test_oid hash09a) $commit_sha9
+$(test_oid hash08a) $commit_sha8
+$(test_oid hash07a) $commit_sha7
+$(test_oid hash06a) $commit_sha6
+EOF
+
+cat >expect_log_x <<EOF
+$commit_sha15 15th
+x notes on 15th commit
+
+$commit_sha14 14th
+x notes on 14th commit
+
+$commit_sha13 13th
+x notes on 13th commit
+
+$commit_sha12 12th
+x notes on 12th commit
+
+$commit_sha11 11th
+x notes on 11th commit
+
+$commit_sha10 10th
+x notes on 10th commit
+
+$commit_sha9 9th
+x notes on 9th commit
+
+$commit_sha8 8th
+x notes on 8th commit
+
+$commit_sha7 7th
+x notes on 7th commit
+
+$commit_sha6 6th
+x notes on 6th commit
+
+$commit_sha5 5th
+
+$commit_sha4 4th
+
+$commit_sha3 3rd
+
+$commit_sha2 2nd
+
+$commit_sha1 1st
+
+EOF
+
+test_expect_success 'verify state of merge base (x)' 'verify_notes x x'
+
+test_expect_success 'setup local branch (y)' '
+ git update-ref refs/notes/y refs/notes/x &&
+ git config core.notesRef refs/notes/y &&
+ git notes add -f -m "y notes on 3rd commit" 3rd &&
+ git notes add -f -m "y notes on 4th commit" 4th &&
+ git notes add -f -m "y notes on 5th commit" 5th &&
+ git notes remove 6th &&
+ git notes remove 7th &&
+ git notes remove 8th &&
+ git notes add -f -m "y notes on 12th commit" 12th &&
+ git notes add -f -m "y notes on 13th commit" 13th &&
+ git notes add -f -m "y notes on 14th commit" 14th &&
+ git notes add -f -m "y notes on 15th commit" 15th
+'
+
+cat <<EOF | sort >expect_notes_y
+$(test_oid hash15b) $commit_sha15
+$(test_oid hash14b) $commit_sha14
+$(test_oid hash13b) $commit_sha13
+$(test_oid hash12b) $commit_sha12
+$(test_oid hash11a) $commit_sha11
+$(test_oid hash10a) $commit_sha10
+$(test_oid hash09a) $commit_sha9
+$(test_oid hash05b) $commit_sha5
+$(test_oid hash04b) $commit_sha4
+$(test_oid hash03b) $commit_sha3
+EOF
+
+cat >expect_log_y <<EOF
+$commit_sha15 15th
+y notes on 15th commit
+
+$commit_sha14 14th
+y notes on 14th commit
+
+$commit_sha13 13th
+y notes on 13th commit
+
+$commit_sha12 12th
+y notes on 12th commit
+
+$commit_sha11 11th
+x notes on 11th commit
+
+$commit_sha10 10th
+x notes on 10th commit
+
+$commit_sha9 9th
+x notes on 9th commit
+
+$commit_sha8 8th
+
+$commit_sha7 7th
+
+$commit_sha6 6th
+
+$commit_sha5 5th
+y notes on 5th commit
+
+$commit_sha4 4th
+y notes on 4th commit
+
+$commit_sha3 3rd
+y notes on 3rd commit
+
+$commit_sha2 2nd
+
+$commit_sha1 1st
+
+EOF
+
+test_expect_success 'verify state of local branch (y)' 'verify_notes y y'
+
+test_expect_success 'setup remote branch (z)' '
+ git update-ref refs/notes/z refs/notes/x &&
+ git config core.notesRef refs/notes/z &&
+ git notes add -f -m "z notes on 2nd commit" 2nd &&
+ git notes add -f -m "y notes on 4th commit" 4th &&
+ git notes add -f -m "z notes on 5th commit" 5th &&
+ git notes remove 6th &&
+ git notes add -f -m "z notes on 8th commit" 8th &&
+ git notes remove 9th &&
+ git notes add -f -m "z notes on 11th commit" 11th &&
+ git notes remove 12th &&
+ git notes add -f -m "y notes on 14th commit" 14th &&
+ git notes add -f -m "z notes on 15th commit" 15th
+'
+
+cat <<EOF | sort >expect_notes_z
+$(test_oid hash15c) $commit_sha15
+$(test_oid hash14b) $commit_sha14
+$(test_oid hash13a) $commit_sha13
+$(test_oid hash11c) $commit_sha11
+$(test_oid hash10a) $commit_sha10
+$(test_oid hash08c) $commit_sha8
+$(test_oid hash07a) $commit_sha7
+$(test_oid hash05c) $commit_sha5
+$(test_oid hash04b) $commit_sha4
+$(test_oid hash02c) $commit_sha2
+EOF
+
+cat >expect_log_z <<EOF
+$commit_sha15 15th
+z notes on 15th commit
+
+$commit_sha14 14th
+y notes on 14th commit
+
+$commit_sha13 13th
+x notes on 13th commit
+
+$commit_sha12 12th
+
+$commit_sha11 11th
+z notes on 11th commit
+
+$commit_sha10 10th
+x notes on 10th commit
+
+$commit_sha9 9th
+
+$commit_sha8 8th
+z notes on 8th commit
+
+$commit_sha7 7th
+x notes on 7th commit
+
+$commit_sha6 6th
+
+$commit_sha5 5th
+z notes on 5th commit
+
+$commit_sha4 4th
+y notes on 4th commit
+
+$commit_sha3 3rd
+
+$commit_sha2 2nd
+z notes on 2nd commit
+
+$commit_sha1 1st
+
+EOF
+
+test_expect_success 'verify state of remote branch (z)' 'verify_notes z z'
+
+# At this point, before merging z into y, we have the following status:
+#
+# commit | base/x | local/y | remote/z | diff from x to y/z | result
+# -------|---------|---------|----------|----------------------------|-------
+# 1st | [none] | [none] | [none] | unchanged / unchanged | [none]
+# 2nd | [none] | [none] | 283b482 | unchanged / added | 283b482
+# 3rd | [none] | 5772f42 | [none] | added / unchanged | 5772f42
+# 4th | [none] | e2bfd06 | e2bfd06 | added / added (same) | e2bfd06
+# 5th | [none] | 154508c | 99fc34a | added / added (diff) | ???
+# 6th | 11d97fd | [none] | [none] | removed / removed | [none]
+# 7th | 8970033 | [none] | 8970033 | removed / unchanged | [none]
+# 8th | a3daf8a | [none] | 851e163 | removed / changed | ???
+# 9th | 20c613c | 20c613c | [none] | unchanged / removed | [none]
+# 10th | b8d03e1 | b8d03e1 | b8d03e1 | unchanged / unchanged | b8d03e1
+# 11th | 7abbc45 | 7abbc45 | 7e3c535 | unchanged / changed | 7e3c535
+# 12th | dd161bc | a66055f | [none] | changed / removed | ???
+# 13th | 5d30216 | 3a631fd | 5d30216 | changed / unchanged | 3a631fd
+# 14th | b0c95b9 | 5de7ea7 | 5de7ea7 | changed / changed (same) | 5de7ea7
+# 15th | 457a85d | 68b8630 | 9b4b2c6 | changed / changed (diff) | ???
+
+test_expect_success 'merge z into y with invalid strategy => Fail/No changes' '
+ git config core.notesRef refs/notes/y &&
+ test_must_fail git notes merge --strategy=foo z &&
+ # Verify no changes (y)
+ verify_notes y y
+'
+
+test_expect_success 'merge z into y with invalid configuration option => Fail/No changes' '
+ git config core.notesRef refs/notes/y &&
+ cat >expect <<-\EOF &&
+ error: unknown notes merge strategy foo
+ fatal: unable to parse '\''notes.mergeStrategy'\'' from command-line config
+ EOF
+ test_must_fail git -c notes.mergeStrategy="foo" notes merge z 2>actual &&
+ test_cmp expect actual &&
+ # Verify no changes (y)
+ verify_notes y y
+'
+
+cat <<EOF | sort >expect_notes_ours
+$(test_oid hash15b) $commit_sha15
+$(test_oid hash14b) $commit_sha14
+$(test_oid hash13b) $commit_sha13
+$(test_oid hash12b) $commit_sha12
+$(test_oid hash11c) $commit_sha11
+$(test_oid hash10a) $commit_sha10
+$(test_oid hash05b) $commit_sha5
+$(test_oid hash04b) $commit_sha4
+$(test_oid hash03b) $commit_sha3
+$(test_oid hash02c) $commit_sha2
+EOF
+
+cat >expect_log_ours <<EOF
+$commit_sha15 15th
+y notes on 15th commit
+
+$commit_sha14 14th
+y notes on 14th commit
+
+$commit_sha13 13th
+y notes on 13th commit
+
+$commit_sha12 12th
+y notes on 12th commit
+
+$commit_sha11 11th
+z notes on 11th commit
+
+$commit_sha10 10th
+x notes on 10th commit
+
+$commit_sha9 9th
+
+$commit_sha8 8th
+
+$commit_sha7 7th
+
+$commit_sha6 6th
+
+$commit_sha5 5th
+y notes on 5th commit
+
+$commit_sha4 4th
+y notes on 4th commit
+
+$commit_sha3 3rd
+y notes on 3rd commit
+
+$commit_sha2 2nd
+z notes on 2nd commit
+
+$commit_sha1 1st
+
+EOF
+
+test_expect_success 'merge z into y with "ours" strategy => Non-conflicting 3-way merge' '
+ git notes merge --strategy=ours z &&
+ verify_notes y ours
+'
+
+test_expect_success 'reset to pre-merge state (y)' '
+ git update-ref refs/notes/y refs/notes/y^1 &&
+ # Verify pre-merge state
+ verify_notes y y
+'
+
+test_expect_success 'merge z into y with "ours" configuration option => Non-conflicting 3-way merge' '
+ git -c notes.mergeStrategy="ours" notes merge z &&
+ verify_notes y ours
+'
+
+test_expect_success 'reset to pre-merge state (y)' '
+ git update-ref refs/notes/y refs/notes/y^1 &&
+ # Verify pre-merge state
+ verify_notes y y
+'
+
+test_expect_success 'merge z into y with "ours" per-ref configuration option => Non-conflicting 3-way merge' '
+ git -c notes.y.mergeStrategy="ours" notes merge z &&
+ verify_notes y ours
+'
+
+test_expect_success 'reset to pre-merge state (y)' '
+ git update-ref refs/notes/y refs/notes/y^1 &&
+ # Verify pre-merge state
+ verify_notes y y
+'
+
+cat <<EOF | sort >expect_notes_theirs
+$(test_oid hash15c) $commit_sha15
+$(test_oid hash14b) $commit_sha14
+$(test_oid hash13b) $commit_sha13
+$(test_oid hash11c) $commit_sha11
+$(test_oid hash10a) $commit_sha10
+$(test_oid hash08c) $commit_sha8
+$(test_oid hash05c) $commit_sha5
+$(test_oid hash04b) $commit_sha4
+$(test_oid hash03b) $commit_sha3
+$(test_oid hash02c) $commit_sha2
+EOF
+
+cat >expect_log_theirs <<EOF
+$commit_sha15 15th
+z notes on 15th commit
+
+$commit_sha14 14th
+y notes on 14th commit
+
+$commit_sha13 13th
+y notes on 13th commit
+
+$commit_sha12 12th
+
+$commit_sha11 11th
+z notes on 11th commit
+
+$commit_sha10 10th
+x notes on 10th commit
+
+$commit_sha9 9th
+
+$commit_sha8 8th
+z notes on 8th commit
+
+$commit_sha7 7th
+
+$commit_sha6 6th
+
+$commit_sha5 5th
+z notes on 5th commit
+
+$commit_sha4 4th
+y notes on 4th commit
+
+$commit_sha3 3rd
+y notes on 3rd commit
+
+$commit_sha2 2nd
+z notes on 2nd commit
+
+$commit_sha1 1st
+
+EOF
+
+test_expect_success 'merge z into y with "theirs" strategy => Non-conflicting 3-way merge' '
+ git notes merge --strategy=theirs z &&
+ verify_notes y theirs
+'
+
+test_expect_success 'reset to pre-merge state (y)' '
+ git update-ref refs/notes/y refs/notes/y^1 &&
+ # Verify pre-merge state
+ verify_notes y y
+'
+
+test_expect_success 'merge z into y with "theirs" strategy overriding configuration option "ours" => Non-conflicting 3-way merge' '
+ git -c notes.mergeStrategy="ours" notes merge --strategy=theirs z &&
+ verify_notes y theirs
+'
+
+test_expect_success 'reset to pre-merge state (y)' '
+ git update-ref refs/notes/y refs/notes/y^1 &&
+ # Verify pre-merge state
+ verify_notes y y
+'
+
+cat <<EOF | sort >expect_notes_union
+$(test_oid hash15d) $commit_sha15
+$(test_oid hash14b) $commit_sha14
+$(test_oid hash13b) $commit_sha13
+$(test_oid hash12b) $commit_sha12
+$(test_oid hash11c) $commit_sha11
+$(test_oid hash10a) $commit_sha10
+$(test_oid hash08c) $commit_sha8
+$(test_oid hash05d) $commit_sha5
+$(test_oid hash04b) $commit_sha4
+$(test_oid hash03b) $commit_sha3
+$(test_oid hash02c) $commit_sha2
+EOF
+
+cat >expect_log_union <<EOF
+$commit_sha15 15th
+y notes on 15th commit
+
+z notes on 15th commit
+
+$commit_sha14 14th
+y notes on 14th commit
+
+$commit_sha13 13th
+y notes on 13th commit
+
+$commit_sha12 12th
+y notes on 12th commit
+
+$commit_sha11 11th
+z notes on 11th commit
+
+$commit_sha10 10th
+x notes on 10th commit
+
+$commit_sha9 9th
+
+$commit_sha8 8th
+z notes on 8th commit
+
+$commit_sha7 7th
+
+$commit_sha6 6th
+
+$commit_sha5 5th
+y notes on 5th commit
+
+z notes on 5th commit
+
+$commit_sha4 4th
+y notes on 4th commit
+
+$commit_sha3 3rd
+y notes on 3rd commit
+
+$commit_sha2 2nd
+z notes on 2nd commit
+
+$commit_sha1 1st
+
+EOF
+
+test_expect_success 'merge z into y with "union" strategy => Non-conflicting 3-way merge' '
+ git notes merge --strategy=union z &&
+ verify_notes y union
+'
+
+test_expect_success 'reset to pre-merge state (y)' '
+ git update-ref refs/notes/y refs/notes/y^1 &&
+ # Verify pre-merge state
+ verify_notes y y
+'
+
+test_expect_success 'merge z into y with "union" strategy overriding per-ref configuration => Non-conflicting 3-way merge' '
+ git -c notes.y.mergeStrategy="theirs" notes merge --strategy=union z &&
+ verify_notes y union
+'
+
+test_expect_success 'reset to pre-merge state (y)' '
+ git update-ref refs/notes/y refs/notes/y^1 &&
+ # Verify pre-merge state
+ verify_notes y y
+'
+
+test_expect_success 'merge z into y with "union" per-ref overriding general configuration => Non-conflicting 3-way merge' '
+ git -c notes.y.mergeStrategy="union" -c notes.mergeStrategy="theirs" notes merge z &&
+ verify_notes y union
+'
+
+test_expect_success 'reset to pre-merge state (y)' '
+ git update-ref refs/notes/y refs/notes/y^1 &&
+ # Verify pre-merge state
+ verify_notes y y
+'
+
+test_expect_success 'merge z into y with "manual" per-ref only checks specific ref configuration => Conflicting 3-way merge' '
+ test_must_fail git -c notes.z.mergeStrategy="union" notes merge z &&
+ git notes merge --abort &&
+ verify_notes y y
+'
+
+cat <<EOF | sort >expect_notes_union2
+$(test_oid hash15e) $commit_sha15
+$(test_oid hash14b) $commit_sha14
+$(test_oid hash13b) $commit_sha13
+$(test_oid hash12b) $commit_sha12
+$(test_oid hash11c) $commit_sha11
+$(test_oid hash10a) $commit_sha10
+$(test_oid hash08c) $commit_sha8
+$(test_oid hash05e) $commit_sha5
+$(test_oid hash04b) $commit_sha4
+$(test_oid hash03b) $commit_sha3
+$(test_oid hash02c) $commit_sha2
+EOF
+
+cat >expect_log_union2 <<EOF
+$commit_sha15 15th
+z notes on 15th commit
+
+y notes on 15th commit
+
+$commit_sha14 14th
+y notes on 14th commit
+
+$commit_sha13 13th
+y notes on 13th commit
+
+$commit_sha12 12th
+y notes on 12th commit
+
+$commit_sha11 11th
+z notes on 11th commit
+
+$commit_sha10 10th
+x notes on 10th commit
+
+$commit_sha9 9th
+
+$commit_sha8 8th
+z notes on 8th commit
+
+$commit_sha7 7th
+
+$commit_sha6 6th
+
+$commit_sha5 5th
+z notes on 5th commit
+
+y notes on 5th commit
+
+$commit_sha4 4th
+y notes on 4th commit
+
+$commit_sha3 3rd
+y notes on 3rd commit
+
+$commit_sha2 2nd
+z notes on 2nd commit
+
+$commit_sha1 1st
+
+EOF
+
+test_expect_success 'merge y into z with "union" strategy => Non-conflicting 3-way merge' '
+ git config core.notesRef refs/notes/z &&
+ git notes merge --strategy=union y &&
+ verify_notes z union2
+'
+
+test_expect_success 'reset to pre-merge state (z)' '
+ git update-ref refs/notes/z refs/notes/z^1 &&
+ # Verify pre-merge state
+ verify_notes z z
+'
+
+cat <<EOF | sort >expect_notes_cat_sort_uniq
+$(test_oid hash15f) $commit_sha15
+$(test_oid hash14b) $commit_sha14
+$(test_oid hash13b) $commit_sha13
+$(test_oid hash12b) $commit_sha12
+$(test_oid hash11c) $commit_sha11
+$(test_oid hash10a) $commit_sha10
+$(test_oid hash08c) $commit_sha8
+$(test_oid hash05f) $commit_sha5
+$(test_oid hash04b) $commit_sha4
+$(test_oid hash03b) $commit_sha3
+$(test_oid hash02c) $commit_sha2
+EOF
+
+cat >expect_log_cat_sort_uniq <<EOF
+$commit_sha15 15th
+y notes on 15th commit
+z notes on 15th commit
+
+$commit_sha14 14th
+y notes on 14th commit
+
+$commit_sha13 13th
+y notes on 13th commit
+
+$commit_sha12 12th
+y notes on 12th commit
+
+$commit_sha11 11th
+z notes on 11th commit
+
+$commit_sha10 10th
+x notes on 10th commit
+
+$commit_sha9 9th
+
+$commit_sha8 8th
+z notes on 8th commit
+
+$commit_sha7 7th
+
+$commit_sha6 6th
+
+$commit_sha5 5th
+y notes on 5th commit
+z notes on 5th commit
+
+$commit_sha4 4th
+y notes on 4th commit
+
+$commit_sha3 3rd
+y notes on 3rd commit
+
+$commit_sha2 2nd
+z notes on 2nd commit
+
+$commit_sha1 1st
+
+EOF
+
+test_expect_success 'merge y into z with "cat_sort_uniq" strategy => Non-conflicting 3-way merge' '
+ git notes merge --strategy=cat_sort_uniq y &&
+ verify_notes z cat_sort_uniq
+'
+
+test_expect_success 'reset to pre-merge state (z)' '
+ git update-ref refs/notes/z refs/notes/z^1 &&
+ # Verify pre-merge state
+ verify_notes z z
+'
+
+test_expect_success 'merge y into z with "cat_sort_uniq" strategy configuration option => Non-conflicting 3-way merge' '
+ git -c notes.mergeStrategy="cat_sort_uniq" notes merge y &&
+ verify_notes z cat_sort_uniq
+'
+
+test_done
diff --git a/t/t3310-notes-merge-manual-resolve.sh b/t/t3310-notes-merge-manual-resolve.sh
new file mode 100755
index 0000000..60d6ed2
--- /dev/null
+++ b/t/t3310-notes-merge-manual-resolve.sh
@@ -0,0 +1,617 @@
+#!/bin/sh
+#
+# Copyright (c) 2010 Johan Herland
+#
+
+test_description='Test notes merging with manual conflict resolution'
+
+. ./test-lib.sh
+
+# Set up a notes merge scenario with different kinds of conflicts
+test_expect_success 'setup commits' '
+ test_commit 1st &&
+ test_commit 2nd &&
+ test_commit 3rd &&
+ test_commit 4th &&
+ test_commit 5th &&
+
+ test_oid_cache <<-EOF
+ hash04a sha1:6e8e3febca3c2bb896704335cc4d0c34cb2f8715
+ hash03a sha1:e5388c10860456ee60673025345fe2e153eb8cf8
+ hash02a sha1:ceefa674873670e7ecd131814d909723cce2b669
+ hash04b sha1:e2bfd06a37dd2031684a59a6e2b033e212239c78
+ hash03b sha1:5772f42408c0dd6f097a7ca2d24de0e78d1c46b1
+ hash01b sha1:b0a6021ec006d07e80e9b20ec9b444cbd9d560d3
+ hash04c sha1:cff59c793c20bb49a4e01bc06fb06bad642e0d54
+ hash02c sha1:283b48219aee9a4105f6cab337e789065c82c2b9
+ hash01c sha1:0a81da8956346e19bcb27a906f04af327e03e31b
+ hash04d sha1:00494adecf2d9635a02fa431308d67993f853968
+ hash01e sha1:f75d1df88cbfe4258d49852f26cfc83f2ad4494b
+ hash04f sha1:021faa20e931fb48986ffc6282b4bb05553ac946
+ hash01f sha1:0a59e787e6d688aa6309e56e8c1b89431a0fc1c1
+ hash05g sha1:304dfb4325cf243025b9957486eb605a9b51c199
+
+ hash04a sha256:f18a935e65866345098b3b754071dbf9f3aa3520eb27a7b036b278c5e2f1ed7e
+ hash03a sha256:713035dc94067a64e5fa6e4e1821b7c3bde49a77c7cb3f80eaadefa1ca41b3d2
+ hash02a sha256:f160a67e048b6fa75bec3952184154045076692cf5dccd3da21e3fd34b7a3f0f
+ hash04b sha256:c7fba0d6104917fbf35258f40b9fa4fc697cfa992deecd1570a3b08d0a5587a9
+ hash03b sha256:7287a2d78a3766c181b08df38951d784b08b72a44f571ed6d855bd0be22c70f6
+ hash01b sha256:da96cf778c15d0a2bb76f98b2a62f6c9c01730fa7030e8f08ef0191048e7d620
+ hash04c sha256:cb615d2def4b834d5f55b2351df97dc92bee4f5009d285201427f349081c8aca
+ hash02c sha256:63bb527e0b4e1c8e1dd0d54dd778ca7c3718689fd6e37c473044cfbcf1cacfdb
+ hash01c sha256:5b87237ac1fbae0246256fed9f9a1f077c4140fb7e6444925f8dbfa5ae406cd8
+ hash04d sha256:eeddc9f9f6cb3d6b39b861659853f10891dc373e0b6eecb09e03e39b6ce64714
+ hash01e sha256:108f521b1a74c2e6d0b52a4eda87e09162bf847f7d190cfce496ee1af0b29a5a
+ hash04f sha256:901acda0454502b3bbd281f130c419e6c8de78afcf72a8def8d45ad31462bce4
+ hash01f sha256:a2d99d1b8bf23c8af7d9d91368454adc110dfd5cc068a4cebb486ee8f5a1e16c
+ hash05g sha256:4fef015b01da8efe929a68e3bb9b8fbad81f53995f097befe8ebc93f12ab98ec
+ EOF
+'
+
+commit_sha1=$(git rev-parse 1st^{commit})
+commit_sha2=$(git rev-parse 2nd^{commit})
+commit_sha3=$(git rev-parse 3rd^{commit})
+commit_sha4=$(git rev-parse 4th^{commit})
+commit_sha5=$(git rev-parse 5th^{commit})
+
+verify_notes () {
+ notes_ref="$1"
+ git -c core.notesRef="refs/notes/$notes_ref" notes |
+ sort >"output_notes_$notes_ref" &&
+ test_cmp "expect_notes_$notes_ref" "output_notes_$notes_ref" &&
+ git -c core.notesRef="refs/notes/$notes_ref" log --format="%H %s%n%N" \
+ >"output_log_$notes_ref" &&
+ test_cmp "expect_log_$notes_ref" "output_log_$notes_ref"
+}
+
+notes_merge_files_gone () {
+ # No .git/NOTES_MERGE_* files left
+ { ls .git/NOTES_MERGE_* >output || :; } &&
+ test_must_be_empty output
+}
+
+cat <<EOF | sort >expect_notes_x
+$(test_oid hash04a) $commit_sha4
+$(test_oid hash03a) $commit_sha3
+$(test_oid hash02a) $commit_sha2
+EOF
+
+cat >expect_log_x <<EOF
+$commit_sha5 5th
+
+$commit_sha4 4th
+x notes on 4th commit
+
+$commit_sha3 3rd
+x notes on 3rd commit
+
+$commit_sha2 2nd
+x notes on 2nd commit
+
+$commit_sha1 1st
+
+EOF
+
+test_expect_success 'setup merge base (x)' '
+ git config core.notesRef refs/notes/x &&
+ git notes add -m "x notes on 2nd commit" 2nd &&
+ git notes add -m "x notes on 3rd commit" 3rd &&
+ git notes add -m "x notes on 4th commit" 4th &&
+ verify_notes x
+'
+
+cat <<EOF | sort >expect_notes_y
+$(test_oid hash04b) $commit_sha4
+$(test_oid hash03b) $commit_sha3
+$(test_oid hash01b) $commit_sha1
+EOF
+
+cat >expect_log_y <<EOF
+$commit_sha5 5th
+
+$commit_sha4 4th
+y notes on 4th commit
+
+$commit_sha3 3rd
+y notes on 3rd commit
+
+$commit_sha2 2nd
+
+$commit_sha1 1st
+y notes on 1st commit
+
+EOF
+
+test_expect_success 'setup local branch (y)' '
+ git update-ref refs/notes/y refs/notes/x &&
+ git config core.notesRef refs/notes/y &&
+ git notes add -f -m "y notes on 1st commit" 1st &&
+ git notes remove 2nd &&
+ git notes add -f -m "y notes on 3rd commit" 3rd &&
+ git notes add -f -m "y notes on 4th commit" 4th &&
+ verify_notes y
+'
+
+cat <<EOF | sort >expect_notes_z
+$(test_oid hash04c) $commit_sha4
+$(test_oid hash02c) $commit_sha2
+$(test_oid hash01c) $commit_sha1
+EOF
+
+cat >expect_log_z <<EOF
+$commit_sha5 5th
+
+$commit_sha4 4th
+z notes on 4th commit
+
+$commit_sha3 3rd
+
+$commit_sha2 2nd
+z notes on 2nd commit
+
+$commit_sha1 1st
+z notes on 1st commit
+
+EOF
+
+test_expect_success 'setup remote branch (z)' '
+ git update-ref refs/notes/z refs/notes/x &&
+ git config core.notesRef refs/notes/z &&
+ git notes add -f -m "z notes on 1st commit" 1st &&
+ git notes add -f -m "z notes on 2nd commit" 2nd &&
+ git notes remove 3rd &&
+ git notes add -f -m "z notes on 4th commit" 4th &&
+ verify_notes z
+'
+
+# At this point, before merging z into y, we have the following status:
+#
+# commit | base/x | local/y | remote/z | diff from x to y/z
+# -------|---------|---------|----------|---------------------------
+# 1st | [none] | b0a6021 | 0a81da8 | added / added (diff)
+# 2nd | ceefa67 | [none] | 283b482 | removed / changed
+# 3rd | e5388c1 | 5772f42 | [none] | changed / removed
+# 4th | 6e8e3fe | e2bfd06 | cff59c7 | changed / changed (diff)
+# 5th | [none] | [none] | [none] | [none]
+
+cat <<EOF | sort >expect_conflicts
+$commit_sha1
+$commit_sha2
+$commit_sha3
+$commit_sha4
+EOF
+
+cat >expect_conflict_$commit_sha1 <<EOF
+<<<<<<< refs/notes/m
+y notes on 1st commit
+=======
+z notes on 1st commit
+>>>>>>> refs/notes/z
+EOF
+
+cat >expect_conflict_$commit_sha2 <<EOF
+z notes on 2nd commit
+EOF
+
+cat >expect_conflict_$commit_sha3 <<EOF
+y notes on 3rd commit
+EOF
+
+cat >expect_conflict_$commit_sha4 <<EOF
+<<<<<<< refs/notes/m
+y notes on 4th commit
+=======
+z notes on 4th commit
+>>>>>>> refs/notes/z
+EOF
+
+cp expect_notes_y expect_notes_m
+cp expect_log_y expect_log_m
+
+git rev-parse refs/notes/y > pre_merge_y
+git rev-parse refs/notes/z > pre_merge_z
+
+test_expect_success 'merge z into m (== y) with default ("manual") resolver => Conflicting 3-way merge' '
+ git update-ref refs/notes/m refs/notes/y &&
+ git config core.notesRef refs/notes/m &&
+ test_must_fail git notes merge z >output 2>&1 &&
+ # Output should point to where to resolve conflicts
+ test_grep "\\.git/NOTES_MERGE_WORKTREE" output &&
+ # Inspect merge conflicts
+ ls .git/NOTES_MERGE_WORKTREE >output_conflicts &&
+ test_cmp expect_conflicts output_conflicts &&
+ ( for f in $(cat expect_conflicts); do
+ test_cmp "expect_conflict_$f" ".git/NOTES_MERGE_WORKTREE/$f" ||
+ exit 1
+ done ) &&
+ # Verify that current notes tree (pre-merge) has not changed (m == y)
+ verify_notes y &&
+ verify_notes m &&
+ test "$(git rev-parse refs/notes/m)" = "$(cat pre_merge_y)"
+'
+
+cat <<EOF | sort >expect_notes_z
+$(test_oid hash04d) $commit_sha4
+$(test_oid hash02c) $commit_sha2
+$(test_oid hash01c) $commit_sha1
+EOF
+
+cat >expect_log_z <<EOF
+$commit_sha5 5th
+
+$commit_sha4 4th
+z notes on 4th commit
+
+More z notes on 4th commit
+
+$commit_sha3 3rd
+
+$commit_sha2 2nd
+z notes on 2nd commit
+
+$commit_sha1 1st
+z notes on 1st commit
+
+EOF
+
+test_expect_success 'change notes in z' '
+ git notes --ref z append -m "More z notes on 4th commit" 4th &&
+ verify_notes z
+'
+
+test_expect_success 'cannot do merge w/conflicts when previous merge is unfinished' '
+ test -d .git/NOTES_MERGE_WORKTREE &&
+ test_must_fail git notes merge z >output 2>&1 &&
+ # Output should indicate what is wrong
+ test_grep -q "\\.git/NOTES_MERGE_\\* exists" output
+'
+
+# Setup non-conflicting merge between x and new notes ref w
+
+cat <<EOF | sort >expect_notes_w
+$(test_oid hash02a) $commit_sha2
+$(test_oid hash01e) $commit_sha1
+EOF
+
+cat >expect_log_w <<EOF
+$commit_sha5 5th
+
+$commit_sha4 4th
+
+$commit_sha3 3rd
+
+$commit_sha2 2nd
+x notes on 2nd commit
+
+$commit_sha1 1st
+w notes on 1st commit
+
+EOF
+
+test_expect_success 'setup unrelated notes ref (w)' '
+ git config core.notesRef refs/notes/w &&
+ git notes add -m "w notes on 1st commit" 1st &&
+ git notes add -m "x notes on 2nd commit" 2nd &&
+ verify_notes w
+'
+
+cat <<EOF | sort >expect_notes_w
+$(test_oid hash04a) $commit_sha4
+$(test_oid hash03a) $commit_sha3
+$(test_oid hash02a) $commit_sha2
+$(test_oid hash01e) $commit_sha1
+EOF
+
+cat >expect_log_w <<EOF
+$commit_sha5 5th
+
+$commit_sha4 4th
+x notes on 4th commit
+
+$commit_sha3 3rd
+x notes on 3rd commit
+
+$commit_sha2 2nd
+x notes on 2nd commit
+
+$commit_sha1 1st
+w notes on 1st commit
+
+EOF
+
+test_expect_success 'can do merge without conflicts even if previous merge is unfinished (x => w)' '
+ test -d .git/NOTES_MERGE_WORKTREE &&
+ git notes merge x &&
+ verify_notes w &&
+ # Verify that other notes refs has not changed (x and y)
+ verify_notes x &&
+ verify_notes y
+'
+
+cat <<EOF | sort >expect_notes_m
+$(test_oid hash04f) $commit_sha4
+$(test_oid hash03b) $commit_sha3
+$(test_oid hash02c) $commit_sha2
+$(test_oid hash01f) $commit_sha1
+EOF
+
+cat >expect_log_m <<EOF
+$commit_sha5 5th
+
+$commit_sha4 4th
+y and z notes on 4th commit
+
+$commit_sha3 3rd
+y notes on 3rd commit
+
+$commit_sha2 2nd
+z notes on 2nd commit
+
+$commit_sha1 1st
+y and z notes on 1st commit
+
+EOF
+
+test_expect_success 'do not allow mixing --commit and --abort' '
+ test_must_fail git notes merge --commit --abort
+'
+
+test_expect_success 'do not allow mixing --commit and --strategy' '
+ test_must_fail git notes merge --commit --strategy theirs
+'
+
+test_expect_success 'do not allow mixing --abort and --strategy' '
+ test_must_fail git notes merge --abort --strategy theirs
+'
+
+test_expect_success 'finalize conflicting merge (z => m)' '
+ # Resolve conflicts and finalize merge
+ cat >.git/NOTES_MERGE_WORKTREE/$commit_sha1 <<EOF &&
+y and z notes on 1st commit
+EOF
+ cat >.git/NOTES_MERGE_WORKTREE/$commit_sha4 <<EOF &&
+y and z notes on 4th commit
+EOF
+ git notes merge --commit &&
+ notes_merge_files_gone &&
+ # Merge commit has pre-merge y and pre-merge z as parents
+ test "$(git rev-parse refs/notes/m^1)" = "$(cat pre_merge_y)" &&
+ test "$(git rev-parse refs/notes/m^2)" = "$(cat pre_merge_z)" &&
+ # Merge commit mentions the notes refs merged
+ git log -1 --format=%B refs/notes/m > merge_commit_msg &&
+ grep -q refs/notes/m merge_commit_msg &&
+ grep -q refs/notes/z merge_commit_msg &&
+ # Merge commit mentions conflicting notes
+ grep -q "Conflicts" merge_commit_msg &&
+ ( for sha1 in $(cat expect_conflicts); do
+ grep -q "$sha1" merge_commit_msg ||
+ exit 1
+ done ) &&
+ # Verify contents of merge result
+ verify_notes m &&
+ # Verify that other notes refs has not changed (w, x, y and z)
+ verify_notes w &&
+ verify_notes x &&
+ verify_notes y &&
+ verify_notes z
+'
+
+cat >expect_conflict_$commit_sha4 <<EOF
+<<<<<<< refs/notes/m
+y notes on 4th commit
+=======
+z notes on 4th commit
+
+More z notes on 4th commit
+>>>>>>> refs/notes/z
+EOF
+
+cp expect_notes_y expect_notes_m
+cp expect_log_y expect_log_m
+
+git rev-parse refs/notes/y > pre_merge_y
+git rev-parse refs/notes/z > pre_merge_z
+
+test_expect_success 'redo merge of z into m (== y) with default ("manual") resolver => Conflicting 3-way merge' '
+ git update-ref refs/notes/m refs/notes/y &&
+ git config core.notesRef refs/notes/m &&
+ test_must_fail git notes merge z >output 2>&1 &&
+ # Output should point to where to resolve conflicts
+ test_grep "\\.git/NOTES_MERGE_WORKTREE" output &&
+ # Inspect merge conflicts
+ ls .git/NOTES_MERGE_WORKTREE >output_conflicts &&
+ test_cmp expect_conflicts output_conflicts &&
+ ( for f in $(cat expect_conflicts); do
+ test_cmp "expect_conflict_$f" ".git/NOTES_MERGE_WORKTREE/$f" ||
+ exit 1
+ done ) &&
+ # Verify that current notes tree (pre-merge) has not changed (m == y)
+ verify_notes y &&
+ verify_notes m &&
+ test "$(git rev-parse refs/notes/m)" = "$(cat pre_merge_y)"
+'
+
+test_expect_success 'abort notes merge' '
+ git notes merge --abort &&
+ notes_merge_files_gone &&
+ # m has not moved (still == y)
+ test "$(git rev-parse refs/notes/m)" = "$(cat pre_merge_y)" &&
+ # Verify that other notes refs has not changed (w, x, y and z)
+ verify_notes w &&
+ verify_notes x &&
+ verify_notes y &&
+ verify_notes z
+'
+
+git rev-parse refs/notes/y > pre_merge_y
+git rev-parse refs/notes/z > pre_merge_z
+
+test_expect_success 'redo merge of z into m (== y) with default ("manual") resolver => Conflicting 3-way merge' '
+ test_must_fail git notes merge z >output 2>&1 &&
+ # Output should point to where to resolve conflicts
+ test_grep "\\.git/NOTES_MERGE_WORKTREE" output &&
+ # Inspect merge conflicts
+ ls .git/NOTES_MERGE_WORKTREE >output_conflicts &&
+ test_cmp expect_conflicts output_conflicts &&
+ ( for f in $(cat expect_conflicts); do
+ test_cmp "expect_conflict_$f" ".git/NOTES_MERGE_WORKTREE/$f" ||
+ exit 1
+ done ) &&
+ # Verify that current notes tree (pre-merge) has not changed (m == y)
+ verify_notes y &&
+ verify_notes m &&
+ test "$(git rev-parse refs/notes/m)" = "$(cat pre_merge_y)"
+'
+
+cat <<EOF | sort >expect_notes_m
+$(test_oid hash05g) $commit_sha5
+$(test_oid hash02c) $commit_sha2
+$(test_oid hash01f) $commit_sha1
+EOF
+
+cat >expect_log_m <<EOF
+$commit_sha5 5th
+new note on 5th commit
+
+$commit_sha4 4th
+
+$commit_sha3 3rd
+
+$commit_sha2 2nd
+z notes on 2nd commit
+
+$commit_sha1 1st
+y and z notes on 1st commit
+
+EOF
+
+test_expect_success 'add + remove notes in finalized merge (z => m)' '
+ # Resolve one conflict
+ cat >.git/NOTES_MERGE_WORKTREE/$commit_sha1 <<EOF &&
+y and z notes on 1st commit
+EOF
+ # Remove another conflict
+ rm .git/NOTES_MERGE_WORKTREE/$commit_sha4 &&
+ # Remove a D/F conflict
+ rm .git/NOTES_MERGE_WORKTREE/$commit_sha3 &&
+ # Add a new note
+ echo "new note on 5th commit" > .git/NOTES_MERGE_WORKTREE/$commit_sha5 &&
+ # Finalize merge
+ git notes merge --commit &&
+ notes_merge_files_gone &&
+ # Merge commit has pre-merge y and pre-merge z as parents
+ test "$(git rev-parse refs/notes/m^1)" = "$(cat pre_merge_y)" &&
+ test "$(git rev-parse refs/notes/m^2)" = "$(cat pre_merge_z)" &&
+ # Merge commit mentions the notes refs merged
+ git log -1 --format=%B refs/notes/m > merge_commit_msg &&
+ grep -q refs/notes/m merge_commit_msg &&
+ grep -q refs/notes/z merge_commit_msg &&
+ # Merge commit mentions conflicting notes
+ grep -q "Conflicts" merge_commit_msg &&
+ ( for sha1 in $(cat expect_conflicts); do
+ grep -q "$sha1" merge_commit_msg ||
+ exit 1
+ done ) &&
+ # Verify contents of merge result
+ verify_notes m &&
+ # Verify that other notes refs has not changed (w, x, y and z)
+ verify_notes w &&
+ verify_notes x &&
+ verify_notes y &&
+ verify_notes z
+'
+
+cp expect_notes_y expect_notes_m
+cp expect_log_y expect_log_m
+
+test_expect_success 'redo merge of z into m (== y) with default ("manual") resolver => Conflicting 3-way merge' '
+ git update-ref refs/notes/m refs/notes/y &&
+ test_must_fail git notes merge z >output 2>&1 &&
+ # Output should point to where to resolve conflicts
+ test_grep "\\.git/NOTES_MERGE_WORKTREE" output &&
+ # Inspect merge conflicts
+ ls .git/NOTES_MERGE_WORKTREE >output_conflicts &&
+ test_cmp expect_conflicts output_conflicts &&
+ ( for f in $(cat expect_conflicts); do
+ test_cmp "expect_conflict_$f" ".git/NOTES_MERGE_WORKTREE/$f" ||
+ exit 1
+ done ) &&
+ # Verify that current notes tree (pre-merge) has not changed (m == y)
+ verify_notes y &&
+ verify_notes m &&
+ test "$(git rev-parse refs/notes/m)" = "$(cat pre_merge_y)"
+'
+
+cp expect_notes_w expect_notes_m
+cp expect_log_w expect_log_m
+
+test_expect_success 'reset notes ref m to somewhere else (w)' '
+ git update-ref refs/notes/m refs/notes/w &&
+ verify_notes m &&
+ test "$(git rev-parse refs/notes/m)" = "$(git rev-parse refs/notes/w)"
+'
+
+test_expect_success 'fail to finalize conflicting merge if underlying ref has moved in the meantime (m != NOTES_MERGE_PARTIAL^1)' '
+ # Resolve conflicts
+ cat >.git/NOTES_MERGE_WORKTREE/$commit_sha1 <<EOF &&
+y and z notes on 1st commit
+EOF
+ cat >.git/NOTES_MERGE_WORKTREE/$commit_sha4 <<EOF &&
+y and z notes on 4th commit
+EOF
+ # Fail to finalize merge
+ test_must_fail git notes merge --commit >output 2>&1 &&
+ # .git/NOTES_MERGE_* must remain
+ test -f .git/NOTES_MERGE_PARTIAL &&
+ test -f .git/NOTES_MERGE_REF &&
+ test -f .git/NOTES_MERGE_WORKTREE/$commit_sha1 &&
+ test -f .git/NOTES_MERGE_WORKTREE/$commit_sha2 &&
+ test -f .git/NOTES_MERGE_WORKTREE/$commit_sha3 &&
+ test -f .git/NOTES_MERGE_WORKTREE/$commit_sha4 &&
+ # Refs are unchanged
+ test "$(git rev-parse refs/notes/m)" = "$(git rev-parse refs/notes/w)" &&
+ test "$(git rev-parse refs/notes/y)" = "$(git rev-parse NOTES_MERGE_PARTIAL^1)" &&
+ test "$(git rev-parse refs/notes/m)" != "$(git rev-parse NOTES_MERGE_PARTIAL^1)" &&
+ # Mention refs/notes/m, and its current and expected value in output
+ test_grep -q "refs/notes/m" output &&
+ test_grep -q "$(git rev-parse refs/notes/m)" output &&
+ test_grep -q "$(git rev-parse NOTES_MERGE_PARTIAL^1)" output &&
+ # Verify that other notes refs has not changed (w, x, y and z)
+ verify_notes w &&
+ verify_notes x &&
+ verify_notes y &&
+ verify_notes z
+'
+
+test_expect_success 'resolve situation by aborting the notes merge' '
+ git notes merge --abort &&
+ notes_merge_files_gone &&
+ # m has not moved (still == w)
+ test "$(git rev-parse refs/notes/m)" = "$(git rev-parse refs/notes/w)" &&
+ # Verify that other notes refs has not changed (w, x, y and z)
+ verify_notes w &&
+ verify_notes x &&
+ verify_notes y &&
+ verify_notes z
+'
+
+cat >expect_notes <<EOF
+foo
+bar
+EOF
+
+test_expect_success 'switch cwd before committing notes merge' '
+ git notes add -m foo HEAD &&
+ git notes --ref=other add -m bar HEAD &&
+ test_must_fail git notes merge refs/notes/other &&
+ (
+ cd .git/NOTES_MERGE_WORKTREE &&
+ echo "foo" > $(git rev-parse HEAD) &&
+ echo "bar" >> $(git rev-parse HEAD) &&
+ git notes merge --commit
+ ) &&
+ git notes show HEAD > actual_notes &&
+ test_cmp expect_notes actual_notes
+'
+
+test_done
diff --git a/t/t3311-notes-merge-fanout.sh b/t/t3311-notes-merge-fanout.sh
new file mode 100755
index 0000000..5b67541
--- /dev/null
+++ b/t/t3311-notes-merge-fanout.sh
@@ -0,0 +1,446 @@
+#!/bin/sh
+#
+# Copyright (c) 2010 Johan Herland
+#
+
+test_description='Test notes merging at various fanout levels'
+
+. ./test-lib.sh
+
+verify_notes () {
+ notes_ref="$1"
+ commit="$2"
+ if test -f "expect_notes_$notes_ref"
+ then
+ git -c core.notesRef="refs/notes/$notes_ref" notes |
+ sort >"output_notes_$notes_ref" &&
+ test_cmp "expect_notes_$notes_ref" "output_notes_$notes_ref" ||
+ return 1
+ fi &&
+ git -c core.notesRef="refs/notes/$notes_ref" log --format="%H %s%n%N" \
+ "$commit" >"output_log_$notes_ref" &&
+ test_cmp "expect_log_$notes_ref" "output_log_$notes_ref"
+}
+
+verify_fanout () {
+ notes_ref="$1"
+ # Expect entire notes tree to have a fanout == 1
+ git rev-parse --quiet --verify "refs/notes/$notes_ref" >/dev/null &&
+ git ls-tree -r --name-only "refs/notes/$notes_ref" |
+ while read path
+ do
+ echo "$path" | grep "^../[0-9a-f]*$" || {
+ echo "Invalid path \"$path\"" &&
+ return 1;
+ }
+ done
+}
+
+verify_no_fanout () {
+ notes_ref="$1"
+ # Expect entire notes tree to have a fanout == 0
+ git rev-parse --quiet --verify "refs/notes/$notes_ref" >/dev/null &&
+ git ls-tree -r --name-only "refs/notes/$notes_ref" |
+ while read path
+ do
+ echo "$path" | grep -v "^../.*" || {
+ echo "Invalid path \"$path\"" &&
+ return 1;
+ }
+ done
+}
+
+# Set up a notes merge scenario with different kinds of conflicts
+test_expect_success 'setup a few initial commits with notes (notes ref: x)' '
+ git config core.notesRef refs/notes/x &&
+ for i in 1 2 3 4 5
+ do
+ test_commit "commit$i" >/dev/null &&
+ git notes add -m "notes for commit$i" || return 1
+ done &&
+
+ git log --format=oneline &&
+
+ test_oid_cache <<-EOF
+ hash05a sha1:aed91155c7a72c2188e781fdf40e0f3761b299db
+ hash04a sha1:99fab268f9d7ee7b011e091a436c78def8eeee69
+ hash03a sha1:953c20ae26c7aa0b428c20693fe38bc687f9d1a9
+ hash02a sha1:6358796131b8916eaa2dde6902642942a1cb37e1
+ hash01a sha1:b02d459c32f0e68f2fe0981033bb34f38776ba47
+ hash03b sha1:9f506ee70e20379d7f78204c77b334f43d77410d
+ hash02b sha1:23a47d6ea7d589895faf800752054818e1e7627b
+
+ hash05a sha256:3aae5d26619d96dba93795f66325716e4cbc486884f95a6adee8fb0615a76d12
+ hash04a sha256:07e43dd3d89fe634d3252e253b426aacc7285a995dcdbcf94ac284060a1122cf
+ hash03a sha256:26fb52eaa7f4866bf735254587be7b31209ec10e525912ffd8e8ba549ba892ff
+ hash02a sha256:b57ebdf23634e750dcbc4b9a37991d70f90830d568a0e4529ce9de0a3f8d605c
+ hash01a sha256:377903b1572bd5117087a5518fcb1011b5053cccbc59e3c7c823a8615204173b
+ hash03b sha256:04e7b392fda7c185bfa17c9179b56db732edc2dc2b3bf887308dcaabb717270d
+ hash02b sha256:66099aaaec49a485ed990acadd9a9b81232ea592079964113d8f581ff69ef50b
+ EOF
+'
+
+commit_sha1=$(git rev-parse commit1^{commit})
+commit_sha2=$(git rev-parse commit2^{commit})
+commit_sha3=$(git rev-parse commit3^{commit})
+commit_sha4=$(git rev-parse commit4^{commit})
+commit_sha5=$(git rev-parse commit5^{commit})
+
+cat <<EOF | sort >expect_notes_x
+$(test_oid hash05a) $commit_sha5
+$(test_oid hash04a) $commit_sha4
+$(test_oid hash03a) $commit_sha3
+$(test_oid hash02a) $commit_sha2
+$(test_oid hash01a) $commit_sha1
+EOF
+
+cat >expect_log_x <<EOF
+$commit_sha5 commit5
+notes for commit5
+
+$commit_sha4 commit4
+notes for commit4
+
+$commit_sha3 commit3
+notes for commit3
+
+$commit_sha2 commit2
+notes for commit2
+
+$commit_sha1 commit1
+notes for commit1
+
+EOF
+
+test_expect_success 'sanity check (x)' '
+ verify_notes x commit5 &&
+ verify_no_fanout x
+'
+
+num=300
+
+cp expect_log_x expect_log_y
+
+test_expect_success 'Add a few hundred commits w/notes to trigger fanout (x -> y)' '
+ git update-ref refs/notes/y refs/notes/x &&
+ git config core.notesRef refs/notes/y &&
+ test_commit_bulk --start=6 --id=commit $((num - 5)) &&
+ i=0 &&
+ while test $i -lt $((num - 5))
+ do
+ git notes add -m "notes for commit$i" HEAD~$i || return 1
+ i=$((i + 1))
+ done &&
+ test "$(git rev-parse refs/notes/y)" != "$(git rev-parse refs/notes/x)" &&
+ # Expected number of commits and notes
+ test $(git rev-list HEAD | wc -l) = $num &&
+ test $(git notes list | wc -l) = $num &&
+ # 5 first notes unchanged
+ verify_notes y commit5
+'
+
+test_expect_success 'notes tree has fanout (y)' 'verify_fanout y'
+
+test_expect_success 'No-op merge (already included) (x => y)' '
+ git update-ref refs/notes/m refs/notes/y &&
+ git config core.notesRef refs/notes/m &&
+ git notes merge x &&
+ test "$(git rev-parse refs/notes/m)" = "$(git rev-parse refs/notes/y)"
+'
+
+test_expect_success 'Fast-forward merge (y => x)' '
+ git update-ref refs/notes/m refs/notes/x &&
+ git notes merge y &&
+ test "$(git rev-parse refs/notes/m)" = "$(git rev-parse refs/notes/y)"
+'
+
+cat <<EOF | sort >expect_notes_z
+$(test_oid hash03b) $commit_sha3
+$(test_oid hash02b) $commit_sha2
+$(test_oid hash01a) $commit_sha1
+EOF
+
+cat >expect_log_z <<EOF
+$commit_sha5 commit5
+
+$commit_sha4 commit4
+
+$commit_sha3 commit3
+notes for commit3
+
+appended notes for commit3
+
+$commit_sha2 commit2
+new notes for commit2
+
+$commit_sha1 commit1
+notes for commit1
+
+EOF
+
+test_expect_success 'change some of the initial 5 notes (x -> z)' '
+ git update-ref refs/notes/z refs/notes/x &&
+ git config core.notesRef refs/notes/z &&
+ git notes add -f -m "new notes for commit2" commit2 &&
+ git notes append -m "appended notes for commit3" commit3 &&
+ git notes remove commit4 &&
+ git notes remove commit5 &&
+ verify_notes z commit5
+'
+
+test_expect_success 'notes tree has no fanout (z)' 'verify_no_fanout z'
+
+cp expect_log_z expect_log_m
+
+test_expect_success 'successful merge without conflicts (y => z)' '
+ git update-ref refs/notes/m refs/notes/z &&
+ git config core.notesRef refs/notes/m &&
+ git notes merge y &&
+ verify_notes m commit5 &&
+ # x/y/z unchanged
+ verify_notes x commit5 &&
+ verify_notes y commit5 &&
+ verify_notes z commit5
+'
+
+test_expect_success 'notes tree still has fanout after merge (m)' 'verify_fanout m'
+
+cat >expect_log_w <<EOF
+$commit_sha5 commit5
+
+$commit_sha4 commit4
+other notes for commit4
+
+$commit_sha3 commit3
+other notes for commit3
+
+$commit_sha2 commit2
+notes for commit2
+
+$commit_sha1 commit1
+other notes for commit1
+
+EOF
+
+test_expect_success 'introduce conflicting changes (y -> w)' '
+ git update-ref refs/notes/w refs/notes/y &&
+ git config core.notesRef refs/notes/w &&
+ git notes add -f -m "other notes for commit1" commit1 &&
+ git notes add -f -m "other notes for commit3" commit3 &&
+ git notes add -f -m "other notes for commit4" commit4 &&
+ git notes remove commit5 &&
+ verify_notes w commit5
+'
+
+cat >expect_log_m <<EOF
+$commit_sha5 commit5
+
+$commit_sha4 commit4
+other notes for commit4
+
+$commit_sha3 commit3
+other notes for commit3
+
+$commit_sha2 commit2
+new notes for commit2
+
+$commit_sha1 commit1
+other notes for commit1
+
+EOF
+
+test_expect_success 'successful merge using "ours" strategy (z => w)' '
+ git update-ref refs/notes/m refs/notes/w &&
+ git config core.notesRef refs/notes/m &&
+ git notes merge -s ours z &&
+ verify_notes m commit5 &&
+ # w/x/y/z unchanged
+ verify_notes w commit5 &&
+ verify_notes x commit5 &&
+ verify_notes y commit5 &&
+ verify_notes z commit5
+'
+
+test_expect_success 'notes tree still has fanout after merge (m)' 'verify_fanout m'
+
+cat >expect_log_m <<EOF
+$commit_sha5 commit5
+
+$commit_sha4 commit4
+
+$commit_sha3 commit3
+notes for commit3
+
+appended notes for commit3
+
+$commit_sha2 commit2
+new notes for commit2
+
+$commit_sha1 commit1
+other notes for commit1
+
+EOF
+
+test_expect_success 'successful merge using "theirs" strategy (z => w)' '
+ git update-ref refs/notes/m refs/notes/w &&
+ git notes merge -s theirs z &&
+ verify_notes m commit5 &&
+ # w/x/y/z unchanged
+ verify_notes w commit5 &&
+ verify_notes x commit5 &&
+ verify_notes y commit5 &&
+ verify_notes z commit5
+'
+
+test_expect_success 'notes tree still has fanout after merge (m)' 'verify_fanout m'
+
+cat >expect_log_m <<EOF
+$commit_sha5 commit5
+
+$commit_sha4 commit4
+other notes for commit4
+
+$commit_sha3 commit3
+other notes for commit3
+
+notes for commit3
+
+appended notes for commit3
+
+$commit_sha2 commit2
+new notes for commit2
+
+$commit_sha1 commit1
+other notes for commit1
+
+EOF
+
+test_expect_success 'successful merge using "union" strategy (z => w)' '
+ git update-ref refs/notes/m refs/notes/w &&
+ git notes merge -s union z &&
+ verify_notes m commit5 &&
+ # w/x/y/z unchanged
+ verify_notes w commit5 &&
+ verify_notes x commit5 &&
+ verify_notes y commit5 &&
+ verify_notes z commit5
+'
+
+test_expect_success 'notes tree still has fanout after merge (m)' 'verify_fanout m'
+
+cat >expect_log_m <<EOF
+$commit_sha5 commit5
+
+$commit_sha4 commit4
+other notes for commit4
+
+$commit_sha3 commit3
+appended notes for commit3
+notes for commit3
+other notes for commit3
+
+$commit_sha2 commit2
+new notes for commit2
+
+$commit_sha1 commit1
+other notes for commit1
+
+EOF
+
+test_expect_success 'successful merge using "cat_sort_uniq" strategy (z => w)' '
+ git update-ref refs/notes/m refs/notes/w &&
+ git notes merge -s cat_sort_uniq z &&
+ verify_notes m commit5 &&
+ # w/x/y/z unchanged
+ verify_notes w commit5 &&
+ verify_notes x commit5 &&
+ verify_notes y commit5 &&
+ verify_notes z commit5
+'
+
+test_expect_success 'notes tree still has fanout after merge (m)' 'verify_fanout m'
+
+# We're merging z into w. Here are the conflicts we expect:
+#
+# commit | x -> w | x -> z | conflict?
+# -------|-----------|-----------|----------
+# 1 | changed | unchanged | no, use w
+# 2 | unchanged | changed | no, use z
+# 3 | changed | changed | yes (w, then z in conflict markers)
+# 4 | changed | deleted | yes (w)
+# 5 | deleted | deleted | no, deleted
+
+test_expect_success 'fails to merge using "manual" strategy (z => w)' '
+ git update-ref refs/notes/m refs/notes/w &&
+ test_must_fail git notes merge z
+'
+
+test_expect_success 'notes tree still has fanout after merge (m)' 'verify_fanout m'
+
+cat <<EOF | sort >expect_conflicts
+$commit_sha3
+$commit_sha4
+EOF
+
+cat >expect_conflict_$commit_sha3 <<EOF
+<<<<<<< refs/notes/m
+other notes for commit3
+=======
+notes for commit3
+
+appended notes for commit3
+>>>>>>> refs/notes/z
+EOF
+
+cat >expect_conflict_$commit_sha4 <<EOF
+other notes for commit4
+EOF
+
+test_expect_success 'verify conflict entries (with no fanout)' '
+ ls .git/NOTES_MERGE_WORKTREE >output_conflicts &&
+ test_cmp expect_conflicts output_conflicts &&
+ ( for f in $(cat expect_conflicts); do
+ test_cmp "expect_conflict_$f" ".git/NOTES_MERGE_WORKTREE/$f" ||
+ exit 1
+ done ) &&
+ # Verify that current notes tree (pre-merge) has not changed (m == w)
+ test "$(git rev-parse refs/notes/m)" = "$(git rev-parse refs/notes/w)"
+'
+
+cat >expect_log_m <<EOF
+$commit_sha5 commit5
+
+$commit_sha4 commit4
+other notes for commit4
+
+$commit_sha3 commit3
+other notes for commit3
+
+appended notes for commit3
+
+$commit_sha2 commit2
+new notes for commit2
+
+$commit_sha1 commit1
+other notes for commit1
+
+EOF
+
+test_expect_success 'resolve and finalize merge (z => w)' '
+ cat >.git/NOTES_MERGE_WORKTREE/$commit_sha3 <<EOF &&
+other notes for commit3
+
+appended notes for commit3
+EOF
+ git notes merge --commit &&
+ verify_notes m commit5 &&
+ # w/x/y/z unchanged
+ verify_notes w commit5 &&
+ verify_notes x commit5 &&
+ verify_notes y commit5 &&
+ verify_notes z commit5
+'
+
+test_expect_success 'notes tree still has fanout after merge (m)' 'verify_fanout m'
+
+test_done
diff --git a/t/t3320-notes-merge-worktrees.sh b/t/t3320-notes-merge-worktrees.sh
new file mode 100755
index 0000000..0fd3328
--- /dev/null
+++ b/t/t3320-notes-merge-worktrees.sh
@@ -0,0 +1,78 @@
+#!/bin/sh
+#
+# Copyright (c) 2015 Twitter, Inc
+#
+
+test_description='Test merging of notes trees in multiple worktrees'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup commit' '
+ test_commit tantrum
+'
+
+commit_tantrum=$(git rev-parse tantrum^{commit})
+
+test_expect_success 'setup notes ref (x)' '
+ git config core.notesRef refs/notes/x &&
+ git notes add -m "x notes on tantrum" tantrum
+'
+
+test_expect_success 'setup local branch (y)' '
+ git update-ref refs/notes/y refs/notes/x &&
+ git config core.notesRef refs/notes/y &&
+ git notes remove tantrum
+'
+
+test_expect_success 'setup remote branch (z)' '
+ git update-ref refs/notes/z refs/notes/x &&
+ git config core.notesRef refs/notes/z &&
+ git notes add -f -m "conflicting notes on tantrum" tantrum
+'
+
+test_expect_success 'modify notes ref ourselves (x)' '
+ git config core.notesRef refs/notes/x &&
+ git notes add -f -m "more conflicting notes on tantrum" tantrum
+'
+
+test_expect_success 'create some new worktrees' '
+ git worktree add -b newbranch worktree main &&
+ git worktree add -b newbranch2 worktree2 main
+'
+
+test_expect_success 'merge z into y fails and sets NOTES_MERGE_REF' '
+ git config core.notesRef refs/notes/y &&
+ test_must_fail git notes merge z &&
+ echo "refs/notes/y" >expect &&
+ git symbolic-ref NOTES_MERGE_REF >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'merge z into y while mid-merge in another workdir fails' '
+ (
+ cd worktree &&
+ git config core.notesRef refs/notes/y &&
+ test_must_fail git notes merge z 2>err &&
+ test_grep "a notes merge into refs/notes/y is already in-progress at" err
+ ) &&
+ test_must_fail git -C worktree symbolic-ref NOTES_MERGE_REF
+'
+
+test_expect_success 'merge z into x while mid-merge on y succeeds' '
+ (
+ cd worktree2 &&
+ git config core.notesRef refs/notes/x &&
+ test_must_fail git notes merge z >out 2>&1 &&
+ test_grep "Automatic notes merge failed" out &&
+ grep -v "A notes merge into refs/notes/x is already in-progress in" out
+ ) &&
+ echo "refs/notes/x" >expect &&
+ git -C worktree2 symbolic-ref NOTES_MERGE_REF >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t3321-notes-stripspace.sh b/t/t3321-notes-stripspace.sh
new file mode 100755
index 0000000..088a852
--- /dev/null
+++ b/t/t3321-notes-stripspace.sh
@@ -0,0 +1,578 @@
+#!/bin/sh
+#
+# Copyright (c) 2023 Teng Long
+#
+
+test_description='Test commit notes with stripspace behavior'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+MULTI_LF="$LF$LF$LF"
+write_script fake_editor <<\EOF
+echo "$MSG" >"$1"
+echo "$MSG" >&2
+EOF
+GIT_EDITOR=./fake_editor
+export GIT_EDITOR
+
+test_expect_success 'setup the commit' '
+ test_commit 1st
+'
+
+test_expect_success 'add note by editor' '
+ test_when_finished "git notes remove" &&
+ cat >expect <<-EOF &&
+ first-line
+
+ second-line
+ EOF
+
+ MSG="${LF}first-line${MULTI_LF}second-line${LF}" git notes add &&
+ git notes show >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'add note by specifying single "-m", "--stripspace" is the default behavior' '
+ test_when_finished "git notes remove" &&
+ cat >expect <<-EOF &&
+ first-line
+
+ second-line
+ EOF
+
+ git notes add -m "${LF}first-line${MULTI_LF}second-line${LF}" &&
+ git notes show >actual &&
+ test_cmp expect actual &&
+ git notes remove &&
+ git notes add --stripspace -m "${LF}first-line${MULTI_LF}second-line${LF}" &&
+ git notes show >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'add note by specifying single "-m" and "--no-stripspace" ' '
+ test_when_finished "git notes remove" &&
+ cat >expect <<-EOF &&
+ ${LF}first-line${MULTI_LF}second-line
+ EOF
+
+ git notes add --no-stripspace \
+ -m "${LF}first-line${MULTI_LF}second-line${LF}" &&
+ git notes show >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'add note by specifying multiple "-m", "--stripspace" is the default behavior' '
+ test_when_finished "git notes remove" &&
+ cat >expect <<-EOF &&
+ first-line
+
+ second-line
+ EOF
+
+ git notes add -m "${LF}" \
+ -m "first-line" \
+ -m "${MULTI_LF}" \
+ -m "second-line" \
+ -m "${LF}" &&
+ git notes show >actual &&
+ test_cmp expect actual &&
+ git notes remove &&
+ git notes add --stripspace -m "${LF}" \
+ -m "first-line" \
+ -m "${MULTI_LF}" \
+ -m "second-line" \
+ -m "${LF}" &&
+ git notes show >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'add notes by specifying multiple "-m" and "--no-stripspace"' '
+ test_when_finished "git notes remove" &&
+ cat >expect <<-EOF &&
+ ${LF}
+ first-line
+ ${MULTI_LF}
+ second-line${LF}
+ EOF
+
+ git notes add --no-stripspace \
+ -m "${LF}" \
+ -m "first-line" \
+ -m "${MULTI_LF}" \
+ -m "second-line" \
+ -m "${LF}" &&
+ git notes show >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'add note by specifying single "-F", "--stripspace" is the default behavior' '
+ test_when_finished "git notes remove" &&
+ cat >expect <<-EOF &&
+ first-line
+
+ second-line
+ EOF
+
+ cat >note-file <<-EOF &&
+ ${LF}
+ first-line
+ ${MULTI_LF}
+ second-line
+ ${LF}
+ EOF
+
+ git notes add -F note-file &&
+ git notes show >actual &&
+ test_cmp expect actual &&
+ git notes remove &&
+ git notes add --stripspace -F note-file &&
+ git notes show >actual
+'
+
+test_expect_success 'add note by specifying single "-F" and "--no-stripspace"' '
+ test_when_finished "git notes remove" &&
+ cat >expect <<-EOF &&
+ ${LF}
+ first-line
+ ${MULTI_LF}
+ second-line
+ ${LF}
+ EOF
+
+ cat >note-file <<-EOF &&
+ ${LF}
+ first-line
+ ${MULTI_LF}
+ second-line
+ ${LF}
+ EOF
+
+ git notes add --no-stripspace -F note-file &&
+ git notes show >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'add note by specifying multiple "-F", "--stripspace" is the default behavior' '
+ test_when_finished "git notes remove" &&
+ cat >expect <<-EOF &&
+ file-1-first-line
+
+ file-1-second-line
+
+ file-2-first-line
+
+ file-2-second-line
+ EOF
+
+ cat >note-file-1 <<-EOF &&
+ ${LF}
+ file-1-first-line
+ ${MULTI_LF}
+ file-1-second-line
+ ${LF}
+ EOF
+
+ cat >note-file-2 <<-EOF &&
+ ${LF}
+ file-2-first-line
+ ${MULTI_LF}
+ file-2-second-line
+ ${LF}
+ EOF
+
+ git notes add -F note-file-1 -F note-file-2 &&
+ git notes show >actual &&
+ test_cmp expect actual &&
+ git notes remove &&
+ git notes add --stripspace -F note-file-1 -F note-file-2 &&
+ git notes show >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'add note by specifying multiple "-F" with "--no-stripspace"' '
+ test_when_finished "git notes remove" &&
+ cat >expect <<-EOF &&
+ ${LF}
+ file-1-first-line
+ ${MULTI_LF}
+ file-1-second-line
+ ${LF}
+
+ ${LF}
+ file-2-first-line
+ ${MULTI_LF}
+ file-2-second-line
+ ${LF}
+ EOF
+
+ cat >note-file-1 <<-EOF &&
+ ${LF}
+ file-1-first-line
+ ${MULTI_LF}
+ file-1-second-line
+ ${LF}
+ EOF
+
+ cat >note-file-2 <<-EOF &&
+ ${LF}
+ file-2-first-line
+ ${MULTI_LF}
+ file-2-second-line
+ ${LF}
+ EOF
+
+ git notes add --no-stripspace -F note-file-1 -F note-file-2 &&
+ git notes show >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'append note by editor' '
+ test_when_finished "git notes remove" &&
+ cat >expect <<-EOF &&
+ first-line
+
+ second-line
+ EOF
+
+ git notes add -m "first-line" &&
+ MSG="${MULTI_LF}second-line${LF}" git notes append &&
+ git notes show >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'append note by specifying single "-m"' '
+ test_when_finished "git notes remove" &&
+ cat >expect <<-EOF &&
+ first-line
+
+ second-line
+ EOF
+
+ git notes add -m "${LF}first-line" &&
+ git notes append -m "${MULTI_LF}second-line${LF}" &&
+ git notes show >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'append note by specifying multiple "-m"' '
+ test_when_finished "git notes remove" &&
+ cat >expect <<-EOF &&
+ first-line
+
+ second-line
+ EOF
+
+ git notes add -m "${LF}first-line" &&
+ git notes append -m "${MULTI_LF}" \
+ -m "second-line" \
+ -m "${LF}" &&
+ git notes show >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'add note by specifying single "-F"' '
+ test_when_finished "git notes remove" &&
+ cat >expect <<-EOF &&
+ first-line
+
+ second-line
+ EOF
+
+ cat >note-file <<-EOF &&
+ ${LF}
+ first-line
+ ${MULTI_LF}
+ second-line
+ ${LF}
+ EOF
+
+ git notes add -F note-file &&
+ git notes show >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'add notes by specifying multiple "-F"' '
+ test_when_finished "git notes remove" &&
+ cat >expect <<-EOF &&
+ file-1-first-line
+
+ file-1-second-line
+
+ file-2-first-line
+
+ file-2-second-line
+ EOF
+
+ cat >note-file-1 <<-EOF &&
+ ${LF}
+ file-1-first-line
+ ${MULTI_LF}
+ file-1-second-line
+ ${LF}
+ EOF
+
+ cat >note-file-2 <<-EOF &&
+ ${LF}
+ file-2-first-line
+ ${MULTI_LF}
+ file-2-second-line
+ ${LF}
+ EOF
+
+ git notes add -F note-file-1 -F note-file-2 &&
+ git notes show >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'append note by specifying single "-F"' '
+ test_when_finished "git notes remove" &&
+ cat >expect <<-EOF &&
+ initial-line
+
+ first-line
+
+ second-line
+ EOF
+
+ cat >note-file <<-EOF &&
+ ${LF}
+ first-line
+ ${MULTI_LF}
+ second-line
+ ${LF}
+ EOF
+
+ git notes add -m "initial-line" &&
+ git notes append -F note-file &&
+ git notes show >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'append notes by specifying multiple "-F"' '
+ test_when_finished "git notes remove" &&
+ cat >expect <<-EOF &&
+ initial-line
+
+ file-1-first-line
+
+ file-1-second-line
+
+ file-2-first-line
+
+ file-2-second-line
+ EOF
+
+ cat >note-file-1 <<-EOF &&
+ ${LF}
+ file-1-first-line
+ ${MULTI_LF}
+ file-1-second-line
+ ${LF}
+ EOF
+
+ cat >note-file-2 <<-EOF &&
+ ${LF}
+ file-2-first-line
+ ${MULTI_LF}
+ file-2-second-line
+ ${LF}
+ EOF
+
+ git notes add -m "initial-line" &&
+ git notes append -F note-file-1 -F note-file-2 &&
+ git notes show >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'append note by specifying multiple "-F" with "--no-stripspace"' '
+ test_when_finished "git notes remove" &&
+ cat >expect <<-EOF &&
+ initial-line
+ ${LF}${LF}
+ file-1-first-line
+ ${MULTI_LF}
+ file-1-second-line
+ ${LF}
+
+ ${LF}
+ file-2-first-line
+ ${MULTI_LF}
+ file-2-second-line
+ ${LF}
+ EOF
+
+ cat >note-file-1 <<-EOF &&
+ ${LF}
+ file-1-first-line
+ ${MULTI_LF}
+ file-1-second-line
+ ${LF}
+ EOF
+
+ cat >note-file-2 <<-EOF &&
+ ${LF}
+ file-2-first-line
+ ${MULTI_LF}
+ file-2-second-line
+ ${LF}
+ EOF
+
+ git notes add -m "initial-line" &&
+ git notes append --no-stripspace -F note-file-1 -F note-file-2 &&
+ git notes show >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'add notes with empty messages' '
+ rev=$(git rev-parse HEAD) &&
+ git notes add -m "${LF}" \
+ -m "${MULTI_LF}" \
+ -m "${LF}" >actual 2>&1 &&
+ test_grep "Removing note for object" actual
+'
+
+test_expect_success 'add note by specifying "-C", "--no-stripspace" is the default behavior' '
+ test_when_finished "git notes remove" &&
+ cat >expect <<-EOF &&
+ ${LF}
+ first-line
+ ${MULTI_LF}
+ second-line
+ ${LF}
+ EOF
+
+ cat expect | git hash-object -w --stdin >blob &&
+ git notes add -C $(cat blob) &&
+ git notes show >actual &&
+ test_cmp expect actual &&
+ git notes remove &&
+ git notes add --no-stripspace -C $(cat blob) &&
+ git notes show >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'reuse note by specifying "-C" and "--stripspace"' '
+ test_when_finished "git notes remove" &&
+ cat >data <<-EOF &&
+ ${LF}
+ first-line
+ ${MULTI_LF}
+ second-line
+ ${LF}
+ EOF
+
+ cat >expect <<-EOF &&
+ first-line
+
+ second-line
+ EOF
+
+ cat data | git hash-object -w --stdin >blob &&
+ git notes add --stripspace -C $(cat blob) &&
+ git notes show >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'reuse with "-C" and add note with "-m", "-m" will stripspace all together' '
+ test_when_finished "git notes remove" &&
+ cat >data <<-EOF &&
+ ${LF}
+ first-line
+ ${MULTI_LF}
+ second-line
+ ${LF}
+ EOF
+
+ cat >expect <<-EOF &&
+ first-line
+
+ second-line
+
+ third-line
+ EOF
+
+ cat data | git hash-object -w --stdin >blob &&
+ git notes add -C $(cat blob) -m "third-line" &&
+ git notes show >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'add note with "-m" and reuse note with "-C", "-C" will not stripspace all together' '
+ test_when_finished "git notes remove" &&
+ cat >data <<-EOF &&
+
+ second-line
+ EOF
+
+ cat >expect <<-EOF &&
+ first-line
+ ${LF}
+ second-line
+ EOF
+
+ cat data | git hash-object -w --stdin >blob &&
+ git notes add -m "first-line" -C $(cat blob) &&
+ git notes show >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'add note by specifying "-c", "--stripspace" is the default behavior' '
+ test_when_finished "git notes remove" &&
+ cat >expect <<-EOF &&
+ first-line
+
+ second-line
+ EOF
+
+ echo "initial-line" | git hash-object -w --stdin >blob &&
+ MSG="${LF}first-line${MULTI_LF}second-line${LF}" git notes add -c $(cat blob) &&
+ git notes show >actual &&
+ test_cmp expect actual &&
+ git notes remove &&
+ MSG="${LF}first-line${MULTI_LF}second-line${LF}" git notes add --stripspace -c $(cat blob) &&
+ git notes show >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'add note by specifying "-c" with "--no-stripspace"' '
+ test_when_finished "git notes remove" &&
+ cat >expect <<-EOF &&
+ ${LF}first-line${MULTI_LF}second-line${LF}
+ EOF
+
+ echo "initial-line" | git hash-object -w --stdin >blob &&
+ MSG="${LF}first-line${MULTI_LF}second-line${LF}" git notes add --no-stripspace -c $(cat blob) &&
+ git notes show >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'edit note by specifying "-c", "--stripspace" is the default behavior' '
+ test_when_finished "git notes remove" &&
+ cat >expect <<-EOF &&
+ first-line
+
+ second-line
+ EOF
+
+ MSG="${LF}first-line${MULTI_LF}second-line${LF}" git notes edit &&
+ git notes show >actual &&
+ test_cmp expect actual &&
+ git notes remove &&
+ MSG="${LF}first-line${MULTI_LF}second-line${LF}" git notes edit --stripspace &&
+ git notes show >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'edit note by specifying "-c" with "--no-stripspace"' '
+ test_when_finished "git notes remove" &&
+ cat >expect <<-EOF &&
+ ${LF}first-line${MULTI_LF}second-line${LF}
+ EOF
+
+ MSG="${LF}first-line${MULTI_LF}second-line${LF}" git notes add --no-stripspace &&
+ git notes show >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t3400-rebase.sh b/t/t3400-rebase.sh
new file mode 100755
index 0000000..24a539c
--- /dev/null
+++ b/t/t3400-rebase.sh
@@ -0,0 +1,458 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Amos Waterland
+#
+
+test_description='git rebase assorted tests
+
+This test runs git rebase and checks that the author information is not lost
+among other things.
+'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+GIT_AUTHOR_NAME=author@name
+GIT_AUTHOR_EMAIL=bogus@email@address
+export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
+
+test_expect_success 'prepare repository with topic branches' '
+ test_commit "Add A." A First First &&
+ git checkout -b force-3way &&
+ echo Dummy >Y &&
+ git update-index --add Y &&
+ git commit -m "Add Y." &&
+ git checkout -b filemove &&
+ git reset --soft main &&
+ mkdir D &&
+ git mv A D/A &&
+ git commit -m "Move A." &&
+ git checkout -b my-topic-branch main &&
+ test_commit "Add B." B Second Second &&
+ git checkout -f main &&
+ echo Third >>A &&
+ git update-index A &&
+ git commit -m "Modify A." &&
+ git checkout -b side my-topic-branch &&
+ echo Side >>C &&
+ git add C &&
+ git commit -m "Add C" &&
+ git checkout -f my-topic-branch &&
+ git tag topic
+'
+
+test_expect_success 'rebase on dirty worktree' '
+ echo dirty >>A &&
+ test_must_fail git rebase main
+'
+
+test_expect_success 'rebase on dirty cache' '
+ git add A &&
+ test_must_fail git rebase main
+'
+
+test_expect_success 'rebase against main' '
+ git reset --hard HEAD &&
+ git rebase main
+'
+
+test_expect_success 'rebase sets ORIG_HEAD to pre-rebase state' '
+ git checkout -b orig-head topic &&
+ pre="$(git rev-parse --verify HEAD)" &&
+ git rebase main &&
+ test_cmp_rev "$pre" ORIG_HEAD &&
+ test_cmp_rev ! "$pre" HEAD
+'
+
+test_expect_success 'rebase, with <onto> and <upstream> specified as :/quuxery' '
+ test_when_finished "git branch -D torebase" &&
+ git checkout -b torebase my-topic-branch^ &&
+ upstream=$(git rev-parse ":/Add B") &&
+ onto=$(git rev-parse ":/Add A") &&
+ git rebase --onto $onto $upstream &&
+ git reset --hard my-topic-branch^ &&
+ git rebase --onto ":/Add A" ":/Add B" &&
+ git checkout my-topic-branch
+'
+
+test_expect_success 'the rebase operation should not have destroyed author information' '
+ ! (git log | grep "Author:" | grep "<>")
+'
+
+test_expect_success 'the rebase operation should not have destroyed author information (2)' "
+ git log -1 |
+ grep 'Author: $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>'
+"
+
+test_expect_success 'HEAD was detached during rebase' '
+ test $(git rev-parse HEAD@{1}) != $(git rev-parse my-topic-branch@{1})
+'
+
+test_expect_success 'rebase from ambiguous branch name' '
+ git checkout -b topic side &&
+ git rebase main
+'
+
+test_expect_success 'rebase off of the previous branch using "-"' '
+ git checkout main &&
+ git checkout HEAD^ &&
+ git rebase @{-1} >expect.messages &&
+ git merge-base main HEAD >expect.forkpoint &&
+
+ git checkout main &&
+ git checkout HEAD^ &&
+ git rebase - >actual.messages &&
+ git merge-base main HEAD >actual.forkpoint &&
+
+ test_cmp expect.forkpoint actual.forkpoint &&
+ # the next one is dubious---we may want to say "-",
+ # instead of @{-1}, in the message
+ test_cmp expect.messages actual.messages
+'
+
+test_expect_success 'rebase a single mode change' '
+ git checkout main &&
+ git branch -D topic &&
+ echo 1 >X &&
+ git add X &&
+ test_tick &&
+ git commit -m prepare &&
+ git checkout -b modechange HEAD^ &&
+ echo 1 >X &&
+ git add X &&
+ test_chmod +x A &&
+ test_tick &&
+ git commit -m modechange &&
+ GIT_TRACE=1 git rebase main
+'
+
+test_expect_success 'rebase is not broken by diff.renames' '
+ test_config diff.renames copies &&
+ git checkout filemove &&
+ GIT_TRACE=1 git rebase force-3way
+'
+
+test_expect_success 'setup: recover' '
+ test_might_fail git rebase --abort &&
+ git reset --hard &&
+ git checkout modechange
+'
+
+test_expect_success 'Show verbose error when HEAD could not be detached' '
+ >B &&
+ test_when_finished "rm -f B" &&
+ test_must_fail git rebase topic 2>output.err >output.out &&
+ test_grep "The following untracked working tree files would be overwritten by checkout:" output.err &&
+ test_grep B output.err
+'
+
+test_expect_success 'fail when upstream arg is missing and not on branch' '
+ git checkout topic &&
+ test_must_fail git rebase
+'
+
+test_expect_success 'fail when upstream arg is missing and not configured' '
+ git checkout -b no-config topic &&
+ test_must_fail git rebase
+'
+
+test_expect_success 'rebase works with format.useAutoBase' '
+ test_config format.useAutoBase true &&
+ git checkout topic &&
+ git rebase main
+'
+
+test_expect_success 'default to common base in @{upstream}s reflog if no upstream arg (--merge)' '
+ git checkout -b default-base main &&
+ git checkout -b default topic &&
+ git config branch.default.remote . &&
+ git config branch.default.merge refs/heads/default-base &&
+ git rebase --merge &&
+ git rev-parse --verify default-base >expect &&
+ git rev-parse default~1 >actual &&
+ test_cmp expect actual &&
+ git checkout default-base &&
+ git reset --hard HEAD^ &&
+ git checkout default &&
+ git rebase --merge &&
+ git rev-parse --verify default-base >expect &&
+ git rev-parse default~1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'default to common base in @{upstream}s reflog if no upstream arg (--apply)' '
+ git checkout -B default-base main &&
+ git checkout -B default topic &&
+ git config branch.default.remote . &&
+ git config branch.default.merge refs/heads/default-base &&
+ git rebase --apply &&
+ git rev-parse --verify default-base >expect &&
+ git rev-parse default~1 >actual &&
+ test_cmp expect actual &&
+ git checkout default-base &&
+ git reset --hard HEAD^ &&
+ git checkout default &&
+ git rebase --apply &&
+ git rev-parse --verify default-base >expect &&
+ git rev-parse default~1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'cherry-picked commits and fork-point work together' '
+ git checkout default-base &&
+ echo Amended >A &&
+ git commit -a --no-edit --amend &&
+ test_commit B B &&
+ test_commit new_B B "New B" &&
+ test_commit C C &&
+ git checkout default &&
+ git reset --hard default-base@{4} &&
+ test_commit D D &&
+ git cherry-pick -2 default-base^ &&
+ test_commit final_B B "Final B" &&
+ git rebase &&
+ echo Amended >expect &&
+ test_cmp expect A &&
+ echo "Final B" >expect &&
+ test_cmp expect B &&
+ echo C >expect &&
+ test_cmp expect C &&
+ echo D >expect &&
+ test_cmp expect D
+'
+
+test_expect_success 'rebase --apply -q is quiet' '
+ git checkout -b quiet topic &&
+ git rebase --apply -q main >output.out 2>&1 &&
+ test_must_be_empty output.out
+'
+
+test_expect_success 'rebase --merge -q is quiet' '
+ git checkout -B quiet topic &&
+ git rebase --merge -q main >output.out 2>&1 &&
+ test_must_be_empty output.out
+'
+
+test_expect_success 'Rebase a commit that sprinkles CRs in' '
+ (
+ echo "One" &&
+ echo "TwoQ" &&
+ echo "Three" &&
+ echo "FQur" &&
+ echo "Five"
+ ) | q_to_cr >CR &&
+ git add CR &&
+ test_tick &&
+ git commit -a -m "A file with a line with CR" &&
+ git tag file-with-cr &&
+ git checkout HEAD^0 &&
+ git rebase --onto HEAD^^ HEAD^ &&
+ git diff --exit-code file-with-cr:CR HEAD:CR
+'
+
+test_expect_success 'rebase can copy notes' '
+ git config notes.rewrite.rebase true &&
+ git config notes.rewriteRef "refs/notes/*" &&
+ test_commit n1 &&
+ test_commit n2 &&
+ test_commit n3 &&
+ git notes add -m"a note" n3 &&
+ git rebase --onto n1 n2 &&
+ test "a note" = "$(git notes show HEAD)"
+'
+
+test_expect_success 'rebase -m can copy notes' '
+ git reset --hard n3 &&
+ git rebase -m --onto n1 n2 &&
+ test "a note" = "$(git notes show HEAD)"
+'
+
+test_expect_success 'rebase commit with an ancient timestamp' '
+ git reset --hard &&
+
+ >old.one && git add old.one && test_tick &&
+ git commit --date="@12345 +0400" -m "Old one" &&
+ >old.two && git add old.two && test_tick &&
+ git commit --date="@23456 +0500" -m "Old two" &&
+ >old.three && git add old.three && test_tick &&
+ git commit --date="@34567 +0600" -m "Old three" &&
+
+ git cat-file commit HEAD^^ >actual &&
+ grep "author .* 12345 +0400$" actual &&
+ git cat-file commit HEAD^ >actual &&
+ grep "author .* 23456 +0500$" actual &&
+ git cat-file commit HEAD >actual &&
+ grep "author .* 34567 +0600$" actual &&
+
+ git rebase --onto HEAD^^ HEAD^ &&
+
+ git cat-file commit HEAD >actual &&
+ grep "author .* 34567 +0600$" actual
+'
+
+test_expect_success 'rebase with "From " line in commit message' '
+ git checkout -b preserve-from main~1 &&
+ cat >From_.msg <<EOF &&
+Somebody embedded an mbox in a commit message
+
+This is from so-and-so:
+
+From a@b Mon Sep 17 00:00:00 2001
+From: John Doe <nobody@example.com>
+Date: Sat, 11 Nov 2017 00:00:00 +0000
+Subject: not this message
+
+something
+EOF
+ >From_ &&
+ git add From_ &&
+ git commit -F From_.msg &&
+ git rebase main &&
+ git log -1 --pretty=format:%B >out &&
+ test_cmp From_.msg out
+'
+
+test_expect_success 'rebase --apply and --show-current-patch' '
+ test_create_repo conflict-apply &&
+ (
+ cd conflict-apply &&
+ test_commit init &&
+ echo one >>init.t &&
+ git commit -a -m one &&
+ echo two >>init.t &&
+ git commit -a -m two &&
+ git tag two &&
+ test_must_fail git rebase --apply -f --onto init HEAD^ &&
+ GIT_TRACE=1 git rebase --show-current-patch >/dev/null 2>stderr &&
+ grep "show.*$(git rev-parse two)" stderr
+ )
+'
+
+test_expect_success 'rebase --apply and .gitattributes' '
+ test_create_repo attributes &&
+ (
+ cd attributes &&
+ test_commit init &&
+ git config filter.test.clean "sed -e '\''s/smudged/clean/g'\''" &&
+ git config filter.test.smudge "sed -e '\''s/clean/smudged/g'\''" &&
+
+ test_commit second &&
+ git checkout -b test HEAD^ &&
+
+ echo "*.txt filter=test" >.gitattributes &&
+ git add .gitattributes &&
+ test_commit third &&
+
+ echo "This text is smudged." >a.txt &&
+ git add a.txt &&
+ test_commit fourth &&
+
+ git checkout -b removal HEAD^ &&
+ git rm .gitattributes &&
+ git add -u &&
+ test_commit fifth &&
+ git cherry-pick test &&
+
+ git checkout test &&
+ git rebase main &&
+ grep "smudged" a.txt &&
+
+ git checkout removal &&
+ git reset --hard &&
+ git rebase main &&
+ grep "clean" a.txt
+ )
+'
+
+test_expect_success 'rebase--merge.sh and --show-current-patch' '
+ test_create_repo conflict-merge &&
+ (
+ cd conflict-merge &&
+ test_commit init &&
+ echo one >>init.t &&
+ git commit -a -m one &&
+ echo two >>init.t &&
+ git commit -a -m two &&
+ git tag two &&
+ test_must_fail git rebase --merge --onto init HEAD^ &&
+ git rebase --show-current-patch >actual.patch &&
+ GIT_TRACE=1 git rebase --show-current-patch >/dev/null 2>stderr &&
+ grep "show.*REBASE_HEAD" stderr &&
+ test "$(git rev-parse REBASE_HEAD)" = "$(git rev-parse two)"
+ )
+'
+
+test_expect_success 'switch to branch checked out here' '
+ git checkout main &&
+ git rebase main main
+'
+
+test_expect_success 'switch to branch checked out elsewhere fails' '
+ test_when_finished "
+ git worktree remove wt1 &&
+ git worktree remove wt2 &&
+ git branch -d shared
+ " &&
+ git worktree add wt1 -b shared &&
+ git worktree add wt2 -f shared &&
+ # we test in both worktrees to ensure that works
+ # as expected with "first" and "next" worktrees
+ test_must_fail git -C wt1 rebase shared shared &&
+ test_must_fail git -C wt2 rebase shared shared
+'
+
+test_expect_success 'switch to branch not checked out' '
+ git checkout main &&
+ git branch other &&
+ git rebase main other
+'
+
+test_expect_success 'switch to non-branch detaches HEAD' '
+ git checkout main &&
+ old_main=$(git rev-parse HEAD) &&
+ git rebase First Second^0 &&
+ test_cmp_rev HEAD Second &&
+ test_cmp_rev main $old_main &&
+ test_must_fail git symbolic-ref HEAD
+'
+
+test_expect_success 'refuse to switch to branch checked out elsewhere' '
+ git checkout main &&
+ git worktree add wt &&
+ test_must_fail git -C wt rebase main main 2>err &&
+ test_grep "already used by worktree at" err
+'
+
+test_expect_success MINGW,SYMLINKS_WINDOWS 'rebase when .git/logs is a symlink' '
+ git checkout main &&
+ mv .git/logs actual_logs &&
+ cmd //c "mklink /D .git\logs ..\actual_logs" &&
+ git rebase -f HEAD^ &&
+ test -L .git/logs &&
+ rm .git/logs &&
+ mv actual_logs .git/logs
+'
+
+test_expect_success 'rebase when inside worktree subdirectory' '
+ git init main-wt &&
+ (
+ cd main-wt &&
+ git commit --allow-empty -m "initial" &&
+ mkdir -p foo/bar &&
+ test_commit foo/bar/baz &&
+ mkdir -p a/b &&
+ test_commit a/b/c &&
+ # create another branch for our other worktree
+ git branch other &&
+ git worktree add ../other-wt other &&
+ cd ../other-wt &&
+ # create and cd into a subdirectory
+ mkdir -p random/dir &&
+ cd random/dir &&
+ # now do the rebase
+ git rebase --onto HEAD^^ HEAD^ # drops the HEAD^ commit
+ )
+'
+
+test_done
diff --git a/t/t3401-rebase-and-am-rename.sh b/t/t3401-rebase-and-am-rename.sh
new file mode 100755
index 0000000..f18bae9
--- /dev/null
+++ b/t/t3401-rebase-and-am-rename.sh
@@ -0,0 +1,213 @@
+#!/bin/sh
+
+test_description='git rebase + directory rename tests'
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-rebase.sh
+
+test_expect_success 'setup testcase where directory rename should be detected' '
+ test_create_repo dir-rename &&
+ (
+ cd dir-rename &&
+
+ mkdir x &&
+ test_seq 1 10 >x/a &&
+ test_seq 11 20 >x/b &&
+ test_seq 21 30 >x/c &&
+ test_write_lines a b c d e f g h i >l &&
+ git add x l &&
+ git commit -m "Initial" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git mv x y &&
+ git mv l letters &&
+ git commit -m "Rename x to y, l to letters" &&
+
+ git checkout B &&
+ echo j >>l &&
+ test_seq 31 40 >x/d &&
+ git add l x/d &&
+ git commit -m "Modify l, add x/d"
+ )
+'
+
+test_expect_success 'rebase --interactive: directory rename detected' '
+ (
+ cd dir-rename &&
+
+ git checkout B^0 &&
+
+ set_fake_editor &&
+ FAKE_LINES="1" git -c merge.directoryRenames=true rebase --interactive A &&
+
+ git ls-files -s >out &&
+ test_line_count = 5 out &&
+
+ test_path_is_file y/d &&
+ test_path_is_missing x/d
+ )
+'
+
+test_expect_failure 'rebase --apply: directory rename detected' '
+ (
+ cd dir-rename &&
+
+ git checkout B^0 &&
+
+ git -c merge.directoryRenames=true rebase --apply A &&
+
+ git ls-files -s >out &&
+ test_line_count = 5 out &&
+
+ test_path_is_file y/d &&
+ test_path_is_missing x/d
+ )
+'
+
+test_expect_success 'rebase --merge: directory rename detected' '
+ (
+ cd dir-rename &&
+
+ git checkout B^0 &&
+
+ git -c merge.directoryRenames=true rebase --merge A &&
+
+ git ls-files -s >out &&
+ test_line_count = 5 out &&
+
+ test_path_is_file y/d &&
+ test_path_is_missing x/d
+ )
+'
+
+test_expect_failure 'am: directory rename detected' '
+ (
+ cd dir-rename &&
+
+ git checkout A^0 &&
+
+ git format-patch -1 B &&
+
+ git -c merge.directoryRenames=true am --3way 0001*.patch &&
+
+ git ls-files -s >out &&
+ test_line_count = 5 out &&
+
+ test_path_is_file y/d &&
+ test_path_is_missing x/d
+ )
+'
+
+test_expect_success 'setup testcase where directory rename should NOT be detected' '
+ test_create_repo no-dir-rename &&
+ (
+ cd no-dir-rename &&
+
+ mkdir x &&
+ test_seq 1 10 >x/a &&
+ test_seq 11 20 >x/b &&
+ test_seq 21 30 >x/c &&
+ echo original >project_info &&
+ git add x project_info &&
+ git commit -m "Initial" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ echo v2 >project_info &&
+ git add project_info &&
+ git commit -m "Modify project_info" &&
+
+ git checkout B &&
+ mkdir y &&
+ git mv x/c y/c &&
+ echo v1 >project_info &&
+ git add project_info &&
+ git commit -m "Rename x/c to y/c, modify project_info"
+ )
+'
+
+test_expect_success 'rebase --interactive: NO directory rename' '
+ test_when_finished "git -C no-dir-rename rebase --abort" &&
+ (
+ cd no-dir-rename &&
+
+ git checkout B^0 &&
+
+ set_fake_editor &&
+ test_must_fail env FAKE_LINES="1" git rebase --interactive A &&
+
+ git ls-files -s >out &&
+ test_line_count = 6 out &&
+
+ test_path_is_file x/a &&
+ test_path_is_file x/b &&
+ test_path_is_missing x/c
+ )
+'
+
+test_expect_success 'rebase (am): NO directory rename' '
+ test_when_finished "git -C no-dir-rename rebase --abort" &&
+ (
+ cd no-dir-rename &&
+
+ git checkout B^0 &&
+
+ set_fake_editor &&
+ test_must_fail git rebase A &&
+
+ git ls-files -s >out &&
+ test_line_count = 6 out &&
+
+ test_path_is_file x/a &&
+ test_path_is_file x/b &&
+ test_path_is_missing x/c
+ )
+'
+
+test_expect_success 'rebase --merge: NO directory rename' '
+ test_when_finished "git -C no-dir-rename rebase --abort" &&
+ (
+ cd no-dir-rename &&
+
+ git checkout B^0 &&
+
+ set_fake_editor &&
+ test_must_fail git rebase --merge A &&
+
+ git ls-files -s >out &&
+ test_line_count = 6 out &&
+
+ test_path_is_file x/a &&
+ test_path_is_file x/b &&
+ test_path_is_missing x/c
+ )
+'
+
+test_expect_success 'am: NO directory rename' '
+ test_when_finished "git -C no-dir-rename am --abort" &&
+ (
+ cd no-dir-rename &&
+
+ git checkout A^0 &&
+
+ git format-patch -1 B &&
+
+ test_must_fail git am --3way 0001*.patch &&
+
+ git ls-files -s >out &&
+ test_line_count = 6 out &&
+
+ test_path_is_file x/a &&
+ test_path_is_file x/b &&
+ test_path_is_missing x/c
+ )
+'
+
+test_done
diff --git a/t/t3402-rebase-merge.sh b/t/t3402-rebase-merge.sh
new file mode 100755
index 0000000..5c67d07
--- /dev/null
+++ b/t/t3402-rebase-merge.sh
@@ -0,0 +1,225 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Junio C Hamano
+#
+
+test_description='git rebase --merge test'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+T="A quick brown fox
+jumps over the lazy dog."
+for i in 1 2 3 4 5 6 7 8 9 10
+do
+ echo "$i $T"
+done >original
+
+test_expect_success setup '
+ git add original &&
+ git commit -m"initial" &&
+ git branch side &&
+ echo "11 $T" >>original &&
+ git commit -a -m"main updates a bit." &&
+
+ echo "12 $T" >>original &&
+ git commit -a -m"main updates a bit more." &&
+
+ git checkout side &&
+ (echo "0 $T" && cat original) >renamed &&
+ git add renamed &&
+ git update-index --force-remove original &&
+ git commit -a -m"side renames and edits." &&
+
+ tr "[a-z]" "[A-Z]" <original >newfile &&
+ git add newfile &&
+ git commit -a -m"side edits further." &&
+ git branch second-side &&
+
+ tr "[a-m]" "[A-M]" <original >newfile &&
+ rm -f original &&
+ git commit -a -m"side edits once again." &&
+
+ git branch test-rebase side &&
+ git branch test-rebase-pick side &&
+ git branch test-reference-pick side &&
+ git branch test-conflicts side &&
+ git checkout -b test-merge side
+'
+
+test_expect_success 'reference merge' '
+ git merge -s recursive -m "reference merge" main
+'
+
+PRE_REBASE=$(git rev-parse test-rebase)
+test_expect_success rebase '
+ git checkout test-rebase &&
+ GIT_TRACE=1 git rebase --merge main
+'
+
+test_expect_success 'test-rebase@{1} is pre rebase' '
+ test $PRE_REBASE = $(git rev-parse test-rebase@{1})
+'
+
+test_expect_success 'merge and rebase should match' '
+ git diff-tree -r test-rebase test-merge >difference &&
+ if test -s difference
+ then
+ cat difference
+ false
+ else
+ echo happy
+ fi
+'
+
+test_expect_success 'rebase the other way' '
+ git reset --hard main &&
+ git rebase --merge side
+'
+
+test_expect_success 'rebase -Xtheirs' '
+ git checkout -b conflicting main~2 &&
+ echo "AB $T" >> original &&
+ git commit -mconflicting original &&
+ git rebase -Xtheirs main &&
+ grep AB original &&
+ ! grep 11 original
+'
+
+test_expect_success 'rebase -Xtheirs from orphan' '
+ git checkout --orphan orphan-conflicting main~2 &&
+ echo "AB $T" >> original &&
+ git commit -morphan-conflicting original &&
+ git rebase -Xtheirs main &&
+ grep AB original &&
+ ! grep 11 original
+'
+
+test_expect_success 'merge and rebase should match' '
+ git diff-tree -r test-rebase test-merge >difference &&
+ if test -s difference
+ then
+ cat difference
+ false
+ else
+ echo happy
+ fi
+'
+
+test_expect_success 'picking rebase' '
+ git reset --hard side &&
+ git rebase --merge --onto main side^^ &&
+ mb=$(git merge-base main HEAD) &&
+ if test "$mb" = "$(git rev-parse main)"
+ then
+ echo happy
+ else
+ git show-branch
+ false
+ fi &&
+ f=$(git diff-tree --name-only HEAD^ HEAD) &&
+ g=$(git diff-tree --name-only HEAD^^ HEAD^) &&
+ case "$f,$g" in
+ newfile,newfile)
+ echo happy ;;
+ *)
+ echo "$f"
+ echo "$g"
+ false
+ esac
+'
+
+test_expect_success 'rebase --skip works with two conflicts in a row' '
+ git checkout second-side &&
+ tr "[A-Z]" "[a-z]" <newfile >tmp &&
+ mv tmp newfile &&
+ git commit -a -m"edit conflicting with side" &&
+ tr "[d-f]" "[D-F]" <newfile >tmp &&
+ mv tmp newfile &&
+ git commit -a -m"another edit conflicting with side" &&
+ test_must_fail git rebase --merge test-conflicts &&
+ test_must_fail git rebase --skip &&
+ git rebase --skip
+'
+
+test_expect_success '--reapply-cherry-picks' '
+ git init repo &&
+
+ # O(1-10) -- O(1-11) -- O(0-10) main
+ # \
+ # -- O(1-11) -- O(1-12) otherbranch
+
+ printf "Line %d\n" $(test_seq 1 10) >repo/file.txt &&
+ git -C repo add file.txt &&
+ git -C repo commit -m "base commit" &&
+
+ printf "Line %d\n" $(test_seq 1 11) >repo/file.txt &&
+ git -C repo commit -a -m "add 11" &&
+
+ printf "Line %d\n" $(test_seq 0 10) >repo/file.txt &&
+ git -C repo commit -a -m "add 0 delete 11" &&
+
+ git -C repo checkout -b otherbranch HEAD^^ &&
+ printf "Line %d\n" $(test_seq 1 11) >repo/file.txt &&
+ git -C repo commit -a -m "add 11 in another branch" &&
+
+ printf "Line %d\n" $(test_seq 1 12) >repo/file.txt &&
+ git -C repo commit -a -m "add 12 in another branch" &&
+
+ # Regular rebase fails, because the 1-11 commit is deduplicated
+ test_must_fail git -C repo rebase --merge main 2> err &&
+ test_grep "error: could not apply.*add 12 in another branch" err &&
+ git -C repo rebase --abort &&
+
+ # With --reapply-cherry-picks, it works
+ git -C repo rebase --merge --reapply-cherry-picks main
+'
+
+test_expect_success '--reapply-cherry-picks refrains from reading unneeded blobs' '
+ git init server &&
+
+ # O(1-10) -- O(1-11) -- O(1-12) main
+ # \
+ # -- O(0-10) otherbranch
+
+ printf "Line %d\n" $(test_seq 1 10) >server/file.txt &&
+ git -C server add file.txt &&
+ git -C server commit -m "merge base" &&
+
+ printf "Line %d\n" $(test_seq 1 11) >server/file.txt &&
+ git -C server commit -a -m "add 11" &&
+
+ printf "Line %d\n" $(test_seq 1 12) >server/file.txt &&
+ git -C server commit -a -m "add 12" &&
+
+ git -C server checkout -b otherbranch HEAD^^ &&
+ printf "Line %d\n" $(test_seq 0 10) >server/file.txt &&
+ git -C server commit -a -m "add 0" &&
+
+ test_config -C server uploadpack.allowfilter 1 &&
+ test_config -C server uploadpack.allowanysha1inwant 1 &&
+
+ git clone --filter=blob:none "file://$(pwd)/server" client &&
+ git -C client checkout origin/main &&
+ git -C client checkout origin/otherbranch &&
+
+ # Sanity check to ensure that the blobs from the merge base and "add
+ # 11" are missing
+ git -C client rev-list --objects --all --missing=print >missing_list &&
+ MERGE_BASE_BLOB=$(git -C server rev-parse main^^:file.txt) &&
+ ADD_11_BLOB=$(git -C server rev-parse main^:file.txt) &&
+ grep "[?]$MERGE_BASE_BLOB" missing_list &&
+ grep "[?]$ADD_11_BLOB" missing_list &&
+
+ git -C client rebase --merge --reapply-cherry-picks origin/main &&
+
+ # The blob from the merge base had to be fetched, but not "add 11"
+ git -C client rev-list --objects --all --missing=print >missing_list &&
+ ! grep "[?]$MERGE_BASE_BLOB" missing_list &&
+ grep "[?]$ADD_11_BLOB" missing_list
+'
+
+test_done
diff --git a/t/t3403-rebase-skip.sh b/t/t3403-rebase-skip.sh
new file mode 100755
index 0000000..a1911c4
--- /dev/null
+++ b/t/t3403-rebase-skip.sh
@@ -0,0 +1,202 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Eric Wong
+#
+
+test_description='git rebase --merge --skip tests'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+. "$TEST_DIRECTORY"/lib-rebase.sh
+
+# we assume the default git am -3 --skip strategy is tested independently
+# and always works :)
+
+test_expect_success setup '
+ echo hello > hello &&
+ git add hello &&
+ git commit -m "hello" &&
+ git branch skip-reference &&
+ git tag hello &&
+
+ echo world >> hello &&
+ git commit -a -m "hello world" &&
+ echo goodbye >> hello &&
+ git commit -a -m "goodbye" &&
+ git tag goodbye &&
+
+ git checkout --detach &&
+ git checkout HEAD^ . &&
+ test_tick &&
+ git commit -m reverted-goodbye &&
+ git tag reverted-goodbye &&
+ git checkout goodbye &&
+ test_tick &&
+ GIT_AUTHOR_NAME="Another Author" \
+ GIT_AUTHOR_EMAIL="another.author@example.com" \
+ git commit --amend --no-edit -m amended-goodbye \
+ --reset-author &&
+ test_tick &&
+ git tag amended-goodbye &&
+
+ git checkout -f skip-reference &&
+ echo moo > hello &&
+ git commit -a -m "we should skip this" &&
+ echo moo > cow &&
+ git add cow &&
+ git commit -m "this should not be skipped" &&
+ git branch pre-rebase skip-reference &&
+ git branch skip-merge skip-reference
+ '
+
+test_expect_success 'rebase with git am -3 (default)' '
+ test_must_fail git rebase --apply main
+'
+
+test_expect_success 'rebase --skip can not be used with other options' '
+ test_must_fail git rebase -v --skip &&
+ test_must_fail git rebase --skip -v
+'
+
+test_expect_success 'rebase --skip with am -3' '
+ git rebase --skip
+ '
+
+test_expect_success 'rebase moves back to skip-reference' '
+ test refs/heads/skip-reference = $(git symbolic-ref HEAD) &&
+ git branch post-rebase &&
+ git reset --hard pre-rebase &&
+ test_must_fail git rebase main &&
+ echo "hello" > hello &&
+ git add hello &&
+ git rebase --continue &&
+ test refs/heads/skip-reference = $(git symbolic-ref HEAD) &&
+ git reset --hard post-rebase
+'
+
+test_expect_success 'checkout skip-merge' 'git checkout -f skip-merge'
+
+test_expect_success 'rebase with --merge' '
+ test_must_fail git rebase --merge main
+'
+
+test_expect_success 'rebase --skip with --merge' '
+ git rebase --skip
+'
+
+test_expect_success 'merge and reference trees equal' '
+ test -z "$(git diff-tree skip-merge skip-reference)"
+'
+
+test_expect_success 'moved back to branch correctly' '
+ test refs/heads/skip-merge = $(git symbolic-ref HEAD)
+'
+
+test_debug 'gitk --all & sleep 1'
+
+test_expect_success 'skipping final pick removes .git/MERGE_MSG' '
+ test_must_fail git rebase --onto hello reverted-goodbye^ \
+ reverted-goodbye &&
+ git rebase --skip &&
+ test_path_is_missing .git/MERGE_MSG
+'
+
+test_expect_success 'correct advice upon picking empty commit' '
+ test_when_finished "git rebase --abort" &&
+ test_must_fail git rebase -i --onto goodbye \
+ amended-goodbye^ amended-goodbye 2>err &&
+ test_grep "previous cherry-pick is now empty" err &&
+ test_grep "git rebase --skip" err &&
+ test_must_fail git commit &&
+ test_grep "git rebase --skip" err
+'
+
+test_expect_success 'correct authorship when committing empty pick' '
+ test_when_finished "git rebase --abort" &&
+ test_must_fail git rebase -i --onto goodbye \
+ amended-goodbye^ amended-goodbye &&
+ git commit --allow-empty &&
+ git log --pretty=format:"%an <%ae>%n%ad%B" -1 amended-goodbye >expect &&
+ git log --pretty=format:"%an <%ae>%n%ad%B" -1 HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'correct advice upon rewording empty commit' '
+ test_when_finished "git rebase --abort" &&
+ (
+ set_fake_editor &&
+ test_must_fail env FAKE_LINES="reword 1" git rebase -i \
+ --onto goodbye amended-goodbye^ amended-goodbye 2>err
+ ) &&
+ test_grep "previous cherry-pick is now empty" err &&
+ test_grep "git rebase --skip" err &&
+ test_must_fail git commit &&
+ test_grep "git rebase --skip" err
+'
+
+test_expect_success 'correct advice upon editing empty commit' '
+ test_when_finished "git rebase --abort" &&
+ (
+ set_fake_editor &&
+ test_must_fail env FAKE_LINES="edit 1" git rebase -i \
+ --onto goodbye amended-goodbye^ amended-goodbye 2>err
+ ) &&
+ test_grep "previous cherry-pick is now empty" err &&
+ test_grep "git rebase --skip" err &&
+ test_must_fail git commit &&
+ test_grep "git rebase --skip" err
+'
+
+test_expect_success 'correct advice upon cherry-picking an empty commit during a rebase' '
+ test_when_finished "git rebase --abort" &&
+ (
+ set_fake_editor &&
+ test_must_fail env FAKE_LINES="1 exec_git_cherry-pick_amended-goodbye" \
+ git rebase -i goodbye^ goodbye 2>err
+ ) &&
+ test_grep "previous cherry-pick is now empty" err &&
+ test_grep "git cherry-pick --skip" err &&
+ test_must_fail git commit 2>err &&
+ test_grep "git cherry-pick --skip" err
+'
+
+test_expect_success 'correct advice upon multi cherry-pick picking an empty commit during a rebase' '
+ test_when_finished "git rebase --abort" &&
+ (
+ set_fake_editor &&
+ test_must_fail env FAKE_LINES="1 exec_git_cherry-pick_goodbye_amended-goodbye" \
+ git rebase -i goodbye^^ goodbye 2>err
+ ) &&
+ test_grep "previous cherry-pick is now empty" err &&
+ test_grep "git cherry-pick --skip" err &&
+ test_must_fail git commit 2>err &&
+ test_grep "git cherry-pick --skip" err
+'
+
+test_expect_success 'fixup that empties commit fails' '
+ test_when_finished "git rebase --abort" &&
+ (
+ set_fake_editor &&
+ test_must_fail env FAKE_LINES="1 fixup 2" git rebase -i \
+ goodbye^ reverted-goodbye
+ )
+'
+
+test_expect_success 'squash that empties commit fails' '
+ test_when_finished "git rebase --abort" &&
+ (
+ set_fake_editor &&
+ test_must_fail env FAKE_LINES="1 squash 2" git rebase -i \
+ goodbye^ reverted-goodbye
+ )
+'
+
+# Must be the last test in this file
+test_expect_success '$EDITOR and friends are unchanged' '
+ test_editor_unchanged
+'
+
+test_done
diff --git a/t/t3404-rebase-interactive.sh b/t/t3404-rebase-interactive.sh
new file mode 100755
index 0000000..c5f3055
--- /dev/null
+++ b/t/t3404-rebase-interactive.sh
@@ -0,0 +1,2211 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Johannes E. Schindelin
+#
+
+test_description='git rebase interactive
+
+This test runs git rebase "interactively", by faking an edit, and verifies
+that the result still makes sense.
+
+Initial setup:
+
+ one - two - three - four (conflict-branch)
+ /
+ A - B - C - D - E (primary)
+ | \
+ | F - G - H (branch1)
+ | \
+ |\ I (branch2)
+ | \
+ | J - K - L - M (no-conflict-branch)
+ \
+ N - O - P (no-ff-branch)
+
+ where A, B, D and G all touch file1, and one, two, three, four all
+ touch file "conflict".
+'
+
+. ./test-lib.sh
+
+. "$TEST_DIRECTORY"/lib-rebase.sh
+
+test_expect_success 'setup' '
+ git switch -C primary &&
+ test_commit A file1 &&
+ test_commit B file1 &&
+ test_commit C file2 &&
+ test_commit D file1 &&
+ test_commit E file3 &&
+ git checkout -b branch1 A &&
+ test_commit F file4 &&
+ test_commit G file1 &&
+ test_commit H file5 &&
+ git checkout -b branch2 F &&
+ test_commit I file6 &&
+ git checkout -b conflict-branch A &&
+ test_commit one conflict &&
+ test_commit two conflict &&
+ test_commit three conflict &&
+ test_commit four conflict &&
+ git checkout -b no-conflict-branch A &&
+ test_commit J fileJ &&
+ test_commit K fileK &&
+ test_commit L fileL &&
+ test_commit M fileM &&
+ git checkout -b no-ff-branch A &&
+ test_commit N fileN &&
+ test_commit O fileO &&
+ test_commit P fileP
+'
+
+# "exec" commands are run with the user shell by default, but this may
+# be non-POSIX. For example, if SHELL=zsh then ">file" doesn't work
+# to create a file. Unsetting SHELL avoids such non-portable behavior
+# in tests. It must be exported for it to take effect where needed.
+SHELL=
+export SHELL
+
+test_expect_success 'rebase --keep-empty' '
+ git checkout -b emptybranch primary &&
+ git commit --allow-empty -m "empty" &&
+ git rebase --keep-empty -i HEAD~2 &&
+ git log --oneline >actual &&
+ test_line_count = 6 actual
+'
+
+test_expect_success 'rebase -i with empty todo list' '
+ cat >expect <<-\EOF &&
+ error: nothing to do
+ EOF
+ (
+ set_fake_editor &&
+ test_must_fail env FAKE_LINES="#" \
+ git rebase -i HEAD^ >output 2>&1
+ ) &&
+ tail -n 1 output >actual && # Ignore output about changing todo list
+ test_cmp expect actual
+'
+
+test_expect_success 'rebase -i with the exec command' '
+ git checkout primary &&
+ (
+ set_fake_editor &&
+ FAKE_LINES="1 exec_>touch-one
+ 2 exec_>touch-two exec_false exec_>touch-three
+ 3 4 exec_>\"touch-file__name_with_spaces\";_>touch-after-semicolon 5" &&
+ export FAKE_LINES &&
+ test_must_fail git rebase -i A
+ ) &&
+ test_path_is_file touch-one &&
+ test_path_is_file touch-two &&
+ # Missing because we should have stopped by now.
+ test_path_is_missing touch-three &&
+ test_cmp_rev C HEAD &&
+ git rebase --continue &&
+ test_path_is_file touch-three &&
+ test_path_is_file "touch-file name with spaces" &&
+ test_path_is_file touch-after-semicolon &&
+ test_cmp_rev primary HEAD &&
+ rm -f touch-*
+'
+
+test_expect_success 'rebase -i with the exec command runs from tree root' '
+ git checkout primary &&
+ mkdir subdir && (cd subdir &&
+ set_fake_editor &&
+ FAKE_LINES="1 exec_>touch-subdir" \
+ git rebase -i HEAD^
+ ) &&
+ test_path_is_file touch-subdir &&
+ rm -fr subdir
+'
+
+test_expect_success 'rebase -i with exec allows git commands in subdirs' '
+ test_when_finished "rm -rf subdir" &&
+ test_when_finished "git rebase --abort ||:" &&
+ git checkout primary &&
+ mkdir subdir && (cd subdir &&
+ set_fake_editor &&
+ FAKE_LINES="1 x_cd_subdir_&&_git_rev-parse_--is-inside-work-tree" \
+ git rebase -i HEAD^
+ )
+'
+
+test_expect_success 'rebase -i sets work tree properly' '
+ test_when_finished "rm -rf subdir" &&
+ test_when_finished "test_might_fail git rebase --abort" &&
+ mkdir subdir &&
+ git rebase -x "(cd subdir && git rev-parse --show-toplevel)" HEAD^ \
+ >actual &&
+ ! grep "/subdir$" actual
+'
+
+test_expect_success 'rebase -i with the exec command checks tree cleanness' '
+ git checkout primary &&
+ (
+ set_fake_editor &&
+ test_must_fail env FAKE_LINES="exec_echo_foo_>file1 1" \
+ git rebase -i HEAD^
+ ) &&
+ test_cmp_rev primary^ HEAD &&
+ git reset --hard &&
+ git rebase --continue
+'
+
+test_expect_success 'rebase -x with empty command fails' '
+ test_when_finished "git rebase --abort ||:" &&
+ test_must_fail env git rebase -x "" @ 2>actual &&
+ test_write_lines "error: empty exec command" >expected &&
+ test_cmp expected actual &&
+ test_must_fail env git rebase -x " " @ 2>actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'rebase -x with newline in command fails' '
+ test_when_finished "git rebase --abort ||:" &&
+ test_must_fail env git rebase -x "a${LF}b" @ 2>actual &&
+ test_write_lines "error: exec commands cannot contain newlines" \
+ >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'rebase -i with exec of inexistent command' '
+ git checkout primary &&
+ test_when_finished "git rebase --abort" &&
+ (
+ set_fake_editor &&
+ test_must_fail env FAKE_LINES="exec_this-command-does-not-exist 1" \
+ git rebase -i HEAD^ >actual 2>&1
+ ) &&
+ ! grep "Maybe git-rebase is broken" actual
+'
+
+test_expect_success 'implicit interactive rebase does not invoke sequence editor' '
+ test_when_finished "git rebase --abort ||:" &&
+ GIT_SEQUENCE_EDITOR="echo bad >" git rebase -x"echo one" @^
+'
+
+test_expect_success 'no changes are a nop' '
+ git checkout branch2 &&
+ git rebase -i F &&
+ test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
+ test_cmp_rev I HEAD
+'
+
+test_expect_success 'test the [branch] option' '
+ git checkout -b dead-end &&
+ git rm file6 &&
+ git commit -m "stop here" &&
+ git rebase -i F branch2 &&
+ test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" &&
+ test_cmp_rev I branch2 &&
+ test_cmp_rev I HEAD
+'
+
+test_expect_success 'test --onto <branch>' '
+ git checkout -b test-onto branch2 &&
+ git rebase -i --onto branch1 F &&
+ test "$(git symbolic-ref -q HEAD)" = "refs/heads/test-onto" &&
+ test_cmp_rev HEAD^ branch1 &&
+ test_cmp_rev I branch2
+'
+
+test_expect_success 'rebase on top of a non-conflicting commit' '
+ git checkout branch1 &&
+ git tag original-branch1 &&
+ git rebase -i branch2 &&
+ test file6 = $(git diff --name-only original-branch1) &&
+ test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
+ test_cmp_rev I branch2 &&
+ test_cmp_rev I HEAD~2
+'
+
+test_expect_success 'reflog for the branch shows state before rebase' '
+ test_cmp_rev branch1@{1} original-branch1
+'
+
+test_expect_success 'reflog for the branch shows correct finish message' '
+ printf "rebase (finish): refs/heads/branch1 onto %s\n" \
+ "$(git rev-parse branch2)" >expected &&
+ git log -g --pretty=%gs -1 refs/heads/branch1 >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'exchange two commits' '
+ (
+ set_fake_editor &&
+ FAKE_LINES="2 1" git rebase -i HEAD~2
+ ) &&
+ test H = $(git cat-file commit HEAD^ | sed -ne \$p) &&
+ test G = $(git cat-file commit HEAD | sed -ne \$p) &&
+ blob1=$(git rev-parse --short HEAD^:file1) &&
+ blob2=$(git rev-parse --short HEAD:file1) &&
+ commit=$(git rev-parse --short HEAD)
+'
+
+test_expect_success 'stop on conflicting pick' '
+ cat >expect <<-EOF &&
+ diff --git a/file1 b/file1
+ index $blob1..$blob2 100644
+ --- a/file1
+ +++ b/file1
+ @@ -1 +1 @@
+ -A
+ +G
+ EOF
+ cat >expect2 <<-EOF &&
+ <<<<<<< HEAD
+ D
+ =======
+ G
+ >>>>>>> $commit (G)
+ EOF
+ git tag new-branch1 &&
+ test_must_fail git rebase -i primary &&
+ test "$(git rev-parse HEAD~3)" = "$(git rev-parse primary)" &&
+ test_cmp expect .git/rebase-merge/patch &&
+ test_cmp expect2 file1 &&
+ test "$(git diff --name-status |
+ sed -n -e "/^U/s/^U[^a-z]*//p")" = file1 &&
+ test 4 = $(grep -v "^#" < .git/rebase-merge/done | wc -l) &&
+ test 0 = $(grep -c "^[^#]" < .git/rebase-merge/git-rebase-todo)
+'
+
+test_expect_success 'show conflicted patch' '
+ GIT_TRACE=1 git rebase --show-current-patch >/dev/null 2>stderr &&
+ grep "show.*REBASE_HEAD" stderr &&
+ # the original stopped-sha1 is abbreviated
+ stopped_sha1="$(git rev-parse $(cat ".git/rebase-merge/stopped-sha"))" &&
+ test "$(git rev-parse REBASE_HEAD)" = "$stopped_sha1"
+'
+
+test_expect_success 'abort' '
+ git rebase --abort &&
+ test_cmp_rev new-branch1 HEAD &&
+ test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" &&
+ test_path_is_missing .git/rebase-merge
+'
+
+test_expect_success 'abort with error when new base cannot be checked out' '
+ git rm --cached file1 &&
+ git commit -m "remove file in base" &&
+ test_must_fail git rebase -i primary > output 2>&1 &&
+ test_grep "The following untracked working tree files would be overwritten by checkout:" \
+ output &&
+ test_grep "file1" output &&
+ test_path_is_missing .git/rebase-merge &&
+ rm file1 &&
+ git reset --hard HEAD^
+'
+
+test_expect_success 'retain authorship' '
+ echo A > file7 &&
+ git add file7 &&
+ test_tick &&
+ GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
+ git tag twerp &&
+ git rebase -i --onto primary HEAD^ &&
+ git show HEAD | grep "^Author: Twerp Snog"
+'
+
+test_expect_success 'retain authorship w/ conflicts' '
+ oGIT_AUTHOR_NAME=$GIT_AUTHOR_NAME &&
+ test_when_finished "GIT_AUTHOR_NAME=\$oGIT_AUTHOR_NAME" &&
+
+ git reset --hard twerp &&
+ test_commit a conflict a conflict-a &&
+ git reset --hard twerp &&
+
+ GIT_AUTHOR_NAME=AttributeMe &&
+ export GIT_AUTHOR_NAME &&
+ test_commit b conflict b conflict-b &&
+ GIT_AUTHOR_NAME=$oGIT_AUTHOR_NAME &&
+
+ test_must_fail git rebase -i conflict-a &&
+ echo resolved >conflict &&
+ git add conflict &&
+ git rebase --continue &&
+ test_cmp_rev conflict-a^0 HEAD^ &&
+ git show >out &&
+ grep AttributeMe out
+'
+
+test_expect_success 'squash' '
+ git reset --hard twerp &&
+ echo B > file7 &&
+ test_tick &&
+ GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 &&
+ echo "******************************" &&
+ (
+ set_fake_editor &&
+ FAKE_LINES="1 squash 2" EXPECT_HEADER_COUNT=2 \
+ git rebase -i --onto primary HEAD~2
+ ) &&
+ test B = $(cat file7) &&
+ test_cmp_rev HEAD^ primary
+'
+
+test_expect_success 'retain authorship when squashing' '
+ git show HEAD | grep "^Author: Twerp Snog"
+'
+
+test_expect_success '--continue tries to commit' '
+ git reset --hard D &&
+ test_tick &&
+ (
+ set_fake_editor &&
+ test_must_fail git rebase -i --onto new-branch1 HEAD^ &&
+ echo resolved > file1 &&
+ git add file1 &&
+ FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue
+ ) &&
+ test_cmp_rev HEAD^ new-branch1 &&
+ git show HEAD | grep chouette
+'
+
+test_expect_success 'verbose flag is heeded, even after --continue' '
+ git reset --hard primary@{1} &&
+ test_tick &&
+ test_must_fail git rebase -v -i --onto new-branch1 HEAD^ &&
+ echo resolved > file1 &&
+ git add file1 &&
+ git rebase --continue > output &&
+ grep "^ file1 | 2 +-$" output
+'
+
+test_expect_success 'multi-squash only fires up editor once' '
+ base=$(git rev-parse HEAD~4) &&
+ (
+ set_fake_editor &&
+ FAKE_COMMIT_AMEND="ONCE" \
+ FAKE_LINES="1 squash 2 squash 3 squash 4" \
+ EXPECT_HEADER_COUNT=4 \
+ git rebase -i $base
+ ) &&
+ test $base = $(git rev-parse HEAD^) &&
+ test 1 = $(git show | grep ONCE | wc -l)
+'
+
+test_expect_success 'multi-fixup does not fire up editor' '
+ git checkout -b multi-fixup E &&
+ base=$(git rev-parse HEAD~4) &&
+ (
+ set_fake_editor &&
+ FAKE_COMMIT_AMEND="NEVER" \
+ FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \
+ git rebase -i $base
+ ) &&
+ test $base = $(git rev-parse HEAD^) &&
+ test 0 = $(git show | grep NEVER | wc -l) &&
+ git checkout @{-1} &&
+ git branch -D multi-fixup
+'
+
+test_expect_success 'commit message used after conflict' '
+ git checkout -b conflict-fixup conflict-branch &&
+ base=$(git rev-parse HEAD~4) &&
+ (
+ set_fake_editor &&
+ test_must_fail env FAKE_LINES="1 fixup 3 fixup 4" \
+ git rebase -i $base &&
+ echo three > conflict &&
+ git add conflict &&
+ FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \
+ git rebase --continue
+ ) &&
+ test $base = $(git rev-parse HEAD^) &&
+ test 1 = $(git show | grep ONCE | wc -l) &&
+ git checkout @{-1} &&
+ git branch -D conflict-fixup
+'
+
+test_expect_success 'commit message retained after conflict' '
+ git checkout -b conflict-squash conflict-branch &&
+ base=$(git rev-parse HEAD~4) &&
+ (
+ set_fake_editor &&
+ test_must_fail env FAKE_LINES="1 fixup 3 squash 4" \
+ git rebase -i $base &&
+ echo three > conflict &&
+ git add conflict &&
+ FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \
+ git rebase --continue
+ ) &&
+ test $base = $(git rev-parse HEAD^) &&
+ test 2 = $(git show | grep TWICE | wc -l) &&
+ git checkout @{-1} &&
+ git branch -D conflict-squash
+'
+
+test_expect_success 'squash and fixup generate correct log messages' '
+ cat >expect-squash-fixup <<-\EOF &&
+ B
+
+ D
+
+ ONCE
+ EOF
+ git checkout -b squash-fixup E &&
+ base=$(git rev-parse HEAD~4) &&
+ (
+ set_fake_editor &&
+ FAKE_COMMIT_AMEND="ONCE" \
+ FAKE_LINES="1 fixup 2 squash 3 fixup 4" \
+ EXPECT_HEADER_COUNT=4 \
+ git rebase -i $base
+ ) &&
+ git cat-file commit HEAD | sed -e 1,/^\$/d > actual-squash-fixup &&
+ test_cmp expect-squash-fixup actual-squash-fixup &&
+ git cat-file commit HEAD@{2} |
+ grep "^# This is a combination of 3 commits\." &&
+ git cat-file commit HEAD@{3} |
+ grep "^# This is a combination of 2 commits\." &&
+ git checkout @{-1} &&
+ git branch -D squash-fixup
+'
+
+test_expect_success 'squash ignores comments' '
+ git checkout -b skip-comments E &&
+ base=$(git rev-parse HEAD~4) &&
+ (
+ set_fake_editor &&
+ FAKE_COMMIT_AMEND="ONCE" \
+ FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \
+ EXPECT_HEADER_COUNT=4 \
+ git rebase -i $base
+ ) &&
+ test $base = $(git rev-parse HEAD^) &&
+ test 1 = $(git show | grep ONCE | wc -l) &&
+ git checkout @{-1} &&
+ git branch -D skip-comments
+'
+
+test_expect_success 'squash ignores blank lines' '
+ git checkout -b skip-blank-lines E &&
+ base=$(git rev-parse HEAD~4) &&
+ (
+ set_fake_editor &&
+ FAKE_COMMIT_AMEND="ONCE" \
+ FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \
+ EXPECT_HEADER_COUNT=4 \
+ git rebase -i $base
+ ) &&
+ test $base = $(git rev-parse HEAD^) &&
+ test 1 = $(git show | grep ONCE | wc -l) &&
+ git checkout @{-1} &&
+ git branch -D skip-blank-lines
+'
+
+test_expect_success 'squash works as expected' '
+ git checkout -b squash-works no-conflict-branch &&
+ one=$(git rev-parse HEAD~3) &&
+ (
+ set_fake_editor &&
+ FAKE_LINES="1 s 3 2" EXPECT_HEADER_COUNT=2 git rebase -i HEAD~3
+ ) &&
+ test $one = $(git rev-parse HEAD~2)
+'
+
+test_expect_success 'interrupted squash works as expected' '
+ git checkout -b interrupted-squash conflict-branch &&
+ one=$(git rev-parse HEAD~3) &&
+ (
+ set_fake_editor &&
+ test_must_fail env FAKE_LINES="1 squash 3 2" \
+ git rebase -i HEAD~3
+ ) &&
+ test_write_lines one two four > conflict &&
+ git add conflict &&
+ test_must_fail git rebase --continue &&
+ echo resolved > conflict &&
+ git add conflict &&
+ git rebase --continue &&
+ test $one = $(git rev-parse HEAD~2)
+'
+
+test_expect_success 'interrupted squash works as expected (case 2)' '
+ git checkout -b interrupted-squash2 conflict-branch &&
+ one=$(git rev-parse HEAD~3) &&
+ (
+ set_fake_editor &&
+ test_must_fail env FAKE_LINES="3 squash 1 2" \
+ git rebase -i HEAD~3
+ ) &&
+ test_write_lines one four > conflict &&
+ git add conflict &&
+ test_must_fail git rebase --continue &&
+ test_write_lines one two four > conflict &&
+ git add conflict &&
+ test_must_fail git rebase --continue &&
+ echo resolved > conflict &&
+ git add conflict &&
+ git rebase --continue &&
+ test $one = $(git rev-parse HEAD~2)
+'
+
+test_expect_success '--continue tries to commit, even for "edit"' '
+ echo unrelated > file7 &&
+ git add file7 &&
+ test_tick &&
+ git commit -m "unrelated change" &&
+ parent=$(git rev-parse HEAD^) &&
+ test_tick &&
+ (
+ set_fake_editor &&
+ FAKE_LINES="edit 1" git rebase -i HEAD^ &&
+ echo edited > file7 &&
+ git add file7 &&
+ FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue
+ ) &&
+ test edited = $(git show HEAD:file7) &&
+ git show HEAD | grep chouette &&
+ test $parent = $(git rev-parse HEAD^)
+'
+
+test_expect_success 'aborted --continue does not squash commits after "edit"' '
+ old=$(git rev-parse HEAD) &&
+ test_tick &&
+ (
+ set_fake_editor &&
+ FAKE_LINES="edit 1" git rebase -i HEAD^ &&
+ echo "edited again" > file7 &&
+ git add file7 &&
+ test_must_fail env FAKE_COMMIT_MESSAGE=" " git rebase --continue
+ ) &&
+ test $old = $(git rev-parse HEAD) &&
+ git rebase --abort
+'
+
+test_expect_success 'auto-amend only edited commits after "edit"' '
+ test_tick &&
+ (
+ set_fake_editor &&
+ FAKE_LINES="edit 1" git rebase -i HEAD^ &&
+ echo "edited again" > file7 &&
+ git add file7 &&
+ FAKE_COMMIT_MESSAGE="edited file7 again" git commit &&
+ echo "and again" > file7 &&
+ git add file7 &&
+ test_tick &&
+ test_must_fail env FAKE_COMMIT_MESSAGE="and again" \
+ git rebase --continue
+ ) &&
+ git rebase --abort
+'
+
+test_expect_success 'clean error after failed "exec"' '
+ test_tick &&
+ test_when_finished "git rebase --abort || :" &&
+ (
+ set_fake_editor &&
+ test_must_fail env FAKE_LINES="1 exec_false" git rebase -i HEAD^
+ ) &&
+ echo "edited again" > file7 &&
+ git add file7 &&
+ test_must_fail git rebase --continue 2>error &&
+ test_grep "you have staged changes in your working tree" error &&
+ test_grep ! "could not open.*for reading" error
+'
+
+test_expect_success 'rebase a detached HEAD' '
+ grandparent=$(git rev-parse HEAD~2) &&
+ git checkout $(git rev-parse HEAD) &&
+ test_tick &&
+ (
+ set_fake_editor &&
+ FAKE_LINES="2 1" git rebase -i HEAD~2
+ ) &&
+ test $grandparent = $(git rev-parse HEAD~2)
+'
+
+test_expect_success 'rebase a commit violating pre-commit' '
+ test_hook pre-commit <<-\EOF &&
+ test -z "$(git diff --cached --check)"
+ EOF
+ echo "monde! " >> file1 &&
+ test_tick &&
+ test_must_fail git commit -m doesnt-verify file1 &&
+ git commit -m doesnt-verify --no-verify file1 &&
+ test_tick &&
+ (
+ set_fake_editor &&
+ FAKE_LINES=2 git rebase -i HEAD~2
+ )
+'
+
+test_expect_success 'rebase with a file named HEAD in worktree' '
+ git reset --hard &&
+ git checkout -b branch3 A &&
+
+ (
+ GIT_AUTHOR_NAME="Squashed Away" &&
+ export GIT_AUTHOR_NAME &&
+ >HEAD &&
+ git add HEAD &&
+ git commit -m "Add head" &&
+ >BODY &&
+ git add BODY &&
+ git commit -m "Add body"
+ ) &&
+
+ (
+ set_fake_editor &&
+ FAKE_LINES="1 squash 2" git rebase -i @{-1}
+ ) &&
+ test "$(git show -s --pretty=format:%an)" = "Squashed Away"
+
+'
+
+test_expect_success 'do "noop" when there is nothing to cherry-pick' '
+
+ git checkout -b branch4 HEAD &&
+ GIT_EDITOR=: git commit --amend \
+ --author="Somebody else <somebody@else.com>" &&
+ test $(git rev-parse branch3) != $(git rev-parse branch4) &&
+ git rebase -i branch3 &&
+ test_cmp_rev branch3 branch4
+
+'
+
+test_expect_success 'submodule rebase setup' '
+ git checkout A &&
+ mkdir sub &&
+ (
+ cd sub && git init && >elif &&
+ git add elif && git commit -m "submodule initial"
+ ) &&
+ echo 1 >file1 &&
+ git add file1 sub &&
+ test_tick &&
+ git commit -m "One" &&
+ echo 2 >file1 &&
+ test_tick &&
+ git commit -a -m "Two" &&
+ (
+ cd sub && echo 3 >elif &&
+ git commit -a -m "submodule second"
+ ) &&
+ test_tick &&
+ git commit -a -m "Three changes submodule"
+'
+
+test_expect_success 'submodule rebase -i' '
+ (
+ set_fake_editor &&
+ FAKE_LINES="1 squash 2 3" git rebase -i A
+ )
+'
+
+test_expect_success 'submodule conflict setup' '
+ git tag submodule-base &&
+ git checkout HEAD^ &&
+ (
+ cd sub && git checkout HEAD^ && echo 4 >elif &&
+ git add elif && git commit -m "submodule conflict"
+ ) &&
+ git add sub &&
+ test_tick &&
+ git commit -m "Conflict in submodule" &&
+ git tag submodule-topic
+'
+
+test_expect_success 'rebase -i continue with only submodule staged' '
+ test_must_fail git rebase -i submodule-base &&
+ git add sub &&
+ git rebase --continue &&
+ test $(git rev-parse submodule-base) != $(git rev-parse HEAD)
+'
+
+test_expect_success 'rebase -i continue with unstaged submodule' '
+ git checkout submodule-topic &&
+ git reset --hard &&
+ test_must_fail git rebase -i submodule-base &&
+ git reset &&
+ git rebase --continue &&
+ test_cmp_rev submodule-base HEAD
+'
+
+test_expect_success 'avoid unnecessary reset' '
+ git checkout primary &&
+ git reset --hard &&
+ test-tool chmtime =123456789 file3 &&
+ git update-index --refresh &&
+ HEAD=$(git rev-parse HEAD) &&
+ git rebase -i HEAD~4 &&
+ test $HEAD = $(git rev-parse HEAD) &&
+ MTIME=$(test-tool chmtime --get file3) &&
+ test 123456789 = $MTIME
+'
+
+test_expect_success 'reword' '
+ git checkout -b reword-branch primary &&
+ (
+ set_fake_editor &&
+ FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" \
+ git rebase -i A &&
+ git show HEAD | grep "E changed" &&
+ test $(git rev-parse primary) != $(git rev-parse HEAD) &&
+ test_cmp_rev primary^ HEAD^ &&
+ FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" \
+ git rebase -i A &&
+ git show HEAD^ | grep "D changed" &&
+ FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" \
+ git rebase -i A &&
+ git show HEAD~3 | grep "B changed" &&
+ FAKE_LINES="1 r 2 pick 3 p 4" FAKE_COMMIT_MESSAGE="C changed" \
+ git rebase -i A
+ ) &&
+ git show HEAD~2 | grep "C changed"
+'
+
+test_expect_success 'no uncommitted changes when rewording and the todo list is reloaded' '
+ git checkout E &&
+ test_when_finished "git checkout @{-1}" &&
+ (
+ set_fake_editor &&
+ GIT_SEQUENCE_EDITOR="\"$PWD/fake-editor.sh\"" &&
+ export GIT_SEQUENCE_EDITOR &&
+ set_reword_editor &&
+ FAKE_LINES="reword 1 reword 2" git rebase -i C
+ ) &&
+ check_reworded_commits D E
+'
+
+test_expect_success 'rebase -i can copy notes' '
+ git config notes.rewrite.rebase true &&
+ git config notes.rewriteRef "refs/notes/*" &&
+ test_commit n1 &&
+ test_commit n2 &&
+ test_commit n3 &&
+ git notes add -m"a note" n3 &&
+ git rebase -i --onto n1 n2 &&
+ test "a note" = "$(git notes show HEAD)"
+'
+
+test_expect_success 'rebase -i can copy notes over a fixup' '
+ cat >expect <<-\EOF &&
+ an earlier note
+
+ a note
+ EOF
+ git reset --hard n3 &&
+ git notes add -m"an earlier note" n2 &&
+ (
+ set_fake_editor &&
+ GIT_NOTES_REWRITE_MODE=concatenate FAKE_LINES="1 f 2" \
+ git rebase -i n1
+ ) &&
+ git notes show > output &&
+ test_cmp expect output
+'
+
+test_expect_success 'rebase while detaching HEAD' '
+ git symbolic-ref HEAD &&
+ grandparent=$(git rev-parse HEAD~2) &&
+ test_tick &&
+ (
+ set_fake_editor &&
+ FAKE_LINES="2 1" git rebase -i HEAD~2 HEAD^0
+ ) &&
+ test $grandparent = $(git rev-parse HEAD~2) &&
+ test_must_fail git symbolic-ref HEAD
+'
+
+test_tick # Ensure that the rebased commits get a different timestamp.
+test_expect_success 'always cherry-pick with --no-ff' '
+ git checkout no-ff-branch &&
+ git tag original-no-ff-branch &&
+ git rebase -i --no-ff A &&
+ for p in 0 1 2
+ do
+ test ! $(git rev-parse HEAD~$p) = $(git rev-parse original-no-ff-branch~$p) &&
+ git diff HEAD~$p original-no-ff-branch~$p > out &&
+ test_must_be_empty out || return 1
+ done &&
+ test_cmp_rev HEAD~3 original-no-ff-branch~3 &&
+ git diff HEAD~3 original-no-ff-branch~3 > out &&
+ test_must_be_empty out
+'
+
+test_expect_success 'set up commits with funny messages' '
+ git checkout -b funny A &&
+ echo >>file1 &&
+ test_tick &&
+ git commit -a -m "end with slash\\" &&
+ echo >>file1 &&
+ test_tick &&
+ git commit -a -m "something (\000) that looks like octal" &&
+ echo >>file1 &&
+ test_tick &&
+ git commit -a -m "something (\n) that looks like a newline" &&
+ echo >>file1 &&
+ test_tick &&
+ git commit -a -m "another commit"
+'
+
+test_expect_success 'rebase-i history with funny messages' '
+ git rev-list A..funny >expect &&
+ test_tick &&
+ (
+ set_fake_editor &&
+ FAKE_LINES="1 2 3 4" git rebase -i A
+ ) &&
+ git rev-list A.. >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'prepare for rebase -i --exec' '
+ git checkout primary &&
+ git checkout -b execute &&
+ test_commit one_exec main.txt one_exec &&
+ test_commit two_exec main.txt two_exec &&
+ test_commit three_exec main.txt three_exec
+'
+
+test_expect_success 'running "git rebase -i --exec git show HEAD"' '
+ (
+ set_fake_editor &&
+ git rebase -i --exec "git show HEAD" HEAD~2 >actual &&
+ FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
+ export FAKE_LINES &&
+ git rebase -i HEAD~2 >expect
+ ) &&
+ sed -e "1,9d" expect >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'running "git rebase --exec git show HEAD -i"' '
+ git reset --hard execute &&
+ (
+ set_fake_editor &&
+ git rebase --exec "git show HEAD" -i HEAD~2 >actual &&
+ FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
+ export FAKE_LINES &&
+ git rebase -i HEAD~2 >expect
+ ) &&
+ sed -e "1,9d" expect >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'running "git rebase -ix git show HEAD"' '
+ git reset --hard execute &&
+ (
+ set_fake_editor &&
+ git rebase -ix "git show HEAD" HEAD~2 >actual &&
+ FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
+ export FAKE_LINES &&
+ git rebase -i HEAD~2 >expect
+ ) &&
+ sed -e "1,9d" expect >expected &&
+ test_cmp expected actual
+'
+
+
+test_expect_success 'rebase -ix with several <CMD>' '
+ git reset --hard execute &&
+ (
+ set_fake_editor &&
+ git rebase -ix "git show HEAD; pwd" HEAD~2 >actual &&
+ FAKE_LINES="1 exec_git_show_HEAD;_pwd 2 exec_git_show_HEAD;_pwd" &&
+ export FAKE_LINES &&
+ git rebase -i HEAD~2 >expect
+ ) &&
+ sed -e "1,9d" expect >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'rebase -ix with several instances of --exec' '
+ git reset --hard execute &&
+ (
+ set_fake_editor &&
+ git rebase -i --exec "git show HEAD" --exec "pwd" HEAD~2 >actual &&
+ FAKE_LINES="1 exec_git_show_HEAD exec_pwd 2
+ exec_git_show_HEAD exec_pwd" &&
+ export FAKE_LINES &&
+ git rebase -i HEAD~2 >expect
+ ) &&
+ sed -e "1,11d" expect >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'rebase -ix with --autosquash' '
+ git reset --hard execute &&
+ git checkout -b autosquash &&
+ echo second >second.txt &&
+ git add second.txt &&
+ git commit -m "fixup! two_exec" &&
+ echo bis >bis.txt &&
+ git add bis.txt &&
+ git commit -m "fixup! two_exec" &&
+ git checkout -b autosquash_actual &&
+ git rebase -i --exec "git show HEAD" --autosquash HEAD~4 >actual &&
+ git checkout autosquash &&
+ (
+ set_fake_editor &&
+ git checkout -b autosquash_expected &&
+ FAKE_LINES="1 fixup 3 fixup 4 exec_git_show_HEAD 2 exec_git_show_HEAD" &&
+ export FAKE_LINES &&
+ git rebase -i HEAD~4 >expect
+ ) &&
+ sed -e "1,13d" expect >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'rebase --exec works without -i ' '
+ git reset --hard execute &&
+ rm -rf exec_output &&
+ EDITOR="echo >invoked_editor" git rebase --exec "echo a line >>exec_output" HEAD~2 2>actual &&
+ test_grep "Successfully rebased and updated" actual &&
+ test_line_count = 2 exec_output &&
+ test_path_is_missing invoked_editor
+'
+
+test_expect_success 'rebase -i --exec without <CMD>' '
+ git reset --hard execute &&
+ test_must_fail git rebase -i --exec 2>actual &&
+ test_grep "requires a value" actual &&
+ git checkout primary
+'
+
+test_expect_success 'rebase -i --root re-order and drop commits' '
+ git checkout E &&
+ (
+ set_fake_editor &&
+ FAKE_LINES="3 1 2 5" git rebase -i --root
+ ) &&
+ test E = $(git cat-file commit HEAD | sed -ne \$p) &&
+ test B = $(git cat-file commit HEAD^ | sed -ne \$p) &&
+ test A = $(git cat-file commit HEAD^^ | sed -ne \$p) &&
+ test C = $(git cat-file commit HEAD^^^ | sed -ne \$p) &&
+ test 0 = $(git cat-file commit HEAD^^^ | grep -c ^parent\ )
+'
+
+test_expect_success 'rebase -i --root retain root commit author and message' '
+ git checkout A &&
+ echo B >file7 &&
+ git add file7 &&
+ GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" &&
+ (
+ set_fake_editor &&
+ FAKE_LINES="2" git rebase -i --root
+ ) &&
+ git cat-file commit HEAD | grep -q "^author Twerp Snog" &&
+ git cat-file commit HEAD | grep -q "^different author$"
+'
+
+test_expect_success 'rebase -i --root temporary sentinel commit' '
+ git checkout B &&
+ (
+ set_fake_editor &&
+ test_must_fail env FAKE_LINES="2" git rebase -i --root
+ ) &&
+ git cat-file commit HEAD | grep "^tree $EMPTY_TREE" &&
+ git rebase --abort
+'
+
+test_expect_success 'rebase -i --root fixup root commit' '
+ git checkout B &&
+ (
+ set_fake_editor &&
+ FAKE_LINES="1 fixup 2" git rebase -i --root
+ ) &&
+ test A = $(git cat-file commit HEAD | sed -ne \$p) &&
+ test B = $(git show HEAD:file1) &&
+ test 0 = $(git cat-file commit HEAD | grep -c ^parent\ )
+'
+
+test_expect_success 'rebase -i --root reword original root commit' '
+ test_when_finished "test_might_fail git rebase --abort" &&
+ git checkout -b reword-original-root-branch primary &&
+ (
+ set_fake_editor &&
+ FAKE_LINES="reword 1 2" FAKE_COMMIT_MESSAGE="A changed" \
+ git rebase -i --root
+ ) &&
+ git show HEAD^ | grep "A changed" &&
+ test -z "$(git show -s --format=%p HEAD^)"
+'
+
+test_expect_success 'rebase -i --root reword new root commit' '
+ test_when_finished "test_might_fail git rebase --abort" &&
+ git checkout -b reword-now-root-branch primary &&
+ (
+ set_fake_editor &&
+ FAKE_LINES="reword 3 1" FAKE_COMMIT_MESSAGE="C changed" \
+ git rebase -i --root
+ ) &&
+ git show HEAD^ | grep "C changed" &&
+ test -z "$(git show -s --format=%p HEAD^)"
+'
+
+test_expect_success 'rebase -i --root when root has untracked file conflict' '
+ test_when_finished "reset_rebase" &&
+ git checkout -b failing-root-pick A &&
+ echo x >file2 &&
+ git rm file1 &&
+ git commit -m "remove file 1 add file 2" &&
+ echo z >file1 &&
+ (
+ set_fake_editor &&
+ test_must_fail env FAKE_LINES="1 2" git rebase -i --root
+ ) &&
+ rm file1 &&
+ git rebase --continue &&
+ test "$(git log -1 --format=%B)" = "remove file 1 add file 2" &&
+ test "$(git rev-list --count HEAD)" = 2
+'
+
+test_expect_success 'rebase -i --root reword root when root has untracked file conflict' '
+ test_when_finished "reset_rebase" &&
+ echo z>file1 &&
+ (
+ set_fake_editor &&
+ test_must_fail env FAKE_LINES="reword 1 2" \
+ FAKE_COMMIT_MESSAGE="Modified A" git rebase -i --root &&
+ rm file1 &&
+ FAKE_COMMIT_MESSAGE="Reworded A" git rebase --continue
+ ) &&
+ test "$(git log -1 --format=%B HEAD^)" = "Reworded A" &&
+ test "$(git rev-list --count HEAD)" = 2
+'
+
+test_expect_success 'rebase --edit-todo does not work on non-interactive rebase' '
+ git checkout reword-original-root-branch &&
+ git reset --hard &&
+ git checkout conflict-branch &&
+ (
+ set_fake_editor &&
+ test_must_fail git rebase -f --apply --onto HEAD~2 HEAD~ &&
+ test_must_fail git rebase --edit-todo
+ ) &&
+ git rebase --abort
+'
+
+test_expect_success 'rebase --edit-todo can be used to modify todo' '
+ git reset --hard &&
+ git checkout no-conflict-branch^0 &&
+ (
+ set_fake_editor &&
+ FAKE_LINES="edit 1 2 3" git rebase -i HEAD~3 &&
+ FAKE_LINES="2 1" git rebase --edit-todo &&
+ git rebase --continue
+ ) &&
+ test M = $(git cat-file commit HEAD^ | sed -ne \$p) &&
+ test L = $(git cat-file commit HEAD | sed -ne \$p)
+'
+
+test_expect_success 'rebase -i produces readable reflog' '
+ git reset --hard &&
+ git branch -f branch-reflog-test H &&
+ git rebase -i --onto I F branch-reflog-test &&
+ cat >expect <<-\EOF &&
+ rebase (finish): returning to refs/heads/branch-reflog-test
+ rebase (pick): H
+ rebase (pick): G
+ rebase (start): checkout I
+ EOF
+ git reflog -n4 HEAD |
+ sed "s/[^:]*: //" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rebase -i respects core.commentchar' '
+ git reset --hard &&
+ git checkout E^0 &&
+ test_config core.commentchar "\\" &&
+ write_script remove-all-but-first.sh <<-\EOF &&
+ sed -e "2,\$s/^/\\\\/" "$1" >"$1.tmp" &&
+ mv "$1.tmp" "$1"
+ EOF
+ (
+ test_set_editor "$(pwd)/remove-all-but-first.sh" &&
+ git rebase -i B
+ ) &&
+ test B = $(git cat-file commit HEAD^ | sed -ne \$p)
+'
+
+test_expect_success 'rebase -i respects core.commentchar=auto' '
+ test_config core.commentchar auto &&
+ write_script copy-edit-script.sh <<-\EOF &&
+ cp "$1" edit-script
+ EOF
+ test_when_finished "git rebase --abort || :" &&
+ (
+ test_set_editor "$(pwd)/copy-edit-script.sh" &&
+ git rebase -i HEAD^
+ ) &&
+ test -z "$(grep -ve "^#" -e "^\$" -e "^pick" edit-script)"
+'
+
+test_expect_success 'rebase -i, with <onto> and <upstream> specified as :/quuxery' '
+ test_when_finished "git branch -D torebase" &&
+ git checkout -b torebase branch1 &&
+ upstream=$(git rev-parse ":/J") &&
+ onto=$(git rev-parse ":/A") &&
+ git rebase --onto $onto $upstream &&
+ git reset --hard branch1 &&
+ git rebase --onto ":/A" ":/J" &&
+ git checkout branch1
+'
+
+test_expect_success 'rebase -i with --strategy and -X' '
+ git checkout -b conflict-merge-use-theirs conflict-branch &&
+ git reset --hard HEAD^ &&
+ echo five >conflict &&
+ echo Z >file1 &&
+ git commit -a -m "one file conflict" &&
+ EDITOR=true git rebase -i --strategy=recursive -Xours conflict-branch &&
+ test $(git show conflict-branch:conflict) = $(cat conflict) &&
+ test $(cat file1) = Z
+'
+
+test_expect_success 'interrupted rebase -i with --strategy and -X' '
+ git checkout -b conflict-merge-use-theirs-interrupted conflict-branch &&
+ git reset --hard HEAD^ &&
+ >breakpoint &&
+ git add breakpoint &&
+ git commit -m "breakpoint for interactive mode" &&
+ echo five >conflict &&
+ echo Z >file1 &&
+ git commit -a -m "one file conflict" &&
+ (
+ set_fake_editor &&
+ FAKE_LINES="edit 1 2" git rebase -i --strategy=recursive \
+ -Xours conflict-branch
+ ) &&
+ git rebase --continue &&
+ test $(git show conflict-branch:conflict) = $(cat conflict) &&
+ test $(cat file1) = Z
+'
+
+test_expect_success 'rebase -i error on commits with \ in message' '
+ current_head=$(git rev-parse HEAD) &&
+ test_when_finished "git rebase --abort; git reset --hard $current_head; rm -f error" &&
+ test_commit TO-REMOVE will-conflict old-content &&
+ test_commit "\temp" will-conflict new-content dummy &&
+ test_must_fail env EDITOR=true git rebase -i HEAD^ --onto HEAD^^ 2>error &&
+ test_expect_code 1 grep " emp" error
+'
+
+test_expect_success 'short commit ID setup' '
+ test_when_finished "git checkout primary" &&
+ git checkout --orphan collide &&
+ git rm -rf . &&
+ (
+ unset test_tick &&
+ test_commit collide1 collide &&
+ test_commit --notick collide2 collide &&
+ test_commit --notick collide3 collide
+ )
+'
+
+if test -n "$GIT_TEST_FIND_COLLIDER"
+then
+ author="$(unset test_tick; test_tick; git var GIT_AUTHOR_IDENT)"
+ committer="$(unset test_tick; test_tick; git var GIT_COMMITTER_IDENT)"
+ blob="$(git rev-parse collide2:collide)"
+ from="$(git rev-parse collide1^0)"
+ repl="commit refs/heads/collider-&\\n"
+ repl="${repl}author $author\\ncommitter $committer\\n"
+ repl="${repl}data <<EOF\\ncollide2 &\\nEOF\\n"
+ repl="${repl}from $from\\nM 100644 $blob collide\\n"
+ test_seq 1 32768 | sed "s|.*|$repl|" >script &&
+ git fast-import <script &&
+ git pack-refs &&
+ git for-each-ref >refs &&
+ grep "^$(test_oid t3404_collision)" <refs >matches &&
+ cat matches &&
+ test_line_count -gt 2 matches || {
+ echo "Could not find a collider" >&2
+ exit 1
+ }
+fi
+
+test_expect_success 'short commit ID collide' '
+ test_oid_cache <<-EOF &&
+ # collision-related constants
+ t3404_collision sha1:6bcd
+ t3404_collision sha256:0161
+ t3404_collider sha1:ac4f2ee
+ t3404_collider sha256:16697
+ EOF
+ test_when_finished "reset_rebase && git checkout primary" &&
+ git checkout collide &&
+ colliding_id=$(test_oid t3404_collision) &&
+ hexsz=$(test_oid hexsz) &&
+ test $colliding_id = "$(git rev-parse HEAD | cut -c 1-4)" &&
+ test_config core.abbrev 4 &&
+ (
+ unset test_tick &&
+ test_tick &&
+ set_fake_editor &&
+ FAKE_COMMIT_MESSAGE="collide2 $(test_oid t3404_collider)" \
+ FAKE_LINES="reword 1 break 2" git rebase -i HEAD~2 &&
+ test $colliding_id = "$(git rev-parse HEAD | cut -c 1-4)" &&
+ grep "^pick $colliding_id " \
+ .git/rebase-merge/git-rebase-todo.tmp &&
+ grep -E "^pick [0-9a-f]{$hexsz}" \
+ .git/rebase-merge/git-rebase-todo &&
+ grep -E "^pick [0-9a-f]{$hexsz}" \
+ .git/rebase-merge/git-rebase-todo.backup &&
+ git rebase --continue
+ ) &&
+ collide2="$(git rev-parse HEAD~1 | cut -c 1-4)" &&
+ collide3="$(git rev-parse collide3 | cut -c 1-4)" &&
+ test "$collide2" = "$collide3"
+'
+
+test_expect_success 'respect core.abbrev' '
+ git config core.abbrev 12 &&
+ (
+ set_cat_todo_editor &&
+ test_must_fail git rebase -i HEAD~4 >todo-list
+ ) &&
+ test 4 = $(grep -c -E "pick [0-9a-f]{12,}" todo-list)
+'
+
+test_expect_success 'todo count' '
+ write_script dump-raw.sh <<-\EOF &&
+ cat "$1"
+ EOF
+ (
+ test_set_editor "$(pwd)/dump-raw.sh" &&
+ git rebase -i HEAD~4 >actual
+ ) &&
+ test_grep "^# Rebase ..* onto ..* ([0-9]" actual
+'
+
+test_expect_success 'rebase -i commits that overwrite untracked files (pick)' '
+ git checkout --force A &&
+ git clean -f &&
+ cat >todo <<-EOF &&
+ exec >file2
+ pick $(git rev-parse B) B
+ pick $(git rev-parse C) C
+ pick $(git rev-parse D) D
+ exec cat .git/rebase-merge/done >actual
+ EOF
+ (
+ set_replace_editor todo &&
+ test_must_fail git rebase -i A
+ ) &&
+ test_cmp_rev HEAD B &&
+ test_cmp_rev REBASE_HEAD C &&
+ head -n3 todo >expect &&
+ test_cmp expect .git/rebase-merge/done &&
+ rm file2 &&
+ test_path_is_missing .git/rebase-merge/patch &&
+ echo changed >file1 &&
+ git add file1 &&
+ test_must_fail git rebase --continue 2>err &&
+ grep "error: you have staged changes in your working tree" err &&
+ git reset --hard HEAD &&
+ git rebase --continue &&
+ test_cmp_rev HEAD D &&
+ tail -n3 todo >>expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rebase -i commits that overwrite untracked files (squash)' '
+ git checkout --force branch2 &&
+ git clean -f &&
+ git tag original-branch2 &&
+ (
+ set_fake_editor &&
+ FAKE_LINES="edit 1 squash 2" git rebase -i A
+ ) &&
+ test_cmp_rev HEAD F &&
+ test_path_is_missing file6 &&
+ >file6 &&
+ test_must_fail git rebase --continue &&
+ test_cmp_rev HEAD F &&
+ test_cmp_rev REBASE_HEAD I &&
+ rm file6 &&
+ test_path_is_missing .git/rebase-merge/patch &&
+ echo changed >file1 &&
+ git add file1 &&
+ test_must_fail git rebase --continue 2>err &&
+ grep "error: you have staged changes in your working tree" err &&
+ git reset --hard HEAD &&
+ git rebase --continue &&
+ test $(git cat-file commit HEAD | sed -ne \$p) = I &&
+ git reset --hard original-branch2
+'
+
+test_expect_success 'rebase -i commits that overwrite untracked files (no ff)' '
+ git checkout --force branch2 &&
+ git clean -f &&
+ (
+ set_fake_editor &&
+ FAKE_LINES="edit 1 2" git rebase -i --no-ff A
+ ) &&
+ test $(git cat-file commit HEAD | sed -ne \$p) = F &&
+ test_path_is_missing file6 &&
+ >file6 &&
+ test_must_fail git rebase --continue &&
+ test $(git cat-file commit HEAD | sed -ne \$p) = F &&
+ test_cmp_rev REBASE_HEAD I &&
+ rm file6 &&
+ test_path_is_missing .git/rebase-merge/patch &&
+ echo changed >file1 &&
+ git add file1 &&
+ test_must_fail git rebase --continue 2>err &&
+ grep "error: you have staged changes in your working tree" err &&
+ git reset --hard HEAD &&
+ git rebase --continue &&
+ test $(git cat-file commit HEAD | sed -ne \$p) = I
+'
+
+test_expect_success 'rebase --continue removes CHERRY_PICK_HEAD' '
+ git checkout -b commit-to-skip &&
+ for double in X 3 1
+ do
+ test_seq 5 | sed "s/$double/&&/" >seq &&
+ git add seq &&
+ test_tick &&
+ git commit -m seq-$double || return 1
+ done &&
+ git tag seq-onto &&
+ git reset --hard HEAD~2 &&
+ git cherry-pick seq-onto &&
+ (
+ set_fake_editor &&
+ test_must_fail env FAKE_LINES= git rebase -i seq-onto
+ ) &&
+ test -d .git/rebase-merge &&
+ git rebase --continue &&
+ git diff --exit-code seq-onto &&
+ test ! -d .git/rebase-merge &&
+ test ! -f .git/CHERRY_PICK_HEAD
+'
+
+rebase_setup_and_clean () {
+ test_when_finished "
+ git checkout primary &&
+ test_might_fail git branch -D $1 &&
+ test_might_fail git rebase --abort
+ " &&
+ git checkout -b $1 ${2:-primary}
+}
+
+test_expect_success 'drop' '
+ rebase_setup_and_clean drop-test &&
+ (
+ set_fake_editor &&
+ FAKE_LINES="1 drop 2 3 d 4 5" git rebase -i --root
+ ) &&
+ test E = $(git cat-file commit HEAD | sed -ne \$p) &&
+ test C = $(git cat-file commit HEAD^ | sed -ne \$p) &&
+ test A = $(git cat-file commit HEAD^^ | sed -ne \$p)
+'
+
+test_expect_success 'rebase -i respects rebase.missingCommitsCheck = ignore' '
+ test_config rebase.missingCommitsCheck ignore &&
+ rebase_setup_and_clean missing-commit &&
+ (
+ set_fake_editor &&
+ FAKE_LINES="1 2 3 4" git rebase -i --root 2>actual
+ ) &&
+ test D = $(git cat-file commit HEAD | sed -ne \$p) &&
+ test_grep \
+ "Successfully rebased and updated refs/heads/missing-commit" \
+ actual
+'
+
+test_expect_success 'rebase -i respects rebase.missingCommitsCheck = warn' '
+ cat >expect <<-EOF &&
+ Warning: some commits may have been dropped accidentally.
+ Dropped commits (newer to older):
+ - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
+ To avoid this message, use "drop" to explicitly remove a commit.
+ EOF
+ test_config rebase.missingCommitsCheck warn &&
+ rebase_setup_and_clean missing-commit &&
+ (
+ set_fake_editor &&
+ FAKE_LINES="1 2 3 4" git rebase -i --root 2>actual.2
+ ) &&
+ head -n4 actual.2 >actual &&
+ test_cmp expect actual &&
+ test D = $(git cat-file commit HEAD | sed -ne \$p)
+'
+
+test_expect_success 'rebase -i respects rebase.missingCommitsCheck = error' '
+ cat >expect <<-EOF &&
+ Warning: some commits may have been dropped accidentally.
+ Dropped commits (newer to older):
+ - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
+ - $(git rev-list --pretty=oneline --abbrev-commit -1 primary~2)
+ To avoid this message, use "drop" to explicitly remove a commit.
+
+ Use '\''git config rebase.missingCommitsCheck'\'' to change the level of warnings.
+ The possible behaviours are: ignore, warn, error.
+
+ You can fix this with '\''git rebase --edit-todo'\'' and then run '\''git rebase --continue'\''.
+ Or you can abort the rebase with '\''git rebase --abort'\''.
+ EOF
+ test_config rebase.missingCommitsCheck error &&
+ rebase_setup_and_clean missing-commit &&
+ (
+ set_fake_editor &&
+ test_must_fail env FAKE_LINES="1 2 4" \
+ git rebase -i --root 2>actual &&
+ test_cmp expect actual &&
+ cp .git/rebase-merge/git-rebase-todo.backup \
+ .git/rebase-merge/git-rebase-todo &&
+ FAKE_LINES="1 2 drop 3 4 drop 5" git rebase --edit-todo
+ ) &&
+ git rebase --continue &&
+ test D = $(git cat-file commit HEAD | sed -ne \$p) &&
+ test B = $(git cat-file commit HEAD^ | sed -ne \$p)
+'
+
+test_expect_success 'rebase --edit-todo respects rebase.missingCommitsCheck = ignore' '
+ test_config rebase.missingCommitsCheck ignore &&
+ rebase_setup_and_clean missing-commit &&
+ (
+ set_fake_editor &&
+ FAKE_LINES="break 1 2 3 4 5" git rebase -i --root &&
+ FAKE_LINES="1 2 3 4" git rebase --edit-todo &&
+ git rebase --continue 2>actual
+ ) &&
+ test D = $(git cat-file commit HEAD | sed -ne \$p) &&
+ test_grep \
+ "Successfully rebased and updated refs/heads/missing-commit" \
+ actual
+'
+
+test_expect_success 'rebase --edit-todo respects rebase.missingCommitsCheck = warn' '
+ cat >expect <<-EOF &&
+ error: invalid command '\''pickled'\''
+ error: invalid line 1: pickled $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
+ Warning: some commits may have been dropped accidentally.
+ Dropped commits (newer to older):
+ - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
+ - $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
+ To avoid this message, use "drop" to explicitly remove a commit.
+ EOF
+ head -n5 expect >expect.2 &&
+ tail -n1 expect >>expect.2 &&
+ tail -n4 expect.2 >expect.3 &&
+ test_config rebase.missingCommitsCheck warn &&
+ rebase_setup_and_clean missing-commit &&
+ (
+ set_fake_editor &&
+ test_must_fail env FAKE_LINES="bad 1 2 3 4 5" \
+ git rebase -i --root &&
+ cp .git/rebase-merge/git-rebase-todo.backup orig &&
+ FAKE_LINES="2 3 4" git rebase --edit-todo 2>actual.2 &&
+ head -n7 actual.2 >actual &&
+ test_cmp expect actual &&
+ cp orig .git/rebase-merge/git-rebase-todo &&
+ FAKE_LINES="1 2 3 4" git rebase --edit-todo 2>actual.2 &&
+ head -n4 actual.2 >actual &&
+ test_cmp expect.3 actual &&
+ git rebase --continue 2>actual
+ ) &&
+ test D = $(git cat-file commit HEAD | sed -ne \$p) &&
+ test_grep \
+ "Successfully rebased and updated refs/heads/missing-commit" \
+ actual
+'
+
+test_expect_success 'rebase --edit-todo respects rebase.missingCommitsCheck = error' '
+ cat >expect <<-EOF &&
+ error: invalid command '\''pickled'\''
+ error: invalid line 1: pickled $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
+ Warning: some commits may have been dropped accidentally.
+ Dropped commits (newer to older):
+ - $(git rev-list --pretty=oneline --abbrev-commit -1 primary)
+ - $(git rev-list --pretty=oneline --abbrev-commit -1 primary~4)
+ To avoid this message, use "drop" to explicitly remove a commit.
+
+ Use '\''git config rebase.missingCommitsCheck'\'' to change the level of warnings.
+ The possible behaviours are: ignore, warn, error.
+
+ You can fix this with '\''git rebase --edit-todo'\'' and then run '\''git rebase --continue'\''.
+ Or you can abort the rebase with '\''git rebase --abort'\''.
+ EOF
+ tail -n11 expect >expect.2 &&
+ head -n3 expect.2 >expect.3 &&
+ tail -n7 expect.2 >>expect.3 &&
+ test_config rebase.missingCommitsCheck error &&
+ rebase_setup_and_clean missing-commit &&
+ (
+ set_fake_editor &&
+ test_must_fail env FAKE_LINES="bad 1 2 3 4 5" \
+ git rebase -i --root &&
+ cp .git/rebase-merge/git-rebase-todo.backup orig &&
+ test_must_fail env FAKE_LINES="2 3 4" \
+ git rebase --edit-todo 2>actual &&
+ test_cmp expect actual &&
+ test_must_fail git rebase --continue 2>actual &&
+ test_cmp expect.2 actual &&
+ test_must_fail git rebase --edit-todo &&
+ cp orig .git/rebase-merge/git-rebase-todo &&
+ test_must_fail env FAKE_LINES="1 2 3 4" \
+ git rebase --edit-todo 2>actual &&
+ test_cmp expect.3 actual &&
+ test_must_fail git rebase --continue 2>actual &&
+ test_cmp expect.3 actual &&
+ cp orig .git/rebase-merge/git-rebase-todo &&
+ FAKE_LINES="1 2 3 4 drop 5" git rebase --edit-todo &&
+ git rebase --continue 2>actual
+ ) &&
+ test D = $(git cat-file commit HEAD | sed -ne \$p) &&
+ test_grep \
+ "Successfully rebased and updated refs/heads/missing-commit" \
+ actual
+'
+
+test_expect_success 'rebase.missingCommitsCheck = error after resolving conflicts' '
+ test_config rebase.missingCommitsCheck error &&
+ (
+ set_fake_editor &&
+ FAKE_LINES="drop 1 break 2 3 4" git rebase -i A E
+ ) &&
+ git rebase --edit-todo &&
+ test_must_fail git rebase --continue &&
+ echo x >file1 &&
+ git add file1 &&
+ git rebase --continue
+'
+
+test_expect_success 'rebase.missingCommitsCheck = error when editing for a second time' '
+ test_config rebase.missingCommitsCheck error &&
+ (
+ set_fake_editor &&
+ FAKE_LINES="1 break 2 3" git rebase -i A D &&
+ cp .git/rebase-merge/git-rebase-todo todo &&
+ test_must_fail env FAKE_LINES=2 git rebase --edit-todo &&
+ GIT_SEQUENCE_EDITOR="cp todo" git rebase --edit-todo &&
+ git rebase --continue
+ )
+'
+
+test_expect_success 'respects rebase.abbreviateCommands with fixup, squash and exec' '
+ rebase_setup_and_clean abbrevcmd &&
+ test_commit "first" file1.txt "first line" first &&
+ test_commit "second" file1.txt "another line" second &&
+ test_commit "fixup! first" file2.txt "first line again" first_fixup &&
+ test_commit "squash! second" file1.txt "another line here" second_squash &&
+ cat >expected <<-EOF &&
+ p $(git rev-list --abbrev-commit -1 first) first
+ f $(git rev-list --abbrev-commit -1 first_fixup) fixup! first
+ x git show HEAD
+ p $(git rev-list --abbrev-commit -1 second) second
+ s $(git rev-list --abbrev-commit -1 second_squash) squash! second
+ x git show HEAD
+ EOF
+ git checkout abbrevcmd &&
+ test_config rebase.abbreviateCommands true &&
+ (
+ set_cat_todo_editor &&
+ test_must_fail git rebase -i --exec "git show HEAD" \
+ --autosquash primary >actual
+ ) &&
+ test_cmp expected actual
+'
+
+test_expect_success 'static check of bad command' '
+ rebase_setup_and_clean bad-cmd &&
+ (
+ set_fake_editor &&
+ test_must_fail env FAKE_LINES="1 2 3 bad 4 5" \
+ git rebase -i --root 2>actual &&
+ test_grep "pickled $(git rev-list --oneline -1 primary~1)" \
+ actual &&
+ test_grep "You can fix this with .git rebase --edit-todo.." \
+ actual &&
+ FAKE_LINES="1 2 3 drop 4 5" git rebase --edit-todo
+ ) &&
+ git rebase --continue &&
+ test E = $(git cat-file commit HEAD | sed -ne \$p) &&
+ test C = $(git cat-file commit HEAD^ | sed -ne \$p)
+'
+
+test_expect_success 'the first command cannot be a fixup' '
+ rebase_setup_and_clean fixup-first &&
+
+ cat >orig <<-EOF &&
+ fixup $(git log -1 --format="%h %s" B)
+ pick $(git log -1 --format="%h %s" C)
+ EOF
+
+ (
+ set_replace_editor orig &&
+ test_must_fail git rebase -i A 2>actual
+ ) &&
+ grep "cannot .fixup. without a previous commit" actual &&
+ grep "You can fix this with .git rebase --edit-todo.." actual &&
+ # verify that the todo list has not been truncated
+ grep -v "^#" .git/rebase-merge/git-rebase-todo >actual &&
+ test_cmp orig actual &&
+
+ test_must_fail git rebase --edit-todo 2>actual &&
+ grep "cannot .fixup. without a previous commit" actual &&
+ grep "You can fix this with .git rebase --edit-todo.." actual &&
+ # verify that the todo list has not been truncated
+ grep -v "^#" .git/rebase-merge/git-rebase-todo >actual &&
+ test_cmp orig actual
+'
+
+test_expect_success 'tabs and spaces are accepted in the todolist' '
+ rebase_setup_and_clean indented-comment &&
+ write_script add-indent.sh <<-\EOF &&
+ (
+ # Turn single spaces into space/tab mix
+ sed "1s/ / /g; 2s/ / /g; 3s/ / /g" "$1"
+ printf "\n\t# comment\n #more\n\t # comment\n"
+ ) >"$1.new"
+ mv "$1.new" "$1"
+ EOF
+ (
+ test_set_editor "$(pwd)/add-indent.sh" &&
+ git rebase -i HEAD^^^
+ ) &&
+ test E = $(git cat-file commit HEAD | sed -ne \$p)
+'
+
+test_expect_success 'static check of bad SHA-1' '
+ rebase_setup_and_clean bad-sha &&
+ (
+ set_fake_editor &&
+ test_must_fail env FAKE_LINES="1 2 edit fakesha 3 4 5 #" \
+ git rebase -i --root 2>actual &&
+ test_grep "edit XXXXXXX False commit" actual &&
+ test_grep "You can fix this with .git rebase --edit-todo.." \
+ actual &&
+ FAKE_LINES="1 2 4 5 6" git rebase --edit-todo
+ ) &&
+ git rebase --continue &&
+ test E = $(git cat-file commit HEAD | sed -ne \$p)
+'
+
+test_expect_success 'editor saves as CR/LF' '
+ git checkout -b with-crlf &&
+ write_script add-crs.sh <<-\EOF &&
+ sed -e "s/\$/Q/" <"$1" | tr Q "\\015" >"$1".new &&
+ mv -f "$1".new "$1"
+ EOF
+ (
+ test_set_editor "$(pwd)/add-crs.sh" &&
+ git rebase -i HEAD^
+ )
+'
+
+test_expect_success 'rebase -i --gpg-sign=<key-id>' '
+ test_when_finished "test_might_fail git rebase --abort" &&
+ (
+ set_fake_editor &&
+ FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" \
+ HEAD^ >out 2>err
+ ) &&
+ test_grep "$SQ-S\"S I Gner\"$SQ" err
+'
+
+test_expect_success 'rebase -i --gpg-sign=<key-id> overrides commit.gpgSign' '
+ test_when_finished "test_might_fail git rebase --abort" &&
+ test_config commit.gpgsign true &&
+ (
+ set_fake_editor &&
+ FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" \
+ HEAD^ >out 2>err
+ ) &&
+ test_grep "$SQ-S\"S I Gner\"$SQ" err
+'
+
+test_expect_success 'valid author header after --root swap' '
+ rebase_setup_and_clean author-header no-conflict-branch &&
+ git commit --amend --author="Au ${SQ}thor <author@example.com>" --no-edit &&
+ git cat-file commit HEAD | grep ^author >expected &&
+ (
+ set_fake_editor &&
+ FAKE_LINES="5 1" git rebase -i --root
+ ) &&
+ git cat-file commit HEAD^ | grep ^author >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'valid author header when author contains single quote' '
+ rebase_setup_and_clean author-header no-conflict-branch &&
+ git commit --amend --author="Au ${SQ}thor <author@example.com>" --no-edit &&
+ git cat-file commit HEAD | grep ^author >expected &&
+ (
+ set_fake_editor &&
+ FAKE_LINES="2" git rebase -i HEAD~2
+ ) &&
+ git cat-file commit HEAD | grep ^author >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'post-commit hook is called' '
+ >actual &&
+ test_hook post-commit <<-\EOS &&
+ git rev-parse HEAD >>actual
+ EOS
+ (
+ set_fake_editor &&
+ FAKE_LINES="edit 4 1 reword 2 fixup 3" git rebase -i A E &&
+ echo x>file3 &&
+ git add file3 &&
+ FAKE_COMMIT_MESSAGE=edited git rebase --continue
+ ) &&
+ git rev-parse HEAD@{5} HEAD@{4} HEAD@{3} HEAD@{2} HEAD@{1} HEAD \
+ >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'correct error message for partial commit after empty pick' '
+ test_when_finished "git rebase --abort" &&
+ (
+ set_fake_editor &&
+ FAKE_LINES="2 1 1" &&
+ export FAKE_LINES &&
+ test_must_fail git rebase -i A D
+ ) &&
+ echo x >file1 &&
+ test_must_fail git commit file1 2>err &&
+ test_grep "cannot do a partial commit during a rebase." err
+'
+
+test_expect_success 'correct error message for commit --amend after empty pick' '
+ test_when_finished "git rebase --abort" &&
+ (
+ set_fake_editor &&
+ FAKE_LINES="1 1" &&
+ export FAKE_LINES &&
+ test_must_fail git rebase -i A D
+ ) &&
+ echo x>file1 &&
+ test_must_fail git commit -a --amend 2>err &&
+ test_grep "middle of a rebase -- cannot amend." err
+'
+
+test_expect_success 'todo has correct onto hash' '
+ GIT_SEQUENCE_EDITOR=cat git rebase -i no-conflict-branch~4 no-conflict-branch >actual &&
+ onto=$(git rev-parse --short HEAD~4) &&
+ test_grep "^# Rebase ..* onto $onto" actual
+'
+
+test_expect_success 'ORIG_HEAD is updated correctly' '
+ test_when_finished "git checkout primary && git branch -D test-orig-head" &&
+ git checkout -b test-orig-head A &&
+ git commit --allow-empty -m A1 &&
+ git commit --allow-empty -m A2 &&
+ git commit --allow-empty -m A3 &&
+ git commit --allow-empty -m A4 &&
+ git rebase primary &&
+ test_cmp_rev ORIG_HEAD test-orig-head@{1}
+'
+
+test_expect_success '--update-refs adds label and update-ref commands' '
+ git checkout -b update-refs no-conflict-branch &&
+ git branch -f base HEAD~4 &&
+ git branch -f first HEAD~3 &&
+ git branch -f second HEAD~3 &&
+ git branch -f third HEAD~1 &&
+ git commit --allow-empty --fixup=third &&
+ git branch -f is-not-reordered &&
+ git commit --allow-empty --fixup=HEAD~4 &&
+ git branch -f shared-tip &&
+ (
+ set_cat_todo_editor &&
+
+ cat >expect <<-EOF &&
+ pick $(git log -1 --format=%h J) J
+ fixup $(git log -1 --format=%h update-refs) fixup! J # empty
+ update-ref refs/heads/second
+ update-ref refs/heads/first
+ pick $(git log -1 --format=%h K) K
+ pick $(git log -1 --format=%h L) L
+ fixup $(git log -1 --format=%h is-not-reordered) fixup! L # empty
+ update-ref refs/heads/third
+ pick $(git log -1 --format=%h M) M
+ update-ref refs/heads/no-conflict-branch
+ update-ref refs/heads/is-not-reordered
+ update-ref refs/heads/shared-tip
+ EOF
+
+ test_must_fail git rebase -i --autosquash --update-refs primary >todo &&
+ test_cmp expect todo &&
+
+ test_must_fail git -c rebase.autosquash=true \
+ -c rebase.updaterefs=true \
+ rebase -i primary >todo &&
+
+ test_cmp expect todo
+ )
+'
+
+test_expect_success '--update-refs adds commands with --rebase-merges' '
+ git checkout -b update-refs-with-merge no-conflict-branch &&
+ git branch -f base HEAD~4 &&
+ git branch -f first HEAD~3 &&
+ git branch -f second HEAD~3 &&
+ git branch -f third HEAD~1 &&
+ git merge -m merge branch2 &&
+ git branch -f merge-branch &&
+ git commit --fixup=third --allow-empty &&
+ (
+ set_cat_todo_editor &&
+
+ cat >expect <<-EOF &&
+ label onto
+ reset onto
+ pick $(git log -1 --format=%h branch2~1) F
+ pick $(git log -1 --format=%h branch2) I
+ update-ref refs/heads/branch2
+ label merge
+ reset onto
+ pick $(git log -1 --format=%h refs/heads/second) J
+ update-ref refs/heads/second
+ update-ref refs/heads/first
+ pick $(git log -1 --format=%h refs/heads/third~1) K
+ pick $(git log -1 --format=%h refs/heads/third) L
+ fixup $(git log -1 --format=%h update-refs-with-merge) fixup! L # empty
+ update-ref refs/heads/third
+ pick $(git log -1 --format=%h HEAD~2) M
+ update-ref refs/heads/no-conflict-branch
+ merge -C $(git log -1 --format=%h HEAD~1) merge # merge
+ update-ref refs/heads/merge-branch
+ EOF
+
+ test_must_fail git rebase -i --autosquash \
+ --rebase-merges=rebase-cousins \
+ --update-refs primary >todo &&
+
+ test_cmp expect todo &&
+
+ test_must_fail git -c rebase.autosquash=true \
+ -c rebase.updaterefs=true \
+ rebase -i \
+ --rebase-merges=rebase-cousins \
+ primary >todo &&
+
+ test_cmp expect todo
+ )
+'
+
+test_expect_success '--update-refs updates refs correctly' '
+ git checkout -B update-refs no-conflict-branch &&
+ git branch -f base HEAD~4 &&
+ git branch -f first HEAD~3 &&
+ git branch -f second HEAD~3 &&
+ git branch -f third HEAD~1 &&
+ test_commit extra2 fileX &&
+ git commit --amend --fixup=L &&
+
+ git rebase -i --autosquash --update-refs primary 2>err &&
+
+ test_cmp_rev HEAD~3 refs/heads/first &&
+ test_cmp_rev HEAD~3 refs/heads/second &&
+ test_cmp_rev HEAD~1 refs/heads/third &&
+ test_cmp_rev HEAD refs/heads/no-conflict-branch &&
+
+ cat >expect <<-\EOF &&
+ Successfully rebased and updated refs/heads/update-refs.
+ Updated the following refs with --update-refs:
+ refs/heads/first
+ refs/heads/no-conflict-branch
+ refs/heads/second
+ refs/heads/third
+ EOF
+
+ # Clear "Rebasing (X/Y)" progress lines and drop leading tabs.
+ sed -e "s/Rebasing.*Successfully/Successfully/g" -e "s/^\t//g" \
+ <err >err.trimmed &&
+ test_cmp expect err.trimmed
+'
+
+test_expect_success 'respect user edits to update-ref steps' '
+ git checkout -B update-refs-break no-conflict-branch &&
+ git branch -f base HEAD~4 &&
+ git branch -f first HEAD~3 &&
+ git branch -f second HEAD~3 &&
+ git branch -f third HEAD~1 &&
+ git branch -f unseen base &&
+
+ # First, we will add breaks to the expected todo file
+ cat >fake-todo-1 <<-EOF &&
+ pick $(git rev-parse HEAD~3)
+ break
+ update-ref refs/heads/second
+ update-ref refs/heads/first
+
+ pick $(git rev-parse HEAD~2)
+ pick $(git rev-parse HEAD~1)
+ update-ref refs/heads/third
+
+ pick $(git rev-parse HEAD)
+ update-ref refs/heads/no-conflict-branch
+ EOF
+
+ # Second, we will drop some update-refs commands (and move one)
+ cat >fake-todo-2 <<-EOF &&
+ update-ref refs/heads/second
+
+ pick $(git rev-parse HEAD~2)
+ update-ref refs/heads/third
+ pick $(git rev-parse HEAD~1)
+ break
+
+ pick $(git rev-parse HEAD)
+ EOF
+
+ # Third, we will:
+ # * insert a new one (new-branch),
+ # * re-add an old one (first), and
+ # * add a second instance of a previously-stored one (second)
+ cat >fake-todo-3 <<-EOF &&
+ update-ref refs/heads/unseen
+ update-ref refs/heads/new-branch
+ pick $(git rev-parse HEAD)
+ update-ref refs/heads/first
+ update-ref refs/heads/second
+ EOF
+
+ (
+ set_replace_editor fake-todo-1 &&
+ git rebase -i --update-refs primary &&
+
+ # These branches are currently locked.
+ for b in first second third no-conflict-branch
+ do
+ test_must_fail git branch -f $b base || return 1
+ done &&
+
+ set_replace_editor fake-todo-2 &&
+ git rebase --edit-todo &&
+
+ # These branches are currently locked.
+ for b in second third
+ do
+ test_must_fail git branch -f $b base || return 1
+ done &&
+
+ # These branches are currently unlocked for checkout.
+ for b in first no-conflict-branch
+ do
+ git worktree add wt-$b $b &&
+ git worktree remove wt-$b || return 1
+ done &&
+
+ git rebase --continue &&
+
+ set_replace_editor fake-todo-3 &&
+ git rebase --edit-todo &&
+
+ # These branches are currently locked.
+ for b in second third first unseen
+ do
+ test_must_fail git branch -f $b base || return 1
+ done &&
+
+ # These branches are currently unlocked for checkout.
+ for b in no-conflict-branch
+ do
+ git worktree add wt-$b $b &&
+ git worktree remove wt-$b || return 1
+ done &&
+
+ git rebase --continue
+ ) &&
+
+ test_cmp_rev HEAD~2 refs/heads/third &&
+ test_cmp_rev HEAD~1 refs/heads/unseen &&
+ test_cmp_rev HEAD~1 refs/heads/new-branch &&
+ test_cmp_rev HEAD refs/heads/first &&
+ test_cmp_rev HEAD refs/heads/second &&
+ test_cmp_rev HEAD refs/heads/no-conflict-branch
+'
+
+test_expect_success '--update-refs: all update-ref lines removed' '
+ git checkout -b test-refs-not-removed no-conflict-branch &&
+ git branch -f base HEAD~4 &&
+ git branch -f first HEAD~3 &&
+ git branch -f second HEAD~3 &&
+ git branch -f third HEAD~1 &&
+ git branch -f tip &&
+
+ test_commit test-refs-not-removed &&
+ git commit --amend --fixup first &&
+
+ git rev-parse first second third tip no-conflict-branch >expect-oids &&
+
+ (
+ set_cat_todo_editor &&
+ test_must_fail git rebase -i --update-refs base >todo.raw &&
+ sed -e "/^update-ref/d" <todo.raw >todo
+ ) &&
+ (
+ set_replace_editor todo &&
+ git rebase -i --update-refs base
+ ) &&
+
+ # Ensure refs are not deleted and their OIDs have not changed
+ git rev-parse first second third tip no-conflict-branch >actual-oids &&
+ test_cmp expect-oids actual-oids
+'
+
+test_expect_success '--update-refs: all update-ref lines removed, then some re-added' '
+ git checkout -b test-refs-not-removed2 no-conflict-branch &&
+ git branch -f base HEAD~4 &&
+ git branch -f first HEAD~3 &&
+ git branch -f second HEAD~3 &&
+ git branch -f third HEAD~1 &&
+ git branch -f tip &&
+
+ test_commit test-refs-not-removed2 &&
+ git commit --amend --fixup first &&
+
+ git rev-parse first second third >expect-oids &&
+
+ (
+ set_cat_todo_editor &&
+ test_must_fail git rebase -i \
+ --autosquash --update-refs \
+ base >todo.raw &&
+ sed -e "/^update-ref/d" <todo.raw >todo
+ ) &&
+
+ # Add a break to the end of the todo so we can edit later
+ echo "break" >>todo &&
+
+ (
+ set_replace_editor todo &&
+ git rebase -i --autosquash --update-refs base &&
+ echo "update-ref refs/heads/tip" >todo &&
+ git rebase --edit-todo &&
+ git rebase --continue
+ ) &&
+
+ # Ensure first/second/third are unchanged, but tip is updated
+ git rev-parse first second third >actual-oids &&
+ test_cmp expect-oids actual-oids &&
+ test_cmp_rev HEAD tip
+'
+
+test_expect_success '--update-refs: --edit-todo with no update-ref lines' '
+ git checkout -b test-refs-not-removed3 no-conflict-branch &&
+ git branch -f base HEAD~4 &&
+ git branch -f first HEAD~3 &&
+ git branch -f second HEAD~3 &&
+ git branch -f third HEAD~1 &&
+ git branch -f tip &&
+
+ test_commit test-refs-not-removed3 &&
+ git commit --amend --fixup first &&
+
+ git rev-parse first second third tip no-conflict-branch >expect-oids &&
+
+ (
+ set_cat_todo_editor &&
+ test_must_fail git rebase -i \
+ --autosquash --update-refs \
+ base >todo.raw &&
+ sed -e "/^update-ref/d" <todo.raw >todo
+ ) &&
+
+ # Add a break to the beginning of the todo so we can resume with no
+ # update-ref lines
+ echo "break" >todo.new &&
+ cat todo >>todo.new &&
+
+ (
+ set_replace_editor todo.new &&
+ git rebase -i --autosquash --update-refs base &&
+
+ # Make no changes when editing so update-refs is still empty
+ cat todo >todo.new &&
+ git rebase --edit-todo &&
+ git rebase --continue
+ ) &&
+
+ # Ensure refs are not deleted and their OIDs have not changed
+ git rev-parse first second third tip no-conflict-branch >actual-oids &&
+ test_cmp expect-oids actual-oids
+'
+
+test_expect_success '--update-refs: check failed ref update' '
+ test_when_finished "test_might_fail git rebase --abort" &&
+ git checkout -B update-refs-error no-conflict-branch &&
+ git branch -f base HEAD~4 &&
+ git branch -f first HEAD~3 &&
+ git branch -f second HEAD~2 &&
+ git branch -f third HEAD~1 &&
+
+ cat >fake-todo <<-EOF &&
+ pick $(git rev-parse HEAD~3)
+ break
+ update-ref refs/heads/first
+
+ pick $(git rev-parse HEAD~2)
+ update-ref refs/heads/second
+
+ pick $(git rev-parse HEAD~1)
+ update-ref refs/heads/third
+
+ pick $(git rev-parse HEAD)
+ update-ref refs/heads/no-conflict-branch
+ EOF
+
+ (
+ set_replace_editor fake-todo &&
+ git rebase -i --update-refs base
+ ) &&
+
+ # At this point, the values of first, second, and third are
+ # recorded in the update-refs file. We will force-update the
+ # "second" ref, but "git branch -f" will not work because of
+ # the lock in the update-refs file.
+ git rev-parse third >.git/refs/heads/second &&
+
+ test_must_fail git rebase --continue 2>err &&
+ grep "update_ref failed for ref '\''refs/heads/second'\''" err &&
+
+ cat >expect <<-\EOF &&
+ Updated the following refs with --update-refs:
+ refs/heads/first
+ refs/heads/no-conflict-branch
+ refs/heads/third
+ Failed to update the following refs with --update-refs:
+ refs/heads/second
+ EOF
+
+ # Clear "Rebasing (X/Y)" progress lines and drop leading tabs.
+ tail -n 6 err >err.last &&
+ sed -e "s/Rebasing.*Successfully/Successfully/g" -e "s/^\t//g" \
+ <err.last >err.trimmed &&
+ test_cmp expect err.trimmed
+'
+
+test_expect_success 'bad labels and refs rejected when parsing todo list' '
+ test_when_finished "test_might_fail git rebase --abort" &&
+ cat >todo <<-\EOF &&
+ exec >execed
+ label #
+ label :invalid
+ update-ref :bad
+ update-ref topic
+ EOF
+ rm -f execed &&
+ (
+ set_replace_editor todo &&
+ test_must_fail git rebase -i HEAD 2>err
+ ) &&
+ grep "'\''#'\'' is not a valid label" err &&
+ grep "'\'':invalid'\'' is not a valid label" err &&
+ grep "'\'':bad'\'' is not a valid refname" err &&
+ grep "update-ref requires a fully qualified refname e.g. refs/heads/topic" \
+ err &&
+ test_path_is_missing execed
+'
+
+# This must be the last test in this file
+test_expect_success '$EDITOR and friends are unchanged' '
+ test_editor_unchanged
+'
+
+test_done
diff --git a/t/t3405-rebase-malformed.sh b/t/t3405-rebase-malformed.sh
new file mode 100755
index 0000000..8979bc3
--- /dev/null
+++ b/t/t3405-rebase-malformed.sh
@@ -0,0 +1,94 @@
+#!/bin/sh
+
+test_description='rebase should handle arbitrary git message'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-rebase.sh
+
+cat >F <<\EOF
+This is an example of a commit log message
+that does not conform to git commit convention.
+
+It has two paragraphs, but its first paragraph is not friendly
+to oneline summary format.
+EOF
+
+cat >G <<\EOF
+commit log message containing a diff
+EOF
+
+
+test_expect_success setup '
+
+ >file1 &&
+ >file2 &&
+ git add file1 file2 &&
+ test_tick &&
+ git commit -m "Initial commit" &&
+ git branch diff-in-message &&
+ git branch empty-message-merge &&
+
+ git checkout -b multi-line-subject &&
+ cat F >file2 &&
+ git add file2 &&
+ test_tick &&
+ git commit -F F &&
+
+ git cat-file commit HEAD | sed -e "1,/^\$/d" >F0 &&
+
+ git checkout diff-in-message &&
+ echo "commit log message containing a diff" >G &&
+ echo "" >>G &&
+ cat G >file2 &&
+ git add file2 &&
+ git diff --cached >>G &&
+ test_tick &&
+ git commit -F G &&
+
+ git cat-file commit HEAD | sed -e "1,/^\$/d" >G0 &&
+
+ git checkout empty-message-merge &&
+ echo file3 >file3 &&
+ git add file3 &&
+ git commit --allow-empty-message -m "" &&
+
+ git checkout main &&
+
+ echo One >file1 &&
+ test_tick &&
+ git add file1 &&
+ git commit -m "Second commit"
+'
+
+test_expect_success 'rebase commit with multi-line subject' '
+
+ git rebase main multi-line-subject &&
+ git cat-file commit HEAD | sed -e "1,/^\$/d" >F1 &&
+
+ test_cmp F0 F1 &&
+ test_cmp F F0
+'
+
+test_expect_success 'rebase commit with diff in message' '
+ git rebase main diff-in-message &&
+ git cat-file commit HEAD | sed -e "1,/^$/d" >G1 &&
+ test_cmp G0 G1 &&
+ test_cmp G G0
+'
+
+test_expect_success 'rebase -m commit with empty message' '
+ git rebase -m main empty-message-merge
+'
+
+test_expect_success 'rebase -i commit with empty message' '
+ git checkout diff-in-message &&
+ set_fake_editor &&
+ test_must_fail env FAKE_COMMIT_MESSAGE=" " FAKE_LINES="reword 1" \
+ git rebase -i HEAD^
+'
+
+test_done
diff --git a/t/t3406-rebase-message.sh b/t/t3406-rebase-message.sh
new file mode 100755
index 0000000..a1d7fa7
--- /dev/null
+++ b/t/t3406-rebase-message.sh
@@ -0,0 +1,258 @@
+#!/bin/sh
+
+test_description='messages from rebase operation'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ test_commit O fileO &&
+ test_commit X fileX &&
+ git branch fast-forward &&
+ test_commit A fileA &&
+ test_commit B fileB &&
+ test_commit Y fileY &&
+
+ git checkout -b conflicts O &&
+ test_commit P &&
+ test_commit conflict-X fileX &&
+ test_commit Q &&
+
+ git checkout -b topic O &&
+ git cherry-pick A B &&
+ test_commit Z fileZ &&
+ git tag start
+'
+
+test_expect_success 'rebase -m' '
+ git rebase -m main >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'rebase against main twice' '
+ git rebase --apply main >out &&
+ test_grep "Current branch topic is up to date" out
+'
+
+test_expect_success 'rebase against main twice with --force' '
+ git rebase --force-rebase --apply main >out &&
+ test_grep "Current branch topic is up to date, rebase forced" out
+'
+
+test_expect_success 'rebase against main twice from another branch' '
+ git checkout topic^ &&
+ git rebase --apply main topic >out &&
+ test_grep "Current branch topic is up to date" out
+'
+
+test_expect_success 'rebase fast-forward to main' '
+ git checkout topic^ &&
+ git rebase --apply topic >out &&
+ test_grep "Fast-forwarded HEAD to topic" out
+'
+
+test_expect_success 'rebase --stat' '
+ git reset --hard start &&
+ git rebase --stat main >diffstat.txt &&
+ grep "^ fileX | *1 +$" diffstat.txt
+'
+
+test_expect_success 'rebase w/config rebase.stat' '
+ git reset --hard start &&
+ git config rebase.stat true &&
+ git rebase main >diffstat.txt &&
+ grep "^ fileX | *1 +$" diffstat.txt
+'
+
+test_expect_success 'rebase -n overrides config rebase.stat config' '
+ git reset --hard start &&
+ git config rebase.stat true &&
+ git rebase -n main >diffstat.txt &&
+ ! grep "^ fileX | *1 +$" diffstat.txt
+'
+
+test_expect_success 'rebase --onto outputs the invalid ref' '
+ test_must_fail git rebase --onto invalid-ref HEAD HEAD 2>err &&
+ test_grep "invalid-ref" err
+'
+
+test_expect_success 'error out early upon -C<n> or --whitespace=<bad>' '
+ test_must_fail git rebase -Cnot-a-number HEAD 2>err &&
+ test_grep "numerical value" err &&
+ test_must_fail git rebase --whitespace=bad HEAD 2>err &&
+ test_grep "Invalid whitespace option" err
+'
+
+write_reflog_expect () {
+ if test $mode = --apply
+ then
+ sed 's/(continue)/(pick)/'
+ else
+ cat
+ fi >expect
+}
+
+test_reflog () {
+ mode=$1
+ reflog_action="$2"
+
+ test_expect_success "rebase $mode reflog${reflog_action:+ GIT_REFLOG_ACTION=$reflog_action}" '
+ git checkout conflicts &&
+ test_when_finished "git reset --hard Q" &&
+
+ (
+ if test -n "$reflog_action"
+ then
+ GIT_REFLOG_ACTION="$reflog_action" &&
+ export GIT_REFLOG_ACTION
+ fi &&
+ test_must_fail git rebase $mode main &&
+ echo resolved >fileX &&
+ git add fileX &&
+ git rebase --continue
+ ) &&
+
+ git log -g --format=%gs -5 >actual &&
+ write_reflog_expect <<-EOF &&
+ ${reflog_action:-rebase} (finish): returning to refs/heads/conflicts
+ ${reflog_action:-rebase} (pick): Q
+ ${reflog_action:-rebase} (continue): conflict-X
+ ${reflog_action:-rebase} (pick): P
+ ${reflog_action:-rebase} (start): checkout main
+ EOF
+ test_cmp expect actual &&
+
+ git log -g --format=%gs -1 conflicts >actual &&
+ write_reflog_expect <<-EOF &&
+ ${reflog_action:-rebase} (finish): refs/heads/conflicts onto $(git rev-parse main)
+ EOF
+ test_cmp expect actual &&
+
+ # check there is only one new entry in the branch reflog
+ test_cmp_rev conflicts@{1} Q
+ '
+
+ test_expect_success "rebase $mode fast-forward reflog${reflog_action:+ GIT_REFLOG_ACTION=$reflog_action}" '
+ git checkout fast-forward &&
+ test_when_finished "git reset --hard X" &&
+
+ (
+ if test -n "$reflog_action"
+ then
+ GIT_REFLOG_ACTION="$reflog_action" &&
+ export GIT_REFLOG_ACTION
+ fi &&
+ git rebase $mode main
+ ) &&
+
+ git log -g --format=%gs -2 >actual &&
+ write_reflog_expect <<-EOF &&
+ ${reflog_action:-rebase} (finish): returning to refs/heads/fast-forward
+ ${reflog_action:-rebase} (start): checkout main
+ EOF
+ test_cmp expect actual &&
+
+ git log -g --format=%gs -1 fast-forward >actual &&
+ write_reflog_expect <<-EOF &&
+ ${reflog_action:-rebase} (finish): refs/heads/fast-forward onto $(git rev-parse main)
+ EOF
+ test_cmp expect actual &&
+
+ # check there is only one new entry in the branch reflog
+ test_cmp_rev fast-forward@{1} X
+ '
+
+ test_expect_success "rebase $mode --skip reflog${reflog_action:+ GIT_REFLOG_ACTION=$reflog_action}" '
+ git checkout conflicts &&
+ test_when_finished "git reset --hard Q" &&
+
+ (
+ if test -n "$reflog_action"
+ then
+ GIT_REFLOG_ACTION="$reflog_action" &&
+ export GIT_REFLOG_ACTION
+ fi &&
+ test_must_fail git rebase $mode main &&
+ git rebase --skip
+ ) &&
+
+ git log -g --format=%gs -4 >actual &&
+ write_reflog_expect <<-EOF &&
+ ${reflog_action:-rebase} (finish): returning to refs/heads/conflicts
+ ${reflog_action:-rebase} (pick): Q
+ ${reflog_action:-rebase} (pick): P
+ ${reflog_action:-rebase} (start): checkout main
+ EOF
+ test_cmp expect actual
+ '
+
+ test_expect_success "rebase $mode --abort reflog${reflog_action:+ GIT_REFLOG_ACTION=$reflog_action}" '
+ git checkout conflicts &&
+ test_when_finished "git reset --hard Q" &&
+
+ git log -g -1 conflicts >branch-expect &&
+ (
+ if test -n "$reflog_action"
+ then
+ GIT_REFLOG_ACTION="$reflog_action" &&
+ export GIT_REFLOG_ACTION
+ fi &&
+ test_must_fail git rebase $mode main &&
+ git rebase --abort
+ ) &&
+
+ git log -g --format=%gs -3 >actual &&
+ write_reflog_expect <<-EOF &&
+ ${reflog_action:-rebase} (abort): returning to refs/heads/conflicts
+ ${reflog_action:-rebase} (pick): P
+ ${reflog_action:-rebase} (start): checkout main
+ EOF
+ test_cmp expect actual &&
+
+ # check branch reflog is unchanged
+ git log -g -1 conflicts >branch-actual &&
+ test_cmp branch-expect branch-actual
+ '
+
+ test_expect_success "rebase $mode --abort detached HEAD reflog${reflog_action:+ GIT_REFLOG_ACTION=$reflog_action}" '
+ git checkout Q &&
+ test_when_finished "git reset --hard Q" &&
+
+ (
+ if test -n "$reflog_action"
+ then
+ GIT_REFLOG_ACTION="$reflog_action" &&
+ export GIT_REFLOG_ACTION
+ fi &&
+ test_must_fail git rebase $mode main &&
+ git rebase --abort
+ ) &&
+
+ git log -g --format=%gs -3 >actual &&
+ write_reflog_expect <<-EOF &&
+ ${reflog_action:-rebase} (abort): returning to $(git rev-parse Q)
+ ${reflog_action:-rebase} (pick): P
+ ${reflog_action:-rebase} (start): checkout main
+ EOF
+ test_cmp expect actual
+ '
+}
+
+test_reflog --merge
+test_reflog --merge my-reflog-action
+test_reflog --apply
+test_reflog --apply my-reflog-action
+
+test_expect_success 'rebase -i onto unrelated history' '
+ git init unrelated &&
+ test_commit -C unrelated 1 &&
+ git -C unrelated remote add -f origin "$PWD" &&
+ git -C unrelated branch --set-upstream-to=origin/main &&
+ git -C unrelated -c core.editor=true rebase -i -v --stat >actual &&
+ test_grep "Changes to " actual &&
+ test_grep "5 files changed" actual
+'
+
+test_done
diff --git a/t/t3407-rebase-abort.sh b/t/t3407-rebase-abort.sh
new file mode 100755
index 0000000..ebbaed1
--- /dev/null
+++ b/t/t3407-rebase-abort.sh
@@ -0,0 +1,116 @@
+#!/bin/sh
+
+test_description='git rebase --abort tests'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success setup '
+ test_commit a a a &&
+ git branch to-rebase &&
+
+ test_commit --annotate b a b &&
+ test_commit --annotate c a c &&
+
+ git checkout to-rebase &&
+ test_commit "merge should fail on this" a d d &&
+ test_commit --annotate "merge should fail on this, too" a e pre-rebase
+'
+
+# Check that HEAD is equal to "pre-rebase" and the current branch is
+# "to-rebase"
+check_head() {
+ test_cmp_rev HEAD pre-rebase^{commit} &&
+ test "$(git symbolic-ref HEAD)" = refs/heads/to-rebase
+}
+
+testrebase() {
+ type=$1
+ state_dir=$2
+
+ test_expect_success "rebase$type --abort" '
+ # Clean up the state from the previous one
+ git reset --hard pre-rebase &&
+ test_must_fail git rebase$type main &&
+ test_path_is_dir "$state_dir" &&
+ git rebase --abort &&
+ check_head &&
+ test_path_is_missing "$state_dir"
+ '
+
+ test_expect_success "rebase$type --abort after --skip" '
+ # Clean up the state from the previous one
+ git reset --hard pre-rebase &&
+ test_must_fail git rebase$type main &&
+ test_path_is_dir "$state_dir" &&
+ test_must_fail git rebase --skip &&
+ test_cmp_rev HEAD main &&
+ git rebase --abort &&
+ check_head &&
+ test_path_is_missing "$state_dir"
+ '
+
+ test_expect_success "rebase$type --abort after --continue" '
+ # Clean up the state from the previous one
+ git reset --hard pre-rebase &&
+ test_must_fail git rebase$type main &&
+ test_path_is_dir "$state_dir" &&
+ echo c > a &&
+ echo d >> a &&
+ git add a &&
+ test_must_fail git rebase --continue &&
+ test_cmp_rev ! HEAD main &&
+ git rebase --abort &&
+ check_head &&
+ test_path_is_missing "$state_dir"
+ '
+
+ test_expect_success "rebase$type --abort when checking out a tag" '
+ test_when_finished "git symbolic-ref HEAD refs/heads/to-rebase" &&
+ git reset --hard a -- &&
+ test_must_fail git rebase$type --onto b c pre-rebase &&
+ test_cmp_rev HEAD b^{commit} &&
+ git rebase --abort &&
+ test_cmp_rev HEAD pre-rebase^{commit} &&
+ ! git symbolic-ref HEAD
+ '
+
+ test_expect_success "rebase$type --abort does not update reflog" '
+ # Clean up the state from the previous one
+ git reset --hard pre-rebase &&
+ git reflog show to-rebase > reflog_before &&
+ test_must_fail git rebase$type main &&
+ git rebase --abort &&
+ git reflog show to-rebase > reflog_after &&
+ test_cmp reflog_before reflog_after &&
+ rm reflog_before reflog_after
+ '
+
+ test_expect_success 'rebase --abort can not be used with other options' '
+ # Clean up the state from the previous one
+ git reset --hard pre-rebase &&
+ test_must_fail git rebase$type main &&
+ test_must_fail git rebase -v --abort &&
+ test_must_fail git rebase --abort -v &&
+ git rebase --abort
+ '
+
+ test_expect_success "rebase$type --quit" '
+ test_when_finished "git symbolic-ref HEAD refs/heads/to-rebase" &&
+ # Clean up the state from the previous one
+ git reset --hard pre-rebase &&
+ test_must_fail git rebase$type main &&
+ test_path_is_dir $state_dir &&
+ head_before=$(git rev-parse HEAD) &&
+ git rebase --quit &&
+ test_cmp_rev HEAD $head_before &&
+ test_path_is_missing .git/rebase-apply
+ '
+}
+
+testrebase " --apply" .git/rebase-apply
+testrebase " --merge" .git/rebase-merge
+
+test_done
diff --git a/t/t3408-rebase-multi-line.sh b/t/t3408-rebase-multi-line.sh
new file mode 100755
index 0000000..7b4607d
--- /dev/null
+++ b/t/t3408-rebase-multi-line.sh
@@ -0,0 +1,59 @@
+#!/bin/sh
+
+test_description='rebasing a commit with multi-line first paragraph.'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+
+ >file &&
+ git add file &&
+ test_tick &&
+ git commit -m initial &&
+
+ echo hello >file &&
+ test_tick &&
+ git commit -a -m "A sample commit log message that has a long
+summary that spills over multiple lines.
+
+But otherwise with a sane description." &&
+
+ git branch side &&
+
+ git reset --hard HEAD^ &&
+ >elif &&
+ git add elif &&
+ test_tick &&
+ git commit -m second &&
+
+ git checkout -b side2 &&
+ >afile &&
+ git add afile &&
+ test_tick &&
+ git commit -m third &&
+ echo hello >afile &&
+ test_tick &&
+ git commit -a -m fourth &&
+ git checkout -b side-merge &&
+ git reset --hard HEAD^^ &&
+ git merge --no-ff -m "A merge commit log message that has a long
+summary that spills over multiple lines.
+
+But otherwise with a sane description." side2 &&
+ git branch side-merge-original
+'
+
+test_expect_success rebase '
+
+ git checkout side &&
+ git rebase main &&
+ git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
+ git cat-file commit side@{1} | sed -e "1,/^\$/d" >expect &&
+ test_cmp expect actual
+
+'
+test_done
diff --git a/t/t3409-rebase-environ.sh b/t/t3409-rebase-environ.sh
new file mode 100755
index 0000000..acaf555
--- /dev/null
+++ b/t/t3409-rebase-environ.sh
@@ -0,0 +1,24 @@
+#!/bin/sh
+
+test_description='git rebase interactive environment'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ test_commit one &&
+ test_commit two &&
+ test_commit three
+'
+
+test_expect_success 'rebase --exec does not muck with GIT_DIR' '
+ git rebase --exec "printf %s \$GIT_DIR >environ" HEAD~1 &&
+ test_must_be_empty environ
+'
+
+test_expect_success 'rebase --exec does not muck with GIT_WORK_TREE' '
+ git rebase --exec "printf %s \$GIT_WORK_TREE >environ" HEAD~1 &&
+ test_must_be_empty environ
+'
+
+test_done
diff --git a/t/t3412-rebase-root.sh b/t/t3412-rebase-root.sh
new file mode 100755
index 0000000..e75b3d0
--- /dev/null
+++ b/t/t3412-rebase-root.sh
@@ -0,0 +1,242 @@
+#!/bin/sh
+
+test_description='git rebase --root
+
+Tests if git rebase --root --onto <newparent> can rebase the root commit.
+'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+log_with_names () {
+ git rev-list --topo-order --parents --pretty="tformat:%s" HEAD |
+ git name-rev --annotate-stdin --name-only --refs=refs/heads/$1
+}
+
+
+test_expect_success 'prepare repository' '
+ test_commit 1 A &&
+ test_commit 2 A &&
+ git symbolic-ref HEAD refs/heads/other &&
+ rm .git/index &&
+ test_commit 3 B &&
+ test_commit 1b A 1 &&
+ test_commit 4 B
+'
+
+test_expect_success 'rebase --root fails with too many args' '
+ git checkout -B fail other &&
+ test_must_fail git rebase --onto main --root fail fail
+'
+
+test_expect_success 'setup pre-rebase hook' '
+ test_hook --setup pre-rebase <<-\EOF
+ echo "$1,$2" >.git/PRE-REBASE-INPUT
+ EOF
+'
+cat > expect <<EOF
+4
+3
+2
+1
+EOF
+
+test_expect_success 'rebase --root --onto <newbase>' '
+ git checkout -b work other &&
+ git rebase --root --onto main &&
+ git log --pretty=tformat:"%s" > rebased &&
+ test_cmp expect rebased
+'
+
+test_expect_success 'pre-rebase got correct input (1)' '
+ test "z$(cat .git/PRE-REBASE-INPUT)" = z--root,
+'
+
+test_expect_success 'rebase --root --onto <newbase> <branch>' '
+ git branch work2 other &&
+ git rebase --root --onto main work2 &&
+ git log --pretty=tformat:"%s" > rebased2 &&
+ test_cmp expect rebased2
+'
+
+test_expect_success 'pre-rebase got correct input (2)' '
+ test "z$(cat .git/PRE-REBASE-INPUT)" = z--root,work2
+'
+
+test_expect_success 'rebase -i --root --onto <newbase>' '
+ git checkout -b work3 other &&
+ git rebase -i --root --onto main &&
+ git log --pretty=tformat:"%s" > rebased3 &&
+ test_cmp expect rebased3
+'
+
+test_expect_success 'pre-rebase got correct input (3)' '
+ test "z$(cat .git/PRE-REBASE-INPUT)" = z--root,
+'
+
+test_expect_success 'rebase -i --root --onto <newbase> <branch>' '
+ git branch work4 other &&
+ git rebase -i --root --onto main work4 &&
+ git log --pretty=tformat:"%s" > rebased4 &&
+ test_cmp expect rebased4
+'
+
+test_expect_success 'pre-rebase got correct input (4)' '
+ test "z$(cat .git/PRE-REBASE-INPUT)" = z--root,work4
+'
+
+test_expect_success 'set up merge history' '
+ git checkout other^ &&
+ git checkout -b side &&
+ test_commit 5 C &&
+ git checkout other &&
+ git merge side
+'
+
+cat > expect-side <<'EOF'
+commit work6 work6~1 work6^2
+Merge branch 'side' into other
+commit work6^2 work6~2
+5
+commit work6~1 work6~2
+4
+commit work6~2 work6~3
+3
+commit work6~3 work6~4
+2
+commit work6~4
+1
+EOF
+
+test_expect_success 'set up second root and merge' '
+ git symbolic-ref HEAD refs/heads/third &&
+ rm .git/index &&
+ rm A B C &&
+ test_commit 6 D &&
+ git checkout other &&
+ git merge --allow-unrelated-histories third
+'
+
+cat > expect-third <<'EOF'
+commit work7 work7~1 work7^2
+Merge branch 'third' into other
+commit work7^2 work7~4
+6
+commit work7~1 work7~2 work7~1^2
+Merge branch 'side' into other
+commit work7~1^2 work7~3
+5
+commit work7~2 work7~3
+4
+commit work7~3 work7~4
+3
+commit work7~4 work7~5
+2
+commit work7~5
+1
+EOF
+
+test_expect_success 'setup pre-rebase hook that fails' '
+ test_hook --setup --clobber pre-rebase <<-\EOF
+ false
+ EOF
+'
+
+test_expect_success 'pre-rebase hook stops rebase' '
+ git checkout -b stops1 other &&
+ test_must_fail git rebase --root --onto main &&
+ test "z$(git symbolic-ref HEAD)" = zrefs/heads/stops1 &&
+ test 0 = $(git rev-list other...stops1 | wc -l)
+'
+
+test_expect_success 'pre-rebase hook stops rebase -i' '
+ git checkout -b stops2 other &&
+ test_must_fail git rebase --root --onto main &&
+ test "z$(git symbolic-ref HEAD)" = zrefs/heads/stops2 &&
+ test 0 = $(git rev-list other...stops2 | wc -l)
+'
+
+test_expect_success 'remove pre-rebase hook' '
+ rm -f .git/hooks/pre-rebase
+'
+
+test_expect_success 'set up a conflict' '
+ git checkout main &&
+ echo conflict > B &&
+ git add B &&
+ git commit -m conflict
+'
+
+test_expect_success 'rebase --root with conflict (first part)' '
+ git checkout -b conflict1 other &&
+ test_must_fail git rebase --root --onto main &&
+ git ls-files -u | grep "B$"
+'
+
+test_expect_success 'fix the conflict' '
+ echo 3 > B &&
+ git add B
+'
+
+cat > expect-conflict <<EOF
+6
+5
+4
+3
+conflict
+2
+1
+EOF
+
+test_expect_success 'rebase --root with conflict (second part)' '
+ git rebase --continue &&
+ git log --pretty=tformat:"%s" > conflict1 &&
+ test_cmp expect-conflict conflict1
+'
+
+test_expect_success 'rebase -i --root with conflict (first part)' '
+ git checkout -b conflict2 other &&
+ test_must_fail git rebase -i --root --onto main &&
+ git ls-files -u | grep "B$"
+'
+
+test_expect_success 'fix the conflict' '
+ echo 3 > B &&
+ git add B
+'
+
+test_expect_success 'rebase -i --root with conflict (second part)' '
+ git rebase --continue &&
+ git log --pretty=tformat:"%s" > conflict2 &&
+ test_cmp expect-conflict conflict2
+'
+
+cat >expect-conflict-p <<\EOF
+commit conflict3 conflict3~1 conflict3^2
+Merge branch 'third' into other
+commit conflict3^2 conflict3~4
+6
+commit conflict3~1 conflict3~2 conflict3~1^2
+Merge branch 'side' into other
+commit conflict3~1^2 conflict3~3
+5
+commit conflict3~2 conflict3~3
+4
+commit conflict3~3 conflict3~4
+3
+commit conflict3~4 conflict3~5
+conflict
+commit conflict3~5 conflict3~6
+2
+commit conflict3~6
+1
+EOF
+
+test_expect_success 'fix the conflict' '
+ echo 3 > B &&
+ git add B
+'
+
+test_done
diff --git a/t/t3413-rebase-hook.sh b/t/t3413-rebase-hook.sh
new file mode 100755
index 0000000..e845683
--- /dev/null
+++ b/t/t3413-rebase-hook.sh
@@ -0,0 +1,140 @@
+#!/bin/sh
+
+test_description='git rebase with its hook(s)'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ echo hello >file &&
+ git add file &&
+ test_tick &&
+ git commit -m initial &&
+ echo goodbye >file &&
+ git add file &&
+ test_tick &&
+ git commit -m second &&
+ git checkout -b side HEAD^ &&
+ echo world >git &&
+ git add git &&
+ test_tick &&
+ git commit -m side &&
+ git checkout main &&
+ git log --pretty=oneline --abbrev-commit --graph --all &&
+ git branch test side
+'
+
+test_expect_success 'rebase' '
+ git checkout test &&
+ git reset --hard side &&
+ git rebase main &&
+ test "z$(cat git)" = zworld
+'
+
+test_expect_success 'rebase -i' '
+ git checkout test &&
+ git reset --hard side &&
+ EDITOR=true git rebase -i main &&
+ test "z$(cat git)" = zworld
+'
+
+test_expect_success 'setup pre-rebase hook' '
+ test_hook --setup pre-rebase <<-\EOF
+ echo "$1,$2" >.git/PRE-REBASE-INPUT
+ EOF
+'
+
+test_expect_success 'pre-rebase hook gets correct input (1)' '
+ git checkout test &&
+ git reset --hard side &&
+ git rebase main &&
+ test "z$(cat git)" = zworld &&
+ test "z$(cat .git/PRE-REBASE-INPUT)" = zmain,
+
+'
+
+test_expect_success 'pre-rebase hook gets correct input (2)' '
+ git checkout test &&
+ git reset --hard side &&
+ git rebase main test &&
+ test "z$(cat git)" = zworld &&
+ test "z$(cat .git/PRE-REBASE-INPUT)" = zmain,test
+'
+
+test_expect_success 'pre-rebase hook gets correct input (3)' '
+ git checkout test &&
+ git reset --hard side &&
+ git checkout main &&
+ git rebase main test &&
+ test "z$(cat git)" = zworld &&
+ test "z$(cat .git/PRE-REBASE-INPUT)" = zmain,test
+'
+
+test_expect_success 'pre-rebase hook gets correct input (4)' '
+ git checkout test &&
+ git reset --hard side &&
+ EDITOR=true git rebase -i main &&
+ test "z$(cat git)" = zworld &&
+ test "z$(cat .git/PRE-REBASE-INPUT)" = zmain,
+
+'
+
+test_expect_success 'pre-rebase hook gets correct input (5)' '
+ git checkout test &&
+ git reset --hard side &&
+ EDITOR=true git rebase -i main test &&
+ test "z$(cat git)" = zworld &&
+ test "z$(cat .git/PRE-REBASE-INPUT)" = zmain,test
+'
+
+test_expect_success 'pre-rebase hook gets correct input (6)' '
+ git checkout test &&
+ git reset --hard side &&
+ git checkout main &&
+ EDITOR=true git rebase -i main test &&
+ test "z$(cat git)" = zworld &&
+ test "z$(cat .git/PRE-REBASE-INPUT)" = zmain,test
+'
+
+test_expect_success 'setup pre-rebase hook that fails' '
+ test_hook --setup --clobber pre-rebase <<-\EOF
+ false
+ EOF
+'
+
+test_expect_success 'pre-rebase hook stops rebase (1)' '
+ git checkout test &&
+ git reset --hard side &&
+ test_must_fail git rebase main &&
+ test "z$(git symbolic-ref HEAD)" = zrefs/heads/test &&
+ test 0 = $(git rev-list HEAD...side | wc -l)
+'
+
+test_expect_success 'pre-rebase hook stops rebase (2)' '
+ git checkout test &&
+ git reset --hard side &&
+ test_must_fail env EDITOR=: git rebase -i main &&
+ test "z$(git symbolic-ref HEAD)" = zrefs/heads/test &&
+ test 0 = $(git rev-list HEAD...side | wc -l)
+'
+
+test_expect_success 'rebase --no-verify overrides pre-rebase (1)' '
+ git checkout test &&
+ git reset --hard side &&
+ git rebase --no-verify main &&
+ test "z$(git symbolic-ref HEAD)" = zrefs/heads/test &&
+ test "z$(cat git)" = zworld
+'
+
+test_expect_success 'rebase --no-verify overrides pre-rebase (2)' '
+ git checkout test &&
+ git reset --hard side &&
+ EDITOR=true git rebase --no-verify -i main &&
+ test "z$(git symbolic-ref HEAD)" = zrefs/heads/test &&
+ test "z$(cat git)" = zworld
+'
+
+test_done
diff --git a/t/t3415-rebase-autosquash.sh b/t/t3415-rebase-autosquash.sh
new file mode 100755
index 0000000..a364530
--- /dev/null
+++ b/t/t3415-rebase-autosquash.sh
@@ -0,0 +1,469 @@
+#!/bin/sh
+
+test_description='auto squash'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+. "$TEST_DIRECTORY"/lib-rebase.sh
+
+test_expect_success setup '
+ echo 0 >file0 &&
+ git add . &&
+ test_tick &&
+ git commit -m "initial commit" &&
+ echo 0 >file1 &&
+ echo 2 >file2 &&
+ git add . &&
+ test_tick &&
+ git commit -m "first commit" &&
+ git tag first-commit &&
+ echo 3 >file3 &&
+ git add . &&
+ test_tick &&
+ git commit -m "second commit" &&
+ git tag base
+'
+
+test_auto_fixup () {
+ no_squash= &&
+ if test "x$1" = 'x!'
+ then
+ no_squash=true
+ shift
+ fi &&
+
+ git reset --hard base &&
+ echo 1 >file1 &&
+ git add -u &&
+ test_tick &&
+ git commit -m "fixup! first" &&
+
+ git tag $1 &&
+ test_tick &&
+ git rebase $2 -i HEAD^^^ &&
+ git log --oneline >actual &&
+ if test -n "$no_squash"
+ then
+ test_line_count = 4 actual
+ else
+ test_line_count = 3 actual &&
+ git diff --exit-code $1 &&
+ echo 1 >expect &&
+ git cat-file blob HEAD^:file1 >actual &&
+ test_cmp expect actual &&
+ git cat-file commit HEAD^ >commit &&
+ grep first commit >actual &&
+ test_line_count = 1 actual
+ fi
+}
+
+test_expect_success 'auto fixup (option)' '
+ test_auto_fixup final-fixup-option --autosquash
+'
+
+test_expect_success 'auto fixup (config)' '
+ git config rebase.autosquash true &&
+ test_auto_fixup final-fixup-config-true &&
+ test_auto_fixup ! fixup-config-true-no --no-autosquash &&
+ git config rebase.autosquash false &&
+ test_auto_fixup ! final-fixup-config-false
+'
+
+test_auto_squash () {
+ no_squash= &&
+ if test "x$1" = 'x!'
+ then
+ no_squash=true
+ shift
+ fi &&
+
+ git reset --hard base &&
+ echo 1 >file1 &&
+ git add -u &&
+ test_tick &&
+ git commit -m "squash! first" -m "extra para for first" &&
+ git tag $1 &&
+ test_tick &&
+ git rebase $2 -i HEAD^^^ &&
+ git log --oneline >actual &&
+ if test -n "$no_squash"
+ then
+ test_line_count = 4 actual
+ else
+ test_line_count = 3 actual &&
+ git diff --exit-code $1 &&
+ echo 1 >expect &&
+ git cat-file blob HEAD^:file1 >actual &&
+ test_cmp expect actual &&
+ git cat-file commit HEAD^ >commit &&
+ grep first commit >actual &&
+ test_line_count = 2 actual
+ fi
+}
+
+test_expect_success 'auto squash (option)' '
+ test_auto_squash final-squash --autosquash
+'
+
+test_expect_success 'auto squash (config)' '
+ git config rebase.autosquash true &&
+ test_auto_squash final-squash-config-true &&
+ test_auto_squash ! squash-config-true-no --no-autosquash &&
+ git config rebase.autosquash false &&
+ test_auto_squash ! final-squash-config-false
+'
+
+test_expect_success 'misspelled auto squash' '
+ git reset --hard base &&
+ echo 1 >file1 &&
+ git add -u &&
+ test_tick &&
+ git commit -m "squash! forst" &&
+ git tag final-missquash &&
+ test_tick &&
+ git rebase --autosquash -i HEAD^^^ &&
+ git log --oneline >actual &&
+ test_line_count = 4 actual &&
+ git diff --exit-code final-missquash &&
+ git rev-list final-missquash...HEAD >list &&
+ test_must_be_empty list
+'
+
+test_expect_success 'auto squash that matches 2 commits' '
+ git reset --hard base &&
+ echo 4 >file4 &&
+ git add file4 &&
+ test_tick &&
+ git commit -m "first new commit" &&
+ echo 1 >file1 &&
+ git add -u &&
+ test_tick &&
+ git commit -m "squash! first" -m "extra para for first" &&
+ git tag final-multisquash &&
+ test_tick &&
+ git rebase --autosquash -i HEAD~4 &&
+ git log --oneline >actual &&
+ test_line_count = 4 actual &&
+ git diff --exit-code final-multisquash &&
+ echo 1 >expect &&
+ git cat-file blob HEAD^^:file1 >actual &&
+ test_cmp expect actual &&
+ git cat-file commit HEAD^^ >commit &&
+ grep first commit >actual &&
+ test_line_count = 2 actual &&
+ git cat-file commit HEAD >commit &&
+ grep first commit >actual &&
+ test_line_count = 1 actual
+'
+
+test_expect_success 'auto squash that matches a commit after the squash' '
+ git reset --hard base &&
+ echo 1 >file1 &&
+ git add -u &&
+ test_tick &&
+ git commit -m "squash! third" &&
+ echo 4 >file4 &&
+ git add file4 &&
+ test_tick &&
+ git commit -m "third commit" &&
+ git tag final-presquash &&
+ test_tick &&
+ git rebase --autosquash -i HEAD~4 &&
+ git log --oneline >actual &&
+ test_line_count = 5 actual &&
+ git diff --exit-code final-presquash &&
+ echo 0 >expect &&
+ git cat-file blob HEAD^^:file1 >actual &&
+ test_cmp expect actual &&
+ echo 1 >expect &&
+ git cat-file blob HEAD^:file1 >actual &&
+ test_cmp expect actual &&
+ git cat-file commit HEAD >commit &&
+ grep third commit >actual &&
+ test_line_count = 1 actual &&
+ git cat-file commit HEAD^ >commit &&
+ grep third commit >actual &&
+ test_line_count = 1 actual
+'
+test_expect_success 'auto squash that matches a sha1' '
+ git reset --hard base &&
+ echo 1 >file1 &&
+ git add -u &&
+ test_tick &&
+ oid=$(git rev-parse --short HEAD^) &&
+ git commit -m "squash! $oid" -m "extra para" &&
+ git tag final-shasquash &&
+ test_tick &&
+ git rebase --autosquash -i HEAD^^^ &&
+ git log --oneline >actual &&
+ test_line_count = 3 actual &&
+ git diff --exit-code final-shasquash &&
+ echo 1 >expect &&
+ git cat-file blob HEAD^:file1 >actual &&
+ test_cmp expect actual &&
+ git cat-file commit HEAD^ >commit &&
+ ! grep "squash" commit &&
+ grep "^extra para" commit >actual &&
+ test_line_count = 1 actual
+'
+
+test_expect_success 'auto squash that matches longer sha1' '
+ git reset --hard base &&
+ echo 1 >file1 &&
+ git add -u &&
+ test_tick &&
+ oid=$(git rev-parse --short=11 HEAD^) &&
+ git commit -m "squash! $oid" -m "extra para" &&
+ git tag final-longshasquash &&
+ test_tick &&
+ git rebase --autosquash -i HEAD^^^ &&
+ git log --oneline >actual &&
+ test_line_count = 3 actual &&
+ git diff --exit-code final-longshasquash &&
+ echo 1 >expect &&
+ git cat-file blob HEAD^:file1 >actual &&
+ test_cmp expect actual &&
+ git cat-file commit HEAD^ >commit &&
+ ! grep "squash" commit &&
+ grep "^extra para" commit >actual &&
+ test_line_count = 1 actual
+'
+
+test_expect_success 'auto squash of fixup commit that matches branch name which points back to fixup commit' '
+ git reset --hard base &&
+ git commit --allow-empty -m "fixup! self-cycle" &&
+ git branch self-cycle &&
+ GIT_SEQUENCE_EDITOR="cat >tmp" git rebase --autosquash -i HEAD^^ &&
+ sed -ne "/^[^#]/{s/[0-9a-f]\{7,\}/HASH/g;p;}" tmp >actual &&
+ cat <<-EOF >expect &&
+ pick HASH second commit
+ pick HASH fixup! self-cycle # empty
+ EOF
+ test_cmp expect actual
+'
+
+test_auto_commit_flags () {
+ git reset --hard base &&
+ echo 1 >file1 &&
+ git add -u &&
+ test_tick &&
+ git commit --$1 first-commit -m "extra para for first" &&
+ git tag final-commit-$1 &&
+ test_tick &&
+ git rebase --autosquash -i HEAD^^^ &&
+ git log --oneline >actual &&
+ test_line_count = 3 actual &&
+ git diff --exit-code final-commit-$1 &&
+ echo 1 >expect &&
+ git cat-file blob HEAD^:file1 >actual &&
+ test_cmp expect actual &&
+ git cat-file commit HEAD^ >commit &&
+ grep first commit >actual &&
+ test_line_count = $2 actual
+}
+
+test_expect_success 'use commit --fixup' '
+ test_auto_commit_flags fixup 1
+'
+
+test_expect_success 'use commit --squash' '
+ test_auto_commit_flags squash 2
+'
+
+test_auto_fixup_fixup () {
+ git reset --hard base &&
+ echo 1 >file1 &&
+ git add -u &&
+ test_tick &&
+ git commit -m "$1! first" -m "extra para for first" &&
+ echo 2 >file1 &&
+ git add -u &&
+ test_tick &&
+ git commit -m "$1! $2! first" -m "second extra para for first" &&
+ git tag "final-$1-$2" &&
+ test_tick &&
+ (
+ set_cat_todo_editor &&
+ test_must_fail git rebase --autosquash -i HEAD^^^^ >actual &&
+ head=$(git rev-parse --short HEAD) &&
+ parent1=$(git rev-parse --short HEAD^) &&
+ parent2=$(git rev-parse --short HEAD^^) &&
+ parent3=$(git rev-parse --short HEAD^^^) &&
+ cat >expected <<-EOF &&
+ pick $parent3 first commit
+ $1 $parent1 $1! first
+ $1 $head $1! $2! first
+ pick $parent2 second commit
+ EOF
+ test_cmp expected actual
+ ) &&
+ git rebase --autosquash -i HEAD^^^^ &&
+ git log --oneline >actual &&
+ test_line_count = 3 actual
+ git diff --exit-code "final-$1-$2" &&
+ echo 2 >expect &&
+ git cat-file blob HEAD^:file1 >actual &&
+ test_cmp expect actual &&
+ git cat-file commit HEAD^ >commit &&
+ grep first commit >actual &&
+ if test "$1" = "fixup"
+ then
+ test_line_count = 1 actual
+ elif test "$1" = "squash"
+ then
+ test_line_count = 3 actual
+ else
+ false
+ fi
+}
+
+test_expect_success 'fixup! fixup!' '
+ test_auto_fixup_fixup fixup fixup
+'
+
+test_expect_success 'fixup! squash!' '
+ test_auto_fixup_fixup fixup squash
+'
+
+test_expect_success 'squash! squash!' '
+ test_auto_fixup_fixup squash squash
+'
+
+test_expect_success 'squash! fixup!' '
+ test_auto_fixup_fixup squash fixup
+'
+
+test_expect_success 'autosquash with custom inst format' '
+ git reset --hard base &&
+ git config --add rebase.instructionFormat "[%an @ %ar] %s" &&
+ echo 2 >file1 &&
+ git add -u &&
+ test_tick &&
+ oid=$(git rev-parse --short HEAD^) &&
+ git commit -m "squash! $oid" -m "extra para for first" &&
+ echo 1 >file1 &&
+ git add -u &&
+ test_tick &&
+ subject=$(git log -n 1 --format=%s HEAD~2) &&
+ git commit -m "squash! $subject" -m "second extra para for first" &&
+ git tag final-squash-instFmt &&
+ test_tick &&
+ git rebase --autosquash -i HEAD~4 &&
+ git log --oneline >actual &&
+ test_line_count = 3 actual &&
+ git diff --exit-code final-squash-instFmt &&
+ echo 1 >expect &&
+ git cat-file blob HEAD^:file1 >actual &&
+ test_cmp expect actual &&
+ git cat-file commit HEAD^ >commit &&
+ ! grep "squash" commit &&
+ grep first commit >actual &&
+ test_line_count = 3 actual
+'
+
+test_expect_success 'autosquash with empty custom instructionFormat' '
+ git reset --hard base &&
+ test_commit empty-instructionFormat-test &&
+ (
+ set_cat_todo_editor &&
+ test_must_fail git -c rebase.instructionFormat= \
+ rebase --autosquash --force-rebase -i HEAD^ >actual &&
+ git log -1 --format="pick %h %s" >expect &&
+ test_cmp expect actual
+ )
+'
+
+set_backup_editor () {
+ write_script backup-editor.sh <<-\EOF
+ cp "$1" .git/backup-"$(basename "$1")"
+ EOF
+ test_set_editor "$PWD/backup-editor.sh"
+}
+
+test_expect_success 'autosquash with multiple empty patches' '
+ test_tick &&
+ git commit --allow-empty -m "empty" &&
+ test_tick &&
+ git commit --allow-empty -m "empty2" &&
+ test_tick &&
+ >fixup &&
+ git add fixup &&
+ git commit --fixup HEAD^^ &&
+ (
+ set_backup_editor &&
+ GIT_USE_REBASE_HELPER=false \
+ git rebase -i --force-rebase --autosquash HEAD~4 &&
+ grep empty2 .git/backup-git-rebase-todo
+ )
+'
+
+test_expect_success 'extra spaces after fixup!' '
+ base=$(git rev-parse HEAD) &&
+ test_commit to-fixup &&
+ git commit --allow-empty -m "fixup! to-fixup" &&
+ git rebase -i --autosquash --keep-empty HEAD~2 &&
+ parent=$(git rev-parse HEAD^) &&
+ test $base = $parent
+'
+
+test_expect_success 'wrapped original subject' '
+ if test -d .git/rebase-merge; then git rebase --abort; fi &&
+ base=$(git rev-parse HEAD) &&
+ echo "wrapped subject" >wrapped &&
+ git add wrapped &&
+ test_tick &&
+ git commit --allow-empty -m "$(printf "To\nfixup")" &&
+ test_tick &&
+ git commit --allow-empty -m "fixup! To fixup" &&
+ git rebase -i --autosquash --keep-empty HEAD~2 &&
+ parent=$(git rev-parse HEAD^) &&
+ test $base = $parent
+'
+
+test_expect_success 'abort last squash' '
+ test_when_finished "test_might_fail git rebase --abort" &&
+ test_when_finished "git checkout main" &&
+
+ git checkout -b some-squashes &&
+ git commit --allow-empty -m first &&
+ git commit --allow-empty --squash HEAD &&
+ git commit --allow-empty -m second &&
+ git commit --allow-empty --squash HEAD &&
+
+ test_must_fail git -c core.editor="grep -q ^pick" \
+ rebase -ki --autosquash HEAD~4 &&
+ : do not finish the squash, but resolve it manually &&
+ git commit --allow-empty --amend -m edited-first &&
+ git rebase --skip &&
+ git show >actual &&
+ ! grep first actual
+'
+
+test_expect_success 'fixup a fixup' '
+ echo 0to-fixup >file0 &&
+ test_tick &&
+ git commit -m "to-fixup" file0 &&
+ test_tick &&
+ git commit --squash HEAD -m X --allow-empty &&
+ test_tick &&
+ git commit --squash HEAD^ -m Y --allow-empty &&
+ test_tick &&
+ git commit -m "squash! $(git rev-parse HEAD^)" -m Z --allow-empty &&
+ test_tick &&
+ git commit -m "squash! $(git rev-parse HEAD^^)" -m W --allow-empty &&
+ git rebase -ki --autosquash HEAD~5 &&
+ test XZWY = $(git show | tr -cd W-Z)
+'
+
+test_expect_success 'fixup does not clean up commit message' '
+ oneline="#818" &&
+ git commit --allow-empty -m "$oneline" &&
+ git commit --fixup HEAD --allow-empty &&
+ git -c commit.cleanup=strip rebase -ki --autosquash HEAD~2 &&
+ test "$oneline" = "$(git show -s --format=%s)"
+'
+
+test_done
diff --git a/t/t3416-rebase-onto-threedots.sh b/t/t3416-rebase-onto-threedots.sh
new file mode 100755
index 0000000..f8c4ed7
--- /dev/null
+++ b/t/t3416-rebase-onto-threedots.sh
@@ -0,0 +1,229 @@
+#!/bin/sh
+
+test_description='git rebase --onto A...B'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY/lib-rebase.sh"
+
+# Rebase only the tip commit of "topic" on merge base between "main"
+# and "topic". Cannot do this for "side" with "main" because there
+# is no single merge base.
+#
+#
+# F---G topic G'
+# / /
+# A---B---C---D---E main --> A---B---C---D---E
+# \ \ /
+# \ x
+# \ / \
+# H---I---J---K side
+
+test_expect_success setup '
+ test_commit A &&
+ test_commit B &&
+ git branch side &&
+ test_commit C &&
+ git branch topic &&
+ git checkout side &&
+ test_commit H &&
+ git checkout main &&
+ test_tick &&
+ git merge H &&
+ git tag D &&
+ test_commit E &&
+ git checkout topic &&
+ test_commit F &&
+ test_commit G &&
+ git checkout side &&
+ test_tick &&
+ git merge C &&
+ git tag I &&
+ test_commit J &&
+ test_commit K
+'
+
+test_expect_success 'rebase --onto main...topic' '
+ git reset --hard &&
+ git checkout topic &&
+ git reset --hard G &&
+
+ git rebase --onto main...topic F &&
+ git rev-parse HEAD^1 >actual &&
+ git rev-parse C^0 >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rebase --onto main...' '
+ git reset --hard &&
+ git checkout topic &&
+ git reset --hard G &&
+
+ git rebase --onto main... F &&
+ git rev-parse HEAD^1 >actual &&
+ git rev-parse C^0 >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rebase --onto main...side' '
+ git reset --hard &&
+ git checkout side &&
+ git reset --hard K &&
+
+ test_must_fail git rebase --onto main...side J
+'
+
+test_expect_success 'rebase -i --onto main...topic' '
+ git reset --hard &&
+ git checkout topic &&
+ git reset --hard G &&
+ (
+ set_fake_editor &&
+ EXPECT_COUNT=1 git rebase -i --onto main...topic F
+ ) &&
+ git rev-parse HEAD^1 >actual &&
+ git rev-parse C^0 >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rebase -i --onto main...' '
+ git reset --hard &&
+ git checkout topic &&
+ git reset --hard G &&
+ (
+ set_fake_editor &&
+ EXPECT_COUNT=1 git rebase -i --onto main... F
+ ) &&
+ git rev-parse HEAD^1 >actual &&
+ git rev-parse C^0 >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rebase --onto main...side requires a single merge-base' '
+ git reset --hard &&
+ git checkout side &&
+ git reset --hard K &&
+
+ test_must_fail git rebase -i --onto main...side J 2>err &&
+ grep "need exactly one merge base" err
+'
+
+test_expect_success 'rebase --keep-base --onto incompatible' '
+ test_must_fail git rebase --keep-base --onto main...
+'
+
+test_expect_success 'rebase --keep-base --root incompatible' '
+ test_must_fail git rebase --keep-base --root
+'
+
+test_expect_success 'rebase --keep-base main from topic' '
+ git reset --hard &&
+ git checkout topic &&
+ git reset --hard G &&
+
+ git rebase --keep-base main &&
+ git rev-parse C >base.expect &&
+ git merge-base main HEAD >base.actual &&
+ test_cmp base.expect base.actual &&
+
+ git rev-parse HEAD~2 >actual &&
+ git rev-parse C^0 >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rebase --keep-base main topic from main' '
+ git checkout main &&
+ git branch -f topic G &&
+
+ git rebase --keep-base main topic &&
+ git rev-parse C >base.expect &&
+ git merge-base main HEAD >base.actual &&
+ test_cmp base.expect base.actual &&
+
+ git rev-parse HEAD~2 >actual &&
+ git rev-parse C^0 >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rebase --keep-base main from side' '
+ git reset --hard &&
+ git checkout side &&
+ git reset --hard K &&
+
+ test_must_fail git rebase --keep-base main
+'
+
+test_expect_success 'rebase -i --keep-base main from topic' '
+ git reset --hard &&
+ git checkout topic &&
+ git reset --hard G &&
+
+ (
+ set_fake_editor &&
+ EXPECT_COUNT=2 git rebase -i --keep-base main
+ ) &&
+ git rev-parse C >base.expect &&
+ git merge-base main HEAD >base.actual &&
+ test_cmp base.expect base.actual &&
+
+ git rev-parse HEAD~2 >actual &&
+ git rev-parse C^0 >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rebase -i --keep-base main topic from main' '
+ git checkout main &&
+ git branch -f topic G &&
+
+ (
+ set_fake_editor &&
+ EXPECT_COUNT=2 git rebase -i --keep-base main topic
+ ) &&
+ git rev-parse C >base.expect &&
+ git merge-base main HEAD >base.actual &&
+ test_cmp base.expect base.actual &&
+
+ git rev-parse HEAD~2 >actual &&
+ git rev-parse C^0 >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rebase --keep-base requires a single merge base' '
+ git reset --hard &&
+ git checkout side &&
+ git reset --hard K &&
+
+ test_must_fail git rebase -i --keep-base main 2>err &&
+ grep "need exactly one merge base with branch" err
+'
+
+test_expect_success 'rebase --keep-base keeps cherry picks' '
+ git checkout -f -B main E &&
+ git cherry-pick F &&
+ (
+ set_fake_editor &&
+ EXPECT_COUNT=2 git rebase -i --keep-base HEAD G
+ ) &&
+ test_cmp_rev HEAD G
+'
+
+test_expect_success 'rebase --keep-base --no-reapply-cherry-picks' '
+ git checkout -f -B main E &&
+ git cherry-pick F &&
+ (
+ set_fake_editor &&
+ EXPECT_COUNT=1 git rebase -i --keep-base \
+ --no-reapply-cherry-picks HEAD G
+ ) &&
+ test_cmp_rev HEAD^ C
+'
+
+# This must be the last test in this file
+test_expect_success '$EDITOR and friends are unchanged' '
+ test_editor_unchanged
+'
+
+test_done
diff --git a/t/t3417-rebase-whitespace-fix.sh b/t/t3417-rebase-whitespace-fix.sh
new file mode 100755
index 0000000..96f2cf2
--- /dev/null
+++ b/t/t3417-rebase-whitespace-fix.sh
@@ -0,0 +1,124 @@
+#!/bin/sh
+
+test_description='git rebase --whitespace=fix
+
+This test runs git rebase --whitespace=fix and make sure that it works.
+'
+
+. ./test-lib.sh
+
+# prepare initial revision of "file" with a blank line at the end
+cat >file <<EOF
+a
+b
+c
+
+EOF
+
+# expected contents in "file" after rebase
+cat >expect-first <<EOF
+a
+b
+c
+EOF
+
+# prepare second revision of "file"
+cat >second <<EOF
+a
+b
+c
+
+d
+e
+f
+
+
+
+
+EOF
+
+# expected contents in second revision after rebase
+cat >expect-second <<EOF
+a
+b
+c
+
+d
+e
+f
+EOF
+
+test_expect_success 'blank line at end of file; extend at end of file' '
+ git commit --allow-empty -m "Initial empty commit" &&
+ git add file && git commit -m first &&
+ mv second file &&
+ git add file && git commit -m second &&
+ git rebase --whitespace=fix HEAD^^ &&
+ git diff --exit-code HEAD^:file expect-first &&
+ test_cmp expect-second file
+'
+
+# prepare third revision of "file"
+sed -e's/Z//' >third <<EOF
+a
+b
+c
+
+d
+e
+f
+ Z
+ Z
+h
+i
+j
+k
+l
+EOF
+
+sed -e's/ //g' <third >expect-third
+
+test_expect_success 'two blanks line at end of file; extend at end of file' '
+ cp third file && git add file && git commit -m third &&
+ git rebase --whitespace=fix HEAD^^ &&
+ git diff --exit-code HEAD^:file expect-second &&
+ test_cmp expect-third file
+'
+
+test_expect_success 'same, but do not remove trailing spaces' '
+ git config core.whitespace "-blank-at-eol" &&
+ git reset --hard HEAD^ &&
+ cp third file && git add file && git commit -m third &&
+ git rebase --whitespace=fix HEAD^^ &&
+ git diff --exit-code HEAD^:file expect-second &&
+ test_cmp file third
+'
+
+sed -e's/Z//' >beginning <<EOF
+a
+ Z
+ Z
+EOF
+
+cat >expect-beginning <<EOF
+a
+
+
+1
+2
+3
+4
+5
+EOF
+
+test_expect_success 'at beginning of file' '
+ git config core.whitespace "blank-at-eol" &&
+ cp beginning file &&
+ git commit -m beginning file &&
+ test_write_lines 1 2 3 4 5 >>file &&
+ git commit -m more file &&
+ git rebase --whitespace=fix HEAD^^ &&
+ test_cmp expect-beginning file
+'
+
+test_done
diff --git a/t/t3418-rebase-continue.sh b/t/t3418-rebase-continue.sh
new file mode 100755
index 0000000..127216f
--- /dev/null
+++ b/t/t3418-rebase-continue.sh
@@ -0,0 +1,357 @@
+#!/bin/sh
+
+test_description='git rebase --continue tests'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+. "$TEST_DIRECTORY"/lib-rebase.sh
+
+set_fake_editor
+
+test_expect_success 'setup' '
+ test_commit "commit-new-file-F1" F1 1 &&
+ test_commit "commit-new-file-F2" F2 2 &&
+
+ git checkout -b topic HEAD^ &&
+ test_commit "commit-new-file-F2-on-topic-branch" F2 22 &&
+
+ git checkout main
+'
+
+test_expect_success 'merge based rebase --continue with works with touched file' '
+ rm -fr .git/rebase-* &&
+ git reset --hard &&
+ git checkout main &&
+
+ FAKE_LINES="edit 1" git rebase -i HEAD^ &&
+ test-tool chmtime =-60 F1 &&
+ git rebase --continue
+'
+
+test_expect_success 'merge based rebase --continue removes .git/MERGE_MSG' '
+ git checkout -f --detach topic &&
+
+ test_must_fail git rebase --onto main HEAD^ &&
+ git read-tree --reset -u HEAD &&
+ test_path_is_file .git/MERGE_MSG &&
+ git rebase --continue &&
+ test_path_is_missing .git/MERGE_MSG
+'
+
+test_expect_success 'apply based rebase --continue works with touched file' '
+ rm -fr .git/rebase-* &&
+ git reset --hard &&
+ git checkout main &&
+
+ test_must_fail git rebase --apply --onto main main topic &&
+ echo "Resolved" >F2 &&
+ git add F2 &&
+ test-tool chmtime =-60 F1 &&
+ git rebase --continue
+'
+
+test_expect_success 'rebase --continue can not be used with other options' '
+ test_must_fail git rebase -v --continue &&
+ test_must_fail git rebase --continue -v
+'
+
+test_expect_success 'rebase --continue remembers merge strategy and options' '
+ rm -fr .git/rebase-* &&
+ git reset --hard commit-new-file-F2-on-topic-branch &&
+ test_commit "commit-new-file-F3-on-topic-branch" F3 32 &&
+ test_when_finished "rm -fr test-bin" &&
+ mkdir test-bin &&
+
+ write_script test-bin/git-merge-funny <<-\EOF &&
+ printf "[%s]\n" $# "$1" "$2" "$3" "$5" >actual
+ shift 3 &&
+ exec git merge-recursive "$@"
+ EOF
+
+ cat >expect <<-\EOF &&
+ [7]
+ [--option=arg with space]
+ [--op"tion\]
+ [--new
+ line ]
+ [--]
+ EOF
+
+ rm -f actual &&
+ (
+ PATH=./test-bin:$PATH &&
+ test_must_fail git rebase -s funny -X"option=arg with space" \
+ -Xop\"tion\\ -X"new${LF}line " main topic
+ ) &&
+ test_cmp expect actual &&
+ rm actual &&
+ echo "Resolved" >F2 &&
+ git add F2 &&
+ (
+ PATH=./test-bin:$PATH &&
+ git rebase --continue
+ ) &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rebase -r passes merge strategy options correctly' '
+ rm -fr .git/rebase-* &&
+ git reset --hard commit-new-file-F3-on-topic-branch &&
+ test_commit merge-theirs &&
+ git reset --hard HEAD^ &&
+ test_commit some-other-commit &&
+ test_tick &&
+ git merge --no-ff merge-theirs &&
+ FAKE_LINES="1 3 edit 4 5 7 8 9" git rebase -i -f -r -m \
+ -s recursive --strategy-option=theirs HEAD~2 &&
+ test_commit force-change-ours &&
+ git rebase --continue
+'
+
+test_expect_success '--skip after failed fixup cleans commit message' '
+ test_when_finished "test_might_fail git rebase --abort" &&
+ git checkout -b with-conflicting-fixup &&
+ test_commit wants-fixup &&
+ test_commit "fixup 1" wants-fixup.t 1 wants-fixup-1 &&
+ test_commit "fixup 2" wants-fixup.t 2 wants-fixup-2 &&
+ test_commit "fixup 3" wants-fixup.t 3 wants-fixup-3 &&
+ test_must_fail env FAKE_LINES="1 fixup 2 squash 4" \
+ git rebase -i HEAD~4 &&
+
+ : now there is a conflict, and comments in the commit message &&
+ test_commit_message HEAD <<-\EOF &&
+ # This is a combination of 2 commits.
+ # This is the 1st commit message:
+
+ wants-fixup
+
+ # The commit message #2 will be skipped:
+
+ # fixup 1
+ EOF
+
+ : skip and continue &&
+ echo "cp \"\$1\" .git/copy.txt" | write_script copy-editor.sh &&
+ (test_set_editor "$PWD/copy-editor.sh" && git rebase --skip) &&
+
+ : the user should not have had to edit the commit message &&
+ test_path_is_missing .git/copy.txt &&
+
+ : now the comments in the commit message should have been cleaned up &&
+ test_commit_message HEAD -m wants-fixup &&
+
+ : now, let us ensure that "squash" is handled correctly &&
+ git reset --hard wants-fixup-3 &&
+ test_must_fail env FAKE_LINES="1 squash 2 squash 1 squash 3 squash 1" \
+ git rebase -i HEAD~4 &&
+
+ : the second squash failed, but there are two more in the chain &&
+ (test_set_editor "$PWD/copy-editor.sh" &&
+ test_must_fail git rebase --skip) &&
+
+ : not the final squash, no need to edit the commit message &&
+ test_path_is_missing .git/copy.txt &&
+
+ : The first and third squashes succeeded, therefore: &&
+ test_commit_message HEAD <<-\EOF &&
+ # This is a combination of 3 commits.
+ # This is the 1st commit message:
+
+ wants-fixup
+
+ # This is the commit message #2:
+
+ fixup 1
+
+ # This is the commit message #3:
+
+ fixup 2
+ EOF
+
+ (test_set_editor "$PWD/copy-editor.sh" && git rebase --skip) &&
+ test_commit_message HEAD <<-\EOF &&
+ wants-fixup
+
+ fixup 1
+
+ fixup 2
+ EOF
+
+ : Final squash failed, but there was still a squash &&
+ head -n1 .git/copy.txt >first-line &&
+ test_grep "# This is a combination of 3 commits" first-line &&
+ test_grep "# This is the commit message #3:" .git/copy.txt
+'
+
+test_expect_success 'setup rerere database' '
+ rm -fr .git/rebase-* &&
+ git reset --hard commit-new-file-F3-on-topic-branch &&
+ git checkout main &&
+ test_commit "commit-new-file-F3" F3 3 &&
+ test_config rerere.enabled true &&
+ git update-ref refs/heads/topic commit-new-file-F3-on-topic-branch &&
+ test_must_fail git rebase -m main topic &&
+ echo "Resolved" >F2 &&
+ cp F2 expected-F2 &&
+ git add F2 &&
+ test_must_fail git rebase --continue &&
+ echo "Resolved" >F3 &&
+ cp F3 expected-F3 &&
+ git add F3 &&
+ git rebase --continue &&
+ git reset --hard topic@{1}
+'
+
+prepare () {
+ rm -fr .git/rebase-* &&
+ git reset --hard commit-new-file-F3-on-topic-branch &&
+ git checkout main &&
+ test_config rerere.enabled true
+}
+
+test_rerere_autoupdate () {
+ action=$1 &&
+ test_expect_success "rebase $action --continue remembers --rerere-autoupdate" '
+ prepare &&
+ test_must_fail git rebase $action --rerere-autoupdate main topic &&
+ test_cmp expected-F2 F2 &&
+ git diff-files --quiet &&
+ test_must_fail git rebase --continue &&
+ test_cmp expected-F3 F3 &&
+ git diff-files --quiet &&
+ git rebase --continue
+ '
+
+ test_expect_success "rebase $action --continue honors rerere.autoUpdate" '
+ prepare &&
+ test_config rerere.autoupdate true &&
+ test_must_fail git rebase $action main topic &&
+ test_cmp expected-F2 F2 &&
+ git diff-files --quiet &&
+ test_must_fail git rebase --continue &&
+ test_cmp expected-F3 F3 &&
+ git diff-files --quiet &&
+ git rebase --continue
+ '
+
+ test_expect_success "rebase $action --continue remembers --no-rerere-autoupdate" '
+ prepare &&
+ test_config rerere.autoupdate true &&
+ test_must_fail git rebase $action --no-rerere-autoupdate main topic &&
+ test_cmp expected-F2 F2 &&
+ test_must_fail git diff-files --quiet &&
+ git add F2 &&
+ test_must_fail git rebase --continue &&
+ test_cmp expected-F3 F3 &&
+ test_must_fail git diff-files --quiet &&
+ git add F3 &&
+ git rebase --continue
+ '
+}
+
+test_rerere_autoupdate --apply
+test_rerere_autoupdate -m
+GIT_SEQUENCE_EDITOR=: && export GIT_SEQUENCE_EDITOR
+test_rerere_autoupdate -i
+unset GIT_SEQUENCE_EDITOR
+
+test_expect_success 'the todo command "break" works' '
+ rm -f execed &&
+ FAKE_LINES="break b exec_>execed" git rebase -i HEAD &&
+ test_path_is_missing execed &&
+ git rebase --continue &&
+ test_path_is_missing execed &&
+ git rebase --continue &&
+ test_path_is_file execed
+'
+
+test_expect_success 'patch file is removed before break command' '
+ test_when_finished "git rebase --abort" &&
+ cat >todo <<-\EOF &&
+ pick commit-new-file-F2-on-topic-branch
+ break
+ EOF
+
+ (
+ set_replace_editor todo &&
+ test_must_fail git rebase -i --onto commit-new-file-F2 HEAD
+ ) &&
+ test_path_is_file .git/rebase-merge/patch &&
+ echo 22>F2 &&
+ git add F2 &&
+ git rebase --continue &&
+ test_path_is_missing .git/rebase-merge/patch
+'
+
+test_expect_success '--reschedule-failed-exec' '
+ test_when_finished "git rebase --abort" &&
+ test_must_fail git rebase -x false --reschedule-failed-exec HEAD^ &&
+ grep "^exec false" .git/rebase-merge/git-rebase-todo &&
+ git rebase --abort &&
+ test_must_fail git -c rebase.rescheduleFailedExec=true \
+ rebase -x false HEAD^ 2>err &&
+ grep "^exec false" .git/rebase-merge/git-rebase-todo &&
+ test_grep "has been rescheduled" err
+'
+
+test_expect_success 'rebase.rescheduleFailedExec only affects `rebase -i`' '
+ test_config rebase.rescheduleFailedExec true &&
+ test_must_fail git rebase -x false HEAD^ &&
+ grep "^exec false" .git/rebase-merge/git-rebase-todo &&
+ git rebase --abort &&
+ git rebase HEAD^
+'
+
+test_expect_success 'rebase.rescheduleFailedExec=true & --no-reschedule-failed-exec' '
+ test_when_finished "git rebase --abort" &&
+ test_config rebase.rescheduleFailedExec true &&
+ test_must_fail git rebase -x false --no-reschedule-failed-exec HEAD~2 &&
+ test_must_fail git rebase --continue 2>err &&
+ ! grep "has been rescheduled" err
+'
+
+test_expect_success 'new rebase.rescheduleFailedExec=true setting in an ongoing rebase is ignored' '
+ test_when_finished "git rebase --abort" &&
+ test_must_fail git rebase -x false HEAD~2 &&
+ test_config rebase.rescheduleFailedExec true &&
+ test_must_fail git rebase --continue 2>err &&
+ ! grep "has been rescheduled" err
+'
+
+test_expect_success 'there is no --no-reschedule-failed-exec in an ongoing rebase' '
+ test_when_finished "git rebase --abort" &&
+ test_must_fail git rebase -x false HEAD~2 &&
+ test_expect_code 129 git rebase --continue --no-reschedule-failed-exec &&
+ test_expect_code 129 git rebase --edit-todo --no-reschedule-failed-exec
+'
+
+test_orig_head_helper () {
+ test_when_finished 'git rebase --abort &&
+ git checkout topic &&
+ git reset --hard commit-new-file-F2-on-topic-branch' &&
+ git update-ref -d ORIG_HEAD &&
+ test_must_fail git rebase "$@" &&
+ test_cmp_rev ORIG_HEAD commit-new-file-F2-on-topic-branch
+}
+
+test_orig_head () {
+ type=$1
+ test_expect_success "rebase $type sets ORIG_HEAD correctly" '
+ git checkout topic &&
+ git reset --hard commit-new-file-F2-on-topic-branch &&
+ test_orig_head_helper $type main
+ '
+
+ test_expect_success "rebase $type <upstream> <branch> sets ORIG_HEAD correctly" '
+ git checkout main &&
+ test_orig_head_helper $type main topic
+ '
+}
+
+test_orig_head --apply
+test_orig_head --merge
+
+test_done
diff --git a/t/t3419-rebase-patch-id.sh b/t/t3419-rebase-patch-id.sh
new file mode 100755
index 0000000..6c61f24
--- /dev/null
+++ b/t/t3419-rebase-patch-id.sh
@@ -0,0 +1,118 @@
+#!/bin/sh
+
+test_description='git rebase - test patch id computation'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+scramble () {
+ i=0
+ while read x
+ do
+ if test $i -ne 0
+ then
+ echo "$x"
+ fi
+ i=$((($i+1) % 10))
+ done <"$1" >"$1.new"
+ mv -f "$1.new" "$1"
+}
+
+test_expect_success 'setup' '
+ git commit --allow-empty -m initial &&
+ git tag root
+'
+
+test_expect_success 'setup: 500 lines' '
+ rm -f .gitattributes &&
+ git checkout -q -f main &&
+ git reset --hard root &&
+ test_seq 500 >file &&
+ git add file &&
+ git commit -q -m initial &&
+ git branch -f other &&
+
+ scramble file &&
+ git add file &&
+ git commit -q -m "change big file" &&
+
+ git checkout -q other &&
+ : >newfile &&
+ git add newfile &&
+ git commit -q -m "add small file" &&
+
+ git cherry-pick main >/dev/null 2>&1 &&
+
+ git branch -f squashed main &&
+ git checkout -q -f squashed &&
+ git reset -q --soft HEAD~2 &&
+ git commit -q -m squashed &&
+
+ git branch -f mode main &&
+ git checkout -q -f mode &&
+ test_chmod +x file &&
+ git commit -q -a --amend &&
+
+ git branch -f modeother other &&
+ git checkout -q -f modeother &&
+ test_chmod +x file &&
+ git commit -q -a --amend
+'
+
+test_expect_success 'detect upstream patch' '
+ git checkout -q main^{} &&
+ scramble file &&
+ git add file &&
+ git commit -q -m "change big file again" &&
+ git checkout -q other^{} &&
+ git rebase main &&
+ git rev-list main...HEAD~ >revs &&
+ test_must_be_empty revs
+'
+
+test_expect_success 'detect upstream patch binary' '
+ echo "file binary" >.gitattributes &&
+ git checkout -q other^{} &&
+ git rebase main &&
+ git rev-list main...HEAD~ >revs &&
+ test_must_be_empty revs &&
+ test_when_finished "rm .gitattributes"
+'
+
+test_expect_success 'detect upstream patch modechange' '
+ git checkout -q modeother^{} &&
+ git rebase mode &&
+ git rev-list mode...HEAD~ >revs &&
+ test_must_be_empty revs
+'
+
+test_expect_success 'do not drop patch' '
+ git checkout -q other^{} &&
+ test_must_fail git rebase squashed &&
+ test_when_finished "git rebase --abort"
+'
+
+test_expect_success 'do not drop patch binary' '
+ echo "file binary" >.gitattributes &&
+ git checkout -q other^{} &&
+ test_must_fail git rebase squashed &&
+ test_when_finished "git rebase --abort" &&
+ test_when_finished "rm .gitattributes"
+'
+
+test_expect_success 'do not drop patch modechange' '
+ git checkout -q modeother^{} &&
+ git rebase other &&
+ cat >expected <<-\EOF &&
+ diff --git a/file b/file
+ old mode 100644
+ new mode 100755
+ EOF
+ git diff HEAD~ >modediff &&
+ test_cmp expected modediff
+'
+
+test_done
diff --git a/t/t3420-rebase-autostash.sh b/t/t3420-rebase-autostash.sh
new file mode 100755
index 0000000..693934e
--- /dev/null
+++ b/t/t3420-rebase-autostash.sh
@@ -0,0 +1,336 @@
+#!/bin/sh
+#
+# Copyright (c) 2013 Ramkumar Ramachandra
+#
+
+test_description='git rebase --autostash tests'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success setup '
+ echo hello-world >file0 &&
+ git add . &&
+ test_tick &&
+ git commit -m "initial commit" &&
+ git checkout -b feature-branch &&
+ echo another-hello >file1 &&
+ echo goodbye >file2 &&
+ git add . &&
+ test_tick &&
+ git commit -m "second commit" &&
+ echo final-goodbye >file3 &&
+ git add . &&
+ test_tick &&
+ git commit -m "third commit" &&
+ git checkout -b unrelated-onto-branch main &&
+ echo unrelated >file4 &&
+ git add . &&
+ test_tick &&
+ git commit -m "unrelated commit" &&
+ git checkout -b related-onto-branch main &&
+ echo conflicting-change >file2 &&
+ git add . &&
+ test_tick &&
+ git commit -m "related commit" &&
+ remove_progress_re="$(printf "s/.*\\r//")"
+'
+
+create_expected_success_apply () {
+ cat >expected <<-EOF
+ $(grep "^Created autostash: [0-9a-f][0-9a-f]*\$" actual)
+ First, rewinding head to replay your work on top of it...
+ Applying: second commit
+ Applying: third commit
+ Applied autostash.
+ EOF
+}
+
+create_expected_success_merge () {
+ q_to_cr >expected <<-EOF
+ $(grep "^Created autostash: [0-9a-f][0-9a-f]*\$" actual)
+ Applied autostash.
+ Successfully rebased and updated refs/heads/rebased-feature-branch.
+ EOF
+}
+
+create_expected_failure_apply () {
+ cat >expected <<-EOF
+ $(grep "^Created autostash: [0-9a-f][0-9a-f]*\$" actual)
+ First, rewinding head to replay your work on top of it...
+ Applying: second commit
+ Applying: third commit
+ Applying autostash resulted in conflicts.
+ Your changes are safe in the stash.
+ You can run "git stash pop" or "git stash drop" at any time.
+ EOF
+}
+
+create_expected_failure_merge () {
+ cat >expected <<-EOF
+ $(grep "^Created autostash: [0-9a-f][0-9a-f]*\$" actual)
+ Applying autostash resulted in conflicts.
+ Your changes are safe in the stash.
+ You can run "git stash pop" or "git stash drop" at any time.
+ Successfully rebased and updated refs/heads/rebased-feature-branch.
+ EOF
+}
+
+testrebase () {
+ type=$1
+ dotest=$2
+
+ test_expect_success "rebase$type: dirty worktree, --no-autostash" '
+ test_config rebase.autostash true &&
+ git reset --hard &&
+ git checkout -b rebased-feature-branch feature-branch &&
+ test_when_finished git branch -D rebased-feature-branch &&
+ test_when_finished git checkout feature-branch &&
+ echo dirty >>file3 &&
+ test_must_fail git rebase$type --no-autostash unrelated-onto-branch
+ '
+
+ test_expect_success "rebase$type: dirty worktree, non-conflicting rebase" '
+ test_config rebase.autostash true &&
+ git reset --hard &&
+ git checkout -b rebased-feature-branch feature-branch &&
+ echo dirty >>file3 &&
+ git rebase$type unrelated-onto-branch >actual 2>&1 &&
+ grep unrelated file4 &&
+ grep dirty file3 &&
+ git checkout feature-branch
+ '
+
+ test_expect_success "rebase$type --autostash: check output" '
+ test_when_finished git branch -D rebased-feature-branch &&
+ suffix=${type#\ --} && suffix=${suffix:-apply} &&
+ if test ${suffix} = "interactive"; then
+ suffix=merge
+ fi &&
+ create_expected_success_$suffix &&
+ sed "$remove_progress_re" <actual >actual2 &&
+ test_cmp expected actual2
+ '
+
+ test_expect_success "rebase$type: dirty index, non-conflicting rebase" '
+ test_config rebase.autostash true &&
+ git reset --hard &&
+ git checkout -b rebased-feature-branch feature-branch &&
+ test_when_finished git branch -D rebased-feature-branch &&
+ echo dirty >>file3 &&
+ git add file3 &&
+ git rebase$type unrelated-onto-branch &&
+ grep unrelated file4 &&
+ grep dirty file3 &&
+ git checkout feature-branch
+ '
+
+ test_expect_success "rebase$type: conflicting rebase" '
+ test_config rebase.autostash true &&
+ git reset --hard &&
+ git checkout -b rebased-feature-branch feature-branch &&
+ test_when_finished git branch -D rebased-feature-branch &&
+ echo dirty >>file3 &&
+ test_must_fail git rebase$type related-onto-branch &&
+ test_path_is_file $dotest/autostash &&
+ test_path_is_missing file3 &&
+ rm -rf $dotest &&
+ git reset --hard &&
+ git checkout feature-branch
+ '
+
+ test_expect_success "rebase$type: --continue" '
+ test_config rebase.autostash true &&
+ git reset --hard &&
+ git checkout -b rebased-feature-branch feature-branch &&
+ test_when_finished git branch -D rebased-feature-branch &&
+ echo dirty >>file3 &&
+ test_must_fail git rebase$type related-onto-branch &&
+ test_path_is_file $dotest/autostash &&
+ test_path_is_missing file3 &&
+ echo "conflicting-plus-goodbye" >file2 &&
+ git add file2 &&
+ git rebase --continue &&
+ test_path_is_missing $dotest/autostash &&
+ grep dirty file3 &&
+ git checkout feature-branch
+ '
+
+ test_expect_success "rebase$type: --skip" '
+ test_config rebase.autostash true &&
+ git reset --hard &&
+ git checkout -b rebased-feature-branch feature-branch &&
+ test_when_finished git branch -D rebased-feature-branch &&
+ echo dirty >>file3 &&
+ test_must_fail git rebase$type related-onto-branch &&
+ test_path_is_file $dotest/autostash &&
+ test_path_is_missing file3 &&
+ git rebase --skip &&
+ test_path_is_missing $dotest/autostash &&
+ grep dirty file3 &&
+ git checkout feature-branch
+ '
+
+ test_expect_success "rebase$type: --abort" '
+ test_config rebase.autostash true &&
+ git reset --hard &&
+ git checkout -b rebased-feature-branch feature-branch &&
+ test_when_finished git branch -D rebased-feature-branch &&
+ echo dirty >>file3 &&
+ test_must_fail git rebase$type related-onto-branch &&
+ test_path_is_file $dotest/autostash &&
+ test_path_is_missing file3 &&
+ git rebase --abort &&
+ test_path_is_missing $dotest/autostash &&
+ grep dirty file3 &&
+ git checkout feature-branch
+ '
+
+ test_expect_success "rebase$type: --quit" '
+ test_config rebase.autostash true &&
+ git reset --hard &&
+ git checkout -b rebased-feature-branch feature-branch &&
+ test_when_finished git branch -D rebased-feature-branch &&
+ echo dirty >>file3 &&
+ git diff >expect &&
+ test_must_fail git rebase$type related-onto-branch &&
+ test_path_is_file $dotest/autostash &&
+ test_path_is_missing file3 &&
+ git rebase --quit &&
+ test_when_finished git stash drop &&
+ test_path_is_missing $dotest/autostash &&
+ ! grep dirty file3 &&
+ git stash show -p >actual &&
+ test_cmp expect actual &&
+ git reset --hard &&
+ git checkout feature-branch
+ '
+
+ test_expect_success "rebase$type: non-conflicting rebase, conflicting stash" '
+ test_config rebase.autostash true &&
+ git reset --hard &&
+ git checkout -b rebased-feature-branch feature-branch &&
+ echo dirty >file4 &&
+ git add file4 &&
+ git rebase$type unrelated-onto-branch >actual 2>&1 &&
+ test_path_is_missing $dotest &&
+ git reset --hard &&
+ grep unrelated file4 &&
+ ! grep dirty file4 &&
+ git checkout feature-branch &&
+ git stash pop &&
+ grep dirty file4
+ '
+
+ test_expect_success "rebase$type: check output with conflicting stash" '
+ test_when_finished git branch -D rebased-feature-branch &&
+ suffix=${type#\ --} && suffix=${suffix:-apply} &&
+ if test ${suffix} = "interactive"; then
+ suffix=merge
+ fi &&
+ create_expected_failure_$suffix &&
+ sed "$remove_progress_re" <actual >actual2 &&
+ test_cmp expected actual2
+ '
+}
+
+test_expect_success "rebase: fast-forward rebase" '
+ test_config rebase.autostash true &&
+ git reset --hard &&
+ git checkout -b behind-feature-branch feature-branch~1 &&
+ test_when_finished git branch -D behind-feature-branch &&
+ echo dirty >>file1 &&
+ git rebase feature-branch &&
+ grep dirty file1 &&
+ git checkout feature-branch
+'
+
+test_expect_success "rebase: noop rebase" '
+ test_config rebase.autostash true &&
+ git reset --hard &&
+ git checkout -b same-feature-branch feature-branch &&
+ test_when_finished git branch -D same-feature-branch &&
+ echo dirty >>file1 &&
+ git rebase feature-branch &&
+ grep dirty file1 &&
+ git checkout feature-branch
+'
+
+testrebase " --apply" .git/rebase-apply
+testrebase " --merge" .git/rebase-merge
+testrebase " --interactive" .git/rebase-merge
+
+test_expect_success 'abort rebase -i with --autostash' '
+ test_when_finished "git reset --hard" &&
+ echo uncommitted-content >file0 &&
+ (
+ write_script abort-editor.sh <<-\EOF &&
+ echo >"$1"
+ EOF
+ test_set_editor "$(pwd)/abort-editor.sh" &&
+ test_must_fail git rebase -i --autostash HEAD^ &&
+ rm -f abort-editor.sh
+ ) &&
+ echo uncommitted-content >expected &&
+ test_cmp expected file0
+'
+
+test_expect_success 'restore autostash on editor failure' '
+ test_when_finished "git reset --hard" &&
+ echo uncommitted-content >file0 &&
+ (
+ test_set_editor "false" &&
+ test_must_fail git rebase -i --autostash HEAD^
+ ) &&
+ echo uncommitted-content >expected &&
+ test_cmp expected file0
+'
+
+test_expect_success 'autostash is saved on editor failure with conflict' '
+ test_when_finished "git reset --hard" &&
+ echo uncommitted-content >file0 &&
+ (
+ write_script abort-editor.sh <<-\EOF &&
+ echo conflicting-content >file0
+ exit 1
+ EOF
+ test_set_editor "$(pwd)/abort-editor.sh" &&
+ test_must_fail git rebase -i --autostash HEAD^ &&
+ rm -f abort-editor.sh
+ ) &&
+ echo conflicting-content >expected &&
+ test_cmp expected file0 &&
+ git checkout file0 &&
+ git stash pop &&
+ echo uncommitted-content >expected &&
+ test_cmp expected file0
+'
+
+test_expect_success 'autostash with dirty submodules' '
+ test_when_finished "git reset --hard && git checkout main" &&
+ git checkout -b with-submodule &&
+ git -c protocol.file.allow=always submodule add ./ sub &&
+ test_tick &&
+ git commit -m add-submodule &&
+ echo changed >sub/file0 &&
+ git rebase -i --autostash HEAD
+'
+
+test_expect_success 'branch is left alone when possible' '
+ git checkout -b unchanged-branch &&
+ echo changed >file0 &&
+ git rebase --autostash unchanged-branch &&
+ test changed = "$(cat file0)" &&
+ test unchanged-branch = "$(git rev-parse --abbrev-ref HEAD)"
+'
+
+test_expect_success 'never change active branch' '
+ git checkout -b not-the-feature-branch unrelated-onto-branch &&
+ test_when_finished "git reset --hard && git checkout main" &&
+ echo changed >file0 &&
+ git rebase --autostash not-the-feature-branch feature-branch &&
+ test_cmp_rev not-the-feature-branch unrelated-onto-branch
+'
+
+test_done
diff --git a/t/t3421-rebase-topology-linear.sh b/t/t3421-rebase-topology-linear.sh
new file mode 100755
index 0000000..62d86d5
--- /dev/null
+++ b/t/t3421-rebase-topology-linear.sh
@@ -0,0 +1,359 @@
+#!/bin/sh
+
+test_description='basic rebase topology tests'
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-rebase.sh
+
+# a---b---c
+# \
+# d---e
+test_expect_success 'setup' '
+ test_commit a &&
+ test_commit b &&
+ test_commit c &&
+ git checkout b &&
+ test_commit d &&
+ test_commit e
+'
+
+test_run_rebase () {
+ result=$1
+ shift
+ test_expect_$result "simple rebase $*" "
+ reset_rebase &&
+ git rebase $* c e &&
+ test_cmp_rev c HEAD~2 &&
+ test_linear_range 'd e' c..
+ "
+}
+test_run_rebase success --apply
+test_run_rebase success -m
+test_run_rebase success -i
+
+test_expect_success 'setup branches and remote tracking' '
+ git tag -l >tags &&
+ for tag in $(cat tags)
+ do
+ git branch branch-$tag $tag || return 1
+ done &&
+ git remote add origin "file://$PWD" &&
+ git fetch origin
+'
+
+test_run_rebase () {
+ result=$1
+ shift
+ test_expect_$result "rebase $* is no-op if upstream is an ancestor" "
+ reset_rebase &&
+ git rebase $* b e &&
+ test_cmp_rev e HEAD
+ "
+}
+test_run_rebase success --apply
+test_run_rebase success -m
+test_run_rebase success -i
+
+test_run_rebase () {
+ result=$1
+ shift
+ test_expect_$result "rebase $* -f rewrites even if upstream is an ancestor" "
+ reset_rebase &&
+ git rebase $* -f b e &&
+ test_cmp_rev ! e HEAD &&
+ test_cmp_rev b HEAD~2 &&
+ test_linear_range 'd e' b..
+ "
+}
+test_run_rebase success --apply
+test_run_rebase success --fork-point
+test_run_rebase success -m
+test_run_rebase success -i
+
+test_run_rebase () {
+ result=$1
+ shift
+ test_expect_$result "rebase $* -f rewrites even if remote upstream is an ancestor" "
+ reset_rebase &&
+ git rebase $* -f branch-b branch-e &&
+ test_cmp_rev ! branch-e origin/branch-e &&
+ test_cmp_rev branch-b HEAD~2 &&
+ test_linear_range 'd e' branch-b..
+ "
+}
+test_run_rebase success --apply
+test_run_rebase success --fork-point
+test_run_rebase success -m
+test_run_rebase success -i
+
+test_run_rebase () {
+ result=$1
+ shift
+ test_expect_$result "rebase $* fast-forwards from ancestor of upstream" "
+ reset_rebase &&
+ git rebase $* e b &&
+ test_cmp_rev e HEAD
+ "
+}
+test_run_rebase success --apply
+test_run_rebase success --fork-point
+test_run_rebase success -m
+test_run_rebase success -i
+
+# f
+# /
+# a---b---c---g---h
+# \
+# d---gp--i
+#
+# gp = cherry-picked g
+# h = reverted g
+#
+# Reverted patches are there for tests to be able to check if a commit
+# that introduced the same change as another commit is
+# dropped. Without reverted commits, we could get false positives
+# because applying the patch succeeds, but simply results in no
+# changes.
+test_expect_success 'setup of linear history for range selection tests' '
+ git checkout c &&
+ test_commit g &&
+ revert h g &&
+ git checkout d &&
+ cherry_pick gp g &&
+ test_commit i &&
+ git checkout b &&
+ test_commit f
+'
+
+test_run_rebase () {
+ result=$1
+ shift
+ test_expect_$result "rebase $* drops patches in upstream" "
+ reset_rebase &&
+ git rebase $* h i &&
+ test_cmp_rev h HEAD~2 &&
+ test_linear_range 'd i' h..
+ "
+}
+test_run_rebase success --apply
+test_run_rebase success -m
+test_run_rebase success -i
+
+test_run_rebase () {
+ result=$1
+ shift
+ test_expect_$result "rebase $* can drop last patch if in upstream" "
+ reset_rebase &&
+ git rebase $* h gp &&
+ test_cmp_rev h HEAD^ &&
+ test_linear_range 'd' h..
+ "
+}
+test_run_rebase success --apply
+test_run_rebase success -m
+test_run_rebase success -i
+
+test_run_rebase () {
+ result=$1
+ shift
+ test_expect_$result "rebase $* --onto drops patches in upstream" "
+ reset_rebase &&
+ git rebase $* --onto f h i &&
+ test_cmp_rev f HEAD~2 &&
+ test_linear_range 'd i' f..
+ "
+}
+test_run_rebase success --apply
+test_run_rebase success -m
+test_run_rebase success -i
+
+test_run_rebase () {
+ result=$1
+ shift
+ test_expect_$result "rebase $* --onto does not drop patches in onto" "
+ reset_rebase &&
+ git rebase $* --onto h f i &&
+ test_cmp_rev h HEAD~3 &&
+ test_linear_range 'd gp i' h..
+ "
+}
+test_run_rebase success --apply
+test_run_rebase success -m
+test_run_rebase success -i
+
+# a---b---c---j!
+# \
+# d---k!--l
+#
+# ! = empty
+test_expect_success 'setup of linear history for empty commit tests' '
+ git checkout c &&
+ make_empty j &&
+ git checkout d &&
+ make_empty k &&
+ test_commit l
+'
+
+test_run_rebase () {
+ result=$1
+ shift
+ test_expect_$result "rebase $* keeps begin-empty commits" "
+ reset_rebase &&
+ git rebase $* j l &&
+ test_cmp_rev c HEAD~4 &&
+ test_linear_range 'j d k l' c..
+ "
+}
+test_run_rebase failure --apply
+test_run_rebase success -m
+test_run_rebase success -i
+
+test_run_rebase () {
+ result=$1
+ shift
+ test_expect_$result "rebase $* --no-keep-empty drops begin-empty commits" "
+ reset_rebase &&
+ git rebase $* --no-keep-empty c l &&
+ test_cmp_rev c HEAD~2 &&
+ test_linear_range 'd l' c..
+ "
+}
+test_run_rebase success -m
+test_run_rebase success -i
+
+test_run_rebase () {
+ result=$1
+ shift
+ test_expect_$result "rebase $* --keep-empty keeps empty even if already in upstream" "
+ reset_rebase &&
+ git rebase $* --keep-empty j l &&
+ test_cmp_rev j HEAD~3 &&
+ test_linear_range 'd k l' j..
+ "
+}
+test_run_rebase success -m
+test_run_rebase success -i
+test_run_rebase success --rebase-merges
+
+# m
+# /
+# a---b---c---g
+#
+# x---y---bp
+#
+# bp = cherry-picked b
+# m = reverted b
+#
+# Reverted patches are there for tests to be able to check if a commit
+# that introduced the same change as another commit is
+# dropped. Without reverted commits, we could get false positives
+# because applying the patch succeeds, but simply results in no
+# changes.
+test_expect_success 'setup of linear history for test involving root' '
+ git checkout b &&
+ revert m b &&
+ git checkout --orphan disjoint &&
+ git rm -rf . &&
+ test_commit x &&
+ test_commit y &&
+ cherry_pick bp b
+'
+
+test_run_rebase () {
+ result=$1
+ shift
+ test_expect_$result "rebase $* --onto --root" "
+ reset_rebase &&
+ git rebase $* --onto c --root y &&
+ test_cmp_rev c HEAD~2 &&
+ test_linear_range 'x y' c..
+ "
+}
+test_run_rebase success --apply
+test_run_rebase success -m
+test_run_rebase success -i
+
+test_run_rebase () {
+ result=$1
+ shift
+ test_expect_$result "rebase $* without --onto --root with disjoint history" "
+ reset_rebase &&
+ git rebase $* c y &&
+ test_cmp_rev c HEAD~2 &&
+ test_linear_range 'x y' c..
+ "
+}
+test_run_rebase success --apply
+test_run_rebase success -m
+test_run_rebase success -i
+
+test_run_rebase () {
+ result=$1
+ shift
+ test_expect_$result "rebase $* --onto --root drops patch in onto" "
+ reset_rebase &&
+ git rebase $* --onto m --root bp &&
+ test_cmp_rev m HEAD~2 &&
+ test_linear_range 'x y' m..
+ "
+}
+test_run_rebase success --apply
+test_run_rebase success -m
+test_run_rebase success -i
+
+test_run_rebase () {
+ result=$1
+ shift
+ test_expect_$result "rebase $* --onto --root with merge-base does not go to root" "
+ reset_rebase &&
+ git rebase $* --onto m --root g &&
+ test_cmp_rev m HEAD~2 &&
+ test_linear_range 'c g' m..
+ "
+}
+
+test_run_rebase success --apply
+test_run_rebase success -m
+test_run_rebase success -i
+
+test_run_rebase () {
+ result=$1
+ shift
+ test_expect_$result "rebase $* without --onto --root with disjoint history drops patch in onto" "
+ reset_rebase &&
+ git rebase $* m bp &&
+ test_cmp_rev m HEAD~2 &&
+ test_linear_range 'x y' m..
+ "
+}
+test_run_rebase success --apply
+test_run_rebase success -m
+test_run_rebase success -i
+
+test_run_rebase () {
+ result=$1
+ shift
+ test_expect_$result "rebase $* --root on linear history is a no-op" "
+ reset_rebase &&
+ git rebase $* --root c &&
+ test_cmp_rev c HEAD
+ "
+}
+test_run_rebase success ''
+test_run_rebase success -m
+test_run_rebase success -i
+
+test_run_rebase () {
+ result=$1
+ shift
+ test_expect_$result "rebase $* -f --root on linear history causes re-write" "
+ reset_rebase &&
+ git rebase $* -f --root c &&
+ test_cmp_rev ! a HEAD~2 &&
+ test_linear_range 'a b c' HEAD
+ "
+}
+test_run_rebase success ''
+test_run_rebase success -m
+test_run_rebase success -i
+
+test_done
diff --git a/t/t3422-rebase-incompatible-options.sh b/t/t3422-rebase-incompatible-options.sh
new file mode 100755
index 0000000..2eba00b
--- /dev/null
+++ b/t/t3422-rebase-incompatible-options.sh
@@ -0,0 +1,146 @@
+#!/bin/sh
+
+test_description='test if rebase detects and aborts on incompatible options'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ test_seq 2 9 >foo &&
+ git add foo &&
+ git commit -m orig &&
+
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ test_seq 1 9 >foo &&
+ git add foo &&
+ git commit -m A &&
+
+ git checkout B &&
+ echo "q qfoo();" | q_to_tab >>foo &&
+ git add foo &&
+ git commit -m B
+'
+
+#
+# Rebase has a couple options which are specific to the apply backend,
+# and several options which are specific to the merge backend. Flags
+# from the different sets cannot work together, and we do not want to
+# just ignore one of the sets of flags. Make sure rebase warns the
+# user and aborts instead.
+#
+
+test_rebase_am_only () {
+ opt=$1
+ shift
+ test_expect_success "$opt incompatible with --merge" "
+ git checkout B^0 &&
+ test_must_fail git rebase $opt --merge A
+ "
+
+ test_expect_success "$opt incompatible with --strategy=ours" "
+ git checkout B^0 &&
+ test_must_fail git rebase $opt --strategy=ours A
+ "
+
+ test_expect_success "$opt incompatible with --strategy-option=ours" "
+ git checkout B^0 &&
+ test_must_fail git rebase $opt --strategy-option=ours A
+ "
+
+ test_expect_success "$opt incompatible with --autosquash" "
+ git checkout B^0 &&
+ test_must_fail git rebase $opt --autosquash A
+ "
+
+ test_expect_success "$opt incompatible with --interactive" "
+ git checkout B^0 &&
+ test_must_fail git rebase $opt --interactive A
+ "
+
+ test_expect_success "$opt incompatible with --exec" "
+ git checkout B^0 &&
+ test_must_fail git rebase $opt --exec 'true' A
+ "
+
+ test_expect_success "$opt incompatible with --keep-empty" "
+ git checkout B^0 &&
+ test_must_fail git rebase $opt --keep-empty A
+ "
+
+ test_expect_success "$opt incompatible with --empty=..." "
+ git checkout B^0 &&
+ test_must_fail git rebase $opt --empty=ask A
+ "
+
+ test_expect_success "$opt incompatible with --no-reapply-cherry-picks" "
+ git checkout B^0 &&
+ test_must_fail git rebase $opt --no-reapply-cherry-picks A
+ "
+
+ test_expect_success "$opt incompatible with --reapply-cherry-picks" "
+ git checkout B^0 &&
+ test_must_fail git rebase $opt --reapply-cherry-picks A
+ "
+
+ test_expect_success "$opt incompatible with --rebase-merges" "
+ git checkout B^0 &&
+ test_must_fail git rebase $opt --rebase-merges A
+ "
+
+ test_expect_success "$opt incompatible with --update-refs" "
+ git checkout B^0 &&
+ test_must_fail git rebase $opt --update-refs A
+ "
+
+ test_expect_success "$opt incompatible with --root without --onto" "
+ git checkout B^0 &&
+ test_must_fail git rebase $opt --root A
+ "
+
+ test_expect_success "$opt incompatible with rebase.autosquash" "
+ git checkout B^0 &&
+ test_must_fail git -c rebase.autosquash=true rebase $opt A 2>err &&
+ grep -e --no-autosquash err
+ "
+
+ test_expect_success "$opt incompatible with rebase.rebaseMerges" "
+ git checkout B^0 &&
+ test_must_fail git -c rebase.rebaseMerges=true rebase $opt A 2>err &&
+ grep -e --no-rebase-merges err
+ "
+
+ test_expect_success "$opt incompatible with rebase.updateRefs" "
+ git checkout B^0 &&
+ test_must_fail git -c rebase.updateRefs=true rebase $opt A 2>err &&
+ grep -e --no-update-refs err
+ "
+
+ test_expect_success "$opt okay with overridden rebase.autosquash" "
+ test_when_finished \"git reset --hard B^0\" &&
+ git checkout B^0 &&
+ git -c rebase.autosquash=true rebase --no-autosquash $opt A
+ "
+
+ test_expect_success "$opt okay with overridden rebase.rebaseMerges" "
+ test_when_finished \"git reset --hard B^0\" &&
+ git checkout B^0 &&
+ git -c rebase.rebaseMerges=true rebase --no-rebase-merges $opt A
+ "
+
+ test_expect_success "$opt okay with overridden rebase.updateRefs" "
+ test_when_finished \"git reset --hard B^0\" &&
+ git checkout B^0 &&
+ git -c rebase.updateRefs=true rebase --no-update-refs $opt A
+ "
+}
+
+# Check options which imply --apply
+test_rebase_am_only --whitespace=fix
+test_rebase_am_only -C4
+# Also check an explicit --apply
+test_rebase_am_only --apply
+
+test_done
diff --git a/t/t3423-rebase-reword.sh b/t/t3423-rebase-reword.sh
new file mode 100755
index 0000000..2fab703
--- /dev/null
+++ b/t/t3423-rebase-reword.sh
@@ -0,0 +1,49 @@
+#!/bin/sh
+
+test_description='git rebase interactive with rewording'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+. "$TEST_DIRECTORY"/lib-rebase.sh
+
+test_expect_success 'setup' '
+ test_commit main file-1 test &&
+
+ git checkout -b stuff &&
+
+ test_commit feature_a file-2 aaa &&
+ test_commit feature_b file-2 ddd
+'
+
+test_expect_success 'reword without issues functions as intended' '
+ test_when_finished "reset_rebase" &&
+
+ git checkout stuff^0 &&
+
+ set_fake_editor &&
+ FAKE_LINES="pick 1 reword 2" FAKE_COMMIT_MESSAGE="feature_b_reworded" \
+ git rebase -i -v main &&
+
+ test "$(git log -1 --format=%B)" = "feature_b_reworded" &&
+ test $(git rev-list --count HEAD) = 3
+'
+
+test_expect_success 'reword after a conflict preserves commit' '
+ test_when_finished "reset_rebase" &&
+
+ git checkout stuff^0 &&
+
+ set_fake_editor &&
+ test_must_fail env FAKE_LINES="reword 2" \
+ git rebase -i -v main &&
+
+ git checkout --theirs file-2 &&
+ git add file-2 &&
+ FAKE_COMMIT_MESSAGE="feature_b_reworded" git rebase --continue &&
+
+ test "$(git log -1 --format=%B)" = "feature_b_reworded" &&
+ test $(git rev-list --count HEAD) = 2
+'
+
+test_done
diff --git a/t/t3424-rebase-empty.sh b/t/t3424-rebase-empty.sh
new file mode 100755
index 0000000..5e1045a
--- /dev/null
+++ b/t/t3424-rebase-empty.sh
@@ -0,0 +1,170 @@
+#!/bin/sh
+
+test_description='git rebase of commits that start or become empty'
+
+. ./test-lib.sh
+
+test_expect_success 'setup test repository' '
+ test_write_lines 1 2 3 4 5 6 7 8 9 10 >numbers &&
+ test_write_lines A B C D E F G H I J >letters &&
+ git add numbers letters &&
+ git commit -m A &&
+
+ git branch upstream &&
+ git branch localmods &&
+
+ git checkout upstream &&
+ test_write_lines A B C D E >letters &&
+ git add letters &&
+ git commit -m B &&
+
+ test_write_lines 1 2 3 4 five 6 7 8 9 ten >numbers &&
+ git add numbers &&
+ git commit -m C &&
+
+ git checkout localmods &&
+ test_write_lines 1 2 3 4 five 6 7 8 9 10 >numbers &&
+ git add numbers &&
+ git commit -m C2 &&
+
+ git commit --allow-empty -m D &&
+
+ test_write_lines A B C D E >letters &&
+ git add letters &&
+ git commit -m "Five letters ought to be enough for anybody"
+'
+
+test_expect_failure 'rebase (apply-backend)' '
+ test_when_finished "git rebase --abort" &&
+ git checkout -B testing localmods &&
+ # rebase (--apply) should not drop commits that start empty
+ git rebase --apply upstream &&
+
+ test_write_lines D C B A >expect &&
+ git log --format=%s >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rebase --merge --empty=drop' '
+ git checkout -B testing localmods &&
+ git rebase --merge --empty=drop upstream &&
+
+ test_write_lines D C B A >expect &&
+ git log --format=%s >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rebase --merge uses default of --empty=drop' '
+ git checkout -B testing localmods &&
+ git rebase --merge upstream &&
+
+ test_write_lines D C B A >expect &&
+ git log --format=%s >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rebase --merge --empty=keep' '
+ git checkout -B testing localmods &&
+ git rebase --merge --empty=keep upstream &&
+
+ test_write_lines D C2 C B A >expect &&
+ git log --format=%s >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rebase --merge --empty=ask' '
+ git checkout -B testing localmods &&
+ test_must_fail git rebase --merge --empty=ask upstream &&
+
+ git rebase --skip &&
+
+ test_write_lines D C B A >expect &&
+ git log --format=%s >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rebase --interactive --empty=drop' '
+ git checkout -B testing localmods &&
+ git rebase --interactive --empty=drop upstream &&
+
+ test_write_lines D C B A >expect &&
+ git log --format=%s >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rebase --interactive --empty=keep' '
+ git checkout -B testing localmods &&
+ git rebase --interactive --empty=keep upstream &&
+
+ test_write_lines D C2 C B A >expect &&
+ git log --format=%s >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rebase --interactive --empty=ask' '
+ git checkout -B testing localmods &&
+ test_must_fail git rebase --interactive --empty=ask upstream &&
+
+ git rebase --skip &&
+
+ test_write_lines D C B A >expect &&
+ git log --format=%s >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rebase --interactive uses default of --empty=ask' '
+ git checkout -B testing localmods &&
+ test_must_fail git rebase --interactive upstream &&
+
+ git rebase --skip &&
+
+ test_write_lines D C B A >expect &&
+ git log --format=%s >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rebase --merge --empty=drop --keep-empty' '
+ git checkout -B testing localmods &&
+ git rebase --merge --empty=drop --keep-empty upstream &&
+
+ test_write_lines D C B A >expect &&
+ git log --format=%s >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rebase --merge --empty=drop --no-keep-empty' '
+ git checkout -B testing localmods &&
+ git rebase --merge --empty=drop --no-keep-empty upstream &&
+
+ test_write_lines C B A >expect &&
+ git log --format=%s >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rebase --merge --empty=keep --keep-empty' '
+ git checkout -B testing localmods &&
+ git rebase --merge --empty=keep --keep-empty upstream &&
+
+ test_write_lines D C2 C B A >expect &&
+ git log --format=%s >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rebase --merge --empty=keep --no-keep-empty' '
+ git checkout -B testing localmods &&
+ git rebase --merge --empty=keep --no-keep-empty upstream &&
+
+ test_write_lines C2 C B A >expect &&
+ git log --format=%s >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rebase --merge does not leave state laying around' '
+ git checkout -B testing localmods~2 &&
+ git rebase --merge upstream &&
+
+ test_path_is_missing .git/CHERRY_PICK_HEAD &&
+ test_path_is_missing .git/MERGE_MSG
+'
+
+test_done
diff --git a/t/t3425-rebase-topology-merges.sh b/t/t3425-rebase-topology-merges.sh
new file mode 100755
index 0000000..a16428b
--- /dev/null
+++ b/t/t3425-rebase-topology-merges.sh
@@ -0,0 +1,111 @@
+#!/bin/sh
+
+test_description='rebase topology tests with merges'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-rebase.sh
+
+test_revision_subjects () {
+ expected="$1"
+ shift
+ set -- $(git log --format=%s --no-walk=unsorted "$@")
+ test "$expected" = "$*"
+}
+
+# a---b-----------c
+# \ \
+# d-------e \
+# \ \ \
+# n---o---w---v
+# \
+# z
+test_expect_success 'setup of non-linear-history' '
+ test_commit a &&
+ test_commit b &&
+ test_commit c &&
+ git checkout b &&
+ test_commit d &&
+ test_commit e &&
+
+ git checkout c &&
+ test_commit g &&
+ revert h g &&
+ git checkout d &&
+ cherry_pick gp g &&
+ test_commit i &&
+ git checkout b &&
+ test_commit f &&
+
+ git checkout d &&
+ test_commit n &&
+ test_commit o &&
+ test_merge w e &&
+ test_merge v c &&
+ git checkout o &&
+ test_commit z
+'
+
+test_run_rebase () {
+ result=$1
+ shift
+ test_expect_$result "rebase $* after merge from upstream" "
+ reset_rebase &&
+ git rebase $* e w &&
+ test_cmp_rev e HEAD~2 &&
+ test_linear_range 'n o' e..
+ "
+}
+test_run_rebase success --apply
+test_run_rebase success -m
+test_run_rebase success -i
+
+test_run_rebase () {
+ result=$1
+ shift
+ expected=$1
+ shift
+ test_expect_$result "rebase $* of non-linear history is linearized in place" "
+ reset_rebase &&
+ git rebase $* d w &&
+ test_cmp_rev d HEAD~3 &&
+ test_linear_range "\'"$expected"\'" d..
+ "
+}
+test_run_rebase success 'n o e' --apply
+test_run_rebase success 'n o e' -m
+test_run_rebase success 'n o e' -i
+
+test_run_rebase () {
+ result=$1
+ shift
+ expected=$1
+ shift
+ test_expect_$result "rebase $* of non-linear history is linearized upstream" "
+ reset_rebase &&
+ git rebase $* c w &&
+ test_cmp_rev c HEAD~4 &&
+ test_linear_range "\'"$expected"\'" c..
+ "
+}
+test_run_rebase success 'd n o e' --apply
+test_run_rebase success 'd n o e' -m
+test_run_rebase success 'd n o e' -i
+
+test_run_rebase () {
+ result=$1
+ shift
+ expected=$1
+ shift
+ test_expect_$result "rebase $* of non-linear history with merges after upstream merge is linearized" "
+ reset_rebase &&
+ git rebase $* c v &&
+ test_cmp_rev c HEAD~4 &&
+ test_linear_range "\'"$expected"\'" c..
+ "
+}
+test_run_rebase success 'd n o e' --apply
+test_run_rebase success 'd n o e' -m
+test_run_rebase success 'd n o e' -i
+
+test_done
diff --git a/t/t3426-rebase-submodule.sh b/t/t3426-rebase-submodule.sh
new file mode 100755
index 0000000..ba069dc
--- /dev/null
+++ b/t/t3426-rebase-submodule.sh
@@ -0,0 +1,64 @@
+#!/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 &&
+ may_only_be_test_must_fail "$2" &&
+ $2 git rebase "$1"
+}
+
+test_submodule_switch_func "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 &&
+ mkdir .git/info &&
+ echo "fake-editor.sh" >.git/info/exclude &&
+ may_only_be_test_must_fail "$2" &&
+ $2 git rebase -i "$1"
+}
+
+test_submodule_switch_func "git_rebase_interactive"
+
+test_expect_success 'rebase interactive ignores modified submodules' '
+ test_when_finished "rm -rf super sub" &&
+ git init sub &&
+ git -C sub commit --allow-empty -m "Initial commit" &&
+ git init super &&
+ git -c protocol.file.allow=always \
+ -C super submodule add ../sub &&
+ git -C super config submodule.sub.ignore dirty &&
+ >super/foo &&
+ git -C super add foo &&
+ git -C super commit -m "Initial commit" &&
+ test_commit -C super a &&
+ test_commit -C super b &&
+ test_commit -C super/sub c &&
+ set_fake_editor &&
+ git -C super rebase -i HEAD^^
+'
+
+test_done
diff --git a/t/t3427-rebase-subtree.sh b/t/t3427-rebase-subtree.sh
new file mode 100755
index 0000000..1b3e97c
--- /dev/null
+++ b/t/t3427-rebase-subtree.sh
@@ -0,0 +1,93 @@
+#!/bin/sh
+
+test_description='git rebase tests for -Xsubtree
+
+This test runs git rebase and tests the subtree strategy.
+'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-rebase.sh
+
+commit_message() {
+ git log --pretty=format:%s -1 "$1"
+}
+
+# There are a few bugs in the rebase with regards to the subtree strategy, and
+# this test script tries to document them. First, the following commit history
+# is generated (the onelines are shown, time flows from left to right):
+#
+# topic_1 - topic_2 - topic_3
+# \
+# README ---------------------- Add subproject main - topic_4 - files_subtree/topic_5
+#
+# Where the merge moves the files topic_[123].t into the subdirectory
+# files_subtree/ and topic_4 as well as files_subtree/topic_5 add files to that
+# directory directly.
+#
+# Then, in subsequent test cases, `git filter-branch` is used to distill just
+# the commits that touch files_subtree/. To give it a final pre-rebase touch,
+# an empty commit is added on top. The pre-rebase commit history looks like
+# this:
+#
+# Add subproject main - topic_4 - files_subtree/topic_5 - Empty commit
+#
+# where the root commit adds three files: topic_1.t, topic_2.t and topic_3.t.
+#
+# This commit history is then rebased onto `topic_3` with the
+# `-Xsubtree=files_subtree` option in two different ways:
+#
+# 1. without specifying a rebase backend
+# 2. using the `--rebase-merges` backend
+
+test_expect_success 'setup' '
+ test_commit README &&
+
+ git init files &&
+ test_commit -C files topic_1 &&
+ test_commit -C files topic_2 &&
+ test_commit -C files topic_3 &&
+
+ : perform subtree merge into files_subtree/ &&
+ git fetch files refs/heads/main:refs/heads/files-main &&
+ git merge -s ours --no-commit --allow-unrelated-histories \
+ files-main &&
+ git read-tree --prefix=files_subtree -u files-main &&
+ git commit -m "Add subproject main" &&
+
+ : add two extra commits to rebase &&
+ test_commit -C files_subtree topic_4 &&
+ test_commit files_subtree/topic_5 &&
+
+ git checkout -b to-rebase &&
+ git fast-export --no-data HEAD -- files_subtree/ |
+ sed -e "s%\([0-9a-f]\{40\} \)files_subtree/%\1%" |
+ git fast-import --force --quiet &&
+ git reset --hard &&
+ git commit -m "Empty commit" --allow-empty
+'
+
+test_expect_success 'Rebase -Xsubtree --empty=ask --onto commit' '
+ reset_rebase &&
+ git checkout -b rebase-onto to-rebase &&
+ test_must_fail git rebase -Xsubtree=files_subtree --empty=ask --onto files-main main &&
+ : first pick results in no changes &&
+ git rebase --skip &&
+ test "$(commit_message HEAD~2)" = "topic_4" &&
+ test "$(commit_message HEAD~)" = "files_subtree/topic_5" &&
+ test "$(commit_message HEAD)" = "Empty commit"
+'
+
+test_expect_success 'Rebase -Xsubtree --empty=ask --rebase-merges --onto commit' '
+ reset_rebase &&
+ git checkout -b rebase-merges-onto to-rebase &&
+ test_must_fail git rebase -Xsubtree=files_subtree --empty=ask --rebase-merges --onto files-main --root &&
+ : first pick results in no changes &&
+ git rebase --skip &&
+ test "$(commit_message HEAD~2)" = "topic_4" &&
+ test "$(commit_message HEAD~)" = "files_subtree/topic_5" &&
+ test "$(commit_message HEAD)" = "Empty commit"
+'
+
+test_done
diff --git a/t/t3428-rebase-signoff.sh b/t/t3428-rebase-signoff.sh
new file mode 100755
index 0000000..e1b1e94
--- /dev/null
+++ b/t/t3428-rebase-signoff.sh
@@ -0,0 +1,85 @@
+#!/bin/sh
+
+test_description='git rebase --signoff
+
+This test runs git rebase --signoff and make sure that it works.
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# A simple file to commit
+cat >file <<EOF
+a
+EOF
+
+# Expected commit message for initial commit after rebase --signoff
+cat >expected-initial-signed <<EOF
+Initial empty commit
+
+Signed-off-by: $(git var GIT_COMMITTER_IDENT | sed -e "s/>.*/>/")
+EOF
+
+# Expected commit message after rebase --signoff
+cat >expected-signed <<EOF
+first
+
+Signed-off-by: $(git var GIT_COMMITTER_IDENT | sed -e "s/>.*/>/")
+EOF
+
+# Expected commit message after rebase without --signoff (or with --no-signoff)
+cat >expected-unsigned <<EOF
+first
+EOF
+
+
+# We configure an alias to do the rebase --signoff so that
+# on the next subtest we can show that --no-signoff overrides the alias
+test_expect_success 'rebase --signoff adds a sign-off line' '
+ git commit --allow-empty -m "Initial empty commit" &&
+ git add file && git commit -m first &&
+ git config alias.rbs "rebase --signoff" &&
+ git rbs HEAD^ &&
+ git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
+ test_cmp expected-signed actual
+'
+
+test_expect_success 'rebase --no-signoff does not add a sign-off line' '
+ git commit --amend -m "first" &&
+ git rbs --no-signoff HEAD^ &&
+ git cat-file commit HEAD | sed -e "1,/^\$/d" > actual &&
+ test_cmp expected-unsigned actual
+'
+
+test_expect_success 'rebase --exec --signoff adds a sign-off line' '
+ test_when_finished "rm exec" &&
+ git commit --amend -m "first" &&
+ git rebase --exec "touch exec" --signoff HEAD^ &&
+ test_path_is_file exec &&
+ git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
+ test_cmp expected-signed actual
+'
+
+test_expect_success 'rebase --root --signoff adds a sign-off line' '
+ git commit --amend -m "first" &&
+ git rebase --root --keep-empty --signoff &&
+ git cat-file commit HEAD^ | sed -e "1,/^\$/d" >actual &&
+ test_cmp expected-initial-signed actual &&
+ git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
+ test_cmp expected-signed actual
+'
+
+test_expect_success 'rebase -i --signoff fails' '
+ git commit --amend -m "first" &&
+ git rebase -i --signoff HEAD^ &&
+ git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
+ test_cmp expected-signed actual
+'
+
+test_expect_success 'rebase -m --signoff fails' '
+ git commit --amend -m "first" &&
+ git rebase -m --signoff HEAD^ &&
+ git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
+ test_cmp expected-signed actual
+'
+test_done
diff --git a/t/t3429-rebase-edit-todo.sh b/t/t3429-rebase-edit-todo.sh
new file mode 100755
index 0000000..8e0d039
--- /dev/null
+++ b/t/t3429-rebase-edit-todo.sh
@@ -0,0 +1,91 @@
+#!/bin/sh
+
+test_description='rebase should reread the todo file if an exec modifies it'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-rebase.sh
+
+test_expect_success 'setup' '
+ test_commit first file &&
+ test_commit second file &&
+ test_commit third file
+'
+
+test_expect_success 'rebase exec modifies rebase-todo' '
+ todo=.git/rebase-merge/git-rebase-todo &&
+ git rebase HEAD~1 -x "echo exec touch F >>$todo" &&
+ test -e F
+'
+
+test_expect_success 'rebase exec with an empty list does not exec anything' '
+ git rebase HEAD -x "true" 2>output &&
+ ! grep "Executing: true" output
+'
+
+test_expect_success 'loose object cache vs re-reading todo list' '
+ GIT_REBASE_TODO=.git/rebase-merge/git-rebase-todo &&
+ export GIT_REBASE_TODO &&
+ write_script append-todo.sh <<-\EOS &&
+ # For values 5 and 6, this yields SHA-1s with the same first two digits
+ echo "pick $(git rev-parse --short \
+ $(printf "%s\\n" \
+ "tree $EMPTY_TREE" \
+ "author A U Thor <author@example.org> $1 +0000" \
+ "committer A U Thor <author@example.org> $1 +0000" \
+ "" \
+ "$1" |
+ git hash-object -t commit -w --stdin))" >>$GIT_REBASE_TODO
+
+ shift
+ test -z "$*" ||
+ echo "exec $0 $*" >>$GIT_REBASE_TODO
+ EOS
+
+ git rebase HEAD -x "./append-todo.sh 5 6"
+'
+
+test_expect_success 'todo is re-read after reword and squash' '
+ write_script reword-editor.sh <<-\EOS &&
+ GIT_SEQUENCE_EDITOR="echo \"exec echo $(cat file) >>actual\" >>" \
+ git rebase --edit-todo
+ EOS
+
+ test_write_lines first third >expected &&
+ set_fake_editor &&
+ GIT_SEQUENCE_EDITOR="$EDITOR" FAKE_LINES="reword 1 squash 2 fixup 3" \
+ GIT_EDITOR=./reword-editor.sh git rebase -i --root third &&
+ test_cmp expected actual
+'
+
+test_expect_success 're-reading todo doesnt interfere with revert --edit' '
+ git reset --hard third &&
+
+ git revert --edit third second &&
+
+ cat >expect <<-\EOF &&
+ Revert "second"
+ Revert "third"
+ third
+ second
+ first
+ EOF
+ git log --format="%s" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 're-reading todo doesnt interfere with cherry-pick --edit' '
+ git reset --hard first &&
+
+ git cherry-pick --edit second third &&
+
+ cat >expect <<-\EOF &&
+ third
+ second
+ first
+ EOF
+ git log --format="%s" >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t3430-rebase-merges.sh b/t/t3430-rebase-merges.sh
new file mode 100755
index 0000000..59b5d6b
--- /dev/null
+++ b/t/t3430-rebase-merges.sh
@@ -0,0 +1,614 @@
+#!/bin/sh
+#
+# Copyright (c) 2018 Johannes E. Schindelin
+#
+
+test_description='git rebase -i --rebase-merges
+
+This test runs git rebase "interactively", retaining the branch structure by
+recreating merge commits.
+
+Initial setup:
+
+ -- B -- (first)
+ / \
+ A - C - D - E - H (main)
+ \ \ /
+ \ F - G (second)
+ \
+ Conflicting-G
+'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-rebase.sh
+. "$TEST_DIRECTORY"/lib-log-graph.sh
+
+test_cmp_graph () {
+ cat >expect &&
+ lib_test_cmp_graph --boundary --format=%s "$@"
+}
+
+test_expect_success 'setup' '
+ write_script replace-editor.sh <<-\EOF &&
+ mv "$1" "$(git rev-parse --git-path ORIGINAL-TODO)"
+ cp script-from-scratch "$1"
+ EOF
+
+ test_commit A &&
+ git checkout -b first &&
+ test_commit B &&
+ b=$(git rev-parse --short HEAD) &&
+ git checkout main &&
+ test_commit C &&
+ c=$(git rev-parse --short HEAD) &&
+ test_commit D &&
+ d=$(git rev-parse --short HEAD) &&
+ git merge --no-commit B &&
+ test_tick &&
+ git commit -m E &&
+ git tag -m E E &&
+ e=$(git rev-parse --short HEAD) &&
+ git checkout -b second C &&
+ test_commit F &&
+ f=$(git rev-parse --short HEAD) &&
+ test_commit G &&
+ g=$(git rev-parse --short HEAD) &&
+ git checkout main &&
+ git merge --no-commit G &&
+ test_tick &&
+ git commit -m H &&
+ h=$(git rev-parse --short HEAD) &&
+ git tag -m H H &&
+ git checkout A &&
+ test_commit conflicting-G G.t
+'
+
+test_expect_success 'create completely different structure' '
+ cat >script-from-scratch <<-\EOF &&
+ label onto
+
+ # onebranch
+ pick G
+ pick D
+ label onebranch
+
+ # second
+ reset onto
+ pick B
+ label second
+
+ reset onto
+ merge -C H second
+ merge onebranch # Merge the topic branch '\''onebranch'\''
+ EOF
+ test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
+ test_tick &&
+ git rebase -i -r A main &&
+ test_cmp_graph <<-\EOF
+ * Merge the topic branch '\''onebranch'\''
+ |\
+ | * D
+ | * G
+ * | H
+ |\ \
+ | |/
+ |/|
+ | * B
+ |/
+ * A
+ EOF
+'
+
+test_expect_success 'generate correct todo list' '
+ cat >expect <<-EOF &&
+ label onto
+
+ reset onto
+ pick $b B
+ label E
+
+ reset onto
+ pick $c C
+ label branch-point
+ pick $f F
+ pick $g G
+ label H
+
+ reset branch-point # C
+ pick $d D
+ merge -C $e E # E
+ merge -C $h H # H
+
+ EOF
+
+ grep -v "^#" <.git/ORIGINAL-TODO >output &&
+ test_cmp expect output
+'
+
+test_expect_success '`reset` refuses to overwrite untracked files' '
+ git checkout B &&
+ test_commit dont-overwrite-untracked &&
+ cat >script-from-scratch <<-EOF &&
+ exec >dont-overwrite-untracked.t
+ pick $(git rev-parse B) B
+ reset refs/tags/dont-overwrite-untracked
+ pick $(git rev-parse C) C
+ exec cat .git/rebase-merge/done >actual
+ EOF
+ test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
+ test_must_fail git rebase -ir A &&
+ test_cmp_rev HEAD B &&
+ head -n3 script-from-scratch >expect &&
+ test_cmp expect .git/rebase-merge/done &&
+ rm dont-overwrite-untracked.t &&
+ git rebase --continue &&
+ tail -n3 script-from-scratch >>expect &&
+ test_cmp expect actual
+'
+
+test_expect_success '`reset` rejects trees' '
+ test_when_finished "test_might_fail git rebase --abort" &&
+ test_must_fail env GIT_SEQUENCE_EDITOR="echo reset A^{tree} >" \
+ git rebase -i B C >out 2>err &&
+ grep "object .* is a tree" err &&
+ test_must_be_empty out
+'
+
+test_expect_success '`reset` only looks for labels under refs/rewritten/' '
+ test_when_finished "test_might_fail git rebase --abort" &&
+ git branch refs/rewritten/my-label A &&
+ test_must_fail env GIT_SEQUENCE_EDITOR="echo reset my-label >" \
+ git rebase -i B C >out 2>err &&
+ grep "could not resolve ${SQ}my-label${SQ}" err &&
+ test_must_be_empty out
+'
+
+test_expect_success 'failed `merge -C` writes patch (may be rescheduled, too)' '
+ test_when_finished "test_might_fail git rebase --abort" &&
+ git checkout -b conflicting-merge A &&
+
+ : fail because of conflicting untracked file &&
+ >G.t &&
+ echo "merge -C H G" >script-from-scratch &&
+ test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
+ test_tick &&
+ test_must_fail git rebase -ir HEAD &&
+ test_cmp_rev REBASE_HEAD H^0 &&
+ grep "^merge -C .* G$" .git/rebase-merge/done &&
+ grep "^merge -C .* G$" .git/rebase-merge/git-rebase-todo &&
+ test_path_is_missing .git/rebase-merge/patch &&
+ echo changed >file1 &&
+ git add file1 &&
+ test_must_fail git rebase --continue 2>err &&
+ grep "error: you have staged changes in your working tree" err &&
+
+ : fail because of merge conflict &&
+ git reset --hard conflicting-G &&
+ test_must_fail git rebase --continue &&
+ ! grep "^merge -C .* G$" .git/rebase-merge/git-rebase-todo &&
+ test_path_is_file .git/rebase-merge/patch
+'
+
+test_expect_success 'failed `merge <branch>` does not crash' '
+ test_when_finished "test_might_fail git rebase --abort" &&
+ git checkout conflicting-G &&
+
+ echo "merge G" >script-from-scratch &&
+ test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
+ test_tick &&
+ test_must_fail git rebase -ir HEAD &&
+ ! grep "^merge G$" .git/rebase-merge/git-rebase-todo &&
+ grep "^Merge branch ${SQ}G${SQ}$" .git/rebase-merge/message
+'
+
+test_expect_success 'merge -c commits before rewording and reloads todo-list' '
+ cat >script-from-scratch <<-\EOF &&
+ merge -c E B
+ merge -c H G
+ EOF
+
+ git checkout -b merge-c H &&
+ (
+ set_reword_editor &&
+ GIT_SEQUENCE_EDITOR="\"$PWD/replace-editor.sh\"" \
+ git rebase -i -r D
+ ) &&
+ check_reworded_commits E H
+'
+
+test_expect_success 'merge -c rewords when a strategy is given' '
+ git checkout -b merge-c-with-strategy H &&
+ write_script git-merge-override <<-\EOF &&
+ echo overridden$1 >G.t
+ git add G.t
+ EOF
+
+ PATH="$PWD:$PATH" \
+ GIT_SEQUENCE_EDITOR="echo merge -c H G >" \
+ GIT_EDITOR="echo edited >>" \
+ git rebase --no-ff -ir -s override -Xxopt E &&
+ test_write_lines overridden--xopt >expect &&
+ test_cmp expect G.t &&
+ test_write_lines H "" edited "" >expect &&
+ git log --format=%B -1 >actual &&
+ test_cmp expect actual
+
+'
+test_expect_success 'with a branch tip that was cherry-picked already' '
+ git checkout -b already-upstream main &&
+ base="$(git rev-parse --verify HEAD)" &&
+
+ test_commit A1 &&
+ test_commit A2 &&
+ git reset --hard $base &&
+ test_commit B1 &&
+ test_tick &&
+ git merge -m "Merge branch A" A2 &&
+
+ git checkout -b upstream-with-a2 $base &&
+ test_tick &&
+ git cherry-pick A2 &&
+
+ git checkout already-upstream &&
+ test_tick &&
+ git rebase -i -r upstream-with-a2 &&
+ test_cmp_graph upstream-with-a2.. <<-\EOF
+ * Merge branch A
+ |\
+ | * A1
+ * | B1
+ |/
+ o A2
+ EOF
+'
+
+test_expect_success '--no-rebase-merges countermands --rebase-merges' '
+ git checkout -b no-rebase-merges E &&
+ git rebase --rebase-merges --no-rebase-merges C &&
+ test_cmp_graph C.. <<-\EOF
+ * B
+ * D
+ o C
+ EOF
+'
+
+test_expect_success 'do not rebase cousins unless asked for' '
+ git checkout -b cousins main &&
+ before="$(git rev-parse --verify HEAD)" &&
+ test_tick &&
+ git rebase -r HEAD^ &&
+ test_cmp_rev HEAD $before &&
+ test_tick &&
+ git rebase --rebase-merges=rebase-cousins HEAD^ &&
+ test_cmp_graph HEAD^.. <<-\EOF
+ * Merge the topic branch '\''onebranch'\''
+ |\
+ | * D
+ | * G
+ |/
+ o H
+ EOF
+'
+
+test_expect_success 'rebase.rebaseMerges=rebase-cousins is equivalent to --rebase-merges=rebase-cousins' '
+ test_config rebase.rebaseMerges rebase-cousins &&
+ git checkout -b config-rebase-cousins main &&
+ git rebase HEAD^ &&
+ test_cmp_graph HEAD^.. <<-\EOF
+ * Merge the topic branch '\''onebranch'\''
+ |\
+ | * D
+ | * G
+ |/
+ o H
+ EOF
+'
+
+test_expect_success '--no-rebase-merges overrides rebase.rebaseMerges=no-rebase-cousins' '
+ test_config rebase.rebaseMerges no-rebase-cousins &&
+ git checkout -b override-config-no-rebase-cousins E &&
+ git rebase --no-rebase-merges C &&
+ test_cmp_graph C.. <<-\EOF
+ * B
+ * D
+ o C
+ EOF
+'
+
+test_expect_success '--rebase-merges overrides rebase.rebaseMerges=rebase-cousins' '
+ test_config rebase.rebaseMerges rebase-cousins &&
+ git checkout -b override-config-rebase-cousins E &&
+ before="$(git rev-parse --verify HEAD)" &&
+ test_tick &&
+ git rebase --rebase-merges C &&
+ test_cmp_rev HEAD $before
+'
+
+test_expect_success 'refs/rewritten/* is worktree-local' '
+ git worktree add wt &&
+ cat >wt/script-from-scratch <<-\EOF &&
+ label xyz
+ exec GIT_DIR=../.git git rev-parse --verify refs/rewritten/xyz >a || :
+ exec git rev-parse --verify refs/rewritten/xyz >b
+ EOF
+
+ test_config -C wt sequence.editor \""$PWD"/replace-editor.sh\" &&
+ git -C wt rebase -i HEAD &&
+ test_must_be_empty wt/a &&
+ test_cmp_rev HEAD "$(cat wt/b)"
+'
+
+test_expect_success '--abort cleans up refs/rewritten' '
+ git checkout -b abort-cleans-refs-rewritten H &&
+ GIT_SEQUENCE_EDITOR="echo break >>" git rebase -ir @^ &&
+ git rev-parse --verify refs/rewritten/onto &&
+ git rebase --abort &&
+ test_must_fail git rev-parse --verify refs/rewritten/onto
+'
+
+test_expect_success '--quit cleans up refs/rewritten' '
+ git checkout -b quit-cleans-refs-rewritten H &&
+ GIT_SEQUENCE_EDITOR="echo break >>" git rebase -ir @^ &&
+ git rev-parse --verify refs/rewritten/onto &&
+ git rebase --quit &&
+ test_must_fail git rev-parse --verify refs/rewritten/onto
+'
+
+test_expect_success 'post-rewrite hook and fixups work for merges' '
+ git checkout -b post-rewrite H &&
+ test_commit same1 &&
+ git reset --hard HEAD^ &&
+ test_commit same2 &&
+ git merge -m "to fix up" same1 &&
+ echo same old same old >same2.t &&
+ test_tick &&
+ git commit --fixup HEAD same2.t &&
+ fixup="$(git rev-parse HEAD)" &&
+
+ test_hook post-rewrite <<-\EOF &&
+ cat >actual
+ EOF
+
+ test_tick &&
+ git rebase -i --autosquash -r HEAD^^^ &&
+ printf "%s %s\n%s %s\n%s %s\n%s %s\n" >expect $(git rev-parse \
+ $fixup^^2 HEAD^2 \
+ $fixup^^ HEAD^ \
+ $fixup^ HEAD \
+ $fixup HEAD) &&
+ test_cmp expect actual
+'
+
+test_expect_success 'refuse to merge ancestors of HEAD' '
+ echo "merge HEAD^" >script-from-scratch &&
+ test_config -C wt sequence.editor \""$PWD"/replace-editor.sh\" &&
+ before="$(git rev-parse HEAD)" &&
+ git rebase -i HEAD &&
+ test_cmp_rev HEAD $before
+'
+
+test_expect_success 'root commits' '
+ git checkout --orphan unrelated &&
+ (GIT_AUTHOR_NAME="Parsnip" GIT_AUTHOR_EMAIL="root@example.com" \
+ test_commit second-root) &&
+ test_commit third-root &&
+ cat >script-from-scratch <<-\EOF &&
+ pick third-root
+ label first-branch
+ reset [new root]
+ pick second-root
+ merge first-branch # Merge the 3rd root
+ EOF
+ test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
+ test_tick &&
+ git rebase -i --force-rebase --root -r &&
+ test "Parsnip" = "$(git show -s --format=%an HEAD^)" &&
+ test $(git rev-parse second-root^0) != $(git rev-parse HEAD^) &&
+ test $(git rev-parse second-root:second-root.t) = \
+ $(git rev-parse HEAD^:second-root.t) &&
+ test_cmp_graph HEAD <<-\EOF &&
+ * Merge the 3rd root
+ |\
+ | * third-root
+ * second-root
+ EOF
+
+ : fast forward if possible &&
+ before="$(git rev-parse --verify HEAD)" &&
+ test_might_fail git config --unset sequence.editor &&
+ test_tick &&
+ git rebase -i --root -r &&
+ test_cmp_rev HEAD $before
+'
+
+test_expect_success 'a "merge" into a root commit is a fast-forward' '
+ head=$(git rev-parse HEAD) &&
+ cat >script-from-scratch <<-EOF &&
+ reset [new root]
+ merge $head
+ EOF
+ test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
+ test_tick &&
+ git rebase -i -r HEAD^ &&
+ test_cmp_rev HEAD $head
+'
+
+test_expect_success 'A root commit can be a cousin, treat it that way' '
+ git checkout --orphan khnum &&
+ test_commit yama &&
+ git checkout -b asherah main &&
+ test_commit shamkat &&
+ git merge --allow-unrelated-histories khnum &&
+ test_tick &&
+ git rebase -f -r HEAD^ &&
+ test_cmp_rev ! HEAD^2 khnum &&
+ test_cmp_graph HEAD^.. <<-\EOF &&
+ * Merge branch '\''khnum'\'' into asherah
+ |\
+ | * yama
+ o shamkat
+ EOF
+ test_tick &&
+ git rebase --rebase-merges=rebase-cousins HEAD^ &&
+ test_cmp_graph HEAD^.. <<-\EOF
+ * Merge branch '\''khnum'\'' into asherah
+ |\
+ | * yama
+ |/
+ o shamkat
+ EOF
+'
+
+test_expect_success 'labels that are object IDs are rewritten' '
+ git checkout -b third B &&
+ test_commit I &&
+ third=$(git rev-parse HEAD) &&
+ git checkout -b labels main &&
+ git merge --no-commit third &&
+ test_tick &&
+ git commit -m "Merge commit '\''$third'\'' into labels" &&
+ echo noop >script-from-scratch &&
+ test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
+ test_tick &&
+ git rebase -i -r A &&
+ grep "^label $third-" .git/ORIGINAL-TODO &&
+ ! grep "^label $third$" .git/ORIGINAL-TODO
+'
+
+test_expect_success 'octopus merges' '
+ git checkout -b three &&
+ test_commit before-octopus &&
+ test_commit three &&
+ git checkout -b two HEAD^ &&
+ test_commit two &&
+ git checkout -b one HEAD^ &&
+ test_commit one &&
+ test_tick &&
+ (GIT_AUTHOR_NAME="Hank" GIT_AUTHOR_EMAIL="hank@sea.world" \
+ git merge -m "Tüntenfüsch" two three) &&
+
+ : fast forward if possible &&
+ before="$(git rev-parse --verify HEAD)" &&
+ test_tick &&
+ git rebase -i -r HEAD^^ &&
+ test_cmp_rev HEAD $before &&
+
+ test_tick &&
+ git rebase -i --force-rebase -r HEAD^^ &&
+ test "Hank" = "$(git show -s --format=%an HEAD)" &&
+ test "$before" != $(git rev-parse HEAD) &&
+ test_cmp_graph HEAD^^.. <<-\EOF
+ *-. Tüntenfüsch
+ |\ \
+ | | * three
+ | * | two
+ | |/
+ * / one
+ |/
+ o before-octopus
+ EOF
+'
+
+test_expect_success 'with --autosquash and --exec' '
+ git checkout -b with-exec H &&
+ echo Booh >B.t &&
+ test_tick &&
+ git commit --fixup B B.t &&
+ write_script show.sh <<-\EOF &&
+ subject="$(git show -s --format=%s HEAD)"
+ content="$(git diff HEAD^ HEAD | tail -n 1)"
+ echo "$subject: $content"
+ EOF
+ test_tick &&
+ git rebase -ir --autosquash --exec ./show.sh A >actual &&
+ grep "B: +Booh" actual &&
+ grep "E: +Booh" actual &&
+ grep "G: +G" actual
+'
+
+test_expect_success '--continue after resolving conflicts after a merge' '
+ git checkout -b already-has-g E &&
+ git cherry-pick E..G &&
+ test_commit H2 &&
+
+ git checkout -b conflicts-in-merge H &&
+ test_commit H2 H2.t conflicts H2-conflict &&
+ test_must_fail git rebase -r already-has-g &&
+ grep conflicts H2.t &&
+ echo resolved >H2.t &&
+ git add -u &&
+ git rebase --continue &&
+ test_must_fail git rev-parse --verify HEAD^2 &&
+ test_path_is_missing .git/MERGE_HEAD
+'
+
+test_expect_success '--rebase-merges with strategies' '
+ git checkout -b with-a-strategy F &&
+ test_tick &&
+ git merge -m "Merge conflicting-G" conflicting-G &&
+
+ : first, test with a merge strategy option &&
+ git rebase -ir -Xtheirs G &&
+ echo conflicting-G >expect &&
+ test_cmp expect G.t &&
+
+ : now, try with a merge strategy other than recursive &&
+ git reset --hard @{1} &&
+ write_script git-merge-override <<-\EOF &&
+ echo overridden$1 >>G.t
+ git add G.t
+ EOF
+ PATH="$PWD:$PATH" git rebase -ir -s override -Xxopt G &&
+ test_write_lines G overridden--xopt >expect &&
+ test_cmp expect G.t
+'
+
+test_expect_success '--rebase-merges with commit that can generate bad characters for filename' '
+ git checkout -b colon-in-label E &&
+ git merge -m "colon: this should work" G &&
+ git rebase --rebase-merges --force-rebase E
+'
+
+test_expect_success '--rebase-merges with message matched with onto label' '
+ git checkout -b onto-label E &&
+ git merge -m onto G &&
+ git rebase --rebase-merges --force-rebase E &&
+ test_cmp_graph <<-\EOF
+ * onto
+ |\
+ | * G
+ | * F
+ * | E
+ |\ \
+ | * | B
+ * | | D
+ | |/
+ |/|
+ * | C
+ |/
+ * A
+ EOF
+'
+
+test_expect_success 'progress shows the correct total' '
+ git checkout -b progress H &&
+ git rebase --rebase-merges --force-rebase --verbose A 2> err &&
+ # Expecting "Rebasing (N/14)" here, no bogus total number
+ grep "^Rebasing.*/14.$" err >progress &&
+ test_line_count = 14 progress
+'
+
+test_expect_success 'truncate label names' '
+ commit=$(git commit-tree -p HEAD^ -p HEAD -m "0123456789 我 123" HEAD^{tree}) &&
+ git merge --ff-only $commit &&
+
+ done="$(git rev-parse --git-path rebase-merge/done)" &&
+ git -c rebase.maxLabelLength=14 rebase --rebase-merges -x "cp \"$done\" out" --root &&
+ grep "label 0123456789-我$" out &&
+ git -c rebase.maxLabelLength=13 rebase --rebase-merges -x "cp \"$done\" out" --root &&
+ grep "label 0123456789-$" out
+'
+
+test_done
diff --git a/t/t3431-rebase-fork-point.sh b/t/t3431-rebase-fork-point.sh
new file mode 100755
index 0000000..0bb284d
--- /dev/null
+++ b/t/t3431-rebase-fork-point.sh
@@ -0,0 +1,116 @@
+#!/bin/sh
+#
+# Copyright (c) 2019 Denton Liu
+#
+
+test_description='git rebase --fork-point test'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# A---B---D---E (main)
+# \
+# C*---F---G (side)
+#
+# C was formerly part of main but main was rewound to remove C
+#
+test_expect_success setup '
+ test_commit A &&
+ test_commit B &&
+ test_commit C &&
+ git branch -t side &&
+ git reset --hard HEAD^ &&
+ test_commit D &&
+ test_commit E &&
+ git checkout side &&
+ test_commit F &&
+ test_commit G
+'
+
+do_test_rebase () {
+ expected="$1" &&
+ shift &&
+ git checkout main &&
+ git reset --hard E &&
+ git checkout side &&
+ git reset --hard G &&
+ git rebase $* &&
+ test_write_lines $expected >expect &&
+ git log --pretty=%s >actual &&
+ test_cmp expect actual
+}
+
+test_rebase () {
+ expected="$1" &&
+ shift &&
+ test_expect_success "git rebase $*" "do_test_rebase '$expected' $*"
+}
+
+test_rebase 'G F E D B A'
+test_rebase 'G F D B A' --onto D
+test_rebase 'G F C B A' --keep-base
+test_rebase 'G F C E D B A' --no-fork-point
+test_rebase 'G F C D B A' --no-fork-point --onto D
+test_rebase 'G F C B A' --no-fork-point --keep-base
+
+test_rebase 'G F E D B A' --fork-point refs/heads/main
+test_rebase 'G F E D B A' --fork-point main
+
+test_rebase 'G F D B A' --fork-point --onto D refs/heads/main
+test_rebase 'G F D B A' --fork-point --onto D main
+
+test_rebase 'G F B A' --fork-point --keep-base refs/heads/main
+test_rebase 'G F B A' --fork-point --keep-base main
+
+test_rebase 'G F C E D B A' refs/heads/main
+test_rebase 'G F C E D B A' main
+
+test_rebase 'G F C D B A' --onto D refs/heads/main
+test_rebase 'G F C D B A' --onto D main
+
+test_rebase 'G F C B A' --keep-base refs/heads/main
+test_rebase 'G F C B A' --keep-base main
+
+test_expect_success 'git rebase --fork-point with ambigous refname' '
+ git checkout main &&
+ git checkout -b one &&
+ git checkout side &&
+ git tag one &&
+ test_must_fail git rebase --fork-point --onto D one
+'
+
+test_expect_success '--fork-point and --root both given' '
+ test_must_fail git rebase --fork-point --root 2>err &&
+ test_grep "cannot be used together" err
+'
+
+test_expect_success 'rebase.forkPoint set to false' '
+ test_config rebase.forkPoint false &&
+ do_test_rebase "G F C E D B A"
+'
+
+test_expect_success 'rebase.forkPoint set to false and then to true' '
+ test_config_global rebase.forkPoint false &&
+ test_config rebase.forkPoint true &&
+ do_test_rebase "G F E D B A"
+'
+
+test_expect_success 'rebase.forkPoint set to false and command line says --fork-point' '
+ test_config rebase.forkPoint false &&
+ do_test_rebase "G F E D B A" --fork-point
+'
+
+test_expect_success 'rebase.forkPoint set to true and command line says --no-fork-point' '
+ test_config rebase.forkPoint true &&
+ do_test_rebase "G F C E D B A" --no-fork-point
+'
+
+test_expect_success 'rebase.forkPoint set to true and --root given' '
+ test_config rebase.forkPoint true &&
+ git rebase --root
+'
+
+test_done
diff --git a/t/t3432-rebase-fast-forward.sh b/t/t3432-rebase-fast-forward.sh
new file mode 100755
index 0000000..7f1a5dd
--- /dev/null
+++ b/t/t3432-rebase-fast-forward.sh
@@ -0,0 +1,142 @@
+#!/bin/sh
+#
+# Copyright (c) 2019 Denton Liu
+#
+
+test_description='ensure rebase fast-forwards commits when possible'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ test_commit A &&
+ test_commit B &&
+ test_commit C &&
+ test_commit D &&
+ git checkout -t -b side
+'
+
+test_rebase_same_head () {
+ status_n="$1" &&
+ shift &&
+ what_n="$1" &&
+ shift &&
+ cmp_n="$1" &&
+ shift &&
+ status_f="$1" &&
+ shift &&
+ what_f="$1" &&
+ shift &&
+ cmp_f="$1" &&
+ shift &&
+ test_rebase_same_head_ $status_n $what_n $cmp_n 0 " --apply" "$*" &&
+ test_rebase_same_head_ $status_f $what_f $cmp_f 0 " --apply --no-ff" "$*"
+ test_rebase_same_head_ $status_n $what_n $cmp_n 0 " --merge" "$*" &&
+ test_rebase_same_head_ $status_f $what_f $cmp_f 0 " --merge --no-ff" "$*"
+ test_rebase_same_head_ $status_n $what_n $cmp_n 1 " --merge" "$*" &&
+ test_rebase_same_head_ $status_f $what_f $cmp_f 1 " --merge --no-ff" "$*"
+}
+
+test_rebase_same_head_ () {
+ status="$1" &&
+ shift &&
+ what="$1" &&
+ shift &&
+ cmp="$1" &&
+ shift &&
+ abbreviate="$1" &&
+ shift &&
+ flag="$1"
+ shift &&
+ if test $abbreviate -eq 1
+ then
+ msg="git rebase$flag $* (rebase.abbreviateCommands = true) with $changes is $what with $cmp HEAD"
+ else
+ msg="git rebase$flag $* with $changes is $what with $cmp HEAD"
+ fi &&
+ test_expect_$status "$msg" "
+ if test $abbreviate -eq 1
+ then
+ test_config rebase.abbreviateCommands true
+ fi &&
+ oldhead=\$(git rev-parse HEAD) &&
+ test_when_finished 'git reset --hard \$oldhead' &&
+ git reflog HEAD >expect &&
+ git rebase$flag $* >stdout &&
+ git reflog HEAD >actual &&
+ if test $what = work
+ then
+ old=\$(wc -l <expect) &&
+ test_line_count '-gt' \$old actual
+ elif test $what = noop
+ then
+ test_cmp expect actual
+ fi &&
+ newhead=\$(git rev-parse HEAD) &&
+ if test $cmp = same
+ then
+ test_cmp_rev \$oldhead \$newhead
+ elif test $cmp = diff
+ then
+ test_cmp_rev ! \$oldhead \$newhead
+ fi
+ "
+}
+
+changes='no changes'
+test_rebase_same_head success noop same success work same
+test_rebase_same_head success noop same success work same main
+test_rebase_same_head success noop same success work diff --onto B B
+test_rebase_same_head success noop same success work diff --onto B... B
+test_rebase_same_head success noop same success work same --onto main... main
+test_rebase_same_head success noop same success work same --keep-base main
+test_rebase_same_head success noop same success work same --keep-base
+test_rebase_same_head success noop same success work same --no-fork-point
+test_rebase_same_head success noop same success work same --keep-base --no-fork-point
+test_rebase_same_head success noop same success work same --fork-point main
+test_rebase_same_head success noop same success work diff --fork-point --onto B B
+test_rebase_same_head success noop same success work diff --fork-point --onto B... B
+test_rebase_same_head success noop same success work same --fork-point --onto main... main
+test_rebase_same_head success noop same success work same --keep-base --keep-base main
+
+test_expect_success 'add work same to side' '
+ test_commit E
+'
+
+changes='our changes'
+test_rebase_same_head success noop same success work same
+test_rebase_same_head success noop same success work same main
+test_rebase_same_head success noop same success work diff --onto B B
+test_rebase_same_head success noop same success work diff --onto B... B
+test_rebase_same_head success noop same success work same --onto main... main
+test_rebase_same_head success noop same success work same --keep-base main
+test_rebase_same_head success noop same success work same --keep-base
+test_rebase_same_head success noop same success work same --no-fork-point
+test_rebase_same_head success noop same success work same --keep-base --no-fork-point
+test_rebase_same_head success noop same success work same --fork-point main
+test_rebase_same_head success noop same success work diff --fork-point --onto B B
+test_rebase_same_head success noop same success work diff --fork-point --onto B... B
+test_rebase_same_head success noop same success work same --fork-point --onto main... main
+test_rebase_same_head success noop same success work same --fork-point --keep-base main
+
+test_expect_success 'add work same to upstream' '
+ git checkout main &&
+ test_commit F &&
+ git checkout side
+'
+
+changes='our and their changes'
+test_rebase_same_head success noop same success work diff --onto B B
+test_rebase_same_head success noop same success work diff --onto B... B
+test_rebase_same_head success noop same success work diff --onto main... main
+test_rebase_same_head success noop same success work diff --keep-base main
+test_rebase_same_head success noop same success work diff --keep-base
+test_rebase_same_head failure work same success work diff --fork-point --onto B B
+test_rebase_same_head failure work same success work diff --fork-point --onto B... B
+test_rebase_same_head success noop same success work diff --fork-point --onto main... main
+test_rebase_same_head success noop same success work diff --fork-point --keep-base main
+
+test_done
diff --git a/t/t3433-rebase-across-mode-change.sh b/t/t3433-rebase-across-mode-change.sh
new file mode 100755
index 0000000..c8172b0
--- /dev/null
+++ b/t/t3433-rebase-across-mode-change.sh
@@ -0,0 +1,49 @@
+#!/bin/sh
+
+test_description='git rebase across mode change'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ mkdir DS &&
+ >DS/whatever &&
+ git add DS &&
+ git commit -m base &&
+
+ git branch side1 &&
+ git branch side2 &&
+
+ git checkout side1 &&
+ git rm -rf DS &&
+ test_ln_s_add unrelated DS &&
+ git commit -m side1 &&
+
+ git checkout side2 &&
+ >unrelated &&
+ git add unrelated &&
+ git commit -m commit1 &&
+
+ echo >>unrelated &&
+ git commit -am commit2
+'
+
+test_expect_success 'rebase changes with the apply backend' '
+ test_when_finished "git rebase --abort || true" &&
+ git checkout -b apply-backend side2 &&
+ git rebase side1
+'
+
+test_expect_success 'rebase changes with the merge backend' '
+ test_when_finished "git rebase --abort || true" &&
+ git checkout -b merge-backend side2 &&
+ git rebase -m side1
+'
+
+test_expect_success 'rebase changes with the merge backend with a delay' '
+ test_when_finished "git rebase --abort || true" &&
+ git checkout -b merge-delay-backend side2 &&
+ git rebase -m --exec "sleep 1" side1
+'
+
+test_done
diff --git a/t/t3434-rebase-i18n.sh b/t/t3434-rebase-i18n.sh
new file mode 100755
index 0000000..e6fef69
--- /dev/null
+++ b/t/t3434-rebase-i18n.sh
@@ -0,0 +1,87 @@
+#!/bin/sh
+#
+# Copyright (c) 2019 Doan Tran Cong Danh
+#
+
+test_description='rebase with changing encoding
+
+Initial setup:
+
+1 - 2 main
+ \
+ 3 - 4 first
+ \
+ 5 - 6 second
+'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+compare_msg () {
+ iconv -f "$2" -t "$3" "$TEST_DIRECTORY/t3434/$1" >expect &&
+ git cat-file commit HEAD >raw &&
+ sed "1,/^$/d" raw >actual &&
+ test_cmp expect actual
+}
+
+test_expect_success setup '
+ test_commit one &&
+ git branch first &&
+ test_commit two &&
+ git switch first &&
+ test_commit three &&
+ git branch second &&
+ test_commit four &&
+ git switch second &&
+ test_commit five &&
+ test_commit six
+'
+
+test_expect_success 'rebase --rebase-merges update encoding eucJP to UTF-8' '
+ git switch -c merge-eucJP-UTF-8 first &&
+ git config i18n.commitencoding eucJP &&
+ git merge -F "$TEST_DIRECTORY/t3434/eucJP.txt" second &&
+ git config i18n.commitencoding UTF-8 &&
+ git rebase --rebase-merges main &&
+ compare_msg eucJP.txt eucJP UTF-8
+'
+
+test_expect_success 'rebase --rebase-merges update encoding eucJP to ISO-2022-JP' '
+ git switch -c merge-eucJP-ISO-2022-JP first &&
+ git config i18n.commitencoding eucJP &&
+ git merge -F "$TEST_DIRECTORY/t3434/eucJP.txt" second &&
+ git config i18n.commitencoding ISO-2022-JP &&
+ git rebase --rebase-merges main &&
+ compare_msg eucJP.txt eucJP ISO-2022-JP
+'
+
+test_rebase_continue_update_encode () {
+ old=$1
+ new=$2
+ msgfile=$3
+ test_expect_success "rebase --continue update from $old to $new" '
+ (git rebase --abort || : abort current git-rebase failure) &&
+ git switch -c conflict-$old-$new one &&
+ echo for-conflict >two.t &&
+ git add two.t &&
+ git config i18n.commitencoding $old &&
+ git commit -F "$TEST_DIRECTORY/t3434/$msgfile" &&
+ git config i18n.commitencoding $new &&
+ test_must_fail git rebase -m main &&
+ test -f .git/rebase-merge/message &&
+ git stripspace <.git/rebase-merge/message >two.t &&
+ git add two.t &&
+ git rebase --continue &&
+ compare_msg $msgfile $old $new &&
+ : git-commit assume invalid utf-8 is latin1 &&
+ test_cmp expect two.t
+ '
+}
+
+test_rebase_continue_update_encode ISO-8859-1 UTF-8 ISO8859-1.txt
+test_rebase_continue_update_encode eucJP UTF-8 eucJP.txt
+test_rebase_continue_update_encode eucJP ISO-2022-JP eucJP.txt
+
+test_done
diff --git a/t/t3434/ISO8859-1.txt b/t/t3434/ISO8859-1.txt
new file mode 100644
index 0000000..7cbef0e
--- /dev/null
+++ b/t/t3434/ISO8859-1.txt
@@ -0,0 +1,3 @@
+ÄËÑÏÖ
+
+Ábçdèfg
diff --git a/t/t3434/eucJP.txt b/t/t3434/eucJP.txt
new file mode 100644
index 0000000..546f2aa
--- /dev/null
+++ b/t/t3434/eucJP.txt
@@ -0,0 +1,4 @@
+¤Ï¤ì¤Ò¤Û¤Õ
+
+¤·¤Æ¤¤¤ë¤Î¤¬¡¢¤¤¤ë¤Î¤Ç¡£
+ßÀÉͤۤì¤×¤ê¤Ý¤ì¤Þ¤Ó¤°¤ê¤í¤Ø¡£
diff --git a/t/t3435-rebase-gpg-sign.sh b/t/t3435-rebase-gpg-sign.sh
new file mode 100755
index 0000000..6aa2aeb
--- /dev/null
+++ b/t/t3435-rebase-gpg-sign.sh
@@ -0,0 +1,123 @@
+#!/bin/sh
+#
+# Copyright (c) 2020 Doan Tran Cong Danh
+#
+
+test_description='test rebase --[no-]gpg-sign'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY/lib-rebase.sh"
+. "$TEST_DIRECTORY/lib-gpg.sh"
+
+if ! test_have_prereq GPG
+then
+ skip_all='skip all test rebase --[no-]gpg-sign, gpg not available'
+ test_done
+fi
+
+test_rebase_gpg_sign () {
+ local must_fail= will=will fake_editor=
+ if test "x$1" = "x!"
+ then
+ must_fail=test_must_fail
+ will="won't"
+ shift
+ fi
+ conf=$1
+ shift
+ test_expect_success "rebase $* with commit.gpgsign=$conf $will sign commit" "
+ git reset two &&
+ git config commit.gpgsign $conf &&
+ set_fake_editor &&
+ FAKE_LINES='r 1 p 2' git rebase --force-rebase --root $* &&
+ $must_fail git verify-commit HEAD^ &&
+ $must_fail git verify-commit HEAD
+ "
+}
+
+test_expect_success 'setup' '
+ test_commit one &&
+ test_commit two &&
+ test_must_fail git verify-commit HEAD &&
+ test_must_fail git verify-commit HEAD^
+'
+
+test_expect_success 'setup: merge commit' '
+ test_commit fork-point &&
+ git switch -c side &&
+ test_commit three &&
+ git switch main &&
+ git merge --no-ff side &&
+ git tag merged
+'
+
+test_rebase_gpg_sign ! false
+test_rebase_gpg_sign true
+test_rebase_gpg_sign ! true --no-gpg-sign
+test_rebase_gpg_sign ! true --gpg-sign --no-gpg-sign
+test_rebase_gpg_sign false --no-gpg-sign --gpg-sign
+test_rebase_gpg_sign true -i
+test_rebase_gpg_sign ! true -i --no-gpg-sign
+test_rebase_gpg_sign ! true -i --gpg-sign --no-gpg-sign
+test_rebase_gpg_sign false -i --no-gpg-sign --gpg-sign
+
+test_expect_success 'rebase -r, merge strategy, --gpg-sign will sign commit' '
+ git reset --hard merged &&
+ test_unconfig commit.gpgsign &&
+ git rebase -fr --gpg-sign -s resolve --root &&
+ git verify-commit HEAD
+'
+
+test_expect_success 'rebase -r, merge strategy, commit.gpgsign=true will sign commit' '
+ git reset --hard merged &&
+ git config commit.gpgsign true &&
+ git rebase -fr -s resolve --root &&
+ git verify-commit HEAD
+'
+
+test_expect_success 'rebase -r, merge strategy, commit.gpgsign=false --gpg-sign will sign commit' '
+ git reset --hard merged &&
+ git config commit.gpgsign false &&
+ git rebase -fr --gpg-sign -s resolve --root &&
+ git verify-commit HEAD
+'
+
+test_expect_success "rebase -r, merge strategy, commit.gpgsign=true --no-gpg-sign won't sign commit" '
+ git reset --hard merged &&
+ git config commit.gpgsign true &&
+ git rebase -fr --no-gpg-sign -s resolve --root &&
+ test_must_fail git verify-commit HEAD
+'
+
+test_expect_success 'rebase -r --gpg-sign will sign commit' '
+ git reset --hard merged &&
+ test_unconfig commit.gpgsign &&
+ git rebase -fr --gpg-sign --root &&
+ git verify-commit HEAD
+'
+
+test_expect_success 'rebase -r with commit.gpgsign=true will sign commit' '
+ git reset --hard merged &&
+ git config commit.gpgsign true &&
+ git rebase -fr --root &&
+ git verify-commit HEAD
+'
+
+test_expect_success 'rebase -r --gpg-sign with commit.gpgsign=false will sign commit' '
+ git reset --hard merged &&
+ git config commit.gpgsign false &&
+ git rebase -fr --gpg-sign --root &&
+ git verify-commit HEAD
+'
+
+test_expect_success "rebase -r --no-gpg-sign with commit.gpgsign=true won't sign commit" '
+ git reset --hard merged &&
+ git config commit.gpgsign true &&
+ git rebase -fr --no-gpg-sign --root &&
+ test_must_fail git verify-commit HEAD
+'
+
+test_done
diff --git a/t/t3436-rebase-more-options.sh b/t/t3436-rebase-more-options.sh
new file mode 100755
index 0000000..94671d3
--- /dev/null
+++ b/t/t3436-rebase-more-options.sh
@@ -0,0 +1,209 @@
+#!/bin/sh
+#
+# Copyright (c) 2019 Rohit Ashiwal
+#
+
+test_description='tests to ensure compatibility between am and interactive backends'
+
+. ./test-lib.sh
+
+. "$TEST_DIRECTORY"/lib-rebase.sh
+
+GIT_AUTHOR_DATE="1999-04-02T08:03:20+05:30"
+export GIT_AUTHOR_DATE
+
+# This is a special case in which both am and interactive backends
+# provide the same output. It was done intentionally because
+# both the backends fall short of optimal behaviour.
+test_expect_success 'setup' '
+ git checkout -b topic &&
+ test_write_lines "line 1" " line 2" "line 3" >file &&
+ git add file &&
+ git commit -m "add file" &&
+
+ test_write_lines "line 1" "new line 2" "line 3" >file &&
+ git commit -am "update file" &&
+ git tag side &&
+ test_commit commit1 foo foo1 &&
+ test_commit commit2 foo foo2 &&
+ test_commit commit3 foo foo3 &&
+
+ git checkout --orphan main &&
+ rm foo &&
+ test_write_lines "line 1" " line 2" "line 3" >file &&
+ git commit -am "add file" &&
+ git tag main &&
+
+ mkdir test-bin &&
+ write_script test-bin/git-merge-test <<-\EOF
+ exec git merge-recursive "$@"
+ EOF
+'
+
+test_expect_success '--ignore-whitespace works with apply backend' '
+ test_must_fail git rebase --apply main side &&
+ git rebase --abort &&
+ git rebase --apply --ignore-whitespace main side &&
+ git diff --exit-code side
+'
+
+test_expect_success '--ignore-whitespace works with merge backend' '
+ test_must_fail git rebase --merge main side &&
+ git rebase --abort &&
+ git rebase --merge --ignore-whitespace main side &&
+ git diff --exit-code side
+'
+
+test_expect_success '--ignore-whitespace is remembered when continuing' '
+ (
+ set_fake_editor &&
+ FAKE_LINES="break 1" git rebase -i --ignore-whitespace \
+ main side &&
+ git rebase --continue
+ ) &&
+ git diff --exit-code side
+'
+
+test_ctime_is_atime () {
+ git log $1 --format="$GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> %ai" >authortime &&
+ git log $1 --format="%cn <%ce> %ci" >committertime &&
+ test_cmp authortime committertime
+}
+
+test_expect_success '--committer-date-is-author-date works with apply backend' '
+ GIT_AUTHOR_DATE="@1234 +0300" git commit --amend --reset-author &&
+ git rebase --apply --committer-date-is-author-date HEAD^ &&
+ test_ctime_is_atime -1
+'
+
+test_expect_success '--committer-date-is-author-date works with merge backend' '
+ GIT_AUTHOR_DATE="@1234 +0300" git commit --amend --reset-author &&
+ git rebase -m --committer-date-is-author-date HEAD^ &&
+ test_ctime_is_atime -1
+'
+
+test_expect_success '--committer-date-is-author-date works when rewording' '
+ GIT_AUTHOR_DATE="@1234 +0300" git commit --amend --reset-author &&
+ (
+ set_fake_editor &&
+ FAKE_COMMIT_MESSAGE=edited \
+ FAKE_LINES="reword 1" \
+ git rebase -i --committer-date-is-author-date HEAD^
+ ) &&
+ test_write_lines edited "" >expect &&
+ git log --format="%B" -1 >actual &&
+ test_cmp expect actual &&
+ test_ctime_is_atime -1
+'
+
+test_expect_success '--committer-date-is-author-date works with rebase -r' '
+ git checkout side &&
+ GIT_AUTHOR_DATE="@1234 +0300" git merge --no-ff commit3 &&
+ git rebase -r --root --committer-date-is-author-date &&
+ test_ctime_is_atime
+'
+
+test_expect_success '--committer-date-is-author-date works when forking merge' '
+ git checkout side &&
+ GIT_AUTHOR_DATE="@1234 +0300" git merge --no-ff commit3 &&
+ PATH="./test-bin:$PATH" git rebase -r --root --strategy=test \
+ --committer-date-is-author-date &&
+ test_ctime_is_atime
+'
+
+test_expect_success '--committer-date-is-author-date works when committing conflict resolution' '
+ git checkout commit2 &&
+ GIT_AUTHOR_DATE="@1980 +0000" git commit --amend --only --reset-author &&
+ test_must_fail git rebase -m --committer-date-is-author-date \
+ --onto HEAD^^ HEAD^ &&
+ echo resolved > foo &&
+ git add foo &&
+ git rebase --continue &&
+ test_ctime_is_atime -1
+'
+
+# Checking for +0000 in the author date is sufficient since the
+# default timezone is UTC but the timezone used while committing is
+# +0530. The inverted logic in the grep is necessary to check all the
+# author dates in the file.
+test_atime_is_ignored () {
+ git log $1 --format=%ai >authortime &&
+ ! grep -v +0000 authortime
+}
+
+test_expect_success '--reset-author-date works with apply backend' '
+ git commit --amend --date="$GIT_AUTHOR_DATE" &&
+ git rebase --apply --reset-author-date HEAD^ &&
+ test_atime_is_ignored -1
+'
+
+test_expect_success '--reset-author-date works with merge backend' '
+ git commit --amend --date="$GIT_AUTHOR_DATE" &&
+ git rebase --reset-author-date -m HEAD^ &&
+ test_atime_is_ignored -1
+'
+
+test_expect_success '--reset-author-date works after conflict resolution' '
+ test_must_fail git rebase --reset-author-date -m \
+ --onto commit2^^ commit2^ commit2 &&
+ echo resolved >foo &&
+ git add foo &&
+ git rebase --continue &&
+ test_atime_is_ignored -1
+'
+
+test_expect_success '--reset-author-date works with rebase -r' '
+ git checkout side &&
+ git merge --no-ff commit3 &&
+ git rebase -r --root --reset-author-date &&
+ test_atime_is_ignored
+'
+
+test_expect_success '--reset-author-date with --committer-date-is-author-date works' '
+ test_must_fail git rebase -m --committer-date-is-author-date \
+ --reset-author-date --onto commit2^^ commit2^ commit3 &&
+ git checkout --theirs foo &&
+ git add foo &&
+ git rebase --continue &&
+ test_ctime_is_atime -2 &&
+ test_atime_is_ignored -2
+'
+
+test_expect_success 'reset-author-date with --committer-date-is-author-date works when rewording' '
+ GIT_AUTHOR_DATE="@1234 +0300" git commit --amend --reset-author &&
+ (
+ set_fake_editor &&
+ FAKE_COMMIT_MESSAGE=edited \
+ FAKE_LINES="reword 1" \
+ git rebase -i --committer-date-is-author-date \
+ --reset-author-date HEAD^
+ ) &&
+ test_write_lines edited "" >expect &&
+ git log --format="%B" -1 >actual &&
+ test_cmp expect actual &&
+ test_atime_is_ignored -1
+'
+
+test_expect_success '--reset-author-date --committer-date-is-author-date works when forking merge' '
+ GIT_SEQUENCE_EDITOR="echo \"merge -C $(git rev-parse HEAD) commit3\">" \
+ PATH="./test-bin:$PATH" git rebase -i --strategy=test \
+ --reset-author-date \
+ --committer-date-is-author-date side side &&
+ test_ctime_is_atime -1 &&
+ test_atime_is_ignored -1
+ '
+
+test_expect_success '--ignore-date is an alias for --reset-author-date' '
+ git commit --amend --date="$GIT_AUTHOR_DATE" &&
+ git rebase --apply --ignore-date HEAD^ &&
+ git commit --allow-empty -m empty --date="$GIT_AUTHOR_DATE" &&
+ git rebase -m --ignore-date HEAD^ &&
+ test_atime_is_ignored -2
+'
+
+# This must be the last test in this file
+test_expect_success '$EDITOR and friends are unchanged' '
+ test_editor_unchanged
+'
+
+test_done
diff --git a/t/t3437-rebase-fixup-options.sh b/t/t3437-rebase-fixup-options.sh
new file mode 100755
index 0000000..7929e2e
--- /dev/null
+++ b/t/t3437-rebase-fixup-options.sh
@@ -0,0 +1,223 @@
+#!/bin/sh
+#
+# Copyright (c) 2018 Phillip Wood
+#
+
+test_description='git rebase interactive fixup options
+
+This test checks the "fixup [-C|-c]" command of rebase interactive.
+In addition to amending the contents of the commit, "fixup -C"
+replaces the original commit message with the message of the fixup
+commit. "fixup -c" also replaces the original message, but opens the
+editor to allow the user to edit the message before committing. Similar
+to the "fixup" command that works with "fixup!", "fixup -C" works with
+"amend!" upon --autosquash.
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+. "$TEST_DIRECTORY"/lib-rebase.sh
+
+EMPTY=""
+
+get_author () {
+ rev="$1" &&
+ git log -1 --pretty=format:"%an %ae %at" "$rev"
+}
+
+test_expect_success 'setup' '
+ cat >message <<-EOF &&
+ amend! B
+ $EMPTY
+ new subject
+ $EMPTY
+ new
+ body
+ EOF
+
+ test_commit initial &&
+ test_commit A A &&
+ test_commit B B &&
+ get_author HEAD >expected-author &&
+ ORIG_AUTHOR_NAME="$GIT_AUTHOR_NAME" &&
+ ORIG_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" &&
+ GIT_AUTHOR_NAME="Amend Author" &&
+ GIT_AUTHOR_EMAIL="amend@example.com" &&
+ test_commit "$(cat message)" A A1 A1 &&
+ test_commit A2 A &&
+ test_commit A3 A &&
+ GIT_AUTHOR_NAME="$ORIG_AUTHOR_NAME" &&
+ GIT_AUTHOR_EMAIL="$ORIG_AUTHOR_EMAIL" &&
+ git checkout -b conflicts-branch A &&
+ test_commit conflicts A &&
+
+ set_fake_editor &&
+ git checkout -b branch B &&
+ echo B1 >B &&
+ test_tick &&
+ git commit --fixup=HEAD -a &&
+ git tag B1 &&
+ test_tick &&
+ FAKE_COMMIT_AMEND="edited 1" git commit --fixup=reword:B &&
+ test_tick &&
+ FAKE_COMMIT_AMEND="edited 2" git commit --fixup=reword:HEAD &&
+ echo B2 >B &&
+ test_tick &&
+ FAKE_COMMIT_AMEND="edited squash" git commit --squash=HEAD -a &&
+ git tag B2 &&
+ echo B3 >B &&
+ test_tick &&
+ FAKE_COMMIT_AMEND="edited 3" git commit -a --fixup=amend:HEAD^ &&
+ git tag B3 &&
+
+ GIT_AUTHOR_NAME="Rebase Author" &&
+ GIT_AUTHOR_EMAIL="rebase.author@example.com" &&
+ GIT_COMMITTER_NAME="Rebase Committer" &&
+ GIT_COMMITTER_EMAIL="rebase.committer@example.com"
+'
+
+test_expect_success 'simple fixup -C works' '
+ test_when_finished "test_might_fail git rebase --abort" &&
+ git checkout --detach A2 &&
+ FAKE_LINES="1 fixup_-C 2" git rebase -i B &&
+ test_cmp_rev HEAD^ B &&
+ test_cmp_rev HEAD^{tree} A2^{tree} &&
+ test_commit_message HEAD -m "A2"
+'
+
+test_expect_success 'simple fixup -c works' '
+ test_when_finished "test_might_fail git rebase --abort" &&
+ git checkout --detach A2 &&
+ git log -1 --pretty=format:%B >expected-fixup-message &&
+ test_write_lines "" "Modified A2" >>expected-fixup-message &&
+ FAKE_LINES="1 fixup_-c 2" \
+ FAKE_COMMIT_AMEND="Modified A2" \
+ git rebase -i B &&
+ test_cmp_rev HEAD^ B &&
+ test_cmp_rev HEAD^{tree} A2^{tree} &&
+ test_commit_message HEAD expected-fixup-message
+'
+
+test_expect_success 'fixup -C removes amend! from message' '
+ test_when_finished "test_might_fail git rebase --abort" &&
+ git checkout --detach A1 &&
+ git log -1 --pretty=format:%b >expected-message &&
+ FAKE_LINES="1 fixup_-C 2" git rebase -i A &&
+ test_cmp_rev HEAD^ A &&
+ test_cmp_rev HEAD^{tree} A1^{tree} &&
+ test_commit_message HEAD expected-message &&
+ get_author HEAD >actual-author &&
+ test_cmp expected-author actual-author
+'
+
+test_expect_success 'fixup -C with conflicts gives correct message' '
+ test_when_finished "test_might_fail git rebase --abort" &&
+ git checkout --detach A1 &&
+ git log -1 --pretty=format:%b >expected-message &&
+ test_write_lines "" "edited" >>expected-message &&
+ test_must_fail env FAKE_LINES="1 fixup_-C 2" git rebase -i conflicts &&
+ git checkout --theirs -- A &&
+ git add A &&
+ FAKE_COMMIT_AMEND=edited git rebase --continue &&
+ test_cmp_rev HEAD^ conflicts &&
+ test_cmp_rev HEAD^{tree} A1^{tree} &&
+ test_commit_message HEAD expected-message &&
+ get_author HEAD >actual-author &&
+ test_cmp expected-author actual-author
+'
+
+test_expect_success 'skipping fixup -C after fixup gives correct message' '
+ test_when_finished "test_might_fail git rebase --abort" &&
+ git checkout --detach A3 &&
+ test_must_fail env FAKE_LINES="1 fixup 2 fixup_-C 4" git rebase -i A &&
+ git reset --hard &&
+ FAKE_COMMIT_AMEND=edited git rebase --continue &&
+ test_commit_message HEAD -m "B"
+'
+
+test_expect_success 'sequence of fixup, fixup -C & squash --signoff works' '
+ git checkout --detach B3 &&
+ FAKE_LINES="1 fixup 2 fixup_-C 3 fixup_-C 4 squash 5 fixup_-C 6" \
+ FAKE_COMMIT_AMEND=squashed \
+ FAKE_MESSAGE_COPY=actual-squash-message \
+ git -c commit.status=false rebase -ik --signoff A &&
+ git diff-tree --exit-code --patch HEAD B3 -- &&
+ test_cmp_rev HEAD^ A &&
+ test_cmp "$TEST_DIRECTORY/t3437/expected-squash-message" \
+ actual-squash-message
+'
+
+test_expect_success 'first fixup -C commented out in sequence fixup fixup -C fixup -C' '
+ test_when_finished "test_might_fail git rebase --abort" &&
+ git checkout --detach B2~ &&
+ git log -1 --pretty=format:%b >expected-message &&
+ FAKE_LINES="1 fixup 2 fixup_-C 3 fixup_-C 4" git rebase -i A &&
+ test_cmp_rev HEAD^ A &&
+ test_commit_message HEAD expected-message
+'
+
+test_expect_success 'multiple fixup -c opens editor once' '
+ test_when_finished "test_might_fail git rebase --abort" &&
+ git checkout --detach A3 &&
+ git log -1 --pretty=format:%B >expected-message &&
+ test_write_lines "" "Modified-A3" >>expected-message &&
+ FAKE_COMMIT_AMEND="Modified-A3" \
+ FAKE_LINES="1 fixup_-C 2 fixup_-c 3 fixup_-c 4" \
+ EXPECT_HEADER_COUNT=4 \
+ git rebase -i A &&
+ test_cmp_rev HEAD^ A &&
+ get_author HEAD >actual-author &&
+ test_cmp expected-author actual-author &&
+ test_commit_message HEAD expected-message
+'
+
+test_expect_success 'sequence squash, fixup & fixup -c gives combined message' '
+ test_when_finished "test_might_fail git rebase --abort" &&
+ git checkout --detach A3 &&
+ FAKE_LINES="1 squash 2 fixup 3 fixup_-c 4" \
+ FAKE_MESSAGE_COPY=actual-combined-message \
+ git -c commit.status=false rebase -i A &&
+ test_cmp "$TEST_DIRECTORY/t3437/expected-combined-message" \
+ actual-combined-message &&
+ test_cmp_rev HEAD^ A
+'
+
+test_expect_success 'fixup -C works upon --autosquash with amend!' '
+ git checkout --detach B3 &&
+ FAKE_COMMIT_AMEND=squashed \
+ FAKE_MESSAGE_COPY=actual-squash-message \
+ git -c commit.status=false rebase -ik --autosquash \
+ --signoff A &&
+ git diff-tree --exit-code --patch HEAD B3 -- &&
+ test_cmp_rev HEAD^ A &&
+ test_cmp "$TEST_DIRECTORY/t3437/expected-squash-message" \
+ actual-squash-message
+'
+
+test_expect_success 'fixup -[Cc]<commit> works' '
+ test_when_finished "test_might_fail git rebase --abort" &&
+ cat >todo <<-\EOF &&
+ pick A
+ fixup -CA1
+ pick B
+ fixup -cA2
+ EOF
+ (
+ set_replace_editor todo &&
+ FAKE_COMMIT_MESSAGE="edited and fixed up" \
+ git rebase -i initial initial
+ ) &&
+ git log --pretty=format:%B initial.. >actual &&
+ cat >expect <<-EOF &&
+ edited and fixed up
+ $EMPTY
+ new subject
+ $EMPTY
+ new
+ body
+ EOF
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t3437/expected-combined-message b/t/t3437/expected-combined-message
new file mode 100644
index 0000000..a26cfb2
--- /dev/null
+++ b/t/t3437/expected-combined-message
@@ -0,0 +1,21 @@
+# This is a combination of 4 commits.
+# This is the 1st commit message:
+
+B
+
+# This is the commit message #2:
+
+# amend! B
+
+new subject
+
+new
+body
+
+# The commit message #3 will be skipped:
+
+# A2
+
+# This is the commit message #4:
+
+A3
diff --git a/t/t3437/expected-squash-message b/t/t3437/expected-squash-message
new file mode 100644
index 0000000..ab2434f
--- /dev/null
+++ b/t/t3437/expected-squash-message
@@ -0,0 +1,51 @@
+# This is a combination of 6 commits.
+# The 1st commit message will be skipped:
+
+# B
+#
+# Signed-off-by: Rebase Committer <rebase.committer@example.com>
+
+# The commit message #2 will be skipped:
+
+# fixup! B
+
+# The commit message #3 will be skipped:
+
+# amend! B
+#
+# B
+#
+# edited 1
+#
+# Signed-off-by: Rebase Committer <rebase.committer@example.com>
+
+# This is the commit message #4:
+
+# amend! amend! B
+
+B
+
+edited 1
+
+edited 2
+
+Signed-off-by: Rebase Committer <rebase.committer@example.com>
+
+# This is the commit message #5:
+
+# squash! amend! amend! B
+
+edited squash
+
+# This is the commit message #6:
+
+# amend! amend! amend! B
+
+B
+
+edited 1
+
+edited 2
+
+edited 3
+squashed
diff --git a/t/t3438-rebase-broken-files.sh b/t/t3438-rebase-broken-files.sh
new file mode 100755
index 0000000..c614c4f
--- /dev/null
+++ b/t/t3438-rebase-broken-files.sh
@@ -0,0 +1,61 @@
+#!/bin/sh
+
+test_description='rebase behavior when on-disk files are broken'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'set up conflicting branches' '
+ test_commit base file &&
+ git checkout -b branch1 &&
+ test_commit one file &&
+ git checkout -b branch2 HEAD^ &&
+ test_commit two file
+'
+
+create_conflict () {
+ test_when_finished "git rebase --abort" &&
+ git checkout -B tmp branch2 &&
+ test_must_fail git rebase branch1
+}
+
+check_resolve_fails () {
+ echo resolved >file &&
+ git add file &&
+ test_must_fail git rebase --continue
+}
+
+for item in NAME EMAIL DATE
+do
+ test_expect_success "detect missing GIT_AUTHOR_$item" '
+ create_conflict &&
+
+ grep -v $item .git/rebase-merge/author-script >tmp &&
+ mv tmp .git/rebase-merge/author-script &&
+
+ check_resolve_fails
+ '
+done
+
+for item in NAME EMAIL DATE
+do
+ test_expect_success "detect duplicate GIT_AUTHOR_$item" '
+ create_conflict &&
+
+ grep -i $item .git/rebase-merge/author-script >tmp &&
+ cat tmp >>.git/rebase-merge/author-script &&
+
+ check_resolve_fails
+ '
+done
+
+test_expect_success 'unknown key in author-script' '
+ create_conflict &&
+
+ echo "GIT_AUTHOR_BOGUS=${SQ}whatever${SQ}" \
+ >>.git/rebase-merge/author-script &&
+
+ check_resolve_fails
+'
+
+test_done
diff --git a/t/t3500-cherry.sh b/t/t3500-cherry.sh
new file mode 100755
index 0000000..78c3eac
--- /dev/null
+++ b/t/t3500-cherry.sh
@@ -0,0 +1,81 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Yann Dirson, based on t3400 by Amos Waterland
+#
+
+test_description='git cherry should detect patches integrated upstream
+
+This test cherry-picks one local change of two into main branch, and
+checks that git cherry only returns the second patch in the local branch
+'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+GIT_AUTHOR_EMAIL=bogus_email_address
+export GIT_AUTHOR_EMAIL
+
+test_expect_success 'prepare repository with topic branch, and check cherry finds the 2 patches from there' '
+ echo First > A &&
+ git update-index --add A &&
+ test_tick &&
+ git commit -m "Add A." &&
+
+ git checkout -b my-topic-branch &&
+
+ echo Second > B &&
+ git update-index --add B &&
+ test_tick &&
+ git commit -m "Add B." &&
+
+ echo AnotherSecond > C &&
+ git update-index --add C &&
+ test_tick &&
+ git commit -m "Add C." &&
+
+ git checkout -f main &&
+ rm -f B C &&
+
+ echo Third >> A &&
+ git update-index A &&
+ test_tick &&
+ git commit -m "Modify A." &&
+
+ expr "$(echo $(git cherry main my-topic-branch) )" : "+ [^ ]* + .*"
+'
+
+test_expect_success 'check that cherry with limit returns only the top patch' '
+ expr "$(echo $(git cherry main my-topic-branch my-topic-branch^1) )" : "+ [^ ]*"
+'
+
+test_expect_success 'cherry-pick one of the 2 patches, and check cherry recognized one and only one as new' '
+ git cherry-pick my-topic-branch^0 &&
+ echo $(git cherry main my-topic-branch) &&
+ expr "$(echo $(git cherry main my-topic-branch) )" : "+ [^ ]* - .*"
+'
+
+test_expect_success 'cherry ignores whitespace' '
+ git switch --orphan=upstream-with-space &&
+ test_commit initial file &&
+ >expect &&
+ git switch --create=feature-without-space &&
+
+ # A spaceless file on the feature branch. Expect a match upstream.
+ printf space >file &&
+ git add file &&
+ git commit -m"file without space" &&
+ git log --format="- %H" -1 >>expect &&
+
+ # A further change. Should not match upstream.
+ test_commit change file &&
+ git log --format="+ %H" -1 >>expect &&
+
+ git switch upstream-with-space &&
+ # Same as the spaceless file, just with spaces and on upstream.
+ test_commit "file with space" file "s p a c e" file-with-space &&
+ git cherry upstream-with-space feature-without-space >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t3501-revert-cherry-pick.sh b/t/t3501-revert-cherry-pick.sh
new file mode 100755
index 0000000..aeab689
--- /dev/null
+++ b/t/t3501-revert-cherry-pick.sh
@@ -0,0 +1,237 @@
+#!/bin/sh
+
+test_description='miscellaneous basic tests for cherry-pick and revert'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+
+ for l in a b c d e f g h i j k l m n o
+ do
+ echo $l$l$l$l$l$l$l$l$l || return 1
+ done >oops &&
+
+ test_tick &&
+ git add oops &&
+ git commit -m initial &&
+ git tag initial &&
+
+ test_tick &&
+ echo "Add extra line at the end" >>oops &&
+ git commit -a -m added &&
+ git tag added &&
+
+ test_tick &&
+ git mv oops spoo &&
+ git commit -m rename1 &&
+ git tag rename1 &&
+
+ test_tick &&
+ git checkout -b side initial &&
+ git mv oops opos &&
+ git commit -m rename2 &&
+ git tag rename2
+'
+
+test_expect_success 'cherry-pick --nonsense' '
+
+ pos=$(git rev-parse HEAD) &&
+ git diff --exit-code HEAD &&
+ test_must_fail git cherry-pick --nonsense 2>msg &&
+ git diff --exit-code HEAD "$pos" &&
+ test_grep "[Uu]sage:" msg
+'
+
+test_expect_success 'revert --nonsense' '
+
+ pos=$(git rev-parse HEAD) &&
+ git diff --exit-code HEAD &&
+ test_must_fail git revert --nonsense 2>msg &&
+ git diff --exit-code HEAD "$pos" &&
+ test_grep "[Uu]sage:" msg
+'
+
+# the following two test cherry-pick and revert with renames
+#
+# --
+# + rename2: renames oops to opos
+# + rename1: renames oops to spoo
+# + added: adds extra line to oops
+# ++ initial: has lines in oops
+
+test_expect_success 'cherry-pick after renaming branch' '
+
+ git checkout rename2 &&
+ git cherry-pick added &&
+ test_cmp_rev rename2 HEAD^ &&
+ grep "Add extra line at the end" opos &&
+ git reflog -1 | grep cherry-pick
+
+'
+
+test_expect_success 'revert after renaming branch' '
+
+ git checkout rename1 &&
+ git revert added &&
+ test_cmp_rev rename1 HEAD^ &&
+ test_path_is_file spoo &&
+ test_cmp_rev initial:oops HEAD:spoo &&
+ git reflog -1 | grep revert
+
+'
+
+test_expect_success 'cherry-pick on stat-dirty working tree' '
+ git clone . copy &&
+ (
+ cd copy &&
+ git checkout initial &&
+ test-tool chmtime +40 oops &&
+ git cherry-pick added
+ )
+'
+
+test_expect_success 'revert forbidden on dirty working tree' '
+
+ echo content >extra_file &&
+ git add extra_file &&
+ test_must_fail git revert HEAD 2>errors &&
+ test_grep "your local changes would be overwritten by " errors
+
+'
+
+test_expect_success 'cherry-pick on unborn branch' '
+ git checkout --orphan unborn &&
+ git rm --cached -r . &&
+ rm -rf * &&
+ git cherry-pick initial &&
+ git diff --quiet initial &&
+ test_cmp_rev ! initial HEAD
+'
+
+test_expect_success 'cherry-pick "-" to pick from previous branch' '
+ git checkout unborn &&
+ test_commit to-pick actual content &&
+ git checkout main &&
+ git cherry-pick - &&
+ echo content >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'cherry-pick "-" is meaningless without checkout' '
+ test_create_repo afresh &&
+ (
+ cd afresh &&
+ test_commit one &&
+ test_commit two &&
+ test_commit three &&
+ test_must_fail git cherry-pick -
+ )
+'
+
+test_expect_success 'cherry-pick "-" works with arguments' '
+ git checkout -b side-branch &&
+ test_commit change actual change &&
+ git checkout main &&
+ git cherry-pick -s - &&
+ echo "Signed-off-by: C O Mitter <committer@example.com>" >expect &&
+ git cat-file commit HEAD | grep ^Signed-off-by: >signoff &&
+ test_cmp expect signoff &&
+ echo change >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'cherry-pick works with dirty renamed file' '
+ test_commit to-rename &&
+ git checkout -b unrelated &&
+ test_commit unrelated &&
+ git checkout @{-1} &&
+ git mv to-rename.t renamed &&
+ test_tick &&
+ git commit -m renamed &&
+ echo modified >renamed &&
+ git cherry-pick refs/heads/unrelated &&
+ test $(git rev-parse :0:renamed) = $(git rev-parse HEAD~2:to-rename.t) &&
+ grep -q "^modified$" renamed
+'
+
+test_expect_success 'advice from failed revert' '
+ test_when_finished "git reset --hard" &&
+ test_commit --no-tag "add dream" dream dream &&
+ dream_oid=$(git rev-parse --short HEAD) &&
+ cat <<-EOF >expected &&
+ error: could not revert $dream_oid... add dream
+ hint: After resolving the conflicts, mark them with
+ hint: "git add/rm <pathspec>", then run
+ hint: "git revert --continue".
+ hint: You can instead skip this commit with "git revert --skip".
+ hint: To abort and get back to the state before "git revert",
+ hint: run "git revert --abort".
+ EOF
+ test_commit --append --no-tag "double-add dream" dream dream &&
+ test_must_fail git revert HEAD^ 2>actual &&
+ test_cmp expected actual
+'
+
+test_expect_subject () {
+ echo "$1" >expect &&
+ git log -1 --pretty=%s >actual &&
+ test_cmp expect actual
+}
+
+test_expect_success 'titles of fresh reverts' '
+ test_commit --no-tag A file1 &&
+ test_commit --no-tag B file1 &&
+ git revert --no-edit HEAD &&
+ test_expect_subject "Revert \"B\"" &&
+ git revert --no-edit HEAD &&
+ test_expect_subject "Reapply \"B\"" &&
+ git revert --no-edit HEAD &&
+ test_expect_subject "Revert \"Reapply \"B\"\""
+'
+
+test_expect_success 'title of legacy double revert' '
+ test_commit --no-tag "Revert \"Revert \"B\"\"" file1 &&
+ git revert --no-edit HEAD &&
+ test_expect_subject "Revert \"Revert \"Revert \"B\"\"\""
+'
+
+test_expect_success 'identification of reverted commit (default)' '
+ test_commit to-ident &&
+ test_when_finished "git reset --hard to-ident" &&
+ git checkout --detach to-ident &&
+ git revert --no-edit HEAD &&
+ git cat-file commit HEAD >actual.raw &&
+ grep "^This reverts " actual.raw >actual &&
+ echo "This reverts commit $(git rev-parse HEAD^)." >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'identification of reverted commit (--reference)' '
+ git checkout --detach to-ident &&
+ git revert --reference --no-edit HEAD &&
+ git cat-file commit HEAD >actual.raw &&
+ grep "^This reverts " actual.raw >actual &&
+ echo "This reverts commit $(git show -s --pretty=reference HEAD^)." >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'identification of reverted commit (revert.reference)' '
+ git checkout --detach to-ident &&
+ git -c revert.reference=true revert --no-edit HEAD &&
+ git cat-file commit HEAD >actual.raw &&
+ grep "^This reverts " actual.raw >actual &&
+ echo "This reverts commit $(git show -s --pretty=reference HEAD^)." >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'cherry-pick is unaware of --reference (for now)' '
+ test_when_finished "git reset --hard" &&
+ test_must_fail git cherry-pick --reference HEAD 2>actual &&
+ grep "^usage: git cherry-pick" actual
+'
+
+test_done
diff --git a/t/t3502-cherry-pick-merge.sh b/t/t3502-cherry-pick-merge.sh
new file mode 100755
index 0000000..1b2c0d6
--- /dev/null
+++ b/t/t3502-cherry-pick-merge.sh
@@ -0,0 +1,136 @@
+#!/bin/sh
+
+test_description='cherry picking and reverting a merge
+
+ b---c
+ / /
+ initial---a
+
+'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+
+ >A &&
+ >B &&
+ git add A B &&
+ git commit -m "Initial" &&
+ git tag initial &&
+ git branch side &&
+ echo new line >A &&
+ git commit -m "add line to A" A &&
+ git tag a &&
+ git checkout side &&
+ echo new line >B &&
+ git commit -m "add line to B" B &&
+ git tag b &&
+ git checkout main &&
+ git merge side &&
+ git tag c
+
+'
+
+test_expect_success 'cherry-pick -m complains of bogus numbers' '
+ # expect 129 here to distinguish between cases where
+ # there was nothing to cherry-pick
+ test_expect_code 129 git cherry-pick -m &&
+ test_expect_code 129 git cherry-pick -m foo b &&
+ test_expect_code 129 git cherry-pick -m -1 b &&
+ test_expect_code 129 git cherry-pick -m 0 b
+'
+
+test_expect_success 'cherry-pick explicit first parent of a non-merge' '
+
+ git reset --hard &&
+ git checkout a^0 &&
+ git cherry-pick -m 1 b &&
+ git diff --exit-code c --
+
+'
+
+test_expect_success 'cherry pick a merge without -m should fail' '
+
+ git reset --hard &&
+ git checkout a^0 &&
+ test_must_fail git cherry-pick c &&
+ git diff --exit-code a --
+
+'
+
+test_expect_success 'cherry pick a merge (1)' '
+
+ git reset --hard &&
+ git checkout a^0 &&
+ git cherry-pick -m 1 c &&
+ git diff --exit-code c
+
+'
+
+test_expect_success 'cherry pick a merge (2)' '
+
+ git reset --hard &&
+ git checkout b^0 &&
+ git cherry-pick -m 2 c &&
+ git diff --exit-code c
+
+'
+
+test_expect_success 'cherry pick a merge relative to nonexistent parent should fail' '
+
+ git reset --hard &&
+ git checkout b^0 &&
+ test_must_fail git cherry-pick -m 3 c
+
+'
+
+test_expect_success 'revert explicit first parent of a non-merge' '
+
+ git reset --hard &&
+ git checkout c^0 &&
+ git revert -m 1 b &&
+ git diff --exit-code a --
+
+'
+
+test_expect_success 'revert a merge without -m should fail' '
+
+ git reset --hard &&
+ git checkout c^0 &&
+ test_must_fail git revert c &&
+ git diff --exit-code c
+
+'
+
+test_expect_success 'revert a merge (1)' '
+
+ git reset --hard &&
+ git checkout c^0 &&
+ git revert -m 1 c &&
+ git diff --exit-code a --
+
+'
+
+test_expect_success 'revert a merge (2)' '
+
+ git reset --hard &&
+ git checkout c^0 &&
+ git revert -m 2 c &&
+ git diff --exit-code b --
+
+'
+
+test_expect_success 'revert a merge relative to nonexistent parent should fail' '
+
+ git reset --hard &&
+ git checkout c^0 &&
+ test_must_fail git revert -m 3 c &&
+ git diff --exit-code c
+
+'
+
+test_done
diff --git a/t/t3503-cherry-pick-root.sh b/t/t3503-cherry-pick-root.sh
new file mode 100755
index 0000000..76d393d
--- /dev/null
+++ b/t/t3503-cherry-pick-root.sh
@@ -0,0 +1,82 @@
+#!/bin/sh
+
+test_description='test cherry-picking (and reverting) a root commit'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+
+ echo first > file1 &&
+ git add file1 &&
+ test_tick &&
+ git commit -m "first" &&
+
+ git symbolic-ref HEAD refs/heads/second &&
+ rm .git/index file1 &&
+ echo second > file2 &&
+ git add file2 &&
+ test_tick &&
+ git commit -m "second" &&
+
+ git symbolic-ref HEAD refs/heads/third &&
+ rm .git/index file2 &&
+ echo third > file3 &&
+ git add file3 &&
+ test_tick &&
+ git commit -m "third"
+
+'
+
+test_expect_success 'cherry-pick a root commit' '
+
+ git checkout second^0 &&
+ git cherry-pick main &&
+ echo first >expect &&
+ test_cmp expect file1
+
+'
+
+test_expect_success 'revert a root commit' '
+
+ git revert main &&
+ test_path_is_missing file1
+
+'
+
+test_expect_success 'cherry-pick a root commit with an external strategy' '
+
+ git cherry-pick --strategy=resolve main &&
+ echo first >expect &&
+ test_cmp expect file1
+
+'
+
+test_expect_success 'revert a root commit with an external strategy' '
+
+ git revert --strategy=resolve main &&
+ test_path_is_missing file1
+
+'
+
+test_expect_success 'cherry-pick two root commits' '
+
+ echo first >expect.file1 &&
+ echo second >expect.file2 &&
+ echo third >expect.file3 &&
+
+ git checkout second^0 &&
+ git cherry-pick main third &&
+
+ test_cmp expect.file1 file1 &&
+ test_cmp expect.file2 file2 &&
+ test_cmp expect.file3 file3 &&
+ git rev-parse --verify HEAD^^ &&
+ test_must_fail git rev-parse --verify HEAD^^^
+
+'
+
+test_done
diff --git a/t/t3504-cherry-pick-rerere.sh b/t/t3504-cherry-pick-rerere.sh
new file mode 100755
index 0000000..4581ae9
--- /dev/null
+++ b/t/t3504-cherry-pick-rerere.sh
@@ -0,0 +1,106 @@
+#!/bin/sh
+
+test_description='cherry-pick should rerere for conflicts'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success setup '
+ test_commit foo &&
+ test_commit foo-main foo &&
+ test_commit bar-main bar &&
+
+ git checkout -b dev foo &&
+ test_commit foo-dev foo &&
+ test_commit bar-dev bar &&
+ git config rerere.enabled true
+'
+
+test_expect_success 'conflicting merge' '
+ test_must_fail git merge main
+'
+
+test_expect_success 'fixup' '
+ echo foo-resolved >foo &&
+ echo bar-resolved >bar &&
+ git commit -am resolved &&
+ cp foo foo-expect &&
+ cp bar bar-expect &&
+ git reset --hard HEAD^
+'
+
+test_expect_success 'cherry-pick conflict with --rerere-autoupdate' '
+ test_must_fail git cherry-pick --rerere-autoupdate foo..bar-main &&
+ test_cmp foo-expect foo &&
+ git diff-files --quiet &&
+ test_must_fail git cherry-pick --continue &&
+ test_cmp bar-expect bar &&
+ git diff-files --quiet &&
+ git cherry-pick --continue &&
+ git reset --hard bar-dev
+'
+
+test_expect_success 'cherry-pick conflict repsects rerere.autoUpdate' '
+ test_config rerere.autoUpdate true &&
+ test_must_fail git cherry-pick foo..bar-main &&
+ test_cmp foo-expect foo &&
+ git diff-files --quiet &&
+ test_must_fail git cherry-pick --continue &&
+ test_cmp bar-expect bar &&
+ git diff-files --quiet &&
+ git cherry-pick --continue &&
+ git reset --hard bar-dev
+'
+
+test_expect_success 'cherry-pick conflict with --no-rerere-autoupdate' '
+ test_config rerere.autoUpdate true &&
+ test_must_fail git cherry-pick --no-rerere-autoupdate foo..bar-main &&
+ test_cmp foo-expect foo &&
+ test_must_fail git diff-files --quiet &&
+ git add foo &&
+ test_must_fail git cherry-pick --continue &&
+ test_cmp bar-expect bar &&
+ test_must_fail git diff-files --quiet &&
+ git add bar &&
+ git cherry-pick --continue &&
+ git reset --hard bar-dev
+'
+
+test_expect_success 'cherry-pick --continue rejects --rerere-autoupdate' '
+ test_must_fail git cherry-pick --rerere-autoupdate foo..bar-main &&
+ test_cmp foo-expect foo &&
+ git diff-files --quiet &&
+ test_must_fail git cherry-pick --continue --rerere-autoupdate >actual 2>&1 &&
+ echo "fatal: cherry-pick: --rerere-autoupdate cannot be used with --continue" >expect &&
+ test_cmp expect actual &&
+ test_must_fail git cherry-pick --continue --no-rerere-autoupdate >actual 2>&1 &&
+ echo "fatal: cherry-pick: --no-rerere-autoupdate cannot be used with --continue" >expect &&
+ test_cmp expect actual &&
+ git cherry-pick --abort
+'
+
+test_expect_success 'cherry-pick --rerere-autoupdate more than once' '
+ test_must_fail git cherry-pick --rerere-autoupdate --rerere-autoupdate foo..bar-main &&
+ test_cmp foo-expect foo &&
+ git diff-files --quiet &&
+ git cherry-pick --abort &&
+ test_must_fail git cherry-pick --rerere-autoupdate --no-rerere-autoupdate --rerere-autoupdate foo..bar-main &&
+ test_cmp foo-expect foo &&
+ git diff-files --quiet &&
+ git cherry-pick --abort &&
+ test_must_fail git cherry-pick --rerere-autoupdate --no-rerere-autoupdate foo..bar-main &&
+ test_must_fail git diff-files --quiet &&
+ git cherry-pick --abort
+'
+
+test_expect_success 'cherry-pick conflict without rerere' '
+ test_config rerere.enabled false &&
+ test_must_fail git cherry-pick foo-main &&
+ grep ===== foo &&
+ grep foo-dev foo &&
+ grep foo-main foo
+'
+
+test_done
diff --git a/t/t3505-cherry-pick-empty.sh b/t/t3505-cherry-pick-empty.sh
new file mode 100755
index 0000000..eba3c38
--- /dev/null
+++ b/t/t3505-cherry-pick-empty.sh
@@ -0,0 +1,102 @@
+#!/bin/sh
+
+test_description='test cherry-picking an empty commit'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success setup '
+
+ echo first > file1 &&
+ git add file1 &&
+ test_tick &&
+ git commit -m "first" &&
+
+ git checkout -b empty-message-branch &&
+ echo third >> file1 &&
+ git add file1 &&
+ test_tick &&
+ git commit --allow-empty-message -m "" &&
+
+ git checkout main &&
+ git checkout -b empty-change-branch &&
+ test_tick &&
+ git commit --allow-empty -m "empty"
+
+'
+
+test_expect_success 'cherry-pick an empty commit' '
+ git checkout main &&
+ test_expect_code 1 git cherry-pick empty-change-branch
+'
+
+test_expect_success 'index lockfile was removed' '
+ test ! -f .git/index.lock
+'
+
+test_expect_success 'cherry-pick a commit with an empty message' '
+ test_when_finished "git reset --hard empty-message-branch~1" &&
+ git checkout main &&
+ git cherry-pick empty-message-branch
+'
+
+test_expect_success 'index lockfile was removed' '
+ test ! -f .git/index.lock
+'
+
+test_expect_success 'cherry-pick a commit with an empty message with --allow-empty-message' '
+ git checkout -f main &&
+ git cherry-pick --allow-empty-message empty-message-branch
+'
+
+test_expect_success 'cherry pick an empty non-ff commit without --allow-empty' '
+ git checkout main &&
+ echo fourth >>file2 &&
+ git add file2 &&
+ git commit -m "fourth" &&
+ test_must_fail git cherry-pick empty-change-branch
+'
+
+test_expect_success 'cherry pick an empty non-ff commit with --allow-empty' '
+ git checkout main &&
+ git cherry-pick --allow-empty empty-change-branch
+'
+
+test_expect_success 'cherry pick with --keep-redundant-commits' '
+ git checkout main &&
+ git cherry-pick --keep-redundant-commits HEAD^
+'
+
+test_expect_success 'cherry-pick a commit that becomes no-op (prep)' '
+ git checkout main &&
+ git branch fork &&
+ echo foo >file2 &&
+ git add file2 &&
+ test_tick &&
+ git commit -m "add file2 on main" &&
+
+ git checkout fork &&
+ echo foo >file2 &&
+ git add file2 &&
+ test_tick &&
+ git commit -m "add file2 on the side"
+'
+
+test_expect_success 'cherry-pick a no-op without --keep-redundant' '
+ git reset --hard &&
+ git checkout fork^0 &&
+ test_must_fail git cherry-pick main
+'
+
+test_expect_success 'cherry-pick a no-op with --keep-redundant' '
+ git reset --hard &&
+ git checkout fork^0 &&
+ git cherry-pick --keep-redundant-commits main &&
+ git show -s --format=%s >actual &&
+ echo "add file2 on main" >expect &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t3506-cherry-pick-ff.sh b/t/t3506-cherry-pick-ff.sh
new file mode 100755
index 0000000..b71bad1
--- /dev/null
+++ b/t/t3506-cherry-pick-ff.sh
@@ -0,0 +1,124 @@
+#!/bin/sh
+
+test_description='test cherry-picking with --ff option'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ echo first > file1 &&
+ git add file1 &&
+ test_tick &&
+ git commit -m "first" &&
+ git tag first &&
+
+ git checkout -b other &&
+ echo second >> file1 &&
+ git add file1 &&
+ test_tick &&
+ git commit -m "second" &&
+ git tag second &&
+ test_oid_cache <<-EOF
+ cp_ff sha1:1df192cd8bc58a2b275d842cede4d221ad9000d1
+ cp_ff sha256:e70d6b7fc064bddb516b8d512c9057094b96ce6ff08e12080acc4fe7f1d60a1d
+ EOF
+'
+
+test_expect_success 'cherry-pick using --ff fast forwards' '
+ git checkout main &&
+ git reset --hard first &&
+ test_tick &&
+ git cherry-pick --ff second &&
+ test "$(git rev-parse --verify HEAD)" = "$(git rev-parse --verify second)"
+'
+
+test_expect_success 'cherry-pick not using --ff does not fast forwards' '
+ git checkout main &&
+ git reset --hard first &&
+ test_tick &&
+ git cherry-pick second &&
+ test "$(git rev-parse --verify HEAD)" != "$(git rev-parse --verify second)"
+'
+
+#
+# We setup the following graph:
+#
+# B---C
+# / /
+# first---A
+#
+# (This has been taken from t3502-cherry-pick-merge.sh)
+#
+test_expect_success 'merge setup' '
+ git checkout main &&
+ git reset --hard first &&
+ echo new line >A &&
+ git add A &&
+ test_tick &&
+ git commit -m "add line to A" A &&
+ git tag A &&
+ git checkout -b side first &&
+ echo new line >B &&
+ git add B &&
+ test_tick &&
+ git commit -m "add line to B" B &&
+ git tag B &&
+ git checkout main &&
+ git merge side &&
+ git tag C &&
+ git checkout -b new A
+'
+
+test_expect_success 'cherry-pick explicit first parent of a non-merge with --ff' '
+ git reset --hard A -- &&
+ git cherry-pick --ff -m 1 B &&
+ git diff --exit-code C --
+'
+
+test_expect_success 'cherry pick a merge with --ff but without -m should fail' '
+ git reset --hard A -- &&
+ test_must_fail git cherry-pick --ff C &&
+ git diff --exit-code A --
+'
+
+test_expect_success 'cherry pick with --ff a merge (1)' '
+ git reset --hard A -- &&
+ git cherry-pick --ff -m 1 C &&
+ git diff --exit-code C &&
+ test "$(git rev-parse --verify HEAD)" = "$(git rev-parse --verify C)"
+'
+
+test_expect_success 'cherry pick with --ff a merge (2)' '
+ git reset --hard B -- &&
+ git cherry-pick --ff -m 2 C &&
+ git diff --exit-code C &&
+ test "$(git rev-parse --verify HEAD)" = "$(git rev-parse --verify C)"
+'
+
+test_expect_success 'cherry pick a merge relative to nonexistent parent with --ff should fail' '
+ git reset --hard B -- &&
+ test_must_fail git cherry-pick --ff -m 3 C
+'
+
+test_expect_success 'cherry pick a root commit with --ff' '
+ git reset --hard first -- &&
+ git rm file1 &&
+ echo first >file2 &&
+ git add file2 &&
+ git commit --amend -m "file2" &&
+ git cherry-pick --ff first &&
+ test "$(git rev-parse --verify HEAD)" = "$(test_oid cp_ff)"
+'
+
+test_expect_success 'cherry-pick --ff on unborn branch' '
+ git checkout --orphan unborn &&
+ git rm --cached -r . &&
+ rm -rf * &&
+ git cherry-pick --ff first &&
+ test_cmp_rev first HEAD
+'
+
+test_done
diff --git a/t/t3507-cherry-pick-conflict.sh b/t/t3507-cherry-pick-conflict.sh
new file mode 100755
index 0000000..c88d597
--- /dev/null
+++ b/t/t3507-cherry-pick-conflict.sh
@@ -0,0 +1,588 @@
+#!/bin/sh
+
+test_description='test cherry-pick and revert with conflicts
+
+ -
+ + picked: rewrites foo to c
+ + base: rewrites foo to b
+ + initial: writes foo as a, unrelated as unrelated
+
+'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_CREATE_REPO_NO_TEMPLATE=1
+. ./test-lib.sh
+
+pristine_detach () {
+ git checkout -f "$1^0" &&
+ git read-tree -u --reset HEAD &&
+ git clean -d -f -f -q -x
+}
+
+test_expect_success setup '
+
+ echo unrelated >unrelated &&
+ git add unrelated &&
+ test_commit initial foo a &&
+ test_commit base foo b &&
+ test_commit picked foo c &&
+ test_commit --signoff picked-signed foo d &&
+ git checkout -b topic initial &&
+ test_commit redundant-pick foo c redundant &&
+ git commit --allow-empty --allow-empty-message &&
+ git tag empty &&
+ git checkout main &&
+ git config advice.detachedhead false
+
+'
+
+test_expect_success 'failed cherry-pick does not advance HEAD' '
+ pristine_detach initial &&
+
+ head=$(git rev-parse HEAD) &&
+ test_must_fail git cherry-pick picked &&
+ newhead=$(git rev-parse HEAD) &&
+
+ test "$head" = "$newhead"
+'
+
+test_expect_success 'advice from failed cherry-pick' '
+ pristine_detach initial &&
+
+ picked=$(git rev-parse --short picked) &&
+ cat <<-EOF >expected &&
+ error: could not apply $picked... picked
+ hint: After resolving the conflicts, mark them with
+ hint: "git add/rm <pathspec>", then run
+ hint: "git cherry-pick --continue".
+ hint: You can instead skip this commit with "git cherry-pick --skip".
+ hint: To abort and get back to the state before "git cherry-pick",
+ hint: run "git cherry-pick --abort".
+ EOF
+ test_must_fail git cherry-pick picked 2>actual &&
+
+ test_cmp expected actual
+'
+
+test_expect_success 'advice from failed cherry-pick --no-commit' "
+ pristine_detach initial &&
+
+ picked=\$(git rev-parse --short picked) &&
+ cat <<-EOF >expected &&
+ error: could not apply \$picked... picked
+ hint: after resolving the conflicts, mark the corrected paths
+ hint: with 'git add <paths>' or 'git rm <paths>'
+ EOF
+ test_must_fail git cherry-pick --no-commit picked 2>actual &&
+
+ test_cmp expected actual
+"
+
+test_expect_success 'failed cherry-pick sets CHERRY_PICK_HEAD' '
+ pristine_detach initial &&
+ test_must_fail git cherry-pick picked &&
+ test_cmp_rev picked CHERRY_PICK_HEAD
+'
+
+test_expect_success 'successful cherry-pick does not set CHERRY_PICK_HEAD' '
+ pristine_detach initial &&
+ git cherry-pick base &&
+ test_must_fail git rev-parse --verify CHERRY_PICK_HEAD
+'
+
+test_expect_success 'cherry-pick --no-commit does not set CHERRY_PICK_HEAD' '
+ pristine_detach initial &&
+ git cherry-pick --no-commit base &&
+ test_must_fail git rev-parse --verify CHERRY_PICK_HEAD
+'
+
+test_expect_success 'cherry-pick w/dirty tree does not set CHERRY_PICK_HEAD' '
+ pristine_detach initial &&
+ echo foo >foo &&
+ test_must_fail git cherry-pick base &&
+ test_must_fail git rev-parse --verify CHERRY_PICK_HEAD
+'
+
+test_expect_success \
+ 'cherry-pick --strategy=resolve w/dirty tree does not set CHERRY_PICK_HEAD' '
+ pristine_detach initial &&
+ echo foo >foo &&
+ test_must_fail git cherry-pick --strategy=resolve base &&
+ test_must_fail git rev-parse --verify CHERRY_PICK_HEAD
+'
+
+test_expect_success 'GIT_CHERRY_PICK_HELP suppresses CHERRY_PICK_HEAD' '
+ pristine_detach initial &&
+ (
+ GIT_CHERRY_PICK_HELP="and then do something else" &&
+ export GIT_CHERRY_PICK_HELP &&
+ test_must_fail git cherry-pick picked
+ ) &&
+ test_must_fail git rev-parse --verify CHERRY_PICK_HEAD
+'
+
+test_expect_success 'git reset clears CHERRY_PICK_HEAD' '
+ pristine_detach initial &&
+
+ test_must_fail git cherry-pick picked &&
+ git reset &&
+
+ test_must_fail git rev-parse --verify CHERRY_PICK_HEAD
+'
+
+test_expect_success 'failed commit does not clear CHERRY_PICK_HEAD' '
+ pristine_detach initial &&
+
+ test_must_fail git cherry-pick picked &&
+ test_must_fail git commit &&
+
+ test_cmp_rev picked CHERRY_PICK_HEAD
+'
+
+test_expect_success 'cancelled commit does not clear CHERRY_PICK_HEAD' '
+ pristine_detach initial &&
+
+ test_must_fail git cherry-pick picked &&
+ echo resolved >foo &&
+ git add foo &&
+ git update-index --refresh -q &&
+ test_must_fail git diff-index --exit-code HEAD &&
+ (
+ GIT_EDITOR=false &&
+ export GIT_EDITOR &&
+ test_must_fail git commit
+ ) &&
+
+ test_cmp_rev picked CHERRY_PICK_HEAD
+'
+
+test_expect_success 'successful commit clears CHERRY_PICK_HEAD' '
+ pristine_detach initial &&
+
+ test_must_fail git cherry-pick picked &&
+ echo resolved >foo &&
+ git add foo &&
+ git commit &&
+
+ test_must_fail git rev-parse --verify CHERRY_PICK_HEAD
+'
+
+test_expect_success 'partial commit of cherry-pick fails' '
+ pristine_detach initial &&
+
+ test_must_fail git cherry-pick picked &&
+ echo resolved >foo &&
+ git add foo &&
+ test_must_fail git commit foo 2>err &&
+
+ test_grep "cannot do a partial commit during a cherry-pick." err
+'
+
+test_expect_success 'commit --amend of cherry-pick fails' '
+ pristine_detach initial &&
+
+ test_must_fail git cherry-pick picked &&
+ echo resolved >foo &&
+ git add foo &&
+ test_must_fail git commit --amend 2>err &&
+
+ test_grep "in the middle of a cherry-pick -- cannot amend." err
+'
+
+test_expect_success 'successful final commit clears cherry-pick state' '
+ pristine_detach initial &&
+
+ test_must_fail git cherry-pick base picked-signed &&
+ echo resolved >foo &&
+ test_path_is_file .git/sequencer/todo &&
+ git commit -a &&
+ test_path_is_missing .git/sequencer
+'
+
+test_expect_success 'reset after final pick clears cherry-pick state' '
+ pristine_detach initial &&
+
+ test_must_fail git cherry-pick base picked-signed &&
+ echo resolved >foo &&
+ test_path_is_file .git/sequencer/todo &&
+ git reset &&
+ test_path_is_missing .git/sequencer
+'
+
+test_expect_success 'failed cherry-pick produces dirty index' '
+ pristine_detach initial &&
+
+ test_must_fail git cherry-pick picked &&
+
+ test_must_fail git update-index --refresh -q &&
+ test_must_fail git diff-index --exit-code HEAD
+'
+
+test_expect_success 'failed cherry-pick registers participants in index' '
+ pristine_detach initial &&
+ {
+ git checkout base -- foo &&
+ git ls-files --stage foo &&
+ git checkout initial -- foo &&
+ git ls-files --stage foo &&
+ git checkout picked -- foo &&
+ git ls-files --stage foo
+ } >stages &&
+ sed "
+ 1 s/ 0 / 1 /
+ 2 s/ 0 / 2 /
+ 3 s/ 0 / 3 /
+ " stages >expected &&
+ git read-tree -u --reset HEAD &&
+
+ test_must_fail git cherry-pick picked &&
+ git ls-files --stage --unmerged >actual &&
+
+ test_cmp expected actual
+'
+
+test_expect_success \
+ 'cherry-pick conflict, ensure commit.cleanup = scissors places scissors line properly' '
+ pristine_detach initial &&
+ git config commit.cleanup scissors &&
+ cat <<-EOF >expected &&
+ picked
+
+ # ------------------------ >8 ------------------------
+ # Do not modify or remove the line above.
+ # Everything below it will be ignored.
+ #
+ # Conflicts:
+ # foo
+ EOF
+
+ test_must_fail git cherry-pick picked &&
+
+ test_cmp expected .git/MERGE_MSG
+'
+
+test_expect_success \
+ 'cherry-pick conflict, ensure cleanup=scissors places scissors line properly' '
+ pristine_detach initial &&
+ git config --unset commit.cleanup &&
+ cat <<-EOF >expected &&
+ picked
+
+ # ------------------------ >8 ------------------------
+ # Do not modify or remove the line above.
+ # Everything below it will be ignored.
+ #
+ # Conflicts:
+ # foo
+ EOF
+
+ test_must_fail git cherry-pick --cleanup=scissors picked &&
+
+ test_cmp expected .git/MERGE_MSG
+'
+
+test_expect_success 'failed cherry-pick describes conflict in work tree' '
+ pristine_detach initial &&
+ cat <<-EOF >expected &&
+ <<<<<<< HEAD
+ a
+ =======
+ c
+ >>>>>>> objid (picked)
+ EOF
+
+ test_must_fail git cherry-pick picked &&
+
+ sed "s/[a-f0-9]* (/objid (/" foo >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'diff3 -m style' '
+ pristine_detach initial &&
+ git config merge.conflictstyle diff3 &&
+ cat <<-EOF >expected &&
+ <<<<<<< HEAD
+ a
+ ||||||| parent of objid (picked)
+ b
+ =======
+ c
+ >>>>>>> objid (picked)
+ EOF
+
+ test_must_fail git cherry-pick picked &&
+
+ sed "s/[a-f0-9]* (/objid (/" foo >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'revert also handles conflicts sanely' '
+ git config --unset merge.conflictstyle &&
+ pristine_detach initial &&
+ cat <<-EOF >expected &&
+ <<<<<<< HEAD
+ a
+ =======
+ b
+ >>>>>>> parent of objid (picked)
+ EOF
+ {
+ git checkout picked -- foo &&
+ git ls-files --stage foo &&
+ git checkout initial -- foo &&
+ git ls-files --stage foo &&
+ git checkout base -- foo &&
+ git ls-files --stage foo
+ } >stages &&
+ sed "
+ 1 s/ 0 / 1 /
+ 2 s/ 0 / 2 /
+ 3 s/ 0 / 3 /
+ " stages >expected-stages &&
+ git read-tree -u --reset HEAD &&
+
+ head=$(git rev-parse HEAD) &&
+ test_must_fail git revert picked &&
+ newhead=$(git rev-parse HEAD) &&
+ git ls-files --stage --unmerged >actual-stages &&
+
+ test "$head" = "$newhead" &&
+ test_must_fail git update-index --refresh -q &&
+ test_must_fail git diff-index --exit-code HEAD &&
+ test_cmp expected-stages actual-stages &&
+ sed "s/[a-f0-9]* (/objid (/" foo >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'failed revert sets REVERT_HEAD' '
+ pristine_detach initial &&
+ test_must_fail git revert picked &&
+ test_cmp_rev picked REVERT_HEAD
+'
+
+test_expect_success 'successful revert does not set REVERT_HEAD' '
+ pristine_detach base &&
+ git revert base &&
+ test_must_fail git rev-parse --verify CHERRY_PICK_HEAD &&
+ test_must_fail git rev-parse --verify REVERT_HEAD
+'
+
+test_expect_success 'revert --no-commit sets REVERT_HEAD' '
+ pristine_detach base &&
+ git revert --no-commit base &&
+ test_must_fail git rev-parse --verify CHERRY_PICK_HEAD &&
+ test_cmp_rev base REVERT_HEAD
+'
+
+test_expect_success 'revert w/dirty tree does not set REVERT_HEAD' '
+ pristine_detach base &&
+ echo foo >foo &&
+ test_must_fail git revert base &&
+ test_must_fail git rev-parse --verify CHERRY_PICK_HEAD &&
+ test_must_fail git rev-parse --verify REVERT_HEAD
+'
+
+test_expect_success 'GIT_CHERRY_PICK_HELP does not suppress REVERT_HEAD' '
+ pristine_detach initial &&
+ (
+ GIT_CHERRY_PICK_HELP="and then do something else" &&
+ GIT_REVERT_HELP="and then do something else, again" &&
+ export GIT_CHERRY_PICK_HELP GIT_REVERT_HELP &&
+ test_must_fail git revert picked
+ ) &&
+ test_must_fail git rev-parse --verify CHERRY_PICK_HEAD &&
+ test_cmp_rev picked REVERT_HEAD
+'
+
+test_expect_success 'git reset clears REVERT_HEAD' '
+ pristine_detach initial &&
+ test_must_fail git revert picked &&
+ git reset &&
+ test_must_fail git rev-parse --verify REVERT_HEAD
+'
+
+test_expect_success 'failed commit does not clear REVERT_HEAD' '
+ pristine_detach initial &&
+ test_must_fail git revert picked &&
+ test_must_fail git commit &&
+ test_cmp_rev picked REVERT_HEAD
+'
+
+test_expect_success 'successful final commit clears revert state' '
+ pristine_detach picked-signed &&
+
+ test_must_fail git revert picked-signed base &&
+ echo resolved >foo &&
+ test_path_is_file .git/sequencer/todo &&
+ git commit -a &&
+ test_path_is_missing .git/sequencer
+'
+
+test_expect_success 'reset after final pick clears revert state' '
+ pristine_detach picked-signed &&
+
+ test_must_fail git revert picked-signed base &&
+ echo resolved >foo &&
+ test_path_is_file .git/sequencer/todo &&
+ git reset &&
+ test_path_is_missing .git/sequencer
+'
+
+test_expect_success 'revert conflict, diff3 -m style' '
+ pristine_detach initial &&
+ git config merge.conflictstyle diff3 &&
+ cat <<-EOF >expected &&
+ <<<<<<< HEAD
+ a
+ ||||||| objid (picked)
+ c
+ =======
+ b
+ >>>>>>> parent of objid (picked)
+ EOF
+
+ test_must_fail git revert picked &&
+
+ sed "s/[a-f0-9]* (/objid (/" foo >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success \
+ 'revert conflict, ensure commit.cleanup = scissors places scissors line properly' '
+ pristine_detach initial &&
+ git config commit.cleanup scissors &&
+ cat >expected <<-EOF &&
+ Revert "picked"
+
+ This reverts commit OBJID.
+
+ # ------------------------ >8 ------------------------
+ # Do not modify or remove the line above.
+ # Everything below it will be ignored.
+ #
+ # Conflicts:
+ # foo
+ EOF
+
+ test_must_fail git revert picked &&
+
+ sed "s/$OID_REGEX/OBJID/" .git/MERGE_MSG >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success \
+ 'revert conflict, ensure cleanup=scissors places scissors line properly' '
+ pristine_detach initial &&
+ git config --unset commit.cleanup &&
+ cat >expected <<-EOF &&
+ Revert "picked"
+
+ This reverts commit OBJID.
+
+ # ------------------------ >8 ------------------------
+ # Do not modify or remove the line above.
+ # Everything below it will be ignored.
+ #
+ # Conflicts:
+ # foo
+ EOF
+
+ test_must_fail git revert --cleanup=scissors picked &&
+
+ sed "s/$OID_REGEX/OBJID/" .git/MERGE_MSG >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'failed cherry-pick does not forget -s' '
+ pristine_detach initial &&
+ test_must_fail git cherry-pick -s picked &&
+ test_grep -e "Signed-off-by" .git/MERGE_MSG
+'
+
+test_expect_success 'commit after failed cherry-pick does not add duplicated -s' '
+ pristine_detach initial &&
+ test_must_fail git cherry-pick -s picked-signed &&
+ git commit -a -s &&
+ test $(git show -s >tmp && grep -c "Signed-off-by" tmp && rm tmp) = 1
+'
+
+test_expect_success 'commit after failed cherry-pick adds -s at the right place' '
+ pristine_detach initial &&
+ test_must_fail git cherry-pick picked &&
+
+ git commit -a -s &&
+
+ # Do S-o-b and Conflicts appear in the right order?
+ cat <<-\EOF >expect &&
+ Signed-off-by: C O Mitter <committer@example.com>
+ # Conflicts:
+ EOF
+ grep -e "^# Conflicts:" -e "^Signed-off-by" .git/COMMIT_EDITMSG >actual &&
+ test_cmp expect actual &&
+
+ cat <<-\EOF >expected &&
+ picked
+
+ Signed-off-by: C O Mitter <committer@example.com>
+ EOF
+
+ git show -s --pretty=format:%B >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'commit --amend -s places the sign-off at the right place' '
+ pristine_detach initial &&
+ test_must_fail git cherry-pick picked &&
+
+ # emulate old-style conflicts block
+ mv .git/MERGE_MSG .git/MERGE_MSG+ &&
+ sed -e "/^# Conflicts:/,\$s/^# *//" .git/MERGE_MSG+ >.git/MERGE_MSG &&
+
+ git commit -a &&
+ git commit --amend -s &&
+
+ # Do S-o-b and Conflicts appear in the right order?
+ cat <<-\EOF >expect &&
+ Signed-off-by: C O Mitter <committer@example.com>
+ Conflicts:
+ EOF
+ grep -e "^Conflicts:" -e "^Signed-off-by" .git/COMMIT_EDITMSG >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'cherry-pick preserves sparse-checkout' '
+ pristine_detach initial &&
+ test_config core.sparseCheckout true &&
+ test_when_finished "
+ echo \"/*\" >.git/info/sparse-checkout
+ git read-tree --reset -u HEAD
+ rm .git/info/sparse-checkout" &&
+ mkdir .git/info &&
+ echo /unrelated >.git/info/sparse-checkout &&
+ git read-tree --reset -u HEAD &&
+ test_must_fail git cherry-pick -Xours picked>actual &&
+ test_grep ! "Changes not staged for commit:" actual
+'
+
+test_expect_success 'cherry-pick --continue remembers --keep-redundant-commits' '
+ test_when_finished "git cherry-pick --abort || :" &&
+ pristine_detach initial &&
+ test_must_fail git cherry-pick --keep-redundant-commits picked redundant &&
+ echo c >foo &&
+ git add foo &&
+ git cherry-pick --continue
+'
+
+test_expect_success 'cherry-pick --continue remembers --allow-empty and --allow-empty-message' '
+ test_when_finished "git cherry-pick --abort || :" &&
+ pristine_detach initial &&
+ test_must_fail git cherry-pick --allow-empty --allow-empty-message \
+ picked empty &&
+ echo c >foo &&
+ git add foo &&
+ git cherry-pick --continue
+'
+
+test_done
diff --git a/t/t3508-cherry-pick-many-commits.sh b/t/t3508-cherry-pick-many-commits.sh
new file mode 100755
index 0000000..2d53ce7
--- /dev/null
+++ b/t/t3508-cherry-pick-many-commits.sh
@@ -0,0 +1,199 @@
+#!/bin/sh
+
+test_description='test cherry-picking many commits'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+check_head_differs_from() {
+ test_cmp_rev ! HEAD "$1"
+}
+
+check_head_equals() {
+ test_cmp_rev HEAD "$1"
+}
+
+test_expect_success setup '
+ echo first > file1 &&
+ git add file1 &&
+ test_tick &&
+ git commit -m "first" &&
+ git tag first &&
+
+ git checkout -b other &&
+ for val in second third fourth
+ do
+ echo $val >> file1 &&
+ git add file1 &&
+ test_tick &&
+ git commit -m "$val" &&
+ git tag $val || return 1
+ done
+'
+
+test_expect_success 'cherry-pick first..fourth works' '
+ git checkout -f main &&
+ git reset --hard first &&
+ test_tick &&
+ git cherry-pick first..fourth &&
+ git diff --quiet other &&
+ git diff --quiet HEAD other &&
+ check_head_differs_from fourth
+'
+
+test_expect_success 'cherry-pick three one two works' '
+ git checkout -f first &&
+ test_commit one &&
+ test_commit two &&
+ test_commit three &&
+ git checkout -f main &&
+ git reset --hard first &&
+ git cherry-pick three one two &&
+ git diff --quiet three &&
+ git diff --quiet HEAD three &&
+ test "$(git log --reverse --format=%s first..)" = "three
+one
+two"
+'
+
+test_expect_success 'cherry-pick three one two: fails' '
+ git checkout -f main &&
+ git reset --hard first &&
+ test_must_fail git cherry-pick three one two:
+'
+
+test_expect_success 'output to keep user entertained during multi-pick' '
+ cat <<-\EOF >expected &&
+ [main OBJID] second
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:14:13 2005 -0700
+ 1 file changed, 1 insertion(+)
+ [main OBJID] third
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:15:13 2005 -0700
+ 1 file changed, 1 insertion(+)
+ [main OBJID] fourth
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:16:13 2005 -0700
+ 1 file changed, 1 insertion(+)
+ EOF
+
+ git checkout -f main &&
+ git reset --hard first &&
+ test_tick &&
+ git cherry-pick first..fourth >actual &&
+ sed -e "s/$_x05[0-9a-f][0-9a-f]/OBJID/" <actual >actual.fuzzy &&
+ test_line_count -ge 3 actual.fuzzy &&
+ test_cmp expected actual.fuzzy
+'
+
+test_expect_success 'cherry-pick --strategy resolve first..fourth works' '
+ git checkout -f main &&
+ git reset --hard first &&
+ test_tick &&
+ git cherry-pick --strategy resolve first..fourth &&
+ git diff --quiet other &&
+ git diff --quiet HEAD other &&
+ check_head_differs_from fourth
+'
+
+test_expect_success 'output during multi-pick indicates merge strategy' '
+ cat <<-\EOF >expected &&
+ Trying simple merge.
+ [main OBJID] second
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:14:13 2005 -0700
+ 1 file changed, 1 insertion(+)
+ Trying simple merge.
+ [main OBJID] third
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:15:13 2005 -0700
+ 1 file changed, 1 insertion(+)
+ Trying simple merge.
+ [main OBJID] fourth
+ Author: A U Thor <author@example.com>
+ Date: Thu Apr 7 15:16:13 2005 -0700
+ 1 file changed, 1 insertion(+)
+ EOF
+
+ git checkout -f main &&
+ git reset --hard first &&
+ test_tick &&
+ git cherry-pick --strategy resolve first..fourth >actual &&
+ sed -e "s/$_x05[0-9a-f][0-9a-f]/OBJID/" <actual >actual.fuzzy &&
+ test_cmp expected actual.fuzzy
+'
+
+test_expect_success 'cherry-pick --ff first..fourth works' '
+ git checkout -f main &&
+ git reset --hard first &&
+ test_tick &&
+ git cherry-pick --ff first..fourth &&
+ git diff --quiet other &&
+ git diff --quiet HEAD other &&
+ check_head_equals fourth
+'
+
+test_expect_success 'cherry-pick -n first..fourth works' '
+ git checkout -f main &&
+ git reset --hard first &&
+ test_tick &&
+ git cherry-pick -n first..fourth &&
+ git diff --quiet other &&
+ git diff --cached --quiet other &&
+ git diff --quiet HEAD first
+'
+
+test_expect_success 'revert first..fourth works' '
+ git checkout -f main &&
+ git reset --hard fourth &&
+ test_tick &&
+ git revert first..fourth &&
+ git diff --quiet first &&
+ git diff --cached --quiet first &&
+ git diff --quiet HEAD first
+'
+
+test_expect_success 'revert ^first fourth works' '
+ git checkout -f main &&
+ git reset --hard fourth &&
+ test_tick &&
+ git revert ^first fourth &&
+ git diff --quiet first &&
+ git diff --cached --quiet first &&
+ git diff --quiet HEAD first
+'
+
+test_expect_success 'revert fourth fourth~1 fourth~2 works' '
+ git checkout -f main &&
+ git reset --hard fourth &&
+ test_tick &&
+ git revert fourth fourth~1 fourth~2 &&
+ git diff --quiet first &&
+ git diff --cached --quiet first &&
+ git diff --quiet HEAD first
+'
+
+test_expect_success 'cherry-pick -3 fourth works' '
+ git checkout -f main &&
+ git reset --hard first &&
+ test_tick &&
+ git cherry-pick -3 fourth &&
+ git diff --quiet other &&
+ git diff --quiet HEAD other &&
+ check_head_differs_from fourth
+'
+
+test_expect_success 'cherry-pick --stdin works' '
+ git checkout -f main &&
+ git reset --hard first &&
+ test_tick &&
+ git rev-list --reverse first..fourth | git cherry-pick --stdin &&
+ git diff --quiet other &&
+ git diff --quiet HEAD other &&
+ check_head_differs_from fourth
+'
+
+test_done
diff --git a/t/t3509-cherry-pick-merge-df.sh b/t/t3509-cherry-pick-merge-df.sh
new file mode 100755
index 0000000..f415924
--- /dev/null
+++ b/t/t3509-cherry-pick-merge-df.sh
@@ -0,0 +1,104 @@
+#!/bin/sh
+
+test_description='Test cherry-pick with directory/file conflicts'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success 'Initialize repository' '
+ mkdir a &&
+ >a/f &&
+ git add a &&
+ git commit -m a
+'
+
+test_expect_success 'Setup rename across paths each below D/F conflicts' '
+ mkdir b &&
+ test_ln_s_add ../a b/a &&
+ git commit -m b &&
+
+ git checkout -b branch &&
+ rm b/a &&
+ git mv a b/a &&
+ test_ln_s_add b/a a &&
+ git commit -m swap &&
+
+ >f1 &&
+ git add f1 &&
+ git commit -m f1
+'
+
+test_expect_success 'Cherry-pick succeeds with rename across D/F conflicts' '
+ git reset --hard &&
+ git checkout main^0 &&
+ git cherry-pick branch
+'
+
+test_expect_success 'Setup rename with file on one side matching directory name on other' '
+ git checkout --orphan nick-testcase &&
+ git rm -rf . &&
+
+ >empty &&
+ git add empty &&
+ git commit -m "Empty file" &&
+
+ git checkout -b simple &&
+ mv empty file &&
+ mkdir empty &&
+ mv file empty &&
+ git add empty/file &&
+ git commit -m "Empty file under empty dir" &&
+
+ echo content >newfile &&
+ git add newfile &&
+ git commit -m "New file"
+'
+
+test_expect_success 'Cherry-pick succeeds with was_a_dir/file -> was_a_dir (resolve)' '
+ git reset --hard &&
+ git checkout -q nick-testcase^0 &&
+ git cherry-pick --strategy=resolve simple
+'
+
+test_expect_success 'Cherry-pick succeeds with was_a_dir/file -> was_a_dir (recursive)' '
+ git reset --hard &&
+ git checkout -q nick-testcase^0 &&
+ git cherry-pick --strategy=recursive simple
+'
+
+test_expect_success 'Setup rename with file on one side matching different dirname on other' '
+ git reset --hard &&
+ git checkout --orphan mergeme &&
+ git rm -rf . &&
+
+ mkdir sub &&
+ mkdir othersub &&
+ echo content > sub/file &&
+ echo foo > othersub/whatever &&
+ git add -A &&
+ git commit -m "Common commit" &&
+
+ git rm -rf othersub &&
+ git mv sub/file othersub &&
+ git commit -m "Commit to merge" &&
+
+ git checkout -b newhead mergeme~1 &&
+ >independent-change &&
+ git add independent-change &&
+ git commit -m "Completely unrelated change"
+'
+
+test_expect_success 'Cherry-pick with rename to different D/F conflict succeeds (resolve)' '
+ git reset --hard &&
+ git checkout -q newhead^0 &&
+ git cherry-pick --strategy=resolve mergeme
+'
+
+test_expect_success 'Cherry-pick with rename to different D/F conflict succeeds (recursive)' '
+ git reset --hard &&
+ git checkout -q newhead^0 &&
+ git cherry-pick --strategy=recursive mergeme
+'
+
+test_done
diff --git a/t/t3510-cherry-pick-sequence.sh b/t/t3510-cherry-pick-sequence.sh
new file mode 100755
index 0000000..72020a5
--- /dev/null
+++ b/t/t3510-cherry-pick-sequence.sh
@@ -0,0 +1,692 @@
+#!/bin/sh
+
+test_description='Test cherry-pick continuation features
+
+ + conflicting: rewrites unrelated to conflicting
+ + yetanotherpick: rewrites foo to e
+ + anotherpick: rewrites foo to d
+ + picked: rewrites foo to c
+ + unrelatedpick: rewrites unrelated to reallyunrelated
+ + base: rewrites foo to b
+ + initial: writes foo as a, unrelated as unrelated
+
+'
+
+. ./test-lib.sh
+
+# Repeat first match 10 times
+_r10='\1\1\1\1\1\1\1\1\1\1'
+
+pristine_detach () {
+ git cherry-pick --quit &&
+ git checkout -f "$1^0" &&
+ git read-tree -u --reset HEAD &&
+ git clean -d -f -f -q -x
+}
+
+test_expect_success setup '
+ git config advice.detachedhead false &&
+ echo unrelated >unrelated &&
+ git add unrelated &&
+ test_commit initial foo a &&
+ test_commit base foo b &&
+ test_commit unrelatedpick unrelated reallyunrelated &&
+ test_commit picked foo c &&
+ test_commit anotherpick foo d &&
+ test_commit yetanotherpick foo e &&
+ pristine_detach initial &&
+ test_commit conflicting unrelated
+'
+
+test_expect_success 'cherry-pick persists data on failure' '
+ pristine_detach initial &&
+ test_expect_code 1 git cherry-pick -s base..anotherpick &&
+ test_path_is_dir .git/sequencer &&
+ test_path_is_file .git/sequencer/head &&
+ test_path_is_file .git/sequencer/todo &&
+ test_path_is_file .git/sequencer/opts
+'
+
+test_expect_success 'cherry-pick mid-cherry-pick-sequence' '
+ pristine_detach initial &&
+ test_must_fail git cherry-pick base..anotherpick &&
+ test_cmp_rev picked CHERRY_PICK_HEAD &&
+ # "oops, I forgot that these patches rely on the change from base"
+ git checkout HEAD foo &&
+ git cherry-pick base &&
+ git cherry-pick picked &&
+ git cherry-pick --continue &&
+ git diff --exit-code anotherpick
+'
+
+test_expect_success 'cherry-pick persists opts correctly' '
+ pristine_detach initial &&
+ # to make sure that the session to cherry-pick a sequence
+ # gets interrupted, use a high-enough number that is larger
+ # than the number of parents of any commit we have created
+ mainline=4 &&
+ test_expect_code 128 git cherry-pick -s -m $mainline --strategy=recursive -X patience -X ours --edit initial..anotherpick &&
+ test_path_is_dir .git/sequencer &&
+ test_path_is_file .git/sequencer/head &&
+ test_path_is_file .git/sequencer/todo &&
+ test_path_is_file .git/sequencer/opts &&
+ echo "true" >expect &&
+ git config --file=.git/sequencer/opts --get-all options.signoff >actual &&
+ test_cmp expect actual &&
+ echo "$mainline" >expect &&
+ git config --file=.git/sequencer/opts --get-all options.mainline >actual &&
+ test_cmp expect actual &&
+ echo "recursive" >expect &&
+ git config --file=.git/sequencer/opts --get-all options.strategy >actual &&
+ test_cmp expect actual &&
+ cat >expect <<-\EOF &&
+ patience
+ ours
+ EOF
+ git config --file=.git/sequencer/opts --get-all options.strategy-option >actual &&
+ test_cmp expect actual &&
+ echo "true" >expect &&
+ git config --file=.git/sequencer/opts --get-all options.edit >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'revert persists opts correctly' '
+ pristine_detach initial &&
+ # to make sure that the session to revert a sequence
+ # gets interrupted, revert commits that are not in the history
+ # of HEAD.
+ test_expect_code 1 git revert -s --strategy=recursive -X patience -X ours --no-edit picked yetanotherpick &&
+ test_path_is_dir .git/sequencer &&
+ test_path_is_file .git/sequencer/head &&
+ test_path_is_file .git/sequencer/todo &&
+ test_path_is_file .git/sequencer/opts &&
+ echo "true" >expect &&
+ git config --file=.git/sequencer/opts --get-all options.signoff >actual &&
+ test_cmp expect actual &&
+ echo "recursive" >expect &&
+ git config --file=.git/sequencer/opts --get-all options.strategy >actual &&
+ test_cmp expect actual &&
+ cat >expect <<-\EOF &&
+ patience
+ ours
+ EOF
+ git config --file=.git/sequencer/opts --get-all options.strategy-option >actual &&
+ test_cmp expect actual &&
+ echo "false" >expect &&
+ git config --file=.git/sequencer/opts --get-all options.edit >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'cherry-pick cleans up sequencer state upon success' '
+ pristine_detach initial &&
+ git cherry-pick initial..picked &&
+ test_path_is_missing .git/sequencer
+'
+
+test_expect_success 'cherry-pick --skip requires cherry-pick in progress' '
+ pristine_detach initial &&
+ test_must_fail git cherry-pick --skip
+'
+
+test_expect_success 'revert --skip requires revert in progress' '
+ pristine_detach initial &&
+ test_must_fail git revert --skip
+'
+
+test_expect_success 'cherry-pick --skip to skip commit' '
+ pristine_detach initial &&
+ test_must_fail git cherry-pick anotherpick &&
+ test_must_fail git revert --skip &&
+ git cherry-pick --skip &&
+ test_cmp_rev initial HEAD &&
+ test_path_is_missing .git/CHERRY_PICK_HEAD
+'
+
+test_expect_success 'revert --skip to skip commit' '
+ pristine_detach anotherpick &&
+ test_must_fail git revert anotherpick~1 &&
+ test_must_fail git cherry-pick --skip &&
+ git revert --skip &&
+ test_cmp_rev anotherpick HEAD
+'
+
+test_expect_success 'skip "empty" commit' '
+ pristine_detach picked &&
+ test_commit dummy foo d &&
+ test_must_fail git cherry-pick anotherpick 2>err &&
+ test_grep "git cherry-pick --skip" err &&
+ git cherry-pick --skip &&
+ test_cmp_rev dummy HEAD
+'
+
+test_expect_success 'skip a commit and check if rest of sequence is correct' '
+ pristine_detach initial &&
+ echo e >expect &&
+ cat >expect.log <<-EOF &&
+ OBJID
+ :100644 100644 OBJID OBJID M foo
+ OBJID
+ :100644 100644 OBJID OBJID M foo
+ OBJID
+ :100644 100644 OBJID OBJID M unrelated
+ OBJID
+ :000000 100644 OBJID OBJID A foo
+ :000000 100644 OBJID OBJID A unrelated
+ EOF
+ test_must_fail git cherry-pick base..yetanotherpick &&
+ test_must_fail git cherry-pick --skip &&
+ echo d >foo &&
+ git add foo &&
+ git cherry-pick --continue &&
+ {
+ git rev-list HEAD |
+ git diff-tree --root --stdin |
+ sed "s/$OID_REGEX/OBJID/g"
+ } >actual.log &&
+ test_cmp expect foo &&
+ test_cmp expect.log actual.log
+'
+
+test_expect_success 'check advice when we move HEAD by committing' '
+ pristine_detach initial &&
+ cat >expect <<-EOF &&
+ error: there is nothing to skip
+ hint: have you committed already?
+ hint: try "git cherry-pick --continue"
+ fatal: cherry-pick failed
+ EOF
+ test_must_fail git cherry-pick base..yetanotherpick &&
+ echo c >foo &&
+ git commit -a &&
+ test_path_is_missing .git/CHERRY_PICK_HEAD &&
+ test_must_fail git cherry-pick --skip 2>advice &&
+ test_cmp expect advice
+'
+
+test_expect_success 'selectively advise --skip while launching another sequence' '
+ pristine_detach initial &&
+ cat >expect <<-EOF &&
+ error: cherry-pick is already in progress
+ hint: try "git cherry-pick (--continue | --skip | --abort | --quit)"
+ fatal: cherry-pick failed
+ EOF
+ test_must_fail git cherry-pick picked..yetanotherpick &&
+ test_must_fail git cherry-pick picked..yetanotherpick 2>advice &&
+ test_cmp expect advice &&
+ cat >expect <<-EOF &&
+ error: cherry-pick is already in progress
+ hint: try "git cherry-pick (--continue | --abort | --quit)"
+ fatal: cherry-pick failed
+ EOF
+ git reset --merge &&
+ test_must_fail git cherry-pick picked..yetanotherpick 2>advice &&
+ test_cmp expect advice
+'
+
+test_expect_success 'allow skipping commit but not abort for a new history' '
+ pristine_detach initial &&
+ cat >expect <<-EOF &&
+ error: cannot abort from a branch yet to be born
+ fatal: cherry-pick failed
+ EOF
+ git checkout --orphan new_disconnected &&
+ git reset --hard &&
+ test_must_fail git cherry-pick anotherpick &&
+ test_must_fail git cherry-pick --abort 2>advice &&
+ git cherry-pick --skip &&
+ test_cmp expect advice
+'
+
+test_expect_success 'allow skipping stopped cherry-pick because of untracked file modifications' '
+ test_when_finished "rm unrelated" &&
+ pristine_detach initial &&
+ git rm --cached unrelated &&
+ git commit -m "untrack unrelated" &&
+ test_must_fail git cherry-pick initial base &&
+ test_path_is_missing .git/CHERRY_PICK_HEAD &&
+ git cherry-pick --skip
+'
+
+test_expect_success '--quit does not complain when no cherry-pick is in progress' '
+ pristine_detach initial &&
+ git cherry-pick --quit
+'
+
+test_expect_success '--abort requires cherry-pick in progress' '
+ pristine_detach initial &&
+ test_must_fail git cherry-pick --abort
+'
+
+test_expect_success '--quit cleans up sequencer state' '
+ pristine_detach initial &&
+ test_expect_code 1 git cherry-pick base..picked &&
+ git cherry-pick --quit &&
+ test_path_is_missing .git/sequencer &&
+ test_path_is_missing .git/CHERRY_PICK_HEAD
+'
+
+test_expect_success '--quit keeps HEAD and conflicted index intact' '
+ pristine_detach initial &&
+ cat >expect <<-\EOF &&
+ OBJID
+ :100644 100644 OBJID OBJID M unrelated
+ OBJID
+ :000000 100644 OBJID OBJID A foo
+ :000000 100644 OBJID OBJID A unrelated
+ EOF
+ test_expect_code 1 git cherry-pick base..picked &&
+ git cherry-pick --quit &&
+ test_path_is_missing .git/sequencer &&
+ test_must_fail git update-index --refresh &&
+ {
+ git rev-list HEAD |
+ git diff-tree --root --stdin |
+ sed "s/$OID_REGEX/OBJID/g"
+ } >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--abort to cancel multiple cherry-pick' '
+ pristine_detach initial &&
+ test_expect_code 1 git cherry-pick base..anotherpick &&
+ git cherry-pick --abort &&
+ test_path_is_missing .git/sequencer &&
+ test_path_is_missing .git/CHERRY_PICK_HEAD &&
+ test_cmp_rev initial HEAD &&
+ git update-index --refresh &&
+ git diff-index --exit-code HEAD
+'
+
+test_expect_success '--abort to cancel single cherry-pick' '
+ pristine_detach initial &&
+ test_expect_code 1 git cherry-pick picked &&
+ git cherry-pick --abort &&
+ test_path_is_missing .git/sequencer &&
+ test_path_is_missing .git/CHERRY_PICK_HEAD &&
+ test_cmp_rev initial HEAD &&
+ git update-index --refresh &&
+ git diff-index --exit-code HEAD
+'
+
+test_expect_success '--abort does not unsafely change HEAD' '
+ pristine_detach initial &&
+ test_must_fail git cherry-pick picked anotherpick &&
+ git reset --hard base &&
+ test_must_fail git cherry-pick picked anotherpick &&
+ git cherry-pick --abort 2>actual &&
+ test_grep "You seem to have moved HEAD" actual &&
+ test_cmp_rev base HEAD
+'
+
+test_expect_success 'cherry-pick --abort to cancel multiple revert' '
+ pristine_detach anotherpick &&
+ test_expect_code 1 git revert base..picked &&
+ git cherry-pick --abort &&
+ test_path_is_missing .git/sequencer &&
+ test_path_is_missing .git/CHERRY_PICK_HEAD &&
+ test_cmp_rev anotherpick HEAD &&
+ git update-index --refresh &&
+ git diff-index --exit-code HEAD
+'
+
+test_expect_success 'revert --abort works, too' '
+ pristine_detach anotherpick &&
+ test_expect_code 1 git revert base..picked &&
+ git revert --abort &&
+ test_path_is_missing .git/sequencer &&
+ test_cmp_rev anotherpick HEAD
+'
+
+test_expect_success '--abort to cancel single revert' '
+ pristine_detach anotherpick &&
+ test_expect_code 1 git revert picked &&
+ git revert --abort &&
+ test_path_is_missing .git/sequencer &&
+ test_cmp_rev anotherpick HEAD &&
+ git update-index --refresh &&
+ git diff-index --exit-code HEAD
+'
+
+test_expect_success '--abort keeps unrelated change, easy case' '
+ pristine_detach unrelatedpick &&
+ echo changed >expect &&
+ test_expect_code 1 git cherry-pick picked..yetanotherpick &&
+ echo changed >unrelated &&
+ git cherry-pick --abort &&
+ test_cmp expect unrelated
+'
+
+test_expect_success '--abort refuses to clobber unrelated change, harder case' '
+ pristine_detach initial &&
+ echo changed >expect &&
+ test_expect_code 1 git cherry-pick base..anotherpick &&
+ echo changed >unrelated &&
+ test_must_fail git cherry-pick --abort &&
+ test_cmp expect unrelated &&
+ git rev-list HEAD >log &&
+ test_line_count = 2 log &&
+ test_must_fail git update-index --refresh &&
+
+ git checkout unrelated &&
+ git cherry-pick --abort &&
+ test_cmp_rev initial HEAD
+'
+
+test_expect_success 'cherry-pick still writes sequencer state when one commit is left' '
+ pristine_detach initial &&
+ test_expect_code 1 git cherry-pick base..picked &&
+ test_path_is_dir .git/sequencer &&
+ echo "resolved" >foo &&
+ git add foo &&
+ git commit &&
+ {
+ git rev-list HEAD |
+ git diff-tree --root --stdin |
+ sed "s/$OID_REGEX/OBJID/g"
+ } >actual &&
+ cat >expect <<-\EOF &&
+ OBJID
+ :100644 100644 OBJID OBJID M foo
+ OBJID
+ :100644 100644 OBJID OBJID M unrelated
+ OBJID
+ :000000 100644 OBJID OBJID A foo
+ :000000 100644 OBJID OBJID A unrelated
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success '--abort after last commit in sequence' '
+ pristine_detach initial &&
+ test_expect_code 1 git cherry-pick base..picked &&
+ git cherry-pick --abort &&
+ test_path_is_missing .git/sequencer &&
+ test_path_is_missing .git/CHERRY_PICK_HEAD &&
+ test_cmp_rev initial HEAD &&
+ git update-index --refresh &&
+ git diff-index --exit-code HEAD
+'
+
+test_expect_success 'cherry-pick does not implicitly stomp an existing operation' '
+ pristine_detach initial &&
+ test_expect_code 1 git cherry-pick base..anotherpick &&
+ test-tool chmtime --get .git/sequencer >expect &&
+ test_expect_code 128 git cherry-pick unrelatedpick &&
+ test-tool chmtime --get .git/sequencer >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--continue complains when no cherry-pick is in progress' '
+ pristine_detach initial &&
+ test_expect_code 128 git cherry-pick --continue
+'
+
+test_expect_success '--continue complains when there are unresolved conflicts' '
+ pristine_detach initial &&
+ test_expect_code 1 git cherry-pick base..anotherpick &&
+ test_expect_code 128 git cherry-pick --continue
+'
+
+test_expect_success '--continue of single cherry-pick' '
+ pristine_detach initial &&
+ echo c >expect &&
+ test_must_fail git cherry-pick picked &&
+ echo c >foo &&
+ git add foo &&
+ git cherry-pick --continue &&
+
+ test_cmp expect foo &&
+ test_cmp_rev initial HEAD^ &&
+ git diff --exit-code HEAD &&
+ test_must_fail git rev-parse --verify CHERRY_PICK_HEAD
+'
+
+test_expect_success '--continue of single revert' '
+ pristine_detach initial &&
+ echo resolved >expect &&
+ echo "Revert \"picked\"" >expect.msg &&
+ test_must_fail git revert picked &&
+ echo resolved >foo &&
+ git add foo &&
+ git cherry-pick --continue &&
+
+ git diff --exit-code HEAD &&
+ test_cmp expect foo &&
+ test_cmp_rev initial HEAD^ &&
+ git diff-tree -s --pretty=tformat:%s HEAD >msg &&
+ test_cmp expect.msg msg &&
+ test_must_fail git rev-parse --verify CHERRY_PICK_HEAD &&
+ test_must_fail git rev-parse --verify REVERT_HEAD
+'
+
+test_expect_success '--continue after resolving conflicts' '
+ pristine_detach initial &&
+ echo d >expect &&
+ cat >expect.log <<-\EOF &&
+ OBJID
+ :100644 100644 OBJID OBJID M foo
+ OBJID
+ :100644 100644 OBJID OBJID M foo
+ OBJID
+ :100644 100644 OBJID OBJID M unrelated
+ OBJID
+ :000000 100644 OBJID OBJID A foo
+ :000000 100644 OBJID OBJID A unrelated
+ EOF
+ test_must_fail git cherry-pick base..anotherpick &&
+ echo c >foo &&
+ git add foo &&
+ git cherry-pick --continue &&
+ {
+ git rev-list HEAD |
+ git diff-tree --root --stdin |
+ sed "s/$OID_REGEX/OBJID/g"
+ } >actual.log &&
+ test_cmp expect foo &&
+ test_cmp expect.log actual.log
+'
+
+test_expect_success '--continue after resolving conflicts and committing' '
+ pristine_detach initial &&
+ test_expect_code 1 git cherry-pick base..anotherpick &&
+ echo "c" >foo &&
+ git add foo &&
+ git commit &&
+ git cherry-pick --continue &&
+ test_path_is_missing .git/sequencer &&
+ {
+ git rev-list HEAD |
+ git diff-tree --root --stdin |
+ sed "s/$OID_REGEX/OBJID/g"
+ } >actual &&
+ cat >expect <<-\EOF &&
+ OBJID
+ :100644 100644 OBJID OBJID M foo
+ OBJID
+ :100644 100644 OBJID OBJID M foo
+ OBJID
+ :100644 100644 OBJID OBJID M unrelated
+ OBJID
+ :000000 100644 OBJID OBJID A foo
+ :000000 100644 OBJID OBJID A unrelated
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success '--continue asks for help after resolving patch to nil' '
+ pristine_detach conflicting &&
+ test_must_fail git cherry-pick initial..picked &&
+
+ test_cmp_rev unrelatedpick CHERRY_PICK_HEAD &&
+ git checkout HEAD -- unrelated &&
+ test_must_fail git cherry-pick --continue 2>msg &&
+ test_grep "The previous cherry-pick is now empty" msg
+'
+
+test_expect_success 'follow advice and skip nil patch' '
+ pristine_detach conflicting &&
+ test_must_fail git cherry-pick initial..picked &&
+
+ git checkout HEAD -- unrelated &&
+ test_must_fail git cherry-pick --continue &&
+ git reset &&
+ git cherry-pick --continue &&
+
+ git rev-list initial..HEAD >commits &&
+ test_line_count = 3 commits
+'
+
+test_expect_success '--continue respects opts' '
+ pristine_detach initial &&
+ test_expect_code 1 git cherry-pick -x base..anotherpick &&
+ echo "c" >foo &&
+ git add foo &&
+ git commit &&
+ git cherry-pick --continue &&
+ test_path_is_missing .git/sequencer &&
+ git cat-file commit HEAD >anotherpick_msg &&
+ git cat-file commit HEAD~1 >picked_msg &&
+ git cat-file commit HEAD~2 >unrelatedpick_msg &&
+ git cat-file commit HEAD~3 >initial_msg &&
+ ! grep "cherry picked from" initial_msg &&
+ grep "cherry picked from" unrelatedpick_msg &&
+ grep "cherry picked from" picked_msg &&
+ grep "cherry picked from" anotherpick_msg
+'
+
+test_expect_success '--continue of single-pick respects -x' '
+ pristine_detach initial &&
+ test_must_fail git cherry-pick -x picked &&
+ echo c >foo &&
+ git add foo &&
+ git cherry-pick --continue &&
+ test_path_is_missing .git/sequencer &&
+ git cat-file commit HEAD >msg &&
+ grep "cherry picked from" msg
+'
+
+test_expect_success '--continue respects -x in first commit in multi-pick' '
+ pristine_detach initial &&
+ test_must_fail git cherry-pick -x picked anotherpick &&
+ echo c >foo &&
+ git add foo &&
+ git cherry-pick --continue &&
+ test_path_is_missing .git/sequencer &&
+ git cat-file commit HEAD^ >msg &&
+ picked=$(git rev-parse --verify picked) &&
+ grep "cherry picked from.*$picked" msg
+'
+
+test_expect_failure '--signoff is automatically propagated to resolved conflict' '
+ pristine_detach initial &&
+ test_expect_code 1 git cherry-pick --signoff base..anotherpick &&
+ echo "c" >foo &&
+ git add foo &&
+ git commit &&
+ git cherry-pick --continue &&
+ test_path_is_missing .git/sequencer &&
+ git cat-file commit HEAD >anotherpick_msg &&
+ git cat-file commit HEAD~1 >picked_msg &&
+ git cat-file commit HEAD~2 >unrelatedpick_msg &&
+ git cat-file commit HEAD~3 >initial_msg &&
+ ! grep "Signed-off-by:" initial_msg &&
+ grep "Signed-off-by:" unrelatedpick_msg &&
+ ! grep "Signed-off-by:" picked_msg &&
+ grep "Signed-off-by:" anotherpick_msg
+'
+
+test_expect_failure '--signoff dropped for implicit commit of resolution, multi-pick case' '
+ pristine_detach initial &&
+ test_must_fail git cherry-pick -s picked anotherpick &&
+ echo c >foo &&
+ git add foo &&
+ git cherry-pick --continue &&
+
+ git diff --exit-code HEAD &&
+ test_cmp_rev initial HEAD^^ &&
+ git cat-file commit HEAD^ >msg &&
+ ! grep Signed-off-by: msg
+'
+
+test_expect_failure 'sign-off needs to be reaffirmed after conflict resolution, single-pick case' '
+ pristine_detach initial &&
+ test_must_fail git cherry-pick -s picked &&
+ echo c >foo &&
+ git add foo &&
+ git cherry-pick --continue &&
+
+ git diff --exit-code HEAD &&
+ test_cmp_rev initial HEAD^ &&
+ git cat-file commit HEAD >msg &&
+ ! grep Signed-off-by: msg
+'
+
+test_expect_success 'malformed instruction sheet 1' '
+ pristine_detach initial &&
+ test_expect_code 1 git cherry-pick base..anotherpick &&
+ echo "resolved" >foo &&
+ git add foo &&
+ git commit &&
+ sed "s/pick /pick/" .git/sequencer/todo >new_sheet &&
+ cp new_sheet .git/sequencer/todo &&
+ test_expect_code 128 git cherry-pick --continue
+'
+
+test_expect_success 'malformed instruction sheet 2' '
+ pristine_detach initial &&
+ test_expect_code 1 git cherry-pick base..anotherpick &&
+ echo "resolved" >foo &&
+ git add foo &&
+ git commit &&
+ sed "s/pick/revert/" .git/sequencer/todo >new_sheet &&
+ cp new_sheet .git/sequencer/todo &&
+ test_expect_code 128 git cherry-pick --continue
+'
+
+test_expect_success 'empty commit set (no commits to walk)' '
+ pristine_detach initial &&
+ test_expect_code 128 git cherry-pick base..base
+'
+
+test_expect_success 'empty commit set (culled during walk)' '
+ pristine_detach initial &&
+ test_expect_code 128 git cherry-pick -2 --author=no.such.author base
+'
+
+test_expect_success 'malformed instruction sheet 3' '
+ pristine_detach initial &&
+ test_expect_code 1 git cherry-pick base..anotherpick &&
+ echo "resolved" >foo &&
+ git add foo &&
+ git commit &&
+ sed "s/pick \([0-9a-f]*\)/pick $_r10/" .git/sequencer/todo >new_sheet &&
+ cp new_sheet .git/sequencer/todo &&
+ test_expect_code 128 git cherry-pick --continue
+'
+
+test_expect_success 'instruction sheet, fat-fingers version' '
+ pristine_detach initial &&
+ test_expect_code 1 git cherry-pick base..anotherpick &&
+ echo "c" >foo &&
+ git add foo &&
+ git commit &&
+ sed "s/pick \([0-9a-f]*\)/pick \1 /" .git/sequencer/todo >new_sheet &&
+ cp new_sheet .git/sequencer/todo &&
+ git cherry-pick --continue
+'
+
+test_expect_success 'commit descriptions in insn sheet are optional' '
+ pristine_detach initial &&
+ test_expect_code 1 git cherry-pick base..anotherpick &&
+ echo "c" >foo &&
+ git add foo &&
+ git commit &&
+ cut -d" " -f1,2 .git/sequencer/todo >new_sheet &&
+ cp new_sheet .git/sequencer/todo &&
+ git cherry-pick --continue &&
+ test_path_is_missing .git/sequencer &&
+ git rev-list HEAD >commits &&
+ test_line_count = 4 commits
+'
+
+test_done
diff --git a/t/t3511-cherry-pick-x.sh b/t/t3511-cherry-pick-x.sh
new file mode 100755
index 0000000..dd5d92e
--- /dev/null
+++ b/t/t3511-cherry-pick-x.sh
@@ -0,0 +1,322 @@
+#!/bin/sh
+
+test_description='Test cherry-pick -x and -s'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+pristine_detach () {
+ git cherry-pick --quit &&
+ git checkout -f "$1^0" &&
+ git read-tree -u --reset HEAD &&
+ git clean -d -f -f -q -x
+}
+
+mesg_one_line='base: commit message'
+
+mesg_no_footer="$mesg_one_line
+
+OneWordBodyThatsNotA-S-o-B"
+
+mesg_with_footer="$mesg_no_footer
+
+Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
+Signed-off-by: A.U. Thor <author@example.com>
+Signed-off-by: B.U. Thor <buthor@example.com>"
+
+mesg_broken_footer="$mesg_no_footer
+
+This is not recognized as a footer because Myfooter is not a recognized token.
+Myfooter: A.U. Thor <author@example.com>"
+
+mesg_with_footer_sob="$mesg_with_footer
+Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
+
+mesg_with_cherry_footer="$mesg_with_footer_sob
+(cherry picked from commit da39a3ee5e6b4b0d3255bfef95601890afd80709)
+Tested-by: C.U. Thor <cuthor@example.com>"
+
+mesg_unclean="$mesg_one_line
+
+
+leading empty lines
+
+
+consecutive empty lines
+
+# hash tag comment
+
+trailing empty lines
+
+
+"
+
+test_expect_success setup '
+ git config advice.detachedhead false &&
+ echo unrelated >unrelated &&
+ git add unrelated &&
+ test_commit initial foo a &&
+ test_commit "$mesg_one_line" foo b mesg-one-line &&
+ git reset --hard initial &&
+ test_commit "$mesg_no_footer" foo b mesg-no-footer &&
+ git reset --hard initial &&
+ test_commit "$mesg_broken_footer" foo b mesg-broken-footer &&
+ git reset --hard initial &&
+ test_commit "$mesg_with_footer" foo b mesg-with-footer &&
+ git reset --hard initial &&
+ test_commit "$mesg_with_footer_sob" foo b mesg-with-footer-sob &&
+ git reset --hard initial &&
+ test_commit "$mesg_with_cherry_footer" foo b mesg-with-cherry-footer &&
+ git reset --hard initial &&
+ test_config commit.cleanup verbatim &&
+ test_commit "$mesg_unclean" foo b mesg-unclean &&
+ test_unconfig commit.cleanup &&
+ pristine_detach initial &&
+ test_commit conflicting unrelated
+'
+
+test_expect_success 'cherry-pick -x inserts blank line after one line subject' '
+ pristine_detach initial &&
+ sha1=$(git rev-parse mesg-one-line^0) &&
+ git cherry-pick -x mesg-one-line &&
+ cat <<-EOF >expect &&
+ $mesg_one_line
+
+ (cherry picked from commit $sha1)
+ EOF
+ git log -1 --pretty=format:%B >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'cherry-pick -s inserts blank line after one line subject' '
+ pristine_detach initial &&
+ git cherry-pick -s mesg-one-line &&
+ cat <<-EOF >expect &&
+ $mesg_one_line
+
+ Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
+ EOF
+ git log -1 --pretty=format:%B >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'cherry-pick -s inserts blank line after non-conforming footer' '
+ pristine_detach initial &&
+ git cherry-pick -s mesg-broken-footer &&
+ cat <<-EOF >expect &&
+ $mesg_broken_footer
+
+ Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
+ EOF
+ git log -1 --pretty=format:%B >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'cherry-pick -s recognizes trailer config' '
+ pristine_detach initial &&
+ git -c "trailer.Myfooter.ifexists=add" cherry-pick -s mesg-broken-footer &&
+ cat <<-EOF >expect &&
+ $mesg_broken_footer
+ Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
+ EOF
+ git log -1 --pretty=format:%B >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'cherry-pick -x inserts blank line when conforming footer not found' '
+ pristine_detach initial &&
+ sha1=$(git rev-parse mesg-no-footer^0) &&
+ git cherry-pick -x mesg-no-footer &&
+ cat <<-EOF >expect &&
+ $mesg_no_footer
+
+ (cherry picked from commit $sha1)
+ EOF
+ git log -1 --pretty=format:%B >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'cherry-pick -s inserts blank line when conforming footer not found' '
+ pristine_detach initial &&
+ git cherry-pick -s mesg-no-footer &&
+ cat <<-EOF >expect &&
+ $mesg_no_footer
+
+ Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
+ EOF
+ git log -1 --pretty=format:%B >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'cherry-pick -x -s inserts blank line when conforming footer not found' '
+ pristine_detach initial &&
+ sha1=$(git rev-parse mesg-no-footer^0) &&
+ git cherry-pick -x -s mesg-no-footer &&
+ cat <<-EOF >expect &&
+ $mesg_no_footer
+
+ (cherry picked from commit $sha1)
+ Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
+ EOF
+ git log -1 --pretty=format:%B >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'cherry-pick -s adds sob when last sob doesnt match committer' '
+ pristine_detach initial &&
+ git cherry-pick -s mesg-with-footer &&
+ cat <<-EOF >expect &&
+ $mesg_with_footer
+ Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
+ EOF
+ git log -1 --pretty=format:%B >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'cherry-pick -x -s adds sob when last sob doesnt match committer' '
+ pristine_detach initial &&
+ sha1=$(git rev-parse mesg-with-footer^0) &&
+ git cherry-pick -x -s mesg-with-footer &&
+ cat <<-EOF >expect &&
+ $mesg_with_footer
+ (cherry picked from commit $sha1)
+ Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
+ EOF
+ git log -1 --pretty=format:%B >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'cherry-pick -s refrains from adding duplicate trailing sob' '
+ pristine_detach initial &&
+ git cherry-pick -s mesg-with-footer-sob &&
+ cat <<-EOF >expect &&
+ $mesg_with_footer_sob
+ EOF
+ git log -1 --pretty=format:%B >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'cherry-pick -x -s adds sob even when trailing sob exists for committer' '
+ pristine_detach initial &&
+ sha1=$(git rev-parse mesg-with-footer-sob^0) &&
+ git cherry-pick -x -s mesg-with-footer-sob &&
+ cat <<-EOF >expect &&
+ $mesg_with_footer_sob
+ (cherry picked from commit $sha1)
+ Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
+ EOF
+ git log -1 --pretty=format:%B >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'cherry-pick -x handles commits with no NL at end of message' '
+ pristine_detach initial &&
+ printf "title\n\nSigned-off-by: A <a@example.com>" >msg &&
+ sha1=$(git commit-tree -p initial mesg-with-footer^{tree} <msg) &&
+ git cherry-pick -x $sha1 &&
+ git log -1 --pretty=format:%B >actual &&
+
+ printf "\n(cherry picked from commit %s)\n" $sha1 >>msg &&
+ test_cmp msg actual
+'
+
+test_expect_success 'cherry-pick -x handles commits with no footer and no NL at end of message' '
+ pristine_detach initial &&
+ printf "title\n\nnot a footer" >msg &&
+ sha1=$(git commit-tree -p initial mesg-with-footer^{tree} <msg) &&
+ git cherry-pick -x $sha1 &&
+ git log -1 --pretty=format:%B >actual &&
+
+ printf "\n\n(cherry picked from commit %s)\n" $sha1 >>msg &&
+ test_cmp msg actual
+'
+
+test_expect_success 'cherry-pick -s handles commits with no NL at end of message' '
+ pristine_detach initial &&
+ printf "title\n\nSigned-off-by: A <a@example.com>" >msg &&
+ sha1=$(git commit-tree -p initial mesg-with-footer^{tree} <msg) &&
+ git cherry-pick -s $sha1 &&
+ git log -1 --pretty=format:%B >actual &&
+
+ printf "\nSigned-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>\n" >>msg &&
+ test_cmp msg actual
+'
+
+test_expect_success 'cherry-pick -s handles commits with no footer and no NL at end of message' '
+ pristine_detach initial &&
+ printf "title\n\nnot a footer" >msg &&
+ sha1=$(git commit-tree -p initial mesg-with-footer^{tree} <msg) &&
+ git cherry-pick -s $sha1 &&
+ git log -1 --pretty=format:%B >actual &&
+
+ printf "\n\nSigned-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>\n" >>msg &&
+ test_cmp msg actual
+'
+
+test_expect_success 'cherry-pick -x treats "(cherry picked from..." line as part of footer' '
+ pristine_detach initial &&
+ sha1=$(git rev-parse mesg-with-cherry-footer^0) &&
+ git cherry-pick -x mesg-with-cherry-footer &&
+ cat <<-EOF >expect &&
+ $mesg_with_cherry_footer
+ (cherry picked from commit $sha1)
+ EOF
+ git log -1 --pretty=format:%B >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'cherry-pick -s treats "(cherry picked from..." line as part of footer' '
+ pristine_detach initial &&
+ git cherry-pick -s mesg-with-cherry-footer &&
+ cat <<-EOF >expect &&
+ $mesg_with_cherry_footer
+ Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
+ EOF
+ git log -1 --pretty=format:%B >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'cherry-pick -x -s treats "(cherry picked from..." line as part of footer' '
+ pristine_detach initial &&
+ sha1=$(git rev-parse mesg-with-cherry-footer^0) &&
+ git cherry-pick -x -s mesg-with-cherry-footer &&
+ cat <<-EOF >expect &&
+ $mesg_with_cherry_footer
+ (cherry picked from commit $sha1)
+ Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
+ EOF
+ git log -1 --pretty=format:%B >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'cherry-pick preserves commit message' '
+ pristine_detach initial &&
+ printf "$mesg_unclean" >expect &&
+ git log -1 --pretty=format:%B mesg-unclean >actual &&
+ test_cmp expect actual &&
+ git cherry-pick mesg-unclean &&
+ git log -1 --pretty=format:%B >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'cherry-pick -x cleans commit message' '
+ pristine_detach initial &&
+ git cherry-pick -x mesg-unclean &&
+ git log -1 --pretty=format:%B >actual &&
+ printf "%s\n(cherry picked from commit %s)\n" \
+ "$mesg_unclean" $(git rev-parse mesg-unclean) |
+ git stripspace >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'cherry-pick -x respects commit.cleanup' '
+ pristine_detach initial &&
+ git -c commit.cleanup=strip cherry-pick -x mesg-unclean &&
+ git log -1 --pretty=format:%B >actual &&
+ printf "%s\n(cherry picked from commit %s)\n" \
+ "$mesg_unclean" $(git rev-parse mesg-unclean) |
+ git stripspace -s >expect &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t3512-cherry-pick-submodule.sh b/t/t3512-cherry-pick-submodule.sh
new file mode 100755
index 0000000..f22d1dd
--- /dev/null
+++ b/t/t3512-cherry-pick-submodule.sh
@@ -0,0 +1,56 @@
+#!/bin/sh
+
+test_description='cherry-pick can handle submodules'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-submodule-update.sh
+
+if test "$GIT_TEST_MERGE_ALGORITHM" != ort
+then
+ KNOWN_FAILURE_NOFF_MERGE_DOESNT_CREATE_EMPTY_SUBMODULE_DIR=1
+ KNOWN_FAILURE_NOFF_MERGE_ATTEMPTS_TO_MERGE_REMOVED_SUBMODULE_FILES=1
+fi
+test_submodule_switch "cherry-pick"
+
+test_expect_success 'unrelated submodule/file conflict is ignored' '
+ test_config_global protocol.file.allow always &&
+
+ test_create_repo sub &&
+
+ touch sub/file &&
+ git -C sub add file &&
+ git -C sub commit -m "add a file in a submodule" &&
+
+ test_create_repo a_repo &&
+ (
+ cd a_repo &&
+ >a_file &&
+ git add a_file &&
+ git commit -m "add a file" &&
+
+ git branch test &&
+ git checkout test &&
+
+ mkdir sub &&
+ >sub/content &&
+ git add sub/content &&
+ git commit -m "add a regular folder with name sub" &&
+
+ echo "123" >a_file &&
+ git add a_file &&
+ git commit -m "modify a file" &&
+
+ git checkout main &&
+
+ git submodule add ../sub sub &&
+ git submodule update sub &&
+ git commit -m "add a submodule info folder with name sub" &&
+
+ git cherry-pick test
+ )
+'
+
+test_done
diff --git a/t/t3513-revert-submodule.sh b/t/t3513-revert-submodule.sh
new file mode 100755
index 0000000..8bfe3ed
--- /dev/null
+++ b/t/t3513-revert-submodule.sh
@@ -0,0 +1,39 @@
+#!/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" cf "$TRASH_DIRECTORY/tmp.tar" * &&
+ may_only_be_test_must_fail "$2" &&
+ $2 git checkout "$1" &&
+ if test -n "$2"
+ then
+ return
+ fi &&
+ git revert HEAD &&
+ rm -rf * &&
+ "$TAR" xf "$TRASH_DIRECTORY/tmp.tar" &&
+ git status -su >actual &&
+ ls -1pR * >>actual &&
+ test_cmp expect actual &&
+ git revert HEAD
+}
+
+if test "$GIT_TEST_MERGE_ALGORITHM" != ort
+then
+ KNOWN_FAILURE_NOFF_MERGE_DOESNT_CREATE_EMPTY_SUBMODULE_DIR=1
+fi
+test_submodule_switch_func "git_revert"
+
+test_done
diff --git a/t/t3514-cherry-pick-revert-gpg.sh b/t/t3514-cherry-pick-revert-gpg.sh
new file mode 100755
index 0000000..5b2e250
--- /dev/null
+++ b/t/t3514-cherry-pick-revert-gpg.sh
@@ -0,0 +1,86 @@
+#!/bin/sh
+#
+# Copyright (c) 2020 Doan Tran Cong Danh
+#
+
+test_description='test {cherry-pick,revert} --[no-]gpg-sign'
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY/lib-gpg.sh"
+
+if ! test_have_prereq GPG
+then
+ skip_all='skip all test {cherry-pick,revert} --[no-]gpg-sign, gpg not available'
+ test_done
+fi
+
+test_gpg_sign () {
+ local must_fail= will=will fake_editor=
+ if test "x$1" = "x!"
+ then
+ must_fail=test_must_fail
+ will="won't"
+ shift
+ fi
+ conf=$1
+ cmd=$2
+ cmit=$3
+ shift 3
+ test_expect_success "$cmd $* $cmit with commit.gpgsign=$conf $will sign commit" "
+ git reset --hard tip &&
+ git config commit.gpgsign $conf &&
+ git $cmd $* $cmit &&
+ git rev-list tip.. >rev-list &&
+ $must_fail git verify-commit \$(cat rev-list)
+ "
+}
+
+test_expect_success 'setup' '
+ test_commit one &&
+ git switch -c side &&
+ test_commit side1 &&
+ test_commit side2 &&
+ git switch - &&
+ test_commit two &&
+ test_commit three &&
+ test_commit tip
+'
+
+test_gpg_sign ! false cherry-pick side
+test_gpg_sign ! false cherry-pick ..side
+test_gpg_sign true cherry-pick side
+test_gpg_sign true cherry-pick ..side
+test_gpg_sign ! true cherry-pick side --no-gpg-sign
+test_gpg_sign ! true cherry-pick ..side --no-gpg-sign
+test_gpg_sign ! true cherry-pick side --gpg-sign --no-gpg-sign
+test_gpg_sign ! true cherry-pick ..side --gpg-sign --no-gpg-sign
+test_gpg_sign false cherry-pick side --no-gpg-sign --gpg-sign
+test_gpg_sign false cherry-pick ..side --no-gpg-sign --gpg-sign
+test_gpg_sign true cherry-pick side --edit
+test_gpg_sign true cherry-pick ..side --edit
+test_gpg_sign ! true cherry-pick side --edit --no-gpg-sign
+test_gpg_sign ! true cherry-pick ..side --edit --no-gpg-sign
+test_gpg_sign ! true cherry-pick side --edit --gpg-sign --no-gpg-sign
+test_gpg_sign ! true cherry-pick ..side --edit --gpg-sign --no-gpg-sign
+test_gpg_sign false cherry-pick side --edit --no-gpg-sign --gpg-sign
+test_gpg_sign false cherry-pick ..side --edit --no-gpg-sign --gpg-sign
+
+test_gpg_sign ! false revert HEAD --edit
+test_gpg_sign ! false revert two.. --edit
+test_gpg_sign true revert HEAD --edit
+test_gpg_sign true revert two.. --edit
+test_gpg_sign ! true revert HEAD --edit --no-gpg-sign
+test_gpg_sign ! true revert two.. --edit --no-gpg-sign
+test_gpg_sign ! true revert HEAD --edit --gpg-sign --no-gpg-sign
+test_gpg_sign ! true revert two.. --edit --gpg-sign --no-gpg-sign
+test_gpg_sign false revert HEAD --edit --no-gpg-sign --gpg-sign
+test_gpg_sign false revert two.. --edit --no-gpg-sign --gpg-sign
+test_gpg_sign true revert HEAD --no-edit
+test_gpg_sign true revert two.. --no-edit
+test_gpg_sign ! true revert HEAD --no-edit --no-gpg-sign
+test_gpg_sign ! true revert two.. --no-edit --no-gpg-sign
+test_gpg_sign ! true revert HEAD --no-edit --gpg-sign --no-gpg-sign
+test_gpg_sign ! true revert two.. --no-edit --gpg-sign --no-gpg-sign
+test_gpg_sign false revert HEAD --no-edit --no-gpg-sign --gpg-sign
+
+test_done
diff --git a/t/t3600-rm.sh b/t/t3600-rm.sh
new file mode 100755
index 0000000..98259e2
--- /dev/null
+++ b/t/t3600-rm.sh
@@ -0,0 +1,908 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Carl D. Worth
+#
+
+test_description='Test of the various options to git rm.'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+# Setup some files to be removed, some with funny characters
+test_expect_success 'Initialize test directory' '
+ touch -- foo bar baz "space embedded" -q &&
+ git add -- foo bar baz "space embedded" -q &&
+ git commit -m "add normal files"
+'
+
+if test_have_prereq !FUNNYNAMES
+then
+ say 'Your filesystem does not allow tabs in filenames.'
+fi
+
+test_expect_success FUNNYNAMES 'add files with funny names' '
+ touch -- "tab embedded" "newline${LF}embedded" &&
+ git add -- "tab embedded" "newline${LF}embedded" &&
+ git commit -m "add files with tabs and newlines"
+'
+
+test_expect_success 'Pre-check that foo exists and is in index before git rm foo' '
+ test_path_is_file foo &&
+ git ls-files --error-unmatch foo
+'
+
+test_expect_success 'Test that git rm foo succeeds' '
+ git rm --cached foo
+'
+
+test_expect_success 'Test that git rm --cached foo succeeds if the index matches the file' '
+ echo content >foo &&
+ git add foo &&
+ git rm --cached foo
+'
+
+test_expect_success 'Test that git rm --cached foo succeeds if the index matches the file' '
+ echo content >foo &&
+ git add foo &&
+ git commit -m foo &&
+ echo "other content" >foo &&
+ git rm --cached foo
+'
+
+test_expect_success 'Test that git rm --cached foo fails if the index matches neither the file nor HEAD' '
+ echo content >foo &&
+ git add foo &&
+ git commit -m foo --allow-empty &&
+ echo "other content" >foo &&
+ git add foo &&
+ echo "yet another content" >foo &&
+ test_must_fail git rm --cached foo
+'
+
+test_expect_success 'Test that git rm --cached -f foo works in case where --cached only did not' '
+ echo content >foo &&
+ git add foo &&
+ git commit -m foo --allow-empty &&
+ echo "other content" >foo &&
+ git add foo &&
+ echo "yet another content" >foo &&
+ git rm --cached -f foo
+'
+
+test_expect_success 'Post-check that foo exists but is not in index after git rm foo' '
+ test_path_is_file foo &&
+ test_must_fail git ls-files --error-unmatch foo
+'
+
+test_expect_success 'Pre-check that bar exists and is in index before "git rm bar"' '
+ test_path_is_file bar &&
+ git ls-files --error-unmatch bar
+'
+
+test_expect_success 'Test that "git rm bar" succeeds' '
+ git rm bar
+'
+
+test_expect_success 'Post-check that bar does not exist and is not in index after "git rm -f bar"' '
+ test_path_is_missing bar &&
+ test_must_fail git ls-files --error-unmatch bar
+'
+
+test_expect_success 'Test that "git rm -- -q" succeeds (remove a file that looks like an option)' '
+ git rm -- -q
+'
+
+test_expect_success FUNNYNAMES 'Test that "git rm -f" succeeds with embedded space, tab, or newline characters.' '
+ git rm -f "space embedded" "tab embedded" "newline${LF}embedded"
+'
+
+test_expect_success SANITY 'Test that "git rm -f" fails if its rm fails' '
+ test_when_finished "chmod 775 ." &&
+ chmod a-w . &&
+ test_must_fail git rm -f baz
+'
+
+test_expect_success 'When the rm in "git rm -f" fails, it should not remove the file from the index' '
+ git ls-files --error-unmatch baz
+'
+
+test_expect_success 'Remove nonexistent file with --ignore-unmatch' '
+ git rm --ignore-unmatch nonexistent
+'
+
+test_expect_success '"rm" command printed' '
+ echo frotz >test-file &&
+ git add test-file &&
+ git commit -m "add file for rm test" &&
+ git rm test-file >rm-output.raw &&
+ grep "^rm " rm-output.raw >rm-output &&
+ test_line_count = 1 rm-output &&
+ rm -f test-file rm-output.raw rm-output &&
+ git commit -m "remove file from rm test"
+'
+
+test_expect_success '"rm" command suppressed with --quiet' '
+ echo frotz >test-file &&
+ git add test-file &&
+ git commit -m "add file for rm --quiet test" &&
+ git rm --quiet test-file >rm-output &&
+ test_must_be_empty rm-output &&
+ rm -f test-file rm-output &&
+ git commit -m "remove file from rm --quiet test"
+'
+
+# Now, failure cases.
+test_expect_success 'Re-add foo and baz' '
+ git add foo baz &&
+ git ls-files --error-unmatch foo baz
+'
+
+test_expect_success 'Modify foo -- rm should refuse' '
+ echo >>foo &&
+ test_must_fail git rm foo baz &&
+ test_path_is_file foo &&
+ test_path_is_file baz &&
+ git ls-files --error-unmatch foo baz
+'
+
+test_expect_success 'Modified foo -- rm -f should work' '
+ git rm -f foo baz &&
+ test_path_is_missing foo &&
+ test_path_is_missing baz &&
+ test_must_fail git ls-files --error-unmatch foo &&
+ test_must_fail git ls-files --error-unmatch bar
+'
+
+test_expect_success 'Re-add foo and baz for HEAD tests' '
+ echo frotz >foo &&
+ git checkout HEAD -- baz &&
+ git add foo baz &&
+ git ls-files --error-unmatch foo baz
+'
+
+test_expect_success 'foo is different in index from HEAD -- rm should refuse' '
+ test_must_fail git rm foo baz &&
+ test_path_is_file foo &&
+ test_path_is_file baz &&
+ git ls-files --error-unmatch foo baz
+'
+
+test_expect_success 'but with -f it should work.' '
+ git rm -f foo baz &&
+ test_path_is_missing foo &&
+ test_path_is_missing baz &&
+ test_must_fail git ls-files --error-unmatch foo &&
+ test_must_fail git ls-files --error-unmatch baz
+'
+
+test_expect_success 'refuse to remove cached empty file with modifications' '
+ >empty &&
+ git add empty &&
+ echo content >empty &&
+ test_must_fail git rm --cached empty
+'
+
+test_expect_success 'remove intent-to-add file without --force' '
+ echo content >intent-to-add &&
+ git add -N intent-to-add &&
+ git rm --cached intent-to-add
+'
+
+test_expect_success 'Recursive test setup' '
+ mkdir -p frotz &&
+ echo qfwfq >frotz/nitfol &&
+ git add frotz &&
+ git commit -m "subdir test"
+'
+
+test_expect_success 'Recursive without -r fails' '
+ test_must_fail git rm frotz &&
+ test_path_is_dir frotz &&
+ test_path_is_file frotz/nitfol
+'
+
+test_expect_success 'Recursive with -r but dirty' '
+ echo qfwfq >>frotz/nitfol &&
+ test_must_fail git rm -r frotz &&
+ test_path_is_dir frotz &&
+ test_path_is_file frotz/nitfol
+'
+
+test_expect_success 'Recursive with -r -f' '
+ git rm -f -r frotz &&
+ test_path_is_missing frotz/nitfol &&
+ test_path_is_missing frotz
+'
+
+test_expect_success 'Remove nonexistent file returns nonzero exit status' '
+ test_must_fail git rm nonexistent
+'
+
+test_expect_success 'Call "rm" from outside the work tree' '
+ mkdir repo &&
+ (
+ cd repo &&
+ git init &&
+ echo something >somefile &&
+ git add somefile &&
+ git commit -m "add a file" &&
+ (
+ cd .. &&
+ git --git-dir=repo/.git --work-tree=repo rm somefile
+ ) &&
+ test_must_fail git ls-files --error-unmatch somefile
+ )
+'
+
+test_expect_success 'refresh index before checking if it is up-to-date' '
+ git reset --hard &&
+ test-tool chmtime -86400 frotz/nitfol &&
+ git rm frotz/nitfol &&
+ test_path_is_missing frotz/nitfol
+'
+
+choke_git_rm_setup() {
+ git reset -q --hard &&
+ test_when_finished "rm -f .git/index.lock && git reset -q --hard" &&
+ i=0 &&
+ hash=$(test_oid deadbeef) &&
+ while test $i -lt 12000
+ do
+ echo "100644 $hash 0 some-file-$i"
+ i=$(( $i + 1 ))
+ done | git update-index --index-info
+}
+
+test_expect_success 'choking "git rm" should not let it die with cruft (induce SIGPIPE)' '
+ choke_git_rm_setup &&
+ # git command is intentionally placed upstream of pipe to induce SIGPIPE
+ git rm -n "some-file-*" | : &&
+ test_path_is_missing .git/index.lock
+'
+
+
+test_expect_success !MINGW 'choking "git rm" should not let it die with cruft (induce and check SIGPIPE)' '
+ choke_git_rm_setup &&
+ OUT=$( ((trap "" PIPE && git rm -n "some-file-*"; echo $? 1>&3) | :) 3>&1 ) &&
+ test_match_signal 13 "$OUT" &&
+ test_path_is_missing .git/index.lock
+'
+
+test_expect_success 'Resolving by removal is not a warning-worthy event' '
+ git reset -q --hard &&
+ test_when_finished "rm -f .git/index.lock msg && git reset -q --hard" &&
+ blob=$(echo blob | git hash-object -w --stdin) &&
+ printf "100644 $blob %d\tblob\n" 1 2 3 | git update-index --index-info &&
+ git rm blob >msg 2>&1 &&
+ test_grep ! "needs merge" msg &&
+ test_must_fail git ls-files -s --error-unmatch blob
+'
+
+test_expect_success 'rm removes subdirectories recursively' '
+ mkdir -p dir/subdir/subsubdir &&
+ echo content >dir/subdir/subsubdir/file &&
+ git add dir/subdir/subsubdir/file &&
+ git rm -f dir/subdir/subsubdir/file &&
+ test_path_is_missing dir
+'
+
+cat >expect <<EOF
+M .gitmodules
+D submod
+EOF
+
+cat >expect.modified <<EOF
+ M submod
+EOF
+
+cat >expect.modified_inside <<EOF
+ m submod
+EOF
+
+cat >expect.modified_untracked <<EOF
+ ? submod
+EOF
+
+cat >expect.cached <<EOF
+D submod
+EOF
+
+cat >expect.both_deleted<<EOF
+D .gitmodules
+D submod
+EOF
+
+test_expect_success 'rm removes empty submodules from work tree' '
+ mkdir submod &&
+ hash=$(git rev-parse HEAD) &&
+ git update-index --add --cacheinfo 160000 "$hash" submod &&
+ git config -f .gitmodules submodule.sub.url ./. &&
+ git config -f .gitmodules submodule.sub.path submod &&
+ git submodule init &&
+ git add .gitmodules &&
+ git commit -m "add submodule" &&
+ git rm submod &&
+ test_path_is_missing submod &&
+ git status -s -uno --ignore-submodules=none >actual &&
+ test_cmp expect actual &&
+ test_must_fail git config -f .gitmodules submodule.sub.url &&
+ test_must_fail git config -f .gitmodules submodule.sub.path
+'
+
+test_expect_success 'rm removes removed submodule from index and .gitmodules' '
+ git reset --hard &&
+ git -c protocol.file.allow=always submodule update &&
+ rm -rf submod &&
+ git rm submod &&
+ git status -s -uno --ignore-submodules=none >actual &&
+ test_cmp expect actual &&
+ test_must_fail git config -f .gitmodules submodule.sub.url &&
+ test_must_fail git config -f .gitmodules submodule.sub.path
+'
+
+test_expect_success 'rm removes work tree of unmodified submodules' '
+ git reset --hard &&
+ git submodule update &&
+ git rm submod &&
+ test_path_is_missing submod &&
+ git status -s -uno --ignore-submodules=none >actual &&
+ test_cmp expect actual &&
+ test_must_fail git config -f .gitmodules submodule.sub.url &&
+ test_must_fail git config -f .gitmodules submodule.sub.path
+'
+
+test_expect_success 'rm removes a submodule with a trailing /' '
+ git reset --hard &&
+ git submodule update &&
+ git rm submod/ &&
+ test_path_is_missing submod &&
+ git status -s -uno --ignore-submodules=none >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rm fails when given a file with a trailing /' '
+ test_must_fail git rm empty/
+'
+
+test_expect_success 'rm succeeds when given a directory with a trailing /' '
+ git rm -r frotz/
+'
+
+test_expect_success 'rm of a populated submodule with different HEAD fails unless forced' '
+ git reset --hard &&
+ git submodule update &&
+ git -C submod checkout HEAD^ &&
+ test_must_fail git rm submod &&
+ test_path_is_dir submod &&
+ test_path_is_file submod/.git &&
+ git status -s -uno --ignore-submodules=none >actual &&
+ test_cmp expect.modified actual &&
+ git rm -f submod &&
+ test_path_is_missing submod &&
+ git status -s -uno --ignore-submodules=none >actual &&
+ test_cmp expect actual &&
+ test_must_fail git config -f .gitmodules submodule.sub.url &&
+ test_must_fail git config -f .gitmodules submodule.sub.path
+'
+
+test_expect_success 'rm --cached leaves work tree of populated submodules and .gitmodules alone' '
+ git reset --hard &&
+ git submodule update &&
+ git rm --cached submod &&
+ test_path_is_dir submod &&
+ test_path_is_file submod/.git &&
+ git status -s -uno >actual &&
+ test_cmp expect.cached actual &&
+ git config -f .gitmodules submodule.sub.url &&
+ git config -f .gitmodules submodule.sub.path
+'
+
+test_expect_success 'rm --dry-run does not touch the submodule or .gitmodules' '
+ git reset --hard &&
+ git submodule update &&
+ git rm -n submod &&
+ test_path_is_file submod/.git &&
+ git diff-index --exit-code HEAD
+'
+
+test_expect_success 'rm does not complain when no .gitmodules file is found' '
+ git reset --hard &&
+ git submodule update &&
+ git rm .gitmodules &&
+ git rm submod >actual 2>actual.err &&
+ test_must_be_empty actual.err &&
+ test_path_is_missing submod &&
+ test_path_is_missing submod/.git &&
+ git status -s -uno >actual &&
+ test_cmp expect.both_deleted actual
+'
+
+test_expect_success 'rm will error out on a modified .gitmodules file unless staged' '
+ git reset --hard &&
+ git submodule update &&
+ git config -f .gitmodules foo.bar true &&
+ test_must_fail git rm submod >actual 2>actual.err &&
+ test_file_not_empty actual.err &&
+ test_path_is_dir submod &&
+ test_path_is_file submod/.git &&
+ git diff-files --quiet -- submod &&
+ git add .gitmodules &&
+ git rm submod >actual 2>actual.err &&
+ test_must_be_empty actual.err &&
+ test_path_is_missing submod &&
+ test_path_is_missing submod/.git &&
+ git status -s -uno >actual &&
+ test_cmp expect actual
+'
+test_expect_success 'rm will not error out on .gitmodules file with zero stat data' '
+ git reset --hard &&
+ git submodule update &&
+ git read-tree HEAD &&
+ git rm submod &&
+ test_path_is_missing submod
+'
+
+test_expect_success 'rm issues a warning when section is not found in .gitmodules' '
+ git reset --hard &&
+ git submodule update &&
+ git config -f .gitmodules --remove-section submodule.sub &&
+ git add .gitmodules &&
+ echo "warning: Could not find section in .gitmodules where path=submod" >expect.err &&
+ git rm submod >actual 2>actual.err &&
+ test_cmp expect.err actual.err &&
+ test_path_is_missing submod &&
+ test_path_is_missing submod/.git &&
+ git status -s -uno >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rm of a populated submodule with modifications fails unless forced' '
+ git reset --hard &&
+ git submodule update &&
+ echo X >submod/empty &&
+ test_must_fail git rm submod &&
+ test_path_is_dir submod &&
+ test_path_is_file submod/.git &&
+ git status -s -uno --ignore-submodules=none >actual &&
+ test_cmp expect.modified_inside actual &&
+ git rm -f submod &&
+ test_path_is_missing submod &&
+ git status -s -uno --ignore-submodules=none >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rm of a populated submodule with untracked files fails unless forced' '
+ git reset --hard &&
+ git submodule update &&
+ echo X >submod/untracked &&
+ test_must_fail git rm submod &&
+ test_path_is_dir submod &&
+ test_path_is_file submod/.git &&
+ git status -s -uno --ignore-submodules=none >actual &&
+ test_cmp expect.modified_untracked actual &&
+ git rm -f submod &&
+ test_path_is_missing submod &&
+ git status -s -uno --ignore-submodules=none >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'setup submodule conflict' '
+ git reset --hard &&
+ git submodule update &&
+ git checkout -b branch1 &&
+ echo 1 >nitfol &&
+ git add nitfol &&
+ git commit -m "added nitfol 1" &&
+ git checkout -b branch2 main &&
+ echo 2 >nitfol &&
+ git add nitfol &&
+ git commit -m "added nitfol 2" &&
+ git checkout -b conflict1 main &&
+ git -C submod fetch &&
+ git -C submod checkout branch1 &&
+ git add submod &&
+ git commit -m "submod 1" &&
+ git checkout -b conflict2 main &&
+ git -C submod checkout branch2 &&
+ git add submod &&
+ git commit -m "submod 2"
+'
+
+cat >expect.conflict <<EOF
+UU submod
+EOF
+
+test_expect_success 'rm removes work tree of unmodified conflicted submodule' '
+ git checkout conflict1 &&
+ git reset --hard &&
+ git submodule update &&
+ test_must_fail git merge conflict2 &&
+ git rm submod &&
+ test_path_is_missing submod &&
+ git status -s -uno --ignore-submodules=none >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rm of a conflicted populated submodule with different HEAD fails unless forced' '
+ git checkout conflict1 &&
+ git reset --hard &&
+ git submodule update &&
+ git -C submod checkout HEAD^ &&
+ test_must_fail git merge conflict2 &&
+ test_must_fail git rm submod &&
+ test_path_is_dir submod &&
+ test_path_is_file submod/.git &&
+ git status -s -uno --ignore-submodules=none >actual &&
+ test_cmp expect.conflict actual &&
+ git rm -f submod &&
+ test_path_is_missing submod &&
+ git status -s -uno --ignore-submodules=none >actual &&
+ test_cmp expect actual &&
+ test_must_fail git config -f .gitmodules submodule.sub.url &&
+ test_must_fail git config -f .gitmodules submodule.sub.path
+'
+
+test_expect_success 'rm of a conflicted populated submodule with modifications fails unless forced' '
+ git checkout conflict1 &&
+ git reset --hard &&
+ git submodule update &&
+ echo X >submod/empty &&
+ test_must_fail git merge conflict2 &&
+ test_must_fail git rm submod &&
+ test_path_is_dir submod &&
+ test_path_is_file submod/.git &&
+ git status -s -uno --ignore-submodules=none >actual &&
+ test_cmp expect.conflict actual &&
+ git rm -f submod &&
+ test_path_is_missing submod &&
+ git status -s -uno --ignore-submodules=none >actual &&
+ test_cmp expect actual &&
+ test_must_fail git config -f .gitmodules submodule.sub.url &&
+ test_must_fail git config -f .gitmodules submodule.sub.path
+'
+
+test_expect_success 'rm of a conflicted populated submodule with untracked files fails unless forced' '
+ git checkout conflict1 &&
+ git reset --hard &&
+ git submodule update &&
+ echo X >submod/untracked &&
+ test_must_fail git merge conflict2 &&
+ test_must_fail git rm submod &&
+ test_path_is_dir submod &&
+ test_path_is_file submod/.git &&
+ git status -s -uno --ignore-submodules=none >actual &&
+ test_cmp expect.conflict actual &&
+ git rm -f submod &&
+ test_path_is_missing submod &&
+ git status -s -uno --ignore-submodules=none >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rm of a conflicted populated submodule with a .git directory fails even when forced' '
+ git checkout conflict1 &&
+ git reset --hard &&
+ git submodule update &&
+ (
+ cd submod &&
+ rm .git &&
+ cp -R ../.git/modules/sub .git &&
+ GIT_WORK_TREE=. git config --unset core.worktree
+ ) &&
+ test_must_fail git merge conflict2 &&
+ test_must_fail git rm submod &&
+ test_path_is_dir submod &&
+ test_path_is_dir submod/.git &&
+ git status -s -uno --ignore-submodules=none >actual &&
+ test_cmp expect.conflict actual &&
+ test_must_fail git rm -f submod &&
+ test_path_is_dir submod &&
+ test_path_is_dir submod/.git &&
+ git status -s -uno --ignore-submodules=none >actual &&
+ test_cmp expect.conflict actual &&
+ git merge --abort &&
+ rm -rf submod
+'
+
+test_expect_success 'rm of a conflicted unpopulated submodule succeeds' '
+ git checkout conflict1 &&
+ git reset --hard &&
+ test_must_fail git merge conflict2 &&
+ git rm submod &&
+ test_path_is_missing submod &&
+ git status -s -uno --ignore-submodules=none >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rm of a populated submodule with a .git directory migrates git dir' '
+ git checkout -f main &&
+ git reset --hard &&
+ git submodule update &&
+ (
+ cd submod &&
+ rm .git &&
+ cp -R ../.git/modules/sub .git &&
+ GIT_WORK_TREE=. git config --unset core.worktree &&
+ rm -r ../.git/modules/sub
+ ) &&
+ git rm submod 2>output.err &&
+ test_path_is_missing submod &&
+ test_path_is_missing submod/.git &&
+ git status -s -uno --ignore-submodules=none >actual &&
+ test_file_not_empty actual &&
+ test_grep Migrating output.err
+'
+
+cat >expect.deepmodified <<EOF
+ M submod/subsubmod
+EOF
+
+test_expect_success 'setup subsubmodule' '
+ test_config_global protocol.file.allow always &&
+ git reset --hard &&
+ git submodule update &&
+ (
+ cd submod &&
+ hash=$(git rev-parse HEAD) &&
+ git update-index --add --cacheinfo 160000 "$hash" subsubmod &&
+ git config -f .gitmodules submodule.sub.url ../. &&
+ git config -f .gitmodules submodule.sub.path subsubmod &&
+ git submodule init &&
+ git add .gitmodules &&
+ git commit -m "add subsubmodule" &&
+ git submodule update subsubmod
+ ) &&
+ git commit -a -m "added deep submodule"
+'
+
+test_expect_success 'rm recursively removes work tree of unmodified submodules' '
+ git rm submod &&
+ test_path_is_missing submod &&
+ git status -s -uno --ignore-submodules=none >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rm of a populated nested submodule with different nested HEAD fails unless forced' '
+ git reset --hard &&
+ git submodule update --recursive &&
+ git -C submod/subsubmod checkout HEAD^ &&
+ test_must_fail git rm submod &&
+ test_path_is_dir submod &&
+ test_path_is_file submod/.git &&
+ git status -s -uno --ignore-submodules=none >actual &&
+ test_cmp expect.modified_inside actual &&
+ git rm -f submod &&
+ test_path_is_missing submod &&
+ git status -s -uno --ignore-submodules=none >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rm of a populated nested submodule with nested modifications fails unless forced' '
+ git reset --hard &&
+ git submodule update --recursive &&
+ echo X >submod/subsubmod/empty &&
+ test_must_fail git rm submod &&
+ test_path_is_dir submod &&
+ test_path_is_file submod/.git &&
+ git status -s -uno --ignore-submodules=none >actual &&
+ test_cmp expect.modified_inside actual &&
+ git rm -f submod &&
+ test_path_is_missing submod &&
+ git status -s -uno --ignore-submodules=none >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rm of a populated nested submodule with nested untracked files fails unless forced' '
+ git reset --hard &&
+ git submodule update --recursive &&
+ echo X >submod/subsubmod/untracked &&
+ test_must_fail git rm submod &&
+ test_path_is_dir submod &&
+ test_path_is_file submod/.git &&
+ git status -s -uno --ignore-submodules=none >actual &&
+ test_cmp expect.modified_untracked actual &&
+ git rm -f submod &&
+ test_path_is_missing submod &&
+ git status -s -uno --ignore-submodules=none >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success "rm absorbs submodule's nested .git directory" '
+ git reset --hard &&
+ git submodule update --recursive &&
+ (
+ cd submod/subsubmod &&
+ rm .git &&
+ mv ../../.git/modules/sub/modules/sub .git &&
+ GIT_WORK_TREE=. git config --unset core.worktree
+ ) &&
+ git rm submod 2>output.err &&
+ test_path_is_missing submod &&
+ test_path_is_missing submod/subsubmod/.git &&
+ git status -s -uno --ignore-submodules=none >actual &&
+ test_file_not_empty actual &&
+ test_grep Migrating output.err
+'
+
+test_expect_success 'checking out a commit after submodule removal needs manual updates' '
+ git commit -m "submodule removal" submod .gitmodules &&
+ git checkout HEAD^ &&
+ git submodule update &&
+ git checkout -q HEAD^ &&
+ git checkout -q main 2>actual &&
+ test_grep "^warning: unable to rmdir '\''submod'\'':" actual &&
+ git status -s submod >actual &&
+ echo "?? submod/" >expected &&
+ test_cmp expected actual &&
+ rm -rf submod &&
+ git status -s -uno --ignore-submodules=none >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'rm of d/f when d has become a non-directory' '
+ rm -rf d &&
+ mkdir d &&
+ >d/f &&
+ git add d &&
+ rm -rf d &&
+ >d &&
+ git rm d/f &&
+ test_must_fail git rev-parse --verify :d/f &&
+ test_path_is_file d
+'
+
+test_expect_success SYMLINKS 'rm of d/f when d has become a dangling symlink' '
+ rm -rf d &&
+ mkdir d &&
+ >d/f &&
+ git add d &&
+ rm -rf d &&
+ ln -s nonexistent d &&
+ git rm d/f &&
+ test_must_fail git rev-parse --verify :d/f &&
+ test -h d &&
+ test_path_is_missing d
+'
+
+test_expect_success 'rm of file when it has become a directory' '
+ rm -rf d &&
+ >d &&
+ git add d &&
+ rm -f d &&
+ mkdir d &&
+ >d/f &&
+ test_must_fail git rm d &&
+ git rev-parse --verify :d &&
+ test_path_is_file d/f
+'
+
+test_expect_success SYMLINKS 'rm across a symlinked leading path (no index)' '
+ rm -rf d e &&
+ mkdir e &&
+ echo content >e/f &&
+ ln -s e d &&
+ git add -A e d &&
+ git commit -m "symlink d to e, e/f exists" &&
+ test_must_fail git rm d/f &&
+ git rev-parse --verify :d &&
+ git rev-parse --verify :e/f &&
+ test -h d &&
+ test_path_is_file e/f
+'
+
+test_expect_failure SYMLINKS 'rm across a symlinked leading path (w/ index)' '
+ rm -rf d e &&
+ mkdir d &&
+ echo content >d/f &&
+ git add -A e d &&
+ git commit -m "d/f exists" &&
+ mv d e &&
+ ln -s e d &&
+ test_must_fail git rm d/f &&
+ git rev-parse --verify :d/f &&
+ test -h d &&
+ test_path_is_file e/f
+'
+
+test_expect_success 'setup for testing rm messages' '
+ >bar.txt &&
+ >foo.txt &&
+ git add bar.txt foo.txt
+'
+
+test_expect_success 'rm files with different staged content' '
+ cat >expect <<-\EOF &&
+ error: the following files have staged content different from both the
+ file and the HEAD:
+ bar.txt
+ foo.txt
+ (use -f to force removal)
+ EOF
+ echo content1 >foo.txt &&
+ echo content1 >bar.txt &&
+ test_must_fail git rm foo.txt bar.txt 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rm files with different staged content without hints' '
+ cat >expect <<-\EOF &&
+ error: the following files have staged content different from both the
+ file and the HEAD:
+ bar.txt
+ foo.txt
+ EOF
+ echo content2 >foo.txt &&
+ echo content2 >bar.txt &&
+ test_must_fail git -c advice.rmhints=false rm foo.txt bar.txt 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rm file with local modification' '
+ cat >expect <<-\EOF &&
+ error: the following file has local modifications:
+ foo.txt
+ (use --cached to keep the file, or -f to force removal)
+ EOF
+ git commit -m "testing rm 3" &&
+ echo content3 >foo.txt &&
+ test_must_fail git rm foo.txt 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rm file with local modification without hints' '
+ cat >expect <<-\EOF &&
+ error: the following file has local modifications:
+ bar.txt
+ EOF
+ echo content4 >bar.txt &&
+ test_must_fail git -c advice.rmhints=false rm bar.txt 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rm file with changes in the index' '
+ cat >expect <<-\EOF &&
+ error: the following file has changes staged in the index:
+ foo.txt
+ (use --cached to keep the file, or -f to force removal)
+ EOF
+ git reset --hard &&
+ echo content5 >foo.txt &&
+ git add foo.txt &&
+ test_must_fail git rm foo.txt 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rm file with changes in the index without hints' '
+ cat >expect <<-\EOF &&
+ error: the following file has changes staged in the index:
+ foo.txt
+ EOF
+ test_must_fail git -c advice.rmhints=false rm foo.txt 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rm files with two different errors' '
+ cat >expect <<-\EOF &&
+ error: the following file has staged content different from both the
+ file and the HEAD:
+ foo1.txt
+ (use -f to force removal)
+ error: the following file has changes staged in the index:
+ bar1.txt
+ (use --cached to keep the file, or -f to force removal)
+ EOF
+ echo content >foo1.txt &&
+ git add foo1.txt &&
+ echo content6 >foo1.txt &&
+ echo content6 >bar1.txt &&
+ git add bar1.txt &&
+ test_must_fail git rm bar1.txt foo1.txt 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rm empty string should fail' '
+ test_must_fail git rm -rf ""
+'
+
+test_done
diff --git a/t/t3601-rm-pathspec-file.sh b/t/t3601-rm-pathspec-file.sh
new file mode 100755
index 0000000..7cef129
--- /dev/null
+++ b/t/t3601-rm-pathspec-file.sh
@@ -0,0 +1,80 @@
+#!/bin/sh
+
+test_description='rm --pathspec-from-file'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_tick
+
+test_expect_success setup '
+ echo A >fileA.t &&
+ echo B >fileB.t &&
+ echo C >fileC.t &&
+ echo D >fileD.t &&
+ git add fileA.t fileB.t fileC.t fileD.t &&
+ git commit -m "files" &&
+
+ git tag checkpoint
+'
+
+restore_checkpoint () {
+ git reset --hard checkpoint
+}
+
+verify_expect () {
+ git status --porcelain --untracked-files=no -- fileA.t fileB.t fileC.t fileD.t >actual &&
+ test_cmp expect actual
+}
+
+test_expect_success 'simplest' '
+ restore_checkpoint &&
+
+ cat >expect <<-\EOF &&
+ D fileA.t
+ EOF
+
+ echo fileA.t | git rm --pathspec-from-file=- &&
+ verify_expect
+'
+
+test_expect_success '--pathspec-file-nul' '
+ restore_checkpoint &&
+
+ cat >expect <<-\EOF &&
+ D fileA.t
+ D fileB.t
+ EOF
+
+ printf "fileA.t\0fileB.t\0" | git rm --pathspec-from-file=- --pathspec-file-nul &&
+ verify_expect
+'
+
+test_expect_success 'only touches what was listed' '
+ restore_checkpoint &&
+
+ cat >expect <<-\EOF &&
+ D fileB.t
+ D fileC.t
+ EOF
+
+ printf "fileB.t\nfileC.t\n" | git rm --pathspec-from-file=- &&
+ verify_expect
+'
+
+test_expect_success 'error conditions' '
+ restore_checkpoint &&
+ echo fileA.t >list &&
+
+ test_must_fail git rm --pathspec-from-file=list -- fileA.t 2>err &&
+ test_grep -e ".--pathspec-from-file. and pathspec arguments cannot be used together" err &&
+
+ test_must_fail git rm --pathspec-file-nul 2>err &&
+ test_grep -e "the option .--pathspec-file-nul. requires .--pathspec-from-file." err &&
+
+ >empty_list &&
+ test_must_fail git rm --pathspec-from-file=empty_list 2>err &&
+ test_grep -e "No pathspec was given. Which files should I remove?" err
+'
+
+test_done
diff --git a/t/t3602-rm-sparse-checkout.sh b/t/t3602-rm-sparse-checkout.sh
new file mode 100755
index 0000000..08580fd
--- /dev/null
+++ b/t/t3602-rm-sparse-checkout.sh
@@ -0,0 +1,139 @@
+#!/bin/sh
+
+test_description='git rm in sparse checked out working trees'
+
+. ./test-lib.sh
+
+test_expect_success 'setup' "
+ mkdir -p sub/dir &&
+ touch a b c sub/d sub/dir/e &&
+ git add -A &&
+ git commit -m files &&
+
+ cat >sparse_error_header <<-EOF &&
+ The following paths and/or pathspecs matched paths that exist
+ outside of your sparse-checkout definition, so will not be
+ updated in the index:
+ EOF
+
+ cat >sparse_hint <<-EOF &&
+ hint: If you intend to update such entries, try one of the following:
+ hint: * Use the --sparse option.
+ hint: * Disable or modify the sparsity rules.
+ hint: Disable this message with \"git config advice.updateSparsePath false\"
+ EOF
+
+ echo b | cat sparse_error_header - >sparse_entry_b_error &&
+ cat sparse_entry_b_error sparse_hint >b_error_and_hint
+"
+
+for opt in "" -f --dry-run
+do
+ test_expect_success "rm${opt:+ $opt} does not remove sparse entries" '
+ git sparse-checkout set --no-cone a &&
+ test_must_fail git rm $opt b 2>stderr &&
+ test_cmp b_error_and_hint stderr &&
+ git ls-files --error-unmatch b
+ '
+done
+
+test_expect_success 'recursive rm does not remove sparse entries' '
+ git reset --hard &&
+ git sparse-checkout set sub/dir &&
+ git rm -r sub &&
+ git status --porcelain -uno >actual &&
+ cat >expected <<-\EOF &&
+ D sub/dir/e
+ EOF
+ test_cmp expected actual &&
+
+ git rm --sparse -r sub &&
+ git status --porcelain -uno >actual2 &&
+ cat >expected2 <<-\EOF &&
+ D sub/d
+ D sub/dir/e
+ EOF
+ test_cmp expected2 actual2
+'
+
+test_expect_success 'recursive rm --sparse removes sparse entries' '
+ git reset --hard &&
+ git sparse-checkout set "sub/dir" &&
+ git rm --sparse -r sub &&
+ git status --porcelain -uno >actual &&
+ cat >expected <<-\EOF &&
+ D sub/d
+ D sub/dir/e
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'rm obeys advice.updateSparsePath' '
+ git reset --hard &&
+ git sparse-checkout set a &&
+ test_must_fail git -c advice.updateSparsePath=false rm b 2>stderr &&
+ test_cmp sparse_entry_b_error stderr
+'
+
+test_expect_success 'do not advice about sparse entries when they do not match the pathspec' '
+ git reset --hard &&
+ git sparse-checkout set a &&
+ test_must_fail git rm nonexistent 2>stderr &&
+ grep "fatal: pathspec .nonexistent. did not match any files" stderr &&
+ ! grep -F -f sparse_error_header stderr
+'
+
+test_expect_success 'do not warn about sparse entries when pathspec matches dense entries' '
+ git reset --hard &&
+ git sparse-checkout set a &&
+ git rm "[ba]" 2>stderr &&
+ test_must_be_empty stderr &&
+ git ls-files --error-unmatch b &&
+ test_must_fail git ls-files --error-unmatch a
+'
+
+test_expect_success 'do not warn about sparse entries with --ignore-unmatch' '
+ git reset --hard &&
+ git sparse-checkout set a &&
+ git rm --ignore-unmatch b 2>stderr &&
+ test_must_be_empty stderr &&
+ git ls-files --error-unmatch b
+'
+
+test_expect_success 'refuse to rm a non-skip-worktree path outside sparse cone' '
+ git reset --hard &&
+ git sparse-checkout set a &&
+ git update-index --no-skip-worktree b &&
+ test_must_fail git rm b 2>stderr &&
+ test_cmp b_error_and_hint stderr &&
+ git rm --sparse b 2>stderr &&
+ test_must_be_empty stderr &&
+ test_path_is_missing b
+'
+
+test_expect_success 'can remove files from non-sparse dir' '
+ git reset --hard &&
+ git sparse-checkout disable &&
+ mkdir -p w x/y &&
+ test_commit w/f &&
+ test_commit x/y/f &&
+
+ git sparse-checkout set --no-cone w !/x y/ &&
+ git rm w/f.t x/y/f.t 2>stderr &&
+ test_must_be_empty stderr
+'
+
+test_expect_success 'refuse to remove non-skip-worktree file from sparse dir' '
+ git reset --hard &&
+ git sparse-checkout disable &&
+ mkdir -p x/y/z &&
+ test_commit x/y/z/f &&
+ git sparse-checkout set --no-cone !/x y/ !x/y/z &&
+
+ git update-index --no-skip-worktree x/y/z/f.t &&
+ test_must_fail git rm x/y/z/f.t 2>stderr &&
+ echo x/y/z/f.t | cat sparse_error_header - sparse_hint >expect &&
+ test_cmp expect stderr
+'
+
+test_done
diff --git a/t/t3700-add.sh b/t/t3700-add.sh
new file mode 100755
index 0000000..f23d39f
--- /dev/null
+++ b/t/t3700-add.sh
@@ -0,0 +1,509 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Carl D. Worth
+#
+
+test_description='Test of git add, including the -- option.'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+. "$TEST_DIRECTORY"/lib-unique-files.sh
+
+# Test the file mode "$1" of the file "$2" in the index.
+test_mode_in_index () {
+ case "$(git ls-files -s "$2")" in
+ "$1 "*" $2")
+ echo pass
+ ;;
+ *)
+ echo fail
+ git ls-files -s "$2"
+ return 1
+ ;;
+ esac
+}
+
+test_expect_success 'Test of git add' '
+ touch foo && git add foo
+'
+
+test_expect_success 'Post-check that foo is in the index' '
+ git ls-files foo | grep foo
+'
+
+test_expect_success 'Test that "git add -- -q" works' '
+ touch -- -q && git add -- -q
+'
+
+BATCH_CONFIGURATION='-c core.fsync=loose-object -c core.fsyncmethod=batch'
+
+test_expect_success 'git add: core.fsyncmethod=batch' "
+ test_create_unique_files 2 4 files_base_dir1 &&
+ GIT_TEST_FSYNC=1 git $BATCH_CONFIGURATION add -- ./files_base_dir1/ &&
+ git ls-files --stage files_base_dir1/ |
+ test_parse_ls_files_stage_oids >added_files_oids &&
+
+ # We created 2 subdirs with 4 files each (8 files total) above
+ test_line_count = 8 added_files_oids &&
+ git cat-file --batch-check='%(objectname)' <added_files_oids >added_files_actual &&
+ test_cmp added_files_oids added_files_actual
+"
+
+test_expect_success 'git update-index: core.fsyncmethod=batch' "
+ test_create_unique_files 2 4 files_base_dir2 &&
+ find files_base_dir2 ! -type d -print | xargs git $BATCH_CONFIGURATION update-index --add -- &&
+ git ls-files --stage files_base_dir2 |
+ test_parse_ls_files_stage_oids >added_files2_oids &&
+
+ # We created 2 subdirs with 4 files each (8 files total) above
+ test_line_count = 8 added_files2_oids &&
+ git cat-file --batch-check='%(objectname)' <added_files2_oids >added_files2_actual &&
+ test_cmp added_files2_oids added_files2_actual
+"
+
+test_expect_success \
+ 'git add: Test that executable bit is not used if core.filemode=0' \
+ 'git config core.filemode 0 &&
+ echo foo >xfoo1 &&
+ chmod 755 xfoo1 &&
+ git add xfoo1 &&
+ test_mode_in_index 100644 xfoo1'
+
+test_expect_success 'git add: filemode=0 should not get confused by symlink' '
+ rm -f xfoo1 &&
+ test_ln_s_add foo xfoo1 &&
+ test_mode_in_index 120000 xfoo1
+'
+
+test_expect_success \
+ 'git update-index --add: Test that executable bit is not used...' \
+ 'git config core.filemode 0 &&
+ echo foo >xfoo2 &&
+ chmod 755 xfoo2 &&
+ git update-index --add xfoo2 &&
+ test_mode_in_index 100644 xfoo2'
+
+test_expect_success 'git add: filemode=0 should not get confused by symlink' '
+ rm -f xfoo2 &&
+ test_ln_s_add foo xfoo2 &&
+ test_mode_in_index 120000 xfoo2
+'
+
+test_expect_success \
+ 'git update-index --add: Test that executable bit is not used...' \
+ 'git config core.filemode 0 &&
+ test_ln_s_add xfoo2 xfoo3 && # runs git update-index --add
+ test_mode_in_index 120000 xfoo3'
+
+test_expect_success '.gitignore test setup' '
+ echo "*.ig" >.gitignore &&
+ mkdir c.if d.ig &&
+ >a.ig && >b.if &&
+ >c.if/c.if && >c.if/c.ig &&
+ >d.ig/d.if && >d.ig/d.ig
+'
+
+test_expect_success '.gitignore is honored' '
+ git add . &&
+ git ls-files >files &&
+ sed -n "/\\.ig/p" <files >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'error out when attempting to add ignored ones without -f' '
+ test_must_fail git add a.?? &&
+ git ls-files >files &&
+ sed -n "/\\.ig/p" <files >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'error out when attempting to add ignored ones without -f' '
+ test_must_fail git add d.?? &&
+ git ls-files >files &&
+ sed -n "/\\.ig/p" <files >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'error out when attempting to add ignored ones but add others' '
+ touch a.if &&
+ test_must_fail git add a.?? &&
+ git ls-files >files &&
+ sed -n "/\\.ig/p" <files >actual &&
+ test_must_be_empty actual &&
+ grep a.if files
+'
+
+test_expect_success 'add ignored ones with -f' '
+ git add -f a.?? &&
+ git ls-files --error-unmatch a.ig
+'
+
+test_expect_success 'add ignored ones with -f' '
+ git add -f d.??/* &&
+ git ls-files --error-unmatch d.ig/d.if d.ig/d.ig
+'
+
+test_expect_success 'add ignored ones with -f' '
+ rm -f .git/index &&
+ git add -f d.?? &&
+ git ls-files --error-unmatch d.ig/d.if d.ig/d.ig
+'
+
+test_expect_success '.gitignore with subdirectory' '
+
+ rm -f .git/index &&
+ mkdir -p sub/dir &&
+ echo "!dir/a.*" >sub/.gitignore &&
+ >sub/a.ig &&
+ >sub/dir/a.ig &&
+ git add sub/dir &&
+ git ls-files --error-unmatch sub/dir/a.ig &&
+ rm -f .git/index &&
+ (
+ cd sub/dir &&
+ git add .
+ ) &&
+ git ls-files --error-unmatch sub/dir/a.ig
+'
+
+mkdir 1 1/2 1/3
+touch 1/2/a 1/3/b 1/2/c
+test_expect_success 'check correct prefix detection' '
+ rm -f .git/index &&
+ git add 1/2/a 1/3/b 1/2/c
+'
+
+test_expect_success 'git add with filemode=0, symlinks=0, and unmerged entries' '
+ for s in 1 2 3
+ do
+ echo $s > stage$s &&
+ echo "100755 $(git hash-object -w stage$s) $s file" &&
+ echo "120000 $(printf $s | git hash-object -w -t blob --stdin) $s symlink" || return 1
+ done | git update-index --index-info &&
+ git config core.filemode 0 &&
+ git config core.symlinks 0 &&
+ echo new > file &&
+ echo new > symlink &&
+ git add file symlink &&
+ git ls-files --stage | grep "^100755 .* 0 file$" &&
+ git ls-files --stage | grep "^120000 .* 0 symlink$"
+'
+
+test_expect_success 'git add with filemode=0, symlinks=0 prefers stage 2 over stage 1' '
+ git rm --cached -f file symlink &&
+ (
+ echo "100644 $(git hash-object -w stage1) 1 file" &&
+ echo "100755 $(git hash-object -w stage2) 2 file" &&
+ echo "100644 $(printf 1 | git hash-object -w -t blob --stdin) 1 symlink" &&
+ echo "120000 $(printf 2 | git hash-object -w -t blob --stdin) 2 symlink"
+ ) | git update-index --index-info &&
+ git config core.filemode 0 &&
+ git config core.symlinks 0 &&
+ echo new > file &&
+ echo new > symlink &&
+ git add file symlink &&
+ git ls-files --stage | grep "^100755 .* 0 file$" &&
+ git ls-files --stage | grep "^120000 .* 0 symlink$"
+'
+
+test_expect_success 'git add --refresh' '
+ >foo && git add foo && git commit -a -m "commit all" &&
+ test -z "$(git diff-index HEAD -- foo)" &&
+ git read-tree HEAD &&
+ case "$(git diff-index HEAD -- foo)" in
+ :100644" "*"M foo") echo pass;;
+ *) echo fail; false;;
+ esac &&
+ git add --refresh -- foo &&
+ test -z "$(git diff-index HEAD -- foo)"
+'
+
+test_expect_success 'git add --refresh with pathspec' '
+ git reset --hard &&
+ echo >foo && echo >bar && echo >baz &&
+ git add foo bar baz && H=$(git rev-parse :foo) && git rm -f foo &&
+ echo "100644 $H 3 foo" | git update-index --index-info &&
+ test-tool chmtime -60 bar baz &&
+ git add --refresh bar >actual &&
+ test_must_be_empty actual &&
+
+ git diff-files --name-only >actual &&
+ ! grep bar actual &&
+ grep baz actual
+'
+
+test_expect_success 'git add --refresh correctly reports no match error' "
+ echo \"fatal: pathspec ':(icase)nonexistent' did not match any files\" >expect &&
+ test_must_fail git add --refresh ':(icase)nonexistent' 2>actual &&
+ test_cmp expect actual
+"
+
+test_expect_success POSIXPERM,SANITY 'git add should fail atomically upon an unreadable file' '
+ git reset --hard &&
+ date >foo1 &&
+ date >foo2 &&
+ chmod 0 foo2 &&
+ test_must_fail git add --verbose . &&
+ ! ( git ls-files foo1 | grep foo1 )
+'
+
+rm -f foo2
+
+test_expect_success POSIXPERM,SANITY 'git add --ignore-errors' '
+ git reset --hard &&
+ date >foo1 &&
+ date >foo2 &&
+ chmod 0 foo2 &&
+ test_must_fail git add --verbose --ignore-errors . &&
+ git ls-files foo1 | grep foo1
+'
+
+rm -f foo2
+
+test_expect_success POSIXPERM,SANITY 'git add (add.ignore-errors)' '
+ git config add.ignore-errors 1 &&
+ git reset --hard &&
+ date >foo1 &&
+ date >foo2 &&
+ chmod 0 foo2 &&
+ test_must_fail git add --verbose . &&
+ git ls-files foo1 | grep foo1
+'
+rm -f foo2
+
+test_expect_success POSIXPERM,SANITY 'git add (add.ignore-errors = false)' '
+ git config add.ignore-errors 0 &&
+ git reset --hard &&
+ date >foo1 &&
+ date >foo2 &&
+ chmod 0 foo2 &&
+ test_must_fail git add --verbose . &&
+ ! ( git ls-files foo1 | grep foo1 )
+'
+rm -f foo2
+
+test_expect_success POSIXPERM,SANITY '--no-ignore-errors overrides config' '
+ git config add.ignore-errors 1 &&
+ git reset --hard &&
+ date >foo1 &&
+ date >foo2 &&
+ chmod 0 foo2 &&
+ test_must_fail git add --verbose --no-ignore-errors . &&
+ ! ( git ls-files foo1 | grep foo1 ) &&
+ git config add.ignore-errors 0
+'
+rm -f foo2
+
+test_expect_success BSLASHPSPEC "git add 'fo\\[ou\\]bar' ignores foobar" '
+ git reset --hard &&
+ touch fo\[ou\]bar foobar &&
+ git add '\''fo\[ou\]bar'\'' &&
+ git ls-files fo\[ou\]bar | grep -F fo\[ou\]bar &&
+ ! ( git ls-files foobar | grep foobar )
+'
+
+test_expect_success 'git add to resolve conflicts on otherwise ignored path' '
+ git reset --hard &&
+ H=$(git rev-parse :1/2/a) &&
+ (
+ echo "100644 $H 1 track-this" &&
+ echo "100644 $H 3 track-this"
+ ) | git update-index --index-info &&
+ echo track-this >>.gitignore &&
+ echo resolved >track-this &&
+ git add track-this
+'
+
+test_expect_success '"add non-existent" should fail' '
+ test_must_fail git add non-existent &&
+ ! (git ls-files | grep "non-existent")
+'
+
+test_expect_success 'git add -A on empty repo does not error out' '
+ rm -fr empty &&
+ git init empty &&
+ (
+ cd empty &&
+ git add -A . &&
+ git add -A
+ )
+'
+
+test_expect_success '"git add ." in empty repo' '
+ rm -fr empty &&
+ git init empty &&
+ (
+ cd empty &&
+ git add .
+ )
+'
+
+test_expect_success 'error on a repository with no commits' '
+ rm -fr empty &&
+ git init empty &&
+ test_must_fail git add empty >actual 2>&1 &&
+ cat >expect <<-EOF &&
+ error: '"'empty/'"' does not have a commit checked out
+ fatal: adding files failed
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'git add --dry-run of existing changed file' "
+ echo new >>track-this &&
+ git add --dry-run track-this >actual 2>&1 &&
+ echo \"add 'track-this'\" | test_cmp - actual
+"
+
+test_expect_success 'git add --dry-run of non-existing file' "
+ echo ignored-file >>.gitignore &&
+ test_must_fail git add --dry-run track-this ignored-file >actual 2>&1
+"
+
+test_expect_success 'git add --dry-run of an existing file output' "
+ echo \"fatal: pathspec 'ignored-file' did not match any files\" >expect &&
+ test_cmp expect actual
+"
+
+cat >expect.err <<\EOF
+The following paths are ignored by one of your .gitignore files:
+ignored-file
+hint: Use -f if you really want to add them.
+hint: Turn this message off by running
+hint: "git config advice.addIgnoredFile false"
+EOF
+cat >expect.out <<\EOF
+add 'track-this'
+EOF
+
+test_expect_success 'git add --dry-run --ignore-missing of non-existing file' '
+ test_must_fail git add --dry-run --ignore-missing track-this ignored-file >actual.out 2>actual.err
+'
+
+test_expect_success 'git add --dry-run --ignore-missing of non-existing file output' '
+ test_cmp expect.out actual.out &&
+ test_cmp expect.err actual.err
+'
+
+test_expect_success 'git add --dry-run --interactive should fail' '
+ test_must_fail git add --dry-run --interactive
+'
+
+test_expect_success 'git add empty string should fail' '
+ test_must_fail git add ""
+'
+
+test_expect_success 'git add --chmod=[+-]x stages correctly' '
+ rm -f foo1 &&
+ echo foo >foo1 &&
+ git add --chmod=+x foo1 &&
+ test_mode_in_index 100755 foo1 &&
+ git add --chmod=-x foo1 &&
+ test_mode_in_index 100644 foo1
+'
+
+test_expect_success POSIXPERM,SYMLINKS 'git add --chmod=+x with symlinks' '
+ git config core.filemode 1 &&
+ git config core.symlinks 1 &&
+ rm -f foo2 &&
+ echo foo >foo2 &&
+ git add --chmod=+x foo2 &&
+ test_mode_in_index 100755 foo2
+'
+
+test_expect_success 'git add --chmod=[+-]x changes index with already added file' '
+ rm -f foo3 xfoo3 &&
+ git reset --hard &&
+ echo foo >foo3 &&
+ git add foo3 &&
+ git add --chmod=+x foo3 &&
+ test_mode_in_index 100755 foo3 &&
+ echo foo >xfoo3 &&
+ chmod 755 xfoo3 &&
+ git add xfoo3 &&
+ git add --chmod=-x xfoo3 &&
+ test_mode_in_index 100644 xfoo3
+'
+
+test_expect_success POSIXPERM 'git add --chmod=[+-]x does not change the working tree' '
+ echo foo >foo4 &&
+ git add foo4 &&
+ git add --chmod=+x foo4 &&
+ ! test -x foo4
+'
+
+test_expect_success 'git add --chmod fails with non regular files (but updates the other paths)' '
+ git reset --hard &&
+ test_ln_s_add foo foo3 &&
+ touch foo4 &&
+ test_must_fail git add --chmod=+x foo3 foo4 2>stderr &&
+ test_grep "cannot chmod +x .foo3." stderr &&
+ test_mode_in_index 120000 foo3 &&
+ test_mode_in_index 100755 foo4
+'
+
+test_expect_success 'git add --chmod honors --dry-run' '
+ git reset --hard &&
+ echo foo >foo4 &&
+ git add foo4 &&
+ git add --chmod=+x --dry-run foo4 &&
+ test_mode_in_index 100644 foo4
+'
+
+test_expect_success 'git add --chmod --dry-run reports error for non regular files' '
+ git reset --hard &&
+ test_ln_s_add foo foo4 &&
+ test_must_fail git add --chmod=+x --dry-run foo4 2>stderr &&
+ test_grep "cannot chmod +x .foo4." stderr
+'
+
+test_expect_success 'git add --chmod --dry-run reports error for unmatched pathspec' '
+ test_must_fail git add --chmod=+x --dry-run nonexistent 2>stderr &&
+ test_grep "pathspec .nonexistent. did not match any files" stderr
+'
+
+test_expect_success 'no file status change if no pathspec is given' '
+ >foo5 &&
+ >foo6 &&
+ git add foo5 foo6 &&
+ git add --chmod=+x &&
+ test_mode_in_index 100644 foo5 &&
+ test_mode_in_index 100644 foo6
+'
+
+test_expect_success 'no file status change if no pathspec is given in subdir' '
+ mkdir -p sub &&
+ (
+ cd sub &&
+ >sub-foo1 &&
+ >sub-foo2 &&
+ git add . &&
+ git add --chmod=+x &&
+ test_mode_in_index 100644 sub-foo1 &&
+ test_mode_in_index 100644 sub-foo2
+ )
+'
+
+test_expect_success 'all statuses changed in folder if . is given' '
+ git init repo &&
+ (
+ cd repo &&
+ mkdir -p sub/dir &&
+ touch x y z sub/a sub/dir/b &&
+ git add -A &&
+ git add --chmod=+x . &&
+ test $(git ls-files --stage | grep ^100644 | wc -l) -eq 0 &&
+ git add --chmod=-x . &&
+ test $(git ls-files --stage | grep ^100755 | wc -l) -eq 0
+ )
+'
+
+test_expect_success CASE_INSENSITIVE_FS 'path is case-insensitive' '
+ path="$(pwd)/BLUB" &&
+ touch "$path" &&
+ downcased="$(echo "$path" | tr A-Z a-z)" &&
+ git add "$downcased"
+'
+
+test_done
diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh
new file mode 100755
index 0000000..0b5339a
--- /dev/null
+++ b/t/t3701-add-interactive.sh
@@ -0,0 +1,1133 @@
+#!/bin/sh
+
+test_description='add -i basic tests'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-terminal.sh
+
+diff_cmp () {
+ for x
+ do
+ sed -e '/^index/s/[0-9a-f]*[1-9a-f][0-9a-f]*\.\./1234567../' \
+ -e '/^index/s/\.\.[0-9a-f]*[1-9a-f][0-9a-f]*/..9abcdef/' \
+ -e '/^index/s/ 00*\.\./ 0000000../' \
+ -e '/^index/s/\.\.00*$/..0000000/' \
+ -e '/^index/s/\.\.00* /..0000000 /' \
+ "$x" >"$x.filtered"
+ done
+ test_cmp "$1.filtered" "$2.filtered"
+}
+
+# This function uses a trick to manipulate the interactive add to use color:
+# the `want_color()` function special-cases the situation where a pager was
+# spawned and Git now wants to output colored text: to detect that situation,
+# the environment variable `GIT_PAGER_IN_USE` is set. However, color is
+# suppressed despite that environment variable if the `TERM` variable
+# indicates a dumb terminal, so we set that variable, too.
+
+force_color () {
+ # The first element of $@ may be a shell function, as a result POSIX
+ # does not guarantee that "one-shot assignment" will not persist after
+ # the function call. Thus, we prevent these variables from escaping
+ # this function's context with this subshell.
+ (
+ GIT_PAGER_IN_USE=true &&
+ TERM=vt100 &&
+ export GIT_PAGER_IN_USE TERM &&
+ "$@"
+ )
+}
+
+test_expect_success 'warn about add.interactive.useBuiltin' '
+ cat >expect <<-\EOF &&
+ warning: the add.interactive.useBuiltin setting has been removed!
+ See its entry in '\''git help config'\'' for details.
+ No changes.
+ EOF
+
+ for v in = =true =false
+ do
+ git -c "add.interactive.useBuiltin$v" add -p >out 2>actual &&
+ test_must_be_empty out &&
+ test_cmp expect actual || return 1
+ done
+'
+
+test_expect_success 'setup (initial)' '
+ echo content >file &&
+ git add file &&
+ echo more >>file &&
+ echo lines >>file
+'
+test_expect_success 'status works (initial)' '
+ git add -i </dev/null >output &&
+ grep "+1/-0 *+2/-0 file" output
+'
+
+test_expect_success 'setup expected' '
+ cat >expected <<-\EOF
+ new file mode 100644
+ index 0000000..d95f3ad
+ --- /dev/null
+ +++ b/file
+ @@ -0,0 +1 @@
+ +content
+ EOF
+'
+
+test_expect_success 'diff works (initial)' '
+ test_write_lines d 1 | git add -i >output &&
+ sed -ne "/new file/,/content/p" <output >diff &&
+ diff_cmp expected diff
+'
+test_expect_success 'revert works (initial)' '
+ git add file &&
+ test_write_lines r 1 | git add -i &&
+ git ls-files >output &&
+ ! grep . output
+'
+
+test_expect_success 'add untracked (multiple)' '
+ test_when_finished "git reset && rm [1-9]" &&
+ touch $(test_seq 9) &&
+ test_write_lines a "2-5 8-" | git add -i -- [1-9] &&
+ test_write_lines 2 3 4 5 8 9 >expected &&
+ git ls-files [1-9] >output &&
+ test_cmp expected output
+'
+
+test_expect_success 'setup (commit)' '
+ echo baseline >file &&
+ git add file &&
+ git commit -m commit &&
+ echo content >>file &&
+ git add file &&
+ echo more >>file &&
+ echo lines >>file
+'
+test_expect_success 'status works (commit)' '
+ git add -i </dev/null >output &&
+ grep "+1/-0 *+2/-0 file" output
+'
+
+test_expect_success 'update can stage deletions' '
+ >to-delete &&
+ git add to-delete &&
+ rm to-delete &&
+ test_write_lines u t "" | git add -i &&
+ git ls-files to-delete >output &&
+ test_must_be_empty output
+'
+
+test_expect_success 'setup expected' '
+ cat >expected <<-\EOF
+ index 180b47c..b6f2c08 100644
+ --- a/file
+ +++ b/file
+ @@ -1 +1,2 @@
+ baseline
+ +content
+ EOF
+'
+
+test_expect_success 'diff works (commit)' '
+ test_write_lines d 1 | git add -i >output &&
+ sed -ne "/^index/,/content/p" <output >diff &&
+ diff_cmp expected diff
+'
+test_expect_success 'revert works (commit)' '
+ git add file &&
+ test_write_lines r 1 | git add -i &&
+ git add -i </dev/null >output &&
+ grep "unchanged *+3/-0 file" output
+'
+
+test_expect_success 'setup expected' '
+ cat >expected <<-\EOF
+ EOF
+'
+
+test_expect_success 'dummy edit works' '
+ test_set_editor : &&
+ test_write_lines e a | git add -p &&
+ git diff > diff &&
+ diff_cmp expected diff
+'
+
+test_expect_success 'setup patch' '
+ cat >patch <<-\EOF
+ @@ -1,1 +1,4 @@
+ this
+ +patch
+ -does not
+ apply
+ EOF
+'
+
+test_expect_success 'setup fake editor' '
+ write_script "fake_editor.sh" <<-\EOF &&
+ mv -f "$1" oldpatch &&
+ mv -f patch "$1"
+ EOF
+ test_set_editor "$(pwd)/fake_editor.sh"
+'
+
+test_expect_success 'bad edit rejected' '
+ git reset &&
+ test_write_lines e n d | git add -p >output &&
+ grep "hunk does not apply" output
+'
+
+test_expect_success 'setup patch' '
+ cat >patch <<-\EOF
+ this patch
+ is garbage
+ EOF
+'
+
+test_expect_success 'garbage edit rejected' '
+ git reset &&
+ test_write_lines e n d | git add -p >output &&
+ grep "hunk does not apply" output
+'
+
+test_expect_success 'setup patch' '
+ cat >patch <<-\EOF
+ @@ -1,0 +1,0 @@
+ baseline
+ +content
+ +newcontent
+ +lines
+ EOF
+'
+
+test_expect_success 'setup expected' '
+ cat >expected <<-\EOF
+ diff --git a/file b/file
+ index b5dd6c9..f910ae9 100644
+ --- a/file
+ +++ b/file
+ @@ -1,4 +1,4 @@
+ baseline
+ content
+ -newcontent
+ +more
+ lines
+ EOF
+'
+
+test_expect_success 'real edit works' '
+ test_write_lines e n d | git add -p &&
+ git diff >output &&
+ diff_cmp expected output
+'
+
+test_expect_success 'setup file' '
+ test_write_lines a "" b "" c >file &&
+ git add file &&
+ test_write_lines a "" d "" c >file
+'
+
+test_expect_success 'setup patch' '
+ SP=" " &&
+ NULL="" &&
+ cat >patch <<-EOF
+ @@ -1,4 +1,4 @@
+ a
+ $NULL
+ -b
+ +f
+ $SP
+ c
+ EOF
+'
+
+test_expect_success 'setup expected' '
+ cat >expected <<-EOF
+ diff --git a/file b/file
+ index b5dd6c9..f910ae9 100644
+ --- a/file
+ +++ b/file
+ @@ -1,5 +1,5 @@
+ a
+ $SP
+ -f
+ +d
+ $SP
+ c
+ EOF
+'
+
+test_expect_success 'edit can strip spaces from empty context lines' '
+ test_write_lines e n q | git add -p 2>error &&
+ test_must_be_empty error &&
+ git diff >output &&
+ diff_cmp expected output
+'
+
+test_expect_success 'skip files similarly as commit -a' '
+ git reset &&
+ echo file >.gitignore &&
+ echo changed >file &&
+ echo y | git add -p file &&
+ git diff >output &&
+ git reset &&
+ git commit -am commit &&
+ git diff >expected &&
+ diff_cmp expected output &&
+ git reset --hard HEAD^
+'
+rm -f .gitignore
+
+test_expect_success FILEMODE 'patch does not affect mode' '
+ git reset --hard &&
+ echo content >>file &&
+ chmod +x file &&
+ printf "n\\ny\\n" | git add -p &&
+ git show :file | grep content &&
+ git diff file | grep "new mode"
+'
+
+test_expect_success FILEMODE 'stage mode but not hunk' '
+ git reset --hard &&
+ echo content >>file &&
+ chmod +x file &&
+ printf "y\\nn\\n" | git add -p &&
+ git diff --cached file | grep "new mode" &&
+ git diff file | grep "+content"
+'
+
+
+test_expect_success FILEMODE 'stage mode and hunk' '
+ git reset --hard &&
+ echo content >>file &&
+ chmod +x file &&
+ printf "y\\ny\\n" | git add -p &&
+ git diff --cached file >out &&
+ grep "new mode" out &&
+ grep "+content" out &&
+ git diff file >out &&
+ test_must_be_empty out
+'
+
+# end of tests disabled when filemode is not usable
+
+test_expect_success 'different prompts for mode change/deleted' '
+ git reset --hard &&
+ >file &&
+ >deleted &&
+ git add --chmod=+x file deleted &&
+ echo changed >file &&
+ rm deleted &&
+ test_write_lines n n n |
+ git -c core.filemode=true add -p >actual &&
+ sed -n "s/^\(([0-9/]*) Stage .*?\).*/\1/p" actual >actual.filtered &&
+ cat >expect <<-\EOF &&
+ (1/1) Stage deletion [y,n,q,a,d,?]?
+ (1/2) Stage mode change [y,n,q,a,d,j,J,g,/,?]?
+ (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,?]?
+ EOF
+ test_cmp expect actual.filtered
+'
+
+test_expect_success 'correct message when there is nothing to do' '
+ git reset --hard &&
+ git add -p 2>err &&
+ test_grep "No changes" err &&
+ printf "\\0123" >binary &&
+ git add binary &&
+ printf "\\0abc" >binary &&
+ git add -p 2>err &&
+ test_grep "Only binary files changed" err
+'
+
+test_expect_success 'setup again' '
+ git reset --hard &&
+ test_chmod +x file &&
+ echo content >>file &&
+ test_write_lines A B C D>file2 &&
+ git add file2
+'
+
+# Write the patch file with a new line at the top and bottom
+test_expect_success 'setup patch' '
+ cat >patch <<-\EOF
+ index 180b47c..b6f2c08 100644
+ --- a/file
+ +++ b/file
+ @@ -1,2 +1,4 @@
+ +firstline
+ baseline
+ content
+ +lastline
+ \ No newline at end of file
+ diff --git a/file2 b/file2
+ index 8422d40..35b930a 100644
+ --- a/file2
+ +++ b/file2
+ @@ -1,4 +1,5 @@
+ -A
+ +Z
+ B
+ +Y
+ C
+ -D
+ +X
+ EOF
+'
+
+# Expected output, diff is similar to the patch but w/ diff at the top
+test_expect_success 'setup expected' '
+ echo diff --git a/file b/file >expected &&
+ sed -e "/^index 180b47c/s/ 100644/ 100755/" \
+ -e /1,5/s//1,4/ \
+ -e /Y/d patch >>expected &&
+ cat >expected-output <<-\EOF
+ --- a/file
+ +++ b/file
+ @@ -1,2 +1,4 @@
+ +firstline
+ baseline
+ content
+ +lastline
+ \ No newline at end of file
+ @@ -1,2 +1,3 @@
+ +firstline
+ baseline
+ content
+ @@ -1,2 +2,3 @@
+ baseline
+ content
+ +lastline
+ \ No newline at end of file
+ --- a/file2
+ +++ b/file2
+ @@ -1,4 +1,5 @@
+ -A
+ +Z
+ B
+ +Y
+ C
+ -D
+ +X
+ @@ -1,2 +1,2 @@
+ -A
+ +Z
+ B
+ @@ -2,2 +2,3 @@
+ B
+ +Y
+ C
+ @@ -3,2 +4,2 @@
+ C
+ -D
+ +X
+ EOF
+'
+
+# Test splitting the first patch, then adding both
+test_expect_success 'add first line works' '
+ git commit -am "clear local changes" &&
+ git apply patch &&
+ test_write_lines s y y s y n y | git add -p 2>error >raw-output &&
+ sed -n -e "s/^([1-9]\/[1-9]) Stage this hunk[^@]*\(@@ .*\)/\1/" \
+ -e "/^[-+@ \\\\]"/p raw-output >output &&
+ test_must_be_empty error &&
+ git diff --cached >diff &&
+ diff_cmp expected diff &&
+ test_cmp expected-output output
+'
+
+test_expect_success 'setup expected' '
+ cat >expected <<-\EOF
+ diff --git a/non-empty b/non-empty
+ deleted file mode 100644
+ index d95f3ad..0000000
+ --- a/non-empty
+ +++ /dev/null
+ @@ -1 +0,0 @@
+ -content
+ EOF
+'
+
+test_expect_success 'deleting a non-empty file' '
+ git reset --hard &&
+ echo content >non-empty &&
+ git add non-empty &&
+ git commit -m non-empty &&
+ rm non-empty &&
+ echo y | git add -p non-empty &&
+ git diff --cached >diff &&
+ diff_cmp expected diff
+'
+
+test_expect_success 'setup expected' '
+ cat >expected <<-\EOF
+ diff --git a/empty b/empty
+ deleted file mode 100644
+ index e69de29..0000000
+ EOF
+'
+
+test_expect_success 'deleting an empty file' '
+ git reset --hard &&
+ > empty &&
+ git add empty &&
+ git commit -m empty &&
+ rm empty &&
+ echo y | git add -p empty &&
+ git diff --cached >diff &&
+ diff_cmp expected diff
+'
+
+test_expect_success 'adding an empty file' '
+ git init added &&
+ (
+ cd added &&
+ test_commit initial &&
+ >empty &&
+ git add empty &&
+ test_tick &&
+ git commit -m empty &&
+ git tag added-file &&
+ git reset --hard HEAD^ &&
+ test_path_is_missing empty &&
+
+ echo y | git checkout -p added-file -- >actual &&
+ test_path_is_file empty &&
+ test_grep "Apply addition to index and worktree" actual
+ )
+'
+
+test_expect_success 'split hunk setup' '
+ git reset --hard &&
+ test_write_lines 10 20 30 40 50 60 >test &&
+ git add test &&
+ test_tick &&
+ git commit -m test &&
+
+ test_write_lines 10 15 20 21 22 23 24 30 40 50 60 >test
+'
+
+test_expect_success 'goto hunk' '
+ test_when_finished "git reset" &&
+ tr _ " " >expect <<-EOF &&
+ (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,?]? + 1: -1,2 +1,3 +15
+ _ 2: -2,4 +3,8 +21
+ go to which hunk? @@ -1,2 +1,3 @@
+ _10
+ +15
+ _20
+ (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]?_
+ EOF
+ test_write_lines s y g 1 | git add -p >actual &&
+ tail -n 7 <actual >actual.trimmed &&
+ test_cmp expect actual.trimmed
+'
+
+test_expect_success 'navigate to hunk via regex' '
+ test_when_finished "git reset" &&
+ tr _ " " >expect <<-EOF &&
+ (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,?]? @@ -1,2 +1,3 @@
+ _10
+ +15
+ _20
+ (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]?_
+ EOF
+ test_write_lines s y /1,2 | git add -p >actual &&
+ tail -n 5 <actual >actual.trimmed &&
+ test_cmp expect actual.trimmed
+'
+
+test_expect_success 'split hunk "add -p (edit)"' '
+ # Split, say Edit and do nothing. Then:
+ #
+ # 1. Broken version results in a patch that does not apply and
+ # only takes [y/n] (edit again) so the first q is discarded
+ # and then n attempts to discard the edit. Repeat q enough
+ # times to get out.
+ #
+ # 2. Correct version applies the (not)edited version, and asks
+ # about the next hunk, against which we say q and program
+ # exits.
+ printf "%s\n" s e q n q q |
+ EDITOR=: git add -p &&
+ git diff >actual &&
+ ! grep "^+15" actual
+'
+
+test_expect_success 'split hunk "add -p (no, yes, edit)"' '
+ test_write_lines 5 10 20 21 30 31 40 50 60 >test &&
+ git reset &&
+ # test sequence is s(plit), n(o), y(es), e(dit)
+ # q n q q is there to make sure we exit at the end.
+ printf "%s\n" s n y e q n q q |
+ EDITOR=: git add -p 2>error &&
+ test_must_be_empty error &&
+ git diff >actual &&
+ ! grep "^+31" actual
+'
+
+test_expect_success 'split hunk with incomplete line at end' '
+ git reset --hard &&
+ printf "missing LF" >>test &&
+ git add test &&
+ test_write_lines before 10 20 30 40 50 60 70 >test &&
+ git grep --cached missing &&
+ test_write_lines s n y q | git add -p &&
+ test_must_fail git grep --cached missing &&
+ git grep before &&
+ test_must_fail git grep --cached before
+'
+
+test_expect_success 'edit, adding lines to the first hunk' '
+ test_write_lines 10 11 20 30 40 50 51 60 >test &&
+ git reset &&
+ tr _ " " >patch <<-EOF &&
+ @@ -1,5 +1,6 @@
+ _10
+ +11
+ +12
+ _20
+ +21
+ +22
+ _30
+ EOF
+ # test sequence is s(plit), e(dit), n(o)
+ # q n q q is there to make sure we exit at the end.
+ printf "%s\n" s e n q n q q |
+ EDITOR=./fake_editor.sh git add -p 2>error &&
+ test_must_be_empty error &&
+ git diff --cached >actual &&
+ grep "^+22" actual
+'
+
+test_expect_success 'patch mode ignores unmerged entries' '
+ git reset --hard &&
+ test_commit conflict &&
+ test_commit non-conflict &&
+ git checkout -b side &&
+ test_commit side conflict.t &&
+ git checkout main &&
+ test_commit main conflict.t &&
+ test_must_fail git merge side &&
+ echo changed >non-conflict.t &&
+ echo y | git add -p >output &&
+ ! grep a/conflict.t output &&
+ cat >expected <<-\EOF &&
+ * Unmerged path conflict.t
+ diff --git a/non-conflict.t b/non-conflict.t
+ index f766221..5ea2ed4 100644
+ --- a/non-conflict.t
+ +++ b/non-conflict.t
+ @@ -1 +1 @@
+ -non-conflict
+ +changed
+ EOF
+ git diff --cached >diff &&
+ diff_cmp expected diff
+'
+
+test_expect_success 'index is refreshed after applying patch' '
+ git reset --hard &&
+ echo content >test &&
+ printf y | git add -p &&
+ git diff-files --exit-code
+'
+
+test_expect_success 'diffs can be colorized' '
+ git reset --hard &&
+
+ echo content >test &&
+ printf y >y &&
+ force_color git add -p >output 2>&1 <y &&
+ git diff-files --exit-code &&
+
+ # We do not want to depend on the exact coloring scheme
+ # git uses for diffs, so just check that we saw some kind of color.
+ grep "$(printf "\\033")" output
+'
+
+test_expect_success 'colors can be overridden' '
+ git reset --hard &&
+ test_when_finished "git rm -f color-test" &&
+ test_write_lines context old more-context >color-test &&
+ git add color-test &&
+ test_write_lines context new more-context another-one >color-test &&
+
+ echo trigger an error message >input &&
+ force_color git \
+ -c color.interactive.error=blue \
+ add -i 2>err.raw <input &&
+ test_decode_color <err.raw >err &&
+ grep "<BLUE>Huh (trigger)?<RESET>" err &&
+
+ test_write_lines help quit >input &&
+ force_color git \
+ -c color.interactive.header=red \
+ -c color.interactive.help=green \
+ -c color.interactive.prompt=yellow \
+ add -i >actual.raw <input &&
+ test_decode_color <actual.raw >actual &&
+ cat >expect <<-\EOF &&
+ <RED> staged unstaged path<RESET>
+ 1: +3/-0 +2/-1 color-test
+
+ <RED>*** Commands ***<RESET>
+ 1: <YELLOW>s<RESET>tatus 2: <YELLOW>u<RESET>pdate 3: <YELLOW>r<RESET>evert 4: <YELLOW>a<RESET>dd untracked
+ 5: <YELLOW>p<RESET>atch 6: <YELLOW>d<RESET>iff 7: <YELLOW>q<RESET>uit 8: <YELLOW>h<RESET>elp
+ <YELLOW>What now<RESET>> <GREEN>status - show paths with changes<RESET>
+ <GREEN>update - add working tree state to the staged set of changes<RESET>
+ <GREEN>revert - revert staged set of changes back to the HEAD version<RESET>
+ <GREEN>patch - pick hunks and update selectively<RESET>
+ <GREEN>diff - view diff between HEAD and index<RESET>
+ <GREEN>add untracked - add contents of untracked files to the staged set of changes<RESET>
+ <RED>*** Commands ***<RESET>
+ 1: <YELLOW>s<RESET>tatus 2: <YELLOW>u<RESET>pdate 3: <YELLOW>r<RESET>evert 4: <YELLOW>a<RESET>dd untracked
+ 5: <YELLOW>p<RESET>atch 6: <YELLOW>d<RESET>iff 7: <YELLOW>q<RESET>uit 8: <YELLOW>h<RESET>elp
+ <YELLOW>What now<RESET>> Bye.
+ EOF
+ test_cmp expect actual &&
+
+ : exercise recolor_hunk by editing and then look at the hunk again &&
+ test_write_lines s e K q >input &&
+ force_color git \
+ -c color.interactive.prompt=yellow \
+ -c color.diff.meta=italic \
+ -c color.diff.frag=magenta \
+ -c color.diff.context=cyan \
+ -c color.diff.old=bold \
+ -c color.diff.new=blue \
+ -c core.editor=touch \
+ add -p >actual.raw <input &&
+ test_decode_color <actual.raw >actual.decoded &&
+ sed "s/index [0-9a-f]*\\.\\.[0-9a-f]* 100644/<INDEX-LINE>/" <actual.decoded >actual &&
+ cat >expect <<-\EOF &&
+ <ITALIC>diff --git a/color-test b/color-test<RESET>
+ <ITALIC><INDEX-LINE><RESET>
+ <ITALIC>--- a/color-test<RESET>
+ <ITALIC>+++ b/color-test<RESET>
+ <MAGENTA>@@ -1,3 +1,4 @@<RESET>
+ <CYAN> context<RESET>
+ <BOLD>-old<RESET>
+ <BLUE>+<RESET><BLUE>new<RESET>
+ <CYAN> more-context<RESET>
+ <BLUE>+<RESET><BLUE>another-one<RESET>
+ <YELLOW>(1/1) Stage this hunk [y,n,q,a,d,s,e,?]? <RESET><BOLD>Split into 2 hunks.<RESET>
+ <MAGENTA>@@ -1,3 +1,3 @@<RESET>
+ <CYAN> context<RESET>
+ <BOLD>-old<RESET>
+ <BLUE>+<RESET><BLUE>new<RESET>
+ <CYAN> more-context<RESET>
+ <YELLOW>(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]? <RESET><MAGENTA>@@ -3 +3,2 @@<RESET>
+ <CYAN> more-context<RESET>
+ <BLUE>+<RESET><BLUE>another-one<RESET>
+ <YELLOW>(2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,?]? <RESET><MAGENTA>@@ -1,3 +1,3 @@<RESET>
+ <CYAN> context<RESET>
+ <BOLD>-old<RESET>
+ <BLUE>+new<RESET>
+ <CYAN> more-context<RESET>
+ <YELLOW>(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]? <RESET>
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'brackets appear without color' '
+ git reset --hard &&
+ test_when_finished "git rm -f bracket-test" &&
+ test_write_lines context old more-context >bracket-test &&
+ git add bracket-test &&
+ test_write_lines context new more-context another-one >bracket-test &&
+
+ test_write_lines quit >input &&
+ git add -i >actual <input &&
+
+ sed "s/^|//" >expect <<-\EOF &&
+ | staged unstaged path
+ | 1: +3/-0 +2/-1 bracket-test
+ |
+ |*** Commands ***
+ | 1: [s]tatus 2: [u]pdate 3: [r]evert 4: [a]dd untracked
+ | 5: [p]atch 6: [d]iff 7: [q]uit 8: [h]elp
+ |What now> Bye.
+ EOF
+
+ test_cmp expect actual
+'
+
+test_expect_success 'colors can be skipped with color.ui=false' '
+ git reset --hard &&
+ test_when_finished "git rm -f color-test" &&
+ test_write_lines context old more-context >color-test &&
+ git add color-test &&
+ test_write_lines context new more-context another-one >color-test &&
+
+ test_write_lines help quit >input &&
+ force_color git \
+ -c color.ui=false \
+ add -i >actual.raw <input &&
+ test_decode_color <actual.raw >actual &&
+ test_cmp actual.raw actual
+'
+
+test_expect_success 'colorized diffs respect diff.wsErrorHighlight' '
+ git reset --hard &&
+
+ echo "old " >test &&
+ git add test &&
+ echo "new " >test &&
+
+ printf y >y &&
+ force_color git -c diff.wsErrorHighlight=all add -p >output.raw 2>&1 <y &&
+ test_decode_color <output.raw >output &&
+ grep "old<" output
+'
+
+test_expect_success 'diffFilter filters diff' '
+ git reset --hard &&
+
+ echo content >test &&
+ test_config interactive.diffFilter "sed s/^/foo:/" &&
+ printf y >y &&
+ force_color git add -p >output 2>&1 <y &&
+
+ # avoid depending on the exact coloring or content of the prompts,
+ # and just make sure we saw our diff prefixed
+ grep foo:.*content output
+'
+
+test_expect_success 'detect bogus diffFilter output' '
+ git reset --hard &&
+
+ echo content >test &&
+ test_config interactive.diffFilter "sed 6d" &&
+ printf y >y &&
+ force_color test_must_fail git add -p <y >output 2>&1 &&
+ grep "mismatched output" output
+'
+
+test_expect_success 'handle iffy colored hunk headers' '
+ git reset --hard &&
+
+ echo content >test &&
+ printf n >n &&
+ force_color git -c interactive.diffFilter="sed s/.*@@.*/XX/" \
+ add -p >output 2>&1 <n &&
+ grep "^XX$" output
+'
+
+test_expect_success 'handle very large filtered diff' '
+ git reset --hard &&
+ # The specific number here is not important, but it must
+ # be large enough that the output of "git diff --color"
+ # fills up the pipe buffer. 10,000 results in ~200k of
+ # colored output.
+ test_seq 10000 >test &&
+ test_config interactive.diffFilter cat &&
+ printf y >y &&
+ force_color git add -p >output 2>&1 <y &&
+ git diff-files --exit-code -- test
+'
+
+test_expect_success 'diff.algorithm is passed to `git diff-files`' '
+ git reset --hard &&
+
+ >file &&
+ git add file &&
+ echo changed >file &&
+ test_must_fail git -c diff.algorithm=bogus add -p 2>err &&
+ test_grep "error: option diff-algorithm accepts " err
+'
+
+test_expect_success 'patch-mode via -i prompts for files' '
+ git reset --hard &&
+
+ echo one >file &&
+ echo two >test &&
+ git add -i <<-\EOF &&
+ patch
+ test
+
+ y
+ quit
+ EOF
+
+ echo test >expect &&
+ git diff --cached --name-only >actual &&
+ diff_cmp expect actual
+'
+
+test_expect_success 'add -p handles globs' '
+ git reset --hard &&
+
+ mkdir -p subdir &&
+ echo base >one.c &&
+ echo base >subdir/two.c &&
+ git add "*.c" &&
+ git commit -m base &&
+
+ echo change >one.c &&
+ echo change >subdir/two.c &&
+ git add -p "*.c" <<-\EOF &&
+ y
+ y
+ EOF
+
+ cat >expect <<-\EOF &&
+ one.c
+ subdir/two.c
+ EOF
+ git diff --cached --name-only >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'add -p handles relative paths' '
+ git reset --hard &&
+
+ echo base >relpath.c &&
+ git add "*.c" &&
+ git commit -m relpath &&
+
+ echo change >relpath.c &&
+ mkdir -p subdir &&
+ git -C subdir add -p .. 2>error <<-\EOF &&
+ y
+ EOF
+
+ test_must_be_empty error &&
+
+ cat >expect <<-\EOF &&
+ relpath.c
+ EOF
+ git diff --cached --name-only >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'add -p does not expand argument lists' '
+ git reset --hard &&
+
+ echo content >not-changed &&
+ git add not-changed &&
+ git commit -m "add not-changed file" &&
+
+ echo change >file &&
+ GIT_TRACE=$(pwd)/trace.out git add -p . <<-\EOF &&
+ y
+ EOF
+
+ # we know that "file" must be mentioned since we actually
+ # update it, but we want to be sure that our "." pathspec
+ # was not expanded into the argument list of any command.
+ # So look only for "not-changed".
+ ! grep -E "^trace: (built-in|exec|run_command): .*not-changed" trace.out
+'
+
+test_expect_success 'hunk-editing handles custom comment char' '
+ git reset --hard &&
+ echo change >>file &&
+ test_config core.commentChar "\$" &&
+ echo e | GIT_EDITOR=true git add -p &&
+ git diff --exit-code
+'
+
+test_expect_success 'add -p works even with color.ui=always' '
+ git reset --hard &&
+ echo change >>file &&
+ test_config color.ui always &&
+ echo y | git add -p &&
+ echo file >expect &&
+ git diff --cached --name-only >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'setup different kinds of dirty submodules' '
+ test_create_repo for-submodules &&
+ (
+ cd for-submodules &&
+ test_commit initial &&
+ test_create_repo dirty-head &&
+ (
+ cd dirty-head &&
+ test_commit initial
+ ) &&
+ cp -R dirty-head dirty-otherwise &&
+ cp -R dirty-head dirty-both-ways &&
+ git add dirty-head &&
+ git add dirty-otherwise dirty-both-ways &&
+ git commit -m initial &&
+
+ cd dirty-head &&
+ test_commit updated &&
+ cd ../dirty-both-ways &&
+ test_commit updated &&
+ echo dirty >>initial &&
+ : >untracked &&
+ cd ../dirty-otherwise &&
+ echo dirty >>initial &&
+ : >untracked
+ ) &&
+ git -C for-submodules diff-files --name-only >actual &&
+ cat >expected <<-\EOF &&
+ dirty-both-ways
+ dirty-head
+ EOF
+ test_cmp expected actual &&
+ git -C for-submodules diff-files --name-only --ignore-submodules=none >actual &&
+ cat >expected <<-\EOF &&
+ dirty-both-ways
+ dirty-head
+ dirty-otherwise
+ EOF
+ test_cmp expected actual &&
+ git -C for-submodules diff-files --name-only --ignore-submodules=dirty >actual &&
+ cat >expected <<-\EOF &&
+ dirty-both-ways
+ dirty-head
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'status ignores dirty submodules (except HEAD)' '
+ git -C for-submodules add -i </dev/null >output &&
+ grep dirty-head output &&
+ grep dirty-both-ways output &&
+ ! grep dirty-otherwise output
+'
+
+test_expect_success 'handle submodules' '
+ echo 123 >>for-submodules/dirty-otherwise/initial.t &&
+
+ force_color git -C for-submodules add -p dirty-otherwise >output 2>&1 &&
+ grep "No changes" output &&
+
+ force_color git -C for-submodules add -p dirty-head >output 2>&1 <y &&
+ git -C for-submodules ls-files --stage dirty-head >actual &&
+ rev="$(git -C for-submodules/dirty-head rev-parse HEAD)" &&
+ grep "$rev" actual
+'
+
+test_expect_success 'set up pathological context' '
+ git reset --hard &&
+ test_write_lines a a a a a a a a a a a >a &&
+ git add a &&
+ git commit -m a &&
+ test_write_lines c b a a a a a a a b a a a a >a &&
+ test_write_lines a a a a a a a b a a a a >expected-1 &&
+ test_write_lines b a a a a a a a b a a a a >expected-2 &&
+ # check editing can cope with missing header and deleted context lines
+ # as well as changes to other lines
+ test_write_lines +b " a" >patch
+'
+
+test_expect_success 'add -p works with pathological context lines' '
+ git reset &&
+ printf "%s\n" n y |
+ git add -p &&
+ git cat-file blob :a >actual &&
+ test_cmp expected-1 actual
+'
+
+test_expect_success 'add -p patch editing works with pathological context lines' '
+ git reset &&
+ # n q q below is in case edit fails
+ printf "%s\n" e y n q q |
+ git add -p &&
+ git cat-file blob :a >actual &&
+ test_cmp expected-2 actual
+'
+
+test_expect_success 'checkout -p works with pathological context lines' '
+ test_write_lines a a a a a a >a &&
+ git add a &&
+ test_write_lines a b a b a b a b a b a >a &&
+ test_write_lines s n n y q | git checkout -p &&
+ test_write_lines a b a b a a b a b a >expect &&
+ test_cmp expect a
+'
+
+# This should be called from a subshell as it sets a temporary editor
+setup_new_file() {
+ write_script new-file-editor.sh <<-\EOF &&
+ sed /^#/d "$1" >patch &&
+ sed /^+c/d patch >"$1"
+ EOF
+ test_set_editor "$(pwd)/new-file-editor.sh" &&
+ test_write_lines a b c d e f >new-file &&
+ test_write_lines a b d e f >new-file-expect &&
+ test_write_lines "@@ -0,0 +1,6 @@" +a +b +c +d +e +f >patch-expect
+}
+
+test_expect_success 'add -N followed by add -p patch editing' '
+ git reset --hard &&
+ (
+ setup_new_file &&
+ git add -N new-file &&
+ test_write_lines e n q | git add -p &&
+ git cat-file blob :new-file >actual &&
+ test_cmp new-file-expect actual &&
+ test_cmp patch-expect patch
+ )
+'
+
+test_expect_success 'checkout -p patch editing of added file' '
+ git reset --hard &&
+ (
+ setup_new_file &&
+ git add new-file &&
+ git commit -m "add new file" &&
+ git rm new-file &&
+ git commit -m "remove new file" &&
+ test_write_lines e n q | git checkout -p HEAD^ &&
+ test_cmp new-file-expect new-file &&
+ test_cmp patch-expect patch
+ )
+'
+
+test_expect_success 'show help from add--helper' '
+ git reset --hard &&
+ cat >expect <<-EOF &&
+
+ <BOLD>*** Commands ***<RESET>
+ 1: <BOLD;BLUE>s<RESET>tatus 2: <BOLD;BLUE>u<RESET>pdate 3: <BOLD;BLUE>r<RESET>evert 4: <BOLD;BLUE>a<RESET>dd untracked
+ 5: <BOLD;BLUE>p<RESET>atch 6: <BOLD;BLUE>d<RESET>iff 7: <BOLD;BLUE>q<RESET>uit 8: <BOLD;BLUE>h<RESET>elp
+ <BOLD;BLUE>What now<RESET>> <BOLD;RED>status - show paths with changes<RESET>
+ <BOLD;RED>update - add working tree state to the staged set of changes<RESET>
+ <BOLD;RED>revert - revert staged set of changes back to the HEAD version<RESET>
+ <BOLD;RED>patch - pick hunks and update selectively<RESET>
+ <BOLD;RED>diff - view diff between HEAD and index<RESET>
+ <BOLD;RED>add untracked - add contents of untracked files to the staged set of changes<RESET>
+ <BOLD>*** Commands ***<RESET>
+ 1: <BOLD;BLUE>s<RESET>tatus 2: <BOLD;BLUE>u<RESET>pdate 3: <BOLD;BLUE>r<RESET>evert 4: <BOLD;BLUE>a<RESET>dd untracked
+ 5: <BOLD;BLUE>p<RESET>atch 6: <BOLD;BLUE>d<RESET>iff 7: <BOLD;BLUE>q<RESET>uit 8: <BOLD;BLUE>h<RESET>elp
+ <BOLD;BLUE>What now<RESET>>$SP
+ Bye.
+ EOF
+ test_write_lines h | force_color git add -i >actual.colored &&
+ test_decode_color <actual.colored >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'reset -p with unmerged files' '
+ test_when_finished "git checkout --force main" &&
+ test_commit one conflict &&
+ git checkout -B side HEAD^ &&
+ test_commit two conflict &&
+ test_must_fail git merge one &&
+
+ # this is a noop with only an unmerged entry
+ git reset -p &&
+
+ # add files that sort before and after unmerged entry
+ echo a >a &&
+ echo z >z &&
+ git add a z &&
+
+ # confirm that we can reset those files
+ printf "%s\n" y y | git reset -p &&
+ git diff-index --cached --diff-filter=u HEAD >staged &&
+ test_must_be_empty staged
+'
+
+test_done
diff --git a/t/t3702-add-edit.sh b/t/t3702-add-edit.sh
new file mode 100755
index 0000000..82bfb2f
--- /dev/null
+++ b/t/t3702-add-edit.sh
@@ -0,0 +1,129 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Johannes E. Schindelin
+#
+
+test_description='add -e basic tests'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+
+cat > file << EOF
+LO, praise of the prowess of people-kings
+of spear-armed Danes, in days long sped,
+we have heard, and what honor the athelings won!
+Oft Scyld the Scefing from squadroned foes,
+from many a tribe, the mead-bench tore,
+awing the earls. Since erst he lay
+friendless, a foundling, fate repaid him:
+for he waxed under welkin, in wealth he throve,
+till before him the folk, both far and near,
+who house by the whale-path, heard his mandate,
+gave him gifts: a good king he!
+EOF
+
+cat > second-part << EOF
+To him an heir was afterward born,
+a son in his halls, whom heaven sent
+to favor the folk, feeling their woe
+that erst they had lacked an earl for leader
+so long a while; the Lord endowed him,
+the Wielder of Wonder, with world's renown.
+EOF
+
+test_expect_success 'setup' '
+
+ git add file &&
+ test_tick &&
+ git commit -m initial file
+
+'
+
+cat > expected-patch << EOF
+diff --git a/file b/file
+--- a/file
++++ b/file
+@@ -1,11 +1,6 @@
+-LO, praise of the prowess of people-kings
+-of spear-armed Danes, in days long sped,
+-we have heard, and what honor the athelings won!
+-Oft Scyld the Scefing from squadroned foes,
+-from many a tribe, the mead-bench tore,
+-awing the earls. Since erst he lay
+-friendless, a foundling, fate repaid him:
+-for he waxed under welkin, in wealth he throve,
+-till before him the folk, both far and near,
+-who house by the whale-path, heard his mandate,
+-gave him gifts: a good king he!
++To him an heir was afterward born,
++a son in his halls, whom heaven sent
++to favor the folk, feeling their woe
++that erst they had lacked an earl for leader
++so long a while; the Lord endowed him,
++the Wielder of Wonder, with world's renown.
+EOF
+
+cat > patch << EOF
+diff --git a/file b/file
+index b9834b5..ef6e94c 100644
+--- a/file
++++ b/file
+@@ -3,1 +3,333 @@ of spear-armed Danes, in days long sped,
+ we have heard, and what honor the athelings won!
++
+ Oft Scyld the Scefing from squadroned foes,
+@@ -2,7 +1,5 @@ awing the earls. Since erst he lay
+ friendless, a foundling, fate repaid him:
++
+ for he waxed under welkin, in wealth he throve,
+EOF
+
+cat > expected << EOF
+diff --git a/file b/file
+--- a/file
++++ b/file
+@@ -1,10 +1,12 @@
+ LO, praise of the prowess of people-kings
+ of spear-armed Danes, in days long sped,
+ we have heard, and what honor the athelings won!
++
+ Oft Scyld the Scefing from squadroned foes,
+ from many a tribe, the mead-bench tore,
+ awing the earls. Since erst he lay
+ friendless, a foundling, fate repaid him:
++
+ for he waxed under welkin, in wealth he throve,
+ till before him the folk, both far and near,
+ who house by the whale-path, heard his mandate,
+EOF
+
+echo "#!$SHELL_PATH" >fake-editor.sh
+cat >> fake-editor.sh <<\EOF
+grep -E -v '^index' "$1" >orig-patch &&
+mv -f patch "$1"
+EOF
+
+test_set_editor "$(pwd)/fake-editor.sh"
+chmod a+x fake-editor.sh
+
+test_expect_success 'add -e' '
+
+ cp second-part file &&
+ git add -e &&
+ test_cmp second-part file &&
+ test_cmp expected-patch orig-patch &&
+ git diff --cached >actual &&
+ grep -v index actual >out &&
+ test_cmp expected out
+
+'
+
+test_expect_success 'add -e notices editor failure' '
+ git reset --hard &&
+ echo change >>file &&
+ test_must_fail env GIT_EDITOR=false git add -e &&
+ test_expect_code 1 git diff --exit-code
+'
+
+test_done
diff --git a/t/t3703-add-magic-pathspec.sh b/t/t3703-add-magic-pathspec.sh
new file mode 100755
index 0000000..d840710
--- /dev/null
+++ b/t/t3703-add-magic-pathspec.sh
@@ -0,0 +1,59 @@
+#!/bin/sh
+
+test_description='magic pathspec tests using git-add'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ mkdir sub anothersub &&
+ : >sub/foo &&
+ : >anothersub/foo
+'
+
+test_expect_success 'add :/' "
+ cat >expected <<-EOF &&
+ add 'anothersub/foo'
+ add 'expected'
+ add 'sub/actual'
+ add 'sub/foo'
+ EOF
+ (cd sub && git add -n :/ >actual) &&
+ test_cmp expected sub/actual
+"
+
+cat >expected <<EOF
+add 'anothersub/foo'
+EOF
+
+test_expect_success 'add :/anothersub' '
+ (cd sub && git add -n :/anothersub >actual) &&
+ test_cmp expected sub/actual
+'
+
+test_expect_success 'add :/non-existent' '
+ (cd sub && test_must_fail git add -n :/non-existent)
+'
+
+cat >expected <<EOF
+add 'sub/foo'
+EOF
+
+if test_have_prereq !MINGW && mkdir ":" 2>/dev/null
+then
+ test_set_prereq COLON_DIR
+fi
+
+test_expect_success COLON_DIR 'a file with the same (long) magic name exists' '
+ : >":(icase)ha" &&
+ test_must_fail git add -n ":(icase)ha" &&
+ git add -n "./:(icase)ha"
+'
+
+test_expect_success COLON_DIR 'a file with the same (short) magic name exists' '
+ : >":/bar" &&
+ test_must_fail git add -n :/bar &&
+ git add -n "./:/bar"
+'
+
+test_done
diff --git a/t/t3704-add-pathspec-file.sh b/t/t3704-add-pathspec-file.sh
new file mode 100755
index 0000000..3aa59f6
--- /dev/null
+++ b/t/t3704-add-pathspec-file.sh
@@ -0,0 +1,160 @@
+#!/bin/sh
+
+test_description='add --pathspec-from-file'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_tick
+
+test_expect_success setup '
+ test_commit file0 &&
+ echo A >fileA.t &&
+ echo B >fileB.t &&
+ echo C >fileC.t &&
+ echo D >fileD.t
+'
+
+restore_checkpoint () {
+ git reset
+}
+
+verify_expect () {
+ git status --porcelain --untracked-files=no -- fileA.t fileB.t fileC.t fileD.t >actual &&
+ test_cmp expect actual
+}
+
+test_expect_success '--pathspec-from-file from stdin' '
+ restore_checkpoint &&
+
+ echo fileA.t | git add --pathspec-from-file=- &&
+
+ cat >expect <<-\EOF &&
+ A fileA.t
+ EOF
+ verify_expect
+'
+
+test_expect_success '--pathspec-from-file from file' '
+ restore_checkpoint &&
+
+ echo fileA.t >list &&
+ git add --pathspec-from-file=list &&
+
+ cat >expect <<-\EOF &&
+ A fileA.t
+ EOF
+ verify_expect
+'
+
+test_expect_success 'NUL delimiters' '
+ restore_checkpoint &&
+
+ printf "fileA.t\0fileB.t\0" | git add --pathspec-from-file=- --pathspec-file-nul &&
+
+ cat >expect <<-\EOF &&
+ A fileA.t
+ A fileB.t
+ EOF
+ verify_expect
+'
+
+test_expect_success 'LF delimiters' '
+ restore_checkpoint &&
+
+ printf "fileA.t\nfileB.t\n" | git add --pathspec-from-file=- &&
+
+ cat >expect <<-\EOF &&
+ A fileA.t
+ A fileB.t
+ EOF
+ verify_expect
+'
+
+test_expect_success 'no trailing delimiter' '
+ restore_checkpoint &&
+
+ printf "fileA.t\nfileB.t" | git add --pathspec-from-file=- &&
+
+ cat >expect <<-\EOF &&
+ A fileA.t
+ A fileB.t
+ EOF
+ verify_expect
+'
+
+test_expect_success 'CRLF delimiters' '
+ restore_checkpoint &&
+
+ printf "fileA.t\r\nfileB.t\r\n" | git add --pathspec-from-file=- &&
+
+ cat >expect <<-\EOF &&
+ A fileA.t
+ A fileB.t
+ EOF
+ verify_expect
+'
+
+test_expect_success 'quotes' '
+ restore_checkpoint &&
+
+ cat >list <<-\EOF &&
+ "file\101.t"
+ EOF
+
+ git add --pathspec-from-file=list &&
+
+ cat >expect <<-\EOF &&
+ A fileA.t
+ EOF
+ verify_expect
+'
+
+test_expect_success 'quotes not compatible with --pathspec-file-nul' '
+ restore_checkpoint &&
+
+ cat >list <<-\EOF &&
+ "file\101.t"
+ EOF
+
+ test_must_fail git add --pathspec-from-file=list --pathspec-file-nul
+'
+
+test_expect_success 'only touches what was listed' '
+ restore_checkpoint &&
+
+ printf "fileB.t\nfileC.t\n" | git add --pathspec-from-file=- &&
+
+ cat >expect <<-\EOF &&
+ A fileB.t
+ A fileC.t
+ EOF
+ verify_expect
+'
+
+test_expect_success 'error conditions' '
+ restore_checkpoint &&
+ echo fileA.t >list &&
+ >empty_list &&
+
+ test_must_fail git add --pathspec-from-file=list --interactive 2>err &&
+ test_grep -e "options .--pathspec-from-file. and .--interactive/--patch. cannot be used together" err &&
+
+ test_must_fail git add --pathspec-from-file=list --patch 2>err &&
+ test_grep -e "options .--pathspec-from-file. and .--interactive/--patch. cannot be used together" err &&
+
+ test_must_fail git add --pathspec-from-file=list --edit 2>err &&
+ test_grep -e "options .--pathspec-from-file. and .--edit. cannot be used together" err &&
+
+ test_must_fail git add --pathspec-from-file=list -- fileA.t 2>err &&
+ test_grep -e ".--pathspec-from-file. and pathspec arguments cannot be used together" err &&
+
+ test_must_fail git add --pathspec-file-nul 2>err &&
+ test_grep -e "the option .--pathspec-file-nul. requires .--pathspec-from-file." err &&
+
+ # This case succeeds, but still prints to stderr
+ git add --pathspec-from-file=empty_list 2>err &&
+ test_grep -e "Nothing specified, nothing added." err
+'
+
+test_done
diff --git a/t/t3705-add-sparse-checkout.sh b/t/t3705-add-sparse-checkout.sh
new file mode 100755
index 0000000..2bade9e
--- /dev/null
+++ b/t/t3705-add-sparse-checkout.sh
@@ -0,0 +1,236 @@
+#!/bin/sh
+
+test_description='git add in sparse checked out working trees'
+
+. ./test-lib.sh
+
+SPARSE_ENTRY_BLOB=""
+
+# Optionally take a printf format string to write to the sparse_entry file
+setup_sparse_entry () {
+ # 'sparse_entry' might already be in the index with the skip-worktree
+ # bit set. Remove it so that the subsequent git add can update it.
+ git update-index --force-remove sparse_entry &&
+ if test $# -eq 1
+ then
+ printf "$1" >sparse_entry
+ else
+ >sparse_entry
+ fi &&
+ git add sparse_entry &&
+ git update-index --skip-worktree sparse_entry &&
+ git config core.sparseCheckout false &&
+ git commit --allow-empty -m "ensure sparse_entry exists at HEAD" &&
+ SPARSE_ENTRY_BLOB=$(git rev-parse :sparse_entry)
+}
+
+test_sparse_entry_unchanged () {
+ echo "100644 $SPARSE_ENTRY_BLOB 0 sparse_entry" >expected &&
+ git ls-files --stage sparse_entry >actual &&
+ test_cmp expected actual
+}
+
+setup_gitignore () {
+ test_when_finished rm -f .gitignore &&
+ cat >.gitignore <<-EOF
+ *
+ !/sparse_entry
+ EOF
+}
+
+test_sparse_entry_unstaged () {
+ git diff --staged -- sparse_entry >diff &&
+ test_must_be_empty diff
+}
+
+test_expect_success 'setup' "
+ cat >sparse_error_header <<-EOF &&
+ The following paths and/or pathspecs matched paths that exist
+ outside of your sparse-checkout definition, so will not be
+ updated in the index:
+ EOF
+
+ cat >sparse_hint <<-EOF &&
+ hint: If you intend to update such entries, try one of the following:
+ hint: * Use the --sparse option.
+ hint: * Disable or modify the sparsity rules.
+ hint: Disable this message with \"git config advice.updateSparsePath false\"
+ EOF
+
+ echo sparse_entry | cat sparse_error_header - >sparse_entry_error &&
+ cat sparse_entry_error sparse_hint >error_and_hint
+"
+
+test_expect_success 'git add does not remove sparse entries' '
+ setup_sparse_entry &&
+ rm sparse_entry &&
+ test_must_fail git add sparse_entry 2>stderr &&
+ test_sparse_entry_unstaged &&
+ test_cmp error_and_hint stderr &&
+ test_sparse_entry_unchanged
+'
+
+test_expect_success 'git add -A does not remove sparse entries' '
+ setup_sparse_entry &&
+ rm sparse_entry &&
+ setup_gitignore &&
+ git add -A 2>stderr &&
+ test_must_be_empty stderr &&
+ test_sparse_entry_unchanged
+'
+
+test_expect_success 'git add . does not remove sparse entries' '
+ setup_sparse_entry &&
+ rm sparse_entry &&
+ setup_gitignore &&
+ test_must_fail git add . 2>stderr &&
+ test_sparse_entry_unstaged &&
+
+ cat sparse_error_header >expect &&
+ echo . >>expect &&
+ cat sparse_hint >>expect &&
+
+ test_cmp expect stderr &&
+ test_sparse_entry_unchanged
+'
+
+for opt in "" -f -u --ignore-removal --dry-run
+do
+ test_expect_success "git add${opt:+ $opt} does not update sparse entries" '
+ setup_sparse_entry &&
+ echo modified >sparse_entry &&
+ test_must_fail git add $opt sparse_entry 2>stderr &&
+ test_sparse_entry_unstaged &&
+ test_cmp error_and_hint stderr &&
+ test_sparse_entry_unchanged
+ '
+done
+
+test_expect_success 'git add --refresh does not update sparse entries' '
+ setup_sparse_entry &&
+ git ls-files --debug sparse_entry | grep mtime >before &&
+ test-tool chmtime -60 sparse_entry &&
+ test_must_fail git add --refresh sparse_entry 2>stderr &&
+ test_sparse_entry_unstaged &&
+ test_cmp error_and_hint stderr &&
+ git ls-files --debug sparse_entry | grep mtime >after &&
+ test_cmp before after
+'
+
+test_expect_success 'git add --chmod does not update sparse entries' '
+ setup_sparse_entry &&
+ test_must_fail git add --chmod=+x sparse_entry 2>stderr &&
+ test_sparse_entry_unstaged &&
+ test_cmp error_and_hint stderr &&
+ test_sparse_entry_unchanged &&
+ ! test -x sparse_entry
+'
+
+test_expect_success 'git add --renormalize does not update sparse entries' '
+ test_when_finished rm .gitattributes &&
+ test_config core.autocrlf false &&
+ setup_sparse_entry "LINEONE\r\nLINETWO\r\n" &&
+ echo "sparse_entry text=auto" >.gitattributes &&
+ test_must_fail git add --renormalize sparse_entry 2>stderr &&
+ test_sparse_entry_unstaged &&
+ test_cmp error_and_hint stderr &&
+ test_sparse_entry_unchanged
+'
+
+test_expect_success 'git add --dry-run --ignore-missing warn on sparse path' '
+ setup_sparse_entry &&
+ rm sparse_entry &&
+ test_must_fail git add --dry-run --ignore-missing sparse_entry 2>stderr &&
+ test_sparse_entry_unstaged &&
+ test_cmp error_and_hint stderr &&
+ test_sparse_entry_unchanged
+'
+
+test_expect_success 'do not advice about sparse entries when they do not match the pathspec' '
+ setup_sparse_entry &&
+ test_must_fail git add nonexistent 2>stderr &&
+ grep "fatal: pathspec .nonexistent. did not match any files" stderr &&
+ ! grep -F -f sparse_error_header stderr
+'
+
+test_expect_success 'do not warn when pathspec matches dense entries' '
+ setup_sparse_entry &&
+ echo modified >sparse_entry &&
+ >dense_entry &&
+ git add "*_entry" 2>stderr &&
+ test_must_be_empty stderr &&
+ test_sparse_entry_unchanged &&
+ git ls-files --error-unmatch dense_entry
+'
+
+test_expect_success 'git add fails outside of sparse-checkout definition' '
+ test_when_finished git sparse-checkout disable &&
+ test_commit a &&
+ git sparse-checkout init --no-cone &&
+ git sparse-checkout set a &&
+ echo >>sparse_entry &&
+
+ git update-index --no-skip-worktree sparse_entry &&
+ test_must_fail git add sparse_entry &&
+ test_sparse_entry_unstaged &&
+
+ test_must_fail git add --chmod=+x sparse_entry &&
+ test_sparse_entry_unstaged &&
+
+ test_must_fail git add --renormalize sparse_entry &&
+ test_sparse_entry_unstaged &&
+
+ # Avoid munging CRLFs to avoid an error message
+ git -c core.autocrlf=input add --sparse sparse_entry 2>stderr &&
+ test_must_be_empty stderr &&
+ git ls-files --stage >actual &&
+ grep "^100644 .*sparse_entry\$" actual &&
+
+ git add --sparse --chmod=+x sparse_entry 2>stderr &&
+ test_must_be_empty stderr &&
+ git ls-files --stage >actual &&
+ grep "^100755 .*sparse_entry\$" actual &&
+
+ git reset &&
+
+ # This will print a message over stderr on Windows.
+ git add --sparse --renormalize sparse_entry &&
+ git status --porcelain >actual &&
+ grep "^M sparse_entry\$" actual
+'
+
+test_expect_success 'add obeys advice.updateSparsePath' '
+ setup_sparse_entry &&
+ test_must_fail git -c advice.updateSparsePath=false add sparse_entry 2>stderr &&
+ test_sparse_entry_unstaged &&
+ test_cmp sparse_entry_error stderr
+
+'
+
+test_expect_success 'add allows sparse entries with --sparse' '
+ git sparse-checkout set --no-cone a &&
+ echo modified >sparse_entry &&
+ test_must_fail git add sparse_entry &&
+ test_sparse_entry_unchanged &&
+ git add --sparse sparse_entry 2>stderr &&
+ test_must_be_empty stderr
+'
+
+test_expect_success 'can add files from non-sparse dir' '
+ git sparse-checkout set w !/x y/ &&
+ mkdir -p w x/y &&
+ touch w/f x/y/f &&
+ git add w/f x/y/f 2>stderr &&
+ test_must_be_empty stderr
+'
+
+test_expect_success 'refuse to add non-skip-worktree file from sparse dir' '
+ git sparse-checkout set !/x y/ !x/y/z &&
+ mkdir -p x/y/z &&
+ touch x/y/z/f &&
+ test_must_fail git add x/y/z/f 2>stderr &&
+ echo x/y/z/f | cat sparse_error_header - sparse_hint >expect &&
+ test_cmp expect stderr
+'
+
+test_done
diff --git a/t/t3800-mktag.sh b/t/t3800-mktag.sh
new file mode 100755
index 0000000..d3e428f
--- /dev/null
+++ b/t/t3800-mktag.sh
@@ -0,0 +1,587 @@
+#!/bin/sh
+#
+#
+
+test_description='git mktag: tag object verify test'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+###########################################################
+# check the tag.sig file, expecting verify_tag() to fail,
+# and checking that the error message matches the pattern
+# given in the expect.pat file.
+
+check_verify_failure () {
+ subject=$1 &&
+ message=$2 &&
+ shift 2 &&
+
+ no_strict= &&
+ fsck_obj_ok= &&
+ no_strict= &&
+ while test $# != 0
+ do
+ case "$1" in
+ --no-strict)
+ no_strict=yes
+ ;;
+ --fsck-obj-ok)
+ fsck_obj_ok=yes
+ ;;
+ esac &&
+ shift
+ done &&
+
+ test_expect_success "fail with [--[no-]strict]: $subject" '
+ test_must_fail git mktag <tag.sig 2>err &&
+ if test -z "$no_strict"
+ then
+ test_must_fail git mktag <tag.sig 2>err2 &&
+ test_cmp err err2
+ else
+ git mktag --no-strict <tag.sig
+ fi
+ '
+
+ test_expect_success "setup: $subject" '
+ tag_ref=refs/tags/bad_tag &&
+
+ # Reset any leftover state from the last $subject
+ rm -rf bad-tag &&
+
+ git init --bare bad-tag &&
+ bad_tag=$(git -C bad-tag hash-object -t tag -w --stdin --literally <tag.sig)
+ '
+
+ test_expect_success "hash-object & fsck unreachable: $subject" '
+ if test -n "$fsck_obj_ok"
+ then
+ git -C bad-tag fsck
+ else
+ test_must_fail git -C bad-tag fsck
+ fi
+ '
+
+ test_expect_success "update-ref & fsck reachable: $subject" '
+ # Make sure the earlier test created it for us
+ git rev-parse "$bad_tag" &&
+
+ # The update-ref of the bad content will fail, do it
+ # anyway to see if it segfaults
+ test_might_fail git -C bad-tag update-ref "$tag_ref" "$bad_tag" &&
+
+ # Manually create the broken, we cannot do it with
+ # update-ref
+ test-tool -C bad-tag ref-store main delete-refs 0 msg "$tag_ref" &&
+ test-tool -C bad-tag ref-store main update-ref msg "$tag_ref" $bad_tag $ZERO_OID REF_SKIP_OID_VERIFICATION &&
+
+ # Unlike fsck-ing unreachable content above, this
+ # will always fail.
+ test_must_fail git -C bad-tag fsck
+ '
+
+ test_expect_success "for-each-ref: $subject" '
+ # Make sure the earlier test created it for us
+ git rev-parse "$bad_tag" &&
+
+ test-tool -C bad-tag ref-store main delete-refs 0 msg "$tag_ref" &&
+ test-tool -C bad-tag ref-store main update-ref msg "$tag_ref" $bad_tag $ZERO_OID REF_SKIP_OID_VERIFICATION &&
+
+ printf "%s tag\t%s\n" "$bad_tag" "$tag_ref" >expected &&
+ git -C bad-tag for-each-ref "$tag_ref" >actual &&
+ test_cmp expected actual &&
+
+ test_must_fail git -C bad-tag for-each-ref --format="%(*objectname)"
+ '
+
+ test_expect_success "fast-export & fast-import: $subject" '
+ # Make sure the earlier test created it for us
+ git rev-parse "$bad_tag" &&
+
+ test_must_fail git -C bad-tag fast-export --all &&
+ test_must_fail git -C bad-tag fast-export "$bad_tag"
+ '
+}
+
+test_expect_mktag_success() {
+ test_expect_success "$1" '
+ git hash-object -t tag -w --stdin <tag.sig >expected &&
+ git fsck --strict &&
+
+ git mktag <tag.sig >hash &&
+ test_cmp expected hash &&
+ test_when_finished "git update-ref -d refs/tags/mytag $(cat hash)" &&
+ git update-ref refs/tags/mytag $(cat hash) $(test_oid zero) &&
+ git fsck --strict
+ '
+}
+
+###########################################################
+# first create a commit, so we have a valid object/type
+# for the tag.
+test_expect_success 'setup' '
+ test_commit A &&
+ test_commit B &&
+ head=$(git rev-parse --verify HEAD) &&
+ head_parent=$(git rev-parse --verify HEAD~) &&
+ tree=$(git rev-parse HEAD^{tree}) &&
+ blob=$(git rev-parse --verify HEAD:B.t)
+'
+
+test_expect_success 'basic usage' '
+ cat >tag.sig <<-EOF &&
+ object $head
+ type commit
+ tag mytag
+ tagger T A Gger <tagger@example.com> 1206478233 -0500
+ EOF
+ git mktag <tag.sig &&
+ git mktag --end-of-options <tag.sig &&
+ test_expect_code 129 git mktag --unknown-option
+'
+
+############################################################
+# 1. length check
+
+cat >tag.sig <<EOF
+too short for a tag
+EOF
+
+check_verify_failure 'Tag object length check' \
+ '^error:.* missingObject:' 'strict'
+
+############################################################
+# 2. object line label check
+
+cat >tag.sig <<EOF
+xxxxxx $head
+type tag
+tag mytag
+tagger . <> 0 +0000
+
+EOF
+
+check_verify_failure '"object" line label check' '^error:.* missingObject:'
+
+############################################################
+# 3. object line hash check
+
+cat >tag.sig <<EOF
+object $(echo $head | tr 0-9a-f z)
+type tag
+tag mytag
+tagger . <> 0 +0000
+
+EOF
+
+check_verify_failure '"object" line check' '^error:.* badObjectSha1:'
+
+############################################################
+# 4. type line label check
+
+cat >tag.sig <<EOF
+object $head
+xxxx tag
+tag mytag
+tagger . <> 0 +0000
+
+EOF
+
+check_verify_failure '"type" line label check' '^error:.* missingTypeEntry:'
+
+############################################################
+# 5. type line eol check
+
+echo "object $head" >tag.sig
+printf "type tagsssssssssssssssssssssssssssssss" >>tag.sig
+
+check_verify_failure '"type" line eol check' '^error:.* unterminatedHeader:'
+
+############################################################
+# 6. tag line label check #1
+
+cat >tag.sig <<EOF
+object $head
+type tag
+xxx mytag
+tagger . <> 0 +0000
+
+EOF
+
+check_verify_failure '"tag" line label check #1' \
+ '^error:.* missingTagEntry:'
+
+############################################################
+# 7. tag line label check #2
+
+cat >tag.sig <<EOF
+object $head
+type taggggggggggggggggggggggggggggggg
+tag
+EOF
+
+check_verify_failure '"tag" line label check #2' \
+ '^error:.* badType:'
+
+############################################################
+# 8. type line type-name length check
+
+cat >tag.sig <<EOF
+object $head
+type taggggggggggggggggggggggggggggggg
+tag mytag
+EOF
+
+check_verify_failure '"type" line type-name length check' \
+ '^error:.* badType:'
+
+############################################################
+# 9. verify object (hash/type) check
+
+cat >tag.sig <<EOF
+object $(test_oid deadbeef)
+type tag
+tag mytag
+tagger . <> 0 +0000
+
+EOF
+
+check_verify_failure 'verify object (hash/type) check -- correct type, nonexisting object' \
+ '^fatal: could not read tagged object' \
+ --fsck-obj-ok
+
+cat >tag.sig <<EOF
+object $head
+type tagggg
+tag mytag
+tagger . <> 0 +0000
+
+EOF
+
+check_verify_failure 'verify object (hash/type) check -- made-up type, valid object' \
+ '^error:.* badType:'
+
+cat >tag.sig <<EOF
+object $(test_oid deadbeef)
+type tagggg
+tag mytag
+tagger . <> 0 +0000
+
+EOF
+
+check_verify_failure 'verify object (hash/type) check -- made-up type, nonexisting object' \
+ '^error:.* badType:'
+
+cat >tag.sig <<EOF
+object $head
+type tree
+tag mytag
+tagger . <> 0 +0000
+
+EOF
+
+check_verify_failure 'verify object (hash/type) check -- mismatched type, valid object' \
+ '^fatal: object.*tagged as.*tree.*but is.*commit' \
+ --fsck-obj-ok
+
+############################################################
+# 9.5. verify object (hash/type) check -- replacement
+
+test_expect_success 'setup replacement of commit -> commit and tree -> blob' '
+ git replace $head_parent $head &&
+ git replace -f $tree $blob
+'
+
+cat >tag.sig <<EOF
+object $head_parent
+type commit
+tag mytag
+tagger . <> 0 +0000
+
+EOF
+
+test_expect_mktag_success 'tag to a commit replaced by another commit'
+
+cat >tag.sig <<EOF
+object $tree
+type tree
+tag mytag
+tagger . <> 0 +0000
+
+EOF
+
+check_verify_failure 'verify object (hash/type) check -- mismatched type, valid object' \
+ '^fatal: object.*tagged as.*tree.*but is.*blob' \
+ --fsck-obj-ok
+
+############################################################
+# 10. verify tag-name check
+
+cat >tag.sig <<EOF
+object $head
+type commit
+tag my tag
+tagger . <> 0 +0000
+
+EOF
+
+check_verify_failure 'verify tag-name check' \
+ '^error:.* badTagName:' \
+ --no-strict \
+ --fsck-obj-ok
+
+############################################################
+# 11. tagger line label check #1
+
+cat >tag.sig <<EOF
+object $head
+type commit
+tag mytag
+
+This is filler
+EOF
+
+check_verify_failure '"tagger" line label check #1' \
+ '^error:.* missingTaggerEntry:' \
+ --no-strict \
+ --fsck-obj-ok
+
+############################################################
+# 12. tagger line label check #2
+
+cat >tag.sig <<EOF
+object $head
+type commit
+tag mytag
+tagger
+
+This is filler
+EOF
+
+check_verify_failure '"tagger" line label check #2' \
+ '^error:.* missingTaggerEntry:' \
+ --no-strict \
+ --fsck-obj-ok
+
+############################################################
+# 13. allow missing tag author name like fsck
+
+cat >tag.sig <<EOF
+object $head
+type commit
+tag mytag
+tagger <> 0 +0000
+
+This is filler
+EOF
+
+test_expect_mktag_success 'allow missing tag author name'
+
+############################################################
+# 14. disallow missing tag author name
+
+cat >tag.sig <<EOF
+object $head
+type commit
+tag mytag
+tagger T A Gger <
+ > 0 +0000
+
+EOF
+
+check_verify_failure 'disallow malformed tagger' \
+ '^error:.* badEmail:' \
+ --no-strict \
+ --fsck-obj-ok
+
+############################################################
+# 15. allow empty tag email
+
+cat >tag.sig <<EOF
+object $head
+type commit
+tag mytag
+tagger T A Gger <> 0 +0000
+
+EOF
+
+test_expect_mktag_success 'allow empty tag email'
+
+############################################################
+# 16. allow spaces in tag email like fsck
+
+cat >tag.sig <<EOF
+object $head
+type commit
+tag mytag
+tagger T A Gger <tag ger@example.com> 0 +0000
+
+EOF
+
+test_expect_mktag_success 'allow spaces in tag email like fsck'
+
+############################################################
+# 17. disallow missing tag timestamp
+
+tr '_' ' ' >tag.sig <<EOF
+object $head
+type commit
+tag mytag
+tagger T A Gger <tagger@example.com>__
+
+EOF
+
+check_verify_failure 'disallow missing tag timestamp' \
+ '^error:.* badDate:'
+
+############################################################
+# 18. detect invalid tag timestamp1
+
+cat >tag.sig <<EOF
+object $head
+type commit
+tag mytag
+tagger T A Gger <tagger@example.com> Tue Mar 25 15:47:44 2008
+
+EOF
+
+check_verify_failure 'detect invalid tag timestamp1' \
+ '^error:.* badDate:'
+
+############################################################
+# 19. detect invalid tag timestamp2
+
+cat >tag.sig <<EOF
+object $head
+type commit
+tag mytag
+tagger T A Gger <tagger@example.com> 2008-03-31T12:20:15-0500
+
+EOF
+
+check_verify_failure 'detect invalid tag timestamp2' \
+ '^error:.* badDate:'
+
+############################################################
+# 20. detect invalid tag timezone1
+
+cat >tag.sig <<EOF
+object $head
+type commit
+tag mytag
+tagger T A Gger <tagger@example.com> 1206478233 GMT
+
+EOF
+
+check_verify_failure 'detect invalid tag timezone1' \
+ '^error:.* badTimezone:'
+
+############################################################
+# 21. detect invalid tag timezone2
+
+cat >tag.sig <<EOF
+object $head
+type commit
+tag mytag
+tagger T A Gger <tagger@example.com> 1206478233 + 30
+
+EOF
+
+check_verify_failure 'detect invalid tag timezone2' \
+ '^error:.* badTimezone:'
+
+############################################################
+# 22. allow invalid tag timezone3 (the maximum is -1200/+1400)
+
+cat >tag.sig <<EOF
+object $head
+type commit
+tag mytag
+tagger T A Gger <tagger@example.com> 1206478233 -1430
+
+EOF
+
+test_expect_mktag_success 'allow invalid tag timezone'
+
+############################################################
+# 23. detect invalid header entry
+
+cat >tag.sig <<EOF
+object $head
+type commit
+tag mytag
+tagger T A Gger <tagger@example.com> 1206478233 -0500
+this line should not be here
+
+EOF
+
+check_verify_failure 'detect invalid header entry' \
+ '^error:.* extraHeaderEntry:' \
+ --no-strict \
+ --fsck-obj-ok
+
+test_expect_success 'invalid header entry config & fsck' '
+ test_must_fail git mktag <tag.sig &&
+ git mktag --no-strict <tag.sig &&
+
+ test_must_fail git -c fsck.extraHeaderEntry=error mktag <tag.sig &&
+ test_must_fail git -c fsck.extraHeaderEntry=error mktag --no-strict <tag.sig &&
+
+ test_must_fail git -c fsck.extraHeaderEntry=warn mktag <tag.sig &&
+ git -c fsck.extraHeaderEntry=warn mktag --no-strict <tag.sig &&
+
+ git -c fsck.extraHeaderEntry=ignore mktag <tag.sig &&
+ git -c fsck.extraHeaderEntry=ignore mktag --no-strict <tag.sig &&
+
+ git fsck &&
+ git -c fsck.extraHeaderEntry=warn fsck 2>err &&
+ grep "warning .*extraHeaderEntry:" err &&
+ test_must_fail git -c fsck.extraHeaderEntry=error 2>err fsck &&
+ grep "error .* extraHeaderEntry:" err
+'
+
+cat >tag.sig <<EOF
+object $head
+type commit
+tag mytag
+tagger T A Gger <tagger@example.com> 1206478233 -0500
+
+
+this line comes after an extra newline
+EOF
+
+test_expect_mktag_success 'allow extra newlines at start of body'
+
+cat >tag.sig <<EOF
+object $head
+type commit
+tag mytag
+tagger T A Gger <tagger@example.com> 1206478233 -0500
+
+EOF
+
+test_expect_mktag_success 'allow a blank line before an empty body (1)'
+
+cat >tag.sig <<EOF
+object $head
+type commit
+tag mytag
+tagger T A Gger <tagger@example.com> 1206478233 -0500
+EOF
+
+test_expect_mktag_success 'allow no blank line before an empty body (2)'
+
+############################################################
+# 24. create valid tag
+
+cat >tag.sig <<EOF
+object $head
+type commit
+tag mytag
+tagger T A Gger <tagger@example.com> 1206478233 -0500
+EOF
+
+test_expect_mktag_success 'create valid tag object'
+
+test_done
diff --git a/t/t3900-i18n-commit.sh b/t/t3900-i18n-commit.sh
new file mode 100755
index 0000000..f27d09c
--- /dev/null
+++ b/t/t3900-i18n-commit.sh
@@ -0,0 +1,240 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Junio C Hamano
+#
+
+test_description='commit and log output encodings'
+
+. ./test-lib.sh
+
+compare_with () {
+ git show -s $1 | sed -e '1,/^$/d' -e 's/^ //' >current &&
+ case "$3" in
+ '')
+ test_cmp "$2" current ;;
+ ?*)
+ iconv -f "$3" -t UTF-8 >current.utf8 <current &&
+ iconv -f "$3" -t UTF-8 >expect.utf8 <"$2" &&
+ test_cmp expect.utf8 current.utf8
+ ;;
+ esac
+}
+
+test_expect_success setup '
+ : >F &&
+ git add F &&
+ T=$(git write-tree) &&
+ C=$(git commit-tree $T <"$TEST_DIRECTORY"/t3900/1-UTF-8.txt) &&
+ git update-ref HEAD $C &&
+ git tag C0
+'
+
+test_expect_success 'no encoding header for base case' '
+ E=$(git cat-file commit C0 | sed -ne "s/^encoding //p") &&
+ test z = "z$E"
+'
+
+test_expect_success 'UTF-16 refused because of NULs' '
+ echo UTF-16 >F &&
+ test_must_fail git commit -a -F "$TEST_DIRECTORY"/t3900/UTF-16.txt
+'
+
+test_expect_success 'UTF-8 invalid characters refused' '
+ test_when_finished "rm -f \"\$HOME/stderr\" \"\$HOME/invalid\"" &&
+ echo "UTF-8 characters" >F &&
+ printf "Commit message\n\nInvalid surrogate:\355\240\200\n" \
+ >"$HOME/invalid" &&
+ git commit -a -F "$HOME/invalid" 2>"$HOME"/stderr &&
+ test_grep "did not conform" "$HOME"/stderr
+'
+
+test_expect_success 'UTF-8 overlong sequences rejected' '
+ test_when_finished "rm -f \"\$HOME/stderr\" \"\$HOME/invalid\"" &&
+ rm -f "$HOME/stderr" "$HOME/invalid" &&
+ echo "UTF-8 overlong" >F &&
+ printf "\340\202\251ommit message\n\nThis is not a space:\300\240\n" \
+ >"$HOME/invalid" &&
+ git commit -a -F "$HOME/invalid" 2>"$HOME"/stderr &&
+ test_grep "did not conform" "$HOME"/stderr
+'
+
+test_expect_success 'UTF-8 non-characters refused' '
+ test_when_finished "rm -f \"\$HOME/stderr\" \"\$HOME/invalid\"" &&
+ echo "UTF-8 non-character 1" >F &&
+ printf "Commit message\n\nNon-character:\364\217\277\276\n" \
+ >"$HOME/invalid" &&
+ git commit -a -F "$HOME/invalid" 2>"$HOME"/stderr &&
+ test_grep "did not conform" "$HOME"/stderr
+'
+
+test_expect_success 'UTF-8 non-characters refused' '
+ test_when_finished "rm -f \"\$HOME/stderr\" \"\$HOME/invalid\"" &&
+ echo "UTF-8 non-character 2." >F &&
+ printf "Commit message\n\nNon-character:\357\267\220\n" \
+ >"$HOME/invalid" &&
+ git commit -a -F "$HOME/invalid" 2>"$HOME"/stderr &&
+ test_grep "did not conform" "$HOME"/stderr
+'
+
+for H in ISO8859-1 eucJP ISO-2022-JP
+do
+ test_expect_success "$H setup" '
+ git config i18n.commitencoding $H &&
+ git checkout -b $H C0 &&
+ echo $H >F &&
+ git commit -a -F "$TEST_DIRECTORY"/t3900/$H.txt
+ '
+done
+
+for H in ISO8859-1 eucJP ISO-2022-JP
+do
+ test_expect_success "check encoding header for $H" '
+ E=$(git cat-file commit '$H' | sed -ne "s/^encoding //p") &&
+ test "z$E" = "z'$H'"
+ '
+done
+
+test_expect_success 'config to remove customization' '
+ git config --unset-all i18n.commitencoding &&
+ if Z=$(git config --get-all i18n.commitencoding)
+ then
+ echo Oops, should have failed.
+ false
+ else
+ test z = "z$Z"
+ fi &&
+ git config i18n.commitencoding UTF-8
+'
+
+test_expect_success 'ISO8859-1 should be shown in UTF-8 now' '
+ compare_with ISO8859-1 "$TEST_DIRECTORY"/t3900/1-UTF-8.txt
+'
+
+for H in eucJP ISO-2022-JP
+do
+ test_expect_success "$H should be shown in UTF-8 now" '
+ compare_with '$H' "$TEST_DIRECTORY"/t3900/2-UTF-8.txt
+ '
+done
+
+test_expect_success 'config to add customization' '
+ git config --unset-all i18n.commitencoding &&
+ if Z=$(git config --get-all i18n.commitencoding)
+ then
+ echo Oops, should have failed.
+ false
+ else
+ test z = "z$Z"
+ fi
+'
+
+for H in ISO8859-1 eucJP ISO-2022-JP
+do
+ test_expect_success "$H should be shown in itself now" '
+ git config i18n.commitencoding '$H' &&
+ compare_with '$H' "$TEST_DIRECTORY"/t3900/'$H'.txt
+ '
+done
+
+test_expect_success 'config to tweak customization' '
+ git config i18n.logoutputencoding UTF-8
+'
+
+test_expect_success 'ISO8859-1 should be shown in UTF-8 now' '
+ compare_with ISO8859-1 "$TEST_DIRECTORY"/t3900/1-UTF-8.txt
+'
+
+for H in eucJP ISO-2022-JP
+do
+ test_expect_success "$H should be shown in UTF-8 now" '
+ compare_with '$H' "$TEST_DIRECTORY"/t3900/2-UTF-8.txt
+ '
+done
+
+for J in eucJP ISO-2022-JP
+do
+ if test "$J" = ISO-2022-JP
+ then
+ ICONV=$J
+ else
+ ICONV=
+ fi
+ git config i18n.logoutputencoding $J
+ for H in eucJP ISO-2022-JP
+ do
+ test_expect_success "$H should be shown in $J now" '
+ compare_with '$H' "$TEST_DIRECTORY"/t3900/'$J'.txt $ICONV
+ '
+ done
+done
+
+for H in ISO8859-1 eucJP ISO-2022-JP
+do
+ test_expect_success "No conversion with $H" '
+ compare_with "--encoding=none '$H'" "$TEST_DIRECTORY"/t3900/'$H'.txt
+ '
+done
+
+test_commit_autosquash_flags () {
+ H=$1
+ flag=$2
+ test_expect_success "commit --$flag with $H encoding" '
+ git config i18n.commitencoding $H &&
+ git checkout -b $H-$flag C0 &&
+ echo $H >>F &&
+ git commit -a -F "$TEST_DIRECTORY"/t3900/$H.txt &&
+ test_tick &&
+ echo intermediate stuff >>G &&
+ git add G &&
+ git commit -a -m "intermediate commit" &&
+ test_tick &&
+ echo $H $flag >>F &&
+ git commit -a --$flag HEAD~1 &&
+ E=$(git cat-file commit '$H-$flag' |
+ sed -ne "s/^encoding //p") &&
+ test "z$E" = "z$H" &&
+ git config --unset-all i18n.commitencoding &&
+ git rebase --autosquash -i HEAD^^^ &&
+ git log --oneline >actual &&
+ test_line_count = 3 actual
+ '
+}
+
+test_commit_autosquash_flags eucJP fixup
+
+test_commit_autosquash_flags ISO-2022-JP squash
+
+test_commit_autosquash_multi_encoding () {
+ flag=$1
+ old=$2
+ new=$3
+ msg=$4
+ test_expect_success "commit --$flag into $old from $new" '
+ git checkout -b $flag-$old-$new C0 &&
+ git config i18n.commitencoding $old &&
+ echo $old >>F &&
+ git commit -a -F "$TEST_DIRECTORY"/t3900/$msg &&
+ test_tick &&
+ echo intermediate stuff >>G &&
+ git add G &&
+ git commit -a -m "intermediate commit" &&
+ test_tick &&
+ git config i18n.commitencoding $new &&
+ echo $new-$flag >>F &&
+ git commit -a --$flag HEAD^ &&
+ git rebase --autosquash -i HEAD^^^ &&
+ git rev-list HEAD >actual &&
+ test_line_count = 3 actual &&
+ iconv -f $old -t UTF-8 "$TEST_DIRECTORY"/t3900/$msg >expect &&
+ git cat-file commit HEAD^ >raw &&
+ (sed "1,/^$/d" raw | iconv -f $new -t utf-8) >actual &&
+ test_cmp expect actual
+ '
+}
+
+test_commit_autosquash_multi_encoding fixup UTF-8 ISO-8859-1 1-UTF-8.txt
+test_commit_autosquash_multi_encoding squash ISO-8859-1 UTF-8 ISO8859-1.txt
+test_commit_autosquash_multi_encoding squash eucJP ISO-2022-JP eucJP.txt
+test_commit_autosquash_multi_encoding fixup ISO-2022-JP UTF-8 ISO-2022-JP.txt
+
+test_done
diff --git a/t/t3900/1-UTF-8.txt b/t/t3900/1-UTF-8.txt
new file mode 100644
index 0000000..ee31e19
--- /dev/null
+++ b/t/t3900/1-UTF-8.txt
@@ -0,0 +1,3 @@
+ÄËÑÃÖ
+
+Ãbçdèfg
diff --git a/t/t3900/2-UTF-8.txt b/t/t3900/2-UTF-8.txt
new file mode 100644
index 0000000..63f4f8f
--- /dev/null
+++ b/t/t3900/2-UTF-8.txt
@@ -0,0 +1,4 @@
+ã¯ã‚Œã²ã»ãµ
+
+ã—ã¦ã„ã‚‹ã®ãŒã€ã„ã‚‹ã®ã§ã€‚
+濱浜ã»ã‚Œã·ã‚Šã½ã‚Œã¾ã³ãã‚Šã‚ã¸ã€‚
diff --git a/t/t3900/ISO-2022-JP.txt b/t/t3900/ISO-2022-JP.txt
new file mode 100644
index 0000000..74b5330
--- /dev/null
+++ b/t/t3900/ISO-2022-JP.txt
@@ -0,0 +1,4 @@
+$B$O$l$R$[$U(B
+
+$B$7$F$$$k$N$,!"$$$k$N$G!#(B
+$B_@IM$[$l$W$j$]$l$^$S$0$j$m$X!#(B
diff --git a/t/t3900/ISO8859-1.txt b/t/t3900/ISO8859-1.txt
new file mode 100644
index 0000000..7cbef0e
--- /dev/null
+++ b/t/t3900/ISO8859-1.txt
@@ -0,0 +1,3 @@
+ÄËÑÏÖ
+
+Ábçdèfg
diff --git a/t/t3900/UTF-16.txt b/t/t3900/UTF-16.txt
new file mode 100644
index 0000000..2257f05
--- /dev/null
+++ b/t/t3900/UTF-16.txt
Binary files differ
diff --git a/t/t3900/eucJP.txt b/t/t3900/eucJP.txt
new file mode 100644
index 0000000..546f2aa
--- /dev/null
+++ b/t/t3900/eucJP.txt
@@ -0,0 +1,4 @@
+¤Ï¤ì¤Ò¤Û¤Õ
+
+¤·¤Æ¤¤¤ë¤Î¤¬¡¢¤¤¤ë¤Î¤Ç¡£
+ßÀÉͤۤì¤×¤ê¤Ý¤ì¤Þ¤Ó¤°¤ê¤í¤Ø¡£
diff --git a/t/t3901-i18n-patch.sh b/t/t3901-i18n-patch.sh
new file mode 100755
index 0000000..4b37f78
--- /dev/null
+++ b/t/t3901-i18n-patch.sh
@@ -0,0 +1,319 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Junio C Hamano
+#
+
+test_description='i18n settings and format-patch | am pipe'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+check_encoding () {
+ # Make sure characters are not corrupted
+ cnt="$1" header="$2" i=1 j=0
+ while test "$i" -le $cnt
+ do
+ git format-patch --encoding=UTF-8 --stdout HEAD~$i..HEAD~$j |
+ grep "^From: =?UTF-8?q?=C3=81=C3=A9=C3=AD=20=C3=B3=C3=BA?=" &&
+ git cat-file commit HEAD~$j |
+ case "$header" in
+ 8859)
+ grep "^encoding ISO8859-1" ;;
+ *)
+ grep "^encoding ISO8859-1"; test "$?" != 0 ;;
+ esac || return 1
+ j=$i
+ i=$(($i+1))
+ done
+}
+
+test_expect_success setup '
+ git config i18n.commitencoding UTF-8 &&
+
+ # use UTF-8 in author and committer name to match the
+ # i18n.commitencoding settings
+ . "$TEST_DIRECTORY"/t3901/utf8.txt &&
+
+ test_tick &&
+ echo "$GIT_AUTHOR_NAME" >mine &&
+ git add mine &&
+ git commit -s -m "Initial commit" &&
+
+ test_tick &&
+ echo Hello world >mine &&
+ git add mine &&
+ git commit -s -m "Second on main" &&
+
+ # the first commit on the side branch is UTF-8
+ test_tick &&
+ git checkout -b side main^ &&
+ echo Another file >yours &&
+ git add yours &&
+ git commit -s -m "Second on side" &&
+
+ if test_have_prereq !MINGW
+ then
+ # the second one on the side branch is ISO-8859-1
+ git config i18n.commitencoding ISO8859-1 &&
+ # use author and committer name in ISO-8859-1 to match it.
+ . "$TEST_DIRECTORY"/t3901/8859-1.txt
+ fi &&
+ test_tick &&
+ echo Yet another >theirs &&
+ git add theirs &&
+ git commit -s -m "Third on side" &&
+
+ # Back to default
+ git config i18n.commitencoding UTF-8
+'
+
+test_expect_success 'format-patch output (ISO-8859-1)' '
+ git config i18n.logoutputencoding ISO8859-1 &&
+
+ git format-patch --stdout main..HEAD^ >out-l1 &&
+ git format-patch --stdout HEAD^ >out-l2 &&
+ grep "^Content-Type: text/plain; charset=ISO8859-1" out-l1 &&
+ grep "^From: =?ISO8859-1?q?=C1=E9=ED=20=F3=FA?=" out-l1 &&
+ grep "^Content-Type: text/plain; charset=ISO8859-1" out-l2 &&
+ grep "^From: =?ISO8859-1?q?=C1=E9=ED=20=F3=FA?=" out-l2
+'
+
+test_expect_success 'format-patch output (UTF-8)' '
+ git config i18n.logoutputencoding UTF-8 &&
+
+ git format-patch --stdout main..HEAD^ >out-u1 &&
+ git format-patch --stdout HEAD^ >out-u2 &&
+ grep "^Content-Type: text/plain; charset=UTF-8" out-u1 &&
+ grep "^From: =?UTF-8?q?=C3=81=C3=A9=C3=AD=20=C3=B3=C3=BA?=" out-u1 &&
+ grep "^Content-Type: text/plain; charset=UTF-8" out-u2 &&
+ grep "^From: =?UTF-8?q?=C3=81=C3=A9=C3=AD=20=C3=B3=C3=BA?=" out-u2
+'
+
+test_expect_success 'rebase (U/U)' '
+ # We want the result of rebase in UTF-8
+ git config i18n.commitencoding UTF-8 &&
+
+ # The test is about logoutputencoding not affecting the
+ # final outcome -- it is used internally to generate the
+ # patch and the log.
+
+ git config i18n.logoutputencoding UTF-8 &&
+
+ # The result will be committed by GIT_COMMITTER_NAME --
+ # we want UTF-8 encoded name.
+ . "$TEST_DIRECTORY"/t3901/utf8.txt &&
+ git checkout -b test &&
+ git rebase main &&
+
+ check_encoding 2
+'
+
+test_expect_success 'rebase (U/L)' '
+ git config i18n.commitencoding UTF-8 &&
+ git config i18n.logoutputencoding ISO8859-1 &&
+ . "$TEST_DIRECTORY"/t3901/utf8.txt &&
+
+ git reset --hard side &&
+ git rebase main &&
+
+ check_encoding 2
+'
+
+test_expect_success !MINGW 'rebase (L/L)' '
+ # In this test we want ISO-8859-1 encoded commits as the result
+ git config i18n.commitencoding ISO8859-1 &&
+ git config i18n.logoutputencoding ISO8859-1 &&
+ . "$TEST_DIRECTORY"/t3901/8859-1.txt &&
+
+ git reset --hard side &&
+ git rebase main &&
+
+ check_encoding 2 8859
+'
+
+test_expect_success !MINGW 'rebase (L/U)' '
+ # This is pathological -- use UTF-8 as intermediate form
+ # to get ISO-8859-1 results.
+ git config i18n.commitencoding ISO8859-1 &&
+ git config i18n.logoutputencoding UTF-8 &&
+ . "$TEST_DIRECTORY"/t3901/8859-1.txt &&
+
+ git reset --hard side &&
+ git rebase main &&
+
+ check_encoding 2 8859
+'
+
+test_expect_success 'cherry-pick(U/U)' '
+ # Both the commitencoding and logoutputencoding is set to UTF-8.
+
+ git config i18n.commitencoding UTF-8 &&
+ git config i18n.logoutputencoding UTF-8 &&
+ . "$TEST_DIRECTORY"/t3901/utf8.txt &&
+
+ git reset --hard main &&
+ git cherry-pick side^ &&
+ git cherry-pick side &&
+ git revert HEAD &&
+
+ check_encoding 3
+'
+
+test_expect_success !MINGW 'cherry-pick(L/L)' '
+ # Both the commitencoding and logoutputencoding is set to ISO-8859-1
+
+ git config i18n.commitencoding ISO8859-1 &&
+ git config i18n.logoutputencoding ISO8859-1 &&
+ . "$TEST_DIRECTORY"/t3901/8859-1.txt &&
+
+ git reset --hard main &&
+ git cherry-pick side^ &&
+ git cherry-pick side &&
+ git revert HEAD &&
+
+ check_encoding 3 8859
+'
+
+test_expect_success 'cherry-pick(U/L)' '
+ # Commitencoding is set to UTF-8 but logoutputencoding is ISO-8859-1
+
+ git config i18n.commitencoding UTF-8 &&
+ git config i18n.logoutputencoding ISO8859-1 &&
+ . "$TEST_DIRECTORY"/t3901/utf8.txt &&
+
+ git reset --hard main &&
+ git cherry-pick side^ &&
+ git cherry-pick side &&
+ git revert HEAD &&
+
+ check_encoding 3
+'
+
+test_expect_success !MINGW 'cherry-pick(L/U)' '
+ # Again, the commitencoding is set to ISO-8859-1 but
+ # logoutputencoding is set to UTF-8.
+
+ git config i18n.commitencoding ISO8859-1 &&
+ git config i18n.logoutputencoding UTF-8 &&
+ . "$TEST_DIRECTORY"/t3901/8859-1.txt &&
+
+ git reset --hard main &&
+ git cherry-pick side^ &&
+ git cherry-pick side &&
+ git revert HEAD &&
+
+ check_encoding 3 8859
+'
+
+test_expect_success 'rebase --merge (U/U)' '
+ git config i18n.commitencoding UTF-8 &&
+ git config i18n.logoutputencoding UTF-8 &&
+ . "$TEST_DIRECTORY"/t3901/utf8.txt &&
+
+ git reset --hard side &&
+ git rebase --merge main &&
+
+ check_encoding 2
+'
+
+test_expect_success 'rebase --merge (U/L)' '
+ git config i18n.commitencoding UTF-8 &&
+ git config i18n.logoutputencoding ISO8859-1 &&
+ . "$TEST_DIRECTORY"/t3901/utf8.txt &&
+
+ git reset --hard side &&
+ git rebase --merge main &&
+
+ check_encoding 2
+'
+
+test_expect_success 'rebase --merge (L/L)' '
+ # In this test we want ISO-8859-1 encoded commits as the result
+ git config i18n.commitencoding ISO8859-1 &&
+ git config i18n.logoutputencoding ISO8859-1 &&
+ . "$TEST_DIRECTORY"/t3901/8859-1.txt &&
+
+ git reset --hard side &&
+ git rebase --merge main &&
+
+ check_encoding 2 8859
+'
+
+test_expect_success 'rebase --merge (L/U)' '
+ # This is pathological -- use UTF-8 as intermediate form
+ # to get ISO-8859-1 results.
+ git config i18n.commitencoding ISO8859-1 &&
+ git config i18n.logoutputencoding UTF-8 &&
+ . "$TEST_DIRECTORY"/t3901/8859-1.txt &&
+
+ git reset --hard side &&
+ git rebase --merge main &&
+
+ check_encoding 2 8859
+'
+
+test_expect_success 'am (U/U)' '
+ # Apply UTF-8 patches with UTF-8 commitencoding
+ git config i18n.commitencoding UTF-8 &&
+ . "$TEST_DIRECTORY"/t3901/utf8.txt &&
+
+ git reset --hard main &&
+ git am out-u1 out-u2 &&
+
+ check_encoding 2
+'
+
+test_expect_success !MINGW 'am (L/L)' '
+ # Apply ISO-8859-1 patches with ISO-8859-1 commitencoding
+ git config i18n.commitencoding ISO8859-1 &&
+ . "$TEST_DIRECTORY"/t3901/8859-1.txt &&
+
+ git reset --hard main &&
+ git am out-l1 out-l2 &&
+
+ check_encoding 2 8859
+'
+
+test_expect_success 'am (U/L)' '
+ # Apply ISO-8859-1 patches with UTF-8 commitencoding
+ git config i18n.commitencoding UTF-8 &&
+ . "$TEST_DIRECTORY"/t3901/utf8.txt &&
+ git reset --hard main &&
+
+ # am specifies --utf8 by default.
+ git am out-l1 out-l2 &&
+
+ check_encoding 2
+'
+
+test_expect_success 'am --no-utf8 (U/L)' '
+ # Apply ISO-8859-1 patches with UTF-8 commitencoding
+ git config i18n.commitencoding UTF-8 &&
+ . "$TEST_DIRECTORY"/t3901/utf8.txt &&
+
+ git reset --hard main &&
+ git am --no-utf8 out-l1 out-l2 2>err &&
+
+ # commit-tree will warn that the commit message does not contain valid UTF-8
+ # as mailinfo did not convert it
+ test_grep "did not conform" err &&
+
+ check_encoding 2
+'
+
+test_expect_success !MINGW 'am (L/U)' '
+ # Apply UTF-8 patches with ISO-8859-1 commitencoding
+ git config i18n.commitencoding ISO8859-1 &&
+ . "$TEST_DIRECTORY"/t3901/8859-1.txt &&
+
+ git reset --hard main &&
+ # mailinfo will re-code the commit message to the charset specified by
+ # i18n.commitencoding
+ git am out-u1 out-u2 &&
+
+ check_encoding 2 8859
+'
+
+test_done
diff --git a/t/t3901/8859-1.txt b/t/t3901/8859-1.txt
new file mode 100755
index 0000000..38c21a6
--- /dev/null
+++ b/t/t3901/8859-1.txt
@@ -0,0 +1,4 @@
+: to be sourced in t3901 -- this is latin-1
+GIT_AUTHOR_NAME="Áéí óú" &&
+GIT_COMMITTER_NAME=$GIT_AUTHOR_NAME &&
+export GIT_AUTHOR_NAME GIT_COMMITTER_NAME
diff --git a/t/t3901/utf8.txt b/t/t3901/utf8.txt
new file mode 100755
index 0000000..5f5205c
--- /dev/null
+++ b/t/t3901/utf8.txt
@@ -0,0 +1,4 @@
+: to be sourced in t3901 -- this is utf8
+GIT_AUTHOR_NAME="Ãéí óú" &&
+GIT_COMMITTER_NAME=$GIT_AUTHOR_NAME &&
+export GIT_AUTHOR_NAME GIT_COMMITTER_NAME
diff --git a/t/t3902-quoted.sh b/t/t3902-quoted.sh
new file mode 100755
index 0000000..72a5a56
--- /dev/null
+++ b/t/t3902-quoted.sh
@@ -0,0 +1,153 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Junio C Hamano
+#
+
+test_description='quoted output'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+FN='濱野'
+GN='ç´”'
+HT=' '
+DQ='"'
+
+test_have_prereq MINGW ||
+echo foo 2>/dev/null > "Name and an${HT}HT"
+if ! test -f "Name and an${HT}HT"
+then
+ # FAT/NTFS does not allow tabs in filenames
+ skip_all='Your filesystem does not allow tabs in filenames'
+ test_done
+fi
+
+for_each_name () {
+ for name in \
+ Name "Name and a${LF}LF" "Name and an${HT}HT" "Name${DQ}" \
+ "$FN$HT$GN" "$FN$LF$GN" "$FN $GN" "$FN$GN" "$FN$DQ$GN" \
+ "With SP in it" "$FN/file"
+ do
+ eval "$1"
+ done
+}
+
+test_expect_success 'setup' '
+
+ mkdir "$FN" &&
+ for_each_name "echo initial >\"\$name\"" &&
+ git add . &&
+ git commit -q -m Initial &&
+
+ for_each_name "echo second >\"\$name\"" &&
+ git commit -a -m Second &&
+
+ for_each_name "echo modified >\"\$name\""
+
+'
+
+test_expect_success 'setup expected files' '
+cat >expect.quoted <<\EOF &&
+Name
+"Name and a\nLF"
+"Name and an\tHT"
+"Name\""
+With SP in it
+"\346\277\261\351\207\216\t\347\264\224"
+"\346\277\261\351\207\216\n\347\264\224"
+"\346\277\261\351\207\216 \347\264\224"
+"\346\277\261\351\207\216\"\347\264\224"
+"\346\277\261\351\207\216/file"
+"\346\277\261\351\207\216\347\264\224"
+EOF
+
+cat >expect.raw <<\EOF
+Name
+"Name and a\nLF"
+"Name and an\tHT"
+"Name\""
+With SP in it
+"濱野\t純"
+"濱野\n純"
+濱野 純
+"濱野\"純"
+濱野/file
+濱野純
+EOF
+'
+
+test_expect_success 'check fully quoted output from ls-files' '
+
+ git ls-files >current && test_cmp expect.quoted current
+
+'
+
+test_expect_success 'check fully quoted output from diff-files' '
+
+ git diff --name-only >current &&
+ test_cmp expect.quoted current
+
+'
+
+test_expect_success 'check fully quoted output from diff-index' '
+
+ git diff --name-only HEAD >current &&
+ test_cmp expect.quoted current
+
+'
+
+test_expect_success 'check fully quoted output from diff-tree' '
+
+ git diff --name-only HEAD^ HEAD >current &&
+ test_cmp expect.quoted current
+
+'
+
+test_expect_success 'check fully quoted output from ls-tree' '
+
+ git ls-tree --name-only -r HEAD >current &&
+ test_cmp expect.quoted current
+
+'
+
+test_expect_success 'setting core.quotepath' '
+
+ git config --bool core.quotepath false
+
+'
+
+test_expect_success 'check fully quoted output from ls-files' '
+
+ git ls-files >current && test_cmp expect.raw current
+
+'
+
+test_expect_success 'check fully quoted output from diff-files' '
+
+ git diff --name-only >current &&
+ test_cmp expect.raw current
+
+'
+
+test_expect_success 'check fully quoted output from diff-index' '
+
+ git diff --name-only HEAD >current &&
+ test_cmp expect.raw current
+
+'
+
+test_expect_success 'check fully quoted output from diff-tree' '
+
+ git diff --name-only HEAD^ HEAD >current &&
+ test_cmp expect.raw current
+
+'
+
+test_expect_success 'check fully quoted output from ls-tree' '
+
+ git ls-tree --name-only -r HEAD >current &&
+ test_cmp expect.raw current
+
+'
+
+test_done
diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
new file mode 100755
index 0000000..34faeac
--- /dev/null
+++ b/t/t3903-stash.sh
@@ -0,0 +1,1519 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Johannes E Schindelin
+#
+
+test_description='Test git stash'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-unique-files.sh
+
+test_expect_success 'usage on cmd and subcommand invalid option' '
+ test_expect_code 129 git stash --invalid-option 2>usage &&
+ grep "or: git stash" usage &&
+
+ test_expect_code 129 git stash push --invalid-option 2>usage &&
+ ! grep "or: git stash" usage
+'
+
+test_expect_success 'usage on main command -h emits a summary of subcommands' '
+ test_expect_code 129 git stash -h >usage &&
+ grep -F "usage: git stash list" usage &&
+ grep -F "or: git stash show" usage
+'
+
+test_expect_success 'usage for subcommands should emit subcommand usage' '
+ test_expect_code 129 git stash push -h >usage &&
+ grep -F "usage: git stash [push" usage
+'
+
+diff_cmp () {
+ for i in "$1" "$2"
+ do
+ sed -e 's/^index 0000000\.\.[0-9a-f]*/index 0000000..1234567/' \
+ -e 's/^index [0-9a-f]*\.\.[0-9a-f]*/index 1234567..89abcde/' \
+ -e 's/^index [0-9a-f]*,[0-9a-f]*\.\.[0-9a-f]*/index 1234567,7654321..89abcde/' \
+ "$i" >"$i.compare" || return 1
+ done &&
+ test_cmp "$1.compare" "$2.compare" &&
+ rm -f "$1.compare" "$2.compare"
+}
+
+setup_stash() {
+ echo 1 >file &&
+ git add file &&
+ echo unrelated >other-file &&
+ git add other-file &&
+ test_tick &&
+ git commit -m initial &&
+ echo 2 >file &&
+ git add file &&
+ echo 3 >file &&
+ test_tick &&
+ git stash &&
+ git diff-files --quiet &&
+ git diff-index --cached --quiet HEAD
+}
+
+test_expect_success 'stash some dirty working directory' '
+ setup_stash
+'
+
+cat >expect <<EOF
+diff --git a/file b/file
+index 0cfbf08..00750ed 100644
+--- a/file
++++ b/file
+@@ -1 +1 @@
+-2
++3
+EOF
+
+test_expect_success 'parents of stash' '
+ test $(git rev-parse stash^) = $(git rev-parse HEAD) &&
+ git diff stash^2..stash >output &&
+ diff_cmp expect output
+'
+
+test_expect_success 'applying bogus stash does nothing' '
+ test_must_fail git stash apply stash@{1} &&
+ echo 1 >expect &&
+ test_cmp expect file
+'
+
+test_expect_success 'apply does not need clean working directory' '
+ echo 4 >other-file &&
+ git stash apply &&
+ echo 3 >expect &&
+ test_cmp expect file
+'
+
+test_expect_success 'apply does not clobber working directory changes' '
+ git reset --hard &&
+ echo 4 >file &&
+ test_must_fail git stash apply &&
+ echo 4 >expect &&
+ test_cmp expect file
+'
+
+test_expect_success 'apply stashed changes' '
+ git reset --hard &&
+ echo 5 >other-file &&
+ git add other-file &&
+ test_tick &&
+ git commit -m other-file &&
+ git stash apply &&
+ test 3 = $(cat file) &&
+ test 1 = $(git show :file) &&
+ test 1 = $(git show HEAD:file)
+'
+
+test_expect_success 'apply stashed changes (including index)' '
+ git reset --hard HEAD^ &&
+ echo 6 >other-file &&
+ git add other-file &&
+ test_tick &&
+ git commit -m other-file &&
+ git stash apply --index &&
+ test 3 = $(cat file) &&
+ test 2 = $(git show :file) &&
+ test 1 = $(git show HEAD:file)
+'
+
+test_expect_success 'unstashing in a subdirectory' '
+ git reset --hard HEAD &&
+ mkdir subdir &&
+ (
+ cd subdir &&
+ git stash apply
+ )
+'
+
+test_expect_success 'stash drop complains of extra options' '
+ test_must_fail git stash drop --foo
+'
+
+test_expect_success 'drop top stash' '
+ git reset --hard &&
+ git stash list >expected &&
+ echo 7 >file &&
+ git stash &&
+ git stash drop &&
+ git stash list >actual &&
+ test_cmp expected actual &&
+ git stash apply &&
+ test 3 = $(cat file) &&
+ test 1 = $(git show :file) &&
+ test 1 = $(git show HEAD:file)
+'
+
+test_expect_success 'drop middle stash' '
+ git reset --hard &&
+ echo 8 >file &&
+ git stash &&
+ echo 9 >file &&
+ git stash &&
+ git stash drop stash@{1} &&
+ test 2 = $(git stash list | wc -l) &&
+ git stash apply &&
+ test 9 = $(cat file) &&
+ test 1 = $(git show :file) &&
+ test 1 = $(git show HEAD:file) &&
+ git reset --hard &&
+ git stash drop &&
+ git stash apply &&
+ test 3 = $(cat file) &&
+ test 1 = $(git show :file) &&
+ test 1 = $(git show HEAD:file)
+'
+
+test_expect_success 'drop middle stash by index' '
+ git reset --hard &&
+ echo 8 >file &&
+ git stash &&
+ echo 9 >file &&
+ git stash &&
+ git stash drop 1 &&
+ test 2 = $(git stash list | wc -l) &&
+ git stash apply &&
+ test 9 = $(cat file) &&
+ test 1 = $(git show :file) &&
+ test 1 = $(git show HEAD:file) &&
+ git reset --hard &&
+ git stash drop &&
+ git stash apply &&
+ test 3 = $(cat file) &&
+ test 1 = $(git show :file) &&
+ test 1 = $(git show HEAD:file)
+'
+
+test_expect_success 'drop stash reflog updates refs/stash' '
+ git reset --hard &&
+ git rev-parse refs/stash >expect &&
+ echo 9 >file &&
+ git stash &&
+ git stash drop stash@{0} &&
+ git rev-parse refs/stash >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success REFFILES 'drop stash reflog updates refs/stash with rewrite' '
+ git init repo &&
+ (
+ cd repo &&
+ setup_stash
+ ) &&
+ echo 9 >repo/file &&
+
+ old_oid="$(git -C repo rev-parse stash@{0})" &&
+ git -C repo stash &&
+ new_oid="$(git -C repo rev-parse stash@{0})" &&
+
+ cat >expect <<-EOF &&
+ $(test_oid zero) $old_oid
+ $old_oid $new_oid
+ EOF
+ cut -d" " -f1-2 repo/.git/logs/refs/stash >actual &&
+ test_cmp expect actual &&
+
+ git -C repo stash drop stash@{1} &&
+ cut -d" " -f1-2 repo/.git/logs/refs/stash >actual &&
+ cat >expect <<-EOF &&
+ $(test_oid zero) $new_oid
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'stash pop' '
+ git reset --hard &&
+ git stash pop &&
+ test 3 = $(cat file) &&
+ test 1 = $(git show :file) &&
+ test 1 = $(git show HEAD:file) &&
+ test 0 = $(git stash list | wc -l)
+'
+
+cat >expect <<EOF
+diff --git a/file2 b/file2
+new file mode 100644
+index 0000000..1fe912c
+--- /dev/null
++++ b/file2
+@@ -0,0 +1 @@
++bar2
+EOF
+
+cat >expect1 <<EOF
+diff --git a/file b/file
+index 257cc56..5716ca5 100644
+--- a/file
++++ b/file
+@@ -1 +1 @@
+-foo
++bar
+EOF
+
+cat >expect2 <<EOF
+diff --git a/file b/file
+index 7601807..5716ca5 100644
+--- a/file
++++ b/file
+@@ -1 +1 @@
+-baz
++bar
+diff --git a/file2 b/file2
+new file mode 100644
+index 0000000..1fe912c
+--- /dev/null
++++ b/file2
+@@ -0,0 +1 @@
++bar2
+EOF
+
+test_expect_success 'stash branch' '
+ echo foo >file &&
+ git commit file -m first &&
+ echo bar >file &&
+ echo bar2 >file2 &&
+ git add file2 &&
+ git stash &&
+ echo baz >file &&
+ git commit file -m second &&
+ git stash branch stashbranch &&
+ test refs/heads/stashbranch = $(git symbolic-ref HEAD) &&
+ test $(git rev-parse HEAD) = $(git rev-parse main^) &&
+ git diff --cached >output &&
+ diff_cmp expect output &&
+ git diff >output &&
+ diff_cmp expect1 output &&
+ git add file &&
+ git commit -m alternate\ second &&
+ git diff main..stashbranch >output &&
+ diff_cmp output expect2 &&
+ test 0 = $(git stash list | wc -l)
+'
+
+test_expect_success 'apply -q is quiet' '
+ echo foo >file &&
+ git stash &&
+ git stash apply -q >output.out 2>&1 &&
+ test_must_be_empty output.out
+'
+
+test_expect_success 'apply --index -q is quiet' '
+ # Added file, deleted file, modified file all staged for commit
+ echo foo >new-file &&
+ echo test >file &&
+ git add new-file file &&
+ git rm other-file &&
+
+ git stash &&
+ git stash apply --index -q >output.out 2>&1 &&
+ test_must_be_empty output.out
+'
+
+test_expect_success 'save -q is quiet' '
+ git stash save --quiet >output.out 2>&1 &&
+ test_must_be_empty output.out
+'
+
+test_expect_success 'pop -q works and is quiet' '
+ git stash pop -q >output.out 2>&1 &&
+ echo bar >expect &&
+ git show :file >actual &&
+ test_cmp expect actual &&
+ test_must_be_empty output.out
+'
+
+test_expect_success 'pop -q --index works and is quiet' '
+ echo foo >file &&
+ git add file &&
+ git stash save --quiet &&
+ git stash pop -q --index >output.out 2>&1 &&
+ git diff-files file2 >file2.diff &&
+ test_must_be_empty file2.diff &&
+ test foo = "$(git show :file)" &&
+ test_must_be_empty output.out
+'
+
+test_expect_success 'drop -q is quiet' '
+ git stash &&
+ git stash drop -q >output.out 2>&1 &&
+ test_must_be_empty output.out
+'
+
+test_expect_success 'stash push -q --staged refreshes the index' '
+ git reset --hard &&
+ echo test >file &&
+ git add file &&
+ git stash push -q --staged &&
+ git diff-files >output.out &&
+ test_must_be_empty output.out
+'
+
+test_expect_success 'stash apply -q --index refreshes the index' '
+ echo test >other-file &&
+ git add other-file &&
+ echo another-change >other-file &&
+ git diff-files >expect &&
+ git stash &&
+
+ git stash apply -q --index &&
+ git diff-files >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'stash -k' '
+ echo bar3 >file &&
+ echo bar4 >file2 &&
+ git add file2 &&
+ git stash -k &&
+ test bar,bar4 = $(cat file),$(cat file2)
+'
+
+test_expect_success 'stash --no-keep-index' '
+ echo bar33 >file &&
+ echo bar44 >file2 &&
+ git add file2 &&
+ git stash --no-keep-index &&
+ test bar,bar2 = $(cat file),$(cat file2)
+'
+
+test_expect_success 'stash --staged' '
+ echo bar3 >file &&
+ echo bar4 >file2 &&
+ git add file2 &&
+ git stash --staged &&
+ test bar3,bar2 = $(cat file),$(cat file2) &&
+ git reset --hard &&
+ git stash pop &&
+ test bar,bar4 = $(cat file),$(cat file2)
+'
+
+test_expect_success 'dont assume push with non-option args' '
+ test_must_fail git stash -q drop 2>err &&
+ test_grep -e "subcommand wasn'\''t specified; '\''push'\'' can'\''t be assumed due to unexpected token '\''drop'\''" err
+'
+
+test_expect_success 'stash --invalid-option' '
+ echo bar5 >file &&
+ echo bar6 >file2 &&
+ git add file2 &&
+ test_must_fail git stash --invalid-option &&
+ test_must_fail git stash save --invalid-option &&
+ test bar5,bar6 = $(cat file),$(cat file2)
+'
+
+test_expect_success 'stash an added file' '
+ git reset --hard &&
+ echo new >file3 &&
+ git add file3 &&
+ git stash save "added file" &&
+ ! test -r file3 &&
+ git stash apply &&
+ test new = "$(cat file3)"
+'
+
+test_expect_success 'stash --intent-to-add file' '
+ git reset --hard &&
+ echo new >file4 &&
+ git add --intent-to-add file4 &&
+ test_when_finished "git rm -f file4" &&
+ test_must_fail git stash
+'
+
+test_expect_success 'stash rm then recreate' '
+ git reset --hard &&
+ git rm file &&
+ echo bar7 >file &&
+ git stash save "rm then recreate" &&
+ test bar = "$(cat file)" &&
+ git stash apply &&
+ test bar7 = "$(cat file)"
+'
+
+test_expect_success 'stash rm and ignore' '
+ git reset --hard &&
+ git rm file &&
+ echo file >.gitignore &&
+ git stash save "rm and ignore" &&
+ test bar = "$(cat file)" &&
+ test file = "$(cat .gitignore)" &&
+ git stash apply &&
+ ! test -r file &&
+ test file = "$(cat .gitignore)"
+'
+
+test_expect_success 'stash rm and ignore (stage .gitignore)' '
+ git reset --hard &&
+ git rm file &&
+ echo file >.gitignore &&
+ git add .gitignore &&
+ git stash save "rm and ignore (stage .gitignore)" &&
+ test bar = "$(cat file)" &&
+ ! test -r .gitignore &&
+ git stash apply &&
+ ! test -r file &&
+ test file = "$(cat .gitignore)"
+'
+
+test_expect_success SYMLINKS 'stash file to symlink' '
+ git reset --hard &&
+ rm file &&
+ ln -s file2 file &&
+ git stash save "file to symlink" &&
+ test_path_is_file_not_symlink file &&
+ test bar = "$(cat file)" &&
+ git stash apply &&
+ test_path_is_symlink file &&
+ test "$(test_readlink file)" = file2
+'
+
+test_expect_success SYMLINKS 'stash file to symlink (stage rm)' '
+ git reset --hard &&
+ git rm file &&
+ ln -s file2 file &&
+ git stash save "file to symlink (stage rm)" &&
+ test_path_is_file_not_symlink file &&
+ test bar = "$(cat file)" &&
+ git stash apply &&
+ test_path_is_symlink file &&
+ test "$(test_readlink file)" = file2
+'
+
+test_expect_success SYMLINKS 'stash file to symlink (full stage)' '
+ git reset --hard &&
+ rm file &&
+ ln -s file2 file &&
+ git add file &&
+ git stash save "file to symlink (full stage)" &&
+ test_path_is_file_not_symlink file &&
+ test bar = "$(cat file)" &&
+ git stash apply &&
+ test_path_is_symlink file &&
+ test "$(test_readlink file)" = file2
+'
+
+# This test creates a commit with a symlink used for the following tests
+
+test_expect_success 'stash symlink to file' '
+ git reset --hard &&
+ test_ln_s_add file filelink &&
+ git commit -m "Add symlink" &&
+ rm filelink &&
+ cp file filelink &&
+ git stash save "symlink to file"
+'
+
+test_expect_success SYMLINKS 'this must have re-created the symlink' '
+ test -h filelink &&
+ case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
+'
+
+test_expect_success 'unstash must re-create the file' '
+ git stash apply &&
+ ! test -h filelink &&
+ test bar = "$(cat file)"
+'
+
+test_expect_success 'stash symlink to file (stage rm)' '
+ git reset --hard &&
+ git rm filelink &&
+ cp file filelink &&
+ git stash save "symlink to file (stage rm)"
+'
+
+test_expect_success SYMLINKS 'this must have re-created the symlink' '
+ test -h filelink &&
+ case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
+'
+
+test_expect_success 'unstash must re-create the file' '
+ git stash apply &&
+ ! test -h filelink &&
+ test bar = "$(cat file)"
+'
+
+test_expect_success 'stash symlink to file (full stage)' '
+ git reset --hard &&
+ rm filelink &&
+ cp file filelink &&
+ git add filelink &&
+ git stash save "symlink to file (full stage)"
+'
+
+test_expect_success SYMLINKS 'this must have re-created the symlink' '
+ test -h filelink &&
+ case "$(ls -l filelink)" in *" filelink -> file") :;; *) false;; esac
+'
+
+test_expect_success 'unstash must re-create the file' '
+ git stash apply &&
+ ! test -h filelink &&
+ test bar = "$(cat file)"
+'
+
+test_expect_failure 'stash directory to file' '
+ git reset --hard &&
+ mkdir dir &&
+ echo foo >dir/file &&
+ git add dir/file &&
+ git commit -m "Add file in dir" &&
+ rm -fr dir &&
+ echo bar >dir &&
+ git stash save "directory to file" &&
+ test_path_is_dir dir &&
+ test foo = "$(cat dir/file)" &&
+ test_must_fail git stash apply &&
+ test bar = "$(cat dir)" &&
+ git reset --soft HEAD^
+'
+
+test_expect_failure 'stash file to directory' '
+ git reset --hard &&
+ rm file &&
+ mkdir file &&
+ echo foo >file/file &&
+ git stash save "file to directory" &&
+ test_path_is_file file &&
+ test bar = "$(cat file)" &&
+ git stash apply &&
+ test_path_is_file file/file &&
+ test foo = "$(cat file/file)"
+'
+
+test_expect_success 'giving too many ref arguments does not modify files' '
+ git stash clear &&
+ test_when_finished "git reset --hard HEAD" &&
+ echo foo >file2 &&
+ git stash &&
+ echo bar >file2 &&
+ git stash &&
+ test-tool chmtime =123456789 file2 &&
+ for type in apply pop "branch stash-branch"
+ do
+ test_must_fail git stash $type stash@{0} stash@{1} 2>err &&
+ test_grep "Too many revisions" err &&
+ test 123456789 = $(test-tool chmtime -g file2) || return 1
+ done
+'
+
+test_expect_success 'drop: too many arguments errors out (does nothing)' '
+ git stash list >expect &&
+ test_must_fail git stash drop stash@{0} stash@{1} 2>err &&
+ test_grep "Too many revisions" err &&
+ git stash list >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'show: too many arguments errors out (does nothing)' '
+ test_must_fail git stash show stash@{0} stash@{1} 2>err 1>out &&
+ test_grep "Too many revisions" err &&
+ test_must_be_empty out
+'
+
+test_expect_success 'stash create - no changes' '
+ git stash clear &&
+ test_when_finished "git reset --hard HEAD" &&
+ git reset --hard &&
+ git stash create >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'stash branch - no stashes on stack, stash-like argument' '
+ git stash clear &&
+ test_when_finished "git reset --hard HEAD" &&
+ git reset --hard &&
+ echo foo >>file &&
+ STASH_ID=$(git stash create) &&
+ git reset --hard &&
+ git stash branch stash-branch ${STASH_ID} &&
+ test_when_finished "git reset --hard HEAD && git checkout main &&
+ git branch -D stash-branch" &&
+ test $(git ls-files --modified | wc -l) -eq 1
+'
+
+test_expect_success 'stash branch - stashes on stack, stash-like argument' '
+ git stash clear &&
+ test_when_finished "git reset --hard HEAD" &&
+ git reset --hard &&
+ echo foo >>file &&
+ git stash &&
+ test_when_finished "git stash drop" &&
+ echo bar >>file &&
+ STASH_ID=$(git stash create) &&
+ git reset --hard &&
+ git stash branch stash-branch ${STASH_ID} &&
+ test_when_finished "git reset --hard HEAD && git checkout main &&
+ git branch -D stash-branch" &&
+ test $(git ls-files --modified | wc -l) -eq 1
+'
+
+test_expect_success 'stash branch complains with no arguments' '
+ test_must_fail git stash branch 2>err &&
+ test_grep "No branch name specified" err
+'
+
+test_expect_success 'stash show format defaults to --stat' '
+ git stash clear &&
+ test_when_finished "git reset --hard HEAD" &&
+ git reset --hard &&
+ echo foo >>file &&
+ git stash &&
+ test_when_finished "git stash drop" &&
+ echo bar >>file &&
+ STASH_ID=$(git stash create) &&
+ git reset --hard &&
+ cat >expected <<-EOF &&
+ file | 1 +
+ 1 file changed, 1 insertion(+)
+ EOF
+ git stash show ${STASH_ID} >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'stash show - stashes on stack, stash-like argument' '
+ git stash clear &&
+ test_when_finished "git reset --hard HEAD" &&
+ git reset --hard &&
+ echo foo >>file &&
+ git stash &&
+ test_when_finished "git stash drop" &&
+ echo bar >>file &&
+ STASH_ID=$(git stash create) &&
+ git reset --hard &&
+ echo "1 0 file" >expected &&
+ git stash show --numstat ${STASH_ID} >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'stash show -p - stashes on stack, stash-like argument' '
+ git stash clear &&
+ test_when_finished "git reset --hard HEAD" &&
+ git reset --hard &&
+ echo foo >>file &&
+ git stash &&
+ test_when_finished "git stash drop" &&
+ echo bar >>file &&
+ STASH_ID=$(git stash create) &&
+ git reset --hard &&
+ cat >expected <<-EOF &&
+ diff --git a/file b/file
+ index 7601807..935fbd3 100644
+ --- a/file
+ +++ b/file
+ @@ -1 +1,2 @@
+ baz
+ +bar
+ EOF
+ git stash show -p ${STASH_ID} >actual &&
+ diff_cmp expected actual
+'
+
+test_expect_success 'stash show - no stashes on stack, stash-like argument' '
+ git stash clear &&
+ test_when_finished "git reset --hard HEAD" &&
+ git reset --hard &&
+ echo foo >>file &&
+ STASH_ID=$(git stash create) &&
+ git reset --hard &&
+ echo "1 0 file" >expected &&
+ git stash show --numstat ${STASH_ID} >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'stash show -p - no stashes on stack, stash-like argument' '
+ git stash clear &&
+ test_when_finished "git reset --hard HEAD" &&
+ git reset --hard &&
+ echo foo >>file &&
+ STASH_ID=$(git stash create) &&
+ git reset --hard &&
+ cat >expected <<-EOF &&
+ diff --git a/file b/file
+ index 7601807..71b52c4 100644
+ --- a/file
+ +++ b/file
+ @@ -1 +1,2 @@
+ baz
+ +foo
+ EOF
+ git stash show -p ${STASH_ID} >actual &&
+ diff_cmp expected actual
+'
+
+test_expect_success 'stash show --patience shows diff' '
+ git reset --hard &&
+ echo foo >>file &&
+ STASH_ID=$(git stash create) &&
+ git reset --hard &&
+ cat >expected <<-EOF &&
+ diff --git a/file b/file
+ index 7601807..71b52c4 100644
+ --- a/file
+ +++ b/file
+ @@ -1 +1,2 @@
+ baz
+ +foo
+ EOF
+ git stash show --patience ${STASH_ID} >actual &&
+ diff_cmp expected actual
+'
+
+test_expect_success 'drop: fail early if specified stash is not a stash ref' '
+ git stash clear &&
+ test_when_finished "git reset --hard HEAD && git stash clear" &&
+ git reset --hard &&
+ echo foo >file &&
+ git stash &&
+ echo bar >file &&
+ git stash &&
+ test_must_fail git stash drop $(git rev-parse stash@{0}) &&
+ git stash pop &&
+ test bar = "$(cat file)" &&
+ git reset --hard HEAD
+'
+
+test_expect_success 'pop: fail early if specified stash is not a stash ref' '
+ git stash clear &&
+ test_when_finished "git reset --hard HEAD && git stash clear" &&
+ git reset --hard &&
+ echo foo >file &&
+ git stash &&
+ echo bar >file &&
+ git stash &&
+ test_must_fail git stash pop $(git rev-parse stash@{0}) &&
+ git stash pop &&
+ test bar = "$(cat file)" &&
+ git reset --hard HEAD
+'
+
+test_expect_success 'ref with non-existent reflog' '
+ git stash clear &&
+ echo bar5 >file &&
+ echo bar6 >file2 &&
+ git add file2 &&
+ git stash &&
+ test_must_fail git rev-parse --quiet --verify does-not-exist &&
+ test_must_fail git stash drop does-not-exist &&
+ test_must_fail git stash drop does-not-exist@{0} &&
+ test_must_fail git stash pop does-not-exist &&
+ test_must_fail git stash pop does-not-exist@{0} &&
+ test_must_fail git stash apply does-not-exist &&
+ test_must_fail git stash apply does-not-exist@{0} &&
+ test_must_fail git stash show does-not-exist &&
+ test_must_fail git stash show does-not-exist@{0} &&
+ test_must_fail git stash branch tmp does-not-exist &&
+ test_must_fail git stash branch tmp does-not-exist@{0} &&
+ git stash drop
+'
+
+test_expect_success 'invalid ref of the form stash@{n}, n >= N' '
+ git stash clear &&
+ test_must_fail git stash drop stash@{0} &&
+ echo bar5 >file &&
+ echo bar6 >file2 &&
+ git add file2 &&
+ git stash &&
+ test_must_fail git stash drop stash@{1} &&
+ test_must_fail git stash pop stash@{1} &&
+ test_must_fail git stash apply stash@{1} &&
+ test_must_fail git stash show stash@{1} &&
+ test_must_fail git stash branch tmp stash@{1} &&
+ git stash drop
+'
+
+test_expect_success 'invalid ref of the form "n", n >= N' '
+ git stash clear &&
+ test_must_fail git stash drop 0 &&
+ echo bar5 >file &&
+ echo bar6 >file2 &&
+ git add file2 &&
+ git stash &&
+ test_must_fail git stash drop 1 &&
+ test_must_fail git stash pop 1 &&
+ test_must_fail git stash apply 1 &&
+ test_must_fail git stash show 1 &&
+ test_must_fail git stash branch tmp 1 &&
+ git stash drop
+'
+
+test_expect_success 'valid ref of the form "n", n < N' '
+ git stash clear &&
+ echo bar5 >file &&
+ echo bar6 >file2 &&
+ git add file2 &&
+ git stash &&
+ git stash show 0 &&
+ git stash branch tmp 0 &&
+ git checkout main &&
+ git stash &&
+ git stash apply 0 &&
+ git reset --hard &&
+ git stash pop 0 &&
+ git stash &&
+ git stash drop 0 &&
+ test_must_fail git stash drop
+'
+
+test_expect_success 'branch: do not drop the stash if the branch exists' '
+ git stash clear &&
+ echo foo >file &&
+ git add file &&
+ git commit -m initial &&
+ echo bar >file &&
+ git stash &&
+ test_must_fail git stash branch main stash@{0} &&
+ git rev-parse stash@{0} --
+'
+
+test_expect_success 'branch: should not drop the stash if the apply fails' '
+ git stash clear &&
+ git reset HEAD~1 --hard &&
+ echo foo >file &&
+ git add file &&
+ git commit -m initial &&
+ echo bar >file &&
+ git stash &&
+ echo baz >file &&
+ test_when_finished "git checkout main" &&
+ test_must_fail git stash branch new_branch stash@{0} &&
+ git rev-parse stash@{0} --
+'
+
+test_expect_success 'apply: show same status as git status (relative to ./)' '
+ git stash clear &&
+ echo 1 >subdir/subfile1 &&
+ echo 2 >subdir/subfile2 &&
+ git add subdir/subfile1 &&
+ git commit -m subdir &&
+ (
+ cd subdir &&
+ echo x >subfile1 &&
+ echo x >../file &&
+ git status >../expect &&
+ git stash &&
+ sane_unset GIT_MERGE_VERBOSITY &&
+ git stash apply
+ ) |
+ sed -e 1d >actual && # drop "Saved..."
+ test_cmp expect actual
+'
+
+cat >expect <<EOF
+diff --git a/HEAD b/HEAD
+new file mode 100644
+index 0000000..fe0cbee
+--- /dev/null
++++ b/HEAD
+@@ -0,0 +1 @@
++file-not-a-ref
+EOF
+
+test_expect_success 'stash where working directory contains "HEAD" file' '
+ git stash clear &&
+ git reset --hard &&
+ echo file-not-a-ref >HEAD &&
+ git add HEAD &&
+ test_tick &&
+ git stash &&
+ git diff-files --quiet &&
+ git diff-index --cached --quiet HEAD &&
+ test "$(git rev-parse stash^)" = "$(git rev-parse HEAD)" &&
+ git diff stash^..stash >output &&
+ diff_cmp expect output
+'
+
+test_expect_success 'store called with invalid commit' '
+ test_must_fail git stash store foo
+'
+
+test_expect_success 'store called with non-stash commit' '
+ test_must_fail git stash store HEAD
+'
+
+test_expect_success 'store updates stash ref and reflog' '
+ git stash clear &&
+ git reset --hard &&
+ echo quux >bazzy &&
+ git add bazzy &&
+ STASH_ID=$(git stash create) &&
+ git reset --hard &&
+ test_path_is_missing bazzy &&
+ git stash store -m quuxery $STASH_ID &&
+ test $(git rev-parse stash) = $STASH_ID &&
+ git reflog --format=%H stash| grep $STASH_ID &&
+ git stash pop &&
+ grep quux bazzy
+'
+
+test_expect_success 'handle stash specification with spaces' '
+ git stash clear &&
+ echo pig >file &&
+ git stash &&
+ stamp=$(git log -g --format="%cd" -1 refs/stash) &&
+ test_tick &&
+ echo cow >file &&
+ git stash &&
+ git stash apply "stash@{$stamp}" &&
+ grep pig file
+'
+
+test_expect_success 'setup stash with index and worktree changes' '
+ git stash clear &&
+ git reset --hard &&
+ echo index >file &&
+ git add file &&
+ echo working >file &&
+ git stash
+'
+
+test_expect_success 'stash list -p shows simple diff' '
+ cat >expect <<-EOF &&
+ stash@{0}
+
+ diff --git a/file b/file
+ index 257cc56..d26b33d 100644
+ --- a/file
+ +++ b/file
+ @@ -1 +1 @@
+ -foo
+ +working
+ EOF
+ git stash list --format=%gd -p >actual &&
+ diff_cmp expect actual
+'
+
+test_expect_success 'stash list --cc shows combined diff' '
+ cat >expect <<-\EOF &&
+ stash@{0}
+
+ diff --cc file
+ index 257cc56,9015a7a..d26b33d
+ --- a/file
+ +++ b/file
+ @@@ -1,1 -1,1 +1,1 @@@
+ - foo
+ -index
+ ++working
+ EOF
+ git stash list --format=%gd -p --cc >actual &&
+ diff_cmp expect actual
+'
+
+test_expect_success 'stash is not confused by partial renames' '
+ mv file renamed &&
+ git add renamed &&
+ git stash &&
+ git stash apply &&
+ test_path_is_file renamed &&
+ test_path_is_missing file
+'
+
+test_expect_success 'push -m shows right message' '
+ >foo &&
+ git add foo &&
+ git stash push -m "test message" &&
+ echo "stash@{0}: On main: test message" >expect &&
+ git stash list -1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'push -m also works without space' '
+ >foo &&
+ git add foo &&
+ git stash push -m"unspaced test message" &&
+ echo "stash@{0}: On main: unspaced test message" >expect &&
+ git stash list -1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'store -m foo shows right message' '
+ git stash clear &&
+ git reset --hard &&
+ echo quux >bazzy &&
+ git add bazzy &&
+ STASH_ID=$(git stash create) &&
+ git stash store -m "store m" $STASH_ID &&
+ echo "stash@{0}: store m" >expect &&
+ git stash list -1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'store -mfoo shows right message' '
+ git stash clear &&
+ git reset --hard &&
+ echo quux >bazzy &&
+ git add bazzy &&
+ STASH_ID=$(git stash create) &&
+ git stash store -m"store mfoo" $STASH_ID &&
+ echo "stash@{0}: store mfoo" >expect &&
+ git stash list -1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'store --message=foo shows right message' '
+ git stash clear &&
+ git reset --hard &&
+ echo quux >bazzy &&
+ git add bazzy &&
+ STASH_ID=$(git stash create) &&
+ git stash store --message="store message=foo" $STASH_ID &&
+ echo "stash@{0}: store message=foo" >expect &&
+ git stash list -1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'store --message foo shows right message' '
+ git stash clear &&
+ git reset --hard &&
+ echo quux >bazzy &&
+ git add bazzy &&
+ STASH_ID=$(git stash create) &&
+ git stash store --message "store message foo" $STASH_ID &&
+ echo "stash@{0}: store message foo" >expect &&
+ git stash list -1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'push -mfoo uses right message' '
+ >foo &&
+ git add foo &&
+ git stash push -m"test mfoo" &&
+ echo "stash@{0}: On main: test mfoo" >expect &&
+ git stash list -1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'push --message foo is synonym for -mfoo' '
+ >foo &&
+ git add foo &&
+ git stash push --message "test message foo" &&
+ echo "stash@{0}: On main: test message foo" >expect &&
+ git stash list -1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'push --message=foo is synonym for -mfoo' '
+ >foo &&
+ git add foo &&
+ git stash push --message="test message=foo" &&
+ echo "stash@{0}: On main: test message=foo" >expect &&
+ git stash list -1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'push -m shows right message' '
+ >foo &&
+ git add foo &&
+ git stash push -m "test m foo" &&
+ echo "stash@{0}: On main: test m foo" >expect &&
+ git stash list -1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'create stores correct message' '
+ >foo &&
+ git add foo &&
+ STASH_ID=$(git stash create "create test message") &&
+ echo "On main: create test message" >expect &&
+ git show --pretty=%s -s ${STASH_ID} >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'create when branch name has /' '
+ test_when_finished "git checkout main" &&
+ git checkout -b some/topic &&
+ >foo &&
+ git add foo &&
+ STASH_ID=$(git stash create "create test message") &&
+ echo "On some/topic: create test message" >expect &&
+ git show --pretty=%s -s ${STASH_ID} >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'create with multiple arguments for the message' '
+ >foo &&
+ git add foo &&
+ STASH_ID=$(git stash create test untracked) &&
+ echo "On main: test untracked" >expect &&
+ git show --pretty=%s -s ${STASH_ID} >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'create in a detached state' '
+ test_when_finished "git checkout main" &&
+ git checkout HEAD~1 &&
+ >foo &&
+ git add foo &&
+ STASH_ID=$(git stash create) &&
+ HEAD_ID=$(git rev-parse --short HEAD) &&
+ echo "WIP on (no branch): ${HEAD_ID} initial" >expect &&
+ git show --pretty=%s -s ${STASH_ID} >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'stash -- <pathspec> stashes and restores the file' '
+ >foo &&
+ >bar &&
+ git add foo bar &&
+ git stash push -- foo &&
+ test_path_is_file bar &&
+ test_path_is_missing foo &&
+ git stash pop &&
+ test_path_is_file foo &&
+ test_path_is_file bar
+'
+
+test_expect_success 'stash -- <pathspec> stashes in subdirectory' '
+ mkdir sub &&
+ >foo &&
+ >bar &&
+ git add foo bar &&
+ (
+ cd sub &&
+ git stash push -- ../foo
+ ) &&
+ test_path_is_file bar &&
+ test_path_is_missing foo &&
+ git stash pop &&
+ test_path_is_file foo &&
+ test_path_is_file bar
+'
+
+test_expect_success 'stash with multiple pathspec arguments' '
+ >foo &&
+ >bar &&
+ >extra &&
+ git add foo bar extra &&
+ git stash push -- foo bar &&
+ test_path_is_missing bar &&
+ test_path_is_missing foo &&
+ test_path_is_file extra &&
+ git stash pop &&
+ test_path_is_file foo &&
+ test_path_is_file bar &&
+ test_path_is_file extra
+'
+
+test_expect_success 'stash with file including $IFS character' '
+ >"foo bar" &&
+ >foo &&
+ >bar &&
+ git add foo* &&
+ git stash push -- "foo b*" &&
+ test_path_is_missing "foo bar" &&
+ test_path_is_file foo &&
+ test_path_is_file bar &&
+ git stash pop &&
+ test_path_is_file "foo bar" &&
+ test_path_is_file foo &&
+ test_path_is_file bar
+'
+
+test_expect_success 'stash with pathspec matching multiple paths' '
+ echo original >file &&
+ echo original >other-file &&
+ git commit -m "two" file other-file &&
+ echo modified >file &&
+ echo modified >other-file &&
+ git stash push -- "*file" &&
+ echo original >expect &&
+ test_cmp expect file &&
+ test_cmp expect other-file &&
+ git stash pop &&
+ echo modified >expect &&
+ test_cmp expect file &&
+ test_cmp expect other-file
+'
+
+test_expect_success 'stash push -p with pathspec shows no changes only once' '
+ >foo &&
+ git add foo &&
+ git commit -m "tmp" &&
+ git stash push -p foo >actual &&
+ echo "No local changes to save" >expect &&
+ git reset --hard HEAD~ &&
+ test_cmp expect actual
+'
+
+test_expect_success 'push <pathspec>: show no changes when there are none' '
+ >foo &&
+ git add foo &&
+ git commit -m "tmp" &&
+ git stash push foo >actual &&
+ echo "No local changes to save" >expect &&
+ git reset --hard HEAD~ &&
+ test_cmp expect actual
+'
+
+test_expect_success 'push: <pathspec> not in the repository errors out' '
+ >untracked &&
+ test_must_fail git stash push untracked &&
+ test_path_is_file untracked
+'
+
+test_expect_success 'push: -q is quiet with changes' '
+ >foo &&
+ git add foo &&
+ git stash push -q >output 2>&1 &&
+ test_must_be_empty output
+'
+
+test_expect_success 'push: -q is quiet with no changes' '
+ git stash push -q >output 2>&1 &&
+ test_must_be_empty output
+'
+
+test_expect_success 'push: -q is quiet even if there is no initial commit' '
+ git init foo_dir &&
+ test_when_finished rm -rf foo_dir &&
+ (
+ cd foo_dir &&
+ >bar &&
+ test_must_fail git stash push -q >output 2>&1 &&
+ test_must_be_empty output
+ )
+'
+
+test_expect_success 'untracked files are left in place when -u is not given' '
+ >file &&
+ git add file &&
+ >untracked &&
+ git stash push file &&
+ test_path_is_file untracked
+'
+
+test_expect_success 'stash without verb with pathspec' '
+ >"foo bar" &&
+ >foo &&
+ >bar &&
+ git add foo* &&
+ git stash -- "foo b*" &&
+ test_path_is_missing "foo bar" &&
+ test_path_is_file foo &&
+ test_path_is_file bar &&
+ git stash pop &&
+ test_path_is_file "foo bar" &&
+ test_path_is_file foo &&
+ test_path_is_file bar
+'
+
+test_expect_success 'stash -k -- <pathspec> leaves unstaged files intact' '
+ git reset &&
+ >foo &&
+ >bar &&
+ git add foo bar &&
+ git commit -m "test" &&
+ echo "foo" >foo &&
+ echo "bar" >bar &&
+ git stash -k -- foo &&
+ test "",bar = $(cat foo),$(cat bar) &&
+ git stash pop &&
+ test foo,bar = $(cat foo),$(cat bar)
+'
+
+test_expect_success 'stash -- <subdir> leaves untracked files in subdir intact' '
+ git reset &&
+ >subdir/untracked &&
+ >subdir/tracked1 &&
+ >subdir/tracked2 &&
+ git add subdir/tracked* &&
+ git stash -- subdir/ &&
+ test_path_is_missing subdir/tracked1 &&
+ test_path_is_missing subdir/tracked2 &&
+ test_path_is_file subdir/untracked &&
+ git stash pop &&
+ test_path_is_file subdir/tracked1 &&
+ test_path_is_file subdir/tracked2 &&
+ test_path_is_file subdir/untracked
+'
+
+test_expect_success 'stash -- <subdir> works with binary files' '
+ git reset &&
+ >subdir/untracked &&
+ >subdir/tracked &&
+ cp "$TEST_DIRECTORY"/test-binary-1.png subdir/tracked-binary &&
+ git add subdir/tracked* &&
+ git stash -- subdir/ &&
+ test_path_is_missing subdir/tracked &&
+ test_path_is_missing subdir/tracked-binary &&
+ test_path_is_file subdir/untracked &&
+ git stash pop &&
+ test_path_is_file subdir/tracked &&
+ test_path_is_file subdir/tracked-binary &&
+ test_path_is_file subdir/untracked
+'
+
+test_expect_success 'stash with user.name and user.email set works' '
+ test_config user.name "A U Thor" &&
+ test_config user.email "a.u@thor" &&
+ git stash
+'
+
+test_expect_success 'stash works when user.name and user.email are not set' '
+ git reset &&
+ >1 &&
+ git add 1 &&
+ echo "$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>" >expect &&
+ git stash &&
+ git show -s --format="%an <%ae>" refs/stash >actual &&
+ test_cmp expect actual &&
+ >2 &&
+ git add 2 &&
+ test_config user.useconfigonly true &&
+ (
+ sane_unset GIT_AUTHOR_NAME &&
+ sane_unset GIT_AUTHOR_EMAIL &&
+ sane_unset GIT_COMMITTER_NAME &&
+ sane_unset GIT_COMMITTER_EMAIL &&
+ test_unconfig user.email &&
+ test_unconfig user.name &&
+ test_must_fail git commit -m "should fail" &&
+ echo "git stash <git@stash>" >expect &&
+ >2 &&
+ git stash &&
+ git show -s --format="%an <%ae>" refs/stash >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'stash --keep-index with file deleted in index does not resurrect it on disk' '
+ test_commit to-remove to-remove &&
+ git rm to-remove &&
+ git stash --keep-index &&
+ test_path_is_missing to-remove
+'
+
+test_expect_success 'stash apply should succeed with unmodified file' '
+ echo base >file &&
+ git add file &&
+ git commit -m base &&
+
+ # now stash a modification
+ echo modified >file &&
+ git stash &&
+
+ # make the file stat dirty
+ cp file other &&
+ mv other file &&
+
+ git stash apply
+'
+
+test_expect_success 'stash handles skip-worktree entries nicely' '
+ test_commit A &&
+ echo changed >A.t &&
+ git add A.t &&
+ git update-index --skip-worktree A.t &&
+ rm A.t &&
+ git stash &&
+
+ git rev-parse --verify refs/stash:A.t
+'
+
+
+BATCH_CONFIGURATION='-c core.fsync=loose-object -c core.fsyncmethod=batch'
+
+test_expect_success 'stash with core.fsyncmethod=batch' "
+ test_create_unique_files 2 4 files_base_dir &&
+ GIT_TEST_FSYNC=1 git $BATCH_CONFIGURATION stash push -u -- ./files_base_dir/ &&
+
+ # The files were untracked, so use the third parent,
+ # which contains the untracked files
+ git ls-tree -r stash^3 -- ./files_base_dir/ |
+ test_parse_ls_tree_oids >stashed_files_oids &&
+
+ # We created 2 dirs with 4 files each (8 files total) above
+ test_line_count = 8 stashed_files_oids &&
+ git cat-file --batch-check='%(objectname)' <stashed_files_oids >stashed_files_actual &&
+ test_cmp stashed_files_oids stashed_files_actual
+"
+
+
+test_expect_success 'git stash succeeds despite directory/file change' '
+ test_create_repo directory_file_switch_v1 &&
+ (
+ cd directory_file_switch_v1 &&
+ test_commit init &&
+
+ test_write_lines this file has some words >filler &&
+ git add filler &&
+ git commit -m filler &&
+
+ git rm filler &&
+ mkdir filler &&
+ echo contents >filler/file &&
+ git stash push
+ )
+'
+
+test_expect_success 'git stash can pop file -> directory saved changes' '
+ test_create_repo directory_file_switch_v2 &&
+ (
+ cd directory_file_switch_v2 &&
+ test_commit init &&
+
+ test_write_lines this file has some words >filler &&
+ git add filler &&
+ git commit -m filler &&
+
+ git rm filler &&
+ mkdir filler &&
+ echo contents >filler/file &&
+ cp filler/file expect &&
+ git stash push --include-untracked &&
+ git stash apply --index &&
+ test_cmp expect filler/file
+ )
+'
+
+test_expect_success 'git stash can pop directory -> file saved changes' '
+ test_create_repo directory_file_switch_v3 &&
+ (
+ cd directory_file_switch_v3 &&
+ test_commit init &&
+
+ mkdir filler &&
+ test_write_lines some words >filler/file1 &&
+ test_write_lines and stuff >filler/file2 &&
+ git add filler &&
+ git commit -m filler &&
+
+ git rm -rf filler &&
+ echo contents >filler &&
+ cp filler expect &&
+ git stash push --include-untracked &&
+ git stash apply --index &&
+ test_cmp expect filler
+ )
+'
+
+test_expect_success 'restore untracked files even when we hit conflicts' '
+ git init restore_untracked_after_conflict &&
+ (
+ cd restore_untracked_after_conflict &&
+
+ echo hi >a &&
+ echo there >b &&
+ git add . &&
+ git commit -m first &&
+ echo hello >a &&
+ echo something >c &&
+
+ git stash push --include-untracked &&
+
+ echo conflict >a &&
+ git add a &&
+ git commit -m second &&
+
+ test_must_fail git stash pop &&
+
+ test_path_is_file c
+ )
+'
+
+test_done
diff --git a/t/t3904-stash-patch.sh b/t/t3904-stash-patch.sh
new file mode 100755
index 0000000..accfe38
--- /dev/null
+++ b/t/t3904-stash-patch.sh
@@ -0,0 +1,115 @@
+#!/bin/sh
+
+test_description='stash -p'
+. ./lib-patch-mode.sh
+
+if ! test_have_prereq PERL
+then
+ skip_all='skipping stash -p tests, perl not available'
+ test_done
+fi
+
+test_expect_success 'setup' '
+ mkdir dir &&
+ echo parent > dir/foo &&
+ echo dummy > bar &&
+ echo committed > HEAD &&
+ git add bar dir/foo HEAD &&
+ git commit -m initial &&
+ test_tick &&
+ test_commit second dir/foo head &&
+ echo index > dir/foo &&
+ git add dir/foo &&
+ set_and_save_state bar bar_work bar_index &&
+ save_head
+'
+
+# note: order of files with unstaged changes: HEAD bar dir/foo
+
+test_expect_success 'saying "n" does nothing' '
+ set_state HEAD HEADfile_work HEADfile_index &&
+ set_state dir/foo work index &&
+ test_write_lines n n n | test_must_fail git stash save -p &&
+ verify_state HEAD HEADfile_work HEADfile_index &&
+ verify_saved_state bar &&
+ verify_state dir/foo work index
+'
+
+test_expect_success 'git stash -p' '
+ test_write_lines y n y | git stash save -p &&
+ verify_state HEAD committed HEADfile_index &&
+ verify_saved_state bar &&
+ verify_state dir/foo head index &&
+ git reset --hard &&
+ git stash apply &&
+ verify_state HEAD HEADfile_work committed &&
+ verify_state bar dummy dummy &&
+ verify_state dir/foo work head
+'
+
+test_expect_success 'git stash -p --no-keep-index' '
+ set_state HEAD HEADfile_work HEADfile_index &&
+ set_state bar bar_work bar_index &&
+ set_state dir/foo work index &&
+ test_write_lines y n y | git stash save -p --no-keep-index &&
+ verify_state HEAD committed committed &&
+ verify_state bar bar_work dummy &&
+ verify_state dir/foo head head &&
+ git reset --hard &&
+ git stash apply --index &&
+ verify_state HEAD HEADfile_work HEADfile_index &&
+ verify_state bar dummy bar_index &&
+ verify_state dir/foo work index
+'
+
+test_expect_success 'git stash --no-keep-index -p' '
+ set_state HEAD HEADfile_work HEADfile_index &&
+ set_state bar bar_work bar_index &&
+ set_state dir/foo work index &&
+ test_write_lines y n y | git stash save --no-keep-index -p &&
+ verify_state HEAD committed committed &&
+ verify_state dir/foo head head &&
+ verify_state bar bar_work dummy &&
+ git reset --hard &&
+ git stash apply --index &&
+ verify_state HEAD HEADfile_work HEADfile_index &&
+ verify_state bar dummy bar_index &&
+ verify_state dir/foo work index
+'
+
+test_expect_success 'stash -p --no-keep-index -- <pathspec> does not unstage other files' '
+ set_state HEAD HEADfile_work HEADfile_index &&
+ set_state dir/foo work index &&
+ echo y | git stash push -p --no-keep-index -- HEAD &&
+ verify_state HEAD committed committed &&
+ verify_state dir/foo work index
+'
+
+test_expect_success 'none of this moved HEAD' '
+ verify_saved_head
+'
+
+test_expect_success 'stash -p with split hunk' '
+ git reset --hard &&
+ cat >test <<-\EOF &&
+ aaa
+ bbb
+ ccc
+ EOF
+ git add test &&
+ git commit -m "initial" &&
+ cat >test <<-\EOF &&
+ aaa
+ added line 1
+ bbb
+ added line 2
+ ccc
+ EOF
+ printf "%s\n" s n y q |
+ git stash -p 2>error &&
+ test_must_be_empty error &&
+ grep "added line 1" test &&
+ ! grep "added line 2" test
+'
+
+test_done
diff --git a/t/t3905-stash-include-untracked.sh b/t/t3905-stash-include-untracked.sh
new file mode 100755
index 0000000..1289ae3
--- /dev/null
+++ b/t/t3905-stash-include-untracked.sh
@@ -0,0 +1,431 @@
+#!/bin/sh
+#
+# Copyright (c) 2011 David Caldwell
+#
+
+test_description='Test git stash --include-untracked'
+
+. ./test-lib.sh
+
+test_expect_success 'stash save --include-untracked some dirty working directory' '
+ echo 1 >file &&
+ git add file &&
+ test_tick &&
+ git commit -m initial &&
+ echo 2 >file &&
+ git add file &&
+ echo 3 >file &&
+ test_tick &&
+ echo 1 >file2 &&
+ echo 1 >HEAD &&
+ mkdir untracked &&
+ echo untracked >untracked/untracked &&
+ git stash --include-untracked &&
+ git diff-files --quiet &&
+ git diff-index --cached --quiet HEAD
+'
+
+test_expect_success 'stash save --include-untracked cleaned the untracked files' '
+ cat >expect <<-EOF &&
+ ?? actual
+ ?? expect
+ EOF
+
+ git status --porcelain >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'stash save --include-untracked stashed the untracked files' '
+ one_blob=$(echo 1 | git hash-object --stdin) &&
+ tracked=$(git rev-parse --short "$one_blob") &&
+ untracked_blob=$(echo untracked | git hash-object --stdin) &&
+ untracked=$(git rev-parse --short "$untracked_blob") &&
+ cat >expect.diff <<-EOF &&
+ diff --git a/HEAD b/HEAD
+ new file mode 100644
+ index 0000000..$tracked
+ --- /dev/null
+ +++ b/HEAD
+ @@ -0,0 +1 @@
+ +1
+ diff --git a/file2 b/file2
+ new file mode 100644
+ index 0000000..$tracked
+ --- /dev/null
+ +++ b/file2
+ @@ -0,0 +1 @@
+ +1
+ diff --git a/untracked/untracked b/untracked/untracked
+ new file mode 100644
+ index 0000000..$untracked
+ --- /dev/null
+ +++ b/untracked/untracked
+ @@ -0,0 +1 @@
+ +untracked
+ EOF
+ cat >expect.lstree <<-EOF &&
+ HEAD
+ file2
+ untracked
+ EOF
+
+ test_path_is_missing file2 &&
+ test_path_is_missing untracked &&
+ test_path_is_missing HEAD &&
+ git diff HEAD stash^3 -- HEAD file2 untracked >actual &&
+ test_cmp expect.diff actual &&
+ git ls-tree --name-only stash^3: >actual &&
+ test_cmp expect.lstree actual
+'
+test_expect_success 'stash save --patch --include-untracked fails' '
+ test_must_fail git stash --patch --include-untracked
+'
+
+test_expect_success 'stash save --patch --all fails' '
+ test_must_fail git stash --patch --all
+'
+
+test_expect_success 'clean up untracked/untracked file to prepare for next tests' '
+ git clean --force --quiet
+
+'
+
+test_expect_success 'stash pop after save --include-untracked leaves files untracked again' '
+ cat >expect <<-EOF &&
+ M file
+ ?? HEAD
+ ?? actual
+ ?? expect
+ ?? file2
+ ?? untracked/
+ EOF
+
+ git stash pop &&
+ git status --porcelain >actual &&
+ test_cmp expect actual &&
+ echo 1 >expect_file2 &&
+ test_cmp expect_file2 file2 &&
+ echo untracked >untracked_expect &&
+ test_cmp untracked_expect untracked/untracked
+'
+
+test_expect_success 'clean up untracked/ directory to prepare for next tests' '
+ git clean --force --quiet -d
+'
+
+test_expect_success 'stash save -u dirty index' '
+ echo 4 >file3 &&
+ git add file3 &&
+ test_tick &&
+ git stash -u
+'
+
+test_expect_success 'stash save --include-untracked dirty index got stashed' '
+ four_blob=$(echo 4 | git hash-object --stdin) &&
+ blob=$(git rev-parse --short "$four_blob") &&
+ cat >expect <<-EOF &&
+ diff --git a/file3 b/file3
+ new file mode 100644
+ index 0000000..$blob
+ --- /dev/null
+ +++ b/file3
+ @@ -0,0 +1 @@
+ +4
+ EOF
+
+ git stash pop --index &&
+ test_when_finished "git reset" &&
+ git diff --cached >actual &&
+ test_cmp expect actual
+'
+
+# Must direct output somewhere where it won't be considered an untracked file
+test_expect_success 'stash save --include-untracked -q is quiet' '
+ echo 1 >file5 &&
+ git stash save --include-untracked --quiet >.git/stash-output.out 2>&1 &&
+ test_line_count = 0 .git/stash-output.out &&
+ rm -f .git/stash-output.out
+'
+
+test_expect_success 'stash save --include-untracked removed files' '
+ rm -f file &&
+ git stash save --include-untracked &&
+ echo 1 >expect &&
+ test_when_finished "rm -f expect" &&
+ test_cmp expect file
+'
+
+test_expect_success 'stash save --include-untracked removed files got stashed' '
+ git stash pop &&
+ test_path_is_missing file
+'
+
+test_expect_success 'stash save --include-untracked respects .gitignore' '
+ cat >.gitignore <<-EOF &&
+ .gitignore
+ ignored
+ ignored.d/
+ EOF
+
+ echo ignored >ignored &&
+ mkdir ignored.d &&
+ echo ignored >ignored.d/untracked &&
+ git stash -u &&
+ test_file_not_empty ignored &&
+ test_file_not_empty ignored.d/untracked &&
+ test_file_not_empty .gitignore
+'
+
+test_expect_success 'stash save -u can stash with only untracked files different' '
+ echo 4 >file4 &&
+ git stash -u &&
+ test_path_is_missing file4
+'
+
+test_expect_success 'stash save --all does not respect .gitignore' '
+ git stash -a &&
+ test_path_is_missing ignored &&
+ test_path_is_missing ignored.d &&
+ test_path_is_missing .gitignore
+'
+
+test_expect_success 'stash save --all is stash poppable' '
+ git stash pop &&
+ test_file_not_empty ignored &&
+ test_file_not_empty ignored.d/untracked &&
+ test_file_not_empty .gitignore
+'
+
+test_expect_success 'stash push --include-untracked with pathspec' '
+ >foo &&
+ >bar &&
+ git stash push --include-untracked -- foo &&
+ test_path_is_file bar &&
+ test_path_is_missing foo &&
+ git stash pop &&
+ test_path_is_file bar &&
+ test_path_is_file foo
+'
+
+test_expect_success 'stash push with $IFS character' '
+ >"foo bar" &&
+ >foo &&
+ >bar &&
+ git add foo* &&
+ git stash push --include-untracked -- "foo b*" &&
+ test_path_is_missing "foo bar" &&
+ test_path_is_file foo &&
+ test_path_is_file bar &&
+ git stash pop &&
+ test_path_is_file "foo bar" &&
+ test_path_is_file foo &&
+ test_path_is_file bar
+'
+
+test_expect_success 'stash previously ignored file' '
+ cat >.gitignore <<-EOF &&
+ ignored
+ ignored.d/*
+ EOF
+
+ git reset HEAD &&
+ git add .gitignore &&
+ git commit -m "Add .gitignore" &&
+ >ignored.d/foo &&
+ echo "!ignored.d/foo" >>.gitignore &&
+ git stash save --include-untracked &&
+ test_path_is_missing ignored.d/foo &&
+ git stash pop &&
+ test_path_is_file ignored.d/foo
+'
+
+test_expect_success 'stash -u -- <untracked> doesnt print error' '
+ >untracked &&
+ git stash push -u -- untracked 2>actual &&
+ test_path_is_missing untracked &&
+ test_line_count = 0 actual
+'
+
+test_expect_success 'stash -u -- <untracked> leaves rest of working tree in place' '
+ >tracked &&
+ git add tracked &&
+ >untracked &&
+ git stash push -u -- untracked &&
+ test_path_is_missing untracked &&
+ test_path_is_file tracked
+'
+
+test_expect_success 'stash -u -- <tracked> <untracked> clears changes in both' '
+ >tracked &&
+ git add tracked &&
+ >untracked &&
+ git stash push -u -- tracked untracked &&
+ test_path_is_missing tracked &&
+ test_path_is_missing untracked
+'
+
+test_expect_success 'stash --all -- <ignored> stashes ignored file' '
+ >ignored.d/bar &&
+ git stash push --all -- ignored.d/bar &&
+ test_path_is_missing ignored.d/bar
+'
+
+test_expect_success 'stash --all -- <tracked> <ignored> clears changes in both' '
+ >tracked &&
+ git add tracked &&
+ >ignored.d/bar &&
+ git stash push --all -- tracked ignored.d/bar &&
+ test_path_is_missing tracked &&
+ test_path_is_missing ignored.d/bar
+'
+
+test_expect_success 'stash -u -- <ignored> leaves ignored file alone' '
+ >ignored.d/bar &&
+ git stash push -u -- ignored.d/bar &&
+ test_path_is_file ignored.d/bar
+'
+
+test_expect_success 'stash -u -- <non-existent> shows no changes when there are none' '
+ git stash push -u -- non-existent >actual &&
+ echo "No local changes to save" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'stash -u with globs' '
+ >untracked.txt &&
+ git stash -u -- ":(glob)**/*.txt" &&
+ test_path_is_missing untracked.txt
+'
+
+test_expect_success 'stash show --include-untracked shows untracked files' '
+ git reset --hard &&
+ git clean -xf &&
+ >untracked &&
+ >tracked &&
+ git add tracked &&
+ empty_blob_oid=$(git rev-parse --short :tracked) &&
+ git stash -u &&
+
+ cat >expect <<-EOF &&
+ tracked | 0
+ untracked | 0
+ 2 files changed, 0 insertions(+), 0 deletions(-)
+ EOF
+ git stash show --include-untracked >actual &&
+ test_cmp expect actual &&
+ git stash show -u >actual &&
+ test_cmp expect actual &&
+ git stash show --no-include-untracked --include-untracked >actual &&
+ test_cmp expect actual &&
+ git stash show --only-untracked --include-untracked >actual &&
+ test_cmp expect actual &&
+ git -c stash.showIncludeUntracked=true stash show >actual &&
+ test_cmp expect actual &&
+
+ cat >expect <<-EOF &&
+ diff --git a/tracked b/tracked
+ new file mode 100644
+ index 0000000..$empty_blob_oid
+ diff --git a/untracked b/untracked
+ new file mode 100644
+ index 0000000..$empty_blob_oid
+ EOF
+ git stash show -p --include-untracked >actual &&
+ test_cmp expect actual &&
+ git stash show --include-untracked -p >actual &&
+ test_cmp expect actual &&
+ git -c stash.showIncludeUntracked=true stash show -p >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'stash show --only-untracked only shows untracked files' '
+ git reset --hard &&
+ git clean -xf &&
+ >untracked &&
+ >tracked &&
+ git add tracked &&
+ empty_blob_oid=$(git rev-parse --short :tracked) &&
+ git stash -u &&
+
+ cat >expect <<-EOF &&
+ untracked | 0
+ 1 file changed, 0 insertions(+), 0 deletions(-)
+ EOF
+ git stash show --only-untracked >actual &&
+ test_cmp expect actual &&
+ git stash show --no-include-untracked --only-untracked >actual &&
+ test_cmp expect actual &&
+ git stash show --include-untracked --only-untracked >actual &&
+ test_cmp expect actual &&
+
+ cat >expect <<-EOF &&
+ diff --git a/untracked b/untracked
+ new file mode 100644
+ index 0000000..$empty_blob_oid
+ EOF
+ git stash show -p --only-untracked >actual &&
+ test_cmp expect actual &&
+ git stash show --only-untracked -p >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'stash show --no-include-untracked cancels --{include,only}-untracked' '
+ git reset --hard &&
+ git clean -xf &&
+ >untracked &&
+ >tracked &&
+ git add tracked &&
+ git stash -u &&
+
+ cat >expect <<-EOF &&
+ tracked | 0
+ 1 file changed, 0 insertions(+), 0 deletions(-)
+ EOF
+ git stash show --only-untracked --no-include-untracked >actual &&
+ test_cmp expect actual &&
+ git stash show --include-untracked --no-include-untracked >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'stash show --include-untracked errors on duplicate files' '
+ git reset --hard &&
+ git clean -xf &&
+ >tracked &&
+ git add tracked &&
+ tree=$(git write-tree) &&
+ i_commit=$(git commit-tree -p HEAD -m "index on any-branch" "$tree") &&
+ test_when_finished "rm -f untracked_index" &&
+ u_commit=$(
+ GIT_INDEX_FILE="untracked_index" &&
+ export GIT_INDEX_FILE &&
+ git update-index --add tracked &&
+ u_tree=$(git write-tree) &&
+ git commit-tree -m "untracked files on any-branch" "$u_tree"
+ ) &&
+ w_commit=$(git commit-tree -p HEAD -p "$i_commit" -p "$u_commit" -m "WIP on any-branch" "$tree") &&
+ test_must_fail git stash show --include-untracked "$w_commit" 2>err &&
+ test_grep "worktree and untracked commit have duplicate entries: tracked" err
+'
+
+test_expect_success 'stash show --{include,only}-untracked on stashes without untracked entries' '
+ git reset --hard &&
+ git clean -xf &&
+ >tracked &&
+ git add tracked &&
+ git stash &&
+
+ git stash show >expect &&
+ git stash show --include-untracked >actual &&
+ test_cmp expect actual &&
+
+ git stash show --only-untracked >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'stash -u ignores sub-repository' '
+ test_when_finished "rm -rf sub-repo" &&
+ git init sub-repo &&
+ git stash -u
+'
+
+test_done
diff --git a/t/t3906-stash-submodule.sh b/t/t3906-stash-submodule.sh
new file mode 100755
index 0000000..0f7348e
--- /dev/null
+++ b/t/t3906-stash-submodule.sh
@@ -0,0 +1,69 @@
+#!/bin/sh
+
+test_description='stash can handle submodules'
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-submodule-update.sh
+
+git_stash () {
+ git status -su >expect &&
+ ls -1pR * >>expect &&
+ may_only_be_test_must_fail "$2" &&
+ $2 git read-tree -u -m "$1" &&
+ if test -n "$2"
+ then
+ return
+ fi &&
+ 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_func "git_stash"
+
+setup_basic () {
+ test_when_finished "rm -rf main sub" &&
+ git init sub &&
+ (
+ cd sub &&
+ test_commit sub_file
+ ) &&
+ git init main &&
+ (
+ cd main &&
+ git -c protocol.file.allow=always submodule add ../sub &&
+ test_commit main_file
+ )
+}
+
+test_expect_success 'stash push with submodule.recurse=true preserves dirty submodule worktree' '
+ setup_basic &&
+ (
+ cd main &&
+ git config submodule.recurse true &&
+ echo "x" >main_file.t &&
+ echo "y" >sub/sub_file.t &&
+ git stash push &&
+ test_must_fail git -C sub diff --quiet
+ )
+'
+
+test_expect_success 'stash push and pop with submodule.recurse=true preserves dirty submodule worktree' '
+ setup_basic &&
+ (
+ cd main &&
+ git config submodule.recurse true &&
+ echo "x" >main_file.t &&
+ echo "y" >sub/sub_file.t &&
+ git stash push &&
+ git stash pop &&
+ test_must_fail git -C sub diff --quiet
+ )
+'
+
+test_done
diff --git a/t/t3907-stash-show-config.sh b/t/t3907-stash-show-config.sh
new file mode 100755
index 0000000..10914bb
--- /dev/null
+++ b/t/t3907-stash-show-config.sh
@@ -0,0 +1,83 @@
+#!/bin/sh
+
+test_description='Test git stash show configuration.'
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ test_commit file
+'
+
+# takes three parameters:
+# 1. the stash.showStat value (or "<unset>")
+# 2. the stash.showPatch value (or "<unset>")
+# 3. the diff options of the expected output (or nothing for no output)
+test_stat_and_patch () {
+ if test "<unset>" = "$1"
+ then
+ test_unconfig stash.showStat
+ else
+ test_config stash.showStat "$1"
+ fi &&
+
+ if test "<unset>" = "$2"
+ then
+ test_unconfig stash.showPatch
+ else
+ test_config stash.showPatch "$2"
+ fi &&
+
+ shift 2 &&
+ echo 2 >file.t &&
+ if test $# != 0
+ then
+ git diff "$@" >expect
+ fi &&
+ git stash &&
+ git stash show >actual &&
+
+ if test $# = 0
+ then
+ test_must_be_empty actual
+ else
+ test_cmp expect actual
+ fi
+}
+
+test_expect_success 'showStat unset showPatch unset' '
+ test_stat_and_patch "<unset>" "<unset>" --stat
+'
+
+test_expect_success 'showStat unset showPatch false' '
+ test_stat_and_patch "<unset>" false --stat
+'
+
+test_expect_success 'showStat unset showPatch true' '
+ test_stat_and_patch "<unset>" true --stat -p
+'
+
+test_expect_success 'showStat false showPatch unset' '
+ test_stat_and_patch false "<unset>"
+'
+
+test_expect_success 'showStat false showPatch false' '
+ test_stat_and_patch false false
+'
+
+test_expect_success 'showStat false showPatch true' '
+ test_stat_and_patch false true -p
+'
+
+test_expect_success 'showStat true showPatch unset' '
+ test_stat_and_patch true "<unset>" --stat
+'
+
+test_expect_success 'showStat true showPatch false' '
+ test_stat_and_patch true false --stat
+'
+
+test_expect_success 'showStat true showPatch true' '
+ test_stat_and_patch true true --stat -p
+'
+
+test_done
diff --git a/t/t3908-stash-in-worktree.sh b/t/t3908-stash-in-worktree.sh
new file mode 100755
index 0000000..347a89b
--- /dev/null
+++ b/t/t3908-stash-in-worktree.sh
@@ -0,0 +1,28 @@
+#!/bin/sh
+#
+# Copyright (c) 2019 Johannes E Schindelin
+#
+
+test_description='Test git stash in a worktree'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ test_commit initial &&
+ git worktree add wt &&
+ test_commit -C wt in-worktree
+'
+
+test_expect_success 'apply in subdirectory' '
+ mkdir wt/subdir &&
+ (
+ cd wt/subdir &&
+ echo modified >../initial.t &&
+ git stash &&
+ git stash apply >out
+ ) &&
+ grep "\.\.\/initial\.t" wt/subdir/out
+'
+
+test_done
diff --git a/t/t3909-stash-pathspec-file.sh b/t/t3909-stash-pathspec-file.sh
new file mode 100755
index 0000000..73f2dbd
--- /dev/null
+++ b/t/t3909-stash-pathspec-file.sh
@@ -0,0 +1,100 @@
+#!/bin/sh
+
+test_description='stash --pathspec-from-file'
+
+. ./test-lib.sh
+
+test_tick
+
+test_expect_success setup '
+ >fileA.t &&
+ >fileB.t &&
+ >fileC.t &&
+ >fileD.t &&
+ git add fileA.t fileB.t fileC.t fileD.t &&
+ git commit -m "Files" &&
+
+ git tag checkpoint
+'
+
+restore_checkpoint () {
+ git reset --hard checkpoint
+}
+
+verify_expect () {
+ git stash show --name-status >actual &&
+ test_cmp expect actual
+}
+
+test_expect_success 'simplest' '
+ restore_checkpoint &&
+
+ # More files are written to make sure that git didnt ignore
+ # --pathspec-from-file, stashing everything
+ echo A >fileA.t &&
+ echo B >fileB.t &&
+ echo C >fileC.t &&
+ echo D >fileD.t &&
+
+ cat >expect <<-\EOF &&
+ M fileA.t
+ EOF
+
+ echo fileA.t | git stash push --pathspec-from-file=- &&
+ verify_expect
+'
+
+test_expect_success '--pathspec-file-nul' '
+ restore_checkpoint &&
+
+ # More files are written to make sure that git didnt ignore
+ # --pathspec-from-file, stashing everything
+ echo A >fileA.t &&
+ echo B >fileB.t &&
+ echo C >fileC.t &&
+ echo D >fileD.t &&
+
+ cat >expect <<-\EOF &&
+ M fileA.t
+ M fileB.t
+ EOF
+
+ printf "fileA.t\0fileB.t\0" | git stash push --pathspec-from-file=- --pathspec-file-nul &&
+ verify_expect
+'
+
+test_expect_success 'only touches what was listed' '
+ restore_checkpoint &&
+
+ # More files are written to make sure that git didnt ignore
+ # --pathspec-from-file, stashing everything
+ echo A >fileA.t &&
+ echo B >fileB.t &&
+ echo C >fileC.t &&
+ echo D >fileD.t &&
+
+ cat >expect <<-\EOF &&
+ M fileB.t
+ M fileC.t
+ EOF
+
+ printf "fileB.t\nfileC.t\n" | git stash push --pathspec-from-file=- &&
+ verify_expect
+'
+
+test_expect_success 'error conditions' '
+ restore_checkpoint &&
+ echo A >fileA.t &&
+ echo fileA.t >list &&
+
+ test_must_fail git stash push --pathspec-from-file=list --patch 2>err &&
+ test_grep -e "options .--pathspec-from-file. and .--patch. cannot be used together" err &&
+
+ test_must_fail git stash push --pathspec-from-file=list -- fileA.t 2>err &&
+ test_grep -e ".--pathspec-from-file. and pathspec arguments cannot be used together" err &&
+
+ test_must_fail git stash push --pathspec-file-nul 2>err &&
+ test_grep -e "the option .--pathspec-file-nul. requires .--pathspec-from-file." err
+'
+
+test_done
diff --git a/t/t3910-mac-os-precompose.sh b/t/t3910-mac-os-precompose.sh
new file mode 100755
index 0000000..898267a
--- /dev/null
+++ b/t/t3910-mac-os-precompose.sh
@@ -0,0 +1,223 @@
+#!/bin/sh
+#
+# Copyright (c) 2012 Torsten Bögershausen
+#
+
+test_description='utf-8 decomposed (nfd) converted to precomposed (nfc)'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+if ! test_have_prereq UTF8_NFD_TO_NFC
+then
+ skip_all="filesystem does not corrupt utf-8"
+ test_done
+fi
+
+# create utf-8 variables
+Adiarnfc=$(printf '\303\204')
+Adiarnfd=$(printf 'A\314\210')
+
+Odiarnfc=$(printf '\303\226')
+Odiarnfd=$(printf 'O\314\210')
+AEligatu=$(printf '\303\206')
+Invalidu=$(printf '\303\377')
+
+
+#Create a string with 255 bytes (decomposed)
+Alongd=$Adiarnfd$Adiarnfd$Adiarnfd$Adiarnfd$Adiarnfd$Adiarnfd$Adiarnfd #21 Byte
+Alongd=$Alongd$Alongd$Alongd #63 Byte
+Alongd=$Alongd$Alongd$Alongd$Alongd$Adiarnfd #255 Byte
+
+#Create a string with 254 bytes (precomposed)
+Alongc=$AEligatu$AEligatu$AEligatu$AEligatu$AEligatu #10 Byte
+Alongc=$Alongc$Alongc$Alongc$Alongc$Alongc #50 Byte
+Alongc=$Alongc$Alongc$Alongc$Alongc$Alongc #250 Byte
+Alongc=$Alongc$AEligatu$AEligatu #254 Byte
+
+test_expect_success "detect if nfd needed" '
+ precomposeunicode=$(git config core.precomposeunicode) &&
+ test "$precomposeunicode" = true &&
+ git config core.precomposeunicode true
+'
+test_expect_success "setup" '
+ >x &&
+ git add x &&
+ git commit -m "1st commit" &&
+ git rm x &&
+ git commit -m "rm x"
+'
+test_expect_success "setup case mac" '
+ git checkout -b mac_os
+'
+# This will test nfd2nfc in git diff
+test_expect_success "git diff f.Adiar" '
+ touch f.$Adiarnfc &&
+ git add f.$Adiarnfc &&
+ echo f.Adiarnfc >f.$Adiarnfc &&
+ git diff f.$Adiarnfd >expect &&
+ git diff f.$Adiarnfc >actual &&
+ test_cmp expect actual &&
+ git reset HEAD f.Adiarnfc &&
+ rm f.$Adiarnfc expect actual
+'
+# This will test nfd2nfc in git diff-files
+test_expect_success "git diff-files f.Adiar" '
+ touch f.$Adiarnfc &&
+ git add f.$Adiarnfc &&
+ echo f.Adiarnfc >f.$Adiarnfc &&
+ git diff-files f.$Adiarnfd >expect &&
+ git diff-files f.$Adiarnfc >actual &&
+ test_cmp expect actual &&
+ git reset HEAD f.Adiarnfc &&
+ rm f.$Adiarnfc expect actual
+'
+# This will test nfd2nfc in git diff-index
+test_expect_success "git diff-index f.Adiar" '
+ touch f.$Adiarnfc &&
+ git add f.$Adiarnfc &&
+ echo f.Adiarnfc >f.$Adiarnfc &&
+ git diff-index HEAD f.$Adiarnfd >expect &&
+ git diff-index HEAD f.$Adiarnfc >actual &&
+ test_cmp expect actual &&
+ git reset HEAD f.Adiarnfc &&
+ rm f.$Adiarnfc expect actual
+'
+# This will test nfd2nfc in readdir()
+test_expect_success "add file Adiarnfc" '
+ echo f.Adiarnfc >f.$Adiarnfc &&
+ git add f.$Adiarnfc &&
+ git commit -m "add f.$Adiarnfc"
+'
+# This will test nfd2nfc in git diff-tree
+test_expect_success "git diff-tree f.Adiar" '
+ echo f.Adiarnfc >>f.$Adiarnfc &&
+ git diff-tree HEAD f.$Adiarnfd >expect &&
+ git diff-tree HEAD f.$Adiarnfc >actual &&
+ test_cmp expect actual &&
+ git checkout f.$Adiarnfc &&
+ rm expect actual
+'
+# This will test nfd2nfc in git stage()
+test_expect_success "stage file d.Adiarnfd/f.Adiarnfd" '
+ mkdir d.$Adiarnfd &&
+ echo d.$Adiarnfd/f.$Adiarnfd >d.$Adiarnfd/f.$Adiarnfd &&
+ git stage d.$Adiarnfd/f.$Adiarnfd &&
+ git commit -m "add d.$Adiarnfd/f.$Adiarnfd"
+'
+test_expect_success "add link Adiarnfc" '
+ ln -s d.$Adiarnfd/f.$Adiarnfd l.$Adiarnfc &&
+ git add l.$Adiarnfc &&
+ git commit -m "add l.Adiarnfc"
+'
+# This will test git log
+test_expect_success "git log f.Adiar" '
+ git log f.$Adiarnfc > f.Adiarnfc.log &&
+ git log f.$Adiarnfd > f.Adiarnfd.log &&
+ test -s f.Adiarnfc.log &&
+ test -s f.Adiarnfd.log &&
+ test_cmp f.Adiarnfc.log f.Adiarnfd.log &&
+ rm f.Adiarnfc.log f.Adiarnfd.log
+'
+# This will test git ls-files
+test_expect_success "git lsfiles f.Adiar" '
+ git ls-files f.$Adiarnfc > f.Adiarnfc.log &&
+ git ls-files f.$Adiarnfd > f.Adiarnfd.log &&
+ test -s f.Adiarnfc.log &&
+ test -s f.Adiarnfd.log &&
+ test_cmp f.Adiarnfc.log f.Adiarnfd.log &&
+ rm f.Adiarnfc.log f.Adiarnfd.log
+'
+# This will test git mv
+test_expect_success "git mv" '
+ git mv f.$Adiarnfd f.$Odiarnfc &&
+ git mv d.$Adiarnfd d.$Odiarnfc &&
+ git mv l.$Adiarnfd l.$Odiarnfc &&
+ git commit -m "mv Adiarnfd Odiarnfc"
+'
+# Files can be checked out as nfc
+# And the link has been corrected from nfd to nfc
+test_expect_success "git checkout nfc" '
+ rm f.$Odiarnfc &&
+ git checkout f.$Odiarnfc
+'
+# Make it possible to checkout files with their NFD names
+test_expect_success "git checkout file nfd" '
+ rm -f f.* &&
+ git checkout f.$Odiarnfd
+'
+# Make it possible to checkout links with their NFD names
+test_expect_success "git checkout link nfd" '
+ rm l.* &&
+ git checkout l.$Odiarnfd
+'
+test_expect_success "setup case mac2" '
+ git checkout main &&
+ git reset --hard &&
+ git checkout -b mac_os_2
+'
+# This will test nfd2nfc in git commit
+test_expect_success "commit file d2.Adiarnfd/f.Adiarnfd" '
+ mkdir d2.$Adiarnfd &&
+ echo d2.$Adiarnfd/f.$Adiarnfd >d2.$Adiarnfd/f.$Adiarnfd &&
+ git add d2.$Adiarnfd/f.$Adiarnfd &&
+ git commit -m "add d2.$Adiarnfd/f.$Adiarnfd" -- d2.$Adiarnfd/f.$Adiarnfd
+'
+test_expect_success "setup for long decomposed filename" '
+ git checkout main &&
+ git reset --hard &&
+ git checkout -b mac_os_long_nfd_fn
+'
+test_expect_success "Add long decomposed filename" '
+ echo longd >$Alongd &&
+ git add * &&
+ git commit -m "Long filename"
+'
+test_expect_success "setup for long precomposed filename" '
+ git checkout main &&
+ git reset --hard &&
+ git checkout -b mac_os_long_nfc_fn
+'
+test_expect_success "Add long precomposed filename" '
+ echo longc >$Alongc &&
+ git add * &&
+ git commit -m "Long filename"
+'
+
+test_expect_failure 'handle existing decomposed filenames' '
+ echo content >"verbatim.$Adiarnfd" &&
+ git -c core.precomposeunicode=false add "verbatim.$Adiarnfd" &&
+ git commit -m "existing decomposed file" &&
+ git ls-files --exclude-standard -o "verbatim*" >untracked &&
+ test_must_be_empty untracked
+'
+
+test_expect_success "unicode decomposed: git restore -p . " '
+ DIRNAMEPWD=dir.Odiarnfc &&
+ DIRNAMEINREPO=dir.$Adiarnfc &&
+ export DIRNAMEPWD DIRNAMEINREPO &&
+ git init "$DIRNAMEPWD" &&
+ (
+ cd "$DIRNAMEPWD" &&
+ mkdir "$DIRNAMEINREPO" &&
+ cd "$DIRNAMEINREPO" &&
+ echo "Initial" >file &&
+ git add file &&
+ echo "More stuff" >>file &&
+ echo y | git restore -p .
+ )
+'
+
+# Test if the global core.precomposeunicode stops autosensing
+# Must be the last test case
+test_expect_success "respect git config --global core.precomposeunicode" '
+ git config --global core.precomposeunicode true &&
+ rm -rf .git &&
+ git init &&
+ precomposeunicode=$(git config core.precomposeunicode) &&
+ test "$precomposeunicode" = "true"
+'
+
+test_done
diff --git a/t/t3920-crlf-messages.sh b/t/t3920-crlf-messages.sh
new file mode 100755
index 0000000..67fd234
--- /dev/null
+++ b/t/t3920-crlf-messages.sh
@@ -0,0 +1,128 @@
+#!/bin/sh
+
+test_description='Test ref-filter and pretty APIs for commit and tag messages using CRLF'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+LIB_CRLF_BRANCHES=""
+
+create_crlf_ref () {
+ branch="$1" &&
+ cat >.crlf-orig-$branch.txt &&
+ cat .crlf-orig-$branch.txt | append_cr >.crlf-message-$branch.txt &&
+ grep 'Subject' .crlf-orig-$branch.txt | tr '\n' ' ' | sed 's/[ ]*$//' | tr -d '\n' >.crlf-subject-$branch.txt &&
+ grep 'Body' .crlf-orig-$branch.txt | append_cr >.crlf-body-$branch.txt &&
+ LIB_CRLF_BRANCHES="${LIB_CRLF_BRANCHES} ${branch}" &&
+ test_tick &&
+ hash=$(git commit-tree HEAD^{tree} -p HEAD -F .crlf-message-${branch}.txt) &&
+ git branch ${branch} ${hash} &&
+ git tag tag-${branch} ${branch} -F .crlf-message-${branch}.txt --cleanup=verbatim
+}
+
+create_crlf_refs () {
+ create_crlf_ref crlf <<-\EOF &&
+ Subject first line
+
+ Body first line
+ Body second line
+ EOF
+ create_crlf_ref crlf-empty-lines-after-subject <<-\EOF &&
+ Subject first line
+
+
+ Body first line
+ Body second line
+ EOF
+ create_crlf_ref crlf-two-line-subject <<-\EOF &&
+ Subject first line
+ Subject second line
+
+ Body first line
+ Body second line
+ EOF
+ create_crlf_ref crlf-two-line-subject-no-body <<-\EOF &&
+ Subject first line
+ Subject second line
+ EOF
+ create_crlf_ref crlf-two-line-subject-no-body-trailing-newline <<-\EOF
+ Subject first line
+ Subject second line
+
+ EOF
+}
+
+test_crlf_subject_body_and_contents() {
+ command_and_args="$@" &&
+ command=$1 &&
+ if test ${command} = "branch" || test ${command} = "for-each-ref" || test ${command} = "tag"
+ then
+ atoms="(contents:subject) (contents:body) (contents)"
+ elif test ${command} = "log" || test ${command} = "show"
+ then
+ atoms="s b B"
+ fi &&
+ files="subject body message" &&
+ while test -n "${atoms}"
+ do
+ set ${atoms} && atom=$1 && shift && atoms="$*" &&
+ set ${files} && file=$1 && shift && files="$*" &&
+ test_expect_success "${command}: --format='%${atom}' works with messages using CRLF" "
+ rm -f expect &&
+ for ref in ${LIB_CRLF_BRANCHES}
+ do
+ cat .crlf-${file}-\"\${ref}\".txt >>expect &&
+ printf \"\n\" >>expect || return 1
+ done &&
+ git $command_and_args --format=\"%${atom}\" >actual &&
+ test_cmp expect actual
+ "
+ done
+}
+
+
+test_expect_success 'Setup refs with commit and tag messages using CRLF' '
+ test_commit inital &&
+ create_crlf_refs
+'
+
+test_expect_success 'branch: --verbose works with messages using CRLF' '
+ rm -f expect &&
+ for branch in $LIB_CRLF_BRANCHES
+ do
+ printf " " >>expect &&
+ cat .crlf-subject-${branch}.txt >>expect &&
+ printf "\n" >>expect || return 1
+ done &&
+ git branch -v >tmp &&
+ # Remove first two columns, and the line for the currently checked out branch
+ current=$(git branch --show-current) &&
+ grep -v $current <tmp | awk "{\$1=\$2=\"\"}1" >actual &&
+ test_cmp expect actual
+'
+
+test_crlf_subject_body_and_contents branch --list crlf*
+
+test_crlf_subject_body_and_contents tag --list tag-crlf*
+
+test_crlf_subject_body_and_contents for-each-ref refs/heads/crlf*
+
+test_expect_success 'log: --oneline works with messages using CRLF' '
+ for branch in $LIB_CRLF_BRANCHES
+ do
+ cat .crlf-subject-${branch}.txt >expect &&
+ printf "\n" >>expect &&
+ git log --oneline -1 ${branch} >tmp-branch &&
+ git log --oneline -1 tag-${branch} >tmp-tag &&
+ cut -d" " -f2- <tmp-branch >actual-branch &&
+ cut -d" " -f2- <tmp-tag >actual-tag &&
+ test_cmp expect actual-branch &&
+ test_cmp expect actual-tag || return 1
+ done
+'
+
+test_crlf_subject_body_and_contents log --all --reverse --grep Subject
+
+test_crlf_subject_body_and_contents show $LIB_CRLF_BRANCHES
+
+test_done
diff --git a/t/t4000-diff-format.sh b/t/t4000-diff-format.sh
new file mode 100755
index 0000000..8d50331
--- /dev/null
+++ b/t/t4000-diff-format.sh
@@ -0,0 +1,126 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+test_description='Test built-in diff output engine.
+
+We happen to know that all diff plumbing and diff Porcelain share the
+same command line parser, so testing one should be sufficient; pick
+diff-files as a representative.
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-diff.sh
+
+echo >path0 'Line 1
+Line 2
+line 3'
+cat path0 >path1
+chmod +x path1
+mkdir path2
+>path2/path3
+
+test_expect_success 'update-index --add two files with and without +x.' '
+ git update-index --add path0 path1 path2/path3
+'
+
+mv path0 path0-
+sed -e 's/line/Line/' <path0- >path0
+chmod +x path0
+rm -f path1
+test_expect_success 'git diff-files -p after editing work tree.' '
+ git diff-files -p >actual
+'
+
+# that's as far as it comes
+if [ "$(git config --get core.filemode)" = false ]
+then
+ say 'filemode disabled on the filesystem'
+ test_done
+fi
+
+cat >expected <<\EOF
+diff --git a/path0 b/path0
+old mode 100644
+new mode 100755
+--- a/path0
++++ b/path0
+@@ -1,3 +1,3 @@
+ Line 1
+ Line 2
+-line 3
++Line 3
+diff --git a/path1 b/path1
+deleted file mode 100755
+--- a/path1
++++ /dev/null
+@@ -1,3 +0,0 @@
+-Line 1
+-Line 2
+-line 3
+EOF
+
+test_expect_success 'validate git diff-files -p output.' '
+ compare_diff_patch expected actual
+'
+
+test_expect_success 'git diff-files -s after editing work tree' '
+ git diff-files -s >actual 2>err &&
+ test_must_be_empty actual &&
+ test_must_be_empty err
+'
+
+test_expect_success 'git diff-files --no-patch as synonym for -s' '
+ git diff-files --no-patch >actual 2>err &&
+ test_must_be_empty actual &&
+ test_must_be_empty err
+'
+
+test_expect_success 'git diff-files --no-patch --patch shows the patch' '
+ git diff-files --no-patch --patch >actual &&
+ compare_diff_patch expected actual
+'
+
+test_expect_success 'git diff-files --no-patch --patch-with-raw shows the patch and raw data' '
+ git diff-files --no-patch --patch-with-raw >actual &&
+ grep -q "^:100644 100755 .* $ZERO_OID M path0\$" actual &&
+ tail -n +4 actual >actual-patch &&
+ compare_diff_patch expected actual-patch
+'
+
+test_expect_success 'git diff-files --patch --no-patch does not show the patch' '
+ git diff-files --patch --no-patch >actual 2>err &&
+ test_must_be_empty actual &&
+ test_must_be_empty err
+'
+
+
+# Smudge path2/path3 so that dirstat has something to show
+date >path2/path3
+
+for format in stat raw numstat shortstat summary \
+ dirstat cumulative dirstat-by-file \
+ patch-with-raw patch-with-stat compact-summary
+do
+ test_expect_success "--no-patch in 'git diff-files --no-patch --$format' is a no-op" '
+ git diff-files --no-patch "--$format" >actual &&
+ git diff-files "--$format" >expect &&
+ test_cmp expect actual
+ '
+
+ test_expect_success "--no-patch clears all previous ones" '
+ git diff-files --$format -s -p >actual &&
+ git diff-files -p >expect &&
+ test_cmp expect actual
+ '
+
+ test_expect_success "--no-patch in 'git diff --no-patch --$format' is a no-op" '
+ git diff --no-patch "--$format" >actual &&
+ git diff "--$format" >expect &&
+ test_cmp expect actual
+ '
+done
+
+test_done
diff --git a/t/t4001-diff-rename.sh b/t/t4001-diff-rename.sh
new file mode 100755
index 0000000..85be136
--- /dev/null
+++ b/t/t4001-diff-rename.sh
@@ -0,0 +1,289 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+test_description='Test rename detection in diff engine.
+
+'
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-diff.sh
+
+test_expect_success 'setup' '
+ cat >path0 <<-\EOF &&
+ Line 1
+ Line 2
+ Line 3
+ Line 4
+ Line 5
+ Line 6
+ Line 7
+ Line 8
+ Line 9
+ Line 10
+ line 11
+ Line 12
+ Line 13
+ Line 14
+ Line 15
+ EOF
+ cat >expected <<-\EOF &&
+ diff --git a/path0 b/path1
+ rename from path0
+ rename to path1
+ --- a/path0
+ +++ b/path1
+ @@ -8,7 +8,7 @@ Line 7
+ Line 8
+ Line 9
+ Line 10
+ -line 11
+ +Line 11
+ Line 12
+ Line 13
+ Line 14
+ EOF
+ cat >no-rename <<-\EOF
+ diff --git a/path0 b/path0
+ deleted file mode 100644
+ index fdbec44..0000000
+ --- a/path0
+ +++ /dev/null
+ @@ -1,15 +0,0 @@
+ -Line 1
+ -Line 2
+ -Line 3
+ -Line 4
+ -Line 5
+ -Line 6
+ -Line 7
+ -Line 8
+ -Line 9
+ -Line 10
+ -line 11
+ -Line 12
+ -Line 13
+ -Line 14
+ -Line 15
+ diff --git a/path1 b/path1
+ new file mode 100644
+ index 0000000..752c50e
+ --- /dev/null
+ +++ b/path1
+ @@ -0,0 +1,15 @@
+ +Line 1
+ +Line 2
+ +Line 3
+ +Line 4
+ +Line 5
+ +Line 6
+ +Line 7
+ +Line 8
+ +Line 9
+ +Line 10
+ +Line 11
+ +Line 12
+ +Line 13
+ +Line 14
+ +Line 15
+ EOF
+'
+
+test_expect_success \
+ 'update-index --add a file.' \
+ 'git update-index --add path0'
+
+test_expect_success \
+ 'write that tree.' \
+ 'tree=$(git write-tree) && echo $tree'
+
+sed -e 's/line/Line/' <path0 >path1
+rm -f path0
+test_expect_success \
+ 'renamed and edited the file.' \
+ 'git update-index --add --remove path0 path1'
+
+test_expect_success \
+ 'git diff-index -p -M after rename and editing.' \
+ 'git diff-index -p -M $tree >current'
+
+
+test_expect_success \
+ 'validate the output.' \
+ 'compare_diff_patch current expected'
+
+test_expect_success 'test diff.renames=true' '
+ git -c diff.renames=true diff --cached $tree >current &&
+ compare_diff_patch current expected
+'
+
+test_expect_success 'test diff.renames=false' '
+ git -c diff.renames=false diff --cached $tree >current &&
+ compare_diff_patch current no-rename
+'
+
+test_expect_success 'test diff.renames unset' '
+ git diff --cached $tree >current &&
+ compare_diff_patch current expected
+'
+
+test_expect_success 'favour same basenames over different ones' '
+ cp path1 another-path &&
+ git add another-path &&
+ git commit -m 1 &&
+ git rm path1 &&
+ mkdir subdir &&
+ git mv another-path subdir/path1 &&
+ git status >out &&
+ test_grep "renamed: .*path1 -> subdir/path1" out
+'
+
+test_expect_success 'test diff.renames=true for git status' '
+ git -c diff.renames=true status >out &&
+ test_grep "renamed: .*path1 -> subdir/path1" out
+'
+
+test_expect_success 'test diff.renames=false for git status' '
+ git -c diff.renames=false status >out &&
+ test_grep ! "renamed: .*path1 -> subdir/path1" out &&
+ test_grep "new file: .*subdir/path1" out &&
+ test_grep "deleted: .*[^/]path1" out
+'
+
+test_expect_success 'favour same basenames even with minor differences' '
+ git show HEAD:path1 | sed "s/15/16/" > subdir/path1 &&
+ git status >out &&
+ test_grep "renamed: .*path1 -> subdir/path1" out
+'
+
+test_expect_success 'two files with same basename and same content' '
+ git reset --hard &&
+ mkdir -p dir/A dir/B &&
+ cp path1 dir/A/file &&
+ cp path1 dir/B/file &&
+ git add dir &&
+ git commit -m 2 &&
+ git mv dir other-dir &&
+ git status >out &&
+ test_grep "renamed: .*dir/A/file -> other-dir/A/file" out
+'
+
+test_expect_success 'setup for many rename source candidates' '
+ git reset --hard &&
+ for i in 0 1 2 3 4 5 6 7 8 9;
+ do
+ for j in 0 1 2 3 4 5 6 7 8 9;
+ do
+ echo "$i$j" >"path$i$j" || return 1
+ done
+ done &&
+ git add "path??" &&
+ test_tick &&
+ git commit -m "hundred" &&
+ (cat path1 && echo new) >new-path &&
+ echo old >>path1 &&
+ git add new-path path1 &&
+ git diff -l 4 -C -C --cached --name-status >actual 2>actual.err &&
+ sed -e "s/^\([CM]\)[0-9]* /\1 /" actual >actual.munged &&
+ cat >expect <<-EOF &&
+ C path1 new-path
+ M path1
+ EOF
+ test_cmp expect actual.munged &&
+ grep warning actual.err
+'
+
+test_expect_success 'rename pretty print with nothing in common' '
+ mkdir -p a/b/ &&
+ : >a/b/c &&
+ git add a/b/c &&
+ git commit -m "create a/b/c" &&
+ mkdir -p c/b/ &&
+ git mv a/b/c c/b/a &&
+ git commit -m "a/b/c -> c/b/a" &&
+ git diff -M --summary HEAD^ HEAD >output &&
+ test_grep " a/b/c => c/b/a " output &&
+ git diff -M --stat HEAD^ HEAD >output &&
+ test_grep " a/b/c => c/b/a " output
+'
+
+test_expect_success 'rename pretty print with common prefix' '
+ mkdir -p c/d &&
+ git mv c/b/a c/d/e &&
+ git commit -m "c/b/a -> c/d/e" &&
+ git diff -M --summary HEAD^ HEAD >output &&
+ test_grep " c/{b/a => d/e} " output &&
+ git diff -M --stat HEAD^ HEAD >output &&
+ test_grep " c/{b/a => d/e} " output
+'
+
+test_expect_success 'rename pretty print with common suffix' '
+ mkdir d &&
+ git mv c/d/e d/e &&
+ git commit -m "c/d/e -> d/e" &&
+ git diff -M --summary HEAD^ HEAD >output &&
+ test_grep " {c/d => d}/e " output &&
+ git diff -M --stat HEAD^ HEAD >output &&
+ test_grep " {c/d => d}/e " output
+'
+
+test_expect_success 'rename pretty print with common prefix and suffix' '
+ mkdir d/f &&
+ git mv d/e d/f/e &&
+ git commit -m "d/e -> d/f/e" &&
+ git diff -M --summary HEAD^ HEAD >output &&
+ test_grep " d/{ => f}/e " output &&
+ git diff -M --stat HEAD^ HEAD >output &&
+ test_grep " d/{ => f}/e " output
+'
+
+test_expect_success 'rename pretty print common prefix and suffix overlap' '
+ mkdir d/f/f &&
+ git mv d/f/e d/f/f/e &&
+ git commit -m "d/f/e d/f/f/e" &&
+ git diff -M --summary HEAD^ HEAD >output &&
+ test_grep " d/f/{ => f}/e " output &&
+ git diff -M --stat HEAD^ HEAD >output &&
+ test_grep " d/f/{ => f}/e " output
+'
+
+test_expect_success 'diff-tree -l0 defaults to a big rename limit, not zero' '
+ test_write_lines line1 line2 line3 >myfile &&
+ git add myfile &&
+ git commit -m x &&
+
+ test_write_lines line1 line2 line4 >myotherfile &&
+ git rm myfile &&
+ git add myotherfile &&
+ git commit -m x &&
+
+ git diff-tree -M -l0 HEAD HEAD^ >actual &&
+ # Verify that a rename from myotherfile to myfile was detected
+ grep "myotherfile.*myfile" actual
+'
+
+test_expect_success 'basename similarity vs best similarity' '
+ mkdir subdir &&
+ test_write_lines line1 line2 line3 line4 line5 \
+ line6 line7 line8 line9 line10 >subdir/file.txt &&
+ git add subdir/file.txt &&
+ git commit -m "base txt" &&
+
+ git rm subdir/file.txt &&
+ test_write_lines line1 line2 line3 line4 line5 \
+ line6 line7 line8 >file.txt &&
+ test_write_lines line1 line2 line3 line4 line5 \
+ line6 line7 line8 line9 >file.md &&
+ git add file.txt file.md &&
+ git commit -a -m "rename" &&
+ git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
+ # subdir/file.txt is 88% similar to file.md, 78% similar to file.txt,
+ # but since same basenames are checked first...
+ cat >expected <<-\EOF &&
+ A file.md
+ R078 subdir/file.txt file.txt
+ EOF
+ test_cmp expected actual
+'
+
+test_done
diff --git a/t/t4002-diff-basic.sh b/t/t4002-diff-basic.sh
new file mode 100755
index 0000000..7afc883
--- /dev/null
+++ b/t/t4002-diff-basic.sh
@@ -0,0 +1,414 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+test_description='Test diff raw-output.
+
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+. "$TEST_DIRECTORY"/lib-read-tree-m-3way.sh
+
+test_oid_cache <<\EOF
+aa_1 sha1:ccba72ad3888a3520b39efcf780b9ee64167535d
+aa_1 sha256:9febfbf18197819b2735c45291f138525d2476d59470f98239647544586ba403
+
+aa_2 sha1:6aa2b5335b16431a0ef71e5c0a28be69183cf6a2
+aa_2 sha256:6eaa3437de83f145a4aaa6ba355303075ade547b128ec6a2cd00a81ff7ce7a56
+
+an_1 sha1:7e426fb079479fd67f6d81f984e4ec649a44bc25
+an_1 sha256:8f92a0bec99e399a38e3bd0e1bf19fbf121e0160efb29b857df79d439f1c4536
+
+dd_1 sha1:bcc68ef997017466d5c9094bcf7692295f588c9a
+dd_1 sha256:07e17428b00639b85485d2b01083d219e2f3e3ba8579e9ca44e9cc8dd554d952
+
+df_1 sha1:6d50f65d3bdab91c63444294d38f08aeff328e42
+df_1 sha256:e367cecc27e9bf5451b1c65828cb21938d36a5f8e39c1b03ad6509cc36bb8e9d
+
+df_2 sha1:71420ab81e254145d26d6fc0cddee64c1acd4787
+df_2 sha256:0f0a86d10347ff6921d03a3c954679f3f1d14fa3d5cd82f57b32c09755f3a47d
+
+dfd1 sha1:68a6d8b91da11045cf4aa3a5ab9f2a781c701249
+dfd1 sha256:f3bd3265b02b6978ce86490d8ad026c573639c974b3de1d9faf30d8d5a77d3d5
+
+dm_1 sha1:141c1f1642328e4bc46a7d801a71da392e66791e
+dm_1 sha256:c89f8656e7b94e21ee5fbaf0e2149bbf783c51edbe2ce110349cac13059ee7ed
+
+dm_2 sha1:3c4d8de5fbad08572bab8e10eef8dbb264cf0231
+dm_2 sha256:83a572e37e0c94086294dae2cecc43d9131afd6f6c906e495c78972230b54988
+
+dn_1 sha1:35abde1506ddf806572ff4d407bd06885d0f8ee9
+dn_1 sha256:775d5852582070e620be63327bfa515fab8f71c7ac3e4f0c3cd6267b4377ba28
+
+ll_2 sha1:1d41122ebdd7a640f29d3c9cc4f9d70094374762
+ll_2 sha256:7917b4948a883cfed0a77d3d5a625dc8577d6ddcc3c6c3bbc56c4d4226a2246d
+
+md_1 sha1:03f24c8c4700babccfd28b654e7e8eac402ad6cd
+md_1 sha256:fc9f30369b978595ad685ba11ca9a17de0af16d79cd4b629975f4f1590033902
+
+md_2 sha1:103d9f89b50b9aad03054b579be5e7aa665f2d57
+md_2 sha256:fc78ec75275628762fe520479a6b2398dec295ce7aabcb1d15e5963c7b4e9317
+
+mm_1 sha1:b258508afb7ceb449981bd9d63d2d3e971bf8d34
+mm_1 sha256:a4b7847d228e900e3000285e240c20fd96f9dd41ce1445305f6eada126d4a04a
+
+mm_2 sha1:b431b272d829ff3aa4d1a5085f4394ab4d3305b6
+mm_2 sha256:3f8b83ea36aacf689bcf1a1290a9a8ed341564d32682ea6f76fea9a979186782
+
+mm_3 sha1:19989d4559aae417fedee240ccf2ba315ea4dc2b
+mm_3 sha256:71b3bfc5747ac033fff9ea0ab39ee453a3af2969890e75d6ef547b87544e2681
+
+mn_1 sha1:bd084b0c27c7b6cc34f11d6d0509a29be3caf970
+mn_1 sha256:47a67450583d7a329eb01a7c4ba644945af72c0ed2c7c95eb5a00d6e46d4d483
+
+mn_2 sha1:a716d58de4a570e0038f5c307bd8db34daea021f
+mn_2 sha256:f95104c1ebe27acb84bac25a7be98c71f6b8d3054b21f357a5be0c524ad97e08
+
+nm_1 sha1:c8f25781e8f1792e3e40b74225e20553041b5226
+nm_1 sha256:09baddc7afaa62e62e152c23c9c3ab94bf15a3894031e227e9be7fe68e1f4e49
+
+nm_2 sha1:cdb9a8c3da571502ac30225e9c17beccb8387983
+nm_2 sha256:58b5227956ac2d2a08d0efa513c0ae37430948b16791ea3869a1308dbf05536d
+
+na_1 sha1:15885881ea69115351c09b38371f0348a3fb8c67
+na_1 sha256:18e4fdd1670cd7968ee23d35bfd29e5418d56fb190c840094c1c57ceee0aad8f
+
+nd_1 sha1:a4e179e4291e5536a5e1c82e091052772d2c5a93
+nd_1 sha256:07dac9b01d00956ea0c65bd993d7de4864aeef2ed3cbb1255d9f1d949fcd6df6
+
+ss_1 sha1:40c959f984c8b89a2b02520d17f00d717f024397
+ss_1 sha256:50fc1b5df74d9910db2f9270993484235f15b69b75b01bcfb53e059289d14af9
+
+ss_2 sha1:2ac547ae9614a00d1b28275de608131f7a0e259f
+ss_2 sha256:a90f02e6044f1497d13db587d22ab12f90150a7d1e084afcf96065fab35ae2bc
+
+tt_1 sha1:4ac13458899ab908ef3b1128fa378daefc88d356
+tt_1 sha256:c53113c7dd5060e86b5b251428bd058f6726f66273c6a24bff1c61a04f498dd3
+
+tt_2 sha1:4c86f9a85fbc5e6804ee2e17a797538fbe785bca
+tt_2 sha256:0775f2a296129a7cf2862b46bc0e88c14d593f2773a3e3fb1c5193db6f5a7e77
+
+tt_3 sha1:c4e4a12231b9fa79a0053cb6077fcb21bb5b135a
+tt_3 sha256:47860f93cdd211f96443e0560f21c57ab6c2f4b0ac27ff03651a352e53fe8484
+
+z__1 sha1:7d670fdcdb9929f6c7dac196ff78689cd1c566a1
+z__1 sha256:44d0f37aff5e51cfcfdd1134c93a6419bcca7b9964f792ffcd5f9b4fcba1ee63
+
+z__2 sha1:5e5f22072bb39f6e12cf663a57cb634c76eefb49
+z__2 sha256:d29de162113190fed104eb5f010820cef4e315f89b9326e8497f7219fb737894
+
+z__3 sha1:1ba523955d5160681af65cb776411f574c1e8155
+z__3 sha256:07422d772b07794ab4369a5648e617719f89c2d2212cbeab05d97214b6471636
+
+zaa1 sha1:8acb8e9750e3f644bf323fcf3d338849db106c77
+zaa1 sha256:e79b029282c8abec2d9f3f7faceaf2a1405e02d1f368e66450ae66cf5b68d1f4
+
+zaa2 sha1:6c0b99286d0bce551ac4a7b3dff8b706edff3715
+zaa2 sha256:c82bd78c3e69ea1796e6b1a7a3ba45bb106c50e819296475b862123d3f5cc5a0
+
+zan1 sha1:087494262084cefee7ed484d20c8dc0580791272
+zan1 sha256:4b159eb3804d05599023dd074f771d06d02870f4ab24a7165add8ac3d703b8d3
+
+zdd1 sha1:879007efae624d2b1307214b24a956f0a8d686a8
+zdd1 sha256:eecfdd4d8092dd0363fb6d4548b54c6afc8982c3ed9b34e393f1d6a921d8eaa3
+
+zdm1 sha1:9b541b2275c06e3a7b13f28badf5294e2ae63df4
+zdm1 sha256:ab136e88e19a843c4bf7713d2090d5a2186ba16a6a80dacc12eeddd256a8e556
+
+zdm2 sha1:d77371d15817fcaa57eeec27f770c505ba974ec1
+zdm2 sha256:1c1a5f57363f46a15d95ce8527b3c2c158d88d16853b4acbf81bd20fd2c89a46
+
+zdn1 sha1:beb5d38c55283d280685ea21a0e50cfcc0ca064a
+zdn1 sha256:0f0eca66183617b0aa5ad74b256540329f841470922ca6760263c996d825eb18
+
+zmd1 sha1:d41fda41b7ec4de46b43cb7ea42a45001ae393d5
+zmd1 sha256:1ed32d481852eddf31a0ce12652a0ad14bf5b7a842667b5dbb0b50f35bf1c80a
+
+zmd2 sha1:a79ac3be9377639e1c7d1edf1ae1b3a5f0ccd8a9
+zmd2 sha256:b238da211b404f8917df2d9c6f7030535e904b2186131007a3c292ec6902f933
+
+zmm1 sha1:4ca22bae2527d3d9e1676498a0fba3b355bd1278
+zmm1 sha256:072b1d85b5f34fabc99dfa46008c5418df68302d3e317430006f49b32d244226
+
+zmm2 sha1:61422ba9c2c873416061a88cd40a59a35b576474
+zmm2 sha256:81dd5d2b3c5cda16fef552256aed4e2ea0802a8450a08f308a92142112ff6dda
+
+zmm3 sha1:697aad7715a1e7306ca76290a3dd4208fbaeddfa
+zmm3 sha256:8b10fab49e9be3414aa5e9a93d0e46f9569053440138a7c19a5eb5536d8e95bf
+
+zmn1 sha1:b16d7b25b869f2beb124efa53467d8a1550ad694
+zmn1 sha256:609e4f75d1295e844c826feeba213acb0b6cfc609adfe8ff705b19e3829ae3e9
+
+zmn2 sha1:a5c544c21cfcb07eb80a4d89a5b7d1570002edfd
+zmn2 sha256:d6d03edf2dc1a3b267a8205de5f41a2ff4b03def8c7ae02052b543fb09d589fc
+
+zna1 sha1:d12979c22fff69c59ca9409e7a8fe3ee25eaee80
+zna1 sha256:b37b80e789e8ea32aa323f004628f02013f632124b0282c7fe00a127d3c64c3c
+
+znd1 sha1:a18393c636b98e9bd7296b8b437ea4992b72440c
+znd1 sha256:af92a22eee8c38410a0c9d2b5135a10aeb052cbc7cf675541ed9a67bfcaf7cf9
+
+znm1 sha1:3fdbe17fd013303a2e981e1ca1c6cd6e72789087
+znm1 sha256:f75aeaa0c11e76918e381c105f0752932c6150e941fec565d24fa31098a13dc1
+
+znm2 sha1:7e09d6a3a14bd630913e8c75693cea32157b606d
+znm2 sha256:938d73cfbaa1c902a84fb5b3afd9736aa0590367fb9bd59c6c4d072ce70fcd6d
+EOF
+
+cat >.test-plain-OA <<EOF
+:000000 100644 $(test_oid zero) $(test_oid aa_1) A AA
+:000000 100644 $(test_oid zero) $(test_oid an_1) A AN
+:100644 000000 $(test_oid dd_1) $(test_oid zero) D DD
+:000000 040000 $(test_oid zero) $(test_oid df_1) A DF
+:100644 000000 $(test_oid dm_1) $(test_oid zero) D DM
+:100644 000000 $(test_oid dn_1) $(test_oid zero) D DN
+:000000 100644 $(test_oid zero) $(test_oid ll_2) A LL
+:100644 100644 $(test_oid md_1) $(test_oid md_2) M MD
+:100644 100644 $(test_oid mm_1) $(test_oid mm_2) M MM
+:100644 100644 $(test_oid mn_1) $(test_oid mn_2) M MN
+:100644 100644 $(test_oid ss_1) $(test_oid ss_2) M SS
+:100644 100644 $(test_oid tt_1) $(test_oid tt_2) M TT
+:040000 040000 $(test_oid z__1) $(test_oid z__2) M Z
+EOF
+
+cat >.test-recursive-OA <<EOF
+:000000 100644 $(test_oid zero) $(test_oid aa_1) A AA
+:000000 100644 $(test_oid zero) $(test_oid an_1) A AN
+:100644 000000 $(test_oid dd_1) $(test_oid zero) D DD
+:000000 100644 $(test_oid zero) $(test_oid dfd1) A DF/DF
+:100644 000000 $(test_oid dm_1) $(test_oid zero) D DM
+:100644 000000 $(test_oid dn_1) $(test_oid zero) D DN
+:000000 100644 $(test_oid zero) $(test_oid ll_2) A LL
+:100644 100644 $(test_oid md_1) $(test_oid md_2) M MD
+:100644 100644 $(test_oid mm_1) $(test_oid mm_2) M MM
+:100644 100644 $(test_oid mn_1) $(test_oid mn_2) M MN
+:100644 100644 $(test_oid ss_1) $(test_oid ss_2) M SS
+:100644 100644 $(test_oid tt_1) $(test_oid tt_2) M TT
+:000000 100644 $(test_oid zero) $(test_oid zaa1) A Z/AA
+:000000 100644 $(test_oid zero) $(test_oid zan1) A Z/AN
+:100644 000000 $(test_oid zdd1) $(test_oid zero) D Z/DD
+:100644 000000 $(test_oid zdm1) $(test_oid zero) D Z/DM
+:100644 000000 $(test_oid zdn1) $(test_oid zero) D Z/DN
+:100644 100644 $(test_oid zmd1) $(test_oid zmd2) M Z/MD
+:100644 100644 $(test_oid zmm1) $(test_oid zmm2) M Z/MM
+:100644 100644 $(test_oid zmn1) $(test_oid zmn2) M Z/MN
+EOF
+cat >.test-plain-OB <<EOF
+:000000 100644 $(test_oid zero) $(test_oid aa_2) A AA
+:100644 000000 $(test_oid dd_1) $(test_oid zero) D DD
+:000000 100644 $(test_oid zero) $(test_oid df_2) A DF
+:100644 100644 $(test_oid dm_1) $(test_oid dm_2) M DM
+:000000 100644 $(test_oid zero) $(test_oid ll_2) A LL
+:100644 000000 $(test_oid md_1) $(test_oid zero) D MD
+:100644 100644 $(test_oid mm_1) $(test_oid mm_3) M MM
+:000000 100644 $(test_oid zero) $(test_oid na_1) A NA
+:100644 000000 $(test_oid nd_1) $(test_oid zero) D ND
+:100644 100644 $(test_oid nm_1) $(test_oid nm_2) M NM
+:100644 100644 $(test_oid ss_1) $(test_oid ss_2) M SS
+:100644 100644 $(test_oid tt_1) $(test_oid tt_3) M TT
+:040000 040000 $(test_oid z__1) $(test_oid z__3) M Z
+EOF
+cat >.test-recursive-OB <<EOF
+:000000 100644 $(test_oid zero) $(test_oid aa_2) A AA
+:100644 000000 $(test_oid dd_1) $(test_oid zero) D DD
+:000000 100644 $(test_oid zero) $(test_oid df_2) A DF
+:100644 100644 $(test_oid dm_1) $(test_oid dm_2) M DM
+:000000 100644 $(test_oid zero) $(test_oid ll_2) A LL
+:100644 000000 $(test_oid md_1) $(test_oid zero) D MD
+:100644 100644 $(test_oid mm_1) $(test_oid mm_3) M MM
+:000000 100644 $(test_oid zero) $(test_oid na_1) A NA
+:100644 000000 $(test_oid nd_1) $(test_oid zero) D ND
+:100644 100644 $(test_oid nm_1) $(test_oid nm_2) M NM
+:100644 100644 $(test_oid ss_1) $(test_oid ss_2) M SS
+:100644 100644 $(test_oid tt_1) $(test_oid tt_3) M TT
+:000000 100644 $(test_oid zero) $(test_oid zaa2) A Z/AA
+:100644 000000 $(test_oid zdd1) $(test_oid zero) D Z/DD
+:100644 100644 $(test_oid zdm1) $(test_oid zdm2) M Z/DM
+:100644 000000 $(test_oid zmd1) $(test_oid zero) D Z/MD
+:100644 100644 $(test_oid zmm1) $(test_oid zmm3) M Z/MM
+:000000 100644 $(test_oid zero) $(test_oid zna1) A Z/NA
+:100644 000000 $(test_oid znd1) $(test_oid zero) D Z/ND
+:100644 100644 $(test_oid znm1) $(test_oid znm2) M Z/NM
+EOF
+cat >.test-plain-AB <<EOF
+:100644 100644 $(test_oid aa_1) $(test_oid aa_2) M AA
+:100644 000000 $(test_oid an_1) $(test_oid zero) D AN
+:000000 100644 $(test_oid zero) $(test_oid df_2) A DF
+:040000 000000 $(test_oid df_1) $(test_oid zero) D DF
+:000000 100644 $(test_oid zero) $(test_oid dm_2) A DM
+:000000 100644 $(test_oid zero) $(test_oid dn_1) A DN
+:100644 000000 $(test_oid md_2) $(test_oid zero) D MD
+:100644 100644 $(test_oid mm_2) $(test_oid mm_3) M MM
+:100644 100644 $(test_oid mn_2) $(test_oid mn_1) M MN
+:000000 100644 $(test_oid zero) $(test_oid na_1) A NA
+:100644 000000 $(test_oid nd_1) $(test_oid zero) D ND
+:100644 100644 $(test_oid nm_1) $(test_oid nm_2) M NM
+:100644 100644 $(test_oid tt_2) $(test_oid tt_3) M TT
+:040000 040000 $(test_oid z__2) $(test_oid z__3) M Z
+EOF
+cat >.test-recursive-AB <<EOF
+:100644 100644 $(test_oid aa_1) $(test_oid aa_2) M AA
+:100644 000000 $(test_oid an_1) $(test_oid zero) D AN
+:000000 100644 $(test_oid zero) $(test_oid df_2) A DF
+:100644 000000 $(test_oid dfd1) $(test_oid zero) D DF/DF
+:000000 100644 $(test_oid zero) $(test_oid dm_2) A DM
+:000000 100644 $(test_oid zero) $(test_oid dn_1) A DN
+:100644 000000 $(test_oid md_2) $(test_oid zero) D MD
+:100644 100644 $(test_oid mm_2) $(test_oid mm_3) M MM
+:100644 100644 $(test_oid mn_2) $(test_oid mn_1) M MN
+:000000 100644 $(test_oid zero) $(test_oid na_1) A NA
+:100644 000000 $(test_oid nd_1) $(test_oid zero) D ND
+:100644 100644 $(test_oid nm_1) $(test_oid nm_2) M NM
+:100644 100644 $(test_oid tt_2) $(test_oid tt_3) M TT
+:100644 100644 $(test_oid zaa1) $(test_oid zaa2) M Z/AA
+:100644 000000 $(test_oid zan1) $(test_oid zero) D Z/AN
+:000000 100644 $(test_oid zero) $(test_oid zdm2) A Z/DM
+:000000 100644 $(test_oid zero) $(test_oid zdn1) A Z/DN
+:100644 000000 $(test_oid zmd2) $(test_oid zero) D Z/MD
+:100644 100644 $(test_oid zmm2) $(test_oid zmm3) M Z/MM
+:100644 100644 $(test_oid zmn2) $(test_oid zmn1) M Z/MN
+:000000 100644 $(test_oid zero) $(test_oid zna1) A Z/NA
+:100644 000000 $(test_oid znd1) $(test_oid zero) D Z/ND
+:100644 100644 $(test_oid znm1) $(test_oid znm2) M Z/NM
+EOF
+
+cmp_diff_files_output () {
+ # diff-files never reports additions. Also it does not fill in the
+ # object ID for the changed files because it wants you to look at the
+ # filesystem.
+ sed <"$2" >.test-tmp \
+ -e '/^:000000 /d;s/'$OID_REGEX'\( [MCRNDU][0-9]*\) /'$ZERO_OID'\1 /' &&
+ test_cmp "$1" .test-tmp
+}
+
+test_expect_success 'diff-tree of known trees.' '
+ git diff-tree $tree_O $tree_A >.test-a &&
+ cmp -s .test-a .test-plain-OA
+'
+
+test_expect_success 'diff-tree of known trees.' '
+ git diff-tree -r $tree_O $tree_A >.test-a &&
+ cmp -s .test-a .test-recursive-OA
+'
+
+test_expect_success 'diff-tree of known trees.' '
+ git diff-tree $tree_O $tree_B >.test-a &&
+ cmp -s .test-a .test-plain-OB
+'
+
+test_expect_success 'diff-tree of known trees.' '
+ git diff-tree -r $tree_O $tree_B >.test-a &&
+ cmp -s .test-a .test-recursive-OB
+'
+
+test_expect_success 'diff-tree of known trees.' '
+ git diff-tree $tree_A $tree_B >.test-a &&
+ cmp -s .test-a .test-plain-AB
+'
+
+test_expect_success 'diff-tree of known trees.' '
+ git diff-tree -r $tree_A $tree_B >.test-a &&
+ cmp -s .test-a .test-recursive-AB
+'
+
+test_expect_success 'diff-tree --stdin of known trees.' '
+ echo $tree_A $tree_B | git diff-tree --stdin > .test-a &&
+ echo $tree_A $tree_B > .test-plain-ABx &&
+ cat .test-plain-AB >> .test-plain-ABx &&
+ cmp -s .test-a .test-plain-ABx
+'
+
+test_expect_success 'diff-tree --stdin of known trees.' '
+ echo $tree_A $tree_B | git diff-tree -r --stdin > .test-a &&
+ echo $tree_A $tree_B > .test-recursive-ABx &&
+ cat .test-recursive-AB >> .test-recursive-ABx &&
+ cmp -s .test-a .test-recursive-ABx
+'
+
+test_expect_success 'diff-cache O with A in cache' '
+ git read-tree $tree_A &&
+ git diff-index --cached $tree_O >.test-a &&
+ cmp -s .test-a .test-recursive-OA
+'
+
+test_expect_success 'diff-cache O with B in cache' '
+ git read-tree $tree_B &&
+ git diff-index --cached $tree_O >.test-a &&
+ cmp -s .test-a .test-recursive-OB
+'
+
+test_expect_success 'diff-cache A with B in cache' '
+ git read-tree $tree_B &&
+ git diff-index --cached $tree_A >.test-a &&
+ cmp -s .test-a .test-recursive-AB
+'
+
+test_expect_success 'diff-files with O in cache and A checked out' '
+ rm -fr Z [A-Z][A-Z] &&
+ git read-tree $tree_A &&
+ git checkout-index -f -a &&
+ git read-tree --reset $tree_O &&
+ test_must_fail git update-index --refresh -q &&
+ git diff-files >.test-a &&
+ cmp_diff_files_output .test-a .test-recursive-OA
+'
+
+test_expect_success 'diff-files with O in cache and B checked out' '
+ rm -fr Z [A-Z][A-Z] &&
+ git read-tree $tree_B &&
+ git checkout-index -f -a &&
+ git read-tree --reset $tree_O &&
+ test_must_fail git update-index --refresh -q &&
+ git diff-files >.test-a &&
+ cmp_diff_files_output .test-a .test-recursive-OB
+'
+
+test_expect_success 'diff-files with A in cache and B checked out' '
+ rm -fr Z [A-Z][A-Z] &&
+ git read-tree $tree_B &&
+ git checkout-index -f -a &&
+ git read-tree --reset $tree_A &&
+ test_must_fail git update-index --refresh -q &&
+ git diff-files >.test-a &&
+ cmp_diff_files_output .test-a .test-recursive-AB
+'
+
+################################################################
+# Now we have established the baseline, we do not have to
+# rely on individual object ID values that much.
+
+test_expect_success 'diff-tree O A == diff-tree -R A O' '
+ git diff-tree $tree_O $tree_A >.test-a &&
+ git diff-tree -R $tree_A $tree_O >.test-b &&
+ cmp -s .test-a .test-b
+'
+
+test_expect_success 'diff-tree -r O A == diff-tree -r -R A O' '
+ git diff-tree -r $tree_O $tree_A >.test-a &&
+ git diff-tree -r -R $tree_A $tree_O >.test-b &&
+ cmp -s .test-a .test-b
+'
+
+test_expect_success 'diff-tree B A == diff-tree -R A B' '
+ git diff-tree $tree_B $tree_A >.test-a &&
+ git diff-tree -R $tree_A $tree_B >.test-b &&
+ cmp -s .test-a .test-b
+'
+
+test_expect_success 'diff-tree -r B A == diff-tree -r -R A B' '
+ git diff-tree -r $tree_B $tree_A >.test-a &&
+ git diff-tree -r -R $tree_A $tree_B >.test-b &&
+ cmp -s .test-a .test-b'
+
+test_expect_success 'diff can read from stdin' '
+ test_must_fail git diff --no-index -- MN - < NN |
+ grep -v "^index" | sed "s#/-#/NN#" >.test-a &&
+ test_must_fail git diff --no-index -- MN NN |
+ grep -v "^index" >.test-b &&
+ test_cmp .test-a .test-b
+'
+
+test_done
diff --git a/t/t4003-diff-rename-1.sh b/t/t4003-diff-rename-1.sh
new file mode 100755
index 0000000..ebe0918
--- /dev/null
+++ b/t/t4003-diff-rename-1.sh
@@ -0,0 +1,130 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+test_description='More rename detection
+
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-diff.sh ;# test-lib chdir's into trash
+
+test_expect_success 'prepare reference tree' '
+ COPYING_test_data >COPYING &&
+ echo frotz >rezrov &&
+ git update-index --add COPYING rezrov &&
+ tree=$(git write-tree) &&
+ echo $tree
+'
+
+test_expect_success 'prepare work tree' '
+ sed -e 's/HOWEVER/However/' <COPYING >COPYING.1 &&
+ sed -e 's/GPL/G.P.L/g' <COPYING >COPYING.2 &&
+ rm -f COPYING &&
+ git update-index --add --remove COPYING COPYING.?
+'
+
+# tree has COPYING and rezrov. work tree has COPYING.1 and COPYING.2,
+# both are slightly edited, and unchanged rezrov. So we say you
+# copy-and-edit one, and rename-and-edit the other. We do not say
+# anything about rezrov.
+
+GIT_DIFF_OPTS=--unified=0 git diff-index -C -p $tree >current
+cat >expected <<\EOF
+diff --git a/COPYING b/COPYING.1
+copy from COPYING
+copy to COPYING.1
+--- a/COPYING
++++ b/COPYING.1
+@@ -6 +6 @@
+- HOWEVER, in order to allow a migration to GPLv3 if that seems like
++ However, in order to allow a migration to GPLv3 if that seems like
+diff --git a/COPYING b/COPYING.2
+rename from COPYING
+rename to COPYING.2
+--- a/COPYING
++++ b/COPYING.2
+@@ -2 +2 @@
+- Note that the only valid version of the GPL as far as this project
++ Note that the only valid version of the G.P.L as far as this project
+@@ -6 +6 @@
+- HOWEVER, in order to allow a migration to GPLv3 if that seems like
++ HOWEVER, in order to allow a migration to G.P.Lv3 if that seems like
+@@ -12 +12 @@
+- This file is licensed under the GPL v2, or a later version
++ This file is licensed under the G.P.L v2, or a later version
+EOF
+
+test_expect_success 'validate output from rename/copy detection (#1)' '
+ compare_diff_patch current expected
+'
+
+test_expect_success 'prepare work tree again' '
+ mv COPYING.2 COPYING &&
+ git update-index --add --remove COPYING COPYING.1 COPYING.2
+'
+
+# tree has COPYING and rezrov. work tree has COPYING and COPYING.1,
+# both are slightly edited, and unchanged rezrov. So we say you
+# edited one, and copy-and-edit the other. We do not say
+# anything about rezrov.
+
+GIT_DIFF_OPTS=--unified=0 git diff-index -C -p $tree >current
+cat >expected <<\EOF
+diff --git a/COPYING b/COPYING
+--- a/COPYING
++++ b/COPYING
+@@ -2 +2 @@
+- Note that the only valid version of the GPL as far as this project
++ Note that the only valid version of the G.P.L as far as this project
+@@ -6 +6 @@
+- HOWEVER, in order to allow a migration to GPLv3 if that seems like
++ HOWEVER, in order to allow a migration to G.P.Lv3 if that seems like
+@@ -12 +12 @@
+- This file is licensed under the GPL v2, or a later version
++ This file is licensed under the G.P.L v2, or a later version
+diff --git a/COPYING b/COPYING.1
+copy from COPYING
+copy to COPYING.1
+--- a/COPYING
++++ b/COPYING.1
+@@ -6 +6 @@
+- HOWEVER, in order to allow a migration to GPLv3 if that seems like
++ However, in order to allow a migration to GPLv3 if that seems like
+EOF
+
+test_expect_success 'validate output from rename/copy detection (#2)' '
+ compare_diff_patch current expected
+'
+
+test_expect_success 'prepare work tree once again' '
+ COPYING_test_data >COPYING &&
+ git update-index --add --remove COPYING COPYING.1
+'
+
+# tree has COPYING and rezrov. work tree has COPYING and COPYING.1,
+# but COPYING is not edited. We say you copy-and-edit COPYING.1; this
+# is only possible because -C mode now reports the unmodified file to
+# the diff-core. Unchanged rezrov, although being fed to
+# git diff-index as well, should not be mentioned.
+
+GIT_DIFF_OPTS=--unified=0 \
+ git diff-index -C --find-copies-harder -p $tree >current
+cat >expected <<\EOF
+diff --git a/COPYING b/COPYING.1
+copy from COPYING
+copy to COPYING.1
+--- a/COPYING
++++ b/COPYING.1
+@@ -6 +6 @@
+- HOWEVER, in order to allow a migration to GPLv3 if that seems like
++ However, in order to allow a migration to GPLv3 if that seems like
+EOF
+
+test_expect_success 'validate output from rename/copy detection (#3)' '
+ compare_diff_patch current expected
+'
+
+test_done
diff --git a/t/t4004-diff-rename-symlink.sh b/t/t4004-diff-rename-symlink.sh
new file mode 100755
index 0000000..1d70d4d
--- /dev/null
+++ b/t/t4004-diff-rename-symlink.sh
@@ -0,0 +1,71 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+test_description='More rename detection tests.
+
+The rename detection logic should be able to detect pure rename or
+copy of symbolic links, but should not produce rename/copy followed
+by an edit for them.
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-diff.sh
+
+test_expect_success SYMLINKS 'prepare reference tree' '
+ echo xyzzy | tr -d '\\\\'012 >yomin &&
+ ln -s xyzzy frotz &&
+ git update-index --add frotz yomin &&
+ tree=$(git write-tree) &&
+ echo $tree
+'
+
+test_expect_success SYMLINKS 'prepare work tree' '
+ mv frotz rezrov &&
+ rm -f yomin &&
+ ln -s xyzzy nitfol &&
+ ln -s xzzzy bozbar &&
+ git update-index --add --remove frotz rezrov nitfol bozbar yomin
+'
+
+# tree has frotz pointing at xyzzy, and yomin that contains xyzzy to
+# confuse things. work tree has rezrov (xyzzy) nitfol (xyzzy) and
+# bozbar (xzzzy).
+# rezrov and nitfol are rename/copy of frotz and bozbar should be
+# a new creation.
+
+test_expect_success SYMLINKS 'setup diff output' '
+ GIT_DIFF_OPTS=--unified=0 git diff-index -C -p $tree >current &&
+ cat >expected <<\EOF
+diff --git a/bozbar b/bozbar
+new file mode 120000
+--- /dev/null
++++ b/bozbar
+@@ -0,0 +1 @@
++xzzzy
+\ No newline at end of file
+diff --git a/frotz b/nitfol
+similarity index 100%
+copy from frotz
+copy to nitfol
+diff --git a/frotz b/rezrov
+similarity index 100%
+rename from frotz
+rename to rezrov
+diff --git a/yomin b/yomin
+deleted file mode 100644
+--- a/yomin
++++ /dev/null
+@@ -1 +0,0 @@
+-xyzzy
+\ No newline at end of file
+EOF
+'
+
+test_expect_success SYMLINKS 'validate diff output' '
+ compare_diff_patch current expected
+'
+
+test_done
diff --git a/t/t4005-diff-rename-2.sh b/t/t4005-diff-rename-2.sh
new file mode 100755
index 0000000..5c756dc
--- /dev/null
+++ b/t/t4005-diff-rename-2.sh
@@ -0,0 +1,79 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+test_description='Same rename detection as t4003 but testing diff-raw.'
+
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-diff.sh ;# test-lib chdir's into trash
+
+test_expect_success 'setup reference tree' '
+ COPYING_test_data >COPYING &&
+ echo frotz >rezrov &&
+ git update-index --add COPYING rezrov &&
+ tree=$(git write-tree) &&
+ echo $tree &&
+ sed -e "s/HOWEVER/However/" <COPYING >COPYING.1 &&
+ sed -e "s/GPL/G.P.L/g" <COPYING >COPYING.2 &&
+ origoid=$(git hash-object COPYING) &&
+ oid1=$(git hash-object COPYING.1) &&
+ oid2=$(git hash-object COPYING.2)
+'
+
+################################################################
+# tree has COPYING and rezrov. work tree has COPYING.1 and COPYING.2,
+# both are slightly edited, and unchanged rezrov. We say COPYING.1
+# and COPYING.2 are based on COPYING, and do not say anything about
+# rezrov.
+
+test_expect_success 'validate output from rename/copy detection (#1)' '
+ rm -f COPYING &&
+ git update-index --add --remove COPYING COPYING.? &&
+
+ cat <<-EOF >expected &&
+ :100644 100644 $origoid $oid1 C1234 COPYING COPYING.1
+ :100644 100644 $origoid $oid2 R1234 COPYING COPYING.2
+ EOF
+ git diff-index -C $tree >current &&
+ compare_diff_raw expected current
+'
+
+################################################################
+# tree has COPYING and rezrov. work tree has COPYING and COPYING.1,
+# both are slightly edited, and unchanged rezrov. We say COPYING.1
+# is based on COPYING and COPYING is still there, and do not say anything
+# about rezrov.
+
+test_expect_success 'validate output from rename/copy detection (#2)' '
+ mv COPYING.2 COPYING &&
+ git update-index --add --remove COPYING COPYING.1 COPYING.2 &&
+
+ cat <<-EOF >expected &&
+ :100644 100644 $origoid $oid2 M COPYING
+ :100644 100644 $origoid $oid1 C1234 COPYING COPYING.1
+ EOF
+ git diff-index -C $tree >current &&
+ compare_diff_raw current expected
+'
+
+################################################################
+# tree has COPYING and rezrov. work tree has the same COPYING and
+# copy-edited COPYING.1, and unchanged rezrov. We should not say
+# anything about rezrov or COPYING, since the revised again diff-raw
+# nows how to say Copy.
+
+test_expect_success 'validate output from rename/copy detection (#3)' '
+ COPYING_test_data >COPYING &&
+ git update-index --add --remove COPYING COPYING.1 &&
+
+ cat <<-EOF >expected &&
+ :100644 100644 $origoid $oid1 C1234 COPYING COPYING.1
+ EOF
+ git diff-index -C --find-copies-harder $tree >current &&
+ compare_diff_raw current expected
+'
+
+test_done
diff --git a/t/t4006-diff-mode.sh b/t/t4006-diff-mode.sh
new file mode 100755
index 0000000..dbd4c0d
--- /dev/null
+++ b/t/t4006-diff-mode.sh
@@ -0,0 +1,68 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+test_description='Test mode change diffs.
+
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+sed_script='s/\(:100644 100755\) \('"$OID_REGEX"'\) \2 /\1 X X /'
+
+test_expect_success 'setup' '
+ echo frotz >rezrov &&
+ git update-index --add rezrov &&
+ tree=$(git write-tree) &&
+ echo $tree
+'
+
+test_expect_success 'chmod' '
+ test_chmod +x rezrov &&
+ git diff-index $tree >current &&
+ sed -e "$sed_script" <current >check &&
+ echo ":100644 100755 X X M rezrov" >expected &&
+ test_cmp expected check
+'
+
+test_expect_success 'prepare binary file' '
+ git commit -m one &&
+ test_commit --printf two binbin "\00\01\02\03\04\05\06"
+'
+
+test_expect_success '--stat output after text chmod' '
+ test_chmod -x rezrov &&
+ cat >expect <<-\EOF &&
+ rezrov | 0
+ 1 file changed, 0 insertions(+), 0 deletions(-)
+ EOF
+ git diff HEAD --stat >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--shortstat output after text chmod' '
+ tail -n 1 <expect >expect.short &&
+ git diff HEAD --shortstat >actual &&
+ test_cmp expect.short actual
+'
+
+test_expect_success '--stat output after binary chmod' '
+ test_chmod +x binbin &&
+ cat >expect <<-EOF &&
+ binbin | Bin
+ rezrov | 0
+ 2 files changed, 0 insertions(+), 0 deletions(-)
+ EOF
+ git diff HEAD --stat >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--shortstat output after binary chmod' '
+ tail -n 1 <expect >expect.short &&
+ git diff HEAD --shortstat >actual &&
+ test_cmp expect.short actual
+'
+
+test_done
diff --git a/t/t4007-rename-3.sh b/t/t4007-rename-3.sh
new file mode 100755
index 0000000..b86165c
--- /dev/null
+++ b/t/t4007-rename-3.sh
@@ -0,0 +1,92 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+test_description='Rename interaction with pathspec.
+
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-diff.sh ;# test-lib chdir's into trash
+
+test_expect_success 'prepare reference tree' '
+ mkdir path0 path1 &&
+ COPYING_test_data >path0/COPYING &&
+ git update-index --add path0/COPYING &&
+ tree=$(git write-tree) &&
+ blob=$(git rev-parse :path0/COPYING)
+'
+
+test_expect_success 'prepare work tree' '
+ cp path0/COPYING path1/COPYING &&
+ git update-index --add --remove path0/COPYING path1/COPYING
+'
+
+# In the tree, there is only path0/COPYING. In the cache, path0 and
+# path1 both have COPYING and the latter is a copy of path0/COPYING.
+# Comparing the full tree with cache should tell us so.
+
+cat >expected <<EOF
+:100644 100644 $blob $blob C100 path0/COPYING path1/COPYING
+EOF
+
+test_expect_success 'copy detection' '
+ git diff-index -C --find-copies-harder $tree >current &&
+ compare_diff_raw current expected
+'
+
+test_expect_success 'copy detection, cached' '
+ git diff-index -C --find-copies-harder --cached $tree >current &&
+ compare_diff_raw current expected
+'
+
+# In the tree, there is only path0/COPYING. In the cache, path0 and
+# path1 both have COPYING and the latter is a copy of path0/COPYING.
+# However when we say we care only about path1, we should just see
+# path1/COPYING suddenly appearing from nowhere, not detected as
+# a copy from path0/COPYING.
+
+cat >expected <<EOF
+:000000 100644 $ZERO_OID $blob A path1/COPYING
+EOF
+
+test_expect_success 'copy, limited to a subtree' '
+ git diff-index -C --find-copies-harder $tree path1 >current &&
+ compare_diff_raw current expected
+'
+
+test_expect_success 'tweak work tree' '
+ rm -f path0/COPYING &&
+ git update-index --remove path0/COPYING
+'
+# In the tree, there is only path0/COPYING. In the cache, path0 does
+# not have COPYING anymore and path1 has COPYING which is a copy of
+# path0/COPYING. Showing the full tree with cache should tell us about
+# the rename.
+
+cat >expected <<EOF
+:100644 100644 $blob $blob R100 path0/COPYING path1/COPYING
+EOF
+
+test_expect_success 'rename detection' '
+ git diff-index -C --find-copies-harder $tree >current &&
+ compare_diff_raw current expected
+'
+
+# In the tree, there is only path0/COPYING. In the cache, path0 does
+# not have COPYING anymore and path1 has COPYING which is a copy of
+# path0/COPYING. When we say we care only about path1, we should just
+# see path1/COPYING appearing from nowhere.
+
+cat >expected <<EOF
+:000000 100644 $ZERO_OID $blob A path1/COPYING
+EOF
+
+test_expect_success 'rename, limited to a subtree' '
+ git diff-index -C --find-copies-harder $tree path1 >current &&
+ compare_diff_raw current expected
+'
+
+test_done
diff --git a/t/t4008-diff-break-rewrite.sh b/t/t4008-diff-break-rewrite.sh
new file mode 100755
index 0000000..562aaf3
--- /dev/null
+++ b/t/t4008-diff-break-rewrite.sh
@@ -0,0 +1,159 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+test_description='Break and then rename
+
+We have two very different files, file0 and file1, registered in a tree.
+
+We update file1 so drastically that it is more similar to file0, and
+then remove file0. With -B, changes to file1 should be broken into
+separate delete and create, resulting in removal of file0, removal of
+original file1 and creation of completely rewritten file1. The latter
+two are then merged back into a single "complete rewrite".
+
+Further, with -B and -M together, these three modifications should
+turn into rename-edit of file0 into file1.
+
+Starting from the same two files in the tree, we swap file0 and file1.
+With -B, this should be detected as two complete rewrites.
+
+Further, with -B and -M together, these should turn into two renames.
+'
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-diff.sh ;# test-lib chdir's into trash
+
+test_expect_success setup '
+ echo some dissimilar content >file0 &&
+ COPYING_test_data >file1 &&
+ blob0_id=$(git hash-object file0) &&
+ blob1_id=$(git hash-object file1) &&
+ git update-index --add file0 file1 &&
+ git tag reference $(git write-tree)
+'
+
+test_expect_success 'change file1 with copy-edit of file0 and remove file0' '
+ sed -e "s/git/GIT/" file0 >file1 &&
+ blob2_id=$(git hash-object file1) &&
+ rm -f file0 &&
+ git update-index --remove file0 file1
+'
+
+test_expect_success 'run diff with -B (#1)' '
+ git diff-index -B --cached reference >current &&
+ cat >expect <<-EOF &&
+ :100644 000000 $blob0_id $ZERO_OID D file0
+ :100644 100644 $blob1_id $blob2_id M100 file1
+ EOF
+ compare_diff_raw expect current
+'
+
+test_expect_success 'run diff with -B and -M (#2)' '
+ git diff-index -B -M reference >current &&
+ cat >expect <<-EOF &&
+ :100644 100644 $blob0_id $blob2_id R100 file0 file1
+ EOF
+ compare_diff_raw expect current
+'
+
+test_expect_success 'swap file0 and file1' '
+ rm -f file0 file1 &&
+ git read-tree -m reference &&
+ git checkout-index -f -u -a &&
+ mv file0 tmp &&
+ mv file1 file0 &&
+ mv tmp file1 &&
+ git update-index file0 file1
+'
+
+test_expect_success 'run diff with -B (#3)' '
+ git diff-index -B reference >current &&
+ cat >expect <<-EOF &&
+ :100644 100644 $blob0_id $blob1_id M100 file0
+ :100644 100644 $blob1_id $blob0_id M100 file1
+ EOF
+ compare_diff_raw expect current
+'
+
+test_expect_success 'run diff with -B and -M (#4)' '
+ git diff-index -B -M reference >current &&
+ cat >expect <<-EOF &&
+ :100644 100644 $blob1_id $blob1_id R100 file1 file0
+ :100644 100644 $blob0_id $blob0_id R100 file0 file1
+ EOF
+ compare_diff_raw expect current
+'
+
+test_expect_success 'make file0 into something completely different' '
+ rm -f file0 &&
+ test_ln_s_add frotz file0 &&
+ slink_id=$(printf frotz | git hash-object --stdin) &&
+ git update-index file1
+'
+
+test_expect_success 'run diff with -B (#5)' '
+ git diff-index -B reference >current &&
+ cat >expect <<-EOF &&
+ :100644 120000 $blob0_id $slink_id T file0
+ :100644 100644 $blob1_id $blob0_id M100 file1
+ EOF
+ compare_diff_raw expect current
+'
+
+test_expect_success 'run diff with -B -M (#6)' '
+ git diff-index -B -M reference >current &&
+
+ # file0 changed from regular to symlink. file1 is the same as the preimage
+ # of file0. Because the change does not make file0 disappear, file1 is
+ # denoted as a copy of file0
+ cat >expect <<-EOF &&
+ :100644 120000 $blob0_id $slink_id T file0
+ :100644 100644 $blob0_id $blob0_id C file0 file1
+ EOF
+ compare_diff_raw expect current
+'
+
+test_expect_success 'run diff with -M (#7)' '
+ git diff-index -M reference >current &&
+
+ # This should not mistake file0 as the copy source of new file1
+ # due to type differences.
+ cat >expect <<-EOF &&
+ :100644 120000 $blob0_id $slink_id T file0
+ :100644 100644 $blob1_id $blob0_id M file1
+ EOF
+ compare_diff_raw expect current
+'
+
+test_expect_success 'file1 edited to look like file0 and file0 rename-edited to file2' '
+ rm -f file0 file1 &&
+ git read-tree -m reference &&
+ git checkout-index -f -u -a &&
+ sed -e "s/git/GIT/" file0 >file1 &&
+ sed -e "s/git/GET/" file0 >file2 &&
+ blob3_id=$(git hash-object file2) &&
+ rm -f file0 &&
+ git update-index --add --remove file0 file1 file2
+'
+
+test_expect_success 'run diff with -B (#8)' '
+ git diff-index -B reference >current &&
+ cat >expect <<-EOF &&
+ :100644 000000 $blob0_id $ZERO_OID D file0
+ :100644 100644 $blob1_id $blob2_id M100 file1
+ :000000 100644 $ZERO_OID $blob3_id A file2
+ EOF
+ compare_diff_raw expect current
+'
+
+test_expect_success 'run diff with -B -C (#9)' '
+ git diff-index -B -C reference >current &&
+ cat >expect <<-EOF &&
+ :100644 100644 $blob0_id $blob2_id C095 file0 file1
+ :100644 100644 $blob0_id $blob3_id R095 file0 file2
+ EOF
+ compare_diff_raw expect current
+'
+
+test_done
diff --git a/t/t4009-diff-rename-4.sh b/t/t4009-diff-rename-4.sh
new file mode 100755
index 0000000..3480781
--- /dev/null
+++ b/t/t4009-diff-rename-4.sh
@@ -0,0 +1,100 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+test_description='Same rename detection as t4003 but testing diff-raw -z.
+
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-diff.sh ;# test-lib chdir's into trash
+
+test_expect_success \
+ 'prepare reference tree' \
+ 'COPYING_test_data >COPYING &&
+ echo frotz >rezrov &&
+ git update-index --add COPYING rezrov &&
+ orig=$(git hash-object COPYING) &&
+ tree=$(git write-tree) &&
+ echo $tree'
+
+test_expect_success \
+ 'prepare work tree' \
+ 'sed -e 's/HOWEVER/However/' <COPYING >COPYING.1 &&
+ sed -e 's/GPL/G.P.L/g' <COPYING >COPYING.2 &&
+ rm -f COPYING &&
+ c1=$(git hash-object COPYING.1) &&
+ c2=$(git hash-object COPYING.2) &&
+ git update-index --add --remove COPYING COPYING.?'
+
+# tree has COPYING and rezrov. work tree has COPYING.1 and COPYING.2,
+# both are slightly edited, and unchanged rezrov. We say COPYING.1
+# and COPYING.2 are based on COPYING, and do not say anything about
+# rezrov.
+
+git diff-index -z -C $tree >current
+
+cat >expected <<EOF
+:100644 100644 $orig $c1 C1234
+COPYING
+COPYING.1
+:100644 100644 $orig $c2 R1234
+COPYING
+COPYING.2
+EOF
+
+test_expect_success \
+ 'validate output from rename/copy detection (#1)' \
+ 'compare_diff_raw_z current expected'
+
+################################################################
+
+test_expect_success \
+ 'prepare work tree again' \
+ 'mv COPYING.2 COPYING &&
+ git update-index --add --remove COPYING COPYING.1 COPYING.2'
+
+# tree has COPYING and rezrov. work tree has COPYING and COPYING.1,
+# both are slightly edited, and unchanged rezrov. We say COPYING.1
+# is based on COPYING and COPYING is still there, and do not say anything
+# about rezrov.
+
+git diff-index -z -C $tree >current
+cat >expected <<EOF
+:100644 100644 $orig $c2 M
+COPYING
+:100644 100644 $orig $c1 C1234
+COPYING
+COPYING.1
+EOF
+
+test_expect_success \
+ 'validate output from rename/copy detection (#2)' \
+ 'compare_diff_raw_z current expected'
+
+################################################################
+
+# tree has COPYING and rezrov. work tree has the same COPYING and
+# copy-edited COPYING.1, and unchanged rezrov. We should not say
+# anything about rezrov or COPYING, since the revised again diff-raw
+# nows how to say Copy.
+
+test_expect_success \
+ 'prepare work tree once again' \
+ 'COPYING_test_data >COPYING &&
+ git update-index --add --remove COPYING COPYING.1'
+
+git diff-index -z -C --find-copies-harder $tree >current
+cat >expected <<EOF
+:100644 100644 $orig $c1 C1234
+COPYING
+COPYING.1
+EOF
+
+test_expect_success \
+ 'validate output from rename/copy detection (#3)' \
+ 'compare_diff_raw_z current expected'
+
+test_done
diff --git a/t/t4010-diff-pathspec.sh b/t/t4010-diff-pathspec.sh
new file mode 100755
index 0000000..9d9650e
--- /dev/null
+++ b/t/t4010-diff-pathspec.sh
@@ -0,0 +1,154 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+test_description='Pathspec restrictions
+
+Prepare:
+ file0
+ path1/file1
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-diff.sh ;# test-lib chdir's into trash
+
+test_expect_success \
+ setup \
+ 'echo frotz >file0 &&
+ mkdir path1 &&
+ echo rezrov >path1/file1 &&
+ before0=$(git hash-object file0) &&
+ before1=$(git hash-object path1/file1) &&
+ git update-index --add file0 path1/file1 &&
+ tree=$(git write-tree) &&
+ echo "$tree" &&
+ echo nitfol >file0 &&
+ echo yomin >path1/file1 &&
+ after0=$(git hash-object file0) &&
+ after1=$(git hash-object path1/file1) &&
+ git update-index file0 path1/file1'
+
+cat >expected <<\EOF
+EOF
+test_expect_success \
+ 'limit to path should show nothing' \
+ 'git diff-index --cached $tree -- path >current &&
+ compare_diff_raw current expected'
+
+cat >expected <<EOF
+:100644 100644 $before1 $after1 M path1/file1
+EOF
+test_expect_success \
+ 'limit to path1 should show path1/file1' \
+ 'git diff-index --cached $tree -- path1 >current &&
+ compare_diff_raw current expected'
+
+cat >expected <<EOF
+:100644 100644 $before1 $after1 M path1/file1
+EOF
+test_expect_success \
+ 'limit to path1/ should show path1/file1' \
+ 'git diff-index --cached $tree -- path1/ >current &&
+ compare_diff_raw current expected'
+
+cat >expected <<EOF
+:100644 100644 $before1 $after1 M path1/file1
+EOF
+test_expect_success \
+ '"*file1" should show path1/file1' \
+ 'git diff-index --cached $tree -- "*file1" >current &&
+ compare_diff_raw current expected'
+
+cat >expected <<EOF
+:100644 100644 $before0 $after0 M file0
+EOF
+test_expect_success \
+ 'limit to file0 should show file0' \
+ 'git diff-index --cached $tree -- file0 >current &&
+ compare_diff_raw current expected'
+
+cat >expected <<\EOF
+EOF
+test_expect_success \
+ 'limit to file0/ should emit nothing.' \
+ 'git diff-index --cached $tree -- file0/ >current &&
+ compare_diff_raw current expected'
+
+test_expect_success 'diff-tree pathspec' '
+ tree2=$(git write-tree) &&
+ echo "$tree2" &&
+ git diff-tree -r --name-only $tree $tree2 -- pa path1/a >current &&
+ test_must_be_empty current
+'
+
+test_expect_success 'diff-tree with wildcard shows dir also matches' '
+ git diff-tree --name-only $EMPTY_TREE $tree -- "f*" >result &&
+ echo file0 >expected &&
+ test_cmp expected result
+'
+
+test_expect_success 'diff-tree -r with wildcard' '
+ git diff-tree -r --name-only $EMPTY_TREE $tree -- "*file1" >result &&
+ echo path1/file1 >expected &&
+ test_cmp expected result
+'
+
+test_expect_success 'diff-tree with wildcard shows dir also matches' '
+ git diff-tree --name-only $tree $tree2 -- "path1/f*" >result &&
+ echo path1 >expected &&
+ test_cmp expected result
+'
+
+test_expect_success 'diff-tree -r with wildcard from beginning' '
+ git diff-tree -r --name-only $tree $tree2 -- "path1/*file1" >result &&
+ echo path1/file1 >expected &&
+ test_cmp expected result
+'
+
+test_expect_success 'diff-tree -r with wildcard' '
+ git diff-tree -r --name-only $tree $tree2 -- "path1/f*" >result &&
+ echo path1/file1 >expected &&
+ test_cmp expected result
+'
+
+test_expect_success 'setup submodules' '
+ test_tick &&
+ git init submod &&
+ ( cd submod && test_commit first ) &&
+ git add submod &&
+ git commit -m first &&
+ ( cd submod && test_commit second ) &&
+ git add submod &&
+ git commit -m second
+'
+
+test_expect_success 'diff-tree ignores trailing slash on submodule path' '
+ git diff --name-only HEAD^ HEAD submod >expect &&
+ git diff --name-only HEAD^ HEAD submod/ >actual &&
+ test_cmp expect actual &&
+ git diff --name-only HEAD^ HEAD -- submod/whatever >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'diff multiple wildcard pathspecs' '
+ mkdir path2 &&
+ echo rezrov >path2/file1 &&
+ git update-index --add path2/file1 &&
+ tree3=$(git write-tree) &&
+ git diff --name-only $tree $tree3 -- "path2*1" "path1*1" >actual &&
+ cat <<-\EOF >expect &&
+ path1/file1
+ path2/file1
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'diff-cache ignores trailing slash on submodule path' '
+ git diff --name-only HEAD^ submod >expect &&
+ git diff --name-only HEAD^ submod/ >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t4011-diff-symlink.sh b/t/t4011-diff-symlink.sh
new file mode 100755
index 0000000..d7a5f7a
--- /dev/null
+++ b/t/t4011-diff-symlink.sh
@@ -0,0 +1,177 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Johannes Schindelin
+#
+
+test_description='Test diff of symlinks.
+
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-diff.sh
+
+# Print the short OID of a symlink with the given name.
+symlink_oid () {
+ local oid=$(printf "%s" "$1" | git hash-object --stdin) &&
+ git rev-parse --short "$oid"
+}
+
+# Print the short OID of the given file.
+short_oid () {
+ local oid=$(git hash-object "$1") &&
+ git rev-parse --short "$oid"
+}
+
+test_expect_success 'diff new symlink and file' '
+ symlink=$(symlink_oid xyzzy) &&
+ cat >expected <<-EOF &&
+ diff --git a/frotz b/frotz
+ new file mode 120000
+ index 0000000..$symlink
+ --- /dev/null
+ +++ b/frotz
+ @@ -0,0 +1 @@
+ +xyzzy
+ \ No newline at end of file
+ diff --git a/nitfol b/nitfol
+ new file mode 100644
+ index 0000000..$symlink
+ --- /dev/null
+ +++ b/nitfol
+ @@ -0,0 +1 @@
+ +xyzzy
+ EOF
+
+ # the empty tree
+ git update-index &&
+ tree=$(git write-tree) &&
+
+ test_ln_s_add xyzzy frotz &&
+ echo xyzzy >nitfol &&
+ git update-index --add nitfol &&
+ GIT_DIFF_OPTS=--unified=0 git diff-index -M -p $tree >current &&
+ compare_diff_patch expected current
+'
+
+test_expect_success 'diff unchanged symlink and file' '
+ tree=$(git write-tree) &&
+ git update-index frotz nitfol &&
+ test -z "$(git diff-index --name-only $tree)"
+'
+
+test_expect_success 'diff removed symlink and file' '
+ cat >expected <<-EOF &&
+ diff --git a/frotz b/frotz
+ deleted file mode 120000
+ index $symlink..0000000
+ --- a/frotz
+ +++ /dev/null
+ @@ -1 +0,0 @@
+ -xyzzy
+ \ No newline at end of file
+ diff --git a/nitfol b/nitfol
+ deleted file mode 100644
+ index $symlink..0000000
+ --- a/nitfol
+ +++ /dev/null
+ @@ -1 +0,0 @@
+ -xyzzy
+ EOF
+ mv frotz frotz2 &&
+ mv nitfol nitfol2 &&
+ git diff-index -M -p $tree >current &&
+ compare_diff_patch expected current
+'
+
+test_expect_success 'diff identical, but newly created symlink and file' '
+ >expected &&
+ rm -f frotz nitfol &&
+ echo xyzzy >nitfol &&
+ test-tool chmtime +10 nitfol &&
+ if test_have_prereq SYMLINKS
+ then
+ ln -s xyzzy frotz
+ else
+ printf xyzzy >frotz
+ # the symlink property propagates from the index
+ fi &&
+ git diff-index -M -p $tree >current &&
+ compare_diff_patch expected current &&
+
+ >expected &&
+ git diff-index -M -p -w $tree >current &&
+ compare_diff_patch expected current
+'
+
+test_expect_success 'diff different symlink and file' '
+ new=$(symlink_oid yxyyz) &&
+ cat >expected <<-EOF &&
+ diff --git a/frotz b/frotz
+ index $symlink..$new 120000
+ --- a/frotz
+ +++ b/frotz
+ @@ -1 +1 @@
+ -xyzzy
+ \ No newline at end of file
+ +yxyyz
+ \ No newline at end of file
+ diff --git a/nitfol b/nitfol
+ index $symlink..$new 100644
+ --- a/nitfol
+ +++ b/nitfol
+ @@ -1 +1 @@
+ -xyzzy
+ +yxyyz
+ EOF
+ rm -f frotz &&
+ if test_have_prereq SYMLINKS
+ then
+ ln -s yxyyz frotz
+ else
+ printf yxyyz >frotz
+ # the symlink property propagates from the index
+ fi &&
+ echo yxyyz >nitfol &&
+ git diff-index -M -p $tree >current &&
+ compare_diff_patch expected current
+'
+
+test_expect_success SYMLINKS 'diff symlinks with non-existing targets' '
+ ln -s narf pinky &&
+ ln -s take\ over brain &&
+ test_must_fail git diff --no-index pinky brain >output 2>output.err &&
+ grep narf output &&
+ test_must_be_empty output.err
+'
+
+test_expect_success SYMLINKS 'setup symlinks with attributes' '
+ echo "*.bin diff=bin" >>.gitattributes &&
+ echo content >file.bin &&
+ ln -s file.bin link.bin &&
+ git add -N file.bin link.bin
+'
+
+test_expect_success SYMLINKS 'symlinks do not respect userdiff config by path' '
+ file=$(short_oid file.bin) &&
+ link=$(symlink_oid file.bin) &&
+ cat >expect <<-EOF &&
+ diff --git a/file.bin b/file.bin
+ new file mode 100644
+ index 0000000..$file
+ Binary files /dev/null and b/file.bin differ
+ diff --git a/link.bin b/link.bin
+ new file mode 120000
+ index 0000000..$link
+ --- /dev/null
+ +++ b/link.bin
+ @@ -0,0 +1 @@
+ +file.bin
+ \ No newline at end of file
+ EOF
+ git config diff.bin.binary true &&
+ git diff file.bin link.bin >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t4012-diff-binary.sh b/t/t4012-diff-binary.sh
new file mode 100755
index 0000000..c64d9d2
--- /dev/null
+++ b/t/t4012-diff-binary.sh
@@ -0,0 +1,134 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Junio C Hamano
+#
+
+test_description='Binary diff and apply
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+cat >expect.binary-numstat <<\EOF
+1 1 a
+- - b
+1 1 c
+- - d
+EOF
+
+test_expect_success 'prepare repository' '
+ echo AIT >a && echo BIT >b && echo CIT >c && echo DIT >d &&
+ git update-index --add a b c d &&
+ echo git >a &&
+ cat "$TEST_DIRECTORY"/test-binary-1.png >b &&
+ echo git >c &&
+ cat b b >d
+'
+
+cat > expected <<\EOF
+ a | 2 +-
+ b | Bin
+ c | 2 +-
+ d | Bin
+ 4 files changed, 2 insertions(+), 2 deletions(-)
+EOF
+test_expect_success 'apply --stat output for binary file change' '
+ git diff >diff &&
+ git apply --stat --summary <diff >current &&
+ test_cmp expected current
+'
+
+test_expect_success 'diff --shortstat output for binary file change' '
+ tail -n 1 expected >expect &&
+ git diff --shortstat >current &&
+ test_cmp expect current
+'
+
+test_expect_success 'diff --shortstat output for binary file change only' '
+ echo " 1 file changed, 0 insertions(+), 0 deletions(-)" >expected &&
+ git diff --shortstat -- b >current &&
+ test_cmp expected current
+'
+
+test_expect_success 'apply --numstat notices binary file change' '
+ git diff >diff &&
+ git apply --numstat <diff >current &&
+ test_cmp expect.binary-numstat current
+'
+
+test_expect_success 'apply --numstat understands diff --binary format' '
+ git diff --binary >diff &&
+ git apply --numstat <diff >current &&
+ test_cmp expect.binary-numstat current
+'
+
+# apply needs to be able to skip the binary material correctly
+# in order to report the line number of a corrupt patch.
+test_expect_success 'apply detecting corrupt patch correctly' '
+ git diff >output &&
+ sed -e "s/-CIT/xCIT/" <output >broken &&
+ test_must_fail git apply --stat --summary broken 2>detected &&
+ detected=$(cat detected) &&
+ detected=$(expr "$detected" : "error.*at line \\([0-9]*\\)\$") &&
+ detected=$(sed -ne "${detected}p" broken) &&
+ test "$detected" = xCIT
+'
+
+test_expect_success 'apply detecting corrupt patch correctly' '
+ git diff --binary | sed -e "s/-CIT/xCIT/" >broken &&
+ test_must_fail git apply --stat --summary broken 2>detected &&
+ detected=$(cat detected) &&
+ detected=$(expr "$detected" : "error.*at line \\([0-9]*\\)\$") &&
+ detected=$(sed -ne "${detected}p" broken) &&
+ test "$detected" = xCIT
+'
+
+test_expect_success 'initial commit' 'git commit -a -m initial'
+
+# Try removal (b), modification (d), and creation (e).
+test_expect_success 'diff-index with --binary' '
+ echo AIT >a && mv b e && echo CIT >c && cat e >d &&
+ git update-index --add --remove a b c d e &&
+ tree0=$(git write-tree) &&
+ git diff --cached --binary >current &&
+ git apply --stat --summary current
+'
+
+test_expect_success 'apply binary patch' '
+ git reset --hard &&
+ git apply --binary --index <current &&
+ tree1=$(git write-tree) &&
+ test "$tree1" = "$tree0"
+'
+
+test_expect_success 'diff --no-index with binary creation' '
+ echo Q | q_to_nul >binary &&
+ # hide error code from diff, which just indicates differences
+ test_might_fail git diff --binary --no-index /dev/null binary >current &&
+ rm binary &&
+ git apply --binary <current &&
+ echo Q >expected &&
+ nul_to_q <binary >actual &&
+ test_cmp expected actual
+'
+
+cat >expect <<EOF
+ binfilë | Bin 0 -> 1026 bytes
+ tëxtfilë | 10000 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+EOF
+
+test_expect_success 'diff --stat with binary files and big change count' '
+ printf "\01\00%1024d" 1 >binfilë &&
+ git add binfilë &&
+ i=0 &&
+ while test $i -lt 10000; do
+ echo $i &&
+ i=$(($i + 1)) || return 1
+ done >tëxtfilë &&
+ git add tëxtfilë &&
+ git -c core.quotepath=false diff --cached --stat binfilë tëxtfilë >output &&
+ grep " | " output >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t4013-diff-various.sh b/t/t4013-diff-various.sh
new file mode 100755
index 0000000..5cc17c2
--- /dev/null
+++ b/t/t4013-diff-various.sh
@@ -0,0 +1,669 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Junio C Hamano
+#
+
+test_description='Various diff formatting options'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=master
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-diff.sh
+
+test_expect_success setup '
+
+ GIT_AUTHOR_DATE="2006-06-26 00:00:00 +0000" &&
+ GIT_COMMITTER_DATE="2006-06-26 00:00:00 +0000" &&
+ export GIT_AUTHOR_DATE GIT_COMMITTER_DATE &&
+
+ mkdir dir &&
+ mkdir dir2 &&
+ test_write_lines 1 2 3 >file0 &&
+ test_write_lines A B >dir/sub &&
+ cat file0 >file2 &&
+ git add file0 file2 dir/sub &&
+ git commit -m Initial &&
+
+ git branch initial &&
+ git branch side &&
+
+ GIT_AUTHOR_DATE="2006-06-26 00:01:00 +0000" &&
+ GIT_COMMITTER_DATE="2006-06-26 00:01:00 +0000" &&
+ export GIT_AUTHOR_DATE GIT_COMMITTER_DATE &&
+
+ test_write_lines 4 5 6 >>file0 &&
+ test_write_lines C D >>dir/sub &&
+ rm -f file2 &&
+ git update-index --remove file0 file2 dir/sub &&
+ git commit -m "Second${LF}${LF}This is the second commit." &&
+
+ GIT_AUTHOR_DATE="2006-06-26 00:02:00 +0000" &&
+ GIT_COMMITTER_DATE="2006-06-26 00:02:00 +0000" &&
+ export GIT_AUTHOR_DATE GIT_COMMITTER_DATE &&
+
+ test_write_lines A B C >file1 &&
+ git add file1 &&
+ test_write_lines E F >>dir/sub &&
+ git update-index dir/sub &&
+ git commit -m Third &&
+
+ GIT_AUTHOR_DATE="2006-06-26 00:03:00 +0000" &&
+ GIT_COMMITTER_DATE="2006-06-26 00:03:00 +0000" &&
+ export GIT_AUTHOR_DATE GIT_COMMITTER_DATE &&
+
+ git checkout side &&
+ test_write_lines A B C >>file0 &&
+ test_write_lines 1 2 >>dir/sub &&
+ cat dir/sub >file3 &&
+ git add file3 &&
+ git update-index file0 dir/sub &&
+ git commit -m Side &&
+
+ GIT_AUTHOR_DATE="2006-06-26 00:04:00 +0000" &&
+ GIT_COMMITTER_DATE="2006-06-26 00:04:00 +0000" &&
+ export GIT_AUTHOR_DATE GIT_COMMITTER_DATE &&
+
+ git checkout master &&
+ git pull -s ours --no-rebase . side &&
+
+ GIT_AUTHOR_DATE="2006-06-26 00:05:00 +0000" &&
+ GIT_COMMITTER_DATE="2006-06-26 00:05:00 +0000" &&
+ export GIT_AUTHOR_DATE GIT_COMMITTER_DATE &&
+
+ test_write_lines A B C >>file0 &&
+ test_write_lines 1 2 >>dir/sub &&
+ git update-index file0 dir/sub &&
+
+ mkdir dir3 &&
+ cp dir/sub dir3/sub &&
+ test-tool chmtime +1 dir3/sub &&
+
+ git config log.showroot false &&
+ git commit --amend &&
+
+ GIT_AUTHOR_DATE="2006-06-26 00:06:00 +0000" &&
+ GIT_COMMITTER_DATE="2006-06-26 00:06:00 +0000" &&
+ export GIT_AUTHOR_DATE GIT_COMMITTER_DATE &&
+ git checkout -b rearrange initial &&
+ test_write_lines B A >dir/sub &&
+ git add dir/sub &&
+ git commit -m "Rearranged lines in dir/sub" &&
+ git checkout master &&
+
+ GIT_AUTHOR_DATE="2006-06-26 00:06:00 +0000" &&
+ GIT_COMMITTER_DATE="2006-06-26 00:06:00 +0000" &&
+ export GIT_AUTHOR_DATE GIT_COMMITTER_DATE &&
+ git checkout -b mode initial &&
+ git update-index --chmod=+x file0 &&
+ git commit -m "update mode" &&
+ git checkout -f master &&
+
+ GIT_AUTHOR_DATE="2006-06-26 00:06:00 +0000" &&
+ GIT_COMMITTER_DATE="2006-06-26 00:06:00 +0000" &&
+ export GIT_AUTHOR_DATE GIT_COMMITTER_DATE &&
+ git checkout -b note initial &&
+ git update-index --chmod=+x file2 &&
+ git commit -m "update mode (file2)" &&
+ git notes add -m "note" &&
+ git checkout -f master &&
+
+ # Same merge as master, but with parents reversed. Hide it in a
+ # pseudo-ref to avoid impacting tests with --all.
+ commit=$(echo reverse |
+ git commit-tree -p master^2 -p master^1 master^{tree}) &&
+ git update-ref REVERSE $commit &&
+
+ git config diff.renames false &&
+
+ git show-branch
+'
+
+: <<\EOF
+! [initial] Initial
+ * [master] Merge branch 'side'
+ ! [rearrange] Rearranged lines in dir/sub
+ ! [side] Side
+----
+ + [rearrange] Rearranged lines in dir/sub
+ - [master] Merge branch 'side'
+ * + [side] Side
+ * [master^] Third
+ * [master~2] Second
++*++ [initial] Initial
+EOF
+
+process_diffs () {
+ perl -e '
+ my $oid_length = length($ARGV[0]);
+ my $x40 = "[0-9a-f]{40}";
+ my $xab = "[0-9a-f]{4,16}";
+ my $orx = "[0-9a-f]" x $oid_length;
+
+ sub munge_oid {
+ my ($oid) = @_;
+ my $x;
+
+ return "" unless length $oid;
+
+ if ($oid =~ /^(100644|100755|120000)$/) {
+ return $oid;
+ }
+
+ if ($oid =~ /^0*$/) {
+ $x = "0";
+ } else {
+ $x = "f";
+ }
+
+ if (length($oid) == 40) {
+ return $x x $oid_length;
+ } else {
+ return $x x length($oid);
+ }
+ }
+
+ while (<STDIN>) {
+ s/($orx)/munge_oid($1)/ge;
+ s/From ($x40)( |\))/"From " . munge_oid($1) . $2/ge;
+ s/commit ($x40)($| \(from )($x40?)/"commit " . munge_oid($1) . $2 . munge_oid($3)/ge;
+ s/\b($x40)( |\.\.|$)/munge_oid($1) . $2/ge;
+ s/^($x40)($| )/munge_oid($1) . $2/e;
+ s/($xab)(\.\.|,| |\.\.\.|$)/munge_oid($1) . $2/ge;
+ print;
+ }
+ ' "$ZERO_OID" <"$1"
+}
+
+V=$(git version | sed -e 's/^git version //' -e 's/\./\\./g')
+while read magic cmd
+do
+ status=success
+ case "$magic" in
+ '' | '#'*)
+ continue ;;
+ :*)
+ magic=${magic#:}
+ label="$magic-$cmd"
+ case "$magic" in
+ noellipses) ;;
+ failure)
+ status=failure
+ magic=
+ label="$cmd" ;;
+ *)
+ BUG "unknown magic $magic" ;;
+ esac ;;
+ *)
+ cmd="$magic $cmd" magic=
+ label="$cmd" ;;
+ esac
+ test=$(echo "$label" | sed -e 's|[/ ][/ ]*|_|g')
+ pfx=$(printf "%04d" $test_count)
+ expect="$TEST_DIRECTORY/t4013/diff.$test"
+ actual="$pfx-diff.$test"
+
+ test_expect_$status "git $cmd # magic is ${magic:-(not used)}" '
+ {
+ echo "$ git $cmd"
+ case "$magic" in
+ "")
+ GIT_PRINT_SHA1_ELLIPSIS=yes git $cmd ;;
+ noellipses)
+ git $cmd ;;
+ esac |
+ sed -e "s/^\\(-*\\)$V\\(-*\\)\$/\\1g-i-t--v-e-r-s-i-o-n\2/" \
+ -e "s/^\\(.*mixed; boundary=\"-*\\)$V\\(-*\\)\"\$/\\1g-i-t--v-e-r-s-i-o-n\2\"/"
+ echo "\$"
+ } >"$actual" &&
+ if test -f "$expect"
+ then
+ process_diffs "$actual" >actual &&
+ process_diffs "$expect" >expect &&
+ case $cmd in
+ *format-patch* | *-stat*)
+ test_cmp expect actual;;
+ *)
+ test_cmp expect actual;;
+ esac &&
+ rm -f "$actual" actual expect
+ else
+ # this is to help developing new tests.
+ cp "$actual" "$expect"
+ false
+ fi
+ '
+done <<\EOF
+diff-tree initial
+diff-tree -r initial
+diff-tree -r --abbrev initial
+diff-tree -r --abbrev=4 initial
+diff-tree --root initial
+diff-tree --root --abbrev initial
+:noellipses diff-tree --root --abbrev initial
+diff-tree --root -r initial
+diff-tree --root -r --abbrev initial
+:noellipses diff-tree --root -r --abbrev initial
+diff-tree --root -r --abbrev=4 initial
+:noellipses diff-tree --root -r --abbrev=4 initial
+diff-tree -p initial
+diff-tree --root -p initial
+diff-tree --root -p --abbrev=10 initial
+diff-tree --root -p --full-index initial
+diff-tree --root -p --full-index --abbrev=10 initial
+diff-tree --patch-with-stat initial
+diff-tree --root --patch-with-stat initial
+diff-tree --patch-with-raw initial
+diff-tree --root --patch-with-raw initial
+
+diff-tree --pretty initial
+diff-tree --pretty --root initial
+diff-tree --pretty -p initial
+diff-tree --pretty --stat initial
+diff-tree --pretty --summary initial
+diff-tree --pretty --stat --summary initial
+diff-tree --pretty --root -p initial
+diff-tree --pretty --root --stat initial
+# improved by Timo's patch
+diff-tree --pretty --root --summary initial
+# improved by Timo's patch
+diff-tree --pretty --root --summary -r initial
+diff-tree --pretty --root --stat --summary initial
+diff-tree --pretty --patch-with-stat initial
+diff-tree --pretty --root --patch-with-stat initial
+diff-tree --pretty --patch-with-raw initial
+diff-tree --pretty --root --patch-with-raw initial
+
+diff-tree --pretty=oneline initial
+diff-tree --pretty=oneline --root initial
+diff-tree --pretty=oneline -p initial
+diff-tree --pretty=oneline --root -p initial
+diff-tree --pretty=oneline --patch-with-stat initial
+# improved by Timo's patch
+diff-tree --pretty=oneline --root --patch-with-stat initial
+diff-tree --pretty=oneline --patch-with-raw initial
+diff-tree --pretty=oneline --root --patch-with-raw initial
+
+diff-tree --pretty side
+diff-tree --pretty -p side
+diff-tree --pretty --patch-with-stat side
+
+diff-tree initial mode
+diff-tree --stat initial mode
+diff-tree --summary initial mode
+
+diff-tree master
+diff-tree -m master
+diff-tree -p master
+diff-tree -p -m master
+diff-tree -c master
+diff-tree -c --abbrev master
+:noellipses diff-tree -c --abbrev master
+diff-tree --cc master
+# stat only should show the diffstat with the first parent
+diff-tree -c --stat master
+diff-tree --cc --stat master
+diff-tree -c --stat --summary master
+diff-tree --cc --stat --summary master
+# stat summary should show the diffstat and summary with the first parent
+diff-tree -c --stat --summary side
+diff-tree --cc --stat --summary side
+diff-tree --cc --shortstat master
+diff-tree --cc --summary REVERSE
+# improved by Timo's patch
+diff-tree --cc --patch-with-stat master
+# improved by Timo's patch
+diff-tree --cc --patch-with-stat --summary master
+# this is correct
+diff-tree --cc --patch-with-stat --summary side
+
+log master
+log -p master
+log --root master
+log --root -p master
+log --patch-with-stat master
+log --root --patch-with-stat master
+log --root --patch-with-stat --summary master
+# improved by Timo's patch
+log --root -c --patch-with-stat --summary master
+# improved by Timo's patch
+log --root --cc --patch-with-stat --summary master
+log --no-diff-merges -p --first-parent master
+log --diff-merges=off -p --first-parent master
+log --first-parent --diff-merges=off -p master
+log -p --first-parent master
+log -p --diff-merges=first-parent master
+log --diff-merges=first-parent master
+log -m -p --first-parent master
+log -m -p master
+log --cc -m -p master
+log -c -m -p master
+log -m --raw master
+log -m --stat master
+log -SF master
+log -S F master
+log -SF -p master
+log -SF master --max-count=0
+log -SF master --max-count=1
+log -SF master --max-count=2
+log -GF master
+log -GF -p master
+log -GF -p --pickaxe-all master
+log -IA -IB -I1 -I2 -p master
+log --decorate --all
+log --decorate=full --all
+log --decorate --clear-decorations --all
+log --decorate=full --clear-decorations --all
+
+rev-list --parents HEAD
+rev-list --children HEAD
+
+whatchanged master
+:noellipses whatchanged master
+whatchanged -p master
+whatchanged --root master
+:noellipses whatchanged --root master
+whatchanged --root -p master
+whatchanged --patch-with-stat master
+whatchanged --root --patch-with-stat master
+whatchanged --root --patch-with-stat --summary master
+# improved by Timo's patch
+whatchanged --root -c --patch-with-stat --summary master
+# improved by Timo's patch
+whatchanged --root --cc --patch-with-stat --summary master
+whatchanged -SF master
+:noellipses whatchanged -SF master
+whatchanged -SF -p master
+
+log --patch-with-stat master -- dir/
+whatchanged --patch-with-stat master -- dir/
+log --patch-with-stat --summary master -- dir/
+whatchanged --patch-with-stat --summary master -- dir/
+
+show initial
+show --root initial
+show side
+show master
+show -c master
+show -m master
+show --first-parent master
+show --stat side
+show --stat --summary side
+show --patch-with-stat side
+show --patch-with-raw side
+:noellipses show --patch-with-raw side
+show --patch-with-stat --summary side
+
+format-patch --stdout initial..side
+format-patch --stdout initial..master^
+format-patch --stdout initial..master
+format-patch --stdout --no-numbered initial..master
+format-patch --stdout --numbered initial..master
+format-patch --attach --stdout initial..side
+format-patch --attach --stdout --suffix=.diff initial..side
+format-patch --attach --stdout initial..master^
+format-patch --attach --stdout initial..master
+format-patch --inline --stdout initial..side
+format-patch --inline --stdout initial..master^
+format-patch --inline --stdout --numbered-files initial..master
+format-patch --inline --stdout initial..master
+format-patch --inline --stdout --subject-prefix=TESTCASE initial..master
+config format.subjectprefix DIFFERENT_PREFIX
+format-patch --inline --stdout initial..master^^
+format-patch --stdout --cover-letter -n initial..master^
+
+diff --abbrev initial..side
+diff -U initial..side
+diff -U1 initial..side
+diff -r initial..side
+diff --stat initial..side
+diff -r --stat initial..side
+diff initial..side
+diff --patch-with-stat initial..side
+diff --patch-with-raw initial..side
+:noellipses diff --patch-with-raw initial..side
+diff --patch-with-stat -r initial..side
+diff --patch-with-raw -r initial..side
+:noellipses diff --patch-with-raw -r initial..side
+diff --name-status dir2 dir
+diff --no-index --name-status dir2 dir
+diff --no-index --name-status -- dir2 dir
+diff --no-index dir dir3
+diff master master^ side
+# Can't use spaces...
+diff --line-prefix=abc master master^ side
+diff --dirstat master~1 master~2
+diff --dirstat initial rearrange
+diff --dirstat-by-file initial rearrange
+diff --dirstat --cc master~1 master
+# No-index --abbrev and --no-abbrev
+diff --raw initial
+:noellipses diff --raw initial
+diff --raw --abbrev=4 initial
+:noellipses diff --raw --abbrev=4 initial
+diff --raw --no-abbrev initial
+diff --no-index --raw dir2 dir
+:noellipses diff --no-index --raw dir2 dir
+diff --no-index --raw --abbrev=4 dir2 dir
+:noellipses diff --no-index --raw --abbrev=4 dir2 dir
+diff --no-index --raw --no-abbrev dir2 dir
+
+diff-tree --pretty --root --stat --compact-summary initial
+diff-tree --pretty -R --root --stat --compact-summary initial
+diff-tree --pretty note
+diff-tree --pretty --notes note
+diff-tree --format=%N note
+diff-tree --stat --compact-summary initial mode
+diff-tree -R --stat --compact-summary initial mode
+EOF
+
+test_expect_success 'log -m matches pure log' '
+ git log master >result &&
+ process_diffs result >expected &&
+ git log -m >result &&
+ process_diffs result >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'log --diff-merges=on matches --diff-merges=separate' '
+ git log -p --diff-merges=separate master >result &&
+ process_diffs result >expected &&
+ git log -p --diff-merges=on master >result &&
+ process_diffs result >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'log --dd matches --diff-merges=1 -p' '
+ git log --diff-merges=1 -p master >result &&
+ process_diffs result >expected &&
+ git log --dd master >result &&
+ process_diffs result >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'deny wrong log.diffMerges config' '
+ test_config log.diffMerges wrong-value &&
+ test_expect_code 128 git log
+'
+
+test_expect_success 'git config log.diffMerges first-parent' '
+ git log -p --diff-merges=first-parent master >result &&
+ process_diffs result >expected &&
+ test_config log.diffMerges first-parent &&
+ git log -p --diff-merges=on master >result &&
+ process_diffs result >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'git config log.diffMerges first-parent vs -m' '
+ git log -p --diff-merges=first-parent master >result &&
+ process_diffs result >expected &&
+ test_config log.diffMerges first-parent &&
+ git log -p -m master >result &&
+ process_diffs result >actual &&
+ test_cmp expected actual
+'
+
+# -m in "git diff-index" means "match missing", that differs
+# from its meaning in "git diff". Let's check it in diff-index.
+# The line in the output for removed file should disappear when
+# we provide -m in diff-index.
+test_expect_success 'git diff-index -m' '
+ rm -f file1 &&
+ git diff-index HEAD >without-m &&
+ lines_count=$(wc -l <without-m) &&
+ git diff-index -m HEAD >with-m &&
+ git restore file1 &&
+ test_line_count = $((lines_count - 1)) with-m
+'
+
+test_expect_success 'log -S requires an argument' '
+ test_must_fail git log -S
+'
+
+test_expect_success 'diff --cached on unborn branch' '
+ echo ref: refs/heads/unborn >.git/HEAD &&
+ git diff --cached >result &&
+ process_diffs result >actual &&
+ process_diffs "$TEST_DIRECTORY/t4013/diff.diff_--cached" >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'diff --cached -- file on unborn branch' '
+ git diff --cached -- file0 >result &&
+ process_diffs result >actual &&
+ process_diffs "$TEST_DIRECTORY/t4013/diff.diff_--cached_--_file0" >expected &&
+ test_cmp expected actual
+'
+test_expect_success 'diff --line-prefix with spaces' '
+ git diff --line-prefix="| | | " --cached -- file0 >result &&
+ process_diffs result >actual &&
+ process_diffs "$TEST_DIRECTORY/t4013/diff.diff_--line-prefix_--cached_--_file0" >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'diff-tree --stdin with log formatting' '
+ cat >expect <<-\EOF &&
+ Side
+ Third
+ Second
+ EOF
+ git rev-list master | git diff-tree --stdin --format=%s -s >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'diff-tree --stdin with pathspec' '
+ cat >expect <<-EOF &&
+ Third
+
+ dir/sub
+ Second
+
+ dir/sub
+ EOF
+ git rev-list master^ |
+ git diff-tree -r --stdin --name-only --format=%s dir >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'show A B ... -- <pathspec>' '
+ # side touches dir/sub, file0, and file3
+ # master^ touches dir/sub, and file1
+ # master^^ touches dir/sub, file0, and file2
+ git show --name-only --format="<%s>" side master^ master^^ -- dir >actual &&
+ cat >expect <<-\EOF &&
+ <Side>
+
+ dir/sub
+ <Third>
+
+ dir/sub
+ <Second>
+
+ dir/sub
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'diff -I<regex>: setup' '
+ git checkout master &&
+ test_seq 50 >file0 &&
+ git commit -m "Set up -I<regex> test file" file0 &&
+ test_seq 50 | sed -e "s/13/ten and three/" -e "/7\$/d" >file0 &&
+ echo >>file0
+'
+test_expect_success 'diff -I<regex>' '
+ git diff --ignore-blank-lines -I"ten.*e" -I"^[124-9]" >actual &&
+ cat >expect <<-\EOF &&
+ diff --git a/file0 b/file0
+ --- a/file0
+ +++ b/file0
+ @@ -34,7 +31,6 @@
+ 34
+ 35
+ 36
+ -37
+ 38
+ 39
+ 40
+ EOF
+ compare_diff_patch expect actual
+'
+
+test_expect_success 'diff -I<regex> --stat' '
+ git diff --stat --ignore-blank-lines -I"ten.*e" -I"^[124-9]" >actual &&
+ cat >expect <<-\EOF &&
+ file0 | 1 -
+ 1 file changed, 1 deletion(-)
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'diff -I<regex>: detect malformed regex' '
+ test_expect_code 129 git diff --ignore-matching-lines="^[124-9" 2>error &&
+ test_grep "invalid regex given to -I: " error
+'
+
+# check_prefix <patch> <src> <dst>
+# check only lines with paths to avoid dependency on exact oid/contents
+check_prefix () {
+ grep -E '^(diff|---|\+\+\+) ' "$1" >actual.paths &&
+ cat >expect <<-EOF &&
+ diff --git $2 $3
+ --- $2
+ +++ $3
+ EOF
+ test_cmp expect actual.paths
+}
+
+test_expect_success 'diff-files does not respect diff.noprefix' '
+ git -c diff.noprefix diff-files -p >actual &&
+ check_prefix actual a/file0 b/file0
+'
+
+test_expect_success 'diff-files respects --no-prefix' '
+ git diff-files -p --no-prefix >actual &&
+ check_prefix actual file0 file0
+'
+
+test_expect_success 'diff respects diff.noprefix' '
+ git -c diff.noprefix diff >actual &&
+ check_prefix actual file0 file0
+'
+
+test_expect_success 'diff --default-prefix overrides diff.noprefix' '
+ git -c diff.noprefix diff --default-prefix >actual &&
+ check_prefix actual a/file0 b/file0
+'
+
+test_expect_success 'diff respects diff.mnemonicprefix' '
+ git -c diff.mnemonicprefix diff >actual &&
+ check_prefix actual i/file0 w/file0
+'
+
+test_expect_success 'diff --default-prefix overrides diff.mnemonicprefix' '
+ git -c diff.mnemonicprefix diff --default-prefix >actual &&
+ check_prefix actual a/file0 b/file0
+'
+
+test_done
diff --git a/t/t4013/diff.config_format.subjectprefix_DIFFERENT_PREFIX b/t/t4013/diff.config_format.subjectprefix_DIFFERENT_PREFIX
new file mode 100644
index 0000000..78f8970
--- /dev/null
+++ b/t/t4013/diff.config_format.subjectprefix_DIFFERENT_PREFIX
@@ -0,0 +1,2 @@
+$ git config format.subjectprefix DIFFERENT_PREFIX
+$
diff --git a/t/t4013/diff.diff-tree_--cc_--patch-with-stat_--summary_master b/t/t4013/diff.diff-tree_--cc_--patch-with-stat_--summary_master
new file mode 100644
index 0000000..9951e36
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--cc_--patch-with-stat_--summary_master
@@ -0,0 +1,34 @@
+$ git diff-tree --cc --patch-with-stat --summary master
+59d314ad6f356dd08601a4cd5e530381da3e3c64
+ dir/sub | 2 ++
+ file0 | 3 +++
+ 2 files changed, 5 insertions(+)
+
+diff --cc dir/sub
+index cead32e,7289e35..992913c
+--- a/dir/sub
++++ b/dir/sub
+@@@ -1,6 -1,4 +1,8 @@@
+ A
+ B
+ +C
+ +D
+ +E
+ +F
++ 1
++ 2
+diff --cc file0
+index b414108,f4615da..10a8a9f
+--- a/file0
++++ b/file0
+@@@ -1,6 -1,6 +1,9 @@@
+ 1
+ 2
+ 3
+ +4
+ +5
+ +6
++ A
++ B
++ C
+$
diff --git a/t/t4013/diff.diff-tree_--cc_--patch-with-stat_--summary_side b/t/t4013/diff.diff-tree_--cc_--patch-with-stat_--summary_side
new file mode 100644
index 0000000..cec33fa
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--cc_--patch-with-stat_--summary_side
@@ -0,0 +1,39 @@
+$ git diff-tree --cc --patch-with-stat --summary side
+c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file3 | 4 ++++
+ 3 files changed, 9 insertions(+)
+ create mode 100644 file3
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+$
diff --git a/t/t4013/diff.diff-tree_--cc_--patch-with-stat_master b/t/t4013/diff.diff-tree_--cc_--patch-with-stat_master
new file mode 100644
index 0000000..db3c0a7
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--cc_--patch-with-stat_master
@@ -0,0 +1,34 @@
+$ git diff-tree --cc --patch-with-stat master
+59d314ad6f356dd08601a4cd5e530381da3e3c64
+ dir/sub | 2 ++
+ file0 | 3 +++
+ 2 files changed, 5 insertions(+)
+
+diff --cc dir/sub
+index cead32e,7289e35..992913c
+--- a/dir/sub
++++ b/dir/sub
+@@@ -1,6 -1,4 +1,8 @@@
+ A
+ B
+ +C
+ +D
+ +E
+ +F
++ 1
++ 2
+diff --cc file0
+index b414108,f4615da..10a8a9f
+--- a/file0
++++ b/file0
+@@@ -1,6 -1,6 +1,9 @@@
+ 1
+ 2
+ 3
+ +4
+ +5
+ +6
++ A
++ B
++ C
+$
diff --git a/t/t4013/diff.diff-tree_--cc_--shortstat_master b/t/t4013/diff.diff-tree_--cc_--shortstat_master
new file mode 100644
index 0000000..a4ca42d
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--cc_--shortstat_master
@@ -0,0 +1,4 @@
+$ git diff-tree --cc --shortstat master
+59d314ad6f356dd08601a4cd5e530381da3e3c64
+ 2 files changed, 5 insertions(+)
+$
diff --git a/t/t4013/diff.diff-tree_--cc_--stat_--summary_master b/t/t4013/diff.diff-tree_--cc_--stat_--summary_master
new file mode 100644
index 0000000..d019867
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--cc_--stat_--summary_master
@@ -0,0 +1,6 @@
+$ git diff-tree --cc --stat --summary master
+59d314ad6f356dd08601a4cd5e530381da3e3c64
+ dir/sub | 2 ++
+ file0 | 3 +++
+ 2 files changed, 5 insertions(+)
+$
diff --git a/t/t4013/diff.diff-tree_--cc_--stat_--summary_side b/t/t4013/diff.diff-tree_--cc_--stat_--summary_side
new file mode 100644
index 0000000..12b2eee
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--cc_--stat_--summary_side
@@ -0,0 +1,8 @@
+$ git diff-tree --cc --stat --summary side
+c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file3 | 4 ++++
+ 3 files changed, 9 insertions(+)
+ create mode 100644 file3
+$
diff --git a/t/t4013/diff.diff-tree_--cc_--stat_master b/t/t4013/diff.diff-tree_--cc_--stat_master
new file mode 100644
index 0000000..40b9179
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--cc_--stat_master
@@ -0,0 +1,6 @@
+$ git diff-tree --cc --stat master
+59d314ad6f356dd08601a4cd5e530381da3e3c64
+ dir/sub | 2 ++
+ file0 | 3 +++
+ 2 files changed, 5 insertions(+)
+$
diff --git a/t/t4013/diff.diff-tree_--cc_--summary_REVERSE b/t/t4013/diff.diff-tree_--cc_--summary_REVERSE
new file mode 100644
index 0000000..e208dd5
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--cc_--summary_REVERSE
@@ -0,0 +1,6 @@
+$ git diff-tree --cc --summary REVERSE
+2562325a7ee916efb2481da93073b82cec801cbc
+ create mode 100644 file1
+ delete mode 100644 file2
+ delete mode 100644 file3
+$
diff --git a/t/t4013/diff.diff-tree_--cc_master b/t/t4013/diff.diff-tree_--cc_master
new file mode 100644
index 0000000..5ecb4e1
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--cc_master
@@ -0,0 +1,30 @@
+$ git diff-tree --cc master
+59d314ad6f356dd08601a4cd5e530381da3e3c64
+diff --cc dir/sub
+index cead32e,7289e35..992913c
+--- a/dir/sub
++++ b/dir/sub
+@@@ -1,6 -1,4 +1,8 @@@
+ A
+ B
+ +C
+ +D
+ +E
+ +F
++ 1
++ 2
+diff --cc file0
+index b414108,f4615da..10a8a9f
+--- a/file0
++++ b/file0
+@@@ -1,6 -1,6 +1,9 @@@
+ 1
+ 2
+ 3
+ +4
+ +5
+ +6
++ A
++ B
++ C
+$
diff --git a/t/t4013/diff.diff-tree_--format=%N_note b/t/t4013/diff.diff-tree_--format=%N_note
new file mode 100644
index 0000000..93042ed
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--format=%N_note
@@ -0,0 +1,6 @@
+$ git diff-tree --format=%N note
+note
+
+
+:100644 100755 01e79c32a8c99c557f0757da7cb6d65b3414466d 01e79c32a8c99c557f0757da7cb6d65b3414466d M file2
+$
diff --git a/t/t4013/diff.diff-tree_--patch-with-raw_initial b/t/t4013/diff.diff-tree_--patch-with-raw_initial
new file mode 100644
index 0000000..fc177ab
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--patch-with-raw_initial
@@ -0,0 +1,2 @@
+$ git diff-tree --patch-with-raw initial
+$
diff --git a/t/t4013/diff.diff-tree_--patch-with-stat_initial b/t/t4013/diff.diff-tree_--patch-with-stat_initial
new file mode 100644
index 0000000..bd905b1
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--patch-with-stat_initial
@@ -0,0 +1,2 @@
+$ git diff-tree --patch-with-stat initial
+$
diff --git a/t/t4013/diff.diff-tree_--pretty=oneline_--patch-with-raw_initial b/t/t4013/diff.diff-tree_--pretty=oneline_--patch-with-raw_initial
new file mode 100644
index 0000000..7bb8b45
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--pretty=oneline_--patch-with-raw_initial
@@ -0,0 +1,2 @@
+$ git diff-tree --pretty=oneline --patch-with-raw initial
+$
diff --git a/t/t4013/diff.diff-tree_--pretty=oneline_--patch-with-stat_initial b/t/t4013/diff.diff-tree_--pretty=oneline_--patch-with-stat_initial
new file mode 100644
index 0000000..cbdde4f
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--pretty=oneline_--patch-with-stat_initial
@@ -0,0 +1,2 @@
+$ git diff-tree --pretty=oneline --patch-with-stat initial
+$
diff --git a/t/t4013/diff.diff-tree_--pretty=oneline_--root_--patch-with-raw_initial b/t/t4013/diff.diff-tree_--pretty=oneline_--root_--patch-with-raw_initial
new file mode 100644
index 0000000..cd79f1a
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--pretty=oneline_--root_--patch-with-raw_initial
@@ -0,0 +1,33 @@
+$ git diff-tree --pretty=oneline --root --patch-with-raw initial
+444ac553ac7612cc88969031b02b3767fb8a353a Initial
+:000000 100644 0000000000000000000000000000000000000000 35d242ba79ae89ac695e26b3d4c27a8e6f028f9e A dir/sub
+:000000 100644 0000000000000000000000000000000000000000 01e79c32a8c99c557f0757da7cb6d65b3414466d A file0
+:000000 100644 0000000000000000000000000000000000000000 01e79c32a8c99c557f0757da7cb6d65b3414466d A file2
+
+diff --git a/dir/sub b/dir/sub
+new file mode 100644
+index 0000000..35d242b
+--- /dev/null
++++ b/dir/sub
+@@ -0,0 +1,2 @@
++A
++B
+diff --git a/file0 b/file0
+new file mode 100644
+index 0000000..01e79c3
+--- /dev/null
++++ b/file0
+@@ -0,0 +1,3 @@
++1
++2
++3
+diff --git a/file2 b/file2
+new file mode 100644
+index 0000000..01e79c3
+--- /dev/null
++++ b/file2
+@@ -0,0 +1,3 @@
++1
++2
++3
+$
diff --git a/t/t4013/diff.diff-tree_--pretty=oneline_--root_--patch-with-stat_initial b/t/t4013/diff.diff-tree_--pretty=oneline_--root_--patch-with-stat_initial
new file mode 100644
index 0000000..817ed06
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--pretty=oneline_--root_--patch-with-stat_initial
@@ -0,0 +1,34 @@
+$ git diff-tree --pretty=oneline --root --patch-with-stat initial
+444ac553ac7612cc88969031b02b3767fb8a353a Initial
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file2 | 3 +++
+ 3 files changed, 8 insertions(+)
+
+diff --git a/dir/sub b/dir/sub
+new file mode 100644
+index 0000000..35d242b
+--- /dev/null
++++ b/dir/sub
+@@ -0,0 +1,2 @@
++A
++B
+diff --git a/file0 b/file0
+new file mode 100644
+index 0000000..01e79c3
+--- /dev/null
++++ b/file0
+@@ -0,0 +1,3 @@
++1
++2
++3
+diff --git a/file2 b/file2
+new file mode 100644
+index 0000000..01e79c3
+--- /dev/null
++++ b/file2
+@@ -0,0 +1,3 @@
++1
++2
++3
+$
diff --git a/t/t4013/diff.diff-tree_--pretty=oneline_--root_-p_initial b/t/t4013/diff.diff-tree_--pretty=oneline_--root_-p_initial
new file mode 100644
index 0000000..3c5092c
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--pretty=oneline_--root_-p_initial
@@ -0,0 +1,29 @@
+$ git diff-tree --pretty=oneline --root -p initial
+444ac553ac7612cc88969031b02b3767fb8a353a Initial
+diff --git a/dir/sub b/dir/sub
+new file mode 100644
+index 0000000..35d242b
+--- /dev/null
++++ b/dir/sub
+@@ -0,0 +1,2 @@
++A
++B
+diff --git a/file0 b/file0
+new file mode 100644
+index 0000000..01e79c3
+--- /dev/null
++++ b/file0
+@@ -0,0 +1,3 @@
++1
++2
++3
+diff --git a/file2 b/file2
+new file mode 100644
+index 0000000..01e79c3
+--- /dev/null
++++ b/file2
+@@ -0,0 +1,3 @@
++1
++2
++3
+$
diff --git a/t/t4013/diff.diff-tree_--pretty=oneline_--root_initial b/t/t4013/diff.diff-tree_--pretty=oneline_--root_initial
new file mode 100644
index 0000000..08920ac
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--pretty=oneline_--root_initial
@@ -0,0 +1,6 @@
+$ git diff-tree --pretty=oneline --root initial
+444ac553ac7612cc88969031b02b3767fb8a353a Initial
+:000000 040000 0000000000000000000000000000000000000000 da7a33fa77d8066d6698643940ce5860fe2d7fb3 A dir
+:000000 100644 0000000000000000000000000000000000000000 01e79c32a8c99c557f0757da7cb6d65b3414466d A file0
+:000000 100644 0000000000000000000000000000000000000000 01e79c32a8c99c557f0757da7cb6d65b3414466d A file2
+$
diff --git a/t/t4013/diff.diff-tree_--pretty=oneline_-p_initial b/t/t4013/diff.diff-tree_--pretty=oneline_-p_initial
new file mode 100644
index 0000000..94b76bf
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--pretty=oneline_-p_initial
@@ -0,0 +1,2 @@
+$ git diff-tree --pretty=oneline -p initial
+$
diff --git a/t/t4013/diff.diff-tree_--pretty=oneline_initial b/t/t4013/diff.diff-tree_--pretty=oneline_initial
new file mode 100644
index 0000000..d50970d
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--pretty=oneline_initial
@@ -0,0 +1,2 @@
+$ git diff-tree --pretty=oneline initial
+$
diff --git a/t/t4013/diff.diff-tree_--pretty_--notes_note b/t/t4013/diff.diff-tree_--pretty_--notes_note
new file mode 100644
index 0000000..4d0bde6
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--pretty_--notes_note
@@ -0,0 +1,12 @@
+$ git diff-tree --pretty --notes note
+commit a6f364368ca320bc5a92e18912e16fa6b3dff598
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:06:00 2006 +0000
+
+ update mode (file2)
+
+Notes:
+ note
+
+:100644 100755 01e79c32a8c99c557f0757da7cb6d65b3414466d 01e79c32a8c99c557f0757da7cb6d65b3414466d M file2
+$
diff --git a/t/t4013/diff.diff-tree_--pretty_--patch-with-raw_initial b/t/t4013/diff.diff-tree_--pretty_--patch-with-raw_initial
new file mode 100644
index 0000000..3a85316
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--pretty_--patch-with-raw_initial
@@ -0,0 +1,2 @@
+$ git diff-tree --pretty --patch-with-raw initial
+$
diff --git a/t/t4013/diff.diff-tree_--pretty_--patch-with-stat_initial b/t/t4013/diff.diff-tree_--pretty_--patch-with-stat_initial
new file mode 100644
index 0000000..2e08239
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--pretty_--patch-with-stat_initial
@@ -0,0 +1,2 @@
+$ git diff-tree --pretty --patch-with-stat initial
+$
diff --git a/t/t4013/diff.diff-tree_--pretty_--patch-with-stat_side b/t/t4013/diff.diff-tree_--pretty_--patch-with-stat_side
new file mode 100644
index 0000000..fe3f6b7
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--pretty_--patch-with-stat_side
@@ -0,0 +1,43 @@
+$ git diff-tree --pretty --patch-with-stat side
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file3 | 4 ++++
+ 3 files changed, 9 insertions(+)
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+$
diff --git a/t/t4013/diff.diff-tree_--pretty_--root_--patch-with-raw_initial b/t/t4013/diff.diff-tree_--pretty_--root_--patch-with-raw_initial
new file mode 100644
index 0000000..a3203bd
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--pretty_--root_--patch-with-raw_initial
@@ -0,0 +1,38 @@
+$ git diff-tree --pretty --root --patch-with-raw initial
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+
+:000000 100644 0000000000000000000000000000000000000000 35d242ba79ae89ac695e26b3d4c27a8e6f028f9e A dir/sub
+:000000 100644 0000000000000000000000000000000000000000 01e79c32a8c99c557f0757da7cb6d65b3414466d A file0
+:000000 100644 0000000000000000000000000000000000000000 01e79c32a8c99c557f0757da7cb6d65b3414466d A file2
+
+diff --git a/dir/sub b/dir/sub
+new file mode 100644
+index 0000000..35d242b
+--- /dev/null
++++ b/dir/sub
+@@ -0,0 +1,2 @@
++A
++B
+diff --git a/file0 b/file0
+new file mode 100644
+index 0000000..01e79c3
+--- /dev/null
++++ b/file0
+@@ -0,0 +1,3 @@
++1
++2
++3
+diff --git a/file2 b/file2
+new file mode 100644
+index 0000000..01e79c3
+--- /dev/null
++++ b/file2
+@@ -0,0 +1,3 @@
++1
++2
++3
+$
diff --git a/t/t4013/diff.diff-tree_--pretty_--root_--patch-with-stat_initial b/t/t4013/diff.diff-tree_--pretty_--root_--patch-with-stat_initial
new file mode 100644
index 0000000..06eb77e
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--pretty_--root_--patch-with-stat_initial
@@ -0,0 +1,39 @@
+$ git diff-tree --pretty --root --patch-with-stat initial
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file2 | 3 +++
+ 3 files changed, 8 insertions(+)
+
+diff --git a/dir/sub b/dir/sub
+new file mode 100644
+index 0000000..35d242b
+--- /dev/null
++++ b/dir/sub
+@@ -0,0 +1,2 @@
++A
++B
+diff --git a/file0 b/file0
+new file mode 100644
+index 0000000..01e79c3
+--- /dev/null
++++ b/file0
+@@ -0,0 +1,3 @@
++1
++2
++3
+diff --git a/file2 b/file2
+new file mode 100644
+index 0000000..01e79c3
+--- /dev/null
++++ b/file2
+@@ -0,0 +1,3 @@
++1
++2
++3
+$
diff --git a/t/t4013/diff.diff-tree_--pretty_--root_--stat_--compact-summary_initial b/t/t4013/diff.diff-tree_--pretty_--root_--stat_--compact-summary_initial
new file mode 100644
index 0000000..d6451ff
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--pretty_--root_--stat_--compact-summary_initial
@@ -0,0 +1,12 @@
+$ git diff-tree --pretty --root --stat --compact-summary initial
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+
+ dir/sub (new) | 2 ++
+ file0 (new) | 3 +++
+ file2 (new) | 3 +++
+ 3 files changed, 8 insertions(+)
+$
diff --git a/t/t4013/diff.diff-tree_--pretty_--root_--stat_--summary_initial b/t/t4013/diff.diff-tree_--pretty_--root_--stat_--summary_initial
new file mode 100644
index 0000000..680eab5
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--pretty_--root_--stat_--summary_initial
@@ -0,0 +1,15 @@
+$ git diff-tree --pretty --root --stat --summary initial
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file2 | 3 +++
+ 3 files changed, 8 insertions(+)
+ create mode 100644 dir/sub
+ create mode 100644 file0
+ create mode 100644 file2
+$
diff --git a/t/t4013/diff.diff-tree_--pretty_--root_--stat_initial b/t/t4013/diff.diff-tree_--pretty_--root_--stat_initial
new file mode 100644
index 0000000..9722d1b
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--pretty_--root_--stat_initial
@@ -0,0 +1,12 @@
+$ git diff-tree --pretty --root --stat initial
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file2 | 3 +++
+ 3 files changed, 8 insertions(+)
+$
diff --git a/t/t4013/diff.diff-tree_--pretty_--root_--summary_-r_initial b/t/t4013/diff.diff-tree_--pretty_--root_--summary_-r_initial
new file mode 100644
index 0000000..ccdaafb
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--pretty_--root_--summary_-r_initial
@@ -0,0 +1,11 @@
+$ git diff-tree --pretty --root --summary -r initial
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+
+ create mode 100644 dir/sub
+ create mode 100644 file0
+ create mode 100644 file2
+$
diff --git a/t/t4013/diff.diff-tree_--pretty_--root_--summary_initial b/t/t4013/diff.diff-tree_--pretty_--root_--summary_initial
new file mode 100644
index 0000000..58e5f74
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--pretty_--root_--summary_initial
@@ -0,0 +1,11 @@
+$ git diff-tree --pretty --root --summary initial
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+
+ create mode 100644 dir/sub
+ create mode 100644 file0
+ create mode 100644 file2
+$
diff --git a/t/t4013/diff.diff-tree_--pretty_--root_-p_initial b/t/t4013/diff.diff-tree_--pretty_--root_-p_initial
new file mode 100644
index 0000000..d0411f6
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--pretty_--root_-p_initial
@@ -0,0 +1,34 @@
+$ git diff-tree --pretty --root -p initial
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+
+diff --git a/dir/sub b/dir/sub
+new file mode 100644
+index 0000000..35d242b
+--- /dev/null
++++ b/dir/sub
+@@ -0,0 +1,2 @@
++A
++B
+diff --git a/file0 b/file0
+new file mode 100644
+index 0000000..01e79c3
+--- /dev/null
++++ b/file0
+@@ -0,0 +1,3 @@
++1
++2
++3
+diff --git a/file2 b/file2
+new file mode 100644
+index 0000000..01e79c3
+--- /dev/null
++++ b/file2
+@@ -0,0 +1,3 @@
++1
++2
++3
+$
diff --git a/t/t4013/diff.diff-tree_--pretty_--root_initial b/t/t4013/diff.diff-tree_--pretty_--root_initial
new file mode 100644
index 0000000..94e32ea
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--pretty_--root_initial
@@ -0,0 +1,11 @@
+$ git diff-tree --pretty --root initial
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+
+:000000 040000 0000000000000000000000000000000000000000 da7a33fa77d8066d6698643940ce5860fe2d7fb3 A dir
+:000000 100644 0000000000000000000000000000000000000000 01e79c32a8c99c557f0757da7cb6d65b3414466d A file0
+:000000 100644 0000000000000000000000000000000000000000 01e79c32a8c99c557f0757da7cb6d65b3414466d A file2
+$
diff --git a/t/t4013/diff.diff-tree_--pretty_--stat_--summary_initial b/t/t4013/diff.diff-tree_--pretty_--stat_--summary_initial
new file mode 100644
index 0000000..c22983a
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--pretty_--stat_--summary_initial
@@ -0,0 +1,2 @@
+$ git diff-tree --pretty --stat --summary initial
+$
diff --git a/t/t4013/diff.diff-tree_--pretty_--stat_initial b/t/t4013/diff.diff-tree_--pretty_--stat_initial
new file mode 100644
index 0000000..8fdcfb4
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--pretty_--stat_initial
@@ -0,0 +1,2 @@
+$ git diff-tree --pretty --stat initial
+$
diff --git a/t/t4013/diff.diff-tree_--pretty_--summary_initial b/t/t4013/diff.diff-tree_--pretty_--summary_initial
new file mode 100644
index 0000000..9bc2c4f
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--pretty_--summary_initial
@@ -0,0 +1,2 @@
+$ git diff-tree --pretty --summary initial
+$
diff --git a/t/t4013/diff.diff-tree_--pretty_-R_--root_--stat_--compact-summary_initial b/t/t4013/diff.diff-tree_--pretty_-R_--root_--stat_--compact-summary_initial
new file mode 100644
index 0000000..1989e55
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--pretty_-R_--root_--stat_--compact-summary_initial
@@ -0,0 +1,12 @@
+$ git diff-tree --pretty -R --root --stat --compact-summary initial
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+
+ dir/sub (gone) | 2 --
+ file0 (gone) | 3 ---
+ file2 (gone) | 3 ---
+ 3 files changed, 8 deletions(-)
+$
diff --git a/t/t4013/diff.diff-tree_--pretty_-p_initial b/t/t4013/diff.diff-tree_--pretty_-p_initial
new file mode 100644
index 0000000..3c9942f
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--pretty_-p_initial
@@ -0,0 +1,2 @@
+$ git diff-tree --pretty -p initial
+$
diff --git a/t/t4013/diff.diff-tree_--pretty_-p_side b/t/t4013/diff.diff-tree_--pretty_-p_side
new file mode 100644
index 0000000..b993aa7
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--pretty_-p_side
@@ -0,0 +1,38 @@
+$ git diff-tree --pretty -p side
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+$
diff --git a/t/t4013/diff.diff-tree_--pretty_initial b/t/t4013/diff.diff-tree_--pretty_initial
new file mode 100644
index 0000000..14715bf
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--pretty_initial
@@ -0,0 +1,2 @@
+$ git diff-tree --pretty initial
+$
diff --git a/t/t4013/diff.diff-tree_--pretty_note b/t/t4013/diff.diff-tree_--pretty_note
new file mode 100644
index 0000000..1fa5967
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--pretty_note
@@ -0,0 +1,9 @@
+$ git diff-tree --pretty note
+commit a6f364368ca320bc5a92e18912e16fa6b3dff598
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:06:00 2006 +0000
+
+ update mode (file2)
+
+:100644 100755 01e79c32a8c99c557f0757da7cb6d65b3414466d 01e79c32a8c99c557f0757da7cb6d65b3414466d M file2
+$
diff --git a/t/t4013/diff.diff-tree_--pretty_side b/t/t4013/diff.diff-tree_--pretty_side
new file mode 100644
index 0000000..e9b6e1c
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--pretty_side
@@ -0,0 +1,11 @@
+$ git diff-tree --pretty side
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+
+:040000 040000 da7a33fa77d8066d6698643940ce5860fe2d7fb3 f977ed46ae6873c1c30ab878e15a4accedc3618b M dir
+:100644 100644 01e79c32a8c99c557f0757da7cb6d65b3414466d f4615da674c09df322d6ba8d6b21ecfb1b1ba510 M file0
+:000000 100644 0000000000000000000000000000000000000000 7289e35bff32727c08dda207511bec138fdb9ea5 A file3
+$
diff --git a/t/t4013/diff.diff-tree_--root_--abbrev_initial b/t/t4013/diff.diff-tree_--root_--abbrev_initial
new file mode 100644
index 0000000..5aa84b2
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--root_--abbrev_initial
@@ -0,0 +1,6 @@
+$ git diff-tree --root --abbrev initial
+444ac553ac7612cc88969031b02b3767fb8a353a
+:000000 040000 0000000... da7a33f... A dir
+:000000 100644 0000000... 01e79c3... A file0
+:000000 100644 0000000... 01e79c3... A file2
+$
diff --git a/t/t4013/diff.diff-tree_--root_--patch-with-raw_initial b/t/t4013/diff.diff-tree_--root_--patch-with-raw_initial
new file mode 100644
index 0000000..d295e47
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--root_--patch-with-raw_initial
@@ -0,0 +1,33 @@
+$ git diff-tree --root --patch-with-raw initial
+444ac553ac7612cc88969031b02b3767fb8a353a
+:000000 100644 0000000000000000000000000000000000000000 35d242ba79ae89ac695e26b3d4c27a8e6f028f9e A dir/sub
+:000000 100644 0000000000000000000000000000000000000000 01e79c32a8c99c557f0757da7cb6d65b3414466d A file0
+:000000 100644 0000000000000000000000000000000000000000 01e79c32a8c99c557f0757da7cb6d65b3414466d A file2
+
+diff --git a/dir/sub b/dir/sub
+new file mode 100644
+index 0000000..35d242b
+--- /dev/null
++++ b/dir/sub
+@@ -0,0 +1,2 @@
++A
++B
+diff --git a/file0 b/file0
+new file mode 100644
+index 0000000..01e79c3
+--- /dev/null
++++ b/file0
+@@ -0,0 +1,3 @@
++1
++2
++3
+diff --git a/file2 b/file2
+new file mode 100644
+index 0000000..01e79c3
+--- /dev/null
++++ b/file2
+@@ -0,0 +1,3 @@
++1
++2
++3
+$
diff --git a/t/t4013/diff.diff-tree_--root_--patch-with-stat_initial b/t/t4013/diff.diff-tree_--root_--patch-with-stat_initial
new file mode 100644
index 0000000..ad69ffe
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--root_--patch-with-stat_initial
@@ -0,0 +1,34 @@
+$ git diff-tree --root --patch-with-stat initial
+444ac553ac7612cc88969031b02b3767fb8a353a
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file2 | 3 +++
+ 3 files changed, 8 insertions(+)
+
+diff --git a/dir/sub b/dir/sub
+new file mode 100644
+index 0000000..35d242b
+--- /dev/null
++++ b/dir/sub
+@@ -0,0 +1,2 @@
++A
++B
+diff --git a/file0 b/file0
+new file mode 100644
+index 0000000..01e79c3
+--- /dev/null
++++ b/file0
+@@ -0,0 +1,3 @@
++1
++2
++3
+diff --git a/file2 b/file2
+new file mode 100644
+index 0000000..01e79c3
+--- /dev/null
++++ b/file2
+@@ -0,0 +1,3 @@
++1
++2
++3
+$
diff --git a/t/t4013/diff.diff-tree_--root_-p_--abbrev=10_initial b/t/t4013/diff.diff-tree_--root_-p_--abbrev=10_initial
new file mode 100644
index 0000000..7518a90
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--root_-p_--abbrev=10_initial
@@ -0,0 +1,29 @@
+$ git diff-tree --root -p --abbrev=10 initial
+444ac553ac7612cc88969031b02b3767fb8a353a
+diff --git a/dir/sub b/dir/sub
+new file mode 100644
+index 0000000000..35d242ba79
+--- /dev/null
++++ b/dir/sub
+@@ -0,0 +1,2 @@
++A
++B
+diff --git a/file0 b/file0
+new file mode 100644
+index 0000000000..01e79c32a8
+--- /dev/null
++++ b/file0
+@@ -0,0 +1,3 @@
++1
++2
++3
+diff --git a/file2 b/file2
+new file mode 100644
+index 0000000000..01e79c32a8
+--- /dev/null
++++ b/file2
+@@ -0,0 +1,3 @@
++1
++2
++3
+$
diff --git a/t/t4013/diff.diff-tree_--root_-p_--full-index_--abbrev=10_initial b/t/t4013/diff.diff-tree_--root_-p_--full-index_--abbrev=10_initial
new file mode 100644
index 0000000..69f913f
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--root_-p_--full-index_--abbrev=10_initial
@@ -0,0 +1,29 @@
+$ git diff-tree --root -p --full-index --abbrev=10 initial
+444ac553ac7612cc88969031b02b3767fb8a353a
+diff --git a/dir/sub b/dir/sub
+new file mode 100644
+index 0000000000000000000000000000000000000000..35d242ba79ae89ac695e26b3d4c27a8e6f028f9e
+--- /dev/null
++++ b/dir/sub
+@@ -0,0 +1,2 @@
++A
++B
+diff --git a/file0 b/file0
+new file mode 100644
+index 0000000000000000000000000000000000000000..01e79c32a8c99c557f0757da7cb6d65b3414466d
+--- /dev/null
++++ b/file0
+@@ -0,0 +1,3 @@
++1
++2
++3
+diff --git a/file2 b/file2
+new file mode 100644
+index 0000000000000000000000000000000000000000..01e79c32a8c99c557f0757da7cb6d65b3414466d
+--- /dev/null
++++ b/file2
+@@ -0,0 +1,3 @@
++1
++2
++3
+$
diff --git a/t/t4013/diff.diff-tree_--root_-p_--full-index_initial b/t/t4013/diff.diff-tree_--root_-p_--full-index_initial
new file mode 100644
index 0000000..1b0b671
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--root_-p_--full-index_initial
@@ -0,0 +1,29 @@
+$ git diff-tree --root -p --full-index initial
+444ac553ac7612cc88969031b02b3767fb8a353a
+diff --git a/dir/sub b/dir/sub
+new file mode 100644
+index 0000000000000000000000000000000000000000..35d242ba79ae89ac695e26b3d4c27a8e6f028f9e
+--- /dev/null
++++ b/dir/sub
+@@ -0,0 +1,2 @@
++A
++B
+diff --git a/file0 b/file0
+new file mode 100644
+index 0000000000000000000000000000000000000000..01e79c32a8c99c557f0757da7cb6d65b3414466d
+--- /dev/null
++++ b/file0
+@@ -0,0 +1,3 @@
++1
++2
++3
+diff --git a/file2 b/file2
+new file mode 100644
+index 0000000000000000000000000000000000000000..01e79c32a8c99c557f0757da7cb6d65b3414466d
+--- /dev/null
++++ b/file2
+@@ -0,0 +1,3 @@
++1
++2
++3
+$
diff --git a/t/t4013/diff.diff-tree_--root_-p_initial b/t/t4013/diff.diff-tree_--root_-p_initial
new file mode 100644
index 0000000..3219c72
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--root_-p_initial
@@ -0,0 +1,29 @@
+$ git diff-tree --root -p initial
+444ac553ac7612cc88969031b02b3767fb8a353a
+diff --git a/dir/sub b/dir/sub
+new file mode 100644
+index 0000000..35d242b
+--- /dev/null
++++ b/dir/sub
+@@ -0,0 +1,2 @@
++A
++B
+diff --git a/file0 b/file0
+new file mode 100644
+index 0000000..01e79c3
+--- /dev/null
++++ b/file0
+@@ -0,0 +1,3 @@
++1
++2
++3
+diff --git a/file2 b/file2
+new file mode 100644
+index 0000000..01e79c3
+--- /dev/null
++++ b/file2
+@@ -0,0 +1,3 @@
++1
++2
++3
+$
diff --git a/t/t4013/diff.diff-tree_--root_-r_--abbrev=4_initial b/t/t4013/diff.diff-tree_--root_-r_--abbrev=4_initial
new file mode 100644
index 0000000..0c53616
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--root_-r_--abbrev=4_initial
@@ -0,0 +1,6 @@
+$ git diff-tree --root -r --abbrev=4 initial
+444ac553ac7612cc88969031b02b3767fb8a353a
+:000000 100644 0000... 35d2... A dir/sub
+:000000 100644 0000... 01e7... A file0
+:000000 100644 0000... 01e7... A file2
+$
diff --git a/t/t4013/diff.diff-tree_--root_-r_--abbrev_initial b/t/t4013/diff.diff-tree_--root_-r_--abbrev_initial
new file mode 100644
index 0000000..c7b460f
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--root_-r_--abbrev_initial
@@ -0,0 +1,6 @@
+$ git diff-tree --root -r --abbrev initial
+444ac553ac7612cc88969031b02b3767fb8a353a
+:000000 100644 0000000... 35d242b... A dir/sub
+:000000 100644 0000000... 01e79c3... A file0
+:000000 100644 0000000... 01e79c3... A file2
+$
diff --git a/t/t4013/diff.diff-tree_--root_-r_initial b/t/t4013/diff.diff-tree_--root_-r_initial
new file mode 100644
index 0000000..eed435e
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--root_-r_initial
@@ -0,0 +1,6 @@
+$ git diff-tree --root -r initial
+444ac553ac7612cc88969031b02b3767fb8a353a
+:000000 100644 0000000000000000000000000000000000000000 35d242ba79ae89ac695e26b3d4c27a8e6f028f9e A dir/sub
+:000000 100644 0000000000000000000000000000000000000000 01e79c32a8c99c557f0757da7cb6d65b3414466d A file0
+:000000 100644 0000000000000000000000000000000000000000 01e79c32a8c99c557f0757da7cb6d65b3414466d A file2
+$
diff --git a/t/t4013/diff.diff-tree_--root_initial b/t/t4013/diff.diff-tree_--root_initial
new file mode 100644
index 0000000..ddf6b06
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--root_initial
@@ -0,0 +1,6 @@
+$ git diff-tree --root initial
+444ac553ac7612cc88969031b02b3767fb8a353a
+:000000 040000 0000000000000000000000000000000000000000 da7a33fa77d8066d6698643940ce5860fe2d7fb3 A dir
+:000000 100644 0000000000000000000000000000000000000000 01e79c32a8c99c557f0757da7cb6d65b3414466d A file0
+:000000 100644 0000000000000000000000000000000000000000 01e79c32a8c99c557f0757da7cb6d65b3414466d A file2
+$
diff --git a/t/t4013/diff.diff-tree_--stat_--compact-summary_initial_mode b/t/t4013/diff.diff-tree_--stat_--compact-summary_initial_mode
new file mode 100644
index 0000000..9c7c8f6
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--stat_--compact-summary_initial_mode
@@ -0,0 +1,4 @@
+$ git diff-tree --stat --compact-summary initial mode
+ file0 (mode +x) | 0
+ 1 file changed, 0 insertions(+), 0 deletions(-)
+$
diff --git a/t/t4013/diff.diff-tree_--stat_initial_mode b/t/t4013/diff.diff-tree_--stat_initial_mode
new file mode 100644
index 0000000..0e5943c
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--stat_initial_mode
@@ -0,0 +1,4 @@
+$ git diff-tree --stat initial mode
+ file0 | 0
+ 1 file changed, 0 insertions(+), 0 deletions(-)
+$
diff --git a/t/t4013/diff.diff-tree_--summary_initial_mode b/t/t4013/diff.diff-tree_--summary_initial_mode
new file mode 100644
index 0000000..25846b6
--- /dev/null
+++ b/t/t4013/diff.diff-tree_--summary_initial_mode
@@ -0,0 +1,3 @@
+$ git diff-tree --summary initial mode
+ mode change 100644 => 100755 file0
+$
diff --git a/t/t4013/diff.diff-tree_-R_--stat_--compact-summary_initial_mode b/t/t4013/diff.diff-tree_-R_--stat_--compact-summary_initial_mode
new file mode 100644
index 0000000..e38f3d3
--- /dev/null
+++ b/t/t4013/diff.diff-tree_-R_--stat_--compact-summary_initial_mode
@@ -0,0 +1,4 @@
+$ git diff-tree -R --stat --compact-summary initial mode
+ file0 (mode -x) | 0
+ 1 file changed, 0 insertions(+), 0 deletions(-)
+$
diff --git a/t/t4013/diff.diff-tree_-c_--abbrev_master b/t/t4013/diff.diff-tree_-c_--abbrev_master
new file mode 100644
index 0000000..b8e4aa2
--- /dev/null
+++ b/t/t4013/diff.diff-tree_-c_--abbrev_master
@@ -0,0 +1,5 @@
+$ git diff-tree -c --abbrev master
+59d314ad6f356dd08601a4cd5e530381da3e3c64
+::100644 100644 100644 cead32e... 7289e35... 992913c... MM dir/sub
+::100644 100644 100644 b414108... f4615da... 10a8a9f... MM file0
+$
diff --git a/t/t4013/diff.diff-tree_-c_--stat_--summary_master b/t/t4013/diff.diff-tree_-c_--stat_--summary_master
new file mode 100644
index 0000000..81c3021
--- /dev/null
+++ b/t/t4013/diff.diff-tree_-c_--stat_--summary_master
@@ -0,0 +1,6 @@
+$ git diff-tree -c --stat --summary master
+59d314ad6f356dd08601a4cd5e530381da3e3c64
+ dir/sub | 2 ++
+ file0 | 3 +++
+ 2 files changed, 5 insertions(+)
+$
diff --git a/t/t4013/diff.diff-tree_-c_--stat_--summary_side b/t/t4013/diff.diff-tree_-c_--stat_--summary_side
new file mode 100644
index 0000000..e8dc12b
--- /dev/null
+++ b/t/t4013/diff.diff-tree_-c_--stat_--summary_side
@@ -0,0 +1,8 @@
+$ git diff-tree -c --stat --summary side
+c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file3 | 4 ++++
+ 3 files changed, 9 insertions(+)
+ create mode 100644 file3
+$
diff --git a/t/t4013/diff.diff-tree_-c_--stat_master b/t/t4013/diff.diff-tree_-c_--stat_master
new file mode 100644
index 0000000..89d59b1
--- /dev/null
+++ b/t/t4013/diff.diff-tree_-c_--stat_master
@@ -0,0 +1,6 @@
+$ git diff-tree -c --stat master
+59d314ad6f356dd08601a4cd5e530381da3e3c64
+ dir/sub | 2 ++
+ file0 | 3 +++
+ 2 files changed, 5 insertions(+)
+$
diff --git a/t/t4013/diff.diff-tree_-c_master b/t/t4013/diff.diff-tree_-c_master
new file mode 100644
index 0000000..e2d2bb2
--- /dev/null
+++ b/t/t4013/diff.diff-tree_-c_master
@@ -0,0 +1,5 @@
+$ git diff-tree -c master
+59d314ad6f356dd08601a4cd5e530381da3e3c64
+::100644 100644 100644 cead32e925b1420c84c14cbf7cf755e7e45af8ad 7289e35bff32727c08dda207511bec138fdb9ea5 992913c5aa0a5476d10c49ed0f21fc0c6d1aedf3 MM dir/sub
+::100644 100644 100644 b414108e81e5091fe0974a1858b4d0d22b107f70 f4615da674c09df322d6ba8d6b21ecfb1b1ba510 10a8a9f3657f91a156b9f0184ed79a20adef9f7f MM file0
+$
diff --git a/t/t4013/diff.diff-tree_-m_master b/t/t4013/diff.diff-tree_-m_master
new file mode 100644
index 0000000..6d0a220
--- /dev/null
+++ b/t/t4013/diff.diff-tree_-m_master
@@ -0,0 +1,11 @@
+$ git diff-tree -m master
+59d314ad6f356dd08601a4cd5e530381da3e3c64
+:040000 040000 65f5c9dd60ce3b2b3324b618ac7accf8d912c113 0564e026437809817a64fff393079714b6dd4628 M dir
+:100644 100644 b414108e81e5091fe0974a1858b4d0d22b107f70 10a8a9f3657f91a156b9f0184ed79a20adef9f7f M file0
+59d314ad6f356dd08601a4cd5e530381da3e3c64
+:040000 040000 f977ed46ae6873c1c30ab878e15a4accedc3618b 0564e026437809817a64fff393079714b6dd4628 M dir
+:100644 100644 f4615da674c09df322d6ba8d6b21ecfb1b1ba510 10a8a9f3657f91a156b9f0184ed79a20adef9f7f M file0
+:000000 100644 0000000000000000000000000000000000000000 b1e67221afe8461efd244b487afca22d46b95eb8 A file1
+:100644 000000 01e79c32a8c99c557f0757da7cb6d65b3414466d 0000000000000000000000000000000000000000 D file2
+:100644 000000 7289e35bff32727c08dda207511bec138fdb9ea5 0000000000000000000000000000000000000000 D file3
+$
diff --git a/t/t4013/diff.diff-tree_-p_-m_master b/t/t4013/diff.diff-tree_-p_-m_master
new file mode 100644
index 0000000..b60bea0
--- /dev/null
+++ b/t/t4013/diff.diff-tree_-p_-m_master
@@ -0,0 +1,80 @@
+$ git diff-tree -p -m master
+59d314ad6f356dd08601a4cd5e530381da3e3c64
+diff --git a/dir/sub b/dir/sub
+index cead32e..992913c 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -4,3 +4,5 @@ C
+ D
+ E
+ F
++1
++2
+diff --git a/file0 b/file0
+index b414108..10a8a9f 100644
+--- a/file0
++++ b/file0
+@@ -4,3 +4,6 @@
+ 4
+ 5
+ 6
++A
++B
++C
+59d314ad6f356dd08601a4cd5e530381da3e3c64
+diff --git a/dir/sub b/dir/sub
+index 7289e35..992913c 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,4 +1,8 @@
+ A
+ B
++C
++D
++E
++F
+ 1
+ 2
+diff --git a/file0 b/file0
+index f4615da..10a8a9f 100644
+--- a/file0
++++ b/file0
+@@ -1,6 +1,9 @@
+ 1
+ 2
+ 3
++4
++5
++6
+ A
+ B
+ C
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
+diff --git a/file2 b/file2
+deleted file mode 100644
+index 01e79c3..0000000
+--- a/file2
++++ /dev/null
+@@ -1,3 +0,0 @@
+-1
+-2
+-3
+diff --git a/file3 b/file3
+deleted file mode 100644
+index 7289e35..0000000
+--- a/file3
++++ /dev/null
+@@ -1,4 +0,0 @@
+-A
+-B
+-1
+-2
+$
diff --git a/t/t4013/diff.diff-tree_-p_initial b/t/t4013/diff.diff-tree_-p_initial
new file mode 100644
index 0000000..e20ce88
--- /dev/null
+++ b/t/t4013/diff.diff-tree_-p_initial
@@ -0,0 +1,2 @@
+$ git diff-tree -p initial
+$
diff --git a/t/t4013/diff.diff-tree_-p_master b/t/t4013/diff.diff-tree_-p_master
new file mode 100644
index 0000000..b182875
--- /dev/null
+++ b/t/t4013/diff.diff-tree_-p_master
@@ -0,0 +1,2 @@
+$ git diff-tree -p master
+$
diff --git a/t/t4013/diff.diff-tree_-r_--abbrev=4_initial b/t/t4013/diff.diff-tree_-r_--abbrev=4_initial
new file mode 100644
index 0000000..c5a3aa5
--- /dev/null
+++ b/t/t4013/diff.diff-tree_-r_--abbrev=4_initial
@@ -0,0 +1,2 @@
+$ git diff-tree -r --abbrev=4 initial
+$
diff --git a/t/t4013/diff.diff-tree_-r_--abbrev_initial b/t/t4013/diff.diff-tree_-r_--abbrev_initial
new file mode 100644
index 0000000..0b689b7
--- /dev/null
+++ b/t/t4013/diff.diff-tree_-r_--abbrev_initial
@@ -0,0 +1,2 @@
+$ git diff-tree -r --abbrev initial
+$
diff --git a/t/t4013/diff.diff-tree_-r_initial b/t/t4013/diff.diff-tree_-r_initial
new file mode 100644
index 0000000..1765d83
--- /dev/null
+++ b/t/t4013/diff.diff-tree_-r_initial
@@ -0,0 +1,2 @@
+$ git diff-tree -r initial
+$
diff --git a/t/t4013/diff.diff-tree_initial b/t/t4013/diff.diff-tree_initial
new file mode 100644
index 0000000..b49fc53
--- /dev/null
+++ b/t/t4013/diff.diff-tree_initial
@@ -0,0 +1,2 @@
+$ git diff-tree initial
+$
diff --git a/t/t4013/diff.diff-tree_initial_mode b/t/t4013/diff.diff-tree_initial_mode
new file mode 100644
index 0000000..c47c094
--- /dev/null
+++ b/t/t4013/diff.diff-tree_initial_mode
@@ -0,0 +1,3 @@
+$ git diff-tree initial mode
+:100644 100755 01e79c32a8c99c557f0757da7cb6d65b3414466d 01e79c32a8c99c557f0757da7cb6d65b3414466d M file0
+$
diff --git a/t/t4013/diff.diff-tree_master b/t/t4013/diff.diff-tree_master
new file mode 100644
index 0000000..fe9226f
--- /dev/null
+++ b/t/t4013/diff.diff-tree_master
@@ -0,0 +1,2 @@
+$ git diff-tree master
+$
diff --git a/t/t4013/diff.diff_--abbrev_initial..side b/t/t4013/diff.diff_--abbrev_initial..side
new file mode 100644
index 0000000..a88e66f
--- /dev/null
+++ b/t/t4013/diff.diff_--abbrev_initial..side
@@ -0,0 +1,32 @@
+$ git diff --abbrev initial..side
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+$
diff --git a/t/t4013/diff.diff_--cached b/t/t4013/diff.diff_--cached
new file mode 100644
index 0000000..ff16e83
--- /dev/null
+++ b/t/t4013/diff.diff_--cached
@@ -0,0 +1,38 @@
+diff --git a/dir/sub b/dir/sub
+new file mode 100644
+index 0000000..992913c
+--- /dev/null
++++ b/dir/sub
+@@ -0,0 +1,8 @@
++A
++B
++C
++D
++E
++F
++1
++2
+diff --git a/file0 b/file0
+new file mode 100644
+index 0000000..10a8a9f
+--- /dev/null
++++ b/file0
+@@ -0,0 +1,9 @@
++1
++2
++3
++4
++5
++6
++A
++B
++C
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
diff --git a/t/t4013/diff.diff_--cached_--_file0 b/t/t4013/diff.diff_--cached_--_file0
new file mode 100644
index 0000000..b9bb858
--- /dev/null
+++ b/t/t4013/diff.diff_--cached_--_file0
@@ -0,0 +1,15 @@
+diff --git a/file0 b/file0
+new file mode 100644
+index 0000000..10a8a9f
+--- /dev/null
++++ b/file0
+@@ -0,0 +1,9 @@
++1
++2
++3
++4
++5
++6
++A
++B
++C
diff --git a/t/t4013/diff.diff_--dirstat-by-file_initial_rearrange b/t/t4013/diff.diff_--dirstat-by-file_initial_rearrange
new file mode 100644
index 0000000..e48e33f
--- /dev/null
+++ b/t/t4013/diff.diff_--dirstat-by-file_initial_rearrange
@@ -0,0 +1,3 @@
+$ git diff --dirstat-by-file initial rearrange
+ 100.0% dir/
+$
diff --git a/t/t4013/diff.diff_--dirstat_--cc_master~1_master b/t/t4013/diff.diff_--dirstat_--cc_master~1_master
new file mode 100644
index 0000000..fba4e34
--- /dev/null
+++ b/t/t4013/diff.diff_--dirstat_--cc_master~1_master
@@ -0,0 +1,3 @@
+$ git diff --dirstat --cc master~1 master
+ 40.0% dir/
+$
diff --git a/t/t4013/diff.diff_--dirstat_initial_rearrange b/t/t4013/diff.diff_--dirstat_initial_rearrange
new file mode 100644
index 0000000..5fb02c1
--- /dev/null
+++ b/t/t4013/diff.diff_--dirstat_initial_rearrange
@@ -0,0 +1,3 @@
+$ git diff --dirstat initial rearrange
+ 100.0% dir/
+$
diff --git a/t/t4013/diff.diff_--dirstat_master~1_master~2 b/t/t4013/diff.diff_--dirstat_master~1_master~2
new file mode 100644
index 0000000..b672e1c
--- /dev/null
+++ b/t/t4013/diff.diff_--dirstat_master~1_master~2
@@ -0,0 +1,3 @@
+$ git diff --dirstat master~1 master~2
+ 40.0% dir/
+$
diff --git a/t/t4013/diff.diff_--line-prefix=abc_master_master^_side b/t/t4013/diff.diff_--line-prefix=abc_master_master^_side
new file mode 100644
index 0000000..99f91e7
--- /dev/null
+++ b/t/t4013/diff.diff_--line-prefix=abc_master_master^_side
@@ -0,0 +1,29 @@
+$ git diff --line-prefix=abc master master^ side
+abcdiff --cc dir/sub
+abcindex cead32e,7289e35..992913c
+abc--- a/dir/sub
+abc+++ b/dir/sub
+abc@@@ -1,6 -1,4 +1,8 @@@
+abc A
+abc B
+abc +C
+abc +D
+abc +E
+abc +F
+abc+ 1
+abc+ 2
+abcdiff --cc file0
+abcindex b414108,f4615da..10a8a9f
+abc--- a/file0
+abc+++ b/file0
+abc@@@ -1,6 -1,6 +1,9 @@@
+abc 1
+abc 2
+abc 3
+abc +4
+abc +5
+abc +6
+abc+ A
+abc+ B
+abc+ C
+$
diff --git a/t/t4013/diff.diff_--line-prefix_--cached_--_file0 b/t/t4013/diff.diff_--line-prefix_--cached_--_file0
new file mode 100644
index 0000000..f41ba4d
--- /dev/null
+++ b/t/t4013/diff.diff_--line-prefix_--cached_--_file0
@@ -0,0 +1,15 @@
+| | | diff --git a/file0 b/file0
+| | | new file mode 100644
+| | | index 0000000..10a8a9f
+| | | --- /dev/null
+| | | +++ b/file0
+| | | @@ -0,0 +1,9 @@
+| | | +1
+| | | +2
+| | | +3
+| | | +4
+| | | +5
+| | | +6
+| | | +A
+| | | +B
+| | | +C
diff --git a/t/t4013/diff.diff_--name-status_dir2_dir b/t/t4013/diff.diff_--name-status_dir2_dir
new file mode 100644
index 0000000..d0d96aa
--- /dev/null
+++ b/t/t4013/diff.diff_--name-status_dir2_dir
@@ -0,0 +1,2 @@
+$ git diff --name-status dir2 dir
+$
diff --git a/t/t4013/diff.diff_--no-index_--name-status_--_dir2_dir b/t/t4013/diff.diff_--no-index_--name-status_--_dir2_dir
new file mode 100644
index 0000000..6756f8d
--- /dev/null
+++ b/t/t4013/diff.diff_--no-index_--name-status_--_dir2_dir
@@ -0,0 +1,3 @@
+$ git diff --no-index --name-status -- dir2 dir
+A dir/sub
+$
diff --git a/t/t4013/diff.diff_--no-index_--name-status_dir2_dir b/t/t4013/diff.diff_--no-index_--name-status_dir2_dir
new file mode 100644
index 0000000..6a47584
--- /dev/null
+++ b/t/t4013/diff.diff_--no-index_--name-status_dir2_dir
@@ -0,0 +1,3 @@
+$ git diff --no-index --name-status dir2 dir
+A dir/sub
+$
diff --git a/t/t4013/diff.diff_--no-index_--raw_--abbrev=4_dir2_dir b/t/t4013/diff.diff_--no-index_--raw_--abbrev=4_dir2_dir
new file mode 100644
index 0000000..a71b38a
--- /dev/null
+++ b/t/t4013/diff.diff_--no-index_--raw_--abbrev=4_dir2_dir
@@ -0,0 +1,3 @@
+$ git diff --no-index --raw --abbrev=4 dir2 dir
+:000000 100644 0000... 0000... A dir/sub
+$
diff --git a/t/t4013/diff.diff_--no-index_--raw_--no-abbrev_dir2_dir b/t/t4013/diff.diff_--no-index_--raw_--no-abbrev_dir2_dir
new file mode 100644
index 0000000..e0f0097
--- /dev/null
+++ b/t/t4013/diff.diff_--no-index_--raw_--no-abbrev_dir2_dir
@@ -0,0 +1,3 @@
+$ git diff --no-index --raw --no-abbrev dir2 dir
+:000000 100644 0000000000000000000000000000000000000000 0000000000000000000000000000000000000000 A dir/sub
+$
diff --git a/t/t4013/diff.diff_--no-index_--raw_dir2_dir b/t/t4013/diff.diff_--no-index_--raw_dir2_dir
new file mode 100644
index 0000000..3cb4ee7
--- /dev/null
+++ b/t/t4013/diff.diff_--no-index_--raw_dir2_dir
@@ -0,0 +1,3 @@
+$ git diff --no-index --raw dir2 dir
+:000000 100644 0000000... 0000000... A dir/sub
+$
diff --git a/t/t4013/diff.diff_--no-index_dir_dir3 b/t/t4013/diff.diff_--no-index_dir_dir3
new file mode 100644
index 0000000..2142c2b
--- /dev/null
+++ b/t/t4013/diff.diff_--no-index_dir_dir3
@@ -0,0 +1,2 @@
+$ git diff --no-index dir dir3
+$
diff --git a/t/t4013/diff.diff_--patch-with-raw_-r_initial..side b/t/t4013/diff.diff_--patch-with-raw_-r_initial..side
new file mode 100644
index 0000000..3590dc7
--- /dev/null
+++ b/t/t4013/diff.diff_--patch-with-raw_-r_initial..side
@@ -0,0 +1,36 @@
+$ git diff --patch-with-raw -r initial..side
+:100644 100644 35d242b... 7289e35... M dir/sub
+:100644 100644 01e79c3... f4615da... M file0
+:000000 100644 0000000... 7289e35... A file3
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+$
diff --git a/t/t4013/diff.diff_--patch-with-raw_initial..side b/t/t4013/diff.diff_--patch-with-raw_initial..side
new file mode 100644
index 0000000..b21d5dc
--- /dev/null
+++ b/t/t4013/diff.diff_--patch-with-raw_initial..side
@@ -0,0 +1,36 @@
+$ git diff --patch-with-raw initial..side
+:100644 100644 35d242b... 7289e35... M dir/sub
+:100644 100644 01e79c3... f4615da... M file0
+:000000 100644 0000000... 7289e35... A file3
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+$
diff --git a/t/t4013/diff.diff_--patch-with-stat_-r_initial..side b/t/t4013/diff.diff_--patch-with-stat_-r_initial..side
new file mode 100644
index 0000000..be8d1ea
--- /dev/null
+++ b/t/t4013/diff.diff_--patch-with-stat_-r_initial..side
@@ -0,0 +1,37 @@
+$ git diff --patch-with-stat -r initial..side
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file3 | 4 ++++
+ 3 files changed, 9 insertions(+)
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+$
diff --git a/t/t4013/diff.diff_--patch-with-stat_initial..side b/t/t4013/diff.diff_--patch-with-stat_initial..side
new file mode 100644
index 0000000..5424e6d
--- /dev/null
+++ b/t/t4013/diff.diff_--patch-with-stat_initial..side
@@ -0,0 +1,37 @@
+$ git diff --patch-with-stat initial..side
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file3 | 4 ++++
+ 3 files changed, 9 insertions(+)
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+$
diff --git a/t/t4013/diff.diff_--raw_--abbrev=4_initial b/t/t4013/diff.diff_--raw_--abbrev=4_initial
new file mode 100644
index 0000000..c3641db
--- /dev/null
+++ b/t/t4013/diff.diff_--raw_--abbrev=4_initial
@@ -0,0 +1,6 @@
+$ git diff --raw --abbrev=4 initial
+:100644 100644 35d2... 9929... M dir/sub
+:100644 100644 01e7... 10a8... M file0
+:000000 100644 0000... b1e6... A file1
+:100644 000000 01e7... 0000... D file2
+$
diff --git a/t/t4013/diff.diff_--raw_--no-abbrev_initial b/t/t4013/diff.diff_--raw_--no-abbrev_initial
new file mode 100644
index 0000000..c87a125
--- /dev/null
+++ b/t/t4013/diff.diff_--raw_--no-abbrev_initial
@@ -0,0 +1,6 @@
+$ git diff --raw --no-abbrev initial
+:100644 100644 35d242ba79ae89ac695e26b3d4c27a8e6f028f9e 992913c5aa0a5476d10c49ed0f21fc0c6d1aedf3 M dir/sub
+:100644 100644 01e79c32a8c99c557f0757da7cb6d65b3414466d 10a8a9f3657f91a156b9f0184ed79a20adef9f7f M file0
+:000000 100644 0000000000000000000000000000000000000000 b1e67221afe8461efd244b487afca22d46b95eb8 A file1
+:100644 000000 01e79c32a8c99c557f0757da7cb6d65b3414466d 0000000000000000000000000000000000000000 D file2
+$
diff --git a/t/t4013/diff.diff_--raw_initial b/t/t4013/diff.diff_--raw_initial
new file mode 100644
index 0000000..a3e9780
--- /dev/null
+++ b/t/t4013/diff.diff_--raw_initial
@@ -0,0 +1,6 @@
+$ git diff --raw initial
+:100644 100644 35d242b... 992913c... M dir/sub
+:100644 100644 01e79c3... 10a8a9f... M file0
+:000000 100644 0000000... b1e6722... A file1
+:100644 000000 01e79c3... 0000000... D file2
+$
diff --git a/t/t4013/diff.diff_--stat_initial..side b/t/t4013/diff.diff_--stat_initial..side
new file mode 100644
index 0000000..b7741e2
--- /dev/null
+++ b/t/t4013/diff.diff_--stat_initial..side
@@ -0,0 +1,6 @@
+$ git diff --stat initial..side
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file3 | 4 ++++
+ 3 files changed, 9 insertions(+)
+$
diff --git a/t/t4013/diff.diff_-U1_initial..side b/t/t4013/diff.diff_-U1_initial..side
new file mode 100644
index 0000000..b69f8f0
--- /dev/null
+++ b/t/t4013/diff.diff_-U1_initial..side
@@ -0,0 +1,29 @@
+$ git diff -U1 initial..side
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2 +2,3 @@ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -3 +3,4 @@
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+$
diff --git a/t/t4013/diff.diff_-U2_initial..side b/t/t4013/diff.diff_-U2_initial..side
new file mode 100644
index 0000000..8ffe04f
--- /dev/null
+++ b/t/t4013/diff.diff_-U2_initial..side
@@ -0,0 +1,31 @@
+$ git diff -U2 initial..side
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -2,2 +2,5 @@
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+$
diff --git a/t/t4013/diff.diff_-U_initial..side b/t/t4013/diff.diff_-U_initial..side
new file mode 100644
index 0000000..c66c0dd
--- /dev/null
+++ b/t/t4013/diff.diff_-U_initial..side
@@ -0,0 +1,32 @@
+$ git diff -U initial..side
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+$
diff --git a/t/t4013/diff.diff_-r_--stat_initial..side b/t/t4013/diff.diff_-r_--stat_initial..side
new file mode 100644
index 0000000..5d514f5
--- /dev/null
+++ b/t/t4013/diff.diff_-r_--stat_initial..side
@@ -0,0 +1,6 @@
+$ git diff -r --stat initial..side
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file3 | 4 ++++
+ 3 files changed, 9 insertions(+)
+$
diff --git a/t/t4013/diff.diff_-r_initial..side b/t/t4013/diff.diff_-r_initial..side
new file mode 100644
index 0000000..5bb2fe2
--- /dev/null
+++ b/t/t4013/diff.diff_-r_initial..side
@@ -0,0 +1,32 @@
+$ git diff -r initial..side
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+$
diff --git a/t/t4013/diff.diff_initial..side b/t/t4013/diff.diff_initial..side
new file mode 100644
index 0000000..c8adaf5
--- /dev/null
+++ b/t/t4013/diff.diff_initial..side
@@ -0,0 +1,32 @@
+$ git diff initial..side
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+$
diff --git a/t/t4013/diff.diff_master_master^_side b/t/t4013/diff.diff_master_master^_side
new file mode 100644
index 0000000..50ec9ca
--- /dev/null
+++ b/t/t4013/diff.diff_master_master^_side
@@ -0,0 +1,29 @@
+$ git diff master master^ side
+diff --cc dir/sub
+index cead32e,7289e35..992913c
+--- a/dir/sub
++++ b/dir/sub
+@@@ -1,6 -1,4 +1,8 @@@
+ A
+ B
+ +C
+ +D
+ +E
+ +F
++ 1
++ 2
+diff --cc file0
+index b414108,f4615da..10a8a9f
+--- a/file0
++++ b/file0
+@@@ -1,6 -1,6 +1,9 @@@
+ 1
+ 2
+ 3
+ +4
+ +5
+ +6
++ A
++ B
++ C
+$
diff --git a/t/t4013/diff.format-patch_--attach_--stdout_--suffix=.diff_initial..side b/t/t4013/diff.format-patch_--attach_--stdout_--suffix=.diff_initial..side
new file mode 100644
index 0000000..547ca06
--- /dev/null
+++ b/t/t4013/diff.format-patch_--attach_--stdout_--suffix=.diff_initial..side
@@ -0,0 +1,61 @@
+$ git format-patch --attach --stdout --suffix=.diff initial..side
+From c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a Mon Sep 17 00:00:00 2001
+From: A U Thor <author@example.com>
+Date: Mon, 26 Jun 2006 00:03:00 +0000
+Subject: [PATCH] Side
+MIME-Version: 1.0
+Content-Type: multipart/mixed; boundary="------------g-i-t--v-e-r-s-i-o-n"
+
+This is a multi-part message in MIME format.
+--------------g-i-t--v-e-r-s-i-o-n
+Content-Type: text/plain; charset=UTF-8; format=fixed
+Content-Transfer-Encoding: 8bit
+
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file3 | 4 ++++
+ 3 files changed, 9 insertions(+)
+ create mode 100644 file3
+
+
+--------------g-i-t--v-e-r-s-i-o-n
+Content-Type: text/x-patch; name="0001-Side.diff"
+Content-Transfer-Encoding: 8bit
+Content-Disposition: attachment; filename="0001-Side.diff"
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+
+--------------g-i-t--v-e-r-s-i-o-n--
+
+
+$
diff --git a/t/t4013/diff.format-patch_--attach_--stdout_initial..master b/t/t4013/diff.format-patch_--attach_--stdout_initial..master
new file mode 100644
index 0000000..52fedc1
--- /dev/null
+++ b/t/t4013/diff.format-patch_--attach_--stdout_initial..master
@@ -0,0 +1,170 @@
+$ git format-patch --attach --stdout initial..master
+From 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44 Mon Sep 17 00:00:00 2001
+From: A U Thor <author@example.com>
+Date: Mon, 26 Jun 2006 00:01:00 +0000
+Subject: [PATCH 1/3] Second
+MIME-Version: 1.0
+Content-Type: multipart/mixed; boundary="------------g-i-t--v-e-r-s-i-o-n"
+
+This is a multi-part message in MIME format.
+--------------g-i-t--v-e-r-s-i-o-n
+Content-Type: text/plain; charset=UTF-8; format=fixed
+Content-Transfer-Encoding: 8bit
+
+
+This is the second commit.
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file2 | 3 ---
+ 3 files changed, 5 insertions(+), 3 deletions(-)
+ delete mode 100644 file2
+
+
+--------------g-i-t--v-e-r-s-i-o-n
+Content-Type: text/x-patch; name="0001-Second.patch"
+Content-Transfer-Encoding: 8bit
+Content-Disposition: attachment; filename="0001-Second.patch"
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..8422d40 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++C
++D
+diff --git a/file0 b/file0
+index 01e79c3..b414108 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++4
++5
++6
+diff --git a/file2 b/file2
+deleted file mode 100644
+index 01e79c3..0000000
+--- a/file2
++++ /dev/null
+@@ -1,3 +0,0 @@
+-1
+-2
+-3
+
+--------------g-i-t--v-e-r-s-i-o-n--
+
+
+
+From 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0 Mon Sep 17 00:00:00 2001
+From: A U Thor <author@example.com>
+Date: Mon, 26 Jun 2006 00:02:00 +0000
+Subject: [PATCH 2/3] Third
+MIME-Version: 1.0
+Content-Type: multipart/mixed; boundary="------------g-i-t--v-e-r-s-i-o-n"
+
+This is a multi-part message in MIME format.
+--------------g-i-t--v-e-r-s-i-o-n
+Content-Type: text/plain; charset=UTF-8; format=fixed
+Content-Transfer-Encoding: 8bit
+
+---
+ dir/sub | 2 ++
+ file1 | 3 +++
+ 2 files changed, 5 insertions(+)
+ create mode 100644 file1
+
+
+--------------g-i-t--v-e-r-s-i-o-n
+Content-Type: text/x-patch; name="0002-Third.patch"
+Content-Transfer-Encoding: 8bit
+Content-Disposition: attachment; filename="0002-Third.patch"
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
+
+--------------g-i-t--v-e-r-s-i-o-n--
+
+
+
+From c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a Mon Sep 17 00:00:00 2001
+From: A U Thor <author@example.com>
+Date: Mon, 26 Jun 2006 00:03:00 +0000
+Subject: [PATCH 3/3] Side
+MIME-Version: 1.0
+Content-Type: multipart/mixed; boundary="------------g-i-t--v-e-r-s-i-o-n"
+
+This is a multi-part message in MIME format.
+--------------g-i-t--v-e-r-s-i-o-n
+Content-Type: text/plain; charset=UTF-8; format=fixed
+Content-Transfer-Encoding: 8bit
+
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file3 | 4 ++++
+ 3 files changed, 9 insertions(+)
+ create mode 100644 file3
+
+
+--------------g-i-t--v-e-r-s-i-o-n
+Content-Type: text/x-patch; name="0003-Side.patch"
+Content-Transfer-Encoding: 8bit
+Content-Disposition: attachment; filename="0003-Side.patch"
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+
+--------------g-i-t--v-e-r-s-i-o-n--
+
+
+$
diff --git a/t/t4013/diff.format-patch_--attach_--stdout_initial..master^ b/t/t4013/diff.format-patch_--attach_--stdout_initial..master^
new file mode 100644
index 0000000..1c3cde2
--- /dev/null
+++ b/t/t4013/diff.format-patch_--attach_--stdout_initial..master^
@@ -0,0 +1,110 @@
+$ git format-patch --attach --stdout initial..master^
+From 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44 Mon Sep 17 00:00:00 2001
+From: A U Thor <author@example.com>
+Date: Mon, 26 Jun 2006 00:01:00 +0000
+Subject: [PATCH 1/2] Second
+MIME-Version: 1.0
+Content-Type: multipart/mixed; boundary="------------g-i-t--v-e-r-s-i-o-n"
+
+This is a multi-part message in MIME format.
+--------------g-i-t--v-e-r-s-i-o-n
+Content-Type: text/plain; charset=UTF-8; format=fixed
+Content-Transfer-Encoding: 8bit
+
+
+This is the second commit.
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file2 | 3 ---
+ 3 files changed, 5 insertions(+), 3 deletions(-)
+ delete mode 100644 file2
+
+
+--------------g-i-t--v-e-r-s-i-o-n
+Content-Type: text/x-patch; name="0001-Second.patch"
+Content-Transfer-Encoding: 8bit
+Content-Disposition: attachment; filename="0001-Second.patch"
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..8422d40 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++C
++D
+diff --git a/file0 b/file0
+index 01e79c3..b414108 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++4
++5
++6
+diff --git a/file2 b/file2
+deleted file mode 100644
+index 01e79c3..0000000
+--- a/file2
++++ /dev/null
+@@ -1,3 +0,0 @@
+-1
+-2
+-3
+
+--------------g-i-t--v-e-r-s-i-o-n--
+
+
+
+From 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0 Mon Sep 17 00:00:00 2001
+From: A U Thor <author@example.com>
+Date: Mon, 26 Jun 2006 00:02:00 +0000
+Subject: [PATCH 2/2] Third
+MIME-Version: 1.0
+Content-Type: multipart/mixed; boundary="------------g-i-t--v-e-r-s-i-o-n"
+
+This is a multi-part message in MIME format.
+--------------g-i-t--v-e-r-s-i-o-n
+Content-Type: text/plain; charset=UTF-8; format=fixed
+Content-Transfer-Encoding: 8bit
+
+---
+ dir/sub | 2 ++
+ file1 | 3 +++
+ 2 files changed, 5 insertions(+)
+ create mode 100644 file1
+
+
+--------------g-i-t--v-e-r-s-i-o-n
+Content-Type: text/x-patch; name="0002-Third.patch"
+Content-Transfer-Encoding: 8bit
+Content-Disposition: attachment; filename="0002-Third.patch"
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
+
+--------------g-i-t--v-e-r-s-i-o-n--
+
+
+$
diff --git a/t/t4013/diff.format-patch_--attach_--stdout_initial..side b/t/t4013/diff.format-patch_--attach_--stdout_initial..side
new file mode 100644
index 0000000..4717bd8
--- /dev/null
+++ b/t/t4013/diff.format-patch_--attach_--stdout_initial..side
@@ -0,0 +1,61 @@
+$ git format-patch --attach --stdout initial..side
+From c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a Mon Sep 17 00:00:00 2001
+From: A U Thor <author@example.com>
+Date: Mon, 26 Jun 2006 00:03:00 +0000
+Subject: [PATCH] Side
+MIME-Version: 1.0
+Content-Type: multipart/mixed; boundary="------------g-i-t--v-e-r-s-i-o-n"
+
+This is a multi-part message in MIME format.
+--------------g-i-t--v-e-r-s-i-o-n
+Content-Type: text/plain; charset=UTF-8; format=fixed
+Content-Transfer-Encoding: 8bit
+
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file3 | 4 ++++
+ 3 files changed, 9 insertions(+)
+ create mode 100644 file3
+
+
+--------------g-i-t--v-e-r-s-i-o-n
+Content-Type: text/x-patch; name="0001-Side.patch"
+Content-Transfer-Encoding: 8bit
+Content-Disposition: attachment; filename="0001-Side.patch"
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+
+--------------g-i-t--v-e-r-s-i-o-n--
+
+
+$
diff --git a/t/t4013/diff.format-patch_--inline_--stdout_--numbered-files_initial..master b/t/t4013/diff.format-patch_--inline_--stdout_--numbered-files_initial..master
new file mode 100644
index 0000000..02c4db7
--- /dev/null
+++ b/t/t4013/diff.format-patch_--inline_--stdout_--numbered-files_initial..master
@@ -0,0 +1,170 @@
+$ git format-patch --inline --stdout --numbered-files initial..master
+From 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44 Mon Sep 17 00:00:00 2001
+From: A U Thor <author@example.com>
+Date: Mon, 26 Jun 2006 00:01:00 +0000
+Subject: [PATCH 1/3] Second
+MIME-Version: 1.0
+Content-Type: multipart/mixed; boundary="------------g-i-t--v-e-r-s-i-o-n"
+
+This is a multi-part message in MIME format.
+--------------g-i-t--v-e-r-s-i-o-n
+Content-Type: text/plain; charset=UTF-8; format=fixed
+Content-Transfer-Encoding: 8bit
+
+
+This is the second commit.
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file2 | 3 ---
+ 3 files changed, 5 insertions(+), 3 deletions(-)
+ delete mode 100644 file2
+
+
+--------------g-i-t--v-e-r-s-i-o-n
+Content-Type: text/x-patch; name="1"
+Content-Transfer-Encoding: 8bit
+Content-Disposition: inline; filename="1"
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..8422d40 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++C
++D
+diff --git a/file0 b/file0
+index 01e79c3..b414108 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++4
++5
++6
+diff --git a/file2 b/file2
+deleted file mode 100644
+index 01e79c3..0000000
+--- a/file2
++++ /dev/null
+@@ -1,3 +0,0 @@
+-1
+-2
+-3
+
+--------------g-i-t--v-e-r-s-i-o-n--
+
+
+
+From 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0 Mon Sep 17 00:00:00 2001
+From: A U Thor <author@example.com>
+Date: Mon, 26 Jun 2006 00:02:00 +0000
+Subject: [PATCH 2/3] Third
+MIME-Version: 1.0
+Content-Type: multipart/mixed; boundary="------------g-i-t--v-e-r-s-i-o-n"
+
+This is a multi-part message in MIME format.
+--------------g-i-t--v-e-r-s-i-o-n
+Content-Type: text/plain; charset=UTF-8; format=fixed
+Content-Transfer-Encoding: 8bit
+
+---
+ dir/sub | 2 ++
+ file1 | 3 +++
+ 2 files changed, 5 insertions(+)
+ create mode 100644 file1
+
+
+--------------g-i-t--v-e-r-s-i-o-n
+Content-Type: text/x-patch; name="2"
+Content-Transfer-Encoding: 8bit
+Content-Disposition: inline; filename="2"
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
+
+--------------g-i-t--v-e-r-s-i-o-n--
+
+
+
+From c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a Mon Sep 17 00:00:00 2001
+From: A U Thor <author@example.com>
+Date: Mon, 26 Jun 2006 00:03:00 +0000
+Subject: [PATCH 3/3] Side
+MIME-Version: 1.0
+Content-Type: multipart/mixed; boundary="------------g-i-t--v-e-r-s-i-o-n"
+
+This is a multi-part message in MIME format.
+--------------g-i-t--v-e-r-s-i-o-n
+Content-Type: text/plain; charset=UTF-8; format=fixed
+Content-Transfer-Encoding: 8bit
+
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file3 | 4 ++++
+ 3 files changed, 9 insertions(+)
+ create mode 100644 file3
+
+
+--------------g-i-t--v-e-r-s-i-o-n
+Content-Type: text/x-patch; name="3"
+Content-Transfer-Encoding: 8bit
+Content-Disposition: inline; filename="3"
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+
+--------------g-i-t--v-e-r-s-i-o-n--
+
+
+$
diff --git a/t/t4013/diff.format-patch_--inline_--stdout_--subject-prefix=TESTCASE_initial..master b/t/t4013/diff.format-patch_--inline_--stdout_--subject-prefix=TESTCASE_initial..master
new file mode 100644
index 0000000..c7677c5
--- /dev/null
+++ b/t/t4013/diff.format-patch_--inline_--stdout_--subject-prefix=TESTCASE_initial..master
@@ -0,0 +1,170 @@
+$ git format-patch --inline --stdout --subject-prefix=TESTCASE initial..master
+From 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44 Mon Sep 17 00:00:00 2001
+From: A U Thor <author@example.com>
+Date: Mon, 26 Jun 2006 00:01:00 +0000
+Subject: [TESTCASE 1/3] Second
+MIME-Version: 1.0
+Content-Type: multipart/mixed; boundary="------------g-i-t--v-e-r-s-i-o-n"
+
+This is a multi-part message in MIME format.
+--------------g-i-t--v-e-r-s-i-o-n
+Content-Type: text/plain; charset=UTF-8; format=fixed
+Content-Transfer-Encoding: 8bit
+
+
+This is the second commit.
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file2 | 3 ---
+ 3 files changed, 5 insertions(+), 3 deletions(-)
+ delete mode 100644 file2
+
+
+--------------g-i-t--v-e-r-s-i-o-n
+Content-Type: text/x-patch; name="0001-Second.patch"
+Content-Transfer-Encoding: 8bit
+Content-Disposition: inline; filename="0001-Second.patch"
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..8422d40 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++C
++D
+diff --git a/file0 b/file0
+index 01e79c3..b414108 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++4
++5
++6
+diff --git a/file2 b/file2
+deleted file mode 100644
+index 01e79c3..0000000
+--- a/file2
++++ /dev/null
+@@ -1,3 +0,0 @@
+-1
+-2
+-3
+
+--------------g-i-t--v-e-r-s-i-o-n--
+
+
+
+From 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0 Mon Sep 17 00:00:00 2001
+From: A U Thor <author@example.com>
+Date: Mon, 26 Jun 2006 00:02:00 +0000
+Subject: [TESTCASE 2/3] Third
+MIME-Version: 1.0
+Content-Type: multipart/mixed; boundary="------------g-i-t--v-e-r-s-i-o-n"
+
+This is a multi-part message in MIME format.
+--------------g-i-t--v-e-r-s-i-o-n
+Content-Type: text/plain; charset=UTF-8; format=fixed
+Content-Transfer-Encoding: 8bit
+
+---
+ dir/sub | 2 ++
+ file1 | 3 +++
+ 2 files changed, 5 insertions(+)
+ create mode 100644 file1
+
+
+--------------g-i-t--v-e-r-s-i-o-n
+Content-Type: text/x-patch; name="0002-Third.patch"
+Content-Transfer-Encoding: 8bit
+Content-Disposition: inline; filename="0002-Third.patch"
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
+
+--------------g-i-t--v-e-r-s-i-o-n--
+
+
+
+From c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a Mon Sep 17 00:00:00 2001
+From: A U Thor <author@example.com>
+Date: Mon, 26 Jun 2006 00:03:00 +0000
+Subject: [TESTCASE 3/3] Side
+MIME-Version: 1.0
+Content-Type: multipart/mixed; boundary="------------g-i-t--v-e-r-s-i-o-n"
+
+This is a multi-part message in MIME format.
+--------------g-i-t--v-e-r-s-i-o-n
+Content-Type: text/plain; charset=UTF-8; format=fixed
+Content-Transfer-Encoding: 8bit
+
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file3 | 4 ++++
+ 3 files changed, 9 insertions(+)
+ create mode 100644 file3
+
+
+--------------g-i-t--v-e-r-s-i-o-n
+Content-Type: text/x-patch; name="0003-Side.patch"
+Content-Transfer-Encoding: 8bit
+Content-Disposition: inline; filename="0003-Side.patch"
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+
+--------------g-i-t--v-e-r-s-i-o-n--
+
+
+$
diff --git a/t/t4013/diff.format-patch_--inline_--stdout_initial..master b/t/t4013/diff.format-patch_--inline_--stdout_initial..master
new file mode 100644
index 0000000..5b3e34e
--- /dev/null
+++ b/t/t4013/diff.format-patch_--inline_--stdout_initial..master
@@ -0,0 +1,170 @@
+$ git format-patch --inline --stdout initial..master
+From 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44 Mon Sep 17 00:00:00 2001
+From: A U Thor <author@example.com>
+Date: Mon, 26 Jun 2006 00:01:00 +0000
+Subject: [PATCH 1/3] Second
+MIME-Version: 1.0
+Content-Type: multipart/mixed; boundary="------------g-i-t--v-e-r-s-i-o-n"
+
+This is a multi-part message in MIME format.
+--------------g-i-t--v-e-r-s-i-o-n
+Content-Type: text/plain; charset=UTF-8; format=fixed
+Content-Transfer-Encoding: 8bit
+
+
+This is the second commit.
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file2 | 3 ---
+ 3 files changed, 5 insertions(+), 3 deletions(-)
+ delete mode 100644 file2
+
+
+--------------g-i-t--v-e-r-s-i-o-n
+Content-Type: text/x-patch; name="0001-Second.patch"
+Content-Transfer-Encoding: 8bit
+Content-Disposition: inline; filename="0001-Second.patch"
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..8422d40 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++C
++D
+diff --git a/file0 b/file0
+index 01e79c3..b414108 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++4
++5
++6
+diff --git a/file2 b/file2
+deleted file mode 100644
+index 01e79c3..0000000
+--- a/file2
++++ /dev/null
+@@ -1,3 +0,0 @@
+-1
+-2
+-3
+
+--------------g-i-t--v-e-r-s-i-o-n--
+
+
+
+From 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0 Mon Sep 17 00:00:00 2001
+From: A U Thor <author@example.com>
+Date: Mon, 26 Jun 2006 00:02:00 +0000
+Subject: [PATCH 2/3] Third
+MIME-Version: 1.0
+Content-Type: multipart/mixed; boundary="------------g-i-t--v-e-r-s-i-o-n"
+
+This is a multi-part message in MIME format.
+--------------g-i-t--v-e-r-s-i-o-n
+Content-Type: text/plain; charset=UTF-8; format=fixed
+Content-Transfer-Encoding: 8bit
+
+---
+ dir/sub | 2 ++
+ file1 | 3 +++
+ 2 files changed, 5 insertions(+)
+ create mode 100644 file1
+
+
+--------------g-i-t--v-e-r-s-i-o-n
+Content-Type: text/x-patch; name="0002-Third.patch"
+Content-Transfer-Encoding: 8bit
+Content-Disposition: inline; filename="0002-Third.patch"
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
+
+--------------g-i-t--v-e-r-s-i-o-n--
+
+
+
+From c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a Mon Sep 17 00:00:00 2001
+From: A U Thor <author@example.com>
+Date: Mon, 26 Jun 2006 00:03:00 +0000
+Subject: [PATCH 3/3] Side
+MIME-Version: 1.0
+Content-Type: multipart/mixed; boundary="------------g-i-t--v-e-r-s-i-o-n"
+
+This is a multi-part message in MIME format.
+--------------g-i-t--v-e-r-s-i-o-n
+Content-Type: text/plain; charset=UTF-8; format=fixed
+Content-Transfer-Encoding: 8bit
+
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file3 | 4 ++++
+ 3 files changed, 9 insertions(+)
+ create mode 100644 file3
+
+
+--------------g-i-t--v-e-r-s-i-o-n
+Content-Type: text/x-patch; name="0003-Side.patch"
+Content-Transfer-Encoding: 8bit
+Content-Disposition: inline; filename="0003-Side.patch"
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+
+--------------g-i-t--v-e-r-s-i-o-n--
+
+
+$
diff --git a/t/t4013/diff.format-patch_--inline_--stdout_initial..master^ b/t/t4013/diff.format-patch_--inline_--stdout_initial..master^
new file mode 100644
index 0000000..d13f8a8
--- /dev/null
+++ b/t/t4013/diff.format-patch_--inline_--stdout_initial..master^
@@ -0,0 +1,110 @@
+$ git format-patch --inline --stdout initial..master^
+From 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44 Mon Sep 17 00:00:00 2001
+From: A U Thor <author@example.com>
+Date: Mon, 26 Jun 2006 00:01:00 +0000
+Subject: [PATCH 1/2] Second
+MIME-Version: 1.0
+Content-Type: multipart/mixed; boundary="------------g-i-t--v-e-r-s-i-o-n"
+
+This is a multi-part message in MIME format.
+--------------g-i-t--v-e-r-s-i-o-n
+Content-Type: text/plain; charset=UTF-8; format=fixed
+Content-Transfer-Encoding: 8bit
+
+
+This is the second commit.
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file2 | 3 ---
+ 3 files changed, 5 insertions(+), 3 deletions(-)
+ delete mode 100644 file2
+
+
+--------------g-i-t--v-e-r-s-i-o-n
+Content-Type: text/x-patch; name="0001-Second.patch"
+Content-Transfer-Encoding: 8bit
+Content-Disposition: inline; filename="0001-Second.patch"
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..8422d40 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++C
++D
+diff --git a/file0 b/file0
+index 01e79c3..b414108 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++4
++5
++6
+diff --git a/file2 b/file2
+deleted file mode 100644
+index 01e79c3..0000000
+--- a/file2
++++ /dev/null
+@@ -1,3 +0,0 @@
+-1
+-2
+-3
+
+--------------g-i-t--v-e-r-s-i-o-n--
+
+
+
+From 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0 Mon Sep 17 00:00:00 2001
+From: A U Thor <author@example.com>
+Date: Mon, 26 Jun 2006 00:02:00 +0000
+Subject: [PATCH 2/2] Third
+MIME-Version: 1.0
+Content-Type: multipart/mixed; boundary="------------g-i-t--v-e-r-s-i-o-n"
+
+This is a multi-part message in MIME format.
+--------------g-i-t--v-e-r-s-i-o-n
+Content-Type: text/plain; charset=UTF-8; format=fixed
+Content-Transfer-Encoding: 8bit
+
+---
+ dir/sub | 2 ++
+ file1 | 3 +++
+ 2 files changed, 5 insertions(+)
+ create mode 100644 file1
+
+
+--------------g-i-t--v-e-r-s-i-o-n
+Content-Type: text/x-patch; name="0002-Third.patch"
+Content-Transfer-Encoding: 8bit
+Content-Disposition: inline; filename="0002-Third.patch"
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
+
+--------------g-i-t--v-e-r-s-i-o-n--
+
+
+$
diff --git a/t/t4013/diff.format-patch_--inline_--stdout_initial..master^^ b/t/t4013/diff.format-patch_--inline_--stdout_initial..master^^
new file mode 100644
index 0000000..caec553
--- /dev/null
+++ b/t/t4013/diff.format-patch_--inline_--stdout_initial..master^^
@@ -0,0 +1,62 @@
+$ git format-patch --inline --stdout initial..master^^
+From 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44 Mon Sep 17 00:00:00 2001
+From: A U Thor <author@example.com>
+Date: Mon, 26 Jun 2006 00:01:00 +0000
+Subject: [DIFFERENT_PREFIX] Second
+MIME-Version: 1.0
+Content-Type: multipart/mixed; boundary="------------g-i-t--v-e-r-s-i-o-n"
+
+This is a multi-part message in MIME format.
+--------------g-i-t--v-e-r-s-i-o-n
+Content-Type: text/plain; charset=UTF-8; format=fixed
+Content-Transfer-Encoding: 8bit
+
+
+This is the second commit.
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file2 | 3 ---
+ 3 files changed, 5 insertions(+), 3 deletions(-)
+ delete mode 100644 file2
+
+
+--------------g-i-t--v-e-r-s-i-o-n
+Content-Type: text/x-patch; name="0001-Second.patch"
+Content-Transfer-Encoding: 8bit
+Content-Disposition: inline; filename="0001-Second.patch"
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..8422d40 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++C
++D
+diff --git a/file0 b/file0
+index 01e79c3..b414108 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++4
++5
++6
+diff --git a/file2 b/file2
+deleted file mode 100644
+index 01e79c3..0000000
+--- a/file2
++++ /dev/null
+@@ -1,3 +0,0 @@
+-1
+-2
+-3
+
+--------------g-i-t--v-e-r-s-i-o-n--
+
+
+$
diff --git a/t/t4013/diff.format-patch_--inline_--stdout_initial..side b/t/t4013/diff.format-patch_--inline_--stdout_initial..side
new file mode 100644
index 0000000..d3a6762
--- /dev/null
+++ b/t/t4013/diff.format-patch_--inline_--stdout_initial..side
@@ -0,0 +1,61 @@
+$ git format-patch --inline --stdout initial..side
+From c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a Mon Sep 17 00:00:00 2001
+From: A U Thor <author@example.com>
+Date: Mon, 26 Jun 2006 00:03:00 +0000
+Subject: [PATCH] Side
+MIME-Version: 1.0
+Content-Type: multipart/mixed; boundary="------------g-i-t--v-e-r-s-i-o-n"
+
+This is a multi-part message in MIME format.
+--------------g-i-t--v-e-r-s-i-o-n
+Content-Type: text/plain; charset=UTF-8; format=fixed
+Content-Transfer-Encoding: 8bit
+
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file3 | 4 ++++
+ 3 files changed, 9 insertions(+)
+ create mode 100644 file3
+
+
+--------------g-i-t--v-e-r-s-i-o-n
+Content-Type: text/x-patch; name="0001-Side.patch"
+Content-Transfer-Encoding: 8bit
+Content-Disposition: inline; filename="0001-Side.patch"
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+
+--------------g-i-t--v-e-r-s-i-o-n--
+
+
+$
diff --git a/t/t4013/diff.format-patch_--stdout_--cover-letter_-n_initial..master^ b/t/t4013/diff.format-patch_--stdout_--cover-letter_-n_initial..master^
new file mode 100644
index 0000000..244d964
--- /dev/null
+++ b/t/t4013/diff.format-patch_--stdout_--cover-letter_-n_initial..master^
@@ -0,0 +1,103 @@
+$ git format-patch --stdout --cover-letter -n initial..master^
+From 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0 Mon Sep 17 00:00:00 2001
+From: C O Mitter <committer@example.com>
+Date: Mon, 26 Jun 2006 00:06:00 +0000
+Subject: [DIFFERENT_PREFIX 0/2] *** SUBJECT HERE ***
+
+*** BLURB HERE ***
+
+A U Thor (2):
+ Second
+ Third
+
+ dir/sub | 4 ++++
+ file0 | 3 +++
+ file1 | 3 +++
+ file2 | 3 ---
+ 4 files changed, 10 insertions(+), 3 deletions(-)
+ create mode 100644 file1
+ delete mode 100644 file2
+
+--
+g-i-t--v-e-r-s-i-o-n
+
+From 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44 Mon Sep 17 00:00:00 2001
+From: A U Thor <author@example.com>
+Date: Mon, 26 Jun 2006 00:01:00 +0000
+Subject: [DIFFERENT_PREFIX 1/2] Second
+
+This is the second commit.
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file2 | 3 ---
+ 3 files changed, 5 insertions(+), 3 deletions(-)
+ delete mode 100644 file2
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..8422d40 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++C
++D
+diff --git a/file0 b/file0
+index 01e79c3..b414108 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++4
++5
++6
+diff --git a/file2 b/file2
+deleted file mode 100644
+index 01e79c3..0000000
+--- a/file2
++++ /dev/null
+@@ -1,3 +0,0 @@
+-1
+-2
+-3
+--
+g-i-t--v-e-r-s-i-o-n
+
+
+From 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0 Mon Sep 17 00:00:00 2001
+From: A U Thor <author@example.com>
+Date: Mon, 26 Jun 2006 00:02:00 +0000
+Subject: [DIFFERENT_PREFIX 2/2] Third
+
+---
+ dir/sub | 2 ++
+ file1 | 3 +++
+ 2 files changed, 5 insertions(+)
+ create mode 100644 file1
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
+--
+g-i-t--v-e-r-s-i-o-n
+
+$
diff --git a/t/t4013/diff.format-patch_--stdout_--no-numbered_initial..master b/t/t4013/diff.format-patch_--stdout_--no-numbered_initial..master
new file mode 100644
index 0000000..bfc287a
--- /dev/null
+++ b/t/t4013/diff.format-patch_--stdout_--no-numbered_initial..master
@@ -0,0 +1,127 @@
+$ git format-patch --stdout --no-numbered initial..master
+From 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44 Mon Sep 17 00:00:00 2001
+From: A U Thor <author@example.com>
+Date: Mon, 26 Jun 2006 00:01:00 +0000
+Subject: [PATCH] Second
+
+This is the second commit.
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file2 | 3 ---
+ 3 files changed, 5 insertions(+), 3 deletions(-)
+ delete mode 100644 file2
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..8422d40 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++C
++D
+diff --git a/file0 b/file0
+index 01e79c3..b414108 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++4
++5
++6
+diff --git a/file2 b/file2
+deleted file mode 100644
+index 01e79c3..0000000
+--- a/file2
++++ /dev/null
+@@ -1,3 +0,0 @@
+-1
+-2
+-3
+--
+g-i-t--v-e-r-s-i-o-n
+
+
+From 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0 Mon Sep 17 00:00:00 2001
+From: A U Thor <author@example.com>
+Date: Mon, 26 Jun 2006 00:02:00 +0000
+Subject: [PATCH] Third
+
+---
+ dir/sub | 2 ++
+ file1 | 3 +++
+ 2 files changed, 5 insertions(+)
+ create mode 100644 file1
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
+--
+g-i-t--v-e-r-s-i-o-n
+
+
+From c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a Mon Sep 17 00:00:00 2001
+From: A U Thor <author@example.com>
+Date: Mon, 26 Jun 2006 00:03:00 +0000
+Subject: [PATCH] Side
+
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file3 | 4 ++++
+ 3 files changed, 9 insertions(+)
+ create mode 100644 file3
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+--
+g-i-t--v-e-r-s-i-o-n
+
+$
diff --git a/t/t4013/diff.format-patch_--stdout_--numbered_initial..master b/t/t4013/diff.format-patch_--stdout_--numbered_initial..master
new file mode 100644
index 0000000..568f6f5
--- /dev/null
+++ b/t/t4013/diff.format-patch_--stdout_--numbered_initial..master
@@ -0,0 +1,127 @@
+$ git format-patch --stdout --numbered initial..master
+From 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44 Mon Sep 17 00:00:00 2001
+From: A U Thor <author@example.com>
+Date: Mon, 26 Jun 2006 00:01:00 +0000
+Subject: [PATCH 1/3] Second
+
+This is the second commit.
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file2 | 3 ---
+ 3 files changed, 5 insertions(+), 3 deletions(-)
+ delete mode 100644 file2
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..8422d40 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++C
++D
+diff --git a/file0 b/file0
+index 01e79c3..b414108 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++4
++5
++6
+diff --git a/file2 b/file2
+deleted file mode 100644
+index 01e79c3..0000000
+--- a/file2
++++ /dev/null
+@@ -1,3 +0,0 @@
+-1
+-2
+-3
+--
+g-i-t--v-e-r-s-i-o-n
+
+
+From 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0 Mon Sep 17 00:00:00 2001
+From: A U Thor <author@example.com>
+Date: Mon, 26 Jun 2006 00:02:00 +0000
+Subject: [PATCH 2/3] Third
+
+---
+ dir/sub | 2 ++
+ file1 | 3 +++
+ 2 files changed, 5 insertions(+)
+ create mode 100644 file1
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
+--
+g-i-t--v-e-r-s-i-o-n
+
+
+From c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a Mon Sep 17 00:00:00 2001
+From: A U Thor <author@example.com>
+Date: Mon, 26 Jun 2006 00:03:00 +0000
+Subject: [PATCH 3/3] Side
+
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file3 | 4 ++++
+ 3 files changed, 9 insertions(+)
+ create mode 100644 file3
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+--
+g-i-t--v-e-r-s-i-o-n
+
+$
diff --git a/t/t4013/diff.format-patch_--stdout_initial..master b/t/t4013/diff.format-patch_--stdout_initial..master
new file mode 100644
index 0000000..5f0352f
--- /dev/null
+++ b/t/t4013/diff.format-patch_--stdout_initial..master
@@ -0,0 +1,127 @@
+$ git format-patch --stdout initial..master
+From 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44 Mon Sep 17 00:00:00 2001
+From: A U Thor <author@example.com>
+Date: Mon, 26 Jun 2006 00:01:00 +0000
+Subject: [PATCH 1/3] Second
+
+This is the second commit.
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file2 | 3 ---
+ 3 files changed, 5 insertions(+), 3 deletions(-)
+ delete mode 100644 file2
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..8422d40 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++C
++D
+diff --git a/file0 b/file0
+index 01e79c3..b414108 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++4
++5
++6
+diff --git a/file2 b/file2
+deleted file mode 100644
+index 01e79c3..0000000
+--- a/file2
++++ /dev/null
+@@ -1,3 +0,0 @@
+-1
+-2
+-3
+--
+g-i-t--v-e-r-s-i-o-n
+
+
+From 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0 Mon Sep 17 00:00:00 2001
+From: A U Thor <author@example.com>
+Date: Mon, 26 Jun 2006 00:02:00 +0000
+Subject: [PATCH 2/3] Third
+
+---
+ dir/sub | 2 ++
+ file1 | 3 +++
+ 2 files changed, 5 insertions(+)
+ create mode 100644 file1
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
+--
+g-i-t--v-e-r-s-i-o-n
+
+
+From c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a Mon Sep 17 00:00:00 2001
+From: A U Thor <author@example.com>
+Date: Mon, 26 Jun 2006 00:03:00 +0000
+Subject: [PATCH 3/3] Side
+
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file3 | 4 ++++
+ 3 files changed, 9 insertions(+)
+ create mode 100644 file3
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+--
+g-i-t--v-e-r-s-i-o-n
+
+$
diff --git a/t/t4013/diff.format-patch_--stdout_initial..master^ b/t/t4013/diff.format-patch_--stdout_initial..master^
new file mode 100644
index 0000000..2ae454d
--- /dev/null
+++ b/t/t4013/diff.format-patch_--stdout_initial..master^
@@ -0,0 +1,81 @@
+$ git format-patch --stdout initial..master^
+From 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44 Mon Sep 17 00:00:00 2001
+From: A U Thor <author@example.com>
+Date: Mon, 26 Jun 2006 00:01:00 +0000
+Subject: [PATCH 1/2] Second
+
+This is the second commit.
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file2 | 3 ---
+ 3 files changed, 5 insertions(+), 3 deletions(-)
+ delete mode 100644 file2
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..8422d40 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++C
++D
+diff --git a/file0 b/file0
+index 01e79c3..b414108 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++4
++5
++6
+diff --git a/file2 b/file2
+deleted file mode 100644
+index 01e79c3..0000000
+--- a/file2
++++ /dev/null
+@@ -1,3 +0,0 @@
+-1
+-2
+-3
+--
+g-i-t--v-e-r-s-i-o-n
+
+
+From 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0 Mon Sep 17 00:00:00 2001
+From: A U Thor <author@example.com>
+Date: Mon, 26 Jun 2006 00:02:00 +0000
+Subject: [PATCH 2/2] Third
+
+---
+ dir/sub | 2 ++
+ file1 | 3 +++
+ 2 files changed, 5 insertions(+)
+ create mode 100644 file1
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
+--
+g-i-t--v-e-r-s-i-o-n
+
+$
diff --git a/t/t4013/diff.format-patch_--stdout_initial..side b/t/t4013/diff.format-patch_--stdout_initial..side
new file mode 100644
index 0000000..a7d52fb
--- /dev/null
+++ b/t/t4013/diff.format-patch_--stdout_initial..side
@@ -0,0 +1,47 @@
+$ git format-patch --stdout initial..side
+From c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a Mon Sep 17 00:00:00 2001
+From: A U Thor <author@example.com>
+Date: Mon, 26 Jun 2006 00:03:00 +0000
+Subject: [PATCH] Side
+
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file3 | 4 ++++
+ 3 files changed, 9 insertions(+)
+ create mode 100644 file3
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+--
+g-i-t--v-e-r-s-i-o-n
+
+$
diff --git a/t/t4013/diff.log_--cc_-m_-p_master b/t/t4013/diff.log_--cc_-m_-p_master
new file mode 100644
index 0000000..7c217cf
--- /dev/null
+++ b/t/t4013/diff.log_--cc_-m_-p_master
@@ -0,0 +1,200 @@
+$ git log --cc -m -p master
+commit 59d314ad6f356dd08601a4cd5e530381da3e3c64 (from 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0)
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+diff --git a/dir/sub b/dir/sub
+index cead32e..992913c 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -4,3 +4,5 @@ C
+ D
+ E
+ F
++1
++2
+diff --git a/file0 b/file0
+index b414108..10a8a9f 100644
+--- a/file0
++++ b/file0
+@@ -4,3 +4,6 @@
+ 4
+ 5
+ 6
++A
++B
++C
+
+commit 59d314ad6f356dd08601a4cd5e530381da3e3c64 (from c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a)
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+diff --git a/dir/sub b/dir/sub
+index 7289e35..992913c 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,4 +1,8 @@
+ A
+ B
++C
++D
++E
++F
+ 1
+ 2
+diff --git a/file0 b/file0
+index f4615da..10a8a9f 100644
+--- a/file0
++++ b/file0
+@@ -1,6 +1,9 @@
+ 1
+ 2
+ 3
++4
++5
++6
+ A
+ B
+ C
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
+diff --git a/file2 b/file2
+deleted file mode 100644
+index 01e79c3..0000000
+--- a/file2
++++ /dev/null
+@@ -1,3 +0,0 @@
+-1
+-2
+-3
+diff --git a/file3 b/file3
+deleted file mode 100644
+index 7289e35..0000000
+--- a/file3
++++ /dev/null
+@@ -1,4 +0,0 @@
+-A
+-B
+-1
+-2
+
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..8422d40 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++C
++D
+diff --git a/file0 b/file0
+index 01e79c3..b414108 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++4
++5
++6
+diff --git a/file2 b/file2
+deleted file mode 100644
+index 01e79c3..0000000
+--- a/file2
++++ /dev/null
+@@ -1,3 +0,0 @@
+-1
+-2
+-3
+
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+$
diff --git a/t/t4013/diff.log_--decorate=full_--all b/t/t4013/diff.log_--decorate=full_--all
new file mode 100644
index 0000000..6b0b334
--- /dev/null
+++ b/t/t4013/diff.log_--decorate=full_--all
@@ -0,0 +1,61 @@
+$ git log --decorate=full --all
+commit b7e0bc69303b488b47deca799a7d723971dfa6cd (refs/heads/mode)
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:06:00 2006 +0000
+
+ update mode
+
+commit a6f364368ca320bc5a92e18912e16fa6b3dff598 (refs/heads/note)
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:06:00 2006 +0000
+
+ update mode (file2)
+
+Notes:
+ note
+
+commit cd4e72fd96faed3f0ba949dc42967430374e2290 (refs/heads/rearrange)
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:06:00 2006 +0000
+
+ Rearranged lines in dir/sub
+
+commit cbacedd14cb8b89255a2c02b59e77a2e9a8021a0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:06:00 2006 +0000
+
+ Notes added by 'git notes add'
+
+commit 59d314ad6f356dd08601a4cd5e530381da3e3c64 (HEAD -> refs/heads/master)
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a (refs/heads/side)
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+
+commit 444ac553ac7612cc88969031b02b3767fb8a353a (refs/heads/initial)
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+$
diff --git a/t/t4013/diff.log_--decorate=full_--clear-decorations_--all b/t/t4013/diff.log_--decorate=full_--clear-decorations_--all
new file mode 100644
index 0000000..1c030a6
--- /dev/null
+++ b/t/t4013/diff.log_--decorate=full_--clear-decorations_--all
@@ -0,0 +1,61 @@
+$ git log --decorate=full --clear-decorations --all
+commit b7e0bc69303b488b47deca799a7d723971dfa6cd (refs/heads/mode)
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:06:00 2006 +0000
+
+ update mode
+
+commit a6f364368ca320bc5a92e18912e16fa6b3dff598 (refs/heads/note)
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:06:00 2006 +0000
+
+ update mode (file2)
+
+Notes:
+ note
+
+commit cd4e72fd96faed3f0ba949dc42967430374e2290 (refs/heads/rearrange)
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:06:00 2006 +0000
+
+ Rearranged lines in dir/sub
+
+commit cbacedd14cb8b89255a2c02b59e77a2e9a8021a0 (refs/notes/commits)
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:06:00 2006 +0000
+
+ Notes added by 'git notes add'
+
+commit 59d314ad6f356dd08601a4cd5e530381da3e3c64 (HEAD -> refs/heads/master)
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a (refs/heads/side)
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+
+commit 444ac553ac7612cc88969031b02b3767fb8a353a (refs/heads/initial)
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+$
diff --git a/t/t4013/diff.log_--decorate=full_--decorate-all_--all b/t/t4013/diff.log_--decorate=full_--decorate-all_--all
new file mode 100644
index 0000000..d6e7928
--- /dev/null
+++ b/t/t4013/diff.log_--decorate=full_--decorate-all_--all
@@ -0,0 +1,61 @@
+$ git log --decorate=full --decorate-all --all
+commit b7e0bc69303b488b47deca799a7d723971dfa6cd (refs/heads/mode)
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:06:00 2006 +0000
+
+ update mode
+
+commit a6f364368ca320bc5a92e18912e16fa6b3dff598 (refs/heads/note)
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:06:00 2006 +0000
+
+ update mode (file2)
+
+Notes:
+ note
+
+commit cd4e72fd96faed3f0ba949dc42967430374e2290 (refs/heads/rearrange)
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:06:00 2006 +0000
+
+ Rearranged lines in dir/sub
+
+commit cbacedd14cb8b89255a2c02b59e77a2e9a8021a0 (refs/notes/commits)
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:06:00 2006 +0000
+
+ Notes added by 'git notes add'
+
+commit 59d314ad6f356dd08601a4cd5e530381da3e3c64 (HEAD -> refs/heads/master)
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a (refs/heads/side)
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+
+commit 444ac553ac7612cc88969031b02b3767fb8a353a (refs/heads/initial)
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+$
diff --git a/t/t4013/diff.log_--decorate_--all b/t/t4013/diff.log_--decorate_--all
new file mode 100644
index 0000000..c7df1f5
--- /dev/null
+++ b/t/t4013/diff.log_--decorate_--all
@@ -0,0 +1,61 @@
+$ git log --decorate --all
+commit b7e0bc69303b488b47deca799a7d723971dfa6cd (mode)
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:06:00 2006 +0000
+
+ update mode
+
+commit a6f364368ca320bc5a92e18912e16fa6b3dff598 (note)
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:06:00 2006 +0000
+
+ update mode (file2)
+
+Notes:
+ note
+
+commit cd4e72fd96faed3f0ba949dc42967430374e2290 (rearrange)
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:06:00 2006 +0000
+
+ Rearranged lines in dir/sub
+
+commit cbacedd14cb8b89255a2c02b59e77a2e9a8021a0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:06:00 2006 +0000
+
+ Notes added by 'git notes add'
+
+commit 59d314ad6f356dd08601a4cd5e530381da3e3c64 (HEAD -> master)
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a (side)
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+
+commit 444ac553ac7612cc88969031b02b3767fb8a353a (initial)
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+$
diff --git a/t/t4013/diff.log_--decorate_--clear-decorations_--all b/t/t4013/diff.log_--decorate_--clear-decorations_--all
new file mode 100644
index 0000000..88be82c
--- /dev/null
+++ b/t/t4013/diff.log_--decorate_--clear-decorations_--all
@@ -0,0 +1,61 @@
+$ git log --decorate --clear-decorations --all
+commit b7e0bc69303b488b47deca799a7d723971dfa6cd (mode)
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:06:00 2006 +0000
+
+ update mode
+
+commit a6f364368ca320bc5a92e18912e16fa6b3dff598 (note)
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:06:00 2006 +0000
+
+ update mode (file2)
+
+Notes:
+ note
+
+commit cd4e72fd96faed3f0ba949dc42967430374e2290 (rearrange)
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:06:00 2006 +0000
+
+ Rearranged lines in dir/sub
+
+commit cbacedd14cb8b89255a2c02b59e77a2e9a8021a0 (refs/notes/commits)
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:06:00 2006 +0000
+
+ Notes added by 'git notes add'
+
+commit 59d314ad6f356dd08601a4cd5e530381da3e3c64 (HEAD -> master)
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a (side)
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+
+commit 444ac553ac7612cc88969031b02b3767fb8a353a (initial)
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+$
diff --git a/t/t4013/diff.log_--decorate_--decorate-all_--all b/t/t4013/diff.log_--decorate_--decorate-all_--all
new file mode 100644
index 0000000..5d22618
--- /dev/null
+++ b/t/t4013/diff.log_--decorate_--decorate-all_--all
@@ -0,0 +1,61 @@
+$ git log --decorate --decorate-all --all
+commit b7e0bc69303b488b47deca799a7d723971dfa6cd (mode)
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:06:00 2006 +0000
+
+ update mode
+
+commit a6f364368ca320bc5a92e18912e16fa6b3dff598 (note)
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:06:00 2006 +0000
+
+ update mode (file2)
+
+Notes:
+ note
+
+commit cd4e72fd96faed3f0ba949dc42967430374e2290 (rearrange)
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:06:00 2006 +0000
+
+ Rearranged lines in dir/sub
+
+commit cbacedd14cb8b89255a2c02b59e77a2e9a8021a0 (refs/notes/commits)
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:06:00 2006 +0000
+
+ Notes added by 'git notes add'
+
+commit 59d314ad6f356dd08601a4cd5e530381da3e3c64 (HEAD -> master)
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a (side)
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+
+commit 444ac553ac7612cc88969031b02b3767fb8a353a (initial)
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+$
diff --git a/t/t4013/diff.log_--diff-merges=first-parent_master b/t/t4013/diff.log_--diff-merges=first-parent_master
new file mode 100644
index 0000000..fa63a55
--- /dev/null
+++ b/t/t4013/diff.log_--diff-merges=first-parent_master
@@ -0,0 +1,56 @@
+$ git log --diff-merges=first-parent master
+commit 59d314ad6f356dd08601a4cd5e530381da3e3c64
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+diff --git a/dir/sub b/dir/sub
+index cead32e..992913c 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -4,3 +4,5 @@ C
+ D
+ E
+ F
++1
++2
+diff --git a/file0 b/file0
+index b414108..10a8a9f 100644
+--- a/file0
++++ b/file0
+@@ -4,3 +4,6 @@
+ 4
+ 5
+ 6
++A
++B
++C
+
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+$
diff --git a/t/t4013/diff.log_--diff-merges=off_-p_--first-parent_master b/t/t4013/diff.log_--diff-merges=off_-p_--first-parent_master
new file mode 100644
index 0000000..194e893
--- /dev/null
+++ b/t/t4013/diff.log_--diff-merges=off_-p_--first-parent_master
@@ -0,0 +1,78 @@
+$ git log --diff-merges=off -p --first-parent master
+commit 59d314ad6f356dd08601a4cd5e530381da3e3c64
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..8422d40 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++C
++D
+diff --git a/file0 b/file0
+index 01e79c3..b414108 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++4
++5
++6
+diff --git a/file2 b/file2
+deleted file mode 100644
+index 01e79c3..0000000
+--- a/file2
++++ /dev/null
+@@ -1,3 +0,0 @@
+-1
+-2
+-3
+
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+$
diff --git a/t/t4013/diff.log_--first-parent_--diff-merges=off_-p_master b/t/t4013/diff.log_--first-parent_--diff-merges=off_-p_master
new file mode 100644
index 0000000..5d7461a
--- /dev/null
+++ b/t/t4013/diff.log_--first-parent_--diff-merges=off_-p_master
@@ -0,0 +1,78 @@
+$ git log --first-parent --diff-merges=off -p master
+commit 80e25ffa65bcdbe82ef654b4d06dbbde7945c37f
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..8422d40 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++C
++D
+diff --git a/file0 b/file0
+index 01e79c3..b414108 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++4
++5
++6
+diff --git a/file2 b/file2
+deleted file mode 100644
+index 01e79c3..0000000
+--- a/file2
++++ /dev/null
+@@ -1,3 +0,0 @@
+-1
+-2
+-3
+
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+$
diff --git a/t/t4013/diff.log_--no-diff-merges_-p_--first-parent_master b/t/t4013/diff.log_--no-diff-merges_-p_--first-parent_master
new file mode 100644
index 0000000..5970022
--- /dev/null
+++ b/t/t4013/diff.log_--no-diff-merges_-p_--first-parent_master
@@ -0,0 +1,78 @@
+$ git log --no-diff-merges -p --first-parent master
+commit 59d314ad6f356dd08601a4cd5e530381da3e3c64
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..8422d40 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++C
++D
+diff --git a/file0 b/file0
+index 01e79c3..b414108 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++4
++5
++6
+diff --git a/file2 b/file2
+deleted file mode 100644
+index 01e79c3..0000000
+--- a/file2
++++ /dev/null
+@@ -1,3 +0,0 @@
+-1
+-2
+-3
+
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+$
diff --git a/t/t4013/diff.log_--patch-with-stat_--summary_master_--_dir_ b/t/t4013/diff.log_--patch-with-stat_--summary_master_--_dir_
new file mode 100644
index 0000000..a18f147
--- /dev/null
+++ b/t/t4013/diff.log_--patch-with-stat_--summary_master_--_dir_
@@ -0,0 +1,74 @@
+$ git log --patch-with-stat --summary master -- dir/
+commit 59d314ad6f356dd08601a4cd5e530381da3e3c64
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+---
+ dir/sub | 2 ++
+ 1 file changed, 2 insertions(+)
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+---
+ dir/sub | 2 ++
+ 1 file changed, 2 insertions(+)
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+---
+ dir/sub | 2 ++
+ 1 file changed, 2 insertions(+)
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..8422d40 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++C
++D
+
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+$
diff --git a/t/t4013/diff.log_--patch-with-stat_master b/t/t4013/diff.log_--patch-with-stat_master
new file mode 100644
index 0000000..ae425c4
--- /dev/null
+++ b/t/t4013/diff.log_--patch-with-stat_master
@@ -0,0 +1,129 @@
+$ git log --patch-with-stat master
+commit 59d314ad6f356dd08601a4cd5e530381da3e3c64
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file3 | 4 ++++
+ 3 files changed, 9 insertions(+)
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+---
+ dir/sub | 2 ++
+ file1 | 3 +++
+ 2 files changed, 5 insertions(+)
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file2 | 3 ---
+ 3 files changed, 5 insertions(+), 3 deletions(-)
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..8422d40 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++C
++D
+diff --git a/file0 b/file0
+index 01e79c3..b414108 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++4
++5
++6
+diff --git a/file2 b/file2
+deleted file mode 100644
+index 01e79c3..0000000
+--- a/file2
++++ /dev/null
+@@ -1,3 +0,0 @@
+-1
+-2
+-3
+
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+$
diff --git a/t/t4013/diff.log_--patch-with-stat_master_--_dir_ b/t/t4013/diff.log_--patch-with-stat_master_--_dir_
new file mode 100644
index 0000000..d5207ca
--- /dev/null
+++ b/t/t4013/diff.log_--patch-with-stat_master_--_dir_
@@ -0,0 +1,74 @@
+$ git log --patch-with-stat master -- dir/
+commit 59d314ad6f356dd08601a4cd5e530381da3e3c64
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+---
+ dir/sub | 2 ++
+ 1 file changed, 2 insertions(+)
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+---
+ dir/sub | 2 ++
+ 1 file changed, 2 insertions(+)
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+---
+ dir/sub | 2 ++
+ 1 file changed, 2 insertions(+)
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..8422d40 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++C
++D
+
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+$
diff --git a/t/t4013/diff.log_--root_--cc_--patch-with-stat_--summary_master b/t/t4013/diff.log_--root_--cc_--patch-with-stat_--summary_master
new file mode 100644
index 0000000..0fc1e8c
--- /dev/null
+++ b/t/t4013/diff.log_--root_--cc_--patch-with-stat_--summary_master
@@ -0,0 +1,199 @@
+$ git log --root --cc --patch-with-stat --summary master
+commit 59d314ad6f356dd08601a4cd5e530381da3e3c64
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+ dir/sub | 2 ++
+ file0 | 3 +++
+ 2 files changed, 5 insertions(+)
+
+diff --cc dir/sub
+index cead32e,7289e35..992913c
+--- a/dir/sub
++++ b/dir/sub
+@@@ -1,6 -1,4 +1,8 @@@
+ A
+ B
+ +C
+ +D
+ +E
+ +F
++ 1
++ 2
+diff --cc file0
+index b414108,f4615da..10a8a9f
+--- a/file0
++++ b/file0
+@@@ -1,6 -1,6 +1,9 @@@
+ 1
+ 2
+ 3
+ +4
+ +5
+ +6
++ A
++ B
++ C
+
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file3 | 4 ++++
+ 3 files changed, 9 insertions(+)
+ create mode 100644 file3
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+---
+ dir/sub | 2 ++
+ file1 | 3 +++
+ 2 files changed, 5 insertions(+)
+ create mode 100644 file1
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file2 | 3 ---
+ 3 files changed, 5 insertions(+), 3 deletions(-)
+ delete mode 100644 file2
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..8422d40 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++C
++D
+diff --git a/file0 b/file0
+index 01e79c3..b414108 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++4
++5
++6
+diff --git a/file2 b/file2
+deleted file mode 100644
+index 01e79c3..0000000
+--- a/file2
++++ /dev/null
+@@ -1,3 +0,0 @@
+-1
+-2
+-3
+
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file2 | 3 +++
+ 3 files changed, 8 insertions(+)
+ create mode 100644 dir/sub
+ create mode 100644 file0
+ create mode 100644 file2
+
+diff --git a/dir/sub b/dir/sub
+new file mode 100644
+index 0000000..35d242b
+--- /dev/null
++++ b/dir/sub
+@@ -0,0 +1,2 @@
++A
++B
+diff --git a/file0 b/file0
+new file mode 100644
+index 0000000..01e79c3
+--- /dev/null
++++ b/file0
+@@ -0,0 +1,3 @@
++1
++2
++3
+diff --git a/file2 b/file2
+new file mode 100644
+index 0000000..01e79c3
+--- /dev/null
++++ b/file2
+@@ -0,0 +1,3 @@
++1
++2
++3
+$
diff --git a/t/t4013/diff.log_--root_--patch-with-stat_--summary_master b/t/t4013/diff.log_--root_--patch-with-stat_--summary_master
new file mode 100644
index 0000000..dffc09d
--- /dev/null
+++ b/t/t4013/diff.log_--root_--patch-with-stat_--summary_master
@@ -0,0 +1,167 @@
+$ git log --root --patch-with-stat --summary master
+commit 59d314ad6f356dd08601a4cd5e530381da3e3c64
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file3 | 4 ++++
+ 3 files changed, 9 insertions(+)
+ create mode 100644 file3
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+---
+ dir/sub | 2 ++
+ file1 | 3 +++
+ 2 files changed, 5 insertions(+)
+ create mode 100644 file1
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file2 | 3 ---
+ 3 files changed, 5 insertions(+), 3 deletions(-)
+ delete mode 100644 file2
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..8422d40 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++C
++D
+diff --git a/file0 b/file0
+index 01e79c3..b414108 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++4
++5
++6
+diff --git a/file2 b/file2
+deleted file mode 100644
+index 01e79c3..0000000
+--- a/file2
++++ /dev/null
+@@ -1,3 +0,0 @@
+-1
+-2
+-3
+
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file2 | 3 +++
+ 3 files changed, 8 insertions(+)
+ create mode 100644 dir/sub
+ create mode 100644 file0
+ create mode 100644 file2
+
+diff --git a/dir/sub b/dir/sub
+new file mode 100644
+index 0000000..35d242b
+--- /dev/null
++++ b/dir/sub
+@@ -0,0 +1,2 @@
++A
++B
+diff --git a/file0 b/file0
+new file mode 100644
+index 0000000..01e79c3
+--- /dev/null
++++ b/file0
+@@ -0,0 +1,3 @@
++1
++2
++3
+diff --git a/file2 b/file2
+new file mode 100644
+index 0000000..01e79c3
+--- /dev/null
++++ b/file2
+@@ -0,0 +1,3 @@
++1
++2
++3
+$
diff --git a/t/t4013/diff.log_--root_--patch-with-stat_master b/t/t4013/diff.log_--root_--patch-with-stat_master
new file mode 100644
index 0000000..55aa980
--- /dev/null
+++ b/t/t4013/diff.log_--root_--patch-with-stat_master
@@ -0,0 +1,161 @@
+$ git log --root --patch-with-stat master
+commit 59d314ad6f356dd08601a4cd5e530381da3e3c64
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file3 | 4 ++++
+ 3 files changed, 9 insertions(+)
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+---
+ dir/sub | 2 ++
+ file1 | 3 +++
+ 2 files changed, 5 insertions(+)
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file2 | 3 ---
+ 3 files changed, 5 insertions(+), 3 deletions(-)
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..8422d40 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++C
++D
+diff --git a/file0 b/file0
+index 01e79c3..b414108 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++4
++5
++6
+diff --git a/file2 b/file2
+deleted file mode 100644
+index 01e79c3..0000000
+--- a/file2
++++ /dev/null
+@@ -1,3 +0,0 @@
+-1
+-2
+-3
+
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file2 | 3 +++
+ 3 files changed, 8 insertions(+)
+
+diff --git a/dir/sub b/dir/sub
+new file mode 100644
+index 0000000..35d242b
+--- /dev/null
++++ b/dir/sub
+@@ -0,0 +1,2 @@
++A
++B
+diff --git a/file0 b/file0
+new file mode 100644
+index 0000000..01e79c3
+--- /dev/null
++++ b/file0
+@@ -0,0 +1,3 @@
++1
++2
++3
+diff --git a/file2 b/file2
+new file mode 100644
+index 0000000..01e79c3
+--- /dev/null
++++ b/file2
+@@ -0,0 +1,3 @@
++1
++2
++3
+$
diff --git a/t/t4013/diff.log_--root_-c_--patch-with-stat_--summary_master b/t/t4013/diff.log_--root_-c_--patch-with-stat_--summary_master
new file mode 100644
index 0000000..019d85f
--- /dev/null
+++ b/t/t4013/diff.log_--root_-c_--patch-with-stat_--summary_master
@@ -0,0 +1,199 @@
+$ git log --root -c --patch-with-stat --summary master
+commit 59d314ad6f356dd08601a4cd5e530381da3e3c64
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+ dir/sub | 2 ++
+ file0 | 3 +++
+ 2 files changed, 5 insertions(+)
+
+diff --combined dir/sub
+index cead32e,7289e35..992913c
+--- a/dir/sub
++++ b/dir/sub
+@@@ -1,6 -1,4 +1,8 @@@
+ A
+ B
+ +C
+ +D
+ +E
+ +F
++ 1
++ 2
+diff --combined file0
+index b414108,f4615da..10a8a9f
+--- a/file0
++++ b/file0
+@@@ -1,6 -1,6 +1,9 @@@
+ 1
+ 2
+ 3
+ +4
+ +5
+ +6
++ A
++ B
++ C
+
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file3 | 4 ++++
+ 3 files changed, 9 insertions(+)
+ create mode 100644 file3
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+---
+ dir/sub | 2 ++
+ file1 | 3 +++
+ 2 files changed, 5 insertions(+)
+ create mode 100644 file1
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file2 | 3 ---
+ 3 files changed, 5 insertions(+), 3 deletions(-)
+ delete mode 100644 file2
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..8422d40 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++C
++D
+diff --git a/file0 b/file0
+index 01e79c3..b414108 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++4
++5
++6
+diff --git a/file2 b/file2
+deleted file mode 100644
+index 01e79c3..0000000
+--- a/file2
++++ /dev/null
+@@ -1,3 +0,0 @@
+-1
+-2
+-3
+
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file2 | 3 +++
+ 3 files changed, 8 insertions(+)
+ create mode 100644 dir/sub
+ create mode 100644 file0
+ create mode 100644 file2
+
+diff --git a/dir/sub b/dir/sub
+new file mode 100644
+index 0000000..35d242b
+--- /dev/null
++++ b/dir/sub
+@@ -0,0 +1,2 @@
++A
++B
+diff --git a/file0 b/file0
+new file mode 100644
+index 0000000..01e79c3
+--- /dev/null
++++ b/file0
+@@ -0,0 +1,3 @@
++1
++2
++3
+diff --git a/file2 b/file2
+new file mode 100644
+index 0000000..01e79c3
+--- /dev/null
++++ b/file2
+@@ -0,0 +1,3 @@
++1
++2
++3
+$
diff --git a/t/t4013/diff.log_--root_-p_master b/t/t4013/diff.log_--root_-p_master
new file mode 100644
index 0000000..b42c334
--- /dev/null
+++ b/t/t4013/diff.log_--root_-p_master
@@ -0,0 +1,142 @@
+$ git log --root -p master
+commit 59d314ad6f356dd08601a4cd5e530381da3e3c64
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..8422d40 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++C
++D
+diff --git a/file0 b/file0
+index 01e79c3..b414108 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++4
++5
++6
+diff --git a/file2 b/file2
+deleted file mode 100644
+index 01e79c3..0000000
+--- a/file2
++++ /dev/null
+@@ -1,3 +0,0 @@
+-1
+-2
+-3
+
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+
+diff --git a/dir/sub b/dir/sub
+new file mode 100644
+index 0000000..35d242b
+--- /dev/null
++++ b/dir/sub
+@@ -0,0 +1,2 @@
++A
++B
+diff --git a/file0 b/file0
+new file mode 100644
+index 0000000..01e79c3
+--- /dev/null
++++ b/file0
+@@ -0,0 +1,3 @@
++1
++2
++3
+diff --git a/file2 b/file2
+new file mode 100644
+index 0000000..01e79c3
+--- /dev/null
++++ b/file2
+@@ -0,0 +1,3 @@
++1
++2
++3
+$
diff --git a/t/t4013/diff.log_--root_master b/t/t4013/diff.log_--root_master
new file mode 100644
index 0000000..e8f4615
--- /dev/null
+++ b/t/t4013/diff.log_--root_master
@@ -0,0 +1,34 @@
+$ git log --root master
+commit 59d314ad6f356dd08601a4cd5e530381da3e3c64
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+$
diff --git a/t/t4013/diff.log_-GF_-p_--pickaxe-all_master b/t/t4013/diff.log_-GF_-p_--pickaxe-all_master
new file mode 100644
index 0000000..d36f880
--- /dev/null
+++ b/t/t4013/diff.log_-GF_-p_--pickaxe-all_master
@@ -0,0 +1,27 @@
+$ git log -GF -p --pickaxe-all master
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
+$
diff --git a/t/t4013/diff.log_-GF_-p_master b/t/t4013/diff.log_-GF_-p_master
new file mode 100644
index 0000000..9d93f2c
--- /dev/null
+++ b/t/t4013/diff.log_-GF_-p_master
@@ -0,0 +1,18 @@
+$ git log -GF -p master
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+$
diff --git a/t/t4013/diff.log_-GF_master b/t/t4013/diff.log_-GF_master
new file mode 100644
index 0000000..4c6708d
--- /dev/null
+++ b/t/t4013/diff.log_-GF_master
@@ -0,0 +1,7 @@
+$ git log -GF master
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+$
diff --git a/t/t4013/diff.log_-IA_-IB_-I1_-I2_-p_master b/t/t4013/diff.log_-IA_-IB_-I1_-I2_-p_master
new file mode 100644
index 0000000..929f35a
--- /dev/null
+++ b/t/t4013/diff.log_-IA_-IB_-I1_-I2_-p_master
@@ -0,0 +1,99 @@
+$ git log -IA -IB -I1 -I2 -p master
+commit 59d314ad6f356dd08601a4cd5e530381da3e3c64
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..8422d40 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++C
++D
+diff --git a/file0 b/file0
+index 01e79c3..b414108 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++4
++5
++6
+diff --git a/file2 b/file2
+deleted file mode 100644
+index 01e79c3..0000000
+--- a/file2
++++ /dev/null
+@@ -1,3 +0,0 @@
+-1
+-2
+-3
+
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+$
diff --git a/t/t4013/diff.log_-SF_-p_master b/t/t4013/diff.log_-SF_-p_master
new file mode 100644
index 0000000..5e32438
--- /dev/null
+++ b/t/t4013/diff.log_-SF_-p_master
@@ -0,0 +1,18 @@
+$ git log -SF -p master
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+$
diff --git a/t/t4013/diff.log_-SF_master b/t/t4013/diff.log_-SF_master
new file mode 100644
index 0000000..c1599f2
--- /dev/null
+++ b/t/t4013/diff.log_-SF_master
@@ -0,0 +1,7 @@
+$ git log -SF master
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+$
diff --git a/t/t4013/diff.log_-SF_master_--max-count=0 b/t/t4013/diff.log_-SF_master_--max-count=0
new file mode 100644
index 0000000..c1fc6c8
--- /dev/null
+++ b/t/t4013/diff.log_-SF_master_--max-count=0
@@ -0,0 +1,2 @@
+$ git log -SF master --max-count=0
+$
diff --git a/t/t4013/diff.log_-SF_master_--max-count=1 b/t/t4013/diff.log_-SF_master_--max-count=1
new file mode 100644
index 0000000..c981a03
--- /dev/null
+++ b/t/t4013/diff.log_-SF_master_--max-count=1
@@ -0,0 +1,7 @@
+$ git log -SF master --max-count=1
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+$
diff --git a/t/t4013/diff.log_-SF_master_--max-count=2 b/t/t4013/diff.log_-SF_master_--max-count=2
new file mode 100644
index 0000000..a6c55fd
--- /dev/null
+++ b/t/t4013/diff.log_-SF_master_--max-count=2
@@ -0,0 +1,7 @@
+$ git log -SF master --max-count=2
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+$
diff --git a/t/t4013/diff.log_-S_F_master b/t/t4013/diff.log_-S_F_master
new file mode 100644
index 0000000..978d2b4
--- /dev/null
+++ b/t/t4013/diff.log_-S_F_master
@@ -0,0 +1,7 @@
+$ git log -S F master
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+$
diff --git a/t/t4013/diff.log_-c_-m_-p_master b/t/t4013/diff.log_-c_-m_-p_master
new file mode 100644
index 0000000..b660f3d
--- /dev/null
+++ b/t/t4013/diff.log_-c_-m_-p_master
@@ -0,0 +1,200 @@
+$ git log -c -m -p master
+commit 59d314ad6f356dd08601a4cd5e530381da3e3c64 (from 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0)
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+diff --git a/dir/sub b/dir/sub
+index cead32e..992913c 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -4,3 +4,5 @@ C
+ D
+ E
+ F
++1
++2
+diff --git a/file0 b/file0
+index b414108..10a8a9f 100644
+--- a/file0
++++ b/file0
+@@ -4,3 +4,6 @@
+ 4
+ 5
+ 6
++A
++B
++C
+
+commit 59d314ad6f356dd08601a4cd5e530381da3e3c64 (from c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a)
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+diff --git a/dir/sub b/dir/sub
+index 7289e35..992913c 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,4 +1,8 @@
+ A
+ B
++C
++D
++E
++F
+ 1
+ 2
+diff --git a/file0 b/file0
+index f4615da..10a8a9f 100644
+--- a/file0
++++ b/file0
+@@ -1,6 +1,9 @@
+ 1
+ 2
+ 3
++4
++5
++6
+ A
+ B
+ C
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
+diff --git a/file2 b/file2
+deleted file mode 100644
+index 01e79c3..0000000
+--- a/file2
++++ /dev/null
+@@ -1,3 +0,0 @@
+-1
+-2
+-3
+diff --git a/file3 b/file3
+deleted file mode 100644
+index 7289e35..0000000
+--- a/file3
++++ /dev/null
+@@ -1,4 +0,0 @@
+-A
+-B
+-1
+-2
+
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..8422d40 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++C
++D
+diff --git a/file0 b/file0
+index 01e79c3..b414108 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++4
++5
++6
+diff --git a/file2 b/file2
+deleted file mode 100644
+index 01e79c3..0000000
+--- a/file2
++++ /dev/null
+@@ -1,3 +0,0 @@
+-1
+-2
+-3
+
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+$
diff --git a/t/t4013/diff.log_-m_--raw_master b/t/t4013/diff.log_-m_--raw_master
new file mode 100644
index 0000000..cd2ecc4
--- /dev/null
+++ b/t/t4013/diff.log_-m_--raw_master
@@ -0,0 +1,61 @@
+$ git log -m --raw master
+commit 59d314ad6f356dd08601a4cd5e530381da3e3c64 (from 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0)
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+:100644 100644 cead32e... 992913c... M dir/sub
+:100644 100644 b414108... 10a8a9f... M file0
+
+commit 59d314ad6f356dd08601a4cd5e530381da3e3c64 (from c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a)
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+:100644 100644 7289e35... 992913c... M dir/sub
+:100644 100644 f4615da... 10a8a9f... M file0
+:000000 100644 0000000... b1e6722... A file1
+:100644 000000 01e79c3... 0000000... D file2
+:100644 000000 7289e35... 0000000... D file3
+
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+
+:100644 100644 35d242b... 7289e35... M dir/sub
+:100644 100644 01e79c3... f4615da... M file0
+:000000 100644 0000000... 7289e35... A file3
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+
+:100644 100644 8422d40... cead32e... M dir/sub
+:000000 100644 0000000... b1e6722... A file1
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+
+:100644 100644 35d242b... 8422d40... M dir/sub
+:100644 100644 01e79c3... b414108... M file0
+:100644 000000 01e79c3... 0000000... D file2
+
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+$
diff --git a/t/t4013/diff.log_-m_--stat_master b/t/t4013/diff.log_-m_--stat_master
new file mode 100644
index 0000000..c7db084
--- /dev/null
+++ b/t/t4013/diff.log_-m_--stat_master
@@ -0,0 +1,66 @@
+$ git log -m --stat master
+commit 59d314ad6f356dd08601a4cd5e530381da3e3c64 (from 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0)
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+ dir/sub | 2 ++
+ file0 | 3 +++
+ 2 files changed, 5 insertions(+)
+
+commit 59d314ad6f356dd08601a4cd5e530381da3e3c64 (from c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a)
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+ dir/sub | 4 ++++
+ file0 | 3 +++
+ file1 | 3 +++
+ file2 | 3 ---
+ file3 | 4 ----
+ 5 files changed, 10 insertions(+), 7 deletions(-)
+
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file3 | 4 ++++
+ 3 files changed, 9 insertions(+)
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+
+ dir/sub | 2 ++
+ file1 | 3 +++
+ 2 files changed, 5 insertions(+)
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file2 | 3 ---
+ 3 files changed, 5 insertions(+), 3 deletions(-)
+
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+$
diff --git a/t/t4013/diff.log_-m_-p_--first-parent_master b/t/t4013/diff.log_-m_-p_--first-parent_master
new file mode 100644
index 0000000..7a0073f
--- /dev/null
+++ b/t/t4013/diff.log_-m_-p_--first-parent_master
@@ -0,0 +1,100 @@
+$ git log -m -p --first-parent master
+commit 59d314ad6f356dd08601a4cd5e530381da3e3c64
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+diff --git a/dir/sub b/dir/sub
+index cead32e..992913c 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -4,3 +4,5 @@ C
+ D
+ E
+ F
++1
++2
+diff --git a/file0 b/file0
+index b414108..10a8a9f 100644
+--- a/file0
++++ b/file0
+@@ -4,3 +4,6 @@
+ 4
+ 5
+ 6
++A
++B
++C
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..8422d40 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++C
++D
+diff --git a/file0 b/file0
+index 01e79c3..b414108 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++4
++5
++6
+diff --git a/file2 b/file2
+deleted file mode 100644
+index 01e79c3..0000000
+--- a/file2
++++ /dev/null
+@@ -1,3 +0,0 @@
+-1
+-2
+-3
+
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+$
diff --git a/t/t4013/diff.log_-m_-p_master b/t/t4013/diff.log_-m_-p_master
new file mode 100644
index 0000000..9ca62a0
--- /dev/null
+++ b/t/t4013/diff.log_-m_-p_master
@@ -0,0 +1,200 @@
+$ git log -m -p master
+commit 59d314ad6f356dd08601a4cd5e530381da3e3c64 (from 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0)
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+diff --git a/dir/sub b/dir/sub
+index cead32e..992913c 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -4,3 +4,5 @@ C
+ D
+ E
+ F
++1
++2
+diff --git a/file0 b/file0
+index b414108..10a8a9f 100644
+--- a/file0
++++ b/file0
+@@ -4,3 +4,6 @@
+ 4
+ 5
+ 6
++A
++B
++C
+
+commit 59d314ad6f356dd08601a4cd5e530381da3e3c64 (from c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a)
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+diff --git a/dir/sub b/dir/sub
+index 7289e35..992913c 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,4 +1,8 @@
+ A
+ B
++C
++D
++E
++F
+ 1
+ 2
+diff --git a/file0 b/file0
+index f4615da..10a8a9f 100644
+--- a/file0
++++ b/file0
+@@ -1,6 +1,9 @@
+ 1
+ 2
+ 3
++4
++5
++6
+ A
+ B
+ C
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
+diff --git a/file2 b/file2
+deleted file mode 100644
+index 01e79c3..0000000
+--- a/file2
++++ /dev/null
+@@ -1,3 +0,0 @@
+-1
+-2
+-3
+diff --git a/file3 b/file3
+deleted file mode 100644
+index 7289e35..0000000
+--- a/file3
++++ /dev/null
+@@ -1,4 +0,0 @@
+-A
+-B
+-1
+-2
+
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..8422d40 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++C
++D
+diff --git a/file0 b/file0
+index 01e79c3..b414108 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++4
++5
++6
+diff --git a/file2 b/file2
+deleted file mode 100644
+index 01e79c3..0000000
+--- a/file2
++++ /dev/null
+@@ -1,3 +0,0 @@
+-1
+-2
+-3
+
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+$
diff --git a/t/t4013/diff.log_-p_--diff-merges=first-parent_master b/t/t4013/diff.log_-p_--diff-merges=first-parent_master
new file mode 100644
index 0000000..9538a27
--- /dev/null
+++ b/t/t4013/diff.log_-p_--diff-merges=first-parent_master
@@ -0,0 +1,137 @@
+$ git log -p --diff-merges=first-parent master
+commit 59d314ad6f356dd08601a4cd5e530381da3e3c64
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+diff --git a/dir/sub b/dir/sub
+index cead32e..992913c 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -4,3 +4,5 @@ C
+ D
+ E
+ F
++1
++2
+diff --git a/file0 b/file0
+index b414108..10a8a9f 100644
+--- a/file0
++++ b/file0
+@@ -4,3 +4,6 @@
+ 4
+ 5
+ 6
++A
++B
++C
+
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..8422d40 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++C
++D
+diff --git a/file0 b/file0
+index 01e79c3..b414108 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++4
++5
++6
+diff --git a/file2 b/file2
+deleted file mode 100644
+index 01e79c3..0000000
+--- a/file2
++++ /dev/null
+@@ -1,3 +0,0 @@
+-1
+-2
+-3
+
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+$
diff --git a/t/t4013/diff.log_-p_--first-parent_master b/t/t4013/diff.log_-p_--first-parent_master
new file mode 100644
index 0000000..28840eb
--- /dev/null
+++ b/t/t4013/diff.log_-p_--first-parent_master
@@ -0,0 +1,100 @@
+$ git log -p --first-parent master
+commit 59d314ad6f356dd08601a4cd5e530381da3e3c64
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+diff --git a/dir/sub b/dir/sub
+index cead32e..992913c 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -4,3 +4,5 @@ C
+ D
+ E
+ F
++1
++2
+diff --git a/file0 b/file0
+index b414108..10a8a9f 100644
+--- a/file0
++++ b/file0
+@@ -4,3 +4,6 @@
+ 4
+ 5
+ 6
++A
++B
++C
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..8422d40 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++C
++D
+diff --git a/file0 b/file0
+index 01e79c3..b414108 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++4
++5
++6
+diff --git a/file2 b/file2
+deleted file mode 100644
+index 01e79c3..0000000
+--- a/file2
++++ /dev/null
+@@ -1,3 +0,0 @@
+-1
+-2
+-3
+
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+$
diff --git a/t/t4013/diff.log_-p_master b/t/t4013/diff.log_-p_master
new file mode 100644
index 0000000..bf1326d
--- /dev/null
+++ b/t/t4013/diff.log_-p_master
@@ -0,0 +1,115 @@
+$ git log -p master
+commit 59d314ad6f356dd08601a4cd5e530381da3e3c64
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..8422d40 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++C
++D
+diff --git a/file0 b/file0
+index 01e79c3..b414108 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++4
++5
++6
+diff --git a/file2 b/file2
+deleted file mode 100644
+index 01e79c3..0000000
+--- a/file2
++++ /dev/null
+@@ -1,3 +0,0 @@
+-1
+-2
+-3
+
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+$
diff --git a/t/t4013/diff.log_master b/t/t4013/diff.log_master
new file mode 100644
index 0000000..a8f6ce5
--- /dev/null
+++ b/t/t4013/diff.log_master
@@ -0,0 +1,34 @@
+$ git log master
+commit 59d314ad6f356dd08601a4cd5e530381da3e3c64
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+$
diff --git a/t/t4013/diff.noellipses-diff-tree_--root_--abbrev_initial b/t/t4013/diff.noellipses-diff-tree_--root_--abbrev_initial
new file mode 100644
index 0000000..4bdad40
--- /dev/null
+++ b/t/t4013/diff.noellipses-diff-tree_--root_--abbrev_initial
@@ -0,0 +1,6 @@
+$ git diff-tree --root --abbrev initial
+444ac553ac7612cc88969031b02b3767fb8a353a
+:000000 040000 0000000 da7a33f A dir
+:000000 100644 0000000 01e79c3 A file0
+:000000 100644 0000000 01e79c3 A file2
+$
diff --git a/t/t4013/diff.noellipses-diff-tree_--root_-r_--abbrev=4_initial b/t/t4013/diff.noellipses-diff-tree_--root_-r_--abbrev=4_initial
new file mode 100644
index 0000000..26fbfeb
--- /dev/null
+++ b/t/t4013/diff.noellipses-diff-tree_--root_-r_--abbrev=4_initial
@@ -0,0 +1,6 @@
+$ git diff-tree --root -r --abbrev=4 initial
+444ac553ac7612cc88969031b02b3767fb8a353a
+:000000 100644 0000 35d2 A dir/sub
+:000000 100644 0000 01e7 A file0
+:000000 100644 0000 01e7 A file2
+$
diff --git a/t/t4013/diff.noellipses-diff-tree_--root_-r_--abbrev_initial b/t/t4013/diff.noellipses-diff-tree_--root_-r_--abbrev_initial
new file mode 100644
index 0000000..2ac8561
--- /dev/null
+++ b/t/t4013/diff.noellipses-diff-tree_--root_-r_--abbrev_initial
@@ -0,0 +1,6 @@
+$ git diff-tree --root -r --abbrev initial
+444ac553ac7612cc88969031b02b3767fb8a353a
+:000000 100644 0000000 35d242b A dir/sub
+:000000 100644 0000000 01e79c3 A file0
+:000000 100644 0000000 01e79c3 A file2
+$
diff --git a/t/t4013/diff.noellipses-diff-tree_-c_--abbrev_master b/t/t4013/diff.noellipses-diff-tree_-c_--abbrev_master
new file mode 100644
index 0000000..bb80f01
--- /dev/null
+++ b/t/t4013/diff.noellipses-diff-tree_-c_--abbrev_master
@@ -0,0 +1,5 @@
+$ git diff-tree -c --abbrev master
+59d314ad6f356dd08601a4cd5e530381da3e3c64
+::100644 100644 100644 cead32e 7289e35 992913c MM dir/sub
+::100644 100644 100644 b414108 f4615da 10a8a9f MM file0
+$
diff --git a/t/t4013/diff.noellipses-diff_--no-index_--raw_--abbrev=4_dir2_dir b/t/t4013/diff.noellipses-diff_--no-index_--raw_--abbrev=4_dir2_dir
new file mode 100644
index 0000000..41b7baf
--- /dev/null
+++ b/t/t4013/diff.noellipses-diff_--no-index_--raw_--abbrev=4_dir2_dir
@@ -0,0 +1,3 @@
+$ git diff --no-index --raw --abbrev=4 dir2 dir
+:000000 100644 0000 0000 A dir/sub
+$
diff --git a/t/t4013/diff.noellipses-diff_--no-index_--raw_dir2_dir b/t/t4013/diff.noellipses-diff_--no-index_--raw_dir2_dir
new file mode 100644
index 0000000..0cf3a3e
--- /dev/null
+++ b/t/t4013/diff.noellipses-diff_--no-index_--raw_dir2_dir
@@ -0,0 +1,3 @@
+$ git diff --no-index --raw dir2 dir
+:000000 100644 0000000 0000000 A dir/sub
+$
diff --git a/t/t4013/diff.noellipses-diff_--patch-with-raw_-r_initial..side b/t/t4013/diff.noellipses-diff_--patch-with-raw_-r_initial..side
new file mode 100644
index 0000000..8d1f1e3
--- /dev/null
+++ b/t/t4013/diff.noellipses-diff_--patch-with-raw_-r_initial..side
@@ -0,0 +1,36 @@
+$ git diff --patch-with-raw -r initial..side
+:100644 100644 35d242b 7289e35 M dir/sub
+:100644 100644 01e79c3 f4615da M file0
+:000000 100644 0000000 7289e35 A file3
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+$
diff --git a/t/t4013/diff.noellipses-diff_--patch-with-raw_initial..side b/t/t4013/diff.noellipses-diff_--patch-with-raw_initial..side
new file mode 100644
index 0000000..50d8aee
--- /dev/null
+++ b/t/t4013/diff.noellipses-diff_--patch-with-raw_initial..side
@@ -0,0 +1,36 @@
+$ git diff --patch-with-raw initial..side
+:100644 100644 35d242b 7289e35 M dir/sub
+:100644 100644 01e79c3 f4615da M file0
+:000000 100644 0000000 7289e35 A file3
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+$
diff --git a/t/t4013/diff.noellipses-diff_--raw_--abbrev=4_initial b/t/t4013/diff.noellipses-diff_--raw_--abbrev=4_initial
new file mode 100644
index 0000000..8ae44d6
--- /dev/null
+++ b/t/t4013/diff.noellipses-diff_--raw_--abbrev=4_initial
@@ -0,0 +1,6 @@
+$ git diff --raw --abbrev=4 initial
+:100644 100644 35d2 9929 M dir/sub
+:100644 100644 01e7 10a8 M file0
+:000000 100644 0000 b1e6 A file1
+:100644 000000 01e7 0000 D file2
+$
diff --git a/t/t4013/diff.noellipses-diff_--raw_initial b/t/t4013/diff.noellipses-diff_--raw_initial
new file mode 100644
index 0000000..0175bfb
--- /dev/null
+++ b/t/t4013/diff.noellipses-diff_--raw_initial
@@ -0,0 +1,6 @@
+$ git diff --raw initial
+:100644 100644 35d242b 992913c M dir/sub
+:100644 100644 01e79c3 10a8a9f M file0
+:000000 100644 0000000 b1e6722 A file1
+:100644 000000 01e79c3 0000000 D file2
+$
diff --git a/t/t4013/diff.noellipses-show_--patch-with-raw_side b/t/t4013/diff.noellipses-show_--patch-with-raw_side
new file mode 100644
index 0000000..32fed3d
--- /dev/null
+++ b/t/t4013/diff.noellipses-show_--patch-with-raw_side
@@ -0,0 +1,42 @@
+$ git show --patch-with-raw side
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+
+:100644 100644 35d242b 7289e35 M dir/sub
+:100644 100644 01e79c3 f4615da M file0
+:000000 100644 0000000 7289e35 A file3
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+$
diff --git a/t/t4013/diff.noellipses-whatchanged_--root_master b/t/t4013/diff.noellipses-whatchanged_--root_master
new file mode 100644
index 0000000..c2cfd4e
--- /dev/null
+++ b/t/t4013/diff.noellipses-whatchanged_--root_master
@@ -0,0 +1,42 @@
+$ git whatchanged --root master
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+
+:100644 100644 35d242b 7289e35 M dir/sub
+:100644 100644 01e79c3 f4615da M file0
+:000000 100644 0000000 7289e35 A file3
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+
+:100644 100644 8422d40 cead32e M dir/sub
+:000000 100644 0000000 b1e6722 A file1
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+
+:100644 100644 35d242b 8422d40 M dir/sub
+:100644 100644 01e79c3 b414108 M file0
+:100644 000000 01e79c3 0000000 D file2
+
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+
+:000000 100644 0000000 35d242b A dir/sub
+:000000 100644 0000000 01e79c3 A file0
+:000000 100644 0000000 01e79c3 A file2
+$
diff --git a/t/t4013/diff.noellipses-whatchanged_-SF_master b/t/t4013/diff.noellipses-whatchanged_-SF_master
new file mode 100644
index 0000000..b36ce58
--- /dev/null
+++ b/t/t4013/diff.noellipses-whatchanged_-SF_master
@@ -0,0 +1,9 @@
+$ git whatchanged -SF master
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+
+:100644 100644 8422d40 cead32e M dir/sub
+$
diff --git a/t/t4013/diff.noellipses-whatchanged_master b/t/t4013/diff.noellipses-whatchanged_master
new file mode 100644
index 0000000..55e500f
--- /dev/null
+++ b/t/t4013/diff.noellipses-whatchanged_master
@@ -0,0 +1,32 @@
+$ git whatchanged master
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+
+:100644 100644 35d242b 7289e35 M dir/sub
+:100644 100644 01e79c3 f4615da M file0
+:000000 100644 0000000 7289e35 A file3
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+
+:100644 100644 8422d40 cead32e M dir/sub
+:000000 100644 0000000 b1e6722 A file1
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+
+:100644 100644 35d242b 8422d40 M dir/sub
+:100644 100644 01e79c3 b414108 M file0
+:100644 000000 01e79c3 0000000 D file2
+$
diff --git a/t/t4013/diff.rev-list_--children_HEAD b/t/t4013/diff.rev-list_--children_HEAD
new file mode 100644
index 0000000..e7f17d5
--- /dev/null
+++ b/t/t4013/diff.rev-list_--children_HEAD
@@ -0,0 +1,7 @@
+$ git rev-list --children HEAD
+59d314ad6f356dd08601a4cd5e530381da3e3c64
+c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a 59d314ad6f356dd08601a4cd5e530381da3e3c64
+9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0 59d314ad6f356dd08601a4cd5e530381da3e3c64
+1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+444ac553ac7612cc88969031b02b3767fb8a353a 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44 c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+$
diff --git a/t/t4013/diff.rev-list_--parents_HEAD b/t/t4013/diff.rev-list_--parents_HEAD
new file mode 100644
index 0000000..65d2a80
--- /dev/null
+++ b/t/t4013/diff.rev-list_--parents_HEAD
@@ -0,0 +1,7 @@
+$ git rev-list --parents HEAD
+59d314ad6f356dd08601a4cd5e530381da3e3c64 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0 c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a 444ac553ac7612cc88969031b02b3767fb8a353a
+9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44 444ac553ac7612cc88969031b02b3767fb8a353a
+444ac553ac7612cc88969031b02b3767fb8a353a
+$
diff --git a/t/t4013/diff.show_--first-parent_master b/t/t4013/diff.show_--first-parent_master
new file mode 100644
index 0000000..3dcbe47
--- /dev/null
+++ b/t/t4013/diff.show_--first-parent_master
@@ -0,0 +1,30 @@
+$ git show --first-parent master
+commit 59d314ad6f356dd08601a4cd5e530381da3e3c64
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+diff --git a/dir/sub b/dir/sub
+index cead32e..992913c 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -4,3 +4,5 @@ C
+ D
+ E
+ F
++1
++2
+diff --git a/file0 b/file0
+index b414108..10a8a9f 100644
+--- a/file0
++++ b/file0
+@@ -4,3 +4,6 @@
+ 4
+ 5
+ 6
++A
++B
++C
+$
diff --git a/t/t4013/diff.show_--patch-with-raw_side b/t/t4013/diff.show_--patch-with-raw_side
new file mode 100644
index 0000000..221b46a
--- /dev/null
+++ b/t/t4013/diff.show_--patch-with-raw_side
@@ -0,0 +1,42 @@
+$ git show --patch-with-raw side
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+
+:100644 100644 35d242b... 7289e35... M dir/sub
+:100644 100644 01e79c3... f4615da... M file0
+:000000 100644 0000000... 7289e35... A file3
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+$
diff --git a/t/t4013/diff.show_--patch-with-stat_--summary_side b/t/t4013/diff.show_--patch-with-stat_--summary_side
new file mode 100644
index 0000000..95a474e
--- /dev/null
+++ b/t/t4013/diff.show_--patch-with-stat_--summary_side
@@ -0,0 +1,44 @@
+$ git show --patch-with-stat --summary side
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file3 | 4 ++++
+ 3 files changed, 9 insertions(+)
+ create mode 100644 file3
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+$
diff --git a/t/t4013/diff.show_--patch-with-stat_side b/t/t4013/diff.show_--patch-with-stat_side
new file mode 100644
index 0000000..974e99b
--- /dev/null
+++ b/t/t4013/diff.show_--patch-with-stat_side
@@ -0,0 +1,43 @@
+$ git show --patch-with-stat side
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file3 | 4 ++++
+ 3 files changed, 9 insertions(+)
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+$
diff --git a/t/t4013/diff.show_--root_initial b/t/t4013/diff.show_--root_initial
new file mode 100644
index 0000000..8c89136
--- /dev/null
+++ b/t/t4013/diff.show_--root_initial
@@ -0,0 +1,34 @@
+$ git show --root initial
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+
+diff --git a/dir/sub b/dir/sub
+new file mode 100644
+index 0000000..35d242b
+--- /dev/null
++++ b/dir/sub
+@@ -0,0 +1,2 @@
++A
++B
+diff --git a/file0 b/file0
+new file mode 100644
+index 0000000..01e79c3
+--- /dev/null
++++ b/file0
+@@ -0,0 +1,3 @@
++1
++2
++3
+diff --git a/file2 b/file2
+new file mode 100644
+index 0000000..01e79c3
+--- /dev/null
++++ b/file2
+@@ -0,0 +1,3 @@
++1
++2
++3
+$
diff --git a/t/t4013/diff.show_--stat_--summary_side b/t/t4013/diff.show_--stat_--summary_side
new file mode 100644
index 0000000..a71492f
--- /dev/null
+++ b/t/t4013/diff.show_--stat_--summary_side
@@ -0,0 +1,13 @@
+$ git show --stat --summary side
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file3 | 4 ++++
+ 3 files changed, 9 insertions(+)
+ create mode 100644 file3
+$
diff --git a/t/t4013/diff.show_--stat_side b/t/t4013/diff.show_--stat_side
new file mode 100644
index 0000000..9be7124
--- /dev/null
+++ b/t/t4013/diff.show_--stat_side
@@ -0,0 +1,12 @@
+$ git show --stat side
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file3 | 4 ++++
+ 3 files changed, 9 insertions(+)
+$
diff --git a/t/t4013/diff.show_-c_master b/t/t4013/diff.show_-c_master
new file mode 100644
index 0000000..81aba8d
--- /dev/null
+++ b/t/t4013/diff.show_-c_master
@@ -0,0 +1,36 @@
+$ git show -c master
+commit 59d314ad6f356dd08601a4cd5e530381da3e3c64
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+diff --combined dir/sub
+index cead32e,7289e35..992913c
+--- a/dir/sub
++++ b/dir/sub
+@@@ -1,6 -1,4 +1,8 @@@
+ A
+ B
+ +C
+ +D
+ +E
+ +F
++ 1
++ 2
+diff --combined file0
+index b414108,f4615da..10a8a9f
+--- a/file0
++++ b/file0
+@@@ -1,6 -1,6 +1,9 @@@
+ 1
+ 2
+ 3
+ +4
+ +5
+ +6
++ A
++ B
++ C
+$
diff --git a/t/t4013/diff.show_-m_master b/t/t4013/diff.show_-m_master
new file mode 100644
index 0000000..4ea2ee4
--- /dev/null
+++ b/t/t4013/diff.show_-m_master
@@ -0,0 +1,93 @@
+$ git show -m master
+commit 59d314ad6f356dd08601a4cd5e530381da3e3c64 (from 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0)
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+diff --git a/dir/sub b/dir/sub
+index cead32e..992913c 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -4,3 +4,5 @@ C
+ D
+ E
+ F
++1
++2
+diff --git a/file0 b/file0
+index b414108..10a8a9f 100644
+--- a/file0
++++ b/file0
+@@ -4,3 +4,6 @@
+ 4
+ 5
+ 6
++A
++B
++C
+
+commit 59d314ad6f356dd08601a4cd5e530381da3e3c64 (from c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a)
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+diff --git a/dir/sub b/dir/sub
+index 7289e35..992913c 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,4 +1,8 @@
+ A
+ B
++C
++D
++E
++F
+ 1
+ 2
+diff --git a/file0 b/file0
+index f4615da..10a8a9f 100644
+--- a/file0
++++ b/file0
+@@ -1,6 +1,9 @@
+ 1
+ 2
+ 3
++4
++5
++6
+ A
+ B
+ C
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
+diff --git a/file2 b/file2
+deleted file mode 100644
+index 01e79c3..0000000
+--- a/file2
++++ /dev/null
+@@ -1,3 +0,0 @@
+-1
+-2
+-3
+diff --git a/file3 b/file3
+deleted file mode 100644
+index 7289e35..0000000
+--- a/file3
++++ /dev/null
+@@ -1,4 +0,0 @@
+-A
+-B
+-1
+-2
+$
diff --git a/t/t4013/diff.show_initial b/t/t4013/diff.show_initial
new file mode 100644
index 0000000..4c4066a
--- /dev/null
+++ b/t/t4013/diff.show_initial
@@ -0,0 +1,7 @@
+$ git show initial
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+$
diff --git a/t/t4013/diff.show_master b/t/t4013/diff.show_master
new file mode 100644
index 0000000..fb08ce0
--- /dev/null
+++ b/t/t4013/diff.show_master
@@ -0,0 +1,36 @@
+$ git show master
+commit 59d314ad6f356dd08601a4cd5e530381da3e3c64
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+diff --cc dir/sub
+index cead32e,7289e35..992913c
+--- a/dir/sub
++++ b/dir/sub
+@@@ -1,6 -1,4 +1,8 @@@
+ A
+ B
+ +C
+ +D
+ +E
+ +F
++ 1
++ 2
+diff --cc file0
+index b414108,f4615da..10a8a9f
+--- a/file0
++++ b/file0
+@@@ -1,6 -1,6 +1,9 @@@
+ 1
+ 2
+ 3
+ +4
+ +5
+ +6
++ A
++ B
++ C
+$
diff --git a/t/t4013/diff.show_side b/t/t4013/diff.show_side
new file mode 100644
index 0000000..530a073
--- /dev/null
+++ b/t/t4013/diff.show_side
@@ -0,0 +1,38 @@
+$ git show side
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+$
diff --git a/t/t4013/diff.whatchanged_--patch-with-stat_--summary_master_--_dir_ b/t/t4013/diff.whatchanged_--patch-with-stat_--summary_master_--_dir_
new file mode 100644
index 0000000..c8b6af2
--- /dev/null
+++ b/t/t4013/diff.whatchanged_--patch-with-stat_--summary_master_--_dir_
@@ -0,0 +1,61 @@
+$ git whatchanged --patch-with-stat --summary master -- dir/
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+---
+ dir/sub | 2 ++
+ 1 file changed, 2 insertions(+)
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+---
+ dir/sub | 2 ++
+ 1 file changed, 2 insertions(+)
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+---
+ dir/sub | 2 ++
+ 1 file changed, 2 insertions(+)
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..8422d40 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++C
++D
+$
diff --git a/t/t4013/diff.whatchanged_--patch-with-stat_master b/t/t4013/diff.whatchanged_--patch-with-stat_master
new file mode 100644
index 0000000..1ac431b
--- /dev/null
+++ b/t/t4013/diff.whatchanged_--patch-with-stat_master
@@ -0,0 +1,116 @@
+$ git whatchanged --patch-with-stat master
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file3 | 4 ++++
+ 3 files changed, 9 insertions(+)
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+---
+ dir/sub | 2 ++
+ file1 | 3 +++
+ 2 files changed, 5 insertions(+)
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file2 | 3 ---
+ 3 files changed, 5 insertions(+), 3 deletions(-)
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..8422d40 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++C
++D
+diff --git a/file0 b/file0
+index 01e79c3..b414108 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++4
++5
++6
+diff --git a/file2 b/file2
+deleted file mode 100644
+index 01e79c3..0000000
+--- a/file2
++++ /dev/null
+@@ -1,3 +0,0 @@
+-1
+-2
+-3
+$
diff --git a/t/t4013/diff.whatchanged_--patch-with-stat_master_--_dir_ b/t/t4013/diff.whatchanged_--patch-with-stat_master_--_dir_
new file mode 100644
index 0000000..b30c285
--- /dev/null
+++ b/t/t4013/diff.whatchanged_--patch-with-stat_master_--_dir_
@@ -0,0 +1,61 @@
+$ git whatchanged --patch-with-stat master -- dir/
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+---
+ dir/sub | 2 ++
+ 1 file changed, 2 insertions(+)
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+---
+ dir/sub | 2 ++
+ 1 file changed, 2 insertions(+)
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+---
+ dir/sub | 2 ++
+ 1 file changed, 2 insertions(+)
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..8422d40 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++C
++D
+$
diff --git a/t/t4013/diff.whatchanged_--root_--cc_--patch-with-stat_--summary_master b/t/t4013/diff.whatchanged_--root_--cc_--patch-with-stat_--summary_master
new file mode 100644
index 0000000..30aae78
--- /dev/null
+++ b/t/t4013/diff.whatchanged_--root_--cc_--patch-with-stat_--summary_master
@@ -0,0 +1,199 @@
+$ git whatchanged --root --cc --patch-with-stat --summary master
+commit 59d314ad6f356dd08601a4cd5e530381da3e3c64
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+ dir/sub | 2 ++
+ file0 | 3 +++
+ 2 files changed, 5 insertions(+)
+
+diff --cc dir/sub
+index cead32e,7289e35..992913c
+--- a/dir/sub
++++ b/dir/sub
+@@@ -1,6 -1,4 +1,8 @@@
+ A
+ B
+ +C
+ +D
+ +E
+ +F
++ 1
++ 2
+diff --cc file0
+index b414108,f4615da..10a8a9f
+--- a/file0
++++ b/file0
+@@@ -1,6 -1,6 +1,9 @@@
+ 1
+ 2
+ 3
+ +4
+ +5
+ +6
++ A
++ B
++ C
+
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file3 | 4 ++++
+ 3 files changed, 9 insertions(+)
+ create mode 100644 file3
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+---
+ dir/sub | 2 ++
+ file1 | 3 +++
+ 2 files changed, 5 insertions(+)
+ create mode 100644 file1
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file2 | 3 ---
+ 3 files changed, 5 insertions(+), 3 deletions(-)
+ delete mode 100644 file2
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..8422d40 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++C
++D
+diff --git a/file0 b/file0
+index 01e79c3..b414108 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++4
++5
++6
+diff --git a/file2 b/file2
+deleted file mode 100644
+index 01e79c3..0000000
+--- a/file2
++++ /dev/null
+@@ -1,3 +0,0 @@
+-1
+-2
+-3
+
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file2 | 3 +++
+ 3 files changed, 8 insertions(+)
+ create mode 100644 dir/sub
+ create mode 100644 file0
+ create mode 100644 file2
+
+diff --git a/dir/sub b/dir/sub
+new file mode 100644
+index 0000000..35d242b
+--- /dev/null
++++ b/dir/sub
+@@ -0,0 +1,2 @@
++A
++B
+diff --git a/file0 b/file0
+new file mode 100644
+index 0000000..01e79c3
+--- /dev/null
++++ b/file0
+@@ -0,0 +1,3 @@
++1
++2
++3
+diff --git a/file2 b/file2
+new file mode 100644
+index 0000000..01e79c3
+--- /dev/null
++++ b/file2
+@@ -0,0 +1,3 @@
++1
++2
++3
+$
diff --git a/t/t4013/diff.whatchanged_--root_--patch-with-stat_--summary_master b/t/t4013/diff.whatchanged_--root_--patch-with-stat_--summary_master
new file mode 100644
index 0000000..db90e51
--- /dev/null
+++ b/t/t4013/diff.whatchanged_--root_--patch-with-stat_--summary_master
@@ -0,0 +1,160 @@
+$ git whatchanged --root --patch-with-stat --summary master
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file3 | 4 ++++
+ 3 files changed, 9 insertions(+)
+ create mode 100644 file3
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+---
+ dir/sub | 2 ++
+ file1 | 3 +++
+ 2 files changed, 5 insertions(+)
+ create mode 100644 file1
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file2 | 3 ---
+ 3 files changed, 5 insertions(+), 3 deletions(-)
+ delete mode 100644 file2
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..8422d40 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++C
++D
+diff --git a/file0 b/file0
+index 01e79c3..b414108 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++4
++5
++6
+diff --git a/file2 b/file2
+deleted file mode 100644
+index 01e79c3..0000000
+--- a/file2
++++ /dev/null
+@@ -1,3 +0,0 @@
+-1
+-2
+-3
+
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file2 | 3 +++
+ 3 files changed, 8 insertions(+)
+ create mode 100644 dir/sub
+ create mode 100644 file0
+ create mode 100644 file2
+
+diff --git a/dir/sub b/dir/sub
+new file mode 100644
+index 0000000..35d242b
+--- /dev/null
++++ b/dir/sub
+@@ -0,0 +1,2 @@
++A
++B
+diff --git a/file0 b/file0
+new file mode 100644
+index 0000000..01e79c3
+--- /dev/null
++++ b/file0
+@@ -0,0 +1,3 @@
++1
++2
++3
+diff --git a/file2 b/file2
+new file mode 100644
+index 0000000..01e79c3
+--- /dev/null
++++ b/file2
+@@ -0,0 +1,3 @@
++1
++2
++3
+$
diff --git a/t/t4013/diff.whatchanged_--root_--patch-with-stat_master b/t/t4013/diff.whatchanged_--root_--patch-with-stat_master
new file mode 100644
index 0000000..9a6cc92
--- /dev/null
+++ b/t/t4013/diff.whatchanged_--root_--patch-with-stat_master
@@ -0,0 +1,154 @@
+$ git whatchanged --root --patch-with-stat master
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file3 | 4 ++++
+ 3 files changed, 9 insertions(+)
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+---
+ dir/sub | 2 ++
+ file1 | 3 +++
+ 2 files changed, 5 insertions(+)
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file2 | 3 ---
+ 3 files changed, 5 insertions(+), 3 deletions(-)
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..8422d40 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++C
++D
+diff --git a/file0 b/file0
+index 01e79c3..b414108 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++4
++5
++6
+diff --git a/file2 b/file2
+deleted file mode 100644
+index 01e79c3..0000000
+--- a/file2
++++ /dev/null
+@@ -1,3 +0,0 @@
+-1
+-2
+-3
+
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file2 | 3 +++
+ 3 files changed, 8 insertions(+)
+
+diff --git a/dir/sub b/dir/sub
+new file mode 100644
+index 0000000..35d242b
+--- /dev/null
++++ b/dir/sub
+@@ -0,0 +1,2 @@
++A
++B
+diff --git a/file0 b/file0
+new file mode 100644
+index 0000000..01e79c3
+--- /dev/null
++++ b/file0
+@@ -0,0 +1,3 @@
++1
++2
++3
+diff --git a/file2 b/file2
+new file mode 100644
+index 0000000..01e79c3
+--- /dev/null
++++ b/file2
+@@ -0,0 +1,3 @@
++1
++2
++3
+$
diff --git a/t/t4013/diff.whatchanged_--root_-c_--patch-with-stat_--summary_master b/t/t4013/diff.whatchanged_--root_-c_--patch-with-stat_--summary_master
new file mode 100644
index 0000000..d1d32bd
--- /dev/null
+++ b/t/t4013/diff.whatchanged_--root_-c_--patch-with-stat_--summary_master
@@ -0,0 +1,199 @@
+$ git whatchanged --root -c --patch-with-stat --summary master
+commit 59d314ad6f356dd08601a4cd5e530381da3e3c64
+Merge: 9a6d494 c7a2ab9
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:04:00 2006 +0000
+
+ Merge branch 'side'
+
+ dir/sub | 2 ++
+ file0 | 3 +++
+ 2 files changed, 5 insertions(+)
+
+diff --combined dir/sub
+index cead32e,7289e35..992913c
+--- a/dir/sub
++++ b/dir/sub
+@@@ -1,6 -1,4 +1,8 @@@
+ A
+ B
+ +C
+ +D
+ +E
+ +F
++ 1
++ 2
+diff --combined file0
+index b414108,f4615da..10a8a9f
+--- a/file0
++++ b/file0
+@@@ -1,6 -1,6 +1,9 @@@
+ 1
+ 2
+ 3
+ +4
+ +5
+ +6
++ A
++ B
++ C
+
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file3 | 4 ++++
+ 3 files changed, 9 insertions(+)
+ create mode 100644 file3
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+---
+ dir/sub | 2 ++
+ file1 | 3 +++
+ 2 files changed, 5 insertions(+)
+ create mode 100644 file1
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file2 | 3 ---
+ 3 files changed, 5 insertions(+), 3 deletions(-)
+ delete mode 100644 file2
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..8422d40 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++C
++D
+diff --git a/file0 b/file0
+index 01e79c3..b414108 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++4
++5
++6
+diff --git a/file2 b/file2
+deleted file mode 100644
+index 01e79c3..0000000
+--- a/file2
++++ /dev/null
+@@ -1,3 +0,0 @@
+-1
+-2
+-3
+
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+---
+ dir/sub | 2 ++
+ file0 | 3 +++
+ file2 | 3 +++
+ 3 files changed, 8 insertions(+)
+ create mode 100644 dir/sub
+ create mode 100644 file0
+ create mode 100644 file2
+
+diff --git a/dir/sub b/dir/sub
+new file mode 100644
+index 0000000..35d242b
+--- /dev/null
++++ b/dir/sub
+@@ -0,0 +1,2 @@
++A
++B
+diff --git a/file0 b/file0
+new file mode 100644
+index 0000000..01e79c3
+--- /dev/null
++++ b/file0
+@@ -0,0 +1,3 @@
++1
++2
++3
+diff --git a/file2 b/file2
+new file mode 100644
+index 0000000..01e79c3
+--- /dev/null
++++ b/file2
+@@ -0,0 +1,3 @@
++1
++2
++3
+$
diff --git a/t/t4013/diff.whatchanged_--root_-p_master b/t/t4013/diff.whatchanged_--root_-p_master
new file mode 100644
index 0000000..ebf1f06
--- /dev/null
+++ b/t/t4013/diff.whatchanged_--root_-p_master
@@ -0,0 +1,135 @@
+$ git whatchanged --root -p master
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..8422d40 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++C
++D
+diff --git a/file0 b/file0
+index 01e79c3..b414108 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++4
++5
++6
+diff --git a/file2 b/file2
+deleted file mode 100644
+index 01e79c3..0000000
+--- a/file2
++++ /dev/null
+@@ -1,3 +0,0 @@
+-1
+-2
+-3
+
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+
+diff --git a/dir/sub b/dir/sub
+new file mode 100644
+index 0000000..35d242b
+--- /dev/null
++++ b/dir/sub
+@@ -0,0 +1,2 @@
++A
++B
+diff --git a/file0 b/file0
+new file mode 100644
+index 0000000..01e79c3
+--- /dev/null
++++ b/file0
+@@ -0,0 +1,3 @@
++1
++2
++3
+diff --git a/file2 b/file2
+new file mode 100644
+index 0000000..01e79c3
+--- /dev/null
++++ b/file2
+@@ -0,0 +1,3 @@
++1
++2
++3
+$
diff --git a/t/t4013/diff.whatchanged_--root_master b/t/t4013/diff.whatchanged_--root_master
new file mode 100644
index 0000000..a405cb6
--- /dev/null
+++ b/t/t4013/diff.whatchanged_--root_master
@@ -0,0 +1,42 @@
+$ git whatchanged --root master
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+
+:100644 100644 35d242b... 7289e35... M dir/sub
+:100644 100644 01e79c3... f4615da... M file0
+:000000 100644 0000000... 7289e35... A file3
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+
+:100644 100644 8422d40... cead32e... M dir/sub
+:000000 100644 0000000... b1e6722... A file1
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+
+:100644 100644 35d242b... 8422d40... M dir/sub
+:100644 100644 01e79c3... b414108... M file0
+:100644 000000 01e79c3... 0000000... D file2
+
+commit 444ac553ac7612cc88969031b02b3767fb8a353a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:00:00 2006 +0000
+
+ Initial
+
+:000000 100644 0000000... 35d242b... A dir/sub
+:000000 100644 0000000... 01e79c3... A file0
+:000000 100644 0000000... 01e79c3... A file2
+$
diff --git a/t/t4013/diff.whatchanged_-SF_-p_master b/t/t4013/diff.whatchanged_-SF_-p_master
new file mode 100644
index 0000000..f39da84
--- /dev/null
+++ b/t/t4013/diff.whatchanged_-SF_-p_master
@@ -0,0 +1,18 @@
+$ git whatchanged -SF -p master
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+$
diff --git a/t/t4013/diff.whatchanged_-SF_master b/t/t4013/diff.whatchanged_-SF_master
new file mode 100644
index 0000000..0499321
--- /dev/null
+++ b/t/t4013/diff.whatchanged_-SF_master
@@ -0,0 +1,9 @@
+$ git whatchanged -SF master
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+
+:100644 100644 8422d40... cead32e... M dir/sub
+$
diff --git a/t/t4013/diff.whatchanged_-p_master b/t/t4013/diff.whatchanged_-p_master
new file mode 100644
index 0000000..f18d432
--- /dev/null
+++ b/t/t4013/diff.whatchanged_-p_master
@@ -0,0 +1,102 @@
+$ git whatchanged -p master
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..7289e35 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++1
++2
+diff --git a/file0 b/file0
+index 01e79c3..f4615da 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++A
++B
++C
+diff --git a/file3 b/file3
+new file mode 100644
+index 0000000..7289e35
+--- /dev/null
++++ b/file3
+@@ -0,0 +1,4 @@
++A
++B
++1
++2
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+
+diff --git a/dir/sub b/dir/sub
+index 8422d40..cead32e 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -2,3 +2,5 @@ A
+ B
+ C
+ D
++E
++F
+diff --git a/file1 b/file1
+new file mode 100644
+index 0000000..b1e6722
+--- /dev/null
++++ b/file1
+@@ -0,0 +1,3 @@
++A
++B
++C
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+
+diff --git a/dir/sub b/dir/sub
+index 35d242b..8422d40 100644
+--- a/dir/sub
++++ b/dir/sub
+@@ -1,2 +1,4 @@
+ A
+ B
++C
++D
+diff --git a/file0 b/file0
+index 01e79c3..b414108 100644
+--- a/file0
++++ b/file0
+@@ -1,3 +1,6 @@
+ 1
+ 2
+ 3
++4
++5
++6
+diff --git a/file2 b/file2
+deleted file mode 100644
+index 01e79c3..0000000
+--- a/file2
++++ /dev/null
+@@ -1,3 +0,0 @@
+-1
+-2
+-3
+$
diff --git a/t/t4013/diff.whatchanged_master b/t/t4013/diff.whatchanged_master
new file mode 100644
index 0000000..cd3bcc2
--- /dev/null
+++ b/t/t4013/diff.whatchanged_master
@@ -0,0 +1,32 @@
+$ git whatchanged master
+commit c7a2ab9e8eac7b117442a607d5a9b3950ae34d5a
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:03:00 2006 +0000
+
+ Side
+
+:100644 100644 35d242b... 7289e35... M dir/sub
+:100644 100644 01e79c3... f4615da... M file0
+:000000 100644 0000000... 7289e35... A file3
+
+commit 9a6d4949b6b76956d9d5e26f2791ec2ceff5fdc0
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:02:00 2006 +0000
+
+ Third
+
+:100644 100644 8422d40... cead32e... M dir/sub
+:000000 100644 0000000... b1e6722... A file1
+
+commit 1bde4ae5f36c8d9abe3a0fce0c6aab3c4a12fe44
+Author: A U Thor <author@example.com>
+Date: Mon Jun 26 00:01:00 2006 +0000
+
+ Second
+
+ This is the second commit.
+
+:100644 100644 35d242b... 8422d40... M dir/sub
+:100644 100644 01e79c3... b414108... M file0
+:100644 000000 01e79c3... 0000000... D file2
+$
diff --git a/t/t4014-format-patch.sh b/t/t4014-format-patch.sh
new file mode 100755
index 0000000..5ced27e
--- /dev/null
+++ b/t/t4014-format-patch.sh
@@ -0,0 +1,2454 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Junio C Hamano
+#
+
+test_description='various format-patch tests'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-terminal.sh
+
+test_expect_success setup '
+ test_write_lines 1 2 3 4 5 6 7 8 9 10 >file &&
+ cat file >elif &&
+ git add file elif &&
+ test_tick &&
+ git commit -m Initial &&
+ git checkout -b side &&
+
+ test_write_lines 1 2 5 6 A B C 7 8 9 10 >file &&
+ test_chmod +x elif &&
+ test_tick &&
+ git commit -m "Side changes #1" &&
+
+ test_write_lines D E F >>file &&
+ git update-index file &&
+ test_tick &&
+ git commit -m "Side changes #2" &&
+ git tag C2 &&
+
+ test_write_lines 5 6 1 2 3 A 4 B C 7 8 9 10 D E F >file &&
+ git update-index file &&
+ test_tick &&
+ git commit -m "Side changes #3 with \\n backslash-n in it." &&
+
+ git checkout main &&
+ git diff-tree -p C2 >patch &&
+ git apply --index <patch &&
+ test_tick &&
+ git commit -m "Main accepts moral equivalent of #2" &&
+
+ git checkout side &&
+ git checkout -b patchid &&
+ test_write_lines 5 6 1 2 3 A 4 B C 7 8 9 10 D E F >file2 &&
+ test_write_lines 1 2 3 A 4 B C 7 8 9 10 D E F 5 6 >file3 &&
+ test_write_lines 8 9 10 >file &&
+ git add file file2 file3 &&
+ test_tick &&
+ git commit -m "patchid 1" &&
+ test_write_lines 4 A B 7 8 9 10 >file2 &&
+ test_write_lines 8 9 10 5 6 >file3 &&
+ git add file2 file3 &&
+ test_tick &&
+ git commit -m "patchid 2" &&
+ test_write_lines 10 5 6 >file &&
+ git add file &&
+ test_tick &&
+ git commit -m "patchid 3" &&
+
+ git checkout -b empty main &&
+ test_tick &&
+ git commit --allow-empty -m "empty commit" &&
+
+ git checkout main
+'
+
+test_expect_success 'format-patch --ignore-if-in-upstream' '
+ git format-patch --stdout main..side >patch0 &&
+ grep "^From " patch0 >from0 &&
+ test_line_count = 3 from0
+'
+
+test_expect_success 'format-patch --ignore-if-in-upstream' '
+ git format-patch --stdout \
+ --ignore-if-in-upstream main..side >patch1 &&
+ grep "^From " patch1 >from1 &&
+ test_line_count = 2 from1
+'
+
+test_expect_success 'format-patch --ignore-if-in-upstream handles tags' '
+ git tag -a v1 -m tag side &&
+ git tag -a v2 -m tag main &&
+ git format-patch --stdout --ignore-if-in-upstream v2..v1 >patch1 &&
+ grep "^From " patch1 >from1 &&
+ test_line_count = 2 from1
+'
+
+test_expect_success "format-patch doesn't consider merge commits" '
+ git checkout -b feature main &&
+ echo "Another line" >>file &&
+ test_tick &&
+ git commit -am "Feature branch change #1" &&
+ echo "Yet another line" >>file &&
+ test_tick &&
+ git commit -am "Feature branch change #2" &&
+ git checkout -b merger main &&
+ test_tick &&
+ git merge --no-ff feature &&
+ git format-patch -3 --stdout >patch &&
+ grep "^From " patch >from &&
+ test_line_count = 3 from
+'
+
+test_expect_success 'format-patch result applies' '
+ git checkout -b rebuild-0 main &&
+ git am -3 patch0 &&
+ git rev-list main.. >list &&
+ test_line_count = 2 list
+'
+
+test_expect_success 'format-patch --ignore-if-in-upstream result applies' '
+ git checkout -b rebuild-1 main &&
+ git am -3 patch1 &&
+ git rev-list main.. >list &&
+ test_line_count = 2 list
+'
+
+test_expect_success 'commit did not screw up the log message' '
+ git cat-file commit side >actual &&
+ grep "^Side .* with .* backslash-n" actual
+'
+
+test_expect_success 'format-patch did not screw up the log message' '
+ grep "^Subject: .*Side changes #3 with .* backslash-n" patch0 &&
+ grep "^Subject: .*Side changes #3 with .* backslash-n" patch1
+'
+
+test_expect_success 'replay did not screw up the log message' '
+ git cat-file commit rebuild-1 >actual &&
+ grep "^Side .* with .* backslash-n" actual
+'
+
+test_expect_success 'format-patch empty commit' '
+ git format-patch --stdout main..empty >empty &&
+ grep "^From " empty >from &&
+ test_line_count = 1 from
+'
+
+test_expect_success 'extra headers' '
+ git config format.headers "To: R E Cipient <rcipient@example.com>
+" &&
+ git config --add format.headers "Cc: S E Cipient <scipient@example.com>
+" &&
+ git format-patch --stdout main..side >patch2 &&
+ sed -e "/^\$/q" patch2 >hdrs2 &&
+ grep "^To: R E Cipient <rcipient@example.com>\$" hdrs2 &&
+ grep "^Cc: S E Cipient <scipient@example.com>\$" hdrs2
+'
+
+test_expect_success 'extra headers without newlines' '
+ git config --replace-all format.headers "To: R E Cipient <rcipient@example.com>" &&
+ git config --add format.headers "Cc: S E Cipient <scipient@example.com>" &&
+ git format-patch --stdout main..side >patch3 &&
+ sed -e "/^\$/q" patch3 >hdrs3 &&
+ grep "^To: R E Cipient <rcipient@example.com>\$" hdrs3 &&
+ grep "^Cc: S E Cipient <scipient@example.com>\$" hdrs3
+'
+
+test_expect_success 'extra headers with multiple To:s' '
+ git config --replace-all format.headers "To: R E Cipient <rcipient@example.com>" &&
+ git config --add format.headers "To: S E Cipient <scipient@example.com>" &&
+ git format-patch --stdout main..side >patch4 &&
+ sed -e "/^\$/q" patch4 >hdrs4 &&
+ grep "^To: R E Cipient <rcipient@example.com>,\$" hdrs4 &&
+ grep "^ *S E Cipient <scipient@example.com>\$" hdrs4
+'
+
+test_expect_success 'additional command line cc (ascii)' '
+ git config --replace-all format.headers "Cc: R E Cipient <rcipient@example.com>" &&
+ git format-patch --cc="S E Cipient <scipient@example.com>" --stdout main..side >patch5 &&
+ sed -e "/^\$/q" patch5 >hdrs5 &&
+ grep "^Cc: R E Cipient <rcipient@example.com>,\$" hdrs5 &&
+ grep "^ *S E Cipient <scipient@example.com>\$" hdrs5
+'
+
+test_expect_failure 'additional command line cc (rfc822)' '
+ git config --replace-all format.headers "Cc: R E Cipient <rcipient@example.com>" &&
+ git format-patch --cc="S. E. Cipient <scipient@example.com>" --stdout main..side >patch5 &&
+ sed -e "/^\$/q" patch5 >hdrs5 &&
+ grep "^Cc: R E Cipient <rcipient@example.com>,\$" hdrs5 &&
+ grep "^ *\"S. E. Cipient\" <scipient@example.com>\$" hdrs5
+'
+
+test_expect_success 'command line headers' '
+ git config --unset-all format.headers &&
+ git format-patch --add-header="Cc: R E Cipient <rcipient@example.com>" --stdout main..side >patch6 &&
+ sed -e "/^\$/q" patch6 >hdrs6 &&
+ grep "^Cc: R E Cipient <rcipient@example.com>\$" hdrs6
+'
+
+test_expect_success 'configuration headers and command line headers' '
+ git config --replace-all format.headers "Cc: R E Cipient <rcipient@example.com>" &&
+ git format-patch --add-header="Cc: S E Cipient <scipient@example.com>" --stdout main..side >patch7 &&
+ sed -e "/^\$/q" patch7 >hdrs7 &&
+ grep "^Cc: R E Cipient <rcipient@example.com>,\$" hdrs7 &&
+ grep "^ *S E Cipient <scipient@example.com>\$" hdrs7
+'
+
+test_expect_success 'command line To: header (ascii)' '
+ git config --unset-all format.headers &&
+ git format-patch --to="R E Cipient <rcipient@example.com>" --stdout main..side >patch8 &&
+ sed -e "/^\$/q" patch8 >hdrs8 &&
+ grep "^To: R E Cipient <rcipient@example.com>\$" hdrs8
+'
+
+test_expect_failure 'command line To: header (rfc822)' '
+ git format-patch --to="R. E. Cipient <rcipient@example.com>" --stdout main..side >patch8 &&
+ sed -e "/^\$/q" patch8 >hdrs8 &&
+ grep "^To: \"R. E. Cipient\" <rcipient@example.com>\$" hdrs8
+'
+
+test_expect_failure 'command line To: header (rfc2047)' '
+ git format-patch --to="R Ä Cipient <rcipient@example.com>" --stdout main..side >patch8 &&
+ sed -e "/^\$/q" patch8 >hdrs8 &&
+ grep "^To: =?UTF-8?q?R=20=C3=84=20Cipient?= <rcipient@example.com>\$" hdrs8
+'
+
+test_expect_success 'configuration To: header (ascii)' '
+ git config format.to "R E Cipient <rcipient@example.com>" &&
+ git format-patch --stdout main..side >patch9 &&
+ sed -e "/^\$/q" patch9 >hdrs9 &&
+ grep "^To: R E Cipient <rcipient@example.com>\$" hdrs9
+'
+
+test_expect_failure 'configuration To: header (rfc822)' '
+ git config format.to "R. E. Cipient <rcipient@example.com>" &&
+ git format-patch --stdout main..side >patch9 &&
+ sed -e "/^\$/q" patch9 >hdrs9 &&
+ grep "^To: \"R. E. Cipient\" <rcipient@example.com>\$" hdrs9
+'
+
+test_expect_failure 'configuration To: header (rfc2047)' '
+ git config format.to "R Ä Cipient <rcipient@example.com>" &&
+ git format-patch --stdout main..side >patch9 &&
+ sed -e "/^\$/q" patch9 >hdrs9 &&
+ grep "^To: =?UTF-8?q?R=20=C3=84=20Cipient?= <rcipient@example.com>\$" hdrs9
+'
+
+# check_patch <patch>: Verify that <patch> looks like a half-sane
+# patch email to avoid a false positive with !grep
+check_patch () {
+ grep -e "^From:" "$1" &&
+ grep -e "^Date:" "$1" &&
+ grep -e "^Subject:" "$1"
+}
+
+test_expect_success 'format.from=false' '
+ git -c format.from=false format-patch --stdout main..side >patch &&
+ sed -e "/^\$/q" patch >hdrs &&
+ check_patch patch &&
+ ! grep "^From: C O Mitter <committer@example.com>\$" hdrs
+'
+
+test_expect_success 'format.from=true' '
+ git -c format.from=true format-patch --stdout main..side >patch &&
+ sed -e "/^\$/q" patch >hdrs &&
+ check_patch hdrs &&
+ grep "^From: C O Mitter <committer@example.com>\$" hdrs
+'
+
+test_expect_success 'format.from with address' '
+ git -c format.from="F R Om <from@example.com>" format-patch --stdout main..side >patch &&
+ sed -e "/^\$/q" patch >hdrs &&
+ check_patch hdrs &&
+ grep "^From: F R Om <from@example.com>\$" hdrs
+'
+
+test_expect_success '--no-from overrides format.from' '
+ git -c format.from="F R Om <from@example.com>" format-patch --no-from --stdout main..side >patch &&
+ sed -e "/^\$/q" patch >hdrs &&
+ check_patch hdrs &&
+ ! grep "^From: F R Om <from@example.com>\$" hdrs
+'
+
+test_expect_success '--from overrides format.from' '
+ git -c format.from="F R Om <from@example.com>" format-patch --from --stdout main..side >patch &&
+ sed -e "/^\$/q" patch >hdrs &&
+ check_patch hdrs &&
+ ! grep "^From: F R Om <from@example.com>\$" hdrs
+'
+
+test_expect_success '--no-to overrides config.to' '
+ git config --replace-all format.to \
+ "R E Cipient <rcipient@example.com>" &&
+ git format-patch --no-to --stdout main..side >patch10 &&
+ sed -e "/^\$/q" patch10 >hdrs10 &&
+ check_patch hdrs10 &&
+ ! grep "^To: R E Cipient <rcipient@example.com>\$" hdrs10
+'
+
+test_expect_success '--no-to and --to replaces config.to' '
+ git config --replace-all format.to \
+ "Someone <someone@out.there>" &&
+ git format-patch --no-to --to="Someone Else <else@out.there>" \
+ --stdout main..side >patch11 &&
+ sed -e "/^\$/q" patch11 >hdrs11 &&
+ check_patch hdrs11 &&
+ ! grep "^To: Someone <someone@out.there>\$" hdrs11 &&
+ grep "^To: Someone Else <else@out.there>\$" hdrs11
+'
+
+test_expect_success '--no-cc overrides config.cc' '
+ git config --replace-all format.cc \
+ "C E Cipient <rcipient@example.com>" &&
+ git format-patch --no-cc --stdout main..side >patch12 &&
+ sed -e "/^\$/q" patch12 >hdrs12 &&
+ check_patch hdrs12 &&
+ ! grep "^Cc: C E Cipient <rcipient@example.com>\$" hdrs12
+'
+
+test_expect_success '--no-add-header overrides config.headers' '
+ git config --replace-all format.headers \
+ "Header1: B E Cipient <rcipient@example.com>" &&
+ git format-patch --no-add-header --stdout main..side >patch13 &&
+ sed -e "/^\$/q" patch13 >hdrs13 &&
+ check_patch hdrs13 &&
+ ! grep "^Header1: B E Cipient <rcipient@example.com>\$" hdrs13
+'
+
+test_expect_success 'multiple files' '
+ rm -rf patches/ &&
+ git checkout side &&
+ git format-patch -o patches/ main &&
+ ls patches/0001-Side-changes-1.patch patches/0002-Side-changes-2.patch patches/0003-Side-changes-3-with-n-backslash-n-in-it.patch
+'
+
+test_expect_success 'filename length limit' '
+ test_when_finished "rm -f 000*" &&
+ rm -rf 000[1-9]-*.patch &&
+ for len in 15 25 35
+ do
+ git format-patch --filename-max-length=$len -3 side &&
+ max=$(
+ for patch in 000[1-9]-*.patch
+ do
+ echo "$patch" | wc -c || exit 1
+ done |
+ sort -nr |
+ head -n 1
+ ) &&
+ test $max -le $len || return 1
+ done
+'
+
+test_expect_success 'filename length limit from config' '
+ test_when_finished "rm -f 000*" &&
+ rm -rf 000[1-9]-*.patch &&
+ for len in 15 25 35
+ do
+ git -c format.filenameMaxLength=$len format-patch -3 side &&
+ max=$(
+ for patch in 000[1-9]-*.patch
+ do
+ echo "$patch" | wc -c || exit 1
+ done |
+ sort -nr |
+ head -n 1
+ ) &&
+ test $max -le $len || return 1
+ done
+'
+
+test_expect_success 'filename limit applies only to basename' '
+ test_when_finished "rm -rf patches/" &&
+ rm -rf patches/ &&
+ for len in 15 25 35
+ do
+ git format-patch -o patches --filename-max-length=$len -3 side &&
+ max=$(
+ for patch in patches/000[1-9]-*.patch
+ do
+ echo "${patch#patches/}" | wc -c || exit 1
+ done |
+ sort -nr |
+ head -n 1
+ ) &&
+ test $max -le $len || return 1
+ done
+'
+
+test_expect_success 'reroll count' '
+ rm -fr patches &&
+ git format-patch -o patches --cover-letter --reroll-count 4 main..side >list &&
+ ! grep -v "^patches/v4-000[0-3]-" list &&
+ sed -n -e "/^Subject: /p" $(cat list) >subjects &&
+ ! grep -v "^Subject: \[PATCH v4 [0-3]/3\] " subjects
+'
+
+test_expect_success 'reroll count (-v)' '
+ rm -fr patches &&
+ git format-patch -o patches --cover-letter -v4 main..side >list &&
+ ! grep -v "^patches/v4-000[0-3]-" list &&
+ sed -n -e "/^Subject: /p" $(cat list) >subjects &&
+ ! grep -v "^Subject: \[PATCH v4 [0-3]/3\] " subjects
+'
+
+test_expect_success 'reroll count (-v) with a fractional number' '
+ rm -fr patches &&
+ git format-patch -o patches --cover-letter -v4.4 main..side >list &&
+ ! grep -v "^patches/v4.4-000[0-3]-" list &&
+ sed -n -e "/^Subject: /p" $(cat list) >subjects &&
+ ! grep -v "^Subject: \[PATCH v4.4 [0-3]/3\] " subjects
+'
+
+test_expect_success 'reroll (-v) count with a non number' '
+ rm -fr patches &&
+ git format-patch -o patches --cover-letter -v4rev2 main..side >list &&
+ ! grep -v "^patches/v4rev2-000[0-3]-" list &&
+ sed -n -e "/^Subject: /p" $(cat list) >subjects &&
+ ! grep -v "^Subject: \[PATCH v4rev2 [0-3]/3\] " subjects
+'
+
+test_expect_success 'reroll (-v) count with a non-pathname character' '
+ rm -fr patches &&
+ git format-patch -o patches --cover-letter -v4---..././../--1/.2// main..side >list &&
+ ! grep -v "patches/v4-\.-\.-\.-1-\.2-000[0-3]-" list &&
+ sed -n -e "/^Subject: /p" $(cat list) >subjects &&
+ ! grep -v "^Subject: \[PATCH v4---\.\.\./\./\.\./--1/\.2// [0-3]/3\] " subjects
+'
+
+check_threading () {
+ expect="$1" &&
+ shift &&
+ git format-patch --stdout "$@" >patch &&
+ # Prints everything between the Message-ID and In-Reply-To,
+ # and replaces all Message-ID-lookalikes by a sequence number
+ perl -ne '
+ if (/^(message-id|references|in-reply-to)/i) {
+ $printing = 1;
+ } elsif (/^\S/) {
+ $printing = 0;
+ }
+ if ($printing) {
+ $h{$1}=$i++ if (/<([^>]+)>/ and !exists $h{$1});
+ for $k (keys %h) {s/$k/$h{$k}/};
+ print;
+ }
+ print "---\n" if /^From /i;
+ ' <patch >actual &&
+ test_cmp "$expect" actual
+}
+
+cat >>expect.no-threading <<EOF
+---
+---
+---
+EOF
+
+test_expect_success 'no threading' '
+ git checkout side &&
+ check_threading expect.no-threading main
+'
+
+cat >expect.thread <<EOF
+---
+Message-ID: <0>
+---
+Message-ID: <1>
+In-Reply-To: <0>
+References: <0>
+---
+Message-ID: <2>
+In-Reply-To: <0>
+References: <0>
+EOF
+
+test_expect_success 'thread' '
+ check_threading expect.thread --thread main
+'
+
+test_expect_success '--thread overrides format.thread=deep' '
+ test_config format.thread deep &&
+ check_threading expect.thread --thread main
+'
+
+cat >expect.in-reply-to <<EOF
+---
+Message-ID: <0>
+In-Reply-To: <1>
+References: <1>
+---
+Message-ID: <2>
+In-Reply-To: <1>
+References: <1>
+---
+Message-ID: <3>
+In-Reply-To: <1>
+References: <1>
+EOF
+
+test_expect_success 'thread in-reply-to' '
+ check_threading expect.in-reply-to --in-reply-to="<test.message>" \
+ --thread main
+'
+
+cat >expect.cover-letter <<EOF
+---
+Message-ID: <0>
+---
+Message-ID: <1>
+In-Reply-To: <0>
+References: <0>
+---
+Message-ID: <2>
+In-Reply-To: <0>
+References: <0>
+---
+Message-ID: <3>
+In-Reply-To: <0>
+References: <0>
+EOF
+
+test_expect_success 'thread cover-letter' '
+ check_threading expect.cover-letter --cover-letter --thread main
+'
+
+cat >expect.cl-irt <<EOF
+---
+Message-ID: <0>
+In-Reply-To: <1>
+References: <1>
+---
+Message-ID: <2>
+In-Reply-To: <0>
+References: <1>
+ <0>
+---
+Message-ID: <3>
+In-Reply-To: <0>
+References: <1>
+ <0>
+---
+Message-ID: <4>
+In-Reply-To: <0>
+References: <1>
+ <0>
+EOF
+
+test_expect_success 'thread cover-letter in-reply-to' '
+ check_threading expect.cl-irt --cover-letter \
+ --in-reply-to="<test.message>" --thread main
+'
+
+test_expect_success 'thread explicit shallow' '
+ check_threading expect.cl-irt --cover-letter \
+ --in-reply-to="<test.message>" --thread=shallow main
+'
+
+cat >expect.deep <<EOF
+---
+Message-ID: <0>
+---
+Message-ID: <1>
+In-Reply-To: <0>
+References: <0>
+---
+Message-ID: <2>
+In-Reply-To: <1>
+References: <0>
+ <1>
+EOF
+
+test_expect_success 'thread deep' '
+ check_threading expect.deep --thread=deep main
+'
+
+cat >expect.deep-irt <<EOF
+---
+Message-ID: <0>
+In-Reply-To: <1>
+References: <1>
+---
+Message-ID: <2>
+In-Reply-To: <0>
+References: <1>
+ <0>
+---
+Message-ID: <3>
+In-Reply-To: <2>
+References: <1>
+ <0>
+ <2>
+EOF
+
+test_expect_success 'thread deep in-reply-to' '
+ check_threading expect.deep-irt --thread=deep \
+ --in-reply-to="<test.message>" main
+'
+
+cat >expect.deep-cl <<EOF
+---
+Message-ID: <0>
+---
+Message-ID: <1>
+In-Reply-To: <0>
+References: <0>
+---
+Message-ID: <2>
+In-Reply-To: <1>
+References: <0>
+ <1>
+---
+Message-ID: <3>
+In-Reply-To: <2>
+References: <0>
+ <1>
+ <2>
+EOF
+
+test_expect_success 'thread deep cover-letter' '
+ check_threading expect.deep-cl --cover-letter --thread=deep main
+'
+
+cat >expect.deep-cl-irt <<EOF
+---
+Message-ID: <0>
+In-Reply-To: <1>
+References: <1>
+---
+Message-ID: <2>
+In-Reply-To: <0>
+References: <1>
+ <0>
+---
+Message-ID: <3>
+In-Reply-To: <2>
+References: <1>
+ <0>
+ <2>
+---
+Message-ID: <4>
+In-Reply-To: <3>
+References: <1>
+ <0>
+ <2>
+ <3>
+EOF
+
+test_expect_success 'thread deep cover-letter in-reply-to' '
+ check_threading expect.deep-cl-irt --cover-letter \
+ --in-reply-to="<test.message>" --thread=deep main
+'
+
+test_expect_success 'thread via config' '
+ test_config format.thread true &&
+ check_threading expect.thread main
+'
+
+test_expect_success 'thread deep via config' '
+ test_config format.thread deep &&
+ check_threading expect.deep main
+'
+
+test_expect_success 'thread config + override' '
+ test_config format.thread deep &&
+ check_threading expect.thread --thread main
+'
+
+test_expect_success 'thread config + --no-thread' '
+ test_config format.thread deep &&
+ check_threading expect.no-threading --no-thread main
+'
+
+test_expect_success 'excessive subject' '
+ rm -rf patches/ &&
+ git checkout side &&
+ before=$(git hash-object file) &&
+ before=$(git rev-parse --short $before) &&
+ test_write_lines 5 6 1 2 3 A 4 B C 7 8 9 10 D E F >>file &&
+ after=$(git hash-object file) &&
+ after=$(git rev-parse --short $after) &&
+ git update-index file &&
+ git commit -m "This is an excessively long subject line for a message due to the habit some projects have of not having a short, one-line subject at the start of the commit message, but rather sticking a whole paragraph right at the start as the only thing in the commit message. It had better not become the filename for the patch." &&
+ git format-patch -o patches/ main..side &&
+ ls patches/0004-This-is-an-excessively-long-subject-line-for-a-messa.patch
+'
+
+test_expect_success 'failure to write cover-letter aborts gracefully' '
+ test_when_finished "rmdir 0000-cover-letter.patch" &&
+ mkdir 0000-cover-letter.patch &&
+ test_must_fail git format-patch --no-renames --cover-letter -1
+'
+
+test_expect_success 'cover-letter inherits diff options' '
+ git mv file foo &&
+ git commit -m foo &&
+ git format-patch --no-renames --cover-letter -1 &&
+ check_patch 0000-cover-letter.patch &&
+ ! grep "file => foo .* 0 *\$" 0000-cover-letter.patch &&
+ git format-patch --cover-letter -1 -M &&
+ grep "file => foo .* 0 *\$" 0000-cover-letter.patch
+'
+
+cat >expect <<EOF
+ This is an excessively long subject line for a message due to the
+ habit some projects have of not having a short, one-line subject at
+ the start of the commit message, but rather sticking a whole
+ paragraph right at the start as the only thing in the commit
+ message. It had better not become the filename for the patch.
+ foo
+
+EOF
+
+test_expect_success 'shortlog of cover-letter wraps overly-long onelines' '
+ git format-patch --cover-letter -2 &&
+ sed -e "1,/A U Thor/d" -e "/^\$/q" 0000-cover-letter.patch >output &&
+ test_cmp expect output
+'
+
+cat >expect <<EOF
+index $before..$after 100644
+--- a/file
++++ b/file
+@@ -13,4 +13,20 @@ C
+ 10
+ D
+ E
+ F
++5
+EOF
+
+test_expect_success 'format-patch respects -U' '
+ git format-patch -U4 -2 &&
+ sed -e "1,/^diff/d" -e "/^+5/q" \
+ <0001-This-is-an-excessively-long-subject-line-for-a-messa.patch \
+ >output &&
+ test_cmp expect output
+'
+
+cat >expect <<EOF
+
+diff --git a/file b/file
+index $before..$after 100644
+--- a/file
++++ b/file
+@@ -14,3 +14,19 @@ C
+ D
+ E
+ F
++5
+EOF
+
+test_expect_success 'format-patch -p suppresses stat' '
+ git format-patch -p -2 &&
+ sed -e "1,/^\$/d" -e "/^+5/q" 0001-This-is-an-excessively-long-subject-line-for-a-messa.patch >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'format-patch from a subdirectory (1)' '
+ filename=$(
+ rm -rf sub &&
+ mkdir -p sub/dir &&
+ cd sub/dir &&
+ git format-patch -1
+ ) &&
+ case "$filename" in
+ 0*)
+ ;; # ok
+ *)
+ echo "Oops? $filename"
+ false
+ ;;
+ esac &&
+ test -f "$filename"
+'
+
+test_expect_success 'format-patch from a subdirectory (2)' '
+ filename=$(
+ rm -rf sub &&
+ mkdir -p sub/dir &&
+ cd sub/dir &&
+ git format-patch -1 -o ..
+ ) &&
+ case "$filename" in
+ ../0*)
+ ;; # ok
+ *)
+ echo "Oops? $filename"
+ false
+ ;;
+ esac &&
+ basename=$(expr "$filename" : ".*/\(.*\)") &&
+ test -f "sub/$basename"
+'
+
+test_expect_success 'format-patch from a subdirectory (3)' '
+ rm -f 0* &&
+ filename=$(
+ rm -rf sub &&
+ mkdir -p sub/dir &&
+ cd sub/dir &&
+ git format-patch -1 -o "$TRASH_DIRECTORY"
+ ) &&
+ basename=$(expr "$filename" : ".*/\(.*\)") &&
+ test -f "$basename"
+'
+
+test_expect_success 'format-patch --in-reply-to' '
+ git format-patch -1 --stdout --in-reply-to "baz@foo.bar" >patch8 &&
+ grep "^In-Reply-To: <baz@foo.bar>" patch8 &&
+ grep "^References: <baz@foo.bar>" patch8
+'
+
+test_expect_success 'format-patch --signoff' '
+ git format-patch -1 --signoff --stdout >out &&
+ grep "^Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>" out
+'
+
+test_expect_success 'format-patch --notes --signoff' '
+ git notes --ref test add -m "test message" HEAD &&
+ git format-patch -1 --signoff --stdout --notes=test >out &&
+ # Three dashes must come after S-o-b
+ ! sed "/^Signed-off-by: /q" out | grep "test message" &&
+ sed "1,/^Signed-off-by: /d" out | grep "test message" &&
+ # Notes message must come after three dashes
+ ! sed "/^---$/q" out | grep "test message" &&
+ sed "1,/^---$/d" out | grep "test message"
+'
+
+test_expect_success 'format-patch notes output control' '
+ git notes add -m "notes config message" HEAD &&
+ test_when_finished git notes remove HEAD &&
+
+ git format-patch -1 --stdout >out &&
+ ! grep "notes config message" out &&
+ git format-patch -1 --stdout --notes >out &&
+ grep "notes config message" out &&
+ git format-patch -1 --stdout --no-notes >out &&
+ ! grep "notes config message" out &&
+ git format-patch -1 --stdout --notes --no-notes >out &&
+ ! grep "notes config message" out &&
+ git format-patch -1 --stdout --no-notes --notes >out &&
+ grep "notes config message" out &&
+
+ test_config format.notes true &&
+ git format-patch -1 --stdout >out &&
+ grep "notes config message" out &&
+ git format-patch -1 --stdout --notes >out &&
+ grep "notes config message" out &&
+ git format-patch -1 --stdout --no-notes >out &&
+ ! grep "notes config message" out &&
+ git format-patch -1 --stdout --notes --no-notes >out &&
+ ! grep "notes config message" out &&
+ git format-patch -1 --stdout --no-notes --notes >out &&
+ grep "notes config message" out
+'
+
+test_expect_success 'format-patch with multiple notes refs' '
+ git notes --ref note1 add -m "this is note 1" HEAD &&
+ test_when_finished git notes --ref note1 remove HEAD &&
+ git notes --ref note2 add -m "this is note 2" HEAD &&
+ test_when_finished git notes --ref note2 remove HEAD &&
+
+ git format-patch -1 --stdout >out &&
+ ! grep "this is note 1" out &&
+ ! grep "this is note 2" out &&
+ git format-patch -1 --stdout --notes=note1 >out &&
+ grep "this is note 1" out &&
+ ! grep "this is note 2" out &&
+ git format-patch -1 --stdout --notes=note2 >out &&
+ ! grep "this is note 1" out &&
+ grep "this is note 2" out &&
+ git format-patch -1 --stdout --notes=note1 --notes=note2 >out &&
+ grep "this is note 1" out &&
+ grep "this is note 2" out &&
+
+ test_config format.notes note1 &&
+ git format-patch -1 --stdout >out &&
+ grep "this is note 1" out &&
+ ! grep "this is note 2" out &&
+ git format-patch -1 --stdout --no-notes >out &&
+ ! grep "this is note 1" out &&
+ ! grep "this is note 2" out &&
+ git format-patch -1 --stdout --notes=note2 >out &&
+ grep "this is note 1" out &&
+ grep "this is note 2" out &&
+ git format-patch -1 --stdout --no-notes --notes=note2 >out &&
+ ! grep "this is note 1" out &&
+ grep "this is note 2" out &&
+
+ git config --add format.notes note2 &&
+ git format-patch -1 --stdout >out &&
+ grep "this is note 1" out &&
+ grep "this is note 2" out &&
+ git format-patch -1 --stdout --no-notes >out &&
+ ! grep "this is note 1" out &&
+ ! grep "this is note 2" out
+'
+
+test_expect_success 'format-patch with multiple notes refs in config' '
+ test_when_finished "test_unconfig format.notes" &&
+
+ git notes --ref note1 add -m "this is note 1" HEAD &&
+ test_when_finished git notes --ref note1 remove HEAD &&
+ git notes --ref note2 add -m "this is note 2" HEAD &&
+ test_when_finished git notes --ref note2 remove HEAD &&
+
+ git config format.notes note1 &&
+ git format-patch -1 --stdout >out &&
+ grep "this is note 1" out &&
+ ! grep "this is note 2" out &&
+ git config format.notes note2 &&
+ git format-patch -1 --stdout >out &&
+ ! grep "this is note 1" out &&
+ grep "this is note 2" out &&
+ git config --add format.notes note1 &&
+ git format-patch -1 --stdout >out &&
+ grep "this is note 1" out &&
+ grep "this is note 2" out &&
+
+ git config --replace-all format.notes note1 &&
+ git config --add format.notes false &&
+ git format-patch -1 --stdout >out &&
+ ! grep "this is note 1" out &&
+ ! grep "this is note 2" out &&
+ git config --add format.notes note2 &&
+ git format-patch -1 --stdout >out &&
+ ! grep "this is note 1" out &&
+ grep "this is note 2" out
+'
+
+echo "fatal: --name-only does not make sense" >expect.name-only
+echo "fatal: --name-status does not make sense" >expect.name-status
+echo "fatal: --check does not make sense" >expect.check
+
+test_expect_success 'options no longer allowed for format-patch' '
+ test_must_fail git format-patch --name-only 2>output &&
+ test_cmp expect.name-only output &&
+ test_must_fail git format-patch --name-status 2>output &&
+ test_cmp expect.name-status output &&
+ test_must_fail git format-patch --check 2>output &&
+ test_cmp expect.check output
+'
+
+test_expect_success 'format-patch --numstat should produce a patch' '
+ git format-patch --numstat --stdout main..side >output &&
+ grep "^diff --git a/" output >diff &&
+ test_line_count = 5 diff
+'
+
+test_expect_success 'format-patch -- <path>' '
+ rm -f *.patch &&
+ git checkout -b pathspec main &&
+
+ echo file_a 1 >file_a &&
+ echo file_b 1 >file_b &&
+ git add file_a file_b &&
+ git commit -m pathspec_initial &&
+
+ echo file_a 2 >>file_a &&
+ git add file_a &&
+ git commit -m pathspec_a &&
+
+ echo file_b 2 >>file_b &&
+ git add file_b &&
+ git commit -m pathspec_b &&
+
+ echo file_a 3 >>file_a &&
+ echo file_b 3 >>file_b &&
+ git add file_a file_b &&
+ git commit -m pathspec_ab &&
+
+ cat >expect <<-\EOF &&
+ 0001-pathspec_initial.patch
+ 0002-pathspec_a.patch
+ 0003-pathspec_ab.patch
+ EOF
+
+ git format-patch main..pathspec -- file_a >output &&
+ test_cmp expect output &&
+ ! grep file_b *.patch
+'
+
+test_expect_success 'format-patch --ignore-if-in-upstream HEAD' '
+ git checkout side &&
+ git format-patch --ignore-if-in-upstream HEAD
+'
+
+test_expect_success 'get git version' '
+ git_version=$(git --version) &&
+ git_version=${git_version##* }
+'
+
+signature() {
+ printf "%s\n%s\n\n" "-- " "${1:-$git_version}"
+}
+
+test_expect_success 'format-patch default signature' '
+ git format-patch --stdout -1 >patch &&
+ tail -n 3 patch >output &&
+ signature >expect &&
+ test_cmp expect output
+'
+
+test_expect_success 'format-patch --signature' '
+ git format-patch --stdout --signature="my sig" -1 >patch &&
+ tail -n 3 patch >output &&
+ signature "my sig" >expect &&
+ test_cmp expect output
+'
+
+test_expect_success 'format-patch with format.signature config' '
+ git config format.signature "config sig" &&
+ git format-patch --stdout -1 >output &&
+ grep "config sig" output
+'
+
+test_expect_success 'format-patch --signature overrides format.signature' '
+ git config format.signature "config sig" &&
+ git format-patch --stdout --signature="overrides" -1 >output &&
+ ! grep "config sig" output &&
+ grep "overrides" output
+'
+
+test_expect_success 'format-patch --no-signature ignores format.signature' '
+ git config format.signature "config sig" &&
+ git format-patch --stdout --signature="my sig" --no-signature \
+ -1 >output &&
+ check_patch output &&
+ ! grep "config sig" output &&
+ ! grep "my sig" output &&
+ ! grep "^-- \$" output
+'
+
+test_expect_success 'format-patch --signature --cover-letter' '
+ git config --unset-all format.signature &&
+ git format-patch --stdout --signature="my sig" --cover-letter \
+ -1 >output &&
+ grep "my sig" output >sig &&
+ test_line_count = 2 sig
+'
+
+test_expect_success 'format.signature="" suppresses signatures' '
+ git config format.signature "" &&
+ git format-patch --stdout -1 >output &&
+ check_patch output &&
+ ! grep "^-- \$" output
+'
+
+test_expect_success 'format-patch --no-signature suppresses signatures' '
+ git config --unset-all format.signature &&
+ git format-patch --stdout --no-signature -1 >output &&
+ check_patch output &&
+ ! grep "^-- \$" output
+'
+
+test_expect_success 'format-patch --signature="" suppresses signatures' '
+ git format-patch --stdout --signature="" -1 >output &&
+ check_patch output &&
+ ! grep "^-- \$" output
+'
+
+test_expect_success 'prepare mail-signature input' '
+ cat >mail-signature <<-\EOF
+
+ Test User <test.email@kernel.org>
+ http://git.kernel.org/cgit/git/git.git
+
+ git.kernel.org/?p=git/git.git;a=summary
+
+ EOF
+'
+
+test_expect_success '--signature-file=file works' '
+ git format-patch --stdout --signature-file=mail-signature -1 >output &&
+ check_patch output &&
+ sed -e "1,/^-- \$/d" output >actual &&
+ {
+ cat mail-signature && echo
+ } >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'format.signaturefile works' '
+ test_config format.signaturefile mail-signature &&
+ git format-patch --stdout -1 >output &&
+ check_patch output &&
+ sed -e "1,/^-- \$/d" output >actual &&
+ {
+ cat mail-signature && echo
+ } >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success '--no-signature suppresses format.signaturefile ' '
+ test_config format.signaturefile mail-signature &&
+ git format-patch --stdout --no-signature -1 >output &&
+ check_patch output &&
+ ! grep "^-- \$" output
+'
+
+test_expect_success '--signature-file overrides format.signaturefile' '
+ cat >other-mail-signature <<-\EOF &&
+ Use this other signature instead of mail-signature.
+ EOF
+ test_config format.signaturefile mail-signature &&
+ git format-patch --stdout \
+ --signature-file=other-mail-signature -1 >output &&
+ check_patch output &&
+ sed -e "1,/^-- \$/d" output >actual &&
+ {
+ cat other-mail-signature && echo
+ } >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success '--signature overrides format.signaturefile' '
+ test_config format.signaturefile mail-signature &&
+ git format-patch --stdout --signature="my sig" -1 >output &&
+ check_patch output &&
+ grep "my sig" output
+'
+
+test_expect_success TTY 'format-patch --stdout paginates' '
+ rm -f pager_used &&
+ test_terminal env GIT_PAGER="wc >pager_used" git format-patch --stdout --all &&
+ test_path_is_file pager_used
+'
+
+ test_expect_success TTY 'format-patch --stdout pagination can be disabled' '
+ rm -f pager_used &&
+ test_terminal env GIT_PAGER="wc >pager_used" git --no-pager format-patch --stdout --all &&
+ test_terminal env GIT_PAGER="wc >pager_used" git -c "pager.format-patch=false" format-patch --stdout --all &&
+ test_path_is_missing pager_used &&
+ test_path_is_missing .git/pager_used
+'
+
+test_expect_success 'format-patch handles multi-line subjects' '
+ rm -rf patches/ &&
+ echo content >>file &&
+ test_write_lines one two three >msg &&
+ git add file &&
+ git commit -F msg &&
+ git format-patch -o patches -1 &&
+ grep ^Subject: patches/0001-one.patch >actual &&
+ echo "Subject: [PATCH] one two three" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'format-patch handles multi-line encoded subjects' '
+ rm -rf patches/ &&
+ echo content >>file &&
+ test_write_lines en två tre >msg &&
+ git add file &&
+ git commit -F msg &&
+ git format-patch -o patches -1 &&
+ grep ^Subject: patches/0001-en.patch >actual &&
+ echo "Subject: [PATCH] =?UTF-8?q?en=20tv=C3=A5=20tre?=" >expect &&
+ test_cmp expect actual
+'
+
+M8="foo bar "
+M64=$M8$M8$M8$M8$M8$M8$M8$M8
+M512=$M64$M64$M64$M64$M64$M64$M64$M64
+cat >expect <<'EOF'
+Subject: [PATCH] foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo
+ bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar
+ foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo
+ bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar
+ foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo
+ bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar
+ foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar foo bar
+EOF
+test_expect_success 'format-patch wraps extremely long subject (ascii)' '
+ echo content >>file &&
+ git add file &&
+ git commit -m "$M512" &&
+ git format-patch --stdout -1 >patch &&
+ sed -n "/^Subject/p; /^ /p; /^$/q" patch >subject &&
+ test_cmp expect subject
+'
+
+M8="föö bar "
+M64=$M8$M8$M8$M8$M8$M8$M8$M8
+M512=$M64$M64$M64$M64$M64$M64$M64$M64
+cat >expect <<'EOF'
+Subject: [PATCH] =?UTF-8?q?f=C3=B6=C3=B6=20bar=20f=C3=B6=C3=B6=20bar=20f?=
+ =?UTF-8?q?=C3=B6=C3=B6=20bar=20f=C3=B6=C3=B6=20bar=20f=C3=B6=C3=B6=20bar?=
+ =?UTF-8?q?=20f=C3=B6=C3=B6=20bar=20f=C3=B6=C3=B6=20bar=20f=C3=B6=C3=B6=20?=
+ =?UTF-8?q?bar=20f=C3=B6=C3=B6=20bar=20f=C3=B6=C3=B6=20bar=20f=C3=B6=C3=B6?=
+ =?UTF-8?q?=20bar=20f=C3=B6=C3=B6=20bar=20f=C3=B6=C3=B6=20bar=20f=C3=B6?=
+ =?UTF-8?q?=C3=B6=20bar=20f=C3=B6=C3=B6=20bar=20f=C3=B6=C3=B6=20bar=20f?=
+ =?UTF-8?q?=C3=B6=C3=B6=20bar=20f=C3=B6=C3=B6=20bar=20f=C3=B6=C3=B6=20bar?=
+ =?UTF-8?q?=20f=C3=B6=C3=B6=20bar=20f=C3=B6=C3=B6=20bar=20f=C3=B6=C3=B6=20?=
+ =?UTF-8?q?bar=20f=C3=B6=C3=B6=20bar=20f=C3=B6=C3=B6=20bar=20f=C3=B6=C3=B6?=
+ =?UTF-8?q?=20bar=20f=C3=B6=C3=B6=20bar=20f=C3=B6=C3=B6=20bar=20f=C3=B6?=
+ =?UTF-8?q?=C3=B6=20bar=20f=C3=B6=C3=B6=20bar=20f=C3=B6=C3=B6=20bar=20f?=
+ =?UTF-8?q?=C3=B6=C3=B6=20bar=20f=C3=B6=C3=B6=20bar=20f=C3=B6=C3=B6=20bar?=
+ =?UTF-8?q?=20f=C3=B6=C3=B6=20bar=20f=C3=B6=C3=B6=20bar=20f=C3=B6=C3=B6=20?=
+ =?UTF-8?q?bar=20f=C3=B6=C3=B6=20bar=20f=C3=B6=C3=B6=20bar=20f=C3=B6=C3=B6?=
+ =?UTF-8?q?=20bar=20f=C3=B6=C3=B6=20bar=20f=C3=B6=C3=B6=20bar=20f=C3=B6?=
+ =?UTF-8?q?=C3=B6=20bar=20f=C3=B6=C3=B6=20bar=20f=C3=B6=C3=B6=20bar=20f?=
+ =?UTF-8?q?=C3=B6=C3=B6=20bar=20f=C3=B6=C3=B6=20bar=20f=C3=B6=C3=B6=20bar?=
+ =?UTF-8?q?=20f=C3=B6=C3=B6=20bar=20f=C3=B6=C3=B6=20bar=20f=C3=B6=C3=B6=20?=
+ =?UTF-8?q?bar=20f=C3=B6=C3=B6=20bar=20f=C3=B6=C3=B6=20bar=20f=C3=B6=C3=B6?=
+ =?UTF-8?q?=20bar=20f=C3=B6=C3=B6=20bar=20f=C3=B6=C3=B6=20bar=20f=C3=B6?=
+ =?UTF-8?q?=C3=B6=20bar=20f=C3=B6=C3=B6=20bar=20f=C3=B6=C3=B6=20bar=20f?=
+ =?UTF-8?q?=C3=B6=C3=B6=20bar=20f=C3=B6=C3=B6=20bar=20f=C3=B6=C3=B6=20bar?=
+ =?UTF-8?q?=20f=C3=B6=C3=B6=20bar=20f=C3=B6=C3=B6=20bar=20f=C3=B6=C3=B6=20?=
+ =?UTF-8?q?bar?=
+EOF
+test_expect_success 'format-patch wraps extremely long subject (rfc2047)' '
+ rm -rf patches/ &&
+ echo content >>file &&
+ git add file &&
+ git commit -m "$M512" &&
+ git format-patch --stdout -1 >patch &&
+ sed -n "/^Subject/p; /^ /p; /^$/q" patch >subject &&
+ test_cmp expect subject
+'
+
+check_author() {
+ echo content >>file &&
+ git add file &&
+ GIT_AUTHOR_NAME=$1 git commit -m author-check &&
+ git format-patch --stdout -1 >patch &&
+ sed -n "/^From: /p; /^ /p; /^$/q" patch >actual &&
+ test_cmp expect actual
+}
+
+cat >expect <<'EOF'
+From: "Foo B. Bar" <author@example.com>
+EOF
+test_expect_success 'format-patch quotes dot in from-headers' '
+ check_author "Foo B. Bar"
+'
+
+cat >expect <<'EOF'
+From: "Foo \"The Baz\" Bar" <author@example.com>
+EOF
+test_expect_success 'format-patch quotes double-quote in from-headers' '
+ check_author "Foo \"The Baz\" Bar"
+'
+
+cat >expect <<'EOF'
+From: =?UTF-8?q?F=C3=B6o=20Bar?= <author@example.com>
+EOF
+test_expect_success 'format-patch uses rfc2047-encoded from-headers when necessary' '
+ check_author "Föo Bar"
+'
+
+cat >expect <<'EOF'
+From: =?UTF-8?q?F=C3=B6o=20B=2E=20Bar?= <author@example.com>
+EOF
+test_expect_success 'rfc2047-encoded from-headers leave no rfc822 specials' '
+ check_author "Föo B. Bar"
+'
+
+cat >expect <<EOF
+From: foo_bar_foo_bar_foo_bar_foo_bar_foo_bar_foo_bar_foo_bar_foo_bar_
+ <author@example.com>
+EOF
+test_expect_success 'format-patch wraps moderately long from-header (ascii)' '
+ check_author "foo_bar_foo_bar_foo_bar_foo_bar_foo_bar_foo_bar_foo_bar_foo_bar_"
+'
+
+cat >expect <<'EOF'
+From: Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar
+ Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo
+ Bar Foo Bar Foo Bar Foo Bar <author@example.com>
+EOF
+test_expect_success 'format-patch wraps extremely long from-header (ascii)' '
+ check_author "Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar"
+'
+
+cat >expect <<'EOF'
+From: "Foo.Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar
+ Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo
+ Bar Foo Bar Foo Bar Foo Bar" <author@example.com>
+EOF
+test_expect_success 'format-patch wraps extremely long from-header (rfc822)' '
+ check_author "Foo.Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar"
+'
+
+cat >expect <<'EOF'
+From: =?UTF-8?q?Fo=C3=B6=20Bar=20Foo=20Bar=20Foo=20Bar=20Foo=20Bar=20Foo?=
+ =?UTF-8?q?=20Bar=20Foo=20Bar=20Foo=20Bar=20Foo=20Bar=20Foo=20Bar=20Foo=20?=
+ =?UTF-8?q?Bar=20Foo=20Bar=20Foo=20Bar=20Foo=20Bar=20Foo=20Bar=20Foo=20Bar?=
+ =?UTF-8?q?=20Foo=20Bar=20Foo=20Bar=20Foo=20Bar=20Foo=20Bar=20Foo=20Bar=20?=
+ =?UTF-8?q?Foo=20Bar=20Foo=20Bar?= <author@example.com>
+EOF
+test_expect_success 'format-patch wraps extremely long from-header (rfc2047)' '
+ check_author "Foö Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar"
+'
+
+cat >expect <<'EOF'
+From: Foö Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar
+ Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo
+ Bar Foo Bar Foo Bar Foo Bar <author@example.com>
+EOF
+test_expect_success 'format-patch wraps extremely long from-header (non-ASCII without Q-encoding)' '
+ echo content >>file &&
+ git add file &&
+ GIT_AUTHOR_NAME="Foö Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar Foo Bar" \
+ git commit -m author-check &&
+ git format-patch --no-encode-email-headers --stdout -1 >patch &&
+ sed -n "/^From: /p; /^ /p; /^$/q" patch >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<'EOF'
+Subject: [PATCH] Foö
+EOF
+test_expect_success 'subject lines are unencoded with --no-encode-email-headers' '
+ echo content >>file &&
+ git add file &&
+ git commit -m "Foö" &&
+ git format-patch --no-encode-email-headers -1 --stdout >patch &&
+ grep ^Subject: patch >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<'EOF'
+Subject: [PATCH] Foö
+EOF
+test_expect_success 'subject lines are unencoded with format.encodeEmailHeaders=false' '
+ echo content >>file &&
+ git add file &&
+ git commit -m "Foö" &&
+ git config format.encodeEmailHeaders false &&
+ git format-patch -1 --stdout >patch &&
+ grep ^Subject: patch >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<'EOF'
+Subject: [PATCH] =?UTF-8?q?Fo=C3=B6?=
+EOF
+test_expect_success '--encode-email-headers overrides format.encodeEmailHeaders' '
+ echo content >>file &&
+ git add file &&
+ git commit -m "Foö" &&
+ git config format.encodeEmailHeaders false &&
+ git format-patch --encode-email-headers -1 --stdout >patch &&
+ grep ^Subject: patch >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<'EOF'
+Subject: header with . in it
+EOF
+test_expect_success 'subject lines do not have 822 atom-quoting' '
+ echo content >>file &&
+ git add file &&
+ git commit -m "header with . in it" &&
+ git format-patch -k -1 --stdout >patch &&
+ grep ^Subject: patch >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<'EOF'
+Subject: [PREFIX 1/1] header with . in it
+EOF
+test_expect_success 'subject prefixes have space prepended' '
+ git format-patch -n -1 --stdout --subject-prefix=PREFIX >patch &&
+ grep ^Subject: patch >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<'EOF'
+Subject: [1/1] header with . in it
+EOF
+test_expect_success 'empty subject prefix does not have extra space' '
+ git format-patch -n -1 --stdout --subject-prefix= >patch &&
+ grep ^Subject: patch >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--rfc' '
+ cat >expect <<-\EOF &&
+ Subject: [RFC PATCH 1/1] header with . in it
+ EOF
+ git format-patch -n -1 --stdout --rfc >patch &&
+ grep "^Subject:" patch >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--rfc does not overwrite prefix' '
+ cat >expect <<-\EOF &&
+ Subject: [RFC PATCH foobar 1/1] header with . in it
+ EOF
+ git -c format.subjectPrefix="PATCH foobar" \
+ format-patch -n -1 --stdout --rfc >patch &&
+ grep "^Subject:" patch >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--rfc is argument order independent' '
+ cat >expect <<-\EOF &&
+ Subject: [RFC PATCH foobar 1/1] header with . in it
+ EOF
+ git format-patch -n -1 --stdout --rfc \
+ --subject-prefix="PATCH foobar" >patch &&
+ grep "^Subject:" patch >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--from=ident notices bogus ident' '
+ test_must_fail git format-patch -1 --stdout --from=foo >patch
+'
+
+test_expect_success '--from=ident replaces author' '
+ git format-patch -1 --stdout --from="Me <me@example.com>" >patch &&
+ cat >expect <<-\EOF &&
+ From: Me <me@example.com>
+
+ From: A U Thor <author@example.com>
+
+ EOF
+ sed -ne "/^From:/p; /^$/p; /^---$/q" patch >patch.head &&
+ test_cmp expect patch.head
+'
+
+test_expect_success '--from uses committer ident' '
+ git format-patch -1 --stdout --from >patch &&
+ cat >expect <<-\EOF &&
+ From: C O Mitter <committer@example.com>
+
+ From: A U Thor <author@example.com>
+
+ EOF
+ sed -ne "/^From:/p; /^$/p; /^---$/q" patch >patch.head &&
+ test_cmp expect patch.head
+'
+
+test_expect_success '--from omits redundant in-body header' '
+ git format-patch -1 --stdout --from="A U Thor <author@example.com>" >patch &&
+ cat >expect <<-\EOF &&
+ From: A U Thor <author@example.com>
+
+ EOF
+ sed -ne "/^From:/p; /^$/p; /^---$/q" patch >patch.head &&
+ test_cmp expect patch.head
+'
+
+test_expect_success 'with --force-in-body-from, redundant in-body from is kept' '
+ git format-patch --force-in-body-from \
+ -1 --stdout --from="A U Thor <author@example.com>" >patch &&
+ cat >expect <<-\EOF &&
+ From: A U Thor <author@example.com>
+
+ From: A U Thor <author@example.com>
+
+ EOF
+ sed -ne "/^From:/p; /^$/p; /^---$/q" patch >patch.head &&
+ test_cmp expect patch.head
+'
+
+test_expect_success 'format.forceInBodyFrom, equivalent to --force-in-body-from' '
+ git -c format.forceInBodyFrom=yes format-patch \
+ -1 --stdout --from="A U Thor <author@example.com>" >patch &&
+ cat >expect <<-\EOF &&
+ From: A U Thor <author@example.com>
+
+ From: A U Thor <author@example.com>
+
+ EOF
+ sed -ne "/^From:/p; /^$/p; /^---$/q" patch >patch.head &&
+ test_cmp expect patch.head
+'
+
+test_expect_success 'format.forceInBodyFrom, equivalent to --force-in-body-from' '
+ git -c format.forceInBodyFrom=yes format-patch --no-force-in-body-from \
+ -1 --stdout --from="A U Thor <author@example.com>" >patch &&
+ cat >expect <<-\EOF &&
+ From: A U Thor <author@example.com>
+
+ EOF
+ sed -ne "/^From:/p; /^$/p; /^---$/q" patch >patch.head &&
+ test_cmp expect patch.head
+'
+
+test_expect_success 'in-body headers trigger content encoding' '
+ test_env GIT_AUTHOR_NAME="éxötìc" test_commit exotic &&
+ test_when_finished "git reset --hard HEAD^" &&
+ git format-patch -1 --stdout --from >patch &&
+ cat >expect <<-\EOF &&
+ From: C O Mitter <committer@example.com>
+ Content-Type: text/plain; charset=UTF-8
+
+ From: éxötìc <author@example.com>
+
+ EOF
+ sed -ne "/^From:/p; /^$/p; /^Content-Type/p; /^---$/q" patch >patch.head &&
+ test_cmp expect patch.head
+'
+
+append_signoff()
+{
+ C=$(git commit-tree HEAD^^{tree} -p HEAD) &&
+ git format-patch --stdout --signoff $C^..$C >append_signoff.patch &&
+ sed -n -e "1,/^---$/p" append_signoff.patch |
+ grep -E -n "^Subject|Sign|^$"
+}
+
+test_expect_success 'signoff: commit with no body' '
+ append_signoff </dev/null >actual &&
+ cat <<-\EOF | sed "s/EOL$//" >expect &&
+ 4:Subject: [PATCH] EOL
+ 8:
+ 9:Signed-off-by: C O Mitter <committer@example.com>
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'signoff: commit with only subject' '
+ echo subject | append_signoff >actual &&
+ cat >expect <<-\EOF &&
+ 4:Subject: [PATCH] subject
+ 8:
+ 9:Signed-off-by: C O Mitter <committer@example.com>
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'signoff: commit with only subject that does not end with NL' '
+ printf subject | append_signoff >actual &&
+ cat >expect <<-\EOF &&
+ 4:Subject: [PATCH] subject
+ 8:
+ 9:Signed-off-by: C O Mitter <committer@example.com>
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'signoff: no existing signoffs' '
+ append_signoff <<-\EOF >actual &&
+ subject
+
+ body
+ EOF
+ cat >expect <<-\EOF &&
+ 4:Subject: [PATCH] subject
+ 8:
+ 10:
+ 11:Signed-off-by: C O Mitter <committer@example.com>
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'signoff: no existing signoffs and no trailing NL' '
+ printf "subject\n\nbody" | append_signoff >actual &&
+ cat >expect <<-\EOF &&
+ 4:Subject: [PATCH] subject
+ 8:
+ 10:
+ 11:Signed-off-by: C O Mitter <committer@example.com>
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'signoff: some random signoff' '
+ append_signoff <<-\EOF >actual &&
+ subject
+
+ body
+
+ Signed-off-by: my@house
+ EOF
+ cat >expect <<-\EOF &&
+ 4:Subject: [PATCH] subject
+ 8:
+ 10:
+ 11:Signed-off-by: my@house
+ 12:Signed-off-by: C O Mitter <committer@example.com>
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'signoff: misc conforming footer elements' '
+ append_signoff <<-\EOF >actual &&
+ subject
+
+ body
+
+ Signed-off-by: my@house
+ (cherry picked from commit da39a3ee5e6b4b0d3255bfef95601890afd80709)
+ Tested-by: Some One <someone@example.com>
+ Bug: 1234
+ EOF
+ cat >expect <<-\EOF &&
+ 4:Subject: [PATCH] subject
+ 8:
+ 10:
+ 11:Signed-off-by: my@house
+ 15:Signed-off-by: C O Mitter <committer@example.com>
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'signoff: some random signoff-alike' '
+ append_signoff <<-\EOF >actual &&
+ subject
+
+ body
+ Fooled-by-me: my@house
+ EOF
+ cat >expect <<-\EOF &&
+ 4:Subject: [PATCH] subject
+ 8:
+ 11:
+ 12:Signed-off-by: C O Mitter <committer@example.com>
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'signoff: not really a signoff' '
+ append_signoff <<-\EOF >actual &&
+ subject
+
+ I want to mention about Signed-off-by: here.
+ EOF
+ cat >expect <<-\EOF &&
+ 4:Subject: [PATCH] subject
+ 8:
+ 9:I want to mention about Signed-off-by: here.
+ 10:
+ 11:Signed-off-by: C O Mitter <committer@example.com>
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'signoff: not really a signoff (2)' '
+ append_signoff <<-\EOF >actual &&
+ subject
+
+ My unfortunate
+ Signed-off-by: example happens to be wrapped here.
+ EOF
+ cat >expect <<-\EOF &&
+ 4:Subject: [PATCH] subject
+ 8:
+ 10:Signed-off-by: example happens to be wrapped here.
+ 11:Signed-off-by: C O Mitter <committer@example.com>
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'signoff: valid S-o-b paragraph in the middle' '
+ append_signoff <<-\EOF >actual &&
+ subject
+
+ Signed-off-by: my@house
+ Signed-off-by: your@house
+
+ A lot of houses.
+ EOF
+ cat >expect <<-\EOF &&
+ 4:Subject: [PATCH] subject
+ 8:
+ 9:Signed-off-by: my@house
+ 10:Signed-off-by: your@house
+ 11:
+ 13:
+ 14:Signed-off-by: C O Mitter <committer@example.com>
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'signoff: the same signoff at the end' '
+ append_signoff <<-\EOF >actual &&
+ subject
+
+ body
+
+ Signed-off-by: C O Mitter <committer@example.com>
+ EOF
+ cat >expect <<-\EOF &&
+ 4:Subject: [PATCH] subject
+ 8:
+ 10:
+ 11:Signed-off-by: C O Mitter <committer@example.com>
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'signoff: the same signoff at the end, no trailing NL' '
+ printf "subject\n\nSigned-off-by: C O Mitter <committer@example.com>" |
+ append_signoff >actual &&
+ cat >expect <<-\EOF &&
+ 4:Subject: [PATCH] subject
+ 8:
+ 9:Signed-off-by: C O Mitter <committer@example.com>
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'signoff: the same signoff NOT at the end' '
+ append_signoff <<-\EOF >actual &&
+ subject
+
+ body
+
+ Signed-off-by: C O Mitter <committer@example.com>
+ Signed-off-by: my@house
+ EOF
+ cat >expect <<-\EOF &&
+ 4:Subject: [PATCH] subject
+ 8:
+ 10:
+ 11:Signed-off-by: C O Mitter <committer@example.com>
+ 12:Signed-off-by: my@house
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'signoff: tolerate garbage in conforming footer' '
+ append_signoff <<-\EOF >actual &&
+ subject
+
+ body
+
+ Tested-by: my@house
+ Some Trash
+ Signed-off-by: C O Mitter <committer@example.com>
+ EOF
+ cat >expect <<-\EOF &&
+ 4:Subject: [PATCH] subject
+ 8:
+ 10:
+ 13:Signed-off-by: C O Mitter <committer@example.com>
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'signoff: respect trailer config' '
+ append_signoff <<-\EOF >actual &&
+ subject
+
+ Myfooter: x
+ Some Trash
+ EOF
+ cat >expect <<-\EOF &&
+ 4:Subject: [PATCH] subject
+ 8:
+ 11:
+ 12:Signed-off-by: C O Mitter <committer@example.com>
+ EOF
+ test_cmp expect actual &&
+
+ test_config trailer.Myfooter.ifexists add &&
+ append_signoff <<-\EOF >actual &&
+ subject
+
+ Myfooter: x
+ Some Trash
+ EOF
+ cat >expect <<-\EOF &&
+ 4:Subject: [PATCH] subject
+ 8:
+ 11:Signed-off-by: C O Mitter <committer@example.com>
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'signoff: footer begins with non-signoff without @ sign' '
+ append_signoff <<-\EOF >actual &&
+ subject
+
+ body
+
+ Reviewed-id: Noone
+ Tested-by: my@house
+ Change-id: Ideadbeef
+ Signed-off-by: C O Mitter <committer@example.com>
+ Bug: 1234
+ EOF
+ cat >expect <<-\EOF &&
+ 4:Subject: [PATCH] subject
+ 8:
+ 10:
+ 14:Signed-off-by: C O Mitter <committer@example.com>
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'format patch ignores color.ui' '
+ test_unconfig color.ui &&
+ git format-patch --stdout -1 >expect &&
+ test_config color.ui always &&
+ git format-patch --stdout -1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'format patch respects diff.relative' '
+ rm -rf subdir &&
+ mkdir subdir &&
+ echo other content >subdir/file2 &&
+ git add subdir/file2 &&
+ git commit -F msg &&
+ test_unconfig diff.relative &&
+ git format-patch --relative=subdir --stdout -1 >expect &&
+ test_config diff.relative true &&
+ git -C subdir format-patch --stdout -1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'cover letter with invalid --cover-from-description and config' '
+ test_config branch.rebuild-1.description "config subject
+
+body" &&
+ test_must_fail git format-patch --cover-letter --cover-from-description garbage main &&
+ test_config format.coverFromDescription garbage &&
+ test_must_fail git format-patch --cover-letter main
+'
+
+test_expect_success 'cover letter with format.coverFromDescription = default' '
+ test_config branch.rebuild-1.description "config subject
+
+body" &&
+ test_config format.coverFromDescription default &&
+ git checkout rebuild-1 &&
+ git format-patch --stdout --cover-letter main >actual &&
+ grep "^Subject: \[PATCH 0/2\] \*\*\* SUBJECT HERE \*\*\*$" actual &&
+ ! grep "^\*\*\* BLURB HERE \*\*\*$" actual &&
+ grep "^config subject$" actual &&
+ grep "^body$" actual
+'
+
+test_expect_success 'cover letter with --cover-from-description default' '
+ test_config branch.rebuild-1.description "config subject
+
+body" &&
+ git checkout rebuild-1 &&
+ git format-patch --stdout --cover-letter --cover-from-description default main >actual &&
+ grep "^Subject: \[PATCH 0/2\] \*\*\* SUBJECT HERE \*\*\*$" actual &&
+ ! grep "^\*\*\* BLURB HERE \*\*\*$" actual &&
+ grep "^config subject$" actual &&
+ grep "^body$" actual
+'
+
+test_expect_success 'cover letter with format.coverFromDescription = none' '
+ test_config branch.rebuild-1.description "config subject
+
+body" &&
+ test_config format.coverFromDescription none &&
+ git checkout rebuild-1 &&
+ git format-patch --stdout --cover-letter main >actual &&
+ grep "^Subject: \[PATCH 0/2\] \*\*\* SUBJECT HERE \*\*\*$" actual &&
+ grep "^\*\*\* BLURB HERE \*\*\*$" actual &&
+ ! grep "^config subject$" actual &&
+ ! grep "^body$" actual
+'
+
+test_expect_success 'cover letter with --cover-from-description none' '
+ test_config branch.rebuild-1.description "config subject
+
+body" &&
+ git checkout rebuild-1 &&
+ git format-patch --stdout --cover-letter --cover-from-description none main >actual &&
+ grep "^Subject: \[PATCH 0/2\] \*\*\* SUBJECT HERE \*\*\*$" actual &&
+ grep "^\*\*\* BLURB HERE \*\*\*$" actual &&
+ ! grep "^config subject$" actual &&
+ ! grep "^body$" actual
+'
+
+test_expect_success 'cover letter with format.coverFromDescription = message' '
+ test_config branch.rebuild-1.description "config subject
+
+body" &&
+ test_config format.coverFromDescription message &&
+ git checkout rebuild-1 &&
+ git format-patch --stdout --cover-letter main >actual &&
+ grep "^Subject: \[PATCH 0/2\] \*\*\* SUBJECT HERE \*\*\*$" actual &&
+ ! grep "^\*\*\* BLURB HERE \*\*\*$" actual &&
+ grep "^config subject$" actual &&
+ grep "^body$" actual
+'
+
+test_expect_success 'cover letter with --cover-from-description message' '
+ test_config branch.rebuild-1.description "config subject
+
+body" &&
+ git checkout rebuild-1 &&
+ git format-patch --stdout --cover-letter --cover-from-description message main >actual &&
+ grep "^Subject: \[PATCH 0/2\] \*\*\* SUBJECT HERE \*\*\*$" actual &&
+ ! grep "^\*\*\* BLURB HERE \*\*\*$" actual &&
+ grep "^config subject$" actual &&
+ grep "^body$" actual
+'
+
+test_expect_success 'cover letter with format.coverFromDescription = subject' '
+ test_config branch.rebuild-1.description "config subject
+
+body" &&
+ test_config format.coverFromDescription subject &&
+ git checkout rebuild-1 &&
+ git format-patch --stdout --cover-letter main >actual &&
+ grep "^Subject: \[PATCH 0/2\] config subject$" actual &&
+ ! grep "^\*\*\* BLURB HERE \*\*\*$" actual &&
+ ! grep "^config subject$" actual &&
+ grep "^body$" actual
+'
+
+test_expect_success 'cover letter with --cover-from-description subject' '
+ test_config branch.rebuild-1.description "config subject
+
+body" &&
+ git checkout rebuild-1 &&
+ git format-patch --stdout --cover-letter --cover-from-description subject main >actual &&
+ grep "^Subject: \[PATCH 0/2\] config subject$" actual &&
+ ! grep "^\*\*\* BLURB HERE \*\*\*$" actual &&
+ ! grep "^config subject$" actual &&
+ grep "^body$" actual
+'
+
+test_expect_success 'cover letter with format.coverFromDescription = auto (short subject line)' '
+ test_config branch.rebuild-1.description "config subject
+
+body" &&
+ test_config format.coverFromDescription auto &&
+ git checkout rebuild-1 &&
+ git format-patch --stdout --cover-letter main >actual &&
+ grep "^Subject: \[PATCH 0/2\] config subject$" actual &&
+ ! grep "^\*\*\* BLURB HERE \*\*\*$" actual &&
+ ! grep "^config subject$" actual &&
+ grep "^body$" actual
+'
+
+test_expect_success 'cover letter with --cover-from-description auto (short subject line)' '
+ test_config branch.rebuild-1.description "config subject
+
+body" &&
+ git checkout rebuild-1 &&
+ git format-patch --stdout --cover-letter --cover-from-description auto main >actual &&
+ grep "^Subject: \[PATCH 0/2\] config subject$" actual &&
+ ! grep "^\*\*\* BLURB HERE \*\*\*$" actual &&
+ ! grep "^config subject$" actual &&
+ grep "^body$" actual
+'
+
+test_expect_success 'cover letter with format.coverFromDescription = auto (long subject line)' '
+ test_config branch.rebuild-1.description "this is a really long first line and it is over 100 characters long which is the threshold for long subjects
+
+body" &&
+ test_config format.coverFromDescription auto &&
+ git checkout rebuild-1 &&
+ git format-patch --stdout --cover-letter main >actual &&
+ grep "^Subject: \[PATCH 0/2\] \*\*\* SUBJECT HERE \*\*\*$" actual &&
+ ! grep "^\*\*\* BLURB HERE \*\*\*$" actual &&
+ grep "^this is a really long first line and it is over 100 characters long which is the threshold for long subjects$" actual &&
+ grep "^body$" actual
+'
+
+test_expect_success 'cover letter with --cover-from-description auto (long subject line)' '
+ test_config branch.rebuild-1.description "this is a really long first line and it is over 100 characters long which is the threshold for long subjects
+
+body" &&
+ git checkout rebuild-1 &&
+ git format-patch --stdout --cover-letter --cover-from-description auto main >actual &&
+ grep "^Subject: \[PATCH 0/2\] \*\*\* SUBJECT HERE \*\*\*$" actual &&
+ ! grep "^\*\*\* BLURB HERE \*\*\*$" actual &&
+ grep "^this is a really long first line and it is over 100 characters long which is the threshold for long subjects$" actual &&
+ grep "^body$" actual
+'
+
+test_expect_success 'cover letter with command-line --cover-from-description overrides config' '
+ test_config branch.rebuild-1.description "config subject
+
+body" &&
+ test_config format.coverFromDescription none &&
+ git checkout rebuild-1 &&
+ git format-patch --stdout --cover-letter --cover-from-description subject main >actual &&
+ grep "^Subject: \[PATCH 0/2\] config subject$" actual &&
+ ! grep "^\*\*\* BLURB HERE \*\*\*$" actual &&
+ ! grep "^config subject$" actual &&
+ grep "^body$" actual
+'
+
+test_expect_success 'cover letter using branch description (1)' '
+ git checkout rebuild-1 &&
+ test_config branch.rebuild-1.description hello &&
+ git format-patch --stdout --cover-letter main >actual &&
+ grep hello actual
+'
+
+test_expect_success 'cover letter using branch description (2)' '
+ git checkout rebuild-1 &&
+ test_config branch.rebuild-1.description hello &&
+ git format-patch --stdout --cover-letter rebuild-1~2..rebuild-1 >actual &&
+ grep hello actual
+'
+
+test_expect_success 'cover letter using branch description (3)' '
+ git checkout rebuild-1 &&
+ test_config branch.rebuild-1.description hello &&
+ git format-patch --stdout --cover-letter ^main rebuild-1 >actual &&
+ grep hello actual
+'
+
+test_expect_success 'cover letter using branch description (4)' '
+ git checkout rebuild-1 &&
+ test_config branch.rebuild-1.description hello &&
+ git format-patch --stdout --cover-letter main.. >actual &&
+ grep hello actual
+'
+
+test_expect_success 'cover letter using branch description (5)' '
+ git checkout rebuild-1 &&
+ test_config branch.rebuild-1.description hello &&
+ git format-patch --stdout --cover-letter -2 HEAD >actual &&
+ grep hello actual
+'
+
+test_expect_success 'cover letter using branch description (6)' '
+ git checkout rebuild-1 &&
+ test_config branch.rebuild-1.description hello &&
+ git format-patch --stdout --cover-letter -2 >actual &&
+ grep hello actual
+'
+
+test_expect_success 'cover letter with --description-file' '
+ test_when_finished "rm -f description.txt" &&
+ cat >description.txt <<-\EOF &&
+ subject from file
+
+ body from file
+ EOF
+ git checkout rebuild-1 &&
+ git format-patch --stdout --cover-letter --cover-from-description auto \
+ --description-file description.txt main >actual &&
+ grep "^Subject: \[PATCH 0/2\] subject from file$" actual &&
+ grep "^body from file$" actual
+'
+
+test_expect_success 'cover letter with nothing' '
+ git format-patch --stdout --cover-letter >actual &&
+ test_line_count = 0 actual
+'
+
+test_expect_success 'cover letter auto' '
+ mkdir -p tmp &&
+ test_when_finished "rm -rf tmp;
+ git config --unset format.coverletter" &&
+
+ git config format.coverletter auto &&
+ git format-patch -o tmp -1 >list &&
+ test_line_count = 1 list &&
+ git format-patch -o tmp -2 >list &&
+ test_line_count = 3 list
+'
+
+test_expect_success 'cover letter auto user override' '
+ mkdir -p tmp &&
+ test_when_finished "rm -rf tmp;
+ git config --unset format.coverletter" &&
+
+ git config format.coverletter auto &&
+ git format-patch -o tmp --cover-letter -1 >list &&
+ test_line_count = 2 list &&
+ git format-patch -o tmp --cover-letter -2 >list &&
+ test_line_count = 3 list &&
+ git format-patch -o tmp --no-cover-letter -1 >list &&
+ test_line_count = 1 list &&
+ git format-patch -o tmp --no-cover-letter -2 >list &&
+ test_line_count = 2 list
+'
+
+test_expect_success 'format-patch --zero-commit' '
+ git format-patch --zero-commit --stdout v2..v1 >patch2 &&
+ grep "^From " patch2 | sort | uniq >actual &&
+ echo "From $ZERO_OID Mon Sep 17 00:00:00 2001" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'From line has expected format' '
+ git format-patch --stdout v2..v1 >patch2 &&
+ grep "^From " patch2 >from &&
+ grep "^From $OID_REGEX Mon Sep 17 00:00:00 2001$" patch2 >filtered &&
+ test_cmp from filtered
+'
+
+test_expect_success 'format-patch -o with no leading directories' '
+ rm -fr patches &&
+ git format-patch -o patches main..side &&
+ count=$(git rev-list --count main..side) &&
+ ls patches >list &&
+ test_line_count = $count list
+'
+
+test_expect_success 'format-patch -o with leading existing directories' '
+ rm -rf existing-dir &&
+ mkdir existing-dir &&
+ git format-patch -o existing-dir/patches main..side &&
+ count=$(git rev-list --count main..side) &&
+ ls existing-dir/patches >list &&
+ test_line_count = $count list
+'
+
+test_expect_success 'format-patch -o with leading non-existing directories' '
+ rm -rf non-existing-dir &&
+ git format-patch -o non-existing-dir/patches main..side &&
+ count=$(git rev-list --count main..side) &&
+ test_path_is_dir non-existing-dir &&
+ ls non-existing-dir/patches >list &&
+ test_line_count = $count list
+'
+
+test_expect_success 'format-patch format.outputDirectory option' '
+ test_config format.outputDirectory patches &&
+ rm -fr patches &&
+ git format-patch main..side &&
+ count=$(git rev-list --count main..side) &&
+ ls patches >list &&
+ test_line_count = $count list
+'
+
+test_expect_success 'format-patch -o overrides format.outputDirectory' '
+ test_config format.outputDirectory patches &&
+ rm -fr patches patchset &&
+ git format-patch main..side -o patchset &&
+ test_path_is_missing patches &&
+ test_path_is_dir patchset
+'
+
+test_expect_success 'format-patch forbids multiple outputs' '
+ rm -fr outfile outdir &&
+ test_must_fail \
+ git format-patch --stdout --output-directory=outdir &&
+ test_must_fail \
+ git format-patch --stdout --output=outfile &&
+ test_must_fail \
+ git format-patch --output=outfile --output-directory=outdir
+'
+
+test_expect_success 'configured outdir does not conflict with output options' '
+ rm -fr outfile outdir &&
+ test_config format.outputDirectory outdir &&
+ git format-patch --stdout &&
+ test_path_is_missing outdir &&
+ git format-patch --output=outfile &&
+ test_path_is_missing outdir
+'
+
+test_expect_success 'format-patch --output' '
+ rm -fr outfile &&
+ git format-patch -3 --stdout HEAD >expect &&
+ git format-patch -3 --output=outfile HEAD &&
+ test_cmp expect outfile
+'
+
+test_expect_success 'format-patch --cover-letter --output' '
+ rm -fr outfile &&
+ git format-patch --cover-letter -3 --stdout HEAD >expect &&
+ git format-patch --cover-letter -3 --output=outfile HEAD &&
+ test_cmp expect outfile
+'
+
+test_expect_success 'format-patch --base' '
+ git checkout patchid &&
+
+ git format-patch --stdout --base=HEAD~3 -1 >patch &&
+ tail -n 7 patch >actual1 &&
+
+ git format-patch --stdout --base=HEAD~3 HEAD~.. >patch &&
+ tail -n 7 patch >actual2 &&
+
+ echo >expect &&
+ git rev-parse HEAD~3 >commit-id-base &&
+ echo "base-commit: $(cat commit-id-base)" >>expect &&
+
+ git show --patch HEAD~2 >patch &&
+ git patch-id --stable <patch >patch.id.raw &&
+ awk "{print \"prerequisite-patch-id:\", \$1}" <patch.id.raw >>expect &&
+
+ git show --patch HEAD~1 >patch &&
+ git patch-id --stable <patch >patch.id.raw &&
+ awk "{print \"prerequisite-patch-id:\", \$1}" <patch.id.raw >>expect &&
+
+ signature >>expect &&
+ test_cmp expect actual1 &&
+ test_cmp expect actual2 &&
+
+ echo >fail &&
+ echo "base-commit: $(cat commit-id-base)" >>fail &&
+
+ git show --patch HEAD~2 >patch &&
+ git patch-id --unstable <patch >patch.id.raw &&
+ awk "{print \"prerequisite-patch-id:\", \$1}" <patch.id.raw >>fail &&
+
+ git show --patch HEAD~1 >patch &&
+ git patch-id --unstable <patch >patch.id.raw &&
+ awk "{print \"prerequisite-patch-id:\", \$1}" <patch.id.raw >>fail &&
+
+ signature >>fail &&
+ ! test_cmp fail actual1 &&
+ ! test_cmp fail actual2
+'
+
+test_expect_success 'format-patch --base errors out when base commit is in revision list' '
+ test_must_fail git format-patch --base=HEAD -2 &&
+ test_must_fail git format-patch --base=HEAD~1 -2 &&
+ git format-patch --stdout --base=HEAD~2 -2 >patch &&
+ grep "^base-commit:" patch >actual &&
+ git rev-parse HEAD~2 >commit-id-base &&
+ echo "base-commit: $(cat commit-id-base)" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'format-patch --base errors out when base commit is not ancestor of revision list' '
+ # For history as below:
+ #
+ # ---Q---P---Z---Y---*---X
+ # \ /
+ # ------------W
+ #
+ # If "format-patch Z..X" is given, P and Z can not be specified as the base commit
+ git checkout -b topic1 main &&
+ git rev-parse HEAD >commit-id-base &&
+ test_commit P &&
+ git rev-parse HEAD >commit-id-P &&
+ test_commit Z &&
+ git rev-parse HEAD >commit-id-Z &&
+ test_commit Y &&
+ git checkout -b topic2 main &&
+ test_commit W &&
+ git merge topic1 &&
+ test_commit X &&
+ test_must_fail git format-patch --base=$(cat commit-id-P) -3 &&
+ test_must_fail git format-patch --base=$(cat commit-id-Z) -3 &&
+ git format-patch --stdout --base=$(cat commit-id-base) -3 >patch &&
+ grep "^base-commit:" patch >actual &&
+ echo "base-commit: $(cat commit-id-base)" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'format-patch --base=auto' '
+ git checkout -b upstream main &&
+ git checkout -b local upstream &&
+ git branch --set-upstream-to=upstream &&
+ test_commit N1 &&
+ test_commit N2 &&
+ git format-patch --stdout --base=auto -2 >patch &&
+ grep "^base-commit:" patch >actual &&
+ git rev-parse upstream >commit-id-base &&
+ echo "base-commit: $(cat commit-id-base)" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'format-patch errors out when history involves criss-cross' '
+ # setup criss-cross history
+ #
+ # B---M1---D
+ # / \ /
+ # A X
+ # \ / \
+ # C---M2---E
+ #
+ git checkout main &&
+ test_commit A &&
+ git checkout -b xb main &&
+ test_commit B &&
+ git checkout -b xc main &&
+ test_commit C &&
+ git checkout -b xbc xb -- &&
+ git merge xc &&
+ git checkout -b xcb xc -- &&
+ git branch --set-upstream-to=xbc &&
+ git merge xb &&
+ git checkout xbc &&
+ test_commit D &&
+ git checkout xcb &&
+ test_commit E &&
+ test_must_fail git format-patch --base=auto -1
+'
+
+test_expect_success 'format-patch format.useAutoBase whenAble history involves criss-cross' '
+ test_config format.useAutoBase whenAble &&
+ git format-patch -1 >patch &&
+ ! grep "^base-commit:" patch
+'
+
+test_expect_success 'format-patch format.useAutoBase option' '
+ git checkout local &&
+ test_config format.useAutoBase true &&
+ git format-patch --stdout -1 >patch &&
+ grep "^base-commit:" patch >actual &&
+ git rev-parse upstream >commit-id-base &&
+ echo "base-commit: $(cat commit-id-base)" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'format-patch format.useAutoBase option with whenAble' '
+ git checkout local &&
+ test_config format.useAutoBase whenAble &&
+ git format-patch --stdout -1 >patch &&
+ grep "^base-commit:" patch >actual &&
+ git rev-parse upstream >commit-id-base &&
+ echo "base-commit: $(cat commit-id-base)" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'format-patch --base overrides format.useAutoBase' '
+ test_config format.useAutoBase true &&
+ git format-patch --stdout --base=HEAD~1 -1 >patch &&
+ grep "^base-commit:" patch >actual &&
+ git rev-parse HEAD~1 >commit-id-base &&
+ echo "base-commit: $(cat commit-id-base)" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'format-patch --no-base overrides format.useAutoBase' '
+ test_config format.useAutoBase true &&
+ git format-patch --stdout --no-base -1 >patch &&
+ ! grep "^base-commit:" patch
+'
+
+test_expect_success 'format-patch --no-base overrides format.useAutoBase whenAble' '
+ test_config format.useAutoBase whenAble &&
+ git format-patch --stdout --no-base -1 >patch &&
+ ! grep "^base-commit:" patch
+'
+
+test_expect_success 'format-patch --base with --attach' '
+ git format-patch --attach=mimemime --stdout --base=HEAD~ -1 >patch &&
+ sed -n -e "/^base-commit:/s/.*/1/p" -e "/^---*mimemime--$/s/.*/2/p" \
+ patch >actual &&
+ test_write_lines 1 2 >expect &&
+ test_cmp expect actual
+'
+test_expect_success 'format-patch --attach cover-letter only is non-multipart' '
+ test_when_finished "rm -fr patches" &&
+ git format-patch -o patches --cover-letter --attach=mimemime --base=HEAD~ -1 &&
+ ! grep -E "^--+mimemime" patches/0000*.patch &&
+ grep -E "^--+mimemime$" patches/0001*.patch >output &&
+ test_line_count = 2 output &&
+ grep -E "^--+mimemime--$" patches/0001*.patch >output &&
+ test_line_count = 1 output
+'
+
+test_expect_success 'format-patch with format.attach' '
+ test_when_finished "rm -fr patches" &&
+ separator=attachment-separator &&
+ test_config format.attach "$separator" &&
+ filename=$(git format-patch -o patches -1) &&
+ grep "^Content-Type: multipart/.*$separator" "$filename"
+'
+
+test_expect_success 'format-patch with format.attach=disabled' '
+ test_when_finished "rm -fr patches" &&
+ separator=attachment-separator &&
+ test_config_global format.attach "$separator" &&
+ test_config format.attach "" &&
+ filename=$(git format-patch -o patches -1) &&
+ # The output should not even declare content type for text/plain.
+ ! grep "^Content-Type: multipart/" "$filename"
+'
+
+test_expect_success '-c format.mboxrd format-patch' '
+ sp=" " &&
+ cat >msg <<-INPUT_END &&
+ mboxrd should escape the body
+
+ From could trip up a loose mbox parser
+ >From extra escape for reversibility
+ >>From extra escape for reversibility 2
+ from lower case not escaped
+ Fromm bad speling not escaped
+ From with leading space not escaped
+
+ F
+ From
+ From$sp
+ From $sp
+ From $sp
+ INPUT_END
+
+ cat >expect <<-INPUT_END &&
+ >From could trip up a loose mbox parser
+ >>From extra escape for reversibility
+ >>>From extra escape for reversibility 2
+ from lower case not escaped
+ Fromm bad speling not escaped
+ From with leading space not escaped
+
+ F
+ From
+ From
+ From
+ From
+ INPUT_END
+
+ C=$(git commit-tree HEAD^^{tree} -p HEAD <msg) &&
+ git -c format.mboxrd format-patch --stdout -1 $C~1..$C >patch &&
+ git format-patch --pretty=mboxrd --stdout -1 $C~1..$C >compat &&
+ test_cmp patch compat &&
+ git grep -h --no-index -A11 \
+ "^>From could trip up a loose mbox parser" patch >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'interdiff: setup' '
+ git checkout -b boop main &&
+ test_commit fnorp blorp &&
+ test_commit fleep blorp
+'
+
+test_expect_success 'interdiff: cover-letter' '
+ sed "y/q/ /" >expect <<-\EOF &&
+ +fleep
+ --q
+ EOF
+ git format-patch --cover-letter --interdiff=boop~2 -1 boop &&
+ test_grep "^Interdiff:$" 0000-cover-letter.patch &&
+ test_grep ! "^Interdiff:$" 0001-fleep.patch &&
+ sed "1,/^@@ /d; /^-- $/q" 0000-cover-letter.patch >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'interdiff: reroll-count' '
+ git format-patch --cover-letter --interdiff=boop~2 -v2 -1 boop &&
+ test_grep "^Interdiff ..* v1:$" v2-0000-cover-letter.patch
+'
+
+test_expect_success 'interdiff: reroll-count with a non-integer' '
+ git format-patch --cover-letter --interdiff=boop~2 -v2.2 -1 boop &&
+ test_grep "^Interdiff:$" v2.2-0000-cover-letter.patch
+'
+
+test_expect_success 'interdiff: reroll-count with a integer' '
+ git format-patch --cover-letter --interdiff=boop~2 -v2 -1 boop &&
+ test_grep "^Interdiff ..* v1:$" v2-0000-cover-letter.patch
+'
+
+test_expect_success 'interdiff: solo-patch' '
+ cat >expect <<-\EOF &&
+ +fleep
+
+ EOF
+ git format-patch --interdiff=boop~2 -1 boop &&
+ test_grep "^Interdiff:$" 0001-fleep.patch &&
+ sed "1,/^ @@ /d; /^$/q" 0001-fleep.patch >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'format-patch does not respect diff.noprefix' '
+ git -c diff.noprefix format-patch -1 --stdout >actual &&
+ grep "^--- a/blorp" actual
+'
+
+test_expect_success 'format-patch respects format.noprefix' '
+ git -c format.noprefix format-patch -1 --stdout >actual &&
+ grep "^--- blorp" actual
+'
+
+test_expect_success 'format-patch --default-prefix overrides format.noprefix' '
+ git -c format.noprefix \
+ format-patch -1 --default-prefix --stdout >actual &&
+ grep "^--- a/blorp" actual
+'
+
+test_done
diff --git a/t/t4015-diff-whitespace.sh b/t/t4015-diff-whitespace.sh
new file mode 100755
index 0000000..b443626
--- /dev/null
+++ b/t/t4015-diff-whitespace.sh
@@ -0,0 +1,2380 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Johannes E. Schindelin
+# Copyright (c) 2023 Google LLC
+
+test_description='Test special whitespace in diff engine.
+
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-diff.sh
+
+for opt_res in --patch --quiet -s --stat --shortstat --dirstat=lines \
+ --raw! --name-only! --name-status!
+do
+ opts=${opt_res%!} expect_failure=
+ test "$opts" = "$opt_res" ||
+ expect_failure="test_expect_code 1"
+
+ test_expect_success "status with $opts (different)" '
+ echo foo >x &&
+ git add x &&
+ echo bar >x &&
+ test_expect_code 1 git diff -w $opts --exit-code x
+ '
+
+ test_expect_success POSIXPERM "status with $opts (mode differs)" '
+ test_when_finished "git update-index --chmod=-x x" &&
+ echo foo >x &&
+ git add x &&
+ git update-index --chmod=+x x &&
+ test_expect_code 1 git diff -w $opts --exit-code x
+ '
+
+ test_expect_success "status with $opts (removing an empty file)" '
+ : >x &&
+ git add x &&
+ rm x &&
+ test_expect_code 1 git diff -w $opts --exit-code -- x
+ '
+
+ test_expect_success "status with $opts (different but equivalent)" '
+ echo foo >x &&
+ git add x &&
+ echo " foo" >x &&
+ $expect_failure git diff -w $opts --exit-code x
+ '
+done
+
+test_expect_success "Ray Lehtiniemi's example" '
+ cat <<-\EOF >x &&
+ do {
+ nothing;
+ } while (0);
+ EOF
+ git update-index --add x &&
+ old_hash_x=$(git hash-object x) &&
+ before=$(git rev-parse --short "$old_hash_x") &&
+
+ cat <<-\EOF >x &&
+ do
+ {
+ nothing;
+ }
+ while (0);
+ EOF
+ new_hash_x=$(git hash-object x) &&
+ after=$(git rev-parse --short "$new_hash_x") &&
+
+ cat <<-EOF >expect &&
+ diff --git a/x b/x
+ index $before..$after 100644
+ --- a/x
+ +++ b/x
+ @@ -1,3 +1,5 @@
+ -do {
+ +do
+ +{
+ nothing;
+ -} while (0);
+ +}
+ +while (0);
+ EOF
+
+ git diff >out &&
+ test_cmp expect out &&
+
+ git diff -w >out &&
+ test_cmp expect out &&
+
+ git diff -b >out &&
+ test_cmp expect out
+'
+
+test_expect_success 'another test, without options' '
+ tr Q "\015" <<-\EOF >x &&
+ whitespace at beginning
+ whitespace change
+ whitespace in the middle
+ whitespace at end
+ unchanged line
+ CR at endQ
+ EOF
+
+ git update-index x &&
+ old_hash_x=$(git hash-object x) &&
+ before=$(git rev-parse --short "$old_hash_x") &&
+
+ tr "_" " " <<-\EOF >x &&
+ _ whitespace at beginning
+ whitespace change
+ white space in the middle
+ whitespace at end__
+ unchanged line
+ CR at end
+ EOF
+ new_hash_x=$(git hash-object x) &&
+ after=$(git rev-parse --short "$new_hash_x") &&
+
+ tr "Q_" "\015 " <<-EOF >expect &&
+ diff --git a/x b/x
+ index $before..$after 100644
+ --- a/x
+ +++ b/x
+ @@ -1,6 +1,6 @@
+ -whitespace at beginning
+ -whitespace change
+ -whitespace in the middle
+ -whitespace at end
+ + whitespace at beginning
+ +whitespace change
+ +white space in the middle
+ +whitespace at end__
+ unchanged line
+ -CR at endQ
+ +CR at end
+ EOF
+
+ git diff >out &&
+ test_cmp expect out &&
+
+ git diff -w >out &&
+ test_must_be_empty out &&
+
+ git diff -w -b >out &&
+ test_must_be_empty out &&
+
+ git diff -w --ignore-space-at-eol >out &&
+ test_must_be_empty out &&
+
+ git diff -w -b --ignore-space-at-eol >out &&
+ test_must_be_empty out &&
+
+ git diff -w --ignore-cr-at-eol >out &&
+ test_must_be_empty out &&
+
+ tr "Q_" "\015 " <<-EOF >expect &&
+ diff --git a/x b/x
+ index $before..$after 100644
+ --- a/x
+ +++ b/x
+ @@ -1,6 +1,6 @@
+ -whitespace at beginning
+ +_ whitespace at beginning
+ whitespace change
+ -whitespace in the middle
+ +white space in the middle
+ whitespace at end__
+ unchanged line
+ CR at end
+ EOF
+ git diff -b >out &&
+ test_cmp expect out &&
+
+ git diff -b --ignore-space-at-eol >out &&
+ test_cmp expect out &&
+
+ git diff -b --ignore-cr-at-eol >out &&
+ test_cmp expect out &&
+
+ tr "Q_" "\015 " <<-EOF >expect &&
+ diff --git a/x b/x
+ index $before..$after 100644
+ --- a/x
+ +++ b/x
+ @@ -1,6 +1,6 @@
+ -whitespace at beginning
+ -whitespace change
+ -whitespace in the middle
+ +_ whitespace at beginning
+ +whitespace change
+ +white space in the middle
+ whitespace at end__
+ unchanged line
+ CR at end
+ EOF
+ git diff --ignore-space-at-eol >out &&
+ test_cmp expect out &&
+
+ git diff --ignore-space-at-eol --ignore-cr-at-eol >out &&
+ test_cmp expect out &&
+
+ tr "Q_" "\015 " <<-EOF >expect &&
+ diff --git a/x b/x
+ index_$before..$after 100644
+ --- a/x
+ +++ b/x
+ @@ -1,6 +1,6 @@
+ -whitespace at beginning
+ -whitespace change
+ -whitespace in the middle
+ -whitespace at end
+ +_ whitespace at beginning
+ +whitespace_ _change
+ +white space in the middle
+ +whitespace at end__
+ unchanged line
+ CR at end
+ EOF
+ git diff --ignore-cr-at-eol >out &&
+ test_cmp expect out
+'
+
+test_expect_success 'ignore-blank-lines: only new lines' '
+ test_seq 5 >x &&
+ git update-index x &&
+ test_seq 5 | sed "/3/i\\
+" >x &&
+ git diff --ignore-blank-lines >out &&
+ test_must_be_empty out
+'
+
+test_expect_success 'ignore-blank-lines: only new lines with space' '
+ test_seq 5 >x &&
+ git update-index x &&
+ test_seq 5 | sed "/3/i\\
+ " >x &&
+ git diff -w --ignore-blank-lines >out &&
+ test_must_be_empty out
+'
+
+test_expect_success 'ignore-blank-lines: after change' '
+ cat <<-\EOF >x &&
+ 1
+ 2
+
+ 3
+ 4
+ 5
+
+ 6
+ 7
+ EOF
+ git update-index x &&
+ cat <<-\EOF >x &&
+ change
+
+ 1
+ 2
+ 3
+ 4
+ 5
+ 6
+
+ 7
+ EOF
+ git diff --inter-hunk-context=100 --ignore-blank-lines >out.tmp &&
+ cat <<-\EOF >expected &&
+ diff --git a/x b/x
+ --- a/x
+ +++ b/x
+ @@ -1,6 +1,7 @@
+ +change
+ +
+ 1
+ 2
+ -
+ 3
+ 4
+ 5
+ EOF
+ compare_diff_patch expected out.tmp
+'
+
+test_expect_success 'ignore-blank-lines: before change' '
+ cat <<-\EOF >x &&
+ 1
+ 2
+
+ 3
+ 4
+ 5
+ 6
+ 7
+ EOF
+ git update-index x &&
+ cat <<-\EOF >x &&
+
+ 1
+ 2
+ 3
+ 4
+ 5
+
+ 6
+ 7
+ change
+ EOF
+ git diff --inter-hunk-context=100 --ignore-blank-lines >out.tmp &&
+ cat <<-\EOF >expected &&
+ diff --git a/x b/x
+ --- a/x
+ +++ b/x
+ @@ -4,5 +4,7 @@
+ 3
+ 4
+ 5
+ +
+ 6
+ 7
+ +change
+ EOF
+ compare_diff_patch expected out.tmp
+'
+
+test_expect_success 'ignore-blank-lines: between changes' '
+ cat <<-\EOF >x &&
+ 1
+ 2
+ 3
+ 4
+ 5
+
+
+ 6
+ 7
+ 8
+ 9
+ 10
+ EOF
+ git update-index x &&
+ cat <<-\EOF >x &&
+ change
+ 1
+ 2
+
+ 3
+ 4
+ 5
+ 6
+ 7
+ 8
+
+ 9
+ 10
+ change
+ EOF
+ git diff --ignore-blank-lines >out.tmp &&
+ cat <<-\EOF >expected &&
+ diff --git a/x b/x
+ --- a/x
+ +++ b/x
+ @@ -1,5 +1,7 @@
+ +change
+ 1
+ 2
+ +
+ 3
+ 4
+ 5
+ @@ -8,5 +8,7 @@
+ 6
+ 7
+ 8
+ +
+ 9
+ 10
+ +change
+ EOF
+ compare_diff_patch expected out.tmp
+'
+
+test_expect_success 'ignore-blank-lines: between changes (with interhunkctx)' '
+ test_seq 10 >x &&
+ git update-index x &&
+ cat <<-\EOF >x &&
+ change
+ 1
+ 2
+
+ 3
+ 4
+ 5
+
+ 6
+ 7
+ 8
+ 9
+
+ 10
+ change
+ EOF
+ git diff --inter-hunk-context=2 --ignore-blank-lines >out.tmp &&
+ cat <<-\EOF >expected &&
+ diff --git a/x b/x
+ --- a/x
+ +++ b/x
+ @@ -1,10 +1,15 @@
+ +change
+ 1
+ 2
+ +
+ 3
+ 4
+ 5
+ +
+ 6
+ 7
+ 8
+ 9
+ +
+ 10
+ +change
+ EOF
+ compare_diff_patch expected out.tmp
+'
+
+test_expect_success 'ignore-blank-lines: scattered spaces' '
+ test_seq 10 >x &&
+ git update-index x &&
+ cat <<-\EOF >x &&
+ change
+ 1
+ 2
+ 3
+
+ 4
+
+ 5
+
+ 6
+
+
+ 7
+
+ 8
+ 9
+ 10
+ change
+ EOF
+ git diff --inter-hunk-context=4 --ignore-blank-lines >out.tmp &&
+ cat <<-\EOF >expected &&
+ diff --git a/x b/x
+ --- a/x
+ +++ b/x
+ @@ -1,3 +1,4 @@
+ +change
+ 1
+ 2
+ 3
+ @@ -8,3 +15,4 @@
+ 8
+ 9
+ 10
+ +change
+ EOF
+ compare_diff_patch expected out.tmp
+'
+
+test_expect_success 'ignore-blank-lines: spaces coalesce' '
+ test_seq 6 >x &&
+ git update-index x &&
+ cat <<-\EOF >x &&
+ change
+ 1
+ 2
+ 3
+
+ 4
+
+ 5
+
+ 6
+ change
+ EOF
+ git diff --inter-hunk-context=4 --ignore-blank-lines >out.tmp &&
+ cat <<-\EOF >expected &&
+ diff --git a/x b/x
+ --- a/x
+ +++ b/x
+ @@ -1,6 +1,11 @@
+ +change
+ 1
+ 2
+ 3
+ +
+ 4
+ +
+ 5
+ +
+ 6
+ +change
+ EOF
+ compare_diff_patch expected out.tmp
+'
+
+test_expect_success 'ignore-blank-lines: mix changes and blank lines' '
+ test_seq 16 >x &&
+ git update-index x &&
+ cat <<-\EOF >x &&
+ change
+ 1
+ 2
+
+ 3
+ 4
+ 5
+ change
+ 6
+ 7
+ 8
+
+ 9
+ 10
+ 11
+ change
+ 12
+ 13
+ 14
+
+ 15
+ 16
+ change
+ EOF
+ git diff --ignore-blank-lines >out.tmp &&
+ cat <<-\EOF >expected &&
+ diff --git a/x b/x
+ --- a/x
+ +++ b/x
+ @@ -1,8 +1,11 @@
+ +change
+ 1
+ 2
+ +
+ 3
+ 4
+ 5
+ +change
+ 6
+ 7
+ 8
+ @@ -9,8 +13,11 @@
+ 9
+ 10
+ 11
+ +change
+ 12
+ 13
+ 14
+ +
+ 15
+ 16
+ +change
+ EOF
+ compare_diff_patch expected out.tmp
+'
+
+test_expect_success 'check mixed spaces and tabs in indent' '
+ # This is indented with SP HT SP.
+ echo " foo();" >x &&
+ test_must_fail git diff --check >check &&
+ grep "space before tab in indent" check
+'
+
+test_expect_success 'check mixed tabs and spaces in indent' '
+ # This is indented with HT SP HT.
+ echo " foo();" >x &&
+ test_must_fail git diff --check >check &&
+ grep "space before tab in indent" check
+'
+
+test_expect_success 'check with no whitespace errors' '
+ git commit -m "snapshot" &&
+ echo "foo();" >x &&
+ git diff --check
+'
+
+test_expect_success 'check with trailing whitespace' '
+ echo "foo(); " >x &&
+ test_must_fail git diff --check
+'
+
+test_expect_success 'check with space before tab in indent' '
+ # indent has space followed by hard tab
+ echo " foo();" >x &&
+ test_must_fail git diff --check
+'
+
+test_expect_success '--check and --exit-code are not exclusive' '
+ git checkout x &&
+ git diff --check --exit-code
+'
+
+test_expect_success '--check and --quiet are not exclusive' '
+ git diff --check --quiet
+'
+
+test_expect_success '-w and --exit-code interact sensibly' '
+ test_when_finished "git checkout x" &&
+ {
+ test_seq 15 &&
+ echo " 16"
+ } >x &&
+ test_must_fail git diff --exit-code &&
+ git diff -w >actual &&
+ test_must_be_empty actual &&
+ git diff -w --exit-code
+'
+
+test_expect_success '-I and --exit-code interact sensibly' '
+ test_when_finished "git checkout x" &&
+ {
+ test_seq 15 &&
+ echo " 16"
+ } >x &&
+ test_must_fail git diff --exit-code &&
+ git diff -I. >actual &&
+ test_must_be_empty actual &&
+ git diff -I. --exit-code
+'
+
+test_expect_success 'check staged with no whitespace errors' '
+ echo "foo();" >x &&
+ git add x &&
+ git diff --cached --check
+'
+
+test_expect_success 'check staged with trailing whitespace' '
+ echo "foo(); " >x &&
+ git add x &&
+ test_must_fail git diff --cached --check
+'
+
+test_expect_success 'check staged with space before tab in indent' '
+ # indent has space followed by hard tab
+ echo " foo();" >x &&
+ git add x &&
+ test_must_fail git diff --cached --check
+'
+
+test_expect_success 'check with no whitespace errors (diff-index)' '
+ echo "foo();" >x &&
+ git add x &&
+ git diff-index --check HEAD
+'
+
+test_expect_success 'check with trailing whitespace (diff-index)' '
+ echo "foo(); " >x &&
+ git add x &&
+ test_must_fail git diff-index --check HEAD
+'
+
+test_expect_success 'check with space before tab in indent (diff-index)' '
+ # indent has space followed by hard tab
+ echo " foo();" >x &&
+ git add x &&
+ test_must_fail git diff-index --check HEAD
+'
+
+test_expect_success 'check staged with no whitespace errors (diff-index)' '
+ echo "foo();" >x &&
+ git add x &&
+ git diff-index --cached --check HEAD
+'
+
+test_expect_success 'check staged with trailing whitespace (diff-index)' '
+ echo "foo(); " >x &&
+ git add x &&
+ test_must_fail git diff-index --cached --check HEAD
+'
+
+test_expect_success 'check staged with space before tab in indent (diff-index)' '
+ # indent has space followed by hard tab
+ echo " foo();" >x &&
+ git add x &&
+ test_must_fail git diff-index --cached --check HEAD
+'
+
+test_expect_success 'check with no whitespace errors (diff-tree)' '
+ echo "foo();" >x &&
+ git commit -m "new commit" x &&
+ git diff-tree --check HEAD^ HEAD
+'
+
+test_expect_success 'check with trailing whitespace (diff-tree)' '
+ echo "foo(); " >x &&
+ git commit -m "another commit" x &&
+ test_must_fail git diff-tree --check HEAD^ HEAD
+'
+
+test_expect_success 'check with space before tab in indent (diff-tree)' '
+ # indent has space followed by hard tab
+ echo " foo();" >x &&
+ git commit -m "yet another" x &&
+ test_must_fail git diff-tree --check HEAD^ HEAD
+'
+
+test_expect_success 'check with ignored trailing whitespace attr (diff-tree)' '
+ test_when_finished "git reset --hard HEAD^" &&
+
+ # create a whitespace error that should be ignored
+ echo "* -whitespace" >.gitattributes &&
+ git add .gitattributes &&
+ echo "foo(); " >x &&
+ git add x &&
+ git commit -m "add trailing space" &&
+
+ # with a worktree diff-tree ignores the whitespace error
+ git diff-tree --root --check HEAD &&
+
+ # without a worktree diff-tree still ignores the whitespace error
+ git -C .git diff-tree --root --check HEAD
+'
+
+test_expect_success 'check trailing whitespace (trailing-space: off)' '
+ git config core.whitespace "-trailing-space" &&
+ echo "foo (); " >x &&
+ git diff --check
+'
+
+test_expect_success 'check trailing whitespace (trailing-space: on)' '
+ git config core.whitespace "trailing-space" &&
+ echo "foo (); " >x &&
+ test_must_fail git diff --check
+'
+
+test_expect_success 'check space before tab in indent (space-before-tab: off)' '
+ # indent contains space followed by HT
+ git config core.whitespace "-space-before-tab" &&
+ echo " foo ();" >x &&
+ git diff --check
+'
+
+test_expect_success 'check space before tab in indent (space-before-tab: on)' '
+ # indent contains space followed by HT
+ git config core.whitespace "space-before-tab" &&
+ echo " foo (); " >x &&
+ test_must_fail git diff --check
+'
+
+test_expect_success 'check spaces as indentation (indent-with-non-tab: off)' '
+ git config core.whitespace "-indent-with-non-tab" &&
+ echo " foo ();" >x &&
+ git diff --check
+'
+
+test_expect_success 'check spaces as indentation (indent-with-non-tab: on)' '
+ git config core.whitespace "indent-with-non-tab" &&
+ echo " foo ();" >x &&
+ test_must_fail git diff --check
+'
+
+test_expect_success 'ditto, but tabwidth=9' '
+ git config core.whitespace "indent-with-non-tab,tabwidth=9" &&
+ git diff --check
+'
+
+test_expect_success 'check tabs and spaces as indentation (indent-with-non-tab: on)' '
+ git config core.whitespace "indent-with-non-tab" &&
+ echo " foo ();" >x &&
+ test_must_fail git diff --check
+'
+
+test_expect_success 'ditto, but tabwidth=10' '
+ git config core.whitespace "indent-with-non-tab,tabwidth=10" &&
+ test_must_fail git diff --check
+'
+
+test_expect_success 'ditto, but tabwidth=20' '
+ git config core.whitespace "indent-with-non-tab,tabwidth=20" &&
+ git diff --check
+'
+
+test_expect_success 'check tabs as indentation (tab-in-indent: off)' '
+ git config core.whitespace "-tab-in-indent" &&
+ echo " foo ();" >x &&
+ git diff --check
+'
+
+test_expect_success 'check tabs as indentation (tab-in-indent: on)' '
+ git config core.whitespace "tab-in-indent" &&
+ echo " foo ();" >x &&
+ test_must_fail git diff --check
+'
+
+test_expect_success 'check tabs and spaces as indentation (tab-in-indent: on)' '
+ git config core.whitespace "tab-in-indent" &&
+ echo " foo ();" >x &&
+ test_must_fail git diff --check
+'
+
+test_expect_success 'ditto, but tabwidth=1 (must be irrelevant)' '
+ git config core.whitespace "tab-in-indent,tabwidth=1" &&
+ test_must_fail git diff --check
+'
+
+test_expect_success 'check tab-in-indent and indent-with-non-tab conflict' '
+ git config core.whitespace "tab-in-indent,indent-with-non-tab" &&
+ echo "foo ();" >x &&
+ test_must_fail git diff --check
+'
+
+test_expect_success 'check tab-in-indent excluded from wildcard whitespace attribute' '
+ git config --unset core.whitespace &&
+ echo "x whitespace" >.gitattributes &&
+ echo " foo ();" >x &&
+ git diff --check &&
+ rm -f .gitattributes
+'
+
+test_expect_success 'line numbers in --check output are correct' '
+ echo "" >x &&
+ echo "foo(); " >>x &&
+ test_must_fail git diff --check >check &&
+ grep "x:2:" check
+'
+
+test_expect_success 'checkdiff detects new trailing blank lines (1)' '
+ echo "foo();" >x &&
+ echo "" >>x &&
+ test_must_fail git diff --check >check &&
+ grep "new blank line" check
+'
+
+test_expect_success 'checkdiff detects new trailing blank lines (2)' '
+ test_write_lines a b "" "" >x &&
+ git add x &&
+ test_write_lines a "" "" "" "" >x &&
+ test_must_fail git diff --check >check &&
+ grep "new blank line" check
+'
+
+test_expect_success 'checkdiff allows new blank lines' '
+ git checkout x &&
+ mv x y &&
+ (
+ echo "/* This is new */" &&
+ echo "" &&
+ cat y
+ ) >x &&
+ git diff --check
+'
+
+test_expect_success 'whitespace-only changes not reported (diff)' '
+ git reset --hard &&
+ echo >x "hello world" &&
+ git add x &&
+ git commit -m "hello 1" &&
+ echo >x "hello world" &&
+ git diff -b >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'whitespace-only changes not reported (diffstat)' '
+ # reuse state from previous test
+ git diff --stat -b >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'whitespace changes with modification reported (diffstat)' '
+ git reset --hard &&
+ echo >x "hello world" &&
+ git update-index --chmod=+x x &&
+ git diff --stat --cached -b >actual &&
+ cat <<-EOF >expect &&
+ x | 0
+ 1 file changed, 0 insertions(+), 0 deletions(-)
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'whitespace-only changes reported across renames (diffstat)' '
+ git reset --hard &&
+ for i in 1 2 3 4 5 6 7 8 9; do echo "$i$i$i$i$i$i" || return 1; done >x &&
+ git add x &&
+ git commit -m "base" &&
+ sed -e "5s/^/ /" x >z &&
+ git rm x &&
+ git add z &&
+ git diff -w -M --cached --stat >actual &&
+ cat <<-EOF >expect &&
+ x => z | 0
+ 1 file changed, 0 insertions(+), 0 deletions(-)
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'whitespace-only changes reported across renames' '
+ git reset --hard HEAD~1 &&
+ for i in 1 2 3 4 5 6 7 8 9; do echo "$i$i$i$i$i$i" || return 1; done >x &&
+ git add x &&
+ hash_x=$(git hash-object x) &&
+ before=$(git rev-parse --short "$hash_x") &&
+ git commit -m "base" &&
+ sed -e "5s/^/ /" x >z &&
+ git rm x &&
+ git add z &&
+ hash_z=$(git hash-object z) &&
+ after=$(git rev-parse --short "$hash_z") &&
+ git diff -w -M --cached >actual.raw &&
+ sed -e "/^similarity index /s/[0-9][0-9]*/NUM/" actual.raw >actual &&
+ cat <<-EOF >expect &&
+ diff --git a/x b/z
+ similarity index NUM%
+ rename from x
+ rename to z
+ index $before..$after 100644
+ EOF
+ test_cmp expect actual
+'
+
+cat >expected <<\EOF
+diff --git a/empty b/void
+similarity index 100%
+rename from empty
+rename to void
+EOF
+
+test_expect_success 'rename empty' '
+ git reset --hard &&
+ >empty &&
+ git add empty &&
+ git commit -m empty &&
+ git mv empty void &&
+ git diff -w --cached -M >current &&
+ test_cmp expected current
+'
+
+test_expect_success 'combined diff with autocrlf conversion' '
+
+ git reset --hard &&
+ test_commit "one side" x hello one-side &&
+ git checkout HEAD^ &&
+ echo >x goodbye &&
+ git commit -m "the other side" x &&
+ git config core.autocrlf true &&
+ test_must_fail git merge one-side >actual &&
+ test_grep "Automatic merge failed" actual &&
+
+ git diff >actual.raw &&
+ sed -e "1,/^@@@/d" actual.raw >actual &&
+ ! grep "^-" actual
+
+'
+
+# Start testing the colored format for whitespace checks
+
+test_expect_success 'setup diff colors' '
+ git config color.diff.plain normal &&
+ git config color.diff.meta bold &&
+ git config color.diff.frag cyan &&
+ git config color.diff.func normal &&
+ git config color.diff.old red &&
+ git config color.diff.new green &&
+ git config color.diff.commit yellow &&
+ git config color.diff.whitespace blue &&
+
+ git config core.autocrlf false
+'
+
+test_expect_success 'diff that introduces a line with only tabs' '
+ git config core.whitespace blank-at-eol &&
+ git reset --hard &&
+ echo "test" >x &&
+ old_hash_x=$(git hash-object x) &&
+ before=$(git rev-parse --short "$old_hash_x") &&
+ git commit -m "initial" x &&
+ echo "{NTN}" | tr "NT" "\n\t" >>x &&
+ new_hash_x=$(git hash-object x) &&
+ after=$(git rev-parse --short "$new_hash_x") &&
+ git diff --color >current.raw &&
+ test_decode_color <current.raw >current &&
+
+ cat >expected <<-EOF &&
+ <BOLD>diff --git a/x b/x<RESET>
+ <BOLD>index $before..$after 100644<RESET>
+ <BOLD>--- a/x<RESET>
+ <BOLD>+++ b/x<RESET>
+ <CYAN>@@ -1 +1,4 @@<RESET>
+ test<RESET>
+ <GREEN>+<RESET><GREEN>{<RESET>
+ <GREEN>+<RESET><BLUE> <RESET>
+ <GREEN>+<RESET><GREEN>}<RESET>
+ EOF
+
+ test_cmp expected current
+'
+
+test_expect_success 'diff that introduces and removes ws breakages' '
+ git reset --hard &&
+ {
+ echo "0. blank-at-eol " &&
+ echo "1. blank-at-eol "
+ } >x &&
+ old_hash_x=$(git hash-object x) &&
+ before=$(git rev-parse --short "$old_hash_x") &&
+ git commit -a --allow-empty -m preimage &&
+ {
+ echo "0. blank-at-eol " &&
+ echo "1. still-blank-at-eol " &&
+ echo "2. and a new line "
+ } >x &&
+ new_hash_x=$(git hash-object x) &&
+ after=$(git rev-parse --short "$new_hash_x") &&
+
+ git diff --color >current.raw &&
+ test_decode_color <current.raw >current &&
+
+ cat >expected <<-EOF &&
+ <BOLD>diff --git a/x b/x<RESET>
+ <BOLD>index $before..$after 100644<RESET>
+ <BOLD>--- a/x<RESET>
+ <BOLD>+++ b/x<RESET>
+ <CYAN>@@ -1,2 +1,3 @@<RESET>
+ 0. blank-at-eol <RESET>
+ <RED>-1. blank-at-eol <RESET>
+ <GREEN>+<RESET><GREEN>1. still-blank-at-eol<RESET><BLUE> <RESET>
+ <GREEN>+<RESET><GREEN>2. and a new line<RESET><BLUE> <RESET>
+ EOF
+
+ test_cmp expected current
+'
+
+test_expect_success 'ws-error-highlight test setup' '
+
+ git reset --hard &&
+ {
+ echo "0. blank-at-eol " &&
+ echo "1. blank-at-eol "
+ } >x &&
+ old_hash_x=$(git hash-object x) &&
+ before=$(git rev-parse --short "$old_hash_x") &&
+ git commit -a --allow-empty -m preimage &&
+ {
+ echo "0. blank-at-eol " &&
+ echo "1. still-blank-at-eol " &&
+ echo "2. and a new line "
+ } >x &&
+ new_hash_x=$(git hash-object x) &&
+ after=$(git rev-parse --short "$new_hash_x") &&
+
+ cat >expect.default-old <<-EOF &&
+ <BOLD>diff --git a/x b/x<RESET>
+ <BOLD>index $before..$after 100644<RESET>
+ <BOLD>--- a/x<RESET>
+ <BOLD>+++ b/x<RESET>
+ <CYAN>@@ -1,2 +1,3 @@<RESET>
+ 0. blank-at-eol <RESET>
+ <RED>-<RESET><RED>1. blank-at-eol<RESET><BLUE> <RESET>
+ <GREEN>+<RESET><GREEN>1. still-blank-at-eol<RESET><BLUE> <RESET>
+ <GREEN>+<RESET><GREEN>2. and a new line<RESET><BLUE> <RESET>
+ EOF
+
+ cat >expect.all <<-EOF &&
+ <BOLD>diff --git a/x b/x<RESET>
+ <BOLD>index $before..$after 100644<RESET>
+ <BOLD>--- a/x<RESET>
+ <BOLD>+++ b/x<RESET>
+ <CYAN>@@ -1,2 +1,3 @@<RESET>
+ <RESET>0. blank-at-eol<RESET><BLUE> <RESET>
+ <RED>-<RESET><RED>1. blank-at-eol<RESET><BLUE> <RESET>
+ <GREEN>+<RESET><GREEN>1. still-blank-at-eol<RESET><BLUE> <RESET>
+ <GREEN>+<RESET><GREEN>2. and a new line<RESET><BLUE> <RESET>
+ EOF
+
+ cat >expect.none <<-EOF
+ <BOLD>diff --git a/x b/x<RESET>
+ <BOLD>index $before..$after 100644<RESET>
+ <BOLD>--- a/x<RESET>
+ <BOLD>+++ b/x<RESET>
+ <CYAN>@@ -1,2 +1,3 @@<RESET>
+ 0. blank-at-eol <RESET>
+ <RED>-1. blank-at-eol <RESET>
+ <GREEN>+1. still-blank-at-eol <RESET>
+ <GREEN>+2. and a new line <RESET>
+ EOF
+
+'
+
+test_expect_success 'test --ws-error-highlight option' '
+
+ git diff --color --ws-error-highlight=default,old >current.raw &&
+ test_decode_color <current.raw >current &&
+ test_cmp expect.default-old current &&
+
+ git diff --color --ws-error-highlight=all >current.raw &&
+ test_decode_color <current.raw >current &&
+ test_cmp expect.all current &&
+
+ git diff --color --ws-error-highlight=none >current.raw &&
+ test_decode_color <current.raw >current &&
+ test_cmp expect.none current
+
+'
+
+test_expect_success 'test diff.wsErrorHighlight config' '
+
+ git -c diff.wsErrorHighlight=default,old diff --color >current.raw &&
+ test_decode_color <current.raw >current &&
+ test_cmp expect.default-old current &&
+
+ git -c diff.wsErrorHighlight=all diff --color >current.raw &&
+ test_decode_color <current.raw >current &&
+ test_cmp expect.all current &&
+
+ git -c diff.wsErrorHighlight=none diff --color >current.raw &&
+ test_decode_color <current.raw >current &&
+ test_cmp expect.none current
+
+'
+
+test_expect_success 'option overrides diff.wsErrorHighlight' '
+
+ git -c diff.wsErrorHighlight=none \
+ diff --color --ws-error-highlight=default,old >current.raw &&
+ test_decode_color <current.raw >current &&
+ test_cmp expect.default-old current &&
+
+ git -c diff.wsErrorHighlight=default \
+ diff --color --ws-error-highlight=all >current.raw &&
+ test_decode_color <current.raw >current &&
+ test_cmp expect.all current &&
+
+ git -c diff.wsErrorHighlight=all \
+ diff --color --ws-error-highlight=none >current.raw &&
+ test_decode_color <current.raw >current &&
+ test_cmp expect.none current
+
+'
+
+test_expect_success 'detect moved code, complete file' '
+ git reset --hard &&
+ cat <<-\EOF >test.c &&
+ #include<stdio.h>
+ main()
+ {
+ printf("Hello World");
+ }
+ EOF
+ git add test.c &&
+ git commit -m "add main function" &&
+ file=$(git rev-parse --short HEAD:test.c) &&
+ git mv test.c main.c &&
+ test_config color.diff.oldMoved "normal red" &&
+ test_config color.diff.newMoved "normal green" &&
+ git diff HEAD --color-moved=zebra --color --no-renames >actual.raw &&
+ test_decode_color <actual.raw >actual &&
+ cat >expected <<-EOF &&
+ <BOLD>diff --git a/main.c b/main.c<RESET>
+ <BOLD>new file mode 100644<RESET>
+ <BOLD>index 0000000..$file<RESET>
+ <BOLD>--- /dev/null<RESET>
+ <BOLD>+++ b/main.c<RESET>
+ <CYAN>@@ -0,0 +1,5 @@<RESET>
+ <BGREEN>+<RESET><BGREEN>#include<stdio.h><RESET>
+ <BGREEN>+<RESET><BGREEN>main()<RESET>
+ <BGREEN>+<RESET><BGREEN>{<RESET>
+ <BGREEN>+<RESET><BGREEN>printf("Hello World");<RESET>
+ <BGREEN>+<RESET><BGREEN>}<RESET>
+ <BOLD>diff --git a/test.c b/test.c<RESET>
+ <BOLD>deleted file mode 100644<RESET>
+ <BOLD>index $file..0000000<RESET>
+ <BOLD>--- a/test.c<RESET>
+ <BOLD>+++ /dev/null<RESET>
+ <CYAN>@@ -1,5 +0,0 @@<RESET>
+ <BRED>-#include<stdio.h><RESET>
+ <BRED>-main()<RESET>
+ <BRED>-{<RESET>
+ <BRED>-printf("Hello World");<RESET>
+ <BRED>-}<RESET>
+ EOF
+
+ test_cmp expected actual
+'
+
+test_expect_success 'detect malicious moved code, inside file' '
+ test_config color.diff.oldMoved "normal red" &&
+ test_config color.diff.newMoved "normal green" &&
+ test_config color.diff.oldMovedAlternative "blue" &&
+ test_config color.diff.newMovedAlternative "yellow" &&
+ git reset --hard &&
+ cat <<-\EOF >main.c &&
+ #include<stdio.h>
+ int stuff()
+ {
+ printf("Hello ");
+ printf("World\n");
+ }
+
+ int secure_foo(struct user *u)
+ {
+ if (!u->is_allowed_foo)
+ return;
+ foo(u);
+ }
+
+ int main()
+ {
+ foo();
+ }
+ EOF
+ cat <<-\EOF >test.c &&
+ #include<stdio.h>
+ int bar()
+ {
+ printf("Hello World, but different\n");
+ }
+
+ int another_function()
+ {
+ bar();
+ }
+ EOF
+ git add main.c test.c &&
+ git commit -m "add main and test file" &&
+ before_main=$(git rev-parse --short HEAD:main.c) &&
+ before_test=$(git rev-parse --short HEAD:test.c) &&
+ cat <<-\EOF >main.c &&
+ #include<stdio.h>
+ int stuff()
+ {
+ printf("Hello ");
+ printf("World\n");
+ }
+
+ int main()
+ {
+ foo();
+ }
+ EOF
+ cat <<-\EOF >test.c &&
+ #include<stdio.h>
+ int bar()
+ {
+ printf("Hello World, but different\n");
+ }
+
+ int secure_foo(struct user *u)
+ {
+ foo(u);
+ if (!u->is_allowed_foo)
+ return;
+ }
+
+ int another_function()
+ {
+ bar();
+ }
+ EOF
+ hash_main=$(git hash-object main.c) &&
+ after_main=$(git rev-parse --short "$hash_main") &&
+ hash_test=$(git hash-object test.c) &&
+ after_test=$(git rev-parse --short "$hash_test") &&
+ git diff HEAD --no-renames --color-moved=zebra --color >actual.raw &&
+ test_decode_color <actual.raw >actual &&
+ cat <<-EOF >expected &&
+ <BOLD>diff --git a/main.c b/main.c<RESET>
+ <BOLD>index $before_main..$after_main 100644<RESET>
+ <BOLD>--- a/main.c<RESET>
+ <BOLD>+++ b/main.c<RESET>
+ <CYAN>@@ -5,13 +5,6 @@<RESET> <RESET>printf("Hello ");<RESET>
+ printf("World\n");<RESET>
+ }<RESET>
+ <RESET>
+ <BRED>-int secure_foo(struct user *u)<RESET>
+ <BRED>-{<RESET>
+ <BLUE>-if (!u->is_allowed_foo)<RESET>
+ <BLUE>-return;<RESET>
+ <RED>-foo(u);<RESET>
+ <RED>-}<RESET>
+ <RED>-<RESET>
+ int main()<RESET>
+ {<RESET>
+ foo();<RESET>
+ <BOLD>diff --git a/test.c b/test.c<RESET>
+ <BOLD>index $before_test..$after_test 100644<RESET>
+ <BOLD>--- a/test.c<RESET>
+ <BOLD>+++ b/test.c<RESET>
+ <CYAN>@@ -4,6 +4,13 @@<RESET> <RESET>int bar()<RESET>
+ printf("Hello World, but different\n");<RESET>
+ }<RESET>
+ <RESET>
+ <BGREEN>+<RESET><BGREEN>int secure_foo(struct user *u)<RESET>
+ <BGREEN>+<RESET><BGREEN>{<RESET>
+ <GREEN>+<RESET><GREEN>foo(u);<RESET>
+ <BGREEN>+<RESET><BGREEN>if (!u->is_allowed_foo)<RESET>
+ <BGREEN>+<RESET><BGREEN>return;<RESET>
+ <GREEN>+<RESET><GREEN>}<RESET>
+ <GREEN>+<RESET>
+ int another_function()<RESET>
+ {<RESET>
+ bar();<RESET>
+ EOF
+
+ test_cmp expected actual
+'
+
+test_expect_success 'plain moved code, inside file' '
+ test_config color.diff.oldMoved "normal red" &&
+ test_config color.diff.newMoved "normal green" &&
+ test_config color.diff.oldMovedAlternative "blue" &&
+ test_config color.diff.newMovedAlternative "yellow" &&
+ # needs previous test as setup
+ git diff HEAD --no-renames --color-moved=plain --color >actual.raw &&
+ test_decode_color <actual.raw >actual &&
+ cat <<-EOF >expected &&
+ <BOLD>diff --git a/main.c b/main.c<RESET>
+ <BOLD>index $before_main..$after_main 100644<RESET>
+ <BOLD>--- a/main.c<RESET>
+ <BOLD>+++ b/main.c<RESET>
+ <CYAN>@@ -5,13 +5,6 @@<RESET> <RESET>printf("Hello ");<RESET>
+ printf("World\n");<RESET>
+ }<RESET>
+ <RESET>
+ <BRED>-int secure_foo(struct user *u)<RESET>
+ <BRED>-{<RESET>
+ <BRED>-if (!u->is_allowed_foo)<RESET>
+ <BRED>-return;<RESET>
+ <BRED>-foo(u);<RESET>
+ <BRED>-}<RESET>
+ <BRED>-<RESET>
+ int main()<RESET>
+ {<RESET>
+ foo();<RESET>
+ <BOLD>diff --git a/test.c b/test.c<RESET>
+ <BOLD>index $before_test..$after_test 100644<RESET>
+ <BOLD>--- a/test.c<RESET>
+ <BOLD>+++ b/test.c<RESET>
+ <CYAN>@@ -4,6 +4,13 @@<RESET> <RESET>int bar()<RESET>
+ printf("Hello World, but different\n");<RESET>
+ }<RESET>
+ <RESET>
+ <BGREEN>+<RESET><BGREEN>int secure_foo(struct user *u)<RESET>
+ <BGREEN>+<RESET><BGREEN>{<RESET>
+ <BGREEN>+<RESET><BGREEN>foo(u);<RESET>
+ <BGREEN>+<RESET><BGREEN>if (!u->is_allowed_foo)<RESET>
+ <BGREEN>+<RESET><BGREEN>return;<RESET>
+ <BGREEN>+<RESET><BGREEN>}<RESET>
+ <BGREEN>+<RESET>
+ int another_function()<RESET>
+ {<RESET>
+ bar();<RESET>
+ EOF
+
+ test_cmp expected actual
+'
+
+test_expect_success 'detect blocks of moved code' '
+ git reset --hard &&
+ cat <<-\EOF >lines.txt &&
+ long line 1
+ long line 2
+ long line 3
+ line 4
+ line 5
+ line 6
+ line 7
+ line 8
+ line 9
+ line 10
+ line 11
+ line 12
+ line 13
+ long line 14
+ long line 15
+ long line 16
+ EOF
+ git add lines.txt &&
+ git commit -m "add poetry" &&
+ cat <<-\EOF >lines.txt &&
+ line 4
+ line 5
+ line 6
+ line 7
+ line 8
+ line 9
+ long line 1
+ long line 2
+ long line 3
+ long line 14
+ long line 15
+ long line 16
+ line 10
+ line 11
+ line 12
+ line 13
+ EOF
+ test_config color.diff.oldMoved "magenta" &&
+ test_config color.diff.newMoved "cyan" &&
+ test_config color.diff.oldMovedAlternative "blue" &&
+ test_config color.diff.newMovedAlternative "yellow" &&
+ test_config color.diff.oldMovedDimmed "normal magenta" &&
+ test_config color.diff.newMovedDimmed "normal cyan" &&
+ test_config color.diff.oldMovedAlternativeDimmed "normal blue" &&
+ test_config color.diff.newMovedAlternativeDimmed "normal yellow" &&
+ git diff HEAD --no-renames --color-moved=blocks --color >actual.raw &&
+ grep -v "index" actual.raw | test_decode_color >actual &&
+ cat <<-\EOF >expected &&
+ <BOLD>diff --git a/lines.txt b/lines.txt<RESET>
+ <BOLD>--- a/lines.txt<RESET>
+ <BOLD>+++ b/lines.txt<RESET>
+ <CYAN>@@ -1,16 +1,16 @@<RESET>
+ <MAGENTA>-long line 1<RESET>
+ <MAGENTA>-long line 2<RESET>
+ <MAGENTA>-long line 3<RESET>
+ line 4<RESET>
+ line 5<RESET>
+ line 6<RESET>
+ line 7<RESET>
+ line 8<RESET>
+ line 9<RESET>
+ <CYAN>+<RESET><CYAN>long line 1<RESET>
+ <CYAN>+<RESET><CYAN>long line 2<RESET>
+ <CYAN>+<RESET><CYAN>long line 3<RESET>
+ <CYAN>+<RESET><CYAN>long line 14<RESET>
+ <CYAN>+<RESET><CYAN>long line 15<RESET>
+ <CYAN>+<RESET><CYAN>long line 16<RESET>
+ line 10<RESET>
+ line 11<RESET>
+ line 12<RESET>
+ line 13<RESET>
+ <MAGENTA>-long line 14<RESET>
+ <MAGENTA>-long line 15<RESET>
+ <MAGENTA>-long line 16<RESET>
+ EOF
+ test_cmp expected actual
+
+'
+
+test_expect_success 'detect permutations inside moved code -- dimmed-zebra' '
+ # reuse setup from test before!
+ test_config color.diff.oldMoved "magenta" &&
+ test_config color.diff.newMoved "cyan" &&
+ test_config color.diff.oldMovedAlternative "blue" &&
+ test_config color.diff.newMovedAlternative "yellow" &&
+ test_config color.diff.oldMovedDimmed "normal magenta" &&
+ test_config color.diff.newMovedDimmed "normal cyan" &&
+ test_config color.diff.oldMovedAlternativeDimmed "normal blue" &&
+ test_config color.diff.newMovedAlternativeDimmed "normal yellow" &&
+ git diff HEAD --no-renames --color-moved=dimmed-zebra --color >actual.raw &&
+ grep -v "index" actual.raw | test_decode_color >actual &&
+ cat <<-\EOF >expected &&
+ <BOLD>diff --git a/lines.txt b/lines.txt<RESET>
+ <BOLD>--- a/lines.txt<RESET>
+ <BOLD>+++ b/lines.txt<RESET>
+ <CYAN>@@ -1,16 +1,16 @@<RESET>
+ <BMAGENTA>-long line 1<RESET>
+ <BMAGENTA>-long line 2<RESET>
+ <BMAGENTA>-long line 3<RESET>
+ line 4<RESET>
+ line 5<RESET>
+ line 6<RESET>
+ line 7<RESET>
+ line 8<RESET>
+ line 9<RESET>
+ <BCYAN>+<RESET><BCYAN>long line 1<RESET>
+ <BCYAN>+<RESET><BCYAN>long line 2<RESET>
+ <CYAN>+<RESET><CYAN>long line 3<RESET>
+ <YELLOW>+<RESET><YELLOW>long line 14<RESET>
+ <BYELLOW>+<RESET><BYELLOW>long line 15<RESET>
+ <BYELLOW>+<RESET><BYELLOW>long line 16<RESET>
+ line 10<RESET>
+ line 11<RESET>
+ line 12<RESET>
+ line 13<RESET>
+ <BMAGENTA>-long line 14<RESET>
+ <BMAGENTA>-long line 15<RESET>
+ <BMAGENTA>-long line 16<RESET>
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'zebra alternate color is only used when necessary' '
+ cat >old.txt <<-\EOF &&
+ line 1A should be marked as oldMoved newMovedAlternate
+ line 1B should be marked as oldMoved newMovedAlternate
+ unchanged
+ line 2A should be marked as oldMoved newMovedAlternate
+ line 2B should be marked as oldMoved newMovedAlternate
+ line 3A should be marked as oldMovedAlternate newMoved
+ line 3B should be marked as oldMovedAlternate newMoved
+ unchanged
+ line 4A should be marked as oldMoved newMovedAlternate
+ line 4B should be marked as oldMoved newMovedAlternate
+ line 5A should be marked as oldMovedAlternate newMoved
+ line 5B should be marked as oldMovedAlternate newMoved
+ line 6A should be marked as oldMoved newMoved
+ line 6B should be marked as oldMoved newMoved
+ EOF
+ cat >new.txt <<-\EOF &&
+ line 1A should be marked as oldMoved newMovedAlternate
+ line 1B should be marked as oldMoved newMovedAlternate
+ unchanged
+ line 3A should be marked as oldMovedAlternate newMoved
+ line 3B should be marked as oldMovedAlternate newMoved
+ line 2A should be marked as oldMoved newMovedAlternate
+ line 2B should be marked as oldMoved newMovedAlternate
+ unchanged
+ line 6A should be marked as oldMoved newMoved
+ line 6B should be marked as oldMoved newMoved
+ line 4A should be marked as oldMoved newMovedAlternate
+ line 4B should be marked as oldMoved newMovedAlternate
+ line 5A should be marked as oldMovedAlternate newMoved
+ line 5B should be marked as oldMovedAlternate newMoved
+ EOF
+ test_expect_code 1 git diff --no-index --color --color-moved=zebra \
+ --color-moved-ws=allow-indentation-change \
+ old.txt new.txt >output &&
+ grep -v index output | test_decode_color >actual &&
+ cat >expected <<-\EOF &&
+ <BOLD>diff --git a/old.txt b/new.txt<RESET>
+ <BOLD>--- a/old.txt<RESET>
+ <BOLD>+++ b/new.txt<RESET>
+ <CYAN>@@ -1,14 +1,14 @@<RESET>
+ <BOLD;MAGENTA>-line 1A should be marked as oldMoved newMovedAlternate<RESET>
+ <BOLD;MAGENTA>-line 1B should be marked as oldMoved newMovedAlternate<RESET>
+ <BOLD;CYAN>+<RESET><BOLD;CYAN> line 1A should be marked as oldMoved newMovedAlternate<RESET>
+ <BOLD;CYAN>+<RESET><BOLD;CYAN> line 1B should be marked as oldMoved newMovedAlternate<RESET>
+ unchanged<RESET>
+ <BOLD;MAGENTA>-line 2A should be marked as oldMoved newMovedAlternate<RESET>
+ <BOLD;MAGENTA>-line 2B should be marked as oldMoved newMovedAlternate<RESET>
+ <BOLD;BLUE>-line 3A should be marked as oldMovedAlternate newMoved<RESET>
+ <BOLD;BLUE>-line 3B should be marked as oldMovedAlternate newMoved<RESET>
+ <BOLD;CYAN>+<RESET><BOLD;CYAN> line 3A should be marked as oldMovedAlternate newMoved<RESET>
+ <BOLD;CYAN>+<RESET><BOLD;CYAN> line 3B should be marked as oldMovedAlternate newMoved<RESET>
+ <BOLD;YELLOW>+<RESET><BOLD;YELLOW> line 2A should be marked as oldMoved newMovedAlternate<RESET>
+ <BOLD;YELLOW>+<RESET><BOLD;YELLOW> line 2B should be marked as oldMoved newMovedAlternate<RESET>
+ unchanged<RESET>
+ <BOLD;MAGENTA>-line 4A should be marked as oldMoved newMovedAlternate<RESET>
+ <BOLD;MAGENTA>-line 4B should be marked as oldMoved newMovedAlternate<RESET>
+ <BOLD;BLUE>-line 5A should be marked as oldMovedAlternate newMoved<RESET>
+ <BOLD;BLUE>-line 5B should be marked as oldMovedAlternate newMoved<RESET>
+ <BOLD;MAGENTA>-line 6A should be marked as oldMoved newMoved<RESET>
+ <BOLD;MAGENTA>-line 6B should be marked as oldMoved newMoved<RESET>
+ <BOLD;CYAN>+<RESET><BOLD;CYAN> line 6A should be marked as oldMoved newMoved<RESET>
+ <BOLD;CYAN>+<RESET><BOLD;CYAN> line 6B should be marked as oldMoved newMoved<RESET>
+ <BOLD;YELLOW>+<RESET><BOLD;YELLOW> line 4A should be marked as oldMoved newMovedAlternate<RESET>
+ <BOLD;YELLOW>+<RESET><BOLD;YELLOW> line 4B should be marked as oldMoved newMovedAlternate<RESET>
+ <BOLD;CYAN>+<RESET><BOLD;CYAN> line 5A should be marked as oldMovedAlternate newMoved<RESET>
+ <BOLD;CYAN>+<RESET><BOLD;CYAN> line 5B should be marked as oldMovedAlternate newMoved<RESET>
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'short lines of opposite sign do not get marked as moved' '
+ cat >old.txt <<-\EOF &&
+ this line should be marked as moved
+ unchanged
+ unchanged
+ unchanged
+ unchanged
+ too short
+ this line should be marked as oldMoved newMoved
+ this line should be marked as oldMovedAlternate newMoved
+ unchanged 1
+ unchanged 2
+ unchanged 3
+ unchanged 4
+ this line should be marked as oldMoved newMoved/newMovedAlternate
+ EOF
+ cat >new.txt <<-\EOF &&
+ too short
+ unchanged
+ unchanged
+ this line should be marked as moved
+ too short
+ unchanged
+ unchanged
+ this line should be marked as oldMoved newMoved/newMovedAlternate
+ unchanged 1
+ unchanged 2
+ this line should be marked as oldMovedAlternate newMoved
+ this line should be marked as oldMoved newMoved/newMovedAlternate
+ unchanged 3
+ this line should be marked as oldMoved newMoved
+ unchanged 4
+ EOF
+ test_expect_code 1 git diff --no-index --color --color-moved=zebra \
+ old.txt new.txt >output && cat output &&
+ grep -v index output | test_decode_color >actual &&
+ cat >expect <<-\EOF &&
+ <BOLD>diff --git a/old.txt b/new.txt<RESET>
+ <BOLD>--- a/old.txt<RESET>
+ <BOLD>+++ b/new.txt<RESET>
+ <CYAN>@@ -1,13 +1,15 @@<RESET>
+ <BOLD;MAGENTA>-this line should be marked as moved<RESET>
+ <GREEN>+<RESET><GREEN>too short<RESET>
+ unchanged<RESET>
+ unchanged<RESET>
+ <BOLD;CYAN>+<RESET><BOLD;CYAN>this line should be marked as moved<RESET>
+ <GREEN>+<RESET><GREEN>too short<RESET>
+ unchanged<RESET>
+ unchanged<RESET>
+ <RED>-too short<RESET>
+ <BOLD;MAGENTA>-this line should be marked as oldMoved newMoved<RESET>
+ <BOLD;BLUE>-this line should be marked as oldMovedAlternate newMoved<RESET>
+ <BOLD;CYAN>+<RESET><BOLD;CYAN>this line should be marked as oldMoved newMoved/newMovedAlternate<RESET>
+ unchanged 1<RESET>
+ unchanged 2<RESET>
+ <BOLD;CYAN>+<RESET><BOLD;CYAN>this line should be marked as oldMovedAlternate newMoved<RESET>
+ <BOLD;YELLOW>+<RESET><BOLD;YELLOW>this line should be marked as oldMoved newMoved/newMovedAlternate<RESET>
+ unchanged 3<RESET>
+ <BOLD;CYAN>+<RESET><BOLD;CYAN>this line should be marked as oldMoved newMoved<RESET>
+ unchanged 4<RESET>
+ <BOLD;MAGENTA>-this line should be marked as oldMoved newMoved/newMovedAlternate<RESET>
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'cmd option assumes configured colored-moved' '
+ test_config color.diff.oldMoved "magenta" &&
+ test_config color.diff.newMoved "cyan" &&
+ test_config color.diff.oldMovedAlternative "blue" &&
+ test_config color.diff.newMovedAlternative "yellow" &&
+ test_config color.diff.oldMovedDimmed "normal magenta" &&
+ test_config color.diff.newMovedDimmed "normal cyan" &&
+ test_config color.diff.oldMovedAlternativeDimmed "normal blue" &&
+ test_config color.diff.newMovedAlternativeDimmed "normal yellow" &&
+ test_config diff.colorMoved zebra &&
+ git diff HEAD --no-renames --color-moved --color >actual.raw &&
+ grep -v "index" actual.raw | test_decode_color >actual &&
+ cat <<-\EOF >expected &&
+ <BOLD>diff --git a/lines.txt b/lines.txt<RESET>
+ <BOLD>--- a/lines.txt<RESET>
+ <BOLD>+++ b/lines.txt<RESET>
+ <CYAN>@@ -1,16 +1,16 @@<RESET>
+ <MAGENTA>-long line 1<RESET>
+ <MAGENTA>-long line 2<RESET>
+ <MAGENTA>-long line 3<RESET>
+ line 4<RESET>
+ line 5<RESET>
+ line 6<RESET>
+ line 7<RESET>
+ line 8<RESET>
+ line 9<RESET>
+ <CYAN>+<RESET><CYAN>long line 1<RESET>
+ <CYAN>+<RESET><CYAN>long line 2<RESET>
+ <CYAN>+<RESET><CYAN>long line 3<RESET>
+ <YELLOW>+<RESET><YELLOW>long line 14<RESET>
+ <YELLOW>+<RESET><YELLOW>long line 15<RESET>
+ <YELLOW>+<RESET><YELLOW>long line 16<RESET>
+ line 10<RESET>
+ line 11<RESET>
+ line 12<RESET>
+ line 13<RESET>
+ <MAGENTA>-long line 14<RESET>
+ <MAGENTA>-long line 15<RESET>
+ <MAGENTA>-long line 16<RESET>
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'no effect on diff from --color-moved with --word-diff' '
+ cat <<-\EOF >text.txt &&
+ Lorem Ipsum is simply dummy text of the printing and typesetting industry.
+ EOF
+ git add text.txt &&
+ git commit -a -m "clean state" &&
+ cat <<-\EOF >text.txt &&
+ simply Lorem Ipsum dummy is text of the typesetting and printing industry.
+ EOF
+ git diff --color-moved --word-diff >actual &&
+ git diff --word-diff >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'no effect on show from --color-moved with --word-diff' '
+ git show --color-moved --word-diff >actual &&
+ git show --word-diff >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'set up whitespace tests' '
+ git reset --hard &&
+ # Note that these lines have no leading or trailing whitespace.
+ cat <<-\EOF >lines.txt &&
+ line 1
+ line 2
+ line 3
+ line 4
+ line 5
+ long line 6
+ long line 7
+ long line 8
+ long line 9
+ EOF
+ git add lines.txt &&
+ git commit -m "add poetry" &&
+ git config color.diff.oldMoved "magenta" &&
+ git config color.diff.newMoved "cyan"
+'
+
+test_expect_success 'move detection ignoring whitespace ' '
+ q_to_tab <<-\EOF >lines.txt &&
+ Qlong line 6
+ Qlong line 7
+ Qlong line 8
+ Qchanged long line 9
+ line 1
+ line 2
+ line 3
+ line 4
+ line 5
+ EOF
+ git diff HEAD --no-renames --color-moved --color >actual.raw &&
+ grep -v "index" actual.raw | test_decode_color >actual &&
+ cat <<-\EOF >expected &&
+ <BOLD>diff --git a/lines.txt b/lines.txt<RESET>
+ <BOLD>--- a/lines.txt<RESET>
+ <BOLD>+++ b/lines.txt<RESET>
+ <CYAN>@@ -1,9 +1,9 @@<RESET>
+ <GREEN>+<RESET> <GREEN>long line 6<RESET>
+ <GREEN>+<RESET> <GREEN>long line 7<RESET>
+ <GREEN>+<RESET> <GREEN>long line 8<RESET>
+ <GREEN>+<RESET> <GREEN>changed long line 9<RESET>
+ line 1<RESET>
+ line 2<RESET>
+ line 3<RESET>
+ line 4<RESET>
+ line 5<RESET>
+ <RED>-long line 6<RESET>
+ <RED>-long line 7<RESET>
+ <RED>-long line 8<RESET>
+ <RED>-long line 9<RESET>
+ EOF
+ test_cmp expected actual &&
+
+ git diff HEAD --no-renames --color-moved --color \
+ --color-moved-ws=ignore-all-space >actual.raw &&
+ grep -v "index" actual.raw | test_decode_color >actual &&
+ cat <<-\EOF >expected &&
+ <BOLD>diff --git a/lines.txt b/lines.txt<RESET>
+ <BOLD>--- a/lines.txt<RESET>
+ <BOLD>+++ b/lines.txt<RESET>
+ <CYAN>@@ -1,9 +1,9 @@<RESET>
+ <CYAN>+<RESET> <CYAN>long line 6<RESET>
+ <CYAN>+<RESET> <CYAN>long line 7<RESET>
+ <CYAN>+<RESET> <CYAN>long line 8<RESET>
+ <GREEN>+<RESET> <GREEN>changed long line 9<RESET>
+ line 1<RESET>
+ line 2<RESET>
+ line 3<RESET>
+ line 4<RESET>
+ line 5<RESET>
+ <MAGENTA>-long line 6<RESET>
+ <MAGENTA>-long line 7<RESET>
+ <MAGENTA>-long line 8<RESET>
+ <RED>-long line 9<RESET>
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'move detection ignoring whitespace changes' '
+ git reset --hard &&
+ # Lines 6-8 have a space change, but 9 is new whitespace
+ q_to_tab <<-\EOF >lines.txt &&
+ longQline 6
+ longQline 7
+ longQline 8
+ long liQne 9
+ line 1
+ line 2
+ line 3
+ line 4
+ line 5
+ EOF
+
+ git diff HEAD --no-renames --color-moved --color >actual.raw &&
+ grep -v "index" actual.raw | test_decode_color >actual &&
+ cat <<-\EOF >expected &&
+ <BOLD>diff --git a/lines.txt b/lines.txt<RESET>
+ <BOLD>--- a/lines.txt<RESET>
+ <BOLD>+++ b/lines.txt<RESET>
+ <CYAN>@@ -1,9 +1,9 @@<RESET>
+ <GREEN>+<RESET><GREEN>long line 6<RESET>
+ <GREEN>+<RESET><GREEN>long line 7<RESET>
+ <GREEN>+<RESET><GREEN>long line 8<RESET>
+ <GREEN>+<RESET><GREEN>long li ne 9<RESET>
+ line 1<RESET>
+ line 2<RESET>
+ line 3<RESET>
+ line 4<RESET>
+ line 5<RESET>
+ <RED>-long line 6<RESET>
+ <RED>-long line 7<RESET>
+ <RED>-long line 8<RESET>
+ <RED>-long line 9<RESET>
+ EOF
+ test_cmp expected actual &&
+
+ git diff HEAD --no-renames --color-moved --color \
+ --color-moved-ws=ignore-space-change >actual.raw &&
+ grep -v "index" actual.raw | test_decode_color >actual &&
+ cat <<-\EOF >expected &&
+ <BOLD>diff --git a/lines.txt b/lines.txt<RESET>
+ <BOLD>--- a/lines.txt<RESET>
+ <BOLD>+++ b/lines.txt<RESET>
+ <CYAN>@@ -1,9 +1,9 @@<RESET>
+ <CYAN>+<RESET><CYAN>long line 6<RESET>
+ <CYAN>+<RESET><CYAN>long line 7<RESET>
+ <CYAN>+<RESET><CYAN>long line 8<RESET>
+ <GREEN>+<RESET><GREEN>long li ne 9<RESET>
+ line 1<RESET>
+ line 2<RESET>
+ line 3<RESET>
+ line 4<RESET>
+ line 5<RESET>
+ <MAGENTA>-long line 6<RESET>
+ <MAGENTA>-long line 7<RESET>
+ <MAGENTA>-long line 8<RESET>
+ <RED>-long line 9<RESET>
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'move detection ignoring whitespace at eol' '
+ git reset --hard &&
+ # Lines 6-9 have new eol whitespace, but 9 also has it in the middle
+ q_to_tab <<-\EOF >lines.txt &&
+ long line 6Q
+ long line 7Q
+ long line 8Q
+ longQline 9Q
+ line 1
+ line 2
+ line 3
+ line 4
+ line 5
+ EOF
+
+ # avoid cluttering the output with complaints about our eol whitespace
+ test_config core.whitespace -blank-at-eol &&
+
+ git diff HEAD --no-renames --color-moved --color >actual.raw &&
+ grep -v "index" actual.raw | test_decode_color >actual &&
+ cat <<-\EOF >expected &&
+ <BOLD>diff --git a/lines.txt b/lines.txt<RESET>
+ <BOLD>--- a/lines.txt<RESET>
+ <BOLD>+++ b/lines.txt<RESET>
+ <CYAN>@@ -1,9 +1,9 @@<RESET>
+ <GREEN>+<RESET><GREEN>long line 6 <RESET>
+ <GREEN>+<RESET><GREEN>long line 7 <RESET>
+ <GREEN>+<RESET><GREEN>long line 8 <RESET>
+ <GREEN>+<RESET><GREEN>long line 9 <RESET>
+ line 1<RESET>
+ line 2<RESET>
+ line 3<RESET>
+ line 4<RESET>
+ line 5<RESET>
+ <RED>-long line 6<RESET>
+ <RED>-long line 7<RESET>
+ <RED>-long line 8<RESET>
+ <RED>-long line 9<RESET>
+ EOF
+ test_cmp expected actual &&
+
+ git diff HEAD --no-renames --color-moved --color \
+ --color-moved-ws=ignore-space-at-eol >actual.raw &&
+ grep -v "index" actual.raw | test_decode_color >actual &&
+ cat <<-\EOF >expected &&
+ <BOLD>diff --git a/lines.txt b/lines.txt<RESET>
+ <BOLD>--- a/lines.txt<RESET>
+ <BOLD>+++ b/lines.txt<RESET>
+ <CYAN>@@ -1,9 +1,9 @@<RESET>
+ <CYAN>+<RESET><CYAN>long line 6 <RESET>
+ <CYAN>+<RESET><CYAN>long line 7 <RESET>
+ <CYAN>+<RESET><CYAN>long line 8 <RESET>
+ <GREEN>+<RESET><GREEN>long line 9 <RESET>
+ line 1<RESET>
+ line 2<RESET>
+ line 3<RESET>
+ line 4<RESET>
+ line 5<RESET>
+ <MAGENTA>-long line 6<RESET>
+ <MAGENTA>-long line 7<RESET>
+ <MAGENTA>-long line 8<RESET>
+ <RED>-long line 9<RESET>
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'clean up whitespace-test colors' '
+ git config --unset color.diff.oldMoved &&
+ git config --unset color.diff.newMoved
+'
+
+test_expect_success '--color-moved block at end of diff output respects MIN_ALNUM_COUNT' '
+ git reset --hard &&
+ >bar &&
+ cat <<-\EOF >foo &&
+ irrelevant_line
+ line1
+ EOF
+ git add foo bar &&
+ git commit -m x &&
+
+ cat <<-\EOF >bar &&
+ line1
+ EOF
+ cat <<-\EOF >foo &&
+ irrelevant_line
+ EOF
+
+ git diff HEAD --color-moved=zebra --color --no-renames >actual.raw &&
+ grep -v "index" actual.raw | test_decode_color >actual &&
+ cat >expected <<-\EOF &&
+ <BOLD>diff --git a/bar b/bar<RESET>
+ <BOLD>--- a/bar<RESET>
+ <BOLD>+++ b/bar<RESET>
+ <CYAN>@@ -0,0 +1 @@<RESET>
+ <GREEN>+<RESET><GREEN>line1<RESET>
+ <BOLD>diff --git a/foo b/foo<RESET>
+ <BOLD>--- a/foo<RESET>
+ <BOLD>+++ b/foo<RESET>
+ <CYAN>@@ -1,2 +1 @@<RESET>
+ irrelevant_line<RESET>
+ <RED>-line1<RESET>
+ EOF
+
+ test_cmp expected actual
+'
+
+test_expect_success '--color-moved respects MIN_ALNUM_COUNT' '
+ git reset --hard &&
+ cat <<-\EOF >foo &&
+ nineteen chars 456789
+ irrelevant_line
+ twenty chars 234567890
+ EOF
+ >bar &&
+ git add foo bar &&
+ git commit -m x &&
+
+ cat <<-\EOF >foo &&
+ irrelevant_line
+ EOF
+ cat <<-\EOF >bar &&
+ twenty chars 234567890
+ nineteen chars 456789
+ EOF
+
+ git diff HEAD --color-moved=zebra --color --no-renames >actual.raw &&
+ grep -v "index" actual.raw | test_decode_color >actual &&
+ cat >expected <<-\EOF &&
+ <BOLD>diff --git a/bar b/bar<RESET>
+ <BOLD>--- a/bar<RESET>
+ <BOLD>+++ b/bar<RESET>
+ <CYAN>@@ -0,0 +1,2 @@<RESET>
+ <BOLD;CYAN>+<RESET><BOLD;CYAN>twenty chars 234567890<RESET>
+ <GREEN>+<RESET><GREEN>nineteen chars 456789<RESET>
+ <BOLD>diff --git a/foo b/foo<RESET>
+ <BOLD>--- a/foo<RESET>
+ <BOLD>+++ b/foo<RESET>
+ <CYAN>@@ -1,3 +1 @@<RESET>
+ <RED>-nineteen chars 456789<RESET>
+ irrelevant_line<RESET>
+ <BOLD;MAGENTA>-twenty chars 234567890<RESET>
+ EOF
+
+ test_cmp expected actual
+'
+
+test_expect_success '--color-moved treats adjacent blocks as separate for MIN_ALNUM_COUNT' '
+ git reset --hard &&
+ cat <<-\EOF >foo &&
+ 7charsA
+ irrelevant_line
+ 7charsB
+ 7charsC
+ EOF
+ >bar &&
+ git add foo bar &&
+ git commit -m x &&
+
+ cat <<-\EOF >foo &&
+ irrelevant_line
+ EOF
+ cat <<-\EOF >bar &&
+ 7charsB
+ 7charsC
+ 7charsA
+ EOF
+
+ git diff HEAD --color-moved=zebra --color --no-renames >actual.raw &&
+ grep -v "index" actual.raw | test_decode_color >actual &&
+ cat >expected <<-\EOF &&
+ <BOLD>diff --git a/bar b/bar<RESET>
+ <BOLD>--- a/bar<RESET>
+ <BOLD>+++ b/bar<RESET>
+ <CYAN>@@ -0,0 +1,3 @@<RESET>
+ <GREEN>+<RESET><GREEN>7charsB<RESET>
+ <GREEN>+<RESET><GREEN>7charsC<RESET>
+ <GREEN>+<RESET><GREEN>7charsA<RESET>
+ <BOLD>diff --git a/foo b/foo<RESET>
+ <BOLD>--- a/foo<RESET>
+ <BOLD>+++ b/foo<RESET>
+ <CYAN>@@ -1,4 +1 @@<RESET>
+ <RED>-7charsA<RESET>
+ irrelevant_line<RESET>
+ <RED>-7charsB<RESET>
+ <RED>-7charsC<RESET>
+ EOF
+
+ test_cmp expected actual
+'
+
+test_expect_success '--color-moved rewinds for MIN_ALNUM_COUNT' '
+ git reset --hard &&
+ test_write_lines >file \
+ A B C one two three four five six seven D E F G H I J &&
+ git add file &&
+ test_write_lines >file \
+ one two A B C D E F G H I J two three four five six seven &&
+ git diff --color-moved=zebra -- file &&
+
+ git diff --color-moved=zebra --color -- file >actual.raw &&
+ grep -v "index" actual.raw | test_decode_color >actual &&
+ cat >expected <<-\EOF &&
+ <BOLD>diff --git a/file b/file<RESET>
+ <BOLD>--- a/file<RESET>
+ <BOLD>+++ b/file<RESET>
+ <CYAN>@@ -1,13 +1,8 @@<RESET>
+ <GREEN>+<RESET><GREEN>one<RESET>
+ <GREEN>+<RESET><GREEN>two<RESET>
+ A<RESET>
+ B<RESET>
+ C<RESET>
+ <RED>-one<RESET>
+ <BOLD;MAGENTA>-two<RESET>
+ <BOLD;MAGENTA>-three<RESET>
+ <BOLD;MAGENTA>-four<RESET>
+ <BOLD;MAGENTA>-five<RESET>
+ <BOLD;MAGENTA>-six<RESET>
+ <BOLD;MAGENTA>-seven<RESET>
+ D<RESET>
+ E<RESET>
+ F<RESET>
+ <CYAN>@@ -15,3 +10,9 @@<RESET> <RESET>G<RESET>
+ H<RESET>
+ I<RESET>
+ J<RESET>
+ <BOLD;CYAN>+<RESET><BOLD;CYAN>two<RESET>
+ <BOLD;CYAN>+<RESET><BOLD;CYAN>three<RESET>
+ <BOLD;CYAN>+<RESET><BOLD;CYAN>four<RESET>
+ <BOLD;CYAN>+<RESET><BOLD;CYAN>five<RESET>
+ <BOLD;CYAN>+<RESET><BOLD;CYAN>six<RESET>
+ <BOLD;CYAN>+<RESET><BOLD;CYAN>seven<RESET>
+ EOF
+
+ test_cmp expected actual
+'
+
+test_expect_success 'move detection with submodules' '
+ test_create_repo bananas &&
+ echo ripe >bananas/recipe &&
+ git -C bananas add recipe &&
+ test_commit fruit &&
+ test_commit -C bananas recipe &&
+ git submodule add ./bananas &&
+ git add bananas &&
+ git commit -a -m "bananas are like a heavy library?" &&
+ echo foul >bananas/recipe &&
+ echo ripe >fruit.t &&
+
+ git diff --submodule=diff --color-moved --color >actual &&
+
+ # no move detection as the moved line is across repository boundaries.
+ test_decode_color <actual >decoded_actual &&
+ ! grep BGREEN decoded_actual &&
+ ! grep BRED decoded_actual &&
+
+ # nor did we mess with it another way
+ git diff --submodule=diff --color >expect.raw &&
+ test_decode_color <expect.raw >expect &&
+ test_cmp expect decoded_actual &&
+ rm -rf bananas &&
+ git submodule deinit bananas
+'
+
+test_expect_success 'only move detection ignores white spaces' '
+ git reset --hard &&
+ q_to_tab <<-\EOF >text.txt &&
+ a long line to exceed per-line minimum
+ another long line to exceed per-line minimum
+ original file
+ EOF
+ git add text.txt &&
+ git commit -m "add text" &&
+ q_to_tab <<-\EOF >text.txt &&
+ Qa long line to exceed per-line minimum
+ Qanother long line to exceed per-line minimum
+ new file
+ EOF
+
+ # Make sure we get a different diff using -w
+ git diff --color --color-moved -w >actual.raw &&
+ grep -v "index" actual.raw | test_decode_color >actual &&
+ q_to_tab <<-\EOF >expected &&
+ <BOLD>diff --git a/text.txt b/text.txt<RESET>
+ <BOLD>--- a/text.txt<RESET>
+ <BOLD>+++ b/text.txt<RESET>
+ <CYAN>@@ -1,3 +1,3 @@<RESET>
+ Qa long line to exceed per-line minimum<RESET>
+ Qanother long line to exceed per-line minimum<RESET>
+ <RED>-original file<RESET>
+ <GREEN>+<RESET><GREEN>new file<RESET>
+ EOF
+ test_cmp expected actual &&
+
+ # And now ignoring white space only in the move detection
+ git diff --color --color-moved \
+ --color-moved-ws=ignore-all-space,ignore-space-change,ignore-space-at-eol >actual.raw &&
+ grep -v "index" actual.raw | test_decode_color >actual &&
+ q_to_tab <<-\EOF >expected &&
+ <BOLD>diff --git a/text.txt b/text.txt<RESET>
+ <BOLD>--- a/text.txt<RESET>
+ <BOLD>+++ b/text.txt<RESET>
+ <CYAN>@@ -1,3 +1,3 @@<RESET>
+ <BOLD;MAGENTA>-a long line to exceed per-line minimum<RESET>
+ <BOLD;MAGENTA>-another long line to exceed per-line minimum<RESET>
+ <RED>-original file<RESET>
+ <BOLD;CYAN>+<RESET>Q<BOLD;CYAN>a long line to exceed per-line minimum<RESET>
+ <BOLD;CYAN>+<RESET>Q<BOLD;CYAN>another long line to exceed per-line minimum<RESET>
+ <GREEN>+<RESET><GREEN>new file<RESET>
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'compare whitespace delta across moved blocks' '
+
+ git reset --hard &&
+ q_to_tab <<-\EOF >text.txt &&
+ QIndented
+ QText across
+ Qsome lines
+ QBut! <- this stands out
+ QAdjusting with
+ QQdifferent starting
+ Qwhite spaces
+ QAnother outlier
+ QQQIndented
+ QQQText across
+ QQQfive lines
+ QQQthat has similar lines
+ QQQto previous blocks, but with different indent
+ QQQYetQAnotherQoutlierQ
+ QLine with internal w h i t e s p a c e change
+ EOF
+
+ git add text.txt &&
+ git commit -m "add text.txt" &&
+
+ q_to_tab <<-\EOF >text.txt &&
+ QQIndented
+ QQText across
+ QQsome lines
+ QQQBut! <- this stands out
+ Adjusting with
+ Qdifferent starting
+ white spaces
+ AnotherQoutlier
+ QQIndented
+ QQText across
+ QQfive lines
+ QQthat has similar lines
+ QQto previous blocks, but with different indent
+ QQYetQAnotherQoutlier
+ QLine with internal whitespace change
+ EOF
+
+ git diff --color --color-moved --color-moved-ws=allow-indentation-change >actual.raw &&
+ grep -v "index" actual.raw | test_decode_color >actual &&
+
+ q_to_tab <<-\EOF >expected &&
+ <BOLD>diff --git a/text.txt b/text.txt<RESET>
+ <BOLD>--- a/text.txt<RESET>
+ <BOLD>+++ b/text.txt<RESET>
+ <CYAN>@@ -1,15 +1,15 @@<RESET>
+ <BOLD;MAGENTA>-QIndented<RESET>
+ <BOLD;MAGENTA>-QText across<RESET>
+ <BOLD;MAGENTA>-Qsome lines<RESET>
+ <RED>-QBut! <- this stands out<RESET>
+ <BOLD;MAGENTA>-QAdjusting with<RESET>
+ <BOLD;MAGENTA>-QQdifferent starting<RESET>
+ <BOLD;MAGENTA>-Qwhite spaces<RESET>
+ <RED>-QAnother outlier<RESET>
+ <BOLD;MAGENTA>-QQQIndented<RESET>
+ <BOLD;MAGENTA>-QQQText across<RESET>
+ <BOLD;MAGENTA>-QQQfive lines<RESET>
+ <BOLD;MAGENTA>-QQQthat has similar lines<RESET>
+ <BOLD;MAGENTA>-QQQto previous blocks, but with different indent<RESET>
+ <RED>-QQQYetQAnotherQoutlierQ<RESET>
+ <RED>-QLine with internal w h i t e s p a c e change<RESET>
+ <BOLD;CYAN>+<RESET>QQ<BOLD;CYAN>Indented<RESET>
+ <BOLD;CYAN>+<RESET>QQ<BOLD;CYAN>Text across<RESET>
+ <BOLD;CYAN>+<RESET>QQ<BOLD;CYAN>some lines<RESET>
+ <GREEN>+<RESET>QQQ<GREEN>But! <- this stands out<RESET>
+ <BOLD;CYAN>+<RESET><BOLD;CYAN>Adjusting with<RESET>
+ <BOLD;CYAN>+<RESET>Q<BOLD;CYAN>different starting<RESET>
+ <BOLD;CYAN>+<RESET><BOLD;CYAN>white spaces<RESET>
+ <GREEN>+<RESET><GREEN>AnotherQoutlier<RESET>
+ <BOLD;CYAN>+<RESET>QQ<BOLD;CYAN>Indented<RESET>
+ <BOLD;CYAN>+<RESET>QQ<BOLD;CYAN>Text across<RESET>
+ <BOLD;CYAN>+<RESET>QQ<BOLD;CYAN>five lines<RESET>
+ <BOLD;CYAN>+<RESET>QQ<BOLD;CYAN>that has similar lines<RESET>
+ <BOLD;CYAN>+<RESET>QQ<BOLD;CYAN>to previous blocks, but with different indent<RESET>
+ <GREEN>+<RESET>QQ<GREEN>YetQAnotherQoutlier<RESET>
+ <GREEN>+<RESET>Q<GREEN>Line with internal whitespace change<RESET>
+ EOF
+
+ test_cmp expected actual
+'
+
+test_expect_success 'bogus settings in move detection erroring out' '
+ test_must_fail git diff --color-moved=bogus 2>err &&
+ test_grep "must be one of" err &&
+ test_grep bogus err &&
+
+ test_must_fail git -c diff.colormoved=bogus diff 2>err &&
+ test_grep "must be one of" err &&
+ test_grep "from command-line config" err &&
+
+ test_must_fail git diff --color-moved-ws=bogus 2>err &&
+ test_grep "possible values" err &&
+ test_grep bogus err &&
+
+ test_must_fail git -c diff.colormovedws=bogus diff 2>err &&
+ test_grep "possible values" err &&
+ test_grep "from command-line config" err
+'
+
+test_expect_success 'compare whitespace delta incompatible with other space options' '
+ test_must_fail git diff \
+ --color-moved-ws=allow-indentation-change,ignore-all-space \
+ 2>err &&
+ test_grep allow-indentation-change err
+'
+
+EMPTY=''
+test_expect_success 'compare mixed whitespace delta across moved blocks' '
+
+ git reset --hard &&
+ tr "^|Q_" "\f\v\t " <<-EOF >text.txt &&
+ ^__
+ |____too short without
+ ^
+ ___being grouped across blank line
+ ${EMPTY}
+ context
+ lines
+ to
+ anchor
+ ____Indented text to
+ _Q____be further indented by four spaces across
+ ____Qseveral lines
+ QQ____These two lines have had their
+ ____indentation reduced by four spaces
+ Qdifferent indentation change
+ ____too short
+ EOF
+
+ git add text.txt &&
+ git commit -m "add text.txt" &&
+
+ tr "^|Q_" "\f\v\t " <<-EOF >text.txt &&
+ context
+ lines
+ to
+ anchor
+ QIndented text to
+ QQbe further indented by four spaces across
+ Q____several lines
+ ${EMPTY}
+ QQtoo short without
+ ${EMPTY}
+ ^Q_______being grouped across blank line
+ ${EMPTY}
+ Q_QThese two lines have had their
+ indentation reduced by four spaces
+ QQdifferent indentation change
+ __Qtoo short
+ EOF
+
+ git -c color.diff.whitespace="normal red" \
+ -c core.whitespace=space-before-tab \
+ diff --color --color-moved --ws-error-highlight=all \
+ --color-moved-ws=allow-indentation-change >actual.raw &&
+ grep -v "index" actual.raw | tr "\f\v" "^|" | test_decode_color >actual &&
+
+ cat <<-\EOF >expected &&
+ <BOLD>diff --git a/text.txt b/text.txt<RESET>
+ <BOLD>--- a/text.txt<RESET>
+ <BOLD>+++ b/text.txt<RESET>
+ <CYAN>@@ -1,16 +1,16 @@<RESET>
+ <BOLD;MAGENTA>-<RESET><BOLD;MAGENTA>^<RESET><BRED> <RESET>
+ <BOLD;MAGENTA>-<RESET><BOLD;MAGENTA>| too short without<RESET>
+ <BOLD;MAGENTA>-<RESET><BOLD;MAGENTA>^<RESET>
+ <BOLD;MAGENTA>-<RESET><BOLD;MAGENTA> being grouped across blank line<RESET>
+ <BOLD;MAGENTA>-<RESET>
+ <RESET>context<RESET>
+ <RESET>lines<RESET>
+ <RESET>to<RESET>
+ <RESET>anchor<RESET>
+ <BOLD;MAGENTA>-<RESET><BOLD;MAGENTA> Indented text to<RESET>
+ <BOLD;MAGENTA>-<RESET><BRED> <RESET> <BOLD;MAGENTA> be further indented by four spaces across<RESET>
+ <BOLD;MAGENTA>-<RESET><BRED> <RESET> <BOLD;MAGENTA>several lines<RESET>
+ <BOLD;BLUE>-<RESET> <BOLD;BLUE> These two lines have had their<RESET>
+ <BOLD;BLUE>-<RESET><BOLD;BLUE> indentation reduced by four spaces<RESET>
+ <BOLD;MAGENTA>-<RESET> <BOLD;MAGENTA>different indentation change<RESET>
+ <RED>-<RESET><RED> too short<RESET>
+ <BOLD;CYAN>+<RESET> <BOLD;CYAN>Indented text to<RESET>
+ <BOLD;CYAN>+<RESET> <BOLD;CYAN>be further indented by four spaces across<RESET>
+ <BOLD;CYAN>+<RESET> <BOLD;CYAN> several lines<RESET>
+ <BOLD;YELLOW>+<RESET>
+ <BOLD;YELLOW>+<RESET> <BOLD;YELLOW>too short without<RESET>
+ <BOLD;YELLOW>+<RESET>
+ <BOLD;YELLOW>+<RESET><BOLD;YELLOW>^ being grouped across blank line<RESET>
+ <BOLD;YELLOW>+<RESET>
+ <BOLD;CYAN>+<RESET> <BRED> <RESET> <BOLD;CYAN>These two lines have had their<RESET>
+ <BOLD;CYAN>+<RESET><BOLD;CYAN>indentation reduced by four spaces<RESET>
+ <BOLD;YELLOW>+<RESET> <BOLD;YELLOW>different indentation change<RESET>
+ <GREEN>+<RESET><BRED> <RESET> <GREEN>too short<RESET>
+ EOF
+
+ test_cmp expected actual
+'
+
+test_expect_success 'combine --ignore-blank-lines with --function-context' '
+ test_write_lines 1 "" 2 3 4 5 >a &&
+ test_write_lines 1 2 3 4 >b &&
+ test_must_fail git diff --no-index \
+ --ignore-blank-lines --function-context a b >actual.raw &&
+ sed -n "/@@/,\$p" <actual.raw >actual &&
+ cat <<-\EOF >expect &&
+ @@ -1,6 +1,4 @@
+ 1
+ -
+ 2
+ 3
+ 4
+ -5
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'combine --ignore-blank-lines with --function-context 2' '
+ test_write_lines a b c "" function 1 2 3 4 5 "" 6 7 8 9 >a &&
+ test_write_lines "" a b c "" function 1 2 3 4 5 6 7 8 >b &&
+ test_must_fail git diff --no-index \
+ --ignore-blank-lines --function-context a b >actual.raw &&
+ sed -n "/@@/,\$p" <actual.raw >actual &&
+ cat <<-\EOF >expect &&
+ @@ -5,11 +6,9 @@ c
+ function
+ 1
+ 2
+ 3
+ 4
+ 5
+ -
+ 6
+ 7
+ 8
+ -9
+ EOF
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t4016-diff-quote.sh b/t/t4016-diff-quote.sh
new file mode 100755
index 0000000..5a8d887
--- /dev/null
+++ b/t/t4016-diff-quote.sh
@@ -0,0 +1,89 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Junio C Hamano
+#
+
+test_description='Quoting paths in diff output.
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+P0='pathname'
+P1='pathname with HT'
+P2='pathname with SP'
+P3='pathname
+with LF'
+test_have_prereq !MINGW &&
+echo 2>/dev/null >"$P1" && test -f "$P1" && rm -f "$P1" || {
+ skip_all='Your filesystem does not allow tabs in filenames'
+ test_done
+}
+
+test_expect_success setup '
+ echo P0.0 >"$P0.0" &&
+ echo P0.1 >"$P0.1" &&
+ echo P0.2 >"$P0.2" &&
+ echo P0.3 >"$P0.3" &&
+ echo P1.0 >"$P1.0" &&
+ echo P1.2 >"$P1.2" &&
+ echo P1.3 >"$P1.3" &&
+ git add . &&
+ git commit -m initial &&
+ git mv "$P0.0" "R$P0.0" &&
+ git mv "$P0.1" "R$P1.0" &&
+ git mv "$P0.2" "R$P2.0" &&
+ git mv "$P0.3" "R$P3.0" &&
+ git mv "$P1.0" "R$P0.1" &&
+ git mv "$P1.2" "R$P2.1" &&
+ git mv "$P1.3" "R$P3.1" &&
+ :
+'
+
+test_expect_success 'setup expected files' '
+cat >expect <<\EOF
+ rename pathname.1 => "Rpathname\twith HT.0" (100%)
+ rename pathname.3 => "Rpathname\nwith LF.0" (100%)
+ rename "pathname\twith HT.3" => "Rpathname\nwith LF.1" (100%)
+ rename pathname.2 => Rpathname with SP.0 (100%)
+ rename "pathname\twith HT.2" => Rpathname with SP.1 (100%)
+ rename pathname.0 => Rpathname.0 (100%)
+ rename "pathname\twith HT.0" => Rpathname.1 (100%)
+EOF
+'
+
+test_expect_success 'git diff --summary -M HEAD' '
+ git diff --summary -M HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git diff --numstat -M HEAD' '
+ cat >expect <<-\EOF &&
+ 0 0 pathname.1 => "Rpathname\twith HT.0"
+ 0 0 pathname.3 => "Rpathname\nwith LF.0"
+ 0 0 "pathname\twith HT.3" => "Rpathname\nwith LF.1"
+ 0 0 pathname.2 => Rpathname with SP.0
+ 0 0 "pathname\twith HT.2" => Rpathname with SP.1
+ 0 0 pathname.0 => Rpathname.0
+ 0 0 "pathname\twith HT.0" => Rpathname.1
+ EOF
+ git diff --numstat -M HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git diff --stat -M HEAD' '
+ cat >expect <<-\EOF &&
+ pathname.1 => "Rpathname\twith HT.0" | 0
+ pathname.3 => "Rpathname\nwith LF.0" | 0
+ "pathname\twith HT.3" => "Rpathname\nwith LF.1" | 0
+ pathname.2 => Rpathname with SP.0 | 0
+ "pathname\twith HT.2" => Rpathname with SP.1 | 0
+ pathname.0 => Rpathname.0 | 0
+ "pathname\twith HT.0" => Rpathname.1 | 0
+ 7 files changed, 0 insertions(+), 0 deletions(-)
+ EOF
+ git diff --stat -M HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t4017-diff-retval.sh b/t/t4017-diff-retval.sh
new file mode 100755
index 0000000..f439f46
--- /dev/null
+++ b/t/t4017-diff-retval.sh
@@ -0,0 +1,146 @@
+#!/bin/sh
+
+test_description='Return value of diffs'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ echo "1 " >a &&
+ git add . &&
+ git commit -m zeroth &&
+ echo 1 >a &&
+ git add . &&
+ git commit -m first &&
+ echo 2 >b &&
+ git add . &&
+ git commit -a -m second
+'
+
+test_expect_success 'git diff --quiet -w HEAD^^ HEAD^' '
+ git diff --quiet -w HEAD^^ HEAD^
+'
+
+test_expect_success 'git diff --quiet HEAD^^ HEAD^' '
+ test_must_fail git diff --quiet HEAD^^ HEAD^
+'
+
+test_expect_success 'git diff --quiet -w HEAD^ HEAD' '
+ test_must_fail git diff --quiet -w HEAD^ HEAD
+'
+
+test_expect_success 'git diff-tree HEAD^ HEAD' '
+ test_expect_code 1 git diff-tree --exit-code HEAD^ HEAD
+'
+test_expect_success 'git diff-tree HEAD^ HEAD -- a' '
+ git diff-tree --exit-code HEAD^ HEAD -- a
+'
+test_expect_success 'git diff-tree HEAD^ HEAD -- b' '
+ test_expect_code 1 git diff-tree --exit-code HEAD^ HEAD -- b
+'
+test_expect_success 'echo HEAD | git diff-tree --stdin' '
+ echo $(git rev-parse HEAD) | test_expect_code 1 git diff-tree --exit-code --stdin
+'
+test_expect_success 'git diff-tree HEAD HEAD' '
+ git diff-tree --exit-code HEAD HEAD
+'
+test_expect_success 'git diff-files' '
+ git diff-files --exit-code
+'
+test_expect_success 'git diff-index --cached HEAD' '
+ git diff-index --exit-code --cached HEAD
+'
+test_expect_success 'git diff-index --cached HEAD^' '
+ test_expect_code 1 git diff-index --exit-code --cached HEAD^
+'
+test_expect_success 'git diff-index --cached HEAD^' '
+ echo text >>b &&
+ echo 3 >c &&
+ git add . &&
+ test_expect_code 1 git diff-index --exit-code --cached HEAD^
+'
+test_expect_success 'git diff-tree -Stext HEAD^ HEAD -- b' '
+ git commit -m "text in b" &&
+ test_expect_code 1 git diff-tree -p --exit-code -Stext HEAD^ HEAD -- b
+'
+test_expect_success 'git diff-tree -Snot-found HEAD^ HEAD -- b' '
+ git diff-tree -p --exit-code -Snot-found HEAD^ HEAD -- b
+'
+test_expect_success 'git diff-files' '
+ echo 3 >>c &&
+ test_expect_code 1 git diff-files --exit-code
+'
+test_expect_success 'git diff-index --cached HEAD' '
+ git update-index c &&
+ test_expect_code 1 git diff-index --exit-code --cached HEAD
+'
+
+test_expect_success '--check --exit-code returns 0 for no difference' '
+
+ git diff --check --exit-code
+
+'
+
+test_expect_success '--check --exit-code returns 1 for a clean difference' '
+
+ echo "good" > a &&
+ test_expect_code 1 git diff --check --exit-code
+
+'
+
+test_expect_success '--check --exit-code returns 3 for a dirty difference' '
+
+ echo "bad " >> a &&
+ test_expect_code 3 git diff --check --exit-code
+
+'
+
+test_expect_success '--check with --no-pager returns 2 for dirty difference' '
+
+ test_expect_code 2 git --no-pager diff --check
+
+'
+
+test_expect_success 'check should test not just the last line' '
+ echo "" >>a &&
+ test_expect_code 2 git --no-pager diff --check
+
+'
+
+test_expect_success 'check detects leftover conflict markers' '
+ git reset --hard &&
+ git checkout HEAD^ &&
+ echo binary >>b &&
+ git commit -m "side" b &&
+ test_must_fail git merge main &&
+ git add b &&
+ test_expect_code 2 git --no-pager diff --cached --check >test.out &&
+ test 3 = $(grep "conflict marker" test.out | wc -l) &&
+ git reset --hard
+'
+
+test_expect_success 'check honors conflict marker length' '
+ git reset --hard &&
+ echo ">>>>>>> boo" >>b &&
+ echo "======" >>a &&
+ git diff --check a &&
+ test_expect_code 2 git diff --check b &&
+ git reset --hard &&
+ echo ">>>>>>>> boo" >>b &&
+ echo "========" >>a &&
+ git diff --check &&
+ echo "b conflict-marker-size=8" >.gitattributes &&
+ test_expect_code 2 git diff --check b &&
+ git diff --check a &&
+ git reset --hard
+'
+
+test_expect_success 'option errors are not confused by --exit-code' '
+ test_must_fail git diff --exit-code --nonsense 2>err &&
+ grep '^usage:' err
+'
+
+test_done
diff --git a/t/t4018-diff-funcname.sh b/t/t4018-diff-funcname.sh
new file mode 100755
index 0000000..e026fac
--- /dev/null
+++ b/t/t4018-diff-funcname.sh
@@ -0,0 +1,122 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Johannes E. Schindelin
+#
+
+test_description='Test custom diff function name patterns'
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ # a non-trivial custom pattern
+ git config diff.custom1.funcname "!static
+!String
+[^ ].*s.*" &&
+
+ # a custom pattern which matches to end of line
+ git config diff.custom2.funcname "......Beer\$" &&
+
+ # alternation in pattern
+ git config diff.custom3.funcname "Beer$" &&
+ git config diff.custom3.xfuncname "^[ ]*((public|static).*)$" &&
+
+ # for regexp compilation tests
+ echo A >A.java &&
+ echo B >B.java
+'
+
+test_expect_success 'setup: test-tool userdiff' '
+ # Make sure additions to builtin_drivers are sorted
+ test_when_finished "rm builtin-drivers.sorted" &&
+ test-tool userdiff list-builtin-drivers >builtin-drivers &&
+ test_file_not_empty builtin-drivers &&
+ sort <builtin-drivers >builtin-drivers.sorted &&
+ test_cmp builtin-drivers.sorted builtin-drivers &&
+
+ # Ditto, but "custom" requires the .git directory and config
+ # to be setup and read.
+ test_when_finished "rm custom-drivers.sorted" &&
+ test-tool userdiff list-custom-drivers >custom-drivers &&
+ test_file_not_empty custom-drivers &&
+ sort <custom-drivers >custom-drivers.sorted &&
+ test_cmp custom-drivers.sorted custom-drivers
+'
+
+diffpatterns="
+ $(cat builtin-drivers)
+ $(cat custom-drivers)
+"
+
+for p in $diffpatterns
+do
+ test_expect_success "builtin $p pattern compiles" '
+ echo "*.java diff=$p" >.gitattributes &&
+ test_expect_code 1 git diff --no-index \
+ A.java B.java 2>msg &&
+ test_grep ! fatal msg &&
+ test_grep ! error msg
+ '
+ test_expect_success "builtin $p wordRegex pattern compiles" '
+ echo "*.java diff=$p" >.gitattributes &&
+ test_expect_code 1 git diff --no-index --word-diff \
+ A.java B.java 2>msg &&
+ test_grep ! fatal msg &&
+ test_grep ! error msg
+ '
+
+ test_expect_success "builtin $p pattern compiles on bare repo with --attr-source" '
+ test_when_finished "rm -rf bare.git" &&
+ git checkout -B master &&
+ git add . &&
+ echo "*.java diff=notexist" >.gitattributes &&
+ git add .gitattributes &&
+ git commit -am "changing gitattributes" &&
+ git checkout -B branchA &&
+ echo "*.java diff=$p" >.gitattributes &&
+ git add .gitattributes &&
+ git commit -am "changing gitattributes" &&
+ git clone --bare --no-local . bare.git &&
+ git -C bare.git symbolic-ref HEAD refs/heads/master &&
+ test_expect_code 1 git -C bare.git --attr-source=branchA \
+ diff --exit-code HEAD:A.java HEAD:B.java 2>msg &&
+ test_grep ! fatal msg &&
+ test_grep ! error msg
+ '
+done
+
+test_expect_success 'last regexp must not be negated' '
+ echo "*.java diff=java" >.gitattributes &&
+ test_config diff.java.funcname "!static" &&
+ test_expect_code 128 git diff --no-index A.java B.java 2>msg &&
+ test_grep ": Last expression must not be negated:" msg
+'
+
+test_expect_success 'setup hunk header tests' '
+ for i in $diffpatterns
+ do
+ echo "$i-* diff=$i" || return 1
+ done > .gitattributes &&
+
+ # add all test files to the index
+ (
+ cd "$TEST_DIRECTORY"/t4018 &&
+ git --git-dir="$TRASH_DIRECTORY/.git" add .
+ ) &&
+
+ # place modified files in the worktree
+ for i in $(git ls-files)
+ do
+ sed -e "s/ChangeMe/IWasChanged/" <"$TEST_DIRECTORY/t4018/$i" >"$i" || return 1
+ done
+'
+
+# check each individual file
+for i in $(git ls-files)
+do
+ test_expect_success "hunk header: $i" "
+ git diff -U1 $i >actual &&
+ grep '@@ .* @@.*RIGHT' actual
+ "
+done
+
+test_done
diff --git a/t/t4018/README b/t/t4018/README
new file mode 100644
index 0000000..2d25b2b
--- /dev/null
+++ b/t/t4018/README
@@ -0,0 +1,15 @@
+How to write RIGHT test cases
+=============================
+
+Insert the word "ChangeMe" (exactly this form) at a distance of
+at least two lines from the line that must appear in the hunk header.
+
+The text that must appear in the hunk header must contain the word
+"right", but in all upper-case, like in the title above.
+
+This text is a bit twisted and out of order, but it is itself a
+test case for the default hunk header pattern. Know what you are doing
+if you change it.
+
+BTW, this tests that the head line goes to the hunk header, not the line
+of equal signs.
diff --git a/t/t4018/bash-arithmetic-function b/t/t4018/bash-arithmetic-function
new file mode 100644
index 0000000..c0b276c
--- /dev/null
+++ b/t/t4018/bash-arithmetic-function
@@ -0,0 +1,4 @@
+RIGHT() ((
+
+ ChangeMe = "$x" + "$y"
+))
diff --git a/t/t4018/bash-bashism-style-compact b/t/t4018/bash-bashism-style-compact
new file mode 100644
index 0000000..1ca3126
--- /dev/null
+++ b/t/t4018/bash-bashism-style-compact
@@ -0,0 +1,6 @@
+function RIGHT {
+ function InvalidSyntax{
+ :
+ echo 'ChangeMe'
+ }
+}
diff --git a/t/t4018/bash-bashism-style-function b/t/t4018/bash-bashism-style-function
new file mode 100644
index 0000000..f1de4fa
--- /dev/null
+++ b/t/t4018/bash-bashism-style-function
@@ -0,0 +1,4 @@
+function RIGHT {
+ :
+ echo 'ChangeMe'
+}
diff --git a/t/t4018/bash-bashism-style-whitespace b/t/t4018/bash-bashism-style-whitespace
new file mode 100644
index 0000000..ade85dd
--- /dev/null
+++ b/t/t4018/bash-bashism-style-whitespace
@@ -0,0 +1,4 @@
+ function RIGHT ( ) {
+
+ ChangeMe
+ }
diff --git a/t/t4018/bash-conditional-function b/t/t4018/bash-conditional-function
new file mode 100644
index 0000000..c5949e8
--- /dev/null
+++ b/t/t4018/bash-conditional-function
@@ -0,0 +1,4 @@
+RIGHT() [[ \
+
+ "$a" > "$ChangeMe"
+]]
diff --git a/t/t4018/bash-missing-parentheses b/t/t4018/bash-missing-parentheses
new file mode 100644
index 0000000..8c8a05d
--- /dev/null
+++ b/t/t4018/bash-missing-parentheses
@@ -0,0 +1,6 @@
+function RIGHT {
+ functionInvalidSyntax {
+ :
+ echo 'ChangeMe'
+ }
+}
diff --git a/t/t4018/bash-mixed-style-compact b/t/t4018/bash-mixed-style-compact
new file mode 100644
index 0000000..d9364cb
--- /dev/null
+++ b/t/t4018/bash-mixed-style-compact
@@ -0,0 +1,4 @@
+function RIGHT(){
+ :
+ echo 'ChangeMe'
+}
diff --git a/t/t4018/bash-mixed-style-function b/t/t4018/bash-mixed-style-function
new file mode 100644
index 0000000..555f9b2
--- /dev/null
+++ b/t/t4018/bash-mixed-style-function
@@ -0,0 +1,4 @@
+function RIGHT() {
+
+ ChangeMe
+}
diff --git a/t/t4018/bash-nested-functions b/t/t4018/bash-nested-functions
new file mode 100644
index 0000000..2c9237e
--- /dev/null
+++ b/t/t4018/bash-nested-functions
@@ -0,0 +1,6 @@
+outer() {
+ RIGHT() {
+ :
+ echo 'ChangeMe'
+ }
+}
diff --git a/t/t4018/bash-other-characters b/t/t4018/bash-other-characters
new file mode 100644
index 0000000..a3f390d
--- /dev/null
+++ b/t/t4018/bash-other-characters
@@ -0,0 +1,4 @@
+_RIGHT_0n() {
+
+ ChangeMe
+}
diff --git a/t/t4018/bash-posix-style-compact b/t/t4018/bash-posix-style-compact
new file mode 100644
index 0000000..045bd20
--- /dev/null
+++ b/t/t4018/bash-posix-style-compact
@@ -0,0 +1,4 @@
+RIGHT(){
+
+ ChangeMe
+}
diff --git a/t/t4018/bash-posix-style-function b/t/t4018/bash-posix-style-function
new file mode 100644
index 0000000..a4d1448
--- /dev/null
+++ b/t/t4018/bash-posix-style-function
@@ -0,0 +1,4 @@
+RIGHT() {
+
+ ChangeMe
+}
diff --git a/t/t4018/bash-posix-style-whitespace b/t/t4018/bash-posix-style-whitespace
new file mode 100644
index 0000000..4d984f0
--- /dev/null
+++ b/t/t4018/bash-posix-style-whitespace
@@ -0,0 +1,4 @@
+ RIGHT ( ) {
+
+ ChangeMe
+ }
diff --git a/t/t4018/bash-subshell-function b/t/t4018/bash-subshell-function
new file mode 100644
index 0000000..80baa09
--- /dev/null
+++ b/t/t4018/bash-subshell-function
@@ -0,0 +1,4 @@
+RIGHT() (
+
+ ChangeMe=2
+)
diff --git a/t/t4018/bash-trailing-comment b/t/t4018/bash-trailing-comment
new file mode 100644
index 0000000..f1edbed
--- /dev/null
+++ b/t/t4018/bash-trailing-comment
@@ -0,0 +1,4 @@
+RIGHT() { # Comment
+
+ ChangeMe
+}
diff --git a/t/t4018/cpp-c++-function b/t/t4018/cpp-c++-function
new file mode 100644
index 0000000..9ee6bbe
--- /dev/null
+++ b/t/t4018/cpp-c++-function
@@ -0,0 +1,4 @@
+Item RIGHT::DoSomething( Args with_spaces )
+{
+ ChangeMe;
+}
diff --git a/t/t4018/cpp-class-constructor b/t/t4018/cpp-class-constructor
new file mode 100644
index 0000000..ec4f115
--- /dev/null
+++ b/t/t4018/cpp-class-constructor
@@ -0,0 +1,4 @@
+Item::Item(int RIGHT)
+{
+ ChangeMe;
+}
diff --git a/t/t4018/cpp-class-constructor-mem-init b/t/t4018/cpp-class-constructor-mem-init
new file mode 100644
index 0000000..49a69f3
--- /dev/null
+++ b/t/t4018/cpp-class-constructor-mem-init
@@ -0,0 +1,5 @@
+Item::Item(int RIGHT) :
+ member(0)
+{
+ ChangeMe;
+}
diff --git a/t/t4018/cpp-class-definition b/t/t4018/cpp-class-definition
new file mode 100644
index 0000000..11b61da
--- /dev/null
+++ b/t/t4018/cpp-class-definition
@@ -0,0 +1,4 @@
+class RIGHT
+{
+ int ChangeMe;
+};
diff --git a/t/t4018/cpp-class-definition-derived b/t/t4018/cpp-class-definition-derived
new file mode 100644
index 0000000..3b98cd0
--- /dev/null
+++ b/t/t4018/cpp-class-definition-derived
@@ -0,0 +1,5 @@
+class RIGHT :
+ public Baseclass
+{
+ int ChangeMe;
+};
diff --git a/t/t4018/cpp-class-destructor b/t/t4018/cpp-class-destructor
new file mode 100644
index 0000000..5487665
--- /dev/null
+++ b/t/t4018/cpp-class-destructor
@@ -0,0 +1,4 @@
+RIGHT::~RIGHT()
+{
+ ChangeMe;
+}
diff --git a/t/t4018/cpp-function-returning-global-type b/t/t4018/cpp-function-returning-global-type
new file mode 100644
index 0000000..1084d59
--- /dev/null
+++ b/t/t4018/cpp-function-returning-global-type
@@ -0,0 +1,4 @@
+::Item get::it::RIGHT()
+{
+ ChangeMe;
+}
diff --git a/t/t4018/cpp-function-returning-nested b/t/t4018/cpp-function-returning-nested
new file mode 100644
index 0000000..d9750aa
--- /dev/null
+++ b/t/t4018/cpp-function-returning-nested
@@ -0,0 +1,5 @@
+get::Item get::it::RIGHT()
+{
+ ChangeMe;
+}
+
diff --git a/t/t4018/cpp-function-returning-pointer b/t/t4018/cpp-function-returning-pointer
new file mode 100644
index 0000000..ef15657
--- /dev/null
+++ b/t/t4018/cpp-function-returning-pointer
@@ -0,0 +1,4 @@
+const char *get_it_RIGHT(char *ptr)
+{
+ ChangeMe;
+}
diff --git a/t/t4018/cpp-function-returning-reference b/t/t4018/cpp-function-returning-reference
new file mode 100644
index 0000000..01b051d
--- /dev/null
+++ b/t/t4018/cpp-function-returning-reference
@@ -0,0 +1,4 @@
+string& get::it::RIGHT(char *ptr)
+{
+ ChangeMe;
+}
diff --git a/t/t4018/cpp-gnu-style-function b/t/t4018/cpp-gnu-style-function
new file mode 100644
index 0000000..08c7c75
--- /dev/null
+++ b/t/t4018/cpp-gnu-style-function
@@ -0,0 +1,5 @@
+const char *
+RIGHT(int arg)
+{
+ ChangeMe;
+}
diff --git a/t/t4018/cpp-namespace-definition b/t/t4018/cpp-namespace-definition
new file mode 100644
index 0000000..6749980
--- /dev/null
+++ b/t/t4018/cpp-namespace-definition
@@ -0,0 +1,4 @@
+namespace RIGHT
+{
+ ChangeMe;
+}
diff --git a/t/t4018/cpp-operator-definition b/t/t4018/cpp-operator-definition
new file mode 100644
index 0000000..1acd827
--- /dev/null
+++ b/t/t4018/cpp-operator-definition
@@ -0,0 +1,4 @@
+Value operator+(Value LEFT, Value RIGHT)
+{
+ ChangeMe;
+}
diff --git a/t/t4018/cpp-skip-access-specifiers b/t/t4018/cpp-skip-access-specifiers
new file mode 100644
index 0000000..4d4a9db
--- /dev/null
+++ b/t/t4018/cpp-skip-access-specifiers
@@ -0,0 +1,8 @@
+class RIGHT : public Baseclass
+{
+public:
+protected:
+private:
+ void DoSomething();
+ int ChangeMe;
+};
diff --git a/t/t4018/cpp-skip-comment-block b/t/t4018/cpp-skip-comment-block
new file mode 100644
index 0000000..3800b99
--- /dev/null
+++ b/t/t4018/cpp-skip-comment-block
@@ -0,0 +1,9 @@
+struct item RIGHT(int i)
+// Do not
+// pick up
+/* these
+** comments.
+*/
+{
+ ChangeMe;
+}
diff --git a/t/t4018/cpp-skip-labels b/t/t4018/cpp-skip-labels
new file mode 100644
index 0000000..b9c10ab
--- /dev/null
+++ b/t/t4018/cpp-skip-labels
@@ -0,0 +1,8 @@
+void RIGHT (void)
+{
+repeat: // C++ comment
+next: /* C comment */
+ do_something();
+
+ ChangeMe;
+}
diff --git a/t/t4018/cpp-struct-definition b/t/t4018/cpp-struct-definition
new file mode 100644
index 0000000..521c59f
--- /dev/null
+++ b/t/t4018/cpp-struct-definition
@@ -0,0 +1,9 @@
+struct RIGHT {
+ unsigned
+ /* this bit field looks like a label and should not be picked up */
+ decoy_bitfield: 2,
+ more : 1;
+ int filler;
+
+ int ChangeMe;
+};
diff --git a/t/t4018/cpp-struct-single-line b/t/t4018/cpp-struct-single-line
new file mode 100644
index 0000000..a0de5fb
--- /dev/null
+++ b/t/t4018/cpp-struct-single-line
@@ -0,0 +1,7 @@
+void wrong()
+{
+}
+
+struct RIGHT_iterator_tag {};
+
+int ChangeMe;
diff --git a/t/t4018/cpp-template-function-definition b/t/t4018/cpp-template-function-definition
new file mode 100644
index 0000000..0cdf5ba
--- /dev/null
+++ b/t/t4018/cpp-template-function-definition
@@ -0,0 +1,4 @@
+template<class T> int RIGHT(T arg)
+{
+ ChangeMe;
+}
diff --git a/t/t4018/cpp-union-definition b/t/t4018/cpp-union-definition
new file mode 100644
index 0000000..7ec94df
--- /dev/null
+++ b/t/t4018/cpp-union-definition
@@ -0,0 +1,4 @@
+union RIGHT {
+ double v;
+ int ChangeMe;
+};
diff --git a/t/t4018/cpp-void-c-function b/t/t4018/cpp-void-c-function
new file mode 100644
index 0000000..153081e
--- /dev/null
+++ b/t/t4018/cpp-void-c-function
@@ -0,0 +1,4 @@
+void RIGHT (void)
+{
+ ChangeMe;
+}
diff --git a/t/t4018/css-attribute-value-selector b/t/t4018/css-attribute-value-selector
new file mode 100644
index 0000000..918256b
--- /dev/null
+++ b/t/t4018/css-attribute-value-selector
@@ -0,0 +1,4 @@
+[class*="RIGHT"] {
+ background : #000;
+ border : 10px ChangeMe #C6C6C6;
+}
diff --git a/t/t4018/css-block-level-@-statements b/t/t4018/css-block-level-@-statements
new file mode 100644
index 0000000..d6755f2
--- /dev/null
+++ b/t/t4018/css-block-level-@-statements
@@ -0,0 +1,10 @@
+@keyframes RIGHT {
+ from {
+ background : #000;
+ border : 10px ChangeMe #C6C6C6;
+ }
+ to {
+ background : #fff;
+ border : 10px solid #C6C6C6;
+ }
+}
diff --git a/t/t4018/css-brace-in-col-1 b/t/t4018/css-brace-in-col-1
new file mode 100644
index 0000000..7831577
--- /dev/null
+++ b/t/t4018/css-brace-in-col-1
@@ -0,0 +1,5 @@
+RIGHT label.control-label
+{
+ margin-top: 10px!important;
+ border : 10px ChangeMe #C6C6C6;
+}
diff --git a/t/t4018/css-class-selector b/t/t4018/css-class-selector
new file mode 100644
index 0000000..f790a00
--- /dev/null
+++ b/t/t4018/css-class-selector
@@ -0,0 +1,4 @@
+.RIGHT {
+ background : #000;
+ border : 10px ChangeMe #C6C6C6;
+}
diff --git a/t/t4018/css-colon-eol b/t/t4018/css-colon-eol
new file mode 100644
index 0000000..5a30553
--- /dev/null
+++ b/t/t4018/css-colon-eol
@@ -0,0 +1,4 @@
+RIGHT h1 {
+color:
+ChangeMe;
+}
diff --git a/t/t4018/css-colon-selector b/t/t4018/css-colon-selector
new file mode 100644
index 0000000..c6d71fb
--- /dev/null
+++ b/t/t4018/css-colon-selector
@@ -0,0 +1,5 @@
+RIGHT a:hover {
+ margin-top:
+ 10px!important;
+ border : 10px ChangeMe #C6C6C6;
+}
diff --git a/t/t4018/css-common b/t/t4018/css-common
new file mode 100644
index 0000000..84ed754
--- /dev/null
+++ b/t/t4018/css-common
@@ -0,0 +1,4 @@
+RIGHT label.control-label {
+ margin-top: 10px!important;
+ border : 10px ChangeMe #C6C6C6;
+}
diff --git a/t/t4018/css-id-selector b/t/t4018/css-id-selector
new file mode 100644
index 0000000..17c5111
--- /dev/null
+++ b/t/t4018/css-id-selector
@@ -0,0 +1,4 @@
+#RIGHT {
+ background : #000;
+ border : 10px ChangeMe #C6C6C6;
+}
diff --git a/t/t4018/css-long-selector-list b/t/t4018/css-long-selector-list
new file mode 100644
index 0000000..7ccd25d
--- /dev/null
+++ b/t/t4018/css-long-selector-list
@@ -0,0 +1,6 @@
+p.header,
+label.control-label,
+div ul#RIGHT {
+ margin-top: 10px!important;
+ border : 10px ChangeMe #C6C6C6;
+}
diff --git a/t/t4018/css-prop-sans-indent b/t/t4018/css-prop-sans-indent
new file mode 100644
index 0000000..a9e3c86
--- /dev/null
+++ b/t/t4018/css-prop-sans-indent
@@ -0,0 +1,5 @@
+RIGHT, label.control-label {
+margin-top: 10px!important;
+padding: 0;
+border : 10px ChangeMe #C6C6C6;
+}
diff --git a/t/t4018/css-root-selector b/t/t4018/css-root-selector
new file mode 100644
index 0000000..22b958e
--- /dev/null
+++ b/t/t4018/css-root-selector
@@ -0,0 +1,4 @@
+:RIGHT {
+ background : #000;
+ border : 10px ChangeMe #C6C6C6;
+}
diff --git a/t/t4018/css-short-selector-list b/t/t4018/css-short-selector-list
new file mode 100644
index 0000000..6a0bdee
--- /dev/null
+++ b/t/t4018/css-short-selector-list
@@ -0,0 +1,4 @@
+label.control, div ul#RIGHT {
+ margin-top: 10px!important;
+ border : 10px ChangeMe #C6C6C6;
+}
diff --git a/t/t4018/css-trailing-space b/t/t4018/css-trailing-space
new file mode 100644
index 0000000..32b5606
--- /dev/null
+++ b/t/t4018/css-trailing-space
@@ -0,0 +1,5 @@
+RIGHT label.control-label {
+ margin:10px;
+ padding:10px;
+ border : 10px ChangeMe #C6C6C6;
+}
diff --git a/t/t4018/custom1-pattern b/t/t4018/custom1-pattern
new file mode 100644
index 0000000..e8fd59f
--- /dev/null
+++ b/t/t4018/custom1-pattern
@@ -0,0 +1,17 @@
+public class Beer
+{
+ int special, RIGHT;
+ public static void main(String args[])
+ {
+ String s=" ";
+ for(int x = 99; x > 0; x--)
+ {
+ System.out.print(x + " bottles of beer on the wall "
+ + x + " bottles of beer\n" // ChangeMe
+ + "Take one down, pass it around, " + (x - 1)
+ + " bottles of beer on the wall.\n");
+ }
+ System.out.print("Go to the store, buy some more,\n"
+ + "99 bottles of beer on the wall.\n");
+ }
+}
diff --git a/t/t4018/custom2-match-to-end-of-line b/t/t4018/custom2-match-to-end-of-line
new file mode 100644
index 0000000..f88ac31
--- /dev/null
+++ b/t/t4018/custom2-match-to-end-of-line
@@ -0,0 +1,8 @@
+public class RIGHT_Beer
+{
+ int special;
+ public static void main(String args[])
+ {
+ System.out.print("ChangeMe");
+ }
+}
diff --git a/t/t4018/custom3-alternation-in-pattern b/t/t4018/custom3-alternation-in-pattern
new file mode 100644
index 0000000..5f3769c
--- /dev/null
+++ b/t/t4018/custom3-alternation-in-pattern
@@ -0,0 +1,17 @@
+public class Beer
+{
+ int special;
+ public static void main(String RIGHT[])
+ {
+ String s=" ";
+ for(int x = 99; x > 0; x--)
+ {
+ System.out.print(x + " bottles of beer on the wall "
+ + x + " bottles of beer\n" // ChangeMe
+ + "Take one down, pass it around, " + (x - 1)
+ + " bottles of beer on the wall.\n");
+ }
+ System.out.print("Go to the store, buy some more,\n"
+ + "99 bottles of beer on the wall.\n");
+ }
+}
diff --git a/t/t4018/dts-labels b/t/t4018/dts-labels
new file mode 100644
index 0000000..b21ef87
--- /dev/null
+++ b/t/t4018/dts-labels
@@ -0,0 +1,9 @@
+/ {
+ label_1: node1@ff00 {
+ label2: RIGHT {
+ vendor,some-property;
+
+ ChangeMe = <0x45-30>;
+ };
+ };
+};
diff --git a/t/t4018/dts-node-unitless b/t/t4018/dts-node-unitless
new file mode 100644
index 0000000..c5287d9
--- /dev/null
+++ b/t/t4018/dts-node-unitless
@@ -0,0 +1,8 @@
+/ {
+ label_1: node1 {
+ RIGHT {
+ prop-array = <1>, <4>;
+ ChangeMe = <0xffeedd00>;
+ };
+ };
+};
diff --git a/t/t4018/dts-nodes b/t/t4018/dts-nodes
new file mode 100644
index 0000000..5a4334b
--- /dev/null
+++ b/t/t4018/dts-nodes
@@ -0,0 +1,8 @@
+/ {
+ label_1: node1@ff00 {
+ RIGHT@deadf00,4000 {
+ #size-cells = <1>;
+ ChangeMe = <0xffeedd00>;
+ };
+ };
+};
diff --git a/t/t4018/dts-nodes-boolean-prop b/t/t4018/dts-nodes-boolean-prop
new file mode 100644
index 0000000..afc6b5b
--- /dev/null
+++ b/t/t4018/dts-nodes-boolean-prop
@@ -0,0 +1,9 @@
+/ {
+ label_1: node1@ff00 {
+ RIGHT@deadf00,4000 {
+ boolean-prop1;
+
+ ChangeMe;
+ };
+ };
+};
diff --git a/t/t4018/dts-nodes-comment1 b/t/t4018/dts-nodes-comment1
new file mode 100644
index 0000000..559dfce
--- /dev/null
+++ b/t/t4018/dts-nodes-comment1
@@ -0,0 +1,8 @@
+/ {
+ label_1: node1@ff00 {
+ RIGHT@deadf00,4000 /* &a comment */ {
+ #size-cells = <1>;
+ ChangeMe = <0xffeedd00>;
+ };
+ };
+};
diff --git a/t/t4018/dts-nodes-comment2 b/t/t4018/dts-nodes-comment2
new file mode 100644
index 0000000..27e9718
--- /dev/null
+++ b/t/t4018/dts-nodes-comment2
@@ -0,0 +1,8 @@
+/ {
+ label_1: node1@ff00 {
+ RIGHT@deadf00,4000 { /* a trailing comment */
+ #size-cells = <1>;
+ ChangeMe = <0xffeedd00>;
+ };
+ };
+};
diff --git a/t/t4018/dts-nodes-multiline-prop b/t/t4018/dts-nodes-multiline-prop
new file mode 100644
index 0000000..072d58b
--- /dev/null
+++ b/t/t4018/dts-nodes-multiline-prop
@@ -0,0 +1,13 @@
+/ {
+ label_1: node1@ff00 {
+ RIGHT@deadf00,4000 {
+ multilineprop = <3>,
+ <4>,
+ <5>,
+ <6>,
+ <7>;
+
+ ChangeMe = <0xffeedd00>;
+ };
+ };
+};
diff --git a/t/t4018/dts-reference b/t/t4018/dts-reference
new file mode 100644
index 0000000..8f0c87d
--- /dev/null
+++ b/t/t4018/dts-reference
@@ -0,0 +1,9 @@
+&label_1 {
+ TEST = <455>;
+};
+
+&RIGHT {
+ vendor,some-property;
+
+ ChangeMe = <0x45-30>;
+};
diff --git a/t/t4018/dts-root b/t/t4018/dts-root
new file mode 100644
index 0000000..4353b82
--- /dev/null
+++ b/t/t4018/dts-root
@@ -0,0 +1,5 @@
+/ { RIGHT /* Technically just supposed to be a slash and brace */
+ #size-cells = <1>;
+
+ ChangeMe = <0xffeedd00>;
+};
diff --git a/t/t4018/dts-root-comment b/t/t4018/dts-root-comment
new file mode 100644
index 0000000..333a625
--- /dev/null
+++ b/t/t4018/dts-root-comment
@@ -0,0 +1,8 @@
+/ { RIGHT /* Technically just supposed to be a slash and brace */
+ #size-cells = <1>;
+
+ /* This comment should be ignored */
+
+ some-property = <40+2>;
+ ChangeMe = <0xffeedd00>;
+};
diff --git a/t/t4018/elixir-do-not-pick-end b/t/t4018/elixir-do-not-pick-end
new file mode 100644
index 0000000..fae08ba
--- /dev/null
+++ b/t/t4018/elixir-do-not-pick-end
@@ -0,0 +1,5 @@
+defmodule RIGHT do
+end
+#
+#
+# ChangeMe; do not pick up 'end' line
diff --git a/t/t4018/elixir-ex-unit-test b/t/t4018/elixir-ex-unit-test
new file mode 100644
index 0000000..0560a2b
--- /dev/null
+++ b/t/t4018/elixir-ex-unit-test
@@ -0,0 +1,6 @@
+defmodule Test do
+ test "RIGHT" do
+ assert true == true
+ assert ChangeMe
+ end
+end
diff --git a/t/t4018/elixir-function b/t/t4018/elixir-function
new file mode 100644
index 0000000..d452f49
--- /dev/null
+++ b/t/t4018/elixir-function
@@ -0,0 +1,5 @@
+def function(RIGHT, arg) do
+ # comment
+ # comment
+ ChangeMe
+end
diff --git a/t/t4018/elixir-macro b/t/t4018/elixir-macro
new file mode 100644
index 0000000..4f925e9
--- /dev/null
+++ b/t/t4018/elixir-macro
@@ -0,0 +1,5 @@
+defmacro foo(RIGHT) do
+ # Code
+ # Code
+ ChangeMe
+end
diff --git a/t/t4018/elixir-module b/t/t4018/elixir-module
new file mode 100644
index 0000000..91a4e7a
--- /dev/null
+++ b/t/t4018/elixir-module
@@ -0,0 +1,9 @@
+defmodule RIGHT do
+ @moduledoc """
+ Foo bar
+ """
+
+ def ChangeMe(a) where is_map(a) do
+ a
+ end
+end
diff --git a/t/t4018/elixir-module-func b/t/t4018/elixir-module-func
new file mode 100644
index 0000000..c9910d0
--- /dev/null
+++ b/t/t4018/elixir-module-func
@@ -0,0 +1,8 @@
+defmodule Foo do
+ def fun(RIGHT) do
+ # Code
+ # Code
+ # Code
+ ChangeMe
+ end
+end
diff --git a/t/t4018/elixir-nested-module b/t/t4018/elixir-nested-module
new file mode 100644
index 0000000..771ebc5
--- /dev/null
+++ b/t/t4018/elixir-nested-module
@@ -0,0 +1,9 @@
+defmodule MyApp.RIGHT do
+ @moduledoc """
+ Foo bar
+ """
+
+ def ChangeMe(a) where is_map(a) do
+ a
+ end
+end
diff --git a/t/t4018/elixir-private-function b/t/t4018/elixir-private-function
new file mode 100644
index 0000000..1aabe33
--- /dev/null
+++ b/t/t4018/elixir-private-function
@@ -0,0 +1,5 @@
+defp function(RIGHT, arg) do
+ # comment
+ # comment
+ ChangeMe
+end
diff --git a/t/t4018/elixir-protocol b/t/t4018/elixir-protocol
new file mode 100644
index 0000000..7d91736
--- /dev/null
+++ b/t/t4018/elixir-protocol
@@ -0,0 +1,6 @@
+defprotocol RIGHT do
+ @doc """
+ Calculates the size (and not the length!) of a data structure
+ """
+ def size(data, ChangeMe)
+end
diff --git a/t/t4018/elixir-protocol-implementation b/t/t4018/elixir-protocol-implementation
new file mode 100644
index 0000000..f9234bb
--- /dev/null
+++ b/t/t4018/elixir-protocol-implementation
@@ -0,0 +1,5 @@
+defimpl RIGHT do
+ # Docs
+ # Docs
+ def foo(ChangeMe), do: :ok
+end
diff --git a/t/t4018/fortran-block-data b/t/t4018/fortran-block-data
new file mode 100644
index 0000000..63d4e21
--- /dev/null
+++ b/t/t4018/fortran-block-data
@@ -0,0 +1,5 @@
+ BLOCK DATA RIGHT
+
+ COMMON /B/ C, ChangeMe
+ DATA C, ChangeMe / 2.0, 6.0 /
+ END
diff --git a/t/t4018/fortran-comment b/t/t4018/fortran-comment
new file mode 100644
index 0000000..7b10d17
--- /dev/null
+++ b/t/t4018/fortran-comment
@@ -0,0 +1,13 @@
+ module a
+
+ contains
+
+ ! subroutine wrong
+ subroutine RIGHT
+ ! subroutine wrong
+
+ real ChangeMe
+
+ end subroutine RIGHT
+
+ end module a
diff --git a/t/t4018/fortran-comment-keyword b/t/t4018/fortran-comment-keyword
new file mode 100644
index 0000000..e9206a5
--- /dev/null
+++ b/t/t4018/fortran-comment-keyword
@@ -0,0 +1,14 @@
+ module a
+
+ contains
+
+ subroutine RIGHT (funcA, funcB)
+
+ real funcA ! grid function a
+ real funcB ! grid function b
+
+ real ChangeMe
+
+ end subroutine RIGHT
+
+ end module a
diff --git a/t/t4018/fortran-comment-legacy b/t/t4018/fortran-comment-legacy
new file mode 100644
index 0000000..53cd062
--- /dev/null
+++ b/t/t4018/fortran-comment-legacy
@@ -0,0 +1,13 @@
+ module a
+
+ contains
+
+C subroutine wrong
+ subroutine RIGHT
+C subroutine wrong
+
+ real ChangeMe
+
+ end subroutine RIGHT
+
+ end module a
diff --git a/t/t4018/fortran-comment-legacy-star b/t/t4018/fortran-comment-legacy-star
new file mode 100644
index 0000000..2cbcdc3
--- /dev/null
+++ b/t/t4018/fortran-comment-legacy-star
@@ -0,0 +1,13 @@
+ module a
+
+ contains
+
+* subroutine wrong
+ subroutine RIGHT
+* subroutine wrong
+
+ real ChangeMe
+
+ end subroutine RIGHT
+
+ end module a
diff --git a/t/t4018/fortran-external-function b/t/t4018/fortran-external-function
new file mode 100644
index 0000000..5a2d85d
--- /dev/null
+++ b/t/t4018/fortran-external-function
@@ -0,0 +1,9 @@
+function RIGHT(a, b) result(c)
+
+integer, intent(in) :: ChangeMe
+integer, intent(in) :: b
+integer, intent(out) :: c
+
+c = a+b
+
+end function RIGHT
diff --git a/t/t4018/fortran-external-subroutine b/t/t4018/fortran-external-subroutine
new file mode 100644
index 0000000..4ce85fe
--- /dev/null
+++ b/t/t4018/fortran-external-subroutine
@@ -0,0 +1,5 @@
+subroutine RIGHT
+
+real ChangeMe
+
+end subroutine RIGHT
diff --git a/t/t4018/fortran-module b/t/t4018/fortran-module
new file mode 100644
index 0000000..c4b737d
--- /dev/null
+++ b/t/t4018/fortran-module
@@ -0,0 +1,5 @@
+module RIGHT
+
+use ChangeMe
+
+end module RIGHT
diff --git a/t/t4018/fortran-module-procedure b/t/t4018/fortran-module-procedure
new file mode 100644
index 0000000..1ce6d85
--- /dev/null
+++ b/t/t4018/fortran-module-procedure
@@ -0,0 +1,13 @@
+ module RIGHT
+
+ implicit none
+ private
+
+ interface letters ! generic interface
+ module procedure aaaa, &
+ bbbb, &
+ ChangeMe, &
+ dddd
+ end interface
+
+end module RIGHT
diff --git a/t/t4018/fortran-program b/t/t4018/fortran-program
new file mode 100644
index 0000000..4616895
--- /dev/null
+++ b/t/t4018/fortran-program
@@ -0,0 +1,5 @@
+program RIGHT
+
+call ChangeMe
+
+end program RIGHT
diff --git a/t/t4018/fountain-scene b/t/t4018/fountain-scene
new file mode 100644
index 0000000..6b3257d
--- /dev/null
+++ b/t/t4018/fountain-scene
@@ -0,0 +1,4 @@
+EXT. STREET RIGHT OUTSIDE - DAY
+
+CHARACTER
+You didn't say the magic phrase, "ChangeMe".
diff --git a/t/t4018/golang-complex-function b/t/t4018/golang-complex-function
new file mode 100644
index 0000000..e057dce
--- /dev/null
+++ b/t/t4018/golang-complex-function
@@ -0,0 +1,8 @@
+type Test struct {
+ a Type
+}
+
+func (t *Test) RIGHT(a Type) (Type, error) {
+ t.a = a
+ return ChangeMe, nil
+}
diff --git a/t/t4018/golang-func b/t/t4018/golang-func
new file mode 100644
index 0000000..8e9c9ac
--- /dev/null
+++ b/t/t4018/golang-func
@@ -0,0 +1,4 @@
+func RIGHT() {
+ a := 5
+ b := ChangeMe
+}
diff --git a/t/t4018/golang-interface b/t/t4018/golang-interface
new file mode 100644
index 0000000..553bede
--- /dev/null
+++ b/t/t4018/golang-interface
@@ -0,0 +1,4 @@
+type RIGHT interface {
+ a() Type
+ b() ChangeMe
+}
diff --git a/t/t4018/golang-long-func b/t/t4018/golang-long-func
new file mode 100644
index 0000000..ac3a77b
--- /dev/null
+++ b/t/t4018/golang-long-func
@@ -0,0 +1,5 @@
+func RIGHT(aVeryVeryVeryLongVariableName AVeryVeryVeryLongType,
+ anotherLongVariableName AnotherLongType) {
+ a := 5
+ b := ChangeMe
+}
diff --git a/t/t4018/golang-struct b/t/t4018/golang-struct
new file mode 100644
index 0000000..5deda77
--- /dev/null
+++ b/t/t4018/golang-struct
@@ -0,0 +1,4 @@
+type RIGHT struct {
+ a Type
+ b ChangeMe
+}
diff --git a/t/t4018/java-class-brace-on-separate-line b/t/t4018/java-class-brace-on-separate-line
new file mode 100644
index 0000000..8795acd
--- /dev/null
+++ b/t/t4018/java-class-brace-on-separate-line
@@ -0,0 +1,6 @@
+class RIGHT
+{
+ static int ONE;
+ static int TWO;
+ static int ChangeMe;
+}
diff --git a/t/t4018/java-class-member-function b/t/t4018/java-class-member-function
new file mode 100644
index 0000000..3b95f68
--- /dev/null
+++ b/t/t4018/java-class-member-function
@@ -0,0 +1,12 @@
+public class Beer
+{
+ int special;
+ public static void main(String RIGHT[])
+ {
+ someMethodCall();
+ someOtherMethod("17")
+ .doThat();
+ // Whatever
+ System.out.print("ChangeMe");
+ }
+}
diff --git a/t/t4018/java-class-space-before-type-parameters b/t/t4018/java-class-space-before-type-parameters
new file mode 100644
index 0000000..0bdef1d
--- /dev/null
+++ b/t/t4018/java-class-space-before-type-parameters
@@ -0,0 +1,6 @@
+class RIGHT <TYPE, PARAMS, AFTER, SPACE> {
+ static int ONE;
+ static int TWO;
+ static int THREE;
+ private A ChangeMe;
+}
diff --git a/t/t4018/java-class-type-parameters b/t/t4018/java-class-type-parameters
new file mode 100644
index 0000000..579aa7a
--- /dev/null
+++ b/t/t4018/java-class-type-parameters
@@ -0,0 +1,6 @@
+class RIGHT<A, B> {
+ static int ONE;
+ static int TWO;
+ static int THREE;
+ private A ChangeMe;
+}
diff --git a/t/t4018/java-class-type-parameters-implements b/t/t4018/java-class-type-parameters-implements
new file mode 100644
index 0000000..b8038b1
--- /dev/null
+++ b/t/t4018/java-class-type-parameters-implements
@@ -0,0 +1,6 @@
+class RIGHT<A, B> implements List<A> {
+ static int ONE;
+ static int TWO;
+ static int THREE;
+ private A ChangeMe;
+}
diff --git a/t/t4018/java-enum-constant b/t/t4018/java-enum-constant
new file mode 100644
index 0000000..a1931c8
--- /dev/null
+++ b/t/t4018/java-enum-constant
@@ -0,0 +1,6 @@
+private enum RIGHT {
+ ONE,
+ TWO,
+ THREE,
+ ChangeMe
+}
diff --git a/t/t4018/java-interface-type-parameters b/t/t4018/java-interface-type-parameters
new file mode 100644
index 0000000..a4baa1a
--- /dev/null
+++ b/t/t4018/java-interface-type-parameters
@@ -0,0 +1,6 @@
+interface RIGHT<A, B> {
+ static int ONE;
+ static int TWO;
+ static int THREE;
+ public B foo(A ChangeMe);
+}
diff --git a/t/t4018/java-interface-type-parameters-extends b/t/t4018/java-interface-type-parameters-extends
new file mode 100644
index 0000000..31d7fb3
--- /dev/null
+++ b/t/t4018/java-interface-type-parameters-extends
@@ -0,0 +1,6 @@
+interface RIGHT<A, B> extends Function<A, B> {
+ static int ONE;
+ static int TWO;
+ static int THREE;
+ public B foo(A ChangeMe);
+}
diff --git a/t/t4018/java-method-return-generic-bounded b/t/t4018/java-method-return-generic-bounded
new file mode 100644
index 0000000..66dd78c
--- /dev/null
+++ b/t/t4018/java-method-return-generic-bounded
@@ -0,0 +1,9 @@
+class MyExample {
+ public <T extends Bar & Foo<T>, R> Map<T, R[]> foo(String[] RIGHT) {
+ someMethodCall();
+ someOtherMethod()
+ .doThat();
+ // Whatever...
+ return (List<T>) Arrays.asList("ChangeMe");
+ }
+}
diff --git a/t/t4018/java-method-return-generic-wildcard b/t/t4018/java-method-return-generic-wildcard
new file mode 100644
index 0000000..96e9e5f
--- /dev/null
+++ b/t/t4018/java-method-return-generic-wildcard
@@ -0,0 +1,9 @@
+class MyExample {
+ public List<? extends Comparable> foo(String[] RIGHT) {
+ someMethodCall();
+ someOtherMethod()
+ .doThat();
+ // Whatever...
+ return Arrays.asList("ChangeMe");
+ }
+}
diff --git a/t/t4018/java-nested-field b/t/t4018/java-nested-field
new file mode 100644
index 0000000..d92d3ec
--- /dev/null
+++ b/t/t4018/java-nested-field
@@ -0,0 +1,6 @@
+class MyExample {
+ private static class RIGHT {
+ // change an inner class field
+ String inner = "ChangeMe";
+ }
+}
diff --git a/t/t4018/java-non-sealed b/t/t4018/java-non-sealed
new file mode 100644
index 0000000..069087c
--- /dev/null
+++ b/t/t4018/java-non-sealed
@@ -0,0 +1,8 @@
+public abstract sealed class SealedClass {
+ public static non-sealed class RIGHT extends SealedClass {
+ static int ONE;
+ static int TWO;
+ static int THREE;
+ private int ChangeMe;
+ }
+}
diff --git a/t/t4018/java-record b/t/t4018/java-record
new file mode 100644
index 0000000..97aa819
--- /dev/null
+++ b/t/t4018/java-record
@@ -0,0 +1,6 @@
+public record RIGHT(int comp1, double comp2, String comp3) {
+ static int ONE;
+ static int TWO;
+ static int THREE;
+ static int ChangeMe;
+}
diff --git a/t/t4018/java-record-space-before-components b/t/t4018/java-record-space-before-components
new file mode 100644
index 0000000..9827f22
--- /dev/null
+++ b/t/t4018/java-record-space-before-components
@@ -0,0 +1,6 @@
+public record RIGHT (String components, String after, String space) {
+ static int ONE;
+ static int TWO;
+ static int THREE;
+ static int ChangeMe;
+}
diff --git a/t/t4018/java-record-type-parameters b/t/t4018/java-record-type-parameters
new file mode 100644
index 0000000..f62a035
--- /dev/null
+++ b/t/t4018/java-record-type-parameters
@@ -0,0 +1,6 @@
+public record RIGHT<A, N extends Number>(A comp1, N comp2, int comp3) {
+ static int ONE;
+ static int TWO;
+ static int THREE;
+ static int ChangeMe;
+}
diff --git a/t/t4018/java-sealed b/t/t4018/java-sealed
new file mode 100644
index 0000000..785fbc6
--- /dev/null
+++ b/t/t4018/java-sealed
@@ -0,0 +1,7 @@
+public abstract sealed class Sealed { // RIGHT
+ static int ONE;
+ static int TWO;
+ static int THREE;
+ public final class ChangeMe extends Sealed {
+ }
+}
diff --git a/t/t4018/java-sealed-permits b/t/t4018/java-sealed-permits
new file mode 100644
index 0000000..18dd489
--- /dev/null
+++ b/t/t4018/java-sealed-permits
@@ -0,0 +1,6 @@
+public abstract sealed class RIGHT permits PermittedA, PermittedB {
+ static int ONE;
+ static int TWO;
+ static int THREE;
+ private int ChangeMe;
+}
diff --git a/t/t4018/java-sealed-type-parameters b/t/t4018/java-sealed-type-parameters
new file mode 100644
index 0000000..e6530c4
--- /dev/null
+++ b/t/t4018/java-sealed-type-parameters
@@ -0,0 +1,6 @@
+public abstract sealed class RIGHT<A, B> {
+ static int ONE;
+ static int TWO;
+ static int THREE;
+ private int ChangeMe;
+}
diff --git a/t/t4018/java-sealed-type-parameters-implements-permits b/t/t4018/java-sealed-type-parameters-implements-permits
new file mode 100644
index 0000000..bd6e6d3
--- /dev/null
+++ b/t/t4018/java-sealed-type-parameters-implements-permits
@@ -0,0 +1,6 @@
+public abstract sealed class RIGHT<A, B> implements List<A> permits PermittedA, PermittedB {
+ static int ONE;
+ static int TWO;
+ static int THREE;
+ private int ChangeMe;
+}
diff --git a/t/t4018/java-sealed-type-parameters-permits b/t/t4018/java-sealed-type-parameters-permits
new file mode 100644
index 0000000..25a0da6
--- /dev/null
+++ b/t/t4018/java-sealed-type-parameters-permits
@@ -0,0 +1,6 @@
+public abstract sealed class RIGHT<A, B> permits PermittedA, PermittedB {
+ static int ONE;
+ static int TWO;
+ static int THREE;
+ private int ChangeMe;
+}
diff --git a/t/t4018/kotlin-class b/t/t4018/kotlin-class
new file mode 100644
index 0000000..bb864f2
--- /dev/null
+++ b/t/t4018/kotlin-class
@@ -0,0 +1,5 @@
+class RIGHT {
+ //comment
+ //comment
+ return ChangeMe
+}
diff --git a/t/t4018/kotlin-enum-class b/t/t4018/kotlin-enum-class
new file mode 100644
index 0000000..8885f90
--- /dev/null
+++ b/t/t4018/kotlin-enum-class
@@ -0,0 +1,5 @@
+enum class RIGHT{
+ // Left
+ // a comment
+ ChangeMe
+}
diff --git a/t/t4018/kotlin-fun b/t/t4018/kotlin-fun
new file mode 100644
index 0000000..2a60280
--- /dev/null
+++ b/t/t4018/kotlin-fun
@@ -0,0 +1,5 @@
+fun RIGHT(){
+ //a comment
+ //b comment
+ return ChangeMe()
+}
diff --git a/t/t4018/kotlin-inheritace-class b/t/t4018/kotlin-inheritace-class
new file mode 100644
index 0000000..77376c1
--- /dev/null
+++ b/t/t4018/kotlin-inheritace-class
@@ -0,0 +1,5 @@
+open class RIGHT{
+ // a comment
+ // b comment
+ // ChangeMe
+}
diff --git a/t/t4018/kotlin-inline-class b/t/t4018/kotlin-inline-class
new file mode 100644
index 0000000..7bf46dd
--- /dev/null
+++ b/t/t4018/kotlin-inline-class
@@ -0,0 +1,5 @@
+value class RIGHT(Args){
+ // a comment
+ // b comment
+ ChangeMe
+}
diff --git a/t/t4018/kotlin-interface b/t/t4018/kotlin-interface
new file mode 100644
index 0000000..f686ba7
--- /dev/null
+++ b/t/t4018/kotlin-interface
@@ -0,0 +1,5 @@
+interface RIGHT{
+ //another comment
+ //another comment
+ //ChangeMe
+}
diff --git a/t/t4018/kotlin-nested-fun b/t/t4018/kotlin-nested-fun
new file mode 100644
index 0000000..1218685
--- /dev/null
+++ b/t/t4018/kotlin-nested-fun
@@ -0,0 +1,9 @@
+class LEFT{
+ class CENTER{
+ fun RIGHT( a:Int){
+ //comment
+ //comment
+ ChangeMe
+ }
+ }
+}
diff --git a/t/t4018/kotlin-public-class b/t/t4018/kotlin-public-class
new file mode 100644
index 0000000..9433fcc
--- /dev/null
+++ b/t/t4018/kotlin-public-class
@@ -0,0 +1,5 @@
+public class RIGHT{
+ //comment1
+ //comment2
+ ChangeMe
+}
diff --git a/t/t4018/kotlin-sealed-class b/t/t4018/kotlin-sealed-class
new file mode 100644
index 0000000..0efa4a4
--- /dev/null
+++ b/t/t4018/kotlin-sealed-class
@@ -0,0 +1,5 @@
+sealed class RIGHT {
+ // a comment
+ // b comment
+ ChangeMe
+}
diff --git a/t/t4018/markdown-heading-indented b/t/t4018/markdown-heading-indented
new file mode 100644
index 0000000..1991c2b
--- /dev/null
+++ b/t/t4018/markdown-heading-indented
@@ -0,0 +1,6 @@
+Indented headings are allowed, as long as the indent is no more than 3 spaces.
+
+ ### RIGHT
+
+- something
+- ChangeMe
diff --git a/t/t4018/markdown-heading-non-headings b/t/t4018/markdown-heading-non-headings
new file mode 100644
index 0000000..c479c1a
--- /dev/null
+++ b/t/t4018/markdown-heading-non-headings
@@ -0,0 +1,17 @@
+Headings can be right next to other lines of the file:
+# RIGHT
+Indents of four or more spaces make a code block:
+
+ # code comment, not heading
+
+If there's no space after the final hash, it's not a heading:
+
+#hashtag
+
+Sequences of more than 6 hashes don't make a heading:
+
+####### over-enthusiastic heading
+
+So the detected heading should be right up at the start of this file.
+
+ChangeMe
diff --git a/t/t4018/matlab-class-definition b/t/t4018/matlab-class-definition
new file mode 100644
index 0000000..84daedf
--- /dev/null
+++ b/t/t4018/matlab-class-definition
@@ -0,0 +1,5 @@
+classdef RIGHT
+ properties
+ ChangeMe
+ end
+end
diff --git a/t/t4018/matlab-function b/t/t4018/matlab-function
new file mode 100644
index 0000000..897a9b1
--- /dev/null
+++ b/t/t4018/matlab-function
@@ -0,0 +1,4 @@
+function y = RIGHT()
+x = 5;
+y = ChangeMe + x;
+end
diff --git a/t/t4018/matlab-octave-section-1 b/t/t4018/matlab-octave-section-1
new file mode 100644
index 0000000..3bb6c46
--- /dev/null
+++ b/t/t4018/matlab-octave-section-1
@@ -0,0 +1,3 @@
+%%% RIGHT section
+# this is octave script
+ChangeMe = 1;
diff --git a/t/t4018/matlab-octave-section-2 b/t/t4018/matlab-octave-section-2
new file mode 100644
index 0000000..ab2980f
--- /dev/null
+++ b/t/t4018/matlab-octave-section-2
@@ -0,0 +1,3 @@
+## RIGHT section
+# this is octave script
+ChangeMe = 1;
diff --git a/t/t4018/matlab-section b/t/t4018/matlab-section
new file mode 100644
index 0000000..5ea59a5
--- /dev/null
+++ b/t/t4018/matlab-section
@@ -0,0 +1,3 @@
+%% RIGHT section
+% this is understood by both matlab and octave
+ChangeMe = 1;
diff --git a/t/t4018/perl-skip-end-of-heredoc b/t/t4018/perl-skip-end-of-heredoc
new file mode 100644
index 0000000..c22d39b
--- /dev/null
+++ b/t/t4018/perl-skip-end-of-heredoc
@@ -0,0 +1,8 @@
+sub RIGHTwithheredocument {
+ print <<"EOF"
+decoy here-doc
+EOF
+ # some lines of context
+ # to pad it out
+ print "ChangeMe\n";
+}
diff --git a/t/t4018/perl-skip-forward-decl b/t/t4018/perl-skip-forward-decl
new file mode 100644
index 0000000..a98cb8b
--- /dev/null
+++ b/t/t4018/perl-skip-forward-decl
@@ -0,0 +1,10 @@
+package RIGHT;
+
+use strict;
+use warnings;
+use parent qw(Exporter);
+our @EXPORT_OK = qw(round finalround);
+
+sub other; # forward declaration
+
+# ChangeMe
diff --git a/t/t4018/perl-skip-sub-in-pod b/t/t4018/perl-skip-sub-in-pod
new file mode 100644
index 0000000..e39f024
--- /dev/null
+++ b/t/t4018/perl-skip-sub-in-pod
@@ -0,0 +1,18 @@
+=head1 NAME
+
+Beer - subroutine to output fragment of a drinking song
+
+=head1 SYNOPSIS_RIGHT
+
+ use Beer qw(round finalround);
+
+ sub song {
+ for (my $i = 99; $i > 0; $i--) {
+ round $i;
+ }
+ finalround;
+ }
+
+ ChangeMe;
+
+=cut
diff --git a/t/t4018/perl-sub-definition b/t/t4018/perl-sub-definition
new file mode 100644
index 0000000..a507d1f
--- /dev/null
+++ b/t/t4018/perl-sub-definition
@@ -0,0 +1,4 @@
+sub RIGHT {
+ my ($n) = @_;
+ print "ChangeMe";
+}
diff --git a/t/t4018/perl-sub-definition-kr-brace b/t/t4018/perl-sub-definition-kr-brace
new file mode 100644
index 0000000..330b3df
--- /dev/null
+++ b/t/t4018/perl-sub-definition-kr-brace
@@ -0,0 +1,4 @@
+sub RIGHT
+{
+ print "ChangeMe\n";
+}
diff --git a/t/t4018/php-abstract-class b/t/t4018/php-abstract-class
new file mode 100644
index 0000000..5213e12
--- /dev/null
+++ b/t/t4018/php-abstract-class
@@ -0,0 +1,4 @@
+abstract class RIGHT
+{
+ const FOO = 'ChangeMe';
+}
diff --git a/t/t4018/php-abstract-method b/t/t4018/php-abstract-method
new file mode 100644
index 0000000..ce215df
--- /dev/null
+++ b/t/t4018/php-abstract-method
@@ -0,0 +1,7 @@
+abstract class Klass
+{
+ abstract public function RIGHT(): ?string
+ {
+ return 'ChangeMe';
+ }
+}
diff --git a/t/t4018/php-class b/t/t4018/php-class
new file mode 100644
index 0000000..7785b63
--- /dev/null
+++ b/t/t4018/php-class
@@ -0,0 +1,4 @@
+class RIGHT
+{
+ const FOO = 'ChangeMe';
+}
diff --git a/t/t4018/php-enum b/t/t4018/php-enum
new file mode 100644
index 0000000..91a69c1
--- /dev/null
+++ b/t/t4018/php-enum
@@ -0,0 +1,4 @@
+enum RIGHT: string
+{
+ case Foo = 'ChangeMe';
+}
diff --git a/t/t4018/php-final-class b/t/t4018/php-final-class
new file mode 100644
index 0000000..69f5710
--- /dev/null
+++ b/t/t4018/php-final-class
@@ -0,0 +1,4 @@
+final class RIGHT
+{
+ const FOO = 'ChangeMe';
+}
diff --git a/t/t4018/php-final-method b/t/t4018/php-final-method
new file mode 100644
index 0000000..537fb8a
--- /dev/null
+++ b/t/t4018/php-final-method
@@ -0,0 +1,7 @@
+class Klass
+{
+ final public function RIGHT(): string
+ {
+ return 'ChangeMe';
+ }
+}
diff --git a/t/t4018/php-function b/t/t4018/php-function
new file mode 100644
index 0000000..35717c5
--- /dev/null
+++ b/t/t4018/php-function
@@ -0,0 +1,4 @@
+function RIGHT()
+{
+ return 'ChangeMe';
+}
diff --git a/t/t4018/php-interface b/t/t4018/php-interface
new file mode 100644
index 0000000..86b49ad
--- /dev/null
+++ b/t/t4018/php-interface
@@ -0,0 +1,4 @@
+interface RIGHT
+{
+ public function foo($ChangeMe);
+}
diff --git a/t/t4018/php-method b/t/t4018/php-method
new file mode 100644
index 0000000..03af1a6
--- /dev/null
+++ b/t/t4018/php-method
@@ -0,0 +1,7 @@
+class Klass
+{
+ public static function RIGHT()
+ {
+ return 'ChangeMe';
+ }
+}
diff --git a/t/t4018/php-trait b/t/t4018/php-trait
new file mode 100644
index 0000000..65b8c82
--- /dev/null
+++ b/t/t4018/php-trait
@@ -0,0 +1,7 @@
+trait RIGHT
+{
+ public function foo($ChangeMe)
+ {
+ return 'foo';
+ }
+}
diff --git a/t/t4018/python-async-def b/t/t4018/python-async-def
new file mode 100644
index 0000000..87640e0
--- /dev/null
+++ b/t/t4018/python-async-def
@@ -0,0 +1,4 @@
+async def RIGHT(pi: int = 3.14):
+ while True:
+ break
+ return ChangeMe()
diff --git a/t/t4018/python-class b/t/t4018/python-class
new file mode 100644
index 0000000..ba9e741
--- /dev/null
+++ b/t/t4018/python-class
@@ -0,0 +1,4 @@
+class RIGHT(int, str):
+ # comment
+ # another comment
+ # ChangeMe
diff --git a/t/t4018/python-def b/t/t4018/python-def
new file mode 100644
index 0000000..e50b31b
--- /dev/null
+++ b/t/t4018/python-def
@@ -0,0 +1,4 @@
+def RIGHT(pi: int = 3.14):
+ while True:
+ break
+ return ChangeMe()
diff --git a/t/t4018/python-indented-async-def b/t/t4018/python-indented-async-def
new file mode 100644
index 0000000..f5d0325
--- /dev/null
+++ b/t/t4018/python-indented-async-def
@@ -0,0 +1,7 @@
+class Foo:
+ async def RIGHT(self, x: int):
+ return [
+ 1,
+ 2,
+ ChangeMe,
+ ]
diff --git a/t/t4018/python-indented-class b/t/t4018/python-indented-class
new file mode 100644
index 0000000..19b4f35
--- /dev/null
+++ b/t/t4018/python-indented-class
@@ -0,0 +1,5 @@
+if TYPE_CHECKING:
+ class RIGHT:
+ # comment
+ # another comment
+ # ChangeMe
diff --git a/t/t4018/python-indented-def b/t/t4018/python-indented-def
new file mode 100644
index 0000000..208fbad
--- /dev/null
+++ b/t/t4018/python-indented-def
@@ -0,0 +1,7 @@
+class Foo:
+ def RIGHT(self, x: int):
+ return [
+ 1,
+ 2,
+ ChangeMe,
+ ]
diff --git a/t/t4018/rust-fn b/t/t4018/rust-fn
new file mode 100644
index 0000000..cbe0215
--- /dev/null
+++ b/t/t4018/rust-fn
@@ -0,0 +1,5 @@
+pub(self) fn RIGHT<T>(x: &[T]) where T: Debug {
+ let _ = x;
+ // a comment
+ let a = ChangeMe;
+}
diff --git a/t/t4018/rust-impl b/t/t4018/rust-impl
new file mode 100644
index 0000000..09df3cd
--- /dev/null
+++ b/t/t4018/rust-impl
@@ -0,0 +1,5 @@
+impl<'a, T: AsRef<[u8]>> std::RIGHT for Git<'a> {
+
+ pub fn ChangeMe(&self) -> () {
+ }
+}
diff --git a/t/t4018/rust-macro-rules b/t/t4018/rust-macro-rules
new file mode 100644
index 0000000..ec610c5
--- /dev/null
+++ b/t/t4018/rust-macro-rules
@@ -0,0 +1,6 @@
+macro_rules! RIGHT {
+ () => {
+ // a comment
+ let x = ChangeMe;
+ };
+}
diff --git a/t/t4018/rust-struct b/t/t4018/rust-struct
new file mode 100644
index 0000000..76aff1c
--- /dev/null
+++ b/t/t4018/rust-struct
@@ -0,0 +1,5 @@
+#[derive(Debug)]
+pub(super) struct RIGHT<'a> {
+ name: &'a str,
+ age: ChangeMe,
+}
diff --git a/t/t4018/rust-trait b/t/t4018/rust-trait
new file mode 100644
index 0000000..ea397f0
--- /dev/null
+++ b/t/t4018/rust-trait
@@ -0,0 +1,5 @@
+unsafe trait RIGHT<T> {
+ fn len(&self) -> u32;
+ fn ChangeMe(&self, n: u32) -> T;
+ fn iter<F>(&self, f: F) where F: Fn(T);
+}
diff --git a/t/t4018/scheme-class b/t/t4018/scheme-class
new file mode 100644
index 0000000..e5e07b4
--- /dev/null
+++ b/t/t4018/scheme-class
@@ -0,0 +1,7 @@
+(define book-class%
+ (class* () object% RIGHT
+ (field (pages 5))
+ (field (ChangeMe 5))
+ (define/public (letters)
+ (* pages 500))
+ (super-new)))
diff --git a/t/t4018/scheme-def b/t/t4018/scheme-def
new file mode 100644
index 0000000..1e2673d
--- /dev/null
+++ b/t/t4018/scheme-def
@@ -0,0 +1,4 @@
+(def (some-func x y z) RIGHT
+ (let ((a x)
+ (b y))
+ (ChangeMe a b)))
diff --git a/t/t4018/scheme-def-variant b/t/t4018/scheme-def-variant
new file mode 100644
index 0000000..d857a61
--- /dev/null
+++ b/t/t4018/scheme-def-variant
@@ -0,0 +1,4 @@
+(defmethod {print point} RIGHT
+ (lambda (self)
+ (with ((point x y) self)
+ (printf "{ChangeMe x:~a y:~a}~n" x y))))
diff --git a/t/t4018/scheme-define-slash-public b/t/t4018/scheme-define-slash-public
new file mode 100644
index 0000000..39a93a1
--- /dev/null
+++ b/t/t4018/scheme-define-slash-public
@@ -0,0 +1,7 @@
+(define bar-class%
+ (class object%
+ (field (info 5))
+ (define/public (foo) RIGHT
+ (+ info 42)
+ (* info ChangeMe))
+ (super-new)))
diff --git a/t/t4018/scheme-define-syntax b/t/t4018/scheme-define-syntax
new file mode 100644
index 0000000..7d5e99e
--- /dev/null
+++ b/t/t4018/scheme-define-syntax
@@ -0,0 +1,8 @@
+(define-syntax define-test-suite RIGHT
+ (syntax-rules ()
+ ((_ suite-name (name test) ChangeMe ...)
+ (define suite-name
+ (let ((tests
+ `((name . ,test) ...)))
+ (lambda ()
+ (run-suite 'suite-name tests)))))))
diff --git a/t/t4018/scheme-define-variant b/t/t4018/scheme-define-variant
new file mode 100644
index 0000000..9117088
--- /dev/null
+++ b/t/t4018/scheme-define-variant
@@ -0,0 +1,4 @@
+(define* (some-func x y z) RIGHT
+ (let ((a x)
+ (b y))
+ (ChangeMe a b)))
diff --git a/t/t4018/scheme-library b/t/t4018/scheme-library
new file mode 100644
index 0000000..82ea3df
--- /dev/null
+++ b/t/t4018/scheme-library
@@ -0,0 +1,11 @@
+(library (my-helpers id-stuff) RIGHT
+ (export find-dup)
+ (import (ChangeMe))
+ (define (find-dup l)
+ (and (pair? l)
+ (let loop ((rest (cdr l)))
+ (cond
+ [(null? rest) (find-dup (cdr l))]
+ [(bound-identifier=? (car l) (car rest))
+ (car rest)]
+ [else (loop (cdr rest))])))))
diff --git a/t/t4018/scheme-local-define b/t/t4018/scheme-local-define
new file mode 100644
index 0000000..bc6d8ae
--- /dev/null
+++ b/t/t4018/scheme-local-define
@@ -0,0 +1,4 @@
+(define (higher-order)
+ (define local-function RIGHT
+ (lambda (x)
+ (car "this is" "ChangeMe"))))
diff --git a/t/t4018/scheme-module b/t/t4018/scheme-module
new file mode 100644
index 0000000..edfae0e
--- /dev/null
+++ b/t/t4018/scheme-module
@@ -0,0 +1,6 @@
+(module A RIGHT
+ (export with-display-exception)
+ (extern (display-exception display-exception ChangeMe))
+ (def (with-display-exception thunk)
+ (with-catch (lambda (e) (display-exception e (current-error-port)) e)
+ thunk)))
diff --git a/t/t4018/scheme-top-level-define b/t/t4018/scheme-top-level-define
new file mode 100644
index 0000000..624743c
--- /dev/null
+++ b/t/t4018/scheme-top-level-define
@@ -0,0 +1,4 @@
+(define (some-func x y z) RIGHT
+ (let ((a x)
+ (b y))
+ (ChangeMe a b)))
diff --git a/t/t4018/scheme-user-defined-define b/t/t4018/scheme-user-defined-define
new file mode 100644
index 0000000..35fe7cc
--- /dev/null
+++ b/t/t4018/scheme-user-defined-define
@@ -0,0 +1,6 @@
+(define-test-suite record\ case-tests RIGHT
+ (record-case-1 (lambda (fail)
+ (let ((a (make-foo 1 2)))
+ (record-case a
+ ((bar x) (ChangeMe))
+ ((foo a b) (+ a b)))))))
diff --git a/t/t4019-diff-wserror.sh b/t/t4019-diff-wserror.sh
new file mode 100755
index 0000000..d2b3109
--- /dev/null
+++ b/t/t4019-diff-wserror.sh
@@ -0,0 +1,298 @@
+#!/bin/sh
+
+test_description='diff whitespace error detection'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+
+ git config diff.color.whitespace "blue reverse" &&
+ >F &&
+ git add F &&
+ echo " Eight SP indent" >>F &&
+ echo " HT and SP indent" >>F &&
+ echo "With trailing SP " >>F &&
+ echo "Carriage ReturnQ" | tr Q "\015" >>F &&
+ echo "No problem" >>F &&
+ echo >>F
+
+'
+
+blue_grep='7;34m' ;# ESC [ 7 ; 3 4 m
+
+printf "\033[%s" "$blue_grep" >check-grep
+if (grep "$blue_grep" <check-grep | grep "$blue_grep") >/dev/null 2>&1
+then
+ grep_a=grep
+elif (grep -a "$blue_grep" <check-grep | grep -a "$blue_grep") >/dev/null 2>&1
+then
+ grep_a='grep -a'
+else
+ grep_a=grep ;# expected to fail...
+fi
+rm -f check-grep
+
+prepare_output () {
+ git diff --color >output
+ $grep_a "$blue_grep" output >error
+ $grep_a -v "$blue_grep" output >normal
+ return 0
+}
+
+test_expect_success default '
+
+ prepare_output &&
+
+ grep Eight normal >/dev/null &&
+ grep HT error >/dev/null &&
+ grep With error >/dev/null &&
+ grep Return error >/dev/null &&
+ grep No normal >/dev/null
+
+'
+
+test_expect_success 'default (attribute)' '
+
+ test_might_fail git config --unset core.whitespace &&
+ echo "F whitespace" >.gitattributes &&
+ prepare_output &&
+
+ grep Eight error >/dev/null &&
+ grep HT error >/dev/null &&
+ grep With error >/dev/null &&
+ grep Return error >/dev/null &&
+ grep No normal >/dev/null
+
+'
+
+test_expect_success 'default, tabwidth=10 (attribute)' '
+
+ git config core.whitespace "tabwidth=10" &&
+ echo "F whitespace" >.gitattributes &&
+ prepare_output &&
+
+ grep Eight normal >/dev/null &&
+ grep HT error >/dev/null &&
+ grep With error >/dev/null &&
+ grep Return error >/dev/null &&
+ grep No normal >/dev/null
+
+'
+
+test_expect_success 'no check (attribute)' '
+
+ test_might_fail git config --unset core.whitespace &&
+ echo "F -whitespace" >.gitattributes &&
+ prepare_output &&
+
+ grep Eight normal >/dev/null &&
+ grep HT normal >/dev/null &&
+ grep With normal >/dev/null &&
+ grep Return normal >/dev/null &&
+ grep No normal >/dev/null
+
+'
+
+test_expect_success 'no check, tabwidth=10 (attribute), must be irrelevant' '
+
+ git config core.whitespace "tabwidth=10" &&
+ echo "F -whitespace" >.gitattributes &&
+ prepare_output &&
+
+ grep Eight normal >/dev/null &&
+ grep HT normal >/dev/null &&
+ grep With normal >/dev/null &&
+ grep Return normal >/dev/null &&
+ grep No normal >/dev/null
+
+'
+
+test_expect_success 'without -trail' '
+
+ rm -f .gitattributes &&
+ git config core.whitespace -trail &&
+ prepare_output &&
+
+ grep Eight normal >/dev/null &&
+ grep HT error >/dev/null &&
+ grep With normal >/dev/null &&
+ grep Return normal >/dev/null &&
+ grep No normal >/dev/null
+
+'
+
+test_expect_success 'without -trail (attribute)' '
+
+ test_might_fail git config --unset core.whitespace &&
+ echo "F whitespace=-trail" >.gitattributes &&
+ prepare_output &&
+
+ grep Eight normal >/dev/null &&
+ grep HT error >/dev/null &&
+ grep With normal >/dev/null &&
+ grep Return normal >/dev/null &&
+ grep No normal >/dev/null
+
+'
+
+test_expect_success 'without -space' '
+
+ rm -f .gitattributes &&
+ git config core.whitespace -space &&
+ prepare_output &&
+
+ grep Eight normal >/dev/null &&
+ grep HT normal >/dev/null &&
+ grep With error >/dev/null &&
+ grep Return error >/dev/null &&
+ grep No normal >/dev/null
+
+'
+
+test_expect_success 'without -space (attribute)' '
+
+ test_might_fail git config --unset core.whitespace &&
+ echo "F whitespace=-space" >.gitattributes &&
+ prepare_output &&
+
+ grep Eight normal >/dev/null &&
+ grep HT normal >/dev/null &&
+ grep With error >/dev/null &&
+ grep Return error >/dev/null &&
+ grep No normal >/dev/null
+
+'
+
+test_expect_success 'with indent-non-tab only' '
+
+ rm -f .gitattributes &&
+ git config core.whitespace indent,-trailing,-space &&
+ prepare_output &&
+
+ grep Eight error >/dev/null &&
+ grep HT normal >/dev/null &&
+ grep With normal >/dev/null &&
+ grep Return normal >/dev/null &&
+ grep No normal >/dev/null
+
+'
+
+test_expect_success 'with indent-non-tab only (attribute)' '
+
+ test_might_fail git config --unset core.whitespace &&
+ echo "F whitespace=indent,-trailing,-space" >.gitattributes &&
+ prepare_output &&
+
+ grep Eight error >/dev/null &&
+ grep HT normal >/dev/null &&
+ grep With normal >/dev/null &&
+ grep Return normal >/dev/null &&
+ grep No normal >/dev/null
+
+'
+
+test_expect_success 'with indent-non-tab only, tabwidth=10' '
+
+ rm -f .gitattributes &&
+ git config core.whitespace indent,tabwidth=10,-trailing,-space &&
+ prepare_output &&
+
+ grep Eight normal >/dev/null &&
+ grep HT normal >/dev/null &&
+ grep With normal >/dev/null &&
+ grep Return normal >/dev/null &&
+ grep No normal >/dev/null
+
+'
+
+test_expect_success 'with indent-non-tab only, tabwidth=10 (attribute)' '
+
+ test_might_fail git config --unset core.whitespace &&
+ echo "F whitespace=indent,-trailing,-space,tabwidth=10" >.gitattributes &&
+ prepare_output &&
+
+ grep Eight normal >/dev/null &&
+ grep HT normal >/dev/null &&
+ grep With normal >/dev/null &&
+ grep Return normal >/dev/null &&
+ grep No normal >/dev/null
+
+'
+
+test_expect_success 'with cr-at-eol' '
+
+ rm -f .gitattributes &&
+ git config core.whitespace cr-at-eol &&
+ prepare_output &&
+
+ grep Eight normal >/dev/null &&
+ grep HT error >/dev/null &&
+ grep With error >/dev/null &&
+ grep Return normal >/dev/null &&
+ grep No normal >/dev/null
+
+'
+
+test_expect_success 'with cr-at-eol (attribute)' '
+
+ test_might_fail git config --unset core.whitespace &&
+ echo "F whitespace=trailing,cr-at-eol" >.gitattributes &&
+ prepare_output &&
+
+ grep Eight normal >/dev/null &&
+ grep HT error >/dev/null &&
+ grep With error >/dev/null &&
+ grep Return normal >/dev/null &&
+ grep No normal >/dev/null
+
+'
+
+test_expect_success 'trailing empty lines (1)' '
+
+ rm -f .gitattributes &&
+ test_must_fail git diff --check >output &&
+ grep "new blank line at" output &&
+ grep "trailing whitespace" output
+
+'
+
+test_expect_success 'trailing empty lines (2)' '
+
+ echo "F -whitespace" >.gitattributes &&
+ git diff --check >output &&
+ test_must_be_empty output
+
+'
+
+test_expect_success 'checkdiff shows correct line number for trailing blank lines' '
+
+ printf "a\nb\n" > G &&
+ git add G &&
+ printf "x\nx\nx\na\nb\nc\n\n" > G &&
+ [ "$(git diff --check -- G)" = "G:7: new blank line at EOF." ]
+
+'
+
+test_expect_success 'do not color trailing cr in context' '
+ test_might_fail git config --unset core.whitespace &&
+ rm -f .gitattributes &&
+ echo AAAQ | tr Q "\015" >G &&
+ git add G &&
+ echo BBBQ | tr Q "\015" >>G &&
+ git diff --color G | tr "\015" Q >output &&
+ grep "BBB.*${blue_grep}Q" output &&
+ grep "AAA.*\[mQ" output
+
+'
+
+test_expect_success 'color new trailing blank lines' '
+ test_write_lines a b "" "" >x &&
+ git add x &&
+ test_write_lines a "" "" "" c "" "" "" "" >x &&
+ git diff --color x >output &&
+ cnt=$($grep_a "${blue_grep}" output | wc -l) &&
+ test $cnt = 2
+'
+
+test_done
diff --git a/t/t4020-diff-external.sh b/t/t4020-diff-external.sh
new file mode 100755
index 0000000..c1ac09e
--- /dev/null
+++ b/t/t4020-diff-external.sh
@@ -0,0 +1,274 @@
+#!/bin/sh
+
+test_description='external diff interface test'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+
+ test_tick &&
+ echo initial >file &&
+ git add file &&
+ git commit -m initial &&
+
+ test_tick &&
+ echo second >file &&
+ before=$(git hash-object file) &&
+ before=$(git rev-parse --short $before) &&
+ git add file &&
+ git commit -m second &&
+
+ test_tick &&
+ echo third >file
+'
+
+test_expect_success 'GIT_EXTERNAL_DIFF environment' '
+ cat >expect <<-EOF &&
+ file $(git rev-parse --verify HEAD:file) 100644 file $(test_oid zero) 100644
+ EOF
+ GIT_EXTERNAL_DIFF=echo git diff >out &&
+ cut -d" " -f1,3- <out >actual &&
+ test_cmp expect actual
+
+'
+
+test_expect_success 'GIT_EXTERNAL_DIFF environment should apply only to diff' '
+ GIT_EXTERNAL_DIFF=echo git log -p -1 HEAD >out &&
+ grep "^diff --git a/file b/file" out
+
+'
+
+test_expect_success 'GIT_EXTERNAL_DIFF environment and --no-ext-diff' '
+ GIT_EXTERNAL_DIFF=echo git diff --no-ext-diff >out &&
+ grep "^diff --git a/file b/file" out
+
+'
+
+test_expect_success SYMLINKS 'typechange diff' '
+ rm -f file &&
+ ln -s elif file &&
+
+ cat >expect <<-EOF &&
+ file $(git rev-parse --verify HEAD:file) 100644 $(test_oid zero) 120000
+ EOF
+ GIT_EXTERNAL_DIFF=echo git diff >out &&
+ cut -d" " -f1,3-4,6- <out >actual &&
+ test_cmp expect actual &&
+
+ GIT_EXTERNAL_DIFF=echo git diff --no-ext-diff >actual &&
+ git diff >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'diff.external' '
+ git reset --hard &&
+ echo third >file &&
+ test_config diff.external echo &&
+
+ cat >expect <<-EOF &&
+ file $(git rev-parse --verify HEAD:file) 100644 $(test_oid zero) 100644
+ EOF
+ git diff >out &&
+ cut -d" " -f1,3-4,6- <out >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'diff.external should apply only to diff' '
+ test_config diff.external echo &&
+ git log -p -1 HEAD >out &&
+ grep "^diff --git a/file b/file" out
+'
+
+test_expect_success 'diff.external and --no-ext-diff' '
+ test_config diff.external echo &&
+ git diff --no-ext-diff >out &&
+ grep "^diff --git a/file b/file" out
+'
+
+test_expect_success 'diff attribute' '
+ git reset --hard &&
+ echo third >file &&
+
+ git config diff.parrot.command echo &&
+
+ echo >.gitattributes "file diff=parrot" &&
+
+ cat >expect <<-EOF &&
+ file $(git rev-parse --verify HEAD:file) 100644 $(test_oid zero) 100644
+ EOF
+ git diff >out &&
+ cut -d" " -f1,3-4,6- <out >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success !SANITIZE_LEAK 'diff attribute should apply only to diff' '
+ git log -p -1 HEAD >out &&
+ grep "^diff --git a/file b/file" out
+
+'
+
+test_expect_success 'diff attribute and --no-ext-diff' '
+ git diff --no-ext-diff >out &&
+ grep "^diff --git a/file b/file" out
+
+'
+
+test_expect_success 'diff attribute' '
+
+ git config --unset diff.parrot.command &&
+ git config diff.color.command echo &&
+
+ echo >.gitattributes "file diff=color" &&
+
+ cat >expect <<-EOF &&
+ file $(git rev-parse --verify HEAD:file) 100644 $(test_oid zero) 100644
+ EOF
+ git diff >out &&
+ cut -d" " -f1,3-4,6- <out >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success !SANITIZE_LEAK 'diff attribute should apply only to diff' '
+ git log -p -1 HEAD >out &&
+ grep "^diff --git a/file b/file" out
+
+'
+
+test_expect_success 'diff attribute and --no-ext-diff' '
+ git diff --no-ext-diff >out &&
+ grep "^diff --git a/file b/file" out
+
+'
+
+test_expect_success 'GIT_EXTERNAL_DIFF trumps diff.external' '
+ >.gitattributes &&
+ test_config diff.external "echo ext-global" &&
+
+ cat >expect <<-EOF &&
+ ext-env file $(git rev-parse --verify HEAD:file) 100644 file $(test_oid zero) 100644
+ EOF
+ GIT_EXTERNAL_DIFF="echo ext-env" git diff >out &&
+ cut -d" " -f1-2,4- <out >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'attributes trump GIT_EXTERNAL_DIFF and diff.external' '
+ test_config diff.foo.command "echo ext-attribute" &&
+ test_config diff.external "echo ext-global" &&
+ echo "file diff=foo" >.gitattributes &&
+
+ cat >expect <<-EOF &&
+ ext-attribute file $(git rev-parse --verify HEAD:file) 100644 file $(test_oid zero) 100644
+ EOF
+ GIT_EXTERNAL_DIFF="echo ext-env" git diff >out &&
+ cut -d" " -f1-2,4- <out >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'no diff with -diff' '
+ echo >.gitattributes "file -diff" &&
+ git diff >out &&
+ grep Binary out
+'
+
+echo NULZbetweenZwords | perl -pe 'y/Z/\000/' > file
+
+test_expect_success 'force diff with "diff"' '
+ after=$(git hash-object file) &&
+ after=$(git rev-parse --short $after) &&
+ echo >.gitattributes "file diff" &&
+ git diff >actual &&
+ sed -e "s/^index .*/index $before..$after 100644/" \
+ "$TEST_DIRECTORY"/t4020/diff.NUL >expected-diff &&
+ test_cmp expected-diff actual
+'
+
+test_expect_success 'GIT_EXTERNAL_DIFF with more than one changed files' '
+ echo anotherfile > file2 &&
+ git add file2 &&
+ git commit -m "added 2nd file" &&
+ echo modified >file2 &&
+ GIT_EXTERNAL_DIFF=echo git diff
+'
+
+test_expect_success 'GIT_EXTERNAL_DIFF path counter/total' '
+ write_script external-diff.sh <<-\EOF &&
+ echo $GIT_DIFF_PATH_COUNTER of $GIT_DIFF_PATH_TOTAL >>counter.txt
+ EOF
+ >counter.txt &&
+ cat >expect <<-\EOF &&
+ 1 of 2
+ 2 of 2
+ EOF
+ GIT_EXTERNAL_DIFF=./external-diff.sh git diff &&
+ test_cmp expect counter.txt
+'
+
+test_expect_success 'GIT_EXTERNAL_DIFF generates pretty paths' '
+ test_when_finished "git rm -f file.ext" &&
+ touch file.ext &&
+ git add file.ext &&
+ echo with extension > file.ext &&
+
+ cat >expect <<-EOF &&
+ file.ext
+ EOF
+ GIT_EXTERNAL_DIFF=echo git diff file.ext >out &&
+ basename $(cut -d" " -f2 <out) >actual &&
+ test_cmp expect actual
+'
+
+echo "#!$SHELL_PATH" >fake-diff.sh
+cat >> fake-diff.sh <<\EOF
+cat $2 >> crlfed.txt
+EOF
+chmod a+x fake-diff.sh
+
+keep_only_cr () {
+ tr -dc '\015'
+}
+
+test_expect_success 'external diff with autocrlf = true' '
+ test_config core.autocrlf true &&
+ GIT_EXTERNAL_DIFF=./fake-diff.sh git diff &&
+ test $(wc -l < crlfed.txt) = $(cat crlfed.txt | keep_only_cr | wc -c)
+'
+
+test_expect_success 'diff --cached' '
+ test_config core.autocrlf true &&
+ git add file &&
+ git update-index --assume-unchanged file &&
+ echo second >file &&
+ git diff --cached >actual &&
+ test_cmp expected-diff actual
+'
+
+test_expect_success 'clean up crlf leftovers' '
+ git update-index --no-assume-unchanged file &&
+ rm -f file* &&
+ git reset --hard
+'
+
+test_expect_success 'submodule diff' '
+ git init sub &&
+ ( cd sub && test_commit sub1 ) &&
+ git add sub &&
+ test_tick &&
+ git commit -m "add submodule" &&
+ ( cd sub && test_commit sub2 ) &&
+ write_script gather_pre_post.sh <<-\EOF &&
+ echo "$1 $4" # path, mode
+ cat "$2" # old file
+ cat "$5" # new file
+ EOF
+ GIT_EXTERNAL_DIFF=./gather_pre_post.sh git diff >actual &&
+ cat >expected <<-EOF &&
+ sub 160000
+ Subproject commit $(git rev-parse HEAD:sub)
+ Subproject commit $(cd sub && git rev-parse HEAD)
+ EOF
+ test_cmp expected actual
+'
+
+test_done
diff --git a/t/t4020/diff.NUL b/t/t4020/diff.NUL
new file mode 100644
index 0000000..db2f890
--- /dev/null
+++ b/t/t4020/diff.NUL
Binary files differ
diff --git a/t/t4021-format-patch-numbered.sh b/t/t4021-format-patch-numbered.sh
new file mode 100755
index 0000000..1219aa2
--- /dev/null
+++ b/t/t4021-format-patch-numbered.sh
@@ -0,0 +1,142 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Brian C Gernhardt
+#
+
+test_description='Format-patch numbering options'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+
+ echo A > file &&
+ git add file &&
+ git commit -m First &&
+
+ echo B >> file &&
+ git commit -a -m Second &&
+
+ echo C >> file &&
+ git commit -a -m Third
+
+'
+
+# Each of these gets used multiple times.
+
+test_num_no_numbered() {
+ cnt=$(grep "^Subject: \[PATCH\]" $1 | wc -l) &&
+ test $cnt = $2
+}
+
+test_single_no_numbered() {
+ test_num_no_numbered $1 1
+}
+
+test_no_numbered() {
+ test_num_no_numbered $1 2
+}
+
+test_single_cover_letter_numbered() {
+ grep "^Subject: \[PATCH 0/1\]" $1 &&
+ grep "^Subject: \[PATCH 1/1\]" $1
+}
+
+test_single_numbered() {
+ grep "^Subject: \[PATCH 1/1\]" $1
+}
+
+test_numbered() {
+ grep "^Subject: \[PATCH 1/2\]" $1 &&
+ grep "^Subject: \[PATCH 2/2\]" $1
+}
+
+test_expect_success 'single patch defaults to no numbers' '
+ git format-patch --stdout HEAD~1 >patch0.single &&
+ test_single_no_numbered patch0.single
+'
+
+test_expect_success 'multiple patch defaults to numbered' '
+
+ git format-patch --stdout HEAD~2 >patch0.multiple &&
+ test_numbered patch0.multiple
+
+'
+
+test_expect_success 'Use --numbered' '
+
+ git format-patch --numbered --stdout HEAD~1 >patch1 &&
+ test_single_numbered patch1
+
+'
+
+test_expect_success 'format.numbered = true' '
+
+ git config format.numbered true &&
+ git format-patch --stdout HEAD~2 >patch2 &&
+ test_numbered patch2
+
+'
+
+test_expect_success 'format.numbered && single patch' '
+
+ git format-patch --stdout HEAD^ > patch3 &&
+ test_single_numbered patch3
+
+'
+
+test_expect_success 'format.numbered && --no-numbered' '
+
+ git format-patch --no-numbered --stdout HEAD~2 >patch4 &&
+ test_no_numbered patch4
+
+'
+
+test_expect_success 'format.numbered && --keep-subject' '
+
+ git format-patch --keep-subject --stdout HEAD^ >patch4a &&
+ grep "^Subject: Third" patch4a
+
+'
+
+test_expect_success 'format.numbered = auto' '
+
+ git config format.numbered auto &&
+ git format-patch --stdout HEAD~2 > patch5 &&
+ test_numbered patch5
+
+'
+
+test_expect_success 'format.numbered = auto && single patch' '
+
+ git format-patch --stdout HEAD^ > patch6 &&
+ test_single_no_numbered patch6
+
+'
+
+test_expect_success 'format.numbered = auto && --no-numbered' '
+
+ git format-patch --no-numbered --stdout HEAD~2 > patch7 &&
+ test_no_numbered patch7
+
+'
+
+test_expect_success '--start-number && --numbered' '
+
+ git format-patch --start-number 3 --numbered --stdout HEAD~1 > patch8 &&
+ grep "^Subject: \[PATCH 3/3\]" patch8
+'
+
+test_expect_success 'single patch with cover-letter defaults to numbers' '
+ git format-patch --cover-letter --stdout HEAD~1 >patch9.single &&
+ test_single_cover_letter_numbered patch9.single
+'
+
+test_expect_success 'Use --no-numbered and --cover-letter single patch' '
+ git format-patch --no-numbered --stdout --cover-letter HEAD~1 >patch10 &&
+ test_no_numbered patch10
+'
+
+
+
+test_done
diff --git a/t/t4022-diff-rewrite.sh b/t/t4022-diff-rewrite.sh
new file mode 100755
index 0000000..6fed993
--- /dev/null
+++ b/t/t4022-diff-rewrite.sh
@@ -0,0 +1,101 @@
+#!/bin/sh
+
+test_description='rewrite diff'
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-diff-data.sh
+
+test_expect_success setup '
+
+ COPYING_test_data >test.data &&
+ cp test.data test &&
+ git add test &&
+ tr \
+ "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" \
+ "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM" \
+ <test.data >test &&
+ echo "to be deleted" >test2 &&
+ blob=$(git hash-object test2) &&
+ blob=$(git rev-parse --short $blob) &&
+ git add test2
+
+'
+
+test_expect_success 'detect rewrite' '
+
+ actual=$(git diff-files -B --summary test) &&
+ expr "$actual" : " rewrite test ([0-9]*%)$"
+
+'
+
+cat >expect <<EOF
+diff --git a/test2 b/test2
+deleted file mode 100644
+index $blob..0000000
+--- a/test2
++++ /dev/null
+@@ -1 +0,0 @@
+-to be deleted
+EOF
+test_expect_success 'show deletion diff without -D' '
+
+ rm test2 &&
+ git diff -- test2 >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<EOF
+diff --git a/test2 b/test2
+deleted file mode 100644
+index $blob..0000000
+EOF
+test_expect_success 'suppress deletion diff with -D' '
+
+ git diff -D -- test2 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'show deletion diff with -B' '
+
+ git diff -B -- test >actual &&
+ grep "Linus Torvalds" actual
+'
+
+test_expect_success 'suppress deletion diff with -B -D' '
+
+ git diff -B -D -- test >actual &&
+ grep -v "Linus Torvalds" actual
+'
+
+test_expect_success 'prepare a file that ends with an incomplete line' '
+ test_seq 1 99 >seq &&
+ printf 100 >>seq &&
+ git add seq &&
+ git commit seq -m seq
+'
+
+test_expect_success 'rewrite the middle 90% of sequence file and terminate with newline' '
+ test_seq 1 5 >seq &&
+ test_seq 9331 9420 >>seq &&
+ test_seq 96 100 >>seq
+'
+
+test_expect_success 'confirm that sequence file is considered a rewrite' '
+ git diff -B seq >res &&
+ grep "dissimilarity index" res
+'
+
+test_expect_success 'no newline at eof is on its own line without -B' '
+ git diff seq >res &&
+ grep "^\\\\ " res &&
+ ! grep "^..*\\\\ " res
+'
+
+test_expect_success 'no newline at eof is on its own line with -B' '
+ git diff -B seq >res &&
+ grep "^\\\\ " res &&
+ ! grep "^..*\\\\ " res
+'
+
+test_done
+
diff --git a/t/t4023-diff-rename-typechange.sh b/t/t4023-diff-rename-typechange.sh
new file mode 100755
index 0000000..787605c
--- /dev/null
+++ b/t/t4023-diff-rename-typechange.sh
@@ -0,0 +1,88 @@
+#!/bin/sh
+
+test_description='typechange rename detection'
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-diff.sh
+
+test_expect_success setup '
+
+ rm -f foo bar &&
+ COPYING_test_data >foo &&
+ test_ln_s_add linklink bar &&
+ git add foo &&
+ git commit -a -m Initial &&
+ git tag one &&
+
+ git rm -f foo bar &&
+ COPYING_test_data >bar &&
+ test_ln_s_add linklink foo &&
+ git add bar &&
+ git commit -a -m Second &&
+ git tag two &&
+
+ git rm -f foo bar &&
+ COPYING_test_data >foo &&
+ git add foo &&
+ git commit -a -m Third &&
+ git tag three &&
+
+ mv foo bar &&
+ test_ln_s_add linklink foo &&
+ git add bar &&
+ git commit -a -m Fourth &&
+ git tag four &&
+
+ # This is purely for sanity check
+
+ git rm -f foo bar &&
+ COPYING_test_data >foo &&
+ cat "$TEST_DIRECTORY"/../Makefile >bar &&
+ git add foo bar &&
+ git commit -a -m Fifth &&
+ git tag five &&
+
+ git rm -f foo bar &&
+ cat "$TEST_DIRECTORY"/../Makefile >foo &&
+ COPYING_test_data >bar &&
+ git add foo bar &&
+ git commit -a -m Sixth &&
+ git tag six
+
+'
+
+test_expect_success 'cross renames to be detected for regular files' '
+ git diff-tree five six -r --name-status -B -M >out &&
+ sort out >actual &&
+ {
+ echo "R100 foo bar" &&
+ echo "R100 bar foo"
+ } | sort >expect &&
+ test_cmp expect actual
+
+'
+
+test_expect_success 'cross renames to be detected for typechange' '
+ git diff-tree one two -r --name-status -B -M >out &&
+ sort out >actual &&
+ {
+ echo "R100 foo bar" &&
+ echo "R100 bar foo"
+ } | sort >expect &&
+ test_cmp expect actual
+
+'
+
+test_expect_success 'moves and renames' '
+ git diff-tree three four -r --name-status -B -M >out &&
+ sort out >actual &&
+ {
+ # see -B -M (#6) in t4008
+ echo "C100 foo bar" &&
+ echo "T100 foo"
+ } | sort >expect &&
+ test_cmp expect actual
+
+'
+
+test_done
diff --git a/t/t4024-diff-optimize-common.sh b/t/t4024-diff-optimize-common.sh
new file mode 100755
index 0000000..e2f0eca
--- /dev/null
+++ b/t/t4024-diff-optimize-common.sh
@@ -0,0 +1,158 @@
+#!/bin/sh
+
+test_description='common tail optimization'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+z=zzzzzzzz ;# 8
+z="$z$z$z$z$z$z$z$z" ;# 64
+z="$z$z$z$z$z$z$z$z" ;# 512
+z="$z$z$z$z" ;# 2048
+z2047=$(expr "$z" : '.\(.*\)') ; #2047
+
+x=zzzzzzzzzz ;# 10
+y="$x$x$x$x$x$x$x$x$x$x" ;# 100
+z="$y$y$y$y$y$y$y$y$y$y" ;# 1000
+z1000=$z
+z100=$y
+z10=$x
+
+zs() {
+ count="$1"
+ while test "$count" -ge 1000
+ do
+ count=$(($count - 1000))
+ printf "%s" $z1000
+ done
+ while test "$count" -ge 100
+ do
+ count=$(($count - 100))
+ printf "%s" $z100
+ done
+ while test "$count" -ge 10
+ do
+ count=$(($count - 10))
+ printf "%s" $z10
+ done
+ while test "$count" -ge 1
+ do
+ count=$(($count - 1))
+ printf "z"
+ done
+}
+
+zc () {
+ sed -e "/^index/d" \
+ -e "s/$z1000/Q/g" \
+ -e "s/QQQQQQQQQ/Z9000/g" \
+ -e "s/QQQQQQQQ/Z8000/g" \
+ -e "s/QQQQQQQ/Z7000/g" \
+ -e "s/QQQQQQ/Z6000/g" \
+ -e "s/QQQQQ/Z5000/g" \
+ -e "s/QQQQ/Z4000/g" \
+ -e "s/QQQ/Z3000/g" \
+ -e "s/QQ/Z2000/g" \
+ -e "s/Q/Z1000/g" \
+ -e "s/$z100/Q/g" \
+ -e "s/QQQQQQQQQ/Z900/g" \
+ -e "s/QQQQQQQQ/Z800/g" \
+ -e "s/QQQQQQQ/Z700/g" \
+ -e "s/QQQQQQ/Z600/g" \
+ -e "s/QQQQQ/Z500/g" \
+ -e "s/QQQQ/Z400/g" \
+ -e "s/QQQ/Z300/g" \
+ -e "s/QQ/Z200/g" \
+ -e "s/Q/Z100/g" \
+ -e "s/000Z//g" \
+ -e "s/$z10/Q/g" \
+ -e "s/QQQQQQQQQ/Z90/g" \
+ -e "s/QQQQQQQQ/Z80/g" \
+ -e "s/QQQQQQQ/Z70/g" \
+ -e "s/QQQQQQ/Z60/g" \
+ -e "s/QQQQQ/Z50/g" \
+ -e "s/QQQQ/Z40/g" \
+ -e "s/QQQ/Z30/g" \
+ -e "s/QQ/Z20/g" \
+ -e "s/Q/Z10/g" \
+ -e "s/00Z//g" \
+ -e "s/z/Q/g" \
+ -e "s/QQQQQQQQQ/Z9/g" \
+ -e "s/QQQQQQQQ/Z8/g" \
+ -e "s/QQQQQQQ/Z7/g" \
+ -e "s/QQQQQQ/Z6/g" \
+ -e "s/QQQQQ/Z5/g" \
+ -e "s/QQQQ/Z4/g" \
+ -e "s/QQQ/Z3/g" \
+ -e "s/QQ/Z2/g" \
+ -e "s/Q/Z1/g" \
+ -e "s/0Z//g" \
+ ;
+}
+
+expect_pattern () {
+ cnt="$1"
+ cat <<EOF
+diff --git a/file-a$cnt b/file-a$cnt
+--- a/file-a$cnt
++++ b/file-a$cnt
+@@ -1 +1 @@
+-Z${cnt}a
++Z${cnt}A
+diff --git a/file-b$cnt b/file-b$cnt
+--- a/file-b$cnt
++++ b/file-b$cnt
+@@ -1 +1 @@
+-b
++B
+diff --git a/file-c$cnt b/file-c$cnt
+--- a/file-c$cnt
++++ b/file-c$cnt
+@@ -1 +1 @@
+-cZ$cnt
+\ No newline at end of file
++CZ$cnt
+\ No newline at end of file
+diff --git a/file-d$cnt b/file-d$cnt
+--- a/file-d$cnt
++++ b/file-d$cnt
+@@ -1 +1 @@
+-d
++D
+EOF
+}
+
+sample='1023 1024 1025 2047 4095'
+
+test_expect_success setup '
+
+ for n in $sample
+ do
+ ( zs $n && echo a ) >file-a$n &&
+ ( echo b && zs $n && echo ) >file-b$n &&
+ ( printf c && zs $n ) >file-c$n &&
+ ( echo d && zs $n ) >file-d$n &&
+
+ git add file-a$n file-b$n file-c$n file-d$n &&
+
+ ( zs $n && echo A ) >file-a$n &&
+ ( echo B && zs $n && echo ) >file-b$n &&
+ ( printf C && zs $n ) >file-c$n &&
+ ( echo D && zs $n ) >file-d$n &&
+
+ expect_pattern $n || return 1
+
+ done >expect
+'
+
+test_expect_success 'diff -U0' '
+
+ for n in $sample
+ do
+ git diff -U0 file-?$n || return 1
+ done | zc >actual &&
+ test_cmp expect actual
+
+'
+
+test_done
diff --git a/t/t4025-hunk-header.sh b/t/t4025-hunk-header.sh
new file mode 100755
index 0000000..5397cb7
--- /dev/null
+++ b/t/t4025-hunk-header.sh
@@ -0,0 +1,39 @@
+#!/bin/sh
+
+test_description='diff hunk header truncation'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+N='日本語'
+N1='æ—¥'
+N2='日本'
+NS="$N$N$N$N$N$N$N$N$N$N$N$N$N"
+
+test_expect_success setup '
+
+ (
+ echo "A $NS" &&
+ printf " %s\n" B C D E F G H I J K &&
+ echo "L $NS" &&
+ printf " %s\n" M N O P Q R S T U V
+ ) >file &&
+ git add file &&
+
+ sed -e "/^ [EP]/s/$/ modified/" <file >file+ &&
+ mv file+ file
+
+'
+
+test_expect_success 'hunk header truncation with an overly long line' '
+
+ git diff | sed -n -e "s/^.*@@//p" >actual &&
+ (
+ echo " A $N$N$N$N$N$N$N$N$N2" &&
+ echo " L $N$N$N$N$N$N$N$N$N1"
+ ) >expected &&
+ test_cmp expected actual
+
+'
+
+test_done
diff --git a/t/t4026-color.sh b/t/t4026-color.sh
new file mode 100755
index 0000000..cc3f60d
--- /dev/null
+++ b/t/t4026-color.sh
@@ -0,0 +1,159 @@
+#!/bin/sh
+#
+# Copyright (c) 2008 Timo Hirvonen
+#
+
+test_description='Test diff/status color escape codes'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+ESC=$(printf '\033')
+color()
+{
+ actual=$(git config --get-color no.such.slot "$1") &&
+ test "$actual" = "${2:+$ESC}$2"
+}
+
+invalid_color()
+{
+ test_must_fail git config --get-color no.such.slot "$1"
+}
+
+test_expect_success 'reset' '
+ color "reset" "[m"
+'
+
+test_expect_success 'empty color is empty' '
+ color "" ""
+'
+
+test_expect_success 'attribute before color name' '
+ color "bold red" "[1;31m"
+'
+
+test_expect_success 'aixterm bright fg color' '
+ color "brightred" "[91m"
+'
+
+test_expect_success 'aixterm bright bg color' '
+ color "green brightblue" "[32;104m"
+'
+
+test_expect_success 'color name before attribute' '
+ color "red bold" "[1;31m"
+'
+
+test_expect_success 'attr fg bg' '
+ color "ul blue red" "[4;34;41m"
+'
+
+test_expect_success 'fg attr bg' '
+ color "blue ul red" "[4;34;41m"
+'
+
+test_expect_success 'fg bg attr' '
+ color "blue red ul" "[4;34;41m"
+'
+
+test_expect_success 'fg bg attr...' '
+ color "blue bold dim ul blink reverse" "[1;2;4;5;7;34m"
+'
+
+test_expect_success 'reset fg bg attr...' '
+ color "reset blue bold dim ul blink reverse" "[;1;2;4;5;7;34m"
+'
+
+# note that nobold and nodim are the same code (22)
+test_expect_success 'attr negation' '
+ color "nobold nodim noul noblink noreverse" "[22;24;25;27m"
+'
+
+test_expect_success '"no-" variant of negation' '
+ color "no-bold no-blink" "[22;25m"
+'
+
+test_expect_success 'long color specification' '
+ color "254 255 bold dim ul blink reverse" "[1;2;4;5;7;38;5;254;48;5;255m"
+'
+
+test_expect_success 'absurdly long color specification' '
+ color \
+ "#ffffff #ffffff bold nobold dim nodim italic noitalic
+ ul noul blink noblink reverse noreverse strike nostrike" \
+ "[1;2;3;4;5;7;9;22;23;24;25;27;29;38;2;255;255;255;48;2;255;255;255m"
+'
+
+test_expect_success '0-7 are aliases for basic ANSI color names' '
+ color "0 7" "[30;47m"
+'
+
+test_expect_success '8-15 are aliases for aixterm color names' '
+ color "12 13" "[94;105m"
+'
+
+test_expect_success '256 colors' '
+ color "254 bold 255" "[1;38;5;254;48;5;255m"
+'
+
+test_expect_success '24-bit colors' '
+ color "#ff00ff black" "[38;2;255;0;255;40m"
+'
+
+test_expect_success '"default" foreground' '
+ color "default" "[39m"
+'
+
+test_expect_success '"normal default" to clear background' '
+ color "normal default" "[49m"
+'
+
+test_expect_success '"default" can be combined with attributes' '
+ color "default default no-reverse bold" "[1;27;39;49m"
+'
+
+test_expect_success '"normal" yields no color at all"' '
+ color "normal black" "[40m"
+'
+
+test_expect_success '-1 is a synonym for "normal"' '
+ color "-1 black" "[40m"
+'
+
+test_expect_success 'color too small' '
+ invalid_color "-2"
+'
+
+test_expect_success 'color too big' '
+ invalid_color "256"
+'
+
+test_expect_success 'extra character after color number' '
+ invalid_color "3X"
+'
+
+test_expect_success 'extra character after color name' '
+ invalid_color "redX"
+'
+
+test_expect_success 'extra character after attribute' '
+ invalid_color "dimX"
+'
+
+test_expect_success 'unknown color slots are ignored (diff)' '
+ git config color.diff.nosuchslotwilleverbedefined white &&
+ git diff --color
+'
+
+test_expect_success 'unknown color slots are ignored (branch)' '
+ git config color.branch.nosuchslotwilleverbedefined white &&
+ git branch -a
+'
+
+test_expect_success 'unknown color slots are ignored (status)' '
+ git config color.status.nosuchslotwilleverbedefined white &&
+ { git status; ret=$?; } &&
+ case $ret in 0|1) : ok ;; *) false ;; esac
+'
+
+test_done
diff --git a/t/t4027-diff-submodule.sh b/t/t4027-diff-submodule.sh
new file mode 100755
index 0000000..40164ae
--- /dev/null
+++ b/t/t4027-diff-submodule.sh
@@ -0,0 +1,292 @@
+#!/bin/sh
+
+test_description='difference in submodules'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-diff.sh
+
+test_expect_success setup '
+ test_tick &&
+ test_create_repo sub &&
+ (
+ cd sub &&
+ echo hello >world &&
+ git add world &&
+ git commit -m submodule
+ ) &&
+
+ test_tick &&
+ echo frotz >nitfol &&
+ git add nitfol sub &&
+ git commit -m superproject &&
+
+ (
+ cd sub &&
+ echo goodbye >world &&
+ git add world &&
+ git commit -m "submodule #2"
+ ) &&
+
+ git -C sub rev-list HEAD >revs &&
+ set x $(cat revs) &&
+ echo ":160000 160000 $3 $ZERO_OID M sub" >expect &&
+ subtip=$3 subprev=$2
+'
+
+test_expect_success 'git diff --raw HEAD' '
+ hexsz=$(test_oid hexsz) &&
+ git diff --raw --abbrev=$hexsz HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git diff-index --raw HEAD' '
+ git diff-index --raw HEAD >actual.index &&
+ test_cmp expect actual.index
+'
+
+test_expect_success 'git diff-files --raw' '
+ git diff-files --raw >actual.files &&
+ test_cmp expect actual.files
+'
+
+expect_from_to () {
+ printf "%sSubproject commit %s\n+Subproject commit %s\n" \
+ "-" "$1" "$2"
+}
+
+test_expect_success 'git diff HEAD' '
+ git diff HEAD >actual &&
+ sed -e "1,/^@@/d" actual >actual.body &&
+ expect_from_to >expect.body $subtip $subprev &&
+ test_cmp expect.body actual.body
+'
+
+test_expect_success 'git diff HEAD with dirty submodule (work tree)' '
+ echo >>sub/world &&
+ git diff HEAD >actual &&
+ sed -e "1,/^@@/d" actual >actual.body &&
+ expect_from_to >expect.body $subtip $subprev-dirty &&
+ test_cmp expect.body actual.body
+'
+
+test_expect_success 'git diff HEAD with dirty submodule (index)' '
+ (
+ cd sub &&
+ git reset --hard &&
+ echo >>world &&
+ git add world
+ ) &&
+ git diff HEAD >actual &&
+ sed -e "1,/^@@/d" actual >actual.body &&
+ expect_from_to >expect.body $subtip $subprev-dirty &&
+ test_cmp expect.body actual.body
+'
+
+test_expect_success 'git diff HEAD with dirty submodule (untracked)' '
+ (
+ cd sub &&
+ git reset --hard &&
+ git clean -qfdx &&
+ >cruft
+ ) &&
+ git diff HEAD >actual &&
+ sed -e "1,/^@@/d" actual >actual.body &&
+ expect_from_to >expect.body $subtip $subprev &&
+ test_cmp expect.body actual.body
+'
+
+test_expect_success 'git diff HEAD with dirty submodule (untracked) (none ignored)' '
+ test_config diff.ignoreSubmodules none &&
+ git diff HEAD >actual &&
+ sed -e "1,/^@@/d" actual >actual.body &&
+ expect_from_to >expect.body $subtip $subprev-dirty &&
+ test_cmp expect.body actual.body
+'
+
+test_expect_success 'git diff HEAD with dirty submodule (work tree, refs match)' '
+ git commit -m "x" sub &&
+ echo >>sub/world &&
+ git diff HEAD >actual &&
+ sed -e "1,/^@@/d" actual >actual.body &&
+ expect_from_to >expect.body $subprev $subprev-dirty &&
+ test_cmp expect.body actual.body &&
+ git diff --ignore-submodules HEAD >actual2 &&
+ test_must_be_empty actual2 &&
+ git diff --ignore-submodules=untracked HEAD >actual3 &&
+ sed -e "1,/^@@/d" actual3 >actual3.body &&
+ expect_from_to >expect.body $subprev $subprev-dirty &&
+ test_cmp expect.body actual3.body &&
+ git diff --ignore-submodules=dirty HEAD >actual4 &&
+ test_must_be_empty actual4
+'
+
+test_expect_success 'git diff HEAD with dirty submodule (work tree, refs match) [.gitmodules]' '
+ git config diff.ignoreSubmodules dirty &&
+ git diff HEAD >actual &&
+ test_must_be_empty actual &&
+ git config --add -f .gitmodules submodule.subname.ignore none &&
+ git config --add -f .gitmodules submodule.subname.path sub &&
+ git diff HEAD >actual &&
+ sed -e "1,/^@@/d" actual >actual.body &&
+ expect_from_to >expect.body $subprev $subprev-dirty &&
+ test_cmp expect.body actual.body &&
+ git config -f .gitmodules submodule.subname.ignore all &&
+ git config -f .gitmodules submodule.subname.path sub &&
+ git diff HEAD >actual2 &&
+ test_must_be_empty actual2 &&
+ git config -f .gitmodules submodule.subname.ignore untracked &&
+ git diff HEAD >actual3 &&
+ sed -e "1,/^@@/d" actual3 >actual3.body &&
+ expect_from_to >expect.body $subprev $subprev-dirty &&
+ test_cmp expect.body actual3.body &&
+ git config -f .gitmodules submodule.subname.ignore dirty &&
+ git diff HEAD >actual4 &&
+ test_must_be_empty actual4 &&
+ git config submodule.subname.ignore none &&
+ git config submodule.subname.path sub &&
+ git diff HEAD >actual &&
+ sed -e "1,/^@@/d" actual >actual.body &&
+ expect_from_to >expect.body $subprev $subprev-dirty &&
+ test_cmp expect.body actual.body &&
+ git config --remove-section submodule.subname &&
+ git config --remove-section -f .gitmodules submodule.subname &&
+ git config --unset diff.ignoreSubmodules &&
+ rm .gitmodules
+'
+
+test_expect_success 'git diff HEAD with dirty submodule (index, refs match)' '
+ (
+ cd sub &&
+ git reset --hard &&
+ echo >>world &&
+ git add world
+ ) &&
+ git diff HEAD >actual &&
+ sed -e "1,/^@@/d" actual >actual.body &&
+ expect_from_to >expect.body $subprev $subprev-dirty &&
+ test_cmp expect.body actual.body
+'
+
+test_expect_success 'git diff HEAD with dirty submodule (untracked, refs match)' '
+ (
+ cd sub &&
+ git reset --hard &&
+ git clean -qfdx &&
+ >cruft
+ ) &&
+ git diff --ignore-submodules=none HEAD >actual &&
+ sed -e "1,/^@@/d" actual >actual.body &&
+ expect_from_to >expect.body $subprev $subprev-dirty &&
+ test_cmp expect.body actual.body &&
+ git diff --ignore-submodules=all HEAD >actual2 &&
+ test_must_be_empty actual2 &&
+ git diff HEAD >actual3 &&
+ test_must_be_empty actual3 &&
+ git diff --ignore-submodules=dirty HEAD >actual4 &&
+ test_must_be_empty actual4
+'
+
+test_expect_success 'git diff HEAD with dirty submodule (untracked, refs match) [.gitmodules]' '
+ git config --add -f .gitmodules submodule.subname.ignore all &&
+ git config --add -f .gitmodules submodule.subname.path sub &&
+ git diff HEAD >actual2 &&
+ test_must_be_empty actual2 &&
+ git config -f .gitmodules submodule.subname.ignore untracked &&
+ git diff HEAD >actual3 &&
+ test_must_be_empty actual3 &&
+ git config -f .gitmodules submodule.subname.ignore dirty &&
+ git diff HEAD >actual4 &&
+ test_must_be_empty actual4 &&
+ git config submodule.subname.ignore none &&
+ git config submodule.subname.path sub &&
+ git diff HEAD >actual &&
+ sed -e "1,/^@@/d" actual >actual.body &&
+ expect_from_to >expect.body $subprev $subprev-dirty &&
+ test_cmp expect.body actual.body &&
+ git config --remove-section submodule.subname &&
+ git config --remove-section -f .gitmodules submodule.subname &&
+ rm .gitmodules
+'
+
+test_expect_success 'git diff between submodule commits' '
+ git diff HEAD^..HEAD >actual &&
+ sed -e "1,/^@@/d" actual >actual.body &&
+ expect_from_to >expect.body $subtip $subprev &&
+ test_cmp expect.body actual.body &&
+ git diff --ignore-submodules=dirty HEAD^..HEAD >actual &&
+ sed -e "1,/^@@/d" actual >actual.body &&
+ expect_from_to >expect.body $subtip $subprev &&
+ test_cmp expect.body actual.body &&
+ git diff --ignore-submodules HEAD^..HEAD >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'git diff between submodule commits [.gitmodules]' '
+ git diff HEAD^..HEAD >actual &&
+ sed -e "1,/^@@/d" actual >actual.body &&
+ expect_from_to >expect.body $subtip $subprev &&
+ test_cmp expect.body actual.body &&
+ git config --add -f .gitmodules submodule.subname.ignore dirty &&
+ git config --add -f .gitmodules submodule.subname.path sub &&
+ git diff HEAD^..HEAD >actual &&
+ sed -e "1,/^@@/d" actual >actual.body &&
+ expect_from_to >expect.body $subtip $subprev &&
+ test_cmp expect.body actual.body &&
+ git config -f .gitmodules submodule.subname.ignore all &&
+ git diff HEAD^..HEAD >actual &&
+ test_must_be_empty actual &&
+ git config submodule.subname.ignore dirty &&
+ git config submodule.subname.path sub &&
+ git diff HEAD^..HEAD >actual &&
+ sed -e "1,/^@@/d" actual >actual.body &&
+ expect_from_to >expect.body $subtip $subprev &&
+ git config --remove-section submodule.subname &&
+ git config --remove-section -f .gitmodules submodule.subname &&
+ rm .gitmodules
+'
+
+test_expect_success 'git diff (empty submodule dir)' '
+ rm -rf sub/* sub/.git &&
+ git diff > actual.empty &&
+ test_must_be_empty actual.empty
+'
+
+test_expect_success 'conflicted submodule setup' '
+ c=$(test_oid ff_1) &&
+ (
+ echo "000000 $ZERO_OID 0 sub" &&
+ echo "160000 1$c 1 sub" &&
+ echo "160000 2$c 2 sub" &&
+ echo "160000 3$c 3 sub"
+ ) | git update-index --index-info &&
+ echo >expect.nosub "diff --cc sub
+index 2ffffff,3ffffff..0000000
+--- a/sub
++++ b/sub
+@@@ -1,1 -1,1 +1,1 @@@
+- Subproject commit 2$c
+ -Subproject commit 3$c
+++Subproject commit $ZERO_OID" &&
+
+ hh=$(git rev-parse HEAD) &&
+ sed -e "s/$ZERO_OID/$hh/" expect.nosub >expect.withsub
+
+'
+
+test_expect_success 'combined (empty submodule)' '
+ rm -fr sub && mkdir sub &&
+ git diff >actual &&
+ test_cmp expect.nosub actual
+'
+
+test_expect_success 'combined (with submodule)' '
+ rm -fr sub &&
+ git clone --no-checkout . sub &&
+ git diff >actual &&
+ test_cmp expect.withsub actual
+'
+
+
+
+test_done
diff --git a/t/t4028-format-patch-mime-headers.sh b/t/t4028-format-patch-mime-headers.sh
new file mode 100755
index 0000000..60cb819
--- /dev/null
+++ b/t/t4028-format-patch-mime-headers.sh
@@ -0,0 +1,32 @@
+#!/bin/sh
+
+test_description='format-patch mime headers and extra headers do not conflict'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'create commit with utf-8 body' '
+ echo content >file &&
+ git add file &&
+ git commit -m one &&
+ echo more >>file &&
+ git commit -a -m "two
+
+ utf-8 body: ñ"
+'
+
+test_expect_success 'patch has mime headers' '
+ rm -f 0001-two.patch &&
+ git format-patch HEAD^ &&
+ grep -i "content-type: text/plain; charset=utf-8" 0001-two.patch
+'
+
+test_expect_success 'patch has mime and extra headers' '
+ rm -f 0001-two.patch &&
+ git config format.headers "x-foo: bar" &&
+ git format-patch HEAD^ &&
+ grep -i "x-foo: bar" 0001-two.patch &&
+ grep -i "content-type: text/plain; charset=utf-8" 0001-two.patch
+'
+
+test_done
diff --git a/t/t4029-diff-trailing-space.sh b/t/t4029-diff-trailing-space.sh
new file mode 100755
index 0000000..5f8ffef
--- /dev/null
+++ b/t/t4029-diff-trailing-space.sh
@@ -0,0 +1,44 @@
+#!/bin/sh
+#
+# Copyright (c) Jim Meyering
+#
+test_description='diff honors config option, diff.suppressBlankEmpty'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+cat <<\EOF >expected ||
+diff --git a/f b/f
+index 5f6a263..8cb8bae 100644
+--- a/f
++++ b/f
+@@ -1,2 +1,2 @@
+
+-x
++y
+EOF
+exit 1
+
+test_expect_success "$test_description" '
+ printf "\nx\n" > f &&
+ before=$(git hash-object f) &&
+ before=$(git rev-parse --short $before) &&
+ git add f &&
+ git commit -q -m. f &&
+ printf "\ny\n" > f &&
+ after=$(git hash-object f) &&
+ after=$(git rev-parse --short $after) &&
+ sed -e "s/^index .*/index $before..$after 100644/" expected >exp &&
+ git config --bool diff.suppressBlankEmpty true &&
+ git diff f > actual &&
+ test_cmp exp actual &&
+ perl -i.bak -p -e "s/^\$/ /" exp &&
+ git config --bool diff.suppressBlankEmpty false &&
+ git diff f > actual &&
+ test_cmp exp actual &&
+ git config --bool --unset diff.suppressBlankEmpty &&
+ git diff f > actual &&
+ test_cmp exp actual
+'
+
+test_done
diff --git a/t/t4030-diff-textconv.sh b/t/t4030-diff-textconv.sh
new file mode 100755
index 0000000..a39a626
--- /dev/null
+++ b/t/t4030-diff-textconv.sh
@@ -0,0 +1,173 @@
+#!/bin/sh
+
+test_description='diff.*.textconv tests'
+. ./test-lib.sh
+
+find_diff() {
+ sed '1,/^index /d' | sed '/^-- $/,$d'
+}
+
+cat >expect.binary <<'EOF'
+Binary files a/file and b/file differ
+EOF
+
+cat >expect.text <<'EOF'
+--- a/file
++++ b/file
+@@ -1 +1,2 @@
+ 0
++1
+EOF
+
+cat >hexdump <<'EOF'
+#!/bin/sh
+"$PERL_PATH" -e '$/ = undef; $_ = <>; s/./ord($&)/ge; print $_' < "$1"
+EOF
+chmod +x hexdump
+
+test_expect_success 'setup binary file with history' '
+ test_commit --printf one file "\\0\\n" &&
+ test_commit --printf --append two file "\\01\\n"
+'
+
+test_expect_success 'file is considered binary by porcelain' '
+ git diff HEAD^ HEAD >diff &&
+ find_diff <diff >actual &&
+ test_cmp expect.binary actual
+'
+
+test_expect_success 'file is considered binary by plumbing' '
+ git diff-tree -p HEAD^ HEAD >diff &&
+ find_diff <diff >actual &&
+ test_cmp expect.binary actual
+'
+
+test_expect_success 'setup textconv filters' '
+ echo file diff=foo >.gitattributes &&
+ git config diff.foo.textconv "\"$(pwd)\""/hexdump &&
+ git config diff.fail.textconv false
+'
+
+test_expect_success 'diff produces text' '
+ git diff HEAD^ HEAD >diff &&
+ find_diff <diff >actual &&
+ test_cmp expect.text actual
+'
+
+test_expect_success 'show commit produces text' '
+ git show HEAD >diff &&
+ find_diff <diff >actual &&
+ test_cmp expect.text actual
+'
+
+test_expect_success 'diff-tree produces binary' '
+ git diff-tree -p HEAD^ HEAD >diff &&
+ find_diff <diff >actual &&
+ test_cmp expect.binary actual
+'
+
+test_expect_success 'log produces text' '
+ git log -1 -p >log &&
+ find_diff <log >actual &&
+ test_cmp expect.text actual
+'
+
+test_expect_success 'format-patch produces binary' '
+ git format-patch --no-binary --stdout HEAD^ >patch &&
+ find_diff <patch >actual &&
+ test_cmp expect.binary actual
+'
+
+test_expect_success 'status -v produces text' '
+ git reset --soft HEAD^ &&
+ git status -v >diff &&
+ find_diff <diff >actual &&
+ test_cmp expect.text actual &&
+ git reset --soft HEAD@{1}
+'
+
+test_expect_success 'show blob produces binary' '
+ git show HEAD:file >actual &&
+ printf "\\0\\n\\01\\n" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'show --textconv blob produces text' '
+ git show --textconv HEAD:file >actual &&
+ printf "0\\n1\\n" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'show --no-textconv blob produces binary' '
+ git show --no-textconv HEAD:file >actual &&
+ printf "\\0\\n\\01\\n" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'grep-diff (-G) operates on textconv data (add)' '
+ echo one >expect &&
+ git log --root --format=%s -G0 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'grep-diff (-G) operates on textconv data (modification)' '
+ echo two >expect &&
+ git log --root --format=%s -G1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'pickaxe (-S) operates on textconv data (add)' '
+ echo one >expect &&
+ git log --root --format=%s -S0 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'pickaxe (-S) operates on textconv data (modification)' '
+ echo two >expect &&
+ git log --root --format=%s -S1 >actual &&
+ test_cmp expect actual
+'
+
+cat >expect.stat <<'EOF'
+ file | Bin 2 -> 4 bytes
+ 1 file changed, 0 insertions(+), 0 deletions(-)
+EOF
+test_expect_success 'diffstat does not run textconv' '
+ echo file diff=fail >.gitattributes &&
+ git diff --stat HEAD^ HEAD >actual &&
+ test_cmp expect.stat actual &&
+
+ head -n1 <expect.stat >expect.line1 &&
+ head -n1 <actual >actual.line1 &&
+ test_cmp expect.line1 actual.line1
+'
+# restore working setup
+echo file diff=foo >.gitattributes
+
+symlink=$(git rev-parse --short $(printf frotz | git hash-object --stdin))
+cat >expect.typechange <<EOF
+--- a/file
++++ /dev/null
+@@ -1,2 +0,0 @@
+-0
+-1
+diff --git a/file b/file
+new file mode 120000
+index 0000000..$symlink
+--- /dev/null
++++ b/file
+@@ -0,0 +1 @@
++frotz
+\ No newline at end of file
+EOF
+
+test_expect_success 'textconv does not act on symlinks' '
+ rm -f file &&
+ test_ln_s_add frotz file &&
+ git commit -m typechange &&
+ git show >diff &&
+ find_diff <diff >actual &&
+ test_cmp expect.typechange actual
+'
+
+test_done
diff --git a/t/t4031-diff-rewrite-binary.sh b/t/t4031-diff-rewrite-binary.sh
new file mode 100755
index 0000000..c4394a2
--- /dev/null
+++ b/t/t4031-diff-rewrite-binary.sh
@@ -0,0 +1,80 @@
+#!/bin/sh
+
+test_description='rewrite diff on binary file'
+
+. ./test-lib.sh
+
+# We must be large enough to meet the MINIMUM_BREAK_SIZE
+# requirement.
+make_file() {
+ # common first line to help identify rewrite versus regular diff
+ printf "=\n" >file
+ for i in 1 2 3 4 5 6 7 8 9 10
+ do
+ for j in 1 2 3 4 5 6 7 8 9
+ do
+ for k in 1 2 3 4 5
+ do
+ printf "$1\n"
+ done
+ done
+ done >>file
+}
+
+test_expect_success 'create binary file with changes' '
+ make_file "\\0" &&
+ git add file &&
+ make_file "\\01"
+'
+
+test_expect_success 'vanilla diff is binary' '
+ git diff >diff &&
+ grep "Binary files a/file and b/file differ" diff
+'
+
+test_expect_success 'rewrite diff is binary' '
+ git diff -B >diff &&
+ grep "dissimilarity index" diff &&
+ grep "Binary files a/file and b/file differ" diff
+'
+
+test_expect_success 'rewrite diff can show binary patch' '
+ git diff -B --binary >diff &&
+ grep "dissimilarity index" diff &&
+ grep "GIT binary patch" diff
+'
+
+test_expect_success 'rewrite diff --numstat shows binary changes' '
+ git diff -B --numstat --summary >diff &&
+ grep -e "- - " diff &&
+ grep " rewrite file" diff
+'
+
+test_expect_success 'diff --stat counts binary rewrite as 0 lines' '
+ git diff -B --stat --summary >diff &&
+ grep "Bin" diff &&
+ test_grep "0 insertions.*0 deletions" diff &&
+ grep " rewrite file" diff
+'
+
+{
+ echo "#!$SHELL_PATH"
+ cat <<'EOF'
+"$PERL_PATH" -e '$/ = undef; $_ = <>; s/./ord($&)/ge; print $_' < "$1"
+EOF
+} >dump
+chmod +x dump
+
+test_expect_success 'setup textconv' '
+ echo file diff=foo >.gitattributes &&
+ git config diff.foo.textconv "\"$(pwd)\""/dump
+'
+
+test_expect_success 'rewrite diff respects textconv' '
+ git diff -B >diff &&
+ grep "dissimilarity index" diff &&
+ grep "^-61" diff &&
+ grep "^-0" diff
+'
+
+test_done
diff --git a/t/t4032-diff-inter-hunk-context.sh b/t/t4032-diff-inter-hunk-context.sh
new file mode 100755
index 0000000..7db92d0
--- /dev/null
+++ b/t/t4032-diff-inter-hunk-context.sh
@@ -0,0 +1,118 @@
+#!/bin/sh
+
+test_description='diff hunk fusing'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+f() {
+ echo $1
+ i=1
+ while test $i -le $2
+ do
+ echo $i
+ i=$(expr $i + 1)
+ done
+ echo $3
+}
+
+t() {
+ use_config=
+ git config --unset diff.interHunkContext
+
+ case $# in
+ 4) hunks=$4; cmd="diff -U$3";;
+ 5) hunks=$5; cmd="diff -U$3 --inter-hunk-context=$4";;
+ 6) hunks=$5; cmd="diff -U$3"; git config diff.interHunkContext $4; use_config="(diff.interHunkContext=$4) ";;
+ esac
+ label="$use_config$cmd, $1 common $2"
+ file=f$1
+ expected=expected.$file.$3.$hunks
+
+ if ! test -f $file
+ then
+ f A $1 B >$file
+ git add $file
+ git commit -q -m. $file
+ f X $1 Y >$file
+ fi
+
+ test_expect_success "$label: count hunks ($hunks)" "
+ test $(git $cmd $file | grep '^@@ ' | wc -l) = $hunks
+ "
+
+ test -f $expected &&
+ test_expect_success "$label: check output" "
+ git $cmd $file | grep -v '^index ' >actual &&
+ test_cmp $expected actual
+ "
+}
+
+cat <<EOF >expected.f1.0.1 || exit 1
+diff --git a/f1 b/f1
+--- a/f1
++++ b/f1
+@@ -1,3 +1,3 @@
+-A
++X
+ 1
+-B
++Y
+EOF
+
+cat <<EOF >expected.f1.0.2 || exit 1
+diff --git a/f1 b/f1
+--- a/f1
++++ b/f1
+@@ -1 +1 @@
+-A
++X
+@@ -3 +3 @@ A
+-B
++Y
+EOF
+
+# common lines ctx intrctx hunks
+t 1 line 0 2
+t 1 line 0 0 2
+t 1 line 0 1 1
+t 1 line 0 2 1
+t 1 line 1 1
+
+t 2 lines 0 2
+t 2 lines 0 0 2
+t 2 lines 0 1 2
+t 2 lines 0 2 1
+t 2 lines 1 1
+
+t 3 lines 1 2
+t 3 lines 1 0 2
+t 3 lines 1 1 1
+t 3 lines 1 2 1
+
+t 9 lines 3 2
+t 9 lines 3 2 2
+t 9 lines 3 3 1
+
+# use diff.interHunkContext?
+t 1 line 0 0 2 config
+t 1 line 0 1 1 config
+t 1 line 0 2 1 config
+t 9 lines 3 3 1 config
+t 2 lines 0 0 2 config
+t 2 lines 0 1 2 config
+t 2 lines 0 2 1 config
+t 3 lines 1 0 2 config
+t 3 lines 1 1 1 config
+t 3 lines 1 2 1 config
+t 9 lines 3 2 2 config
+t 9 lines 3 3 1 config
+
+test_expect_success 'diff.interHunkContext invalid' '
+ git config diff.interHunkContext asdf &&
+ test_must_fail git diff &&
+ git config diff.interHunkContext -1 &&
+ test_must_fail git diff
+'
+
+test_done
diff --git a/t/t4033-diff-patience.sh b/t/t4033-diff-patience.sh
new file mode 100755
index 0000000..f7be7f5
--- /dev/null
+++ b/t/t4033-diff-patience.sh
@@ -0,0 +1,21 @@
+#!/bin/sh
+
+test_description='patience diff algorithm'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-diff-alternative.sh
+
+test_expect_success '--ignore-space-at-eol with a single appended character' '
+ printf "a\nb\nc\n" >pre &&
+ printf "a\nbX\nc\n" >post &&
+ test_must_fail git diff --no-index \
+ --patience --ignore-space-at-eol pre post >diff &&
+ grep "^+.*X" diff
+'
+
+test_diff_frobnitz "patience"
+
+test_diff_unique "patience"
+
+test_done
diff --git a/t/t4034-diff-words.sh b/t/t4034-diff-words.sh
new file mode 100755
index 0000000..74586f3
--- /dev/null
+++ b/t/t4034-diff-words.sh
@@ -0,0 +1,416 @@
+#!/bin/sh
+
+test_description='word diff colors'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-diff.sh
+
+cat >pre.simple <<-\EOF
+ h(4)
+
+ a = b + c
+EOF
+cat >post.simple <<-\EOF
+ h(4),hh[44]
+
+ a = b + c
+
+ aa = a
+
+ aeff = aeff * ( aaa )
+EOF
+pre=$(git rev-parse --short $(git hash-object pre.simple))
+post=$(git rev-parse --short $(git hash-object post.simple))
+cat >expect.letter-runs-are-words <<-EOF
+ <BOLD>diff --git a/pre b/post<RESET>
+ <BOLD>index $pre..$post 100644<RESET>
+ <BOLD>--- a/pre<RESET>
+ <BOLD>+++ b/post<RESET>
+ <CYAN>@@ -1,3 +1,7 @@<RESET>
+ h(4),<GREEN>hh<RESET>[44]
+
+ a = b + c<RESET>
+
+ <GREEN>aa = a<RESET>
+
+ <GREEN>aeff = aeff * ( aaa<RESET> )
+EOF
+cat >expect.non-whitespace-is-word <<-EOF
+ <BOLD>diff --git a/pre b/post<RESET>
+ <BOLD>index $pre..$post 100644<RESET>
+ <BOLD>--- a/pre<RESET>
+ <BOLD>+++ b/post<RESET>
+ <CYAN>@@ -1,3 +1,7 @@<RESET>
+ h(4)<GREEN>,hh[44]<RESET>
+
+ a = b + c<RESET>
+
+ <GREEN>aa = a<RESET>
+
+ <GREEN>aeff = aeff * ( aaa )<RESET>
+EOF
+
+word_diff () {
+ pre=$(git rev-parse --short $(git hash-object pre)) &&
+ post=$(git rev-parse --short $(git hash-object post)) &&
+ test_must_fail git diff --no-index "$@" pre post >output &&
+ test_decode_color <output >output.decrypted &&
+ sed -e "2s/index [^ ]*/index $pre..$post/" expect >expected
+ test_cmp expected output.decrypted
+}
+
+test_language_driver () {
+ lang=$1
+ test_expect_success "diff driver '$lang'" '
+ cp "$TEST_DIRECTORY/t4034/'"$lang"'/pre" \
+ "$TEST_DIRECTORY/t4034/'"$lang"'/post" \
+ "$TEST_DIRECTORY/t4034/'"$lang"'/expect" . &&
+ echo "* diff='"$lang"'" >.gitattributes &&
+ word_diff --color-words
+ '
+ test_expect_success "diff driver '$lang' in Islandic" '
+ LANG=is_IS.UTF-8 LANGUAGE=is LC_ALL="$is_IS_locale" \
+ word_diff --color-words
+ '
+}
+
+test_expect_success setup '
+ git config diff.color.old red &&
+ git config diff.color.new green &&
+ git config diff.color.func magenta
+'
+
+test_expect_success 'set up pre and post with runs of whitespace' '
+ cp pre.simple pre &&
+ cp post.simple post
+'
+
+test_expect_success 'word diff with runs of whitespace' '
+ cat >expect <<-EOF &&
+ <BOLD>diff --git a/pre b/post<RESET>
+ <BOLD>index $pre..$post 100644<RESET>
+ <BOLD>--- a/pre<RESET>
+ <BOLD>+++ b/post<RESET>
+ <CYAN>@@ -1,3 +1,7 @@<RESET>
+ <RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
+
+ a = b + c<RESET>
+
+ <GREEN>aa = a<RESET>
+
+ <GREEN>aeff = aeff * ( aaa )<RESET>
+ EOF
+ word_diff --color-words &&
+ word_diff --word-diff=color &&
+ word_diff --color --word-diff=color
+'
+
+test_expect_success '--word-diff=porcelain' '
+ sed "s/#.*$//" >expect <<-EOF &&
+ diff --git a/pre b/post
+ index $pre..$post 100644
+ --- a/pre
+ +++ b/post
+ @@ -1,3 +1,7 @@
+ -h(4)
+ +h(4),hh[44]
+ ~
+ # significant space
+ ~
+ a = b + c
+ ~
+ ~
+ +aa = a
+ ~
+ ~
+ +aeff = aeff * ( aaa )
+ ~
+ EOF
+ word_diff --word-diff=porcelain
+'
+
+test_expect_success '--word-diff=plain' '
+ cat >expect <<-EOF &&
+ diff --git a/pre b/post
+ index $pre..$post 100644
+ --- a/pre
+ +++ b/post
+ @@ -1,3 +1,7 @@
+ [-h(4)-]{+h(4),hh[44]+}
+
+ a = b + c
+
+ {+aa = a+}
+
+ {+aeff = aeff * ( aaa )+}
+ EOF
+ word_diff --word-diff=plain &&
+ word_diff --word-diff=plain --no-color
+'
+
+test_expect_success '--word-diff=plain --color' '
+ cat >expect <<-EOF &&
+ <BOLD>diff --git a/pre b/post<RESET>
+ <BOLD>index $pre..$post 100644<RESET>
+ <BOLD>--- a/pre<RESET>
+ <BOLD>+++ b/post<RESET>
+ <CYAN>@@ -1,3 +1,7 @@<RESET>
+ <RED>[-h(4)-]<RESET><GREEN>{+h(4),hh[44]+}<RESET>
+
+ a = b + c<RESET>
+
+ <GREEN>{+aa = a+}<RESET>
+
+ <GREEN>{+aeff = aeff * ( aaa )+}<RESET>
+ EOF
+ word_diff --word-diff=plain --color
+'
+
+test_expect_success 'word diff without context' '
+ cat >expect <<-EOF &&
+ <BOLD>diff --git a/pre b/post<RESET>
+ <BOLD>index $pre..$post 100644<RESET>
+ <BOLD>--- a/pre<RESET>
+ <BOLD>+++ b/post<RESET>
+ <CYAN>@@ -1 +1 @@<RESET>
+ <RED>h(4)<RESET><GREEN>h(4),hh[44]<RESET>
+ <CYAN>@@ -3,0 +4,4 @@<RESET> <RESET><MAGENTA>a = b + c<RESET>
+
+ <GREEN>aa = a<RESET>
+
+ <GREEN>aeff = aeff * ( aaa )<RESET>
+ EOF
+ word_diff --color-words --unified=0
+'
+
+test_expect_success 'word diff with a regular expression' '
+ cp expect.letter-runs-are-words expect &&
+ word_diff --color-words="[a-z]+"
+'
+
+test_expect_success 'word diff with zero length matches' '
+ cp expect.letter-runs-are-words expect &&
+ word_diff --color-words="[a-z${LF}]*"
+'
+
+test_expect_success 'set up a diff driver' '
+ git config diff.testdriver.wordRegex "[^[:space:]]" &&
+ cat <<-\EOF >.gitattributes
+ pre diff=testdriver
+ post diff=testdriver
+ EOF
+'
+
+test_expect_success 'option overrides .gitattributes' '
+ cp expect.letter-runs-are-words expect &&
+ word_diff --color-words="[a-z]+"
+'
+
+test_expect_success 'use regex supplied by driver' '
+ cp expect.non-whitespace-is-word expect &&
+ word_diff --color-words
+'
+
+test_expect_success 'set up diff.wordRegex option' '
+ git config diff.wordRegex "[[:alnum:]]+"
+'
+
+test_expect_success 'command-line overrides config' '
+ cp expect.letter-runs-are-words expect &&
+ word_diff --color-words="[a-z]+"
+'
+
+test_expect_success 'command-line overrides config: --word-diff-regex' '
+ cat >expect <<-EOF &&
+ <BOLD>diff --git a/pre b/post<RESET>
+ <BOLD>index $pre..$post 100644<RESET>
+ <BOLD>--- a/pre<RESET>
+ <BOLD>+++ b/post<RESET>
+ <CYAN>@@ -1,3 +1,7 @@<RESET>
+ h(4),<GREEN>{+hh+}<RESET>[44]
+
+ a = b + c<RESET>
+
+ <GREEN>{+aa = a+}<RESET>
+
+ <GREEN>{+aeff = aeff * ( aaa+}<RESET> )
+ EOF
+ word_diff --color --word-diff-regex="[a-z]+"
+'
+
+test_expect_success '.gitattributes override config' '
+ cp expect.non-whitespace-is-word expect &&
+ word_diff --color-words
+'
+
+test_expect_success 'setup: remove diff driver regex' '
+ test_unconfig diff.testdriver.wordRegex
+'
+
+test_expect_success 'use configured regex' '
+ cat >expect <<-EOF &&
+ <BOLD>diff --git a/pre b/post<RESET>
+ <BOLD>index $pre..$post 100644<RESET>
+ <BOLD>--- a/pre<RESET>
+ <BOLD>+++ b/post<RESET>
+ <CYAN>@@ -1,3 +1,7 @@<RESET>
+ h(4),<GREEN>hh[44<RESET>]
+
+ a = b + c<RESET>
+
+ <GREEN>aa = a<RESET>
+
+ <GREEN>aeff = aeff * ( aaa<RESET> )
+ EOF
+ word_diff --color-words
+'
+
+test_expect_success 'test parsing words for newline' '
+ echo "aaa (aaa)" >pre &&
+ echo "aaa (aaa) aaa" >post &&
+ pre=$(git rev-parse --short $(git hash-object pre)) &&
+ post=$(git rev-parse --short $(git hash-object post)) &&
+ cat >expect <<-EOF &&
+ <BOLD>diff --git a/pre b/post<RESET>
+ <BOLD>index $pre..$post 100644<RESET>
+ <BOLD>--- a/pre<RESET>
+ <BOLD>+++ b/post<RESET>
+ <CYAN>@@ -1 +1 @@<RESET>
+ aaa (aaa) <GREEN>aaa<RESET>
+ EOF
+ word_diff --color-words="a+"
+'
+
+test_expect_success 'test when words are only removed at the end' '
+ echo "(:" >pre &&
+ echo "(" >post &&
+ pre=$(git rev-parse --short $(git hash-object pre)) &&
+ post=$(git rev-parse --short $(git hash-object post)) &&
+ cat >expect <<-EOF &&
+ <BOLD>diff --git a/pre b/post<RESET>
+ <BOLD>index $pre..$post 100644<RESET>
+ <BOLD>--- a/pre<RESET>
+ <BOLD>+++ b/post<RESET>
+ <CYAN>@@ -1 +1 @@<RESET>
+ (<RED>:<RESET>
+ EOF
+ word_diff --color-words=.
+'
+
+test_expect_success '--word-diff=none' '
+ echo "(:" >pre &&
+ echo "(" >post &&
+ pre=$(git rev-parse --short $(git hash-object pre)) &&
+ post=$(git rev-parse --short $(git hash-object post)) &&
+ cat >expect <<-EOF &&
+ diff --git a/pre b/post
+ index $pre..$post 100644
+ --- a/pre
+ +++ b/post
+ @@ -1 +1 @@
+ -(:
+ +(
+ EOF
+ word_diff --word-diff=plain --word-diff=none
+'
+
+test_expect_success 'unset default driver' '
+ test_unconfig diff.wordregex
+'
+
+test_language_driver ada
+test_language_driver bibtex
+test_language_driver cpp
+test_language_driver csharp
+test_language_driver css
+test_language_driver dts
+test_language_driver fortran
+test_language_driver html
+test_language_driver java
+test_language_driver kotlin
+test_language_driver matlab
+test_language_driver objc
+test_language_driver pascal
+test_language_driver perl
+test_language_driver php
+test_language_driver python
+test_language_driver ruby
+test_language_driver scheme
+test_language_driver tex
+
+test_expect_success 'word-diff with diff.sbe' '
+ cat >pre <<-\EOF &&
+ a
+
+ b
+ EOF
+ cat >post <<-\EOF &&
+ a
+
+ c
+ EOF
+ pre=$(git rev-parse --short $(git hash-object pre)) &&
+ post=$(git rev-parse --short $(git hash-object post)) &&
+ cat >expect <<-EOF &&
+ diff --git a/pre b/post
+ index $pre..$post 100644
+ --- a/pre
+ +++ b/post
+ @@ -1,3 +1,3 @@
+ a
+
+ [-b-]{+c+}
+ EOF
+ test_config diff.suppress-blank-empty true &&
+ word_diff --word-diff=plain
+'
+
+test_expect_success 'word-diff with no newline at EOF' '
+ printf "%s" "a a a a a" >pre &&
+ printf "%s" "a a ab a a" >post &&
+ pre=$(git rev-parse --short $(git hash-object pre)) &&
+ post=$(git rev-parse --short $(git hash-object post)) &&
+ cat >expect <<-EOF &&
+ diff --git a/pre b/post
+ index $pre..$post 100644
+ --- a/pre
+ +++ b/post
+ @@ -1 +1 @@
+ a a [-a-]{+ab+} a a
+ EOF
+ word_diff --word-diff=plain
+'
+
+test_expect_success 'setup history with two files' '
+ echo "a b; c" >a.tex &&
+ echo "a b; c" >z.txt &&
+ git add a.tex z.txt &&
+ git commit -minitial &&
+
+ # modify both
+ echo "a bx; c" >a.tex &&
+ echo "a bx; c" >z.txt &&
+ git commit -mmodified -a
+'
+
+test_expect_success 'wordRegex for the first file does not apply to the second' '
+ echo "*.tex diff=tex" >.gitattributes &&
+ test_config diff.tex.wordRegex "[a-z]+|." &&
+ cat >expect <<-\EOF &&
+ diff --git a/a.tex b/a.tex
+ --- a/a.tex
+ +++ b/a.tex
+ @@ -1 +1 @@
+ a [-b-]{+bx+}; c
+ diff --git a/z.txt b/z.txt
+ --- a/z.txt
+ +++ b/z.txt
+ @@ -1 +1 @@
+ a [-b;-]{+bx;+} c
+ EOF
+ git diff --word-diff HEAD~ >actual &&
+ compare_diff_patch expect actual
+'
+
+test_done
diff --git a/t/t4034/ada/expect b/t/t4034/ada/expect
new file mode 100644
index 0000000..a682d28
--- /dev/null
+++ b/t/t4034/ada/expect
@@ -0,0 +1,27 @@
+<BOLD>diff --git a/pre b/post<RESET>
+<BOLD>index d96fdd1..df21bb0 100644<RESET>
+<BOLD>--- a/pre<RESET>
+<BOLD>+++ b/post<RESET>
+<CYAN>@@ -1,13 +1,13 @@<RESET>
+Ada.Text_IO.Put_Line("Hello World<RED>!<RESET><GREEN>?<RESET>");
+1 <RED>1e-10<RESET><GREEN>1e10<RESET> 16#FE12#E2 3.141_592 '<RED>x<RESET><GREEN>y<RESET>'
+<RED>a<RESET><GREEN>x<RESET>+<RED>b a<RESET><GREEN>y x<RESET>-<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>*<RED>b a<RESET><GREEN>y x<RESET>/<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>**<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>(<RED>b<RESET><GREEN>y<RESET>)
+<RED>a<RESET><GREEN>x<RESET>:=<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>=<RED>b a<RESET><GREEN>y x<RESET>/= <RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET><<RED>b a<RESET><GREEN>y x<RESET><=<RED>b a<RESET><GREEN>y x<RESET>><RED>b a<RESET><GREEN>y x<RESET>>=<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>,<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>=><RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>..<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET><><RED>b<RESET><GREEN>y<RESET>
diff --git a/t/t4034/ada/post b/t/t4034/ada/post
new file mode 100644
index 0000000..df21bb0
--- /dev/null
+++ b/t/t4034/ada/post
@@ -0,0 +1,13 @@
+Ada.Text_IO.Put_Line("Hello World?");
+1 1e10 16#FE12#E2 3.141_592 'y'
+x+y x-y
+x*y x/y
+x**y
+x(y)
+x:=y
+x=y x/= y
+x<y x<=y x>y x>=y
+x,y
+x=>y
+x..y
+x<>y
diff --git a/t/t4034/ada/pre b/t/t4034/ada/pre
new file mode 100644
index 0000000..d96fdd1
--- /dev/null
+++ b/t/t4034/ada/pre
@@ -0,0 +1,13 @@
+Ada.Text_IO.Put_Line("Hello World!");
+1 1e-10 16#FE12#E2 3.141_592 'x'
+a+b a-b
+a*b a/b
+a**b
+a(b)
+a:=b
+a=b a/= b
+a<b a<=b a>b a>=b
+a,b
+a=>b
+a..b
+a<>b
diff --git a/t/t4034/bibtex/expect b/t/t4034/bibtex/expect
new file mode 100644
index 0000000..a157774
--- /dev/null
+++ b/t/t4034/bibtex/expect
@@ -0,0 +1,15 @@
+<BOLD>diff --git a/pre b/post<RESET>
+<BOLD>index 95cd55b..ddcba9b 100644<RESET>
+<BOLD>--- a/pre<RESET>
+<BOLD>+++ b/post<RESET>
+<CYAN>@@ -1,9 +1,10 @@<RESET>
+@article{aldous1987uie,<RESET>
+ title={{Ultimate instability of exponential back-off protocol for acknowledgment-based transmission control of random access communication channels}},<RESET>
+ author={Aldous, <RED>D.<RESET><GREEN>David<RESET>},
+ journal={Information Theory, IEEE Transactions on},<RESET>
+ volume={<RED>33<RESET><GREEN>Bogus.<RESET>},
+ number={<RED>2<RESET><GREEN>4<RESET>},
+ pages={219--223},<RESET>
+ year=<GREEN>1987,<RESET>
+<GREEN> note={This is in fact a rather funny read since ethernet works well in practice. The<RESET> {<RED>1987<RESET><GREEN>\em pre} reference is the right one, however.<RESET>}<RED>,<RESET>
+}<RESET>
diff --git a/t/t4034/bibtex/post b/t/t4034/bibtex/post
new file mode 100644
index 0000000..ddcba9b
--- /dev/null
+++ b/t/t4034/bibtex/post
@@ -0,0 +1,10 @@
+@article{aldous1987uie,
+ title={{Ultimate instability of exponential back-off protocol for acknowledgment-based transmission control of random access communication channels}},
+ author={Aldous, David},
+ journal={Information Theory, IEEE Transactions on},
+ volume={Bogus.},
+ number={4},
+ pages={219--223},
+ year=1987,
+ note={This is in fact a rather funny read since ethernet works well in practice. The {\em pre} reference is the right one, however.}
+}
diff --git a/t/t4034/bibtex/pre b/t/t4034/bibtex/pre
new file mode 100644
index 0000000..95cd55b
--- /dev/null
+++ b/t/t4034/bibtex/pre
@@ -0,0 +1,9 @@
+@article{aldous1987uie,
+ title={{Ultimate instability of exponential back-off protocol for acknowledgment-based transmission control of random access communication channels}},
+ author={Aldous, D.},
+ journal={Information Theory, IEEE Transactions on},
+ volume={33},
+ number={2},
+ pages={219--223},
+ year={1987},
+}
diff --git a/t/t4034/cpp/expect b/t/t4034/cpp/expect
new file mode 100644
index 0000000..dc500ae
--- /dev/null
+++ b/t/t4034/cpp/expect
@@ -0,0 +1,35 @@
+<BOLD>diff --git a/pre b/post<RESET>
+<BOLD>index a1a09b7..f1b6f3c 100644<RESET>
+<BOLD>--- a/pre<RESET>
+<BOLD>+++ b/post<RESET>
+<CYAN>@@ -1,30 +1,30 @@<RESET>
+Foo() : x(0<RED>&&1<RESET><GREEN>&42<RESET>) { <RED>foo0<RESET><GREEN>bar<RESET>(x.<RED>find<RESET><GREEN>Find<RESET>); }
+cout<<"Hello World<RED>!<RESET><GREEN>?<RESET>\n"<<endl;
+<GREEN>(<RESET>1 <RED>-<RESET><GREEN>+<RESET>1e10 0xabcdef<GREEN>)<RESET> '<RED>x<RESET><GREEN>2<RESET>'
+// long double<RESET>
+<RED>3.141592653e-10l<RESET><GREEN>3.141592654e+10l<RESET>
+// float<RESET>
+<RED>120E5f<RESET><GREEN>120E6f<RESET>
+// hex<RESET>
+<RED>0xdead<RESET><GREEN>0xdeaf<RESET>'1<RED>eaF<RESET><GREEN>eaf<RESET>+<RED>8ULL<RESET><GREEN>7ULL<RESET>
+// octal<RESET>
+<RED>01234567<RESET><GREEN>01234560<RESET>
+// binary<RESET>
+<RED>0b1000<RESET><GREEN>0b1100<RESET>+e1
+// expression<RESET>
+1.5-e+<RED>2<RESET><GREEN>3<RESET>+f
+// another one<RESET>
+str.e+<RED>65<RESET><GREEN>75<RESET>
+[a] b<RED>-><RESET><GREEN>->*<RESET>v d<RED>.<RESET><GREEN>.*<RESET>e
+<GREEN>~<RESET>!a <GREEN>!<RESET>~b c<RED>++<RESET><GREEN>+<RESET> d<RED>--<RESET><GREEN>-<RESET> e*<GREEN>*<RESET>f g<RED>&<RESET><GREEN>&&<RESET>h
+a<RED>*<RESET><GREEN>*=<RESET>b c<RED>/<RESET><GREEN>/=<RESET>d e<RED>%<RESET><GREEN>%=<RESET>f
+a<RED>+<RESET><GREEN>++<RESET>b c<RED>-<RESET><GREEN>--<RESET>d
+a<RED><<<RESET><GREEN><<=<RESET>b c<RED>>><RESET><GREEN>>>=<RESET>d
+a<RED><<RESET><GREEN><=<RESET>b c<RED><=<RESET><GREEN><<RESET>d e<RED>><RESET><GREEN>>=<RESET>f g<RED>>=<RESET><GREEN>><RESET>h i<RED><=<RESET><GREEN><=><RESET>j
+a<RED>==<RESET><GREEN>!=<RESET>b c<RED>!=<RESET><GREEN>=<RESET>d
+a<RED>^<RESET><GREEN>^=<RESET>b c<RED>|<RESET><GREEN>|=<RESET>d e<RED>&&<RESET><GREEN>&=<RESET>f
+a<RED>||<RESET><GREEN>|<RESET>b
+a?<GREEN>:<RESET>b
+a<RED>=<RESET><GREEN>==<RESET>b c<RED>+=<RESET><GREEN>+<RESET>d e<RED>-=<RESET><GREEN>-<RESET>f g<RED>*=<RESET><GREEN>*<RESET>h i<RED>/=<RESET><GREEN>/<RESET>j k<RED>%=<RESET><GREEN>%<RESET>l m<RED><<=<RESET><GREEN><<<RESET>n o<RED>>>=<RESET><GREEN>>><RESET>p q<RED>&=<RESET><GREEN>&<RESET>r s<RED>^=<RESET><GREEN>^<RESET>t u<RED>|=<RESET><GREEN>|<RESET>v
+a,b<RESET>
+a<RED>::<RESET><GREEN>:<RESET>b
diff --git a/t/t4034/cpp/post b/t/t4034/cpp/post
new file mode 100644
index 0000000..f1b6f3c
--- /dev/null
+++ b/t/t4034/cpp/post
@@ -0,0 +1,30 @@
+Foo() : x(0&42) { bar(x.Find); }
+cout<<"Hello World?\n"<<endl;
+(1 +1e10 0xabcdef) '2'
+// long double
+3.141592654e+10l
+// float
+120E6f
+// hex
+0xdeaf'1eaf+7ULL
+// octal
+01234560
+// binary
+0b1100+e1
+// expression
+1.5-e+3+f
+// another one
+str.e+75
+[a] b->*v d.*e
+~!a !~b c+ d- e**f g&&h
+a*=b c/=d e%=f
+a++b c--d
+a<<=b c>>=d
+a<=b c<d e>=f g>h i<=>j
+a!=b c=d
+a^=b c|=d e&=f
+a|b
+a?:b
+a==b c+d e-f g*h i/j k%l m<<n o>>p q&r s^t u|v
+a,b
+a:b
diff --git a/t/t4034/cpp/pre b/t/t4034/cpp/pre
new file mode 100644
index 0000000..a1a09b7
--- /dev/null
+++ b/t/t4034/cpp/pre
@@ -0,0 +1,30 @@
+Foo():x(0&&1){ foo0( x.find); }
+cout<<"Hello World!\n"<<endl;
+1 -1e10 0xabcdef 'x'
+// long double
+3.141592653e-10l
+// float
+120E5f
+// hex
+0xdead'1eaF+8ULL
+// octal
+01234567
+// binary
+0b1000+e1
+// expression
+1.5-e+2+f
+// another one
+str.e+65
+[a] b->v d.e
+!a ~b c++ d-- e*f g&h
+a*b c/d e%f
+a+b c-d
+a<<b c>>d
+a<b c<=d e>f g>=h i<=j
+a==b c!=d
+a^b c|d e&&f
+a||b
+a?b
+a=b c+=d e-=f g*=h i/=j k%=l m<<=n o>>=p q&=r s^=t u|=v
+a,b
+a::b
diff --git a/t/t4034/csharp/expect b/t/t4034/csharp/expect
new file mode 100644
index 0000000..e5d1dd2
--- /dev/null
+++ b/t/t4034/csharp/expect
@@ -0,0 +1,35 @@
+<BOLD>diff --git a/pre b/post<RESET>
+<BOLD>index 9106d63..dd5f421 100644<RESET>
+<BOLD>--- a/pre<RESET>
+<BOLD>+++ b/post<RESET>
+<CYAN>@@ -1,18 +1,18 @@<RESET>
+Foo() : x(0<RED>&&1<RESET><GREEN>&42<RESET>) { <GREEN>bar(x);<RESET> }
+cout<<"Hello World<RED>!<RESET><GREEN>?<RESET>\n"<<endl;
+<GREEN>(<RESET>1<GREEN>) (<RESET>-1e10<GREEN>) (<RESET>0xabcdef<GREEN>)<RESET> '<RED>x<RESET><GREEN>y<RESET>'
+[<RED>a<RESET><GREEN>x<RESET>] <RED>a<RESET><GREEN>x<RESET>-><RED>b a<RESET><GREEN>y x<RESET>.<RED>b<RESET><GREEN>y<RESET>
+!<RED>a<RESET><GREEN>x<RESET> ~<RED>a a<RESET><GREEN>x x<RESET>++ <RED>a<RESET><GREEN>x<RESET>-- <RED>a<RESET><GREEN>x<RESET>*<RED>b a<RESET><GREEN>y x<RESET>&<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>*<RED>b a<RESET><GREEN>y x<RESET>/<RED>b a<RESET><GREEN>y x<RESET>%<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>+<RED>b a<RESET><GREEN>y x<RESET>-<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET><<<RED>b a<RESET><GREEN>y x<RESET>>><RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET><<RED>b a<RESET><GREEN>y x<RESET><=<RED>b a<RESET><GREEN>y x<RESET>><RED>b a<RESET><GREEN>y x<RESET>>=<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>==<RED>b a<RESET><GREEN>y x<RESET>!=<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>&<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>^<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>|<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>&&<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>||<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>?<RED>b<RESET><GREEN>y<RESET>:z
+<RED>a<RESET><GREEN>x<RESET>=<RED>b a<RESET><GREEN>y x<RESET>+=<RED>b a<RESET><GREEN>y x<RESET>-=<RED>b a<RESET><GREEN>y x<RESET>*=<RED>b a<RESET><GREEN>y x<RESET>/=<RED>b a<RESET><GREEN>y x<RESET>%=<RED>b a<RESET><GREEN>y x<RESET><<=<RED>b a<RESET><GREEN>y x<RESET>>>=<RED>b a<RESET><GREEN>y x<RESET>&=<RED>b a<RESET><GREEN>y x<RESET>^=<RED>b a<RESET><GREEN>y x<RESET>|=<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>,y
diff --git a/t/t4034/csharp/post b/t/t4034/csharp/post
new file mode 100644
index 0000000..dd5f421
--- /dev/null
+++ b/t/t4034/csharp/post
@@ -0,0 +1,18 @@
+Foo() : x(0&42) { bar(x); }
+cout<<"Hello World?\n"<<endl;
+(1) (-1e10) (0xabcdef) 'y'
+[x] x->y x.y
+!x ~x x++ x-- x*y x&y
+x*y x/y x%y
+x+y x-y
+x<<y x>>y
+x<y x<=y x>y x>=y
+x==y x!=y
+x&y
+x^y
+x|y
+x&&y
+x||y
+x?y:z
+x=y x+=y x-=y x*=y x/=y x%=y x<<=y x>>=y x&=y x^=y x|=y
+x,y
diff --git a/t/t4034/csharp/pre b/t/t4034/csharp/pre
new file mode 100644
index 0000000..9106d63
--- /dev/null
+++ b/t/t4034/csharp/pre
@@ -0,0 +1,18 @@
+Foo():x(0&&1){}
+cout<<"Hello World!\n"<<endl;
+1 -1e10 0xabcdef 'x'
+[a] a->b a.b
+!a ~a a++ a-- a*b a&b
+a*b a/b a%b
+a+b a-b
+a<<b a>>b
+a<b a<=b a>b a>=b
+a==b a!=b
+a&b
+a^b
+a|b
+a&&b
+a||b
+a?b:z
+a=b a+=b a-=b a*=b a/=b a%=b a<<=b a>>=b a&=b a^=b a|=b
+a,y
diff --git a/t/t4034/css/expect b/t/t4034/css/expect
new file mode 100644
index 0000000..ed10393
--- /dev/null
+++ b/t/t4034/css/expect
@@ -0,0 +1,16 @@
+<BOLD>diff --git a/pre b/post<RESET>
+<BOLD>index b8ae0bb..fe500b7 100644<RESET>
+<BOLD>--- a/pre<RESET>
+<BOLD>+++ b/post<RESET>
+<CYAN>@@ -1,10 +1,10 @@<RESET>
+.<RED>class-form<RESET><GREEN>other-form<RESET> label.control-label {
+ margin-top: <RED>10<RESET><GREEN>15<RESET>px!important;
+ border : 10px <RED>dashed<RESET><GREEN>dotted<RESET> #C6C6C6;
+}<RESET>
+<RED>#CCCCCC<RESET><GREEN>#CCCCCB<RESET>
+10em<RESET>
+<RED>padding-bottom<RESET><GREEN>margin-left<RESET>
+150<RED>px<RESET><GREEN>em<RESET>
+10px
+<RED>!important<RESET>
+<RED>div<RESET><GREEN>li<RESET>.class#id
diff --git a/t/t4034/css/post b/t/t4034/css/post
new file mode 100644
index 0000000..fe500b7
--- /dev/null
+++ b/t/t4034/css/post
@@ -0,0 +1,10 @@
+.other-form label.control-label {
+ margin-top: 15px!important;
+ border : 10px dotted #C6C6C6;
+}
+#CCCCCB
+10em
+margin-left
+150em
+10px
+li.class#id
diff --git a/t/t4034/css/pre b/t/t4034/css/pre
new file mode 100644
index 0000000..b8ae0bb
--- /dev/null
+++ b/t/t4034/css/pre
@@ -0,0 +1,10 @@
+.class-form label.control-label {
+ margin-top: 10px!important;
+ border : 10px dashed #C6C6C6;
+}
+#CCCCCC
+10em
+padding-bottom
+150px
+10px!important
+div.class#id
diff --git a/t/t4034/dts/expect b/t/t4034/dts/expect
new file mode 100644
index 0000000..560fc99
--- /dev/null
+++ b/t/t4034/dts/expect
@@ -0,0 +1,37 @@
+<BOLD>diff --git a/pre b/post<RESET>
+<BOLD>index b6a9051..7803aee 100644<RESET>
+<BOLD>--- a/pre<RESET>
+<BOLD>+++ b/post<RESET>
+<CYAN>@@ -1,32 +1,32 @@<RESET>
+/ {<RESET>
+ <RED>this_handle<RESET><GREEN>HANDLE_2<RESET>: <RED>node<RESET><GREEN>new-node<RESET>@<RED>f00<RESET><GREEN>eeda<RESET> {
+ compatible = "<RED>mydev<RESET><GREEN>vendor,compat<RESET>";
+ string-prop = <RED>start<RESET><GREEN>end<RESET>: "hello <RED>world!<RESET><GREEN>world?<RESET>" <RED>end<RESET><GREEN>start<RESET>: ;
+ <RED>#size-cells<RESET><GREEN>#address-cells<RESET> = <<RED>0+0<RESET><GREEN>0+40<RESET>>;
+ reg = <<RED>0xf00<RESET><GREEN>0xeeda<RESET>>;
+ prop = <<GREEN>(<RESET>1<GREEN>)<RESET>>;
+ prop = <<GREEN>(<RESET>-1e10<GREEN>)<RESET>>;
+ prop = <(!<RED>3<RESET><GREEN>1<RESET>)>;
+ prop = <(~<RED>3<RESET><GREEN>1<RESET>)>;
+ prop = <(<RED>3<RESET><GREEN>1<RESET>*<RED>4<RESET><GREEN>2<RESET>)>;
+ prop = <(<RED>3<RESET><GREEN>1<RESET>&<RED>4<RESET><GREEN>2<RESET>)>;
+ prop = <(<RED>3<RESET><GREEN>1<RESET>*<RED>4<RESET><GREEN>2<RESET>)>;
+ prop = <(<RED>3<RESET><GREEN>1<RESET>/<RED>4<RESET><GREEN>2<RESET>)>;
+ prop = <(<RED>3<RESET><GREEN>1<RESET>%<RED>4<RESET><GREEN>2<RESET>)>;
+ prop = <(<RED>3+4<RESET><GREEN>1+2<RESET>)>;
+ prop = <(<RED>3-4<RESET><GREEN>1-2<RESET>)>;
+ prop = /bits/ <RED>64<RESET><GREEN>32<RESET> <(<RED>3<RESET><GREEN>1<RESET><<<RED>4<RESET><GREEN>2<RESET>)>;
+ prop = <(<RED>3<RESET><GREEN>1<RESET>>><RED>4<RESET><GREEN>2<RESET>)>;
+ prop = <(<RED>3<RESET><GREEN>1<RESET>&<RED>4<RESET><GREEN>2<RESET>)>;
+ prop = <(<RED>3<RESET><GREEN>1<RESET>^<RED>4<RESET><GREEN>2<RESET>)>;
+ prop = <(<RED>3<RESET><GREEN>1<RESET>|<RED>4<RESET><GREEN>2<RESET>)>;
+ prop = <(<RED>3<RESET><GREEN>1<RESET>&&<RED>4<RESET><GREEN>2<RESET>)>;
+ prop = <(<RED>3<RESET><GREEN>1<RESET>||<RED>4<RESET><GREEN>2<RESET>)>;
+ prop = <(<RED>4?5<RESET><GREEN>1?2<RESET>:3)>;
+ list = <&<RED>this_handle<RESET><GREEN>HANDLE_2<RESET>>, <0 0 0 <RED>0<RESET><GREEN>1<RESET>>;
+ };<RESET>
+
+ &<RED>phandle<RESET><GREEN>phandle2<RESET> {
+ <RED>pre-phandle<RESET><GREEN>prop_handle<RESET> = <&<RED>this_handle<RESET><GREEN>HANDLE_2<RESET>>;
+ };<RESET>
+};<RESET>
diff --git a/t/t4034/dts/post b/t/t4034/dts/post
new file mode 100644
index 0000000..7803aee
--- /dev/null
+++ b/t/t4034/dts/post
@@ -0,0 +1,32 @@
+/ {
+ HANDLE_2: new-node@eeda {
+ compatible = "vendor,compat";
+ string-prop = end: "hello world?" start: ;
+ #address-cells = <0+40>;
+ reg = <0xeeda>;
+ prop = <(1)>;
+ prop = <(-1e10)>;
+ prop = <(!1)>;
+ prop = <(~1)>;
+ prop = <(1*2)>;
+ prop = <(1&2)>;
+ prop = <(1*2)>;
+ prop = <(1/2)>;
+ prop = <(1%2)>;
+ prop = <(1+2)>;
+ prop = <(1-2)>;
+ prop = /bits/ 32 <(1<<2)>;
+ prop = <(1>>2)>;
+ prop = <(1&2)>;
+ prop = <(1^2)>;
+ prop = <(1|2)>;
+ prop = <(1&&2)>;
+ prop = <(1||2)>;
+ prop = <(1?2:3)>;
+ list = <&HANDLE_2>, <0 0 0 1>;
+ };
+
+ &phandle2 {
+ prop_handle = <&HANDLE_2>;
+ };
+};
diff --git a/t/t4034/dts/pre b/t/t4034/dts/pre
new file mode 100644
index 0000000..b6a9051
--- /dev/null
+++ b/t/t4034/dts/pre
@@ -0,0 +1,32 @@
+/ {
+ this_handle: node@f00 {
+ compatible = "mydev";
+ string-prop = start: "hello world!" end: ;
+ #size-cells = <0+0>;
+ reg = <0xf00>;
+ prop = <1>;
+ prop = <-1e10>;
+ prop = <(!3)>;
+ prop = <(~3)>;
+ prop = <(3*4)>;
+ prop = <(3&4)>;
+ prop = <(3*4)>;
+ prop = <(3/4)>;
+ prop = <(3%4)>;
+ prop = <(3+4)>;
+ prop = <(3-4)>;
+ prop = /bits/ 64 <(3<<4)>;
+ prop = <(3>>4)>;
+ prop = <(3&4)>;
+ prop = <(3^4)>;
+ prop = <(3|4)>;
+ prop = <(3&&4)>;
+ prop = <(3||4)>;
+ prop = <(4?5:3)>;
+ list = <&this_handle>, <0 0 0 0>;
+ };
+
+ &phandle {
+ pre-phandle = <&this_handle>;
+ };
+};
diff --git a/t/t4034/fortran/expect b/t/t4034/fortran/expect
new file mode 100644
index 0000000..b233dbd
--- /dev/null
+++ b/t/t4034/fortran/expect
@@ -0,0 +1,10 @@
+<BOLD>diff --git a/pre b/post<RESET>
+<BOLD>index 87f0d0b..d308da2 100644<RESET>
+<BOLD>--- a/pre<RESET>
+<BOLD>+++ b/post<RESET>
+<CYAN>@@ -1,5 +1,5 @@<RESET>
+print *, "Hello World<RED>!<RESET><GREEN>?<RESET>"
+
+DO10I = 1,10<RESET>
+<RED>DO10I<RESET><GREEN>DO 10 I<RESET> = 1,10
+<RED>DO10I<RESET><GREEN>DO 1 0 I<RESET> = 1,10
diff --git a/t/t4034/fortran/post b/t/t4034/fortran/post
new file mode 100644
index 0000000..d308da2
--- /dev/null
+++ b/t/t4034/fortran/post
@@ -0,0 +1,5 @@
+print *, "Hello World?"
+
+DO10I = 1,10
+DO 10 I = 1,10
+DO 1 0 I = 1,10
diff --git a/t/t4034/fortran/pre b/t/t4034/fortran/pre
new file mode 100644
index 0000000..87f0d0b
--- /dev/null
+++ b/t/t4034/fortran/pre
@@ -0,0 +1,5 @@
+print *, "Hello World!"
+
+DO10I = 1,10
+DO10I = 1,10
+DO10I = 1,10
diff --git a/t/t4034/html/expect b/t/t4034/html/expect
new file mode 100644
index 0000000..447b49a
--- /dev/null
+++ b/t/t4034/html/expect
@@ -0,0 +1,8 @@
+<BOLD>diff --git a/pre b/post<RESET>
+<BOLD>index 8ca4aea..46921e5 100644<RESET>
+<BOLD>--- a/pre<RESET>
+<BOLD>+++ b/post<RESET>
+<CYAN>@@ -1,3 +1,3 @@<RESET>
+<tag <GREEN>newattr="newvalue"<RESET>><GREEN>added<RESET> content</tag>
+<tag attr=<RED>"value"<RESET><GREEN>"newvalue"<RESET>><RED>content<RESET><GREEN>changed<RESET></tag>
+<<RED>tag<RESET><GREEN>newtag<RESET>>content <RED>&entity;<RESET><GREEN>&newentity;<RESET><<RED>/tag<RESET><GREEN>/newtag<RESET>>
diff --git a/t/t4034/html/post b/t/t4034/html/post
new file mode 100644
index 0000000..46921e5
--- /dev/null
+++ b/t/t4034/html/post
@@ -0,0 +1,3 @@
+<tag newattr="newvalue">added content</tag>
+<tag attr="newvalue">changed</tag>
+<newtag>content &newentity;</newtag>
diff --git a/t/t4034/html/pre b/t/t4034/html/pre
new file mode 100644
index 0000000..8ca4aea
--- /dev/null
+++ b/t/t4034/html/pre
@@ -0,0 +1,3 @@
+<tag>content</tag>
+<tag attr="value">content</tag>
+<tag>content &entity;</tag>
diff --git a/t/t4034/java/expect b/t/t4034/java/expect
new file mode 100644
index 0000000..37d1ea2
--- /dev/null
+++ b/t/t4034/java/expect
@@ -0,0 +1,36 @@
+<BOLD>diff --git a/pre b/post<RESET>
+<BOLD>index 23d5c8a..7e8c026 100644<RESET>
+<BOLD>--- a/pre<RESET>
+<BOLD>+++ b/post<RESET>
+<CYAN>@@ -1,19 +1,19 @@<RESET>
+Foo() : x(0<RED>&&1<RESET><GREEN>&42<RESET>) { <GREEN>bar(x);<RESET> }
+cout<<"Hello World<RED>!<RESET><GREEN>?<RESET>\n"<<endl;
+<GREEN>(<RESET>1<GREEN>) (<RESET>-1e10<GREEN>) (<RESET>0xabcdef<GREEN>)<RESET> '<RED>x<RESET><GREEN>y<RESET>'
+[<RED>a<RESET><GREEN>x<RESET>] <RED>a<RESET><GREEN>x<RESET>-><RED>b a<RESET><GREEN>y x<RESET>.<RED>b<RESET><GREEN>y<RESET>
+!<RED>a<RESET><GREEN>x<RESET> ~<RED>a a<RESET><GREEN>x x<RESET>++ <RED>a<RESET><GREEN>x<RESET>-- <RED>a<RESET><GREEN>x<RESET>*<RED>b a<RESET><GREEN>y x<RESET>&<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>*<RED>b a<RESET><GREEN>y x<RESET>/<RED>b a<RESET><GREEN>y x<RESET>%<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>+<RED>b a<RESET><GREEN>y x<RESET>-<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET><<<RED>b a<RESET><GREEN>y x<RESET>>><RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET><<RED>b a<RESET><GREEN>y x<RESET><=<RED>b a<RESET><GREEN>y x<RESET>><RED>b a<RESET><GREEN>y x<RESET>>=<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>==<RED>b a<RESET><GREEN>y x<RESET>!=<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>&<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>^<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>|<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>&&<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>||<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>?<RED>b<RESET><GREEN>y<RESET>:z
+<RED>a<RESET><GREEN>x<RESET>=<RED>b a<RESET><GREEN>y x<RESET>+=<RED>b a<RESET><GREEN>y x<RESET>-=<RED>b a<RESET><GREEN>y x<RESET>*=<RED>b a<RESET><GREEN>y x<RESET>/=<RED>b a<RESET><GREEN>y x<RESET>%=<RED>b a<RESET><GREEN>y x<RESET><<=<RED>b a<RESET><GREEN>y x<RESET>>>=<RED>b a<RESET><GREEN>y x<RESET>&=<RED>b a<RESET><GREEN>y x<RESET>^=<RED>b a<RESET><GREEN>y x<RESET>|=<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>,y
+<RED>a<RESET><GREEN>x<RESET>::<RED>b<RESET><GREEN>y<RESET>
diff --git a/t/t4034/java/post b/t/t4034/java/post
new file mode 100644
index 0000000..7e8c026
--- /dev/null
+++ b/t/t4034/java/post
@@ -0,0 +1,19 @@
+Foo() : x(0&42) { bar(x); }
+cout<<"Hello World?\n"<<endl;
+(1) (-1e10) (0xabcdef) 'y'
+[x] x->y x.y
+!x ~x x++ x-- x*y x&y
+x*y x/y x%y
+x+y x-y
+x<<y x>>y
+x<y x<=y x>y x>=y
+x==y x!=y
+x&y
+x^y
+x|y
+x&&y
+x||y
+x?y:z
+x=y x+=y x-=y x*=y x/=y x%=y x<<=y x>>=y x&=y x^=y x|=y
+x,y
+x::y
diff --git a/t/t4034/java/pre b/t/t4034/java/pre
new file mode 100644
index 0000000..23d5c8a
--- /dev/null
+++ b/t/t4034/java/pre
@@ -0,0 +1,19 @@
+Foo():x(0&&1){}
+cout<<"Hello World!\n"<<endl;
+1 -1e10 0xabcdef 'x'
+[a] a->b a.b
+!a ~a a++ a-- a*b a&b
+a*b a/b a%b
+a+b a-b
+a<<b a>>b
+a<b a<=b a>b a>=b
+a==b a!=b
+a&b
+a^b
+a|b
+a&&b
+a||b
+a?b:z
+a=b a+=b a-=b a*=b a/=b a%=b a<<=b a>>=b a&=b a^=b a|=b
+a,y
+a::b
diff --git a/t/t4034/kotlin/expect b/t/t4034/kotlin/expect
new file mode 100644
index 0000000..7f76f75
--- /dev/null
+++ b/t/t4034/kotlin/expect
@@ -0,0 +1,43 @@
+<BOLD>diff --git a/pre b/post<RESET>
+<BOLD>index 11ea3de..2e1df4c 100644<RESET>
+<BOLD>--- a/pre<RESET>
+<BOLD>+++ b/post<RESET>
+<CYAN>@@ -1,30 +1,30 @@<RESET>
+println("Hello World<RED>!\n<RESET><GREEN>?<RESET>")
+<GREEN>(<RESET>1<GREEN>) (<RESET>-1e10<GREEN>) (<RESET>0xabcdef<GREEN>)<RESET> '<RED>x<RESET><GREEN>y<RESET>'
+[<RED>a<RESET><GREEN>x<RESET>] <RED>a<RESET><GREEN>x<RESET>-><RED>b a<RESET><GREEN>y x<RESET>.<RED>b<RESET><GREEN>y<RESET>
+!<RED>a a<RESET><GREEN>x x<RESET>.inv() <RED>a<RESET><GREEN>x<RESET>*<RED>b a<RESET><GREEN>y x<RESET>&<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>*<RED>b a<RESET><GREEN>y x<RESET>/<RED>b a<RESET><GREEN>y x<RESET>%<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>+<RED>b a<RESET><GREEN>y x<RESET>-<RED>b<RESET><GREEN>y<RESET>
+a <RED>shr<RESET><GREEN>shl<RESET> b
+<RED>a<RESET><GREEN>x<RESET><<RED>b a<RESET><GREEN>y x<RESET><=<RED>b a<RESET><GREEN>y x<RESET>><RED>b a<RESET><GREEN>y x<RESET>>=<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>==<RED>b a<RESET><GREEN>y x<RESET>!=<RED>b a<RESET><GREEN>y x<RESET>===<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET> and <RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>^<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET> or <RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>&&<RED>b a<RESET><GREEN>y x<RESET>||<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>=<RED>b a<RESET><GREEN>y x<RESET>+=<RED>b a<RESET><GREEN>y x<RESET>-=<RED>b a<RESET><GREEN>y x<RESET>*=<RED>b a<RESET><GREEN>y x<RESET>/=<RED>b a<RESET><GREEN>y x<RESET>%=<RED>b a<RESET><GREEN>y x<RESET><<=<RED>b a<RESET><GREEN>y x<RESET>>>=<RED>b a<RESET><GREEN>y x<RESET>&=<RED>b a<RESET><GREEN>y x<RESET>^=<RED>b a<RESET><GREEN>y x<RESET>|=<RED>b<RESET><GREEN>y<RESET>
+a<RED>=<RESET><GREEN>+=<RESET>b c<RED>+=<RESET><GREEN>=<RESET>d e<RED>-=<RESET><GREEN><=<RESET>f g<RED>*=<RESET><GREEN>>=<RESET>h i<RED>/=<RESET><GREEN>/<RESET>j k<RED>%=<RESET><GREEN>%<RESET>l m<RED><<=<RESET><GREEN><<<RESET>n o<RED>>>=<RESET><GREEN>>><RESET>p q<RED>&=<RESET><GREEN>&<RESET>r s<RED>^=<RESET><GREEN>^<RESET>t u<RED>|=<RESET><GREEN>|<RESET>v
+a<RED><<=<RESET><GREEN><=<RESET>b
+a<RED>||<RESET><GREEN>|<RESET>b a<RED>&&<RESET><GREEN>&<RESET>b
+<RED>a<RESET><GREEN>x<RESET>,y
+--a<RED>==<RESET><GREEN>!=<RESET>--b
+a++<RED>==<RESET><GREEN>!=<RESET>++b
+<RED>0xFF_EC_DE_5E 0b100_000 100_000<RESET><GREEN>0xFF_E1_DE_5E 0b100_100 200_000<RESET>
+a<RED>==<RESET><GREEN>===<RESET>b
+a<RED>!!<RESET><GREEN>!=<RESET>b
+<RED>_32<RESET><GREEN>_33<RESET>.find(arr)
+X.<RED>fill<RESET><GREEN>find<RESET>()
+X.<RED>u<RESET><GREEN>f<RESET>+1
+X.u<RED>-<RESET><GREEN>+<RESET>2
+a<RED>.<RESET><GREEN>..<RESET>b
+a<RED>?.<RESET><GREEN>?:<RESET>b
+<RED>.32_00_456<RESET><GREEN>.32_00_446<RESET>
diff --git a/t/t4034/kotlin/post b/t/t4034/kotlin/post
new file mode 100644
index 0000000..2e1df4c
--- /dev/null
+++ b/t/t4034/kotlin/post
@@ -0,0 +1,30 @@
+println("Hello World?")
+(1) (-1e10) (0xabcdef) 'y'
+[x] x->y x.y
+!x x.inv() x*y x&y
+x*y x/y x%y
+x+y x-y
+a shl b
+x<y x<=y x>y x>=y
+x==y x!=y x===y
+x and y
+x^y
+x or y
+x&&y x||y
+x=y x+=y x-=y x*=y x/=y x%=y x<<=y x>>=y x&=y x^=y x|=y
+a+=b c=d e<=f g>=h i/j k%l m<<n o>>p q&r s^t u|v
+a<=b
+a|b a&b
+x,y
+--a!=--b
+a++!=++b
+0xFF_E1_DE_5E 0b100_100 200_000
+a===b
+a!=b
+_33.find(arr)
+X.find()
+X.f+1
+X.u+2
+a..b
+a?:b
+.32_00_446
diff --git a/t/t4034/kotlin/pre b/t/t4034/kotlin/pre
new file mode 100644
index 0000000..11ea3de
--- /dev/null
+++ b/t/t4034/kotlin/pre
@@ -0,0 +1,30 @@
+println("Hello World!\n")
+1 -1e10 0xabcdef 'x'
+[a] a->b a.b
+!a a.inv() a*b a&b
+a*b a/b a%b
+a+b a-b
+a shr b
+a<b a<=b a>b a>=b
+a==b a!=b a===b
+a and b
+a^b
+a or b
+a&&b a||b
+a=b a+=b a-=b a*=b a/=b a%=b a<<=b a>>=b a&=b a^=b a|=b
+a=b c+=d e-=f g*=h i/=j k%=l m<<=n o>>=p q&=r s^=t u|=v
+a<<=b
+a||b a&&b
+a,y
+--a==--b
+a++==++b
+0xFF_EC_DE_5E 0b100_000 100_000
+a==b
+a!!b
+_32.find(arr)
+X.fill()
+X.u+1
+X.u-2
+a.b
+a?.b
+.32_00_456
diff --git a/t/t4034/matlab/expect b/t/t4034/matlab/expect
new file mode 100644
index 0000000..72cf3e9
--- /dev/null
+++ b/t/t4034/matlab/expect
@@ -0,0 +1,14 @@
+<BOLD>diff --git a/pre b/post<RESET>
+<BOLD>index dc204db..70e05f0 100644<RESET>
+<BOLD>--- a/pre<RESET>
+<BOLD>+++ b/post<RESET>
+<CYAN>@@ -1,9 +1,9 @@<RESET>
+(<RED>1<RESET><GREEN>0<RESET>) (<RED>-1e10<RESET><GREEN>-0e10<RESET>) '<RED>b<RESET><GREEN>y<RESET>';
+[<RED>a<RESET><GREEN>x<RESET>] {<RED>a<RESET><GREEN>x<RESET>} <RED>a<RESET><GREEN>x<RESET>.<RED>b<RESET><GREEN>y<RESET>;
+~<RED>a<RESET><GREEN>x<RESET>;
+<RED>a<RESET><GREEN>x<RESET>*<RED>b a<RESET><GREEN>y x<RESET>.*<RED>b a<RESET><GREEN>y x<RESET>/<RED>b a<RESET><GREEN>y x<RESET>./<RED>b a<RESET><GREEN>y x<RESET>^<RED>b a<RESET><GREEN>y x<RESET>.^<RED>b a<RESET><GREEN>y x<RESET>.\<RED>b a<RESET><GREEN>y x<RESET>.';
+<RED>a<RESET><GREEN>x<RESET>+<RED>b a<RESET><GREEN>y x<RESET>-<RED>b<RESET><GREEN>y<RESET>;
+<RED>a<RESET><GREEN>x<RESET>&<RED>b a<RESET><GREEN>y x<RESET>&&<RED>b a<RESET><GREEN>y x<RESET>|<RED>b a<RESET><GREEN>y x<RESET>||<RED>b<RESET><GREEN>y<RESET>;
+<RED>a<RESET><GREEN>x<RESET><<RED>b a<RESET><GREEN>y x<RESET><=<RED>b a<RESET><GREEN>y x<RESET>><RED>b a<RESET><GREEN>y x<RESET>>=<RED>b<RESET><GREEN>y<RESET>;
+<RED>a<RESET><GREEN>x<RESET>==<RED>b a<RESET><GREEN>y x<RESET>~=<RED>b<RESET><GREEN>y<RESET>;
+<RED>a<RESET><GREEN>x<RESET>,<RED>b<RESET><GREEN>y<RESET>;
diff --git a/t/t4034/matlab/post b/t/t4034/matlab/post
new file mode 100644
index 0000000..70e05f0
--- /dev/null
+++ b/t/t4034/matlab/post
@@ -0,0 +1,9 @@
+(0) (-0e10) 'y';
+[x] {x} x.y;
+~x;
+x*y x.*y x/y x./y x^y x.^y x.\y x.';
+x+y x-y;
+x&y x&&y x|y x||y;
+x<y x<=y x>y x>=y;
+x==y x~=y;
+x,y;
diff --git a/t/t4034/matlab/pre b/t/t4034/matlab/pre
new file mode 100644
index 0000000..dc204db
--- /dev/null
+++ b/t/t4034/matlab/pre
@@ -0,0 +1,9 @@
+(1) (-1e10) 'b';
+[a] {a} a.b;
+~a;
+a*b a.*b a/b a./b a^b a.^b a.\b a.';
+a+b a-b;
+a&b a&&b a|b a||b;
+a<b a<=b a>b a>=b;
+a==b a~=b;
+a,b;
diff --git a/t/t4034/objc/expect b/t/t4034/objc/expect
new file mode 100644
index 0000000..e5d1dd2
--- /dev/null
+++ b/t/t4034/objc/expect
@@ -0,0 +1,35 @@
+<BOLD>diff --git a/pre b/post<RESET>
+<BOLD>index 9106d63..dd5f421 100644<RESET>
+<BOLD>--- a/pre<RESET>
+<BOLD>+++ b/post<RESET>
+<CYAN>@@ -1,18 +1,18 @@<RESET>
+Foo() : x(0<RED>&&1<RESET><GREEN>&42<RESET>) { <GREEN>bar(x);<RESET> }
+cout<<"Hello World<RED>!<RESET><GREEN>?<RESET>\n"<<endl;
+<GREEN>(<RESET>1<GREEN>) (<RESET>-1e10<GREEN>) (<RESET>0xabcdef<GREEN>)<RESET> '<RED>x<RESET><GREEN>y<RESET>'
+[<RED>a<RESET><GREEN>x<RESET>] <RED>a<RESET><GREEN>x<RESET>-><RED>b a<RESET><GREEN>y x<RESET>.<RED>b<RESET><GREEN>y<RESET>
+!<RED>a<RESET><GREEN>x<RESET> ~<RED>a a<RESET><GREEN>x x<RESET>++ <RED>a<RESET><GREEN>x<RESET>-- <RED>a<RESET><GREEN>x<RESET>*<RED>b a<RESET><GREEN>y x<RESET>&<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>*<RED>b a<RESET><GREEN>y x<RESET>/<RED>b a<RESET><GREEN>y x<RESET>%<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>+<RED>b a<RESET><GREEN>y x<RESET>-<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET><<<RED>b a<RESET><GREEN>y x<RESET>>><RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET><<RED>b a<RESET><GREEN>y x<RESET><=<RED>b a<RESET><GREEN>y x<RESET>><RED>b a<RESET><GREEN>y x<RESET>>=<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>==<RED>b a<RESET><GREEN>y x<RESET>!=<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>&<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>^<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>|<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>&&<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>||<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>?<RED>b<RESET><GREEN>y<RESET>:z
+<RED>a<RESET><GREEN>x<RESET>=<RED>b a<RESET><GREEN>y x<RESET>+=<RED>b a<RESET><GREEN>y x<RESET>-=<RED>b a<RESET><GREEN>y x<RESET>*=<RED>b a<RESET><GREEN>y x<RESET>/=<RED>b a<RESET><GREEN>y x<RESET>%=<RED>b a<RESET><GREEN>y x<RESET><<=<RED>b a<RESET><GREEN>y x<RESET>>>=<RED>b a<RESET><GREEN>y x<RESET>&=<RED>b a<RESET><GREEN>y x<RESET>^=<RED>b a<RESET><GREEN>y x<RESET>|=<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>,y
diff --git a/t/t4034/objc/post b/t/t4034/objc/post
new file mode 100644
index 0000000..dd5f421
--- /dev/null
+++ b/t/t4034/objc/post
@@ -0,0 +1,18 @@
+Foo() : x(0&42) { bar(x); }
+cout<<"Hello World?\n"<<endl;
+(1) (-1e10) (0xabcdef) 'y'
+[x] x->y x.y
+!x ~x x++ x-- x*y x&y
+x*y x/y x%y
+x+y x-y
+x<<y x>>y
+x<y x<=y x>y x>=y
+x==y x!=y
+x&y
+x^y
+x|y
+x&&y
+x||y
+x?y:z
+x=y x+=y x-=y x*=y x/=y x%=y x<<=y x>>=y x&=y x^=y x|=y
+x,y
diff --git a/t/t4034/objc/pre b/t/t4034/objc/pre
new file mode 100644
index 0000000..9106d63
--- /dev/null
+++ b/t/t4034/objc/pre
@@ -0,0 +1,18 @@
+Foo():x(0&&1){}
+cout<<"Hello World!\n"<<endl;
+1 -1e10 0xabcdef 'x'
+[a] a->b a.b
+!a ~a a++ a-- a*b a&b
+a*b a/b a%b
+a+b a-b
+a<<b a>>b
+a<b a<=b a>b a>=b
+a==b a!=b
+a&b
+a^b
+a|b
+a&&b
+a||b
+a?b:z
+a=b a+=b a-=b a*=b a/=b a%=b a<<=b a>>=b a&=b a^=b a|=b
+a,y
diff --git a/t/t4034/pascal/expect b/t/t4034/pascal/expect
new file mode 100644
index 0000000..2ce4230
--- /dev/null
+++ b/t/t4034/pascal/expect
@@ -0,0 +1,35 @@
+<BOLD>diff --git a/pre b/post<RESET>
+<BOLD>index 077046c..8865e6b 100644<RESET>
+<BOLD>--- a/pre<RESET>
+<BOLD>+++ b/post<RESET>
+<CYAN>@@ -1,18 +1,18 @@<RESET>
+writeln("Hello World<RED>!<RESET><GREEN>?<RESET>");
+<GREEN>(<RESET>1<GREEN>) (<RESET>-1e10<GREEN>) (<RESET>0xabcdef<GREEN>)<RESET> '<RED>x<RESET><GREEN>y<RESET>'
+[<RED>a<RESET><GREEN>x<RESET>] <RED>a<RESET><GREEN>x<RESET>-><RED>b a<RESET><GREEN>y x<RESET>.<RED>b<RESET><GREEN>y<RESET>
+!<RED>a<RESET><GREEN>x<RESET> ~<RED>a a<RESET><GREEN>x x<RESET>++ <RED>a<RESET><GREEN>x<RESET>-- <RED>a<RESET><GREEN>x<RESET>*<RED>b a<RESET><GREEN>y x<RESET>&<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>*<RED>b a<RESET><GREEN>y x<RESET>/<RED>b a<RESET><GREEN>y x<RESET>%<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>+<RED>b a<RESET><GREEN>y x<RESET>-<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET><<<RED>b a<RESET><GREEN>y x<RESET>>><RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET><<RED>b a<RESET><GREEN>y x<RESET><=<RED>b a<RESET><GREEN>y x<RESET>><RED>b a<RESET><GREEN>y x<RESET>>=<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>==<RED>b a<RESET><GREEN>y x<RESET>!=<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>&<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>^<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>|<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>&&<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>||<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>?<RED>b<RESET><GREEN>y<RESET>:z
+<RED>a<RESET><GREEN>x<RESET>=<RED>b a<RESET><GREEN>y x<RESET>+=<RED>b a<RESET><GREEN>y x<RESET>-=<RED>b a<RESET><GREEN>y x<RESET>*=<RED>b a<RESET><GREEN>y x<RESET>/=<RED>b a<RESET><GREEN>y x<RESET>%=<RED>b a<RESET><GREEN>y x<RESET><<=<RED>b a<RESET><GREEN>y x<RESET>>>=<RED>b a<RESET><GREEN>y x<RESET>&=<RED>b a<RESET><GREEN>y x<RESET>^=<RED>b a<RESET><GREEN>y x<RESET>|=<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>,y
+<RED>a<RESET><GREEN>x<RESET>::<RED>b<RESET><GREEN>y<RESET>
diff --git a/t/t4034/pascal/post b/t/t4034/pascal/post
new file mode 100644
index 0000000..8865e6b
--- /dev/null
+++ b/t/t4034/pascal/post
@@ -0,0 +1,18 @@
+writeln("Hello World?");
+(1) (-1e10) (0xabcdef) 'y'
+[x] x->y x.y
+!x ~x x++ x-- x*y x&y
+x*y x/y x%y
+x+y x-y
+x<<y x>>y
+x<y x<=y x>y x>=y
+x==y x!=y
+x&y
+x^y
+x|y
+x&&y
+x||y
+x?y:z
+x=y x+=y x-=y x*=y x/=y x%=y x<<=y x>>=y x&=y x^=y x|=y
+x,y
+x::y
diff --git a/t/t4034/pascal/pre b/t/t4034/pascal/pre
new file mode 100644
index 0000000..077046c
--- /dev/null
+++ b/t/t4034/pascal/pre
@@ -0,0 +1,18 @@
+writeln("Hello World!");
+1 -1e10 0xabcdef 'x'
+[a] a->b a.b
+!a ~a a++ a-- a*b a&b
+a*b a/b a%b
+a+b a-b
+a<<b a>>b
+a<b a<=b a>b a>=b
+a==b a!=b
+a&b
+a^b
+a|b
+a&&b
+a||b
+a?b:z
+a=b a+=b a-=b a*=b a/=b a%=b a<<=b a>>=b a&=b a^=b a|=b
+a,y
+a::b
diff --git a/t/t4034/perl/expect b/t/t4034/perl/expect
new file mode 100644
index 0000000..a1deb6b
--- /dev/null
+++ b/t/t4034/perl/expect
@@ -0,0 +1,13 @@
+<BOLD>diff --git a/pre b/post<RESET>
+<BOLD>index f6610d3..e8b72ef 100644<RESET>
+<BOLD>--- a/pre<RESET>
+<BOLD>+++ b/post<RESET>
+<CYAN>@@ -4,8 +4,8 @@<RESET>
+
+package Frotz;<RESET>
+sub new {<RESET>
+ my <GREEN>(<RESET>$class<GREEN>, %opts)<RESET> = <RED>shift<RESET><GREEN>@_<RESET>;
+ return bless { <GREEN>xyzzy => "nitfol", %opts<RESET> }, $class;
+}<RESET>
+
+__END__<RESET>
diff --git a/t/t4034/perl/post b/t/t4034/perl/post
new file mode 100644
index 0000000..e8b72ef
--- /dev/null
+++ b/t/t4034/perl/post
@@ -0,0 +1,22 @@
+#!/usr/bin/perl
+
+use strict;
+
+package Frotz;
+sub new {
+ my ($class, %opts) = @_;
+ return bless { xyzzy => "nitfol", %opts }, $class;
+}
+
+__END__
+=head1 NAME
+
+frotz - Frotz
+
+=head1 SYNOPSIS
+
+ use frotz;
+
+ $nitfol = new Frotz();
+
+=cut
diff --git a/t/t4034/perl/pre b/t/t4034/perl/pre
new file mode 100644
index 0000000..f6610d3
--- /dev/null
+++ b/t/t4034/perl/pre
@@ -0,0 +1,22 @@
+#!/usr/bin/perl
+
+use strict;
+
+package Frotz;
+sub new {
+ my $class = shift;
+ return bless {}, $class;
+}
+
+__END__
+=head1 NAME
+
+frotz - Frotz
+
+=head1 SYNOPSIS
+
+ use frotz;
+
+ $nitfol = new Frotz();
+
+=cut
diff --git a/t/t4034/php/expect b/t/t4034/php/expect
new file mode 100644
index 0000000..0404408
--- /dev/null
+++ b/t/t4034/php/expect
@@ -0,0 +1,35 @@
+<BOLD>diff --git a/pre b/post<RESET>
+<BOLD>index cf6e06b..4420a49 100644<RESET>
+<BOLD>--- a/pre<RESET>
+<BOLD>+++ b/post<RESET>
+<CYAN>@@ -1,18 +1,18 @@<RESET>
+<GREEN>(<RESET>$var<GREEN>)<RESET> $ var
+<?="Hello World<RED>!<RESET><GREEN>?<RESET>"?>
+<GREEN>(<RESET>1<GREEN>) (<RESET>-1e10<GREEN>) (<RESET>0xabcdef<GREEN>)<RESET> '<RED>x<RESET><GREEN>y<RESET>'
+[<RED>a<RESET><GREEN>x<RESET>] <RED>a<RESET><GREEN>x<RESET>-><RED>b a<RESET><GREEN>y x<RESET>.<RED>b<RESET><GREEN>y<RESET>
+!<RED>a<RESET><GREEN>x<RESET> ~<RED>a a<RESET><GREEN>x x<RESET>++ <RED>a<RESET><GREEN>x<RESET>-- <RED>a<RESET><GREEN>x<RESET>*<RED>b a<RESET><GREEN>y x<RESET>&<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>*<RED>b a<RESET><GREEN>y x<RESET>/<RED>b a<RESET><GREEN>y x<RESET>%<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>+<RED>b a<RESET><GREEN>y x<RESET>-<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET><<<RED>b a<RESET><GREEN>y x<RESET>>><RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET><<RED>b a<RESET><GREEN>y x<RESET><=<RED>b a<RESET><GREEN>y x<RESET>><RED>b a<RESET><GREEN>y x<RESET>>=<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>==<RED>b a<RESET><GREEN>y x<RESET>!=<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>&<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>^<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>|<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>&&<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>||<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>?<RED>b<RESET><GREEN>y<RESET>:z
+<RED>a<RESET><GREEN>x<RESET>=<RED>b a<RESET><GREEN>y x<RESET>+=<RED>b a<RESET><GREEN>y x<RESET>-=<RED>b a<RESET><GREEN>y x<RESET>*=<RED>b a<RESET><GREEN>y x<RESET>/=<RED>b a<RESET><GREEN>y x<RESET>%=<RED>b a<RESET><GREEN>y x<RESET><<=<RED>b a<RESET><GREEN>y x<RESET>>>=<RED>b a<RESET><GREEN>y x<RESET>&=<RED>b a<RESET><GREEN>y x<RESET>^=<RED>b a<RESET><GREEN>y x<RESET>|=<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>,y
diff --git a/t/t4034/php/post b/t/t4034/php/post
new file mode 100644
index 0000000..4420a49
--- /dev/null
+++ b/t/t4034/php/post
@@ -0,0 +1,18 @@
+($var) $ var
+<?="Hello World?"?>
+(1) (-1e10) (0xabcdef) 'y'
+[x] x->y x.y
+!x ~x x++ x-- x*y x&y
+x*y x/y x%y
+x+y x-y
+x<<y x>>y
+x<y x<=y x>y x>=y
+x==y x!=y
+x&y
+x^y
+x|y
+x&&y
+x||y
+x?y:z
+x=y x+=y x-=y x*=y x/=y x%=y x<<=y x>>=y x&=y x^=y x|=y
+x,y
diff --git a/t/t4034/php/pre b/t/t4034/php/pre
new file mode 100644
index 0000000..cf6e06b
--- /dev/null
+++ b/t/t4034/php/pre
@@ -0,0 +1,18 @@
+$var $var
+<?= "Hello World!" ?>
+1 -1e10 0xabcdef 'x'
+[a] a->b a.b
+!a ~a a++ a-- a*b a&b
+a*b a/b a%b
+a+b a-b
+a<<b a>>b
+a<b a<=b a>b a>=b
+a==b a!=b
+a&b
+a^b
+a|b
+a&&b
+a||b
+a?b:z
+a=b a+=b a-=b a*=b a/=b a%=b a<<=b a>>=b a&=b a^=b a|=b
+a,y
diff --git a/t/t4034/python/expect b/t/t4034/python/expect
new file mode 100644
index 0000000..8abb8a4
--- /dev/null
+++ b/t/t4034/python/expect
@@ -0,0 +1,34 @@
+<BOLD>diff --git a/pre b/post<RESET>
+<BOLD>index 438f776..68baf34 100644<RESET>
+<BOLD>--- a/pre<RESET>
+<BOLD>+++ b/post<RESET>
+<CYAN>@@ -1,17 +1,17 @@<RESET>
+print<RED>u<RESET> "Hello World<RED>!<RESET><GREEN>?<RESET>\n"<GREEN>; print<RESET>
+<GREEN>(<RESET>1<GREEN>) (<RESET>-1e10<GREEN>) (<RESET>0xabcdef<GREEN>) u<RESET>'<RED>x<RESET><GREEN>y<RESET>'
+[<RED>a<RESET><GREEN>x<RESET>] <RED>a<RESET><GREEN>x<RESET>-><RED>b a<RESET><GREEN>y x<RESET>.<RED>b<RESET><GREEN>y<RESET>
+!<RED>a<RESET><GREEN>x<RESET> ~<RED>a a<RESET><GREEN>x x<RESET>++ <RED>a<RESET><GREEN>x<RESET>-- <RED>a<RESET><GREEN>x<RESET>*<RED>b a<RESET><GREEN>y x<RESET>&<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>*<RED>b a<RESET><GREEN>y x<RESET>/<RED>b a<RESET><GREEN>y x<RESET>%<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>+<RED>b a<RESET><GREEN>y x<RESET>-<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET><<<RED>b a<RESET><GREEN>y x<RESET>>><RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET><<RED>b a<RESET><GREEN>y x<RESET><=<RED>b a<RESET><GREEN>y x<RESET>><RED>b a<RESET><GREEN>y x<RESET>>=<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>==<RED>b a<RESET><GREEN>y x<RESET>!=<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>&<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>^<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>|<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>&&<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>||<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>?<RED>b<RESET><GREEN>y<RESET>:z
+<RED>a<RESET><GREEN>x<RESET>=<RED>b a<RESET><GREEN>y x<RESET>+=<RED>b a<RESET><GREEN>y x<RESET>-=<RED>b a<RESET><GREEN>y x<RESET>*=<RED>b a<RESET><GREEN>y x<RESET>/=<RED>b a<RESET><GREEN>y x<RESET>%=<RED>b a<RESET><GREEN>y x<RESET><<=<RED>b a<RESET><GREEN>y x<RESET>>>=<RED>b a<RESET><GREEN>y x<RESET>&=<RED>b a<RESET><GREEN>y x<RESET>^=<RED>b a<RESET><GREEN>y x<RESET>|=<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>,y
diff --git a/t/t4034/python/post b/t/t4034/python/post
new file mode 100644
index 0000000..68baf34
--- /dev/null
+++ b/t/t4034/python/post
@@ -0,0 +1,17 @@
+print "Hello World?\n"; print
+(1) (-1e10) (0xabcdef) u'y'
+[x] x->y x.y
+!x ~x x++ x-- x*y x&y
+x*y x/y x%y
+x+y x-y
+x<<y x>>y
+x<y x<=y x>y x>=y
+x==y x!=y
+x&y
+x^y
+x|y
+x&&y
+x||y
+x?y:z
+x=y x+=y x-=y x*=y x/=y x%=y x<<=y x>>=y x&=y x^=y x|=y
+x,y
diff --git a/t/t4034/python/pre b/t/t4034/python/pre
new file mode 100644
index 0000000..438f776
--- /dev/null
+++ b/t/t4034/python/pre
@@ -0,0 +1,17 @@
+print u"Hello World!\n"
+1 -1e10 0xabcdef 'x'
+[a] a->b a.b
+!a ~a a++ a-- a*b a&b
+a*b a/b a%b
+a+b a-b
+a<<b a>>b
+a<b a<=b a>b a>=b
+a==b a!=b
+a&b
+a^b
+a|b
+a&&b
+a||b
+a?b:z
+a=b a+=b a-=b a*=b a/=b a%=b a<<=b a>>=b a&=b a^=b a|=b
+a,y
diff --git a/t/t4034/ruby/expect b/t/t4034/ruby/expect
new file mode 100644
index 0000000..16e1dd5
--- /dev/null
+++ b/t/t4034/ruby/expect
@@ -0,0 +1,34 @@
+<BOLD>diff --git a/pre b/post<RESET>
+<BOLD>index 30ed9a1..7678f14 100644<RESET>
+<BOLD>--- a/pre<RESET>
+<BOLD>+++ b/post<RESET>
+<CYAN>@@ -1,17 +1,17 @@<RESET>
+10.downto(1) {|<RED>x<RESET><GREEN>y<RESET>| puts <RED>x<RESET><GREEN>y<RESET>}
+<GREEN>(<RESET>1<GREEN>) (<RESET>-1e10<GREEN>) (<RESET>0xabcdef<GREEN>)<RESET> '<RED>x<RESET><GREEN>y<RESET>'
+[<RED>a<RESET><GREEN>x<RESET>] <RED>a<RESET><GREEN>x<RESET>-><RED>b a<RESET><GREEN>y x<RESET>.<RED>b<RESET><GREEN>y<RESET>
+!<RED>a<RESET><GREEN>x<RESET> ~<RED>a a<RESET><GREEN>x x<RESET>++ <RED>a<RESET><GREEN>x<RESET>-- <RED>a<RESET><GREEN>x<RESET>*<RED>b a<RESET><GREEN>y x<RESET>&<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>*<RED>b a<RESET><GREEN>y x<RESET>/<RED>b a<RESET><GREEN>y x<RESET>%<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>+<RED>b a<RESET><GREEN>y x<RESET>-<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET><<<RED>b a<RESET><GREEN>y x<RESET>>><RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET><<RED>b a<RESET><GREEN>y x<RESET><=<RED>b a<RESET><GREEN>y x<RESET>><RED>b a<RESET><GREEN>y x<RESET>>=<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>==<RED>b a<RESET><GREEN>y x<RESET>!=<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>&<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>^<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>|<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>&&<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>||<RED>b<RESET>
+<RED>a?b<RESET><GREEN>y<RESET>
+<GREEN>x?y<RESET>:z
+<RED>a<RESET><GREEN>x<RESET>=<RED>b a<RESET><GREEN>y x<RESET>+=<RED>b a<RESET><GREEN>y x<RESET>-=<RED>b a<RESET><GREEN>y x<RESET>*=<RED>b a<RESET><GREEN>y x<RESET>/=<RED>b a<RESET><GREEN>y x<RESET>%=<RED>b a<RESET><GREEN>y x<RESET><<=<RED>b a<RESET><GREEN>y x<RESET>>>=<RED>b a<RESET><GREEN>y x<RESET>&=<RED>b a<RESET><GREEN>y x<RESET>^=<RED>b a<RESET><GREEN>y x<RESET>|=<RED>b<RESET>
+<RED>a<RESET><GREEN>y<RESET>
+<GREEN>x<RESET>,y
diff --git a/t/t4034/ruby/post b/t/t4034/ruby/post
new file mode 100644
index 0000000..7678f14
--- /dev/null
+++ b/t/t4034/ruby/post
@@ -0,0 +1,17 @@
+10.downto(1) {|y| puts y}
+(1) (-1e10) (0xabcdef) 'y'
+[x] x->y x.y
+!x ~x x++ x-- x*y x&y
+x*y x/y x%y
+x+y x-y
+x<<y x>>y
+x<y x<=y x>y x>=y
+x==y x!=y
+x&y
+x^y
+x|y
+x&&y
+x||y
+x?y:z
+x=y x+=y x-=y x*=y x/=y x%=y x<<=y x>>=y x&=y x^=y x|=y
+x,y
diff --git a/t/t4034/ruby/pre b/t/t4034/ruby/pre
new file mode 100644
index 0000000..30ed9a1
--- /dev/null
+++ b/t/t4034/ruby/pre
@@ -0,0 +1,17 @@
+10.downto(1) {|x| puts x}
+1 -1e10 0xabcdef 'x'
+[a] a->b a.b
+!a ~a a++ a-- a*b a&b
+a*b a/b a%b
+a+b a-b
+a<<b a>>b
+a<b a<=b a>b a>=b
+a==b a!=b
+a&b
+a^b
+a|b
+a&&b
+a||b
+a?b:z
+a=b a+=b a-=b a*=b a/=b a%=b a<<=b a>>=b a&=b a^=b a|=b
+a,y
diff --git a/t/t4034/scheme/expect b/t/t4034/scheme/expect
new file mode 100644
index 0000000..496cd5d
--- /dev/null
+++ b/t/t4034/scheme/expect
@@ -0,0 +1,11 @@
+<BOLD>diff --git a/pre b/post<RESET>
+<BOLD>index 74b6605..63b6ac4 100644<RESET>
+<BOLD>--- a/pre<RESET>
+<BOLD>+++ b/post<RESET>
+<CYAN>@@ -1,6 +1,6 @@<RESET>
+(define (<RED>myfunc a b<RESET><GREEN>my-func first second<RESET>)
+ ; This is a <RED>really<RESET><GREEN>(moderately)<RESET> cool function.
+ (<RED>this\place<RESET><GREEN>that\place<RESET> (+ 3 4))
+ (define <RED>some-text<RESET><GREEN>|a greeting|<RESET> "hello")
+ (let ((c (<RED>+ a b<RESET><GREEN>add1 first<RESET>)))
+ (format "one more than the total is %d" (<RED>add1<RESET><GREEN>+<RESET> c <GREEN>second<RESET>))))
diff --git a/t/t4034/scheme/post b/t/t4034/scheme/post
new file mode 100644
index 0000000..63b6ac4
--- /dev/null
+++ b/t/t4034/scheme/post
@@ -0,0 +1,6 @@
+(define (my-func first second)
+ ; This is a (moderately) cool function.
+ (that\place (+ 3 4))
+ (define |a greeting| "hello")
+ (let ((c (add1 first)))
+ (format "one more than the total is %d" (+ c second))))
diff --git a/t/t4034/scheme/pre b/t/t4034/scheme/pre
new file mode 100644
index 0000000..74b6605
--- /dev/null
+++ b/t/t4034/scheme/pre
@@ -0,0 +1,6 @@
+(define (myfunc a b)
+ ; This is a really cool function.
+ (this\place (+ 3 4))
+ (define some-text "hello")
+ (let ((c (+ a b)))
+ (format "one more than the total is %d" (add1 c))))
diff --git a/t/t4034/tex/expect b/t/t4034/tex/expect
new file mode 100644
index 0000000..604969b
--- /dev/null
+++ b/t/t4034/tex/expect
@@ -0,0 +1,9 @@
+<BOLD>diff --git a/pre b/post<RESET>
+<BOLD>index 2b2dfcb..65cab61 100644<RESET>
+<BOLD>--- a/pre<RESET>
+<BOLD>+++ b/post<RESET>
+<CYAN>@@ -1,4 +1,4 @@<RESET>
+\section{Something <GREEN>new<RESET>}
+<RED>\emph<RESET><GREEN>\textbf<RESET>{Macro style}
+{<RED>\em<RESET><GREEN>\bfseries<RESET> State toggle style}
+\\[<RED>1em<RESET><GREEN>1cm<RESET>]
diff --git a/t/t4034/tex/post b/t/t4034/tex/post
new file mode 100644
index 0000000..65cab61
--- /dev/null
+++ b/t/t4034/tex/post
@@ -0,0 +1,4 @@
+\section{Something new}
+\textbf{Macro style}
+{\bfseries State toggle style}
+\\[1cm]
diff --git a/t/t4034/tex/pre b/t/t4034/tex/pre
new file mode 100644
index 0000000..2b2dfcb
--- /dev/null
+++ b/t/t4034/tex/pre
@@ -0,0 +1,4 @@
+\section{Something}
+\emph{Macro style}
+{\em State toggle style}
+\\[1em]
diff --git a/t/t4035-diff-quiet.sh b/t/t4035-diff-quiet.sh
new file mode 100755
index 0000000..76f8034
--- /dev/null
+++ b/t/t4035-diff-quiet.sh
@@ -0,0 +1,165 @@
+#!/bin/sh
+
+test_description='Return value of diffs'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ echo 1 >a &&
+ git add . &&
+ git commit -m first &&
+ echo 2 >b &&
+ git add . &&
+ git commit -a -m second &&
+ mkdir -p test-outside/repo && (
+ cd test-outside/repo &&
+ git init &&
+ echo "1 1" >a &&
+ git add . &&
+ git commit -m 1
+ ) &&
+ mkdir -p test-outside/non/git && (
+ cd test-outside/non/git &&
+ echo "1 1" >a &&
+ echo "1 1" >matching-file &&
+ echo "1 1 " >trailing-space &&
+ echo "1 1" >extra-space &&
+ echo "2" >never-match
+ )
+'
+
+test_expect_success 'git diff-tree HEAD^ HEAD' '
+ test_expect_code 1 git diff-tree --quiet HEAD^ HEAD >cnt &&
+ test_line_count = 0 cnt
+'
+test_expect_success 'git diff-tree HEAD^ HEAD -- a' '
+ test_expect_code 0 git diff-tree --quiet HEAD^ HEAD -- a >cnt &&
+ test_line_count = 0 cnt
+'
+test_expect_success 'git diff-tree HEAD^ HEAD -- b' '
+ test_expect_code 1 git diff-tree --quiet HEAD^ HEAD -- b >cnt &&
+ test_line_count = 0 cnt
+'
+# this diff outputs one line: sha1 of the given head
+test_expect_success 'echo HEAD | git diff-tree --stdin' '
+ echo $(git rev-parse HEAD) |
+ test_expect_code 1 git diff-tree --quiet --stdin >cnt &&
+ test_line_count = 1 cnt
+'
+test_expect_success 'git diff-tree HEAD HEAD' '
+ test_expect_code 0 git diff-tree --quiet HEAD HEAD >cnt &&
+ test_line_count = 0 cnt
+'
+test_expect_success 'git diff-files' '
+ test_expect_code 0 git diff-files --quiet >cnt &&
+ test_line_count = 0 cnt
+'
+test_expect_success 'git diff-index --cached HEAD' '
+ test_expect_code 0 git diff-index --quiet --cached HEAD >cnt &&
+ test_line_count = 0 cnt
+'
+test_expect_success 'git diff-index --cached HEAD^' '
+ test_expect_code 1 git diff-index --quiet --cached HEAD^ >cnt &&
+ test_line_count = 0 cnt
+'
+test_expect_success 'git diff-index --cached HEAD^' '
+ echo text >>b &&
+ echo 3 >c &&
+ git add . &&
+ test_expect_code 1 git diff-index --quiet --cached HEAD^ >cnt &&
+ test_line_count = 0 cnt
+'
+test_expect_success 'git diff-tree -Stext HEAD^ HEAD -- b' '
+ git commit -m "text in b" &&
+ test_expect_code 1 git diff-tree --quiet -Stext HEAD^ HEAD -- b >cnt &&
+ test_line_count = 0 cnt
+'
+test_expect_success 'git diff-tree -Snot-found HEAD^ HEAD -- b' '
+ test_expect_code 0 git diff-tree --quiet -Snot-found HEAD^ HEAD -- b >cnt &&
+ test_line_count = 0 cnt
+'
+test_expect_success 'git diff-files' '
+ echo 3 >>c &&
+ test_expect_code 1 git diff-files --quiet >cnt &&
+ test_line_count = 0 cnt
+'
+
+test_expect_success 'git diff-index --cached HEAD' '
+ git update-index c &&
+ test_expect_code 1 git diff-index --quiet --cached HEAD >cnt &&
+ test_line_count = 0 cnt
+'
+
+test_expect_success 'git diff, one file outside repo' '
+ (
+ cd test-outside/repo &&
+ test_expect_code 0 git diff --quiet a ../non/git/matching-file &&
+ test_expect_code 1 git diff --quiet a ../non/git/extra-space
+ )
+'
+
+test_expect_success 'git diff, both files outside repo' '
+ (
+ GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY/test-outside" &&
+ export GIT_CEILING_DIRECTORIES &&
+ cd test-outside/non/git &&
+ test_expect_code 0 git diff --quiet a matching-file &&
+ test_expect_code 1 git diff --quiet a extra-space
+ )
+'
+
+test_expect_success 'git diff --ignore-space-at-eol, one file outside repo' '
+ (
+ cd test-outside/repo &&
+ test_expect_code 0 git diff --quiet --ignore-space-at-eol a ../non/git/trailing-space &&
+ test_expect_code 1 git diff --quiet --ignore-space-at-eol a ../non/git/extra-space
+ )
+'
+
+test_expect_success 'git diff --ignore-space-at-eol, both files outside repo' '
+ (
+ GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY/test-outside" &&
+ export GIT_CEILING_DIRECTORIES &&
+ cd test-outside/non/git &&
+ test_expect_code 0 git diff --quiet --ignore-space-at-eol a trailing-space &&
+ test_expect_code 1 git diff --quiet --ignore-space-at-eol a extra-space
+ )
+'
+
+test_expect_success 'git diff --ignore-all-space, one file outside repo' '
+ (
+ cd test-outside/repo &&
+ test_expect_code 0 git diff --quiet --ignore-all-space a ../non/git/trailing-space &&
+ test_expect_code 0 git diff --quiet --ignore-all-space a ../non/git/extra-space &&
+ test_expect_code 1 git diff --quiet --ignore-all-space a ../non/git/never-match
+ )
+'
+
+test_expect_success 'git diff --ignore-all-space, both files outside repo' '
+ (
+ GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY/test-outside" &&
+ export GIT_CEILING_DIRECTORIES &&
+ cd test-outside/non/git &&
+ test_expect_code 0 git diff --quiet --ignore-all-space a trailing-space &&
+ test_expect_code 0 git diff --quiet --ignore-all-space a extra-space &&
+ test_expect_code 1 git diff --quiet --ignore-all-space a never-match
+ )
+'
+
+test_expect_success 'git diff --quiet ignores stat-change only entries' '
+ test-tool chmtime +10 a &&
+ echo modified >>b &&
+ test_expect_code 1 git diff --quiet
+'
+
+test_expect_success 'git diff --quiet on a path that need conversion' '
+ echo "crlf.txt text=auto" >.gitattributes &&
+ printf "Hello\r\nWorld\r\n" >crlf.txt &&
+ git add .gitattributes crlf.txt &&
+
+ printf "Hello\r\nWorld\n" >crlf.txt &&
+ git diff --quiet crlf.txt
+'
+
+test_done
diff --git a/t/t4036-format-patch-signer-mime.sh b/t/t4036-format-patch-signer-mime.sh
new file mode 100755
index 0000000..48655bc
--- /dev/null
+++ b/t/t4036-format-patch-signer-mime.sh
@@ -0,0 +1,51 @@
+#!/bin/sh
+
+test_description='format-patch -s should force MIME encoding as needed'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+
+ >F &&
+ git add F &&
+ git commit -m initial &&
+ echo new line >F &&
+
+ test_tick &&
+ git commit -m "This adds some lines to F" F
+
+'
+
+test_expect_success 'format normally' '
+
+ git format-patch --stdout -1 >output &&
+ ! grep Content-Type output
+
+'
+
+test_expect_success 'format with signoff without funny signer name' '
+
+ git format-patch -s --stdout -1 >output &&
+ ! grep Content-Type output
+
+'
+
+test_expect_success 'format with non ASCII signer name' '
+
+ GIT_COMMITTER_NAME="ã¯ã¾ã® ãµã«ãŠã†" \
+ git format-patch -s --stdout -1 >output &&
+ grep Content-Type output
+
+'
+
+test_expect_success 'attach and signoff do not duplicate mime headers' '
+
+ GIT_COMMITTER_NAME="ã¯ã¾ã® ãµã«ãŠã†" \
+ git format-patch -s --stdout -1 --attach >output &&
+ test $(grep -ci ^MIME-Version: output) = 1
+
+'
+
+test_done
+
diff --git a/t/t4037-diff-r-t-dirs.sh b/t/t4037-diff-r-t-dirs.sh
new file mode 100755
index 0000000..b5f96fe
--- /dev/null
+++ b/t/t4037-diff-r-t-dirs.sh
@@ -0,0 +1,54 @@
+#!/bin/sh
+
+test_description='diff -r -t shows directory additions and deletions'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ mkdir dc dr dt &&
+ >dc/1 &&
+ >dr/2 &&
+ >dt/3 &&
+ >fc &&
+ >fr &&
+ >ft &&
+ git add . &&
+ test_tick &&
+ git commit -m initial &&
+
+ rm -fr dt dr ft fr &&
+ mkdir da ft &&
+ for p in dc/1 da/4 dt ft/5 fc
+ do
+ echo hello >$p || exit
+ done &&
+ git add -u &&
+ git add . &&
+ test_tick &&
+ git commit -m second
+'
+
+cat >expect <<\EOF
+A da
+A da/4
+M dc
+M dc/1
+D dr
+D dr/2
+A dt
+D dt
+D dt/3
+M fc
+D fr
+D ft
+A ft
+A ft/5
+EOF
+
+test_expect_success verify '
+ git diff-tree -r -t --name-status HEAD^ HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t4038-diff-combined.sh b/t/t4038-diff-combined.sh
new file mode 100755
index 0000000..2ce26e5
--- /dev/null
+++ b/t/t4038-diff-combined.sh
@@ -0,0 +1,546 @@
+#!/bin/sh
+
+test_description='combined diff'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-diff.sh
+
+setup_helper () {
+ one=$1 branch=$2 side=$3 &&
+
+ git branch $side $branch &&
+ for l in $one two three fyra
+ do
+ echo $l
+ done >file &&
+ git add file &&
+ test_tick &&
+ git commit -m $branch &&
+ git checkout $side &&
+ for l in $one two three quatro
+ do
+ echo $l
+ done >file &&
+ git add file &&
+ test_tick &&
+ git commit -m $side &&
+ test_must_fail git merge $branch &&
+ for l in $one three four
+ do
+ echo $l
+ done >file &&
+ git add file &&
+ test_tick &&
+ git commit -m "merge $branch into $side"
+}
+
+verify_helper () {
+ it=$1 &&
+
+ # Ignore lines that were removed only from the other parent
+ sed -e '
+ 1,/^@@@/d
+ /^ -/d
+ s/^\(.\)./\1/
+ ' "$it" >"$it.actual.1" &&
+ sed -e '
+ 1,/^@@@/d
+ /^- /d
+ s/^.\(.\)/\1/
+ ' "$it" >"$it.actual.2" &&
+
+ git diff "$it^" "$it" -- | sed -e '1,/^@@/d' >"$it.expect.1" &&
+ test_cmp "$it.expect.1" "$it.actual.1" &&
+
+ git diff "$it^2" "$it" -- | sed -e '1,/^@@/d' >"$it.expect.2" &&
+ test_cmp "$it.expect.2" "$it.actual.2"
+}
+
+test_expect_success setup '
+ >file &&
+ git add file &&
+ test_tick &&
+ git commit -m initial &&
+
+ git branch withone &&
+ git branch sansone &&
+
+ git checkout withone &&
+ setup_helper one withone sidewithone &&
+
+ git checkout sansone &&
+ setup_helper "" sansone sidesansone
+'
+
+test_expect_success 'check combined output (1)' '
+ git show sidewithone -- >sidewithone &&
+ verify_helper sidewithone
+'
+
+test_expect_success 'check combined output (1) with git diff <rev>^!' '
+ git diff sidewithone^! -- >sidewithone &&
+ verify_helper sidewithone
+'
+
+test_expect_success 'check combined output (2)' '
+ git show sidesansone -- >sidesansone &&
+ verify_helper sidesansone
+'
+
+test_expect_success 'check combined output (2) with git diff <rev>^!' '
+ git diff sidesansone^! -- >sidesansone &&
+ verify_helper sidesansone
+'
+
+test_expect_success 'diagnose truncated file' '
+ >file &&
+ git add file &&
+ git commit --amend -C HEAD &&
+ git show >out &&
+ grep "diff --cc file" out
+'
+
+test_expect_success 'setup for --cc --raw' '
+ blob=$(echo file | git hash-object --stdin -w) &&
+ base_tree=$(echo "100644 blob $blob file" | git mktree) &&
+ trees= &&
+ for i in $(test_seq 1 40)
+ do
+ blob=$(echo file$i | git hash-object --stdin -w) &&
+ trees="$trees$(echo "100644 blob $blob file" | git mktree)$LF" || return 1
+ done
+'
+
+test_expect_success 'check --cc --raw with four trees' '
+ four_trees=$(echo "$trees" | sed -e 4q) &&
+ git diff --cc --raw $four_trees $base_tree >out &&
+ # Check for four leading colons in the output:
+ grep "^::::[^:]" out
+'
+
+test_expect_success 'check --cc --raw with forty trees' '
+ git diff --cc --raw $trees $base_tree >out &&
+ # Check for forty leading colons in the output:
+ grep "^::::::::::::::::::::::::::::::::::::::::[^:]" out
+'
+
+test_expect_success 'setup combined ignore spaces' '
+ git checkout main &&
+ >test &&
+ git add test &&
+ git commit -m initial &&
+
+ tr -d Q <<-\EOF >test &&
+ always coalesce
+ eol space coalesce Q
+ space change coalesce
+ all spa ces coalesce
+ eol spaces Q
+ space change
+ all spa ces
+ EOF
+ git commit -m "test space change" -a &&
+
+ git checkout -b side HEAD^ &&
+ tr -d Q <<-\EOF >test &&
+ always coalesce
+ eol space coalesce
+ space change coalesce
+ all spaces coalesce
+ eol spaces
+ space change
+ all spaces
+ EOF
+ git commit -m "test other space changes" -a &&
+
+ test_must_fail git merge main &&
+ tr -d Q <<-\EOF >test &&
+ eol spaces Q
+ space change
+ all spa ces
+ EOF
+ git commit -m merged -a
+'
+
+test_expect_success 'check combined output (no ignore space)' '
+ git show >actual.tmp &&
+ sed -e "1,/^@@@/d" < actual.tmp >actual &&
+ tr -d Q <<-\EOF >expected &&
+ --always coalesce
+ - eol space coalesce
+ - space change coalesce
+ - all spaces coalesce
+ - eol spaces
+ - space change
+ - all spaces
+ -eol space coalesce Q
+ -space change coalesce
+ -all spa ces coalesce
+ + eol spaces Q
+ + space change
+ + all spa ces
+ EOF
+ compare_diff_patch expected actual
+'
+
+test_expect_success 'check combined output (ignore space at eol)' '
+ git show --ignore-space-at-eol >actual.tmp &&
+ sed -e "1,/^@@@/d" < actual.tmp >actual &&
+ tr -d Q <<-\EOF >expected &&
+ --always coalesce
+ --eol space coalesce
+ - space change coalesce
+ - all spaces coalesce
+ -space change coalesce
+ -all spa ces coalesce
+ eol spaces Q
+ - space change
+ - all spaces
+ + space change
+ + all spa ces
+ EOF
+ compare_diff_patch expected actual
+'
+
+test_expect_success 'check combined output (ignore space change)' '
+ git show -b >actual.tmp &&
+ sed -e "1,/^@@@/d" < actual.tmp >actual &&
+ tr -d Q <<-\EOF >expected &&
+ --always coalesce
+ --eol space coalesce
+ --space change coalesce
+ - all spaces coalesce
+ -all spa ces coalesce
+ eol spaces Q
+ space change
+ - all spaces
+ + all spa ces
+ EOF
+ compare_diff_patch expected actual
+'
+
+test_expect_success 'check combined output (ignore all spaces)' '
+ git show -w >actual.tmp &&
+ sed -e "1,/^@@@/d" < actual.tmp >actual &&
+ tr -d Q <<-\EOF >expected &&
+ --always coalesce
+ --eol space coalesce
+ --space change coalesce
+ --all spaces coalesce
+ eol spaces Q
+ space change
+ all spa ces
+ EOF
+ compare_diff_patch expected actual
+'
+
+test_expect_success 'combine diff coalesce simple' '
+ >test &&
+ git add test &&
+ git commit -m initial &&
+ test_seq 4 >test &&
+ git commit -a -m empty1 &&
+ git branch side1 &&
+ git checkout HEAD^ &&
+ test_seq 5 >test &&
+ git commit -a -m empty2 &&
+ test_must_fail git merge side1 &&
+ >test &&
+ git commit -a -m merge &&
+ git show >actual.tmp &&
+ sed -e "1,/^@@@/d" < actual.tmp >actual &&
+ tr -d Q <<-\EOF >expected &&
+ --1
+ --2
+ --3
+ --4
+ - 5
+ EOF
+ compare_diff_patch expected actual
+'
+
+test_expect_success 'combine diff coalesce tricky' '
+ >test &&
+ git add test &&
+ git commit -m initial --allow-empty &&
+ cat <<-\EOF >test &&
+ 3
+ 1
+ 2
+ 3
+ 4
+ EOF
+ git commit -a -m empty1 &&
+ git branch -f side1 &&
+ git checkout HEAD^ &&
+ cat <<-\EOF >test &&
+ 1
+ 3
+ 5
+ 4
+ EOF
+ git commit -a -m empty2 &&
+ git branch -f side2 &&
+ test_must_fail git merge side1 &&
+ >test &&
+ git commit -a -m merge &&
+ git show >actual.tmp &&
+ sed -e "1,/^@@@/d" < actual.tmp >actual &&
+ tr -d Q <<-\EOF >expected &&
+ -3
+ --1
+ -2
+ --3
+ - 5
+ --4
+ EOF
+ compare_diff_patch expected actual &&
+ git checkout -f side1 &&
+ test_must_fail git merge side2 &&
+ >test &&
+ git commit -a -m merge &&
+ git show >actual.tmp &&
+ sed -e "1,/^@@@/d" < actual.tmp >actual &&
+ tr -d Q <<-\EOF >expected &&
+ - 3
+ --1
+ - 2
+ --3
+ -5
+ --4
+ EOF
+ compare_diff_patch expected actual
+'
+
+test_expect_failure 'combine diff coalesce three parents' '
+ >test &&
+ git add test &&
+ git commit -m initial --allow-empty &&
+ cat <<-\EOF >test &&
+ 3
+ 1
+ 2
+ 3
+ 4
+ EOF
+ git commit -a -m empty1 &&
+ git checkout -B side1 &&
+ git checkout HEAD^ &&
+ cat <<-\EOF >test &&
+ 1
+ 3
+ 7
+ 5
+ 4
+ EOF
+ git commit -a -m empty2 &&
+ git branch -f side2 &&
+ git checkout HEAD^ &&
+ cat <<-\EOF >test &&
+ 3
+ 1
+ 6
+ 5
+ 4
+ EOF
+ git commit -a -m empty3 &&
+ >test &&
+ git add test &&
+ TREE=$(git write-tree) &&
+ COMMIT=$(git commit-tree -p HEAD -p side1 -p side2 -m merge $TREE) &&
+ git show $COMMIT >actual.tmp &&
+ sed -e "1,/^@@@/d" < actual.tmp >actual &&
+ tr -d Q <<-\EOF >expected &&
+ -- 3
+ ---1
+ - 6
+ - 2
+ --3
+ -7
+ - -5
+ ---4
+ EOF
+ compare_diff_patch expected actual
+'
+
+# Test for a bug reported at
+# https://lore.kernel.org/git/20130515143508.GO25742@login.drsnuggles.stderr.nl/
+# where a delete lines were missing from combined diff output when they
+# occurred exactly before the context lines of a later change.
+test_expect_success 'combine diff missing delete bug' '
+ git commit -m initial --allow-empty &&
+ cat <<-\EOF >test &&
+ 1
+ 2
+ 3
+ 4
+ EOF
+ git add test &&
+ git commit -a -m side1 &&
+ git checkout -B side1 &&
+ git checkout HEAD^ &&
+ cat <<-\EOF >test &&
+ 0
+ 1
+ 2
+ 3
+ 4modified
+ EOF
+ git add test &&
+ git commit -m side2 &&
+ git branch -f side2 &&
+ test_must_fail git merge --no-commit side1 &&
+ cat <<-\EOF >test &&
+ 1
+ 2
+ 3
+ 4modified
+ EOF
+ git add test &&
+ git commit -a -m merge &&
+ git diff-tree -c -p HEAD >actual.tmp &&
+ sed -e "1,/^@@@/d" < actual.tmp >actual &&
+ tr -d Q <<-\EOF >expected &&
+ - 0
+ 1
+ 2
+ 3
+ -4
+ +4modified
+ EOF
+ compare_diff_patch expected actual
+'
+
+test_expect_success 'combine diff gets tree sorting right' '
+ # create a directory and a file that sort differently in trees
+ # versus byte-wise (implied "/" sorts after ".")
+ git checkout -f main &&
+ mkdir foo &&
+ echo base >foo/one &&
+ echo base >foo/two &&
+ echo base >foo.ext &&
+ git add foo foo.ext &&
+ git commit -m base &&
+
+ # one side modifies a file in the directory, along with the root
+ # file...
+ echo main >foo/one &&
+ echo main >foo.ext &&
+ git commit -a -m main &&
+
+ # the other side modifies the other file in the directory
+ git checkout -b other HEAD^ &&
+ echo other >foo/two &&
+ git commit -a -m other &&
+
+ # And now we merge. The files in the subdirectory will resolve cleanly,
+ # meaning that a combined diff will not find them interesting. But it
+ # will find the tree itself interesting, because it had to be merged.
+ git checkout main &&
+ git merge other &&
+
+ printf "MM\tfoo\n" >expect &&
+ git diff-tree -c --name-status -t HEAD >actual.tmp &&
+ sed 1d <actual.tmp >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'setup for --combined-all-paths' '
+ git branch side1c &&
+ git branch side2c &&
+ git checkout side1c &&
+ test_seq 1 10 >filename-side1c &&
+ side1cf=$(git hash-object filename-side1c) &&
+ git add filename-side1c &&
+ git commit -m with &&
+ git checkout side2c &&
+ test_seq 1 9 >filename-side2c &&
+ echo ten >>filename-side2c &&
+ side2cf=$(git hash-object filename-side2c) &&
+ git add filename-side2c &&
+ git commit -m iam &&
+ git checkout -b mergery side1c &&
+ git merge --no-commit side2c &&
+ git rm filename-side1c &&
+ echo eleven >>filename-side2c &&
+ git mv filename-side2c filename-merged &&
+ mergedf=$(git hash-object filename-merged) &&
+ git add filename-merged &&
+ git commit
+'
+
+test_expect_success '--combined-all-paths and --raw' '
+ cat <<-EOF >expect &&
+ ::100644 100644 100644 $side1cf $side2cf $mergedf RR filename-side1c filename-side2c filename-merged
+ EOF
+ git diff-tree -c -M --raw --combined-all-paths HEAD >actual.tmp &&
+ sed 1d <actual.tmp >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--combined-all-paths and --cc' '
+ cat <<-\EOF >expect &&
+ --- a/filename-side1c
+ --- a/filename-side2c
+ +++ b/filename-merged
+ EOF
+ git diff-tree --cc -M --combined-all-paths HEAD >actual.tmp &&
+ grep ^[-+][-+][-+] <actual.tmp >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success FUNNYNAMES 'setup for --combined-all-paths with funny names' '
+ git branch side1d &&
+ git branch side2d &&
+ git checkout side1d &&
+ test_seq 1 10 >"$(printf "file\twith\ttabs")" &&
+ git add file* &&
+ side1df=$(git hash-object *tabs) &&
+ git commit -m with &&
+ git checkout side2d &&
+ test_seq 1 9 >"$(printf "i\tam\ttabbed")" &&
+ echo ten >>"$(printf "i\tam\ttabbed")" &&
+ git add *tabbed &&
+ side2df=$(git hash-object *tabbed) &&
+ git commit -m iam &&
+ git checkout -b funny-names-mergery side1d &&
+ git merge --no-commit side2d &&
+ git rm *tabs &&
+ echo eleven >>"$(printf "i\tam\ttabbed")" &&
+ git mv "$(printf "i\tam\ttabbed")" "$(printf "fickle\tnaming")" &&
+ git add fickle* &&
+ headf=$(git hash-object fickle*) &&
+ git commit &&
+ head=$(git rev-parse HEAD)
+'
+
+test_expect_success FUNNYNAMES '--combined-all-paths and --raw and funny names' '
+ cat <<-EOF >expect &&
+ ::100644 100644 100644 $side1df $side2df $headf RR "file\twith\ttabs" "i\tam\ttabbed" "fickle\tnaming"
+ EOF
+ git diff-tree -c -M --raw --combined-all-paths HEAD >actual.tmp &&
+ sed 1d <actual.tmp >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success FUNNYNAMES '--combined-all-paths and --raw -and -z and funny names' '
+ printf "$head\0::100644 100644 100644 $side1df $side2df $headf RR\0file\twith\ttabs\0i\tam\ttabbed\0fickle\tnaming\0" >expect &&
+ git diff-tree -c -M --raw --combined-all-paths -z HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success FUNNYNAMES '--combined-all-paths and --cc and funny names' '
+ cat <<-\EOF >expect &&
+ --- "a/file\twith\ttabs"
+ --- "a/i\tam\ttabbed"
+ +++ "b/fickle\tnaming"
+ EOF
+ git diff-tree --cc -M --combined-all-paths HEAD >actual.tmp &&
+ grep ^[-+][-+][-+] <actual.tmp >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t4039-diff-assume-unchanged.sh b/t/t4039-diff-assume-unchanged.sh
new file mode 100755
index 0000000..78090e6
--- /dev/null
+++ b/t/t4039-diff-assume-unchanged.sh
@@ -0,0 +1,43 @@
+#!/bin/sh
+
+test_description='diff with assume-unchanged entries'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# external diff has been tested in t4020-diff-external.sh
+
+test_expect_success 'setup' '
+ echo zero > zero &&
+ git add zero &&
+ git commit -m zero &&
+ echo one > one &&
+ echo two > two &&
+ blob=$(git hash-object one) &&
+ git add one two &&
+ git commit -m onetwo &&
+ git update-index --assume-unchanged one &&
+ echo borked >> one &&
+ test "$(git ls-files -v one)" = "h one"
+'
+
+test_expect_success 'diff-index does not examine assume-unchanged entries' '
+ git diff-index HEAD^ -- one | grep -q $blob
+'
+
+test_expect_success 'diff-files does not examine assume-unchanged entries' '
+ rm one &&
+ test -z "$(git diff-files -- one)"
+'
+
+test_expect_success POSIXPERM 'find-copies-harder is not confused by mode bits' '
+ echo content >exec &&
+ chmod +x exec &&
+ git add exec &&
+ git commit -m exec &&
+ git update-index --assume-unchanged exec &&
+ git diff-files --find-copies-harder -- exec >actual &&
+ test_must_be_empty actual
+'
+
+test_done
diff --git a/t/t4040-whitespace-status.sh b/t/t4040-whitespace-status.sh
new file mode 100755
index 0000000..eec3d73
--- /dev/null
+++ b/t/t4040-whitespace-status.sh
@@ -0,0 +1,76 @@
+#!/bin/sh
+
+test_description='diff --exit-code with whitespace'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ mkdir a b &&
+ echo >c &&
+ echo >a/d &&
+ echo >b/e &&
+ git add . &&
+ test_tick &&
+ git commit -m initial &&
+ echo " " >a/d &&
+ test_tick &&
+ git commit -a -m second &&
+ echo " " >a/d &&
+ echo " " >b/e &&
+ git add a/d
+'
+
+test_expect_success 'diff-tree --exit-code' '
+ test_must_fail git diff --exit-code HEAD^ HEAD &&
+ test_must_fail git diff-tree --exit-code HEAD^ HEAD
+'
+
+test_expect_success 'diff-tree -b --exit-code' '
+ git diff -b --exit-code HEAD^ HEAD &&
+ git diff-tree -b -p --exit-code HEAD^ HEAD
+'
+
+test_expect_success 'diff-index --cached --exit-code' '
+ test_must_fail git diff --cached --exit-code HEAD &&
+ test_must_fail git diff-index --cached --exit-code HEAD
+'
+
+test_expect_success 'diff-index -b -p --cached --exit-code' '
+ git diff -b --cached --exit-code HEAD &&
+ git diff-index -b -p --cached --exit-code HEAD
+'
+
+test_expect_success 'diff-index --exit-code' '
+ test_must_fail git diff --exit-code HEAD &&
+ test_must_fail git diff-index --exit-code HEAD
+'
+
+test_expect_success 'diff-index -b -p --exit-code' '
+ git diff -b --exit-code HEAD &&
+ git diff-index -b -p --exit-code HEAD
+'
+
+test_expect_success 'diff-files --exit-code' '
+ test_must_fail git diff --exit-code &&
+ test_must_fail git diff-files --exit-code
+'
+
+test_expect_success 'diff-files -b -p --exit-code' '
+ git diff -b --exit-code &&
+ git diff-files -b -p --exit-code
+'
+
+test_expect_success 'diff-files --diff-filter --quiet' '
+ git reset --hard &&
+ rm a/d &&
+ echo x >>b/e &&
+ test_must_fail git diff-files --diff-filter=M --quiet
+'
+
+test_expect_success 'diff-tree --diff-filter --quiet' '
+ git commit -a -m "worktree state" &&
+ test_must_fail git diff-tree --diff-filter=M --quiet HEAD^ HEAD
+'
+
+test_done
diff --git a/t/t4041-diff-submodule-option.sh b/t/t4041-diff-submodule-option.sh
new file mode 100755
index 0000000..0c1502d
--- /dev/null
+++ b/t/t4041-diff-submodule-option.sh
@@ -0,0 +1,551 @@
+#!/bin/sh
+#
+# Copyright (c) 2009 Jens Lehmann, based on t7401 by Ping Yin
+# Copyright (c) 2011 Alexey Shumkin (+ non-UTF-8 commit encoding tests)
+#
+
+test_description='Support for verbose submodule differences in git diff
+
+This test tries to verify the sanity of the --submodule option of git diff.
+'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+# Tested non-UTF-8 encoding
+test_encoding="ISO8859-1"
+
+# String "added" in German (translated with Google Translate), encoded in UTF-8,
+# used in sample commit log messages in add_file() function below.
+added=$(printf "hinzugef\303\274gt")
+add_file () {
+ (
+ cd "$1" &&
+ shift &&
+ for name
+ do
+ echo "$name" >"$name" &&
+ git add "$name" &&
+ test_tick &&
+ # "git commit -m" would break MinGW, as Windows refuse to pass
+ # $test_encoding encoded parameter to git.
+ echo "Add $name ($added $name)" | iconv -f utf-8 -t $test_encoding |
+ git -c "i18n.commitEncoding=$test_encoding" commit -F -
+ done >/dev/null &&
+ git rev-parse --short --verify HEAD
+ )
+}
+commit_file () {
+ test_tick &&
+ git commit "$@" -m "Commit $*" >/dev/null
+}
+
+test_create_repo sm1 &&
+add_file . foo >/dev/null
+
+head1=$(add_file sm1 foo1 foo2)
+fullhead1=$(cd sm1; git rev-parse --verify HEAD)
+
+test_expect_success 'added submodule' '
+ git add sm1 &&
+ git diff-index -p --submodule=log HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 0000000...$head1 (new submodule)
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'added submodule, set diff.submodule' '
+ git config diff.submodule log &&
+ git add sm1 &&
+ git diff --cached >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 0000000...$head1 (new submodule)
+ EOF
+ git config --unset diff.submodule &&
+ test_cmp expected actual
+'
+
+test_expect_success '--submodule=short overrides diff.submodule' '
+ test_config diff.submodule log &&
+ git add sm1 &&
+ git diff --submodule=short --cached >actual &&
+ cat >expected <<-EOF &&
+ diff --git a/sm1 b/sm1
+ new file mode 160000
+ index 0000000..$head1
+ --- /dev/null
+ +++ b/sm1
+ @@ -0,0 +1 @@
+ +Subproject commit $fullhead1
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'diff.submodule does not affect plumbing' '
+ test_config diff.submodule log &&
+ git diff-index -p HEAD >actual &&
+ cat >expected <<-EOF &&
+ diff --git a/sm1 b/sm1
+ new file mode 160000
+ index 0000000..$head1
+ --- /dev/null
+ +++ b/sm1
+ @@ -0,0 +1 @@
+ +Subproject commit $fullhead1
+ EOF
+ test_cmp expected actual
+'
+
+commit_file sm1 &&
+head2=$(add_file sm1 foo3)
+
+test_expect_success 'modified submodule(forward)' '
+ git diff-index -p --submodule=log HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 $head1..$head2:
+ > Add foo3 ($added foo3)
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'modified submodule(forward)' '
+ git diff --submodule=log >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 $head1..$head2:
+ > Add foo3 ($added foo3)
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'modified submodule(forward) --submodule' '
+ git diff --submodule >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 $head1..$head2:
+ > Add foo3 ($added foo3)
+ EOF
+ test_cmp expected actual
+'
+
+fullhead2=$(cd sm1; git rev-parse --verify HEAD)
+test_expect_success 'modified submodule(forward) --submodule=short' '
+ git diff --submodule=short >actual &&
+ cat >expected <<-EOF &&
+ diff --git a/sm1 b/sm1
+ index $head1..$head2 160000
+ --- a/sm1
+ +++ b/sm1
+ @@ -1 +1 @@
+ -Subproject commit $fullhead1
+ +Subproject commit $fullhead2
+ EOF
+ test_cmp expected actual
+'
+
+commit_file sm1 &&
+head3=$(
+ cd sm1 &&
+ git reset --hard HEAD~2 >/dev/null &&
+ git rev-parse --short --verify HEAD
+)
+
+test_expect_success 'modified submodule(backward)' '
+ git diff-index -p --submodule=log HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 $head2..$head3 (rewind):
+ < Add foo3 ($added foo3)
+ < Add foo2 ($added foo2)
+ EOF
+ test_cmp expected actual
+'
+
+head4=$(add_file sm1 foo4 foo5)
+test_expect_success 'modified submodule(backward and forward)' '
+ git diff-index -p --submodule=log HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 $head2...$head4:
+ > Add foo5 ($added foo5)
+ > Add foo4 ($added foo4)
+ < Add foo3 ($added foo3)
+ < Add foo2 ($added foo2)
+ EOF
+ test_cmp expected actual
+'
+
+commit_file sm1 &&
+mv sm1 sm1-bak &&
+echo sm1 >sm1 &&
+head5=$(git hash-object sm1 | cut -c1-7) &&
+git add sm1 &&
+rm -f sm1 &&
+mv sm1-bak sm1
+
+test_expect_success 'typechanged submodule(submodule->blob), --cached' '
+ git diff --submodule=log --cached >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 $head4...0000000 (submodule deleted)
+ diff --git a/sm1 b/sm1
+ new file mode 100644
+ index 0000000..$head5
+ --- /dev/null
+ +++ b/sm1
+ @@ -0,0 +1 @@
+ +sm1
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'typechanged submodule(submodule->blob)' '
+ git diff --submodule=log >actual &&
+ cat >expected <<-EOF &&
+ diff --git a/sm1 b/sm1
+ deleted file mode 100644
+ index $head5..0000000
+ --- a/sm1
+ +++ /dev/null
+ @@ -1 +0,0 @@
+ -sm1
+ Submodule sm1 0000000...$head4 (new submodule)
+ EOF
+ test_cmp expected actual
+'
+
+rm -rf sm1 &&
+git checkout-index sm1
+test_expect_success 'typechanged submodule(submodule->blob)' '
+ git diff-index -p --submodule=log HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 $head4...0000000 (submodule deleted)
+ diff --git a/sm1 b/sm1
+ new file mode 100644
+ index 0000000..$head5
+ --- /dev/null
+ +++ b/sm1
+ @@ -0,0 +1 @@
+ +sm1
+ EOF
+ test_cmp expected actual
+'
+
+rm -f sm1 &&
+test_create_repo sm1 &&
+head6=$(add_file sm1 foo6 foo7)
+fullhead6=$(cd sm1; git rev-parse --verify HEAD)
+test_expect_success 'nonexistent commit' '
+ git diff-index -p --submodule=log HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 $head4...$head6 (commits not present)
+ EOF
+ test_cmp expected actual
+'
+
+commit_file
+test_expect_success 'typechanged submodule(blob->submodule)' '
+ git diff-index -p --submodule=log HEAD >actual &&
+ cat >expected <<-EOF &&
+ diff --git a/sm1 b/sm1
+ deleted file mode 100644
+ index $head5..0000000
+ --- a/sm1
+ +++ /dev/null
+ @@ -1 +0,0 @@
+ -sm1
+ Submodule sm1 0000000...$head6 (new submodule)
+ EOF
+ test_cmp expected actual
+'
+
+commit_file sm1 &&
+test_expect_success 'submodule is up to date' '
+ git diff-index -p --submodule=log HEAD >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'submodule contains untracked content' '
+ echo new > sm1/new-file &&
+ git diff-index -p --ignore-submodules=none --submodule=log HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 contains untracked content
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'submodule contains untracked content (untracked ignored)' '
+ git diff-index -p --submodule=log HEAD >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'submodule contains untracked content (dirty ignored)' '
+ git diff-index -p --ignore-submodules=dirty --submodule=log HEAD >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'submodule contains untracked content (all ignored)' '
+ git diff-index -p --ignore-submodules=all --submodule=log HEAD >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'submodule contains untracked and modified content' '
+ echo new > sm1/foo6 &&
+ git diff-index -p --ignore-submodules=none --submodule=log HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 contains untracked content
+ Submodule sm1 contains modified content
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'submodule contains untracked and modified content (untracked ignored)' '
+ echo new > sm1/foo6 &&
+ git diff-index -p --submodule=log HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 contains modified content
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'submodule contains untracked and modified content (dirty ignored)' '
+ echo new > sm1/foo6 &&
+ git diff-index -p --ignore-submodules=dirty --submodule=log HEAD >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'submodule contains untracked and modified content (all ignored)' '
+ echo new > sm1/foo6 &&
+ git diff-index -p --ignore-submodules --submodule=log HEAD >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'submodule contains modified content' '
+ rm -f sm1/new-file &&
+ git diff-index -p --submodule=log HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 contains modified content
+ EOF
+ test_cmp expected actual
+'
+
+(cd sm1; git commit -mchange foo6 >/dev/null) &&
+head8=$(cd sm1; git rev-parse --short --verify HEAD) &&
+test_expect_success 'submodule is modified' '
+ git diff-index -p --submodule=log HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 $head6..$head8:
+ > change
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'modified submodule contains untracked content' '
+ echo new > sm1/new-file &&
+ git diff-index -p --ignore-submodules=none --submodule=log HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 contains untracked content
+ Submodule sm1 $head6..$head8:
+ > change
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'modified submodule contains untracked content (untracked ignored)' '
+ git diff-index -p --submodule=log HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 $head6..$head8:
+ > change
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'modified submodule contains untracked content (dirty ignored)' '
+ git diff-index -p --ignore-submodules=dirty --submodule=log HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 $head6..$head8:
+ > change
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'modified submodule contains untracked content (all ignored)' '
+ git diff-index -p --ignore-submodules=all --submodule=log HEAD >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'modified submodule contains untracked and modified content' '
+ echo modification >> sm1/foo6 &&
+ git diff-index -p --ignore-submodules=none --submodule=log HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 contains untracked content
+ Submodule sm1 contains modified content
+ Submodule sm1 $head6..$head8:
+ > change
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'modified submodule contains untracked and modified content (untracked ignored)' '
+ echo modification >> sm1/foo6 &&
+ git diff-index -p --submodule=log HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 contains modified content
+ Submodule sm1 $head6..$head8:
+ > change
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'modified submodule contains untracked and modified content (dirty ignored)' '
+ echo modification >> sm1/foo6 &&
+ git diff-index -p --ignore-submodules=dirty --submodule=log HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 $head6..$head8:
+ > change
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'modified submodule contains untracked and modified content (all ignored)' '
+ echo modification >> sm1/foo6 &&
+ git diff-index -p --ignore-submodules --submodule=log HEAD >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'modified submodule contains modified content' '
+ rm -f sm1/new-file &&
+ git diff-index -p --submodule=log HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 contains modified content
+ Submodule sm1 $head6..$head8:
+ > change
+ EOF
+ test_cmp expected actual
+'
+
+rm -rf sm1
+test_expect_success 'deleted submodule' '
+ git diff-index -p --submodule=log HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 $head6...0000000 (submodule deleted)
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'create second submodule' '
+ test_create_repo sm2 &&
+ head7=$(add_file sm2 foo8 foo9) &&
+ git add sm2
+'
+
+test_expect_success 'multiple submodules' '
+ git diff-index -p --submodule=log HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 $head6...0000000 (submodule deleted)
+ Submodule sm2 0000000...$head7 (new submodule)
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'path filter' '
+ git diff-index -p --submodule=log HEAD sm2 >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm2 0000000...$head7 (new submodule)
+ EOF
+ test_cmp expected actual
+'
+
+commit_file sm2
+test_expect_success 'given commit' '
+ git diff-index -p --submodule=log HEAD^ >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 $head6...0000000 (submodule deleted)
+ Submodule sm2 0000000...$head7 (new submodule)
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'given commit --submodule' '
+ git diff-index -p --submodule HEAD^ >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 $head6...0000000 (submodule deleted)
+ Submodule sm2 0000000...$head7 (new submodule)
+ EOF
+ test_cmp expected actual
+'
+
+fullhead7=$(cd sm2; git rev-parse --verify HEAD)
+
+test_expect_success 'given commit --submodule=short' '
+ git diff-index -p --submodule=short HEAD^ >actual &&
+ cat >expected <<-EOF &&
+ diff --git a/sm1 b/sm1
+ deleted file mode 160000
+ index $head6..0000000
+ --- a/sm1
+ +++ /dev/null
+ @@ -1 +0,0 @@
+ -Subproject commit $fullhead6
+ diff --git a/sm2 b/sm2
+ new file mode 160000
+ index 0000000..$head7
+ --- /dev/null
+ +++ b/sm2
+ @@ -0,0 +1 @@
+ +Subproject commit $fullhead7
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'setup .git file for sm2' '
+ (cd sm2 &&
+ REAL="$(pwd)/../.real" &&
+ mv .git "$REAL" &&
+ echo "gitdir: $REAL" >.git)
+'
+
+test_expect_success 'diff --submodule with .git file' '
+ git diff --submodule HEAD^ >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 $head6...0000000 (submodule deleted)
+ Submodule sm2 0000000...$head7 (new submodule)
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'diff --submodule with objects referenced by alternates' '
+ mkdir sub_alt &&
+ (cd sub_alt &&
+ git init &&
+ echo a >a &&
+ git add a &&
+ git commit -m a
+ ) &&
+ mkdir super &&
+ (cd super &&
+ git clone -s ../sub_alt sub &&
+ git init &&
+ git add sub &&
+ git commit -m "sub a"
+ ) &&
+ (cd sub_alt &&
+ sha1_before=$(git rev-parse --short HEAD) &&
+ echo b >b &&
+ git add b &&
+ git commit -m b &&
+ sha1_after=$(git rev-parse --short HEAD) &&
+ {
+ echo "Submodule sub $sha1_before..$sha1_after:" &&
+ echo " > b"
+ } >../expected
+ ) &&
+ (cd super &&
+ (cd sub &&
+ git fetch &&
+ git checkout origin/main
+ ) &&
+ git diff --submodule > ../actual
+ ) &&
+ test_cmp expected actual
+'
+
+test_done
diff --git a/t/t4042-diff-textconv-caching.sh b/t/t4042-diff-textconv-caching.sh
new file mode 100755
index 0000000..bf33aed
--- /dev/null
+++ b/t/t4042-diff-textconv-caching.sh
@@ -0,0 +1,121 @@
+#!/bin/sh
+
+test_description='test textconv caching'
+. ./test-lib.sh
+
+cat >helper <<'EOF'
+#!/bin/sh
+sed 's/^/converted: /' "$@" >helper.out
+cat helper.out
+EOF
+chmod +x helper
+
+test_expect_success 'setup' '
+ echo foo content 1 >foo.bin &&
+ echo bar content 1 >bar.bin &&
+ git add . &&
+ git commit -m one &&
+ foo1=$(git rev-parse --short HEAD:foo.bin) &&
+ bar1=$(git rev-parse --short HEAD:bar.bin) &&
+ echo foo content 2 >foo.bin &&
+ echo bar content 2 >bar.bin &&
+ git commit -a -m two &&
+ foo2=$(git rev-parse --short HEAD:foo.bin) &&
+ bar2=$(git rev-parse --short HEAD:bar.bin) &&
+ echo "*.bin diff=magic" >.gitattributes &&
+ git config diff.magic.textconv ./helper &&
+ git config diff.magic.cachetextconv true
+'
+
+cat >expect <<EOF
+diff --git a/bar.bin b/bar.bin
+index $bar1..$bar2 100644
+--- a/bar.bin
++++ b/bar.bin
+@@ -1 +1 @@
+-converted: bar content 1
++converted: bar content 2
+diff --git a/foo.bin b/foo.bin
+index $foo1..$foo2 100644
+--- a/foo.bin
++++ b/foo.bin
+@@ -1 +1 @@
+-converted: foo content 1
++converted: foo content 2
+EOF
+
+test_expect_success 'first textconv works' '
+ git diff HEAD^ HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'cached textconv produces same output' '
+ git diff HEAD^ HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'cached textconv does not run helper' '
+ rm -f helper.out &&
+ git diff HEAD^ HEAD >actual &&
+ test_cmp expect actual &&
+ ! test -r helper.out
+'
+
+cat >expect <<EOF
+diff --git a/bar.bin b/bar.bin
+index $bar1..$bar2 100644
+--- a/bar.bin
++++ b/bar.bin
+@@ -1,2 +1,2 @@
+ converted: other
+-converted: bar content 1
++converted: bar content 2
+diff --git a/foo.bin b/foo.bin
+index $foo1..$foo2 100644
+--- a/foo.bin
++++ b/foo.bin
+@@ -1,2 +1,2 @@
+ converted: other
+-converted: foo content 1
++converted: foo content 2
+EOF
+test_expect_success 'changing textconv invalidates cache' '
+ echo other >other &&
+ git config diff.magic.textconv "./helper other" &&
+ git diff HEAD^ HEAD >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<EOF
+diff --git a/bar.bin b/bar.bin
+index $bar1..$bar2 100644
+--- a/bar.bin
++++ b/bar.bin
+@@ -1,2 +1,2 @@
+ converted: other
+-converted: bar content 1
++converted: bar content 2
+diff --git a/foo.bin b/foo.bin
+index $foo1..$foo2 100644
+--- a/foo.bin
++++ b/foo.bin
+@@ -1 +1 @@
+-converted: foo content 1
++converted: foo content 2
+EOF
+test_expect_success 'switching diff driver produces correct results' '
+ git config diff.moremagic.textconv ./helper &&
+ echo foo.bin diff=moremagic >>.gitattributes &&
+ git diff HEAD^ HEAD >actual &&
+ test_cmp expect actual
+'
+
+# The point here is to test that we can log the notes cache and still use it to
+# produce a diff later (older versions of git would segfault on this). It's
+# much more likely to come up in the real world with "log --all -p", but using
+# --no-walk lets us reliably reproduce the order of traversal.
+test_expect_success 'log notes cache and still use cache for -p' '
+ git log --no-walk -p refs/notes/textconv/magic HEAD
+'
+
+test_done
diff --git a/t/t4043-diff-rename-binary.sh b/t/t4043-diff-rename-binary.sh
new file mode 100755
index 0000000..2a2cf91
--- /dev/null
+++ b/t/t4043-diff-rename-binary.sh
@@ -0,0 +1,45 @@
+#!/bin/sh
+#
+# Copyright (c) 2010 Jakub Narebski, Christian Couder
+#
+
+test_description='Move a binary file'
+
+. ./test-lib.sh
+
+
+test_expect_success 'prepare repository' '
+ git init &&
+ echo foo > foo &&
+ echo "barQ" | q_to_nul > bar &&
+ git add . &&
+ git commit -m "Initial commit"
+'
+
+test_expect_success 'move the files into a "sub" directory' '
+ mkdir sub &&
+ git mv bar foo sub/ &&
+ git commit -m "Moved to sub/"
+'
+
+cat > expected <<\EOF
+- - bar => sub/bar
+0 0 foo => sub/foo
+
+diff --git a/bar b/sub/bar
+similarity index 100%
+rename from bar
+rename to sub/bar
+diff --git a/foo b/sub/foo
+similarity index 100%
+rename from foo
+rename to sub/foo
+EOF
+
+test_expect_success 'git show -C -C report renames' '
+ git show -C -C --raw --binary --numstat >patch-with-stat &&
+ tail -n 11 patch-with-stat >current &&
+ test_cmp expected current
+'
+
+test_done
diff --git a/t/t4044-diff-index-unique-abbrev.sh b/t/t4044-diff-index-unique-abbrev.sh
new file mode 100755
index 0000000..9f6043d
--- /dev/null
+++ b/t/t4044-diff-index-unique-abbrev.sh
@@ -0,0 +1,57 @@
+#!/bin/sh
+
+test_description='test unique sha1 abbreviation on "index from..to" line'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ test_oid_cache <<-EOF &&
+ val1 sha1:4827
+ val1 sha256:5664
+
+ val2 sha1:11742
+ val2 sha256:10625
+
+ hash1 sha1:51d2738463ea4ca66f8691c91e33ce64b7d41bb1
+ hash1 sha256:ae31dfff0af93b2c62b0098a039b38569c43b0a7e97b873000ca42d128f27350
+
+ hasht1 sha1:51d27384
+ hasht1 sha256:ae31dfff
+
+ hash2 sha1:51d2738efb4ad8a1e40bed839ab8e116f0a15e47
+ hash2 sha256:ae31dffada88a46fd5f53c7ed5aa25a7a8951f1d5e88456c317c8d5484d263e5
+
+ hasht2 sha1:51d2738e
+ hasht2 sha256:ae31dffa
+ EOF
+
+ cat >expect_initial <<-EOF &&
+ 100644 blob $(test_oid hash1) foo
+ EOF
+
+ cat >expect_update <<-EOF &&
+ 100644 blob $(test_oid hash2) foo
+ EOF
+
+ test_oid val1 > foo &&
+ git add foo &&
+ git commit -m "initial" &&
+ git cat-file -p HEAD: > actual &&
+ test_cmp expect_initial actual &&
+ test_oid val2 > foo &&
+ git commit -a -m "update" &&
+ git cat-file -p HEAD: > actual &&
+ test_cmp expect_update actual
+'
+
+cat >expect <<EOF
+index $(test_oid hasht1)..$(test_oid hasht2) 100644
+EOF
+
+test_expect_success 'diff does not produce ambiguous index line' '
+ git diff HEAD^..HEAD | grep index > actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t4045-diff-relative.sh b/t/t4045-diff-relative.sh
new file mode 100755
index 0000000..9b46c4c
--- /dev/null
+++ b/t/t4045-diff-relative.sh
@@ -0,0 +1,249 @@
+#!/bin/sh
+
+test_description='diff --relative tests'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ git commit --allow-empty -m empty &&
+ echo content >file1 &&
+ mkdir subdir &&
+ echo other content >subdir/file2 &&
+ blob_file1=$(git hash-object file1) &&
+ blob_file2=$(git hash-object subdir/file2) &&
+ git add . &&
+ git commit -m one
+'
+
+check_diff () {
+ dir=$1
+ shift
+ expect=$1
+ shift
+ short_blob=$(git rev-parse --short $blob_file2)
+ cat >expected <<-EOF
+ diff --git a/$expect b/$expect
+ new file mode 100644
+ index 0000000..$short_blob
+ --- /dev/null
+ +++ b/$expect
+ @@ -0,0 +1 @@
+ +other content
+ EOF
+ test_expect_success "-p $*" "
+ git -C '$dir' diff -p $* HEAD^ >actual &&
+ test_cmp expected actual
+ "
+}
+
+check_numstat () {
+ dir=$1
+ shift
+ expect=$1
+ shift
+ cat >expected <<-EOF
+ 1 0 $expect
+ EOF
+ test_expect_success "--numstat $*" "
+ echo '1 0 $expect' >expected &&
+ git -C '$dir' diff --numstat $* HEAD^ >actual &&
+ test_cmp expected actual
+ "
+}
+
+check_stat () {
+ dir=$1
+ shift
+ expect=$1
+ shift
+ cat >expected <<-EOF
+ $expect | 1 +
+ 1 file changed, 1 insertion(+)
+ EOF
+ test_expect_success "--stat $*" "
+ git -C '$dir' diff --stat $* HEAD^ >actual &&
+ test_cmp expected actual
+ "
+}
+
+check_raw () {
+ dir=$1
+ shift
+ expect=$1
+ shift
+ cat >expected <<-EOF
+ :000000 100644 $ZERO_OID $blob_file2 A $expect
+ EOF
+ test_expect_success "--raw $*" "
+ git -C '$dir' diff --no-abbrev --raw $* HEAD^ >actual &&
+ test_cmp expected actual
+ "
+}
+
+for type in diff numstat stat raw
+do
+ check_$type . file2 --relative=subdir/
+ check_$type . file2 --relative=subdir
+ check_$type subdir file2 --relative
+ check_$type . dir/file2 --relative=sub
+done
+
+check_diff_relative_option () {
+ dir=$1
+ shift
+ expect=$1
+ shift
+ relative_opt=$1
+ shift
+ test_expect_success "config diff.relative $relative_opt -p $*" "
+ short_blob=\$(git rev-parse --short $blob_file2) &&
+ cat >expected <<-EOF &&
+ diff --git a/$expect b/$expect
+ new file mode 100644
+ index 0000000..\$short_blob
+ --- /dev/null
+ +++ b/$expect
+ @@ -0,0 +1 @@
+ +other content
+ EOF
+ test_config -C $dir diff.relative $relative_opt &&
+ git -C '$dir' diff -p $* HEAD^ >actual &&
+ test_cmp expected actual
+ "
+}
+
+check_diff_no_relative_option () {
+ dir=$1
+ shift
+ expect=$1
+ shift
+ relative_opt=$1
+ shift
+ test_expect_success "config diff.relative $relative_opt -p $*" "
+ short_blob_file1=\$(git rev-parse --short $blob_file1) &&
+ short_blob_file2=\$(git rev-parse --short $blob_file2) &&
+ cat >expected <<-EOF &&
+ diff --git a/file1 b/file1
+ new file mode 100644
+ index 0000000..\$short_blob_file1
+ --- /dev/null
+ +++ b/file1
+ @@ -0,0 +1 @@
+ +content
+ diff --git a/$expect b/$expect
+ new file mode 100644
+ index 0000000..\$short_blob_file2
+ --- /dev/null
+ +++ b/$expect
+ @@ -0,0 +1 @@
+ +other content
+ EOF
+ test_config -C $dir diff.relative $relative_opt &&
+ git -C '$dir' diff -p $* HEAD^ >actual &&
+ test_cmp expected actual
+ "
+}
+
+check_diff_no_relative_option . subdir/file2 false
+check_diff_no_relative_option . subdir/file2 true --no-relative
+check_diff_no_relative_option . subdir/file2 false --no-relative
+check_diff_no_relative_option subdir subdir/file2 false
+check_diff_no_relative_option subdir subdir/file2 true --no-relative
+check_diff_no_relative_option subdir subdir/file2 false --no-relative
+
+check_diff_relative_option . file2 false --relative=subdir/
+check_diff_relative_option . file2 false --relative=subdir
+check_diff_relative_option . file2 true --relative=subdir/
+check_diff_relative_option . file2 true --relative=subdir
+check_diff_relative_option subdir file2 false --relative
+check_diff_relative_option subdir file2 true --relative
+check_diff_relative_option subdir file2 true
+check_diff_relative_option subdir file2 false --no-relative --relative
+check_diff_relative_option subdir file2 true --no-relative --relative
+check_diff_relative_option . file2 false --no-relative --relative=subdir
+check_diff_relative_option . file2 true --no-relative --relative=subdir
+
+test_expect_success 'external diff with --relative' '
+ test_when_finished "git reset --hard" &&
+ echo changed >file1 &&
+ echo changed >subdir/file2 &&
+
+ write_script mydiff <<-\EOF &&
+ # hacky pretend diff; the goal here is just to make sure we got
+ # passed sensible input that we _could_ diff, without relying on
+ # the specific output of a system diff tool.
+ echo "diff a/$1 b/$1" &&
+ echo "--- a/$1" &&
+ echo "+++ b/$1" &&
+ echo "@@ -1 +0,0 @@" &&
+ sed "s/^/-/" "$2" &&
+ sed "s/^/+/" "$5"
+ EOF
+
+ cat >expect <<-\EOF &&
+ diff a/file2 b/file2
+ --- a/file2
+ +++ b/file2
+ @@ -1 +0,0 @@
+ -other content
+ +changed
+ EOF
+ GIT_EXTERNAL_DIFF=./mydiff git diff --relative=subdir >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'setup diff --relative unmerged' '
+ test_commit zero file0 &&
+ test_commit base subdir/file0 &&
+ git switch -c br1 &&
+ test_commit one file0 &&
+ test_commit sub1 subdir/file0 &&
+ git switch -c br2 base &&
+ test_commit two file0 &&
+ git switch -c br3 &&
+ test_commit sub3 subdir/file0
+'
+
+test_expect_success 'diff --relative without change in subdir' '
+ git switch br2 &&
+ test_when_finished "git merge --abort" &&
+ test_must_fail git merge one &&
+ git -C subdir diff --relative >out &&
+ test_must_be_empty out &&
+ git -C subdir diff --relative --name-only >out &&
+ test_must_be_empty out
+'
+
+test_expect_success 'diff --relative --name-only with change in subdir' '
+ git switch br3 &&
+ test_when_finished "git merge --abort" &&
+ test_must_fail git merge sub1 &&
+ test_write_lines file0 file0 >expected &&
+ git -C subdir diff --relative --name-only >out &&
+ test_cmp expected out
+'
+
+test_expect_failure 'diff --relative with change in subdir' '
+ git switch br3 &&
+ br1_blob=$(git rev-parse --short --verify br1:subdir/file0) &&
+ br3_blob=$(git rev-parse --short --verify br3:subdir/file0) &&
+ test_when_finished "git merge --abort" &&
+ test_must_fail git merge br1 &&
+ cat >expected <<-EOF &&
+ diff --cc file0
+ index $br3_blob,$br1_blob..0000000
+ --- a/file0
+ +++ b/file0
+ @@@ -1,1 -1,1 +1,5 @@@
+ ++<<<<<<< HEAD
+ +sub3
+ ++=======
+ + sub1
+ ++>>>>>>> br1
+ EOF
+ git -C subdir diff --relative >out &&
+ test_cmp expected out
+'
+
+test_done
diff --git a/t/t4046-diff-unmerged.sh b/t/t4046-diff-unmerged.sh
new file mode 100755
index 0000000..ffaf693
--- /dev/null
+++ b/t/t4046-diff-unmerged.sh
@@ -0,0 +1,99 @@
+#!/bin/sh
+
+test_description='diff with unmerged index entries'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ for i in 0 1 2 3
+ do
+ blob=$(echo $i | git hash-object --stdin) &&
+ eval "blob$i=$blob" &&
+ eval "m$i=\"100644 \$blob$i $i\"" || return 1
+ done &&
+ paths= &&
+ for b in o x
+ do
+ for o in o x
+ do
+ for t in o x
+ do
+ path="$b$o$t" &&
+ case "$path" in ooo) continue ;; esac &&
+ paths="$paths$path " &&
+ p=" $path" &&
+ case "$b" in x) echo "$m1$p" ;; esac &&
+ case "$o" in x) echo "$m2$p" ;; esac &&
+ case "$t" in x) echo "$m3$p" ;; esac ||
+ return 1
+ done
+ done
+ done >ls-files-s.expect &&
+ git update-index --index-info <ls-files-s.expect &&
+ git ls-files -s >ls-files-s.actual &&
+ test_cmp ls-files-s.expect ls-files-s.actual
+'
+
+test_expect_success 'diff-files -0' '
+ for path in $paths
+ do
+ >"$path" &&
+ echo ":000000 100644 $ZERO_OID $ZERO_OID U $path" || return 1
+ done >diff-files-0.expect &&
+ git diff-files -0 >diff-files-0.actual &&
+ test_cmp diff-files-0.expect diff-files-0.actual
+'
+
+test_expect_success 'diff-files -1' '
+ for path in $paths
+ do
+ >"$path" &&
+ echo ":000000 100644 $ZERO_OID $ZERO_OID U $path" &&
+ case "$path" in
+ x??) echo ":100644 100644 $blob1 $ZERO_OID M $path"
+ esac || return 1
+ done >diff-files-1.expect &&
+ git diff-files -1 >diff-files-1.actual &&
+ test_cmp diff-files-1.expect diff-files-1.actual
+'
+
+test_expect_success 'diff-files -2' '
+ for path in $paths
+ do
+ >"$path" &&
+ echo ":000000 100644 $ZERO_OID $ZERO_OID U $path" &&
+ case "$path" in
+ ?x?) echo ":100644 100644 $blob2 $ZERO_OID M $path"
+ esac || return 1
+ done >diff-files-2.expect &&
+ git diff-files -2 >diff-files-2.actual &&
+ test_cmp diff-files-2.expect diff-files-2.actual &&
+ git diff-files >diff-files-default-2.actual &&
+ test_cmp diff-files-2.expect diff-files-default-2.actual
+'
+
+test_expect_success 'diff-files -3' '
+ for path in $paths
+ do
+ >"$path" &&
+ echo ":000000 100644 $ZERO_OID $ZERO_OID U $path" &&
+ case "$path" in
+ ??x) echo ":100644 100644 $blob3 $ZERO_OID M $path"
+ esac || return 1
+ done >diff-files-3.expect &&
+ git diff-files -3 >diff-files-3.actual &&
+ test_cmp diff-files-3.expect diff-files-3.actual
+'
+
+test_expect_success 'diff --stat' '
+ for path in $paths
+ do
+ echo " $path | Unmerged" || return 1
+ done >diff-stat.expect &&
+ echo " 0 files changed" >>diff-stat.expect &&
+ git diff --cached --stat >diff-stat.actual &&
+ test_cmp diff-stat.expect diff-stat.actual
+'
+
+test_done
diff --git a/t/t4047-diff-dirstat.sh b/t/t4047-diff-dirstat.sh
new file mode 100755
index 0000000..7b73462
--- /dev/null
+++ b/t/t4047-diff-dirstat.sh
@@ -0,0 +1,993 @@
+#!/bin/sh
+
+test_description='diff --dirstat tests'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# set up two commits where the second commit has these files
+# (10 lines in each file):
+#
+# unchanged/text (unchanged from 1st commit)
+# changed/text (changed 1st line)
+# rearranged/text (swapped 1st and 2nd line)
+# dst/copy/unchanged/text (copied from src/copy/unchanged/text, unchanged)
+# dst/copy/changed/text (copied from src/copy/changed/text, changed)
+# dst/copy/rearranged/text (copied from src/copy/rearranged/text, rearranged)
+# dst/move/unchanged/text (moved from src/move/unchanged/text, unchanged)
+# dst/move/changed/text (moved from src/move/changed/text, changed)
+# dst/move/rearranged/text (moved from src/move/rearranged/text, rearranged)
+
+test_expect_success 'setup' '
+ mkdir unchanged &&
+ mkdir changed &&
+ mkdir rearranged &&
+ mkdir src &&
+ mkdir src/copy &&
+ mkdir src/copy/unchanged &&
+ mkdir src/copy/changed &&
+ mkdir src/copy/rearranged &&
+ mkdir src/move &&
+ mkdir src/move/unchanged &&
+ mkdir src/move/changed &&
+ mkdir src/move/rearranged &&
+ cat <<EOF >unchanged/text &&
+unchanged line #0
+unchanged line #1
+unchanged line #2
+unchanged line #3
+unchanged line #4
+unchanged line #5
+unchanged line #6
+unchanged line #7
+unchanged line #8
+unchanged line #9
+EOF
+ cat <<EOF >changed/text &&
+changed line #0
+changed line #1
+changed line #2
+changed line #3
+changed line #4
+changed line #5
+changed line #6
+changed line #7
+changed line #8
+changed line #9
+EOF
+ cat <<EOF >rearranged/text &&
+rearranged line #0
+rearranged line #1
+rearranged line #2
+rearranged line #3
+rearranged line #4
+rearranged line #5
+rearranged line #6
+rearranged line #7
+rearranged line #8
+rearranged line #9
+EOF
+ cat <<EOF >src/copy/unchanged/text &&
+copy unchanged line #0
+copy unchanged line #1
+copy unchanged line #2
+copy unchanged line #3
+copy unchanged line #4
+copy unchanged line #5
+copy unchanged line #6
+copy unchanged line #7
+copy unchanged line #8
+copy unchanged line #9
+EOF
+ cat <<EOF >src/copy/changed/text &&
+copy changed line #0
+copy changed line #1
+copy changed line #2
+copy changed line #3
+copy changed line #4
+copy changed line #5
+copy changed line #6
+copy changed line #7
+copy changed line #8
+copy changed line #9
+EOF
+ cat <<EOF >src/copy/rearranged/text &&
+copy rearranged line #0
+copy rearranged line #1
+copy rearranged line #2
+copy rearranged line #3
+copy rearranged line #4
+copy rearranged line #5
+copy rearranged line #6
+copy rearranged line #7
+copy rearranged line #8
+copy rearranged line #9
+EOF
+ cat <<EOF >src/move/unchanged/text &&
+move unchanged line #0
+move unchanged line #1
+move unchanged line #2
+move unchanged line #3
+move unchanged line #4
+move unchanged line #5
+move unchanged line #6
+move unchanged line #7
+move unchanged line #8
+move unchanged line #9
+EOF
+ cat <<EOF >src/move/changed/text &&
+move changed line #0
+move changed line #1
+move changed line #2
+move changed line #3
+move changed line #4
+move changed line #5
+move changed line #6
+move changed line #7
+move changed line #8
+move changed line #9
+EOF
+ cat <<EOF >src/move/rearranged/text &&
+move rearranged line #0
+move rearranged line #1
+move rearranged line #2
+move rearranged line #3
+move rearranged line #4
+move rearranged line #5
+move rearranged line #6
+move rearranged line #7
+move rearranged line #8
+move rearranged line #9
+EOF
+ git add . &&
+ git commit -m "initial" &&
+ mkdir dst &&
+ mkdir dst/copy &&
+ mkdir dst/copy/unchanged &&
+ mkdir dst/copy/changed &&
+ mkdir dst/copy/rearranged &&
+ mkdir dst/move &&
+ mkdir dst/move/unchanged &&
+ mkdir dst/move/changed &&
+ mkdir dst/move/rearranged &&
+ cat <<EOF >changed/text &&
+CHANGED XXXXXXX line #0
+changed line #1
+changed line #2
+changed line #3
+changed line #4
+changed line #5
+changed line #6
+changed line #7
+changed line #8
+changed line #9
+EOF
+ cat <<EOF >rearranged/text &&
+rearranged line #1
+rearranged line #0
+rearranged line #2
+rearranged line #3
+rearranged line #4
+rearranged line #5
+rearranged line #6
+rearranged line #7
+rearranged line #8
+rearranged line #9
+EOF
+ cat <<EOF >dst/copy/unchanged/text &&
+copy unchanged line #0
+copy unchanged line #1
+copy unchanged line #2
+copy unchanged line #3
+copy unchanged line #4
+copy unchanged line #5
+copy unchanged line #6
+copy unchanged line #7
+copy unchanged line #8
+copy unchanged line #9
+EOF
+ cat <<EOF >dst/copy/changed/text &&
+copy XXXCHANGED line #0
+copy changed line #1
+copy changed line #2
+copy changed line #3
+copy changed line #4
+copy changed line #5
+copy changed line #6
+copy changed line #7
+copy changed line #8
+copy changed line #9
+EOF
+ cat <<EOF >dst/copy/rearranged/text &&
+copy rearranged line #1
+copy rearranged line #0
+copy rearranged line #2
+copy rearranged line #3
+copy rearranged line #4
+copy rearranged line #5
+copy rearranged line #6
+copy rearranged line #7
+copy rearranged line #8
+copy rearranged line #9
+EOF
+ cat <<EOF >dst/move/unchanged/text &&
+move unchanged line #0
+move unchanged line #1
+move unchanged line #2
+move unchanged line #3
+move unchanged line #4
+move unchanged line #5
+move unchanged line #6
+move unchanged line #7
+move unchanged line #8
+move unchanged line #9
+EOF
+ cat <<EOF >dst/move/changed/text &&
+move XXXCHANGED line #0
+move changed line #1
+move changed line #2
+move changed line #3
+move changed line #4
+move changed line #5
+move changed line #6
+move changed line #7
+move changed line #8
+move changed line #9
+EOF
+ cat <<EOF >dst/move/rearranged/text &&
+move rearranged line #1
+move rearranged line #0
+move rearranged line #2
+move rearranged line #3
+move rearranged line #4
+move rearranged line #5
+move rearranged line #6
+move rearranged line #7
+move rearranged line #8
+move rearranged line #9
+EOF
+ git add . &&
+ git rm -r src/move/unchanged &&
+ git rm -r src/move/changed &&
+ git rm -r src/move/rearranged &&
+ git commit -m "changes" &&
+ git config diff.renames false
+'
+
+cat <<EOF >expect_diff_stat
+1 1 changed/text
+10 0 dst/copy/changed/text
+10 0 dst/copy/rearranged/text
+10 0 dst/copy/unchanged/text
+10 0 dst/move/changed/text
+10 0 dst/move/rearranged/text
+10 0 dst/move/unchanged/text
+1 1 rearranged/text
+0 10 src/move/changed/text
+0 10 src/move/rearranged/text
+0 10 src/move/unchanged/text
+EOF
+
+cat <<EOF >expect_diff_stat_M
+1 1 changed/text
+10 0 dst/copy/changed/text
+10 0 dst/copy/rearranged/text
+10 0 dst/copy/unchanged/text
+1 1 {src => dst}/move/changed/text
+1 1 {src => dst}/move/rearranged/text
+0 0 {src => dst}/move/unchanged/text
+1 1 rearranged/text
+EOF
+
+cat <<EOF >expect_diff_stat_CC
+1 1 changed/text
+1 1 {src => dst}/copy/changed/text
+1 1 {src => dst}/copy/rearranged/text
+0 0 {src => dst}/copy/unchanged/text
+1 1 {src => dst}/move/changed/text
+1 1 {src => dst}/move/rearranged/text
+0 0 {src => dst}/move/unchanged/text
+1 1 rearranged/text
+EOF
+
+test_expect_success 'sanity check setup (--numstat)' '
+ git diff --numstat HEAD^..HEAD >actual_diff_stat &&
+ test_cmp expect_diff_stat actual_diff_stat &&
+ git diff --numstat -M HEAD^..HEAD >actual_diff_stat_M &&
+ test_cmp expect_diff_stat_M actual_diff_stat_M &&
+ git diff --numstat -C -C HEAD^..HEAD >actual_diff_stat_CC &&
+ test_cmp expect_diff_stat_CC actual_diff_stat_CC
+'
+
+# changed/text and rearranged/text falls below default 3% threshold
+cat <<EOF >expect_diff_dirstat
+ 10.8% dst/copy/changed/
+ 10.8% dst/copy/rearranged/
+ 10.8% dst/copy/unchanged/
+ 10.8% dst/move/changed/
+ 10.8% dst/move/rearranged/
+ 10.8% dst/move/unchanged/
+ 10.8% src/move/changed/
+ 10.8% src/move/rearranged/
+ 10.8% src/move/unchanged/
+EOF
+
+# rearranged/text falls below default 3% threshold
+cat <<EOF >expect_diff_dirstat_M
+ 5.8% changed/
+ 29.3% dst/copy/changed/
+ 29.3% dst/copy/rearranged/
+ 29.3% dst/copy/unchanged/
+ 5.8% dst/move/changed/
+EOF
+
+# rearranged/text falls below default 3% threshold
+cat <<EOF >expect_diff_dirstat_CC
+ 32.6% changed/
+ 32.6% dst/copy/changed/
+ 32.6% dst/move/changed/
+EOF
+
+test_expect_success 'various ways to misspell --dirstat' '
+ test_must_fail git show --dirstat10 &&
+ test_must_fail git show --dirstat10,files &&
+ test_must_fail git show -X=20 &&
+ test_must_fail git show -X=20,cumulative
+'
+
+test_expect_success 'vanilla --dirstat' '
+ git diff --dirstat HEAD^..HEAD >actual_diff_dirstat &&
+ test_cmp expect_diff_dirstat actual_diff_dirstat &&
+ git diff --dirstat -M HEAD^..HEAD >actual_diff_dirstat_M &&
+ test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
+ git diff --dirstat -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
+ test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
+'
+
+test_expect_success 'vanilla -X' '
+ git diff -X HEAD^..HEAD >actual_diff_dirstat &&
+ test_cmp expect_diff_dirstat actual_diff_dirstat &&
+ git diff -X -M HEAD^..HEAD >actual_diff_dirstat_M &&
+ test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
+ git diff -X -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
+ test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
+'
+
+test_expect_success 'explicit defaults: --dirstat=changes,noncumulative,3' '
+ git diff --dirstat=changes,noncumulative,3 HEAD^..HEAD >actual_diff_dirstat &&
+ test_cmp expect_diff_dirstat actual_diff_dirstat &&
+ git diff --dirstat=changes,noncumulative,3 -M HEAD^..HEAD >actual_diff_dirstat_M &&
+ test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
+ git diff --dirstat=changes,noncumulative,3 -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
+ test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
+'
+
+test_expect_success 'explicit defaults: -Xchanges,noncumulative,3' '
+ git diff -Xchanges,noncumulative,3 HEAD^..HEAD >actual_diff_dirstat &&
+ test_cmp expect_diff_dirstat actual_diff_dirstat &&
+ git diff -Xchanges,noncumulative,3 -M HEAD^..HEAD >actual_diff_dirstat_M &&
+ test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
+ git diff -Xchanges,noncumulative,3 -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
+ test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
+'
+
+test_expect_success 'later options override earlier options:' '
+ git diff --dirstat=files,10,cumulative,changes,noncumulative,3 HEAD^..HEAD >actual_diff_dirstat &&
+ test_cmp expect_diff_dirstat actual_diff_dirstat &&
+ git diff --dirstat=files,10,cumulative,changes,noncumulative,3 -M HEAD^..HEAD >actual_diff_dirstat_M &&
+ test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
+ git diff --dirstat=files,10,cumulative,changes,noncumulative,3 -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
+ test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC &&
+ git diff --dirstat=files --dirstat=10 --dirstat=cumulative --dirstat=changes --dirstat=noncumulative -X3 HEAD^..HEAD >actual_diff_dirstat &&
+ test_cmp expect_diff_dirstat actual_diff_dirstat &&
+ git diff --dirstat=files --dirstat=10 --dirstat=cumulative --dirstat=changes --dirstat=noncumulative -X3 -M HEAD^..HEAD >actual_diff_dirstat_M &&
+ test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
+ git diff --dirstat=files --dirstat=10 --dirstat=cumulative --dirstat=changes --dirstat=noncumulative -X3 -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
+ test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
+'
+
+test_expect_success 'non-defaults in config overridden by explicit defaults on command line' '
+ git -c diff.dirstat=files,cumulative,50 diff --dirstat=changes,noncumulative,3 HEAD^..HEAD >actual_diff_dirstat &&
+ test_cmp expect_diff_dirstat actual_diff_dirstat &&
+ git -c diff.dirstat=files,cumulative,50 diff --dirstat=changes,noncumulative,3 -M HEAD^..HEAD >actual_diff_dirstat_M &&
+ test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
+ git -c diff.dirstat=files,cumulative,50 diff --dirstat=changes,noncumulative,3 -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
+ test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
+'
+
+cat <<EOF >expect_diff_dirstat
+ 2.1% changed/
+ 10.8% dst/copy/changed/
+ 10.8% dst/copy/rearranged/
+ 10.8% dst/copy/unchanged/
+ 10.8% dst/move/changed/
+ 10.8% dst/move/rearranged/
+ 10.8% dst/move/unchanged/
+ 0.0% rearranged/
+ 10.8% src/move/changed/
+ 10.8% src/move/rearranged/
+ 10.8% src/move/unchanged/
+EOF
+
+cat <<EOF >expect_diff_dirstat_M
+ 5.8% changed/
+ 29.3% dst/copy/changed/
+ 29.3% dst/copy/rearranged/
+ 29.3% dst/copy/unchanged/
+ 5.8% dst/move/changed/
+ 0.1% dst/move/rearranged/
+ 0.1% rearranged/
+EOF
+
+cat <<EOF >expect_diff_dirstat_CC
+ 32.6% changed/
+ 32.6% dst/copy/changed/
+ 0.6% dst/copy/rearranged/
+ 32.6% dst/move/changed/
+ 0.6% dst/move/rearranged/
+ 0.6% rearranged/
+EOF
+
+test_expect_success '--dirstat=0' '
+ git diff --dirstat=0 HEAD^..HEAD >actual_diff_dirstat &&
+ test_cmp expect_diff_dirstat actual_diff_dirstat &&
+ git diff --dirstat=0 -M HEAD^..HEAD >actual_diff_dirstat_M &&
+ test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
+ git diff --dirstat=0 -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
+ test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
+'
+
+test_expect_success '-X0' '
+ git diff -X0 HEAD^..HEAD >actual_diff_dirstat &&
+ test_cmp expect_diff_dirstat actual_diff_dirstat &&
+ git diff -X0 -M HEAD^..HEAD >actual_diff_dirstat_M &&
+ test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
+ git diff -X0 -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
+ test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
+'
+
+test_expect_success 'diff.dirstat=0' '
+ git -c diff.dirstat=0 diff --dirstat HEAD^..HEAD >actual_diff_dirstat &&
+ test_cmp expect_diff_dirstat actual_diff_dirstat &&
+ git -c diff.dirstat=0 diff --dirstat -M HEAD^..HEAD >actual_diff_dirstat_M &&
+ test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
+ git -c diff.dirstat=0 diff --dirstat -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
+ test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
+'
+
+cat <<EOF >expect_diff_dirstat
+ 2.1% changed/
+ 10.8% dst/copy/changed/
+ 10.8% dst/copy/rearranged/
+ 10.8% dst/copy/unchanged/
+ 32.5% dst/copy/
+ 10.8% dst/move/changed/
+ 10.8% dst/move/rearranged/
+ 10.8% dst/move/unchanged/
+ 32.5% dst/move/
+ 65.1% dst/
+ 0.0% rearranged/
+ 10.8% src/move/changed/
+ 10.8% src/move/rearranged/
+ 10.8% src/move/unchanged/
+ 32.5% src/move/
+EOF
+
+cat <<EOF >expect_diff_dirstat_M
+ 5.8% changed/
+ 29.3% dst/copy/changed/
+ 29.3% dst/copy/rearranged/
+ 29.3% dst/copy/unchanged/
+ 88.0% dst/copy/
+ 5.8% dst/move/changed/
+ 0.1% dst/move/rearranged/
+ 5.9% dst/move/
+ 94.0% dst/
+ 0.1% rearranged/
+EOF
+
+cat <<EOF >expect_diff_dirstat_CC
+ 32.6% changed/
+ 32.6% dst/copy/changed/
+ 0.6% dst/copy/rearranged/
+ 33.3% dst/copy/
+ 32.6% dst/move/changed/
+ 0.6% dst/move/rearranged/
+ 33.3% dst/move/
+ 66.6% dst/
+ 0.6% rearranged/
+EOF
+
+test_expect_success '--dirstat=0 --cumulative' '
+ git diff --dirstat=0 --cumulative HEAD^..HEAD >actual_diff_dirstat &&
+ test_cmp expect_diff_dirstat actual_diff_dirstat &&
+ git diff --dirstat=0 --cumulative -M HEAD^..HEAD >actual_diff_dirstat_M &&
+ test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
+ git diff --dirstat=0 --cumulative -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
+ test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
+'
+
+test_expect_success '--dirstat=0,cumulative' '
+ git diff --dirstat=0,cumulative HEAD^..HEAD >actual_diff_dirstat &&
+ test_cmp expect_diff_dirstat actual_diff_dirstat &&
+ git diff --dirstat=0,cumulative -M HEAD^..HEAD >actual_diff_dirstat_M &&
+ test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
+ git diff --dirstat=0,cumulative -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
+ test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
+'
+
+test_expect_success '-X0,cumulative' '
+ git diff -X0,cumulative HEAD^..HEAD >actual_diff_dirstat &&
+ test_cmp expect_diff_dirstat actual_diff_dirstat &&
+ git diff -X0,cumulative -M HEAD^..HEAD >actual_diff_dirstat_M &&
+ test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
+ git diff -X0,cumulative -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
+ test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
+'
+
+test_expect_success 'diff.dirstat=0,cumulative' '
+ git -c diff.dirstat=0,cumulative diff --dirstat HEAD^..HEAD >actual_diff_dirstat &&
+ test_cmp expect_diff_dirstat actual_diff_dirstat &&
+ git -c diff.dirstat=0,cumulative diff --dirstat -M HEAD^..HEAD >actual_diff_dirstat_M &&
+ test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
+ git -c diff.dirstat=0,cumulative diff --dirstat -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
+ test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
+'
+
+test_expect_success 'diff.dirstat=0 & --dirstat=cumulative' '
+ git -c diff.dirstat=0 diff --dirstat=cumulative HEAD^..HEAD >actual_diff_dirstat &&
+ test_cmp expect_diff_dirstat actual_diff_dirstat &&
+ git -c diff.dirstat=0 diff --dirstat=cumulative -M HEAD^..HEAD >actual_diff_dirstat_M &&
+ test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
+ git -c diff.dirstat=0 diff --dirstat=cumulative -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
+ test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
+'
+
+cat <<EOF >expect_diff_dirstat
+ 9.0% changed/
+ 9.0% dst/copy/changed/
+ 9.0% dst/copy/rearranged/
+ 9.0% dst/copy/unchanged/
+ 9.0% dst/move/changed/
+ 9.0% dst/move/rearranged/
+ 9.0% dst/move/unchanged/
+ 9.0% rearranged/
+ 9.0% src/move/changed/
+ 9.0% src/move/rearranged/
+ 9.0% src/move/unchanged/
+EOF
+
+cat <<EOF >expect_diff_dirstat_M
+ 14.2% changed/
+ 14.2% dst/copy/changed/
+ 14.2% dst/copy/rearranged/
+ 14.2% dst/copy/unchanged/
+ 14.2% dst/move/changed/
+ 14.2% dst/move/rearranged/
+ 14.2% rearranged/
+EOF
+
+cat <<EOF >expect_diff_dirstat_CC
+ 16.6% changed/
+ 16.6% dst/copy/changed/
+ 16.6% dst/copy/rearranged/
+ 16.6% dst/move/changed/
+ 16.6% dst/move/rearranged/
+ 16.6% rearranged/
+EOF
+
+test_expect_success '--dirstat-by-file' '
+ git diff --dirstat-by-file HEAD^..HEAD >actual_diff_dirstat &&
+ test_cmp expect_diff_dirstat actual_diff_dirstat &&
+ git diff --dirstat-by-file -M HEAD^..HEAD >actual_diff_dirstat_M &&
+ test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
+ git diff --dirstat-by-file -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
+ test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
+'
+
+test_expect_success '--dirstat=files' '
+ git diff --dirstat=files HEAD^..HEAD >actual_diff_dirstat &&
+ test_cmp expect_diff_dirstat actual_diff_dirstat &&
+ git diff --dirstat=files -M HEAD^..HEAD >actual_diff_dirstat_M &&
+ test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
+ git diff --dirstat=files -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
+ test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
+'
+
+test_expect_success 'diff.dirstat=files' '
+ git -c diff.dirstat=files diff --dirstat HEAD^..HEAD >actual_diff_dirstat &&
+ test_cmp expect_diff_dirstat actual_diff_dirstat &&
+ git -c diff.dirstat=files diff --dirstat -M HEAD^..HEAD >actual_diff_dirstat_M &&
+ test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
+ git -c diff.dirstat=files diff --dirstat -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
+ test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
+'
+
+cat <<EOF >expect_diff_dirstat
+ 27.2% dst/copy/
+ 27.2% dst/move/
+ 27.2% src/move/
+EOF
+
+cat <<EOF >expect_diff_dirstat_M
+ 14.2% changed/
+ 14.2% dst/copy/changed/
+ 14.2% dst/copy/rearranged/
+ 14.2% dst/copy/unchanged/
+ 14.2% dst/move/changed/
+ 14.2% dst/move/rearranged/
+ 14.2% rearranged/
+EOF
+
+cat <<EOF >expect_diff_dirstat_CC
+ 16.6% changed/
+ 16.6% dst/copy/changed/
+ 16.6% dst/copy/rearranged/
+ 16.6% dst/move/changed/
+ 16.6% dst/move/rearranged/
+ 16.6% rearranged/
+EOF
+
+test_expect_success '--dirstat-by-file=10' '
+ git diff --dirstat-by-file=10 HEAD^..HEAD >actual_diff_dirstat &&
+ test_cmp expect_diff_dirstat actual_diff_dirstat &&
+ git diff --dirstat-by-file=10 -M HEAD^..HEAD >actual_diff_dirstat_M &&
+ test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
+ git diff --dirstat-by-file=10 -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
+ test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
+'
+
+test_expect_success '--dirstat=files,10' '
+ git diff --dirstat=files,10 HEAD^..HEAD >actual_diff_dirstat &&
+ test_cmp expect_diff_dirstat actual_diff_dirstat &&
+ git diff --dirstat=files,10 -M HEAD^..HEAD >actual_diff_dirstat_M &&
+ test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
+ git diff --dirstat=files,10 -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
+ test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
+'
+
+test_expect_success 'diff.dirstat=10,files' '
+ git -c diff.dirstat=10,files diff --dirstat HEAD^..HEAD >actual_diff_dirstat &&
+ test_cmp expect_diff_dirstat actual_diff_dirstat &&
+ git -c diff.dirstat=10,files diff --dirstat -M HEAD^..HEAD >actual_diff_dirstat_M &&
+ test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
+ git -c diff.dirstat=10,files diff --dirstat -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
+ test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
+'
+
+cat <<EOF >expect_diff_dirstat
+ 9.0% changed/
+ 9.0% dst/copy/changed/
+ 9.0% dst/copy/rearranged/
+ 9.0% dst/copy/unchanged/
+ 27.2% dst/copy/
+ 9.0% dst/move/changed/
+ 9.0% dst/move/rearranged/
+ 9.0% dst/move/unchanged/
+ 27.2% dst/move/
+ 54.5% dst/
+ 9.0% rearranged/
+ 9.0% src/move/changed/
+ 9.0% src/move/rearranged/
+ 9.0% src/move/unchanged/
+ 27.2% src/move/
+EOF
+
+cat <<EOF >expect_diff_dirstat_M
+ 14.2% changed/
+ 14.2% dst/copy/changed/
+ 14.2% dst/copy/rearranged/
+ 14.2% dst/copy/unchanged/
+ 42.8% dst/copy/
+ 14.2% dst/move/changed/
+ 14.2% dst/move/rearranged/
+ 28.5% dst/move/
+ 71.4% dst/
+ 14.2% rearranged/
+EOF
+
+cat <<EOF >expect_diff_dirstat_CC
+ 16.6% changed/
+ 16.6% dst/copy/changed/
+ 16.6% dst/copy/rearranged/
+ 33.3% dst/copy/
+ 16.6% dst/move/changed/
+ 16.6% dst/move/rearranged/
+ 33.3% dst/move/
+ 66.6% dst/
+ 16.6% rearranged/
+EOF
+
+test_expect_success '--dirstat-by-file --cumulative' '
+ git diff --dirstat-by-file --cumulative HEAD^..HEAD >actual_diff_dirstat &&
+ test_cmp expect_diff_dirstat actual_diff_dirstat &&
+ git diff --dirstat-by-file --cumulative -M HEAD^..HEAD >actual_diff_dirstat_M &&
+ test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
+ git diff --dirstat-by-file --cumulative -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
+ test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
+'
+
+test_expect_success '--dirstat=files,cumulative' '
+ git diff --dirstat=files,cumulative HEAD^..HEAD >actual_diff_dirstat &&
+ test_cmp expect_diff_dirstat actual_diff_dirstat &&
+ git diff --dirstat=files,cumulative -M HEAD^..HEAD >actual_diff_dirstat_M &&
+ test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
+ git diff --dirstat=files,cumulative -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
+ test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
+'
+
+test_expect_success 'diff.dirstat=cumulative,files' '
+ git -c diff.dirstat=cumulative,files diff --dirstat HEAD^..HEAD >actual_diff_dirstat &&
+ test_cmp expect_diff_dirstat actual_diff_dirstat &&
+ git -c diff.dirstat=cumulative,files diff --dirstat -M HEAD^..HEAD >actual_diff_dirstat_M &&
+ test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
+ git -c diff.dirstat=cumulative,files diff --dirstat -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
+ test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
+'
+
+cat <<EOF >expect_diff_dirstat
+ 27.2% dst/copy/
+ 27.2% dst/move/
+ 54.5% dst/
+ 27.2% src/move/
+EOF
+
+cat <<EOF >expect_diff_dirstat_M
+ 14.2% changed/
+ 14.2% dst/copy/changed/
+ 14.2% dst/copy/rearranged/
+ 14.2% dst/copy/unchanged/
+ 42.8% dst/copy/
+ 14.2% dst/move/changed/
+ 14.2% dst/move/rearranged/
+ 28.5% dst/move/
+ 71.4% dst/
+ 14.2% rearranged/
+EOF
+
+cat <<EOF >expect_diff_dirstat_CC
+ 16.6% changed/
+ 16.6% dst/copy/changed/
+ 16.6% dst/copy/rearranged/
+ 33.3% dst/copy/
+ 16.6% dst/move/changed/
+ 16.6% dst/move/rearranged/
+ 33.3% dst/move/
+ 66.6% dst/
+ 16.6% rearranged/
+EOF
+
+test_expect_success '--dirstat=files,cumulative,10' '
+ git diff --dirstat=files,cumulative,10 HEAD^..HEAD >actual_diff_dirstat &&
+ test_cmp expect_diff_dirstat actual_diff_dirstat &&
+ git diff --dirstat=files,cumulative,10 -M HEAD^..HEAD >actual_diff_dirstat_M &&
+ test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
+ git diff --dirstat=files,cumulative,10 -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
+ test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
+'
+
+test_expect_success 'diff.dirstat=10,cumulative,files' '
+ git -c diff.dirstat=10,cumulative,files diff --dirstat HEAD^..HEAD >actual_diff_dirstat &&
+ test_cmp expect_diff_dirstat actual_diff_dirstat &&
+ git -c diff.dirstat=10,cumulative,files diff --dirstat -M HEAD^..HEAD >actual_diff_dirstat_M &&
+ test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
+ git -c diff.dirstat=10,cumulative,files diff --dirstat -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
+ test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
+'
+
+cat <<EOF >expect_diff_dirstat
+ 27.2% dst/copy/
+ 27.2% dst/move/
+ 54.5% dst/
+ 27.2% src/move/
+EOF
+
+cat <<EOF >expect_diff_dirstat_M
+ 42.8% dst/copy/
+ 28.5% dst/move/
+ 71.4% dst/
+EOF
+
+cat <<EOF >expect_diff_dirstat_CC
+ 33.3% dst/copy/
+ 33.3% dst/move/
+ 66.6% dst/
+EOF
+
+test_expect_success '--dirstat=files,cumulative,16.7' '
+ git diff --dirstat=files,cumulative,16.7 HEAD^..HEAD >actual_diff_dirstat &&
+ test_cmp expect_diff_dirstat actual_diff_dirstat &&
+ git diff --dirstat=files,cumulative,16.7 -M HEAD^..HEAD >actual_diff_dirstat_M &&
+ test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
+ git diff --dirstat=files,cumulative,16.7 -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
+ test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
+'
+
+test_expect_success 'diff.dirstat=16.7,cumulative,files' '
+ git -c diff.dirstat=16.7,cumulative,files diff --dirstat HEAD^..HEAD >actual_diff_dirstat &&
+ test_cmp expect_diff_dirstat actual_diff_dirstat &&
+ git -c diff.dirstat=16.7,cumulative,files diff --dirstat -M HEAD^..HEAD >actual_diff_dirstat_M &&
+ test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
+ git -c diff.dirstat=16.7,cumulative,files diff --dirstat -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
+ test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
+'
+
+test_expect_success 'diff.dirstat=16.70,cumulative,files' '
+ git -c diff.dirstat=16.70,cumulative,files diff --dirstat HEAD^..HEAD >actual_diff_dirstat &&
+ test_cmp expect_diff_dirstat actual_diff_dirstat &&
+ git -c diff.dirstat=16.70,cumulative,files diff --dirstat -M HEAD^..HEAD >actual_diff_dirstat_M &&
+ test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
+ git -c diff.dirstat=16.70,cumulative,files diff --dirstat -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
+ test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
+'
+
+test_expect_success '--dirstat=files,cumulative,27.2' '
+ git diff --dirstat=files,cumulative,27.2 HEAD^..HEAD >actual_diff_dirstat &&
+ test_cmp expect_diff_dirstat actual_diff_dirstat &&
+ git diff --dirstat=files,cumulative,27.2 -M HEAD^..HEAD >actual_diff_dirstat_M &&
+ test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
+ git diff --dirstat=files,cumulative,27.2 -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
+ test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
+'
+
+test_expect_success '--dirstat=files,cumulative,27.09' '
+ git diff --dirstat=files,cumulative,27.09 HEAD^..HEAD >actual_diff_dirstat &&
+ test_cmp expect_diff_dirstat actual_diff_dirstat &&
+ git diff --dirstat=files,cumulative,27.09 -M HEAD^..HEAD >actual_diff_dirstat_M &&
+ test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
+ git diff --dirstat=files,cumulative,27.09 -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
+ test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
+'
+
+cat <<EOF >expect_diff_dirstat
+ 10.6% dst/copy/changed/
+ 10.6% dst/copy/rearranged/
+ 10.6% dst/copy/unchanged/
+ 10.6% dst/move/changed/
+ 10.6% dst/move/rearranged/
+ 10.6% dst/move/unchanged/
+ 10.6% src/move/changed/
+ 10.6% src/move/rearranged/
+ 10.6% src/move/unchanged/
+EOF
+
+cat <<EOF >expect_diff_dirstat_M
+ 5.2% changed/
+ 26.3% dst/copy/changed/
+ 26.3% dst/copy/rearranged/
+ 26.3% dst/copy/unchanged/
+ 5.2% dst/move/changed/
+ 5.2% dst/move/rearranged/
+ 5.2% rearranged/
+EOF
+
+cat <<EOF >expect_diff_dirstat_CC
+ 16.6% changed/
+ 16.6% dst/copy/changed/
+ 16.6% dst/copy/rearranged/
+ 16.6% dst/move/changed/
+ 16.6% dst/move/rearranged/
+ 16.6% rearranged/
+EOF
+
+test_expect_success '--dirstat=lines' '
+ git diff --dirstat=lines HEAD^..HEAD >actual_diff_dirstat &&
+ test_cmp expect_diff_dirstat actual_diff_dirstat &&
+ git diff --dirstat=lines -M HEAD^..HEAD >actual_diff_dirstat_M &&
+ test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
+ git diff --dirstat=lines -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
+ test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
+'
+
+test_expect_success 'diff.dirstat=lines' '
+ git -c diff.dirstat=lines diff --dirstat HEAD^..HEAD >actual_diff_dirstat &&
+ test_cmp expect_diff_dirstat actual_diff_dirstat &&
+ git -c diff.dirstat=lines diff --dirstat -M HEAD^..HEAD >actual_diff_dirstat_M &&
+ test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
+ git -c diff.dirstat=lines diff --dirstat -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
+ test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
+'
+
+cat <<EOF >expect_diff_dirstat
+ 2.1% changed/
+ 10.6% dst/copy/changed/
+ 10.6% dst/copy/rearranged/
+ 10.6% dst/copy/unchanged/
+ 10.6% dst/move/changed/
+ 10.6% dst/move/rearranged/
+ 10.6% dst/move/unchanged/
+ 2.1% rearranged/
+ 10.6% src/move/changed/
+ 10.6% src/move/rearranged/
+ 10.6% src/move/unchanged/
+EOF
+
+cat <<EOF >expect_diff_dirstat_M
+ 5.2% changed/
+ 26.3% dst/copy/changed/
+ 26.3% dst/copy/rearranged/
+ 26.3% dst/copy/unchanged/
+ 5.2% dst/move/changed/
+ 5.2% dst/move/rearranged/
+ 5.2% rearranged/
+EOF
+
+cat <<EOF >expect_diff_dirstat_CC
+ 16.6% changed/
+ 16.6% dst/copy/changed/
+ 16.6% dst/copy/rearranged/
+ 16.6% dst/move/changed/
+ 16.6% dst/move/rearranged/
+ 16.6% rearranged/
+EOF
+
+test_expect_success '--dirstat=lines,0' '
+ git diff --dirstat=lines,0 HEAD^..HEAD >actual_diff_dirstat &&
+ test_cmp expect_diff_dirstat actual_diff_dirstat &&
+ git diff --dirstat=lines,0 -M HEAD^..HEAD >actual_diff_dirstat_M &&
+ test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
+ git diff --dirstat=lines,0 -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
+ test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
+'
+
+test_expect_success 'diff.dirstat=0,lines' '
+ git -c diff.dirstat=0,lines diff --dirstat HEAD^..HEAD >actual_diff_dirstat &&
+ test_cmp expect_diff_dirstat actual_diff_dirstat &&
+ git -c diff.dirstat=0,lines diff --dirstat -M HEAD^..HEAD >actual_diff_dirstat_M &&
+ test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
+ git -c diff.dirstat=0,lines diff --dirstat -C -C HEAD^..HEAD >actual_diff_dirstat_CC &&
+ test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC
+'
+
+test_expect_success '--dirstat=future_param,lines,0 should fail loudly' '
+ test_must_fail git diff --dirstat=future_param,lines,0 HEAD^..HEAD >actual_diff_dirstat 2>actual_error &&
+ test_debug "cat actual_error" &&
+ test_must_be_empty actual_diff_dirstat &&
+ test_grep -q "future_param" actual_error &&
+ test_grep -q "\--dirstat" actual_error
+'
+
+test_expect_success '--dirstat=dummy1,cumulative,2dummy should report both unrecognized parameters' '
+ test_must_fail git diff --dirstat=dummy1,cumulative,2dummy HEAD^..HEAD >actual_diff_dirstat 2>actual_error &&
+ test_debug "cat actual_error" &&
+ test_must_be_empty actual_diff_dirstat &&
+ test_grep -q "dummy1" actual_error &&
+ test_grep -q "2dummy" actual_error &&
+ test_grep -q "\--dirstat" actual_error
+'
+
+test_expect_success 'diff.dirstat=future_param,0,lines should warn, but still work' '
+ git -c diff.dirstat=future_param,0,lines diff --dirstat HEAD^..HEAD >actual_diff_dirstat 2>actual_error &&
+ test_debug "cat actual_error" &&
+ test_cmp expect_diff_dirstat actual_diff_dirstat &&
+ test_grep -q "future_param" actual_error &&
+ test_grep -q "diff\\.dirstat" actual_error &&
+
+ git -c diff.dirstat=future_param,0,lines diff --dirstat -M HEAD^..HEAD >actual_diff_dirstat_M 2>actual_error &&
+ test_debug "cat actual_error" &&
+ test_cmp expect_diff_dirstat_M actual_diff_dirstat_M &&
+ test_grep -q "future_param" actual_error &&
+ test_grep -q "diff\\.dirstat" actual_error &&
+
+ git -c diff.dirstat=future_param,0,lines diff --dirstat -C -C HEAD^..HEAD >actual_diff_dirstat_CC 2>actual_error &&
+ test_debug "cat actual_error" &&
+ test_cmp expect_diff_dirstat_CC actual_diff_dirstat_CC &&
+ test_grep -q "future_param" actual_error &&
+ test_grep -q "diff\\.dirstat" actual_error
+'
+
+test_expect_success '--shortstat --dirstat should output only one dirstat' '
+ git diff --shortstat --dirstat=changes HEAD^..HEAD >out &&
+ grep " dst/copy/changed/$" out >actual_diff_shortstat_dirstat_changes &&
+ test_line_count = 1 actual_diff_shortstat_dirstat_changes &&
+
+ git diff --shortstat --dirstat=lines HEAD^..HEAD >out &&
+ grep " dst/copy/changed/$" out >actual_diff_shortstat_dirstat_lines &&
+ test_line_count = 1 actual_diff_shortstat_dirstat_lines &&
+
+ git diff --shortstat --dirstat=files HEAD^..HEAD >out &&
+ grep " dst/copy/changed/$" out >actual_diff_shortstat_dirstat_files &&
+ test_line_count = 1 actual_diff_shortstat_dirstat_files
+'
+
+test_done
diff --git a/t/t4048-diff-combined-binary.sh b/t/t4048-diff-combined-binary.sh
new file mode 100755
index 0000000..0260cf6
--- /dev/null
+++ b/t/t4048-diff-combined-binary.sh
@@ -0,0 +1,221 @@
+#!/bin/sh
+
+test_description='combined and merge diff handle binary files and textconv'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success 'setup binary merge conflict' '
+ echo oneQ1 | q_to_nul >binary &&
+ git add binary &&
+ git commit -m one &&
+ echo twoQ2 | q_to_nul >binary &&
+ git commit -a -m two &&
+ two=$(git rev-parse --short HEAD:binary) &&
+ git checkout -b branch-binary HEAD^ &&
+ echo threeQ3 | q_to_nul >binary &&
+ git commit -a -m three &&
+ three=$(git rev-parse --short HEAD:binary) &&
+ test_must_fail git merge main &&
+ echo resolvedQhooray | q_to_nul >binary &&
+ git commit -a -m resolved &&
+ res=$(git rev-parse --short HEAD:binary)
+'
+
+cat >expect <<EOF
+resolved
+
+diff --git a/binary b/binary
+index $three..$res 100644
+Binary files a/binary and b/binary differ
+resolved
+
+diff --git a/binary b/binary
+index $two..$res 100644
+Binary files a/binary and b/binary differ
+EOF
+test_expect_success 'diff -m indicates binary-ness' '
+ git show --format=%s -m >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<EOF
+resolved
+
+diff --combined binary
+index $three,$two..$res
+Binary files differ
+EOF
+test_expect_success 'diff -c indicates binary-ness' '
+ git show --format=%s -c >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<EOF
+resolved
+
+diff --cc binary
+index $three,$two..$res
+Binary files differ
+EOF
+test_expect_success 'diff --cc indicates binary-ness' '
+ git show --format=%s --cc >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'setup non-binary with binary attribute' '
+ git checkout main &&
+ test_commit one text &&
+ test_commit two text &&
+ two=$(git rev-parse --short HEAD:text) &&
+ git checkout -b branch-text HEAD^ &&
+ test_commit three text &&
+ three=$(git rev-parse --short HEAD:text) &&
+ test_must_fail git merge main &&
+ test_commit resolved text &&
+ res=$(git rev-parse --short HEAD:text) &&
+ echo text -diff >.gitattributes
+'
+
+cat >expect <<EOF
+resolved
+
+diff --git a/text b/text
+index $three..$res 100644
+Binary files a/text and b/text differ
+resolved
+
+diff --git a/text b/text
+index $two..$res 100644
+Binary files a/text and b/text differ
+EOF
+test_expect_success 'diff -m respects binary attribute' '
+ git show --format=%s -m >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<EOF
+resolved
+
+diff --combined text
+index $three,$two..$res
+Binary files differ
+EOF
+test_expect_success 'diff -c respects binary attribute' '
+ git show --format=%s -c >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<EOF
+resolved
+
+diff --cc text
+index $three,$two..$res
+Binary files differ
+EOF
+test_expect_success 'diff --cc respects binary attribute' '
+ git show --format=%s --cc >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'setup textconv attribute' '
+ echo "text diff=upcase" >.gitattributes &&
+ git config diff.upcase.textconv "tr a-z A-Z <"
+'
+
+cat >expect <<EOF
+resolved
+
+diff --git a/text b/text
+index $three..$res 100644
+--- a/text
++++ b/text
+@@ -1 +1 @@
+-THREE
++RESOLVED
+resolved
+
+diff --git a/text b/text
+index $two..$res 100644
+--- a/text
++++ b/text
+@@ -1 +1 @@
+-TWO
++RESOLVED
+EOF
+test_expect_success 'diff -m respects textconv attribute' '
+ git show --format=%s -m >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<EOF
+resolved
+
+diff --combined text
+index $three,$two..$res
+--- a/text
++++ b/text
+@@@ -1,1 -1,1 +1,1 @@@
+- THREE
+ -TWO
+++RESOLVED
+EOF
+test_expect_success 'diff -c respects textconv attribute' '
+ git show --format=%s -c >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<EOF
+resolved
+
+diff --cc text
+index $three,$two..$res
+--- a/text
++++ b/text
+@@@ -1,1 -1,1 +1,1 @@@
+- THREE
+ -TWO
+++RESOLVED
+EOF
+test_expect_success 'diff --cc respects textconv attribute' '
+ git show --format=%s --cc >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<EOF
+diff --combined text
+index $three,$two..$res
+--- a/text
++++ b/text
+@@@ -1,1 -1,1 +1,1 @@@
+- three
+ -two
+++resolved
+EOF
+test_expect_success 'diff-tree plumbing does not respect textconv' '
+ git diff-tree HEAD -c -p >full &&
+ tail -n +2 full >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<EOF
+diff --cc text
+index $three,$two..0000000
+--- a/text
++++ b/text
+@@@ -1,1 -1,1 +1,5 @@@
+++<<<<<<< HEAD
+ +THREE
+++=======
++ TWO
+++>>>>>>> MAIN
+EOF
+test_expect_success 'diff --cc respects textconv on worktree file' '
+ git reset --hard HEAD^ &&
+ test_must_fail git merge main &&
+ git diff >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t4049-diff-stat-count.sh b/t/t4049-diff-stat-count.sh
new file mode 100755
index 0000000..0a4fc73
--- /dev/null
+++ b/t/t4049-diff-stat-count.sh
@@ -0,0 +1,70 @@
+#!/bin/sh
+# Copyright (c) 2011, Google Inc.
+
+test_description='diff --stat-count'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ >a &&
+ >b &&
+ >c &&
+ >d &&
+ git add a b c d &&
+ git commit -m initial
+'
+
+test_expect_success 'mode-only change show as a 0-line change' '
+ git reset --hard &&
+ test_chmod +x b d &&
+ echo a >a &&
+ echo c >c &&
+ cat >expect <<-\EOF &&
+ a | 1 +
+ b | 0
+ ...
+ 4 files changed, 2 insertions(+)
+ EOF
+ git diff --stat --stat-count=2 HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'binary changes do not count in lines' '
+ git reset --hard &&
+ echo a >a &&
+ echo c >c &&
+ cat "$TEST_DIRECTORY"/test-binary-1.png >d &&
+ cat >expect <<-\EOF &&
+ a | 1 +
+ c | 1 +
+ ...
+ 3 files changed, 2 insertions(+)
+ EOF
+ git diff --stat --stat-count=2 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'exclude unmerged entries from total file count' '
+ git reset --hard &&
+ echo a >a &&
+ echo b >b &&
+ git ls-files -s a >x &&
+ git rm -f d &&
+ for stage in 1 2 3
+ do
+ sed -e "s/ 0 a/ $stage d/" x || return 1
+ done |
+ git update-index --index-info &&
+ echo d >d &&
+ cat >expect <<-\EOF &&
+ a | 1 +
+ b | 1 +
+ ...
+ 3 files changed, 3 insertions(+)
+ EOF
+ git diff --stat --stat-count=2 >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t4050-diff-histogram.sh b/t/t4050-diff-histogram.sh
new file mode 100755
index 0000000..c61b30f
--- /dev/null
+++ b/t/t4050-diff-histogram.sh
@@ -0,0 +1,13 @@
+#!/bin/sh
+
+test_description='histogram diff algorithm'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-diff-alternative.sh
+
+test_diff_frobnitz "histogram"
+
+test_diff_unique "histogram"
+
+test_done
diff --git a/t/t4051-diff-function-context.sh b/t/t4051-diff-function-context.sh
new file mode 100755
index 0000000..725278a
--- /dev/null
+++ b/t/t4051-diff-function-context.sh
@@ -0,0 +1,213 @@
+#!/bin/sh
+
+test_description='diff function context'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+dir="$TEST_DIRECTORY/t4051"
+
+commit_and_tag () {
+ tag=$1 &&
+ shift &&
+ git add "$@" &&
+ test_tick &&
+ git commit -m "$tag" &&
+ git tag "$tag"
+}
+
+first_context_line () {
+ awk '
+ found {print; exit}
+ /^@@/ {found = 1}
+ '
+}
+
+last_context_line () {
+ sed -ne \$p
+}
+
+check_diff () {
+ name=$1
+ desc=$2
+ options="-W $3"
+
+ test_expect_success "$desc" '
+ git diff $options "$name^" "$name" >"$name.diff"
+ '
+
+ test_expect_success ' diff applies' '
+ test_when_finished "git reset --hard" &&
+ git checkout --detach "$name^" &&
+ git apply --index "$name.diff" &&
+ git diff --exit-code "$name"
+ '
+}
+
+test_expect_success 'setup' '
+ cat "$dir/includes.c" "$dir/dummy.c" "$dir/dummy.c" "$dir/hello.c" \
+ "$dir/dummy.c" "$dir/dummy.c" >file.c &&
+ commit_and_tag initial file.c &&
+
+ grep -v "delete me from hello" <file.c >file.c.new &&
+ mv file.c.new file.c &&
+ commit_and_tag changed_hello file.c &&
+
+ grep -v "delete me from includes" <file.c >file.c.new &&
+ mv file.c.new file.c &&
+ commit_and_tag changed_includes file.c &&
+
+ cat "$dir/appended1.c" >>file.c &&
+ commit_and_tag appended file.c &&
+
+ cat "$dir/appended2.c" >>file.c &&
+ commit_and_tag extended file.c &&
+
+ grep -v "Begin of second part" <file.c >file.c.new &&
+ mv file.c.new file.c &&
+ commit_and_tag long_common_tail file.c &&
+
+ git checkout initial &&
+ cat "$dir/hello.c" "$dir/dummy.c" >file.c &&
+ commit_and_tag hello_dummy file.c &&
+
+ # overlap function context of 1st change and -u context of 2nd change
+ grep -v "delete me from hello" <"$dir/hello.c" >file.c &&
+ sed "2a\\
+ extra line" <"$dir/dummy.c" >>file.c &&
+ commit_and_tag changed_hello_dummy file.c &&
+
+ git checkout initial &&
+ grep -v "delete me from hello" <file.c >file.c.new &&
+ mv file.c.new file.c &&
+ cat "$dir/appended1.c" >>file.c &&
+ commit_and_tag changed_hello_appended file.c
+'
+
+check_diff changed_hello 'changed function'
+
+test_expect_success ' context includes comment' '
+ grep "^ .*Hello comment" changed_hello.diff
+'
+
+test_expect_success ' context includes begin' '
+ grep "^ .*Begin of hello" changed_hello.diff
+'
+
+test_expect_success ' context includes end' '
+ grep "^ .*End of hello" changed_hello.diff
+'
+
+test_expect_success ' context does not include other functions' '
+ test $(grep -c "^[ +-].*Begin" changed_hello.diff) -le 1
+'
+
+test_expect_success ' context does not include preceding empty lines' '
+ test "$(first_context_line <changed_hello.diff)" != " "
+'
+
+test_expect_success ' context does not include trailing empty lines' '
+ test "$(last_context_line <changed_hello.diff)" != " "
+'
+
+check_diff changed_includes 'changed includes'
+
+test_expect_success ' context includes begin' '
+ grep "^ .*Begin.h" changed_includes.diff
+'
+
+test_expect_success ' context includes end' '
+ grep "^ .*End.h" changed_includes.diff
+'
+
+test_expect_success ' context does not include other functions' '
+ test $(grep -c "^[ +-].*Begin" changed_includes.diff) -le 1
+'
+
+test_expect_success ' context does not include trailing empty lines' '
+ test "$(last_context_line <changed_includes.diff)" != " "
+'
+
+check_diff appended 'appended function'
+
+test_expect_success ' context includes begin' '
+ grep "^[+].*Begin of first part" appended.diff
+'
+
+test_expect_success ' context includes end' '
+ grep "^[+].*End of first part" appended.diff
+'
+
+test_expect_success ' context does not include other functions' '
+ test $(grep -c "^[ +-].*Begin" appended.diff) -le 1
+'
+
+check_diff extended 'appended function part'
+
+test_expect_success ' context includes begin' '
+ grep "^ .*Begin of first part" extended.diff
+'
+
+test_expect_success ' context includes end' '
+ grep "^[+].*End of second part" extended.diff
+'
+
+test_expect_success ' context does not include other functions' '
+ test $(grep -c "^[ +-].*Begin" extended.diff) -le 2
+'
+
+test_expect_success ' context does not include preceding empty lines' '
+ test "$(first_context_line <extended.diff)" != " "
+'
+
+check_diff long_common_tail 'change with long common tail and no context' -U0
+
+test_expect_success ' context includes begin' '
+ grep "^ .*Begin of first part" long_common_tail.diff
+'
+
+test_expect_success ' context includes end' '
+ grep "^ .*End of second part" long_common_tail.diff
+'
+
+test_expect_success ' context does not include other functions' '
+ test $(grep -c "^[ +-].*Begin" long_common_tail.diff) -le 2
+'
+
+test_expect_success ' context does not include preceding empty lines' '
+ test "$(first_context_line <long_common_tail.diff)" != " "
+'
+
+check_diff changed_hello_appended 'changed function plus appended function'
+
+test_expect_success ' context includes begin' '
+ grep "^ .*Begin of hello" changed_hello_appended.diff &&
+ grep "^[+].*Begin of first part" changed_hello_appended.diff
+'
+
+test_expect_success ' context includes end' '
+ grep "^ .*End of hello" changed_hello_appended.diff &&
+ grep "^[+].*End of first part" changed_hello_appended.diff
+'
+
+test_expect_success ' context does not include other functions' '
+ test $(grep -c "^[ +-].*Begin" changed_hello_appended.diff) -le 2
+'
+
+check_diff changed_hello_dummy 'changed two consecutive functions'
+
+test_expect_success ' context includes begin' '
+ grep "^ .*Begin of hello" changed_hello_dummy.diff &&
+ grep "^ .*Begin of dummy" changed_hello_dummy.diff
+'
+
+test_expect_success ' context includes end' '
+ grep "^ .*End of hello" changed_hello_dummy.diff &&
+ grep "^ .*End of dummy" changed_hello_dummy.diff
+'
+
+test_expect_success ' overlapping hunks are merged' '
+ test $(grep -c "^@@" changed_hello_dummy.diff) -eq 1
+'
+
+test_done
diff --git a/t/t4051/appended1.c b/t/t4051/appended1.c
new file mode 100644
index 0000000..a9f56f1
--- /dev/null
+++ b/t/t4051/appended1.c
@@ -0,0 +1,15 @@
+
+int appended(void) // Begin of first part
+{
+ int i;
+ char *s = "a string";
+
+ printf("%s\n", s);
+
+ for (i = 99;
+ i >= 0;
+ i--) {
+ printf("%d bottles of beer on the wall\n", i);
+ }
+
+ printf("End of first part\n");
diff --git a/t/t4051/appended2.c b/t/t4051/appended2.c
new file mode 100644
index 0000000..e651f71
--- /dev/null
+++ b/t/t4051/appended2.c
@@ -0,0 +1,35 @@
+ printf("Begin of second part\n");
+
+ /*
+ * Lorem ipsum dolor sit amet, consectetuer sadipscing elitr,
+ * sed diam nonumy eirmod tempor invidunt ut labore et dolore
+ * magna aliquyam erat, sed diam voluptua. At vero eos et
+ * accusam et justo duo dolores et ea rebum. Stet clita kasd
+ * gubergren, no sea takimata sanctus est Lorem ipsum dolor
+ * sit amet.
+ *
+ * Lorem ipsum dolor sit amet, consectetuer sadipscing elitr,
+ * sed diam nonumy eirmod tempor invidunt ut labore et dolore
+ * magna aliquyam erat, sed diam voluptua. At vero eos et
+ * accusam et justo duo dolores et ea rebum. Stet clita kasd
+ * gubergren, no sea takimata sanctus est Lorem ipsum dolor
+ * sit amet.
+ *
+ * Lorem ipsum dolor sit amet, consectetuer sadipscing elitr,
+ * sed diam nonumy eirmod tempor invidunt ut labore et dolore
+ * magna aliquyam erat, sed diam voluptua. At vero eos et
+ * accusam et justo duo dolores et ea rebum. Stet clita kasd
+ * gubergren, no sea takimata sanctus est Lorem ipsum dolor
+ * sit amet.
+ *
+ * Lorem ipsum dolor sit amet, consectetuer sadipscing elitr,
+ * sed diam nonumy eirmod tempor invidunt ut labore et dolore
+ * magna aliquyam erat, sed diam voluptua. At vero eos et
+ * accusam et justo duo dolores et ea rebum. Stet clita kasd
+ * gubergren, no sea takimata sanctus est Lorem ipsum dolor
+ * sit amet.
+ *
+ */
+
+ return 0;
+} // End of second part
diff --git a/t/t4051/dummy.c b/t/t4051/dummy.c
new file mode 100644
index 0000000..a43016e
--- /dev/null
+++ b/t/t4051/dummy.c
@@ -0,0 +1,7 @@
+
+static int dummy(void) // Begin of dummy
+{
+ int rc = 0;
+
+ return rc;
+} // End of dummy
diff --git a/t/t4051/hello.c b/t/t4051/hello.c
new file mode 100644
index 0000000..73e767e
--- /dev/null
+++ b/t/t4051/hello.c
@@ -0,0 +1,24 @@
+
+/*
+ * Hello comment.
+ */
+static void hello(void) // Begin of hello
+{
+ /*
+ * Classic.
+ */
+ putchar('H');
+ putchar('e');
+ putchar('l');
+ putchar('l');
+ putchar('o');
+ putchar(' ');
+ /* delete me from hello */
+ putchar('w');
+ putchar('o');
+ putchar('r');
+ putchar('l');
+ putchar('d');
+ putchar('.');
+ putchar('\n');
+} // End of hello
diff --git a/t/t4051/includes.c b/t/t4051/includes.c
new file mode 100644
index 0000000..efc68f8
--- /dev/null
+++ b/t/t4051/includes.c
@@ -0,0 +1,20 @@
+#include <Begin.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <stddef.h>
+#include <stdlib.h>
+#include <stdarg.h>
+/* delete me from includes */
+#include <string.h>
+#include <sys/types.h>
+#include <dirent.h>
+#include <sys/time.h>
+#include <time.h>
+#include <signal.h>
+#include <assert.h>
+#include <regex.h>
+#include <utime.h>
+#include <syslog.h>
+#include <End.h>
diff --git a/t/t4052-stat-output.sh b/t/t4052-stat-output.sh
new file mode 100755
index 0000000..7badd72
--- /dev/null
+++ b/t/t4052-stat-output.sh
@@ -0,0 +1,417 @@
+#!/bin/sh
+#
+# Copyright (c) 2012 Zbigniew Jędrzejewski-Szmek
+#
+
+test_description='test --stat output of various commands'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-terminal.sh
+
+# 120-character name
+name=aaaaaaaaaa
+name=$name$name$name$name$name$name$name$name$name$name$name$name
+test_expect_success 'preparation' '
+ >"$name" &&
+ git add "$name" &&
+ git commit -m message &&
+ echo a >"$name" &&
+ git commit -m message "$name"
+'
+
+cat >expect72 <<-'EOF'
+ ...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
+EOF
+test_expect_success "format-patch: small change with long name gives more space to the name" '
+ git format-patch -1 --stdout >output &&
+ grep " | " output >actual &&
+ test_cmp expect72 actual
+'
+
+while read cmd args
+do
+ cat >expect80 <<-'EOF'
+ ...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
+ EOF
+ test_expect_success "$cmd: small change with long name gives more space to the name" '
+ git $cmd $args >output &&
+ grep " | " output >actual &&
+ test_cmp expect80 actual
+ '
+done <<\EOF
+diff HEAD^ HEAD --stat
+show --stat
+log -1 --stat
+EOF
+
+cat >expect.60 <<-'EOF'
+ ...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
+EOF
+cat >expect.6030 <<-'EOF'
+ ...aaaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
+EOF
+while read verb expect cmd args
+do
+ # No width limit applied when statNameWidth is ignored
+ case "$expect" in expect72|expect.6030)
+ test_expect_success "$cmd $verb diff.statNameWidth with long name" '
+ git -c diff.statNameWidth=30 $cmd $args >output &&
+ grep " | " output >actual &&
+ test_cmp $expect actual
+ ';;
+ esac
+ # Maximum width limit still applied when statNameWidth is ignored
+ case "$expect" in expect.60|expect.6030)
+ test_expect_success "$cmd --stat=width $verb diff.statNameWidth with long name" '
+ git -c diff.statNameWidth=30 $cmd $args --stat=60 >output &&
+ grep " | " output >actual &&
+ test_cmp $expect actual
+ ';;
+ esac
+done <<\EOF
+ignores expect72 format-patch -1 --stdout
+ignores expect.60 format-patch -1 --stdout
+respects expect.6030 diff HEAD^ HEAD --stat
+respects expect.6030 show --stat
+respects expect.6030 log -1 --stat
+EOF
+
+cat >expect.40 <<-'EOF'
+ ...aaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
+EOF
+cat >expect2.40 <<-'EOF'
+ ...aaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
+ ...aaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
+EOF
+cat >expect2.6030 <<-'EOF'
+ ...aaaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
+ ...aaaaaaaaaaaaaaaaaaaaaaaaaaa | 1 +
+EOF
+while read expect cmd args
+do
+ test_expect_success "$cmd --stat=width: a long name is given more room when the bar is short" '
+ git $cmd $args --stat=40 >output &&
+ grep " | " output >actual &&
+ test_cmp $expect.40 actual
+ '
+
+ test_expect_success "$cmd --stat-width=width with long name" '
+ git $cmd $args --stat-width=40 >output &&
+ grep " | " output >actual &&
+ test_cmp $expect.40 actual
+ '
+
+ test_expect_success "$cmd --stat=width,name-width with long name" '
+ git $cmd $args --stat=60,30 >output &&
+ grep " | " output >actual &&
+ test_cmp $expect.6030 actual
+ '
+
+ test_expect_success "$cmd --stat-name-width=width with long name" '
+ git $cmd $args --stat-name-width=30 >output &&
+ grep " | " output >actual &&
+ test_cmp $expect.6030 actual
+ '
+done <<\EOF
+expect2 format-patch --cover-letter -1 --stdout
+expect diff HEAD^ HEAD --stat
+expect show --stat
+expect log -1 --stat
+EOF
+
+test_expect_success 'preparation for big-change tests' '
+ >abcd &&
+ git add abcd &&
+ git commit -m message &&
+ i=0 &&
+ while test $i -lt 1000
+ do
+ echo $i && i=$(($i + 1)) || return 1
+ done >abcd &&
+ git commit -m message abcd
+'
+
+cat >expect72 <<'EOF'
+ abcd | 1000 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ abcd | 1000 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+EOF
+test_expect_success "format-patch --cover-letter ignores COLUMNS with big change" '
+ COLUMNS=200 git format-patch -1 --stdout --cover-letter >output &&
+ grep " | " output >actual &&
+ test_cmp expect72 actual
+'
+
+cat >expect72 <<'EOF'
+ abcd | 1000 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+EOF
+cat >expect72-graph <<'EOF'
+| abcd | 1000 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+EOF
+cat >expect200 <<'EOF'
+ abcd | 1000 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+EOF
+cat >expect200-graph <<'EOF'
+| abcd | 1000 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+EOF
+while read verb expect cmd args
+do
+ test_expect_success "$cmd $verb COLUMNS with big change" '
+ COLUMNS=200 git $cmd $args >output &&
+ grep " | " output >actual &&
+ test_cmp "$expect" actual
+ '
+
+ case "$cmd" in diff|show) continue;; esac
+
+ test_expect_success "$cmd --graph $verb COLUMNS with big change" '
+ COLUMNS=200 git $cmd $args --graph >output &&
+ grep " | " output >actual &&
+ test_cmp "$expect-graph" actual
+ '
+done <<\EOF
+ignores expect72 format-patch -1 --stdout
+respects expect200 diff HEAD^ HEAD --stat
+respects expect200 show --stat
+respects expect200 log -1 --stat
+EOF
+
+cat >expect40 <<'EOF'
+ abcd | 1000 ++++++++++++++++++++++++++
+EOF
+cat >expect40-graph <<'EOF'
+| abcd | 1000 ++++++++++++++++++++++++
+EOF
+while read verb expect cmd args
+do
+ test_expect_success "$cmd $verb not enough COLUMNS with big change" '
+ COLUMNS=40 git $cmd $args >output &&
+ grep " | " output >actual &&
+ test_cmp "$expect" actual
+ '
+
+ case "$cmd" in diff|show) continue;; esac
+
+ test_expect_success "$cmd --graph $verb not enough COLUMNS with big change" '
+ COLUMNS=40 git $cmd $args --graph >output &&
+ grep " | " output >actual &&
+ test_cmp "$expect-graph" actual
+ '
+done <<\EOF
+ignores expect72 format-patch -1 --stdout
+respects expect40 diff HEAD^ HEAD --stat
+respects expect40 show --stat
+respects expect40 log -1 --stat
+EOF
+
+cat >expect40 <<'EOF'
+ abcd | 1000 ++++++++++++++++++++++++++
+EOF
+cat >expect40-graph <<'EOF'
+| abcd | 1000 ++++++++++++++++++++++++++
+EOF
+while read verb expect cmd args
+do
+ test_expect_success "$cmd $verb diff.statGraphWidth" '
+ git -c diff.statGraphWidth=26 $cmd $args >output &&
+ grep " | " output >actual &&
+ test_cmp "$expect" actual
+ '
+
+ case "$cmd" in diff|show) continue;; esac
+
+ test_expect_success "$cmd --graph $verb diff.statGraphWidth" '
+ git -c diff.statGraphWidth=26 $cmd $args --graph >output &&
+ grep " | " output >actual &&
+ test_cmp "$expect-graph" actual
+ '
+done <<\EOF
+ignores expect72 format-patch -1 --stdout
+respects expect40 diff HEAD^ HEAD --stat
+respects expect40 show --stat
+respects expect40 log -1 --stat
+EOF
+
+cat >expect <<'EOF'
+ abcd | 1000 ++++++++++++++++++++++++++
+EOF
+cat >expect-graph <<'EOF'
+| abcd | 1000 ++++++++++++++++++++++++++
+EOF
+while read cmd args
+do
+ test_expect_success "$cmd --stat=width with big change" '
+ git $cmd $args --stat=40 >output &&
+ grep " | " output >actual &&
+ test_cmp expect actual
+ '
+
+ test_expect_success "$cmd --stat-width=width with big change" '
+ git $cmd $args --stat-width=40 >output &&
+ grep " | " output >actual &&
+ test_cmp expect actual
+ '
+
+ test_expect_success "$cmd --stat-graph-width=width with big change" '
+ git $cmd $args --stat-graph-width=26 >output &&
+ grep " | " output >actual &&
+ test_cmp expect actual
+ '
+
+ case "$cmd" in diff|show) continue;; esac
+
+ test_expect_success "$cmd --stat-width=width --graph with big change" '
+ git $cmd $args --stat-width=40 --graph >output &&
+ grep " | " output >actual &&
+ test_cmp expect-graph actual
+ '
+
+ test_expect_success "$cmd --stat-graph-width=width --graph with big change" '
+ git $cmd $args --stat-graph-width=26 --graph >output &&
+ grep " | " output >actual &&
+ test_cmp expect-graph actual
+ '
+done <<\EOF
+format-patch -1 --stdout
+diff HEAD^ HEAD --stat
+show --stat
+log -1 --stat
+EOF
+
+test_expect_success 'preparation for long-name tests' '
+ cp abcd aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&
+ git add aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&
+ git commit -m message
+'
+
+cat >expect <<'EOF'
+ ...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1000 ++++++++++++
+EOF
+cat >expect-graph <<'EOF'
+| ...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1000 ++++++++++++
+EOF
+while read cmd args
+do
+ test_expect_success "$cmd --stat=width with big change is more balanced" '
+ git $cmd $args --stat-width=60 >output &&
+ grep " | " output >actual &&
+ test_cmp expect actual
+ '
+
+ case "$cmd" in diff|show) continue;; esac
+
+ test_expect_success "$cmd --stat=width --graph with big change is balanced" '
+ git $cmd $args --stat-width=60 --graph >output &&
+ grep " | " output >actual &&
+ test_cmp expect-graph actual
+ '
+done <<\EOF
+format-patch -1 --stdout
+diff HEAD^ HEAD --stat
+show --stat
+log -1 --stat
+EOF
+
+cat >expect72 <<'EOF'
+ ...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1000 +++++++++++++++++
+EOF
+cat >expect72-graph <<'EOF'
+| ...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1000 +++++++++++++++++
+EOF
+cat >expect200 <<'EOF'
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1000 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+EOF
+cat >expect200-graph <<'EOF'
+| aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1000 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+EOF
+while read verb expect cmd args
+do
+ test_expect_success "$cmd $verb COLUMNS with long name" '
+ COLUMNS=200 git $cmd $args >output &&
+ grep " | " output >actual &&
+ test_cmp "$expect" actual
+ '
+
+ case "$cmd" in diff|show) continue;; esac
+
+ test_expect_success "$cmd --graph $verb COLUMNS with long name" '
+ COLUMNS=200 git $cmd $args --graph >output &&
+ grep " | " output >actual &&
+ test_cmp "$expect-graph" actual
+ '
+done <<\EOF
+ignores expect72 format-patch -1 --stdout
+respects expect200 diff HEAD^ HEAD --stat
+respects expect200 show --stat
+respects expect200 log -1 --stat
+EOF
+
+cat >expect1 <<'EOF'
+ ...aaaaaaa | 1000 ++++++
+EOF
+cat >expect1-graph <<'EOF'
+| ...aaaaaaa | 1000 ++++++
+EOF
+while read verb expect cmd args
+do
+ test_expect_success COLUMNS_CAN_BE_1 \
+ "$cmd $verb prefix greater than COLUMNS with big change" '
+ COLUMNS=1 git $cmd $args >output &&
+ grep " | " output >actual &&
+ test_cmp "$expect" actual
+ '
+
+ case "$cmd" in diff|show) continue;; esac
+
+ test_expect_success COLUMNS_CAN_BE_1 \
+ "$cmd --graph $verb prefix greater than COLUMNS with big change" '
+ COLUMNS=1 git $cmd $args --graph >output &&
+ grep " | " output >actual &&
+ test_cmp "$expect-graph" actual
+ '
+done <<\EOF
+ignores expect72 format-patch -1 --stdout
+respects expect1 diff HEAD^ HEAD --stat
+respects expect1 show --stat
+respects expect1 log -1 --stat
+EOF
+
+cat >expect <<'EOF'
+ abcd | 1000 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+EOF
+test_expect_success 'merge --stat respects diff.statGraphWidth with big change' '
+ git checkout -b branch1 HEAD^^ &&
+ git -c diff.statGraphWidth=26 merge --stat --no-ff main^ >output &&
+ grep " | " output >actual &&
+ test_cmp expect40 actual
+'
+test_expect_success 'merge --stat respects COLUMNS with big change' '
+ git checkout -b branch2 HEAD^^ &&
+ COLUMNS=100 git merge --stat --no-ff main^ >output &&
+ grep " | " output >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<'EOF'
+ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa | 1000 +++++++++++++++++++++++++++++++++++++++
+EOF
+cat >expect.30 <<'EOF'
+ ...aaaaaaaaaaaaaaaaaaaaaaaaaaa | 1000 ++++++++++++++++++++++++++++++++++++++++
+EOF
+test_expect_success 'merge --stat respects diff.statNameWidth with long name' '
+ git switch branch1 &&
+ git -c diff.statNameWidth=30 merge --stat --no-ff main >output &&
+ grep " | " output >actual &&
+ test_cmp expect.30 actual
+'
+test_expect_success 'merge --stat respects COLUMNS with long name' '
+ git switch branch2 &&
+ COLUMNS=100 git merge --stat --no-ff main >output &&
+ grep " | " output >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t4053-diff-no-index.sh b/t/t4053-diff-no-index.sh
new file mode 100755
index 0000000..5ce345d
--- /dev/null
+++ b/t/t4053-diff-no-index.sh
@@ -0,0 +1,287 @@
+#!/bin/sh
+
+test_description='diff --no-index'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ mkdir a &&
+ mkdir b &&
+ echo 1 >a/1 &&
+ echo 2 >a/2 &&
+ git init repo &&
+ echo 1 >repo/a &&
+ mkdir -p non/git &&
+ echo 1 >non/git/a &&
+ echo 1 >non/git/b
+'
+
+test_expect_success 'git diff --no-index --exit-code' '
+ git diff --no-index --exit-code a/1 non/git/a &&
+ test_expect_code 1 git diff --no-index --exit-code a/1 a/2
+'
+
+test_expect_success 'git diff --no-index directories' '
+ test_expect_code 1 git diff --no-index a b >cnt &&
+ test_line_count = 14 cnt
+'
+
+test_expect_success 'git diff --no-index relative path outside repo' '
+ (
+ cd repo &&
+ test_expect_code 0 git diff --no-index a ../non/git/a &&
+ test_expect_code 0 git diff --no-index ../non/git/a ../non/git/b
+ )
+'
+
+test_expect_success 'git diff --no-index with broken index' '
+ (
+ cd repo &&
+ echo broken >.git/index &&
+ git diff --no-index a ../non/git/a
+ )
+'
+
+test_expect_success 'git diff outside repo with broken index' '
+ (
+ cd repo &&
+ git diff ../non/git/a ../non/git/b
+ )
+'
+
+test_expect_success 'git diff --no-index executed outside repo gives correct error message' '
+ (
+ GIT_CEILING_DIRECTORIES=$TRASH_DIRECTORY/non &&
+ export GIT_CEILING_DIRECTORIES &&
+ cd non/git &&
+ test_must_fail git diff --no-index a 2>actual.err &&
+ test_grep "usage: git diff --no-index" actual.err
+ )
+'
+
+test_expect_success 'diff D F and diff F D' '
+ (
+ cd repo &&
+ echo in-repo >a &&
+ echo non-repo >../non/git/a &&
+ mkdir sub &&
+ echo sub-repo >sub/a &&
+
+ test_must_fail git diff --no-index sub/a ../non/git/a >expect &&
+ test_must_fail git diff --no-index sub/a ../non/git/ >actual &&
+ test_cmp expect actual &&
+
+ test_must_fail git diff --no-index a ../non/git/a >expect &&
+ test_must_fail git diff --no-index a ../non/git/ >actual &&
+ test_cmp expect actual &&
+
+ test_must_fail git diff --no-index ../non/git/a a >expect &&
+ test_must_fail git diff --no-index ../non/git a >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'turning a file into a directory' '
+ (
+ cd non/git &&
+ mkdir d e e/sub &&
+ echo 1 >d/sub &&
+ echo 2 >e/sub/file &&
+ printf "D\td/sub\nA\te/sub/file\n" >expect &&
+ test_must_fail git diff --no-index --name-status d e >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'diff from repo subdir shows real paths (explicit)' '
+ echo "diff --git a/../../non/git/a b/../../non/git/b" >expect &&
+ test_expect_code 1 \
+ git -C repo/sub \
+ diff --no-index ../../non/git/a ../../non/git/b >actual &&
+ head -n 1 <actual >actual.head &&
+ test_cmp expect actual.head
+'
+
+test_expect_success 'diff from repo subdir shows real paths (implicit)' '
+ echo "diff --git a/../../non/git/a b/../../non/git/b" >expect &&
+ test_expect_code 1 \
+ git -C repo/sub \
+ diff ../../non/git/a ../../non/git/b >actual &&
+ head -n 1 <actual >actual.head &&
+ test_cmp expect actual.head
+'
+
+test_expect_success 'diff --no-index from repo subdir respects config (explicit)' '
+ echo "diff --git ../../non/git/a ../../non/git/b" >expect &&
+ test_config -C repo diff.noprefix true &&
+ test_expect_code 1 \
+ git -C repo/sub \
+ diff --no-index ../../non/git/a ../../non/git/b >actual &&
+ head -n 1 <actual >actual.head &&
+ test_cmp expect actual.head
+'
+
+test_expect_success 'diff --no-index from repo subdir respects config (implicit)' '
+ echo "diff --git ../../non/git/a ../../non/git/b" >expect &&
+ test_config -C repo diff.noprefix true &&
+ test_expect_code 1 \
+ git -C repo/sub \
+ diff ../../non/git/a ../../non/git/b >actual &&
+ head -n 1 <actual >actual.head &&
+ test_cmp expect actual.head
+'
+
+test_expect_success 'diff --no-index from repo subdir with absolute paths' '
+ cat <<-EOF >expect &&
+ 1 1 $(pwd)/non/git/{a => b}
+ EOF
+ test_expect_code 1 \
+ git -C repo/sub diff --numstat \
+ "$(pwd)/non/git/a" "$(pwd)/non/git/b" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'diff --no-index allows external diff' '
+ test_expect_code 1 \
+ env GIT_EXTERNAL_DIFF="echo external ;:" \
+ git diff --no-index non/git/a non/git/b >actual &&
+ echo external >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'diff --no-index normalizes mode: no changes' '
+ echo foo >x &&
+ cp x y &&
+ git diff --no-index x y >out &&
+ test_must_be_empty out
+'
+
+test_expect_success POSIXPERM 'diff --no-index normalizes mode: chmod +x' '
+ chmod +x y &&
+ cat >expected <<-\EOF &&
+ diff --git a/x b/y
+ old mode 100644
+ new mode 100755
+ EOF
+ test_expect_code 1 git diff --no-index x y >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success POSIXPERM 'diff --no-index normalizes: mode not like git mode' '
+ chmod 666 x &&
+ chmod 777 y &&
+ cat >expected <<-\EOF &&
+ diff --git a/x b/y
+ old mode 100644
+ new mode 100755
+ EOF
+ test_expect_code 1 git diff --no-index x y >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success POSIXPERM,SYMLINKS 'diff --no-index normalizes: mode not like git mode (symlink)' '
+ ln -s y z &&
+ X_OID=$(git hash-object --stdin <x) &&
+ Z_OID=$(printf y | git hash-object --stdin) &&
+ cat >expected <<-EOF &&
+ diff --git a/x b/x
+ deleted file mode 100644
+ index $X_OID..$ZERO_OID
+ --- a/x
+ +++ /dev/null
+ @@ -1 +0,0 @@
+ -foo
+ diff --git a/z b/z
+ new file mode 120000
+ index $ZERO_OID..$Z_OID
+ --- /dev/null
+ +++ b/z
+ @@ -0,0 +1 @@
+ +y
+ \ No newline at end of file
+ EOF
+ test_expect_code 1 git -c core.abbrev=no diff --no-index x z >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success "diff --no-index treats '-' as stdin" '
+ cat >expect <<-EOF &&
+ diff --git a/- b/a/1
+ index $ZERO_OID..$(git hash-object --stdin <a/1) 100644
+ --- a/-
+ +++ b/a/1
+ @@ -1 +1 @@
+ -x
+ +1
+ EOF
+
+ test_write_lines x | test_expect_code 1 \
+ git -c core.abbrev=no diff --no-index -- - a/1 >actual &&
+ test_cmp expect actual &&
+
+ test_write_lines 1 | git diff --no-index -- a/1 - >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success "diff --no-index -R treats '-' as stdin" '
+ cat >expect <<-EOF &&
+ diff --git b/a/1 a/-
+ index $(git hash-object --stdin <a/1)..$ZERO_OID 100644
+ --- b/a/1
+ +++ a/-
+ @@ -1 +1 @@
+ -1
+ +x
+ EOF
+
+ test_write_lines x | test_expect_code 1 \
+ git -c core.abbrev=no diff --no-index -R -- - a/1 >actual &&
+ test_cmp expect actual &&
+
+ test_write_lines 1 | git diff --no-index -R -- a/1 - >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'diff --no-index refuses to diff stdin and a directory' '
+ test_must_fail git diff --no-index -- - a </dev/null 2>err &&
+ grep "fatal: cannot compare stdin to a directory" err
+'
+
+test_expect_success PIPE 'diff --no-index refuses to diff a named pipe and a directory' '
+ test_when_finished "rm -f pipe" &&
+ mkfifo pipe &&
+ test_must_fail git diff --no-index -- pipe a 2>err &&
+ grep "fatal: cannot compare a named pipe to a directory" err
+'
+
+test_expect_success PIPE,SYMLINKS 'diff --no-index reads from pipes' '
+ test_when_finished "rm -f old new new-link" &&
+ mkfifo old &&
+ mkfifo new &&
+ ln -s new new-link &&
+ {
+ (test_write_lines a b c >old) &
+ } &&
+ test_when_finished "kill $! || :" &&
+ {
+ (test_write_lines a x c >new) &
+ } &&
+ test_when_finished "kill $! || :" &&
+
+ cat >expect <<-EOF &&
+ diff --git a/old b/new-link
+ --- a/old
+ +++ b/new-link
+ @@ -1,3 +1,3 @@
+ a
+ -b
+ +x
+ c
+ EOF
+
+ test_expect_code 1 git diff --no-index old new-link >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t4054-diff-bogus-tree.sh b/t/t4054-diff-bogus-tree.sh
new file mode 100755
index 0000000..05c88f8
--- /dev/null
+++ b/t/t4054-diff-bogus-tree.sh
@@ -0,0 +1,84 @@
+#!/bin/sh
+
+test_description='test diff with a bogus tree containing the null sha1'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'create bogus tree' '
+ name=$(echo $ZERO_OID | sed -e "s/00/Q/g") &&
+ bogus_tree=$(
+ printf "100644 fooQ$name" |
+ q_to_nul |
+ git hash-object --literally -w --stdin -t tree
+ )
+'
+
+test_expect_success 'create tree with matching file' '
+ echo bar >foo &&
+ git add foo &&
+ good_tree=$(git write-tree) &&
+ blob=$(git rev-parse :foo)
+'
+
+test_expect_success 'raw diff shows null sha1 (addition)' '
+ echo ":000000 100644 $ZERO_OID $ZERO_OID A foo" >expect &&
+ git diff-tree $EMPTY_TREE $bogus_tree >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'raw diff shows null sha1 (removal)' '
+ echo ":100644 000000 $ZERO_OID $ZERO_OID D foo" >expect &&
+ git diff-tree $bogus_tree $EMPTY_TREE >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'raw diff shows null sha1 (modification)' '
+ echo ":100644 100644 $blob $ZERO_OID M foo" >expect &&
+ git diff-tree $good_tree $bogus_tree >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'raw diff shows null sha1 (other direction)' '
+ echo ":100644 100644 $ZERO_OID $blob M foo" >expect &&
+ git diff-tree $bogus_tree $good_tree >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'raw diff shows null sha1 (reverse)' '
+ echo ":100644 100644 $ZERO_OID $blob M foo" >expect &&
+ git diff-tree -R $good_tree $bogus_tree >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'raw diff shows null sha1 (index)' '
+ echo ":100644 100644 $ZERO_OID $blob M foo" >expect &&
+ git diff-index $bogus_tree >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'patch fails due to bogus sha1 (addition)' '
+ test_must_fail git diff-tree -p $EMPTY_TREE $bogus_tree
+'
+
+test_expect_success 'patch fails due to bogus sha1 (removal)' '
+ test_must_fail git diff-tree -p $bogus_tree $EMPTY_TREE
+'
+
+test_expect_success 'patch fails due to bogus sha1 (modification)' '
+ test_must_fail git diff-tree -p $good_tree $bogus_tree
+'
+
+test_expect_success 'patch fails due to bogus sha1 (other direction)' '
+ test_must_fail git diff-tree -p $bogus_tree $good_tree
+'
+
+test_expect_success 'patch fails due to bogus sha1 (reverse)' '
+ test_must_fail git diff-tree -R -p $good_tree $bogus_tree
+'
+
+test_expect_success 'patch fails due to bogus sha1 (index)' '
+ test_must_fail git diff-index -p $bogus_tree
+'
+
+test_done
diff --git a/t/t4055-diff-context.sh b/t/t4055-diff-context.sh
new file mode 100755
index 0000000..3ea9ae9
--- /dev/null
+++ b/t/t4055-diff-context.sh
@@ -0,0 +1,93 @@
+#!/bin/sh
+#
+# Copyright (c) 2012 Mozilla Foundation
+#
+
+test_description='diff.context configuration'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ cat >template <<-\EOF &&
+ firstline
+ b
+ c
+ d
+ e
+ f
+ preline
+ TARGET
+ postline
+ i
+ j
+ k
+ l
+ m
+ n
+ EOF
+ sed "/TARGET/d" >x <template &&
+ git update-index --add x &&
+ git commit -m initial &&
+
+ sed "s/TARGET/ADDED/" >x <template &&
+ git update-index --add x &&
+ git commit -m next &&
+
+ sed "s/TARGET/MODIFIED/" >x <template
+'
+
+test_expect_success 'the default number of context lines is 3' '
+ git diff >output &&
+ ! grep "^ d" output &&
+ grep "^ e" output &&
+ grep "^ j" output &&
+ ! grep "^ k" output
+'
+
+test_expect_success 'diff.context honored by "log"' '
+ git log -1 -p >output &&
+ ! grep firstline output &&
+ git config diff.context 8 &&
+ git log -1 -p >output &&
+ grep "^ firstline" output
+'
+
+test_expect_success 'The -U option overrides diff.context' '
+ git config diff.context 8 &&
+ git log -U4 -1 >output &&
+ ! grep "^ firstline" output
+'
+
+test_expect_success 'diff.context honored by "diff"' '
+ git config diff.context 8 &&
+ git diff >output &&
+ grep "^ firstline" output
+'
+
+test_expect_success 'plumbing not affected' '
+ git config diff.context 8 &&
+ git diff-files -p >output &&
+ ! grep "^ firstline" output
+'
+
+test_expect_success 'non-integer config parsing' '
+ git config diff.context no &&
+ test_must_fail git diff 2>output &&
+ test_grep "bad numeric config value" output
+'
+
+test_expect_success 'negative integer config parsing' '
+ git config diff.context -1 &&
+ test_must_fail git diff 2>output &&
+ test_grep "bad config variable" output
+'
+
+test_expect_success '-U0 is valid, so is diff.context=0' '
+ git config diff.context 0 &&
+ git diff >output &&
+ grep "^-ADDED" output &&
+ grep "^+MODIFIED" output
+'
+
+test_done
diff --git a/t/t4056-diff-order.sh b/t/t4056-diff-order.sh
new file mode 100755
index 0000000..aec1d9d
--- /dev/null
+++ b/t/t4056-diff-order.sh
@@ -0,0 +1,200 @@
+#!/bin/sh
+
+test_description='diff order & rotate'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+create_files () {
+ echo "$1" >a.h &&
+ echo "$1" >b.c &&
+ echo "$1" >c/Makefile &&
+ echo "$1" >d.txt &&
+ git add a.h b.c c/Makefile d.txt &&
+ git commit -m"$1"
+}
+
+test_expect_success 'setup' '
+ mkdir c &&
+ create_files 1 &&
+ create_files 2 &&
+
+ cat >order_file_1 <<-\EOF &&
+ *Makefile
+ *.txt
+ *.h
+ EOF
+
+ cat >order_file_2 <<-\EOF &&
+ *Makefile
+ *.h
+ *.c
+ EOF
+
+ cat >expect_none <<-\EOF &&
+ a.h
+ b.c
+ c/Makefile
+ d.txt
+ EOF
+
+ cat >expect_1 <<-\EOF &&
+ c/Makefile
+ d.txt
+ a.h
+ b.c
+ EOF
+
+ cat >expect_2 <<-\EOF
+ c/Makefile
+ a.h
+ b.c
+ d.txt
+ EOF
+'
+
+test_expect_success "no order (=tree object order)" '
+ git diff --name-only HEAD^..HEAD >actual &&
+ test_cmp expect_none actual
+'
+
+test_expect_success 'missing orderfile' '
+ rm -f bogus_file &&
+ test_must_fail git diff -Obogus_file --name-only HEAD^..HEAD
+'
+
+test_expect_success POSIXPERM,SANITY 'unreadable orderfile' '
+ >unreadable_file &&
+ chmod -r unreadable_file &&
+ test_must_fail git diff -Ounreadable_file --name-only HEAD^..HEAD
+'
+
+test_expect_success "orderfile using option from subdir with --output" '
+ mkdir subdir &&
+ git -C subdir diff -O../order_file_1 --output ../actual --name-only HEAD^..HEAD &&
+ test_cmp expect_1 actual
+'
+
+for i in 1 2
+do
+ test_expect_success "orderfile using option ($i)" '
+ git diff -Oorder_file_$i --name-only HEAD^..HEAD >actual &&
+ test_cmp expect_$i actual
+ '
+
+ test_expect_success PIPE "orderfile is fifo ($i)" '
+ rm -f order_fifo &&
+ mkfifo order_fifo &&
+ {
+ cat order_file_$i >order_fifo &
+ } &&
+ git diff -O order_fifo --name-only HEAD^..HEAD >actual &&
+ wait &&
+ test_cmp expect_$i actual
+ '
+
+ test_expect_success "orderfile using config ($i)" '
+ git -c diff.orderfile=order_file_$i diff --name-only HEAD^..HEAD >actual &&
+ test_cmp expect_$i actual
+ '
+
+ test_expect_success "cancelling configured orderfile ($i)" '
+ git -c diff.orderfile=order_file_$i diff -O/dev/null --name-only HEAD^..HEAD >actual &&
+ test_cmp expect_none actual
+ '
+done
+
+test_expect_success 'setup for testing combine-diff order' '
+ git checkout -b tmp HEAD~ &&
+ create_files 3 &&
+ git checkout main &&
+ git merge --no-commit -s ours tmp &&
+ create_files 5
+'
+
+test_expect_success "combine-diff: no order (=tree object order)" '
+ git diff --name-only HEAD HEAD^ HEAD^2 >actual &&
+ test_cmp expect_none actual
+'
+
+for i in 1 2
+do
+ test_expect_success "combine-diff: orderfile using option ($i)" '
+ git diff -Oorder_file_$i --name-only HEAD HEAD^ HEAD^2 >actual &&
+ test_cmp expect_$i actual
+ '
+done
+
+### rotate and skip
+
+test_expect_success 'rotate and skip setup' '
+ >sample1.t &&
+ >sample2.t &&
+ >sample3.t &&
+ >sample4.t &&
+ git add sample[1234].t &&
+ git commit -m "added" sample[1234].t &&
+ echo modified >>sample1.t &&
+ echo modified >>sample2.t &&
+ echo modified >>sample4.t &&
+ git commit -m "updated" sample[1234].t
+'
+
+test_expect_success 'diff --rotate-to' '
+ git diff --rotate-to=sample2.t --name-only HEAD^ >actual &&
+ test_write_lines sample2.t sample4.t sample1.t >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'diff --skip-to' '
+ git diff --skip-to=sample2.t --name-only HEAD^ >actual &&
+ test_write_lines sample2.t sample4.t >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'diff --rotate/skip-to error condition' '
+ test_must_fail git diff --rotate-to=sample3.t HEAD^ &&
+ test_must_fail git diff --skip-to=sample3.t HEAD^
+'
+
+test_expect_success 'log --rotate-to' '
+ git log --rotate-to=sample3.t --raw HEAD~2.. >raw &&
+ # just distill the commit header and paths
+ sed -n -e "s/^commit.*/commit/p" \
+ -e "/^:/s/^.* //p" raw >actual &&
+
+ cat >expect <<-\EOF &&
+ commit
+ sample4.t
+ sample1.t
+ sample2.t
+ commit
+ sample3.t
+ sample4.t
+ sample1.t
+ sample2.t
+ EOF
+
+ test_cmp expect actual
+'
+
+test_expect_success 'log --skip-to' '
+ git log --skip-to=sample3.t --raw HEAD~2.. >raw &&
+ # just distill the commit header and paths
+ sed -n -e "s/^commit.*/commit/p" \
+ -e "/^:/s/^.* //p" raw >actual &&
+
+ cat >expect <<-\EOF &&
+ commit
+ sample4.t
+ commit
+ sample3.t
+ sample4.t
+ EOF
+
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t4057-diff-combined-paths.sh b/t/t4057-diff-combined-paths.sh
new file mode 100755
index 0000000..9a7505c
--- /dev/null
+++ b/t/t4057-diff-combined-paths.sh
@@ -0,0 +1,110 @@
+#!/bin/sh
+
+test_description='combined diff show only paths that are different to all parents'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# verify that diffc.expect matches output of
+# $(git diff -c --name-only HEAD HEAD^ HEAD^2)
+diffc_verify () {
+ git diff -c --name-only HEAD HEAD^ HEAD^2 >diffc.actual &&
+ test_cmp diffc.expect diffc.actual
+}
+
+test_expect_success 'trivial merge - combine-diff empty' '
+ for i in $(test_seq 1 9)
+ do
+ echo $i >$i.txt &&
+ git add $i.txt || return 1
+ done &&
+ git commit -m "init" &&
+ git checkout -b side &&
+ for i in $(test_seq 2 9)
+ do
+ echo $i/2 >>$i.txt || return 1
+ done &&
+ git commit -a -m "side 2-9" &&
+ git checkout main &&
+ echo 1/2 >1.txt &&
+ git commit -a -m "main 1" &&
+ git merge side &&
+ >diffc.expect &&
+ diffc_verify
+'
+
+
+test_expect_success 'only one truly conflicting path' '
+ git checkout side &&
+ for i in $(test_seq 2 9)
+ do
+ echo $i/3 >>$i.txt || return 1
+ done &&
+ echo "4side" >>4.txt &&
+ git commit -a -m "side 2-9 +4" &&
+ git checkout main &&
+ for i in $(test_seq 1 9)
+ do
+ echo $i/3 >>$i.txt || return 1
+ done &&
+ echo "4main" >>4.txt &&
+ git commit -a -m "main 1-9 +4" &&
+ test_must_fail git merge side &&
+ cat <<-\EOF >4.txt &&
+ 4
+ 4/2
+ 4/3
+ 4main
+ 4side
+ EOF
+ git add 4.txt &&
+ git commit -m "merge side (2)" &&
+ echo 4.txt >diffc.expect &&
+ diffc_verify
+'
+
+test_expect_success 'merge introduces new file' '
+ git checkout side &&
+ for i in $(test_seq 5 9)
+ do
+ echo $i/4 >>$i.txt || return 1
+ done &&
+ git commit -a -m "side 5-9" &&
+ git checkout main &&
+ for i in $(test_seq 1 3)
+ do
+ echo $i/4 >>$i.txt || return 1
+ done &&
+ git commit -a -m "main 1-3 +4hello" &&
+ git merge side &&
+ echo "Hello World" >4hello.txt &&
+ git add 4hello.txt &&
+ git commit --amend &&
+ echo 4hello.txt >diffc.expect &&
+ diffc_verify
+'
+
+test_expect_success 'merge removed a file' '
+ git checkout side &&
+ for i in $(test_seq 5 9)
+ do
+ echo $i/5 >>$i.txt || return 1
+ done &&
+ git commit -a -m "side 5-9" &&
+ git checkout main &&
+ for i in $(test_seq 1 3)
+ do
+ echo $i/4 >>$i.txt || return 1
+ done &&
+ git commit -a -m "main 1-3" &&
+ git merge side &&
+ git rm 4.txt &&
+ git commit --amend &&
+ echo 4.txt >diffc.expect &&
+ diffc_verify
+'
+
+test_done
diff --git a/t/t4058-diff-duplicates.sh b/t/t4058-diff-duplicates.sh
new file mode 100755
index 0000000..2501c89
--- /dev/null
+++ b/t/t4058-diff-duplicates.sh
@@ -0,0 +1,189 @@
+#!/bin/sh
+
+# NOTICE:
+# This testsuite does a number of diffs and checks that the output match.
+# However, it is a "garbage in, garbage out" situation; the trees have
+# duplicate entries for individual paths, and it results in diffs that do
+# not make much sense. As such, it is not clear that the diffs are
+# "correct". The primary purpose of these tests was to verify that
+# diff-tree does not segfault, but there is perhaps some value in ensuring
+# that the diff output isn't wildly unreasonable.
+
+test_description='test tree diff when trees have duplicate entries'
+. ./test-lib.sh
+
+# make_tree_entry <mode> <mode> <sha1>
+#
+# We have to rely on perl here because not all printfs understand
+# hex escapes (only octal), and xxd is not portable.
+make_tree_entry () {
+ printf '%s %s\0' "$1" "$2" &&
+ perl -e 'print chr(hex($_)) for ($ARGV[0] =~ /../g)' "$3"
+}
+
+# Like git-mktree, but without all of the pesky sanity checking.
+# Arguments come in groups of three, each group specifying a single
+# tree entry (see make_tree_entry above).
+make_tree () {
+ while test $# -gt 2; do
+ make_tree_entry "$1" "$2" "$3"
+ shift; shift; shift
+ done |
+ git hash-object --literally -w -t tree --stdin
+}
+
+# this is kind of a convoluted setup, but matches
+# a real-world case. Each tree contains four entries
+# for the given path, one with one sha1, and three with
+# the other. The first tree has them split across
+# two subtrees (which are themselves duplicate entries in
+# the root tree), and the second has them all in a single subtree.
+test_expect_success 'create trees with duplicate entries' '
+ blob_one=$(echo one | git hash-object -w --stdin) &&
+ blob_two=$(echo two | git hash-object -w --stdin) &&
+ inner_one_a=$(make_tree \
+ 100644 inner $blob_one
+ ) &&
+ inner_one_b=$(make_tree \
+ 100644 inner $blob_two \
+ 100644 inner $blob_two \
+ 100644 inner $blob_two
+ ) &&
+ outer_one=$(make_tree \
+ 040000 outer $inner_one_a \
+ 040000 outer $inner_one_b
+ ) &&
+ inner_two=$(make_tree \
+ 100644 inner $blob_one \
+ 100644 inner $blob_two \
+ 100644 inner $blob_two \
+ 100644 inner $blob_two
+ ) &&
+ outer_two=$(make_tree \
+ 040000 outer $inner_two
+ ) &&
+ git tag one $outer_one &&
+ git tag two $outer_two
+'
+
+test_expect_success 'create tree without duplicate entries' '
+ blob_one=$(echo one | git hash-object -w --stdin) &&
+ outer_three=$(make_tree \
+ 100644 renamed $blob_one
+ ) &&
+ git tag three $outer_three
+'
+
+test_expect_success 'diff-tree between duplicate trees' '
+ # See NOTICE at top of file
+ {
+ printf ":000000 100644 $ZERO_OID $blob_two A\touter/inner\n" &&
+ printf ":000000 100644 $ZERO_OID $blob_two A\touter/inner\n" &&
+ printf ":000000 100644 $ZERO_OID $blob_two A\touter/inner\n" &&
+ printf ":100644 000000 $blob_two $ZERO_OID D\touter/inner\n" &&
+ printf ":100644 000000 $blob_two $ZERO_OID D\touter/inner\n" &&
+ printf ":100644 000000 $blob_two $ZERO_OID D\touter/inner\n"
+ } >expect &&
+ git diff-tree -r --no-abbrev one two >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'diff-tree with renames' '
+ # See NOTICE at top of file.
+ git diff-tree -M -r --no-abbrev one two >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'diff-tree FROM duplicate tree' '
+ # See NOTICE at top of file.
+ {
+ printf ":100644 000000 $blob_one $ZERO_OID D\touter/inner\n" &&
+ printf ":100644 000000 $blob_two $ZERO_OID D\touter/inner\n" &&
+ printf ":100644 000000 $blob_two $ZERO_OID D\touter/inner\n" &&
+ printf ":100644 000000 $blob_two $ZERO_OID D\touter/inner\n" &&
+ printf ":000000 100644 $ZERO_OID $blob_one A\trenamed\n"
+ } >expect &&
+ git diff-tree -r --no-abbrev one three >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'diff-tree FROM duplicate tree, with renames' '
+ # See NOTICE at top of file.
+ {
+ printf ":100644 000000 $blob_two $ZERO_OID D\touter/inner\n" &&
+ printf ":100644 000000 $blob_two $ZERO_OID D\touter/inner\n" &&
+ printf ":100644 000000 $blob_two $ZERO_OID D\touter/inner\n" &&
+ printf ":100644 100644 $blob_one $blob_one R100\touter/inner\trenamed\n"
+ } >expect &&
+ git diff-tree -M -r --no-abbrev one three >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'create a few commits' '
+ git commit-tree -m "Duplicate Entries" two^{tree} >commit_id &&
+ git branch base $(cat commit_id) &&
+
+ git commit-tree -p $(cat commit_id) -m "Just one" three^{tree} >up &&
+ git branch update $(cat up) &&
+
+ git commit-tree -p $(cat up) -m "Back to weird" two^{tree} >final &&
+ git branch final $(cat final) &&
+
+ rm commit_id up final
+'
+
+test_expect_failure 'git read-tree does not segfault' '
+ test_when_finished rm .git/index.lock &&
+ test_might_fail git read-tree --reset base
+'
+
+test_expect_failure 'reset --hard does not segfault' '
+ test_when_finished rm .git/index.lock &&
+ git checkout base &&
+ test_might_fail git reset --hard
+'
+
+test_expect_failure 'git diff HEAD does not segfault' '
+ git checkout base &&
+ GIT_TEST_CHECK_CACHE_TREE=false &&
+ git reset --hard &&
+ test_might_fail git diff HEAD
+'
+
+test_expect_failure 'can switch to another branch when status is empty' '
+ git clean -ffdqx &&
+ git status --porcelain -uno >actual &&
+ test_must_be_empty actual &&
+ git checkout update
+'
+
+test_expect_success 'forcibly switch to another branch, verify status empty' '
+ git checkout -f update &&
+ git status --porcelain -uno >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'fast-forward from non-duplicate entries to duplicate' '
+ git merge final
+'
+
+test_expect_failure 'clean status, switch branches, status still clean' '
+ git status --porcelain -uno >actual &&
+ test_must_be_empty actual &&
+ git checkout base &&
+ git status --porcelain -uno >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'switch to base branch and force status to be clean' '
+ git checkout base &&
+ GIT_TEST_CHECK_CACHE_TREE=false git reset --hard &&
+ git status --porcelain -uno >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_failure 'fast-forward from duplicate entries to non-duplicate' '
+ git merge update
+'
+
+test_done
diff --git a/t/t4059-diff-submodule-not-initialized.sh b/t/t4059-diff-submodule-not-initialized.sh
new file mode 100755
index 0000000..d489230
--- /dev/null
+++ b/t/t4059-diff-submodule-not-initialized.sh
@@ -0,0 +1,127 @@
+#!/bin/sh
+#
+# Copyright (c) 2016 Jacob Keller, based on t4041 by Jens Lehmann
+#
+
+test_description='Test for submodule diff on non-checked out submodule
+
+This test tries to verify that add_submodule_odb works when the submodule was
+initialized previously but the checkout has since been removed.
+'
+
+. ./test-lib.sh
+
+# Tested non-UTF-8 encoding
+test_encoding="ISO8859-1"
+
+# String "added" in German (translated with Google Translate), encoded in UTF-8,
+# used in sample commit log messages in add_file() function below.
+added=$(printf "hinzugef\303\274gt")
+
+add_file () {
+ (
+ cd "$1" &&
+ shift &&
+ for name
+ do
+ echo "$name" >"$name" &&
+ git add "$name" &&
+ test_tick &&
+ # "git commit -m" would break MinGW, as Windows refuse to pass
+ # $test_encoding encoded parameter to git.
+ echo "Add $name ($added $name)" | iconv -f utf-8 -t $test_encoding |
+ git -c "i18n.commitEncoding=$test_encoding" commit -F -
+ done >/dev/null &&
+ git rev-parse --short --verify HEAD
+ )
+}
+
+commit_file () {
+ test_tick &&
+ git commit "$@" -m "Commit $*" >/dev/null
+}
+
+test_expect_success 'setup - submodules' '
+ test_create_repo sm2 &&
+ add_file . foo &&
+ add_file sm2 foo1 foo2 &&
+ smhead1=$(git -C sm2 rev-parse --short --verify HEAD)
+'
+
+test_expect_success 'setup - git submodule add' '
+ git -c protocol.file.allow=always submodule add ./sm2 sm1 &&
+ commit_file sm1 .gitmodules &&
+ git diff-tree -p --no-commit-id --submodule=log HEAD -- sm1 >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 0000000...$smhead1 (new submodule)
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'submodule directory removed' '
+ rm -rf sm1 &&
+ git diff-tree -p --no-commit-id --submodule=log HEAD -- sm1 >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 0000000...$smhead1 (new submodule)
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'setup - submodule multiple commits' '
+ git submodule update --checkout sm1 &&
+ smhead2=$(add_file sm1 foo3 foo4) &&
+ commit_file sm1 &&
+ git diff-tree -p --no-commit-id --submodule=log HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 $smhead1..$smhead2:
+ > Add foo4 ($added foo4)
+ > Add foo3 ($added foo3)
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'submodule removed multiple commits' '
+ rm -rf sm1 &&
+ git diff-tree -p --no-commit-id --submodule=log HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 $smhead1..$smhead2:
+ > Add foo4 ($added foo4)
+ > Add foo3 ($added foo3)
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'submodule not initialized in new clone' '
+ git clone . sm3 &&
+ git -C sm3 diff-tree -p --no-commit-id --submodule=log HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 $smhead1...$smhead2 (commits not present)
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'setup submodule moved' '
+ git submodule update --checkout sm1 &&
+ git mv sm1 sm4 &&
+ commit_file sm4 &&
+ git diff-tree -p --no-commit-id --submodule=log HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm4 0000000...$smhead2 (new submodule)
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'submodule moved then removed' '
+ smhead3=$(add_file sm4 foo6 foo7) &&
+ commit_file sm4 &&
+ rm -rf sm4 &&
+ git diff-tree -p --no-commit-id --submodule=log HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm4 $smhead2..$smhead3:
+ > Add foo7 ($added foo7)
+ > Add foo6 ($added foo6)
+ EOF
+ test_cmp expected actual
+'
+
+test_done
diff --git a/t/t4060-diff-submodule-option-diff-format.sh b/t/t4060-diff-submodule-option-diff-format.sh
new file mode 100755
index 0000000..97c6424
--- /dev/null
+++ b/t/t4060-diff-submodule-option-diff-format.sh
@@ -0,0 +1,976 @@
+#!/bin/sh
+#
+# Copyright (c) 2009 Jens Lehmann, based on t7401 by Ping Yin
+# Copyright (c) 2011 Alexey Shumkin (+ non-UTF-8 commit encoding tests)
+# Copyright (c) 2016 Jacob Keller (copy + convert to --submodule=diff)
+#
+
+test_description='Support for diff format verbose submodule difference in git diff
+
+This test tries to verify the sanity of --submodule=diff option of git diff.
+'
+
+. ./test-lib.sh
+
+# Tested non-UTF-8 encoding
+test_encoding="ISO8859-1"
+
+# String "added" in German (translated with Google Translate), encoded in UTF-8,
+# used in sample commit log messages in add_file() function below.
+added=$(printf "hinzugef\303\274gt")
+
+add_file () {
+ (
+ cd "$1" &&
+ shift &&
+ for name
+ do
+ echo "$name" >"$name" &&
+ git add "$name" &&
+ test_tick &&
+ # "git commit -m" would break MinGW, as Windows refuse to pass
+ # $test_encoding encoded parameter to git.
+ echo "Add $name ($added $name)" | iconv -f utf-8 -t $test_encoding |
+ git -c "i18n.commitEncoding=$test_encoding" commit -F -
+ done >/dev/null &&
+ git rev-parse --short --verify HEAD
+ )
+}
+
+commit_file () {
+ test_tick &&
+ git commit "$@" -m "Commit $*" >/dev/null
+}
+
+diff_cmp () {
+ for i in "$1" "$2"
+ do
+ sed -e 's/^index 0000000\.\.[0-9a-f]*/index 0000000..1234567/' \
+ -e 's/^index [0-9a-f]*\.\.[0-9a-f]*/index 1234567..89abcde/' \
+ "$i" >"$i.compare" || return 1
+ done &&
+ test_cmp "$1.compare" "$2.compare" &&
+ rm -f "$1.compare" "$2.compare"
+}
+
+test_expect_success 'setup repository' '
+ test_create_repo sm1 &&
+ add_file . foo &&
+ head1=$(add_file sm1 foo1 foo2) &&
+ fullhead1=$(git -C sm1 rev-parse --verify HEAD)
+'
+
+test_expect_success 'added submodule' '
+ git add sm1 &&
+ git diff-index -p --submodule=diff HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 0000000...$head1 (new submodule)
+ diff --git a/sm1/foo1 b/sm1/foo1
+ new file mode 100644
+ index 0000000..1715acd
+ --- /dev/null
+ +++ b/sm1/foo1
+ @@ -0,0 +1 @@
+ +foo1
+ diff --git a/sm1/foo2 b/sm1/foo2
+ new file mode 100644
+ index 0000000..54b060e
+ --- /dev/null
+ +++ b/sm1/foo2
+ @@ -0,0 +1 @@
+ +foo2
+ EOF
+ diff_cmp expected actual
+'
+
+test_expect_success 'added submodule, set diff.submodule' '
+ test_config diff.submodule log &&
+ git add sm1 &&
+ git diff-index -p --submodule=diff HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 0000000...$head1 (new submodule)
+ diff --git a/sm1/foo1 b/sm1/foo1
+ new file mode 100644
+ index 0000000..1715acd
+ --- /dev/null
+ +++ b/sm1/foo1
+ @@ -0,0 +1 @@
+ +foo1
+ diff --git a/sm1/foo2 b/sm1/foo2
+ new file mode 100644
+ index 0000000..54b060e
+ --- /dev/null
+ +++ b/sm1/foo2
+ @@ -0,0 +1 @@
+ +foo2
+ EOF
+ diff_cmp expected actual
+'
+
+test_expect_success '--submodule=short overrides diff.submodule' '
+ test_config diff.submodule log &&
+ git add sm1 &&
+ git diff --submodule=short --cached >actual &&
+ cat >expected <<-EOF &&
+ diff --git a/sm1 b/sm1
+ new file mode 160000
+ index 0000000..$head1
+ --- /dev/null
+ +++ b/sm1
+ @@ -0,0 +1 @@
+ +Subproject commit $fullhead1
+ EOF
+ diff_cmp expected actual
+'
+
+test_expect_success 'diff.submodule does not affect plumbing' '
+ test_config diff.submodule log &&
+ git diff-index -p HEAD >actual &&
+ cat >expected <<-EOF &&
+ diff --git a/sm1 b/sm1
+ new file mode 160000
+ index 0000000..$head1
+ --- /dev/null
+ +++ b/sm1
+ @@ -0,0 +1 @@
+ +Subproject commit $fullhead1
+ EOF
+ diff_cmp expected actual
+'
+
+commit_file sm1 &&
+head2=$(add_file sm1 foo3)
+
+test_expect_success 'modified submodule(forward)' '
+ git diff-index -p --submodule=diff HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 $head1..$head2:
+ diff --git a/sm1/foo3 b/sm1/foo3
+ new file mode 100644
+ index 0000000..c1ec6c6
+ --- /dev/null
+ +++ b/sm1/foo3
+ @@ -0,0 +1 @@
+ +foo3
+ EOF
+ diff_cmp expected actual
+'
+
+test_expect_success 'modified submodule(forward)' '
+ git diff --submodule=diff >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 $head1..$head2:
+ diff --git a/sm1/foo3 b/sm1/foo3
+ new file mode 100644
+ index 0000000..c1ec6c6
+ --- /dev/null
+ +++ b/sm1/foo3
+ @@ -0,0 +1 @@
+ +foo3
+ EOF
+ diff_cmp expected actual
+'
+
+test_expect_success 'modified submodule(forward) --submodule' '
+ git diff --submodule >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 $head1..$head2:
+ > Add foo3 ($added foo3)
+ EOF
+ diff_cmp expected actual
+'
+
+fullhead2=$(cd sm1; git rev-parse --verify HEAD)
+test_expect_success 'modified submodule(forward) --submodule=short' '
+ git diff --submodule=short >actual &&
+ cat >expected <<-EOF &&
+ diff --git a/sm1 b/sm1
+ index $head1..$head2 160000
+ --- a/sm1
+ +++ b/sm1
+ @@ -1 +1 @@
+ -Subproject commit $fullhead1
+ +Subproject commit $fullhead2
+ EOF
+ diff_cmp expected actual
+'
+
+commit_file sm1 &&
+head3=$(
+ cd sm1 &&
+ git reset --hard HEAD~2 >/dev/null &&
+ git rev-parse --short --verify HEAD
+)
+
+test_expect_success 'modified submodule(backward)' '
+ git diff-index -p --submodule=diff HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 $head2..$head3 (rewind):
+ diff --git a/sm1/foo2 b/sm1/foo2
+ deleted file mode 100644
+ index 54b060e..0000000
+ --- a/sm1/foo2
+ +++ /dev/null
+ @@ -1 +0,0 @@
+ -foo2
+ diff --git a/sm1/foo3 b/sm1/foo3
+ deleted file mode 100644
+ index c1ec6c6..0000000
+ --- a/sm1/foo3
+ +++ /dev/null
+ @@ -1 +0,0 @@
+ -foo3
+ EOF
+ diff_cmp expected actual
+'
+
+head4=$(add_file sm1 foo4 foo5)
+test_expect_success 'modified submodule(backward and forward)' '
+ git diff-index -p --submodule=diff HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 $head2...$head4:
+ diff --git a/sm1/foo2 b/sm1/foo2
+ deleted file mode 100644
+ index 54b060e..0000000
+ --- a/sm1/foo2
+ +++ /dev/null
+ @@ -1 +0,0 @@
+ -foo2
+ diff --git a/sm1/foo3 b/sm1/foo3
+ deleted file mode 100644
+ index c1ec6c6..0000000
+ --- a/sm1/foo3
+ +++ /dev/null
+ @@ -1 +0,0 @@
+ -foo3
+ diff --git a/sm1/foo4 b/sm1/foo4
+ new file mode 100644
+ index 0000000..a0016db
+ --- /dev/null
+ +++ b/sm1/foo4
+ @@ -0,0 +1 @@
+ +foo4
+ diff --git a/sm1/foo5 b/sm1/foo5
+ new file mode 100644
+ index 0000000..d6f2413
+ --- /dev/null
+ +++ b/sm1/foo5
+ @@ -0,0 +1 @@
+ +foo5
+ EOF
+ diff_cmp expected actual
+'
+
+commit_file sm1 &&
+mv sm1 sm1-bak &&
+echo sm1 >sm1 &&
+head5=$(git hash-object sm1 | cut -c1-7) &&
+git add sm1 &&
+rm -f sm1 &&
+mv sm1-bak sm1
+
+test_expect_success 'typechanged submodule(submodule->blob), --cached' '
+ git diff --submodule=diff --cached >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 $head4...0000000 (submodule deleted)
+ diff --git a/sm1/foo1 b/sm1/foo1
+ deleted file mode 100644
+ index 1715acd..0000000
+ --- a/sm1/foo1
+ +++ /dev/null
+ @@ -1 +0,0 @@
+ -foo1
+ diff --git a/sm1/foo4 b/sm1/foo4
+ deleted file mode 100644
+ index a0016db..0000000
+ --- a/sm1/foo4
+ +++ /dev/null
+ @@ -1 +0,0 @@
+ -foo4
+ diff --git a/sm1/foo5 b/sm1/foo5
+ deleted file mode 100644
+ index d6f2413..0000000
+ --- a/sm1/foo5
+ +++ /dev/null
+ @@ -1 +0,0 @@
+ -foo5
+ diff --git a/sm1 b/sm1
+ new file mode 100644
+ index 0000000..9da5fb8
+ --- /dev/null
+ +++ b/sm1
+ @@ -0,0 +1 @@
+ +sm1
+ EOF
+ diff_cmp expected actual
+'
+
+test_expect_success 'typechanged submodule(submodule->blob)' '
+ git diff --submodule=diff >actual &&
+ cat >expected <<-EOF &&
+ diff --git a/sm1 b/sm1
+ deleted file mode 100644
+ index 9da5fb8..0000000
+ --- a/sm1
+ +++ /dev/null
+ @@ -1 +0,0 @@
+ -sm1
+ Submodule sm1 0000000...$head4 (new submodule)
+ diff --git a/sm1/foo1 b/sm1/foo1
+ new file mode 100644
+ index 0000000..1715acd
+ --- /dev/null
+ +++ b/sm1/foo1
+ @@ -0,0 +1 @@
+ +foo1
+ diff --git a/sm1/foo4 b/sm1/foo4
+ new file mode 100644
+ index 0000000..a0016db
+ --- /dev/null
+ +++ b/sm1/foo4
+ @@ -0,0 +1 @@
+ +foo4
+ diff --git a/sm1/foo5 b/sm1/foo5
+ new file mode 100644
+ index 0000000..d6f2413
+ --- /dev/null
+ +++ b/sm1/foo5
+ @@ -0,0 +1 @@
+ +foo5
+ EOF
+ diff_cmp expected actual
+'
+
+rm -rf sm1 &&
+git checkout-index sm1
+test_expect_success 'typechanged submodule(submodule->blob)' '
+ git diff-index -p --submodule=diff HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 $head4...0000000 (submodule deleted)
+ diff --git a/sm1 b/sm1
+ new file mode 100644
+ index 0000000..9da5fb8
+ --- /dev/null
+ +++ b/sm1
+ @@ -0,0 +1 @@
+ +sm1
+ EOF
+ diff_cmp expected actual
+'
+
+rm -f sm1 &&
+test_create_repo sm1 &&
+head6=$(add_file sm1 foo6 foo7)
+test_expect_success 'nonexistent commit' '
+ git diff-index -p --submodule=diff HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 $head4...$head6 (commits not present)
+ EOF
+ diff_cmp expected actual
+'
+
+commit_file
+test_expect_success 'typechanged submodule(blob->submodule)' '
+ git diff-index -p --submodule=diff HEAD >actual &&
+ cat >expected <<-EOF &&
+ diff --git a/sm1 b/sm1
+ deleted file mode 100644
+ index 9da5fb8..0000000
+ --- a/sm1
+ +++ /dev/null
+ @@ -1 +0,0 @@
+ -sm1
+ Submodule sm1 0000000...$head6 (new submodule)
+ diff --git a/sm1/foo6 b/sm1/foo6
+ new file mode 100644
+ index 0000000..462398b
+ --- /dev/null
+ +++ b/sm1/foo6
+ @@ -0,0 +1 @@
+ +foo6
+ diff --git a/sm1/foo7 b/sm1/foo7
+ new file mode 100644
+ index 0000000..6e9262c
+ --- /dev/null
+ +++ b/sm1/foo7
+ @@ -0,0 +1 @@
+ +foo7
+ EOF
+ diff_cmp expected actual
+'
+
+commit_file sm1 &&
+test_expect_success 'submodule is up to date' '
+ head7=$(git -C sm1 rev-parse --short --verify HEAD) &&
+ git diff-index -p --submodule=diff HEAD >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'submodule contains untracked content' '
+ echo new > sm1/new-file &&
+ git diff-index -p --ignore-submodules=none --submodule=diff HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 contains untracked content
+ EOF
+ diff_cmp expected actual
+'
+
+test_expect_success 'submodule contains untracked content (untracked ignored)' '
+ git diff-index -p --submodule=diff HEAD >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'submodule contains untracked content (dirty ignored)' '
+ git diff-index -p --ignore-submodules=dirty --submodule=diff HEAD >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'submodule contains untracked content (all ignored)' '
+ git diff-index -p --ignore-submodules=all --submodule=diff HEAD >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'submodule contains untracked and modified content' '
+ echo new > sm1/foo6 &&
+ git diff-index -p --ignore-submodules=none --submodule=diff HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 contains untracked content
+ Submodule sm1 contains modified content
+ diff --git a/sm1/foo6 b/sm1/foo6
+ index 462398b..3e75765 100644
+ --- a/sm1/foo6
+ +++ b/sm1/foo6
+ @@ -1 +1 @@
+ -foo6
+ +new
+ EOF
+ diff_cmp expected actual
+'
+
+# NOT OK
+test_expect_success 'submodule contains untracked and modified content (untracked ignored)' '
+ echo new > sm1/foo6 &&
+ git diff-index -p --submodule=diff HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 contains modified content
+ diff --git a/sm1/foo6 b/sm1/foo6
+ index 462398b..3e75765 100644
+ --- a/sm1/foo6
+ +++ b/sm1/foo6
+ @@ -1 +1 @@
+ -foo6
+ +new
+ EOF
+ diff_cmp expected actual
+'
+
+test_expect_success 'submodule contains untracked and modified content (dirty ignored)' '
+ echo new > sm1/foo6 &&
+ git diff-index -p --ignore-submodules=dirty --submodule=diff HEAD >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'submodule contains untracked and modified content (all ignored)' '
+ echo new > sm1/foo6 &&
+ git diff-index -p --ignore-submodules --submodule=diff HEAD >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'submodule contains modified content' '
+ rm -f sm1/new-file &&
+ git diff-index -p --submodule=diff HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 contains modified content
+ diff --git a/sm1/foo6 b/sm1/foo6
+ index 462398b..3e75765 100644
+ --- a/sm1/foo6
+ +++ b/sm1/foo6
+ @@ -1 +1 @@
+ -foo6
+ +new
+ EOF
+ diff_cmp expected actual
+'
+
+(cd sm1; git commit -mchange foo6 >/dev/null) &&
+head8=$(cd sm1; git rev-parse --short --verify HEAD) &&
+test_expect_success 'submodule is modified' '
+ git diff-index -p --submodule=diff HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 $head7..$head8:
+ diff --git a/sm1/foo6 b/sm1/foo6
+ index 462398b..3e75765 100644
+ --- a/sm1/foo6
+ +++ b/sm1/foo6
+ @@ -1 +1 @@
+ -foo6
+ +new
+ EOF
+ diff_cmp expected actual
+'
+
+test_expect_success 'modified submodule contains untracked content' '
+ echo new > sm1/new-file &&
+ git diff-index -p --ignore-submodules=none --submodule=diff HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 contains untracked content
+ Submodule sm1 $head7..$head8:
+ diff --git a/sm1/foo6 b/sm1/foo6
+ index 462398b..3e75765 100644
+ --- a/sm1/foo6
+ +++ b/sm1/foo6
+ @@ -1 +1 @@
+ -foo6
+ +new
+ EOF
+ diff_cmp expected actual
+'
+
+test_expect_success 'modified submodule contains untracked content (untracked ignored)' '
+ git diff-index -p --submodule=diff HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 $head7..$head8:
+ diff --git a/sm1/foo6 b/sm1/foo6
+ index 462398b..3e75765 100644
+ --- a/sm1/foo6
+ +++ b/sm1/foo6
+ @@ -1 +1 @@
+ -foo6
+ +new
+ EOF
+ diff_cmp expected actual
+'
+
+test_expect_success 'modified submodule contains untracked content (dirty ignored)' '
+ git diff-index -p --ignore-submodules=dirty --submodule=diff HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 $head7..$head8:
+ diff --git a/sm1/foo6 b/sm1/foo6
+ index 462398b..3e75765 100644
+ --- a/sm1/foo6
+ +++ b/sm1/foo6
+ @@ -1 +1 @@
+ -foo6
+ +new
+ EOF
+ diff_cmp expected actual
+'
+
+test_expect_success 'modified submodule contains untracked content (all ignored)' '
+ git diff-index -p --ignore-submodules=all --submodule=diff HEAD >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'modified submodule contains untracked and modified content' '
+ echo modification >> sm1/foo6 &&
+ git diff-index -p --ignore-submodules=none --submodule=diff HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 contains untracked content
+ Submodule sm1 contains modified content
+ Submodule sm1 $head7..$head8:
+ diff --git a/sm1/foo6 b/sm1/foo6
+ index 462398b..dfda541 100644
+ --- a/sm1/foo6
+ +++ b/sm1/foo6
+ @@ -1 +1,2 @@
+ -foo6
+ +new
+ +modification
+ EOF
+ diff_cmp expected actual
+'
+
+test_expect_success 'modified submodule contains untracked and modified content (untracked ignored)' '
+ echo modification >> sm1/foo6 &&
+ git diff-index -p --submodule=diff HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 contains modified content
+ Submodule sm1 $head7..$head8:
+ diff --git a/sm1/foo6 b/sm1/foo6
+ index 462398b..e20e2d9 100644
+ --- a/sm1/foo6
+ +++ b/sm1/foo6
+ @@ -1 +1,3 @@
+ -foo6
+ +new
+ +modification
+ +modification
+ EOF
+ diff_cmp expected actual
+'
+
+test_expect_success 'modified submodule contains untracked and modified content (dirty ignored)' '
+ echo modification >> sm1/foo6 &&
+ git diff-index -p --ignore-submodules=dirty --submodule=diff HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 $head7..$head8:
+ diff --git a/sm1/foo6 b/sm1/foo6
+ index 462398b..3e75765 100644
+ --- a/sm1/foo6
+ +++ b/sm1/foo6
+ @@ -1 +1 @@
+ -foo6
+ +new
+ EOF
+ diff_cmp expected actual
+'
+
+test_expect_success 'modified submodule contains untracked and modified content (all ignored)' '
+ echo modification >> sm1/foo6 &&
+ git diff-index -p --ignore-submodules --submodule=diff HEAD >actual &&
+ test_must_be_empty actual
+'
+
+# NOT OK
+test_expect_success 'modified submodule contains modified content' '
+ rm -f sm1/new-file &&
+ git diff-index -p --submodule=diff HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 contains modified content
+ Submodule sm1 $head7..$head8:
+ diff --git a/sm1/foo6 b/sm1/foo6
+ index 462398b..ac466ca 100644
+ --- a/sm1/foo6
+ +++ b/sm1/foo6
+ @@ -1 +1,5 @@
+ -foo6
+ +new
+ +modification
+ +modification
+ +modification
+ +modification
+ EOF
+ diff_cmp expected actual
+'
+
+rm -rf sm1
+test_expect_success 'deleted submodule' '
+ git diff-index -p --submodule=diff HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 $head7...0000000 (submodule deleted)
+ EOF
+ diff_cmp expected actual
+'
+
+test_expect_success 'create second submodule' '
+ test_create_repo sm2 &&
+ head9=$(add_file sm2 foo8 foo9) &&
+ git add sm2
+'
+
+test_expect_success 'multiple submodules' '
+ git diff-index -p --submodule=diff HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 $head7...0000000 (submodule deleted)
+ Submodule sm2 0000000...$head9 (new submodule)
+ diff --git a/sm2/foo8 b/sm2/foo8
+ new file mode 100644
+ index 0000000..db9916b
+ --- /dev/null
+ +++ b/sm2/foo8
+ @@ -0,0 +1 @@
+ +foo8
+ diff --git a/sm2/foo9 b/sm2/foo9
+ new file mode 100644
+ index 0000000..9c3b4f6
+ --- /dev/null
+ +++ b/sm2/foo9
+ @@ -0,0 +1 @@
+ +foo9
+ EOF
+ diff_cmp expected actual
+'
+
+test_expect_success 'path filter' '
+ git diff-index -p --submodule=diff HEAD sm2 >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm2 0000000...$head9 (new submodule)
+ diff --git a/sm2/foo8 b/sm2/foo8
+ new file mode 100644
+ index 0000000..db9916b
+ --- /dev/null
+ +++ b/sm2/foo8
+ @@ -0,0 +1 @@
+ +foo8
+ diff --git a/sm2/foo9 b/sm2/foo9
+ new file mode 100644
+ index 0000000..9c3b4f6
+ --- /dev/null
+ +++ b/sm2/foo9
+ @@ -0,0 +1 @@
+ +foo9
+ EOF
+ diff_cmp expected actual
+'
+
+cat >.gitmodules <<-EOF
+[submodule "sm2"]
+ path = sm2
+ url = bogus_url
+EOF
+git add .gitmodules
+commit_file sm2 .gitmodules
+
+test_expect_success 'given commit' '
+ git diff-index -p --submodule=diff HEAD^ >actual &&
+ cat >expected <<-EOF &&
+ diff --git a/.gitmodules b/.gitmodules
+ new file mode 100644
+ index 1234567..89abcde
+ --- /dev/null
+ +++ b/.gitmodules
+ @@ -0,0 +1,3 @@
+ +[submodule "sm2"]
+ +path = sm2
+ +url = bogus_url
+ Submodule sm1 $head7...0000000 (submodule deleted)
+ Submodule sm2 0000000...$head9 (new submodule)
+ diff --git a/sm2/foo8 b/sm2/foo8
+ new file mode 100644
+ index 0000000..db9916b
+ --- /dev/null
+ +++ b/sm2/foo8
+ @@ -0,0 +1 @@
+ +foo8
+ diff --git a/sm2/foo9 b/sm2/foo9
+ new file mode 100644
+ index 0000000..9c3b4f6
+ --- /dev/null
+ +++ b/sm2/foo9
+ @@ -0,0 +1 @@
+ +foo9
+ EOF
+ diff_cmp expected actual
+'
+
+test_expect_success 'setup .git file for sm2' '
+ git submodule absorbgitdirs sm2
+'
+
+test_expect_success 'diff --submodule=diff with .git file' '
+ git diff --submodule=diff HEAD^ >actual &&
+ cat >expected <<-EOF &&
+ diff --git a/.gitmodules b/.gitmodules
+ new file mode 100644
+ index 1234567..89abcde
+ --- /dev/null
+ +++ b/.gitmodules
+ @@ -0,0 +1,3 @@
+ +[submodule "sm2"]
+ +path = sm2
+ +url = bogus_url
+ Submodule sm1 $head7...0000000 (submodule deleted)
+ Submodule sm2 0000000...$head9 (new submodule)
+ diff --git a/sm2/foo8 b/sm2/foo8
+ new file mode 100644
+ index 0000000..db9916b
+ --- /dev/null
+ +++ b/sm2/foo8
+ @@ -0,0 +1 @@
+ +foo8
+ diff --git a/sm2/foo9 b/sm2/foo9
+ new file mode 100644
+ index 0000000..9c3b4f6
+ --- /dev/null
+ +++ b/sm2/foo9
+ @@ -0,0 +1 @@
+ +foo9
+ EOF
+ diff_cmp expected actual
+'
+
+mv sm2 sm2-bak
+
+test_expect_success 'deleted submodule with .git file' '
+ git diff-index -p --submodule=diff HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 $head7...0000000 (submodule deleted)
+ Submodule sm2 $head9...0000000 (submodule deleted)
+ diff --git a/sm2/foo8 b/sm2/foo8
+ deleted file mode 100644
+ index 1234567..89abcde
+ --- a/sm2/foo8
+ +++ /dev/null
+ @@ -1 +0,0 @@
+ -foo8
+ diff --git a/sm2/foo9 b/sm2/foo9
+ deleted file mode 100644
+ index 1234567..89abcde
+ --- a/sm2/foo9
+ +++ /dev/null
+ @@ -1 +0,0 @@
+ -foo9
+ EOF
+ diff_cmp expected actual
+'
+
+echo submodule-to-blob>sm2
+
+test_expect_success 'typechanged(submodule->blob) submodule with .git file' '
+ git diff-index -p --submodule=diff HEAD >actual &&
+ cat >expected <<-EOF &&
+ Submodule sm1 $head7...0000000 (submodule deleted)
+ Submodule sm2 $head9...0000000 (submodule deleted)
+ diff --git a/sm2/foo8 b/sm2/foo8
+ deleted file mode 100644
+ index 1234567..89abcde
+ --- a/sm2/foo8
+ +++ /dev/null
+ @@ -1 +0,0 @@
+ -foo8
+ diff --git a/sm2/foo9 b/sm2/foo9
+ deleted file mode 100644
+ index 1234567..89abcde
+ --- a/sm2/foo9
+ +++ /dev/null
+ @@ -1 +0,0 @@
+ -foo9
+ diff --git a/sm2 b/sm2
+ new file mode 100644
+ index 1234567..89abcde
+ --- /dev/null
+ +++ b/sm2
+ @@ -0,0 +1 @@
+ +submodule-to-blob
+ EOF
+ diff_cmp expected actual
+'
+
+rm sm2
+mv sm2-bak sm2
+
+test_expect_success 'setup nested submodule' '
+ git -c protocol.file.allow=always -C sm2 submodule add ../sm2 nested &&
+ git -C sm2 commit -a -m "nested sub" &&
+ head10=$(git -C sm2 rev-parse --short --verify HEAD)
+'
+
+test_expect_success 'move nested submodule HEAD' '
+ echo "nested content" >sm2/nested/file &&
+ git -C sm2/nested add file &&
+ git -C sm2/nested commit --allow-empty -m "new HEAD" &&
+ head11=$(git -C sm2/nested rev-parse --short --verify HEAD)
+'
+
+test_expect_success 'diff --submodule=diff with moved nested submodule HEAD' '
+ cat >expected <<-EOF &&
+ Submodule nested $head9..$head11:
+ diff --git a/nested/file b/nested/file
+ new file mode 100644
+ index 0000000..ca281f5
+ --- /dev/null
+ +++ b/nested/file
+ @@ -0,0 +1 @@
+ +nested content
+ EOF
+ git -C sm2 diff --submodule=diff >actual 2>err &&
+ test_must_be_empty err &&
+ diff_cmp expected actual
+'
+
+test_expect_success 'diff --submodule=diff recurses into nested submodules' '
+ cat >expected <<-EOF &&
+ Submodule sm1 $head7...0000000 (submodule deleted)
+ Submodule sm2 contains modified content
+ Submodule sm2 $head9..$head10:
+ diff --git a/sm2/.gitmodules b/sm2/.gitmodules
+ new file mode 100644
+ index 0000000..3a816b8
+ --- /dev/null
+ +++ b/sm2/.gitmodules
+ @@ -0,0 +1,3 @@
+ +[submodule "nested"]
+ + path = nested
+ + url = ../sm2
+ Submodule nested 0000000...$head11 (new submodule)
+ diff --git a/sm2/nested/file b/sm2/nested/file
+ new file mode 100644
+ index 0000000..ca281f5
+ --- /dev/null
+ +++ b/sm2/nested/file
+ @@ -0,0 +1 @@
+ +nested content
+ diff --git a/sm2/nested/foo8 b/sm2/nested/foo8
+ new file mode 100644
+ index 0000000..db9916b
+ --- /dev/null
+ +++ b/sm2/nested/foo8
+ @@ -0,0 +1 @@
+ +foo8
+ diff --git a/sm2/nested/foo9 b/sm2/nested/foo9
+ new file mode 100644
+ index 0000000..9c3b4f6
+ --- /dev/null
+ +++ b/sm2/nested/foo9
+ @@ -0,0 +1 @@
+ +foo9
+ EOF
+ git diff --submodule=diff >actual 2>err &&
+ test_must_be_empty err &&
+ diff_cmp expected actual
+'
+
+(cd sm2; commit_file nested)
+commit_file sm2
+head12=$(cd sm2; git rev-parse --short --verify HEAD)
+
+mv sm2 sm2-bak
+
+test_expect_success 'diff --submodule=diff recurses into deleted nested submodules' '
+ cat >expected <<-EOF &&
+ Submodule sm1 $head7...0000000 (submodule deleted)
+ Submodule sm2 $head12...0000000 (submodule deleted)
+ diff --git a/sm2/.gitmodules b/sm2/.gitmodules
+ deleted file mode 100644
+ index 3a816b8..0000000
+ --- a/sm2/.gitmodules
+ +++ /dev/null
+ @@ -1,3 +0,0 @@
+ -[submodule "nested"]
+ - path = nested
+ - url = ../sm2
+ diff --git a/sm2/foo8 b/sm2/foo8
+ deleted file mode 100644
+ index db9916b..0000000
+ --- a/sm2/foo8
+ +++ /dev/null
+ @@ -1 +0,0 @@
+ -foo8
+ diff --git a/sm2/foo9 b/sm2/foo9
+ deleted file mode 100644
+ index 9c3b4f6..0000000
+ --- a/sm2/foo9
+ +++ /dev/null
+ @@ -1 +0,0 @@
+ -foo9
+ Submodule nested $head11...0000000 (submodule deleted)
+ diff --git a/sm2/nested/file b/sm2/nested/file
+ deleted file mode 100644
+ index ca281f5..0000000
+ --- a/sm2/nested/file
+ +++ /dev/null
+ @@ -1 +0,0 @@
+ -nested content
+ diff --git a/sm2/nested/foo8 b/sm2/nested/foo8
+ deleted file mode 100644
+ index db9916b..0000000
+ --- a/sm2/nested/foo8
+ +++ /dev/null
+ @@ -1 +0,0 @@
+ -foo8
+ diff --git a/sm2/nested/foo9 b/sm2/nested/foo9
+ deleted file mode 100644
+ index 9c3b4f6..0000000
+ --- a/sm2/nested/foo9
+ +++ /dev/null
+ @@ -1 +0,0 @@
+ -foo9
+ EOF
+ git diff --submodule=diff >actual 2>err &&
+ test_must_be_empty err &&
+ diff_cmp expected actual
+'
+
+mv sm2-bak sm2
+
+test_done
diff --git a/t/t4061-diff-indent.sh b/t/t4061-diff-indent.sh
new file mode 100755
index 0000000..7750b87
--- /dev/null
+++ b/t/t4061-diff-indent.sh
@@ -0,0 +1,371 @@
+#!/bin/sh
+
+test_description='Test diff indent heuristic.
+
+'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-diff.sh
+
+# Compare two diff outputs. Ignore "index" lines, because we don't
+# care about SHA-1s or file modes.
+compare_diff () {
+ sed -e "/^index /d" <"$1" >.tmp-1
+ sed -e "/^index /d" <"$2" >.tmp-2
+ test_cmp .tmp-1 .tmp-2 && rm -f .tmp-1 .tmp-2
+}
+
+# Compare blame output using the expectation for a diff as reference.
+# Only look for the lines coming from non-boundary commits.
+compare_blame () {
+ sed -n -e "1,4d" -e "s/^+//p" <"$1" >.tmp-1
+ sed -ne "s/^[^^][^)]*) *//p" <"$2" >.tmp-2
+ test_cmp .tmp-1 .tmp-2 && rm -f .tmp-1 .tmp-2
+}
+
+test_expect_success 'prepare' '
+ cat <<-\EOF >spaces.txt &&
+ 1
+ 2
+ a
+
+ b
+ 3
+ 4
+ EOF
+
+ cat <<-\EOF >functions.c &&
+ 1
+ 2
+ /* function */
+ foo() {
+ foo
+ }
+
+ 3
+ 4
+ EOF
+
+ git add spaces.txt functions.c &&
+ test_tick &&
+ git commit -m initial &&
+ git branch old &&
+
+ cat <<-\EOF >spaces.txt &&
+ 1
+ 2
+ a
+
+ b
+ a
+
+ b
+ 3
+ 4
+ EOF
+
+ cat <<-\EOF >functions.c &&
+ 1
+ 2
+ /* function */
+ bar() {
+ foo
+ }
+
+ /* function */
+ foo() {
+ foo
+ }
+
+ 3
+ 4
+ EOF
+
+ git add spaces.txt functions.c &&
+ test_tick &&
+ git commit -m initial &&
+ git branch new &&
+
+ tr "_" " " <<-\EOF >spaces-expect &&
+ diff --git a/spaces.txt b/spaces.txt
+ --- a/spaces.txt
+ +++ b/spaces.txt
+ @@ -3,5 +3,8 @@
+ a
+ _
+ b
+ +a
+ +
+ +b
+ 3
+ 4
+ EOF
+
+ tr "_" " " <<-\EOF >spaces-compacted-expect &&
+ diff --git a/spaces.txt b/spaces.txt
+ --- a/spaces.txt
+ +++ b/spaces.txt
+ @@ -2,6 +2,9 @@
+ 2
+ a
+ _
+ +b
+ +a
+ +
+ b
+ 3
+ 4
+ EOF
+
+ tr "_" " " <<-\EOF >functions-expect &&
+ diff --git a/functions.c b/functions.c
+ --- a/functions.c
+ +++ b/functions.c
+ @@ -1,6 +1,11 @@
+ 1
+ 2
+ /* function */
+ +bar() {
+ + foo
+ +}
+ +
+ +/* function */
+ foo() {
+ foo
+ }
+ EOF
+
+ tr "_" " " <<-\EOF >functions-compacted-expect
+ diff --git a/functions.c b/functions.c
+ --- a/functions.c
+ +++ b/functions.c
+ @@ -1,5 +1,10 @@
+ 1
+ 2
+ +/* function */
+ +bar() {
+ + foo
+ +}
+ +
+ /* function */
+ foo() {
+ foo
+ EOF
+'
+
+# --- diff tests ----------------------------------------------------------
+
+test_expect_success 'diff: ugly spaces' '
+ git diff --no-indent-heuristic old new -- spaces.txt >out &&
+ compare_diff spaces-expect out
+'
+
+test_expect_success 'diff: --no-indent-heuristic overrides config' '
+ git -c diff.indentHeuristic=true diff --no-indent-heuristic old new -- spaces.txt >out2 &&
+ compare_diff spaces-expect out2
+'
+
+test_expect_success 'diff: nice spaces with --indent-heuristic' '
+ git -c diff.indentHeuristic=false diff --indent-heuristic old new -- spaces.txt >out-compacted &&
+ compare_diff spaces-compacted-expect out-compacted
+'
+
+test_expect_success 'diff: nice spaces with diff.indentHeuristic=true' '
+ git -c diff.indentHeuristic=true diff old new -- spaces.txt >out-compacted2 &&
+ compare_diff spaces-compacted-expect out-compacted2
+'
+
+test_expect_success 'diff: --indent-heuristic with --patience' '
+ git diff --indent-heuristic --patience old new -- spaces.txt >out-compacted3 &&
+ compare_diff spaces-compacted-expect out-compacted3
+'
+
+test_expect_success 'diff: --indent-heuristic with --histogram' '
+ git diff --indent-heuristic --histogram old new -- spaces.txt >out-compacted4 &&
+ compare_diff spaces-compacted-expect out-compacted4
+'
+
+test_expect_success 'diff: ugly functions' '
+ git diff --no-indent-heuristic old new -- functions.c >out &&
+ compare_diff functions-expect out
+'
+
+test_expect_success 'diff: nice functions with --indent-heuristic' '
+ git diff --indent-heuristic old new -- functions.c >out-compacted &&
+ compare_diff functions-compacted-expect out-compacted
+'
+
+# --- blame tests ---------------------------------------------------------
+
+test_expect_success 'blame: nice spaces with --indent-heuristic' '
+ git blame --indent-heuristic old..new -- spaces.txt >out-blame-compacted &&
+ compare_blame spaces-compacted-expect out-blame-compacted
+'
+
+test_expect_success 'blame: nice spaces with diff.indentHeuristic=true' '
+ git -c diff.indentHeuristic=true blame old..new -- spaces.txt >out-blame-compacted2 &&
+ compare_blame spaces-compacted-expect out-blame-compacted2
+'
+
+test_expect_success 'blame: ugly spaces with --no-indent-heuristic' '
+ git blame --no-indent-heuristic old..new -- spaces.txt >out-blame &&
+ compare_blame spaces-expect out-blame
+'
+
+test_expect_success 'blame: ugly spaces with diff.indentHeuristic=false' '
+ git -c diff.indentHeuristic=false blame old..new -- spaces.txt >out-blame2 &&
+ compare_blame spaces-expect out-blame2
+'
+
+test_expect_success 'blame: --no-indent-heuristic overrides config' '
+ git -c diff.indentHeuristic=true blame --no-indent-heuristic old..new -- spaces.txt >out-blame3 &&
+ git blame old..new -- spaces.txt >out-blame &&
+ compare_blame spaces-expect out-blame3
+'
+
+test_expect_success 'blame: --indent-heuristic overrides config' '
+ git -c diff.indentHeuristic=false blame --indent-heuristic old..new -- spaces.txt >out-blame-compacted3 &&
+ compare_blame spaces-compacted-expect out-blame-compacted2
+'
+
+# --- diff-tree tests -----------------------------------------------------
+
+test_expect_success 'diff-tree: nice spaces with --indent-heuristic' '
+ git diff-tree --indent-heuristic -p old new -- spaces.txt >out-diff-tree-compacted &&
+ compare_diff spaces-compacted-expect out-diff-tree-compacted
+'
+
+test_expect_success 'diff-tree: nice spaces with diff.indentHeuristic=true' '
+ git -c diff.indentHeuristic=true diff-tree -p old new -- spaces.txt >out-diff-tree-compacted2 &&
+ compare_diff spaces-compacted-expect out-diff-tree-compacted2
+'
+
+test_expect_success 'diff-tree: ugly spaces with --no-indent-heuristic' '
+ git diff-tree --no-indent-heuristic -p old new -- spaces.txt >out-diff-tree &&
+ compare_diff spaces-expect out-diff-tree
+'
+
+test_expect_success 'diff-tree: ugly spaces with diff.indentHeuristic=false' '
+ git -c diff.indentHeuristic=false diff-tree -p old new -- spaces.txt >out-diff-tree2 &&
+ compare_diff spaces-expect out-diff-tree2
+'
+
+test_expect_success 'diff-tree: --indent-heuristic overrides config' '
+ git -c diff.indentHeuristic=false diff-tree --indent-heuristic -p old new -- spaces.txt >out-diff-tree-compacted3 &&
+ compare_diff spaces-compacted-expect out-diff-tree-compacted3
+'
+
+test_expect_success 'diff-tree: --no-indent-heuristic overrides config' '
+ git -c diff.indentHeuristic=true diff-tree --no-indent-heuristic -p old new -- spaces.txt >out-diff-tree3 &&
+ compare_diff spaces-expect out-diff-tree3
+'
+
+# --- diff-index tests ----------------------------------------------------
+
+test_expect_success 'diff-index: nice spaces with --indent-heuristic' '
+ git checkout -B diff-index &&
+ git reset --soft HEAD~ &&
+ git diff-index --indent-heuristic -p old -- spaces.txt >out-diff-index-compacted &&
+ compare_diff spaces-compacted-expect out-diff-index-compacted &&
+ git checkout -f main
+'
+
+test_expect_success 'diff-index: nice spaces with diff.indentHeuristic=true' '
+ git checkout -B diff-index &&
+ git reset --soft HEAD~ &&
+ git -c diff.indentHeuristic=true diff-index -p old -- spaces.txt >out-diff-index-compacted2 &&
+ compare_diff spaces-compacted-expect out-diff-index-compacted2 &&
+ git checkout -f main
+'
+
+test_expect_success 'diff-index: ugly spaces with --no-indent-heuristic' '
+ git checkout -B diff-index &&
+ git reset --soft HEAD~ &&
+ git diff-index --no-indent-heuristic -p old -- spaces.txt >out-diff-index &&
+ compare_diff spaces-expect out-diff-index &&
+ git checkout -f main
+'
+
+test_expect_success 'diff-index: ugly spaces with diff.indentHeuristic=false' '
+ git checkout -B diff-index &&
+ git reset --soft HEAD~ &&
+ git -c diff.indentHeuristic=false diff-index -p old -- spaces.txt >out-diff-index2 &&
+ compare_diff spaces-expect out-diff-index2 &&
+ git checkout -f main
+'
+
+test_expect_success 'diff-index: --indent-heuristic overrides config' '
+ git checkout -B diff-index &&
+ git reset --soft HEAD~ &&
+ git -c diff.indentHeuristic=false diff-index --indent-heuristic -p old -- spaces.txt >out-diff-index-compacted3 &&
+ compare_diff spaces-compacted-expect out-diff-index-compacted3 &&
+ git checkout -f main
+'
+
+test_expect_success 'diff-index: --no-indent-heuristic overrides config' '
+ git checkout -B diff-index &&
+ git reset --soft HEAD~ &&
+ git -c diff.indentHeuristic=true diff-index --no-indent-heuristic -p old -- spaces.txt >out-diff-index3 &&
+ compare_diff spaces-expect out-diff-index3 &&
+ git checkout -f main
+'
+
+# --- diff-files tests ----------------------------------------------------
+
+test_expect_success 'diff-files: nice spaces with --indent-heuristic' '
+ git checkout -B diff-files &&
+ git reset HEAD~ &&
+ git diff-files --indent-heuristic -p spaces.txt >out-diff-files-raw &&
+ grep -v index out-diff-files-raw >out-diff-files-compacted &&
+ compare_diff spaces-compacted-expect out-diff-files-compacted &&
+ git checkout -f main
+'
+
+test_expect_success 'diff-files: nice spaces with diff.indentHeuristic=true' '
+ git checkout -B diff-files &&
+ git reset HEAD~ &&
+ git -c diff.indentHeuristic=true diff-files -p spaces.txt >out-diff-files-raw2 &&
+ grep -v index out-diff-files-raw2 >out-diff-files-compacted2 &&
+ compare_diff spaces-compacted-expect out-diff-files-compacted2 &&
+ git checkout -f main
+'
+
+test_expect_success 'diff-files: ugly spaces with --no-indent-heuristic' '
+ git checkout -B diff-files &&
+ git reset HEAD~ &&
+ git diff-files --no-indent-heuristic -p spaces.txt >out-diff-files-raw &&
+ grep -v index out-diff-files-raw >out-diff-files &&
+ compare_diff spaces-expect out-diff-files &&
+ git checkout -f main
+'
+
+test_expect_success 'diff-files: ugly spaces with diff.indentHeuristic=false' '
+ git checkout -B diff-files &&
+ git reset HEAD~ &&
+ git -c diff.indentHeuristic=false diff-files -p spaces.txt >out-diff-files-raw2 &&
+ grep -v index out-diff-files-raw2 >out-diff-files &&
+ compare_diff spaces-expect out-diff-files &&
+ git checkout -f main
+'
+
+test_expect_success 'diff-files: --indent-heuristic overrides config' '
+ git checkout -B diff-files &&
+ git reset HEAD~ &&
+ git -c diff.indentHeuristic=false diff-files --indent-heuristic -p spaces.txt >out-diff-files-raw3 &&
+ grep -v index out-diff-files-raw3 >out-diff-files-compacted &&
+ compare_diff spaces-compacted-expect out-diff-files-compacted &&
+ git checkout -f main
+'
+
+test_expect_success 'diff-files: --no-indent-heuristic overrides config' '
+ git checkout -B diff-files &&
+ git reset HEAD~ &&
+ git -c diff.indentHeuristic=true diff-files --no-indent-heuristic -p spaces.txt >out-diff-files-raw4 &&
+ grep -v index out-diff-files-raw4 >out-diff-files &&
+ compare_diff spaces-expect out-diff-files &&
+ git checkout -f main
+'
+
+test_done
diff --git a/t/t4062-diff-pickaxe.sh b/t/t4062-diff-pickaxe.sh
new file mode 100755
index 0000000..a90b46b
--- /dev/null
+++ b/t/t4062-diff-pickaxe.sh
@@ -0,0 +1,30 @@
+#!/bin/sh
+#
+# Copyright (c) 2016 Johannes Schindelin
+#
+
+test_description='Pickaxe options'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ test_commit initial &&
+ printf "%04096d" 0 >4096-zeroes.txt &&
+ git add 4096-zeroes.txt &&
+ test_tick &&
+ git commit -m "A 4k file"
+'
+
+# OpenBSD only supports up to 255 repetitions, so repeat twice for 64*64=4096.
+test_expect_success '-G matches' '
+ git diff --name-only -G "^(0{64}){64}$" HEAD^ >out &&
+ test 4096-zeroes.txt = "$(cat out)"
+'
+
+test_expect_success '-S --pickaxe-regex' '
+ git diff --name-only -S0 --pickaxe-regex HEAD^ >out &&
+ test 4096-zeroes.txt = "$(cat out)"
+'
+
+test_done
diff --git a/t/t4063-diff-blobs.sh b/t/t4063-diff-blobs.sh
new file mode 100755
index 0000000..7e6c9d6
--- /dev/null
+++ b/t/t4063-diff-blobs.sh
@@ -0,0 +1,98 @@
+#!/bin/sh
+
+test_description='test direct comparison of blobs via git-diff'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+run_diff () {
+ # use full-index to make it easy to match the index line
+ git diff --full-index "$@" >diff
+}
+
+check_index () {
+ grep "^index $1\\.\\.$2" diff
+}
+
+check_mode () {
+ grep "^old mode $1" diff &&
+ grep "^new mode $2" diff
+}
+
+check_paths () {
+ grep "^diff --git a/$1 b/$2" diff
+}
+
+test_expect_success 'create some blobs' '
+ echo one >one &&
+ echo two >two &&
+ chmod +x two &&
+ git add . &&
+
+ # cover systems where modes are ignored
+ git update-index --chmod=+x two &&
+
+ git commit -m base &&
+
+ sha1_one=$(git rev-parse HEAD:one) &&
+ sha1_two=$(git rev-parse HEAD:two)
+'
+
+test_expect_success 'diff by sha1' '
+ run_diff $sha1_one $sha1_two
+'
+test_expect_success 'index of sha1 diff' '
+ check_index $sha1_one $sha1_two
+'
+test_expect_success 'sha1 diff uses arguments as paths' '
+ check_paths $sha1_one $sha1_two
+'
+test_expect_success 'sha1 diff has no mode change' '
+ ! grep mode diff
+'
+
+test_expect_success 'diff by tree:path (run)' '
+ run_diff HEAD:one HEAD:two
+'
+test_expect_success 'index of tree:path diff' '
+ check_index $sha1_one $sha1_two
+'
+test_expect_success 'tree:path diff uses filenames as paths' '
+ check_paths one two
+'
+test_expect_success 'tree:path diff shows mode change' '
+ check_mode 100644 100755
+'
+
+test_expect_success 'diff by ranged tree:path' '
+ run_diff HEAD:one..HEAD:two
+'
+test_expect_success 'index of ranged tree:path diff' '
+ check_index $sha1_one $sha1_two
+'
+test_expect_success 'ranged tree:path diff uses filenames as paths' '
+ check_paths one two
+'
+test_expect_success 'ranged tree:path diff shows mode change' '
+ check_mode 100644 100755
+'
+
+test_expect_success 'diff blob against file' '
+ run_diff HEAD:one two
+'
+test_expect_success 'index of blob-file diff' '
+ check_index $sha1_one $sha1_two
+'
+test_expect_success 'blob-file diff uses filename as paths' '
+ check_paths one two
+'
+test_expect_success FILEMODE 'blob-file diff shows mode change' '
+ check_mode 100644 100755
+'
+
+test_expect_success 'blob-file diff prefers filename to sha1' '
+ run_diff $sha1_one two &&
+ check_paths two two
+'
+
+test_done
diff --git a/t/t4064-diff-oidfind.sh b/t/t4064-diff-oidfind.sh
new file mode 100755
index 0000000..6d8c898
--- /dev/null
+++ b/t/t4064-diff-oidfind.sh
@@ -0,0 +1,123 @@
+#!/bin/sh
+
+test_description='test finding specific blobs in the revision walking'
+. ./test-lib.sh
+
+test_expect_success 'setup ' '
+ git commit --allow-empty -m "empty initial commit" &&
+
+ echo "Hello, world!" >greeting &&
+ git add greeting &&
+ git commit -m "add the greeting blob" && # borrowed from Git from the Bottom Up
+ git tag -m "the blob" greeting $(git rev-parse HEAD:greeting) &&
+
+ echo asdf >unrelated &&
+ git add unrelated &&
+ git commit -m "unrelated history" &&
+
+ git revert HEAD^ &&
+
+ git commit --allow-empty -m "another unrelated commit"
+'
+
+test_expect_success 'find the greeting blob' '
+ cat >expect <<-EOF &&
+ Revert "add the greeting blob"
+ add the greeting blob
+ EOF
+
+ git log --format=%s --find-object=greeting^{blob} >actual &&
+
+ test_cmp expect actual
+'
+
+test_expect_success 'setup a tree' '
+ mkdir a &&
+ echo asdf >a/file &&
+ git add a/file &&
+ git commit -m "add a file in a subdirectory"
+'
+
+test_expect_success 'find a tree' '
+ cat >expect <<-EOF &&
+ add a file in a subdirectory
+ EOF
+
+ git log --format=%s -t --find-object=HEAD:a >actual &&
+
+ test_cmp expect actual
+'
+
+test_expect_success 'setup a submodule' '
+ test_create_repo sub &&
+ test_commit -C sub sub &&
+ git submodule add ./sub sub &&
+ git commit -a -m "add sub"
+'
+
+test_expect_success 'find a submodule' '
+ cat >expect <<-EOF &&
+ add sub
+ EOF
+
+ git log --format=%s --find-object=HEAD:sub >actual &&
+
+ test_cmp expect actual
+'
+
+test_expect_success 'set up merge tests' '
+ test_commit base &&
+
+ git checkout -b boring base^ &&
+ echo boring >file &&
+ git add file &&
+ git commit -m boring &&
+
+ git checkout -b interesting base^ &&
+ echo interesting >file &&
+ git add file &&
+ git commit -m interesting &&
+
+ blob=$(git rev-parse interesting:file)
+'
+
+test_expect_success 'detect merge which introduces blob' '
+ git checkout -B merge base &&
+ git merge --no-commit boring &&
+ echo interesting >file &&
+ git commit -am "introduce blob" &&
+ git diff-tree --format=%s --find-object=$blob -c --name-status HEAD >actual &&
+ cat >expect <<-\EOF &&
+ introduce blob
+
+ AM file
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'detect merge which removes blob' '
+ git checkout -B merge interesting &&
+ git merge --no-commit base &&
+ echo boring >file &&
+ git commit -am "remove blob" &&
+ git diff-tree --format=%s --find-object=$blob -c --name-status HEAD >actual &&
+ cat >expect <<-\EOF &&
+ remove blob
+
+ MA file
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'do not detect merge that does not touch blob' '
+ git checkout -B merge interesting &&
+ git merge -m "untouched blob" base &&
+ git diff-tree --format=%s --find-object=$blob -c --name-status HEAD >actual &&
+ cat >expect <<-\EOF &&
+ untouched blob
+
+ EOF
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t4065-diff-anchored.sh b/t/t4065-diff-anchored.sh
new file mode 100755
index 0000000..b3f510f
--- /dev/null
+++ b/t/t4065-diff-anchored.sh
@@ -0,0 +1,94 @@
+#!/bin/sh
+
+test_description='anchored diff algorithm'
+
+. ./test-lib.sh
+
+test_expect_success '--anchored' '
+ printf "a\nb\nc\n" >pre &&
+ printf "c\na\nb\n" >post &&
+
+ # normally, c is moved to produce the smallest diff
+ test_expect_code 1 git diff --no-index pre post >diff &&
+ grep "^+c" diff &&
+
+ # with anchor, a is moved
+ test_expect_code 1 git diff --no-index --anchored=c pre post >diff &&
+ grep "^+a" diff
+'
+
+test_expect_success '--anchored multiple' '
+ printf "a\nb\nc\nd\ne\nf\n" >pre &&
+ printf "c\na\nb\nf\nd\ne\n" >post &&
+
+ # with 1 anchor, c is not moved, but f is moved
+ test_expect_code 1 git diff --no-index --anchored=c pre post >diff &&
+ grep "^+a" diff && # a is moved instead of c
+ grep "^+f" diff &&
+
+ # with 2 anchors, c and f are not moved
+ test_expect_code 1 git diff --no-index --anchored=c --anchored=f pre post >diff &&
+ grep "^+a" diff &&
+ grep "^+d" diff # d is moved instead of f
+'
+
+test_expect_success '--anchored with nonexistent line has no effect' '
+ printf "a\nb\nc\n" >pre &&
+ printf "c\na\nb\n" >post &&
+
+ test_expect_code 1 git diff --no-index --anchored=x pre post >diff &&
+ grep "^+c" diff
+'
+
+test_expect_success '--anchored with non-unique line has no effect' '
+ printf "a\nb\nc\nd\ne\nc\n" >pre &&
+ printf "c\na\nb\nc\nd\ne\n" >post &&
+
+ test_expect_code 1 git diff --no-index --anchored=c pre post >diff &&
+ grep "^+c" diff
+'
+
+test_expect_success 'diff still produced with impossible multiple --anchored' '
+ printf "a\nb\nc\n" >pre &&
+ printf "c\na\nb\n" >post &&
+
+ test_expect_code 1 git diff --no-index --anchored=a --anchored=c pre post >diff &&
+ mv post expected_post &&
+
+ # Ensure that the diff is correct by applying it and then
+ # comparing the result with the original
+ git apply diff &&
+ diff expected_post post
+'
+
+test_expect_success 'later algorithm arguments override earlier ones' '
+ printf "a\nb\nc\n" >pre &&
+ printf "c\na\nb\n" >post &&
+
+ test_expect_code 1 git diff --no-index --patience --anchored=c pre post >diff &&
+ grep "^+a" diff &&
+
+ test_expect_code 1 git diff --no-index --anchored=c --patience pre post >diff &&
+ grep "^+c" diff &&
+
+ test_expect_code 1 git diff --no-index --histogram --anchored=c pre post >diff &&
+ grep "^+a" diff &&
+
+ test_expect_code 1 git diff --no-index --anchored=c --histogram pre post >diff &&
+ grep "^+c" diff
+'
+
+test_expect_success '--anchored works with other commands like "git show"' '
+ printf "a\nb\nc\n" >file &&
+ git add file &&
+ git commit -m foo &&
+ printf "c\na\nb\n" >file &&
+ git add file &&
+ git commit -m foo &&
+
+ # with anchor, a is moved
+ git show --patience --anchored=c >diff &&
+ grep "^+a" diff
+'
+
+test_done
diff --git a/t/t4066-diff-emit-delay.sh b/t/t4066-diff-emit-delay.sh
new file mode 100755
index 0000000..0ecb391
--- /dev/null
+++ b/t/t4066-diff-emit-delay.sh
@@ -0,0 +1,83 @@
+#!/bin/sh
+
+test_description='test combined/stat/moved interaction'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# This test covers a weird 3-way interaction between "--cc -p", which will run
+# the combined diff code, along with "--stat", which will be computed as a
+# first-parent stat during the combined diff, and "--color-moved", which
+# enables the emitted_symbols list to store the diff in memory.
+
+test_expect_success 'set up history with a merge' '
+ test_commit A &&
+ test_commit B &&
+ git checkout -b side HEAD^ &&
+ test_commit C &&
+ git merge -m M main &&
+ test_commit D
+'
+
+test_expect_success 'log --cc -p --stat --color-moved' '
+ cat >expect <<-EOF &&
+ commit D
+ ---
+ D.t | 1 +
+ 1 file changed, 1 insertion(+)
+
+ diff --git a/D.t b/D.t
+ new file mode 100644
+ index 0000000..$(git rev-parse --short D:D.t)
+ --- /dev/null
+ +++ b/D.t
+ @@ -0,0 +1 @@
+ +D
+ commit M
+
+ B.t | 1 +
+ 1 file changed, 1 insertion(+)
+ commit C
+ ---
+ C.t | 1 +
+ 1 file changed, 1 insertion(+)
+
+ diff --git a/C.t b/C.t
+ new file mode 100644
+ index 0000000..$(git rev-parse --short C:C.t)
+ --- /dev/null
+ +++ b/C.t
+ @@ -0,0 +1 @@
+ +C
+ commit B
+ ---
+ B.t | 1 +
+ 1 file changed, 1 insertion(+)
+
+ diff --git a/B.t b/B.t
+ new file mode 100644
+ index 0000000..$(git rev-parse --short B:B.t)
+ --- /dev/null
+ +++ b/B.t
+ @@ -0,0 +1 @@
+ +B
+ commit A
+ ---
+ A.t | 1 +
+ 1 file changed, 1 insertion(+)
+
+ diff --git a/A.t b/A.t
+ new file mode 100644
+ index 0000000..$(git rev-parse --short A:A.t)
+ --- /dev/null
+ +++ b/A.t
+ @@ -0,0 +1 @@
+ +A
+ EOF
+ git log --format="commit %s" --cc -p --stat --color-moved >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t4067-diff-partial-clone.sh b/t/t4067-diff-partial-clone.sh
new file mode 100755
index 0000000..7af3a08
--- /dev/null
+++ b/t/t4067-diff-partial-clone.sh
@@ -0,0 +1,184 @@
+#!/bin/sh
+
+test_description='behavior of diff when reading objects in a partial clone'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'git show batches blobs' '
+ test_when_finished "rm -rf server client trace" &&
+
+ test_create_repo server &&
+ echo a >server/a &&
+ echo b >server/b &&
+ git -C server add a b &&
+ git -C server commit -m x &&
+
+ test_config -C server uploadpack.allowfilter 1 &&
+ test_config -C server uploadpack.allowanysha1inwant 1 &&
+ git clone --bare --filter=blob:limit=0 "file://$(pwd)/server" client &&
+
+ # Ensure that there is exactly 1 negotiation by checking that there is
+ # only 1 "done" line sent. ("done" marks the end of negotiation.)
+ GIT_TRACE_PACKET="$(pwd)/trace" git -C client show HEAD &&
+ grep "fetch> done" trace >done_lines &&
+ test_line_count = 1 done_lines
+'
+
+test_expect_success 'diff batches blobs' '
+ test_when_finished "rm -rf server client trace" &&
+
+ test_create_repo server &&
+ echo a >server/a &&
+ echo b >server/b &&
+ git -C server add a b &&
+ git -C server commit -m x &&
+ echo c >server/c &&
+ echo d >server/d &&
+ git -C server add c d &&
+ git -C server commit -m x &&
+
+ test_config -C server uploadpack.allowfilter 1 &&
+ test_config -C server uploadpack.allowanysha1inwant 1 &&
+ git clone --bare --filter=blob:limit=0 "file://$(pwd)/server" client &&
+
+ # Ensure that there is exactly 1 negotiation by checking that there is
+ # only 1 "done" line sent. ("done" marks the end of negotiation.)
+ GIT_TRACE_PACKET="$(pwd)/trace" git -C client diff HEAD^ HEAD &&
+ grep "fetch> done" trace >done_lines &&
+ test_line_count = 1 done_lines
+'
+
+test_expect_success 'diff skips same-OID blobs' '
+ test_when_finished "rm -rf server client trace" &&
+
+ test_create_repo server &&
+ echo a >server/a &&
+ echo b >server/b &&
+ git -C server add a b &&
+ git -C server commit -m x &&
+ echo another-a >server/a &&
+ git -C server add a &&
+ git -C server commit -m x &&
+
+ test_config -C server uploadpack.allowfilter 1 &&
+ test_config -C server uploadpack.allowanysha1inwant 1 &&
+ git clone --bare --filter=blob:limit=0 "file://$(pwd)/server" client &&
+
+ echo a | git hash-object --stdin >hash-old-a &&
+ echo another-a | git hash-object --stdin >hash-new-a &&
+ echo b | git hash-object --stdin >hash-b &&
+
+ # Ensure that only a and another-a are fetched.
+ GIT_TRACE_PACKET="$(pwd)/trace" git -C client diff HEAD^ HEAD &&
+ grep "want $(cat hash-old-a)" trace &&
+ grep "want $(cat hash-new-a)" trace &&
+ ! grep "want $(cat hash-b)" trace
+'
+
+test_expect_success 'when fetching missing objects, diff skips GITLINKs' '
+ test_when_finished "rm -rf sub server client trace" &&
+ test_config_global protocol.file.allow always &&
+
+ test_create_repo sub &&
+ test_commit -C sub first &&
+
+ test_create_repo server &&
+ echo a >server/a &&
+ git -C server add a &&
+ git -C server submodule add "file://$(pwd)/sub" &&
+ git -C server commit -m x &&
+
+ test_commit -C server/sub second &&
+ echo another-a >server/a &&
+ git -C server add a sub &&
+ git -C server commit -m x &&
+
+ test_config -C server uploadpack.allowfilter 1 &&
+ test_config -C server uploadpack.allowanysha1inwant 1 &&
+ git clone --bare --filter=blob:limit=0 "file://$(pwd)/server" client &&
+
+ echo a | git hash-object --stdin >hash-old-a &&
+ echo another-a | git hash-object --stdin >hash-new-a &&
+
+ # Ensure that a and another-a are fetched, and check (by successful
+ # execution of the diff) that no invalid OIDs are sent.
+ GIT_TRACE_PACKET="$(pwd)/trace" git -C client diff HEAD^ HEAD &&
+ grep "want $(cat hash-old-a)" trace &&
+ grep "want $(cat hash-new-a)" trace
+'
+
+test_expect_success 'diff with rename detection batches blobs' '
+ test_when_finished "rm -rf server client trace" &&
+
+ test_create_repo server &&
+ echo a >server/a &&
+ printf "b\nb\nb\nb\nb\n" >server/b &&
+ git -C server add a b &&
+ git -C server commit -m x &&
+ rm server/b &&
+ printf "b\nb\nb\nb\nbX\n" >server/c &&
+ git -C server add c &&
+ git -C server commit -a -m x &&
+
+ test_config -C server uploadpack.allowfilter 1 &&
+ test_config -C server uploadpack.allowanysha1inwant 1 &&
+ git clone --bare --filter=blob:limit=0 "file://$(pwd)/server" client &&
+
+ # Ensure that there is exactly 1 negotiation by checking that there is
+ # only 1 "done" line sent. ("done" marks the end of negotiation.)
+ GIT_TRACE_PACKET="$(pwd)/trace" git -C client diff --raw -M HEAD^ HEAD >out &&
+ grep ":100644 100644.*R[0-9][0-9][0-9].*b.*c" out &&
+ grep "fetch> done" trace >done_lines &&
+ test_line_count = 1 done_lines
+'
+
+test_expect_success 'diff does not fetch anything if inexact rename detection is not needed' '
+ test_when_finished "rm -rf server client trace" &&
+
+ test_create_repo server &&
+ echo a >server/a &&
+ printf "b\nb\nb\nb\nb\n" >server/b &&
+ git -C server add a b &&
+ git -C server commit -m x &&
+ mv server/b server/c &&
+ git -C server add c &&
+ git -C server commit -a -m x &&
+
+ test_config -C server uploadpack.allowfilter 1 &&
+ test_config -C server uploadpack.allowanysha1inwant 1 &&
+ git clone --bare --filter=blob:limit=0 "file://$(pwd)/server" client &&
+
+ # Ensure no fetches.
+ GIT_TRACE_PACKET="$(pwd)/trace" git -C client diff --raw -M HEAD^ HEAD &&
+ test_path_is_missing trace
+'
+
+test_expect_success 'diff --break-rewrites fetches only if necessary, and batches blobs if it does' '
+ test_when_finished "rm -rf server client trace" &&
+
+ test_create_repo server &&
+ echo a >server/a &&
+ printf "b\nb\nb\nb\nb\n" >server/b &&
+ git -C server add a b &&
+ git -C server commit -m x &&
+ printf "c\nc\nc\nc\nc\n" >server/b &&
+ git -C server commit -a -m x &&
+
+ test_config -C server uploadpack.allowfilter 1 &&
+ test_config -C server uploadpack.allowanysha1inwant 1 &&
+ git clone --bare --filter=blob:limit=0 "file://$(pwd)/server" client &&
+
+ # Ensure no fetches.
+ GIT_TRACE_PACKET="$(pwd)/trace" git -C client diff --raw -M HEAD^ HEAD &&
+ test_path_is_missing trace &&
+
+ # But with --break-rewrites, ensure that there is exactly 1 negotiation
+ # by checking that there is only 1 "done" line sent. ("done" marks the
+ # end of negotiation.)
+ GIT_TRACE_PACKET="$(pwd)/trace" git -C client diff --break-rewrites --raw -M HEAD^ HEAD &&
+ grep "fetch> done" trace >done_lines &&
+ test_line_count = 1 done_lines
+'
+
+test_done
diff --git a/t/t4068-diff-symmetric-merge-base.sh b/t/t4068-diff-symmetric-merge-base.sh
new file mode 100755
index 0000000..eff63c1
--- /dev/null
+++ b/t/t4068-diff-symmetric-merge-base.sh
@@ -0,0 +1,203 @@
+#!/bin/sh
+
+test_description='behavior of diff with symmetric-diff setups and --merge-base'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+# build these situations:
+# - normal merge with one merge base (br1...b2r);
+# - criss-cross merge ie 2 merge bases (br1...main);
+# - disjoint subgraph (orphan branch, br3...main).
+#
+# B---E <-- main
+# / \ /
+# A X
+# \ / \
+# C---D--G <-- br1
+# \ /
+# ---F <-- br2
+#
+# H <-- br3
+#
+# We put files into a few commits so that we can verify the
+# output as well.
+
+test_expect_success setup '
+ git commit --allow-empty -m A &&
+ echo b >b &&
+ git add b &&
+ git commit -m B &&
+ git checkout -b br1 HEAD^ &&
+ echo c >c &&
+ git add c &&
+ git commit -m C &&
+ git tag -m commit-C commit-C &&
+ git merge -m D main &&
+ git tag commit-D &&
+ git checkout main &&
+ git merge -m E commit-C &&
+ git checkout -b br2 commit-C &&
+ echo f >f &&
+ git add f &&
+ git commit -m F &&
+ git checkout br1 &&
+ git merge -m G br2 &&
+ git checkout --orphan br3 &&
+ git commit -m H
+'
+
+test_expect_success 'diff with one merge base' '
+ git diff commit-D...br1 >tmp &&
+ tail -n 1 tmp >actual &&
+ echo +f >expect &&
+ test_cmp expect actual
+'
+
+# The output (in tmp) can have +b or +c depending
+# on which merge base (commit B or C) is picked.
+# It should have one of those two, which comes out
+# to seven lines.
+test_expect_success 'diff with two merge bases' '
+ git diff br1...main >tmp 2>err &&
+ test_line_count = 7 tmp &&
+ test_line_count = 1 err
+'
+
+test_expect_success 'diff with no merge bases' '
+ test_must_fail git diff br2...br3 2>err &&
+ test_grep "fatal: br2...br3: no merge base" err
+'
+
+test_expect_success 'diff with too many symmetric differences' '
+ test_must_fail git diff br1...main br2...br3 2>err &&
+ test_grep "usage" err
+'
+
+test_expect_success 'diff with symmetric difference and extraneous arg' '
+ test_must_fail git diff main br1...main 2>err &&
+ test_grep "usage" err
+'
+
+test_expect_success 'diff with two ranges' '
+ test_must_fail git diff main br1..main br2..br3 2>err &&
+ test_grep "usage" err
+'
+
+test_expect_success 'diff with ranges and extra arg' '
+ test_must_fail git diff main br1..main commit-D 2>err &&
+ test_grep "usage" err
+'
+
+test_expect_success 'diff --merge-base with no commits' '
+ test_must_fail git diff --merge-base
+'
+
+test_expect_success 'diff --merge-base with three commits' '
+ test_must_fail git diff --merge-base br1 br2 main 2>err &&
+ test_grep "usage" err
+'
+
+for cmd in diff-index diff
+do
+ test_expect_success "$cmd --merge-base with one commit" '
+ git checkout main &&
+ git $cmd commit-C >expect &&
+ git $cmd --merge-base br2 >actual &&
+ test_cmp expect actual
+ '
+
+ test_expect_success "$cmd --merge-base with annotated tag" '
+ git checkout main &&
+ git $cmd commit-C >expect &&
+ git $cmd --merge-base commit-C >actual &&
+ test_cmp expect actual
+ '
+
+ test_expect_success "$cmd --merge-base with one commit and unstaged changes" '
+ git checkout main &&
+ test_when_finished git reset --hard &&
+ echo unstaged >>c &&
+ git $cmd commit-C >expect &&
+ git $cmd --merge-base br2 >actual &&
+ test_cmp expect actual
+ '
+
+ test_expect_success "$cmd --merge-base with one commit and staged and unstaged changes" '
+ git checkout main &&
+ test_when_finished git reset --hard &&
+ echo staged >>c &&
+ git add c &&
+ echo unstaged >>c &&
+ git $cmd commit-C >expect &&
+ git $cmd --merge-base br2 >actual &&
+ test_cmp expect actual
+ '
+
+ test_expect_success "$cmd --merge-base --cached with one commit and staged and unstaged changes" '
+ git checkout main &&
+ test_when_finished git reset --hard &&
+ echo staged >>c &&
+ git add c &&
+ echo unstaged >>c &&
+ git $cmd --cached commit-C >expect &&
+ git $cmd --cached --merge-base br2 >actual &&
+ test_cmp expect actual
+ '
+
+ test_expect_success "$cmd --merge-base with non-commit" '
+ git checkout main &&
+ test_must_fail git $cmd --merge-base main^{tree} 2>err &&
+ test_grep "is a tree, not a commit" err
+ '
+
+ test_expect_success "$cmd --merge-base with no merge bases and one commit" '
+ git checkout main &&
+ test_must_fail git $cmd --merge-base br3 2>err &&
+ test_grep "fatal: no merge base found" err
+ '
+
+ test_expect_success "$cmd --merge-base with multiple merge bases and one commit" '
+ git checkout main &&
+ test_must_fail git $cmd --merge-base br1 2>err &&
+ test_grep "fatal: multiple merge bases found" err
+ '
+done
+
+for cmd in diff-tree diff
+do
+ test_expect_success "$cmd --merge-base with two commits" '
+ git $cmd commit-C main >expect &&
+ git $cmd --merge-base br2 main >actual &&
+ test_cmp expect actual
+ '
+
+ test_expect_success "$cmd --merge-base commit and non-commit" '
+ test_must_fail git $cmd --merge-base br2 main^{tree} 2>err &&
+ test_grep "is a tree, not a commit" err
+ '
+
+ test_expect_success "$cmd --merge-base with no merge bases and two commits" '
+ test_must_fail git $cmd --merge-base br2 br3 2>err &&
+ test_grep "fatal: no merge base found" err
+ '
+
+ test_expect_success "$cmd --merge-base with multiple merge bases and two commits" '
+ test_must_fail git $cmd --merge-base main br1 2>err &&
+ test_grep "fatal: multiple merge bases found" err
+ '
+done
+
+test_expect_success 'diff-tree --merge-base with one commit' '
+ test_must_fail git diff-tree --merge-base main 2>err &&
+ test_grep "fatal: --merge-base only works with two commits" err
+'
+
+test_expect_success 'diff --merge-base with range' '
+ test_must_fail git diff --merge-base br2..br3 2>err &&
+ test_grep "fatal: --merge-base does not work with ranges" err
+'
+
+test_done
diff --git a/t/t4069-remerge-diff.sh b/t/t4069-remerge-diff.sh
new file mode 100755
index 0000000..07323eb
--- /dev/null
+++ b/t/t4069-remerge-diff.sh
@@ -0,0 +1,320 @@
+#!/bin/sh
+
+test_description='remerge-diff handling'
+
+. ./test-lib.sh
+
+# This test is ort-specific
+if test "${GIT_TEST_MERGE_ALGORITHM}" != ort
+then
+ skip_all="GIT_TEST_MERGE_ALGORITHM != ort"
+ test_done
+fi
+
+test_expect_success 'setup basic merges' '
+ test_write_lines 1 2 3 4 5 6 7 8 9 >numbers &&
+ git add numbers &&
+ git commit -m base &&
+
+ git branch feature_a &&
+ git branch feature_b &&
+ git branch feature_c &&
+
+ git branch ab_resolution &&
+ git branch bc_resolution &&
+
+ git checkout feature_a &&
+ test_write_lines 1 2 three 4 5 6 7 eight 9 >numbers &&
+ git commit -a -m change_a &&
+
+ git checkout feature_b &&
+ test_write_lines 1 2 tres 4 5 6 7 8 9 >numbers &&
+ git commit -a -m change_b &&
+
+ git checkout feature_c &&
+ test_write_lines 1 2 3 4 5 6 7 8 9 10 >numbers &&
+ git commit -a -m change_c &&
+
+ git checkout bc_resolution &&
+ git merge --ff-only feature_b &&
+ # no conflict
+ git merge feature_c &&
+
+ git checkout ab_resolution &&
+ git merge --ff-only feature_a &&
+ # conflicts!
+ test_must_fail git merge feature_b &&
+ # Resolve conflict...and make another change elsewhere
+ test_write_lines 1 2 drei 4 5 6 7 acht 9 >numbers &&
+ git add numbers &&
+ git merge --continue
+'
+
+test_expect_success 'remerge-diff on a clean merge' '
+ git log -1 --oneline bc_resolution >expect &&
+ git show --oneline --remerge-diff bc_resolution >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'remerge-diff on a clean merge with a filter' '
+ git show --oneline --remerge-diff --diff-filter=U bc_resolution >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'remerge-diff with both a resolved conflict and an unrelated change' '
+ git log -1 --oneline ab_resolution >tmp &&
+ cat <<-EOF >>tmp &&
+ diff --git a/numbers b/numbers
+ remerge CONFLICT (content): Merge conflict in numbers
+ index a1fb731..6875544 100644
+ --- a/numbers
+ +++ b/numbers
+ @@ -1,13 +1,9 @@
+ 1
+ 2
+ -<<<<<<< b0ed5cb (change_a)
+ -three
+ -=======
+ -tres
+ ->>>>>>> 6cd3f82 (change_b)
+ +drei
+ 4
+ 5
+ 6
+ 7
+ -eight
+ +acht
+ 9
+ EOF
+ # Hashes above are sha1; rip them out so test works with sha256
+ sed -e "s/[0-9a-f]\{7,\}/HASH/g" tmp >expect &&
+
+ git show --oneline --remerge-diff ab_resolution >tmp &&
+ sed -e "s/[0-9a-f]\{7,\}/HASH/g" tmp >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'pickaxe still includes additional headers for relevant changes' '
+ # reuses "expect" from the previous testcase
+
+ git log --oneline --remerge-diff -Sacht ab_resolution >tmp &&
+ sed -e "s/[0-9a-f]\{7,\}/HASH/g" tmp >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'can filter out additional headers with pickaxe' '
+ git show --remerge-diff --submodule=log --find-object=HEAD ab_resolution >actual &&
+ test_must_be_empty actual &&
+
+ git show --remerge-diff -S"not present" --all >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'setup non-content conflicts' '
+ git switch --orphan base &&
+
+ test_write_lines 1 2 3 4 5 6 7 8 9 >numbers &&
+ test_write_lines a b c d e f g h i >letters &&
+ test_write_lines in the way >content &&
+ git add numbers letters content &&
+ git commit -m base &&
+
+ git branch side1 &&
+ git branch side2 &&
+
+ git checkout side1 &&
+ test_write_lines 1 2 three 4 5 6 7 8 9 >numbers &&
+ git mv letters letters_side1 &&
+ git mv content file_or_directory &&
+ git add numbers &&
+ git commit -m side1 &&
+
+ git checkout side2 &&
+ git rm numbers &&
+ git mv letters letters_side2 &&
+ mkdir file_or_directory &&
+ echo hello >file_or_directory/world &&
+ git add file_or_directory/world &&
+ git commit -m side2 &&
+
+ git checkout -b resolution side1 &&
+ test_must_fail git merge side2 &&
+ test_write_lines 1 2 three 4 5 6 7 8 9 >numbers &&
+ git add numbers &&
+ git add letters_side1 &&
+ git rm letters &&
+ git rm letters_side2 &&
+ git add file_or_directory~HEAD &&
+ git mv file_or_directory~HEAD wanted_content &&
+ git commit -m resolved
+'
+
+test_expect_success 'remerge-diff with non-content conflicts' '
+ git log -1 --oneline resolution >tmp &&
+ cat <<-EOF >>tmp &&
+ diff --git a/file_or_directory~HASH (side1) b/wanted_content
+ similarity index 100%
+ rename from file_or_directory~HASH (side1)
+ rename to wanted_content
+ remerge CONFLICT (file/directory): directory in the way of file_or_directory from HASH (side1); moving it to file_or_directory~HASH (side1) instead.
+ diff --git a/letters b/letters
+ remerge CONFLICT (rename/rename): letters renamed to letters_side1 in HASH (side1) and to letters_side2 in HASH (side2).
+ diff --git a/letters_side2 b/letters_side2
+ deleted file mode 100644
+ index b236ae5..0000000
+ --- a/letters_side2
+ +++ /dev/null
+ @@ -1,9 +0,0 @@
+ -a
+ -b
+ -c
+ -d
+ -e
+ -f
+ -g
+ -h
+ -i
+ diff --git a/numbers b/numbers
+ remerge CONFLICT (modify/delete): numbers deleted in HASH (side2) and modified in HASH (side1). Version HASH (side1) of numbers left in tree.
+ EOF
+ # We still have some sha1 hashes above; rip them out so test works
+ # with sha256
+ sed -e "s/[0-9a-f]\{7,\}/HASH/g" tmp >expect &&
+
+ git show --oneline --remerge-diff resolution >tmp &&
+ sed -e "s/[0-9a-f]\{7,\}/HASH/g" tmp >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'remerge-diff w/ diff-filter=U: all conflict headers, no diff content' '
+ git log -1 --oneline resolution >tmp &&
+ cat <<-EOF >>tmp &&
+ diff --git a/file_or_directory~HASH (side1) b/file_or_directory~HASH (side1)
+ remerge CONFLICT (file/directory): directory in the way of file_or_directory from HASH (side1); moving it to file_or_directory~HASH (side1) instead.
+ diff --git a/letters b/letters
+ remerge CONFLICT (rename/rename): letters renamed to letters_side1 in HASH (side1) and to letters_side2 in HASH (side2).
+ diff --git a/numbers b/numbers
+ remerge CONFLICT (modify/delete): numbers deleted in HASH (side2) and modified in HASH (side1). Version HASH (side1) of numbers left in tree.
+ EOF
+ # We still have some sha1 hashes above; rip them out so test works
+ # with sha256
+ sed -e "s/[0-9a-f]\{7,\}/HASH/g" tmp >expect &&
+
+ git show --oneline --remerge-diff --diff-filter=U resolution >tmp &&
+ sed -e "s/[0-9a-f]\{7,\}/HASH/g" tmp >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'submodule formatting ignores additional headers' '
+ # Reuses "expect" from last testcase
+
+ git show --oneline --remerge-diff --diff-filter=U --submodule=log >tmp &&
+ sed -e "s/[0-9a-f]\{7,\}/HASH/g" tmp >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'remerge-diff w/ diff-filter=R: relevant file + conflict header' '
+ git log -1 --oneline resolution >tmp &&
+ cat <<-EOF >>tmp &&
+ diff --git a/file_or_directory~HASH (side1) b/wanted_content
+ similarity index 100%
+ rename from file_or_directory~HASH (side1)
+ rename to wanted_content
+ remerge CONFLICT (file/directory): directory in the way of file_or_directory from HASH (side1); moving it to file_or_directory~HASH (side1) instead.
+ EOF
+ # We still have some sha1 hashes above; rip them out so test works
+ # with sha256
+ sed -e "s/[0-9a-f]\{7,\}/HASH/g" tmp >expect &&
+
+ git show --oneline --remerge-diff --diff-filter=R resolution >tmp &&
+ sed -e "s/[0-9a-f]\{7,\}/HASH/g" tmp >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'remerge-diff w/ pathspec: limits to relevant file including conflict header' '
+ git log -1 --oneline resolution >tmp &&
+ cat <<-EOF >>tmp &&
+ diff --git a/letters b/letters
+ remerge CONFLICT (rename/rename): letters renamed to letters_side1 in HASH (side1) and to letters_side2 in HASH (side2).
+ diff --git a/letters_side2 b/letters_side2
+ deleted file mode 100644
+ index b236ae5..0000000
+ --- a/letters_side2
+ +++ /dev/null
+ @@ -1,9 +0,0 @@
+ -a
+ -b
+ -c
+ -d
+ -e
+ -f
+ -g
+ -h
+ -i
+ EOF
+ # We still have some sha1 hashes above; rip them out so test works
+ # with sha256
+ sed -e "s/[0-9a-f]\{7,\}/HASH/g" tmp >expect &&
+
+ git show --oneline --remerge-diff resolution -- "letters*" >tmp &&
+ sed -e "s/[0-9a-f]\{7,\}/HASH/g" tmp >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'setup non-content conflicts' '
+ git switch --orphan newbase &&
+
+ test_write_lines 1 2 3 4 5 6 7 8 9 >numbers &&
+ git add numbers &&
+ git commit -m base &&
+
+ git branch newside1 &&
+ git branch newside2 &&
+
+ git checkout newside1 &&
+ test_write_lines 1 2 three 4 5 6 7 8 9 >numbers &&
+ git add numbers &&
+ git commit -m side1 &&
+
+ git checkout newside2 &&
+ test_write_lines 1 2 drei 4 5 6 7 8 9 >numbers &&
+ git add numbers &&
+ git commit -m side2 &&
+
+ git checkout -b newresolution newside1 &&
+ test_must_fail git merge newside2 &&
+ git checkout --theirs numbers &&
+ git add -u numbers &&
+ git commit -m resolved
+'
+
+test_expect_success 'remerge-diff turns off history simplification' '
+ git log -1 --oneline newresolution >tmp &&
+ cat <<-EOF >>tmp &&
+ diff --git a/numbers b/numbers
+ remerge CONFLICT (content): Merge conflict in numbers
+ index 070e9e7..5335e78 100644
+ --- a/numbers
+ +++ b/numbers
+ @@ -1,10 +1,6 @@
+ 1
+ 2
+ -<<<<<<< 96f1e45 (side1)
+ -three
+ -=======
+ drei
+ ->>>>>>> 4fd522f (side2)
+ 4
+ 5
+ 6
+ EOF
+ # We still have some sha1 hashes above; rip them out so test works
+ # with sha256
+ sed -e "s/[0-9a-f]\{7,\}/HASH/g" tmp >expect &&
+
+ git show --oneline --remerge-diff newresolution -- numbers >tmp &&
+ sed -e "s/[0-9a-f]\{7,\}/HASH/g" tmp >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t4100-apply-stat.sh b/t/t4100-apply-stat.sh
new file mode 100755
index 0000000..d503547
--- /dev/null
+++ b/t/t4100-apply-stat.sh
@@ -0,0 +1,42 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+test_description='git apply --stat --summary test, with --recount
+
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+UNC='s/^\(@@ -[1-9][0-9]*\),[0-9]* \(+[1-9][0-9]*\),[0-9]* @@/\1,999 \2,999 @@/'
+
+num=0
+while read title
+do
+ num=$(( $num + 1 ))
+ test_expect_success "$title" '
+ git apply --stat --summary \
+ <"$TEST_DIRECTORY/t4100/t-apply-$num.patch" >current &&
+ test_cmp "$TEST_DIRECTORY"/t4100/t-apply-$num.expect current
+ '
+
+ test_expect_success "$title with recount" '
+ sed -e "$UNC" <"$TEST_DIRECTORY/t4100/t-apply-$num.patch" |
+ git apply --recount --stat --summary >current &&
+ test_cmp "$TEST_DIRECTORY"/t4100/t-apply-$num.expect current
+ '
+done <<\EOF
+rename
+copy
+rewrite
+mode
+non git (1)
+non git (2)
+non git (3)
+incomplete (1)
+incomplete (2)
+EOF
+
+test_done
diff --git a/t/t4100/t-apply-1.expect b/t/t4100/t-apply-1.expect
new file mode 100644
index 0000000..540e64d
--- /dev/null
+++ b/t/t4100/t-apply-1.expect
@@ -0,0 +1,11 @@
+ Documentation/git-ssh-pull.txt | 12 ++++++------
+ Documentation/git-ssh-push.txt | 10 +++++-----
+ Documentation/git.txt | 6 +++---
+ Makefile | 6 +++---
+ ssh-pull.c | 4 ++--
+ ssh-push.c | 14 +++++++-------
+ 6 files changed, 26 insertions(+), 26 deletions(-)
+ rename Documentation/{git-rpull.txt => git-ssh-pull.txt} (90%)
+ rename Documentation/{git-rpush.txt => git-ssh-push.txt} (71%)
+ rename rpull.c => ssh-pull.c (97%)
+ rename rpush.c => ssh-push.c (93%)
diff --git a/t/t4100/t-apply-1.patch b/t/t4100/t-apply-1.patch
new file mode 100644
index 0000000..43394f8
--- /dev/null
+++ b/t/t4100/t-apply-1.patch
@@ -0,0 +1,194 @@
+418aaf847a8b3ffffb4f777a2dd5262ca5ce0ef7 (from dc93841715dfa9a9cdda6f2c4a25eec831ea7aa0)
+diff --git a/Documentation/git-rpull.txt b/Documentation/git-ssh-pull.txt
+similarity index 90%
+rename from Documentation/git-rpull.txt
+rename to Documentation/git-ssh-pull.txt
+--- a/Documentation/git-rpull.txt
++++ b/Documentation/git-ssh-pull.txt
+@@ -1,21 +1,21 @@
+-git-rpull(1)
+-============
++git-ssh-pull(1)
++===============
+ v0.1, May 2005
+
+ NAME
+ ----
+-git-rpull - Pulls from a remote repository over ssh connection
++git-ssh-pull - Pulls from a remote repository over ssh connection
+
+
+
+ SYNOPSIS
+ --------
+-'git-rpull' [-c] [-t] [-a] [-d] [-v] [--recover] commit-id url
++'git-ssh-pull' [-c] [-t] [-a] [-d] [-v] [--recover] commit-id url
+
+ DESCRIPTION
+ -----------
+-Pulls from a remote repository over ssh connection, invoking git-rpush on
+-the other end.
++Pulls from a remote repository over ssh connection, invoking git-ssh-push
++on the other end.
+
+ OPTIONS
+ -------
+diff --git a/Documentation/git-rpush.txt b/Documentation/git-ssh-push.txt
+similarity index 71%
+rename from Documentation/git-rpush.txt
+rename to Documentation/git-ssh-push.txt
+--- a/Documentation/git-rpush.txt
++++ b/Documentation/git-ssh-push.txt
+@@ -1,19 +1,19 @@
+-git-rpush(1)
+-============
++git-ssh-push(1)
++===============
+ v0.1, May 2005
+
+ NAME
+ ----
+-git-rpush - Helper "server-side" program used by git-rpull
++git-ssh-push - Helper "server-side" program used by git-ssh-pull
+
+
+ SYNOPSIS
+ --------
+-'git-rpush'
++'git-ssh-push'
+
+ DESCRIPTION
+ -----------
+-Helper "server-side" program used by git-rpull.
++Helper "server-side" program used by git-ssh-pull.
+
+
+ Author
+diff --git a/Documentation/git.txt b/Documentation/git.txt
+--- a/Documentation/git.txt
++++ b/Documentation/git.txt
+@@ -148,7 +148,7 @@ link:git-resolve-script.html[git-resolve
+ link:git-tag-script.html[git-tag-script]::
+ An example script to create a tag object signed with GPG
+
+-link:git-rpull.html[git-rpull]::
++link:git-ssh-pull.html[git-ssh-pull]::
+ Pulls from a remote repository over ssh connection
+
+ Interrogators:
+@@ -156,8 +156,8 @@ Interrogators:
+ link:git-diff-helper.html[git-diff-helper]::
+ Generates patch format output for git-diff-*
+
+-link:git-rpush.html[git-rpush]::
+- Helper "server-side" program used by git-rpull
++link:git-ssh-push.html[git-ssh-push]::
++ Helper "server-side" program used by git-ssh-pull
+
+
+
+diff --git a/Makefile b/Makefile
+--- a/Makefile
++++ b/Makefile
+@@ -30,7 +30,7 @@ PROG= git-update-index git-diff-files
+ git-checkout-cache git-diff-tree git-rev-tree git-ls-files \
+ git-check-files git-ls-tree git-merge-base git-merge-cache \
+ git-unpack-file git-export git-diff-cache git-convert-cache \
+- git-http-pull git-rpush git-rpull git-rev-list git-mktag \
++ git-http-pull git-ssh-push git-ssh-pull git-rev-list git-mktag \
+ git-diff-helper git-tar-tree git-local-pull git-write-blob \
+ git-get-tar-commit-id git-mkdelta git-apply git-stripspace
+
+@@ -105,8 +105,8 @@ git-diff-cache: diff-cache.c
+ git-convert-cache: convert-cache.c
+ git-http-pull: http-pull.c pull.c
+ git-local-pull: local-pull.c pull.c
+-git-rpush: rsh.c
+-git-rpull: rsh.c pull.c
++git-ssh-push: rsh.c
++git-ssh-pull: rsh.c pull.c
+ git-rev-list: rev-list.c
+ git-mktag: mktag.c
+ git-diff-helper: diff-helper.c
+diff --git a/rpull.c b/ssh-pull.c
+similarity index 97%
+rename from rpull.c
+rename to ssh-pull.c
+--- a/rpull.c
++++ b/ssh-pull.c
+@@ -64,13 +64,13 @@ int main(int argc, char **argv)
+ arg++;
+ }
+ if (argc < arg + 2) {
+- usage("git-rpull [-c] [-t] [-a] [-v] [-d] [--recover] commit-id url");
++ usage("git-ssh-pull [-c] [-t] [-a] [-v] [-d] [--recover] commit-id url");
+ return 1;
+ }
+ commit_id = argv[arg];
+ url = argv[arg + 1];
+
+- if (setup_connection(&fd_in, &fd_out, "git-rpush", url, arg, argv + 1))
++ if (setup_connection(&fd_in, &fd_out, "git-ssh-push", url, arg, argv + 1))
+ return 1;
+
+ if (get_version())
+diff --git a/rpush.c b/ssh-push.c
+similarity index 93%
+rename from rpush.c
+rename to ssh-push.c
+--- a/rpush.c
++++ b/ssh-push.c
+@@ -16,7 +16,7 @@ int serve_object(int fd_in, int fd_out)
+ do {
+ size = read(fd_in, sha1 + posn, 20 - posn);
+ if (size < 0) {
+- perror("git-rpush: read ");
++ perror("git-ssh-push: read ");
+ return -1;
+ }
+ if (!size)
+@@ -30,7 +30,7 @@ int serve_object(int fd_in, int fd_out)
+ buf = map_sha1_file(sha1, &objsize);
+
+ if (!buf) {
+- fprintf(stderr, "git-rpush: could not find %s\n",
++ fprintf(stderr, "git-ssh-push: could not find %s\n",
+ sha1_to_hex(sha1));
+ remote = -1;
+ }
+@@ -45,9 +45,9 @@ int serve_object(int fd_in, int fd_out)
+ size = write(fd_out, buf + posn, objsize - posn);
+ if (size <= 0) {
+ if (!size) {
+- fprintf(stderr, "git-rpush: write closed");
++ fprintf(stderr, "git-ssh-push: write closed");
+ } else {
+- perror("git-rpush: write ");
++ perror("git-ssh-push: write ");
+ }
+ return -1;
+ }
+@@ -71,7 +71,7 @@ void service(int fd_in, int fd_out) {
+ retval = read(fd_in, &type, 1);
+ if (retval < 1) {
+ if (retval < 0)
+- perror("rpush: read ");
++ perror("git-ssh-push: read ");
+ return;
+ }
+ if (type == 'v' && serve_version(fd_in, fd_out))
+@@ -91,12 +91,12 @@ int main(int argc, char **argv)
+ arg++;
+ }
+ if (argc < arg + 2) {
+- usage("git-rpush [-c] [-t] [-a] commit-id url");
++ usage("git-ssh-push [-c] [-t] [-a] commit-id url");
+ return 1;
+ }
+ commit_id = argv[arg];
+ url = argv[arg + 1];
+- if (setup_connection(&fd_in, &fd_out, "git-rpull", url, arg, argv + 1))
++ if (setup_connection(&fd_in, &fd_out, "git-ssh-pull", url, arg, argv + 1))
+ return 1;
+
+ service(fd_in, fd_out);
diff --git a/t/t4100/t-apply-2.expect b/t/t4100/t-apply-2.expect
new file mode 100644
index 0000000..d1e6459
--- /dev/null
+++ b/t/t4100/t-apply-2.expect
@@ -0,0 +1,5 @@
+ Makefile | 2 +-
+ git-fetch-script | 5 -----
+ git-pull-script | 34 +---------------------------------
+ 3 files changed, 2 insertions(+), 39 deletions(-)
+ copy git-pull-script => git-fetch-script (87%)
diff --git a/t/t4100/t-apply-2.patch b/t/t4100/t-apply-2.patch
new file mode 100644
index 0000000..f5c7d60
--- /dev/null
+++ b/t/t4100/t-apply-2.patch
@@ -0,0 +1,72 @@
+7ef76925d9c19ef74874e1735e2436e56d0c4897 (from 6b14d7faf0bad026a81a27bac07b47691f621b8f)
+diff --git a/Makefile b/Makefile
+--- a/Makefile
++++ b/Makefile
+@@ -20,7 +20,7 @@ INSTALL=install
+
+ SCRIPTS=git-apply-patch-script git-merge-one-file-script git-prune-script \
+ git-pull-script git-tag-script git-resolve-script git-whatchanged \
+- git-deltafy-script
++ git-deltafy-script git-fetch-script
+
+ PROG= git-update-index git-diff-files git-init-db git-write-tree \
+ git-read-tree git-commit-tree git-cat-file git-fsck-cache \
+diff --git a/git-pull-script b/git-fetch-script
+similarity index 87%
+copy from git-pull-script
+copy to git-fetch-script
+--- a/git-pull-script
++++ b/git-fetch-script
+@@ -39,8 +39,3 @@ download_one "$merge_repo/$merge_name" "
+
+ echo "Getting object database"
+ download_objects "$merge_repo" "$(cat "$GIT_DIR"/MERGE_HEAD)"
+-
+-git-resolve-script \
+- "$(cat "$GIT_DIR"/HEAD)" \
+- "$(cat "$GIT_DIR"/MERGE_HEAD)" \
+- "$merge_repo"
+diff --git a/git-pull-script b/git-pull-script
+--- a/git-pull-script
++++ b/git-pull-script
+@@ -6,39 +6,7 @@ merge_name=${2:-HEAD}
+ : ${GIT_DIR=.git}
+ : ${GIT_OBJECT_DIRECTORY="${SHA1_FILE_DIRECTORY-"$GIT_DIR/objects"}"}
+
+-download_one () {
+- # remote_path="$1" local_file="$2"
+- case "$1" in
+- http://*)
+- wget -q -O "$2" "$1" ;;
+- /*)
+- test -f "$1" && cat >"$2" "$1" ;;
+- *)
+- rsync -L "$1" "$2" ;;
+- esac
+-}
+-
+-download_objects () {
+- # remote_repo="$1" head_sha1="$2"
+- case "$1" in
+- http://*)
+- git-http-pull -a "$2" "$1/"
+- ;;
+- /*)
+- git-local-pull -l -a "$2" "$1/"
+- ;;
+- *)
+- rsync -avz --ignore-existing \
+- "$1/objects/." "$GIT_OBJECT_DIRECTORY"/.
+- ;;
+- esac
+-}
+-
+-echo "Getting remote $merge_name"
+-download_one "$merge_repo/$merge_name" "$GIT_DIR"/MERGE_HEAD
+-
+-echo "Getting object database"
+-download_objects "$merge_repo" "$(cat "$GIT_DIR"/MERGE_HEAD)"
++git-fetch-script "$merge_repo" "$merge_name"
+
+ git-resolve-script \
+ "$(cat "$GIT_DIR"/HEAD)" \
diff --git a/t/t4100/t-apply-3.expect b/t/t4100/t-apply-3.expect
new file mode 100644
index 0000000..912a552
--- /dev/null
+++ b/t/t4100/t-apply-3.expect
@@ -0,0 +1,7 @@
+ Documentation/git-ls-tree.txt | 20 +-
+ ls-tree.c | 459 ++++++++++++++++++++++-------------------
+ t/t3100-ls-tree-restrict.sh | 3
+ tree.c | 2
+ tree.h | 1
+ 5 files changed, 262 insertions(+), 223 deletions(-)
+ rewrite ls-tree.c (82%)
diff --git a/t/t4100/t-apply-3.patch b/t/t4100/t-apply-3.patch
new file mode 100644
index 0000000..cac172e
--- /dev/null
+++ b/t/t4100/t-apply-3.patch
@@ -0,0 +1,567 @@
+6af1f0192ff8740fe77db7cf02c739ccfbdf119c (from 2bc2564145835996734d6ed5d1880f85b17233d6)
+diff --git a/Documentation/git-ls-tree.txt b/Documentation/git-ls-tree.txt
+--- a/Documentation/git-ls-tree.txt
++++ b/Documentation/git-ls-tree.txt
+@@ -4,23 +4,26 @@ v0.1, May 2005
+
+ NAME
+ ----
+-git-ls-tree - Displays a tree object in human readable form
++git-ls-tree - Lists the contents of a tree object.
+
+
+ SYNOPSIS
+ --------
+-'git-ls-tree' [-r] [-z] <tree-ish> [paths...]
++'git-ls-tree' [-d] [-r] [-z] <tree-ish> [paths...]
+
+ DESCRIPTION
+ -----------
+-Converts the tree object to a human readable (and script processable)
+-form.
++Lists the contents of a tree object, like what "/bin/ls -a" does
++in the current working directory.
+
+ OPTIONS
+ -------
+ <tree-ish>::
+ Id of a tree.
+
++-d::
++ show only the named tree entry itself, not its children
++
+ -r::
+ recurse into sub-trees
+
+@@ -28,18 +31,19 @@ OPTIONS
+ \0 line termination on output
+
+ paths::
+- Optionally, restrict the output of git-ls-tree to specific
+- paths. Directories will only list their tree blob ids.
+- Implies -r.
++ When paths are given, shows them. Otherwise implicitly
++ uses the root level of the tree as the sole path argument.
++
+
+ Output Format
+ -------------
+- <mode>\t <type>\t <object>\t <file>
++ <mode> SP <type> SP <object> TAB <file>
+
+
+ Author
+ ------
+ Written by Linus Torvalds <torvalds@osdl.org>
++Completely rewritten from scratch by Junio C Hamano <junkio@cox.net>
+
+ Documentation
+ --------------
+diff --git a/ls-tree.c b/ls-tree.c
+dissimilarity index 82%
+--- ls-tree.c
++++ ls-tree.c
+@@ -1,212 +1,247 @@
+-/*
+- * GIT - The information manager from hell
+- *
+- * Copyright (C) Linus Torvalds, 2005
+- */
+-#include "cache.h"
+-
+-static int line_termination = '\n';
+-static int recursive = 0;
+-
+-struct path_prefix {
+- struct path_prefix *prev;
+- const char *name;
+-};
+-
+-#define DEBUG(fmt, ...)
+-
+-static int string_path_prefix(char *buff, size_t blen, struct path_prefix *prefix)
+-{
+- int len = 0;
+- if (prefix) {
+- if (prefix->prev) {
+- len = string_path_prefix(buff,blen,prefix->prev);
+- buff += len;
+- blen -= len;
+- if (blen > 0) {
+- *buff = '/';
+- len++;
+- buff++;
+- blen--;
+- }
+- }
+- strncpy(buff,prefix->name,blen);
+- return len + strlen(prefix->name);
+- }
+-
+- return 0;
+-}
+-
+-static void print_path_prefix(struct path_prefix *prefix)
+-{
+- if (prefix) {
+- if (prefix->prev) {
+- print_path_prefix(prefix->prev);
+- putchar('/');
+- }
+- fputs(prefix->name, stdout);
+- }
+-}
+-
+-/*
+- * return:
+- * -1 if prefix is *not* a subset of path
+- * 0 if prefix == path
+- * 1 if prefix is a subset of path
+- */
+-static int pathcmp(const char *path, struct path_prefix *prefix)
+-{
+- char buff[PATH_MAX];
+- int len,slen;
+-
+- if (prefix == NULL)
+- return 1;
+-
+- len = string_path_prefix(buff, sizeof buff, prefix);
+- slen = strlen(path);
+-
+- if (slen < len)
+- return -1;
+-
+- if (strncmp(path,buff,len) == 0) {
+- if (slen == len)
+- return 0;
+- else
+- return 1;
+- }
+-
+- return -1;
+-}
+-
+-/*
+- * match may be NULL, or a *sorted* list of paths
+- */
+-static void list_recursive(void *buffer,
+- const char *type,
+- unsigned long size,
+- struct path_prefix *prefix,
+- char **match, int matches)
+-{
+- struct path_prefix this_prefix;
+- this_prefix.prev = prefix;
+-
+- if (strcmp(type, "tree"))
+- die("expected a 'tree' node");
+-
+- if (matches)
+- recursive = 1;
+-
+- while (size) {
+- int namelen = strlen(buffer)+1;
+- void *eltbuf = NULL;
+- char elttype[20];
+- unsigned long eltsize;
+- unsigned char *sha1 = buffer + namelen;
+- char *path = strchr(buffer, ' ') + 1;
+- unsigned int mode;
+- const char *matched = NULL;
+- int mtype = -1;
+- int mindex;
+-
+- if (size < namelen + 20 || sscanf(buffer, "%o", &mode) != 1)
+- die("corrupt 'tree' file");
+- buffer = sha1 + 20;
+- size -= namelen + 20;
+-
+- this_prefix.name = path;
+- for ( mindex = 0; mindex < matches; mindex++) {
+- mtype = pathcmp(match[mindex],&this_prefix);
+- if (mtype >= 0) {
+- matched = match[mindex];
+- break;
+- }
+- }
+-
+- /*
+- * If we're not matching, or if this is an exact match,
+- * print out the info
+- */
+- if (!matches || (matched != NULL && mtype == 0)) {
+- printf("%06o %s %s\t", mode,
+- S_ISDIR(mode) ? "tree" : "blob",
+- sha1_to_hex(sha1));
+- print_path_prefix(&this_prefix);
+- putchar(line_termination);
+- }
+-
+- if (! recursive || ! S_ISDIR(mode))
+- continue;
+-
+- if (matches && ! matched)
+- continue;
+-
+- if (! (eltbuf = read_sha1_file(sha1, elttype, &eltsize)) ) {
+- error("cannot read %s", sha1_to_hex(sha1));
+- continue;
+- }
+-
+- /* If this is an exact directory match, we may have
+- * directory files following this path. Match on them.
+- * Otherwise, we're at a patch subcomponent, and we need
+- * to try to match again.
+- */
+- if (mtype == 0)
+- mindex++;
+-
+- list_recursive(eltbuf, elttype, eltsize, &this_prefix, &match[mindex], matches-mindex);
+- free(eltbuf);
+- }
+-}
+-
+-static int qcmp(const void *a, const void *b)
+-{
+- return strcmp(*(char **)a, *(char **)b);
+-}
+-
+-static int list(unsigned char *sha1,char **path)
+-{
+- void *buffer;
+- unsigned long size;
+- int npaths;
+-
+- for (npaths = 0; path[npaths] != NULL; npaths++)
+- ;
+-
+- qsort(path,npaths,sizeof(char *),qcmp);
+-
+- buffer = read_object_with_reference(sha1, "tree", &size, NULL);
+- if (!buffer)
+- die("unable to read sha1 file");
+- list_recursive(buffer, "tree", size, NULL, path, npaths);
+- free(buffer);
+- return 0;
+-}
+-
+-static const char *ls_tree_usage = "git-ls-tree [-r] [-z] <key> [paths...]";
+-
+-int main(int argc, char **argv)
+-{
+- unsigned char sha1[20];
+-
+- while (1 < argc && argv[1][0] == '-') {
+- switch (argv[1][1]) {
+- case 'z':
+- line_termination = 0;
+- break;
+- case 'r':
+- recursive = 1;
+- break;
+- default:
+- usage(ls_tree_usage);
+- }
+- argc--; argv++;
+- }
+-
+- if (argc < 2)
+- usage(ls_tree_usage);
+- if (get_sha1(argv[1], sha1) < 0)
+- usage(ls_tree_usage);
+- if (list(sha1, &argv[2]) < 0)
+- die("list failed");
+- return 0;
+-}
++/*
++ * GIT - The information manager from hell
++ *
++ * Copyright (C) Linus Torvalds, 2005
++ */
++#include "cache.h"
++#include "blob.h"
++#include "tree.h"
++
++static int line_termination = '\n';
++#define LS_RECURSIVE 1
++#define LS_TREE_ONLY 2
++static int ls_options = 0;
++
++static struct tree_entry_list root_entry;
++
++static void prepare_root(unsigned char *sha1)
++{
++ unsigned char rsha[20];
++ unsigned long size;
++ void *buf;
++ struct tree *root_tree;
++
++ buf = read_object_with_reference(sha1, "tree", &size, rsha);
++ free(buf);
++ if (!buf)
++ die("Could not read %s", sha1_to_hex(sha1));
++
++ root_tree = lookup_tree(rsha);
++ if (!root_tree)
++ die("Could not read %s", sha1_to_hex(sha1));
++
++ /* Prepare a fake entry */
++ root_entry.directory = 1;
++ root_entry.executable = root_entry.symlink = 0;
++ root_entry.mode = S_IFDIR;
++ root_entry.name = "";
++ root_entry.item.tree = root_tree;
++ root_entry.parent = NULL;
++}
++
++static int prepare_children(struct tree_entry_list *elem)
++{
++ if (!elem->directory)
++ return -1;
++ if (!elem->item.tree->object.parsed) {
++ struct tree_entry_list *e;
++ if (parse_tree(elem->item.tree))
++ return -1;
++ /* Set up the parent link */
++ for (e = elem->item.tree->entries; e; e = e->next)
++ e->parent = elem;
++ }
++ return 0;
++}
++
++static struct tree_entry_list *find_entry_0(struct tree_entry_list *elem,
++ const char *path,
++ const char *path_end)
++{
++ const char *ep;
++ int len;
++
++ while (path < path_end) {
++ if (prepare_children(elem))
++ return NULL;
++
++ /* In elem->tree->entries, find the one that has name
++ * that matches what is between path and ep.
++ */
++ elem = elem->item.tree->entries;
++
++ ep = strchr(path, '/');
++ if (!ep || path_end <= ep)
++ ep = path_end;
++ len = ep - path;
++
++ while (elem) {
++ if ((strlen(elem->name) == len) &&
++ !strncmp(elem->name, path, len))
++ break;
++ elem = elem->next;
++ }
++ if (path_end <= ep || !elem)
++ return elem;
++ while (*ep == '/' && ep < path_end)
++ ep++;
++ path = ep;
++ }
++ return NULL;
++}
++
++static struct tree_entry_list *find_entry(const char *path,
++ const char *path_end)
++{
++ /* Find tree element, descending from root, that
++ * corresponds to the named path, lazily expanding
++ * the tree if possible.
++ */
++ if (path == path_end) {
++ /* Special. This is the root level */
++ return &root_entry;
++ }
++ return find_entry_0(&root_entry, path, path_end);
++}
++
++static void show_entry_name(struct tree_entry_list *e)
++{
++ /* This is yucky. The root level is there for
++ * our convenience but we really want to do a
++ * forest.
++ */
++ if (e->parent && e->parent != &root_entry) {
++ show_entry_name(e->parent);
++ putchar('/');
++ }
++ printf("%s", e->name);
++}
++
++static const char *entry_type(struct tree_entry_list *e)
++{
++ return (e->directory ? "tree" : "blob");
++}
++
++static const char *entry_hex(struct tree_entry_list *e)
++{
++ return sha1_to_hex(e->directory
++ ? e->item.tree->object.sha1
++ : e->item.blob->object.sha1);
++}
++
++/* forward declaration for mutually recursive routines */
++static int show_entry(struct tree_entry_list *, int);
++
++static int show_children(struct tree_entry_list *e, int level)
++{
++ if (prepare_children(e))
++ die("internal error: ls-tree show_children called with non tree");
++ e = e->item.tree->entries;
++ while (e) {
++ show_entry(e, level);
++ e = e->next;
++ }
++ return 0;
++}
++
++static int show_entry(struct tree_entry_list *e, int level)
++{
++ int err = 0;
++
++ if (e != &root_entry) {
++ printf("%06o %s %s ", e->mode, entry_type(e),
++ entry_hex(e));
++ show_entry_name(e);
++ putchar(line_termination);
++ }
++
++ if (e->directory) {
++ /* If this is a directory, we have the following cases:
++ * (1) This is the top-level request (explicit path from the
++ * command line, or "root" if there is no command line).
++ * a. Without any flag. We show direct children. We do not
++ * recurse into them.
++ * b. With -r. We do recurse into children.
++ * c. With -d. We do not recurse into children.
++ * (2) We came here because our caller is either (1-a) or
++ * (1-b).
++ * a. Without any flag. We do not show our children (which
++ * are grandchildren for the original request).
++ * b. With -r. We continue to recurse into our children.
++ * c. With -d. We should not have come here to begin with.
++ */
++ if (level == 0 && !(ls_options & LS_TREE_ONLY))
++ /* case (1)-a and (1)-b */
++ err = err | show_children(e, level+1);
++ else if (level && ls_options & LS_RECURSIVE)
++ /* case (2)-b */
++ err = err | show_children(e, level+1);
++ }
++ return err;
++}
++
++static int list_one(const char *path, const char *path_end)
++{
++ int err = 0;
++ struct tree_entry_list *e = find_entry(path, path_end);
++ if (!e) {
++ /* traditionally ls-tree does not complain about
++ * missing path. We may change this later to match
++ * what "/bin/ls -a" does, which is to complain.
++ */
++ return err;
++ }
++ err = err | show_entry(e, 0);
++ return err;
++}
++
++static int list(char **path)
++{
++ int i;
++ int err = 0;
++ for (i = 0; path[i]; i++) {
++ int len = strlen(path[i]);
++ while (0 <= len && path[i][len] == '/')
++ len--;
++ err = err | list_one(path[i], path[i] + len);
++ }
++ return err;
++}
++
++static const char *ls_tree_usage =
++ "git-ls-tree [-d] [-r] [-z] <tree-ish> [path...]";
++
++int main(int argc, char **argv)
++{
++ static char *path0[] = { "", NULL };
++ char **path;
++ unsigned char sha1[20];
++
++ while (1 < argc && argv[1][0] == '-') {
++ switch (argv[1][1]) {
++ case 'z':
++ line_termination = 0;
++ break;
++ case 'r':
++ ls_options |= LS_RECURSIVE;
++ break;
++ case 'd':
++ ls_options |= LS_TREE_ONLY;
++ break;
++ default:
++ usage(ls_tree_usage);
++ }
++ argc--; argv++;
++ }
++
++ if (argc < 2)
++ usage(ls_tree_usage);
++ if (get_sha1(argv[1], sha1) < 0)
++ usage(ls_tree_usage);
++
++ path = (argc == 2) ? path0 : (argv + 2);
++ prepare_root(sha1);
++ if (list(path) < 0)
++ die("list failed");
++ return 0;
++}
+diff --git a/t/t3100-ls-tree-restrict.sh b/t/t3100-ls-tree-restrict.sh
+--- a/t/t3100-ls-tree-restrict.sh
++++ b/t/t3100-ls-tree-restrict.sh
+@@ -74,8 +74,8 @@ test_expect_success \
+ 'ls-tree filtered' \
+ 'git-ls-tree $tree path1 path0 >current &&
+ cat >expected <<\EOF &&
+-100644 blob X path0
+ 120000 blob X path1
++100644 blob X path0
+ EOF
+ test_output'
+
+@@ -85,7 +85,6 @@ test_expect_success \
+ cat >expected <<\EOF &&
+ 040000 tree X path2
+ 040000 tree X path2/baz
+-100644 blob X path2/baz/b
+ 120000 blob X path2/bazbo
+ 100644 blob X path2/foo
+ EOF
+diff --git a/tree.c b/tree.c
+--- a/tree.c
++++ b/tree.c
+@@ -133,7 +133,7 @@ int parse_tree_buffer(struct tree *item,
+ }
+ if (obj)
+ add_ref(&item->object, obj);
+-
++ entry->parent = NULL; /* needs to be filled by the user */
+ *list_p = entry;
+ list_p = &entry->next;
+ }
+diff --git a/tree.h b/tree.h
+--- a/tree.h
++++ b/tree.h
+@@ -16,6 +16,7 @@ struct tree_entry_list {
+ struct tree *tree;
+ struct blob *blob;
+ } item;
++ struct tree_entry_list *parent;
+ };
+
+ struct tree {
diff --git a/t/t4100/t-apply-4.expect b/t/t4100/t-apply-4.expect
new file mode 100644
index 0000000..1ec028b
--- /dev/null
+++ b/t/t4100/t-apply-4.expect
@@ -0,0 +1,5 @@
+ t/t0000-basic.sh | 0
+ t/test-lib.sh | 0
+ 2 files changed, 0 insertions(+), 0 deletions(-)
+ mode change 100644 => 100755 t/t0000-basic.sh
+ mode change 100644 => 100755 t/test-lib.sh
diff --git a/t/t4100/t-apply-4.patch b/t/t4100/t-apply-4.patch
new file mode 100644
index 0000000..4a56ab5
--- /dev/null
+++ b/t/t4100/t-apply-4.patch
@@ -0,0 +1,7 @@
+ceede59ea90cebad52ba9c8263fef3fb6ef17593 (from 368f99d57e8ed17243f2e164431449d48bfca2fb)
+diff --git a/t/t0000-basic.sh b/t/t0000-basic.sh
+old mode 100644
+new mode 100755
+diff --git a/t/test-lib.sh b/t/test-lib.sh
+old mode 100644
+new mode 100755
diff --git a/t/t4100/t-apply-5.expect b/t/t4100/t-apply-5.expect
new file mode 100644
index 0000000..b387df1
--- /dev/null
+++ b/t/t4100/t-apply-5.expect
@@ -0,0 +1,19 @@
+ Documentation/git-rpull.txt | 50 -------------------
+ Documentation/git-rpush.txt | 30 ------------
+ Documentation/git-ssh-pull.txt | 50 +++++++++++++++++++
+ Documentation/git-ssh-push.txt | 30 ++++++++++++
+ Documentation/git.txt | 6 +-
+ Makefile | 6 +-
+ rpull.c | 83 --------------------------------
+ rpush.c | 104 ----------------------------------------
+ ssh-pull.c | 83 ++++++++++++++++++++++++++++++++
+ ssh-push.c | 104 ++++++++++++++++++++++++++++++++++++++++
+ 10 files changed, 273 insertions(+), 273 deletions(-)
+ delete Documentation/git-rpull.txt
+ delete Documentation/git-rpush.txt
+ create Documentation/git-ssh-pull.txt
+ create Documentation/git-ssh-push.txt
+ delete rpull.c
+ delete rpush.c
+ create ssh-pull.c
+ create ssh-push.c
diff --git a/t/t4100/t-apply-5.patch b/t/t4100/t-apply-5.patch
new file mode 100644
index 0000000..57ec79d
--- /dev/null
+++ b/t/t4100/t-apply-5.patch
@@ -0,0 +1,612 @@
+diff a/Documentation/git-rpull.txt b/Documentation/git-rpull.txt
+--- a/Documentation/git-rpull.txt
++++ /dev/null
+@@ -1,50 +0,0 @@
+-git-rpull(1)
+-============
+-v0.1, May 2005
+-
+-NAME
+-----
+-git-rpull - Pulls from a remote repository over ssh connection
+-
+-
+-
+-SYNOPSIS
+---------
+-'git-rpull' [-c] [-t] [-a] [-d] [-v] [--recover] commit-id url
+-
+-DESCRIPTION
+------------
+-Pulls from a remote repository over ssh connection, invoking git-rpush on
+-the other end.
+-
+-OPTIONS
+--------
+--c::
+- Get the commit objects.
+--t::
+- Get trees associated with the commit objects.
+--a::
+- Get all the objects.
+--d::
+- Do not check for delta base objects (use this option
+- only when you know the remote repository is not
+- deltified).
+---recover::
+- Check dependency of deltified object more carefully than
+- usual, to recover after earlier pull that was interrupted.
+--v::
+- Report what is downloaded.
+-
+-
+-Author
+-------
+-Written by Linus Torvalds <torvalds@osdl.org>
+-
+-Documentation
+---------------
+-Documentation by David Greaves, Junio C Hamano and the git-list <git@vger.kernel.org>.
+-
+-GIT
+----
+-Part of the link:git.html[git] suite
+-
+diff a/Documentation/git-rpush.txt b/Documentation/git-rpush.txt
+--- a/Documentation/git-rpush.txt
++++ /dev/null
+@@ -1,30 +0,0 @@
+-git-rpush(1)
+-============
+-v0.1, May 2005
+-
+-NAME
+-----
+-git-rpush - Helper "server-side" program used by git-rpull
+-
+-
+-SYNOPSIS
+---------
+-'git-rpush'
+-
+-DESCRIPTION
+------------
+-Helper "server-side" program used by git-rpull.
+-
+-
+-Author
+-------
+-Written by Linus Torvalds <torvalds@osdl.org>
+-
+-Documentation
+---------------
+-Documentation by David Greaves, Junio C Hamano and the git-list <git@vger.kernel.org>.
+-
+-GIT
+----
+-Part of the link:git.html[git] suite
+-
+diff a/Documentation/git-ssh-pull.txt b/Documentation/git-ssh-pull.txt
+--- /dev/null
++++ b/Documentation/git-ssh-pull.txt
+@@ -0,0 +1,50 @@
++git-ssh-pull(1)
++===============
++v0.1, May 2005
++
++NAME
++----
++git-ssh-pull - Pulls from a remote repository over ssh connection
++
++
++
++SYNOPSIS
++--------
++'git-ssh-pull' [-c] [-t] [-a] [-d] [-v] [--recover] commit-id url
++
++DESCRIPTION
++-----------
++Pulls from a remote repository over ssh connection, invoking git-ssh-push
++on the other end.
++
++OPTIONS
++-------
++-c::
++ Get the commit objects.
++-t::
++ Get trees associated with the commit objects.
++-a::
++ Get all the objects.
++-d::
++ Do not check for delta base objects (use this option
++ only when you know the remote repository is not
++ deltified).
++--recover::
++ Check dependency of deltified object more carefully than
++ usual, to recover after earlier pull that was interrupted.
++-v::
++ Report what is downloaded.
++
++
++Author
++------
++Written by Linus Torvalds <torvalds@osdl.org>
++
++Documentation
++--------------
++Documentation by David Greaves, Junio C Hamano and the git-list <git@vger.kernel.org>.
++
++GIT
++---
++Part of the link:git.html[git] suite
++
+diff a/Documentation/git-ssh-push.txt b/Documentation/git-ssh-push.txt
+--- /dev/null
++++ b/Documentation/git-ssh-push.txt
+@@ -0,0 +1,30 @@
++git-ssh-push(1)
++===============
++v0.1, May 2005
++
++NAME
++----
++git-ssh-push - Helper "server-side" program used by git-ssh-pull
++
++
++SYNOPSIS
++--------
++'git-ssh-push'
++
++DESCRIPTION
++-----------
++Helper "server-side" program used by git-ssh-pull.
++
++
++Author
++------
++Written by Linus Torvalds <torvalds@osdl.org>
++
++Documentation
++--------------
++Documentation by David Greaves, Junio C Hamano and the git-list <git@vger.kernel.org>.
++
++GIT
++---
++Part of the link:git.html[git] suite
++
+diff a/Documentation/git.txt b/Documentation/git.txt
+--- a/Documentation/git.txt
++++ b/Documentation/git.txt
+@@ -148,7 +148,7 @@ link:git-resolve-script.html[git-resolve
+ link:git-tag-script.html[git-tag-script]::
+ An example script to create a tag object signed with GPG
+
+-link:git-rpull.html[git-rpull]::
++link:git-ssh-pull.html[git-ssh-pull]::
+ Pulls from a remote repository over ssh connection
+
+ Interrogators:
+@@ -156,8 +156,8 @@ Interrogators:
+ link:git-diff-helper.html[git-diff-helper]::
+ Generates patch format output for git-diff-*
+
+-link:git-rpush.html[git-rpush]::
+- Helper "server-side" program used by git-rpull
++link:git-ssh-push.html[git-ssh-push]::
++ Helper "server-side" program used by git-ssh-pull
+
+
+
+diff a/Makefile b/Makefile
+--- a/Makefile
++++ b/Makefile
+@@ -30,7 +30,7 @@ PROG= git-update-index git-diff-files
+ git-checkout-cache git-diff-tree git-rev-tree git-ls-files \
+ git-check-files git-ls-tree git-merge-base git-merge-cache \
+ git-unpack-file git-export git-diff-cache git-convert-cache \
+- git-http-pull git-rpush git-rpull git-rev-list git-mktag \
++ git-http-pull git-ssh-push git-ssh-pull git-rev-list git-mktag \
+ git-diff-helper git-tar-tree git-local-pull git-write-blob \
+ git-get-tar-commit-id git-mkdelta git-apply git-stripspace
+
+@@ -105,8 +105,8 @@ git-diff-cache: diff-cache.c
+ git-convert-cache: convert-cache.c
+ git-http-pull: http-pull.c pull.c
+ git-local-pull: local-pull.c pull.c
+-git-rpush: rsh.c
+-git-rpull: rsh.c pull.c
++git-ssh-push: rsh.c
++git-ssh-pull: rsh.c pull.c
+ git-rev-list: rev-list.c
+ git-mktag: mktag.c
+ git-diff-helper: diff-helper.c
+diff a/rpull.c b/rpull.c
+--- a/rpull.c
++++ /dev/null
+@@ -1,83 +0,0 @@
+-#include "cache.h"
+-#include "commit.h"
+-#include "rsh.h"
+-#include "pull.h"
+-
+-static int fd_in;
+-static int fd_out;
+-
+-static unsigned char remote_version = 0;
+-static unsigned char local_version = 1;
+-
+-int fetch(unsigned char *sha1)
+-{
+- int ret;
+- signed char remote;
+- char type = 'o';
+- if (has_sha1_file(sha1))
+- return 0;
+- write(fd_out, &type, 1);
+- write(fd_out, sha1, 20);
+- if (read(fd_in, &remote, 1) < 1)
+- return -1;
+- if (remote < 0)
+- return remote;
+- ret = write_sha1_from_fd(sha1, fd_in);
+- if (!ret)
+- pull_say("got %s\n", sha1_to_hex(sha1));
+- return ret;
+-}
+-
+-int get_version(void)
+-{
+- char type = 'v';
+- write(fd_out, &type, 1);
+- write(fd_out, &local_version, 1);
+- if (read(fd_in, &remote_version, 1) < 1) {
+- return error("Couldn't read version from remote end");
+- }
+- return 0;
+-}
+-
+-int main(int argc, char **argv)
+-{
+- char *commit_id;
+- char *url;
+- int arg = 1;
+-
+- while (arg < argc && argv[arg][0] == '-') {
+- if (argv[arg][1] == 't') {
+- get_tree = 1;
+- } else if (argv[arg][1] == 'c') {
+- get_history = 1;
+- } else if (argv[arg][1] == 'd') {
+- get_delta = 0;
+- } else if (!strcmp(argv[arg], "--recover")) {
+- get_delta = 2;
+- } else if (argv[arg][1] == 'a') {
+- get_all = 1;
+- get_tree = 1;
+- get_history = 1;
+- } else if (argv[arg][1] == 'v') {
+- get_verbosely = 1;
+- }
+- arg++;
+- }
+- if (argc < arg + 2) {
+- usage("git-rpull [-c] [-t] [-a] [-v] [-d] [--recover] commit-id url");
+- return 1;
+- }
+- commit_id = argv[arg];
+- url = argv[arg + 1];
+-
+- if (setup_connection(&fd_in, &fd_out, "git-rpush", url, arg, argv + 1))
+- return 1;
+-
+- if (get_version())
+- return 1;
+-
+- if (pull(commit_id))
+- return 1;
+-
+- return 0;
+-}
+diff a/rpush.c b/rpush.c
+--- a/rpush.c
++++ /dev/null
+@@ -1,104 +0,0 @@
+-#include "cache.h"
+-#include "rsh.h"
+-#include <sys/socket.h>
+-#include <errno.h>
+-
+-unsigned char local_version = 1;
+-unsigned char remote_version = 0;
+-
+-int serve_object(int fd_in, int fd_out) {
+- ssize_t size;
+- int posn = 0;
+- char sha1[20];
+- unsigned long objsize;
+- void *buf;
+- signed char remote;
+- do {
+- size = read(fd_in, sha1 + posn, 20 - posn);
+- if (size < 0) {
+- perror("git-rpush: read ");
+- return -1;
+- }
+- if (!size)
+- return -1;
+- posn += size;
+- } while (posn < 20);
+-
+- /* fprintf(stderr, "Serving %s\n", sha1_to_hex(sha1)); */
+- remote = 0;
+-
+- buf = map_sha1_file(sha1, &objsize);
+-
+- if (!buf) {
+- fprintf(stderr, "git-rpush: could not find %s\n",
+- sha1_to_hex(sha1));
+- remote = -1;
+- }
+-
+- write(fd_out, &remote, 1);
+-
+- if (remote < 0)
+- return 0;
+-
+- posn = 0;
+- do {
+- size = write(fd_out, buf + posn, objsize - posn);
+- if (size <= 0) {
+- if (!size) {
+- fprintf(stderr, "git-rpush: write closed");
+- } else {
+- perror("git-rpush: write ");
+- }
+- return -1;
+- }
+- posn += size;
+- } while (posn < objsize);
+- return 0;
+-}
+-
+-int serve_version(int fd_in, int fd_out)
+-{
+- if (read(fd_in, &remote_version, 1) < 1)
+- return -1;
+- write(fd_out, &local_version, 1);
+- return 0;
+-}
+-
+-void service(int fd_in, int fd_out) {
+- char type;
+- int retval;
+- do {
+- retval = read(fd_in, &type, 1);
+- if (retval < 1) {
+- if (retval < 0)
+- perror("rpush: read ");
+- return;
+- }
+- if (type == 'v' && serve_version(fd_in, fd_out))
+- return;
+- if (type == 'o' && serve_object(fd_in, fd_out))
+- return;
+- } while (1);
+-}
+-
+-int main(int argc, char **argv)
+-{
+- int arg = 1;
+- char *commit_id;
+- char *url;
+- int fd_in, fd_out;
+- while (arg < argc && argv[arg][0] == '-') {
+- arg++;
+- }
+- if (argc < arg + 2) {
+- usage("git-rpush [-c] [-t] [-a] commit-id url");
+- return 1;
+- }
+- commit_id = argv[arg];
+- url = argv[arg + 1];
+- if (setup_connection(&fd_in, &fd_out, "git-rpull", url, arg, argv + 1))
+- return 1;
+-
+- service(fd_in, fd_out);
+- return 0;
+-}
+diff a/ssh-pull.c b/ssh-pull.c
+--- /dev/null
++++ b/ssh-pull.c
+@@ -0,0 +1,83 @@
++#include "cache.h"
++#include "commit.h"
++#include "rsh.h"
++#include "pull.h"
++
++static int fd_in;
++static int fd_out;
++
++static unsigned char remote_version = 0;
++static unsigned char local_version = 1;
++
++int fetch(unsigned char *sha1)
++{
++ int ret;
++ signed char remote;
++ char type = 'o';
++ if (has_sha1_file(sha1))
++ return 0;
++ write(fd_out, &type, 1);
++ write(fd_out, sha1, 20);
++ if (read(fd_in, &remote, 1) < 1)
++ return -1;
++ if (remote < 0)
++ return remote;
++ ret = write_sha1_from_fd(sha1, fd_in);
++ if (!ret)
++ pull_say("got %s\n", sha1_to_hex(sha1));
++ return ret;
++}
++
++int get_version(void)
++{
++ char type = 'v';
++ write(fd_out, &type, 1);
++ write(fd_out, &local_version, 1);
++ if (read(fd_in, &remote_version, 1) < 1) {
++ return error("Couldn't read version from remote end");
++ }
++ return 0;
++}
++
++int main(int argc, char **argv)
++{
++ char *commit_id;
++ char *url;
++ int arg = 1;
++
++ while (arg < argc && argv[arg][0] == '-') {
++ if (argv[arg][1] == 't') {
++ get_tree = 1;
++ } else if (argv[arg][1] == 'c') {
++ get_history = 1;
++ } else if (argv[arg][1] == 'd') {
++ get_delta = 0;
++ } else if (!strcmp(argv[arg], "--recover")) {
++ get_delta = 2;
++ } else if (argv[arg][1] == 'a') {
++ get_all = 1;
++ get_tree = 1;
++ get_history = 1;
++ } else if (argv[arg][1] == 'v') {
++ get_verbosely = 1;
++ }
++ arg++;
++ }
++ if (argc < arg + 2) {
++ usage("git-ssh-pull [-c] [-t] [-a] [-v] [-d] [--recover] commit-id url");
++ return 1;
++ }
++ commit_id = argv[arg];
++ url = argv[arg + 1];
++
++ if (setup_connection(&fd_in, &fd_out, "git-ssh-push", url, arg, argv + 1))
++ return 1;
++
++ if (get_version())
++ return 1;
++
++ if (pull(commit_id))
++ return 1;
++
++ return 0;
++}
+diff a/ssh-push.c b/ssh-push.c
+--- /dev/null
++++ b/ssh-push.c
+@@ -0,0 +1,104 @@
++#include "cache.h"
++#include "rsh.h"
++#include <sys/socket.h>
++#include <errno.h>
++
++unsigned char local_version = 1;
++unsigned char remote_version = 0;
++
++int serve_object(int fd_in, int fd_out) {
++ ssize_t size;
++ int posn = 0;
++ char sha1[20];
++ unsigned long objsize;
++ void *buf;
++ signed char remote;
++ do {
++ size = read(fd_in, sha1 + posn, 20 - posn);
++ if (size < 0) {
++ perror("git-ssh-push: read ");
++ return -1;
++ }
++ if (!size)
++ return -1;
++ posn += size;
++ } while (posn < 20);
++
++ /* fprintf(stderr, "Serving %s\n", sha1_to_hex(sha1)); */
++ remote = 0;
++
++ buf = map_sha1_file(sha1, &objsize);
++
++ if (!buf) {
++ fprintf(stderr, "git-ssh-push: could not find %s\n",
++ sha1_to_hex(sha1));
++ remote = -1;
++ }
++
++ write(fd_out, &remote, 1);
++
++ if (remote < 0)
++ return 0;
++
++ posn = 0;
++ do {
++ size = write(fd_out, buf + posn, objsize - posn);
++ if (size <= 0) {
++ if (!size) {
++ fprintf(stderr, "git-ssh-push: write closed");
++ } else {
++ perror("git-ssh-push: write ");
++ }
++ return -1;
++ }
++ posn += size;
++ } while (posn < objsize);
++ return 0;
++}
++
++int serve_version(int fd_in, int fd_out)
++{
++ if (read(fd_in, &remote_version, 1) < 1)
++ return -1;
++ write(fd_out, &local_version, 1);
++ return 0;
++}
++
++void service(int fd_in, int fd_out) {
++ char type;
++ int retval;
++ do {
++ retval = read(fd_in, &type, 1);
++ if (retval < 1) {
++ if (retval < 0)
++ perror("git-ssh-push: read ");
++ return;
++ }
++ if (type == 'v' && serve_version(fd_in, fd_out))
++ return;
++ if (type == 'o' && serve_object(fd_in, fd_out))
++ return;
++ } while (1);
++}
++
++int main(int argc, char **argv)
++{
++ int arg = 1;
++ char *commit_id;
++ char *url;
++ int fd_in, fd_out;
++ while (arg < argc && argv[arg][0] == '-') {
++ arg++;
++ }
++ if (argc < arg + 2) {
++ usage("git-ssh-push [-c] [-t] [-a] commit-id url");
++ return 1;
++ }
++ commit_id = argv[arg];
++ url = argv[arg + 1];
++ if (setup_connection(&fd_in, &fd_out, "git-ssh-pull", url, arg, argv + 1))
++ return 1;
++
++ service(fd_in, fd_out);
++ return 0;
++}
diff --git a/t/t4100/t-apply-6.expect b/t/t4100/t-apply-6.expect
new file mode 100644
index 0000000..1c343d4
--- /dev/null
+++ b/t/t4100/t-apply-6.expect
@@ -0,0 +1,5 @@
+ Makefile | 2 +-
+ git-fetch-script | 41 +++++++++++++++++++++++++++++++++++++++++
+ git-pull-script | 34 +---------------------------------
+ 3 files changed, 43 insertions(+), 34 deletions(-)
+ create git-fetch-script
diff --git a/t/t4100/t-apply-6.patch b/t/t4100/t-apply-6.patch
new file mode 100644
index 0000000..a72729a
--- /dev/null
+++ b/t/t4100/t-apply-6.patch
@@ -0,0 +1,101 @@
+diff a/Makefile b/Makefile
+--- a/Makefile
++++ b/Makefile
+@@ -20,7 +20,7 @@ INSTALL=install
+
+ SCRIPTS=git-apply-patch-script git-merge-one-file-script git-prune-script \
+ git-pull-script git-tag-script git-resolve-script git-whatchanged \
+- git-deltafy-script
++ git-deltafy-script git-fetch-script
+
+ PROG= git-update-index git-diff-files git-init-db git-write-tree \
+ git-read-tree git-commit-tree git-cat-file git-fsck-cache \
+diff a/git-fetch-script b/git-fetch-script
+--- /dev/null
++++ b/git-fetch-script
+@@ -0,0 +1,41 @@
++#!/bin/sh
++#
++merge_repo=$1
++merge_name=${2:-HEAD}
++
++: ${GIT_DIR=.git}
++: ${GIT_OBJECT_DIRECTORY="${SHA1_FILE_DIRECTORY-"$GIT_DIR/objects"}"}
++
++download_one () {
++ # remote_path="$1" local_file="$2"
++ case "$1" in
++ http://*)
++ wget -q -O "$2" "$1" ;;
++ /*)
++ test -f "$1" && cat >"$2" "$1" ;;
++ *)
++ rsync -L "$1" "$2" ;;
++ esac
++}
++
++download_objects () {
++ # remote_repo="$1" head_sha1="$2"
++ case "$1" in
++ http://*)
++ git-http-pull -a "$2" "$1/"
++ ;;
++ /*)
++ git-local-pull -l -a "$2" "$1/"
++ ;;
++ *)
++ rsync -avz --ignore-existing \
++ "$1/objects/." "$GIT_OBJECT_DIRECTORY"/.
++ ;;
++ esac
++}
++
++echo "Getting remote $merge_name"
++download_one "$merge_repo/$merge_name" "$GIT_DIR"/MERGE_HEAD
++
++echo "Getting object database"
++download_objects "$merge_repo" "$(cat "$GIT_DIR"/MERGE_HEAD)"
+diff a/git-pull-script b/git-pull-script
+--- a/git-pull-script
++++ b/git-pull-script
+@@ -6,39 +6,7 @@ merge_name=${2:-HEAD}
+ : ${GIT_DIR=.git}
+ : ${GIT_OBJECT_DIRECTORY="${SHA1_FILE_DIRECTORY-"$GIT_DIR/objects"}"}
+
+-download_one () {
+- # remote_path="$1" local_file="$2"
+- case "$1" in
+- http://*)
+- wget -q -O "$2" "$1" ;;
+- /*)
+- test -f "$1" && cat >"$2" "$1" ;;
+- *)
+- rsync -L "$1" "$2" ;;
+- esac
+-}
+-
+-download_objects () {
+- # remote_repo="$1" head_sha1="$2"
+- case "$1" in
+- http://*)
+- git-http-pull -a "$2" "$1/"
+- ;;
+- /*)
+- git-local-pull -l -a "$2" "$1/"
+- ;;
+- *)
+- rsync -avz --ignore-existing \
+- "$1/objects/." "$GIT_OBJECT_DIRECTORY"/.
+- ;;
+- esac
+-}
+-
+-echo "Getting remote $merge_name"
+-download_one "$merge_repo/$merge_name" "$GIT_DIR"/MERGE_HEAD
+-
+-echo "Getting object database"
+-download_objects "$merge_repo" "$(cat "$GIT_DIR"/MERGE_HEAD)"
++git-fetch-script "$merge_repo" "$merge_name"
+
+ git-resolve-script \
+ "$(cat "$GIT_DIR"/HEAD)" \
diff --git a/t/t4100/t-apply-7.expect b/t/t4100/t-apply-7.expect
new file mode 100644
index 0000000..1283164
--- /dev/null
+++ b/t/t4100/t-apply-7.expect
@@ -0,0 +1,6 @@
+ Documentation/git-ls-tree.txt | 20 +-
+ ls-tree.c | 333 +++++++++++++++++++++++------------------
+ t/t3100-ls-tree-restrict.sh | 3
+ tree.c | 2
+ tree.h | 1
+ 5 files changed, 199 insertions(+), 160 deletions(-)
diff --git a/t/t4100/t-apply-7.patch b/t/t4100/t-apply-7.patch
new file mode 100644
index 0000000..fa24305
--- /dev/null
+++ b/t/t4100/t-apply-7.patch
@@ -0,0 +1,494 @@
+diff a/Documentation/git-ls-tree.txt b/Documentation/git-ls-tree.txt
+--- a/Documentation/git-ls-tree.txt
++++ b/Documentation/git-ls-tree.txt
+@@ -4,23 +4,26 @@ v0.1, May 2005
+
+ NAME
+ ----
+-git-ls-tree - Displays a tree object in human readable form
++git-ls-tree - Lists the contents of a tree object.
+
+
+ SYNOPSIS
+ --------
+-'git-ls-tree' [-r] [-z] <tree-ish> [paths...]
++'git-ls-tree' [-d] [-r] [-z] <tree-ish> [paths...]
+
+ DESCRIPTION
+ -----------
+-Converts the tree object to a human readable (and script processable)
+-form.
++Lists the contents of a tree object, like what "/bin/ls -a" does
++in the current working directory.
+
+ OPTIONS
+ -------
+ <tree-ish>::
+ Id of a tree.
+
++-d::
++ show only the named tree entry itself, not its children
++
+ -r::
+ recurse into sub-trees
+
+@@ -28,18 +31,19 @@ OPTIONS
+ \0 line termination on output
+
+ paths::
+- Optionally, restrict the output of git-ls-tree to specific
+- paths. Directories will only list their tree blob ids.
+- Implies -r.
++ When paths are given, shows them. Otherwise implicitly
++ uses the root level of the tree as the sole path argument.
++
+
+ Output Format
+ -------------
+- <mode>\t <type>\t <object>\t <file>
++ <mode> SP <type> SP <object> TAB <file>
+
+
+ Author
+ ------
+ Written by Linus Torvalds <torvalds@osdl.org>
++Completely rewritten from scratch by Junio C Hamano <junkio@cox.net>
+
+ Documentation
+ --------------
+diff a/ls-tree.c b/ls-tree.c
+--- a/ls-tree.c
++++ b/ls-tree.c
+@@ -4,188 +4,217 @@
+ * Copyright (C) Linus Torvalds, 2005
+ */
+ #include "cache.h"
++#include "blob.h"
++#include "tree.h"
+
+ static int line_termination = '\n';
+-static int recursive = 0;
++#define LS_RECURSIVE 1
++#define LS_TREE_ONLY 2
++static int ls_options = 0;
+
+-struct path_prefix {
+- struct path_prefix *prev;
+- const char *name;
+-};
+-
+-#define DEBUG(fmt, ...)
+-
+-static int string_path_prefix(char *buff, size_t blen, struct path_prefix *prefix)
+-{
+- int len = 0;
+- if (prefix) {
+- if (prefix->prev) {
+- len = string_path_prefix(buff,blen,prefix->prev);
+- buff += len;
+- blen -= len;
+- if (blen > 0) {
+- *buff = '/';
+- len++;
+- buff++;
+- blen--;
+- }
+- }
+- strncpy(buff,prefix->name,blen);
+- return len + strlen(prefix->name);
+- }
++static struct tree_entry_list root_entry;
+
+- return 0;
++static void prepare_root(unsigned char *sha1)
++{
++ unsigned char rsha[20];
++ unsigned long size;
++ void *buf;
++ struct tree *root_tree;
++
++ buf = read_object_with_reference(sha1, "tree", &size, rsha);
++ free(buf);
++ if (!buf)
++ die("Could not read %s", sha1_to_hex(sha1));
++
++ root_tree = lookup_tree(rsha);
++ if (!root_tree)
++ die("Could not read %s", sha1_to_hex(sha1));
++
++ /* Prepare a fake entry */
++ root_entry.directory = 1;
++ root_entry.executable = root_entry.symlink = 0;
++ root_entry.mode = S_IFDIR;
++ root_entry.name = "";
++ root_entry.item.tree = root_tree;
++ root_entry.parent = NULL;
+ }
+
+-static void print_path_prefix(struct path_prefix *prefix)
++static int prepare_children(struct tree_entry_list *elem)
+ {
+- if (prefix) {
+- if (prefix->prev) {
+- print_path_prefix(prefix->prev);
+- putchar('/');
+- }
+- fputs(prefix->name, stdout);
++ if (!elem->directory)
++ return -1;
++ if (!elem->item.tree->object.parsed) {
++ struct tree_entry_list *e;
++ if (parse_tree(elem->item.tree))
++ return -1;
++ /* Set up the parent link */
++ for (e = elem->item.tree->entries; e; e = e->next)
++ e->parent = elem;
+ }
++ return 0;
+ }
+
+-/*
+- * return:
+- * -1 if prefix is *not* a subset of path
+- * 0 if prefix == path
+- * 1 if prefix is a subset of path
+- */
+-static int pathcmp(const char *path, struct path_prefix *prefix)
+-{
+- char buff[PATH_MAX];
+- int len,slen;
++static struct tree_entry_list *find_entry_0(struct tree_entry_list *elem,
++ const char *path,
++ const char *path_end)
++{
++ const char *ep;
++ int len;
++
++ while (path < path_end) {
++ if (prepare_children(elem))
++ return NULL;
+
+- if (prefix == NULL)
+- return 1;
++ /* In elem->tree->entries, find the one that has name
++ * that matches what is between path and ep.
++ */
++ elem = elem->item.tree->entries;
+
+- len = string_path_prefix(buff, sizeof buff, prefix);
+- slen = strlen(path);
++ ep = strchr(path, '/');
++ if (!ep || path_end <= ep)
++ ep = path_end;
++ len = ep - path;
++
++ while (elem) {
++ if ((strlen(elem->name) == len) &&
++ !strncmp(elem->name, path, len))
++ break;
++ elem = elem->next;
++ }
++ if (path_end <= ep || !elem)
++ return elem;
++ while (*ep == '/' && ep < path_end)
++ ep++;
++ path = ep;
++ }
++ return NULL;
++}
+
+- if (slen < len)
+- return -1;
++static struct tree_entry_list *find_entry(const char *path,
++ const char *path_end)
++{
++ /* Find tree element, descending from root, that
++ * corresponds to the named path, lazily expanding
++ * the tree if possible.
++ */
++ if (path == path_end) {
++ /* Special. This is the root level */
++ return &root_entry;
++ }
++ return find_entry_0(&root_entry, path, path_end);
++}
+
+- if (strncmp(path,buff,len) == 0) {
+- if (slen == len)
+- return 0;
+- else
+- return 1;
++static void show_entry_name(struct tree_entry_list *e)
++{
++ /* This is yucky. The root level is there for
++ * our convenience but we really want to do a
++ * forest.
++ */
++ if (e->parent && e->parent != &root_entry) {
++ show_entry_name(e->parent);
++ putchar('/');
+ }
++ printf("%s", e->name);
++}
+
+- return -1;
+-}
++static const char *entry_type(struct tree_entry_list *e)
++{
++ return (e->directory ? "tree" : "blob");
++}
+
+-/*
+- * match may be NULL, or a *sorted* list of paths
+- */
+-static void list_recursive(void *buffer,
+- const char *type,
+- unsigned long size,
+- struct path_prefix *prefix,
+- char **match, int matches)
+-{
+- struct path_prefix this_prefix;
+- this_prefix.prev = prefix;
+-
+- if (strcmp(type, "tree"))
+- die("expected a 'tree' node");
+-
+- if (matches)
+- recursive = 1;
+-
+- while (size) {
+- int namelen = strlen(buffer)+1;
+- void *eltbuf = NULL;
+- char elttype[20];
+- unsigned long eltsize;
+- unsigned char *sha1 = buffer + namelen;
+- char *path = strchr(buffer, ' ') + 1;
+- unsigned int mode;
+- const char *matched = NULL;
+- int mtype = -1;
+- int mindex;
+-
+- if (size < namelen + 20 || sscanf(buffer, "%o", &mode) != 1)
+- die("corrupt 'tree' file");
+- buffer = sha1 + 20;
+- size -= namelen + 20;
+-
+- this_prefix.name = path;
+- for ( mindex = 0; mindex < matches; mindex++) {
+- mtype = pathcmp(match[mindex],&this_prefix);
+- if (mtype >= 0) {
+- matched = match[mindex];
+- break;
+- }
+- }
++static const char *entry_hex(struct tree_entry_list *e)
++{
++ return sha1_to_hex(e->directory
++ ? e->item.tree->object.sha1
++ : e->item.blob->object.sha1);
++}
+
+- /*
+- * If we're not matching, or if this is an exact match,
+- * print out the info
+- */
+- if (!matches || (matched != NULL && mtype == 0)) {
+- printf("%06o %s %s\t", mode,
+- S_ISDIR(mode) ? "tree" : "blob",
+- sha1_to_hex(sha1));
+- print_path_prefix(&this_prefix);
+- putchar(line_termination);
+- }
++/* forward declaration for mutually recursive routines */
++static int show_entry(struct tree_entry_list *, int);
+
+- if (! recursive || ! S_ISDIR(mode))
+- continue;
++static int show_children(struct tree_entry_list *e, int level)
++{
++ if (prepare_children(e))
++ die("internal error: ls-tree show_children called with non tree");
++ e = e->item.tree->entries;
++ while (e) {
++ show_entry(e, level);
++ e = e->next;
++ }
++ return 0;
++}
+
+- if (matches && ! matched)
+- continue;
++static int show_entry(struct tree_entry_list *e, int level)
++{
++ int err = 0;
+
+- if (! (eltbuf = read_sha1_file(sha1, elttype, &eltsize)) ) {
+- error("cannot read %s", sha1_to_hex(sha1));
+- continue;
+- }
++ if (e != &root_entry) {
++ printf("%06o %s %s ", e->mode, entry_type(e),
++ entry_hex(e));
++ show_entry_name(e);
++ putchar(line_termination);
++ }
+
+- /* If this is an exact directory match, we may have
+- * directory files following this path. Match on them.
+- * Otherwise, we're at a patch subcomponent, and we need
+- * to try to match again.
++ if (e->directory) {
++ /* If this is a directory, we have the following cases:
++ * (1) This is the top-level request (explicit path from the
++ * command line, or "root" if there is no command line).
++ * a. Without any flag. We show direct children. We do not
++ * recurse into them.
++ * b. With -r. We do recurse into children.
++ * c. With -d. We do not recurse into children.
++ * (2) We came here because our caller is either (1-a) or
++ * (1-b).
++ * a. Without any flag. We do not show our children (which
++ * are grandchildren for the original request).
++ * b. With -r. We continue to recurse into our children.
++ * c. With -d. We should not have come here to begin with.
+ */
+- if (mtype == 0)
+- mindex++;
+-
+- list_recursive(eltbuf, elttype, eltsize, &this_prefix, &match[mindex], matches-mindex);
+- free(eltbuf);
++ if (level == 0 && !(ls_options & LS_TREE_ONLY))
++ /* case (1)-a and (1)-b */
++ err = err | show_children(e, level+1);
++ else if (level && ls_options & LS_RECURSIVE)
++ /* case (2)-b */
++ err = err | show_children(e, level+1);
+ }
++ return err;
+ }
+
+-static int qcmp(const void *a, const void *b)
++static int list_one(const char *path, const char *path_end)
+ {
+- return strcmp(*(char **)a, *(char **)b);
++ int err = 0;
++ struct tree_entry_list *e = find_entry(path, path_end);
++ if (!e) {
++ /* traditionally ls-tree does not complain about
++ * missing path. We may change this later to match
++ * what "/bin/ls -a" does, which is to complain.
++ */
++ return err;
++ }
++ err = err | show_entry(e, 0);
++ return err;
+ }
+
+-static int list(unsigned char *sha1,char **path)
++static int list(char **path)
+ {
+- void *buffer;
+- unsigned long size;
+- int npaths;
+-
+- for (npaths = 0; path[npaths] != NULL; npaths++)
+- ;
+-
+- qsort(path,npaths,sizeof(char *),qcmp);
+-
+- buffer = read_object_with_reference(sha1, "tree", &size, NULL);
+- if (!buffer)
+- die("unable to read sha1 file");
+- list_recursive(buffer, "tree", size, NULL, path, npaths);
+- free(buffer);
+- return 0;
++ int i;
++ int err = 0;
++ for (i = 0; path[i]; i++) {
++ int len = strlen(path[i]);
++ while (0 <= len && path[i][len] == '/')
++ len--;
++ err = err | list_one(path[i], path[i] + len);
++ }
++ return err;
+ }
+
+-static const char *ls_tree_usage = "git-ls-tree [-r] [-z] <key> [paths...]";
++static const char *ls_tree_usage =
++ "git-ls-tree [-d] [-r] [-z] <tree-ish> [path...]";
+
+ int main(int argc, char **argv)
+ {
++ static char *path0[] = { "", NULL };
++ char **path;
+ unsigned char sha1[20];
+
+ while (1 < argc && argv[1][0] == '-') {
+@@ -194,7 +223,10 @@ int main(int argc, char **argv)
+ line_termination = 0;
+ break;
+ case 'r':
+- recursive = 1;
++ ls_options |= LS_RECURSIVE;
++ break;
++ case 'd':
++ ls_options |= LS_TREE_ONLY;
+ break;
+ default:
+ usage(ls_tree_usage);
+@@ -206,7 +238,10 @@ int main(int argc, char **argv)
+ usage(ls_tree_usage);
+ if (get_sha1(argv[1], sha1) < 0)
+ usage(ls_tree_usage);
+- if (list(sha1, &argv[2]) < 0)
++
++ path = (argc == 2) ? path0 : (argv + 2);
++ prepare_root(sha1);
++ if (list(path) < 0)
+ die("list failed");
+ return 0;
+ }
+diff a/t/t3100-ls-tree-restrict.sh b/t/t3100-ls-tree-restrict.sh
+--- a/t/t3100-ls-tree-restrict.sh
++++ b/t/t3100-ls-tree-restrict.sh
+@@ -74,8 +74,8 @@ test_expect_success \
+ 'ls-tree filtered' \
+ 'git-ls-tree $tree path1 path0 >current &&
+ cat >expected <<\EOF &&
+-100644 blob X path0
+ 120000 blob X path1
++100644 blob X path0
+ EOF
+ test_output'
+
+@@ -85,7 +85,6 @@ test_expect_success \
+ cat >expected <<\EOF &&
+ 040000 tree X path2
+ 040000 tree X path2/baz
+-100644 blob X path2/baz/b
+ 120000 blob X path2/bazbo
+ 100644 blob X path2/foo
+ EOF
+diff a/tree.c b/tree.c
+--- a/tree.c
++++ b/tree.c
+@@ -133,7 +133,7 @@ int parse_tree_buffer(struct tree *item,
+ }
+ if (obj)
+ add_ref(&item->object, obj);
+-
++ entry->parent = NULL; /* needs to be filled by the user */
+ *list_p = entry;
+ list_p = &entry->next;
+ }
+diff a/tree.h b/tree.h
+--- a/tree.h
++++ b/tree.h
+@@ -16,6 +16,7 @@ struct tree_entry_list {
+ struct tree *tree;
+ struct blob *blob;
+ } item;
++ struct tree_entry_list *parent;
+ };
+
+ struct tree {
diff --git a/t/t4100/t-apply-8.expect b/t/t4100/t-apply-8.expect
new file mode 100644
index 0000000..55a55c3
--- /dev/null
+++ b/t/t4100/t-apply-8.expect
@@ -0,0 +1,2 @@
+ t/t4100-apply-stat.sh | 2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/t/t4100/t-apply-8.patch b/t/t4100/t-apply-8.patch
new file mode 100644
index 0000000..5ca13e6
--- /dev/null
+++ b/t/t4100/t-apply-8.patch
@@ -0,0 +1,11 @@
+diff --git a/t/t4100-apply-stat.sh b/t/t4100-apply-stat.sh
+index be837bb..0798c64 100755
+--- a/t/t4100-apply-stat.sh
++++ b/t/t4100-apply-stat.sh
+@@ -35,4 +35,4 @@ non git (2)
+ non git (3)
+ EOF
+
+-test_done
++test_done
+\ No newline at end of file
diff --git a/t/t4100/t-apply-9.expect b/t/t4100/t-apply-9.expect
new file mode 100644
index 0000000..55a55c3
--- /dev/null
+++ b/t/t4100/t-apply-9.expect
@@ -0,0 +1,2 @@
+ t/t4100-apply-stat.sh | 2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/t/t4100/t-apply-9.patch b/t/t4100/t-apply-9.patch
new file mode 100644
index 0000000..875d57d
--- /dev/null
+++ b/t/t4100/t-apply-9.patch
@@ -0,0 +1,11 @@
+diff --git a/t/t4100-apply-stat.sh b/t/t4100-apply-stat.sh
+index 0798c64..be837bb 100755
+--- a/t/t4100-apply-stat.sh
++++ b/t/t4100-apply-stat.sh
+@@ -35,4 +35,4 @@ non git (2)
+ non git (3)
+ EOF
+
+-test_done
+\ No newline at end of file
++test_done
diff --git a/t/t4101-apply-nonl.sh b/t/t4101-apply-nonl.sh
new file mode 100755
index 0000000..b116919
--- /dev/null
+++ b/t/t4101-apply-nonl.sh
@@ -0,0 +1,33 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+test_description='git apply should handle files with incomplete lines.
+
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# setup
+
+(echo a; echo b) >frotz.0
+(echo a; echo b; echo c) >frotz.1
+(echo a; echo b | tr -d '\012') >frotz.2
+(echo a; echo c; echo b | tr -d '\012') >frotz.3
+
+for i in 0 1 2 3
+do
+ for j in 0 1 2 3
+ do
+ test $i -eq $j && continue
+ cat frotz.$i >frotz
+ test_expect_success "apply diff between $i and $j" '
+ git apply <"$TEST_DIRECTORY"/t4101/diff.$i-$j &&
+ test_cmp frotz.$j frotz
+ '
+ done
+done
+
+test_done
diff --git a/t/t4101/diff.0-1 b/t/t4101/diff.0-1
new file mode 100644
index 0000000..1010a88
--- /dev/null
+++ b/t/t4101/diff.0-1
@@ -0,0 +1,6 @@
+--- a/frotz
++++ b/frotz
+@@ -1,2 +1,3 @@
+ a
+ b
++c
diff --git a/t/t4101/diff.0-2 b/t/t4101/diff.0-2
new file mode 100644
index 0000000..36460a2
--- /dev/null
+++ b/t/t4101/diff.0-2
@@ -0,0 +1,7 @@
+--- a/frotz
++++ b/frotz
+@@ -1,2 +1,2 @@
+ a
+-b
++b
+\ No newline at end of file
diff --git a/t/t4101/diff.0-3 b/t/t4101/diff.0-3
new file mode 100644
index 0000000..b281c43
--- /dev/null
+++ b/t/t4101/diff.0-3
@@ -0,0 +1,8 @@
+--- a/frotz
++++ b/frotz
+@@ -1,2 +1,3 @@
+ a
+-b
++c
++b
+\ No newline at end of file
diff --git a/t/t4101/diff.1-0 b/t/t4101/diff.1-0
new file mode 100644
index 0000000..f0a2e92
--- /dev/null
+++ b/t/t4101/diff.1-0
@@ -0,0 +1,6 @@
+--- a/frotz
++++ b/frotz
+@@ -1,3 +1,2 @@
+ a
+ b
+-c
diff --git a/t/t4101/diff.1-2 b/t/t4101/diff.1-2
new file mode 100644
index 0000000..2a440a5
--- /dev/null
+++ b/t/t4101/diff.1-2
@@ -0,0 +1,8 @@
+--- a/frotz
++++ b/frotz
+@@ -1,3 +1,2 @@
+ a
+-b
+-c
++b
+\ No newline at end of file
diff --git a/t/t4101/diff.1-3 b/t/t4101/diff.1-3
new file mode 100644
index 0000000..61aff97
--- /dev/null
+++ b/t/t4101/diff.1-3
@@ -0,0 +1,8 @@
+--- a/frotz
++++ b/frotz
+@@ -1,3 +1,3 @@
+ a
+-b
+ c
++b
+\ No newline at end of file
diff --git a/t/t4101/diff.2-0 b/t/t4101/diff.2-0
new file mode 100644
index 0000000..c2e71ee
--- /dev/null
+++ b/t/t4101/diff.2-0
@@ -0,0 +1,7 @@
+--- a/frotz
++++ b/frotz
+@@ -1,2 +1,2 @@
+ a
+-b
+\ No newline at end of file
++b
diff --git a/t/t4101/diff.2-1 b/t/t4101/diff.2-1
new file mode 100644
index 0000000..a66d9fd
--- /dev/null
+++ b/t/t4101/diff.2-1
@@ -0,0 +1,8 @@
+--- a/frotz
++++ b/frotz
+@@ -1,2 +1,3 @@
+ a
+-b
+\ No newline at end of file
++b
++c
diff --git a/t/t4101/diff.2-3 b/t/t4101/diff.2-3
new file mode 100644
index 0000000..5633c83
--- /dev/null
+++ b/t/t4101/diff.2-3
@@ -0,0 +1,7 @@
+--- a/frotz
++++ b/frotz
+@@ -1,2 +1,3 @@
+ a
++c
+ b
+\ No newline at end of file
diff --git a/t/t4101/diff.3-0 b/t/t4101/diff.3-0
new file mode 100644
index 0000000..10b1a41
--- /dev/null
+++ b/t/t4101/diff.3-0
@@ -0,0 +1,8 @@
+--- a/frotz
++++ b/frotz
+@@ -1,3 +1,2 @@
+ a
+-c
+-b
+\ No newline at end of file
++b
diff --git a/t/t4101/diff.3-1 b/t/t4101/diff.3-1
new file mode 100644
index 0000000..c799c60
--- /dev/null
+++ b/t/t4101/diff.3-1
@@ -0,0 +1,8 @@
+--- a/frotz
++++ b/frotz
+@@ -1,3 +1,3 @@
+ a
++b
+ c
+-b
+\ No newline at end of file
diff --git a/t/t4101/diff.3-2 b/t/t4101/diff.3-2
new file mode 100644
index 0000000..f8d1ba6
--- /dev/null
+++ b/t/t4101/diff.3-2
@@ -0,0 +1,7 @@
+--- a/frotz
++++ b/frotz
+@@ -1,3 +1,2 @@
+ a
+-c
+ b
+\ No newline at end of file
diff --git a/t/t4102-apply-rename.sh b/t/t4102-apply-rename.sh
new file mode 100755
index 0000000..d1e06fc
--- /dev/null
+++ b/t/t4102-apply-rename.sh
@@ -0,0 +1,59 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+test_description='git apply handling copy/rename patch.
+
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# setup
+
+cat >test-patch <<\EOF
+diff --git a/foo b/bar
+similarity index 47%
+rename from foo
+rename to bar
+--- a/foo
++++ b/bar
+@@ -1 +1 @@
+-This is foo
++This is bar
+EOF
+
+echo 'This is foo' >foo
+chmod +x foo
+
+test_expect_success setup \
+ 'git update-index --add foo'
+
+test_expect_success apply \
+ 'git apply --index --stat --summary --apply test-patch'
+
+test_expect_success FILEMODE validate \
+ 'test -f bar && ls -l bar | grep "^-..x......"'
+
+test_expect_success 'apply reverse' \
+ 'git apply -R --index --stat --summary --apply test-patch &&
+ test "$(cat foo)" = "This is foo"'
+
+cat >test-patch <<\EOF
+diff --git a/foo b/bar
+similarity index 47%
+copy from foo
+copy to bar
+--- a/foo
++++ b/bar
+@@ -1 +1 @@
+-This is foo
++This is bar
+EOF
+
+test_expect_success 'apply copy' \
+ 'git apply --index --stat --summary --apply test-patch &&
+ test "$(cat bar)" = "This is bar" && test "$(cat foo)" = "This is foo"'
+
+test_done
diff --git a/t/t4103-apply-binary.sh b/t/t4103-apply-binary.sh
new file mode 100755
index 0000000..d370ecf
--- /dev/null
+++ b/t/t4103-apply-binary.sh
@@ -0,0 +1,184 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+test_description='git apply handling binary patches
+
+'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ cat >file1 <<-\EOF &&
+ A quick brown fox jumps over the lazy dog.
+ A tiny little penguin runs around in circles.
+ There is a flag with Linux written on it.
+ A slow black-and-white panda just sits there,
+ munching on his bamboo.
+ EOF
+ cat file1 >file2 &&
+ cat file1 >file4 &&
+
+ git update-index --add --remove file1 file2 file4 &&
+ git commit -m "Initial Version" 2>/dev/null &&
+
+ git checkout -b binary &&
+ perl -pe "y/x/\000/" <file1 >file3 &&
+ cat file3 >file4 &&
+ git add file2 &&
+ perl -pe "y/\000/v/" <file3 >file1 &&
+ rm -f file2 &&
+ git update-index --add --remove file1 file2 file3 file4 &&
+ git commit -m "Second Version" &&
+
+ git diff-tree -p main binary >B.diff &&
+ git diff-tree -p -C main binary >C.diff &&
+
+ git diff-tree -p --binary main binary >BF.diff &&
+ git diff-tree -p --binary -C main binary >CF.diff &&
+
+ git diff-tree -p --full-index main binary >B-index.diff &&
+ git diff-tree -p -C --full-index main binary >C-index.diff &&
+
+ git diff-tree -p --binary --no-prefix main binary -- file3 >B0.diff &&
+
+ git init other-repo &&
+ (
+ cd other-repo &&
+ git fetch .. main &&
+ git reset --hard FETCH_HEAD
+ )
+'
+
+test_expect_success 'stat binary diff -- should not fail.' \
+ 'git checkout main &&
+ git apply --stat --summary B.diff'
+
+test_expect_success 'stat binary -p0 diff -- should not fail.' '
+ git checkout main &&
+ git apply --stat -p0 B0.diff
+'
+
+test_expect_success 'stat binary diff (copy) -- should not fail.' \
+ 'git checkout main &&
+ git apply --stat --summary C.diff'
+
+test_expect_success 'check binary diff -- should fail.' \
+ 'git checkout main &&
+ test_must_fail git apply --check B.diff'
+
+test_expect_success 'check binary diff (copy) -- should fail.' \
+ 'git checkout main &&
+ test_must_fail git apply --check C.diff'
+
+test_expect_success \
+ 'check incomplete binary diff with replacement -- should fail.' '
+ git checkout main &&
+ test_must_fail git apply --check --allow-binary-replacement B.diff
+'
+
+test_expect_success \
+ 'check incomplete binary diff with replacement (copy) -- should fail.' '
+ git checkout main &&
+ test_must_fail git apply --check --allow-binary-replacement C.diff
+'
+
+test_expect_success 'check binary diff with replacement.' \
+ 'git checkout main &&
+ git apply --check --allow-binary-replacement BF.diff'
+
+test_expect_success 'check binary diff with replacement (copy).' \
+ 'git checkout main &&
+ git apply --check --allow-binary-replacement CF.diff'
+
+# Now we start applying them.
+
+do_reset () {
+ rm -f file? &&
+ git reset --hard &&
+ git checkout -f main
+}
+
+test_expect_success 'apply binary diff -- should fail.' \
+ 'do_reset &&
+ test_must_fail git apply B.diff'
+
+test_expect_success 'apply binary diff -- should fail.' \
+ 'do_reset &&
+ test_must_fail git apply --index B.diff'
+
+test_expect_success 'apply binary diff (copy) -- should fail.' \
+ 'do_reset &&
+ test_must_fail git apply C.diff'
+
+test_expect_success 'apply binary diff (copy) -- should fail.' \
+ 'do_reset &&
+ test_must_fail git apply --index C.diff'
+
+test_expect_success 'apply binary diff with full-index' '
+ do_reset &&
+ git apply B-index.diff
+'
+
+test_expect_success 'apply binary diff with full-index (copy)' '
+ do_reset &&
+ git apply C-index.diff
+'
+
+test_expect_success 'apply full-index binary diff in new repo' '
+ (cd other-repo &&
+ do_reset &&
+ test_must_fail git apply ../B-index.diff)
+'
+
+test_expect_success 'apply binary diff without replacement.' \
+ 'do_reset &&
+ git apply BF.diff'
+
+test_expect_success 'apply binary diff without replacement (copy).' \
+ 'do_reset &&
+ git apply CF.diff'
+
+test_expect_success 'apply binary diff.' \
+ 'do_reset &&
+ git apply --allow-binary-replacement --index BF.diff &&
+ test -z "$(git diff --name-status binary)"'
+
+test_expect_success 'apply binary diff (copy).' \
+ 'do_reset &&
+ git apply --allow-binary-replacement --index CF.diff &&
+ test -z "$(git diff --name-status binary)"'
+
+test_expect_success 'apply binary -p0 diff' '
+ do_reset &&
+ git apply -p0 --index B0.diff &&
+ test -z "$(git diff --name-status binary -- file3)"
+'
+
+test_expect_success 'reject truncated binary diff' '
+ do_reset &&
+
+ # this length is calculated to get us very close to
+ # the 8192-byte strbuf we will use to read in the patch.
+ test-tool genrandom foo 6205 >file1 &&
+ git diff --binary >patch &&
+
+ # truncate the patch at the second "literal" line,
+ # but exclude the trailing newline. We must use perl
+ # for this, since tools like "sed" cannot reliably
+ # produce output without the trailing newline.
+ perl -pe "
+ if (/^literal/ && \$count++ >= 1) {
+ chomp;
+ print;
+ exit 0;
+ }
+ " <patch >patch.trunc &&
+
+ do_reset &&
+ test_must_fail git apply patch.trunc
+'
+test_done
diff --git a/t/t4104-apply-boundary.sh b/t/t4104-apply-boundary.sh
new file mode 100755
index 0000000..71ef413
--- /dev/null
+++ b/t/t4104-apply-boundary.sh
@@ -0,0 +1,113 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+test_description='git apply boundary tests'
+
+. ./test-lib.sh
+
+L="c d e f g h i j k l m n o p q r s t u v w x"
+
+test_expect_success setup '
+ test_write_lines b $L y >victim &&
+ cat victim >original &&
+ git update-index --add victim &&
+
+ # add to the head
+ test_write_lines a b $L y >victim &&
+ cat victim >add-a-expect &&
+ git diff victim >add-a-patch.with &&
+ git diff --unified=0 >add-a-patch.without &&
+
+ # insert at line two
+ test_write_lines b a $L y >victim &&
+ cat victim >insert-a-expect &&
+ git diff victim >insert-a-patch.with &&
+ git diff --unified=0 >insert-a-patch.without &&
+
+ # modify at the head
+ test_write_lines a $L y >victim &&
+ cat victim >mod-a-expect &&
+ git diff victim >mod-a-patch.with &&
+ git diff --unified=0 >mod-a-patch.without &&
+
+ # remove from the head
+ test_write_lines $L y >victim &&
+ cat victim >del-a-expect &&
+ git diff victim >del-a-patch.with &&
+ git diff --unified=0 >del-a-patch.without &&
+
+ # add to the tail
+ test_write_lines b $L y z >victim &&
+ cat victim >add-z-expect &&
+ git diff victim >add-z-patch.with &&
+ git diff --unified=0 >add-z-patch.without &&
+
+ # modify at the tail
+ test_write_lines b $L z >victim &&
+ cat victim >mod-z-expect &&
+ git diff victim >mod-z-patch.with &&
+ git diff --unified=0 >mod-z-patch.without &&
+
+ # remove from the tail
+ test_write_lines b $L >victim &&
+ cat victim >del-z-expect &&
+ git diff victim >del-z-patch.with &&
+ git diff --unified=0 >del-z-patch.without
+
+ # done
+'
+
+for with in with without
+do
+ case "$with" in
+ with) u= ;;
+ without) u=--unidiff-zero ;;
+ esac
+ for kind in add-a add-z insert-a mod-a mod-z del-a del-z
+ do
+ test_expect_success "apply $kind-patch $with context" '
+ cat original >victim &&
+ git update-index victim &&
+ git apply --index $u "$kind-patch.$with" &&
+ test_cmp "$kind-expect" victim
+ '
+ done
+done
+
+for kind in add-a add-z insert-a mod-a mod-z del-a del-z
+do
+ rm -f $kind-ng.without
+ sed -e "s/^diff --git /diff /" \
+ -e '/^index /d' \
+ <$kind-patch.without >$kind-ng.without
+ test_expect_success "apply non-git $kind-patch without context" '
+ cat original >victim &&
+ git update-index victim &&
+ git apply --unidiff-zero --index "$kind-ng.without" &&
+ test_cmp "$kind-expect" victim
+ '
+done
+
+test_expect_success 'two lines' '
+ >file &&
+ git add file &&
+ echo aaa >file &&
+ git diff >patch &&
+ git add file &&
+ echo bbb >file &&
+ git add file &&
+ test_must_fail git apply --check patch
+'
+
+test_expect_success 'apply patch with 3 context lines matching at end' '
+ test_write_lines a b c d >file &&
+ git add file &&
+ echo e >>file &&
+ git diff >patch &&
+ >file &&
+ test_must_fail git apply patch
+'
+
+test_done
diff --git a/t/t4105-apply-fuzz.sh b/t/t4105-apply-fuzz.sh
new file mode 100755
index 0000000..ed814a8
--- /dev/null
+++ b/t/t4105-apply-fuzz.sh
@@ -0,0 +1,53 @@
+#!/bin/sh
+
+test_description='apply with fuzz and offset'
+
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+dotest () {
+ name="$1" && shift &&
+ test_expect_success "$name" "
+ git checkout-index -f -q -u file &&
+ git apply $* &&
+ test_cmp expect file
+ "
+}
+
+test_expect_success setup '
+
+ test_write_lines 1 2 3 4 5 6 7 8 9 10 11 12 >file &&
+ git update-index --add file &&
+ test_write_lines 1 2 3 4 5 6 7 a b c d e 8 9 10 11 12 >file &&
+ cat file >expect &&
+ git diff >O0.diff &&
+
+ sed -e "s/@@ -5,6 +5,11 @@/@@ -2,6 +2,11 @@/" >O1.diff O0.diff &&
+ sed -e "s/@@ -5,6 +5,11 @@/@@ -7,6 +7,11 @@/" >O2.diff O0.diff &&
+ sed -e "s/@@ -5,6 +5,11 @@/@@ -19,6 +19,11 @@/" >O3.diff O0.diff &&
+
+ sed -e "s/^ 5/ S/" >F0.diff O0.diff &&
+ sed -e "s/^ 5/ S/" >F1.diff O1.diff &&
+ sed -e "s/^ 5/ S/" >F2.diff O2.diff &&
+ sed -e "s/^ 5/ S/" >F3.diff O3.diff
+
+'
+
+dotest 'unmodified patch' O0.diff
+
+dotest 'minus offset' O1.diff
+
+dotest 'plus offset' O2.diff
+
+dotest 'big offset' O3.diff
+
+dotest 'fuzz with no offset' -C2 F0.diff
+
+dotest 'fuzz with minus offset' -C2 F1.diff
+
+dotest 'fuzz with plus offset' -C2 F2.diff
+
+dotest 'fuzz with big offset' -C2 F3.diff
+
+test_done
diff --git a/t/t4106-apply-stdin.sh b/t/t4106-apply-stdin.sh
new file mode 100755
index 0000000..5c150f3
--- /dev/null
+++ b/t/t4106-apply-stdin.sh
@@ -0,0 +1,31 @@
+#!/bin/sh
+
+test_description='git apply --numstat - <patch'
+
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ echo hello >text &&
+ git add text &&
+ echo goodbye >text &&
+ git diff >patch
+'
+
+test_expect_success 'git apply --numstat - < patch' '
+ echo "1 1 text" >expect &&
+ git apply --numstat - <patch >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git apply --numstat - < patch patch' '
+ cat >expect <<-\EOF &&
+ 1 1 text
+ 1 1 text
+ EOF
+ git apply --numstat - < patch patch >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t4107-apply-ignore-whitespace.sh b/t/t4107-apply-ignore-whitespace.sh
new file mode 100755
index 0000000..ac72eea
--- /dev/null
+++ b/t/t4107-apply-ignore-whitespace.sh
@@ -0,0 +1,195 @@
+#!/bin/sh
+#
+# Copyright (c) 2009 Giuseppe Bilotta
+#
+
+test_description='git-apply --ignore-whitespace.
+
+'
+. ./test-lib.sh
+
+# This primes main.c file that indents without using HT at all.
+# Various patches with HT and other spaces are attempted in the test.
+
+cat > patch1.patch <<\EOF
+diff --git a/main.c b/main.c
+new file mode 100644
+--- /dev/null
++++ b/main.c
+@@ -0,0 +1,22 @@
++#include <stdio.h>
++
++void print_int(int num);
++int func(int num);
++
++int main() {
++ int i;
++
++ for (i = 0; i < 10; i++) {
++ print_int(func(i)); /* stuff */
++ }
++
++ return 0;
++}
++
++int func(int num) {
++ return num * num;
++}
++
++void print_int(int num) {
++ printf("%d", num);
++}
+EOF
+
+# Since whitespace is very significant and we want to prevent whitespace
+# mangling when creating this test from a patch, we protect 'fixable'
+# whitespace by replacing spaces with Z and replacing them at patch
+# creation time, hence the sed trick.
+
+# This patch will fail unless whitespace differences are being ignored
+
+sed -e 's/Z/ /g' > patch2.patch <<\EOF
+diff --git a/main.c b/main.c
+--- a/main.c
++++ b/main.c
+@@ -10,6 +10,8 @@
+Z print_int(func(i)); /* stuff */
+Z }
+Z
++ printf("\n");
++
+Z return 0;
+Z}
+Z
+EOF
+
+# This patch will fail even if whitespace differences are being ignored,
+# because of the missing string at EOL. TODO: this testcase should be
+# improved by creating a line that has the same hash with and without
+# the final string.
+
+sed -e 's/Z/ /g' > patch3.patch <<\EOF
+diff --git a/main.c b/main.c
+--- a/main.c
++++ b/main.c
+@@ -10,3 +10,4 @@
+Z for (i = 0; i < 10; i++) {
+Z print_int(func(i));Z
++ /* stuff */
+Z }
+EOF
+
+# This patch will fail even if whitespace differences are being ignored,
+# because of the missing EOL at EOF.
+
+sed -e 's/Z/ /g' > patch4.patch <<\EOF
+diff --git a/main.c b/main.c
+--- a/main.c
++++ b/main.c
+@@ -21,1 +21,1 @@
+- };Z
+\ No newline at end of file
++ };
+EOF
+
+# This patch will fail unless whitespace differences are being ignored.
+
+sed -e 's/Z/ /g' > patch5.patch <<\EOF
+diff --git a/main.c b/main.c
+--- a/main.c
++++ b/main.c
+@@ -2,2 +2,3 @@
+Z void print_int(int num);
++ /* a comment */
+Z int func(int num);
+EOF
+
+# And this is how the final output should be. Patches introduce
+# HTs but the original SP indents are mostly kept.
+
+sed -e 's/T/ /g' > main.c.final <<\EOF
+#include <stdio.h>
+
+void print_int(int num);
+int func(int num);
+
+int main() {
+ int i;
+
+ for (i = 0; i < 10; i++) {
+ print_int(func(i)); /* stuff */
+ }
+
+Tprintf("\n");
+
+ return 0;
+}
+
+int func(int num) {
+ return num * num;
+}
+
+void print_int(int num) {
+ printf("%d", num);
+}
+EOF
+
+test_expect_success 'file creation' '
+ git apply patch1.patch
+'
+
+test_expect_success 'patch2 fails (retab)' '
+ test_must_fail git apply patch2.patch
+'
+
+test_expect_success 'patch2 applies with --ignore-whitespace' '
+ git apply --ignore-whitespace patch2.patch
+'
+
+test_expect_success 'patch2 reverse applies with --ignore-space-change' '
+ git apply -R --ignore-space-change patch2.patch
+'
+
+git config apply.ignorewhitespace change
+
+test_expect_success 'patch2 applies (apply.ignorewhitespace = change)' '
+ git apply patch2.patch &&
+ test_cmp main.c.final main.c
+'
+
+test_expect_success 'patch3 fails (missing string at EOL)' '
+ test_must_fail git apply patch3.patch
+'
+
+test_expect_success 'patch4 fails (missing EOL at EOF)' '
+ test_must_fail git apply patch4.patch
+'
+
+test_expect_success 'patch5 fails (leading whitespace differences matter)' '
+ test_must_fail git apply patch5.patch
+'
+
+test_expect_success 're-create file (with --ignore-whitespace)' '
+ rm -f main.c &&
+ git apply patch1.patch
+'
+
+test_expect_success 'patch5 fails (--no-ignore-whitespace)' '
+ test_must_fail git apply --no-ignore-whitespace patch5.patch
+'
+
+test_expect_success 'apply --ignore-space-change --inaccurate-eof' '
+ echo 1 >file &&
+ git apply --ignore-space-change --inaccurate-eof <<-\EOF &&
+ diff --git a/file b/file
+ --- a/file
+ +++ b/file
+ @@ -1 +1 @@
+ -1
+ +2
+ EOF
+ printf 2 >expect &&
+ test_cmp expect file
+'
+
+test_done
diff --git a/t/t4108-apply-threeway.sh b/t/t4108-apply-threeway.sh
new file mode 100755
index 0000000..c558282
--- /dev/null
+++ b/t/t4108-apply-threeway.sh
@@ -0,0 +1,296 @@
+#!/bin/sh
+
+test_description='git apply --3way'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+print_sanitized_conflicted_diff () {
+ git diff HEAD >diff.raw &&
+ sed -e '
+ /^index /d
+ s/^\(+[<>|][<>|][<>|][<>|]*\) .*/\1/
+ ' diff.raw
+}
+
+test_expect_success setup '
+ test_tick &&
+ test_write_lines 1 2 3 4 5 6 7 >one &&
+ cat one >two &&
+ git add one two &&
+ git commit -m initial &&
+
+ git branch side &&
+
+ test_tick &&
+ test_write_lines 1 two 3 4 5 six 7 >one &&
+ test_write_lines 1 two 3 4 5 6 7 >two &&
+ git commit -a -m main &&
+
+ git checkout side &&
+ test_write_lines 1 2 3 4 five 6 7 >one &&
+ test_write_lines 1 2 3 4 five 6 7 >two &&
+ git commit -a -m side &&
+
+ git checkout main
+'
+
+test_expect_success 'apply without --3way' '
+ git diff side^ side >P.diff &&
+
+ # should fail to apply
+ git reset --hard &&
+ git checkout main^0 &&
+ test_must_fail git apply --index P.diff &&
+ # should leave things intact
+ git diff-files --exit-code &&
+ git diff-index --exit-code --cached HEAD
+'
+
+test_apply_with_3way () {
+ # Merging side should be similar to applying this patch
+ git diff ...side >P.diff &&
+
+ # The corresponding conflicted merge
+ git reset --hard &&
+ git checkout main^0 &&
+ test_must_fail git merge --no-commit side &&
+ git ls-files -s >expect.ls &&
+ print_sanitized_conflicted_diff >expect.diff &&
+
+ # should fail to apply
+ git reset --hard &&
+ git checkout main^0 &&
+ test_must_fail git apply --index --3way P.diff &&
+ git ls-files -s >actual.ls &&
+ print_sanitized_conflicted_diff >actual.diff &&
+
+ # The result should resemble the corresponding merge
+ test_cmp expect.ls actual.ls &&
+ test_cmp expect.diff actual.diff
+}
+
+test_expect_success 'apply with --3way' '
+ test_apply_with_3way
+'
+
+test_expect_success 'apply with --3way with merge.conflictStyle = diff3' '
+ test_config merge.conflictStyle diff3 &&
+ test_apply_with_3way
+'
+
+test_expect_success 'apply with --3way with rerere enabled' '
+ test_config rerere.enabled true &&
+
+ # Merging side should be similar to applying this patch
+ git diff ...side >P.diff &&
+
+ # The corresponding conflicted merge
+ git reset --hard &&
+ git checkout main^0 &&
+ test_must_fail git merge --no-commit side &&
+
+ # Manually resolve and record the resolution
+ test_write_lines 1 two 3 4 five six 7 >one &&
+ git rerere &&
+ cat one >expect &&
+
+ # should fail to apply
+ git reset --hard &&
+ git checkout main^0 &&
+ test_must_fail git apply --index --3way P.diff &&
+
+ # but rerere should have replayed the recorded resolution
+ test_cmp expect one
+'
+
+test_expect_success 'apply -3 with add/add conflict setup' '
+ git reset --hard &&
+
+ git checkout -b adder &&
+ test_write_lines 1 2 3 4 5 6 7 >three &&
+ test_write_lines 1 2 3 4 5 6 7 >four &&
+ git add three four &&
+ git commit -m "add three and four" &&
+
+ git checkout -b another adder^ &&
+ test_write_lines 1 2 3 4 5 6 7 >three &&
+ test_write_lines 1 2 3 four 5 6 7 >four &&
+ git add three four &&
+ git commit -m "add three and four" &&
+
+ # Merging another should be similar to applying this patch
+ git diff adder...another >P.diff &&
+
+ git checkout adder^0 &&
+ test_must_fail git merge --no-commit another &&
+ git ls-files -s >expect.ls &&
+ print_sanitized_conflicted_diff >expect.diff
+'
+
+test_expect_success 'apply -3 with add/add conflict' '
+ # should fail to apply ...
+ git reset --hard &&
+ git checkout adder^0 &&
+ test_must_fail git apply --index --3way P.diff &&
+ # ... and leave conflicts in the index and in the working tree
+ git ls-files -s >actual.ls &&
+ print_sanitized_conflicted_diff >actual.diff &&
+
+ # The result should resemble the corresponding merge
+ test_cmp expect.ls actual.ls &&
+ test_cmp expect.diff actual.diff
+'
+
+test_expect_success 'apply -3 with add/add conflict (dirty working tree)' '
+ # should fail to apply ...
+ git reset --hard &&
+ git checkout adder^0 &&
+ echo >>four &&
+ cat four >four.save &&
+ cat three >three.save &&
+ git ls-files -s >expect.ls &&
+ test_must_fail git apply --index --3way P.diff &&
+ # ... and should not touch anything
+ git ls-files -s >actual.ls &&
+ test_cmp expect.ls actual.ls &&
+ test_cmp four.save four &&
+ test_cmp three.save three
+'
+
+test_expect_success 'apply -3 with ambiguous repeating file' '
+ git reset --hard &&
+ test_write_lines 1 2 1 2 1 2 1 2 1 2 1 >one_two_repeat &&
+ git add one_two_repeat &&
+ git commit -m "init one" &&
+ test_write_lines 1 2 1 2 1 2 1 2 one 2 1 >one_two_repeat &&
+ git commit -a -m "change one" &&
+
+ git diff HEAD~ >Repeat.diff &&
+ git reset --hard HEAD~ &&
+
+ test_write_lines 1 2 1 2 1 2 one 2 1 2 one >one_two_repeat &&
+ git commit -a -m "change surrounding one" &&
+
+ git apply --index --3way Repeat.diff &&
+ test_write_lines 1 2 1 2 1 2 one 2 one 2 one >expect &&
+
+ test_cmp expect one_two_repeat
+'
+
+test_expect_success 'apply with --3way --cached clean apply' '
+ # Merging side should be similar to applying this patch
+ git diff ...side >P.diff &&
+
+ # The corresponding cleanly applied merge
+ git reset --hard &&
+ git checkout main~ &&
+ git merge --no-commit side &&
+ git ls-files -s >expect.ls &&
+
+ # should succeed
+ git reset --hard &&
+ git checkout main~ &&
+ git apply --cached --3way P.diff &&
+ git ls-files -s >actual.ls &&
+ print_sanitized_conflicted_diff >actual.diff &&
+
+ # The cache should resemble the corresponding merge
+ # (both files at stage #0)
+ test_cmp expect.ls actual.ls &&
+ # However the working directory should not change
+ >expect.diff &&
+ test_cmp expect.diff actual.diff
+'
+
+test_expect_success 'apply with --3way --cached and conflicts' '
+ # Merging side should be similar to applying this patch
+ git diff ...side >P.diff &&
+
+ # The corresponding conflicted merge
+ git reset --hard &&
+ git checkout main^0 &&
+ test_must_fail git merge --no-commit side &&
+ git ls-files -s >expect.ls &&
+
+ # should fail to apply
+ git reset --hard &&
+ git checkout main^0 &&
+ test_must_fail git apply --cached --3way P.diff &&
+ git ls-files -s >actual.ls &&
+ print_sanitized_conflicted_diff >actual.diff &&
+
+ # The cache should resemble the corresponding merge
+ # (one file at stage #0, one file at stages #1 #2 #3)
+ test_cmp expect.ls actual.ls &&
+ # However the working directory should not change
+ >expect.diff &&
+ test_cmp expect.diff actual.diff
+'
+
+test_expect_success 'apply binary file patch' '
+ git reset --hard main &&
+ cp "$TEST_DIRECTORY/test-binary-1.png" bin.png &&
+ git add bin.png &&
+ git commit -m "add binary file" &&
+
+ cp "$TEST_DIRECTORY/test-binary-2.png" bin.png &&
+
+ git diff --binary >bin.diff &&
+ git reset --hard &&
+
+ # Apply must succeed.
+ git apply bin.diff
+'
+
+test_expect_success 'apply binary file patch with 3way' '
+ git reset --hard main &&
+ cp "$TEST_DIRECTORY/test-binary-1.png" bin.png &&
+ git add bin.png &&
+ git commit -m "add binary file" &&
+
+ cp "$TEST_DIRECTORY/test-binary-2.png" bin.png &&
+
+ git diff --binary >bin.diff &&
+ git reset --hard &&
+
+ # Apply must succeed.
+ git apply --3way --index bin.diff
+'
+
+test_expect_success 'apply full-index patch with 3way' '
+ git reset --hard main &&
+ cp "$TEST_DIRECTORY/test-binary-1.png" bin.png &&
+ git add bin.png &&
+ git commit -m "add binary file" &&
+
+ cp "$TEST_DIRECTORY/test-binary-2.png" bin.png &&
+
+ git diff --full-index >bin.diff &&
+ git reset --hard &&
+
+ # Apply must succeed.
+ git apply --3way --index bin.diff
+'
+
+test_expect_success 'apply delete then new patch with 3way' '
+ git reset --hard main &&
+ test_write_lines 2 > delnew &&
+ git add delnew &&
+ git diff --cached >> new.patch &&
+ git reset --hard &&
+ test_write_lines 1 > delnew &&
+ git add delnew &&
+ git commit -m "delnew" &&
+ rm delnew &&
+ git diff >> delete-then-new.patch &&
+ cat new.patch >> delete-then-new.patch &&
+
+ git checkout -- . &&
+ # Apply must succeed.
+ git apply --3way delete-then-new.patch
+'
+
+test_done
diff --git a/t/t4109-apply-multifrag.sh b/t/t4109-apply-multifrag.sh
new file mode 100755
index 0000000..4dc6d8e
--- /dev/null
+++ b/t/t4109-apply-multifrag.sh
@@ -0,0 +1,37 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+# Copyright (c) 2005 Robert Fitzsimons
+#
+
+test_description='git apply test patches with multiple fragments.'
+
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+cp "$TEST_DIRECTORY/t4109/patch1.patch" .
+cp "$TEST_DIRECTORY/t4109/patch2.patch" .
+cp "$TEST_DIRECTORY/t4109/patch3.patch" .
+cp "$TEST_DIRECTORY/t4109/patch4.patch" .
+
+test_expect_success 'git apply (1)' '
+ git apply patch1.patch patch2.patch &&
+ test_cmp "$TEST_DIRECTORY/t4109/expect-1" main.c
+'
+rm -f main.c
+
+test_expect_success 'git apply (2)' '
+ git apply patch1.patch patch2.patch patch3.patch &&
+ test_cmp "$TEST_DIRECTORY/t4109/expect-2" main.c
+'
+rm -f main.c
+
+test_expect_success 'git apply (3)' '
+ git apply patch1.patch patch4.patch &&
+ test_cmp "$TEST_DIRECTORY/t4109/expect-3" main.c
+'
+mv main.c main.c.git
+
+test_done
+
diff --git a/t/t4109/expect-1 b/t/t4109/expect-1
new file mode 100644
index 0000000..1db5ff1
--- /dev/null
+++ b/t/t4109/expect-1
@@ -0,0 +1,31 @@
+#include <stdlib.h>
+#include <stdio.h>
+
+int func(int num);
+void print_int(int num);
+void print_ln();
+
+int main() {
+ int i;
+
+ for (i = 0; i < 10; i++) {
+ print_int(func(i));
+ }
+
+ print_ln();
+
+ return 0;
+}
+
+int func(int num) {
+ return num * num;
+}
+
+void print_int(int num) {
+ printf("%d", num);
+}
+
+void print_ln() {
+ printf("\n");
+}
+
diff --git a/t/t4109/expect-2 b/t/t4109/expect-2
new file mode 100644
index 0000000..bc52924
--- /dev/null
+++ b/t/t4109/expect-2
@@ -0,0 +1,23 @@
+#include <stdio.h>
+
+int func(int num);
+void print_int(int num);
+
+int main() {
+ int i;
+
+ for (i = 0; i < 10; i++) {
+ print_int(func(i));
+ }
+
+ return 0;
+}
+
+int func(int num) {
+ return num * num;
+}
+
+void print_int(int num) {
+ printf("%d", num);
+}
+
diff --git a/t/t4109/expect-3 b/t/t4109/expect-3
new file mode 100644
index 0000000..cd2a475
--- /dev/null
+++ b/t/t4109/expect-3
@@ -0,0 +1,24 @@
+#include <stdio.h>
+
+int func(int num);
+int func2(int num);
+
+int main() {
+ int i;
+
+ for (i = 0; i < 10; i++) {
+ printf("%d", func(i));
+ printf("%d", func3(i));
+ }
+
+ return 0;
+}
+
+int func(int num) {
+ return num * num;
+}
+
+int func2(int num) {
+ return num * num * num;
+}
+
diff --git a/t/t4109/patch1.patch b/t/t4109/patch1.patch
new file mode 100644
index 0000000..1d411fc
--- /dev/null
+++ b/t/t4109/patch1.patch
@@ -0,0 +1,28 @@
+diff --git a/main.c b/main.c
+new file mode 100644
+--- /dev/null
++++ b/main.c
+@@ -0,0 +1,23 @@
++#include <stdio.h>
++
++int func(int num);
++void print_int(int num);
++
++int main() {
++ int i;
++
++ for (i = 0; i < 10; i++) {
++ print_int(func(i));
++ }
++
++ return 0;
++}
++
++int func(int num) {
++ return num * num;
++}
++
++void print_int(int num) {
++ printf("%d", num);
++}
++
diff --git a/t/t4109/patch2.patch b/t/t4109/patch2.patch
new file mode 100644
index 0000000..8c6b06d
--- /dev/null
+++ b/t/t4109/patch2.patch
@@ -0,0 +1,30 @@
+diff --git a/main.c b/main.c
+--- a/main.c
++++ b/main.c
+@@ -1,7 +1,9 @@
++#include <stdlib.h>
+ #include <stdio.h>
+
+ int func(int num);
+ void print_int(int num);
++void print_ln();
+
+ int main() {
+ int i;
+@@ -10,6 +12,8 @@
+ print_int(func(i));
+ }
+
++ print_ln();
++
+ return 0;
+ }
+
+@@ -21,3 +25,7 @@
+ printf("%d", num);
+ }
+
++void print_ln() {
++ printf("\n");
++}
++
diff --git a/t/t4109/patch3.patch b/t/t4109/patch3.patch
new file mode 100644
index 0000000..d696c55
--- /dev/null
+++ b/t/t4109/patch3.patch
@@ -0,0 +1,31 @@
+cat > patch3.patch <<\EOF
+diff --git a/main.c b/main.c
+--- a/main.c
++++ b/main.c
+@@ -1,9 +1,7 @@
+-#include <stdlib.h>
+ #include <stdio.h>
+
+ int func(int num);
+ void print_int(int num);
+-void print_ln();
+
+ int main() {
+ int i;
+@@ -12,8 +10,6 @@
+ print_int(func(i));
+ }
+
+- print_ln();
+-
+ return 0;
+ }
+
+@@ -25,7 +21,3 @@
+ printf("%d", num);
+ }
+
+-void print_ln() {
+- printf("\n");
+-}
+-
diff --git a/t/t4109/patch4.patch b/t/t4109/patch4.patch
new file mode 100644
index 0000000..4b08590
--- /dev/null
+++ b/t/t4109/patch4.patch
@@ -0,0 +1,30 @@
+diff --git a/main.c b/main.c
+--- a/main.c
++++ b/main.c
+@@ -1,13 +1,14 @@
+ #include <stdio.h>
+
+ int func(int num);
+-void print_int(int num);
++int func2(int num);
+
+ int main() {
+ int i;
+
+ for (i = 0; i < 10; i++) {
+- print_int(func(i));
++ printf("%d", func(i));
++ printf("%d", func3(i));
+ }
+
+ return 0;
+@@ -17,7 +18,7 @@
+ return num * num;
+ }
+
+-void print_int(int num) {
+- printf("%d", num);
++int func2(int num) {
++ return num * num * num;
+ }
+
diff --git a/t/t4110-apply-scan.sh b/t/t4110-apply-scan.sh
new file mode 100755
index 0000000..266302a
--- /dev/null
+++ b/t/t4110-apply-scan.sh
@@ -0,0 +1,24 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+# Copyright (c) 2005 Robert Fitzsimons
+#
+
+test_description='git apply test for patches which require scanning forwards and backwards.
+
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'git apply scan' '
+ git apply \
+ "$TEST_DIRECTORY/t4110/patch1.patch" \
+ "$TEST_DIRECTORY/t4110/patch2.patch" \
+ "$TEST_DIRECTORY/t4110/patch3.patch" \
+ "$TEST_DIRECTORY/t4110/patch4.patch" \
+ "$TEST_DIRECTORY/t4110/patch5.patch" &&
+ test_cmp new.txt "$TEST_DIRECTORY/t4110/expect"
+'
+
+test_done
diff --git a/t/t4110/expect b/t/t4110/expect
new file mode 100644
index 0000000..87cc493
--- /dev/null
+++ b/t/t4110/expect
@@ -0,0 +1,20 @@
+a1
+a11
+a111
+a1111
+b1
+b11
+b111
+b1111
+b2
+b22
+b222
+b2222
+c1
+c11
+c111
+c1111
+c2
+c22
+c222
+c2222
diff --git a/t/t4110/patch1.patch b/t/t4110/patch1.patch
new file mode 100644
index 0000000..5613908
--- /dev/null
+++ b/t/t4110/patch1.patch
@@ -0,0 +1,17 @@
+diff --git a/new.txt b/new.txt
+new file mode 100644
+--- /dev/null
++++ b/new.txt
+@@ -0,0 +1,12 @@
++a1
++a11
++a111
++a1111
++b1
++b11
++b111
++b1111
++c1
++c11
++c111
++c1111
diff --git a/t/t4110/patch2.patch b/t/t4110/patch2.patch
new file mode 100644
index 0000000..0497424
--- /dev/null
+++ b/t/t4110/patch2.patch
@@ -0,0 +1,11 @@
+diff --git a/new.txt b/new.txt
+--- a/new.txt
++++ b/new.txt
+@@ -1,7 +1,3 @@
+-a1
+-a11
+-a111
+-a1111
+ b1
+ b11
+ b111
diff --git a/t/t4110/patch3.patch b/t/t4110/patch3.patch
new file mode 100644
index 0000000..26bd442
--- /dev/null
+++ b/t/t4110/patch3.patch
@@ -0,0 +1,14 @@
+diff --git a/new.txt b/new.txt
+--- a/new.txt
++++ b/new.txt
+@@ -6,6 +6,10 @@
+ b11
+ b111
+ b1111
++b2
++b22
++b222
++b2222
+ c1
+ c11
+ c111
diff --git a/t/t4110/patch4.patch b/t/t4110/patch4.patch
new file mode 100644
index 0000000..9ffb9c2
--- /dev/null
+++ b/t/t4110/patch4.patch
@@ -0,0 +1,11 @@
+diff --git a/new.txt b/new.txt
+--- a/new.txt
++++ b/new.txt
+@@ -1,3 +1,7 @@
++a1
++a11
++a111
++a1111
+ b1
+ b11
+ b111
diff --git a/t/t4110/patch5.patch b/t/t4110/patch5.patch
new file mode 100644
index 0000000..c5ac691
--- /dev/null
+++ b/t/t4110/patch5.patch
@@ -0,0 +1,11 @@
+diff --git a/new.txt b/new.txt
+--- a/new.txt
++++ b/new.txt
+@@ -10,3 +10,7 @@
+ c11
+ c111
+ c1111
++c2
++c22
++c222
++c2222
diff --git a/t/t4111-apply-subdir.sh b/t/t4111-apply-subdir.sh
new file mode 100755
index 0000000..e9a87d7
--- /dev/null
+++ b/t/t4111-apply-subdir.sh
@@ -0,0 +1,157 @@
+#!/bin/sh
+
+test_description='patching from inconvenient places'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ cat >patch <<-\EOF &&
+ diff file.orig file
+ --- a/file.orig
+ +++ b/file
+ @@ -1 +1,2 @@
+ 1
+ +2
+ EOF
+ patch="$(pwd)/patch" &&
+
+ echo 1 >preimage &&
+ printf "%s\n" 1 2 >postimage &&
+ echo 3 >other &&
+
+ test_tick &&
+ git commit --allow-empty -m basis
+'
+
+test_expect_success 'setup: subdir' '
+ reset_subdir() {
+ git reset &&
+ mkdir -p sub/dir/b &&
+ mkdir -p objects &&
+ cp "$1" file &&
+ cp "$1" objects/file &&
+ cp "$1" sub/dir/file &&
+ cp "$1" sub/dir/b/file &&
+ git add file sub/dir/file sub/dir/b/file objects/file &&
+ cp "$2" file &&
+ cp "$2" sub/dir/file &&
+ cp "$2" sub/dir/b/file &&
+ cp "$2" objects/file &&
+ test_might_fail git update-index --refresh -q
+ }
+'
+
+test_expect_success 'apply from subdir of toplevel' '
+ cp postimage expected &&
+ reset_subdir other preimage &&
+ (
+ cd sub/dir &&
+ git apply "$patch"
+ ) &&
+ test_cmp expected sub/dir/file
+'
+
+test_expect_success 'apply --cached from subdir of toplevel' '
+ cp postimage expected &&
+ cp other expected.working &&
+ reset_subdir preimage other &&
+ (
+ cd sub/dir &&
+ git apply --cached "$patch"
+ ) &&
+ git show :sub/dir/file >actual &&
+ test_cmp expected actual &&
+ test_cmp expected.working sub/dir/file
+'
+
+test_expect_success 'apply --index from subdir of toplevel' '
+ cp postimage expected &&
+ reset_subdir preimage other &&
+ (
+ cd sub/dir &&
+ test_must_fail git apply --index "$patch"
+ ) &&
+ reset_subdir other preimage &&
+ (
+ cd sub/dir &&
+ test_must_fail git apply --index "$patch"
+ ) &&
+ reset_subdir preimage preimage &&
+ (
+ cd sub/dir &&
+ git apply --index "$patch"
+ ) &&
+ git show :sub/dir/file >actual &&
+ test_cmp expected actual &&
+ test_cmp expected sub/dir/file
+'
+
+test_expect_success 'apply half-broken patch from subdir of toplevel' '
+ (
+ cd sub/dir &&
+ test_must_fail git apply <<-EOF
+ --- sub/dir/file
+ +++ sub/dir/file
+ @@ -1,0 +1,0 @@
+ --- file_in_root
+ +++ file_in_root
+ @@ -1,0 +1,0 @@
+ EOF
+ )
+'
+
+test_expect_success 'apply from .git dir' '
+ cp postimage expected &&
+ cp preimage .git/file &&
+ cp preimage .git/objects/file &&
+ (
+ cd .git &&
+ git apply "$patch"
+ ) &&
+ test_cmp expected .git/file
+'
+
+test_expect_success 'apply from subdir of .git dir' '
+ cp postimage expected &&
+ cp preimage .git/file &&
+ cp preimage .git/objects/file &&
+ (
+ cd .git/objects &&
+ git apply "$patch"
+ ) &&
+ test_cmp expected .git/objects/file
+'
+
+test_expect_success 'apply --cached from .git dir' '
+ cp postimage expected &&
+ cp other expected.working &&
+ cp other .git/file &&
+ reset_subdir preimage other &&
+ (
+ cd .git &&
+ git apply --cached "$patch"
+ ) &&
+ git show :file >actual &&
+ test_cmp expected actual &&
+ test_cmp expected.working file &&
+ test_cmp expected.working .git/file
+'
+
+test_expect_success 'apply --cached from subdir of .git dir' '
+ cp postimage expected &&
+ cp preimage expected.subdir &&
+ cp other .git/file &&
+ cp other .git/objects/file &&
+ reset_subdir preimage other &&
+ (
+ cd .git/objects &&
+ git apply --cached "$patch"
+ ) &&
+ git show :file >actual &&
+ git show :objects/file >actual.subdir &&
+ test_cmp expected actual &&
+ test_cmp expected.subdir actual.subdir
+'
+
+test_done
diff --git a/t/t4112-apply-renames.sh b/t/t4112-apply-renames.sh
new file mode 100755
index 0000000..d53aa42
--- /dev/null
+++ b/t/t4112-apply-renames.sh
@@ -0,0 +1,146 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+test_description='git apply should not get confused with rename/copy.
+
+'
+
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# setup
+
+mkdir -p klibc/arch/x86_64/include/klibc
+
+cat >klibc/arch/x86_64/include/klibc/archsetjmp.h <<\EOF
+/*
+ * arch/x86_64/include/klibc/archsetjmp.h
+ */
+
+#ifndef _KLIBC_ARCHSETJMP_H
+#define _KLIBC_ARCHSETJMP_H
+
+struct __jmp_buf {
+ unsigned long __rbx;
+ unsigned long __rsp;
+ unsigned long __rbp;
+ unsigned long __r12;
+ unsigned long __r13;
+ unsigned long __r14;
+ unsigned long __r15;
+ unsigned long __rip;
+};
+
+typedef struct __jmp_buf jmp_buf[1];
+
+#endif /* _SETJMP_H */
+EOF
+cat >klibc/README <<\EOF
+This is a simple readme file.
+EOF
+
+cat >patch <<\EOF
+diff --git a/klibc/arch/x86_64/include/klibc/archsetjmp.h b/include/arch/cris/klibc/archsetjmp.h
+similarity index 76%
+copy from klibc/arch/x86_64/include/klibc/archsetjmp.h
+copy to include/arch/cris/klibc/archsetjmp.h
+--- a/klibc/arch/x86_64/include/klibc/archsetjmp.h
++++ b/include/arch/cris/klibc/archsetjmp.h
+@@ -1,21 +1,24 @@
+ /*
+- * arch/x86_64/include/klibc/archsetjmp.h
++ * arch/cris/include/klibc/archsetjmp.h
+ */
+
+ #ifndef _KLIBC_ARCHSETJMP_H
+ #define _KLIBC_ARCHSETJMP_H
+
+ struct __jmp_buf {
+- unsigned long __rbx;
+- unsigned long __rsp;
+- unsigned long __rbp;
+- unsigned long __r12;
+- unsigned long __r13;
+- unsigned long __r14;
+- unsigned long __r15;
+- unsigned long __rip;
++ unsigned long __r0;
++ unsigned long __r1;
++ unsigned long __r2;
++ unsigned long __r3;
++ unsigned long __r4;
++ unsigned long __r5;
++ unsigned long __r6;
++ unsigned long __r7;
++ unsigned long __r8;
++ unsigned long __sp;
++ unsigned long __srp;
+ };
+
+ typedef struct __jmp_buf jmp_buf[1];
+
+-#endif /* _SETJMP_H */
++#endif /* _KLIBC_ARCHSETJMP_H */
+diff --git a/klibc/arch/x86_64/include/klibc/archsetjmp.h b/include/arch/m32r/klibc/archsetjmp.h
+similarity index 66%
+rename from klibc/arch/x86_64/include/klibc/archsetjmp.h
+rename to include/arch/m32r/klibc/archsetjmp.h
+--- a/klibc/arch/x86_64/include/klibc/archsetjmp.h
++++ b/include/arch/m32r/klibc/archsetjmp.h
+@@ -1,21 +1,21 @@
+ /*
+- * arch/x86_64/include/klibc/archsetjmp.h
++ * arch/m32r/include/klibc/archsetjmp.h
+ */
+
+ #ifndef _KLIBC_ARCHSETJMP_H
+ #define _KLIBC_ARCHSETJMP_H
+
+ struct __jmp_buf {
+- unsigned long __rbx;
+- unsigned long __rsp;
+- unsigned long __rbp;
++ unsigned long __r8;
++ unsigned long __r9;
++ unsigned long __r10;
++ unsigned long __r11;
+ unsigned long __r12;
+ unsigned long __r13;
+ unsigned long __r14;
+ unsigned long __r15;
+- unsigned long __rip;
+ };
+
+ typedef struct __jmp_buf jmp_buf[1];
+
+-#endif /* _SETJMP_H */
++#endif /* _KLIBC_ARCHSETJMP_H */
+diff --git a/klibc/README b/klibc/README
+--- a/klibc/README
++++ b/klibc/README
+@@ -1,1 +1,4 @@
+ This is a simple readme file.
++And we add a few
++lines at the
++end of it.
+diff --git a/klibc/README b/klibc/arch/README
+copy from klibc/README
+copy to klibc/arch/README
+--- a/klibc/README
++++ b/klibc/arch/README
+@@ -1,1 +1,3 @@
+ This is a simple readme file.
++And we copy it to one level down, and
++add a few lines at the end of it.
+EOF
+
+find klibc -type f -print | xargs git update-index --add --
+
+test_expect_success 'check rename/copy patch' 'git apply --check patch'
+
+test_expect_success 'apply rename/copy patch' 'git apply --index patch'
+
+test_done
diff --git a/t/t4113-apply-ending.sh b/t/t4113-apply-ending.sh
new file mode 100755
index 0000000..66fa515
--- /dev/null
+++ b/t/t4113-apply-ending.sh
@@ -0,0 +1,53 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Catalin Marinas
+#
+
+test_description='git apply trying to add an ending line.
+
+'
+. ./test-lib.sh
+
+# setup
+
+cat >test-patch <<\EOF
+diff --git a/file b/file
+--- a/file
++++ b/file
+@@ -1,2 +1,3 @@
+ a
+ b
++c
+EOF
+
+echo 'a' >file
+echo 'b' >>file
+echo 'c' >>file
+
+test_expect_success setup \
+ 'git update-index --add file'
+
+# test
+
+test_expect_success 'apply at the end' \
+ 'test_must_fail git apply --index test-patch'
+
+cat >test-patch <<\EOF
+diff a/file b/file
+--- a/file
++++ b/file
+@@ -1,2 +1,3 @@
++a
+ b
+ c
+EOF
+
+echo >file 'a
+b
+c'
+git update-index file
+
+test_expect_success 'apply at the beginning' \
+ 'test_must_fail git apply --index test-patch'
+
+test_done
diff --git a/t/t4114-apply-typechange.sh b/t/t4114-apply-typechange.sh
new file mode 100755
index 0000000..8ff3640
--- /dev/null
+++ b/t/t4114-apply-typechange.sh
@@ -0,0 +1,130 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Eric Wong
+#
+
+test_description='git apply should not get confused with type changes.
+
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup repository and commits' '
+ echo "hello world" > foo &&
+ echo "hi planet" > bar &&
+ git update-index --add foo bar &&
+ git commit -m initial &&
+ git branch initial &&
+ rm -f foo &&
+ test_ln_s_add bar foo &&
+ git commit -m "foo symlinked to bar" &&
+ git branch foo-symlinked-to-bar &&
+ git rm -f foo &&
+ echo "how far is the sun?" > foo &&
+ git update-index --add foo &&
+ git commit -m "foo back to file" &&
+ git branch foo-back-to-file &&
+ printf "\0" > foo &&
+ git update-index foo &&
+ git commit -m "foo becomes binary" &&
+ git branch foo-becomes-binary &&
+ rm -f foo &&
+ git update-index --remove foo &&
+ mkdir foo &&
+ echo "if only I knew" > foo/baz &&
+ git update-index --add foo/baz &&
+ git commit -m "foo becomes a directory" &&
+ git branch "foo-becomes-a-directory" &&
+ echo "hello world" > foo/baz &&
+ git update-index foo/baz &&
+ git commit -m "foo/baz is the original foo" &&
+ git branch foo-baz-renamed-from-foo
+ '
+
+test_expect_success 'file renamed from foo to foo/baz' '
+ git checkout -f initial &&
+ git diff-tree -M -p HEAD foo-baz-renamed-from-foo > patch &&
+ git apply --index < patch
+ '
+test_debug 'cat patch'
+
+
+test_expect_success 'file renamed from foo/baz to foo' '
+ git checkout -f foo-baz-renamed-from-foo &&
+ git diff-tree -M -p HEAD initial > patch &&
+ git apply --index < patch
+ '
+test_debug 'cat patch'
+
+
+test_expect_success 'directory becomes file' '
+ git checkout -f foo-becomes-a-directory &&
+ git diff-tree -p HEAD initial > patch &&
+ git apply --index < patch
+ '
+test_debug 'cat patch'
+
+
+test_expect_success 'file becomes directory' '
+ git checkout -f initial &&
+ git diff-tree -p HEAD foo-becomes-a-directory > patch &&
+ git apply --index < patch
+ '
+test_debug 'cat patch'
+
+
+test_expect_success 'file becomes symlink' '
+ git checkout -f initial &&
+ git diff-tree -p HEAD foo-symlinked-to-bar > patch &&
+ git apply --index < patch
+ '
+test_debug 'cat patch'
+
+
+test_expect_success 'symlink becomes file' '
+ git checkout -f foo-symlinked-to-bar &&
+ git diff-tree -p HEAD foo-back-to-file > patch &&
+ git apply --index < patch
+ '
+test_debug 'cat patch'
+
+test_expect_success 'symlink becomes file, in reverse' '
+ git checkout -f foo-symlinked-to-bar &&
+ git diff-tree -p HEAD foo-back-to-file > patch &&
+ git checkout foo-back-to-file &&
+ git apply -R --index < patch
+ '
+
+test_expect_success 'binary file becomes symlink' '
+ git checkout -f foo-becomes-binary &&
+ git diff-tree -p --binary HEAD foo-symlinked-to-bar > patch &&
+ git apply --index < patch
+ '
+test_debug 'cat patch'
+
+test_expect_success 'symlink becomes binary file' '
+ git checkout -f foo-symlinked-to-bar &&
+ git diff-tree -p --binary HEAD foo-becomes-binary > patch &&
+ git apply --index < patch
+ '
+test_debug 'cat patch'
+
+
+test_expect_success 'symlink becomes directory' '
+ git checkout -f foo-symlinked-to-bar &&
+ git diff-tree -p HEAD foo-becomes-a-directory > patch &&
+ git apply --index < patch
+ '
+test_debug 'cat patch'
+
+
+test_expect_success 'directory becomes symlink' '
+ git checkout -f foo-becomes-a-directory &&
+ git diff-tree -p HEAD foo-symlinked-to-bar > patch &&
+ git apply --index < patch
+ '
+test_debug 'cat patch'
+
+
+test_done
diff --git a/t/t4115-apply-symlink.sh b/t/t4115-apply-symlink.sh
new file mode 100755
index 0000000..cbef0a5
--- /dev/null
+++ b/t/t4115-apply-symlink.sh
@@ -0,0 +1,144 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+test_description='git apply symlinks and partial files
+
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+
+ test_ln_s_add path1/path2/path3/path4/path5 link1 &&
+ git commit -m initial &&
+
+ git branch side &&
+
+ rm -f link? &&
+
+ test_ln_s_add htap6 link1 &&
+ git commit -m second &&
+
+ git diff-tree -p HEAD^ HEAD >patch &&
+ git apply --stat --summary patch
+
+'
+
+test_expect_success SYMLINKS 'apply symlink patch' '
+
+ git checkout side &&
+ git apply patch &&
+ git diff-files -p >patched &&
+ test_cmp patch patched
+
+'
+
+test_expect_success 'apply --index symlink patch' '
+
+ git checkout -f side &&
+ git apply --index patch &&
+ git diff-index --cached -p HEAD >patched &&
+ test_cmp patch patched
+
+'
+
+test_expect_success 'symlink setup' '
+ ln -s .git symlink &&
+ git add symlink &&
+ git commit -m "add symlink"
+'
+
+test_expect_success SYMLINKS 'symlink escape when creating new files' '
+ test_when_finished "git reset --hard && git clean -dfx" &&
+
+ cat >patch <<-EOF &&
+ diff --git a/symlink b/renamed-symlink
+ similarity index 100%
+ rename from symlink
+ rename to renamed-symlink
+ --
+ diff --git /dev/null b/renamed-symlink/create-me
+ new file mode 100644
+ index 0000000..039727e
+ --- /dev/null
+ +++ b/renamed-symlink/create-me
+ @@ -0,0 +1,1 @@
+ +busted
+ EOF
+
+ test_must_fail git apply patch 2>stderr &&
+ cat >expected_stderr <<-EOF &&
+ error: affected file ${SQ}renamed-symlink/create-me${SQ} is beyond a symbolic link
+ EOF
+ test_cmp expected_stderr stderr &&
+ test_path_is_missing .git/create-me
+'
+
+test_expect_success SYMLINKS 'symlink escape when modifying file' '
+ test_when_finished "git reset --hard && git clean -dfx" &&
+ touch .git/modify-me &&
+
+ cat >patch <<-EOF &&
+ diff --git a/symlink b/renamed-symlink
+ similarity index 100%
+ rename from symlink
+ rename to renamed-symlink
+ --
+ diff --git a/renamed-symlink/modify-me b/renamed-symlink/modify-me
+ index 1111111..2222222 100644
+ --- a/renamed-symlink/modify-me
+ +++ b/renamed-symlink/modify-me
+ @@ -0,0 +1,1 @@
+ +busted
+ EOF
+
+ test_must_fail git apply patch 2>stderr &&
+ cat >expected_stderr <<-EOF &&
+ error: renamed-symlink/modify-me: No such file or directory
+ EOF
+ test_cmp expected_stderr stderr &&
+ test_must_be_empty .git/modify-me
+'
+
+test_expect_success SYMLINKS 'symlink escape when deleting file' '
+ test_when_finished "git reset --hard && git clean -dfx && rm .git/delete-me" &&
+ touch .git/delete-me &&
+
+ cat >patch <<-EOF &&
+ diff --git a/symlink b/renamed-symlink
+ similarity index 100%
+ rename from symlink
+ rename to renamed-symlink
+ --
+ diff --git a/renamed-symlink/delete-me b/renamed-symlink/delete-me
+ deleted file mode 100644
+ index 1111111..0000000 100644
+ EOF
+
+ test_must_fail git apply patch 2>stderr &&
+ cat >expected_stderr <<-EOF &&
+ error: renamed-symlink/delete-me: No such file or directory
+ EOF
+ test_cmp expected_stderr stderr &&
+ test_path_is_file .git/delete-me
+'
+
+test_expect_success SYMLINKS '--reject removes .rej symlink if it exists' '
+ test_when_finished "git reset --hard && git clean -dfx" &&
+
+ test_commit file &&
+ echo modified >file.t &&
+ git diff -- file.t >patch &&
+ echo modified-again >file.t &&
+
+ ln -s foo file.t.rej &&
+ test_must_fail git apply patch --reject 2>err &&
+ test_grep "Rejected hunk" err &&
+ test_path_is_missing foo &&
+ test_path_is_file file.t.rej
+'
+
+test_done
diff --git a/t/t4116-apply-reverse.sh b/t/t4116-apply-reverse.sh
new file mode 100755
index 0000000..a9f4ddd
--- /dev/null
+++ b/t/t4116-apply-reverse.sh
@@ -0,0 +1,93 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+test_description='git apply in reverse
+
+'
+
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+
+ test_write_lines a b c d e f g h i j k l m n >file1 &&
+ perl -pe "y/ijk/\\000\\001\\002/" <file1 >file2 &&
+
+ git add file1 file2 &&
+ git commit -m initial &&
+ git tag initial &&
+
+ test_write_lines a b c g h i J K L m o n p q >file1 &&
+ perl -pe "y/mon/\\000\\001\\002/" <file1 >file2 &&
+
+ git commit -a -m second &&
+ git tag second &&
+
+ git diff --binary initial second >patch
+
+'
+
+test_expect_success 'apply in forward' '
+
+ T0=$(git rev-parse "second^{tree}") &&
+ git reset --hard initial &&
+ git apply --index --binary patch &&
+ T1=$(git write-tree) &&
+ test "$T0" = "$T1"
+'
+
+test_expect_success 'apply in reverse' '
+
+ git reset --hard second &&
+ git apply --reverse --binary --index patch &&
+ git diff >diff &&
+ test_must_be_empty diff
+
+'
+
+test_expect_success 'setup separate repository lacking postimage' '
+
+ git archive --format=tar --prefix=initial/ initial | $TAR xf - &&
+ (
+ cd initial && git init && git add .
+ ) &&
+
+ git archive --format=tar --prefix=second/ second | $TAR xf - &&
+ (
+ cd second && git init && git add .
+ )
+
+'
+
+test_expect_success 'apply in forward without postimage' '
+
+ T0=$(git rev-parse "second^{tree}") &&
+ (
+ cd initial &&
+ git apply --index --binary ../patch &&
+ T1=$(git write-tree) &&
+ test "$T0" = "$T1"
+ )
+'
+
+test_expect_success 'apply in reverse without postimage' '
+
+ T0=$(git rev-parse "initial^{tree}") &&
+ (
+ cd second &&
+ git apply --index --binary --reverse ../patch &&
+ T1=$(git write-tree) &&
+ test "$T0" = "$T1"
+ )
+'
+
+test_expect_success 'reversing a whitespace introduction' '
+ sed "s/a/a /" < file1 > file1.new &&
+ mv file1.new file1 &&
+ git diff | git apply --reverse --whitespace=error
+'
+
+test_done
diff --git a/t/t4117-apply-reject.sh b/t/t4117-apply-reject.sh
new file mode 100755
index 0000000..c86d05a
--- /dev/null
+++ b/t/t4117-apply-reject.sh
@@ -0,0 +1,107 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+test_description='git apply with rejects
+
+'
+
+. ./test-lib.sh
+
+test_expect_success setup '
+ test_write_lines 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 >file1 &&
+ cat file1 >saved.file1 &&
+ git update-index --add file1 &&
+ git commit -m initial &&
+
+ test_write_lines 1 2 A B 4 5 6 7 8 9 10 11 12 C 13 14 15 16 17 18 19 20 D 21 >file1 &&
+ git diff >patch.1 &&
+ cat file1 >clean &&
+
+ test_write_lines 1 E 2 3 4 5 6 7 8 9 10 11 12 C 13 14 15 16 17 18 19 20 F 21 >expected &&
+
+ mv file1 file2 &&
+ git update-index --add --remove file1 file2 &&
+ git diff -M HEAD >patch.2 &&
+
+ rm -f file1 file2 &&
+ mv saved.file1 file1 &&
+ git update-index --add --remove file1 file2 &&
+
+ test_write_lines 1 E 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 F 21 >file1 &&
+
+ cat file1 >saved.file1
+'
+
+test_expect_success 'apply --reject is incompatible with --3way' '
+ test_when_finished "cat saved.file1 >file1" &&
+ git diff >patch.0 &&
+ git checkout file1 &&
+ test_must_fail git apply --reject --3way patch.0 &&
+ git diff --exit-code
+'
+
+test_expect_success 'apply without --reject should fail' '
+
+ test_must_fail git apply patch.1 &&
+ test_cmp file1 saved.file1
+'
+
+test_expect_success 'apply without --reject should fail' '
+
+ test_must_fail git apply --verbose patch.1 &&
+ test_cmp file1 saved.file1
+'
+
+test_expect_success 'apply with --reject should fail but update the file' '
+
+ cat saved.file1 >file1 &&
+ rm -f file1.rej file2.rej &&
+
+ test_must_fail git apply --reject patch.1 &&
+ test_cmp expected file1 &&
+
+ test_path_is_file file1.rej &&
+ test_path_is_missing file2.rej
+'
+
+test_expect_success 'apply with --reject should fail but update the file' '
+
+ cat saved.file1 >file1 &&
+ rm -f file1.rej file2.rej file2 &&
+
+ test_must_fail git apply --reject patch.2 >rejects &&
+ test_path_is_missing file1 &&
+ test_cmp expected file2 &&
+
+ test_path_is_file file2.rej &&
+ test_path_is_missing file1.rej
+
+'
+
+test_expect_success 'the same test with --verbose' '
+
+ cat saved.file1 >file1 &&
+ rm -f file1.rej file2.rej file2 &&
+
+ test_must_fail git apply --reject --verbose patch.2 >rejects &&
+ test_path_is_missing file1 &&
+ test_cmp expected file2 &&
+
+ test_path_is_file file2.rej &&
+ test_path_is_missing file1.rej
+
+'
+
+test_expect_success 'apply cleanly with --verbose' '
+
+ git cat-file -p HEAD:file1 >file1 &&
+ rm -f file?.rej file2 &&
+
+ git apply --verbose patch.1 &&
+
+ test_cmp file1 clean
+'
+
+test_done
diff --git a/t/t4118-apply-empty-context.sh b/t/t4118-apply-empty-context.sh
new file mode 100755
index 0000000..69c9c48
--- /dev/null
+++ b/t/t4118-apply-empty-context.sh
@@ -0,0 +1,53 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Junio C Hamano
+#
+
+test_description='git apply with new style GNU diff with empty context
+
+'
+
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ test_write_lines "" "" A B C "" >file1 &&
+ cat file1 >file1.orig &&
+ {
+ cat file1 &&
+ echo Q | tr -d "\\012"
+ } >file2 &&
+ cat file2 >file2.orig &&
+ git add file1 file2 &&
+ sed -e "/^B/d" <file1.orig >file1 &&
+ cat file1 > file2 &&
+ echo Q | tr -d "\\012" >>file2 &&
+ cat file1 >file1.mods &&
+ cat file2 >file2.mods &&
+ git diff |
+ sed -e "s/^ \$//" >diff.output
+'
+
+test_expect_success 'apply --numstat' '
+
+ git apply --numstat diff.output >actual &&
+ {
+ echo "0 1 file1" &&
+ echo "0 1 file2"
+ } >expect &&
+ test_cmp expect actual
+
+'
+
+test_expect_success 'apply --apply' '
+
+ cat file1.orig >file1 &&
+ cat file2.orig >file2 &&
+ git update-index file1 file2 &&
+ git apply --index diff.output &&
+ test_cmp file1.mods file1 &&
+ test_cmp file2.mods file2
+'
+
+test_done
diff --git a/t/t4119-apply-config.sh b/t/t4119-apply-config.sh
new file mode 100755
index 0000000..208c961
--- /dev/null
+++ b/t/t4119-apply-config.sh
@@ -0,0 +1,181 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Junio C Hamano
+#
+
+test_description='git apply --whitespace=strip and configuration file.
+
+'
+
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ mkdir sub &&
+ echo A >sub/file1 &&
+ cp sub/file1 saved &&
+ git add sub/file1 &&
+ echo "B " >sub/file1 &&
+ git diff >patch.file
+'
+
+# Also handcraft GNU diff output; note this has trailing whitespace.
+tr '_' ' ' >gpatch.file <<\EOF &&
+--- file1 2007-02-21 01:04:24.000000000 -0800
++++ file1+ 2007-02-21 01:07:44.000000000 -0800
+@@ -1 +1 @@
+-A
++B_
+EOF
+
+sed -e 's|file1|sub/&|' gpatch.file >gpatch-sub.file &&
+sed -e '
+ /^--- /s|file1|a/sub/&|
+ /^+++ /s|file1|b/sub/&|
+' gpatch.file >gpatch-ab-sub.file &&
+
+check_result () {
+ if grep " " "$1"
+ then
+ echo "Eh?"
+ false
+ elif grep B "$1"
+ then
+ echo Happy
+ else
+ echo "Huh?"
+ false
+ fi
+}
+
+test_expect_success 'apply --whitespace=strip' '
+
+ rm -f sub/file1 &&
+ cp saved sub/file1 &&
+ git update-index --refresh &&
+
+ git apply --whitespace=strip patch.file &&
+ check_result sub/file1
+'
+
+test_expect_success 'apply --whitespace=strip from config' '
+
+ rm -f sub/file1 &&
+ cp saved sub/file1 &&
+ git update-index --refresh &&
+
+ git config apply.whitespace strip &&
+ git apply patch.file &&
+ check_result sub/file1
+'
+
+D=$(pwd)
+
+test_expect_success 'apply --whitespace=strip in subdir' '
+
+ cd "$D" &&
+ git config --unset-all apply.whitespace &&
+ rm -f sub/file1 &&
+ cp saved sub/file1 &&
+ git update-index --refresh &&
+
+ cd sub &&
+ git apply --whitespace=strip ../patch.file &&
+ check_result file1
+'
+
+test_expect_success 'apply --whitespace=strip from config in subdir' '
+
+ cd "$D" &&
+ git config apply.whitespace strip &&
+ rm -f sub/file1 &&
+ cp saved sub/file1 &&
+ git update-index --refresh &&
+
+ cd sub &&
+ git apply ../patch.file &&
+ check_result file1
+'
+
+test_expect_success 'same in subdir but with traditional patch input' '
+
+ cd "$D" &&
+ git config apply.whitespace strip &&
+ rm -f sub/file1 &&
+ cp saved sub/file1 &&
+ git update-index --refresh &&
+
+ cd sub &&
+ git apply ../gpatch.file &&
+ check_result file1
+'
+
+test_expect_success 'same but with traditional patch input of depth 1' '
+
+ cd "$D" &&
+ git config apply.whitespace strip &&
+ rm -f sub/file1 &&
+ cp saved sub/file1 &&
+ git update-index --refresh &&
+
+ cd sub &&
+ git apply ../gpatch-sub.file &&
+ check_result file1
+'
+
+test_expect_success 'same but with traditional patch input of depth 2' '
+
+ cd "$D" &&
+ git config apply.whitespace strip &&
+ rm -f sub/file1 &&
+ cp saved sub/file1 &&
+ git update-index --refresh &&
+
+ cd sub &&
+ git apply ../gpatch-ab-sub.file &&
+ check_result file1
+'
+
+test_expect_success 'same but with traditional patch input of depth 1' '
+
+ cd "$D" &&
+ git config apply.whitespace strip &&
+ rm -f sub/file1 &&
+ cp saved sub/file1 &&
+ git update-index --refresh &&
+
+ git apply -p0 gpatch-sub.file &&
+ check_result sub/file1
+'
+
+test_expect_success 'same but with traditional patch input of depth 2' '
+
+ cd "$D" &&
+ git config apply.whitespace strip &&
+ rm -f sub/file1 &&
+ cp saved sub/file1 &&
+ git update-index --refresh &&
+
+ git apply gpatch-ab-sub.file &&
+ check_result sub/file1
+'
+
+test_expect_success 'in subdir with traditional patch input' '
+ cd "$D" &&
+ git config apply.whitespace strip &&
+ cat >.gitattributes <<-EOF &&
+ /* whitespace=blank-at-eol
+ sub/* whitespace=-blank-at-eol
+ EOF
+ rm -f sub/file1 &&
+ cp saved sub/file1 &&
+ git update-index --refresh &&
+
+ cd sub &&
+ git apply ../gpatch.file &&
+ echo "B " >expect &&
+ test_cmp expect file1
+'
+
+test_done
diff --git a/t/t4120-apply-popt.sh b/t/t4120-apply-popt.sh
new file mode 100755
index 0000000..697e86c
--- /dev/null
+++ b/t/t4120-apply-popt.sh
@@ -0,0 +1,89 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Shawn O. Pearce
+#
+
+test_description='git apply -p handling.'
+
+. ./test-lib.sh
+
+test_expect_success setup '
+ mkdir sub &&
+ echo A >sub/file1 &&
+ cp sub/file1 file1.saved &&
+ git add sub/file1 &&
+ echo B >sub/file1 &&
+ git diff >patch.file &&
+ git checkout -- sub/file1 &&
+ git mv sub süb &&
+ echo B >süb/file1 &&
+ git diff >patch.escaped &&
+ grep "[\]" patch.escaped &&
+ rm süb/file1 &&
+ rmdir süb
+'
+
+test_expect_success 'apply git diff with -p2' '
+ cp file1.saved file1 &&
+ git apply -p2 patch.file
+'
+
+test_expect_success 'apply with too large -p' '
+ cp file1.saved file1 &&
+ test_must_fail git apply --stat -p3 patch.file 2>err &&
+ test_grep "removing 3 leading" err
+'
+
+test_expect_success 'apply (-p2) traditional diff with funny filenames' '
+ cat >patch.quotes <<-\EOF &&
+ diff -u "a/"sub/file1 "b/"sub/file1
+ --- "a/"sub/file1
+ +++ "b/"sub/file1
+ @@ -1 +1 @@
+ -A
+ +B
+ EOF
+ echo B >expected &&
+
+ cp file1.saved file1 &&
+ git apply -p2 patch.quotes &&
+ test_cmp expected file1
+'
+
+test_expect_success 'apply with too large -p and fancy filename' '
+ cp file1.saved file1 &&
+ test_must_fail git apply --stat -p3 patch.escaped 2>err &&
+ test_grep "removing 3 leading" err
+'
+
+test_expect_success 'apply (-p2) diff, mode change only' '
+ cat >patch.chmod <<-\EOF &&
+ diff --git a/sub/file1 b/sub/file1
+ old mode 100644
+ new mode 100755
+ EOF
+ test_chmod -x file1 &&
+ git apply --index -p2 patch.chmod &&
+ case $(git ls-files -s file1) in 100755*) : good;; *) false;; esac
+'
+
+test_expect_success FILEMODE 'file mode was changed' '
+ test -x file1
+'
+
+test_expect_success 'apply (-p2) diff, rename' '
+ cat >patch.rename <<-\EOF &&
+ diff --git a/sub/file1 b/sub/file2
+ similarity index 100%
+ rename from sub/file1
+ rename to sub/file2
+ EOF
+ echo A >expected &&
+
+ cp file1.saved file1 &&
+ rm -f file2 &&
+ git apply -p2 patch.rename &&
+ test_cmp expected file2
+'
+
+test_done
diff --git a/t/t4121-apply-diffs.sh b/t/t4121-apply-diffs.sh
new file mode 100755
index 0000000..a80cec9
--- /dev/null
+++ b/t/t4121-apply-diffs.sh
@@ -0,0 +1,36 @@
+#!/bin/sh
+
+test_description='git apply for contextually independent diffs'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+echo '1
+2
+3
+4
+5
+6
+7
+8' >file
+
+test_expect_success 'setup' \
+ 'git add file &&
+ git commit -q -m 1 &&
+ git checkout -b test &&
+ mv file file.tmp &&
+ echo 0 >file &&
+ cat file.tmp >>file &&
+ rm file.tmp &&
+ git commit -a -q -m 2 &&
+ echo 9 >>file &&
+ git commit -a -q -m 3 &&
+ git checkout main'
+
+test_expect_success \
+ 'check if contextually independent diffs for the same file apply' \
+ '( git diff test~2 test~1 && git diff test~1 test~0 )| git apply'
+
+test_done
diff --git a/t/t4122-apply-symlink-inside.sh b/t/t4122-apply-symlink-inside.sh
new file mode 100755
index 0000000..2089d84
--- /dev/null
+++ b/t/t4122-apply-symlink-inside.sh
@@ -0,0 +1,158 @@
+#!/bin/sh
+
+test_description='apply to deeper directory without getting fooled with symlink'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+
+ mkdir -p arch/i386/boot arch/x86_64 &&
+ test_write_lines 1 2 3 4 5 >arch/i386/boot/Makefile &&
+ test_ln_s_add ../i386/boot arch/x86_64/boot &&
+ git add . &&
+ test_tick &&
+ git commit -m initial &&
+ git branch test &&
+
+ rm arch/x86_64/boot &&
+ mkdir arch/x86_64/boot &&
+ test_write_lines 2 3 4 5 6 >arch/x86_64/boot/Makefile &&
+ git add . &&
+ test_tick &&
+ git commit -a -m second &&
+
+ git format-patch --binary -1 --stdout >test.patch
+
+'
+
+test_expect_success apply '
+
+ git checkout test &&
+ git diff --exit-code test &&
+ git diff --exit-code --cached test &&
+ git apply --index test.patch
+
+'
+
+test_expect_success 'check result' '
+
+ git diff --exit-code main &&
+ git diff --exit-code --cached main &&
+ test_tick &&
+ git commit -m replay &&
+ T1=$(git rev-parse "main^{tree}") &&
+ T2=$(git rev-parse "HEAD^{tree}") &&
+ test "z$T1" = "z$T2"
+
+'
+
+test_expect_success SYMLINKS 'do not read from beyond symbolic link' '
+ git reset --hard &&
+ mkdir -p arch/x86_64/dir &&
+ >arch/x86_64/dir/file &&
+ git add arch/x86_64/dir/file &&
+ echo line >arch/x86_64/dir/file &&
+ git diff >patch &&
+ git reset --hard &&
+
+ mkdir arch/i386/dir &&
+ >arch/i386/dir/file &&
+ ln -s ../i386/dir arch/x86_64/dir &&
+
+ test_must_fail git apply patch &&
+ test_must_fail git apply --cached patch &&
+ test_must_fail git apply --index patch
+
+'
+
+test_expect_success SYMLINKS 'do not follow symbolic link (setup)' '
+
+ rm -rf arch/i386/dir arch/x86_64/dir &&
+ git reset --hard &&
+ ln -s ../i386/dir arch/x86_64/dir &&
+ git add arch/x86_64/dir &&
+ git diff HEAD >add_symlink.patch &&
+ git reset --hard &&
+
+ mkdir arch/x86_64/dir &&
+ >arch/x86_64/dir/file &&
+ git add arch/x86_64/dir/file &&
+ git diff HEAD >add_file.patch &&
+ git diff -R HEAD >del_file.patch &&
+ git reset --hard &&
+ rm -fr arch/x86_64/dir &&
+
+ cat add_symlink.patch add_file.patch >patch &&
+ cat add_symlink.patch del_file.patch >tricky_del &&
+
+ mkdir arch/i386/dir
+'
+
+test_expect_success SYMLINKS 'do not follow symbolic link (same input)' '
+
+ # same input creates a confusing symbolic link
+ test_must_fail git apply patch 2>error-wt &&
+ test_grep "beyond a symbolic link" error-wt &&
+ test_path_is_missing arch/x86_64/dir &&
+ test_path_is_missing arch/i386/dir/file &&
+
+ test_must_fail git apply --index patch 2>error-ix &&
+ test_grep "beyond a symbolic link" error-ix &&
+ test_path_is_missing arch/x86_64/dir &&
+ test_path_is_missing arch/i386/dir/file &&
+ test_must_fail git ls-files --error-unmatch arch/x86_64/dir &&
+ test_must_fail git ls-files --error-unmatch arch/i386/dir &&
+
+ test_must_fail git apply --cached patch 2>error-ct &&
+ test_grep "beyond a symbolic link" error-ct &&
+ test_must_fail git ls-files --error-unmatch arch/x86_64/dir &&
+ test_must_fail git ls-files --error-unmatch arch/i386/dir &&
+
+ >arch/i386/dir/file &&
+ git add arch/i386/dir/file &&
+
+ test_must_fail git apply tricky_del &&
+ test_path_is_file arch/i386/dir/file &&
+
+ test_must_fail git apply --index tricky_del &&
+ test_path_is_file arch/i386/dir/file &&
+ test_must_fail git ls-files --error-unmatch arch/x86_64/dir &&
+ git ls-files --error-unmatch arch/i386/dir &&
+
+ test_must_fail git apply --cached tricky_del &&
+ test_must_fail git ls-files --error-unmatch arch/x86_64/dir &&
+ git ls-files --error-unmatch arch/i386/dir
+'
+
+test_expect_success SYMLINKS 'do not follow symbolic link (existing)' '
+
+ # existing symbolic link
+ git reset --hard &&
+ ln -s ../i386/dir arch/x86_64/dir &&
+ git add arch/x86_64/dir &&
+
+ test_must_fail git apply add_file.patch 2>error-wt-add &&
+ test_grep "beyond a symbolic link" error-wt-add &&
+ test_path_is_missing arch/i386/dir/file &&
+
+ mkdir arch/i386/dir &&
+ >arch/i386/dir/file &&
+ test_must_fail git apply del_file.patch 2>error-wt-del &&
+ test_grep "beyond a symbolic link" error-wt-del &&
+ test_path_is_file arch/i386/dir/file &&
+ rm arch/i386/dir/file &&
+
+ test_must_fail git apply --index add_file.patch 2>error-ix-add &&
+ test_grep "beyond a symbolic link" error-ix-add &&
+ test_path_is_missing arch/i386/dir/file &&
+ test_must_fail git ls-files --error-unmatch arch/i386/dir &&
+
+ test_must_fail git apply --cached add_file.patch 2>error-ct-file &&
+ test_grep "beyond a symbolic link" error-ct-file &&
+ test_must_fail git ls-files --error-unmatch arch/i386/dir
+'
+
+test_done
diff --git a/t/t4123-apply-shrink.sh b/t/t4123-apply-shrink.sh
new file mode 100755
index 0000000..3ef8461
--- /dev/null
+++ b/t/t4123-apply-shrink.sh
@@ -0,0 +1,46 @@
+#!/bin/sh
+
+test_description='apply a patch that is larger than the preimage'
+
+. ./test-lib.sh
+
+cat >F <<\EOF
+1
+2
+3
+4
+5
+6
+7
+8
+999999
+A
+B
+C
+D
+E
+F
+G
+H
+I
+J
+
+EOF
+
+test_expect_success setup '
+
+ git add F &&
+ mv F G &&
+ sed -e "s/1/11/" -e "s/999999/9/" -e "s/H/HH/" <G >F &&
+ git diff >patch &&
+ sed -e "/^\$/d" <G >F &&
+ git add F
+
+'
+
+test_expect_success 'apply should fail gracefully' '
+ test_must_fail git apply --index patch &&
+ test_path_is_missing .git/index.lock
+'
+
+test_done
diff --git a/t/t4124-apply-ws-rule.sh b/t/t4124-apply-ws-rule.sh
new file mode 100755
index 0000000..485c7d2
--- /dev/null
+++ b/t/t4124-apply-ws-rule.sh
@@ -0,0 +1,559 @@
+#!/bin/sh
+
+test_description='core.whitespace rules and git apply'
+
+. ./test-lib.sh
+
+prepare_test_file () {
+
+ # A line that has character X is touched iff RULE is in effect:
+ # X RULE
+ # ! trailing-space
+ # @ space-before-tab
+ # # indent-with-non-tab (default tab width 8)
+ # = indent-with-non-tab,tabwidth=16
+ # % tab-in-indent
+ sed -e "s/_/ /g" -e "s/>/ /" <<-\EOF
+ An_SP in an ordinary line>and a HT.
+ >A HT (%).
+ _>A SP and a HT (@%).
+ _>_A SP, a HT and a SP (@%).
+ _______Seven SP.
+ ________Eight SP (#).
+ _______>Seven SP and a HT (@%).
+ ________>Eight SP and a HT (@#%).
+ _______>_Seven SP, a HT and a SP (@%).
+ ________>_Eight SP, a HT and a SP (@#%).
+ _______________Fifteen SP (#).
+ _______________>Fifteen SP and a HT (@#%).
+ ________________Sixteen SP (#=).
+ ________________>Sixteen SP and a HT (@#%=).
+ _____a__Five SP, a non WS, two SP.
+ A line with a (!) trailing SP_
+ A line with a (!) trailing HT>
+ EOF
+}
+
+apply_patch () {
+ cmd_prefix= &&
+ if test "x$1" = 'x!'
+ then
+ cmd_prefix=test_must_fail &&
+ shift
+ fi &&
+ >target &&
+ sed -e "s|\([ab]\)/file|\1/target|" <patch |
+ $cmd_prefix git apply "$@"
+}
+
+test_fix () {
+ # fix should not barf
+ apply_patch --whitespace=fix || return 1
+
+ # find touched lines
+ $DIFF file target | sed -n -e "s/^> //p" >fixed
+ # busybox's diff(1) doesn't output normal format
+ if ! test -s fixed
+ then
+ $DIFF -u file target |
+ grep -v '^+++ target' |
+ sed -ne "/^+/s/+//p" >fixed
+ fi
+
+ # the changed lines are all expected to change
+ fixed_cnt=$(wc -l <fixed)
+ case "$1" in
+ '') expect_cnt=$fixed_cnt ;;
+ ?*) expect_cnt=$(grep "[$1]" <fixed | wc -l) ;;
+ esac
+ test $fixed_cnt -eq $expect_cnt || return 1
+
+ # and we are not missing anything
+ case "$1" in
+ '') expect_cnt=0 ;;
+ ?*) expect_cnt=$(grep "[$1]" <file | wc -l) ;;
+ esac
+ test $fixed_cnt -eq $expect_cnt || return 1
+
+ # Get the patch actually applied
+ git diff-files -p target >fixed-patch
+ test -s fixed-patch && return 0
+
+ # Make sure it is complaint-free
+ >target
+ git apply --whitespace=error-all <fixed-patch
+
+}
+
+test_expect_success setup '
+
+ >file &&
+ git add file &&
+ prepare_test_file >file &&
+ git diff-files -p >patch &&
+ >target &&
+ git add target
+
+'
+
+test_expect_success 'whitespace=nowarn, default rule' '
+
+ apply_patch --whitespace=nowarn &&
+ test_cmp file target
+
+'
+
+test_expect_success 'whitespace=warn, default rule' '
+
+ apply_patch --whitespace=warn &&
+ test_cmp file target
+
+'
+
+test_expect_success 'whitespace=error-all, default rule' '
+
+ apply_patch ! --whitespace=error-all &&
+ test_must_be_empty target
+
+'
+
+test_expect_success 'whitespace=error-all, no rule' '
+
+ git config core.whitespace -trailing,-space-before,-indent &&
+ apply_patch --whitespace=error-all &&
+ test_cmp file target
+
+'
+
+test_expect_success 'whitespace=error-all, no rule (attribute)' '
+
+ git config --unset core.whitespace &&
+ echo "target -whitespace" >.gitattributes &&
+ apply_patch --whitespace=error-all &&
+ test_cmp file target
+
+'
+
+test_expect_success 'spaces inserted by tab-in-indent' '
+
+ git config core.whitespace -trailing,-space,-indent,tab &&
+ rm -f .gitattributes &&
+ test_fix % &&
+ sed -e "s/_/ /g" -e "s/>/ /" <<-\EOF >expect &&
+ An_SP in an ordinary line>and a HT.
+ ________A HT (%).
+ ________A SP and a HT (@%).
+ _________A SP, a HT and a SP (@%).
+ _______Seven SP.
+ ________Eight SP (#).
+ ________Seven SP and a HT (@%).
+ ________________Eight SP and a HT (@#%).
+ _________Seven SP, a HT and a SP (@%).
+ _________________Eight SP, a HT and a SP (@#%).
+ _______________Fifteen SP (#).
+ ________________Fifteen SP and a HT (@#%).
+ ________________Sixteen SP (#=).
+ ________________________Sixteen SP and a HT (@#%=).
+ _____a__Five SP, a non WS, two SP.
+ A line with a (!) trailing SP_
+ A line with a (!) trailing HT>
+ EOF
+ test_cmp expect target
+
+'
+
+for t in - ''
+do
+ case "$t" in '') tt='!' ;; *) tt= ;; esac
+ for s in - ''
+ do
+ case "$s" in '') ts='@' ;; *) ts= ;; esac
+ for i in - ''
+ do
+ case "$i" in '') ti='#' ti16='=';; *) ti= ti16= ;; esac
+ for h in - ''
+ do
+ [ -z "$h$i" ] && continue
+ case "$h" in '') th='%' ;; *) th= ;; esac
+ rule=${t}trailing,${s}space,${i}indent,${h}tab
+
+ rm -f .gitattributes
+ test_expect_success "rule=$rule" '
+ git config core.whitespace "$rule" &&
+ test_fix "$tt$ts$ti$th"
+ '
+
+ test_expect_success "rule=$rule,tabwidth=16" '
+ git config core.whitespace "$rule,tabwidth=16" &&
+ test_fix "$tt$ts$ti16$th"
+ '
+
+ test_expect_success "rule=$rule (attributes)" '
+ git config --unset core.whitespace &&
+ echo "target whitespace=$rule" >.gitattributes &&
+ test_fix "$tt$ts$ti$th"
+ '
+
+ test_expect_success "rule=$rule,tabwidth=16 (attributes)" '
+ echo "target whitespace=$rule,tabwidth=16" >.gitattributes &&
+ test_fix "$tt$ts$ti16$th"
+ '
+
+ done
+ done
+ done
+done
+
+create_patch () {
+ sed -e "s/_/ /" <<-\EOF
+ diff --git a/target b/target
+ index e69de29..8bd6648 100644
+ --- a/target
+ +++ b/target
+ @@ -0,0 +1,3 @@
+ +An empty line follows
+ +
+ +A line with trailing whitespace and no newline_
+ \ No newline at end of file
+ EOF
+}
+
+test_expect_success 'trailing whitespace & no newline at the end of file' '
+ >target &&
+ create_patch >patch-file &&
+ git apply --whitespace=fix patch-file &&
+ grep "newline$" target &&
+ grep "^$" target
+'
+
+test_expect_success 'blank at EOF with --whitespace=fix (1)' '
+ test_might_fail git config --unset core.whitespace &&
+ rm -f .gitattributes &&
+
+ test_write_lines a b c >one &&
+ git add one &&
+ test_write_lines a b c >expect &&
+ { cat expect && echo; } >one &&
+ git diff -- one >patch &&
+
+ git checkout one &&
+ git apply --whitespace=fix patch &&
+ test_cmp expect one
+'
+
+test_expect_success 'blank at EOF with --whitespace=fix (2)' '
+ test_write_lines a b c >one &&
+ git add one &&
+ test_write_lines a b >expect &&
+ { cat expect && test_write_lines "" ""; } >one &&
+ git diff -- one >patch &&
+
+ git checkout one &&
+ git apply --whitespace=fix patch &&
+ test_cmp expect one
+'
+
+test_expect_success 'blank at EOF with --whitespace=fix (3)' '
+ test_write_lines a b "" >one &&
+ git add one &&
+ test_write_lines a c "" >expect &&
+ { cat expect && test_write_lines "" ""; } >one &&
+ git diff -- one >patch &&
+
+ git checkout one &&
+ git apply --whitespace=fix patch &&
+ test_cmp expect one
+'
+
+test_expect_success 'blank at end of hunk, not at EOF with --whitespace=fix' '
+ test_write_lines a b "" "" "" "" "" d >one &&
+ git add one &&
+ test_write_lines a b "" "" "" "" "" "" d >expect &&
+ cp expect one &&
+ git diff -- one >patch &&
+
+ git checkout one &&
+ git apply --whitespace=fix patch &&
+ test_cmp expect one
+'
+
+test_expect_success 'blank at EOF with --whitespace=warn' '
+ test_write_lines a b c >one &&
+ git add one &&
+ echo >>one &&
+ cat one >expect &&
+ git diff -- one >patch &&
+
+ git checkout one &&
+ git apply --whitespace=warn patch 2>error &&
+ test_cmp expect one &&
+ grep "new blank line at EOF" error
+'
+
+test_expect_success 'blank at EOF with --whitespace=error' '
+ test_write_lines a b c >one &&
+ git add one &&
+ cat one >expect &&
+ echo >>one &&
+ git diff -- one >patch &&
+
+ git checkout one &&
+ test_must_fail git apply --whitespace=error patch 2>error &&
+ test_cmp expect one &&
+ grep "new blank line at EOF" error
+'
+
+test_expect_success 'blank but not empty at EOF' '
+ test_write_lines a b c >one &&
+ git add one &&
+ echo " " >>one &&
+ cat one >expect &&
+ git diff -- one >patch &&
+
+ git checkout one &&
+ git apply --whitespace=warn patch 2>error &&
+ test_cmp expect one &&
+ grep "new blank line at EOF" error
+'
+
+test_expect_success 'applying beyond EOF requires one non-blank context line' '
+ test_write_lines "" "" "" "" >one &&
+ git add one &&
+ echo b >>one &&
+ git diff -- one >patch &&
+
+ git checkout one &&
+ test_write_lines a "" >one &&
+ cp one expect &&
+ test_must_fail git apply --whitespace=fix patch &&
+ test_cmp expect one &&
+ test_must_fail git apply --ignore-space-change --whitespace=fix patch &&
+ test_cmp expect one
+'
+
+test_expect_success 'tons of blanks at EOF should not apply' '
+ for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16; do
+ test_write_lines "" "" "" "" || return 1
+ done >one &&
+ git add one &&
+ echo a >>one &&
+ git diff -- one >patch &&
+
+ >one &&
+ test_must_fail git apply --whitespace=fix patch &&
+ test_must_fail git apply --ignore-space-change --whitespace=fix patch
+'
+
+test_expect_success 'missing blank line at end with --whitespace=fix' '
+ echo a >one &&
+ echo >>one &&
+ git add one &&
+ echo b >>one &&
+ cp one expect &&
+ git diff -- one >patch &&
+ echo a >one &&
+ cp one saved-one &&
+ test_must_fail git apply patch &&
+ git apply --whitespace=fix patch &&
+ test_cmp expect one &&
+ mv saved-one one &&
+ git apply --ignore-space-change --whitespace=fix patch &&
+ test_cmp expect one
+'
+
+test_expect_success 'two missing blank lines at end with --whitespace=fix' '
+ test_write_lines a "" b c >one &&
+ cp one no-blank-lines &&
+ test_write_lines "" "" >>one &&
+ git add one &&
+ echo d >>one &&
+ cp one expect &&
+ echo >>one &&
+ git diff -- one >patch &&
+ cp no-blank-lines one &&
+ test_must_fail git apply patch &&
+ git apply --whitespace=fix patch &&
+ test_cmp expect one &&
+ mv no-blank-lines one &&
+ test_must_fail git apply patch &&
+ git apply --ignore-space-change --whitespace=fix patch &&
+ test_cmp expect one
+'
+
+test_expect_success 'missing blank line at end, insert before end, --whitespace=fix' '
+ test_write_lines a "" >one &&
+ git add one &&
+ test_write_lines b a "" >one &&
+ cp one expect &&
+ git diff -- one >patch &&
+ echo a >one &&
+ test_must_fail git apply patch &&
+ git apply --whitespace=fix patch &&
+ test_cmp expect one
+'
+
+test_expect_success 'shrink file with tons of missing blanks at end of file' '
+ test_write_lines a b c >one &&
+ cp one no-blank-lines &&
+ for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16; do
+ test_write_lines "" "" "" "" || return 1
+ done >>one &&
+ git add one &&
+ echo a >one &&
+ cp one expect &&
+ git diff -- one >patch &&
+ cp no-blank-lines one &&
+ test_must_fail git apply patch &&
+ git apply --whitespace=fix patch &&
+ test_cmp expect one &&
+ mv no-blank-lines one &&
+ git apply --ignore-space-change --whitespace=fix patch &&
+ test_cmp expect one
+'
+
+test_expect_success 'missing blanks at EOF must only match blank lines' '
+ test_write_lines a b >one &&
+ git add one &&
+ test_write_lines c d >>one &&
+ git diff -- one >patch &&
+
+ echo a >one &&
+ test_must_fail git apply patch &&
+ test_must_fail git apply --whitespace=fix patch &&
+ test_must_fail git apply --ignore-space-change --whitespace=fix patch
+'
+
+sed -e's/Z//' >one <<EOF
+a
+b
+c
+ Z
+EOF
+
+test_expect_success 'missing blank line should match context line with spaces' '
+ git add one &&
+ echo d >>one &&
+ git diff -- one >patch &&
+ test_write_lines a b c >one &&
+ cp one expect &&
+ test_write_lines "" d >>expect &&
+ git add one &&
+
+ git apply --whitespace=fix patch &&
+ test_cmp expect one
+'
+
+sed -e's/Z//' >one <<EOF
+a
+b
+c
+ Z
+EOF
+
+test_expect_success 'same, but with the --ignore-space-option' '
+ git add one &&
+ echo d >>one &&
+ cp one expect &&
+ git diff -- one >patch &&
+ test_write_lines a b c >one &&
+ git add one &&
+
+ git checkout-index -f one &&
+ git apply --ignore-space-change --whitespace=fix patch &&
+ test_cmp expect one
+'
+
+test_expect_success 'same, but with CR-LF line endings && cr-at-eol set' '
+ git config core.whitespace cr-at-eol &&
+ printf "a\r\n" >one &&
+ printf "b\r\n" >>one &&
+ printf "c\r\n" >>one &&
+ cp one save-one &&
+ printf " \r\n" >>one &&
+ git add one &&
+ printf "d\r\n" >>one &&
+ cp one expect &&
+ git diff -- one >patch &&
+ mv save-one one &&
+
+ git apply --ignore-space-change --whitespace=fix patch &&
+ test_cmp expect one
+'
+
+test_expect_success 'CR-LF line endings && add line && text=auto' '
+ git config --unset core.whitespace &&
+ printf "a\r\n" >one &&
+ cp one save-one &&
+ git add one &&
+ printf "b\r\n" >>one &&
+ cp one expect &&
+ git diff -- one >patch &&
+ mv save-one one &&
+ echo "one text=auto" >.gitattributes &&
+ git apply patch &&
+ test_cmp expect one
+'
+
+test_expect_success 'CR-LF line endings && change line && text=auto' '
+ printf "a\r\n" >one &&
+ cp one save-one &&
+ git add one &&
+ printf "b\r\n" >one &&
+ cp one expect &&
+ git diff -- one >patch &&
+ mv save-one one &&
+ echo "one text=auto" >.gitattributes &&
+ git apply patch &&
+ test_cmp expect one
+'
+
+test_expect_success 'LF in repo, CRLF in worktree && change line && text=auto' '
+ printf "a\n" >one &&
+ git add one &&
+ printf "b\r\n" >one &&
+ git diff -- one >patch &&
+ printf "a\r\n" >one &&
+ echo "one text=auto" >.gitattributes &&
+ git -c core.eol=CRLF apply patch &&
+ printf "b\r\n" >expect &&
+ test_cmp expect one
+'
+
+test_expect_success 'whitespace=fix to expand' '
+ qz_to_tab_space >preimage <<-\EOF &&
+ QQa
+ QQb
+ QQc
+ ZZZZZZZZZZZZZZZZd
+ QQe
+ QQf
+ QQg
+ EOF
+ qz_to_tab_space >patch <<-\EOF &&
+ diff --git a/preimage b/preimage
+ --- a/preimage
+ +++ b/preimage
+ @@ -1,7 +1,6 @@
+ QQa
+ QQb
+ QQc
+ -QQd
+ QQe
+ QQf
+ QQg
+ EOF
+ git -c core.whitespace=tab-in-indent apply --whitespace=fix patch
+'
+
+test_expect_success 'whitespace check skipped for excluded paths' '
+ git config core.whitespace blank-at-eol &&
+ >used &&
+ >unused &&
+ git add used unused &&
+ echo "used" >used &&
+ echo "unused " >unused &&
+ git diff-files -p used unused >patch &&
+ git apply --include=used --stat --whitespace=error <patch
+'
+
+test_done
diff --git a/t/t4125-apply-ws-fuzz.sh b/t/t4125-apply-ws-fuzz.sh
new file mode 100755
index 0000000..090987c
--- /dev/null
+++ b/t/t4125-apply-ws-fuzz.sh
@@ -0,0 +1,100 @@
+#!/bin/sh
+
+test_description='applying patch that has broken whitespaces in context'
+
+. ./test-lib.sh
+
+test_expect_success setup '
+
+ >file &&
+ git add file &&
+
+ # file-0 is full of whitespace breakages
+ printf "%s \n" a bb c d eeee f ggg h >file-0 &&
+
+ # patch-0 creates a whitespace broken file
+ cat file-0 >file &&
+ git diff >patch-0 &&
+ git add file &&
+
+ # file-1 is still full of whitespace breakages,
+ # but has one line updated, without fixing any
+ # whitespaces.
+ # patch-1 records that change.
+ sed -e "s/d/D/" file-0 >file-1 &&
+ cat file-1 >file &&
+ git diff >patch-1 &&
+
+ # patch-all is the effect of both patch-0 and patch-1
+ >file &&
+ git add file &&
+ cat file-1 >file &&
+ git diff >patch-all &&
+
+ # patch-2 is the same as patch-1 but is based
+ # on a version that already has whitespace fixed,
+ # and does not introduce whitespace breakages.
+ sed -e "s/ \$//" patch-1 >patch-2 &&
+
+ # If all whitespace breakages are fixed the contents
+ # should look like file-fixed
+ sed -e "s/ \$//" file-1 >file-fixed
+
+'
+
+test_expect_success nofix '
+
+ >file &&
+ git add file &&
+
+ # Baseline. Applying without fixing any whitespace
+ # breakages.
+ git apply --whitespace=nowarn patch-0 &&
+ git apply --whitespace=nowarn patch-1 &&
+
+ # The result should obviously match.
+ test_cmp file-1 file
+'
+
+test_expect_success 'withfix (forward)' '
+
+ >file &&
+ git add file &&
+
+ # The first application will munge the context lines
+ # the second patch depends on. We should be able to
+ # adjust and still apply.
+ git apply --whitespace=fix patch-0 &&
+ git apply --whitespace=fix patch-1 &&
+
+ test_cmp file-fixed file
+'
+
+test_expect_success 'withfix (backward)' '
+
+ >file &&
+ git add file &&
+
+ # Now we have a whitespace breakages on our side.
+ git apply --whitespace=nowarn patch-0 &&
+
+ # And somebody sends in a patch based on image
+ # with whitespace already fixed.
+ git apply --whitespace=fix patch-2 &&
+
+ # The result should accept the whitespace fixed
+ # postimage. But the line with "h" is beyond context
+ # horizon and left unfixed.
+
+ sed -e /h/d file-fixed >fixed-head &&
+ sed -e /h/d file >file-head &&
+ test_cmp fixed-head file-head &&
+
+ sed -n -e /h/p file-fixed >fixed-tail &&
+ sed -n -e /h/p file >file-tail &&
+
+ ! test_cmp fixed-tail file-tail
+
+'
+
+test_done
diff --git a/t/t4126-apply-empty.sh b/t/t4126-apply-empty.sh
new file mode 100755
index 0000000..ece9fae
--- /dev/null
+++ b/t/t4126-apply-empty.sh
@@ -0,0 +1,69 @@
+#!/bin/sh
+
+test_description='apply empty'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ >empty &&
+ git add empty &&
+ test_tick &&
+ git commit -m initial &&
+ git commit --allow-empty -m "empty commit" &&
+ git format-patch --always HEAD~ >empty.patch &&
+ test_write_lines a b c d e >empty &&
+ cat empty >expect &&
+ git diff |
+ sed -e "/^diff --git/d" \
+ -e "/^index /d" \
+ -e "s|a/empty|empty.orig|" \
+ -e "s|b/empty|empty|" >patch0 &&
+ sed -e "s|empty|missing|" patch0 >patch1 &&
+ >empty &&
+ git update-index --refresh
+'
+
+test_expect_success 'apply empty' '
+ rm -f missing &&
+ test_when_finished "git reset --hard" &&
+ git apply patch0 &&
+ test_cmp expect empty
+'
+
+test_expect_success 'apply empty patch fails' '
+ test_when_finished "git reset --hard" &&
+ test_must_fail git apply empty.patch &&
+ test_must_fail git apply - </dev/null
+'
+
+test_expect_success 'apply with --allow-empty succeeds' '
+ test_when_finished "git reset --hard" &&
+ git apply --allow-empty empty.patch &&
+ git apply --allow-empty - </dev/null
+'
+
+test_expect_success 'apply --index empty' '
+ rm -f missing &&
+ test_when_finished "git reset --hard" &&
+ git apply --index patch0 &&
+ test_cmp expect empty &&
+ git diff --exit-code
+'
+
+test_expect_success 'apply create' '
+ rm -f missing &&
+ test_when_finished "git reset --hard" &&
+ git apply patch1 &&
+ test_cmp expect missing
+'
+
+test_expect_success 'apply --index create' '
+ rm -f missing &&
+ test_when_finished "git reset --hard" &&
+ git apply --index patch1 &&
+ test_cmp expect missing &&
+ git diff --exit-code
+'
+
+test_done
diff --git a/t/t4127-apply-same-fn.sh b/t/t4127-apply-same-fn.sh
new file mode 100755
index 0000000..aa5cfae
--- /dev/null
+++ b/t/t4127-apply-same-fn.sh
@@ -0,0 +1,98 @@
+#!/bin/sh
+
+test_description='apply same filename'
+
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+modify () {
+ sed -e "$1" < "$2" > "$2".x &&
+ mv "$2".x "$2"
+}
+
+test_expect_success setup '
+ test_write_lines a b c d e f g h i j k l m >same_fn &&
+ cp same_fn other_fn &&
+ git add same_fn other_fn &&
+ git commit -m initial
+'
+test_expect_success 'apply same filename with independent changes' '
+ modify "s/^d/z/" same_fn &&
+ git diff > patch0 &&
+ git add same_fn &&
+ modify "s/^i/y/" same_fn &&
+ git diff >> patch0 &&
+ cp same_fn same_fn2 &&
+ git reset --hard &&
+ git apply patch0 &&
+ test_cmp same_fn same_fn2
+'
+
+test_expect_success 'apply same filename with overlapping changes' '
+ git reset --hard &&
+
+ # Store same_fn so that we can check apply -R in next test
+ cp same_fn same_fn1 &&
+
+ modify "s/^d/z/" same_fn &&
+ git diff > patch0 &&
+ git add same_fn &&
+ modify "s/^e/y/" same_fn &&
+ git diff >> patch0 &&
+ cp same_fn same_fn2 &&
+ git reset --hard &&
+ git apply patch0 &&
+ test_cmp same_fn same_fn2
+'
+
+test_expect_success 'apply same filename with overlapping changes, in reverse' '
+ git apply -R patch0 &&
+ test_cmp same_fn same_fn1
+'
+
+test_expect_success 'apply same new filename after rename' '
+ git reset --hard &&
+ git mv same_fn new_fn &&
+ modify "s/^d/z/" new_fn &&
+ git add new_fn &&
+ git diff -M --cached > patch1 &&
+ modify "s/^e/y/" new_fn &&
+ git diff >> patch1 &&
+ cp new_fn new_fn2 &&
+ git reset --hard &&
+ git apply --index patch1 &&
+ test_cmp new_fn new_fn2
+'
+
+test_expect_success 'apply same old filename after rename -- should fail.' '
+ git reset --hard &&
+ git mv same_fn new_fn &&
+ modify "s/^d/z/" new_fn &&
+ git add new_fn &&
+ git diff -M --cached > patch1 &&
+ git mv new_fn same_fn &&
+ modify "s/^e/y/" same_fn &&
+ git diff >> patch1 &&
+ git reset --hard &&
+ test_must_fail git apply patch1
+'
+
+test_expect_success 'apply A->B (rename), C->A (rename), A->A -- should pass.' '
+ git reset --hard &&
+ git mv same_fn new_fn &&
+ modify "s/^d/z/" new_fn &&
+ git add new_fn &&
+ git diff -M --cached > patch1 &&
+ git commit -m "a rename" &&
+ git mv other_fn same_fn &&
+ modify "s/^e/y/" same_fn &&
+ git add same_fn &&
+ git diff -M --cached >> patch1 &&
+ modify "s/^g/x/" same_fn &&
+ git diff >> patch1 &&
+ git reset --hard HEAD^ &&
+ git apply patch1
+'
+
+test_done
diff --git a/t/t4128-apply-root.sh b/t/t4128-apply-root.sh
new file mode 100755
index 0000000..ed94c90
--- /dev/null
+++ b/t/t4128-apply-root.sh
@@ -0,0 +1,123 @@
+#!/bin/sh
+
+test_description='apply same filename'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+
+ mkdir -p some/sub/dir &&
+ echo Hello > some/sub/dir/file &&
+ git add some/sub/dir/file &&
+ git commit -m initial &&
+ git tag initial
+
+'
+
+cat > patch << EOF
+diff a/bla/blub/dir/file b/bla/blub/dir/file
+--- a/bla/blub/dir/file
++++ b/bla/blub/dir/file
+@@ -1,1 +1,1 @@
+-Hello
++Bello
+EOF
+
+test_expect_success 'apply --directory -p (1)' '
+ git apply --directory=some/sub -p3 --index patch &&
+ echo Bello >expect &&
+ git show :some/sub/dir/file >actual &&
+ test_cmp expect actual &&
+ test_cmp expect some/sub/dir/file
+
+'
+
+test_expect_success 'apply --directory -p (2) ' '
+
+ git reset --hard initial &&
+ git apply --directory=some/sub/ -p3 --index patch &&
+ echo Bello >expect &&
+ git show :some/sub/dir/file >actual &&
+ test_cmp expect actual &&
+ test_cmp expect some/sub/dir/file
+
+'
+
+cat > patch << EOF
+diff --git a/newfile b/newfile
+new file mode 100644
+index 0000000..d95f3ad
+--- /dev/null
++++ b/newfile
+@@ -0,0 +1 @@
++content
+EOF
+
+test_expect_success 'apply --directory (new file)' '
+ git reset --hard initial &&
+ git apply --directory=some/sub/dir/ --index patch &&
+ echo content >expect &&
+ git show :some/sub/dir/newfile >actual &&
+ test_cmp expect actual &&
+ test_cmp expect some/sub/dir/newfile
+'
+
+cat > patch << EOF
+diff --git a/c/newfile2 b/c/newfile2
+new file mode 100644
+index 0000000..d95f3ad
+--- /dev/null
++++ b/c/newfile2
+@@ -0,0 +1 @@
++content
+EOF
+
+test_expect_success 'apply --directory -p (new file)' '
+ git reset --hard initial &&
+ git apply -p2 --directory=some/sub/dir/ --index patch &&
+ echo content >expect &&
+ git show :some/sub/dir/newfile2 >actual &&
+ test_cmp expect actual &&
+ test_cmp expect some/sub/dir/newfile2
+'
+
+cat > patch << EOF
+diff --git a/delfile b/delfile
+deleted file mode 100644
+index d95f3ad..0000000
+--- a/delfile
++++ /dev/null
+@@ -1 +0,0 @@
+-content
+EOF
+
+test_expect_success 'apply --directory (delete file)' '
+ git reset --hard initial &&
+ echo content >some/sub/dir/delfile &&
+ git add some/sub/dir/delfile &&
+ git apply --directory=some/sub/dir/ --index patch &&
+ git ls-files >out &&
+ ! grep delfile out
+'
+
+cat > patch << 'EOF'
+diff --git "a/qu\157tefile" "b/qu\157tefile"
+new file mode 100644
+index 0000000..d95f3ad
+--- /dev/null
++++ "b/qu\157tefile"
+@@ -0,0 +1 @@
++content
+EOF
+
+test_expect_success 'apply --directory (quoted filename)' '
+ git reset --hard initial &&
+ git apply --directory=some/sub/dir/ --index patch &&
+ echo content >expect &&
+ git show :some/sub/dir/quotefile >actual &&
+ test_cmp expect actual &&
+ test_cmp expect some/sub/dir/quotefile
+'
+
+test_done
diff --git a/t/t4129-apply-samemode.sh b/t/t4129-apply-samemode.sh
new file mode 100755
index 0000000..e7a7295
--- /dev/null
+++ b/t/t4129-apply-samemode.sh
@@ -0,0 +1,104 @@
+#!/bin/sh
+
+test_description='applying patch with mode bits'
+
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ echo original >file &&
+ git add file &&
+ test_tick &&
+ git commit -m initial &&
+ git tag initial &&
+ echo modified >file &&
+ git diff --stat -p >patch-0.txt &&
+ chmod +x file &&
+ git diff --stat -p >patch-1.txt &&
+ sed "s/^\(new mode \).*/\1/" <patch-1.txt >patch-empty-mode.txt &&
+ sed "s/^\(new mode \).*/\1garbage/" <patch-1.txt >patch-bogus-mode.txt
+'
+
+test_expect_success FILEMODE 'same mode (no index)' '
+ git reset --hard &&
+ chmod +x file &&
+ git apply patch-0.txt &&
+ test -x file
+'
+
+test_expect_success FILEMODE 'same mode (with index)' '
+ git reset --hard &&
+ chmod +x file &&
+ git add file &&
+ git apply --index patch-0.txt &&
+ test -x file &&
+ git diff --exit-code
+'
+
+test_expect_success FILEMODE 'same mode (index only)' '
+ git reset --hard &&
+ chmod +x file &&
+ git add file &&
+ git apply --cached patch-0.txt &&
+ git ls-files -s file | grep "^100755"
+'
+
+test_expect_success FILEMODE 'mode update (no index)' '
+ git reset --hard &&
+ git apply patch-1.txt &&
+ test -x file
+'
+
+test_expect_success FILEMODE 'mode update (with index)' '
+ git reset --hard &&
+ git apply --index patch-1.txt &&
+ test -x file &&
+ git diff --exit-code
+'
+
+test_expect_success FILEMODE 'mode update (index only)' '
+ git reset --hard &&
+ git apply --cached patch-1.txt &&
+ git ls-files -s file | grep "^100755"
+'
+
+test_expect_success FILEMODE 'empty mode is rejected' '
+ git reset --hard &&
+ test_must_fail git apply patch-empty-mode.txt 2>err &&
+ test_grep "invalid mode" err
+'
+
+test_expect_success FILEMODE 'bogus mode is rejected' '
+ git reset --hard &&
+ test_must_fail git apply patch-bogus-mode.txt 2>err &&
+ test_grep "invalid mode" err
+'
+
+test_expect_success POSIXPERM 'do not use core.sharedRepository for working tree files' '
+ git reset --hard &&
+ test_config core.sharedRepository 0666 &&
+ (
+ # Remove a default ACL if possible.
+ (setfacl -k . 2>/dev/null || true) &&
+ umask 0077 &&
+
+ # Test both files (f1) and leading dirs (d)
+ mkdir d &&
+ touch f1 d/f2 &&
+ git add f1 d/f2 &&
+ git diff --staged >patch-f1-and-f2.txt &&
+
+ rm -rf d f1 &&
+ git apply patch-f1-and-f2.txt &&
+
+ echo "-rw-------" >f1_mode.expected &&
+ echo "drwx------" >d_mode.expected &&
+ test_modebits f1 >f1_mode.actual &&
+ test_modebits d >d_mode.actual &&
+ test_cmp f1_mode.expected f1_mode.actual &&
+ test_cmp d_mode.expected d_mode.actual
+ )
+'
+
+test_done
diff --git a/t/t4130-apply-criss-cross-rename.sh b/t/t4130-apply-criss-cross-rename.sh
new file mode 100755
index 0000000..f3ea632
--- /dev/null
+++ b/t/t4130-apply-criss-cross-rename.sh
@@ -0,0 +1,72 @@
+#!/bin/sh
+
+test_description='git apply handling criss-cross rename patch.'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+create_file() {
+ cnt=0
+ while test $cnt -le 100
+ do
+ cnt=$(($cnt + 1))
+ echo "$2" >> "$1"
+ done
+}
+
+test_expect_success 'setup' '
+ # Ensure that file sizes are different, because on Windows
+ # lstat() does not discover inode numbers, and we need
+ # other properties to discover swapped files
+ # (mtime is not always different, either).
+ create_file file1 "some content" &&
+ create_file file2 "some other content" &&
+ create_file file3 "again something else" &&
+ git add file1 file2 file3 &&
+ git commit -m 1
+'
+
+test_expect_success 'criss-cross rename' '
+ mv file1 tmp &&
+ mv file2 file1 &&
+ mv tmp file2 &&
+ cp file1 file1-swapped &&
+ cp file2 file2-swapped
+'
+
+test_expect_success 'diff -M -B' '
+ git diff -M -B > diff &&
+ git reset --hard
+
+'
+
+test_expect_success 'apply' '
+ git apply diff &&
+ test_cmp file1 file1-swapped &&
+ test_cmp file2 file2-swapped
+'
+
+test_expect_success 'criss-cross rename' '
+ git reset --hard &&
+ mv file1 tmp &&
+ mv file2 file1 &&
+ mv file3 file2 &&
+ mv tmp file3 &&
+ cp file1 file1-swapped &&
+ cp file2 file2-swapped &&
+ cp file3 file3-swapped
+'
+
+test_expect_success 'diff -M -B' '
+ git diff -M -B > diff &&
+ git reset --hard
+'
+
+test_expect_success 'apply' '
+ git apply diff &&
+ test_cmp file1 file1-swapped &&
+ test_cmp file2 file2-swapped &&
+ test_cmp file3 file3-swapped
+'
+
+test_done
diff --git a/t/t4131-apply-fake-ancestor.sh b/t/t4131-apply-fake-ancestor.sh
new file mode 100755
index 0000000..b1361ce
--- /dev/null
+++ b/t/t4131-apply-fake-ancestor.sh
@@ -0,0 +1,42 @@
+#!/bin/sh
+#
+# Copyright (c) 2009 Stephen Boyd
+#
+
+test_description='git apply --build-fake-ancestor handling.'
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ test_commit 1 &&
+ test_commit 2 &&
+ mkdir sub &&
+ test_commit 3 sub/3.t &&
+ test_commit 4
+'
+
+test_expect_success 'apply --build-fake-ancestor' '
+ git checkout 2 &&
+ echo "A" > 1.t &&
+ git diff > 1.patch &&
+ git reset --hard &&
+ git checkout 1 &&
+ git apply --build-fake-ancestor 1.ancestor 1.patch
+'
+
+test_expect_success 'apply --build-fake-ancestor in a subdirectory' '
+ git checkout 3 &&
+ echo "C" > sub/3.t &&
+ git diff > 3.patch &&
+ git reset --hard &&
+ git checkout 4 &&
+ (
+ cd sub &&
+ git apply --build-fake-ancestor 3.ancestor ../3.patch &&
+ test -f 3.ancestor
+ ) &&
+ git apply --build-fake-ancestor 3.ancestor 3.patch &&
+ test_cmp sub/3.ancestor 3.ancestor
+'
+
+test_done
diff --git a/t/t4132-apply-removal.sh b/t/t4132-apply-removal.sh
new file mode 100755
index 0000000..c1e3049
--- /dev/null
+++ b/t/t4132-apply-removal.sh
@@ -0,0 +1,98 @@
+#!/bin/sh
+#
+# Copyright (c) 2009 Junio C Hamano
+
+test_description='git-apply notices removal patches generated by GNU diff'
+
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ cat <<-EOF >c &&
+ diff -ruN a/file b/file
+ --- a/file TS0
+ +++ b/file TS1
+ @@ -0,0 +1 @@
+ +something
+ EOF
+
+ cat <<-EOF >d &&
+ diff -ruN a/file b/file
+ --- a/file TS0
+ +++ b/file TS1
+ @@ -1 +0,0 @@
+ -something
+ EOF
+
+ timeWest="1982-09-16 07:00:00.000000000 -0800" &&
+ timeGMT="1982-09-16 15:00:00.000000000 +0000" &&
+ timeEast="1982-09-17 00:00:00.000000000 +0900" &&
+
+ epocWest="1969-12-31 16:00:00.000000000 -0800" &&
+ epocGMT="1970-01-01 00:00:00.000000000 +0000" &&
+ epocEast="1970-01-01 09:00:00.000000000 +0900" &&
+ epocWest2="1969-12-31 16:00:00 -08:00" &&
+
+ sed -e "s/TS0/$epocWest/" -e "s/TS1/$timeWest/" <c >createWest.patch &&
+ sed -e "s/TS0/$epocEast/" -e "s/TS1/$timeEast/" <c >createEast.patch &&
+ sed -e "s/TS0/$epocGMT/" -e "s/TS1/$timeGMT/" <c >createGMT.patch &&
+
+ sed -e "s/TS0/$timeWest/" -e "s/TS1/$timeWest/" <c >addWest.patch &&
+ sed -e "s/TS0/$timeEast/" -e "s/TS1/$timeEast/" <c >addEast.patch &&
+ sed -e "s/TS0/$timeGMT/" -e "s/TS1/$timeGMT/" <c >addGMT.patch &&
+
+ sed -e "s/TS0/$timeWest/" -e "s/TS1/$timeWest/" <d >emptyWest.patch &&
+ sed -e "s/TS0/$timeEast/" -e "s/TS1/$timeEast/" <d >emptyEast.patch &&
+ sed -e "s/TS0/$timeGMT/" -e "s/TS1/$timeGMT/" <d >emptyGMT.patch &&
+
+ sed -e "s/TS0/$timeWest/" -e "s/TS1/$epocWest/" <d >removeWest.patch &&
+ sed -e "s/TS0/$timeEast/" -e "s/TS1/$epocEast/" <d >removeEast.patch &&
+ sed -e "s/TS0/$timeGMT/" -e "s/TS1/$epocGMT/" <d >removeGMT.patch &&
+ sed -e "s/TS0/$timeWest/" -e "s/TS1/$epocWest2/" <d >removeWest2.patch &&
+
+ echo something >something
+'
+
+for patch in *.patch
+do
+ test_expect_success "test $patch" '
+ rm -f file .git/index &&
+ case "$patch" in
+ create*)
+ # must be able to create
+ git apply --index $patch &&
+ test_cmp file something &&
+ # must notice the file is already there
+ >file &&
+ git add file &&
+ test_must_fail git apply $patch
+ ;;
+ add*)
+ # must be able to create or patch
+ git apply $patch &&
+ test_cmp file something &&
+ >file &&
+ git apply $patch &&
+ test_cmp file something
+ ;;
+ empty*)
+ # must leave an empty file
+ cat something >file &&
+ git add file &&
+ git apply --index $patch &&
+ test -f file &&
+ test_must_be_empty file
+ ;;
+ remove*)
+ # must remove the file
+ cat something >file &&
+ git add file &&
+ git apply --index $patch &&
+ ! test -f file
+ ;;
+ esac
+ '
+done
+
+test_done
diff --git a/t/t4133-apply-filenames.sh b/t/t4133-apply-filenames.sh
new file mode 100755
index 0000000..c21ddb2
--- /dev/null
+++ b/t/t4133-apply-filenames.sh
@@ -0,0 +1,64 @@
+#!/bin/sh
+#
+# Copyright (c) 2010 Andreas Gruenbacher
+#
+
+test_description='git apply filename consistency check'
+
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ cat > bad1.patch <<EOF &&
+diff --git a/f b/f
+new file mode 100644
+index 0000000..d00491f
+--- /dev/null
++++ b/f-blah
+@@ -0,0 +1 @@
++1
+EOF
+ cat > bad2.patch <<EOF
+diff --git a/f b/f
+deleted file mode 100644
+index d00491f..0000000
+--- b/f-blah
++++ /dev/null
+@@ -1 +0,0 @@
+-1
+EOF
+'
+
+test_expect_success 'apply diff with inconsistent filenames in headers' '
+ test_must_fail git apply bad1.patch 2>err &&
+ test_grep "inconsistent new filename" err &&
+ test_must_fail git apply bad2.patch 2>err &&
+ test_grep "inconsistent old filename" err
+'
+
+test_expect_success 'apply diff with new filename missing from headers' '
+ cat >missing_new_filename.diff <<-\EOF &&
+ diff --git a/f b/f
+ index 0000000..d00491f
+ --- a/f
+ @@ -0,0 +1 @@
+ +1
+ EOF
+ test_must_fail git apply missing_new_filename.diff 2>err &&
+ test_grep "lacks filename information" err
+'
+
+test_expect_success 'apply diff with old filename missing from headers' '
+ cat >missing_old_filename.diff <<-\EOF &&
+ diff --git a/f b/f
+ index d00491f..0000000
+ +++ b/f
+ @@ -1 +0,0 @@
+ -1
+ EOF
+ test_must_fail git apply missing_old_filename.diff 2>err &&
+ test_grep "lacks filename information" err
+'
+
+test_done
diff --git a/t/t4134-apply-submodule.sh b/t/t4134-apply-submodule.sh
new file mode 100755
index 0000000..aceb4c4
--- /dev/null
+++ b/t/t4134-apply-submodule.sh
@@ -0,0 +1,40 @@
+#!/bin/sh
+#
+# Copyright (c) 2010 Peter Collingbourne
+#
+
+test_description='git apply submodule tests'
+
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ cat > create-sm.patch <<EOF &&
+diff --git a/dir/sm b/dir/sm
+new file mode 160000
+index 0000000..0123456
+--- /dev/null
++++ b/dir/sm
+@@ -0,0 +1 @@
++Subproject commit $(test_oid numeric)
+EOF
+ cat > remove-sm.patch <<EOF
+diff --git a/dir/sm b/dir/sm
+deleted file mode 160000
+index 0123456..0000000
+--- a/dir/sm
++++ /dev/null
+@@ -1 +0,0 @@
+-Subproject commit $(test_oid numeric)
+EOF
+'
+
+test_expect_success 'removing a submodule also removes all leading subdirectories' '
+ git apply --index create-sm.patch &&
+ test -d dir/sm &&
+ git apply --index remove-sm.patch &&
+ test \! -d dir
+'
+
+test_done
diff --git a/t/t4135-apply-weird-filenames.sh b/t/t4135-apply-weird-filenames.sh
new file mode 100755
index 0000000..d3502c6
--- /dev/null
+++ b/t/t4135-apply-weird-filenames.sh
@@ -0,0 +1,102 @@
+#!/bin/sh
+
+test_description='git apply with weird postimage filenames'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ vector=$TEST_DIRECTORY/t4135 &&
+
+ test_tick &&
+ git commit --allow-empty -m preimage &&
+ git tag preimage &&
+
+ reset_preimage() {
+ git checkout -f preimage^0 &&
+ git read-tree -u --reset HEAD &&
+ git update-index --refresh
+ }
+'
+
+try_filename() {
+ desc=$1
+ postimage=$2
+ prereq=${3:-}
+ exp1=${4:-success}
+ exp2=${5:-success}
+ exp3=${6:-success}
+
+ test_expect_$exp1 $prereq "$desc, git-style file creation patch" "
+ echo postimage >expected &&
+ reset_preimage &&
+ rm -f '$postimage' &&
+ git apply -v \"\$vector\"/'git-$desc.diff' &&
+ test_cmp expected '$postimage'
+ "
+
+ test_expect_$exp2 $prereq "$desc, traditional patch" "
+ echo postimage >expected &&
+ reset_preimage &&
+ echo preimage >'$postimage' &&
+ git apply -v \"\$vector\"/'diff-$desc.diff' &&
+ test_cmp expected '$postimage'
+ "
+
+ test_expect_$exp3 $prereq "$desc, traditional file creation patch" "
+ echo postimage >expected &&
+ reset_preimage &&
+ rm -f '$postimage' &&
+ git apply -v \"\$vector\"/'add-$desc.diff' &&
+ test_cmp expected '$postimage'
+ "
+}
+
+try_filename 'plain' 'postimage.txt'
+try_filename 'with spaces' 'post image.txt'
+try_filename 'with tab' 'post image.txt' FUNNYNAMES
+try_filename 'with backslash' 'post\image.txt' BSLASHPSPEC
+try_filename 'with quote' '"postimage".txt' FUNNYNAMES success failure success
+
+test_expect_success 'whitespace-damaged traditional patch' '
+ echo postimage >expected &&
+ reset_preimage &&
+ rm -f postimage.txt &&
+ git apply -v "$vector/damaged.diff" &&
+ test_cmp expected postimage.txt
+'
+
+test_expect_success 'traditional patch with colon in timezone' '
+ echo postimage >expected &&
+ reset_preimage &&
+ rm -f "post image.txt" &&
+ git apply "$vector/funny-tz.diff" &&
+ test_cmp expected "post image.txt"
+'
+
+test_expect_success 'traditional, whitespace-damaged, colon in timezone' '
+ echo postimage >expected &&
+ reset_preimage &&
+ rm -f "post image.txt" &&
+ git apply "$vector/damaged-tz.diff" &&
+ test_cmp expected "post image.txt"
+'
+
+cat >diff-from-svn <<\EOF
+Index: Makefile
+===================================================================
+diff --git a/branches/Makefile
+deleted file mode 100644
+--- a/branches/Makefile (revision 13)
++++ /dev/null (nonexistent)
+@@ +1 0,0 @@
+-
+EOF
+
+test_expect_success 'apply handles a diff generated by Subversion' '
+ >Makefile &&
+ git apply -p2 diff-from-svn &&
+ test_path_is_missing Makefile
+'
+
+test_done
diff --git a/t/t4135/.gitignore b/t/t4135/.gitignore
new file mode 100644
index 0000000..3e58e65
--- /dev/null
+++ b/t/t4135/.gitignore
@@ -0,0 +1,3 @@
+/file-creation/
+/trad-creation/
+/trad-modification/
diff --git a/t/t4135/add-plain.diff b/t/t4135/add-plain.diff
new file mode 100644
index 0000000..cf5970a
--- /dev/null
+++ b/t/t4135/add-plain.diff
@@ -0,0 +1,5 @@
+diff -pruN a/postimage.txt b/postimage.txt
+--- a/postimage.txt 1969-12-31 18:00:00.000000000 -0600
++++ b/postimage.txt 2010-08-18 20:13:31.484002255 -0500
+@@ -0,0 +1 @@
++postimage
diff --git a/t/t4135/add-with backslash.diff b/t/t4135/add-with backslash.diff
new file mode 100644
index 0000000..c6861e1
--- /dev/null
+++ b/t/t4135/add-with backslash.diff
@@ -0,0 +1,5 @@
+diff -pruN a/post\image.txt b/post\image.txt
+--- a/post\image.txt 1969-12-31 18:00:00.000000000 -0600
++++ b/post\image.txt 2010-08-18 20:13:31.692002255 -0500
+@@ -0,0 +1 @@
++postimage
diff --git a/t/t4135/add-with quote.diff b/t/t4135/add-with quote.diff
new file mode 100644
index 0000000..866de78
--- /dev/null
+++ b/t/t4135/add-with quote.diff
@@ -0,0 +1,5 @@
+diff -pruN a/"postimage".txt b/"postimage".txt
+--- a/"postimage".txt 1969-12-31 18:00:00.000000000 -0600
++++ b/"postimage".txt 2010-08-18 20:13:31.756002255 -0500
+@@ -0,0 +1 @@
++postimage
diff --git a/t/t4135/add-with spaces.diff b/t/t4135/add-with spaces.diff
new file mode 100644
index 0000000..a9a1212
--- /dev/null
+++ b/t/t4135/add-with spaces.diff
@@ -0,0 +1,5 @@
+diff -pruN a/post image.txt b/post image.txt
+--- a/post image.txt 1969-12-31 18:00:00.000000000 -0600
++++ b/post image.txt 2010-08-18 20:13:31.556002255 -0500
+@@ -0,0 +1 @@
++postimage
diff --git a/t/t4135/add-with tab.diff b/t/t4135/add-with tab.diff
new file mode 100644
index 0000000..bb67cb7
--- /dev/null
+++ b/t/t4135/add-with tab.diff
@@ -0,0 +1,5 @@
+diff -pruN a/post image.txt b/post image.txt
+--- a/post image.txt 1969-12-31 18:00:00.000000000 -0600
++++ b/post image.txt 2010-08-18 20:13:31.628002255 -0500
+@@ -0,0 +1 @@
++postimage
diff --git a/t/t4135/damaged-tz.diff b/t/t4135/damaged-tz.diff
new file mode 100644
index 0000000..07aaf08
--- /dev/null
+++ b/t/t4135/damaged-tz.diff
@@ -0,0 +1,5 @@
+diff -urN -X /usr/people/jes/exclude-linux linux-2.6.12-rc2-mm3-vanilla/post image.txt linux-2.6.12-rc2-mm3/post image.txt
+--- linux-2.6.12-rc2-mm3-vanilla/post image.txt 1969-12-31 16:00:00 -08:00
++++ linux-2.6.12-rc2-mm3/post image.txt 2005-04-12 02:14:06 -07:00
+@@ -0,0 +1 @@
++postimage
diff --git a/t/t4135/damaged.diff b/t/t4135/damaged.diff
new file mode 100644
index 0000000..68f7ede
--- /dev/null
+++ b/t/t4135/damaged.diff
@@ -0,0 +1,5 @@
+diff -pruN a/postimage.txt b/postimage.txt
+--- a/postimage.txt 1969-12-31 18:00:00.000000000 -0600
++++ b/postimage.txt 2010-08-18 20:13:31.484002255 -0500
+@@ -0,0 +1 @@
++postimage
diff --git a/t/t4135/diff-plain.diff b/t/t4135/diff-plain.diff
new file mode 100644
index 0000000..acedcfa
--- /dev/null
+++ b/t/t4135/diff-plain.diff
@@ -0,0 +1,5 @@
+--- postimage.txt.orig 2010-08-18 20:13:31.432002255 -0500
++++ postimage.txt 2010-08-18 20:13:31.432002255 -0500
+@@ -1 +1 @@
+-preimage
++postimage
diff --git a/t/t4135/diff-with backslash.diff b/t/t4135/diff-with backslash.diff
new file mode 100644
index 0000000..9068a61
--- /dev/null
+++ b/t/t4135/diff-with backslash.diff
@@ -0,0 +1,5 @@
+--- post\image.txt.orig 2010-08-18 20:13:31.680002255 -0500
++++ post\image.txt 2010-08-18 20:13:31.680002255 -0500
+@@ -1 +1 @@
+-preimage
++postimage
diff --git a/t/t4135/diff-with quote.diff b/t/t4135/diff-with quote.diff
new file mode 100644
index 0000000..c8e8cc1
--- /dev/null
+++ b/t/t4135/diff-with quote.diff
@@ -0,0 +1,5 @@
+--- "postimage".txt.orig 2010-08-18 20:13:31.744002255 -0500
++++ "postimage".txt 2010-08-18 20:13:31.744002255 -0500
+@@ -1 +1 @@
+-preimage
++postimage
diff --git a/t/t4135/diff-with spaces.diff b/t/t4135/diff-with spaces.diff
new file mode 100644
index 0000000..3512056
--- /dev/null
+++ b/t/t4135/diff-with spaces.diff
@@ -0,0 +1,5 @@
+--- post image.txt.orig 2010-08-18 20:13:31.544002255 -0500
++++ post image.txt 2010-08-18 20:13:31.544002255 -0500
+@@ -1 +1 @@
+-preimage
++postimage
diff --git a/t/t4135/diff-with tab.diff b/t/t4135/diff-with tab.diff
new file mode 100644
index 0000000..4e6d9b2
--- /dev/null
+++ b/t/t4135/diff-with tab.diff
@@ -0,0 +1,5 @@
+--- post image.txt.orig 2010-08-18 20:13:31.616002255 -0500
++++ post image.txt 2010-08-18 20:13:31.616002255 -0500
+@@ -1 +1 @@
+-preimage
++postimage
diff --git a/t/t4135/funny-tz.diff b/t/t4135/funny-tz.diff
new file mode 100644
index 0000000..998e3a8
--- /dev/null
+++ b/t/t4135/funny-tz.diff
@@ -0,0 +1,5 @@
+diff -urN -X /usr/people/jes/exclude-linux linux-2.6.12-rc2-mm3-vanilla/post image.txt linux-2.6.12-rc2-mm3/post image.txt
+--- linux-2.6.12-rc2-mm3-vanilla/post image.txt 1969-12-31 16:00:00 -08:00
++++ linux-2.6.12-rc2-mm3/post image.txt 2005-04-12 02:14:06 -07:00
+@@ -0,0 +1 @@
++postimage
diff --git a/t/t4135/git-plain.diff b/t/t4135/git-plain.diff
new file mode 100644
index 0000000..db47d1a
--- /dev/null
+++ b/t/t4135/git-plain.diff
@@ -0,0 +1,7 @@
+diff --git a/postimage.txt b/postimage.txt
+new file mode 100644
+index 0000000..eff0c54
+--- /dev/null
++++ b/postimage.txt
+@@ -0,0 +1 @@
++postimage
diff --git a/t/t4135/git-with backslash.diff b/t/t4135/git-with backslash.diff
new file mode 100644
index 0000000..0e84a10
--- /dev/null
+++ b/t/t4135/git-with backslash.diff
@@ -0,0 +1,7 @@
+diff --git "a/post\\image.txt" "b/post\\image.txt"
+new file mode 100644
+index 0000000..eff0c54
+--- /dev/null
++++ "b/post\\image.txt"
+@@ -0,0 +1 @@
++postimage
diff --git a/t/t4135/git-with quote.diff b/t/t4135/git-with quote.diff
new file mode 100644
index 0000000..bdbea8a
--- /dev/null
+++ b/t/t4135/git-with quote.diff
@@ -0,0 +1,7 @@
+diff --git "a/\"postimage\".txt" "b/\"postimage\".txt"
+new file mode 100644
+index 0000000..eff0c54
+--- /dev/null
++++ "b/\"postimage\".txt"
+@@ -0,0 +1 @@
++postimage
diff --git a/t/t4135/git-with spaces.diff b/t/t4135/git-with spaces.diff
new file mode 100644
index 0000000..baaa810
--- /dev/null
+++ b/t/t4135/git-with spaces.diff
@@ -0,0 +1,7 @@
+diff --git a/post image.txt b/post image.txt
+new file mode 100644
+index 0000000..eff0c54
+--- /dev/null
++++ b/post image.txt
+@@ -0,0 +1 @@
++postimage
diff --git a/t/t4135/git-with tab.diff b/t/t4135/git-with tab.diff
new file mode 100644
index 0000000..cca3c92
--- /dev/null
+++ b/t/t4135/git-with tab.diff
@@ -0,0 +1,7 @@
+diff --git "a/post\timage.txt" "b/post\timage.txt"
+new file mode 100644
+index 0000000..eff0c54
+--- /dev/null
++++ "b/post\timage.txt"
+@@ -0,0 +1 @@
++postimage
diff --git a/t/t4135/make-patches b/t/t4135/make-patches
new file mode 100755
index 0000000..f5f45dd
--- /dev/null
+++ b/t/t4135/make-patches
@@ -0,0 +1,45 @@
+#!/bin/sh
+
+do_filename() {
+ desc=$1
+ postimage=$2
+
+ rm -fr file-creation &&
+ git init file-creation &&
+ (
+ cd file-creation &&
+ git commit --allow-empty -m init &&
+ echo postimage >"$postimage" &&
+ git add -N "$postimage" &&
+ git diff HEAD >"../git-$desc.diff"
+ ) &&
+
+ rm -fr trad-modification &&
+ mkdir trad-modification &&
+ (
+ cd trad-modification &&
+ echo preimage >"$postimage.orig" &&
+ echo postimage >"$postimage" &&
+ ! diff -u "$postimage.orig" "$postimage" >"../diff-$desc.diff"
+ ) &&
+
+ rm -fr trad-creation &&
+ mkdir trad-creation &&
+ (
+ cd trad-creation &&
+ mkdir a b &&
+ echo postimage >"b/$postimage" &&
+ ! diff -pruN a b >"../add-$desc.diff"
+ )
+}
+
+do_filename plain postimage.txt &&
+do_filename 'with spaces' 'post image.txt' &&
+do_filename 'with tab' 'post image.txt' &&
+do_filename 'with backslash' 'post\image.txt' &&
+do_filename 'with quote' '"postimage".txt' &&
+expand add-plain.diff >damaged.diff ||
+{
+ echo >&2 Failed. &&
+ exit 1
+}
diff --git a/t/t4136-apply-check.sh b/t/t4136-apply-check.sh
new file mode 100755
index 0000000..dfec1c5
--- /dev/null
+++ b/t/t4136-apply-check.sh
@@ -0,0 +1,64 @@
+#!/bin/sh
+
+test_description='git apply should exit non-zero with unrecognized input.'
+
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ test_commit 1
+'
+
+test_expect_success 'apply --check exits non-zero with unrecognized input' '
+ test_must_fail git apply --check - <<-\EOF
+ I am not a patch
+ I look nothing like a patch
+ git apply must fail
+ EOF
+'
+
+test_expect_success 'apply exits non-zero with no-op patch' '
+ cat >input <<-\EOF &&
+ diff --get a/1 b/1
+ index 6696ea4..606eddd 100644
+ --- a/1
+ +++ b/1
+ @@ -1,1 +1,1 @@
+ 1
+ EOF
+ test_must_fail git apply --stat input &&
+ test_must_fail git apply --check input
+'
+
+test_expect_success '`apply --recount` allows no-op patch' '
+ echo 1 >1 &&
+ git apply --recount --check <<-\EOF
+ diff --get a/1 b/1
+ index 6696ea4..606eddd 100644
+ --- a/1
+ +++ b/1
+ @@ -1,1 +1,1 @@
+ 1
+ EOF
+'
+
+test_expect_success 'invalid combination: create and copy' '
+ test_must_fail git apply --check - <<-\EOF
+ diff --git a/1 b/2
+ new file mode 100644
+ copy from 1
+ copy to 2
+ EOF
+'
+
+test_expect_success 'invalid combination: create and rename' '
+ test_must_fail git apply --check - <<-\EOF
+ diff --git a/1 b/2
+ new file mode 100644
+ rename from 1
+ rename to 2
+ EOF
+'
+
+test_done
diff --git a/t/t4137-apply-submodule.sh b/t/t4137-apply-submodule.sh
new file mode 100755
index 0000000..07d5262
--- /dev/null
+++ b/t/t4137-apply-submodule.sh
@@ -0,0 +1,24 @@
+#!/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" >diff &&
+ may_only_be_test_must_fail "$2" &&
+ $2 git apply --index diff
+}
+
+test_submodule_switch_func "apply_index"
+
+apply_3way () {
+ git diff --ignore-submodules=dirty "..$1" >diff &&
+ may_only_be_test_must_fail "$2" &&
+ $2 git apply --3way diff
+}
+
+test_submodule_switch_func "apply_3way"
+
+test_done
diff --git a/t/t4138-apply-ws-expansion.sh b/t/t4138-apply-ws-expansion.sh
new file mode 100755
index 0000000..8bbf826
--- /dev/null
+++ b/t/t4138-apply-ws-expansion.sh
@@ -0,0 +1,121 @@
+#!/bin/sh
+#
+# Copyright (C) 2015 Kyle J. McKay
+#
+
+test_description='git apply test patches with whitespace expansion.'
+
+. ./test-lib.sh
+
+test_expect_success setup '
+ #
+ ## create test-N, patchN.patch, expect-N files
+ #
+
+ # test 1
+ printf "\t%s\n" 1 2 3 4 5 6 >before &&
+ printf "\t%s\n" 1 2 3 >after &&
+ printf "%64s\n" a b c >>after &&
+ printf "\t%s\n" 4 5 6 >>after &&
+ test_expect_code 1 git diff --no-index before after >patch1.patch.raw &&
+ sed -e "s/before/test-1/" -e "s/after/test-1/" patch1.patch.raw >patch1.patch &&
+ printf "%64s\n" 1 2 3 4 5 6 >test-1 &&
+ printf "%64s\n" 1 2 3 a b c 4 5 6 >expect-1 &&
+
+ # test 2
+ printf "\t%s\n" a b c d e f >before &&
+ printf "\t%s\n" a b c >after &&
+ n=10 &&
+ x=1 &&
+ while test $x -lt $n
+ do
+ printf "%63s%d\n" "" $x >>after &&
+ x=$(( $x + 1 )) || return 1
+ done &&
+ printf "\t%s\n" d e f >>after &&
+ test_expect_code 1 git diff --no-index before after >patch2.patch.raw &&
+ sed -e "s/before/test-2/" -e "s/after/test-2/" patch2.patch.raw >patch2.patch &&
+ printf "%64s\n" a b c d e f >test-2 &&
+ printf "%64s\n" a b c >expect-2 &&
+ x=1 &&
+ while test $x -lt $n
+ do
+ printf "%63s%d\n" "" $x >>expect-2 &&
+ x=$(( $x + 1 )) || return 1
+ done &&
+ printf "%64s\n" d e f >>expect-2 &&
+
+ # test 3
+ printf "\t%s\n" a b c d e f >before &&
+ printf "\t%s\n" a b c >after &&
+ n=100 &&
+ x=0 &&
+ while test $x -lt $n
+ do
+ printf "%63s%02d\n" "" $x >>after &&
+ x=$(( $x + 1 )) || return 1
+ done &&
+ printf "\t%s\n" d e f >>after &&
+ test_expect_code 1 git diff --no-index before after >patch3.patch.raw &&
+ sed -e "s/before/test-3/" -e "s/after/test-3/" patch3.patch.raw >patch3.patch &&
+ printf "%64s\n" a b c d e f >test-3 &&
+ printf "%64s\n" a b c >expect-3 &&
+ x=0 &&
+ while test $x -lt $n
+ do
+ printf "%63s%02d\n" "" $x >>expect-3 &&
+ x=$(( $x + 1 )) || return 1
+ done &&
+ printf "%64s\n" d e f >>expect-3 &&
+
+ # test 4
+ >before &&
+ x=0 &&
+ while test $x -lt 50
+ do
+ printf "\t%02d\n" $x >>before &&
+ x=$(( $x + 1 )) || return 1
+ done &&
+ cat before >after &&
+ printf "%64s\n" a b c >>after &&
+ while test $x -lt 100
+ do
+ printf "\t%02d\n" $x >>before &&
+ printf "\t%02d\n" $x >>after &&
+ x=$(( $x + 1 )) || return 1
+ done &&
+ test_expect_code 1 git diff --no-index before after >patch4.patch.raw &&
+ sed -e "s/before/test-4/" -e "s/after/test-4/" patch4.patch.raw >patch4.patch &&
+ >test-4 &&
+ x=0 &&
+ while test $x -lt 50
+ do
+ printf "%63s%02d\n" "" $x >>test-4 &&
+ x=$(( $x + 1 )) || return 1
+ done &&
+ cat test-4 >expect-4 &&
+ printf "%64s\n" a b c >>expect-4 &&
+ while test $x -lt 100
+ do
+ printf "%63s%02d\n" "" $x >>test-4 &&
+ printf "%63s%02d\n" "" $x >>expect-4 &&
+ x=$(( $x + 1 )) || return 1
+ done &&
+
+ git config core.whitespace tab-in-indent,tabwidth=63 &&
+ git config apply.whitespace fix
+
+'
+
+# Note that `patch` can successfully apply all patches when run
+# with the --ignore-whitespace option.
+
+for t in 1 2 3 4
+do
+ test_expect_success 'apply with ws expansion (t=$t)' '
+ git apply patch$t.patch &&
+ test_cmp expect-$t test-$t
+ '
+done
+
+test_done
diff --git a/t/t4139-apply-escape.sh b/t/t4139-apply-escape.sh
new file mode 100755
index 0000000..e5c7439
--- /dev/null
+++ b/t/t4139-apply-escape.sh
@@ -0,0 +1,143 @@
+#!/bin/sh
+
+test_description='paths written by git-apply cannot escape the working tree'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# tests will try to write to ../foo, and we do not
+# want them to escape the trash directory when they
+# fail
+test_expect_success 'bump git repo one level down' '
+ mkdir inside &&
+ mv .git inside/ &&
+ cd inside
+'
+
+# $1 = name of file
+# $2 = current path to file (if different)
+mkpatch_add () {
+ rm -f "${2:-$1}" &&
+ cat <<-EOF
+ diff --git a/$1 b/$1
+ new file mode 100644
+ index 0000000..53c74cd
+ --- /dev/null
+ +++ b/$1
+ @@ -0,0 +1 @@
+ +evil
+ EOF
+}
+
+mkpatch_del () {
+ echo evil >"${2:-$1}" &&
+ cat <<-EOF
+ diff --git a/$1 b/$1
+ deleted file mode 100644
+ index 53c74cd..0000000
+ --- a/$1
+ +++ /dev/null
+ @@ -1 +0,0 @@
+ -evil
+ EOF
+}
+
+# $1 = name of file
+# $2 = content of symlink
+mkpatch_symlink () {
+ rm -f "$1" &&
+ cat <<-EOF
+ diff --git a/$1 b/$1
+ new file mode 120000
+ index 0000000..$(printf "%s" "$2" | git hash-object --stdin)
+ --- /dev/null
+ +++ b/$1
+ @@ -0,0 +1 @@
+ +$2
+ \ No newline at end of file
+ EOF
+}
+
+test_expect_success 'cannot create file containing ..' '
+ mkpatch_add ../foo >patch &&
+ test_must_fail git apply patch &&
+ test_path_is_missing ../foo
+'
+
+test_expect_success 'can create file containing .. with --unsafe-paths' '
+ mkpatch_add ../foo >patch &&
+ git apply --unsafe-paths patch &&
+ test_path_is_file ../foo
+'
+
+test_expect_success 'cannot create file containing .. (index)' '
+ mkpatch_add ../foo >patch &&
+ test_must_fail git apply --index patch &&
+ test_path_is_missing ../foo
+'
+
+test_expect_success 'cannot create file containing .. with --unsafe-paths (index)' '
+ mkpatch_add ../foo >patch &&
+ test_must_fail git apply --index --unsafe-paths patch &&
+ test_path_is_missing ../foo
+'
+
+test_expect_success 'cannot delete file containing ..' '
+ mkpatch_del ../foo >patch &&
+ test_must_fail git apply patch &&
+ test_path_is_file ../foo
+'
+
+test_expect_success 'can delete file containing .. with --unsafe-paths' '
+ mkpatch_del ../foo >patch &&
+ git apply --unsafe-paths patch &&
+ test_path_is_missing ../foo
+'
+
+test_expect_success 'cannot delete file containing .. (index)' '
+ mkpatch_del ../foo >patch &&
+ test_must_fail git apply --index patch &&
+ test_path_is_file ../foo
+'
+
+test_expect_success SYMLINKS 'symlink escape via ..' '
+ {
+ mkpatch_symlink tmp .. &&
+ mkpatch_add tmp/foo ../foo
+ } >patch &&
+ test_must_fail git apply patch &&
+ test_path_is_missing tmp &&
+ test_path_is_missing ../foo
+'
+
+test_expect_success SYMLINKS 'symlink escape via .. (index)' '
+ {
+ mkpatch_symlink tmp .. &&
+ mkpatch_add tmp/foo ../foo
+ } >patch &&
+ test_must_fail git apply --index patch &&
+ test_path_is_missing tmp &&
+ test_path_is_missing ../foo
+'
+
+test_expect_success SYMLINKS 'symlink escape via absolute path' '
+ {
+ mkpatch_symlink tmp "$(pwd)" &&
+ mkpatch_add tmp/foo ../foo
+ } >patch &&
+ test_must_fail git apply patch &&
+ test_path_is_missing tmp &&
+ test_path_is_missing ../foo
+'
+
+test_expect_success SYMLINKS 'symlink escape via absolute path (index)' '
+ {
+ mkpatch_symlink tmp "$(pwd)" &&
+ mkpatch_add tmp/foo ../foo
+ } >patch &&
+ test_must_fail git apply --index patch &&
+ test_path_is_missing tmp &&
+ test_path_is_missing ../foo
+'
+
+test_done
diff --git a/t/t4140-apply-ita.sh b/t/t4140-apply-ita.sh
new file mode 100755
index 0000000..b375aca
--- /dev/null
+++ b/t/t4140-apply-ita.sh
@@ -0,0 +1,57 @@
+#!/bin/sh
+
+test_description='git apply of i-t-a file'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ test_write_lines 1 2 3 4 5 >blueprint &&
+
+ cat blueprint >test-file &&
+ git add -N test-file &&
+ git diff >creation-patch &&
+ grep "new file mode 100644" creation-patch &&
+
+ rm -f test-file &&
+ git diff >deletion-patch &&
+ grep "deleted file mode 100644" deletion-patch
+'
+
+test_expect_success 'apply creation patch to ita path (--cached)' '
+ git rm -f test-file &&
+ cat blueprint >test-file &&
+ git add -N test-file &&
+
+ git apply --cached creation-patch &&
+ git cat-file blob :test-file >actual &&
+ test_cmp blueprint actual
+'
+
+test_expect_success 'apply creation patch to ita path (--index)' '
+ git rm -f test-file &&
+ cat blueprint >test-file &&
+ git add -N test-file &&
+ rm -f test-file &&
+
+ test_must_fail git apply --index creation-patch
+'
+
+test_expect_success 'apply deletion patch to ita path (--cached)' '
+ git rm -f test-file &&
+ cat blueprint >test-file &&
+ git add -N test-file &&
+
+ git apply --cached deletion-patch &&
+ test_must_fail git ls-files --stage --error-unmatch test-file
+'
+
+test_expect_success 'apply deletion patch to ita path (--index)' '
+ cat blueprint >test-file &&
+ git add -N test-file &&
+
+ test_must_fail git apply --index deletion-patch &&
+ git ls-files --stage --error-unmatch test-file
+'
+
+test_done
diff --git a/t/t4141-apply-too-large.sh b/t/t4141-apply-too-large.sh
new file mode 100755
index 0000000..20cc120
--- /dev/null
+++ b/t/t4141-apply-too-large.sh
@@ -0,0 +1,23 @@
+#!/bin/sh
+
+test_description='git apply with too-large patch'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success EXPENSIVE 'git apply rejects patches that are too large' '
+ sz=$((1024 * 1024 * 1023)) &&
+ {
+ cat <<-\EOF &&
+ diff --git a/file b/file
+ new file mode 100644
+ --- /dev/null
+ +++ b/file
+ @@ -0,0 +1 @@
+ EOF
+ test-tool genzeros
+ } | test_copy_bytes $sz | test_must_fail git apply 2>err &&
+ grep "patch too large" err
+'
+
+test_done
diff --git a/t/t4150-am.sh b/t/t4150-am.sh
new file mode 100755
index 0000000..3b12576
--- /dev/null
+++ b/t/t4150-am.sh
@@ -0,0 +1,1282 @@
+#!/bin/sh
+
+test_description='git am running'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success 'setup: messages' '
+ cat >msg <<-\EOF &&
+ second
+
+ Lorem ipsum dolor sit amet, consectetuer sadipscing elitr, sed diam nonumy
+ eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
+ voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita
+ kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem
+ ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod
+ tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At
+ vero eos et accusam et justo duo dolores et ea rebum.
+
+ EOF
+ qz_to_tab_space <<-\EOF >>msg &&
+ QDuis autem vel eum iriure dolor in hendrerit in vulputate velit
+ Qesse molestie consequat, vel illum dolore eu feugiat nulla facilisis
+ Qat vero eros et accumsan et iusto odio dignissim qui blandit
+ Qpraesent luptatum zzril delenit augue duis dolore te feugait nulla
+ Qfacilisi.
+ EOF
+ cat >>msg <<-\EOF &&
+
+ Lorem ipsum dolor sit amet,
+ consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut
+ laoreet dolore magna aliquam erat volutpat.
+
+ git
+ ---
+ +++
+
+ Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit
+ lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure
+ dolor in hendrerit in vulputate velit esse molestie consequat, vel illum
+ dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio
+ dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te
+ feugait nulla facilisi.
+
+ Reported-by: A N Other <a.n.other@example.com>
+ EOF
+
+ cat >failmail <<-\EOF &&
+ From foo@example.com Fri May 23 10:43:49 2008
+ From: foo@example.com
+ To: bar@example.com
+ Subject: Re: [RFC/PATCH] git-foo.sh
+ Date: Fri, 23 May 2008 05:23:42 +0200
+
+ Sometimes we have to find out that there'\''s nothing left.
+
+ EOF
+
+ cat >pine <<-\EOF &&
+ From MAILER-DAEMON Fri May 23 10:43:49 2008
+ Date: 23 May 2008 05:23:42 +0200
+ From: Mail System Internal Data <MAILER-DAEMON@example.com>
+ Subject: DON'\''T DELETE THIS MESSAGE -- FOLDER INTERNAL DATA
+ Message-ID: <foo-0001@example.com>
+
+ This text is part of the internal format of your mail folder, and is not
+ a real message. It is created automatically by the mail system software.
+ If deleted, important folder data will be lost, and it will be re-created
+ with the data reset to initial values.
+
+ EOF
+
+ cat >msg-without-scissors-line <<-\EOF &&
+ Test that git-am --scissors cuts at the scissors line
+
+ This line should be included in the commit message.
+ EOF
+
+ printf "Subject: " >subject-prefix &&
+
+ cat - subject-prefix msg-without-scissors-line >msg-with-scissors-line <<-\EOF
+ This line should not be included in the commit message with --scissors enabled.
+
+ - - >8 - - remove everything above this line - - >8 - -
+
+ EOF
+'
+
+test_expect_success setup '
+ echo hello >file &&
+ git add file &&
+ test_tick &&
+ git commit -m first &&
+ git tag first &&
+
+ echo world >>file &&
+ git add file &&
+ test_tick &&
+ git commit -F msg &&
+ git tag second &&
+
+ git format-patch --stdout first >patch1 &&
+ {
+ echo "Message-ID: <1226501681-24923-1-git-send-email-bda@mnsspb.ru>" &&
+ echo "X-Fake-Field: Line One" &&
+ echo "X-Fake-Field: Line Two" &&
+ echo "X-Fake-Field: Line Three" &&
+ git format-patch --stdout first | sed -e "1d"
+ } > patch1.eml &&
+ {
+ echo "X-Fake-Field: Line One" &&
+ echo "X-Fake-Field: Line Two" &&
+ echo "X-Fake-Field: Line Three" &&
+ git format-patch --stdout first | sed -e "1d"
+ } | append_cr >patch1-crlf.eml &&
+ {
+ printf "%255s\\n" "" &&
+ echo "X-Fake-Field: Line One" &&
+ echo "X-Fake-Field: Line Two" &&
+ echo "X-Fake-Field: Line Three" &&
+ git format-patch --stdout first | sed -e "1d"
+ } > patch1-ws.eml &&
+ {
+ sed -ne "1p" msg &&
+ echo &&
+ echo "From: $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>" &&
+ echo "Date: $GIT_AUTHOR_DATE" &&
+ echo &&
+ sed -e "1,2d" msg &&
+ echo "---" &&
+ git diff-tree --no-commit-id --stat -p second
+ } >patch1-stgit.eml &&
+ mkdir stgit-series &&
+ cp patch1-stgit.eml stgit-series/patch &&
+ {
+ echo "# This series applies on GIT commit $(git rev-parse first)" &&
+ echo "patch"
+ } >stgit-series/series &&
+ {
+ echo "# HG changeset patch" &&
+ echo "# User $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>" &&
+ echo "# Date $test_tick 25200" &&
+ echo "# $(git show --pretty="%aD" -s second)" &&
+ echo "# Node ID $ZERO_OID" &&
+ echo "# Parent $ZERO_OID" &&
+ cat msg &&
+ echo &&
+ git diff-tree --no-commit-id -p second
+ } >patch1-hg.eml &&
+
+
+ echo file >file &&
+ git add file &&
+ git commit -F msg-without-scissors-line &&
+ git tag expected-for-scissors &&
+ git reset --hard HEAD^ &&
+
+ echo file >file &&
+ git add file &&
+ git commit -F msg-with-scissors-line &&
+ git tag expected-for-no-scissors &&
+ git format-patch --stdout expected-for-no-scissors^ >patch-with-scissors-line.eml &&
+ git reset --hard HEAD^ &&
+
+ sed -n -e "3,\$p" msg >file &&
+ git add file &&
+ test_tick &&
+ git commit -m third &&
+
+ git format-patch --stdout first >patch2 &&
+
+ git checkout -b lorem &&
+ sed -n -e "11,\$p" msg >file &&
+ head -n 9 msg >>file &&
+ test_tick &&
+ git commit -a -m "moved stuff" &&
+
+ echo goodbye >another &&
+ git add another &&
+ test_tick &&
+ git commit -m "added another file" &&
+
+ git format-patch --stdout main >lorem-move.patch &&
+ git format-patch --no-prefix --stdout main >lorem-zero.patch &&
+
+ git checkout -b rename &&
+ git mv file renamed &&
+ git commit -m "renamed a file" &&
+
+ git format-patch -M --stdout lorem >rename.patch &&
+
+ git reset --soft lorem^ &&
+ git commit -m "renamed a file and added another" &&
+
+ git format-patch -M --stdout lorem^ >rename-add.patch &&
+
+ git checkout -b empty-commit &&
+ git commit -m "empty commit" --allow-empty &&
+
+ : >empty.patch &&
+ git format-patch --always --stdout empty-commit^ >empty-commit.patch &&
+
+ # reset time
+ sane_unset test_tick &&
+ test_tick
+'
+
+test_expect_success 'am applies patch correctly' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout first &&
+ test_tick &&
+ git am <patch1 &&
+ test_path_is_missing .git/rebase-apply &&
+ git diff --exit-code second &&
+ test "$(git rev-parse second)" = "$(git rev-parse HEAD)" &&
+ test "$(git rev-parse second^)" = "$(git rev-parse HEAD^)"
+'
+
+test_expect_success 'am fails if index is dirty' '
+ test_when_finished "rm -f dirtyfile" &&
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout first &&
+ echo dirtyfile >dirtyfile &&
+ git add dirtyfile &&
+ test_must_fail git am patch1 &&
+ test_path_is_dir .git/rebase-apply &&
+ test_cmp_rev first HEAD
+'
+
+test_expect_success 'am applies patch e-mail not in a mbox' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout first &&
+ git am patch1.eml &&
+ test_path_is_missing .git/rebase-apply &&
+ git diff --exit-code second &&
+ test "$(git rev-parse second)" = "$(git rev-parse HEAD)" &&
+ test "$(git rev-parse second^)" = "$(git rev-parse HEAD^)"
+'
+
+test_expect_success 'am applies patch e-mail not in a mbox with CRLF' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout first &&
+ git am patch1-crlf.eml &&
+ test_path_is_missing .git/rebase-apply &&
+ git diff --exit-code second &&
+ test "$(git rev-parse second)" = "$(git rev-parse HEAD)" &&
+ test "$(git rev-parse second^)" = "$(git rev-parse HEAD^)"
+'
+
+test_expect_success 'am applies patch e-mail with preceding whitespace' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout first &&
+ git am patch1-ws.eml &&
+ test_path_is_missing .git/rebase-apply &&
+ git diff --exit-code second &&
+ test "$(git rev-parse second)" = "$(git rev-parse HEAD)" &&
+ test "$(git rev-parse second^)" = "$(git rev-parse HEAD^)"
+'
+
+test_expect_success 'am applies stgit patch' '
+ rm -fr .git/rebase-apply &&
+ git checkout -f first &&
+ git am patch1-stgit.eml &&
+ test_path_is_missing .git/rebase-apply &&
+ git diff --exit-code second &&
+ test_cmp_rev second HEAD &&
+ test_cmp_rev second^ HEAD^
+'
+
+test_expect_success 'am --patch-format=stgit applies stgit patch' '
+ rm -fr .git/rebase-apply &&
+ git checkout -f first &&
+ git am --patch-format=stgit <patch1-stgit.eml &&
+ test_path_is_missing .git/rebase-apply &&
+ git diff --exit-code second &&
+ test_cmp_rev second HEAD &&
+ test_cmp_rev second^ HEAD^
+'
+
+test_expect_success 'am applies stgit series' '
+ rm -fr .git/rebase-apply &&
+ git checkout -f first &&
+ git am stgit-series/series &&
+ test_path_is_missing .git/rebase-apply &&
+ git diff --exit-code second &&
+ test_cmp_rev second HEAD &&
+ test_cmp_rev second^ HEAD^
+'
+
+test_expect_success 'am applies hg patch' '
+ rm -fr .git/rebase-apply &&
+ git checkout -f first &&
+ git am patch1-hg.eml &&
+ test_path_is_missing .git/rebase-apply &&
+ git diff --exit-code second &&
+ test_cmp_rev second HEAD &&
+ test_cmp_rev second^ HEAD^
+'
+
+test_expect_success 'am --patch-format=hg applies hg patch' '
+ rm -fr .git/rebase-apply &&
+ git checkout -f first &&
+ git am --patch-format=hg <patch1-hg.eml &&
+ test_path_is_missing .git/rebase-apply &&
+ git diff --exit-code second &&
+ test_cmp_rev second HEAD &&
+ test_cmp_rev second^ HEAD^
+'
+
+test_expect_success 'am with applypatch-msg hook' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout first &&
+ test_hook applypatch-msg <<-\EOF &&
+ cat "$1" >actual-msg &&
+ echo hook-message >"$1"
+ EOF
+ git am patch1 &&
+ test_path_is_missing .git/rebase-apply &&
+ git diff --exit-code second &&
+ echo hook-message >expected &&
+ git log -1 --format=format:%B >actual &&
+ test_cmp expected actual &&
+ git log -1 --format=format:%B second >expected &&
+ test_cmp expected actual-msg
+'
+
+test_expect_success 'am with failing applypatch-msg hook' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout first &&
+ test_hook applypatch-msg <<-\EOF &&
+ exit 1
+ EOF
+ test_must_fail git am patch1 &&
+ test_path_is_dir .git/rebase-apply &&
+ git diff --exit-code first &&
+ test_cmp_rev first HEAD
+'
+
+test_expect_success 'am with failing applypatch-msg hook (no verify)' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout first &&
+ test_hook applypatch-msg <<-\EOF &&
+ echo hook-message >"$1"
+ exit 1
+ EOF
+ git am --no-verify patch1 &&
+ test_path_is_missing .git/rebase-apply &&
+ git diff --exit-code second &&
+ git log -1 --format=format:%B >actual &&
+ test_cmp msg actual
+'
+
+test_expect_success 'am with pre-applypatch hook' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout first &&
+ test_hook pre-applypatch <<-\EOF &&
+ git diff first >diff.actual
+ exit 0
+ EOF
+ git am patch1 &&
+ test_path_is_missing .git/rebase-apply &&
+ git diff --exit-code second &&
+ test_cmp_rev second HEAD &&
+ git diff first..second >diff.expected &&
+ test_cmp diff.expected diff.actual
+'
+
+test_expect_success 'am with failing pre-applypatch hook' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout first &&
+ test_hook pre-applypatch <<-\EOF &&
+ exit 1
+ EOF
+ test_must_fail git am patch1 &&
+ test_path_is_dir .git/rebase-apply &&
+ git diff --exit-code second &&
+ test_cmp_rev first HEAD
+'
+
+test_expect_success 'am with failing pre-applypatch hook (no verify)' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout first &&
+ touch empty-file &&
+ test_hook pre-applypatch <<-\EOF &&
+ rm empty-file
+ exit 1
+ EOF
+ git am --no-verify patch1 &&
+ test_path_is_missing .git/rebase-apply &&
+ test_path_is_file empty-file &&
+ git diff --exit-code second &&
+ git log -1 --format=format:%B >actual &&
+ test_cmp msg actual
+'
+
+test_expect_success 'am with post-applypatch hook' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout first &&
+ test_hook post-applypatch <<-\EOF &&
+ git rev-parse HEAD >head.actual
+ git diff second >diff.actual
+ exit 0
+ EOF
+ git am patch1 &&
+ test_path_is_missing .git/rebase-apply &&
+ test_cmp_rev second HEAD &&
+ git rev-parse second >head.expected &&
+ test_cmp head.expected head.actual &&
+ git diff second >diff.expected &&
+ test_cmp diff.expected diff.actual
+'
+
+test_expect_success 'am with failing post-applypatch hook' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout first &&
+ test_hook post-applypatch <<-\EOF &&
+ git rev-parse HEAD >head.actual
+ exit 1
+ EOF
+ git am patch1 &&
+ test_path_is_missing .git/rebase-apply &&
+ git diff --exit-code second &&
+ test_cmp_rev second HEAD &&
+ git rev-parse second >head.expected &&
+ test_cmp head.expected head.actual
+'
+
+test_expect_success 'am --scissors cuts the message at the scissors line' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout second &&
+ git am --scissors patch-with-scissors-line.eml &&
+ test_path_is_missing .git/rebase-apply &&
+ git diff --exit-code expected-for-scissors &&
+ test_cmp_rev expected-for-scissors HEAD
+'
+
+test_expect_success 'am --no-scissors overrides mailinfo.scissors' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout second &&
+ test_config mailinfo.scissors true &&
+ git am --no-scissors patch-with-scissors-line.eml &&
+ test_path_is_missing .git/rebase-apply &&
+ git diff --exit-code expected-for-no-scissors &&
+ test_cmp_rev expected-for-no-scissors HEAD
+'
+
+test_expect_success 'setup: new author and committer' '
+ GIT_AUTHOR_NAME="Another Thor" &&
+ GIT_AUTHOR_EMAIL="a.thor@example.com" &&
+ GIT_COMMITTER_NAME="Co M Miter" &&
+ GIT_COMMITTER_EMAIL="c.miter@example.com" &&
+ export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL
+'
+
+compare () {
+ a=$(git cat-file commit "$2" | grep "^$1 ") &&
+ b=$(git cat-file commit "$3" | grep "^$1 ") &&
+ test "$a" = "$b"
+}
+
+test_expect_success 'am changes committer and keeps author' '
+ test_tick &&
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout first &&
+ git am patch2 &&
+ test_path_is_missing .git/rebase-apply &&
+ test "$(git rev-parse main^^)" = "$(git rev-parse HEAD^^)" &&
+ git diff --exit-code main..HEAD &&
+ git diff --exit-code main^..HEAD^ &&
+ compare author main HEAD &&
+ compare author main^ HEAD^ &&
+ test "$GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>" = \
+ "$(git log -1 --pretty=format:"%cn <%ce>" HEAD)"
+'
+
+test_expect_success 'am --signoff adds Signed-off-by: line' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout -b topic_2 first &&
+ git am --signoff <patch2 &&
+ {
+ printf "third\n\nSigned-off-by: %s <%s>\n\n" \
+ "$GIT_COMMITTER_NAME" "$GIT_COMMITTER_EMAIL" &&
+ cat msg &&
+ printf "Signed-off-by: %s <%s>\n\n" \
+ "$GIT_COMMITTER_NAME" "$GIT_COMMITTER_EMAIL"
+ } >expected-log &&
+ git log --pretty=%B -2 HEAD >actual &&
+ test_cmp expected-log actual
+'
+
+test_expect_success 'am stays in branch' '
+ echo refs/heads/topic_2 >expected &&
+ git symbolic-ref HEAD >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'am --signoff does not add Signed-off-by: line if already there' '
+ git format-patch --stdout first >patch3 &&
+ git reset --hard first &&
+ git am --signoff <patch3 &&
+ git log --pretty=%B -2 HEAD >actual &&
+ test_cmp expected-log actual
+'
+
+test_expect_success 'am --signoff adds Signed-off-by: if another author is preset' '
+ NAME="A N Other" &&
+ EMAIL="a.n.other@example.com" &&
+ {
+ printf "third\n\nSigned-off-by: %s <%s>\nSigned-off-by: %s <%s>\n\n" \
+ "$GIT_COMMITTER_NAME" "$GIT_COMMITTER_EMAIL" \
+ "$NAME" "$EMAIL" &&
+ cat msg &&
+ printf "Signed-off-by: %s <%s>\nSigned-off-by: %s <%s>\n\n" \
+ "$GIT_COMMITTER_NAME" "$GIT_COMMITTER_EMAIL" \
+ "$NAME" "$EMAIL"
+ } >expected-log &&
+ git reset --hard first &&
+ GIT_COMMITTER_NAME="$NAME" GIT_COMMITTER_EMAIL="$EMAIL" \
+ git am --signoff <patch3 &&
+ git log --pretty=%B -2 HEAD >actual &&
+ test_cmp expected-log actual
+'
+
+test_expect_success 'am --signoff duplicates Signed-off-by: if it is not the last one' '
+ NAME="A N Other" &&
+ EMAIL="a.n.other@example.com" &&
+ {
+ printf "third\n\nSigned-off-by: %s <%s>\n\
+Signed-off-by: %s <%s>\nSigned-off-by: %s <%s>\n\n" \
+ "$GIT_COMMITTER_NAME" "$GIT_COMMITTER_EMAIL" \
+ "$NAME" "$EMAIL" \
+ "$GIT_COMMITTER_NAME" "$GIT_COMMITTER_EMAIL" &&
+ cat msg &&
+ printf "Signed-off-by: %s <%s>\nSigned-off-by: %s <%s>\n\
+Signed-off-by: %s <%s>\n\n" \
+ "$GIT_COMMITTER_NAME" "$GIT_COMMITTER_EMAIL" \
+ "$NAME" "$EMAIL" \
+ "$GIT_COMMITTER_NAME" "$GIT_COMMITTER_EMAIL"
+ } >expected-log &&
+ git format-patch --stdout first >patch3 &&
+ git reset --hard first &&
+ git am --signoff <patch3 &&
+ git log --pretty=%B -2 HEAD >actual &&
+ test_cmp expected-log actual
+'
+
+test_expect_success 'am without --keep removes Re: and [PATCH] stuff' '
+ git format-patch --stdout HEAD^ >tmp &&
+ sed -e "/^Subject/ s,\[PATCH,Re: Re: Re: & 1/5 v2] [foo," tmp >patch4 &&
+ git reset --hard HEAD^ &&
+ git am <patch4 &&
+ git rev-parse HEAD >expected &&
+ git rev-parse topic_2 >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'am --keep really keeps the subject' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout HEAD^ &&
+ git am --keep patch4 &&
+ test_path_is_missing .git/rebase-apply &&
+ git cat-file commit HEAD >actual &&
+ grep "Re: Re: Re: \[PATCH 1/5 v2\] \[foo\] third" actual
+'
+
+test_expect_success 'am --keep-non-patch really keeps the non-patch part' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout HEAD^ &&
+ git am --keep-non-patch patch4 &&
+ test_path_is_missing .git/rebase-apply &&
+ git cat-file commit HEAD >actual &&
+ grep "^\[foo\] third" actual
+'
+
+test_expect_success 'setup am -3' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout -b base3way topic_2 &&
+ sed -n -e "3,\$p" msg >file &&
+ head -n 9 msg >>file &&
+ git add file &&
+ test_tick &&
+ git commit -m "copied stuff"
+'
+
+test_expect_success 'am -3 falls back to 3-way merge' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout -b lorem2 base3way &&
+ git am -3 lorem-move.patch &&
+ test_path_is_missing .git/rebase-apply &&
+ git diff --exit-code lorem
+'
+
+test_expect_success 'am -3 -p0 can read --no-prefix patch' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout -b lorem3 base3way &&
+ git am -3 -p0 lorem-zero.patch &&
+ test_path_is_missing .git/rebase-apply &&
+ git diff --exit-code lorem
+'
+
+test_expect_success 'am with config am.threeWay falls back to 3-way merge' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout -b lorem4 base3way &&
+ test_config am.threeWay 1 &&
+ git am lorem-move.patch &&
+ test_path_is_missing .git/rebase-apply &&
+ git diff --exit-code lorem
+'
+
+test_expect_success 'am with config am.threeWay overridden by --no-3way' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout -b lorem5 base3way &&
+ test_config am.threeWay 1 &&
+ test_must_fail git am --no-3way lorem-move.patch &&
+ test_path_is_dir .git/rebase-apply
+'
+
+test_expect_success 'am can rename a file' '
+ grep "^rename from" rename.patch &&
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout lorem^0 &&
+ git am rename.patch &&
+ test_path_is_missing .git/rebase-apply &&
+ git update-index --refresh &&
+ git diff --exit-code rename
+'
+
+test_expect_success 'am -3 can rename a file' '
+ grep "^rename from" rename.patch &&
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout lorem^0 &&
+ git am -3 rename.patch &&
+ test_path_is_missing .git/rebase-apply &&
+ git update-index --refresh &&
+ git diff --exit-code rename
+'
+
+test_expect_success 'am -3 can rename a file after falling back to 3-way merge' '
+ grep "^rename from" rename-add.patch &&
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout lorem^0 &&
+ git am -3 rename-add.patch &&
+ test_path_is_missing .git/rebase-apply &&
+ git update-index --refresh &&
+ git diff --exit-code rename
+'
+
+test_expect_success 'am -3 -q is quiet' '
+ rm -fr .git/rebase-apply &&
+ git checkout -f lorem2 &&
+ git reset base3way --hard &&
+ git am -3 -q lorem-move.patch >output.out 2>&1 &&
+ test_must_be_empty output.out
+'
+
+test_expect_success 'am pauses on conflict' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout lorem2^^ &&
+ test_must_fail git am lorem-move.patch &&
+ test -d .git/rebase-apply
+'
+
+test_expect_success 'am --show-current-patch' '
+ git am --show-current-patch >actual.patch &&
+ test_cmp .git/rebase-apply/0001 actual.patch
+'
+
+test_expect_success 'am --show-current-patch=raw' '
+ git am --show-current-patch=raw >actual.patch &&
+ test_cmp .git/rebase-apply/0001 actual.patch
+'
+
+test_expect_success 'am --show-current-patch=diff' '
+ git am --show-current-patch=diff >actual.patch &&
+ test_cmp .git/rebase-apply/patch actual.patch
+'
+
+test_expect_success 'am accepts repeated --show-current-patch' '
+ git am --show-current-patch --show-current-patch=raw >actual.patch &&
+ test_cmp .git/rebase-apply/0001 actual.patch
+'
+
+test_expect_success 'am detects incompatible --show-current-patch' '
+ test_must_fail git am --show-current-patch=raw --show-current-patch=diff &&
+ test_must_fail git am --show-current-patch --show-current-patch=diff
+'
+
+test_expect_success 'am --skip works' '
+ echo goodbye >expected &&
+ git am --skip &&
+ test_path_is_missing .git/rebase-apply &&
+ git diff --exit-code lorem2^^ -- file &&
+ test_cmp expected another
+'
+
+test_expect_success 'am --abort removes a stray directory' '
+ mkdir .git/rebase-apply &&
+ git am --abort &&
+ test_path_is_missing .git/rebase-apply
+'
+
+test_expect_success 'am refuses patches when paused' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout lorem2^^ &&
+
+ test_must_fail git am lorem-move.patch &&
+ test_path_is_dir .git/rebase-apply &&
+ test_cmp_rev lorem2^^ HEAD &&
+
+ test_must_fail git am <lorem-move.patch &&
+ test_path_is_dir .git/rebase-apply &&
+ test_cmp_rev lorem2^^ HEAD
+'
+
+test_expect_success 'am --resolved works' '
+ echo goodbye >expected &&
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout lorem2^^ &&
+ test_must_fail git am lorem-move.patch &&
+ test -d .git/rebase-apply &&
+ echo resolved >>file &&
+ git add file &&
+ git am --resolved &&
+ test_path_is_missing .git/rebase-apply &&
+ test_cmp expected another
+'
+
+test_expect_success 'am --resolved fails if index has no changes' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout lorem2^^ &&
+ test_must_fail git am lorem-move.patch &&
+ test_path_is_dir .git/rebase-apply &&
+ test_cmp_rev lorem2^^ HEAD &&
+ test_must_fail git am --resolved &&
+ test_path_is_dir .git/rebase-apply &&
+ test_cmp_rev lorem2^^ HEAD
+'
+
+test_expect_success 'am --resolved fails if index has unmerged entries' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout second &&
+ test_must_fail git am -3 lorem-move.patch &&
+ test_path_is_dir .git/rebase-apply &&
+ test_cmp_rev second HEAD &&
+ test_must_fail git am --resolved >err &&
+ test_path_is_dir .git/rebase-apply &&
+ test_cmp_rev second HEAD &&
+ test_grep "still have unmerged paths" err
+'
+
+test_expect_success 'am takes patches from a Pine mailbox' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout first &&
+ cat pine patch1 | git am &&
+ test_path_is_missing .git/rebase-apply &&
+ git diff --exit-code main^..HEAD
+'
+
+test_expect_success 'am fails on mail without patch' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ test_must_fail git am <failmail &&
+ git am --abort &&
+ test_path_is_missing .git/rebase-apply
+'
+
+test_expect_success 'am fails on empty patch' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ echo "---" >>failmail &&
+ test_must_fail git am <failmail &&
+ git am --skip &&
+ test_path_is_missing .git/rebase-apply
+'
+
+test_expect_success 'am works from stdin in subdirectory' '
+ rm -fr subdir &&
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout first &&
+ (
+ mkdir -p subdir &&
+ cd subdir &&
+ git am <../patch1
+ ) &&
+ git diff --exit-code second
+'
+
+test_expect_success 'am works from file (relative path given) in subdirectory' '
+ rm -fr subdir &&
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout first &&
+ (
+ mkdir -p subdir &&
+ cd subdir &&
+ git am ../patch1
+ ) &&
+ git diff --exit-code second
+'
+
+test_expect_success 'am works from file (absolute path given) in subdirectory' '
+ rm -fr subdir &&
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout first &&
+ P=$(pwd) &&
+ (
+ mkdir -p subdir &&
+ cd subdir &&
+ git am "$P/patch1"
+ ) &&
+ git diff --exit-code second
+'
+
+test_expect_success 'am --committer-date-is-author-date' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout first &&
+ test_tick &&
+ git am --committer-date-is-author-date patch1 &&
+ git cat-file commit HEAD | sed -e "/^\$/q" >head1 &&
+ sed -ne "/^author /s/.*> //p" head1 >at &&
+ sed -ne "/^committer /s/.*> //p" head1 >ct &&
+ test_cmp at ct
+'
+
+test_expect_success 'am without --committer-date-is-author-date' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout first &&
+ test_tick &&
+ git am patch1 &&
+ git cat-file commit HEAD | sed -e "/^\$/q" >head1 &&
+ sed -ne "/^author /s/.*> //p" head1 >at &&
+ sed -ne "/^committer /s/.*> //p" head1 >ct &&
+ ! test_cmp at ct
+'
+
+# This checks for +0000 because TZ is set to UTC and that should
+# show up when the current time is used. The date in message is set
+# by test_tick that uses -0700 timezone; if this feature does not
+# work, we will see that instead of +0000.
+test_expect_success 'am --ignore-date' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout first &&
+ test_tick &&
+ git am --ignore-date patch1 &&
+ git cat-file commit HEAD | sed -e "/^\$/q" >head1 &&
+ sed -ne "/^author /s/.*> //p" head1 >at &&
+ grep "+0000" at
+'
+
+test_expect_success 'am into an unborn branch' '
+ git rev-parse first^{tree} >expected &&
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ rm -fr subdir &&
+ mkdir subdir &&
+ git format-patch --numbered-files -o subdir -1 first &&
+ (
+ cd subdir &&
+ git init &&
+ git am 1
+ ) &&
+ (
+ cd subdir &&
+ git rev-parse HEAD^{tree} >../actual
+ ) &&
+ test_cmp expected actual
+'
+
+test_expect_success 'am newline in subject' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout first &&
+ test_tick &&
+ sed -e "s/second/second \\\n foo/" patch1 >patchnl &&
+ git am <patchnl >output.out 2>&1 &&
+ test_grep "^Applying: second \\\n foo$" output.out
+'
+
+test_expect_success 'am -q is quiet' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout first &&
+ test_tick &&
+ git am -q <patch1 >output.out 2>&1 &&
+ test_must_be_empty output.out
+'
+
+test_expect_success 'am empty-file does not infloop' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ touch empty-file &&
+ test_tick &&
+ test_must_fail git am empty-file 2>actual &&
+ echo Patch format detection failed. >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'am --message-id really adds the message id' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout HEAD^ &&
+ git am --message-id patch1.eml &&
+ test_path_is_missing .git/rebase-apply &&
+ git cat-file commit HEAD | tail -n1 >actual &&
+ grep Message-ID patch1.eml >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'am.messageid really adds the message id' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout HEAD^ &&
+ test_config am.messageid true &&
+ git am patch1.eml &&
+ test_path_is_missing .git/rebase-apply &&
+ git cat-file commit HEAD | tail -n1 >actual &&
+ grep Message-ID patch1.eml >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'am --message-id -s signs off after the message id' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout HEAD^ &&
+ git am -s --message-id patch1.eml &&
+ test_path_is_missing .git/rebase-apply &&
+ git cat-file commit HEAD | tail -n2 | head -n1 >actual &&
+ grep Message-ID patch1.eml >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'am -3 works with rerere' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+
+ # make patches one->two and two->three...
+ test_commit one file &&
+ test_commit two file &&
+ test_commit three file &&
+ git format-patch -2 --stdout >seq.patch &&
+
+ # and create a situation that conflicts...
+ git reset --hard one &&
+ test_commit other file &&
+
+ # enable rerere...
+ test_config rerere.enabled true &&
+ test_when_finished "rm -rf .git/rr-cache" &&
+
+ # ...and apply. Our resolution is to skip the first
+ # patch, and the rerere the second one.
+ test_must_fail git am -3 seq.patch &&
+ test_must_fail git am --skip &&
+ echo resolved >file &&
+ git add file &&
+ git am --resolved &&
+
+ # now apply again, and confirm that rerere engaged (we still
+ # expect failure from am because rerere does not auto-commit
+ # for us).
+ git reset --hard other &&
+ test_must_fail git am -3 seq.patch &&
+ test_must_fail git am --skip &&
+ echo resolved >expect &&
+ test_cmp expect file
+'
+
+test_expect_success 'am -s unexpected trailer block' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ echo signed >file &&
+ git add file &&
+ cat >msg <<-EOF &&
+ subject here
+
+ Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
+ [jc: tweaked log message]
+ Signed-off-by: J C H <j@c.h>
+ EOF
+ git commit -F msg &&
+ git cat-file commit HEAD | sed -e "1,/^$/d" >original &&
+ git format-patch --stdout -1 >patch &&
+
+ git reset --hard HEAD^ &&
+ git am -s patch &&
+ (
+ cat original &&
+ echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
+ ) >expect &&
+ git cat-file commit HEAD | sed -e "1,/^$/d" >actual &&
+ test_cmp expect actual &&
+
+ cat >msg <<-\EOF &&
+ subject here
+
+ We make sure that there is a blank line between the log
+ message proper and Signed-off-by: line added.
+ EOF
+ git reset HEAD^ &&
+ git commit -F msg file &&
+ git cat-file commit HEAD | sed -e "1,/^$/d" >original &&
+ git format-patch --stdout -1 >patch &&
+
+ git reset --hard HEAD^ &&
+ git am -s patch &&
+
+ (
+ cat original &&
+ echo &&
+ echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
+ ) >expect &&
+ git cat-file commit HEAD | sed -e "1,/^$/d" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'am --patch-format=mboxrd handles mboxrd' '
+ rm -fr .git/rebase-apply &&
+ git checkout -f first &&
+ echo mboxrd >>file &&
+ git add file &&
+ cat >msg <<-\INPUT_END &&
+ mboxrd should escape the body
+
+ From could trip up a loose mbox parser
+ >From extra escape for reversibility
+ INPUT_END
+ git commit -F msg &&
+ git -c format.mboxrd format-patch --stdout -1 >mboxrd1 &&
+ grep "^>From could trip up a loose mbox parser" mboxrd1 &&
+ git checkout -f first &&
+ git am --patch-format=mboxrd mboxrd1 &&
+ git cat-file commit HEAD | tail -n4 >out &&
+ test_cmp msg out
+'
+
+test_expect_success 'am works with multi-line in-body headers' '
+ FORTY="String that has a length of more than forty characters" &&
+ LONG="$FORTY $FORTY" &&
+ rm -fr .git/rebase-apply &&
+ git checkout -f first &&
+ echo one >> file &&
+ git commit -am "$LONG
+
+ Body test" --author="$LONG <long@example.com>" &&
+ git format-patch --stdout -1 >patch &&
+ # bump from, date, and subject down to in-body header
+ perl -lpe "
+ if (/^From:/) {
+ print \"From: x <x\@example.com>\";
+ print \"Date: Sat, 1 Jan 2000 00:00:00 +0000\";
+ print \"Subject: x\n\";
+ }
+ " patch >msg &&
+ git checkout HEAD^ &&
+ git am msg &&
+ # Ensure that the author and full message are present
+ git cat-file commit HEAD | grep "^author.*long@example.com" &&
+ git cat-file commit HEAD | grep "^$LONG$"
+'
+
+test_expect_success 'am --quit keeps HEAD where it is' '
+ mkdir .git/rebase-apply &&
+ >.git/rebase-apply/last &&
+ >.git/rebase-apply/next &&
+ git rev-parse HEAD^ >.git/ORIG_HEAD &&
+ git rev-parse HEAD >expected &&
+ git am --quit &&
+ test_path_is_missing .git/rebase-apply &&
+ git rev-parse HEAD >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'am and .gitattibutes' '
+ test_create_repo attributes &&
+ (
+ cd attributes &&
+ test_commit init &&
+ git config filter.test.clean "sed -e '\''s/smudged/clean/g'\''" &&
+ git config filter.test.smudge "sed -e '\''s/clean/smudged/g'\''" &&
+
+ test_commit second &&
+ git checkout -b test HEAD^ &&
+
+ echo "*.txt filter=test conflict-marker-size=10" >.gitattributes &&
+ git add .gitattributes &&
+ test_commit third &&
+
+ echo "This text is smudged." >a.txt &&
+ git add a.txt &&
+ test_commit fourth &&
+
+ git checkout -b removal HEAD^ &&
+ git rm .gitattributes &&
+ git add -u &&
+ test_commit fifth &&
+ git cherry-pick test &&
+
+ git checkout -b conflict third &&
+ echo "This text is different." >a.txt &&
+ git add a.txt &&
+ test_commit sixth &&
+
+ git checkout test &&
+ git format-patch --stdout main..HEAD >patches &&
+ git reset --hard main &&
+ git am patches &&
+ grep "smudged" a.txt &&
+
+ git checkout removal &&
+ git reset --hard &&
+ git format-patch --stdout main..HEAD >patches &&
+ git reset --hard main &&
+ git am patches &&
+ grep "clean" a.txt &&
+
+ git checkout conflict &&
+ git reset --hard &&
+ git format-patch --stdout main..HEAD >patches &&
+ git reset --hard fourth &&
+ test_must_fail git am -3 patches &&
+ grep "<<<<<<<<<<" a.txt
+ )
+'
+
+test_expect_success 'apply binary blob in partial clone' '
+ printf "\\000" >binary &&
+ git add binary &&
+ git commit -m "binary blob" &&
+ git format-patch --stdout -m HEAD^ >patch &&
+
+ test_create_repo server &&
+ test_config -C server uploadpack.allowfilter 1 &&
+ test_config -C server uploadpack.allowanysha1inwant 1 &&
+ git clone --filter=blob:none "file://$(pwd)/server" client &&
+ test_when_finished "rm -rf client" &&
+
+ # Exercise to make sure that it works
+ git -C client am ../patch
+'
+
+test_expect_success 'an empty input file is error regardless of --empty option' '
+ test_when_finished "git am --abort || :" &&
+ test_must_fail git am --empty=drop empty.patch 2>actual &&
+ echo "Patch format detection failed." >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'invalid when passing the --empty option alone' '
+ test_when_finished "git am --abort || :" &&
+ git checkout empty-commit^ &&
+ test_must_fail git am --empty empty-commit.patch 2>err &&
+ echo "error: invalid value for '\''--empty'\'': '\''empty-commit.patch'\''" >expected &&
+ test_cmp expected err
+'
+
+test_expect_success 'a message without a patch is an error (default)' '
+ test_when_finished "git am --abort || :" &&
+ test_must_fail git am empty-commit.patch >err &&
+ grep "Patch is empty" err
+'
+
+test_expect_success 'a message without a patch is an error where an explicit "--empty=stop" is given' '
+ test_when_finished "git am --abort || :" &&
+ test_must_fail git am --empty=stop empty-commit.patch >err &&
+ grep "Patch is empty." err
+'
+
+test_expect_success 'a message without a patch will be skipped when "--empty=drop" is given' '
+ git am --empty=drop empty-commit.patch >output &&
+ git rev-parse empty-commit^ >expected &&
+ git rev-parse HEAD >actual &&
+ test_cmp expected actual &&
+ grep "Skipping: empty commit" output
+'
+
+test_expect_success 'record as an empty commit when meeting e-mail message that lacks a patch' '
+ git am --empty=keep empty-commit.patch >output &&
+ test_path_is_missing .git/rebase-apply &&
+ git show empty-commit --format="%B" >expected &&
+ git show HEAD --format="%B" >actual &&
+ grep -f actual expected &&
+ grep "Creating an empty commit: empty commit" output
+'
+
+test_expect_success 'skip an empty patch in the middle of an am session' '
+ git checkout empty-commit^ &&
+ test_must_fail git am empty-commit.patch >err &&
+ grep "Patch is empty." err &&
+ grep "To record the empty patch as an empty commit, run \"git am --allow-empty\"." err &&
+ git am --skip &&
+ test_path_is_missing .git/rebase-apply &&
+ git rev-parse empty-commit^ >expected &&
+ git rev-parse HEAD >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'record an empty patch as an empty commit in the middle of an am session' '
+ git checkout empty-commit^ &&
+ test_must_fail git am empty-commit.patch >err &&
+ grep "Patch is empty." err &&
+ grep "To record the empty patch as an empty commit, run \"git am --allow-empty\"." err &&
+ git am --allow-empty >output &&
+ grep "No changes - recorded it as an empty commit." output &&
+ test_path_is_missing .git/rebase-apply &&
+ git show empty-commit --format="%B" >expected &&
+ git show HEAD --format="%B" >actual &&
+ grep -f actual expected
+'
+
+test_expect_success 'create an non-empty commit when the index IS changed though "--allow-empty" is given' '
+ git checkout empty-commit^ &&
+ test_must_fail git am empty-commit.patch >err &&
+ : >empty-file &&
+ git add empty-file &&
+ git am --allow-empty &&
+ git show empty-commit --format="%B" >expected &&
+ git show HEAD --format="%B" >actual &&
+ grep -f actual expected &&
+ git diff HEAD^..HEAD --name-only
+'
+
+test_expect_success 'cannot create empty commits when there is a clean index due to merge conflicts' '
+ test_when_finished "git am --abort || :" &&
+ git rev-parse HEAD >expected &&
+ test_must_fail git am seq.patch &&
+ test_must_fail git am --allow-empty >err &&
+ ! grep "To record the empty patch as an empty commit, run \"git am --allow-empty\"." err &&
+ git rev-parse HEAD >actual &&
+ test_cmp actual expected
+'
+
+test_expect_success 'cannot create empty commits when there is unmerged index due to merge conflicts' '
+ test_when_finished "git am --abort || :" &&
+ git rev-parse HEAD >expected &&
+ test_must_fail git am -3 seq.patch &&
+ test_must_fail git am --allow-empty >err &&
+ ! grep "To record the empty patch as an empty commit, run \"git am --allow-empty\"." err &&
+ git rev-parse HEAD >actual &&
+ test_cmp actual expected
+'
+
+test_done
diff --git a/t/t4151-am-abort.sh b/t/t4151-am-abort.sh
new file mode 100755
index 0000000..edb38da
--- /dev/null
+++ b/t/t4151-am-abort.sh
@@ -0,0 +1,227 @@
+#!/bin/sh
+
+test_description='am --abort'
+
+. ./test-lib.sh
+
+test_expect_success setup '
+ test_write_lines a b c d e f g >file-1 &&
+ cp file-1 file-2 &&
+ test_tick &&
+ git add file-1 file-2 &&
+ git commit -m initial &&
+ git tag initial &&
+ git format-patch --stdout --root initial >initial.patch &&
+ for i in 2 3 4 5 6
+ do
+ echo $i >>file-1 &&
+ echo $i >otherfile-$i &&
+ git add otherfile-$i &&
+ test_tick &&
+ git commit -a -m $i || return 1
+ done &&
+ git branch changes &&
+ git format-patch --no-numbered initial &&
+ git checkout -b conflicting initial &&
+ echo different >>file-1 &&
+ echo whatever >new-file &&
+ git add file-1 new-file &&
+ git commit -m different &&
+ git checkout -b side initial &&
+ echo local change >file-2-expect
+'
+
+for with3 in '' ' -3'
+do
+ test_expect_success "am$with3 stops at a patch that does not apply" '
+
+ git reset --hard initial &&
+ cp file-2-expect file-2 &&
+
+ test_must_fail git am$with3 000[1245]-*.patch &&
+ git log --pretty=tformat:%s >actual &&
+ test_write_lines 3 2 initial >expect &&
+ test_cmp expect actual
+ '
+
+ test_expect_success "am$with3 --skip continue after failed am$with3" '
+ test_must_fail git am$with3 --skip >output &&
+ test_grep "^Applying: 6$" output &&
+ test_cmp file-2-expect file-2 &&
+ test ! -f .git/MERGE_RR
+ '
+
+ test_expect_success "am --abort goes back after failed am$with3" '
+ git am --abort &&
+ git rev-parse HEAD >actual &&
+ git rev-parse initial >expect &&
+ test_cmp expect actual &&
+ test_cmp file-2-expect file-2 &&
+ git diff-index --exit-code --cached HEAD &&
+ test ! -f .git/MERGE_RR
+ '
+
+done
+
+test_expect_success 'am -3 --skip removes otherfile-4' '
+ git reset --hard initial &&
+ test_must_fail git am -3 0003-*.patch &&
+ test 3 -eq $(git ls-files -u | wc -l) &&
+ test 4 = "$(cat otherfile-4)" &&
+ git am --skip &&
+ test_cmp_rev initial HEAD &&
+ test -z "$(git ls-files -u)" &&
+ test_path_is_missing otherfile-4
+'
+
+test_expect_success 'am -3 --abort removes otherfile-4' '
+ git reset --hard initial &&
+ test_must_fail git am -3 0003-*.patch &&
+ test 3 -eq $(git ls-files -u | wc -l) &&
+ test 4 = "$(cat otherfile-4)" &&
+ git am --abort &&
+ test_cmp_rev initial HEAD &&
+ test -z "$(git ls-files -u)" &&
+ test_path_is_missing otherfile-4
+'
+
+test_expect_success 'am --abort will keep the local commits intact' '
+ test_must_fail git am 0004-*.patch &&
+ test_commit unrelated &&
+ git rev-parse HEAD >expect &&
+ git am --abort &&
+ git rev-parse HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'am --abort will keep dirty index intact' '
+ git reset --hard initial &&
+ echo dirtyfile >dirtyfile &&
+ cp dirtyfile dirtyfile.expected &&
+ git add dirtyfile &&
+ test_must_fail git am 0001-*.patch &&
+ test_cmp_rev initial HEAD &&
+ test_path_is_file dirtyfile &&
+ test_cmp dirtyfile.expected dirtyfile &&
+ git am --abort &&
+ test_cmp_rev initial HEAD &&
+ test_path_is_file dirtyfile &&
+ test_cmp dirtyfile.expected dirtyfile
+'
+
+test_expect_success 'am -3 stops on conflict on unborn branch' '
+ git checkout -f --orphan orphan &&
+ git reset &&
+ rm -f otherfile-4 &&
+ test_must_fail git am -3 0003-*.patch &&
+ test 2 -eq $(git ls-files -u | wc -l) &&
+ test 4 = "$(cat otherfile-4)"
+'
+
+test_expect_success 'am -3 --skip clears index on unborn branch' '
+ test_path_is_dir .git/rebase-apply &&
+ echo tmpfile >tmpfile &&
+ git add tmpfile &&
+ git am --skip &&
+ test -z "$(git ls-files)" &&
+ test_path_is_missing otherfile-4 &&
+ test_path_is_missing tmpfile
+'
+
+test_expect_success 'am -3 --abort removes otherfile-4 on unborn branch' '
+ git checkout -f --orphan orphan &&
+ git reset &&
+ rm -f otherfile-4 file-1 &&
+ test_must_fail git am -3 0003-*.patch &&
+ test 2 -eq $(git ls-files -u | wc -l) &&
+ test 4 = "$(cat otherfile-4)" &&
+ git am --abort &&
+ test -z "$(git ls-files -u)" &&
+ test_path_is_missing otherfile-4
+'
+
+test_expect_success 'am -3 --abort on unborn branch removes applied commits' '
+ git checkout -f --orphan orphan &&
+ git reset &&
+ rm -f otherfile-4 otherfile-2 file-1 file-2 &&
+ test_must_fail git am -3 initial.patch 0003-*.patch &&
+ test 3 -eq $(git ls-files -u | wc -l) &&
+ test 4 = "$(cat otherfile-4)" &&
+ git am --abort &&
+ test -z "$(git ls-files -u)" &&
+ test_path_is_missing otherfile-4 &&
+ test_path_is_missing file-1 &&
+ test_path_is_missing file-2 &&
+ test 0 -eq $(git log --oneline 2>/dev/null | wc -l) &&
+ test refs/heads/orphan = "$(git symbolic-ref HEAD)"
+'
+
+test_expect_success 'am --abort on unborn branch will keep local commits intact' '
+ git checkout -f --orphan orphan &&
+ git reset &&
+ test_must_fail git am 0004-*.patch &&
+ test_commit unrelated2 &&
+ git rev-parse HEAD >expect &&
+ git am --abort &&
+ git rev-parse HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'am --skip leaves index stat info alone' '
+ git checkout -f --orphan skip-stat-info &&
+ git reset &&
+ test_commit skip-should-be-untouched &&
+ test-tool chmtime =0 skip-should-be-untouched.t &&
+ git update-index --refresh &&
+ git diff-files --exit-code --quiet &&
+ test_must_fail git am 0001-*.patch &&
+ git am --skip &&
+ git diff-files --exit-code --quiet
+'
+
+test_expect_success 'am --abort leaves index stat info alone' '
+ git checkout -f --orphan abort-stat-info &&
+ git reset &&
+ test_commit abort-should-be-untouched &&
+ test-tool chmtime =0 abort-should-be-untouched.t &&
+ git update-index --refresh &&
+ git diff-files --exit-code --quiet &&
+ test_must_fail git am 0001-*.patch &&
+ git am --abort &&
+ git diff-files --exit-code --quiet
+'
+
+test_expect_success 'git am --abort return failed exit status when it fails' '
+ test_when_finished "rm -rf file-2/ && git reset --hard && git am --abort" &&
+ git checkout changes &&
+ git format-patch -1 --stdout conflicting >changes.mbox &&
+ test_must_fail git am --3way changes.mbox &&
+
+ git rm file-2 &&
+ mkdir file-2 &&
+ echo precious >file-2/somefile &&
+ test_must_fail git am --abort &&
+ test_path_is_dir file-2/
+'
+
+test_expect_success 'git am --abort cleans relevant files' '
+ git checkout changes &&
+ git format-patch -1 --stdout conflicting >changes.mbox &&
+ test_must_fail git am --3way changes.mbox &&
+
+ test_path_is_file new-file &&
+ echo further changes >>file-1 &&
+ echo change other file >>file-2 &&
+
+ # Abort, and expect the files touched by am to be reverted
+ git am --abort &&
+
+ test_path_is_missing new-file &&
+
+ # Files not involved in am operation are left modified
+ git diff --name-only changes >actual &&
+ test_write_lines file-2 >expect &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t4152-am-subjects.sh b/t/t4152-am-subjects.sh
new file mode 100755
index 0000000..9f2edba
--- /dev/null
+++ b/t/t4152-am-subjects.sh
@@ -0,0 +1,79 @@
+#!/bin/sh
+
+test_description='test subject preservation with format-patch | am'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+make_patches() {
+ type=$1
+ subject=$2
+ test_expect_success "create patches with $type subject" '
+ git reset --hard baseline &&
+ echo $type >file &&
+ git commit -a -m "$subject" &&
+ git format-patch -1 --stdout >$type.patch &&
+ git format-patch -1 --stdout -k >$type-k.patch
+ '
+}
+
+check_subject() {
+ git reset --hard baseline &&
+ git am $2 $1.patch &&
+ git log -1 --pretty=format:%B >actual &&
+ test_cmp expect actual
+}
+
+test_expect_success 'setup baseline commit' '
+ test_commit baseline file
+'
+
+SHORT_SUBJECT='short subject'
+make_patches short "$SHORT_SUBJECT"
+
+LONG_SUBJECT1='this is a long subject that is virtually guaranteed'
+LONG_SUBJECT2='to require wrapping via format-patch if it is all'
+LONG_SUBJECT3='going to appear on a single line'
+LONG_SUBJECT="$LONG_SUBJECT1 $LONG_SUBJECT2 $LONG_SUBJECT3"
+make_patches long "$LONG_SUBJECT"
+
+MULTILINE_SUBJECT="$LONG_SUBJECT1
+$LONG_SUBJECT2
+$LONG_SUBJECT3"
+make_patches multiline "$MULTILINE_SUBJECT"
+
+echo "$SHORT_SUBJECT" >expect
+test_expect_success 'short subject preserved (format-patch | am)' '
+ check_subject short
+'
+test_expect_success 'short subject preserved (format-patch -k | am)' '
+ check_subject short-k
+'
+test_expect_success 'short subject preserved (format-patch -k | am -k)' '
+ check_subject short-k -k
+'
+
+echo "$LONG_SUBJECT" >expect
+test_expect_success 'long subject preserved (format-patch | am)' '
+ check_subject long
+'
+test_expect_success 'long subject preserved (format-patch -k | am)' '
+ check_subject long-k
+'
+test_expect_success 'long subject preserved (format-patch -k | am -k)' '
+ check_subject long-k -k
+'
+
+echo "$LONG_SUBJECT" >expect
+test_expect_success 'multiline subject unwrapped (format-patch | am)' '
+ check_subject multiline
+'
+test_expect_success 'multiline subject unwrapped (format-patch -k | am)' '
+ check_subject multiline-k
+'
+echo "$MULTILINE_SUBJECT" >expect
+test_expect_success 'multiline subject preserved (format-patch -k | am -k)' '
+ check_subject multiline-k -k
+'
+
+test_done
diff --git a/t/t4153-am-resume-override-opts.sh b/t/t4153-am-resume-override-opts.sh
new file mode 100755
index 0000000..4add7c7
--- /dev/null
+++ b/t/t4153-am-resume-override-opts.sh
@@ -0,0 +1,102 @@
+#!/bin/sh
+
+test_description='git-am command-line options override saved options'
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-terminal.sh
+
+format_patch () {
+ git format-patch --stdout -1 "$1" >"$1".eml
+}
+
+test_expect_success 'setup' '
+ test_commit initial file &&
+ test_commit first file &&
+
+ git checkout initial &&
+ git mv file file2 &&
+ test_tick &&
+ git commit -m renamed-file &&
+ git tag renamed-file &&
+
+ git checkout -b side initial &&
+ test_commit side1 file &&
+ test_commit side2 file &&
+
+ format_patch side1 &&
+ format_patch side2
+'
+
+test_expect_success TTY '--3way overrides --no-3way' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout renamed-file &&
+
+ # Applying side1 will fail as the file has been renamed.
+ test_must_fail git am --no-3way side[12].eml &&
+ test_path_is_dir .git/rebase-apply &&
+ test_cmp_rev renamed-file HEAD &&
+ test -z "$(git ls-files -u)" &&
+
+ # Applying side1 with am --3way will succeed due to the threeway-merge.
+ # Applying side2 will fail as --3way does not apply to it.
+ test_must_fail test_terminal git am --3way </dev/zero &&
+ test_path_is_dir .git/rebase-apply &&
+ test side1 = "$(cat file2)"
+'
+
+test_expect_success '--no-quiet overrides --quiet' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout first &&
+
+ # Applying side1 will be quiet.
+ test_must_fail git am --quiet side[123].eml >out &&
+ test_path_is_dir .git/rebase-apply &&
+ test_grep ! "^Applying: " out &&
+ echo side1 >file &&
+ git add file &&
+
+ # Applying side1 will not be quiet.
+ # Applying side2 will be quiet.
+ git am --no-quiet --continue >out &&
+ echo "Applying: side1" >expected &&
+ test_cmp expected out
+'
+
+test_expect_success '--signoff overrides --no-signoff' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout first &&
+
+ test_must_fail git am --no-signoff side[12].eml &&
+ test_path_is_dir .git/rebase-apply &&
+ echo side1 >file &&
+ git add file &&
+ git am --signoff --continue &&
+
+ # Applied side1 will be signed off
+ echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>" >expected &&
+ git cat-file commit HEAD^ | grep "Signed-off-by:" >actual &&
+ test_cmp expected actual &&
+
+ # Applied side2 will not be signed off
+ test $(git cat-file commit HEAD | grep -c "Signed-off-by:") -eq 0
+'
+
+test_expect_success TTY '--reject overrides --no-reject' '
+ rm -fr .git/rebase-apply &&
+ git reset --hard &&
+ git checkout first &&
+ rm -f file.rej &&
+
+ test_must_fail git am --no-reject side1.eml &&
+ test_path_is_dir .git/rebase-apply &&
+ test_path_is_missing file.rej &&
+
+ test_must_fail test_terminal git am --reject </dev/zero &&
+ test_path_is_dir .git/rebase-apply &&
+ test_path_is_file file.rej
+'
+
+test_done
diff --git a/t/t4200-rerere.sh b/t/t4200-rerere.sh
new file mode 100755
index 0000000..fb53ddd
--- /dev/null
+++ b/t/t4200-rerere.sh
@@ -0,0 +1,674 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Johannes E. Schindelin
+#
+
+test_description='git rerere
+
+! [fifth] version1
+ ! [first] first
+ ! [fourth] version1
+ ! [main] initial
+ ! [second] prefer first over second
+ ! [third] version2
+------
+ + [third] version2
++ [fifth] version1
+ + [fourth] version1
++ + + [third^] third
+ - [second] prefer first over second
+ + + [first] first
+ + [second^] second
+++++++ [main] initial
+'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ cat >a1 <<-\EOF &&
+ Some title
+ ==========
+ Whether '\''tis nobler in the mind to suffer
+ The slings and arrows of outrageous fortune,
+ Or to take arms against a sea of troubles,
+ And by opposing end them? To die: to sleep;
+ No more; and by a sleep to say we end
+ The heart-ache and the thousand natural shocks
+ That flesh is heir to, '\''tis a consummation
+ Devoutly to be wish'\''d.
+ EOF
+
+ git add a1 &&
+ test_tick &&
+ git commit -q -a -m initial &&
+
+ cat >>a1 <<-\EOF &&
+ Some title
+ ==========
+ To die, to sleep;
+ To sleep: perchance to dream: ay, there'\''s the rub;
+ For in that sleep of death what dreams may come
+ When we have shuffled off this mortal coil,
+ Must give us pause: there'\''s the respect
+ That makes calamity of so long life;
+ EOF
+
+ git checkout -b first &&
+ test_tick &&
+ git commit -q -a -m first &&
+
+ git checkout -b second main &&
+ git show first:a1 |
+ sed -e "s/To die, t/To die! T/" -e "s/Some title/Some Title/" >a1 &&
+ echo "* END *" >>a1 &&
+ test_tick &&
+ git commit -q -a -m second
+'
+
+test_expect_success 'nothing recorded without rerere' '
+ rm -rf .git/rr-cache &&
+ git config rerere.enabled false &&
+ test_must_fail git merge first &&
+ ! test -d .git/rr-cache
+'
+
+test_expect_success 'activate rerere, old style (conflicting merge)' '
+ git reset --hard &&
+ mkdir .git/rr-cache &&
+ test_might_fail git config --unset rerere.enabled &&
+ test_must_fail git merge first &&
+
+ sha1=$(perl -pe "s/ .*//" .git/MERGE_RR) &&
+ rr=.git/rr-cache/$sha1 &&
+ grep "^=======\$" $rr/preimage &&
+ ! test -f $rr/postimage &&
+ ! test -f $rr/thisimage
+'
+
+test_expect_success 'rerere.enabled works, too' '
+ rm -rf .git/rr-cache &&
+ git config rerere.enabled true &&
+ git reset --hard &&
+ test_must_fail git merge first &&
+
+ sha1=$(perl -pe "s/ .*//" .git/MERGE_RR) &&
+ rr=.git/rr-cache/$sha1 &&
+ grep ^=======$ $rr/preimage
+'
+
+test_expect_success 'set up rr-cache' '
+ rm -rf .git/rr-cache &&
+ git config rerere.enabled true &&
+ git reset --hard &&
+ test_must_fail git merge first &&
+ sha1=$(perl -pe "s/ .*//" .git/MERGE_RR) &&
+ rr=.git/rr-cache/$sha1
+'
+
+test_expect_success 'rr-cache looks sane' '
+ # no postimage or thisimage yet
+ ! test -f $rr/postimage &&
+ ! test -f $rr/thisimage &&
+
+ # preimage has right number of lines
+ cnt=$(sed -ne "/^<<<<<<</,/^>>>>>>>/p" $rr/preimage | wc -l) &&
+ echo $cnt &&
+ test $cnt = 13
+'
+
+test_expect_success 'rerere diff' '
+ git show first:a1 >a1 &&
+ cat >expect <<-\EOF &&
+ --- a/a1
+ +++ b/a1
+ @@ -1,4 +1,4 @@
+ -Some Title
+ +Some title
+ ==========
+ Whether '\''tis nobler in the mind to suffer
+ The slings and arrows of outrageous fortune,
+ @@ -8,21 +8,11 @@
+ The heart-ache and the thousand natural shocks
+ That flesh is heir to, '\''tis a consummation
+ Devoutly to be wish'\''d.
+ -<<<<<<<
+ -Some Title
+ -==========
+ -To die! To sleep;
+ -=======
+ Some title
+ ==========
+ To die, to sleep;
+ ->>>>>>>
+ To sleep: perchance to dream: ay, there'\''s the rub;
+ For in that sleep of death what dreams may come
+ When we have shuffled off this mortal coil,
+ Must give us pause: there'\''s the respect
+ That makes calamity of so long life;
+ -<<<<<<<
+ -=======
+ -* END *
+ ->>>>>>>
+ EOF
+ git rerere diff >out &&
+ test_cmp expect out
+'
+
+test_expect_success 'rerere status' '
+ echo a1 >expect &&
+ git rerere status >out &&
+ test_cmp expect out
+'
+
+test_expect_success 'first postimage wins' '
+ git show first:a1 | sed "s/To die: t/To die! T/" >expect &&
+
+ git commit -q -a -m "prefer first over second" &&
+ test -f $rr/postimage &&
+
+ oldmtimepost=$(test-tool chmtime --get -60 $rr/postimage) &&
+
+ git checkout -b third main &&
+ git show second^:a1 | sed "s/To die: t/To die! T/" >a1 &&
+ git commit -q -a -m third &&
+
+ test_must_fail git merge first &&
+ # rerere kicked in
+ ! grep "^=======\$" a1 &&
+ test_cmp expect a1
+'
+
+test_expect_success 'rerere updates postimage timestamp' '
+ newmtimepost=$(test-tool chmtime --get $rr/postimage) &&
+ test $oldmtimepost -lt $newmtimepost
+'
+
+test_expect_success 'rerere clear' '
+ mv $rr/postimage .git/post-saved &&
+ echo "$sha1 a1" | perl -pe "y/\012/\000/" >.git/MERGE_RR &&
+ git rerere clear &&
+ ! test -d $rr
+'
+
+test_expect_success 'leftover directory' '
+ git reset --hard &&
+ mkdir -p $rr &&
+ test_must_fail git merge first &&
+ test -f $rr/preimage
+'
+
+test_expect_success 'missing preimage' '
+ git reset --hard &&
+ mkdir -p $rr &&
+ cp .git/post-saved $rr/postimage &&
+ test_must_fail git merge first &&
+ test -f $rr/preimage
+'
+
+test_expect_success 'set up for garbage collection tests' '
+ mkdir -p $rr &&
+ echo Hello >$rr/preimage &&
+ echo World >$rr/postimage &&
+
+ sha2=$(test_oid deadbeef) &&
+ rr2=.git/rr-cache/$sha2 &&
+ mkdir $rr2 &&
+ echo Hello >$rr2/preimage &&
+
+ almost_15_days_ago=$((60-15*86400)) &&
+ just_over_15_days_ago=$((-1-15*86400)) &&
+ almost_60_days_ago=$((60-60*86400)) &&
+ just_over_60_days_ago=$((-1-60*86400)) &&
+
+ test-tool chmtime =$just_over_60_days_ago $rr/preimage &&
+ test-tool chmtime =$almost_60_days_ago $rr/postimage &&
+ test-tool chmtime =$almost_15_days_ago $rr2/preimage
+'
+
+test_expect_success 'gc preserves young or recently used records' '
+ git rerere gc &&
+ test -f $rr/preimage &&
+ test -f $rr2/preimage
+'
+
+test_expect_success 'old records rest in peace' '
+ test-tool chmtime =$just_over_60_days_ago $rr/postimage &&
+ test-tool chmtime =$just_over_15_days_ago $rr2/preimage &&
+ git rerere gc &&
+ ! test -f $rr/preimage &&
+ ! test -f $rr2/preimage
+'
+
+rerere_gc_custom_expiry_test () {
+ five_days="$1" right_now="$2"
+ test_expect_success "rerere gc with custom expiry ($five_days, $right_now)" '
+ rm -fr .git/rr-cache &&
+ rr=.git/rr-cache/$ZERO_OID &&
+ mkdir -p "$rr" &&
+ >"$rr/preimage" &&
+ >"$rr/postimage" &&
+
+ two_days_ago=$((-2*86400)) &&
+ test-tool chmtime =$two_days_ago "$rr/preimage" &&
+ test-tool chmtime =$two_days_ago "$rr/postimage" &&
+
+ find .git/rr-cache -type f | sort >original &&
+
+ git -c "gc.rerereresolved=$five_days" \
+ -c "gc.rerereunresolved=$five_days" rerere gc &&
+ find .git/rr-cache -type f | sort >actual &&
+ test_cmp original actual &&
+
+ git -c "gc.rerereresolved=$five_days" \
+ -c "gc.rerereunresolved=$right_now" rerere gc &&
+ find .git/rr-cache -type f | sort >actual &&
+ test_cmp original actual &&
+
+ git -c "gc.rerereresolved=$right_now" \
+ -c "gc.rerereunresolved=$right_now" rerere gc &&
+ find .git/rr-cache -type f | sort >actual &&
+ test_must_be_empty actual
+ '
+}
+
+rerere_gc_custom_expiry_test 5 0
+
+rerere_gc_custom_expiry_test 5.days.ago now
+
+test_expect_success 'setup: file2 added differently in two branches' '
+ git reset --hard &&
+
+ git checkout -b fourth &&
+ echo Hallo >file2 &&
+ git add file2 &&
+ test_tick &&
+ git commit -m version1 &&
+
+ git checkout third &&
+ echo Bello >file2 &&
+ git add file2 &&
+ test_tick &&
+ git commit -m version2 &&
+
+ test_must_fail git merge fourth &&
+ echo Cello >file2 &&
+ git add file2 &&
+ git commit -m resolution
+'
+
+test_expect_success 'resolution was recorded properly' '
+ echo Cello >expected &&
+
+ git reset --hard HEAD~2 &&
+ git checkout -b fifth &&
+
+ echo Hallo >file3 &&
+ git add file3 &&
+ test_tick &&
+ git commit -m version1 &&
+
+ git checkout third &&
+ echo Bello >file3 &&
+ git add file3 &&
+ test_tick &&
+ git commit -m version2 &&
+ git tag version2 &&
+
+ test_must_fail git merge fifth &&
+ test_cmp expected file3 &&
+ test_must_fail git update-index --refresh
+'
+
+test_expect_success 'rerere.autoupdate' '
+ git config rerere.autoupdate true &&
+ git reset --hard &&
+ git checkout version2 &&
+ test_must_fail git merge fifth &&
+ git update-index --refresh
+'
+
+test_expect_success 'merge --rerere-autoupdate' '
+ test_might_fail git config --unset rerere.autoupdate &&
+ git reset --hard &&
+ git checkout version2 &&
+ test_must_fail git merge --rerere-autoupdate fifth &&
+ git update-index --refresh
+'
+
+test_expect_success 'merge --no-rerere-autoupdate' '
+ headblob=$(git rev-parse version2:file3) &&
+ mergeblob=$(git rev-parse fifth:file3) &&
+ cat >expected <<-EOF &&
+ 100644 $headblob 2 file3
+ 100644 $mergeblob 3 file3
+ EOF
+
+ git config rerere.autoupdate true &&
+ git reset --hard &&
+ git checkout version2 &&
+ test_must_fail git merge --no-rerere-autoupdate fifth &&
+ git ls-files -u >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'set up an unresolved merge' '
+ headblob=$(git rev-parse version2:file3) &&
+ mergeblob=$(git rev-parse fifth:file3) &&
+ cat >expected.unresolved <<-EOF &&
+ 100644 $headblob 2 file3
+ 100644 $mergeblob 3 file3
+ EOF
+
+ test_might_fail git config --unset rerere.autoupdate &&
+ git reset --hard &&
+ git checkout version2 &&
+ ancestor=$(git merge-base version2 fifth) &&
+ test_must_fail git merge-recursive "$ancestor" -- HEAD fifth &&
+
+ git ls-files --stage >failedmerge &&
+ cp file3 file3.conflict &&
+
+ git ls-files -u >actual &&
+ test_cmp expected.unresolved actual
+'
+
+test_expect_success 'explicit rerere' '
+ test_might_fail git config --unset rerere.autoupdate &&
+ git rm -fr --cached . &&
+ git update-index --index-info <failedmerge &&
+ cp file3.conflict file3 &&
+ test_must_fail git update-index --refresh -q &&
+
+ git rerere &&
+ git ls-files -u >actual &&
+ test_cmp expected.unresolved actual
+'
+
+test_expect_success 'explicit rerere with autoupdate' '
+ git config rerere.autoupdate true &&
+ git rm -fr --cached . &&
+ git update-index --index-info <failedmerge &&
+ cp file3.conflict file3 &&
+ test_must_fail git update-index --refresh -q &&
+
+ git rerere &&
+ git update-index --refresh
+'
+
+test_expect_success 'explicit rerere --rerere-autoupdate overrides' '
+ git config rerere.autoupdate false &&
+ git rm -fr --cached . &&
+ git update-index --index-info <failedmerge &&
+ cp file3.conflict file3 &&
+ git rerere &&
+ git ls-files -u >actual1 &&
+
+ git rm -fr --cached . &&
+ git update-index --index-info <failedmerge &&
+ cp file3.conflict file3 &&
+ git rerere --rerere-autoupdate &&
+ git update-index --refresh &&
+
+ git rm -fr --cached . &&
+ git update-index --index-info <failedmerge &&
+ cp file3.conflict file3 &&
+ git rerere --rerere-autoupdate --no-rerere-autoupdate &&
+ git ls-files -u >actual2 &&
+
+ git rm -fr --cached . &&
+ git update-index --index-info <failedmerge &&
+ cp file3.conflict file3 &&
+ git rerere --rerere-autoupdate --no-rerere-autoupdate --rerere-autoupdate &&
+ git update-index --refresh &&
+
+ test_cmp expected.unresolved actual1 &&
+ test_cmp expected.unresolved actual2
+'
+
+test_expect_success 'rerere --no-no-rerere-autoupdate' '
+ git rm -fr --cached . &&
+ git update-index --index-info <failedmerge &&
+ cp file3.conflict file3 &&
+ test_must_fail git rerere --no-no-rerere-autoupdate 2>err &&
+ test_grep [Uu]sage err &&
+ test_must_fail git update-index --refresh
+'
+
+test_expect_success 'rerere -h' '
+ test_must_fail git rerere -h >help &&
+ test_grep [Uu]sage help
+'
+
+concat_insert () {
+ last=$1
+ shift
+ cat early && printf "%s\n" "$@" && cat late "$last"
+}
+
+count_pre_post () {
+ find .git/rr-cache/ -type f -name "preimage*" >actual &&
+ test_line_count = "$1" actual &&
+ find .git/rr-cache/ -type f -name "postimage*" >actual &&
+ test_line_count = "$2" actual
+}
+
+merge_conflict_resolve () {
+ git reset --hard &&
+ test_must_fail git merge six.1 &&
+ # Resolution is to replace 7 with 6.1 and 6.2 (i.e. take both)
+ concat_insert short 6.1 6.2 >file1 &&
+ concat_insert long 6.1 6.2 >file2
+}
+
+test_expect_success 'multiple identical conflicts' '
+ rm -fr .git/rr-cache &&
+ mkdir .git/rr-cache &&
+ git reset --hard &&
+
+ test_seq 1 6 >early &&
+ >late &&
+ test_seq 11 15 >short &&
+ test_seq 111 120 >long &&
+ concat_insert short >file1 &&
+ concat_insert long >file2 &&
+ git add file1 file2 &&
+ git commit -m base &&
+ git tag base &&
+ git checkout -b six.1 &&
+ concat_insert short 6.1 >file1 &&
+ concat_insert long 6.1 >file2 &&
+ git add file1 file2 &&
+ git commit -m 6.1 &&
+ git checkout -b six.2 HEAD^ &&
+ concat_insert short 6.2 >file1 &&
+ concat_insert long 6.2 >file2 &&
+ git add file1 file2 &&
+ git commit -m 6.2 &&
+
+ # At this point, six.1 and six.2
+ # - derive from common ancestor that has two files
+ # 1...6 7 11..15 (file1) and 1...6 7 111..120 (file2)
+ # - six.1 replaces these 7s with 6.1
+ # - six.2 replaces these 7s with 6.2
+
+ merge_conflict_resolve &&
+
+ # Check that rerere knows that file1 and file2 have conflicts
+
+ printf "%s\n" file1 file2 >expect &&
+ git ls-files -u | sed -e "s/^.* //" | sort -u >actual &&
+ test_cmp expect actual &&
+
+ git rerere status | sort >actual &&
+ test_cmp expect actual &&
+
+ git rerere remaining >actual &&
+ test_cmp expect actual &&
+
+ count_pre_post 2 0 &&
+
+ # Pretend that the conflicts were made quite some time ago
+ test-tool chmtime -172800 $(find .git/rr-cache/ -type f) &&
+
+ # Unresolved entries have not expired yet
+ git -c gc.rerereresolved=5 -c gc.rerereunresolved=5 rerere gc &&
+ count_pre_post 2 0 &&
+
+ # Unresolved entries have expired
+ git -c gc.rerereresolved=5 -c gc.rerereunresolved=1 rerere gc &&
+ count_pre_post 0 0 &&
+
+ # Recreate the conflicted state
+ merge_conflict_resolve &&
+ count_pre_post 2 0 &&
+
+ # Clear it
+ git rerere clear &&
+ count_pre_post 0 0 &&
+
+ # Recreate the conflicted state
+ merge_conflict_resolve &&
+ count_pre_post 2 0 &&
+
+ # We resolved file1 and file2
+ git rerere &&
+ git rerere remaining >actual &&
+ test_must_be_empty actual &&
+
+ # We must have recorded both of them
+ count_pre_post 2 2 &&
+
+ # Now we should be able to resolve them both
+ git reset --hard &&
+ test_must_fail git merge six.1 &&
+ git rerere &&
+
+ git rerere remaining >actual &&
+ test_must_be_empty actual &&
+
+ concat_insert short 6.1 6.2 >file1.expect &&
+ concat_insert long 6.1 6.2 >file2.expect &&
+ test_cmp file1.expect file1 &&
+ test_cmp file2.expect file2 &&
+
+ # Forget resolution for file2
+ git rerere forget file2 &&
+ echo file2 >expect &&
+ git rerere status >actual &&
+ test_cmp expect actual &&
+ count_pre_post 2 1 &&
+
+ # file2 already has correct resolution, so record it again
+ git rerere &&
+
+ # Pretend that the resolutions are old again
+ test-tool chmtime -172800 $(find .git/rr-cache/ -type f) &&
+
+ # Resolved entries have not expired yet
+ git -c gc.rerereresolved=5 -c gc.rerereunresolved=5 rerere gc &&
+
+ count_pre_post 2 2 &&
+
+ # Resolved entries have expired
+ git -c gc.rerereresolved=1 -c gc.rerereunresolved=5 rerere gc &&
+ count_pre_post 0 0
+'
+
+test_expect_success 'rerere with unexpected conflict markers does not crash' '
+ git reset --hard &&
+
+ git checkout -b branch-1 main &&
+ echo "bar" >test &&
+ git add test &&
+ git commit -q -m two &&
+
+ git reset --hard &&
+ git checkout -b branch-2 main &&
+ echo "foo" >test &&
+ git add test &&
+ git commit -q -a -m one &&
+
+ test_must_fail git merge branch-1 &&
+ echo "<<<<<<< a" >test &&
+ git rerere &&
+
+ git rerere clear
+'
+
+test_expect_success 'rerere with inner conflict markers' '
+ git reset --hard &&
+
+ git checkout -b A main &&
+ echo "bar" >test &&
+ git add test &&
+ git commit -q -m two &&
+ echo "baz" >test &&
+ git add test &&
+ git commit -q -m three &&
+
+ git reset --hard &&
+ git checkout -b B main &&
+ echo "foo" >test &&
+ git add test &&
+ git commit -q -a -m one &&
+
+ test_must_fail git merge A~ &&
+ git add test &&
+ git commit -q -m "will solve conflicts later" &&
+ test_must_fail git merge A &&
+
+ echo "resolved" >test &&
+ git add test &&
+ git commit -q -m "solved conflict" &&
+
+ echo "resolved" >expect &&
+
+ git reset --hard HEAD~~ &&
+ test_must_fail git merge A~ &&
+ git add test &&
+ git commit -q -m "will solve conflicts later" &&
+ test_must_fail git merge A &&
+ cat test >actual &&
+ test_cmp expect actual &&
+
+ git add test &&
+ git commit -m "rerere solved conflict" &&
+ git reset --hard HEAD~ &&
+ test_must_fail git merge A &&
+ cat test >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'setup simple stage 1 handling' '
+ test_create_repo stage_1_handling &&
+ (
+ cd stage_1_handling &&
+
+ test_seq 1 10 >original &&
+ git add original &&
+ git commit -m original &&
+
+ git checkout -b A main &&
+ git mv original A &&
+ git commit -m "rename to A" &&
+
+ git checkout -b B main &&
+ git mv original B &&
+ git commit -m "rename to B"
+ )
+'
+
+test_expect_success 'test simple stage 1 handling' '
+ (
+ cd stage_1_handling &&
+
+ git config rerere.enabled true &&
+ git checkout A^0 &&
+ test_must_fail git merge B^0
+ )
+'
+
+test_done
diff --git a/t/t4201-shortlog.sh b/t/t4201-shortlog.sh
new file mode 100755
index 0000000..d738270
--- /dev/null
+++ b/t/t4201-shortlog.sh
@@ -0,0 +1,401 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Johannes E. Schindelin
+#
+
+test_description='git shortlog
+'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ test_tick &&
+ echo 1 >a1 &&
+ git add a1 &&
+ tree=$(git write-tree) &&
+ commit=$(printf "%s\n" "Test" "" | git commit-tree "$tree") &&
+ git update-ref HEAD "$commit" &&
+
+ echo 2 >a1 &&
+ git commit --quiet -m "This is a very, very long first line for the commit message to see if it is wrapped correctly" a1 &&
+
+ # test if the wrapping is still valid
+ # when replacing all is by treble clefs.
+ echo 3 >a1 &&
+ git commit --quiet -m "$(
+ echo "This is a very, very long first line for the commit message to see if it is wrapped correctly" |
+ sed "s/i/1234/g" |
+ tr 1234 "\360\235\204\236")" a1 &&
+
+ # now fsck up the utf8
+ git config i18n.commitencoding non-utf-8 &&
+ echo 4 >a1 &&
+ git commit --quiet -m "$(
+ echo "This is a very, very long first line for the commit message to see if it is wrapped correctly" |
+ sed "s/i/1234/g" |
+ tr 1234 "\370\235\204\236")" a1 &&
+
+ echo 5 >a1 &&
+ git commit --quiet -m "a 12 34 56 78" a1 &&
+
+ echo 6 >a1 &&
+ git commit --quiet -m "Commit by someone else" \
+ --author="Someone else <not!me>" a1 &&
+
+ cat >expect.template <<-\EOF
+ A U Thor (5):
+ SUBJECT
+ SUBJECT
+ SUBJECT
+ SUBJECT
+ SUBJECT
+
+ Someone else (1):
+ SUBJECT
+
+ EOF
+'
+
+fuzz() {
+ file=$1 &&
+ sed "
+ s/$OID_REGEX/OBJECT_NAME/g
+ s/$_x35/OBJID/g
+ s/^ \{6\}[CTa].*/ SUBJECT/g
+ s/^ \{8\}[^ ].*/ CONTINUATION/g
+ " <"$file" >"$file.fuzzy" &&
+ sed "/CONTINUATION/ d" <"$file.fuzzy"
+}
+
+test_expect_success 'default output format' '
+ git shortlog HEAD >log &&
+ fuzz log >log.predictable &&
+ test_cmp expect.template log.predictable
+'
+
+test_expect_success 'pretty format' '
+ sed s/SUBJECT/OBJECT_NAME/ expect.template >expect &&
+ git shortlog --format="%H" HEAD >log &&
+ fuzz log >log.predictable &&
+ test_cmp expect log.predictable
+'
+
+test_expect_success 'pretty format (with --date)' '
+ sed "s/SUBJECT/2005-04-07 OBJECT_NAME/" expect.template >expect &&
+ git shortlog --format="%ad %H" --date=short HEAD >log &&
+ fuzz log >log.predictable &&
+ test_cmp expect log.predictable
+'
+
+test_expect_success '--abbrev' '
+ sed s/SUBJECT/OBJID/ expect.template >expect &&
+ git shortlog --format="%h" --abbrev=35 HEAD >log &&
+ fuzz log >log.predictable &&
+ test_cmp expect log.predictable
+'
+
+test_expect_success 'output from user-defined format is re-wrapped' '
+ sed "s/SUBJECT/two lines/" expect.template >expect &&
+ git shortlog --format="two%nlines" HEAD >log &&
+ fuzz log >log.predictable &&
+ test_cmp expect log.predictable
+'
+
+test_expect_success !MINGW 'shortlog wrapping' '
+ cat >expect <<\EOF &&
+A U Thor (5):
+ Test
+ This is a very, very long first line for the commit message to see if
+ it is wrapped correctly
+ Thð„žs ð„žs a very, very long fð„žrst lð„žne for the commð„žt message to see ð„žf
+ ð„žt ð„žs wrapped correctly
+ Thø„žs ø„žs a very, very long fø„žrst lø„žne for the commø„žt
+ message to see ø„žf ø„žt ø„žs wrapped correctly
+ a 12 34
+ 56 78
+
+Someone else (1):
+ Commit by someone else
+
+EOF
+ git shortlog -w HEAD >out &&
+ test_cmp expect out
+'
+
+test_expect_success !MINGW 'shortlog from non-git directory' '
+ git log --no-expand-tabs HEAD >log &&
+ GIT_DIR=non-existing git shortlog -w <log >out &&
+ test_cmp expect out
+'
+
+test_expect_success !MINGW 'shortlog can read --format=raw output' '
+ git log --format=raw HEAD >log &&
+ GIT_DIR=non-existing git shortlog -w <log >out &&
+ test_cmp expect out
+'
+
+test_expect_success 'shortlog from non-git directory refuses extra arguments' '
+ test_must_fail env GIT_DIR=non-existing git shortlog foo 2>out &&
+ test_grep "too many arguments" out
+'
+
+test_expect_success 'shortlog should add newline when input line matches wraplen' '
+ cat >expect <<\EOF &&
+A U Thor (2):
+ bbbbbbbbbbbbbbbbbb: bbbbbbbb bbb bbbb bbbbbbb bb bbbb bbb bbbbb bbbbbb
+ aaaaaaaaaaaaaaaaaaaaaa: aaaaaa aaaaaaaaaa aaaa aaaaaaaa aa aaaa aa aaa
+
+EOF
+ git shortlog -w >out <<\EOF &&
+commit 0000000000000000000000000000000000000001
+Author: A U Thor <author@example.com>
+Date: Thu Apr 7 15:14:13 2005 -0700
+
+ aaaaaaaaaaaaaaaaaaaaaa: aaaaaa aaaaaaaaaa aaaa aaaaaaaa aa aaaa aa aaa
+
+commit 0000000000000000000000000000000000000002
+Author: A U Thor <author@example.com>
+Date: Thu Apr 7 15:14:13 2005 -0700
+
+ bbbbbbbbbbbbbbbbbb: bbbbbbbb bbb bbbb bbbbbbb bb bbbb bbb bbbbb bbbbbb
+
+EOF
+ test_cmp expect out
+'
+
+iconvfromutf8toiso88591() {
+ printf "%s" "$*" | iconv -f UTF-8 -t ISO8859-1
+}
+
+DSCHO="Jöhännës \"Dschö\" Schindëlin"
+DSCHOE="$DSCHO <Johannes.Schindelin@gmx.de>"
+MSG1="set a1 to 2 and some non-ASCII chars: Äßø"
+MSG2="set a1 to 3 and some non-ASCII chars: áæï"
+cat > expect << EOF
+$DSCHO (2):
+ $MSG1
+ $MSG2
+
+EOF
+
+test_expect_success !MINGW 'shortlog encoding' '
+ git reset --hard "$commit" &&
+ git config --unset i18n.commitencoding &&
+ echo 2 > a1 &&
+ git commit --quiet -m "$MSG1" --author="$DSCHOE" a1 &&
+ git config i18n.commitencoding "ISO8859-1" &&
+ echo 3 > a1 &&
+ git commit --quiet -m "$(iconvfromutf8toiso88591 "$MSG2")" \
+ --author="$(iconvfromutf8toiso88591 "$DSCHOE")" a1 &&
+ git config --unset i18n.commitencoding &&
+ git shortlog HEAD~2.. > out &&
+test_cmp expect out'
+
+test_expect_success 'shortlog with revision pseudo options' '
+ git shortlog --all &&
+ git shortlog --branches &&
+ git shortlog --exclude=refs/heads/m* --all
+'
+
+test_expect_success 'shortlog with --output=<file>' '
+ git shortlog --output=shortlog -1 main >output &&
+ test_must_be_empty output &&
+ test_line_count = 3 shortlog
+'
+
+test_expect_success 'shortlog --committer (internal)' '
+ git checkout --orphan side &&
+ git commit --allow-empty -m one &&
+ git commit --allow-empty -m two &&
+ GIT_COMMITTER_NAME="Sin Nombre" git commit --allow-empty -m three &&
+
+ cat >expect <<-\EOF &&
+ 2 C O Mitter
+ 1 Sin Nombre
+ EOF
+ git shortlog -nsc HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'shortlog --committer (external)' '
+ git log --format=full | git shortlog -nsc >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--group=committer is the same as --committer' '
+ git shortlog -ns --group=committer HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'shortlog --group=trailer:signed-off-by' '
+ git commit --allow-empty -m foo -s &&
+ GIT_COMMITTER_NAME="SOB One" \
+ GIT_COMMITTER_EMAIL=sob@example.com \
+ git commit --allow-empty -m foo -s &&
+ git commit --allow-empty --amend --no-edit -s &&
+ cat >expect <<-\EOF &&
+ 2 C O Mitter <committer@example.com>
+ 1 SOB One <sob@example.com>
+ EOF
+ git shortlog -nse --group=trailer:signed-off-by HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'shortlog --group=format' '
+ git shortlog -s --date="format:%Y" --group="format:%cN (%cd)" \
+ HEAD >actual &&
+ cat >expect <<-\EOF &&
+ 4 C O Mitter (2005)
+ 1 Sin Nombre (2005)
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'shortlog --group=<format> DWIM' '
+ git shortlog -s --date="format:%Y" --group="%cN (%cd)" HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'shortlog bogus --group' '
+ test_must_fail git shortlog --group=bogus HEAD 2>err &&
+ grep "unknown group type" err
+'
+
+test_expect_success 'trailer idents are split' '
+ cat >expect <<-\EOF &&
+ 2 C O Mitter
+ 1 SOB One
+ EOF
+ git shortlog -ns --group=trailer:signed-off-by HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'trailer idents are mailmapped' '
+ cat >expect <<-\EOF &&
+ 2 C O Mitter
+ 1 Another Name
+ EOF
+ echo "Another Name <sob@example.com>" >mail.map &&
+ git -c mailmap.file=mail.map shortlog -ns \
+ --group=trailer:signed-off-by HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'shortlog de-duplicates trailers in a single commit' '
+ git commit --allow-empty -F - <<-\EOF &&
+ subject one
+
+ this message has two distinct values, plus a repeat
+
+ Repeated-trailer: Foo
+ Repeated-trailer: Bar
+ Repeated-trailer: Foo
+ EOF
+
+ git commit --allow-empty -F - <<-\EOF &&
+ subject two
+
+ similar to the previous, but without the second distinct value
+
+ Repeated-trailer: Foo
+ Repeated-trailer: Foo
+ EOF
+
+ cat >expect <<-\EOF &&
+ 2 Foo
+ 1 Bar
+ EOF
+ git shortlog -ns --group=trailer:repeated-trailer -2 HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'shortlog can match multiple groups' '
+ git commit --allow-empty -F - <<-\EOF &&
+ subject one
+
+ this has two trailers that are distinct from the author; it will count
+ 3 times in the output
+
+ Some-trailer: User A <a@example.com>
+ Another-trailer: User B <b@example.com>
+ EOF
+
+ git commit --allow-empty -F - <<-\EOF &&
+ subject two
+
+ this one has two trailers, one of which is a duplicate with the author;
+ it will only be counted once for them
+
+ Another-trailer: A U Thor <author@example.com>
+ Some-trailer: User B <b@example.com>
+ EOF
+
+ cat >expect <<-\EOF &&
+ 2 A U Thor
+ 2 User B
+ 1 User A
+ EOF
+ git shortlog -ns \
+ --group=author \
+ --group=trailer:some-trailer \
+ --group=trailer:another-trailer \
+ -2 HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'shortlog can match multiple format groups' '
+ GIT_COMMITTER_NAME="$GIT_AUTHOR_NAME" \
+ git commit --allow-empty -m "identical names" &&
+ test_tick &&
+ cat >expect <<-\EOF &&
+ 2 A U Thor
+ 1 C O Mitter
+ EOF
+ git shortlog -ns --group="%cn" --group="%an" -2 HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'set up option selection tests' '
+ git commit --allow-empty -F - <<-\EOF
+ subject
+
+ body
+
+ Trailer-one: value-one
+ Trailer-two: value-two
+ EOF
+'
+
+test_expect_success '--no-group resets group list to author' '
+ cat >expect <<-\EOF &&
+ 1 A U Thor
+ EOF
+ git shortlog -ns \
+ --group=committer \
+ --group=trailer:trailer-one \
+ --no-group \
+ -1 HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--no-group resets trailer list' '
+ cat >expect <<-\EOF &&
+ 1 value-two
+ EOF
+ git shortlog -ns \
+ --group=trailer:trailer-one \
+ --no-group \
+ --group=trailer:trailer-two \
+ -1 HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'stdin with multiple groups reports error' '
+ git log >log &&
+ test_must_fail git shortlog --group=author --group=committer <log
+'
+
+test_done
diff --git a/t/t4202-log.sh b/t/t4202-log.sh
new file mode 100755
index 0000000..7086366
--- /dev/null
+++ b/t/t4202-log.sh
@@ -0,0 +1,2386 @@
+#!/bin/sh
+
+test_description='git log'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY/lib-gpg.sh"
+. "$TEST_DIRECTORY/lib-terminal.sh"
+. "$TEST_DIRECTORY/lib-log-graph.sh"
+
+test_cmp_graph () {
+ lib_test_cmp_graph --format=%s "$@"
+}
+
+test_expect_success setup '
+
+ echo one >one &&
+ git add one &&
+ test_tick &&
+ git commit -m initial &&
+
+ echo ichi >one &&
+ git add one &&
+ test_tick &&
+ git commit -m second &&
+
+ git mv one ichi &&
+ test_tick &&
+ git commit -m third &&
+
+ cp ichi ein &&
+ git add ein &&
+ test_tick &&
+ git commit -m fourth &&
+
+ mkdir a &&
+ echo ni >a/two &&
+ git add a/two &&
+ test_tick &&
+ git commit -m fifth &&
+
+ git rm a/two &&
+ test_tick &&
+ git commit -m sixth
+
+'
+
+printf "sixth\nfifth\nfourth\nthird\nsecond\ninitial" > expect
+test_expect_success 'pretty' '
+
+ git log --pretty="format:%s" > actual &&
+ test_cmp expect actual
+'
+
+printf "sixth\nfifth\nfourth\nthird\nsecond\ninitial\n" > expect
+test_expect_success 'pretty (tformat)' '
+
+ git log --pretty="tformat:%s" > actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'pretty (shortcut)' '
+
+ git log --pretty="%s" > actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'format' '
+
+ git log --format="%s" > actual &&
+ test_cmp expect actual
+'
+
+cat > expect << EOF
+ This is
+ the sixth
+ commit.
+ This is
+ the fifth
+ commit.
+EOF
+
+test_expect_success 'format %w(11,1,2)' '
+
+ git log -2 --format="%w(11,1,2)This is the %s commit." > actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'format %w(,1,2)' '
+
+ git log -2 --format="%w(,1,2)This is%nthe %s%ncommit." > actual &&
+ test_cmp expect actual
+'
+
+cat > expect << EOF
+$(git rev-parse --short :/sixth ) sixth
+$(git rev-parse --short :/fifth ) fifth
+$(git rev-parse --short :/fourth ) fourth
+$(git rev-parse --short :/third ) third
+$(git rev-parse --short :/second ) second
+$(git rev-parse --short :/initial) initial
+EOF
+test_expect_success 'oneline' '
+
+ git log --oneline > actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'diff-filter=A' '
+
+ git log --no-renames --pretty="format:%s" --diff-filter=A HEAD > actual &&
+ git log --no-renames --pretty="format:%s" --diff-filter A HEAD > actual-separate &&
+ printf "fifth\nfourth\nthird\ninitial" > expect &&
+ test_cmp expect actual &&
+ test_cmp expect actual-separate
+
+'
+
+test_expect_success 'diff-filter=M' '
+
+ git log --pretty="format:%s" --diff-filter=M HEAD >actual &&
+ printf "second" >expect &&
+ test_cmp expect actual
+
+'
+
+test_expect_success 'diff-filter=D' '
+
+ git log --no-renames --pretty="format:%s" --diff-filter=D HEAD >actual &&
+ printf "sixth\nthird" >expect &&
+ test_cmp expect actual
+
+'
+
+test_expect_success 'diff-filter=R' '
+
+ git log -M --pretty="format:%s" --diff-filter=R HEAD >actual &&
+ printf "third" >expect &&
+ test_cmp expect actual
+
+'
+
+test_expect_success 'multiple --diff-filter bits' '
+
+ git log -M --pretty="format:%s" --diff-filter=R HEAD >expect &&
+ git log -M --pretty="format:%s" --diff-filter=Ra HEAD >actual &&
+ test_cmp expect actual &&
+ git log -M --pretty="format:%s" --diff-filter=aR HEAD >actual &&
+ test_cmp expect actual &&
+ git log -M --pretty="format:%s" \
+ --diff-filter=a --diff-filter=R HEAD >actual &&
+ test_cmp expect actual
+
+'
+
+test_expect_success 'diff-filter=C' '
+
+ git log -C -C --pretty="format:%s" --diff-filter=C HEAD >actual &&
+ printf "fourth" >expect &&
+ test_cmp expect actual
+
+'
+
+test_expect_success 'git log --follow' '
+
+ git log --follow --pretty="format:%s" ichi >actual &&
+ printf "third\nsecond\ninitial" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git config log.follow works like --follow' '
+ test_config log.follow true &&
+ git log --pretty="format:%s" ichi >actual &&
+ printf "third\nsecond\ninitial" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git config log.follow does not die with multiple paths' '
+ test_config log.follow true &&
+ git log --pretty="format:%s" ichi ein
+'
+
+test_expect_success 'git config log.follow does not die with no paths' '
+ test_config log.follow true &&
+ git log --
+'
+
+test_expect_success 'git log --follow rejects unsupported pathspec magic' '
+ test_must_fail git log --follow ":(top,glob,icase)ichi" 2>stderr &&
+ # check full error message; we want to be sure we mention both
+ # of the rejected types (glob,icase), but not the allowed one (top)
+ echo "fatal: pathspec magic not supported by --follow: ${SQ}glob${SQ}, ${SQ}icase${SQ}" >expect &&
+ test_cmp expect stderr
+'
+
+test_expect_success 'log.follow disabled with unsupported pathspec magic' '
+ test_config log.follow true &&
+ git log --format=%s ":(glob,icase)ichi" >actual &&
+ echo third >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git config log.follow is overridden by --no-follow' '
+ test_config log.follow true &&
+ git log --no-follow --pretty="format:%s" ichi >actual &&
+ printf "third" >expect &&
+ test_cmp expect actual
+'
+
+# Note that these commits are intentionally listed out of order.
+last_three="$(git rev-parse :/fourth :/sixth :/fifth)"
+cat > expect << EOF
+$(git rev-parse --short :/sixth ) sixth
+$(git rev-parse --short :/fifth ) fifth
+$(git rev-parse --short :/fourth) fourth
+EOF
+test_expect_success 'git log --no-walk <commits> sorts by commit time' '
+ git log --no-walk --oneline $last_three > actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git log --no-walk=sorted <commits> sorts by commit time' '
+ git log --no-walk=sorted --oneline $last_three > actual &&
+ test_cmp expect actual
+'
+
+cat > expect << EOF
+=== $(git rev-parse --short :/sixth ) sixth
+=== $(git rev-parse --short :/fifth ) fifth
+=== $(git rev-parse --short :/fourth) fourth
+EOF
+test_expect_success 'git log --line-prefix="=== " --no-walk <commits> sorts by commit time' '
+ git log --line-prefix="=== " --no-walk --oneline $last_three > actual &&
+ test_cmp expect actual
+'
+
+cat > expect << EOF
+$(git rev-parse --short :/fourth) fourth
+$(git rev-parse --short :/sixth ) sixth
+$(git rev-parse --short :/fifth ) fifth
+EOF
+test_expect_success 'git log --no-walk=unsorted <commits> leaves list of commits as given' '
+ git log --no-walk=unsorted --oneline $last_three > actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git show <commits> leaves list of commits as given' '
+ git show --oneline -s $last_three > actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'setup case sensitivity tests' '
+ echo case >one &&
+ test_tick &&
+ git add one &&
+ git commit -a -m Second
+'
+
+test_expect_success 'log --grep' '
+ echo second >expect &&
+ git log -1 --pretty="tformat:%s" --grep=sec >actual &&
+ test_cmp expect actual
+'
+
+for noop_opt in --invert-grep --all-match
+do
+ test_expect_success "log $noop_opt without --grep is a NOOP" '
+ git log >expect &&
+ git log $noop_opt >actual &&
+ test_cmp expect actual
+ '
+done
+
+cat > expect << EOF
+second
+initial
+EOF
+test_expect_success 'log --invert-grep --grep' '
+ # Fixed
+ git -c grep.patternType=fixed log --pretty="tformat:%s" --invert-grep --grep=th --grep=Sec >actual &&
+ test_cmp expect actual &&
+
+ # POSIX basic
+ git -c grep.patternType=basic log --pretty="tformat:%s" --invert-grep --grep=t[h] --grep=S[e]c >actual &&
+ test_cmp expect actual &&
+
+ # POSIX extended
+ git -c grep.patternType=extended log --pretty="tformat:%s" --invert-grep --grep=t[h] --grep=S[e]c >actual &&
+ test_cmp expect actual &&
+
+ # PCRE
+ if test_have_prereq PCRE
+ then
+ git -c grep.patternType=perl log --pretty="tformat:%s" --invert-grep --grep=t[h] --grep=S[e]c >actual &&
+ test_cmp expect actual
+ fi
+'
+
+test_expect_success 'log --invert-grep --grep -i' '
+ echo initial >expect &&
+
+ # Fixed
+ git -c grep.patternType=fixed log --pretty="tformat:%s" --invert-grep -i --grep=th --grep=Sec >actual &&
+ test_cmp expect actual &&
+
+ # POSIX basic
+ git -c grep.patternType=basic log --pretty="tformat:%s" --invert-grep -i --grep=t[h] --grep=S[e]c >actual &&
+ test_cmp expect actual &&
+
+ # POSIX extended
+ git -c grep.patternType=extended log --pretty="tformat:%s" --invert-grep -i --grep=t[h] --grep=S[e]c >actual &&
+ test_cmp expect actual &&
+
+ # PCRE
+ if test_have_prereq PCRE
+ then
+ git -c grep.patternType=perl log --pretty="tformat:%s" --invert-grep -i --grep=t[h] --grep=S[e]c >actual &&
+ test_cmp expect actual
+ fi
+'
+
+test_expect_success 'log --grep option parsing' '
+ echo second >expect &&
+ git log -1 --pretty="tformat:%s" --grep sec >actual &&
+ test_cmp expect actual &&
+ test_must_fail git log -1 --pretty="tformat:%s" --grep
+'
+
+test_expect_success 'log -i --grep' '
+ echo Second >expect &&
+ git log -1 --pretty="tformat:%s" -i --grep=sec >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log --grep -i' '
+ echo Second >expect &&
+
+ # Fixed
+ git log -1 --pretty="tformat:%s" --grep=sec -i >actual &&
+ test_cmp expect actual &&
+
+ # POSIX basic
+ git -c grep.patternType=basic log -1 --pretty="tformat:%s" --grep=s[e]c -i >actual &&
+ test_cmp expect actual &&
+
+ # POSIX extended
+ git -c grep.patternType=extended log -1 --pretty="tformat:%s" --grep=s[e]c -i >actual &&
+ test_cmp expect actual &&
+
+ # PCRE
+ if test_have_prereq PCRE
+ then
+ git -c grep.patternType=perl log -1 --pretty="tformat:%s" --grep=s[e]c -i >actual &&
+ test_cmp expect actual
+ fi
+'
+
+test_expect_success 'log -F -E --grep=<ere> uses ere' '
+ echo second >expect &&
+ # basic would need \(s\) to do the same
+ git log -1 --pretty="tformat:%s" -F -E --grep="(s).c.nd" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success PCRE 'log -F -E --perl-regexp --grep=<pcre> uses PCRE' '
+ test_when_finished "rm -rf num_commits" &&
+ git init num_commits &&
+ (
+ cd num_commits &&
+ test_commit 1d &&
+ test_commit 2e
+ ) &&
+
+ # In PCRE \d in [\d] is like saying "0-9", and matches the 2
+ # in 2e...
+ echo 2e >expect &&
+ git -C num_commits log -1 --pretty="tformat:%s" -F -E --perl-regexp --grep="[\d]" >actual &&
+ test_cmp expect actual &&
+
+ # ...in POSIX basic and extended it is the same as [d],
+ # i.e. "d", which matches 1d, but does not match 2e.
+ echo 1d >expect &&
+ git -C num_commits log -1 --pretty="tformat:%s" -F -E --grep="[\d]" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log with grep.patternType configuration' '
+ git -c grep.patterntype=fixed \
+ log -1 --pretty=tformat:%s --grep=s.c.nd >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'log with grep.patternType configuration and command line' '
+ echo second >expect &&
+ git -c grep.patterntype=fixed \
+ log -1 --pretty=tformat:%s --basic-regexp --grep=s.c.nd >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success !FAIL_PREREQS 'log with various grep.patternType configurations & command-lines' '
+ git init pattern-type &&
+ (
+ cd pattern-type &&
+ test_commit 1 file A &&
+
+ # The tagname is overridden here because creating a
+ # tag called "(1|2)" as test_commit would otherwise
+ # implicitly do would fail on e.g. MINGW.
+ test_commit "(1|2)" file B 2 &&
+
+ echo "(1|2)" >expect.fixed &&
+ cp expect.fixed expect.basic &&
+ cp expect.fixed expect.extended &&
+ cp expect.fixed expect.perl &&
+
+ # A strcmp-like match with fixed.
+ git -c grep.patternType=fixed log --pretty=tformat:%s \
+ --grep="(1|2)" >actual.fixed &&
+
+ # POSIX basic matches (, | and ) literally.
+ git -c grep.patternType=basic log --pretty=tformat:%s \
+ --grep="(.|.)" >actual.basic &&
+
+ # POSIX extended needs to have | escaped to match it
+ # literally, whereas under basic this is the same as
+ # (|2), i.e. it would also match "1". This test checks
+ # for extended by asserting that it is not matching
+ # what basic would match.
+ git -c grep.patternType=extended log --pretty=tformat:%s \
+ --grep="\|2" >actual.extended &&
+ if test_have_prereq PCRE
+ then
+ # Only PCRE would match [\d]\| with only
+ # "(1|2)" due to [\d]. POSIX basic would match
+ # both it and "1" since similarly to the
+ # extended match above it is the same as
+ # \([\d]\|\). POSIX extended would
+ # match neither.
+ git -c grep.patternType=perl log --pretty=tformat:%s \
+ --grep="[\d]\|" >actual.perl &&
+ test_cmp expect.perl actual.perl
+ fi &&
+ test_cmp expect.fixed actual.fixed &&
+ test_cmp expect.basic actual.basic &&
+ test_cmp expect.extended actual.extended &&
+
+ git log --pretty=tformat:%s -F \
+ --grep="(1|2)" >actual.fixed.short-arg &&
+ git log --pretty=tformat:%s -E \
+ --grep="\|2" >actual.extended.short-arg &&
+ if test_have_prereq PCRE
+ then
+ git log --pretty=tformat:%s -P \
+ --grep="[\d]\|" >actual.perl.short-arg
+ else
+ test_must_fail git log -P \
+ --grep="[\d]\|"
+ fi &&
+ test_cmp expect.fixed actual.fixed.short-arg &&
+ test_cmp expect.extended actual.extended.short-arg &&
+ if test_have_prereq PCRE
+ then
+ test_cmp expect.perl actual.perl.short-arg
+ fi &&
+
+ git log --pretty=tformat:%s --fixed-strings \
+ --grep="(1|2)" >actual.fixed.long-arg &&
+ git log --pretty=tformat:%s --basic-regexp \
+ --grep="(.|.)" >actual.basic.long-arg &&
+ git log --pretty=tformat:%s --extended-regexp \
+ --grep="\|2" >actual.extended.long-arg &&
+ if test_have_prereq PCRE
+ then
+ git log --pretty=tformat:%s --perl-regexp \
+ --grep="[\d]\|" >actual.perl.long-arg &&
+ test_cmp expect.perl actual.perl.long-arg
+ else
+ test_must_fail git log --perl-regexp \
+ --grep="[\d]\|"
+ fi &&
+ test_cmp expect.fixed actual.fixed.long-arg &&
+ test_cmp expect.basic actual.basic.long-arg &&
+ test_cmp expect.extended actual.extended.long-arg
+ )
+'
+
+for cmd in show whatchanged reflog format-patch
+do
+ case "$cmd" in
+ format-patch) myarg="HEAD~.." ;;
+ *) myarg= ;;
+ esac
+
+ test_expect_success "$cmd: understands grep.patternType, like 'log'" '
+ git init "pattern-type-$cmd" &&
+ (
+ cd "pattern-type-$cmd" &&
+ test_commit 1 file A &&
+ test_commit "(1|2)" file B 2 &&
+
+ git -c grep.patternType=fixed $cmd --grep="..." $myarg >actual &&
+ test_must_be_empty actual &&
+
+ git -c grep.patternType=basic $cmd --grep="..." $myarg >actual &&
+ test_file_not_empty actual
+ )
+ '
+done
+
+test_expect_success 'log --author' '
+ cat >expect <<-\EOF &&
+ Author: <BOLD;RED>A U<RESET> Thor <author@example.com>
+ EOF
+ git log -1 --color=always --author="A U" >log &&
+ grep Author log >actual.raw &&
+ test_decode_color <actual.raw >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log --committer' '
+ cat >expect <<-\EOF &&
+ Commit: C O Mitter <committer@<BOLD;RED>example<RESET>.com>
+ EOF
+ git log -1 --color=always --pretty=fuller --committer="example" >log &&
+ grep "Commit:" log >actual.raw &&
+ test_decode_color <actual.raw >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log -i --grep with color' '
+ cat >expect <<-\EOF &&
+ <BOLD;RED>Sec<RESET>ond
+ <BOLD;RED>sec<RESET>ond
+ EOF
+ git log --color=always -i --grep=^sec >log &&
+ grep -i sec log >actual.raw &&
+ test_decode_color <actual.raw >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '-c color.grep.selected log --grep' '
+ cat >expect <<-\EOF &&
+ <GREEN>th<RESET><BOLD;RED>ir<RESET><GREEN>d<RESET>
+ EOF
+ git -c color.grep.selected="green" log --color=always --grep=ir >log &&
+ grep ir log >actual.raw &&
+ test_decode_color <actual.raw >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '-c color.grep.matchSelected log --grep' '
+ cat >expect <<-\EOF &&
+ <BLUE>i<RESET>n<BLUE>i<RESET>t<BLUE>i<RESET>al
+ EOF
+ git -c color.grep.matchSelected="blue" log --color=always --grep=i >log &&
+ grep al log >actual.raw &&
+ test_decode_color <actual.raw >actual &&
+ test_cmp expect actual
+'
+
+cat > expect <<EOF
+* Second
+* sixth
+* fifth
+* fourth
+* third
+* second
+* initial
+EOF
+
+test_expect_success 'simple log --graph' '
+ test_cmp_graph
+'
+
+cat > expect <<EOF
+123 * Second
+123 * sixth
+123 * fifth
+123 * fourth
+123 * third
+123 * second
+123 * initial
+EOF
+
+test_expect_success 'simple log --graph --line-prefix="123 "' '
+ test_cmp_graph --line-prefix="123 "
+'
+
+test_expect_success 'set up merge history' '
+ git checkout -b side HEAD~4 &&
+ test_commit side-1 1 1 &&
+ test_commit side-2 2 2 &&
+ git checkout main &&
+ git merge side
+'
+
+cat > expect <<\EOF
+* Merge branch 'side'
+|\
+| * side-2
+| * side-1
+* | Second
+* | sixth
+* | fifth
+* | fourth
+|/
+* third
+* second
+* initial
+EOF
+
+test_expect_success 'log --graph with merge' '
+ test_cmp_graph --date-order
+'
+
+cat > expect <<\EOF
+| | | * Merge branch 'side'
+| | | |\
+| | | | * side-2
+| | | | * side-1
+| | | * | Second
+| | | * | sixth
+| | | * | fifth
+| | | * | fourth
+| | | |/
+| | | * third
+| | | * second
+| | | * initial
+EOF
+
+test_expect_success 'log --graph --line-prefix="| | | " with merge' '
+ test_cmp_graph --line-prefix="| | | " --date-order
+'
+
+cat > expect.colors <<\EOF
+* Merge branch 'side'
+<BLUE>|<RESET><CYAN>\<RESET>
+<BLUE>|<RESET> * side-2
+<BLUE>|<RESET> * side-1
+* <CYAN>|<RESET> Second
+* <CYAN>|<RESET> sixth
+* <CYAN>|<RESET> fifth
+* <CYAN>|<RESET> fourth
+<CYAN>|<RESET><CYAN>/<RESET>
+* third
+* second
+* initial
+EOF
+
+test_expect_success 'log --graph with merge with log.graphColors' '
+ test_config log.graphColors " blue,invalid-color, cyan, red , " &&
+ lib_test_cmp_colored_graph --date-order --format=%s
+'
+
+test_expect_success 'log --raw --graph -m with merge' '
+ git log --raw --graph --oneline -m main | head -n 500 >actual &&
+ grep "initial" actual
+'
+
+test_expect_success 'diff-tree --graph' '
+ git diff-tree --graph main^ | head -n 500 >actual &&
+ grep "one" actual
+'
+
+cat > expect <<\EOF
+* commit main
+|\ Merge: A B
+| | Author: A U Thor <author@example.com>
+| |
+| | Merge branch 'side'
+| |
+| * commit tags/side-2
+| | Author: A U Thor <author@example.com>
+| |
+| | side-2
+| |
+| * commit tags/side-1
+| | Author: A U Thor <author@example.com>
+| |
+| | side-1
+| |
+* | commit main~1
+| | Author: A U Thor <author@example.com>
+| |
+| | Second
+| |
+* | commit main~2
+| | Author: A U Thor <author@example.com>
+| |
+| | sixth
+| |
+* | commit main~3
+| | Author: A U Thor <author@example.com>
+| |
+| | fifth
+| |
+* | commit main~4
+|/ Author: A U Thor <author@example.com>
+|
+| fourth
+|
+* commit tags/side-1~1
+| Author: A U Thor <author@example.com>
+|
+| third
+|
+* commit tags/side-1~2
+| Author: A U Thor <author@example.com>
+|
+| second
+|
+* commit tags/side-1~3
+ Author: A U Thor <author@example.com>
+
+ initial
+EOF
+
+test_expect_success 'log --graph with full output' '
+ git log --graph --date-order --pretty=short |
+ git name-rev --name-only --annotate-stdin |
+ sed "s/Merge:.*/Merge: A B/;s/ *\$//" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'set up more tangled history' '
+ git checkout -b tangle HEAD~6 &&
+ test_commit tangle-a tangle-a a &&
+ git merge main~3 &&
+ git update-ref refs/prefetch/merge HEAD &&
+ git merge side~1 &&
+ git update-ref refs/rewritten/merge HEAD &&
+ git checkout main &&
+ git merge tangle &&
+ git update-ref refs/hidden/tangle HEAD &&
+ git checkout -b reach &&
+ test_commit reach &&
+ git checkout main &&
+ git checkout -b octopus-a &&
+ test_commit octopus-a &&
+ git checkout main &&
+ git checkout -b octopus-b &&
+ test_commit octopus-b &&
+ git checkout main &&
+ test_commit seventh &&
+ git merge octopus-a octopus-b &&
+ git merge reach
+'
+
+cat > expect <<\EOF
+* Merge tag 'reach'
+|\
+| \
+| \
+*-. \ Merge tags 'octopus-a' and 'octopus-b'
+|\ \ \
+* | | | seventh
+| | * | octopus-b
+| |/ /
+|/| |
+| * | octopus-a
+|/ /
+| * reach
+|/
+* Merge branch 'tangle'
+|\
+| * Merge branch 'side' (early part) into tangle
+| |\
+| * \ Merge branch 'main' (early part) into tangle
+| |\ \
+| * | | tangle-a
+* | | | Merge branch 'side'
+|\ \ \ \
+| * | | | side-2
+| | |_|/
+| |/| |
+| * | | side-1
+* | | | Second
+* | | | sixth
+| |_|/
+|/| |
+* | | fifth
+* | | fourth
+|/ /
+* / third
+|/
+* second
+* initial
+EOF
+
+test_expect_success 'log --graph with merge' '
+ test_cmp_graph --date-order
+'
+
+test_expect_success 'log.decorate configuration' '
+ git log --oneline --no-decorate >expect.none &&
+ git log --oneline --decorate >expect.short &&
+ git log --oneline --decorate=full >expect.full &&
+
+ echo "[log] decorate" >>.git/config &&
+ git log --oneline >actual &&
+ test_cmp expect.short actual &&
+
+ test_config log.decorate true &&
+ git log --oneline >actual &&
+ test_cmp expect.short actual &&
+ git log --oneline --decorate=full >actual &&
+ test_cmp expect.full actual &&
+ git log --oneline --decorate=no >actual &&
+ test_cmp expect.none actual &&
+
+ test_config log.decorate no &&
+ git log --oneline >actual &&
+ test_cmp expect.none actual &&
+ git log --oneline --decorate >actual &&
+ test_cmp expect.short actual &&
+ git log --oneline --decorate=full >actual &&
+ test_cmp expect.full actual &&
+
+ test_config log.decorate 1 &&
+ git log --oneline >actual &&
+ test_cmp expect.short actual &&
+ git log --oneline --decorate=full >actual &&
+ test_cmp expect.full actual &&
+ git log --oneline --decorate=no >actual &&
+ test_cmp expect.none actual &&
+
+ test_config log.decorate short &&
+ git log --oneline >actual &&
+ test_cmp expect.short actual &&
+ git log --oneline --no-decorate >actual &&
+ test_cmp expect.none actual &&
+ git log --oneline --decorate=full >actual &&
+ test_cmp expect.full actual &&
+
+ test_config log.decorate full &&
+ git log --oneline >actual &&
+ test_cmp expect.full actual &&
+ git log --oneline --no-decorate >actual &&
+ test_cmp expect.none actual &&
+ git log --oneline --decorate >actual &&
+ test_cmp expect.short actual &&
+
+ test_unconfig log.decorate &&
+ git log --pretty=raw >expect.raw &&
+ test_config log.decorate full &&
+ git log --pretty=raw >actual &&
+ test_cmp expect.raw actual
+
+'
+
+test_expect_success 'parse log.excludeDecoration with no value' '
+ cp .git/config .git/config.orig &&
+ test_when_finished mv .git/config.orig .git/config &&
+
+ cat >>.git/config <<-\EOF &&
+ [log]
+ excludeDecoration
+ EOF
+ cat >expect <<-\EOF &&
+ error: missing value for '\''log.excludeDecoration'\''
+ EOF
+ git log --decorate=short 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'decorate-refs with glob' '
+ cat >expect.decorate <<-\EOF &&
+ Merge-tag-reach
+ Merge-tags-octopus-a-and-octopus-b
+ seventh
+ octopus-b (octopus-b)
+ octopus-a (octopus-a)
+ reach
+ EOF
+ cat >expect.no-decorate <<-\EOF &&
+ Merge-tag-reach
+ Merge-tags-octopus-a-and-octopus-b
+ seventh
+ octopus-b
+ octopus-a
+ reach
+ EOF
+ git log -n6 --decorate=short --pretty="tformat:%f%d" \
+ --decorate-refs="heads/octopus*" >actual &&
+ test_cmp expect.decorate actual &&
+ git log -n6 --decorate=short --pretty="tformat:%f%d" \
+ --decorate-refs-exclude="heads/octopus*" \
+ --decorate-refs="heads/octopus*" >actual &&
+ test_cmp expect.no-decorate actual &&
+ git -c log.excludeDecoration="heads/octopus*" log \
+ -n6 --decorate=short --pretty="tformat:%f%d" \
+ --decorate-refs="heads/octopus*" >actual &&
+ test_cmp expect.decorate actual
+'
+
+test_expect_success 'decorate-refs without globs' '
+ cat >expect.decorate <<-\EOF &&
+ Merge-tag-reach
+ Merge-tags-octopus-a-and-octopus-b
+ seventh
+ octopus-b
+ octopus-a
+ reach (tag: reach)
+ EOF
+ git log -n6 --decorate=short --pretty="tformat:%f%d" \
+ --decorate-refs="tags/reach" >actual &&
+ test_cmp expect.decorate actual
+'
+
+test_expect_success 'multiple decorate-refs' '
+ cat >expect.decorate <<-\EOF &&
+ Merge-tag-reach
+ Merge-tags-octopus-a-and-octopus-b
+ seventh
+ octopus-b (octopus-b)
+ octopus-a (octopus-a)
+ reach (tag: reach)
+ EOF
+ git log -n6 --decorate=short --pretty="tformat:%f%d" \
+ --decorate-refs="heads/octopus*" \
+ --decorate-refs="tags/reach" >actual &&
+ test_cmp expect.decorate actual
+'
+
+test_expect_success 'decorate-refs-exclude with glob' '
+ cat >expect.decorate <<-\EOF &&
+ Merge-tag-reach (HEAD -> main)
+ Merge-tags-octopus-a-and-octopus-b
+ seventh (tag: seventh)
+ octopus-b (tag: octopus-b)
+ octopus-a (tag: octopus-a)
+ reach (tag: reach, reach)
+ EOF
+ git log -n6 --decorate=short --pretty="tformat:%f%d" \
+ --decorate-refs-exclude="heads/octopus*" >actual &&
+ test_cmp expect.decorate actual &&
+ git -c log.excludeDecoration="heads/octopus*" log \
+ -n6 --decorate=short --pretty="tformat:%f%d" >actual &&
+ test_cmp expect.decorate actual
+'
+
+test_expect_success 'decorate-refs-exclude without globs' '
+ cat >expect.decorate <<-\EOF &&
+ Merge-tag-reach (HEAD -> main)
+ Merge-tags-octopus-a-and-octopus-b
+ seventh (tag: seventh)
+ octopus-b (tag: octopus-b, octopus-b)
+ octopus-a (tag: octopus-a, octopus-a)
+ reach (reach)
+ EOF
+ git log -n6 --decorate=short --pretty="tformat:%f%d" \
+ --decorate-refs-exclude="tags/reach" >actual &&
+ test_cmp expect.decorate actual &&
+ git -c log.excludeDecoration="tags/reach" log \
+ -n6 --decorate=short --pretty="tformat:%f%d" >actual &&
+ test_cmp expect.decorate actual
+'
+
+test_expect_success 'multiple decorate-refs-exclude' '
+ cat >expect.decorate <<-\EOF &&
+ Merge-tag-reach (HEAD -> main)
+ Merge-tags-octopus-a-and-octopus-b
+ seventh (tag: seventh)
+ octopus-b (tag: octopus-b)
+ octopus-a (tag: octopus-a)
+ reach (reach)
+ EOF
+ git log -n6 --decorate=short --pretty="tformat:%f%d" \
+ --decorate-refs-exclude="heads/octopus*" \
+ --decorate-refs-exclude="tags/reach" >actual &&
+ test_cmp expect.decorate actual &&
+ git -c log.excludeDecoration="heads/octopus*" \
+ -c log.excludeDecoration="tags/reach" log \
+ -n6 --decorate=short --pretty="tformat:%f%d" >actual &&
+ test_cmp expect.decorate actual &&
+ git -c log.excludeDecoration="heads/octopus*" log \
+ --decorate-refs-exclude="tags/reach" \
+ -n6 --decorate=short --pretty="tformat:%f%d" >actual &&
+ test_cmp expect.decorate actual
+'
+
+test_expect_success 'decorate-refs and decorate-refs-exclude' '
+ cat >expect.no-decorate <<-\EOF &&
+ Merge-tag-reach (main)
+ Merge-tags-octopus-a-and-octopus-b
+ seventh
+ octopus-b
+ octopus-a
+ reach (reach)
+ EOF
+ git log -n6 --decorate=short --pretty="tformat:%f%d" \
+ --decorate-refs="heads/*" \
+ --decorate-refs-exclude="heads/oc*" >actual &&
+ test_cmp expect.no-decorate actual
+'
+
+test_expect_success 'deocrate-refs and log.excludeDecoration' '
+ cat >expect.decorate <<-\EOF &&
+ Merge-tag-reach (main)
+ Merge-tags-octopus-a-and-octopus-b
+ seventh
+ octopus-b (octopus-b)
+ octopus-a (octopus-a)
+ reach (reach)
+ EOF
+ git -c log.excludeDecoration="heads/oc*" log \
+ --decorate-refs="heads/*" \
+ -n6 --decorate=short --pretty="tformat:%f%d" >actual &&
+ test_cmp expect.decorate actual
+'
+
+test_expect_success 'decorate-refs-exclude and simplify-by-decoration' '
+ cat >expect.decorate <<-\EOF &&
+ Merge-tag-reach (HEAD -> main)
+ reach (tag: reach, reach)
+ seventh (tag: seventh)
+ Merge-branch-tangle (refs/hidden/tangle)
+ Merge-branch-side-early-part-into-tangle (refs/rewritten/merge, tangle)
+ Merge-branch-main-early-part-into-tangle (refs/prefetch/merge)
+ EOF
+ git log -n6 --decorate=short --pretty="tformat:%f%d" \
+ --decorate-refs-exclude="*octopus*" \
+ --simplify-by-decoration >actual &&
+ test_cmp expect.decorate actual &&
+ git -c log.excludeDecoration="*octopus*" log \
+ -n6 --decorate=short --pretty="tformat:%f%d" \
+ --simplify-by-decoration >actual &&
+ test_cmp expect.decorate actual
+'
+
+test_expect_success 'decorate-refs with implied decorate from format' '
+ cat >expect <<-\EOF &&
+ side-2 (tag: side-2)
+ side-1
+ EOF
+ git log --no-walk --format="%s%d" \
+ --decorate-refs="*side-2" side-1 side-2 \
+ >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'implied decorate does not override option' '
+ cat >expect <<-\EOF &&
+ side-2 (tag: refs/tags/side-2, refs/heads/side)
+ side-1 (tag: refs/tags/side-1)
+ EOF
+ git log --no-walk --format="%s%d" \
+ --decorate=full side-1 side-2 \
+ >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'decorate-refs and simplify-by-decoration without output' '
+ cat >expect <<-\EOF &&
+ side-2
+ initial
+ EOF
+ # Do not just use a --format without %d here; we want to
+ # make sure that we did not accidentally turn on displaying
+ # the decorations, too. And that requires one of the regular
+ # formats.
+ git log --decorate-refs="*side-2" --oneline \
+ --simplify-by-decoration >actual.raw &&
+ sed "s/^[0-9a-f]* //" <actual.raw >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'decorate-refs-exclude HEAD' '
+ git log --decorate=full --oneline \
+ --decorate-refs-exclude="HEAD" >actual &&
+ ! grep HEAD actual
+'
+
+test_expect_success 'decorate-refs focus from default' '
+ git log --decorate=full --oneline \
+ --decorate-refs="refs/heads" >actual &&
+ ! grep HEAD actual
+'
+
+test_expect_success '--clear-decorations overrides defaults' '
+ cat >expect.default <<-\EOF &&
+ Merge-tag-reach (HEAD -> refs/heads/main)
+ Merge-tags-octopus-a-and-octopus-b
+ seventh (tag: refs/tags/seventh)
+ octopus-b (tag: refs/tags/octopus-b, refs/heads/octopus-b)
+ octopus-a (tag: refs/tags/octopus-a, refs/heads/octopus-a)
+ reach (tag: refs/tags/reach, refs/heads/reach)
+ Merge-branch-tangle
+ Merge-branch-side-early-part-into-tangle (refs/heads/tangle)
+ Merge-branch-main-early-part-into-tangle
+ tangle-a (tag: refs/tags/tangle-a)
+ Merge-branch-side
+ side-2 (tag: refs/tags/side-2, refs/heads/side)
+ side-1 (tag: refs/tags/side-1)
+ Second
+ sixth
+ fifth
+ fourth
+ third
+ second
+ initial
+ EOF
+ git log --decorate=full --pretty="tformat:%f%d" >actual &&
+ test_cmp expect.default actual &&
+
+ cat >expect.all <<-\EOF &&
+ Merge-tag-reach (HEAD -> refs/heads/main)
+ Merge-tags-octopus-a-and-octopus-b
+ seventh (tag: refs/tags/seventh)
+ octopus-b (tag: refs/tags/octopus-b, refs/heads/octopus-b)
+ octopus-a (tag: refs/tags/octopus-a, refs/heads/octopus-a)
+ reach (tag: refs/tags/reach, refs/heads/reach)
+ Merge-branch-tangle (refs/hidden/tangle)
+ Merge-branch-side-early-part-into-tangle (refs/rewritten/merge, refs/heads/tangle)
+ Merge-branch-main-early-part-into-tangle (refs/prefetch/merge)
+ tangle-a (tag: refs/tags/tangle-a)
+ Merge-branch-side
+ side-2 (tag: refs/tags/side-2, refs/heads/side)
+ side-1 (tag: refs/tags/side-1)
+ Second
+ sixth
+ fifth
+ fourth
+ third
+ second
+ initial
+ EOF
+ git log --decorate=full --pretty="tformat:%f%d" \
+ --clear-decorations >actual &&
+ test_cmp expect.all actual &&
+ git -c log.initialDecorationSet=all log \
+ --decorate=full --pretty="tformat:%f%d" >actual &&
+ test_cmp expect.all actual
+'
+
+test_expect_success '--clear-decorations clears previous exclusions' '
+ cat >expect.all <<-\EOF &&
+ Merge-tag-reach (HEAD -> refs/heads/main)
+ reach (tag: refs/tags/reach, refs/heads/reach)
+ Merge-tags-octopus-a-and-octopus-b
+ octopus-b (tag: refs/tags/octopus-b, refs/heads/octopus-b)
+ octopus-a (tag: refs/tags/octopus-a, refs/heads/octopus-a)
+ seventh (tag: refs/tags/seventh)
+ Merge-branch-tangle (refs/hidden/tangle)
+ Merge-branch-side-early-part-into-tangle (refs/rewritten/merge, refs/heads/tangle)
+ Merge-branch-main-early-part-into-tangle (refs/prefetch/merge)
+ tangle-a (tag: refs/tags/tangle-a)
+ side-2 (tag: refs/tags/side-2, refs/heads/side)
+ side-1 (tag: refs/tags/side-1)
+ initial
+ EOF
+
+ git log --decorate=full --pretty="tformat:%f%d" \
+ --simplify-by-decoration \
+ --decorate-refs-exclude="heads/octopus*" \
+ --decorate-refs="heads" \
+ --clear-decorations >actual &&
+ test_cmp expect.all actual &&
+
+ cat >expect.filtered <<-\EOF &&
+ Merge-tags-octopus-a-and-octopus-b
+ octopus-b (refs/heads/octopus-b)
+ octopus-a (refs/heads/octopus-a)
+ initial
+ EOF
+
+ git log --decorate=full --pretty="tformat:%f%d" \
+ --simplify-by-decoration \
+ --decorate-refs-exclude="heads/octopus" \
+ --decorate-refs="heads" \
+ --clear-decorations \
+ --decorate-refs-exclude="tags/" \
+ --decorate-refs="heads/octopus*" >actual &&
+ test_cmp expect.filtered actual
+'
+
+test_expect_success 'log.decorate config parsing' '
+ git log --oneline --decorate=full >expect.full &&
+ git log --oneline --decorate=short >expect.short &&
+
+ test_config log.decorate full &&
+ test_config log.mailmap true &&
+ git log --oneline >actual &&
+ test_cmp expect.full actual &&
+ git log --oneline --decorate=short >actual &&
+ test_cmp expect.short actual
+'
+
+test_expect_success TTY 'log output on a TTY' '
+ git log --color --oneline --decorate >expect.short &&
+
+ test_terminal git log --oneline >actual &&
+ test_cmp expect.short actual
+'
+
+test_expect_success 'reflog is expected format' '
+ git log -g --abbrev-commit --pretty=oneline >expect &&
+ git reflog >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'whatchanged is expected format' '
+ git log --no-merges --raw >expect &&
+ git whatchanged >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log.abbrevCommit configuration' '
+ git log --abbrev-commit >expect.log.abbrev &&
+ git log --no-abbrev-commit >expect.log.full &&
+ git log --pretty=raw >expect.log.raw &&
+ git reflog --abbrev-commit >expect.reflog.abbrev &&
+ git reflog --no-abbrev-commit >expect.reflog.full &&
+ git whatchanged --abbrev-commit >expect.whatchanged.abbrev &&
+ git whatchanged --no-abbrev-commit >expect.whatchanged.full &&
+
+ test_config log.abbrevCommit true &&
+
+ git log >actual &&
+ test_cmp expect.log.abbrev actual &&
+ git log --no-abbrev-commit >actual &&
+ test_cmp expect.log.full actual &&
+
+ git log --pretty=raw >actual &&
+ test_cmp expect.log.raw actual &&
+
+ git reflog >actual &&
+ test_cmp expect.reflog.abbrev actual &&
+ git reflog --no-abbrev-commit >actual &&
+ test_cmp expect.reflog.full actual &&
+
+ git whatchanged >actual &&
+ test_cmp expect.whatchanged.abbrev actual &&
+ git whatchanged --no-abbrev-commit >actual &&
+ test_cmp expect.whatchanged.full actual
+'
+
+test_expect_success 'show added path under "--follow -M"' '
+ # This tests for a regression introduced in v1.7.2-rc0~103^2~2
+ test_create_repo regression &&
+ (
+ cd regression &&
+ test_commit needs-another-commit &&
+ test_commit foo.bar &&
+ git log -M --follow -p foo.bar.t &&
+ git log -M --follow --stat foo.bar.t &&
+ git log -M --follow --name-only foo.bar.t
+ )
+'
+
+test_expect_success 'git log -c --follow' '
+ test_create_repo follow-c &&
+ (
+ cd follow-c &&
+ test_commit initial file original &&
+ git rm file &&
+ test_commit rename file2 original &&
+ git reset --hard initial &&
+ test_commit modify file foo &&
+ git merge -m merge rename &&
+ git log -c --follow file2
+ )
+'
+
+cat >expect <<\EOF
+* commit COMMIT_OBJECT_NAME
+|\ Merge: MERGE_PARENTS
+| | Author: A U Thor <author@example.com>
+| |
+| | Merge HEADS DESCRIPTION
+| |
+| * commit COMMIT_OBJECT_NAME
+| | Author: A U Thor <author@example.com>
+| |
+| | reach
+| | ---
+| | reach.t | 1 +
+| | 1 file changed, 1 insertion(+)
+| |
+| | diff --git a/reach.t b/reach.t
+| | new file mode 100644
+| | index BEFORE..AFTER
+| | --- /dev/null
+| | +++ b/reach.t
+| | @@ -0,0 +1 @@
+| | +reach
+| |
+| \
+*-. \ commit COMMIT_OBJECT_NAME
+|\ \ \ Merge: MERGE_PARENTS
+| | | | Author: A U Thor <author@example.com>
+| | | |
+| | | | Merge HEADS DESCRIPTION
+| | | |
+| | * | commit COMMIT_OBJECT_NAME
+| | |/ Author: A U Thor <author@example.com>
+| | |
+| | | octopus-b
+| | | ---
+| | | octopus-b.t | 1 +
+| | | 1 file changed, 1 insertion(+)
+| | |
+| | | diff --git a/octopus-b.t b/octopus-b.t
+| | | new file mode 100644
+| | | index BEFORE..AFTER
+| | | --- /dev/null
+| | | +++ b/octopus-b.t
+| | | @@ -0,0 +1 @@
+| | | +octopus-b
+| | |
+| * | commit COMMIT_OBJECT_NAME
+| |/ Author: A U Thor <author@example.com>
+| |
+| | octopus-a
+| | ---
+| | octopus-a.t | 1 +
+| | 1 file changed, 1 insertion(+)
+| |
+| | diff --git a/octopus-a.t b/octopus-a.t
+| | new file mode 100644
+| | index BEFORE..AFTER
+| | --- /dev/null
+| | +++ b/octopus-a.t
+| | @@ -0,0 +1 @@
+| | +octopus-a
+| |
+* | commit COMMIT_OBJECT_NAME
+|/ Author: A U Thor <author@example.com>
+|
+| seventh
+| ---
+| seventh.t | 1 +
+| 1 file changed, 1 insertion(+)
+|
+| diff --git a/seventh.t b/seventh.t
+| new file mode 100644
+| index BEFORE..AFTER
+| --- /dev/null
+| +++ b/seventh.t
+| @@ -0,0 +1 @@
+| +seventh
+|
+* commit COMMIT_OBJECT_NAME
+|\ Merge: MERGE_PARENTS
+| | Author: A U Thor <author@example.com>
+| |
+| | Merge branch 'tangle'
+| |
+| * commit COMMIT_OBJECT_NAME
+| |\ Merge: MERGE_PARENTS
+| | | Author: A U Thor <author@example.com>
+| | |
+| | | Merge branch 'side' (early part) into tangle
+| | |
+| * | commit COMMIT_OBJECT_NAME
+| |\ \ Merge: MERGE_PARENTS
+| | | | Author: A U Thor <author@example.com>
+| | | |
+| | | | Merge branch 'main' (early part) into tangle
+| | | |
+| * | | commit COMMIT_OBJECT_NAME
+| | | | Author: A U Thor <author@example.com>
+| | | |
+| | | | tangle-a
+| | | | ---
+| | | | tangle-a | 1 +
+| | | | 1 file changed, 1 insertion(+)
+| | | |
+| | | | diff --git a/tangle-a b/tangle-a
+| | | | new file mode 100644
+| | | | index BEFORE..AFTER
+| | | | --- /dev/null
+| | | | +++ b/tangle-a
+| | | | @@ -0,0 +1 @@
+| | | | +a
+| | | |
+* | | | commit COMMIT_OBJECT_NAME
+|\ \ \ \ Merge: MERGE_PARENTS
+| | | | | Author: A U Thor <author@example.com>
+| | | | |
+| | | | | Merge branch 'side'
+| | | | |
+| * | | | commit COMMIT_OBJECT_NAME
+| | |_|/ Author: A U Thor <author@example.com>
+| |/| |
+| | | | side-2
+| | | | ---
+| | | | 2 | 1 +
+| | | | 1 file changed, 1 insertion(+)
+| | | |
+| | | | diff --git a/2 b/2
+| | | | new file mode 100644
+| | | | index BEFORE..AFTER
+| | | | --- /dev/null
+| | | | +++ b/2
+| | | | @@ -0,0 +1 @@
+| | | | +2
+| | | |
+| * | | commit COMMIT_OBJECT_NAME
+| | | | Author: A U Thor <author@example.com>
+| | | |
+| | | | side-1
+| | | | ---
+| | | | 1 | 1 +
+| | | | 1 file changed, 1 insertion(+)
+| | | |
+| | | | diff --git a/1 b/1
+| | | | new file mode 100644
+| | | | index BEFORE..AFTER
+| | | | --- /dev/null
+| | | | +++ b/1
+| | | | @@ -0,0 +1 @@
+| | | | +1
+| | | |
+* | | | commit COMMIT_OBJECT_NAME
+| | | | Author: A U Thor <author@example.com>
+| | | |
+| | | | Second
+| | | | ---
+| | | | one | 1 +
+| | | | 1 file changed, 1 insertion(+)
+| | | |
+| | | | diff --git a/one b/one
+| | | | new file mode 100644
+| | | | index BEFORE..AFTER
+| | | | --- /dev/null
+| | | | +++ b/one
+| | | | @@ -0,0 +1 @@
+| | | | +case
+| | | |
+* | | | commit COMMIT_OBJECT_NAME
+| |_|/ Author: A U Thor <author@example.com>
+|/| |
+| | | sixth
+| | | ---
+| | | a/two | 1 -
+| | | 1 file changed, 1 deletion(-)
+| | |
+| | | diff --git a/a/two b/a/two
+| | | deleted file mode 100644
+| | | index BEFORE..AFTER
+| | | --- a/a/two
+| | | +++ /dev/null
+| | | @@ -1 +0,0 @@
+| | | -ni
+| | |
+* | | commit COMMIT_OBJECT_NAME
+| | | Author: A U Thor <author@example.com>
+| | |
+| | | fifth
+| | | ---
+| | | a/two | 1 +
+| | | 1 file changed, 1 insertion(+)
+| | |
+| | | diff --git a/a/two b/a/two
+| | | new file mode 100644
+| | | index BEFORE..AFTER
+| | | --- /dev/null
+| | | +++ b/a/two
+| | | @@ -0,0 +1 @@
+| | | +ni
+| | |
+* | | commit COMMIT_OBJECT_NAME
+|/ / Author: A U Thor <author@example.com>
+| |
+| | fourth
+| | ---
+| | ein | 1 +
+| | 1 file changed, 1 insertion(+)
+| |
+| | diff --git a/ein b/ein
+| | new file mode 100644
+| | index BEFORE..AFTER
+| | --- /dev/null
+| | +++ b/ein
+| | @@ -0,0 +1 @@
+| | +ichi
+| |
+* | commit COMMIT_OBJECT_NAME
+|/ Author: A U Thor <author@example.com>
+|
+| third
+| ---
+| ichi | 1 +
+| one | 1 -
+| 2 files changed, 1 insertion(+), 1 deletion(-)
+|
+| diff --git a/ichi b/ichi
+| new file mode 100644
+| index BEFORE..AFTER
+| --- /dev/null
+| +++ b/ichi
+| @@ -0,0 +1 @@
+| +ichi
+| diff --git a/one b/one
+| deleted file mode 100644
+| index BEFORE..AFTER
+| --- a/one
+| +++ /dev/null
+| @@ -1 +0,0 @@
+| -ichi
+|
+* commit COMMIT_OBJECT_NAME
+| Author: A U Thor <author@example.com>
+|
+| second
+| ---
+| one | 2 +-
+| 1 file changed, 1 insertion(+), 1 deletion(-)
+|
+| diff --git a/one b/one
+| index BEFORE..AFTER 100644
+| --- a/one
+| +++ b/one
+| @@ -1 +1 @@
+| -one
+| +ichi
+|
+* commit COMMIT_OBJECT_NAME
+ Author: A U Thor <author@example.com>
+
+ initial
+ ---
+ one | 1 +
+ 1 file changed, 1 insertion(+)
+
+ diff --git a/one b/one
+ new file mode 100644
+ index BEFORE..AFTER
+ --- /dev/null
+ +++ b/one
+ @@ -0,0 +1 @@
+ +one
+EOF
+
+test_expect_success 'log --graph with diff and stats' '
+ lib_test_cmp_short_graph --no-renames --stat -p
+'
+
+cat >expect <<\EOF
+*** * commit COMMIT_OBJECT_NAME
+*** |\ Merge: MERGE_PARENTS
+*** | | Author: A U Thor <author@example.com>
+*** | |
+*** | | Merge HEADS DESCRIPTION
+*** | |
+*** | * commit COMMIT_OBJECT_NAME
+*** | | Author: A U Thor <author@example.com>
+*** | |
+*** | | reach
+*** | | ---
+*** | | reach.t | 1 +
+*** | | 1 file changed, 1 insertion(+)
+*** | |
+*** | | diff --git a/reach.t b/reach.t
+*** | | new file mode 100644
+*** | | index BEFORE..AFTER
+*** | | --- /dev/null
+*** | | +++ b/reach.t
+*** | | @@ -0,0 +1 @@
+*** | | +reach
+*** | |
+*** | \
+*** *-. \ commit COMMIT_OBJECT_NAME
+*** |\ \ \ Merge: MERGE_PARENTS
+*** | | | | Author: A U Thor <author@example.com>
+*** | | | |
+*** | | | | Merge HEADS DESCRIPTION
+*** | | | |
+*** | | * | commit COMMIT_OBJECT_NAME
+*** | | |/ Author: A U Thor <author@example.com>
+*** | | |
+*** | | | octopus-b
+*** | | | ---
+*** | | | octopus-b.t | 1 +
+*** | | | 1 file changed, 1 insertion(+)
+*** | | |
+*** | | | diff --git a/octopus-b.t b/octopus-b.t
+*** | | | new file mode 100644
+*** | | | index BEFORE..AFTER
+*** | | | --- /dev/null
+*** | | | +++ b/octopus-b.t
+*** | | | @@ -0,0 +1 @@
+*** | | | +octopus-b
+*** | | |
+*** | * | commit COMMIT_OBJECT_NAME
+*** | |/ Author: A U Thor <author@example.com>
+*** | |
+*** | | octopus-a
+*** | | ---
+*** | | octopus-a.t | 1 +
+*** | | 1 file changed, 1 insertion(+)
+*** | |
+*** | | diff --git a/octopus-a.t b/octopus-a.t
+*** | | new file mode 100644
+*** | | index BEFORE..AFTER
+*** | | --- /dev/null
+*** | | +++ b/octopus-a.t
+*** | | @@ -0,0 +1 @@
+*** | | +octopus-a
+*** | |
+*** * | commit COMMIT_OBJECT_NAME
+*** |/ Author: A U Thor <author@example.com>
+*** |
+*** | seventh
+*** | ---
+*** | seventh.t | 1 +
+*** | 1 file changed, 1 insertion(+)
+*** |
+*** | diff --git a/seventh.t b/seventh.t
+*** | new file mode 100644
+*** | index BEFORE..AFTER
+*** | --- /dev/null
+*** | +++ b/seventh.t
+*** | @@ -0,0 +1 @@
+*** | +seventh
+*** |
+*** * commit COMMIT_OBJECT_NAME
+*** |\ Merge: MERGE_PARENTS
+*** | | Author: A U Thor <author@example.com>
+*** | |
+*** | | Merge branch 'tangle'
+*** | |
+*** | * commit COMMIT_OBJECT_NAME
+*** | |\ Merge: MERGE_PARENTS
+*** | | | Author: A U Thor <author@example.com>
+*** | | |
+*** | | | Merge branch 'side' (early part) into tangle
+*** | | |
+*** | * | commit COMMIT_OBJECT_NAME
+*** | |\ \ Merge: MERGE_PARENTS
+*** | | | | Author: A U Thor <author@example.com>
+*** | | | |
+*** | | | | Merge branch 'main' (early part) into tangle
+*** | | | |
+*** | * | | commit COMMIT_OBJECT_NAME
+*** | | | | Author: A U Thor <author@example.com>
+*** | | | |
+*** | | | | tangle-a
+*** | | | | ---
+*** | | | | tangle-a | 1 +
+*** | | | | 1 file changed, 1 insertion(+)
+*** | | | |
+*** | | | | diff --git a/tangle-a b/tangle-a
+*** | | | | new file mode 100644
+*** | | | | index BEFORE..AFTER
+*** | | | | --- /dev/null
+*** | | | | +++ b/tangle-a
+*** | | | | @@ -0,0 +1 @@
+*** | | | | +a
+*** | | | |
+*** * | | | commit COMMIT_OBJECT_NAME
+*** |\ \ \ \ Merge: MERGE_PARENTS
+*** | | | | | Author: A U Thor <author@example.com>
+*** | | | | |
+*** | | | | | Merge branch 'side'
+*** | | | | |
+*** | * | | | commit COMMIT_OBJECT_NAME
+*** | | |_|/ Author: A U Thor <author@example.com>
+*** | |/| |
+*** | | | | side-2
+*** | | | | ---
+*** | | | | 2 | 1 +
+*** | | | | 1 file changed, 1 insertion(+)
+*** | | | |
+*** | | | | diff --git a/2 b/2
+*** | | | | new file mode 100644
+*** | | | | index BEFORE..AFTER
+*** | | | | --- /dev/null
+*** | | | | +++ b/2
+*** | | | | @@ -0,0 +1 @@
+*** | | | | +2
+*** | | | |
+*** | * | | commit COMMIT_OBJECT_NAME
+*** | | | | Author: A U Thor <author@example.com>
+*** | | | |
+*** | | | | side-1
+*** | | | | ---
+*** | | | | 1 | 1 +
+*** | | | | 1 file changed, 1 insertion(+)
+*** | | | |
+*** | | | | diff --git a/1 b/1
+*** | | | | new file mode 100644
+*** | | | | index BEFORE..AFTER
+*** | | | | --- /dev/null
+*** | | | | +++ b/1
+*** | | | | @@ -0,0 +1 @@
+*** | | | | +1
+*** | | | |
+*** * | | | commit COMMIT_OBJECT_NAME
+*** | | | | Author: A U Thor <author@example.com>
+*** | | | |
+*** | | | | Second
+*** | | | | ---
+*** | | | | one | 1 +
+*** | | | | 1 file changed, 1 insertion(+)
+*** | | | |
+*** | | | | diff --git a/one b/one
+*** | | | | new file mode 100644
+*** | | | | index BEFORE..AFTER
+*** | | | | --- /dev/null
+*** | | | | +++ b/one
+*** | | | | @@ -0,0 +1 @@
+*** | | | | +case
+*** | | | |
+*** * | | | commit COMMIT_OBJECT_NAME
+*** | |_|/ Author: A U Thor <author@example.com>
+*** |/| |
+*** | | | sixth
+*** | | | ---
+*** | | | a/two | 1 -
+*** | | | 1 file changed, 1 deletion(-)
+*** | | |
+*** | | | diff --git a/a/two b/a/two
+*** | | | deleted file mode 100644
+*** | | | index BEFORE..AFTER
+*** | | | --- a/a/two
+*** | | | +++ /dev/null
+*** | | | @@ -1 +0,0 @@
+*** | | | -ni
+*** | | |
+*** * | | commit COMMIT_OBJECT_NAME
+*** | | | Author: A U Thor <author@example.com>
+*** | | |
+*** | | | fifth
+*** | | | ---
+*** | | | a/two | 1 +
+*** | | | 1 file changed, 1 insertion(+)
+*** | | |
+*** | | | diff --git a/a/two b/a/two
+*** | | | new file mode 100644
+*** | | | index BEFORE..AFTER
+*** | | | --- /dev/null
+*** | | | +++ b/a/two
+*** | | | @@ -0,0 +1 @@
+*** | | | +ni
+*** | | |
+*** * | | commit COMMIT_OBJECT_NAME
+*** |/ / Author: A U Thor <author@example.com>
+*** | |
+*** | | fourth
+*** | | ---
+*** | | ein | 1 +
+*** | | 1 file changed, 1 insertion(+)
+*** | |
+*** | | diff --git a/ein b/ein
+*** | | new file mode 100644
+*** | | index BEFORE..AFTER
+*** | | --- /dev/null
+*** | | +++ b/ein
+*** | | @@ -0,0 +1 @@
+*** | | +ichi
+*** | |
+*** * | commit COMMIT_OBJECT_NAME
+*** |/ Author: A U Thor <author@example.com>
+*** |
+*** | third
+*** | ---
+*** | ichi | 1 +
+*** | one | 1 -
+*** | 2 files changed, 1 insertion(+), 1 deletion(-)
+*** |
+*** | diff --git a/ichi b/ichi
+*** | new file mode 100644
+*** | index BEFORE..AFTER
+*** | --- /dev/null
+*** | +++ b/ichi
+*** | @@ -0,0 +1 @@
+*** | +ichi
+*** | diff --git a/one b/one
+*** | deleted file mode 100644
+*** | index BEFORE..AFTER
+*** | --- a/one
+*** | +++ /dev/null
+*** | @@ -1 +0,0 @@
+*** | -ichi
+*** |
+*** * commit COMMIT_OBJECT_NAME
+*** | Author: A U Thor <author@example.com>
+*** |
+*** | second
+*** | ---
+*** | one | 2 +-
+*** | 1 file changed, 1 insertion(+), 1 deletion(-)
+*** |
+*** | diff --git a/one b/one
+*** | index BEFORE..AFTER 100644
+*** | --- a/one
+*** | +++ b/one
+*** | @@ -1 +1 @@
+*** | -one
+*** | +ichi
+*** |
+*** * commit COMMIT_OBJECT_NAME
+*** Author: A U Thor <author@example.com>
+***
+*** initial
+*** ---
+*** one | 1 +
+*** 1 file changed, 1 insertion(+)
+***
+*** diff --git a/one b/one
+*** new file mode 100644
+*** index BEFORE..AFTER
+*** --- /dev/null
+*** +++ b/one
+*** @@ -0,0 +1 @@
+*** +one
+EOF
+
+test_expect_success 'log --line-prefix="*** " --graph with diff and stats' '
+ lib_test_cmp_short_graph --line-prefix="*** " --no-renames --stat -p
+'
+
+cat >expect <<-\EOF
+* reach
+|
+| A reach.t
+* Merge branch 'tangle'
+* Merge branch 'side'
+|\
+| * side-2
+|
+| A 2
+* Second
+|
+| A one
+* sixth
+
+ D a/two
+EOF
+
+test_expect_success 'log --graph with --name-status' '
+ test_cmp_graph --name-status tangle..reach
+'
+
+cat >expect <<-\EOF
+* reach
+|
+| reach.t
+* Merge branch 'tangle'
+* Merge branch 'side'
+|\
+| * side-2
+|
+| 2
+* Second
+|
+| one
+* sixth
+
+ a/two
+EOF
+
+test_expect_success 'log --graph with --name-only' '
+ test_cmp_graph --name-only tangle..reach
+'
+
+test_expect_success '--no-graph countermands --graph' '
+ git log >expect &&
+ git log --graph --no-graph >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--graph countermands --no-graph' '
+ git log --graph >expect &&
+ git log --no-graph --graph >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--no-graph does not unset --topo-order' '
+ git log --topo-order >expect &&
+ git log --topo-order --no-graph >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--no-graph does not unset --parents' '
+ git log --parents >expect &&
+ git log --parents --no-graph >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--reverse and --graph conflict' '
+ test_must_fail git log --reverse --graph 2>stderr &&
+ test_grep "cannot be used together" stderr
+'
+
+test_expect_success '--reverse --graph --no-graph works' '
+ git log --reverse >expect &&
+ git log --reverse --graph --no-graph >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--show-linear-break and --graph conflict' '
+ test_must_fail git log --show-linear-break --graph 2>stderr &&
+ test_grep "cannot be used together" stderr
+'
+
+test_expect_success '--show-linear-break --graph --no-graph works' '
+ git log --show-linear-break >expect &&
+ git log --show-linear-break --graph --no-graph >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--no-walk and --graph conflict' '
+ test_must_fail git log --no-walk --graph 2>stderr &&
+ test_grep "cannot be used together" stderr
+'
+
+test_expect_success '--no-walk --graph --no-graph works' '
+ git log --no-walk >expect &&
+ git log --no-walk --graph --no-graph >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--walk-reflogs and --graph conflict' '
+ test_must_fail git log --walk-reflogs --graph 2>stderr &&
+ (test_grep "cannot combine" stderr ||
+ test_grep "cannot be used together" stderr)
+'
+
+test_expect_success '--walk-reflogs --graph --no-graph works' '
+ git log --walk-reflogs >expect &&
+ git log --walk-reflogs --graph --no-graph >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'dotdot is a parent directory' '
+ mkdir -p a/b &&
+ ( echo sixth && echo fifth ) >expect &&
+ ( cd a/b && git log --format=%s .. ) >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success GPG 'setup signed branch' '
+ test_when_finished "git reset --hard && git checkout main" &&
+ git checkout -b signed main &&
+ echo foo >foo &&
+ git add foo &&
+ git commit -S -m signed_commit
+'
+
+test_expect_success GPG 'setup signed branch with subkey' '
+ test_when_finished "git reset --hard && git checkout main" &&
+ git checkout -b signed-subkey main &&
+ echo foo >foo &&
+ git add foo &&
+ git commit -SB7227189 -m signed_commit
+'
+
+test_expect_success GPGSM 'setup signed branch x509' '
+ test_when_finished "git reset --hard && git checkout main" &&
+ git checkout -b signed-x509 main &&
+ echo foo >foo &&
+ git add foo &&
+ test_config gpg.format x509 &&
+ test_config user.signingkey $GIT_COMMITTER_EMAIL &&
+ git commit -S -m signed_commit
+'
+
+test_expect_success GPGSSH 'setup sshkey signed branch' '
+ test_config gpg.format ssh &&
+ test_config user.signingkey "${GPGSSH_KEY_PRIMARY}" &&
+ test_when_finished "git reset --hard && git checkout main" &&
+ git checkout -b signed-ssh main &&
+ echo foo >foo &&
+ git add foo &&
+ git commit -S -m signed_commit
+'
+
+test_expect_success GPGSSH,GPGSSH_VERIFYTIME 'create signed commits with keys having defined lifetimes' '
+ test_config gpg.format ssh &&
+ touch file &&
+ git add file &&
+
+ echo expired >file && test_tick && git commit -a -m expired -S"${GPGSSH_KEY_EXPIRED}" &&
+ git tag expired-signed &&
+
+ echo notyetvalid >file && test_tick && git commit -a -m notyetvalid -S"${GPGSSH_KEY_NOTYETVALID}" &&
+ git tag notyetvalid-signed &&
+
+ echo timeboxedvalid >file && test_tick && git commit -a -m timeboxedvalid -S"${GPGSSH_KEY_TIMEBOXEDVALID}" &&
+ git tag timeboxedvalid-signed &&
+
+ echo timeboxedinvalid >file && test_tick && git commit -a -m timeboxedinvalid -S"${GPGSSH_KEY_TIMEBOXEDINVALID}" &&
+ git tag timeboxedinvalid-signed
+'
+
+test_expect_success GPGSM 'log x509 fingerprint' '
+ echo "F8BF62E0693D0694816377099909C779FA23FD65 | " >expect &&
+ git log -n1 --format="%GF | %GP" signed-x509 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success GPGSM 'log OpenPGP fingerprint' '
+ echo "D4BE22311AD3131E5EDA29A461092E85B7227189" > expect &&
+ git log -n1 --format="%GP" signed-subkey >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success GPGSSH 'log ssh key fingerprint' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ ssh-keygen -lf "${GPGSSH_KEY_PRIMARY}" | awk "{print \$2\" | \"}" >expect &&
+ git log -n1 --format="%GF | %GP" signed-ssh >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success GPG 'log --graph --show-signature' '
+ git log --graph --show-signature -n1 signed >actual &&
+ grep "^| gpg: Signature made" actual &&
+ grep "^| gpg: Good signature" actual
+'
+
+test_expect_success GPGSM 'log --graph --show-signature x509' '
+ git log --graph --show-signature -n1 signed-x509 >actual &&
+ grep "^| gpgsm: Signature made" actual &&
+ grep "^| gpgsm: Good signature" actual
+'
+
+test_expect_success GPGSSH 'log --graph --show-signature ssh' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ git log --graph --show-signature -n1 signed-ssh >actual &&
+ grep "${GOOD_SIGNATURE_TRUSTED}" actual
+'
+
+test_expect_success GPGSSH,GPGSSH_VERIFYTIME 'log shows failure on expired signature key' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ git log --graph --show-signature -n1 expired-signed >actual &&
+ ! grep "${GPGSSH_GOOD_SIGNATURE_TRUSTED}" actual
+'
+
+test_expect_success GPGSSH,GPGSSH_VERIFYTIME 'log shows failure on not yet valid signature key' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ git log --graph --show-signature -n1 notyetvalid-signed >actual &&
+ ! grep "${GPGSSH_GOOD_SIGNATURE_TRUSTED}" actual
+'
+
+test_expect_success GPGSSH,GPGSSH_VERIFYTIME 'log show success with commit date and key validity matching' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ git log --graph --show-signature -n1 timeboxedvalid-signed >actual &&
+ grep "${GPGSSH_GOOD_SIGNATURE_TRUSTED}" actual &&
+ ! grep "${GPGSSH_BAD_SIGNATURE}" actual
+'
+
+test_expect_success GPGSSH,GPGSSH_VERIFYTIME 'log shows failure with commit date outside of key validity' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ git log --graph --show-signature -n1 timeboxedinvalid-signed >actual &&
+ ! grep "${GPGSSH_GOOD_SIGNATURE_TRUSTED}" actual
+'
+
+test_expect_success GPG 'log --graph --show-signature for merged tag' '
+ test_when_finished "git reset --hard && git checkout main" &&
+ git checkout -b plain main &&
+ echo aaa >bar &&
+ git add bar &&
+ git commit -m bar_commit &&
+ git checkout -b tagged main &&
+ echo bbb >baz &&
+ git add baz &&
+ git commit -m baz_commit &&
+ git tag -s -m signed_tag_msg signed_tag &&
+ git checkout plain &&
+ git merge --no-ff -m msg signed_tag &&
+ git log --graph --show-signature -n1 plain >actual &&
+ grep "^|\\\ merged tag" actual &&
+ grep "^| | gpg: Signature made" actual &&
+ grep "^| | gpg: Good signature" actual
+'
+
+test_expect_success GPG 'log --graph --show-signature for merged tag in shallow clone' '
+ test_when_finished "git reset --hard && git checkout main" &&
+ git checkout -b plain-shallow main &&
+ echo aaa >bar &&
+ git add bar &&
+ git commit -m bar_commit &&
+ git checkout --detach main &&
+ echo bbb >baz &&
+ git add baz &&
+ git commit -m baz_commit &&
+ git tag -s -m signed_tag_msg signed_tag_shallow &&
+ hash=$(git rev-parse HEAD) &&
+ git checkout plain-shallow &&
+ git merge --no-ff -m msg signed_tag_shallow &&
+ git clone --depth 1 --no-local . shallow &&
+ test_when_finished "rm -rf shallow" &&
+ git -C shallow log --graph --show-signature -n1 plain-shallow >actual &&
+ grep "tag signed_tag_shallow names a non-parent $hash" actual
+'
+
+test_expect_success GPG 'log --graph --show-signature for merged tag with missing key' '
+ test_when_finished "git reset --hard && git checkout main" &&
+ git checkout -b plain-nokey main &&
+ echo aaa >bar &&
+ git add bar &&
+ git commit -m bar_commit &&
+ git checkout -b tagged-nokey main &&
+ echo bbb >baz &&
+ git add baz &&
+ git commit -m baz_commit &&
+ git tag -s -m signed_tag_msg signed_tag_nokey &&
+ git checkout plain-nokey &&
+ git merge --no-ff -m msg signed_tag_nokey &&
+ GNUPGHOME=. git log --graph --show-signature -n1 plain-nokey >actual &&
+ grep "^|\\\ merged tag" actual &&
+ grep "^| | gpg: Signature made" actual &&
+ grep -E "^| | gpg: Can'"'"'t check signature: (public key not found|No public key)" actual
+'
+
+test_expect_success GPG 'log --graph --show-signature for merged tag with bad signature' '
+ test_when_finished "git reset --hard && git checkout main" &&
+ git checkout -b plain-bad main &&
+ echo aaa >bar &&
+ git add bar &&
+ git commit -m bar_commit &&
+ git checkout -b tagged-bad main &&
+ echo bbb >baz &&
+ git add baz &&
+ git commit -m baz_commit &&
+ git tag -s -m signed_tag_msg signed_tag_bad &&
+ git cat-file tag signed_tag_bad >raw &&
+ sed -e "s/signed_tag_msg/forged/" raw >forged &&
+ git hash-object -w -t tag forged >forged.tag &&
+ git checkout plain-bad &&
+ git merge --no-ff -m msg "$(cat forged.tag)" &&
+ git log --graph --show-signature -n1 plain-bad >actual &&
+ grep "^|\\\ merged tag" actual &&
+ grep "^| | gpg: Signature made" actual &&
+ grep "^| | gpg: BAD signature from" actual
+'
+
+test_expect_success GPG 'log --show-signature for merged tag with GPG failure' '
+ test_when_finished "git reset --hard && git checkout main" &&
+ git checkout -b plain-fail main &&
+ echo aaa >bar &&
+ git add bar &&
+ git commit -m bar_commit &&
+ git checkout -b tagged-fail main &&
+ echo bbb >baz &&
+ git add baz &&
+ git commit -m baz_commit &&
+ git tag -s -m signed_tag_msg signed_tag_fail &&
+ git checkout plain-fail &&
+ git merge --no-ff -m msg signed_tag_fail &&
+ if ! test_have_prereq VALGRIND
+ then
+ TMPDIR="$(pwd)/bogus" git log --show-signature -n1 plain-fail >actual &&
+ grep "^merged tag" actual &&
+ grep "^No signature" actual &&
+ ! grep "^gpg: Signature made" actual
+ fi
+'
+
+test_expect_success GPGSM 'log --graph --show-signature for merged tag x509' '
+ test_when_finished "git reset --hard && git checkout main" &&
+ test_config gpg.format x509 &&
+ test_config user.signingkey $GIT_COMMITTER_EMAIL &&
+ git checkout -b plain-x509 main &&
+ echo aaa >bar &&
+ git add bar &&
+ git commit -m bar_commit &&
+ git checkout -b tagged-x509 main &&
+ echo bbb >baz &&
+ git add baz &&
+ git commit -m baz_commit &&
+ git tag -s -m signed_tag_msg signed_tag_x509 &&
+ git checkout plain-x509 &&
+ git merge --no-ff -m msg signed_tag_x509 &&
+ git log --graph --show-signature -n1 plain-x509 >actual &&
+ grep "^|\\\ merged tag" actual &&
+ grep "^| | gpgsm: Signature made" actual &&
+ grep "^| | gpgsm: Good signature" actual
+'
+
+test_expect_success GPGSM 'log --graph --show-signature for merged tag x509 missing key' '
+ test_when_finished "git reset --hard && git checkout main" &&
+ test_config gpg.format x509 &&
+ test_config user.signingkey $GIT_COMMITTER_EMAIL &&
+ git checkout -b plain-x509-nokey main &&
+ echo aaa >bar &&
+ git add bar &&
+ git commit -m bar_commit &&
+ git checkout -b tagged-x509-nokey main &&
+ echo bbb >baz &&
+ git add baz &&
+ git commit -m baz_commit &&
+ git tag -s -m signed_tag_msg signed_tag_x509_nokey &&
+ git checkout plain-x509-nokey &&
+ git merge --no-ff -m msg signed_tag_x509_nokey &&
+ GNUPGHOME=. git log --graph --show-signature -n1 plain-x509-nokey >actual &&
+ grep "^|\\\ merged tag" actual &&
+ grep -e "^| | gpgsm: certificate not found" \
+ -e "^| | gpgsm: failed to find the certificate: Not found" actual
+'
+
+test_expect_success GPGSM 'log --graph --show-signature for merged tag x509 bad signature' '
+ test_when_finished "git reset --hard && git checkout main" &&
+ test_config gpg.format x509 &&
+ test_config user.signingkey $GIT_COMMITTER_EMAIL &&
+ git checkout -b plain-x509-bad main &&
+ echo aaa >bar &&
+ git add bar &&
+ git commit -m bar_commit &&
+ git checkout -b tagged-x509-bad main &&
+ echo bbb >baz &&
+ git add baz &&
+ git commit -m baz_commit &&
+ git tag -s -m signed_tag_msg signed_tag_x509_bad &&
+ git cat-file tag signed_tag_x509_bad >raw &&
+ sed -e "s/signed_tag_msg/forged/" raw >forged &&
+ git hash-object -w -t tag forged >forged.tag &&
+ git checkout plain-x509-bad &&
+ git merge --no-ff -m msg "$(cat forged.tag)" &&
+ git log --graph --show-signature -n1 plain-x509-bad >actual &&
+ grep "^|\\\ merged tag" actual &&
+ grep "^| | gpgsm: Signature made" actual &&
+ grep "^| | gpgsm: invalid signature" actual
+'
+
+
+test_expect_success GPG '--no-show-signature overrides --show-signature' '
+ git log -1 --show-signature --no-show-signature signed >actual &&
+ ! grep "^gpg:" actual
+'
+
+test_expect_success GPG 'log.showsignature=true behaves like --show-signature' '
+ test_config log.showsignature true &&
+ git log -1 signed >actual &&
+ grep "gpg: Signature made" actual &&
+ grep "gpg: Good signature" actual
+'
+
+test_expect_success GPG '--no-show-signature overrides log.showsignature=true' '
+ test_config log.showsignature true &&
+ git log -1 --no-show-signature signed >actual &&
+ ! grep "^gpg:" actual
+'
+
+test_expect_success GPG '--show-signature overrides log.showsignature=false' '
+ test_config log.showsignature false &&
+ git log -1 --show-signature signed >actual &&
+ grep "gpg: Signature made" actual &&
+ grep "gpg: Good signature" actual
+'
+
+test_expect_success 'log --graph --no-walk is forbidden' '
+ test_must_fail git log --graph --no-walk
+'
+
+test_expect_success 'log on empty repo fails' '
+ git init empty &&
+ test_when_finished "rm -rf empty" &&
+ test_must_fail git -C empty log 2>stderr &&
+ test_grep does.not.have.any.commits stderr
+'
+
+test_expect_success REFFILES 'log diagnoses bogus HEAD hash' '
+ git init empty &&
+ test_when_finished "rm -rf empty" &&
+ echo 1234abcd >empty/.git/refs/heads/main &&
+ test_must_fail git -C empty log 2>stderr &&
+ test_grep broken stderr
+'
+
+test_expect_success REFFILES 'log diagnoses bogus HEAD symref' '
+ git init empty &&
+ echo "ref: refs/heads/invalid.lock" > empty/.git/HEAD &&
+ test_must_fail git -C empty log 2>stderr &&
+ test_grep broken stderr &&
+ test_must_fail git -C empty log --default totally-bogus 2>stderr &&
+ test_grep broken stderr
+'
+
+test_expect_success 'log does not default to HEAD when rev input is given' '
+ git log --branches=does-not-exist >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'do not default to HEAD with ignored object on cmdline' '
+ git log --ignore-missing $ZERO_OID >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'do not default to HEAD with ignored object on stdin' '
+ echo $ZERO_OID | git log --ignore-missing --stdin >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'set up --source tests' '
+ git checkout --orphan source-a &&
+ test_commit one &&
+ test_commit two &&
+ git checkout -b source-b HEAD^ &&
+ test_commit three
+'
+
+test_expect_success 'log --source paints branch names' '
+ cat >expect <<-EOF &&
+ $(git rev-parse --short :/three) source-b three
+ $(git rev-parse --short :/two ) source-a two
+ $(git rev-parse --short :/one ) source-b one
+ EOF
+ git log --oneline --source source-a source-b >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log --source paints tag names' '
+ git tag -m tagged source-tag &&
+ cat >expect <<-EOF &&
+ $(git rev-parse --short :/three) source-tag three
+ $(git rev-parse --short :/two ) source-a two
+ $(git rev-parse --short :/one ) source-tag one
+ EOF
+ git log --oneline --source source-tag source-a >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log --source paints symmetric ranges' '
+ cat >expect <<-EOF &&
+ $(git rev-parse --short :/three) source-b three
+ $(git rev-parse --short :/two ) source-a two
+ EOF
+ git log --oneline --source source-a...source-b >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--exclude-promisor-objects does not BUG-crash' '
+ test_must_fail git log --exclude-promisor-objects source-a
+'
+
+test_expect_success 'log --decorate includes all levels of tag annotated tags' '
+ git checkout -b branch &&
+ git commit --allow-empty -m "new commit" &&
+ git tag lightweight HEAD &&
+ git tag -m annotated annotated HEAD &&
+ git tag -m double-0 double-0 HEAD &&
+ git tag -m double-1 double-1 double-0 &&
+ cat >expect <<-\EOF &&
+ HEAD -> branch, tag: lightweight, tag: double-1, tag: double-0, tag: annotated
+ EOF
+ git log -1 --format="%D" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log --decorate does not include things outside filter' '
+ reflist="refs/prefetch refs/rebase-merge refs/bundle" &&
+
+ for ref in $reflist
+ do
+ git update-ref $ref/fake HEAD || return 1
+ done &&
+
+ git log --decorate=full --oneline >actual &&
+
+ # None of the refs are visible:
+ ! grep /fake actual
+'
+
+test_expect_success 'log --end-of-options' '
+ git update-ref refs/heads/--source HEAD &&
+ git log --end-of-options --source >actual &&
+ git log >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'set up commits with different authors' '
+ git checkout --orphan authors &&
+ test_commit --author "Jim <jim@example.com>" jim_1 &&
+ test_commit --author "Val <val@example.com>" val_1 &&
+ test_commit --author "Val <val@example.com>" val_2 &&
+ test_commit --author "Jim <jim@example.com>" jim_2 &&
+ test_commit --author "Val <val@example.com>" val_3 &&
+ test_commit --author "Jim <jim@example.com>" jim_3
+'
+
+test_expect_success 'log --invert-grep --grep --author' '
+ cat >expect <<-\EOF &&
+ val_3
+ val_1
+ EOF
+ git log --format=%s --author=Val --grep 2 --invert-grep >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t4203-mailmap.sh b/t/t4203-mailmap.sh
new file mode 100755
index 0000000..8a88dd7
--- /dev/null
+++ b/t/t4203-mailmap.sh
@@ -0,0 +1,1090 @@
+#!/bin/sh
+
+test_description='.mailmap configurations'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success 'setup commits and contacts file' '
+ test_commit initial one one &&
+ test_commit --author "nick1 <bugs@company.xx>" --append second one two
+'
+
+test_expect_success 'check-mailmap no arguments' '
+ test_must_fail git check-mailmap
+'
+
+test_expect_success 'check-mailmap arguments' '
+ cat >expect <<-EOF &&
+ $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>
+ nick1 <bugs@company.xx>
+ EOF
+ git check-mailmap \
+ "$GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>" \
+ "nick1 <bugs@company.xx>" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'check-mailmap --stdin' '
+ cat >expect <<-EOF &&
+ $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>
+ nick1 <bugs@company.xx>
+ EOF
+ git check-mailmap --stdin <expect >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'check-mailmap --stdin arguments: no mapping' '
+ test_when_finished "rm contacts" &&
+ cat >contacts <<-EOF &&
+ $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>
+ nick1 <bugs@company.xx>
+ EOF
+ cat >expect <<-\EOF &&
+ Internal Guy <bugs@company.xy>
+ EOF
+ cat contacts >>expect &&
+
+ git check-mailmap --stdin "Internal Guy <bugs@company.xy>" \
+ <contacts >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'check-mailmap --stdin arguments: mapping' '
+ test_when_finished "rm .mailmap" &&
+ cat >.mailmap <<-EOF &&
+ New Name <$GIT_AUTHOR_EMAIL>
+ EOF
+ cat >stdin <<-EOF &&
+ Old Name <$GIT_AUTHOR_EMAIL>
+ EOF
+
+ cp .mailmap expect &&
+ git check-mailmap --stdin <stdin >actual &&
+ test_cmp expect actual &&
+
+ cat .mailmap >>expect &&
+ git check-mailmap --stdin "Another Old Name <$GIT_AUTHOR_EMAIL>" \
+ <stdin >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'check-mailmap bogus contact' '
+ test_must_fail git check-mailmap bogus
+'
+
+test_expect_success 'check-mailmap bogus contact --stdin' '
+ test_must_fail git check-mailmap --stdin bogus </dev/null
+'
+
+test_expect_success 'No mailmap' '
+ cat >expect <<-EOF &&
+ $GIT_AUTHOR_NAME (1):
+ initial
+
+ nick1 (1):
+ second
+
+ EOF
+ git shortlog HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'setup default .mailmap' '
+ cat >default.map <<-EOF
+ Repo Guy <$GIT_AUTHOR_EMAIL>
+ EOF
+'
+
+test_expect_success 'test default .mailmap' '
+ test_when_finished "rm .mailmap" &&
+ cp default.map .mailmap &&
+
+ cat >expect <<-\EOF &&
+ Repo Guy (1):
+ initial
+
+ nick1 (1):
+ second
+
+ EOF
+ git shortlog HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'mailmap.file set' '
+ test_when_finished "rm .mailmap" &&
+ cp default.map .mailmap &&
+
+ test_config mailmap.file internal.map &&
+ cat >internal.map <<-\EOF &&
+ Internal Guy <bugs@company.xx>
+ EOF
+
+ cat >expect <<-\EOF &&
+ Internal Guy (1):
+ second
+
+ Repo Guy (1):
+ initial
+
+ EOF
+ git shortlog HEAD >actual &&
+ test_cmp expect actual &&
+
+ # The internal_mailmap/.mailmap file is an a subdirectory, but
+ # as shown here it can also be outside the repository
+ test_when_finished "rm -rf sub-repo" &&
+ git clone . sub-repo &&
+ (
+ cd sub-repo &&
+ cp ../.mailmap . &&
+ git config mailmap.file ../internal.map &&
+ git shortlog HEAD >actual &&
+ test_cmp ../expect actual
+ )
+'
+
+test_expect_success 'mailmap.file override' '
+ test_config mailmap.file internal.map &&
+ cat >internal.map <<-EOF &&
+ Internal Guy <bugs@company.xx>
+ External Guy <$GIT_AUTHOR_EMAIL>
+ EOF
+
+ cat >expect <<-\EOF &&
+ External Guy (1):
+ initial
+
+ Internal Guy (1):
+ second
+
+ EOF
+ git shortlog HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'mailmap.file non-existent' '
+ test_when_finished "rm .mailmap" &&
+ cp default.map .mailmap &&
+
+ cat >expect <<-\EOF &&
+ Repo Guy (1):
+ initial
+
+ nick1 (1):
+ second
+
+ EOF
+ git shortlog HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'name entry after email entry' '
+ test_when_finished "rm .mailmap" &&
+ cp default.map .mailmap &&
+
+ test_config mailmap.file internal.map &&
+ cat >internal.map <<-\EOF &&
+ <bugs@company.xy> <bugs@company.xx>
+ Internal Guy <bugs@company.xx>
+ EOF
+
+ cat >expect <<-\EOF &&
+ Internal Guy (1):
+ second
+
+ Repo Guy (1):
+ initial
+
+ EOF
+
+ git shortlog HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'name entry after email entry, case-insensitive' '
+ test_when_finished "rm .mailmap" &&
+ cp default.map .mailmap &&
+
+ test_config mailmap.file internal.map &&
+ cat >internal.map <<-\EOF &&
+ <bugs@company.xy> <bugs@company.xx>
+ Internal Guy <BUGS@Company.xx>
+ EOF
+
+ cat >expect <<-\EOF &&
+ Internal Guy (1):
+ second
+
+ Repo Guy (1):
+ initial
+
+ EOF
+ git shortlog HEAD >actual &&
+ test_cmp expect actual &&
+
+ cat >internal.map <<-\EOF &&
+ NiCk <BuGs@CoMpAnY.Xy> NICK1 <BUGS@COMPANY.XX>
+ EOF
+
+ cat >expect <<-\EOF &&
+ NiCk (1):
+ second
+
+ Repo Guy (1):
+ initial
+
+ EOF
+ git shortlog HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'No mailmap files, but configured' '
+ cat >expect <<-EOF &&
+ $GIT_AUTHOR_NAME (1):
+ initial
+
+ nick1 (1):
+ second
+
+ EOF
+ git shortlog HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'setup mailmap blob tests' '
+ git checkout -b map &&
+ test_when_finished "git checkout main" &&
+ cat >just-bugs <<-\EOF &&
+ Blob Guy <bugs@company.xx>
+ EOF
+ cat >both <<-EOF &&
+ Blob Guy <$GIT_AUTHOR_EMAIL>
+ Blob Guy <bugs@company.xx>
+ EOF
+ printf "Tricky Guy <$GIT_AUTHOR_EMAIL>" >no-newline &&
+ git add just-bugs both no-newline &&
+ git commit -m "my mailmaps" &&
+
+ cat >internal.map <<-EOF
+ Internal Guy <$GIT_AUTHOR_EMAIL>
+ EOF
+'
+
+test_expect_success 'mailmap.blob set' '
+ test_when_finished "rm .mailmap" &&
+ cp default.map .mailmap &&
+
+ cat >expect <<-\EOF &&
+ Blob Guy (1):
+ second
+
+ Repo Guy (1):
+ initial
+
+ EOF
+ git -c mailmap.blob=map:just-bugs shortlog HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'mailmap.blob overrides .mailmap' '
+ test_when_finished "rm .mailmap" &&
+ cp default.map .mailmap &&
+
+ cat >expect <<-\EOF &&
+ Blob Guy (2):
+ initial
+ second
+
+ EOF
+ git -c mailmap.blob=map:both shortlog HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'mailmap.file overrides mailmap.blob' '
+ cat >expect <<-\EOF &&
+ Blob Guy (1):
+ second
+
+ Internal Guy (1):
+ initial
+
+ EOF
+ git \
+ -c mailmap.blob=map:both \
+ -c mailmap.file=internal.map \
+ shortlog HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'mailmap.file can be missing' '
+ test_when_finished "rm .mailmap" &&
+ cp default.map .mailmap &&
+
+ test_config mailmap.file nonexistent &&
+ cat >expect <<-\EOF &&
+ Repo Guy (1):
+ initial
+
+ nick1 (1):
+ second
+
+ EOF
+ git shortlog HEAD >actual 2>err &&
+ test_must_be_empty err &&
+ test_cmp expect actual
+'
+
+test_expect_success 'mailmap.blob can be missing' '
+ test_when_finished "rm .mailmap" &&
+ cp default.map .mailmap &&
+
+ cat >expect <<-\EOF &&
+ Repo Guy (1):
+ initial
+
+ nick1 (1):
+ second
+
+ EOF
+ git -c mailmap.blob=map:nonexistent shortlog HEAD >actual 2>err &&
+ test_must_be_empty err &&
+ test_cmp expect actual
+'
+
+test_expect_success 'mailmap.blob might be the wrong type' '
+ test_when_finished "rm .mailmap" &&
+ cp default.map .mailmap &&
+
+ git -c mailmap.blob=HEAD: shortlog HEAD >actual 2>err &&
+ test_grep "mailmap is not a blob" err &&
+ test_cmp expect actual
+'
+
+test_expect_success 'mailmap.blob defaults to off in non-bare repo' '
+ git init non-bare &&
+ (
+ cd non-bare &&
+ test_commit one .mailmap "Fake Name <$GIT_AUTHOR_EMAIL>" &&
+ cat >expect <<-\EOF &&
+ 1 Fake Name
+ EOF
+ git shortlog -ns HEAD >actual &&
+ test_cmp expect actual &&
+ rm .mailmap &&
+ cat >expect <<-EOF &&
+ 1 $GIT_AUTHOR_NAME
+ EOF
+ git shortlog -ns HEAD >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'mailmap.blob defaults to HEAD:.mailmap in bare repo' '
+ git clone --bare non-bare bare &&
+ (
+ cd bare &&
+ cat >expect <<-\EOF &&
+ 1 Fake Name
+ EOF
+ git shortlog -ns HEAD >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'mailmap.blob can handle blobs without trailing newline' '
+ cat >expect <<-\EOF &&
+ Tricky Guy (1):
+ initial
+
+ nick1 (1):
+ second
+
+ EOF
+ git -c mailmap.blob=map:no-newline shortlog HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'single-character name' '
+ test_when_finished "rm .mailmap" &&
+ cat >.mailmap <<-EOF &&
+ A <$GIT_AUTHOR_EMAIL>
+ EOF
+
+ cat >expect <<-EOF &&
+ 1 A <$GIT_AUTHOR_EMAIL>
+ 1 nick1 <bugs@company.xx>
+ EOF
+ git shortlog -es HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'preserve canonical email case' '
+ test_when_finished "rm .mailmap" &&
+ cat >.mailmap <<-EOF &&
+ <AUTHOR@example.com> <$GIT_AUTHOR_EMAIL>
+ EOF
+
+ cat >expect <<-EOF &&
+ 1 $GIT_AUTHOR_NAME <AUTHOR@example.com>
+ 1 nick1 <bugs@company.xx>
+ EOF
+ git shortlog -es HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'gitmailmap(5) example output: setup' '
+ test_create_repo doc &&
+ test_commit -C doc --author "Joe Developer <joe@example.com>" A &&
+ test_commit -C doc --author "Joe R. Developer <joe@example.com>" B &&
+ test_commit -C doc --author "Jane Doe <jane@example.com>" C &&
+ test_commit -C doc --author "Jane Doe <jane@laptop.(none)>" D &&
+ test_commit -C doc --author "Jane D. <jane@desktop.(none)>" E
+'
+
+test_expect_success 'gitmailmap(5) example output: example #1' '
+ test_config -C doc mailmap.file ../doc.map &&
+ cat >doc.map <<-\EOF &&
+ Joe R. Developer <joe@example.com>
+ Jane Doe <jane@example.com>
+ Jane Doe <jane@desktop.(none)>
+ EOF
+
+ cat >expect <<-\EOF &&
+ Author Joe Developer <joe@example.com> maps to Joe R. Developer <joe@example.com>
+ Committer C O Mitter <committer@example.com> maps to C O Mitter <committer@example.com>
+
+ Author Joe R. Developer <joe@example.com> maps to Joe R. Developer <joe@example.com>
+ Committer C O Mitter <committer@example.com> maps to C O Mitter <committer@example.com>
+
+ Author Jane Doe <jane@example.com> maps to Jane Doe <jane@example.com>
+ Committer C O Mitter <committer@example.com> maps to C O Mitter <committer@example.com>
+
+ Author Jane Doe <jane@laptop.(none)> maps to Jane Doe <jane@laptop.(none)>
+ Committer C O Mitter <committer@example.com> maps to C O Mitter <committer@example.com>
+
+ Author Jane D. <jane@desktop.(none)> maps to Jane Doe <jane@desktop.(none)>
+ Committer C O Mitter <committer@example.com> maps to C O Mitter <committer@example.com>
+ EOF
+ git -C doc log --reverse --pretty=format:"Author %an <%ae> maps to %aN <%aE>%nCommitter %cn <%ce> maps to %cN <%cE>%n" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'gitmailmap(5) example output: example #2' '
+ test_config -C doc mailmap.file ../doc.map &&
+ cat >doc.map <<-\EOF &&
+ Joe R. Developer <joe@example.com>
+ Jane Doe <jane@example.com> <jane@laptop.(none)>
+ Jane Doe <jane@example.com> <jane@desktop.(none)>
+ EOF
+
+ cat >expect <<-\EOF &&
+ Author Joe Developer <joe@example.com> maps to Joe R. Developer <joe@example.com>
+ Committer C O Mitter <committer@example.com> maps to C O Mitter <committer@example.com>
+
+ Author Joe R. Developer <joe@example.com> maps to Joe R. Developer <joe@example.com>
+ Committer C O Mitter <committer@example.com> maps to C O Mitter <committer@example.com>
+
+ Author Jane Doe <jane@example.com> maps to Jane Doe <jane@example.com>
+ Committer C O Mitter <committer@example.com> maps to C O Mitter <committer@example.com>
+
+ Author Jane Doe <jane@laptop.(none)> maps to Jane Doe <jane@example.com>
+ Committer C O Mitter <committer@example.com> maps to C O Mitter <committer@example.com>
+
+ Author Jane D. <jane@desktop.(none)> maps to Jane Doe <jane@example.com>
+ Committer C O Mitter <committer@example.com> maps to C O Mitter <committer@example.com>
+ EOF
+ git -C doc log --reverse --pretty=format:"Author %an <%ae> maps to %aN <%aE>%nCommitter %cn <%ce> maps to %cN <%cE>%n" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'gitmailmap(5) example output: example #3' '
+ test_config -C doc mailmap.file ../doc.map &&
+ cat >>doc.map <<-\EOF &&
+ Joe R. Developer <joe@example.com> Joe <bugs@example.com>
+ Jane Doe <jane@example.com> Jane <bugs@example.com>
+ EOF
+
+ test_commit -C doc --author "Joe <bugs@example.com>" F &&
+ test_commit -C doc --author "Jane <bugs@example.com>" G &&
+
+ cat >>expect <<-\EOF &&
+
+ Author Joe <bugs@example.com> maps to Joe R. Developer <joe@example.com>
+ Committer C O Mitter <committer@example.com> maps to C O Mitter <committer@example.com>
+
+ Author Jane <bugs@example.com> maps to Jane Doe <jane@example.com>
+ Committer C O Mitter <committer@example.com> maps to C O Mitter <committer@example.com>
+ EOF
+ git -C doc log --reverse --pretty=format:"Author %an <%ae> maps to %aN <%aE>%nCommitter %cn <%ce> maps to %cN <%cE>%n" >actual &&
+ test_cmp expect actual
+'
+
+
+test_expect_success 'Shortlog output (complex mapping)' '
+ test_config mailmap.file complex.map &&
+ cat >complex.map <<-EOF &&
+ Committed <$GIT_COMMITTER_EMAIL>
+ <cto@company.xx> <cto@coompany.xx>
+ Some Dude <some@dude.xx> nick1 <bugs@company.xx>
+ Other Author <other@author.xx> nick2 <bugs@company.xx>
+ Other Author <other@author.xx> <nick2@company.xx>
+ Santa Claus <santa.claus@northpole.xx> <me@company.xx>
+ EOF
+
+ test_commit --author "nick2 <bugs@company.xx>" --append third one three &&
+ test_commit --author "nick2 <nick2@company.xx>" --append fourth one four &&
+ test_commit --author "santa <me@company.xx>" --append fifth one five &&
+ test_commit --author "claus <me@company.xx>" --append sixth one six &&
+ test_commit --author "CTO <cto@coompany.xx>" --append seventh one seven &&
+
+ cat >expect <<-EOF &&
+ $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> (1):
+ initial
+
+ CTO <cto@company.xx> (1):
+ seventh
+
+ Other Author <other@author.xx> (2):
+ third
+ fourth
+
+ Santa Claus <santa.claus@northpole.xx> (2):
+ fifth
+ sixth
+
+ Some Dude <some@dude.xx> (1):
+ second
+
+ EOF
+
+ git shortlog -e HEAD >actual &&
+ test_cmp expect actual
+
+'
+
+test_expect_success 'Log output (complex mapping)' '
+ test_config mailmap.file complex.map &&
+
+ cat >expect <<-EOF &&
+ Author CTO <cto@coompany.xx> maps to CTO <cto@company.xx>
+ Committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> maps to Committed <$GIT_COMMITTER_EMAIL>
+
+ Author claus <me@company.xx> maps to Santa Claus <santa.claus@northpole.xx>
+ Committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> maps to Committed <$GIT_COMMITTER_EMAIL>
+
+ Author santa <me@company.xx> maps to Santa Claus <santa.claus@northpole.xx>
+ Committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> maps to Committed <$GIT_COMMITTER_EMAIL>
+
+ Author nick2 <nick2@company.xx> maps to Other Author <other@author.xx>
+ Committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> maps to Committed <$GIT_COMMITTER_EMAIL>
+
+ Author nick2 <bugs@company.xx> maps to Other Author <other@author.xx>
+ Committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> maps to Committed <$GIT_COMMITTER_EMAIL>
+
+ Author nick1 <bugs@company.xx> maps to Some Dude <some@dude.xx>
+ Committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> maps to Committed <$GIT_COMMITTER_EMAIL>
+
+ Author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> maps to $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>
+ Committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> maps to Committed <$GIT_COMMITTER_EMAIL>
+ EOF
+
+ git log --pretty=format:"Author %an <%ae> maps to %aN <%aE>%nCommitter %cn <%ce> maps to %cN <%cE>%n" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Log output (local-part email address)' '
+ cat >expect <<-EOF &&
+ Author email cto@coompany.xx has local-part cto
+ Committer email $GIT_COMMITTER_EMAIL has local-part $TEST_COMMITTER_LOCALNAME
+
+ Author email me@company.xx has local-part me
+ Committer email $GIT_COMMITTER_EMAIL has local-part $TEST_COMMITTER_LOCALNAME
+
+ Author email me@company.xx has local-part me
+ Committer email $GIT_COMMITTER_EMAIL has local-part $TEST_COMMITTER_LOCALNAME
+
+ Author email nick2@company.xx has local-part nick2
+ Committer email $GIT_COMMITTER_EMAIL has local-part $TEST_COMMITTER_LOCALNAME
+
+ Author email bugs@company.xx has local-part bugs
+ Committer email $GIT_COMMITTER_EMAIL has local-part $TEST_COMMITTER_LOCALNAME
+
+ Author email bugs@company.xx has local-part bugs
+ Committer email $GIT_COMMITTER_EMAIL has local-part $TEST_COMMITTER_LOCALNAME
+
+ Author email author@example.com has local-part author
+ Committer email $GIT_COMMITTER_EMAIL has local-part $TEST_COMMITTER_LOCALNAME
+ EOF
+
+ git log --pretty=format:"Author email %ae has local-part %al%nCommitter email %ce has local-part %cl%n" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Log output with --use-mailmap' '
+ test_config mailmap.file complex.map &&
+
+ cat >expect <<-EOF &&
+ Author: CTO <cto@company.xx>
+ Author: Santa Claus <santa.claus@northpole.xx>
+ Author: Santa Claus <santa.claus@northpole.xx>
+ Author: Other Author <other@author.xx>
+ Author: Other Author <other@author.xx>
+ Author: Some Dude <some@dude.xx>
+ Author: $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>
+ EOF
+
+ git log --use-mailmap >log &&
+ grep Author log >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Log output with log.mailmap' '
+ test_config mailmap.file complex.map &&
+
+ cat >expect <<-EOF &&
+ Author: CTO <cto@company.xx>
+ Author: Santa Claus <santa.claus@northpole.xx>
+ Author: Santa Claus <santa.claus@northpole.xx>
+ Author: Other Author <other@author.xx>
+ Author: Other Author <other@author.xx>
+ Author: Some Dude <some@dude.xx>
+ Author: $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>
+ EOF
+
+ git -c log.mailmap=True log >log &&
+ grep Author log >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log.mailmap=false disables mailmap' '
+ cat >expect <<-EOF &&
+ Author: CTO <cto@coompany.xx>
+ Author: claus <me@company.xx>
+ Author: santa <me@company.xx>
+ Author: nick2 <nick2@company.xx>
+ Author: nick2 <bugs@company.xx>
+ Author: nick1 <bugs@company.xx>
+ Author: $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>
+ EOF
+ git -c log.mailmap=false log >log &&
+ grep Author log >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--no-use-mailmap disables mailmap' '
+ cat >expect <<-EOF &&
+ Author: CTO <cto@coompany.xx>
+ Author: claus <me@company.xx>
+ Author: santa <me@company.xx>
+ Author: nick2 <nick2@company.xx>
+ Author: nick2 <bugs@company.xx>
+ Author: nick1 <bugs@company.xx>
+ Author: $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>
+ EOF
+ git log --no-use-mailmap >log &&
+ grep Author log >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Grep author with --use-mailmap' '
+ test_config mailmap.file complex.map &&
+
+ cat >expect <<-\EOF &&
+ Author: Santa Claus <santa.claus@northpole.xx>
+ Author: Santa Claus <santa.claus@northpole.xx>
+ EOF
+ git log --use-mailmap --author Santa >log &&
+ grep Author log >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Grep author with log.mailmap' '
+ test_config mailmap.file complex.map &&
+
+ cat >expect <<-\EOF &&
+ Author: Santa Claus <santa.claus@northpole.xx>
+ Author: Santa Claus <santa.claus@northpole.xx>
+ EOF
+
+ git -c log.mailmap=True log --author Santa >log &&
+ grep Author log >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log.mailmap is true by default these days' '
+ test_config mailmap.file complex.map &&
+ git log --author Santa >log &&
+ grep Author log >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Only grep replaced author with --use-mailmap' '
+ test_config mailmap.file complex.map &&
+ git log --use-mailmap --author "<cto@coompany.xx>" >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'Blame --porcelain output (complex mapping)' '
+ test_config mailmap.file complex.map &&
+
+ cat >expect <<-EOF &&
+ 1 1 1
+ A U Thor
+ 2 2 1
+ Some Dude
+ 3 3 1
+ Other Author
+ 4 4 1
+ Other Author
+ 5 5 1
+ Santa Claus
+ 6 6 1
+ Santa Claus
+ 7 7 1
+ CTO
+ EOF
+
+ git blame --porcelain one >actual.blame &&
+
+ NUM="[0-9][0-9]*" &&
+ sed -n <actual.blame >actual.fuzz \
+ -e "s/^author //p" \
+ -e "s/^$OID_REGEX \\($NUM $NUM $NUM\\)$/\\1/p" &&
+ test_cmp expect actual.fuzz
+'
+
+test_expect_success 'Blame output (complex mapping)' '
+ git -c mailmap.file=complex.map blame one >a &&
+ git blame one >b &&
+ test_file_not_empty a &&
+ ! cmp a b
+'
+
+test_expect_success 'commit --author honors mailmap' '
+ test_config mailmap.file complex.map &&
+
+ cat >expect <<-\EOF &&
+ Some Dude <some@dude.xx>
+ EOF
+
+ test_must_fail git commit --author "nick" --allow-empty -meight &&
+ git commit --author "Some Dude" --allow-empty -meight &&
+ git show --pretty=format:"%an <%ae>%n" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'comment syntax: setup' '
+ test_create_repo comm &&
+ test_commit -C comm --author "A <a@example.com>" A &&
+ test_commit -C comm --author "B <b@example.com>" B &&
+ test_commit -C comm --author "C <#@example.com>" C &&
+ test_commit -C comm --author "D <d@e#ample.com>" D &&
+
+ test_config -C comm mailmap.file ../doc.map &&
+ cat >>doc.map <<-\EOF &&
+ # Ah <a@example.com>
+
+ ; Bee <b@example.com>
+ Cee <cee@example.com> <#@example.com>
+ Dee <dee@example.com> <d@e#ample.com>
+ EOF
+
+ cat >expect <<-\EOF &&
+ Author A <a@example.com> maps to A <a@example.com>
+ Committer C O Mitter <committer@example.com> maps to C O Mitter <committer@example.com>
+
+ Author B <b@example.com> maps to ; Bee <b@example.com>
+ Committer C O Mitter <committer@example.com> maps to C O Mitter <committer@example.com>
+
+ Author C <#@example.com> maps to Cee <cee@example.com>
+ Committer C O Mitter <committer@example.com> maps to C O Mitter <committer@example.com>
+
+ Author D <d@e#ample.com> maps to Dee <dee@example.com>
+ Committer C O Mitter <committer@example.com> maps to C O Mitter <committer@example.com>
+ EOF
+ git -C comm log --reverse --pretty=format:"Author %an <%ae> maps to %aN <%aE>%nCommitter %cn <%ce> maps to %cN <%cE>%n" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'whitespace syntax: setup' '
+ test_create_repo space &&
+ test_commit -C space --author "A <a@example.com>" A &&
+ test_commit -C space --author "B <b@example.com>" B &&
+ test_commit -C space --author " C <c@example.com>" C &&
+ test_commit -C space --author " D <d@example.com>" D &&
+ test_commit -C space --author "E E <e@example.com>" E &&
+ test_commit -C space --author "F F <f@example.com>" F &&
+ test_commit -C space --author "G G <g@example.com>" G &&
+ test_commit -C space --author "H H <h@example.com>" H &&
+
+ test_config -C space mailmap.file ../space.map &&
+ cat >>space.map <<-\EOF &&
+ Ah <ah@example.com> < a@example.com >
+ Bee <bee@example.com > < b@example.com >
+ Cee <cee@example.com> C <c@example.com>
+ dee <dee@example.com> D <d@example.com>
+ eee <eee@example.com> E E <e@example.com>
+ eff <eff@example.com> F F <f@example.com>
+ gee <gee@example.com> G G <g@example.com>
+ aitch <aitch@example.com> H H <h@example.com>
+ EOF
+
+ cat >expect <<-\EOF &&
+ Author A <a@example.com> maps to A <a@example.com>
+ Committer C O Mitter <committer@example.com> maps to C O Mitter <committer@example.com>
+
+ Author B <b@example.com> maps to B <b@example.com>
+ Committer C O Mitter <committer@example.com> maps to C O Mitter <committer@example.com>
+
+ Author C <c@example.com> maps to Cee <cee@example.com>
+ Committer C O Mitter <committer@example.com> maps to C O Mitter <committer@example.com>
+
+ Author D <d@example.com> maps to dee <dee@example.com>
+ Committer C O Mitter <committer@example.com> maps to C O Mitter <committer@example.com>
+
+ Author E E <e@example.com> maps to eee <eee@example.com>
+ Committer C O Mitter <committer@example.com> maps to C O Mitter <committer@example.com>
+
+ Author F F <f@example.com> maps to eff <eff@example.com>
+ Committer C O Mitter <committer@example.com> maps to C O Mitter <committer@example.com>
+
+ Author G G <g@example.com> maps to gee <gee@example.com>
+ Committer C O Mitter <committer@example.com> maps to C O Mitter <committer@example.com>
+
+ Author H H <h@example.com> maps to H H <h@example.com>
+ Committer C O Mitter <committer@example.com> maps to C O Mitter <committer@example.com>
+ EOF
+ git -C space log --reverse --pretty=format:"Author %an <%ae> maps to %aN <%aE>%nCommitter %cn <%ce> maps to %cN <%cE>%n" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'empty syntax: setup' '
+ test_create_repo empty &&
+ test_commit -C empty --author "A <>" A &&
+ test_commit -C empty --author "B <b@example.com>" B &&
+ test_commit -C empty --author "C <c@example.com>" C &&
+
+ test_config -C empty mailmap.file ../empty.map &&
+ cat >>empty.map <<-\EOF &&
+ Ah <ah@example.com> <>
+ Bee <bee@example.com> <>
+ Cee <> <c@example.com>
+ EOF
+
+ cat >expect <<-\EOF &&
+ Author A <> maps to Bee <bee@example.com>
+ Committer C O Mitter <committer@example.com> maps to C O Mitter <committer@example.com>
+
+ Author B <b@example.com> maps to B <b@example.com>
+ Committer C O Mitter <committer@example.com> maps to C O Mitter <committer@example.com>
+
+ Author C <c@example.com> maps to C <c@example.com>
+ Committer C O Mitter <committer@example.com> maps to C O Mitter <committer@example.com>
+ EOF
+ git -C empty log --reverse --pretty=format:"Author %an <%ae> maps to %aN <%aE>%nCommitter %cn <%ce> maps to %cN <%cE>%n" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'set up mailmap location tests' '
+ git init --bare loc-bare &&
+ git --git-dir=loc-bare --work-tree=. commit \
+ --allow-empty -m foo --author="Orig <orig@example.com>" &&
+ echo "New <new@example.com> <orig@example.com>" >loc-bare/.mailmap
+'
+
+test_expect_success 'bare repo with --work-tree finds mailmap at top-level' '
+ git -C loc-bare --work-tree=. log -1 --format=%aE >actual &&
+ echo new@example.com >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'bare repo does not look in current directory' '
+ git -C loc-bare log -1 --format=%aE >actual &&
+ echo orig@example.com >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'non-git shortlog respects mailmap in current dir' '
+ git --git-dir=loc-bare log -1 >input &&
+ nongit cp "$TRASH_DIRECTORY/loc-bare/.mailmap" . &&
+ nongit git shortlog -s <input >actual &&
+ echo " 1 New" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'shortlog on stdin respects mailmap from repo' '
+ cp loc-bare/.mailmap . &&
+ git shortlog -s <input >actual &&
+ echo " 1 New" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'find top-level mailmap from subdir' '
+ git clone loc-bare loc-wt &&
+ cp loc-bare/.mailmap loc-wt &&
+ mkdir loc-wt/subdir &&
+ git -C loc-wt/subdir log -1 --format=%aE >actual &&
+ echo new@example.com >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success SYMLINKS 'set up symlink tests' '
+ git commit --allow-empty -m foo --author="Orig <orig@example.com>" &&
+ echo "New <new@example.com> <orig@example.com>" >map &&
+ rm -f .mailmap
+'
+
+test_expect_success SYMLINKS 'symlinks respected in mailmap.file' '
+ test_when_finished "rm symlink" &&
+ ln -s map symlink &&
+ git -c mailmap.file="$(pwd)/symlink" log -1 --format=%aE >actual &&
+ echo "new@example.com" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success SYMLINKS 'symlinks respected in non-repo shortlog' '
+ git log -1 >input &&
+ test_when_finished "nongit rm .mailmap" &&
+ nongit ln -sf "$TRASH_DIRECTORY/map" .mailmap &&
+ nongit git shortlog -s <input >actual &&
+ echo " 1 New" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success SYMLINKS 'symlinks not respected in-tree' '
+ test_when_finished "rm .mailmap" &&
+ ln -s map .mailmap &&
+ git log -1 --format=%aE >actual &&
+ echo "orig@example.com" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'prepare for cat-file --mailmap' '
+ rm -f .mailmap &&
+ git commit --allow-empty -m foo --author="Orig <orig@example.com>"
+'
+
+test_expect_success '--no-use-mailmap disables mailmap in cat-file' '
+ test_when_finished "rm .mailmap" &&
+ cat >.mailmap <<-EOF &&
+ A U Thor <author@example.com> Orig <orig@example.com>
+ EOF
+ cat >expect <<-EOF &&
+ author Orig <orig@example.com>
+ EOF
+ git cat-file --no-use-mailmap commit HEAD >log &&
+ sed -n "/^author /s/\([^>]*>\).*/\1/p" log >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--use-mailmap enables mailmap in cat-file' '
+ test_when_finished "rm .mailmap" &&
+ cat >.mailmap <<-EOF &&
+ A U Thor <author@example.com> Orig <orig@example.com>
+ EOF
+ cat >expect <<-EOF &&
+ author A U Thor <author@example.com>
+ EOF
+ git cat-file --use-mailmap commit HEAD >log &&
+ sed -n "/^author /s/\([^>]*>\).*/\1/p" log >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--no-mailmap disables mailmap in cat-file for annotated tag objects' '
+ test_when_finished "rm .mailmap" &&
+ cat >.mailmap <<-EOF &&
+ Orig <orig@example.com> C O Mitter <committer@example.com>
+ EOF
+ cat >expect <<-EOF &&
+ tagger C O Mitter <committer@example.com>
+ EOF
+ git tag -a -m "annotated tag" v1 &&
+ git cat-file --no-mailmap -p v1 >log &&
+ sed -n "/^tagger /s/\([^>]*>\).*/\1/p" log >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--mailmap enables mailmap in cat-file for annotated tag objects' '
+ test_when_finished "rm .mailmap" &&
+ cat >.mailmap <<-EOF &&
+ Orig <orig@example.com> C O Mitter <committer@example.com>
+ EOF
+ cat >expect <<-EOF &&
+ tagger Orig <orig@example.com>
+ EOF
+ git tag -a -m "annotated tag" v2 &&
+ git cat-file --mailmap -p v2 >log &&
+ sed -n "/^tagger /s/\([^>]*>\).*/\1/p" log >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git cat-file -s returns correct size with --use-mailmap' '
+ test_when_finished "rm .mailmap" &&
+ cat >.mailmap <<-\EOF &&
+ C O Mitter <committer@example.com> Orig <orig@example.com>
+ EOF
+ git cat-file commit HEAD >commit.out &&
+ echo $(wc -c <commit.out) >expect &&
+ git cat-file --use-mailmap commit HEAD >commit.out &&
+ echo $(wc -c <commit.out) >>expect &&
+ git cat-file -s HEAD >actual &&
+ git cat-file --use-mailmap -s HEAD >>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git cat-file -s returns correct size with --use-mailmap for tag objects' '
+ test_when_finished "rm .mailmap" &&
+ cat >.mailmap <<-\EOF &&
+ Orig <orig@example.com> C O Mitter <committer@example.com>
+ EOF
+ git tag -a -m "annotated tag" v3 &&
+ git cat-file tag v3 >tag.out &&
+ echo $(wc -c <tag.out) >expect &&
+ git cat-file --use-mailmap tag v3 >tag.out &&
+ echo $(wc -c <tag.out) >>expect &&
+ git cat-file -s v3 >actual &&
+ git cat-file --use-mailmap -s v3 >>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git cat-file --batch-check returns correct size with --use-mailmap' '
+ test_when_finished "rm .mailmap" &&
+ cat >.mailmap <<-\EOF &&
+ C O Mitter <committer@example.com> Orig <orig@example.com>
+ EOF
+ git cat-file commit HEAD >commit.out &&
+ commit_size=$(wc -c <commit.out) &&
+ commit_sha=$(git rev-parse HEAD) &&
+ echo $commit_sha commit $commit_size >expect &&
+ git cat-file --use-mailmap commit HEAD >commit.out &&
+ commit_size=$(wc -c <commit.out) &&
+ echo $commit_sha commit $commit_size >>expect &&
+ echo "HEAD" >in &&
+ git cat-file --batch-check <in >actual &&
+ git cat-file --use-mailmap --batch-check <in >>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git cat-file --batch-command returns correct size with --use-mailmap' '
+ test_when_finished "rm .mailmap" &&
+ cat >.mailmap <<-\EOF &&
+ C O Mitter <committer@example.com> Orig <orig@example.com>
+ EOF
+ git cat-file commit HEAD >commit.out &&
+ commit_size=$(wc -c <commit.out) &&
+ commit_sha=$(git rev-parse HEAD) &&
+ echo $commit_sha commit $commit_size >expect &&
+ git cat-file --use-mailmap commit HEAD >commit.out &&
+ commit_size=$(wc -c <commit.out) &&
+ echo $commit_sha commit $commit_size >>expect &&
+ echo "info HEAD" >in &&
+ git cat-file --batch-command <in >actual &&
+ git cat-file --use-mailmap --batch-command <in >>actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t4204-patch-id.sh b/t/t4204-patch-id.sh
new file mode 100755
index 0000000..a7fa94c
--- /dev/null
+++ b/t/t4204-patch-id.sh
@@ -0,0 +1,313 @@
+#!/bin/sh
+
+test_description='git patch-id'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ str="ab cd ef gh ij kl mn op" &&
+ test_write_lines $str >foo &&
+ test_write_lines $str >bar &&
+ git add foo bar &&
+ git commit -a -m initial &&
+ test_write_lines $str b >foo &&
+ test_write_lines $str b >bar &&
+ git commit -a -m first &&
+ git checkout -b same main &&
+ git commit --amend -m same-msg &&
+ git checkout -b notsame main &&
+ echo c >foo &&
+ echo c >bar &&
+ git commit --amend -a -m notsame-msg &&
+ git checkout -b with_space main~ &&
+ cat >foo <<-\EOF &&
+ a b
+ c d
+ e f
+ g h
+ i j
+ k l
+ m n
+ op
+ EOF
+ cp foo bar &&
+ git add foo bar &&
+ git commit --amend -m "with spaces" &&
+ test_write_lines bar foo >bar-then-foo &&
+ test_write_lines foo bar >foo-then-bar
+
+'
+
+test_expect_success 'patch-id output is well-formed' '
+ git log -p -1 >log.output &&
+ git patch-id <log.output >output &&
+ grep "^$OID_REGEX $(git rev-parse HEAD)$" output
+'
+
+#calculate patch id. Make sure output is not empty.
+calc_patch_id () {
+ patch_name="$1"
+ shift
+ git patch-id "$@" >patch-id.output &&
+ sed "s/ .*//" patch-id.output >patch-id_"$patch_name" &&
+ test_line_count -eq 1 patch-id_"$patch_name"
+}
+
+get_top_diff () {
+ git log -p -1 "$@" -O bar-then-foo --full-index --
+}
+
+get_patch_id () {
+ get_top_diff "$1" >top-diff.output &&
+ calc_patch_id <top-diff.output "$@"
+}
+
+test_expect_success 'patch-id detects equality' '
+ get_patch_id main &&
+ get_patch_id same &&
+ test_cmp patch-id_main patch-id_same
+'
+
+test_expect_success 'patch-id detects inequality' '
+ get_patch_id main &&
+ get_patch_id notsame &&
+ ! test_cmp patch-id_main patch-id_notsame
+'
+test_expect_success 'patch-id detects equality binary' '
+ cat >.gitattributes <<-\EOF &&
+ foo binary
+ bar binary
+ EOF
+ get_patch_id main &&
+ get_patch_id same &&
+ git log -p -1 --binary main >top-diff.output &&
+ calc_patch_id <top-diff.output main_binpatch &&
+ git log -p -1 --binary same >top-diff.output &&
+ calc_patch_id <top-diff.output same_binpatch &&
+ test_cmp patch-id_main patch-id_main_binpatch &&
+ test_cmp patch-id_same patch-id_same_binpatch &&
+ test_cmp patch-id_main patch-id_same &&
+ test_when_finished "rm .gitattributes"
+'
+
+test_expect_success 'patch-id detects inequality binary' '
+ cat >.gitattributes <<-\EOF &&
+ foo binary
+ bar binary
+ EOF
+ get_patch_id main &&
+ get_patch_id notsame &&
+ ! test_cmp patch-id_main patch-id_notsame &&
+ test_when_finished "rm .gitattributes"
+'
+
+test_expect_success 'patch-id supports git-format-patch output' '
+ get_patch_id main &&
+ git checkout same &&
+ git format-patch -1 --stdout >format-patch.output &&
+ calc_patch_id same <format-patch.output &&
+ test_cmp patch-id_main patch-id_same &&
+ set $(git patch-id <format-patch.output) &&
+ test "$2" = $(git rev-parse HEAD)
+'
+
+test_expect_success 'whitespace is irrelevant in footer' '
+ get_patch_id main &&
+ git checkout same &&
+ git format-patch -1 --stdout >format-patch.output &&
+ sed "s/ \$//" format-patch.output | calc_patch_id same &&
+ test_cmp patch-id_main patch-id_same
+'
+
+cmp_patch_id () {
+ if
+ test "$1" = "relevant"
+ then
+ ! test_cmp patch-id_"$2" patch-id_"$3"
+ else
+ test_cmp patch-id_"$2" patch-id_"$3"
+ fi
+}
+
+test_patch_id_file_order () {
+ relevant="$1"
+ shift
+ name="order-${1}-$relevant"
+ shift
+ get_top_diff "main" >top-diff.output &&
+ calc_patch_id <top-diff.output "$name" "$@" &&
+ git checkout same &&
+ git format-patch -1 --stdout -O foo-then-bar >format-patch.output &&
+ calc_patch_id <format-patch.output "ordered-$name" "$@" &&
+ cmp_patch_id $relevant "$name" "ordered-$name"
+}
+
+test_patch_id_whitespace () {
+ relevant="$1"
+ shift
+ name="ws-${1}-$relevant"
+ shift
+ get_top_diff "main~" >top-diff.output &&
+ calc_patch_id <top-diff.output "$name" "$@" &&
+ get_top_diff "with_space" >top-diff.output &&
+ calc_patch_id <top-diff.output "ws-$name" "$@" &&
+ cmp_patch_id $relevant "$name" "ws-$name"
+}
+
+
+# combined test for options: add more tests here to make them
+# run with all options
+test_patch_id () {
+ test_patch_id_file_order "$@"
+}
+
+# small tests with detailed diagnostic for basic options.
+test_expect_success 'file order is irrelevant with --stable' '
+ test_patch_id_file_order irrelevant --stable --stable
+'
+
+test_expect_success 'file order is relevant with --unstable' '
+ test_patch_id_file_order relevant --unstable --unstable
+'
+
+test_expect_success 'whitespace is relevant with --verbatim' '
+ test_patch_id_whitespace relevant --verbatim --verbatim
+'
+
+test_expect_success 'whitespace is irrelevant without --verbatim' '
+ test_patch_id_whitespace irrelevant --stable --stable
+'
+
+#Now test various option combinations.
+test_expect_success 'default is unstable' '
+ test_patch_id relevant default
+'
+
+test_expect_success 'patchid.stable = true is stable' '
+ test_config patchid.stable true &&
+ test_patch_id irrelevant patchid.stable=true
+'
+
+test_expect_success 'patchid.stable = false is unstable' '
+ test_config patchid.stable false &&
+ test_patch_id relevant patchid.stable=false
+'
+
+test_expect_success 'patchid.verbatim = true is correct and stable' '
+ test_config patchid.verbatim true &&
+ test_patch_id_whitespace relevant patchid.verbatim=true &&
+ test_patch_id irrelevant patchid.verbatim=true
+'
+
+test_expect_success 'patchid.verbatim = false is unstable' '
+ test_config patchid.verbatim false &&
+ test_patch_id relevant patchid.verbatim=false
+'
+
+test_expect_success '--unstable overrides patchid.stable = true' '
+ test_config patchid.stable true &&
+ test_patch_id relevant patchid.stable=true--unstable --unstable
+'
+
+test_expect_success '--stable overrides patchid.stable = false' '
+ test_config patchid.stable false &&
+ test_patch_id irrelevant patchid.stable=false--stable --stable
+'
+
+test_expect_success '--verbatim overrides patchid.stable = false' '
+ test_config patchid.stable false &&
+ test_patch_id_whitespace relevant stable=false--verbatim --verbatim
+'
+
+test_expect_success 'patch-id supports git-format-patch MIME output' '
+ get_patch_id main &&
+ git checkout same &&
+ git format-patch -1 --attach --stdout >format-patch.output &&
+ calc_patch_id <format-patch.output same &&
+ test_cmp patch-id_main patch-id_same
+'
+
+test_expect_success 'patch-id respects config from subdir' '
+ test_config patchid.stable true &&
+ mkdir subdir &&
+
+ # copy these because test_patch_id() looks for them in
+ # the current directory
+ cp bar-then-foo foo-then-bar subdir &&
+
+ (
+ cd subdir &&
+ test_patch_id irrelevant patchid.stable=true
+ )
+'
+
+test_expect_success 'patch-id handles no-nl-at-eof markers' '
+ cat >nonl <<-\EOF &&
+ diff --git i/a w/a
+ index e69de29..2e65efe 100644
+ --- i/a
+ +++ w/a
+ @@ -0,0 +1 @@
+ +a
+ \ No newline at end of file
+ diff --git i/b w/b
+ index e69de29..6178079 100644
+ --- i/b
+ +++ w/b
+ @@ -0,0 +1 @@
+ +b
+ EOF
+ cat >withnl <<-\EOF &&
+ diff --git i/a w/a
+ index e69de29..7898192 100644
+ --- i/a
+ +++ w/a
+ @@ -0,0 +1 @@
+ +a
+ diff --git i/b w/b
+ index e69de29..6178079 100644
+ --- i/b
+ +++ w/b
+ @@ -0,0 +1 @@
+ +b
+ EOF
+ calc_patch_id nonl <nonl &&
+ calc_patch_id withnl <withnl &&
+ test_cmp patch-id_nonl patch-id_withnl &&
+ calc_patch_id nonl-inc-ws --verbatim <nonl &&
+ calc_patch_id withnl-inc-ws --verbatim <withnl &&
+ ! test_cmp patch-id_nonl-inc-ws patch-id_withnl-inc-ws
+'
+
+test_expect_success 'patch-id handles diffs with one line of before/after' '
+ cat >diffu1 <<-\EOF &&
+ diff --git a/bar b/bar
+ index bdaf90f..31051f6 100644
+ --- a/bar
+ +++ b/bar
+ @@ -2 +2,2 @@
+ b
+ +c
+ diff --git a/car b/car
+ index 00750ed..2ae5e34 100644
+ --- a/car
+ +++ b/car
+ @@ -1 +1,2 @@
+ 3
+ +d
+ diff --git a/foo b/foo
+ index e439850..7146eb8 100644
+ --- a/foo
+ +++ b/foo
+ @@ -2 +2,2 @@
+ a
+ +e
+ EOF
+ calc_patch_id diffu1 <diffu1 &&
+ test_config patchid.stable true &&
+ calc_patch_id diffu1stable <diffu1
+'
+test_done
diff --git a/t/t4205-log-pretty-formats.sh b/t/t4205-log-pretty-formats.sh
new file mode 100755
index 0000000..e3d655e
--- /dev/null
+++ b/t/t4205-log-pretty-formats.sh
@@ -0,0 +1,1201 @@
+#!/bin/sh
+#
+# Copyright (c) 2010, Will Palmer
+# Copyright (c) 2011, Alexey Shumkin (+ non-UTF-8 commit encoding tests)
+#
+
+test_description='Test pretty formats'
+. ./test-lib.sh
+
+# Tested non-UTF-8 encoding
+test_encoding="ISO8859-1"
+
+sample_utf8_part=$(printf "f\303\244ng")
+
+commit_msg () {
+ # String "initial. initial" partly in German
+ # (translated with Google Translate),
+ # encoded in UTF-8, used as a commit log message below.
+ msg="initial. an${sample_utf8_part}lich\n"
+ if test -n "$1"
+ then
+ printf "$msg" | iconv -f utf-8 -t "$1"
+ else
+ printf "$msg"
+ fi
+}
+
+test_expect_success 'set up basic repos' '
+ >foo &&
+ >bar &&
+ git add foo &&
+ test_tick &&
+ git config i18n.commitEncoding $test_encoding &&
+ commit_msg $test_encoding | git commit -F - &&
+ git add bar &&
+ test_tick &&
+ git commit -m "add bar" &&
+ git config --unset i18n.commitEncoding
+'
+
+test_expect_success 'alias builtin format' '
+ git log --pretty=oneline >expected &&
+ git config pretty.test-alias oneline &&
+ git log --pretty=test-alias >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'alias masking builtin format' '
+ git log --pretty=oneline >expected &&
+ git config pretty.oneline "%H" &&
+ git log --pretty=oneline >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'alias user-defined format' '
+ git log --pretty="format:%h" >expected &&
+ git config pretty.test-alias "format:%h" &&
+ git log --pretty=test-alias >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'alias user-defined tformat with %s (ISO8859-1 encoding)' '
+ git config i18n.logOutputEncoding $test_encoding &&
+ git log --oneline >expected-s &&
+ git log --pretty="tformat:%h %s" >actual-s &&
+ git config --unset i18n.logOutputEncoding &&
+ test_cmp expected-s actual-s
+'
+
+test_expect_success 'alias user-defined tformat with %s (utf-8 encoding)' '
+ git log --oneline >expected-s &&
+ git log --pretty="tformat:%h %s" >actual-s &&
+ test_cmp expected-s actual-s
+'
+
+test_expect_success 'alias user-defined tformat' '
+ git log --pretty="tformat:%h" >expected &&
+ git config pretty.test-alias "tformat:%h" &&
+ git log --pretty=test-alias >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'alias non-existent format' '
+ git config pretty.test-alias format-that-will-never-exist &&
+ test_must_fail git log --pretty=test-alias
+'
+
+test_expect_success 'alias of an alias' '
+ git log --pretty="tformat:%h" >expected &&
+ git config pretty.test-foo "tformat:%h" &&
+ git config pretty.test-bar test-foo &&
+ git log --pretty=test-bar >actual && test_cmp expected actual
+'
+
+test_expect_success 'alias masking an alias' '
+ git log --pretty=format:"Two %H" >expected &&
+ git config pretty.duplicate "format:One %H" &&
+ git config --add pretty.duplicate "format:Two %H" &&
+ git log --pretty=duplicate >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'alias loop' '
+ git config pretty.test-foo test-bar &&
+ git config pretty.test-bar test-foo &&
+ test_must_fail git log --pretty=test-foo
+'
+
+test_expect_success 'NUL separation' '
+ printf "add bar\0$(commit_msg)" >expected &&
+ git log -z --pretty="format:%s" >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'NUL termination' '
+ printf "add bar\0$(commit_msg)\0" >expected &&
+ git log -z --pretty="tformat:%s" >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'NUL separation with --stat' '
+ stat0_part=$(git diff --stat HEAD^ HEAD) &&
+ stat1_part=$(git diff-tree --no-commit-id --stat --root HEAD^) &&
+ printf "add bar\n$stat0_part\n\0$(commit_msg)\n$stat1_part\n" >expected &&
+ git log -z --stat --pretty="format:%s" >actual &&
+ test_cmp expected actual
+'
+
+test_expect_failure 'NUL termination with --stat' '
+ stat0_part=$(git diff --stat HEAD^ HEAD) &&
+ stat1_part=$(git diff-tree --no-commit-id --stat --root HEAD^) &&
+ printf "add bar\n$stat0_part\n\0$(commit_msg)\n$stat1_part\n0" >expected &&
+ git log -z --stat --pretty="tformat:%s" >actual &&
+ test_cmp expected actual
+'
+
+for p in short medium full fuller email raw
+do
+ test_expect_success "NUL termination with --reflog --pretty=$p" '
+ revs="$(git rev-list --reflog)" &&
+ for r in $revs
+ do
+ git show -s "$r" --pretty="$p" &&
+ printf "\0" || return 1
+ done >expect &&
+ {
+ git log -z --reflog --pretty="$p" &&
+ printf "\0"
+ } >actual &&
+ test_cmp expect actual
+ '
+done
+
+test_expect_success 'NUL termination with --reflog --pretty=oneline' '
+ revs="$(git rev-list --reflog)" &&
+ for r in $revs
+ do
+ git show -s --pretty=oneline "$r" >raw &&
+ cat raw | lf_to_nul || return 1
+ done >expect &&
+ # the trailing NUL is already produced so we do not need to
+ # output another one
+ git log -z --pretty=oneline --reflog >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'setup more commits' '
+ test_commit "message one" one one message-one &&
+ test_commit "message two" two two message-two &&
+ head1=$(git rev-parse --verify --short HEAD~0) &&
+ head2=$(git rev-parse --verify --short HEAD~1) &&
+ head3=$(git rev-parse --verify --short HEAD~2) &&
+ head4=$(git rev-parse --verify --short HEAD~3)
+'
+
+test_expect_success 'left alignment formatting' '
+ git log --pretty="tformat:%<(40)%s" >actual &&
+ qz_to_tab_space <<-EOF >expected &&
+ message two Z
+ message one Z
+ add bar Z
+ $(commit_msg) Z
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'left alignment formatting. i18n.logOutputEncoding' '
+ git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%<(40)%s" >actual &&
+ qz_to_tab_space <<-EOF | iconv -f utf-8 -t $test_encoding >expected &&
+ message two Z
+ message one Z
+ add bar Z
+ $(commit_msg) Z
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'left alignment formatting at the nth column' '
+ git log --pretty="tformat:%h %<|(40)%s" >actual &&
+ qz_to_tab_space <<-EOF >expected &&
+ $head1 message two Z
+ $head2 message one Z
+ $head3 add bar Z
+ $head4 $(commit_msg) Z
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'left alignment formatting at the nth column' '
+ COLUMNS=50 git log --pretty="tformat:%h %<|(-10)%s" >actual &&
+ qz_to_tab_space <<-EOF >expected &&
+ $head1 message two Z
+ $head2 message one Z
+ $head3 add bar Z
+ $head4 $(commit_msg) Z
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'left alignment formatting at the nth column. i18n.logOutputEncoding' '
+ git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%h %<|(40)%s" >actual &&
+ qz_to_tab_space <<-EOF | iconv -f utf-8 -t $test_encoding >expected &&
+ $head1 message two Z
+ $head2 message one Z
+ $head3 add bar Z
+ $head4 $(commit_msg) Z
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'left alignment formatting with no padding' '
+ git log --pretty="tformat:%<(1)%s" >actual &&
+ cat <<-EOF >expected &&
+ message two
+ message one
+ add bar
+ $(commit_msg)
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'left alignment formatting with no padding. i18n.logOutputEncoding' '
+ git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%<(1)%s" >actual &&
+ cat <<-EOF | iconv -f utf-8 -t $test_encoding >expected &&
+ message two
+ message one
+ add bar
+ $(commit_msg)
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'left alignment formatting with trunc' '
+ git log --pretty="tformat:%<(10,trunc)%s" >actual &&
+ qz_to_tab_space <<-\EOF >expected &&
+ message ..
+ message ..
+ add bar Z
+ initial...
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'left alignment formatting with trunc. i18n.logOutputEncoding' '
+ git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%<(10,trunc)%s" >actual &&
+ qz_to_tab_space <<-\EOF | iconv -f utf-8 -t $test_encoding >expected &&
+ message ..
+ message ..
+ add bar Z
+ initial...
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'left alignment formatting with ltrunc' '
+ git log --pretty="tformat:%<(10,ltrunc)%s" >actual &&
+ qz_to_tab_space <<-EOF >expected &&
+ ..sage two
+ ..sage one
+ add bar Z
+ ..${sample_utf8_part}lich
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'left alignment formatting with ltrunc. i18n.logOutputEncoding' '
+ git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%<(10,ltrunc)%s" >actual &&
+ qz_to_tab_space <<-EOF | iconv -f utf-8 -t $test_encoding >expected &&
+ ..sage two
+ ..sage one
+ add bar Z
+ ..${sample_utf8_part}lich
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'left alignment formatting with mtrunc' '
+ git log --pretty="tformat:%<(10,mtrunc)%s" >actual &&
+ qz_to_tab_space <<-\EOF >expected &&
+ mess.. two
+ mess.. one
+ add bar Z
+ init..lich
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'left alignment formatting with mtrunc. i18n.logOutputEncoding' '
+ git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%<(10,mtrunc)%s" >actual &&
+ qz_to_tab_space <<-\EOF | iconv -f utf-8 -t $test_encoding >expected &&
+ mess.. two
+ mess.. one
+ add bar Z
+ init..lich
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'right alignment formatting' '
+ git log --pretty="tformat:%>(40)%s" >actual &&
+ qz_to_tab_space <<-EOF >expected &&
+ Z message two
+ Z message one
+ Z add bar
+ Z $(commit_msg)
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'right alignment formatting. i18n.logOutputEncoding' '
+ git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%>(40)%s" >actual &&
+ qz_to_tab_space <<-EOF | iconv -f utf-8 -t $test_encoding >expected &&
+ Z message two
+ Z message one
+ Z add bar
+ Z $(commit_msg)
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'right alignment formatting at the nth column' '
+ git log --pretty="tformat:%h %>|(40)%s" >actual &&
+ qz_to_tab_space <<-EOF >expected &&
+ $head1 message two
+ $head2 message one
+ $head3 add bar
+ $head4 $(commit_msg)
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'right alignment formatting at the nth column' '
+ COLUMNS=50 git log --pretty="tformat:%h %>|(-10)%s" >actual &&
+ qz_to_tab_space <<-EOF >expected &&
+ $head1 message two
+ $head2 message one
+ $head3 add bar
+ $head4 $(commit_msg)
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'right alignment formatting at the nth column. i18n.logOutputEncoding' '
+ git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%h %>|(40)%s" >actual &&
+ qz_to_tab_space <<-EOF | iconv -f utf-8 -t $test_encoding >expected &&
+ $head1 message two
+ $head2 message one
+ $head3 add bar
+ $head4 $(commit_msg)
+ EOF
+ test_cmp expected actual
+'
+
+# Note: Space between 'message' and 'two' should be in the same column
+# as in previous test.
+test_expect_success 'right alignment formatting at the nth column with --graph. i18n.logOutputEncoding' '
+ git -c i18n.logOutputEncoding=$test_encoding log --graph --pretty="tformat:%h %>|(40)%s" >actual &&
+ iconv -f utf-8 -t $test_encoding >expected <<-EOF &&
+ * $head1 message two
+ * $head2 message one
+ * $head3 add bar
+ * $head4 $(commit_msg)
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'right alignment formatting with no padding' '
+ git log --pretty="tformat:%>(1)%s" >actual &&
+ cat <<-EOF >expected &&
+ message two
+ message one
+ add bar
+ $(commit_msg)
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'right alignment formatting with no padding and with --graph' '
+ git log --graph --pretty="tformat:%>(1)%s" >actual &&
+ cat <<-EOF >expected &&
+ * message two
+ * message one
+ * add bar
+ * $(commit_msg)
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'right alignment formatting with no padding. i18n.logOutputEncoding' '
+ git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%>(1)%s" >actual &&
+ cat <<-EOF | iconv -f utf-8 -t $test_encoding >expected &&
+ message two
+ message one
+ add bar
+ $(commit_msg)
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'center alignment formatting' '
+ git log --pretty="tformat:%><(40)%s" >actual &&
+ qz_to_tab_space <<-EOF >expected &&
+ Z message two Z
+ Z message one Z
+ Z add bar Z
+ Z $(commit_msg) Z
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'center alignment formatting. i18n.logOutputEncoding' '
+ git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%><(40)%s" >actual &&
+ qz_to_tab_space <<-EOF | iconv -f utf-8 -t $test_encoding >expected &&
+ Z message two Z
+ Z message one Z
+ Z add bar Z
+ Z $(commit_msg) Z
+ EOF
+ test_cmp expected actual
+'
+test_expect_success 'center alignment formatting at the nth column' '
+ git log --pretty="tformat:%h %><|(40)%s" >actual &&
+ qz_to_tab_space <<-EOF >expected &&
+ $head1 message two Z
+ $head2 message one Z
+ $head3 add bar Z
+ $head4 $(commit_msg) Z
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'center alignment formatting at the nth column' '
+ COLUMNS=70 git log --pretty="tformat:%h %><|(-30)%s" >actual &&
+ qz_to_tab_space <<-EOF >expected &&
+ $head1 message two Z
+ $head2 message one Z
+ $head3 add bar Z
+ $head4 $(commit_msg) Z
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'center alignment formatting at the nth column. i18n.logOutputEncoding' '
+ git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%h %><|(40)%s" >actual &&
+ qz_to_tab_space <<-EOF | iconv -f utf-8 -t $test_encoding >expected &&
+ $head1 message two Z
+ $head2 message one Z
+ $head3 add bar Z
+ $head4 $(commit_msg) Z
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'center alignment formatting with no padding' '
+ git log --pretty="tformat:%><(1)%s" >actual &&
+ cat <<-EOF >expected &&
+ message two
+ message one
+ add bar
+ $(commit_msg)
+ EOF
+ test_cmp expected actual
+'
+
+# save HEAD's SHA-1 digest (with no abbreviations) to use it below
+# as far as the next test amends HEAD
+old_head1=$(git rev-parse --verify HEAD~0)
+test_expect_success 'center alignment formatting with no padding. i18n.logOutputEncoding' '
+ git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%><(1)%s" >actual &&
+ cat <<-EOF | iconv -f utf-8 -t $test_encoding >expected &&
+ message two
+ message one
+ add bar
+ $(commit_msg)
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'left/right alignment formatting with stealing' '
+ git commit --amend -m short --author "long long long <long@me.com>" &&
+ git log --pretty="tformat:%<(10,trunc)%s%>>(10,ltrunc)% an" >actual &&
+ cat <<-\EOF >expected &&
+ short long long long
+ message .. A U Thor
+ add bar A U Thor
+ initial... A U Thor
+ EOF
+ test_cmp expected actual
+'
+test_expect_success 'left/right alignment formatting with stealing. i18n.logOutputEncoding' '
+ git -c i18n.logOutputEncoding=$test_encoding log --pretty="tformat:%<(10,trunc)%s%>>(10,ltrunc)% an" >actual &&
+ cat <<-\EOF | iconv -f utf-8 -t $test_encoding >expected &&
+ short long long long
+ message .. A U Thor
+ add bar A U Thor
+ initial... A U Thor
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'strbuf_utf8_replace() not producing NUL' '
+ git log --color --pretty="tformat:%<(10,trunc)%s%>>(10,ltrunc)%C(auto)%d" |
+ test_decode_color |
+ nul_to_q >actual &&
+ ! grep Q actual
+'
+
+# --date=[XXX] and corresponding %a[X] %c[X] format equivalency
+test_expect_success '--date=iso-strict %ad%cd is the same as %aI%cI' '
+ git log --format=%ad%n%cd --date=iso-strict >expected &&
+ git log --format=%aI%n%cI >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success '--date=short %ad%cd is the same as %as%cs' '
+ git log --format=%ad%n%cd --date=short >expected &&
+ git log --format=%as%n%cs >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success '--date=human %ad%cd is the same as %ah%ch' '
+ git log --format=%ad%n%cd --date=human >expected &&
+ git log --format=%ah%n%ch >actual &&
+ test_cmp expected actual
+'
+
+# get new digests (with no abbreviations)
+test_expect_success 'set up log decoration tests' '
+ head1=$(git rev-parse --verify HEAD~0) &&
+ head2=$(git rev-parse --verify HEAD~1)
+'
+
+test_expect_success 'log decoration properly follows tag chain' '
+ git tag -a tag1 -m tag1 &&
+ git tag -a tag2 -m tag2 tag1 &&
+ git tag -d tag1 &&
+ git commit --amend -m shorter &&
+ git log --no-walk --tags --pretty="%H %d" --decorate=full >actual &&
+ cat <<-EOF >expected &&
+ $head2 (tag: refs/tags/message-one)
+ $old_head1 (tag: refs/tags/message-two)
+ $head1 (tag: refs/tags/tag2)
+ EOF
+ sort -k3 actual >actual1 &&
+ test_cmp expected actual1
+'
+
+test_expect_success 'clean log decoration' '
+ git log --no-walk --tags --pretty="%H %D" --decorate=full >actual &&
+ cat >expected <<-EOF &&
+ $head2 tag: refs/tags/message-one
+ $old_head1 tag: refs/tags/message-two
+ $head1 tag: refs/tags/tag2
+ EOF
+ sort -k3 actual >actual1 &&
+ test_cmp expected actual1
+'
+
+test_expect_success 'pretty format %decorate' '
+ git checkout -b foo &&
+ git commit --allow-empty -m "new commit" &&
+ git tag bar &&
+ git branch qux &&
+
+ echo " (HEAD -> foo, tag: bar, qux)" >expect1 &&
+ git log --format="%(decorate)" -1 >actual1 &&
+ test_cmp expect1 actual1 &&
+
+ echo "HEAD -> foo, tag: bar, qux" >expect2 &&
+ git log --format="%(decorate:prefix=,suffix=)" -1 >actual2 &&
+ test_cmp expect2 actual2 &&
+
+ echo "[ bar; qux; foo ]" >expect3 &&
+ git log --format="%(decorate:prefix=[ ,suffix= ],separator=%x3B ,tag=)" \
+ --decorate-refs=refs/ -1 >actual3 &&
+ test_cmp expect3 actual3 &&
+
+ # Try with a typo (in "separator"), in which case the placeholder should
+ # not be replaced.
+ echo "%(decorate:prefix=[ ,suffix= ],separater=; )" >expect4 &&
+ git log --format="%(decorate:prefix=[ ,suffix= ],separater=%x3B )" \
+ -1 >actual4 &&
+ test_cmp expect4 actual4 &&
+
+ echo "HEAD->foo bar qux" >expect5 &&
+ git log --format="%(decorate:prefix=,suffix=,separator= ,tag=,pointer=->)" \
+ -1 >actual5 &&
+ test_cmp expect5 actual5
+'
+
+cat >trailers <<EOF
+Signed-off-by: A U Thor <author@example.com>
+Acked-by: A U Thor <author@example.com>
+[ v2 updated patch description ]
+Signed-off-by: A U Thor
+ <author@example.com>
+EOF
+
+unfold () {
+ perl -0pe 's/\n\s+/ /g'
+}
+
+test_expect_success 'set up trailer tests' '
+ echo "Some contents" >trailerfile &&
+ git add trailerfile &&
+ git commit -F - <<-EOF
+ trailers: this commit message has trailers
+
+ This commit is a test commit with trailers at the end. We parse this
+ message and display the trailers using %(trailers).
+
+ $(cat trailers)
+ EOF
+'
+
+test_expect_success 'pretty format %(trailers) shows trailers' '
+ git log --no-walk --pretty="%(trailers)" >actual &&
+ {
+ cat trailers &&
+ echo
+ } >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'pretty format %(trailers:) enables no options' '
+ git log --no-walk --pretty="%(trailers:)" >actual &&
+ # "expect" the same as the test above
+ test_cmp expect actual
+'
+
+test_expect_success '%(trailers:only) shows only "key: value" trailers' '
+ git log --no-walk --pretty="%(trailers:only)" >actual &&
+ {
+ grep -v patch.description <trailers &&
+ echo
+ } >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success '%(trailers:only=yes) shows only "key: value" trailers' '
+ git log --no-walk --pretty=format:"%(trailers:only=yes)" >actual &&
+ grep -v patch.description <trailers >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success '%(trailers:only=no) shows all trailers' '
+ git log --no-walk --pretty=format:"%(trailers:only=no)" >actual &&
+ cat trailers >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success '%(trailers:only=no,only=true) shows only "key: value" trailers' '
+ git log --no-walk --pretty=format:"%(trailers:only=yes)" >actual &&
+ grep -v patch.description <trailers >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success '%(trailers:unfold) unfolds trailers' '
+ git log --no-walk --pretty="%(trailers:unfold)" >actual &&
+ {
+ unfold <trailers &&
+ echo
+ } >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success ':only and :unfold work together' '
+ git log --no-walk --pretty="%(trailers:only,unfold)" >actual &&
+ git log --no-walk --pretty="%(trailers:unfold,only)" >reverse &&
+ test_cmp actual reverse &&
+ {
+ grep -v patch.description <trailers | unfold &&
+ echo
+ } >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'pretty format %(trailers:key=foo) shows that trailer' '
+ git log --no-walk --pretty="format:%(trailers:key=Acked-by)" >actual &&
+ echo "Acked-by: A U Thor <author@example.com>" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'pretty format %(trailers:key=foo) is case insensitive' '
+ git log --no-walk --pretty="format:%(trailers:key=AcKed-bY)" >actual &&
+ echo "Acked-by: A U Thor <author@example.com>" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'pretty format %(trailers:key=foo:) trailing colon also works' '
+ git log --no-walk --pretty="format:%(trailers:key=Acked-by:)" >actual &&
+ echo "Acked-by: A U Thor <author@example.com>" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'pretty format %(trailers:key=foo) multiple keys' '
+ git log --no-walk --pretty="format:%(trailers:key=Acked-by:,key=Signed-off-By)" >actual &&
+ grep -v patch.description <trailers >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success '%(trailers:key=nonexistent) becomes empty' '
+ git log --no-walk --pretty="x%(trailers:key=Nacked-by)x" >actual &&
+ echo "xx" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success '%(trailers:key=foo) handles multiple lines even if folded' '
+ git log --no-walk --pretty="format:%(trailers:key=Signed-Off-by)" >actual &&
+ grep -v patch.description <trailers | grep -v Acked-by >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success '%(trailers:key=foo,unfold) properly unfolds' '
+ git log --no-walk --pretty="format:%(trailers:key=Signed-Off-by,unfold)" >actual &&
+ unfold <trailers | grep Signed-off-by >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'pretty format %(trailers:key=foo,only=no) also includes nontrailer lines' '
+ git log --no-walk --pretty="format:%(trailers:key=Acked-by,only=no)" >actual &&
+ {
+ echo "Acked-by: A U Thor <author@example.com>" &&
+ grep patch.description <trailers
+ } >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success '%(trailers:key) without value is error' '
+ git log --no-walk --pretty="tformat:%(trailers:key)" >actual &&
+ echo "%(trailers:key)" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success '%(trailers:keyonly) shows only keys' '
+ git log --no-walk --pretty="format:%(trailers:keyonly)" >actual &&
+ test_write_lines \
+ "Signed-off-by" \
+ "Acked-by" \
+ "[ v2 updated patch description ]" \
+ "Signed-off-by" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success '%(trailers:key=foo,keyonly) shows only key' '
+ git log --no-walk --pretty="format:%(trailers:key=Acked-by,keyonly)" >actual &&
+ echo "Acked-by" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success '%(trailers:key=foo,valueonly) shows only value' '
+ git log --no-walk --pretty="format:%(trailers:key=Acked-by,valueonly)" >actual &&
+ echo "A U Thor <author@example.com>" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success '%(trailers:valueonly) shows only values' '
+ git log --no-walk --pretty="format:%(trailers:valueonly)" >actual &&
+ test_write_lines \
+ "A U Thor <author@example.com>" \
+ "A U Thor <author@example.com>" \
+ "[ v2 updated patch description ]" \
+ "A U Thor" \
+ " <author@example.com>" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success '%(trailers:key=foo,keyonly,valueonly) shows nothing' '
+ git log --no-walk --pretty="format:%(trailers:key=Acked-by,keyonly,valueonly)" >actual &&
+ echo >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'pretty format %(trailers:separator) changes separator' '
+ git log --no-walk --pretty=format:"X%(trailers:separator=%x00)X" >actual &&
+ (
+ printf "XSigned-off-by: A U Thor <author@example.com>\0" &&
+ printf "Acked-by: A U Thor <author@example.com>\0" &&
+ printf "[ v2 updated patch description ]\0" &&
+ printf "Signed-off-by: A U Thor\n <author@example.com>X"
+ ) >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'pretty format %(trailers:separator=X,unfold) changes separator' '
+ git log --no-walk --pretty=format:"X%(trailers:separator=%x00,unfold)X" >actual &&
+ (
+ printf "XSigned-off-by: A U Thor <author@example.com>\0" &&
+ printf "Acked-by: A U Thor <author@example.com>\0" &&
+ printf "[ v2 updated patch description ]\0" &&
+ printf "Signed-off-by: A U Thor <author@example.com>X"
+ ) >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'pretty format %(trailers:key_value_separator) changes key-value separator' '
+ git log --no-walk --pretty=format:"X%(trailers:key_value_separator=%x00)X" >actual &&
+ (
+ printf "XSigned-off-by\0A U Thor <author@example.com>\n" &&
+ printf "Acked-by\0A U Thor <author@example.com>\n" &&
+ printf "[ v2 updated patch description ]\n" &&
+ printf "Signed-off-by\0A U Thor\n <author@example.com>\nX"
+ ) >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'pretty format %(trailers:key_value_separator,unfold) changes key-value separator' '
+ git log --no-walk --pretty=format:"X%(trailers:key_value_separator=%x00,unfold)X" >actual &&
+ (
+ printf "XSigned-off-by\0A U Thor <author@example.com>\n" &&
+ printf "Acked-by\0A U Thor <author@example.com>\n" &&
+ printf "[ v2 updated patch description ]\n" &&
+ printf "Signed-off-by\0A U Thor <author@example.com>\nX"
+ ) >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'pretty format %(trailers:separator,key_value_separator) changes both separators' '
+ git log --no-walk --pretty=format:"%(trailers:separator=%x00,key_value_separator=%x00%x00,unfold)" >actual &&
+ (
+ printf "Signed-off-by\0\0A U Thor <author@example.com>\0" &&
+ printf "Acked-by\0\0A U Thor <author@example.com>\0" &&
+ printf "[ v2 updated patch description ]\0" &&
+ printf "Signed-off-by\0\0A U Thor <author@example.com>"
+ ) >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'pretty format %(trailers) combining separator/key/keyonly/valueonly' '
+ git commit --allow-empty -F - <<-\EOF &&
+ Important fix
+
+ The fix is explained here
+
+ Closes: #1234
+ EOF
+
+ git commit --allow-empty -F - <<-\EOF &&
+ Another fix
+
+ The fix is explained here
+
+ Closes: #567
+ Closes: #890
+ EOF
+
+ git commit --allow-empty -F - <<-\EOF &&
+ Does not close any tickets
+ EOF
+
+ git log --pretty="%s% (trailers:separator=%x2c%x20,key=Closes,valueonly)" HEAD~3.. >actual &&
+ test_write_lines \
+ "Does not close any tickets" \
+ "Another fix #567, #890" \
+ "Important fix #1234" >expect &&
+ test_cmp expect actual &&
+
+ git log --pretty="%s% (trailers:separator=%x2c%x20,key=Closes,keyonly)" HEAD~3.. >actual &&
+ test_write_lines \
+ "Does not close any tickets" \
+ "Another fix Closes, Closes" \
+ "Important fix Closes" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'trailer parsing not fooled by --- line' '
+ git commit --allow-empty -F - <<-\EOF &&
+ this is the subject
+
+ This is the body. The message has a "---" line which would confuse a
+ message+patch parser. But here we know we have only a commit message,
+ so we get it right.
+
+ trailer: wrong
+ ---
+ This is more body.
+
+ trailer: right
+ EOF
+
+ {
+ echo "trailer: right" &&
+ echo
+ } >expect &&
+ git log --no-walk --format="%(trailers)" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'set up %S tests' '
+ git checkout --orphan source-a &&
+ test_commit one &&
+ test_commit two &&
+ git checkout -b source-b HEAD^ &&
+ test_commit three
+'
+
+test_expect_success 'log --format=%S paints branch names' '
+ cat >expect <<-\EOF &&
+ source-b
+ source-a
+ source-b
+ EOF
+ git log --format=%S source-a source-b >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log --format=%S paints tag names' '
+ git tag -m tagged source-tag &&
+ cat >expect <<-\EOF &&
+ source-tag
+ source-a
+ source-tag
+ EOF
+ git log --format=%S source-tag source-a >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log --format=%S paints symmetric ranges' '
+ cat >expect <<-\EOF &&
+ source-b
+ source-a
+ EOF
+ git log --format=%S source-a...source-b >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '%S in git log --format works with other placeholders (part 1)' '
+ git log --format="source-b %h" source-b >expect &&
+ git log --format="%S %h" source-b >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '%S in git log --format works with other placeholders (part 2)' '
+ git log --format="%h source-b" source-b >expect &&
+ git log --format="%h %S" source-b >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'setup more commits for %S with --bisect' '
+ test_commit four &&
+ test_commit five &&
+
+ head1=$(git rev-parse --verify HEAD~0) &&
+ head2=$(git rev-parse --verify HEAD~1) &&
+ head3=$(git rev-parse --verify HEAD~2) &&
+ head4=$(git rev-parse --verify HEAD~3)
+'
+
+test_expect_success '%S with --bisect labels commits with refs/bisect/bad ref' '
+ git update-ref refs/bisect/bad-$head1 $head1 &&
+ git update-ref refs/bisect/go $head1 &&
+ git update-ref refs/bisect/bad-$head2 $head2 &&
+ git update-ref refs/bisect/b $head3 &&
+ git update-ref refs/bisect/bad-$head4 $head4 &&
+ git update-ref refs/bisect/good-$head4 $head4 &&
+
+ # We expect to see the range of commits betwee refs/bisect/good-$head4
+ # and refs/bisect/bad-$head1. The "source" ref is the nearest bisect ref
+ # from which the commit is reachable.
+ cat >expect <<-EOF &&
+ $head1 refs/bisect/bad-$head1
+ $head2 refs/bisect/bad-$head2
+ $head3 refs/bisect/bad-$head2
+ EOF
+ git log --bisect --format="%H %S" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log --pretty=reference' '
+ git log --pretty="tformat:%h (%s, %as)" >expect &&
+ git log --pretty=reference >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log --pretty=reference with log.date is overridden by short date' '
+ git log --pretty="tformat:%h (%s, %as)" >expect &&
+ test_config log.date rfc &&
+ git log --pretty=reference >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log --pretty=reference with explicit date overrides short date' '
+ git log --date=rfc --pretty="tformat:%h (%s, %ad)" >expect &&
+ git log --date=rfc --pretty=reference >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log --pretty=reference is never unabbreviated' '
+ git log --pretty="tformat:%h (%s, %as)" >expect &&
+ git log --no-abbrev-commit --pretty=reference >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log --pretty=reference is never decorated' '
+ git log --pretty="tformat:%h (%s, %as)" >expect &&
+ git log --decorate=short --pretty=reference >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log --pretty=reference does not output reflog info' '
+ git log --walk-reflogs --pretty="tformat:%h (%s, %as)" >expect &&
+ git log --walk-reflogs --pretty=reference >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log --pretty=reference is colored appropriately' '
+ git log --color=always --pretty="tformat:%C(auto)%h (%s, %as)" >expect &&
+ git log --color=always --pretty=reference >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '%(describe) vs git describe' '
+ git log --format="%H" | while read hash
+ do
+ if desc=$(git describe $hash)
+ then
+ : >expect-contains-good
+ else
+ : >expect-contains-bad
+ fi &&
+ echo "$hash $desc" || return 1
+ done >expect &&
+ test_path_exists expect-contains-good &&
+ test_path_exists expect-contains-bad &&
+
+ git log --format="%H %(describe)" >actual 2>err &&
+ test_cmp expect actual &&
+ test_must_be_empty err
+'
+
+test_expect_success '%(describe:match=...) vs git describe --match ...' '
+ test_when_finished "git tag -d tag-match" &&
+ git tag -a -m tagged tag-match &&
+ git describe --match "*-match" >expect &&
+ git log -1 --format="%(describe:match=*-match)" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '%(describe:exclude=...) vs git describe --exclude ...' '
+ test_when_finished "git tag -d tag-exclude" &&
+ git tag -a -m tagged tag-exclude &&
+ git describe --exclude "*-exclude" >expect &&
+ git log -1 --format="%(describe:exclude=*-exclude)" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '%(describe:tags) vs git describe --tags' '
+ test_when_finished "git tag -d tagname" &&
+ git tag tagname &&
+ git describe --tags >expect &&
+ git log -1 --format="%(describe:tags)" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '%(describe:abbrev=...) vs git describe --abbrev=...' '
+ test_when_finished "git tag -d tagname" &&
+
+ # Case 1: We have commits between HEAD and the most recent tag
+ # reachable from it
+ test_commit --no-tag file &&
+ git describe --abbrev=15 >expect &&
+ git log -1 --format="%(describe:abbrev=15)" >actual &&
+ test_cmp expect actual &&
+
+ # Make sure the hash used is at least 15 digits long
+ sed -e "s/^.*-g\([0-9a-f]*\)$/\1/" <actual >hexpart &&
+ test 16 -le $(wc -c <hexpart) &&
+
+ # Case 2: We have a tag at HEAD, describe directly gives the
+ # name of the tag
+ git tag -a -m tagged tagname &&
+ git describe --abbrev=15 >expect &&
+ git log -1 --format="%(describe:abbrev=15)" >actual &&
+ test_cmp expect actual &&
+ test tagname = $(cat actual)
+'
+
+test_expect_success 'log --pretty with space stealing' '
+ printf mm0 >expect &&
+ git log -1 --pretty="format:mm%>>|(1)%x30" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log --pretty with invalid padding format' '
+ printf "%s%%<(20" "$(git rev-parse HEAD)" >expect &&
+ git log -1 --pretty="format:%H%<(20" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log --pretty with magical wrapping directives' '
+ commit_id=$(git commit-tree HEAD^{tree} -m "describe me") &&
+ git tag describe-me $commit_id &&
+ printf "\n(tag:\ndescribe-me)%%+w(2)" >expect &&
+ git log -1 --pretty="format:%w(1)%+d%+w(2)" $commit_id >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success SIZE_T_IS_64BIT 'log --pretty with overflowing wrapping directive' '
+ printf "%%w(2147483649,1,1)0" >expect &&
+ git log -1 --pretty="format:%w(2147483649,1,1)%x30" >actual &&
+ test_cmp expect actual &&
+ printf "%%w(1,2147483649,1)0" >expect &&
+ git log -1 --pretty="format:%w(1,2147483649,1)%x30" >actual &&
+ test_cmp expect actual &&
+ printf "%%w(1,1,2147483649)0" >expect &&
+ git log -1 --pretty="format:%w(1,1,2147483649)%x30" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success SIZE_T_IS_64BIT 'log --pretty with overflowing padding directive' '
+ printf "%%<(2147483649)0" >expect &&
+ git log -1 --pretty="format:%<(2147483649)%x30" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log --pretty with padding and preceding control chars' '
+ printf "\20\20 0" >expect &&
+ git log -1 --pretty="format:%x10%x10%>|(4)%x30" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log --pretty truncation with control chars' '
+ test_commit "$(printf "\20\20\20\20xxxx")" file contents commit-with-control-chars &&
+ printf "\20\20\20\20x.." >expect &&
+ git log -1 --pretty="format:%<(3,trunc)%s" commit-with-control-chars >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success EXPENSIVE,SIZE_T_IS_64BIT 'log --pretty with huge commit message' '
+ # We only assert that this command does not crash. This needs to be
+ # executed with the address sanitizer to demonstrate failure.
+ git log -1 --pretty="format:%>(2147483646)%x41%41%>(2147483646)%x41" >/dev/null
+'
+
+test_expect_success EXPENSIVE,SIZE_T_IS_64BIT 'set up huge commit' '
+ test-tool genzeros 2147483649 | tr "\000" "1" >expect &&
+ huge_commit=$(git commit-tree -F expect HEAD^{tree})
+'
+
+test_expect_success EXPENSIVE,SIZE_T_IS_64BIT 'log --pretty with huge commit message' '
+ git log -1 --format="%B%<(1)%x30" $huge_commit >actual &&
+ echo 0 >>expect &&
+ test_cmp expect actual
+'
+
+test_expect_success EXPENSIVE,SIZE_T_IS_64BIT 'log --pretty with huge commit message does not cause allocation failure' '
+ test_must_fail git log -1 --format="%<(1)%B" $huge_commit 2>error &&
+ cat >expect <<-EOF &&
+ fatal: number too large to represent as int on this platform: 2147483649
+ EOF
+ test_cmp expect error
+'
+
+# pretty-formats note wide char limitations, and add tests
+test_expect_failure 'wide and decomposed characters column counting' '
+
+# from t/lib-unicode-nfc-nfd.sh hex values converted to octal
+ utf8_nfc=$(printf "\303\251") && # e acute combined.
+ utf8_nfd=$(printf "\145\314\201") && # e with a combining acute (i.e. decomposed)
+ utf8_emoji=$(printf "\360\237\221\250") &&
+
+# replacement character when requesting a wide char fits in a single display colum.
+# "half wide" alternative could be a plain ASCII dot `.`
+ utf8_vert_ell=$(printf "\342\213\256") &&
+
+# use ${xxx} here!
+ nfc10="${utf8_nfc}${utf8_nfc}${utf8_nfc}${utf8_nfc}${utf8_nfc}${utf8_nfc}${utf8_nfc}${utf8_nfc}${utf8_nfc}${utf8_nfc}" &&
+ nfd10="${utf8_nfd}${utf8_nfd}${utf8_nfd}${utf8_nfd}${utf8_nfd}${utf8_nfd}${utf8_nfd}${utf8_nfd}${utf8_nfd}${utf8_nfd}" &&
+ emoji5="${utf8_emoji}${utf8_emoji}${utf8_emoji}${utf8_emoji}${utf8_emoji}" &&
+# emoji5 uses 10 display columns
+
+ test_commit "abcdefghij" &&
+ test_commit --no-tag "${nfc10}" &&
+ test_commit --no-tag "${nfd10}" &&
+ test_commit --no-tag "${emoji5}" &&
+ printf "${utf8_emoji}..${utf8_emoji}${utf8_vert_ell}\n${utf8_nfd}..${utf8_nfd}${utf8_nfd}\n${utf8_nfc}..${utf8_nfc}${utf8_nfc}\na..ij\n" >expected &&
+ git log --format="%<(5,mtrunc)%s" -4 >actual &&
+ test_cmp expected actual
+'
+
+test_done
diff --git a/t/t4206-log-follow-harder-copies.sh b/t/t4206-log-follow-harder-copies.sh
new file mode 100755
index 0000000..9167b03
--- /dev/null
+++ b/t/t4206-log-follow-harder-copies.sh
@@ -0,0 +1,58 @@
+#!/bin/sh
+#
+# Copyright (c) 2010 Bo Yang
+#
+
+test_description='Test --follow should always find copies hard in git log.
+
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-diff.sh
+
+echo >path0 'Line 1
+Line 2
+Line 3
+'
+
+test_expect_success 'add a file path0 and commit.' '
+ git add path0 &&
+ git commit -m "Add path0"
+'
+
+echo >path0 'New line 1
+New line 2
+New line 3
+'
+test_expect_success 'Change path0.' '
+ git add path0 &&
+ git commit -m "Change path0"
+'
+
+cat <path0 >path1
+test_expect_success 'copy path0 to path1.' '
+ git add path1 &&
+ git commit -m "Copy path1 from path0"
+'
+
+test_expect_success 'find the copy path0 -> path1 harder' '
+ git log --follow --name-status --pretty="format:%s" path1 > current
+'
+
+cat >expected <<\EOF
+Copy path1 from path0
+C100 path0 path1
+
+Change path0
+M path0
+
+Add path0
+A path0
+EOF
+
+test_expect_success 'validate the output.' '
+ compare_diff_patch current expected
+'
+
+test_done
diff --git a/t/t4207-log-decoration-colors.sh b/t/t4207-log-decoration-colors.sh
new file mode 100755
index 0000000..21986a8
--- /dev/null
+++ b/t/t4207-log-decoration-colors.sh
@@ -0,0 +1,128 @@
+#!/bin/sh
+#
+# Copyright (c) 2010 Nazri Ramliy
+#
+
+test_description='test "git log --decorate" colors'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ git config diff.color.commit yellow &&
+ git config color.decorate.branch green &&
+ git config color.decorate.remoteBranch red &&
+ git config color.decorate.tag "reverse bold yellow" &&
+ git config color.decorate.stash magenta &&
+ git config color.decorate.grafted black &&
+ git config color.decorate.HEAD cyan &&
+
+ c_reset="<RESET>" &&
+
+ c_commit="<YELLOW>" &&
+ c_branch="<GREEN>" &&
+ c_remoteBranch="<RED>" &&
+ c_tag="<BOLD;REVERSE;YELLOW>" &&
+ c_stash="<MAGENTA>" &&
+ c_HEAD="<CYAN>" &&
+ c_grafted="<BLACK>" &&
+
+ test_commit A &&
+ git clone . other &&
+ (
+ cd other &&
+ test_commit A1
+ ) &&
+
+ git remote add -f other ./other &&
+ test_commit B &&
+ git tag v1.0 &&
+ echo >>A.t &&
+ git stash save Changes to A.t
+'
+
+cmp_filtered_decorations () {
+ sed "s/$OID_REGEX/COMMIT_ID/" actual | test_decode_color >filtered &&
+ test_cmp expect filtered
+}
+
+# We want log to show all, but the second parent to refs/stash is irrelevant
+# to this test since it does not contain any decoration, hence --first-parent
+test_expect_success 'commit decorations colored correctly' '
+ cat >expect <<-EOF &&
+ ${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_HEAD}HEAD${c_reset}\
+${c_commit} -> ${c_reset}${c_branch}main${c_reset}${c_commit}, \
+${c_reset}${c_tag}tag: ${c_reset}${c_tag}v1.0${c_reset}${c_commit}, \
+${c_reset}${c_tag}tag: ${c_reset}${c_tag}B${c_reset}${c_commit})${c_reset} B
+${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}\
+${c_tag}tag: ${c_reset}${c_tag}A1${c_reset}${c_commit}, \
+${c_reset}${c_remoteBranch}other/main${c_reset}${c_commit})${c_reset} A1
+ ${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}\
+${c_stash}refs/stash${c_reset}${c_commit})${c_reset} On main: Changes to A.t
+ ${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}\
+${c_tag}tag: ${c_reset}${c_tag}A${c_reset}${c_commit})${c_reset} A
+ EOF
+
+ git log --first-parent --no-abbrev --decorate --oneline --color=always --all >actual &&
+ cmp_filtered_decorations
+'
+
+test_expect_success 'test coloring with replace-objects' '
+ test_when_finished rm -rf .git/refs/replace* &&
+ test_commit C &&
+ test_commit D &&
+
+ git replace HEAD~1 HEAD~2 &&
+
+ cat >expect <<-EOF &&
+ ${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_HEAD}HEAD${c_reset}\
+${c_commit} -> ${c_reset}${c_branch}main${c_reset}${c_commit}, \
+${c_reset}${c_tag}tag: ${c_reset}${c_tag}D${c_reset}${c_commit})${c_reset} D
+ ${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}\
+${c_tag}tag: ${c_reset}${c_tag}C${c_reset}${c_commit}, \
+${c_reset}${c_grafted}replaced${c_reset}${c_commit})${c_reset} B
+ ${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}\
+${c_tag}tag: ${c_reset}${c_tag}A${c_reset}${c_commit})${c_reset} A
+EOF
+
+ git log --first-parent --no-abbrev --decorate --oneline --color=always HEAD >actual &&
+ cmp_filtered_decorations &&
+ git replace -d HEAD~1 &&
+
+ GIT_REPLACE_REF_BASE=refs/replace2/ git replace HEAD~1 HEAD~2 &&
+ GIT_REPLACE_REF_BASE=refs/replace2/ git log --first-parent \
+ --no-abbrev --decorate --oneline --color=always HEAD >actual &&
+ cmp_filtered_decorations
+'
+
+test_expect_success 'test coloring with grafted commit' '
+ test_when_finished rm -rf .git/refs/replace* &&
+
+ git replace --graft HEAD HEAD~2 &&
+
+ cat >expect <<-EOF &&
+ ${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_HEAD}HEAD${c_reset}\
+${c_commit} -> ${c_reset}${c_branch}main${c_reset}${c_commit}, \
+${c_reset}${c_tag}tag: ${c_reset}${c_tag}D${c_reset}${c_commit}, \
+${c_reset}${c_grafted}replaced${c_reset}${c_commit})${c_reset} D
+ ${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}\
+${c_tag}tag: ${c_reset}${c_tag}v1.0${c_reset}${c_commit}, \
+${c_reset}${c_tag}tag: ${c_reset}${c_tag}B${c_reset}${c_commit})${c_reset} B
+ ${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}\
+${c_tag}tag: ${c_reset}${c_tag}A${c_reset}${c_commit})${c_reset} A
+ EOF
+
+ git log --first-parent --no-abbrev --decorate --oneline --color=always HEAD >actual &&
+ cmp_filtered_decorations &&
+ git replace -d HEAD &&
+
+ GIT_REPLACE_REF_BASE=refs/replace2/ git replace --graft HEAD HEAD~2 &&
+ GIT_REPLACE_REF_BASE=refs/replace2/ git log --first-parent \
+ --no-abbrev --decorate --oneline --color=always HEAD >actual &&
+ cmp_filtered_decorations
+'
+
+test_done
diff --git a/t/t4208-log-magic-pathspec.sh b/t/t4208-log-magic-pathspec.sh
new file mode 100755
index 0000000..806b280
--- /dev/null
+++ b/t/t4208-log-magic-pathspec.sh
@@ -0,0 +1,145 @@
+#!/bin/sh
+
+test_description='magic pathspec tests using git-log'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ test_commit initial &&
+ test_tick &&
+ git commit --allow-empty -m empty &&
+ mkdir sub
+'
+
+test_expect_success '"git log :/" should not be ambiguous' '
+ git log :/
+'
+
+test_expect_success '"git log :/a" should be ambiguous (applied both rev and worktree)' '
+ : >a &&
+ test_must_fail git log :/a 2>error &&
+ test_grep ambiguous error
+'
+
+test_expect_success '"git log :/a -- " should not be ambiguous' '
+ git log :/a --
+'
+
+test_expect_success '"git log :/detached -- " should find a commit only in HEAD' '
+ test_when_finished "git checkout main" &&
+ git checkout --detach &&
+ test_commit --no-tag detached &&
+ test_commit --no-tag something-else &&
+ git log :/detached --
+'
+
+test_expect_success '"git log :/detached -- " should not find an orphaned commit' '
+ test_must_fail git log :/detached --
+'
+
+test_expect_success '"git log :/detached -- " should find HEAD only of own worktree' '
+ git worktree add other-tree HEAD &&
+ git -C other-tree checkout --detach &&
+ test_tick &&
+ git -C other-tree commit --allow-empty -m other-detached &&
+ git -C other-tree log :/other-detached -- &&
+ test_must_fail git log :/other-detached --
+'
+
+test_expect_success '"git log -- :/a" should not be ambiguous' '
+ git log -- :/a
+'
+
+test_expect_success '"git log :/any/path/" should not segfault' '
+ test_must_fail git log :/any/path/
+'
+
+# This differs from the ":/a" check above in that :/in looks like a pathspec,
+# but doesn't match an actual file.
+test_expect_success '"git log :/in" should not be ambiguous' '
+ git log :/in
+'
+
+test_expect_success '"git log :" should be ambiguous' '
+ test_must_fail git log : 2>error &&
+ test_grep ambiguous error
+'
+
+test_expect_success 'git log -- :' '
+ git log -- :
+'
+
+test_expect_success 'git log HEAD -- :/' '
+ initial=$(git rev-parse --short HEAD^) &&
+ cat >expected <<-EOF &&
+ $initial initial
+ EOF
+ (cd sub && git log --oneline HEAD -- :/ >../actual) &&
+ test_cmp expected actual
+'
+
+test_expect_success '"git log :^sub" is not ambiguous' '
+ git log :^sub
+'
+
+test_expect_success '"git log :^does-not-exist" does not match anything' '
+ test_must_fail git log :^does-not-exist
+'
+
+test_expect_success '"git log :!" behaves the same as :^' '
+ git log :!sub &&
+ test_must_fail git log :!does-not-exist
+'
+
+test_expect_success '"git log :(exclude)sub" is not ambiguous' '
+ git log ":(exclude)sub"
+'
+
+test_expect_success '"git log :(exclude)sub --" must resolve as an object' '
+ test_must_fail git log ":(exclude)sub" --
+'
+
+test_expect_success '"git log :(unknown-magic) complains of bogus magic' '
+ test_must_fail git log ":(unknown-magic)" 2>error &&
+ test_grep pathspec.magic error
+'
+
+test_expect_success 'command line pathspec parsing for "git log"' '
+ git reset --hard &&
+ >a &&
+ git add a &&
+ git commit -m "add an empty a" --allow-empty &&
+ echo 1 >a &&
+ git commit -a -m "update a to 1" &&
+ git checkout HEAD^ &&
+ echo 2 >a &&
+ git commit -a -m "update a to 2" &&
+ test_must_fail git merge main &&
+ git add a &&
+ git log --merge -- a
+'
+
+test_expect_success 'tree_entry_interesting does not match past submodule boundaries' '
+ test_when_finished "rm -rf repo submodule" &&
+ test_config_global protocol.file.allow always &&
+ git init submodule &&
+ test_commit -C submodule initial &&
+ git init repo &&
+ >"repo/[bracket]" &&
+ git -C repo add "[bracket]" &&
+ test_tick &&
+ git -C repo commit -m bracket &&
+ git -C repo rev-list HEAD -- "[bracket]" >expect &&
+
+ git -C repo submodule add ../submodule &&
+ test_tick &&
+ git -C repo commit -m submodule &&
+
+ git -C repo rev-list HEAD -- "[bracket]" >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t4209-log-pickaxe.sh b/t/t4209-log-pickaxe.sh
new file mode 100755
index 0000000..64e1623
--- /dev/null
+++ b/t/t4209-log-pickaxe.sh
@@ -0,0 +1,226 @@
+#!/bin/sh
+
+test_description='log --grep/--author/--regexp-ignore-case/-S/-G'
+. ./test-lib.sh
+
+test_log () {
+ expect=$1
+ kind=$2
+ needle=$3
+ shift 3
+ rest=$@
+
+ case $kind in
+ --*)
+ opt=$kind=$needle
+ ;;
+ *)
+ opt=$kind$needle
+ ;;
+ esac
+ case $expect in
+ expect_nomatch)
+ match=nomatch
+ ;;
+ *)
+ match=match
+ ;;
+ esac
+
+ test_expect_success "log $kind${rest:+ $rest} ($match)" "
+ git log $rest $opt --format=%H >actual &&
+ test_cmp $expect actual
+ "
+}
+
+# test -i and --regexp-ignore-case and expect both to behave the same way
+test_log_icase () {
+ test_log $@ --regexp-ignore-case
+ test_log $@ -i
+}
+
+test_expect_success setup '
+ >expect_nomatch &&
+
+ >file &&
+ git add file &&
+ test_tick &&
+ git commit -m initial &&
+ git rev-parse --verify HEAD >expect_initial &&
+
+ echo Picked >file &&
+ git add file &&
+ test_tick &&
+ git commit --author="Another Person <another@example.com>" -m second &&
+ git rev-parse --verify HEAD >expect_second
+'
+
+test_expect_success 'usage' '
+ test_expect_code 129 git log -S 2>err &&
+ test_grep "switch.*requires a value" err &&
+
+ test_expect_code 129 git log -G 2>err &&
+ test_grep "switch.*requires a value" err &&
+
+ test_expect_code 128 git log -Gregex -Sstring 2>err &&
+ grep "cannot be used together" err &&
+
+ test_expect_code 128 git log -Gregex --find-object=HEAD 2>err &&
+ grep "cannot be used together" err &&
+
+ test_expect_code 128 git log -Sstring --find-object=HEAD 2>err &&
+ grep "cannot be used together" err &&
+
+ test_expect_code 128 git log --pickaxe-all --find-object=HEAD 2>err &&
+ grep "cannot be used together" err
+'
+
+test_expect_success 'usage: --pickaxe-regex' '
+ test_expect_code 128 git log -Gregex --pickaxe-regex 2>err &&
+ grep "cannot be used together" err
+'
+
+test_expect_success 'usage: --no-pickaxe-regex' '
+ cat >expect <<-\EOF &&
+ fatal: unrecognized argument: --no-pickaxe-regex
+ EOF
+
+ test_expect_code 128 git log -Sstring --no-pickaxe-regex 2>actual &&
+ test_cmp expect actual &&
+
+ test_expect_code 128 git log -Gstring --no-pickaxe-regex 2>err &&
+ test_cmp expect actual
+'
+
+test_log expect_initial --grep initial
+test_log expect_nomatch --grep InItial
+test_log_icase expect_initial --grep InItial
+test_log_icase expect_nomatch --grep initail
+
+test_log expect_second --author Person
+test_log expect_nomatch --author person
+test_log_icase expect_second --author person
+test_log_icase expect_nomatch --author spreon
+
+test_log expect_nomatch -G picked
+test_log expect_second -G Picked
+test_log_icase expect_nomatch -G pickle
+test_log_icase expect_second -G picked
+
+test_expect_success 'log -G --textconv (missing textconv tool)' '
+ echo "* diff=test" >.gitattributes &&
+ test_must_fail git -c diff.test.textconv=missing log -Gfoo &&
+ rm .gitattributes
+'
+
+test_expect_success 'log -G --no-textconv (missing textconv tool)' '
+ echo "* diff=test" >.gitattributes &&
+ git -c diff.test.textconv=missing log -Gfoo --no-textconv >actual &&
+ test_cmp expect_nomatch actual &&
+ rm .gitattributes
+'
+
+test_log expect_nomatch -S picked
+test_log expect_second -S Picked
+test_log_icase expect_second -S picked
+test_log_icase expect_nomatch -S pickle
+
+test_log expect_nomatch -S p.cked --pickaxe-regex
+test_log expect_second -S P.cked --pickaxe-regex
+test_log_icase expect_second -S p.cked --pickaxe-regex
+test_log_icase expect_nomatch -S p.ckle --pickaxe-regex
+
+test_expect_success 'log -S --textconv (missing textconv tool)' '
+ echo "* diff=test" >.gitattributes &&
+ test_must_fail git -c diff.test.textconv=missing log -Sfoo &&
+ rm .gitattributes
+'
+
+test_expect_success 'log -S --no-textconv (missing textconv tool)' '
+ echo "* diff=test" >.gitattributes &&
+ git -c diff.test.textconv=missing log -Sfoo --no-textconv >actual &&
+ test_cmp expect_nomatch actual &&
+ rm .gitattributes
+'
+
+test_expect_success 'setup log -[GS] plain & regex' '
+ test_create_repo GS-plain &&
+ test_commit -C GS-plain --append A data.txt "a" &&
+ test_commit -C GS-plain --append B data.txt "a a" &&
+ test_commit -C GS-plain --append C data.txt "b" &&
+ test_commit -C GS-plain --append D data.txt "[b]" &&
+ test_commit -C GS-plain E data.txt "" &&
+
+ # We also include E, the deletion commit
+ git -C GS-plain log --grep="[ABE]" >A-to-B-then-E-log &&
+ git -C GS-plain log --grep="[CDE]" >C-to-D-then-E-log &&
+ git -C GS-plain log --grep="[DE]" >D-then-E-log &&
+ git -C GS-plain log >full-log
+'
+
+test_expect_success 'log -G trims diff new/old [-+]' '
+ git -C GS-plain log -G"[+-]a" >log &&
+ test_must_be_empty log &&
+ git -C GS-plain log -G"^a" >log &&
+ test_cmp log A-to-B-then-E-log
+'
+
+test_expect_success 'log -S<pat> is not a regex, but -S<pat> --pickaxe-regex is' '
+ git -C GS-plain log -S"a" >log &&
+ test_cmp log A-to-B-then-E-log &&
+
+ git -C GS-plain log -S"[a]" >log &&
+ test_must_be_empty log &&
+
+ git -C GS-plain log -S"[a]" --pickaxe-regex >log &&
+ test_cmp log A-to-B-then-E-log &&
+
+ git -C GS-plain log -S"[b]" >log &&
+ test_cmp log D-then-E-log &&
+
+ git -C GS-plain log -S"[b]" --pickaxe-regex >log &&
+ test_cmp log C-to-D-then-E-log
+'
+
+test_expect_success 'setup log -[GS] binary & --text' '
+ test_create_repo GS-bin-txt &&
+ test_commit -C GS-bin-txt --printf A data.bin "a\na\0a\n" &&
+ test_commit -C GS-bin-txt --append --printf B data.bin "a\na\0a\n" &&
+ test_commit -C GS-bin-txt C data.bin "" &&
+ git -C GS-bin-txt log >full-log
+'
+
+test_expect_success 'log -G ignores binary files' '
+ git -C GS-bin-txt log -Ga >log &&
+ test_must_be_empty log
+'
+
+test_expect_success 'log -G looks into binary files with -a' '
+ git -C GS-bin-txt log -a -Ga >log &&
+ test_cmp log full-log
+'
+
+test_expect_success 'log -G looks into binary files with textconv filter' '
+ test_when_finished "rm GS-bin-txt/.gitattributes" &&
+ (
+ cd GS-bin-txt &&
+ echo "* diff=bin" >.gitattributes &&
+ git -c diff.bin.textconv=cat log -Ga >../log
+ ) &&
+ test_cmp log full-log
+'
+
+test_expect_success 'log -S looks into binary files' '
+ git -C GS-bin-txt log -Sa >log &&
+ test_cmp log full-log
+'
+
+test_expect_success 'log -S --pickaxe-regex looks into binary files' '
+ git -C GS-bin-txt log --pickaxe-regex -Sa >log &&
+ test_cmp log full-log &&
+
+ git -C GS-bin-txt log --pickaxe-regex -S"[a]" >log &&
+ test_cmp log full-log
+'
+
+test_done
diff --git a/t/t4210-log-i18n.sh b/t/t4210-log-i18n.sh
new file mode 100755
index 0000000..d2dfcf1
--- /dev/null
+++ b/t/t4210-log-i18n.sh
@@ -0,0 +1,134 @@
+#!/bin/sh
+
+test_description='test log with i18n features'
+. ./lib-gettext.sh
+
+# two forms of é
+utf8_e=$(printf '\303\251')
+latin1_e=$(printf '\351')
+
+# invalid UTF-8
+invalid_e=$(printf '\303\50)') # ")" at end to close opening "("
+
+have_reg_illseq=
+if test_have_prereq GETTEXT_LOCALE &&
+ ! LC_ALL=$is_IS_locale test-tool regex --silent $latin1_e
+then
+ have_reg_illseq=1
+fi
+
+test_expect_success 'create commits in different encodings' '
+ test_tick &&
+ cat >msg <<-EOF &&
+ utf8
+
+ t${utf8_e}st
+ EOF
+ git add msg &&
+ git -c i18n.commitencoding=utf8 commit -F msg &&
+ cat >msg <<-EOF &&
+ latin1
+
+ t${latin1_e}st
+ EOF
+ git add msg &&
+ git -c i18n.commitencoding=ISO-8859-1 commit -F msg
+'
+
+test_expect_success 'log --grep searches in log output encoding (utf8)' '
+ cat >expect <<-\EOF &&
+ latin1
+ utf8
+ EOF
+ git log --encoding=utf8 --format=%s --grep=$utf8_e >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success !MINGW 'log --grep searches in log output encoding (latin1)' '
+ cat >expect <<-\EOF &&
+ latin1
+ utf8
+ EOF
+ git log --encoding=ISO-8859-1 --format=%s --grep=$latin1_e >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success !MINGW 'log --grep does not find non-reencoded values (utf8)' '
+ git log --encoding=utf8 --format=%s --grep=$latin1_e >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'log --grep does not find non-reencoded values (latin1)' '
+ git log --encoding=ISO-8859-1 --format=%s --grep=$utf8_e >actual &&
+ test_must_be_empty actual
+'
+
+triggers_undefined_behaviour () {
+ local engine=$1
+
+ case $engine in
+ fixed)
+ if test -n "$have_reg_illseq" &&
+ ! test_have_prereq LIBPCRE2
+ then
+ return 0
+ fi
+ ;;
+ basic|extended)
+ if test -n "$have_reg_illseq"
+ then
+ return 0
+ fi
+ ;;
+ esac
+ return 1
+}
+
+mismatched_git_log () {
+ local pattern=$1
+
+ LC_ALL=$is_IS_locale git log --encoding=ISO-8859-1 --format=%s \
+ --grep=$pattern
+}
+
+for engine in fixed basic extended perl
+do
+ prereq=
+ if test $engine = "perl"
+ then
+ prereq=PCRE
+ fi
+ force_regex=
+ if test $engine != "fixed"
+ then
+ force_regex='.*'
+ fi
+
+ test_expect_success $prereq "config grep.patternType=$engine" "
+ git config grep.patternType $engine
+ "
+
+ test_expect_success GETTEXT_LOCALE,$prereq "log --grep does not find non-reencoded values (latin1 + locale)" "
+ mismatched_git_log '$force_regex$utf8_e' >actual &&
+ test_must_be_empty actual
+ "
+
+ if ! triggers_undefined_behaviour $engine
+ then
+ test_expect_success !MINGW,GETTEXT_LOCALE,$prereq "log --grep searches in log output encoding (latin1 + locale)" "
+ cat >expect <<-\EOF &&
+ latin1
+ utf8
+ EOF
+ mismatched_git_log '$force_regex$latin1_e' >actual &&
+ test_cmp expect actual
+ "
+
+ test_expect_success GETTEXT_LOCALE,$prereq "log --grep does not die on invalid UTF-8 value (latin1 + locale + invalid needle)" "
+ mismatched_git_log '$force_regex$invalid_e' >actual &&
+ test_must_be_empty actual
+ "
+ fi
+done
+
+test_done
diff --git a/t/t4211-line-log.sh b/t/t4211-line-log.sh
new file mode 100755
index 0000000..02d76dc
--- /dev/null
+++ b/t/t4211-line-log.sh
@@ -0,0 +1,340 @@
+#!/bin/sh
+
+test_description='test log -L'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success 'setup (import history)' '
+ git fast-import < "$TEST_DIRECTORY"/t4211/history.export &&
+ git reset --hard
+'
+
+test_expect_success 'basic command line parsing' '
+ # This may fail due to "no such path a.c in commit", or
+ # "-L is incompatible with pathspec", depending on the
+ # order the error is checked. Either is acceptable.
+ test_must_fail git log -L1,1:a.c -- a.c &&
+
+ # -L requires there is no pathspec
+ test_must_fail git log -L1,1:b.c -- b.c 2>error &&
+ test_grep "cannot be used with pathspec" error &&
+
+ # This would fail because --follow wants a single path, but
+ # we may fail due to incompatibility between -L/--follow in
+ # the future. Either is acceptable.
+ test_must_fail git log -L1,1:b.c --follow &&
+ test_must_fail git log --follow -L1,1:b.c &&
+
+ # This would fail because -L wants no pathspec, but
+ # we may fail due to incompatibility between -L/--follow in
+ # the future. Either is acceptable.
+ test_must_fail git log --follow -L1,1:b.c -- b.c
+'
+
+canned_test_1 () {
+ test_expect_$1 "$2" "
+ git log $2 >actual &&
+ test_cmp \"\$TEST_DIRECTORY\"/t4211/$(test_oid algo)/expect.$3 actual
+ "
+}
+
+canned_test () {
+ canned_test_1 success "$@"
+}
+canned_test_failure () {
+ canned_test_1 failure "$@"
+}
+
+test_bad_opts () {
+ test_expect_success "invalid args: $1" "
+ test_must_fail git log $1 2>errors &&
+ test_grep '$2' errors
+ "
+}
+
+canned_test "-L 4,12:a.c simple" simple-f
+canned_test "-L 4,+9:a.c simple" simple-f
+canned_test "-L '/long f/,/^}/:a.c' simple" simple-f
+canned_test "-L :f:a.c simple" simple-f-to-main
+
+canned_test "-L '/main/,/^}/:a.c' simple" simple-main
+canned_test "-L :main:a.c simple" simple-main-to-end
+
+canned_test "-L 1,+4:a.c simple" beginning-of-file
+
+canned_test "-L 20:a.c simple" end-of-file
+
+canned_test "-L '/long f/',/^}/:a.c -L /main/,/^}/:a.c simple" two-ranges
+canned_test "-L 24,+1:a.c simple" vanishes-early
+
+canned_test "-M -L '/long f/,/^}/:b.c' move-support" move-support-f
+canned_test "-M -L ':f:b.c' parallel-change" parallel-change-f-to-main
+
+canned_test "-L 4,12:a.c -L :main:a.c simple" multiple
+canned_test "-L 4,18:a.c -L ^:main:a.c simple" multiple-overlapping
+canned_test "-L :main:a.c -L 4,18:a.c simple" multiple-overlapping
+canned_test "-L 4:a.c -L 8,12:a.c simple" multiple-superset
+canned_test "-L 8,12:a.c -L 4:a.c simple" multiple-superset
+
+test_bad_opts "-L" "switch.*requires a value"
+test_bad_opts "-L b.c" "argument not .start,end:file"
+test_bad_opts "-L 1:" "argument not .start,end:file"
+test_bad_opts "-L 1:nonexistent" "There is no path"
+test_bad_opts "-L 1:simple" "There is no path"
+test_bad_opts "-L '/foo:b.c'" "argument not .start,end:file"
+test_bad_opts "-L 1000:b.c" "has only.*lines"
+test_bad_opts "-L :b.c" "argument not .start,end:file"
+test_bad_opts "-L :foo:b.c" "no match"
+
+test_expect_success '-L X (X == nlines)' '
+ n=$(wc -l <b.c) &&
+ git log -L $n:b.c
+'
+
+test_expect_success '-L X (X == nlines + 1)' '
+ n=$(expr $(wc -l <b.c) + 1) &&
+ test_must_fail git log -L $n:b.c
+'
+
+test_expect_success '-L X (X == nlines + 2)' '
+ n=$(expr $(wc -l <b.c) + 2) &&
+ test_must_fail git log -L $n:b.c
+'
+
+test_expect_success '-L ,Y (Y == nlines)' '
+ n=$(printf "%d" $(wc -l <b.c)) &&
+ git log -L ,$n:b.c
+'
+
+test_expect_success '-L ,Y (Y == nlines + 1)' '
+ n=$(expr $(wc -l <b.c) + 1) &&
+ git log -L ,$n:b.c
+'
+
+test_expect_success '-L ,Y (Y == nlines + 2)' '
+ n=$(expr $(wc -l <b.c) + 2) &&
+ git log -L ,$n:b.c
+'
+
+test_expect_success '-L with --first-parent and a merge' '
+ git checkout parallel-change &&
+ git log --first-parent -L 1,1:b.c
+'
+
+test_expect_success '-L with --output' '
+ git checkout parallel-change &&
+ git log --output=log -L :main:b.c >output &&
+ test_must_be_empty output &&
+ test_line_count = 70 log
+'
+
+test_expect_success 'range_set_union' '
+ test_seq 500 > c.c &&
+ git add c.c &&
+ git commit -m "many lines" &&
+ test_seq 1000 > c.c &&
+ git add c.c &&
+ git commit -m "modify many lines" &&
+ git log $(for x in $(test_seq 200); do echo -L $((2*x)),+1:c.c || return 1; done)
+'
+
+test_expect_success '-s shows only line-log commits' '
+ git log --format="commit %s" -L1,24:b.c >expect.raw &&
+ grep ^commit expect.raw >expect &&
+ git log --format="commit %s" -L1,24:b.c -s >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '-p shows the default patch output' '
+ git log -L1,24:b.c >expect &&
+ git log -L1,24:b.c -p >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--raw is forbidden' '
+ test_must_fail git log -L1,24:b.c --raw
+'
+
+test_expect_success 'setup for checking fancy rename following' '
+ git checkout --orphan moves-start &&
+ git reset --hard &&
+
+ printf "%s\n" 12 13 14 15 b c d e >file-1 &&
+ printf "%s\n" 22 23 24 25 B C D E >file-2 &&
+ git add file-1 file-2 &&
+ test_tick &&
+ git commit -m "Add file-1 and file-2" &&
+ oid_add_f1_f2=$(git rev-parse --short HEAD) &&
+
+ git checkout -b moves-main &&
+ printf "%s\n" 11 12 13 14 15 b c d e >file-1 &&
+ git commit -a -m "Modify file-1 on main" &&
+ oid_mod_f1_main=$(git rev-parse --short HEAD) &&
+
+ printf "%s\n" 21 22 23 24 25 B C D E >file-2 &&
+ git commit -a -m "Modify file-2 on main #1" &&
+ oid_mod_f2_main_1=$(git rev-parse --short HEAD) &&
+
+ git mv file-1 renamed-1 &&
+ git commit -m "Rename file-1 to renamed-1 on main" &&
+
+ printf "%s\n" 11 12 13 14 15 b c d e f >renamed-1 &&
+ git commit -a -m "Modify renamed-1 on main" &&
+ oid_mod_r1_main=$(git rev-parse --short HEAD) &&
+
+ printf "%s\n" 21 22 23 24 25 B C D E F >file-2 &&
+ git commit -a -m "Modify file-2 on main #2" &&
+ oid_mod_f2_main_2=$(git rev-parse --short HEAD) &&
+
+ git checkout -b moves-side moves-start &&
+ printf "%s\n" 12 13 14 15 16 b c d e >file-1 &&
+ git commit -a -m "Modify file-1 on side #1" &&
+ oid_mod_f1_side_1=$(git rev-parse --short HEAD) &&
+
+ printf "%s\n" 22 23 24 25 26 B C D E >file-2 &&
+ git commit -a -m "Modify file-2 on side" &&
+ oid_mod_f2_side=$(git rev-parse --short HEAD) &&
+
+ git mv file-2 renamed-2 &&
+ git commit -m "Rename file-2 to renamed-2 on side" &&
+
+ printf "%s\n" 12 13 14 15 16 a b c d e >file-1 &&
+ git commit -a -m "Modify file-1 on side #2" &&
+ oid_mod_f1_side_2=$(git rev-parse --short HEAD) &&
+
+ printf "%s\n" 22 23 24 25 26 A B C D E >renamed-2 &&
+ git commit -a -m "Modify renamed-2 on side" &&
+ oid_mod_r2_side=$(git rev-parse --short HEAD) &&
+
+ git checkout moves-main &&
+ git merge moves-side &&
+ oid_merge=$(git rev-parse --short HEAD)
+'
+
+test_expect_success 'fancy rename following #1' '
+ cat >expect <<-EOF &&
+ $oid_merge Merge branch '\''moves-side'\'' into moves-main
+ $oid_mod_f1_side_2 Modify file-1 on side #2
+ $oid_mod_f1_side_1 Modify file-1 on side #1
+ $oid_mod_r1_main Modify renamed-1 on main
+ $oid_mod_f1_main Modify file-1 on main
+ $oid_add_f1_f2 Add file-1 and file-2
+ EOF
+ git log -L1:renamed-1 --oneline --no-patch >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'fancy rename following #2' '
+ cat >expect <<-EOF &&
+ $oid_merge Merge branch '\''moves-side'\'' into moves-main
+ $oid_mod_r2_side Modify renamed-2 on side
+ $oid_mod_f2_side Modify file-2 on side
+ $oid_mod_f2_main_2 Modify file-2 on main #2
+ $oid_mod_f2_main_1 Modify file-2 on main #1
+ $oid_add_f1_f2 Add file-1 and file-2
+ EOF
+ git log -L1:renamed-2 --oneline --no-patch >actual &&
+ test_cmp expect actual
+'
+
+# Create the following linear history, where each commit does what its
+# subject line promises:
+#
+# * 66c6410 Modify func2() in file.c
+# * 50834e5 Modify other-file
+# * fe5851c Modify func1() in file.c
+# * 8c7c7dd Add other-file
+# * d5f4417 Add func1() and func2() in file.c
+test_expect_success 'setup for checking line-log and parent oids' '
+ git checkout --orphan parent-oids &&
+ git reset --hard &&
+
+ cat >file.c <<-\EOF &&
+ int func1()
+ {
+ return F1;
+ }
+
+ int func2()
+ {
+ return F2;
+ }
+ EOF
+ git add file.c &&
+ test_tick &&
+ first_tick=$test_tick &&
+ git commit -m "Add func1() and func2() in file.c" &&
+
+ echo 1 >other-file &&
+ git add other-file &&
+ test_tick &&
+ git commit -m "Add other-file" &&
+
+ sed -e "s/F1/F1 + 1/" file.c >tmp &&
+ mv tmp file.c &&
+ git commit -a -m "Modify func1() in file.c" &&
+
+ echo 2 >other-file &&
+ git commit -a -m "Modify other-file" &&
+
+ sed -e "s/F2/F2 + 2/" file.c >tmp &&
+ mv tmp file.c &&
+ git commit -a -m "Modify func2() in file.c" &&
+
+ head_oid=$(git rev-parse --short HEAD) &&
+ prev_oid=$(git rev-parse --short HEAD^) &&
+ root_oid=$(git rev-parse --short HEAD~4)
+'
+
+# Parent oid should be from immediate parent.
+test_expect_success 'parent oids without parent rewriting' '
+ cat >expect <<-EOF &&
+ $head_oid $prev_oid Modify func2() in file.c
+ $root_oid Add func1() and func2() in file.c
+ EOF
+ git log --format="%h %p %s" --no-patch -L:func2:file.c >actual &&
+ test_cmp expect actual
+'
+
+# Parent oid should be from the most recent ancestor touching func2(),
+# i.e. in this case from the root commit.
+test_expect_success 'parent oids with parent rewriting' '
+ cat >expect <<-EOF &&
+ $head_oid $root_oid Modify func2() in file.c
+ $root_oid Add func1() and func2() in file.c
+ EOF
+ git log --format="%h %p %s" --no-patch -L:func2:file.c --parents >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'line-log with --before' '
+ echo $root_oid >expect &&
+ git log --format=%h --no-patch -L:func2:file.c --before=$first_tick >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'setup tests for zero-width regular expressions' '
+ cat >expect <<-EOF
+ Modify func1() in file.c
+ Add func1() and func2() in file.c
+ EOF
+'
+
+test_expect_success 'zero-width regex $ matches any function name' '
+ git log --format="%s" --no-patch "-L:$:file.c" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'zero-width regex ^ matches any function name' '
+ git log --format="%s" --no-patch "-L:^:file.c" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'zero-width regex .* matches any function name' '
+ git log --format="%s" --no-patch "-L:.*:file.c" >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t4211/history.export b/t/t4211/history.export
new file mode 100644
index 0000000..006972a
--- /dev/null
+++ b/t/t4211/history.export
@@ -0,0 +1,406 @@
+blob
+mark :1
+data 157
+#include <stdio.h>
+
+int f(int x)
+{
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+}
+
+/*
+ * A comment.
+ */
+
+int main ()
+{
+ printf("%d\n", f(15));
+ return 0;
+}
+
+reset refs/tags/simple
+commit refs/tags/simple
+mark :2
+author Thomas Rast <trast@student.ethz.ch> 1362044688 +0100
+committer Thomas Rast <trast@student.ethz.ch> 1362044688 +0100
+data 8
+initial
+M 100644 :1 a.c
+
+blob
+mark :3
+data 168
+#include <stdio.h>
+
+int f(int x)
+{
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+ return s;
+}
+
+/*
+ * A comment.
+ */
+
+int main ()
+{
+ printf("%d\n", f(15));
+ return 0;
+}
+
+commit refs/tags/simple
+mark :4
+author Thomas Rast <trast@student.ethz.ch> 1362044695 +0100
+committer Thomas Rast <trast@student.ethz.ch> 1362044695 +0100
+data 11
+change f()
+from :2
+M 100644 :3 a.c
+
+blob
+mark :5
+data 171
+#include <stdio.h>
+
+long f(long x)
+{
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+ return s;
+}
+
+/*
+ * A comment.
+ */
+
+int main ()
+{
+ printf("%ld\n", f(15));
+ return 0;
+}
+
+commit refs/tags/simple
+mark :6
+author Thomas Rast <trast@student.ethz.ch> 1362044716 +0100
+committer Thomas Rast <trast@student.ethz.ch> 1362044716 +0100
+data 21
+touch both functions
+from :4
+M 100644 :5 a.c
+
+blob
+mark :7
+data 185
+#include <stdio.h>
+
+long f(long x)
+{
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+ return s;
+}
+
+/*
+ * This is only an example!
+ */
+
+int main ()
+{
+ printf("%ld\n", f(15));
+ return 0;
+}
+
+commit refs/tags/simple
+mark :8
+author Thomas Rast <trast@student.ethz.ch> 1362044741 +0100
+committer Thomas Rast <trast@student.ethz.ch> 1362044741 +0100
+data 14
+touch comment
+from :6
+M 100644 :7 a.c
+
+blob
+mark :9
+data 205
+#include <unistd.h>
+#include <stdio.h>
+
+long f(long x)
+{
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+ return s;
+}
+
+/*
+ * This is only an example!
+ */
+
+int main ()
+{
+ printf("%ld\n", f(15));
+ return 0;
+}
+
+commit refs/tags/simple
+mark :10
+author Thomas Rast <trast@student.ethz.ch> 1362044860 +0100
+committer Thomas Rast <trast@student.ethz.ch> 1362044860 +0100
+data 25
+change at very beginning
+from :8
+M 100644 :9 a.c
+
+blob
+mark :11
+data 204
+#include <unistd.h>
+#include <stdio.h>
+
+long f(long x)
+{
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+ return s;
+}
+
+/*
+ * This is only an example!
+ */
+
+int main ()
+{
+ printf("%ld\n", f(15));
+ return 0;
+}
+commit refs/tags/simple
+mark :12
+author Thomas Rast <trast@student.ethz.ch> 1362044890 +0100
+committer Thomas Rast <trast@student.ethz.ch> 1362044890 +0100
+data 36
+change to an incomplete line at end
+from :10
+M 100644 :11 a.c
+
+blob
+mark :13
+data 238
+#include <unistd.h>
+#include <stdio.h>
+
+long f(long x)
+{
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+ return s;
+}
+
+/*
+ * This is only an example!
+ */
+
+int main ()
+{
+ printf("%ld\n", f(15));
+ return 0;
+}
+
+/* incomplete lines are bad! */
+
+commit refs/tags/simple
+mark :14
+author Thomas Rast <trast@student.ethz.ch> 1362044923 +0100
+committer Thomas Rast <trast@student.ethz.ch> 1362044923 +0100
+data 29
+change back to complete line
+from :12
+M 100644 :13 a.c
+
+commit refs/tags/move-support
+mark :15
+author Thomas Rast <trast@student.ethz.ch> 1362044968 +0100
+committer Thomas Rast <trast@student.ethz.ch> 1362044968 +0100
+data 10
+move file
+from :14
+D a.c
+M 100644 :13 b.c
+
+blob
+mark :16
+data 237
+#include <unistd.h>
+#include <stdio.h>
+
+long f(long x)
+{
+ int s = 0;
+ while (x) {
+ x /= 2;
+ s++;
+ }
+ return s;
+}
+
+/*
+ * This is only an example!
+ */
+
+int main ()
+{
+ printf("%ld\n", f(15));
+ return 0;
+}
+
+/* incomplete lines are bad! */
+
+commit refs/tags/move-support
+mark :17
+author Thomas Rast <trast@student.ethz.ch> 1362044990 +0100
+committer Thomas Rast <trast@student.ethz.ch> 1362044990 +0100
+data 22
+another simple change
+from :15
+M 100644 :16 b.c
+
+blob
+mark :18
+data 254
+#include <unistd.h>
+#include <stdio.h>
+
+long f(long x);
+
+/*
+ * This is only an example!
+ */
+
+int main ()
+{
+ printf("%ld\n", f(15));
+ return 0;
+}
+
+/* incomplete lines are bad! */
+
+long f(long x)
+{
+ int s = 0;
+ while (x) {
+ x /= 2;
+ s++;
+ }
+ return s;
+}
+
+commit refs/heads/main
+mark :19
+author Thomas Rast <trast@student.ethz.ch> 1362045024 +0100
+committer Thomas Rast <trast@student.ethz.ch> 1362045024 +0100
+data 21
+move within the file
+from :17
+M 100644 :18 b.c
+
+blob
+mark :20
+data 243
+#include <unistd.h>
+#include <stdio.h>
+
+long f(long x)
+{
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+ return s;
+}
+
+/*
+ * This is only a short example!
+ */
+
+int main ()
+{
+ printf("%ld\n", f(15));
+ return 0;
+}
+
+/* incomplete lines are bad! */
+
+commit refs/heads/parallel-change
+mark :21
+author Thomas Rast <trast@inf.ethz.ch> 1365776157 +0200
+committer Thomas Rast <trast@inf.ethz.ch> 1365776157 +0200
+data 55
+change on another line of history while rename happens
+from :14
+M 100644 :20 a.c
+
+blob
+mark :22
+data 242
+#include <unistd.h>
+#include <stdio.h>
+
+long f(long x)
+{
+ int s = 0;
+ while (x) {
+ x /= 2;
+ s++;
+ }
+ return s;
+}
+
+/*
+ * This is only a short example!
+ */
+
+int main ()
+{
+ printf("%ld\n", f(15));
+ return 0;
+}
+
+/* incomplete lines are bad! */
+
+commit refs/heads/parallel-change
+mark :23
+author Thomas Rast <trast@inf.ethz.ch> 1365776184 +0200
+committer Thomas Rast <trast@inf.ethz.ch> 1365776191 +0200
+data 24
+Merge across the rename
+from :21
+merge :17
+D a.c
+M 100644 :22 b.c
+
+reset refs/heads/parallel-change
+from :23
+
diff --git a/t/t4211/sha1/expect.beginning-of-file b/t/t4211/sha1/expect.beginning-of-file
new file mode 100644
index 0000000..91b4054
--- /dev/null
+++ b/t/t4211/sha1/expect.beginning-of-file
@@ -0,0 +1,43 @@
+commit 4a23ae5c98d59a58c6da036156959f2dc9f472ad
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:47:40 2013 +0100
+
+ change at very beginning
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -1,3 +1,4 @@
++#include <unistd.h>
+ #include <stdio.h>
+
+ long f(long x)
+
+commit a6eb82647d5d67f893da442f8f9375fd89a3b1e2
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:45:16 2013 +0100
+
+ touch both functions
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -1,3 +1,3 @@
+ #include <stdio.h>
+
+-int f(int x)
++long f(long x)
+
+commit de4c48ae814792c02a49c4c3c0c757ae69c55f6a
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:48 2013 +0100
+
+ initial
+
+diff --git a/a.c b/a.c
+--- /dev/null
++++ b/a.c
+@@ -0,0 +1,3 @@
++#include <stdio.h>
++
++int f(int x)
diff --git a/t/t4211/sha1/expect.end-of-file b/t/t4211/sha1/expect.end-of-file
new file mode 100644
index 0000000..bd25bb2
--- /dev/null
+++ b/t/t4211/sha1/expect.end-of-file
@@ -0,0 +1,62 @@
+commit 4659538844daa2849b1a9e7d6fadb96fcd26fc83
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:48:43 2013 +0100
+
+ change back to complete line
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -20,3 +20,5 @@
+ printf("%ld\n", f(15));
+ return 0;
+-}
+\ No newline at end of file
++}
++
++/* incomplete lines are bad! */
+
+commit 100b61a6f2f720f812620a9d10afb3a960ccb73c
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:48:10 2013 +0100
+
+ change to an incomplete line at end
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -20,3 +20,3 @@
+ printf("%ld\n", f(15));
+ return 0;
+-}
++}
+\ No newline at end of file
+
+commit a6eb82647d5d67f893da442f8f9375fd89a3b1e2
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:45:16 2013 +0100
+
+ touch both functions
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -19,3 +19,3 @@
+- printf("%d\n", f(15));
++ printf("%ld\n", f(15));
+ return 0;
+ }
+
+commit de4c48ae814792c02a49c4c3c0c757ae69c55f6a
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:48 2013 +0100
+
+ initial
+
+diff --git a/a.c b/a.c
+--- /dev/null
++++ b/a.c
+@@ -0,0 +18,3 @@
++ printf("%d\n", f(15));
++ return 0;
++}
diff --git a/t/t4211/sha1/expect.move-support-f b/t/t4211/sha1/expect.move-support-f
new file mode 100644
index 0000000..c905e01
--- /dev/null
+++ b/t/t4211/sha1/expect.move-support-f
@@ -0,0 +1,80 @@
+commit 6ce3c4ff690136099bb17e1a8766b75764726ea7
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:49:50 2013 +0100
+
+ another simple change
+
+diff --git a/b.c b/b.c
+--- a/b.c
++++ b/b.c
+@@ -4,9 +4,9 @@
+ long f(long x)
+ {
+ int s = 0;
+ while (x) {
+- x >>= 1;
++ x /= 2;
+ s++;
+ }
+ return s;
+ }
+
+commit a6eb82647d5d67f893da442f8f9375fd89a3b1e2
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:45:16 2013 +0100
+
+ touch both functions
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -3,9 +3,9 @@
+-int f(int x)
++long f(long x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+ return s;
+ }
+
+commit f04fb20f2c77850996cba739709acc6faecc58f7
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:55 2013 +0100
+
+ change f()
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -3,8 +3,9 @@
+ int f(int x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
++ return s;
+ }
+
+commit de4c48ae814792c02a49c4c3c0c757ae69c55f6a
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:48 2013 +0100
+
+ initial
+
+diff --git a/a.c b/a.c
+--- /dev/null
++++ b/a.c
+@@ -0,0 +3,8 @@
++int f(int x)
++{
++ int s = 0;
++ while (x) {
++ x >>= 1;
++ s++;
++ }
++}
diff --git a/t/t4211/sha1/expect.multiple b/t/t4211/sha1/expect.multiple
new file mode 100644
index 0000000..76ad5b5
--- /dev/null
+++ b/t/t4211/sha1/expect.multiple
@@ -0,0 +1,104 @@
+commit 4659538844daa2849b1a9e7d6fadb96fcd26fc83
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:48:43 2013 +0100
+
+ change back to complete line
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -18,5 +18,7 @@
+ int main ()
+ {
+ printf("%ld\n", f(15));
+ return 0;
+-}
+\ No newline at end of file
++}
++
++/* incomplete lines are bad! */
+
+commit 100b61a6f2f720f812620a9d10afb3a960ccb73c
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:48:10 2013 +0100
+
+ change to an incomplete line at end
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -18,5 +18,5 @@
+ int main ()
+ {
+ printf("%ld\n", f(15));
+ return 0;
+-}
++}
+\ No newline at end of file
+
+commit a6eb82647d5d67f893da442f8f9375fd89a3b1e2
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:45:16 2013 +0100
+
+ touch both functions
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -3,9 +3,9 @@
+-int f(int x)
++long f(long x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+ return s;
+ }
+@@ -17,5 +17,5 @@
+ int main ()
+ {
+- printf("%d\n", f(15));
++ printf("%ld\n", f(15));
+ return 0;
+ }
+
+commit f04fb20f2c77850996cba739709acc6faecc58f7
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:55 2013 +0100
+
+ change f()
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -3,8 +3,9 @@
+ int f(int x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
++ return s;
+ }
+
+commit de4c48ae814792c02a49c4c3c0c757ae69c55f6a
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:48 2013 +0100
+
+ initial
+
+diff --git a/a.c b/a.c
+--- /dev/null
++++ b/a.c
+@@ -0,0 +3,8 @@
++int f(int x)
++{
++ int s = 0;
++ while (x) {
++ x >>= 1;
++ s++;
++ }
++}
diff --git a/t/t4211/sha1/expect.multiple-overlapping b/t/t4211/sha1/expect.multiple-overlapping
new file mode 100644
index 0000000..d930b6e
--- /dev/null
+++ b/t/t4211/sha1/expect.multiple-overlapping
@@ -0,0 +1,187 @@
+commit 4659538844daa2849b1a9e7d6fadb96fcd26fc83
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:48:43 2013 +0100
+
+ change back to complete line
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -4,19 +4,21 @@
+ long f(long x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+ return s;
+ }
+
+ /*
+ * This is only an example!
+ */
+
+ int main ()
+ {
+ printf("%ld\n", f(15));
+ return 0;
+-}
+\ No newline at end of file
++}
++
++/* incomplete lines are bad! */
+
+commit 100b61a6f2f720f812620a9d10afb3a960ccb73c
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:48:10 2013 +0100
+
+ change to an incomplete line at end
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -4,19 +4,19 @@
+ long f(long x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+ return s;
+ }
+
+ /*
+ * This is only an example!
+ */
+
+ int main ()
+ {
+ printf("%ld\n", f(15));
+ return 0;
+-}
++}
+\ No newline at end of file
+
+commit 39b6eb2d5b706d3322184a169f666f25ed3fbd00
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:45:41 2013 +0100
+
+ touch comment
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -3,19 +3,19 @@
+ long f(long x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+ return s;
+ }
+
+ /*
+- * A comment.
++ * This is only an example!
+ */
+
+ int main ()
+ {
+ printf("%ld\n", f(15));
+ return 0;
+ }
+
+commit a6eb82647d5d67f893da442f8f9375fd89a3b1e2
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:45:16 2013 +0100
+
+ touch both functions
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -3,19 +3,19 @@
+-int f(int x)
++long f(long x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+ return s;
+ }
+
+ /*
+ * A comment.
+ */
+
+ int main ()
+ {
+- printf("%d\n", f(15));
++ printf("%ld\n", f(15));
+ return 0;
+ }
+
+commit f04fb20f2c77850996cba739709acc6faecc58f7
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:55 2013 +0100
+
+ change f()
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -3,18 +3,19 @@
+ int f(int x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
++ return s;
+ }
+
+ /*
+ * A comment.
+ */
+
+ int main ()
+ {
+ printf("%d\n", f(15));
+ return 0;
+ }
+
+commit de4c48ae814792c02a49c4c3c0c757ae69c55f6a
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:48 2013 +0100
+
+ initial
+
+diff --git a/a.c b/a.c
+--- /dev/null
++++ b/a.c
+@@ -0,0 +3,18 @@
++int f(int x)
++{
++ int s = 0;
++ while (x) {
++ x >>= 1;
++ s++;
++ }
++}
++
++/*
++ * A comment.
++ */
++
++int main ()
++{
++ printf("%d\n", f(15));
++ return 0;
++}
diff --git a/t/t4211/sha1/expect.multiple-superset b/t/t4211/sha1/expect.multiple-superset
new file mode 100644
index 0000000..d930b6e
--- /dev/null
+++ b/t/t4211/sha1/expect.multiple-superset
@@ -0,0 +1,187 @@
+commit 4659538844daa2849b1a9e7d6fadb96fcd26fc83
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:48:43 2013 +0100
+
+ change back to complete line
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -4,19 +4,21 @@
+ long f(long x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+ return s;
+ }
+
+ /*
+ * This is only an example!
+ */
+
+ int main ()
+ {
+ printf("%ld\n", f(15));
+ return 0;
+-}
+\ No newline at end of file
++}
++
++/* incomplete lines are bad! */
+
+commit 100b61a6f2f720f812620a9d10afb3a960ccb73c
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:48:10 2013 +0100
+
+ change to an incomplete line at end
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -4,19 +4,19 @@
+ long f(long x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+ return s;
+ }
+
+ /*
+ * This is only an example!
+ */
+
+ int main ()
+ {
+ printf("%ld\n", f(15));
+ return 0;
+-}
++}
+\ No newline at end of file
+
+commit 39b6eb2d5b706d3322184a169f666f25ed3fbd00
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:45:41 2013 +0100
+
+ touch comment
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -3,19 +3,19 @@
+ long f(long x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+ return s;
+ }
+
+ /*
+- * A comment.
++ * This is only an example!
+ */
+
+ int main ()
+ {
+ printf("%ld\n", f(15));
+ return 0;
+ }
+
+commit a6eb82647d5d67f893da442f8f9375fd89a3b1e2
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:45:16 2013 +0100
+
+ touch both functions
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -3,19 +3,19 @@
+-int f(int x)
++long f(long x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+ return s;
+ }
+
+ /*
+ * A comment.
+ */
+
+ int main ()
+ {
+- printf("%d\n", f(15));
++ printf("%ld\n", f(15));
+ return 0;
+ }
+
+commit f04fb20f2c77850996cba739709acc6faecc58f7
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:55 2013 +0100
+
+ change f()
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -3,18 +3,19 @@
+ int f(int x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
++ return s;
+ }
+
+ /*
+ * A comment.
+ */
+
+ int main ()
+ {
+ printf("%d\n", f(15));
+ return 0;
+ }
+
+commit de4c48ae814792c02a49c4c3c0c757ae69c55f6a
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:48 2013 +0100
+
+ initial
+
+diff --git a/a.c b/a.c
+--- /dev/null
++++ b/a.c
+@@ -0,0 +3,18 @@
++int f(int x)
++{
++ int s = 0;
++ while (x) {
++ x >>= 1;
++ s++;
++ }
++}
++
++/*
++ * A comment.
++ */
++
++int main ()
++{
++ printf("%d\n", f(15));
++ return 0;
++}
diff --git a/t/t4211/sha1/expect.parallel-change-f-to-main b/t/t4211/sha1/expect.parallel-change-f-to-main
new file mode 100644
index 0000000..052def8
--- /dev/null
+++ b/t/t4211/sha1/expect.parallel-change-f-to-main
@@ -0,0 +1,160 @@
+commit 0469c60bc4837d52d97b1f081dec5f98dea20fed
+Merge: ba227c6 6ce3c4f
+Author: Thomas Rast <trast@inf.ethz.ch>
+Date: Fri Apr 12 16:16:24 2013 +0200
+
+ Merge across the rename
+
+
+commit 6ce3c4ff690136099bb17e1a8766b75764726ea7
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:49:50 2013 +0100
+
+ another simple change
+
+diff --git a/b.c b/b.c
+--- a/b.c
++++ b/b.c
+@@ -4,14 +4,14 @@
+ long f(long x)
+ {
+ int s = 0;
+ while (x) {
+- x >>= 1;
++ x /= 2;
+ s++;
+ }
+ return s;
+ }
+
+ /*
+ * This is only an example!
+ */
+
+
+commit ba227c6632349700fbb957dec2b50f5e2358be3f
+Author: Thomas Rast <trast@inf.ethz.ch>
+Date: Fri Apr 12 16:15:57 2013 +0200
+
+ change on another line of history while rename happens
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -4,14 +4,14 @@
+ long f(long x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+ return s;
+ }
+
+ /*
+- * This is only an example!
++ * This is only a short example!
+ */
+
+
+commit 39b6eb2d5b706d3322184a169f666f25ed3fbd00
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:45:41 2013 +0100
+
+ touch comment
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -3,14 +3,14 @@
+ long f(long x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+ return s;
+ }
+
+ /*
+- * A comment.
++ * This is only an example!
+ */
+
+
+commit a6eb82647d5d67f893da442f8f9375fd89a3b1e2
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:45:16 2013 +0100
+
+ touch both functions
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -3,14 +3,14 @@
+-int f(int x)
++long f(long x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+ return s;
+ }
+
+ /*
+ * A comment.
+ */
+
+
+commit f04fb20f2c77850996cba739709acc6faecc58f7
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:55 2013 +0100
+
+ change f()
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -3,13 +3,14 @@
+ int f(int x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
++ return s;
+ }
+
+ /*
+ * A comment.
+ */
+
+
+commit de4c48ae814792c02a49c4c3c0c757ae69c55f6a
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:48 2013 +0100
+
+ initial
+
+diff --git a/a.c b/a.c
+--- /dev/null
++++ b/a.c
+@@ -0,0 +3,13 @@
++int f(int x)
++{
++ int s = 0;
++ while (x) {
++ x >>= 1;
++ s++;
++ }
++}
++
++/*
++ * A comment.
++ */
++
diff --git a/t/t4211/sha1/expect.simple-f b/t/t4211/sha1/expect.simple-f
new file mode 100644
index 0000000..a1f5bc4
--- /dev/null
+++ b/t/t4211/sha1/expect.simple-f
@@ -0,0 +1,59 @@
+commit a6eb82647d5d67f893da442f8f9375fd89a3b1e2
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:45:16 2013 +0100
+
+ touch both functions
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -3,9 +3,9 @@
+-int f(int x)
++long f(long x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+ return s;
+ }
+
+commit f04fb20f2c77850996cba739709acc6faecc58f7
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:55 2013 +0100
+
+ change f()
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -3,8 +3,9 @@
+ int f(int x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
++ return s;
+ }
+
+commit de4c48ae814792c02a49c4c3c0c757ae69c55f6a
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:48 2013 +0100
+
+ initial
+
+diff --git a/a.c b/a.c
+--- /dev/null
++++ b/a.c
+@@ -0,0 +3,8 @@
++int f(int x)
++{
++ int s = 0;
++ while (x) {
++ x >>= 1;
++ s++;
++ }
++}
diff --git a/t/t4211/sha1/expect.simple-f-to-main b/t/t4211/sha1/expect.simple-f-to-main
new file mode 100644
index 0000000..a475768
--- /dev/null
+++ b/t/t4211/sha1/expect.simple-f-to-main
@@ -0,0 +1,100 @@
+commit 39b6eb2d5b706d3322184a169f666f25ed3fbd00
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:45:41 2013 +0100
+
+ touch comment
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -3,14 +3,14 @@
+ long f(long x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+ return s;
+ }
+
+ /*
+- * A comment.
++ * This is only an example!
+ */
+
+
+commit a6eb82647d5d67f893da442f8f9375fd89a3b1e2
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:45:16 2013 +0100
+
+ touch both functions
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -3,14 +3,14 @@
+-int f(int x)
++long f(long x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+ return s;
+ }
+
+ /*
+ * A comment.
+ */
+
+
+commit f04fb20f2c77850996cba739709acc6faecc58f7
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:55 2013 +0100
+
+ change f()
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -3,13 +3,14 @@
+ int f(int x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
++ return s;
+ }
+
+ /*
+ * A comment.
+ */
+
+
+commit de4c48ae814792c02a49c4c3c0c757ae69c55f6a
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:48 2013 +0100
+
+ initial
+
+diff --git a/a.c b/a.c
+--- /dev/null
++++ b/a.c
+@@ -0,0 +3,13 @@
++int f(int x)
++{
++ int s = 0;
++ while (x) {
++ x >>= 1;
++ s++;
++ }
++}
++
++/*
++ * A comment.
++ */
++
diff --git a/t/t4211/sha1/expect.simple-main b/t/t4211/sha1/expect.simple-main
new file mode 100644
index 0000000..39ce39b
--- /dev/null
+++ b/t/t4211/sha1/expect.simple-main
@@ -0,0 +1,68 @@
+commit 4659538844daa2849b1a9e7d6fadb96fcd26fc83
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:48:43 2013 +0100
+
+ change back to complete line
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -18,5 +18,5 @@
+ int main ()
+ {
+ printf("%ld\n", f(15));
+ return 0;
+-}
+\ No newline at end of file
++}
+
+commit 100b61a6f2f720f812620a9d10afb3a960ccb73c
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:48:10 2013 +0100
+
+ change to an incomplete line at end
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -18,5 +18,5 @@
+ int main ()
+ {
+ printf("%ld\n", f(15));
+ return 0;
+-}
++}
+\ No newline at end of file
+
+commit a6eb82647d5d67f893da442f8f9375fd89a3b1e2
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:45:16 2013 +0100
+
+ touch both functions
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -17,5 +17,5 @@
+ int main ()
+ {
+- printf("%d\n", f(15));
++ printf("%ld\n", f(15));
+ return 0;
+ }
+
+commit de4c48ae814792c02a49c4c3c0c757ae69c55f6a
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:48 2013 +0100
+
+ initial
+
+diff --git a/a.c b/a.c
+--- /dev/null
++++ b/a.c
+@@ -0,0 +16,5 @@
++int main ()
++{
++ printf("%d\n", f(15));
++ return 0;
++}
diff --git a/t/t4211/sha1/expect.simple-main-to-end b/t/t4211/sha1/expect.simple-main-to-end
new file mode 100644
index 0000000..8480bd9
--- /dev/null
+++ b/t/t4211/sha1/expect.simple-main-to-end
@@ -0,0 +1,70 @@
+commit 4659538844daa2849b1a9e7d6fadb96fcd26fc83
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:48:43 2013 +0100
+
+ change back to complete line
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -18,5 +18,7 @@
+ int main ()
+ {
+ printf("%ld\n", f(15));
+ return 0;
+-}
+\ No newline at end of file
++}
++
++/* incomplete lines are bad! */
+
+commit 100b61a6f2f720f812620a9d10afb3a960ccb73c
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:48:10 2013 +0100
+
+ change to an incomplete line at end
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -18,5 +18,5 @@
+ int main ()
+ {
+ printf("%ld\n", f(15));
+ return 0;
+-}
++}
+\ No newline at end of file
+
+commit a6eb82647d5d67f893da442f8f9375fd89a3b1e2
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:45:16 2013 +0100
+
+ touch both functions
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -17,5 +17,5 @@
+ int main ()
+ {
+- printf("%d\n", f(15));
++ printf("%ld\n", f(15));
+ return 0;
+ }
+
+commit de4c48ae814792c02a49c4c3c0c757ae69c55f6a
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:48 2013 +0100
+
+ initial
+
+diff --git a/a.c b/a.c
+--- /dev/null
++++ b/a.c
+@@ -0,0 +16,5 @@
++int main ()
++{
++ printf("%d\n", f(15));
++ return 0;
++}
diff --git a/t/t4211/sha1/expect.two-ranges b/t/t4211/sha1/expect.two-ranges
new file mode 100644
index 0000000..6109aa0
--- /dev/null
+++ b/t/t4211/sha1/expect.two-ranges
@@ -0,0 +1,102 @@
+commit 4659538844daa2849b1a9e7d6fadb96fcd26fc83
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:48:43 2013 +0100
+
+ change back to complete line
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -18,5 +18,5 @@
+ int main ()
+ {
+ printf("%ld\n", f(15));
+ return 0;
+-}
+\ No newline at end of file
++}
+
+commit 100b61a6f2f720f812620a9d10afb3a960ccb73c
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:48:10 2013 +0100
+
+ change to an incomplete line at end
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -18,5 +18,5 @@
+ int main ()
+ {
+ printf("%ld\n", f(15));
+ return 0;
+-}
++}
+\ No newline at end of file
+
+commit a6eb82647d5d67f893da442f8f9375fd89a3b1e2
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:45:16 2013 +0100
+
+ touch both functions
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -3,9 +3,9 @@
+-int f(int x)
++long f(long x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+ return s;
+ }
+@@ -17,5 +17,5 @@
+ int main ()
+ {
+- printf("%d\n", f(15));
++ printf("%ld\n", f(15));
+ return 0;
+ }
+
+commit f04fb20f2c77850996cba739709acc6faecc58f7
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:55 2013 +0100
+
+ change f()
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -3,8 +3,9 @@
+ int f(int x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
++ return s;
+ }
+
+commit de4c48ae814792c02a49c4c3c0c757ae69c55f6a
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:48 2013 +0100
+
+ initial
+
+diff --git a/a.c b/a.c
+--- /dev/null
++++ b/a.c
+@@ -0,0 +3,8 @@
++int f(int x)
++{
++ int s = 0;
++ while (x) {
++ x >>= 1;
++ s++;
++ }
++}
diff --git a/t/t4211/sha1/expect.vanishes-early b/t/t4211/sha1/expect.vanishes-early
new file mode 100644
index 0000000..1f7cd06
--- /dev/null
+++ b/t/t4211/sha1/expect.vanishes-early
@@ -0,0 +1,39 @@
+commit 4659538844daa2849b1a9e7d6fadb96fcd26fc83
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:48:43 2013 +0100
+
+ change back to complete line
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -22,1 +24,1 @@
+-}
+\ No newline at end of file
++/* incomplete lines are bad! */
+
+commit 100b61a6f2f720f812620a9d10afb3a960ccb73c
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:48:10 2013 +0100
+
+ change to an incomplete line at end
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -22,1 +22,1 @@
+-}
++}
+\ No newline at end of file
+
+commit de4c48ae814792c02a49c4c3c0c757ae69c55f6a
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:48 2013 +0100
+
+ initial
+
+diff --git a/a.c b/a.c
+--- /dev/null
++++ b/a.c
+@@ -0,0 +20,1 @@
++}
diff --git a/t/t4211/sha256/expect.beginning-of-file b/t/t4211/sha256/expect.beginning-of-file
new file mode 100644
index 0000000..5adfdfc
--- /dev/null
+++ b/t/t4211/sha256/expect.beginning-of-file
@@ -0,0 +1,43 @@
+commit 62a40b38fa4f00800004aee81ef287b7201317594ebcb990f38cbe493b01d200
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:47:40 2013 +0100
+
+ change at very beginning
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -1,3 +1,4 @@
++#include <unistd.h>
+ #include <stdio.h>
+
+ long f(long x)
+
+commit ccf97b9878189c40a981da50b15713bb80a35755326320ec80900caf22ced46f
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:45:16 2013 +0100
+
+ touch both functions
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -1,3 +1,3 @@
+ #include <stdio.h>
+
+-int f(int x)
++long f(long x)
+
+commit 1dd7e9b2b1699324b53b341e728653b913bc192a14dfea168c5b51f2b3d03592
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:48 2013 +0100
+
+ initial
+
+diff --git a/a.c b/a.c
+--- /dev/null
++++ b/a.c
+@@ -0,0 +1,3 @@
++#include <stdio.h>
++
++int f(int x)
diff --git a/t/t4211/sha256/expect.end-of-file b/t/t4211/sha256/expect.end-of-file
new file mode 100644
index 0000000..03ab5c1
--- /dev/null
+++ b/t/t4211/sha256/expect.end-of-file
@@ -0,0 +1,62 @@
+commit 5526ed05c2476b56af8b7be499e8f78bd50f490740733a9ca7e1f55878fa90a9
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:48:43 2013 +0100
+
+ change back to complete line
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -20,3 +20,5 @@
+ printf("%ld\n", f(15));
+ return 0;
+-}
+\ No newline at end of file
++}
++
++/* incomplete lines are bad! */
+
+commit 29f32ac3141c48b22803e5c4127b719917b67d0f8ca8c5248bebfa2a19f7da10
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:48:10 2013 +0100
+
+ change to an incomplete line at end
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -20,3 +20,3 @@
+ printf("%ld\n", f(15));
+ return 0;
+-}
++}
+\ No newline at end of file
+
+commit ccf97b9878189c40a981da50b15713bb80a35755326320ec80900caf22ced46f
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:45:16 2013 +0100
+
+ touch both functions
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -19,3 +19,3 @@
+- printf("%d\n", f(15));
++ printf("%ld\n", f(15));
+ return 0;
+ }
+
+commit 1dd7e9b2b1699324b53b341e728653b913bc192a14dfea168c5b51f2b3d03592
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:48 2013 +0100
+
+ initial
+
+diff --git a/a.c b/a.c
+--- /dev/null
++++ b/a.c
+@@ -0,0 +18,3 @@
++ printf("%d\n", f(15));
++ return 0;
++}
diff --git a/t/t4211/sha256/expect.move-support-f b/t/t4211/sha256/expect.move-support-f
new file mode 100644
index 0000000..223b4ed
--- /dev/null
+++ b/t/t4211/sha256/expect.move-support-f
@@ -0,0 +1,80 @@
+commit 4f7a58195a92c400e28a2354328587f1ff14fb77f5cf894536f17ccbc72931b9
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:49:50 2013 +0100
+
+ another simple change
+
+diff --git a/b.c b/b.c
+--- a/b.c
++++ b/b.c
+@@ -4,9 +4,9 @@
+ long f(long x)
+ {
+ int s = 0;
+ while (x) {
+- x >>= 1;
++ x /= 2;
+ s++;
+ }
+ return s;
+ }
+
+commit ccf97b9878189c40a981da50b15713bb80a35755326320ec80900caf22ced46f
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:45:16 2013 +0100
+
+ touch both functions
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -3,9 +3,9 @@
+-int f(int x)
++long f(long x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+ return s;
+ }
+
+commit f6434acd34260a6c9f61e96d96bf9a323d330561df5b1ca2631104f82026dfed
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:55 2013 +0100
+
+ change f()
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -3,8 +3,9 @@
+ int f(int x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
++ return s;
+ }
+
+commit 1dd7e9b2b1699324b53b341e728653b913bc192a14dfea168c5b51f2b3d03592
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:48 2013 +0100
+
+ initial
+
+diff --git a/a.c b/a.c
+--- /dev/null
++++ b/a.c
+@@ -0,0 +3,8 @@
++int f(int x)
++{
++ int s = 0;
++ while (x) {
++ x >>= 1;
++ s++;
++ }
++}
diff --git a/t/t4211/sha256/expect.multiple b/t/t4211/sha256/expect.multiple
new file mode 100644
index 0000000..ca00409
--- /dev/null
+++ b/t/t4211/sha256/expect.multiple
@@ -0,0 +1,104 @@
+commit 5526ed05c2476b56af8b7be499e8f78bd50f490740733a9ca7e1f55878fa90a9
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:48:43 2013 +0100
+
+ change back to complete line
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -18,5 +18,7 @@
+ int main ()
+ {
+ printf("%ld\n", f(15));
+ return 0;
+-}
+\ No newline at end of file
++}
++
++/* incomplete lines are bad! */
+
+commit 29f32ac3141c48b22803e5c4127b719917b67d0f8ca8c5248bebfa2a19f7da10
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:48:10 2013 +0100
+
+ change to an incomplete line at end
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -18,5 +18,5 @@
+ int main ()
+ {
+ printf("%ld\n", f(15));
+ return 0;
+-}
++}
+\ No newline at end of file
+
+commit ccf97b9878189c40a981da50b15713bb80a35755326320ec80900caf22ced46f
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:45:16 2013 +0100
+
+ touch both functions
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -3,9 +3,9 @@
+-int f(int x)
++long f(long x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+ return s;
+ }
+@@ -17,5 +17,5 @@
+ int main ()
+ {
+- printf("%d\n", f(15));
++ printf("%ld\n", f(15));
+ return 0;
+ }
+
+commit f6434acd34260a6c9f61e96d96bf9a323d330561df5b1ca2631104f82026dfed
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:55 2013 +0100
+
+ change f()
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -3,8 +3,9 @@
+ int f(int x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
++ return s;
+ }
+
+commit 1dd7e9b2b1699324b53b341e728653b913bc192a14dfea168c5b51f2b3d03592
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:48 2013 +0100
+
+ initial
+
+diff --git a/a.c b/a.c
+--- /dev/null
++++ b/a.c
+@@ -0,0 +3,8 @@
++int f(int x)
++{
++ int s = 0;
++ while (x) {
++ x >>= 1;
++ s++;
++ }
++}
diff --git a/t/t4211/sha256/expect.multiple-overlapping b/t/t4211/sha256/expect.multiple-overlapping
new file mode 100644
index 0000000..9015a45
--- /dev/null
+++ b/t/t4211/sha256/expect.multiple-overlapping
@@ -0,0 +1,187 @@
+commit 5526ed05c2476b56af8b7be499e8f78bd50f490740733a9ca7e1f55878fa90a9
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:48:43 2013 +0100
+
+ change back to complete line
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -4,19 +4,21 @@
+ long f(long x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+ return s;
+ }
+
+ /*
+ * This is only an example!
+ */
+
+ int main ()
+ {
+ printf("%ld\n", f(15));
+ return 0;
+-}
+\ No newline at end of file
++}
++
++/* incomplete lines are bad! */
+
+commit 29f32ac3141c48b22803e5c4127b719917b67d0f8ca8c5248bebfa2a19f7da10
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:48:10 2013 +0100
+
+ change to an incomplete line at end
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -4,19 +4,19 @@
+ long f(long x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+ return s;
+ }
+
+ /*
+ * This is only an example!
+ */
+
+ int main ()
+ {
+ printf("%ld\n", f(15));
+ return 0;
+-}
++}
+\ No newline at end of file
+
+commit 5a1b3989063d55e71e7685efa3392f133385b4034bddde530dcb5090d8b8b8ca
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:45:41 2013 +0100
+
+ touch comment
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -3,19 +3,19 @@
+ long f(long x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+ return s;
+ }
+
+ /*
+- * A comment.
++ * This is only an example!
+ */
+
+ int main ()
+ {
+ printf("%ld\n", f(15));
+ return 0;
+ }
+
+commit ccf97b9878189c40a981da50b15713bb80a35755326320ec80900caf22ced46f
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:45:16 2013 +0100
+
+ touch both functions
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -3,19 +3,19 @@
+-int f(int x)
++long f(long x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+ return s;
+ }
+
+ /*
+ * A comment.
+ */
+
+ int main ()
+ {
+- printf("%d\n", f(15));
++ printf("%ld\n", f(15));
+ return 0;
+ }
+
+commit f6434acd34260a6c9f61e96d96bf9a323d330561df5b1ca2631104f82026dfed
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:55 2013 +0100
+
+ change f()
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -3,18 +3,19 @@
+ int f(int x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
++ return s;
+ }
+
+ /*
+ * A comment.
+ */
+
+ int main ()
+ {
+ printf("%d\n", f(15));
+ return 0;
+ }
+
+commit 1dd7e9b2b1699324b53b341e728653b913bc192a14dfea168c5b51f2b3d03592
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:48 2013 +0100
+
+ initial
+
+diff --git a/a.c b/a.c
+--- /dev/null
++++ b/a.c
+@@ -0,0 +3,18 @@
++int f(int x)
++{
++ int s = 0;
++ while (x) {
++ x >>= 1;
++ s++;
++ }
++}
++
++/*
++ * A comment.
++ */
++
++int main ()
++{
++ printf("%d\n", f(15));
++ return 0;
++}
diff --git a/t/t4211/sha256/expect.multiple-superset b/t/t4211/sha256/expect.multiple-superset
new file mode 100644
index 0000000..9015a45
--- /dev/null
+++ b/t/t4211/sha256/expect.multiple-superset
@@ -0,0 +1,187 @@
+commit 5526ed05c2476b56af8b7be499e8f78bd50f490740733a9ca7e1f55878fa90a9
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:48:43 2013 +0100
+
+ change back to complete line
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -4,19 +4,21 @@
+ long f(long x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+ return s;
+ }
+
+ /*
+ * This is only an example!
+ */
+
+ int main ()
+ {
+ printf("%ld\n", f(15));
+ return 0;
+-}
+\ No newline at end of file
++}
++
++/* incomplete lines are bad! */
+
+commit 29f32ac3141c48b22803e5c4127b719917b67d0f8ca8c5248bebfa2a19f7da10
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:48:10 2013 +0100
+
+ change to an incomplete line at end
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -4,19 +4,19 @@
+ long f(long x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+ return s;
+ }
+
+ /*
+ * This is only an example!
+ */
+
+ int main ()
+ {
+ printf("%ld\n", f(15));
+ return 0;
+-}
++}
+\ No newline at end of file
+
+commit 5a1b3989063d55e71e7685efa3392f133385b4034bddde530dcb5090d8b8b8ca
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:45:41 2013 +0100
+
+ touch comment
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -3,19 +3,19 @@
+ long f(long x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+ return s;
+ }
+
+ /*
+- * A comment.
++ * This is only an example!
+ */
+
+ int main ()
+ {
+ printf("%ld\n", f(15));
+ return 0;
+ }
+
+commit ccf97b9878189c40a981da50b15713bb80a35755326320ec80900caf22ced46f
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:45:16 2013 +0100
+
+ touch both functions
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -3,19 +3,19 @@
+-int f(int x)
++long f(long x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+ return s;
+ }
+
+ /*
+ * A comment.
+ */
+
+ int main ()
+ {
+- printf("%d\n", f(15));
++ printf("%ld\n", f(15));
+ return 0;
+ }
+
+commit f6434acd34260a6c9f61e96d96bf9a323d330561df5b1ca2631104f82026dfed
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:55 2013 +0100
+
+ change f()
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -3,18 +3,19 @@
+ int f(int x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
++ return s;
+ }
+
+ /*
+ * A comment.
+ */
+
+ int main ()
+ {
+ printf("%d\n", f(15));
+ return 0;
+ }
+
+commit 1dd7e9b2b1699324b53b341e728653b913bc192a14dfea168c5b51f2b3d03592
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:48 2013 +0100
+
+ initial
+
+diff --git a/a.c b/a.c
+--- /dev/null
++++ b/a.c
+@@ -0,0 +3,18 @@
++int f(int x)
++{
++ int s = 0;
++ while (x) {
++ x >>= 1;
++ s++;
++ }
++}
++
++/*
++ * A comment.
++ */
++
++int main ()
++{
++ printf("%d\n", f(15));
++ return 0;
++}
diff --git a/t/t4211/sha256/expect.parallel-change-f-to-main b/t/t4211/sha256/expect.parallel-change-f-to-main
new file mode 100644
index 0000000..e68f892
--- /dev/null
+++ b/t/t4211/sha256/expect.parallel-change-f-to-main
@@ -0,0 +1,160 @@
+commit 98117c2059b76c36995748fb97b02542aef477fe26379e94c18fd70f7790bc67
+Merge: b511694 4f7a581
+Author: Thomas Rast <trast@inf.ethz.ch>
+Date: Fri Apr 12 16:16:24 2013 +0200
+
+ Merge across the rename
+
+
+commit 4f7a58195a92c400e28a2354328587f1ff14fb77f5cf894536f17ccbc72931b9
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:49:50 2013 +0100
+
+ another simple change
+
+diff --git a/b.c b/b.c
+--- a/b.c
++++ b/b.c
+@@ -4,14 +4,14 @@
+ long f(long x)
+ {
+ int s = 0;
+ while (x) {
+- x >>= 1;
++ x /= 2;
+ s++;
+ }
+ return s;
+ }
+
+ /*
+ * This is only an example!
+ */
+
+
+commit b511694f5337663fbd697622993a5f8e1099eca84be4df313f2b3ee94a098b42
+Author: Thomas Rast <trast@inf.ethz.ch>
+Date: Fri Apr 12 16:15:57 2013 +0200
+
+ change on another line of history while rename happens
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -4,14 +4,14 @@
+ long f(long x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+ return s;
+ }
+
+ /*
+- * This is only an example!
++ * This is only a short example!
+ */
+
+
+commit 5a1b3989063d55e71e7685efa3392f133385b4034bddde530dcb5090d8b8b8ca
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:45:41 2013 +0100
+
+ touch comment
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -3,14 +3,14 @@
+ long f(long x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+ return s;
+ }
+
+ /*
+- * A comment.
++ * This is only an example!
+ */
+
+
+commit ccf97b9878189c40a981da50b15713bb80a35755326320ec80900caf22ced46f
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:45:16 2013 +0100
+
+ touch both functions
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -3,14 +3,14 @@
+-int f(int x)
++long f(long x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+ return s;
+ }
+
+ /*
+ * A comment.
+ */
+
+
+commit f6434acd34260a6c9f61e96d96bf9a323d330561df5b1ca2631104f82026dfed
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:55 2013 +0100
+
+ change f()
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -3,13 +3,14 @@
+ int f(int x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
++ return s;
+ }
+
+ /*
+ * A comment.
+ */
+
+
+commit 1dd7e9b2b1699324b53b341e728653b913bc192a14dfea168c5b51f2b3d03592
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:48 2013 +0100
+
+ initial
+
+diff --git a/a.c b/a.c
+--- /dev/null
++++ b/a.c
+@@ -0,0 +3,13 @@
++int f(int x)
++{
++ int s = 0;
++ while (x) {
++ x >>= 1;
++ s++;
++ }
++}
++
++/*
++ * A comment.
++ */
++
diff --git a/t/t4211/sha256/expect.simple-f b/t/t4211/sha256/expect.simple-f
new file mode 100644
index 0000000..65508d7
--- /dev/null
+++ b/t/t4211/sha256/expect.simple-f
@@ -0,0 +1,59 @@
+commit ccf97b9878189c40a981da50b15713bb80a35755326320ec80900caf22ced46f
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:45:16 2013 +0100
+
+ touch both functions
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -3,9 +3,9 @@
+-int f(int x)
++long f(long x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+ return s;
+ }
+
+commit f6434acd34260a6c9f61e96d96bf9a323d330561df5b1ca2631104f82026dfed
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:55 2013 +0100
+
+ change f()
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -3,8 +3,9 @@
+ int f(int x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
++ return s;
+ }
+
+commit 1dd7e9b2b1699324b53b341e728653b913bc192a14dfea168c5b51f2b3d03592
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:48 2013 +0100
+
+ initial
+
+diff --git a/a.c b/a.c
+--- /dev/null
++++ b/a.c
+@@ -0,0 +3,8 @@
++int f(int x)
++{
++ int s = 0;
++ while (x) {
++ x >>= 1;
++ s++;
++ }
++}
diff --git a/t/t4211/sha256/expect.simple-f-to-main b/t/t4211/sha256/expect.simple-f-to-main
new file mode 100644
index 0000000..77b721c
--- /dev/null
+++ b/t/t4211/sha256/expect.simple-f-to-main
@@ -0,0 +1,100 @@
+commit 5a1b3989063d55e71e7685efa3392f133385b4034bddde530dcb5090d8b8b8ca
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:45:41 2013 +0100
+
+ touch comment
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -3,14 +3,14 @@
+ long f(long x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+ return s;
+ }
+
+ /*
+- * A comment.
++ * This is only an example!
+ */
+
+
+commit ccf97b9878189c40a981da50b15713bb80a35755326320ec80900caf22ced46f
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:45:16 2013 +0100
+
+ touch both functions
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -3,14 +3,14 @@
+-int f(int x)
++long f(long x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+ return s;
+ }
+
+ /*
+ * A comment.
+ */
+
+
+commit f6434acd34260a6c9f61e96d96bf9a323d330561df5b1ca2631104f82026dfed
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:55 2013 +0100
+
+ change f()
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -3,13 +3,14 @@
+ int f(int x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
++ return s;
+ }
+
+ /*
+ * A comment.
+ */
+
+
+commit 1dd7e9b2b1699324b53b341e728653b913bc192a14dfea168c5b51f2b3d03592
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:48 2013 +0100
+
+ initial
+
+diff --git a/a.c b/a.c
+--- /dev/null
++++ b/a.c
+@@ -0,0 +3,13 @@
++int f(int x)
++{
++ int s = 0;
++ while (x) {
++ x >>= 1;
++ s++;
++ }
++}
++
++/*
++ * A comment.
++ */
++
diff --git a/t/t4211/sha256/expect.simple-main b/t/t4211/sha256/expect.simple-main
new file mode 100644
index 0000000..d20708c
--- /dev/null
+++ b/t/t4211/sha256/expect.simple-main
@@ -0,0 +1,68 @@
+commit 5526ed05c2476b56af8b7be499e8f78bd50f490740733a9ca7e1f55878fa90a9
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:48:43 2013 +0100
+
+ change back to complete line
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -18,5 +18,5 @@
+ int main ()
+ {
+ printf("%ld\n", f(15));
+ return 0;
+-}
+\ No newline at end of file
++}
+
+commit 29f32ac3141c48b22803e5c4127b719917b67d0f8ca8c5248bebfa2a19f7da10
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:48:10 2013 +0100
+
+ change to an incomplete line at end
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -18,5 +18,5 @@
+ int main ()
+ {
+ printf("%ld\n", f(15));
+ return 0;
+-}
++}
+\ No newline at end of file
+
+commit ccf97b9878189c40a981da50b15713bb80a35755326320ec80900caf22ced46f
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:45:16 2013 +0100
+
+ touch both functions
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -17,5 +17,5 @@
+ int main ()
+ {
+- printf("%d\n", f(15));
++ printf("%ld\n", f(15));
+ return 0;
+ }
+
+commit 1dd7e9b2b1699324b53b341e728653b913bc192a14dfea168c5b51f2b3d03592
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:48 2013 +0100
+
+ initial
+
+diff --git a/a.c b/a.c
+--- /dev/null
++++ b/a.c
+@@ -0,0 +16,5 @@
++int main ()
++{
++ printf("%d\n", f(15));
++ return 0;
++}
diff --git a/t/t4211/sha256/expect.simple-main-to-end b/t/t4211/sha256/expect.simple-main-to-end
new file mode 100644
index 0000000..617cdf3
--- /dev/null
+++ b/t/t4211/sha256/expect.simple-main-to-end
@@ -0,0 +1,70 @@
+commit 5526ed05c2476b56af8b7be499e8f78bd50f490740733a9ca7e1f55878fa90a9
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:48:43 2013 +0100
+
+ change back to complete line
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -18,5 +18,7 @@
+ int main ()
+ {
+ printf("%ld\n", f(15));
+ return 0;
+-}
+\ No newline at end of file
++}
++
++/* incomplete lines are bad! */
+
+commit 29f32ac3141c48b22803e5c4127b719917b67d0f8ca8c5248bebfa2a19f7da10
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:48:10 2013 +0100
+
+ change to an incomplete line at end
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -18,5 +18,5 @@
+ int main ()
+ {
+ printf("%ld\n", f(15));
+ return 0;
+-}
++}
+\ No newline at end of file
+
+commit ccf97b9878189c40a981da50b15713bb80a35755326320ec80900caf22ced46f
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:45:16 2013 +0100
+
+ touch both functions
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -17,5 +17,5 @@
+ int main ()
+ {
+- printf("%d\n", f(15));
++ printf("%ld\n", f(15));
+ return 0;
+ }
+
+commit 1dd7e9b2b1699324b53b341e728653b913bc192a14dfea168c5b51f2b3d03592
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:48 2013 +0100
+
+ initial
+
+diff --git a/a.c b/a.c
+--- /dev/null
++++ b/a.c
+@@ -0,0 +16,5 @@
++int main ()
++{
++ printf("%d\n", f(15));
++ return 0;
++}
diff --git a/t/t4211/sha256/expect.two-ranges b/t/t4211/sha256/expect.two-ranges
new file mode 100644
index 0000000..af57c8b
--- /dev/null
+++ b/t/t4211/sha256/expect.two-ranges
@@ -0,0 +1,102 @@
+commit 5526ed05c2476b56af8b7be499e8f78bd50f490740733a9ca7e1f55878fa90a9
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:48:43 2013 +0100
+
+ change back to complete line
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -18,5 +18,5 @@
+ int main ()
+ {
+ printf("%ld\n", f(15));
+ return 0;
+-}
+\ No newline at end of file
++}
+
+commit 29f32ac3141c48b22803e5c4127b719917b67d0f8ca8c5248bebfa2a19f7da10
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:48:10 2013 +0100
+
+ change to an incomplete line at end
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -18,5 +18,5 @@
+ int main ()
+ {
+ printf("%ld\n", f(15));
+ return 0;
+-}
++}
+\ No newline at end of file
+
+commit ccf97b9878189c40a981da50b15713bb80a35755326320ec80900caf22ced46f
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:45:16 2013 +0100
+
+ touch both functions
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -3,9 +3,9 @@
+-int f(int x)
++long f(long x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
+ return s;
+ }
+@@ -17,5 +17,5 @@
+ int main ()
+ {
+- printf("%d\n", f(15));
++ printf("%ld\n", f(15));
+ return 0;
+ }
+
+commit f6434acd34260a6c9f61e96d96bf9a323d330561df5b1ca2631104f82026dfed
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:55 2013 +0100
+
+ change f()
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -3,8 +3,9 @@
+ int f(int x)
+ {
+ int s = 0;
+ while (x) {
+ x >>= 1;
+ s++;
+ }
++ return s;
+ }
+
+commit 1dd7e9b2b1699324b53b341e728653b913bc192a14dfea168c5b51f2b3d03592
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:48 2013 +0100
+
+ initial
+
+diff --git a/a.c b/a.c
+--- /dev/null
++++ b/a.c
+@@ -0,0 +3,8 @@
++int f(int x)
++{
++ int s = 0;
++ while (x) {
++ x >>= 1;
++ s++;
++ }
++}
diff --git a/t/t4211/sha256/expect.vanishes-early b/t/t4211/sha256/expect.vanishes-early
new file mode 100644
index 0000000..11ec9bd
--- /dev/null
+++ b/t/t4211/sha256/expect.vanishes-early
@@ -0,0 +1,39 @@
+commit 5526ed05c2476b56af8b7be499e8f78bd50f490740733a9ca7e1f55878fa90a9
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:48:43 2013 +0100
+
+ change back to complete line
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -22,1 +24,1 @@
+-}
+\ No newline at end of file
++/* incomplete lines are bad! */
+
+commit 29f32ac3141c48b22803e5c4127b719917b67d0f8ca8c5248bebfa2a19f7da10
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:48:10 2013 +0100
+
+ change to an incomplete line at end
+
+diff --git a/a.c b/a.c
+--- a/a.c
++++ b/a.c
+@@ -22,1 +22,1 @@
+-}
++}
+\ No newline at end of file
+
+commit 1dd7e9b2b1699324b53b341e728653b913bc192a14dfea168c5b51f2b3d03592
+Author: Thomas Rast <trast@student.ethz.ch>
+Date: Thu Feb 28 10:44:48 2013 +0100
+
+ initial
+
+diff --git a/a.c b/a.c
+--- /dev/null
++++ b/a.c
+@@ -0,0 +20,1 @@
++}
diff --git a/t/t4212-log-corrupt.sh b/t/t4212-log-corrupt.sh
new file mode 100755
index 0000000..e6b5912
--- /dev/null
+++ b/t/t4212-log-corrupt.sh
@@ -0,0 +1,136 @@
+#!/bin/sh
+
+test_description='git log with invalid commit headers'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ test_commit foo &&
+
+ git cat-file commit HEAD >ok.commit &&
+ sed "s/>/>-<>/" <ok.commit >broken_email.commit &&
+
+ git hash-object --literally -w -t commit broken_email.commit >broken_email.hash &&
+ git update-ref refs/heads/broken_email $(cat broken_email.hash)
+'
+
+test_expect_success 'fsck notices broken commit' '
+ test_must_fail git fsck 2>actual &&
+ test_grep invalid.author actual
+'
+
+test_expect_success 'git log with broken author email' '
+ {
+ echo commit $(cat broken_email.hash) &&
+ echo "Author: A U Thor <author@example.com>" &&
+ echo "Date: Thu Apr 7 15:13:13 2005 -0700" &&
+ echo &&
+ echo " foo"
+ } >expect.out &&
+
+ git log broken_email >actual.out 2>actual.err &&
+
+ test_cmp expect.out actual.out &&
+ test_must_be_empty actual.err
+'
+
+test_expect_success 'git log --format with broken author email' '
+ echo "A U Thor+author@example.com+Thu Apr 7 15:13:13 2005 -0700" >expect.out &&
+
+ git log --format="%an+%ae+%ad" broken_email >actual.out 2>actual.err &&
+
+ test_cmp expect.out actual.out &&
+ test_must_be_empty actual.err
+'
+
+test_expect_success '--until handles broken email' '
+ git rev-list --until=1980-01-01 broken_email >actual &&
+ test_must_be_empty actual
+'
+
+munge_author_date () {
+ git cat-file commit "$1" >commit.orig &&
+ sed "s/^\(author .*>\) [0-9]*/\1 $2/" <commit.orig >commit.munge &&
+ git hash-object --literally -w -t commit commit.munge
+}
+
+test_expect_success 'unparsable dates produce sentinel value' '
+ commit=$(munge_author_date HEAD totally_bogus) &&
+ echo "Date: Thu Jan 1 00:00:00 1970 +0000" >expect &&
+ git log -1 $commit >actual.full &&
+ grep Date <actual.full >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'unparsable dates produce sentinel value (%ad)' '
+ commit=$(munge_author_date HEAD totally_bogus) &&
+ echo >expect &&
+ git log -1 --format=%ad $commit >actual &&
+ test_cmp expect actual
+'
+
+# date is 2^64 + 1
+test_expect_success 'date parser recognizes integer overflow' '
+ commit=$(munge_author_date HEAD 18446744073709551617) &&
+ echo "Thu Jan 1 00:00:00 1970 +0000" >expect &&
+ git log -1 --format=%ad $commit >actual &&
+ test_cmp expect actual
+'
+
+# date is 2^64 - 2
+test_expect_success 'date parser recognizes time_t overflow' '
+ commit=$(munge_author_date HEAD 18446744073709551614) &&
+ echo "Thu Jan 1 00:00:00 1970 +0000" >expect &&
+ git log -1 --format=%ad $commit >actual &&
+ test_cmp expect actual
+'
+
+# date is within 2^63-1, but enough to choke glibc's gmtime
+test_expect_success 'absurdly far-in-future date' '
+ commit=$(munge_author_date HEAD 999999999999999999) &&
+ git log -1 --format=%ad $commit
+'
+
+test_expect_success 'create commits with whitespace committer dates' '
+ # It is important that this subject line is numeric, since we want to
+ # be sure we are not confused by skipping whitespace and accidentally
+ # parsing the subject as a timestamp.
+ #
+ # Do not use munge_author_date here. Besides not hitting the committer
+ # line, it leaves the timezone intact, and we want nothing but
+ # whitespace.
+ #
+ # We will make two munged commits here. The first, ws_commit, will
+ # be purely spaces. The second contains a vertical tab, which is
+ # considered a space by strtoumax(), but not by our isspace().
+ test_commit 1234567890 &&
+ git cat-file commit HEAD >commit.orig &&
+ sed "s/>.*/> /" <commit.orig >commit.munge &&
+ ws_commit=$(git hash-object --literally -w -t commit commit.munge) &&
+ sed "s/>.*/> $(printf "\013")/" <commit.orig >commit.munge &&
+ vt_commit=$(git hash-object --literally -w -t commit commit.munge)
+'
+
+test_expect_success '--until treats whitespace date as sentinel' '
+ echo $ws_commit >expect &&
+ git rev-list --until=1980-01-01 $ws_commit >actual &&
+ test_cmp expect actual &&
+
+ echo $vt_commit >expect &&
+ git rev-list --until=1980-01-01 $vt_commit >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'pretty-printer handles whitespace date' '
+ # as with the %ad test above, we will show these as the empty string,
+ # not the 1970 epoch date. This is intentional; see 7d9a281941 (t4212:
+ # test bogus timestamps with git-log, 2014-02-24) for more discussion.
+ echo : >expect &&
+ git log -1 --format="%at:%ct" $ws_commit >actual &&
+ test_cmp expect actual &&
+ git log -1 --format="%at:%ct" $vt_commit >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t4213-log-tabexpand.sh b/t/t4213-log-tabexpand.sh
new file mode 100755
index 0000000..590fce9
--- /dev/null
+++ b/t/t4213-log-tabexpand.sh
@@ -0,0 +1,106 @@
+#!/bin/sh
+
+test_description='log/show --expand-tabs'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+HT=" "
+title='tab indent at the beginning of the title line'
+body='tab indent on a line in the body'
+
+# usage: count_expand $indent $numSP $numHT @format_args
+count_expand ()
+{
+ expect=
+ count=$(( $1 + $2 )) ;# expected spaces
+ while test $count -gt 0
+ do
+ expect="$expect "
+ count=$(( $count - 1 ))
+ done
+ shift 2
+ count=$1 ;# expected tabs
+ while test $count -gt 0
+ do
+ expect="$expect$HT"
+ count=$(( $count - 1 ))
+ done
+ shift
+
+ # The remainder of the command line is "git show -s" options
+ case " $* " in
+ *' --pretty=short '*)
+ line=$title ;;
+ *)
+ line=$body ;;
+ esac
+
+ # Prefix the output with the command line arguments, and
+ # replace SP with a dot both in the expected and actual output
+ # so that test_cmp would show the difference together with the
+ # breakage in a way easier to consume by the debugging user.
+ {
+ echo "git show -s $*"
+ echo "$expect$line"
+ } | sed -e 's/ /./g' >expect
+
+ {
+ echo "git show -s $*"
+ git show -s "$@" |
+ sed -n -e "/$line\$/p"
+ } | sed -e 's/ /./g' >actual
+
+ test_cmp expect actual
+}
+
+test_expand ()
+{
+ fmt=$1
+ case "$fmt" in
+ *=raw | *=short | *=email)
+ default="0 1" ;;
+ *)
+ default="8 0" ;;
+ esac
+ case "$fmt" in
+ *=email)
+ in=0 ;;
+ *)
+ in=4 ;;
+ esac
+ test_expect_success "expand/no-expand${fmt:+ for $fmt}" '
+ count_expand $in $default $fmt &&
+ count_expand $in 8 0 $fmt --expand-tabs &&
+ count_expand $in 8 0 --expand-tabs $fmt &&
+ count_expand $in 8 0 $fmt --expand-tabs=8 &&
+ count_expand $in 8 0 --expand-tabs=8 $fmt &&
+ count_expand $in 0 1 $fmt --no-expand-tabs &&
+ count_expand $in 0 1 --no-expand-tabs $fmt &&
+ count_expand $in 0 1 $fmt --expand-tabs=0 &&
+ count_expand $in 0 1 --expand-tabs=0 $fmt &&
+ count_expand $in 4 0 $fmt --expand-tabs=4 &&
+ count_expand $in 4 0 --expand-tabs=4 $fmt
+ '
+}
+
+test_expect_success 'setup' '
+ test_tick &&
+ sed -e "s/Q/$HT/g" <<-EOF >msg &&
+ Q$title
+
+ Q$body
+ EOF
+ git commit --allow-empty -F msg
+'
+
+test_expand ""
+test_expand --pretty
+test_expand --pretty=short
+test_expand --pretty=medium
+test_expand --pretty=full
+test_expand --pretty=fuller
+test_expand --pretty=raw
+test_expand --pretty=email
+
+test_done
diff --git a/t/t4214-log-graph-octopus.sh b/t/t4214-log-graph-octopus.sh
new file mode 100755
index 0000000..7905597
--- /dev/null
+++ b/t/t4214-log-graph-octopus.sh
@@ -0,0 +1,351 @@
+#!/bin/sh
+
+test_description='git log --graph of skewed left octopus merge.'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-log-graph.sh
+
+test_cmp_graph () {
+ cat >expect &&
+ lib_test_cmp_graph --color=never --date-order --format=%s "$@"
+}
+
+test_cmp_colored_graph () {
+ lib_test_cmp_colored_graph --date-order --format=%s "$@"
+}
+
+test_expect_success 'set up merge history' '
+ test_commit initial &&
+ for i in 1 2 3 4 ; do
+ git checkout main -b $i || return $?
+ # Make tag name different from branch name, to avoid
+ # ambiguity error when calling checkout.
+ test_commit $i $i $i tag$i || return $?
+ done &&
+ git checkout 1 -b merge &&
+ test_merge octopus-merge 1 2 3 4 &&
+ test_commit after-merge &&
+ git checkout 1 -b L &&
+ test_commit left &&
+ git checkout 4 -b crossover &&
+ test_commit after-4 &&
+ git checkout initial -b more-L &&
+ test_commit after-initial
+'
+
+test_expect_success 'log --graph with tricky octopus merge, no color' '
+ test_cmp_graph left octopus-merge <<-\EOF
+ * left
+ | *-. octopus-merge
+ |/|\ \
+ | | | * 4
+ | | * | 3
+ | | |/
+ | * / 2
+ | |/
+ * / 1
+ |/
+ * initial
+ EOF
+'
+
+test_expect_success 'log --graph with tricky octopus merge with colors' '
+ test_config log.graphColors red,green,yellow,blue,magenta,cyan &&
+ cat >expect.colors <<-\EOF &&
+ * left
+ <RED>|<RESET> *<MAGENTA>-<RESET><MAGENTA>.<RESET> octopus-merge
+ <RED>|<RESET><RED>/<RESET><YELLOW>|<RESET><BLUE>\<RESET> <MAGENTA>\<RESET>
+ <RED>|<RESET> <YELLOW>|<RESET> <BLUE>|<RESET> * 4
+ <RED>|<RESET> <YELLOW>|<RESET> * <MAGENTA>|<RESET> 3
+ <RED>|<RESET> <YELLOW>|<RESET> <MAGENTA>|<RESET><MAGENTA>/<RESET>
+ <RED>|<RESET> * <MAGENTA>/<RESET> 2
+ <RED>|<RESET> <MAGENTA>|<RESET><MAGENTA>/<RESET>
+ * <MAGENTA>/<RESET> 1
+ <MAGENTA>|<RESET><MAGENTA>/<RESET>
+ * initial
+ EOF
+ test_cmp_colored_graph left octopus-merge
+'
+
+# Repeat the previous two tests with "normal" octopus merge (i.e.,
+# without the first parent skewing to the "left" branch column).
+
+test_expect_success 'log --graph with normal octopus merge, no color' '
+ test_cmp_graph octopus-merge <<-\EOF
+ *---. octopus-merge
+ |\ \ \
+ | | | * 4
+ | | * | 3
+ | | |/
+ | * / 2
+ | |/
+ * / 1
+ |/
+ * initial
+ EOF
+'
+
+test_expect_success 'log --graph with normal octopus merge with colors' '
+ cat >expect.colors <<-\EOF &&
+ *<YELLOW>-<RESET><YELLOW>-<RESET><BLUE>-<RESET><BLUE>.<RESET> octopus-merge
+ <RED>|<RESET><GREEN>\<RESET> <YELLOW>\<RESET> <BLUE>\<RESET>
+ <RED>|<RESET> <GREEN>|<RESET> <YELLOW>|<RESET> * 4
+ <RED>|<RESET> <GREEN>|<RESET> * <BLUE>|<RESET> 3
+ <RED>|<RESET> <GREEN>|<RESET> <BLUE>|<RESET><BLUE>/<RESET>
+ <RED>|<RESET> * <BLUE>/<RESET> 2
+ <RED>|<RESET> <BLUE>|<RESET><BLUE>/<RESET>
+ * <BLUE>/<RESET> 1
+ <BLUE>|<RESET><BLUE>/<RESET>
+ * initial
+ EOF
+ test_config log.graphColors red,green,yellow,blue,magenta,cyan &&
+ test_cmp_colored_graph octopus-merge
+'
+
+test_expect_success 'log --graph with normal octopus merge and child, no color' '
+ test_cmp_graph after-merge <<-\EOF
+ * after-merge
+ *---. octopus-merge
+ |\ \ \
+ | | | * 4
+ | | * | 3
+ | | |/
+ | * / 2
+ | |/
+ * / 1
+ |/
+ * initial
+ EOF
+'
+
+test_expect_success 'log --graph with normal octopus and child merge with colors' '
+ cat >expect.colors <<-\EOF &&
+ * after-merge
+ *<BLUE>-<RESET><BLUE>-<RESET><MAGENTA>-<RESET><MAGENTA>.<RESET> octopus-merge
+ <GREEN>|<RESET><YELLOW>\<RESET> <BLUE>\<RESET> <MAGENTA>\<RESET>
+ <GREEN>|<RESET> <YELLOW>|<RESET> <BLUE>|<RESET> * 4
+ <GREEN>|<RESET> <YELLOW>|<RESET> * <MAGENTA>|<RESET> 3
+ <GREEN>|<RESET> <YELLOW>|<RESET> <MAGENTA>|<RESET><MAGENTA>/<RESET>
+ <GREEN>|<RESET> * <MAGENTA>/<RESET> 2
+ <GREEN>|<RESET> <MAGENTA>|<RESET><MAGENTA>/<RESET>
+ * <MAGENTA>/<RESET> 1
+ <MAGENTA>|<RESET><MAGENTA>/<RESET>
+ * initial
+ EOF
+ test_config log.graphColors red,green,yellow,blue,magenta,cyan &&
+ test_cmp_colored_graph after-merge
+'
+
+test_expect_success 'log --graph with tricky octopus merge and its child, no color' '
+ test_cmp_graph left after-merge <<-\EOF
+ * left
+ | * after-merge
+ | *-. octopus-merge
+ |/|\ \
+ | | | * 4
+ | | * | 3
+ | | |/
+ | * / 2
+ | |/
+ * / 1
+ |/
+ * initial
+ EOF
+'
+
+test_expect_success 'log --graph with tricky octopus merge and its child with colors' '
+ test_config log.graphColors red,green,yellow,blue,magenta,cyan &&
+ cat >expect.colors <<-\EOF &&
+ * left
+ <RED>|<RESET> * after-merge
+ <RED>|<RESET> *<CYAN>-<RESET><CYAN>.<RESET> octopus-merge
+ <RED>|<RESET><RED>/<RESET><BLUE>|<RESET><MAGENTA>\<RESET> <CYAN>\<RESET>
+ <RED>|<RESET> <BLUE>|<RESET> <MAGENTA>|<RESET> * 4
+ <RED>|<RESET> <BLUE>|<RESET> * <CYAN>|<RESET> 3
+ <RED>|<RESET> <BLUE>|<RESET> <CYAN>|<RESET><CYAN>/<RESET>
+ <RED>|<RESET> * <CYAN>/<RESET> 2
+ <RED>|<RESET> <CYAN>|<RESET><CYAN>/<RESET>
+ * <CYAN>/<RESET> 1
+ <CYAN>|<RESET><CYAN>/<RESET>
+ * initial
+ EOF
+ test_cmp_colored_graph left after-merge
+'
+
+test_expect_success 'log --graph with crossover in octopus merge, no color' '
+ test_cmp_graph after-4 octopus-merge <<-\EOF
+ * after-4
+ | *---. octopus-merge
+ | |\ \ \
+ | |_|_|/
+ |/| | |
+ * | | | 4
+ | | | * 3
+ | |_|/
+ |/| |
+ | | * 2
+ | |/
+ |/|
+ | * 1
+ |/
+ * initial
+ EOF
+'
+
+test_expect_success 'log --graph with crossover in octopus merge with colors' '
+ test_config log.graphColors red,green,yellow,blue,magenta,cyan &&
+ cat >expect.colors <<-\EOF &&
+ * after-4
+ <RED>|<RESET> *<BLUE>-<RESET><BLUE>-<RESET><RED>-<RESET><RED>.<RESET> octopus-merge
+ <RED>|<RESET> <GREEN>|<RESET><YELLOW>\<RESET> <BLUE>\<RESET> <RED>\<RESET>
+ <RED>|<RESET> <GREEN>|<RESET><RED>_<RESET><YELLOW>|<RESET><RED>_<RESET><BLUE>|<RESET><RED>/<RESET>
+ <RED>|<RESET><RED>/<RESET><GREEN>|<RESET> <YELLOW>|<RESET> <BLUE>|<RESET>
+ * <GREEN>|<RESET> <YELLOW>|<RESET> <BLUE>|<RESET> 4
+ <MAGENTA>|<RESET> <GREEN>|<RESET> <YELLOW>|<RESET> * 3
+ <MAGENTA>|<RESET> <GREEN>|<RESET><MAGENTA>_<RESET><YELLOW>|<RESET><MAGENTA>/<RESET>
+ <MAGENTA>|<RESET><MAGENTA>/<RESET><GREEN>|<RESET> <YELLOW>|<RESET>
+ <MAGENTA>|<RESET> <GREEN>|<RESET> * 2
+ <MAGENTA>|<RESET> <GREEN>|<RESET><MAGENTA>/<RESET>
+ <MAGENTA>|<RESET><MAGENTA>/<RESET><GREEN>|<RESET>
+ <MAGENTA>|<RESET> * 1
+ <MAGENTA>|<RESET><MAGENTA>/<RESET>
+ * initial
+ EOF
+ test_cmp_colored_graph after-4 octopus-merge
+'
+
+test_expect_success 'log --graph with crossover in octopus merge and its child, no color' '
+ test_cmp_graph after-4 after-merge <<-\EOF
+ * after-4
+ | * after-merge
+ | *---. octopus-merge
+ | |\ \ \
+ | |_|_|/
+ |/| | |
+ * | | | 4
+ | | | * 3
+ | |_|/
+ |/| |
+ | | * 2
+ | |/
+ |/|
+ | * 1
+ |/
+ * initial
+ EOF
+'
+
+test_expect_success 'log --graph with crossover in octopus merge and its child with colors' '
+ test_config log.graphColors red,green,yellow,blue,magenta,cyan &&
+ cat >expect.colors <<-\EOF &&
+ * after-4
+ <RED>|<RESET> * after-merge
+ <RED>|<RESET> *<MAGENTA>-<RESET><MAGENTA>-<RESET><RED>-<RESET><RED>.<RESET> octopus-merge
+ <RED>|<RESET> <YELLOW>|<RESET><BLUE>\<RESET> <MAGENTA>\<RESET> <RED>\<RESET>
+ <RED>|<RESET> <YELLOW>|<RESET><RED>_<RESET><BLUE>|<RESET><RED>_<RESET><MAGENTA>|<RESET><RED>/<RESET>
+ <RED>|<RESET><RED>/<RESET><YELLOW>|<RESET> <BLUE>|<RESET> <MAGENTA>|<RESET>
+ * <YELLOW>|<RESET> <BLUE>|<RESET> <MAGENTA>|<RESET> 4
+ <CYAN>|<RESET> <YELLOW>|<RESET> <BLUE>|<RESET> * 3
+ <CYAN>|<RESET> <YELLOW>|<RESET><CYAN>_<RESET><BLUE>|<RESET><CYAN>/<RESET>
+ <CYAN>|<RESET><CYAN>/<RESET><YELLOW>|<RESET> <BLUE>|<RESET>
+ <CYAN>|<RESET> <YELLOW>|<RESET> * 2
+ <CYAN>|<RESET> <YELLOW>|<RESET><CYAN>/<RESET>
+ <CYAN>|<RESET><CYAN>/<RESET><YELLOW>|<RESET>
+ <CYAN>|<RESET> * 1
+ <CYAN>|<RESET><CYAN>/<RESET>
+ * initial
+ EOF
+ test_cmp_colored_graph after-4 after-merge
+'
+
+test_expect_success 'log --graph with unrelated commit and octopus tip, no color' '
+ test_cmp_graph after-initial octopus-merge <<-\EOF
+ * after-initial
+ | *---. octopus-merge
+ | |\ \ \
+ | | | | * 4
+ | |_|_|/
+ |/| | |
+ | | | * 3
+ | |_|/
+ |/| |
+ | | * 2
+ | |/
+ |/|
+ | * 1
+ |/
+ * initial
+ EOF
+'
+
+test_expect_success 'log --graph with unrelated commit and octopus tip with colors' '
+ test_config log.graphColors red,green,yellow,blue,magenta,cyan &&
+ cat >expect.colors <<-\EOF &&
+ * after-initial
+ <RED>|<RESET> *<BLUE>-<RESET><BLUE>-<RESET><MAGENTA>-<RESET><MAGENTA>.<RESET> octopus-merge
+ <RED>|<RESET> <GREEN>|<RESET><YELLOW>\<RESET> <BLUE>\<RESET> <MAGENTA>\<RESET>
+ <RED>|<RESET> <GREEN>|<RESET> <YELLOW>|<RESET> <BLUE>|<RESET> * 4
+ <RED>|<RESET> <GREEN>|<RESET><RED>_<RESET><YELLOW>|<RESET><RED>_<RESET><BLUE>|<RESET><RED>/<RESET>
+ <RED>|<RESET><RED>/<RESET><GREEN>|<RESET> <YELLOW>|<RESET> <BLUE>|<RESET>
+ <RED>|<RESET> <GREEN>|<RESET> <YELLOW>|<RESET> * 3
+ <RED>|<RESET> <GREEN>|<RESET><RED>_<RESET><YELLOW>|<RESET><RED>/<RESET>
+ <RED>|<RESET><RED>/<RESET><GREEN>|<RESET> <YELLOW>|<RESET>
+ <RED>|<RESET> <GREEN>|<RESET> * 2
+ <RED>|<RESET> <GREEN>|<RESET><RED>/<RESET>
+ <RED>|<RESET><RED>/<RESET><GREEN>|<RESET>
+ <RED>|<RESET> * 1
+ <RED>|<RESET><RED>/<RESET>
+ * initial
+ EOF
+ test_cmp_colored_graph after-initial octopus-merge
+'
+
+test_expect_success 'log --graph with unrelated commit and octopus child, no color' '
+ test_cmp_graph after-initial after-merge <<-\EOF
+ * after-initial
+ | * after-merge
+ | *---. octopus-merge
+ | |\ \ \
+ | | | | * 4
+ | |_|_|/
+ |/| | |
+ | | | * 3
+ | |_|/
+ |/| |
+ | | * 2
+ | |/
+ |/|
+ | * 1
+ |/
+ * initial
+ EOF
+'
+
+test_expect_success 'log --graph with unrelated commit and octopus child with colors' '
+ test_config log.graphColors red,green,yellow,blue,magenta,cyan &&
+ cat >expect.colors <<-\EOF &&
+ * after-initial
+ <RED>|<RESET> * after-merge
+ <RED>|<RESET> *<MAGENTA>-<RESET><MAGENTA>-<RESET><CYAN>-<RESET><CYAN>.<RESET> octopus-merge
+ <RED>|<RESET> <YELLOW>|<RESET><BLUE>\<RESET> <MAGENTA>\<RESET> <CYAN>\<RESET>
+ <RED>|<RESET> <YELLOW>|<RESET> <BLUE>|<RESET> <MAGENTA>|<RESET> * 4
+ <RED>|<RESET> <YELLOW>|<RESET><RED>_<RESET><BLUE>|<RESET><RED>_<RESET><MAGENTA>|<RESET><RED>/<RESET>
+ <RED>|<RESET><RED>/<RESET><YELLOW>|<RESET> <BLUE>|<RESET> <MAGENTA>|<RESET>
+ <RED>|<RESET> <YELLOW>|<RESET> <BLUE>|<RESET> * 3
+ <RED>|<RESET> <YELLOW>|<RESET><RED>_<RESET><BLUE>|<RESET><RED>/<RESET>
+ <RED>|<RESET><RED>/<RESET><YELLOW>|<RESET> <BLUE>|<RESET>
+ <RED>|<RESET> <YELLOW>|<RESET> * 2
+ <RED>|<RESET> <YELLOW>|<RESET><RED>/<RESET>
+ <RED>|<RESET><RED>/<RESET><YELLOW>|<RESET>
+ <RED>|<RESET> * 1
+ <RED>|<RESET><RED>/<RESET>
+ * initial
+ EOF
+ test_cmp_colored_graph after-initial after-merge
+'
+
+test_done
diff --git a/t/t4215-log-skewed-merges.sh b/t/t4215-log-skewed-merges.sh
new file mode 100755
index 0000000..b877ac7
--- /dev/null
+++ b/t/t4215-log-skewed-merges.sh
@@ -0,0 +1,374 @@
+#!/bin/sh
+
+test_description='git log --graph of skewed merges'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-log-graph.sh
+
+check_graph () {
+ cat >expect &&
+ lib_test_cmp_graph --format=%s "$@"
+}
+
+test_expect_success 'log --graph with merge fusing with its left and right neighbors' '
+ git checkout --orphan _p &&
+ test_commit A &&
+ test_commit B &&
+ git checkout -b _q @^ && test_commit C &&
+ git checkout -b _r @^ && test_commit D &&
+ git checkout _p && git merge --no-ff _q _r -m E &&
+ git checkout _r && test_commit F &&
+ git checkout _p && git merge --no-ff _r -m G &&
+ git checkout @^^ && git merge --no-ff _p -m H &&
+
+ check_graph <<-\EOF
+ * H
+ |\
+ | * G
+ | |\
+ | | * F
+ | * | E
+ |/|\|
+ | | * D
+ | * | C
+ | |/
+ * / B
+ |/
+ * A
+ EOF
+'
+
+test_expect_success 'log --graph with left-skewed merge' '
+ git checkout --orphan 0_p && test_commit 0_A &&
+ git checkout -b 0_q 0_p && test_commit 0_B &&
+ git checkout -b 0_r 0_p &&
+ test_commit 0_C &&
+ test_commit 0_D &&
+ git checkout -b 0_s 0_p && test_commit 0_E &&
+ git checkout -b 0_t 0_p && git merge --no-ff 0_r^ 0_s -m 0_F &&
+ git checkout 0_p && git merge --no-ff 0_s -m 0_G &&
+ git checkout @^ && git merge --no-ff 0_q 0_r 0_t 0_p -m 0_H &&
+
+ check_graph <<-\EOF
+ *-----. 0_H
+ |\ \ \ \
+ | | | | * 0_G
+ | |_|_|/|
+ |/| | | |
+ | | | * | 0_F
+ | |_|/|\|
+ |/| | | |
+ | | | | * 0_E
+ | |_|_|/
+ |/| | |
+ | | * | 0_D
+ | | |/
+ | | * 0_C
+ | |/
+ |/|
+ | * 0_B
+ |/
+ * 0_A
+ EOF
+'
+
+test_expect_success 'log --graph with nested left-skewed merge' '
+ git checkout --orphan 1_p &&
+ test_commit 1_A &&
+ test_commit 1_B &&
+ test_commit 1_C &&
+ git checkout -b 1_q @^ && test_commit 1_D &&
+ git checkout 1_p && git merge --no-ff 1_q -m 1_E &&
+ git checkout -b 1_r @~3 && test_commit 1_F &&
+ git checkout 1_p && git merge --no-ff 1_r -m 1_G &&
+ git checkout @^^ && git merge --no-ff 1_p -m 1_H &&
+
+ check_graph <<-\EOF
+ * 1_H
+ |\
+ | * 1_G
+ | |\
+ | | * 1_F
+ | * | 1_E
+ |/| |
+ | * | 1_D
+ * | | 1_C
+ |/ /
+ * / 1_B
+ |/
+ * 1_A
+ EOF
+'
+
+test_expect_success 'log --graph with nested left-skewed merge following normal merge' '
+ git checkout --orphan 2_p &&
+ test_commit 2_A &&
+ test_commit 2_B &&
+ test_commit 2_C &&
+ git checkout -b 2_q @^^ &&
+ test_commit 2_D &&
+ test_commit 2_E &&
+ git checkout -b 2_r @^ && test_commit 2_F &&
+ git checkout 2_q &&
+ git merge --no-ff 2_r -m 2_G &&
+ git merge --no-ff 2_p^ -m 2_H &&
+ git checkout -b 2_s @^^ && git merge --no-ff 2_q -m 2_J &&
+ git checkout 2_p && git merge --no-ff 2_s -m 2_K &&
+
+ check_graph <<-\EOF
+ * 2_K
+ |\
+ | * 2_J
+ | |\
+ | | * 2_H
+ | | |\
+ | | * | 2_G
+ | |/| |
+ | | * | 2_F
+ | * | | 2_E
+ | |/ /
+ | * | 2_D
+ * | | 2_C
+ | |/
+ |/|
+ * | 2_B
+ |/
+ * 2_A
+ EOF
+'
+
+test_expect_success 'log --graph with nested right-skewed merge following left-skewed merge' '
+ git checkout --orphan 3_p &&
+ test_commit 3_A &&
+ git checkout -b 3_q &&
+ test_commit 3_B &&
+ test_commit 3_C &&
+ git checkout -b 3_r @^ &&
+ test_commit 3_D &&
+ git checkout 3_q && git merge --no-ff 3_r -m 3_E &&
+ git checkout 3_p && git merge --no-ff 3_q -m 3_F &&
+ git checkout 3_r && test_commit 3_G &&
+ git checkout 3_p && git merge --no-ff 3_r -m 3_H &&
+ git checkout @^^ && git merge --no-ff 3_p -m 3_J &&
+
+ check_graph <<-\EOF
+ * 3_J
+ |\
+ | * 3_H
+ | |\
+ | | * 3_G
+ | * | 3_F
+ |/| |
+ | * | 3_E
+ | |\|
+ | | * 3_D
+ | * | 3_C
+ | |/
+ | * 3_B
+ |/
+ * 3_A
+ EOF
+'
+
+test_expect_success 'log --graph with right-skewed merge following a left-skewed one' '
+ git checkout --orphan 4_p &&
+ test_commit 4_A &&
+ test_commit 4_B &&
+ test_commit 4_C &&
+ git checkout -b 4_q @^^ && test_commit 4_D &&
+ git checkout -b 4_r 4_p^ && git merge --no-ff 4_q -m 4_E &&
+ git checkout -b 4_s 4_p^^ &&
+ git merge --no-ff 4_r -m 4_F &&
+ git merge --no-ff 4_p -m 4_G &&
+ git checkout @^^ && git merge --no-ff 4_s -m 4_H &&
+
+ check_graph --date-order <<-\EOF
+ * 4_H
+ |\
+ | * 4_G
+ | |\
+ | * | 4_F
+ |/| |
+ | * | 4_E
+ | |\ \
+ | | * | 4_D
+ | |/ /
+ |/| |
+ | | * 4_C
+ | |/
+ | * 4_B
+ |/
+ * 4_A
+ EOF
+'
+
+test_expect_success 'log --graph with octopus merge with column joining its penultimate parent' '
+ git checkout --orphan 5_p &&
+ test_commit 5_A &&
+ git branch 5_q &&
+ git branch 5_r &&
+ test_commit 5_B &&
+ git checkout 5_q && test_commit 5_C &&
+ git checkout 5_r && test_commit 5_D &&
+ git checkout 5_p &&
+ git merge --no-ff 5_q 5_r -m 5_E &&
+ git checkout 5_q && test_commit 5_F &&
+ git checkout -b 5_s 5_p^ &&
+ git merge --no-ff 5_p 5_q -m 5_G &&
+ git checkout 5_r &&
+ git merge --no-ff 5_s -m 5_H &&
+
+ check_graph <<-\EOF
+ * 5_H
+ |\
+ | *-. 5_G
+ | |\ \
+ | | | * 5_F
+ | | * | 5_E
+ | |/|\ \
+ | |_|/ /
+ |/| | /
+ | | |/
+ * | | 5_D
+ | | * 5_C
+ | |/
+ |/|
+ | * 5_B
+ |/
+ * 5_A
+ EOF
+'
+
+test_expect_success 'log --graph with multiple tips' '
+ git checkout --orphan 6_1 &&
+ test_commit 6_A &&
+ git branch 6_2 &&
+ git branch 6_4 &&
+ test_commit 6_B &&
+ git branch 6_3 &&
+ test_commit 6_C &&
+ git checkout 6_2 && test_commit 6_D &&
+ git checkout 6_3 && test_commit 6_E &&
+ git checkout -b 6_5 6_1 &&
+ git merge --no-ff 6_2 -m 6_F &&
+ git checkout 6_4 && test_commit 6_G &&
+ git checkout 6_3 &&
+ git merge --no-ff 6_4 -m 6_H &&
+ git checkout 6_1 &&
+ git merge --no-ff 6_2 -m 6_I &&
+
+ check_graph 6_1 6_3 6_5 <<-\EOF
+ * 6_I
+ |\
+ | | * 6_H
+ | | |\
+ | | | * 6_G
+ | | * | 6_E
+ | | | | * 6_F
+ | |_|_|/|
+ |/| | |/
+ | | |/|
+ | |/| |
+ | * | | 6_D
+ | | |/
+ | |/|
+ * | | 6_C
+ | |/
+ |/|
+ * | 6_B
+ |/
+ * 6_A
+ EOF
+'
+
+test_expect_success 'log --graph with multiple tips and colors' '
+ test_config log.graphColors red,green,yellow,blue,magenta,cyan &&
+ cat >expect.colors <<-\EOF &&
+ * 6_I
+ <RED>|<RESET><GREEN>\<RESET>
+ <RED>|<RESET> <GREEN>|<RESET> * 6_H
+ <RED>|<RESET> <GREEN>|<RESET> <YELLOW>|<RESET><BLUE>\<RESET>
+ <RED>|<RESET> <GREEN>|<RESET> <YELLOW>|<RESET> * 6_G
+ <RED>|<RESET> <GREEN>|<RESET> <YELLOW>|<RESET> <BLUE>|<RESET> * 6_F
+ <RED>|<RESET> <GREEN>|<RESET><RED>_<RESET><YELLOW>|<RESET><RED>_<RESET><BLUE>|<RESET><RED>/<RESET><GREEN>|<RESET>
+ <RED>|<RESET><RED>/<RESET><GREEN>|<RESET> <YELLOW>|<RESET> <BLUE>|<RESET><GREEN>/<RESET>
+ <RED>|<RESET> <GREEN>|<RESET> <YELLOW>|<RESET><GREEN>/<RESET><BLUE>|<RESET>
+ <RED>|<RESET> <GREEN>|<RESET><GREEN>/<RESET><YELLOW>|<RESET> <BLUE>|<RESET>
+ <RED>|<RESET> <GREEN>|<RESET> * <BLUE>|<RESET> 6_E
+ <RED>|<RESET> * <CYAN>|<RESET> <BLUE>|<RESET> 6_D
+ <RED>|<RESET> <BLUE>|<RESET> <CYAN>|<RESET><BLUE>/<RESET>
+ <RED>|<RESET> <BLUE>|<RESET><BLUE>/<RESET><CYAN>|<RESET>
+ * <BLUE>|<RESET> <CYAN>|<RESET> 6_C
+ <CYAN>|<RESET> <BLUE>|<RESET><CYAN>/<RESET>
+ <CYAN>|<RESET><CYAN>/<RESET><BLUE>|<RESET>
+ * <BLUE>|<RESET> 6_B
+ <BLUE>|<RESET><BLUE>/<RESET>
+ * 6_A
+ EOF
+ lib_test_cmp_colored_graph --date-order --pretty=tformat:%s 6_1 6_3 6_5
+'
+
+test_expect_success 'log --graph with multiple tips' '
+ git checkout --orphan 7_1 &&
+ test_commit 7_A &&
+ test_commit 7_B &&
+ test_commit 7_C &&
+ git checkout -b 7_2 7_1~2 &&
+ test_commit 7_D &&
+ test_commit 7_E &&
+ git checkout -b 7_3 7_1~1 &&
+ test_commit 7_F &&
+ test_commit 7_G &&
+ git checkout -b 7_4 7_2~1 &&
+ test_commit 7_H &&
+ git checkout -b 7_5 7_1~2 &&
+ test_commit 7_I &&
+ git checkout -b 7_6 7_3~1 &&
+ test_commit 7_J &&
+ git checkout -b M_1 7_1 &&
+ git merge --no-ff 7_2 -m 7_M1 &&
+ git checkout -b M_3 7_3 &&
+ git merge --no-ff 7_4 -m 7_M2 &&
+ git checkout -b M_5 7_5 &&
+ git merge --no-ff 7_6 -m 7_M3 &&
+ git checkout -b M_7 7_1 &&
+ git merge --no-ff 7_2 7_3 -m 7_M4 &&
+
+ check_graph M_1 M_3 M_5 M_7 <<-\EOF
+ * 7_M1
+ |\
+ | | * 7_M2
+ | | |\
+ | | | * 7_H
+ | | | | * 7_M3
+ | | | | |\
+ | | | | | * 7_J
+ | | | | * | 7_I
+ | | | | | | * 7_M4
+ | |_|_|_|_|/|\
+ |/| | | | |/ /
+ | | |_|_|/| /
+ | |/| | | |/
+ | | | |_|/|
+ | | |/| | |
+ | | * | | | 7_G
+ | | | |_|/
+ | | |/| |
+ | | * | | 7_F
+ | * | | | 7_E
+ | | |/ /
+ | |/| |
+ | * | | 7_D
+ | | |/
+ | |/|
+ * | | 7_C
+ | |/
+ |/|
+ * | 7_B
+ |/
+ * 7_A
+ EOF
+'
+
+test_done
diff --git a/t/t4216-log-bloom.sh b/t/t4216-log-bloom.sh
new file mode 100755
index 0000000..2ba0324
--- /dev/null
+++ b/t/t4216-log-bloom.sh
@@ -0,0 +1,457 @@
+#!/bin/sh
+
+test_description='git log for a path with Bloom filters'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-chunk.sh
+
+GIT_TEST_COMMIT_GRAPH=0
+GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS=0
+
+test_expect_success 'setup test - repo, commits, commit graph, log outputs' '
+ git init &&
+ mkdir A A/B A/B/C &&
+ test_commit c1 A/file1 &&
+ test_commit c2 A/B/file2 &&
+ test_commit c3 A/B/C/file3 &&
+ test_commit c4 A/file1 &&
+ test_commit c5 A/B/file2 &&
+ test_commit c6 A/B/C/file3 &&
+ test_commit c7 A/file1 &&
+ test_commit c8 A/B/file2 &&
+ test_commit c9 A/B/C/file3 &&
+ test_commit c10 file_to_be_deleted &&
+ git checkout -b side HEAD~4 &&
+ test_commit side-1 file4 &&
+ git checkout main &&
+ git merge side &&
+ test_commit c11 file5 &&
+ mv file5 file5_renamed &&
+ git add file5_renamed &&
+ git commit -m "rename" &&
+ rm file_to_be_deleted &&
+ git add . &&
+ git commit -m "file removed" &&
+ git commit --allow-empty -m "empty" &&
+ git commit-graph write --reachable --changed-paths &&
+
+ test_oid_cache <<-EOF
+ oid_version sha1:1
+ oid_version sha256:2
+ EOF
+'
+
+graph_read_expect () {
+ NUM_CHUNKS=6
+ cat >expect <<- EOF
+ header: 43475048 1 $(test_oid oid_version) $NUM_CHUNKS 0
+ num_commits: $1
+ chunks: oid_fanout oid_lookup commit_metadata generation_data bloom_indexes bloom_data
+ options: bloom(1,10,7) read_generation_data
+ EOF
+ test-tool read-graph >actual &&
+ test_cmp expect actual
+}
+
+test_expect_success 'commit-graph write wrote out the bloom chunks' '
+ graph_read_expect 16
+'
+
+# Turn off any inherited trace2 settings for this test.
+sane_unset GIT_TRACE2 GIT_TRACE2_PERF GIT_TRACE2_EVENT
+sane_unset GIT_TRACE2_PERF_BRIEF
+sane_unset GIT_TRACE2_CONFIG_PARAMS
+
+setup () {
+ rm -f "$TRASH_DIRECTORY/trace.perf" &&
+ git -c core.commitGraph=false log --pretty="format:%s" $1 >log_wo_bloom &&
+ GIT_TRACE2_PERF="$TRASH_DIRECTORY/trace.perf" git -c core.commitGraph=true log --pretty="format:%s" $1 >log_w_bloom
+}
+
+test_bloom_filters_used () {
+ log_args=$1
+ bloom_trace_prefix="statistics:{\"filter_not_present\":${2:-0},\"maybe\""
+ setup "$log_args" &&
+ grep -q "$bloom_trace_prefix" "$TRASH_DIRECTORY/trace.perf" &&
+ test_cmp log_wo_bloom log_w_bloom &&
+ test_path_is_file "$TRASH_DIRECTORY/trace.perf"
+}
+
+test_bloom_filters_not_used () {
+ log_args=$1
+ setup "$log_args" &&
+ ! grep -q "statistics:{\"filter_not_present\":" "$TRASH_DIRECTORY/trace.perf" &&
+ test_cmp log_wo_bloom log_w_bloom
+}
+
+for path in A A/B A/B/C A/file1 A/B/file2 A/B/C/file3 file4 file5 file5_renamed file_to_be_deleted
+do
+ for option in "" \
+ "--all" \
+ "--full-history" \
+ "--full-history --simplify-merges" \
+ "--simplify-merges" \
+ "--simplify-by-decoration" \
+ "--follow" \
+ "--first-parent" \
+ "--topo-order" \
+ "--date-order" \
+ "--author-date-order" \
+ "--ancestry-path side..main"
+ do
+ test_expect_success "git log option: $option for path: $path" '
+ test_bloom_filters_used "$option -- $path" &&
+ test_config commitgraph.readChangedPaths false &&
+ test_bloom_filters_not_used "$option -- $path"
+ '
+ done
+done
+
+test_expect_success 'git log -- folder works with and without the trailing slash' '
+ test_bloom_filters_used "-- A" &&
+ test_bloom_filters_used "-- A/"
+'
+
+test_expect_success 'git log for path that does not exist. ' '
+ test_bloom_filters_used "-- path_does_not_exist"
+'
+
+test_expect_success 'git log with --walk-reflogs does not use Bloom filters' '
+ test_bloom_filters_not_used "--walk-reflogs -- A"
+'
+
+test_expect_success 'git log -- multiple path specs does not use Bloom filters' '
+ test_bloom_filters_not_used "-- file4 A/file1"
+'
+
+test_expect_success 'git log -- "." pathspec at root does not use Bloom filters' '
+ test_bloom_filters_not_used "-- ."
+'
+
+test_expect_success 'git log with wildcard that resolves to a single path uses Bloom filters' '
+ test_bloom_filters_used "-- *4" &&
+ test_bloom_filters_used "-- *renamed"
+'
+
+test_expect_success 'git log with wildcard that resolves to a multiple paths does not uses Bloom filters' '
+ test_bloom_filters_not_used "-- *" &&
+ test_bloom_filters_not_used "-- file*"
+'
+
+test_expect_success 'setup - add commit-graph to the chain without Bloom filters' '
+ test_commit c14 A/anotherFile2 &&
+ test_commit c15 A/B/anotherFile2 &&
+ test_commit c16 A/B/C/anotherFile2 &&
+ git commit-graph write --reachable --split --no-changed-paths &&
+ test_line_count = 2 .git/objects/info/commit-graphs/commit-graph-chain
+'
+
+test_expect_success 'use Bloom filters even if the latest graph does not have Bloom filters' '
+ # Ensure that the number of empty filters is equal to the number of
+ # filters in the latest graph layer to prove that they are loaded (and
+ # ignored).
+ test_bloom_filters_used "-- A/B" 3
+'
+
+test_expect_success 'setup - add commit-graph to the chain with Bloom filters' '
+ test_commit c17 A/anotherFile3 &&
+ git commit-graph write --reachable --changed-paths --split &&
+ test_line_count = 3 .git/objects/info/commit-graphs/commit-graph-chain
+'
+
+test_bloom_filters_used_when_some_filters_are_missing () {
+ log_args=$1
+ bloom_trace_prefix="statistics:{\"filter_not_present\":3,\"maybe\":6,\"definitely_not\":9"
+ setup "$log_args" &&
+ grep -q "$bloom_trace_prefix" "$TRASH_DIRECTORY/trace.perf" &&
+ test_cmp log_wo_bloom log_w_bloom
+}
+
+test_expect_success 'Use Bloom filters if they exist in the latest but not all commit graphs in the chain.' '
+ test_bloom_filters_used_when_some_filters_are_missing "-- A/B"
+'
+
+test_expect_success 'persist filter settings' '
+ test_when_finished rm -rf .git/objects/info/commit-graph* &&
+ rm -rf .git/objects/info/commit-graph* &&
+ GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
+ GIT_TEST_BLOOM_SETTINGS_NUM_HASHES=9 \
+ GIT_TEST_BLOOM_SETTINGS_BITS_PER_ENTRY=15 \
+ git commit-graph write --reachable --changed-paths &&
+ grep "{\"hash_version\":1,\"num_hashes\":9,\"bits_per_entry\":15,\"max_changed_paths\":512" trace2.txt &&
+ GIT_TRACE2_EVENT="$(pwd)/trace2-auto.txt" \
+ git commit-graph write --reachable --changed-paths &&
+ grep "{\"hash_version\":1,\"num_hashes\":9,\"bits_per_entry\":15,\"max_changed_paths\":512" trace2-auto.txt
+'
+
+test_max_changed_paths () {
+ grep "\"max_changed_paths\":$1" $2
+}
+
+test_filter_not_computed () {
+ grep "\"key\":\"filter-not-computed\",\"value\":\"$1\"" $2
+}
+
+test_filter_computed () {
+ grep "\"key\":\"filter-computed\",\"value\":\"$1\"" $2
+}
+
+test_filter_trunc_empty () {
+ grep "\"key\":\"filter-trunc-empty\",\"value\":\"$1\"" $2
+}
+
+test_filter_trunc_large () {
+ grep "\"key\":\"filter-trunc-large\",\"value\":\"$1\"" $2
+}
+
+test_expect_success 'correctly report changes over limit' '
+ git init limits &&
+ (
+ cd limits &&
+ mkdir d &&
+ mkdir d/e &&
+
+ for i in $(test_seq 1 2)
+ do
+ printf $i >d/file$i.txt &&
+ printf $i >d/e/file$i.txt || return 1
+ done &&
+
+ mkdir mode &&
+ printf bash >mode/script.sh &&
+
+ mkdir foo &&
+ touch foo/bar &&
+ touch foo.txt &&
+
+ git add d foo foo.txt mode &&
+ git commit -m "files" &&
+
+ # Commit has 7 file and 4 directory adds
+ GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS=10 \
+ GIT_TRACE2_EVENT="$(pwd)/trace" \
+ git commit-graph write --reachable --changed-paths &&
+ test_max_changed_paths 10 trace &&
+ test_filter_computed 1 trace &&
+ test_filter_trunc_large 1 trace &&
+
+ for path in $(git ls-tree -r --name-only HEAD)
+ do
+ git -c commitGraph.readChangedPaths=false log \
+ -- $path >expect &&
+ git log -- $path >actual &&
+ test_cmp expect actual || return 1
+ done &&
+
+ # Make a variety of path changes
+ printf new1 >d/e/file1.txt &&
+ printf new2 >d/file2.txt &&
+ rm d/e/file2.txt &&
+ rm -r foo &&
+ printf text >foo &&
+ mkdir f &&
+ printf new1 >f/file1.txt &&
+
+ # including a mode-only change (counts as modified)
+ git update-index --chmod=+x mode/script.sh &&
+
+ git add foo d f &&
+ git commit -m "complicated" &&
+
+ # start from scratch and rebuild
+ rm -f .git/objects/info/commit-graph &&
+ GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS=10 \
+ GIT_TRACE2_EVENT="$(pwd)/trace-edit" \
+ git commit-graph write --reachable --changed-paths &&
+ test_max_changed_paths 10 trace-edit &&
+ test_filter_computed 2 trace-edit &&
+ test_filter_trunc_large 2 trace-edit &&
+
+ for path in $(git ls-tree -r --name-only HEAD)
+ do
+ git -c commitGraph.readChangedPaths=false log \
+ -- $path >expect &&
+ git log -- $path >actual &&
+ test_cmp expect actual || return 1
+ done &&
+
+ # start from scratch and rebuild
+ rm -f .git/objects/info/commit-graph &&
+ GIT_TEST_BLOOM_SETTINGS_MAX_CHANGED_PATHS=11 \
+ GIT_TRACE2_EVENT="$(pwd)/trace-update" \
+ git commit-graph write --reachable --changed-paths &&
+ test_max_changed_paths 11 trace-update &&
+ test_filter_computed 2 trace-update &&
+ test_filter_trunc_large 0 trace-update &&
+
+ for path in $(git ls-tree -r --name-only HEAD)
+ do
+ git -c commitGraph.readChangedPaths=false log \
+ -- $path >expect &&
+ git log -- $path >actual &&
+ test_cmp expect actual || return 1
+ done
+ )
+'
+
+test_expect_success 'correctly report commits with no changed paths' '
+ git init empty &&
+ test_when_finished "rm -fr empty" &&
+ (
+ cd empty &&
+
+ git commit --allow-empty -m "initial commit" &&
+
+ GIT_TRACE2_EVENT="$(pwd)/trace.event" \
+ git commit-graph write --reachable --changed-paths &&
+ test_filter_computed 1 trace.event &&
+ test_filter_not_computed 0 trace.event &&
+ test_filter_trunc_empty 1 trace.event &&
+ test_filter_trunc_large 0 trace.event
+ )
+'
+
+test_expect_success 'Bloom generation is limited by --max-new-filters' '
+ (
+ cd limits &&
+ test_commit c2 filter &&
+ test_commit c3 filter &&
+ test_commit c4 no-filter &&
+
+ rm -f trace.event &&
+ GIT_TRACE2_EVENT="$(pwd)/trace.event" \
+ git commit-graph write --reachable --split=replace \
+ --changed-paths --max-new-filters=2 &&
+
+ test_filter_computed 2 trace.event &&
+ test_filter_not_computed 3 trace.event &&
+ test_filter_trunc_empty 0 trace.event &&
+ test_filter_trunc_large 0 trace.event
+ )
+'
+
+test_expect_success 'Bloom generation backfills previously-skipped filters' '
+ # Check specifying commitGraph.maxNewFilters over "git config" works.
+ test_config -C limits commitGraph.maxNewFilters 1 &&
+ (
+ cd limits &&
+
+ rm -f trace.event &&
+ GIT_TRACE2_EVENT="$(pwd)/trace.event" \
+ git commit-graph write --reachable --changed-paths \
+ --split=replace &&
+ test_filter_computed 1 trace.event &&
+ test_filter_not_computed 4 trace.event &&
+ test_filter_trunc_empty 0 trace.event &&
+ test_filter_trunc_large 0 trace.event
+ )
+'
+
+test_expect_success '--max-new-filters overrides configuration' '
+ git init override &&
+ test_when_finished "rm -fr override" &&
+ test_config -C override commitGraph.maxNewFilters 2 &&
+ (
+ cd override &&
+ test_commit one &&
+ test_commit two &&
+
+ rm -f trace.event &&
+ GIT_TRACE2_EVENT="$(pwd)/trace.event" \
+ git commit-graph write --reachable --changed-paths \
+ --max-new-filters=1 &&
+ test_filter_computed 1 trace.event &&
+ test_filter_not_computed 1 trace.event &&
+ test_filter_trunc_empty 0 trace.event &&
+ test_filter_trunc_large 0 trace.event
+ )
+'
+
+test_expect_success 'Bloom generation backfills empty commits' '
+ git init empty &&
+ test_when_finished "rm -fr empty" &&
+ (
+ cd empty &&
+ for i in $(test_seq 1 6)
+ do
+ git commit --allow-empty -m "$i" || return 1
+ done &&
+
+ # Generate Bloom filters for empty commits 1-6, two at a time.
+ for i in $(test_seq 1 3)
+ do
+ rm -f trace.event &&
+ GIT_TRACE2_EVENT="$(pwd)/trace.event" \
+ git commit-graph write --reachable \
+ --changed-paths --max-new-filters=2 &&
+ test_filter_computed 2 trace.event &&
+ test_filter_not_computed 4 trace.event &&
+ test_filter_trunc_empty 2 trace.event &&
+ test_filter_trunc_large 0 trace.event || return 1
+ done &&
+
+ # Finally, make sure that once all commits have filters, that
+ # none are subsequently recomputed.
+ rm -f trace.event &&
+ GIT_TRACE2_EVENT="$(pwd)/trace.event" \
+ git commit-graph write --reachable \
+ --changed-paths --max-new-filters=2 &&
+ test_filter_computed 0 trace.event &&
+ test_filter_not_computed 6 trace.event &&
+ test_filter_trunc_empty 0 trace.event &&
+ test_filter_trunc_large 0 trace.event
+ )
+'
+
+corrupt_graph () {
+ graph=.git/objects/info/commit-graph &&
+ test_when_finished "rm -rf $graph" &&
+ git commit-graph write --reachable --changed-paths &&
+ corrupt_chunk_file $graph "$@"
+}
+
+check_corrupt_graph () {
+ corrupt_graph "$@" &&
+ git -c core.commitGraph=false log -- A/B/file2 >expect.out &&
+ git -c core.commitGraph=true log -- A/B/file2 >out 2>err &&
+ test_cmp expect.out out
+}
+
+test_expect_success 'Bloom reader notices too-small data chunk' '
+ check_corrupt_graph BDAT clear 00000000 &&
+ echo "warning: ignoring too-small changed-path chunk" \
+ "(4 < 12) in commit-graph file" >expect.err &&
+ test_cmp expect.err err
+'
+
+test_expect_success 'Bloom reader notices out-of-bounds filter offsets' '
+ check_corrupt_graph BIDX 12 FFFFFFFF &&
+ # use grep to avoid depending on exact chunk size
+ grep "warning: ignoring out-of-range offset (4294967295) for changed-path filter at pos 3 of .git/objects/info/commit-graph" err
+'
+
+test_expect_success 'Bloom reader notices too-small index chunk' '
+ # replace the index with a single entry, making most
+ # lookups out-of-bounds
+ check_corrupt_graph BIDX clear 00000000 &&
+ echo "warning: commit-graph changed-path index chunk" \
+ "is too small" >expect.err &&
+ test_cmp expect.err err
+'
+
+test_expect_success 'Bloom reader notices out-of-order index offsets' '
+ # we do not know any real offsets, but we can pick
+ # something plausible; we should not get to the point of
+ # actually reading from the bogus offsets anyway.
+ corrupt_graph BIDX 4 0000000c00000005 &&
+ echo "warning: ignoring decreasing changed-path index offsets" \
+ "(12 > 5) for positions 1 and 2 of .git/objects/info/commit-graph" >expect.err &&
+ git -c core.commitGraph=false log -- A/B/file2 >expect.out &&
+ git -c core.commitGraph=true log -- A/B/file2 >out 2>err &&
+ test_cmp expect.out out &&
+ test_cmp expect.err err
+'
+
+test_done
diff --git a/t/t4217-log-limit.sh b/t/t4217-log-limit.sh
new file mode 100755
index 0000000..613f071
--- /dev/null
+++ b/t/t4217-log-limit.sh
@@ -0,0 +1,42 @@
+#!/bin/sh
+
+test_description='git log with filter options limiting the output'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup test' '
+ git init &&
+ echo a >file &&
+ git add file &&
+ GIT_COMMITTER_DATE="2021-02-01 00:00" git commit -m init &&
+ echo a >>file &&
+ git add file &&
+ GIT_COMMITTER_DATE="2022-02-01 00:00" git commit -m first &&
+ echo a >>file &&
+ git add file &&
+ GIT_COMMITTER_DATE="2021-03-01 00:00" git commit -m second &&
+ echo a >>file &&
+ git add file &&
+ GIT_COMMITTER_DATE="2022-03-01 00:00" git commit -m third
+'
+
+test_expect_success 'git log --since-as-filter=...' '
+ git log --since-as-filter="2022-01-01" --format=%s >actual &&
+ cat >expect <<-\EOF &&
+ third
+ first
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'git log --children --since-as-filter=...' '
+ git log --children --since-as-filter="2022-01-01" --format=%s >actual &&
+ cat >expect <<-\EOF &&
+ third
+ first
+ EOF
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t4252-am-options.sh b/t/t4252-am-options.sh
new file mode 100755
index 0000000..e758e63
--- /dev/null
+++ b/t/t4252-am-options.sh
@@ -0,0 +1,78 @@
+#!/bin/sh
+
+test_description='git am with options and not losing them'
+. ./test-lib.sh
+
+tm="$TEST_DIRECTORY/t4252"
+
+test_expect_success setup '
+ cp "$tm/file-1-0" file-1 &&
+ cp "$tm/file-2-0" file-2 &&
+ git add file-1 file-2 &&
+ test_tick &&
+ git commit -m initial &&
+ git tag initial
+'
+
+test_expect_success 'interrupted am --whitespace=fix' '
+ rm -rf .git/rebase-apply &&
+ git reset --hard initial &&
+ test_must_fail git am --whitespace=fix "$tm"/am-test-1-? &&
+ git am --skip &&
+ grep 3 file-1 &&
+ grep "^Six$" file-2
+'
+
+test_expect_success 'interrupted am -C1' '
+ rm -rf .git/rebase-apply &&
+ git reset --hard initial &&
+ test_must_fail git am -C1 "$tm"/am-test-2-? &&
+ git am --skip &&
+ grep 3 file-1 &&
+ grep "^Three$" file-2
+'
+
+test_expect_success 'interrupted am -p2' '
+ rm -rf .git/rebase-apply &&
+ git reset --hard initial &&
+ test_must_fail git am -p2 "$tm"/am-test-3-? &&
+ git am --skip &&
+ grep 3 file-1 &&
+ grep "^Three$" file-2
+'
+
+test_expect_success 'interrupted am -C1 -p2' '
+ rm -rf .git/rebase-apply &&
+ git reset --hard initial &&
+ test_must_fail git am -p2 -C1 "$tm"/am-test-4-? &&
+ git am --skip &&
+ grep 3 file-1 &&
+ grep "^Three$" file-2
+'
+
+test_expect_success 'interrupted am --directory="frotz nitfol"' '
+ rm -rf .git/rebase-apply &&
+ git reset --hard initial &&
+ test_must_fail git am --directory="frotz nitfol" "$tm"/am-test-5-? &&
+ git am --skip &&
+ grep One "frotz nitfol/file-5"
+'
+
+test_expect_success 'apply to a funny path' '
+ with_sq="with'\''sq" &&
+ rm -fr .git/rebase-apply &&
+ git reset --hard initial &&
+ git am --directory="$with_sq" "$tm"/am-test-5-2 &&
+ test -f "$with_sq/file-5"
+'
+
+test_expect_success 'am --reject' '
+ rm -rf .git/rebase-apply &&
+ git reset --hard initial &&
+ test_must_fail git am --reject "$tm"/am-test-6-1 &&
+ grep "@@ -1,3 +1,3 @@" file-2.rej &&
+ test_must_fail git diff-files --exit-code --quiet file-2 &&
+ grep "[-]-reject" .git/rebase-apply/apply-opt
+'
+
+test_done
diff --git a/t/t4252/am-test-1-1 b/t/t4252/am-test-1-1
new file mode 100644
index 0000000..b0c09dc
--- /dev/null
+++ b/t/t4252/am-test-1-1
@@ -0,0 +1,19 @@
+From: A U Thor <au.thor@example.com>
+Date: Thu Dec 4 16:00:00 2008 -0800
+Subject: Three
+
+Application of this should be rejected because the first line in the
+context does not match.
+
+diff --git i/file-1 w/file-1
+index 06e567b..10f8342 100644
+--- i/file-1
++++ w/file-1
+@@ -1,6 +1,6 @@
+ One
+ 2
+-3
++Three
+ 4
+ 5
+ 6
diff --git a/t/t4252/am-test-1-2 b/t/t4252/am-test-1-2
new file mode 100644
index 0000000..1b874ae
--- /dev/null
+++ b/t/t4252/am-test-1-2
@@ -0,0 +1,21 @@
+From: A U Thor <au.thor@example.com>
+Date: Thu Dec 4 16:00:00 2008 -0800
+Subject: Six
+
+Applying this patch with --whitespace=fix should lose
+the trailing whitespace after "Six".
+
+diff --git i/file-2 w/file-2
+index 06e567b..b6f3a16 100644
+--- i/file-2
++++ w/file-2
+@@ -1,7 +1,7 @@
+ 1
+ 2
+-3
++Three
+ 4
+ 5
+-6
++Six
+ 7
diff --git a/t/t4252/am-test-2-1 b/t/t4252/am-test-2-1
new file mode 100644
index 0000000..feda94a
--- /dev/null
+++ b/t/t4252/am-test-2-1
@@ -0,0 +1,19 @@
+From: A U Thor <au.thor@example.com>
+Date: Thu Dec 4 16:00:00 2008 -0800
+Subject: Three
+
+Application of this should be rejected even with -C1 because the
+preimage line in the context does not match.
+
+diff --git i/file-1 w/file-1
+index 06e567b..10f8342 100644
+--- i/file-1
++++ w/file-1
+@@ -1,6 +1,6 @@
+ 1
+ 2
+-Tres
++Three
+ 4
+ 5
+ 6
diff --git a/t/t4252/am-test-2-2 b/t/t4252/am-test-2-2
new file mode 100644
index 0000000..2ac6600
--- /dev/null
+++ b/t/t4252/am-test-2-2
@@ -0,0 +1,21 @@
+From: A U Thor <au.thor@example.com>
+Date: Thu Dec 4 16:00:00 2008 -0800
+Subject: Six
+
+Applying this patch with -C1 should be successful even though
+the first line in the context does not match.
+
+diff --git i/file-2 w/file-2
+index 06e567b..b6f3a16 100644
+--- i/file-2
++++ w/file-2
+@@ -1,7 +1,7 @@
+ One
+ 2
+-3
++Three
+ 4
+ 5
+-6
++Six
+ 7
diff --git a/t/t4252/am-test-3-1 b/t/t4252/am-test-3-1
new file mode 100644
index 0000000..608e5ab
--- /dev/null
+++ b/t/t4252/am-test-3-1
@@ -0,0 +1,19 @@
+From: A U Thor <au.thor@example.com>
+Date: Thu Dec 4 16:00:00 2008 -0800
+Subject: Three
+
+Application of this should be rejected even with -p2 because the
+preimage line in the context does not match.
+
+diff --git i/junk/file-1 w/junk/file-1
+index 06e567b..10f8342 100644
+--- i/junk/file-1
++++ w/junk/file-1
+@@ -1,6 +1,6 @@
+ 1
+ 2
+-Tres
++Three
+ 4
+ 5
+ 6
diff --git a/t/t4252/am-test-3-2 b/t/t4252/am-test-3-2
new file mode 100644
index 0000000..0081b96
--- /dev/null
+++ b/t/t4252/am-test-3-2
@@ -0,0 +1,21 @@
+From: A U Thor <au.thor@example.com>
+Date: Thu Dec 4 16:00:00 2008 -0800
+Subject: Six
+
+Applying this patch with -p2 should be successful even though
+the patch is against a wrong level.
+
+diff --git i/junk/file-2 w/junk/file-2
+index 06e567b..b6f3a16 100644
+--- i/junk/file-2
++++ w/junk/file-2
+@@ -1,7 +1,7 @@
+ 1
+ 2
+-3
++Three
+ 4
+ 5
+-6
++Six
+ 7
diff --git a/t/t4252/am-test-4-1 b/t/t4252/am-test-4-1
new file mode 100644
index 0000000..e48cd6c
--- /dev/null
+++ b/t/t4252/am-test-4-1
@@ -0,0 +1,19 @@
+From: A U Thor <au.thor@example.com>
+Date: Thu Dec 4 16:00:00 2008 -0800
+Subject: Three
+
+Application of this should be rejected even with -C1 -p2 because
+the preimage line in the context does not match.
+
+diff --git i/junk/file-1 w/junk/file-1
+index 06e567b..10f8342 100644
+--- i/junk/file-1
++++ w/junk/file-1
+@@ -1,6 +1,6 @@
+ 1
+ 2
+-Tres
++Three
+ 4
+ 5
+ 6
diff --git a/t/t4252/am-test-4-2 b/t/t4252/am-test-4-2
new file mode 100644
index 0000000..0e69bfa
--- /dev/null
+++ b/t/t4252/am-test-4-2
@@ -0,0 +1,22 @@
+From: A U Thor <au.thor@example.com>
+Date: Thu Dec 4 16:00:00 2008 -0800
+Subject: Six
+
+Applying this patch with -C1 -p2 should be successful even though
+the patch is against a wrong level and the first context line does
+not match.
+
+diff --git i/junk/file-2 w/junk/file-2
+index 06e567b..b6f3a16 100644
+--- i/junk/file-2
++++ w/junk/file-2
+@@ -1,7 +1,7 @@
+ One
+ 2
+-3
++Three
+ 4
+ 5
+-6
++Six
+ 7
diff --git a/t/t4252/am-test-5-1 b/t/t4252/am-test-5-1
new file mode 100644
index 0000000..da7bf29
--- /dev/null
+++ b/t/t4252/am-test-5-1
@@ -0,0 +1,20 @@
+From: A U Thor <au.thor@example.com>
+Date: Thu Dec 4 16:00:00 2008 -0800
+Subject: Six
+
+Applying this patch with --directory='frotz nitfol' should fail
+
+diff --git i/junk/file-2 w/junk/file-2
+index 06e567b..b6f3a16 100644
+--- i/junk/file-2
++++ w/junk/file-2
+@@ -1,7 +1,7 @@
+ One
+ 2
+-3
++Three
+ 4
+ 5
+-6
++Six
+ 7
diff --git a/t/t4252/am-test-5-2 b/t/t4252/am-test-5-2
new file mode 100644
index 0000000..373025b
--- /dev/null
+++ b/t/t4252/am-test-5-2
@@ -0,0 +1,15 @@
+From: A U Thor <au.thor@example.com>
+Date: Thu Dec 4 16:00:00 2008 -0800
+Subject: Six
+
+Applying this patch with --directory='frotz nitfol' should succeed
+
+diff --git i/file-5 w/file-5
+new file mode 100644
+index 000000..1d6ed9f
+--- /dev/null
++++ w/file-5
+@@ -0,0 +1,3 @@
++One
++two
++three
diff --git a/t/t4252/am-test-6-1 b/t/t4252/am-test-6-1
new file mode 100644
index 0000000..a8859e9
--- /dev/null
+++ b/t/t4252/am-test-6-1
@@ -0,0 +1,21 @@
+From: A U Thor <au.thor@example.com>
+Date: Thu Dec 4 16:00:00 2008 -0800
+Subject: Huh
+
+Should fail and leave rejects
+
+diff --git i/file-2 w/file-2
+index 06e567b..b6f3a16 100644
+--- i/file-2
++++ w/file-2
+@@ -1,3 +1,3 @@
+-0
++One
+ 2
+ 3
+@@ -4,4 +4,4 @@
+ 4
+ 5
+-6
++Six
+ 7
diff --git a/t/t4252/file-1-0 b/t/t4252/file-1-0
new file mode 100644
index 0000000..06e567b
--- /dev/null
+++ b/t/t4252/file-1-0
@@ -0,0 +1,7 @@
+1
+2
+3
+4
+5
+6
+7
diff --git a/t/t4252/file-2-0 b/t/t4252/file-2-0
new file mode 100644
index 0000000..06e567b
--- /dev/null
+++ b/t/t4252/file-2-0
@@ -0,0 +1,7 @@
+1
+2
+3
+4
+5
+6
+7
diff --git a/t/t4253-am-keep-cr-dos.sh b/t/t4253-am-keep-cr-dos.sh
new file mode 100755
index 0000000..0ee69d2
--- /dev/null
+++ b/t/t4253-am-keep-cr-dos.sh
@@ -0,0 +1,101 @@
+#!/bin/sh
+#
+# Copyright (c) 2010 Stefan-W. Hahn
+#
+
+test_description='git-am mbox with dos line ending.
+
+'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+# Three patches which will be added as files with dos line ending.
+
+cat >file1 <<\EOF
+line 1
+EOF
+
+cat >file1a <<\EOF
+line 1
+line 4
+EOF
+
+cat >file2 <<\EOF
+line 1
+line 2
+EOF
+
+cat >file3 <<\EOF
+line 1
+line 2
+line 3
+EOF
+
+test_expect_success 'setup repository with dos files' '
+ append_cr <file1 >file &&
+ git add file &&
+ git commit -m Initial &&
+ git tag initial &&
+ append_cr <file2 >file &&
+ git commit -a -m Second &&
+ append_cr <file3 >file &&
+ git commit -a -m Third
+'
+
+test_expect_success 'am with dos files without --keep-cr' '
+ git checkout -b dosfiles initial &&
+ git format-patch -k initial..main &&
+ test_must_fail git am -k -3 000*.patch &&
+ git am --abort &&
+ rm -rf .git/rebase-apply 000*.patch
+'
+
+test_expect_success 'am with dos files with --keep-cr' '
+ git checkout -b dosfiles-keep-cr initial &&
+ git format-patch -k --stdout initial..main >output &&
+ git am --keep-cr -k -3 output &&
+ git diff --exit-code main
+'
+
+test_expect_success 'am with dos files config am.keepcr' '
+ git config am.keepcr 1 &&
+ git checkout -b dosfiles-conf-keepcr initial &&
+ git format-patch -k --stdout initial..main >output &&
+ git am -k -3 output &&
+ git diff --exit-code main
+'
+
+test_expect_success 'am with dos files config am.keepcr overridden by --no-keep-cr' '
+ git config am.keepcr 1 &&
+ git checkout -b dosfiles-conf-keepcr-override initial &&
+ git format-patch -k initial..main &&
+ test_must_fail git am -k -3 --no-keep-cr 000*.patch &&
+ git am --abort &&
+ rm -rf .git/rebase-apply 000*.patch
+'
+
+test_expect_success 'am with dos files with --keep-cr continue' '
+ git checkout -b dosfiles-keep-cr-continue initial &&
+ git format-patch -k initial..main &&
+ append_cr <file1a >file &&
+ git commit -m "different patch" file &&
+ test_must_fail git am --keep-cr -k -3 000*.patch &&
+ append_cr <file2 >file &&
+ git add file &&
+ git am -3 --resolved &&
+ git diff --exit-code main
+'
+
+test_expect_success 'am with unix files config am.keepcr overridden by --no-keep-cr' '
+ git config am.keepcr 1 &&
+ git checkout -b unixfiles-conf-keepcr-override initial &&
+ cp -f file1 file &&
+ git commit -m "line ending to unix" file &&
+ git format-patch -k initial..main &&
+ git am -k -3 --no-keep-cr 000*.patch &&
+ git diff --exit-code -w main
+'
+
+test_done
diff --git a/t/t4254-am-corrupt.sh b/t/t4254-am-corrupt.sh
new file mode 100755
index 0000000..45f1d4f
--- /dev/null
+++ b/t/t4254-am-corrupt.sh
@@ -0,0 +1,84 @@
+#!/bin/sh
+
+test_description='git am with corrupt input'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+make_mbox_with_nul () {
+ space=' '
+ q_nul_in_subject=
+ q_nul_in_body=
+ while test $# -ne 0
+ do
+ case "$1" in
+ subject) q_nul_in_subject='=00' ;;
+ body) q_nul_in_body='=00' ;;
+ esac &&
+ shift
+ done &&
+ cat <<-EOF
+ From ec7364544f690c560304f5a5de9428ea3b978b26 Mon Sep 17 00:00:00 2001
+ From: A U Thor <author@example.com>
+ Date: Sun, 19 Apr 2020 13:42:07 +0700
+ Subject: [PATCH] =?ISO-8859-1?q?=C4=CB${q_nul_in_subject}=D1=CF=D6?=
+ MIME-Version: 1.0
+ Content-Type: text/plain; charset=ISO-8859-1
+ Content-Transfer-Encoding: quoted-printable
+
+ abc${q_nul_in_body}def
+ ---
+ diff --git a/afile b/afile
+ new file mode 100644
+ index 0000000000..e69de29bb2
+ --$space
+ 2.26.1
+ EOF
+}
+
+test_expect_success setup '
+ # Note the missing "+++" line:
+ cat >bad-patch.diff <<-\EOF &&
+ From: A U Thor <au.thor@example.com>
+ diff --git a/f b/f
+ index 7898192..6178079 100644
+ --- a/f
+ @@ -1 +1 @@
+ -a
+ +b
+ EOF
+
+ echo a >f &&
+ git add f &&
+ test_tick &&
+ git commit -m initial
+'
+
+# This used to fail before, too, but with a different diagnostic.
+# fatal: unable to write file '(null)' mode 100644: Bad address
+# Also, it had the unwanted side-effect of deleting f.
+test_expect_success 'try to apply corrupted patch' '
+ test_when_finished "git am --abort" &&
+ test_must_fail git -c advice.amWorkDir=false am bad-patch.diff 2>actual &&
+ echo "error: git diff header lacks filename information (line 4)" >expected &&
+ test_path_is_file f &&
+ test_cmp expected actual
+'
+
+test_expect_success "NUL in commit message's body" '
+ test_when_finished "git am --abort" &&
+ make_mbox_with_nul body >body.patch &&
+ test_must_fail git am body.patch 2>err &&
+ grep "a NUL byte in commit log message not allowed" err
+'
+
+test_expect_success "NUL in commit message's header" "
+ test_when_finished 'git am --abort' &&
+ make_mbox_with_nul subject >subject.patch &&
+ test_must_fail git mailinfo msg patch <subject.patch 2>err &&
+ grep \"a NUL byte in 'Subject' is not allowed\" err &&
+ test_must_fail git am subject.patch 2>err &&
+ grep \"a NUL byte in 'Subject' is not allowed\" err
+"
+
+test_done
diff --git a/t/t4255-am-submodule.sh b/t/t4255-am-submodule.sh
new file mode 100755
index 0000000..a7ba08f
--- /dev/null
+++ b/t/t4255-am-submodule.sh
@@ -0,0 +1,97 @@
+#!/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" >patch &&
+ may_only_be_test_must_fail "$2" &&
+ $2 git am patch
+}
+
+test_submodule_switch_func "am"
+
+am_3way () {
+ git format-patch --stdout --ignore-submodules=dirty "..$1" >patch &&
+ may_only_be_test_must_fail "$2" &&
+ $2 git am --3way patch
+}
+
+KNOWN_FAILURE_NOFF_MERGE_ATTEMPTS_TO_MERGE_REMOVED_SUBMODULE_FILES=1
+test_submodule_switch_func "am_3way"
+
+test_expect_success 'setup diff.submodule' '
+ test_commit one &&
+ INITIAL=$(git rev-parse HEAD) &&
+
+ git init submodule &&
+ (
+ cd submodule &&
+ test_commit two &&
+ git rev-parse HEAD >../initial-submodule
+ ) &&
+ git submodule add ./submodule &&
+ git commit -m first &&
+
+ (
+ cd submodule &&
+ test_commit three &&
+ git rev-parse HEAD >../first-submodule
+ ) &&
+ git add submodule &&
+ git commit -m second &&
+ SECOND=$(git rev-parse HEAD) &&
+
+ (
+ cd submodule &&
+ git mv two.t four.t &&
+ git commit -m "second submodule" &&
+ git rev-parse HEAD >../second-submodule
+ ) &&
+ test_commit four &&
+ git add submodule &&
+ git commit --amend --no-edit &&
+ THIRD=$(git rev-parse HEAD) &&
+ git submodule update --init
+'
+
+run_test() {
+ START_COMMIT=$1 &&
+ EXPECT=$2 &&
+ # Abort any merges in progress: the previous
+ # test may have failed, and we should clean up.
+ test_might_fail git am --abort &&
+ git reset --hard $START_COMMIT &&
+ rm -f *.patch &&
+ git format-patch -1 &&
+ git reset --hard $START_COMMIT^ &&
+ git submodule update &&
+ git am *.patch &&
+ git submodule update &&
+ git -C submodule rev-parse HEAD >actual &&
+ test_cmp $EXPECT actual
+}
+
+test_expect_success 'diff.submodule unset' '
+ test_unconfig diff.submodule &&
+ run_test $SECOND first-submodule
+'
+
+test_expect_success 'diff.submodule unset with extra file' '
+ test_unconfig diff.submodule &&
+ run_test $THIRD second-submodule
+'
+
+test_expect_success 'diff.submodule=log' '
+ test_config diff.submodule log &&
+ run_test $SECOND first-submodule
+'
+
+test_expect_success 'diff.submodule=log with extra file' '
+ test_config diff.submodule log &&
+ run_test $THIRD second-submodule
+'
+
+test_done
diff --git a/t/t4256-am-format-flowed.sh b/t/t4256-am-format-flowed.sh
new file mode 100755
index 0000000..92d8c8b
--- /dev/null
+++ b/t/t4256-am-format-flowed.sh
@@ -0,0 +1,20 @@
+#!/bin/sh
+
+test_description='test format=flowed support of git am'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ cp "$TEST_DIRECTORY/t4256/1/mailinfo.c.orig" mailinfo.c &&
+ git add mailinfo.c &&
+ git commit -m initial
+'
+
+test_expect_success 'am with format=flowed' '
+ git am <"$TEST_DIRECTORY/t4256/1/patch" 2>stderr &&
+ test_grep "warning: Patch sent with format=flowed" stderr &&
+ test_cmp "$TEST_DIRECTORY/t4256/1/mailinfo.c" mailinfo.c
+'
+
+test_done
diff --git a/t/t4256/1/mailinfo.c b/t/t4256/1/mailinfo.c
new file mode 100644
index 0000000..b395adb
--- /dev/null
+++ b/t/t4256/1/mailinfo.c
@@ -0,0 +1,1245 @@
+#include "cache.h"
+#include "config.h"
+#include "utf8.h"
+#include "strbuf.h"
+#include "mailinfo.h"
+
+static void cleanup_space(struct strbuf *sb)
+{
+ size_t pos, cnt;
+ for (pos = 0; pos < sb->len; pos++) {
+ if (isspace(sb->buf[pos])) {
+ sb->buf[pos] = ' ';
+ for (cnt = 0; isspace(sb->buf[pos + cnt + 1]); cnt++);
+ strbuf_remove(sb, pos + 1, cnt);
+ }
+ }
+}
+
+static void get_sane_name(struct strbuf *out, struct strbuf *name, struct strbuf *email)
+{
+ struct strbuf *src = name;
+ if (name->len < 3 || 60 < name->len || strchr(name->buf, '@') ||
+ strchr(name->buf, '<') || strchr(name->buf, '>'))
+ src = email;
+ else if (name == out)
+ return;
+ strbuf_reset(out);
+ strbuf_addbuf(out, src);
+}
+
+static void parse_bogus_from(struct mailinfo *mi, const struct strbuf *line)
+{
+ /* John Doe <johndoe> */
+
+ char *bra, *ket;
+ /* This is fallback, so do not bother if we already have an
+ * e-mail address.
+ */
+ if (mi->email.len)
+ return;
+
+ bra = strchr(line->buf, '<');
+ if (!bra)
+ return;
+ ket = strchr(bra, '>');
+ if (!ket)
+ return;
+
+ strbuf_reset(&mi->email);
+ strbuf_add(&mi->email, bra + 1, ket - bra - 1);
+
+ strbuf_reset(&mi->name);
+ strbuf_add(&mi->name, line->buf, bra - line->buf);
+ strbuf_trim(&mi->name);
+ get_sane_name(&mi->name, &mi->name, &mi->email);
+}
+
+static const char *unquote_comment(struct strbuf *outbuf, const char *in)
+{
+ int c;
+ int take_next_literally = 0;
+
+ strbuf_addch(outbuf, '(');
+
+ while ((c = *in++) != 0) {
+ if (take_next_literally == 1) {
+ take_next_literally = 0;
+ } else {
+ switch (c) {
+ case '\\':
+ take_next_literally = 1;
+ continue;
+ case '(':
+ in = unquote_comment(outbuf, in);
+ continue;
+ case ')':
+ strbuf_addch(outbuf, ')');
+ return in;
+ }
+ }
+
+ strbuf_addch(outbuf, c);
+ }
+
+ return in;
+}
+
+static const char *unquote_quoted_string(struct strbuf *outbuf, const char *in)
+{
+ int c;
+ int take_next_literally = 0;
+
+ while ((c = *in++) != 0) {
+ if (take_next_literally == 1) {
+ take_next_literally = 0;
+ } else {
+ switch (c) {
+ case '\\':
+ take_next_literally = 1;
+ continue;
+ case '"':
+ return in;
+ }
+ }
+
+ strbuf_addch(outbuf, c);
+ }
+
+ return in;
+}
+
+static void unquote_quoted_pair(struct strbuf *line)
+{
+ struct strbuf outbuf;
+ const char *in = line->buf;
+ int c;
+
+ strbuf_init(&outbuf, line->len);
+
+ while ((c = *in++) != 0) {
+ switch (c) {
+ case '"':
+ in = unquote_quoted_string(&outbuf, in);
+ continue;
+ case '(':
+ in = unquote_comment(&outbuf, in);
+ continue;
+ }
+
+ strbuf_addch(&outbuf, c);
+ }
+
+ strbuf_swap(&outbuf, line);
+ strbuf_release(&outbuf);
+
+}
+
+static void handle_from(struct mailinfo *mi, const struct strbuf *from)
+{
+ char *at;
+ size_t el;
+ struct strbuf f;
+
+ strbuf_init(&f, from->len);
+ strbuf_addbuf(&f, from);
+
+ unquote_quoted_pair(&f);
+
+ at = strchr(f.buf, '@');
+ if (!at) {
+ parse_bogus_from(mi, from);
+ goto out;
+ }
+
+ /*
+ * If we already have one email, don't take any confusing lines
+ */
+ if (mi->email.len && strchr(at + 1, '@'))
+ goto out;
+
+ /* Pick up the string around '@', possibly delimited with <>
+ * pair; that is the email part.
+ */
+ while (at > f.buf) {
+ char c = at[-1];
+ if (isspace(c))
+ break;
+ if (c == '<') {
+ at[-1] = ' ';
+ break;
+ }
+ at--;
+ }
+ el = strcspn(at, " \n\t\r\v\f>");
+ strbuf_reset(&mi->email);
+ strbuf_add(&mi->email, at, el);
+ strbuf_remove(&f, at - f.buf, el + (at[el] ? 1 : 0));
+
+ /* The remainder is name. It could be
+ *
+ * - "John Doe <john.doe@xz>" (a), or
+ * - "john.doe@xz (John Doe)" (b), or
+ * - "John (zzz) Doe <john.doe@xz> (Comment)" (c)
+ *
+ * but we have removed the email part, so
+ *
+ * - remove extra spaces which could stay after email (case 'c'), and
+ * - trim from both ends, possibly removing the () pair at the end
+ * (cases 'a' and 'b').
+ */
+ cleanup_space(&f);
+ strbuf_trim(&f);
+ if (f.buf[0] == '(' && f.len && f.buf[f.len - 1] == ')') {
+ strbuf_remove(&f, 0, 1);
+ strbuf_setlen(&f, f.len - 1);
+ }
+
+ get_sane_name(&mi->name, &f, &mi->email);
+out:
+ strbuf_release(&f);
+}
+
+static void handle_header(struct strbuf **out, const struct strbuf *line)
+{
+ if (!*out) {
+ *out = xmalloc(sizeof(struct strbuf));
+ strbuf_init(*out, line->len);
+ } else
+ strbuf_reset(*out);
+
+ strbuf_addbuf(*out, line);
+}
+
+/* NOTE NOTE NOTE. We do not claim we do full MIME. We just attempt
+ * to have enough heuristics to grok MIME encoded patches often found
+ * on our mailing lists. For example, we do not even treat header lines
+ * case insensitively.
+ */
+
+static int slurp_attr(const char *line, const char *name, struct strbuf *attr)
+{
+ const char *ends, *ap = strcasestr(line, name);
+ size_t sz;
+
+ strbuf_setlen(attr, 0);
+ if (!ap)
+ return 0;
+ ap += strlen(name);
+ if (*ap == '"') {
+ ap++;
+ ends = "\"";
+ }
+ else
+ ends = "; \t";
+ sz = strcspn(ap, ends);
+ strbuf_add(attr, ap, sz);
+ return 1;
+}
+
+static int has_attr_value(const char *line, const char *name, const char *value)
+{
+ struct strbuf sb = STRBUF_INIT;
+ int rc = slurp_attr(line, name, &sb) && !strcasecmp(sb.buf, value);
+ strbuf_release(&sb);
+ return rc;
+}
+
+static void handle_content_type(struct mailinfo *mi, struct strbuf *line)
+{
+ struct strbuf *boundary = xmalloc(sizeof(struct strbuf));
+ strbuf_init(boundary, line->len);
+
+ mi->format_flowed = has_attr_value(line->buf, "format=", "flowed");
+ mi->delsp = has_attr_value(line->buf, "delsp=", "yes");
+
+ if (slurp_attr(line->buf, "boundary=", boundary)) {
+ strbuf_insert(boundary, 0, "--", 2);
+ if (++mi->content_top >= &mi->content[MAX_BOUNDARIES]) {
+ error("Too many boundaries to handle");
+ mi->input_error = -1;
+ mi->content_top = &mi->content[MAX_BOUNDARIES] - 1;
+ return;
+ }
+ *(mi->content_top) = boundary;
+ boundary = NULL;
+ }
+ slurp_attr(line->buf, "charset=", &mi->charset);
+
+ if (boundary) {
+ strbuf_release(boundary);
+ free(boundary);
+ }
+}
+
+static void handle_content_transfer_encoding(struct mailinfo *mi,
+ const struct strbuf *line)
+{
+ if (strcasestr(line->buf, "base64"))
+ mi->transfer_encoding = TE_BASE64;
+ else if (strcasestr(line->buf, "quoted-printable"))
+ mi->transfer_encoding = TE_QP;
+ else
+ mi->transfer_encoding = TE_DONTCARE;
+}
+
+static int is_multipart_boundary(struct mailinfo *mi, const struct strbuf *line)
+{
+ struct strbuf *content_top = *(mi->content_top);
+
+ return ((content_top->len <= line->len) &&
+ !memcmp(line->buf, content_top->buf, content_top->len));
+}
+
+static void cleanup_subject(struct mailinfo *mi, struct strbuf *subject)
+{
+ size_t at = 0;
+
+ while (at < subject->len) {
+ char *pos;
+ size_t remove;
+
+ switch (subject->buf[at]) {
+ case 'r': case 'R':
+ if (subject->len <= at + 3)
+ break;
+ if ((subject->buf[at + 1] == 'e' ||
+ subject->buf[at + 1] == 'E') &&
+ subject->buf[at + 2] == ':') {
+ strbuf_remove(subject, at, 3);
+ continue;
+ }
+ at++;
+ break;
+ case ' ': case '\t': case ':':
+ strbuf_remove(subject, at, 1);
+ continue;
+ case '[':
+ pos = strchr(subject->buf + at, ']');
+ if (!pos)
+ break;
+ remove = pos - subject->buf + at + 1;
+ if (!mi->keep_non_patch_brackets_in_subject ||
+ (7 <= remove &&
+ memmem(subject->buf + at, remove, "PATCH", 5)))
+ strbuf_remove(subject, at, remove);
+ else {
+ at += remove;
+ /*
+ * If the input had a space after the ], keep
+ * it. We don't bother with finding the end of
+ * the space, since we later normalize it
+ * anyway.
+ */
+ if (isspace(subject->buf[at]))
+ at += 1;
+ }
+ continue;
+ }
+ break;
+ }
+ strbuf_trim(subject);
+}
+
+#define MAX_HDR_PARSED 10
+static const char *header[MAX_HDR_PARSED] = {
+ "From","Subject","Date",
+};
+
+static inline int cmp_header(const struct strbuf *line, const char *hdr)
+{
+ int len = strlen(hdr);
+ return !strncasecmp(line->buf, hdr, len) && line->len > len &&
+ line->buf[len] == ':' && isspace(line->buf[len + 1]);
+}
+
+static int is_format_patch_separator(const char *line, int len)
+{
+ static const char SAMPLE[] =
+ "From e6807f3efca28b30decfecb1732a56c7db1137ee Mon Sep 17 00:00:00 2001\n";
+ const char *cp;
+
+ if (len != strlen(SAMPLE))
+ return 0;
+ if (!skip_prefix(line, "From ", &cp))
+ return 0;
+ if (strspn(cp, "0123456789abcdef") != 40)
+ return 0;
+ cp += 40;
+ return !memcmp(SAMPLE + (cp - line), cp, strlen(SAMPLE) - (cp - line));
+}
+
+static struct strbuf *decode_q_segment(const struct strbuf *q_seg, int rfc2047)
+{
+ const char *in = q_seg->buf;
+ int c;
+ struct strbuf *out = xmalloc(sizeof(struct strbuf));
+ strbuf_init(out, q_seg->len);
+
+ while ((c = *in++) != 0) {
+ if (c == '=') {
+ int ch, d = *in;
+ if (d == '\n' || !d)
+ break; /* drop trailing newline */
+ ch = hex2chr(in);
+ if (ch >= 0) {
+ strbuf_addch(out, ch);
+ in += 2;
+ continue;
+ }
+ /* garbage -- fall through */
+ }
+ if (rfc2047 && c == '_') /* rfc2047 4.2 (2) */
+ c = 0x20;
+ strbuf_addch(out, c);
+ }
+ return out;
+}
+
+static struct strbuf *decode_b_segment(const struct strbuf *b_seg)
+{
+ /* Decode in..ep, possibly in-place to ot */
+ int c, pos = 0, acc = 0;
+ const char *in = b_seg->buf;
+ struct strbuf *out = xmalloc(sizeof(struct strbuf));
+ strbuf_init(out, b_seg->len);
+
+ while ((c = *in++) != 0) {
+ if (c == '+')
+ c = 62;
+ else if (c == '/')
+ c = 63;
+ else if ('A' <= c && c <= 'Z')
+ c -= 'A';
+ else if ('a' <= c && c <= 'z')
+ c -= 'a' - 26;
+ else if ('0' <= c && c <= '9')
+ c -= '0' - 52;
+ else
+ continue; /* garbage */
+ switch (pos++) {
+ case 0:
+ acc = (c << 2);
+ break;
+ case 1:
+ strbuf_addch(out, (acc | (c >> 4)));
+ acc = (c & 15) << 4;
+ break;
+ case 2:
+ strbuf_addch(out, (acc | (c >> 2)));
+ acc = (c & 3) << 6;
+ break;
+ case 3:
+ strbuf_addch(out, (acc | c));
+ acc = pos = 0;
+ break;
+ }
+ }
+ return out;
+}
+
+static int convert_to_utf8(struct mailinfo *mi,
+ struct strbuf *line, const char *charset)
+{
+ char *out;
+
+ if (!mi->metainfo_charset || !charset || !*charset)
+ return 0;
+
+ if (same_encoding(mi->metainfo_charset, charset))
+ return 0;
+ out = reencode_string(line->buf, mi->metainfo_charset, charset);
+ if (!out) {
+ mi->input_error = -1;
+ return error("cannot convert from %s to %s",
+ charset, mi->metainfo_charset);
+ }
+ strbuf_attach(line, out, strlen(out), strlen(out));
+ return 0;
+}
+
+static void decode_header(struct mailinfo *mi, struct strbuf *it)
+{
+ char *in, *ep, *cp;
+ struct strbuf outbuf = STRBUF_INIT, *dec;
+ struct strbuf charset_q = STRBUF_INIT, piecebuf = STRBUF_INIT;
+ int found_error = 1; /* pessimism */
+
+ in = it->buf;
+ while (in - it->buf <= it->len && (ep = strstr(in, "=?")) != NULL) {
+ int encoding;
+ strbuf_reset(&charset_q);
+ strbuf_reset(&piecebuf);
+
+ if (in != ep) {
+ /*
+ * We are about to process an encoded-word
+ * that begins at ep, but there is something
+ * before the encoded word.
+ */
+ char *scan;
+ for (scan = in; scan < ep; scan++)
+ if (!isspace(*scan))
+ break;
+
+ if (scan != ep || in == it->buf) {
+ /*
+ * We should not lose that "something",
+ * unless we have just processed an
+ * encoded-word, and there is only LWS
+ * before the one we are about to process.
+ */
+ strbuf_add(&outbuf, in, ep - in);
+ }
+ }
+ /* E.g.
+ * ep : "=?iso-2022-jp?B?GyR...?= foo"
+ * ep : "=?ISO-8859-1?Q?Foo=FCbar?= baz"
+ */
+ ep += 2;
+
+ if (ep - it->buf >= it->len || !(cp = strchr(ep, '?')))
+ goto release_return;
+
+ if (cp + 3 - it->buf > it->len)
+ goto release_return;
+ strbuf_add(&charset_q, ep, cp - ep);
+
+ encoding = cp[1];
+ if (!encoding || cp[2] != '?')
+ goto release_return;
+ ep = strstr(cp + 3, "?=");
+ if (!ep)
+ goto release_return;
+ strbuf_add(&piecebuf, cp + 3, ep - cp - 3);
+ switch (tolower(encoding)) {
+ default:
+ goto release_return;
+ case 'b':
+ dec = decode_b_segment(&piecebuf);
+ break;
+ case 'q':
+ dec = decode_q_segment(&piecebuf, 1);
+ break;
+ }
+ if (convert_to_utf8(mi, dec, charset_q.buf))
+ goto release_return;
+
+ strbuf_addbuf(&outbuf, dec);
+ strbuf_release(dec);
+ free(dec);
+ in = ep + 2;
+ }
+ strbuf_addstr(&outbuf, in);
+ strbuf_reset(it);
+ strbuf_addbuf(it, &outbuf);
+ found_error = 0;
+release_return:
+ strbuf_release(&outbuf);
+ strbuf_release(&charset_q);
+ strbuf_release(&piecebuf);
+
+ if (found_error)
+ mi->input_error = -1;
+}
+
+static int check_header(struct mailinfo *mi,
+ const struct strbuf *line,
+ struct strbuf *hdr_data[], int overwrite)
+{
+ int i, ret = 0, len;
+ struct strbuf sb = STRBUF_INIT;
+
+ /* search for the interesting parts */
+ for (i = 0; header[i]; i++) {
+ int len = strlen(header[i]);
+ if ((!hdr_data[i] || overwrite) && cmp_header(line, header[i])) {
+ /* Unwrap inline B and Q encoding, and optionally
+ * normalize the meta information to utf8.
+ */
+ strbuf_add(&sb, line->buf + len + 2, line->len - len - 2);
+ decode_header(mi, &sb);
+ handle_header(&hdr_data[i], &sb);
+ ret = 1;
+ goto check_header_out;
+ }
+ }
+
+ /* Content stuff */
+ if (cmp_header(line, "Content-Type")) {
+ len = strlen("Content-Type: ");
+ strbuf_add(&sb, line->buf + len, line->len - len);
+ decode_header(mi, &sb);
+ strbuf_insert(&sb, 0, "Content-Type: ", len);
+ handle_content_type(mi, &sb);
+ ret = 1;
+ goto check_header_out;
+ }
+ if (cmp_header(line, "Content-Transfer-Encoding")) {
+ len = strlen("Content-Transfer-Encoding: ");
+ strbuf_add(&sb, line->buf + len, line->len - len);
+ decode_header(mi, &sb);
+ handle_content_transfer_encoding(mi, &sb);
+ ret = 1;
+ goto check_header_out;
+ }
+ if (cmp_header(line, "Message-Id")) {
+ len = strlen("Message-Id: ");
+ strbuf_add(&sb, line->buf + len, line->len - len);
+ decode_header(mi, &sb);
+ if (mi->add_message_id)
+ mi->message_id = strbuf_detach(&sb, NULL);
+ ret = 1;
+ goto check_header_out;
+ }
+
+check_header_out:
+ strbuf_release(&sb);
+ return ret;
+}
+
+/*
+ * Returns 1 if the given line or any line beginning with the given line is an
+ * in-body header (that is, check_header will succeed when passed
+ * mi->s_hdr_data).
+ */
+static int is_inbody_header(const struct mailinfo *mi,
+ const struct strbuf *line)
+{
+ int i;
+ for (i = 0; header[i]; i++)
+ if (!mi->s_hdr_data[i] && cmp_header(line, header[i]))
+ return 1;
+ return 0;
+}
+
+static void decode_transfer_encoding(struct mailinfo *mi, struct strbuf *line)
+{
+ struct strbuf *ret;
+
+ switch (mi->transfer_encoding) {
+ case TE_QP:
+ ret = decode_q_segment(line, 0);
+ break;
+ case TE_BASE64:
+ ret = decode_b_segment(line);
+ break;
+ case TE_DONTCARE:
+ default:
+ return;
+ }
+ strbuf_reset(line);
+ strbuf_addbuf(line, ret);
+ strbuf_release(ret);
+ free(ret);
+}
+
+static inline int patchbreak(const struct strbuf *line)
+{
+ size_t i;
+
+ /* Beginning of a "diff -" header? */
+ if (starts_with(line->buf, "diff -"))
+ return 1;
+
+ /* CVS "Index: " line? */
+ if (starts_with(line->buf, "Index: "))
+ return 1;
+
+ /*
+ * "--- <filename>" starts patches without headers
+ * "---<sp>*" is a manual separator
+ */
+ if (line->len < 4)
+ return 0;
+
+ if (starts_with(line->buf, "---")) {
+ /* space followed by a filename? */
+ if (line->buf[3] == ' ' && !isspace(line->buf[4]))
+ return 1;
+ /* Just whitespace? */
+ for (i = 3; i < line->len; i++) {
+ unsigned char c = line->buf[i];
+ if (c == '\n')
+ return 1;
+ if (!isspace(c))
+ break;
+ }
+ return 0;
+ }
+ return 0;
+}
+
+static int is_scissors_line(const char *line)
+{
+ const char *c;
+ int scissors = 0, gap = 0;
+ const char *first_nonblank = NULL, *last_nonblank = NULL;
+ int visible, perforation = 0, in_perforation = 0;
+
+ for (c = line; *c; c++) {
+ if (isspace(*c)) {
+ if (in_perforation) {
+ perforation++;
+ gap++;
+ }
+ continue;
+ }
+ last_nonblank = c;
+ if (first_nonblank == NULL)
+ first_nonblank = c;
+ if (*c == '-') {
+ in_perforation = 1;
+ perforation++;
+ continue;
+ }
+ if ((!memcmp(c, ">8", 2) || !memcmp(c, "8<", 2) ||
+ !memcmp(c, ">%", 2) || !memcmp(c, "%<", 2))) {
+ in_perforation = 1;
+ perforation += 2;
+ scissors += 2;
+ c++;
+ continue;
+ }
+ in_perforation = 0;
+ }
+
+ /*
+ * The mark must be at least 8 bytes long (e.g. "-- >8 --").
+ * Even though there can be arbitrary cruft on the same line
+ * (e.g. "cut here"), in order to avoid misidentification, the
+ * perforation must occupy more than a third of the visible
+ * width of the line, and dashes and scissors must occupy more
+ * than half of the perforation.
+ */
+
+ if (first_nonblank && last_nonblank)
+ visible = last_nonblank - first_nonblank + 1;
+ else
+ visible = 0;
+ return (scissors && 8 <= visible &&
+ visible < perforation * 3 &&
+ gap * 2 < perforation);
+}
+
+static void flush_inbody_header_accum(struct mailinfo *mi)
+{
+ if (!mi->inbody_header_accum.len)
+ return;
+ if (!check_header(mi, &mi->inbody_header_accum, mi->s_hdr_data, 0))
+ BUG("inbody_header_accum, if not empty, must always contain a valid in-body header");
+ strbuf_reset(&mi->inbody_header_accum);
+}
+
+static int check_inbody_header(struct mailinfo *mi, const struct strbuf *line)
+{
+ if (mi->inbody_header_accum.len &&
+ (line->buf[0] == ' ' || line->buf[0] == '\t')) {
+ if (mi->use_scissors && is_scissors_line(line->buf)) {
+ /*
+ * This is a scissors line; do not consider this line
+ * as a header continuation line.
+ */
+ flush_inbody_header_accum(mi);
+ return 0;
+ }
+ strbuf_strip_suffix(&mi->inbody_header_accum, "\n");
+ strbuf_addbuf(&mi->inbody_header_accum, line);
+ return 1;
+ }
+
+ flush_inbody_header_accum(mi);
+
+ if (starts_with(line->buf, ">From") && isspace(line->buf[5]))
+ return is_format_patch_separator(line->buf + 1, line->len - 1);
+ if (starts_with(line->buf, "[PATCH]") && isspace(line->buf[7])) {
+ int i;
+ for (i = 0; header[i]; i++)
+ if (!strcmp("Subject", header[i])) {
+ handle_header(&mi->s_hdr_data[i], line);
+ return 1;
+ }
+ return 0;
+ }
+ if (is_inbody_header(mi, line)) {
+ strbuf_addbuf(&mi->inbody_header_accum, line);
+ return 1;
+ }
+ return 0;
+}
+
+static int handle_commit_msg(struct mailinfo *mi, struct strbuf *line)
+{
+ assert(!mi->filter_stage);
+
+ if (mi->header_stage) {
+ if (!line->len || (line->len == 1 && line->buf[0] == '\n')) {
+ if (mi->inbody_header_accum.len) {
+ flush_inbody_header_accum(mi);
+ mi->header_stage = 0;
+ }
+ return 0;
+ }
+ }
+
+ if (mi->use_inbody_headers && mi->header_stage) {
+ mi->header_stage = check_inbody_header(mi, line);
+ if (mi->header_stage)
+ return 0;
+ } else
+ /* Only trim the first (blank) line of the commit message
+ * when ignoring in-body headers.
+ */
+ mi->header_stage = 0;
+
+ /* normalize the log message to UTF-8. */
+ if (convert_to_utf8(mi, line, mi->charset.buf))
+ return 0; /* mi->input_error already set */
+
+ if (mi->use_scissors && is_scissors_line(line->buf)) {
+ int i;
+
+ strbuf_setlen(&mi->log_message, 0);
+ mi->header_stage = 1;
+
+ /*
+ * We may have already read "secondary headers"; purge
+ * them to give ourselves a clean restart.
+ */
+ for (i = 0; header[i]; i++) {
+ if (mi->s_hdr_data[i])
+ strbuf_release(mi->s_hdr_data[i]);
+ mi->s_hdr_data[i] = NULL;
+ }
+ return 0;
+ }
+
+ if (patchbreak(line)) {
+ if (mi->message_id)
+ strbuf_addf(&mi->log_message,
+ "Message-Id: %s\n", mi->message_id);
+ return 1;
+ }
+
+ strbuf_addbuf(&mi->log_message, line);
+ return 0;
+}
+
+static void handle_patch(struct mailinfo *mi, const struct strbuf *line)
+{
+ fwrite(line->buf, 1, line->len, mi->patchfile);
+ mi->patch_lines++;
+}
+
+static void handle_filter(struct mailinfo *mi, struct strbuf *line)
+{
+ switch (mi->filter_stage) {
+ case 0:
+ if (!handle_commit_msg(mi, line))
+ break;
+ mi->filter_stage++;
+ /* fallthrough */
+ case 1:
+ handle_patch(mi, line);
+ break;
+ }
+}
+
+static int is_rfc2822_header(const struct strbuf *line)
+{
+ /*
+ * The section that defines the loosest possible
+ * field name is "3.6.8 Optional fields".
+ *
+ * optional-field = field-name ":" unstructured CRLF
+ * field-name = 1*ftext
+ * ftext = %d33-57 / %59-126
+ */
+ int ch;
+ char *cp = line->buf;
+
+ /* Count mbox From headers as headers */
+ if (starts_with(cp, "From ") || starts_with(cp, ">From "))
+ return 1;
+
+ while ((ch = *cp++)) {
+ if (ch == ':')
+ return 1;
+ if ((33 <= ch && ch <= 57) ||
+ (59 <= ch && ch <= 126))
+ continue;
+ break;
+ }
+ return 0;
+}
+
+static int read_one_header_line(struct strbuf *line, FILE *in)
+{
+ struct strbuf continuation = STRBUF_INIT;
+
+ /* Get the first part of the line. */
+ if (strbuf_getline_lf(line, in))
+ return 0;
+
+ /*
+ * Is it an empty line or not a valid rfc2822 header?
+ * If so, stop here, and return false ("not a header")
+ */
+ strbuf_rtrim(line);
+ if (!line->len || !is_rfc2822_header(line)) {
+ /* Re-add the newline */
+ strbuf_addch(line, '\n');
+ return 0;
+ }
+
+ /*
+ * Now we need to eat all the continuation lines..
+ * Yuck, 2822 header "folding"
+ */
+ for (;;) {
+ int peek;
+
+ peek = fgetc(in);
+ if (peek == EOF)
+ break;
+ ungetc(peek, in);
+ if (peek != ' ' && peek != '\t')
+ break;
+ if (strbuf_getline_lf(&continuation, in))
+ break;
+ continuation.buf[0] = ' ';
+ strbuf_rtrim(&continuation);
+ strbuf_addbuf(line, &continuation);
+ }
+ strbuf_release(&continuation);
+
+ return 1;
+}
+
+static int find_boundary(struct mailinfo *mi, struct strbuf *line)
+{
+ while (!strbuf_getline_lf(line, mi->input)) {
+ if (*(mi->content_top) && is_multipart_boundary(mi, line))
+ return 1;
+ }
+ return 0;
+}
+
+static int handle_boundary(struct mailinfo *mi, struct strbuf *line)
+{
+ struct strbuf newline = STRBUF_INIT;
+
+ strbuf_addch(&newline, '\n');
+again:
+ if (line->len >= (*(mi->content_top))->len + 2 &&
+ !memcmp(line->buf + (*(mi->content_top))->len, "--", 2)) {
+ /* we hit an end boundary */
+ /* pop the current boundary off the stack */
+ strbuf_release(*(mi->content_top));
+ FREE_AND_NULL(*(mi->content_top));
+
+ /* technically won't happen as is_multipart_boundary()
+ will fail first. But just in case..
+ */
+ if (--mi->content_top < mi->content) {
+ error("Detected mismatched boundaries, can't recover");
+ mi->input_error = -1;
+ mi->content_top = mi->content;
+ strbuf_release(&newline);
+ return 0;
+ }
+ handle_filter(mi, &newline);
+ strbuf_release(&newline);
+ if (mi->input_error)
+ return 0;
+
+ /* skip to the next boundary */
+ if (!find_boundary(mi, line))
+ return 0;
+ goto again;
+ }
+
+ /* set some defaults */
+ mi->transfer_encoding = TE_DONTCARE;
+ strbuf_reset(&mi->charset);
+
+ /* slurp in this section's info */
+ while (read_one_header_line(line, mi->input))
+ check_header(mi, line, mi->p_hdr_data, 0);
+
+ strbuf_release(&newline);
+ /* replenish line */
+ if (strbuf_getline_lf(line, mi->input))
+ return 0;
+ strbuf_addch(line, '\n');
+ return 1;
+}
+
+static void handle_filter_flowed(struct mailinfo *mi, struct strbuf *line,
+ struct strbuf *prev)
+{
+ size_t len = line->len;
+ const char *rest;
+
+ if (!mi->format_flowed) {
+ handle_filter(mi, line);
+ return;
+ }
+
+ if (line->buf[len - 1] == '\n') {
+ len--;
+ if (len && line->buf[len - 1] == '\r')
+ len--;
+ }
+
+ /* Keep signature separator as-is. */
+ if (skip_prefix(line->buf, "-- ", &rest) && rest - line->buf == len) {
+ if (prev->len) {
+ handle_filter(mi, prev);
+ strbuf_reset(prev);
+ }
+ handle_filter(mi, line);
+ return;
+ }
+
+ /* Unstuff space-stuffed line. */
+ if (len && line->buf[0] == ' ') {
+ strbuf_remove(line, 0, 1);
+ len--;
+ }
+
+ /* Save flowed line for later, but without the soft line break. */
+ if (len && line->buf[len - 1] == ' ') {
+ strbuf_add(prev, line->buf, len - !!mi->delsp);
+ return;
+ }
+
+ /* Prepend any previous partial lines */
+ strbuf_insert(line, 0, prev->buf, prev->len);
+ strbuf_reset(prev);
+
+ handle_filter(mi, line);
+}
+
+static void handle_body(struct mailinfo *mi, struct strbuf *line)
+{
+ struct strbuf prev = STRBUF_INIT;
+
+ /* Skip up to the first boundary */
+ if (*(mi->content_top)) {
+ if (!find_boundary(mi, line))
+ goto handle_body_out;
+ }
+
+ do {
+ /* process any boundary lines */
+ if (*(mi->content_top) && is_multipart_boundary(mi, line)) {
+ /* flush any leftover */
+ if (prev.len) {
+ handle_filter(mi, &prev);
+ strbuf_reset(&prev);
+ }
+ if (!handle_boundary(mi, line))
+ goto handle_body_out;
+ }
+
+ /* Unwrap transfer encoding */
+ decode_transfer_encoding(mi, line);
+
+ switch (mi->transfer_encoding) {
+ case TE_BASE64:
+ case TE_QP:
+ {
+ struct strbuf **lines, **it, *sb;
+
+ /* Prepend any previous partial lines */
+ strbuf_insert(line, 0, prev.buf, prev.len);
+ strbuf_reset(&prev);
+
+ /*
+ * This is a decoded line that may contain
+ * multiple new lines. Pass only one chunk
+ * at a time to handle_filter()
+ */
+ lines = strbuf_split(line, '\n');
+ for (it = lines; (sb = *it); it++) {
+ if (*(it + 1) == NULL) /* The last line */
+ if (sb->buf[sb->len - 1] != '\n') {
+ /* Partial line, save it for later. */
+ strbuf_addbuf(&prev, sb);
+ break;
+ }
+ handle_filter_flowed(mi, sb, &prev);
+ }
+ /*
+ * The partial chunk is saved in "prev" and will be
+ * appended by the next iteration of read_line_with_nul().
+ */
+ strbuf_list_free(lines);
+ break;
+ }
+ default:
+ handle_filter_flowed(mi, line, &prev);
+ }
+
+ if (mi->input_error)
+ break;
+ } while (!strbuf_getwholeline(line, mi->input, '\n'));
+
+ if (prev.len)
+ handle_filter(mi, &prev);
+
+ flush_inbody_header_accum(mi);
+
+handle_body_out:
+ strbuf_release(&prev);
+}
+
+static void output_header_lines(FILE *fout, const char *hdr, const struct strbuf *data)
+{
+ const char *sp = data->buf;
+ while (1) {
+ char *ep = strchr(sp, '\n');
+ int len;
+ if (!ep)
+ len = strlen(sp);
+ else
+ len = ep - sp;
+ fprintf(fout, "%s: %.*s\n", hdr, len, sp);
+ if (!ep)
+ break;
+ sp = ep + 1;
+ }
+}
+
+static void handle_info(struct mailinfo *mi)
+{
+ struct strbuf *hdr;
+ int i;
+
+ for (i = 0; header[i]; i++) {
+ /* only print inbody headers if we output a patch file */
+ if (mi->patch_lines && mi->s_hdr_data[i])
+ hdr = mi->s_hdr_data[i];
+ else if (mi->p_hdr_data[i])
+ hdr = mi->p_hdr_data[i];
+ else
+ continue;
+
+ if (!strcmp(header[i], "Subject")) {
+ if (!mi->keep_subject) {
+ cleanup_subject(mi, hdr);
+ cleanup_space(hdr);
+ }
+ output_header_lines(mi->output, "Subject", hdr);
+ } else if (!strcmp(header[i], "From")) {
+ cleanup_space(hdr);
+ handle_from(mi, hdr);
+ fprintf(mi->output, "Author: %s\n", mi->name.buf);
+ fprintf(mi->output, "Email: %s\n", mi->email.buf);
+ } else {
+ cleanup_space(hdr);
+ fprintf(mi->output, "%s: %s\n", header[i], hdr->buf);
+ }
+ }
+ fprintf(mi->output, "\n");
+}
+
+int mailinfo(struct mailinfo *mi, const char *msg, const char *patch)
+{
+ FILE *cmitmsg;
+ int peek;
+ struct strbuf line = STRBUF_INIT;
+
+ cmitmsg = fopen(msg, "w");
+ if (!cmitmsg) {
+ perror(msg);
+ return -1;
+ }
+ mi->patchfile = fopen(patch, "w");
+ if (!mi->patchfile) {
+ perror(patch);
+ fclose(cmitmsg);
+ return -1;
+ }
+
+ mi->p_hdr_data = xcalloc(MAX_HDR_PARSED, sizeof(*(mi->p_hdr_data)));
+ mi->s_hdr_data = xcalloc(MAX_HDR_PARSED, sizeof(*(mi->s_hdr_data)));
+
+ do {
+ peek = fgetc(mi->input);
+ if (peek == EOF) {
+ fclose(cmitmsg);
+ return error("empty patch: '%s'", patch);
+ }
+ } while (isspace(peek));
+ ungetc(peek, mi->input);
+
+ /* process the email header */
+ while (read_one_header_line(&line, mi->input))
+ check_header(mi, &line, mi->p_hdr_data, 1);
+
+ handle_body(mi, &line);
+ fwrite(mi->log_message.buf, 1, mi->log_message.len, cmitmsg);
+ fclose(cmitmsg);
+ fclose(mi->patchfile);
+
+ handle_info(mi);
+ strbuf_release(&line);
+ return mi->input_error;
+}
+
+static int git_mailinfo_config(const char *var, const char *value, void *mi_)
+{
+ struct mailinfo *mi = mi_;
+
+ if (!starts_with(var, "mailinfo."))
+ return git_default_config(var, value, NULL);
+ if (!strcmp(var, "mailinfo.scissors")) {
+ mi->use_scissors = git_config_bool(var, value);
+ return 0;
+ }
+ /* perhaps others here */
+ return 0;
+}
+
+void setup_mailinfo(struct mailinfo *mi)
+{
+ memset(mi, 0, sizeof(*mi));
+ strbuf_init(&mi->name, 0);
+ strbuf_init(&mi->email, 0);
+ strbuf_init(&mi->charset, 0);
+ strbuf_init(&mi->log_message, 0);
+ strbuf_init(&mi->inbody_header_accum, 0);
+ mi->header_stage = 1;
+ mi->use_inbody_headers = 1;
+ mi->content_top = mi->content;
+ git_config(git_mailinfo_config, mi);
+}
+
+void clear_mailinfo(struct mailinfo *mi)
+{
+ int i;
+
+ strbuf_release(&mi->name);
+ strbuf_release(&mi->email);
+ strbuf_release(&mi->charset);
+ strbuf_release(&mi->inbody_header_accum);
+ free(mi->message_id);
+
+ if (mi->p_hdr_data)
+ for (i = 0; mi->p_hdr_data[i]; i++)
+ strbuf_release(mi->p_hdr_data[i]);
+ free(mi->p_hdr_data);
+ if (mi->s_hdr_data)
+ for (i = 0; mi->s_hdr_data[i]; i++)
+ strbuf_release(mi->s_hdr_data[i]);
+ free(mi->s_hdr_data);
+
+ while (mi->content < mi->content_top) {
+ free(*(mi->content_top));
+ mi->content_top--;
+ }
+
+ strbuf_release(&mi->log_message);
+}
diff --git a/t/t4256/1/mailinfo.c.orig b/t/t4256/1/mailinfo.c.orig
new file mode 100644
index 0000000..3281a37
--- /dev/null
+++ b/t/t4256/1/mailinfo.c.orig
@@ -0,0 +1,1185 @@
+#include "cache.h"
+#include "config.h"
+#include "utf8.h"
+#include "strbuf.h"
+#include "mailinfo.h"
+
+static void cleanup_space(struct strbuf *sb)
+{
+ size_t pos, cnt;
+ for (pos = 0; pos < sb->len; pos++) {
+ if (isspace(sb->buf[pos])) {
+ sb->buf[pos] = ' ';
+ for (cnt = 0; isspace(sb->buf[pos + cnt + 1]); cnt++);
+ strbuf_remove(sb, pos + 1, cnt);
+ }
+ }
+}
+
+static void get_sane_name(struct strbuf *out, struct strbuf *name, struct strbuf *email)
+{
+ struct strbuf *src = name;
+ if (name->len < 3 || 60 < name->len || strchr(name->buf, '@') ||
+ strchr(name->buf, '<') || strchr(name->buf, '>'))
+ src = email;
+ else if (name == out)
+ return;
+ strbuf_reset(out);
+ strbuf_addbuf(out, src);
+}
+
+static void parse_bogus_from(struct mailinfo *mi, const struct strbuf *line)
+{
+ /* John Doe <johndoe> */
+
+ char *bra, *ket;
+ /* This is fallback, so do not bother if we already have an
+ * e-mail address.
+ */
+ if (mi->email.len)
+ return;
+
+ bra = strchr(line->buf, '<');
+ if (!bra)
+ return;
+ ket = strchr(bra, '>');
+ if (!ket)
+ return;
+
+ strbuf_reset(&mi->email);
+ strbuf_add(&mi->email, bra + 1, ket - bra - 1);
+
+ strbuf_reset(&mi->name);
+ strbuf_add(&mi->name, line->buf, bra - line->buf);
+ strbuf_trim(&mi->name);
+ get_sane_name(&mi->name, &mi->name, &mi->email);
+}
+
+static const char *unquote_comment(struct strbuf *outbuf, const char *in)
+{
+ int c;
+ int take_next_literally = 0;
+
+ strbuf_addch(outbuf, '(');
+
+ while ((c = *in++) != 0) {
+ if (take_next_literally == 1) {
+ take_next_literally = 0;
+ } else {
+ switch (c) {
+ case '\\':
+ take_next_literally = 1;
+ continue;
+ case '(':
+ in = unquote_comment(outbuf, in);
+ continue;
+ case ')':
+ strbuf_addch(outbuf, ')');
+ return in;
+ }
+ }
+
+ strbuf_addch(outbuf, c);
+ }
+
+ return in;
+}
+
+static const char *unquote_quoted_string(struct strbuf *outbuf, const char *in)
+{
+ int c;
+ int take_next_literally = 0;
+
+ while ((c = *in++) != 0) {
+ if (take_next_literally == 1) {
+ take_next_literally = 0;
+ } else {
+ switch (c) {
+ case '\\':
+ take_next_literally = 1;
+ continue;
+ case '"':
+ return in;
+ }
+ }
+
+ strbuf_addch(outbuf, c);
+ }
+
+ return in;
+}
+
+static void unquote_quoted_pair(struct strbuf *line)
+{
+ struct strbuf outbuf;
+ const char *in = line->buf;
+ int c;
+
+ strbuf_init(&outbuf, line->len);
+
+ while ((c = *in++) != 0) {
+ switch (c) {
+ case '"':
+ in = unquote_quoted_string(&outbuf, in);
+ continue;
+ case '(':
+ in = unquote_comment(&outbuf, in);
+ continue;
+ }
+
+ strbuf_addch(&outbuf, c);
+ }
+
+ strbuf_swap(&outbuf, line);
+ strbuf_release(&outbuf);
+
+}
+
+static void handle_from(struct mailinfo *mi, const struct strbuf *from)
+{
+ char *at;
+ size_t el;
+ struct strbuf f;
+
+ strbuf_init(&f, from->len);
+ strbuf_addbuf(&f, from);
+
+ unquote_quoted_pair(&f);
+
+ at = strchr(f.buf, '@');
+ if (!at) {
+ parse_bogus_from(mi, from);
+ goto out;
+ }
+
+ /*
+ * If we already have one email, don't take any confusing lines
+ */
+ if (mi->email.len && strchr(at + 1, '@'))
+ goto out;
+
+ /* Pick up the string around '@', possibly delimited with <>
+ * pair; that is the email part.
+ */
+ while (at > f.buf) {
+ char c = at[-1];
+ if (isspace(c))
+ break;
+ if (c == '<') {
+ at[-1] = ' ';
+ break;
+ }
+ at--;
+ }
+ el = strcspn(at, " \n\t\r\v\f>");
+ strbuf_reset(&mi->email);
+ strbuf_add(&mi->email, at, el);
+ strbuf_remove(&f, at - f.buf, el + (at[el] ? 1 : 0));
+
+ /* The remainder is name. It could be
+ *
+ * - "John Doe <john.doe@xz>" (a), or
+ * - "john.doe@xz (John Doe)" (b), or
+ * - "John (zzz) Doe <john.doe@xz> (Comment)" (c)
+ *
+ * but we have removed the email part, so
+ *
+ * - remove extra spaces which could stay after email (case 'c'), and
+ * - trim from both ends, possibly removing the () pair at the end
+ * (cases 'a' and 'b').
+ */
+ cleanup_space(&f);
+ strbuf_trim(&f);
+ if (f.buf[0] == '(' && f.len && f.buf[f.len - 1] == ')') {
+ strbuf_remove(&f, 0, 1);
+ strbuf_setlen(&f, f.len - 1);
+ }
+
+ get_sane_name(&mi->name, &f, &mi->email);
+out:
+ strbuf_release(&f);
+}
+
+static void handle_header(struct strbuf **out, const struct strbuf *line)
+{
+ if (!*out) {
+ *out = xmalloc(sizeof(struct strbuf));
+ strbuf_init(*out, line->len);
+ } else
+ strbuf_reset(*out);
+
+ strbuf_addbuf(*out, line);
+}
+
+/* NOTE NOTE NOTE. We do not claim we do full MIME. We just attempt
+ * to have enough heuristics to grok MIME encoded patches often found
+ * on our mailing lists. For example, we do not even treat header lines
+ * case insensitively.
+ */
+
+static int slurp_attr(const char *line, const char *name, struct strbuf *attr)
+{
+ const char *ends, *ap = strcasestr(line, name);
+ size_t sz;
+
+ strbuf_setlen(attr, 0);
+ if (!ap)
+ return 0;
+ ap += strlen(name);
+ if (*ap == '"') {
+ ap++;
+ ends = "\"";
+ }
+ else
+ ends = "; \t";
+ sz = strcspn(ap, ends);
+ strbuf_add(attr, ap, sz);
+ return 1;
+}
+
+static void handle_content_type(struct mailinfo *mi, struct strbuf *line)
+{
+ struct strbuf *boundary = xmalloc(sizeof(struct strbuf));
+ strbuf_init(boundary, line->len);
+
+ if (slurp_attr(line->buf, "boundary=", boundary)) {
+ strbuf_insert(boundary, 0, "--", 2);
+ if (++mi->content_top >= &mi->content[MAX_BOUNDARIES]) {
+ error("Too many boundaries to handle");
+ mi->input_error = -1;
+ mi->content_top = &mi->content[MAX_BOUNDARIES] - 1;
+ return;
+ }
+ *(mi->content_top) = boundary;
+ boundary = NULL;
+ }
+ slurp_attr(line->buf, "charset=", &mi->charset);
+
+ if (boundary) {
+ strbuf_release(boundary);
+ free(boundary);
+ }
+}
+
+static void handle_content_transfer_encoding(struct mailinfo *mi,
+ const struct strbuf *line)
+{
+ if (strcasestr(line->buf, "base64"))
+ mi->transfer_encoding = TE_BASE64;
+ else if (strcasestr(line->buf, "quoted-printable"))
+ mi->transfer_encoding = TE_QP;
+ else
+ mi->transfer_encoding = TE_DONTCARE;
+}
+
+static int is_multipart_boundary(struct mailinfo *mi, const struct strbuf *line)
+{
+ struct strbuf *content_top = *(mi->content_top);
+
+ return ((content_top->len <= line->len) &&
+ !memcmp(line->buf, content_top->buf, content_top->len));
+}
+
+static void cleanup_subject(struct mailinfo *mi, struct strbuf *subject)
+{
+ size_t at = 0;
+
+ while (at < subject->len) {
+ char *pos;
+ size_t remove;
+
+ switch (subject->buf[at]) {
+ case 'r': case 'R':
+ if (subject->len <= at + 3)
+ break;
+ if ((subject->buf[at + 1] == 'e' ||
+ subject->buf[at + 1] == 'E') &&
+ subject->buf[at + 2] == ':') {
+ strbuf_remove(subject, at, 3);
+ continue;
+ }
+ at++;
+ break;
+ case ' ': case '\t': case ':':
+ strbuf_remove(subject, at, 1);
+ continue;
+ case '[':
+ pos = strchr(subject->buf + at, ']');
+ if (!pos)
+ break;
+ remove = pos - subject->buf + at + 1;
+ if (!mi->keep_non_patch_brackets_in_subject ||
+ (7 <= remove &&
+ memmem(subject->buf + at, remove, "PATCH", 5)))
+ strbuf_remove(subject, at, remove);
+ else {
+ at += remove;
+ /*
+ * If the input had a space after the ], keep
+ * it. We don't bother with finding the end of
+ * the space, since we later normalize it
+ * anyway.
+ */
+ if (isspace(subject->buf[at]))
+ at += 1;
+ }
+ continue;
+ }
+ break;
+ }
+ strbuf_trim(subject);
+}
+
+#define MAX_HDR_PARSED 10
+static const char *header[MAX_HDR_PARSED] = {
+ "From","Subject","Date",
+};
+
+static inline int cmp_header(const struct strbuf *line, const char *hdr)
+{
+ int len = strlen(hdr);
+ return !strncasecmp(line->buf, hdr, len) && line->len > len &&
+ line->buf[len] == ':' && isspace(line->buf[len + 1]);
+}
+
+static int is_format_patch_separator(const char *line, int len)
+{
+ static const char SAMPLE[] =
+ "From e6807f3efca28b30decfecb1732a56c7db1137ee Mon Sep 17 00:00:00 2001\n";
+ const char *cp;
+
+ if (len != strlen(SAMPLE))
+ return 0;
+ if (!skip_prefix(line, "From ", &cp))
+ return 0;
+ if (strspn(cp, "0123456789abcdef") != 40)
+ return 0;
+ cp += 40;
+ return !memcmp(SAMPLE + (cp - line), cp, strlen(SAMPLE) - (cp - line));
+}
+
+static struct strbuf *decode_q_segment(const struct strbuf *q_seg, int rfc2047)
+{
+ const char *in = q_seg->buf;
+ int c;
+ struct strbuf *out = xmalloc(sizeof(struct strbuf));
+ strbuf_init(out, q_seg->len);
+
+ while ((c = *in++) != 0) {
+ if (c == '=') {
+ int ch, d = *in;
+ if (d == '\n' || !d)
+ break; /* drop trailing newline */
+ ch = hex2chr(in);
+ if (ch >= 0) {
+ strbuf_addch(out, ch);
+ in += 2;
+ continue;
+ }
+ /* garbage -- fall through */
+ }
+ if (rfc2047 && c == '_') /* rfc2047 4.2 (2) */
+ c = 0x20;
+ strbuf_addch(out, c);
+ }
+ return out;
+}
+
+static struct strbuf *decode_b_segment(const struct strbuf *b_seg)
+{
+ /* Decode in..ep, possibly in-place to ot */
+ int c, pos = 0, acc = 0;
+ const char *in = b_seg->buf;
+ struct strbuf *out = xmalloc(sizeof(struct strbuf));
+ strbuf_init(out, b_seg->len);
+
+ while ((c = *in++) != 0) {
+ if (c == '+')
+ c = 62;
+ else if (c == '/')
+ c = 63;
+ else if ('A' <= c && c <= 'Z')
+ c -= 'A';
+ else if ('a' <= c && c <= 'z')
+ c -= 'a' - 26;
+ else if ('0' <= c && c <= '9')
+ c -= '0' - 52;
+ else
+ continue; /* garbage */
+ switch (pos++) {
+ case 0:
+ acc = (c << 2);
+ break;
+ case 1:
+ strbuf_addch(out, (acc | (c >> 4)));
+ acc = (c & 15) << 4;
+ break;
+ case 2:
+ strbuf_addch(out, (acc | (c >> 2)));
+ acc = (c & 3) << 6;
+ break;
+ case 3:
+ strbuf_addch(out, (acc | c));
+ acc = pos = 0;
+ break;
+ }
+ }
+ return out;
+}
+
+static int convert_to_utf8(struct mailinfo *mi,
+ struct strbuf *line, const char *charset)
+{
+ char *out;
+
+ if (!mi->metainfo_charset || !charset || !*charset)
+ return 0;
+
+ if (same_encoding(mi->metainfo_charset, charset))
+ return 0;
+ out = reencode_string(line->buf, mi->metainfo_charset, charset);
+ if (!out) {
+ mi->input_error = -1;
+ return error("cannot convert from %s to %s",
+ charset, mi->metainfo_charset);
+ }
+ strbuf_attach(line, out, strlen(out), strlen(out));
+ return 0;
+}
+
+static void decode_header(struct mailinfo *mi, struct strbuf *it)
+{
+ char *in, *ep, *cp;
+ struct strbuf outbuf = STRBUF_INIT, *dec;
+ struct strbuf charset_q = STRBUF_INIT, piecebuf = STRBUF_INIT;
+ int found_error = 1; /* pessimism */
+
+ in = it->buf;
+ while (in - it->buf <= it->len && (ep = strstr(in, "=?")) != NULL) {
+ int encoding;
+ strbuf_reset(&charset_q);
+ strbuf_reset(&piecebuf);
+
+ if (in != ep) {
+ /*
+ * We are about to process an encoded-word
+ * that begins at ep, but there is something
+ * before the encoded word.
+ */
+ char *scan;
+ for (scan = in; scan < ep; scan++)
+ if (!isspace(*scan))
+ break;
+
+ if (scan != ep || in == it->buf) {
+ /*
+ * We should not lose that "something",
+ * unless we have just processed an
+ * encoded-word, and there is only LWS
+ * before the one we are about to process.
+ */
+ strbuf_add(&outbuf, in, ep - in);
+ }
+ }
+ /* E.g.
+ * ep : "=?iso-2022-jp?B?GyR...?= foo"
+ * ep : "=?ISO-8859-1?Q?Foo=FCbar?= baz"
+ */
+ ep += 2;
+
+ if (ep - it->buf >= it->len || !(cp = strchr(ep, '?')))
+ goto release_return;
+
+ if (cp + 3 - it->buf > it->len)
+ goto release_return;
+ strbuf_add(&charset_q, ep, cp - ep);
+
+ encoding = cp[1];
+ if (!encoding || cp[2] != '?')
+ goto release_return;
+ ep = strstr(cp + 3, "?=");
+ if (!ep)
+ goto release_return;
+ strbuf_add(&piecebuf, cp + 3, ep - cp - 3);
+ switch (tolower(encoding)) {
+ default:
+ goto release_return;
+ case 'b':
+ dec = decode_b_segment(&piecebuf);
+ break;
+ case 'q':
+ dec = decode_q_segment(&piecebuf, 1);
+ break;
+ }
+ if (convert_to_utf8(mi, dec, charset_q.buf))
+ goto release_return;
+
+ strbuf_addbuf(&outbuf, dec);
+ strbuf_release(dec);
+ free(dec);
+ in = ep + 2;
+ }
+ strbuf_addstr(&outbuf, in);
+ strbuf_reset(it);
+ strbuf_addbuf(it, &outbuf);
+ found_error = 0;
+release_return:
+ strbuf_release(&outbuf);
+ strbuf_release(&charset_q);
+ strbuf_release(&piecebuf);
+
+ if (found_error)
+ mi->input_error = -1;
+}
+
+static int check_header(struct mailinfo *mi,
+ const struct strbuf *line,
+ struct strbuf *hdr_data[], int overwrite)
+{
+ int i, ret = 0, len;
+ struct strbuf sb = STRBUF_INIT;
+
+ /* search for the interesting parts */
+ for (i = 0; header[i]; i++) {
+ int len = strlen(header[i]);
+ if ((!hdr_data[i] || overwrite) && cmp_header(line, header[i])) {
+ /* Unwrap inline B and Q encoding, and optionally
+ * normalize the meta information to utf8.
+ */
+ strbuf_add(&sb, line->buf + len + 2, line->len - len - 2);
+ decode_header(mi, &sb);
+ handle_header(&hdr_data[i], &sb);
+ ret = 1;
+ goto check_header_out;
+ }
+ }
+
+ /* Content stuff */
+ if (cmp_header(line, "Content-Type")) {
+ len = strlen("Content-Type: ");
+ strbuf_add(&sb, line->buf + len, line->len - len);
+ decode_header(mi, &sb);
+ strbuf_insert(&sb, 0, "Content-Type: ", len);
+ handle_content_type(mi, &sb);
+ ret = 1;
+ goto check_header_out;
+ }
+ if (cmp_header(line, "Content-Transfer-Encoding")) {
+ len = strlen("Content-Transfer-Encoding: ");
+ strbuf_add(&sb, line->buf + len, line->len - len);
+ decode_header(mi, &sb);
+ handle_content_transfer_encoding(mi, &sb);
+ ret = 1;
+ goto check_header_out;
+ }
+ if (cmp_header(line, "Message-Id")) {
+ len = strlen("Message-Id: ");
+ strbuf_add(&sb, line->buf + len, line->len - len);
+ decode_header(mi, &sb);
+ if (mi->add_message_id)
+ mi->message_id = strbuf_detach(&sb, NULL);
+ ret = 1;
+ goto check_header_out;
+ }
+
+check_header_out:
+ strbuf_release(&sb);
+ return ret;
+}
+
+/*
+ * Returns 1 if the given line or any line beginning with the given line is an
+ * in-body header (that is, check_header will succeed when passed
+ * mi->s_hdr_data).
+ */
+static int is_inbody_header(const struct mailinfo *mi,
+ const struct strbuf *line)
+{
+ int i;
+ for (i = 0; header[i]; i++)
+ if (!mi->s_hdr_data[i] && cmp_header(line, header[i]))
+ return 1;
+ return 0;
+}
+
+static void decode_transfer_encoding(struct mailinfo *mi, struct strbuf *line)
+{
+ struct strbuf *ret;
+
+ switch (mi->transfer_encoding) {
+ case TE_QP:
+ ret = decode_q_segment(line, 0);
+ break;
+ case TE_BASE64:
+ ret = decode_b_segment(line);
+ break;
+ case TE_DONTCARE:
+ default:
+ return;
+ }
+ strbuf_reset(line);
+ strbuf_addbuf(line, ret);
+ strbuf_release(ret);
+ free(ret);
+}
+
+static inline int patchbreak(const struct strbuf *line)
+{
+ size_t i;
+
+ /* Beginning of a "diff -" header? */
+ if (starts_with(line->buf, "diff -"))
+ return 1;
+
+ /* CVS "Index: " line? */
+ if (starts_with(line->buf, "Index: "))
+ return 1;
+
+ /*
+ * "--- <filename>" starts patches without headers
+ * "---<sp>*" is a manual separator
+ */
+ if (line->len < 4)
+ return 0;
+
+ if (starts_with(line->buf, "---")) {
+ /* space followed by a filename? */
+ if (line->buf[3] == ' ' && !isspace(line->buf[4]))
+ return 1;
+ /* Just whitespace? */
+ for (i = 3; i < line->len; i++) {
+ unsigned char c = line->buf[i];
+ if (c == '\n')
+ return 1;
+ if (!isspace(c))
+ break;
+ }
+ return 0;
+ }
+ return 0;
+}
+
+static int is_scissors_line(const char *line)
+{
+ const char *c;
+ int scissors = 0, gap = 0;
+ const char *first_nonblank = NULL, *last_nonblank = NULL;
+ int visible, perforation = 0, in_perforation = 0;
+
+ for (c = line; *c; c++) {
+ if (isspace(*c)) {
+ if (in_perforation) {
+ perforation++;
+ gap++;
+ }
+ continue;
+ }
+ last_nonblank = c;
+ if (first_nonblank == NULL)
+ first_nonblank = c;
+ if (*c == '-') {
+ in_perforation = 1;
+ perforation++;
+ continue;
+ }
+ if ((!memcmp(c, ">8", 2) || !memcmp(c, "8<", 2) ||
+ !memcmp(c, ">%", 2) || !memcmp(c, "%<", 2))) {
+ in_perforation = 1;
+ perforation += 2;
+ scissors += 2;
+ c++;
+ continue;
+ }
+ in_perforation = 0;
+ }
+
+ /*
+ * The mark must be at least 8 bytes long (e.g. "-- >8 --").
+ * Even though there can be arbitrary cruft on the same line
+ * (e.g. "cut here"), in order to avoid misidentification, the
+ * perforation must occupy more than a third of the visible
+ * width of the line, and dashes and scissors must occupy more
+ * than half of the perforation.
+ */
+
+ if (first_nonblank && last_nonblank)
+ visible = last_nonblank - first_nonblank + 1;
+ else
+ visible = 0;
+ return (scissors && 8 <= visible &&
+ visible < perforation * 3 &&
+ gap * 2 < perforation);
+}
+
+static void flush_inbody_header_accum(struct mailinfo *mi)
+{
+ if (!mi->inbody_header_accum.len)
+ return;
+ if (!check_header(mi, &mi->inbody_header_accum, mi->s_hdr_data, 0))
+ BUG("inbody_header_accum, if not empty, must always contain a valid in-body header");
+ strbuf_reset(&mi->inbody_header_accum);
+}
+
+static int check_inbody_header(struct mailinfo *mi, const struct strbuf *line)
+{
+ if (mi->inbody_header_accum.len &&
+ (line->buf[0] == ' ' || line->buf[0] == '\t')) {
+ if (mi->use_scissors && is_scissors_line(line->buf)) {
+ /*
+ * This is a scissors line; do not consider this line
+ * as a header continuation line.
+ */
+ flush_inbody_header_accum(mi);
+ return 0;
+ }
+ strbuf_strip_suffix(&mi->inbody_header_accum, "\n");
+ strbuf_addbuf(&mi->inbody_header_accum, line);
+ return 1;
+ }
+
+ flush_inbody_header_accum(mi);
+
+ if (starts_with(line->buf, ">From") && isspace(line->buf[5]))
+ return is_format_patch_separator(line->buf + 1, line->len - 1);
+ if (starts_with(line->buf, "[PATCH]") && isspace(line->buf[7])) {
+ int i;
+ for (i = 0; header[i]; i++)
+ if (!strcmp("Subject", header[i])) {
+ handle_header(&mi->s_hdr_data[i], line);
+ return 1;
+ }
+ return 0;
+ }
+ if (is_inbody_header(mi, line)) {
+ strbuf_addbuf(&mi->inbody_header_accum, line);
+ return 1;
+ }
+ return 0;
+}
+
+static int handle_commit_msg(struct mailinfo *mi, struct strbuf *line)
+{
+ assert(!mi->filter_stage);
+
+ if (mi->header_stage) {
+ if (!line->len || (line->len == 1 && line->buf[0] == '\n')) {
+ if (mi->inbody_header_accum.len) {
+ flush_inbody_header_accum(mi);
+ mi->header_stage = 0;
+ }
+ return 0;
+ }
+ }
+
+ if (mi->use_inbody_headers && mi->header_stage) {
+ mi->header_stage = check_inbody_header(mi, line);
+ if (mi->header_stage)
+ return 0;
+ } else
+ /* Only trim the first (blank) line of the commit message
+ * when ignoring in-body headers.
+ */
+ mi->header_stage = 0;
+
+ /* normalize the log message to UTF-8. */
+ if (convert_to_utf8(mi, line, mi->charset.buf))
+ return 0; /* mi->input_error already set */
+
+ if (mi->use_scissors && is_scissors_line(line->buf)) {
+ int i;
+
+ strbuf_setlen(&mi->log_message, 0);
+ mi->header_stage = 1;
+
+ /*
+ * We may have already read "secondary headers"; purge
+ * them to give ourselves a clean restart.
+ */
+ for (i = 0; header[i]; i++) {
+ if (mi->s_hdr_data[i])
+ strbuf_release(mi->s_hdr_data[i]);
+ mi->s_hdr_data[i] = NULL;
+ }
+ return 0;
+ }
+
+ if (patchbreak(line)) {
+ if (mi->message_id)
+ strbuf_addf(&mi->log_message,
+ "Message-Id: %s\n", mi->message_id);
+ return 1;
+ }
+
+ strbuf_addbuf(&mi->log_message, line);
+ return 0;
+}
+
+static void handle_patch(struct mailinfo *mi, const struct strbuf *line)
+{
+ fwrite(line->buf, 1, line->len, mi->patchfile);
+ mi->patch_lines++;
+}
+
+static void handle_filter(struct mailinfo *mi, struct strbuf *line)
+{
+ switch (mi->filter_stage) {
+ case 0:
+ if (!handle_commit_msg(mi, line))
+ break;
+ mi->filter_stage++;
+ /* fallthrough */
+ case 1:
+ handle_patch(mi, line);
+ break;
+ }
+}
+
+static int is_rfc2822_header(const struct strbuf *line)
+{
+ /*
+ * The section that defines the loosest possible
+ * field name is "3.6.8 Optional fields".
+ *
+ * optional-field = field-name ":" unstructured CRLF
+ * field-name = 1*ftext
+ * ftext = %d33-57 / %59-126
+ */
+ int ch;
+ char *cp = line->buf;
+
+ /* Count mbox From headers as headers */
+ if (starts_with(cp, "From ") || starts_with(cp, ">From "))
+ return 1;
+
+ while ((ch = *cp++)) {
+ if (ch == ':')
+ return 1;
+ if ((33 <= ch && ch <= 57) ||
+ (59 <= ch && ch <= 126))
+ continue;
+ break;
+ }
+ return 0;
+}
+
+static int read_one_header_line(struct strbuf *line, FILE *in)
+{
+ struct strbuf continuation = STRBUF_INIT;
+
+ /* Get the first part of the line. */
+ if (strbuf_getline_lf(line, in))
+ return 0;
+
+ /*
+ * Is it an empty line or not a valid rfc2822 header?
+ * If so, stop here, and return false ("not a header")
+ */
+ strbuf_rtrim(line);
+ if (!line->len || !is_rfc2822_header(line)) {
+ /* Re-add the newline */
+ strbuf_addch(line, '\n');
+ return 0;
+ }
+
+ /*
+ * Now we need to eat all the continuation lines..
+ * Yuck, 2822 header "folding"
+ */
+ for (;;) {
+ int peek;
+
+ peek = fgetc(in);
+ if (peek == EOF)
+ break;
+ ungetc(peek, in);
+ if (peek != ' ' && peek != '\t')
+ break;
+ if (strbuf_getline_lf(&continuation, in))
+ break;
+ continuation.buf[0] = ' ';
+ strbuf_rtrim(&continuation);
+ strbuf_addbuf(line, &continuation);
+ }
+ strbuf_release(&continuation);
+
+ return 1;
+}
+
+static int find_boundary(struct mailinfo *mi, struct strbuf *line)
+{
+ while (!strbuf_getline_lf(line, mi->input)) {
+ if (*(mi->content_top) && is_multipart_boundary(mi, line))
+ return 1;
+ }
+ return 0;
+}
+
+static int handle_boundary(struct mailinfo *mi, struct strbuf *line)
+{
+ struct strbuf newline = STRBUF_INIT;
+
+ strbuf_addch(&newline, '\n');
+again:
+ if (line->len >= (*(mi->content_top))->len + 2 &&
+ !memcmp(line->buf + (*(mi->content_top))->len, "--", 2)) {
+ /* we hit an end boundary */
+ /* pop the current boundary off the stack */
+ strbuf_release(*(mi->content_top));
+ FREE_AND_NULL(*(mi->content_top));
+
+ /* technically won't happen as is_multipart_boundary()
+ will fail first. But just in case..
+ */
+ if (--mi->content_top < mi->content) {
+ error("Detected mismatched boundaries, can't recover");
+ mi->input_error = -1;
+ mi->content_top = mi->content;
+ strbuf_release(&newline);
+ return 0;
+ }
+ handle_filter(mi, &newline);
+ strbuf_release(&newline);
+ if (mi->input_error)
+ return 0;
+
+ /* skip to the next boundary */
+ if (!find_boundary(mi, line))
+ return 0;
+ goto again;
+ }
+
+ /* set some defaults */
+ mi->transfer_encoding = TE_DONTCARE;
+ strbuf_reset(&mi->charset);
+
+ /* slurp in this section's info */
+ while (read_one_header_line(line, mi->input))
+ check_header(mi, line, mi->p_hdr_data, 0);
+
+ strbuf_release(&newline);
+ /* replenish line */
+ if (strbuf_getline_lf(line, mi->input))
+ return 0;
+ strbuf_addch(line, '\n');
+ return 1;
+}
+
+static void handle_body(struct mailinfo *mi, struct strbuf *line)
+{
+ struct strbuf prev = STRBUF_INIT;
+
+ /* Skip up to the first boundary */
+ if (*(mi->content_top)) {
+ if (!find_boundary(mi, line))
+ goto handle_body_out;
+ }
+
+ do {
+ /* process any boundary lines */
+ if (*(mi->content_top) && is_multipart_boundary(mi, line)) {
+ /* flush any leftover */
+ if (prev.len) {
+ handle_filter(mi, &prev);
+ strbuf_reset(&prev);
+ }
+ if (!handle_boundary(mi, line))
+ goto handle_body_out;
+ }
+
+ /* Unwrap transfer encoding */
+ decode_transfer_encoding(mi, line);
+
+ switch (mi->transfer_encoding) {
+ case TE_BASE64:
+ case TE_QP:
+ {
+ struct strbuf **lines, **it, *sb;
+
+ /* Prepend any previous partial lines */
+ strbuf_insert(line, 0, prev.buf, prev.len);
+ strbuf_reset(&prev);
+
+ /*
+ * This is a decoded line that may contain
+ * multiple new lines. Pass only one chunk
+ * at a time to handle_filter()
+ */
+ lines = strbuf_split(line, '\n');
+ for (it = lines; (sb = *it); it++) {
+ if (*(it + 1) == NULL) /* The last line */
+ if (sb->buf[sb->len - 1] != '\n') {
+ /* Partial line, save it for later. */
+ strbuf_addbuf(&prev, sb);
+ break;
+ }
+ handle_filter(mi, sb);
+ }
+ /*
+ * The partial chunk is saved in "prev" and will be
+ * appended by the next iteration of read_line_with_nul().
+ */
+ strbuf_list_free(lines);
+ break;
+ }
+ default:
+ handle_filter(mi, line);
+ }
+
+ if (mi->input_error)
+ break;
+ } while (!strbuf_getwholeline(line, mi->input, '\n'));
+
+ flush_inbody_header_accum(mi);
+
+handle_body_out:
+ strbuf_release(&prev);
+}
+
+static void output_header_lines(FILE *fout, const char *hdr, const struct strbuf *data)
+{
+ const char *sp = data->buf;
+ while (1) {
+ char *ep = strchr(sp, '\n');
+ int len;
+ if (!ep)
+ len = strlen(sp);
+ else
+ len = ep - sp;
+ fprintf(fout, "%s: %.*s\n", hdr, len, sp);
+ if (!ep)
+ break;
+ sp = ep + 1;
+ }
+}
+
+static void handle_info(struct mailinfo *mi)
+{
+ struct strbuf *hdr;
+ int i;
+
+ for (i = 0; header[i]; i++) {
+ /* only print inbody headers if we output a patch file */
+ if (mi->patch_lines && mi->s_hdr_data[i])
+ hdr = mi->s_hdr_data[i];
+ else if (mi->p_hdr_data[i])
+ hdr = mi->p_hdr_data[i];
+ else
+ continue;
+
+ if (!strcmp(header[i], "Subject")) {
+ if (!mi->keep_subject) {
+ cleanup_subject(mi, hdr);
+ cleanup_space(hdr);
+ }
+ output_header_lines(mi->output, "Subject", hdr);
+ } else if (!strcmp(header[i], "From")) {
+ cleanup_space(hdr);
+ handle_from(mi, hdr);
+ fprintf(mi->output, "Author: %s\n", mi->name.buf);
+ fprintf(mi->output, "Email: %s\n", mi->email.buf);
+ } else {
+ cleanup_space(hdr);
+ fprintf(mi->output, "%s: %s\n", header[i], hdr->buf);
+ }
+ }
+ fprintf(mi->output, "\n");
+}
+
+int mailinfo(struct mailinfo *mi, const char *msg, const char *patch)
+{
+ FILE *cmitmsg;
+ int peek;
+ struct strbuf line = STRBUF_INIT;
+
+ cmitmsg = fopen(msg, "w");
+ if (!cmitmsg) {
+ perror(msg);
+ return -1;
+ }
+ mi->patchfile = fopen(patch, "w");
+ if (!mi->patchfile) {
+ perror(patch);
+ fclose(cmitmsg);
+ return -1;
+ }
+
+ mi->p_hdr_data = xcalloc(MAX_HDR_PARSED, sizeof(*(mi->p_hdr_data)));
+ mi->s_hdr_data = xcalloc(MAX_HDR_PARSED, sizeof(*(mi->s_hdr_data)));
+
+ do {
+ peek = fgetc(mi->input);
+ if (peek == EOF) {
+ fclose(cmitmsg);
+ return error("empty patch: '%s'", patch);
+ }
+ } while (isspace(peek));
+ ungetc(peek, mi->input);
+
+ /* process the email header */
+ while (read_one_header_line(&line, mi->input))
+ check_header(mi, &line, mi->p_hdr_data, 1);
+
+ handle_body(mi, &line);
+ fwrite(mi->log_message.buf, 1, mi->log_message.len, cmitmsg);
+ fclose(cmitmsg);
+ fclose(mi->patchfile);
+
+ handle_info(mi);
+ strbuf_release(&line);
+ return mi->input_error;
+}
+
+static int git_mailinfo_config(const char *var, const char *value, void *mi_)
+{
+ struct mailinfo *mi = mi_;
+
+ if (!starts_with(var, "mailinfo."))
+ return git_default_config(var, value, NULL);
+ if (!strcmp(var, "mailinfo.scissors")) {
+ mi->use_scissors = git_config_bool(var, value);
+ return 0;
+ }
+ /* perhaps others here */
+ return 0;
+}
+
+void setup_mailinfo(struct mailinfo *mi)
+{
+ memset(mi, 0, sizeof(*mi));
+ strbuf_init(&mi->name, 0);
+ strbuf_init(&mi->email, 0);
+ strbuf_init(&mi->charset, 0);
+ strbuf_init(&mi->log_message, 0);
+ strbuf_init(&mi->inbody_header_accum, 0);
+ mi->header_stage = 1;
+ mi->use_inbody_headers = 1;
+ mi->content_top = mi->content;
+ git_config(git_mailinfo_config, mi);
+}
+
+void clear_mailinfo(struct mailinfo *mi)
+{
+ int i;
+
+ strbuf_release(&mi->name);
+ strbuf_release(&mi->email);
+ strbuf_release(&mi->charset);
+ strbuf_release(&mi->inbody_header_accum);
+ free(mi->message_id);
+
+ if (mi->p_hdr_data)
+ for (i = 0; mi->p_hdr_data[i]; i++)
+ strbuf_release(mi->p_hdr_data[i]);
+ free(mi->p_hdr_data);
+ if (mi->s_hdr_data)
+ for (i = 0; mi->s_hdr_data[i]; i++)
+ strbuf_release(mi->s_hdr_data[i]);
+ free(mi->s_hdr_data);
+
+ while (mi->content < mi->content_top) {
+ free(*(mi->content_top));
+ mi->content_top--;
+ }
+
+ strbuf_release(&mi->log_message);
+}
diff --git a/t/t4256/1/patch b/t/t4256/1/patch
new file mode 100644
index 0000000..bd0d8b0
--- /dev/null
+++ b/t/t4256/1/patch
@@ -0,0 +1,129 @@
+From: A <author@example.com>
+Subject: [PATCH] mailinfo: support format=flowed
+Message-ID: <aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa@example.com>
+Date: Sat, 25 Aug 2018 22:04:50 +0200
+User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101
+ Thunderbird/60.0
+MIME-Version: 1.0
+Content-Type: text/plain; charset=utf-8; format=flowed
+Content-Language: en-US
+Content-Transfer-Encoding: 7bit
+
+---
+ mailinfo.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
+ 1 file changed, 62 insertions(+), 2 deletions(-)
+
+diff --git a/mailinfo.c b/mailinfo.c
+index 3281a37d51..b395adbdf2 100644
+--- a/mailinfo.c
++++ b/mailinfo.c
+@@ -237,11 +237,22 @@ static int slurp_attr(const char *line, const char
+*name, struct strbuf *attr)
+ return 1;
+ }
+
++static int has_attr_value(const char *line, const char *name, const
+char *value)
++{
++ struct strbuf sb = STRBUF_INIT;
++ int rc = slurp_attr(line, name, &sb) && !strcasecmp(sb.buf, value);
++ strbuf_release(&sb);
++ return rc;
++}
++
+ static void handle_content_type(struct mailinfo *mi, struct strbuf *line)
+ {
+ struct strbuf *boundary = xmalloc(sizeof(struct strbuf));
+ strbuf_init(boundary, line->len);
+
++ mi->format_flowed = has_attr_value(line->buf, "format=", "flowed");
++ mi->delsp = has_attr_value(line->buf, "delsp=", "yes");
++
+ if (slurp_attr(line->buf, "boundary=", boundary)) {
+ strbuf_insert(boundary, 0, "--", 2);
+ if (++mi->content_top >= &mi->content[MAX_BOUNDARIES]) {
+@@ -964,6 +975,52 @@ static int handle_boundary(struct mailinfo *mi,
+struct strbuf *line)
+ return 1;
+ }
+
++static void handle_filter_flowed(struct mailinfo *mi, struct strbuf *line,
++ struct strbuf *prev)
++{
++ size_t len = line->len;
++ const char *rest;
++
++ if (!mi->format_flowed) {
++ handle_filter(mi, line);
++ return;
++ }
++
++ if (line->buf[len - 1] == '\n') {
++ len--;
++ if (len && line->buf[len - 1] == '\r')
++ len--;
++ }
++
++ /* Keep signature separator as-is. */
++ if (skip_prefix(line->buf, "-- ", &rest) && rest - line->buf == len) {
++ if (prev->len) {
++ handle_filter(mi, prev);
++ strbuf_reset(prev);
++ }
++ handle_filter(mi, line);
++ return;
++ }
++
++ /* Unstuff space-stuffed line. */
++ if (len && line->buf[0] == ' ') {
++ strbuf_remove(line, 0, 1);
++ len--;
++ }
++
++ /* Save flowed line for later, but without the soft line break. */
++ if (len && line->buf[len - 1] == ' ') {
++ strbuf_add(prev, line->buf, len - !!mi->delsp);
++ return;
++ }
++
++ /* Prepend any previous partial lines */
++ strbuf_insert(line, 0, prev->buf, prev->len);
++ strbuf_reset(prev);
++
++ handle_filter(mi, line);
++}
++
+ static void handle_body(struct mailinfo *mi, struct strbuf *line)
+ {
+ struct strbuf prev = STRBUF_INIT;
+@@ -1012,7 +1069,7 @@ static void handle_body(struct mailinfo *mi,
+struct strbuf *line)
+ strbuf_addbuf(&prev, sb);
+ break;
+ }
+- handle_filter(mi, sb);
++ handle_filter_flowed(mi, sb, &prev);
+ }
+ /*
+ * The partial chunk is saved in "prev" and will be
+@@ -1022,13 +1079,16 @@ static void handle_body(struct mailinfo *mi,
+struct strbuf *line)
+ break;
+ }
+ default:
+- handle_filter(mi, line);
++ handle_filter_flowed(mi, line, &prev);
+ }
+
+ if (mi->input_error)
+ break;
+ } while (!strbuf_getwholeline(line, mi->input, '\n'));
+
++ if (prev.len)
++ handle_filter(mi, &prev);
++
+ flush_inbody_header_accum(mi);
+
+ handle_body_out:
+--
+2.18.0
diff --git a/t/t4257-am-interactive.sh b/t/t4257-am-interactive.sh
new file mode 100755
index 0000000..f26d7fd
--- /dev/null
+++ b/t/t4257-am-interactive.sh
@@ -0,0 +1,54 @@
+#!/bin/sh
+
+test_description='am --interactive tests'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'set up patches to apply' '
+ test_commit unrelated &&
+ test_commit no-conflict &&
+ test_commit conflict-patch file patch &&
+ git format-patch --stdout -2 >mbox &&
+
+ git reset --hard unrelated &&
+ test_commit conflict-main file main base
+'
+
+# Sanity check our setup.
+test_expect_success 'applying all patches generates conflict' '
+ test_must_fail git am mbox &&
+ echo resolved >file &&
+ git add -u &&
+ git am --resolved
+'
+
+test_expect_success 'interactive am can apply a single patch' '
+ git reset --hard base &&
+ # apply the first, but not the second
+ test_write_lines y n | git am -i mbox &&
+
+ echo no-conflict >expect &&
+ git log -1 --format=%s >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'interactive am can resolve conflict' '
+ git reset --hard base &&
+ # apply both; the second one will conflict
+ test_write_lines y y | test_must_fail git am -i mbox &&
+ echo resolved >file &&
+ git add -u &&
+ # interactive "--resolved" will ask us if we want to apply the result
+ echo y | git am -i --resolved &&
+
+ echo conflict-patch >expect &&
+ git log -1 --format=%s >actual &&
+ test_cmp expect actual &&
+
+ echo resolved >expect &&
+ git cat-file blob HEAD:file >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t4258-am-quoted-cr.sh b/t/t4258-am-quoted-cr.sh
new file mode 100755
index 0000000..201915b
--- /dev/null
+++ b/t/t4258-am-quoted-cr.sh
@@ -0,0 +1,37 @@
+#!/bin/sh
+
+test_description='test am --quoted-cr=<action>'
+
+. ./test-lib.sh
+
+DATA="$TEST_DIRECTORY/t4258"
+
+test_expect_success 'setup' '
+ test_write_lines one two three >text &&
+ test_commit one text &&
+ test_write_lines one owt three >text &&
+ test_commit two text
+'
+
+test_expect_success 'am warn if quoted-cr is found' '
+ git reset --hard one &&
+ test_must_fail git am "$DATA/mbox" 2>err &&
+ grep "quoted CRLF detected" err
+'
+
+test_expect_success 'am --quoted-cr=strip' '
+ test_might_fail git am --abort &&
+ git reset --hard one &&
+ git am --quoted-cr=strip "$DATA/mbox" &&
+ git diff --exit-code HEAD two
+'
+
+test_expect_success 'am with config mailinfo.quotedCr=strip' '
+ test_might_fail git am --abort &&
+ git reset --hard one &&
+ test_config mailinfo.quotedCr strip &&
+ git am "$DATA/mbox" &&
+ git diff --exit-code HEAD two
+'
+
+test_done
diff --git a/t/t4258/mbox b/t/t4258/mbox
new file mode 100644
index 0000000..1ae528b
--- /dev/null
+++ b/t/t4258/mbox
@@ -0,0 +1,12 @@
+From: A U Thor <mail@example.com>
+To: list@example.org
+Subject: [PATCH v2] sample
+Date: Mon, 3 Aug 2020 22:40:55 +0700
+Message-ID: <msg-id@example.com>
+Content-Type: text/plain; charset="utf-8"
+Content-Transfer-Encoding: base64
+
+VGhpcyBpcyBjb21taXQgbWVzc2FnZS4NCi0tLQ0KIHRleHQgfCAyICstDQogMSBmaWxlIGNoYW5n
+ZWQsIDEgaW5zZXJ0aW9uKCspLCAxIGRlbGV0aW9uKC0pDQoNCmRpZmYgLS1naXQgYS90ZXh0IGIv
+dGV4dA0KaW5kZXggNTYyNmFiZi4uZjcxOWVmZCAxMDA2NDQNCi0tLSBhL3RleHQNCisrKyBiL3Rl
+eHQNCkBAIC0xICsxIEBADQotb25lDQordHdvDQotLSANCjIuMzEuMQoK
diff --git a/t/t4300-merge-tree.sh b/t/t4300-merge-tree.sh
new file mode 100755
index 0000000..9c19726
--- /dev/null
+++ b/t/t4300-merge-tree.sh
@@ -0,0 +1,382 @@
+#!/bin/sh
+#
+# Copyright (c) 2010 Will Palmer
+#
+
+test_description='git merge-tree'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ test_commit "initial" "initial-file" "initial"
+'
+
+test_expect_success 'file add A, !B' '
+ git reset --hard initial &&
+ test_commit "add-a-not-b" "ONE" "AAA" &&
+ git merge-tree initial initial add-a-not-b >actual &&
+ cat >expected <<EXPECTED &&
+added in remote
+ their 100644 $(git rev-parse HEAD:ONE) ONE
+@@ -0,0 +1 @@
++AAA
+EXPECTED
+
+ test_cmp expected actual
+'
+
+test_expect_success 'file add !A, B' '
+ git reset --hard initial &&
+ test_commit "add-not-a-b" "ONE" "AAA" &&
+ git merge-tree initial add-not-a-b initial >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'file add A, B (same)' '
+ git reset --hard initial &&
+ test_commit "add-a-b-same-A" "ONE" "AAA" &&
+ git reset --hard initial &&
+ test_commit "add-a-b-same-B" "ONE" "AAA" &&
+ git merge-tree initial add-a-b-same-A add-a-b-same-B >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'file add A, B (different)' '
+ git reset --hard initial &&
+ test_commit "add-a-b-diff-A" "ONE" "AAA" &&
+ git reset --hard initial &&
+ test_commit "add-a-b-diff-B" "ONE" "BBB" &&
+ git merge-tree initial add-a-b-diff-A add-a-b-diff-B >actual &&
+ cat >expected <<EXPECTED &&
+added in both
+ our 100644 $(git rev-parse add-a-b-diff-A:ONE) ONE
+ their 100644 $(git rev-parse add-a-b-diff-B:ONE) ONE
+@@ -1 +1,5 @@
++<<<<<<< .our
+ AAA
++=======
++BBB
++>>>>>>> .their
+EXPECTED
+
+ test_cmp expected actual
+'
+
+test_expect_success 'file change A, !B' '
+ git reset --hard initial &&
+ test_commit "change-a-not-b" "initial-file" "BBB" &&
+ git merge-tree initial change-a-not-b initial >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'file change !A, B' '
+ git reset --hard initial &&
+ test_commit "change-not-a-b" "initial-file" "BBB" &&
+ git merge-tree initial initial change-not-a-b >actual &&
+ cat >expected <<EXPECTED &&
+merged
+ result 100644 $(git rev-parse change-a-not-b:initial-file) initial-file
+ our 100644 $(git rev-parse initial:initial-file ) initial-file
+@@ -1 +1 @@
+-initial
++BBB
+EXPECTED
+
+ test_cmp expected actual
+'
+
+test_expect_success '3-way merge with --attr-source' '
+ test_when_finished rm -rf 3-way &&
+ git init 3-way &&
+ (
+ cd 3-way &&
+ test_commit initial file1 foo &&
+ base=$(git rev-parse HEAD) &&
+ git checkout -b brancha &&
+ echo bar >>file1 &&
+ git commit -am "adding bar" &&
+ source=$(git rev-parse HEAD) &&
+ git checkout @{-1} &&
+ git checkout -b branchb &&
+ echo baz >>file1 &&
+ git commit -am "adding baz" &&
+ merge=$(git rev-parse HEAD) &&
+ git checkout -b gitattributes &&
+ test_commit "gitattributes" .gitattributes "file1 merge=union" &&
+ git checkout @{-1} &&
+ tree=$(git --attr-source=gitattributes merge-tree --write-tree \
+ --merge-base "$base" --end-of-options "$source" "$merge") &&
+ test_write_lines foo bar baz >expect &&
+ git cat-file -p "$tree:file1" >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'file change A, B (same)' '
+ git reset --hard initial &&
+ test_commit "change-a-b-same-A" "initial-file" "AAA" &&
+ git reset --hard initial &&
+ test_commit "change-a-b-same-B" "initial-file" "AAA" &&
+ git merge-tree initial change-a-b-same-A change-a-b-same-B >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'file change A, B (different)' '
+ git reset --hard initial &&
+ test_commit "change-a-b-diff-A" "initial-file" "AAA" &&
+ git reset --hard initial &&
+ test_commit "change-a-b-diff-B" "initial-file" "BBB" &&
+ git merge-tree initial change-a-b-diff-A change-a-b-diff-B >actual &&
+ cat >expected <<EXPECTED &&
+changed in both
+ base 100644 $(git rev-parse initial:initial-file ) initial-file
+ our 100644 $(git rev-parse change-a-b-diff-A:initial-file) initial-file
+ their 100644 $(git rev-parse change-a-b-diff-B:initial-file) initial-file
+@@ -1 +1,5 @@
++<<<<<<< .our
+ AAA
++=======
++BBB
++>>>>>>> .their
+EXPECTED
+
+ test_cmp expected actual
+'
+
+test_expect_success 'file change A, B (mixed)' '
+ git reset --hard initial &&
+ test_commit "change-a-b-mix-base" "ONE" "
+AAA
+AAA
+AAA
+AAA
+AAA
+AAA
+AAA
+AAA
+AAA
+AAA
+AAA
+AAA
+AAA
+AAA
+AAA" &&
+ test_commit "change-a-b-mix-A" "ONE" \
+ "$(sed -e "1{s/AAA/BBB/;}" -e "10{s/AAA/BBB/;}" <ONE)" &&
+ git reset --hard change-a-b-mix-base &&
+ test_commit "change-a-b-mix-B" "ONE" \
+ "$(sed -e "1{s/AAA/BBB/;}" -e "10{s/AAA/CCC/;}" <ONE)" &&
+ git merge-tree change-a-b-mix-base change-a-b-mix-A change-a-b-mix-B \
+ >actual &&
+
+ cat >expected <<EXPECTED &&
+changed in both
+ base 100644 $(git rev-parse change-a-b-mix-base:ONE) ONE
+ our 100644 $(git rev-parse change-a-b-mix-A:ONE ) ONE
+ their 100644 $(git rev-parse change-a-b-mix-B:ONE ) ONE
+@@ -7,7 +7,11 @@
+ AAA
+ AAA
+ AAA
++<<<<<<< .our
+ BBB
++=======
++CCC
++>>>>>>> .their
+ AAA
+ AAA
+ AAA
+EXPECTED
+
+ test_cmp expected actual
+'
+
+test_expect_success 'file remove A, !B' '
+ git reset --hard initial &&
+ test_commit "rm-a-not-b-base" "ONE" "AAA" &&
+ git rm ONE &&
+ git commit -m "rm-a-not-b" &&
+ git tag "rm-a-not-b" &&
+ git merge-tree rm-a-not-b-base rm-a-not-b rm-a-not-b-base >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'file remove !A, B' '
+ git reset --hard initial &&
+ test_commit "rm-not-a-b-base" "ONE" "AAA" &&
+ git rm ONE &&
+ git commit -m "rm-not-a-b" &&
+ git tag "rm-not-a-b" &&
+ git merge-tree rm-a-not-b-base rm-a-not-b-base rm-a-not-b >actual &&
+ cat >expected <<EXPECTED &&
+removed in remote
+ base 100644 $(git rev-parse rm-a-not-b-base:ONE) ONE
+ our 100644 $(git rev-parse rm-a-not-b-base:ONE) ONE
+@@ -1 +0,0 @@
+-AAA
+EXPECTED
+
+ test_cmp expected actual
+'
+
+test_expect_success 'file remove A, B (same)' '
+ git reset --hard initial &&
+ test_commit "rm-a-b-base" "ONE" "AAA" &&
+ git rm ONE &&
+ git commit -m "rm-a-b" &&
+ git tag "rm-a-b" &&
+ git merge-tree rm-a-b-base rm-a-b rm-a-b >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'file change A, remove B' '
+ git reset --hard initial &&
+ test_commit "change-a-rm-b-base" "ONE" "AAA" &&
+ test_commit "change-a-rm-b-A" "ONE" "BBB" &&
+ git reset --hard change-a-rm-b-base &&
+ git rm ONE &&
+ git commit -m "change-a-rm-b-B" &&
+ git tag "change-a-rm-b-B" &&
+ git merge-tree change-a-rm-b-base change-a-rm-b-A change-a-rm-b-B \
+ >actual &&
+ cat >expected <<EXPECTED &&
+removed in remote
+ base 100644 $(git rev-parse change-a-rm-b-base:ONE) ONE
+ our 100644 $(git rev-parse change-a-rm-b-A:ONE ) ONE
+@@ -1 +0,0 @@
+-BBB
+EXPECTED
+
+ test_cmp expected actual
+'
+
+test_expect_success 'file remove A, change B' '
+ git reset --hard initial &&
+ test_commit "rm-a-change-b-base" "ONE" "AAA" &&
+
+ git rm ONE &&
+ git commit -m "rm-a-change-b-A" &&
+ git tag "rm-a-change-b-A" &&
+ git reset --hard rm-a-change-b-base &&
+ test_commit "rm-a-change-b-B" "ONE" "BBB" &&
+ git merge-tree rm-a-change-b-base rm-a-change-b-A rm-a-change-b-B \
+ >actual &&
+ cat >expected <<EXPECTED &&
+removed in local
+ base 100644 $(git rev-parse rm-a-change-b-base:ONE) ONE
+ their 100644 $(git rev-parse rm-a-change-b-B:ONE ) ONE
+EXPECTED
+ test_cmp expected actual
+'
+
+test_expect_success 'tree add A, B (same)' '
+ git reset --hard initial &&
+ mkdir sub &&
+ test_commit "add sub/file" "sub/file" "file" add-tree-A &&
+ git merge-tree initial add-tree-A add-tree-A >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'tree add A, B (different)' '
+ git reset --hard initial &&
+ mkdir sub &&
+ test_commit "add sub/file" "sub/file" "AAA" add-tree-a-b-A &&
+ git reset --hard initial &&
+ mkdir sub &&
+ test_commit "add sub/file" "sub/file" "BBB" add-tree-a-b-B &&
+ git merge-tree initial add-tree-a-b-A add-tree-a-b-B >actual &&
+ cat >expect <<-EOF &&
+ added in both
+ our 100644 $(git rev-parse add-tree-a-b-A:sub/file) sub/file
+ their 100644 $(git rev-parse add-tree-a-b-B:sub/file) sub/file
+ @@ -1 +1,5 @@
+ +<<<<<<< .our
+ AAA
+ +=======
+ +BBB
+ +>>>>>>> .their
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'tree unchanged A, removed B' '
+ git reset --hard initial &&
+ mkdir sub &&
+ test_commit "add sub/file" "sub/file" "AAA" tree-remove-b-initial &&
+ git rm sub/file &&
+ test_tick &&
+ git commit -m "remove sub/file" &&
+ git tag tree-remove-b-B &&
+ git merge-tree tree-remove-b-initial tree-remove-b-initial tree-remove-b-B >actual &&
+ cat >expect <<-EOF &&
+ removed in remote
+ base 100644 $(git rev-parse tree-remove-b-initial:sub/file) sub/file
+ our 100644 $(git rev-parse tree-remove-b-initial:sub/file) sub/file
+ @@ -1 +0,0 @@
+ -AAA
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'turn file to tree' '
+ git reset --hard initial &&
+ rm initial-file &&
+ mkdir initial-file &&
+ test_commit "turn-file-to-tree" "initial-file/ONE" "CCC" &&
+ git merge-tree initial initial turn-file-to-tree >actual &&
+ cat >expect <<-EOF &&
+ added in remote
+ their 100644 $(git rev-parse turn-file-to-tree:initial-file/ONE) initial-file/ONE
+ @@ -0,0 +1 @@
+ +CCC
+ removed in remote
+ base 100644 $(git rev-parse initial:initial-file) initial-file
+ our 100644 $(git rev-parse initial:initial-file) initial-file
+ @@ -1 +0,0 @@
+ -initial
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'turn tree to file' '
+ git reset --hard initial &&
+ mkdir dir &&
+ test_commit "add-tree" "dir/path" "AAA" &&
+ test_commit "add-another-tree" "dir/another" "BBB" &&
+ rm -fr dir &&
+ test_commit "make-file" "dir" "CCC" &&
+ git merge-tree add-tree add-another-tree make-file >actual &&
+ cat >expect <<-EOF &&
+ removed in remote
+ base 100644 $(git rev-parse add-tree:dir/path) dir/path
+ our 100644 $(git rev-parse add-tree:dir/path) dir/path
+ @@ -1 +0,0 @@
+ -AAA
+ added in remote
+ their 100644 $(git rev-parse make-file:dir) dir
+ @@ -0,0 +1 @@
+ +CCC
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'merge-tree respects core.useReplaceRefs=false' '
+ test_commit merge-to &&
+ test_commit valid base &&
+ git reset --hard HEAD^ &&
+ test_commit malicious base &&
+
+ test_when_finished "git replace -d $(git rev-parse valid^0)" &&
+ git replace valid^0 malicious^0 &&
+
+ tree=$(git -c core.useReplaceRefs=true merge-tree --write-tree merge-to valid) &&
+ merged=$(git cat-file -p $tree:base) &&
+ test malicious = $merged &&
+
+ tree=$(git -c core.useReplaceRefs=false merge-tree --write-tree merge-to valid) &&
+ merged=$(git cat-file -p $tree:base) &&
+ test valid = $merged
+'
+
+test_done
diff --git a/t/t4301-merge-tree-write-tree.sh b/t/t4301-merge-tree-write-tree.sh
new file mode 100755
index 0000000..b2c8a43
--- /dev/null
+++ b/t/t4301-merge-tree-write-tree.sh
@@ -0,0 +1,948 @@
+#!/bin/sh
+
+test_description='git merge-tree --write-tree'
+
+. ./test-lib.sh
+
+# This test is ort-specific
+if test "$GIT_TEST_MERGE_ALGORITHM" != "ort"
+then
+ skip_all="GIT_TEST_MERGE_ALGORITHM != ort"
+ test_done
+fi
+
+test_expect_success setup '
+ test_write_lines 1 2 3 4 5 >numbers &&
+ echo hello >greeting &&
+ echo foo >whatever &&
+ git add numbers greeting whatever &&
+ test_tick &&
+ git commit -m initial &&
+
+ git branch side1 &&
+ git branch side2 &&
+ git branch side3 &&
+ git branch side4 &&
+
+ git checkout side1 &&
+ test_write_lines 1 2 3 4 5 6 >numbers &&
+ echo hi >greeting &&
+ echo bar >whatever &&
+ git add numbers greeting whatever &&
+ test_tick &&
+ git commit -m modify-stuff &&
+
+ git checkout side2 &&
+ test_write_lines 0 1 2 3 4 5 >numbers &&
+ echo yo >greeting &&
+ git rm whatever &&
+ mkdir whatever &&
+ >whatever/empty &&
+ git add numbers greeting whatever/empty &&
+ test_tick &&
+ git commit -m other-modifications &&
+
+ git checkout side3 &&
+ git mv numbers sequence &&
+ test_tick &&
+ git commit -m rename-numbers &&
+
+ git checkout side4 &&
+ test_write_lines 0 1 2 3 4 5 >numbers &&
+ echo yo >greeting &&
+ git add numbers greeting &&
+ test_tick &&
+ git commit -m other-content-modifications &&
+
+ git switch --orphan unrelated &&
+ >something-else &&
+ git add something-else &&
+ test_tick &&
+ git commit -m first-commit
+'
+
+test_expect_success 'Clean merge' '
+ TREE_OID=$(git merge-tree --write-tree side1 side3) &&
+ q_to_tab <<-EOF >expect &&
+ 100644 blob $(git rev-parse side1:greeting)Qgreeting
+ 100644 blob $(git rev-parse side1:numbers)Qsequence
+ 100644 blob $(git rev-parse side1:whatever)Qwhatever
+ EOF
+
+ git ls-tree $TREE_OID >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Content merge and a few conflicts' '
+ git checkout side1^0 &&
+ test_must_fail git merge side2 &&
+ expected_tree=$(git rev-parse AUTO_MERGE) &&
+
+ # We will redo the merge, while we are still in a conflicted state!
+ git ls-files -u >conflicted-file-info &&
+ test_when_finished "git reset --hard" &&
+
+ test_expect_code 1 git merge-tree --write-tree side1 side2 >RESULT &&
+ actual_tree=$(head -n 1 RESULT) &&
+
+ # Due to differences of e.g. "HEAD" vs "side1", the results will not
+ # exactly match. Dig into individual files.
+
+ # Numbers should have three-way merged cleanly
+ test_write_lines 0 1 2 3 4 5 6 >expect &&
+ git show ${actual_tree}:numbers >actual &&
+ test_cmp expect actual &&
+
+ # whatever and whatever~<branch> should have same HASHES
+ git rev-parse ${expected_tree}:whatever ${expected_tree}:whatever~HEAD >expect &&
+ git rev-parse ${actual_tree}:whatever ${actual_tree}:whatever~side1 >actual &&
+ test_cmp expect actual &&
+
+ # greeting should have a merge conflict
+ git show ${expected_tree}:greeting >tmp &&
+ sed -e s/HEAD/side1/ tmp >expect &&
+ git show ${actual_tree}:greeting >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Auto resolve conflicts by "ours" strategy option' '
+ git checkout side1^0 &&
+
+ # make sure merge conflict exists
+ test_must_fail git merge side4 &&
+ git merge --abort &&
+
+ git merge -X ours side4 &&
+ git rev-parse HEAD^{tree} >expected &&
+
+ git merge-tree -X ours side1 side4 >actual &&
+
+ test_cmp expected actual
+'
+
+test_expect_success 'Barf on misspelled option, with exit code other than 0 or 1' '
+ # Mis-spell with single "s" instead of double "s"
+ test_expect_code 129 git merge-tree --write-tree --mesages FOOBAR side1 side2 2>expect &&
+
+ grep "error: unknown option.*mesages" expect
+'
+
+test_expect_success 'Barf on too many arguments' '
+ test_expect_code 129 git merge-tree --write-tree side1 side2 invalid 2>expect &&
+
+ grep "^usage: git merge-tree" expect
+'
+
+anonymize_hash() {
+ sed -e "s/[0-9a-f]\{40,\}/HASH/g" "$@"
+}
+
+test_expect_success 'test conflict notices and such' '
+ test_expect_code 1 git merge-tree --write-tree --name-only side1 side2 >out &&
+ anonymize_hash out >actual &&
+
+ # Expected results:
+ # "greeting" should merge with conflicts
+ # "numbers" should merge cleanly
+ # "whatever" has *both* a modify/delete and a file/directory conflict
+ cat <<-EOF >expect &&
+ HASH
+ greeting
+ whatever~side1
+
+ Auto-merging greeting
+ CONFLICT (content): Merge conflict in greeting
+ Auto-merging numbers
+ CONFLICT (file/directory): directory in the way of whatever from side1; moving it to whatever~side1 instead.
+ CONFLICT (modify/delete): whatever~side1 deleted in side2 and modified in side1. Version side1 of whatever~side1 left in tree.
+ EOF
+
+ test_cmp expect actual
+'
+
+# directory rename + content conflict
+# Commit O: foo, olddir/{a,b,c}
+# Commit A: modify foo, newdir/{a,b,c}
+# Commit B: modify foo differently & rename foo -> olddir/bar
+# Expected: CONFLICT(content) for newdir/bar (not olddir/bar or foo)
+
+test_expect_success 'directory rename + content conflict' '
+ # Setup
+ git init dir-rename-and-content &&
+ (
+ cd dir-rename-and-content &&
+ test_write_lines 1 2 3 4 5 >foo &&
+ mkdir olddir &&
+ for i in a b c; do echo $i >olddir/$i || exit 1; done &&
+ git add foo olddir &&
+ git commit -m "original" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ test_write_lines 1 2 3 4 5 6 >foo &&
+ git add foo &&
+ git mv olddir newdir &&
+ git commit -m "Modify foo, rename olddir to newdir" &&
+
+ git checkout B &&
+ test_write_lines 1 2 3 4 5 six >foo &&
+ git add foo &&
+ git mv foo olddir/bar &&
+ git commit -m "Modify foo & rename foo -> olddir/bar"
+ ) &&
+ # Testing
+ (
+ cd dir-rename-and-content &&
+
+ test_expect_code 1 \
+ git merge-tree -z A^0 B^0 >out &&
+ echo >>out &&
+ anonymize_hash out >actual &&
+ q_to_tab <<-\EOF | lf_to_nul >expect &&
+ HASH
+ 100644 HASH 1Qnewdir/bar
+ 100644 HASH 2Qnewdir/bar
+ 100644 HASH 3Qnewdir/bar
+ EOF
+
+ q_to_nul <<-EOF >>expect &&
+ Q2Qnewdir/barQolddir/barQCONFLICT (directory rename suggested)QCONFLICT (file location): foo renamed to olddir/bar in B^0, inside a directory that was renamed in A^0, suggesting it should perhaps be moved to newdir/bar.
+ Q1Qnewdir/barQAuto-mergingQAuto-merging newdir/bar
+ Q1Qnewdir/barQCONFLICT (contents)QCONFLICT (content): Merge conflict in newdir/bar
+ Q
+ EOF
+ test_cmp expect actual
+ )
+'
+
+# rename/delete + modify/delete handling
+# Commit O: foo
+# Commit A: modify foo + rename to bar
+# Commit B: delete foo
+# Expected: CONFLICT(rename/delete) + CONFLICT(modify/delete)
+
+test_expect_success 'rename/delete handling' '
+ # Setup
+ git init rename-delete &&
+ (
+ cd rename-delete &&
+ test_write_lines 1 2 3 4 5 >foo &&
+ git add foo &&
+ git commit -m "original" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ test_write_lines 1 2 3 4 5 6 >foo &&
+ git add foo &&
+ git mv foo bar &&
+ git commit -m "Modify foo, rename to bar" &&
+
+ git checkout B &&
+ git rm foo &&
+ git commit -m "remove foo"
+ ) &&
+ # Testing
+ (
+ cd rename-delete &&
+
+ test_expect_code 1 \
+ git merge-tree -z A^0 B^0 >out &&
+ echo >>out &&
+ anonymize_hash out >actual &&
+ q_to_tab <<-\EOF | lf_to_nul >expect &&
+ HASH
+ 100644 HASH 1Qbar
+ 100644 HASH 2Qbar
+ EOF
+
+ q_to_nul <<-EOF >>expect &&
+ Q2QbarQfooQCONFLICT (rename/delete)QCONFLICT (rename/delete): foo renamed to bar in A^0, but deleted in B^0.
+ Q1QbarQCONFLICT (modify/delete)QCONFLICT (modify/delete): bar deleted in B^0 and modified in A^0. Version A^0 of bar left in tree.
+ Q
+ EOF
+ test_cmp expect actual
+ )
+'
+
+# rename/add handling
+# Commit O: foo
+# Commit A: modify foo, add different bar
+# Commit B: modify & rename foo->bar
+# Expected: CONFLICT(add/add) [via rename collide] for bar
+
+test_expect_success 'rename/add handling' '
+ # Setup
+ git init rename-add &&
+ (
+ cd rename-add &&
+ test_write_lines original 1 2 3 4 5 >foo &&
+ git add foo &&
+ git commit -m "original" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ test_write_lines 1 2 3 4 5 >foo &&
+ echo "different file" >bar &&
+ git add foo bar &&
+ git commit -m "Modify foo, add bar" &&
+
+ git checkout B &&
+ test_write_lines original 1 2 3 4 5 6 >foo &&
+ git add foo &&
+ git mv foo bar &&
+ git commit -m "rename foo to bar"
+ ) &&
+ # Testing
+ (
+ cd rename-add &&
+
+ test_expect_code 1 \
+ git merge-tree -z A^0 B^0 >out &&
+ echo >>out &&
+
+ #
+ # First, check that the bar that appears at stage 3 does not
+ # correspond to an individual blob anywhere in history
+ #
+ hash=$(cat out | tr "\0" "\n" | head -n 3 | grep 3.bar | cut -f 2 -d " ") &&
+ git rev-list --objects --all >all_blobs &&
+ ! grep $hash all_blobs &&
+
+ #
+ # Second, check anonymized hash output against expectation
+ #
+ anonymize_hash out >actual &&
+ q_to_tab <<-\EOF | lf_to_nul >expect &&
+ HASH
+ 100644 HASH 2Qbar
+ 100644 HASH 3Qbar
+ EOF
+
+ q_to_nul <<-EOF >>expect &&
+ Q1QbarQAuto-mergingQAuto-merging bar
+ Q1QbarQCONFLICT (contents)QCONFLICT (add/add): Merge conflict in bar
+ Q1QfooQAuto-mergingQAuto-merging foo
+ Q
+ EOF
+ test_cmp expect actual
+ )
+'
+
+# rename/add, where add is a mode conflict
+# Commit O: foo
+# Commit A: modify foo, add symlink bar
+# Commit B: modify & rename foo->bar
+# Expected: CONFLICT(distinct modes) for bar
+
+test_expect_success SYMLINKS 'rename/add, where add is a mode conflict' '
+ # Setup
+ git init rename-add-symlink &&
+ (
+ cd rename-add-symlink &&
+ test_write_lines original 1 2 3 4 5 >foo &&
+ git add foo &&
+ git commit -m "original" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ test_write_lines 1 2 3 4 5 >foo &&
+ ln -s foo bar &&
+ git add foo bar &&
+ git commit -m "Modify foo, add symlink bar" &&
+
+ git checkout B &&
+ test_write_lines original 1 2 3 4 5 6 >foo &&
+ git add foo &&
+ git mv foo bar &&
+ git commit -m "rename foo to bar"
+ ) &&
+ # Testing
+ (
+ cd rename-add-symlink &&
+
+ test_expect_code 1 \
+ git merge-tree -z A^0 B^0 >out &&
+ echo >>out &&
+
+ #
+ # First, check that the bar that appears at stage 3 does not
+ # correspond to an individual blob anywhere in history
+ #
+ hash=$(cat out | tr "\0" "\n" | head -n 3 | grep 3.bar | cut -f 2 -d " ") &&
+ git rev-list --objects --all >all_blobs &&
+ ! grep $hash all_blobs &&
+
+ #
+ # Second, check anonymized hash output against expectation
+ #
+ anonymize_hash out >actual &&
+ q_to_tab <<-\EOF | lf_to_nul >expect &&
+ HASH
+ 120000 HASH 2Qbar
+ 100644 HASH 3Qbar~B^0
+ EOF
+
+ q_to_nul <<-EOF >>expect &&
+ Q2QbarQbar~B^0QCONFLICT (distinct modes)QCONFLICT (distinct types): bar had different types on each side; renamed one of them so each can be recorded somewhere.
+ Q1QfooQAuto-mergingQAuto-merging foo
+ Q
+ EOF
+ test_cmp expect actual
+ )
+'
+
+# rename/rename(1to2) + content conflict handling
+# Commit O: foo
+# Commit A: modify foo & rename to bar
+# Commit B: modify foo & rename to baz
+# Expected: CONFLICT(rename/rename)
+
+test_expect_success 'rename/rename + content conflict' '
+ # Setup
+ git init rr-plus-content &&
+ (
+ cd rr-plus-content &&
+ test_write_lines 1 2 3 4 5 >foo &&
+ git add foo &&
+ git commit -m "original" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ test_write_lines 1 2 3 4 5 six >foo &&
+ git add foo &&
+ git mv foo bar &&
+ git commit -m "Modify foo + rename to bar" &&
+
+ git checkout B &&
+ test_write_lines 1 2 3 4 5 6 >foo &&
+ git add foo &&
+ git mv foo baz &&
+ git commit -m "Modify foo + rename to baz"
+ ) &&
+ # Testing
+ (
+ cd rr-plus-content &&
+
+ test_expect_code 1 \
+ git merge-tree -z A^0 B^0 >out &&
+ echo >>out &&
+ anonymize_hash out >actual &&
+ q_to_tab <<-\EOF | lf_to_nul >expect &&
+ HASH
+ 100644 HASH 2Qbar
+ 100644 HASH 3Qbaz
+ 100644 HASH 1Qfoo
+ EOF
+
+ q_to_nul <<-EOF >>expect &&
+ Q1QfooQAuto-mergingQAuto-merging foo
+ Q3QfooQbarQbazQCONFLICT (rename/rename)QCONFLICT (rename/rename): foo renamed to bar in A^0 and to baz in B^0.
+ Q
+ EOF
+ test_cmp expect actual
+ )
+'
+
+# rename/add/delete
+# Commit O: foo
+# Commit A: rm foo, add different bar
+# Commit B: rename foo->bar
+# Expected: CONFLICT (rename/delete), CONFLICT(add/add) [via rename collide]
+# for bar
+
+test_expect_success 'rename/add/delete conflict' '
+ # Setup
+ git init rad &&
+ (
+ cd rad &&
+ echo "original file" >foo &&
+ git add foo &&
+ git commit -m "original" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git rm foo &&
+ echo "different file" >bar &&
+ git add bar &&
+ git commit -m "Remove foo, add bar" &&
+
+ git checkout B &&
+ git mv foo bar &&
+ git commit -m "rename foo to bar"
+ ) &&
+ # Testing
+ (
+ cd rad &&
+
+ test_expect_code 1 \
+ git merge-tree -z B^0 A^0 >out &&
+ echo >>out &&
+ anonymize_hash out >actual &&
+
+ q_to_tab <<-\EOF | lf_to_nul >expect &&
+ HASH
+ 100644 HASH 2Qbar
+ 100644 HASH 3Qbar
+
+ EOF
+
+ q_to_nul <<-EOF >>expect &&
+ 2QbarQfooQCONFLICT (rename/delete)QCONFLICT (rename/delete): foo renamed to bar in B^0, but deleted in A^0.
+ Q1QbarQAuto-mergingQAuto-merging bar
+ Q1QbarQCONFLICT (contents)QCONFLICT (add/add): Merge conflict in bar
+ Q
+ EOF
+ test_cmp expect actual
+ )
+'
+
+# rename/rename(2to1)/delete/delete
+# Commit O: foo, bar
+# Commit A: rename foo->baz, rm bar
+# Commit B: rename bar->baz, rm foo
+# Expected: 2x CONFLICT (rename/delete), CONFLICT (add/add) via colliding
+# renames for baz
+
+test_expect_success 'rename/rename(2to1)/delete/delete conflict' '
+ # Setup
+ git init rrdd &&
+ (
+ cd rrdd &&
+ echo foo >foo &&
+ echo bar >bar &&
+ git add foo bar &&
+ git commit -m O &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git mv foo baz &&
+ git rm bar &&
+ git commit -m "Rename foo, remove bar" &&
+
+ git checkout B &&
+ git mv bar baz &&
+ git rm foo &&
+ git commit -m "Rename bar, remove foo"
+ ) &&
+ # Testing
+ (
+ cd rrdd &&
+
+ test_expect_code 1 \
+ git merge-tree -z A^0 B^0 >out &&
+ echo >>out &&
+ anonymize_hash out >actual &&
+
+ q_to_tab <<-\EOF | lf_to_nul >expect &&
+ HASH
+ 100644 HASH 2Qbaz
+ 100644 HASH 3Qbaz
+
+ EOF
+
+ q_to_nul <<-EOF >>expect &&
+ 2QbazQbarQCONFLICT (rename/delete)QCONFLICT (rename/delete): bar renamed to baz in B^0, but deleted in A^0.
+ Q2QbazQfooQCONFLICT (rename/delete)QCONFLICT (rename/delete): foo renamed to baz in A^0, but deleted in B^0.
+ Q1QbazQAuto-mergingQAuto-merging baz
+ Q1QbazQCONFLICT (contents)QCONFLICT (add/add): Merge conflict in baz
+ Q
+ EOF
+ test_cmp expect actual
+ )
+'
+
+# mod6: chains of rename/rename(1to2) + add/add via colliding renames
+# Commit O: one, three, five
+# Commit A: one->two, three->four, five->six
+# Commit B: one->six, three->two, five->four
+# Expected: three CONFLICT(rename/rename) messages + three CONFLICT(add/add)
+# messages; each path in two of the multi-way merged contents
+# found in two, four, six
+
+test_expect_success 'mod6: chains of rename/rename(1to2) and add/add via colliding renames' '
+ # Setup
+ git init mod6 &&
+ (
+ cd mod6 &&
+ test_seq 11 19 >one &&
+ test_seq 31 39 >three &&
+ test_seq 51 59 >five &&
+ git add . &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ test_seq 10 19 >one &&
+ echo 40 >>three &&
+ git add one three &&
+ git mv one two &&
+ git mv three four &&
+ git mv five six &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ echo 20 >>one &&
+ echo forty >>three &&
+ echo 60 >>five &&
+ git add one three five &&
+ git mv one six &&
+ git mv three two &&
+ git mv five four &&
+ test_tick &&
+ git commit -m "B"
+ ) &&
+ # Testing
+ (
+ cd mod6 &&
+
+ test_expect_code 1 \
+ git merge-tree -z A^0 B^0 >out &&
+ echo >>out &&
+
+ #
+ # First, check that some of the hashes that appear as stage
+ # conflict entries do not appear as individual blobs anywhere
+ # in history.
+ #
+ hash1=$(cat out | tr "\0" "\n" | head | grep 2.four | cut -f 2 -d " ") &&
+ hash2=$(cat out | tr "\0" "\n" | head | grep 3.two | cut -f 2 -d " ") &&
+ git rev-list --objects --all >all_blobs &&
+ ! grep $hash1 all_blobs &&
+ ! grep $hash2 all_blobs &&
+
+ #
+ # Now compare anonymized hash output with expectation
+ #
+ anonymize_hash out >actual &&
+ q_to_tab <<-\EOF | lf_to_nul >expect &&
+ HASH
+ 100644 HASH 1Qfive
+ 100644 HASH 2Qfour
+ 100644 HASH 3Qfour
+ 100644 HASH 1Qone
+ 100644 HASH 2Qsix
+ 100644 HASH 3Qsix
+ 100644 HASH 1Qthree
+ 100644 HASH 2Qtwo
+ 100644 HASH 3Qtwo
+
+ EOF
+
+ q_to_nul <<-EOF >>expect &&
+ 3QfiveQsixQfourQCONFLICT (rename/rename)QCONFLICT (rename/rename): five renamed to six in A^0 and to four in B^0.
+ Q1QfourQAuto-mergingQAuto-merging four
+ Q1QfourQCONFLICT (contents)QCONFLICT (add/add): Merge conflict in four
+ Q1QoneQAuto-mergingQAuto-merging one
+ Q3QoneQtwoQsixQCONFLICT (rename/rename)QCONFLICT (rename/rename): one renamed to two in A^0 and to six in B^0.
+ Q1QsixQAuto-mergingQAuto-merging six
+ Q1QsixQCONFLICT (contents)QCONFLICT (add/add): Merge conflict in six
+ Q1QthreeQAuto-mergingQAuto-merging three
+ Q3QthreeQfourQtwoQCONFLICT (rename/rename)QCONFLICT (rename/rename): three renamed to four in A^0 and to two in B^0.
+ Q1QtwoQAuto-mergingQAuto-merging two
+ Q1QtwoQCONFLICT (contents)QCONFLICT (add/add): Merge conflict in two
+ Q
+ EOF
+ test_cmp expect actual
+ )
+'
+
+# directory rename + rename/delete + modify/delete + directory/file conflict
+# Commit O: foo, olddir/{a,b,c}
+# Commit A: delete foo, rename olddir/ -> newdir/, add newdir/bar/file
+# Commit B: modify foo & rename foo -> olddir/bar
+# Expected: CONFLICT(content) for newdir/bar (not olddir/bar or foo)
+
+test_expect_success 'directory rename + rename/delete + modify/delete + directory/file conflict' '
+ # Setup
+ git init 4-stacked-conflict &&
+ (
+ cd 4-stacked-conflict &&
+ test_write_lines 1 2 3 4 5 >foo &&
+ mkdir olddir &&
+ for i in a b c; do echo $i >olddir/$i || exit 1; done &&
+ git add foo olddir &&
+ git commit -m "original" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git rm foo &&
+ git mv olddir newdir &&
+ mkdir newdir/bar &&
+ >newdir/bar/file &&
+ git add newdir/bar/file &&
+ git commit -m "rm foo, olddir/ -> newdir/, + newdir/bar/file" &&
+
+ git checkout B &&
+ test_write_lines 1 2 3 4 5 6 >foo &&
+ git add foo &&
+ git mv foo olddir/bar &&
+ git commit -m "Modify foo & rename foo -> olddir/bar"
+ ) &&
+ # Testing
+ (
+ cd 4-stacked-conflict &&
+
+ test_expect_code 1 \
+ git merge-tree -z A^0 B^0 >out &&
+ echo >>out &&
+ anonymize_hash out >actual &&
+
+ q_to_tab <<-\EOF | lf_to_nul >expect &&
+ HASH
+ 100644 HASH 1Qnewdir/bar~B^0
+ 100644 HASH 3Qnewdir/bar~B^0
+ EOF
+
+ q_to_nul <<-EOF >>expect &&
+ Q2Qnewdir/barQolddir/barQCONFLICT (directory rename suggested)QCONFLICT (file location): foo renamed to olddir/bar in B^0, inside a directory that was renamed in A^0, suggesting it should perhaps be moved to newdir/bar.
+ Q2Qnewdir/barQfooQCONFLICT (rename/delete)QCONFLICT (rename/delete): foo renamed to newdir/bar in B^0, but deleted in A^0.
+ Q2Qnewdir/bar~B^0Qnewdir/barQCONFLICT (file/directory)QCONFLICT (file/directory): directory in the way of newdir/bar from B^0; moving it to newdir/bar~B^0 instead.
+ Q1Qnewdir/bar~B^0QCONFLICT (modify/delete)QCONFLICT (modify/delete): newdir/bar~B^0 deleted in A^0 and modified in B^0. Version B^0 of newdir/bar~B^0 left in tree.
+ Q
+ EOF
+ test_cmp expect actual
+ )
+'
+
+for opt in $(git merge-tree --git-completion-helper-all)
+do
+ if test $opt = "--trivial-merge" || test $opt = "--write-tree"
+ then
+ continue
+ fi
+
+ test_expect_success "usage: --trivial-merge is incompatible with $opt" '
+ test_expect_code 128 git merge-tree --trivial-merge $opt side1 side2 side3
+ '
+done
+
+test_expect_success 'Just the conflicted files without the messages' '
+ test_expect_code 1 git merge-tree --write-tree --no-messages --name-only side1 side2 >out &&
+ anonymize_hash out >actual &&
+
+ test_write_lines HASH greeting whatever~side1 >expect &&
+
+ test_cmp expect actual
+'
+
+test_expect_success 'Check conflicted oids and modes without messages' '
+ test_expect_code 1 git merge-tree --write-tree --no-messages side1 side2 >out &&
+ anonymize_hash out >actual &&
+
+ # Compare the basic output format
+ q_to_tab >expect <<-\EOF &&
+ HASH
+ 100644 HASH 1Qgreeting
+ 100644 HASH 2Qgreeting
+ 100644 HASH 3Qgreeting
+ 100644 HASH 1Qwhatever~side1
+ 100644 HASH 2Qwhatever~side1
+ EOF
+
+ test_cmp expect actual &&
+
+ # Check the actual hashes against the `ls-files -u` output too
+ tail -n +2 out | sed -e s/side1/HEAD/ >actual &&
+ test_cmp conflicted-file-info actual
+'
+
+test_expect_success 'NUL terminated conflicted file "lines"' '
+ git checkout -b tweak1 side1 &&
+ test_write_lines zero 1 2 3 4 5 6 >numbers &&
+ git add numbers &&
+ git mv numbers "Αυτά μου φαίνονται κινέζικα" &&
+ git commit -m "Renamed numbers" &&
+
+ test_expect_code 1 git merge-tree --write-tree -z tweak1 side2 >out &&
+ echo >>out &&
+ anonymize_hash out >actual &&
+
+ # Expected results:
+ # "greeting" should merge with conflicts
+ # "whatever" has *both* a modify/delete and a file/directory conflict
+ # "Αυτά μου φαίνονται κινέζικα" should have a conflict
+ echo HASH | lf_to_nul >expect &&
+
+ q_to_tab <<-EOF | lf_to_nul >>expect &&
+ 100644 HASH 1Qgreeting
+ 100644 HASH 2Qgreeting
+ 100644 HASH 3Qgreeting
+ 100644 HASH 1Qwhatever~tweak1
+ 100644 HASH 2Qwhatever~tweak1
+ 100644 HASH 1QΑυτά μου φαίνονται κινέζικα
+ 100644 HASH 2QΑυτά μου φαίνονται κινέζικα
+ 100644 HASH 3QΑυτά μου φαίνονται κινέζικα
+
+ EOF
+
+ q_to_nul <<-EOF >>expect &&
+ 1QgreetingQAuto-mergingQAuto-merging greeting
+ Q1QgreetingQCONFLICT (contents)QCONFLICT (content): Merge conflict in greeting
+ Q2Qwhatever~tweak1QwhateverQCONFLICT (file/directory)QCONFLICT (file/directory): directory in the way of whatever from tweak1; moving it to whatever~tweak1 instead.
+ Q1Qwhatever~tweak1QCONFLICT (modify/delete)QCONFLICT (modify/delete): whatever~tweak1 deleted in side2 and modified in tweak1. Version tweak1 of whatever~tweak1 left in tree.
+ Q1QΑυτά μου φαίνονται κινέζικαQAuto-mergingQAuto-merging Αυτά μου φαίνονται κινέζικα
+ Q1QΑυτά μου φαίνονται κινέζικαQCONFLICT (contents)QCONFLICT (content): Merge conflict in Αυτά μου φαίνονται κινέζικα
+ Q
+ EOF
+
+ test_cmp expect actual
+'
+
+test_expect_success 'error out by default for unrelated histories' '
+ test_expect_code 128 git merge-tree --write-tree side1 unrelated 2>error &&
+
+ grep "refusing to merge unrelated histories" error
+'
+
+test_expect_success 'can override merge of unrelated histories' '
+ git merge-tree --write-tree --allow-unrelated-histories side1 unrelated >tree &&
+ TREE=$(cat tree) &&
+
+ git rev-parse side1:numbers side1:greeting side1:whatever unrelated:something-else >expect &&
+ git rev-parse $TREE:numbers $TREE:greeting $TREE:whatever $TREE:something-else >actual &&
+
+ test_cmp expect actual
+'
+
+test_expect_success SANITY 'merge-ort fails gracefully in a read-only repository' '
+ git init --bare read-only &&
+ git push read-only side1 side2 side3 &&
+ test_when_finished "chmod -R u+w read-only" &&
+ chmod -R a-w read-only &&
+ test_must_fail git -C read-only merge-tree side1 side3 &&
+ test_must_fail git -C read-only merge-tree side1 side2
+'
+
+test_expect_success '--stdin with both a successful and a conflicted merge' '
+ printf "side1 side3\nside1 side2" | git merge-tree --stdin >actual &&
+
+ git checkout side1^0 &&
+ git merge side3 &&
+
+ printf "1\0" >expect &&
+ git rev-parse HEAD^{tree} | lf_to_nul >>expect &&
+ printf "\0" >>expect &&
+
+ git checkout side1^0 &&
+ test_must_fail git merge side2 &&
+ sed s/HEAD/side1/ greeting >tmp &&
+ mv tmp greeting &&
+ git add -u &&
+ git mv whatever~HEAD whatever~side1 &&
+
+ printf "0\0" >>expect &&
+ git write-tree | lf_to_nul >>expect &&
+
+ cat <<-EOF | q_to_tab | lf_to_nul >>expect &&
+ 100644 $(git rev-parse side1~1:greeting) 1Qgreeting
+ 100644 $(git rev-parse side1:greeting) 2Qgreeting
+ 100644 $(git rev-parse side2:greeting) 3Qgreeting
+ 100644 $(git rev-parse side1~1:whatever) 1Qwhatever~side1
+ 100644 $(git rev-parse side1:whatever) 2Qwhatever~side1
+ EOF
+
+ q_to_nul <<-EOF >>expect &&
+ Q1QgreetingQAuto-mergingQAuto-merging greeting
+ Q1QgreetingQCONFLICT (contents)QCONFLICT (content): Merge conflict in greeting
+ Q1QnumbersQAuto-mergingQAuto-merging numbers
+ Q2Qwhatever~side1QwhateverQCONFLICT (file/directory)QCONFLICT (file/directory): directory in the way of whatever from side1; moving it to whatever~side1 instead.
+ Q1Qwhatever~side1QCONFLICT (modify/delete)QCONFLICT (modify/delete): whatever~side1 deleted in side2 and modified in side1. Version side1 of whatever~side1 left in tree.
+ EOF
+
+ printf "\0\0" >>expect &&
+
+ test_cmp expect actual
+'
+
+
+test_expect_success '--merge-base is incompatible with --stdin' '
+ test_must_fail git merge-tree --merge-base=side1 --stdin 2>expect &&
+
+ grep "^fatal: --merge-base is incompatible with --stdin" expect
+'
+
+# specify merge-base as parent of branch2
+# git merge-tree --write-tree --merge-base=c2 c1 c3
+# Commit c1: add file1
+# Commit c2: add file2 after c1
+# Commit c3: add file3 after c2
+# Expected: add file3, and file2 does NOT appear
+
+test_expect_success 'specify merge-base as parent of branch2' '
+ # Setup
+ test_when_finished "rm -rf base-b2-p" &&
+ git init base-b2-p &&
+ test_commit -C base-b2-p c1 file1 &&
+ test_commit -C base-b2-p c2 file2 &&
+ test_commit -C base-b2-p c3 file3 &&
+
+ # Testing
+ TREE_OID=$(git -C base-b2-p merge-tree --write-tree --merge-base=c2 c1 c3) &&
+
+ q_to_tab <<-EOF >expect &&
+ 100644 blob $(git -C base-b2-p rev-parse c1:file1)Qfile1
+ 100644 blob $(git -C base-b2-p rev-parse c3:file3)Qfile3
+ EOF
+
+ git -C base-b2-p ls-tree $TREE_OID >actual &&
+ test_cmp expect actual
+'
+
+# Since the earlier tests have verified that individual merge-tree calls
+# are doing the right thing, this test case is only used to verify that
+# we can also trigger merges via --stdin, and that when we do we get
+# the same answer as running a bunch of separate merges.
+
+test_expect_success 'check the input format when --stdin is passed' '
+ test_when_finished "rm -rf repo" &&
+ git init repo &&
+ test_commit -C repo c1 &&
+ test_commit -C repo c2 &&
+ test_commit -C repo c3 &&
+ printf "c1 c3\nc2 -- c1 c3\nc2 c3" | git -C repo merge-tree --stdin >actual &&
+
+ printf "1\0" >expect &&
+ git -C repo merge-tree --write-tree -z c1 c3 >>expect &&
+ printf "\0" >>expect &&
+
+ printf "1\0" >>expect &&
+ git -C repo merge-tree --write-tree -z --merge-base=c2 c1 c3 >>expect &&
+ printf "\0" >>expect &&
+
+ printf "1\0" >>expect &&
+ git -C repo merge-tree --write-tree -z c2 c3 >>expect &&
+ printf "\0" >>expect &&
+
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t5000-tar-tree.sh b/t/t5000-tar-tree.sh
new file mode 100755
index 0000000..4b4c331
--- /dev/null
+++ b/t/t5000-tar-tree.sh
@@ -0,0 +1,533 @@
+#!/bin/sh
+#
+# Copyright (C) 2005 Rene Scharfe
+#
+
+test_description='git archive and git get-tar-commit-id test
+
+This test covers the topics of file contents, commit date handling and
+commit id embedding:
+
+ The contents of the repository is compared to the extracted tar
+ archive. The repository contains simple text files, symlinks and a
+ binary file (/bin/sh). Only paths shorter than 99 characters are
+ used.
+
+ git archive applies the commit date to every file in the archive it
+ creates. The test sets the commit date to a specific value and checks
+ if the tar archive contains that value.
+
+ When giving git archive a commit id (in contrast to a tree id) it
+ embeds this commit id into the tar archive as a comment. The test
+ checks the ability of git get-tar-commit-id to figure it out from the
+ tar file.
+
+'
+
+TEST_CREATE_REPO_NO_TEMPLATE=1
+. ./test-lib.sh
+
+SUBSTFORMAT=%H%n
+
+test_lazy_prereq TAR_NEEDS_PAX_FALLBACK '
+ (
+ mkdir pax &&
+ cd pax &&
+ "$TAR" xf "$TEST_DIRECTORY"/t5000/pax.tar &&
+ test -f PaxHeaders.1791/file
+ )
+'
+
+test_lazy_prereq GZIP 'gzip --version'
+
+get_pax_header() {
+ file=$1
+ header=$2=
+
+ while read len rest
+ do
+ if test "$len" = $(echo "$len $rest" | wc -c)
+ then
+ case "$rest" in
+ $header*)
+ echo "${rest#$header}"
+ ;;
+ esac
+ fi
+ done <"$file"
+}
+
+check_tar() {
+ tarfile=$1.tar
+ listfile=$1.lst
+ dir=$1
+ dir_with_prefix=$dir/$2
+
+ test_expect_success ' extract tar archive' '
+ (mkdir $dir && cd $dir && "$TAR" xf -) <$tarfile
+ '
+
+ test_expect_success TAR_NEEDS_PAX_FALLBACK ' interpret pax headers' '
+ (
+ cd $dir &&
+ for header in *.paxheader
+ do
+ data=${header%.paxheader}.data &&
+ if test -h $data || test -e $data
+ then
+ path=$(get_pax_header $header path) &&
+ if test -n "$path"
+ then
+ mv "$data" "$path" || exit 1
+ fi
+ fi
+ done
+ )
+ '
+
+ test_expect_success ' validate filenames' '
+ (cd ${dir_with_prefix}a && find .) | sort >$listfile &&
+ test_cmp a.lst $listfile
+ '
+
+ test_expect_success ' validate file contents' '
+ diff -r a ${dir_with_prefix}a
+ '
+}
+
+check_added() {
+ dir=$1
+ path_in_fs=$2
+ path_in_archive=$3
+
+ test_expect_success " validate extra file $path_in_archive" '
+ diff -r $path_in_fs $dir/$path_in_archive
+ '
+}
+
+check_mtime() {
+ dir=$1
+ path_in_archive=$2
+ mtime=$3
+
+ test_expect_success " validate mtime of $path_in_archive" '
+ test-tool chmtime --get $dir/$path_in_archive >actual.mtime &&
+ echo $mtime >expect.mtime &&
+ test_cmp expect.mtime actual.mtime
+ '
+}
+
+test_expect_success 'setup' '
+ test_oid_cache <<-EOF
+ obj sha1:19f9c8273ec45a8938e6999cb59b3ff66739902a
+ obj sha256:3c666f798798601571f5cec0adb57ce4aba8546875e7693177e0535f34d2c49b
+ EOF
+'
+
+test_expect_success 'populate workdir' '
+ mkdir a &&
+ echo simple textfile >a/a &&
+ ten=0123456789 &&
+ hundred="$ten$ten$ten$ten$ten$ten$ten$ten$ten$ten" &&
+ echo long filename >"a/four$hundred" &&
+ mkdir a/bin &&
+ test-tool genrandom "frotz" 500000 >a/bin/sh &&
+ printf "A\$Format:%s\$O" "$SUBSTFORMAT" >a/substfile1 &&
+ printf "A not substituted O" >a/substfile2 &&
+ if test_have_prereq SYMLINKS
+ then
+ ln -s a a/l1
+ else
+ printf %s a >a/l1
+ fi &&
+ (
+ p=long_path_to_a_file &&
+ cd a &&
+ for depth in 1 2 3 4 5
+ do
+ mkdir $p &&
+ cd $p || exit 1
+ done &&
+ echo text >file_with_long_path
+ ) &&
+ (cd a && find .) | sort >a.lst
+'
+
+test_expect_success \
+ 'add ignored file' \
+ 'echo ignore me >a/ignored &&
+ mkdir .git/info &&
+ echo ignored export-ignore >.git/info/attributes'
+
+test_expect_success 'add files to repository' '
+ git add a &&
+ GIT_COMMITTER_DATE="2005-05-27 22:00" git commit -m initial
+'
+
+test_expect_success 'setup export-subst' '
+ echo "substfile?" export-subst >>.git/info/attributes &&
+ git log --max-count=1 "--pretty=format:A${SUBSTFORMAT}O" HEAD \
+ >a/substfile1
+'
+
+test_expect_success 'create bare clone' '
+ git clone --template= --bare . bare.git &&
+ mkdir bare.git/info &&
+ cp .git/info/attributes bare.git/info/attributes
+'
+
+test_expect_success 'remove ignored file' '
+ rm a/ignored
+'
+
+test_expect_success 'git archive' '
+ git archive HEAD >b.tar
+'
+
+check_tar b
+check_mtime b a/a 1117231200
+
+test_expect_success 'git archive --mtime' '
+ git archive --mtime=2002-02-02T02:02:02-0200 HEAD >with_mtime.tar
+'
+
+check_tar with_mtime
+check_mtime with_mtime a/a 1012622522
+
+test_expect_success 'git archive --prefix=prefix/' '
+ git archive --prefix=prefix/ HEAD >with_prefix.tar
+'
+
+check_tar with_prefix prefix/
+
+test_expect_success 'git-archive --prefix=olde-' '
+ git archive --prefix=olde- HEAD >with_olde-prefix.tar
+'
+
+check_tar with_olde-prefix olde-
+
+test_expect_success 'git archive --add-file' '
+ echo untracked >untracked &&
+ git archive --add-file=untracked HEAD >with_untracked.tar
+'
+
+check_tar with_untracked
+check_added with_untracked untracked untracked
+
+test_expect_success 'git archive --add-file twice' '
+ echo untracked >untracked &&
+ git archive --prefix=one/ --add-file=untracked \
+ --prefix=two/ --add-file=untracked \
+ --prefix= HEAD >with_untracked2.tar
+'
+
+check_tar with_untracked2
+check_added with_untracked2 untracked one/untracked
+check_added with_untracked2 untracked two/untracked
+
+test_expect_success 'git archive on large files' '
+ test_config core.bigfilethreshold 1 &&
+ git archive HEAD >b3.tar &&
+ test_cmp_bin b.tar b3.tar
+'
+
+test_expect_success 'git archive in a bare repo' '
+ git --git-dir bare.git archive HEAD >b3.tar
+'
+
+test_expect_success 'git archive vs. the same in a bare repo' '
+ test_cmp_bin b.tar b3.tar
+'
+
+test_expect_success 'git archive with --output' '
+ git archive --output=b4.tar HEAD &&
+ test_cmp_bin b.tar b4.tar
+'
+
+test_expect_success 'git archive --remote' '
+ git archive --remote=. HEAD >b5.tar &&
+ test_cmp_bin b.tar b5.tar
+'
+
+test_expect_success 'git archive --remote with configured remote' '
+ git config remote.foo.url . &&
+ (
+ cd a &&
+ git archive --remote=foo --output=../b5-nick.tar HEAD
+ ) &&
+ test_cmp_bin b.tar b5-nick.tar
+'
+
+test_expect_success 'git get-tar-commit-id' '
+ git get-tar-commit-id <b.tar >actual &&
+ git rev-parse HEAD >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git archive with --output, override inferred format' '
+ git archive --format=tar --output=d4.zip HEAD &&
+ test_cmp_bin b.tar d4.zip
+'
+
+test_expect_success GZIP 'git archive with --output and --remote creates .tgz' '
+ git archive --output=d5.tgz --remote=. HEAD &&
+ gzip -d -c <d5.tgz >d5.tar &&
+ test_cmp_bin b.tar d5.tar
+'
+
+test_expect_success 'git archive --list outside of a git repo' '
+ nongit git archive --list
+'
+
+test_expect_success 'git archive --remote outside of a git repo' '
+ git archive HEAD >expect.tar &&
+ nongit git archive --remote="$PWD" HEAD >actual.tar &&
+ test_cmp_bin expect.tar actual.tar
+'
+
+test_expect_success 'clients cannot access unreachable commits' '
+ test_commit unreachable &&
+ sha1=$(git rev-parse HEAD) &&
+ git reset --hard HEAD^ &&
+ git archive $sha1 >remote.tar &&
+ test_must_fail git archive --remote=. $sha1 >remote.tar
+'
+
+test_expect_success 'upload-archive can allow unreachable commits' '
+ test_commit unreachable1 &&
+ sha1=$(git rev-parse HEAD) &&
+ git reset --hard HEAD^ &&
+ git archive $sha1 >remote.tar &&
+ test_config uploadarchive.allowUnreachable true &&
+ git archive --remote=. $sha1 >remote.tar
+'
+
+test_expect_success 'setup tar filters' '
+ git config tar.tar.foo.command "tr ab ba" &&
+ git config tar.bar.command "tr ab ba" &&
+ git config tar.bar.remote true &&
+ git config tar.invalid baz
+'
+
+test_expect_success 'archive --list mentions user filter' '
+ git archive --list >output &&
+ grep "^tar\.foo\$" output &&
+ grep "^bar\$" output
+'
+
+test_expect_success 'archive --list shows only enabled remote filters' '
+ git archive --list --remote=. >output &&
+ ! grep "^tar\.foo\$" output &&
+ grep "^bar\$" output
+'
+
+test_expect_success 'invoke tar filter by format' '
+ git archive --format=tar.foo HEAD >config.tar.foo &&
+ tr ab ba <config.tar.foo >config.tar &&
+ test_cmp_bin b.tar config.tar &&
+ git archive --format=bar HEAD >config.bar &&
+ tr ab ba <config.bar >config.tar &&
+ test_cmp_bin b.tar config.tar
+'
+
+test_expect_success 'invoke tar filter by extension' '
+ git archive -o config-implicit.tar.foo HEAD &&
+ test_cmp_bin config.tar.foo config-implicit.tar.foo &&
+ git archive -o config-implicit.bar HEAD &&
+ test_cmp_bin config.tar.foo config-implicit.bar
+'
+
+test_expect_success 'default output format remains tar' '
+ git archive -o config-implicit.baz HEAD &&
+ test_cmp_bin b.tar config-implicit.baz
+'
+
+test_expect_success 'extension matching requires dot' '
+ git archive -o config-implicittar.foo HEAD &&
+ test_cmp_bin b.tar config-implicittar.foo
+'
+
+test_expect_success 'only enabled filters are available remotely' '
+ test_must_fail git archive --remote=. --format=tar.foo HEAD \
+ >remote.tar.foo &&
+ git archive --remote=. --format=bar >remote.bar HEAD &&
+ test_cmp_bin remote.bar config.bar
+'
+
+test_expect_success 'invalid filter is reported only once' '
+ test_must_fail git -c tar.invalid.command= archive --format=invalid \
+ HEAD >out 2>err &&
+ test_must_be_empty out &&
+ test_line_count = 1 err
+'
+
+test_expect_success 'git archive --format=tgz' '
+ git archive --format=tgz HEAD >j.tgz
+'
+
+test_expect_success 'git archive --format=tar.gz' '
+ git archive --format=tar.gz HEAD >j1.tar.gz &&
+ test_cmp_bin j.tgz j1.tar.gz
+'
+
+test_expect_success 'infer tgz from .tgz filename' '
+ git archive --output=j2.tgz HEAD &&
+ test_cmp_bin j.tgz j2.tgz
+'
+
+test_expect_success 'infer tgz from .tar.gz filename' '
+ git archive --output=j3.tar.gz HEAD &&
+ test_cmp_bin j.tgz j3.tar.gz
+'
+
+test_expect_success GZIP 'extract tgz file' '
+ gzip -d -c <j.tgz >j.tar &&
+ test_cmp_bin b.tar j.tar
+'
+
+test_expect_success 'remote tar.gz is allowed by default' '
+ git archive --remote=. --format=tar.gz HEAD >remote.tar.gz &&
+ test_cmp_bin j.tgz remote.tar.gz
+'
+
+test_expect_success 'remote tar.gz can be disabled' '
+ git config tar.tar.gz.remote false &&
+ test_must_fail git archive --remote=. --format=tar.gz HEAD \
+ >remote.tar.gz
+'
+
+test_expect_success GZIP 'git archive --format=tgz (external gzip)' '
+ test_config tar.tgz.command "gzip -cn" &&
+ git archive --format=tgz HEAD >external_gzip.tgz
+'
+
+test_expect_success GZIP 'git archive --format=tar.gz (external gzip)' '
+ test_config tar.tar.gz.command "gzip -cn" &&
+ git archive --format=tar.gz HEAD >external_gzip.tar.gz &&
+ test_cmp_bin external_gzip.tgz external_gzip.tar.gz
+'
+
+test_expect_success GZIP 'extract tgz file (external gzip)' '
+ gzip -d -c <external_gzip.tgz >external_gzip.tar &&
+ test_cmp_bin b.tar external_gzip.tar
+'
+
+test_expect_success 'archive and :(glob)' '
+ git archive -v HEAD -- ":(glob)**/sh" >/dev/null 2>actual &&
+ cat >expect <<-\EOF &&
+ a/
+ a/bin/
+ a/bin/sh
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'catch non-matching pathspec' '
+ test_must_fail git archive -v HEAD -- "*.abc" >/dev/null
+'
+
+test_expect_success 'reject paths outside the current directory' '
+ test_must_fail git -C a/bin archive HEAD .. >/dev/null 2>err &&
+ grep "outside the current directory" err
+'
+
+test_expect_success 'allow pathspecs that resolve to the current directory' '
+ git -C a/bin archive -v HEAD ../bin >/dev/null 2>actual &&
+ cat >expect <<-\EOF &&
+ sh
+ EOF
+ test_cmp expect actual
+'
+
+# Pull the size and date of each entry in a tarfile using the system tar.
+#
+# We'll pull out only the year from the date; that avoids any question of
+# timezones impacting the result (as long as we keep our test times away from a
+# year boundary; our reference times are all in August).
+#
+# The output of tar_info is expected to be "<size> <year>", both in decimal. It
+# ignores the return value of tar. We have to do this, because some of our test
+# input is only partial (the real data is 64GB in some cases).
+tar_info () {
+ "$TAR" tvf "$1" |
+ awk '{
+ split($4, date, "-")
+ print $3 " " date[1]
+ }'
+}
+
+# See if our system tar can handle a tar file with huge sizes and dates far in
+# the future, and that we can actually parse its output.
+#
+# The reference file was generated by GNU tar, and the magic time and size are
+# both octal 01000000000001, which overflows normal ustar fields.
+test_lazy_prereq TAR_HUGE '
+ echo "68719476737 4147" >expect &&
+ tar_info "$TEST_DIRECTORY"/t5000/huge-and-future.tar >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success LONG_IS_64BIT 'set up repository with huge blob' '
+ obj=$(test_oid obj) &&
+ path=$(test_oid_to_path $obj) &&
+ mkdir -p .git/objects/$(dirname $path) &&
+ cp "$TEST_DIRECTORY"/t5000/huge-object .git/objects/$path &&
+ rm -f .git/index &&
+ git update-index --add --cacheinfo 100644,$obj,huge &&
+ git commit -m huge
+'
+
+# We expect git to die with SIGPIPE here (otherwise we
+# would generate the whole 64GB).
+test_expect_success LONG_IS_64BIT 'generate tar with huge size' '
+ {
+ git archive HEAD
+ echo $? >exit-code
+ } | test_copy_bytes 4096 >huge.tar &&
+ echo 141 >expect &&
+ test_cmp expect exit-code
+'
+
+test_expect_success TAR_HUGE,LONG_IS_64BIT 'system tar can read our huge size' '
+ echo 68719476737 >expect &&
+ tar_info huge.tar | cut -d" " -f1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success TIME_IS_64BIT 'set up repository with far-future (2^34 - 1) commit' '
+ rm -f .git/index &&
+ echo foo >file &&
+ git add file &&
+ GIT_COMMITTER_DATE="@17179869183 +0000" \
+ git commit -m "tempori parendum"
+'
+
+test_expect_success TIME_IS_64BIT 'generate tar with far-future mtime' '
+ git archive HEAD >future.tar
+'
+
+test_expect_success TAR_HUGE,TIME_IS_64BIT,TIME_T_IS_64BIT 'system tar can read our future mtime' '
+ echo 2514 >expect &&
+ tar_info future.tar | cut -d" " -f2 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success TIME_IS_64BIT 'set up repository with far-far-future (2^36 + 1) commit' '
+ rm -f .git/index &&
+ echo content >file &&
+ git add file &&
+ GIT_TEST_COMMIT_GRAPH=0 GIT_COMMITTER_DATE="@68719476737 +0000" \
+ git commit -m "tempori parendum"
+'
+
+test_expect_success TIME_IS_64BIT 'generate tar with far-far-future mtime' '
+ git archive HEAD >future.tar
+'
+
+test_expect_success TAR_HUGE,TIME_IS_64BIT,TIME_T_IS_64BIT 'system tar can read our future mtime' '
+ echo 4147 >expect &&
+ tar_info future.tar | cut -d" " -f2 >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t5000/huge-and-future.tar b/t/t5000/huge-and-future.tar
new file mode 100644
index 0000000..63155e1
--- /dev/null
+++ b/t/t5000/huge-and-future.tar
Binary files differ
diff --git a/t/t5000/huge-object b/t/t5000/huge-object
new file mode 100644
index 0000000..5cbe9ec
--- /dev/null
+++ b/t/t5000/huge-object
Binary files differ
diff --git a/t/t5000/pax.tar b/t/t5000/pax.tar
new file mode 100644
index 0000000..d911737
--- /dev/null
+++ b/t/t5000/pax.tar
Binary files differ
diff --git a/t/t5001-archive-attr.sh b/t/t5001-archive-attr.sh
new file mode 100755
index 0000000..eaf959d
--- /dev/null
+++ b/t/t5001-archive-attr.sh
@@ -0,0 +1,165 @@
+#!/bin/sh
+
+test_description='git archive attribute tests'
+
+TEST_CREATE_REPO_NO_TEMPLATE=1
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+SUBSTFORMAT='%H (%h)%n'
+
+test_expect_exists() {
+ test_expect_${2:-success} " $1 exists" "test -e $1"
+}
+
+test_expect_missing() {
+ test_expect_${2:-success} " $1 does not exist" "test ! -e $1"
+}
+
+extract_tar_to_dir () {
+ (mkdir "$1" && cd "$1" && "$TAR" xf -) <"$1.tar"
+}
+
+test_expect_success 'setup' '
+ echo ignored >ignored &&
+ mkdir .git/info &&
+ echo ignored export-ignore >>.git/info/attributes &&
+ git add ignored &&
+
+ echo ignored by tree >ignored-by-tree &&
+ echo ignored-by-tree export-ignore >.gitattributes &&
+ mkdir ignored-by-tree.d &&
+ >ignored-by-tree.d/file &&
+ echo ignored-by-tree.d export-ignore >>.gitattributes &&
+ git add ignored-by-tree ignored-by-tree.d .gitattributes &&
+
+ mkdir subdir &&
+ >subdir/included &&
+ >subdir/ignored-by-subtree &&
+ >subdir/ignored-by-tree &&
+ echo ignored-by-subtree export-ignore >subdir/.gitattributes &&
+ git add subdir &&
+
+ echo ignored by worktree >ignored-by-worktree &&
+ echo ignored-by-worktree export-ignore >.gitattributes &&
+ git add ignored-by-worktree &&
+
+ mkdir excluded-by-pathspec.d &&
+ >excluded-by-pathspec.d/file &&
+ git add excluded-by-pathspec.d &&
+
+ printf "A\$Format:%s\$O" "$SUBSTFORMAT" >nosubstfile &&
+ printf "A\$Format:%s\$O" "$SUBSTFORMAT" >substfile1 &&
+ printf "A not substituted O" >substfile2 &&
+ echo "substfile?" export-subst >>.git/info/attributes &&
+ git add nosubstfile substfile1 substfile2 &&
+
+ git commit -m. &&
+
+ git clone --template= --bare . bare &&
+ mkdir bare/info &&
+ cp .git/info/attributes bare/info/attributes
+'
+
+test_expect_success 'git archive' '
+ git archive HEAD >archive.tar &&
+ (mkdir archive && cd archive && "$TAR" xf -) <archive.tar
+'
+
+test_expect_missing archive/ignored
+test_expect_missing archive/ignored-by-tree
+test_expect_missing archive/ignored-by-tree.d
+test_expect_missing archive/ignored-by-tree.d/file
+test_expect_exists archive/ignored-by-worktree
+test_expect_exists archive/excluded-by-pathspec.d
+test_expect_exists archive/excluded-by-pathspec.d/file
+
+test_expect_success 'git archive with pathspec' '
+ git archive HEAD ":!excluded-by-pathspec.d" >archive-pathspec.tar &&
+ extract_tar_to_dir archive-pathspec
+'
+
+test_expect_missing archive-pathspec/ignored
+test_expect_missing archive-pathspec/ignored-by-tree
+test_expect_missing archive-pathspec/ignored-by-tree.d
+test_expect_missing archive-pathspec/ignored-by-tree.d/file
+test_expect_exists archive-pathspec/ignored-by-worktree
+test_expect_missing archive-pathspec/excluded-by-pathspec.d
+test_expect_missing archive-pathspec/excluded-by-pathspec.d/file
+
+test_expect_success 'git archive with wildcard pathspec' '
+ git archive HEAD ":!excluded-by-p*" >archive-pathspec-wildcard.tar &&
+ extract_tar_to_dir archive-pathspec-wildcard
+'
+
+test_expect_missing archive-pathspec-wildcard/ignored
+test_expect_missing archive-pathspec-wildcard/ignored-by-tree
+test_expect_missing archive-pathspec-wildcard/ignored-by-tree.d
+test_expect_missing archive-pathspec-wildcard/ignored-by-tree.d/file
+test_expect_exists archive-pathspec-wildcard/ignored-by-worktree
+test_expect_missing archive-pathspec-wildcard/excluded-by-pathspec.d
+test_expect_missing archive-pathspec-wildcard/excluded-by-pathspec.d/file
+
+test_expect_success 'git -C subdir archive' '
+ git -C subdir archive HEAD >archive-subdir.tar &&
+ extract_tar_to_dir archive-subdir
+'
+
+test_expect_exists archive-subdir/included
+test_expect_missing archive-subdir/ignored-by-subtree
+test_expect_missing archive-subdir/ignored-by-tree
+
+test_expect_success 'git archive with worktree attributes' '
+ git archive --worktree-attributes HEAD >worktree.tar &&
+ (mkdir worktree && cd worktree && "$TAR" xf -) <worktree.tar
+'
+
+test_expect_missing worktree/ignored
+test_expect_exists worktree/ignored-by-tree
+test_expect_missing worktree/ignored-by-worktree
+
+test_expect_success 'git archive --worktree-attributes option' '
+ git archive --worktree-attributes --worktree-attributes HEAD >worktree.tar &&
+ (mkdir worktree2 && cd worktree2 && "$TAR" xf -) <worktree.tar
+'
+
+test_expect_missing worktree2/ignored
+test_expect_exists worktree2/ignored-by-tree
+test_expect_missing worktree2/ignored-by-worktree
+
+test_expect_success 'git archive vs. bare' '
+ (cd bare && git archive HEAD) >bare-archive.tar &&
+ test_cmp_bin archive.tar bare-archive.tar
+'
+
+test_expect_success 'git archive with worktree attributes, bare' '
+ (cd bare && git archive --worktree-attributes HEAD) >bare-worktree.tar &&
+ (mkdir bare-worktree && cd bare-worktree && "$TAR" xf -) <bare-worktree.tar
+'
+
+test_expect_missing bare-worktree/ignored
+test_expect_missing bare-worktree/ignored-by-tree
+test_expect_exists bare-worktree/ignored-by-worktree
+
+test_expect_success 'export-subst' '
+ git log "--pretty=format:A${SUBSTFORMAT}O" HEAD >substfile1.expected &&
+ test_cmp nosubstfile archive/nosubstfile &&
+ test_cmp substfile1.expected archive/substfile1 &&
+ test_cmp substfile2 archive/substfile2
+'
+
+test_expect_success 'export-subst expands %(describe) once' '
+ echo "\$Format:%(describe)\$" >substfile3 &&
+ echo "\$Format:%(describe)\$" >>substfile3 &&
+ echo "\$Format:%(describe)${LF}%(describe)\$" >substfile4 &&
+ git add substfile[34] &&
+ git commit -m export-subst-describe &&
+ git tag -m export-subst-describe export-subst-describe &&
+ git archive HEAD >archive-describe.tar &&
+ extract_tar_to_dir archive-describe &&
+ desc=$(git describe) &&
+ grep -F "$desc" archive-describe/substfile[34] >substituted &&
+ test_line_count = 1 substituted
+'
+
+test_done
diff --git a/t/t5002-archive-attr-pattern.sh b/t/t5002-archive-attr-pattern.sh
new file mode 100755
index 0000000..78ab75f
--- /dev/null
+++ b/t/t5002-archive-attr-pattern.sh
@@ -0,0 +1,88 @@
+#!/bin/sh
+
+test_description='git archive attribute pattern tests'
+
+TEST_PASSES_SANITIZE_LEAK=true
+TEST_CREATE_REPO_NO_TEMPLATE=1
+. ./test-lib.sh
+
+test_expect_exists() {
+ test_expect_success " $1 exists" "test -e $1"
+}
+
+test_expect_missing() {
+ test_expect_success " $1 does not exist" "test ! -e $1"
+}
+
+test_expect_success 'setup' '
+ echo ignored >ignored &&
+ mkdir .git/info &&
+ echo ignored export-ignore >>.git/info/attributes &&
+ git add ignored &&
+
+ mkdir not-ignored-dir &&
+ echo ignored-in-tree >not-ignored-dir/ignored &&
+ echo not-ignored-in-tree >not-ignored-dir/ignored-only-if-dir &&
+ git add not-ignored-dir &&
+
+ mkdir ignored-only-if-dir &&
+ echo ignored by ignored dir >ignored-only-if-dir/ignored-by-ignored-dir &&
+ echo ignored-only-if-dir/ export-ignore >>.git/info/attributes &&
+ git add ignored-only-if-dir &&
+
+ mkdir -p ignored-without-slash &&
+ echo "ignored without slash" >ignored-without-slash/foo &&
+ git add ignored-without-slash/foo &&
+ echo "ignored-without-slash export-ignore" >>.git/info/attributes &&
+
+ mkdir -p wildcard-without-slash &&
+ echo "ignored without slash" >wildcard-without-slash/foo &&
+ git add wildcard-without-slash/foo &&
+ echo "wild*-without-slash export-ignore" >>.git/info/attributes &&
+
+ mkdir -p deep/and/slashless &&
+ echo "ignored without slash" >deep/and/slashless/foo &&
+ git add deep/and/slashless/foo &&
+ echo "deep/and/slashless export-ignore" >>.git/info/attributes &&
+
+ mkdir -p deep/with/wildcard &&
+ echo "ignored without slash" >deep/with/wildcard/foo &&
+ git add deep/with/wildcard/foo &&
+ echo "deep/*t*/wildcard export-ignore" >>.git/info/attributes &&
+
+ mkdir -p one-level-lower/two-levels-lower/ignored-only-if-dir &&
+ echo ignored by ignored dir >one-level-lower/two-levels-lower/ignored-only-if-dir/ignored-by-ignored-dir &&
+ git add one-level-lower &&
+
+ git commit -m. &&
+
+ git clone --template= --bare . bare &&
+ mkdir bare/info &&
+ cp .git/info/attributes bare/info/attributes
+'
+
+test_expect_success 'git archive' '
+ git archive HEAD >archive.tar &&
+ (mkdir archive && cd archive && "$TAR" xf -) <archive.tar
+'
+
+test_expect_missing archive/ignored
+test_expect_missing archive/not-ignored-dir/ignored
+test_expect_exists archive/not-ignored-dir/ignored-only-if-dir
+test_expect_exists archive/not-ignored-dir/
+test_expect_missing archive/ignored-only-if-dir/
+test_expect_missing archive/ignored-ony-if-dir/ignored-by-ignored-dir
+test_expect_missing archive/ignored-without-slash/ &&
+test_expect_missing archive/ignored-without-slash/foo &&
+test_expect_missing archive/wildcard-without-slash/
+test_expect_missing archive/wildcard-without-slash/foo &&
+test_expect_missing archive/deep/and/slashless/ &&
+test_expect_missing archive/deep/and/slashless/foo &&
+test_expect_missing archive/deep/with/wildcard/ &&
+test_expect_missing archive/deep/with/wildcard/foo &&
+test_expect_missing archive/one-level-lower/
+test_expect_missing archive/one-level-lower/two-levels-lower/ignored-only-if-dir/
+test_expect_missing archive/one-level-lower/two-levels-lower/ignored-ony-if-dir/ignored-by-ignored-dir
+
+
+test_done
diff --git a/t/t5003-archive-zip.sh b/t/t5003-archive-zip.sh
new file mode 100755
index 0000000..fc499cd
--- /dev/null
+++ b/t/t5003-archive-zip.sh
@@ -0,0 +1,242 @@
+#!/bin/sh
+
+test_description='git archive --format=zip test'
+
+TEST_CREATE_REPO_NO_TEMPLATE=1
+. ./test-lib.sh
+
+SUBSTFORMAT=%H%n
+
+test_lazy_prereq UNZIP_SYMLINKS '
+ "$GIT_UNZIP" "$TEST_DIRECTORY"/t5003/infozip-symlinks.zip &&
+ test -h symlink
+'
+
+test_lazy_prereq UNZIP_CONVERT '
+ "$GIT_UNZIP" -a "$TEST_DIRECTORY"/t5003/infozip-symlinks.zip
+'
+
+check_zip() {
+ zipfile=$1.zip
+ listfile=$1.lst
+ dir=$1
+ dir_with_prefix=$dir/$2
+
+ test_expect_success UNZIP " extract ZIP archive" '
+ (mkdir $dir && cd $dir && "$GIT_UNZIP" ../$zipfile)
+ '
+
+ test_expect_success UNZIP " validate filenames" "
+ (cd ${dir_with_prefix}a && find .) | sort >$listfile &&
+ test_cmp a.lst $listfile
+ "
+
+ test_expect_success UNZIP " validate file contents" "
+ diff -r a ${dir_with_prefix}a
+ "
+
+ dir=eol_$1
+ dir_with_prefix=$dir/$2
+ extracted=${dir_with_prefix}a
+ original=a
+
+ test_expect_success UNZIP_CONVERT " extract ZIP archive with EOL conversion" '
+ (mkdir $dir && cd $dir && "$GIT_UNZIP" -a ../$zipfile)
+ '
+
+ test_expect_success UNZIP_CONVERT " validate that text files are converted" "
+ test_cmp_bin $extracted/text.cr $extracted/text.crlf &&
+ test_cmp_bin $extracted/text.cr $extracted/text.lf
+ "
+
+ test_expect_success UNZIP_CONVERT " validate that binary files are unchanged" "
+ test_cmp_bin $original/binary.cr $extracted/binary.cr &&
+ test_cmp_bin $original/binary.crlf $extracted/binary.crlf &&
+ test_cmp_bin $original/binary.lf $extracted/binary.lf
+ "
+
+ test_expect_success UNZIP_CONVERT " validate that diff files are converted" "
+ test_cmp_bin $extracted/diff.cr $extracted/diff.crlf &&
+ test_cmp_bin $extracted/diff.cr $extracted/diff.lf
+ "
+
+ test_expect_success UNZIP_CONVERT " validate that -diff files are unchanged" "
+ test_cmp_bin $original/nodiff.cr $extracted/nodiff.cr &&
+ test_cmp_bin $original/nodiff.crlf $extracted/nodiff.crlf &&
+ test_cmp_bin $original/nodiff.lf $extracted/nodiff.lf
+ "
+
+ test_expect_success UNZIP_CONVERT " validate that custom diff is unchanged " "
+ test_cmp_bin $original/custom.cr $extracted/custom.cr &&
+ test_cmp_bin $original/custom.crlf $extracted/custom.crlf &&
+ test_cmp_bin $original/custom.lf $extracted/custom.lf
+ "
+}
+
+check_added() {
+ dir=$1
+ path_in_fs=$2
+ path_in_archive=$3
+
+ test_expect_success UNZIP " validate extra file $path_in_archive" '
+ diff -r $path_in_fs $dir/$path_in_archive
+ '
+}
+
+test_expect_success \
+ 'populate workdir' \
+ 'mkdir a &&
+ echo simple textfile >a/a &&
+ mkdir a/bin &&
+ cp /bin/sh a/bin &&
+ printf "text\r" >a/text.cr &&
+ printf "text\r\n" >a/text.crlf &&
+ printf "text\n" >a/text.lf &&
+ printf "text\r" >a/nodiff.cr &&
+ printf "text\r\n" >a/nodiff.crlf &&
+ printf "text\n" >a/nodiff.lf &&
+ printf "text\r" >a/custom.cr &&
+ printf "text\r\n" >a/custom.crlf &&
+ printf "text\n" >a/custom.lf &&
+ printf "\0\r" >a/binary.cr &&
+ printf "\0\r\n" >a/binary.crlf &&
+ printf "\0\n" >a/binary.lf &&
+ printf "\0\r" >a/diff.cr &&
+ printf "\0\r\n" >a/diff.crlf &&
+ printf "\0\n" >a/diff.lf &&
+ printf "A\$Format:%s\$O" "$SUBSTFORMAT" >a/substfile1 &&
+ printf "A not substituted O" >a/substfile2 &&
+ (p=long_path_to_a_file && cd a &&
+ for depth in 1 2 3 4 5; do mkdir $p && cd $p || exit 1; done &&
+ echo text >file_with_long_path)
+'
+
+test_expect_success SYMLINKS,UNZIP_SYMLINKS 'add symlink' '
+ ln -s a a/symlink_to_a
+'
+
+test_expect_success 'prepare file list' '
+ (cd a && find .) | sort >a.lst
+'
+
+test_expect_success \
+ 'add ignored file' \
+ 'echo ignore me >a/ignored &&
+ mkdir .git/info &&
+ echo ignored export-ignore >.git/info/attributes'
+
+test_expect_success 'add files to repository' '
+ git add a &&
+ GIT_COMMITTER_DATE="2005-05-27 22:00" git commit -m initial
+'
+
+test_expect_success 'setup export-subst and diff attributes' '
+ echo "a/nodiff.* -diff" >>.git/info/attributes &&
+ echo "a/diff.* diff" >>.git/info/attributes &&
+ echo "a/custom.* diff=custom" >>.git/info/attributes &&
+ git config diff.custom.binary true &&
+ echo "substfile?" export-subst >>.git/info/attributes &&
+ git log --max-count=1 "--pretty=format:A${SUBSTFORMAT}O" HEAD \
+ >a/substfile1
+'
+
+test_expect_success 'create bare clone' '
+ git clone --template= --bare . bare.git &&
+ mkdir bare.git/info &&
+ cp .git/info/attributes bare.git/info/attributes &&
+ # Recreate our changes to .git/config rather than just copying it, as
+ # we do not want to clobber core.bare or other settings.
+ git -C bare.git config diff.custom.binary true
+'
+
+test_expect_success \
+ 'remove ignored file' \
+ 'rm a/ignored'
+
+test_expect_success \
+ 'git archive --format=zip' \
+ 'git archive --format=zip HEAD >d.zip'
+
+check_zip d
+
+test_expect_success \
+ 'git archive --format=zip in a bare repo' \
+ '(cd bare.git && git archive --format=zip HEAD) >d1.zip'
+
+test_expect_success \
+ 'git archive --format=zip vs. the same in a bare repo' \
+ 'test_cmp_bin d.zip d1.zip'
+
+test_expect_success 'git archive --format=zip with --output' \
+ 'git archive --format=zip --output=d2.zip HEAD &&
+ test_cmp_bin d.zip d2.zip'
+
+test_expect_success 'git archive with --output, inferring format (local)' '
+ git archive --output=d3.zip HEAD &&
+ test_cmp_bin d.zip d3.zip
+'
+
+test_expect_success 'git archive with --output, inferring format (remote)' '
+ git archive --remote=. --output=d4.zip HEAD &&
+ test_cmp_bin d.zip d4.zip
+'
+
+test_expect_success \
+ 'git archive --format=zip with prefix' \
+ 'git archive --format=zip --prefix=prefix/ HEAD >e.zip'
+
+check_zip e prefix/
+
+test_expect_success 'git archive -0 --format=zip on large files' '
+ test_config core.bigfilethreshold 1 &&
+ git archive -0 --format=zip HEAD >large.zip
+'
+
+check_zip large
+
+test_expect_success 'git archive --format=zip on large files' '
+ test_config core.bigfilethreshold 1 &&
+ git archive --format=zip HEAD >large-compressed.zip
+'
+
+check_zip large-compressed
+
+test_expect_success 'git archive --format=zip --add-file' '
+ echo untracked >untracked &&
+ git archive --format=zip --add-file=untracked HEAD >with_untracked.zip
+'
+
+check_zip with_untracked
+check_added with_untracked untracked untracked
+
+test_expect_success UNZIP 'git archive --format=zip --add-virtual-file' '
+ if test_have_prereq FUNNYNAMES
+ then
+ PATHNAME="pathname with : colon"
+ else
+ PATHNAME="pathname without colon"
+ fi &&
+ git archive --format=zip >with_file_with_content.zip \
+ --add-virtual-file=\""$PATHNAME"\": \
+ --add-virtual-file=hello:world $EMPTY_TREE &&
+ test_when_finished "rm -rf tmp-unpack" &&
+ mkdir tmp-unpack && (
+ cd tmp-unpack &&
+ "$GIT_UNZIP" ../with_file_with_content.zip &&
+ test_path_is_file hello &&
+ test_path_is_file "$PATHNAME" &&
+ test world = $(cat hello)
+ )
+'
+
+test_expect_success 'git archive --format=zip --add-file twice' '
+ echo untracked >untracked &&
+ git archive --format=zip --prefix=one/ --add-file=untracked \
+ --prefix=two/ --add-file=untracked \
+ --prefix= HEAD >with_untracked2.zip
+'
+check_zip with_untracked2
+check_added with_untracked2 untracked one/untracked
+check_added with_untracked2 untracked two/untracked
+
+test_done
diff --git a/t/t5003/infozip-symlinks.zip b/t/t5003/infozip-symlinks.zip
new file mode 100644
index 0000000..065728c
--- /dev/null
+++ b/t/t5003/infozip-symlinks.zip
Binary files differ
diff --git a/t/t5004-archive-corner-cases.sh b/t/t5004-archive-corner-cases.sh
new file mode 100755
index 0000000..9f2c6da
--- /dev/null
+++ b/t/t5004-archive-corner-cases.sh
@@ -0,0 +1,229 @@
+#!/bin/sh
+
+test_description='test corner cases of git-archive'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# the 10knuls.tar file is used to test for an empty git generated tar
+# without having to invoke tar because an otherwise valid empty GNU tar
+# will be considered broken by {Open,Net}BSD tar
+test_expect_success 'create commit with empty tree and fake empty tar' '
+ git commit --allow-empty -m foo &&
+ perl -e "print \"\\0\" x 10240" >10knuls.tar
+'
+
+# Make a dir and clean it up afterwards
+make_dir() {
+ mkdir "$1" &&
+ test_when_finished "rm -rf '$1'"
+}
+
+# Check that the dir given in "$1" contains exactly the
+# set of paths given as arguments.
+check_dir() {
+ dir=$1; shift
+ {
+ echo "$dir" &&
+ for i in "$@"; do
+ echo "$dir/$i"
+ done
+ } | sort >expect &&
+ find "$dir" ! -name pax_global_header -print | sort >actual &&
+ test_cmp expect actual
+}
+
+test_lazy_prereq UNZIP_ZIP64_SUPPORT '
+ "$GIT_UNZIP" -v | grep ZIP64_SUPPORT
+'
+
+# bsdtar/libarchive versions before 3.1.3 consider a tar file with a
+# global pax header that is not followed by a file record as corrupt.
+if "$TAR" tf "$TEST_DIRECTORY"/t5004/empty-with-pax-header.tar >/dev/null 2>&1
+then
+ test_set_prereq HEADER_ONLY_TAR_OK
+fi
+
+test_expect_success HEADER_ONLY_TAR_OK 'tar archive of commit with empty tree' '
+ git archive --format=tar HEAD >empty-with-pax-header.tar &&
+ make_dir extract &&
+ "$TAR" xf empty-with-pax-header.tar -C extract &&
+ check_dir extract
+'
+
+test_expect_success 'tar archive of empty tree is empty' '
+ git archive --format=tar HEAD: >empty.tar &&
+ test_cmp_bin 10knuls.tar empty.tar
+'
+
+test_expect_success 'tar archive of empty tree with prefix' '
+ git archive --format=tar --prefix=foo/ HEAD >prefix.tar &&
+ make_dir extract &&
+ "$TAR" xf prefix.tar -C extract &&
+ check_dir extract foo
+'
+
+test_expect_success UNZIP 'zip archive of empty tree is empty' '
+ # Detect the exit code produced when our particular flavor of unzip
+ # sees an empty archive. Infozip will generate a warning and exit with
+ # code 1. But in the name of sanity, we do not expect other unzip
+ # implementations to do the same thing (it would be perfectly
+ # reasonable to exit 0, for example).
+ #
+ # This makes our test less rigorous on some platforms (unzip may not
+ # handle the empty repo at all, making our later check of its exit code
+ # a no-op). But we cannot do anything reasonable except skip the test
+ # on such platforms anyway, and this is the moral equivalent.
+ {
+ "$GIT_UNZIP" "$TEST_DIRECTORY"/t5004/empty.zip
+ expect_code=$?
+ } &&
+
+ git archive --format=zip HEAD >empty.zip &&
+ make_dir extract &&
+ (
+ cd extract &&
+ test_expect_code $expect_code "$GIT_UNZIP" ../empty.zip
+ ) &&
+ check_dir extract
+'
+
+test_expect_success UNZIP 'zip archive of empty tree with prefix' '
+ # We do not have to play exit-code tricks here, because our
+ # result should not be empty; it has a directory in it.
+ git archive --format=zip --prefix=foo/ HEAD >prefix.zip &&
+ make_dir extract &&
+ (
+ cd extract &&
+ "$GIT_UNZIP" ../prefix.zip
+ ) &&
+ check_dir extract foo
+'
+
+test_expect_success 'archive complains about pathspec on empty tree' '
+ test_must_fail git archive --format=tar HEAD -- foo >/dev/null
+'
+
+test_expect_success 'create a commit with an empty subtree' '
+ empty_tree=$(git hash-object -t tree /dev/null) &&
+ root_tree=$(printf "040000 tree $empty_tree\tsub\n" | git mktree)
+'
+
+test_expect_success 'archive empty subtree with no pathspec' '
+ git archive --format=tar $root_tree >subtree-all.tar &&
+ test_cmp_bin 10knuls.tar subtree-all.tar
+'
+
+test_expect_success 'archive empty subtree by direct pathspec' '
+ git archive --format=tar $root_tree -- sub >subtree-path.tar &&
+ test_cmp_bin 10knuls.tar subtree-path.tar
+'
+
+ZIPINFO=zipinfo
+
+test_lazy_prereq ZIPINFO '
+ n=$("$ZIPINFO" "$TEST_DIRECTORY"/t5004/empty.zip | sed -n "2s/.* //p")
+ test "x$n" = "x0"
+'
+
+test_expect_success ZIPINFO 'zip archive with many entries' '
+ # add a directory with 256 files
+ mkdir 00 &&
+ for a in 0 1 2 3 4 5 6 7 8 9 a b c d e f
+ do
+ for b in 0 1 2 3 4 5 6 7 8 9 a b c d e f
+ do
+ : >00/$a$b || return 1
+ done
+ done &&
+ git add 00 &&
+ git commit -m "256 files in 1 directory" &&
+
+ # duplicate it to get 65536 files in 256 directories
+ subtree=$(git write-tree --prefix=00/) &&
+ for c in 0 1 2 3 4 5 6 7 8 9 a b c d e f
+ do
+ for d in 0 1 2 3 4 5 6 7 8 9 a b c d e f
+ do
+ echo "040000 tree $subtree $c$d" || return 1
+ done
+ done >tree &&
+ tree=$(git mktree <tree) &&
+
+ # zip them
+ git archive -o many.zip $tree &&
+
+ # check the number of entries in the ZIP file directory
+ expr 65536 + 256 >expect &&
+ "$ZIPINFO" -h many.zip >zipinfo &&
+ sed -n "2s/.* //p" <zipinfo >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success EXPENSIVE,UNZIP,UNZIP_ZIP64_SUPPORT \
+ 'zip archive bigger than 4GB' '
+ # build string containing 65536 characters
+ s=0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef &&
+ s=$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s &&
+ s=$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s$s &&
+
+ # create blob with a length of 65536 + 1 bytes
+ blob=$(echo $s | git hash-object -w --stdin) &&
+
+ # create tree containing 65500 entries of that blob
+ for i in $(test_seq 1 65500)
+ do
+ echo "100644 blob $blob $i" || return 1
+ done >tree &&
+ tree=$(git mktree <tree) &&
+
+ # zip it, creating an archive a bit bigger than 4GB
+ git archive -0 -o many-big.zip $tree &&
+
+ "$GIT_UNZIP" -t many-big.zip 9999 65500 &&
+ "$GIT_UNZIP" -t many-big.zip
+'
+
+test_expect_success EXPENSIVE,LONG_IS_64BIT,UNZIP,UNZIP_ZIP64_SUPPORT,ZIPINFO \
+ 'zip archive with files bigger than 4GB' '
+ # Pack created with:
+ # dd if=/dev/zero of=file bs=1M count=4100 && git hash-object -w file
+ mkdir -p .git/objects/pack &&
+ (
+ cd .git/objects/pack &&
+ "$GIT_UNZIP" "$TEST_DIRECTORY"/t5004/big-pack.zip
+ ) &&
+ blob=754a93d6fada4c6873360e6cb4b209132271ab0e &&
+ size=$(expr 4100 "*" 1024 "*" 1024) &&
+
+ # create a tree containing the file
+ tree=$(echo "100644 blob $blob big-file" | git mktree) &&
+
+ # zip it, creating an archive with a file bigger than 4GB
+ git archive -o big.zip $tree &&
+
+ "$GIT_UNZIP" -t big.zip &&
+ "$ZIPINFO" big.zip >big.lst &&
+ grep $size big.lst
+'
+
+build_tree() {
+ perl -e '
+ my $hash = $ARGV[0];
+ foreach my $order (2..6) {
+ $first = 10 ** $order;
+ foreach my $i (-13..-9) {
+ my $name = "a" x ($first + $i);
+ print "100644 blob $hash\t$name\n"
+ }
+ }
+ ' "$1"
+}
+
+test_expect_success 'tar archive with long paths' '
+ blob=$(echo foo | git hash-object -w --stdin) &&
+ tree=$(build_tree $blob | git mktree) &&
+ git archive -o long_paths.tar $tree
+'
+
+test_done
diff --git a/t/t5004/big-pack.zip b/t/t5004/big-pack.zip
new file mode 100644
index 0000000..caaf614
--- /dev/null
+++ b/t/t5004/big-pack.zip
Binary files differ
diff --git a/t/t5004/empty-with-pax-header.tar b/t/t5004/empty-with-pax-header.tar
new file mode 100644
index 0000000..da9e39e
--- /dev/null
+++ b/t/t5004/empty-with-pax-header.tar
Binary files differ
diff --git a/t/t5004/empty.zip b/t/t5004/empty.zip
new file mode 100644
index 0000000..1a76bb6
--- /dev/null
+++ b/t/t5004/empty.zip
Binary files differ
diff --git a/t/t5100-mailinfo.sh b/t/t5100-mailinfo.sh
new file mode 100755
index 0000000..db11cab
--- /dev/null
+++ b/t/t5100-mailinfo.sh
@@ -0,0 +1,271 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+test_description='git mailinfo and git mailsplit test'
+
+. ./test-lib.sh
+
+DATA="$TEST_DIRECTORY/t5100"
+
+test_expect_success 'split sample box' \
+ 'git mailsplit -o. "$DATA/sample.mbox" >last &&
+ last=$(cat last) &&
+ echo total is $last &&
+ test $(cat last) = 18'
+
+check_mailinfo () {
+ mail=$1 opt=$2
+ mo="$mail$opt"
+ git mailinfo -u $opt "msg$mo" "patch$mo" <"$mail" >"info$mo" &&
+ test_cmp "$DATA/msg$mo" "msg$mo" &&
+ test_cmp "$DATA/patch$mo" "patch$mo" &&
+ test_cmp "$DATA/info$mo" "info$mo"
+}
+
+
+for mail in 00*
+do
+ test_expect_success "mailinfo $mail" '
+ check_mailinfo "$mail" "" &&
+ if test -f "$DATA/msg$mail--scissors"
+ then
+ check_mailinfo "$mail" --scissors
+ fi &&
+ if test -f "$DATA/msg$mail--no-inbody-headers"
+ then
+ check_mailinfo "$mail" --no-inbody-headers
+ fi &&
+ if test -f "$DATA/msg$mail--message-id"
+ then
+ check_mailinfo "$mail" --message-id
+ fi
+ '
+done
+
+
+test_expect_success 'split box with rfc2047 samples' \
+ 'mkdir rfc2047 &&
+ git mailsplit -orfc2047 "$DATA/rfc2047-samples.mbox" \
+ >rfc2047/last &&
+ last=$(cat rfc2047/last) &&
+ echo total is $last &&
+ test $(cat rfc2047/last) = 11'
+
+for mail in rfc2047/00*
+do
+ test_expect_success "mailinfo $mail" '
+ git mailinfo -u "$mail-msg" "$mail-patch" <"$mail" >"$mail-info" &&
+ echo msg &&
+ test_cmp "$DATA/empty" "$mail-msg" &&
+ echo patch &&
+ test_cmp "$DATA/empty" "$mail-patch" &&
+ echo info &&
+ test_cmp "$DATA/rfc2047-info-$(basename $mail)" "$mail-info"
+ '
+done
+
+test_expect_success 'respect NULs' '
+
+ git mailsplit -d3 -o. "$DATA/nul-plain" &&
+ test_cmp "$DATA/nul-plain" 001 &&
+ (cat 001 | git mailinfo msg patch) &&
+ test_line_count = 4 patch
+
+'
+
+test_expect_success 'Preserve NULs out of MIME encoded message' '
+
+ git mailsplit -d5 -o. "$DATA/nul-b64.in" &&
+ test_cmp "$DATA/nul-b64.in" 00001 &&
+ git mailinfo msg patch <00001 &&
+ test_cmp "$DATA/nul-b64.expect" patch
+
+'
+
+test_expect_success 'mailinfo on from header without name works' '
+
+ mkdir info-from &&
+ git mailsplit -oinfo-from "$DATA/info-from.in" &&
+ test_cmp "$DATA/info-from.in" info-from/0001 &&
+ git mailinfo info-from/msg info-from/patch \
+ <info-from/0001 >info-from/out &&
+ test_cmp "$DATA/info-from.expect" info-from/out
+
+'
+
+test_expect_success 'mailinfo finds headers after embedded From line' '
+ mkdir embed-from &&
+ git mailsplit -oembed-from "$DATA/embed-from.in" &&
+ test_cmp "$DATA/embed-from.in" embed-from/0001 &&
+ git mailinfo embed-from/msg embed-from/patch \
+ <embed-from/0001 >embed-from/out &&
+ test_cmp "$DATA/embed-from.expect" embed-from/out
+'
+
+test_expect_success 'mailinfo on message with quoted >From' '
+ mkdir quoted-from &&
+ git mailsplit -oquoted-from "$DATA/quoted-from.in" &&
+ test_cmp "$DATA/quoted-from.in" quoted-from/0001 &&
+ git mailinfo quoted-from/msg quoted-from/patch \
+ <quoted-from/0001 >quoted-from/out &&
+ test_cmp "$DATA/quoted-from.expect" quoted-from/msg
+'
+
+test_expect_success 'mailinfo unescapes with --mboxrd' '
+ mkdir mboxrd &&
+ git mailsplit -omboxrd --mboxrd \
+ "$DATA/sample.mboxrd" >last &&
+ test x"$(cat last)" = x2 &&
+ for i in 0001 0002
+ do
+ git mailinfo mboxrd/msg mboxrd/patch \
+ <mboxrd/$i >mboxrd/out &&
+ test_cmp "$DATA/${i}mboxrd" mboxrd/msg || return 1
+ done &&
+ sp=" " &&
+ echo "From " >expect &&
+ echo "From " >>expect &&
+ echo >> expect &&
+ cat >sp <<-INPUT_END &&
+ From mboxrd Mon Sep 17 00:00:00 2001
+ From: trailing spacer <sp@example.com>
+ Subject: [PATCH] a commit with trailing space
+
+ From$sp
+ >From$sp
+
+ INPUT_END
+
+ git mailsplit -f2 -omboxrd --mboxrd <sp >last &&
+ test x"$(cat last)" = x1 &&
+ git mailinfo mboxrd/msg mboxrd/patch <mboxrd/0003 &&
+ test_cmp expect mboxrd/msg
+'
+
+test_expect_success 'mailinfo handles rfc2822 quoted-string' '
+ mkdir quoted-string &&
+ git mailinfo /dev/null /dev/null <"$DATA/quoted-string.in" \
+ >quoted-string/info &&
+ test_cmp "$DATA/quoted-string.expect" quoted-string/info
+'
+
+test_expect_success 'mailinfo handles rfc2822 comment' '
+ mkdir comment &&
+ git mailinfo /dev/null /dev/null <"$DATA/comment.in" \
+ >comment/info &&
+ test_cmp "$DATA/comment.expect" comment/info
+'
+
+test_expect_success 'mailinfo with mailinfo.scissors config' '
+ test_config mailinfo.scissors true &&
+ (
+ mkdir sub &&
+ cd sub &&
+ git mailinfo ../msg0014.sc ../patch0014.sc <../0014 >../info0014.sc
+ ) &&
+ test_cmp "$DATA/msg0014--scissors" msg0014.sc &&
+ test_cmp "$DATA/patch0014--scissors" patch0014.sc &&
+ test_cmp "$DATA/info0014--scissors" info0014.sc
+'
+
+
+test_expect_success 'mailinfo no options' '
+ subj="$(echo "Subject: [PATCH] [other] [PATCH] message" |
+ git mailinfo /dev/null /dev/null)" &&
+ test z"$subj" = z"Subject: message"
+'
+
+test_expect_success 'mailinfo -k' '
+ subj="$(echo "Subject: [PATCH] [other] [PATCH] message" |
+ git mailinfo -k /dev/null /dev/null)" &&
+ test z"$subj" = z"Subject: [PATCH] [other] [PATCH] message"
+'
+
+test_expect_success 'mailinfo -b no [PATCH]' '
+ subj="$(echo "Subject: [other] message" |
+ git mailinfo -b /dev/null /dev/null)" &&
+ test z"$subj" = z"Subject: [other] message"
+'
+
+test_expect_success 'mailinfo -b leading [PATCH]' '
+ subj="$(echo "Subject: [PATCH] [other] message" |
+ git mailinfo -b /dev/null /dev/null)" &&
+ test z"$subj" = z"Subject: [other] message"
+'
+
+test_expect_success 'mailinfo -b double [PATCH]' '
+ subj="$(echo "Subject: [PATCH] [PATCH] message" |
+ git mailinfo -b /dev/null /dev/null)" &&
+ test z"$subj" = z"Subject: message"
+'
+
+test_expect_success 'mailinfo -b trailing [PATCH]' '
+ subj="$(echo "Subject: [other] [PATCH] message" |
+ git mailinfo -b /dev/null /dev/null)" &&
+ test z"$subj" = z"Subject: [other] message"
+'
+
+test_expect_success 'mailinfo -b separated double [PATCH]' '
+ subj="$(echo "Subject: [PATCH] [other] [PATCH] message" |
+ git mailinfo -b /dev/null /dev/null)" &&
+ test z"$subj" = z"Subject: [other] message"
+'
+
+test_expect_success 'mailinfo handles unusual header whitespace' '
+ git mailinfo /dev/null /dev/null >actual <<-\EOF &&
+ From:Real Name <user@example.com>
+ Subject: extra spaces
+ EOF
+
+ cat >expect <<-\EOF &&
+ Author: Real Name
+ Email: user@example.com
+ Subject: extra spaces
+
+ EOF
+ test_cmp expect actual
+'
+
+check_quoted_cr_mail () {
+ mail="$1" && shift &&
+ git mailinfo -u "$@" "$mail.msg" "$mail.patch" \
+ <"$mail" >"$mail.info" 2>"$mail.err" &&
+ test_cmp "$mail-expected.msg" "$mail.msg" &&
+ test_cmp "$mail-expected.patch" "$mail.patch" &&
+ test_cmp "$DATA/quoted-cr-info" "$mail.info"
+}
+
+test_expect_success 'split base64 email with quoted-cr' '
+ mkdir quoted-cr &&
+ git mailsplit -oquoted-cr "$DATA/quoted-cr.mbox" >quoted-cr/last &&
+ test $(cat quoted-cr/last) = 2
+'
+
+test_expect_success 'mailinfo warn CR in base64 encoded email' '
+ sed -e "s/%%$//" -e "s/%%/$(printf \\015)/g" "$DATA/quoted-cr-msg" \
+ >quoted-cr/0001-expected.msg &&
+ sed "s/%%/$(printf \\015)/g" "$DATA/quoted-cr-msg" \
+ >quoted-cr/0002-expected.msg &&
+ sed -e "s/%%$//" -e "s/%%/$(printf \\015)/g" "$DATA/quoted-cr-patch" \
+ >quoted-cr/0001-expected.patch &&
+ sed "s/%%/$(printf \\015)/g" "$DATA/quoted-cr-patch" \
+ >quoted-cr/0002-expected.patch &&
+ check_quoted_cr_mail quoted-cr/0001 &&
+ test_must_be_empty quoted-cr/0001.err &&
+ check_quoted_cr_mail quoted-cr/0002 &&
+ grep "quoted CRLF detected" quoted-cr/0002.err &&
+ check_quoted_cr_mail quoted-cr/0001 --quoted-cr=nowarn &&
+ test_must_be_empty quoted-cr/0001.err &&
+ check_quoted_cr_mail quoted-cr/0002 --quoted-cr=nowarn &&
+ test_must_be_empty quoted-cr/0002.err &&
+ cp quoted-cr/0001-expected.msg quoted-cr/0002-expected.msg &&
+ cp quoted-cr/0001-expected.patch quoted-cr/0002-expected.patch &&
+ check_quoted_cr_mail quoted-cr/0001 --quoted-cr=strip &&
+ test_must_be_empty quoted-cr/0001.err &&
+ check_quoted_cr_mail quoted-cr/0002 --quoted-cr=strip &&
+ test_must_be_empty quoted-cr/0002.err
+'
+
+test_done
diff --git a/t/t5100/.gitattributes b/t/t5100/.gitattributes
new file mode 100644
index 0000000..c93f514
--- /dev/null
+++ b/t/t5100/.gitattributes
@@ -0,0 +1,4 @@
+msg* encoding=UTF-8
+info* encoding=UTF-8
+rfc2047-info-* encoding=UTF-8
+sample.mbox encoding=UTF-8
diff --git a/t/t5100/0001mboxrd b/t/t5100/0001mboxrd
new file mode 100644
index 0000000..494ec55
--- /dev/null
+++ b/t/t5100/0001mboxrd
@@ -0,0 +1,4 @@
+From the beginning, mbox should have been mboxrd
+>From escaped
+From not mangled but this line should have been escaped
+
diff --git a/t/t5100/0002mboxrd b/t/t5100/0002mboxrd
new file mode 100644
index 0000000..71343d4
--- /dev/null
+++ b/t/t5100/0002mboxrd
@@ -0,0 +1,5 @@
+ >From unchanged
+ From also unchanged
+no trailing space, no escaping necessary and '>' was intended:
+>From
+
diff --git a/t/t5100/comment.expect b/t/t5100/comment.expect
new file mode 100644
index 0000000..7228177
--- /dev/null
+++ b/t/t5100/comment.expect
@@ -0,0 +1,5 @@
+Author: A U Thor (this is (really) a comment (honestly))
+Email: somebody@example.com
+Subject: testing comments
+Date: Sun, 25 May 2008 00:38:18 -0700
+
diff --git a/t/t5100/comment.in b/t/t5100/comment.in
new file mode 100644
index 0000000..c53a192
--- /dev/null
+++ b/t/t5100/comment.in
@@ -0,0 +1,9 @@
+From 1234567890123456789012345678901234567890 Mon Sep 17 00:00:00 2001
+From: "A U Thor" <somebody@example.com> (this is \(really\) a comment (honestly))
+Date: Sun, 25 May 2008 00:38:18 -0700
+Subject: [PATCH] testing comments
+
+
+
+---
+patch
diff --git a/t/t5100/embed-from.expect b/t/t5100/embed-from.expect
new file mode 100644
index 0000000..06a3a38
--- /dev/null
+++ b/t/t5100/embed-from.expect
@@ -0,0 +1,5 @@
+Author: Commit Author
+Email: commit@example.com
+Subject: patch subject
+Date: Sat, 13 Sep 2014 21:13:23 -0400
+
diff --git a/t/t5100/embed-from.in b/t/t5100/embed-from.in
new file mode 100644
index 0000000..5f3f84e
--- /dev/null
+++ b/t/t5100/embed-from.in
@@ -0,0 +1,13 @@
+From 1234567890123456789012345678901234567890 Mon Sep 17 00:00:00 2001
+From: Email Author <email@example.com>
+Date: Sun, 25 May 2008 00:38:18 -0700
+Subject: [PATCH] email subject
+
+>From 1234567890123456789012345678901234567890 Mon Sep 17 00:00:00 2001
+From: Commit Author <commit@example.com>
+Date: Sat, 13 Sep 2014 21:13:23 -0400
+Subject: patch subject
+
+patch body
+---
+patch
diff --git a/t/t5100/empty b/t/t5100/empty
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/t/t5100/empty
diff --git a/t/t5100/info-from.expect b/t/t5100/info-from.expect
new file mode 100644
index 0000000..c31d2eb
--- /dev/null
+++ b/t/t5100/info-from.expect
@@ -0,0 +1,5 @@
+Author: bare@example.com
+Email: bare@example.com
+Subject: testing bare address in from header
+Date: Sun, 25 May 2008 00:38:18 -0700
+
diff --git a/t/t5100/info-from.in b/t/t5100/info-from.in
new file mode 100644
index 0000000..4f08209
--- /dev/null
+++ b/t/t5100/info-from.in
@@ -0,0 +1,8 @@
+From 667d8940e719cddee1cfe237cbbe215e20270b09 Mon Sep 17 00:00:00 2001
+From: bare@example.com
+Date: Sun, 25 May 2008 00:38:18 -0700
+Subject: [PATCH] testing bare address in from header
+
+commit message
+---
+patch
diff --git a/t/t5100/info0001 b/t/t5100/info0001
new file mode 100644
index 0000000..f951538
--- /dev/null
+++ b/t/t5100/info0001
@@ -0,0 +1,5 @@
+Author: A (zzz) U Thor (Comment)
+Email: a.u.thor@example.com
+Subject: a commit.
+Date: Fri, 9 Jun 2006 00:44:16 -0700
+
diff --git a/t/t5100/info0002 b/t/t5100/info0002
new file mode 100644
index 0000000..49bb0fe
--- /dev/null
+++ b/t/t5100/info0002
@@ -0,0 +1,5 @@
+Author: A U Thor
+Email: a.u.thor@example.com
+Subject: another patch
+Date: Fri, 9 Jun 2006 00:44:16 -0700
+
diff --git a/t/t5100/info0003 b/t/t5100/info0003
new file mode 100644
index 0000000..bd0d122
--- /dev/null
+++ b/t/t5100/info0003
@@ -0,0 +1,5 @@
+Author: A U Thor
+Email: a.u.thor@example.com
+Subject: third patch
+Date: Fri, 9 Jun 2006 00:44:16 -0700
+
diff --git a/t/t5100/info0004 b/t/t5100/info0004
new file mode 100644
index 0000000..616c309
--- /dev/null
+++ b/t/t5100/info0004
@@ -0,0 +1,5 @@
+Author: YOSHIFUJI Hideaki / å‰è—¤è‹±æ˜Ž
+Email: yoshfuji@linux-ipv6.org
+Subject: GIT: Try all addresses for given remote name
+Date: Thu, 21 Jul 2005 09:10:36 -0400 (EDT)
+
diff --git a/t/t5100/info0005 b/t/t5100/info0005
new file mode 100644
index 0000000..46a46fc
--- /dev/null
+++ b/t/t5100/info0005
@@ -0,0 +1,5 @@
+Author: David KÃ¥gedal
+Email: davidk@lysator.liu.se
+Subject: Fixed two bugs in git-cvsimport-script.
+Date: Mon, 15 Aug 2005 20:18:25 +0200
+
diff --git a/t/t5100/info0006 b/t/t5100/info0006
new file mode 100644
index 0000000..8c05277
--- /dev/null
+++ b/t/t5100/info0006
@@ -0,0 +1,5 @@
+Author: A U Thor
+Email: a.u.thor@example.com
+Subject: a commit.
+Date: Fri, 9 Jun 2006 00:44:16 -0700
+
diff --git a/t/t5100/info0007 b/t/t5100/info0007
new file mode 100644
index 0000000..49bb0fe
--- /dev/null
+++ b/t/t5100/info0007
@@ -0,0 +1,5 @@
+Author: A U Thor
+Email: a.u.thor@example.com
+Subject: another patch
+Date: Fri, 9 Jun 2006 00:44:16 -0700
+
diff --git a/t/t5100/info0008 b/t/t5100/info0008
new file mode 100644
index 0000000..e8a2951
--- /dev/null
+++ b/t/t5100/info0008
@@ -0,0 +1,5 @@
+Author: Junio C Hamano
+Email: junio@kernel.org
+Subject: another patch
+Date: Fri, 9 Jun 2006 00:44:16 -0700
+
diff --git a/t/t5100/info0009 b/t/t5100/info0009
new file mode 100644
index 0000000..2a66321
--- /dev/null
+++ b/t/t5100/info0009
@@ -0,0 +1,5 @@
+Author: F U Bar
+Email: f.u.bar@example.com
+Subject: updates
+Date: Mon, 17 Sep 2001 00:00:00 +0900
+
diff --git a/t/t5100/info0010 b/t/t5100/info0010
new file mode 100644
index 0000000..1791241
--- /dev/null
+++ b/t/t5100/info0010
@@ -0,0 +1,5 @@
+Author: Lukas Sandström
+Email: lukass@etek.chalmers.se
+Subject: git-mailinfo: Fix getting the subject from the body
+Date: Thu, 10 Jul 2008 23:41:33 +0200
+
diff --git a/t/t5100/info0011 b/t/t5100/info0011
new file mode 100644
index 0000000..da5a605
--- /dev/null
+++ b/t/t5100/info0011
@@ -0,0 +1,5 @@
+Author: A U Thor
+Email: a.u.thor@example.com
+Subject: Xyzzy
+Date: Fri, 8 Aug 2008 13:08:37 +0200 (CEST)
+
diff --git a/t/t5100/info0012 b/t/t5100/info0012
new file mode 100644
index 0000000..ac1216f
--- /dev/null
+++ b/t/t5100/info0012
@@ -0,0 +1,5 @@
+Author: Dmitriy Blinov
+Email: bda@mnsspb.ru
+Subject: Изменён ÑпиÑок пакетов необходимых Ð´Ð»Ñ Ñборки
+Date: Wed, 12 Nov 2008 17:54:41 +0300
+
diff --git a/t/t5100/info0012--message-id b/t/t5100/info0012--message-id
new file mode 100644
index 0000000..ac1216f
--- /dev/null
+++ b/t/t5100/info0012--message-id
@@ -0,0 +1,5 @@
+Author: Dmitriy Blinov
+Email: bda@mnsspb.ru
+Subject: Изменён ÑпиÑок пакетов необходимых Ð´Ð»Ñ Ñборки
+Date: Wed, 12 Nov 2008 17:54:41 +0300
+
diff --git a/t/t5100/info0013 b/t/t5100/info0013
new file mode 100644
index 0000000..bbe049e
--- /dev/null
+++ b/t/t5100/info0013
@@ -0,0 +1,5 @@
+Author: A U Thor
+Email: a.u.thor@example.com
+Subject: a patch
+Date: Fri, 9 Jun 2006 00:44:16 -0700
+
diff --git a/t/t5100/info0014 b/t/t5100/info0014
new file mode 100644
index 0000000..08566b3
--- /dev/null
+++ b/t/t5100/info0014
@@ -0,0 +1,5 @@
+Author: Junio Hamano
+Email: junkio@cox.net
+Subject: BLAH ONE
+Date: Thu, 20 Aug 2009 17:18:22 -0700
+
diff --git a/t/t5100/info0014--scissors b/t/t5100/info0014--scissors
new file mode 100644
index 0000000..ab9c8d0
--- /dev/null
+++ b/t/t5100/info0014--scissors
@@ -0,0 +1,5 @@
+Author: Junio C Hamano
+Email: gitster@pobox.com
+Subject: Teach mailinfo to ignore everything before -- >8 -- mark
+Date: Thu, 20 Aug 2009 17:18:22 -0700
+
diff --git a/t/t5100/info0015 b/t/t5100/info0015
new file mode 100644
index 0000000..0114f10
--- /dev/null
+++ b/t/t5100/info0015
@@ -0,0 +1,5 @@
+Author:
+Email:
+Subject: check bogus body header (from)
+Date: Fri, 9 Jun 2006 00:44:16 -0700
+
diff --git a/t/t5100/info0015--no-inbody-headers b/t/t5100/info0015--no-inbody-headers
new file mode 100644
index 0000000..c4d8d77
--- /dev/null
+++ b/t/t5100/info0015--no-inbody-headers
@@ -0,0 +1,5 @@
+Author: A U Thor
+Email: a.u.thor@example.com
+Subject: check bogus body header (from)
+Date: Fri, 9 Jun 2006 00:44:16 -0700
+
diff --git a/t/t5100/info0016 b/t/t5100/info0016
new file mode 100644
index 0000000..38ccd0d
--- /dev/null
+++ b/t/t5100/info0016
@@ -0,0 +1,5 @@
+Author: A U Thor
+Email: a.u.thor@example.com
+Subject: check bogus body header (date)
+Date: bogus
+
diff --git a/t/t5100/info0016--no-inbody-headers b/t/t5100/info0016--no-inbody-headers
new file mode 100644
index 0000000..f4857d4
--- /dev/null
+++ b/t/t5100/info0016--no-inbody-headers
@@ -0,0 +1,5 @@
+Author: A U Thor
+Email: a.u.thor@example.com
+Subject: check bogus body header (date)
+Date: Fri, 9 Jun 2006 00:44:16 -0700
+
diff --git a/t/t5100/info0017 b/t/t5100/info0017
new file mode 100644
index 0000000..d2bc89f
--- /dev/null
+++ b/t/t5100/info0017
@@ -0,0 +1,5 @@
+Author: A U Thor
+Email: a.u.thor@example.com
+Subject: A E I O U
+Date: Mon, 17 Sep 2012 14:23:44 -0700
+
diff --git a/t/t5100/info0018 b/t/t5100/info0018
new file mode 100644
index 0000000..d53e749
--- /dev/null
+++ b/t/t5100/info0018
@@ -0,0 +1,5 @@
+Author: Another Thor
+Email: a.thor@example.com
+Subject: This one contains a tab and a space
+Date: Fri, 9 Jun 2006 00:44:16 -0700
+
diff --git a/t/t5100/info0018--no-inbody-headers b/t/t5100/info0018--no-inbody-headers
new file mode 100644
index 0000000..30b17bd
--- /dev/null
+++ b/t/t5100/info0018--no-inbody-headers
@@ -0,0 +1,5 @@
+Author: A U Thor
+Email: a.u.thor@example.com
+Subject: check multiline inbody headers
+Date: Fri, 9 Jun 2006 00:44:16 -0700
+
diff --git a/t/t5100/msg0001 b/t/t5100/msg0001
new file mode 100644
index 0000000..b275a9a
--- /dev/null
+++ b/t/t5100/msg0001
@@ -0,0 +1,2 @@
+Here is a patch from A U Thor.
+
diff --git a/t/t5100/msg0002 b/t/t5100/msg0002
new file mode 100644
index 0000000..1089382
--- /dev/null
+++ b/t/t5100/msg0002
@@ -0,0 +1,21 @@
+Here is a patch from A U Thor. This addresses the issue raised in the
+message:
+
+From: Nit Picker <nit.picker@example.net>
+Subject: foo is too old
+Message-ID: <nitpicker.12121212@example.net>
+
+Hopefully this would fix the problem stated there.
+
+
+I have included an extra blank line above, but it does not have to be
+stripped away here, along with the
+whitespaces at the end of the above line. They are expected to be squashed
+when the message is made into a commit log by stripspace,
+Also, there are three blank lines after this paragraph,
+two truly blank and another full of spaces in between.
+
+
+
+Hope this helps.
+
diff --git a/t/t5100/msg0003 b/t/t5100/msg0003
new file mode 100644
index 0000000..3402b53
--- /dev/null
+++ b/t/t5100/msg0003
@@ -0,0 +1,9 @@
+Here is a patch from A U Thor. This addresses the issue raised in the
+message:
+
+From: Nit Picker <nit.picker@example.net>
+Subject: foo is too old
+Message-ID: <nitpicker.12121212@example.net>
+
+Hopefully this would fix the problem stated there.
+
diff --git a/t/t5100/msg0004 b/t/t5100/msg0004
new file mode 100644
index 0000000..6f8ba3b
--- /dev/null
+++ b/t/t5100/msg0004
@@ -0,0 +1,7 @@
+Hello.
+
+Try all addresses for given remote name until it succeeds.
+Also supports IPv6.
+
+Signed-of-by: Hideaki YOSHIFUJI <yoshfuji@linux-ipv6.org>
+
diff --git a/t/t5100/msg0005 b/t/t5100/msg0005
new file mode 100644
index 0000000..dd94cd7
--- /dev/null
+++ b/t/t5100/msg0005
@@ -0,0 +1,13 @@
+The git-cvsimport-script had a copule of small bugs that prevented me
+from importing a big CVS repository.
+
+The first was that it didn't handle removed files with a multi-digit
+primary revision number.
+
+The second was that it was asking the CVS server for "F" messages,
+although they were not handled.
+
+I also updated the documentation for that script to correspond to
+actual flags.
+
+Signed-off-by: David KÃ¥gedal <davidk@lysator.liu.se>
diff --git a/t/t5100/msg0006 b/t/t5100/msg0006
new file mode 100644
index 0000000..b275a9a
--- /dev/null
+++ b/t/t5100/msg0006
@@ -0,0 +1,2 @@
+Here is a patch from A U Thor.
+
diff --git a/t/t5100/msg0007 b/t/t5100/msg0007
new file mode 100644
index 0000000..71b23c0
--- /dev/null
+++ b/t/t5100/msg0007
@@ -0,0 +1,2 @@
+Here is an empty patch from A U Thor.
+
diff --git a/t/t5100/msg0008 b/t/t5100/msg0008
new file mode 100644
index 0000000..a80ecb9
--- /dev/null
+++ b/t/t5100/msg0008
@@ -0,0 +1,4 @@
+>Here is an empty patch from A U Thor.
+
+Hey you forgot the patch!
+
diff --git a/t/t5100/msg0009 b/t/t5100/msg0009
new file mode 100644
index 0000000..9ffe131
--- /dev/null
+++ b/t/t5100/msg0009
@@ -0,0 +1,2 @@
+This is to fix diff-format documentation.
+
diff --git a/t/t5100/msg0010 b/t/t5100/msg0010
new file mode 100644
index 0000000..a96c230
--- /dev/null
+++ b/t/t5100/msg0010
@@ -0,0 +1,5 @@
+"Subject: " isn't in the static array "header", and thus
+memcmp("Subject: ", header[i], 7) will never match.
+
+Signed-off-by: Lukas Sandström <lukass@etek.chalmers.se>
+Signed-off-by: Junio C Hamano <gitster@pobox.com>
diff --git a/t/t5100/msg0011 b/t/t5100/msg0011
new file mode 100644
index 0000000..4667f21
--- /dev/null
+++ b/t/t5100/msg0011
@@ -0,0 +1,2 @@
+Here comes a commit log message, and
+its second line is here.
diff --git a/t/t5100/msg0012 b/t/t5100/msg0012
new file mode 100644
index 0000000..1dc2bf7
--- /dev/null
+++ b/t/t5100/msg0012
@@ -0,0 +1,7 @@
+textlive-* иÑправлены на texlive-*
+docutils заменён на python-docutils
+
+ДейÑтвительно, оказалоÑÑŒ, что rest2web вытÑгивает за Ñобой
+python-docutils. Ð’ то Ð²Ñ€ÐµÐ¼Ñ ÐºÐ°Ðº Ñам rest2web не нужен.
+
+Signed-off-by: Dmitriy Blinov <bda@mnsspb.ru>
diff --git a/t/t5100/msg0012--message-id b/t/t5100/msg0012--message-id
new file mode 100644
index 0000000..4448295
--- /dev/null
+++ b/t/t5100/msg0012--message-id
@@ -0,0 +1,8 @@
+textlive-* иÑправлены на texlive-*
+docutils заменён на python-docutils
+
+ДейÑтвительно, оказалоÑÑŒ, что rest2web вытÑгивает за Ñобой
+python-docutils. Ð’ то Ð²Ñ€ÐµÐ¼Ñ ÐºÐ°Ðº Ñам rest2web не нужен.
+
+Signed-off-by: Dmitriy Blinov <bda@mnsspb.ru>
+Message-ID: <1226501681-24923-1-git-send-email-bda@mnsspb.ru>
diff --git a/t/t5100/msg0013 b/t/t5100/msg0013
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/t/t5100/msg0013
diff --git a/t/t5100/msg0014 b/t/t5100/msg0014
new file mode 100644
index 0000000..62e5cd2
--- /dev/null
+++ b/t/t5100/msg0014
@@ -0,0 +1,18 @@
+In real life, we will see a discussion that inspired this patch
+discussing related and unrelated things around >8 scissors mark
+in this part of the message.
+
+Subject: [PATCH] BLAH TWO
+
+And then we will see the scissors.
+
+ This line is not a scissors mark -- >8 -- but talks about it.
+ - - >8 - - please remove everything above this line - - >8 - -
+
+Subject: [PATCH] Teach mailinfo to ignore everything before -- >8 -- mark
+From: Junio C Hamano <gitster@pobox.com>
+
+This teaches mailinfo the scissors -- >8 -- mark; the command ignores
+everything before it in the message body.
+
+Signed-off-by: Junio C Hamano <gitster@pobox.com>
diff --git a/t/t5100/msg0014--scissors b/t/t5100/msg0014--scissors
new file mode 100644
index 0000000..259c6a4
--- /dev/null
+++ b/t/t5100/msg0014--scissors
@@ -0,0 +1,4 @@
+This teaches mailinfo the scissors -- >8 -- mark; the command ignores
+everything before it in the message body.
+
+Signed-off-by: Junio C Hamano <gitster@pobox.com>
diff --git a/t/t5100/msg0015 b/t/t5100/msg0015
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/t/t5100/msg0015
diff --git a/t/t5100/msg0015--no-inbody-headers b/t/t5100/msg0015--no-inbody-headers
new file mode 100644
index 0000000..be5115b
--- /dev/null
+++ b/t/t5100/msg0015--no-inbody-headers
@@ -0,0 +1,3 @@
+From: bogosity
+ - a list
+ - of stuff
diff --git a/t/t5100/msg0016 b/t/t5100/msg0016
new file mode 100644
index 0000000..0d9adad
--- /dev/null
+++ b/t/t5100/msg0016
@@ -0,0 +1,2 @@
+and some content
+
diff --git a/t/t5100/msg0016--no-inbody-headers b/t/t5100/msg0016--no-inbody-headers
new file mode 100644
index 0000000..1063f51
--- /dev/null
+++ b/t/t5100/msg0016--no-inbody-headers
@@ -0,0 +1,4 @@
+Date: bogus
+
+and some content
+
diff --git a/t/t5100/msg0017 b/t/t5100/msg0017
new file mode 100644
index 0000000..2ee0900
--- /dev/null
+++ b/t/t5100/msg0017
@@ -0,0 +1,2 @@
+New content here
+
diff --git a/t/t5100/msg0018 b/t/t5100/msg0018
new file mode 100644
index 0000000..56de83d
--- /dev/null
+++ b/t/t5100/msg0018
@@ -0,0 +1,2 @@
+a commit message
+
diff --git a/t/t5100/msg0018--no-inbody-headers b/t/t5100/msg0018--no-inbody-headers
new file mode 100644
index 0000000..b1e05d3
--- /dev/null
+++ b/t/t5100/msg0018--no-inbody-headers
@@ -0,0 +1,8 @@
+From: Another Thor
+ <a.thor@example.com>
+Subject: This one contains
+ a tab
+ and a space
+
+a commit message
+
diff --git a/t/t5100/nul-b64.expect b/t/t5100/nul-b64.expect
new file mode 100644
index 0000000..d7d680f
--- /dev/null
+++ b/t/t5100/nul-b64.expect
Binary files differ
diff --git a/t/t5100/nul-b64.in b/t/t5100/nul-b64.in
new file mode 100644
index 0000000..16540d9
--- /dev/null
+++ b/t/t5100/nul-b64.in
@@ -0,0 +1,37 @@
+From 667d8940e719cddee1cfe237cbbe215e20270b09 Mon Sep 17 00:00:00 2001
+From: Junio C Hamano <gitster@pobox.com>
+Date: Sun, 25 May 2008 00:38:18 -0700
+Subject: [PATCH] second
+Content-Transfer-Encoding: base64
+
+LS0tCiBmaWxlIHwgIEJpbiAxMzU3IC0+IDEzNTcgYnl0ZXMKIDEgZmlsZXMgY2hhbmdlZCwg
+MCBpbnNlcnRpb25zKCspLCAwIGRlbGV0aW9ucygtKQoKZGlmZiAtLWdpdCBhL2ZpbGUgYi9m
+aWxlCmluZGV4IDc3MzYxZDguLjllMDJiZTYgMTAwNjQ0Ci0tLSBhL2ZpbGUKKysrIGIvZmls
+ZQpAQCAtMSwxMiArMSwxMiBAQAogTG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNl
+Y3RldHVlciBhZGlwaXNjaW5nIGVsaXQuIFN1c3BlbmRpc3NlCiBzaXQgYW1ldCB0dXJwaXMg
+ZWdldCBlc3QgY3Vyc3VzIGxhb3JlZXQuIEFsaXF1YW0gbWF1cmlzLiBQcmFlc2VudAotdm9s
+dXRwYXQuIFByb2luIGluIHB1cnVzLiBOdWxsYSB1cm5hIHNhcGllbiwgZGFwaWJ1cyBzaXQg
+YW1ldCwKK3ZvbHV0cGF0LiBQcm9pbiBpbiBwdXJ1cy4gTnVsbGEgdXJuYSBzYXBpZW4sIGRh
+cGkAdXMgc2l0IGFtZXQsCiBoZW5kcmVyaXQgbmVjLCB0ZW1wdXMgZXUsIG1pLiBVdCBwb3J0
+YSwgbGVvIGlkIHRpbmNpZHVudCB1bGxhbWNvcnBlciwKLXZlbGl0IGZlbGlzIHRyaXN0aXF1
+ZSBhbnRlLCBhdCBsb2JvcnRpcyBkaWFtIHBlZGUgdXQgZHVpLiBQcm9pbiBhYwordmVsaXQg
+ZmVsaXMgdHJpc3RpcXVlIGFudGUsIGF0IGxvAG9ydGlzIGRpYW0gcGVkZSB1dCBkdWkuIFBy
+b2luIGFjCiBsZWN0dXMuIERvbmVjIGF0IG1hc3NhIGFjIGlwc3VtIGhlbmRyZXJpdCBzb2xs
+aWNpdHVkaW4uIE5hbSBkaWN0dW0KIG5pc2kgc2VkIG1pLiBEdWlzIHNlZCBhbnRlLiBVdCB2
+aXRhZSBlc3QgdXQgZHVpIHVsdHJpY2llcyBkaWduaXNzaW0uCiAKLUluIHZlbCBvZGlvIGVn
+ZXQgbmlzbCBjb252YWxsaXMgdm9sdXRwYXQuIE1vcmJpIHZpdGFlIG5pYmguIE51bGxhbQor
+SW4gdmVsIG9kaW8gZWdldCBuaXNsIGNvbnZhbGxpcyB2b2x1dHBhdC4gTW9yAGkgdml0YWUg
+bmkAaC4gTnVsbGFtCiBhY2N1bXNhbiwgZG9sb3IgcXVpcyBhbGlxdWFtIHNjZWxlcmlzcXVl
+LCBlbGl0IGVuaW0gY29uZGltZW50dW0KIG1hdXJpcywgbm9uIHRyaXN0aXF1ZSBtYXVyaXMg
+dHVycGlzIGV0IG1hdXJpcy4gVXQgbm9uIG5pc2wuIE5hbSBkaWFtCiBtaSwgc2VtcGVyIHBv
+c3VlcmUsIGVsZWlmZW5kIHV0LCBhdWN0b3IgdmVsLCBlcmF0LiBTZWQgcG9zdWVyZQpAQCAt
+MTYsNyArMTYsNyBAQCBzZWQgZXN0LiBFdGlhbSBkaWFtIGZlbGlzLCBmZXJtZW50dW0gZWdl
+dCwgYWRpcGlzY2luZyBhdCwgcG9zdWVyZSBpbiwKIGR1aS4gRXRpYW0gbHVjdHVzLgogCiBO
+dWxsYSBpZCBhdWd1ZS4gTmFtIGlhY3VsaXMgYWNjdW1zYW4gbmlzaS4gU3VzcGVuZGlzc2Ug
+cG90ZW50aS4gTnVuYwotdmFyaXVzIGF1Z3VlIG5lYyBvcmNpLiBVdCBjb25kaW1lbnR1bSBk
+b2xvciBzYWdpdHRpcyBuaWJoLiBTdXNwZW5kaXNzZQordmFyaXVzIGF1Z3VlIG5lYyBvcmNp
+LiBVdCBjb25kaW1lbnR1bSBkb2xvciBzYWdpdHRpcyBuaQBoLiBTdXNwZW5kaXNzZQogdGVt
+cG9yIGxlY3R1cyBzZWQgbWFnbmEuIFN1c3BlbmRpc3NlIHBvdGVudGkuIE51bGxhbSB0ZW1w
+b3IgaXBzdW0uIFNlZAogbW9sZXN0aWUgdGVsbHVzLiBQaGFzZWxsdXMgbGlndWxhLiBJbiB2
+ZWhpY3VsYSB1bHRyaWNlcwogbmlzaS4gU3VzcGVuZGlzc2UgZmVsaXMgYXVndWUsIHBlbGxl
+bnRlc3F1ZSBhdCwgZGljdHVtIHZpdmVycmEsCi0tIAoxLjUuNS4xLjU0MC5nNTc3ODAKCg==
diff --git a/t/t5100/nul-plain b/t/t5100/nul-plain
new file mode 100644
index 0000000..3d40691
--- /dev/null
+++ b/t/t5100/nul-plain
Binary files differ
diff --git a/t/t5100/patch0001 b/t/t5100/patch0001
new file mode 100644
index 0000000..02c9774
--- /dev/null
+++ b/t/t5100/patch0001
@@ -0,0 +1,14 @@
+---
+ foo | 2 +-
+ 1 files changed, 1 insertions(+), 1 deletions(-)
+
+diff --git a/foo b/foo
+index 9123cdc..918dcf8 100644
+--- a/foo
++++ b/foo
+@@ -1 +1 @@
+-Fri Jun 9 00:44:04 PDT 2006
++Fri Jun 9 00:44:13 PDT 2006
+--
+1.4.0.g6f2b
+
diff --git a/t/t5100/patch0002 b/t/t5100/patch0002
new file mode 100644
index 0000000..02c9774
--- /dev/null
+++ b/t/t5100/patch0002
@@ -0,0 +1,14 @@
+---
+ foo | 2 +-
+ 1 files changed, 1 insertions(+), 1 deletions(-)
+
+diff --git a/foo b/foo
+index 9123cdc..918dcf8 100644
+--- a/foo
++++ b/foo
+@@ -1 +1 @@
+-Fri Jun 9 00:44:04 PDT 2006
++Fri Jun 9 00:44:13 PDT 2006
+--
+1.4.0.g6f2b
+
diff --git a/t/t5100/patch0003 b/t/t5100/patch0003
new file mode 100644
index 0000000..02c9774
--- /dev/null
+++ b/t/t5100/patch0003
@@ -0,0 +1,14 @@
+---
+ foo | 2 +-
+ 1 files changed, 1 insertions(+), 1 deletions(-)
+
+diff --git a/foo b/foo
+index 9123cdc..918dcf8 100644
+--- a/foo
++++ b/foo
+@@ -1 +1 @@
+-Fri Jun 9 00:44:04 PDT 2006
++Fri Jun 9 00:44:13 PDT 2006
+--
+1.4.0.g6f2b
+
diff --git a/t/t5100/patch0004 b/t/t5100/patch0004
new file mode 100644
index 0000000..196458e
--- /dev/null
+++ b/t/t5100/patch0004
@@ -0,0 +1,93 @@
+diff --git a/connect.c b/connect.c
+--- a/connect.c
++++ b/connect.c
+@@ -96,42 +96,57 @@ static enum protocol get_protocol(const
+ die("I don't handle protocol '%s'", name);
+ }
+
+-static void lookup_host(const char *host, struct sockaddr *in)
+-{
+- struct addrinfo *res;
+- int ret;
+-
+- ret = getaddrinfo(host, NULL, NULL, &res);
+- if (ret)
+- die("Unable to look up %s (%s)", host, gai_strerror(ret));
+- *in = *res->ai_addr;
+- freeaddrinfo(res);
+-}
++#define STR_(s) # s
++#define STR(s) STR_(s)
+
+ static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path)
+ {
+- struct sockaddr addr;
+- int port = DEFAULT_GIT_PORT, sockfd;
+- char *colon;
+-
+- colon = strchr(host, ':');
+- if (colon) {
+- char *end;
+- unsigned long n = strtoul(colon+1, &end, 0);
+- if (colon[1] && !*end) {
+- *colon = 0;
+- port = n;
++ int sockfd = -1;
++ char *colon, *end;
++ char *port = STR(DEFAULT_GIT_PORT);
++ struct addrinfo hints, *ai0, *ai;
++ int gai;
++
++ if (host[0] == '[') {
++ end = strchr(host + 1, ']');
++ if (end) {
++ *end = 0;
++ end++;
++ host++;
++ } else
++ end = host;
++ } else
++ end = host;
++ colon = strchr(end, ':');
++
++ if (colon)
++ port = colon + 1;
++
++ memset(&hints, 0, sizeof(hints));
++ hints.ai_socktype = SOCK_STREAM;
++ hints.ai_protocol = IPPROTO_TCP;
++
++ gai = getaddrinfo(host, port, &hints, &ai);
++ if (gai)
++ die("Unable to look up %s (%s)", host, gai_strerror(gai));
++
++ for (ai0 = ai; ai; ai = ai->ai_next) {
++ sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
++ if (sockfd < 0)
++ continue;
++ if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
++ close(sockfd);
++ sockfd = -1;
++ continue;
+ }
++ break;
+ }
+
+- lookup_host(host, &addr);
+- ((struct sockaddr_in *)&addr)->sin_port = htons(port);
++ freeaddrinfo(ai0);
+
+- sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_IP);
+ if (sockfd < 0)
+ die("unable to create socket (%s)", strerror(errno));
+- if (connect(sockfd, (void *)&addr, sizeof(addr)) < 0)
+- die("unable to connect (%s)", strerror(errno));
++
+ fd[0] = sockfd;
+ fd[1] = sockfd;
+ packet_write(sockfd, "%s %s\n", prog, path);
+
+--
+YOSHIFUJI Hideaki @ USAGI Project <yoshfuji@linux-ipv6.org>
+GPG-FP : 9022 65EB 1ECF 3AD1 0BDF 80D8 4807 F894 E062 0EEA
+
diff --git a/t/t5100/patch0005 b/t/t5100/patch0005
new file mode 100644
index 0000000..ab7a383
--- /dev/null
+++ b/t/t5100/patch0005
@@ -0,0 +1,69 @@
+---
+
+ Documentation/git-cvsimport-script.txt | 9 ++++++++-
+ git-cvsimport-script | 4 ++--
+ 2 files changed, 10 insertions(+), 3 deletions(-)
+
+50452f9c0c2df1f04d83a26266ba704b13861632
+diff --git a/Documentation/git-cvsimport-script.txt b/Documentation/git-cvsimport-script.txt
+--- a/Documentation/git-cvsimport-script.txt
++++ b/Documentation/git-cvsimport-script.txt
+@@ -29,6 +29,10 @@ OPTIONS
+ currently, only the :local:, :ext: and :pserver: access methods
+ are supported.
+
++-C <target-dir>::
++ The GIT repository to import to. If the directory doesn't
++ exist, it will be created. Default is the current directory.
++
+ -i::
+ Import-only: don't perform a checkout after importing. This option
+ ensures the working directory and cache remain untouched and will
+@@ -44,7 +48,7 @@ OPTIONS
+
+ -p <options-for-cvsps>::
+ Additional options for cvsps.
+- The options '-x' and '-A' are implicit and should not be used here.
++ The options '-u' and '-A' are implicit and should not be used here.
+
+ If you need to pass multiple options, separate them with a comma.
+
+@@ -57,6 +61,9 @@ OPTIONS
+ -h::
+ Print a short usage message and exit.
+
++-z <fuzz>::
++ Pass the timestamp fuzz factor to cvsps.
++
+ OUTPUT
+ ------
+ If '-v' is specified, the script reports what it is doing.
+diff --git a/git-cvsimport-script b/git-cvsimport-script
+--- a/git-cvsimport-script
++++ b/git-cvsimport-script
+@@ -190,7 +190,7 @@ sub conn {
+ $self->{'socketo'}->write("Root $repo\n");
+
+ # Trial and error says that this probably is the minimum set
+- $self->{'socketo'}->write("Valid-responses ok error Valid-requests Mode M Mbinary E F Checked-in Created Updated Merged Removed\n");
++ $self->{'socketo'}->write("Valid-responses ok error Valid-requests Mode M Mbinary E Checked-in Created Updated Merged Removed\n");
+
+ $self->{'socketo'}->write("valid-requests\n");
+ $self->{'socketo'}->flush();
+@@ -691,7 +691,7 @@ while(<CVS>) {
+ unlink($tmpname);
+ my $mode = pmode($cvs->{'mode'});
+ push(@new,[$mode, $sha, $fn]); # may be resurrected!
+- } elsif($state == 9 and /^\s+(\S+):\d(?:\.\d+)+->(\d(?:\.\d+)+)\(DEAD\)\s*$/) {
++ } elsif($state == 9 and /^\s+(\S+):\d+(?:\.\d+)+->(\d+(?:\.\d+)+)\(DEAD\)\s*$/) {
+ my $fn = $1;
+ $fn =~ s#^/+##;
+ push(@old,$fn);
+
+--
+David Kågedal
+-
+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
+
diff --git a/t/t5100/patch0006 b/t/t5100/patch0006
new file mode 100644
index 0000000..02c9774
--- /dev/null
+++ b/t/t5100/patch0006
@@ -0,0 +1,14 @@
+---
+ foo | 2 +-
+ 1 files changed, 1 insertions(+), 1 deletions(-)
+
+diff --git a/foo b/foo
+index 9123cdc..918dcf8 100644
+--- a/foo
++++ b/foo
+@@ -1 +1 @@
+-Fri Jun 9 00:44:04 PDT 2006
++Fri Jun 9 00:44:13 PDT 2006
+--
+1.4.0.g6f2b
+
diff --git a/t/t5100/patch0007 b/t/t5100/patch0007
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/t/t5100/patch0007
diff --git a/t/t5100/patch0008 b/t/t5100/patch0008
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/t/t5100/patch0008
diff --git a/t/t5100/patch0009 b/t/t5100/patch0009
new file mode 100644
index 0000000..65615c3
--- /dev/null
+++ b/t/t5100/patch0009
@@ -0,0 +1,13 @@
+diff --git a/Documentation/diff-format.txt b/Documentation/diff-format.txt
+index b426a14..97756ec 100644
+--- a/Documentation/diff-format.txt
++++ b/Documentation/diff-format.txt
+@@ -81,7 +81,7 @@ The "diff" formatting options can be customized via the
+ environment variable 'GIT_DIFF_OPTS'. For example, if you
+ prefer context diff:
+
+- GIT_DIFF_OPTS=-c git-diff-index -p $(cat .git/HEAD)
++ GIT_DIFF_OPTS=-c git-diff-index -p HEAD
+
+
+ 2. When the environment variable 'GIT_EXTERNAL_DIFF' is set, the
diff --git a/t/t5100/patch0010 b/t/t5100/patch0010
new file mode 100644
index 0000000..436821c
--- /dev/null
+++ b/t/t5100/patch0010
@@ -0,0 +1,20 @@
+---
+ builtin-mailinfo.c | 2 +-
+ 1 files changed, 1 insertions(+), 1 deletions(-)
+
+diff --git a/builtin-mailinfo.c b/builtin-mailinfo.c
+index 962aa34..2d1520f 100644
+--- a/builtin-mailinfo.c
++++ b/builtin-mailinfo.c
+@@ -334,7 +334,7 @@ static int check_header(char *line, unsigned linesize, char **hdr_data, int over
+ return 1;
+ if (!memcmp("[PATCH]", line, 7) && isspace(line[7])) {
+ for (i = 0; header[i]; i++) {
+- if (!memcmp("Subject: ", header[i], 9)) {
++ if (!memcmp("Subject", header[i], 7)) {
+ if (! handle_header(line, hdr_data[i], 0)) {
+ return 1;
+ }
+--
+1.5.6.2.455.g1efb2
+
diff --git a/t/t5100/patch0011 b/t/t5100/patch0011
new file mode 100644
index 0000000..0988713
--- /dev/null
+++ b/t/t5100/patch0011
@@ -0,0 +1,22 @@
+---
+ builtin-mailinfo.c | 4 ++--
+
+diff --git a/builtin-mailinfo.c b/builtin-mailinfo.c
+index 3e5fe51..aabfe5c 100644
+--- a/builtin-mailinfo.c
++++ b/builtin-mailinfo.c
+@@ -758,8 +758,8 @@ static void handle_body(void)
+ /* process any boundary lines */
+ if (*content_top && is_multipart_boundary(&line)) {
+ /* flush any leftover */
+- if (line.len)
+- handle_filter(&line);
++ if (prev.len)
++ handle_filter(&prev);
+
+ if (!handle_boundary())
+ goto handle_body_out;
+--
+1.6.0.rc2
+
+
diff --git a/t/t5100/patch0012 b/t/t5100/patch0012
new file mode 100644
index 0000000..36a0b68
--- /dev/null
+++ b/t/t5100/patch0012
@@ -0,0 +1,30 @@
+---
+ howto/build_navy.txt | 6 +++---
+ 1 files changed, 3 insertions(+), 3 deletions(-)
+
+diff --git a/howto/build_navy.txt b/howto/build_navy.txt
+index 3fd3afb..0ee807e 100644
+--- a/howto/build_navy.txt
++++ b/howto/build_navy.txt
+@@ -119,8 +119,8 @@
+ - libxv-dev
+ - libusplash-dev
+ - latex-make
+- - textlive-lang-cyrillic
+- - textlive-latex-extra
++ - texlive-lang-cyrillic
++ - texlive-latex-extra
+ - dia
+ - python-pyrex
+ - libtool
+@@ -128,7 +128,7 @@
+ - sox
+ - cython
+ - imagemagick
+- - docutils
++ - python-docutils
+
+ #. на машине dinar: добавить Ñвой открытый ssh-ключ в authorized_keys2 Ð¿Ð¾Ð»ÑŒÐ·Ð¾Ð²Ð°Ñ‚ÐµÐ»Ñ ddev
+ #. на Ñвоей машине: отредактировать /etc/sudoers (команда ``visudo``) примерно Ñледующим образом::
+--
+1.5.6.5
diff --git a/t/t5100/patch0012--message-id b/t/t5100/patch0012--message-id
new file mode 100644
index 0000000..36a0b68
--- /dev/null
+++ b/t/t5100/patch0012--message-id
@@ -0,0 +1,30 @@
+---
+ howto/build_navy.txt | 6 +++---
+ 1 files changed, 3 insertions(+), 3 deletions(-)
+
+diff --git a/howto/build_navy.txt b/howto/build_navy.txt
+index 3fd3afb..0ee807e 100644
+--- a/howto/build_navy.txt
++++ b/howto/build_navy.txt
+@@ -119,8 +119,8 @@
+ - libxv-dev
+ - libusplash-dev
+ - latex-make
+- - textlive-lang-cyrillic
+- - textlive-latex-extra
++ - texlive-lang-cyrillic
++ - texlive-latex-extra
+ - dia
+ - python-pyrex
+ - libtool
+@@ -128,7 +128,7 @@
+ - sox
+ - cython
+ - imagemagick
+- - docutils
++ - python-docutils
+
+ #. на машине dinar: добавить Ñвой открытый ssh-ключ в authorized_keys2 Ð¿Ð¾Ð»ÑŒÐ·Ð¾Ð²Ð°Ñ‚ÐµÐ»Ñ ddev
+ #. на Ñвоей машине: отредактировать /etc/sudoers (команда ``visudo``) примерно Ñледующим образом::
+--
+1.5.6.5
diff --git a/t/t5100/patch0013 b/t/t5100/patch0013
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/t/t5100/patch0013
diff --git a/t/t5100/patch0014 b/t/t5100/patch0014
new file mode 100644
index 0000000..3f3825f
--- /dev/null
+++ b/t/t5100/patch0014
@@ -0,0 +1,64 @@
+---
+ builtin-mailinfo.c | 37 ++++++++++++++++++++++++++++++++++++-
+ 1 files changed, 36 insertions(+), 1 deletions(-)
+
+diff --git a/builtin-mailinfo.c b/builtin-mailinfo.c
+index b0b5d8f..461c47e 100644
+--- a/builtin-mailinfo.c
++++ b/builtin-mailinfo.c
+@@ -712,6 +712,34 @@ static inline int patchbreak(const struct strbuf *line)
+ return 0;
+ }
+
++static int scissors(const struct strbuf *line)
++{
++ size_t i, len = line->len;
++ int scissors_dashes_seen = 0;
++ const char *buf = line->buf;
++
++ for (i = 0; i < len; i++) {
++ if (isspace(buf[i]))
++ continue;
++ if (buf[i] == '-') {
++ scissors_dashes_seen |= 02;
++ continue;
++ }
++ if (i + 1 < len && !memcmp(buf + i, ">8", 2)) {
++ scissors_dashes_seen |= 01;
++ i++;
++ continue;
++ }
++ if (i + 7 < len && !memcmp(buf + i, "cut here", 8)) {
++ i += 7;
++ continue;
++ }
++ /* everything else --- not scissors */
++ break;
++ }
++ return scissors_dashes_seen == 03;
++}
++
+ static int handle_commit_msg(struct strbuf *line)
+ {
+ static int still_looking = 1;
+@@ -723,10 +751,17 @@ static int handle_commit_msg(struct strbuf *line)
+ strbuf_ltrim(line);
+ if (!line->len)
+ return 0;
+- if ((still_looking = check_header(line, s_hdr_data, 0)) != 0)
++ still_looking = check_header(line, s_hdr_data, 0);
++ if (still_looking)
+ return 0;
+ }
+
++ if (scissors(line)) {
++ fseek(cmitmsg, 0L, SEEK_SET);
++ still_looking = 1;
++ return 0;
++ }
++
+ /* normalize the log message to UTF-8. */
+ if (metainfo_charset)
+ convert_to_utf8(line, charset.buf);
+--
+1.6.4.1
diff --git a/t/t5100/patch0014--scissors b/t/t5100/patch0014--scissors
new file mode 100644
index 0000000..3f3825f
--- /dev/null
+++ b/t/t5100/patch0014--scissors
@@ -0,0 +1,64 @@
+---
+ builtin-mailinfo.c | 37 ++++++++++++++++++++++++++++++++++++-
+ 1 files changed, 36 insertions(+), 1 deletions(-)
+
+diff --git a/builtin-mailinfo.c b/builtin-mailinfo.c
+index b0b5d8f..461c47e 100644
+--- a/builtin-mailinfo.c
++++ b/builtin-mailinfo.c
+@@ -712,6 +712,34 @@ static inline int patchbreak(const struct strbuf *line)
+ return 0;
+ }
+
++static int scissors(const struct strbuf *line)
++{
++ size_t i, len = line->len;
++ int scissors_dashes_seen = 0;
++ const char *buf = line->buf;
++
++ for (i = 0; i < len; i++) {
++ if (isspace(buf[i]))
++ continue;
++ if (buf[i] == '-') {
++ scissors_dashes_seen |= 02;
++ continue;
++ }
++ if (i + 1 < len && !memcmp(buf + i, ">8", 2)) {
++ scissors_dashes_seen |= 01;
++ i++;
++ continue;
++ }
++ if (i + 7 < len && !memcmp(buf + i, "cut here", 8)) {
++ i += 7;
++ continue;
++ }
++ /* everything else --- not scissors */
++ break;
++ }
++ return scissors_dashes_seen == 03;
++}
++
+ static int handle_commit_msg(struct strbuf *line)
+ {
+ static int still_looking = 1;
+@@ -723,10 +751,17 @@ static int handle_commit_msg(struct strbuf *line)
+ strbuf_ltrim(line);
+ if (!line->len)
+ return 0;
+- if ((still_looking = check_header(line, s_hdr_data, 0)) != 0)
++ still_looking = check_header(line, s_hdr_data, 0);
++ if (still_looking)
+ return 0;
+ }
+
++ if (scissors(line)) {
++ fseek(cmitmsg, 0L, SEEK_SET);
++ still_looking = 1;
++ return 0;
++ }
++
+ /* normalize the log message to UTF-8. */
+ if (metainfo_charset)
+ convert_to_utf8(line, charset.buf);
+--
+1.6.4.1
diff --git a/t/t5100/patch0015 b/t/t5100/patch0015
new file mode 100644
index 0000000..ad64848
--- /dev/null
+++ b/t/t5100/patch0015
@@ -0,0 +1,8 @@
+---
+diff --git a/foo b/foo
+index e69de29..d95f3ad 100644
+--- a/foo
++++ b/foo
+@@ -0,0 +1 @@
++content
+
diff --git a/t/t5100/patch0015--no-inbody-headers b/t/t5100/patch0015--no-inbody-headers
new file mode 100644
index 0000000..ad64848
--- /dev/null
+++ b/t/t5100/patch0015--no-inbody-headers
@@ -0,0 +1,8 @@
+---
+diff --git a/foo b/foo
+index e69de29..d95f3ad 100644
+--- a/foo
++++ b/foo
+@@ -0,0 +1 @@
++content
+
diff --git a/t/t5100/patch0016 b/t/t5100/patch0016
new file mode 100644
index 0000000..ad64848
--- /dev/null
+++ b/t/t5100/patch0016
@@ -0,0 +1,8 @@
+---
+diff --git a/foo b/foo
+index e69de29..d95f3ad 100644
+--- a/foo
++++ b/foo
+@@ -0,0 +1 @@
++content
+
diff --git a/t/t5100/patch0016--no-inbody-headers b/t/t5100/patch0016--no-inbody-headers
new file mode 100644
index 0000000..ad64848
--- /dev/null
+++ b/t/t5100/patch0016--no-inbody-headers
@@ -0,0 +1,8 @@
+---
+diff --git a/foo b/foo
+index e69de29..d95f3ad 100644
+--- a/foo
++++ b/foo
+@@ -0,0 +1 @@
++content
+
diff --git a/t/t5100/patch0017 b/t/t5100/patch0017
new file mode 100644
index 0000000..35cf84c
--- /dev/null
+++ b/t/t5100/patch0017
@@ -0,0 +1,6 @@
+diff --git a/foo b/foo
+index e69de29..d95f3ad 100644
+--- a/foo
++++ b/foo
+@@ -0,0 +1 @@
++New content
diff --git a/t/t5100/patch0018 b/t/t5100/patch0018
new file mode 100644
index 0000000..789df6d
--- /dev/null
+++ b/t/t5100/patch0018
@@ -0,0 +1,6 @@
+diff --git a/foo b/foo
+index e69de29..d95f3ad 100644
+--- a/foo
++++ b/foo
+@@ -0,0 +1 @@
++content
diff --git a/t/t5100/patch0018--no-inbody-headers b/t/t5100/patch0018--no-inbody-headers
new file mode 100644
index 0000000..789df6d
--- /dev/null
+++ b/t/t5100/patch0018--no-inbody-headers
@@ -0,0 +1,6 @@
+diff --git a/foo b/foo
+index e69de29..d95f3ad 100644
+--- a/foo
++++ b/foo
+@@ -0,0 +1 @@
++content
diff --git a/t/t5100/quoted-cr-info b/t/t5100/quoted-cr-info
new file mode 100644
index 0000000..dab2228
--- /dev/null
+++ b/t/t5100/quoted-cr-info
@@ -0,0 +1,5 @@
+Author: A U Thor
+Email: mail@example.com
+Subject: sample
+Date: Mon, 3 Aug 2020 22:40:55 +0700
+
diff --git a/t/t5100/quoted-cr-msg b/t/t5100/quoted-cr-msg
new file mode 100644
index 0000000..89b05a0
--- /dev/null
+++ b/t/t5100/quoted-cr-msg
@@ -0,0 +1,2 @@
+On different distro, %%pytest is suffixed with different patterns.%%
+%%
diff --git a/t/t5100/quoted-cr-patch b/t/t5100/quoted-cr-patch
new file mode 100644
index 0000000..65b13ee
--- /dev/null
+++ b/t/t5100/quoted-cr-patch
@@ -0,0 +1,22 @@
+---%%
+ configure | 2 +-%%
+ 1 file changed, 1 insertion(+), 1 deletion(-)%%
+%%
+diff --git a/configure b/configure%%
+index db3538b3..f7c1c095 100755%%
+--- a/configure%%
++++ b/configure%%
+@@ -814,7 +814,7 @@ if [ $have_python3 -eq 1 ]; then%%
+ printf "%%Checking for python3 pytest (>= 3.0)... "%%
+ conf=$(mktemp)%%
+ printf "[pytest]\nminversion=3.0\n" > $conf%%
+- if pytest-3 -c $conf --version >/dev/null 2>&1; then%%
++ if "$python" -m pytest -c $conf --version >/dev/null 2>&1; then%%
+ printf "Yes.\n"%%
+ have_python3_pytest=1%%
+ else%%
+-- %%
+2.28.0%%
+_______________________________________________
+example mailing list -- list@example.org
+To unsubscribe send an email to list-leave@example.org
diff --git a/t/t5100/quoted-cr.mbox b/t/t5100/quoted-cr.mbox
new file mode 100644
index 0000000..a529d4d
--- /dev/null
+++ b/t/t5100/quoted-cr.mbox
@@ -0,0 +1,47 @@
+From nobody Mon Sep 17 00:00:00 2001
+From: A U Thor <mail@example.com>
+To: list@example.org
+Subject: [PATCH v2] sample
+Date: Mon, 3 Aug 2020 22:40:55 +0700
+Message-ID: <msg-id@example.com>
+Content-Type: text/plain; charset="utf-8"
+Content-Transfer-Encoding: base64
+
+T24gZGlmZmVyZW50IGRpc3RybywgDXB5dGVzdCBpcyBzdWZmaXhlZCB3aXRoIGRpZmZlcmVudCBw
+YXR0ZXJucy4KCi0tLQogY29uZmlndXJlIHwgMiArLQogMSBmaWxlIGNoYW5nZWQsIDEgaW5zZXJ0
+aW9uKCspLCAxIGRlbGV0aW9uKC0pCgpkaWZmIC0tZ2l0IGEvY29uZmlndXJlIGIvY29uZmlndXJl
+CmluZGV4IGRiMzUzOGIzLi5mN2MxYzA5NSAxMDA3NTUKLS0tIGEvY29uZmlndXJlCisrKyBiL2Nv
+bmZpZ3VyZQpAQCAtODE0LDcgKzgxNCw3IEBAIGlmIFsgJGhhdmVfcHl0aG9uMyAtZXEgMSBdOyB0
+aGVuCiAgICAgcHJpbnRmICINQ2hlY2tpbmcgZm9yIHB5dGhvbjMgcHl0ZXN0ICg+PSAzLjApLi4u
+ICIKICAgICBjb25mPSQobWt0ZW1wKQogICAgIHByaW50ZiAiW3B5dGVzdF1cbm1pbnZlcnNpb249
+My4wXG4iID4gJGNvbmYKLSAgICBpZiBweXRlc3QtMyAtYyAkY29uZiAtLXZlcnNpb24gPi9kZXYv
+bnVsbCAyPiYxOyB0aGVuCisgICAgaWYgIiRweXRob24iIC1tIHB5dGVzdCAtYyAkY29uZiAtLXZl
+cnNpb24gPi9kZXYvbnVsbCAyPiYxOyB0aGVuCiAgICAgICAgIHByaW50ZiAiWWVzLlxuIgogICAg
+ICAgICBoYXZlX3B5dGhvbjNfcHl0ZXN0PTEKICAgICBlbHNlCi0tIAoyLjI4LjAKX19fX19fX19f
+X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KZXhhbXBsZSBtYWlsaW5nIGxp
+c3QgLS0gbGlzdEBleGFtcGxlLm9yZwpUbyB1bnN1YnNjcmliZSBzZW5kIGFuIGVtYWlsIHRvIGxp
+c3QtbGVhdmVAZXhhbXBsZS5vcmcK
+
+From nobody Mon Sep 17 00:00:00 2001
+From: A U Thor <mail@example.com>
+To: list@example.org
+Subject: [PATCH v2] sample
+Date: Mon, 3 Aug 2020 22:40:55 +0700
+Message-ID: <msg-id2@example.com>
+Content-Type: text/plain; charset="utf-8"
+Content-Transfer-Encoding: base64
+
+T24gZGlmZmVyZW50IGRpc3RybywgDXB5dGVzdCBpcyBzdWZmaXhlZCB3aXRoIGRpZmZlcmVudCBw
+YXR0ZXJucy4NCg0KLS0tDQogY29uZmlndXJlIHwgMiArLQ0KIDEgZmlsZSBjaGFuZ2VkLCAxIGlu
+c2VydGlvbigrKSwgMSBkZWxldGlvbigtKQ0KDQpkaWZmIC0tZ2l0IGEvY29uZmlndXJlIGIvY29u
+ZmlndXJlDQppbmRleCBkYjM1MzhiMy4uZjdjMWMwOTUgMTAwNzU1DQotLS0gYS9jb25maWd1cmUN
+CisrKyBiL2NvbmZpZ3VyZQ0KQEAgLTgxNCw3ICs4MTQsNyBAQCBpZiBbICRoYXZlX3B5dGhvbjMg
+LWVxIDEgXTsgdGhlbg0KICAgICBwcmludGYgIg1DaGVja2luZyBmb3IgcHl0aG9uMyBweXRlc3Qg
+KD49IDMuMCkuLi4gIg0KICAgICBjb25mPSQobWt0ZW1wKQ0KICAgICBwcmludGYgIltweXRlc3Rd
+XG5taW52ZXJzaW9uPTMuMFxuIiA+ICRjb25mDQotICAgIGlmIHB5dGVzdC0zIC1jICRjb25mIC0t
+dmVyc2lvbiA+L2Rldi9udWxsIDI+JjE7IHRoZW4NCisgICAgaWYgIiRweXRob24iIC1tIHB5dGVz
+dCAtYyAkY29uZiAtLXZlcnNpb24gPi9kZXYvbnVsbCAyPiYxOyB0aGVuDQogICAgICAgICBwcmlu
+dGYgIlllcy5cbiINCiAgICAgICAgIGhhdmVfcHl0aG9uM19weXRlc3Q9MQ0KICAgICBlbHNlDQot
+LSANCjIuMjguMA0KX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19f
+X18KZXhhbXBsZSBtYWlsaW5nIGxpc3QgLS0gbGlzdEBleGFtcGxlLm9yZwpUbyB1bnN1YnNjcmli
+ZSBzZW5kIGFuIGVtYWlsIHRvIGxpc3QtbGVhdmVAZXhhbXBsZS5vcmcK
diff --git a/t/t5100/quoted-from.expect b/t/t5100/quoted-from.expect
new file mode 100644
index 0000000..8c9d48c
--- /dev/null
+++ b/t/t5100/quoted-from.expect
@@ -0,0 +1,3 @@
+>From the depths of history, we are stuck with the
+flaky mbox format.
+
diff --git a/t/t5100/quoted-from.in b/t/t5100/quoted-from.in
new file mode 100644
index 0000000..847e1c4
--- /dev/null
+++ b/t/t5100/quoted-from.in
@@ -0,0 +1,10 @@
+From 1234567890123456789012345678901234567890 Mon Sep 17 00:00:00 2001
+From: Author Name <somebody@example.com>
+Date: Sun, 25 May 2008 00:38:18 -0700
+Subject: [PATCH] testing quoted >From
+
+>From the depths of history, we are stuck with the
+flaky mbox format.
+
+---
+patch
diff --git a/t/t5100/quoted-string.expect b/t/t5100/quoted-string.expect
new file mode 100644
index 0000000..cab1bce
--- /dev/null
+++ b/t/t5100/quoted-string.expect
@@ -0,0 +1,5 @@
+Author: Author "The Author" Name
+Email: somebody@example.com
+Subject: testing quoted-pair
+Date: Sun, 25 May 2008 00:38:18 -0700
+
diff --git a/t/t5100/quoted-string.in b/t/t5100/quoted-string.in
new file mode 100644
index 0000000..e2e627a
--- /dev/null
+++ b/t/t5100/quoted-string.in
@@ -0,0 +1,9 @@
+From 1234567890123456789012345678901234567890 Mon Sep 17 00:00:00 2001
+From: "Author \"The Author\" Name" <somebody@example.com>
+Date: Sun, 25 May 2008 00:38:18 -0700
+Subject: [PATCH] testing quoted-pair
+
+
+
+---
+patch
diff --git a/t/t5100/rfc2047-info-0001 b/t/t5100/rfc2047-info-0001
new file mode 100644
index 0000000..0a383b0
--- /dev/null
+++ b/t/t5100/rfc2047-info-0001
@@ -0,0 +1,4 @@
+Author: Keith Moore
+Email: moore@cs.utk.edu
+Subject: If you can read this you understand the example.
+
diff --git a/t/t5100/rfc2047-info-0002 b/t/t5100/rfc2047-info-0002
new file mode 100644
index 0000000..881be75
--- /dev/null
+++ b/t/t5100/rfc2047-info-0002
@@ -0,0 +1,4 @@
+Author: Olle Järnefors
+Email: ojarnef@admin.kth.se
+Subject: Time for ISO 10646?
+
diff --git a/t/t5100/rfc2047-info-0003 b/t/t5100/rfc2047-info-0003
new file mode 100644
index 0000000..d0f7891
--- /dev/null
+++ b/t/t5100/rfc2047-info-0003
@@ -0,0 +1,4 @@
+Author: Patrik Fältström
+Email: paf@nada.kth.se
+Subject: RFC-HDR care and feeding
+
diff --git a/t/t5100/rfc2047-info-0004 b/t/t5100/rfc2047-info-0004
new file mode 100644
index 0000000..f67a90a
--- /dev/null
+++ b/t/t5100/rfc2047-info-0004
@@ -0,0 +1,4 @@
+Author: Nathaniel Borenstein (×ולש ןב ילטפנ)
+Email: nsb@thumper.bellcore.com
+Subject: Test of new header generator
+
diff --git a/t/t5100/rfc2047-info-0005 b/t/t5100/rfc2047-info-0005
new file mode 100644
index 0000000..c27be3b
--- /dev/null
+++ b/t/t5100/rfc2047-info-0005
@@ -0,0 +1,2 @@
+Subject: (a)
+
diff --git a/t/t5100/rfc2047-info-0006 b/t/t5100/rfc2047-info-0006
new file mode 100644
index 0000000..9dad474
--- /dev/null
+++ b/t/t5100/rfc2047-info-0006
@@ -0,0 +1,2 @@
+Subject: (a b)
+
diff --git a/t/t5100/rfc2047-info-0007 b/t/t5100/rfc2047-info-0007
new file mode 100644
index 0000000..294f195
--- /dev/null
+++ b/t/t5100/rfc2047-info-0007
@@ -0,0 +1,2 @@
+Subject: (ab)
+
diff --git a/t/t5100/rfc2047-info-0008 b/t/t5100/rfc2047-info-0008
new file mode 100644
index 0000000..294f195
--- /dev/null
+++ b/t/t5100/rfc2047-info-0008
@@ -0,0 +1,2 @@
+Subject: (ab)
+
diff --git a/t/t5100/rfc2047-info-0009 b/t/t5100/rfc2047-info-0009
new file mode 100644
index 0000000..294f195
--- /dev/null
+++ b/t/t5100/rfc2047-info-0009
@@ -0,0 +1,2 @@
+Subject: (ab)
+
diff --git a/t/t5100/rfc2047-info-0010 b/t/t5100/rfc2047-info-0010
new file mode 100644
index 0000000..9dad474
--- /dev/null
+++ b/t/t5100/rfc2047-info-0010
@@ -0,0 +1,2 @@
+Subject: (a b)
+
diff --git a/t/t5100/rfc2047-info-0011 b/t/t5100/rfc2047-info-0011
new file mode 100644
index 0000000..9dad474
--- /dev/null
+++ b/t/t5100/rfc2047-info-0011
@@ -0,0 +1,2 @@
+Subject: (a b)
+
diff --git a/t/t5100/rfc2047-samples.mbox b/t/t5100/rfc2047-samples.mbox
new file mode 100644
index 0000000..1fc2248
--- /dev/null
+++ b/t/t5100/rfc2047-samples.mbox
@@ -0,0 +1,48 @@
+From nobody Mon Sep 17 00:00:00 2001
+From: =?US-ASCII?Q?Keith_Moore?= <moore@cs.utk.edu>
+To: =?ISO8859-1?Q?Keld_J=F8rn_Simonsen?= <keld@dkuug.dk>
+CC: =?ISO8859-1?Q?Andr=E9?= Pirard <PIRARD@vm1.ulg.ac.be>
+Subject: =?ISO8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?=
+ =?ISO8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?=
+
+From nobody Mon Sep 17 00:00:00 2001
+From: =?ISO8859-1?Q?Olle_J=E4rnefors?= <ojarnef@admin.kth.se>
+To: ietf-822@dimacs.rutgers.edu, ojarnef@admin.kth.se
+Subject: Time for ISO 10646?
+
+From nobody Mon Sep 17 00:00:00 2001
+To: Dave Crocker <dcrocker@mordor.stanford.edu>
+Cc: ietf-822@dimacs.rutgers.edu, paf@comsol.se
+From: =?ISO8859-1?Q?Patrik_F=E4ltstr=F6m?= <paf@nada.kth.se>
+Subject: Re: RFC-HDR care and feeding
+
+From nobody Mon Sep 17 00:00:00 2001
+From: Nathaniel Borenstein <nsb@thumper.bellcore.com>
+ (=?ISO8859-8?b?7eXs+SDv4SDp7Oj08A==?=)
+To: Greg Vaudreuil <gvaudre@NRI.Reston.VA.US>, Ned Freed
+ <ned@innosoft.com>, Keith Moore <moore@cs.utk.edu>
+Subject: Test of new header generator
+MIME-Version: 1.0
+Content-type: text/plain; charset=ISO8859-1
+
+From nobody Mon Sep 17 00:00:00 2001
+Subject: (=?ISO8859-1?Q?a?=)
+
+From nobody Mon Sep 17 00:00:00 2001
+Subject: (=?ISO8859-1?Q?a?= b)
+
+From nobody Mon Sep 17 00:00:00 2001
+Subject: (=?ISO8859-1?Q?a?= =?ISO8859-1?Q?b?=)
+
+From nobody Mon Sep 17 00:00:00 2001
+Subject: (=?ISO8859-1?Q?a?= =?ISO8859-1?Q?b?=)
+
+From nobody Mon Sep 17 00:00:00 2001
+Subject: (=?ISO8859-1?Q?a?=
+ =?ISO8859-1?Q?b?=)
+
+From nobody Mon Sep 17 00:00:00 2001
+Subject: (=?ISO8859-1?Q?a_b?=)
+
+From nobody Mon Sep 17 00:00:00 2001
+Subject: (=?ISO8859-1?Q?a?= =?ISO8859-2?Q?_b?=)
diff --git a/t/t5100/sample.mbox b/t/t5100/sample.mbox
new file mode 100644
index 0000000..4a54ee5
--- /dev/null
+++ b/t/t5100/sample.mbox
@@ -0,0 +1,720 @@
+
+
+
+From nobody Mon Sep 17 00:00:00 2001
+From: A (zzz)
+ U
+ Thor
+ <a.u.thor@example.com> (Comment)
+Date: Fri, 9 Jun 2006 00:44:16 -0700
+Subject: [PATCH] a commit.
+
+Here is a patch from A U Thor.
+
+---
+ foo | 2 +-
+ 1 files changed, 1 insertions(+), 1 deletions(-)
+
+diff --git a/foo b/foo
+index 9123cdc..918dcf8 100644
+--- a/foo
++++ b/foo
+@@ -1 +1 @@
+-Fri Jun 9 00:44:04 PDT 2006
++Fri Jun 9 00:44:13 PDT 2006
+--
+1.4.0.g6f2b
+
+From nobody Mon Sep 17 00:00:00 2001
+From: A U Thor <a.u.thor@example.com>
+Date: Fri, 9 Jun 2006 00:44:16 -0700
+Subject: [PATCH] another patch
+
+Here is a patch from A U Thor. This addresses the issue raised in the
+message:
+
+From: Nit Picker <nit.picker@example.net>
+Subject: foo is too old
+Message-ID: <nitpicker.12121212@example.net>
+
+Hopefully this would fix the problem stated there.
+
+
+I have included an extra blank line above, but it does not have to be
+stripped away here, along with the
+whitespaces at the end of the above line. They are expected to be squashed
+when the message is made into a commit log by stripspace,
+Also, there are three blank lines after this paragraph,
+two truly blank and another full of spaces in between.
+
+
+
+Hope this helps.
+
+---
+ foo | 2 +-
+ 1 files changed, 1 insertions(+), 1 deletions(-)
+
+diff --git a/foo b/foo
+index 9123cdc..918dcf8 100644
+--- a/foo
++++ b/foo
+@@ -1 +1 @@
+-Fri Jun 9 00:44:04 PDT 2006
++Fri Jun 9 00:44:13 PDT 2006
+--
+1.4.0.g6f2b
+
+From nobody Mon Sep 17 00:00:00 2001
+From: Junio C Hamano <junio@kernel.org>
+Date: Fri, 9 Jun 2006 00:44:16 -0700
+Subject: re: [PATCH] another patch
+
+From: A U Thor <a.u.thor@example.com>
+Subject: [PATCH] third patch
+
+Here is a patch from A U Thor. This addresses the issue raised in the
+message:
+
+From: Nit Picker <nit.picker@example.net>
+Subject: foo is too old
+Message-ID: <nitpicker.12121212@example.net>
+
+Hopefully this would fix the problem stated there.
+
+---
+ foo | 2 +-
+ 1 files changed, 1 insertions(+), 1 deletions(-)
+
+diff --git a/foo b/foo
+index 9123cdc..918dcf8 100644
+--- a/foo
++++ b/foo
+@@ -1 +1 @@
+-Fri Jun 9 00:44:04 PDT 2006
++Fri Jun 9 00:44:13 PDT 2006
+--
+1.4.0.g6f2b
+
+From nobody Sat Aug 27 23:07:49 2005
+Path: news.gmane.org!not-for-mail
+Message-ID: <20050721.091036.01119516.yoshfuji@linux-ipv6.org>
+From: YOSHIFUJI Hideaki / =?ISO-2022-JP?B?GyRCNUhGIzFRTEAbKEI=?=
+ <yoshfuji@linux-ipv6.org>
+Newsgroups: gmane.comp.version-control.git
+Subject: [PATCH 1/2] GIT: Try all addresses for given remote name
+Date: Thu, 21 Jul 2005 09:10:36 -0400 (EDT)
+Lines: 99
+Organization: USAGI/WIDE Project
+Approved: news@gmane.org
+NNTP-Posting-Host: main.gmane.org
+Mime-Version: 1.0
+Content-Type: Text/Plain; charset=us-ascii
+Content-Transfer-Encoding: 7bit
+X-Trace: sea.gmane.org 1121951434 29350 80.91.229.2 (21 Jul 2005 13:10:34 GMT)
+X-Complaints-To: usenet@sea.gmane.org
+NNTP-Posting-Date: Thu, 21 Jul 2005 13:10:34 +0000 (UTC)
+
+Hello.
+
+Try all addresses for given remote name until it succeeds.
+Also supports IPv6.
+
+Signed-of-by: Hideaki YOSHIFUJI <yoshfuji@linux-ipv6.org>
+
+diff --git a/connect.c b/connect.c
+--- a/connect.c
++++ b/connect.c
+@@ -96,42 +96,57 @@ static enum protocol get_protocol(const
+ die("I don't handle protocol '%s'", name);
+ }
+
+-static void lookup_host(const char *host, struct sockaddr *in)
+-{
+- struct addrinfo *res;
+- int ret;
+-
+- ret = getaddrinfo(host, NULL, NULL, &res);
+- if (ret)
+- die("Unable to look up %s (%s)", host, gai_strerror(ret));
+- *in = *res->ai_addr;
+- freeaddrinfo(res);
+-}
++#define STR_(s) # s
++#define STR(s) STR_(s)
+
+ static int git_tcp_connect(int fd[2], const char *prog, char *host, char *path)
+ {
+- struct sockaddr addr;
+- int port = DEFAULT_GIT_PORT, sockfd;
+- char *colon;
+-
+- colon = strchr(host, ':');
+- if (colon) {
+- char *end;
+- unsigned long n = strtoul(colon+1, &end, 0);
+- if (colon[1] && !*end) {
+- *colon = 0;
+- port = n;
++ int sockfd = -1;
++ char *colon, *end;
++ char *port = STR(DEFAULT_GIT_PORT);
++ struct addrinfo hints, *ai0, *ai;
++ int gai;
++
++ if (host[0] == '[') {
++ end = strchr(host + 1, ']');
++ if (end) {
++ *end = 0;
++ end++;
++ host++;
++ } else
++ end = host;
++ } else
++ end = host;
++ colon = strchr(end, ':');
++
++ if (colon)
++ port = colon + 1;
++
++ memset(&hints, 0, sizeof(hints));
++ hints.ai_socktype = SOCK_STREAM;
++ hints.ai_protocol = IPPROTO_TCP;
++
++ gai = getaddrinfo(host, port, &hints, &ai);
++ if (gai)
++ die("Unable to look up %s (%s)", host, gai_strerror(gai));
++
++ for (ai0 = ai; ai; ai = ai->ai_next) {
++ sockfd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
++ if (sockfd < 0)
++ continue;
++ if (connect(sockfd, ai->ai_addr, ai->ai_addrlen) < 0) {
++ close(sockfd);
++ sockfd = -1;
++ continue;
+ }
++ break;
+ }
+
+- lookup_host(host, &addr);
+- ((struct sockaddr_in *)&addr)->sin_port = htons(port);
++ freeaddrinfo(ai0);
+
+- sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_IP);
+ if (sockfd < 0)
+ die("unable to create socket (%s)", strerror(errno));
+- if (connect(sockfd, (void *)&addr, sizeof(addr)) < 0)
+- die("unable to connect (%s)", strerror(errno));
++
+ fd[0] = sockfd;
+ fd[1] = sockfd;
+ packet_write(sockfd, "%s %s\n", prog, path);
+
+--
+YOSHIFUJI Hideaki @ USAGI Project <yoshfuji@linux-ipv6.org>
+GPG-FP : 9022 65EB 1ECF 3AD1 0BDF 80D8 4807 F894 E062 0EEA
+
+From nobody Sat Aug 27 23:07:49 2005
+Path: news.gmane.org!not-for-mail
+Message-ID: <u5tacjjdpxq.fsf@lysator.liu.se>
+From: =?ISO8859-1?Q?David_K=E5gedal?= <davidk@lysator.liu.se>
+Newsgroups: gmane.comp.version-control.git
+Subject: [PATCH] Fixed two bugs in git-cvsimport-script.
+Date: Mon, 15 Aug 2005 20:18:25 +0200
+Lines: 83
+Approved: news@gmane.org
+NNTP-Posting-Host: main.gmane.org
+Mime-Version: 1.0
+Content-Type: text/plain; charset=ISO8859-1
+Content-Transfer-Encoding: QUOTED-PRINTABLE
+X-Trace: sea.gmane.org 1124130247 31839 80.91.229.2 (15 Aug 2005 18:24:07 GMT)
+X-Complaints-To: usenet@sea.gmane.org
+NNTP-Posting-Date: Mon, 15 Aug 2005 18:24:07 +0000 (UTC)
+Cc: "Junio C. Hamano" <junkio@cox.net>
+Original-X-From: git-owner@vger.kernel.org Mon Aug 15 20:24:05 2005
+
+The git-cvsimport-script had a copule of small bugs that prevented me
+from importing a big CVS repository.
+
+The first was that it didn't handle removed files with a multi-digit
+primary revision number.
+
+The second was that it was asking the CVS server for "F" messages,
+although they were not handled.
+
+I also updated the documentation for that script to correspond to
+actual flags.
+
+Signed-off-by: David K=E5gedal <davidk@lysator.liu.se>
+---
+
+ Documentation/git-cvsimport-script.txt | 9 ++++++++-
+ git-cvsimport-script | 4 ++--
+ 2 files changed, 10 insertions(+), 3 deletions(-)
+
+50452f9c0c2df1f04d83a26266ba704b13861632
+diff --git a/Documentation/git-cvsimport-script.txt b/Documentation/git=
+-cvsimport-script.txt
+--- a/Documentation/git-cvsimport-script.txt
++++ b/Documentation/git-cvsimport-script.txt
+@@ -29,6 +29,10 @@ OPTIONS
+ currently, only the :local:, :ext: and :pserver: access methods=20
+ are supported.
+=20
++-C <target-dir>::
++ The GIT repository to import to. If the directory doesn't
++ exist, it will be created. Default is the current directory.
++
+ -i::
+ Import-only: don't perform a checkout after importing. This option
+ ensures the working directory and cache remain untouched and will
+@@ -44,7 +48,7 @@ OPTIONS
+=20
+ -p <options-for-cvsps>::
+ Additional options for cvsps.
+- The options '-x' and '-A' are implicit and should not be used here.
++ The options '-u' and '-A' are implicit and should not be used here.
+=20
+ If you need to pass multiple options, separate them with a comma.
+=20
+@@ -57,6 +61,9 @@ OPTIONS
+ -h::
+ Print a short usage message and exit.
+=20
++-z <fuzz>::
++ Pass the timestamp fuzz factor to cvsps.
++
+ OUTPUT
+ ------
+ If '-v' is specified, the script reports what it is doing.
+diff --git a/git-cvsimport-script b/git-cvsimport-script
+--- a/git-cvsimport-script
++++ b/git-cvsimport-script
+@@ -190,7 +190,7 @@ sub conn {
+ $self->{'socketo'}->write("Root $repo\n");
+=20
+ # Trial and error says that this probably is the minimum set
+- $self->{'socketo'}->write("Valid-responses ok error Valid-requests Mo=
+de M Mbinary E F Checked-in Created Updated Merged Removed\n");
++ $self->{'socketo'}->write("Valid-responses ok error Valid-requests Mo=
+de M Mbinary E Checked-in Created Updated Merged Removed\n");
+=20
+ $self->{'socketo'}->write("valid-requests\n");
+ $self->{'socketo'}->flush();
+@@ -691,7 +691,7 @@ while(<CVS>) {
+ unlink($tmpname);
+ my $mode =3D pmode($cvs->{'mode'});
+ push(@new,[$mode, $sha, $fn]); # may be resurrected!
+- } elsif($state =3D=3D 9 and /^\s+(\S+):\d(?:\.\d+)+->(\d(?:\.\d+)+)\(=
+DEAD\)\s*$/) {
++ } elsif($state =3D=3D 9 and /^\s+(\S+):\d+(?:\.\d+)+->(\d+(?:\.\d+)+)=
+\(DEAD\)\s*$/) {
+ my $fn =3D $1;
+ $fn =3D~ s#^/+##;
+ push(@old,$fn);
+
+--=20
+David K=E5gedal
+-
+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
+
+From nobody Mon Sep 17 00:00:00 2001
+From: A U Thor <a.u.thor@example.com>
+References: <Pine.LNX.4.640.0001@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0002@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0003@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0004@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0005@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0006@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0007@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0008@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0009@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0010@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0011@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0012@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0013@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0014@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0015@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0016@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0017@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0018@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0019@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0020@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0021@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0022@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0023@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0024@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0025@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0026@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0027@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0028@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0029@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0030@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0031@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0032@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0033@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0034@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0035@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0036@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0037@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0038@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0039@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0040@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0041@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0042@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0043@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0044@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0045@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0046@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0047@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0048@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0049@woody.linux-foundation.org>
+ <Pine.LNX.4.640.0050@woody.linux-foundation.org>
+Date: Fri, 9 Jun 2006 00:44:16 -0700
+Subject: [PATCH] a commit.
+
+Here is a patch from A U Thor.
+
+---
+ foo | 2 +-
+ 1 files changed, 1 insertions(+), 1 deletions(-)
+
+diff --git a/foo b/foo
+index 9123cdc..918dcf8 100644
+--- a/foo
++++ b/foo
+@@ -1 +1 @@
+-Fri Jun 9 00:44:04 PDT 2006
++Fri Jun 9 00:44:13 PDT 2006
+--
+1.4.0.g6f2b
+
+From nobody Mon Sep 17 00:00:00 2001
+From: A U Thor <a.u.thor@example.com>
+Date: Fri, 9 Jun 2006 00:44:16 -0700
+Subject: [PATCH] another patch
+
+Here is an empty patch from A U Thor.
+
+From nobody Mon Sep 17 00:00:00 2001
+From: Junio C Hamano <junio@kernel.org>
+Date: Fri, 9 Jun 2006 00:44:16 -0700
+Subject: re: [PATCH] another patch
+
+From: A U Thor <a.u.thor@example.com>
+Subject: [PATCH] another patch
+>Here is an empty patch from A U Thor.
+
+Hey you forgot the patch!
+
+From nobody Mon Sep 17 00:00:00 2001
+From: A U Thor <a.u.thor@example.com>
+Date: Mon, 17 Sep 2001 00:00:00 +0900
+Mime-Version: 1.0
+Content-Type: Text/Plain; charset=us-ascii
+Content-Transfer-Encoding: Quoted-Printable
+
+=0A=0AFrom: F U Bar <f.u.bar@example.com>
+Subject: [PATCH] updates=0A=0AThis is to fix diff-format documentation.
+
+diff --git a/Documentation/diff-format.txt b/Documentation/diff-format.txt
+index b426a14..97756ec 100644
+--- a/Documentation/diff-format.txt
++++ b/Documentation/diff-format.txt
+@@ -81,7 +81,7 @@ The "diff" formatting options can be customized via the
+ environment variable 'GIT_DIFF_OPTS'. For example, if you
+ prefer context diff:
+=20
+- GIT_DIFF_OPTS=3D-c git-diff-index -p $(cat .git/HEAD)
++ GIT_DIFF_OPTS=3D-c git-diff-index -p HEAD
+=20
+=20
+ 2. When the environment variable 'GIT_EXTERNAL_DIFF' is set, the
+From b9704a518e21158433baa2cc2d591fea687967f6 Mon Sep 17 00:00:00 2001
+From: =?UTF-8?q?Lukas=20Sandstr=C3=B6m?= <lukass@etek.chalmers.se>
+Date: Thu, 10 Jul 2008 23:41:33 +0200
+Subject: Re: discussion that lead to this patch
+MIME-Version: 1.0
+Content-Type: text/plain; charset=UTF-8
+Content-Transfer-Encoding: 8bit
+
+[PATCH] git-mailinfo: Fix getting the subject from the body
+
+"Subject: " isn't in the static array "header", and thus
+memcmp("Subject: ", header[i], 7) will never match.
+
+Signed-off-by: Lukas Sandström <lukass@etek.chalmers.se>
+Signed-off-by: Junio C Hamano <gitster@pobox.com>
+---
+ builtin-mailinfo.c | 2 +-
+ 1 files changed, 1 insertions(+), 1 deletions(-)
+
+diff --git a/builtin-mailinfo.c b/builtin-mailinfo.c
+index 962aa34..2d1520f 100644
+--- a/builtin-mailinfo.c
++++ b/builtin-mailinfo.c
+@@ -334,7 +334,7 @@ static int check_header(char *line, unsigned linesize, char **hdr_data, int over
+ return 1;
+ if (!memcmp("[PATCH]", line, 7) && isspace(line[7])) {
+ for (i = 0; header[i]; i++) {
+- if (!memcmp("Subject: ", header[i], 9)) {
++ if (!memcmp("Subject", header[i], 7)) {
+ if (! handle_header(line, hdr_data[i], 0)) {
+ return 1;
+ }
+--
+1.5.6.2.455.g1efb2
+
+From nobody Fri Aug 8 22:24:03 2008
+Date: Fri, 8 Aug 2008 13:08:37 +0200 (CEST)
+From: A U Thor <a.u.thor@example.com>
+Subject: [PATCH 3/3 v2] Xyzzy
+MIME-Version: 1.0
+Content-Type: multipart/mixed; boundary="=-=-="
+
+--=-=-=
+Content-Type: text/plain; charset=ISO8859-15
+Content-Transfer-Encoding: quoted-printable
+
+Here comes a commit log message, and
+its second line is here.
+---
+ builtin-mailinfo.c | 4 ++--
+
+diff --git a/builtin-mailinfo.c b/builtin-mailinfo.c
+index 3e5fe51..aabfe5c 100644
+--- a/builtin-mailinfo.c
++++ b/builtin-mailinfo.c
+@@ -758,8 +758,8 @@ static void handle_body(void)
+ /* process any boundary lines */
+ if (*content_top && is_multipart_boundary(&line)) {
+ /* flush any leftover */
+- if (line.len)
+- handle_filter(&line);
++ if (prev.len)
++ handle_filter(&prev);
+=20
+ if (!handle_boundary())
+ goto handle_body_out;
+--=20
+1.6.0.rc2
+
+--=-=-=--
+
+From bda@mnsspb.ru Wed Nov 12 17:54:41 2008
+From: Dmitriy Blinov <bda@mnsspb.ru>
+To: navy-patches@dinar.mns.mnsspb.ru
+Date: Wed, 12 Nov 2008 17:54:41 +0300
+Message-ID: <1226501681-24923-1-git-send-email-bda@mnsspb.ru>
+X-Mailer: git-send-email 1.5.6.5
+MIME-Version: 1.0
+Content-Type: text/plain;
+ charset=utf-8
+Content-Transfer-Encoding: 8bit
+Subject: [Navy-patches] [PATCH]
+ =?utf-8?b?0JjQt9C80LXQvdGR0L0g0YHQv9C40YHQvtC6INC/0LA=?=
+ =?utf-8?b?0LrQtdGC0L7QsiDQvdC10L7QsdGF0L7QtNC40LzRi9GFINC00LvRjyA=?=
+ =?utf-8?b?0YHQsdC+0YDQutC4?=
+
+textlive-* иÑправлены на texlive-*
+docutils заменён на python-docutils
+
+ДейÑтвительно, оказалоÑÑŒ, что rest2web вытÑгивает за Ñобой
+python-docutils. Ð’ то Ð²Ñ€ÐµÐ¼Ñ ÐºÐ°Ðº Ñам rest2web не нужен.
+
+Signed-off-by: Dmitriy Blinov <bda@mnsspb.ru>
+---
+ howto/build_navy.txt | 6 +++---
+ 1 files changed, 3 insertions(+), 3 deletions(-)
+
+diff --git a/howto/build_navy.txt b/howto/build_navy.txt
+index 3fd3afb..0ee807e 100644
+--- a/howto/build_navy.txt
++++ b/howto/build_navy.txt
+@@ -119,8 +119,8 @@
+ - libxv-dev
+ - libusplash-dev
+ - latex-make
+- - textlive-lang-cyrillic
+- - textlive-latex-extra
++ - texlive-lang-cyrillic
++ - texlive-latex-extra
+ - dia
+ - python-pyrex
+ - libtool
+@@ -128,7 +128,7 @@
+ - sox
+ - cython
+ - imagemagick
+- - docutils
++ - python-docutils
+
+ #. на машине dinar: добавить Ñвой открытый ssh-ключ в authorized_keys2 Ð¿Ð¾Ð»ÑŒÐ·Ð¾Ð²Ð°Ñ‚ÐµÐ»Ñ ddev
+ #. на Ñвоей машине: отредактировать /etc/sudoers (команда ``visudo``) примерно Ñледующим образом::
+--
+1.5.6.5
+From nobody Mon Sep 17 00:00:00 2001
+From: <a.u.thor@example.com> (A U Thor)
+Date: Fri, 9 Jun 2006 00:44:16 -0700
+Subject: [PATCH] a patch
+
+From nobody Mon Sep 17 00:00:00 2001
+From: Junio Hamano <junkio@cox.net>
+Date: Thu, 20 Aug 2009 17:18:22 -0700
+Subject: Why doesn't git-am does not like >8 scissors mark?
+
+Subject: [PATCH] BLAH ONE
+
+In real life, we will see a discussion that inspired this patch
+discussing related and unrelated things around >8 scissors mark
+in this part of the message.
+
+Subject: [PATCH] BLAH TWO
+
+And then we will see the scissors.
+
+ This line is not a scissors mark -- >8 -- but talks about it.
+ - - >8 - - please remove everything above this line - - >8 - -
+
+Subject: [PATCH] Teach mailinfo to ignore everything before -- >8 -- mark
+From: Junio C Hamano <gitster@pobox.com>
+
+This teaches mailinfo the scissors -- >8 -- mark; the command ignores
+everything before it in the message body.
+
+Signed-off-by: Junio C Hamano <gitster@pobox.com>
+---
+ builtin-mailinfo.c | 37 ++++++++++++++++++++++++++++++++++++-
+ 1 files changed, 36 insertions(+), 1 deletions(-)
+
+diff --git a/builtin-mailinfo.c b/builtin-mailinfo.c
+index b0b5d8f..461c47e 100644
+--- a/builtin-mailinfo.c
++++ b/builtin-mailinfo.c
+@@ -712,6 +712,34 @@ static inline int patchbreak(const struct strbuf *line)
+ return 0;
+ }
+
++static int scissors(const struct strbuf *line)
++{
++ size_t i, len = line->len;
++ int scissors_dashes_seen = 0;
++ const char *buf = line->buf;
++
++ for (i = 0; i < len; i++) {
++ if (isspace(buf[i]))
++ continue;
++ if (buf[i] == '-') {
++ scissors_dashes_seen |= 02;
++ continue;
++ }
++ if (i + 1 < len && !memcmp(buf + i, ">8", 2)) {
++ scissors_dashes_seen |= 01;
++ i++;
++ continue;
++ }
++ if (i + 7 < len && !memcmp(buf + i, "cut here", 8)) {
++ i += 7;
++ continue;
++ }
++ /* everything else --- not scissors */
++ break;
++ }
++ return scissors_dashes_seen == 03;
++}
++
+ static int handle_commit_msg(struct strbuf *line)
+ {
+ static int still_looking = 1;
+@@ -723,10 +751,17 @@ static int handle_commit_msg(struct strbuf *line)
+ strbuf_ltrim(line);
+ if (!line->len)
+ return 0;
+- if ((still_looking = check_header(line, s_hdr_data, 0)) != 0)
++ still_looking = check_header(line, s_hdr_data, 0);
++ if (still_looking)
+ return 0;
+ }
+
++ if (scissors(line)) {
++ fseek(cmitmsg, 0L, SEEK_SET);
++ still_looking = 1;
++ return 0;
++ }
++
+ /* normalize the log message to UTF-8. */
+ if (metainfo_charset)
+ convert_to_utf8(line, charset.buf);
+--
+1.6.4.1
+From nobody Mon Sep 17 00:00:00 2001
+From: A U Thor <a.u.thor@example.com>
+Subject: check bogus body header (from)
+Date: Fri, 9 Jun 2006 00:44:16 -0700
+
+From: bogosity
+ - a list
+ - of stuff
+---
+diff --git a/foo b/foo
+index e69de29..d95f3ad 100644
+--- a/foo
++++ b/foo
+@@ -0,0 +1 @@
++content
+
+From nobody Mon Sep 17 00:00:00 2001
+From: A U Thor <a.u.thor@example.com>
+Subject: check bogus body header (date)
+Date: Fri, 9 Jun 2006 00:44:16 -0700
+
+Date: bogus
+
+and some content
+
+---
+diff --git a/foo b/foo
+index e69de29..d95f3ad 100644
+--- a/foo
++++ b/foo
+@@ -0,0 +1 @@
++content
+
+From nobody Mon Sep 17 00:00:00 2001
+From: A U Thor <a.u.thor@example.com>
+Subject: A E I O U
+Date: Mon, 17 Sep 2012 14:23:44 -0700
+MIME-Version: 1.0
+Content-Type: text/plain; charset="iso-2022-jp"
+Content-type: text/plain; charset="UTF-8"
+
+New content here
+
+diff --git a/foo b/foo
+index e69de29..d95f3ad 100644
+--- a/foo
++++ b/foo
+@@ -0,0 +1 @@
++New content
+From nobody Mon Sep 17 00:00:00 2001
+From: A U Thor <a.u.thor@example.com>
+Subject: check multiline inbody headers
+Date: Fri, 9 Jun 2006 00:44:16 -0700
+
+From: Another Thor
+ <a.thor@example.com>
+Subject: This one contains
+ a tab
+ and a space
+
+a commit message
+
+diff --git a/foo b/foo
+index e69de29..d95f3ad 100644
+--- a/foo
++++ b/foo
+@@ -0,0 +1 @@
++content
diff --git a/t/t5100/sample.mboxrd b/t/t5100/sample.mboxrd
new file mode 100644
index 0000000..79ad5ae
--- /dev/null
+++ b/t/t5100/sample.mboxrd
@@ -0,0 +1,19 @@
+From mboxrd Mon Sep 17 00:00:00 2001
+From: mboxrd writer <mboxrd@example.com>
+Date: Fri, 9 Jun 2006 00:44:16 -0700
+Subject: [PATCH] a commit with escaped From lines
+
+>From the beginning, mbox should have been mboxrd
+>>From escaped
+From not mangled but this line should have been escaped
+
+From mboxrd Mon Sep 17 00:00:00 2001
+From: mboxrd writer <mboxrd@example.com>
+Date: Fri, 9 Jun 2006 00:44:16 -0700
+Subject: [PATCH 2/2] another with fake From lines
+
+ >From unchanged
+ From also unchanged
+no trailing space, no escaping necessary and '>' was intended:
+>From
+
diff --git a/t/t5150-request-pull.sh b/t/t5150-request-pull.sh
new file mode 100755
index 0000000..cb67bac
--- /dev/null
+++ b/t/t5150-request-pull.sh
@@ -0,0 +1,309 @@
+#!/bin/sh
+
+test_description='Test workflows involving pull request.'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+if ! test_have_prereq PERL
+then
+ skip_all='skipping request-pull tests, perl not available'
+ test_done
+fi
+
+test_expect_success 'setup' '
+
+ git init --bare upstream.git &&
+ git init --bare downstream.git &&
+ git clone upstream.git upstream-private &&
+ git clone downstream.git local &&
+
+ trash_url="file://$TRASH_DIRECTORY" &&
+ downstream_url="$trash_url/downstream.git/" &&
+ upstream_url="$trash_url/upstream.git/" &&
+
+ (
+ cd upstream-private &&
+ cat <<-\EOT >mnemonic.txt &&
+ Thirtey days hath November,
+ Aprile, June, and September:
+ EOT
+ git add mnemonic.txt &&
+ test_tick &&
+ git commit -m "\"Thirty days\", a reminder of month lengths" &&
+ git tag -m "version 1" -a initial &&
+ git push --tags origin main
+ ) &&
+ (
+ cd local &&
+ git remote add upstream "$trash_url/upstream.git" &&
+ git fetch upstream &&
+ git pull upstream main &&
+ cat <<-\EOT >>mnemonic.txt &&
+ Of twyecescore-eightt is but eine,
+ And all the remnante be thrycescore-eine.
+ O’course Leap yare comes an’pynes,
+ Ev’rie foure yares, gote it ryghth.
+ An’twyecescore-eight is but twyecescore-nyne.
+ EOT
+ git add mnemonic.txt &&
+ test_tick &&
+ git commit -m "More detail" &&
+ git tag -m "version 2" -a full &&
+ git checkout -b simplify HEAD^ &&
+ mv mnemonic.txt mnemonic.standard &&
+ cat <<-\EOT >mnemonic.clarified &&
+ Thirty days has September,
+ All the rest I can’t remember.
+ EOT
+ git add -N mnemonic.standard mnemonic.clarified &&
+ git commit -a -m "Adapt to use modern, simpler English
+
+But keep the old version, too, in case some people prefer it." &&
+ git checkout main
+ )
+
+'
+
+test_expect_success 'setup: two scripts for reading pull requests' '
+
+ downstream_url_for_sed=$(
+ printf "%s\n" "$downstream_url" |
+ sed -e '\''s/\\/\\\\/g'\'' -e '\''s/[[/.*^$]/\\&/g'\''
+ ) &&
+
+ cat <<-\EOT >read-request.sed &&
+ #!/bin/sed -nf
+ # Note that a request could ask for "tag $tagname"
+ / in the Git repository at:$/!d
+ n
+ /^$/ n
+ s/ tag \([^ ]*\)$/ tag--\1/
+ s/^[ ]*\(.*\) \([^ ]*\)/please pull\
+ \1\
+ \2/p
+ q
+ EOT
+
+ cat <<-EOT >fuzz.sed
+ #!/bin/sed -nf
+ s/$downstream_url_for_sed/URL/g
+ s/$OID_REGEX/OBJECT_NAME/g
+ s/A U Thor/AUTHOR/g
+ s/[-0-9]\{10\} [:0-9]\{8\} [-+][0-9]\{4\}/DATE/g
+ s/ [^ ].*/ SUBJECT/g
+ s/ [^ ].* (DATE)/ SUBJECT (DATE)/g
+ s|tags/full|BRANCH|g
+ s/mnemonic.txt/FILENAME/g
+ s/^version [0-9]/VERSION/
+ /^ FILENAME | *[0-9]* [-+]*\$/ b diffstat
+ /^AUTHOR ([0-9]*):\$/ b shortlog
+ p
+ b
+ : diffstat
+ n
+ / [0-9]* files* changed/ {
+ a\\
+ DIFFSTAT
+ b
+ }
+ b diffstat
+ : shortlog
+ /^ [a-zA-Z]/ n
+ /^[a-zA-Z]* ([0-9]*):\$/ n
+ /^\$/ N
+ /^\n[a-zA-Z]* ([0-9]*):\$/!{
+ a\\
+ SHORTLOG
+ D
+ }
+ n
+ b shortlog
+ EOT
+
+'
+
+test_expect_success 'pull request when forgot to push' '
+
+ rm -fr downstream.git &&
+ git init --bare downstream.git &&
+ (
+ cd local &&
+ git checkout initial &&
+ git merge --ff-only main &&
+ test_must_fail git request-pull initial "$downstream_url" \
+ 2>../err
+ ) &&
+ grep "No match for commit .*" err &&
+ grep "Are you sure you pushed" err
+
+'
+
+test_expect_success 'pull request after push' '
+
+ rm -fr downstream.git &&
+ git init --bare downstream.git &&
+ (
+ cd local &&
+ git checkout initial &&
+ git merge --ff-only main &&
+ git push origin main:for-upstream &&
+ git request-pull initial origin main:for-upstream >../request
+ ) &&
+ sed -nf read-request.sed <request >digest &&
+ {
+ read task &&
+ read repository &&
+ read branch
+ } <digest &&
+ (
+ cd upstream-private &&
+ git checkout initial &&
+ git pull --ff-only "$repository" "$branch"
+ ) &&
+ test "$branch" = for-upstream &&
+ test_cmp local/mnemonic.txt upstream-private/mnemonic.txt
+
+'
+
+test_expect_success 'request asks HEAD to be pulled' '
+
+ rm -fr downstream.git &&
+ git init --bare downstream.git &&
+ (
+ cd local &&
+ git checkout initial &&
+ git merge --ff-only main &&
+ git push --tags origin main simplify &&
+ git push origin main:for-upstream &&
+ git request-pull initial "$downstream_url" >../request
+ ) &&
+ sed -nf read-request.sed <request >digest &&
+ {
+ read task &&
+ read repository &&
+ read branch
+ } <digest &&
+ test -z "$branch"
+
+'
+
+test_expect_success 'pull request format' '
+
+ rm -fr downstream.git &&
+ git init --bare downstream.git &&
+ cat <<-\EOT >expect &&
+ The following changes since commit OBJECT_NAME:
+
+ SUBJECT (DATE)
+
+ are available in the Git repository at:
+
+ URL BRANCH
+
+ for you to fetch changes up to OBJECT_NAME:
+
+ SUBJECT (DATE)
+
+ ----------------------------------------------------------------
+ VERSION
+
+ ----------------------------------------------------------------
+ SHORTLOG
+
+ DIFFSTAT
+ EOT
+ (
+ cd local &&
+ git checkout initial &&
+ git merge --ff-only main &&
+ git push origin tags/full &&
+ git request-pull initial "$downstream_url" tags/full >../request
+ ) &&
+ <request sed -nf fuzz.sed >request.fuzzy &&
+ test_cmp expect request.fuzzy &&
+
+ (
+ cd local &&
+ git request-pull initial "$downstream_url" tags/full:refs/tags/full
+ ) >request &&
+ sed -nf fuzz.sed <request >request.fuzzy &&
+ test_cmp expect request.fuzzy &&
+
+ (
+ cd local &&
+ git request-pull initial "$downstream_url" full
+ ) >request &&
+ grep " tags/full\$" request
+'
+
+test_expect_success 'request-pull ignores OPTIONS_KEEPDASHDASH poison' '
+
+ (
+ cd local &&
+ OPTIONS_KEEPDASHDASH=Yes &&
+ export OPTIONS_KEEPDASHDASH &&
+ git checkout initial &&
+ git merge --ff-only main &&
+ git push origin main:for-upstream &&
+ git request-pull -- initial "$downstream_url" main:for-upstream >../request
+ )
+
+'
+
+test_expect_success 'request-pull quotes regex metacharacters properly' '
+
+ rm -fr downstream.git &&
+ git init --bare downstream.git &&
+ (
+ cd local &&
+ git checkout initial &&
+ git merge --ff-only main &&
+ git tag -mrelease v2.0 &&
+ git push origin refs/tags/v2.0:refs/tags/v2-0 &&
+ test_must_fail git request-pull initial "$downstream_url" tags/v2.0 \
+ 2>../err
+ ) &&
+ grep "No match for commit .*" err &&
+ grep "Are you sure you pushed" err
+
+'
+
+test_expect_success 'pull request with mismatched object' '
+
+ rm -fr downstream.git &&
+ git init --bare downstream.git &&
+ (
+ cd local &&
+ git checkout initial &&
+ git merge --ff-only main &&
+ git push origin HEAD:refs/tags/full &&
+ test_must_fail git request-pull initial "$downstream_url" tags/full \
+ 2>../err
+ ) &&
+ grep "points to a different object" err &&
+ grep "Are you sure you pushed" err
+
+'
+
+test_expect_success 'pull request with stale object' '
+
+ rm -fr downstream.git &&
+ git init --bare downstream.git &&
+ (
+ cd local &&
+ git checkout initial &&
+ git merge --ff-only main &&
+ git push origin refs/tags/full &&
+ git tag -f -m"Thirty-one days" full &&
+ test_must_fail git request-pull initial "$downstream_url" tags/full \
+ 2>../err
+ ) &&
+ grep "points to a different object" err &&
+ grep "Are you sure you pushed" err
+
+'
+
+test_done
diff --git a/t/t5200-update-server-info.sh b/t/t5200-update-server-info.sh
new file mode 100755
index 0000000..ed9dfd6
--- /dev/null
+++ b/t/t5200-update-server-info.sh
@@ -0,0 +1,42 @@
+#!/bin/sh
+
+test_description='Test git update-server-info'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' 'test_commit file'
+
+test_expect_success 'create info/refs' '
+ git update-server-info &&
+ test_path_is_file .git/info/refs
+'
+
+test_expect_success 'modify and store mtime' '
+ test-tool chmtime =0 .git/info/refs &&
+ test-tool chmtime --get .git/info/refs >a
+'
+
+test_expect_success 'info/refs is not needlessly overwritten' '
+ git update-server-info &&
+ test-tool chmtime --get .git/info/refs >b &&
+ test_cmp a b
+'
+
+test_expect_success 'info/refs can be forced to update' '
+ git update-server-info -f &&
+ test-tool chmtime --get .git/info/refs >b &&
+ ! test_cmp a b
+'
+
+test_expect_success 'info/refs updates when changes are made' '
+ test-tool chmtime =0 .git/info/refs &&
+ test-tool chmtime --get .git/info/refs >b &&
+ test_cmp a b &&
+ git update-ref refs/heads/foo HEAD &&
+ git update-server-info &&
+ test-tool chmtime --get .git/info/refs >b &&
+ ! test_cmp a b
+'
+
+test_done
diff --git a/t/t5300-pack-object.sh b/t/t5300-pack-object.sh
new file mode 100755
index 0000000..d402ec1
--- /dev/null
+++ b/t/t5300-pack-object.sh
@@ -0,0 +1,597 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+test_description='git pack-object
+
+'
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ rm -f .git/index* &&
+ perl -e "print \"a\" x 4096;" >a &&
+ perl -e "print \"b\" x 4096;" >b &&
+ perl -e "print \"c\" x 4096;" >c &&
+ test-tool genrandom "seed a" 2097152 >a_big &&
+ test-tool genrandom "seed b" 2097152 >b_big &&
+ git update-index --add a a_big b b_big c &&
+ cat c >d && echo foo >>d && git update-index --add d &&
+ tree=$(git write-tree) &&
+ commit=$(git commit-tree $tree </dev/null) &&
+ {
+ echo $tree &&
+ echo $commit &&
+ git ls-tree $tree | sed -e "s/.* \\([0-9a-f]*\\) .*/\\1/"
+ } >obj-list &&
+ {
+ git diff-tree --root -p $commit &&
+ while read object
+ do
+ t=$(git cat-file -t $object) &&
+ git cat-file $t $object || return 1
+ done <obj-list
+ } >expect
+'
+
+test_expect_success 'setup pack-object <stdin' '
+ git init pack-object-stdin &&
+ test_commit -C pack-object-stdin one &&
+ test_commit -C pack-object-stdin two
+
+'
+
+test_expect_success 'pack-object <stdin parsing: basic [|--revs]' '
+ cat >in <<-EOF &&
+ $(git -C pack-object-stdin rev-parse one)
+ EOF
+
+ git -C pack-object-stdin pack-objects basic-stdin <in &&
+ idx=$(echo pack-object-stdin/basic-stdin-*.idx) &&
+ git show-index <"$idx" >actual &&
+ test_line_count = 1 actual &&
+
+ git -C pack-object-stdin pack-objects --revs basic-stdin-revs <in &&
+ idx=$(echo pack-object-stdin/basic-stdin-revs-*.idx) &&
+ git show-index <"$idx" >actual &&
+ test_line_count = 3 actual
+'
+
+test_expect_success 'pack-object <stdin parsing: [|--revs] bad line' '
+ cat >in <<-EOF &&
+ $(git -C pack-object-stdin rev-parse one)
+ garbage
+ $(git -C pack-object-stdin rev-parse two)
+ EOF
+
+ sed "s/^> //g" >err.expect <<-EOF &&
+ fatal: expected object ID, got garbage:
+ > garbage
+
+ EOF
+ test_must_fail git -C pack-object-stdin pack-objects bad-line-stdin <in 2>err.actual &&
+ test_cmp err.expect err.actual &&
+
+ cat >err.expect <<-EOF &&
+ fatal: bad revision '"'"'garbage'"'"'
+ EOF
+ test_must_fail git -C pack-object-stdin pack-objects --revs bad-line-stdin-revs <in 2>err.actual &&
+ test_cmp err.expect err.actual
+'
+
+test_expect_success 'pack-object <stdin parsing: [|--revs] empty line' '
+ cat >in <<-EOF &&
+ $(git -C pack-object-stdin rev-parse one)
+
+ $(git -C pack-object-stdin rev-parse two)
+ EOF
+
+ sed -e "s/^> //g" -e "s/Z$//g" >err.expect <<-EOF &&
+ fatal: expected object ID, got garbage:
+ > Z
+
+ EOF
+ test_must_fail git -C pack-object-stdin pack-objects empty-line-stdin <in 2>err.actual &&
+ test_cmp err.expect err.actual &&
+
+ git -C pack-object-stdin pack-objects --revs empty-line-stdin-revs <in &&
+ idx=$(echo pack-object-stdin/empty-line-stdin-revs-*.idx) &&
+ git show-index <"$idx" >actual &&
+ test_line_count = 3 actual
+'
+
+test_expect_success 'pack-object <stdin parsing: [|--revs] with --stdin' '
+ cat >in <<-EOF &&
+ $(git -C pack-object-stdin rev-parse one)
+ $(git -C pack-object-stdin rev-parse two)
+ EOF
+
+ # There is the "--stdin-packs is incompatible with --revs"
+ # test below, but we should make sure that the revision.c
+ # --stdin is not picked up
+ cat >err.expect <<-EOF &&
+ fatal: disallowed abbreviated or ambiguous option '"'"'stdin'"'"'
+ EOF
+ test_must_fail git -C pack-object-stdin pack-objects stdin-with-stdin-option --stdin <in 2>err.actual &&
+ test_cmp err.expect err.actual &&
+
+ test_must_fail git -C pack-object-stdin pack-objects --stdin --revs stdin-with-stdin-option-revs 2>err.actual <in &&
+ test_cmp err.expect err.actual
+'
+
+test_expect_success 'pack-object <stdin parsing: --stdin-packs handles garbage' '
+ cat >in <<-EOF &&
+ $(git -C pack-object-stdin rev-parse one)
+ $(git -C pack-object-stdin rev-parse two)
+ EOF
+
+ # That we get "two" and not "one" has to do with OID
+ # ordering. It happens to be the same here under SHA-1 and
+ # SHA-256. See commentary in pack-objects.c
+ cat >err.expect <<-EOF &&
+ fatal: could not find pack '"'"'$(git -C pack-object-stdin rev-parse two)'"'"'
+ EOF
+ test_must_fail git \
+ -C pack-object-stdin \
+ pack-objects stdin-with-stdin-option --stdin-packs \
+ <in 2>err.actual &&
+ test_cmp err.expect err.actual
+'
+
+# usage: check_deltas <stderr_from_pack_objects> <cmp_op> <nr_deltas>
+# e.g.: check_deltas stderr -gt 0
+check_deltas() {
+ deltas=$(perl -lne '/delta (\d+)/ and print $1' "$1") &&
+ shift &&
+ if ! test "$deltas" "$@"
+ then
+ echo >&2 "unexpected number of deltas (compared $delta $*)"
+ return 1
+ fi
+}
+
+test_expect_success 'pack without delta' '
+ packname_1=$(git pack-objects --progress --window=0 test-1 \
+ <obj-list 2>stderr) &&
+ check_deltas stderr = 0
+'
+
+test_expect_success 'pack-objects with bogus arguments' '
+ test_must_fail git pack-objects --window=0 test-1 blah blah <obj-list
+'
+
+check_unpack () {
+ local packname="$1" &&
+ local object_list="$2" &&
+ local git_config="$3" &&
+ test_when_finished "rm -rf git2" &&
+ git $git_config init --bare git2 &&
+ (
+ git $git_config -C git2 unpack-objects -n <"$packname".pack &&
+ git $git_config -C git2 unpack-objects <"$packname".pack &&
+ git $git_config -C git2 cat-file --batch-check="%(objectname)"
+ ) <"$object_list" >current &&
+ cmp "$object_list" current
+}
+
+test_expect_success 'unpack without delta' '
+ check_unpack test-1-${packname_1} obj-list
+'
+
+BATCH_CONFIGURATION='-c core.fsync=loose-object -c core.fsyncmethod=batch'
+
+test_expect_success 'unpack without delta (core.fsyncmethod=batch)' '
+ check_unpack test-1-${packname_1} obj-list "$BATCH_CONFIGURATION"
+'
+
+test_expect_success 'pack with REF_DELTA' '
+ packname_2=$(git pack-objects --progress test-2 <obj-list 2>stderr) &&
+ check_deltas stderr -gt 0
+'
+
+test_expect_success 'unpack with REF_DELTA' '
+ check_unpack test-2-${packname_2} obj-list
+'
+
+test_expect_success 'unpack with REF_DELTA (core.fsyncmethod=batch)' '
+ check_unpack test-2-${packname_2} obj-list "$BATCH_CONFIGURATION"
+'
+
+test_expect_success 'pack with OFS_DELTA' '
+ packname_3=$(git pack-objects --progress --delta-base-offset test-3 \
+ <obj-list 2>stderr) &&
+ check_deltas stderr -gt 0
+'
+
+test_expect_success 'unpack with OFS_DELTA' '
+ check_unpack test-3-${packname_3} obj-list
+'
+
+test_expect_success 'unpack with OFS_DELTA (core.fsyncmethod=batch)' '
+ check_unpack test-3-${packname_3} obj-list "$BATCH_CONFIGURATION"
+'
+
+test_expect_success 'compare delta flavors' '
+ perl -e '\''
+ defined($_ = -s $_) or die for @ARGV;
+ exit 1 if $ARGV[0] <= $ARGV[1];
+ '\'' test-2-$packname_2.pack test-3-$packname_3.pack
+'
+
+check_use_objects () {
+ test_when_finished "rm -rf git2" &&
+ git init --bare git2 &&
+ cp "$1".pack "$1".idx git2/objects/pack &&
+ (
+ cd git2 &&
+ git diff-tree --root -p $commit &&
+ while read object
+ do
+ t=$(git cat-file -t $object) &&
+ git cat-file $t $object || exit 1
+ done
+ ) <obj-list >current &&
+ cmp expect current
+}
+
+test_expect_success 'use packed objects' '
+ check_use_objects test-1-${packname_1}
+'
+
+test_expect_success 'use packed deltified (REF_DELTA) objects' '
+ check_use_objects test-2-${packname_2}
+'
+
+test_expect_success 'use packed deltified (OFS_DELTA) objects' '
+ check_use_objects test-3-${packname_3}
+'
+
+test_expect_success 'survive missing objects/pack directory' '
+ (
+ rm -fr missing-pack &&
+ mkdir missing-pack &&
+ cd missing-pack &&
+ git init &&
+ GOP=.git/objects/pack &&
+ rm -fr $GOP &&
+ git index-pack --stdin --keep=test <../test-3-${packname_3}.pack &&
+ test -f $GOP/pack-${packname_3}.pack &&
+ cmp $GOP/pack-${packname_3}.pack ../test-3-${packname_3}.pack &&
+ test -f $GOP/pack-${packname_3}.idx &&
+ cmp $GOP/pack-${packname_3}.idx ../test-3-${packname_3}.idx &&
+ test -f $GOP/pack-${packname_3}.keep
+ )
+'
+
+test_expect_success 'verify pack' '
+ git verify-pack test-1-${packname_1}.idx \
+ test-2-${packname_2}.idx \
+ test-3-${packname_3}.idx
+'
+
+test_expect_success 'verify pack -v' '
+ git verify-pack -v test-1-${packname_1}.idx \
+ test-2-${packname_2}.idx \
+ test-3-${packname_3}.idx
+'
+
+test_expect_success 'verify-pack catches mismatched .idx and .pack files' '
+ cat test-1-${packname_1}.idx >test-3.idx &&
+ cat test-2-${packname_2}.pack >test-3.pack &&
+ if git verify-pack test-3.idx
+ then false
+ else :;
+ fi
+'
+
+test_expect_success 'verify-pack catches a corrupted pack signature' '
+ cat test-1-${packname_1}.pack >test-3.pack &&
+ echo | dd of=test-3.pack count=1 bs=1 conv=notrunc seek=2 &&
+ if git verify-pack test-3.idx
+ then false
+ else :;
+ fi
+'
+
+test_expect_success 'verify-pack catches a corrupted pack version' '
+ cat test-1-${packname_1}.pack >test-3.pack &&
+ echo | dd of=test-3.pack count=1 bs=1 conv=notrunc seek=7 &&
+ if git verify-pack test-3.idx
+ then false
+ else :;
+ fi
+'
+
+test_expect_success 'verify-pack catches a corrupted type/size of the 1st packed object data' '
+ cat test-1-${packname_1}.pack >test-3.pack &&
+ echo | dd of=test-3.pack count=1 bs=1 conv=notrunc seek=12 &&
+ if git verify-pack test-3.idx
+ then false
+ else :;
+ fi
+'
+
+test_expect_success 'verify-pack catches a corrupted sum of the index file itself' '
+ l=$(wc -c <test-3.idx) &&
+ l=$(expr $l - 20) &&
+ cat test-1-${packname_1}.pack >test-3.pack &&
+ printf "%20s" "" | dd of=test-3.idx count=20 bs=1 conv=notrunc seek=$l &&
+ if git verify-pack test-3.pack
+ then false
+ else :;
+ fi
+'
+
+test_expect_success 'build pack index for an existing pack' '
+ cat test-1-${packname_1}.pack >test-3.pack &&
+ git index-pack -o tmp.idx test-3.pack &&
+ cmp tmp.idx test-1-${packname_1}.idx &&
+
+ git index-pack --promisor=message test-3.pack &&
+ cmp test-3.idx test-1-${packname_1}.idx &&
+ echo message >expect &&
+ test_cmp expect test-3.promisor &&
+
+ cat test-2-${packname_2}.pack >test-3.pack &&
+ git index-pack -o tmp.idx test-2-${packname_2}.pack &&
+ cmp tmp.idx test-2-${packname_2}.idx &&
+
+ git index-pack test-3.pack &&
+ cmp test-3.idx test-2-${packname_2}.idx &&
+
+ cat test-3-${packname_3}.pack >test-3.pack &&
+ git index-pack -o tmp.idx test-3-${packname_3}.pack &&
+ cmp tmp.idx test-3-${packname_3}.idx &&
+
+ git index-pack test-3.pack &&
+ cmp test-3.idx test-3-${packname_3}.idx &&
+
+ cat test-1-${packname_1}.pack >test-4.pack &&
+ rm -f test-4.keep &&
+ git index-pack --keep=why test-4.pack &&
+ cmp test-1-${packname_1}.idx test-4.idx &&
+ test -f test-4.keep &&
+
+ :
+'
+
+test_expect_success 'unpacking with --strict' '
+
+ for j in a b c d e f g
+ do
+ for i in 0 1 2 3 4 5 6 7 8 9
+ do
+ o=$(echo $j$i | git hash-object -w --stdin) &&
+ echo "100644 $o 0 $j$i" || return 1
+ done
+ done >LIST &&
+ rm -f .git/index &&
+ git update-index --index-info <LIST &&
+ LIST=$(git write-tree) &&
+ rm -f .git/index &&
+ head -n 10 LIST | git update-index --index-info &&
+ LI=$(git write-tree) &&
+ rm -f .git/index &&
+ tail -n 10 LIST | git update-index --index-info &&
+ ST=$(git write-tree) &&
+ git rev-list --objects "$LIST" "$LI" "$ST" >actual &&
+ PACK5=$( git pack-objects test-5 <actual ) &&
+ PACK6=$( test_write_lines "$LIST" "$LI" "$ST" | git pack-objects test-6 ) &&
+ test_create_repo test-5 &&
+ (
+ cd test-5 &&
+ git unpack-objects --strict <../test-5-$PACK5.pack &&
+ git ls-tree -r $LIST &&
+ git ls-tree -r $LI &&
+ git ls-tree -r $ST
+ ) &&
+ test_create_repo test-6 &&
+ (
+ # tree-only into empty repo -- many unreachables
+ cd test-6 &&
+ test_must_fail git unpack-objects --strict <../test-6-$PACK6.pack
+ ) &&
+ (
+ # already populated -- no unreachables
+ cd test-5 &&
+ git unpack-objects --strict <../test-6-$PACK6.pack
+ )
+'
+
+test_expect_success 'index-pack with --strict' '
+
+ for j in a b c d e f g
+ do
+ for i in 0 1 2 3 4 5 6 7 8 9
+ do
+ o=$(echo $j$i | git hash-object -w --stdin) &&
+ echo "100644 $o 0 $j$i" || return 1
+ done
+ done >LIST &&
+ rm -f .git/index &&
+ git update-index --index-info <LIST &&
+ LIST=$(git write-tree) &&
+ rm -f .git/index &&
+ head -n 10 LIST | git update-index --index-info &&
+ LI=$(git write-tree) &&
+ rm -f .git/index &&
+ tail -n 10 LIST | git update-index --index-info &&
+ ST=$(git write-tree) &&
+ git rev-list --objects "$LIST" "$LI" "$ST" >actual &&
+ PACK5=$( git pack-objects test-5 <actual ) &&
+ PACK6=$( test_write_lines "$LIST" "$LI" "$ST" | git pack-objects test-6 ) &&
+ test_create_repo test-7 &&
+ (
+ cd test-7 &&
+ git index-pack --strict --stdin <../test-5-$PACK5.pack &&
+ git ls-tree -r $LIST &&
+ git ls-tree -r $LI &&
+ git ls-tree -r $ST
+ ) &&
+ test_create_repo test-8 &&
+ (
+ # tree-only into empty repo -- many unreachables
+ cd test-8 &&
+ test_must_fail git index-pack --strict --stdin <../test-6-$PACK6.pack
+ ) &&
+ (
+ # already populated -- no unreachables
+ cd test-7 &&
+ git index-pack --strict --stdin <../test-6-$PACK6.pack
+ )
+'
+
+test_expect_success 'honor pack.packSizeLimit' '
+ git config pack.packSizeLimit 3m &&
+ packname_10=$(git pack-objects test-10 <obj-list) &&
+ test 2 = $(ls test-10-*.pack | wc -l)
+'
+
+test_expect_success 'verify resulting packs' '
+ git verify-pack test-10-*.pack
+'
+
+test_expect_success 'tolerate packsizelimit smaller than biggest object' '
+ git config pack.packSizeLimit 1 &&
+ packname_11=$(git pack-objects test-11 <obj-list) &&
+ test 5 = $(ls test-11-*.pack | wc -l)
+'
+
+test_expect_success 'verify resulting packs' '
+ git verify-pack test-11-*.pack
+'
+
+test_expect_success 'set up pack for non-repo tests' '
+ # make sure we have a pack with no matching index file
+ cp test-1-*.pack foo.pack
+'
+
+test_expect_success 'index-pack --stdin complains of non-repo' '
+ nongit test_must_fail git index-pack --object-format=$(test_oid algo) --stdin <foo.pack &&
+ test_path_is_missing non-repo/.git
+'
+
+test_expect_success 'index-pack <pack> works in non-repo' '
+ nongit git index-pack --object-format=$(test_oid algo) ../foo.pack &&
+ test_path_is_file foo.idx
+'
+
+test_expect_success 'index-pack --strict <pack> works in non-repo' '
+ rm -f foo.idx &&
+ nongit git index-pack --strict --object-format=$(test_oid algo) ../foo.pack &&
+ test_path_is_file foo.idx
+'
+
+test_expect_success !PTHREADS,!FAIL_PREREQS \
+ 'index-pack --threads=N or pack.threads=N warns when no pthreads' '
+ test_must_fail git index-pack --threads=2 2>err &&
+ grep ^warning: err >warnings &&
+ test_line_count = 1 warnings &&
+ grep -F "no threads support, ignoring --threads=2" err &&
+
+ test_must_fail git -c pack.threads=2 index-pack 2>err &&
+ grep ^warning: err >warnings &&
+ test_line_count = 1 warnings &&
+ grep -F "no threads support, ignoring pack.threads" err &&
+
+ test_must_fail git -c pack.threads=2 index-pack --threads=4 2>err &&
+ grep ^warning: err >warnings &&
+ test_line_count = 2 warnings &&
+ grep -F "no threads support, ignoring --threads=4" err &&
+ grep -F "no threads support, ignoring pack.threads" err
+'
+
+test_expect_success !PTHREADS,!FAIL_PREREQS \
+ 'pack-objects --threads=N or pack.threads=N warns when no pthreads' '
+ git pack-objects --threads=2 --stdout --all </dev/null >/dev/null 2>err &&
+ grep ^warning: err >warnings &&
+ test_line_count = 1 warnings &&
+ grep -F "no threads support, ignoring --threads" err &&
+
+ git -c pack.threads=2 pack-objects --stdout --all </dev/null >/dev/null 2>err &&
+ grep ^warning: err >warnings &&
+ test_line_count = 1 warnings &&
+ grep -F "no threads support, ignoring pack.threads" err &&
+
+ git -c pack.threads=2 pack-objects --threads=4 --stdout --all </dev/null >/dev/null 2>err &&
+ grep ^warning: err >warnings &&
+ test_line_count = 2 warnings &&
+ grep -F "no threads support, ignoring --threads" err &&
+ grep -F "no threads support, ignoring pack.threads" err
+'
+
+test_expect_success 'pack-objects in too-many-packs mode' '
+ GIT_TEST_FULL_IN_PACK_ARRAY=1 git repack -ad &&
+ git fsck
+'
+
+test_expect_success 'setup: fake a SHA1 hash collision' '
+ git init corrupt &&
+ (
+ cd corrupt &&
+ long_a=$(git hash-object -w ../a | sed -e "s!^..!&/!") &&
+ long_b=$(git hash-object -w ../b | sed -e "s!^..!&/!") &&
+ test -f .git/objects/$long_b &&
+ cp -f .git/objects/$long_a \
+ .git/objects/$long_b
+ )
+'
+
+test_expect_success 'make sure index-pack detects the SHA1 collision' '
+ (
+ cd corrupt &&
+ test_must_fail git index-pack -o ../bad.idx ../test-3.pack 2>msg &&
+ test_grep "SHA1 COLLISION FOUND" msg
+ )
+'
+
+test_expect_success 'make sure index-pack detects the SHA1 collision (large blobs)' '
+ (
+ cd corrupt &&
+ test_must_fail git -c core.bigfilethreshold=1 index-pack -o ../bad.idx ../test-3.pack 2>msg &&
+ test_grep "SHA1 COLLISION FOUND" msg
+ )
+'
+
+test_expect_success 'prefetch objects' '
+ rm -rf server client &&
+
+ git init server &&
+ test_config -C server uploadpack.allowanysha1inwant 1 &&
+ test_config -C server uploadpack.allowfilter 1 &&
+ test_config -C server protocol.version 2 &&
+
+ echo one >server/one &&
+ git -C server add one &&
+ git -C server commit -m one &&
+ git -C server branch one_branch &&
+
+ echo two_a >server/two_a &&
+ echo two_b >server/two_b &&
+ git -C server add two_a two_b &&
+ git -C server commit -m two &&
+
+ echo three >server/three &&
+ git -C server add three &&
+ git -C server commit -m three &&
+ git -C server branch three_branch &&
+
+ # Clone, fetch "two" with blobs excluded, and re-push it. This requires
+ # the client to have the blobs of "two" - verify that these are
+ # prefetched in one batch.
+ git clone --filter=blob:none --single-branch -b one_branch \
+ "file://$(pwd)/server" client &&
+ test_config -C client protocol.version 2 &&
+ TWO=$(git -C server rev-parse three_branch^) &&
+ git -C client fetch --filter=blob:none origin "$TWO" &&
+ GIT_TRACE_PACKET=$(pwd)/trace git -C client push origin "$TWO":refs/heads/two_branch &&
+ grep "fetch> done" trace >donelines &&
+ test_line_count = 1 donelines
+'
+
+test_expect_success 'negative window clamps to 0' '
+ git pack-objects --progress --window=-1 neg-window <obj-list 2>stderr &&
+ check_deltas stderr = 0
+'
+
+test_done
diff --git a/t/t5301-sliding-window.sh b/t/t5301-sliding-window.sh
new file mode 100755
index 0000000..226490d
--- /dev/null
+++ b/t/t5301-sliding-window.sh
@@ -0,0 +1,62 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Shawn Pearce
+#
+
+test_description='mmap sliding window tests'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ rm -f .git/index* &&
+ for i in a b c
+ do
+ echo $i >$i &&
+ test-tool genrandom "$i" 32768 >>$i &&
+ git update-index --add $i || return 1
+ done &&
+ echo d >d && cat c >>d && git update-index --add d &&
+ tree=$(git write-tree) &&
+ commit1=$(git commit-tree $tree </dev/null) &&
+ git update-ref HEAD $commit1 &&
+ git repack -a -d &&
+ test "$(git count-objects)" = "0 objects, 0 kilobytes" &&
+ pack1=$(ls .git/objects/pack/*.pack) &&
+ test -f "$pack1"
+'
+
+test_expect_success 'verify-pack -v, defaults' '
+ git verify-pack -v "$pack1"
+'
+
+test_expect_success 'verify-pack -v, packedGitWindowSize == 1 page' '
+ git config core.packedGitWindowSize 512 &&
+ git verify-pack -v "$pack1"
+'
+
+test_expect_success 'verify-pack -v, packedGit{WindowSize,Limit} == 1 page' '
+ git config core.packedGitWindowSize 512 &&
+ git config core.packedGitLimit 512 &&
+ git verify-pack -v "$pack1"
+'
+
+test_expect_success 'repack -a -d, packedGit{WindowSize,Limit} == 1 page' '
+ git config core.packedGitWindowSize 512 &&
+ git config core.packedGitLimit 512 &&
+ commit2=$(git commit-tree $tree -p $commit1 </dev/null) &&
+ git update-ref HEAD $commit2 &&
+ git repack -a -d &&
+ test "$(git count-objects)" = "0 objects, 0 kilobytes" &&
+ pack2=$(ls .git/objects/pack/*.pack) &&
+ test -f "$pack2" &&
+ test "$pack1" \!= "$pack2"
+'
+
+test_expect_success 'verify-pack -v, defaults' '
+ git config --unset core.packedGitWindowSize &&
+ git config --unset core.packedGitLimit &&
+ git verify-pack -v "$pack2"
+'
+
+test_done
diff --git a/t/t5302-pack-index.sh b/t/t5302-pack-index.sh
new file mode 100755
index 0000000..d88e6f1
--- /dev/null
+++ b/t/t5302-pack-index.sh
@@ -0,0 +1,297 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Nicolas Pitre
+#
+
+test_description='pack index with 64-bit offsets and object CRC'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ rawsz=$(test_oid rawsz) &&
+ rm -rf .git &&
+ git init &&
+ git config pack.threads 1 &&
+ i=1 &&
+ while test $i -le 100
+ do
+ iii=$(printf "%03i" $i) &&
+ test-tool genrandom "bar" 200 > wide_delta_$iii &&
+ test-tool genrandom "baz $iii" 50 >> wide_delta_$iii &&
+ test-tool genrandom "foo"$i 100 > deep_delta_$iii &&
+ test-tool genrandom "foo"$(expr $i + 1) 100 >> deep_delta_$iii &&
+ test-tool genrandom "foo"$(expr $i + 2) 100 >> deep_delta_$iii &&
+ echo $iii >file_$iii &&
+ test-tool genrandom "$iii" 8192 >>file_$iii &&
+ git update-index --add file_$iii deep_delta_$iii wide_delta_$iii &&
+ i=$(expr $i + 1) || return 1
+ done &&
+ { echo 101 && test-tool genrandom 100 8192; } >file_101 &&
+ git update-index --add file_101 &&
+ tree=$(git write-tree) &&
+ commit=$(git commit-tree $tree </dev/null) && {
+ echo $tree &&
+ git ls-tree $tree | sed -e "s/.* \\([0-9a-f]*\\) .*/\\1/"
+ } >obj-list &&
+ git update-ref HEAD $commit
+'
+
+test_expect_success 'pack-objects with index version 1' '
+ pack1=$(git pack-objects --index-version=1 test-1 <obj-list) &&
+ git verify-pack -v "test-1-${pack1}.pack"
+'
+
+test_expect_success 'pack-objects with index version 2' '
+ pack2=$(git pack-objects --index-version=2 test-2 <obj-list) &&
+ git verify-pack -v "test-2-${pack2}.pack"
+'
+
+test_expect_success 'both packs should be identical' '
+ cmp "test-1-${pack1}.pack" "test-2-${pack2}.pack"
+'
+
+test_expect_success 'index v1 and index v2 should be different' '
+ ! cmp "test-1-${pack1}.idx" "test-2-${pack2}.idx"
+'
+
+test_expect_success 'index-pack with index version 1' '
+ git index-pack --index-version=1 -o 1.idx "test-1-${pack1}.pack"
+'
+
+test_expect_success 'index-pack with index version 2' '
+ git index-pack --index-version=2 -o 2.idx "test-1-${pack1}.pack"
+'
+
+test_expect_success 'index-pack results should match pack-objects ones' '
+ cmp "test-1-${pack1}.idx" "1.idx" &&
+ cmp "test-2-${pack2}.idx" "2.idx"
+'
+
+test_expect_success 'index-pack --verify on index version 1' '
+ git index-pack --verify "test-1-${pack1}.pack"
+'
+
+test_expect_success 'index-pack --verify on index version 2' '
+ git index-pack --verify "test-2-${pack2}.pack"
+'
+
+test_expect_success 'pack-objects --index-version=2, is not accepted' '
+ test_must_fail git pack-objects --index-version=2, test-3 <obj-list
+'
+
+test_expect_success 'index v2: force some 64-bit offsets with pack-objects' '
+ pack3=$(git pack-objects --index-version=2,0x40000 test-3 <obj-list)
+'
+
+if msg=$(git verify-pack -v "test-3-${pack3}.pack" 2>&1) ||
+ ! (echo "$msg" | grep "pack too large .* off_t")
+then
+ test_set_prereq OFF64_T
+else
+ say "# skipping tests concerning 64-bit offsets"
+fi
+
+test_expect_success OFF64_T 'index v2: verify a pack with some 64-bit offsets' '
+ git verify-pack -v "test-3-${pack3}.pack"
+'
+
+test_expect_success OFF64_T '64-bit offsets: should be different from previous index v2 results' '
+ ! cmp "test-2-${pack2}.idx" "test-3-${pack3}.idx"
+'
+
+test_expect_success OFF64_T 'index v2: force some 64-bit offsets with index-pack' '
+ git index-pack --index-version=2,0x40000 -o 3.idx "test-1-${pack1}.pack"
+'
+
+test_expect_success OFF64_T '64-bit offsets: index-pack result should match pack-objects one' '
+ cmp "test-3-${pack3}.idx" "3.idx"
+'
+
+test_expect_success OFF64_T 'index-pack --verify on 64-bit offset v2 (cheat)' '
+ # This cheats by knowing which lower offset should still be encoded
+ # in 64-bit representation.
+ git index-pack --verify --index-version=2,0x40000 "test-3-${pack3}.pack"
+'
+
+test_expect_success OFF64_T 'index-pack --verify on 64-bit offset v2' '
+ git index-pack --verify "test-3-${pack3}.pack"
+'
+
+# returns the object number for given object in given pack index
+index_obj_nr()
+{
+ idx_file=$1
+ object_sha1=$2
+ nr=0
+ git show-index < $idx_file |
+ while read offs sha1 extra
+ do
+ nr=$(($nr + 1))
+ test "$sha1" = "$object_sha1" || continue
+ echo "$(($nr - 1))"
+ break
+ done
+}
+
+# returns the pack offset for given object as found in given pack index
+index_obj_offset()
+{
+ idx_file=$1
+ object_sha1=$2
+ git show-index < $idx_file | grep $object_sha1 |
+ ( read offs extra && echo "$offs" )
+}
+
+test_expect_success '[index v1] 1) stream pack to repository' '
+ git index-pack --index-version=1 --stdin < "test-1-${pack1}.pack" &&
+ git prune-packed &&
+ git count-objects | ( read nr rest && test "$nr" -eq 1 ) &&
+ cmp "test-1-${pack1}.pack" ".git/objects/pack/pack-${pack1}.pack" &&
+ cmp "test-1-${pack1}.idx" ".git/objects/pack/pack-${pack1}.idx"
+'
+
+test_expect_success \
+ '[index v1] 2) create a stealth corruption in a delta base reference' '
+ # This test assumes file_101 is a delta smaller than 16 bytes.
+ # It should be against file_100 but we substitute its base for file_099
+ sha1_101=$(git hash-object file_101) &&
+ sha1_099=$(git hash-object file_099) &&
+ offs_101=$(index_obj_offset 1.idx $sha1_101) &&
+ nr_099=$(index_obj_nr 1.idx $sha1_099) &&
+ chmod +w ".git/objects/pack/pack-${pack1}.pack" &&
+ recordsz=$((rawsz + 4)) &&
+ dd of=".git/objects/pack/pack-${pack1}.pack" seek=$(($offs_101 + 1)) \
+ if=".git/objects/pack/pack-${pack1}.idx" \
+ skip=$((4 + 256 * 4 + $nr_099 * recordsz)) \
+ bs=1 count=$rawsz conv=notrunc &&
+ git cat-file blob $sha1_101 > file_101_foo1
+'
+
+test_expect_success \
+ '[index v1] 3) corrupted delta happily returned wrong data' '
+ test -f file_101_foo1 && ! cmp file_101 file_101_foo1
+'
+
+test_expect_success \
+ '[index v1] 4) confirm that the pack is actually corrupted' '
+ test_must_fail git fsck --full $commit
+'
+
+test_expect_success \
+ '[index v1] 5) pack-objects happily reuses corrupted data' '
+ pack4=$(git pack-objects test-4 <obj-list) &&
+ test -f "test-4-${pack4}.pack"
+'
+
+test_expect_success '[index v1] 6) newly created pack is BAD !' '
+ test_must_fail git verify-pack -v "test-4-${pack4}.pack"
+'
+
+test_expect_success '[index v2] 1) stream pack to repository' '
+ rm -f .git/objects/pack/* &&
+ git index-pack --index-version=2 --stdin < "test-1-${pack1}.pack" &&
+ git prune-packed &&
+ git count-objects | ( read nr rest && test "$nr" -eq 1 ) &&
+ cmp "test-1-${pack1}.pack" ".git/objects/pack/pack-${pack1}.pack" &&
+ cmp "test-2-${pack1}.idx" ".git/objects/pack/pack-${pack1}.idx"
+'
+
+test_expect_success \
+ '[index v2] 2) create a stealth corruption in a delta base reference' '
+ # This test assumes file_101 is a delta smaller than 16 bytes.
+ # It should be against file_100 but we substitute its base for file_099
+ sha1_101=$(git hash-object file_101) &&
+ sha1_099=$(git hash-object file_099) &&
+ offs_101=$(index_obj_offset 1.idx $sha1_101) &&
+ nr_099=$(index_obj_nr 1.idx $sha1_099) &&
+ chmod +w ".git/objects/pack/pack-${pack1}.pack" &&
+ dd of=".git/objects/pack/pack-${pack1}.pack" seek=$(($offs_101 + 1)) \
+ if=".git/objects/pack/pack-${pack1}.idx" \
+ skip=$((8 + 256 * 4 + $nr_099 * rawsz)) \
+ bs=1 count=$rawsz conv=notrunc &&
+ git cat-file blob $sha1_101 > file_101_foo2
+'
+
+test_expect_success \
+ '[index v2] 3) corrupted delta happily returned wrong data' '
+ test -f file_101_foo2 && ! cmp file_101 file_101_foo2
+'
+
+test_expect_success \
+ '[index v2] 4) confirm that the pack is actually corrupted' '
+ test_must_fail git fsck --full $commit
+'
+
+test_expect_success \
+ '[index v2] 5) pack-objects refuses to reuse corrupted data' '
+ test_must_fail git pack-objects test-5 <obj-list &&
+ test_must_fail git pack-objects --no-reuse-object test-6 <obj-list
+'
+
+test_expect_success \
+ '[index v2] 6) verify-pack detects CRC mismatch' '
+ rm -f .git/objects/pack/* &&
+ git index-pack --index-version=2 --stdin < "test-1-${pack1}.pack" &&
+ git verify-pack ".git/objects/pack/pack-${pack1}.pack" &&
+ obj=$(git hash-object file_001) &&
+ nr=$(index_obj_nr ".git/objects/pack/pack-${pack1}.idx" $obj) &&
+ chmod +w ".git/objects/pack/pack-${pack1}.idx" &&
+ printf xxxx | dd of=".git/objects/pack/pack-${pack1}.idx" conv=notrunc \
+ bs=1 count=4 seek=$((8 + 256 * 4 + $(wc -l <obj-list) * rawsz + $nr * 4)) &&
+ ( while read obj
+ do git cat-file -p $obj >/dev/null || exit 1
+ done <obj-list ) &&
+ test_must_fail git verify-pack ".git/objects/pack/pack-${pack1}.pack"
+'
+
+test_expect_success 'running index-pack in the object store' '
+ rm -f .git/objects/pack/* &&
+ cp test-1-${pack1}.pack .git/objects/pack/pack-${pack1}.pack &&
+ (
+ cd .git/objects/pack &&
+ git index-pack pack-${pack1}.pack
+ ) &&
+ test -f .git/objects/pack/pack-${pack1}.idx
+'
+
+test_expect_success 'index-pack --strict warns upon missing tagger in tag' '
+ sha=$(git rev-parse HEAD) &&
+ cat >wrong-tag <<EOF &&
+object $sha
+type commit
+tag guten tag
+
+This is an invalid tag.
+EOF
+
+ tag=$(git hash-object -t tag -w --stdin --literally <wrong-tag) &&
+ pack1=$(echo $tag $sha | git pack-objects tag-test) &&
+ echo remove tag object &&
+ thirtyeight=${tag#??} &&
+ rm -f .git/objects/${tag%$thirtyeight}/$thirtyeight &&
+ git index-pack --strict tag-test-${pack1}.pack 2>err &&
+ grep "^warning:.* expected .tagger. line" err
+'
+
+test_expect_success 'index-pack --fsck-objects also warns upon missing tagger in tag' '
+ git index-pack --fsck-objects tag-test-${pack1}.pack 2>err &&
+ grep "^warning:.* expected .tagger. line" err
+'
+
+test_expect_success 'index-pack -v --stdin produces progress for both phases' '
+ pack=$(git pack-objects --all pack </dev/null) &&
+ GIT_PROGRESS_DELAY=0 git index-pack -v --stdin <pack-$pack.pack 2>err &&
+ test_grep "Receiving objects" err &&
+ test_grep "Resolving deltas" err
+'
+
+test_expect_success 'too-large packs report the breach' '
+ pack=$(git pack-objects --all pack </dev/null) &&
+ sz="$(test_file_size pack-$pack.pack)" &&
+ test "$sz" -gt 20 &&
+ test_must_fail git index-pack --max-input-size=20 pack-$pack.pack 2>err &&
+ grep "maximum allowed size (20 bytes)" err
+'
+
+test_done
diff --git a/t/t5303-pack-corruption-resilience.sh b/t/t5303-pack-corruption-resilience.sh
new file mode 100755
index 0000000..61469ef
--- /dev/null
+++ b/t/t5303-pack-corruption-resilience.sh
@@ -0,0 +1,405 @@
+#!/bin/sh
+#
+# Copyright (c) 2008 Nicolas Pitre
+#
+
+test_description='resilience to pack corruptions with redundant objects'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# Note: the test objects are created with knowledge of their pack encoding
+# to ensure good code path coverage, and to facilitate direct alteration
+# later on. The assumed characteristics are:
+#
+# 1) blob_2 is a delta with blob_1 for base and blob_3 is a delta with blob2
+# for base, such that blob_3 delta depth is 2;
+#
+# 2) the bulk of object data is uncompressible so the text part remains
+# visible;
+#
+# 3) object header is always 2 bytes.
+
+create_test_files() {
+ test-tool genrandom "foo" 2000 > file_1 &&
+ test-tool genrandom "foo" 1800 > file_2 &&
+ test-tool genrandom "foo" 1800 > file_3 &&
+ echo " base " >> file_1 &&
+ echo " delta1 " >> file_2 &&
+ echo " delta delta2 " >> file_3 &&
+ test-tool genrandom "bar" 150 >> file_2 &&
+ test-tool genrandom "baz" 100 >> file_3
+}
+
+create_new_pack() {
+ rm -rf .git &&
+ git init &&
+ blob_1=$(git hash-object -t blob -w file_1) &&
+ blob_2=$(git hash-object -t blob -w file_2) &&
+ blob_3=$(git hash-object -t blob -w file_3) &&
+ pack=$(printf "$blob_1\n$blob_2\n$blob_3\n" |
+ git pack-objects $@ .git/objects/pack/pack) &&
+ pack=".git/objects/pack/pack-${pack}" &&
+ git verify-pack -v ${pack}.pack
+}
+
+do_repack() {
+ pack=$(printf "$blob_1\n$blob_2\n$blob_3\n" |
+ git pack-objects $@ .git/objects/pack/pack) &&
+ pack=".git/objects/pack/pack-${pack}"
+}
+
+do_corrupt_object() {
+ ofs=$(git show-index < ${pack}.idx | grep $1 | cut -f1 -d" ") &&
+ ofs=$(($ofs + $2)) &&
+ chmod +w ${pack}.pack &&
+ dd of=${pack}.pack bs=1 conv=notrunc seek=$ofs &&
+ test_must_fail git verify-pack ${pack}.pack
+}
+
+printf '\0' > zero
+
+test_expect_success 'initial setup validation' '
+ create_test_files &&
+ create_new_pack &&
+ git prune-packed &&
+ git cat-file blob $blob_1 > /dev/null &&
+ git cat-file blob $blob_2 > /dev/null &&
+ git cat-file blob $blob_3 > /dev/null
+'
+
+test_expect_success 'create corruption in header of first object' '
+ do_corrupt_object $blob_1 0 < zero &&
+ test_must_fail git cat-file blob $blob_1 > /dev/null &&
+ test_must_fail git cat-file blob $blob_2 > /dev/null &&
+ test_must_fail git cat-file blob $blob_3 > /dev/null
+'
+
+test_expect_success '... but having a loose copy allows for full recovery' '
+ mv ${pack}.idx tmp &&
+ git hash-object -t blob -w file_1 &&
+ mv tmp ${pack}.idx &&
+ git cat-file blob $blob_1 > /dev/null &&
+ git cat-file blob $blob_2 > /dev/null &&
+ git cat-file blob $blob_3 > /dev/null
+'
+
+test_expect_success '... and loose copy of first delta allows for partial recovery' '
+ git prune-packed &&
+ test_must_fail git cat-file blob $blob_2 > /dev/null &&
+ mv ${pack}.idx tmp &&
+ git hash-object -t blob -w file_2 &&
+ mv tmp ${pack}.idx &&
+ test_must_fail git cat-file blob $blob_1 > /dev/null &&
+ git cat-file blob $blob_2 > /dev/null &&
+ git cat-file blob $blob_3 > /dev/null
+'
+
+test_expect_success 'create corruption in data of first object' '
+ create_new_pack &&
+ git prune-packed &&
+ chmod +w ${pack}.pack &&
+ perl -i.bak -pe "s/ base /abcdef/" ${pack}.pack &&
+ test_must_fail git cat-file blob $blob_1 > /dev/null &&
+ test_must_fail git cat-file blob $blob_2 > /dev/null &&
+ test_must_fail git cat-file blob $blob_3 > /dev/null
+'
+
+test_expect_success '... but having a loose copy allows for full recovery' '
+ mv ${pack}.idx tmp &&
+ git hash-object -t blob -w file_1 &&
+ mv tmp ${pack}.idx &&
+ git cat-file blob $blob_1 > /dev/null &&
+ git cat-file blob $blob_2 > /dev/null &&
+ git cat-file blob $blob_3 > /dev/null
+'
+
+test_expect_success '... and loose copy of second object allows for partial recovery' '
+ git prune-packed &&
+ test_must_fail git cat-file blob $blob_2 > /dev/null &&
+ mv ${pack}.idx tmp &&
+ git hash-object -t blob -w file_2 &&
+ mv tmp ${pack}.idx &&
+ test_must_fail git cat-file blob $blob_1 > /dev/null &&
+ git cat-file blob $blob_2 > /dev/null &&
+ git cat-file blob $blob_3 > /dev/null
+'
+
+test_expect_success 'create corruption in header of first delta' '
+ create_new_pack &&
+ git prune-packed &&
+ do_corrupt_object $blob_2 0 < zero &&
+ git cat-file blob $blob_1 > /dev/null &&
+ test_must_fail git cat-file blob $blob_2 > /dev/null &&
+ test_must_fail git cat-file blob $blob_3 > /dev/null
+'
+
+test_expect_success '... but having a loose copy allows for full recovery' '
+ mv ${pack}.idx tmp &&
+ git hash-object -t blob -w file_2 &&
+ mv tmp ${pack}.idx &&
+ git cat-file blob $blob_1 > /dev/null &&
+ git cat-file blob $blob_2 > /dev/null &&
+ git cat-file blob $blob_3 > /dev/null
+'
+
+test_expect_success '... and then a repack "clears" the corruption' '
+ do_repack &&
+ git prune-packed &&
+ git verify-pack ${pack}.pack &&
+ git cat-file blob $blob_1 > /dev/null &&
+ git cat-file blob $blob_2 > /dev/null &&
+ git cat-file blob $blob_3 > /dev/null
+'
+
+test_expect_success 'create corruption in data of first delta' '
+ create_new_pack &&
+ git prune-packed &&
+ chmod +w ${pack}.pack &&
+ perl -i.bak -pe "s/ delta1 /abcdefgh/" ${pack}.pack &&
+ git cat-file blob $blob_1 > /dev/null &&
+ test_must_fail git cat-file blob $blob_2 > /dev/null &&
+ test_must_fail git cat-file blob $blob_3 > /dev/null
+'
+
+test_expect_success '... but having a loose copy allows for full recovery' '
+ mv ${pack}.idx tmp &&
+ git hash-object -t blob -w file_2 &&
+ mv tmp ${pack}.idx &&
+ git cat-file blob $blob_1 > /dev/null &&
+ git cat-file blob $blob_2 > /dev/null &&
+ git cat-file blob $blob_3 > /dev/null
+'
+
+test_expect_success '... and then a repack "clears" the corruption' '
+ do_repack &&
+ git prune-packed &&
+ git verify-pack ${pack}.pack &&
+ git cat-file blob $blob_1 > /dev/null &&
+ git cat-file blob $blob_2 > /dev/null &&
+ git cat-file blob $blob_3 > /dev/null
+'
+
+test_expect_success 'corruption in delta base reference of first delta (OBJ_REF_DELTA)' '
+ create_new_pack &&
+ git prune-packed &&
+ do_corrupt_object $blob_2 2 < zero &&
+ git cat-file blob $blob_1 > /dev/null &&
+ test_must_fail git cat-file blob $blob_2 > /dev/null &&
+ test_must_fail git cat-file blob $blob_3 > /dev/null
+'
+
+test_expect_success '... but having a loose copy allows for full recovery' '
+ mv ${pack}.idx tmp &&
+ git hash-object -t blob -w file_2 &&
+ mv tmp ${pack}.idx &&
+ git cat-file blob $blob_1 > /dev/null &&
+ git cat-file blob $blob_2 > /dev/null &&
+ git cat-file blob $blob_3 > /dev/null
+'
+
+test_expect_success '... and then a repack "clears" the corruption' '
+ do_repack &&
+ git prune-packed &&
+ git verify-pack ${pack}.pack &&
+ git cat-file blob $blob_1 > /dev/null &&
+ git cat-file blob $blob_2 > /dev/null &&
+ git cat-file blob $blob_3 > /dev/null
+'
+
+test_expect_success 'corruption #0 in delta base reference of first delta (OBJ_OFS_DELTA)' '
+ create_new_pack --delta-base-offset &&
+ git prune-packed &&
+ do_corrupt_object $blob_2 2 < zero &&
+ git cat-file blob $blob_1 > /dev/null &&
+ test_must_fail git cat-file blob $blob_2 > /dev/null &&
+ test_must_fail git cat-file blob $blob_3 > /dev/null
+'
+
+test_expect_success '... but having a loose copy allows for full recovery' '
+ mv ${pack}.idx tmp &&
+ git hash-object -t blob -w file_2 &&
+ mv tmp ${pack}.idx &&
+ git cat-file blob $blob_1 > /dev/null &&
+ git cat-file blob $blob_2 > /dev/null &&
+ git cat-file blob $blob_3 > /dev/null
+'
+
+test_expect_success '... and then a repack "clears" the corruption' '
+ do_repack --delta-base-offset &&
+ git prune-packed &&
+ git verify-pack ${pack}.pack &&
+ git cat-file blob $blob_1 > /dev/null &&
+ git cat-file blob $blob_2 > /dev/null &&
+ git cat-file blob $blob_3 > /dev/null
+'
+
+test_expect_success 'corruption #1 in delta base reference of first delta (OBJ_OFS_DELTA)' '
+ create_new_pack --delta-base-offset &&
+ git prune-packed &&
+ printf "\001" | do_corrupt_object $blob_2 2 &&
+ git cat-file blob $blob_1 > /dev/null &&
+ test_must_fail git cat-file blob $blob_2 > /dev/null &&
+ test_must_fail git cat-file blob $blob_3 > /dev/null
+'
+
+test_expect_success '... but having a loose copy allows for full recovery' '
+ mv ${pack}.idx tmp &&
+ git hash-object -t blob -w file_2 &&
+ mv tmp ${pack}.idx &&
+ git cat-file blob $blob_1 > /dev/null &&
+ git cat-file blob $blob_2 > /dev/null &&
+ git cat-file blob $blob_3 > /dev/null
+'
+
+test_expect_success '... and then a repack "clears" the corruption' '
+ do_repack --delta-base-offset &&
+ git prune-packed &&
+ git verify-pack ${pack}.pack &&
+ git cat-file blob $blob_1 > /dev/null &&
+ git cat-file blob $blob_2 > /dev/null &&
+ git cat-file blob $blob_3 > /dev/null
+'
+
+test_expect_success '... and a redundant pack allows for full recovery too' '
+ do_corrupt_object $blob_2 2 < zero &&
+ git cat-file blob $blob_1 > /dev/null &&
+ test_must_fail git cat-file blob $blob_2 > /dev/null &&
+ test_must_fail git cat-file blob $blob_3 > /dev/null &&
+ mv ${pack}.idx tmp &&
+ git hash-object -t blob -w file_1 &&
+ git hash-object -t blob -w file_2 &&
+ printf "$blob_1\n$blob_2\n" | git pack-objects .git/objects/pack/pack &&
+ git prune-packed &&
+ mv tmp ${pack}.idx &&
+ git cat-file blob $blob_1 > /dev/null &&
+ git cat-file blob $blob_2 > /dev/null &&
+ git cat-file blob $blob_3 > /dev/null
+'
+
+test_expect_success 'corruption of delta base reference pointing to wrong object' '
+ create_new_pack --delta-base-offset &&
+ git prune-packed &&
+ printf "\220\033" | do_corrupt_object $blob_3 2 &&
+ git cat-file blob $blob_1 >/dev/null &&
+ git cat-file blob $blob_2 >/dev/null &&
+ test_must_fail git cat-file blob $blob_3 >/dev/null
+'
+
+test_expect_success '... but having a loose copy allows for full recovery' '
+ mv ${pack}.idx tmp &&
+ git hash-object -t blob -w file_3 &&
+ mv tmp ${pack}.idx &&
+ git cat-file blob $blob_1 > /dev/null &&
+ git cat-file blob $blob_2 > /dev/null &&
+ git cat-file blob $blob_3 > /dev/null
+'
+
+test_expect_success '... and then a repack "clears" the corruption' '
+ do_repack --delta-base-offset --no-reuse-delta &&
+ git prune-packed &&
+ git verify-pack ${pack}.pack &&
+ git cat-file blob $blob_1 > /dev/null &&
+ git cat-file blob $blob_2 > /dev/null &&
+ git cat-file blob $blob_3 > /dev/null
+'
+
+test_expect_success 'corrupting header to have too small output buffer fails unpack' '
+ create_new_pack &&
+ git prune-packed &&
+ printf "\262\001" | do_corrupt_object $blob_1 0 &&
+ test_must_fail git cat-file blob $blob_1 > /dev/null &&
+ test_must_fail git cat-file blob $blob_2 > /dev/null &&
+ test_must_fail git cat-file blob $blob_3 > /dev/null
+'
+
+# \0 - empty base
+# \1 - one byte in result
+# \1 - one literal byte (X)
+test_expect_success 'apply good minimal delta' '
+ printf "\0\1\1X" > minimal_delta &&
+ test-tool delta -p /dev/null minimal_delta /dev/null
+'
+
+# \0 - empty base
+# \1 - 1 byte in result
+# \2 - two literal bytes (one too many)
+test_expect_success 'apply delta with too many literal bytes' '
+ printf "\0\1\2XX" > too_big_literal &&
+ test_must_fail test-tool delta -p /dev/null too_big_literal /dev/null
+'
+
+# \4 - four bytes in base
+# \1 - one byte in result
+# \221 - copy, one byte offset, one byte size
+# \0 - copy from offset 0
+# \2 - copy two bytes (one too many)
+test_expect_success 'apply delta with too many copied bytes' '
+ printf "\4\1\221\0\2" > too_big_copy &&
+ printf base >base &&
+ test_must_fail test-tool delta -p base too_big_copy /dev/null
+'
+
+# \0 - empty base
+# \2 - two bytes in result
+# \2 - two literal bytes (we are short one)
+test_expect_success 'apply delta with too few literal bytes' '
+ printf "\0\2\2X" > truncated_delta &&
+ test_must_fail test-tool delta -p /dev/null truncated_delta /dev/null
+'
+
+# \0 - empty base
+# \1 - one byte in result
+# \221 - copy, one byte offset, one byte size
+# \0 - copy from offset 0
+# \1 - copy one byte (we are short one)
+test_expect_success 'apply delta with too few bytes in base' '
+ printf "\0\1\221\0\1" > truncated_base &&
+ test_must_fail test-tool delta -p /dev/null truncated_base /dev/null
+'
+
+# \4 - four bytes in base
+# \2 - two bytes in result
+# \1 - one literal byte (X)
+# \221 - copy, one byte offset, one byte size
+# (offset/size missing)
+#
+# Note that the literal byte is necessary to get past the uninteresting minimum
+# delta size check.
+test_expect_success 'apply delta with truncated copy parameters' '
+ printf "\4\2\1X\221" > truncated_copy_delta &&
+ printf base >base &&
+ test_must_fail test-tool delta -p base truncated_copy_delta /dev/null
+'
+
+# \0 - empty base
+# \1 - one byte in result
+# \1 - one literal byte (X)
+# \1 - trailing garbage command
+test_expect_success 'apply delta with trailing garbage literal' '
+ printf "\0\1\1X\1" > tail_garbage_literal &&
+ test_must_fail test-tool delta -p /dev/null tail_garbage_literal /dev/null
+'
+
+# \4 - four bytes in base
+# \1 - one byte in result
+# \1 - one literal byte (X)
+# \221 - copy, one byte offset, one byte size
+# \0 - copy from offset 0
+# \1 - copy 1 byte
+test_expect_success 'apply delta with trailing garbage copy' '
+ printf "\4\1\1X\221\0\1" > tail_garbage_copy &&
+ printf base >base &&
+ test_must_fail test-tool delta -p /dev/null tail_garbage_copy /dev/null
+'
+
+# \0 - empty base
+# \1 - one byte in result
+# \1 - one literal byte (X)
+# \0 - bogus opcode
+test_expect_success 'apply delta with trailing garbage opcode' '
+ printf "\0\1\1X\0" > tail_garbage_opcode &&
+ test_must_fail test-tool delta -p /dev/null tail_garbage_opcode /dev/null
+'
+
+test_done
diff --git a/t/t5304-prune.sh b/t/t5304-prune.sh
new file mode 100755
index 0000000..1f1f664
--- /dev/null
+++ b/t/t5304-prune.sh
@@ -0,0 +1,367 @@
+#!/bin/sh
+#
+# Copyright (c) 2008 Johannes E. Schindelin
+#
+
+test_description='prune'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+day=$((60*60*24))
+week=$(($day*7))
+
+add_blob() {
+ before=$(git count-objects | sed "s/ .*//") &&
+ BLOB=$(echo aleph_0 | git hash-object -w --stdin) &&
+ BLOB_FILE=.git/objects/$(echo $BLOB | sed "s/^../&\//") &&
+ test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
+ test_path_is_file $BLOB_FILE &&
+ test-tool chmtime =+0 $BLOB_FILE
+}
+
+test_expect_success setup '
+ >file &&
+ git add file &&
+ test_tick &&
+ git commit -m initial &&
+ git gc
+'
+
+test_expect_success 'bare repo prune is quiet without $GIT_DIR/objects/pack' '
+ git clone -q --shared --template= --bare . bare.git &&
+ rmdir bare.git/objects/pack &&
+ git --git-dir=bare.git prune --no-progress 2>prune.err &&
+ test_must_be_empty prune.err &&
+ rm -r bare.git prune.err
+'
+
+test_expect_success 'prune stale packs' '
+ orig_pack=$(echo .git/objects/pack/*.pack) &&
+ >.git/objects/tmp_1.pack &&
+ >.git/objects/tmp_2.pack &&
+ test-tool chmtime =-86501 .git/objects/tmp_1.pack &&
+ git prune --expire 1.day &&
+ test_path_is_file $orig_pack &&
+ test_path_is_file .git/objects/tmp_2.pack &&
+ test_path_is_missing .git/objects/tmp_1.pack
+'
+
+test_expect_success 'prune --expire' '
+ add_blob &&
+ git prune --expire=1.hour.ago &&
+ test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
+ test_path_is_file $BLOB_FILE &&
+ test-tool chmtime =-86500 $BLOB_FILE &&
+ git prune --expire 1.day &&
+ test $before = $(git count-objects | sed "s/ .*//") &&
+ test_path_is_missing $BLOB_FILE
+'
+
+test_expect_success 'gc: implicit prune --expire' '
+ add_blob &&
+ test-tool chmtime =-$((2*$week-30)) $BLOB_FILE &&
+ git gc --no-cruft &&
+ test $((1 + $before)) = $(git count-objects | sed "s/ .*//") &&
+ test_path_is_file $BLOB_FILE &&
+ test-tool chmtime =-$((2*$week+1)) $BLOB_FILE &&
+ git gc --no-cruft &&
+ test $before = $(git count-objects | sed "s/ .*//") &&
+ test_path_is_missing $BLOB_FILE
+'
+
+test_expect_success 'gc: refuse to start with invalid gc.pruneExpire' '
+ test_when_finished "rm -rf repo" &&
+ git init repo &&
+ >repo/.git/config &&
+ git -C repo config gc.pruneExpire invalid &&
+ cat >expect <<-\EOF &&
+ error: Invalid gc.pruneexpire: '\''invalid'\''
+ fatal: bad config variable '\''gc.pruneexpire'\'' in file '\''.git/config'\'' at line 2
+ EOF
+ test_must_fail git -C repo gc 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'gc: start with ok gc.pruneExpire' '
+ git config gc.pruneExpire 2.days.ago &&
+ git gc --no-cruft
+'
+
+test_expect_success 'prune: prune nonsense parameters' '
+ test_must_fail git prune garbage &&
+ test_must_fail git prune --- &&
+ test_must_fail git prune --no-such-option
+'
+
+test_expect_success 'prune: prune unreachable heads' '
+ git config core.logAllRefUpdates false &&
+ >file2 &&
+ git add file2 &&
+ git commit -m temporary &&
+ tmp_head=$(git rev-list -1 HEAD) &&
+ git reset HEAD^ &&
+ git reflog expire --all &&
+ git prune &&
+ test_must_fail git reset $tmp_head --
+'
+
+test_expect_success 'prune: do not prune detached HEAD with no reflog' '
+ git checkout --detach --quiet &&
+ git commit --allow-empty -m "detached commit" &&
+ git reflog expire --all &&
+ git prune -n >prune_actual &&
+ test_must_be_empty prune_actual
+'
+
+test_expect_success 'prune: prune former HEAD after checking out branch' '
+ head_oid=$(git rev-parse HEAD) &&
+ git checkout --quiet main &&
+ git reflog expire --all &&
+ git prune -v >prune_actual &&
+ grep "$head_oid" prune_actual
+'
+
+test_expect_success 'prune: do not prune heads listed as an argument' '
+ >file2 &&
+ git add file2 &&
+ git commit -m temporary &&
+ tmp_head=$(git rev-list -1 HEAD) &&
+ git reset HEAD^ &&
+ git prune -- $tmp_head &&
+ git reset $tmp_head --
+'
+
+test_expect_success 'gc --no-prune' '
+ add_blob &&
+ test-tool chmtime =-$((5001*$day)) $BLOB_FILE &&
+ git config gc.pruneExpire 2.days.ago &&
+ git gc --no-prune --no-cruft &&
+ test 1 = $(git count-objects | sed "s/ .*//") &&
+ test_path_is_file $BLOB_FILE
+'
+
+test_expect_success 'gc respects gc.pruneExpire' '
+ git config gc.pruneExpire 5002.days.ago &&
+ git gc --no-cruft &&
+ test_path_is_file $BLOB_FILE &&
+ git config gc.pruneExpire 5000.days.ago &&
+ git gc --no-cruft &&
+ test_path_is_missing $BLOB_FILE
+'
+
+test_expect_success 'gc --prune=<date>' '
+ add_blob &&
+ test-tool chmtime =-$((5001*$day)) $BLOB_FILE &&
+ git gc --prune=5002.days.ago --no-cruft &&
+ test_path_is_file $BLOB_FILE &&
+ git gc --prune=5000.days.ago --no-cruft &&
+ test_path_is_missing $BLOB_FILE
+'
+
+test_expect_success 'gc --prune=never' '
+ add_blob &&
+ git gc --prune=never --no-cruft &&
+ test_path_is_file $BLOB_FILE &&
+ git gc --prune=now --no-cruft &&
+ test_path_is_missing $BLOB_FILE
+'
+
+test_expect_success 'gc respects gc.pruneExpire=never' '
+ git config gc.pruneExpire never &&
+ add_blob &&
+ git gc --no-cruft &&
+ test_path_is_file $BLOB_FILE &&
+ git config gc.pruneExpire now &&
+ git gc --no-cruft &&
+ test_path_is_missing $BLOB_FILE
+'
+
+test_expect_success 'prune --expire=never' '
+ add_blob &&
+ git prune --expire=never &&
+ test_path_is_file $BLOB_FILE &&
+ git prune &&
+ test_path_is_missing $BLOB_FILE
+'
+
+test_expect_success 'gc: prune old objects after local clone' '
+ add_blob &&
+ test-tool chmtime =-$((2*$week+1)) $BLOB_FILE &&
+ git clone --no-hardlinks . aclone &&
+ (
+ cd aclone &&
+ test 1 = $(git count-objects | sed "s/ .*//") &&
+ test_path_is_file $BLOB_FILE &&
+ git gc --prune --no-cruft &&
+ test 0 = $(git count-objects | sed "s/ .*//") &&
+ test_path_is_missing $BLOB_FILE
+ )
+'
+
+test_expect_success 'garbage report in count-objects -v' '
+ test_when_finished "rm -f .git/objects/pack/fake*" &&
+ test_when_finished "rm -f .git/objects/pack/foo*" &&
+ >.git/objects/pack/foo &&
+ >.git/objects/pack/foo.bar &&
+ >.git/objects/pack/foo.keep &&
+ >.git/objects/pack/foo.pack &&
+ >.git/objects/pack/fake.bar &&
+ >.git/objects/pack/fake.keep &&
+ >.git/objects/pack/fake.pack &&
+ >.git/objects/pack/fake.idx &&
+ >.git/objects/pack/fake2.keep &&
+ >.git/objects/pack/fake3.idx &&
+ git count-objects -v 2>stderr &&
+ grep "index file .git/objects/pack/fake.idx is too small" stderr &&
+ grep "^warning:" stderr | sort >actual &&
+ cat >expected <<\EOF &&
+warning: garbage found: .git/objects/pack/fake.bar
+warning: garbage found: .git/objects/pack/foo
+warning: garbage found: .git/objects/pack/foo.bar
+warning: no corresponding .idx or .pack: .git/objects/pack/fake2.keep
+warning: no corresponding .idx: .git/objects/pack/foo.keep
+warning: no corresponding .idx: .git/objects/pack/foo.pack
+warning: no corresponding .pack: .git/objects/pack/fake3.idx
+EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'clean pack garbage with gc' '
+ test_when_finished "rm -f .git/objects/pack/fake*" &&
+ test_when_finished "rm -f .git/objects/pack/foo*" &&
+ >.git/objects/pack/foo.keep &&
+ >.git/objects/pack/foo.pack &&
+ >.git/objects/pack/fake.idx &&
+ >.git/objects/pack/fake2.keep &&
+ >.git/objects/pack/fake2.idx &&
+ >.git/objects/pack/fake3.keep &&
+ git gc --no-cruft &&
+ git count-objects -v 2>stderr &&
+ grep "^warning:" stderr | sort >actual &&
+ cat >expected <<\EOF &&
+warning: no corresponding .idx or .pack: .git/objects/pack/fake3.keep
+warning: no corresponding .idx: .git/objects/pack/foo.keep
+warning: no corresponding .idx: .git/objects/pack/foo.pack
+EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'prune .git/shallow' '
+ oid=$(echo hi|git commit-tree HEAD^{tree}) &&
+ echo $oid >.git/shallow &&
+ git prune --dry-run >out &&
+ grep $oid .git/shallow &&
+ grep $oid out &&
+ git prune &&
+ test_path_is_missing .git/shallow
+'
+
+test_expect_success 'prune .git/shallow when there are no loose objects' '
+ oid=$(echo hi|git commit-tree HEAD^{tree}) &&
+ echo $oid >.git/shallow &&
+ git update-ref refs/heads/shallow-tip $oid &&
+ git repack -ad &&
+ # verify assumption that all loose objects are gone
+ git count-objects | grep ^0 &&
+ git prune &&
+ echo $oid >expect &&
+ test_cmp expect .git/shallow
+'
+
+test_expect_success 'prune: handle alternate object database' '
+ test_create_repo A &&
+ git -C A commit --allow-empty -m "initial commit" &&
+ git clone --shared A B &&
+ git -C B commit --allow-empty -m "next commit" &&
+ git -C B prune
+'
+
+test_expect_success 'prune: handle index in multiple worktrees' '
+ git worktree add second-worktree &&
+ echo "new blob for second-worktree" >second-worktree/blob &&
+ git -C second-worktree add blob &&
+ git prune --expire=now &&
+ git -C second-worktree show :blob >actual &&
+ test_cmp second-worktree/blob actual
+'
+
+test_expect_success 'prune: handle HEAD in multiple worktrees' '
+ git worktree add --detach third-worktree &&
+ echo "new blob for third-worktree" >third-worktree/blob &&
+ git -C third-worktree add blob &&
+ git -C third-worktree commit -m "third" &&
+ rm .git/worktrees/third-worktree/index &&
+ test_must_fail git -C third-worktree show :blob &&
+ git prune --expire=now &&
+ git -C third-worktree show HEAD:blob >actual &&
+ test_cmp third-worktree/blob actual
+'
+
+test_expect_success 'prune: handle HEAD reflog in multiple worktrees' '
+ git config core.logAllRefUpdates true &&
+ echo "lost blob for third-worktree" >expected &&
+ (
+ cd third-worktree &&
+ cat ../expected >blob &&
+ git add blob &&
+ git commit -m "second commit in third" &&
+ git clean -f && # Remove untracked left behind by deleting index
+ git reset --hard HEAD^
+ ) &&
+ git prune --expire=now &&
+ oid=`git hash-object expected` &&
+ git -C third-worktree show "$oid" >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'prune: handle expire option correctly' '
+ test_must_fail git prune --expire 2>error &&
+ test_grep "requires a value" error &&
+
+ test_must_fail git prune --expire=nyah 2>error &&
+ test_grep "malformed expiration" error &&
+
+ git prune --no-expire
+'
+
+test_expect_success 'trivial prune with bitmaps enabled' '
+ git repack -adb &&
+ blob=$(echo bitmap-unreachable-blob | git hash-object -w --stdin) &&
+ git prune --expire=now &&
+ git cat-file -e HEAD &&
+ test_must_fail git cat-file -e $blob
+'
+
+test_expect_success 'old reachable-from-recent retained with bitmaps' '
+ git repack -adb &&
+ to_drop=$(echo bitmap-from-recent-1 | git hash-object -w --stdin) &&
+ test-tool chmtime -86400 .git/objects/$(test_oid_to_path $to_drop) &&
+ to_save=$(echo bitmap-from-recent-2 | git hash-object -w --stdin) &&
+ test-tool chmtime -86400 .git/objects/$(test_oid_to_path $to_save) &&
+ tree=$(printf "100644 blob $to_save\tfile\n" | git mktree) &&
+ test-tool chmtime -86400 .git/objects/$(test_oid_to_path $tree) &&
+ commit=$(echo foo | git commit-tree $tree) &&
+ git prune --expire=12.hours.ago &&
+ git cat-file -e $commit &&
+ git cat-file -e $tree &&
+ git cat-file -e $to_save &&
+ test_must_fail git cat-file -e $to_drop
+'
+
+test_expect_success 'gc.recentObjectsHook' '
+ add_blob &&
+ test-tool chmtime =-86500 $BLOB_FILE &&
+
+ write_script precious-objects <<-EOF &&
+ echo $BLOB
+ EOF
+ test_config gc.recentObjectsHook ./precious-objects &&
+
+ git prune --expire=now &&
+
+ git cat-file -p $BLOB
+'
+
+test_done
diff --git a/t/t5305-include-tag.sh b/t/t5305-include-tag.sh
new file mode 100755
index 0000000..44bd9ef
--- /dev/null
+++ b/t/t5305-include-tag.sh
@@ -0,0 +1,121 @@
+#!/bin/sh
+
+test_description='git pack-object --include-tag'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+TRASH=$(pwd)
+
+test_expect_success setup '
+ echo c >d &&
+ git update-index --add d &&
+ tree=$(git write-tree) &&
+ commit=$(git commit-tree $tree </dev/null) &&
+ echo "object $commit" >sig &&
+ echo "type commit" >>sig &&
+ echo "tag mytag" >>sig &&
+ echo "tagger $(git var GIT_COMMITTER_IDENT)" >>sig &&
+ echo >>sig &&
+ echo "our test tag" >>sig &&
+ tag=$(git mktag <sig) &&
+ rm d sig &&
+ git update-ref refs/tags/mytag $tag && {
+ echo $tree &&
+ echo $commit &&
+ git ls-tree $tree | sed -e "s/.* \\([0-9a-f]*\\) .*/\\1/"
+ } >obj-list
+'
+
+test_expect_success 'pack without --include-tag' '
+ packname=$(git pack-objects \
+ --window=0 \
+ test-no-include <obj-list)
+'
+
+test_expect_success 'unpack objects' '
+ rm -rf clone.git &&
+ git init clone.git &&
+ git -C clone.git unpack-objects <test-no-include-${packname}.pack
+'
+
+test_expect_success 'check unpacked result (have commit, no tag)' '
+ git rev-list --objects $commit >list.expect &&
+ test_must_fail git -C clone.git cat-file -e $tag &&
+ git -C clone.git rev-list --objects $commit >list.actual &&
+ test_cmp list.expect list.actual
+'
+
+test_expect_success 'pack with --include-tag' '
+ packname=$(git pack-objects \
+ --window=0 \
+ --include-tag \
+ test-include <obj-list)
+'
+
+test_expect_success 'unpack objects' '
+ rm -rf clone.git &&
+ git init clone.git &&
+ git -C clone.git unpack-objects <test-include-${packname}.pack
+'
+
+test_expect_success 'check unpacked result (have commit, have tag)' '
+ git rev-list --objects mytag >list.expect &&
+ git -C clone.git rev-list --objects $tag >list.actual &&
+ test_cmp list.expect list.actual
+'
+
+# A tag of a tag, where the "inner" tag is not otherwise
+# reachable, and a full peel points to a commit reachable from HEAD.
+test_expect_success 'create hidden inner tag' '
+ test_commit commit &&
+ git tag -m inner inner HEAD &&
+ git tag -m outer outer inner &&
+ git tag -d inner
+'
+
+test_expect_success 'pack explicit outer tag' '
+ packname=$(
+ {
+ echo HEAD &&
+ echo outer
+ } |
+ git pack-objects --revs test-hidden-explicit
+ )
+'
+
+test_expect_success 'unpack objects' '
+ rm -rf clone.git &&
+ git init clone.git &&
+ git -C clone.git unpack-objects <test-hidden-explicit-${packname}.pack
+'
+
+test_expect_success 'check unpacked result (have all objects)' '
+ git -C clone.git rev-list --objects $(git rev-parse outer HEAD)
+'
+
+test_expect_success 'pack implied outer tag' '
+ packname=$(
+ echo HEAD |
+ git pack-objects --revs --include-tag test-hidden-implied
+ )
+'
+
+test_expect_success 'unpack objects' '
+ rm -rf clone.git &&
+ git init clone.git &&
+ git -C clone.git unpack-objects <test-hidden-implied-${packname}.pack
+'
+
+test_expect_success 'check unpacked result (have all objects)' '
+ git -C clone.git rev-list --objects $(git rev-parse outer HEAD)
+'
+
+test_expect_success 'single-branch clone can transfer tag' '
+ rm -rf clone.git &&
+ git clone --no-local --single-branch -b main . clone.git &&
+ git -C clone.git fsck
+'
+
+test_done
diff --git a/t/t5306-pack-nobase.sh b/t/t5306-pack-nobase.sh
new file mode 100755
index 0000000..0d50c6b
--- /dev/null
+++ b/t/t5306-pack-nobase.sh
@@ -0,0 +1,78 @@
+#!/bin/sh
+#
+# Copyright (c) 2008 Google Inc.
+#
+
+test_description='git-pack-object with missing base
+
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# Create A-B chain
+#
+test_expect_success 'setup base' '
+ test_write_lines a b c d e f g h i >text &&
+ echo side >side &&
+ git update-index --add text side &&
+ A=$(echo A | git commit-tree $(git write-tree)) &&
+
+ echo m >>text &&
+ git update-index text &&
+ B=$(echo B | git commit-tree $(git write-tree) -p $A) &&
+ git update-ref HEAD $B
+'
+
+# Create repository with C whose parent is B.
+# Repository contains C, C^{tree}, C:text, B, B^{tree}.
+# Repository is missing B:text (best delta base for C:text).
+# Repository is missing A (parent of B).
+# Repository is missing A:side.
+#
+test_expect_success 'setup patch_clone' '
+ base_objects=$(pwd)/.git/objects &&
+ (mkdir patch_clone &&
+ cd patch_clone &&
+ git init &&
+ echo "$base_objects" >.git/objects/info/alternates &&
+ echo q >>text &&
+ git read-tree $B &&
+ git update-index text &&
+ git update-ref HEAD $(echo C | git commit-tree $(git write-tree) -p $B) &&
+ rm .git/objects/info/alternates &&
+
+ git --git-dir=../.git cat-file commit $B |
+ git hash-object -t commit -w --stdin &&
+
+ git --git-dir=../.git cat-file tree "$B^{tree}" |
+ git hash-object -t tree -w --stdin
+ ) &&
+ C=$(git --git-dir=patch_clone/.git rev-parse HEAD)
+'
+
+# Clone patch_clone indirectly by cloning base and fetching.
+#
+test_expect_success 'indirectly clone patch_clone' '
+ (mkdir user_clone &&
+ cd user_clone &&
+ git init &&
+ git pull ../.git &&
+ test $(git rev-parse HEAD) = $B &&
+
+ git pull ../patch_clone/.git &&
+ test $(git rev-parse HEAD) = $C
+ )
+'
+
+# Cloning the patch_clone directly should fail.
+#
+test_expect_success 'clone of patch_clone is incomplete' '
+ (mkdir user_direct &&
+ cd user_direct &&
+ git init &&
+ test_must_fail git fetch ../patch_clone/.git
+ )
+'
+
+test_done
diff --git a/t/t5307-pack-missing-commit.sh b/t/t5307-pack-missing-commit.sh
new file mode 100755
index 0000000..1e02c30
--- /dev/null
+++ b/t/t5307-pack-missing-commit.sh
@@ -0,0 +1,40 @@
+#!/bin/sh
+
+test_description='pack should notice missing commit objects'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ for i in 1 2 3 4 5
+ do
+ echo "$i" >"file$i" &&
+ git add "file$i" &&
+ test_tick &&
+ git commit -m "$i" &&
+ git tag "tag$i" || return 1
+ done &&
+ obj=$(git rev-parse --verify tag3) &&
+ fanout=$(expr "$obj" : "\(..\)") &&
+ remainder=$(expr "$obj" : "..\(.*\)") &&
+ rm -f ".git/objects/$fanout/$remainder"
+'
+
+test_expect_success 'check corruption' '
+ test_must_fail git fsck
+'
+
+test_expect_success 'rev-list notices corruption (1)' '
+ test_must_fail env GIT_TEST_COMMIT_GRAPH=0 git -c core.commitGraph=false rev-list HEAD
+'
+
+test_expect_success 'rev-list notices corruption (2)' '
+ test_must_fail env GIT_TEST_COMMIT_GRAPH=0 git -c core.commitGraph=false rev-list --objects HEAD
+'
+
+test_expect_success 'pack-objects notices corruption' '
+ echo HEAD |
+ test_must_fail git pack-objects --revs pack
+'
+
+test_done
diff --git a/t/t5308-pack-detect-duplicates.sh b/t/t5308-pack-detect-duplicates.sh
new file mode 100755
index 0000000..655cafa
--- /dev/null
+++ b/t/t5308-pack-detect-duplicates.sh
@@ -0,0 +1,83 @@
+#!/bin/sh
+
+test_description='handling of duplicate objects in incoming packfiles'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-pack.sh
+
+test_expect_success 'setup' '
+ test_oid_cache <<-EOF
+ lo_oid sha1:e68fe8129b546b101aee9510c5328e7f21ca1d18
+ lo_oid sha256:471819e8c52bf11513f100b2810a8aa0622d5cd3d1c913758a071dd4b3bad8fe
+
+ missing_oid sha1:e69d000000000000000000000000000000000000
+ missing_oid sha256:4720000000000000000000000000000000000000000000000000000000000000
+ EOF
+'
+
+# The sha1s we have in our pack. It's important that these have the same
+# starting byte, so that they end up in the same fanout section of the index.
+# That lets us make sure we are exercising the binary search with both sets.
+LO_SHA1=$(test_oid lo_oid)
+HI_SHA1=$EMPTY_BLOB
+
+# And here's a "missing sha1" which will produce failed lookups. It must also
+# be in the same fanout section, and should be between the two (so that during
+# our binary search, we are sure to end up looking at one or the other of the
+# duplicate runs).
+MISSING_SHA1=$(test_oid missing_oid)
+
+# git will never intentionally create packfiles with
+# duplicate objects, so we have to construct them by hand.
+#
+# $1 is the name of the packfile to create
+#
+# $2 is the number of times to duplicate each object
+create_pack () {
+ pack_header "$((2 * $2))" >"$1" &&
+ for i in $(test_seq 1 "$2"); do
+ pack_obj $LO_SHA1 &&
+ pack_obj $HI_SHA1
+ done >>"$1" &&
+ pack_trailer "$1"
+}
+
+# double-check that create_pack actually works
+test_expect_success 'pack with no duplicates' '
+ create_pack no-dups.pack 1 &&
+ git index-pack --stdin <no-dups.pack
+'
+
+test_expect_success 'index-pack will allow duplicate objects by default' '
+ clear_packs &&
+ create_pack dups.pack 100 &&
+ git index-pack --stdin <dups.pack
+'
+
+test_expect_success 'create batch-check test vectors' '
+ cat >input <<-EOF &&
+ $LO_SHA1
+ $HI_SHA1
+ $MISSING_SHA1
+ EOF
+ cat >expect <<-EOF
+ $LO_SHA1 blob 2
+ $HI_SHA1 blob 0
+ $MISSING_SHA1 missing
+ EOF
+'
+
+test_expect_success 'lookup in duplicated pack' '
+ git cat-file --batch-check <input >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'index-pack can reject packs with duplicates' '
+ clear_packs &&
+ create_pack dups.pack 2 &&
+ test_must_fail git index-pack --strict --stdin <dups.pack &&
+ test_expect_code 1 git cat-file -e $LO_SHA1
+'
+
+test_done
diff --git a/t/t5309-pack-delta-cycles.sh b/t/t5309-pack-delta-cycles.sh
new file mode 100755
index 0000000..4e910c5
--- /dev/null
+++ b/t/t5309-pack-delta-cycles.sh
@@ -0,0 +1,79 @@
+#!/bin/sh
+
+test_description='test index-pack handling of delta cycles in packfiles'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-pack.sh
+
+# Two similar-ish objects that we have computed deltas between.
+A=$(test_oid packlib_7_0)
+B=$(test_oid packlib_7_76)
+
+# double-check our hand-constucted packs
+test_expect_success 'index-pack works with a single delta (A->B)' '
+ clear_packs &&
+ {
+ pack_header 2 &&
+ pack_obj $A $B &&
+ pack_obj $B
+ } >ab.pack &&
+ pack_trailer ab.pack &&
+ git index-pack --stdin <ab.pack &&
+ git cat-file -t $A &&
+ git cat-file -t $B
+'
+
+test_expect_success 'index-pack works with a single delta (B->A)' '
+ clear_packs &&
+ {
+ pack_header 2 &&
+ pack_obj $A &&
+ pack_obj $B $A
+ } >ba.pack &&
+ pack_trailer ba.pack &&
+ git index-pack --stdin <ba.pack &&
+ git cat-file -t $A &&
+ git cat-file -t $B
+'
+
+test_expect_success 'index-pack detects missing base objects' '
+ clear_packs &&
+ {
+ pack_header 1 &&
+ pack_obj $A $B
+ } >missing.pack &&
+ pack_trailer missing.pack &&
+ test_must_fail git index-pack --fix-thin --stdin <missing.pack
+'
+
+test_expect_success 'index-pack detects REF_DELTA cycles' '
+ clear_packs &&
+ {
+ pack_header 2 &&
+ pack_obj $A $B &&
+ pack_obj $B $A
+ } >cycle.pack &&
+ pack_trailer cycle.pack &&
+ test_must_fail git index-pack --fix-thin --stdin <cycle.pack
+'
+
+test_expect_success 'failover to an object in another pack' '
+ clear_packs &&
+ git index-pack --stdin <ab.pack &&
+ test_must_fail git index-pack --stdin --fix-thin <cycle.pack
+'
+
+test_expect_success 'failover to a duplicate object in the same pack' '
+ clear_packs &&
+ {
+ pack_header 3 &&
+ pack_obj $A $B &&
+ pack_obj $B $A &&
+ pack_obj $A
+ } >recoverable.pack &&
+ pack_trailer recoverable.pack &&
+ test_must_fail git index-pack --fix-thin --stdin <recoverable.pack
+'
+
+test_done
diff --git a/t/t5310-pack-bitmaps.sh b/t/t5310-pack-bitmaps.sh
new file mode 100755
index 0000000..d7fd713
--- /dev/null
+++ b/t/t5310-pack-bitmaps.sh
@@ -0,0 +1,530 @@
+#!/bin/sh
+
+test_description='exercise basic bitmap functionality'
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-bitmap.sh
+
+# t5310 deals only with single-pack bitmaps, so don't write MIDX bitmaps in
+# their place.
+GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0
+
+# Likewise, allow individual tests to control whether or not they use
+# the boundary-based traversal.
+sane_unset GIT_TEST_PACK_USE_BITMAP_BOUNDARY_TRAVERSAL
+
+objpath () {
+ echo ".git/objects/$(echo "$1" | sed -e 's|\(..\)|\1/|')"
+}
+
+# show objects present in pack ($1 should be associated *.idx)
+list_packed_objects () {
+ git show-index <"$1" >object-list &&
+ cut -d' ' -f2 object-list
+}
+
+# has_any pattern-file content-file
+# tests whether content-file has any entry from pattern-file with entries being
+# whole lines.
+has_any () {
+ grep -Ff "$1" "$2"
+}
+
+test_bitmap_cases () {
+ writeLookupTable=false
+ for i in "$@"
+ do
+ case "$i" in
+ "pack.writeBitmapLookupTable") writeLookupTable=true;;
+ esac
+ done
+
+ test_expect_success 'setup test repository' '
+ rm -fr * .git &&
+ git init &&
+ git config pack.writeBitmapLookupTable '"$writeLookupTable"'
+ '
+ setup_bitmap_history
+
+ test_expect_success 'setup writing bitmaps during repack' '
+ git config repack.writeBitmaps true
+ '
+
+ test_expect_success 'full repack creates bitmaps' '
+ GIT_TRACE2_EVENT="$(pwd)/trace" \
+ git repack -ad &&
+ ls .git/objects/pack/ | grep bitmap >output &&
+ test_line_count = 1 output &&
+ grep "\"key\":\"num_selected_commits\",\"value\":\"106\"" trace &&
+ grep "\"key\":\"num_maximal_commits\",\"value\":\"107\"" trace
+ '
+
+ basic_bitmap_tests
+
+ test_expect_success 'pack-objects respects --local (non-local loose)' '
+ git init --bare alt.git &&
+ echo $(pwd)/alt.git/objects >.git/objects/info/alternates &&
+ echo content1 >file1 &&
+ # non-local loose object which is not present in bitmapped pack
+ altblob=$(GIT_DIR=alt.git git hash-object -w file1) &&
+ # non-local loose object which is also present in bitmapped pack
+ git cat-file blob $blob | GIT_DIR=alt.git git hash-object -w --stdin &&
+ git add file1 &&
+ test_tick &&
+ git commit -m commit_file1 &&
+ echo HEAD | git pack-objects --local --stdout --revs >1.pack &&
+ git index-pack 1.pack &&
+ list_packed_objects 1.idx >1.objects &&
+ printf "%s\n" "$altblob" "$blob" >nonlocal-loose &&
+ ! has_any nonlocal-loose 1.objects
+ '
+
+ test_expect_success 'pack-objects respects --honor-pack-keep (local non-bitmapped pack)' '
+ echo content2 >file2 &&
+ blob2=$(git hash-object -w file2) &&
+ git add file2 &&
+ test_tick &&
+ git commit -m commit_file2 &&
+ printf "%s\n" "$blob2" "$bitmaptip" >keepobjects &&
+ pack2=$(git pack-objects pack2 <keepobjects) &&
+ mv pack2-$pack2.* .git/objects/pack/ &&
+ >.git/objects/pack/pack2-$pack2.keep &&
+ rm $(objpath $blob2) &&
+ echo HEAD | git pack-objects --honor-pack-keep --stdout --revs >2a.pack &&
+ git index-pack 2a.pack &&
+ list_packed_objects 2a.idx >2a.objects &&
+ ! has_any keepobjects 2a.objects
+ '
+
+ test_expect_success 'pack-objects respects --local (non-local pack)' '
+ mv .git/objects/pack/pack2-$pack2.* alt.git/objects/pack/ &&
+ echo HEAD | git pack-objects --local --stdout --revs >2b.pack &&
+ git index-pack 2b.pack &&
+ list_packed_objects 2b.idx >2b.objects &&
+ ! has_any keepobjects 2b.objects
+ '
+
+ test_expect_success 'pack-objects respects --honor-pack-keep (local bitmapped pack)' '
+ ls .git/objects/pack/ | grep bitmap >output &&
+ test_line_count = 1 output &&
+ packbitmap=$(basename $(cat output) .bitmap) &&
+ list_packed_objects .git/objects/pack/$packbitmap.idx >packbitmap.objects &&
+ test_when_finished "rm -f .git/objects/pack/$packbitmap.keep" &&
+ >.git/objects/pack/$packbitmap.keep &&
+ echo HEAD | git pack-objects --honor-pack-keep --stdout --revs >3a.pack &&
+ git index-pack 3a.pack &&
+ list_packed_objects 3a.idx >3a.objects &&
+ ! has_any packbitmap.objects 3a.objects
+ '
+
+ test_expect_success 'pack-objects respects --local (non-local bitmapped pack)' '
+ mv .git/objects/pack/$packbitmap.* alt.git/objects/pack/ &&
+ rm -f .git/objects/pack/multi-pack-index &&
+ test_when_finished "mv alt.git/objects/pack/$packbitmap.* .git/objects/pack/" &&
+ echo HEAD | git pack-objects --local --stdout --revs >3b.pack &&
+ git index-pack 3b.pack &&
+ list_packed_objects 3b.idx >3b.objects &&
+ ! has_any packbitmap.objects 3b.objects
+ '
+
+ test_expect_success 'pack-objects to file can use bitmap' '
+ # make sure we still have 1 bitmap index from previous tests
+ ls .git/objects/pack/ | grep bitmap >output &&
+ test_line_count = 1 output &&
+ # verify equivalent packs are generated with/without using bitmap index
+ packasha1=$(git pack-objects --no-use-bitmap-index --all packa </dev/null) &&
+ packbsha1=$(git pack-objects --use-bitmap-index --all packb </dev/null) &&
+ list_packed_objects packa-$packasha1.idx >packa.objects &&
+ list_packed_objects packb-$packbsha1.idx >packb.objects &&
+ test_cmp packa.objects packb.objects
+ '
+
+ test_expect_success 'full repack, reusing previous bitmaps' '
+ git repack -ad &&
+ ls .git/objects/pack/ | grep bitmap >output &&
+ test_line_count = 1 output
+ '
+
+ test_expect_success 'fetch (full bitmap)' '
+ git --git-dir=clone.git fetch origin second:second &&
+ git rev-parse HEAD >expect &&
+ git --git-dir=clone.git rev-parse HEAD >actual &&
+ test_cmp expect actual
+ '
+
+ test_expect_success 'create objects for missing-HAVE tests' '
+ blob=$(echo "missing have" | git hash-object -w --stdin) &&
+ tree=$(printf "100644 blob $blob\tfile\n" | git mktree) &&
+ parent=$(echo parent | git commit-tree $tree) &&
+ commit=$(echo commit | git commit-tree $tree -p $parent) &&
+ cat >revs <<-EOF
+ HEAD
+ ^HEAD^
+ ^$commit
+ EOF
+ '
+
+ test_expect_success 'pack-objects respects --incremental' '
+ cat >revs2 <<-EOF &&
+ HEAD
+ $commit
+ EOF
+ git pack-objects --incremental --stdout --revs <revs2 >4.pack &&
+ git index-pack 4.pack &&
+ list_packed_objects 4.idx >4.objects &&
+ test_line_count = 4 4.objects &&
+ git rev-list --objects $commit >revlist &&
+ cut -d" " -f1 revlist |sort >objects &&
+ test_cmp 4.objects objects
+ '
+
+ test_expect_success 'pack with missing blob' '
+ rm $(objpath $blob) &&
+ git pack-objects --stdout --revs <revs >/dev/null
+ '
+
+ test_expect_success 'pack with missing tree' '
+ rm $(objpath $tree) &&
+ git pack-objects --stdout --revs <revs >/dev/null
+ '
+
+ test_expect_success 'pack with missing parent' '
+ rm $(objpath $parent) &&
+ git pack-objects --stdout --revs <revs >/dev/null
+ '
+
+ test_expect_success JGIT,SHA1 'we can read jgit bitmaps' '
+ git clone --bare . compat-jgit.git &&
+ (
+ cd compat-jgit.git &&
+ rm -f objects/pack/*.bitmap &&
+ jgit gc &&
+ git rev-list --test-bitmap HEAD
+ )
+ '
+
+ test_expect_success JGIT,SHA1 'jgit can read our bitmaps' '
+ git clone --bare . compat-us.git &&
+ (
+ cd compat-us.git &&
+ git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
+ git repack -adb &&
+ # jgit gc will barf if it does not like our bitmaps
+ jgit gc
+ )
+ '
+
+ test_expect_success 'splitting packs does not generate bogus bitmaps' '
+ test-tool genrandom foo $((1024 * 1024)) >rand &&
+ git add rand &&
+ git commit -m "commit with big file" &&
+ git -c pack.packSizeLimit=500k repack -adb &&
+ git init --bare no-bitmaps.git &&
+ git -C no-bitmaps.git fetch .. HEAD
+ '
+
+ test_expect_success 'set up reusable pack' '
+ rm -f .git/objects/pack/*.keep &&
+ git repack -adb &&
+ reusable_pack () {
+ git for-each-ref --format="%(objectname)" |
+ git pack-objects --delta-base-offset --revs --stdout "$@"
+ }
+ '
+
+ test_expect_success 'pack reuse respects --honor-pack-keep' '
+ test_when_finished "rm -f .git/objects/pack/*.keep" &&
+ for i in .git/objects/pack/*.pack
+ do
+ >${i%.pack}.keep || return 1
+ done &&
+ reusable_pack --honor-pack-keep >empty.pack &&
+ git index-pack empty.pack &&
+ git show-index <empty.idx >actual &&
+ test_must_be_empty actual
+ '
+
+ test_expect_success 'pack reuse respects --local' '
+ mv .git/objects/pack/* alt.git/objects/pack/ &&
+ test_when_finished "mv alt.git/objects/pack/* .git/objects/pack/" &&
+ reusable_pack --local >empty.pack &&
+ git index-pack empty.pack &&
+ git show-index <empty.idx >actual &&
+ test_must_be_empty actual
+ '
+
+ test_expect_success 'pack reuse respects --incremental' '
+ reusable_pack --incremental >empty.pack &&
+ git index-pack empty.pack &&
+ git show-index <empty.idx >actual &&
+ test_must_be_empty actual
+ '
+
+ test_expect_success 'truncated bitmap fails gracefully (ewah)' '
+ test_config pack.writebitmaphashcache false &&
+ test_config pack.writebitmaplookuptable false &&
+ git repack -ad &&
+ git rev-list --use-bitmap-index --count --all >expect &&
+ bitmap=$(ls .git/objects/pack/*.bitmap) &&
+ test_when_finished "rm -f $bitmap" &&
+ test_copy_bytes 256 <$bitmap >$bitmap.tmp &&
+ mv -f $bitmap.tmp $bitmap &&
+ git rev-list --use-bitmap-index --count --all >actual 2>stderr &&
+ test_cmp expect actual &&
+ test_grep corrupt.ewah.bitmap stderr
+ '
+
+ test_expect_success 'truncated bitmap fails gracefully (cache)' '
+ git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
+ git repack -ad &&
+ git rev-list --use-bitmap-index --count --all >expect &&
+ bitmap=$(ls .git/objects/pack/*.bitmap) &&
+ test_when_finished "rm -f $bitmap" &&
+ test_copy_bytes 512 <$bitmap >$bitmap.tmp &&
+ mv -f $bitmap.tmp $bitmap &&
+ git rev-list --use-bitmap-index --count --all >actual 2>stderr &&
+ test_cmp expect actual &&
+ test_grep corrupted.bitmap.index stderr
+ '
+
+ # Create a state of history with these properties:
+ #
+ # - refs that allow a client to fetch some new history, while sharing some old
+ # history with the server; we use branches delta-reuse-old and
+ # delta-reuse-new here
+ #
+ # - the new history contains an object that is stored on the server as a delta
+ # against a base that is in the old history
+ #
+ # - the base object is not immediately reachable from the tip of the old
+ # history; finding it would involve digging down through history we know the
+ # other side has
+ #
+ # This should result in a state where fetching from old->new would not
+ # traditionally reuse the on-disk delta (because we'd have to dig to realize
+ # that the client has it), but we will do so if bitmaps can tell us cheaply
+ # that the other side has it.
+ test_expect_success 'set up thin delta-reuse parent' '
+ # This first commit contains the buried base object.
+ test-tool genrandom delta 16384 >file &&
+ git add file &&
+ git commit -m "delta base" &&
+ base=$(git rev-parse --verify HEAD:file) &&
+
+ # These intermediate commits bury the base back in history.
+ # This becomes the "old" state.
+ for i in 1 2 3 4 5
+ do
+ echo $i >file &&
+ git commit -am "intermediate $i" || return 1
+ done &&
+ git branch delta-reuse-old &&
+
+ # And now our new history has a delta against the buried base. Note
+ # that this must be smaller than the original file, since pack-objects
+ # prefers to create deltas from smaller objects to larger.
+ test-tool genrandom delta 16300 >file &&
+ git commit -am "delta result" &&
+ delta=$(git rev-parse --verify HEAD:file) &&
+ git branch delta-reuse-new &&
+
+ # Repack with bitmaps and double check that we have the expected delta
+ # relationship.
+ git repack -adb &&
+ have_delta $delta $base
+ '
+
+ # Now we can sanity-check the non-bitmap behavior (that the server is not able
+ # to reuse the delta). This isn't strictly something we care about, so this
+ # test could be scrapped in the future. But it makes sure that the next test is
+ # actually triggering the feature we want.
+ #
+ # Note that our tools for working with on-the-wire "thin" packs are limited. So
+ # we actually perform the fetch, retain the resulting pack, and inspect the
+ # result.
+ test_expect_success 'fetch without bitmaps ignores delta against old base' '
+ test_config pack.usebitmaps false &&
+ test_when_finished "rm -rf client.git" &&
+ git init --bare client.git &&
+ (
+ cd client.git &&
+ git config transfer.unpackLimit 1 &&
+ git fetch .. delta-reuse-old:delta-reuse-old &&
+ git fetch .. delta-reuse-new:delta-reuse-new &&
+ have_delta $delta $ZERO_OID
+ )
+ '
+
+ # And do the same for the bitmap case, where we do expect to find the delta.
+ test_expect_success 'fetch with bitmaps can reuse old base' '
+ test_config pack.usebitmaps true &&
+ test_when_finished "rm -rf client.git" &&
+ git init --bare client.git &&
+ (
+ cd client.git &&
+ git config transfer.unpackLimit 1 &&
+ git fetch .. delta-reuse-old:delta-reuse-old &&
+ git fetch .. delta-reuse-new:delta-reuse-new &&
+ have_delta $delta $base
+ )
+ '
+
+ test_expect_success 'pack.preferBitmapTips' '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+ git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
+
+ # create enough commits that not all are receive bitmap
+ # coverage even if they are all at the tip of some reference.
+ test_commit_bulk --message="%s" 103 &&
+
+ git rev-list HEAD >commits.raw &&
+ sort <commits.raw >commits &&
+
+ git log --format="create refs/tags/%s %H" HEAD >refs &&
+ git update-ref --stdin <refs &&
+
+ git repack -adb &&
+ test-tool bitmap list-commits | sort >bitmaps &&
+
+ # remember which commits did not receive bitmaps
+ comm -13 bitmaps commits >before &&
+ test_file_not_empty before &&
+
+ # mark the commits which did not receive bitmaps as preferred,
+ # and generate the bitmap again
+ perl -pe "s{^}{create refs/tags/include/$. }" <before |
+ git update-ref --stdin &&
+ git -c pack.preferBitmapTips=refs/tags/include repack -adb &&
+
+ # finally, check that the commit(s) without bitmap coverage
+ # are not the same ones as before
+ test-tool bitmap list-commits | sort >bitmaps &&
+ comm -13 bitmaps commits >after &&
+
+ ! test_cmp before after
+ )
+ '
+
+ test_expect_success 'pack.preferBitmapTips' '
+ git init repo &&
+ test_when_finished "rm -rf repo" &&
+ (
+ cd repo &&
+ git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
+ test_commit_bulk --message="%s" 103 &&
+
+ cat >>.git/config <<-\EOF &&
+ [pack]
+ preferBitmapTips
+ EOF
+ cat >expect <<-\EOF &&
+ error: missing value for '\''pack.preferbitmaptips'\''
+ EOF
+ git repack -adb 2>actual &&
+ test_cmp expect actual
+ )
+ '
+
+ test_expect_success 'complains about multiple pack bitmaps' '
+ rm -fr repo &&
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+ git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
+
+ test_commit base &&
+
+ git repack -adb &&
+ bitmap="$(ls .git/objects/pack/pack-*.bitmap)" &&
+ mv "$bitmap" "$bitmap.bak" &&
+
+ test_commit other &&
+ git repack -ab &&
+
+ mv "$bitmap.bak" "$bitmap" &&
+
+ find .git/objects/pack -type f -name "*.pack" >packs &&
+ find .git/objects/pack -type f -name "*.bitmap" >bitmaps &&
+ test_line_count = 2 packs &&
+ test_line_count = 2 bitmaps &&
+
+ GIT_TRACE2_EVENT=$(pwd)/trace2.txt git rev-list --use-bitmap-index HEAD &&
+ grep "opened bitmap" trace2.txt &&
+ grep "ignoring extra bitmap" trace2.txt
+ )
+ '
+}
+
+test_bitmap_cases
+
+GIT_TEST_PACK_USE_BITMAP_BOUNDARY_TRAVERSAL=1
+export GIT_TEST_PACK_USE_BITMAP_BOUNDARY_TRAVERSAL
+
+test_bitmap_cases
+
+sane_unset GIT_TEST_PACK_USE_BITMAP_BOUNDARY_TRAVERSAL
+
+test_expect_success 'incremental repack fails when bitmaps are requested' '
+ test_commit more-1 &&
+ test_must_fail git repack -d 2>err &&
+ test_grep "Incremental repacks are incompatible with bitmap" err
+'
+
+test_expect_success 'incremental repack can disable bitmaps' '
+ test_commit more-2 &&
+ git repack -d --no-write-bitmap-index
+'
+
+test_expect_success 'boundary-based traversal is used when requested' '
+ git repack -a -d --write-bitmap-index &&
+
+ for argv in \
+ "git -c pack.useBitmapBoundaryTraversal=true" \
+ "git -c feature.experimental=true" \
+ "GIT_TEST_PACK_USE_BITMAP_BOUNDARY_TRAVERSAL=1 git"
+ do
+ eval "GIT_TRACE2_EVENT=1 $argv rev-list --objects \
+ --use-bitmap-index second..other 2>perf" &&
+ grep "\"region_enter\".*\"label\":\"haves/boundary\"" perf ||
+ return 1
+ done &&
+
+ for argv in \
+ "git -c pack.useBitmapBoundaryTraversal=false" \
+ "git -c feature.experimental=true -c pack.useBitmapBoundaryTraversal=false" \
+ "GIT_TEST_PACK_USE_BITMAP_BOUNDARY_TRAVERSAL=0 git -c pack.useBitmapBoundaryTraversal=true" \
+ "GIT_TEST_PACK_USE_BITMAP_BOUNDARY_TRAVERSAL=0 git -c feature.experimental=true"
+ do
+ eval "GIT_TRACE2_EVENT=1 $argv rev-list --objects \
+ --use-bitmap-index second..other 2>perf" &&
+ grep "\"region_enter\".*\"label\":\"haves/classic\"" perf ||
+ return 1
+ done
+'
+
+test_bitmap_cases "pack.writeBitmapLookupTable"
+
+test_expect_success 'verify writing bitmap lookup table when enabled' '
+ GIT_TRACE2_EVENT="$(pwd)/trace2" \
+ git repack -ad &&
+ grep "\"label\":\"writing_lookup_table\"" trace2
+'
+
+test_expect_success 'truncated bitmap fails gracefully (lookup table)' '
+ test_config pack.writebitmaphashcache false &&
+ git repack -adb &&
+ git rev-list --use-bitmap-index --count --all >expect &&
+ bitmap=$(ls .git/objects/pack/*.bitmap) &&
+ test_when_finished "rm -f $bitmap" &&
+ test_copy_bytes 512 <$bitmap >$bitmap.tmp &&
+ mv -f $bitmap.tmp $bitmap &&
+ git rev-list --use-bitmap-index --count --all >actual 2>stderr &&
+ test_cmp expect actual &&
+ test_grep corrupted.bitmap.index stderr
+'
+
+test_done
diff --git a/t/t5311-pack-bitmaps-shallow.sh b/t/t5311-pack-bitmaps-shallow.sh
new file mode 100755
index 0000000..4fe71fe
--- /dev/null
+++ b/t/t5311-pack-bitmaps-shallow.sh
@@ -0,0 +1,58 @@
+#!/bin/sh
+
+test_description='check bitmap operation with shallow repositories'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# We want to create a situation where the shallow, grafted
+# view of reachability does not match reality in a way that
+# might cause us to send insufficient objects.
+#
+# We do this with a history that repeats a state, like:
+#
+# A -- B -- C
+# file=1 file=2 file=1
+#
+# and then create a shallow clone to the second commit, B.
+# In a non-shallow clone, that would mean we already have
+# the tree for A. But in a shallow one, we've grafted away
+# A, and fetching A to B requires that the other side send
+# us the tree for file=1.
+test_shallow_bitmaps () {
+ writeLookupTable=false
+
+ for i in "$@"
+ do
+ case $i in
+ "pack.writeBitmapLookupTable") writeLookupTable=true;;
+ esac
+ done
+
+ test_expect_success 'setup shallow repo' '
+ rm -rf * .git &&
+ git init &&
+ git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
+ echo 1 >file &&
+ git add file &&
+ git commit -m orig &&
+ echo 2 >file &&
+ git commit -a -m update &&
+ git clone --no-local --bare --depth=1 . shallow.git &&
+ echo 1 >file &&
+ git commit -a -m repeat
+ '
+
+ test_expect_success 'turn on bitmaps in the parent' '
+ git repack -adb
+ '
+
+ test_expect_success 'shallow fetch from bitmapped repo' '
+ (cd shallow.git && git fetch)
+ '
+}
+
+test_shallow_bitmaps
+test_shallow_bitmaps "pack.writeBitmapLookupTable"
+
+test_done
diff --git a/t/t5312-prune-corruption.sh b/t/t5312-prune-corruption.sh
new file mode 100755
index 0000000..230cb38
--- /dev/null
+++ b/t/t5312-prune-corruption.sh
@@ -0,0 +1,140 @@
+#!/bin/sh
+
+test_description='
+Test pruning of repositories with minor corruptions. The goal
+here is that we should always be erring on the side of safety. So
+if we see, for example, a ref with a bogus name, it is OK either to
+bail out or to proceed using it as a reachable tip, but it is _not_
+OK to proceed as if it did not exist. Otherwise we might silently
+delete objects that cannot be recovered.
+
+Note that we do assert command failure in these cases, because that is
+what currently happens. If that changes, these tests should be revisited.
+'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'disable reflogs' '
+ git config core.logallrefupdates false &&
+ git reflog expire --expire=all --all
+'
+
+create_bogus_ref () {
+ test-tool ref-store main update-ref msg "refs/heads/bogus..name" $bogus $ZERO_OID REF_SKIP_REFNAME_VERIFICATION &&
+ test_when_finished "test-tool ref-store main delete-refs REF_NO_DEREF msg refs/heads/bogus..name"
+}
+
+test_expect_success 'create history reachable only from a bogus-named ref' '
+ test_tick && git commit --allow-empty -m main &&
+ base=$(git rev-parse HEAD) &&
+ test_tick && git commit --allow-empty -m bogus &&
+ bogus=$(git rev-parse HEAD) &&
+ git cat-file commit $bogus >saved &&
+ git reset --hard HEAD^
+'
+
+test_expect_success 'pruning does not drop bogus object' '
+ test_when_finished "git hash-object -w -t commit saved" &&
+ create_bogus_ref &&
+ test_must_fail git prune --expire=now &&
+ git cat-file -e $bogus
+'
+
+test_expect_success 'put bogus object into pack' '
+ git tag reachable $bogus &&
+ git repack -ad &&
+ git tag -d reachable &&
+ git cat-file -e $bogus
+'
+
+test_expect_success 'non-destructive repack bails on bogus ref' '
+ create_bogus_ref &&
+ test_must_fail git repack -adk
+'
+
+test_expect_success 'GIT_REF_PARANOIA=0 overrides safety' '
+ create_bogus_ref &&
+ GIT_REF_PARANOIA=0 git repack -adk
+'
+
+
+test_expect_success 'destructive repack keeps packed object' '
+ create_bogus_ref &&
+ test_must_fail git repack -Ad --unpack-unreachable=now &&
+ git cat-file -e $bogus &&
+ test_must_fail git repack -ad &&
+ git cat-file -e $bogus
+'
+
+test_expect_success 'destructive repack not confused by dangling symref' '
+ test_when_finished "git symbolic-ref -d refs/heads/dangling" &&
+ git symbolic-ref refs/heads/dangling refs/heads/does-not-exist &&
+ git repack -ad &&
+ test_must_fail git cat-file -e $bogus
+'
+
+# We create two new objects here, "one" and "two". Our
+# main branch points to "two", which is deleted,
+# corrupting the repository. But we'd like to make sure
+# that the otherwise unreachable "one" is not pruned
+# (since it is the user's best bet for recovering
+# from the corruption).
+#
+# Note that we also point HEAD somewhere besides "two",
+# as we want to make sure we test the case where we
+# pick up the reference to "two" by iterating the refs,
+# not by resolving HEAD.
+test_expect_success 'create history with missing tip commit' '
+ test_tick && git commit --allow-empty -m one &&
+ recoverable=$(git rev-parse HEAD) &&
+ git cat-file commit $recoverable >saved &&
+ test_tick && git commit --allow-empty -m two &&
+ missing=$(git rev-parse HEAD) &&
+ git checkout --detach $base &&
+ rm .git/objects/$(echo $missing | sed "s,..,&/,") &&
+ test_must_fail git cat-file -e $missing
+'
+
+test_expect_success 'pruning with a corrupted tip does not drop history' '
+ test_when_finished "git hash-object -w -t commit saved" &&
+ test_must_fail git prune --expire=now &&
+ git cat-file -e $recoverable
+'
+
+test_expect_success 'pack-refs does not silently delete broken loose ref' '
+ git pack-refs --all --prune &&
+ echo $missing >expect &&
+ git rev-parse refs/heads/main >actual &&
+ test_cmp expect actual
+'
+
+# we do not want to count on running pack-refs to
+# actually pack it, as it is perfectly reasonable to
+# skip processing a broken ref
+test_expect_success REFFILES 'create packed-refs file with broken ref' '
+ rm -f .git/refs/heads/main &&
+ cat >.git/packed-refs <<-EOF &&
+ $missing refs/heads/main
+ $recoverable refs/heads/other
+ EOF
+ echo $missing >expect &&
+ git rev-parse refs/heads/main >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success REFFILES 'pack-refs does not silently delete broken packed ref' '
+ git pack-refs --all --prune &&
+ git rev-parse refs/heads/main >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success REFFILES 'pack-refs does not drop broken refs during deletion' '
+ git update-ref -d refs/heads/other &&
+ git rev-parse refs/heads/main >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t5313-pack-bounds-checks.sh b/t/t5313-pack-bounds-checks.sh
new file mode 100755
index 0000000..ceaa670
--- /dev/null
+++ b/t/t5313-pack-bounds-checks.sh
@@ -0,0 +1,196 @@
+#!/bin/sh
+
+test_description='bounds-checking of access to mmapped on-disk file formats'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+clear_base () {
+ test_when_finished 'restore_base' &&
+ rm -f $base
+}
+
+restore_base () {
+ cp base-backup/* .git/objects/pack/
+}
+
+do_pack () {
+ pack_objects=$1; shift
+ sha1=$(
+ for i in $pack_objects
+ do
+ echo $i
+ done | git pack-objects "$@" .git/objects/pack/pack
+ ) &&
+ pack=.git/objects/pack/pack-$sha1.pack &&
+ idx=.git/objects/pack/pack-$sha1.idx &&
+ chmod +w $pack $idx &&
+ test_when_finished 'rm -f "$pack" "$idx"'
+}
+
+munge () {
+ printf "$3" | dd of="$1" bs=1 conv=notrunc seek=$2
+}
+
+# Offset in a v2 .idx to its initial and extended offset tables. For an index
+# with "nr" objects, this is:
+#
+# magic(4) + version(4) + fan-out(4*256) + sha1s(20*nr) + crc(4*nr),
+#
+# for the initial, and another ofs(4*nr) past that for the extended.
+#
+ofs_table () {
+ echo $((4 + 4 + 4*256 + $(test_oid rawsz)*$1 + 4*$1))
+}
+extended_table () {
+ echo $(($(ofs_table "$1") + 4*$1))
+}
+
+test_expect_success 'setup' '
+ test_oid_cache <<-EOF
+ oid000 sha1:1485
+ oid000 sha256:4222
+
+ oidfff sha1:74
+ oidfff sha256:1350
+ EOF
+'
+
+test_expect_success 'set up base packfile and variables' '
+ # the hash of this content starts with ff, which
+ # makes some later computations much simpler
+ test_oid oidfff >file &&
+ git add file &&
+ git commit -m base &&
+ git repack -ad &&
+ base=$(echo .git/objects/pack/*) &&
+ chmod +w $base &&
+ mkdir base-backup &&
+ cp $base base-backup/ &&
+ object=$(git rev-parse HEAD:file)
+'
+
+test_expect_success 'pack/index object count mismatch' '
+ do_pack $object &&
+ munge $pack 8 "\377\0\0\0" &&
+ clear_base &&
+
+ # We enumerate the objects from the completely-fine
+ # .idx, but notice later that the .pack is bogus
+ # and fail to show any data.
+ echo "$object missing" >expect &&
+ git cat-file --batch-all-objects --batch-check >actual &&
+ test_cmp expect actual &&
+
+ # ...and here fail to load the object (without segfaulting),
+ # but fallback to a good copy if available.
+ test_must_fail git cat-file blob $object &&
+ restore_base &&
+ git cat-file blob $object >actual &&
+ test_cmp file actual &&
+
+ # ...and make sure that index-pack --verify, which has its
+ # own reading routines, does not segfault.
+ test_must_fail git index-pack --verify $pack
+'
+
+test_expect_success 'matched bogus object count' '
+ do_pack $object &&
+ munge $pack 8 "\377\0\0\0" &&
+ munge $idx $((255 * 4)) "\377\0\0\0" &&
+ clear_base &&
+
+ # Unlike above, we should notice early that the .idx is totally
+ # bogus, and not even enumerate its contents.
+ git cat-file --batch-all-objects --batch-check >actual &&
+ test_must_be_empty actual &&
+
+ # But as before, we can do the same object-access checks.
+ test_must_fail git cat-file blob $object &&
+ restore_base &&
+ git cat-file blob $object >actual &&
+ test_cmp file actual &&
+
+ test_must_fail git index-pack --verify $pack
+'
+
+# Note that we cannot check the fallback case for these
+# further .idx tests, as we notice the problem in functions
+# whose interface doesn't allow an error return (like use_pack()),
+# and thus we just die().
+#
+# There's also no point in doing enumeration tests, as
+# we are munging offsets here, which are about looking up
+# specific objects.
+
+test_expect_success 'bogus object offset (v1)' '
+ do_pack $object --index-version=1 &&
+ munge $idx $((4 * 256)) "\377\0\0\0" &&
+ clear_base &&
+ test_must_fail git cat-file blob $object &&
+ test_must_fail git index-pack --verify $pack
+'
+
+test_expect_success 'bogus object offset (v2, no msb)' '
+ do_pack $object --index-version=2 &&
+ munge $idx $(ofs_table 1) "\0\377\0\0" &&
+ clear_base &&
+ test_must_fail git cat-file blob $object &&
+ test_must_fail git index-pack --verify $pack
+'
+
+test_expect_success 'bogus offset into v2 extended table' '
+ do_pack $object --index-version=2 &&
+ munge $idx $(ofs_table 1) "\377\0\0\0" &&
+ clear_base &&
+ test_must_fail git cat-file blob $object &&
+ test_must_fail git index-pack --verify $pack
+'
+
+test_expect_success 'bogus offset inside v2 extended table' '
+ # We need two objects here, so we can plausibly require
+ # an extended table (if the first object were larger than 2^31).
+ #
+ # Note that the value is important here. We want $object as
+ # the second entry in sorted-hash order. The hash of this object starts
+ # with "000", which sorts before that of $object (which starts
+ # with "fff").
+ second=$(test_oid oid000 | git hash-object -w --stdin) &&
+ do_pack "$object $second" --index-version=2 &&
+
+ # We have to make extra room for the table, so we cannot
+ # just munge in place as usual.
+ {
+ dd if=$idx bs=1 count=$(($(ofs_table 2) + 4)) &&
+ printf "\200\0\0\0" &&
+ printf "\377\0\0\0\0\0\0\0" &&
+ dd if=$idx bs=1 skip=$(extended_table 2)
+ } >tmp &&
+ mv tmp "$idx" &&
+ clear_base &&
+ test_must_fail git cat-file blob $object &&
+ test_must_fail git index-pack --verify $pack
+'
+
+test_expect_success 'bogus OFS_DELTA in packfile' '
+ # Generate a pack with a delta in it.
+ base=$(test-tool genrandom foo 3000 | git hash-object --stdin -w) &&
+ delta=$(test-tool genrandom foo 2000 | git hash-object --stdin -w) &&
+ do_pack "$base $delta" --delta-base-offset &&
+ rm -f .git/objects/??/* &&
+
+ # Double check that we have the delta we expect.
+ echo $base >expect &&
+ echo $delta | git cat-file --batch-check="%(deltabase)" >actual &&
+ test_cmp expect actual &&
+
+ # Now corrupt it. We assume the varint size for the delta is small
+ # enough to fit in the first byte (which it should be, since it
+ # is a pure deletion from the base), and that original ofs_delta
+ # takes 2 bytes (which it should, as it should be ~3000).
+ ofs=$(git show-index <$idx | grep $delta | cut -d" " -f1) &&
+ munge $pack $(($ofs + 1)) "\177\377" &&
+ test_must_fail git cat-file blob $delta >/dev/null
+'
+
+test_done
diff --git a/t/t5314-pack-cycle-detection.sh b/t/t5314-pack-cycle-detection.sh
new file mode 100755
index 0000000..82734b9
--- /dev/null
+++ b/t/t5314-pack-cycle-detection.sh
@@ -0,0 +1,115 @@
+#!/bin/sh
+
+test_description='test handling of inter-pack delta cycles during repack
+
+The goal here is to create a situation where we have two blobs, A and B, with A
+as a delta against B in one pack, and vice versa in the other. Then if we can
+persuade a full repack to find A from one pack and B from the other, that will
+give us a cycle when we attempt to reuse those deltas.
+
+The trick is in the "persuade" step, as it depends on the internals of how
+pack-objects picks which pack to reuse the deltas from. But we can assume
+that it does so in one of two general strategies:
+
+ 1. Using a static ordering of packs. In this case, no inter-pack cycles can
+ happen. Any objects with a delta relationship must be present in the same
+ pack (i.e., no "--thin" packs on disk), so we will find all related objects
+ from that pack. So assuming there are no cycles within a single pack (and
+ we avoid generating them via pack-objects or importing them via
+ index-pack), then our result will have no cycles.
+
+ So this case should pass the tests no matter how we arrange things.
+
+ 2. Picking the next pack to examine based on locality (i.e., where we found
+ something else recently).
+
+ In this case, we want to make sure that we find the delta versions of A and
+ B and not their base versions. We can do this by putting two blobs in each
+ pack. The first is a "dummy" blob that can only be found in the pack in
+ question. And then the second is the actual delta we want to find.
+
+ The two blobs must be present in the same tree, not present in other trees,
+ and the dummy pathname must sort before the delta path.
+
+The setup below focuses on case 2. We have two commits HEAD and HEAD^, each
+which has two files: "dummy" and "file". Then we can make two packs which
+contain:
+
+ [pack one]
+ HEAD:dummy
+ HEAD:file (as delta against HEAD^:file)
+ HEAD^:file (as base)
+
+ [pack two]
+ HEAD^:dummy
+ HEAD^:file (as delta against HEAD:file)
+ HEAD:file (as base)
+
+Then no matter which order we start looking at the packs in, we know that we
+will always find a delta for "file", because its lookup will always come
+immediately after the lookup for "dummy".
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# Create a pack containing the tree $1 and blob $1:file, with
+# the latter stored as a delta against $2:file.
+#
+# We convince pack-objects to make the delta in the direction of our choosing
+# by marking $2 as a preferred-base edge. That results in $1:file as a thin
+# delta, and index-pack completes it by adding $2:file as a base.
+#
+# Note that the two variants of "file" must be similar enough to convince git
+# to create the delta.
+make_pack () {
+ ln1=$(git rev-parse "$2") &&
+ ln2=$(git rev-parse "$1:dummy") &&
+ ln3=$(git rev-parse "$1:file") &&
+ cat >list <<-EOF
+ -$ln1
+ $ln2 dummy
+ $ln3 file
+ EOF
+ git pack-objects --stdout <list >pack &&
+ git index-pack --stdin --fix-thin <pack
+}
+
+test_expect_success 'setup' '
+ test-tool genrandom base 4096 >base &&
+ for i in one two
+ do
+ # we want shared content here to encourage deltas...
+ cp base file &&
+ echo $i >>file &&
+
+ # ...whereas dummy should be short, because we do not want
+ # deltas that would create duplicates when we --fix-thin
+ echo $i >dummy &&
+
+ git add file dummy &&
+ test_tick &&
+ git commit -m $i ||
+ return 1
+ done &&
+
+ make_pack HEAD^ HEAD &&
+ make_pack HEAD HEAD^
+'
+
+test_expect_success 'repack' '
+ # We first want to check that we do not have any internal errors,
+ # and also that we do not hit the last-ditch cycle-breaking code
+ # in write_object(), which will issue a warning to stderr.
+ git repack -ad 2>stderr &&
+ test_must_be_empty stderr &&
+
+ # And then double-check that the resulting pack is usable (i.e.,
+ # we did not fail to notice any cycles). We know we are accessing
+ # the objects via the new pack here, because "repack -d" will have
+ # removed the others.
+ git cat-file blob HEAD:file >/dev/null &&
+ git cat-file blob HEAD^:file >/dev/null
+'
+
+test_done
diff --git a/t/t5315-pack-objects-compression.sh b/t/t5315-pack-objects-compression.sh
new file mode 100755
index 0000000..c80ea9e
--- /dev/null
+++ b/t/t5315-pack-objects-compression.sh
@@ -0,0 +1,39 @@
+#!/bin/sh
+
+test_description='pack-object compression configuration'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ printf "%2000000s" X |
+ git hash-object -w --stdin >object-name &&
+ # make sure it resulted in a loose object
+ ob=$(sed -e "s/\(..\).*/\1/" object-name) &&
+ ject=$(sed -e "s/..\(.*\)/\1/" object-name) &&
+ test -f .git/objects/$ob/$ject
+'
+
+while read expect config
+do
+ test_expect_success "pack-objects with $config" '
+ test_when_finished "rm -f pack-*.*" &&
+ git $config pack-objects pack <object-name &&
+ sz=$(test_file_size pack-*.pack) &&
+ case "$expect" in
+ small) test "$sz" -le 100000 ;;
+ large) test "$sz" -ge 100000 ;;
+ esac
+ '
+done <<\EOF
+large -c core.compression=0
+small -c core.compression=9
+large -c core.compression=0 -c pack.compression=0
+large -c core.compression=9 -c pack.compression=0
+small -c core.compression=0 -c pack.compression=9
+small -c core.compression=9 -c pack.compression=9
+large -c pack.compression=0
+small -c pack.compression=9
+EOF
+
+test_done
diff --git a/t/t5316-pack-delta-depth.sh b/t/t5316-pack-delta-depth.sh
new file mode 100755
index 0000000..eb4ef3d
--- /dev/null
+++ b/t/t5316-pack-delta-depth.sh
@@ -0,0 +1,121 @@
+#!/bin/sh
+
+test_description='pack-objects breaks long cross-pack delta chains'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# This mirrors a repeated push setup:
+#
+# 1. A client repeatedly modifies some files, makes a
+# commit, and pushes the result. It does this N times
+# before we get around to repacking.
+#
+# 2. Each push generates a thin pack with the new version of
+# various objects. Let's consider some file in the root tree
+# which is updated in each commit.
+#
+# When generating push number X, we feed commit X-1 (and
+# thus blob X-1) as a preferred base. The resulting pack has
+# blob X as a thin delta against blob X-1.
+#
+# On the receiving end, "index-pack --fix-thin" will
+# complete the pack with a base copy of blob X-1.
+#
+# 3. In older versions of git, if we used the delta from
+# pack X, then we'd always find blob X-1 as a base in the
+# same pack (and generate a fresh delta).
+#
+# But with the pack mru, we jump from delta to delta
+# following the traversal order:
+#
+# a. We grab blob X from pack X as a delta, putting it at
+# the tip of our mru list.
+#
+# b. Eventually we move onto commit X-1. We need other
+# objects which are only in pack X-1 (in the test code
+# below, it's the containing tree). That puts pack X-1
+# at the tip of our mru list.
+#
+# c. Eventually we look for blob X-1, and we find the
+# version in pack X-1 (because it's the mru tip).
+#
+# Now we have blob X as a delta against X-1, which is a delta
+# against X-2, and so forth.
+#
+# In the real world, these small pushes would get exploded by
+# unpack-objects rather than "index-pack --fix-thin", but the
+# same principle applies to larger pushes (they only need one
+# repeatedly-modified file to generate the delta chain).
+
+test_expect_success 'create series of packs' '
+ test-tool genrandom foo 4096 >content &&
+ prev= &&
+ for i in $(test_seq 1 10)
+ do
+ cat content >file &&
+ echo $i >>file &&
+ git add file &&
+ git commit -m $i &&
+ cur=$(git rev-parse HEAD^{tree}) &&
+ {
+ if test -n "$prev"
+ then
+ echo "-$prev"
+ fi &&
+ echo $cur &&
+ echo "$(git rev-parse :file) file"
+ } | git pack-objects --stdout >tmp &&
+ GIT_TRACE2_EVENT=$PWD/trace \
+ git index-pack -v --stdin --fix-thin <tmp || return 1 &&
+ grep -c region_enter.*progress trace >enter &&
+ grep -c region_leave.*progress trace >leave &&
+ test_cmp enter leave &&
+ prev=$cur
+ done
+'
+
+max_chain() {
+ git index-pack --verify-stat-only "$1" >output &&
+ perl -lne '
+ BEGIN { $len = 0 }
+ /chain length = (\d+)/ and $len = $1;
+ END { print $len }
+ ' output
+}
+
+# Note that this whole setup is pretty reliant on the current
+# packing heuristics. We double-check that our test case
+# actually produces a long chain. If it doesn't, it should be
+# adjusted (or scrapped if the heuristics have become too unreliable)
+test_expect_success 'packing produces a long delta' '
+ # Use --window=0 to make sure we are seeing reused deltas,
+ # not computing a new long chain.
+ pack=$(git pack-objects --all --window=0 </dev/null pack) &&
+ echo 9 >expect &&
+ max_chain pack-$pack.pack >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--depth limits depth' '
+ pack=$(git pack-objects --all --depth=5 </dev/null pack) &&
+ echo 5 >expect &&
+ max_chain pack-$pack.pack >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--depth=0 disables deltas' '
+ pack=$(git pack-objects --all --depth=0 </dev/null pack) &&
+ echo 0 >expect &&
+ max_chain pack-$pack.pack >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'negative depth disables deltas' '
+ pack=$(git pack-objects --all --depth=-1 </dev/null pack) &&
+ echo 0 >expect &&
+ max_chain pack-$pack.pack >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t5317-pack-objects-filter-objects.sh b/t/t5317-pack-objects-filter-objects.sh
new file mode 100755
index 0000000..2ff3eef
--- /dev/null
+++ b/t/t5317-pack-objects-filter-objects.sh
@@ -0,0 +1,482 @@
+#!/bin/sh
+
+test_description='git pack-objects using object filtering'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# Test blob:none filter.
+
+test_expect_success 'setup r1' '
+ git init r1 &&
+ for n in 1 2 3 4 5
+ do
+ echo "This is file: $n" > r1/file.$n &&
+ git -C r1 add file.$n &&
+ git -C r1 commit -m "$n" || return 1
+ done
+'
+
+parse_verify_pack_blob_oid () {
+ awk '{print $1}' -
+}
+
+test_expect_success 'verify blob count in normal packfile' '
+ git -C r1 ls-files -s file.1 file.2 file.3 file.4 file.5 \
+ >ls_files_result &&
+ test_parse_ls_files_stage_oids <ls_files_result |
+ sort >expected &&
+
+ git -C r1 pack-objects --revs --stdout >all.pack <<-EOF &&
+ HEAD
+ EOF
+ git -C r1 index-pack ../all.pack &&
+
+ git -C r1 verify-pack -v ../all.pack >verify_result &&
+ grep blob verify_result |
+ parse_verify_pack_blob_oid |
+ sort >observed &&
+
+ test_cmp expected observed
+'
+
+test_expect_success 'verify blob:none packfile has no blobs' '
+ git -C r1 pack-objects --revs --stdout --filter=blob:none >filter.pack <<-EOF &&
+ HEAD
+ EOF
+ git -C r1 index-pack ../filter.pack &&
+
+ git -C r1 verify-pack -v ../filter.pack >verify_result &&
+ ! grep blob verify_result
+'
+
+test_expect_success 'verify blob:none packfile without --stdout' '
+ git -C r1 pack-objects --revs --filter=blob:none mypackname >packhash <<-EOF &&
+ HEAD
+ EOF
+ git -C r1 verify-pack -v "mypackname-$(cat packhash).pack" >verify_result &&
+ ! grep blob verify_result
+'
+
+test_expect_success 'verify normal and blob:none packfiles have same commits/trees' '
+ git -C r1 verify-pack -v ../all.pack >verify_result &&
+ grep -E "commit|tree" verify_result |
+ parse_verify_pack_blob_oid |
+ sort >expected &&
+
+ git -C r1 verify-pack -v ../filter.pack >verify_result &&
+ grep -E "commit|tree" verify_result |
+ parse_verify_pack_blob_oid |
+ sort >observed &&
+
+ test_cmp expected observed
+'
+
+test_expect_success 'get an error for missing tree object' '
+ git init r5 &&
+ echo foo >r5/foo &&
+ git -C r5 add foo &&
+ git -C r5 commit -m "foo" &&
+ git -C r5 rev-parse HEAD^{tree} >tree &&
+ del=$(sed "s|..|&/|" tree) &&
+ rm r5/.git/objects/$del &&
+ test_must_fail git -C r5 pack-objects --revs --stdout 2>bad_tree <<-EOF &&
+ HEAD
+ EOF
+ grep "bad tree object" bad_tree
+'
+
+test_expect_success 'setup for tests of tree:0' '
+ mkdir r1/subtree &&
+ echo "This is a file in a subtree" >r1/subtree/file &&
+ git -C r1 add subtree/file &&
+ git -C r1 commit -m subtree
+'
+
+test_expect_success 'verify tree:0 packfile has no blobs or trees' '
+ git -C r1 pack-objects --revs --stdout --filter=tree:0 >commitsonly.pack <<-EOF &&
+ HEAD
+ EOF
+ git -C r1 index-pack ../commitsonly.pack &&
+ git -C r1 verify-pack -v ../commitsonly.pack >objs &&
+ ! grep -E "tree|blob" objs
+'
+
+test_expect_success 'grab tree directly when using tree:0' '
+ # We should get the tree specified directly but not its blobs or subtrees.
+ git -C r1 pack-objects --revs --stdout --filter=tree:0 >commitsonly.pack <<-EOF &&
+ HEAD:
+ EOF
+ git -C r1 index-pack ../commitsonly.pack &&
+ git -C r1 verify-pack -v ../commitsonly.pack >objs &&
+ awk "/tree|blob/{print \$1}" objs >trees_and_blobs &&
+ git -C r1 rev-parse HEAD: >expected &&
+ test_cmp expected trees_and_blobs
+'
+
+# Test blob:limit=<n>[kmg] filter.
+# We boundary test around the size parameter. The filter is strictly less than
+# the value, so size 500 and 1000 should have the same results, but 1001 should
+# filter more.
+
+test_expect_success 'setup r2' '
+ git init r2 &&
+ for n in 1000 10000
+ do
+ printf "%"$n"s" X > r2/large.$n &&
+ git -C r2 add large.$n &&
+ git -C r2 commit -m "$n" || return 1
+ done
+'
+
+test_expect_success 'verify blob count in normal packfile' '
+ git -C r2 ls-files -s large.1000 large.10000 >ls_files_result &&
+ test_parse_ls_files_stage_oids <ls_files_result |
+ sort >expected &&
+
+ git -C r2 pack-objects --revs --stdout >all.pack <<-EOF &&
+ HEAD
+ EOF
+ git -C r2 index-pack ../all.pack &&
+
+ git -C r2 verify-pack -v ../all.pack >verify_result &&
+ grep blob verify_result |
+ parse_verify_pack_blob_oid |
+ sort >observed &&
+
+ test_cmp expected observed
+'
+
+test_expect_success 'verify blob:limit=500 omits all blobs' '
+ git -C r2 pack-objects --revs --stdout --filter=blob:limit=500 >filter.pack <<-EOF &&
+ HEAD
+ EOF
+ git -C r2 index-pack ../filter.pack &&
+
+ git -C r2 verify-pack -v ../filter.pack >verify_result &&
+ ! grep blob verify_result
+'
+
+test_expect_success 'verify blob:limit=1000' '
+ git -C r2 pack-objects --revs --stdout --filter=blob:limit=1000 >filter.pack <<-EOF &&
+ HEAD
+ EOF
+ git -C r2 index-pack ../filter.pack &&
+
+ git -C r2 verify-pack -v ../filter.pack >verify_result &&
+ ! grep blob verify_result
+'
+
+test_expect_success 'verify blob:limit=1001' '
+ git -C r2 ls-files -s large.1000 >ls_files_result &&
+ test_parse_ls_files_stage_oids <ls_files_result |
+ sort >expected &&
+
+ git -C r2 pack-objects --revs --stdout --filter=blob:limit=1001 >filter.pack <<-EOF &&
+ HEAD
+ EOF
+ git -C r2 index-pack ../filter.pack &&
+
+ git -C r2 verify-pack -v ../filter.pack >verify_result &&
+ grep blob verify_result |
+ parse_verify_pack_blob_oid |
+ sort >observed &&
+
+ test_cmp expected observed
+'
+
+test_expect_success 'verify blob:limit=10001' '
+ git -C r2 ls-files -s large.1000 large.10000 >ls_files_result &&
+ test_parse_ls_files_stage_oids <ls_files_result |
+ sort >expected &&
+
+ git -C r2 pack-objects --revs --stdout --filter=blob:limit=10001 >filter.pack <<-EOF &&
+ HEAD
+ EOF
+ git -C r2 index-pack ../filter.pack &&
+
+ git -C r2 verify-pack -v ../filter.pack >verify_result &&
+ grep blob verify_result |
+ parse_verify_pack_blob_oid |
+ sort >observed &&
+
+ test_cmp expected observed
+'
+
+test_expect_success 'verify blob:limit=1k' '
+ git -C r2 ls-files -s large.1000 >ls_files_result &&
+ test_parse_ls_files_stage_oids <ls_files_result |
+ sort >expected &&
+
+ git -C r2 pack-objects --revs --stdout --filter=blob:limit=1k >filter.pack <<-EOF &&
+ HEAD
+ EOF
+ git -C r2 index-pack ../filter.pack &&
+
+ git -C r2 verify-pack -v ../filter.pack >verify_result &&
+ grep blob verify_result |
+ parse_verify_pack_blob_oid |
+ sort >observed &&
+
+ test_cmp expected observed
+'
+
+test_expect_success 'verify explicitly specifying oversized blob in input' '
+ git -C r2 ls-files -s large.1000 large.10000 >ls_files_result &&
+ test_parse_ls_files_stage_oids <ls_files_result |
+ sort >expected &&
+
+ echo HEAD >objects &&
+ git -C r2 rev-parse HEAD:large.10000 >>objects &&
+ git -C r2 pack-objects --revs --stdout --filter=blob:limit=1k <objects >filter.pack &&
+ git -C r2 index-pack ../filter.pack &&
+
+ git -C r2 verify-pack -v ../filter.pack >verify_result &&
+ grep blob verify_result |
+ parse_verify_pack_blob_oid |
+ sort >observed &&
+
+ test_cmp expected observed
+'
+
+test_expect_success 'verify blob:limit=1m' '
+ git -C r2 ls-files -s large.1000 large.10000 >ls_files_result &&
+ test_parse_ls_files_stage_oids <ls_files_result |
+ sort >expected &&
+
+ git -C r2 pack-objects --revs --stdout --filter=blob:limit=1m >filter.pack <<-EOF &&
+ HEAD
+ EOF
+ git -C r2 index-pack ../filter.pack &&
+
+ git -C r2 verify-pack -v ../filter.pack >verify_result &&
+ grep blob verify_result |
+ parse_verify_pack_blob_oid |
+ sort >observed &&
+
+ test_cmp expected observed
+'
+
+test_expect_success 'verify normal and blob:limit packfiles have same commits/trees' '
+ git -C r2 verify-pack -v ../all.pack >verify_result &&
+ grep -E "commit|tree" verify_result |
+ parse_verify_pack_blob_oid |
+ sort >expected &&
+
+ git -C r2 verify-pack -v ../filter.pack >verify_result &&
+ grep -E "commit|tree" verify_result |
+ parse_verify_pack_blob_oid |
+ sort >observed &&
+
+ test_cmp expected observed
+'
+
+test_expect_success 'verify small limit and big limit results in small limit' '
+ git -C r2 ls-files -s large.1000 >ls_files_result &&
+ test_parse_ls_files_stage_oids <ls_files_result |
+ sort >expected &&
+
+ git -C r2 pack-objects --revs --stdout --filter=blob:limit=1001 \
+ --filter=blob:limit=10001 >filter.pack <<-EOF &&
+ HEAD
+ EOF
+ git -C r2 index-pack ../filter.pack &&
+
+ git -C r2 verify-pack -v ../filter.pack >verify_result &&
+ grep blob verify_result |
+ parse_verify_pack_blob_oid |
+ sort >observed &&
+
+ test_cmp expected observed
+'
+
+test_expect_success 'verify big limit and small limit results in small limit' '
+ git -C r2 ls-files -s large.1000 >ls_files_result &&
+ test_parse_ls_files_stage_oids <ls_files_result |
+ sort >expected &&
+
+ git -C r2 pack-objects --revs --stdout --filter=blob:limit=10001 \
+ --filter=blob:limit=1001 >filter.pack <<-EOF &&
+ HEAD
+ EOF
+ git -C r2 index-pack ../filter.pack &&
+
+ git -C r2 verify-pack -v ../filter.pack >verify_result &&
+ grep blob verify_result |
+ parse_verify_pack_blob_oid |
+ sort >observed &&
+
+ test_cmp expected observed
+'
+
+# Test sparse:path=<path> filter.
+# !!!!
+# NOTE: sparse:path filter support has been dropped for security reasons,
+# so the tests have been changed to make sure that using it fails.
+# !!!!
+# Use a local file containing a sparse-checkout specification to filter
+# out blobs not required for the corresponding sparse-checkout. We do not
+# require sparse-checkout to actually be enabled.
+
+test_expect_success 'setup r3' '
+ git init r3 &&
+ mkdir r3/dir1 &&
+ for n in sparse1 sparse2
+ do
+ echo "This is file: $n" > r3/$n &&
+ git -C r3 add $n &&
+ echo "This is file: dir1/$n" > r3/dir1/$n &&
+ git -C r3 add dir1/$n || return 1
+ done &&
+ git -C r3 commit -m "sparse" &&
+ echo dir1/ >pattern1 &&
+ echo sparse1 >pattern2
+'
+
+test_expect_success 'verify blob count in normal packfile' '
+ git -C r3 ls-files -s sparse1 sparse2 dir1/sparse1 dir1/sparse2 \
+ >ls_files_result &&
+ test_parse_ls_files_stage_oids <ls_files_result |
+ sort >expected &&
+
+ git -C r3 pack-objects --revs --stdout >all.pack <<-EOF &&
+ HEAD
+ EOF
+ git -C r3 index-pack ../all.pack &&
+
+ git -C r3 verify-pack -v ../all.pack >verify_result &&
+ grep blob verify_result |
+ parse_verify_pack_blob_oid |
+ sort >observed &&
+
+ test_cmp expected observed
+'
+
+test_expect_success 'verify sparse:path=pattern1 fails' '
+ test_must_fail git -C r3 pack-objects --revs --stdout \
+ --filter=sparse:path=../pattern1 <<-EOF
+ HEAD
+ EOF
+'
+
+test_expect_success 'verify sparse:path=pattern2 fails' '
+ test_must_fail git -C r3 pack-objects --revs --stdout \
+ --filter=sparse:path=../pattern2 <<-EOF
+ HEAD
+ EOF
+'
+
+# Test sparse:oid=<oid-ish> filter.
+# Use a blob containing a sparse-checkout specification to filter
+# out blobs not required for the corresponding sparse-checkout. We do not
+# require sparse-checkout to actually be enabled.
+
+test_expect_success 'setup r4' '
+ git init r4 &&
+ mkdir r4/dir1 &&
+ for n in sparse1 sparse2
+ do
+ echo "This is file: $n" > r4/$n &&
+ git -C r4 add $n &&
+ echo "This is file: dir1/$n" > r4/dir1/$n &&
+ git -C r4 add dir1/$n || return 1
+ done &&
+ echo dir1/ >r4/pattern &&
+ git -C r4 add pattern &&
+ git -C r4 commit -m "pattern"
+'
+
+test_expect_success 'verify blob count in normal packfile' '
+ git -C r4 ls-files -s pattern sparse1 sparse2 dir1/sparse1 dir1/sparse2 \
+ >ls_files_result &&
+ test_parse_ls_files_stage_oids <ls_files_result |
+ sort >expected &&
+
+ git -C r4 pack-objects --revs --stdout >all.pack <<-EOF &&
+ HEAD
+ EOF
+ git -C r4 index-pack ../all.pack &&
+
+ git -C r4 verify-pack -v ../all.pack >verify_result &&
+ grep blob verify_result |
+ parse_verify_pack_blob_oid |
+ sort >observed &&
+
+ test_cmp expected observed
+'
+
+test_expect_success 'verify sparse:oid=OID' '
+ git -C r4 ls-files -s dir1/sparse1 dir1/sparse2 >ls_files_result &&
+ test_parse_ls_files_stage_oids <ls_files_result |
+ sort >expected &&
+
+ git -C r4 ls-files -s pattern >staged &&
+ oid=$(test_parse_ls_files_stage_oids <staged) &&
+ git -C r4 pack-objects --revs --stdout --filter=sparse:oid=$oid >filter.pack <<-EOF &&
+ HEAD
+ EOF
+ git -C r4 index-pack ../filter.pack &&
+
+ git -C r4 verify-pack -v ../filter.pack >verify_result &&
+ grep blob verify_result |
+ parse_verify_pack_blob_oid |
+ sort >observed &&
+
+ test_cmp expected observed
+'
+
+test_expect_success 'verify sparse:oid=oid-ish' '
+ git -C r4 ls-files -s dir1/sparse1 dir1/sparse2 >ls_files_result &&
+ test_parse_ls_files_stage_oids <ls_files_result |
+ sort >expected &&
+
+ git -C r4 pack-objects --revs --stdout --filter=sparse:oid=main:pattern >filter.pack <<-EOF &&
+ HEAD
+ EOF
+ git -C r4 index-pack ../filter.pack &&
+
+ git -C r4 verify-pack -v ../filter.pack >verify_result &&
+ grep blob verify_result |
+ parse_verify_pack_blob_oid |
+ sort >observed &&
+
+ test_cmp expected observed
+'
+
+# Delete some loose objects and use pack-objects, but WITHOUT any filtering.
+# This models previously omitted objects that we did not receive.
+
+test_expect_success 'setup r1 - delete loose blobs' '
+ git -C r1 ls-files -s file.1 file.2 file.3 file.4 file.5 \
+ >ls_files_result &&
+ test_parse_ls_files_stage_oids <ls_files_result |
+ sort >expected &&
+
+ for id in `cat expected | sed "s|..|&/|"`
+ do
+ rm r1/.git/objects/$id || return 1
+ done
+'
+
+test_expect_success 'verify pack-objects fails w/ missing objects' '
+ test_must_fail git -C r1 pack-objects --revs --stdout >miss.pack <<-EOF
+ HEAD
+ EOF
+'
+
+test_expect_success 'verify pack-objects fails w/ --missing=error' '
+ test_must_fail git -C r1 pack-objects --revs --stdout --missing=error >miss.pack <<-EOF
+ HEAD
+ EOF
+'
+
+test_expect_success 'verify pack-objects w/ --missing=allow-any' '
+ git -C r1 pack-objects --revs --stdout --missing=allow-any >miss.pack <<-EOF
+ HEAD
+ EOF
+'
+
+test_done
diff --git a/t/t5318-commit-graph.sh b/t/t5318-commit-graph.sh
new file mode 100755
index 0000000..d4fc65e
--- /dev/null
+++ b/t/t5318-commit-graph.sh
@@ -0,0 +1,946 @@
+#!/bin/sh
+
+test_description='commit graph'
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-chunk.sh
+
+GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS=0
+
+test_expect_success 'usage' '
+ test_expect_code 129 git commit-graph write blah 2>err &&
+ test_expect_code 129 git commit-graph write verify
+'
+
+test_expect_success 'usage shown without sub-command' '
+ test_expect_code 129 git commit-graph 2>err &&
+ grep usage: err
+'
+
+test_expect_success 'usage shown with an error on unknown sub-command' '
+ cat >expect <<-\EOF &&
+ error: unknown subcommand: `unknown'\''
+ EOF
+ test_expect_code 129 git commit-graph unknown 2>stderr &&
+ grep error stderr >actual &&
+ test_cmp expect actual
+'
+
+objdir=".git/objects"
+
+test_expect_success 'setup full repo' '
+ git init full
+'
+
+test_expect_success POSIXPERM 'tweak umask for modebit tests' '
+ umask 022
+'
+
+test_expect_success 'verify graph with no graph file' '
+ git -C full commit-graph verify
+'
+
+test_expect_success 'write graph with no packs' '
+ git -C full commit-graph write --object-dir $objdir &&
+ test_path_is_missing full/$objdir/info/commit-graph
+'
+
+test_expect_success 'exit with correct error on bad input to --stdin-packs' '
+ echo doesnotexist >in &&
+ test_expect_code 1 git -C full commit-graph write --stdin-packs \
+ <in 2>stderr &&
+ test_grep "error adding pack" stderr
+'
+
+test_expect_success 'create commits and repack' '
+ for i in $(test_seq 3)
+ do
+ test_commit -C full $i &&
+ git -C full branch commits/$i || return 1
+ done &&
+ git -C full repack
+'
+
+. "$TEST_DIRECTORY"/lib-commit-graph.sh
+
+graph_git_behavior 'no graph' full commits/3 commits/1
+
+test_expect_success 'exit with correct error on bad input to --stdin-commits' '
+ # invalid, non-hex OID
+ echo HEAD | test_expect_code 1 git -C full commit-graph write \
+ --stdin-commits 2>stderr &&
+ test_grep "unexpected non-hex object ID: HEAD" stderr &&
+ # non-existent OID
+ echo $ZERO_OID | test_expect_code 1 git -C full commit-graph write \
+ --stdin-commits 2>stderr &&
+ test_grep "invalid object" stderr &&
+ # valid commit and tree OID
+ git -C full rev-parse HEAD HEAD^{tree} >in &&
+ git -C full commit-graph write --stdin-commits <in &&
+ graph_read_expect -C full 3 generation_data
+'
+
+test_expect_success 'write graph' '
+ git -C full commit-graph write &&
+ test_path_is_file full/$objdir/info/commit-graph &&
+ graph_read_expect -C full 3 generation_data
+'
+
+test_expect_success POSIXPERM 'write graph has correct permissions' '
+ test_path_is_file full/$objdir/info/commit-graph &&
+ echo "-r--r--r--" >expect &&
+ test_modebits full/$objdir/info/commit-graph >actual &&
+ test_cmp expect actual
+'
+
+graph_git_behavior 'graph exists' full commits/3 commits/1
+
+test_expect_success 'Add more commits' '
+ git -C full reset --hard commits/1 &&
+ for i in $(test_seq 4 5)
+ do
+ test_commit -C full $i &&
+ git -C full branch commits/$i || return 1
+ done &&
+ git -C full reset --hard commits/2 &&
+ for i in $(test_seq 6 7)
+ do
+ test_commit -C full $i &&
+ git -C full branch commits/$i || return 1
+ done &&
+ git -C full reset --hard commits/2 &&
+ git -C full merge commits/4 &&
+ git -C full branch merge/1 &&
+ git -C full reset --hard commits/4 &&
+ git -C full merge commits/6 &&
+ git -C full branch merge/2 &&
+ git -C full reset --hard commits/3 &&
+ git -C full merge commits/5 commits/7 &&
+ git -C full branch merge/3 &&
+ git -C full repack
+'
+
+test_expect_success 'commit-graph write progress off for redirected stderr' '
+ git -C full commit-graph write 2>err &&
+ test_must_be_empty err
+'
+
+test_expect_success 'commit-graph write force progress on for stderr' '
+ GIT_PROGRESS_DELAY=0 git -C full commit-graph write --progress 2>err &&
+ test_file_not_empty err
+'
+
+test_expect_success 'commit-graph write with the --no-progress option' '
+ git -C full commit-graph write --no-progress 2>err &&
+ test_must_be_empty err
+'
+
+test_expect_success 'commit-graph write --stdin-commits progress off for redirected stderr' '
+ git -C full rev-parse commits/5 >in &&
+ git -C full commit-graph write --stdin-commits <in 2>err &&
+ test_must_be_empty err
+'
+
+test_expect_success 'commit-graph write --stdin-commits force progress on for stderr' '
+ git -C full rev-parse commits/5 >in &&
+ GIT_PROGRESS_DELAY=0 git -C full commit-graph write --stdin-commits \
+ --progress <in 2>err &&
+ test_grep "Collecting commits from input" err
+'
+
+test_expect_success 'commit-graph write --stdin-commits with the --no-progress option' '
+ git -C full rev-parse commits/5 >in &&
+ git -C full commit-graph write --stdin-commits --no-progress <in 2>err &&
+ test_must_be_empty err
+'
+
+test_expect_success 'commit-graph verify progress off for redirected stderr' '
+ git -C full commit-graph verify 2>err &&
+ test_must_be_empty err
+'
+
+test_expect_success 'commit-graph verify force progress on for stderr' '
+ GIT_PROGRESS_DELAY=0 git -C full commit-graph verify --progress 2>err &&
+ test_file_not_empty err
+'
+
+test_expect_success 'commit-graph verify with the --no-progress option' '
+ git -C full commit-graph verify --no-progress 2>err &&
+ test_must_be_empty err
+'
+
+# Current graph structure:
+#
+# __M3___
+# / | \
+# 3 M1 5 M2 7
+# |/ \|/ \|
+# 2 4 6
+# |___/____/
+# 1
+
+test_expect_success 'write graph with merges' '
+ git -C full commit-graph write &&
+ test_path_is_file full/$objdir/info/commit-graph &&
+ graph_read_expect -C full 10 "generation_data extra_edges"
+'
+
+graph_git_behavior 'merge 1 vs 2' full merge/1 merge/2
+graph_git_behavior 'merge 1 vs 3' full merge/1 merge/3
+graph_git_behavior 'merge 2 vs 3' full merge/2 merge/3
+
+test_expect_success 'Add one more commit' '
+ test_commit -C full 8 &&
+ git -C full branch commits/8 &&
+ ls full/$objdir/pack | grep idx >existing-idx &&
+ git -C full repack &&
+ ls full/$objdir/pack| grep idx | grep -v -f existing-idx >new-idx
+'
+
+# Current graph structure:
+#
+# 8
+# |
+# __M3___
+# / | \
+# 3 M1 5 M2 7
+# |/ \|/ \|
+# 2 4 6
+# |___/____/
+# 1
+
+graph_git_behavior 'mixed mode, commit 8 vs merge 1' full commits/8 merge/1
+graph_git_behavior 'mixed mode, commit 8 vs merge 2' full commits/8 merge/2
+
+test_expect_success 'write graph with new commit' '
+ git -C full commit-graph write &&
+ test_path_is_file full/$objdir/info/commit-graph &&
+ graph_read_expect -C full 11 "generation_data extra_edges"
+'
+
+graph_git_behavior 'full graph, commit 8 vs merge 1' full commits/8 merge/1
+graph_git_behavior 'full graph, commit 8 vs merge 2' full commits/8 merge/2
+
+test_expect_success 'write graph with nothing new' '
+ git -C full commit-graph write &&
+ test_path_is_file full/$objdir/info/commit-graph &&
+ graph_read_expect -C full 11 "generation_data extra_edges"
+'
+
+graph_git_behavior 'cleared graph, commit 8 vs merge 1' full commits/8 merge/1
+graph_git_behavior 'cleared graph, commit 8 vs merge 2' full commits/8 merge/2
+
+test_expect_success 'build graph from latest pack with closure' '
+ git -C full commit-graph write --stdin-packs <new-idx &&
+ test_path_is_file full/$objdir/info/commit-graph &&
+ graph_read_expect -C full 9 "generation_data extra_edges"
+'
+
+graph_git_behavior 'graph from pack, commit 8 vs merge 1' full commits/8 merge/1
+graph_git_behavior 'graph from pack, commit 8 vs merge 2' full commits/8 merge/2
+
+test_expect_success 'build graph from commits with closure' '
+ git -C full tag -a -m "merge" tag/merge merge/2 &&
+ git -C full rev-parse tag/merge >commits-in &&
+ git -C full rev-parse merge/1 >>commits-in &&
+ git -C full commit-graph write --stdin-commits <commits-in &&
+ test_path_is_file full/$objdir/info/commit-graph &&
+ graph_read_expect -C full 6 "generation_data"
+'
+
+graph_git_behavior 'graph from commits, commit 8 vs merge 1' full commits/8 merge/1
+graph_git_behavior 'graph from commits, commit 8 vs merge 2' full commits/8 merge/2
+
+test_expect_success 'build graph from commits with append' '
+ git -C full rev-parse merge/3 >in &&
+ git -C full commit-graph write --stdin-commits --append <in &&
+ test_path_is_file full/$objdir/info/commit-graph &&
+ graph_read_expect -C full 10 "generation_data extra_edges"
+'
+
+graph_git_behavior 'append graph, commit 8 vs merge 1' full commits/8 merge/1
+graph_git_behavior 'append graph, commit 8 vs merge 2' full commits/8 merge/2
+
+test_expect_success 'build graph using --reachable' '
+ git -C full commit-graph write --reachable &&
+ test_path_is_file full/$objdir/info/commit-graph &&
+ graph_read_expect -C full 11 "generation_data extra_edges"
+'
+
+graph_git_behavior 'append graph, commit 8 vs merge 1' full commits/8 merge/1
+graph_git_behavior 'append graph, commit 8 vs merge 2' full commits/8 merge/2
+
+test_expect_success 'setup bare repo' '
+ git clone --bare --no-local full bare
+'
+
+graph_git_behavior 'bare repo, commit 8 vs merge 1' bare commits/8 merge/1
+graph_git_behavior 'bare repo, commit 8 vs merge 2' bare commits/8 merge/2
+
+test_expect_success 'write graph in bare repo' '
+ git -C bare commit-graph write &&
+ test_path_is_file bare/objects/info/commit-graph &&
+ graph_read_expect -C bare 11 "generation_data extra_edges"
+'
+
+graph_git_behavior 'bare repo with graph, commit 8 vs merge 1' bare commits/8 merge/1
+graph_git_behavior 'bare repo with graph, commit 8 vs merge 2' bare commits/8 merge/2
+
+test_expect_success 'perform fast-forward merge in full repo' '
+ git -C full checkout -b merge-5-to-8 commits/5 &&
+ git -C full merge commits/8 &&
+ git -C full show-ref -s merge-5-to-8 >output &&
+ git -C full show-ref -s commits/8 >expect &&
+ test_cmp expect output
+'
+
+test_expect_success 'check that gc computes commit-graph' '
+ test_commit -C full --no-tag blank &&
+ git -C full commit-graph write --reachable &&
+ cp full/$objdir/info/commit-graph commit-graph-before-gc &&
+ git -C full reset --hard HEAD~1 &&
+ test_config -C full gc.writeCommitGraph true &&
+ git -C full gc &&
+ cp full/$objdir/info/commit-graph commit-graph-after-gc &&
+ ! test_cmp_bin commit-graph-before-gc commit-graph-after-gc &&
+ git -C full commit-graph write --reachable &&
+ test_cmp_bin commit-graph-after-gc full/$objdir/info/commit-graph
+'
+
+test_expect_success 'replace-objects invalidates commit-graph' '
+ test_when_finished rm -rf replace &&
+ git clone full replace &&
+ (
+ cd replace &&
+ git commit-graph write --reachable &&
+ test_path_is_file .git/objects/info/commit-graph &&
+ git replace HEAD~1 HEAD~2 &&
+ graph_git_two_modes "commit-graph verify" &&
+ git -c core.commitGraph=false log >expect &&
+ git -c core.commitGraph=true log >actual &&
+ test_cmp expect actual &&
+ git commit-graph write --reachable &&
+ git -c core.commitGraph=false --no-replace-objects log >expect &&
+ git -c core.commitGraph=true --no-replace-objects log >actual &&
+ test_cmp expect actual &&
+ rm -rf .git/objects/info/commit-graph &&
+ git commit-graph write --reachable &&
+ test_path_is_file .git/objects/info/commit-graph
+ )
+'
+
+test_expect_success 'commit grafts invalidate commit-graph' '
+ test_when_finished rm -rf graft &&
+ git clone --template= full graft &&
+ (
+ cd graft &&
+ git commit-graph write --reachable &&
+ test_path_is_file .git/objects/info/commit-graph &&
+ H1=$(git rev-parse --verify HEAD~1) &&
+ H3=$(git rev-parse --verify HEAD~3) &&
+ mkdir .git/info &&
+ echo "$H1 $H3" >.git/info/grafts &&
+ git -c core.commitGraph=false log >expect &&
+ git -c core.commitGraph=true log >actual &&
+ test_cmp expect actual &&
+ git commit-graph write --reachable &&
+ git -c core.commitGraph=false --no-replace-objects log >expect &&
+ git -c core.commitGraph=true --no-replace-objects log >actual &&
+ test_cmp expect actual &&
+ rm -rf .git/objects/info/commit-graph &&
+ git commit-graph write --reachable &&
+ test_path_is_missing .git/objects/info/commit-graph
+ )
+'
+
+test_expect_success 'replace-objects invalidates commit-graph' '
+ test_when_finished rm -rf shallow &&
+ git clone --depth 2 "file://$TRASH_DIRECTORY/full" shallow &&
+ (
+ cd shallow &&
+ git commit-graph write --reachable &&
+ test_path_is_missing .git/objects/info/commit-graph &&
+ git fetch origin --unshallow &&
+ git commit-graph write --reachable &&
+ test_path_is_file .git/objects/info/commit-graph
+ )
+'
+
+test_expect_success 'warn on improper hash version' '
+ git init --object-format=sha1 sha1 &&
+ (
+ cd sha1 &&
+ test_commit 1 &&
+ git commit-graph write --reachable &&
+ mv .git/objects/info/commit-graph ../cg-sha1
+ ) &&
+ git init --object-format=sha256 sha256 &&
+ (
+ cd sha256 &&
+ test_commit 1 &&
+ git commit-graph write --reachable &&
+ mv .git/objects/info/commit-graph ../cg-sha256
+ ) &&
+ (
+ cd sha1 &&
+ mv ../cg-sha256 .git/objects/info/commit-graph &&
+ git log -1 2>err &&
+ test_grep "commit-graph hash version 2 does not match version 1" err
+ ) &&
+ (
+ cd sha256 &&
+ mv ../cg-sha1 .git/objects/info/commit-graph &&
+ git log -1 2>err &&
+ test_grep "commit-graph hash version 1 does not match version 2" err
+ )
+'
+
+test_expect_success TIME_IS_64BIT,TIME_T_IS_64BIT 'lower layers have overflow chunk' '
+ UNIX_EPOCH_ZERO="@0 +0000" &&
+ FUTURE_DATE="@4147483646 +0000" &&
+ rm -f full/.git/objects/info/commit-graph &&
+ test_commit -C full --date "$FUTURE_DATE" future-1 &&
+ test_commit -C full --date "$UNIX_EPOCH_ZERO" old-1 &&
+ git -C full commit-graph write --reachable &&
+ test_commit -C full --date "$FUTURE_DATE" future-2 &&
+ test_commit -C full --date "$UNIX_EPOCH_ZERO" old-2 &&
+ git -C full commit-graph write --reachable --split=no-merge &&
+ test_commit -C full extra &&
+ git -C full commit-graph write --reachable --split=no-merge &&
+ git -C full commit-graph write --reachable &&
+ graph_read_expect -C full 16 \
+ "generation_data generation_data_overflow extra_edges" &&
+ mv full/.git/objects/info/commit-graph commit-graph-upgraded &&
+ git -C full commit-graph write --reachable &&
+ graph_read_expect -C full 16 \
+ "generation_data generation_data_overflow extra_edges" &&
+ test_cmp full/.git/objects/info/commit-graph commit-graph-upgraded
+'
+
+# the verify tests below expect the commit-graph to contain
+# exactly the commits reachable from the commits/8 branch.
+# If the file changes the set of commits in the list, then the
+# offsets into the binary file will result in different edits
+# and the tests will likely break.
+
+test_expect_success 'git commit-graph verify' '
+ git -C full rev-parse commits/8 >in &&
+ git -C full -c commitGraph.generationVersion=1 commit-graph write \
+ --stdin-commits <in &&
+ git -C full commit-graph verify >output &&
+ graph_read_expect -C full 9 extra_edges 1
+'
+
+NUM_COMMITS=9
+NUM_OCTOPUS_EDGES=2
+HASH_LEN="$(test_oid rawsz)"
+GRAPH_BYTE_VERSION=4
+GRAPH_BYTE_HASH=5
+GRAPH_BYTE_CHUNK_COUNT=6
+GRAPH_CHUNK_LOOKUP_OFFSET=8
+GRAPH_CHUNK_LOOKUP_WIDTH=12
+GRAPH_CHUNK_LOOKUP_ROWS=5
+GRAPH_BYTE_OID_FANOUT_ID=$GRAPH_CHUNK_LOOKUP_OFFSET
+GRAPH_BYTE_OID_LOOKUP_ID=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
+ 1 * $GRAPH_CHUNK_LOOKUP_WIDTH))
+GRAPH_BYTE_COMMIT_DATA_ID=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
+ 2 * $GRAPH_CHUNK_LOOKUP_WIDTH))
+GRAPH_FANOUT_OFFSET=$(($GRAPH_CHUNK_LOOKUP_OFFSET + \
+ $GRAPH_CHUNK_LOOKUP_WIDTH * $GRAPH_CHUNK_LOOKUP_ROWS))
+GRAPH_BYTE_FANOUT1=$(($GRAPH_FANOUT_OFFSET + 4 * 4))
+GRAPH_BYTE_FANOUT2=$(($GRAPH_FANOUT_OFFSET + 4 * 255))
+GRAPH_OID_LOOKUP_OFFSET=$(($GRAPH_FANOUT_OFFSET + 4 * 256))
+GRAPH_BYTE_OID_LOOKUP_ORDER=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * 8))
+GRAPH_BYTE_OID_LOOKUP_MISSING=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * 4 + 10))
+GRAPH_COMMIT_DATA_WIDTH=$(($HASH_LEN + 16))
+GRAPH_COMMIT_DATA_OFFSET=$(($GRAPH_OID_LOOKUP_OFFSET + $HASH_LEN * $NUM_COMMITS))
+GRAPH_BYTE_COMMIT_TREE=$GRAPH_COMMIT_DATA_OFFSET
+GRAPH_BYTE_COMMIT_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN))
+GRAPH_BYTE_COMMIT_EXTRA_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 4))
+GRAPH_BYTE_COMMIT_WRONG_PARENT=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 3))
+GRAPH_BYTE_COMMIT_GENERATION=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 11))
+GRAPH_BYTE_COMMIT_GENERATION_LAST=$(($GRAPH_BYTE_COMMIT_GENERATION + $(($NUM_COMMITS - 1)) * $GRAPH_COMMIT_DATA_WIDTH))
+GRAPH_BYTE_COMMIT_DATE=$(($GRAPH_COMMIT_DATA_OFFSET + $HASH_LEN + 12))
+GRAPH_OCTOPUS_DATA_OFFSET=$(($GRAPH_COMMIT_DATA_OFFSET + \
+ $GRAPH_COMMIT_DATA_WIDTH * $NUM_COMMITS))
+GRAPH_BYTE_OCTOPUS=$(($GRAPH_OCTOPUS_DATA_OFFSET + 4))
+GRAPH_BYTE_FOOTER=$(($GRAPH_OCTOPUS_DATA_OFFSET + 4 * $NUM_OCTOPUS_EDGES))
+
+corrupt_graph_setup() {
+ test_when_finished mv commit-graph-backup full/$objdir/info/commit-graph &&
+ cp full/$objdir/info/commit-graph commit-graph-backup &&
+ chmod u+w full/$objdir/info/commit-graph
+}
+
+corrupt_graph_verify() {
+ grepstr=$1
+ test_must_fail git -C full commit-graph verify 2>test_err &&
+ grep -v "^+" test_err >err &&
+ test_grep "$grepstr" err &&
+ if test "$2" != "no-copy"
+ then
+ cp full/$objdir/info/commit-graph commit-graph-pre-write-test
+ fi &&
+ git -C full status --short &&
+ GIT_TEST_COMMIT_GRAPH_DIE_ON_PARSE=true git -C full commit-graph write &&
+ chmod u+w full/$objdir/info/commit-graph &&
+ git -C full commit-graph verify
+}
+
+# usage: corrupt_graph_and_verify <position> <data> <string> [<zero_pos>]
+# Manipulates the commit-graph file at the position
+# by inserting the data, optionally zeroing the file
+# starting at <zero_pos>, then runs 'git commit-graph verify'
+# and places the output in the file 'err'. Test 'err' for
+# the given string.
+corrupt_graph_and_verify() {
+ pos=$1
+ data="${2:-\0}"
+ grepstr=$3
+ corrupt_graph_setup &&
+ orig_size=$(wc -c <full/$objdir/info/commit-graph) &&
+ zero_pos=${4:-${orig_size}} &&
+ printf "$data" | dd of="full/$objdir/info/commit-graph" bs=1 seek="$pos" conv=notrunc &&
+ dd of="full/$objdir/info/commit-graph" bs=1 seek="$zero_pos" if=/dev/null &&
+ test-tool genzeros $(($orig_size - $zero_pos)) >>"full/$objdir/info/commit-graph" &&
+ corrupt_graph_verify "$grepstr"
+
+}
+
+test_expect_success POSIXPERM,SANITY 'detect permission problem' '
+ corrupt_graph_setup &&
+ chmod 000 full/$objdir/info/commit-graph &&
+ corrupt_graph_verify "Could not open" "no-copy"
+'
+
+test_expect_success 'detect too small' '
+ corrupt_graph_setup &&
+ echo "a small graph" >full/$objdir/info/commit-graph &&
+ corrupt_graph_verify "too small"
+'
+
+test_expect_success 'detect bad signature' '
+ corrupt_graph_and_verify 0 "\0" \
+ "graph signature"
+'
+
+test_expect_success 'detect bad version' '
+ corrupt_graph_and_verify $GRAPH_BYTE_VERSION "\02" \
+ "graph version"
+'
+
+test_expect_success 'detect bad hash version' '
+ corrupt_graph_and_verify $GRAPH_BYTE_HASH "\03" \
+ "hash version"
+'
+
+test_expect_success 'detect low chunk count' '
+ corrupt_graph_and_verify $GRAPH_BYTE_CHUNK_COUNT "\01" \
+ "final chunk has non-zero id"
+'
+
+test_expect_success 'detect missing OID fanout chunk' '
+ corrupt_graph_and_verify $GRAPH_BYTE_OID_FANOUT_ID "\0" \
+ "missing the OID Fanout chunk"
+'
+
+test_expect_success 'detect missing OID lookup chunk' '
+ corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_ID "\0" \
+ "missing the OID Lookup chunk"
+'
+
+test_expect_success 'detect missing commit data chunk' '
+ corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_DATA_ID "\0" \
+ "missing the Commit Data chunk"
+'
+
+test_expect_success 'detect incorrect fanout' '
+ corrupt_graph_and_verify $GRAPH_BYTE_FANOUT1 "\01" \
+ "fanout value"
+'
+
+test_expect_success 'detect incorrect fanout final value' '
+ corrupt_graph_and_verify $GRAPH_BYTE_FANOUT2 "\01" \
+ "oid table and fanout disagree on size"
+'
+
+test_expect_success 'detect incorrect OID order' '
+ corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_ORDER "\01" \
+ "incorrect OID order"
+'
+
+test_expect_success 'detect OID not in object database' '
+ corrupt_graph_and_verify $GRAPH_BYTE_OID_LOOKUP_MISSING "\01" \
+ "from object database"
+'
+
+test_expect_success 'detect incorrect tree OID' '
+ corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_TREE "\01" \
+ "root tree OID for commit"
+'
+
+test_expect_success 'detect incorrect parent int-id' '
+ corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_PARENT "\01" \
+ "invalid parent"
+'
+
+test_expect_success 'detect extra parent int-id' '
+ corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_EXTRA_PARENT "\00" \
+ "is too long"
+'
+
+test_expect_success 'detect wrong parent' '
+ corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_WRONG_PARENT "\01" \
+ "commit-graph parent for"
+'
+
+test_expect_success 'detect incorrect generation number' '
+ corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_GENERATION "\070" \
+ "generation for commit"
+'
+
+test_expect_success 'detect incorrect commit date' '
+ corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_DATE "\01" \
+ "commit date"
+'
+
+test_expect_success 'detect incorrect parent for octopus merge' '
+ corrupt_graph_and_verify $GRAPH_BYTE_OCTOPUS "\01" \
+ "invalid parent"
+'
+
+test_expect_success 'detect invalid checksum hash' '
+ corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
+ "incorrect checksum"
+'
+
+test_expect_success 'detect incorrect chunk count' '
+ corrupt_graph_and_verify $GRAPH_BYTE_CHUNK_COUNT "\377" \
+ "commit-graph file is too small to hold [0-9]* chunks" \
+ $GRAPH_CHUNK_LOOKUP_OFFSET
+'
+
+test_expect_success 'detect mixed generation numbers (non-zero to zero)' '
+ corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_GENERATION_LAST "\0\0\0\0" \
+ "both zero and non-zero generations"
+'
+
+test_expect_success 'detect mixed generation numbers (zero to non-zero)' '
+ corrupt_graph_and_verify $GRAPH_BYTE_COMMIT_GENERATION "\0\0\0\0" \
+ "both zero and non-zero generations"
+'
+
+test_expect_success 'git fsck (checks commit-graph when config set to true)' '
+ git -C full fsck &&
+ corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
+ "incorrect checksum" &&
+ cp commit-graph-pre-write-test full/$objdir/info/commit-graph &&
+ test_must_fail git -C full -c core.commitGraph=true fsck
+'
+
+test_expect_success 'git fsck (ignores commit-graph when config set to false)' '
+ git -C full fsck &&
+ corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
+ "incorrect checksum" &&
+ cp commit-graph-pre-write-test full/$objdir/info/commit-graph &&
+ git -C full -c core.commitGraph=false fsck
+'
+
+test_expect_success 'git fsck (checks commit-graph when config unset)' '
+ test_when_finished "git -C full config core.commitGraph true" &&
+
+ git -C full fsck &&
+ corrupt_graph_and_verify $GRAPH_BYTE_FOOTER "\00" \
+ "incorrect checksum" &&
+ test_unconfig -C full core.commitGraph &&
+ cp commit-graph-pre-write-test full/$objdir/info/commit-graph &&
+ test_must_fail git -C full fsck
+'
+
+test_expect_success 'git fsck shows commit-graph output with --progress' '
+ git -C "$TRASH_DIRECTORY/full" fsck --progress 2>err &&
+ grep "Verifying commits in commit graph" err
+'
+
+test_expect_success 'git fsck suppresses commit-graph output with --no-progress' '
+ git -C "$TRASH_DIRECTORY/full" fsck --no-progress 2>err &&
+ ! grep "Verifying commits in commit graph" err
+'
+
+test_expect_success 'setup non-the_repository tests' '
+ rm -rf repo &&
+ git init repo &&
+ test_commit -C repo one &&
+ test_commit -C repo two &&
+ git -C repo config core.commitGraph true &&
+ git -C repo rev-parse two | \
+ git -C repo commit-graph write --stdin-commits
+'
+
+test_expect_success 'parse_commit_in_graph works for non-the_repository' '
+ test-tool repository parse_commit_in_graph \
+ repo/.git repo "$(git -C repo rev-parse two)" >actual &&
+ {
+ git -C repo log --pretty=format:"%ct " -1 &&
+ git -C repo rev-parse one
+ } >expect &&
+ test_cmp expect actual &&
+
+ test-tool repository parse_commit_in_graph \
+ repo/.git repo "$(git -C repo rev-parse one)" >actual &&
+ git -C repo log --pretty="%ct" -1 one >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'get_commit_tree_in_graph works for non-the_repository' '
+ test-tool repository get_commit_tree_in_graph \
+ repo/.git repo "$(git -C repo rev-parse two)" >actual &&
+ git -C repo rev-parse two^{tree} >expect &&
+ test_cmp expect actual &&
+
+ test-tool repository get_commit_tree_in_graph \
+ repo/.git repo "$(git -C repo rev-parse one)" >actual &&
+ git -C repo rev-parse one^{tree} >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'corrupt commit-graph write (broken parent)' '
+ rm -rf repo &&
+ git init repo &&
+ (
+ cd repo &&
+ empty="$(git mktree </dev/null)" &&
+ cat >broken <<-EOF &&
+ tree $empty
+ parent $ZERO_OID
+ author whatever <whatever@example.com> 1234 -0000
+ committer whatever <whatever@example.com> 1234 -0000
+
+ broken commit
+ EOF
+ broken="$(git hash-object -w -t commit --literally broken)" &&
+ git commit-tree -p "$broken" -m "good commit" "$empty" >good &&
+ test_must_fail git commit-graph write --stdin-commits \
+ <good 2>test_err &&
+ test_grep "unable to parse commit" test_err
+ )
+'
+
+test_expect_success 'corrupt commit-graph write (missing tree)' '
+ rm -rf repo &&
+ git init repo &&
+ (
+ cd repo &&
+ tree="$(git mktree </dev/null)" &&
+ cat >broken <<-EOF &&
+ parent $ZERO_OID
+ author whatever <whatever@example.com> 1234 -0000
+ committer whatever <whatever@example.com> 1234 -0000
+
+ broken commit
+ EOF
+ broken="$(git hash-object -w -t commit --literally broken)" &&
+ git commit-tree -p "$broken" -m "good" "$tree" >good &&
+ test_must_fail git commit-graph write --stdin-commits \
+ <good 2>test_err &&
+ test_grep "unable to parse commit" test_err
+ )
+'
+
+# We test the overflow-related code with the following repo history:
+#
+# 4:F - 5:N - 6:U
+# / \
+# 1:U - 2:N - 3:U M:N
+# \ /
+# 7:N - 8:F - 9:N
+#
+# Here the commits denoted by U have committer date of zero seconds
+# since Unix epoch, the commits denoted by N have committer date
+# starting from 1112354055 seconds since Unix epoch (default committer
+# date for the test suite), and the commits denoted by F have committer
+# date of (2 ^ 31 - 2) seconds since Unix epoch.
+#
+# The largest offset observed is 2 ^ 31, just large enough to overflow.
+#
+
+test_expect_success 'set up and verify repo with generation data overflow chunk' '
+ UNIX_EPOCH_ZERO="@0 +0000" &&
+ FUTURE_DATE="@2147483646 +0000" &&
+
+ git init repo &&
+ (
+ cd repo &&
+
+ test_commit --date "$UNIX_EPOCH_ZERO" 1 &&
+ test_commit 2 &&
+ test_commit --date "$UNIX_EPOCH_ZERO" 3 &&
+ git commit-graph write --reachable &&
+ graph_read_expect 3 generation_data &&
+ test_commit --date "$FUTURE_DATE" 4 &&
+ test_commit 5 &&
+ test_commit --date "$UNIX_EPOCH_ZERO" 6 &&
+ git branch left &&
+ git reset --hard 3 &&
+ test_commit 7 &&
+ test_commit --date "$FUTURE_DATE" 8 &&
+ test_commit 9 &&
+ git branch right &&
+ git reset --hard 3 &&
+ test_merge M left right &&
+ git commit-graph write --reachable &&
+ graph_read_expect 10 "generation_data generation_data_overflow" &&
+ git commit-graph verify
+ )
+'
+
+graph_git_behavior 'generation data overflow chunk repo' repo left right
+
+test_expect_success 'overflow during generation version upgrade' '
+ git init overflow-v2-upgrade &&
+ (
+ cd overflow-v2-upgrade &&
+
+ # This commit will have a date at two seconds past the Epoch,
+ # and a (v1) generation number of 1, since it is a root commit.
+ #
+ # The offset will then be computed as 1-2, which will underflow
+ # to 2^31, which is greater than the v2 offset small limit of
+ # 2^31-1.
+ #
+ # This is sufficient to need a large offset table for the v2
+ # generation numbers.
+ test_commit --date "@2 +0000" base &&
+ git repack -d &&
+
+ # Test that upgrading from generation v1 to v2 correctly
+ # produces the overflow table.
+ git -c commitGraph.generationVersion=1 commit-graph write &&
+ git -c commitGraph.generationVersion=2 commit-graph write \
+ --changed-paths &&
+
+ git rev-list --all
+ )
+'
+
+corrupt_chunk () {
+ graph=full/.git/objects/info/commit-graph &&
+ test_when_finished "rm -rf $graph" &&
+ git -C full commit-graph write --reachable &&
+ corrupt_chunk_file $graph "$@"
+}
+
+check_corrupt_chunk () {
+ corrupt_chunk "$@" &&
+ git -C full -c core.commitGraph=false log >expect.out &&
+ git -C full -c core.commitGraph=true log >out 2>err &&
+ test_cmp expect.out out
+}
+
+test_expect_success 'reader notices too-small oid fanout chunk' '
+ # make it big enough that the graph file is plausible,
+ # otherwise we hit an earlier check
+ check_corrupt_chunk OIDF clear $(printf "000000%02x" $(test_seq 250)) &&
+ cat >expect.err <<-\EOF &&
+ error: commit-graph oid fanout chunk is wrong size
+ error: commit-graph is missing the OID Fanout chunk
+ EOF
+ test_cmp expect.err err
+'
+
+test_expect_success 'reader notices fanout/lookup table mismatch' '
+ check_corrupt_chunk OIDF 1020 "FFFFFFFF" &&
+ cat >expect.err <<-\EOF &&
+ error: commit-graph oid table and fanout disagree on size
+ EOF
+ test_cmp expect.err err
+'
+
+test_expect_success 'reader notices out-of-bounds fanout' '
+ # Rather than try to corrupt a specific hash, we will just
+ # wreck them all. But we cannot just set them all to 0xFFFFFFFF or
+ # similar, as they are used for hi/lo starts in a binary search (so if
+ # they are identical, that indicates that the search should abort
+ # immediately). Instead, we will give them high values that differ by
+ # 2^24, ensuring that any that are used would cause an out-of-bounds
+ # read.
+ check_corrupt_chunk OIDF 0 $(printf "%02x000000" $(test_seq 0 254)) &&
+ cat >expect.err <<-\EOF &&
+ error: commit-graph fanout values out of order
+ EOF
+ test_cmp expect.err err
+'
+
+test_expect_success 'reader notices too-small commit data chunk' '
+ check_corrupt_chunk CDAT clear 00000000 &&
+ cat >expect.err <<-\EOF &&
+ error: commit-graph commit data chunk is wrong size
+ error: commit-graph is missing the Commit Data chunk
+ EOF
+ test_cmp expect.err err
+'
+
+test_expect_success 'reader notices out-of-bounds extra edge' '
+ check_corrupt_chunk EDGE clear &&
+ cat >expect.err <<-\EOF &&
+ error: commit-graph extra-edges pointer out of bounds
+ EOF
+ test_cmp expect.err err
+'
+
+test_expect_success 'reader notices too-small generations chunk' '
+ check_corrupt_chunk GDA2 clear 00000000 &&
+ cat >expect.err <<-\EOF &&
+ error: commit-graph generations chunk is wrong size
+ EOF
+ test_cmp expect.err err
+'
+
+test_expect_success 'stale commit cannot be parsed when given directly' '
+ test_when_finished "rm -rf repo" &&
+ git init repo &&
+ (
+ cd repo &&
+ test_commit A &&
+ test_commit B &&
+ git commit-graph write --reachable &&
+
+ oid=$(git rev-parse B) &&
+ rm .git/objects/"$(test_oid_to_path "$oid")" &&
+
+ # Verify that it is possible to read the commit from the
+ # commit graph when not being paranoid, ...
+ GIT_COMMIT_GRAPH_PARANOIA=false git rev-list B &&
+ # ... but parsing the commit when double checking that
+ # it actually exists in the object database should fail.
+ test_must_fail git rev-list -1 B
+ )
+'
+
+test_expect_success 'stale commit cannot be parsed when traversing graph' '
+ test_when_finished "rm -rf repo" &&
+ git init repo &&
+ (
+ cd repo &&
+
+ test_commit A &&
+ test_commit B &&
+ test_commit C &&
+ git commit-graph write --reachable &&
+
+ # Corrupt the repository by deleting the intermediate commit
+ # object. Commands should notice that this object is absent and
+ # thus that the repository is corrupt even if the commit graph
+ # exists.
+ oid=$(git rev-parse B) &&
+ rm .git/objects/"$(test_oid_to_path "$oid")" &&
+
+ # Again, we should be able to parse the commit when not
+ # being paranoid about commit graph staleness...
+ GIT_COMMIT_GRAPH_PARANOIA=false git rev-parse HEAD~2 &&
+ # ... but fail when we are paranoid.
+ test_must_fail git rev-parse HEAD~2 2>error &&
+ grep "error: commit $oid exists in commit-graph but not in the object database" error
+ )
+'
+
+test_done
diff --git a/t/t5319-multi-pack-index.sh b/t/t5319-multi-pack-index.sh
new file mode 100755
index 0000000..c4c6060
--- /dev/null
+++ b/t/t5319-multi-pack-index.sh
@@ -0,0 +1,1160 @@
+#!/bin/sh
+
+test_description='multi-pack-indexes'
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-chunk.sh
+
+GIT_TEST_MULTI_PACK_INDEX=0
+objdir=.git/objects
+
+HASH_LEN=$(test_oid rawsz)
+
+midx_read_expect () {
+ NUM_PACKS=$1
+ NUM_OBJECTS=$2
+ NUM_CHUNKS=$3
+ OBJECT_DIR=$4
+ EXTRA_CHUNKS="$5"
+ {
+ cat <<-EOF &&
+ header: 4d494458 1 $HASH_LEN $NUM_CHUNKS $NUM_PACKS
+ chunks: pack-names oid-fanout oid-lookup object-offsets$EXTRA_CHUNKS
+ num_objects: $NUM_OBJECTS
+ packs:
+ EOF
+ if test $NUM_PACKS -ge 1
+ then
+ ls $OBJECT_DIR/pack/ | grep idx | sort
+ fi &&
+ printf "object-dir: $OBJECT_DIR\n"
+ } >expect &&
+ test-tool read-midx $OBJECT_DIR >actual &&
+ test_cmp expect actual
+}
+
+test_expect_success 'setup' '
+ test_oid_cache <<-EOF
+ idxoff sha1:2999
+ idxoff sha256:3739
+
+ packnameoff sha1:652
+ packnameoff sha256:940
+
+ fanoutoff sha1:1
+ fanoutoff sha256:3
+ EOF
+'
+
+test_expect_success "don't write midx with no packs" '
+ test_must_fail git multi-pack-index --object-dir=. write &&
+ test_path_is_missing pack/multi-pack-index
+'
+
+test_expect_success SHA1 'warn if a midx contains no oid' '
+ cp "$TEST_DIRECTORY"/t5319/no-objects.midx $objdir/pack/multi-pack-index &&
+ test_must_fail git multi-pack-index verify &&
+ rm $objdir/pack/multi-pack-index
+'
+
+generate_objects () {
+ i=$1
+ iii=$(printf '%03i' $i)
+ {
+ test-tool genrandom "bar" 200 &&
+ test-tool genrandom "baz $iii" 50
+ } >wide_delta_$iii &&
+ {
+ test-tool genrandom "foo"$i 100 &&
+ test-tool genrandom "foo"$(( $i + 1 )) 100 &&
+ test-tool genrandom "foo"$(( $i + 2 )) 100
+ } >deep_delta_$iii &&
+ {
+ echo $iii &&
+ test-tool genrandom "$iii" 8192
+ } >file_$iii &&
+ git update-index --add file_$iii deep_delta_$iii wide_delta_$iii
+}
+
+commit_and_list_objects () {
+ {
+ echo 101 &&
+ test-tool genrandom 100 8192;
+ } >file_101 &&
+ git update-index --add file_101 &&
+ tree=$(git write-tree) &&
+ commit=$(git commit-tree $tree -p HEAD</dev/null) &&
+ {
+ echo $tree &&
+ git ls-tree $tree | sed -e "s/.* \\([0-9a-f]*\\) .*/\\1/"
+ } >obj-list &&
+ git reset --hard $commit
+}
+
+test_expect_success 'create objects' '
+ test_commit initial &&
+ for i in $(test_seq 1 5)
+ do
+ generate_objects $i || return 1
+ done &&
+ commit_and_list_objects
+'
+
+test_expect_success 'write midx with one v1 pack' '
+ pack=$(git pack-objects --index-version=1 $objdir/pack/test <obj-list) &&
+ test_when_finished rm $objdir/pack/test-$pack.pack \
+ $objdir/pack/test-$pack.idx $objdir/pack/multi-pack-index &&
+ git multi-pack-index --object-dir=$objdir write &&
+ midx_read_expect 1 18 4 $objdir
+'
+
+midx_git_two_modes () {
+ git -c core.multiPackIndex=false $1 >expect &&
+ git -c core.multiPackIndex=true $1 >actual &&
+ if [ "$2" = "sorted" ]
+ then
+ sort <expect >expect.sorted &&
+ mv expect.sorted expect &&
+ sort <actual >actual.sorted &&
+ mv actual.sorted actual
+ fi &&
+ test_cmp expect actual
+}
+
+compare_results_with_midx () {
+ MSG=$1
+ test_expect_success "check normal git operations: $MSG" '
+ midx_git_two_modes "rev-list --objects --all" &&
+ midx_git_two_modes "log --raw" &&
+ midx_git_two_modes "count-objects --verbose" &&
+ midx_git_two_modes "cat-file --batch-all-objects --batch-check" &&
+ midx_git_two_modes "cat-file --batch-all-objects --batch-check --unordered" sorted
+ '
+}
+
+test_expect_success 'write midx with one v2 pack' '
+ git pack-objects --index-version=2,0x40 $objdir/pack/test <obj-list &&
+ git multi-pack-index --object-dir=$objdir write &&
+ midx_read_expect 1 18 4 $objdir
+'
+
+compare_results_with_midx "one v2 pack"
+
+test_expect_success 'corrupt idx reports errors' '
+ idx=$(test-tool read-midx $objdir | grep "\.idx\$") &&
+ mv $objdir/pack/$idx backup-$idx &&
+ test_when_finished "mv backup-\$idx \$objdir/pack/\$idx" &&
+
+ # This is the minimum size for a sha-1 based .idx; this lets
+ # us pass perfunctory tests, but anything that actually opens and reads
+ # the idx file will complain.
+ test_copy_bytes 1064 <backup-$idx >$objdir/pack/$idx &&
+
+ git -c core.multiPackIndex=true rev-list --objects --all 2>err &&
+ grep "index unavailable" err
+'
+
+test_expect_success 'add more objects' '
+ for i in $(test_seq 6 10)
+ do
+ generate_objects $i || return 1
+ done &&
+ commit_and_list_objects
+'
+
+test_expect_success 'write midx with two packs' '
+ git pack-objects --index-version=1 $objdir/pack/test-2 <obj-list &&
+ git multi-pack-index --object-dir=$objdir write &&
+ midx_read_expect 2 34 4 $objdir
+'
+
+compare_results_with_midx "two packs"
+
+test_expect_success 'write midx with --stdin-packs' '
+ rm -fr $objdir/pack/multi-pack-index &&
+
+ idx="$(find $objdir/pack -name "test-2-*.idx")" &&
+ basename "$idx" >in &&
+
+ git multi-pack-index write --stdin-packs <in &&
+
+ test-tool read-midx $objdir | grep "\.idx$" >packs &&
+
+ test_cmp packs in
+'
+
+compare_results_with_midx "mixed mode (one pack + extra)"
+
+test_expect_success 'write with no objects and preferred pack' '
+ test_when_finished "rm -rf empty" &&
+ git init empty &&
+ test_must_fail git -C empty multi-pack-index write \
+ --stdin-packs --preferred-pack=does-not-exist </dev/null 2>err &&
+ cat >expect <<-EOF &&
+ warning: unknown preferred pack: ${SQ}does-not-exist${SQ}
+ error: no pack files to index.
+ EOF
+ test_cmp expect err
+'
+
+test_expect_success 'write progress off for redirected stderr' '
+ git multi-pack-index --object-dir=$objdir write 2>err &&
+ test_line_count = 0 err
+'
+
+test_expect_success 'write force progress on for stderr' '
+ GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir write --progress 2>err &&
+ test_file_not_empty err
+'
+
+test_expect_success 'write with the --no-progress option' '
+ GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir write --no-progress 2>err &&
+ test_line_count = 0 err
+'
+
+test_expect_success 'add more packs' '
+ for j in $(test_seq 11 20)
+ do
+ generate_objects $j &&
+ commit_and_list_objects &&
+ git pack-objects --index-version=2 $objdir/pack/test-pack <obj-list || return 1
+ done
+'
+
+compare_results_with_midx "mixed mode (two packs + extra)"
+
+test_expect_success 'write midx with twelve packs' '
+ git multi-pack-index --object-dir=$objdir write &&
+ midx_read_expect 12 74 4 $objdir
+'
+
+compare_results_with_midx "twelve packs"
+
+test_expect_success 'multi-pack-index *.rev cleanup with --object-dir' '
+ git init repo &&
+ git clone -s repo alternate &&
+
+ test_when_finished "rm -rf repo alternate" &&
+
+ (
+ cd repo &&
+ test_commit base &&
+ git repack -d
+ ) &&
+
+ ours="alternate/.git/objects/pack/multi-pack-index-123.rev" &&
+ theirs="repo/.git/objects/pack/multi-pack-index-abc.rev" &&
+ touch "$ours" "$theirs" &&
+
+ (
+ cd alternate &&
+ git multi-pack-index --object-dir ../repo/.git/objects write
+ ) &&
+
+ # writing a midx in "repo" should not remove the .rev file in the
+ # alternate
+ test_path_is_file repo/.git/objects/pack/multi-pack-index &&
+ test_path_is_file $ours &&
+ test_path_is_missing $theirs
+'
+
+test_expect_success 'warn on improper hash version' '
+ git init --object-format=sha1 sha1 &&
+ (
+ cd sha1 &&
+ git config core.multiPackIndex true &&
+ test_commit 1 &&
+ git repack -a &&
+ git multi-pack-index write &&
+ mv .git/objects/pack/multi-pack-index ../mpi-sha1
+ ) &&
+ git init --object-format=sha256 sha256 &&
+ (
+ cd sha256 &&
+ git config core.multiPackIndex true &&
+ test_commit 1 &&
+ git repack -a &&
+ git multi-pack-index write &&
+ mv .git/objects/pack/multi-pack-index ../mpi-sha256
+ ) &&
+ (
+ cd sha1 &&
+ mv ../mpi-sha256 .git/objects/pack/multi-pack-index &&
+ git log -1 2>err &&
+ test_grep "multi-pack-index hash version 2 does not match version 1" err
+ ) &&
+ (
+ cd sha256 &&
+ mv ../mpi-sha1 .git/objects/pack/multi-pack-index &&
+ git log -1 2>err &&
+ test_grep "multi-pack-index hash version 1 does not match version 2" err
+ )
+'
+
+test_expect_success 'midx picks objects from preferred pack' '
+ test_when_finished rm -rf preferred.git &&
+ git init --bare preferred.git &&
+ (
+ cd preferred.git &&
+
+ a=$(echo "a" | git hash-object -w --stdin) &&
+ b=$(echo "b" | git hash-object -w --stdin) &&
+ c=$(echo "c" | git hash-object -w --stdin) &&
+
+ # Set up two packs, duplicating the object "B" at different
+ # offsets.
+ #
+ # Note that the "BC" pack (the one we choose as preferred) sorts
+ # lexically after the "AB" pack, meaning that omitting the
+ # --preferred-pack argument would cause this test to fail (since
+ # the MIDX code would select the copy of "b" in the "AB" pack).
+ git pack-objects objects/pack/test-AB <<-EOF &&
+ $a
+ $b
+ EOF
+ bc=$(git pack-objects objects/pack/test-BC <<-EOF
+ $b
+ $c
+ EOF
+ ) &&
+
+ git multi-pack-index --object-dir=objects \
+ write --preferred-pack=test-BC-$bc.idx 2>err &&
+ test_must_be_empty err &&
+
+ test-tool read-midx --show-objects objects >out &&
+
+ ofs=$(git show-index <objects/pack/test-BC-$bc.idx | grep $b |
+ cut -d" " -f1) &&
+ printf "%s %s\tobjects/pack/test-BC-%s.pack\n" \
+ "$b" "$ofs" "$bc" >expect &&
+ grep ^$b out >actual &&
+
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'preferred packs must be non-empty' '
+ test_when_finished rm -rf preferred.git &&
+ git init preferred.git &&
+ (
+ cd preferred.git &&
+
+ test_commit base &&
+ git repack -ad &&
+
+ empty="$(git pack-objects $objdir/pack/pack </dev/null)" &&
+
+ test_must_fail git multi-pack-index write \
+ --preferred-pack=pack-$empty.pack 2>err &&
+ grep "with no objects" err
+ )
+'
+
+test_expect_success 'verify multi-pack-index success' '
+ git multi-pack-index verify --object-dir=$objdir
+'
+
+test_expect_success 'verify progress off for redirected stderr' '
+ git multi-pack-index verify --object-dir=$objdir 2>err &&
+ test_line_count = 0 err
+'
+
+test_expect_success 'verify force progress on for stderr' '
+ git multi-pack-index verify --object-dir=$objdir --progress 2>err &&
+ test_file_not_empty err
+'
+
+test_expect_success 'verify with the --no-progress option' '
+ git multi-pack-index verify --object-dir=$objdir --no-progress 2>err &&
+ test_line_count = 0 err
+'
+
+# usage: corrupt_midx_and_verify <pos> <data> <objdir> <string>
+corrupt_midx_and_verify() {
+ POS=$1 &&
+ DATA="${2:-\0}" &&
+ OBJDIR=$3 &&
+ GREPSTR="$4" &&
+ COMMAND="$5" &&
+ if test -z "$COMMAND"
+ then
+ COMMAND="git multi-pack-index verify --object-dir=$OBJDIR"
+ fi &&
+ FILE=$OBJDIR/pack/multi-pack-index &&
+ chmod a+w $FILE &&
+ test_when_finished mv midx-backup $FILE &&
+ cp $FILE midx-backup &&
+ printf "$DATA" | dd of="$FILE" bs=1 seek="$POS" conv=notrunc &&
+ test_must_fail $COMMAND 2>test_err &&
+ grep -v "^+" test_err >err &&
+ test_grep "$GREPSTR" err
+}
+
+test_expect_success 'verify bad signature' '
+ corrupt_midx_and_verify 0 "\00" $objdir \
+ "multi-pack-index signature"
+'
+
+NUM_OBJECTS=74
+MIDX_BYTE_VERSION=4
+MIDX_BYTE_OID_VERSION=5
+MIDX_BYTE_CHUNK_COUNT=6
+MIDX_HEADER_SIZE=12
+MIDX_BYTE_CHUNK_ID=$MIDX_HEADER_SIZE
+MIDX_BYTE_CHUNK_OFFSET=$(($MIDX_HEADER_SIZE + 4))
+MIDX_NUM_CHUNKS=5
+MIDX_CHUNK_LOOKUP_WIDTH=12
+MIDX_OFFSET_PACKNAMES=$(($MIDX_HEADER_SIZE + \
+ $MIDX_NUM_CHUNKS * $MIDX_CHUNK_LOOKUP_WIDTH))
+MIDX_BYTE_PACKNAME_ORDER=$(($MIDX_OFFSET_PACKNAMES + 2))
+MIDX_OFFSET_OID_FANOUT=$(($MIDX_OFFSET_PACKNAMES + $(test_oid packnameoff)))
+MIDX_OID_FANOUT_WIDTH=4
+MIDX_BYTE_OID_FANOUT_ORDER=$((MIDX_OFFSET_OID_FANOUT + 250 * $MIDX_OID_FANOUT_WIDTH + $(test_oid fanoutoff)))
+MIDX_OFFSET_OID_LOOKUP=$(($MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
+MIDX_BYTE_OID_LOOKUP=$(($MIDX_OFFSET_OID_LOOKUP + 16 * $HASH_LEN))
+MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
+MIDX_OFFSET_WIDTH=8
+MIDX_BYTE_PACK_INT_ID=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 2))
+MIDX_BYTE_OFFSET=$(($MIDX_OFFSET_OBJECT_OFFSETS + 16 * $MIDX_OFFSET_WIDTH + 6))
+
+test_expect_success 'verify bad version' '
+ corrupt_midx_and_verify $MIDX_BYTE_VERSION "\00" $objdir \
+ "multi-pack-index version"
+'
+
+test_expect_success 'verify bad OID version' '
+ corrupt_midx_and_verify $MIDX_BYTE_OID_VERSION "\03" $objdir \
+ "hash version"
+'
+
+test_expect_success 'verify truncated chunk count' '
+ corrupt_midx_and_verify $MIDX_BYTE_CHUNK_COUNT "\01" $objdir \
+ "final chunk has non-zero id"
+'
+
+test_expect_success 'verify extended chunk count' '
+ corrupt_midx_and_verify $MIDX_BYTE_CHUNK_COUNT "\07" $objdir \
+ "terminating chunk id appears earlier than expected"
+'
+
+test_expect_success 'verify missing required chunk' '
+ corrupt_midx_and_verify $MIDX_BYTE_CHUNK_ID "\01" $objdir \
+ "required pack-name chunk missing"
+'
+
+test_expect_success 'verify invalid chunk offset' '
+ corrupt_midx_and_verify $MIDX_BYTE_CHUNK_OFFSET "\01" $objdir \
+ "improper chunk offset(s)"
+'
+
+test_expect_success 'verify packnames out of order' '
+ corrupt_midx_and_verify $MIDX_BYTE_PACKNAME_ORDER "z" $objdir \
+ "pack names out of order"
+'
+
+test_expect_success 'verify packnames out of order' '
+ corrupt_midx_and_verify $MIDX_BYTE_PACKNAME_ORDER "a" $objdir \
+ "failed to load pack"
+'
+
+test_expect_success 'verify oid fanout out of order' '
+ corrupt_midx_and_verify $MIDX_BYTE_OID_FANOUT_ORDER "\01" $objdir \
+ "oid fanout out of order"
+'
+
+test_expect_success 'verify oid lookup out of order' '
+ corrupt_midx_and_verify $MIDX_BYTE_OID_LOOKUP "\00" $objdir \
+ "oid lookup out of order"
+'
+
+test_expect_success 'verify incorrect pack-int-id' '
+ corrupt_midx_and_verify $MIDX_BYTE_PACK_INT_ID "\07" $objdir \
+ "bad pack-int-id"
+'
+
+test_expect_success 'verify incorrect offset' '
+ corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
+ "incorrect object offset"
+'
+
+test_expect_success 'git-fsck incorrect offset' '
+ corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
+ "incorrect object offset" \
+ "git -c core.multiPackIndex=true fsck" &&
+ test_unconfig core.multiPackIndex &&
+ test_must_fail git fsck &&
+ git -c core.multiPackIndex=false fsck
+'
+
+test_expect_success 'git fsck shows MIDX output with --progress' '
+ git fsck --progress 2>err &&
+ grep "Verifying OID order in multi-pack-index" err &&
+ grep "Verifying object offsets" err
+'
+
+test_expect_success 'git fsck suppresses MIDX output with --no-progress' '
+ git fsck --no-progress 2>err &&
+ ! grep "Verifying OID order in multi-pack-index" err &&
+ ! grep "Verifying object offsets" err
+'
+
+test_expect_success 'corrupt MIDX is not reused' '
+ corrupt_midx_and_verify $MIDX_BYTE_OFFSET "\377" $objdir \
+ "incorrect object offset" &&
+ git multi-pack-index write 2>err &&
+ test_grep checksum.mismatch err &&
+ git multi-pack-index verify
+'
+
+test_expect_success 'verify incorrect checksum' '
+ pos=$(($(wc -c <$objdir/pack/multi-pack-index) - 10)) &&
+ corrupt_midx_and_verify $pos \
+ "\377\377\377\377\377\377\377\377\377\377" \
+ $objdir "incorrect checksum"
+'
+
+test_expect_success 'repack progress off for redirected stderr' '
+ GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir repack 2>err &&
+ test_line_count = 0 err
+'
+
+test_expect_success 'repack force progress on for stderr' '
+ GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir repack --progress 2>err &&
+ test_file_not_empty err
+'
+
+test_expect_success 'repack with the --no-progress option' '
+ GIT_PROGRESS_DELAY=0 git multi-pack-index --object-dir=$objdir repack --no-progress 2>err &&
+ test_line_count = 0 err
+'
+
+test_expect_success 'repack removes multi-pack-index when deleting packs' '
+ test_path_is_file $objdir/pack/multi-pack-index &&
+ # Set GIT_TEST_MULTI_PACK_INDEX to 0 to avoid writing a new
+ # multi-pack-index after repacking, but set "core.multiPackIndex" to
+ # true so that "git repack" can read the existing MIDX.
+ GIT_TEST_MULTI_PACK_INDEX=0 git -c core.multiPackIndex repack -adf &&
+ test_path_is_missing $objdir/pack/multi-pack-index
+'
+
+test_expect_success 'repack preserves multi-pack-index when creating packs' '
+ git init preserve &&
+ test_when_finished "rm -fr preserve" &&
+ (
+ cd preserve &&
+ packdir=.git/objects/pack &&
+ midx=$packdir/multi-pack-index &&
+
+ test_commit 1 &&
+ pack1=$(git pack-objects --all $packdir/pack) &&
+ touch $packdir/pack-$pack1.keep &&
+ test_commit 2 &&
+ pack2=$(git pack-objects --revs $packdir/pack) &&
+ touch $packdir/pack-$pack2.keep &&
+
+ git multi-pack-index write &&
+ cp $midx $midx.bak &&
+
+ cat >pack-input <<-EOF &&
+ HEAD
+ ^HEAD~1
+ EOF
+ test_commit 3 &&
+ pack3=$(git pack-objects --revs $packdir/pack <pack-input) &&
+ test_commit 4 &&
+ pack4=$(git pack-objects --revs $packdir/pack <pack-input) &&
+
+ GIT_TEST_MULTI_PACK_INDEX=0 git -c core.multiPackIndex repack -ad &&
+ ls -la $packdir &&
+ test_path_is_file $packdir/pack-$pack1.pack &&
+ test_path_is_file $packdir/pack-$pack2.pack &&
+ test_path_is_missing $packdir/pack-$pack3.pack &&
+ test_path_is_missing $packdir/pack-$pack4.pack &&
+ test_cmp_bin $midx.bak $midx
+ )
+'
+
+compare_results_with_midx "after repack"
+
+test_expect_success 'multi-pack-index and pack-bitmap' '
+ GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
+ git -c repack.writeBitmaps=true repack -ad &&
+ git multi-pack-index write &&
+ git rev-list --test-bitmap HEAD
+'
+
+test_expect_success 'multi-pack-index and alternates' '
+ git init --bare alt.git &&
+ echo $(pwd)/alt.git/objects >.git/objects/info/alternates &&
+ echo content1 >file1 &&
+ altblob=$(GIT_DIR=alt.git git hash-object -w file1) &&
+ git cat-file blob $altblob &&
+ git rev-list --all
+'
+
+compare_results_with_midx "with alternate (local midx)"
+
+test_expect_success 'multi-pack-index in an alternate' '
+ mv .git/objects/pack/* alt.git/objects/pack &&
+ test_commit add_local_objects &&
+ git repack --local &&
+ git multi-pack-index write &&
+ midx_read_expect 1 3 4 $objdir &&
+ git reset --hard HEAD~1 &&
+ rm -f .git/objects/pack/*
+'
+
+compare_results_with_midx "with alternate (remote midx)"
+
+# usage: corrupt_data <file> <pos> [<data>]
+corrupt_data () {
+ file=$1
+ pos=$2
+ data="${3:-\0}"
+ printf "$data" | dd of="$file" bs=1 seek="$pos" conv=notrunc
+}
+
+# Force 64-bit offsets by manipulating the idx file.
+# This makes the IDX file _incorrect_ so be careful to clean up after!
+test_expect_success 'force some 64-bit offsets with pack-objects' '
+ mkdir objects64 &&
+ mkdir objects64/pack &&
+ for i in $(test_seq 1 11)
+ do
+ generate_objects 11 || return 1
+ done &&
+ commit_and_list_objects &&
+ pack64=$(git pack-objects --index-version=2,0x40 objects64/pack/test-64 <obj-list) &&
+ idx64=objects64/pack/test-64-$pack64.idx &&
+ chmod u+w $idx64 &&
+ corrupt_data $idx64 $(test_oid idxoff) "\02" &&
+ # objects64 is not a real repository, but can serve as an alternate
+ # anyway so we can write a MIDX into it
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+ ( cd ../objects64 && pwd ) >.git/objects/info/alternates &&
+ midx64=$(git multi-pack-index --object-dir=../objects64 write)
+ ) &&
+ midx_read_expect 1 63 5 objects64 " large-offsets"
+'
+
+test_expect_success 'verify multi-pack-index with 64-bit offsets' '
+ git multi-pack-index verify --object-dir=objects64
+'
+
+NUM_OBJECTS=63
+MIDX_OFFSET_OID_FANOUT=$((MIDX_OFFSET_PACKNAMES + 54))
+MIDX_OFFSET_OID_LOOKUP=$((MIDX_OFFSET_OID_FANOUT + 256 * $MIDX_OID_FANOUT_WIDTH))
+MIDX_OFFSET_OBJECT_OFFSETS=$(($MIDX_OFFSET_OID_LOOKUP + $NUM_OBJECTS * $HASH_LEN))
+MIDX_OFFSET_LARGE_OFFSETS=$(($MIDX_OFFSET_OBJECT_OFFSETS + $NUM_OBJECTS * $MIDX_OFFSET_WIDTH))
+MIDX_BYTE_LARGE_OFFSET=$(($MIDX_OFFSET_LARGE_OFFSETS + 3))
+
+test_expect_success 'verify incorrect 64-bit offset' '
+ corrupt_midx_and_verify $MIDX_BYTE_LARGE_OFFSET "\07" objects64 \
+ "incorrect object offset"
+'
+
+test_expect_success 'setup expire tests' '
+ mkdir dup &&
+ (
+ cd dup &&
+ git init &&
+ test-tool genrandom "data" 4096 >large_file.txt &&
+ git update-index --add large_file.txt &&
+ for i in $(test_seq 1 20)
+ do
+ test_commit $i || exit 1
+ done &&
+ git branch A HEAD &&
+ git branch B HEAD~8 &&
+ git branch C HEAD~13 &&
+ git branch D HEAD~16 &&
+ git branch E HEAD~18 &&
+ git pack-objects --revs .git/objects/pack/pack-A <<-EOF &&
+ refs/heads/A
+ ^refs/heads/B
+ EOF
+ git pack-objects --revs .git/objects/pack/pack-B <<-EOF &&
+ refs/heads/B
+ ^refs/heads/C
+ EOF
+ git pack-objects --revs .git/objects/pack/pack-C <<-EOF &&
+ refs/heads/C
+ ^refs/heads/D
+ EOF
+ git pack-objects --revs .git/objects/pack/pack-D <<-EOF &&
+ refs/heads/D
+ ^refs/heads/E
+ EOF
+ git pack-objects --revs .git/objects/pack/pack-E <<-EOF &&
+ refs/heads/E
+ EOF
+ git multi-pack-index write &&
+ cp -r .git/objects/pack .git/objects/pack-backup
+ )
+'
+
+test_expect_success 'expire does not remove any packs' '
+ (
+ cd dup &&
+ ls .git/objects/pack >expect &&
+ git multi-pack-index expire &&
+ ls .git/objects/pack >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'expire progress off for redirected stderr' '
+ (
+ cd dup &&
+ git multi-pack-index expire 2>err &&
+ test_line_count = 0 err
+ )
+'
+
+test_expect_success 'expire force progress on for stderr' '
+ (
+ cd dup &&
+ GIT_PROGRESS_DELAY=0 git multi-pack-index expire --progress 2>err &&
+ test_file_not_empty err
+ )
+'
+
+test_expect_success 'expire with the --no-progress option' '
+ (
+ cd dup &&
+ GIT_PROGRESS_DELAY=0 git multi-pack-index expire --no-progress 2>err &&
+ test_line_count = 0 err
+ )
+'
+
+test_expect_success 'expire removes unreferenced packs' '
+ (
+ cd dup &&
+ git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
+ refs/heads/A
+ ^refs/heads/C
+ EOF
+ git multi-pack-index write &&
+ ls .git/objects/pack | grep -v -e pack-[AB] >expect &&
+ git multi-pack-index expire &&
+ ls .git/objects/pack >actual &&
+ test_cmp expect actual &&
+ ls .git/objects/pack/ | grep idx >expect-idx &&
+ test-tool read-midx .git/objects | grep idx >actual-midx &&
+ test_cmp expect-idx actual-midx &&
+ git multi-pack-index verify &&
+ git fsck
+ )
+'
+
+test_expect_success 'repack with minimum size does not alter existing packs' '
+ (
+ cd dup &&
+ rm -rf .git/objects/pack &&
+ mv .git/objects/pack-backup .git/objects/pack &&
+ test-tool chmtime =-5 .git/objects/pack/pack-D* &&
+ test-tool chmtime =-4 .git/objects/pack/pack-C* &&
+ test-tool chmtime =-3 .git/objects/pack/pack-B* &&
+ test-tool chmtime =-2 .git/objects/pack/pack-A* &&
+ ls .git/objects/pack >expect &&
+ MINSIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 1) &&
+ git multi-pack-index repack --batch-size=$MINSIZE &&
+ ls .git/objects/pack >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'repack respects repack.packKeptObjects=false' '
+ test_when_finished rm -f dup/.git/objects/pack/*keep &&
+ (
+ cd dup &&
+ ls .git/objects/pack/*idx >idx-list &&
+ test_line_count = 5 idx-list &&
+ ls .git/objects/pack/*.pack | sed "s/\.pack/.keep/" >keep-list &&
+ test_line_count = 5 keep-list &&
+ for keep in $(cat keep-list)
+ do
+ touch $keep || return 1
+ done &&
+ git multi-pack-index repack --batch-size=0 &&
+ ls .git/objects/pack/*idx >idx-list &&
+ test_line_count = 5 idx-list &&
+ test-tool read-midx .git/objects | grep idx >midx-list &&
+ test_line_count = 5 midx-list &&
+ THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | sed -n 3p) &&
+ BATCH_SIZE=$((THIRD_SMALLEST_SIZE + 1)) &&
+ git multi-pack-index repack --batch-size=$BATCH_SIZE &&
+ ls .git/objects/pack/*idx >idx-list &&
+ test_line_count = 5 idx-list &&
+ test-tool read-midx .git/objects | grep idx >midx-list &&
+ test_line_count = 5 midx-list
+ )
+'
+
+test_expect_success 'repack creates a new pack' '
+ (
+ cd dup &&
+ ls .git/objects/pack/*idx >idx-list &&
+ test_line_count = 5 idx-list &&
+ THIRD_SMALLEST_SIZE=$(test-tool path-utils file-size .git/objects/pack/*pack | sort -n | head -n 3 | tail -n 1) &&
+ BATCH_SIZE=$(($THIRD_SMALLEST_SIZE + 1)) &&
+ git multi-pack-index repack --batch-size=$BATCH_SIZE &&
+ ls .git/objects/pack/*idx >idx-list &&
+ test_line_count = 6 idx-list &&
+ test-tool read-midx .git/objects | grep idx >midx-list &&
+ test_line_count = 6 midx-list
+ )
+'
+
+test_expect_success 'repack (all) ignores cruft pack' '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+
+ test_commit base &&
+ test_commit --no-tag unreachable &&
+
+ git reset --hard base &&
+ git reflog expire --all --expire=all &&
+ git repack --cruft -d &&
+
+ git multi-pack-index write &&
+
+ find $objdir/pack | sort >before &&
+ git multi-pack-index repack --batch-size=0 &&
+ find $objdir/pack | sort >after &&
+
+ test_cmp before after
+ )
+'
+
+test_expect_success 'repack (--batch-size) ignores cruft pack' '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+
+ test_commit_bulk 5 &&
+ test_commit --no-tag unreachable &&
+
+ git reset --hard HEAD^ &&
+ git reflog expire --all --expire=all &&
+ git repack --cruft -d &&
+
+ test_commit four &&
+
+ find $objdir/pack -type f -name "*.pack" | sort >before &&
+ git repack -d &&
+ find $objdir/pack -type f -name "*.pack" | sort >after &&
+
+ pack="$(comm -13 before after)" &&
+ test_file_size "$pack" >sz &&
+ # Set --batch-size to twice the size of the pack created
+ # in the previous step, since this is enough to
+ # accommodate it and the cruft pack.
+ #
+ # This means that the MIDX machinery *could* combine the
+ # new and cruft packs together.
+ #
+ # We ensure that it does not below.
+ batch="$((($(cat sz) * 2)))" &&
+
+ git multi-pack-index write &&
+
+ find $objdir/pack | sort >before &&
+ git multi-pack-index repack --batch-size=$batch &&
+ find $objdir/pack | sort >after &&
+
+ test_cmp before after
+ )
+'
+
+test_expect_success 'expire removes repacked packs' '
+ (
+ cd dup &&
+ ls -al .git/objects/pack/*pack &&
+ ls -S .git/objects/pack/*pack | head -n 4 >expect &&
+ git multi-pack-index expire &&
+ ls -S .git/objects/pack/*pack >actual &&
+ test_cmp expect actual &&
+ test-tool read-midx .git/objects | grep idx >midx-list &&
+ test_line_count = 4 midx-list
+ )
+'
+
+test_expect_success 'expire works when adding new packs' '
+ (
+ cd dup &&
+ git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
+ refs/heads/A
+ ^refs/heads/B
+ EOF
+ git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
+ refs/heads/B
+ ^refs/heads/C
+ EOF
+ git pack-objects --revs .git/objects/pack/pack-combined <<-EOF &&
+ refs/heads/C
+ ^refs/heads/D
+ EOF
+ git multi-pack-index write &&
+ git pack-objects --revs .git/objects/pack/a-pack <<-EOF &&
+ refs/heads/D
+ ^refs/heads/E
+ EOF
+ git multi-pack-index write &&
+ git pack-objects --revs .git/objects/pack/z-pack <<-EOF &&
+ refs/heads/E
+ EOF
+ git multi-pack-index expire &&
+ ls .git/objects/pack/ | grep idx >expect &&
+ test-tool read-midx .git/objects | grep idx >actual &&
+ test_cmp expect actual &&
+ git multi-pack-index verify
+ )
+'
+
+test_expect_success 'expire respects .keep files' '
+ (
+ cd dup &&
+ git pack-objects --revs .git/objects/pack/pack-all <<-EOF &&
+ refs/heads/A
+ EOF
+ git multi-pack-index write &&
+ PACKA=$(ls .git/objects/pack/a-pack*\.pack | sed s/\.pack\$//) &&
+ touch $PACKA.keep &&
+ git multi-pack-index expire &&
+ test_path_is_file $PACKA.idx &&
+ test_path_is_file $PACKA.keep &&
+ test_path_is_file $PACKA.pack &&
+ test-tool read-midx .git/objects | grep idx >midx-list &&
+ test_line_count = 2 midx-list
+ )
+'
+
+test_expect_success 'expiring unreferenced cruft pack retains pack' '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+
+ test_commit base &&
+ test_commit --no-tag unreachable &&
+ unreachable=$(git rev-parse HEAD) &&
+
+ git reset --hard base &&
+ git reflog expire --all --expire=all &&
+ git repack --cruft -d &&
+ mtimes="$(ls $objdir/pack/pack-*.mtimes)" &&
+
+ echo "base..$unreachable" >in &&
+ pack="$(git pack-objects --revs --delta-base-offset \
+ $objdir/pack/pack <in)" &&
+
+ # Preferring the contents of "$pack" will leave the
+ # cruft pack unreferenced (ie., none of the objects
+ # contained in the cruft pack will have their MIDX copy
+ # selected from the cruft pack).
+ git multi-pack-index write --preferred-pack="pack-$pack.pack" &&
+ git multi-pack-index expire &&
+
+ test_path_is_file "$mtimes"
+ )
+'
+
+test_expect_success 'repack --batch-size=0 repacks everything' '
+ cp -r dup dup2 &&
+ (
+ cd dup &&
+ rm .git/objects/pack/*.keep &&
+ ls .git/objects/pack/*idx >idx-list &&
+ test_line_count = 2 idx-list &&
+ git multi-pack-index repack --batch-size=0 &&
+ ls .git/objects/pack/*idx >idx-list &&
+ test_line_count = 3 idx-list &&
+ test-tool read-midx .git/objects | grep idx >midx-list &&
+ test_line_count = 3 midx-list &&
+ git multi-pack-index expire &&
+ ls -al .git/objects/pack/*idx >idx-list &&
+ test_line_count = 1 idx-list &&
+ git multi-pack-index repack --batch-size=0 &&
+ ls -al .git/objects/pack/*idx >new-idx-list &&
+ test_cmp idx-list new-idx-list
+ )
+'
+
+test_expect_success 'repack --batch-size=<large> repacks everything' '
+ (
+ cd dup2 &&
+ rm .git/objects/pack/*.keep &&
+ ls .git/objects/pack/*idx >idx-list &&
+ test_line_count = 2 idx-list &&
+ git multi-pack-index repack --batch-size=2000000 &&
+ ls .git/objects/pack/*idx >idx-list &&
+ test_line_count = 3 idx-list &&
+ test-tool read-midx .git/objects | grep idx >midx-list &&
+ test_line_count = 3 midx-list &&
+ git multi-pack-index expire &&
+ ls -al .git/objects/pack/*idx >idx-list &&
+ test_line_count = 1 idx-list
+ )
+'
+
+test_expect_success 'load reverse index when missing .idx, .pack' '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+
+ git config core.multiPackIndex true &&
+
+ test_commit base &&
+ git repack -ad &&
+ git multi-pack-index write &&
+
+ git rev-parse HEAD >tip &&
+ pack=$(ls .git/objects/pack/pack-*.pack) &&
+ idx=$(ls .git/objects/pack/pack-*.idx) &&
+
+ mv $idx $idx.bak &&
+ git cat-file --batch-check="%(objectsize:disk)" <tip &&
+
+ mv $idx.bak $idx &&
+
+ mv $pack $pack.bak &&
+ git cat-file --batch-check="%(objectsize:disk)" <tip
+ )
+'
+
+test_expect_success 'usage shown without sub-command' '
+ test_expect_code 129 git multi-pack-index 2>err &&
+ ! test_grep "unrecognized subcommand" err
+'
+
+test_expect_success 'complains when run outside of a repository' '
+ nongit test_must_fail git multi-pack-index write 2>err &&
+ grep "not a git repository" err
+'
+
+test_expect_success 'repack with delta islands' '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+
+ test_commit first &&
+ git repack &&
+ test_commit second &&
+ git repack &&
+
+ git multi-pack-index write &&
+ git -c repack.useDeltaIslands=true multi-pack-index repack
+ )
+'
+
+corrupt_chunk () {
+ midx=.git/objects/pack/multi-pack-index &&
+ test_when_finished "rm -rf $midx" &&
+ git repack -ad --write-midx &&
+ corrupt_chunk_file $midx "$@"
+}
+
+test_expect_success 'reader notices too-small oid fanout chunk' '
+ corrupt_chunk OIDF clear 00000000 &&
+ test_must_fail git log 2>err &&
+ cat >expect <<-\EOF &&
+ error: multi-pack-index OID fanout is of the wrong size
+ fatal: multi-pack-index required OID fanout chunk missing or corrupted
+ EOF
+ test_cmp expect err
+'
+
+test_expect_success 'reader notices too-small oid lookup chunk' '
+ corrupt_chunk OIDL clear 00000000 &&
+ test_must_fail git log 2>err &&
+ cat >expect <<-\EOF &&
+ error: multi-pack-index OID lookup chunk is the wrong size
+ fatal: multi-pack-index required OID lookup chunk missing or corrupted
+ EOF
+ test_cmp expect err
+'
+
+test_expect_success 'reader notices too-small pack names chunk' '
+ # There is no NUL to terminate the name here, so the
+ # chunk is too short.
+ corrupt_chunk PNAM clear 70656666 &&
+ test_must_fail git log 2>err &&
+ cat >expect <<-\EOF &&
+ fatal: multi-pack-index pack-name chunk is too short
+ EOF
+ test_cmp expect err
+'
+
+test_expect_success 'reader handles unaligned chunks' '
+ # A 9-byte PNAM means all of the subsequent chunks
+ # will no longer be 4-byte aligned, but it is still
+ # a valid one-pack chunk on its own (it is "foo.pack\0").
+ corrupt_chunk PNAM clear 666f6f2e7061636b00 &&
+ git -c core.multipackindex=false log >expect.out &&
+ git -c core.multipackindex=true log >out 2>err &&
+ test_cmp expect.out out &&
+ cat >expect.err <<-\EOF &&
+ error: chunk id 4f494446 not 4-byte aligned
+ EOF
+ test_cmp expect.err err
+'
+
+test_expect_success 'reader notices too-small object offset chunk' '
+ corrupt_chunk OOFF clear 00000000 &&
+ test_must_fail git log 2>err &&
+ cat >expect <<-\EOF &&
+ error: multi-pack-index object offset chunk is the wrong size
+ fatal: multi-pack-index required object offsets chunk missing or corrupted
+ EOF
+ test_cmp expect err
+'
+
+test_expect_success 'reader bounds-checks large offset table' '
+ # re-use the objects64 dir here to cheaply get access to a midx
+ # with large offsets.
+ git init repo &&
+ test_when_finished "rm -rf repo" &&
+ (
+ cd repo &&
+ (cd ../objects64 && pwd) >.git/objects/info/alternates &&
+ git multi-pack-index --object-dir=../objects64 write &&
+ midx=../objects64/pack/multi-pack-index &&
+ corrupt_chunk_file $midx LOFF clear &&
+ # using only %(objectsize) is important here; see the commit
+ # message for more details
+ test_must_fail git cat-file --batch-all-objects \
+ --batch-check="%(objectsize)" 2>err &&
+ cat >expect <<-\EOF &&
+ fatal: multi-pack-index large offset out of bounds
+ EOF
+ test_cmp expect err
+ )
+'
+
+test_expect_success 'reader notices too-small revindex chunk' '
+ # We only get a revindex with bitmaps (and likewise only
+ # load it when they are asked for).
+ test_config repack.writeBitmaps true &&
+ corrupt_chunk RIDX clear 00000000 &&
+ git -c core.multipackIndex=false rev-list \
+ --all --use-bitmap-index >expect.out &&
+ git -c core.multipackIndex=true rev-list \
+ --all --use-bitmap-index >out 2>err &&
+ test_cmp expect.out out &&
+ cat >expect.err <<-\EOF &&
+ error: multi-pack-index reverse-index chunk is the wrong size
+ warning: multi-pack bitmap is missing required reverse index
+ EOF
+ test_cmp expect.err err
+'
+
+test_done
diff --git a/t/t5319/no-objects.midx b/t/t5319/no-objects.midx
new file mode 100644
index 0000000..e466b8e
--- /dev/null
+++ b/t/t5319/no-objects.midx
Binary files differ
diff --git a/t/t5320-delta-islands.sh b/t/t5320-delta-islands.sh
new file mode 100755
index 0000000..4063633
--- /dev/null
+++ b/t/t5320-delta-islands.sh
@@ -0,0 +1,145 @@
+#!/bin/sh
+
+test_description='exercise delta islands'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# returns true iff $1 is a delta based on $2
+is_delta_base () {
+ delta_base=$(echo "$1" | git cat-file --batch-check='%(deltabase)') &&
+ echo >&2 "$1 has base $delta_base" &&
+ test "$delta_base" = "$2"
+}
+
+# generate a commit on branch $1 with a single file, "file", whose
+# content is mostly based on the seed $2, but with a unique bit
+# of content $3 appended. This should allow us to see whether
+# blobs of different refs delta against each other.
+commit() {
+ blob=$({ test-tool genrandom "$2" 10240 && echo "$3"; } |
+ git hash-object -w --stdin) &&
+ tree=$(printf '100644 blob %s\tfile\n' "$blob" | git mktree) &&
+ commit=$(echo "$2-$3" | git commit-tree "$tree" ${4:+-p "$4"}) &&
+ git update-ref "refs/heads/$1" "$commit" &&
+ eval "$1"'=$(git rev-parse $1:file)' &&
+ eval "echo >&2 $1=\$$1"
+}
+
+test_expect_success 'setup commits' '
+ commit one seed 1 &&
+ commit two seed 12
+'
+
+# Note: This is heavily dependent on the "prefer larger objects as base"
+# heuristic.
+test_expect_success 'vanilla repack deltas one against two' '
+ git repack -adf &&
+ is_delta_base $one $two
+'
+
+test_expect_success 'island repack with no island definition is vanilla' '
+ git repack -adfi &&
+ is_delta_base $one $two
+'
+
+test_expect_success 'island repack with no matches is vanilla' '
+ git -c "pack.island=refs/foo" repack -adfi &&
+ is_delta_base $one $two
+'
+
+test_expect_success 'separate islands disallows delta' '
+ git -c "pack.island=refs/heads/(.*)" repack -adfi &&
+ ! is_delta_base $one $two &&
+ ! is_delta_base $two $one
+'
+
+test_expect_success 'same island allows delta' '
+ git -c "pack.island=refs/heads" repack -adfi &&
+ is_delta_base $one $two
+'
+
+test_expect_success 'coalesce same-named islands' '
+ git \
+ -c "pack.island=refs/(.*)/one" \
+ -c "pack.island=refs/(.*)/two" \
+ repack -adfi &&
+ is_delta_base $one $two
+'
+
+test_expect_success 'island restrictions drop reused deltas' '
+ git repack -adfi &&
+ is_delta_base $one $two &&
+ git -c "pack.island=refs/heads/(.*)" repack -adi &&
+ ! is_delta_base $one $two &&
+ ! is_delta_base $two $one
+'
+
+test_expect_success 'island regexes are left-anchored' '
+ git -c "pack.island=heads/(.*)" repack -adfi &&
+ is_delta_base $one $two
+'
+
+test_expect_success 'island regexes follow last-one-wins scheme' '
+ git \
+ -c "pack.island=refs/heads/(.*)" \
+ -c "pack.island=refs/heads/" \
+ repack -adfi &&
+ is_delta_base $one $two
+'
+
+test_expect_success 'setup shared history' '
+ commit root shared root &&
+ commit one shared 1 root &&
+ commit two shared 12-long root
+'
+
+# We know that $two will be preferred as a base from $one,
+# because we can transform it with a pure deletion.
+#
+# We also expect $root as a delta against $two by the "longest is base" rule.
+test_expect_success 'vanilla delta goes between branches' '
+ git repack -adf &&
+ is_delta_base $one $two &&
+ is_delta_base $root $two
+'
+
+# Here we should allow $one to base itself on $root; even though
+# they are in different islands, the objects in $root are in a superset
+# of islands compared to those in $one.
+#
+# Similarly, $two can delta against $root by our rules. And unlike $one,
+# in which we are just allowing it, the island rules actually put $root
+# as a possible base for $two, which it would not otherwise be (due to the size
+# sorting).
+test_expect_success 'deltas allowed against superset islands' '
+ git -c "pack.island=refs/heads/(.*)" repack -adfi &&
+ is_delta_base $one $root &&
+ is_delta_base $two $root
+'
+
+# We are going to test the packfile order here, so we again have to make some
+# assumptions. We assume that "$root", as part of our core "one", must come
+# before "$two". This should be guaranteed by the island code. However, for
+# this test to fail without islands, we are also assuming that it would not
+# otherwise do so. This is true by the current write order, which will put
+# commits (and their contents) before their parents.
+test_expect_success 'island core places core objects first' '
+ cat >expect <<-EOF &&
+ $root
+ $two
+ EOF
+ git -c "pack.island=refs/heads/(.*)" \
+ -c "pack.islandcore=one" \
+ repack -adfi &&
+ git verify-pack -v .git/objects/pack/*.pack |
+ cut -d" " -f1 |
+ grep -E "$root|$two" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'unmatched island core is not fatal' '
+ git -c "pack.islandcore=one" repack -adfi
+'
+
+test_done
diff --git a/t/t5321-pack-large-objects.sh b/t/t5321-pack-large-objects.sh
new file mode 100755
index 0000000..70770fe
--- /dev/null
+++ b/t/t5321-pack-large-objects.sh
@@ -0,0 +1,34 @@
+#!/bin/sh
+#
+# Copyright (c) 2018 Johannes Schindelin
+#
+
+test_description='git pack-object with "large" deltas
+
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-pack.sh
+
+# Two similar-ish objects that we have computed deltas between.
+A=$(test_oid packlib_7_0)
+B=$(test_oid packlib_7_76)
+
+test_expect_success 'setup' '
+ clear_packs &&
+ {
+ pack_header 2 &&
+ pack_obj $A $B &&
+ pack_obj $B
+ } >ab.pack &&
+ pack_trailer ab.pack &&
+ git index-pack --stdin <ab.pack
+'
+
+test_expect_success 'repack large deltas' '
+ printf "%s\\n" $A $B |
+ GIT_TEST_OE_DELTA_SIZE=2 git pack-objects tmp-pack
+'
+
+test_done
diff --git a/t/t5322-pack-objects-sparse.sh b/t/t5322-pack-objects-sparse.sh
new file mode 100755
index 0000000..770695c
--- /dev/null
+++ b/t/t5322-pack-objects-sparse.sh
@@ -0,0 +1,142 @@
+#!/bin/sh
+
+test_description='pack-objects object selection using sparse algorithm'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup repo' '
+ test_commit initial &&
+ for i in $(test_seq 1 3)
+ do
+ mkdir f$i &&
+ for j in $(test_seq 1 3)
+ do
+ mkdir f$i/f$j &&
+ echo $j >f$i/f$j/data.txt || return 1
+ done
+ done &&
+ git add . &&
+ git commit -m "Initialized trees" &&
+ for i in $(test_seq 1 3)
+ do
+ git checkout -b topic$i main &&
+ echo change-$i >f$i/f$i/data.txt &&
+ git commit -a -m "Changed f$i/f$i/data.txt" || return 1
+ done &&
+ cat >packinput.txt <<-EOF &&
+ topic1
+ ^topic2
+ ^topic3
+ EOF
+ git rev-parse \
+ topic1 \
+ topic1^{tree} \
+ topic1:f1 \
+ topic1:f1/f1 \
+ topic1:f1/f1/data.txt | sort >expect_objects.txt
+'
+
+test_expect_success 'non-sparse pack-objects' '
+ git pack-objects --stdout --revs --no-sparse <packinput.txt >nonsparse.pack &&
+ git index-pack -o nonsparse.idx nonsparse.pack &&
+ git show-index <nonsparse.idx | awk "{print \$2}" >nonsparse_objects.txt &&
+ test_cmp expect_objects.txt nonsparse_objects.txt
+'
+
+test_expect_success 'sparse pack-objects' '
+ git pack-objects --stdout --revs --sparse <packinput.txt >sparse.pack &&
+ git index-pack -o sparse.idx sparse.pack &&
+ git show-index <sparse.idx | awk "{print \$2}" >sparse_objects.txt &&
+ test_cmp expect_objects.txt sparse_objects.txt
+'
+
+test_expect_success 'duplicate a folder from f3 and commit to topic1' '
+ git checkout topic1 &&
+ echo change-3 >f3/f3/data.txt &&
+ git commit -a -m "Changed f3/f3/data.txt" &&
+ git rev-parse \
+ topic1~1 \
+ topic1~1^{tree} \
+ topic1^{tree} \
+ topic1 \
+ topic1:f1 \
+ topic1:f1/f1 \
+ topic1:f1/f1/data.txt | sort >required_objects.txt
+'
+
+test_expect_success 'non-sparse pack-objects' '
+ git pack-objects --stdout --revs --no-sparse <packinput.txt >nonsparse.pack &&
+ git index-pack -o nonsparse.idx nonsparse.pack &&
+ git show-index <nonsparse.idx | awk "{print \$2}" >nonsparse_objects.txt &&
+ comm -1 -2 required_objects.txt nonsparse_objects.txt >nonsparse_required_objects.txt &&
+ test_cmp required_objects.txt nonsparse_required_objects.txt
+'
+
+test_expect_success 'sparse pack-objects' '
+ git pack-objects --stdout --revs --sparse <packinput.txt >sparse.pack &&
+ git index-pack -o sparse.idx sparse.pack &&
+ git show-index <sparse.idx | awk "{print \$2}" >sparse_objects.txt &&
+ comm -1 -2 required_objects.txt sparse_objects.txt >sparse_required_objects.txt &&
+ test_cmp required_objects.txt sparse_required_objects.txt
+'
+
+# Demonstrate that the algorithms differ when we copy a tree wholesale
+# from one folder to another.
+
+test_expect_success 'duplicate a folder from f1 into f3' '
+ mkdir f3/f4 &&
+ cp -r f1/f1/* f3/f4 &&
+ git add f3/f4 &&
+ git commit -m "Copied f1/f1 to f3/f4" &&
+ cat >packinput.txt <<-EOF &&
+ topic1
+ ^topic1~1
+ EOF
+ git rev-parse \
+ topic1 \
+ topic1^{tree} \
+ topic1:f3 | sort >required_objects.txt
+'
+
+test_expect_success 'non-sparse pack-objects' '
+ git pack-objects --stdout --revs --no-sparse <packinput.txt >nonsparse.pack &&
+ git index-pack -o nonsparse.idx nonsparse.pack &&
+ git show-index <nonsparse.idx | awk "{print \$2}" >nonsparse_objects.txt &&
+ comm -1 -2 required_objects.txt nonsparse_objects.txt >nonsparse_required_objects.txt &&
+ test_cmp required_objects.txt nonsparse_required_objects.txt
+'
+
+# --sparse is enabled by default by pack.useSparse
+test_expect_success 'sparse pack-objects' '
+ GIT_TEST_PACK_SPARSE=-1 &&
+ git rev-parse \
+ topic1 \
+ topic1^{tree} \
+ topic1:f3 \
+ topic1:f3/f4 \
+ topic1:f3/f4/data.txt | sort >expect_sparse_objects.txt &&
+ git pack-objects --stdout --revs <packinput.txt >sparse.pack &&
+ git index-pack -o sparse.idx sparse.pack &&
+ git show-index <sparse.idx | awk "{print \$2}" >sparse_objects.txt &&
+ test_cmp expect_sparse_objects.txt sparse_objects.txt
+'
+
+test_expect_success 'pack.useSparse enables algorithm' '
+ git config pack.useSparse true &&
+ git pack-objects --stdout --revs <packinput.txt >sparse.pack &&
+ git index-pack -o sparse.idx sparse.pack &&
+ git show-index <sparse.idx | awk "{print \$2}" >sparse_objects.txt &&
+ test_cmp expect_sparse_objects.txt sparse_objects.txt
+'
+
+test_expect_success 'pack.useSparse overridden' '
+ git pack-objects --stdout --revs --no-sparse <packinput.txt >sparse.pack &&
+ git index-pack -o sparse.idx sparse.pack &&
+ git show-index <sparse.idx | awk "{print \$2}" >sparse_objects.txt &&
+ test_cmp required_objects.txt sparse_objects.txt
+'
+
+test_done
diff --git a/t/t5323-pack-redundant.sh b/t/t5323-pack-redundant.sh
new file mode 100755
index 0000000..8dbbcc5
--- /dev/null
+++ b/t/t5323-pack-redundant.sh
@@ -0,0 +1,498 @@
+#!/bin/sh
+#
+# Copyright (c) 2018 Jiang Xin
+#
+
+test_description='Test git pack-redundant
+
+In order to test git-pack-redundant, we will create a number of objects and
+packs in the repository `main.git`. The relationship between packs (P1-P8)
+and objects (T, A-R) is showed in the following chart. Objects of a pack will
+be marked with letter x, while objects of redundant packs will be marked with
+exclamation point, and redundant pack itself will be marked with asterisk.
+
+ | T A B C D E F G H I J K L M N O P Q R
+ ----+--------------------------------------
+ P1 | x x x x x x x x
+ P2* | ! ! ! ! ! ! !
+ P3 | x x x x x x
+ P4* | ! ! ! ! !
+ P5 | x x x x
+ P6* | ! ! !
+ P7 | x x
+ P8* | !
+ ----+--------------------------------------
+ ALL | x x x x x x x x x x x x x x x x x x x
+
+Another repository `shared.git` has unique objects (X-Z), while other objects
+(marked with letter s) are shared through alt-odb (of `main.git`). The
+relationship between packs and objects is as follows:
+
+ | T A B C D E F G H I J K L M N O P Q R X Y Z
+ ----+----------------------------------------------
+ Px1 | s s s x x x
+ Px2 | s s s x x x
+'
+
+. ./test-lib.sh
+
+main_repo=main.git
+shared_repo=shared.git
+
+git_pack_redundant='git pack-redundant --i-still-use-this'
+
+# Create commits in <repo> and assign each commit's oid to shell variables
+# given in the arguments (A, B, and C). E.g.:
+#
+# create_commits_in <repo> A B C
+#
+# NOTE: Avoid calling this function from a subshell since variable
+# assignments will disappear when subshell exits.
+create_commits_in () {
+ repo="$1" &&
+ if ! parent=$(git -C "$repo" rev-parse HEAD^{} 2>/dev/null)
+ then
+ parent=
+ fi &&
+ T=$(git -C "$repo" write-tree) &&
+ shift &&
+ while test $# -gt 0
+ do
+ name=$1 &&
+ test_tick &&
+ if test -z "$parent"
+ then
+ oid=$(echo $name | git -C "$repo" commit-tree $T)
+ else
+ oid=$(echo $name | git -C "$repo" commit-tree -p $parent $T)
+ fi &&
+ eval $name=$oid &&
+ parent=$oid &&
+ shift ||
+ return 1
+ done &&
+ git -C "$repo" update-ref refs/heads/main $oid
+}
+
+# Create pack in <repo> and assign pack id to variable given in the 2nd argument
+# (<name>). Commits in the pack will be read from stdin. E.g.:
+#
+# create_pack_in <repo> <name> <<-EOF
+# ...
+# EOF
+#
+# NOTE: commits from stdin should be given using heredoc, not using pipe, and
+# avoid calling this function from a subshell since variable assignments will
+# disappear when subshell exits.
+create_pack_in () {
+ repo="$1" &&
+ name="$2" &&
+ pack=$(git -C "$repo/objects/pack" pack-objects -q pack) &&
+ eval $name=$pack &&
+ eval P$pack=$name:$pack
+}
+
+format_packfiles () {
+ sed \
+ -e "s#.*/pack-\(.*\)\.idx#\1#" \
+ -e "s#.*/pack-\(.*\)\.pack#\1#" |
+ sort -u |
+ while read p
+ do
+ if test -z "$(eval echo \${P$p})"
+ then
+ echo $p
+ else
+ eval echo "\${P$p}"
+ fi
+ done |
+ sort
+}
+
+test_expect_success 'setup main repo' '
+ git init --bare "$main_repo" &&
+ create_commits_in "$main_repo" A B C D E F G H I J K L M N O P Q R
+'
+
+test_expect_success 'main: pack-redundant works with no packfile' '
+ (
+ cd "$main_repo" &&
+ cat >expect <<-EOF &&
+ fatal: Zero packs found!
+ EOF
+ test_must_fail $git_pack_redundant --all >actual 2>&1 &&
+ test_cmp expect actual
+ )
+'
+
+#############################################################################
+# Chart of packs and objects for this test case
+#
+# | T A B C D E F G H I J K L M N O P Q R
+# ----+--------------------------------------
+# P1 | x x x x x x x x
+# ----+--------------------------------------
+# ALL | x x x x x x x x
+#
+#############################################################################
+test_expect_success 'main: pack-redundant works with one packfile' '
+ create_pack_in "$main_repo" P1 <<-EOF &&
+ $T
+ $A
+ $B
+ $C
+ $D
+ $E
+ $F
+ $R
+ EOF
+ (
+ cd "$main_repo" &&
+ $git_pack_redundant --all >out &&
+ test_must_be_empty out
+ )
+'
+
+#############################################################################
+# Chart of packs and objects for this test case
+#
+# | T A B C D E F G H I J K L M N O P Q R
+# ----+--------------------------------------
+# P1 | x x x x x x x x
+# P2 | x x x x x x x
+# P3 | x x x x x x
+# ----+--------------------------------------
+# ALL | x x x x x x x x x x x x x x x
+#
+#############################################################################
+test_expect_success 'main: no redundant for pack 1, 2, 3' '
+ create_pack_in "$main_repo" P2 <<-EOF &&
+ $B
+ $C
+ $D
+ $E
+ $G
+ $H
+ $I
+ EOF
+ create_pack_in "$main_repo" P3 <<-EOF &&
+ $F
+ $I
+ $J
+ $K
+ $L
+ $M
+ EOF
+ (
+ cd "$main_repo" &&
+ $git_pack_redundant --all >out &&
+ test_must_be_empty out
+ )
+'
+
+#############################################################################
+# Chart of packs and objects for this test case
+#
+# | T A B C D E F G H I J K L M N O P Q R
+# ----+--------------------------------------
+# P1 | x x x x x x x x
+# P2 | x x x x x x x
+# P3* | ! ! ! ! ! !
+# P4 | x x x x x
+# P5 | x x x x
+# ----+--------------------------------------
+# ALL | x x x x x x x x x x x x x x x x x x
+#
+#############################################################################
+test_expect_success 'main: one of pack-2/pack-3 is redundant' '
+ create_pack_in "$main_repo" P4 <<-EOF &&
+ $J
+ $K
+ $L
+ $M
+ $P
+ EOF
+ create_pack_in "$main_repo" P5 <<-EOF &&
+ $G
+ $H
+ $N
+ $O
+ EOF
+ (
+ cd "$main_repo" &&
+ cat >expect <<-EOF &&
+ P3:$P3
+ EOF
+ $git_pack_redundant --all >out &&
+ format_packfiles <out >actual &&
+ test_cmp expect actual
+ )
+'
+
+#############################################################################
+# Chart of packs and objects for this test case
+#
+# | T A B C D E F G H I J K L M N O P Q R
+# ----+--------------------------------------
+# P1 | x x x x x x x x
+# P2* | ! ! ! ! ! ! !
+# P3 | x x x x x x
+# P4* | ! ! ! ! !
+# P5 | x x x x
+# P6* | ! ! !
+# P7 | x x
+# ----+--------------------------------------
+# ALL | x x x x x x x x x x x x x x x x x x x
+#
+#############################################################################
+test_expect_success 'main: pack 2, 4, and 6 are redundant' '
+ create_pack_in "$main_repo" P6 <<-EOF &&
+ $N
+ $O
+ $Q
+ EOF
+ create_pack_in "$main_repo" P7 <<-EOF &&
+ $P
+ $Q
+ EOF
+ (
+ cd "$main_repo" &&
+ cat >expect <<-EOF &&
+ P2:$P2
+ P4:$P4
+ P6:$P6
+ EOF
+ $git_pack_redundant --all >out &&
+ format_packfiles <out >actual &&
+ test_cmp expect actual
+ )
+'
+
+#############################################################################
+# Chart of packs and objects for this test case
+#
+# | T A B C D E F G H I J K L M N O P Q R
+# ----+--------------------------------------
+# P1 | x x x x x x x x
+# P2* | ! ! ! ! ! ! !
+# P3 | x x x x x x
+# P4* | ! ! ! ! !
+# P5 | x x x x
+# P6* | ! ! !
+# P7 | x x
+# P8* | !
+# ----+--------------------------------------
+# ALL | x x x x x x x x x x x x x x x x x x x
+#
+#############################################################################
+test_expect_success 'main: pack-8 (subset of pack-1) is also redundant' '
+ create_pack_in "$main_repo" P8 <<-EOF &&
+ $A
+ EOF
+ (
+ cd "$main_repo" &&
+ cat >expect <<-EOF &&
+ P2:$P2
+ P4:$P4
+ P6:$P6
+ P8:$P8
+ EOF
+ $git_pack_redundant --all >out &&
+ format_packfiles <out >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'main: clean loose objects' '
+ (
+ cd "$main_repo" &&
+ git prune-packed &&
+ find objects -type f | sed -e "/objects\/pack\//d" >out &&
+ test_must_be_empty out
+ )
+'
+
+test_expect_success 'main: remove redundant packs and pass fsck' '
+ (
+ cd "$main_repo" &&
+ $git_pack_redundant --all | xargs rm &&
+ git fsck &&
+ $git_pack_redundant --all >out &&
+ test_must_be_empty out
+ )
+'
+
+# The following test cases will execute inside `shared.git`, instead of
+# inside `main.git`.
+test_expect_success 'setup shared.git' '
+ git clone --mirror "$main_repo" "$shared_repo" &&
+ (
+ cd "$shared_repo" &&
+ printf "../../$main_repo/objects\n" >objects/info/alternates
+ )
+'
+
+test_expect_success 'shared: all packs are redundant, but no output without --alt-odb' '
+ (
+ cd "$shared_repo" &&
+ $git_pack_redundant --all >out &&
+ test_must_be_empty out
+ )
+'
+
+#############################################################################
+# Chart of packs and objects for this test case
+#
+# ================= main.git ================
+# | T A B C D E F G H I J K L M N O P Q R <----------+
+# ----+-------------------------------------- |
+# P1 | x x x x x x x x |
+# P3 | x x x x x x |
+# P5 | x x x x |
+# P7 | x x |
+# ----+-------------------------------------- |
+# ALL | x x x x x x x x x x x x x x x x x x x |
+# |
+# |
+# ================ shared.git =============== |
+# | T A B C D E F G H I J K L M N O P Q R <objects/info/alternates>
+# ----+--------------------------------------
+# P1* | s s s s s s s s
+# P3* | s s s s s s
+# P5* | s s s s
+# P7* | s s
+# ----+--------------------------------------
+# ALL | x x x x x x x x x x x x x x x x x x x
+#
+#############################################################################
+test_expect_success 'shared: show redundant packs in stderr for verbose mode' '
+ (
+ cd "$shared_repo" &&
+ cat >expect <<-EOF &&
+ P1:$P1
+ P3:$P3
+ P5:$P5
+ P7:$P7
+ EOF
+ $git_pack_redundant --all --verbose >out 2>out.err &&
+ test_must_be_empty out &&
+ grep "pack$" out.err | format_packfiles >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'shared: remove redundant packs, no packs left' '
+ (
+ cd "$shared_repo" &&
+ cat >expect <<-EOF &&
+ fatal: Zero packs found!
+ EOF
+ $git_pack_redundant --all --alt-odb | xargs rm &&
+ git fsck &&
+ test_must_fail $git_pack_redundant --all --alt-odb >actual 2>&1 &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'shared: create new objects and packs' '
+ create_commits_in "$shared_repo" X Y Z &&
+ create_pack_in "$shared_repo" Px1 <<-EOF &&
+ $X
+ $Y
+ $Z
+ $A
+ $B
+ $C
+ EOF
+ create_pack_in "$shared_repo" Px2 <<-EOF
+ $X
+ $Y
+ $Z
+ $D
+ $E
+ $F
+ EOF
+'
+
+test_expect_success 'shared: no redundant without --alt-odb' '
+ (
+ cd "$shared_repo" &&
+ $git_pack_redundant --all >out &&
+ test_must_be_empty out
+ )
+'
+
+#############################################################################
+# Chart of packs and objects for this test case
+#
+# ================= main.git ================
+# | T A B C D E F G H I J K L M N O P Q R <----------------+
+# ----+-------------------------------------- |
+# P1 | x x x x x x x x |
+# P3 | x x x x x x |
+# P5 | x x x x |
+# P7 | x x |
+# ----+-------------------------------------- |
+# ALL | x x x x x x x x x x x x x x x x x x x |
+# |
+# |
+# ================ shared.git ======================= |
+# | T A B C D E F G H I J K L M N O P Q R X Y Z <objects/info/alternates>
+# ----+----------------------------------------------
+# Px1 | s s s x x x
+# Px2*| s s s ! ! !
+# ----+----------------------------------------------
+# ALL | s s s s s s s s s s s s s s s s s s s x x x
+#
+#############################################################################
+test_expect_success 'shared: one pack is redundant with --alt-odb' '
+ (
+ cd "$shared_repo" &&
+ $git_pack_redundant --all --alt-odb >out &&
+ format_packfiles <out >actual &&
+ test_line_count = 1 actual
+ )
+'
+
+#############################################################################
+# Chart of packs and objects for this test case
+#
+# ================= main.git ================
+# | T A B C D E F G H I J K L M N O P Q R <----------------+
+# ----+-------------------------------------- |
+# P1 | x x x x x x x x |
+# P3 | x x x x x x |
+# P5 | x x x x |
+# P7 | x x |
+# ----+-------------------------------------- |
+# ALL | x x x x x x x x x x x x x x x x x x x |
+# |
+# |
+# ================ shared.git ======================= |
+# | T A B C D E F G H I J K L M N O P Q R X Y Z <objects/info/alternates>
+# ----+----------------------------------------------
+# Px1*| s s s i i i
+# Px2*| s s s i i i
+# ----+----------------------------------------------
+# ALL | s s s s s s s s s s s s s s s s s s s i i i
+# (ignored objects, marked with i)
+#
+#############################################################################
+test_expect_success 'shared: ignore unique objects and all two packs are redundant' '
+ (
+ cd "$shared_repo" &&
+ cat >expect <<-EOF &&
+ Px1:$Px1
+ Px2:$Px2
+ EOF
+ $git_pack_redundant --all --alt-odb >out <<-EOF &&
+ $X
+ $Y
+ $Z
+ EOF
+ format_packfiles <out >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_done
diff --git a/t/t5324-split-commit-graph.sh b/t/t5324-split-commit-graph.sh
new file mode 100755
index 0000000..281266f
--- /dev/null
+++ b/t/t5324-split-commit-graph.sh
@@ -0,0 +1,721 @@
+#!/bin/sh
+
+test_description='split commit graph'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-chunk.sh
+
+GIT_TEST_COMMIT_GRAPH=0
+GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS=0
+
+test_expect_success 'setup repo' '
+ git init &&
+ git config core.commitGraph true &&
+ git config gc.writeCommitGraph false &&
+ infodir=".git/objects/info" &&
+ graphdir="$infodir/commit-graphs" &&
+ test_oid_cache <<-EOM
+ shallow sha1:2132
+ shallow sha256:2436
+
+ base sha1:1408
+ base sha256:1528
+
+ oid_version sha1:1
+ oid_version sha256:2
+ EOM
+'
+
+graph_read_expect() {
+ NUM_BASE=0
+ if test ! -z $2
+ then
+ NUM_BASE=$2
+ fi
+ OPTIONS=
+ if test -z "$3"
+ then
+ OPTIONS=" read_generation_data"
+ fi
+ cat >expect <<- EOF
+ header: 43475048 1 $(test_oid oid_version) 4 $NUM_BASE
+ num_commits: $1
+ chunks: oid_fanout oid_lookup commit_metadata generation_data
+ options:$OPTIONS
+ EOF
+ test-tool read-graph >output &&
+ test_cmp expect output
+}
+
+test_expect_success POSIXPERM 'tweak umask for modebit tests' '
+ umask 022
+'
+
+test_expect_success 'create commits and write commit-graph' '
+ for i in $(test_seq 3)
+ do
+ test_commit $i &&
+ git branch commits/$i || return 1
+ done &&
+ git commit-graph write --reachable &&
+ test_path_is_file $infodir/commit-graph &&
+ graph_read_expect 3
+'
+
+graph_git_two_modes() {
+ git ${2:+ -C "$2"} -c core.commitGraph=true $1 >output &&
+ git ${2:+ -C "$2"} -c core.commitGraph=false $1 >expect &&
+ test_cmp expect output
+}
+
+graph_git_behavior() {
+ MSG=$1
+ BRANCH=$2
+ COMPARE=$3
+ DIR=$4
+ test_expect_success "check normal git operations: $MSG" '
+ graph_git_two_modes "log --oneline $BRANCH" "$DIR" &&
+ graph_git_two_modes "log --topo-order $BRANCH" "$DIR" &&
+ graph_git_two_modes "log --graph $COMPARE..$BRANCH" "$DIR" &&
+ graph_git_two_modes "branch -vv" "$DIR" &&
+ graph_git_two_modes "merge-base -a $BRANCH $COMPARE" "$DIR"
+ '
+}
+
+graph_git_behavior 'graph exists' commits/3 commits/1
+
+verify_chain_files_exist() {
+ for hash in $(cat $1/commit-graph-chain)
+ do
+ test_path_is_file $1/graph-$hash.graph || return 1
+ done
+}
+
+test_expect_success 'add more commits, and write a new base graph' '
+ git reset --hard commits/1 &&
+ for i in $(test_seq 4 5)
+ do
+ test_commit $i &&
+ git branch commits/$i || return 1
+ done &&
+ git reset --hard commits/2 &&
+ for i in $(test_seq 6 10)
+ do
+ test_commit $i &&
+ git branch commits/$i || return 1
+ done &&
+ git reset --hard commits/2 &&
+ git merge commits/4 &&
+ git branch merge/1 &&
+ git reset --hard commits/4 &&
+ git merge commits/6 &&
+ git branch merge/2 &&
+ git commit-graph write --reachable &&
+ graph_read_expect 12
+'
+
+test_expect_success 'fork and fail to base a chain on a commit-graph file' '
+ test_when_finished rm -rf fork &&
+ git clone . fork &&
+ (
+ cd fork &&
+ rm .git/objects/info/commit-graph &&
+ echo "$(pwd)/../.git/objects" >.git/objects/info/alternates &&
+ test_commit new-commit &&
+ git commit-graph write --reachable --split &&
+ test_path_is_file $graphdir/commit-graph-chain &&
+ test_line_count = 1 $graphdir/commit-graph-chain &&
+ verify_chain_files_exist $graphdir
+ )
+'
+
+test_expect_success 'add three more commits, write a tip graph' '
+ git reset --hard commits/3 &&
+ git merge merge/1 &&
+ git merge commits/5 &&
+ git merge merge/2 &&
+ git branch merge/3 &&
+ git commit-graph write --reachable --split &&
+ test_path_is_missing $infodir/commit-graph &&
+ test_path_is_file $graphdir/commit-graph-chain &&
+ ls $graphdir/graph-*.graph >graph-files &&
+ test_line_count = 2 graph-files &&
+ verify_chain_files_exist $graphdir
+'
+
+graph_git_behavior 'split commit-graph: merge 3 vs 2' merge/3 merge/2
+
+test_expect_success 'add one commit, write a tip graph' '
+ test_commit 11 &&
+ git branch commits/11 &&
+ git commit-graph write --reachable --split &&
+ test_path_is_missing $infodir/commit-graph &&
+ test_path_is_file $graphdir/commit-graph-chain &&
+ ls $graphdir/graph-*.graph >graph-files &&
+ test_line_count = 3 graph-files &&
+ verify_chain_files_exist $graphdir
+'
+
+graph_git_behavior 'three-layer commit-graph: commit 11 vs 6' commits/11 commits/6
+
+test_expect_success 'add one commit, write a merged graph' '
+ test_commit 12 &&
+ git branch commits/12 &&
+ git commit-graph write --reachable --split &&
+ test_path_is_file $graphdir/commit-graph-chain &&
+ test_line_count = 2 $graphdir/commit-graph-chain &&
+ ls $graphdir/graph-*.graph >graph-files &&
+ test_line_count = 2 graph-files &&
+ verify_chain_files_exist $graphdir
+'
+
+graph_git_behavior 'merged commit-graph: commit 12 vs 6' commits/12 commits/6
+
+test_expect_success 'create fork and chain across alternate' '
+ git clone . fork &&
+ (
+ cd fork &&
+ git config core.commitGraph true &&
+ rm -rf $graphdir &&
+ echo "$(pwd)/../.git/objects" >.git/objects/info/alternates &&
+ test_commit 13 &&
+ git branch commits/13 &&
+ git commit-graph write --reachable --split &&
+ test_path_is_file $graphdir/commit-graph-chain &&
+ test_line_count = 3 $graphdir/commit-graph-chain &&
+ ls $graphdir/graph-*.graph >graph-files &&
+ test_line_count = 1 graph-files &&
+ git -c core.commitGraph=true rev-list HEAD >expect &&
+ git -c core.commitGraph=false rev-list HEAD >actual &&
+ test_cmp expect actual &&
+ test_commit 14 &&
+ git commit-graph write --reachable --split --object-dir=.git/objects/ &&
+ test_line_count = 3 $graphdir/commit-graph-chain &&
+ ls $graphdir/graph-*.graph >graph-files &&
+ test_line_count = 1 graph-files
+ )
+'
+
+if test -d fork
+then
+ graph_git_behavior 'alternate: commit 13 vs 6' commits/13 origin/commits/6 "fork"
+fi
+
+test_expect_success 'test merge stragety constants' '
+ git clone . merge-2 &&
+ (
+ cd merge-2 &&
+ git config core.commitGraph true &&
+ test_line_count = 2 $graphdir/commit-graph-chain &&
+ test_commit 14 &&
+ git commit-graph write --reachable --split --size-multiple=2 &&
+ test_line_count = 3 $graphdir/commit-graph-chain
+
+ ) &&
+ git clone . merge-10 &&
+ (
+ cd merge-10 &&
+ git config core.commitGraph true &&
+ test_line_count = 2 $graphdir/commit-graph-chain &&
+ test_commit 14 &&
+ git commit-graph write --reachable --split --size-multiple=10 &&
+ test_line_count = 1 $graphdir/commit-graph-chain &&
+ ls $graphdir/graph-*.graph >graph-files &&
+ test_line_count = 1 graph-files
+ ) &&
+ git clone . merge-10-expire &&
+ (
+ cd merge-10-expire &&
+ git config core.commitGraph true &&
+ test_line_count = 2 $graphdir/commit-graph-chain &&
+ test_commit 15 &&
+ touch $graphdir/to-delete.graph $graphdir/to-keep.graph &&
+ test-tool chmtime =1546362000 $graphdir/to-delete.graph &&
+ test-tool chmtime =1546362001 $graphdir/to-keep.graph &&
+ git commit-graph write --reachable --split --size-multiple=10 \
+ --expire-time="2019-01-01 12:00 -05:00" &&
+ test_line_count = 1 $graphdir/commit-graph-chain &&
+ test_path_is_missing $graphdir/to-delete.graph &&
+ test_path_is_file $graphdir/to-keep.graph &&
+ ls $graphdir/graph-*.graph >graph-files &&
+ test_line_count = 3 graph-files
+ ) &&
+ git clone --no-hardlinks . max-commits &&
+ (
+ cd max-commits &&
+ git config core.commitGraph true &&
+ test_line_count = 2 $graphdir/commit-graph-chain &&
+ test_commit 16 &&
+ test_commit 17 &&
+ git commit-graph write --reachable --split --max-commits=1 &&
+ test_line_count = 1 $graphdir/commit-graph-chain &&
+ ls $graphdir/graph-*.graph >graph-files &&
+ test_line_count = 1 graph-files
+ )
+'
+
+test_expect_success 'remove commit-graph-chain file after flattening' '
+ git clone . flatten &&
+ (
+ cd flatten &&
+ test_line_count = 2 $graphdir/commit-graph-chain &&
+ git commit-graph write --reachable &&
+ test_path_is_missing $graphdir/commit-graph-chain &&
+ ls $graphdir >graph-files &&
+ test_line_count = 0 graph-files
+ )
+'
+
+corrupt_file() {
+ file=$1
+ pos=$2
+ data="${3:-\0}"
+ chmod a+w "$file" &&
+ printf "$data" | dd of="$file" bs=1 seek="$pos" conv=notrunc
+}
+
+test_expect_success 'verify hashes along chain, even in shallow' '
+ git clone --no-hardlinks . verify &&
+ (
+ cd verify &&
+ git commit-graph verify &&
+ base_file=$graphdir/graph-$(head -n 1 $graphdir/commit-graph-chain).graph &&
+ corrupt_file "$base_file" $(test_oid shallow) "\01" &&
+ test_must_fail git commit-graph verify --shallow 2>test_err &&
+ grep -v "^+" test_err >err &&
+ test_grep "incorrect checksum" err
+ )
+'
+
+test_expect_success 'verify notices chain slice which is bogus (base)' '
+ git clone --no-hardlinks . verify-chain-bogus-base &&
+ (
+ cd verify-chain-bogus-base &&
+ git commit-graph verify &&
+ base_file=$graphdir/graph-$(sed -n 1p $graphdir/commit-graph-chain).graph &&
+ echo "garbage" >$base_file &&
+ test_must_fail git commit-graph verify 2>test_err &&
+ grep -v "^+" test_err >err &&
+ grep "commit-graph file is too small" err
+ )
+'
+
+test_expect_success 'verify notices chain slice which is bogus (tip)' '
+ git clone --no-hardlinks . verify-chain-bogus-tip &&
+ (
+ cd verify-chain-bogus-tip &&
+ git commit-graph verify &&
+ tip_file=$graphdir/graph-$(sed -n 2p $graphdir/commit-graph-chain).graph &&
+ echo "garbage" >$tip_file &&
+ test_must_fail git commit-graph verify 2>test_err &&
+ grep -v "^+" test_err >err &&
+ grep "commit-graph file is too small" err
+ )
+'
+
+test_expect_success 'verify --shallow does not check base contents' '
+ git clone --no-hardlinks . verify-shallow &&
+ (
+ cd verify-shallow &&
+ git commit-graph verify &&
+ base_file=$graphdir/graph-$(head -n 1 $graphdir/commit-graph-chain).graph &&
+ corrupt_file "$base_file" 1500 "\01" &&
+ git commit-graph verify --shallow &&
+ test_must_fail git commit-graph verify 2>test_err &&
+ grep -v "^+" test_err >err &&
+ test_grep "incorrect checksum" err
+ )
+'
+
+test_expect_success 'warn on base graph chunk incorrect' '
+ git clone --no-hardlinks . base-chunk &&
+ (
+ cd base-chunk &&
+ git commit-graph verify &&
+ base_file=$graphdir/graph-$(tail -n 1 $graphdir/commit-graph-chain).graph &&
+ corrupt_file "$base_file" $(test_oid base) "\01" &&
+ test_must_fail git commit-graph verify --shallow 2>test_err &&
+ grep -v "^+" test_err >err &&
+ test_grep "commit-graph chain does not match" err
+ )
+'
+
+test_expect_success 'verify after commit-graph-chain corruption (base)' '
+ git clone --no-hardlinks . verify-chain-base &&
+ (
+ cd verify-chain-base &&
+ corrupt_file "$graphdir/commit-graph-chain" 30 "G" &&
+ test_must_fail git commit-graph verify 2>test_err &&
+ grep -v "^+" test_err >err &&
+ test_grep "invalid commit-graph chain" err &&
+ corrupt_file "$graphdir/commit-graph-chain" 30 "A" &&
+ test_must_fail git commit-graph verify 2>test_err &&
+ grep -v "^+" test_err >err &&
+ test_grep "unable to find all commit-graph files" err
+ )
+'
+
+test_expect_success 'verify after commit-graph-chain corruption (tip)' '
+ git clone --no-hardlinks . verify-chain-tip &&
+ (
+ cd verify-chain-tip &&
+ corrupt_file "$graphdir/commit-graph-chain" 70 "G" &&
+ test_must_fail git commit-graph verify 2>test_err &&
+ grep -v "^+" test_err >err &&
+ test_grep "invalid commit-graph chain" err &&
+ corrupt_file "$graphdir/commit-graph-chain" 70 "A" &&
+ test_must_fail git commit-graph verify 2>test_err &&
+ grep -v "^+" test_err >err &&
+ test_grep "unable to find all commit-graph files" err
+ )
+'
+
+test_expect_success 'verify notices too-short chain file' '
+ git clone --no-hardlinks . verify-chain-short &&
+ (
+ cd verify-chain-short &&
+ git commit-graph verify &&
+ echo "garbage" >$graphdir/commit-graph-chain &&
+ test_must_fail git commit-graph verify 2>test_err &&
+ grep -v "^+" test_err >err &&
+ grep "commit-graph chain file too small" err
+ )
+'
+
+test_expect_success 'verify across alternates' '
+ git clone --no-hardlinks . verify-alt &&
+ (
+ cd verify-alt &&
+ rm -rf $graphdir &&
+ altdir="$(pwd)/../.git/objects" &&
+ echo "$altdir" >.git/objects/info/alternates &&
+ git commit-graph verify --object-dir="$altdir/" &&
+ test_commit extra &&
+ git commit-graph write --reachable --split &&
+ tip_file=$graphdir/graph-$(tail -n 1 $graphdir/commit-graph-chain).graph &&
+ corrupt_file "$tip_file" 1500 "\01" &&
+ test_must_fail git commit-graph verify --shallow 2>test_err &&
+ grep -v "^+" test_err >err &&
+ test_grep "incorrect checksum" err
+ )
+'
+
+test_expect_success 'reader bounds-checks base-graph chunk' '
+ git clone --no-hardlinks . corrupt-base-chunk &&
+ (
+ cd corrupt-base-chunk &&
+ tip_file=$graphdir/graph-$(tail -n 1 $graphdir/commit-graph-chain).graph &&
+ corrupt_chunk_file "$tip_file" BASE clear 01020304 &&
+ git -c core.commitGraph=false log >expect.out &&
+ git -c core.commitGraph=true log >out 2>err &&
+ test_cmp expect.out out &&
+ grep "commit-graph base graphs chunk is too small" err
+ )
+'
+
+test_expect_success 'add octopus merge' '
+ git reset --hard commits/10 &&
+ git merge commits/3 commits/4 &&
+ git branch merge/octopus &&
+ git commit-graph write --reachable --split &&
+ git commit-graph verify --progress 2>err &&
+ test_line_count = 1 err &&
+ grep "Verifying commits in commit graph: 100% (18/18)" err &&
+ test_grep ! warning err &&
+ test_line_count = 3 $graphdir/commit-graph-chain
+'
+
+graph_git_behavior 'graph exists' merge/octopus commits/12
+
+test_expect_success 'split across alternate where alternate is not split' '
+ git commit-graph write --reachable &&
+ test_path_is_file .git/objects/info/commit-graph &&
+ cp .git/objects/info/commit-graph . &&
+ git clone --no-hardlinks . alt-split &&
+ (
+ cd alt-split &&
+ rm -f .git/objects/info/commit-graph &&
+ echo "$(pwd)"/../.git/objects >.git/objects/info/alternates &&
+ test_commit 18 &&
+ git commit-graph write --reachable --split &&
+ test_line_count = 1 $graphdir/commit-graph-chain
+ ) &&
+ test_cmp commit-graph .git/objects/info/commit-graph
+'
+
+test_expect_success '--split=no-merge always writes an incremental' '
+ test_when_finished rm -rf a b &&
+ rm -rf $graphdir $infodir/commit-graph &&
+ git reset --hard commits/2 &&
+ git rev-list HEAD~1 >a &&
+ git rev-list HEAD >b &&
+ git commit-graph write --split --stdin-commits <a &&
+ git commit-graph write --split=no-merge --stdin-commits <b &&
+ test_line_count = 2 $graphdir/commit-graph-chain
+'
+
+test_expect_success '--split=replace replaces the chain' '
+ rm -rf $graphdir $infodir/commit-graph &&
+ git reset --hard commits/3 &&
+ git rev-list -1 HEAD~2 >a &&
+ git rev-list -1 HEAD~1 >b &&
+ git rev-list -1 HEAD >c &&
+ git commit-graph write --split=no-merge --stdin-commits <a &&
+ git commit-graph write --split=no-merge --stdin-commits <b &&
+ git commit-graph write --split=no-merge --stdin-commits <c &&
+ test_line_count = 3 $graphdir/commit-graph-chain &&
+ git commit-graph write --stdin-commits --split=replace <b &&
+ test_path_is_missing $infodir/commit-graph &&
+ test_path_is_file $graphdir/commit-graph-chain &&
+ ls $graphdir/graph-*.graph >graph-files &&
+ test_line_count = 1 graph-files &&
+ verify_chain_files_exist $graphdir &&
+ graph_read_expect 2
+'
+
+test_expect_success ULIMIT_FILE_DESCRIPTORS 'handles file descriptor exhaustion' '
+ git init ulimit &&
+ (
+ cd ulimit &&
+ for i in $(test_seq 64)
+ do
+ test_commit $i &&
+ run_with_limited_open_files test_might_fail git commit-graph write \
+ --split=no-merge --reachable || return 1
+ done
+ )
+'
+
+while read mode modebits
+do
+ test_expect_success POSIXPERM "split commit-graph respects core.sharedrepository $mode" '
+ rm -rf $graphdir $infodir/commit-graph &&
+ git reset --hard commits/1 &&
+ test_config core.sharedrepository "$mode" &&
+ git commit-graph write --split --reachable &&
+ ls $graphdir/graph-*.graph >graph-files &&
+ test_line_count = 1 graph-files &&
+ echo "$modebits" >expect &&
+ test_modebits $graphdir/graph-*.graph >actual &&
+ test_cmp expect actual &&
+ test_modebits $graphdir/commit-graph-chain >actual &&
+ test_cmp expect actual
+ '
+done <<\EOF
+0666 -r--r--r--
+0600 -r--------
+EOF
+
+test_expect_success '--split=replace with partial Bloom data' '
+ rm -rf $graphdir $infodir/commit-graph &&
+ git reset --hard commits/3 &&
+ git rev-list -1 HEAD~2 >a &&
+ git rev-list -1 HEAD~1 >b &&
+ git commit-graph write --split=no-merge --stdin-commits --changed-paths <a &&
+ git commit-graph write --split=no-merge --stdin-commits <b &&
+ git commit-graph write --split=replace --stdin-commits --changed-paths <c &&
+ ls $graphdir/graph-*.graph >graph-files &&
+ test_line_count = 1 graph-files &&
+ verify_chain_files_exist $graphdir
+'
+
+test_expect_success 'prevent regression for duplicate commits across layers' '
+ git init dup &&
+ git -C dup commit --allow-empty -m one &&
+ git -C dup -c core.commitGraph=false commit-graph write --split=no-merge --reachable 2>err &&
+ test_grep "attempting to write a commit-graph" err &&
+ git -C dup commit-graph write --split=no-merge --reachable &&
+ git -C dup commit --allow-empty -m two &&
+ git -C dup commit-graph write --split=no-merge --reachable &&
+ git -C dup commit --allow-empty -m three &&
+ git -C dup commit-graph write --split --reachable &&
+ git -C dup commit-graph verify
+'
+
+NUM_FIRST_LAYER_COMMITS=64
+NUM_SECOND_LAYER_COMMITS=16
+NUM_THIRD_LAYER_COMMITS=7
+NUM_FOURTH_LAYER_COMMITS=8
+NUM_FIFTH_LAYER_COMMITS=16
+SECOND_LAYER_SEQUENCE_START=$(($NUM_FIRST_LAYER_COMMITS + 1))
+SECOND_LAYER_SEQUENCE_END=$(($SECOND_LAYER_SEQUENCE_START + $NUM_SECOND_LAYER_COMMITS - 1))
+THIRD_LAYER_SEQUENCE_START=$(($SECOND_LAYER_SEQUENCE_END + 1))
+THIRD_LAYER_SEQUENCE_END=$(($THIRD_LAYER_SEQUENCE_START + $NUM_THIRD_LAYER_COMMITS - 1))
+FOURTH_LAYER_SEQUENCE_START=$(($THIRD_LAYER_SEQUENCE_END + 1))
+FOURTH_LAYER_SEQUENCE_END=$(($FOURTH_LAYER_SEQUENCE_START + $NUM_FOURTH_LAYER_COMMITS - 1))
+FIFTH_LAYER_SEQUENCE_START=$(($FOURTH_LAYER_SEQUENCE_END + 1))
+FIFTH_LAYER_SEQUENCE_END=$(($FIFTH_LAYER_SEQUENCE_START + $NUM_FIFTH_LAYER_COMMITS - 1))
+
+# Current split graph chain:
+#
+# 16 commits (No GDAT)
+# ------------------------
+# 64 commits (GDAT)
+#
+test_expect_success 'setup repo for mixed generation commit-graph-chain' '
+ graphdir=".git/objects/info/commit-graphs" &&
+ test_oid_cache <<-EOF &&
+ oid_version sha1:1
+ oid_version sha256:2
+ EOF
+ git init mixed &&
+ (
+ cd mixed &&
+ git config core.commitGraph true &&
+ git config gc.writeCommitGraph false &&
+ for i in $(test_seq $NUM_FIRST_LAYER_COMMITS)
+ do
+ test_commit $i &&
+ git branch commits/$i || return 1
+ done &&
+ git -c commitGraph.generationVersion=2 commit-graph write --reachable --split &&
+ graph_read_expect $NUM_FIRST_LAYER_COMMITS &&
+ test_line_count = 1 $graphdir/commit-graph-chain &&
+ for i in $(test_seq $SECOND_LAYER_SEQUENCE_START $SECOND_LAYER_SEQUENCE_END)
+ do
+ test_commit $i &&
+ git branch commits/$i || return 1
+ done &&
+ git -c commitGraph.generationVersion=1 commit-graph write --reachable --split=no-merge &&
+ test_line_count = 2 $graphdir/commit-graph-chain &&
+ test-tool read-graph >output &&
+ cat >expect <<-EOF &&
+ header: 43475048 1 $(test_oid oid_version) 4 1
+ num_commits: $NUM_SECOND_LAYER_COMMITS
+ chunks: oid_fanout oid_lookup commit_metadata
+ options:
+ EOF
+ test_cmp expect output &&
+ git commit-graph verify &&
+ cat $graphdir/commit-graph-chain
+ )
+'
+
+# The new layer will be added without generation data chunk as it was not
+# present on the layer underneath it.
+#
+# 7 commits (No GDAT)
+# ------------------------
+# 16 commits (No GDAT)
+# ------------------------
+# 64 commits (GDAT)
+#
+test_expect_success 'do not write generation data chunk if not present on existing tip' '
+ git clone mixed mixed-no-gdat &&
+ (
+ cd mixed-no-gdat &&
+ for i in $(test_seq $THIRD_LAYER_SEQUENCE_START $THIRD_LAYER_SEQUENCE_END)
+ do
+ test_commit $i &&
+ git branch commits/$i || return 1
+ done &&
+ git commit-graph write --reachable --split=no-merge &&
+ test_line_count = 3 $graphdir/commit-graph-chain &&
+ test-tool read-graph >output &&
+ cat >expect <<-EOF &&
+ header: 43475048 1 $(test_oid oid_version) 4 2
+ num_commits: $NUM_THIRD_LAYER_COMMITS
+ chunks: oid_fanout oid_lookup commit_metadata
+ options:
+ EOF
+ test_cmp expect output &&
+ git commit-graph verify
+ )
+'
+
+# Number of commits in each layer of the split-commit graph before merge:
+#
+# 8 commits (No GDAT)
+# ------------------------
+# 7 commits (No GDAT)
+# ------------------------
+# 16 commits (No GDAT)
+# ------------------------
+# 64 commits (GDAT)
+#
+# The top two layers are merged and do not have generation data chunk as layer below them does
+# not have generation data chunk.
+#
+# 15 commits (No GDAT)
+# ------------------------
+# 16 commits (No GDAT)
+# ------------------------
+# 64 commits (GDAT)
+#
+test_expect_success 'do not write generation data chunk if the topmost remaining layer does not have generation data chunk' '
+ git clone mixed-no-gdat mixed-merge-no-gdat &&
+ (
+ cd mixed-merge-no-gdat &&
+ for i in $(test_seq $FOURTH_LAYER_SEQUENCE_START $FOURTH_LAYER_SEQUENCE_END)
+ do
+ test_commit $i &&
+ git branch commits/$i || return 1
+ done &&
+ git commit-graph write --reachable --split --size-multiple 1 &&
+ test_line_count = 3 $graphdir/commit-graph-chain &&
+ test-tool read-graph >output &&
+ cat >expect <<-EOF &&
+ header: 43475048 1 $(test_oid oid_version) 4 2
+ num_commits: $(($NUM_THIRD_LAYER_COMMITS + $NUM_FOURTH_LAYER_COMMITS))
+ chunks: oid_fanout oid_lookup commit_metadata
+ options:
+ EOF
+ test_cmp expect output &&
+ git commit-graph verify
+ )
+'
+
+# Number of commits in each layer of the split-commit graph before merge:
+#
+# 16 commits (No GDAT)
+# ------------------------
+# 15 commits (No GDAT)
+# ------------------------
+# 16 commits (No GDAT)
+# ------------------------
+# 64 commits (GDAT)
+#
+# The top three layers are merged and has generation data chunk as the topmost remaining layer
+# has generation data chunk.
+#
+# 47 commits (GDAT)
+# ------------------------
+# 64 commits (GDAT)
+#
+test_expect_success 'write generation data chunk if topmost remaining layer has generation data chunk' '
+ git clone mixed-merge-no-gdat mixed-merge-gdat &&
+ (
+ cd mixed-merge-gdat &&
+ for i in $(test_seq $FIFTH_LAYER_SEQUENCE_START $FIFTH_LAYER_SEQUENCE_END)
+ do
+ test_commit $i &&
+ git branch commits/$i || return 1
+ done &&
+ git commit-graph write --reachable --split --size-multiple 1 &&
+ test_line_count = 2 $graphdir/commit-graph-chain &&
+ test-tool read-graph >output &&
+ cat >expect <<-EOF &&
+ header: 43475048 1 $(test_oid oid_version) 5 1
+ num_commits: $(($NUM_SECOND_LAYER_COMMITS + $NUM_THIRD_LAYER_COMMITS + $NUM_FOURTH_LAYER_COMMITS + $NUM_FIFTH_LAYER_COMMITS))
+ chunks: oid_fanout oid_lookup commit_metadata generation_data
+ options: read_generation_data
+ EOF
+ test_cmp expect output
+ )
+'
+
+test_expect_success 'write generation data chunk when commit-graph chain is replaced' '
+ git clone mixed mixed-replace &&
+ (
+ cd mixed-replace &&
+ git commit-graph write --reachable --split=replace &&
+ test_path_is_file $graphdir/commit-graph-chain &&
+ test_line_count = 1 $graphdir/commit-graph-chain &&
+ verify_chain_files_exist $graphdir &&
+ graph_read_expect $(($NUM_FIRST_LAYER_COMMITS + $NUM_SECOND_LAYER_COMMITS)) &&
+ git commit-graph verify
+ )
+'
+
+test_done
diff --git a/t/t5325-reverse-index.sh b/t/t5325-reverse-index.sh
new file mode 100755
index 0000000..431a603
--- /dev/null
+++ b/t/t5325-reverse-index.sh
@@ -0,0 +1,208 @@
+#!/bin/sh
+
+test_description='on-disk reverse index'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# The below tests want control over the 'pack.writeReverseIndex' setting
+# themselves to assert various combinations of it with other options.
+sane_unset GIT_TEST_NO_WRITE_REV_INDEX
+
+packdir=.git/objects/pack
+
+test_expect_success 'setup' '
+ test_commit base &&
+
+ test_config pack.writeReverseIndex false &&
+ pack=$(git pack-objects --all $packdir/pack) &&
+ rev=$packdir/pack-$pack.rev &&
+
+ test_path_is_missing $rev
+'
+
+test_index_pack () {
+ rm -f $rev &&
+ conf=$1 &&
+ shift &&
+ # remove the index since Windows won't overwrite an existing file
+ rm $packdir/pack-$pack.idx &&
+ git -c pack.writeReverseIndex=$conf index-pack "$@" \
+ $packdir/pack-$pack.pack
+}
+
+test_expect_success 'index-pack with pack.writeReverseIndex' '
+ test_index_pack "" &&
+ test_path_is_missing $rev &&
+
+ test_index_pack false &&
+ test_path_is_missing $rev &&
+
+ test_index_pack true &&
+ test_path_is_file $rev
+'
+
+test_expect_success 'index-pack with --[no-]rev-index' '
+ for conf in "" true false
+ do
+ test_index_pack "$conf" --rev-index &&
+ test_path_exists $rev &&
+
+ test_index_pack "$conf" --no-rev-index &&
+ test_path_is_missing $rev || return 1
+ done
+'
+
+test_expect_success 'index-pack can verify reverse indexes' '
+ test_when_finished "rm -f $rev" &&
+ test_index_pack true &&
+
+ test_path_is_file $rev &&
+ git index-pack --rev-index --verify $packdir/pack-$pack.pack &&
+
+ # Intentionally corrupt the reverse index.
+ chmod u+w $rev &&
+ printf "xxxx" | dd of=$rev bs=1 count=4 conv=notrunc &&
+
+ test_must_fail git index-pack --rev-index --verify \
+ $packdir/pack-$pack.pack 2>err &&
+ grep "validation error" err
+'
+
+test_expect_success 'index-pack infers reverse index name with -o' '
+ git index-pack --rev-index -o other.idx $packdir/pack-$pack.pack &&
+ test_path_is_file other.idx &&
+ test_path_is_file other.rev
+'
+
+test_expect_success 'pack-objects respects pack.writeReverseIndex' '
+ test_when_finished "rm -fr pack-1-*" &&
+
+ git -c pack.writeReverseIndex= pack-objects --all pack-1 &&
+ test_path_is_missing pack-1-*.rev &&
+
+ git -c pack.writeReverseIndex=false pack-objects --all pack-1 &&
+ test_path_is_missing pack-1-*.rev &&
+
+ git -c pack.writeReverseIndex=true pack-objects --all pack-1 &&
+ test_path_is_file pack-1-*.rev
+'
+
+test_expect_success 'reverse index is not generated when available on disk' '
+ test_index_pack true &&
+ test_path_is_file $rev &&
+
+ git rev-parse HEAD >tip &&
+ GIT_TEST_REV_INDEX_DIE_IN_MEMORY=1 git cat-file \
+ --batch-check="%(objectsize:disk)" <tip
+'
+
+test_expect_success 'reverse index is ignored when pack.readReverseIndex is false' '
+ test_index_pack true &&
+ test_path_is_file $rev &&
+
+ test_config pack.readReverseIndex false &&
+
+ git rev-parse HEAD >tip &&
+ GIT_TEST_REV_INDEX_DIE_ON_DISK=1 git cat-file \
+ --batch-check="%(objectsize:disk)" <tip
+'
+
+test_expect_success 'revindex in-memory vs on-disk' '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+
+ test_commit commit &&
+
+ git rev-list --objects --no-object-names --all >objects &&
+
+ git -c pack.writeReverseIndex=false repack -ad &&
+ test_path_is_missing $packdir/pack-*.rev &&
+ git cat-file --batch-check="%(objectsize:disk) %(objectname)" \
+ <objects >in-core &&
+
+ git -c pack.writeReverseIndex=true repack -ad &&
+ test_path_is_file $packdir/pack-*.rev &&
+ git cat-file --batch-check="%(objectsize:disk) %(objectname)" \
+ <objects >on-disk &&
+
+ test_cmp on-disk in-core
+ )
+'
+
+test_expect_success 'fsck succeeds on good rev-index' '
+ test_when_finished rm -fr repo &&
+ git init repo &&
+ (
+ cd repo &&
+
+ test_commit commit &&
+ git -c pack.writeReverseIndex=true repack -ad &&
+ git fsck 2>err &&
+ test_must_be_empty err
+ )
+'
+
+test_expect_success 'set up rev-index corruption tests' '
+ git init corrupt &&
+ (
+ cd corrupt &&
+
+ test_commit commit &&
+ git -c pack.writeReverseIndex=true repack -ad &&
+
+ revfile=$(ls .git/objects/pack/pack-*.rev) &&
+ chmod a+w $revfile &&
+ cp $revfile $revfile.bak
+ )
+'
+
+corrupt_rev_and_verify () {
+ (
+ pos="$1" &&
+ value="$2" &&
+ error="$3" &&
+
+ cd corrupt &&
+ revfile=$(ls .git/objects/pack/pack-*.rev) &&
+
+ # Reset to original rev-file.
+ cp $revfile.bak $revfile &&
+
+ printf "$value" | dd of=$revfile bs=1 seek="$pos" conv=notrunc &&
+ test_must_fail git fsck 2>err &&
+ grep "$error" err
+ )
+}
+
+test_expect_success 'fsck catches invalid checksum' '
+ revfile=$(ls corrupt/.git/objects/pack/pack-*.rev) &&
+ orig_size=$(wc -c <$revfile) &&
+ hashpos=$((orig_size - 10)) &&
+ corrupt_rev_and_verify $hashpos bogus \
+ "invalid checksum"
+'
+
+test_expect_success 'fsck catches invalid row position' '
+ corrupt_rev_and_verify 14 "\07" \
+ "invalid rev-index position"
+'
+
+test_expect_success 'fsck catches invalid header: magic number' '
+ corrupt_rev_and_verify 1 "\07" \
+ "reverse-index file .* has unknown signature"
+'
+
+test_expect_success 'fsck catches invalid header: version' '
+ corrupt_rev_and_verify 7 "\02" \
+ "reverse-index file .* has unsupported version"
+'
+
+test_expect_success 'fsck catches invalid header: hash function' '
+ corrupt_rev_and_verify 11 "\03" \
+ "reverse-index file .* has unsupported hash id"
+'
+
+test_done
diff --git a/t/t5326-multi-pack-bitmaps.sh b/t/t5326-multi-pack-bitmaps.sh
new file mode 100755
index 0000000..70d1b58
--- /dev/null
+++ b/t/t5326-multi-pack-bitmaps.sh
@@ -0,0 +1,516 @@
+#!/bin/sh
+
+test_description='exercise basic multi-pack bitmap functionality'
+. ./test-lib.sh
+. "${TEST_DIRECTORY}/lib-bitmap.sh"
+
+# We'll be writing our own midx and bitmaps, so avoid getting confused by the
+# automatic ones.
+GIT_TEST_MULTI_PACK_INDEX=0
+GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0
+
+# This test exercise multi-pack bitmap functionality where the object order is
+# stored and read from a special chunk within the MIDX, so use the default
+# behavior here.
+sane_unset GIT_TEST_MIDX_WRITE_REV
+sane_unset GIT_TEST_MIDX_READ_RIDX
+
+bitmap_reuse_tests() {
+ from=$1
+ to=$2
+ writeLookupTable=false
+
+ for i in $3-${$#}
+ do
+ case $i in
+ "pack.writeBitmapLookupTable") writeLookupTable=true;;
+ esac
+ done
+
+ test_expect_success "setup pack reuse tests ($from -> $to)" '
+ rm -fr repo &&
+ git init repo &&
+ (
+ cd repo &&
+ git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
+ test_commit_bulk 16 &&
+ git tag old-tip &&
+
+ git config core.multiPackIndex true &&
+ if test "MIDX" = "$from"
+ then
+ git repack -Ad &&
+ git multi-pack-index write --bitmap
+ else
+ git repack -Adb
+ fi
+ )
+ '
+
+ test_expect_success "build bitmap from existing ($from -> $to)" '
+ (
+ cd repo &&
+ git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
+ test_commit_bulk --id=further 16 &&
+ git tag new-tip &&
+
+ if test "MIDX" = "$to"
+ then
+ git repack -d &&
+ git multi-pack-index write --bitmap
+ else
+ git repack -Adb
+ fi
+ )
+ '
+
+ test_expect_success "verify resulting bitmaps ($from -> $to)" '
+ (
+ cd repo &&
+ git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
+ git for-each-ref &&
+ git rev-list --test-bitmap refs/tags/old-tip &&
+ git rev-list --test-bitmap refs/tags/new-tip
+ )
+ '
+}
+
+test_midx_bitmap_cases () {
+ writeLookupTable=false
+ writeBitmapLookupTable=
+
+ for i in "$@"
+ do
+ case $i in
+ "pack.writeBitmapLookupTable")
+ writeLookupTable=true
+ writeBitmapLookupTable="$i"
+ ;;
+ esac
+ done
+
+ test_expect_success 'setup test_repository' '
+ rm -rf * .git &&
+ git init &&
+ git config pack.writeBitmapLookupTable '"$writeLookupTable"'
+ '
+
+ midx_bitmap_core
+
+ bitmap_reuse_tests 'pack' 'MIDX' "$writeBitmapLookupTable"
+ bitmap_reuse_tests 'MIDX' 'pack' "$writeBitmapLookupTable"
+ bitmap_reuse_tests 'MIDX' 'MIDX' "$writeBitmapLookupTable"
+
+ test_expect_success 'missing object closure fails gracefully' '
+ rm -fr repo &&
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+ git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
+
+ test_commit loose &&
+ test_commit packed &&
+
+ # Do not pass "--revs"; we want a pack without the "loose"
+ # commit.
+ git pack-objects $objdir/pack/pack <<-EOF &&
+ $(git rev-parse packed)
+ EOF
+
+ test_must_fail git multi-pack-index write --bitmap 2>err &&
+ grep "doesn.t have full closure" err &&
+ test_path_is_missing $midx
+ )
+ '
+
+ midx_bitmap_partial_tests
+
+ test_expect_success 'removing a MIDX clears stale bitmaps' '
+ rm -fr repo &&
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+ git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
+ test_commit base &&
+ git repack &&
+ git multi-pack-index write --bitmap &&
+
+ # Write a MIDX and bitmap; remove the MIDX but leave the bitmap.
+ stale_bitmap=$midx-$(midx_checksum $objdir).bitmap &&
+ rm $midx &&
+
+ # Then write a new MIDX.
+ test_commit new &&
+ git repack &&
+ git multi-pack-index write --bitmap &&
+
+ test_path_is_file $midx &&
+ test_path_is_file $midx-$(midx_checksum $objdir).bitmap &&
+ test_path_is_missing $stale_bitmap
+ )
+ '
+
+ test_expect_success 'pack.preferBitmapTips' '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+ git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
+
+ test_commit_bulk --message="%s" 103 &&
+
+ git log --format="%H" >commits.raw &&
+ sort <commits.raw >commits &&
+
+ git log --format="create refs/tags/%s %H" HEAD >refs &&
+ git update-ref --stdin <refs &&
+
+ git multi-pack-index write --bitmap &&
+ test_path_is_file $midx &&
+ test_path_is_file $midx-$(midx_checksum $objdir).bitmap &&
+
+ test-tool bitmap list-commits | sort >bitmaps &&
+ comm -13 bitmaps commits >before &&
+ test_line_count = 1 before &&
+
+ perl -ne "printf(\"create refs/tags/include/%d \", $.); print" \
+ <before | git update-ref --stdin &&
+
+ rm -fr $midx-$(midx_checksum $objdir).bitmap &&
+ rm -fr $midx &&
+
+ git -c pack.preferBitmapTips=refs/tags/include \
+ multi-pack-index write --bitmap &&
+ test-tool bitmap list-commits | sort >bitmaps &&
+ comm -13 bitmaps commits >after &&
+
+ ! test_cmp before after
+ )
+ '
+
+ test_expect_success 'writing a bitmap with --refs-snapshot' '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+ git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
+
+ test_commit one &&
+ test_commit two &&
+
+ git rev-parse one >snapshot &&
+
+ git repack -ad &&
+
+ # First, write a MIDX which see both refs/tags/one and
+ # refs/tags/two (causing both of those commits to receive
+ # bitmaps).
+ git multi-pack-index write --bitmap &&
+
+ test_path_is_file $midx &&
+ test_path_is_file $midx-$(midx_checksum $objdir).bitmap &&
+
+ test-tool bitmap list-commits | sort >bitmaps &&
+ grep "$(git rev-parse one)" bitmaps &&
+ grep "$(git rev-parse two)" bitmaps &&
+
+ rm -fr $midx-$(midx_checksum $objdir).bitmap &&
+ rm -fr $midx &&
+
+ # Then again, but with a refs snapshot which only sees
+ # refs/tags/one.
+ git multi-pack-index write --bitmap --refs-snapshot=snapshot &&
+
+ test_path_is_file $midx &&
+ test_path_is_file $midx-$(midx_checksum $objdir).bitmap &&
+
+ test-tool bitmap list-commits | sort >bitmaps &&
+ grep "$(git rev-parse one)" bitmaps &&
+ ! grep "$(git rev-parse two)" bitmaps
+ )
+ '
+
+ test_expect_success 'write a bitmap with --refs-snapshot (preferred tips)' '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+ git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
+
+ test_commit_bulk --message="%s" 103 &&
+
+ git log --format="%H" >commits.raw &&
+ sort <commits.raw >commits &&
+
+ git log --format="create refs/tags/%s %H" HEAD >refs &&
+ git update-ref --stdin <refs &&
+
+ git multi-pack-index write --bitmap &&
+ test_path_is_file $midx &&
+ test_path_is_file $midx-$(midx_checksum $objdir).bitmap &&
+
+ test-tool bitmap list-commits | sort >bitmaps &&
+ comm -13 bitmaps commits >before &&
+ test_line_count = 1 before &&
+
+ (
+ grep -vf before commits.raw &&
+ # mark missing commits as preferred
+ sed "s/^/+/" before
+ ) >snapshot &&
+
+ rm -fr $midx-$(midx_checksum $objdir).bitmap &&
+ rm -fr $midx &&
+
+ git multi-pack-index write --bitmap --refs-snapshot=snapshot &&
+ test-tool bitmap list-commits | sort >bitmaps &&
+ comm -13 bitmaps commits >after &&
+
+ ! test_cmp before after
+ )
+ '
+
+ test_expect_success 'hash-cache values are propagated from pack bitmaps' '
+ rm -fr repo &&
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+ git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
+
+ test_commit base &&
+ test_commit base2 &&
+ git repack -adb &&
+
+ test-tool bitmap dump-hashes >pack.raw &&
+ test_file_not_empty pack.raw &&
+ sort pack.raw >pack.hashes &&
+
+ test_commit new &&
+ git repack &&
+ git multi-pack-index write --bitmap &&
+
+ test-tool bitmap dump-hashes >midx.raw &&
+ sort midx.raw >midx.hashes &&
+
+ # ensure that every namehash in the pack bitmap can be found in
+ # the midx bitmap (i.e., that there are no oid-namehash pairs
+ # unique to the pack bitmap).
+ comm -23 pack.hashes midx.hashes >dropped.hashes &&
+ test_must_be_empty dropped.hashes
+ )
+ '
+
+ test_expect_success 'no .bitmap is written without any objects' '
+ rm -fr repo &&
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+ git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
+
+ empty="$(git pack-objects $objdir/pack/pack </dev/null)" &&
+ cat >packs <<-EOF &&
+ pack-$empty.idx
+ EOF
+
+ git multi-pack-index write --bitmap --stdin-packs \
+ <packs 2>err &&
+
+ grep "bitmap without any objects" err &&
+
+ test_path_is_file $midx &&
+ test_path_is_missing $midx-$(midx_checksum $objdir).bitmap
+ )
+ '
+
+ test_expect_success 'graceful fallback when missing reverse index' '
+ rm -fr repo &&
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+ git config pack.writeBitmapLookupTable '"$writeLookupTable"' &&
+
+ test_commit base &&
+
+ # write a pack and MIDX bitmap containing base
+ git repack -adb &&
+ git multi-pack-index write --bitmap &&
+
+ GIT_TEST_MIDX_READ_RIDX=0 \
+ git rev-list --use-bitmap-index HEAD 2>err &&
+ ! grep "ignoring extra bitmap file" err
+ )
+ '
+}
+
+test_midx_bitmap_cases
+
+test_midx_bitmap_cases "pack.writeBitmapLookupTable"
+
+test_expect_success 'multi-pack-index write writes lookup table if enabled' '
+ rm -fr repo &&
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+ test_commit base &&
+ git config pack.writeBitmapLookupTable true &&
+ git repack -ad &&
+ GIT_TRACE2_EVENT="$(pwd)/trace" \
+ git multi-pack-index write --bitmap &&
+ grep "\"label\":\"writing_lookup_table\"" trace
+ )
+'
+
+test_expect_success 'preferred pack change with existing MIDX bitmap' '
+ git init preferred-pack-with-existing &&
+ (
+ cd preferred-pack-with-existing &&
+
+ test_commit base &&
+ test_commit other &&
+
+ git rev-list --objects --no-object-names base >p1.objects &&
+ git rev-list --objects --no-object-names other >p2.objects &&
+
+ p1="$(git pack-objects "$objdir/pack/pack" \
+ --delta-base-offset <p1.objects)" &&
+ p2="$(git pack-objects "$objdir/pack/pack" \
+ --delta-base-offset <p2.objects)" &&
+
+ # Generate a MIDX containing the first two packs,
+ # marking p1 as preferred, and ensure that it can be
+ # successfully cloned.
+ git multi-pack-index write --bitmap \
+ --preferred-pack="pack-$p1.pack" &&
+ test_path_is_file $midx &&
+ test_path_is_file $midx-$(midx_checksum $objdir).bitmap &&
+ git clone --no-local . clone1 &&
+
+ # Then generate a new pack which sorts ahead of any
+ # existing pack (by tweaking the pack prefix).
+ test_commit foo &&
+ git pack-objects --all --unpacked $objdir/pack/pack0 &&
+
+ # Generate a new MIDX which changes the preferred pack
+ # to a pack contained in the existing MIDX.
+ git multi-pack-index write --bitmap \
+ --preferred-pack="pack-$p2.pack" &&
+ test_path_is_file $midx &&
+ test_path_is_file $midx-$(midx_checksum $objdir).bitmap &&
+
+ # When the above circumstances are met, the preferred
+ # pack should change appropriately and clones should
+ # (still) succeed.
+ git clone --no-local . clone2
+ )
+'
+
+test_expect_success 'tagged commits are selected for bitmapping' '
+ rm -fr repo &&
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+
+ test_commit --annotate base &&
+ git repack -d &&
+
+ # Remove refs/heads/main which points at the commit directly,
+ # leaving only a reference to the annotated tag.
+ git branch -M main &&
+ git checkout base &&
+ git branch -d main &&
+
+ git multi-pack-index write --bitmap &&
+
+ git rev-parse HEAD >want &&
+ test-tool bitmap list-commits >actual &&
+ grep $(cat want) actual
+ )
+'
+
+corrupt_file () {
+ chmod a+w "$1" &&
+ printf "bogus" | dd of="$1" bs=1 seek="12" conv=notrunc
+}
+
+test_expect_success 'git fsck correctly identifies good and bad bitmaps' '
+ git init valid &&
+ test_when_finished rm -rf valid &&
+
+ test_commit_bulk 20 &&
+ git repack -adbf &&
+
+ # Move pack-bitmap aside so it is not deleted
+ # in next repack.
+ packbitmap=$(ls .git/objects/pack/pack-*.bitmap) &&
+ mv "$packbitmap" "$packbitmap.bak" &&
+
+ test_commit_bulk 10 &&
+ git repack -b --write-midx &&
+ midxbitmap=$(ls .git/objects/pack/multi-pack-index-*.bitmap) &&
+
+ # Copy MIDX bitmap to backup. Copy pack bitmap from backup.
+ cp "$midxbitmap" "$midxbitmap.bak" &&
+ cp "$packbitmap.bak" "$packbitmap" &&
+
+ # fsck works at first
+ git fsck 2>err &&
+ test_must_be_empty err &&
+
+ corrupt_file "$packbitmap" &&
+ test_must_fail git fsck 2>err &&
+ grep "bitmap file '\''$packbitmap'\'' has invalid checksum" err &&
+
+ cp "$packbitmap.bak" "$packbitmap" &&
+ corrupt_file "$midxbitmap" &&
+ test_must_fail git fsck 2>err &&
+ grep "bitmap file '\''$midxbitmap'\'' has invalid checksum" err &&
+
+ corrupt_file "$packbitmap" &&
+ test_must_fail git fsck 2>err &&
+ grep "bitmap file '\''$midxbitmap'\'' has invalid checksum" err &&
+ grep "bitmap file '\''$packbitmap'\'' has invalid checksum" err
+'
+
+test_expect_success 'corrupt MIDX with bitmap causes fallback' '
+ git init corrupt-midx-bitmap &&
+ (
+ cd corrupt-midx-bitmap &&
+
+ test_commit first &&
+ git repack -d &&
+ test_commit second &&
+ git repack -d &&
+
+ git multi-pack-index write --bitmap &&
+ checksum=$(midx_checksum $objdir) &&
+ for f in $midx $midx-$checksum.bitmap
+ do
+ mv $f $f.bak || return 1
+ done &&
+
+ # pack everything together, invalidating the MIDX
+ git repack -ad &&
+ # then restore the now-stale MIDX
+ for f in $midx $midx-$checksum.bitmap
+ do
+ mv $f.bak $f || return 1
+ done &&
+
+ git rev-list --count --objects --use-bitmap-index HEAD >out 2>err &&
+ # should attempt opening the broken pack twice (once
+ # from the attempt to load it via the stale bitmap, and
+ # again when attempting to load it from the stale MIDX)
+ # before falling back to the non-MIDX case
+ test 2 -eq $(grep -c "could not open pack" err) &&
+ test 6 -eq $(cat out)
+ )
+'
+
+test_done
diff --git a/t/t5327-multi-pack-bitmaps-rev.sh b/t/t5327-multi-pack-bitmaps-rev.sh
new file mode 100755
index 0000000..e65e311
--- /dev/null
+++ b/t/t5327-multi-pack-bitmaps-rev.sh
@@ -0,0 +1,43 @@
+#!/bin/sh
+
+test_description='exercise basic multi-pack bitmap functionality (.rev files)'
+
+. ./test-lib.sh
+. "${TEST_DIRECTORY}/lib-bitmap.sh"
+
+# We'll be writing our own midx and bitmaps, so avoid getting confused by the
+# automatic ones.
+GIT_TEST_MULTI_PACK_INDEX=0
+GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0
+
+# Unlike t5326, this test exercise multi-pack bitmap functionality where the
+# object order is stored in a separate .rev file.
+GIT_TEST_MIDX_WRITE_REV=1
+GIT_TEST_MIDX_READ_RIDX=0
+export GIT_TEST_MIDX_WRITE_REV
+export GIT_TEST_MIDX_READ_RIDX
+
+test_midx_bitmap_rev () {
+ writeLookupTable=false
+
+ for i in "$@"
+ do
+ case $i in
+ "pack.writeBitmapLookupTable") writeLookupTable=true;;
+ esac
+ done
+
+ test_expect_success 'setup bitmap config' '
+ rm -rf * .git &&
+ git init &&
+ git config pack.writeBitmapLookupTable '"$writeLookupTable"'
+ '
+
+ midx_bitmap_core rev
+ midx_bitmap_partial_tests rev
+}
+
+test_midx_bitmap_rev
+test_midx_bitmap_rev "pack.writeBitmapLookupTable"
+
+test_done
diff --git a/t/t5328-commit-graph-64bit-time.sh b/t/t5328-commit-graph-64bit-time.sh
new file mode 100755
index 0000000..fc6a242
--- /dev/null
+++ b/t/t5328-commit-graph-64bit-time.sh
@@ -0,0 +1,87 @@
+#!/bin/sh
+
+test_description='commit graph with 64-bit timestamps'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+if ! test_have_prereq TIME_IS_64BIT || ! test_have_prereq TIME_T_IS_64BIT
+then
+ skip_all='skipping 64-bit timestamp tests'
+ test_done
+fi
+
+. "$TEST_DIRECTORY"/lib-commit-graph.sh
+. "$TEST_DIRECTORY/lib-chunk.sh"
+
+UNIX_EPOCH_ZERO="@0 +0000"
+FUTURE_DATE="@4147483646 +0000"
+
+GIT_TEST_COMMIT_GRAPH_CHANGED_PATHS=0
+
+test_expect_success 'lower layers have overflow chunk' '
+ rm -f .git/objects/info/commit-graph &&
+ test_commit --date "$FUTURE_DATE" future-1 &&
+ test_commit --date "$UNIX_EPOCH_ZERO" old-1 &&
+ git commit-graph write --reachable &&
+ test_commit --date "$FUTURE_DATE" future-2 &&
+ test_commit --date "$UNIX_EPOCH_ZERO" old-2 &&
+ git commit-graph write --reachable --split=no-merge &&
+ test_commit extra &&
+ git commit-graph write --reachable --split=no-merge &&
+ git commit-graph write --reachable &&
+ graph_read_expect 5 "generation_data generation_data_overflow" &&
+ mv .git/objects/info/commit-graph commit-graph-upgraded &&
+ git commit-graph write --reachable &&
+ graph_read_expect 5 "generation_data generation_data_overflow" &&
+ test_cmp .git/objects/info/commit-graph commit-graph-upgraded
+'
+
+graph_git_behavior 'overflow' '' HEAD~2 HEAD
+
+test_expect_success 'set up and verify repo with generation data overflow chunk' '
+ git init repo &&
+ (
+ cd repo &&
+ test_commit --date "$UNIX_EPOCH_ZERO" 1 &&
+ test_commit 2 &&
+ test_commit --date "$UNIX_EPOCH_ZERO" 3 &&
+ git commit-graph write --reachable &&
+ graph_read_expect 3 generation_data &&
+ test_commit --date "$FUTURE_DATE" 4 &&
+ test_commit 5 &&
+ test_commit --date "$UNIX_EPOCH_ZERO" 6 &&
+ git branch left &&
+ git reset --hard 3 &&
+ test_commit 7 &&
+ test_commit --date "$FUTURE_DATE" 8 &&
+ test_commit 9 &&
+ git branch right &&
+ git reset --hard 3 &&
+ test_merge M left right &&
+ git commit-graph write --reachable &&
+ graph_read_expect 10 "generation_data generation_data_overflow" &&
+ git commit-graph verify
+ )
+'
+
+graph_git_behavior 'overflow 2' repo left right
+
+test_expect_success 'single commit with generation data exceeding UINT32_MAX' '
+ git init repo-uint32-max &&
+ test_commit -C repo-uint32-max --date "@4294967297 +0000" 1 &&
+ git -C repo-uint32-max commit-graph write --reachable &&
+ graph_read_expect -C repo-uint32-max 1 "generation_data" &&
+ git -C repo-uint32-max commit-graph verify
+'
+
+test_expect_success 'reader notices out-of-bounds generation overflow' '
+ graph=.git/objects/info/commit-graph &&
+ test_when_finished "rm -rf $graph" &&
+ git commit-graph write --reachable &&
+ corrupt_chunk_file $graph GDO2 clear &&
+ test_must_fail git log 2>err &&
+ grep "commit-graph overflow generation data is too small" err
+'
+
+test_done
diff --git a/t/t5329-pack-objects-cruft.sh b/t/t5329-pack-objects-cruft.sh
new file mode 100755
index 0000000..fc5fedb
--- /dev/null
+++ b/t/t5329-pack-objects-cruft.sh
@@ -0,0 +1,947 @@
+#!/bin/sh
+
+test_description='cruft pack related pack-objects tests'
+. ./test-lib.sh
+
+objdir=.git/objects
+packdir=$objdir/pack
+
+basic_cruft_pack_tests () {
+ expire="$1"
+
+ test_expect_success "unreachable loose objects are packed (expire $expire)" '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+
+ test_commit base &&
+ git repack -Ad &&
+ test_commit loose &&
+
+ test-tool chmtime +2000 "$objdir/$(test_oid_to_path \
+ $(git rev-parse loose:loose.t))" &&
+ test-tool chmtime +1000 "$objdir/$(test_oid_to_path \
+ $(git rev-parse loose^{tree}))" &&
+
+ (
+ git rev-list --objects --no-object-names base..loose |
+ while read oid
+ do
+ path="$objdir/$(test_oid_to_path "$oid")" &&
+ printf "%s %d\n" "$oid" "$(test-tool chmtime --get "$path")" ||
+ echo "object list generation failed for $oid"
+ done |
+ sort -k1
+ ) >expect &&
+
+ keep="$(basename "$(ls $packdir/pack-*.pack)")" &&
+ cruft="$(echo $keep | git pack-objects --cruft \
+ --cruft-expiration="$expire" $packdir/pack)" &&
+ test-tool pack-mtimes "pack-$cruft.mtimes" >actual &&
+
+ test_cmp expect actual
+ )
+ '
+
+ test_expect_success "unreachable packed objects are packed (expire $expire)" '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+
+ test_commit packed &&
+ git repack -Ad &&
+ test_commit other &&
+
+ git rev-list --objects --no-object-names packed.. >objects &&
+ keep="$(basename "$(ls $packdir/pack-*.pack)")" &&
+ other="$(git pack-objects --delta-base-offset \
+ $packdir/pack <objects)" &&
+ git prune-packed &&
+
+ test-tool chmtime --get -100 "$packdir/pack-$other.pack" >expect &&
+
+ cruft="$(git pack-objects --cruft --cruft-expiration="$expire" $packdir/pack <<-EOF
+ $keep
+ -pack-$other.pack
+ EOF
+ )" &&
+ test-tool pack-mtimes "pack-$cruft.mtimes" >actual.raw &&
+
+ cut -d" " -f2 <actual.raw | sort -u >actual &&
+
+ test_cmp expect actual
+ )
+ '
+
+ test_expect_success "unreachable cruft objects are repacked (expire $expire)" '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+
+ test_commit packed &&
+ git repack -Ad &&
+ test_commit other &&
+
+ git rev-list --objects --no-object-names packed.. >objects &&
+ keep="$(basename "$(ls $packdir/pack-*.pack)")" &&
+
+ cruft_a="$(echo $keep | git pack-objects --cruft --cruft-expiration="$expire" $packdir/pack)" &&
+ git prune-packed &&
+ cruft_b="$(git pack-objects --cruft --cruft-expiration="$expire" $packdir/pack <<-EOF
+ $keep
+ -pack-$cruft_a.pack
+ EOF
+ )" &&
+
+ test-tool pack-mtimes "pack-$cruft_a.mtimes" >expect.raw &&
+ test-tool pack-mtimes "pack-$cruft_b.mtimes" >actual.raw &&
+
+ sort <expect.raw >expect &&
+ sort <actual.raw >actual &&
+
+ test_cmp expect actual
+ )
+ '
+
+ test_expect_success "multiple cruft packs (expire $expire)" '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+
+ test_commit reachable &&
+ git repack -Ad &&
+ keep="$(basename "$(ls $packdir/pack-*.pack)")" &&
+
+ test_commit cruft &&
+ loose="$objdir/$(test_oid_to_path $(git rev-parse cruft))" &&
+
+ # generate three copies of the cruft object in different
+ # cruft packs, each with a unique mtime:
+ # - one expired (1000 seconds ago)
+ # - two non-expired (one 1000 seconds in the future,
+ # one 1500 seconds in the future)
+ test-tool chmtime =-1000 "$loose" &&
+ git pack-objects --cruft $packdir/pack-A <<-EOF &&
+ $keep
+ EOF
+ test-tool chmtime =+1000 "$loose" &&
+ git pack-objects --cruft $packdir/pack-B <<-EOF &&
+ $keep
+ -$(basename $(ls $packdir/pack-A-*.pack))
+ EOF
+ test-tool chmtime =+1500 "$loose" &&
+ git pack-objects --cruft $packdir/pack-C <<-EOF &&
+ $keep
+ -$(basename $(ls $packdir/pack-A-*.pack))
+ -$(basename $(ls $packdir/pack-B-*.pack))
+ EOF
+
+ # ensure the resulting cruft pack takes the most recent
+ # mtime among all copies
+ cruft="$(git pack-objects --cruft \
+ --cruft-expiration="$expire" \
+ $packdir/pack <<-EOF
+ $keep
+ -$(basename $(ls $packdir/pack-A-*.pack))
+ -$(basename $(ls $packdir/pack-B-*.pack))
+ -$(basename $(ls $packdir/pack-C-*.pack))
+ EOF
+ )" &&
+
+ test-tool pack-mtimes "$(basename $(ls $packdir/pack-C-*.mtimes))" >expect.raw &&
+ test-tool pack-mtimes "pack-$cruft.mtimes" >actual.raw &&
+
+ sort expect.raw >expect &&
+ sort actual.raw >actual &&
+ test_cmp expect actual
+ )
+ '
+
+ test_expect_success "cruft packs tolerate missing trees (expire $expire)" '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+
+ test_commit reachable &&
+ test_commit cruft &&
+
+ tree="$(git rev-parse cruft^{tree})" &&
+
+ git reset --hard reachable &&
+ git tag -d cruft &&
+ git reflog expire --all --expire=all &&
+
+ # remove the unreachable tree, but leave the commit
+ # which has it as its root tree intact
+ rm -fr "$objdir/$(test_oid_to_path "$tree")" &&
+
+ git repack -Ad &&
+ basename $(ls $packdir/pack-*.pack) >in &&
+ git pack-objects --cruft --cruft-expiration="$expire" \
+ $packdir/pack <in
+ )
+ '
+
+ test_expect_success "cruft packs tolerate missing blobs (expire $expire)" '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+
+ test_commit reachable &&
+ test_commit cruft &&
+
+ blob="$(git rev-parse cruft:cruft.t)" &&
+
+ git reset --hard reachable &&
+ git tag -d cruft &&
+ git reflog expire --all --expire=all &&
+
+ # remove the unreachable blob, but leave the commit (and
+ # the root tree of that commit) intact
+ rm -fr "$objdir/$(test_oid_to_path "$blob")" &&
+
+ git repack -Ad &&
+ basename $(ls $packdir/pack-*.pack) >in &&
+ git pack-objects --cruft --cruft-expiration="$expire" \
+ $packdir/pack <in
+ )
+ '
+}
+
+basic_cruft_pack_tests never
+basic_cruft_pack_tests 2.weeks.ago
+
+test_expect_success 'cruft tags rescue tagged objects' '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+
+ test_commit packed &&
+ git repack -Ad &&
+
+ test_commit tagged &&
+ git tag -a annotated -m tag &&
+
+ git rev-list --objects --no-object-names packed.. >objects &&
+ while read oid
+ do
+ test-tool chmtime -1000 \
+ "$objdir/$(test_oid_to_path $oid)" || exit 1
+ done <objects &&
+
+ test-tool chmtime -500 \
+ "$objdir/$(test_oid_to_path $(git rev-parse annotated))" &&
+
+ keep="$(basename "$(ls $packdir/pack-*.pack)")" &&
+ cruft="$(echo $keep | git pack-objects --cruft \
+ --cruft-expiration=750.seconds.ago \
+ $packdir/pack)" &&
+ test-tool pack-mtimes "pack-$cruft.mtimes" >actual.raw &&
+ cut -f1 -d" " <actual.raw | sort >actual &&
+
+ (
+ cat objects &&
+ git rev-parse annotated
+ ) >expect.raw &&
+ sort <expect.raw >expect &&
+
+ test_cmp expect actual &&
+ cat actual
+ )
+'
+
+test_expect_success 'cruft commits rescue parents, trees' '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+
+ test_commit packed &&
+ git repack -Ad &&
+
+ test_commit old &&
+ test_commit new &&
+
+ git rev-list --objects --no-object-names packed..new >objects &&
+ while read object
+ do
+ test-tool chmtime -1000 \
+ "$objdir/$(test_oid_to_path $object)" || exit 1
+ done <objects &&
+ test-tool chmtime +500 "$objdir/$(test_oid_to_path \
+ $(git rev-parse HEAD))" &&
+
+ keep="$(basename "$(ls $packdir/pack-*.pack)")" &&
+ cruft="$(echo $keep | git pack-objects --cruft \
+ --cruft-expiration=750.seconds.ago \
+ $packdir/pack)" &&
+ test-tool pack-mtimes "pack-$cruft.mtimes" >actual.raw &&
+
+ cut -d" " -f1 <actual.raw | sort >actual &&
+ sort <objects >expect &&
+
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'cruft trees rescue sub-trees, blobs' '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+
+ test_commit packed &&
+ git repack -Ad &&
+
+ mkdir -p dir/sub &&
+ echo foo >foo &&
+ echo bar >dir/bar &&
+ echo baz >dir/sub/baz &&
+
+ test_tick &&
+ git add . &&
+ git commit -m "pruned" &&
+
+ test-tool chmtime -1000 "$objdir/$(test_oid_to_path $(git rev-parse HEAD))" &&
+ test-tool chmtime -1000 "$objdir/$(test_oid_to_path $(git rev-parse HEAD^{tree}))" &&
+ test-tool chmtime -1000 "$objdir/$(test_oid_to_path $(git rev-parse HEAD:foo))" &&
+ test-tool chmtime -500 "$objdir/$(test_oid_to_path $(git rev-parse HEAD:dir))" &&
+ test-tool chmtime -1000 "$objdir/$(test_oid_to_path $(git rev-parse HEAD:dir/bar))" &&
+ test-tool chmtime -1000 "$objdir/$(test_oid_to_path $(git rev-parse HEAD:dir/sub))" &&
+ test-tool chmtime -1000 "$objdir/$(test_oid_to_path $(git rev-parse HEAD:dir/sub/baz))" &&
+
+ keep="$(basename "$(ls $packdir/pack-*.pack)")" &&
+ cruft="$(echo $keep | git pack-objects --cruft \
+ --cruft-expiration=750.seconds.ago \
+ $packdir/pack)" &&
+ test-tool pack-mtimes "pack-$cruft.mtimes" >actual.raw &&
+ cut -f1 -d" " <actual.raw | sort >actual &&
+
+ git rev-parse HEAD:dir HEAD:dir/bar HEAD:dir/sub HEAD:dir/sub/baz >expect.raw &&
+ sort <expect.raw >expect &&
+
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'expired objects are pruned' '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+
+ test_commit packed &&
+ git repack -Ad &&
+
+ test_commit pruned &&
+
+ git rev-list --objects --no-object-names packed..pruned >objects &&
+ while read object
+ do
+ test-tool chmtime -1000 \
+ "$objdir/$(test_oid_to_path $object)" || exit 1
+ done <objects &&
+
+ keep="$(basename "$(ls $packdir/pack-*.pack)")" &&
+ cruft="$(echo $keep | git pack-objects --cruft \
+ --cruft-expiration=750.seconds.ago \
+ $packdir/pack)" &&
+
+ test-tool pack-mtimes "pack-$cruft.mtimes" >actual &&
+ test_must_be_empty actual
+ )
+'
+
+test_expect_success 'repack --cruft generates a cruft pack' '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+
+ test_commit reachable &&
+ git branch -M main &&
+ git checkout --orphan other &&
+ test_commit unreachable &&
+
+ git checkout main &&
+ git branch -D other &&
+ git tag -d unreachable &&
+ # objects are not cruft if they are contained in the reflogs
+ git reflog expire --all --expire=all &&
+
+ git rev-list --objects --all --no-object-names >reachable.raw &&
+ git cat-file --batch-all-objects --batch-check="%(objectname)" >objects &&
+ sort <reachable.raw >reachable &&
+ comm -13 reachable objects >unreachable &&
+
+ git repack --cruft -d &&
+
+ cruft=$(basename $(ls $packdir/pack-*.mtimes) .mtimes) &&
+ pack=$(basename $(ls $packdir/pack-*.pack | grep -v $cruft) .pack) &&
+
+ git show-index <$packdir/$pack.idx >actual.raw &&
+ cut -f2 -d" " actual.raw | sort >actual &&
+ test_cmp reachable actual &&
+
+ git show-index <$packdir/$cruft.idx >actual.raw &&
+ cut -f2 -d" " actual.raw | sort >actual &&
+ test_cmp unreachable actual
+ )
+'
+
+test_expect_success 'loose objects mtimes upsert others' '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+
+ test_commit reachable &&
+ git repack -Ad &&
+ git branch -M main &&
+
+ git checkout --orphan other &&
+ test_commit cruft &&
+ # incremental repack, leaving existing objects loose (so
+ # they can be "freshened")
+ git repack &&
+
+ tip="$(git rev-parse cruft)" &&
+ path="$objdir/$(test_oid_to_path "$tip")" &&
+ test-tool chmtime --get +1000 "$path" >expect &&
+
+ git checkout main &&
+ git branch -D other &&
+ git tag -d cruft &&
+ git reflog expire --all --expire=all &&
+
+ git repack --cruft -d &&
+
+ mtimes="$(basename $(ls $packdir/pack-*.mtimes))" &&
+ test-tool pack-mtimes "$mtimes" >actual.raw &&
+ grep "$tip" actual.raw | cut -d" " -f2 >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'expiring cruft objects with git gc' '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+
+ test_commit reachable &&
+ git branch -M main &&
+ git checkout --orphan other &&
+ test_commit unreachable &&
+
+ git checkout main &&
+ git branch -D other &&
+ git tag -d unreachable &&
+ # objects are not cruft if they are contained in the reflogs
+ git reflog expire --all --expire=all &&
+
+ git rev-list --objects --all --no-object-names >reachable.raw &&
+ git cat-file --batch-all-objects --batch-check="%(objectname)" >objects &&
+ sort <reachable.raw >reachable &&
+ comm -13 reachable objects >unreachable &&
+
+ # Write a cruft pack containing all unreachable objects.
+ git gc --cruft --prune="01-01-1980" &&
+
+ mtimes=$(ls .git/objects/pack/pack-*.mtimes) &&
+ test_path_is_file $mtimes &&
+
+ # Prune all unreachable objects from the cruft pack.
+ git gc --cruft --prune=now &&
+
+ git cat-file --batch-all-objects --batch-check="%(objectname)" >objects &&
+
+ comm -23 unreachable objects >removed &&
+ test_cmp unreachable removed &&
+ test_path_is_missing $mtimes
+ )
+'
+
+test_expect_success 'cruft packs are not included in geometric repack' '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+
+ test_commit reachable &&
+ git repack -Ad &&
+ git branch -M main &&
+
+ git checkout --orphan other &&
+ test_commit cruft &&
+ git repack -d &&
+
+ git checkout main &&
+ git branch -D other &&
+ git tag -d cruft &&
+ git reflog expire --all --expire=all &&
+
+ git repack --cruft &&
+
+ find $packdir -type f | sort >before &&
+ git repack --geometric=2 -d &&
+ find $packdir -type f | sort >after &&
+
+ test_cmp before after
+ )
+'
+
+test_expect_success 'repack --geometric collects once-cruft objects' '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+
+ test_commit reachable &&
+ git repack -Ad &&
+ git branch -M main &&
+
+ git checkout --orphan other &&
+ git rm -rf . &&
+ test_commit --no-tag cruft &&
+ cruft="$(git rev-parse HEAD)" &&
+
+ git checkout main &&
+ git branch -D other &&
+ git reflog expire --all --expire=all &&
+
+ # Pack the objects created in the previous step into a cruft
+ # pack. Intentionally leave loose copies of those objects
+ # around so we can pick them up in a subsequent --geometric
+ # reapack.
+ git repack --cruft &&
+
+ # Now make those objects reachable, and ensure that they are
+ # packed into the new pack created via a --geometric repack.
+ git update-ref refs/heads/other $cruft &&
+
+ # Without this object, the set of unpacked objects is exactly
+ # the set of objects already in the cruft pack. Tweak that set
+ # to ensure we do not overwrite the cruft pack entirely.
+ test_commit reachable2 &&
+
+ find $packdir -name "pack-*.idx" | sort >before &&
+ git repack --geometric=2 -d &&
+ find $packdir -name "pack-*.idx" | sort >after &&
+
+ {
+ git rev-list --objects --no-object-names $cruft &&
+ git rev-list --objects --no-object-names reachable..reachable2
+ } >want.raw &&
+ sort want.raw >want &&
+
+ pack=$(comm -13 before after) &&
+ git show-index <$pack >objects.raw &&
+
+ cut -d" " -f2 objects.raw | sort >got &&
+
+ test_cmp want got
+ )
+'
+
+test_expect_success 'cruft repack with no reachable objects' '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+
+ test_commit base &&
+ git repack -ad &&
+
+ base="$(git rev-parse base)" &&
+
+ git for-each-ref --format="delete %(refname)" >in &&
+ git update-ref --stdin <in &&
+ git reflog expire --all --expire=all &&
+ rm -fr .git/index &&
+
+ git repack --cruft -d &&
+
+ git cat-file -t $base
+ )
+'
+
+write_blob () {
+ test-tool genrandom "$@" >in &&
+ git hash-object -w -t blob in
+}
+
+find_pack () {
+ for idx in $(ls $packdir/pack-*.idx)
+ do
+ git show-index <$idx >out &&
+ if grep -q "$1" out
+ then
+ echo $idx
+ fi || return 1
+ done
+}
+
+test_expect_success 'cruft repack with --max-pack-size' '
+ git init max-pack-size &&
+ (
+ cd max-pack-size &&
+ test_commit base &&
+
+ # two cruft objects which exceed the maximum pack size
+ foo=$(write_blob foo 1048576) &&
+ bar=$(write_blob bar 1048576) &&
+ test-tool chmtime --get -1000 \
+ "$objdir/$(test_oid_to_path $foo)" >foo.mtime &&
+ test-tool chmtime --get -2000 \
+ "$objdir/$(test_oid_to_path $bar)" >bar.mtime &&
+ git repack --cruft --max-pack-size=1M &&
+ find $packdir -name "*.mtimes" >cruft &&
+ test_line_count = 2 cruft &&
+
+ foo_mtimes="$(basename $(find_pack $foo) .idx).mtimes" &&
+ bar_mtimes="$(basename $(find_pack $bar) .idx).mtimes" &&
+ test-tool pack-mtimes $foo_mtimes >foo.actual &&
+ test-tool pack-mtimes $bar_mtimes >bar.actual &&
+
+ echo "$foo $(cat foo.mtime)" >foo.expect &&
+ echo "$bar $(cat bar.mtime)" >bar.expect &&
+
+ test_cmp foo.expect foo.actual &&
+ test_cmp bar.expect bar.actual &&
+ test "$foo_mtimes" != "$bar_mtimes"
+ )
+'
+
+test_expect_success 'cruft repack with pack.packSizeLimit' '
+ (
+ cd max-pack-size &&
+ # repack everything back together to remove the existing cruft
+ # pack (but to keep its objects)
+ git repack -adk &&
+ git -c pack.packSizeLimit=1M repack --cruft &&
+ # ensure the same post condition is met when --max-pack-size
+ # would otherwise be inferred from the configuration
+ find $packdir -name "*.mtimes" >cruft &&
+ test_line_count = 2 cruft &&
+ for pack in $(cat cruft)
+ do
+ test-tool pack-mtimes "$(basename $pack)" >objects &&
+ test_line_count = 1 objects || return 1
+ done
+ )
+'
+
+test_expect_success 'cruft repack respects repack.cruftWindow' '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+
+ test_commit base &&
+
+ GIT_TRACE2_EVENT=$(pwd)/event.trace \
+ git -c pack.window=1 -c repack.cruftWindow=2 repack \
+ --cruft --window=3 &&
+
+ grep "pack-objects.*--window=2.*--cruft" event.trace
+ )
+'
+
+test_expect_success 'cruft repack respects --window by default' '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+
+ test_commit base &&
+
+ GIT_TRACE2_EVENT=$(pwd)/event.trace \
+ git -c pack.window=2 repack --cruft --window=3 &&
+
+ grep "pack-objects.*--window=3.*--cruft" event.trace
+ )
+'
+
+test_expect_success 'cruft repack respects --quiet' '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+
+ test_commit base &&
+ GIT_PROGRESS_DELAY=0 git repack --cruft --quiet 2>err &&
+ test_must_be_empty err
+ )
+'
+
+test_expect_success 'cruft --local drops unreachable objects' '
+ git init alternate &&
+ git init repo &&
+ test_when_finished "rm -fr alternate repo" &&
+
+ test_commit -C alternate base &&
+ # Pack all objects in alterate so that the cruft repack in "repo" sees
+ # the object it dropped due to `--local` as packed. Otherwise this
+ # object would not appear packed anywhere (since it is not packed in
+ # alternate and likewise not part of the cruft pack in the other repo
+ # because of `--local`).
+ git -C alternate repack -ad &&
+
+ (
+ cd repo &&
+
+ object="$(git -C ../alternate rev-parse HEAD:base.t)" &&
+ git -C ../alternate cat-file -p $object >contents &&
+
+ # Write some reachable objects and two unreachable ones: one
+ # that the alternate has and another that is unique.
+ test_commit other &&
+ git hash-object -w -t blob contents &&
+ cruft="$(echo cruft | git hash-object -w -t blob --stdin)" &&
+
+ ( cd ../alternate/.git/objects && pwd ) \
+ >.git/objects/info/alternates &&
+
+ test_path_is_file $objdir/$(test_oid_to_path $cruft) &&
+ test_path_is_file $objdir/$(test_oid_to_path $object) &&
+
+ git repack -d --cruft --local &&
+
+ test-tool pack-mtimes "$(basename $(ls $packdir/pack-*.mtimes))" \
+ >objects &&
+ ! grep $object objects &&
+ grep $cruft objects
+ )
+'
+
+test_expect_success 'MIDX bitmaps tolerate reachable cruft objects' '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+
+ test_commit reachable &&
+ test_commit cruft &&
+ unreachable="$(git rev-parse cruft)" &&
+
+ git reset --hard $unreachable^ &&
+ git tag -d cruft &&
+ git reflog expire --all --expire=all &&
+
+ git repack --cruft -d &&
+
+ # resurrect the unreachable object via a new commit. the
+ # new commit will get selected for a bitmap, but be
+ # missing one of its parents from the selected packs.
+ git reset --hard $unreachable &&
+ test_commit resurrect &&
+
+ git repack --write-midx --write-bitmap-index --geometric=2 -d
+ )
+'
+
+test_expect_success 'cruft objects are freshend via loose' '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+
+ echo "cruft" >contents &&
+ blob="$(git hash-object -w -t blob contents)" &&
+ loose="$objdir/$(test_oid_to_path $blob)" &&
+
+ test_commit base &&
+
+ git repack --cruft -d &&
+
+ test_path_is_missing "$loose" &&
+ test-tool pack-mtimes "$(basename "$(ls $packdir/pack-*.mtimes)")" >cruft &&
+ grep "$blob" cruft &&
+
+ # write the same object again
+ git hash-object -w -t blob contents &&
+
+ test_path_is_file "$loose"
+ )
+'
+
+test_expect_success 'gc.recentObjectsHook' '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+
+ # Create a handful of objects.
+ #
+ # - one reachable commit, "base", designated for the reachable
+ # pack
+ # - one unreachable commit, "cruft.discard", which is marked
+ # for deletion
+ # - one unreachable commit, "cruft.old", which would be marked
+ # for deletion, but is rescued as an extra cruft tip
+ # - one unreachable commit, "cruft.new", which is not marked
+ # for deletion
+ test_commit base &&
+ git branch -M main &&
+
+ git checkout --orphan discard &&
+ git rm -fr . &&
+ test_commit --no-tag cruft.discard &&
+
+ git checkout --orphan old &&
+ git rm -fr . &&
+ test_commit --no-tag cruft.old &&
+ cruft_old="$(git rev-parse HEAD)" &&
+
+ git checkout --orphan new &&
+ git rm -fr . &&
+ test_commit --no-tag cruft.new &&
+ cruft_new="$(git rev-parse HEAD)" &&
+
+ git checkout main &&
+ git branch -D discard old new &&
+ git reflog expire --all --expire=all &&
+
+ # mark cruft.old with an mtime that is many minutes
+ # older than the expiration period, and mark cruft.new
+ # with an mtime that is in the future (and thus not
+ # eligible for pruning).
+ test-tool chmtime -2000 "$objdir/$(test_oid_to_path $cruft_old)" &&
+ test-tool chmtime +1000 "$objdir/$(test_oid_to_path $cruft_new)" &&
+
+ # Write the list of cruft objects we expect to
+ # accumulate, which is comprised of everything reachable
+ # from cruft.old and cruft.new, but not cruft.discard.
+ git rev-list --objects --no-object-names \
+ $cruft_old $cruft_new >cruft.raw &&
+ sort cruft.raw >cruft.expect &&
+
+ # Write the script to list extra tips, which are limited
+ # to cruft.old, in this case.
+ write_script extra-tips <<-EOF &&
+ echo $cruft_old
+ EOF
+ git config gc.recentObjectsHook ./extra-tips &&
+
+ git repack --cruft --cruft-expiration=now -d &&
+
+ mtimes="$(ls .git/objects/pack/pack-*.mtimes)" &&
+ git show-index <${mtimes%.mtimes}.idx >cruft &&
+ cut -d" " -f2 cruft | sort >cruft.actual &&
+ test_cmp cruft.expect cruft.actual &&
+
+ # Ensure that the "old" objects are removed after
+ # dropping the gc.recentObjectsHook hook.
+ git config --unset gc.recentObjectsHook &&
+ git repack --cruft --cruft-expiration=now -d &&
+
+ mtimes="$(ls .git/objects/pack/pack-*.mtimes)" &&
+ git show-index <${mtimes%.mtimes}.idx >cruft &&
+ cut -d" " -f2 cruft | sort >cruft.actual &&
+
+ git rev-list --objects --no-object-names $cruft_new >cruft.raw &&
+ cp cruft.expect cruft.old &&
+ sort cruft.raw >cruft.expect &&
+ test_cmp cruft.expect cruft.actual &&
+
+ # ensure objects which are no longer in the cruft pack were
+ # removed from the repository
+ for object in $(comm -13 cruft.expect cruft.old)
+ do
+ test_must_fail git cat-file -t $object || return 1
+ done
+ )
+'
+
+test_expect_success 'multi-valued gc.recentObjectsHook' '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+
+ test_commit base &&
+ git branch -M main &&
+
+ git checkout --orphan cruft.a &&
+ git rm -fr . &&
+ test_commit --no-tag cruft.a &&
+ cruft_a="$(git rev-parse HEAD)" &&
+
+ git checkout --orphan cruft.b &&
+ git rm -fr . &&
+ test_commit --no-tag cruft.b &&
+ cruft_b="$(git rev-parse HEAD)" &&
+
+ git checkout main &&
+ git branch -D cruft.a cruft.b &&
+ git reflog expire --all --expire=all &&
+
+ echo "echo $cruft_a" | write_script extra-tips.a &&
+ echo "echo $cruft_b" | write_script extra-tips.b &&
+ echo "false" | write_script extra-tips.c &&
+
+ git rev-list --objects --no-object-names $cruft_a $cruft_b \
+ >cruft.raw &&
+ sort cruft.raw >cruft.expect &&
+
+ # ensure that each extra cruft tip is saved by its
+ # respective hook
+ git config --add gc.recentObjectsHook ./extra-tips.a &&
+ git config --add gc.recentObjectsHook ./extra-tips.b &&
+ git repack --cruft --cruft-expiration=now -d &&
+
+ mtimes="$(ls .git/objects/pack/pack-*.mtimes)" &&
+ git show-index <${mtimes%.mtimes}.idx >cruft &&
+ cut -d" " -f2 cruft | sort >cruft.actual &&
+ test_cmp cruft.expect cruft.actual &&
+
+ # ensure that a dirty exit halts cruft pack generation
+ git config --add gc.recentObjectsHook ./extra-tips.c &&
+ test_must_fail git repack --cruft --cruft-expiration=now -d 2>err &&
+ grep "unable to enumerate additional recent objects" err &&
+
+ # and that the existing cruft pack is left alone
+ test_path_is_file "$mtimes"
+ )
+'
+
+test_expect_success 'additional cruft blobs via gc.recentObjectsHook' '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+
+ test_commit base &&
+
+ blob=$(echo "unreachable" | git hash-object -w --stdin) &&
+
+ # mark the unreachable blob we wrote above as having
+ # aged out of the retention period
+ test-tool chmtime -2000 "$objdir/$(test_oid_to_path $blob)" &&
+
+ # Write the script to list extra tips, which is just the
+ # extra blob as above.
+ write_script extra-tips <<-EOF &&
+ echo $blob
+ EOF
+ git config gc.recentObjectsHook ./extra-tips &&
+
+ git repack --cruft --cruft-expiration=now -d &&
+
+ mtimes="$(ls .git/objects/pack/pack-*.mtimes)" &&
+ git show-index <${mtimes%.mtimes}.idx >cruft &&
+ cut -d" " -f2 cruft >actual &&
+ echo $blob >expect &&
+ test_cmp expect actual
+ )
+'
+
+test_done
diff --git a/t/t5330-no-lazy-fetch-with-commit-graph.sh b/t/t5330-no-lazy-fetch-with-commit-graph.sh
new file mode 100755
index 0000000..5eb28f0
--- /dev/null
+++ b/t/t5330-no-lazy-fetch-with-commit-graph.sh
@@ -0,0 +1,48 @@
+#!/bin/sh
+
+test_description='test for no lazy fetch with the commit-graph'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup: prepare a repository with a commit' '
+ git init with-commit &&
+ test_commit -C with-commit the-commit &&
+ oid=$(git -C with-commit rev-parse HEAD)
+'
+
+test_expect_success 'setup: prepare a repository with commit-graph contains the commit' '
+ git init with-commit-graph &&
+ echo "$(pwd)/with-commit/.git/objects" \
+ >with-commit-graph/.git/objects/info/alternates &&
+ # create a ref that points to the commit in alternates
+ git -C with-commit-graph update-ref refs/ref_to_the_commit "$oid" &&
+ # prepare some other objects to commit-graph
+ test_commit -C with-commit-graph something &&
+ git -c gc.writeCommitGraph=true -C with-commit-graph gc &&
+ test_path_is_file with-commit-graph/.git/objects/info/commit-graph
+'
+
+test_expect_success 'setup: change the alternates to what without the commit' '
+ git init --bare without-commit &&
+ git -C with-commit-graph cat-file -e $oid &&
+ echo "$(pwd)/without-commit/objects" \
+ >with-commit-graph/.git/objects/info/alternates &&
+ test_must_fail git -C with-commit-graph cat-file -e $oid
+'
+
+test_expect_success 'fetch any commit from promisor with the usage of the commit graph' '
+ # setup promisor and prepare any commit to fetch
+ git -C with-commit-graph remote add origin "$(pwd)/with-commit" &&
+ git -C with-commit-graph config remote.origin.promisor true &&
+ git -C with-commit-graph config remote.origin.partialclonefilter blob:none &&
+ test_commit -C with-commit any-commit &&
+ anycommit=$(git -C with-commit rev-parse HEAD) &&
+ GIT_TRACE="$(pwd)/trace.txt" \
+ git -C with-commit-graph fetch origin $anycommit 2>err &&
+ ! grep "fatal: promisor-remote: unable to fork off fetch subprocess" err &&
+ grep "git fetch origin" trace.txt >actual &&
+ test_line_count = 1 actual
+'
+
+test_done
diff --git a/t/t5331-pack-objects-stdin.sh b/t/t5331-pack-objects-stdin.sh
new file mode 100755
index 0000000..2dcf1ee
--- /dev/null
+++ b/t/t5331-pack-objects-stdin.sh
@@ -0,0 +1,240 @@
+#!/bin/sh
+
+test_description='pack-objects --stdin'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+packed_objects () {
+ git show-index <"$1" >tmp-object-list &&
+ cut -d' ' -f2 tmp-object-list | sort &&
+ rm tmp-object-list
+ }
+
+test_expect_success 'setup for --stdin-packs tests' '
+ git init stdin-packs &&
+ (
+ cd stdin-packs &&
+
+ test_commit A &&
+ test_commit B &&
+ test_commit C &&
+
+ for id in A B C
+ do
+ git pack-objects .git/objects/pack/pack-$id \
+ --incremental --revs <<-EOF || exit 1
+ refs/tags/$id
+ EOF
+ done &&
+
+ ls -la .git/objects/pack
+ )
+'
+
+test_expect_success '--stdin-packs with excluded packs' '
+ (
+ cd stdin-packs &&
+
+ PACK_A="$(basename .git/objects/pack/pack-A-*.pack)" &&
+ PACK_B="$(basename .git/objects/pack/pack-B-*.pack)" &&
+ PACK_C="$(basename .git/objects/pack/pack-C-*.pack)" &&
+
+ git pack-objects test --stdin-packs <<-EOF &&
+ $PACK_A
+ ^$PACK_B
+ $PACK_C
+ EOF
+
+ (
+ git show-index <$(ls .git/objects/pack/pack-A-*.idx) &&
+ git show-index <$(ls .git/objects/pack/pack-C-*.idx)
+ ) >expect.raw &&
+ git show-index <$(ls test-*.idx) >actual.raw &&
+
+ cut -d" " -f2 <expect.raw | sort >expect &&
+ cut -d" " -f2 <actual.raw | sort >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success '--stdin-packs is incompatible with --filter' '
+ (
+ cd stdin-packs &&
+ test_must_fail git pack-objects --stdin-packs --stdout \
+ --filter=blob:none </dev/null 2>err &&
+ test_grep "cannot use --filter with --stdin-packs" err
+ )
+'
+
+test_expect_success '--stdin-packs is incompatible with --revs' '
+ (
+ cd stdin-packs &&
+ test_must_fail git pack-objects --stdin-packs --revs out \
+ </dev/null 2>err &&
+ test_grep "cannot use internal rev list with --stdin-packs" err
+ )
+'
+
+test_expect_success '--stdin-packs with loose objects' '
+ (
+ cd stdin-packs &&
+
+ PACK_A="$(basename .git/objects/pack/pack-A-*.pack)" &&
+ PACK_B="$(basename .git/objects/pack/pack-B-*.pack)" &&
+ PACK_C="$(basename .git/objects/pack/pack-C-*.pack)" &&
+
+ test_commit D && # loose
+
+ git pack-objects test2 --stdin-packs --unpacked <<-EOF &&
+ $PACK_A
+ ^$PACK_B
+ $PACK_C
+ EOF
+
+ (
+ git show-index <$(ls .git/objects/pack/pack-A-*.idx) &&
+ git show-index <$(ls .git/objects/pack/pack-C-*.idx) &&
+ git rev-list --objects --no-object-names \
+ refs/tags/C..refs/tags/D
+
+ ) >expect.raw &&
+ ls -la . &&
+ git show-index <$(ls test2-*.idx) >actual.raw &&
+
+ cut -d" " -f2 <expect.raw | sort >expect &&
+ cut -d" " -f2 <actual.raw | sort >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success '--stdin-packs with broken links' '
+ (
+ cd stdin-packs &&
+
+ # make an unreachable object with a bogus parent
+ git cat-file -p HEAD >commit &&
+ sed "s/$(git rev-parse HEAD^)/$(test_oid zero)/" <commit |
+ git hash-object -w -t commit --stdin >in &&
+
+ git pack-objects .git/objects/pack/pack-D <in &&
+
+ PACK_A="$(basename .git/objects/pack/pack-A-*.pack)" &&
+ PACK_B="$(basename .git/objects/pack/pack-B-*.pack)" &&
+ PACK_C="$(basename .git/objects/pack/pack-C-*.pack)" &&
+ PACK_D="$(basename .git/objects/pack/pack-D-*.pack)" &&
+
+ git pack-objects test3 --stdin-packs --unpacked <<-EOF &&
+ $PACK_A
+ ^$PACK_B
+ $PACK_C
+ $PACK_D
+ EOF
+
+ (
+ git show-index <$(ls .git/objects/pack/pack-A-*.idx) &&
+ git show-index <$(ls .git/objects/pack/pack-C-*.idx) &&
+ git show-index <$(ls .git/objects/pack/pack-D-*.idx) &&
+ git rev-list --objects --no-object-names \
+ refs/tags/C..refs/tags/D
+ ) >expect.raw &&
+ git show-index <$(ls test3-*.idx) >actual.raw &&
+
+ cut -d" " -f2 <expect.raw | sort >expect &&
+ cut -d" " -f2 <actual.raw | sort >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'pack-objects --stdin with duplicate packfile' '
+ test_when_finished "rm -fr repo" &&
+
+ git init repo &&
+ (
+ cd repo &&
+ test_commit "commit" &&
+ git repack -ad &&
+
+ {
+ basename .git/objects/pack/pack-*.pack &&
+ basename .git/objects/pack/pack-*.pack
+ } >packfiles &&
+
+ git pack-objects --stdin-packs generated-pack <packfiles &&
+ packed_objects .git/objects/pack/pack-*.idx >expect &&
+ packed_objects generated-pack-*.idx >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'pack-objects --stdin with same packfile excluded and included' '
+ test_when_finished "rm -fr repo" &&
+
+ git init repo &&
+ (
+ cd repo &&
+ test_commit "commit" &&
+ git repack -ad &&
+
+ {
+ basename .git/objects/pack/pack-*.pack &&
+ printf "^%s\n" "$(basename .git/objects/pack/pack-*.pack)"
+ } >packfiles &&
+
+ git pack-objects --stdin-packs generated-pack <packfiles &&
+ packed_objects generated-pack-*.idx >packed-objects &&
+ test_must_be_empty packed-objects
+ )
+'
+
+test_expect_success 'pack-objects --stdin with packfiles from alternate object database' '
+ test_when_finished "rm -fr shared member" &&
+
+ # Set up a shared repository with a single packfile.
+ git init shared &&
+ test_commit -C shared "shared-objects" &&
+ git -C shared repack -ad &&
+ basename shared/.git/objects/pack/pack-*.pack >packfile &&
+
+ # Set up a repository that is connected to the shared repository. This
+ # repository has no objects on its own, but we still expect to be able
+ # to pack objects from its alternate.
+ git clone --shared shared member &&
+ git -C member pack-objects --stdin-packs generated-pack <packfile &&
+ test_cmp shared/.git/objects/pack/pack-*.pack member/generated-pack-*.pack
+'
+
+test_expect_success 'pack-objects --stdin with packfiles from main and alternate object database' '
+ test_when_finished "rm -fr shared member" &&
+
+ # Set up a shared repository with a single packfile.
+ git init shared &&
+ test_commit -C shared "shared-commit" &&
+ git -C shared repack -ad &&
+
+ # Set up a repository that is connected to the shared repository. This
+ # repository has a second packfile so that we can verify that it is
+ # possible to write packs that include packfiles from different object
+ # databases.
+ git clone --shared shared member &&
+ test_commit -C member "local-commit" &&
+ git -C member repack -dl &&
+
+ {
+ basename shared/.git/objects/pack/pack-*.pack &&
+ basename member/.git/objects/pack/pack-*.pack
+ } >packfiles &&
+
+ {
+ packed_objects shared/.git/objects/pack/pack-*.idx &&
+ packed_objects member/.git/objects/pack/pack-*.idx
+ } | sort >expected-objects &&
+
+ git -C member pack-objects --stdin-packs generated-pack <packfiles &&
+ packed_objects member/generated-pack-*.idx >actual-objects &&
+ test_cmp expected-objects actual-objects
+'
+
+test_done
diff --git a/t/t5351-unpack-large-objects.sh b/t/t5351-unpack-large-objects.sh
new file mode 100755
index 0000000..43cbcd5
--- /dev/null
+++ b/t/t5351-unpack-large-objects.sh
@@ -0,0 +1,103 @@
+#!/bin/sh
+#
+# Copyright (c) 2022 Han Xin
+#
+
+test_description='git unpack-objects with large objects'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+prepare_dest () {
+ test_when_finished "rm -rf dest.git" &&
+ git init --bare dest.git &&
+ git -C dest.git config core.bigFileThreshold "$1"
+}
+
+test_expect_success "create large objects (1.5 MB) and PACK" '
+ test-tool genrandom foo 1500000 >big-blob &&
+ test_commit --append foo big-blob &&
+ test-tool genrandom bar 1500000 >big-blob &&
+ test_commit --append bar big-blob &&
+ PACK=$(echo HEAD | git pack-objects --revs pack) &&
+ git verify-pack -v pack-$PACK.pack >out &&
+ sed -n -e "s/^\([0-9a-f][0-9a-f]*\).*\(commit\|tree\|blob\).*/\1/p" \
+ <out >obj-list
+'
+
+test_expect_success 'set memory limitation to 1MB' '
+ GIT_ALLOC_LIMIT=1m &&
+ export GIT_ALLOC_LIMIT
+'
+
+test_expect_success 'unpack-objects failed under memory limitation' '
+ prepare_dest 2m &&
+ test_must_fail git -C dest.git unpack-objects <pack-$PACK.pack 2>err &&
+ grep "fatal: attempting to allocate" err
+'
+
+test_expect_success 'unpack-objects works with memory limitation in dry-run mode' '
+ prepare_dest 2m &&
+ git -C dest.git unpack-objects -n <pack-$PACK.pack &&
+ test_stdout_line_count = 0 find dest.git/objects -type f &&
+ test_dir_is_empty dest.git/objects/pack
+'
+
+test_expect_success 'unpack big object in stream' '
+ prepare_dest 1m &&
+ git -C dest.git unpack-objects <pack-$PACK.pack &&
+ test_dir_is_empty dest.git/objects/pack
+'
+
+check_fsync_events () {
+ local trace="$1" &&
+ shift &&
+
+ cat >expect &&
+ sed -n \
+ -e '/^{"event":"counter",.*"category":"fsync",/ {
+ s/.*"category":"fsync",//;
+ s/}$//;
+ p;
+ }' \
+ <"$trace" >actual &&
+ test_cmp expect actual
+}
+
+BATCH_CONFIGURATION='-c core.fsync=loose-object -c core.fsyncmethod=batch'
+
+test_expect_success 'unpack big object in stream (core.fsyncmethod=batch)' '
+ prepare_dest 1m &&
+ GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
+ GIT_TEST_FSYNC=true \
+ git -C dest.git $BATCH_CONFIGURATION unpack-objects <pack-$PACK.pack &&
+ if grep "core.fsyncMethod = batch is unsupported" trace2.txt
+ then
+ flush_count=7
+ else
+ flush_count=1
+ fi &&
+ check_fsync_events trace2.txt <<-EOF &&
+ "name":"writeout-only","count":6
+ "name":"hardware-flush","count":$flush_count
+ EOF
+
+ test_dir_is_empty dest.git/objects/pack &&
+ git -C dest.git cat-file --batch-check="%(objectname)" <obj-list >current &&
+ cmp obj-list current
+'
+
+test_expect_success 'do not unpack existing large objects' '
+ prepare_dest 1m &&
+ git -C dest.git index-pack --stdin <pack-$PACK.pack &&
+ git -C dest.git unpack-objects <pack-$PACK.pack &&
+
+ # The destination came up with the exact same pack...
+ DEST_PACK=$(echo dest.git/objects/pack/pack-*.pack) &&
+ cmp pack-$PACK.pack $DEST_PACK &&
+
+ # ...and wrote no loose objects
+ test_stdout_line_count = 0 find dest.git/objects -type f ! -name "pack-*"
+'
+
+test_done
diff --git a/t/t5400-send-pack.sh b/t/t5400-send-pack.sh
new file mode 100755
index 0000000..3f81f16
--- /dev/null
+++ b/t/t5400-send-pack.sh
@@ -0,0 +1,303 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+test_description='See why rewinding head breaks send-pack
+
+'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+cnt=64
+test_expect_success setup '
+ test_tick &&
+ mkdir mozart mozart/is &&
+ echo "Commit #0" >mozart/is/pink &&
+ git update-index --add mozart/is/pink &&
+ tree=$(git write-tree) &&
+ commit=$(echo "Commit #0" | git commit-tree $tree) &&
+ zero=$commit &&
+ parent=$zero &&
+ i=0 &&
+ while test $i -le $cnt
+ do
+ i=$(($i+1)) &&
+ test_tick &&
+ echo "Commit #$i" >mozart/is/pink &&
+ git update-index --add mozart/is/pink &&
+ tree=$(git write-tree) &&
+ commit=$(echo "Commit #$i" |
+ git commit-tree $tree -p $parent) &&
+ git update-ref refs/tags/commit$i $commit &&
+ parent=$commit || return 1
+ done &&
+ git update-ref HEAD "$commit" &&
+ git clone ./. victim &&
+ ( cd victim && git config receive.denyCurrentBranch warn && git log ) &&
+ git update-ref HEAD "$zero" &&
+ parent=$zero &&
+ i=0 &&
+ while test $i -le $cnt
+ do
+ i=$(($i+1)) &&
+ test_tick &&
+ echo "Rebase #$i" >mozart/is/pink &&
+ git update-index --add mozart/is/pink &&
+ tree=$(git write-tree) &&
+ commit=$(echo "Rebase #$i" | git commit-tree $tree -p $parent) &&
+ git update-ref refs/tags/rebase$i $commit &&
+ parent=$commit || return 1
+ done &&
+ git update-ref HEAD "$commit" &&
+ echo Rebase &&
+ git log'
+
+test_expect_success 'pack the source repository' '
+ git repack -a -d &&
+ git prune
+'
+
+test_expect_success 'pack the destination repository' '
+ (
+ cd victim &&
+ git repack -a -d &&
+ git prune
+ )
+'
+
+test_expect_success 'refuse pushing rewound head without --force' '
+ pushed_head=$(git rev-parse --verify main) &&
+ victim_orig=$(cd victim && git rev-parse --verify main) &&
+ test_must_fail git send-pack ./victim main &&
+ victim_head=$(cd victim && git rev-parse --verify main) &&
+ test "$victim_head" = "$victim_orig" &&
+ # this should update
+ git send-pack --force ./victim main &&
+ victim_head=$(cd victim && git rev-parse --verify main) &&
+ test "$victim_head" = "$pushed_head"
+'
+
+test_expect_success 'push can be used to delete a ref' '
+ ( cd victim && git branch extra main ) &&
+ git send-pack ./victim :extra main &&
+ ( cd victim &&
+ test_must_fail git rev-parse --verify extra )
+'
+
+test_expect_success 'refuse deleting push with denyDeletes' '
+ (
+ cd victim &&
+ test_might_fail git branch -D extra &&
+ git config receive.denyDeletes true &&
+ git branch extra main
+ ) &&
+ test_must_fail git send-pack ./victim :extra main
+'
+
+test_expect_success 'cannot override denyDeletes with git -c send-pack' '
+ (
+ cd victim &&
+ test_might_fail git branch -D extra &&
+ git config receive.denyDeletes true &&
+ git branch extra main
+ ) &&
+ test_must_fail git -c receive.denyDeletes=false \
+ send-pack ./victim :extra main
+'
+
+test_expect_success 'override denyDeletes with git -c receive-pack' '
+ (
+ cd victim &&
+ test_might_fail git branch -D extra &&
+ git config receive.denyDeletes true &&
+ git branch extra main
+ ) &&
+ git send-pack \
+ --receive-pack="git -c receive.denyDeletes=false receive-pack" \
+ ./victim :extra main
+'
+
+test_expect_success 'denyNonFastforwards trumps --force' '
+ (
+ cd victim &&
+ test_might_fail git branch -D extra &&
+ git config receive.denyNonFastforwards true
+ ) &&
+ victim_orig=$(cd victim && git rev-parse --verify main) &&
+ test_must_fail git send-pack --force ./victim main^:main &&
+ victim_head=$(cd victim && git rev-parse --verify main) &&
+ test "$victim_orig" = "$victim_head"
+'
+
+test_expect_success 'send-pack --all sends all branches' '
+ # make sure we have at least 2 branches with different
+ # values, just to be thorough
+ git branch other-branch HEAD^ &&
+
+ git init --bare all.git &&
+ git send-pack --all all.git &&
+ git for-each-ref refs/heads >expect &&
+ git -C all.git for-each-ref refs/heads >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'push --all excludes remote-tracking hierarchy' '
+ mkdir parent &&
+ (
+ cd parent &&
+ git init && : >file && git add file && git commit -m add
+ ) &&
+ git clone parent child &&
+ (
+ cd child && git push --all
+ ) &&
+ (
+ cd parent &&
+ test -z "$(git for-each-ref refs/remotes/origin)"
+ )
+'
+
+test_expect_success 'receive-pack runs auto-gc in remote repo' '
+ rm -rf parent child &&
+ git init parent &&
+ (
+ # Setup a repo with 2 packs
+ cd parent &&
+ echo "Some text" >file.txt &&
+ git add . &&
+ git commit -m "Initial commit" &&
+ git repack -adl &&
+ echo "Some more text" >>file.txt &&
+ git commit -a -m "Second commit" &&
+ git repack
+ ) &&
+ cp -R parent child &&
+ (
+ # Set the child to auto-pack if more than one pack exists
+ cd child &&
+ git config gc.autopacklimit 1 &&
+ git config gc.autodetach false &&
+ git branch test_auto_gc &&
+ # And create a file that follows the temporary object naming
+ # convention for the auto-gc to remove
+ : >.git/objects/tmp_test_object &&
+ test-tool chmtime =-1209601 .git/objects/tmp_test_object
+ ) &&
+ (
+ cd parent &&
+ echo "Even more text" >>file.txt &&
+ git commit -a -m "Third commit" &&
+ git send-pack ../child HEAD:refs/heads/test_auto_gc
+ ) &&
+ test ! -e child/.git/objects/tmp_test_object
+'
+
+rewound_push_setup() {
+ rm -rf parent child &&
+ mkdir parent &&
+ (
+ cd parent &&
+ git init &&
+ echo one >file && git add file && git commit -m one &&
+ git config receive.denyCurrentBranch warn &&
+ echo two >file && git commit -a -m two
+ ) &&
+ git clone parent child &&
+ (
+ cd child && git reset --hard HEAD^
+ )
+}
+
+test_expect_success 'pushing explicit refspecs respects forcing' '
+ rewound_push_setup &&
+ parent_orig=$(cd parent && git rev-parse --verify main) &&
+ (
+ cd child &&
+ test_must_fail git send-pack ../parent \
+ refs/heads/main:refs/heads/main
+ ) &&
+ parent_head=$(cd parent && git rev-parse --verify main) &&
+ test "$parent_orig" = "$parent_head" &&
+ (
+ cd child &&
+ git send-pack ../parent \
+ +refs/heads/main:refs/heads/main
+ ) &&
+ parent_head=$(cd parent && git rev-parse --verify main) &&
+ child_head=$(cd child && git rev-parse --verify main) &&
+ test "$parent_head" = "$child_head"
+'
+
+test_expect_success 'pushing wildcard refspecs respects forcing' '
+ rewound_push_setup &&
+ parent_orig=$(cd parent && git rev-parse --verify main) &&
+ (
+ cd child &&
+ test_must_fail git send-pack ../parent \
+ "refs/heads/*:refs/heads/*"
+ ) &&
+ parent_head=$(cd parent && git rev-parse --verify main) &&
+ test "$parent_orig" = "$parent_head" &&
+ (
+ cd child &&
+ git send-pack ../parent \
+ "+refs/heads/*:refs/heads/*"
+ ) &&
+ parent_head=$(cd parent && git rev-parse --verify main) &&
+ child_head=$(cd child && git rev-parse --verify main) &&
+ test "$parent_head" = "$child_head"
+'
+
+test_expect_success 'deny pushing to delete current branch' '
+ rewound_push_setup &&
+ (
+ cd child &&
+ test_must_fail git send-pack ../parent :refs/heads/main 2>errs
+ )
+'
+
+extract_ref_advertisement () {
+ perl -lne '
+ # \\ is there to skip capabilities after \0
+ /push< ([^\\]+)/ or next;
+ exit 0 if $1 eq "0000";
+ print $1;
+ '
+}
+
+test_expect_success 'receive-pack de-dupes .have lines' '
+ git init shared &&
+ git -C shared commit --allow-empty -m both &&
+ git clone -s shared fork &&
+ (
+ cd shared &&
+ git checkout -b only-shared &&
+ git commit --allow-empty -m only-shared &&
+ git update-ref refs/heads/foo HEAD
+ ) &&
+
+ # Notable things in this expectation:
+ # - local refs are not de-duped
+ # - .have does not duplicate locals
+ # - .have does not duplicate itself
+ local=$(git -C fork rev-parse HEAD) &&
+ shared=$(git -C shared rev-parse only-shared) &&
+ cat >expect <<-EOF &&
+ $local refs/heads/main
+ $local refs/remotes/origin/HEAD
+ $local refs/remotes/origin/main
+ $shared .have
+ EOF
+
+ GIT_TRACE_PACKET=$(pwd)/trace GIT_TEST_PROTOCOL_VERSION=0 \
+ git push \
+ --receive-pack="unset GIT_TRACE_PACKET; git-receive-pack" \
+ fork HEAD:foo &&
+ extract_ref_advertisement <trace >refs &&
+ test_cmp expect refs
+'
+
+test_done
diff --git a/t/t5401-update-hooks.sh b/t/t5401-update-hooks.sh
new file mode 100755
index 0000000..001b7a1
--- /dev/null
+++ b/t/t5401-update-hooks.sh
@@ -0,0 +1,143 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Shawn O. Pearce
+#
+
+test_description='Test the update hook infrastructure.'
+. ./test-lib.sh
+
+test_expect_success setup '
+ echo This is a test. >a &&
+ git update-index --add a &&
+ tree0=$(git write-tree) &&
+ commit0=$(echo setup | git commit-tree $tree0) &&
+ echo We hope it works. >a &&
+ git update-index a &&
+ tree1=$(git write-tree) &&
+ commit1=$(echo modify | git commit-tree $tree1 -p $commit0) &&
+ git update-ref refs/heads/main $commit0 &&
+ git update-ref refs/heads/tofail $commit1 &&
+ git clone --bare ./. victim.git &&
+ GIT_DIR=victim.git git update-ref refs/heads/tofail $commit1 &&
+ git update-ref refs/heads/main $commit1 &&
+ git update-ref refs/heads/tofail $commit0 &&
+
+ test_hook --setup -C victim.git pre-receive <<-\EOF &&
+ printf %s "$@" >>$GIT_DIR/pre-receive.args
+ cat - >$GIT_DIR/pre-receive.stdin
+ echo STDOUT pre-receive
+ echo STDERR pre-receive >&2
+ EOF
+
+ test_hook --setup -C victim.git update <<-\EOF &&
+ echo "$@" >>$GIT_DIR/update.args
+ read x; printf %s "$x" >$GIT_DIR/update.stdin
+ echo STDOUT update $1
+ echo STDERR update $1 >&2
+ test "$1" = refs/heads/main || exit
+ EOF
+
+ test_hook --setup -C victim.git post-receive <<-\EOF &&
+ printf %s "$@" >>$GIT_DIR/post-receive.args
+ cat - >$GIT_DIR/post-receive.stdin
+ echo STDOUT post-receive
+ echo STDERR post-receive >&2
+ EOF
+
+ test_hook --setup -C victim.git post-update <<-\EOF
+ echo "$@" >>$GIT_DIR/post-update.args
+ read x; printf %s "$x" >$GIT_DIR/post-update.stdin
+ echo STDOUT post-update
+ echo STDERR post-update >&2
+ EOF
+'
+
+test_expect_success push '
+ test_must_fail git send-pack --force ./victim.git \
+ main tofail >send.out 2>send.err
+'
+
+test_expect_success 'updated as expected' '
+ test $(GIT_DIR=victim.git git rev-parse main) = $commit1 &&
+ test $(GIT_DIR=victim.git git rev-parse tofail) = $commit1
+'
+
+test_expect_success 'hooks ran' '
+ test -f victim.git/pre-receive.args &&
+ test -f victim.git/pre-receive.stdin &&
+ test -f victim.git/update.args &&
+ test -f victim.git/update.stdin &&
+ test -f victim.git/post-receive.args &&
+ test -f victim.git/post-receive.stdin &&
+ test -f victim.git/post-update.args &&
+ test -f victim.git/post-update.stdin
+'
+
+test_expect_success 'pre-receive hook input' '
+ (echo $commit0 $commit1 refs/heads/main &&
+ echo $commit1 $commit0 refs/heads/tofail
+ ) | test_cmp - victim.git/pre-receive.stdin
+'
+
+test_expect_success 'update hook arguments' '
+ (echo refs/heads/main $commit0 $commit1 &&
+ echo refs/heads/tofail $commit1 $commit0
+ ) | test_cmp - victim.git/update.args
+'
+
+test_expect_success 'post-receive hook input' '
+ echo $commit0 $commit1 refs/heads/main |
+ test_cmp - victim.git/post-receive.stdin
+'
+
+test_expect_success 'post-update hook arguments' '
+ echo refs/heads/main |
+ test_cmp - victim.git/post-update.args
+'
+
+test_expect_success 'all hook stdin is /dev/null' '
+ test_must_be_empty victim.git/update.stdin &&
+ test_must_be_empty victim.git/post-update.stdin
+'
+
+test_expect_success 'all *-receive hook args are empty' '
+ test_must_be_empty victim.git/pre-receive.args &&
+ test_must_be_empty victim.git/post-receive.args
+'
+
+test_expect_success 'send-pack produced no output' '
+ test_must_be_empty send.out
+'
+
+cat <<EOF >expect
+remote: STDOUT pre-receive
+remote: STDERR pre-receive
+remote: STDOUT update refs/heads/main
+remote: STDERR update refs/heads/main
+remote: STDOUT update refs/heads/tofail
+remote: STDERR update refs/heads/tofail
+remote: error: hook declined to update refs/heads/tofail
+remote: STDOUT post-receive
+remote: STDERR post-receive
+remote: STDOUT post-update
+remote: STDERR post-update
+EOF
+test_expect_success 'send-pack stderr contains hook messages' '
+ grep ^remote: send.err | sed "s/ *\$//" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'pre-receive hook that forgets to read its input' '
+ test_hook --clobber -C victim.git pre-receive <<-\EOF &&
+ exit 0
+ EOF
+ rm -f victim.git/hooks/update victim.git/hooks/post-update &&
+
+ for v in $(test_seq 100 999)
+ do
+ git branch branch_$v main || return
+ done &&
+ git push ./victim.git "+refs/heads/*:refs/heads/*"
+'
+
+test_done
diff --git a/t/t5402-post-merge-hook.sh b/t/t5402-post-merge-hook.sh
new file mode 100755
index 0000000..46ebdfb
--- /dev/null
+++ b/t/t5402-post-merge-hook.sh
@@ -0,0 +1,62 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Josh England
+#
+
+test_description='Test the post-merge hook.'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ echo Data for commit0. >a &&
+ git update-index --add a &&
+ tree0=$(git write-tree) &&
+ commit0=$(echo setup | git commit-tree $tree0) &&
+ echo Changed data for commit1. >a &&
+ git update-index a &&
+ tree1=$(git write-tree) &&
+ commit1=$(echo modify | git commit-tree $tree1 -p $commit0) &&
+ git update-ref refs/heads/main $commit0 &&
+ git clone ./. clone1 &&
+ GIT_DIR=clone1/.git git update-index --add a &&
+ git clone ./. clone2 &&
+ GIT_DIR=clone2/.git git update-index --add a
+'
+
+test_expect_success 'setup clone hooks' '
+ test_when_finished "rm -f hook" &&
+ cat >hook <<-\EOF &&
+ echo $@ >>$GIT_DIR/post-merge.args
+ EOF
+
+ test_hook --setup -C clone1 post-merge <hook &&
+ test_hook --setup -C clone2 post-merge <hook
+'
+
+test_expect_success 'post-merge does not run for up-to-date ' '
+ GIT_DIR=clone1/.git git merge $commit0 &&
+ ! test -f clone1/.git/post-merge.args
+'
+
+test_expect_success 'post-merge runs as expected ' '
+ GIT_DIR=clone1/.git git merge $commit1 &&
+ test -e clone1/.git/post-merge.args
+'
+
+test_expect_success 'post-merge from normal merge receives the right argument ' '
+ grep 0 clone1/.git/post-merge.args
+'
+
+test_expect_success 'post-merge from squash merge runs as expected ' '
+ GIT_DIR=clone2/.git git merge --squash $commit1 &&
+ test -e clone2/.git/post-merge.args
+'
+
+test_expect_success 'post-merge from squash merge receives the right argument ' '
+ grep 1 clone2/.git/post-merge.args
+'
+
+test_done
diff --git a/t/t5403-post-checkout-hook.sh b/t/t5403-post-checkout-hook.sh
new file mode 100755
index 0000000..cfaae54
--- /dev/null
+++ b/t/t5403-post-checkout-hook.sh
@@ -0,0 +1,116 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Josh England
+#
+
+test_description='Test the post-checkout hook.'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ test_hook --setup post-checkout <<-\EOF &&
+ echo "$@" >.git/post-checkout.args
+ EOF
+ test_commit one &&
+ test_commit two &&
+ test_commit rebase-on-me &&
+ git reset --hard HEAD^ &&
+ test_commit three
+'
+
+test_expect_success 'post-checkout receives the right arguments with HEAD unchanged ' '
+ test_when_finished "rm -f .git/post-checkout.args" &&
+ git checkout main &&
+ read old new flag <.git/post-checkout.args &&
+ test $old = $new && test $flag = 1
+'
+
+test_expect_success 'post-checkout args are correct with git checkout -b ' '
+ test_when_finished "rm -f .git/post-checkout.args" &&
+ git checkout -b new1 &&
+ read old new flag <.git/post-checkout.args &&
+ test $old = $new && test $flag = 1
+'
+
+test_expect_success 'post-checkout receives the right args with HEAD changed ' '
+ test_when_finished "rm -f .git/post-checkout.args" &&
+ git checkout two &&
+ read old new flag <.git/post-checkout.args &&
+ test $old != $new && test $flag = 1
+'
+
+test_expect_success 'post-checkout receives the right args when not switching branches ' '
+ test_when_finished "rm -f .git/post-checkout.args" &&
+ git checkout main -- three.t &&
+ read old new flag <.git/post-checkout.args &&
+ test $old = $new && test $flag = 0
+'
+
+test_rebase () {
+ args="$*" &&
+ test_expect_success "post-checkout is triggered on rebase $args" '
+ test_when_finished "rm -f .git/post-checkout.args" &&
+ git checkout -B rebase-test main &&
+ rm -f .git/post-checkout.args &&
+ git rebase $args rebase-on-me &&
+ read old new flag <.git/post-checkout.args &&
+ test_cmp_rev main $old &&
+ test_cmp_rev rebase-on-me $new &&
+ test $flag = 1
+ '
+
+ test_expect_success "post-checkout is triggered on rebase $args with fast-forward" '
+ test_when_finished "rm -f .git/post-checkout.args" &&
+ git checkout -B ff-rebase-test rebase-on-me^ &&
+ rm -f .git/post-checkout.args &&
+ git rebase $args rebase-on-me &&
+ read old new flag <.git/post-checkout.args &&
+ test_cmp_rev rebase-on-me^ $old &&
+ test_cmp_rev rebase-on-me $new &&
+ test $flag = 1
+ '
+
+ test_expect_success "rebase $args fast-forward branch checkout runs post-checkout hook" '
+ test_when_finished "test_might_fail git rebase --abort" &&
+ test_when_finished "rm -f .git/post-checkout.args" &&
+ git update-ref refs/heads/rebase-fast-forward three &&
+ git checkout two &&
+ rm -f .git/post-checkout.args &&
+ git rebase $args HEAD rebase-fast-forward &&
+ read old new flag <.git/post-checkout.args &&
+ test_cmp_rev two $old &&
+ test_cmp_rev three $new &&
+ test $flag = 1
+ '
+
+ test_expect_success "rebase $args checkout does not remove untracked files" '
+ test_when_finished "test_might_fail git rebase --abort" &&
+ test_when_finished "rm -f .git/post-checkout.args" &&
+ git update-ref refs/heads/rebase-fast-forward three &&
+ git checkout two &&
+ rm -f .git/post-checkout.args &&
+ echo untracked >three.t &&
+ test_when_finished "rm three.t" &&
+ test_must_fail git rebase $args HEAD rebase-fast-forward 2>err &&
+ grep "untracked working tree files would be overwritten by checkout" err &&
+ test_path_is_missing .git/post-checkout.args
+
+'
+}
+
+test_rebase --apply &&
+test_rebase --merge
+
+test_expect_success 'post-checkout hook is triggered by clone' '
+ mkdir -p templates/hooks &&
+ write_script templates/hooks/post-checkout <<-\EOF &&
+ echo "$@" >"$GIT_DIR/post-checkout.args"
+ EOF
+ git clone --template=templates . clone3 &&
+ test -f clone3/.git/post-checkout.args
+'
+
+test_done
diff --git a/t/t5404-tracking-branches.sh b/t/t5404-tracking-branches.sh
new file mode 100755
index 0000000..51737ee
--- /dev/null
+++ b/t/t5404-tracking-branches.sh
@@ -0,0 +1,66 @@
+#!/bin/sh
+
+test_description='tracking branch update checks for git push'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ echo 1 >file &&
+ git add file &&
+ git commit -m 1 &&
+ git branch b1 &&
+ git branch b2 &&
+ git branch b3 &&
+ git clone . aa &&
+ git checkout b1 &&
+ echo b1 >>file &&
+ git commit -a -m b1 &&
+ git checkout b2 &&
+ echo b2 >>file &&
+ git commit -a -m b2
+'
+
+test_expect_success 'prepare pushable branches' '
+ cd aa &&
+ b1=$(git rev-parse origin/b1) &&
+ b2=$(git rev-parse origin/b2) &&
+ git checkout -b b1 origin/b1 &&
+ echo aa-b1 >>file &&
+ git commit -a -m aa-b1 &&
+ git checkout -b b2 origin/b2 &&
+ echo aa-b2 >>file &&
+ git commit -a -m aa-b2 &&
+ git checkout main &&
+ echo aa-main >>file &&
+ git commit -a -m aa-main
+'
+
+test_expect_success 'mixed-success push returns error' '
+ test_must_fail git push origin :
+'
+
+test_expect_success 'check tracking branches updated correctly after push' '
+ test "$(git rev-parse origin/main)" = "$(git rev-parse main)"
+'
+
+test_expect_success 'check tracking branches not updated for failed refs' '
+ test "$(git rev-parse origin/b1)" = "$b1" &&
+ test "$(git rev-parse origin/b2)" = "$b2"
+'
+
+test_expect_success 'deleted branches have their tracking branches removed' '
+ git push origin :b1 &&
+ test "$(git rev-parse origin/b1)" = "origin/b1"
+'
+
+test_expect_success 'already deleted tracking branches ignored' '
+ git branch -d -r origin/b3 &&
+ git push origin :b3 >output 2>&1 &&
+ ! grep "^error: " output
+'
+
+test_done
diff --git a/t/t5405-send-pack-rewind.sh b/t/t5405-send-pack-rewind.sh
new file mode 100755
index 0000000..1686ac1
--- /dev/null
+++ b/t/t5405-send-pack-rewind.sh
@@ -0,0 +1,46 @@
+#!/bin/sh
+
+test_description='forced push to replace commit we do not have'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+
+ >file1 && git add file1 && test_tick &&
+ git commit -m Initial &&
+ git config receive.denyCurrentBranch warn &&
+
+ mkdir another && (
+ cd another &&
+ git init &&
+ git fetch --update-head-ok .. main:main
+ ) &&
+
+ >file2 && git add file2 && test_tick &&
+ git commit -m Second
+
+'
+
+test_expect_success 'non forced push should die not segfault' '
+
+ (
+ cd another &&
+ test_must_fail git push .. main:main
+ )
+
+'
+
+test_expect_success 'forced push should succeed' '
+
+ (
+ cd another &&
+ git push .. +main:main
+ )
+
+'
+
+test_done
diff --git a/t/t5406-remote-rejects.sh b/t/t5406-remote-rejects.sh
new file mode 100755
index 0000000..d6a9946
--- /dev/null
+++ b/t/t5406-remote-rejects.sh
@@ -0,0 +1,25 @@
+#!/bin/sh
+
+test_description='remote push rejects are reported by client'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ test_hook update <<-\EOF &&
+ exit 1
+ EOF
+ echo 1 >file &&
+ git add file &&
+ git commit -m 1 &&
+ git clone . child &&
+ cd child &&
+ echo 2 >file &&
+ git commit -a -m 2
+'
+
+test_expect_success 'push reports error' 'test_must_fail git push 2>stderr'
+
+test_expect_success 'individual ref reports error' 'grep rejected stderr'
+
+test_done
diff --git a/t/t5407-post-rewrite-hook.sh b/t/t5407-post-rewrite-hook.sh
new file mode 100755
index 0000000..ad7f8c6
--- /dev/null
+++ b/t/t5407-post-rewrite-hook.sh
@@ -0,0 +1,313 @@
+#!/bin/sh
+#
+# Copyright (c) 2010 Thomas Rast
+#
+
+test_description='Test the post-rewrite hook.'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ test_commit A foo A &&
+ test_commit B foo B &&
+ test_commit C foo C &&
+ test_commit D foo D &&
+ git checkout A^0 &&
+ test_commit E bar E &&
+ test_commit F foo F &&
+ git checkout B &&
+ git merge E &&
+ git tag merge-E &&
+ test_commit G G &&
+ test_commit H H &&
+ test_commit I I &&
+ git checkout main &&
+
+ test_hook --setup post-rewrite <<-EOF
+ echo \$@ > "$TRASH_DIRECTORY"/post-rewrite.args
+ cat > "$TRASH_DIRECTORY"/post-rewrite.data
+ EOF
+'
+
+clear_hook_input () {
+ rm -f post-rewrite.args post-rewrite.data
+}
+
+verify_hook_input () {
+ test_cmp expected.args "$TRASH_DIRECTORY"/post-rewrite.args &&
+ test_cmp expected.data "$TRASH_DIRECTORY"/post-rewrite.data
+}
+
+test_expect_success 'git commit --amend' '
+ clear_hook_input &&
+ echo "D new message" > newmsg &&
+ oldsha=$(git rev-parse HEAD^0) &&
+ git commit -Fnewmsg --amend &&
+ echo amend > expected.args &&
+ echo $oldsha $(git rev-parse HEAD^0) > expected.data &&
+ verify_hook_input
+'
+
+test_expect_success 'git commit --amend --no-post-rewrite' '
+ clear_hook_input &&
+ echo "D new message again" > newmsg &&
+ git commit --no-post-rewrite -Fnewmsg --amend &&
+ test ! -f post-rewrite.args &&
+ test ! -f post-rewrite.data
+'
+
+test_expect_success 'git rebase --apply' '
+ git reset --hard D &&
+ clear_hook_input &&
+ test_must_fail git rebase --apply --onto A B &&
+ echo C > foo &&
+ git add foo &&
+ git rebase --continue &&
+ echo rebase >expected.args &&
+ cat >expected.data <<-EOF &&
+ $(git rev-parse C) $(git rev-parse HEAD^)
+ $(git rev-parse D) $(git rev-parse HEAD)
+ EOF
+ verify_hook_input
+'
+
+test_expect_success 'git rebase --apply --skip' '
+ git reset --hard D &&
+ clear_hook_input &&
+ test_must_fail git rebase --apply --onto A B &&
+ test_must_fail git rebase --skip &&
+ echo D > foo &&
+ git add foo &&
+ git rebase --continue &&
+ echo rebase >expected.args &&
+ cat >expected.data <<-EOF &&
+ $(git rev-parse C) $(git rev-parse HEAD^)
+ $(git rev-parse D) $(git rev-parse HEAD)
+ EOF
+ verify_hook_input
+'
+
+test_expect_success 'git rebase --apply --skip the last one' '
+ git reset --hard F &&
+ clear_hook_input &&
+ test_must_fail git rebase --apply --onto D A &&
+ git rebase --skip &&
+ echo rebase >expected.args &&
+ cat >expected.data <<-EOF &&
+ $(git rev-parse E) $(git rev-parse HEAD)
+ $(git rev-parse F) $(git rev-parse HEAD)
+ EOF
+ verify_hook_input
+'
+
+test_expect_success 'git rebase -m' '
+ git reset --hard D &&
+ clear_hook_input &&
+ test_must_fail git rebase -m --onto A B &&
+ echo C > foo &&
+ git add foo &&
+ git rebase --continue &&
+ echo rebase >expected.args &&
+ cat >expected.data <<-EOF &&
+ $(git rev-parse C) $(git rev-parse HEAD^)
+ $(git rev-parse D) $(git rev-parse HEAD)
+ EOF
+ verify_hook_input
+'
+
+test_expect_success 'git rebase -m --skip' '
+ git reset --hard D &&
+ clear_hook_input &&
+ test_must_fail git rebase -m --onto A B &&
+ test_must_fail git rebase --skip &&
+ echo D > foo &&
+ git add foo &&
+ git rebase --continue &&
+ echo rebase >expected.args &&
+ cat >expected.data <<-EOF &&
+ $(git rev-parse C) $(git rev-parse HEAD^)
+ $(git rev-parse D) $(git rev-parse HEAD)
+ EOF
+ verify_hook_input
+'
+
+test_expect_success 'git rebase with implicit use of merge backend' '
+ git reset --hard D &&
+ clear_hook_input &&
+ test_must_fail git rebase --keep-empty --onto A B &&
+ echo C > foo &&
+ git add foo &&
+ git rebase --continue &&
+ echo rebase >expected.args &&
+ cat >expected.data <<-EOF &&
+ $(git rev-parse C) $(git rev-parse HEAD^)
+ $(git rev-parse D) $(git rev-parse HEAD)
+ EOF
+ verify_hook_input
+'
+
+test_expect_success 'git rebase --skip with implicit use of merge backend' '
+ git reset --hard D &&
+ clear_hook_input &&
+ test_must_fail git rebase --keep-empty --onto A B &&
+ test_must_fail git rebase --skip &&
+ echo D > foo &&
+ git add foo &&
+ git rebase --continue &&
+ echo rebase >expected.args &&
+ cat >expected.data <<-EOF &&
+ $(git rev-parse C) $(git rev-parse HEAD^)
+ $(git rev-parse D) $(git rev-parse HEAD)
+ EOF
+ verify_hook_input
+'
+
+. "$TEST_DIRECTORY"/lib-rebase.sh
+
+set_fake_editor
+
+# Helper to work around the lack of one-shot exporting for
+# test_must_fail (as it is a shell function)
+test_fail_interactive_rebase () {
+ (
+ FAKE_LINES="$1" &&
+ shift &&
+ export FAKE_LINES &&
+ test_must_fail git rebase -i "$@"
+ )
+}
+
+test_expect_success 'git rebase with failed pick' '
+ clear_hook_input &&
+ cat >todo <<-\EOF &&
+ exec >bar
+ merge -C merge-E E
+ exec >G
+ pick G
+ exec >H 2>I
+ pick H
+ fixup I
+ EOF
+
+ (
+ set_replace_editor todo &&
+ test_must_fail git rebase -i D D 2>err
+ ) &&
+ grep "would be overwritten" err &&
+ rm bar &&
+
+ test_must_fail git rebase --continue 2>err &&
+ grep "would be overwritten" err &&
+ rm G &&
+
+ test_must_fail git rebase --continue 2>err &&
+ grep "would be overwritten" err &&
+ rm H &&
+
+ test_must_fail git rebase --continue 2>err &&
+ grep "would be overwritten" err &&
+ rm I &&
+
+ git rebase --continue &&
+ echo rebase >expected.args &&
+ cat >expected.data <<-EOF &&
+ $(git rev-parse merge-E) $(git rev-parse HEAD~2)
+ $(git rev-parse G) $(git rev-parse HEAD~1)
+ $(git rev-parse H) $(git rev-parse HEAD)
+ $(git rev-parse I) $(git rev-parse HEAD)
+ EOF
+ verify_hook_input
+'
+
+test_expect_success 'git rebase -i (unchanged)' '
+ git reset --hard D &&
+ clear_hook_input &&
+ test_fail_interactive_rebase "1 2" --onto A B &&
+ echo C > foo &&
+ git add foo &&
+ git rebase --continue &&
+ echo rebase >expected.args &&
+ cat >expected.data <<-EOF &&
+ $(git rev-parse C) $(git rev-parse HEAD^)
+ $(git rev-parse D) $(git rev-parse HEAD)
+ EOF
+ verify_hook_input
+'
+
+test_expect_success 'git rebase -i (skip)' '
+ git reset --hard D &&
+ clear_hook_input &&
+ test_fail_interactive_rebase "2" --onto A B &&
+ echo D > foo &&
+ git add foo &&
+ git rebase --continue &&
+ echo rebase >expected.args &&
+ cat >expected.data <<-EOF &&
+ $(git rev-parse D) $(git rev-parse HEAD)
+ EOF
+ verify_hook_input
+'
+
+test_expect_success 'git rebase -i (squash)' '
+ git reset --hard D &&
+ clear_hook_input &&
+ test_fail_interactive_rebase "1 squash 2" --onto A B &&
+ echo C > foo &&
+ git add foo &&
+ git rebase --continue &&
+ echo rebase >expected.args &&
+ cat >expected.data <<-EOF &&
+ $(git rev-parse C) $(git rev-parse HEAD)
+ $(git rev-parse D) $(git rev-parse HEAD)
+ EOF
+ verify_hook_input
+'
+
+test_expect_success 'git rebase -i (fixup without conflict)' '
+ git reset --hard D &&
+ clear_hook_input &&
+ FAKE_LINES="1 fixup 2" git rebase -i B &&
+ echo rebase >expected.args &&
+ cat >expected.data <<-EOF &&
+ $(git rev-parse C) $(git rev-parse HEAD)
+ $(git rev-parse D) $(git rev-parse HEAD)
+ EOF
+ verify_hook_input
+'
+
+test_expect_success 'git rebase -i (double edit)' '
+ git reset --hard D &&
+ clear_hook_input &&
+ FAKE_LINES="edit 1 edit 2" git rebase -i B &&
+ git rebase --continue &&
+ echo something > foo &&
+ git add foo &&
+ git rebase --continue &&
+ echo rebase >expected.args &&
+ cat >expected.data <<-EOF &&
+ $(git rev-parse C) $(git rev-parse HEAD^)
+ $(git rev-parse D) $(git rev-parse HEAD)
+ EOF
+ verify_hook_input
+'
+
+test_expect_success 'git rebase -i (exec)' '
+ git reset --hard D &&
+ clear_hook_input &&
+ FAKE_LINES="edit 1 exec_false 2" git rebase -i B &&
+ echo something >bar &&
+ git add bar &&
+ # Fails because of exec false
+ test_must_fail git rebase --continue &&
+ git rebase --continue &&
+ echo rebase >expected.args &&
+ cat >expected.data <<-EOF &&
+ $(git rev-parse C) $(git rev-parse HEAD^)
+ $(git rev-parse D) $(git rev-parse HEAD)
+ EOF
+ verify_hook_input
+'
+
+test_done
diff --git a/t/t5408-send-pack-stdin.sh b/t/t5408-send-pack-stdin.sh
new file mode 100755
index 0000000..e8737df
--- /dev/null
+++ b/t/t5408-send-pack-stdin.sh
@@ -0,0 +1,92 @@
+#!/bin/sh
+
+test_description='send-pack --stdin tests'
+. ./test-lib.sh
+
+create_ref () {
+ tree=$(git write-tree) &&
+ test_tick &&
+ commit=$(echo "$1" | git commit-tree $tree) &&
+ git update-ref "$1" $commit
+}
+
+clear_remote () {
+ rm -rf remote.git &&
+ git init --bare remote.git
+}
+
+verify_push () {
+ git rev-parse "$1" >expect &&
+ git --git-dir=remote.git rev-parse "${2:-$1}" >actual &&
+ test_cmp expect actual
+}
+
+test_expect_success 'setup refs' '
+ cat >refs <<-\EOF &&
+ refs/heads/A
+ refs/heads/C
+ refs/tags/D
+ refs/heads/B
+ refs/tags/E
+ EOF
+ for i in $(cat refs); do
+ create_ref $i || return 1
+ done
+'
+
+# sanity check our setup
+test_expect_success 'refs on cmdline' '
+ clear_remote &&
+ git send-pack remote.git $(cat refs) &&
+ for i in $(cat refs); do
+ verify_push $i || return 1
+ done
+'
+
+test_expect_success 'refs over stdin' '
+ clear_remote &&
+ git send-pack remote.git --stdin <refs &&
+ for i in $(cat refs); do
+ verify_push $i || return 1
+ done
+'
+
+test_expect_success 'stdin lines are full refspecs' '
+ clear_remote &&
+ echo "A:other" >input &&
+ git send-pack remote.git --stdin <input &&
+ verify_push refs/heads/A refs/heads/other
+'
+
+test_expect_success 'stdin mixed with cmdline' '
+ clear_remote &&
+ echo A >input &&
+ git send-pack remote.git --stdin B <input &&
+ verify_push A &&
+ verify_push B
+'
+
+test_expect_success 'cmdline refs written in order' '
+ clear_remote &&
+ test_must_fail git send-pack remote.git A:foo B:foo &&
+ verify_push A foo
+'
+
+test_expect_success '--stdin refs come after cmdline' '
+ clear_remote &&
+ echo A:foo >input &&
+ test_must_fail git send-pack remote.git --stdin B:foo <input &&
+ verify_push B foo
+'
+
+test_expect_success 'refspecs and --mirror do not mix (cmdline)' '
+ clear_remote &&
+ test_must_fail git send-pack remote.git --mirror $(cat refs)
+'
+
+test_expect_success 'refspecs and --mirror do not mix (stdin)' '
+ clear_remote &&
+ test_must_fail git send-pack remote.git --mirror --stdin <refs
+'
+
+test_done
diff --git a/t/t5409-colorize-remote-messages.sh b/t/t5409-colorize-remote-messages.sh
new file mode 100755
index 0000000..fa5de45
--- /dev/null
+++ b/t/t5409-colorize-remote-messages.sh
@@ -0,0 +1,101 @@
+#!/bin/sh
+
+test_description='remote messages are colorized on the client'
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ test_hook --setup update <<-\EOF &&
+ echo error: error
+ echo ERROR: also highlighted
+ echo hint: hint
+ echo hinting: not highlighted
+ echo success: success
+ echo warning: warning
+ echo prefixerror: error
+ echo " " "error: leading space"
+ echo " "
+ echo Err
+ echo SUCCESS
+ exit 0
+ EOF
+ echo 1 >file &&
+ git add file &&
+ git commit -m 1 &&
+ git clone . child &&
+ (
+ cd child &&
+ test_commit message2 file content2
+ )
+'
+
+test_expect_success 'keywords' '
+ git --git-dir child/.git -c color.remote=always push -f origin HEAD:refs/heads/keywords 2>output &&
+ test_decode_color <output >decoded &&
+ grep "<BOLD;RED>error<RESET>: error" decoded &&
+ grep "<YELLOW>hint<RESET>:" decoded &&
+ grep "<BOLD;GREEN>success<RESET>:" decoded &&
+ grep "<BOLD;GREEN>SUCCESS<RESET>" decoded &&
+ grep "<BOLD;YELLOW>warning<RESET>:" decoded
+'
+
+test_expect_success 'whole words at line start' '
+ git --git-dir child/.git -c color.remote=always push -f origin HEAD:refs/heads/whole-words 2>output &&
+ test_decode_color <output >decoded &&
+ grep "<YELLOW>hint<RESET>:" decoded &&
+ grep "hinting: not highlighted" decoded &&
+ grep "prefixerror: error" decoded
+'
+
+test_expect_success 'short line' '
+ git -C child -c color.remote=always push -f origin HEAD:short-line 2>output &&
+ test_decode_color <output >decoded &&
+ grep "remote: Err" decoded
+'
+
+test_expect_success 'case-insensitive' '
+ git --git-dir child/.git -c color.remote=always push -f origin HEAD:refs/heads/case-insensitive 2>output &&
+ test_decode_color <output >decoded &&
+ grep "<BOLD;RED>error<RESET>: error" decoded &&
+ grep "<BOLD;RED>ERROR<RESET>: also highlighted" decoded
+'
+
+test_expect_success 'leading space' '
+ git --git-dir child/.git -c color.remote=always push -f origin HEAD:refs/heads/leading-space 2>output &&
+ test_decode_color <output >decoded &&
+ grep " <BOLD;RED>error<RESET>: leading space" decoded
+'
+
+test_expect_success 'spaces only' '
+ git -C child -c color.remote=always push -f origin HEAD:only-space 2>output &&
+ test_decode_color <output >decoded &&
+ grep "remote: " decoded
+'
+
+test_expect_success 'no coloring for redirected output' '
+ git --git-dir child/.git push -f origin HEAD:refs/heads/redirected-output 2>output &&
+ test_decode_color <output >decoded &&
+ grep "error: error" decoded
+'
+
+test_expect_success 'push with customized color' '
+ git --git-dir child/.git -c color.remote=always -c color.remote.error=blue push -f origin HEAD:refs/heads/customized-color 2>output &&
+ test_decode_color <output >decoded &&
+ grep "<BLUE>error<RESET>:" decoded &&
+ grep "<BOLD;GREEN>success<RESET>:" decoded
+'
+
+
+test_expect_success 'error in customized color' '
+ git --git-dir child/.git -c color.remote=always -c color.remote.error=i-am-not-a-color push -f origin HEAD:refs/heads/error-customized-color 2>output &&
+ test_decode_color <output >decoded &&
+ grep "<BOLD;GREEN>success<RESET>:" decoded
+'
+
+test_expect_success 'fallback to color.ui' '
+ git --git-dir child/.git -c color.ui=always push -f origin HEAD:refs/heads/fallback-color-ui 2>output &&
+ test_decode_color <output >decoded &&
+ grep "<BOLD;RED>error<RESET>: error" decoded
+'
+
+test_done
diff --git a/t/t5410-receive-pack-alternates.sh b/t/t5410-receive-pack-alternates.sh
new file mode 100755
index 0000000..7a45d4c
--- /dev/null
+++ b/t/t5410-receive-pack-alternates.sh
@@ -0,0 +1,45 @@
+#!/bin/sh
+
+test_description='git receive-pack with alternate ref filtering'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ test_commit base &&
+ git clone -s --bare . fork &&
+ git checkout -b public/branch main &&
+ test_commit public &&
+ git checkout -b private/branch main &&
+ test_commit private
+'
+
+extract_haves () {
+ depacketize | perl -lne '/^(\S+) \.have/ and print $1'
+}
+
+test_expect_success 'with core.alternateRefsCommand' '
+ write_script fork/alternate-refs <<-\EOF &&
+ git --git-dir="$1" for-each-ref \
+ --format="%(objectname)" \
+ refs/heads/public/
+ EOF
+ test_config -C fork core.alternateRefsCommand ./alternate-refs &&
+ git rev-parse public/branch >expect &&
+ printf "0000" | git receive-pack fork >actual &&
+ extract_haves <actual >actual.haves &&
+ test_cmp expect actual.haves
+'
+
+test_expect_success 'with core.alternateRefsPrefixes' '
+ test_config -C fork core.alternateRefsPrefixes "refs/heads/private" &&
+ git rev-parse private/branch >expect &&
+ printf "0000" | git receive-pack fork >actual &&
+ extract_haves <actual >actual.haves &&
+ test_cmp expect actual.haves
+'
+
+test_done
diff --git a/t/t5411-proc-receive-hook.sh b/t/t5411-proc-receive-hook.sh
new file mode 100755
index 0000000..92cf52c
--- /dev/null
+++ b/t/t5411-proc-receive-hook.sh
@@ -0,0 +1,120 @@
+#!/bin/sh
+#
+# Copyright (c) 2020 Jiang Xin
+#
+
+test_description='Test proc-receive hook'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+. "$TEST_DIRECTORY"/t5411/common-functions.sh
+
+setup_upstream_and_workbench () {
+ # Refs of upstream : main(A)
+ # Refs of workbench: main(A) tags/v123
+ test_expect_success "setup upstream and workbench" '
+ rm -rf upstream.git &&
+ rm -rf workbench &&
+ git init --bare upstream.git &&
+ git init workbench &&
+ create_commits_in workbench A B &&
+ (
+ cd workbench &&
+ # Try to make a stable fixed width for abbreviated commit ID,
+ # this fixed-width oid will be replaced with "<OID>".
+ git config core.abbrev 7 &&
+ git tag -m "v123" v123 $A &&
+ git remote add origin ../upstream.git &&
+ git push origin main &&
+ git update-ref refs/heads/main $A $B &&
+ git -C ../upstream.git update-ref \
+ refs/heads/main $A $B
+ ) &&
+ TAG=$(git -C workbench rev-parse v123) &&
+
+ # setup pre-receive hook
+ test_hook --setup -C upstream.git pre-receive <<-\EOF &&
+ exec >&2
+ echo "# pre-receive hook"
+ while read old new ref
+ do
+ echo "pre-receive< $old $new $ref"
+ done
+ EOF
+
+ # setup post-receive hook
+ test_hook --setup -C upstream.git post-receive <<-\EOF &&
+ exec >&2
+ echo "# post-receive hook"
+ while read old new ref
+ do
+ echo "post-receive< $old $new $ref"
+ done
+ EOF
+
+ upstream=upstream.git
+ '
+}
+
+run_proc_receive_hook_test() {
+ case $1 in
+ http)
+ PROTOCOL="HTTP protocol"
+ URL_PREFIX="http://.*"
+ ;;
+ local)
+ PROTOCOL="builtin protocol"
+ URL_PREFIX="\.\."
+ ;;
+ esac
+
+ # Include test cases for both file and HTTP protocol
+ for t in "$TEST_DIRECTORY"/t5411/test-*.sh
+ do
+ . "$t"
+ done
+}
+
+# Initialize the upstream repository and local workbench.
+setup_upstream_and_workbench
+
+# Load test cases that only need to be executed once.
+for t in "$TEST_DIRECTORY"/t5411/once-*.sh
+do
+ . "$t"
+done
+
+# Initialize the upstream repository and local workbench.
+setup_upstream_and_workbench
+
+# Run test cases for 'proc-receive' hook on local file protocol.
+run_proc_receive_hook_test local
+
+ROOT_PATH="$PWD"
+. "$TEST_DIRECTORY"/lib-gpg.sh
+. "$TEST_DIRECTORY"/lib-httpd.sh
+. "$TEST_DIRECTORY"/lib-terminal.sh
+start_httpd
+
+# Re-initialize the upstream repository and local workbench.
+setup_upstream_and_workbench
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+test_expect_success "setup for HTTP protocol" '
+ git -C upstream.git config http.receivepack true &&
+ upstream="$HTTPD_DOCUMENT_ROOT_PATH/upstream.git" &&
+ mv upstream.git "$upstream" &&
+ git -C workbench remote set-url origin "$HTTPD_URL/auth-push/smart/upstream.git" &&
+ set_askpass user@host pass@host
+'
+
+setup_askpass_helper
+
+# Run test cases for 'proc-receive' hook on HTTP protocol.
+run_proc_receive_hook_test http
+
+test_done
diff --git a/t/t5411/common-functions.sh b/t/t5411/common-functions.sh
new file mode 100644
index 0000000..3c74778
--- /dev/null
+++ b/t/t5411/common-functions.sh
@@ -0,0 +1,73 @@
+# Create commits in <repo> and assign each commit's oid to shell variables
+# given in the arguments (A, B, and C). E.g.:
+#
+# create_commits_in <repo> A B C
+#
+# NOTE: Never calling this function from a subshell since variable
+# assignments will disappear when subshell exits.
+create_commits_in () {
+ repo="$1" && test -d "$repo" ||
+ error "Repository $repo does not exist."
+ shift &&
+ while test $# -gt 0
+ do
+ name=$1 &&
+ shift &&
+ test_commit -C "$repo" --no-tag "$name" &&
+ eval $name=$(git -C "$repo" rev-parse HEAD)
+ done
+}
+
+get_abbrev_oid () {
+ oid=$1 &&
+ suffix=${oid#???????} &&
+ oid=${oid%$suffix} &&
+ if test -n "$oid"
+ then
+ echo "$oid"
+ else
+ echo "undefined-oid"
+ fi
+}
+
+# Format the output of git-push, git-show-ref and other commands to make a
+# user-friendly and stable text. We can easily prepare the expect text
+# without having to worry about changes of the commit ID (full or abbrev.)
+# of the output. Single quotes are replaced with double quotes, because
+# it is boring to prepare unquoted single quotes in expect text. We also
+# remove some locale error messages. The emitted human-readable errors are
+# redundant to the more machine-readable output the tests already assert.
+make_user_friendly_and_stable_output () {
+ sed \
+ -e "s/'/\"/g" \
+ -e "s/$(get_abbrev_oid $A)[0-9a-f]*/<COMMIT-A>/g" \
+ -e "s/$(get_abbrev_oid $B)[0-9a-f]*/<COMMIT-B>/g" \
+ -e "s/$(get_abbrev_oid $TAG)[0-9a-f]*/<TAG-v123>/g" \
+ -e "s/$ZERO_OID/<ZERO-OID>/g" \
+ -e "s#To $URL_PREFIX/upstream.git#To <URL/of/upstream.git>#" \
+ -e "/^error: / d"
+}
+
+filter_out_user_friendly_and_stable_output () {
+ make_user_friendly_and_stable_output |
+ sed -n ${1+"$@"}
+}
+
+format_and_save_expect () {
+ sed -e 's/^> //' -e 's/Z$//' >expect
+}
+
+test_cmp_refs () {
+ indir=
+ if test "$1" = "-C"
+ then
+ shift
+ indir="$1"
+ shift
+ fi
+ indir=${indir:+"$indir"/}
+ cat >show-ref.expect &&
+ git ${indir:+ -C "$indir"} show-ref >show-ref.pristine &&
+ make_user_friendly_and_stable_output <show-ref.pristine >show-ref.filtered &&
+ test_cmp show-ref.expect show-ref.filtered
+}
diff --git a/t/t5411/once-0010-report-status-v1.sh b/t/t5411/once-0010-report-status-v1.sh
new file mode 100644
index 0000000..f9ffb01
--- /dev/null
+++ b/t/t5411/once-0010-report-status-v1.sh
@@ -0,0 +1,91 @@
+test_expect_success "setup receive.procReceiveRefs" '
+ git -C "$upstream" config --add receive.procReceiveRefs refs/for
+'
+
+test_expect_success "setup proc-receive hook" '
+ test_hook -C "$upstream" --clobber proc-receive <<-EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ok refs/for/main/topic1" \
+ -r "option fall-through" \
+ -r "ok refs/for/main/topic2" \
+ -r "option refname refs/for/changes/23/123/1" \
+ -r "option new-oid $A" \
+ -r "ok refs/for/main/topic2" \
+ -r "option refname refs/for/changes/24/124/2" \
+ -r "option old-oid $B" \
+ -r "option new-oid $A" \
+ -r "option forced-update" \
+ -r "ng refs/for/next/topic target branch not exist"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : (B) refs/for/main/topic1(A) foo(A) refs/for/next/topic(A) refs/for/main/topic2(A)
+test_expect_success "proc-receive: report status v1" '
+ {
+ if test -z "$GIT_DEFAULT_HASH" || test "$GIT_DEFAULT_HASH" = "sha1"
+ then
+ printf "%s %s refs/heads/main\0report-status\n" \
+ $A $B | packetize_raw
+ else
+ printf "%s %s refs/heads/main\0report-status object-format=$GIT_DEFAULT_HASH\n" \
+ $A $B | packetize_raw
+ fi &&
+ printf "%s %s refs/for/main/topic1\n" \
+ $ZERO_OID $A | packetize &&
+ printf "%s %s refs/heads/foo\n" \
+ $ZERO_OID $A | packetize &&
+ printf "%s %s refs/for/next/topic\n" \
+ $ZERO_OID $A | packetize &&
+ printf "%s %s refs/for/main/topic2\n" \
+ $ZERO_OID $A | packetize &&
+ printf 0000 &&
+ printf "" | git -C "$upstream" pack-objects --stdout
+ } | git receive-pack "$upstream" --stateless-rpc \
+ >out 2>&1 &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ # pre-receive hook
+ pre-receive< <COMMIT-A> <COMMIT-B> refs/heads/main
+ pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic1
+ pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/foo
+ pre-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic
+ pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic2
+ # proc-receive hook
+ proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic1
+ proc-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic
+ proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic2
+ proc-receive> ok refs/for/main/topic1
+ proc-receive> option fall-through
+ proc-receive> ok refs/for/main/topic2
+ proc-receive> option refname refs/for/changes/23/123/1
+ proc-receive> option new-oid <COMMIT-A>
+ proc-receive> ok refs/for/main/topic2
+ proc-receive> option refname refs/for/changes/24/124/2
+ proc-receive> option old-oid <COMMIT-B>
+ proc-receive> option new-oid <COMMIT-A>
+ proc-receive> option forced-update
+ proc-receive> ng refs/for/next/topic target branch not exist
+ 000eunpack ok
+ 0017ok refs/heads/main
+ 001cok refs/for/main/topic1
+ 0016ok refs/heads/foo
+ 0033ng refs/for/next/topic target branch not exist
+ 001cok refs/for/main/topic2
+ 0000# post-receive hook
+ post-receive< <COMMIT-A> <COMMIT-B> refs/heads/main
+ post-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic1
+ post-receive< <ZERO-OID> <COMMIT-A> refs/heads/foo
+ post-receive< <ZERO-OID> <COMMIT-A> refs/for/changes/23/123/1
+ post-receive< <COMMIT-B> <COMMIT-A> refs/for/changes/24/124/2
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/for/main/topic1
+ <COMMIT-A> refs/heads/foo
+ <COMMIT-B> refs/heads/main
+ EOF
+'
diff --git a/t/t5411/test-0000-standard-git-push.sh b/t/t5411/test-0000-standard-git-push.sh
new file mode 100644
index 0000000..ce64bb6
--- /dev/null
+++ b/t/t5411/test-0000-standard-git-push.sh
@@ -0,0 +1,134 @@
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git-push : main(B) next(A)
+test_expect_success "git-push ($PROTOCOL)" '
+ git -C workbench push origin \
+ $B:refs/heads/main \
+ HEAD:refs/heads/next \
+ >out 2>&1 &&
+ make_user_friendly_and_stable_output <out >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <COMMIT-A> <COMMIT-B> refs/heads/main Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/next Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <COMMIT-A> <COMMIT-B> refs/heads/main Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/next Z
+ > To <URL/of/upstream.git>
+ > <COMMIT-A>..<COMMIT-B> <COMMIT-B> -> main
+ > * [new branch] HEAD -> next
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-B> refs/heads/main
+ <COMMIT-A> refs/heads/next
+ EOF
+'
+
+# Refs of upstream : main(B) next(A)
+# Refs of workbench: main(A) tags/v123
+# git-push --atomic: main(A) next(B)
+test_expect_success "git-push --atomic ($PROTOCOL)" '
+ test_must_fail git -C workbench push --atomic origin \
+ main \
+ $B:refs/heads/next \
+ >out-$test_count 2>&1 &&
+ filter_out_user_friendly_and_stable_output \
+ -e "/^To / { p; }" \
+ -e "/^ ! / { p; }" \
+ <out-$test_count >actual &&
+ format_and_save_expect <<-EOF &&
+ > To <URL/of/upstream.git>
+ > ! [rejected] main -> main (non-fast-forward)
+ > ! [rejected] <COMMIT-B> -> next (atomic push failed)
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-B> refs/heads/main
+ <COMMIT-A> refs/heads/next
+ EOF
+'
+
+# Refs of upstream : main(B) next(A)
+# Refs of workbench: main(A) tags/v123
+# git-push : main(A) next(B)
+test_expect_success "non-fast-forward git-push ($PROTOCOL)" '
+ test_must_fail git \
+ -C workbench \
+ -c advice.pushUpdateRejected=false \
+ push origin \
+ main \
+ $B:refs/heads/next \
+ >out-$test_count 2>&1 &&
+ make_user_friendly_and_stable_output <out-$test_count >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <COMMIT-A> <COMMIT-B> refs/heads/next Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <COMMIT-A> <COMMIT-B> refs/heads/next Z
+ > To <URL/of/upstream.git>
+ > <COMMIT-A>..<COMMIT-B> <COMMIT-B> -> next
+ > ! [rejected] main -> main (non-fast-forward)
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-B> refs/heads/main
+ <COMMIT-B> refs/heads/next
+ EOF
+'
+
+# Refs of upstream : main(B) next(B)
+# Refs of workbench: main(A) tags/v123
+# git-push -f : main(A) NULL tags/v123 refs/review/main/topic(A) a/b/c(A)
+test_expect_success "git-push -f ($PROTOCOL)" '
+ git -C workbench push -f origin \
+ refs/tags/v123 \
+ :refs/heads/next \
+ main \
+ main:refs/review/main/topic \
+ HEAD:refs/heads/a/b/c \
+ >out 2>&1 &&
+ make_user_friendly_and_stable_output <out >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <COMMIT-B> <COMMIT-A> refs/heads/main Z
+ > remote: pre-receive< <COMMIT-B> <ZERO-OID> refs/heads/next Z
+ > remote: pre-receive< <ZERO-OID> <TAG-v123> refs/tags/v123 Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/review/main/topic Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/a/b/c Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <COMMIT-B> <COMMIT-A> refs/heads/main Z
+ > remote: post-receive< <COMMIT-B> <ZERO-OID> refs/heads/next Z
+ > remote: post-receive< <ZERO-OID> <TAG-v123> refs/tags/v123 Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/review/main/topic Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/a/b/c Z
+ > To <URL/of/upstream.git>
+ > + <COMMIT-B>...<COMMIT-A> main -> main (forced update)
+ > - [deleted] next
+ > * [new tag] v123 -> v123
+ > * [new reference] main -> refs/review/main/topic
+ > * [new branch] HEAD -> a/b/c
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/a/b/c
+ <COMMIT-A> refs/heads/main
+ <COMMIT-A> refs/review/main/topic
+ <TAG-v123> refs/tags/v123
+ EOF
+'
+
+# Refs of upstream : main(A) tags/v123 refs/review/main/topic(A) a/b/c(A)
+# Refs of workbench: main(A) tags/v123
+test_expect_success "cleanup ($PROTOCOL)" '
+ (
+ cd "$upstream" &&
+ git update-ref -d refs/review/main/topic &&
+ git update-ref -d refs/tags/v123 &&
+ git update-ref -d refs/heads/a/b/c
+ )
+'
diff --git a/t/t5411/test-0001-standard-git-push--porcelain.sh b/t/t5411/test-0001-standard-git-push--porcelain.sh
new file mode 100644
index 0000000..373ec3d
--- /dev/null
+++ b/t/t5411/test-0001-standard-git-push--porcelain.sh
@@ -0,0 +1,138 @@
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git-push : main(B) next(A)
+test_expect_success "git-push ($PROTOCOL/porcelain)" '
+ git -C workbench push --porcelain origin \
+ $B:refs/heads/main \
+ HEAD:refs/heads/next \
+ >out 2>&1 &&
+ make_user_friendly_and_stable_output <out >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <COMMIT-A> <COMMIT-B> refs/heads/main Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/next Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <COMMIT-A> <COMMIT-B> refs/heads/main Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/next Z
+ > To <URL/of/upstream.git>
+ > <COMMIT-B>:refs/heads/main <COMMIT-A>..<COMMIT-B>
+ > * HEAD:refs/heads/next [new branch]
+ > Done
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-B> refs/heads/main
+ <COMMIT-A> refs/heads/next
+ EOF
+'
+
+# Refs of upstream : main(B) next(A)
+# Refs of workbench: main(A) tags/v123
+# git-push --atomic: main(A) next(B)
+test_expect_success "git-push --atomic ($PROTOCOL/porcelain)" '
+ test_must_fail git -C workbench push --atomic --porcelain origin \
+ main \
+ $B:refs/heads/next \
+ >out-$test_count 2>&1 &&
+ filter_out_user_friendly_and_stable_output \
+ -e "s/^# GETTEXT POISON #//" \
+ -e "/^To / { p; }" \
+ -e "/^!/ { p; }" \
+ <out-$test_count >actual &&
+ format_and_save_expect <<-EOF &&
+ > To <URL/of/upstream.git>
+ > ! refs/heads/main:refs/heads/main [rejected] (non-fast-forward)
+ > ! <COMMIT-B>:refs/heads/next [rejected] (atomic push failed)
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-B> refs/heads/main
+ <COMMIT-A> refs/heads/next
+ EOF
+'
+
+# Refs of upstream : main(B) next(A)
+# Refs of workbench: main(A) tags/v123
+# git-push : main(A) next(B)
+test_expect_success "non-fast-forward git-push ($PROTOCOL/porcelain)" '
+ test_must_fail git \
+ -C workbench \
+ -c advice.pushUpdateRejected=false \
+ push --porcelain origin \
+ main \
+ $B:refs/heads/next \
+ >out-$test_count 2>&1 &&
+ make_user_friendly_and_stable_output <out-$test_count >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <COMMIT-A> <COMMIT-B> refs/heads/next Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <COMMIT-A> <COMMIT-B> refs/heads/next Z
+ > To <URL/of/upstream.git>
+ > <COMMIT-B>:refs/heads/next <COMMIT-A>..<COMMIT-B>
+ > ! refs/heads/main:refs/heads/main [rejected] (non-fast-forward)
+ > Done
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-B> refs/heads/main
+ <COMMIT-B> refs/heads/next
+ EOF
+'
+
+# Refs of upstream : main(B) next(B)
+# Refs of workbench: main(A) tags/v123
+# git-push -f : main(A) NULL tags/v123 refs/review/main/topic(A) a/b/c(A)
+test_expect_success "git-push -f ($PROTOCOL/porcelain)" '
+ git -C workbench push --porcelain -f origin \
+ refs/tags/v123 \
+ :refs/heads/next \
+ main \
+ main:refs/review/main/topic \
+ HEAD:refs/heads/a/b/c \
+ >out 2>&1 &&
+ make_user_friendly_and_stable_output <out >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <COMMIT-B> <COMMIT-A> refs/heads/main Z
+ > remote: pre-receive< <COMMIT-B> <ZERO-OID> refs/heads/next Z
+ > remote: pre-receive< <ZERO-OID> <TAG-v123> refs/tags/v123 Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/review/main/topic Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/a/b/c Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <COMMIT-B> <COMMIT-A> refs/heads/main Z
+ > remote: post-receive< <COMMIT-B> <ZERO-OID> refs/heads/next Z
+ > remote: post-receive< <ZERO-OID> <TAG-v123> refs/tags/v123 Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/review/main/topic Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/a/b/c Z
+ > To <URL/of/upstream.git>
+ > + refs/heads/main:refs/heads/main <COMMIT-B>...<COMMIT-A> (forced update)
+ > - :refs/heads/next [deleted]
+ > * refs/tags/v123:refs/tags/v123 [new tag]
+ > * refs/heads/main:refs/review/main/topic [new reference]
+ > * HEAD:refs/heads/a/b/c [new branch]
+ > Done
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/a/b/c
+ <COMMIT-A> refs/heads/main
+ <COMMIT-A> refs/review/main/topic
+ <TAG-v123> refs/tags/v123
+ EOF
+'
+
+# Refs of upstream : main(A) tags/v123 refs/review/main/topic(A) a/b/c(A)
+# Refs of workbench: main(A) tags/v123
+test_expect_success "cleanup ($PROTOCOL/porcelain)" '
+ (
+ cd "$upstream" &&
+ git update-ref -d refs/review/main/topic &&
+ git update-ref -d refs/tags/v123 &&
+ git update-ref -d refs/heads/a/b/c
+ )
+'
diff --git a/t/t5411/test-0002-pre-receive-declined.sh b/t/t5411/test-0002-pre-receive-declined.sh
new file mode 100644
index 0000000..98a9d13
--- /dev/null
+++ b/t/t5411/test-0002-pre-receive-declined.sh
@@ -0,0 +1,31 @@
+test_expect_success "setup pre-receive hook ($PROTOCOL)" '
+ mv "$upstream/hooks/pre-receive" "$upstream/hooks/pre-receive.ok" &&
+ test_hook -C "$upstream" --clobber pre-receive <<-\EOF
+ exit 1
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git-push : main(B) next(A)
+test_expect_success "git-push is declined ($PROTOCOL)" '
+ test_must_fail git -C workbench push origin \
+ $B:refs/heads/main \
+ HEAD:refs/heads/next \
+ >out-$test_count 2>&1 &&
+ make_user_friendly_and_stable_output <out-$test_count >actual &&
+ cat >expect <<-EOF &&
+ To <URL/of/upstream.git>
+ ! [remote rejected] <COMMIT-B> -> main (pre-receive hook declined)
+ ! [remote rejected] HEAD -> next (pre-receive hook declined)
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-\EOF
+ <COMMIT-A> refs/heads/main
+ EOF
+'
+
+test_expect_success "cleanup ($PROTOCOL)" '
+ mv "$upstream/hooks/pre-receive.ok" "$upstream/hooks/pre-receive"
+'
diff --git a/t/t5411/test-0003-pre-receive-declined--porcelain.sh b/t/t5411/test-0003-pre-receive-declined--porcelain.sh
new file mode 100644
index 0000000..67ca6dc
--- /dev/null
+++ b/t/t5411/test-0003-pre-receive-declined--porcelain.sh
@@ -0,0 +1,32 @@
+test_expect_success "setup pre-receive hook ($PROTOCOL/porcelain)" '
+ mv "$upstream/hooks/pre-receive" "$upstream/hooks/pre-receive.ok" &&
+ test_hook -C "$upstream" --clobber pre-receive <<-\EOF
+ exit 1
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git-push : main(B) next(A)
+test_expect_success "git-push is declined ($PROTOCOL/porcelain)" '
+ test_must_fail git -C workbench push --porcelain origin \
+ $B:refs/heads/main \
+ HEAD:refs/heads/next \
+ >out-$test_count 2>&1 &&
+ make_user_friendly_and_stable_output <out-$test_count >actual &&
+ format_and_save_expect <<-EOF &&
+ > To <URL/of/upstream.git>
+ > ! <COMMIT-B>:refs/heads/main [remote rejected] (pre-receive hook declined)
+ > ! HEAD:refs/heads/next [remote rejected] (pre-receive hook declined)
+ Done
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ EOF
+'
+
+test_expect_success "cleanup ($PROTOCOL/porcelain)" '
+ mv "$upstream/hooks/pre-receive.ok" "$upstream/hooks/pre-receive"
+'
diff --git a/t/t5411/test-0010-proc-receive-settings.sh b/t/t5411/test-0010-proc-receive-settings.sh
new file mode 100644
index 0000000..a368099
--- /dev/null
+++ b/t/t5411/test-0010-proc-receive-settings.sh
@@ -0,0 +1,7 @@
+test_expect_success "add two receive.procReceiveRefs settings" '
+ (
+ cd "$upstream" &&
+ git config --add receive.procReceiveRefs refs/for &&
+ git config --add receive.procReceiveRefs refs/review/
+ )
+'
diff --git a/t/t5411/test-0011-no-hook-error.sh b/t/t5411/test-0011-no-hook-error.sh
new file mode 100644
index 0000000..d35002b
--- /dev/null
+++ b/t/t5411/test-0011-no-hook-error.sh
@@ -0,0 +1,60 @@
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : next(A) refs/for/main/topic(A)
+test_expect_success "proc-receive: no hook, fail to push special ref ($PROTOCOL)" '
+ test_must_fail git -C workbench push origin \
+ HEAD:next \
+ HEAD:refs/for/main/topic \
+ >out-$test_count 2>&1 &&
+ make_user_friendly_and_stable_output <out-$test_count >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/next Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: error: cannot find hook "proc-receive" Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/next Z
+ > To <URL/of/upstream.git>
+ > * [new branch] HEAD -> next
+ > ! [remote rejected] HEAD -> refs/for/main/topic (fail to run proc-receive hook)
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ <COMMIT-A> refs/heads/next
+ EOF
+'
+
+# Refs of upstream : main(A) next(A)
+# Refs of workbench: main(A) tags/v123
+test_expect_success "cleanup ($PROTOCOL)" '
+ git -C "$upstream" update-ref -d refs/heads/next
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push --atomic: (B) next(A) refs/for/main/topic(A)
+test_expect_success "proc-receive: no hook, all failed for atomic push ($PROTOCOL)" '
+ test_must_fail git -C workbench push --atomic origin \
+ $B:main \
+ HEAD:next \
+ HEAD:refs/for/main/topic >out-$test_count 2>&1 &&
+ make_user_friendly_and_stable_output <out-$test_count >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <COMMIT-A> <COMMIT-B> refs/heads/main Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/next Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: error: cannot find hook "proc-receive" Z
+ > To <URL/of/upstream.git>
+ > ! [remote rejected] <COMMIT-B> -> main (fail to run proc-receive hook)
+ > ! [remote rejected] HEAD -> next (fail to run proc-receive hook)
+ > ! [remote rejected] HEAD -> refs/for/main/topic (fail to run proc-receive hook)
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ EOF
+'
diff --git a/t/t5411/test-0012-no-hook-error--porcelain.sh b/t/t5411/test-0012-no-hook-error--porcelain.sh
new file mode 100644
index 0000000..04468b5
--- /dev/null
+++ b/t/t5411/test-0012-no-hook-error--porcelain.sh
@@ -0,0 +1,62 @@
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : next(A) refs/for/main/topic(A)
+test_expect_success "proc-receive: no hook, fail to push special ref ($PROTOCOL/porcelain)" '
+ test_must_fail git -C workbench push --porcelain origin \
+ HEAD:next \
+ HEAD:refs/for/main/topic \
+ >out-$test_count 2>&1 &&
+ make_user_friendly_and_stable_output <out-$test_count >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/next Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: error: cannot find hook "proc-receive" Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/next Z
+ > To <URL/of/upstream.git>
+ > * HEAD:refs/heads/next [new branch]
+ > ! HEAD:refs/for/main/topic [remote rejected] (fail to run proc-receive hook)
+ Done
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ <COMMIT-A> refs/heads/next
+ EOF
+'
+
+# Refs of upstream : main(A) next(A)
+# Refs of workbench: main(A) tags/v123
+test_expect_success "cleanup ($PROTOCOL/porcelain)" '
+ git -C "$upstream" update-ref -d refs/heads/next
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push --atomic: (B) next(A) refs/for/main/topic(A)
+test_expect_success "proc-receive: no hook, all failed for atomic push ($PROTOCOL/porcelain)" '
+ test_must_fail git -C workbench push --porcelain --atomic origin \
+ $B:main \
+ HEAD:next \
+ HEAD:refs/for/main/topic >out-$test_count 2>&1 &&
+ make_user_friendly_and_stable_output <out-$test_count >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <COMMIT-A> <COMMIT-B> refs/heads/main Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/next Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: error: cannot find hook "proc-receive" Z
+ > To <URL/of/upstream.git>
+ > ! <COMMIT-B>:refs/heads/main [remote rejected] (fail to run proc-receive hook)
+ > ! HEAD:refs/heads/next [remote rejected] (fail to run proc-receive hook)
+ > ! HEAD:refs/for/main/topic [remote rejected] (fail to run proc-receive hook)
+ > Done
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ EOF
+'
diff --git a/t/t5411/test-0013-bad-protocol.sh b/t/t5411/test-0013-bad-protocol.sh
new file mode 100644
index 0000000..8d22e17
--- /dev/null
+++ b/t/t5411/test-0013-bad-protocol.sh
@@ -0,0 +1,302 @@
+test_expect_success "setup proc-receive hook (unknown version, $PROTOCOL)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-\EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v --version 2
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/main/topic(A)
+test_expect_success "proc-receive: bad protocol (unknown version, $PROTOCOL)" '
+ test_must_fail git -C workbench push origin \
+ HEAD:refs/for/main/topic \
+ >out-$test_count 2>&1 &&
+ make_user_friendly_and_stable_output <out-$test_count >actual &&
+
+ # Check status report for git-push
+ sed -n \
+ -e "/^To / { p; }" \
+ -e "/^ ! / { p; }" \
+ <actual >actual-report &&
+ cat >expect <<-EOF &&
+ To <URL/of/upstream.git>
+ ! [remote rejected] HEAD -> refs/for/main/topic (fail to run proc-receive hook)
+ EOF
+ test_cmp expect actual-report &&
+
+ # Check error message from "receive-pack", but ignore unstable fatal error
+ # message ("remote: fatal: the remote end hung up unexpectedly") which
+ # is different from the remote HTTP server with different locale settings.
+ grep "^remote: error:" <actual >actual-error &&
+ format_and_save_expect <<-EOF &&
+ > remote: error: proc-receive version "2" is not supported Z
+ EOF
+ test_cmp expect actual-error &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ EOF
+'
+
+test_expect_success "setup proc-receive hook (hook --die-read-version, $PROTOCOL)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-\EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v --die-read-version
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/main/topic(A)
+test_expect_success "proc-receive: bad protocol (hook --die-read-version, $PROTOCOL)" '
+ test_must_fail git -C workbench push origin \
+ HEAD:refs/for/main/topic \
+ >out-$test_count 2>&1 &&
+ filter_out_user_friendly_and_stable_output \
+ -e "/^To / { p; }" \
+ -e "/^ ! / { p; }" \
+ <out-$test_count >actual &&
+ cat >expect <<-EOF &&
+ To <URL/of/upstream.git>
+ ! [remote rejected] HEAD -> refs/for/main/topic (fail to run proc-receive hook)
+ EOF
+ test_cmp expect actual &&
+ grep "remote: fatal: die with the --die-read-version option" out-$test_count &&
+ grep "remote: error: fail to negotiate version with proc-receive hook" out-$test_count &&
+
+ test_cmp_refs -C "$upstream" <<-\EOF
+ <COMMIT-A> refs/heads/main
+ EOF
+'
+
+test_expect_success "setup proc-receive hook (hook --die-write-version, $PROTOCOL)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-\EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v --die-write-version
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/main/topic(A)
+test_expect_success "proc-receive: bad protocol (hook --die-write-version, $PROTOCOL)" '
+ test_must_fail git -C workbench push origin \
+ HEAD:refs/for/main/topic \
+ >out-$test_count 2>&1 &&
+ filter_out_user_friendly_and_stable_output \
+ -e "/^To / { p; }" \
+ -e "/^ ! / { p; }" \
+ <out-$test_count >actual &&
+ cat >expect <<-EOF &&
+ To <URL/of/upstream.git>
+ ! [remote rejected] HEAD -> refs/for/main/topic (fail to run proc-receive hook)
+ EOF
+ test_cmp expect actual &&
+ grep "remote: fatal: die with the --die-write-version option" out-$test_count &&
+ grep "remote: error: fail to negotiate version with proc-receive hook" out-$test_count &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ EOF
+'
+
+test_expect_success "setup proc-receive hook (hook --die-read-commands, $PROTOCOL)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-\EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v --die-read-commands
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/main/topic(A)
+test_expect_success "proc-receive: bad protocol (hook --die-read-commands, $PROTOCOL)" '
+ test_must_fail git -C workbench push origin \
+ HEAD:refs/for/main/topic \
+ >out-$test_count 2>&1 &&
+ filter_out_user_friendly_and_stable_output \
+ -e "/^To / { p; }" \
+ -e "/^ ! / { p; }" \
+ <out-$test_count >actual &&
+ cat >expect <<-EOF &&
+ To <URL/of/upstream.git>
+ ! [remote rejected] HEAD -> refs/for/main/topic (fail to run proc-receive hook)
+ EOF
+ test_cmp expect actual &&
+ grep "remote: fatal: die with the --die-read-commands option" out-$test_count &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ EOF
+'
+
+test_expect_success "setup proc-receive hook (hook --die-read-push-options, $PROTOCOL)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-\EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v --die-read-push-options
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/main/topic(A)
+test_expect_success "proc-receive: bad protocol (hook --die-read-push-options, $PROTOCOL)" '
+ git -C "$upstream" config receive.advertisePushOptions true &&
+ test_must_fail git -C workbench push origin \
+ -o reviewers=user1,user2 \
+ HEAD:refs/for/main/topic \
+ >out-$test_count 2>&1 &&
+ filter_out_user_friendly_and_stable_output \
+ -e "/^To / { p; }" \
+ -e "/^ ! / { p; }" \
+ <out-$test_count >actual &&
+ cat >expect <<-EOF &&
+ To <URL/of/upstream.git>
+ ! [remote rejected] HEAD -> refs/for/main/topic (fail to run proc-receive hook)
+ EOF
+ test_cmp expect actual &&
+ grep "remote: fatal: die with the --die-read-push-options option" out-$test_count &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ EOF
+'
+
+test_expect_success "setup proc-receive hook (hook --die-write-report, $PROTOCOL)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-\EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v --die-write-report
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/main/topic(A)
+test_expect_success "proc-receive: bad protocol (hook --die-write-report, $PROTOCOL)" '
+ test_must_fail git -C workbench push origin \
+ HEAD:refs/for/main/topic \
+ >out-$test_count 2>&1 &&
+ filter_out_user_friendly_and_stable_output \
+ -e "/^To / { p; }" \
+ -e "/^ ! / { p; }" \
+ <out-$test_count >actual &&
+ cat >expect <<-EOF &&
+ To <URL/of/upstream.git>
+ ! [remote rejected] HEAD -> refs/for/main/topic (fail to run proc-receive hook)
+ EOF
+ test_cmp expect actual &&
+ grep "remote: fatal: die with the --die-write-report option" out-$test_count &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ EOF
+'
+
+test_expect_success "setup proc-receive hook (no report, $PROTOCOL)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-\EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : next(A) refs/for/main/topic(A)
+test_expect_success "proc-receive: bad protocol (no report, $PROTOCOL)" '
+ test_must_fail git -C workbench push origin \
+ HEAD:refs/heads/next \
+ HEAD:refs/for/main/topic >out-$test_count 2>&1 &&
+ make_user_friendly_and_stable_output <out-$test_count >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/next Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/next Z
+ > To <URL/of/upstream.git>
+ > * [new branch] HEAD -> next
+ > ! [remote rejected] HEAD -> refs/for/main/topic (proc-receive failed to report status)
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ <COMMIT-A> refs/heads/next
+ EOF
+'
+
+# Refs of upstream : main(A) next(A)
+# Refs of workbench: main(A) tags/v123
+test_expect_success "cleanup ($PROTOCOL)" '
+ git -C "$upstream" update-ref -d refs/heads/next
+
+'
+
+test_expect_success "setup proc-receive hook (no ref, $PROTOCOL)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-\EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ok"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/main/topic
+test_expect_success "proc-receive: bad protocol (no ref, $PROTOCOL)" '
+ test_must_fail git -C workbench push origin \
+ HEAD:refs/for/main/topic\
+ >out-$test_count 2>&1 &&
+ make_user_friendly_and_stable_output <out-$test_count >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: proc-receive> ok Z
+ > remote: error: proc-receive reported incomplete status line: "ok" Z
+ > To <URL/of/upstream.git>
+ > ! [remote rejected] HEAD -> refs/for/main/topic (proc-receive failed to report status)
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ EOF
+'
+
+test_expect_success "setup proc-receive hook (unknown status, $PROTOCOL)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-\EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "xx refs/for/main/topic"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/main/topic
+test_expect_success "proc-receive: bad protocol (unknown status, $PROTOCOL)" '
+ test_must_fail git -C workbench push origin \
+ HEAD:refs/for/main/topic \
+ >out-$test_count 2>&1 &&
+ make_user_friendly_and_stable_output <out-$test_count >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: proc-receive> xx refs/for/main/topic Z
+ > remote: error: proc-receive reported bad status "xx" on ref "refs/for/main/topic" Z
+ > To <URL/of/upstream.git>
+ > ! [remote rejected] HEAD -> refs/for/main/topic (proc-receive failed to report status)
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ EOF
+'
diff --git a/t/t5411/test-0014-bad-protocol--porcelain.sh b/t/t5411/test-0014-bad-protocol--porcelain.sh
new file mode 100644
index 0000000..298a3d1
--- /dev/null
+++ b/t/t5411/test-0014-bad-protocol--porcelain.sh
@@ -0,0 +1,304 @@
+test_expect_success "setup proc-receive hook (unknown version, $PROTOCOL/porcelain)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-\EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v --version 2
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/main/topic(A)
+test_expect_success "proc-receive: bad protocol (unknown version, $PROTOCOL/porcelain)" '
+ test_must_fail git -C workbench push --porcelain origin \
+ HEAD:refs/for/main/topic \
+ >out-$test_count 2>&1 &&
+ make_user_friendly_and_stable_output <out-$test_count >actual &&
+
+ # Check status report for git-push
+ sed -n \
+ -e "/^To / { p; n; p; n; p; }" \
+ <actual >actual-report &&
+ cat >expect <<-EOF &&
+ To <URL/of/upstream.git>
+ ! HEAD:refs/for/main/topic [remote rejected] (fail to run proc-receive hook)
+ Done
+ EOF
+ test_cmp expect actual-report &&
+
+ # Check error message from "receive-pack", but ignore unstable fatal error
+ # message ("remote: fatal: the remote end hung up unexpectedly") which
+ # is different from the remote HTTP server with different locale settings.
+ grep "^remote: error:" <actual >actual-error &&
+ format_and_save_expect <<-EOF &&
+ > remote: error: proc-receive version "2" is not supported Z
+ EOF
+ test_cmp expect actual-error &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ EOF
+'
+
+test_expect_success "setup proc-receive hook (hook --die-read-version, $PROTOCOL/porcelain)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v --die-read-version
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/main/topic(A)
+test_expect_success "proc-receive: bad protocol (hook --die-read-version, $PROTOCOL/porcelain)" '
+ test_must_fail git -C workbench push --porcelain origin \
+ HEAD:refs/for/main/topic \
+ >out-$test_count 2>&1 &&
+ filter_out_user_friendly_and_stable_output \
+ -e "/^To / { p; n; p; n; p; }" \
+ <out-$test_count >actual &&
+ cat >expect <<-EOF &&
+ To <URL/of/upstream.git>
+ ! HEAD:refs/for/main/topic [remote rejected] (fail to run proc-receive hook)
+ Done
+ EOF
+ test_cmp expect actual &&
+ grep "remote: fatal: die with the --die-read-version option" out-$test_count &&
+ grep "remote: error: fail to negotiate version with proc-receive hook" out-$test_count &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ EOF
+'
+
+test_expect_success "setup proc-receive hook (hook --die-write-version, $PROTOCOL/porcelain)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-\EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v --die-write-version
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/main/topic(A)
+test_expect_success "proc-receive: bad protocol (hook --die-write-version, $PROTOCOL/porcelain)" '
+ test_must_fail git -C workbench push --porcelain origin \
+ HEAD:refs/for/main/topic \
+ >out-$test_count 2>&1 &&
+ filter_out_user_friendly_and_stable_output \
+ -e "/^To / { p; n; p; n; p; }" \
+ <out-$test_count >actual &&
+ cat >expect <<-EOF &&
+ To <URL/of/upstream.git>
+ ! HEAD:refs/for/main/topic [remote rejected] (fail to run proc-receive hook)
+ Done
+ EOF
+ test_cmp expect actual &&
+ grep "remote: fatal: die with the --die-write-version option" out-$test_count &&
+ grep "remote: error: fail to negotiate version with proc-receive hook" out-$test_count &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ EOF
+'
+
+test_expect_success "setup proc-receive hook (hook --die-read-commands, $PROTOCOL/porcelain)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-\EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v --die-read-commands
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/main/topic(A)
+test_expect_success "proc-receive: bad protocol (hook --die-read-commands, $PROTOCOL/porcelain)" '
+ test_must_fail git -C workbench push --porcelain origin \
+ HEAD:refs/for/main/topic \
+ >out-$test_count 2>&1 &&
+ filter_out_user_friendly_and_stable_output \
+ -e "/^To / { p; n; p; n; p; }" \
+ <out-$test_count >actual &&
+ cat >expect <<-EOF &&
+ To <URL/of/upstream.git>
+ ! HEAD:refs/for/main/topic [remote rejected] (fail to run proc-receive hook)
+ Done
+ EOF
+ test_cmp expect actual &&
+ grep "remote: fatal: die with the --die-read-commands option" out-$test_count &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ EOF
+'
+
+test_expect_success "setup proc-receive hook (hook --die-read-push-options, $PROTOCOL/porcelain)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-\EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v --die-read-push-options
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/main/topic(A)
+test_expect_success "proc-receive: bad protocol (hook --die-read-push-options, $PROTOCOL/porcelain)" '
+ git -C "$upstream" config receive.advertisePushOptions true &&
+ test_must_fail git -C workbench push --porcelain origin \
+ -o reviewers=user1,user2 \
+ HEAD:refs/for/main/topic \
+ >out-$test_count 2>&1 &&
+ filter_out_user_friendly_and_stable_output \
+ -e "/^To / { p; n; p; n; p; }" \
+ <out-$test_count >actual &&
+ cat >expect <<-EOF &&
+ To <URL/of/upstream.git>
+ ! HEAD:refs/for/main/topic [remote rejected] (fail to run proc-receive hook)
+ Done
+ EOF
+ test_cmp expect actual &&
+ grep "remote: fatal: die with the --die-read-push-options option" out-$test_count &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ EOF
+'
+
+test_expect_success "setup proc-receive hook (hook --die-write-report, $PROTOCOL/porcelain)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-\EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v --die-write-report
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/main/topic(A)
+test_expect_success "proc-receive: bad protocol (hook --die-write-report, $PROTOCOL/porcelain)" '
+ test_must_fail git -C workbench push --porcelain origin \
+ HEAD:refs/for/main/topic \
+ >out-$test_count 2>&1 &&
+ filter_out_user_friendly_and_stable_output \
+ -e "/^To / { p; n; p; n; p; }" \
+ <out-$test_count >actual &&
+ cat >expect <<-EOF &&
+ To <URL/of/upstream.git>
+ ! HEAD:refs/for/main/topic [remote rejected] (fail to run proc-receive hook)
+ Done
+ EOF
+ test_cmp expect actual &&
+ grep "remote: fatal: die with the --die-write-report option" out-$test_count &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ EOF
+'
+
+test_expect_success "setup proc-receive hook (no report, $PROTOCOL/porcelain)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-\EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : next(A) refs/for/main/topic(A)
+test_expect_success "proc-receive: bad protocol (no report, $PROTOCOL/porcelain)" '
+ test_must_fail git -C workbench push --porcelain origin \
+ HEAD:refs/heads/next \
+ HEAD:refs/for/main/topic >out-$test_count 2>&1 &&
+ make_user_friendly_and_stable_output <out-$test_count >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/next Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/next Z
+ > To <URL/of/upstream.git>
+ > * HEAD:refs/heads/next [new branch]
+ > ! HEAD:refs/for/main/topic [remote rejected] (proc-receive failed to report status)
+ > Done
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ <COMMIT-A> refs/heads/next
+ EOF
+'
+
+# Refs of upstream : main(A) next(A)
+# Refs of workbench: main(A) tags/v123
+test_expect_success "cleanup ($PROTOCOL/porcelain)" '
+ git -C "$upstream" update-ref -d refs/heads/next
+'
+
+test_expect_success "setup proc-receive hook (no ref, $PROTOCOL/porcelain)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-\EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ok"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/main/topic
+test_expect_success "proc-receive: bad protocol (no ref, $PROTOCOL/porcelain)" '
+ test_must_fail git -C workbench push --porcelain origin \
+ HEAD:refs/for/main/topic\
+ >out-$test_count 2>&1 &&
+ make_user_friendly_and_stable_output <out-$test_count >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: proc-receive> ok Z
+ > remote: error: proc-receive reported incomplete status line: "ok" Z
+ > To <URL/of/upstream.git>
+ > ! HEAD:refs/for/main/topic [remote rejected] (proc-receive failed to report status)
+ > Done
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ EOF
+'
+
+test_expect_success "setup proc-receive hook (unknown status, $PROTOCOL/porcelain)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-\EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "xx refs/for/main/topic"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/main/topic
+test_expect_success "proc-receive: bad protocol (unknown status, $PROTOCOL/porcelain)" '
+ test_must_fail git -C workbench push --porcelain origin \
+ HEAD:refs/for/main/topic \
+ >out-$test_count 2>&1 &&
+ make_user_friendly_and_stable_output <out-$test_count >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: proc-receive> xx refs/for/main/topic Z
+ > remote: error: proc-receive reported bad status "xx" on ref "refs/for/main/topic" Z
+ > To <URL/of/upstream.git>
+ > ! HEAD:refs/for/main/topic [remote rejected] (proc-receive failed to report status)
+ > Done
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ EOF
+'
diff --git a/t/t5411/test-0020-report-ng.sh b/t/t5411/test-0020-report-ng.sh
new file mode 100644
index 0000000..6347c96
--- /dev/null
+++ b/t/t5411/test-0020-report-ng.sh
@@ -0,0 +1,63 @@
+test_expect_success "setup proc-receive hook (ng, no message, $PROTOCOL)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-\EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ng refs/for/main/topic"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/main/topic
+test_expect_success "proc-receive: fail to update (ng, no message, $PROTOCOL)" '
+ test_must_fail git -C workbench push origin \
+ HEAD:refs/for/main/topic \
+ >out-$test_count 2>&1 &&
+ make_user_friendly_and_stable_output <out-$test_count >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: proc-receive> ng refs/for/main/topic Z
+ > To <URL/of/upstream.git>
+ > ! [remote rejected] HEAD -> refs/for/main/topic (failed)
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ EOF
+'
+
+test_expect_success "setup proc-receive hook (ng message, $PROTOCOL)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-\EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ng refs/for/main/topic error msg"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/main/topic
+test_expect_success "proc-receive: fail to update (ng, with message, $PROTOCOL)" '
+ test_must_fail git -C workbench push origin \
+ HEAD:refs/for/main/topic \
+ >out-$test_count 2>&1 &&
+ make_user_friendly_and_stable_output <out-$test_count >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: proc-receive> ng refs/for/main/topic error msg Z
+ > To <URL/of/upstream.git>
+ > ! [remote rejected] HEAD -> refs/for/main/topic (error msg)
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ EOF
+'
diff --git a/t/t5411/test-0021-report-ng--porcelain.sh b/t/t5411/test-0021-report-ng--porcelain.sh
new file mode 100644
index 0000000..502b34f
--- /dev/null
+++ b/t/t5411/test-0021-report-ng--porcelain.sh
@@ -0,0 +1,65 @@
+test_expect_success "setup proc-receive hook (ng, no message, $PROTOCOL/porcelain)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-\EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ng refs/for/main/topic"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/main/topic
+test_expect_success "proc-receive: fail to update (ng, no message, $PROTOCOL/porcelain)" '
+ test_must_fail git -C workbench push --porcelain origin \
+ HEAD:refs/for/main/topic \
+ >out-$test_count 2>&1 &&
+ make_user_friendly_and_stable_output <out-$test_count >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: proc-receive> ng refs/for/main/topic Z
+ > To <URL/of/upstream.git>
+ > ! HEAD:refs/for/main/topic [remote rejected] (failed)
+ > Done
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ EOF
+'
+
+test_expect_success "setup proc-receive hook (ng message, $PROTOCOL/porcelain)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-\EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ng refs/for/main/topic error msg"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/main/topic
+test_expect_success "proc-receive: fail to update (ng, with message, $PROTOCOL/porcelain)" '
+ test_must_fail git -C workbench push --porcelain origin \
+ HEAD:refs/for/main/topic \
+ >out-$test_count 2>&1 &&
+ make_user_friendly_and_stable_output <out-$test_count >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: proc-receive> ng refs/for/main/topic error msg Z
+ > To <URL/of/upstream.git>
+ > ! HEAD:refs/for/main/topic [remote rejected] (error msg)
+ > Done
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ EOF
+'
diff --git a/t/t5411/test-0022-report-unexpect-ref.sh b/t/t5411/test-0022-report-unexpect-ref.sh
new file mode 100644
index 0000000..7744392
--- /dev/null
+++ b/t/t5411/test-0022-report-unexpect-ref.sh
@@ -0,0 +1,43 @@
+test_expect_success "setup proc-receive hook (unexpected ref, $PROTOCOL)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-\EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ok refs/heads/main"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : (B) refs/for/main/topic
+test_expect_success "proc-receive: report unexpected ref ($PROTOCOL)" '
+ test_must_fail git -C workbench push origin \
+ $B:refs/heads/main \
+ HEAD:refs/for/main/topic \
+ >out-$test_count 2>&1 &&
+ make_user_friendly_and_stable_output <out-$test_count >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <COMMIT-A> <COMMIT-B> refs/heads/main Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: proc-receive> ok refs/heads/main Z
+ > remote: error: proc-receive reported status on unexpected ref: refs/heads/main Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <COMMIT-A> <COMMIT-B> refs/heads/main Z
+ > To <URL/of/upstream.git>
+ > <COMMIT-A>..<COMMIT-B> <COMMIT-B> -> main
+ > ! [remote rejected] HEAD -> refs/for/main/topic (proc-receive failed to report status)
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-B> refs/heads/main
+ EOF
+'
+
+# Refs of upstream : main(B)
+# Refs of workbench: main(A) tags/v123
+test_expect_success "cleanup ($PROTOCOL)" '
+ git -C "$upstream" update-ref refs/heads/main $A
+'
diff --git a/t/t5411/test-0023-report-unexpect-ref--porcelain.sh b/t/t5411/test-0023-report-unexpect-ref--porcelain.sh
new file mode 100644
index 0000000..6d116ef
--- /dev/null
+++ b/t/t5411/test-0023-report-unexpect-ref--porcelain.sh
@@ -0,0 +1,44 @@
+test_expect_success "setup proc-receive hook (unexpected ref, $PROTOCOL/porcelain)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-\EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ok refs/heads/main"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : (B) refs/for/main/topic
+test_expect_success "proc-receive: report unexpected ref ($PROTOCOL/porcelain)" '
+ test_must_fail git -C workbench push --porcelain origin \
+ $B:refs/heads/main \
+ HEAD:refs/for/main/topic \
+ >out-$test_count 2>&1 &&
+ make_user_friendly_and_stable_output <out-$test_count >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <COMMIT-A> <COMMIT-B> refs/heads/main Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: proc-receive> ok refs/heads/main Z
+ > remote: error: proc-receive reported status on unexpected ref: refs/heads/main Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <COMMIT-A> <COMMIT-B> refs/heads/main Z
+ > To <URL/of/upstream.git>
+ > <COMMIT-B>:refs/heads/main <COMMIT-A>..<COMMIT-B>
+ > ! HEAD:refs/for/main/topic [remote rejected] (proc-receive failed to report status)
+ > Done
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-B> refs/heads/main
+ EOF
+'
+
+# Refs of upstream : main(B)
+# Refs of workbench: main(A) tags/v123
+test_expect_success "cleanup ($PROTOCOL/porcelain)" '
+ git -C "$upstream" update-ref refs/heads/main $A
+'
diff --git a/t/t5411/test-0024-report-unknown-ref.sh b/t/t5411/test-0024-report-unknown-ref.sh
new file mode 100644
index 0000000..619ca2f
--- /dev/null
+++ b/t/t5411/test-0024-report-unknown-ref.sh
@@ -0,0 +1,32 @@
+test_expect_success "setup proc-receive hook (unexpected ref, $PROTOCOL)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-\EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ok refs/for/main/topic"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/a/b/c/my/topic
+test_expect_success "proc-receive: report unknown reference ($PROTOCOL)" '
+ test_must_fail git -C workbench push origin \
+ HEAD:refs/for/a/b/c/my/topic \
+ >out-$test_count 2>&1 &&
+ make_user_friendly_and_stable_output <out-$test_count >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/a/b/c/my/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/a/b/c/my/topic Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: error: proc-receive reported status on unknown ref: refs/for/main/topic Z
+ > To <URL/of/upstream.git>
+ > ! [remote rejected] HEAD -> refs/for/a/b/c/my/topic (proc-receive failed to report status)
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ EOF
+'
diff --git a/t/t5411/test-0025-report-unknown-ref--porcelain.sh b/t/t5411/test-0025-report-unknown-ref--porcelain.sh
new file mode 100644
index 0000000..8b3f5d0
--- /dev/null
+++ b/t/t5411/test-0025-report-unknown-ref--porcelain.sh
@@ -0,0 +1,33 @@
+test_expect_success "setup proc-receive hook (unexpected ref, $PROTOCOL/porcelain)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-\EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ok refs/for/main/topic"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/a/b/c/my/topic
+test_expect_success "proc-receive: report unknown reference ($PROTOCOL/porcelain)" '
+ test_must_fail git -C workbench push --porcelain origin \
+ HEAD:refs/for/a/b/c/my/topic \
+ >out-$test_count 2>&1 &&
+ make_user_friendly_and_stable_output <out-$test_count >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/a/b/c/my/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/a/b/c/my/topic Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: error: proc-receive reported status on unknown ref: refs/for/main/topic Z
+ > To <URL/of/upstream.git>
+ > ! HEAD:refs/for/a/b/c/my/topic [remote rejected] (proc-receive failed to report status)
+ > Done
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ EOF
+'
diff --git a/t/t5411/test-0026-push-options.sh b/t/t5411/test-0026-push-options.sh
new file mode 100644
index 0000000..510fff3
--- /dev/null
+++ b/t/t5411/test-0026-push-options.sh
@@ -0,0 +1,133 @@
+test_expect_success "setup proc-receive hook and disable push-options ($PROTOCOL)" '
+ git -C "$upstream" config receive.advertisePushOptions false &&
+ test_hook -C "$upstream" --clobber proc-receive <<-\EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ok refs/for/main/topic"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push -o ... : refs/for/main/topic
+test_expect_success "proc-receive: not support push options ($PROTOCOL)" '
+ test_must_fail git -C workbench push \
+ -o issue=123 \
+ -o reviewer=user1 \
+ origin \
+ HEAD:refs/for/main/topic \
+ >out-$test_count 2>&1 &&
+ make_user_friendly_and_stable_output <out-$test_count >actual &&
+ test_grep "fatal: the receiving end does not support push options" \
+ actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ EOF
+'
+
+test_expect_success "enable push options ($PROTOCOL)" '
+ git -C "$upstream" config receive.advertisePushOptions true
+'
+
+test_expect_success "setup version=0 for proc-receive hook ($PROTOCOL)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-\EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ --version 0 \
+ -r "ok refs/for/main/topic"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push -o ... : next(A) refs/for/main/topic
+test_expect_success "proc-receive: ignore push-options for version 0 ($PROTOCOL)" '
+ git -C workbench push \
+ --atomic \
+ -o issue=123 \
+ -o reviewer=user1 \
+ origin \
+ HEAD:refs/heads/next \
+ HEAD:refs/for/main/topic \
+ >out 2>&1 &&
+ make_user_friendly_and_stable_output <out >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/next Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/next Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > To <URL/of/upstream.git>
+ > * [new branch] HEAD -> next
+ > * [new reference] HEAD -> refs/for/main/topic
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ <COMMIT-A> refs/heads/next
+ EOF
+'
+
+test_expect_success "restore proc-receive hook ($PROTOCOL)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-\EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ok refs/for/main/topic"
+ EOF
+'
+
+# Refs of upstream : main(A) next(A)
+# Refs of workbench: main(A) tags/v123
+test_expect_success "cleanup ($PROTOCOL)" '
+ git -C "$upstream" update-ref -d refs/heads/next
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push -o ... : next(A) refs/for/main/topic
+test_expect_success "proc-receive: push with options ($PROTOCOL)" '
+ git -C workbench push \
+ --atomic \
+ -o issue=123 \
+ -o reviewer=user1 \
+ origin \
+ HEAD:refs/heads/next \
+ HEAD:refs/for/main/topic \
+ >out 2>&1 &&
+ make_user_friendly_and_stable_output <out >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/next Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive: atomic push_options Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: proc-receive< issue=123 Z
+ > remote: proc-receive< reviewer=user1 Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/next Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > To <URL/of/upstream.git>
+ > * [new branch] HEAD -> next
+ > * [new reference] HEAD -> refs/for/main/topic
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ <COMMIT-A> refs/heads/next
+ EOF
+'
+
+# Refs of upstream : main(A) next(A)
+# Refs of workbench: main(A) tags/v123
+test_expect_success "cleanup ($PROTOCOL)" '
+ git -C "$upstream" update-ref -d refs/heads/next
+'
diff --git a/t/t5411/test-0027-push-options--porcelain.sh b/t/t5411/test-0027-push-options--porcelain.sh
new file mode 100644
index 0000000..9435457
--- /dev/null
+++ b/t/t5411/test-0027-push-options--porcelain.sh
@@ -0,0 +1,138 @@
+test_expect_success "setup proc-receive hook and disable push-options ($PROTOCOL/porcelain)" '
+ git -C "$upstream" config receive.advertisePushOptions false &&
+ test_hook -C "$upstream" --clobber proc-receive <<-\EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ok refs/for/main/topic"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push -o ... : refs/for/main/topic
+test_expect_success "proc-receive: not support push options ($PROTOCOL/porcelain)" '
+ test_must_fail git -C workbench push \
+ --porcelain \
+ -o issue=123 \
+ -o reviewer=user1 \
+ origin \
+ HEAD:refs/for/main/topic \
+ >out-$test_count 2>&1 &&
+ make_user_friendly_and_stable_output <out-$test_count >actual &&
+ test_grep "fatal: the receiving end does not support push options" \
+ actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ EOF
+'
+
+test_expect_success "enable push options ($PROTOCOL/porcelain)" '
+ git -C "$upstream" config receive.advertisePushOptions true
+'
+
+test_expect_success "setup version=0 for proc-receive hook ($PROTOCOL/porcelain)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-\EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ --version 0 \
+ -r "ok refs/for/main/topic"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push -o ... : next(A) refs/for/main/topic
+test_expect_success "proc-receive: ignore push-options for version 0 ($PROTOCOL/porcelain)" '
+ git -C workbench push \
+ --porcelain \
+ --atomic \
+ -o issue=123 \
+ -o reviewer=user1 \
+ origin \
+ HEAD:refs/heads/next \
+ HEAD:refs/for/main/topic \
+ >out 2>&1 &&
+ make_user_friendly_and_stable_output <out >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/next Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/next Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > To <URL/of/upstream.git>
+ > * HEAD:refs/heads/next [new branch]
+ > * HEAD:refs/for/main/topic [new reference]
+ > Done
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ <COMMIT-A> refs/heads/next
+ EOF
+'
+
+test_expect_success "restore proc-receive hook ($PROTOCOL/porcelain)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-\EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ok refs/for/main/topic"
+ EOF
+'
+
+# Refs of upstream : main(A) next(A)
+# Refs of workbench: main(A) tags/v123
+test_expect_success "cleanup ($PROTOCOL/porcelain)" '
+ git -C "$upstream" update-ref -d refs/heads/next
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push -o ... : next(A) refs/for/main/topic
+test_expect_success "proc-receive: push with options ($PROTOCOL/porcelain)" '
+ git -C workbench push \
+ --porcelain \
+ --atomic \
+ -o issue=123 \
+ -o reviewer=user1 \
+ origin \
+ HEAD:refs/heads/next \
+ HEAD:refs/for/main/topic \
+ >out 2>&1 &&
+ make_user_friendly_and_stable_output <out >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/next Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive: atomic push_options Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: proc-receive< issue=123 Z
+ > remote: proc-receive< reviewer=user1 Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/next Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > To <URL/of/upstream.git>
+ > * HEAD:refs/heads/next [new branch]
+ > * HEAD:refs/for/main/topic [new reference]
+ > Done
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ <COMMIT-A> refs/heads/next
+ EOF
+'
+
+# Refs of upstream : main(A) next(A)
+# Refs of workbench: main(A) tags/v123
+test_expect_success "cleanup ($PROTOCOL/porcelain)" '
+ git -C "$upstream" update-ref -d refs/heads/next
+'
diff --git a/t/t5411/test-0030-report-ok.sh b/t/t5411/test-0030-report-ok.sh
new file mode 100644
index 0000000..0f190a6
--- /dev/null
+++ b/t/t5411/test-0030-report-ok.sh
@@ -0,0 +1,33 @@
+test_expect_success "setup proc-receive hook (ok, $PROTOCOL)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-\EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ok refs/for/main/topic"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/main/topic
+test_expect_success "proc-receive: ok ($PROTOCOL)" '
+ git -C workbench push origin \
+ HEAD:refs/for/main/topic \
+ >out 2>&1 &&
+ make_user_friendly_and_stable_output <out >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > To <URL/of/upstream.git>
+ > * [new reference] HEAD -> refs/for/main/topic
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ EOF
+'
diff --git a/t/t5411/test-0031-report-ok--porcelain.sh b/t/t5411/test-0031-report-ok--porcelain.sh
new file mode 100644
index 0000000..7ec3981
--- /dev/null
+++ b/t/t5411/test-0031-report-ok--porcelain.sh
@@ -0,0 +1,34 @@
+test_expect_success "setup proc-receive hook (ok, $PROTOCOL/porcelain)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-\EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ok refs/for/main/topic"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/main/topic
+test_expect_success "proc-receive: ok ($PROTOCOL/porcelain)" '
+ git -C workbench push --porcelain origin \
+ HEAD:refs/for/main/topic \
+ >out 2>&1 &&
+ make_user_friendly_and_stable_output <out >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > To <URL/of/upstream.git>
+ > * HEAD:refs/for/main/topic [new reference]
+ > Done
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ EOF
+'
diff --git a/t/t5411/test-0032-report-with-options.sh b/t/t5411/test-0032-report-with-options.sh
new file mode 100644
index 0000000..07733b9
--- /dev/null
+++ b/t/t5411/test-0032-report-with-options.sh
@@ -0,0 +1,253 @@
+test_expect_success "setup proc-receive hook (option without matching ok, $PROTOCOL)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "option refname refs/pull/123/head" \
+ -r "option old-oid $B"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/next/topic(A) refs/for/a/b/c/topic(A) refs/for/main/topic(A)
+test_expect_success "proc-receive: report option without matching ok ($PROTOCOL)" '
+ test_must_fail git -C workbench push origin \
+ HEAD:refs/for/main/topic \
+ >out-$test_count 2>&1 &&
+ make_user_friendly_and_stable_output <out-$test_count >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: proc-receive> option refname refs/pull/123/head Z
+ > remote: proc-receive> option old-oid <COMMIT-B> Z
+ > remote: error: proc-receive reported "option" without a matching "ok/ng" directive Z
+ > To <URL/of/upstream.git>
+ > ! [remote rejected] HEAD -> refs/for/main/topic (proc-receive failed to report status)
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success "setup proc-receive hook (option refname, $PROTOCOL)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-\EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ok refs/for/main/topic" \
+ -r "option refname refs/pull/123/head"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/next/topic(A) refs/for/a/b/c/topic(A) refs/for/main/topic(A)
+test_expect_success "proc-receive: report option refname ($PROTOCOL)" '
+ git -C workbench push origin \
+ HEAD:refs/for/main/topic \
+ >out 2>&1 &&
+ make_user_friendly_and_stable_output <out >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: proc-receive> option refname refs/pull/123/head Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/pull/123/head Z
+ > To <URL/of/upstream.git>
+ > * [new reference] HEAD -> refs/pull/123/head
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success "setup proc-receive hook (option refname and forced-update, $PROTOCOL)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-\EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ok refs/for/main/topic" \
+ -r "option refname refs/pull/123/head" \
+ -r "option forced-update"
+ EOF
+'
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/next/topic(A) refs/for/a/b/c/topic(A) refs/for/main/topic(A)
+test_expect_success "proc-receive: report option refname and forced-update ($PROTOCOL)" '
+ git -C workbench push origin \
+ HEAD:refs/for/main/topic \
+ >out 2>&1 &&
+ make_user_friendly_and_stable_output <out >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: proc-receive> option refname refs/pull/123/head Z
+ > remote: proc-receive> option forced-update Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/pull/123/head Z
+ > To <URL/of/upstream.git>
+ > * [new reference] HEAD -> refs/pull/123/head
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success "setup proc-receive hook (option refname and old-oid, $PROTOCOL)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ok refs/for/main/topic" \
+ -r "option refname refs/pull/123/head" \
+ -r "option old-oid $B"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/next/topic(A) refs/for/a/b/c/topic(A) refs/for/main/topic(A)
+test_expect_success "proc-receive: report option refname and old-oid ($PROTOCOL)" '
+ git -C workbench push origin \
+ HEAD:refs/for/main/topic \
+ >out 2>&1 &&
+ make_user_friendly_and_stable_output <out >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: proc-receive> option refname refs/pull/123/head Z
+ > remote: proc-receive> option old-oid <COMMIT-B> Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <COMMIT-B> <COMMIT-A> refs/pull/123/head Z
+ > To <URL/of/upstream.git>
+ > <COMMIT-B>..<COMMIT-A> HEAD -> refs/pull/123/head
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success "setup proc-receive hook (option old-oid, $PROTOCOL)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ok refs/for/main/topic" \
+ -r "option old-oid $B"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/next/topic(A) refs/for/a/b/c/topic(A) refs/for/main/topic(A)
+test_expect_success "proc-receive: report option old-oid ($PROTOCOL)" '
+ git -C workbench push origin \
+ HEAD:refs/for/main/topic \
+ >out 2>&1 &&
+ make_user_friendly_and_stable_output <out >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: proc-receive> option old-oid <COMMIT-B> Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <COMMIT-B> <COMMIT-A> refs/for/main/topic Z
+ > To <URL/of/upstream.git>
+ > <COMMIT-B>..<COMMIT-A> HEAD -> refs/for/main/topic
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success "setup proc-receive hook (option old-oid and new-oid, $PROTOCOL)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ok refs/for/main/topic" \
+ -r "option old-oid $A" \
+ -r "option new-oid $B"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/next/topic(A) refs/for/a/b/c/topic(A) refs/for/main/topic(A)
+test_expect_success "proc-receive: report option old-oid and new-oid ($PROTOCOL)" '
+ git -C workbench push origin \
+ HEAD:refs/for/main/topic \
+ >out 2>&1 &&
+ make_user_friendly_and_stable_output <out >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: proc-receive> option old-oid <COMMIT-A> Z
+ > remote: proc-receive> option new-oid <COMMIT-B> Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <COMMIT-A> <COMMIT-B> refs/for/main/topic Z
+ > To <URL/of/upstream.git>
+ > <COMMIT-A>..<COMMIT-B> HEAD -> refs/for/main/topic
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success "setup proc-receive hook (report with multiple rewrites, $PROTOCOL)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ok refs/for/a/b/c/topic" \
+ -r "ok refs/for/next/topic" \
+ -r "option refname refs/pull/123/head" \
+ -r "ok refs/for/main/topic" \
+ -r "option refname refs/pull/124/head" \
+ -r "option old-oid $B" \
+ -r "option forced-update" \
+ -r "option new-oid $A"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/next/topic(A) refs/for/a/b/c/topic(A) refs/for/main/topic(A)
+test_expect_success "proc-receive: report with multiple rewrites ($PROTOCOL)" '
+ git -C workbench push origin \
+ HEAD:refs/for/next/topic \
+ HEAD:refs/for/a/b/c/topic \
+ HEAD:refs/for/main/topic \
+ >out 2>&1 &&
+ make_user_friendly_and_stable_output <out >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/a/b/c/topic Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/a/b/c/topic Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: proc-receive> ok refs/for/a/b/c/topic Z
+ > remote: proc-receive> ok refs/for/next/topic Z
+ > remote: proc-receive> option refname refs/pull/123/head Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: proc-receive> option refname refs/pull/124/head Z
+ > remote: proc-receive> option old-oid <COMMIT-B> Z
+ > remote: proc-receive> option forced-update Z
+ > remote: proc-receive> option new-oid <COMMIT-A> Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/pull/123/head Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/a/b/c/topic Z
+ > remote: post-receive< <COMMIT-B> <COMMIT-A> refs/pull/124/head Z
+ > To <URL/of/upstream.git>
+ > * [new reference] HEAD -> refs/pull/123/head
+ > * [new reference] HEAD -> refs/for/a/b/c/topic
+ > + <COMMIT-B>...<COMMIT-A> HEAD -> refs/pull/124/head (forced update)
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ EOF
+'
diff --git a/t/t5411/test-0033-report-with-options--porcelain.sh b/t/t5411/test-0033-report-with-options--porcelain.sh
new file mode 100644
index 0000000..2e1831b
--- /dev/null
+++ b/t/t5411/test-0033-report-with-options--porcelain.sh
@@ -0,0 +1,262 @@
+test_expect_success "setup proc-receive hook (option without matching ok, $PROTOCOL/porcelain)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "option refname refs/pull/123/head" \
+ -r "option old-oid $B"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/next/topic(A) refs/for/a/b/c/topic(A) refs/for/main/topic(A)
+test_expect_success "proc-receive: report option without matching ok ($PROTOCOL/porcelain)" '
+ test_must_fail git -C workbench push --porcelain origin \
+ HEAD:refs/for/main/topic \
+ >out-$test_count 2>&1 &&
+ make_user_friendly_and_stable_output <out-$test_count >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: proc-receive> option refname refs/pull/123/head Z
+ > remote: proc-receive> option old-oid <COMMIT-B> Z
+ > remote: error: proc-receive reported "option" without a matching "ok/ng" directive Z
+ > To <URL/of/upstream.git>
+ > ! HEAD:refs/for/main/topic [remote rejected] (proc-receive failed to report status)
+ > Done
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success "setup proc-receive hook (option refname, $PROTOCOL/porcelain)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-\EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ok refs/for/main/topic" \
+ -r "option refname refs/pull/123/head"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/next/topic(A) refs/for/a/b/c/topic(A) refs/for/main/topic(A)
+test_expect_success "proc-receive: report option refname ($PROTOCOL/porcelain)" '
+ git -C workbench push --porcelain origin \
+ HEAD:refs/for/main/topic \
+ >out 2>&1 &&
+ make_user_friendly_and_stable_output <out >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: proc-receive> option refname refs/pull/123/head Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/pull/123/head Z
+ > To <URL/of/upstream.git>
+ > * HEAD:refs/pull/123/head [new reference]
+ > Done
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success "setup proc-receive hook (option refname and forced-update, $PROTOCOL/porcelain)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-\EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ok refs/for/main/topic" \
+ -r "option refname refs/pull/123/head" \
+ -r "option forced-update"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/next/topic(A) refs/for/a/b/c/topic(A) refs/for/main/topic(A)
+test_expect_success "proc-receive: report option refname and forced-update ($PROTOCOL/porcelain)" '
+ git -C workbench push --porcelain origin \
+ HEAD:refs/for/main/topic \
+ >out 2>&1 &&
+ make_user_friendly_and_stable_output <out >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: proc-receive> option refname refs/pull/123/head Z
+ > remote: proc-receive> option forced-update Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/pull/123/head Z
+ > To <URL/of/upstream.git>
+ > * HEAD:refs/pull/123/head [new reference]
+ > Done
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success "setup proc-receive hook (option refname and old-oid, $PROTOCOL/porcelain)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ok refs/for/main/topic" \
+ -r "option refname refs/pull/123/head" \
+ -r "option old-oid $B"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/next/topic(A) refs/for/a/b/c/topic(A) refs/for/main/topic(A)
+test_expect_success "proc-receive: report option refname and old-oid ($PROTOCOL/porcelain)" '
+ git -C workbench push --porcelain origin \
+ HEAD:refs/for/main/topic \
+ >out 2>&1 &&
+ make_user_friendly_and_stable_output <out >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: proc-receive> option refname refs/pull/123/head Z
+ > remote: proc-receive> option old-oid <COMMIT-B> Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <COMMIT-B> <COMMIT-A> refs/pull/123/head Z
+ > To <URL/of/upstream.git>
+ > HEAD:refs/pull/123/head <COMMIT-B>..<COMMIT-A>
+ > Done
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success "setup proc-receive hook (option old-oid, $PROTOCOL/porcelain)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ok refs/for/main/topic" \
+ -r "option old-oid $B"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/next/topic(A) refs/for/a/b/c/topic(A) refs/for/main/topic(A)
+test_expect_success "proc-receive: report option old-oid ($PROTOCOL/porcelain)" '
+ git -C workbench push --porcelain origin \
+ HEAD:refs/for/main/topic \
+ >out 2>&1 &&
+ make_user_friendly_and_stable_output <out >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: proc-receive> option old-oid <COMMIT-B> Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <COMMIT-B> <COMMIT-A> refs/for/main/topic Z
+ > To <URL/of/upstream.git>
+ > HEAD:refs/for/main/topic <COMMIT-B>..<COMMIT-A>
+ > Done
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success "setup proc-receive hook (option old-oid and new-oid, $PROTOCOL/porcelain)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ok refs/for/main/topic" \
+ -r "option old-oid $A" \
+ -r "option new-oid $B"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/next/topic(A) refs/for/a/b/c/topic(A) refs/for/main/topic(A)
+test_expect_success "proc-receive: report option old-oid and new-oid ($PROTOCOL/porcelain)" '
+ git -C workbench push --porcelain origin \
+ HEAD:refs/for/main/topic \
+ >out 2>&1 &&
+ make_user_friendly_and_stable_output <out >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: proc-receive> option old-oid <COMMIT-A> Z
+ > remote: proc-receive> option new-oid <COMMIT-B> Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <COMMIT-A> <COMMIT-B> refs/for/main/topic Z
+ > To <URL/of/upstream.git>
+ > HEAD:refs/for/main/topic <COMMIT-A>..<COMMIT-B>
+ > Done
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success "setup proc-receive hook (report with multiple rewrites, $PROTOCOL/porcelain)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ok refs/for/a/b/c/topic" \
+ -r "ok refs/for/next/topic" \
+ -r "option refname refs/pull/123/head" \
+ -r "ok refs/for/main/topic" \
+ -r "option refname refs/pull/124/head" \
+ -r "option old-oid $B" \
+ -r "option forced-update" \
+ -r "option new-oid $A"
+
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/next/topic(A) refs/for/a/b/c/topic(A) refs/for/main/topic(A)
+test_expect_success "proc-receive: report with multiple rewrites ($PROTOCOL/porcelain)" '
+ git -C workbench push --porcelain origin \
+ HEAD:refs/for/next/topic \
+ HEAD:refs/for/a/b/c/topic \
+ HEAD:refs/for/main/topic \
+ >out 2>&1 &&
+ make_user_friendly_and_stable_output <out >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/a/b/c/topic Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/a/b/c/topic Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: proc-receive> ok refs/for/a/b/c/topic Z
+ > remote: proc-receive> ok refs/for/next/topic Z
+ > remote: proc-receive> option refname refs/pull/123/head Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: proc-receive> option refname refs/pull/124/head Z
+ > remote: proc-receive> option old-oid <COMMIT-B> Z
+ > remote: proc-receive> option forced-update Z
+ > remote: proc-receive> option new-oid <COMMIT-A> Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/pull/123/head Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/a/b/c/topic Z
+ > remote: post-receive< <COMMIT-B> <COMMIT-A> refs/pull/124/head Z
+ > To <URL/of/upstream.git>
+ > * HEAD:refs/pull/123/head [new reference]
+ > * HEAD:refs/for/a/b/c/topic [new reference]
+ > + HEAD:refs/pull/124/head <COMMIT-B>...<COMMIT-A> (forced update)
+ > Done
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ EOF
+'
diff --git a/t/t5411/test-0034-report-ft.sh b/t/t5411/test-0034-report-ft.sh
new file mode 100644
index 0000000..0e37535
--- /dev/null
+++ b/t/t5411/test-0034-report-ft.sh
@@ -0,0 +1,42 @@
+test_expect_success "setup proc-receive hook (ft, $PROTOCOL)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-\EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ok refs/for/main/topic" \
+ -r "option fall-through"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/main/topic(B)
+test_expect_success "proc-receive: fall throught, let receive-pack to execute ($PROTOCOL)" '
+ git -C workbench push origin \
+ $B:refs/for/main/topic \
+ >out 2>&1 &&
+ make_user_friendly_and_stable_output <out >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-B> refs/for/main/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-B> refs/for/main/topic Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: proc-receive> option fall-through Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-B> refs/for/main/topic Z
+ > To <URL/of/upstream.git>
+ > * [new reference] <COMMIT-B> -> refs/for/main/topic
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-B> refs/for/main/topic
+ <COMMIT-A> refs/heads/main
+ EOF
+'
+
+# Refs of upstream : main(A) refs/for/main/topic(A)
+# Refs of workbench: main(A) tags/v123
+test_expect_success "cleanup ($PROTOCOL)" '
+ git -C "$upstream" update-ref -d refs/for/main/topic
+'
diff --git a/t/t5411/test-0035-report-ft--porcelain.sh b/t/t5411/test-0035-report-ft--porcelain.sh
new file mode 100644
index 0000000..b9a0518
--- /dev/null
+++ b/t/t5411/test-0035-report-ft--porcelain.sh
@@ -0,0 +1,43 @@
+test_expect_success "setup proc-receive hook (fall-through, $PROTOCOL/porcelain)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-\EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ok refs/for/main/topic" \
+ -r "option fall-through"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/main/topic(B)
+test_expect_success "proc-receive: fall throught, let receive-pack to execute ($PROTOCOL/porcelain)" '
+ git -C workbench push --porcelain origin \
+ $B:refs/for/main/topic \
+ >out 2>&1 &&
+ make_user_friendly_and_stable_output <out >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-B> refs/for/main/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-B> refs/for/main/topic Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: proc-receive> option fall-through Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-B> refs/for/main/topic Z
+ > To <URL/of/upstream.git>
+ > * <COMMIT-B>:refs/for/main/topic [new reference]
+ > Done
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-B> refs/for/main/topic
+ <COMMIT-A> refs/heads/main
+ EOF
+'
+
+# Refs of upstream : main(A) refs/for/main/topic(A)
+# Refs of workbench: main(A) tags/v123
+test_expect_success "cleanup ($PROTOCOL/porcelain)" '
+ git -C "$upstream" update-ref -d refs/for/main/topic
+'
diff --git a/t/t5411/test-0036-report-multi-rewrite-for-one-ref.sh b/t/t5411/test-0036-report-multi-rewrite-for-one-ref.sh
new file mode 100644
index 0000000..889e970
--- /dev/null
+++ b/t/t5411/test-0036-report-multi-rewrite-for-one-ref.sh
@@ -0,0 +1,221 @@
+test_expect_success "setup git config for remote-tracking of special refs" '
+ (
+ cd workbench &&
+ if ! git config --get-all remote.origin.fetch | grep refs/for/
+ then
+ git config --add remote.origin.fetch \
+ "+refs/for/*:refs/t/for/*" &&
+ git config --add remote.origin.fetch \
+ "+refs/pull/*:refs/t/pull/*" &&
+ git config --add remote.origin.fetch \
+ "+refs/changes/*:refs/t/changes/*"
+ fi
+ )
+'
+
+test_expect_success "setup proc-receive hook (multiple rewrites for one ref, no refname for the 1st rewrite, $PROTOCOL)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ok refs/for/main/topic" \
+ -r "option old-oid $A" \
+ -r "option new-oid $B" \
+ -r "ok refs/for/main/topic" \
+ -r "option refname refs/changes/24/124/1" \
+ -r "option old-oid $ZERO_OID" \
+ -r "option new-oid $A" \
+ -r "ok refs/for/main/topic" \
+ -r "option refname refs/changes/25/125/1" \
+ -r "option old-oid $A" \
+ -r "option new-oid $B"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/main/topic(A)
+test_expect_success "proc-receive: multiple rewrite for one ref, no refname for the 1st rewrite ($PROTOCOL)" '
+ git -C workbench push origin \
+ HEAD:refs/for/main/topic \
+ >out 2>&1 &&
+ make_user_friendly_and_stable_output <out >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: proc-receive> option old-oid <COMMIT-A> Z
+ > remote: proc-receive> option new-oid <COMMIT-B> Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: proc-receive> option refname refs/changes/24/124/1 Z
+ > remote: proc-receive> option old-oid <ZERO-OID> Z
+ > remote: proc-receive> option new-oid <COMMIT-A> Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: proc-receive> option refname refs/changes/25/125/1 Z
+ > remote: proc-receive> option old-oid <COMMIT-A> Z
+ > remote: proc-receive> option new-oid <COMMIT-B> Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <COMMIT-A> <COMMIT-B> refs/for/main/topic Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/changes/24/124/1 Z
+ > remote: post-receive< <COMMIT-A> <COMMIT-B> refs/changes/25/125/1 Z
+ > To <URL/of/upstream.git>
+ > <COMMIT-A>..<COMMIT-B> HEAD -> refs/for/main/topic
+ > * [new reference] HEAD -> refs/changes/24/124/1
+ > <COMMIT-A>..<COMMIT-B> HEAD -> refs/changes/25/125/1
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ EOF
+'
+
+test_expect_success "proc-receive: check remote-tracking #1 ($PROTOCOL)" '
+ git -C workbench show-ref |
+ grep -v -e refs/remotes -e refs/heads -e refs/tags >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-A> refs/t/changes/24/124/1
+ <COMMIT-B> refs/t/changes/25/125/1
+ <COMMIT-B> refs/t/for/main/topic
+ EOF
+ test_cmp expect actual &&
+ git -C workbench update-ref -d refs/t/for/main/topic &&
+ git -C workbench update-ref -d refs/t/changes/24/124/1 &&
+ git -C workbench update-ref -d refs/t/changes/25/125/1
+'
+
+test_expect_success "setup proc-receive hook (multiple rewrites for one ref, no refname for the 2nd rewrite, $PROTOCOL)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ok refs/for/main/topic" \
+ -r "option refname refs/changes/24/124/1" \
+ -r "option old-oid $ZERO_OID" \
+ -r "option new-oid $A" \
+ -r "ok refs/for/main/topic" \
+ -r "option old-oid $A" \
+ -r "option new-oid $B" \
+ -r "ok refs/for/main/topic" \
+ -r "option refname refs/changes/25/125/1" \
+ -r "option old-oid $B" \
+ -r "option new-oid $A" \
+ -r "option forced-update"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/main/topic(A)
+test_expect_success "proc-receive: multiple rewrites for one ref, no refname for the 2nd rewrite ($PROTOCOL)" '
+ git -C workbench push origin \
+ HEAD:refs/for/main/topic \
+ >out 2>&1 &&
+ make_user_friendly_and_stable_output <out >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: proc-receive> option refname refs/changes/24/124/1 Z
+ > remote: proc-receive> option old-oid <ZERO-OID> Z
+ > remote: proc-receive> option new-oid <COMMIT-A> Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: proc-receive> option old-oid <COMMIT-A> Z
+ > remote: proc-receive> option new-oid <COMMIT-B> Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: proc-receive> option refname refs/changes/25/125/1 Z
+ > remote: proc-receive> option old-oid <COMMIT-B> Z
+ > remote: proc-receive> option new-oid <COMMIT-A> Z
+ > remote: proc-receive> option forced-update Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/changes/24/124/1 Z
+ > remote: post-receive< <COMMIT-A> <COMMIT-B> refs/for/main/topic Z
+ > remote: post-receive< <COMMIT-B> <COMMIT-A> refs/changes/25/125/1 Z
+ > To <URL/of/upstream.git>
+ > * [new reference] HEAD -> refs/changes/24/124/1
+ > <COMMIT-A>..<COMMIT-B> HEAD -> refs/for/main/topic
+ > + <COMMIT-B>...<COMMIT-A> HEAD -> refs/changes/25/125/1 (forced update)
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ EOF
+'
+
+test_expect_success "proc-receive: check remote-tracking #2 ($PROTOCOL)" '
+ git -C workbench show-ref |
+ grep -v -e refs/remotes -e refs/heads -e refs/tags >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-A> refs/t/changes/24/124/1
+ <COMMIT-A> refs/t/changes/25/125/1
+ <COMMIT-B> refs/t/for/main/topic
+ EOF
+ test_cmp expect actual &&
+ git -C workbench update-ref -d refs/t/for/main/topic &&
+ git -C workbench update-ref -d refs/t/changes/24/124/1 &&
+ git -C workbench update-ref -d refs/t/changes/25/125/1
+'
+
+test_expect_success "setup proc-receive hook (multiple rewrites for one ref, $PROTOCOL)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ok refs/for/main/topic" \
+ -r "option refname refs/changes/23/123/1" \
+ -r "ok refs/for/main/topic" \
+ -r "option refname refs/changes/24/124/2" \
+ -r "option old-oid $A" \
+ -r "option new-oid $B"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/main/topic(A)
+test_expect_success "proc-receive: multiple rewrites for one ref ($PROTOCOL)" '
+ git -C workbench push origin \
+ HEAD:refs/for/main/topic \
+ >out 2>&1 &&
+ make_user_friendly_and_stable_output <out >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: proc-receive> option refname refs/changes/23/123/1 Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: proc-receive> option refname refs/changes/24/124/2 Z
+ > remote: proc-receive> option old-oid <COMMIT-A> Z
+ > remote: proc-receive> option new-oid <COMMIT-B> Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/changes/23/123/1 Z
+ > remote: post-receive< <COMMIT-A> <COMMIT-B> refs/changes/24/124/2 Z
+ > To <URL/of/upstream.git>
+ > * [new reference] HEAD -> refs/changes/23/123/1
+ > <COMMIT-A>..<COMMIT-B> HEAD -> refs/changes/24/124/2
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ EOF
+'
+
+test_expect_success "proc-receive: check remote-tracking #3 ($PROTOCOL)" '
+ git -C workbench show-ref |
+ grep -v -e refs/remotes -e refs/heads -e refs/tags >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-A> refs/t/changes/23/123/1
+ <COMMIT-B> refs/t/changes/24/124/2
+ EOF
+ test_cmp expect actual &&
+ git -C workbench update-ref -d refs/t/changes/24/124/1 &&
+ git -C workbench update-ref -d refs/t/changes/25/125/2
+'
diff --git a/t/t5411/test-0037-report-multi-rewrite-for-one-ref--porcelain.sh b/t/t5411/test-0037-report-multi-rewrite-for-one-ref--porcelain.sh
new file mode 100644
index 0000000..1e523b1
--- /dev/null
+++ b/t/t5411/test-0037-report-multi-rewrite-for-one-ref--porcelain.sh
@@ -0,0 +1,166 @@
+test_expect_success "setup proc-receive hook (multiple rewrites for one ref, no refname for the 1st rewrite, $PROTOCOL/porcelain)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ok refs/for/main/topic" \
+ -r "option old-oid $A" \
+ -r "option new-oid $B" \
+ -r "ok refs/for/main/topic" \
+ -r "option refname refs/changes/24/124/1" \
+ -r "option old-oid $ZERO_OID" \
+ -r "option new-oid $A" \
+ -r "ok refs/for/main/topic" \
+ -r "option refname refs/changes/25/125/1" \
+ -r "option old-oid $A" \
+ -r "option new-oid $B"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/main/topic(A)
+test_expect_success "proc-receive: multiple rewrite for one ref, no refname for the 1st rewrite ($PROTOCOL/porcelain)" '
+ git -C workbench push --porcelain origin \
+ HEAD:refs/for/main/topic \
+ >out 2>&1 &&
+ make_user_friendly_and_stable_output <out >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: proc-receive> option old-oid <COMMIT-A> Z
+ > remote: proc-receive> option new-oid <COMMIT-B> Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: proc-receive> option refname refs/changes/24/124/1 Z
+ > remote: proc-receive> option old-oid <ZERO-OID> Z
+ > remote: proc-receive> option new-oid <COMMIT-A> Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: proc-receive> option refname refs/changes/25/125/1 Z
+ > remote: proc-receive> option old-oid <COMMIT-A> Z
+ > remote: proc-receive> option new-oid <COMMIT-B> Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <COMMIT-A> <COMMIT-B> refs/for/main/topic Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/changes/24/124/1 Z
+ > remote: post-receive< <COMMIT-A> <COMMIT-B> refs/changes/25/125/1 Z
+ > To <URL/of/upstream.git>
+ > HEAD:refs/for/main/topic <COMMIT-A>..<COMMIT-B>
+ > * HEAD:refs/changes/24/124/1 [new reference]
+ > HEAD:refs/changes/25/125/1 <COMMIT-A>..<COMMIT-B>
+ > Done
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ EOF
+'
+
+test_expect_success "setup proc-receive hook (multiple rewrites for one ref, no refname for the 2nd rewrite, $PROTOCOL/porcelain)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ok refs/for/main/topic" \
+ -r "option refname refs/changes/24/124/1" \
+ -r "option old-oid $ZERO_OID" \
+ -r "option new-oid $A" \
+ -r "ok refs/for/main/topic" \
+ -r "option old-oid $A" \
+ -r "option new-oid $B" \
+ -r "ok refs/for/main/topic" \
+ -r "option refname refs/changes/25/125/1" \
+ -r "option old-oid $B" \
+ -r "option new-oid $A" \
+ -r "option forced-update"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/main/topic(A)
+test_expect_success "proc-receive: multiple rewrites for one ref, no refname for the 2nd rewrite ($PROTOCOL/porcelain)" '
+ git -C workbench push --porcelain origin \
+ HEAD:refs/for/main/topic \
+ >out 2>&1 &&
+ make_user_friendly_and_stable_output <out >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: proc-receive> option refname refs/changes/24/124/1 Z
+ > remote: proc-receive> option old-oid <ZERO-OID> Z
+ > remote: proc-receive> option new-oid <COMMIT-A> Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: proc-receive> option old-oid <COMMIT-A> Z
+ > remote: proc-receive> option new-oid <COMMIT-B> Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: proc-receive> option refname refs/changes/25/125/1 Z
+ > remote: proc-receive> option old-oid <COMMIT-B> Z
+ > remote: proc-receive> option new-oid <COMMIT-A> Z
+ > remote: proc-receive> option forced-update Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/changes/24/124/1 Z
+ > remote: post-receive< <COMMIT-A> <COMMIT-B> refs/for/main/topic Z
+ > remote: post-receive< <COMMIT-B> <COMMIT-A> refs/changes/25/125/1 Z
+ > To <URL/of/upstream.git>
+ > * HEAD:refs/changes/24/124/1 [new reference]
+ > HEAD:refs/for/main/topic <COMMIT-A>..<COMMIT-B>
+ > + HEAD:refs/changes/25/125/1 <COMMIT-B>...<COMMIT-A> (forced update)
+ > Done
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ EOF
+'
+
+test_expect_success "setup proc-receive hook (multiple rewrites for one ref, $PROTOCOL/porcelain)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ok refs/for/main/topic" \
+ -r "option refname refs/changes/23/123/1" \
+ -r "ok refs/for/main/topic" \
+ -r "option refname refs/changes/24/124/2" \
+ -r "option old-oid $A" \
+ -r "option new-oid $B"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : refs/for/main/topic(A)
+test_expect_success "proc-receive: multiple rewrites for one ref ($PROTOCOL/porcelain)" '
+ git -C workbench push --porcelain origin \
+ HEAD:refs/for/main/topic \
+ >out 2>&1 &&
+ make_user_friendly_and_stable_output <out >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: proc-receive> option refname refs/changes/23/123/1 Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: proc-receive> option refname refs/changes/24/124/2 Z
+ > remote: proc-receive> option old-oid <COMMIT-A> Z
+ > remote: proc-receive> option new-oid <COMMIT-B> Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/changes/23/123/1 Z
+ > remote: post-receive< <COMMIT-A> <COMMIT-B> refs/changes/24/124/2 Z
+ > To <URL/of/upstream.git>
+ > * HEAD:refs/changes/23/123/1 [new reference]
+ > HEAD:refs/changes/24/124/2 <COMMIT-A>..<COMMIT-B>
+ > Done
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ EOF
+'
diff --git a/t/t5411/test-0038-report-mixed-refs.sh b/t/t5411/test-0038-report-mixed-refs.sh
new file mode 100644
index 0000000..4c70e84
--- /dev/null
+++ b/t/t5411/test-0038-report-mixed-refs.sh
@@ -0,0 +1,87 @@
+test_expect_success "setup proc-receive hook ($PROTOCOL)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ok refs/for/next/topic2" \
+ -r "ng refs/for/next/topic1 fail to call Web API" \
+ -r "ok refs/for/main/topic" \
+ -r "option refname refs/for/main/topic" \
+ -r "option old-oid $A" \
+ -r "option new-oid $B"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : (B) bar(A) baz(A) refs/for/next/topic(A) foo(A) refs/for/main/topic(A)
+test_expect_success "proc-receive: report update of mixed refs ($PROTOCOL)" '
+ test_must_fail git -C workbench push origin \
+ $B:refs/heads/main \
+ HEAD:refs/heads/bar \
+ HEAD:refs/heads/baz \
+ HEAD:refs/for/next/topic2 \
+ HEAD:refs/for/next/topic1 \
+ HEAD:refs/heads/foo \
+ HEAD:refs/for/main/topic \
+ HEAD:refs/for/next/topic3 \
+ >out-$test_count 2>&1 &&
+ make_user_friendly_and_stable_output <out-$test_count >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <COMMIT-A> <COMMIT-B> refs/heads/main Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/bar Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/baz Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic2 Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic1 Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/foo Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic3 Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic2 Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic1 Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic3 Z
+ > remote: proc-receive> ok refs/for/next/topic2 Z
+ > remote: proc-receive> ng refs/for/next/topic1 fail to call Web API Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: proc-receive> option refname refs/for/main/topic Z
+ > remote: proc-receive> option old-oid <COMMIT-A> Z
+ > remote: proc-receive> option new-oid <COMMIT-B> Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <COMMIT-A> <COMMIT-B> refs/heads/main Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/bar Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/baz Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic2 Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/foo Z
+ > remote: post-receive< <COMMIT-A> <COMMIT-B> refs/for/main/topic Z
+ > To <URL/of/upstream.git>
+ > <COMMIT-A>..<COMMIT-B> <COMMIT-B> -> main
+ > * [new branch] HEAD -> bar
+ > * [new branch] HEAD -> baz
+ > * [new reference] HEAD -> refs/for/next/topic2
+ > * [new branch] HEAD -> foo
+ > <COMMIT-A>..<COMMIT-B> HEAD -> refs/for/main/topic
+ > ! [remote rejected] HEAD -> refs/for/next/topic1 (fail to call Web API)
+ > ! [remote rejected] HEAD -> refs/for/next/topic3 (proc-receive failed to report status)
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/bar
+ <COMMIT-A> refs/heads/baz
+ <COMMIT-A> refs/heads/foo
+ <COMMIT-B> refs/heads/main
+ EOF
+'
+
+# Refs of upstream : main(B) foo(A) bar(A)) baz(A)
+# Refs of workbench: main(A) tags/v123
+test_expect_success "cleanup ($PROTOCOL)" '
+ (
+ cd "$upstream" &&
+ git update-ref refs/heads/main $A &&
+ git update-ref -d refs/heads/foo &&
+ git update-ref -d refs/heads/bar &&
+ git update-ref -d refs/heads/baz
+ )
+'
diff --git a/t/t5411/test-0039-report-mixed-refs--porcelain.sh b/t/t5411/test-0039-report-mixed-refs--porcelain.sh
new file mode 100644
index 0000000..40f4c5b
--- /dev/null
+++ b/t/t5411/test-0039-report-mixed-refs--porcelain.sh
@@ -0,0 +1,89 @@
+test_expect_success "setup proc-receive hook ($PROTOCOL/porcelain)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ok refs/for/next/topic2" \
+ -r "ng refs/for/next/topic1 fail to call Web API" \
+ -r "ok refs/for/main/topic" \
+ -r "option refname refs/for/main/topic" \
+ -r "option old-oid $A" \
+ -r "option new-oid $B"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : (B) bar(A) baz(A) refs/for/next/topic(A) foo(A) refs/for/main/topic(A)
+test_expect_success "proc-receive: report update of mixed refs ($PROTOCOL/porcelain)" '
+ test_must_fail git -C workbench push --porcelain origin \
+ $B:refs/heads/main \
+ HEAD:refs/heads/bar \
+ HEAD:refs/heads/baz \
+ HEAD:refs/for/next/topic2 \
+ HEAD:refs/for/next/topic1 \
+ HEAD:refs/heads/foo \
+ HEAD:refs/for/main/topic \
+ HEAD:refs/for/next/topic3 \
+ >out-$test_count 2>&1 &&
+ make_user_friendly_and_stable_output <out-$test_count >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <COMMIT-A> <COMMIT-B> refs/heads/main Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/bar Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/baz Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic2 Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic1 Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/foo Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic3 Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic2 Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic1 Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic3 Z
+ > remote: proc-receive> ok refs/for/next/topic2 Z
+ > remote: proc-receive> ng refs/for/next/topic1 fail to call Web API Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: proc-receive> option refname refs/for/main/topic Z
+ > remote: proc-receive> option old-oid <COMMIT-A> Z
+ > remote: proc-receive> option new-oid <COMMIT-B> Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <COMMIT-A> <COMMIT-B> refs/heads/main Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/bar Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/baz Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic2 Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/heads/foo Z
+ > remote: post-receive< <COMMIT-A> <COMMIT-B> refs/for/main/topic Z
+ > To <URL/of/upstream.git>
+ > <COMMIT-B>:refs/heads/main <COMMIT-A>..<COMMIT-B>
+ > * HEAD:refs/heads/bar [new branch]
+ > * HEAD:refs/heads/baz [new branch]
+ > * HEAD:refs/for/next/topic2 [new reference]
+ > * HEAD:refs/heads/foo [new branch]
+ > HEAD:refs/for/main/topic <COMMIT-A>..<COMMIT-B>
+ > ! HEAD:refs/for/next/topic1 [remote rejected] (fail to call Web API)
+ > ! HEAD:refs/for/next/topic3 [remote rejected] (proc-receive failed to report status)
+ > Done
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/bar
+ <COMMIT-A> refs/heads/baz
+ <COMMIT-A> refs/heads/foo
+ <COMMIT-B> refs/heads/main
+ EOF
+'
+
+# Refs of upstream : main(B) foo(A) bar(A)) baz(A)
+# Refs of workbench: main(A) tags/v123
+test_expect_success "cleanup ($PROTOCOL/porcelain)" '
+ (
+ cd "$upstream" &&
+ git update-ref refs/heads/main $A &&
+ git update-ref -d refs/heads/foo &&
+ git update-ref -d refs/heads/bar &&
+ git update-ref -d refs/heads/baz
+ )
+
+'
diff --git a/t/t5411/test-0040-process-all-refs.sh b/t/t5411/test-0040-process-all-refs.sh
new file mode 100644
index 0000000..7ae3851
--- /dev/null
+++ b/t/t5411/test-0040-process-all-refs.sh
@@ -0,0 +1,111 @@
+test_expect_success "config receive.procReceiveRefs = refs ($PROTOCOL)" '
+ git -C "$upstream" config --unset-all receive.procReceiveRefs &&
+ git -C "$upstream" config --add receive.procReceiveRefs refs
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+test_expect_success "setup upstream branches ($PROTOCOL)" '
+ (
+ cd "$upstream" &&
+ git update-ref refs/heads/main $B &&
+ git update-ref refs/heads/foo $A &&
+ git update-ref refs/heads/bar $A &&
+ git update-ref refs/heads/baz $A
+ )
+
+'
+
+test_expect_success "setup proc-receive hook ($PROTOCOL)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ok refs/heads/main" \
+ -r "option fall-through" \
+ -r "ok refs/heads/foo" \
+ -r "option fall-through" \
+ -r "ok refs/heads/bar" \
+ -r "option fall-through" \
+ -r "ok refs/for/main/topic" \
+ -r "option refname refs/pull/123/head" \
+ -r "option old-oid $A" \
+ -r "option new-oid $B" \
+ -r "ok refs/for/next/topic" \
+ -r "option refname refs/pull/124/head" \
+ -r "option old-oid $B" \
+ -r "option new-oid $A" \
+ -r "option forced-update"
+ EOF
+'
+
+# Refs of upstream : main(B) foo(A) bar(A)) baz(A)
+# Refs of workbench: main(A) tags/v123
+# git push -f : main(A) (NULL) (B) refs/for/main/topic(A) refs/for/next/topic(A)
+test_expect_success "proc-receive: process all refs ($PROTOCOL)" '
+ git -C workbench push -f origin \
+ HEAD:refs/heads/main \
+ :refs/heads/foo \
+ $B:refs/heads/bar \
+ HEAD:refs/for/main/topic \
+ HEAD:refs/for/next/topic \
+ >out 2>&1 &&
+ make_user_friendly_and_stable_output <out >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <COMMIT-A> <COMMIT-B> refs/heads/bar Z
+ > remote: pre-receive< <COMMIT-A> <ZERO-OID> refs/heads/foo Z
+ > remote: pre-receive< <COMMIT-B> <COMMIT-A> refs/heads/main Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <COMMIT-A> <COMMIT-B> refs/heads/bar Z
+ > remote: proc-receive< <COMMIT-A> <ZERO-OID> refs/heads/foo Z
+ > remote: proc-receive< <COMMIT-B> <COMMIT-A> refs/heads/main Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic Z
+ > remote: proc-receive> ok refs/heads/main Z
+ > remote: proc-receive> option fall-through Z
+ > remote: proc-receive> ok refs/heads/foo Z
+ > remote: proc-receive> option fall-through Z
+ > remote: proc-receive> ok refs/heads/bar Z
+ > remote: proc-receive> option fall-through Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: proc-receive> option refname refs/pull/123/head Z
+ > remote: proc-receive> option old-oid <COMMIT-A> Z
+ > remote: proc-receive> option new-oid <COMMIT-B> Z
+ > remote: proc-receive> ok refs/for/next/topic Z
+ > remote: proc-receive> option refname refs/pull/124/head Z
+ > remote: proc-receive> option old-oid <COMMIT-B> Z
+ > remote: proc-receive> option new-oid <COMMIT-A> Z
+ > remote: proc-receive> option forced-update Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <COMMIT-A> <COMMIT-B> refs/heads/bar Z
+ > remote: post-receive< <COMMIT-A> <ZERO-OID> refs/heads/foo Z
+ > remote: post-receive< <COMMIT-B> <COMMIT-A> refs/heads/main Z
+ > remote: post-receive< <COMMIT-A> <COMMIT-B> refs/pull/123/head Z
+ > remote: post-receive< <COMMIT-B> <COMMIT-A> refs/pull/124/head Z
+ > To <URL/of/upstream.git>
+ > <COMMIT-A>..<COMMIT-B> <COMMIT-B> -> bar
+ > - [deleted] foo
+ > + <COMMIT-B>...<COMMIT-A> HEAD -> main (forced update)
+ > <COMMIT-A>..<COMMIT-B> HEAD -> refs/pull/123/head
+ > + <COMMIT-B>...<COMMIT-A> HEAD -> refs/pull/124/head (forced update)
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-B> refs/heads/bar
+ <COMMIT-A> refs/heads/baz
+ <COMMIT-A> refs/heads/main
+ EOF
+'
+
+# Refs of upstream : main(A) bar(A) baz(B)
+# Refs of workbench: main(A) tags/v123
+test_expect_success "cleanup ($PROTOCOL)" '
+ (
+ cd "$upstream" &&
+ git update-ref -d refs/heads/bar &&
+ git update-ref -d refs/heads/baz
+ )
+'
diff --git a/t/t5411/test-0041-process-all-refs--porcelain.sh b/t/t5411/test-0041-process-all-refs--porcelain.sh
new file mode 100644
index 0000000..02e1e08
--- /dev/null
+++ b/t/t5411/test-0041-process-all-refs--porcelain.sh
@@ -0,0 +1,112 @@
+test_expect_success "config receive.procReceiveRefs = refs ($PROTOCOL/porcelain)" '
+ git -C "$upstream" config --unset-all receive.procReceiveRefs &&
+ git -C "$upstream" config --add receive.procReceiveRefs refs
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+test_expect_success "setup upstream branches ($PROTOCOL/porcelain)" '
+ (
+ cd "$upstream" &&
+ git update-ref refs/heads/main $B &&
+ git update-ref refs/heads/foo $A &&
+ git update-ref refs/heads/bar $A &&
+ git update-ref refs/heads/baz $A
+ )
+
+'
+
+test_expect_success "setup proc-receive hook ($PROTOCOL/porcelain)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ok refs/heads/main" \
+ -r "option fall-through" \
+ -r "ok refs/heads/foo" \
+ -r "option fall-through" \
+ -r "ok refs/heads/bar" \
+ -r "option fall-through" \
+ -r "ok refs/for/main/topic" \
+ -r "option refname refs/pull/123/head" \
+ -r "option old-oid $A" \
+ -r "option new-oid $B" \
+ -r "ok refs/for/next/topic" \
+ -r "option refname refs/pull/124/head" \
+ -r "option old-oid $B" \
+ -r "option new-oid $A" \
+ -r "option forced-update"
+ EOF
+'
+
+# Refs of upstream : main(B) foo(A) bar(A)) baz(A)
+# Refs of workbench: main(A) tags/v123
+# git push -f : main(A) (NULL) (B) refs/for/main/topic(A) refs/for/next/topic(A)
+test_expect_success "proc-receive: process all refs ($PROTOCOL/porcelain)" '
+ git -C workbench push --porcelain -f origin \
+ HEAD:refs/heads/main \
+ :refs/heads/foo \
+ $B:refs/heads/bar \
+ HEAD:refs/for/main/topic \
+ HEAD:refs/for/next/topic \
+ >out 2>&1 &&
+ make_user_friendly_and_stable_output <out >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <COMMIT-A> <COMMIT-B> refs/heads/bar Z
+ > remote: pre-receive< <COMMIT-A> <ZERO-OID> refs/heads/foo Z
+ > remote: pre-receive< <COMMIT-B> <COMMIT-A> refs/heads/main Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <COMMIT-A> <COMMIT-B> refs/heads/bar Z
+ > remote: proc-receive< <COMMIT-A> <ZERO-OID> refs/heads/foo Z
+ > remote: proc-receive< <COMMIT-B> <COMMIT-A> refs/heads/main Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/main/topic Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/for/next/topic Z
+ > remote: proc-receive> ok refs/heads/main Z
+ > remote: proc-receive> option fall-through Z
+ > remote: proc-receive> ok refs/heads/foo Z
+ > remote: proc-receive> option fall-through Z
+ > remote: proc-receive> ok refs/heads/bar Z
+ > remote: proc-receive> option fall-through Z
+ > remote: proc-receive> ok refs/for/main/topic Z
+ > remote: proc-receive> option refname refs/pull/123/head Z
+ > remote: proc-receive> option old-oid <COMMIT-A> Z
+ > remote: proc-receive> option new-oid <COMMIT-B> Z
+ > remote: proc-receive> ok refs/for/next/topic Z
+ > remote: proc-receive> option refname refs/pull/124/head Z
+ > remote: proc-receive> option old-oid <COMMIT-B> Z
+ > remote: proc-receive> option new-oid <COMMIT-A> Z
+ > remote: proc-receive> option forced-update Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <COMMIT-A> <COMMIT-B> refs/heads/bar Z
+ > remote: post-receive< <COMMIT-A> <ZERO-OID> refs/heads/foo Z
+ > remote: post-receive< <COMMIT-B> <COMMIT-A> refs/heads/main Z
+ > remote: post-receive< <COMMIT-A> <COMMIT-B> refs/pull/123/head Z
+ > remote: post-receive< <COMMIT-B> <COMMIT-A> refs/pull/124/head Z
+ > To <URL/of/upstream.git>
+ > <COMMIT-B>:refs/heads/bar <COMMIT-A>..<COMMIT-B>
+ > - :refs/heads/foo [deleted]
+ > + HEAD:refs/heads/main <COMMIT-B>...<COMMIT-A> (forced update)
+ > HEAD:refs/pull/123/head <COMMIT-A>..<COMMIT-B>
+ > + HEAD:refs/pull/124/head <COMMIT-B>...<COMMIT-A> (forced update)
+ > Done
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-B> refs/heads/bar
+ <COMMIT-A> refs/heads/baz
+ <COMMIT-A> refs/heads/main
+ EOF
+'
+
+# Refs of upstream : main(A) bar(A) baz(B)
+# Refs of workbench: main(A) tags/v123
+test_expect_success "cleanup ($PROTOCOL/porcelain)" '
+ (
+ cd "$upstream" &&
+ git update-ref -d refs/heads/bar &&
+ git update-ref -d refs/heads/baz
+ )
+'
diff --git a/t/t5411/test-0050-proc-receive-refs-with-modifiers.sh b/t/t5411/test-0050-proc-receive-refs-with-modifiers.sh
new file mode 100644
index 0000000..7efdfe5
--- /dev/null
+++ b/t/t5411/test-0050-proc-receive-refs-with-modifiers.sh
@@ -0,0 +1,129 @@
+test_expect_success "config receive.procReceiveRefs with modifiers ($PROTOCOL)" '
+ (
+ cd "$upstream" &&
+ git config --unset-all receive.procReceiveRefs &&
+ git config --add receive.procReceiveRefs m:refs/heads/main &&
+ git config --add receive.procReceiveRefs ad:refs/heads &&
+ git config --add receive.procReceiveRefs "a!:refs/heads"
+ )
+'
+
+test_expect_success "setup proc-receive hook ($PROTOCOL)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ok refs/heads/main" \
+ -r "option refname refs/pull/123/head" \
+ -r "option old-oid $A" \
+ -r "option new-oid $B" \
+ -r "ok refs/tags/v123 " \
+ -r "option refname refs/pull/124/head"
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+# git push : main(B) tags/v123
+test_expect_success "proc-receive: update branch and new tag ($PROTOCOL)" '
+ git -C workbench push origin \
+ $B:refs/heads/main \
+ v123 >out 2>&1 &&
+ make_user_friendly_and_stable_output <out >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <COMMIT-A> <COMMIT-B> refs/heads/main Z
+ > remote: pre-receive< <ZERO-OID> <TAG-v123> refs/tags/v123 Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <COMMIT-A> <COMMIT-B> refs/heads/main Z
+ > remote: proc-receive< <ZERO-OID> <TAG-v123> refs/tags/v123 Z
+ > remote: proc-receive> ok refs/heads/main Z
+ > remote: proc-receive> option refname refs/pull/123/head Z
+ > remote: proc-receive> option old-oid <COMMIT-A> Z
+ > remote: proc-receive> option new-oid <COMMIT-B> Z
+ > remote: proc-receive> ok refs/tags/v123 Z
+ > remote: proc-receive> option refname refs/pull/124/head Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <COMMIT-A> <COMMIT-B> refs/pull/123/head Z
+ > remote: post-receive< <ZERO-OID> <TAG-v123> refs/pull/124/head Z
+ > To <URL/of/upstream.git>
+ > <COMMIT-A>..<COMMIT-B> <COMMIT-B> -> refs/pull/123/head
+ > * [new reference] v123 -> refs/pull/124/head
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ EOF
+'
+
+# Refs of upstream : main(A)
+# Refs of workbench: main(A) tags/v123
+test_expect_success "setup upstream: create tags/v123 ($PROTOCOL)" '
+ git -C "$upstream" update-ref refs/heads/topic $A &&
+ git -C "$upstream" update-ref refs/tags/v123 $TAG &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ <COMMIT-A> refs/heads/topic
+ <TAG-v123> refs/tags/v123
+ EOF
+'
+
+test_expect_success "setup proc-receive hook ($PROTOCOL)" '
+ test_hook -C "$upstream" --clobber proc-receive <<-EOF
+ printf >&2 "# proc-receive hook\n"
+ test-tool proc-receive -v \
+ -r "ok refs/heads/main" \
+ -r "option refname refs/pull/123/head" \
+ -r "option old-oid $A" \
+ -r "option new-oid $ZERO_OID" \
+ -r "ok refs/heads/next" \
+ -r "option refname refs/pull/124/head" \
+ -r "option new-oid $A"
+ EOF
+'
+
+# Refs of upstream : main(A) topic(A) tags/v123
+# Refs of workbench: main(A) tags/v123
+# git push : NULL topic(B) NULL next(A)
+test_expect_success "proc-receive: create/delete branch, and delete tag ($PROTOCOL)" '
+ git -C workbench push origin \
+ :refs/heads/main \
+ $B:refs/heads/topic \
+ $A:refs/heads/next \
+ :refs/tags/v123 >out 2>&1 &&
+ make_user_friendly_and_stable_output <out >actual &&
+ format_and_save_expect <<-EOF &&
+ > remote: # pre-receive hook Z
+ > remote: pre-receive< <COMMIT-A> <ZERO-OID> refs/heads/main Z
+ > remote: pre-receive< <COMMIT-A> <COMMIT-B> refs/heads/topic Z
+ > remote: pre-receive< <TAG-v123> <ZERO-OID> refs/tags/v123 Z
+ > remote: pre-receive< <ZERO-OID> <COMMIT-A> refs/heads/next Z
+ > remote: # proc-receive hook Z
+ > remote: proc-receive< <COMMIT-A> <ZERO-OID> refs/heads/main Z
+ > remote: proc-receive< <ZERO-OID> <COMMIT-A> refs/heads/next Z
+ > remote: proc-receive> ok refs/heads/main Z
+ > remote: proc-receive> option refname refs/pull/123/head Z
+ > remote: proc-receive> option old-oid <COMMIT-A> Z
+ > remote: proc-receive> option new-oid <ZERO-OID> Z
+ > remote: proc-receive> ok refs/heads/next Z
+ > remote: proc-receive> option refname refs/pull/124/head Z
+ > remote: proc-receive> option new-oid <COMMIT-A> Z
+ > remote: # post-receive hook Z
+ > remote: post-receive< <COMMIT-A> <ZERO-OID> refs/pull/123/head Z
+ > remote: post-receive< <COMMIT-A> <COMMIT-B> refs/heads/topic Z
+ > remote: post-receive< <TAG-v123> <ZERO-OID> refs/tags/v123 Z
+ > remote: post-receive< <ZERO-OID> <COMMIT-A> refs/pull/124/head Z
+ > To <URL/of/upstream.git>
+ > - [deleted] refs/pull/123/head
+ > <COMMIT-A>..<COMMIT-B> <COMMIT-B> -> topic
+ > - [deleted] v123
+ > * [new reference] <COMMIT-A> -> refs/pull/124/head
+ EOF
+ test_cmp expect actual &&
+
+ test_cmp_refs -C "$upstream" <<-EOF
+ <COMMIT-A> refs/heads/main
+ <COMMIT-B> refs/heads/topic
+ EOF
+'
diff --git a/t/t5500-fetch-pack.sh b/t/t5500-fetch-pack.sh
new file mode 100755
index 0000000..1bc15a3
--- /dev/null
+++ b/t/t5500-fetch-pack.sh
@@ -0,0 +1,1069 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Johannes Schindelin
+#
+
+test_description='Testing multi_ack pack fetching'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+# Test fetch-pack/upload-pack pair.
+
+# Some convenience functions
+
+add () {
+ name=$1 &&
+ text="$@" &&
+ branch=$(echo $name | sed -e 's/^\(.\).*$/\1/') &&
+ parents="" &&
+
+ shift &&
+ while test $1; do
+ parents="$parents -p $1" &&
+ shift
+ done &&
+
+ echo "$text" > test.txt &&
+ git update-index --add test.txt &&
+ tree=$(git write-tree) &&
+ # make sure timestamps are in correct order
+ test_tick &&
+ commit=$(echo "$text" | git commit-tree $tree $parents) &&
+ eval "$name=$commit; export $name" &&
+ git update-ref "refs/heads/$branch" "$commit" &&
+ eval ${branch}TIP=$commit
+}
+
+pull_to_client () {
+ number=$1 &&
+ heads=$2 &&
+ count=$3 &&
+ test_expect_success "$number pull" '
+ (
+ cd client &&
+ git fetch-pack -k -v .. $heads &&
+
+ case "$heads" in
+ *A*)
+ git update-ref refs/heads/A "$ATIP";;
+ esac &&
+ case "$heads" in *B*)
+ git update-ref refs/heads/B "$BTIP";;
+ esac &&
+
+ git symbolic-ref HEAD refs/heads/$(
+ echo $heads |
+ sed -e "s/^\(.\).*$/\1/"
+ ) &&
+
+ git fsck --full &&
+
+ mv .git/objects/pack/pack-* . &&
+ p=$(ls -1 pack-*.pack) &&
+ git unpack-objects <$p &&
+ git fsck --full &&
+
+ idx=$(echo pack-*.idx) &&
+ pack_count=$(git show-index <$idx | wc -l) &&
+ test $pack_count = $count &&
+ rm -f pack-*
+ )
+ '
+}
+
+# Here begins the actual testing
+
+# A1 - ... - A20 - A21
+# \
+# B1 - B2 - .. - B70
+
+# client pulls A20, B1. Then tracks only B. Then pulls A.
+
+test_expect_success 'setup' '
+ mkdir client &&
+ (
+ cd client &&
+ git init &&
+ git config transfer.unpacklimit 0
+ ) &&
+ add A1 &&
+ prev=1 &&
+ cur=2 &&
+ while [ $cur -le 10 ]; do
+ add A$cur $(eval echo \$A$prev) &&
+ prev=$cur &&
+ cur=$(($cur+1)) || return 1
+ done &&
+ add B1 $A1 &&
+ git update-ref refs/heads/A "$ATIP" &&
+ git update-ref refs/heads/B "$BTIP" &&
+ git symbolic-ref HEAD refs/heads/B
+'
+
+pull_to_client 1st "refs/heads/B refs/heads/A" $((11*3))
+
+test_expect_success 'post 1st pull setup' '
+ add A11 $A10 &&
+ prev=1 &&
+ cur=2 &&
+ while [ $cur -le 65 ]; do
+ add B$cur $(eval echo \$B$prev) &&
+ prev=$cur &&
+ cur=$(($cur+1)) || return 1
+ done
+'
+
+pull_to_client 2nd "refs/heads/B" $((64*3))
+
+pull_to_client 3rd "refs/heads/A" $((1*3))
+
+test_expect_success 'single branch clone' '
+ git clone --single-branch "file://$(pwd)/." singlebranch
+'
+
+test_expect_success 'single branch object count' '
+ GIT_DIR=singlebranch/.git git count-objects -v |
+ grep "^in-pack:" > count.singlebranch &&
+ echo "in-pack: 198" >expected &&
+ test_cmp expected count.singlebranch
+'
+
+test_expect_success 'single given branch clone' '
+ GIT_TRACE2_EVENT="$(pwd)/branch-a/trace2_event" \
+ git clone --single-branch --branch A "file://$(pwd)/." branch-a &&
+ test_must_fail git --git-dir=branch-a/.git rev-parse origin/B &&
+ grep \"fetch-info\".*\"haves\":0 branch-a/trace2_event &&
+ grep \"fetch-info\".*\"wants\":1 branch-a/trace2_event
+'
+
+test_expect_success 'clone shallow depth 1' '
+ GIT_TRACE2_EVENT="$(pwd)/shallow0/trace2_event" \
+ git clone --no-single-branch --depth 1 "file://$(pwd)/." shallow0 &&
+ test "$(git --git-dir=shallow0/.git rev-list --count HEAD)" = 1 &&
+ grep \"fetch-info\".*\"depth\":1 shallow0/trace2_event
+'
+
+test_expect_success 'clone shallow depth 1 with fsck' '
+ git config --global fetch.fsckobjects true &&
+ git clone --no-single-branch --depth 1 "file://$(pwd)/." shallow0fsck &&
+ test "$(git --git-dir=shallow0fsck/.git rev-list --count HEAD)" = 1 &&
+ git config --global --unset fetch.fsckobjects
+'
+
+test_expect_success 'clone shallow' '
+ git clone --no-single-branch --depth 2 "file://$(pwd)/." shallow
+'
+
+test_expect_success 'clone shallow depth count' '
+ test "$(git --git-dir=shallow/.git rev-list --count HEAD)" = 2
+'
+
+test_expect_success 'clone shallow object count' '
+ (
+ cd shallow &&
+ git count-objects -v
+ ) > count.shallow &&
+ grep "^in-pack: 12" count.shallow
+'
+
+test_expect_success 'clone shallow object count (part 2)' '
+ sed -e "/^in-pack:/d" -e "/^packs:/d" -e "/^size-pack:/d" \
+ -e "/: 0$/d" count.shallow > count_output &&
+ test_must_be_empty count_output
+'
+
+test_expect_success 'fsck in shallow repo' '
+ (
+ cd shallow &&
+ git fsck --full
+ )
+'
+
+test_expect_success 'simple fetch in shallow repo' '
+ (
+ cd shallow &&
+ git fetch
+ )
+'
+
+test_expect_success 'no changes expected' '
+ (
+ cd shallow &&
+ git count-objects -v
+ ) > count.shallow.2 &&
+ cmp count.shallow count.shallow.2
+'
+
+test_expect_success 'fetch same depth in shallow repo' '
+ (
+ cd shallow &&
+ git fetch --depth=2
+ )
+'
+
+test_expect_success 'no changes expected' '
+ (
+ cd shallow &&
+ git count-objects -v
+ ) > count.shallow.3 &&
+ cmp count.shallow count.shallow.3
+'
+
+test_expect_success 'add two more' '
+ add B66 $B65 &&
+ add B67 $B66
+'
+
+test_expect_success 'pull in shallow repo' '
+ (
+ cd shallow &&
+ git pull .. B
+ )
+'
+
+test_expect_success 'clone shallow object count' '
+ (
+ cd shallow &&
+ git count-objects -v
+ ) > count.shallow &&
+ grep "^count: 6" count.shallow
+'
+
+test_expect_success 'add two more (part 2)' '
+ add B68 $B67 &&
+ add B69 $B68
+'
+
+test_expect_success 'deepening pull in shallow repo' '
+ (
+ cd shallow &&
+ GIT_TRACE2_EVENT="$(pwd)/trace2_event" \
+ git pull --depth 4 .. B &&
+ grep \"fetch-info\".*\"depth\":4 trace2_event &&
+ grep \"fetch-info\".*\"shallows\":2 trace2_event
+ )
+'
+
+test_expect_success 'clone shallow object count' '
+ (
+ cd shallow &&
+ git count-objects -v
+ ) > count.shallow &&
+ grep "^count: 12" count.shallow
+'
+
+test_expect_success 'deepening fetch in shallow repo' '
+ (
+ cd shallow &&
+ git fetch --depth 4 .. A:A
+ )
+'
+
+test_expect_success 'clone shallow object count' '
+ (
+ cd shallow &&
+ git count-objects -v
+ ) > count.shallow &&
+ grep "^count: 18" count.shallow
+'
+
+test_expect_success 'pull in shallow repo with missing merge base' '
+ (
+ cd shallow &&
+ git fetch --depth 4 .. A &&
+ test_must_fail git merge --allow-unrelated-histories FETCH_HEAD
+ )
+'
+
+test_expect_success 'additional simple shallow deepenings' '
+ (
+ cd shallow &&
+ git fetch --depth=8 &&
+ git fetch --depth=10 &&
+ git fetch --depth=11
+ )
+'
+
+test_expect_success 'clone shallow depth count' '
+ test "$(git --git-dir=shallow/.git rev-list --count HEAD)" = 11
+'
+
+test_expect_success 'clone shallow object count' '
+ (
+ cd shallow &&
+ git prune &&
+ git count-objects -v
+ ) > count.shallow &&
+ grep "^count: 54" count.shallow
+'
+
+test_expect_success 'fetch --no-shallow on full repo' '
+ test_must_fail git fetch --noshallow
+'
+
+test_expect_success 'fetch --depth --no-shallow' '
+ (
+ cd shallow &&
+ test_must_fail git fetch --depth=1 --noshallow
+ )
+'
+
+test_expect_success 'turn shallow to complete repository' '
+ (
+ cd shallow &&
+ GIT_TRACE2_EVENT="$(pwd)/trace2_event" \
+ git fetch --unshallow &&
+ ! test -f .git/shallow &&
+ git fsck --full &&
+ grep \"fetch-info\".*\"shallows\":2 trace2_event &&
+ grep \"fetch-info\".*\"depth\":2147483647 trace2_event
+ )
+'
+
+test_expect_success 'clone shallow without --no-single-branch' '
+ git clone --depth 1 "file://$(pwd)/." shallow2
+'
+
+test_expect_success 'clone shallow object count' '
+ (
+ cd shallow2 &&
+ git count-objects -v
+ ) > count.shallow2 &&
+ grep "^in-pack: 3" count.shallow2
+'
+
+test_expect_success 'clone shallow with --branch' '
+ git clone --depth 1 --branch A "file://$(pwd)/." shallow3
+'
+
+test_expect_success 'clone shallow object count' '
+ echo "in-pack: 3" > count3.expected &&
+ GIT_DIR=shallow3/.git git count-objects -v |
+ grep "^in-pack" > count3.actual &&
+ test_cmp count3.expected count3.actual
+'
+
+test_expect_success 'clone shallow with detached HEAD' '
+ git checkout HEAD^ &&
+ git clone --depth 1 "file://$(pwd)/." shallow5 &&
+ git checkout - &&
+ GIT_DIR=shallow5/.git git rev-parse HEAD >actual &&
+ git rev-parse HEAD^ >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'shallow clone pulling tags' '
+ git tag -a -m A TAGA1 A &&
+ git tag -a -m B TAGB1 B &&
+ git tag TAGA2 A &&
+ git tag TAGB2 B &&
+ git clone --depth 1 "file://$(pwd)/." shallow6 &&
+
+ cat >taglist.expected <<\EOF &&
+TAGB1
+TAGB2
+EOF
+ GIT_DIR=shallow6/.git git tag -l >taglist.actual &&
+ test_cmp taglist.expected taglist.actual &&
+
+ echo "in-pack: 4" > count6.expected &&
+ GIT_DIR=shallow6/.git git count-objects -v |
+ grep "^in-pack" > count6.actual &&
+ test_cmp count6.expected count6.actual
+'
+
+test_expect_success 'shallow cloning single tag' '
+ git clone --depth 1 --branch=TAGB1 "file://$(pwd)/." shallow7 &&
+ cat >taglist.expected <<\EOF &&
+TAGB1
+TAGB2
+EOF
+ GIT_DIR=shallow7/.git git tag -l >taglist.actual &&
+ test_cmp taglist.expected taglist.actual &&
+
+ echo "in-pack: 4" > count7.expected &&
+ GIT_DIR=shallow7/.git git count-objects -v |
+ grep "^in-pack" > count7.actual &&
+ test_cmp count7.expected count7.actual
+'
+
+test_expect_success 'clone shallow with packed refs' '
+ git pack-refs --all &&
+ git clone --depth 1 --branch A "file://$(pwd)/." shallow8 &&
+ echo "in-pack: 4" > count8.expected &&
+ GIT_DIR=shallow8/.git git count-objects -v |
+ grep "^in-pack" > count8.actual &&
+ test_cmp count8.expected count8.actual
+'
+
+test_expect_success 'in_vain not triggered before first ACK' '
+ rm -rf myserver myclient &&
+ git init myserver &&
+ test_commit -C myserver foo &&
+ git clone "file://$(pwd)/myserver" myclient &&
+
+ # MAX_IN_VAIN is 256. Because of batching, the client will send 496
+ # (16+32+64+128+256) commits, not 256, before giving up. So create 496
+ # irrelevant commits.
+ test_commit_bulk -C myclient 496 &&
+
+ # The new commit that the client wants to fetch.
+ test_commit -C myserver bar &&
+
+ git -C myclient fetch --progress origin 2>log &&
+ test_grep "remote: Total 3 " log
+'
+
+test_expect_success 'in_vain resetted upon ACK' '
+ test_when_finished rm -f log trace2 &&
+ rm -rf myserver myclient &&
+ git init myserver &&
+
+ # Linked list of commits on main. The first is common; the rest are
+ # not.
+ test_commit -C myserver first_main_commit &&
+ git clone "file://$(pwd)/myserver" myclient &&
+ test_commit_bulk -C myclient 255 &&
+
+ # Another linked list of commits on anotherbranch with no connection to
+ # main. The first is common; the rest are not.
+ git -C myserver checkout --orphan anotherbranch &&
+ test_commit -C myserver first_anotherbranch_commit &&
+ git -C myclient fetch origin anotherbranch:refs/heads/anotherbranch &&
+ git -C myclient checkout anotherbranch &&
+ test_commit_bulk -C myclient 255 &&
+
+ # The new commit that the client wants to fetch.
+ git -C myserver checkout main &&
+ test_commit -C myserver to_fetch &&
+
+ # The client will send (as "have"s) all 256 commits in anotherbranch
+ # first. The 256th commit is common between the client and the server,
+ # and should reset in_vain. This allows negotiation to continue until
+ # the client reports that first_anotherbranch_commit is common.
+ GIT_TRACE2_EVENT="$(pwd)/trace2" git -C myclient fetch --progress origin main 2>log &&
+ grep \"key\":\"total_rounds\",\"value\":\"6\" trace2 &&
+ test_grep "Total 3 " log
+'
+
+test_expect_success 'fetch in shallow repo unreachable shallow objects' '
+ (
+ git clone --bare --branch B --single-branch "file://$(pwd)/." no-reflog &&
+ git clone --depth 1 "file://$(pwd)/no-reflog" shallow9 &&
+ cd no-reflog &&
+ git tag -d TAGB1 TAGB2 &&
+ git update-ref refs/heads/B B~~ &&
+ git gc --prune=now &&
+ cd ../shallow9 &&
+ git fetch origin &&
+ git fsck --no-dangling
+ )
+'
+test_expect_success 'fetch creating new shallow root' '
+ (
+ git clone "file://$(pwd)/." shallow10 &&
+ git commit --allow-empty -m empty &&
+ cd shallow10 &&
+ git fetch --depth=1 --progress 2>actual &&
+ # This should fetch only the empty commit, no tree or
+ # blob objects
+ test_grep "remote: Total 1" actual
+ )
+'
+
+test_expect_success 'setup tests for the --stdin parameter' '
+ for head in C D E F
+ do
+ add $head || return 1
+ done &&
+ for head in A B C D E F
+ do
+ git tag $head $head || return 1
+ done &&
+ cat >input <<-\EOF &&
+ refs/heads/C
+ refs/heads/A
+ refs/heads/D
+ refs/tags/C
+ refs/heads/B
+ refs/tags/A
+ refs/heads/E
+ refs/tags/B
+ refs/tags/E
+ refs/tags/D
+ EOF
+ sort <input >expect &&
+ (
+ echo refs/heads/E &&
+ echo refs/tags/E &&
+ cat input
+ ) >input.dup
+'
+
+test_expect_success 'setup fetch refs from cmdline v[12]' '
+ cp -r client client0 &&
+ cp -r client client1 &&
+ cp -r client client2
+'
+
+for version in '' 0 1 2
+do
+ test_expect_success "protocol.version=$version fetch refs from cmdline" "
+ (
+ cd client$version &&
+ GIT_TEST_PROTOCOL_VERSION=$version git fetch-pack --no-progress .. \$(cat ../input)
+ ) >output &&
+ cut -d ' ' -f 2 <output | sort >actual &&
+ test_cmp expect actual
+ "
+done
+
+test_expect_success 'fetch refs from stdin' '
+ (
+ cd client &&
+ git fetch-pack --stdin --no-progress .. <../input
+ ) >output &&
+ cut -d " " -f 2 <output | sort >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'fetch mixed refs from cmdline and stdin' '
+ (
+ cd client &&
+ tail -n +5 ../input |
+ git fetch-pack --stdin --no-progress .. $(head -n 4 ../input)
+ ) >output &&
+ cut -d " " -f 2 <output | sort >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'test duplicate refs from stdin' '
+ (
+ cd client &&
+ git fetch-pack --stdin --no-progress .. <../input.dup
+ ) >output &&
+ cut -d " " -f 2 <output | sort >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'set up tests of missing reference' '
+ cat >expect-error <<-\EOF
+ error: no such remote ref refs/heads/xyzzy
+ EOF
+'
+
+test_expect_success 'test lonely missing ref' '
+ (
+ cd client &&
+ test_must_fail git fetch-pack --no-progress .. refs/heads/xyzzy 2>../error-m
+ ) &&
+ test_cmp expect-error error-m
+'
+
+test_expect_success 'test missing ref after existing' '
+ (
+ cd client &&
+ test_must_fail git fetch-pack --no-progress .. refs/heads/A refs/heads/xyzzy 2>../error-em
+ ) &&
+ test_cmp expect-error error-em
+'
+
+test_expect_success 'test missing ref before existing' '
+ (
+ cd client &&
+ test_must_fail git fetch-pack --no-progress .. refs/heads/xyzzy refs/heads/A 2>../error-me
+ ) &&
+ test_cmp expect-error error-me
+'
+
+test_expect_success 'test --all, --depth, and explicit head' '
+ (
+ cd client &&
+ git fetch-pack --no-progress --all --depth=1 .. refs/heads/A
+ ) >out-adh 2>error-adh
+'
+
+test_expect_success 'test --all, --depth, and explicit tag' '
+ git tag OLDTAG refs/heads/B~5 &&
+ (
+ cd client &&
+ git fetch-pack --no-progress --all --depth=1 .. refs/tags/OLDTAG
+ ) >out-adt 2>error-adt
+'
+
+test_expect_success 'test --all with tag to non-tip' '
+ git commit --allow-empty -m non-tip &&
+ git commit --allow-empty -m tip &&
+ git tag -m "annotated" non-tip HEAD^ &&
+ (
+ cd client &&
+ git fetch-pack --all ..
+ )
+'
+
+test_expect_success 'test --all wrt tag to non-commits' '
+ # create tag-to-{blob,tree,commit,tag}, making sure all tagged objects
+ # are reachable only via created tag references.
+ blob=$(echo "hello blob" | git hash-object -t blob -w --stdin) &&
+ git tag -a -m "tag -> blob" tag-to-blob $blob &&
+
+ tree=$(printf "100644 blob $blob\tfile" | git mktree) &&
+ git tag -a -m "tag -> tree" tag-to-tree $tree &&
+
+ tree2=$(printf "100644 blob $blob\tfile2" | git mktree) &&
+ commit=$(git commit-tree -m "hello commit" $tree) &&
+ git tag -a -m "tag -> commit" tag-to-commit $commit &&
+
+ blob2=$(echo "hello blob2" | git hash-object -t blob -w --stdin) &&
+ tag=$(git mktag <<-EOF
+ object $blob2
+ type blob
+ tag tag-to-blob2
+ tagger author A U Thor <author@example.com> 0 +0000
+
+ hello tag
+ EOF
+ ) &&
+ git tag -a -m "tag -> tag" tag-to-tag $tag &&
+
+ # `fetch-pack --all` should succeed fetching all those objects.
+ mkdir fetchall &&
+ (
+ cd fetchall &&
+ git init &&
+ git fetch-pack --all .. &&
+ git cat-file blob $blob >/dev/null &&
+ git cat-file tree $tree >/dev/null &&
+ git cat-file commit $commit >/dev/null &&
+ git cat-file tag $tag >/dev/null
+ )
+'
+
+test_expect_success 'shallow fetch with tags does not break the repository' '
+ mkdir repo1 &&
+ (
+ cd repo1 &&
+ git init &&
+ test_commit 1 &&
+ test_commit 2 &&
+ test_commit 3 &&
+ mkdir repo2 &&
+ cd repo2 &&
+ git init &&
+ git fetch --depth=2 ../.git main:branch &&
+ git fsck
+ )
+'
+
+test_expect_success 'fetch-pack can fetch a raw sha1' '
+ git init hidden &&
+ (
+ cd hidden &&
+ test_commit 1 &&
+ test_commit 2 &&
+ git update-ref refs/hidden/one HEAD^ &&
+ git config transfer.hiderefs refs/hidden &&
+ git config uploadpack.allowtipsha1inwant true
+ ) &&
+ git fetch-pack hidden $(git -C hidden rev-parse refs/hidden/one)
+'
+
+test_expect_success 'fetch-pack can fetch a raw sha1 that is advertised as a ref' '
+ rm -rf server client &&
+ git init server &&
+ test_commit -C server 1 &&
+
+ git init client &&
+ git -C client fetch-pack ../server \
+ $(git -C server rev-parse refs/heads/main)
+'
+
+test_expect_success 'fetch-pack can fetch a raw sha1 overlapping a named ref' '
+ rm -rf server client &&
+ git init server &&
+ test_commit -C server 1 &&
+ test_commit -C server 2 &&
+
+ git init client &&
+ git -C client fetch-pack ../server \
+ $(git -C server rev-parse refs/tags/1) refs/tags/1
+'
+
+test_expect_success 'fetch-pack cannot fetch a raw sha1 that is not advertised as a ref' '
+ rm -rf server &&
+
+ git init server &&
+ test_commit -C server 5 &&
+ git -C server tag -d 5 &&
+ test_commit -C server 6 &&
+
+ git init client &&
+ # Some protocol versions (e.g. 2) support fetching
+ # unadvertised objects, so restrict this test to v0.
+ test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 git -C client fetch-pack ../server \
+ $(git -C server rev-parse refs/heads/main^) 2>err &&
+ test_grep "Server does not allow request for unadvertised object" err
+'
+
+check_prot_path () {
+ cat >expected <<-EOF &&
+ Diag: url=$1
+ Diag: protocol=$2
+ Diag: path=$3
+ EOF
+ git fetch-pack --diag-url "$1" | grep -v hostandport= >actual &&
+ test_cmp expected actual
+}
+
+check_prot_host_port_path () {
+ case "$2" in
+ *ssh*)
+ pp=ssh
+ uah=userandhost
+ ehost=$(echo $3 | tr -d "[]")
+ diagport="Diag: port=$4"
+ ;;
+ *)
+ pp=$p
+ uah=hostandport
+ ehost=$(echo $3$4 | sed -e "s/22$/:22/" -e "s/NONE//")
+ diagport=""
+ ;;
+ esac
+ cat >exp <<-EOF &&
+ Diag: url=$1
+ Diag: protocol=$pp
+ Diag: $uah=$ehost
+ $diagport
+ Diag: path=$5
+ EOF
+ grep -v "^$" exp >expected
+ git fetch-pack --diag-url "$1" >actual &&
+ test_cmp expected actual
+}
+
+for r in repo re:po re/po
+do
+ # git or ssh with scheme
+ for p in "ssh+git" "git+ssh" git ssh
+ do
+ for h in host user@host user@[::1] user@::1
+ do
+ for c in "" :
+ do
+ test_expect_success "fetch-pack --diag-url $p://$h$c/$r" '
+ check_prot_host_port_path $p://$h/$r $p "$h" NONE "/$r"
+ '
+ # "/~" -> "~" conversion
+ test_expect_success "fetch-pack --diag-url $p://$h$c/~$r" '
+ check_prot_host_port_path $p://$h/~$r $p "$h" NONE "~$r"
+ '
+ done
+ done
+ for h in host User@host User@[::1]
+ do
+ test_expect_success "fetch-pack --diag-url $p://$h:22/$r" '
+ check_prot_host_port_path $p://$h:22/$r $p "$h" 22 "/$r"
+ '
+ done
+ done
+ # file with scheme
+ for p in file
+ do
+ test_expect_success !MINGW "fetch-pack --diag-url $p://$h/$r" '
+ check_prot_path $p://$h/$r $p "/$r"
+ '
+ test_expect_success MINGW "fetch-pack --diag-url $p://$h/$r" '
+ check_prot_path $p://$h/$r $p "//$h/$r"
+ '
+ test_expect_success MINGW "fetch-pack --diag-url $p:///$r" '
+ check_prot_path $p:///$r $p "/$r"
+ '
+ # No "/~" -> "~" conversion for file
+ test_expect_success !MINGW "fetch-pack --diag-url $p://$h/~$r" '
+ check_prot_path $p://$h/~$r $p "/~$r"
+ '
+ test_expect_success MINGW "fetch-pack --diag-url $p://$h/~$r" '
+ check_prot_path $p://$h/~$r $p "//$h/~$r"
+ '
+ done
+ # file without scheme
+ for h in nohost nohost:12 [::1] [::1]:23 [ [:aa
+ do
+ test_expect_success "fetch-pack --diag-url ./$h:$r" '
+ check_prot_path ./$h:$r $p "./$h:$r"
+ '
+ # No "/~" -> "~" conversion for file
+ test_expect_success "fetch-pack --diag-url ./$p:$h/~$r" '
+ check_prot_path ./$p:$h/~$r $p "./$p:$h/~$r"
+ '
+ done
+ #ssh without scheme
+ p=ssh
+ for h in host [::1]
+ do
+ test_expect_success "fetch-pack --diag-url $h:$r" '
+ check_prot_host_port_path $h:$r $p "$h" NONE "$r"
+ '
+ # Do "/~" -> "~" conversion
+ test_expect_success "fetch-pack --diag-url $h:/~$r" '
+ check_prot_host_port_path $h:/~$r $p "$h" NONE "~$r"
+ '
+ done
+done
+
+test_expect_success MINGW 'fetch-pack --diag-url file://c:/repo' '
+ check_prot_path file://c:/repo file c:/repo
+'
+test_expect_success MINGW 'fetch-pack --diag-url c:repo' '
+ check_prot_path c:repo file c:repo
+'
+
+test_expect_success 'clone shallow since ...' '
+ test_create_repo shallow-since &&
+ (
+ cd shallow-since &&
+ GIT_COMMITTER_DATE="100000000 +0700" git commit --allow-empty -m one &&
+ GIT_COMMITTER_DATE="200000000 +0700" git commit --allow-empty -m two &&
+ GIT_COMMITTER_DATE="300000000 +0700" git commit --allow-empty -m three &&
+ git clone --shallow-since "300000000 +0700" "file://$(pwd)/." ../shallow11 &&
+ git -C ../shallow11 log --pretty=tformat:%s HEAD >actual &&
+ echo three >expected &&
+ test_cmp expected actual
+ )
+'
+
+test_expect_success 'fetch shallow since ...' '
+ GIT_TRACE2_EVENT=$(pwd)/shallow11/trace2_event \
+ git -C shallow11 fetch --shallow-since "200000000 +0700" origin &&
+ git -C shallow11 log --pretty=tformat:%s origin/main >actual &&
+ cat >expected <<-\EOF &&
+ three
+ two
+ EOF
+ test_cmp expected actual &&
+ grep \"fetch-info\".*\"deepen-since\":true shallow11/trace2_event
+'
+
+test_expect_success 'clone shallow since selects no commits' '
+ test_create_repo shallow-since-the-future &&
+ (
+ cd shallow-since-the-future &&
+ GIT_COMMITTER_DATE="100000000 +0700" git commit --allow-empty -m one &&
+ GIT_COMMITTER_DATE="200000000 +0700" git commit --allow-empty -m two &&
+ GIT_COMMITTER_DATE="300000000 +0700" git commit --allow-empty -m three &&
+ test_must_fail git clone --shallow-since "900000000 +0700" "file://$(pwd)/." ../shallow111
+ )
+'
+
+# A few subtle things about the request in this test:
+#
+# - the server must have commit-graphs present and enabled
+#
+# - the history is such that our want/have share a common ancestor ("base"
+# here)
+#
+# - we send only a single have, which is fewer than a normal client would
+# send. This ensures that we don't parse "base" up front with
+# parse_object(), but rather traverse to it as a parent while deciding if we
+# can stop the "have" negotiation, and call parse_commit(). The former
+# sees the actual object data and so always loads the three oid, whereas the
+# latter will try to load it lazily.
+#
+# - we must use protocol v2, because it handles the "have" negotiation before
+# processing the shallow directives
+#
+test_expect_success 'shallow since with commit graph and already-seen commit' '
+ test_create_repo shallow-since-graph &&
+ (
+ cd shallow-since-graph &&
+ test_commit base &&
+ test_commit main &&
+ git checkout -b other HEAD^ &&
+ test_commit other &&
+ git commit-graph write --reachable &&
+ git config core.commitGraph true &&
+
+ GIT_PROTOCOL=version=2 git upload-pack . <<-EOF >/dev/null
+ 0012command=fetch
+ $(echo "object-format=$(test_oid algo)" | packetize)
+ 00010013deepen-since 1
+ $(echo "want $(git rev-parse other)" | packetize)
+ $(echo "have $(git rev-parse main)" | packetize)
+ 0000
+ EOF
+ )
+'
+
+test_expect_success 'shallow clone exclude tag two' '
+ test_create_repo shallow-exclude &&
+ (
+ cd shallow-exclude &&
+ test_commit one &&
+ test_commit two &&
+ test_commit three &&
+ git clone --shallow-exclude two "file://$(pwd)/." ../shallow12 &&
+ git -C ../shallow12 log --pretty=tformat:%s HEAD >actual &&
+ echo three >expected &&
+ test_cmp expected actual
+ )
+'
+
+test_expect_success 'fetch exclude tag one' '
+ git -C shallow12 fetch --shallow-exclude one origin &&
+ git -C shallow12 log --pretty=tformat:%s origin/main >actual &&
+ test_write_lines three two >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'fetching deepen' '
+ test_create_repo shallow-deepen &&
+ (
+ cd shallow-deepen &&
+ test_commit one &&
+ test_commit two &&
+ test_commit three &&
+ git clone --depth 1 "file://$(pwd)/." deepen &&
+ test_commit four &&
+ git -C deepen log --pretty=tformat:%s main >actual &&
+ echo three >expected &&
+ test_cmp expected actual &&
+ git -C deepen fetch --deepen=1 &&
+ git -C deepen log --pretty=tformat:%s origin/main >actual &&
+ cat >expected <<-\EOF &&
+ four
+ three
+ two
+ EOF
+ test_cmp expected actual
+ )
+'
+
+test_negotiation_algorithm_default () {
+ test_when_finished rm -rf clientv0 clientv2 &&
+ rm -rf server client &&
+ git init server &&
+ test_commit -C server both_have_1 &&
+ git -C server tag -d both_have_1 &&
+ test_commit -C server both_have_2 &&
+
+ git clone server client &&
+ test_commit -C server server_has &&
+ test_commit -C client client_has &&
+
+ # In both protocol v0 and v2, ensure that the parent of both_have_2 is
+ # not sent as a "have" line. The client should know that the server has
+ # both_have_2, so it only needs to inform the server that it has
+ # both_have_2, and the server can infer the rest.
+
+ rm -f trace &&
+ cp -r client clientv0 &&
+ GIT_TRACE_PACKET="$(pwd)/trace" git -C clientv0 \
+ "$@" fetch origin server_has both_have_2 &&
+ grep "have $(git -C client rev-parse client_has)" trace &&
+ grep "have $(git -C client rev-parse both_have_2)" trace &&
+ ! grep "have $(git -C client rev-parse both_have_2^)" trace &&
+
+ rm -f trace &&
+ cp -r client clientv2 &&
+ GIT_TRACE_PACKET="$(pwd)/trace" git -C clientv2 -c protocol.version=2 \
+ "$@" fetch origin server_has both_have_2 &&
+ grep "have $(git -C client rev-parse client_has)" trace &&
+ grep "have $(git -C client rev-parse both_have_2)" trace &&
+ ! grep "have $(git -C client rev-parse both_have_2^)" trace
+}
+
+test_expect_success 'use ref advertisement to prune "have" lines sent' '
+ test_negotiation_algorithm_default
+'
+
+test_expect_success 'same as last but with config overrides' '
+ test_negotiation_algorithm_default \
+ -c feature.experimental=true \
+ -c fetch.negotiationAlgorithm=consecutive
+'
+
+test_expect_success 'ensure bogus fetch.negotiationAlgorithm yields error' '
+ test_when_finished rm -rf clientv0 &&
+ cp -r client clientv0 &&
+ test_must_fail git -C clientv0 --fetch.negotiationAlgorithm=bogus \
+ fetch origin server_has both_have_2
+'
+
+test_expect_success 'filtering by size' '
+ rm -rf server client &&
+ test_create_repo server &&
+ test_commit -C server one &&
+ test_config -C server uploadpack.allowfilter 1 &&
+
+ test_create_repo client &&
+ GIT_TRACE2_EVENT=$(pwd)/client/trace2_event \
+ git -C client fetch-pack --filter=blob:limit=0 ../server HEAD &&
+
+ # Ensure that object is not inadvertently fetched
+ commit=$(git -C server rev-parse HEAD) &&
+ blob=$(git hash-object server/one.t) &&
+ git -C client rev-list --objects --missing=allow-any "$commit" >oids &&
+ ! grep "$blob" oids &&
+
+ grep \"fetch-info\".*\"filter\":\"blob:limit\" client/trace2_event
+'
+
+test_expect_success 'filtering by size has no effect if support for it is not advertised' '
+ rm -rf server client &&
+ test_create_repo server &&
+ test_commit -C server one &&
+
+ test_create_repo client &&
+ git -C client fetch-pack --filter=blob:limit=0 ../server HEAD 2> err &&
+
+ # Ensure that object is fetched
+ commit=$(git -C server rev-parse HEAD) &&
+ blob=$(git hash-object server/one.t) &&
+ git -C client rev-list --objects --missing=allow-any "$commit" >oids &&
+ grep "$blob" oids &&
+
+ test_grep "filtering not recognized by server" err
+'
+
+fetch_filter_blob_limit_zero () {
+ SERVER="$1"
+ URL="$2"
+
+ rm -rf "$SERVER" client &&
+ test_create_repo "$SERVER" &&
+ test_commit -C "$SERVER" one &&
+ test_config -C "$SERVER" uploadpack.allowfilter 1 &&
+
+ git clone "$URL" client &&
+
+ test_commit -C "$SERVER" two &&
+
+ git -C client fetch --filter=blob:limit=0 origin HEAD:somewhere &&
+
+ # Ensure that commit is fetched, but blob is not
+ commit=$(git -C "$SERVER" rev-parse two) &&
+ blob=$(git hash-object server/two.t) &&
+ git -C client rev-list --objects --missing=allow-any "$commit" >oids &&
+ grep "$commit" oids &&
+ ! grep "$blob" oids
+}
+
+test_expect_success 'fetch with --filter=blob:limit=0' '
+ fetch_filter_blob_limit_zero server server
+'
+
+. "$TEST_DIRECTORY"/lib-httpd.sh
+start_httpd
+
+test_expect_success 'fetch with --filter=blob:limit=0 and HTTP' '
+ fetch_filter_blob_limit_zero "$HTTPD_DOCUMENT_ROOT_PATH/server" "$HTTPD_URL/smart/server"
+'
+
+# DO NOT add non-httpd-specific tests here, because the last part of this
+# test script is only executed when httpd is available and enabled.
+
+test_done
diff --git a/t/t5501-fetch-push-alternates.sh b/t/t5501-fetch-push-alternates.sh
new file mode 100755
index 0000000..66f19a4
--- /dev/null
+++ b/t/t5501-fetch-push-alternates.sh
@@ -0,0 +1,69 @@
+#!/bin/sh
+
+test_description='fetch/push involving alternates'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+count_objects () {
+ loose=0 inpack=0
+ eval "$(
+ git count-objects -v |
+ sed -n -e 's/^count: \(.*\)/loose=\1/p' \
+ -e 's/^in-pack: \(.*\)/inpack=\1/p'
+ )" &&
+ echo $(( $loose + $inpack ))
+}
+
+
+test_expect_success setup '
+ (
+ git init original &&
+ cd original &&
+ i=0 &&
+ while test $i -le 100
+ do
+ echo "$i" >count &&
+ git add count &&
+ git commit -m "$i" || exit
+ i=$(($i + 1))
+ done
+ ) &&
+ (
+ git clone --reference=original "file://$(pwd)/original" one &&
+ cd one &&
+ echo Z >count &&
+ git add count &&
+ git commit -m Z &&
+ count_objects >../one.count
+ ) &&
+ A=$(pwd)/original/.git/objects &&
+ git init receiver &&
+ echo "$A" >receiver/.git/objects/info/alternates &&
+ git init fetcher &&
+ echo "$A" >fetcher/.git/objects/info/alternates
+'
+
+test_expect_success 'pushing into a repository with the same alternate' '
+ (
+ cd one &&
+ git push ../receiver main:refs/heads/it
+ ) &&
+ (
+ cd receiver &&
+ count_objects >../receiver.count
+ ) &&
+ test_cmp one.count receiver.count
+'
+
+test_expect_success 'fetching from a repository with the same alternate' '
+ (
+ cd fetcher &&
+ git fetch ../one main:refs/heads/it &&
+ count_objects >../fetcher.count
+ ) &&
+ test_cmp one.count fetcher.count
+'
+
+test_done
diff --git a/t/t5502-quickfetch.sh b/t/t5502-quickfetch.sh
new file mode 100755
index 0000000..7b3ff21
--- /dev/null
+++ b/t/t5502-quickfetch.sh
@@ -0,0 +1,146 @@
+#!/bin/sh
+
+test_description='test quickfetch from local'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+
+ test_tick &&
+ echo ichi >file &&
+ git add file &&
+ git commit -m initial &&
+
+ cnt=$( (
+ git count-objects | sed -e "s/ *objects,.*//"
+ ) ) &&
+ test $cnt -eq 3
+'
+
+test_expect_success 'clone without alternate' '
+
+ (
+ mkdir cloned &&
+ cd cloned &&
+ git init-db &&
+ git remote add -f origin ..
+ ) &&
+ cnt=$( (
+ cd cloned &&
+ git count-objects | sed -e "s/ *objects,.*//"
+ ) ) &&
+ test $cnt -eq 3
+'
+
+test_expect_success 'further commits in the original' '
+
+ test_tick &&
+ echo ni >file &&
+ git commit -a -m second &&
+
+ cnt=$( (
+ git count-objects | sed -e "s/ *objects,.*//"
+ ) ) &&
+ test $cnt -eq 6
+'
+
+test_expect_success 'copy commit and tree but not blob by hand' '
+
+ git rev-list --objects HEAD |
+ git pack-objects --stdout |
+ (
+ cd cloned &&
+ git unpack-objects
+ ) &&
+
+ cnt=$( (
+ cd cloned &&
+ git count-objects | sed -e "s/ *objects,.*//"
+ ) ) &&
+ test $cnt -eq 6 &&
+
+ blob=$(git rev-parse HEAD:file | sed -e "s|..|&/|") &&
+ test -f "cloned/.git/objects/$blob" &&
+ rm -f "cloned/.git/objects/$blob" &&
+
+ cnt=$( (
+ cd cloned &&
+ git count-objects | sed -e "s/ *objects,.*//"
+ ) ) &&
+ test $cnt -eq 5
+
+'
+
+test_expect_success 'quickfetch should not leave a corrupted repository' '
+
+ (
+ cd cloned &&
+ git fetch
+ ) &&
+
+ cnt=$( (
+ cd cloned &&
+ git count-objects | sed -e "s/ *objects,.*//"
+ ) ) &&
+ test $cnt -eq 6
+
+'
+
+test_expect_success 'quickfetch should not copy from alternate' '
+
+ (
+ mkdir quickclone &&
+ cd quickclone &&
+ git init-db &&
+ (cd ../.git/objects && pwd) >.git/objects/info/alternates &&
+ git remote add origin .. &&
+ git fetch -k -k
+ ) &&
+ obj_cnt=$( (
+ cd quickclone &&
+ git count-objects | sed -e "s/ *objects,.*//"
+ ) ) &&
+ pck_cnt=$( (
+ cd quickclone &&
+ git count-objects -v | sed -n -e "/packs:/{
+ s/packs://
+ p
+ q
+ }"
+ ) ) &&
+ origin_main=$( (
+ cd quickclone &&
+ git rev-parse origin/main
+ ) ) &&
+ echo "loose objects: $obj_cnt, packfiles: $pck_cnt" &&
+ test $obj_cnt -eq 0 &&
+ test $pck_cnt -eq 0 &&
+ test z$origin_main = z$(git rev-parse main)
+
+'
+
+test_expect_success 'quickfetch should handle ~1000 refs (on Windows)' '
+
+ git gc &&
+ head=$(git rev-parse HEAD) &&
+ branchprefix="$head refs/heads/branch" &&
+ for i in 0 1 2 3 4 5 6 7 8 9; do
+ for j in 0 1 2 3 4 5 6 7 8 9; do
+ for k in 0 1 2 3 4 5 6 7 8 9; do
+ echo "$branchprefix$i$j$k" >> .git/packed-refs || return 1
+ done
+ done
+ done &&
+ (
+ cd cloned &&
+ git fetch &&
+ git fetch
+ )
+
+'
+
+test_done
diff --git a/t/t5503-tagfollow.sh b/t/t5503-tagfollow.sh
new file mode 100755
index 0000000..5ebbaa4
--- /dev/null
+++ b/t/t5503-tagfollow.sh
@@ -0,0 +1,164 @@
+#!/bin/sh
+
+test_description='test automatic tag following'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# End state of the repository:
+#
+# T - tag1 S - tag2
+# / /
+# L - A ------ O ------ B
+# \ \ \
+# \ C - origin/cat \
+# origin/main main
+
+test_expect_success setup '
+ test_tick &&
+ echo ichi >file &&
+ git add file &&
+ git commit -m L &&
+ L=$(git rev-parse --verify HEAD) &&
+
+ (
+ mkdir cloned &&
+ cd cloned &&
+ git init-db &&
+ git remote add -f origin ..
+ ) &&
+
+ test_tick &&
+ echo A >file &&
+ git add file &&
+ git commit -m A &&
+ A=$(git rev-parse --verify HEAD)
+'
+
+U=UPLOAD_LOG
+UPATH="$(pwd)/$U"
+
+test_expect_success 'setup expect' '
+cat - <<EOF >expect
+want $A
+EOF
+'
+
+get_needs () {
+ test -s "$1" &&
+ perl -alne '
+ next unless $F[1] eq "upload-pack<";
+ next unless $F[2] eq "want";
+ print $F[2], " ", $F[3];
+ ' "$1"
+}
+
+test_expect_success 'fetch A (new commit : 1 connection)' '
+ rm -f $U &&
+ (
+ cd cloned &&
+ GIT_TRACE_PACKET=$UPATH git fetch &&
+ test $A = $(git rev-parse --verify origin/main)
+ ) &&
+ get_needs $U >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success "create tag T on A, create C on branch cat" '
+ git tag -a -m tag1 tag1 $A &&
+ T=$(git rev-parse --verify tag1) &&
+
+ git checkout -b cat &&
+ echo C >file &&
+ git add file &&
+ git commit -m C &&
+ C=$(git rev-parse --verify HEAD) &&
+ git checkout main
+'
+
+test_expect_success 'setup expect' '
+cat - <<EOF >expect
+want $C
+want $T
+EOF
+'
+
+test_expect_success 'fetch C, T (new branch, tag : 1 connection)' '
+ rm -f $U &&
+ (
+ cd cloned &&
+ GIT_TRACE_PACKET=$UPATH git fetch &&
+ test $C = $(git rev-parse --verify origin/cat) &&
+ test $T = $(git rev-parse --verify tag1) &&
+ test $A = $(git rev-parse --verify tag1^0)
+ ) &&
+ get_needs $U >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success "create commits O, B, tag S on B" '
+ test_tick &&
+ echo O >file &&
+ git add file &&
+ git commit -m O &&
+
+ test_tick &&
+ echo B >file &&
+ git add file &&
+ git commit -m B &&
+ B=$(git rev-parse --verify HEAD) &&
+
+ git tag -a -m tag2 tag2 $B &&
+ S=$(git rev-parse --verify tag2)
+'
+
+test_expect_success 'setup expect' '
+cat - <<EOF >expect
+want $B
+want $S
+EOF
+'
+
+test_expect_success 'fetch B, S (commit and tag : 1 connection)' '
+ rm -f $U &&
+ (
+ cd cloned &&
+ GIT_TRACE_PACKET=$UPATH git fetch &&
+ test $B = $(git rev-parse --verify origin/main) &&
+ test $B = $(git rev-parse --verify tag2^0) &&
+ test $S = $(git rev-parse --verify tag2)
+ ) &&
+ get_needs $U >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'setup expect' '
+cat - <<EOF >expect
+want $B
+want $S
+EOF
+'
+
+test_expect_success 'new clone fetch main and tags' '
+ test_might_fail git branch -D cat &&
+ rm -f $U &&
+ (
+ mkdir clone2 &&
+ cd clone2 &&
+ git init &&
+ git remote add origin .. &&
+ GIT_TRACE_PACKET=$UPATH git fetch &&
+ test $B = $(git rev-parse --verify origin/main) &&
+ test $S = $(git rev-parse --verify tag2) &&
+ test $B = $(git rev-parse --verify tag2^0) &&
+ test $T = $(git rev-parse --verify tag1) &&
+ test $A = $(git rev-parse --verify tag1^0)
+ ) &&
+ get_needs $U >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t5504-fetch-receive-strict.sh b/t/t5504-fetch-receive-strict.sh
new file mode 100755
index 0000000..138e677
--- /dev/null
+++ b/t/t5504-fetch-receive-strict.sh
@@ -0,0 +1,373 @@
+#!/bin/sh
+
+test_description='fetch/receive strict mode'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup and inject "corrupt or missing" object' '
+ echo hello >greetings &&
+ git add greetings &&
+ git commit -m greetings &&
+
+ S=$(git rev-parse :greetings | sed -e "s|^..|&/|") &&
+ X=$(echo bye | git hash-object -w --stdin | sed -e "s|^..|&/|") &&
+ echo $S >S &&
+ echo $X >X &&
+ cp .git/objects/$S .git/objects/$S.back &&
+ mv -f .git/objects/$X .git/objects/$S &&
+
+ test_must_fail git fsck
+'
+
+test_expect_success 'fetch without strict' '
+ rm -rf dst &&
+ git init dst &&
+ (
+ cd dst &&
+ git config fetch.fsckobjects false &&
+ git config transfer.fsckobjects false &&
+ test_must_fail git fetch ../.git main
+ )
+'
+
+test_expect_success 'fetch with !fetch.fsckobjects' '
+ rm -rf dst &&
+ git init dst &&
+ (
+ cd dst &&
+ git config fetch.fsckobjects false &&
+ git config transfer.fsckobjects true &&
+ test_must_fail git fetch ../.git main
+ )
+'
+
+test_expect_success 'fetch with fetch.fsckobjects' '
+ rm -rf dst &&
+ git init dst &&
+ (
+ cd dst &&
+ git config fetch.fsckobjects true &&
+ git config transfer.fsckobjects false &&
+ test_must_fail git fetch ../.git main
+ )
+'
+
+test_expect_success 'fetch with transfer.fsckobjects' '
+ rm -rf dst &&
+ git init dst &&
+ (
+ cd dst &&
+ git config transfer.fsckobjects true &&
+ test_must_fail git fetch ../.git main
+ )
+'
+
+cat >exp <<EOF
+To dst
+! refs/heads/main:refs/heads/test [remote rejected] (missing necessary objects)
+Done
+EOF
+
+test_expect_success 'push without strict' '
+ rm -rf dst &&
+ git init dst &&
+ (
+ cd dst &&
+ git config fetch.fsckobjects false &&
+ git config transfer.fsckobjects false
+ ) &&
+ test_must_fail git push --porcelain dst main:refs/heads/test >act &&
+ test_cmp exp act
+'
+
+test_expect_success 'push with !receive.fsckobjects' '
+ rm -rf dst &&
+ git init dst &&
+ (
+ cd dst &&
+ git config receive.fsckobjects false &&
+ git config transfer.fsckobjects true
+ ) &&
+ test_must_fail git push --porcelain dst main:refs/heads/test >act &&
+ test_cmp exp act
+'
+
+cat >exp <<EOF
+To dst
+! refs/heads/main:refs/heads/test [remote rejected] (unpacker error)
+EOF
+
+test_expect_success 'push with receive.fsckobjects' '
+ rm -rf dst &&
+ git init dst &&
+ (
+ cd dst &&
+ git config receive.fsckobjects true &&
+ git config transfer.fsckobjects false
+ ) &&
+ test_must_fail git push --porcelain dst main:refs/heads/test >act &&
+ test_cmp exp act
+'
+
+test_expect_success 'push with transfer.fsckobjects' '
+ rm -rf dst &&
+ git init dst &&
+ (
+ cd dst &&
+ git config transfer.fsckobjects true
+ ) &&
+ test_must_fail git push --porcelain dst main:refs/heads/test >act &&
+ test_cmp exp act
+'
+
+test_expect_success 'repair the "corrupt or missing" object' '
+ mv -f .git/objects/$(cat S) .git/objects/$(cat X) &&
+ mv .git/objects/$(cat S).back .git/objects/$(cat S) &&
+ rm -rf .git/objects/$(cat X) &&
+ git fsck
+'
+
+cat >bogus-commit <<EOF
+tree $EMPTY_TREE
+author Bugs Bunny 1234567890 +0000
+committer Bugs Bunny <bugs@bun.ni> 1234567890 +0000
+
+This commit object intentionally broken
+EOF
+
+test_expect_success 'setup bogus commit' '
+ commit="$(git hash-object --literally -t commit -w --stdin <bogus-commit)"
+'
+
+test_expect_success 'fsck with no skipList input' '
+ test_must_fail git fsck 2>err &&
+ test_grep "missingEmail" err
+'
+
+test_expect_success 'setup sorted and unsorted skipLists' '
+ cat >SKIP.unsorted <<-EOF &&
+ $(test_oid 004)
+ $(test_oid 002)
+ $commit
+ $(test_oid 001)
+ $(test_oid 003)
+ EOF
+ sort SKIP.unsorted >SKIP.sorted
+'
+
+test_expect_success 'fsck with sorted skipList' '
+ git -c fsck.skipList=SKIP.sorted fsck
+'
+
+test_expect_success 'fsck with unsorted skipList' '
+ git -c fsck.skipList=SKIP.unsorted fsck
+'
+
+test_expect_success 'fsck with invalid or bogus skipList input' '
+ git -c fsck.skipList=/dev/null -c fsck.missingEmail=ignore fsck &&
+ test_must_fail git -c fsck.skipList=does-not-exist -c fsck.missingEmail=ignore fsck 2>err &&
+ test_grep "could not open.*: does-not-exist" err &&
+ test_must_fail git -c fsck.skipList=.git/config -c fsck.missingEmail=ignore fsck 2>err &&
+ test_grep "invalid object name: \[core\]" err
+'
+
+test_expect_success 'fsck with other accepted skipList input (comments & empty lines)' '
+ cat >SKIP.with-comment <<-EOF &&
+ # Some bad commit
+ $(test_oid 001)
+ EOF
+ test_must_fail git -c fsck.skipList=SKIP.with-comment fsck 2>err-with-comment &&
+ test_grep "missingEmail" err-with-comment &&
+ cat >SKIP.with-empty-line <<-EOF &&
+ $(test_oid 001)
+
+ $(test_oid 002)
+ EOF
+ test_must_fail git -c fsck.skipList=SKIP.with-empty-line fsck 2>err-with-empty-line &&
+ test_grep "missingEmail" err-with-empty-line
+'
+
+test_expect_success 'fsck no garbage output from comments & empty lines errors' '
+ test_line_count = 1 err-with-comment &&
+ test_line_count = 1 err-with-empty-line
+'
+
+test_expect_success 'fsck with invalid abbreviated skipList input' '
+ echo $commit | test_copy_bytes 20 >SKIP.abbreviated &&
+ test_must_fail git -c fsck.skipList=SKIP.abbreviated fsck 2>err-abbreviated &&
+ test_grep "^fatal: invalid object name: " err-abbreviated
+'
+
+test_expect_success 'fsck with exhaustive accepted skipList input (various types of comments etc.)' '
+ >SKIP.exhaustive &&
+ echo "# A commented line" >>SKIP.exhaustive &&
+ echo "" >>SKIP.exhaustive &&
+ echo " " >>SKIP.exhaustive &&
+ echo " # Comment after whitespace" >>SKIP.exhaustive &&
+ echo "$commit # Our bad commit (with leading whitespace and trailing comment)" >>SKIP.exhaustive &&
+ echo "# Some bad commit (leading whitespace)" >>SKIP.exhaustive &&
+ echo " $(test_oid 001)" >>SKIP.exhaustive &&
+ git -c fsck.skipList=SKIP.exhaustive fsck 2>err &&
+ test_must_be_empty err
+'
+
+test_expect_success 'push with receive.fsck.skipList' '
+ git push . $commit:refs/heads/bogus &&
+ rm -rf dst &&
+ git init dst &&
+ git --git-dir=dst/.git config receive.fsckObjects true &&
+ test_must_fail git push --porcelain dst bogus &&
+ echo $commit >dst/.git/SKIP &&
+
+ # receive.fsck.* does not fall back on fsck.*
+ git --git-dir=dst/.git config fsck.skipList SKIP &&
+ test_must_fail git push --porcelain dst bogus &&
+
+ # Invalid and/or bogus skipList input
+ git --git-dir=dst/.git config receive.fsck.skipList /dev/null &&
+ test_must_fail git push --porcelain dst bogus &&
+ git --git-dir=dst/.git config receive.fsck.skipList does-not-exist &&
+ test_must_fail git push --porcelain dst bogus 2>err &&
+ test_grep "could not open.*: does-not-exist" err &&
+ git --git-dir=dst/.git config receive.fsck.skipList config &&
+ test_must_fail git push --porcelain dst bogus 2>err &&
+ test_grep "invalid object name: \[core\]" err &&
+
+ git --git-dir=dst/.git config receive.fsck.skipList SKIP &&
+ git push --porcelain dst bogus
+'
+
+test_expect_success 'fetch with fetch.fsck.skipList' '
+ refspec=refs/heads/bogus:refs/heads/bogus &&
+ git push . $commit:refs/heads/bogus &&
+ rm -rf dst &&
+ git init dst &&
+ git --git-dir=dst/.git config fetch.fsckObjects true &&
+ test_must_fail git --git-dir=dst/.git fetch "file://$(pwd)" $refspec &&
+ git --git-dir=dst/.git config fetch.fsck.skipList /dev/null &&
+ test_must_fail git --git-dir=dst/.git fetch "file://$(pwd)" $refspec &&
+ echo $commit >dst/.git/SKIP &&
+
+ # fetch.fsck.* does not fall back on fsck.*
+ git --git-dir=dst/.git config fsck.skipList dst/.git/SKIP &&
+ test_must_fail git --git-dir=dst/.git fetch "file://$(pwd)" $refspec &&
+
+ # Invalid and/or bogus skipList input
+ git --git-dir=dst/.git config fetch.fsck.skipList /dev/null &&
+ test_must_fail git --git-dir=dst/.git fetch "file://$(pwd)" $refspec &&
+ git --git-dir=dst/.git config fetch.fsck.skipList does-not-exist &&
+ test_must_fail git --git-dir=dst/.git fetch "file://$(pwd)" $refspec 2>err &&
+ test_grep "could not open.*: does-not-exist" err &&
+ git --git-dir=dst/.git config fetch.fsck.skipList dst/.git/config &&
+ test_must_fail git --git-dir=dst/.git fetch "file://$(pwd)" $refspec 2>err &&
+ test_grep "invalid object name: \[core\]" err &&
+
+ git --git-dir=dst/.git config fetch.fsck.skipList dst/.git/SKIP &&
+ git --git-dir=dst/.git fetch "file://$(pwd)" $refspec
+'
+
+test_expect_success 'fsck.<unknownmsg-id> dies' '
+ test_must_fail git -c fsck.whatEver=ignore fsck 2>err &&
+ test_grep "Unhandled message id: whatever" err
+'
+
+test_expect_success 'push with receive.fsck.missingEmail=warn' '
+ git push . $commit:refs/heads/bogus &&
+ rm -rf dst &&
+ git init dst &&
+ git --git-dir=dst/.git config receive.fsckobjects true &&
+ test_must_fail git push --porcelain dst bogus &&
+
+ # receive.fsck.<msg-id> does not fall back on fsck.<msg-id>
+ git --git-dir=dst/.git config fsck.missingEmail warn &&
+ test_must_fail git push --porcelain dst bogus &&
+
+ # receive.fsck.<unknownmsg-id> warns
+ git --git-dir=dst/.git config \
+ receive.fsck.whatEver error &&
+
+ git --git-dir=dst/.git config \
+ receive.fsck.missingEmail warn &&
+ git push --porcelain dst bogus >act 2>&1 &&
+ grep "missingEmail" act &&
+ test_grep "skipping unknown msg id.*whatever" act &&
+ git --git-dir=dst/.git branch -D bogus &&
+ git --git-dir=dst/.git config --add \
+ receive.fsck.missingEmail ignore &&
+ git push --porcelain dst bogus >act 2>&1 &&
+ ! grep "missingEmail" act
+'
+
+test_expect_success 'fetch with fetch.fsck.missingEmail=warn' '
+ refspec=refs/heads/bogus:refs/heads/bogus &&
+ git push . $commit:refs/heads/bogus &&
+ rm -rf dst &&
+ git init dst &&
+ git --git-dir=dst/.git config fetch.fsckobjects true &&
+ test_must_fail git --git-dir=dst/.git fetch "file://$(pwd)" $refspec &&
+
+ # fetch.fsck.<msg-id> does not fall back on fsck.<msg-id>
+ git --git-dir=dst/.git config fsck.missingEmail warn &&
+ test_must_fail git --git-dir=dst/.git fetch "file://$(pwd)" $refspec &&
+
+ # receive.fsck.<unknownmsg-id> warns
+ git --git-dir=dst/.git config \
+ fetch.fsck.whatEver error &&
+
+ git --git-dir=dst/.git config \
+ fetch.fsck.missingEmail warn &&
+ git --git-dir=dst/.git fetch "file://$(pwd)" $refspec >act 2>&1 &&
+ grep "missingEmail" act &&
+ test_grep "Skipping unknown msg id.*whatever" act &&
+ rm -rf dst &&
+ git init dst &&
+ git --git-dir=dst/.git config fetch.fsckobjects true &&
+ git --git-dir=dst/.git config \
+ fetch.fsck.missingEmail ignore &&
+ git --git-dir=dst/.git fetch "file://$(pwd)" $refspec >act 2>&1 &&
+ ! grep "missingEmail" act
+'
+
+test_expect_success \
+ 'receive.fsck.unterminatedHeader=warn triggers error' '
+ rm -rf dst &&
+ git init dst &&
+ git --git-dir=dst/.git config receive.fsckobjects true &&
+ git --git-dir=dst/.git config \
+ receive.fsck.unterminatedheader warn &&
+ test_must_fail git push --porcelain dst HEAD >act 2>&1 &&
+ grep "Cannot demote unterminatedheader" act
+'
+
+test_expect_success \
+ 'fetch.fsck.unterminatedHeader=warn triggers error' '
+ rm -rf dst &&
+ git init dst &&
+ git --git-dir=dst/.git config fetch.fsckobjects true &&
+ git --git-dir=dst/.git config \
+ fetch.fsck.unterminatedheader warn &&
+ test_must_fail git --git-dir=dst/.git fetch "file://$(pwd)" HEAD &&
+ grep "Cannot demote unterminatedheader" act
+'
+
+test_expect_success 'badFilemode is not a strict error' '
+ git init --bare badmode.git &&
+ tree=$(
+ cd badmode.git &&
+ blob=$(echo blob | git hash-object -w --stdin | hex2oct) &&
+ printf "123456 foo\0${blob}" |
+ git hash-object -t tree --stdin -w --literally
+ ) &&
+
+ rm -rf dst.git &&
+ git init --bare dst.git &&
+ git -C dst.git config transfer.fsckObjects true &&
+
+ git -C badmode.git push ../dst.git $tree:refs/tags/tree 2>err &&
+ grep "$tree: badFilemode" err
+'
+
+test_done
diff --git a/t/t5505-remote.sh b/t/t5505-remote.sh
new file mode 100755
index 0000000..7789ff1
--- /dev/null
+++ b/t/t5505-remote.sh
@@ -0,0 +1,1495 @@
+#!/bin/sh
+
+test_description='git remote porcelain-ish'
+
+. ./test-lib.sh
+
+setup_repository () {
+ mkdir "$1" && (
+ cd "$1" &&
+ git init -b main &&
+ >file &&
+ git add file &&
+ test_tick &&
+ git commit -m "Initial" &&
+ git checkout -b side &&
+ >elif &&
+ git add elif &&
+ test_tick &&
+ git commit -m "Second" &&
+ git checkout main
+ )
+}
+
+tokens_match () {
+ echo "$1" | tr ' ' '\012' | sort | sed -e '/^$/d' >expect &&
+ echo "$2" | tr ' ' '\012' | sort | sed -e '/^$/d' >actual &&
+ test_cmp expect actual
+}
+
+check_remote_track () {
+ actual=$(git remote show "$1" | sed -ne 's|^ \(.*\) tracked$|\1|p')
+ shift &&
+ tokens_match "$*" "$actual"
+}
+
+check_tracking_branch () {
+ f="" &&
+ r=$(git for-each-ref "--format=%(refname)" |
+ sed -ne "s|^refs/remotes/$1/||p") &&
+ shift &&
+ tokens_match "$*" "$r"
+}
+
+test_expect_success setup '
+ setup_repository one &&
+ setup_repository two &&
+ (
+ cd two &&
+ git branch another
+ ) &&
+ git clone one test
+'
+
+test_expect_success 'add remote whose URL agrees with url.<...>.insteadOf' '
+ test_config url.git@host.com:team/repo.git.insteadOf myremote &&
+ git remote add myremote git@host.com:team/repo.git
+'
+
+test_expect_success 'remote information for the origin' '
+ (
+ cd test &&
+ tokens_match origin "$(git remote)" &&
+ check_remote_track origin main side &&
+ check_tracking_branch origin HEAD main side
+ )
+'
+
+test_expect_success 'add another remote' '
+ (
+ cd test &&
+ git remote add -f second ../two &&
+ tokens_match "origin second" "$(git remote)" &&
+ check_tracking_branch second main side another &&
+ git for-each-ref "--format=%(refname)" refs/remotes |
+ sed -e "/^refs\/remotes\/origin\//d" \
+ -e "/^refs\/remotes\/second\//d" >actual &&
+ test_must_be_empty actual
+ )
+'
+
+test_expect_success 'setup bare clone for server' '
+ git clone --bare "file://$(pwd)/one" srv.bare &&
+ git -C srv.bare config --local uploadpack.allowfilter 1 &&
+ git -C srv.bare config --local uploadpack.allowanysha1inwant 1
+'
+
+test_expect_success 'filters for promisor remotes are listed by git remote -v' '
+ test_when_finished "rm -rf pc" &&
+ git clone --filter=blob:none "file://$(pwd)/srv.bare" pc &&
+ git -C pc remote -v >out &&
+ grep "srv.bare (fetch) \[blob:none\]" out &&
+
+ git -C pc config remote.origin.partialCloneFilter object:type=commit &&
+ git -C pc remote -v >out &&
+ grep "srv.bare (fetch) \[object:type=commit\]" out
+'
+
+test_expect_success 'filters should not be listed for non promisor remotes (remote -v)' '
+ test_when_finished "rm -rf pc" &&
+ git clone one pc &&
+ git -C pc remote -v >out &&
+ ! grep "(fetch) \[.*\]" out
+'
+
+test_expect_success 'filters are listed by git remote -v only' '
+ test_when_finished "rm -rf pc" &&
+ git clone --filter=blob:none "file://$(pwd)/srv.bare" pc &&
+ git -C pc remote >out &&
+ ! grep "\[blob:none\]" out &&
+
+ git -C pc remote show >out &&
+ ! grep "\[blob:none\]" out
+'
+
+test_expect_success 'check remote-tracking' '
+ (
+ cd test &&
+ check_remote_track origin main side &&
+ check_remote_track second main side another
+ )
+'
+
+test_expect_success 'remote forces tracking branches' '
+ (
+ cd test &&
+ case $(git config remote.second.fetch) in
+ +*) true ;;
+ *) false ;;
+ esac
+ )
+'
+
+test_expect_success 'remove remote' '
+ (
+ cd test &&
+ git symbolic-ref refs/remotes/second/HEAD refs/remotes/second/main &&
+ git remote rm second
+ )
+'
+
+test_expect_success 'remove remote' '
+ (
+ cd test &&
+ tokens_match origin "$(git remote)" &&
+ check_remote_track origin main side &&
+ git for-each-ref "--format=%(refname)" refs/remotes |
+ sed -e "/^refs\/remotes\/origin\//d" >actual &&
+ test_must_be_empty actual
+ )
+'
+
+test_expect_success 'remove remote protects local branches' '
+ (
+ cd test &&
+ cat >expect1 <<-\EOF &&
+ Note: A branch outside the refs/remotes/ hierarchy was not removed;
+ to delete it, use:
+ git branch -d main
+ EOF
+ cat >expect2 <<-\EOF &&
+ Note: Some branches outside the refs/remotes/ hierarchy were not removed;
+ to delete them, use:
+ git branch -d foobranch
+ git branch -d main
+ EOF
+ git tag footag &&
+ git config --add remote.oops.fetch "+refs/*:refs/*" &&
+ git remote remove oops 2>actual1 &&
+ git branch foobranch &&
+ git config --add remote.oops.fetch "+refs/*:refs/*" &&
+ git remote rm oops 2>actual2 &&
+ git branch -d foobranch &&
+ git tag -d footag &&
+ test_cmp expect1 actual1 &&
+ test_cmp expect2 actual2
+ )
+'
+
+test_expect_success 'remove errors out early when deleting non-existent branch' '
+ (
+ cd test &&
+ echo "error: No such remote: '\''foo'\''" >expect &&
+ test_expect_code 2 git remote rm foo 2>actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'remove remote with a branch without configured merge' '
+ test_when_finished "(
+ git -C test checkout main;
+ git -C test branch -D two;
+ git -C test config --remove-section remote.two;
+ git -C test config --remove-section branch.second;
+ true
+ )" &&
+ (
+ cd test &&
+ git remote add two ../two &&
+ git fetch two &&
+ git checkout -b second two/main^0 &&
+ git config branch.second.remote two &&
+ git checkout main &&
+ git remote rm two
+ )
+'
+
+test_expect_success 'rename errors out early when deleting non-existent branch' '
+ (
+ cd test &&
+ echo "error: No such remote: '\''foo'\''" >expect &&
+ test_expect_code 2 git remote rename foo bar 2>actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'rename errors out early when new name is invalid' '
+ test_config remote.foo.vcs bar &&
+ echo "fatal: '\''invalid...name'\'' is not a valid remote name" >expect &&
+ test_must_fail git remote rename foo invalid...name 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'add existing foreign_vcs remote' '
+ test_config remote.foo.vcs bar &&
+ echo "error: remote foo already exists." >expect &&
+ test_expect_code 3 git remote add foo bar 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'add existing foreign_vcs remote' '
+ test_config remote.foo.vcs bar &&
+ test_config remote.bar.vcs bar &&
+ echo "error: remote bar already exists." >expect &&
+ test_expect_code 3 git remote rename foo bar 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'add invalid foreign_vcs remote' '
+ echo "fatal: '\''invalid...name'\'' is not a valid remote name" >expect &&
+ test_must_fail git remote add invalid...name bar 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'without subcommand' '
+ echo origin >expect &&
+ git -C test remote >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'without subcommand accepts -v' '
+ cat >expect <<-EOF &&
+ origin $(pwd)/one (fetch)
+ origin $(pwd)/one (push)
+ EOF
+ git -C test remote -v >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'without subcommand does not take arguments' '
+ test_expect_code 129 git -C test remote origin 2>err &&
+ grep "^error: unknown subcommand:" err
+'
+
+cat >test/expect <<EOF
+* remote origin
+ Fetch URL: $(pwd)/one
+ Push URL: $(pwd)/one
+ HEAD branch: main
+ Remote branches:
+ main new (next fetch will store in remotes/origin)
+ side tracked
+ Local branches configured for 'git pull':
+ ahead merges with remote main
+ main merges with remote main
+ octopus merges with remote topic-a
+ and with remote topic-b
+ and with remote topic-c
+ rebase rebases onto remote main
+ Local refs configured for 'git push':
+ main pushes to main (local out of date)
+ main pushes to upstream (create)
+* remote two
+ Fetch URL: ../two
+ Push URL: ../three
+ HEAD branch: main
+ Local refs configured for 'git push':
+ ahead forces to main (fast-forwardable)
+ main pushes to another (up to date)
+EOF
+
+test_expect_success 'show' '
+ (
+ cd test &&
+ git config --add remote.origin.fetch refs/heads/main:refs/heads/upstream &&
+ git fetch &&
+ git checkout -b ahead origin/main &&
+ echo 1 >>file &&
+ test_tick &&
+ git commit -m update file &&
+ git checkout main &&
+ git branch --track octopus origin/main &&
+ git branch --track rebase origin/main &&
+ git branch -d -r origin/main &&
+ git config --add remote.two.url ../two &&
+ git config --add remote.two.pushurl ../three &&
+ git config branch.rebase.rebase true &&
+ git config branch.octopus.merge "topic-a topic-b topic-c" &&
+ (
+ cd ../one &&
+ echo 1 >file &&
+ test_tick &&
+ git commit -m update file
+ ) &&
+ git config --add remote.origin.push : &&
+ git config --add remote.origin.push refs/heads/main:refs/heads/upstream &&
+ git config --add remote.origin.push +refs/tags/lastbackup &&
+ git config --add remote.two.push +refs/heads/ahead:refs/heads/main &&
+ git config --add remote.two.push refs/heads/main:refs/heads/another &&
+ git remote show origin two >output &&
+ git branch -d rebase octopus &&
+ test_cmp expect output
+ )
+'
+
+cat >expect <<EOF
+* remote origin
+ Fetch URL: $(pwd)/one
+ Push URL: $(pwd)/one
+ HEAD branch: main
+ Remote branches:
+ main skipped
+ side tracked
+ Local branches configured for 'git pull':
+ ahead merges with remote main
+ main merges with remote main
+ Local refs configured for 'git push':
+ main pushes to main (local out of date)
+ main pushes to upstream (create)
+EOF
+
+test_expect_success 'show with negative refspecs' '
+ test_when_finished "git -C test config --unset-all --fixed-value remote.origin.fetch ^refs/heads/main" &&
+ git -C test config --add remote.origin.fetch ^refs/heads/main &&
+ git -C test remote show origin >output &&
+ test_cmp expect output
+'
+
+cat >expect <<EOF
+* remote origin
+ Fetch URL: $(pwd)/one
+ Push URL: $(pwd)/one
+ HEAD branch: main
+ Remote branches:
+ main new (next fetch will store in remotes/origin)
+ side stale (use 'git remote prune' to remove)
+ Local branches configured for 'git pull':
+ ahead merges with remote main
+ main merges with remote main
+ Local refs configured for 'git push':
+ main pushes to main (local out of date)
+ main pushes to upstream (create)
+EOF
+
+test_expect_failure 'show stale with negative refspecs' '
+ test_when_finished "git -C test config --unset-all --fixed-value remote.origin.fetch ^refs/heads/side" &&
+ git -C test config --add remote.origin.fetch ^refs/heads/side &&
+ git -C test remote show origin >output &&
+ test_cmp expect output
+'
+
+cat >test/expect <<EOF
+* remote origin
+ Fetch URL: $(pwd)/one
+ Push URL: $(pwd)/one
+ HEAD branch: (not queried)
+ Remote branches: (status not queried)
+ main
+ side
+ Local branches configured for 'git pull':
+ ahead merges with remote main
+ main merges with remote main
+ Local refs configured for 'git push' (status not queried):
+ (matching) pushes to (matching)
+ refs/heads/main pushes to refs/heads/upstream
+ refs/tags/lastbackup forces to refs/tags/lastbackup
+EOF
+
+test_expect_success 'show -n' '
+ mv one one.unreachable &&
+ (
+ cd test &&
+ git remote show -n origin >output &&
+ mv ../one.unreachable ../one &&
+ test_cmp expect output
+ )
+'
+
+test_expect_success 'prune' '
+ (
+ cd one &&
+ git branch -m side side2
+ ) &&
+ (
+ cd test &&
+ git fetch origin &&
+ git remote prune origin &&
+ git rev-parse refs/remotes/origin/side2 &&
+ test_must_fail git rev-parse refs/remotes/origin/side
+ )
+'
+
+test_expect_success 'set-head --delete' '
+ (
+ cd test &&
+ git symbolic-ref refs/remotes/origin/HEAD &&
+ git remote set-head --delete origin &&
+ test_must_fail git symbolic-ref refs/remotes/origin/HEAD
+ )
+'
+
+test_expect_success 'set-head --auto' '
+ (
+ cd test &&
+ git remote set-head --auto origin &&
+ echo refs/remotes/origin/main >expect &&
+ git symbolic-ref refs/remotes/origin/HEAD >output &&
+ test_cmp expect output
+ )
+'
+
+test_expect_success 'set-head --auto has no problem w/multiple HEADs' '
+ (
+ cd test &&
+ git fetch two "refs/heads/*:refs/remotes/two/*" &&
+ git remote set-head --auto two >output 2>&1 &&
+ echo "two/HEAD set to main" >expect &&
+ test_cmp expect output
+ )
+'
+
+cat >test/expect <<\EOF
+refs/remotes/origin/side2
+EOF
+
+test_expect_success 'set-head explicit' '
+ (
+ cd test &&
+ git remote set-head origin side2 &&
+ git symbolic-ref refs/remotes/origin/HEAD >output &&
+ git remote set-head origin main &&
+ test_cmp expect output
+ )
+'
+
+cat >test/expect <<EOF
+Pruning origin
+URL: $(pwd)/one
+ * [would prune] origin/side2
+EOF
+
+test_expect_success 'prune --dry-run' '
+ git -C one branch -m side2 side &&
+ test_when_finished "git -C one branch -m side side2" &&
+ (
+ cd test &&
+ git remote prune --dry-run origin >output &&
+ git rev-parse refs/remotes/origin/side2 &&
+ test_must_fail git rev-parse refs/remotes/origin/side &&
+ test_cmp expect output
+ )
+'
+
+test_expect_success 'add --mirror && prune' '
+ mkdir mirror &&
+ (
+ cd mirror &&
+ git init --bare &&
+ git remote add --mirror -f origin ../one
+ ) &&
+ (
+ cd one &&
+ git branch -m side2 side
+ ) &&
+ (
+ cd mirror &&
+ git rev-parse --verify refs/heads/side2 &&
+ test_must_fail git rev-parse --verify refs/heads/side &&
+ git fetch origin &&
+ git remote prune origin &&
+ test_must_fail git rev-parse --verify refs/heads/side2 &&
+ git rev-parse --verify refs/heads/side
+ )
+'
+
+test_expect_success 'add --mirror=fetch' '
+ mkdir mirror-fetch &&
+ git init -b main mirror-fetch/parent &&
+ (
+ cd mirror-fetch/parent &&
+ test_commit one
+ ) &&
+ git init --bare mirror-fetch/child &&
+ (
+ cd mirror-fetch/child &&
+ git remote add --mirror=fetch -f parent ../parent
+ )
+'
+
+test_expect_success 'fetch mirrors act as mirrors during fetch' '
+ (
+ cd mirror-fetch/parent &&
+ git branch new &&
+ git branch -m main renamed
+ ) &&
+ (
+ cd mirror-fetch/child &&
+ git fetch parent &&
+ git rev-parse --verify refs/heads/new &&
+ git rev-parse --verify refs/heads/renamed
+ )
+'
+
+test_expect_success 'fetch mirrors can prune' '
+ (
+ cd mirror-fetch/child &&
+ git remote prune parent &&
+ test_must_fail git rev-parse --verify refs/heads/main
+ )
+'
+
+test_expect_success 'fetch mirrors do not act as mirrors during push' '
+ (
+ cd mirror-fetch/parent &&
+ git checkout HEAD^0
+ ) &&
+ (
+ cd mirror-fetch/child &&
+ git branch -m renamed renamed2 &&
+ git push parent :
+ ) &&
+ (
+ cd mirror-fetch/parent &&
+ git rev-parse --verify renamed &&
+ test_must_fail git rev-parse --verify refs/heads/renamed2
+ )
+'
+
+test_expect_success 'add fetch mirror with specific branches' '
+ git init --bare mirror-fetch/track &&
+ (
+ cd mirror-fetch/track &&
+ git remote add --mirror=fetch -t heads/new parent ../parent
+ )
+'
+
+test_expect_success 'fetch mirror respects specific branches' '
+ (
+ cd mirror-fetch/track &&
+ git fetch parent &&
+ git rev-parse --verify refs/heads/new &&
+ test_must_fail git rev-parse --verify refs/heads/renamed
+ )
+'
+
+test_expect_success 'add --mirror=push' '
+ mkdir mirror-push &&
+ git init --bare mirror-push/public &&
+ git init -b main mirror-push/private &&
+ (
+ cd mirror-push/private &&
+ test_commit one &&
+ git remote add --mirror=push public ../public
+ )
+'
+
+test_expect_success 'push mirrors act as mirrors during push' '
+ (
+ cd mirror-push/private &&
+ git branch new &&
+ git branch -m main renamed &&
+ git push public
+ ) &&
+ (
+ cd mirror-push/private &&
+ git rev-parse --verify refs/heads/new &&
+ git rev-parse --verify refs/heads/renamed &&
+ test_must_fail git rev-parse --verify refs/heads/main
+ )
+'
+
+test_expect_success 'push mirrors do not act as mirrors during fetch' '
+ (
+ cd mirror-push/public &&
+ git branch -m renamed renamed2 &&
+ git symbolic-ref HEAD refs/heads/renamed2
+ ) &&
+ (
+ cd mirror-push/private &&
+ git fetch public &&
+ git rev-parse --verify refs/heads/renamed &&
+ test_must_fail git rev-parse --verify refs/heads/renamed2
+ )
+'
+
+test_expect_success 'push mirrors do not allow you to specify refs' '
+ git init mirror-push/track &&
+ (
+ cd mirror-push/track &&
+ test_must_fail git remote add --mirror=push -t new public ../public
+ )
+'
+
+test_expect_success 'add alt && prune' '
+ mkdir alttst &&
+ (
+ cd alttst &&
+ git init &&
+ git remote add -f origin ../one &&
+ git config remote.alt.url ../one &&
+ git config remote.alt.fetch "+refs/heads/*:refs/remotes/origin/*"
+ ) &&
+ (
+ cd one &&
+ git branch -m side side2
+ ) &&
+ (
+ cd alttst &&
+ git rev-parse --verify refs/remotes/origin/side &&
+ test_must_fail git rev-parse --verify refs/remotes/origin/side2 &&
+ git fetch alt &&
+ git remote prune alt &&
+ test_must_fail git rev-parse --verify refs/remotes/origin/side &&
+ git rev-parse --verify refs/remotes/origin/side2
+ )
+'
+
+cat >test/expect <<\EOF
+some-tag
+EOF
+
+test_expect_success 'add with reachable tags (default)' '
+ (
+ cd one &&
+ >foobar &&
+ git add foobar &&
+ git commit -m "Foobar" &&
+ git tag -a -m "Foobar tag" foobar-tag &&
+ git reset --hard HEAD~1 &&
+ git tag -a -m "Some tag" some-tag
+ ) &&
+ mkdir add-tags &&
+ (
+ cd add-tags &&
+ git init &&
+ git remote add -f origin ../one &&
+ git tag -l some-tag >../test/output &&
+ git tag -l foobar-tag >>../test/output &&
+ test_must_fail git config remote.origin.tagopt
+ ) &&
+ test_cmp test/expect test/output
+'
+
+cat >test/expect <<\EOF
+some-tag
+foobar-tag
+--tags
+EOF
+
+test_expect_success 'add --tags' '
+ rm -rf add-tags &&
+ (
+ mkdir add-tags &&
+ cd add-tags &&
+ git init &&
+ git remote add -f --tags origin ../one &&
+ git tag -l some-tag >../test/output &&
+ git tag -l foobar-tag >>../test/output &&
+ git config remote.origin.tagopt >>../test/output
+ ) &&
+ test_cmp test/expect test/output
+'
+
+cat >test/expect <<\EOF
+--no-tags
+EOF
+
+test_expect_success 'add --no-tags' '
+ rm -rf add-tags &&
+ (
+ mkdir add-no-tags &&
+ cd add-no-tags &&
+ git init &&
+ git remote add -f --no-tags origin ../one &&
+ grep tagOpt .git/config &&
+ git tag -l some-tag >../test/output &&
+ git tag -l foobar-tag >../test/output &&
+ git config remote.origin.tagopt >>../test/output
+ ) &&
+ (
+ cd one &&
+ git tag -d some-tag foobar-tag
+ ) &&
+ test_cmp test/expect test/output
+'
+
+test_expect_success 'reject --no-no-tags' '
+ (
+ cd add-no-tags &&
+ test_must_fail git remote add -f --no-no-tags neworigin ../one
+ )
+'
+
+cat >one/expect <<\EOF
+ apis/main
+ apis/side
+ drosophila/another
+ drosophila/main
+ drosophila/side
+EOF
+
+test_expect_success 'update' '
+ (
+ cd one &&
+ git remote add drosophila ../two &&
+ git remote add apis ../mirror &&
+ git remote update &&
+ git branch -r >output &&
+ test_cmp expect output
+ )
+'
+
+cat >one/expect <<\EOF
+ drosophila/another
+ drosophila/main
+ drosophila/side
+ manduca/main
+ manduca/side
+ megaloprepus/main
+ megaloprepus/side
+EOF
+
+test_expect_success 'update with arguments' '
+ (
+ cd one &&
+ for b in $(git branch -r)
+ do
+ git branch -r -d $b || exit 1
+ done &&
+ git remote add manduca ../mirror &&
+ git remote add megaloprepus ../mirror &&
+ git config remotes.phobaeticus "drosophila megaloprepus" &&
+ git config remotes.titanus manduca &&
+ git remote update phobaeticus titanus &&
+ git branch -r >output &&
+ test_cmp expect output
+ )
+'
+
+test_expect_success 'update --prune' '
+ (
+ cd one &&
+ git branch -m side2 side3
+ ) &&
+ (
+ cd test &&
+ git remote update --prune &&
+ (
+ cd ../one &&
+ git branch -m side3 side2
+ ) &&
+ git rev-parse refs/remotes/origin/side3 &&
+ test_must_fail git rev-parse refs/remotes/origin/side2
+ )
+'
+
+cat >one/expect <<-\EOF
+ apis/main
+ apis/side
+ manduca/main
+ manduca/side
+ megaloprepus/main
+ megaloprepus/side
+EOF
+
+test_expect_success 'update default' '
+ (
+ cd one &&
+ for b in $(git branch -r)
+ do
+ git branch -r -d $b || exit 1
+ done &&
+ git config remote.drosophila.skipDefaultUpdate true &&
+ git remote update default &&
+ git branch -r >output &&
+ test_cmp expect output
+ )
+'
+
+cat >one/expect <<\EOF
+ drosophila/another
+ drosophila/main
+ drosophila/side
+EOF
+
+test_expect_success 'update default (overridden, with funny whitespace)' '
+ (
+ cd one &&
+ for b in $(git branch -r)
+ do
+ git branch -r -d $b || exit 1
+ done &&
+ git config remotes.default "$(printf "\t drosophila \n")" &&
+ git remote update default &&
+ git branch -r >output &&
+ test_cmp expect output
+ )
+'
+
+test_expect_success 'update (with remotes.default defined)' '
+ (
+ cd one &&
+ for b in $(git branch -r)
+ do
+ git branch -r -d $b || exit 1
+ done &&
+ git config remotes.default "drosophila" &&
+ git remote update &&
+ git branch -r >output &&
+ test_cmp expect output
+ )
+'
+
+test_expect_success '"remote show" does not show symbolic refs' '
+ git clone one three &&
+ (
+ cd three &&
+ git remote show origin >output &&
+ ! grep "^ *HEAD$" < output &&
+ ! grep -i stale < output
+ )
+'
+
+test_expect_success 'reject adding remote with an invalid name' '
+ test_must_fail git remote add some:url desired-name
+'
+
+# The first three test if the tracking branches are properly renamed,
+# the last two ones check if the config is updated.
+
+test_expect_success 'rename a remote' '
+ test_config_global remote.pushDefault origin &&
+ git clone one four &&
+ (
+ cd four &&
+ git config branch.main.pushRemote origin &&
+ GIT_TRACE2_EVENT=$(pwd)/trace \
+ git remote rename --progress origin upstream &&
+ test_region progress "Renaming remote references" trace &&
+ grep "pushRemote" .git/config &&
+ test -z "$(git for-each-ref refs/remotes/origin)" &&
+ test "$(git symbolic-ref refs/remotes/upstream/HEAD)" = "refs/remotes/upstream/main" &&
+ test "$(git rev-parse upstream/main)" = "$(git rev-parse main)" &&
+ test "$(git config remote.upstream.fetch)" = "+refs/heads/*:refs/remotes/upstream/*" &&
+ test "$(git config branch.main.remote)" = "upstream" &&
+ test "$(git config branch.main.pushRemote)" = "upstream" &&
+ test "$(git config --global remote.pushDefault)" = "origin"
+ )
+'
+
+test_expect_success 'rename a remote renames repo remote.pushDefault' '
+ git clone one four.1 &&
+ (
+ cd four.1 &&
+ git config remote.pushDefault origin &&
+ git remote rename origin upstream &&
+ grep pushDefault .git/config &&
+ test "$(git config --local remote.pushDefault)" = "upstream"
+ )
+'
+
+test_expect_success 'rename a remote renames repo remote.pushDefault but ignores global' '
+ test_config_global remote.pushDefault other &&
+ git clone one four.2 &&
+ (
+ cd four.2 &&
+ git config remote.pushDefault origin &&
+ git remote rename origin upstream &&
+ test "$(git config --global remote.pushDefault)" = "other" &&
+ test "$(git config --local remote.pushDefault)" = "upstream"
+ )
+'
+
+test_expect_success 'rename a remote renames repo remote.pushDefault but keeps global' '
+ test_config_global remote.pushDefault origin &&
+ git clone one four.3 &&
+ (
+ cd four.3 &&
+ git config remote.pushDefault origin &&
+ git remote rename origin upstream &&
+ test "$(git config --global remote.pushDefault)" = "origin" &&
+ test "$(git config --local remote.pushDefault)" = "upstream"
+ )
+'
+
+test_expect_success 'rename handles remote without fetch refspec' '
+ git clone --bare one no-refspec.git &&
+ # confirm assumption that bare clone does not create refspec
+ test_expect_code 5 \
+ git -C no-refspec.git config --unset-all remote.origin.fetch &&
+ git -C no-refspec.git config remote.origin.url >expect &&
+ git -C no-refspec.git remote rename origin foo &&
+ git -C no-refspec.git config remote.foo.url >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rename does not update a non-default fetch refspec' '
+ git clone one four.one &&
+ (
+ cd four.one &&
+ git config remote.origin.fetch +refs/heads/*:refs/heads/origin/* &&
+ git remote rename origin upstream &&
+ test "$(git config remote.upstream.fetch)" = "+refs/heads/*:refs/heads/origin/*" &&
+ git rev-parse -q origin/main
+ )
+'
+
+test_expect_success 'rename a remote with name part of fetch spec' '
+ git clone one four.two &&
+ (
+ cd four.two &&
+ git remote rename origin remote &&
+ git remote rename remote upstream &&
+ test "$(git config remote.upstream.fetch)" = "+refs/heads/*:refs/remotes/upstream/*"
+ )
+'
+
+test_expect_success 'rename a remote with name prefix of other remote' '
+ git clone one four.three &&
+ (
+ cd four.three &&
+ git remote add o git://example.com/repo.git &&
+ git remote rename o upstream &&
+ test "$(git rev-parse origin/main)" = "$(git rev-parse main)"
+ )
+'
+
+test_expect_success 'rename succeeds with existing remote.<target>.prune' '
+ git clone one four.four &&
+ test_when_finished git config --global --unset remote.upstream.prune &&
+ git config --global remote.upstream.prune true &&
+ git -C four.four remote rename origin upstream
+'
+
+test_expect_success 'remove a remote' '
+ test_config_global remote.pushDefault origin &&
+ git clone one four.five &&
+ (
+ cd four.five &&
+ git config branch.main.pushRemote origin &&
+ git remote remove origin &&
+ test -z "$(git for-each-ref refs/remotes/origin)" &&
+ test_must_fail git config branch.main.remote &&
+ test_must_fail git config branch.main.pushRemote &&
+ test "$(git config --global remote.pushDefault)" = "origin"
+ )
+'
+
+test_expect_success 'remove a remote removes repo remote.pushDefault' '
+ git clone one four.five.1 &&
+ (
+ cd four.five.1 &&
+ git config remote.pushDefault origin &&
+ git remote remove origin &&
+ test_must_fail git config --local remote.pushDefault
+ )
+'
+
+test_expect_success 'remove a remote removes repo remote.pushDefault but ignores global' '
+ test_config_global remote.pushDefault other &&
+ git clone one four.five.2 &&
+ (
+ cd four.five.2 &&
+ git config remote.pushDefault origin &&
+ git remote remove origin &&
+ test "$(git config --global remote.pushDefault)" = "other" &&
+ test_must_fail git config --local remote.pushDefault
+ )
+'
+
+test_expect_success 'remove a remote removes repo remote.pushDefault but keeps global' '
+ test_config_global remote.pushDefault origin &&
+ git clone one four.five.3 &&
+ (
+ cd four.five.3 &&
+ git config remote.pushDefault origin &&
+ git remote remove origin &&
+ test "$(git config --global remote.pushDefault)" = "origin" &&
+ test_must_fail git config --local remote.pushDefault
+ )
+'
+
+cat >remotes_origin <<EOF
+URL: $(pwd)/one
+Push: refs/heads/main:refs/heads/upstream
+Push: refs/heads/next:refs/heads/upstream2
+Pull: refs/heads/main:refs/heads/origin
+Pull: refs/heads/next:refs/heads/origin2
+EOF
+
+test_expect_success 'migrate a remote from named file in $GIT_DIR/remotes' '
+ git clone one five &&
+ origin_url=$(pwd)/one &&
+ (
+ cd five &&
+ git remote remove origin &&
+ mkdir -p .git/remotes &&
+ cat ../remotes_origin >.git/remotes/origin &&
+ git remote rename origin origin &&
+ test_path_is_missing .git/remotes/origin &&
+ test "$(git config remote.origin.url)" = "$origin_url" &&
+ cat >push_expected <<-\EOF &&
+ refs/heads/main:refs/heads/upstream
+ refs/heads/next:refs/heads/upstream2
+ EOF
+ cat >fetch_expected <<-\EOF &&
+ refs/heads/main:refs/heads/origin
+ refs/heads/next:refs/heads/origin2
+ EOF
+ git config --get-all remote.origin.push >push_actual &&
+ git config --get-all remote.origin.fetch >fetch_actual &&
+ test_cmp push_expected push_actual &&
+ test_cmp fetch_expected fetch_actual
+ )
+'
+
+test_expect_success 'migrate a remote from named file in $GIT_DIR/branches' '
+ git clone --template= one six &&
+ origin_url=$(pwd)/one &&
+ (
+ cd six &&
+ git remote rm origin &&
+ mkdir .git/branches &&
+ echo "$origin_url#main" >.git/branches/origin &&
+ git remote rename origin origin &&
+ test_path_is_missing .git/branches/origin &&
+ test "$(git config remote.origin.url)" = "$origin_url" &&
+ test "$(git config remote.origin.fetch)" = "refs/heads/main:refs/heads/origin" &&
+ test "$(git config remote.origin.push)" = "HEAD:refs/heads/main"
+ )
+'
+
+test_expect_success 'migrate a remote from named file in $GIT_DIR/branches (2)' '
+ git clone --template= one seven &&
+ (
+ cd seven &&
+ git remote rm origin &&
+ mkdir .git/branches &&
+ echo "quux#foom" > .git/branches/origin &&
+ git remote rename origin origin &&
+ test_path_is_missing .git/branches/origin &&
+ test "$(git config remote.origin.url)" = "quux" &&
+ test "$(git config remote.origin.fetch)" = "refs/heads/foom:refs/heads/origin" &&
+ test "$(git config remote.origin.push)" = "HEAD:refs/heads/foom"
+ )
+'
+
+test_expect_success 'remote prune to cause a dangling symref' '
+ git clone one eight &&
+ (
+ cd one &&
+ git checkout side2 &&
+ git branch -D main
+ ) &&
+ (
+ cd eight &&
+ git remote prune origin
+ ) >err 2>&1 &&
+ test_grep "has become dangling" err &&
+
+ : And the dangling symref will not cause other annoying errors &&
+ (
+ cd eight &&
+ git branch -a
+ ) 2>err &&
+ ! grep "points nowhere" err &&
+ (
+ cd eight &&
+ test_must_fail git branch nomore origin
+ ) 2>err &&
+ test_grep "dangling symref" err
+'
+
+test_expect_success 'show empty remote' '
+ test_create_repo empty &&
+ git clone empty empty-clone &&
+ (
+ cd empty-clone &&
+ git remote show origin
+ )
+'
+
+test_expect_success 'remote set-branches requires a remote' '
+ test_must_fail git remote set-branches &&
+ test_must_fail git remote set-branches --add
+'
+
+test_expect_success 'remote set-branches' '
+ echo "+refs/heads/*:refs/remotes/scratch/*" >expect.initial &&
+ sort <<-\EOF >expect.add &&
+ +refs/heads/*:refs/remotes/scratch/*
+ +refs/heads/other:refs/remotes/scratch/other
+ EOF
+ sort <<-\EOF >expect.replace &&
+ +refs/heads/maint:refs/remotes/scratch/maint
+ +refs/heads/main:refs/remotes/scratch/main
+ +refs/heads/next:refs/remotes/scratch/next
+ EOF
+ sort <<-\EOF >expect.add-two &&
+ +refs/heads/maint:refs/remotes/scratch/maint
+ +refs/heads/main:refs/remotes/scratch/main
+ +refs/heads/next:refs/remotes/scratch/next
+ +refs/heads/seen:refs/remotes/scratch/seen
+ +refs/heads/t/topic:refs/remotes/scratch/t/topic
+ EOF
+ sort <<-\EOF >expect.setup-ffonly &&
+ refs/heads/main:refs/remotes/scratch/main
+ +refs/heads/next:refs/remotes/scratch/next
+ EOF
+ sort <<-\EOF >expect.respect-ffonly &&
+ refs/heads/main:refs/remotes/scratch/main
+ +refs/heads/next:refs/remotes/scratch/next
+ +refs/heads/seen:refs/remotes/scratch/seen
+ EOF
+
+ git clone .git/ setbranches &&
+ (
+ cd setbranches &&
+ git remote rename origin scratch &&
+ git config --get-all remote.scratch.fetch >config-result &&
+ sort <config-result >../actual.initial &&
+
+ git remote set-branches scratch --add other &&
+ git config --get-all remote.scratch.fetch >config-result &&
+ sort <config-result >../actual.add &&
+
+ git remote set-branches scratch maint main next &&
+ git config --get-all remote.scratch.fetch >config-result &&
+ sort <config-result >../actual.replace &&
+
+ git remote set-branches --add scratch seen t/topic &&
+ git config --get-all remote.scratch.fetch >config-result &&
+ sort <config-result >../actual.add-two &&
+
+ git config --unset-all remote.scratch.fetch &&
+ git config remote.scratch.fetch \
+ refs/heads/main:refs/remotes/scratch/main &&
+ git config --add remote.scratch.fetch \
+ +refs/heads/next:refs/remotes/scratch/next &&
+ git config --get-all remote.scratch.fetch >config-result &&
+ sort <config-result >../actual.setup-ffonly &&
+
+ git remote set-branches --add scratch seen &&
+ git config --get-all remote.scratch.fetch >config-result &&
+ sort <config-result >../actual.respect-ffonly
+ ) &&
+ test_cmp expect.initial actual.initial &&
+ test_cmp expect.add actual.add &&
+ test_cmp expect.replace actual.replace &&
+ test_cmp expect.add-two actual.add-two &&
+ test_cmp expect.setup-ffonly actual.setup-ffonly &&
+ test_cmp expect.respect-ffonly actual.respect-ffonly
+'
+
+test_expect_success 'remote set-branches with --mirror' '
+ echo "+refs/*:refs/*" >expect.initial &&
+ echo "+refs/heads/main:refs/heads/main" >expect.replace &&
+ git clone --mirror .git/ setbranches-mirror &&
+ (
+ cd setbranches-mirror &&
+ git remote rename origin scratch &&
+ git config --get-all remote.scratch.fetch >../actual.initial &&
+
+ git remote set-branches scratch heads/main &&
+ git config --get-all remote.scratch.fetch >../actual.replace
+ ) &&
+ test_cmp expect.initial actual.initial &&
+ test_cmp expect.replace actual.replace
+'
+
+test_expect_success 'new remote' '
+ git remote add someremote foo &&
+ echo foo >expect &&
+ git config --get-all remote.someremote.url >actual &&
+ cmp expect actual
+'
+
+get_url_test () {
+ cat >expect &&
+ git remote get-url "$@" >actual &&
+ test_cmp expect actual
+}
+
+test_expect_success 'get-url on new remote' '
+ echo foo | get_url_test someremote &&
+ echo foo | get_url_test --all someremote &&
+ echo foo | get_url_test --push someremote &&
+ echo foo | get_url_test --push --all someremote
+'
+
+test_expect_success 'remote set-url with locked config' '
+ test_when_finished "rm -f .git/config.lock" &&
+ git config --get-all remote.someremote.url >expect &&
+ >.git/config.lock &&
+ test_must_fail git remote set-url someremote baz &&
+ git config --get-all remote.someremote.url >actual &&
+ cmp expect actual
+'
+
+test_expect_success 'remote set-url bar' '
+ git remote set-url someremote bar &&
+ echo bar >expect &&
+ git config --get-all remote.someremote.url >actual &&
+ cmp expect actual
+'
+
+test_expect_success 'remote set-url baz bar' '
+ git remote set-url someremote baz bar &&
+ echo baz >expect &&
+ git config --get-all remote.someremote.url >actual &&
+ cmp expect actual
+'
+
+test_expect_success 'remote set-url zot bar' '
+ test_must_fail git remote set-url someremote zot bar &&
+ echo baz >expect &&
+ git config --get-all remote.someremote.url >actual &&
+ cmp expect actual
+'
+
+test_expect_success 'remote set-url --push zot baz' '
+ test_must_fail git remote set-url --push someremote zot baz &&
+ echo "YYY" >expect &&
+ echo baz >>expect &&
+ test_must_fail git config --get-all remote.someremote.pushurl >actual &&
+ echo "YYY" >>actual &&
+ git config --get-all remote.someremote.url >>actual &&
+ cmp expect actual
+'
+
+test_expect_success 'remote set-url --push zot' '
+ git remote set-url --push someremote zot &&
+ echo zot >expect &&
+ echo "YYY" >>expect &&
+ echo baz >>expect &&
+ git config --get-all remote.someremote.pushurl >actual &&
+ echo "YYY" >>actual &&
+ git config --get-all remote.someremote.url >>actual &&
+ cmp expect actual
+'
+
+test_expect_success 'get-url with different urls' '
+ echo baz | get_url_test someremote &&
+ echo baz | get_url_test --all someremote &&
+ echo zot | get_url_test --push someremote &&
+ echo zot | get_url_test --push --all someremote
+'
+
+test_expect_success 'remote set-url --push qux zot' '
+ git remote set-url --push someremote qux zot &&
+ echo qux >expect &&
+ echo "YYY" >>expect &&
+ echo baz >>expect &&
+ git config --get-all remote.someremote.pushurl >actual &&
+ echo "YYY" >>actual &&
+ git config --get-all remote.someremote.url >>actual &&
+ cmp expect actual
+'
+
+test_expect_success 'remote set-url --push foo qu+x' '
+ git remote set-url --push someremote foo qu+x &&
+ echo foo >expect &&
+ echo "YYY" >>expect &&
+ echo baz >>expect &&
+ git config --get-all remote.someremote.pushurl >actual &&
+ echo "YYY" >>actual &&
+ git config --get-all remote.someremote.url >>actual &&
+ cmp expect actual
+'
+
+test_expect_success 'remote set-url --push --add aaa' '
+ git remote set-url --push --add someremote aaa &&
+ echo foo >expect &&
+ echo aaa >>expect &&
+ echo "YYY" >>expect &&
+ echo baz >>expect &&
+ git config --get-all remote.someremote.pushurl >actual &&
+ echo "YYY" >>actual &&
+ git config --get-all remote.someremote.url >>actual &&
+ cmp expect actual
+'
+
+test_expect_success 'get-url on multi push remote' '
+ echo foo | get_url_test --push someremote &&
+ get_url_test --push --all someremote <<-\EOF
+ foo
+ aaa
+ EOF
+'
+
+test_expect_success 'remote set-url --push bar aaa' '
+ git remote set-url --push someremote bar aaa &&
+ echo foo >expect &&
+ echo bar >>expect &&
+ echo "YYY" >>expect &&
+ echo baz >>expect &&
+ git config --get-all remote.someremote.pushurl >actual &&
+ echo "YYY" >>actual &&
+ git config --get-all remote.someremote.url >>actual &&
+ cmp expect actual
+'
+
+test_expect_success 'remote set-url --push --delete bar' '
+ git remote set-url --push --delete someremote bar &&
+ echo foo >expect &&
+ echo "YYY" >>expect &&
+ echo baz >>expect &&
+ git config --get-all remote.someremote.pushurl >actual &&
+ echo "YYY" >>actual &&
+ git config --get-all remote.someremote.url >>actual &&
+ cmp expect actual
+'
+
+test_expect_success 'remote set-url --push --delete foo' '
+ git remote set-url --push --delete someremote foo &&
+ echo "YYY" >expect &&
+ echo baz >>expect &&
+ test_must_fail git config --get-all remote.someremote.pushurl >actual &&
+ echo "YYY" >>actual &&
+ git config --get-all remote.someremote.url >>actual &&
+ cmp expect actual
+'
+
+test_expect_success 'remote set-url --add bbb' '
+ git remote set-url --add someremote bbb &&
+ echo "YYY" >expect &&
+ echo baz >>expect &&
+ echo bbb >>expect &&
+ test_must_fail git config --get-all remote.someremote.pushurl >actual &&
+ echo "YYY" >>actual &&
+ git config --get-all remote.someremote.url >>actual &&
+ cmp expect actual
+'
+
+test_expect_success 'get-url on multi fetch remote' '
+ echo baz | get_url_test someremote &&
+ get_url_test --all someremote <<-\EOF
+ baz
+ bbb
+ EOF
+'
+
+test_expect_success 'remote set-url --delete .*' '
+ test_must_fail git remote set-url --delete someremote .\* &&
+ echo "YYY" >expect &&
+ echo baz >>expect &&
+ echo bbb >>expect &&
+ test_must_fail git config --get-all remote.someremote.pushurl >actual &&
+ echo "YYY" >>actual &&
+ git config --get-all remote.someremote.url >>actual &&
+ cmp expect actual
+'
+
+test_expect_success 'remote set-url --delete bbb' '
+ git remote set-url --delete someremote bbb &&
+ echo "YYY" >expect &&
+ echo baz >>expect &&
+ test_must_fail git config --get-all remote.someremote.pushurl >actual &&
+ echo "YYY" >>actual &&
+ git config --get-all remote.someremote.url >>actual &&
+ cmp expect actual
+'
+
+test_expect_success 'remote set-url --delete baz' '
+ test_must_fail git remote set-url --delete someremote baz &&
+ echo "YYY" >expect &&
+ echo baz >>expect &&
+ test_must_fail git config --get-all remote.someremote.pushurl >actual &&
+ echo "YYY" >>actual &&
+ git config --get-all remote.someremote.url >>actual &&
+ cmp expect actual
+'
+
+test_expect_success 'remote set-url --add ccc' '
+ git remote set-url --add someremote ccc &&
+ echo "YYY" >expect &&
+ echo baz >>expect &&
+ echo ccc >>expect &&
+ test_must_fail git config --get-all remote.someremote.pushurl >actual &&
+ echo "YYY" >>actual &&
+ git config --get-all remote.someremote.url >>actual &&
+ cmp expect actual
+'
+
+test_expect_success 'remote set-url --delete baz' '
+ git remote set-url --delete someremote baz &&
+ echo "YYY" >expect &&
+ echo ccc >>expect &&
+ test_must_fail git config --get-all remote.someremote.pushurl >actual &&
+ echo "YYY" >>actual &&
+ git config --get-all remote.someremote.url >>actual &&
+ cmp expect actual
+'
+
+test_expect_success 'extra args: setup' '
+ # add a dummy origin so that this does not trigger failure
+ git remote add origin .
+'
+
+test_extra_arg () {
+ test_expect_success "extra args: $*" "
+ test_must_fail git remote $* bogus_extra_arg 2>actual &&
+ test_grep '^usage:' actual
+ "
+}
+
+test_extra_arg add nick url
+test_extra_arg rename origin newname
+test_extra_arg remove origin
+test_extra_arg set-head origin main
+# set-branches takes any number of args
+test_extra_arg get-url origin newurl
+test_extra_arg set-url origin newurl oldurl
+# show takes any number of args
+# prune takes any number of args
+# update takes any number of args
+
+test_expect_success 'add remote matching the "insteadOf" URL' '
+ git config url.xyz@example.com.insteadOf backup &&
+ git remote add backup xyz@example.com
+'
+
+test_expect_success 'unqualified <dst> refspec DWIM and advice' '
+ test_when_finished "(cd test && git tag -d some-tag)" &&
+ (
+ cd test &&
+ git tag -a -m "Some tag" some-tag main &&
+ for type in commit tag tree blob
+ do
+ if test "$type" = "blob"
+ then
+ oid=$(git rev-parse some-tag:file)
+ else
+ oid=$(git rev-parse some-tag^{$type})
+ fi &&
+ test_must_fail git push origin $oid:dst 2>err &&
+ test_grep "error: The destination you" err &&
+ test_grep "hint: Did you mean" err &&
+ test_must_fail git -c advice.pushUnqualifiedRefName=false \
+ push origin $oid:dst 2>err &&
+ test_grep "error: The destination you" err &&
+ test_grep ! "hint: Did you mean" err ||
+ exit 1
+ done
+ )
+'
+
+test_expect_success 'refs/remotes/* <src> refspec and unqualified <dst> DWIM and advice' '
+ (
+ cd two &&
+ git tag -a -m "Some tag" my-tag main &&
+ git update-ref refs/trees/my-head-tree HEAD^{tree} &&
+ git update-ref refs/blobs/my-file-blob HEAD:file
+ ) &&
+ (
+ cd test &&
+ git config --add remote.two.fetch "+refs/tags/*:refs/remotes/tags-from-two/*" &&
+ git config --add remote.two.fetch "+refs/trees/*:refs/remotes/trees-from-two/*" &&
+ git config --add remote.two.fetch "+refs/blobs/*:refs/remotes/blobs-from-two/*" &&
+ git fetch --no-tags two &&
+
+ test_must_fail git push origin refs/remotes/two/another:dst 2>err &&
+ test_grep "error: The destination you" err &&
+
+ test_must_fail git push origin refs/remotes/tags-from-two/my-tag:dst-tag 2>err &&
+ test_grep "error: The destination you" err &&
+
+ test_must_fail git push origin refs/remotes/trees-from-two/my-head-tree:dst-tree 2>err &&
+ test_grep "error: The destination you" err &&
+
+ test_must_fail git push origin refs/remotes/blobs-from-two/my-file-blob:dst-blob 2>err &&
+ test_grep "error: The destination you" err
+ )
+'
+
+test_done
diff --git a/t/t5506-remote-groups.sh b/t/t5506-remote-groups.sh
new file mode 100755
index 0000000..0e17617
--- /dev/null
+++ b/t/t5506-remote-groups.sh
@@ -0,0 +1,111 @@
+#!/bin/sh
+
+test_description='git remote group handling'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+mark() {
+ echo "$1" >mark
+}
+
+update_repo() {
+ (cd $1 &&
+ echo content >>file &&
+ git add file &&
+ git commit -F ../mark)
+}
+
+update_repos() {
+ update_repo one $1 &&
+ update_repo two $1
+}
+
+repo_fetched() {
+ if test "$(git log -1 --pretty=format:%s $1 --)" = "$(cat mark)"; then
+ echo >&2 "repo was fetched: $1"
+ return 0
+ fi
+ echo >&2 "repo was not fetched: $1"
+ return 1
+}
+
+test_expect_success 'setup' '
+ mkdir one && (cd one && git init) &&
+ mkdir two && (cd two && git init) &&
+ git remote add -m main one one &&
+ git remote add -m main two two
+'
+
+test_expect_success 'no group updates all' '
+ mark update-all &&
+ update_repos &&
+ git remote update &&
+ repo_fetched one &&
+ repo_fetched two
+'
+
+test_expect_success 'nonexistent group produces error' '
+ mark nonexistent &&
+ update_repos &&
+ test_must_fail git remote update nonexistent &&
+ ! repo_fetched one &&
+ ! repo_fetched two
+'
+
+test_expect_success 'updating group updates all members (remote update)' '
+ mark group-all &&
+ update_repos &&
+ git config --add remotes.all one &&
+ git config --add remotes.all two &&
+ git remote update all &&
+ repo_fetched one &&
+ repo_fetched two
+'
+
+test_expect_success 'updating group updates all members (fetch)' '
+ mark fetch-group-all &&
+ update_repos &&
+ git fetch all &&
+ repo_fetched one &&
+ repo_fetched two
+'
+
+test_expect_success 'updating group does not update non-members (remote update)' '
+ mark group-some &&
+ update_repos &&
+ git config --add remotes.some one &&
+ git remote update some &&
+ repo_fetched one &&
+ ! repo_fetched two
+'
+
+test_expect_success 'updating group does not update non-members (fetch)' '
+ mark fetch-group-some &&
+ update_repos &&
+ git config --add remotes.some one &&
+ git remote update some &&
+ repo_fetched one &&
+ ! repo_fetched two
+'
+
+test_expect_success 'updating remote name updates that remote' '
+ mark remote-name &&
+ update_repos &&
+ git remote update one &&
+ repo_fetched one &&
+ ! repo_fetched two
+'
+
+test_expect_success 'updating group in parallel with a duplicate remote does not fail (fetch)' '
+ mark fetch-group-duplicate &&
+ update_repo one &&
+ git config --add remotes.duplicate one &&
+ git config --add remotes.duplicate one &&
+ git -c fetch.parallel=2 remote update duplicate &&
+ repo_fetched one
+'
+
+test_done
diff --git a/t/t5507-remote-environment.sh b/t/t5507-remote-environment.sh
new file mode 100755
index 0000000..c6a6957
--- /dev/null
+++ b/t/t5507-remote-environment.sh
@@ -0,0 +1,36 @@
+#!/bin/sh
+
+test_description='check environment showed to remote side of transports'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'set up "remote" push situation' '
+ test_commit one &&
+ git config push.default current &&
+ git init remote
+'
+
+test_expect_success 'set up fake ssh' '
+ GIT_SSH_COMMAND="f() {
+ cd \"\$TRASH_DIRECTORY\" &&
+ eval \"\$2\"
+ }; f" &&
+ export GIT_SSH_COMMAND &&
+ export TRASH_DIRECTORY
+'
+
+# due to receive.denyCurrentBranch=true
+test_expect_success 'confirm default push fails' '
+ test_must_fail git push remote
+'
+
+test_expect_success 'config does not travel over same-machine push' '
+ test_must_fail git -c receive.denyCurrentBranch=false push remote
+'
+
+test_expect_success 'config does not travel over ssh push' '
+ test_must_fail git -c receive.denyCurrentBranch=false push host:remote
+'
+
+test_done
diff --git a/t/t5509-fetch-push-namespaces.sh b/t/t5509-fetch-push-namespaces.sh
new file mode 100755
index 0000000..31553b4
--- /dev/null
+++ b/t/t5509-fetch-push-namespaces.sh
@@ -0,0 +1,169 @@
+#!/bin/sh
+
+test_description='fetch/push involving ref namespaces'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success setup '
+ git config --global protocol.ext.allow user &&
+ test_tick &&
+ git init original &&
+ (
+ cd original &&
+ echo 0 >count &&
+ git add count &&
+ test_commit 0 &&
+ echo 1 >count &&
+ git add count &&
+ test_commit 1 &&
+ git remote add pushee-namespaced "ext::git --namespace=namespace %s ../pushee" &&
+ git remote add pushee-unnamespaced ../pushee
+ ) &&
+ commit0=$(cd original && git rev-parse HEAD^) &&
+ commit1=$(cd original && git rev-parse HEAD) &&
+ git init --bare pushee &&
+ git init puller
+'
+
+test_expect_success 'pushing into a repository using a ref namespace' '
+ (
+ cd original &&
+ git push pushee-namespaced main &&
+ git ls-remote pushee-namespaced >actual &&
+ printf "$commit1\trefs/heads/main\n" >expected &&
+ test_cmp expected actual &&
+ git push pushee-namespaced --tags &&
+ git ls-remote pushee-namespaced >actual &&
+ printf "$commit0\trefs/tags/0\n" >>expected &&
+ printf "$commit1\trefs/tags/1\n" >>expected &&
+ test_cmp expected actual &&
+ # Verify that the GIT_NAMESPACE environment variable works as well
+ GIT_NAMESPACE=namespace git ls-remote "ext::git %s ../pushee" >actual &&
+ test_cmp expected actual &&
+ # Verify that --namespace overrides GIT_NAMESPACE
+ GIT_NAMESPACE=garbage git ls-remote pushee-namespaced >actual &&
+ test_cmp expected actual &&
+ # Try a namespace with no content
+ git ls-remote "ext::git --namespace=garbage %s ../pushee" >actual &&
+ test_must_be_empty actual &&
+ git ls-remote pushee-unnamespaced >actual &&
+ sed -e "s|refs/|refs/namespaces/namespace/refs/|" expected >expected.unnamespaced &&
+ test_cmp expected.unnamespaced actual
+ )
+'
+
+test_expect_success 'pulling from a repository using a ref namespace' '
+ (
+ cd puller &&
+ git remote add -f pushee-namespaced "ext::git --namespace=namespace %s ../pushee" &&
+ git for-each-ref refs/ >actual &&
+ printf "$commit1 commit\trefs/remotes/pushee-namespaced/main\n" >expected &&
+ printf "$commit0 commit\trefs/tags/0\n" >>expected &&
+ printf "$commit1 commit\trefs/tags/1\n" >>expected &&
+ test_cmp expected actual
+ )
+'
+
+# This test with clone --mirror checks for possible regressions in clone
+# or the machinery underneath it. It ensures that no future change
+# causes clone to ignore refs in refs/namespaces/*. In particular, it
+# protects against a regression caused by any future change to the refs
+# machinery that might cause it to ignore refs outside of refs/heads/*
+# or refs/tags/*. More generally, this test also checks the high-level
+# functionality of using clone --mirror to back up a set of repos hosted
+# in the namespaces of a single repo.
+test_expect_success 'mirroring a repository using a ref namespace' '
+ git clone --mirror pushee mirror &&
+ (
+ cd mirror &&
+ git for-each-ref refs/ >actual &&
+ printf "$commit1 commit\trefs/namespaces/namespace/refs/heads/main\n" >expected &&
+ printf "$commit0 commit\trefs/namespaces/namespace/refs/tags/0\n" >>expected &&
+ printf "$commit1 commit\trefs/namespaces/namespace/refs/tags/1\n" >>expected &&
+ test_cmp expected actual
+ )
+'
+
+test_expect_success 'hide namespaced refs with transfer.hideRefs' '
+ GIT_NAMESPACE=namespace \
+ git -C pushee -c transfer.hideRefs=refs/tags \
+ ls-remote "ext::git %s ." >actual &&
+ printf "$commit1\trefs/heads/main\n" >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'check that transfer.hideRefs does not match unstripped refs' '
+ GIT_NAMESPACE=namespace \
+ git -C pushee -c transfer.hideRefs=refs/namespaces/namespace/refs/tags \
+ ls-remote "ext::git %s ." >actual &&
+ printf "$commit1\trefs/heads/main\n" >expected &&
+ printf "$commit0\trefs/tags/0\n" >>expected &&
+ printf "$commit1\trefs/tags/1\n" >>expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'hide full refs with transfer.hideRefs' '
+ GIT_NAMESPACE=namespace \
+ git -C pushee -c transfer.hideRefs="^refs/namespaces/namespace/refs/tags" \
+ ls-remote "ext::git %s ." >actual &&
+ printf "$commit1\trefs/heads/main\n" >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'try to update a hidden ref' '
+ test_config -C pushee transfer.hideRefs refs/heads/main &&
+ test_must_fail git -C original push pushee-namespaced main
+'
+
+test_expect_success 'try to update a ref that is not hidden' '
+ test_config -C pushee transfer.hideRefs refs/namespaces/namespace/refs/heads/main &&
+ git -C original push pushee-namespaced main
+'
+
+test_expect_success 'try to update a hidden full ref' '
+ test_config -C pushee transfer.hideRefs "^refs/namespaces/namespace/refs/heads/main" &&
+ test_must_fail git -C original push pushee-namespaced main
+'
+
+test_expect_success 'set up ambiguous HEAD' '
+ git init ambiguous &&
+ (
+ cd ambiguous &&
+ git commit --allow-empty -m foo &&
+ git update-ref refs/namespaces/ns/refs/heads/one HEAD &&
+ git update-ref refs/namespaces/ns/refs/heads/two HEAD &&
+ git symbolic-ref refs/namespaces/ns/HEAD \
+ refs/namespaces/ns/refs/heads/two
+ )
+'
+
+test_expect_success 'clone chooses correct HEAD (v0)' '
+ GIT_NAMESPACE=ns git -c protocol.version=0 \
+ clone ambiguous ambiguous-v0 &&
+ echo refs/heads/two >expect &&
+ git -C ambiguous-v0 symbolic-ref HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'clone chooses correct HEAD (v2)' '
+ GIT_NAMESPACE=ns git -c protocol.version=2 \
+ clone ambiguous ambiguous-v2 &&
+ echo refs/heads/two >expect &&
+ git -C ambiguous-v2 symbolic-ref HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'denyCurrentBranch and unborn branch with ref namespace' '
+ (
+ cd original &&
+ git init unborn &&
+ git remote add unborn-namespaced "ext::git --namespace=namespace %s unborn" &&
+ test_must_fail git push unborn-namespaced HEAD:main &&
+ git -C unborn config receive.denyCurrentBranch updateInstead &&
+ git push unborn-namespaced HEAD:main
+ )
+'
+
+test_done
diff --git a/t/t5510-fetch.sh b/t/t5510-fetch.sh
new file mode 100755
index 0000000..dcadd56
--- /dev/null
+++ b/t/t5510-fetch.sh
@@ -0,0 +1,1268 @@
+#!/bin/sh
+# Copyright (c) 2006, Junio C Hamano.
+
+test_description='Per branch config variables affects "git fetch".
+
+'
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-bundle.sh
+
+D=$(pwd)
+
+test_expect_success setup '
+ echo >file original &&
+ git add file &&
+ git commit -a -m original &&
+ git branch -M main
+'
+
+test_expect_success "clone and setup child repos" '
+ git clone . one &&
+ (
+ cd one &&
+ echo >file updated by one &&
+ git commit -a -m "updated by one"
+ ) &&
+ git clone . two &&
+ (
+ cd two &&
+ git config branch.main.remote one &&
+ git config remote.one.url ../one/.git/ &&
+ git config remote.one.fetch refs/heads/main:refs/heads/one
+ ) &&
+ git clone . three &&
+ (
+ cd three &&
+ git config branch.main.remote two &&
+ git config branch.main.merge refs/heads/one &&
+ mkdir -p .git/remotes &&
+ cat >.git/remotes/two <<-\EOF
+ URL: ../two/.git/
+ Pull: refs/heads/main:refs/heads/two
+ Pull: refs/heads/one:refs/heads/one
+ EOF
+ ) &&
+ git clone . bundle &&
+ git clone . seven
+'
+
+test_expect_success "fetch test" '
+ cd "$D" &&
+ echo >file updated by origin &&
+ git commit -a -m "updated by origin" &&
+ cd two &&
+ git fetch &&
+ git rev-parse --verify refs/heads/one &&
+ mine=$(git rev-parse refs/heads/one) &&
+ his=$(cd ../one && git rev-parse refs/heads/main) &&
+ test "z$mine" = "z$his"
+'
+
+test_expect_success "fetch test for-merge" '
+ cd "$D" &&
+ cd three &&
+ git fetch &&
+ git rev-parse --verify refs/heads/two &&
+ git rev-parse --verify refs/heads/one &&
+ main_in_two=$(cd ../two && git rev-parse main) &&
+ one_in_two=$(cd ../two && git rev-parse one) &&
+ {
+ echo "$one_in_two " &&
+ echo "$main_in_two not-for-merge"
+ } >expected &&
+ cut -f -2 .git/FETCH_HEAD >actual &&
+ test_cmp expected actual'
+
+test_expect_success 'fetch --prune on its own works as expected' '
+ cd "$D" &&
+ git clone . prune &&
+ cd prune &&
+ git update-ref refs/remotes/origin/extrabranch main &&
+
+ git fetch --prune origin &&
+ test_must_fail git rev-parse origin/extrabranch
+'
+
+test_expect_success 'fetch --prune with a branch name keeps branches' '
+ cd "$D" &&
+ git clone . prune-branch &&
+ cd prune-branch &&
+ git update-ref refs/remotes/origin/extrabranch main &&
+
+ git fetch --prune origin main &&
+ git rev-parse origin/extrabranch
+'
+
+test_expect_success 'fetch --prune with a namespace keeps other namespaces' '
+ cd "$D" &&
+ git clone . prune-namespace &&
+ cd prune-namespace &&
+
+ git fetch --prune origin refs/heads/a/*:refs/remotes/origin/a/* &&
+ git rev-parse origin/main
+'
+
+test_expect_success 'fetch --prune handles overlapping refspecs' '
+ cd "$D" &&
+ git update-ref refs/pull/42/head main &&
+ git clone . prune-overlapping &&
+ cd prune-overlapping &&
+ git config --add remote.origin.fetch refs/pull/*/head:refs/remotes/origin/pr/* &&
+
+ git fetch --prune origin &&
+ git rev-parse origin/main &&
+ git rev-parse origin/pr/42 &&
+
+ git config --unset-all remote.origin.fetch &&
+ git config remote.origin.fetch refs/pull/*/head:refs/remotes/origin/pr/* &&
+ git config --add remote.origin.fetch refs/heads/*:refs/remotes/origin/* &&
+
+ git fetch --prune origin &&
+ git rev-parse origin/main &&
+ git rev-parse origin/pr/42
+'
+
+test_expect_success 'fetch --prune --tags prunes branches but not tags' '
+ cd "$D" &&
+ git clone . prune-tags &&
+ cd prune-tags &&
+ git tag sometag main &&
+ # Create what looks like a remote-tracking branch from an earlier
+ # fetch that has since been deleted from the remote:
+ git update-ref refs/remotes/origin/fake-remote main &&
+
+ git fetch --prune --tags origin &&
+ git rev-parse origin/main &&
+ test_must_fail git rev-parse origin/fake-remote &&
+ git rev-parse sometag
+'
+
+test_expect_success 'fetch --prune --tags with branch does not prune other things' '
+ cd "$D" &&
+ git clone . prune-tags-branch &&
+ cd prune-tags-branch &&
+ git tag sometag main &&
+ git update-ref refs/remotes/origin/extrabranch main &&
+
+ git fetch --prune --tags origin main &&
+ git rev-parse origin/extrabranch &&
+ git rev-parse sometag
+'
+
+test_expect_success 'fetch --prune --tags with refspec prunes based on refspec' '
+ cd "$D" &&
+ git clone . prune-tags-refspec &&
+ cd prune-tags-refspec &&
+ git tag sometag main &&
+ git update-ref refs/remotes/origin/foo/otherbranch main &&
+ git update-ref refs/remotes/origin/extrabranch main &&
+
+ git fetch --prune --tags origin refs/heads/foo/*:refs/remotes/origin/foo/* &&
+ test_must_fail git rev-parse refs/remotes/origin/foo/otherbranch &&
+ git rev-parse origin/extrabranch &&
+ git rev-parse sometag
+'
+
+test_expect_success REFFILES 'fetch --prune fails to delete branches' '
+ cd "$D" &&
+ git clone . prune-fail &&
+ cd prune-fail &&
+ git update-ref refs/remotes/origin/extrabranch main &&
+ : this will prevent --prune from locking packed-refs for deleting refs, but adding loose refs still succeeds &&
+ >.git/packed-refs.new &&
+
+ test_must_fail git fetch --prune origin
+'
+
+test_expect_success 'fetch --atomic works with a single branch' '
+ test_when_finished "rm -rf \"$D\"/atomic" &&
+
+ cd "$D" &&
+ git clone . atomic &&
+ git branch atomic-branch &&
+ oid=$(git rev-parse atomic-branch) &&
+ echo "$oid" >expected &&
+
+ git -C atomic fetch --atomic origin &&
+ git -C atomic rev-parse origin/atomic-branch >actual &&
+ test_cmp expected actual &&
+ test $oid = "$(git -C atomic rev-parse --verify FETCH_HEAD)"
+'
+
+test_expect_success 'fetch --atomic works with multiple branches' '
+ test_when_finished "rm -rf \"$D\"/atomic" &&
+
+ cd "$D" &&
+ git clone . atomic &&
+ git branch atomic-branch-1 &&
+ git branch atomic-branch-2 &&
+ git branch atomic-branch-3 &&
+ git rev-parse refs/heads/atomic-branch-1 refs/heads/atomic-branch-2 refs/heads/atomic-branch-3 >actual &&
+
+ git -C atomic fetch --atomic origin &&
+ git -C atomic rev-parse refs/remotes/origin/atomic-branch-1 refs/remotes/origin/atomic-branch-2 refs/remotes/origin/atomic-branch-3 >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'fetch --atomic works with mixed branches and tags' '
+ test_when_finished "rm -rf \"$D\"/atomic" &&
+
+ cd "$D" &&
+ git clone . atomic &&
+ git branch atomic-mixed-branch &&
+ git tag atomic-mixed-tag &&
+ git rev-parse refs/heads/atomic-mixed-branch refs/tags/atomic-mixed-tag >actual &&
+
+ git -C atomic fetch --tags --atomic origin &&
+ git -C atomic rev-parse refs/remotes/origin/atomic-mixed-branch refs/tags/atomic-mixed-tag >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'fetch --atomic prunes references' '
+ test_when_finished "rm -rf \"$D\"/atomic" &&
+
+ cd "$D" &&
+ git branch atomic-prune-delete &&
+ git clone . atomic &&
+ git branch --delete atomic-prune-delete &&
+ git branch atomic-prune-create &&
+ git rev-parse refs/heads/atomic-prune-create >actual &&
+
+ git -C atomic fetch --prune --atomic origin &&
+ test_must_fail git -C atomic rev-parse refs/remotes/origin/atomic-prune-delete &&
+ git -C atomic rev-parse refs/remotes/origin/atomic-prune-create >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'fetch --atomic aborts with non-fast-forward update' '
+ test_when_finished "rm -rf \"$D\"/atomic" &&
+
+ cd "$D" &&
+ git branch atomic-non-ff &&
+ git clone . atomic &&
+ git rev-parse HEAD >actual &&
+
+ git branch atomic-new-branch &&
+ parent_commit=$(git rev-parse atomic-non-ff~) &&
+ git update-ref refs/heads/atomic-non-ff $parent_commit &&
+
+ test_must_fail git -C atomic fetch --atomic origin refs/heads/*:refs/remotes/origin/* &&
+ test_must_fail git -C atomic rev-parse refs/remotes/origin/atomic-new-branch &&
+ git -C atomic rev-parse refs/remotes/origin/atomic-non-ff >expected &&
+ test_cmp expected actual &&
+ test_must_be_empty atomic/.git/FETCH_HEAD
+'
+
+test_expect_success 'fetch --atomic executes a single reference transaction only' '
+ test_when_finished "rm -rf \"$D\"/atomic" &&
+
+ cd "$D" &&
+ git clone . atomic &&
+ git branch atomic-hooks-1 &&
+ git branch atomic-hooks-2 &&
+ head_oid=$(git rev-parse HEAD) &&
+
+ cat >expected <<-EOF &&
+ prepared
+ $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-1
+ $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-2
+ committed
+ $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-1
+ $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-2
+ EOF
+
+ rm -f atomic/actual &&
+ test_hook -C atomic reference-transaction <<-\EOF &&
+ ( echo "$*" && cat ) >>actual
+ EOF
+
+ git -C atomic fetch --atomic origin &&
+ test_cmp expected atomic/actual
+'
+
+test_expect_success 'fetch --atomic aborts all reference updates if hook aborts' '
+ test_when_finished "rm -rf \"$D\"/atomic" &&
+
+ cd "$D" &&
+ git clone . atomic &&
+ git branch atomic-hooks-abort-1 &&
+ git branch atomic-hooks-abort-2 &&
+ git branch atomic-hooks-abort-3 &&
+ git tag atomic-hooks-abort &&
+ head_oid=$(git rev-parse HEAD) &&
+
+ cat >expected <<-EOF &&
+ prepared
+ $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-1
+ $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-2
+ $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-3
+ $ZERO_OID $head_oid refs/tags/atomic-hooks-abort
+ aborted
+ $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-1
+ $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-2
+ $ZERO_OID $head_oid refs/remotes/origin/atomic-hooks-abort-3
+ $ZERO_OID $head_oid refs/tags/atomic-hooks-abort
+ EOF
+
+ rm -f atomic/actual &&
+ test_hook -C atomic/.git reference-transaction <<-\EOF &&
+ ( echo "$*" && cat ) >>actual
+ exit 1
+ EOF
+
+ git -C atomic for-each-ref >expected-refs &&
+ test_must_fail git -C atomic fetch --tags --atomic origin &&
+ git -C atomic for-each-ref >actual-refs &&
+ test_cmp expected-refs actual-refs &&
+ test_must_be_empty atomic/.git/FETCH_HEAD
+'
+
+test_expect_success 'fetch --atomic --append appends to FETCH_HEAD' '
+ test_when_finished "rm -rf \"$D\"/atomic" &&
+
+ cd "$D" &&
+ git clone . atomic &&
+ oid=$(git rev-parse HEAD) &&
+
+ git branch atomic-fetch-head-1 &&
+ git -C atomic fetch --atomic origin atomic-fetch-head-1 &&
+ test_line_count = 1 atomic/.git/FETCH_HEAD &&
+
+ git branch atomic-fetch-head-2 &&
+ git -C atomic fetch --atomic --append origin atomic-fetch-head-2 &&
+ test_line_count = 2 atomic/.git/FETCH_HEAD &&
+ cp atomic/.git/FETCH_HEAD expected &&
+
+ test_hook -C atomic reference-transaction <<-\EOF &&
+ exit 1
+ EOF
+
+ git branch atomic-fetch-head-3 &&
+ test_must_fail git -C atomic fetch --atomic --append origin atomic-fetch-head-3 &&
+ test_cmp expected atomic/.git/FETCH_HEAD
+'
+
+test_expect_success '--refmap="" ignores configured refspec' '
+ cd "$TRASH_DIRECTORY" &&
+ git clone "$D" remote-refs &&
+ git -C remote-refs rev-parse remotes/origin/main >old &&
+ git -C remote-refs update-ref refs/remotes/origin/main main~1 &&
+ git -C remote-refs rev-parse remotes/origin/main >new &&
+ git -C remote-refs fetch --refmap= origin "+refs/heads/*:refs/hidden/origin/*" &&
+ git -C remote-refs rev-parse remotes/origin/main >actual &&
+ test_cmp new actual &&
+ git -C remote-refs fetch origin &&
+ git -C remote-refs rev-parse remotes/origin/main >actual &&
+ test_cmp old actual
+'
+
+test_expect_success '--refmap="" and --prune' '
+ git -C remote-refs update-ref refs/remotes/origin/foo/otherbranch main &&
+ git -C remote-refs update-ref refs/hidden/foo/otherbranch main &&
+ git -C remote-refs fetch --prune --refmap="" origin +refs/heads/*:refs/hidden/* &&
+ git -C remote-refs rev-parse remotes/origin/foo/otherbranch &&
+ test_must_fail git -C remote-refs rev-parse refs/hidden/foo/otherbranch &&
+ git -C remote-refs fetch --prune origin &&
+ test_must_fail git -C remote-refs rev-parse remotes/origin/foo/otherbranch
+'
+
+test_expect_success 'fetch tags when there is no tags' '
+
+ cd "$D" &&
+
+ mkdir notags &&
+ cd notags &&
+ git init &&
+
+ git fetch -t ..
+
+'
+
+test_expect_success 'fetch following tags' '
+
+ cd "$D" &&
+ git tag -a -m "annotated" anno HEAD &&
+ git tag light HEAD &&
+
+ mkdir four &&
+ cd four &&
+ git init &&
+
+ git fetch .. :track &&
+ git show-ref --verify refs/tags/anno &&
+ git show-ref --verify refs/tags/light
+
+'
+
+test_expect_success 'fetch uses remote ref names to describe new refs' '
+ cd "$D" &&
+ git init descriptive &&
+ (
+ cd descriptive &&
+ git config remote.o.url .. &&
+ git config remote.o.fetch "refs/heads/*:refs/crazyheads/*" &&
+ git config --add remote.o.fetch "refs/others/*:refs/heads/*" &&
+ git fetch o
+ ) &&
+ git tag -a -m "Descriptive tag" descriptive-tag &&
+ git branch descriptive-branch &&
+ git checkout descriptive-branch &&
+ echo "Nuts" >crazy &&
+ git add crazy &&
+ git commit -a -m "descriptive commit" &&
+ git update-ref refs/others/crazy HEAD &&
+ (
+ cd descriptive &&
+ git fetch o 2>actual &&
+ test_grep "new branch.* -> refs/crazyheads/descriptive-branch$" actual &&
+ test_grep "new tag.* -> descriptive-tag$" actual &&
+ test_grep "new ref.* -> crazy$" actual
+ ) &&
+ git checkout main
+'
+
+test_expect_success 'fetch must not resolve short tag name' '
+
+ cd "$D" &&
+
+ mkdir five &&
+ cd five &&
+ git init &&
+
+ test_must_fail git fetch .. anno:five
+
+'
+
+test_expect_success 'fetch can now resolve short remote name' '
+
+ cd "$D" &&
+ git update-ref refs/remotes/six/HEAD HEAD &&
+
+ mkdir six &&
+ cd six &&
+ git init &&
+
+ git fetch .. six:six
+'
+
+test_expect_success 'create bundle 1' '
+ cd "$D" &&
+ echo >file updated again by origin &&
+ git commit -a -m "tip" &&
+ git bundle create --version=3 bundle1 main^..main
+'
+
+test_expect_success 'header of bundle looks right' '
+ cat >expect <<-EOF &&
+ # v3 git bundle
+ @object-format=$(test_oid algo)
+ -OID updated by origin
+ OID refs/heads/main
+
+ EOF
+ sed -e "s/$OID_REGEX/OID/g" -e "5q" "$D"/bundle1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'create bundle 2' '
+ cd "$D" &&
+ git bundle create bundle2 main~2..main
+'
+
+test_expect_success 'unbundle 1' '
+ cd "$D/bundle" &&
+ git checkout -b some-branch &&
+ test_must_fail git fetch "$D/bundle1" main:main
+'
+
+
+test_expect_success 'bundle 1 has only 3 files ' '
+ cd "$D" &&
+ test_bundle_object_count bundle1 3
+'
+
+test_expect_success 'unbundle 2' '
+ cd "$D/bundle" &&
+ git fetch ../bundle2 main:main &&
+ test "tip" = "$(git log -1 --pretty=oneline main | cut -d" " -f2)"
+'
+
+test_expect_success 'bundle does not prerequisite objects' '
+ cd "$D" &&
+ touch file2 &&
+ git add file2 &&
+ git commit -m add.file2 file2 &&
+ git bundle create bundle3 -1 HEAD &&
+ test_bundle_object_count bundle3 3
+'
+
+test_expect_success 'bundle should be able to create a full history' '
+
+ cd "$D" &&
+ git tag -a -m "1.0" v1.0 main &&
+ git bundle create bundle4 v1.0
+
+'
+
+test_expect_success 'fetch with a non-applying branch.<name>.merge' '
+ git config branch.main.remote yeti &&
+ git config branch.main.merge refs/heads/bigfoot &&
+ git config remote.blub.url one &&
+ git config remote.blub.fetch "refs/heads/*:refs/remotes/one/*" &&
+ git fetch blub
+'
+
+# URL supplied to fetch does not match the url of the configured branch's remote
+test_expect_success 'fetch from GIT URL with a non-applying branch.<name>.merge [1]' '
+ one_head=$(cd one && git rev-parse HEAD) &&
+ this_head=$(git rev-parse HEAD) &&
+ git update-ref -d FETCH_HEAD &&
+ git fetch one &&
+ test $one_head = "$(git rev-parse --verify FETCH_HEAD)" &&
+ test $this_head = "$(git rev-parse --verify HEAD)"
+'
+
+# URL supplied to fetch matches the url of the configured branch's remote and
+# the merge spec matches the branch the remote HEAD points to
+test_expect_success 'fetch from GIT URL with a non-applying branch.<name>.merge [2]' '
+ one_ref=$(cd one && git symbolic-ref HEAD) &&
+ git config branch.main.remote blub &&
+ git config branch.main.merge "$one_ref" &&
+ git update-ref -d FETCH_HEAD &&
+ git fetch one &&
+ test $one_head = "$(git rev-parse --verify FETCH_HEAD)" &&
+ test $this_head = "$(git rev-parse --verify HEAD)"
+'
+
+# URL supplied to fetch matches the url of the configured branch's remote, but
+# the merge spec does not match the branch the remote HEAD points to
+test_expect_success 'fetch from GIT URL with a non-applying branch.<name>.merge [3]' '
+ git config branch.main.merge "${one_ref}_not" &&
+ git update-ref -d FETCH_HEAD &&
+ git fetch one &&
+ test $one_head = "$(git rev-parse --verify FETCH_HEAD)" &&
+ test $this_head = "$(git rev-parse --verify HEAD)"
+'
+
+# the strange name is: a\!'b
+test_expect_success 'quoting of a strangely named repo' '
+ test_must_fail git fetch "a\\!'\''b" > result 2>&1 &&
+ grep "fatal: '\''a\\\\!'\''b'\''" result
+'
+
+test_expect_success 'bundle should record HEAD correctly' '
+
+ cd "$D" &&
+ git bundle create bundle5 HEAD main &&
+ git bundle list-heads bundle5 >actual &&
+ for h in HEAD refs/heads/main
+ do
+ echo "$(git rev-parse --verify $h) $h" || return 1
+ done >expect &&
+ test_cmp expect actual
+
+'
+
+test_expect_success 'mark initial state of origin/main' '
+ (
+ cd three &&
+ git tag base-origin-main refs/remotes/origin/main
+ )
+'
+
+test_expect_success 'explicit fetch should update tracking' '
+
+ cd "$D" &&
+ git branch -f side &&
+ (
+ cd three &&
+ git update-ref refs/remotes/origin/main base-origin-main &&
+ o=$(git rev-parse --verify refs/remotes/origin/main) &&
+ git fetch origin main &&
+ n=$(git rev-parse --verify refs/remotes/origin/main) &&
+ test "$o" != "$n" &&
+ test_must_fail git rev-parse --verify refs/remotes/origin/side
+ )
+'
+
+test_expect_success 'explicit pull should update tracking' '
+
+ cd "$D" &&
+ git branch -f side &&
+ (
+ cd three &&
+ git update-ref refs/remotes/origin/main base-origin-main &&
+ o=$(git rev-parse --verify refs/remotes/origin/main) &&
+ git pull origin main &&
+ n=$(git rev-parse --verify refs/remotes/origin/main) &&
+ test "$o" != "$n" &&
+ test_must_fail git rev-parse --verify refs/remotes/origin/side
+ )
+'
+
+test_expect_success 'explicit --refmap is allowed only with command-line refspec' '
+ cd "$D" &&
+ (
+ cd three &&
+ test_must_fail git fetch --refmap="*:refs/remotes/none/*"
+ )
+'
+
+test_expect_success 'explicit --refmap option overrides remote.*.fetch' '
+ cd "$D" &&
+ git branch -f side &&
+ (
+ cd three &&
+ git update-ref refs/remotes/origin/main base-origin-main &&
+ o=$(git rev-parse --verify refs/remotes/origin/main) &&
+ git fetch --refmap="refs/heads/*:refs/remotes/other/*" origin main &&
+ n=$(git rev-parse --verify refs/remotes/origin/main) &&
+ test "$o" = "$n" &&
+ test_must_fail git rev-parse --verify refs/remotes/origin/side &&
+ git rev-parse --verify refs/remotes/other/main
+ )
+'
+
+test_expect_success 'explicitly empty --refmap option disables remote.*.fetch' '
+ cd "$D" &&
+ git branch -f side &&
+ (
+ cd three &&
+ git update-ref refs/remotes/origin/main base-origin-main &&
+ o=$(git rev-parse --verify refs/remotes/origin/main) &&
+ git fetch --refmap="" origin main &&
+ n=$(git rev-parse --verify refs/remotes/origin/main) &&
+ test "$o" = "$n" &&
+ test_must_fail git rev-parse --verify refs/remotes/origin/side
+ )
+'
+
+test_expect_success 'configured fetch updates tracking' '
+
+ cd "$D" &&
+ git branch -f side &&
+ (
+ cd three &&
+ git update-ref refs/remotes/origin/main base-origin-main &&
+ o=$(git rev-parse --verify refs/remotes/origin/main) &&
+ git fetch origin &&
+ n=$(git rev-parse --verify refs/remotes/origin/main) &&
+ test "$o" != "$n" &&
+ git rev-parse --verify refs/remotes/origin/side
+ )
+'
+
+test_expect_success 'non-matching refspecs do not confuse tracking update' '
+ cd "$D" &&
+ git update-ref refs/odd/location HEAD &&
+ (
+ cd three &&
+ git update-ref refs/remotes/origin/main base-origin-main &&
+ git config --add remote.origin.fetch \
+ refs/odd/location:refs/remotes/origin/odd &&
+ o=$(git rev-parse --verify refs/remotes/origin/main) &&
+ git fetch origin main &&
+ n=$(git rev-parse --verify refs/remotes/origin/main) &&
+ test "$o" != "$n" &&
+ test_must_fail git rev-parse --verify refs/remotes/origin/odd
+ )
+'
+
+test_expect_success 'pushing nonexistent branch by mistake should not segv' '
+
+ cd "$D" &&
+ test_must_fail git push seven no:no
+
+'
+
+test_expect_success 'auto tag following fetches minimum' '
+
+ cd "$D" &&
+ git clone .git follow &&
+ git checkout HEAD^0 &&
+ (
+ for i in 1 2 3 4 5 6 7
+ do
+ echo $i >>file &&
+ git commit -m $i -a &&
+ git tag -a -m $i excess-$i || exit 1
+ done
+ ) &&
+ git checkout main &&
+ (
+ cd follow &&
+ git fetch
+ )
+'
+
+test_expect_success 'refuse to fetch into the current branch' '
+
+ test_must_fail git fetch . side:main
+
+'
+
+test_expect_success 'fetch into the current branch with --update-head-ok' '
+
+ git fetch --update-head-ok . side:main
+
+'
+
+test_expect_success 'fetch --dry-run does not touch FETCH_HEAD, but still prints what would be written' '
+ rm -f .git/FETCH_HEAD err &&
+ git fetch --dry-run . 2>err &&
+ ! test -f .git/FETCH_HEAD &&
+ grep FETCH_HEAD err
+'
+
+test_expect_success '--no-write-fetch-head does not touch FETCH_HEAD, and does not print what would be written' '
+ rm -f .git/FETCH_HEAD err &&
+ git fetch --no-write-fetch-head . 2>err &&
+ ! test -f .git/FETCH_HEAD &&
+ ! grep FETCH_HEAD err
+'
+
+test_expect_success '--write-fetch-head gets defeated by --dry-run' '
+ rm -f .git/FETCH_HEAD &&
+ git fetch --dry-run --write-fetch-head . &&
+ ! test -f .git/FETCH_HEAD
+'
+
+test_expect_success "should be able to fetch with duplicate refspecs" '
+ mkdir dups &&
+ (
+ cd dups &&
+ git init &&
+ git config branch.main.remote three &&
+ git config remote.three.url ../three/.git &&
+ git config remote.three.fetch +refs/heads/*:refs/remotes/origin/* &&
+ git config --add remote.three.fetch +refs/heads/*:refs/remotes/origin/* &&
+ git fetch three
+ )
+'
+
+test_expect_success 'LHS of refspec follows ref disambiguation rules' '
+ mkdir lhs-ambiguous &&
+ (
+ cd lhs-ambiguous &&
+ git init server &&
+ test_commit -C server unwanted &&
+ test_commit -C server wanted &&
+
+ git init client &&
+
+ # Check a name coming after "refs" alphabetically ...
+ git -C server update-ref refs/heads/s wanted &&
+ git -C server update-ref refs/heads/refs/heads/s unwanted &&
+ git -C client fetch ../server +refs/heads/s:refs/heads/checkthis &&
+ git -C server rev-parse wanted >expect &&
+ git -C client rev-parse checkthis >actual &&
+ test_cmp expect actual &&
+
+ # ... and one before.
+ git -C server update-ref refs/heads/q wanted &&
+ git -C server update-ref refs/heads/refs/heads/q unwanted &&
+ git -C client fetch ../server +refs/heads/q:refs/heads/checkthis &&
+ git -C server rev-parse wanted >expect &&
+ git -C client rev-parse checkthis >actual &&
+ test_cmp expect actual &&
+
+ # Tags are preferred over branches like refs/{heads,tags}/*
+ git -C server update-ref refs/tags/t wanted &&
+ git -C server update-ref refs/heads/t unwanted &&
+ git -C client fetch ../server +t:refs/heads/checkthis &&
+ git -C server rev-parse wanted >expect &&
+ git -C client rev-parse checkthis >actual
+ )
+'
+
+test_expect_success 'fetch.writeCommitGraph' '
+ git clone three write &&
+ (
+ cd three &&
+ test_commit new
+ ) &&
+ (
+ cd write &&
+ git -c fetch.writeCommitGraph fetch origin &&
+ test_path_is_file .git/objects/info/commit-graphs/commit-graph-chain
+ )
+'
+
+test_expect_success 'fetch.writeCommitGraph with submodules' '
+ test_config_global protocol.file.allow always &&
+ git clone dups super &&
+ (
+ cd super &&
+ git submodule add "file://$TRASH_DIRECTORY/three" &&
+ git commit -m "add submodule"
+ ) &&
+ git clone "super" super-clone &&
+ (
+ cd super-clone &&
+ rm -rf .git/objects/info &&
+ git -c fetch.writeCommitGraph=true fetch origin &&
+ test_path_is_file .git/objects/info/commit-graphs/commit-graph-chain
+ )
+'
+
+# fetches from first configured url
+test_expect_success 'fetch from multiple configured URLs in single remote' '
+ git init url1 &&
+ git remote add multipleurls url1 &&
+ git remote set-url --add multipleurls url2 &&
+ git fetch multipleurls
+'
+
+# configured prune tests
+
+set_config_tristate () {
+ # var=$1 val=$2
+ case "$2" in
+ unset)
+ test_unconfig "$1"
+ ;;
+ *)
+ git config "$1" "$2"
+ key=$(echo $1 | sed -e 's/^remote\.origin/fetch/')
+ git_fetch_c="$git_fetch_c -c $key=$2"
+ ;;
+ esac
+}
+
+test_configured_prune () {
+ test_configured_prune_type "$@" "name"
+ test_configured_prune_type "$@" "link"
+}
+
+test_configured_prune_type () {
+ fetch_prune=$1
+ remote_origin_prune=$2
+ fetch_prune_tags=$3
+ remote_origin_prune_tags=$4
+ expected_branch=$5
+ expected_tag=$6
+ cmdline=$7
+ mode=$8
+
+ if test -z "$cmdline_setup"
+ then
+ test_expect_success 'setup cmdline_setup variable for subsequent test' '
+ remote_url="file://$(git -C one config remote.origin.url)" &&
+ remote_fetch="$(git -C one config remote.origin.fetch)" &&
+ cmdline_setup="\"$remote_url\" \"$remote_fetch\""
+ '
+ fi
+
+ if test "$mode" = 'link'
+ then
+ new_cmdline=""
+
+ if test "$cmdline" = ""
+ then
+ new_cmdline=$cmdline_setup
+ else
+ new_cmdline=$(perl -e '
+ my ($cmdline, $url) = @ARGV;
+ $cmdline =~ s[origin(?!/)][quotemeta($url)]ge;
+ print $cmdline;
+ ' -- "$cmdline" "$remote_url")
+ fi
+
+ if test "$fetch_prune_tags" = 'true' ||
+ test "$remote_origin_prune_tags" = 'true'
+ then
+ if ! printf '%s' "$cmdline\n" | grep -q refs/remotes/origin/
+ then
+ new_cmdline="$new_cmdline refs/tags/*:refs/tags/*"
+ fi
+ fi
+
+ cmdline="$new_cmdline"
+ fi
+
+ test_expect_success "$mode prune fetch.prune=$1 remote.origin.prune=$2 fetch.pruneTags=$3 remote.origin.pruneTags=$4${7:+ $7}; branch:$5 tag:$6" '
+ # make sure a newbranch is there in . and also in one
+ git branch -f newbranch &&
+ git tag -f newtag &&
+ (
+ cd one &&
+ test_unconfig fetch.prune &&
+ test_unconfig fetch.pruneTags &&
+ test_unconfig remote.origin.prune &&
+ test_unconfig remote.origin.pruneTags &&
+ git fetch '"$cmdline_setup"' &&
+ git rev-parse --verify refs/remotes/origin/newbranch &&
+ git rev-parse --verify refs/tags/newtag
+ ) &&
+
+ # now remove them
+ git branch -d newbranch &&
+ git tag -d newtag &&
+
+ # then test
+ (
+ cd one &&
+ git_fetch_c="" &&
+ set_config_tristate fetch.prune $fetch_prune &&
+ set_config_tristate fetch.pruneTags $fetch_prune_tags &&
+ set_config_tristate remote.origin.prune $remote_origin_prune &&
+ set_config_tristate remote.origin.pruneTags $remote_origin_prune_tags &&
+
+ if test "$mode" != "link"
+ then
+ git_fetch_c=""
+ fi &&
+ git$git_fetch_c fetch '"$cmdline"' &&
+ case "$expected_branch" in
+ pruned)
+ test_must_fail git rev-parse --verify refs/remotes/origin/newbranch
+ ;;
+ kept)
+ git rev-parse --verify refs/remotes/origin/newbranch
+ ;;
+ esac &&
+ case "$expected_tag" in
+ pruned)
+ test_must_fail git rev-parse --verify refs/tags/newtag
+ ;;
+ kept)
+ git rev-parse --verify refs/tags/newtag
+ ;;
+ esac
+ )
+ '
+}
+
+# $1 config: fetch.prune
+# $2 config: remote.<name>.prune
+# $3 config: fetch.pruneTags
+# $4 config: remote.<name>.pruneTags
+# $5 expect: branch to be pruned?
+# $6 expect: tag to be pruned?
+# $7 git-fetch $cmdline:
+#
+# $1 $2 $3 $4 $5 $6 $7
+test_configured_prune unset unset unset unset kept kept ""
+test_configured_prune unset unset unset unset kept kept "--no-prune"
+test_configured_prune unset unset unset unset pruned kept "--prune"
+test_configured_prune unset unset unset unset kept pruned \
+ "--prune origin refs/tags/*:refs/tags/*"
+test_configured_prune unset unset unset unset pruned pruned \
+ "--prune origin refs/tags/*:refs/tags/* +refs/heads/*:refs/remotes/origin/*"
+
+test_configured_prune false unset unset unset kept kept ""
+test_configured_prune false unset unset unset kept kept "--no-prune"
+test_configured_prune false unset unset unset pruned kept "--prune"
+
+test_configured_prune true unset unset unset pruned kept ""
+test_configured_prune true unset unset unset pruned kept "--prune"
+test_configured_prune true unset unset unset kept kept "--no-prune"
+
+test_configured_prune unset false unset unset kept kept ""
+test_configured_prune unset false unset unset kept kept "--no-prune"
+test_configured_prune unset false unset unset pruned kept "--prune"
+
+test_configured_prune false false unset unset kept kept ""
+test_configured_prune false false unset unset kept kept "--no-prune"
+test_configured_prune false false unset unset pruned kept "--prune"
+test_configured_prune false false unset unset kept pruned \
+ "--prune origin refs/tags/*:refs/tags/*"
+test_configured_prune false false unset unset pruned pruned \
+ "--prune origin refs/tags/*:refs/tags/* +refs/heads/*:refs/remotes/origin/*"
+
+test_configured_prune true false unset unset kept kept ""
+test_configured_prune true false unset unset pruned kept "--prune"
+test_configured_prune true false unset unset kept kept "--no-prune"
+
+test_configured_prune unset true unset unset pruned kept ""
+test_configured_prune unset true unset unset kept kept "--no-prune"
+test_configured_prune unset true unset unset pruned kept "--prune"
+
+test_configured_prune false true unset unset pruned kept ""
+test_configured_prune false true unset unset kept kept "--no-prune"
+test_configured_prune false true unset unset pruned kept "--prune"
+
+test_configured_prune true true unset unset pruned kept ""
+test_configured_prune true true unset unset pruned kept "--prune"
+test_configured_prune true true unset unset kept kept "--no-prune"
+test_configured_prune true true unset unset kept pruned \
+ "--prune origin refs/tags/*:refs/tags/*"
+test_configured_prune true true unset unset pruned pruned \
+ "--prune origin refs/tags/*:refs/tags/* +refs/heads/*:refs/remotes/origin/*"
+
+# --prune-tags on its own does nothing, needs --prune as well, same
+# for fetch.pruneTags without fetch.prune
+test_configured_prune unset unset unset unset kept kept "--prune-tags"
+test_configured_prune unset unset true unset kept kept ""
+test_configured_prune unset unset unset true kept kept ""
+
+# These will prune the tags
+test_configured_prune unset unset unset unset pruned pruned "--prune --prune-tags"
+test_configured_prune true unset true unset pruned pruned ""
+test_configured_prune unset true unset true pruned pruned ""
+
+# remote.<name>.pruneTags overrides fetch.pruneTags, just like
+# remote.<name>.prune overrides fetch.prune if set.
+test_configured_prune true unset true unset pruned pruned ""
+test_configured_prune false true false true pruned pruned ""
+test_configured_prune true false true false kept kept ""
+
+# When --prune-tags is supplied it's ignored if an explicit refspec is
+# given, same for the configuration options.
+test_configured_prune unset unset unset unset pruned kept \
+ "--prune --prune-tags origin +refs/heads/*:refs/remotes/origin/*"
+test_configured_prune unset unset true unset pruned kept \
+ "--prune origin +refs/heads/*:refs/remotes/origin/*"
+test_configured_prune unset unset unset true pruned kept \
+ "--prune origin +refs/heads/*:refs/remotes/origin/*"
+
+# Pruning that also takes place if a file:// url replaces a named
+# remote. However, because there's no implicit
+# +refs/heads/*:refs/remotes/origin/* refspec and supplying it on the
+# command-line negates --prune-tags, the branches will not be pruned.
+test_configured_prune_type unset unset unset unset kept kept "origin --prune-tags" "name"
+test_configured_prune_type unset unset unset unset kept kept "origin --prune-tags" "link"
+test_configured_prune_type unset unset unset unset pruned pruned "origin --prune --prune-tags" "name"
+test_configured_prune_type unset unset unset unset kept pruned "origin --prune --prune-tags" "link"
+test_configured_prune_type unset unset unset unset pruned pruned "--prune --prune-tags origin" "name"
+test_configured_prune_type unset unset unset unset kept pruned "--prune --prune-tags origin" "link"
+test_configured_prune_type unset unset true unset pruned pruned "--prune origin" "name"
+test_configured_prune_type unset unset true unset kept pruned "--prune origin" "link"
+test_configured_prune_type unset unset unset true pruned pruned "--prune origin" "name"
+test_configured_prune_type unset unset unset true kept pruned "--prune origin" "link"
+test_configured_prune_type true unset true unset pruned pruned "origin" "name"
+test_configured_prune_type true unset true unset kept pruned "origin" "link"
+test_configured_prune_type unset true true unset pruned pruned "origin" "name"
+test_configured_prune_type unset true true unset kept pruned "origin" "link"
+test_configured_prune_type unset true unset true pruned pruned "origin" "name"
+test_configured_prune_type unset true unset true kept pruned "origin" "link"
+
+# When all remote.origin.fetch settings are deleted a --prune
+# --prune-tags still implicitly supplies refs/tags/*:refs/tags/* so
+# tags, but not tracking branches, will be deleted.
+test_expect_success 'remove remote.origin.fetch "one"' '
+ (
+ cd one &&
+ git config --unset-all remote.origin.fetch
+ )
+'
+test_configured_prune_type unset unset unset unset kept pruned "origin --prune --prune-tags" "name"
+test_configured_prune_type unset unset unset unset kept pruned "origin --prune --prune-tags" "link"
+
+test_expect_success 'all boundary commits are excluded' '
+ test_commit base &&
+ test_commit oneside &&
+ git checkout HEAD^ &&
+ test_commit otherside &&
+ git checkout main &&
+ test_tick &&
+ git merge otherside &&
+ ad=$(git log --no-walk --format=%ad HEAD) &&
+ git bundle create twoside-boundary.bdl main --since="$ad" &&
+ test_bundle_object_count --thin twoside-boundary.bdl 3
+'
+
+test_expect_success 'fetch --prune prints the remotes url' '
+ git branch goodbye &&
+ git clone . only-prunes &&
+ git branch -D goodbye &&
+ (
+ cd only-prunes &&
+ git fetch --prune origin 2>&1 | head -n1 >../actual
+ ) &&
+ echo "From ${D}/." >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'branchname D/F conflict resolved by --prune' '
+ git branch dir/file &&
+ git clone . prune-df-conflict &&
+ git branch -D dir/file &&
+ git branch dir &&
+ (
+ cd prune-df-conflict &&
+ git fetch --prune &&
+ git rev-parse origin/dir >../actual
+ ) &&
+ git rev-parse dir >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'fetching a one-level ref works' '
+ test_commit extra &&
+ git reset --hard HEAD^ &&
+ git update-ref refs/foo extra &&
+ git init one-level &&
+ (
+ cd one-level &&
+ git fetch .. HEAD refs/foo
+ )
+'
+
+test_expect_success 'fetching with auto-gc does not lock up' '
+ write_script askyesno <<-\EOF &&
+ echo "$*" &&
+ false
+ EOF
+ git clone "file://$D" auto-gc &&
+ test_commit test2 &&
+ (
+ cd auto-gc &&
+ git config fetch.unpackLimit 1 &&
+ git config gc.autoPackLimit 1 &&
+ git config gc.autoDetach false &&
+ GIT_ASK_YESNO="$D/askyesno" git fetch --verbose >fetch.out 2>&1 &&
+ test_grep "Auto packing the repository" fetch.out &&
+ ! grep "Should I try again" fetch.out
+ )
+'
+
+for section in fetch transfer
+do
+ test_expect_success "$section.hideRefs affects connectivity check" '
+ GIT_TRACE="$PWD"/trace git -c $section.hideRefs=refs -c \
+ $section.hideRefs="!refs/tags/" fetch &&
+ grep "git rev-list .*--exclude-hidden=fetch" trace
+ '
+done
+
+test_expect_success 'prepare source branch' '
+ echo one >onebranch &&
+ git checkout --orphan onebranch &&
+ git rm --cached -r . &&
+ git add onebranch &&
+ git commit -m onebranch &&
+ git rev-list --objects onebranch -- >actual &&
+ # 3 objects should be created, at least ...
+ test 3 -le $(wc -l <actual)
+'
+
+validate_store_type () {
+ git -C dest count-objects -v >actual &&
+ case "$store_type" in
+ packed)
+ grep "^count: 0$" actual ;;
+ loose)
+ grep "^packs: 0$" actual ;;
+ esac || {
+ echo "store_type is $store_type"
+ cat actual
+ false
+ }
+}
+
+test_unpack_limit () {
+ store_type=$1
+
+ case "$store_type" in
+ packed) fetch_limit=1 transfer_limit=10000 ;;
+ loose) fetch_limit=10000 transfer_limit=1 ;;
+ esac
+
+ test_expect_success "fetch trumps transfer limit" '
+ rm -fr dest &&
+ git --bare init dest &&
+ git -C dest config fetch.unpacklimit $fetch_limit &&
+ git -C dest config transfer.unpacklimit $transfer_limit &&
+ git -C dest fetch .. onebranch &&
+ validate_store_type
+ '
+}
+
+test_unpack_limit packed
+test_unpack_limit loose
+
+setup_negotiation_tip () {
+ SERVER="$1"
+ URL="$2"
+ USE_PROTOCOL_V2="$3"
+
+ rm -rf "$SERVER" client trace &&
+ git init -b main "$SERVER" &&
+ test_commit -C "$SERVER" alpha_1 &&
+ test_commit -C "$SERVER" alpha_2 &&
+ git -C "$SERVER" checkout --orphan beta &&
+ test_commit -C "$SERVER" beta_1 &&
+ test_commit -C "$SERVER" beta_2 &&
+
+ git clone "$URL" client &&
+
+ if test "$USE_PROTOCOL_V2" -eq 1
+ then
+ git -C "$SERVER" config protocol.version 2 &&
+ git -C client config protocol.version 2
+ fi &&
+
+ test_commit -C "$SERVER" beta_s &&
+ git -C "$SERVER" checkout main &&
+ test_commit -C "$SERVER" alpha_s &&
+ git -C "$SERVER" tag -d alpha_1 alpha_2 beta_1 beta_2
+}
+
+check_negotiation_tip () {
+ # Ensure that {alpha,beta}_1 are sent as "have", but not {alpha_beta}_2
+ ALPHA_1=$(git -C client rev-parse alpha_1) &&
+ grep "fetch> have $ALPHA_1" trace &&
+ BETA_1=$(git -C client rev-parse beta_1) &&
+ grep "fetch> have $BETA_1" trace &&
+ ALPHA_2=$(git -C client rev-parse alpha_2) &&
+ ! grep "fetch> have $ALPHA_2" trace &&
+ BETA_2=$(git -C client rev-parse beta_2) &&
+ ! grep "fetch> have $BETA_2" trace
+}
+
+test_expect_success '--negotiation-tip limits "have" lines sent' '
+ setup_negotiation_tip server server 0 &&
+ GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
+ --negotiation-tip=alpha_1 --negotiation-tip=beta_1 \
+ origin alpha_s beta_s &&
+ check_negotiation_tip
+'
+
+test_expect_success '--negotiation-tip understands globs' '
+ setup_negotiation_tip server server 0 &&
+ GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
+ --negotiation-tip=*_1 \
+ origin alpha_s beta_s &&
+ check_negotiation_tip
+'
+
+test_expect_success '--negotiation-tip understands abbreviated SHA-1' '
+ setup_negotiation_tip server server 0 &&
+ GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
+ --negotiation-tip=$(git -C client rev-parse --short alpha_1) \
+ --negotiation-tip=$(git -C client rev-parse --short beta_1) \
+ origin alpha_s beta_s &&
+ check_negotiation_tip
+'
+
+test_expect_success '--negotiation-tip rejects missing OIDs' '
+ setup_negotiation_tip server server 0 &&
+ test_must_fail git -C client fetch \
+ --negotiation-tip=alpha_1 \
+ --negotiation-tip=$(test_oid zero) \
+ origin alpha_s beta_s 2>err &&
+ cat >fatal-expect <<-EOF &&
+ fatal: the object $(test_oid zero) does not exist
+EOF
+ grep fatal: err >fatal-actual &&
+ test_cmp fatal-expect fatal-actual
+'
+
+. "$TEST_DIRECTORY"/lib-httpd.sh
+start_httpd
+
+test_expect_success '--negotiation-tip limits "have" lines sent with HTTP protocol v2' '
+ setup_negotiation_tip "$HTTPD_DOCUMENT_ROOT_PATH/server" \
+ "$HTTPD_URL/smart/server" 1 &&
+ GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
+ --negotiation-tip=alpha_1 --negotiation-tip=beta_1 \
+ origin alpha_s beta_s &&
+ check_negotiation_tip
+'
+
+# DO NOT add non-httpd-specific tests here, because the last part of this
+# test script is only executed when httpd is available and enabled.
+
+test_done
diff --git a/t/t5511-refspec.sh b/t/t5511-refspec.sh
new file mode 100755
index 0000000..fc55681
--- /dev/null
+++ b/t/t5511-refspec.sh
@@ -0,0 +1,97 @@
+#!/bin/sh
+
+test_description='refspec parsing'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_refspec () {
+ kind=$1 refspec=$2 expect=$3
+ git config remote.frotz.url "." &&
+ git config --remove-section remote.frotz &&
+ git config remote.frotz.url "." &&
+ git config "remote.frotz.$kind" "$refspec" &&
+ if test "$expect" != invalid
+ then
+ title="$kind $refspec"
+ test='git ls-remote frotz'
+ else
+ title="$kind $refspec (invalid)"
+ test='test_must_fail git ls-remote frotz'
+ fi
+ test_expect_success "$title" "$test"
+}
+
+test_refspec push '' invalid
+test_refspec push ':'
+test_refspec push '::' invalid
+test_refspec push '+:'
+
+test_refspec fetch ''
+test_refspec fetch ':'
+test_refspec fetch '::' invalid
+
+test_refspec push 'refs/heads/*:refs/remotes/frotz/*'
+test_refspec push 'refs/heads/*:refs/remotes/frotz' invalid
+test_refspec push 'refs/heads:refs/remotes/frotz/*' invalid
+test_refspec push 'refs/heads/main:refs/remotes/frotz/xyzzy'
+
+
+# These have invalid LHS, but we do not have a formal "valid sha-1
+# expression syntax checker" so they are not checked with the current
+# code. They will be caught downstream anyway, but we may want to
+# have tighter check later...
+
+: test_refspec push 'refs/heads/main::refs/remotes/frotz/xyzzy' invalid
+: test_refspec push 'refs/heads/maste :refs/remotes/frotz/xyzzy' invalid
+
+test_refspec fetch 'refs/heads/*:refs/remotes/frotz/*'
+test_refspec fetch 'refs/heads/*:refs/remotes/frotz' invalid
+test_refspec fetch 'refs/heads:refs/remotes/frotz/*' invalid
+test_refspec fetch 'refs/heads/main:refs/remotes/frotz/xyzzy'
+test_refspec fetch 'refs/heads/main::refs/remotes/frotz/xyzzy' invalid
+test_refspec fetch 'refs/heads/maste :refs/remotes/frotz/xyzzy' invalid
+
+test_refspec push 'main~1:refs/remotes/frotz/backup'
+test_refspec fetch 'main~1:refs/remotes/frotz/backup' invalid
+test_refspec push 'HEAD~4:refs/remotes/frotz/new'
+test_refspec fetch 'HEAD~4:refs/remotes/frotz/new' invalid
+
+test_refspec push 'HEAD'
+test_refspec fetch 'HEAD'
+test_refspec push '@'
+test_refspec fetch '@'
+test_refspec push 'refs/heads/ nitfol' invalid
+test_refspec fetch 'refs/heads/ nitfol' invalid
+
+test_refspec push 'HEAD:' invalid
+test_refspec fetch 'HEAD:'
+test_refspec push 'refs/heads/ nitfol:' invalid
+test_refspec fetch 'refs/heads/ nitfol:' invalid
+
+test_refspec push ':refs/remotes/frotz/deleteme'
+test_refspec fetch ':refs/remotes/frotz/HEAD-to-me'
+test_refspec push ':refs/remotes/frotz/delete me' invalid
+test_refspec fetch ':refs/remotes/frotz/HEAD to me' invalid
+
+test_refspec fetch 'refs/heads/*/for-linus:refs/remotes/mine/*-blah'
+test_refspec push 'refs/heads/*/for-linus:refs/remotes/mine/*-blah'
+
+test_refspec fetch 'refs/heads*/for-linus:refs/remotes/mine/*'
+test_refspec push 'refs/heads*/for-linus:refs/remotes/mine/*'
+
+test_refspec fetch 'refs/heads/*/*/for-linus:refs/remotes/mine/*' invalid
+test_refspec push 'refs/heads/*/*/for-linus:refs/remotes/mine/*' invalid
+
+test_refspec fetch 'refs/heads/*g*/for-linus:refs/remotes/mine/*' invalid
+test_refspec push 'refs/heads/*g*/for-linus:refs/remotes/mine/*' invalid
+
+test_refspec fetch 'refs/heads/*/for-linus:refs/remotes/mine/*'
+test_refspec push 'refs/heads/*/for-linus:refs/remotes/mine/*'
+
+good=$(printf '\303\204')
+test_refspec fetch "refs/heads/${good}"
+bad=$(printf '\011tab')
+test_refspec fetch "refs/heads/${bad}" invalid
+
+test_done
diff --git a/t/t5512-ls-remote.sh b/t/t5512-ls-remote.sh
new file mode 100755
index 0000000..5dbe107
--- /dev/null
+++ b/t/t5512-ls-remote.sh
@@ -0,0 +1,383 @@
+#!/bin/sh
+
+test_description='git ls-remote'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+generate_references () {
+ for ref
+ do
+ oid=$(git rev-parse "$ref") &&
+ printf '%s\t%s\n' "$oid" "$ref" || return 1
+ done
+}
+
+test_expect_success 'set up fake upload-pack' '
+ # This can be used to simulate an upload-pack that just shows the
+ # contents of the "input" file (prepared with the test-tool pkt-line
+ # helper), and does not do any negotiation (since ls-remote does not
+ # need it).
+ write_script cat-input <<-\EOF
+ # send our initial advertisement/response
+ cat input
+ # soak up the flush packet from the client
+ cat
+ EOF
+'
+
+test_expect_success 'dies when no remote found' '
+ test_must_fail git ls-remote
+'
+
+test_expect_success setup '
+ >file &&
+ git add file &&
+ test_tick &&
+ git commit -m initial &&
+ git tag mark &&
+ git tag mark1.1 &&
+ git tag mark1.2 &&
+ git tag mark1.10 &&
+ git show-ref --tags -d >expected.tag.raw &&
+ sed -e "s/ / /" expected.tag.raw >expected.tag &&
+ generate_references HEAD >expected.all &&
+ git show-ref -d >refs &&
+ sed -e "s/ / /" refs >>expected.all &&
+
+ git remote add self "$(pwd)/.git" &&
+ git remote add self2 "."
+'
+
+test_expect_success 'ls-remote --tags .git' '
+ git ls-remote --tags .git >actual &&
+ test_cmp expected.tag actual
+'
+
+test_expect_success 'ls-remote .git' '
+ git ls-remote .git >actual &&
+ test_cmp expected.all actual
+'
+
+test_expect_success 'ls-remote --tags self' '
+ git ls-remote --tags self >actual &&
+ test_cmp expected.tag actual
+'
+
+test_expect_success 'ls-remote self' '
+ git ls-remote self >actual &&
+ test_cmp expected.all actual
+'
+
+test_expect_success 'ls-remote --sort="version:refname" --tags self' '
+ generate_references \
+ refs/tags/mark \
+ refs/tags/mark1.1 \
+ refs/tags/mark1.2 \
+ refs/tags/mark1.10 >expect &&
+ git ls-remote --sort="version:refname" --tags self >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'ls-remote --sort="-version:refname" --tags self' '
+ generate_references \
+ refs/tags/mark1.10 \
+ refs/tags/mark1.2 \
+ refs/tags/mark1.1 \
+ refs/tags/mark >expect &&
+ git ls-remote --sort="-version:refname" --tags self >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'ls-remote --sort="-refname" --tags self' '
+ generate_references \
+ refs/tags/mark1.2 \
+ refs/tags/mark1.10 \
+ refs/tags/mark1.1 \
+ refs/tags/mark >expect &&
+ git ls-remote --sort="-refname" --tags self >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'dies when no remote specified, multiple remotes found, and no default specified' '
+ test_must_fail git ls-remote
+'
+
+test_expect_success 'succeeds when no remote specified but only one found' '
+ test_when_finished git remote add self2 "." &&
+ git remote remove self2 &&
+ git ls-remote
+'
+
+test_expect_success 'use "origin" when no remote specified and multiple found' '
+ URL="$(pwd)/.git" &&
+ echo "From $URL" >exp_err &&
+
+ git remote add origin "$URL" &&
+ git ls-remote 2>actual_err >actual &&
+
+ test_cmp exp_err actual_err &&
+ test_cmp expected.all actual
+'
+
+test_expect_success 'suppress "From <url>" with -q' '
+ git ls-remote -q 2>actual_err &&
+ ! test_cmp exp_err actual_err
+'
+
+test_expect_success 'use branch.<name>.remote if possible' '
+ #
+ # Test that we are indeed using branch.<name>.remote, not "origin", even
+ # though the "origin" remote has been set.
+ #
+
+ # setup a new remote to differentiate from "origin"
+ git clone . other.git &&
+ (
+ cd other.git &&
+ echo "$(git rev-parse HEAD) HEAD" &&
+ git show-ref | sed -e "s/ / /"
+ ) >exp &&
+
+ URL="other.git" &&
+ echo "From $URL" >exp_err &&
+
+ git remote add other $URL &&
+ git config branch.main.remote other &&
+
+ git ls-remote 2>actual_err >actual &&
+ test_cmp exp_err actual_err &&
+ test_cmp exp actual
+'
+
+test_expect_success 'confuses pattern as remote when no remote specified' '
+ if test_have_prereq MINGW
+ then
+ # Windows does not like asterisks in pathname
+ does_not_exist=main
+ else
+ does_not_exist="refs*main"
+ fi &&
+ cat >exp <<-EOF &&
+ fatal: '\''$does_not_exist'\'' does not appear to be a git repository
+ fatal: Could not read from remote repository.
+
+ Please make sure you have the correct access rights
+ and the repository exists.
+ EOF
+ #
+ # Do not expect "git ls-remote <pattern>" to work; ls-remote needs
+ # <remote> if you want to feed <pattern>, just like you cannot say
+ # fetch <branch>.
+ # We could just as easily have used "main"; the "*" emphasizes its
+ # role as a pattern.
+ test_must_fail git ls-remote "$does_not_exist" >actual 2>&1 &&
+ test_cmp exp actual
+'
+
+test_expect_success 'die with non-2 for wrong repository even with --exit-code' '
+ {
+ git ls-remote --exit-code ./no-such-repository
+ status=$?
+ } &&
+ test $status != 2 && test $status != 0
+'
+
+test_expect_success 'Report success even when nothing matches' '
+ git ls-remote other.git "refs/nsn/*" >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'Report no-match with --exit-code' '
+ test_expect_code 2 git ls-remote --exit-code other.git "refs/nsn/*" >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'Report match with --exit-code' '
+ git ls-remote --exit-code other.git "refs/tags/*" >actual &&
+ git ls-remote . tags/mark* >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'set up some extra tags for ref hiding' '
+ git tag magic/one &&
+ git tag magic/two
+'
+
+for configsection in transfer uploadpack
+do
+ test_expect_success "Hide some refs with $configsection.hiderefs" '
+ test_config $configsection.hiderefs refs/tags &&
+ git ls-remote . >actual &&
+ test_unconfig $configsection.hiderefs &&
+ git ls-remote . >expect.raw &&
+ sed -e "/ refs\/tags\//d" expect.raw >expect &&
+ test_cmp expect actual
+ '
+
+ test_expect_success "Override hiding of $configsection.hiderefs" '
+ test_when_finished "test_unconfig $configsection.hiderefs" &&
+ git config --add $configsection.hiderefs refs/tags &&
+ git config --add $configsection.hiderefs "!refs/tags/magic" &&
+ git config --add $configsection.hiderefs refs/tags/magic/one &&
+ git ls-remote . >actual &&
+ grep refs/tags/magic/two actual &&
+ ! grep refs/tags/magic/one actual
+ '
+
+done
+
+test_expect_success 'overrides work between mixed transfer/upload-pack hideRefs' '
+ test_config uploadpack.hiderefs refs/tags &&
+ test_config transfer.hiderefs "!refs/tags/magic" &&
+ git ls-remote . >actual &&
+ grep refs/tags/magic actual
+'
+
+test_expect_success 'protocol v2 supports hiderefs' '
+ test_config uploadpack.hiderefs refs/tags &&
+ git -c protocol.version=2 ls-remote . >actual &&
+ ! grep refs/tags actual
+'
+
+test_expect_success 'ls-remote --symref' '
+ git fetch origin &&
+ echo "ref: refs/heads/main HEAD" >expect.v2 &&
+ generate_references \
+ HEAD \
+ refs/heads/main >>expect.v2 &&
+ echo "ref: refs/remotes/origin/main refs/remotes/origin/HEAD" >>expect.v2 &&
+ oid=$(git rev-parse HEAD) &&
+ echo "$oid refs/remotes/origin/HEAD" >>expect.v2 &&
+ generate_references \
+ refs/remotes/origin/main \
+ refs/tags/mark \
+ refs/tags/mark1.1 \
+ refs/tags/mark1.10 \
+ refs/tags/mark1.2 >>expect.v2 &&
+ # v0 does not show non-HEAD symrefs
+ grep -v "ref: refs/remotes" <expect.v2 >expect.v0 &&
+ git -c protocol.version=0 ls-remote --symref >actual.v0 &&
+ test_cmp expect.v0 actual.v0 &&
+ git -c protocol.version=2 ls-remote --symref >actual.v2 &&
+ test_cmp expect.v2 actual.v2
+'
+
+test_expect_success 'ls-remote with filtered symref (refname)' '
+ rev=$(git rev-parse HEAD) &&
+ cat >expect <<-EOF &&
+ ref: refs/heads/main HEAD
+ $rev HEAD
+ EOF
+ git ls-remote --symref . HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'ls-remote with filtered symref (--heads)' '
+ git symbolic-ref refs/heads/foo refs/tags/mark &&
+ cat >expect.v2 <<-EOF &&
+ ref: refs/tags/mark refs/heads/foo
+ $rev refs/heads/foo
+ $rev refs/heads/main
+ EOF
+ grep -v "^ref: refs/tags/" <expect.v2 >expect.v0 &&
+ git -c protocol.version=0 ls-remote --symref --heads . >actual.v0 &&
+ test_cmp expect.v0 actual.v0 &&
+ git -c protocol.version=2 ls-remote --symref --heads . >actual.v2 &&
+ test_cmp expect.v2 actual.v2
+'
+
+test_expect_success 'indicate no refs in v0 standards-compliant empty remote' '
+ # Git does not produce an output like this, but it does match the
+ # standard and is produced by other implementations like JGit. So
+ # hard-code the case we care about.
+ #
+ # The actual capabilities do not matter; there are none that would
+ # change how ls-remote behaves.
+ oid=0000000000000000000000000000000000000000 &&
+ test-tool pkt-line pack >input.q <<-EOF &&
+ $oid capabilities^{}Qcaps-go-here
+ 0000
+ EOF
+ q_to_nul <input.q >input &&
+
+ # --exit-code asks the command to exit with 2 when no
+ # matching refs are found.
+ test_expect_code 2 git ls-remote --exit-code --upload-pack=./cat-input .
+'
+
+test_expect_success 'ls-remote works outside repository' '
+ # It is important for this repo to be inside the nongit
+ # area, as we want a repo name that does not include
+ # slashes (because those inhibit some of our configuration
+ # lookups).
+ nongit git init --bare dst.git &&
+ nongit git ls-remote dst.git
+'
+
+test_expect_success 'ls-remote --sort fails gracefully outside repository' '
+ # Use a sort key that requires access to the referenced objects.
+ nongit test_must_fail git ls-remote --sort=authordate "$TRASH_DIRECTORY" 2>err &&
+ test_grep "^fatal: not a git repository, but the field '\''authordate'\'' requires access to object data" err
+'
+
+test_expect_success 'ls-remote patterns work with all protocol versions' '
+ git for-each-ref --format="%(objectname) %(refname)" \
+ refs/heads/main refs/remotes/origin/main >expect &&
+ git -c protocol.version=0 ls-remote . main >actual.v0 &&
+ test_cmp expect actual.v0 &&
+ git -c protocol.version=2 ls-remote . main >actual.v2 &&
+ test_cmp expect actual.v2
+'
+
+test_expect_success 'ls-remote prefixes work with all protocol versions' '
+ git for-each-ref --format="%(objectname) %(refname)" \
+ refs/heads/ refs/tags/ >expect &&
+ git -c protocol.version=0 ls-remote --heads --tags . >actual.v0 &&
+ test_cmp expect actual.v0 &&
+ git -c protocol.version=2 ls-remote --heads --tags . >actual.v2 &&
+ test_cmp expect actual.v2
+'
+
+test_expect_success 'v0 clients can handle multiple symrefs' '
+ # Modern versions of Git will not return multiple symref capabilities
+ # for v0, so we have to hard-code the response. Note that we will
+ # always use both v0 and object-format=sha1 here, as the hard-coded
+ # response reflects a server that only supports those.
+ oid=1234567890123456789012345678901234567890 &&
+ symrefs="symref=refs/remotes/origin/HEAD:refs/remotes/origin/main" &&
+ symrefs="$symrefs symref=HEAD:refs/heads/main" &&
+
+ # Likewise we want to make sure our parser is not fooled by the string
+ # "symref" appearing as part of an earlier cap. But there is no way to
+ # do that via upload-pack, as arbitrary strings can appear only in a
+ # "symref" value itself (where we skip past the values as a whole)
+ # and "agent" (which always appears after "symref", so putting our
+ # parser in a confused state is less interesting).
+ caps="some other caps including a-fake-symref-cap" &&
+
+ test-tool pkt-line pack >input.q <<-EOF &&
+ $oid HEADQ$caps $symrefs
+ $oid refs/heads/main
+ $oid refs/remotes/origin/HEAD
+ $oid refs/remotes/origin/main
+ 0000
+ EOF
+ q_to_nul <input.q >input &&
+
+ cat >expect <<-EOF &&
+ ref: refs/heads/main HEAD
+ $oid HEAD
+ $oid refs/heads/main
+ ref: refs/remotes/origin/main refs/remotes/origin/HEAD
+ $oid refs/remotes/origin/HEAD
+ $oid refs/remotes/origin/main
+ EOF
+
+ git ls-remote --symref --upload-pack=./cat-input . >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t5513-fetch-track.sh b/t/t5513-fetch-track.sh
new file mode 100755
index 0000000..c46c4db
--- /dev/null
+++ b/t/t5513-fetch-track.sh
@@ -0,0 +1,31 @@
+#!/bin/sh
+
+test_description='fetch follows remote-tracking branches correctly'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ >file &&
+ git add . &&
+ test_tick &&
+ git commit -m Initial &&
+ git branch b-0 &&
+ git branch b1 &&
+ git branch b/one &&
+ test_create_repo other &&
+ (
+ cd other &&
+ git config remote.origin.url .. &&
+ git config remote.origin.fetch "+refs/heads/b/*:refs/remotes/b/*"
+ )
+'
+
+test_expect_success fetch '
+ (
+ cd other && git fetch origin &&
+ test "$(git for-each-ref --format="%(refname)")" = refs/remotes/b/one
+ )
+'
+
+test_done
diff --git a/t/t5514-fetch-multiple.sh b/t/t5514-fetch-multiple.sh
new file mode 100755
index 0000000..a95841d
--- /dev/null
+++ b/t/t5514-fetch-multiple.sh
@@ -0,0 +1,212 @@
+#!/bin/sh
+
+test_description='fetch --all works correctly'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+setup_repository () {
+ mkdir "$1" && (
+ cd "$1" &&
+ git init &&
+ >file &&
+ git add file &&
+ test_tick &&
+ git commit -m "Initial" &&
+ git checkout -b side &&
+ >elif &&
+ git add elif &&
+ test_tick &&
+ git commit -m "Second" &&
+ git checkout main
+ )
+}
+
+test_expect_success setup '
+ setup_repository one &&
+ setup_repository two &&
+ (
+ cd two && git branch another
+ ) &&
+ git clone --mirror two three &&
+ git clone one test
+'
+
+cat > test/expect << EOF
+ one/main
+ one/side
+ origin/HEAD -> origin/main
+ origin/main
+ origin/side
+ three/another
+ three/main
+ three/side
+ two/another
+ two/main
+ two/side
+EOF
+
+test_expect_success 'git fetch --all' '
+ (cd test &&
+ git remote add one ../one &&
+ git remote add two ../two &&
+ git remote add three ../three &&
+ git fetch --all &&
+ git branch -r > output &&
+ test_cmp expect output)
+'
+
+test_expect_success 'git fetch --all --no-write-fetch-head' '
+ (cd test &&
+ rm -f .git/FETCH_HEAD &&
+ git fetch --all --no-write-fetch-head &&
+ test_path_is_missing .git/FETCH_HEAD)
+'
+
+test_expect_success 'git fetch --all should continue if a remote has errors' '
+ (git clone one test2 &&
+ cd test2 &&
+ git remote add bad ../non-existing &&
+ git remote add one ../one &&
+ git remote add two ../two &&
+ git remote add three ../three &&
+ test_must_fail git fetch --all &&
+ git branch -r > output &&
+ test_cmp ../test/expect output)
+'
+
+test_expect_success 'git fetch --all does not allow non-option arguments' '
+ (cd test &&
+ test_must_fail git fetch --all origin &&
+ test_must_fail git fetch --all origin main)
+'
+
+cat > expect << EOF
+ origin/HEAD -> origin/main
+ origin/main
+ origin/side
+ three/another
+ three/main
+ three/side
+EOF
+
+test_expect_success 'git fetch --multiple (but only one remote)' '
+ (git clone one test3 &&
+ cd test3 &&
+ git remote add three ../three &&
+ git fetch --multiple three &&
+ git branch -r > output &&
+ test_cmp ../expect output)
+'
+
+cat > expect << EOF
+ one/main
+ one/side
+ two/another
+ two/main
+ two/side
+EOF
+
+test_expect_success 'git fetch --multiple (two remotes)' '
+ (git clone one test4 &&
+ cd test4 &&
+ git remote rm origin &&
+ git remote add one ../one &&
+ git remote add two ../two &&
+ GIT_TRACE=1 git fetch --multiple one two 2>trace &&
+ git branch -r > output &&
+ test_cmp ../expect output &&
+ grep "built-in: git maintenance" trace >gc &&
+ test_line_count = 1 gc
+ )
+'
+
+test_expect_success 'git fetch --multiple (bad remote names)' '
+ (cd test4 &&
+ test_must_fail git fetch --multiple four)
+'
+
+
+test_expect_success 'git fetch --all (skipFetchAll)' '
+ (cd test4 &&
+ for b in $(git branch -r)
+ do
+ git branch -r -d $b || exit 1
+ done &&
+ git remote add three ../three &&
+ git config remote.three.skipFetchAll true &&
+ git fetch --all &&
+ git branch -r > output &&
+ test_cmp ../expect output)
+'
+
+cat > expect << EOF
+ one/main
+ one/side
+ three/another
+ three/main
+ three/side
+ two/another
+ two/main
+ two/side
+EOF
+
+test_expect_success 'git fetch --multiple (ignoring skipFetchAll)' '
+ (cd test4 &&
+ for b in $(git branch -r)
+ do
+ git branch -r -d $b || exit 1
+ done &&
+ git fetch --multiple one two three &&
+ git branch -r > output &&
+ test_cmp ../expect output)
+'
+
+test_expect_success 'git fetch --all --no-tags' '
+ git clone one test5 &&
+ git clone test5 test6 &&
+ (cd test5 && git tag test-tag) &&
+ (
+ cd test6 &&
+ git fetch --all --no-tags &&
+ git tag >output
+ ) &&
+ test_must_be_empty test6/output
+'
+
+test_expect_success 'git fetch --all --tags' '
+ echo test-tag >expect &&
+ git clone one test7 &&
+ git clone test7 test8 &&
+ (
+ cd test7 &&
+ test_commit test-tag &&
+ git reset --hard HEAD^
+ ) &&
+ (
+ cd test8 &&
+ git fetch --all --tags &&
+ git tag >output
+ ) &&
+ test_cmp expect test8/output
+'
+
+test_expect_success 'parallel' '
+ git remote add one ./bogus1 &&
+ git remote add two ./bogus2 &&
+
+ test_must_fail env GIT_TRACE="$PWD/trace" \
+ git fetch --jobs=2 --multiple one two 2>err &&
+ grep "preparing to run up to 2 tasks" trace &&
+ test_grep "could not fetch .one.*128" err &&
+ test_grep "could not fetch .two.*128" err
+'
+
+test_expect_success 'git fetch --multiple --jobs=0 picks a default' '
+ (cd test &&
+ git fetch --multiple --jobs=0)
+'
+
+test_done
diff --git a/t/t5515-fetch-merge-logic.sh b/t/t5515-fetch-merge-logic.sh
new file mode 100755
index 0000000..c100a80
--- /dev/null
+++ b/t/t5515-fetch-merge-logic.sh
@@ -0,0 +1,230 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Santi Béjar, based on t4013 by Junio C Hamano
+#
+#
+
+test_description='Merge logic in fetch'
+
+# NEEDSWORK: If the overspecification of the expected result is reduced, we
+# might be able to run this test in all protocol versions.
+GIT_TEST_PROTOCOL_VERSION=0
+export GIT_TEST_PROTOCOL_VERSION
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+build_script () {
+ script="$1" &&
+ for i in one three_file main topic_2 one_tree three two two2 three2
+ do
+ echo "s/$(test_oid --hash=sha1 "$i")/$(test_oid "$i")/g" >>"$script"
+ done
+}
+
+convert_expected () {
+ file="$1" &&
+ script="$2" &&
+ sed -f "$script" "$file" >"$file.tmp" &&
+ mv "$file.tmp" "$file"
+}
+
+test_expect_success setup '
+ GIT_AUTHOR_DATE="2006-06-26 00:00:00 +0000" &&
+ GIT_COMMITTER_DATE="2006-06-26 00:00:00 +0000" &&
+ export GIT_AUTHOR_DATE GIT_COMMITTER_DATE &&
+
+ test_oid_cache <<-EOF &&
+ one sha1:8e32a6d901327a23ef831511badce7bf3bf46689
+ one sha256:8739546433ab1ac72ee93088dce611210effee072b2b586ceac6dde43ebec9ce
+
+ three_file sha1:0e3b14047d3ee365f4f2a1b673db059c3972589c
+ three_file sha256:bc4447d50c07497a8bfe6eef817f2364ecca9d471452e43b52756cc1a908bd32
+
+ main sha1:ecf3b3627b498bdcb735cc4343bf165f76964e9a
+ main sha256:fff666109892bb4b1c80cd1649d2d8762a0663db8b5d46c8be98360b64fbba5f
+
+ one_tree sha1:22feea448b023a2d864ef94b013735af34d238ba
+ one_tree sha256:6e4743f4ef2356b881dda5e91f5c7cdffe870faf350bf7b312f80a20935f5d83
+
+ three sha1:c61a82b60967180544e3c19f819ddbd0c9f89899
+ three sha256:0cc6d1eda617ded715170786e31ba4e2d0185404ec5a3508dd0d73b324860c6a
+
+ two sha1:525b7fb068d59950d185a8779dc957c77eed73ba
+ two sha256:3b21de3440cd38c2a9e9b464adb923f7054949ed4c918e1a0ac4c95cd52774db
+
+ topic_2 sha1:b4ab76b1a01ea602209932134a44f1e6bd610832
+ topic_2 sha256:380ebae0113f877ce46fcdf39d5bc33e4dc0928db5c5a4d5fdc78381c4d55ae3
+
+ two2 sha1:6134ee8f857693b96ff1cc98d3e2fd62b199e5a8
+ two2 sha256:87a2d3ee29c83a3dc7afd41c0606b11f67603120b910a7be7840accdc18344d4
+
+ three2 sha1:0567da4d5edd2ff4bb292a465ba9e64dcad9536b
+ three2 sha256:cceb3e8eca364fa9a0a39a1efbebecacc664af86cbbd8070571f5faeb5f0e8c3
+ EOF
+
+ echo >file original &&
+ git add file &&
+ git commit -a -m One &&
+ git tag tag-one &&
+ git tag tag-one-tree HEAD^{tree} &&
+ git branch one &&
+
+ echo two >> file &&
+ git commit -a -m Two &&
+ git tag -a -m "Tag Two" tag-two &&
+ git branch two &&
+
+ echo three >> file &&
+ git commit -a -m Three &&
+ git tag -a -m "Tag Three" tag-three &&
+ git tag -a -m "Tag Three file" tag-three-file HEAD^{tree}:file &&
+ git branch three &&
+
+ echo main >> file &&
+ git commit -a -m Main &&
+ git tag -a -m "Tag Main" tag-main &&
+
+ git checkout three &&
+
+ git clone . cloned &&
+ cd cloned &&
+ git config remote.origin.url ../.git/ &&
+
+ git config remote.config-explicit.url ../.git/ &&
+ git config remote.config-explicit.fetch refs/heads/main:remotes/rem/main &&
+ git config --add remote.config-explicit.fetch refs/heads/one:remotes/rem/one &&
+ git config --add remote.config-explicit.fetch two:remotes/rem/two &&
+ git config --add remote.config-explicit.fetch refs/heads/three:remotes/rem/three &&
+ remotes="config-explicit" &&
+
+ git config remote.config-glob.url ../.git/ &&
+ git config remote.config-glob.fetch refs/heads/*:refs/remotes/rem/* &&
+ remotes="$remotes config-glob" &&
+
+ mkdir -p .git/remotes &&
+ cat >.git/remotes/remote-explicit <<-\EOF &&
+ URL: ../.git/
+ Pull: refs/heads/main:remotes/rem/main
+ Pull: refs/heads/one:remotes/rem/one
+ Pull: two:remotes/rem/two
+ Pull: refs/heads/three:remotes/rem/three
+ EOF
+ remotes="$remotes remote-explicit" &&
+
+ cat >.git/remotes/remote-glob <<-\EOF &&
+ URL: ../.git/
+ Pull: refs/heads/*:refs/remotes/rem/*
+ EOF
+ remotes="$remotes remote-glob" &&
+
+ mkdir -p .git/branches &&
+ echo "../.git" > .git/branches/branches-default &&
+ remotes="$remotes branches-default" &&
+
+ echo "../.git#one" > .git/branches/branches-one &&
+ remotes="$remotes branches-one" &&
+
+ for remote in $remotes ; do
+ git config branch.br-$remote.remote $remote &&
+ git config branch.br-$remote-merge.remote $remote &&
+ git config branch.br-$remote-merge.merge refs/heads/three &&
+ git config branch.br-$remote-octopus.remote $remote &&
+ git config branch.br-$remote-octopus.merge refs/heads/one &&
+ git config --add branch.br-$remote-octopus.merge two || return 1
+ done &&
+ build_script sed_script
+'
+
+# Merge logic depends on branch properties and Pull: or .fetch lines
+for remote in $remotes ; do
+ for branch in "" "-merge" "-octopus" ; do
+cat <<EOF
+br-$remote$branch
+br-$remote$branch $remote
+EOF
+ done
+done > tests
+
+# Merge logic does not depend on branch properties,
+# but does depend on Pull: or fetch lines.
+# Use two branches completely unrelated from the arguments,
+# the clone default and one without branch properties
+for branch in main br-unconfig ; do
+ echo $branch
+ for remote in $remotes ; do
+ echo $branch $remote
+ done
+done >> tests
+
+# Merge logic does not depend on branch properties
+# neither in the Pull: or .fetch config
+for branch in main br-unconfig ; do
+ cat <<EOF
+$branch ../.git
+$branch ../.git one
+$branch ../.git one two
+$branch --tags ../.git
+$branch ../.git tag tag-one tag tag-three
+$branch ../.git tag tag-one-tree tag tag-three-file
+$branch ../.git one tag tag-one tag tag-three-file
+EOF
+done >> tests
+
+while read cmd
+do
+ case "$cmd" in
+ '' | '#'*) continue ;;
+ esac
+ test=$(echo "$cmd" | sed -e 's|[/ ][/ ]*|_|g')
+ pfx=$(printf "%04d" $test_count)
+ expect_f="$TEST_DIRECTORY/t5515/fetch.$test"
+ actual_f="$pfx-fetch.$test"
+ expect_r="$TEST_DIRECTORY/t5515/refs.$test"
+ actual_r="$pfx-refs.$test"
+
+ test_expect_success "$cmd" '
+ cp "$expect_f" expect_f &&
+ convert_expected expect_f sed_script &&
+ cp "$expect_r" expect_r &&
+ convert_expected expect_r sed_script &&
+ {
+ echo "# $cmd" &&
+ set x $cmd && shift &&
+ git symbolic-ref HEAD refs/heads/$1 && shift &&
+ rm -f .git/FETCH_HEAD &&
+ git for-each-ref \
+ refs/heads refs/remotes/rem refs/tags |
+ while read val type refname
+ do
+ git update-ref -d "$refname" "$val" || return 1
+ done &&
+ git fetch "$@" >/dev/null &&
+ cat .git/FETCH_HEAD
+ } >"$actual_f" &&
+ git show-ref >"$actual_r" &&
+ if test -f "expect_f"
+ then
+ test_cmp "expect_f" "$actual_f" &&
+ rm -f "$actual_f"
+ else
+ # this is to help developing new tests.
+ cp "$actual_f" "$expect_f"
+ false
+ fi &&
+ if test -f "expect_r"
+ then
+ test_cmp "expect_r" "$actual_r" &&
+ rm -f "$actual_r"
+ else
+ # this is to help developing new tests.
+ cp "$actual_r" "$expect_r"
+ false
+ fi
+ '
+done < tests
+
+test_done
diff --git a/t/t5515/fetch.br-branches-default b/t/t5515/fetch.br-branches-default
new file mode 100644
index 0000000..0bed09c
--- /dev/null
+++ b/t/t5515/fetch.br-branches-default
@@ -0,0 +1,8 @@
+# br-branches-default
+b4ab76b1a01ea602209932134a44f1e6bd610832 branch 'main' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-branches-default-merge b/t/t5515/fetch.br-branches-default-merge
new file mode 100644
index 0000000..59eef63
--- /dev/null
+++ b/t/t5515/fetch.br-branches-default-merge
@@ -0,0 +1,9 @@
+# br-branches-default-merge
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b branch 'three' of ../
+b4ab76b1a01ea602209932134a44f1e6bd610832 not-for-merge branch 'main' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-branches-default-merge_branches-default b/t/t5515/fetch.br-branches-default-merge_branches-default
new file mode 100644
index 0000000..490186c
--- /dev/null
+++ b/t/t5515/fetch.br-branches-default-merge_branches-default
@@ -0,0 +1,9 @@
+# br-branches-default-merge branches-default
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b branch 'three' of ../
+b4ab76b1a01ea602209932134a44f1e6bd610832 not-for-merge branch 'main' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-branches-default-octopus b/t/t5515/fetch.br-branches-default-octopus
new file mode 100644
index 0000000..d484138
--- /dev/null
+++ b/t/t5515/fetch.br-branches-default-octopus
@@ -0,0 +1,10 @@
+# br-branches-default-octopus
+8e32a6d901327a23ef831511badce7bf3bf46689 branch 'one' of ../
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 branch 'two' of ../
+b4ab76b1a01ea602209932134a44f1e6bd610832 not-for-merge branch 'main' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-branches-default-octopus_branches-default b/t/t5515/fetch.br-branches-default-octopus_branches-default
new file mode 100644
index 0000000..b79a5fe
--- /dev/null
+++ b/t/t5515/fetch.br-branches-default-octopus_branches-default
@@ -0,0 +1,10 @@
+# br-branches-default-octopus branches-default
+8e32a6d901327a23ef831511badce7bf3bf46689 branch 'one' of ../
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 branch 'two' of ../
+b4ab76b1a01ea602209932134a44f1e6bd610832 not-for-merge branch 'main' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-branches-default_branches-default b/t/t5515/fetch.br-branches-default_branches-default
new file mode 100644
index 0000000..1c866d8
--- /dev/null
+++ b/t/t5515/fetch.br-branches-default_branches-default
@@ -0,0 +1,8 @@
+# br-branches-default branches-default
+b4ab76b1a01ea602209932134a44f1e6bd610832 branch 'main' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-branches-one b/t/t5515/fetch.br-branches-one
new file mode 100644
index 0000000..6925a77
--- /dev/null
+++ b/t/t5515/fetch.br-branches-one
@@ -0,0 +1,8 @@
+# br-branches-one
+8e32a6d901327a23ef831511badce7bf3bf46689 branch 'one' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-branches-one-merge b/t/t5515/fetch.br-branches-one-merge
new file mode 100644
index 0000000..8525703
--- /dev/null
+++ b/t/t5515/fetch.br-branches-one-merge
@@ -0,0 +1,9 @@
+# br-branches-one-merge
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b branch 'three' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge branch 'one' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-branches-one-merge_branches-one b/t/t5515/fetch.br-branches-one-merge_branches-one
new file mode 100644
index 0000000..3a63a7f
--- /dev/null
+++ b/t/t5515/fetch.br-branches-one-merge_branches-one
@@ -0,0 +1,9 @@
+# br-branches-one-merge branches-one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b branch 'three' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge branch 'one' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-branches-one-octopus b/t/t5515/fetch.br-branches-one-octopus
new file mode 100644
index 0000000..46af763
--- /dev/null
+++ b/t/t5515/fetch.br-branches-one-octopus
@@ -0,0 +1,9 @@
+# br-branches-one-octopus
+8e32a6d901327a23ef831511badce7bf3bf46689 branch 'one' of ../
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 branch 'two' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-branches-one-octopus_branches-one b/t/t5515/fetch.br-branches-one-octopus_branches-one
new file mode 100644
index 0000000..becfde9
--- /dev/null
+++ b/t/t5515/fetch.br-branches-one-octopus_branches-one
@@ -0,0 +1,9 @@
+# br-branches-one-octopus branches-one
+8e32a6d901327a23ef831511badce7bf3bf46689 branch 'one' of ../
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 branch 'two' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-branches-one_branches-one b/t/t5515/fetch.br-branches-one_branches-one
new file mode 100644
index 0000000..9ba34c6
--- /dev/null
+++ b/t/t5515/fetch.br-branches-one_branches-one
@@ -0,0 +1,8 @@
+# br-branches-one branches-one
+8e32a6d901327a23ef831511badce7bf3bf46689 branch 'one' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-config-explicit b/t/t5515/fetch.br-config-explicit
new file mode 100644
index 0000000..7466a73
--- /dev/null
+++ b/t/t5515/fetch.br-config-explicit
@@ -0,0 +1,11 @@
+# br-config-explicit
+b4ab76b1a01ea602209932134a44f1e6bd610832 branch 'main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge branch 'one' of ../
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 not-for-merge branch 'two' of ../
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b not-for-merge branch 'three' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-config-explicit-merge b/t/t5515/fetch.br-config-explicit-merge
new file mode 100644
index 0000000..9375b7d
--- /dev/null
+++ b/t/t5515/fetch.br-config-explicit-merge
@@ -0,0 +1,11 @@
+# br-config-explicit-merge
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b branch 'three' of ../
+b4ab76b1a01ea602209932134a44f1e6bd610832 not-for-merge branch 'main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge branch 'one' of ../
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 not-for-merge branch 'two' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-config-explicit-merge_config-explicit b/t/t5515/fetch.br-config-explicit-merge_config-explicit
new file mode 100644
index 0000000..6335e2b
--- /dev/null
+++ b/t/t5515/fetch.br-config-explicit-merge_config-explicit
@@ -0,0 +1,11 @@
+# br-config-explicit-merge config-explicit
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b branch 'three' of ../
+b4ab76b1a01ea602209932134a44f1e6bd610832 not-for-merge branch 'main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge branch 'one' of ../
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 not-for-merge branch 'two' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-config-explicit-octopus b/t/t5515/fetch.br-config-explicit-octopus
new file mode 100644
index 0000000..c379aa9
--- /dev/null
+++ b/t/t5515/fetch.br-config-explicit-octopus
@@ -0,0 +1,11 @@
+# br-config-explicit-octopus
+8e32a6d901327a23ef831511badce7bf3bf46689 branch 'one' of ../
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 branch 'two' of ../
+b4ab76b1a01ea602209932134a44f1e6bd610832 not-for-merge branch 'main' of ../
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b not-for-merge branch 'three' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-config-explicit-octopus_config-explicit b/t/t5515/fetch.br-config-explicit-octopus_config-explicit
new file mode 100644
index 0000000..7fb3a3e
--- /dev/null
+++ b/t/t5515/fetch.br-config-explicit-octopus_config-explicit
@@ -0,0 +1,11 @@
+# br-config-explicit-octopus config-explicit
+8e32a6d901327a23ef831511badce7bf3bf46689 branch 'one' of ../
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 branch 'two' of ../
+b4ab76b1a01ea602209932134a44f1e6bd610832 not-for-merge branch 'main' of ../
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b not-for-merge branch 'three' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-config-explicit_config-explicit b/t/t5515/fetch.br-config-explicit_config-explicit
new file mode 100644
index 0000000..86045a7
--- /dev/null
+++ b/t/t5515/fetch.br-config-explicit_config-explicit
@@ -0,0 +1,11 @@
+# br-config-explicit config-explicit
+b4ab76b1a01ea602209932134a44f1e6bd610832 branch 'main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge branch 'one' of ../
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 not-for-merge branch 'two' of ../
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b not-for-merge branch 'three' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-config-glob b/t/t5515/fetch.br-config-glob
new file mode 100644
index 0000000..bfeef2b
--- /dev/null
+++ b/t/t5515/fetch.br-config-glob
@@ -0,0 +1,11 @@
+# br-config-glob
+b4ab76b1a01ea602209932134a44f1e6bd610832 not-for-merge branch 'main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge branch 'one' of ../
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b not-for-merge branch 'three' of ../
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 not-for-merge branch 'two' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-config-glob-merge b/t/t5515/fetch.br-config-glob-merge
new file mode 100644
index 0000000..5376a4a
--- /dev/null
+++ b/t/t5515/fetch.br-config-glob-merge
@@ -0,0 +1,11 @@
+# br-config-glob-merge
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b branch 'three' of ../
+b4ab76b1a01ea602209932134a44f1e6bd610832 not-for-merge branch 'main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge branch 'one' of ../
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 not-for-merge branch 'two' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-config-glob-merge_config-glob b/t/t5515/fetch.br-config-glob-merge_config-glob
new file mode 100644
index 0000000..7dfc2fe
--- /dev/null
+++ b/t/t5515/fetch.br-config-glob-merge_config-glob
@@ -0,0 +1,11 @@
+# br-config-glob-merge config-glob
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b branch 'three' of ../
+b4ab76b1a01ea602209932134a44f1e6bd610832 not-for-merge branch 'main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge branch 'one' of ../
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 not-for-merge branch 'two' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-config-glob-octopus b/t/t5515/fetch.br-config-glob-octopus
new file mode 100644
index 0000000..1084030
--- /dev/null
+++ b/t/t5515/fetch.br-config-glob-octopus
@@ -0,0 +1,11 @@
+# br-config-glob-octopus
+8e32a6d901327a23ef831511badce7bf3bf46689 branch 'one' of ../
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 branch 'two' of ../
+b4ab76b1a01ea602209932134a44f1e6bd610832 not-for-merge branch 'main' of ../
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b not-for-merge branch 'three' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-config-glob-octopus_config-glob b/t/t5515/fetch.br-config-glob-octopus_config-glob
new file mode 100644
index 0000000..839866d
--- /dev/null
+++ b/t/t5515/fetch.br-config-glob-octopus_config-glob
@@ -0,0 +1,11 @@
+# br-config-glob-octopus config-glob
+8e32a6d901327a23ef831511badce7bf3bf46689 branch 'one' of ../
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 branch 'two' of ../
+b4ab76b1a01ea602209932134a44f1e6bd610832 not-for-merge branch 'main' of ../
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b not-for-merge branch 'three' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-config-glob_config-glob b/t/t5515/fetch.br-config-glob_config-glob
new file mode 100644
index 0000000..7b0cb91
--- /dev/null
+++ b/t/t5515/fetch.br-config-glob_config-glob
@@ -0,0 +1,11 @@
+# br-config-glob config-glob
+b4ab76b1a01ea602209932134a44f1e6bd610832 not-for-merge branch 'main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge branch 'one' of ../
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b not-for-merge branch 'three' of ../
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 not-for-merge branch 'two' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-remote-explicit b/t/t5515/fetch.br-remote-explicit
new file mode 100644
index 0000000..d6619e7
--- /dev/null
+++ b/t/t5515/fetch.br-remote-explicit
@@ -0,0 +1,11 @@
+# br-remote-explicit
+b4ab76b1a01ea602209932134a44f1e6bd610832 branch 'main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge branch 'one' of ../
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 not-for-merge branch 'two' of ../
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b not-for-merge branch 'three' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-remote-explicit-merge b/t/t5515/fetch.br-remote-explicit-merge
new file mode 100644
index 0000000..5b49b7c
--- /dev/null
+++ b/t/t5515/fetch.br-remote-explicit-merge
@@ -0,0 +1,11 @@
+# br-remote-explicit-merge
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b branch 'three' of ../
+b4ab76b1a01ea602209932134a44f1e6bd610832 not-for-merge branch 'main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge branch 'one' of ../
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 not-for-merge branch 'two' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-remote-explicit-merge_remote-explicit b/t/t5515/fetch.br-remote-explicit-merge_remote-explicit
new file mode 100644
index 0000000..417261d
--- /dev/null
+++ b/t/t5515/fetch.br-remote-explicit-merge_remote-explicit
@@ -0,0 +1,11 @@
+# br-remote-explicit-merge remote-explicit
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b branch 'three' of ../
+b4ab76b1a01ea602209932134a44f1e6bd610832 not-for-merge branch 'main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge branch 'one' of ../
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 not-for-merge branch 'two' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-remote-explicit-octopus b/t/t5515/fetch.br-remote-explicit-octopus
new file mode 100644
index 0000000..2edef64
--- /dev/null
+++ b/t/t5515/fetch.br-remote-explicit-octopus
@@ -0,0 +1,11 @@
+# br-remote-explicit-octopus
+8e32a6d901327a23ef831511badce7bf3bf46689 branch 'one' of ../
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 branch 'two' of ../
+b4ab76b1a01ea602209932134a44f1e6bd610832 not-for-merge branch 'main' of ../
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b not-for-merge branch 'three' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-remote-explicit-octopus_remote-explicit b/t/t5515/fetch.br-remote-explicit-octopus_remote-explicit
new file mode 100644
index 0000000..ceb8752
--- /dev/null
+++ b/t/t5515/fetch.br-remote-explicit-octopus_remote-explicit
@@ -0,0 +1,11 @@
+# br-remote-explicit-octopus remote-explicit
+8e32a6d901327a23ef831511badce7bf3bf46689 branch 'one' of ../
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 branch 'two' of ../
+b4ab76b1a01ea602209932134a44f1e6bd610832 not-for-merge branch 'main' of ../
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b not-for-merge branch 'three' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-remote-explicit_remote-explicit b/t/t5515/fetch.br-remote-explicit_remote-explicit
new file mode 100644
index 0000000..b7abbd2
--- /dev/null
+++ b/t/t5515/fetch.br-remote-explicit_remote-explicit
@@ -0,0 +1,11 @@
+# br-remote-explicit remote-explicit
+b4ab76b1a01ea602209932134a44f1e6bd610832 branch 'main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge branch 'one' of ../
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 not-for-merge branch 'two' of ../
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b not-for-merge branch 'three' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-remote-glob b/t/t5515/fetch.br-remote-glob
new file mode 100644
index 0000000..645412c
--- /dev/null
+++ b/t/t5515/fetch.br-remote-glob
@@ -0,0 +1,11 @@
+# br-remote-glob
+b4ab76b1a01ea602209932134a44f1e6bd610832 not-for-merge branch 'main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge branch 'one' of ../
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b not-for-merge branch 'three' of ../
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 not-for-merge branch 'two' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-remote-glob-merge b/t/t5515/fetch.br-remote-glob-merge
new file mode 100644
index 0000000..8512cfd
--- /dev/null
+++ b/t/t5515/fetch.br-remote-glob-merge
@@ -0,0 +1,11 @@
+# br-remote-glob-merge
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b branch 'three' of ../
+b4ab76b1a01ea602209932134a44f1e6bd610832 not-for-merge branch 'main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge branch 'one' of ../
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 not-for-merge branch 'two' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-remote-glob-merge_remote-glob b/t/t5515/fetch.br-remote-glob-merge_remote-glob
new file mode 100644
index 0000000..7394164
--- /dev/null
+++ b/t/t5515/fetch.br-remote-glob-merge_remote-glob
@@ -0,0 +1,11 @@
+# br-remote-glob-merge remote-glob
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b branch 'three' of ../
+b4ab76b1a01ea602209932134a44f1e6bd610832 not-for-merge branch 'main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge branch 'one' of ../
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 not-for-merge branch 'two' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-remote-glob-octopus b/t/t5515/fetch.br-remote-glob-octopus
new file mode 100644
index 0000000..0ca4719
--- /dev/null
+++ b/t/t5515/fetch.br-remote-glob-octopus
@@ -0,0 +1,11 @@
+# br-remote-glob-octopus
+8e32a6d901327a23ef831511badce7bf3bf46689 branch 'one' of ../
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 branch 'two' of ../
+b4ab76b1a01ea602209932134a44f1e6bd610832 not-for-merge branch 'main' of ../
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b not-for-merge branch 'three' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-remote-glob-octopus_remote-glob b/t/t5515/fetch.br-remote-glob-octopus_remote-glob
new file mode 100644
index 0000000..7e7b0ba
--- /dev/null
+++ b/t/t5515/fetch.br-remote-glob-octopus_remote-glob
@@ -0,0 +1,11 @@
+# br-remote-glob-octopus remote-glob
+8e32a6d901327a23ef831511badce7bf3bf46689 branch 'one' of ../
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 branch 'two' of ../
+b4ab76b1a01ea602209932134a44f1e6bd610832 not-for-merge branch 'main' of ../
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b not-for-merge branch 'three' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-remote-glob_remote-glob b/t/t5515/fetch.br-remote-glob_remote-glob
new file mode 100644
index 0000000..7bae5ec
--- /dev/null
+++ b/t/t5515/fetch.br-remote-glob_remote-glob
@@ -0,0 +1,11 @@
+# br-remote-glob remote-glob
+b4ab76b1a01ea602209932134a44f1e6bd610832 not-for-merge branch 'main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge branch 'one' of ../
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b not-for-merge branch 'three' of ../
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 not-for-merge branch 'two' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-unconfig b/t/t5515/fetch.br-unconfig
new file mode 100644
index 0000000..ccaa54f
--- /dev/null
+++ b/t/t5515/fetch.br-unconfig
@@ -0,0 +1,11 @@
+# br-unconfig
+b4ab76b1a01ea602209932134a44f1e6bd610832 not-for-merge branch 'main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge branch 'one' of ../
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b not-for-merge branch 'three' of ../
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 not-for-merge branch 'two' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-unconfig_--tags_.._.git b/t/t5515/fetch.br-unconfig_--tags_.._.git
new file mode 100644
index 0000000..3afb4f8
--- /dev/null
+++ b/t/t5515/fetch.br-unconfig_--tags_.._.git
@@ -0,0 +1,8 @@
+# br-unconfig --tags ../.git
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-unconfig_.._.git b/t/t5515/fetch.br-unconfig_.._.git
new file mode 100644
index 0000000..284bb1f
--- /dev/null
+++ b/t/t5515/fetch.br-unconfig_.._.git
@@ -0,0 +1,2 @@
+# br-unconfig ../.git
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b ../
diff --git a/t/t5515/fetch.br-unconfig_.._.git_one b/t/t5515/fetch.br-unconfig_.._.git_one
new file mode 100644
index 0000000..11eb5a6
--- /dev/null
+++ b/t/t5515/fetch.br-unconfig_.._.git_one
@@ -0,0 +1,2 @@
+# br-unconfig ../.git one
+8e32a6d901327a23ef831511badce7bf3bf46689 branch 'one' of ../
diff --git a/t/t5515/fetch.br-unconfig_.._.git_one_tag_tag-one_tag_tag-three-file b/t/t5515/fetch.br-unconfig_.._.git_one_tag_tag-one_tag_tag-three-file
new file mode 100644
index 0000000..525713a
--- /dev/null
+++ b/t/t5515/fetch.br-unconfig_.._.git_one_tag_tag-one_tag_tag-three-file
@@ -0,0 +1,8 @@
+# br-unconfig ../.git one tag tag-one tag tag-three-file
+8e32a6d901327a23ef831511badce7bf3bf46689 branch 'one' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 tag 'tag-one' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-unconfig_.._.git_one_two b/t/t5515/fetch.br-unconfig_.._.git_one_two
new file mode 100644
index 0000000..3f1be22
--- /dev/null
+++ b/t/t5515/fetch.br-unconfig_.._.git_one_two
@@ -0,0 +1,3 @@
+# br-unconfig ../.git one two
+8e32a6d901327a23ef831511badce7bf3bf46689 branch 'one' of ../
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 branch 'two' of ../
diff --git a/t/t5515/fetch.br-unconfig_.._.git_tag_tag-one-tree_tag_tag-three-file b/t/t5515/fetch.br-unconfig_.._.git_tag_tag-one-tree_tag_tag-three-file
new file mode 100644
index 0000000..18c871f
--- /dev/null
+++ b/t/t5515/fetch.br-unconfig_.._.git_tag_tag-one-tree_tag_tag-three-file
@@ -0,0 +1,7 @@
+# br-unconfig ../.git tag tag-one-tree tag tag-three-file
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-unconfig_.._.git_tag_tag-one_tag_tag-three b/t/t5515/fetch.br-unconfig_.._.git_tag_tag-one_tag_tag-three
new file mode 100644
index 0000000..7328d30
--- /dev/null
+++ b/t/t5515/fetch.br-unconfig_.._.git_tag_tag-one_tag_tag-three
@@ -0,0 +1,7 @@
+# br-unconfig ../.git tag tag-one tag tag-three
+8e32a6d901327a23ef831511badce7bf3bf46689 tag 'tag-one' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 tag 'tag-three' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-unconfig_branches-default b/t/t5515/fetch.br-unconfig_branches-default
new file mode 100644
index 0000000..3d82f94
--- /dev/null
+++ b/t/t5515/fetch.br-unconfig_branches-default
@@ -0,0 +1,8 @@
+# br-unconfig branches-default
+b4ab76b1a01ea602209932134a44f1e6bd610832 branch 'main' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-unconfig_branches-one b/t/t5515/fetch.br-unconfig_branches-one
new file mode 100644
index 0000000..948ed0c
--- /dev/null
+++ b/t/t5515/fetch.br-unconfig_branches-one
@@ -0,0 +1,8 @@
+# br-unconfig branches-one
+8e32a6d901327a23ef831511badce7bf3bf46689 branch 'one' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-unconfig_config-explicit b/t/t5515/fetch.br-unconfig_config-explicit
new file mode 100644
index 0000000..65aaec8
--- /dev/null
+++ b/t/t5515/fetch.br-unconfig_config-explicit
@@ -0,0 +1,11 @@
+# br-unconfig config-explicit
+b4ab76b1a01ea602209932134a44f1e6bd610832 branch 'main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge branch 'one' of ../
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 not-for-merge branch 'two' of ../
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b not-for-merge branch 'three' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-unconfig_config-glob b/t/t5515/fetch.br-unconfig_config-glob
new file mode 100644
index 0000000..1aa3d45
--- /dev/null
+++ b/t/t5515/fetch.br-unconfig_config-glob
@@ -0,0 +1,11 @@
+# br-unconfig config-glob
+b4ab76b1a01ea602209932134a44f1e6bd610832 not-for-merge branch 'main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge branch 'one' of ../
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b not-for-merge branch 'three' of ../
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 not-for-merge branch 'two' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-unconfig_remote-explicit b/t/t5515/fetch.br-unconfig_remote-explicit
new file mode 100644
index 0000000..16438d2
--- /dev/null
+++ b/t/t5515/fetch.br-unconfig_remote-explicit
@@ -0,0 +1,11 @@
+# br-unconfig remote-explicit
+b4ab76b1a01ea602209932134a44f1e6bd610832 branch 'main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge branch 'one' of ../
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 not-for-merge branch 'two' of ../
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b not-for-merge branch 'three' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.br-unconfig_remote-glob b/t/t5515/fetch.br-unconfig_remote-glob
new file mode 100644
index 0000000..7a01d2e
--- /dev/null
+++ b/t/t5515/fetch.br-unconfig_remote-glob
@@ -0,0 +1,11 @@
+# br-unconfig remote-glob
+b4ab76b1a01ea602209932134a44f1e6bd610832 not-for-merge branch 'main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge branch 'one' of ../
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b not-for-merge branch 'three' of ../
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 not-for-merge branch 'two' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.main b/t/t5515/fetch.main
new file mode 100644
index 0000000..819ed33
--- /dev/null
+++ b/t/t5515/fetch.main
@@ -0,0 +1,11 @@
+# main
+b4ab76b1a01ea602209932134a44f1e6bd610832 not-for-merge branch 'main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge branch 'one' of ../
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b not-for-merge branch 'three' of ../
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 not-for-merge branch 'two' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.main_--tags_.._.git b/t/t5515/fetch.main_--tags_.._.git
new file mode 100644
index 0000000..ddf67a6
--- /dev/null
+++ b/t/t5515/fetch.main_--tags_.._.git
@@ -0,0 +1,8 @@
+# main --tags ../.git
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.main_.._.git b/t/t5515/fetch.main_.._.git
new file mode 100644
index 0000000..285e57e
--- /dev/null
+++ b/t/t5515/fetch.main_.._.git
@@ -0,0 +1,2 @@
+# main ../.git
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b ../
diff --git a/t/t5515/fetch.main_.._.git_one b/t/t5515/fetch.main_.._.git_one
new file mode 100644
index 0000000..f9f511e
--- /dev/null
+++ b/t/t5515/fetch.main_.._.git_one
@@ -0,0 +1,2 @@
+# main ../.git one
+8e32a6d901327a23ef831511badce7bf3bf46689 branch 'one' of ../
diff --git a/t/t5515/fetch.main_.._.git_one_tag_tag-one_tag_tag-three-file b/t/t5515/fetch.main_.._.git_one_tag_tag-one_tag_tag-three-file
new file mode 100644
index 0000000..187643e
--- /dev/null
+++ b/t/t5515/fetch.main_.._.git_one_tag_tag-one_tag_tag-three-file
@@ -0,0 +1,8 @@
+# main ../.git one tag tag-one tag tag-three-file
+8e32a6d901327a23ef831511badce7bf3bf46689 branch 'one' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 tag 'tag-one' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.main_.._.git_one_two b/t/t5515/fetch.main_.._.git_one_two
new file mode 100644
index 0000000..048ad97
--- /dev/null
+++ b/t/t5515/fetch.main_.._.git_one_two
@@ -0,0 +1,3 @@
+# main ../.git one two
+8e32a6d901327a23ef831511badce7bf3bf46689 branch 'one' of ../
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 branch 'two' of ../
diff --git a/t/t5515/fetch.main_.._.git_tag_tag-one-tree_tag_tag-three-file b/t/t5515/fetch.main_.._.git_tag_tag-one-tree_tag_tag-three-file
new file mode 100644
index 0000000..df5f2a7
--- /dev/null
+++ b/t/t5515/fetch.main_.._.git_tag_tag-one-tree_tag_tag-three-file
@@ -0,0 +1,7 @@
+# main ../.git tag tag-one-tree tag tag-three-file
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.main_.._.git_tag_tag-one_tag_tag-three b/t/t5515/fetch.main_.._.git_tag_tag-one_tag_tag-three
new file mode 100644
index 0000000..a40b728
--- /dev/null
+++ b/t/t5515/fetch.main_.._.git_tag_tag-one_tag_tag-three
@@ -0,0 +1,7 @@
+# main ../.git tag tag-one tag tag-three
+8e32a6d901327a23ef831511badce7bf3bf46689 tag 'tag-one' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 tag 'tag-three' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.main_branches-default b/t/t5515/fetch.main_branches-default
new file mode 100644
index 0000000..e3466e8
--- /dev/null
+++ b/t/t5515/fetch.main_branches-default
@@ -0,0 +1,8 @@
+# main branches-default
+b4ab76b1a01ea602209932134a44f1e6bd610832 branch 'main' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.main_branches-one b/t/t5515/fetch.main_branches-one
new file mode 100644
index 0000000..a94f11a
--- /dev/null
+++ b/t/t5515/fetch.main_branches-one
@@ -0,0 +1,8 @@
+# main branches-one
+8e32a6d901327a23ef831511badce7bf3bf46689 branch 'one' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.main_config-explicit b/t/t5515/fetch.main_config-explicit
new file mode 100644
index 0000000..16d04c1
--- /dev/null
+++ b/t/t5515/fetch.main_config-explicit
@@ -0,0 +1,11 @@
+# main config-explicit
+b4ab76b1a01ea602209932134a44f1e6bd610832 branch 'main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge branch 'one' of ../
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 not-for-merge branch 'two' of ../
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b not-for-merge branch 'three' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.main_config-glob b/t/t5515/fetch.main_config-glob
new file mode 100644
index 0000000..f6a6f56
--- /dev/null
+++ b/t/t5515/fetch.main_config-glob
@@ -0,0 +1,11 @@
+# main config-glob
+b4ab76b1a01ea602209932134a44f1e6bd610832 not-for-merge branch 'main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge branch 'one' of ../
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b not-for-merge branch 'three' of ../
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 not-for-merge branch 'two' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.main_remote-explicit b/t/t5515/fetch.main_remote-explicit
new file mode 100644
index 0000000..bf4fd65
--- /dev/null
+++ b/t/t5515/fetch.main_remote-explicit
@@ -0,0 +1,11 @@
+# main remote-explicit
+b4ab76b1a01ea602209932134a44f1e6bd610832 branch 'main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge branch 'one' of ../
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 not-for-merge branch 'two' of ../
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b not-for-merge branch 'three' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/fetch.main_remote-glob b/t/t5515/fetch.main_remote-glob
new file mode 100644
index 0000000..91dc2e2
--- /dev/null
+++ b/t/t5515/fetch.main_remote-glob
@@ -0,0 +1,11 @@
+# main remote-glob
+b4ab76b1a01ea602209932134a44f1e6bd610832 not-for-merge branch 'main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge branch 'one' of ../
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b not-for-merge branch 'three' of ../
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 not-for-merge branch 'two' of ../
+ecf3b3627b498bdcb735cc4343bf165f76964e9a not-for-merge tag 'tag-main' of ../
+8e32a6d901327a23ef831511badce7bf3bf46689 not-for-merge tag 'tag-one' of ../
+22feea448b023a2d864ef94b013735af34d238ba not-for-merge tag 'tag-one-tree' of ../
+c61a82b60967180544e3c19f819ddbd0c9f89899 not-for-merge tag 'tag-three' of ../
+0e3b14047d3ee365f4f2a1b673db059c3972589c not-for-merge tag 'tag-three-file' of ../
+525b7fb068d59950d185a8779dc957c77eed73ba not-for-merge tag 'tag-two' of ../
diff --git a/t/t5515/refs.br-branches-default b/t/t5515/refs.br-branches-default
new file mode 100644
index 0000000..dc4f2b7
--- /dev/null
+++ b/t/t5515/refs.br-branches-default
@@ -0,0 +1,12 @@
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/heads/branches-default
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-branches-default-merge b/t/t5515/refs.br-branches-default-merge
new file mode 100644
index 0000000..dc4f2b7
--- /dev/null
+++ b/t/t5515/refs.br-branches-default-merge
@@ -0,0 +1,12 @@
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/heads/branches-default
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-branches-default-merge_branches-default b/t/t5515/refs.br-branches-default-merge_branches-default
new file mode 100644
index 0000000..dc4f2b7
--- /dev/null
+++ b/t/t5515/refs.br-branches-default-merge_branches-default
@@ -0,0 +1,12 @@
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/heads/branches-default
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-branches-default-octopus b/t/t5515/refs.br-branches-default-octopus
new file mode 100644
index 0000000..dc4f2b7
--- /dev/null
+++ b/t/t5515/refs.br-branches-default-octopus
@@ -0,0 +1,12 @@
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/heads/branches-default
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-branches-default-octopus_branches-default b/t/t5515/refs.br-branches-default-octopus_branches-default
new file mode 100644
index 0000000..dc4f2b7
--- /dev/null
+++ b/t/t5515/refs.br-branches-default-octopus_branches-default
@@ -0,0 +1,12 @@
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/heads/branches-default
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-branches-default_branches-default b/t/t5515/refs.br-branches-default_branches-default
new file mode 100644
index 0000000..dc4f2b7
--- /dev/null
+++ b/t/t5515/refs.br-branches-default_branches-default
@@ -0,0 +1,12 @@
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/heads/branches-default
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-branches-one b/t/t5515/refs.br-branches-one
new file mode 100644
index 0000000..e8f79bf
--- /dev/null
+++ b/t/t5515/refs.br-branches-one
@@ -0,0 +1,12 @@
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/heads/branches-one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-branches-one-merge b/t/t5515/refs.br-branches-one-merge
new file mode 100644
index 0000000..e8f79bf
--- /dev/null
+++ b/t/t5515/refs.br-branches-one-merge
@@ -0,0 +1,12 @@
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/heads/branches-one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-branches-one-merge_branches-one b/t/t5515/refs.br-branches-one-merge_branches-one
new file mode 100644
index 0000000..e8f79bf
--- /dev/null
+++ b/t/t5515/refs.br-branches-one-merge_branches-one
@@ -0,0 +1,12 @@
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/heads/branches-one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-branches-one-octopus b/t/t5515/refs.br-branches-one-octopus
new file mode 100644
index 0000000..e8f79bf
--- /dev/null
+++ b/t/t5515/refs.br-branches-one-octopus
@@ -0,0 +1,12 @@
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/heads/branches-one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-branches-one-octopus_branches-one b/t/t5515/refs.br-branches-one-octopus_branches-one
new file mode 100644
index 0000000..e8f79bf
--- /dev/null
+++ b/t/t5515/refs.br-branches-one-octopus_branches-one
@@ -0,0 +1,12 @@
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/heads/branches-one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-branches-one_branches-one b/t/t5515/refs.br-branches-one_branches-one
new file mode 100644
index 0000000..e8f79bf
--- /dev/null
+++ b/t/t5515/refs.br-branches-one_branches-one
@@ -0,0 +1,12 @@
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/heads/branches-one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-config-explicit b/t/t5515/refs.br-config-explicit
new file mode 100644
index 0000000..a28fa5f
--- /dev/null
+++ b/t/t5515/refs.br-config-explicit
@@ -0,0 +1,15 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/rem/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/rem/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/rem/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/rem/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-config-explicit-merge b/t/t5515/refs.br-config-explicit-merge
new file mode 100644
index 0000000..a28fa5f
--- /dev/null
+++ b/t/t5515/refs.br-config-explicit-merge
@@ -0,0 +1,15 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/rem/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/rem/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/rem/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/rem/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-config-explicit-merge_config-explicit b/t/t5515/refs.br-config-explicit-merge_config-explicit
new file mode 100644
index 0000000..a28fa5f
--- /dev/null
+++ b/t/t5515/refs.br-config-explicit-merge_config-explicit
@@ -0,0 +1,15 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/rem/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/rem/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/rem/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/rem/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-config-explicit-octopus b/t/t5515/refs.br-config-explicit-octopus
new file mode 100644
index 0000000..a28fa5f
--- /dev/null
+++ b/t/t5515/refs.br-config-explicit-octopus
@@ -0,0 +1,15 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/rem/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/rem/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/rem/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/rem/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-config-explicit-octopus_config-explicit b/t/t5515/refs.br-config-explicit-octopus_config-explicit
new file mode 100644
index 0000000..a28fa5f
--- /dev/null
+++ b/t/t5515/refs.br-config-explicit-octopus_config-explicit
@@ -0,0 +1,15 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/rem/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/rem/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/rem/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/rem/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-config-explicit_config-explicit b/t/t5515/refs.br-config-explicit_config-explicit
new file mode 100644
index 0000000..a28fa5f
--- /dev/null
+++ b/t/t5515/refs.br-config-explicit_config-explicit
@@ -0,0 +1,15 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/rem/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/rem/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/rem/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/rem/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-config-glob b/t/t5515/refs.br-config-glob
new file mode 100644
index 0000000..a28fa5f
--- /dev/null
+++ b/t/t5515/refs.br-config-glob
@@ -0,0 +1,15 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/rem/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/rem/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/rem/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/rem/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-config-glob-merge b/t/t5515/refs.br-config-glob-merge
new file mode 100644
index 0000000..a28fa5f
--- /dev/null
+++ b/t/t5515/refs.br-config-glob-merge
@@ -0,0 +1,15 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/rem/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/rem/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/rem/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/rem/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-config-glob-merge_config-glob b/t/t5515/refs.br-config-glob-merge_config-glob
new file mode 100644
index 0000000..a28fa5f
--- /dev/null
+++ b/t/t5515/refs.br-config-glob-merge_config-glob
@@ -0,0 +1,15 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/rem/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/rem/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/rem/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/rem/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-config-glob-octopus b/t/t5515/refs.br-config-glob-octopus
new file mode 100644
index 0000000..a28fa5f
--- /dev/null
+++ b/t/t5515/refs.br-config-glob-octopus
@@ -0,0 +1,15 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/rem/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/rem/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/rem/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/rem/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-config-glob-octopus_config-glob b/t/t5515/refs.br-config-glob-octopus_config-glob
new file mode 100644
index 0000000..a28fa5f
--- /dev/null
+++ b/t/t5515/refs.br-config-glob-octopus_config-glob
@@ -0,0 +1,15 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/rem/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/rem/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/rem/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/rem/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-config-glob_config-glob b/t/t5515/refs.br-config-glob_config-glob
new file mode 100644
index 0000000..a28fa5f
--- /dev/null
+++ b/t/t5515/refs.br-config-glob_config-glob
@@ -0,0 +1,15 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/rem/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/rem/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/rem/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/rem/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-remote-explicit b/t/t5515/refs.br-remote-explicit
new file mode 100644
index 0000000..a28fa5f
--- /dev/null
+++ b/t/t5515/refs.br-remote-explicit
@@ -0,0 +1,15 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/rem/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/rem/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/rem/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/rem/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-remote-explicit-merge b/t/t5515/refs.br-remote-explicit-merge
new file mode 100644
index 0000000..a28fa5f
--- /dev/null
+++ b/t/t5515/refs.br-remote-explicit-merge
@@ -0,0 +1,15 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/rem/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/rem/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/rem/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/rem/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-remote-explicit-merge_remote-explicit b/t/t5515/refs.br-remote-explicit-merge_remote-explicit
new file mode 100644
index 0000000..a28fa5f
--- /dev/null
+++ b/t/t5515/refs.br-remote-explicit-merge_remote-explicit
@@ -0,0 +1,15 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/rem/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/rem/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/rem/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/rem/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-remote-explicit-octopus b/t/t5515/refs.br-remote-explicit-octopus
new file mode 100644
index 0000000..a28fa5f
--- /dev/null
+++ b/t/t5515/refs.br-remote-explicit-octopus
@@ -0,0 +1,15 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/rem/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/rem/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/rem/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/rem/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-remote-explicit-octopus_remote-explicit b/t/t5515/refs.br-remote-explicit-octopus_remote-explicit
new file mode 100644
index 0000000..a28fa5f
--- /dev/null
+++ b/t/t5515/refs.br-remote-explicit-octopus_remote-explicit
@@ -0,0 +1,15 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/rem/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/rem/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/rem/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/rem/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-remote-explicit_remote-explicit b/t/t5515/refs.br-remote-explicit_remote-explicit
new file mode 100644
index 0000000..a28fa5f
--- /dev/null
+++ b/t/t5515/refs.br-remote-explicit_remote-explicit
@@ -0,0 +1,15 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/rem/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/rem/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/rem/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/rem/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-remote-glob b/t/t5515/refs.br-remote-glob
new file mode 100644
index 0000000..a28fa5f
--- /dev/null
+++ b/t/t5515/refs.br-remote-glob
@@ -0,0 +1,15 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/rem/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/rem/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/rem/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/rem/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-remote-glob-merge b/t/t5515/refs.br-remote-glob-merge
new file mode 100644
index 0000000..a28fa5f
--- /dev/null
+++ b/t/t5515/refs.br-remote-glob-merge
@@ -0,0 +1,15 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/rem/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/rem/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/rem/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/rem/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-remote-glob-merge_remote-glob b/t/t5515/refs.br-remote-glob-merge_remote-glob
new file mode 100644
index 0000000..a28fa5f
--- /dev/null
+++ b/t/t5515/refs.br-remote-glob-merge_remote-glob
@@ -0,0 +1,15 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/rem/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/rem/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/rem/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/rem/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-remote-glob-octopus b/t/t5515/refs.br-remote-glob-octopus
new file mode 100644
index 0000000..a28fa5f
--- /dev/null
+++ b/t/t5515/refs.br-remote-glob-octopus
@@ -0,0 +1,15 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/rem/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/rem/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/rem/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/rem/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-remote-glob-octopus_remote-glob b/t/t5515/refs.br-remote-glob-octopus_remote-glob
new file mode 100644
index 0000000..a28fa5f
--- /dev/null
+++ b/t/t5515/refs.br-remote-glob-octopus_remote-glob
@@ -0,0 +1,15 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/rem/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/rem/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/rem/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/rem/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-remote-glob_remote-glob b/t/t5515/refs.br-remote-glob_remote-glob
new file mode 100644
index 0000000..a28fa5f
--- /dev/null
+++ b/t/t5515/refs.br-remote-glob_remote-glob
@@ -0,0 +1,15 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/rem/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/rem/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/rem/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/rem/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-unconfig b/t/t5515/refs.br-unconfig
new file mode 100644
index 0000000..f2ab01f
--- /dev/null
+++ b/t/t5515/refs.br-unconfig
@@ -0,0 +1,11 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-unconfig_--tags_.._.git b/t/t5515/refs.br-unconfig_--tags_.._.git
new file mode 100644
index 0000000..f2ab01f
--- /dev/null
+++ b/t/t5515/refs.br-unconfig_--tags_.._.git
@@ -0,0 +1,11 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-unconfig_.._.git b/t/t5515/refs.br-unconfig_.._.git
new file mode 100644
index 0000000..4a74b68
--- /dev/null
+++ b/t/t5515/refs.br-unconfig_.._.git
@@ -0,0 +1,5 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
diff --git a/t/t5515/refs.br-unconfig_.._.git_one b/t/t5515/refs.br-unconfig_.._.git_one
new file mode 100644
index 0000000..4a74b68
--- /dev/null
+++ b/t/t5515/refs.br-unconfig_.._.git_one
@@ -0,0 +1,5 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
diff --git a/t/t5515/refs.br-unconfig_.._.git_one_tag_tag-one_tag_tag-three-file b/t/t5515/refs.br-unconfig_.._.git_one_tag_tag-one_tag_tag-three-file
new file mode 100644
index 0000000..f2ab01f
--- /dev/null
+++ b/t/t5515/refs.br-unconfig_.._.git_one_tag_tag-one_tag_tag-three-file
@@ -0,0 +1,11 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-unconfig_.._.git_one_two b/t/t5515/refs.br-unconfig_.._.git_one_two
new file mode 100644
index 0000000..4a74b68
--- /dev/null
+++ b/t/t5515/refs.br-unconfig_.._.git_one_two
@@ -0,0 +1,5 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
diff --git a/t/t5515/refs.br-unconfig_.._.git_tag_tag-one-tree_tag_tag-three-file b/t/t5515/refs.br-unconfig_.._.git_tag_tag-one-tree_tag_tag-three-file
new file mode 100644
index 0000000..f2ab01f
--- /dev/null
+++ b/t/t5515/refs.br-unconfig_.._.git_tag_tag-one-tree_tag_tag-three-file
@@ -0,0 +1,11 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-unconfig_.._.git_tag_tag-one_tag_tag-three b/t/t5515/refs.br-unconfig_.._.git_tag_tag-one_tag_tag-three
new file mode 100644
index 0000000..f2ab01f
--- /dev/null
+++ b/t/t5515/refs.br-unconfig_.._.git_tag_tag-one_tag_tag-three
@@ -0,0 +1,11 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-unconfig_branches-default b/t/t5515/refs.br-unconfig_branches-default
new file mode 100644
index 0000000..dc4f2b7
--- /dev/null
+++ b/t/t5515/refs.br-unconfig_branches-default
@@ -0,0 +1,12 @@
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/heads/branches-default
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-unconfig_branches-one b/t/t5515/refs.br-unconfig_branches-one
new file mode 100644
index 0000000..e8f79bf
--- /dev/null
+++ b/t/t5515/refs.br-unconfig_branches-one
@@ -0,0 +1,12 @@
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/heads/branches-one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-unconfig_config-explicit b/t/t5515/refs.br-unconfig_config-explicit
new file mode 100644
index 0000000..a28fa5f
--- /dev/null
+++ b/t/t5515/refs.br-unconfig_config-explicit
@@ -0,0 +1,15 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/rem/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/rem/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/rem/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/rem/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-unconfig_config-glob b/t/t5515/refs.br-unconfig_config-glob
new file mode 100644
index 0000000..a28fa5f
--- /dev/null
+++ b/t/t5515/refs.br-unconfig_config-glob
@@ -0,0 +1,15 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/rem/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/rem/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/rem/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/rem/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-unconfig_remote-explicit b/t/t5515/refs.br-unconfig_remote-explicit
new file mode 100644
index 0000000..a28fa5f
--- /dev/null
+++ b/t/t5515/refs.br-unconfig_remote-explicit
@@ -0,0 +1,15 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/rem/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/rem/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/rem/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/rem/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.br-unconfig_remote-glob b/t/t5515/refs.br-unconfig_remote-glob
new file mode 100644
index 0000000..a28fa5f
--- /dev/null
+++ b/t/t5515/refs.br-unconfig_remote-glob
@@ -0,0 +1,15 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/rem/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/rem/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/rem/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/rem/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.main b/t/t5515/refs.main
new file mode 100644
index 0000000..f2ab01f
--- /dev/null
+++ b/t/t5515/refs.main
@@ -0,0 +1,11 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.main_--tags_.._.git b/t/t5515/refs.main_--tags_.._.git
new file mode 100644
index 0000000..f2ab01f
--- /dev/null
+++ b/t/t5515/refs.main_--tags_.._.git
@@ -0,0 +1,11 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.main_.._.git b/t/t5515/refs.main_.._.git
new file mode 100644
index 0000000..4a74b68
--- /dev/null
+++ b/t/t5515/refs.main_.._.git
@@ -0,0 +1,5 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
diff --git a/t/t5515/refs.main_.._.git_one b/t/t5515/refs.main_.._.git_one
new file mode 100644
index 0000000..4a74b68
--- /dev/null
+++ b/t/t5515/refs.main_.._.git_one
@@ -0,0 +1,5 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
diff --git a/t/t5515/refs.main_.._.git_one_tag_tag-one_tag_tag-three-file b/t/t5515/refs.main_.._.git_one_tag_tag-one_tag_tag-three-file
new file mode 100644
index 0000000..f2ab01f
--- /dev/null
+++ b/t/t5515/refs.main_.._.git_one_tag_tag-one_tag_tag-three-file
@@ -0,0 +1,11 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.main_.._.git_one_two b/t/t5515/refs.main_.._.git_one_two
new file mode 100644
index 0000000..4a74b68
--- /dev/null
+++ b/t/t5515/refs.main_.._.git_one_two
@@ -0,0 +1,5 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
diff --git a/t/t5515/refs.main_.._.git_tag_tag-one-tree_tag_tag-three-file b/t/t5515/refs.main_.._.git_tag_tag-one-tree_tag_tag-three-file
new file mode 100644
index 0000000..f2ab01f
--- /dev/null
+++ b/t/t5515/refs.main_.._.git_tag_tag-one-tree_tag_tag-three-file
@@ -0,0 +1,11 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.main_.._.git_tag_tag-one_tag_tag-three b/t/t5515/refs.main_.._.git_tag_tag-one_tag_tag-three
new file mode 100644
index 0000000..f2ab01f
--- /dev/null
+++ b/t/t5515/refs.main_.._.git_tag_tag-one_tag_tag-three
@@ -0,0 +1,11 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.main_branches-default b/t/t5515/refs.main_branches-default
new file mode 100644
index 0000000..dc4f2b7
--- /dev/null
+++ b/t/t5515/refs.main_branches-default
@@ -0,0 +1,12 @@
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/heads/branches-default
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.main_branches-one b/t/t5515/refs.main_branches-one
new file mode 100644
index 0000000..e8f79bf
--- /dev/null
+++ b/t/t5515/refs.main_branches-one
@@ -0,0 +1,12 @@
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/heads/branches-one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.main_config-explicit b/t/t5515/refs.main_config-explicit
new file mode 100644
index 0000000..a28fa5f
--- /dev/null
+++ b/t/t5515/refs.main_config-explicit
@@ -0,0 +1,15 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/rem/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/rem/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/rem/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/rem/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.main_config-glob b/t/t5515/refs.main_config-glob
new file mode 100644
index 0000000..a28fa5f
--- /dev/null
+++ b/t/t5515/refs.main_config-glob
@@ -0,0 +1,15 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/rem/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/rem/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/rem/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/rem/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.main_remote-explicit b/t/t5515/refs.main_remote-explicit
new file mode 100644
index 0000000..a28fa5f
--- /dev/null
+++ b/t/t5515/refs.main_remote-explicit
@@ -0,0 +1,15 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/rem/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/rem/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/rem/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/rem/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5515/refs.main_remote-glob b/t/t5515/refs.main_remote-glob
new file mode 100644
index 0000000..a28fa5f
--- /dev/null
+++ b/t/t5515/refs.main_remote-glob
@@ -0,0 +1,15 @@
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/HEAD
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/origin/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/origin/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/origin/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/origin/two
+b4ab76b1a01ea602209932134a44f1e6bd610832 refs/remotes/rem/main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/remotes/rem/one
+0567da4d5edd2ff4bb292a465ba9e64dcad9536b refs/remotes/rem/three
+6134ee8f857693b96ff1cc98d3e2fd62b199e5a8 refs/remotes/rem/two
+ecf3b3627b498bdcb735cc4343bf165f76964e9a refs/tags/tag-main
+8e32a6d901327a23ef831511badce7bf3bf46689 refs/tags/tag-one
+22feea448b023a2d864ef94b013735af34d238ba refs/tags/tag-one-tree
+c61a82b60967180544e3c19f819ddbd0c9f89899 refs/tags/tag-three
+0e3b14047d3ee365f4f2a1b673db059c3972589c refs/tags/tag-three-file
+525b7fb068d59950d185a8779dc957c77eed73ba refs/tags/tag-two
diff --git a/t/t5516-fetch-push.sh b/t/t5516-fetch-push.sh
new file mode 100755
index 0000000..2e7c0e1
--- /dev/null
+++ b/t/t5516-fetch-push.sh
@@ -0,0 +1,1901 @@
+#!/bin/sh
+
+test_description='Basic fetch/push functionality.
+
+This test checks the following functionality:
+
+* command-line syntax
+* refspecs
+* fast-forward detection, and overriding it
+* configuration
+* hooks
+* --porcelain output format
+* hiderefs
+* reflogs
+* URL validation
+'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_CREATE_REPO_NO_TEMPLATE=1
+. ./test-lib.sh
+
+D=$(pwd)
+
+mk_empty () {
+ repo_name="$1"
+ test_when_finished "rm -rf \"$repo_name\"" &&
+ test_path_is_missing "$repo_name" &&
+ git init --template= "$repo_name" &&
+ mkdir "$repo_name"/.git/hooks &&
+ git -C "$repo_name" config receive.denyCurrentBranch warn
+}
+
+mk_test () {
+ repo_name="$1"
+ shift
+
+ mk_empty "$repo_name" &&
+ (
+ for ref in "$@"
+ do
+ git push "$repo_name" $the_first_commit:refs/$ref ||
+ exit
+ done &&
+ cd "$repo_name" &&
+ for ref in "$@"
+ do
+ echo "$the_first_commit" >expect &&
+ git show-ref -s --verify refs/$ref >actual &&
+ test_cmp expect actual ||
+ exit
+ done &&
+ git fsck --full
+ )
+}
+
+mk_test_with_hooks() {
+ repo_name=$1
+ mk_test "$@" &&
+ test_hook -C "$repo_name" pre-receive <<-'EOF' &&
+ cat - >>pre-receive.actual
+ EOF
+
+ test_hook -C "$repo_name" update <<-'EOF' &&
+ printf "%s %s %s\n" "$@" >>update.actual
+ EOF
+
+ test_hook -C "$repo_name" post-receive <<-'EOF' &&
+ cat - >>post-receive.actual
+ EOF
+
+ test_hook -C "$repo_name" post-update <<-'EOF'
+ for ref in "$@"
+ do
+ printf "%s\n" "$ref" >>post-update.actual
+ done
+ EOF
+}
+
+mk_child() {
+ test_when_finished "rm -rf \"$2\"" &&
+ git clone --template= "$1" "$2"
+}
+
+check_push_result () {
+ test $# -ge 3 ||
+ BUG "check_push_result requires at least 3 parameters"
+
+ repo_name="$1"
+ shift
+
+ (
+ cd "$repo_name" &&
+ echo "$1" >expect &&
+ shift &&
+ for ref in "$@"
+ do
+ git show-ref -s --verify refs/$ref >actual &&
+ test_cmp expect actual ||
+ exit
+ done &&
+ git fsck --full
+ )
+}
+
+test_expect_success setup '
+
+ >path1 &&
+ git add path1 &&
+ test_tick &&
+ git commit -a -m repo &&
+ the_first_commit=$(git show-ref -s --verify refs/heads/main) &&
+
+ >path2 &&
+ git add path2 &&
+ test_tick &&
+ git commit -a -m second &&
+ the_commit=$(git show-ref -s --verify refs/heads/main)
+
+'
+
+for cmd in push fetch
+do
+ for opt in ipv4 ipv6
+ do
+ test_expect_success "reject 'git $cmd --no-$opt'" '
+ test_must_fail git $cmd --no-$opt 2>err &&
+ grep "unknown option .no-$opt" err
+ '
+ done
+done
+
+test_expect_success 'fetch without wildcard' '
+ mk_empty testrepo &&
+ (
+ cd testrepo &&
+ git fetch .. refs/heads/main:refs/remotes/origin/main &&
+
+ echo "$the_commit commit refs/remotes/origin/main" >expect &&
+ git for-each-ref refs/remotes/origin >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'fetch with wildcard' '
+ mk_empty testrepo &&
+ (
+ cd testrepo &&
+ git config remote.up.url .. &&
+ git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
+ git fetch up &&
+
+ echo "$the_commit commit refs/remotes/origin/main" >expect &&
+ git for-each-ref refs/remotes/origin >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'fetch with insteadOf' '
+ mk_empty testrepo &&
+ (
+ TRASH=$(pwd)/ &&
+ cd testrepo &&
+ git config "url.$TRASH.insteadOf" trash/ &&
+ git config remote.up.url trash/. &&
+ git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
+ git fetch up &&
+
+ echo "$the_commit commit refs/remotes/origin/main" >expect &&
+ git for-each-ref refs/remotes/origin >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'fetch with pushInsteadOf (should not rewrite)' '
+ mk_empty testrepo &&
+ (
+ TRASH=$(pwd)/ &&
+ cd testrepo &&
+ git config "url.trash/.pushInsteadOf" "$TRASH" &&
+ git config remote.up.url "$TRASH." &&
+ git config remote.up.fetch "refs/heads/*:refs/remotes/origin/*" &&
+ git fetch up &&
+
+ echo "$the_commit commit refs/remotes/origin/main" >expect &&
+ git for-each-ref refs/remotes/origin >actual &&
+ test_cmp expect actual
+ )
+'
+
+grep_wrote () {
+ object_count=$1
+ file_name=$2
+ grep 'write_pack_file/wrote.*"value":"'$1'"' $2
+}
+
+test_expect_success 'push without negotiation' '
+ mk_empty testrepo &&
+ git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
+ test_commit -C testrepo unrelated_commit &&
+ git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
+ test_when_finished "rm event" &&
+ GIT_TRACE2_EVENT="$(pwd)/event" git -c protocol.version=2 push testrepo refs/heads/main:refs/remotes/origin/main &&
+ grep_wrote 5 event # 2 commits, 2 trees, 1 blob
+'
+
+test_expect_success 'push with negotiation' '
+ mk_empty testrepo &&
+ git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
+ test_commit -C testrepo unrelated_commit &&
+ git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
+ test_when_finished "rm event" &&
+ GIT_TRACE2_EVENT="$(pwd)/event" \
+ git -c protocol.version=2 -c push.negotiate=1 \
+ push testrepo refs/heads/main:refs/remotes/origin/main &&
+ grep \"key\":\"total_rounds\",\"value\":\"1\" event &&
+ grep_wrote 2 event # 1 commit, 1 tree
+'
+
+test_expect_success 'push with negotiation proceeds anyway even if negotiation fails' '
+ mk_empty testrepo &&
+ git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
+ test_commit -C testrepo unrelated_commit &&
+ git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
+ test_when_finished "rm event" &&
+ GIT_TEST_PROTOCOL_VERSION=0 GIT_TRACE2_EVENT="$(pwd)/event" \
+ git -c push.negotiate=1 push testrepo refs/heads/main:refs/remotes/origin/main 2>err &&
+ grep_wrote 5 event && # 2 commits, 2 trees, 1 blob
+ test_grep "push negotiation failed" err
+'
+
+test_expect_success 'push with negotiation does not attempt to fetch submodules' '
+ mk_empty submodule_upstream &&
+ test_commit -C submodule_upstream submodule_commit &&
+ test_config_global protocol.file.allow always &&
+ git submodule add ./submodule_upstream submodule &&
+ mk_empty testrepo &&
+ git push testrepo $the_first_commit:refs/remotes/origin/first_commit &&
+ test_commit -C testrepo unrelated_commit &&
+ git -C testrepo config receive.hideRefs refs/remotes/origin/first_commit &&
+ GIT_TRACE2_EVENT="$(pwd)/event" git -c submodule.recurse=true \
+ -c protocol.version=2 -c push.negotiate=1 \
+ push testrepo refs/heads/main:refs/remotes/origin/main 2>err &&
+ grep \"key\":\"total_rounds\",\"value\":\"1\" event &&
+ ! grep "Fetching submodule" err
+'
+
+test_expect_success 'push without wildcard' '
+ mk_empty testrepo &&
+
+ git push testrepo refs/heads/main:refs/remotes/origin/main &&
+ (
+ cd testrepo &&
+ echo "$the_commit commit refs/remotes/origin/main" >expect &&
+ git for-each-ref refs/remotes/origin >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'push with wildcard' '
+ mk_empty testrepo &&
+
+ git push testrepo "refs/heads/*:refs/remotes/origin/*" &&
+ (
+ cd testrepo &&
+ echo "$the_commit commit refs/remotes/origin/main" >expect &&
+ git for-each-ref refs/remotes/origin >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'push with insteadOf' '
+ mk_empty testrepo &&
+ TRASH="$(pwd)/" &&
+ test_config "url.$TRASH.insteadOf" trash/ &&
+ git push trash/testrepo refs/heads/main:refs/remotes/origin/main &&
+ (
+ cd testrepo &&
+ echo "$the_commit commit refs/remotes/origin/main" >expect &&
+ git for-each-ref refs/remotes/origin >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'push with pushInsteadOf' '
+ mk_empty testrepo &&
+ TRASH="$(pwd)/" &&
+ test_config "url.$TRASH.pushInsteadOf" trash/ &&
+ git push trash/testrepo refs/heads/main:refs/remotes/origin/main &&
+ (
+ cd testrepo &&
+ echo "$the_commit commit refs/remotes/origin/main" >expect &&
+ git for-each-ref refs/remotes/origin >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'push with pushInsteadOf and explicit pushurl (pushInsteadOf should not rewrite)' '
+ mk_empty testrepo &&
+ test_config "url.trash2/.pushInsteadOf" testrepo/ &&
+ test_config "url.trash3/.pushInsteadOf" trash/wrong &&
+ test_config remote.r.url trash/wrong &&
+ test_config remote.r.pushurl "testrepo/" &&
+ git push r refs/heads/main:refs/remotes/origin/main &&
+ (
+ cd testrepo &&
+ echo "$the_commit commit refs/remotes/origin/main" >expect &&
+ git for-each-ref refs/remotes/origin >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'push with matching heads' '
+
+ mk_test testrepo heads/main &&
+ git push testrepo : &&
+ check_push_result testrepo $the_commit heads/main
+
+'
+
+test_expect_success 'push with matching heads on the command line' '
+
+ mk_test testrepo heads/main &&
+ git push testrepo : &&
+ check_push_result testrepo $the_commit heads/main
+
+'
+
+test_expect_success 'failed (non-fast-forward) push with matching heads' '
+
+ mk_test testrepo heads/main &&
+ git push testrepo : &&
+ git commit --amend -massaged &&
+ test_must_fail git push testrepo &&
+ check_push_result testrepo $the_commit heads/main &&
+ git reset --hard $the_commit
+
+'
+
+test_expect_success 'push --force with matching heads' '
+
+ mk_test testrepo heads/main &&
+ git push testrepo : &&
+ git commit --amend -massaged &&
+ git push --force testrepo : &&
+ ! check_push_result testrepo $the_commit heads/main &&
+ git reset --hard $the_commit
+
+'
+
+test_expect_success 'push with matching heads and forced update' '
+
+ mk_test testrepo heads/main &&
+ git push testrepo : &&
+ git commit --amend -massaged &&
+ git push testrepo +: &&
+ ! check_push_result testrepo $the_commit heads/main &&
+ git reset --hard $the_commit
+
+'
+
+test_expect_success 'push with no ambiguity (1)' '
+
+ mk_test testrepo heads/main &&
+ git push testrepo main:main &&
+ check_push_result testrepo $the_commit heads/main
+
+'
+
+test_expect_success 'push with no ambiguity (2)' '
+
+ mk_test testrepo remotes/origin/main &&
+ git push testrepo main:origin/main &&
+ check_push_result testrepo $the_commit remotes/origin/main
+
+'
+
+test_expect_success 'push with colon-less refspec, no ambiguity' '
+
+ mk_test testrepo heads/main heads/t/main &&
+ git branch -f t/main main &&
+ git push testrepo main &&
+ check_push_result testrepo $the_commit heads/main &&
+ check_push_result testrepo $the_first_commit heads/t/main
+
+'
+
+test_expect_success 'push with weak ambiguity (1)' '
+
+ mk_test testrepo heads/main remotes/origin/main &&
+ git push testrepo main:main &&
+ check_push_result testrepo $the_commit heads/main &&
+ check_push_result testrepo $the_first_commit remotes/origin/main
+
+'
+
+test_expect_success 'push with weak ambiguity (2)' '
+
+ mk_test testrepo heads/main remotes/origin/main remotes/another/main &&
+ git push testrepo main:main &&
+ check_push_result testrepo $the_commit heads/main &&
+ check_push_result testrepo $the_first_commit remotes/origin/main remotes/another/main
+
+'
+
+test_expect_success 'push with ambiguity' '
+
+ mk_test testrepo heads/frotz tags/frotz &&
+ test_must_fail git push testrepo main:frotz &&
+ check_push_result testrepo $the_first_commit heads/frotz tags/frotz
+
+'
+
+test_expect_success 'push with onelevel ref' '
+ mk_test testrepo heads/main &&
+ test_must_fail git push testrepo HEAD:refs/onelevel
+'
+
+test_expect_success 'push with colon-less refspec (1)' '
+
+ mk_test testrepo heads/frotz tags/frotz &&
+ git branch -f frotz main &&
+ git push testrepo frotz &&
+ check_push_result testrepo $the_commit heads/frotz &&
+ check_push_result testrepo $the_first_commit tags/frotz
+
+'
+
+test_expect_success 'push with colon-less refspec (2)' '
+
+ mk_test testrepo heads/frotz tags/frotz &&
+ if git show-ref --verify -q refs/heads/frotz
+ then
+ git branch -D frotz
+ fi &&
+ git tag -f frotz &&
+ git push -f testrepo frotz &&
+ check_push_result testrepo $the_commit tags/frotz &&
+ check_push_result testrepo $the_first_commit heads/frotz
+
+'
+
+test_expect_success 'push with colon-less refspec (3)' '
+
+ mk_test testrepo &&
+ if git show-ref --verify -q refs/tags/frotz
+ then
+ git tag -d frotz
+ fi &&
+ git branch -f frotz main &&
+ git push testrepo frotz &&
+ check_push_result testrepo $the_commit heads/frotz &&
+ test 1 = $( cd testrepo && git show-ref | wc -l )
+'
+
+test_expect_success 'push with colon-less refspec (4)' '
+
+ mk_test testrepo &&
+ if git show-ref --verify -q refs/heads/frotz
+ then
+ git branch -D frotz
+ fi &&
+ git tag -f frotz &&
+ git push testrepo frotz &&
+ check_push_result testrepo $the_commit tags/frotz &&
+ test 1 = $( cd testrepo && git show-ref | wc -l )
+
+'
+
+test_expect_success 'push head with non-existent, incomplete dest' '
+
+ mk_test testrepo &&
+ git push testrepo main:branch &&
+ check_push_result testrepo $the_commit heads/branch
+
+'
+
+test_expect_success 'push tag with non-existent, incomplete dest' '
+
+ mk_test testrepo &&
+ git tag -f v1.0 &&
+ git push testrepo v1.0:tag &&
+ check_push_result testrepo $the_commit tags/tag
+
+'
+
+test_expect_success 'push sha1 with non-existent, incomplete dest' '
+
+ mk_test testrepo &&
+ test_must_fail git push testrepo $(git rev-parse main):foo
+
+'
+
+test_expect_success 'push ref expression with non-existent, incomplete dest' '
+
+ mk_test testrepo &&
+ test_must_fail git push testrepo main^:branch
+
+'
+
+for head in HEAD @
+do
+
+ test_expect_success "push with $head" '
+ mk_test testrepo heads/main &&
+ git checkout main &&
+ git push testrepo $head &&
+ check_push_result testrepo $the_commit heads/main
+ '
+
+ test_expect_success "push with $head nonexisting at remote" '
+ mk_test testrepo heads/main &&
+ git checkout -b local main &&
+ test_when_finished "git checkout main; git branch -D local" &&
+ git push testrepo $head &&
+ check_push_result testrepo $the_commit heads/local
+ '
+
+ test_expect_success "push with +$head" '
+ mk_test testrepo heads/main &&
+ git checkout -b local main &&
+ test_when_finished "git checkout main; git branch -D local" &&
+ git push testrepo main local &&
+ check_push_result testrepo $the_commit heads/main &&
+ check_push_result testrepo $the_commit heads/local &&
+
+ # Without force rewinding should fail
+ git reset --hard $head^ &&
+ test_must_fail git push testrepo $head &&
+ check_push_result testrepo $the_commit heads/local &&
+
+ # With force rewinding should succeed
+ git push testrepo +$head &&
+ check_push_result testrepo $the_first_commit heads/local
+ '
+
+ test_expect_success "push $head with non-existent, incomplete dest" '
+ mk_test testrepo &&
+ git checkout main &&
+ git push testrepo $head:branch &&
+ check_push_result testrepo $the_commit heads/branch
+
+ '
+
+ test_expect_success "push with config remote.*.push = $head" '
+ mk_test testrepo heads/local &&
+ git checkout main &&
+ git branch -f local $the_commit &&
+ test_when_finished "git branch -D local" &&
+ (
+ cd testrepo &&
+ git checkout local &&
+ git reset --hard $the_first_commit
+ ) &&
+ test_config remote.there.url testrepo &&
+ test_config remote.there.push $head &&
+ test_config branch.main.remote there &&
+ git push &&
+ check_push_result testrepo $the_commit heads/main &&
+ check_push_result testrepo $the_first_commit heads/local
+ '
+
+done
+
+test_expect_success "push to remote with no explicit refspec and config remote.*.push = src:dest" '
+ mk_test testrepo heads/main &&
+ git checkout $the_first_commit &&
+ test_config remote.there.url testrepo &&
+ test_config remote.there.push refs/heads/main:refs/heads/main &&
+ git push there &&
+ check_push_result testrepo $the_commit heads/main
+'
+
+test_expect_success 'push with remote.pushdefault' '
+ mk_test up_repo heads/main &&
+ mk_test down_repo heads/main &&
+ test_config remote.up.url up_repo &&
+ test_config remote.down.url down_repo &&
+ test_config branch.main.remote up &&
+ test_config remote.pushdefault down &&
+ test_config push.default matching &&
+ git push &&
+ check_push_result up_repo $the_first_commit heads/main &&
+ check_push_result down_repo $the_commit heads/main
+'
+
+test_expect_success 'push with config remote.*.pushurl' '
+
+ mk_test testrepo heads/main &&
+ git checkout main &&
+ test_config remote.there.url test2repo &&
+ test_config remote.there.pushurl testrepo &&
+ git push there : &&
+ check_push_result testrepo $the_commit heads/main
+'
+
+test_expect_success 'push with config branch.*.pushremote' '
+ mk_test up_repo heads/main &&
+ mk_test side_repo heads/main &&
+ mk_test down_repo heads/main &&
+ test_config remote.up.url up_repo &&
+ test_config remote.pushdefault side_repo &&
+ test_config remote.down.url down_repo &&
+ test_config branch.main.remote up &&
+ test_config branch.main.pushremote down &&
+ test_config push.default matching &&
+ git push &&
+ check_push_result up_repo $the_first_commit heads/main &&
+ check_push_result side_repo $the_first_commit heads/main &&
+ check_push_result down_repo $the_commit heads/main
+'
+
+test_expect_success 'branch.*.pushremote config order is irrelevant' '
+ mk_test one_repo heads/main &&
+ mk_test two_repo heads/main &&
+ test_config remote.one.url one_repo &&
+ test_config remote.two.url two_repo &&
+ test_config branch.main.pushremote two_repo &&
+ test_config remote.pushdefault one_repo &&
+ test_config push.default matching &&
+ git push &&
+ check_push_result one_repo $the_first_commit heads/main &&
+ check_push_result two_repo $the_commit heads/main
+'
+
+test_expect_success 'push rejects empty branch name entries' '
+ mk_test one_repo heads/main &&
+ test_config remote.one.url one_repo &&
+ test_config branch..remote one &&
+ test_config branch..merge refs/heads/ &&
+ test_config branch.main.remote one &&
+ test_config branch.main.merge refs/heads/main &&
+ test_must_fail git push 2>err &&
+ grep "bad config variable .branch\.\." err
+'
+
+test_expect_success 'push ignores "branch." config without subsection' '
+ mk_test one_repo heads/main &&
+ test_config remote.one.url one_repo &&
+ test_config branch.autoSetupMerge true &&
+ test_config branch.main.remote one &&
+ test_config branch.main.merge refs/heads/main &&
+ git push
+'
+
+test_expect_success 'push with dry-run' '
+
+ mk_test testrepo heads/main &&
+ old_commit=$(git -C testrepo show-ref -s --verify refs/heads/main) &&
+ git push --dry-run testrepo : &&
+ check_push_result testrepo $old_commit heads/main
+'
+
+test_expect_success 'push updates local refs' '
+
+ mk_test testrepo heads/main &&
+ mk_child testrepo child &&
+ (
+ cd child &&
+ git pull .. main &&
+ git push &&
+ test $(git rev-parse main) = \
+ $(git rev-parse remotes/origin/main)
+ )
+
+'
+
+test_expect_success 'push updates up-to-date local refs' '
+
+ mk_test testrepo heads/main &&
+ mk_child testrepo child1 &&
+ mk_child testrepo child2 &&
+ (cd child1 && git pull .. main && git push) &&
+ (
+ cd child2 &&
+ git pull ../child1 main &&
+ git push &&
+ test $(git rev-parse main) = \
+ $(git rev-parse remotes/origin/main)
+ )
+
+'
+
+test_expect_success 'push preserves up-to-date packed refs' '
+
+ mk_test testrepo heads/main &&
+ mk_child testrepo child &&
+ (
+ cd child &&
+ git push &&
+ ! test -f .git/refs/remotes/origin/main
+ )
+
+'
+
+test_expect_success 'push does not update local refs on failure' '
+
+ mk_test testrepo heads/main &&
+ mk_child testrepo child &&
+ echo "#!/no/frobnication/today" >testrepo/.git/hooks/pre-receive &&
+ chmod +x testrepo/.git/hooks/pre-receive &&
+ (
+ cd child &&
+ git pull .. main &&
+ test_must_fail git push &&
+ test $(git rev-parse main) != \
+ $(git rev-parse remotes/origin/main)
+ )
+
+'
+
+test_expect_success 'allow deleting an invalid remote ref' '
+
+ mk_test testrepo heads/branch &&
+ rm -f testrepo/.git/objects/??/* &&
+ git push testrepo :refs/heads/branch &&
+ (cd testrepo && test_must_fail git rev-parse --verify refs/heads/branch)
+
+'
+
+test_expect_success 'pushing valid refs triggers post-receive and post-update hooks' '
+ mk_test_with_hooks testrepo heads/main heads/next &&
+ orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
+ newmain=$(git show-ref -s --verify refs/heads/main) &&
+ orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
+ newnext=$ZERO_OID &&
+ git push testrepo refs/heads/main:refs/heads/main :refs/heads/next &&
+ (
+ cd testrepo/.git &&
+ cat >pre-receive.expect <<-EOF &&
+ $orgmain $newmain refs/heads/main
+ $orgnext $newnext refs/heads/next
+ EOF
+
+ cat >update.expect <<-EOF &&
+ refs/heads/main $orgmain $newmain
+ refs/heads/next $orgnext $newnext
+ EOF
+
+ cat >post-receive.expect <<-EOF &&
+ $orgmain $newmain refs/heads/main
+ $orgnext $newnext refs/heads/next
+ EOF
+
+ cat >post-update.expect <<-EOF &&
+ refs/heads/main
+ refs/heads/next
+ EOF
+
+ test_cmp pre-receive.expect pre-receive.actual &&
+ test_cmp update.expect update.actual &&
+ test_cmp post-receive.expect post-receive.actual &&
+ test_cmp post-update.expect post-update.actual
+ )
+'
+
+test_expect_success 'deleting dangling ref triggers hooks with correct args' '
+ mk_test_with_hooks testrepo heads/branch &&
+ orig=$(git -C testrepo rev-parse refs/heads/branch) &&
+ rm -f testrepo/.git/objects/??/* &&
+ git push testrepo :refs/heads/branch &&
+ (
+ cd testrepo/.git &&
+ cat >pre-receive.expect <<-EOF &&
+ $orig $ZERO_OID refs/heads/branch
+ EOF
+
+ cat >update.expect <<-EOF &&
+ refs/heads/branch $orig $ZERO_OID
+ EOF
+
+ cat >post-receive.expect <<-EOF &&
+ $orig $ZERO_OID refs/heads/branch
+ EOF
+
+ cat >post-update.expect <<-EOF &&
+ refs/heads/branch
+ EOF
+
+ test_cmp pre-receive.expect pre-receive.actual &&
+ test_cmp update.expect update.actual &&
+ test_cmp post-receive.expect post-receive.actual &&
+ test_cmp post-update.expect post-update.actual
+ )
+'
+
+test_expect_success 'deletion of a non-existent ref is not fed to post-receive and post-update hooks' '
+ mk_test_with_hooks testrepo heads/main &&
+ orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
+ newmain=$(git show-ref -s --verify refs/heads/main) &&
+ git push testrepo main :refs/heads/nonexistent &&
+ (
+ cd testrepo/.git &&
+ cat >pre-receive.expect <<-EOF &&
+ $orgmain $newmain refs/heads/main
+ $ZERO_OID $ZERO_OID refs/heads/nonexistent
+ EOF
+
+ cat >update.expect <<-EOF &&
+ refs/heads/main $orgmain $newmain
+ refs/heads/nonexistent $ZERO_OID $ZERO_OID
+ EOF
+
+ cat >post-receive.expect <<-EOF &&
+ $orgmain $newmain refs/heads/main
+ EOF
+
+ cat >post-update.expect <<-EOF &&
+ refs/heads/main
+ EOF
+
+ test_cmp pre-receive.expect pre-receive.actual &&
+ test_cmp update.expect update.actual &&
+ test_cmp post-receive.expect post-receive.actual &&
+ test_cmp post-update.expect post-update.actual
+ )
+'
+
+test_expect_success 'deletion of a non-existent ref alone does trigger post-receive and post-update hooks' '
+ mk_test_with_hooks testrepo heads/main &&
+ git push testrepo :refs/heads/nonexistent &&
+ (
+ cd testrepo/.git &&
+ cat >pre-receive.expect <<-EOF &&
+ $ZERO_OID $ZERO_OID refs/heads/nonexistent
+ EOF
+
+ cat >update.expect <<-EOF &&
+ refs/heads/nonexistent $ZERO_OID $ZERO_OID
+ EOF
+
+ test_cmp pre-receive.expect pre-receive.actual &&
+ test_cmp update.expect update.actual &&
+ test_path_is_missing post-receive.actual &&
+ test_path_is_missing post-update.actual
+ )
+'
+
+test_expect_success 'mixed ref updates, deletes, invalid deletes trigger hooks with correct input' '
+ mk_test_with_hooks testrepo heads/main heads/next heads/seen &&
+ orgmain=$(cd testrepo && git show-ref -s --verify refs/heads/main) &&
+ newmain=$(git show-ref -s --verify refs/heads/main) &&
+ orgnext=$(cd testrepo && git show-ref -s --verify refs/heads/next) &&
+ newnext=$ZERO_OID &&
+ orgseen=$(cd testrepo && git show-ref -s --verify refs/heads/seen) &&
+ newseen=$(git show-ref -s --verify refs/heads/main) &&
+ git push testrepo refs/heads/main:refs/heads/main \
+ refs/heads/main:refs/heads/seen :refs/heads/next \
+ :refs/heads/nonexistent &&
+ (
+ cd testrepo/.git &&
+ cat >pre-receive.expect <<-EOF &&
+ $orgmain $newmain refs/heads/main
+ $orgnext $newnext refs/heads/next
+ $orgseen $newseen refs/heads/seen
+ $ZERO_OID $ZERO_OID refs/heads/nonexistent
+ EOF
+
+ cat >update.expect <<-EOF &&
+ refs/heads/main $orgmain $newmain
+ refs/heads/next $orgnext $newnext
+ refs/heads/seen $orgseen $newseen
+ refs/heads/nonexistent $ZERO_OID $ZERO_OID
+ EOF
+
+ cat >post-receive.expect <<-EOF &&
+ $orgmain $newmain refs/heads/main
+ $orgnext $newnext refs/heads/next
+ $orgseen $newseen refs/heads/seen
+ EOF
+
+ cat >post-update.expect <<-EOF &&
+ refs/heads/main
+ refs/heads/next
+ refs/heads/seen
+ EOF
+
+ test_cmp pre-receive.expect pre-receive.actual &&
+ test_cmp update.expect update.actual &&
+ test_cmp post-receive.expect post-receive.actual &&
+ test_cmp post-update.expect post-update.actual
+ )
+'
+
+test_expect_success 'allow deleting a ref using --delete' '
+ mk_test testrepo heads/main &&
+ (cd testrepo && git config receive.denyDeleteCurrent warn) &&
+ git push testrepo --delete main &&
+ (cd testrepo && test_must_fail git rev-parse --verify refs/heads/main)
+'
+
+test_expect_success 'allow deleting a tag using --delete' '
+ mk_test testrepo heads/main &&
+ git tag -a -m dummy_message deltag heads/main &&
+ git push testrepo --tags &&
+ (cd testrepo && git rev-parse --verify -q refs/tags/deltag) &&
+ git push testrepo --delete tag deltag &&
+ (cd testrepo && test_must_fail git rev-parse --verify refs/tags/deltag)
+'
+
+test_expect_success 'push --delete without args aborts' '
+ mk_test testrepo heads/main &&
+ test_must_fail git push testrepo --delete
+'
+
+test_expect_success 'push --delete refuses src:dest refspecs' '
+ mk_test testrepo heads/main &&
+ test_must_fail git push testrepo --delete main:foo
+'
+
+test_expect_success 'push --delete refuses empty string' '
+ mk_test testrepo heads/master &&
+ test_must_fail git push testrepo --delete ""
+'
+
+test_expect_success 'push --delete onelevel refspecs' '
+ mk_test testrepo heads/main &&
+ git -C testrepo update-ref refs/onelevel refs/heads/main &&
+ git push testrepo --delete refs/onelevel &&
+ test_must_fail git -C testrepo rev-parse --verify refs/onelevel
+'
+
+test_expect_success 'warn on push to HEAD of non-bare repository' '
+ mk_test testrepo heads/main &&
+ (
+ cd testrepo &&
+ git checkout main &&
+ git config receive.denyCurrentBranch warn
+ ) &&
+ git push testrepo main 2>stderr &&
+ grep "warning: updating the current branch" stderr
+'
+
+test_expect_success 'deny push to HEAD of non-bare repository' '
+ mk_test testrepo heads/main &&
+ (
+ cd testrepo &&
+ git checkout main &&
+ git config receive.denyCurrentBranch true
+ ) &&
+ test_must_fail git push testrepo main
+'
+
+test_expect_success 'allow push to HEAD of bare repository (bare)' '
+ mk_test testrepo heads/main &&
+ (
+ cd testrepo &&
+ git checkout main &&
+ git config receive.denyCurrentBranch true &&
+ git config core.bare true
+ ) &&
+ git push testrepo main 2>stderr &&
+ ! grep "warning: updating the current branch" stderr
+'
+
+test_expect_success 'allow push to HEAD of non-bare repository (config)' '
+ mk_test testrepo heads/main &&
+ (
+ cd testrepo &&
+ git checkout main &&
+ git config receive.denyCurrentBranch false
+ ) &&
+ git push testrepo main 2>stderr &&
+ ! grep "warning: updating the current branch" stderr
+'
+
+test_expect_success 'fetch with branches' '
+ mk_empty testrepo &&
+ git branch second $the_first_commit &&
+ git checkout second &&
+ mkdir testrepo/.git/branches &&
+ echo ".." > testrepo/.git/branches/branch1 &&
+ (
+ cd testrepo &&
+ git fetch branch1 &&
+ echo "$the_commit commit refs/heads/branch1" >expect &&
+ git for-each-ref refs/heads >actual &&
+ test_cmp expect actual
+ ) &&
+ git checkout main
+'
+
+test_expect_success 'fetch with branches containing #' '
+ mk_empty testrepo &&
+ mkdir testrepo/.git/branches &&
+ echo "..#second" > testrepo/.git/branches/branch2 &&
+ (
+ cd testrepo &&
+ git fetch branch2 &&
+ echo "$the_first_commit commit refs/heads/branch2" >expect &&
+ git for-each-ref refs/heads >actual &&
+ test_cmp expect actual
+ ) &&
+ git checkout main
+'
+
+test_expect_success 'push with branches' '
+ mk_empty testrepo &&
+ git checkout second &&
+
+ test_when_finished "rm -rf .git/branches" &&
+ mkdir .git/branches &&
+ echo "testrepo" > .git/branches/branch1 &&
+
+ git push branch1 &&
+ (
+ cd testrepo &&
+ echo "$the_first_commit commit refs/heads/main" >expect &&
+ git for-each-ref refs/heads >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'push with branches containing #' '
+ mk_empty testrepo &&
+
+ test_when_finished "rm -rf .git/branches" &&
+ mkdir .git/branches &&
+ echo "testrepo#branch3" > .git/branches/branch2 &&
+
+ git push branch2 &&
+ (
+ cd testrepo &&
+ echo "$the_first_commit commit refs/heads/branch3" >expect &&
+ git for-each-ref refs/heads >actual &&
+ test_cmp expect actual
+ ) &&
+ git checkout main
+'
+
+test_expect_success 'push into aliased refs (consistent)' '
+ mk_test testrepo heads/main &&
+ mk_child testrepo child1 &&
+ mk_child testrepo child2 &&
+ (
+ cd child1 &&
+ git branch foo &&
+ git symbolic-ref refs/heads/bar refs/heads/foo &&
+ git config receive.denyCurrentBranch false
+ ) &&
+ (
+ cd child2 &&
+ >path2 &&
+ git add path2 &&
+ test_tick &&
+ git commit -a -m child2 &&
+ git branch foo &&
+ git branch bar &&
+ git push ../child1 foo bar
+ )
+'
+
+test_expect_success 'push into aliased refs (inconsistent)' '
+ mk_test testrepo heads/main &&
+ mk_child testrepo child1 &&
+ mk_child testrepo child2 &&
+ (
+ cd child1 &&
+ git branch foo &&
+ git symbolic-ref refs/heads/bar refs/heads/foo &&
+ git config receive.denyCurrentBranch false
+ ) &&
+ (
+ cd child2 &&
+ >path2 &&
+ git add path2 &&
+ test_tick &&
+ git commit -a -m child2 &&
+ git branch foo &&
+ >path3 &&
+ git add path3 &&
+ test_tick &&
+ git commit -a -m child2 &&
+ git branch bar &&
+ test_must_fail git push ../child1 foo bar 2>stderr &&
+ grep "refusing inconsistent update" stderr
+ )
+'
+
+test_force_push_tag () {
+ tag_type_description=$1
+ tag_args=$2
+
+ test_expect_success "force pushing required to update $tag_type_description" "
+ mk_test testrepo heads/main &&
+ mk_child testrepo child1 &&
+ mk_child testrepo child2 &&
+ (
+ cd child1 &&
+ git tag testTag &&
+ git push ../child2 testTag &&
+ >file1 &&
+ git add file1 &&
+ git commit -m 'file1' &&
+ git tag $tag_args testTag &&
+ test_must_fail git push ../child2 testTag &&
+ git push --force ../child2 testTag &&
+ git tag $tag_args testTag HEAD~ &&
+ test_must_fail git push ../child2 testTag &&
+ git push --force ../child2 testTag &&
+
+ # Clobbering without + in refspec needs --force
+ git tag -f testTag &&
+ test_must_fail git push ../child2 'refs/tags/*:refs/tags/*' &&
+ git push --force ../child2 'refs/tags/*:refs/tags/*' &&
+
+ # Clobbering with + in refspec does not need --force
+ git tag -f testTag HEAD~ &&
+ git push ../child2 '+refs/tags/*:refs/tags/*' &&
+
+ # Clobbering with --no-force still obeys + in refspec
+ git tag -f testTag &&
+ git push --no-force ../child2 '+refs/tags/*:refs/tags/*' &&
+
+ # Clobbering with/without --force and 'tag <name>' format
+ git tag -f testTag HEAD~ &&
+ test_must_fail git push ../child2 tag testTag &&
+ git push --force ../child2 tag testTag
+ )
+ "
+}
+
+test_force_push_tag "lightweight tag" "-f"
+test_force_push_tag "annotated tag" "-f -a -m'tag message'"
+
+test_force_fetch_tag () {
+ tag_type_description=$1
+ tag_args=$2
+
+ test_expect_success "fetch will not clobber an existing $tag_type_description without --force" "
+ mk_test testrepo heads/main &&
+ mk_child testrepo child1 &&
+ mk_child testrepo child2 &&
+ (
+ cd testrepo &&
+ git tag testTag &&
+ git -C ../child1 fetch origin tag testTag &&
+ >file1 &&
+ git add file1 &&
+ git commit -m 'file1' &&
+ git tag $tag_args testTag &&
+ test_must_fail git -C ../child1 fetch origin tag testTag &&
+ git -C ../child1 fetch origin '+refs/tags/*:refs/tags/*'
+ )
+ "
+}
+
+test_force_fetch_tag "lightweight tag" "-f"
+test_force_fetch_tag "annotated tag" "-f -a -m'tag message'"
+
+test_expect_success 'push --porcelain' '
+ mk_empty testrepo &&
+ echo >.git/foo "To testrepo" &&
+ echo >>.git/foo "* refs/heads/main:refs/remotes/origin/main [new reference]" &&
+ echo >>.git/foo "Done" &&
+ git push >.git/bar --porcelain testrepo refs/heads/main:refs/remotes/origin/main &&
+ (
+ cd testrepo &&
+ echo "$the_commit commit refs/remotes/origin/main" >expect &&
+ git for-each-ref refs/remotes/origin >actual &&
+ test_cmp expect actual
+ ) &&
+ test_cmp .git/foo .git/bar
+'
+
+test_expect_success 'push --porcelain bad url' '
+ mk_empty testrepo &&
+ test_must_fail git push >.git/bar --porcelain asdfasdfasd refs/heads/main:refs/remotes/origin/main &&
+ ! grep -q Done .git/bar
+'
+
+test_expect_success 'push --porcelain rejected' '
+ mk_empty testrepo &&
+ git push testrepo refs/heads/main:refs/remotes/origin/main &&
+ (cd testrepo &&
+ git reset --hard origin/main^ &&
+ git config receive.denyCurrentBranch true) &&
+
+ echo >.git/foo "To testrepo" &&
+ echo >>.git/foo "! refs/heads/main:refs/heads/main [remote rejected] (branch is currently checked out)" &&
+ echo >>.git/foo "Done" &&
+
+ test_must_fail git push >.git/bar --porcelain testrepo refs/heads/main:refs/heads/main &&
+ test_cmp .git/foo .git/bar
+'
+
+test_expect_success 'push --porcelain --dry-run rejected' '
+ mk_empty testrepo &&
+ git push testrepo refs/heads/main:refs/remotes/origin/main &&
+ (cd testrepo &&
+ git reset --hard origin/main &&
+ git config receive.denyCurrentBranch true) &&
+
+ echo >.git/foo "To testrepo" &&
+ echo >>.git/foo "! refs/heads/main^:refs/heads/main [rejected] (non-fast-forward)" &&
+ echo >>.git/foo "Done" &&
+
+ test_must_fail git push >.git/bar --porcelain --dry-run testrepo refs/heads/main^:refs/heads/main &&
+ test_cmp .git/foo .git/bar
+'
+
+test_expect_success 'push --prune' '
+ mk_test testrepo heads/main heads/second heads/foo heads/bar &&
+ git push --prune testrepo : &&
+ check_push_result testrepo $the_commit heads/main &&
+ check_push_result testrepo $the_first_commit heads/second &&
+ ! check_push_result testrepo $the_first_commit heads/foo heads/bar
+'
+
+test_expect_success 'push --prune refspec' '
+ mk_test testrepo tmp/main tmp/second tmp/foo tmp/bar &&
+ git push --prune testrepo "refs/heads/*:refs/tmp/*" &&
+ check_push_result testrepo $the_commit tmp/main &&
+ check_push_result testrepo $the_first_commit tmp/second &&
+ ! check_push_result testrepo $the_first_commit tmp/foo tmp/bar
+'
+
+for configsection in transfer receive
+do
+ test_expect_success "push to update a ref hidden by $configsection.hiderefs" '
+ mk_test testrepo heads/main hidden/one hidden/two hidden/three &&
+ (
+ cd testrepo &&
+ git config $configsection.hiderefs refs/hidden
+ ) &&
+
+ # push to unhidden ref succeeds normally
+ git push testrepo main:refs/heads/main &&
+ check_push_result testrepo $the_commit heads/main &&
+
+ # push to update a hidden ref should fail
+ test_must_fail git push testrepo main:refs/hidden/one &&
+ check_push_result testrepo $the_first_commit hidden/one &&
+
+ # push to delete a hidden ref should fail
+ test_must_fail git push testrepo :refs/hidden/two &&
+ check_push_result testrepo $the_first_commit hidden/two &&
+
+ # idempotent push to update a hidden ref should fail
+ test_must_fail git push testrepo $the_first_commit:refs/hidden/three &&
+ check_push_result testrepo $the_first_commit hidden/three
+ '
+done
+
+test_expect_success 'fetch exact SHA1' '
+ mk_test testrepo heads/main hidden/one &&
+ git push testrepo main:refs/hidden/one &&
+ (
+ cd testrepo &&
+ git config transfer.hiderefs refs/hidden
+ ) &&
+ check_push_result testrepo $the_commit hidden/one &&
+
+ mk_child testrepo child &&
+ (
+ cd child &&
+
+ # make sure $the_commit does not exist here
+ git repack -a -d &&
+ git prune &&
+ test_must_fail git cat-file -t $the_commit &&
+
+ # Some protocol versions (e.g. 2) support fetching
+ # unadvertised objects, so restrict this test to v0.
+
+ # fetching the hidden object should fail by default
+ test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
+ git fetch -v ../testrepo $the_commit:refs/heads/copy 2>err &&
+ test_grep "Server does not allow request for unadvertised object" err &&
+ test_must_fail git rev-parse --verify refs/heads/copy &&
+
+ # the server side can allow it to succeed
+ (
+ cd ../testrepo &&
+ git config uploadpack.allowtipsha1inwant true
+ ) &&
+
+ git fetch -v ../testrepo $the_commit:refs/heads/copy main:refs/heads/extra &&
+ cat >expect <<-EOF &&
+ $the_commit
+ $the_first_commit
+ EOF
+ {
+ git rev-parse --verify refs/heads/copy &&
+ git rev-parse --verify refs/heads/extra
+ } >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'fetch exact SHA1 in protocol v2' '
+ mk_test testrepo heads/main hidden/one &&
+ git push testrepo main:refs/hidden/one &&
+ git -C testrepo config transfer.hiderefs refs/hidden &&
+ check_push_result testrepo $the_commit hidden/one &&
+
+ mk_child testrepo child &&
+ git -C child config protocol.version 2 &&
+
+ # make sure $the_commit does not exist here
+ git -C child repack -a -d &&
+ git -C child prune &&
+ test_must_fail git -C child cat-file -t $the_commit &&
+
+ # fetching the hidden object succeeds by default
+ # NEEDSWORK: should this match the v0 behavior instead?
+ git -C child fetch -v ../testrepo $the_commit:refs/heads/copy
+'
+
+for configallowtipsha1inwant in true false
+do
+ test_expect_success "shallow fetch reachable SHA1 (but not a ref), allowtipsha1inwant=$configallowtipsha1inwant" '
+ mk_empty testrepo &&
+ (
+ cd testrepo &&
+ git config uploadpack.allowtipsha1inwant $configallowtipsha1inwant &&
+ git commit --allow-empty -m foo &&
+ git commit --allow-empty -m bar
+ ) &&
+ SHA1=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
+ mk_empty shallow &&
+ (
+ cd shallow &&
+ # Some protocol versions (e.g. 2) support fetching
+ # unadvertised objects, so restrict this test to v0.
+ test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
+ git fetch --depth=1 ../testrepo/.git $SHA1 &&
+ git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
+ git fetch --depth=1 ../testrepo/.git $SHA1 &&
+ git cat-file commit $SHA1
+ )
+ '
+
+ test_expect_success "deny fetch unreachable SHA1, allowtipsha1inwant=$configallowtipsha1inwant" '
+ mk_empty testrepo &&
+ (
+ cd testrepo &&
+ git config uploadpack.allowtipsha1inwant $configallowtipsha1inwant &&
+ git commit --allow-empty -m foo &&
+ git commit --allow-empty -m bar &&
+ git commit --allow-empty -m xyz
+ ) &&
+ SHA1_1=$(git --git-dir=testrepo/.git rev-parse HEAD^^) &&
+ SHA1_2=$(git --git-dir=testrepo/.git rev-parse HEAD^) &&
+ SHA1_3=$(git --git-dir=testrepo/.git rev-parse HEAD) &&
+ (
+ cd testrepo &&
+ git reset --hard $SHA1_2 &&
+ git cat-file commit $SHA1_1 &&
+ git cat-file commit $SHA1_3
+ ) &&
+ mk_empty shallow &&
+ (
+ cd shallow &&
+ # Some protocol versions (e.g. 2) support fetching
+ # unadvertised objects, so restrict this test to v0.
+ test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
+ git fetch ../testrepo/.git $SHA1_3 &&
+ test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
+ git fetch ../testrepo/.git $SHA1_1 &&
+ git --git-dir=../testrepo/.git config uploadpack.allowreachablesha1inwant true &&
+ git fetch ../testrepo/.git $SHA1_1 &&
+ git cat-file commit $SHA1_1 &&
+ test_must_fail git cat-file commit $SHA1_2 &&
+ git fetch ../testrepo/.git $SHA1_2 &&
+ git cat-file commit $SHA1_2 &&
+ test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
+ git fetch ../testrepo/.git $SHA1_3 2>err &&
+ # ideally we would insist this be on a "remote error:"
+ # line, but it is racy; see the commit message
+ test_grep "not our ref.*$SHA1_3\$" err
+ )
+ '
+done
+
+test_expect_success 'fetch follows tags by default' '
+ mk_test testrepo heads/main &&
+ test_when_finished "rm -rf src" &&
+ git init src &&
+ (
+ cd src &&
+ git pull ../testrepo main &&
+ git tag -m "annotated" tag &&
+ git for-each-ref >tmp1 &&
+ sed -n "p; s|refs/heads/main$|refs/remotes/origin/main|p" tmp1 |
+ sort -k 3 >../expect
+ ) &&
+ test_when_finished "rm -rf dst" &&
+ git init dst &&
+ (
+ cd dst &&
+ git remote add origin ../src &&
+ git config branch.main.remote origin &&
+ git config branch.main.merge refs/heads/main &&
+ git pull &&
+ git for-each-ref >../actual
+ ) &&
+ test_cmp expect actual
+'
+
+test_expect_success 'peeled advertisements are not considered ref tips' '
+ mk_empty testrepo &&
+ git -C testrepo commit --allow-empty -m one &&
+ git -C testrepo commit --allow-empty -m two &&
+ git -C testrepo tag -m foo mytag HEAD^ &&
+ oid=$(git -C testrepo rev-parse mytag^{commit}) &&
+ test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
+ git fetch testrepo $oid 2>err &&
+ test_grep "Server does not allow request for unadvertised object" err
+'
+
+test_expect_success 'pushing a specific ref applies remote.$name.push as refmap' '
+ mk_test testrepo heads/main &&
+ test_when_finished "rm -rf src" &&
+ git init src &&
+ test_when_finished "rm -rf dst" &&
+ git init --bare dst &&
+ (
+ cd src &&
+ git pull ../testrepo main &&
+ git branch next &&
+ git config remote.dst.url ../dst &&
+ git config remote.dst.push "+refs/heads/*:refs/remotes/src/*" &&
+ git push dst main &&
+ git show-ref refs/heads/main |
+ sed -e "s|refs/heads/|refs/remotes/src/|" >../dst/expect
+ ) &&
+ (
+ cd dst &&
+ test_must_fail git show-ref refs/heads/next &&
+ test_must_fail git show-ref refs/heads/main &&
+ git show-ref refs/remotes/src/main >actual
+ ) &&
+ test_cmp dst/expect dst/actual
+'
+
+test_expect_success 'with no remote.$name.push, it is not used as refmap' '
+ mk_test testrepo heads/main &&
+ test_when_finished "rm -rf src" &&
+ git init src &&
+ test_when_finished "rm -rf dst" &&
+ git init --bare dst &&
+ (
+ cd src &&
+ git pull ../testrepo main &&
+ git branch next &&
+ git config remote.dst.url ../dst &&
+ git config push.default matching &&
+ git push dst main &&
+ git show-ref refs/heads/main >../dst/expect
+ ) &&
+ (
+ cd dst &&
+ test_must_fail git show-ref refs/heads/next &&
+ git show-ref refs/heads/main >actual
+ ) &&
+ test_cmp dst/expect dst/actual
+'
+
+test_expect_success 'with no remote.$name.push, upstream mapping is used' '
+ mk_test testrepo heads/main &&
+ test_when_finished "rm -rf src" &&
+ git init src &&
+ test_when_finished "rm -rf dst" &&
+ git init --bare dst &&
+ (
+ cd src &&
+ git pull ../testrepo main &&
+ git branch next &&
+ git config remote.dst.url ../dst &&
+ git config remote.dst.fetch "+refs/heads/*:refs/remotes/dst/*" &&
+ git config push.default upstream &&
+
+ git config branch.main.merge refs/heads/trunk &&
+ git config branch.main.remote dst &&
+
+ git push dst main &&
+ git show-ref refs/heads/main |
+ sed -e "s|refs/heads/main|refs/heads/trunk|" >../dst/expect
+ ) &&
+ (
+ cd dst &&
+ test_must_fail git show-ref refs/heads/main &&
+ test_must_fail git show-ref refs/heads/next &&
+ git show-ref refs/heads/trunk >actual
+ ) &&
+ test_cmp dst/expect dst/actual
+'
+
+test_expect_success 'push does not follow tags by default' '
+ mk_test testrepo heads/main &&
+ test_when_finished "rm -rf src" &&
+ git init src &&
+ test_when_finished "rm -rf dst" &&
+ git init --bare dst &&
+ (
+ cd src &&
+ git pull ../testrepo main &&
+ git tag -m "annotated" tag &&
+ git checkout -b another &&
+ git commit --allow-empty -m "future commit" &&
+ git tag -m "future" future &&
+ git checkout main &&
+ git for-each-ref refs/heads/main >../expect &&
+ git push ../dst main
+ ) &&
+ (
+ cd dst &&
+ git for-each-ref >../actual
+ ) &&
+ test_cmp expect actual
+'
+
+test_expect_success 'push --follow-tags only pushes relevant tags' '
+ mk_test testrepo heads/main &&
+ test_when_finished "rm -rf src" &&
+ git init src &&
+ test_when_finished "rm -rf dst" &&
+ git init --bare dst &&
+ (
+ cd src &&
+ git pull ../testrepo main &&
+ git tag -m "annotated" tag &&
+ git checkout -b another &&
+ git commit --allow-empty -m "future commit" &&
+ git tag -m "future" future &&
+ git checkout main &&
+ git for-each-ref refs/heads/main refs/tags/tag >../expect &&
+ git push --follow-tags ../dst main
+ ) &&
+ (
+ cd dst &&
+ git for-each-ref >../actual
+ ) &&
+ test_cmp expect actual
+'
+
+test_expect_success 'push --no-thin must produce non-thin pack' '
+ cat >>path1 <<\EOF &&
+keep base version of path1 big enough, compared to the new changes
+later, in order to pass size heuristics in
+builtin/pack-objects.c:try_delta()
+EOF
+ git commit -am initial &&
+ git init no-thin &&
+ git --git-dir=no-thin/.git config receive.unpacklimit 0 &&
+ git push no-thin/.git refs/heads/main:refs/heads/foo &&
+ echo modified >> path1 &&
+ git commit -am modified &&
+ git repack -adf &&
+ rcvpck="git receive-pack --reject-thin-pack-for-testing" &&
+ git push --no-thin --receive-pack="$rcvpck" no-thin/.git refs/heads/main:refs/heads/foo
+'
+
+test_expect_success 'pushing a tag pushes the tagged object' '
+ blob=$(echo unreferenced | git hash-object -w --stdin) &&
+ git tag -m foo tag-of-blob $blob &&
+ test_when_finished "rm -rf dst.git" &&
+ git init --bare dst.git &&
+ git push dst.git tag-of-blob &&
+ # the receiving index-pack should have noticed
+ # any problems, but we double check
+ echo unreferenced >expect &&
+ git --git-dir=dst.git cat-file blob tag-of-blob >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'push into bare respects core.logallrefupdates' '
+ test_when_finished "rm -rf dst.git" &&
+ git init --bare dst.git &&
+ git -C dst.git config core.logallrefupdates true &&
+
+ # double push to test both with and without
+ # the actual pack transfer
+ git push dst.git main:one &&
+ echo "one@{0} push" >expect &&
+ git -C dst.git log -g --format="%gd %gs" one >actual &&
+ test_cmp expect actual &&
+
+ git push dst.git main:two &&
+ echo "two@{0} push" >expect &&
+ git -C dst.git log -g --format="%gd %gs" two >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'fetch into bare respects core.logallrefupdates' '
+ test_when_finished "rm -rf dst.git" &&
+ git init --bare dst.git &&
+ (
+ cd dst.git &&
+ git config core.logallrefupdates true &&
+
+ # as above, we double-fetch to test both
+ # with and without pack transfer
+ git fetch .. main:one &&
+ echo "one@{0} fetch .. main:one: storing head" >expect &&
+ git log -g --format="%gd %gs" one >actual &&
+ test_cmp expect actual &&
+
+ git fetch .. main:two &&
+ echo "two@{0} fetch .. main:two: storing head" >expect &&
+ git log -g --format="%gd %gs" two >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'receive.denyCurrentBranch = updateInstead' '
+ mk_empty testrepo &&
+ git push testrepo main &&
+ (
+ cd testrepo &&
+ git reset --hard &&
+ git config receive.denyCurrentBranch updateInstead
+ ) &&
+ test_commit third path2 &&
+
+ # Try pushing into a repository with pristine working tree
+ git push testrepo main &&
+ (
+ cd testrepo &&
+ git update-index -q --refresh &&
+ git diff-files --quiet -- &&
+ git diff-index --quiet --cached HEAD -- &&
+ test third = "$(cat path2)" &&
+ test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
+ ) &&
+
+ # Try pushing into a repository with working tree needing a refresh
+ (
+ cd testrepo &&
+ git reset --hard HEAD^ &&
+ test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
+ test-tool chmtime +100 path1
+ ) &&
+ git push testrepo main &&
+ (
+ cd testrepo &&
+ git update-index -q --refresh &&
+ git diff-files --quiet -- &&
+ git diff-index --quiet --cached HEAD -- &&
+ test_cmp ../path1 path1 &&
+ test third = "$(cat path2)" &&
+ test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
+ ) &&
+
+ # Update what is to be pushed
+ test_commit fourth path2 &&
+
+ # Try pushing into a repository with a dirty working tree
+ # (1) the working tree updated
+ (
+ cd testrepo &&
+ echo changed >path1
+ ) &&
+ test_must_fail git push testrepo main &&
+ (
+ cd testrepo &&
+ test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
+ git diff --quiet --cached &&
+ test changed = "$(cat path1)"
+ ) &&
+
+ # (2) the index updated
+ (
+ cd testrepo &&
+ echo changed >path1 &&
+ git add path1
+ ) &&
+ test_must_fail git push testrepo main &&
+ (
+ cd testrepo &&
+ test $(git -C .. rev-parse HEAD^) = $(git rev-parse HEAD) &&
+ git diff --quiet &&
+ test changed = "$(cat path1)"
+ ) &&
+
+ # Introduce a new file in the update
+ test_commit fifth path3 &&
+
+ # (3) the working tree has an untracked file that would interfere
+ (
+ cd testrepo &&
+ git reset --hard &&
+ echo changed >path3
+ ) &&
+ test_must_fail git push testrepo main &&
+ (
+ cd testrepo &&
+ test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
+ git diff --quiet &&
+ git diff --quiet --cached &&
+ test changed = "$(cat path3)"
+ ) &&
+
+ # (4) the target changes to what gets pushed but it still is a change
+ (
+ cd testrepo &&
+ git reset --hard &&
+ echo fifth >path3 &&
+ git add path3
+ ) &&
+ test_must_fail git push testrepo main &&
+ (
+ cd testrepo &&
+ test $(git -C .. rev-parse HEAD^^) = $(git rev-parse HEAD) &&
+ git diff --quiet &&
+ test fifth = "$(cat path3)"
+ ) &&
+
+ # (5) push into void
+ test_when_finished "rm -rf void" &&
+ git init void &&
+ (
+ cd void &&
+ git config receive.denyCurrentBranch updateInstead
+ ) &&
+ git push void main &&
+ (
+ cd void &&
+ test $(git -C .. rev-parse main) = $(git rev-parse HEAD) &&
+ git diff --quiet &&
+ git diff --cached --quiet
+ ) &&
+
+ # (6) updateInstead intervened by fast-forward check
+ test_must_fail git push void main^:main &&
+ test $(git -C void rev-parse HEAD) = $(git rev-parse main) &&
+ git -C void diff --quiet &&
+ git -C void diff --cached --quiet
+'
+
+test_expect_success 'updateInstead with push-to-checkout hook' '
+ test_when_finished "rm -rf testrepo" &&
+ git init testrepo &&
+ git -C testrepo pull .. main &&
+ git -C testrepo reset --hard HEAD^^ &&
+ git -C testrepo tag initial &&
+ git -C testrepo config receive.denyCurrentBranch updateInstead &&
+ test_hook -C testrepo push-to-checkout <<-\EOF &&
+ echo >&2 updating from $(git rev-parse HEAD)
+ echo >&2 updating to "$1"
+
+ git update-index -q --refresh &&
+ git read-tree -u -m HEAD "$1" || {
+ status=$?
+ echo >&2 read-tree failed
+ exit $status
+ }
+ EOF
+
+ # Try pushing into a pristine
+ git push testrepo main &&
+ (
+ cd testrepo &&
+ git diff --quiet &&
+ git diff HEAD --quiet &&
+ test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
+ ) &&
+
+ # Try pushing into a repository with conflicting change
+ (
+ cd testrepo &&
+ git reset --hard initial &&
+ echo conflicting >path2
+ ) &&
+ test_must_fail git push testrepo main &&
+ (
+ cd testrepo &&
+ test $(git rev-parse initial) = $(git rev-parse HEAD) &&
+ test conflicting = "$(cat path2)" &&
+ git diff-index --quiet --cached HEAD
+ ) &&
+
+ # Try pushing into a repository with unrelated change
+ (
+ cd testrepo &&
+ git reset --hard initial &&
+ echo unrelated >path1 &&
+ echo irrelevant >path5 &&
+ git add path5
+ ) &&
+ git push testrepo main &&
+ (
+ cd testrepo &&
+ test "$(cat path1)" = unrelated &&
+ test "$(cat path5)" = irrelevant &&
+ test "$(git diff --name-only --cached HEAD)" = path5 &&
+ test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
+ ) &&
+
+ # push into void
+ test_when_finished "rm -rf void" &&
+ git init void &&
+ git -C void config receive.denyCurrentBranch updateInstead &&
+ test_hook -C void push-to-checkout <<-\EOF &&
+ if git rev-parse --quiet --verify HEAD
+ then
+ has_head=yes
+ echo >&2 updating from $(git rev-parse HEAD)
+ else
+ has_head=no
+ echo >&2 pushing into void
+ fi
+ echo >&2 updating to "$1"
+
+ git update-index -q --refresh &&
+ case "$has_head" in
+ yes)
+ git read-tree -u -m HEAD "$1" ;;
+ no)
+ git read-tree -u -m "$1" ;;
+ esac || {
+ status=$?
+ echo >&2 read-tree failed
+ exit $status
+ }
+ EOF
+
+ git push void main &&
+ (
+ cd void &&
+ git diff --quiet &&
+ git diff --cached --quiet &&
+ test $(git -C .. rev-parse HEAD) = $(git rev-parse HEAD)
+ )
+'
+
+test_expect_success 'denyCurrentBranch and worktrees' '
+ git worktree add new-wt &&
+ git clone . cloned &&
+ test_commit -C cloned first &&
+ test_config receive.denyCurrentBranch refuse &&
+ test_must_fail git -C cloned push origin HEAD:new-wt &&
+ test_config receive.denyCurrentBranch updateInstead &&
+ git -C cloned push origin HEAD:new-wt &&
+ test_path_exists new-wt/first.t &&
+ test_must_fail git -C cloned push --delete origin new-wt
+'
+
+test_expect_success 'denyCurrentBranch and bare repository worktrees' '
+ test_when_finished "rm -fr bare.git" &&
+ git clone --bare . bare.git &&
+ git -C bare.git worktree add wt &&
+ test_commit grape &&
+ git -C bare.git config receive.denyCurrentBranch refuse &&
+ test_must_fail git push bare.git HEAD:wt &&
+ git -C bare.git config receive.denyCurrentBranch updateInstead &&
+ git push bare.git HEAD:wt &&
+ test_path_exists bare.git/wt/grape.t &&
+ test_must_fail git push --delete bare.git wt
+'
+
+test_expect_success 'refuse fetch to current branch of worktree' '
+ test_when_finished "git worktree remove --force wt && git branch -D wt" &&
+ git worktree add wt &&
+ test_commit apple &&
+ test_must_fail git fetch . HEAD:wt &&
+ git fetch -u . HEAD:wt
+'
+
+test_expect_success 'refuse fetch to current branch of bare repository worktree' '
+ test_when_finished "rm -fr bare.git" &&
+ git clone --bare . bare.git &&
+ git -C bare.git worktree add wt &&
+ test_commit banana &&
+ test_must_fail git -C bare.git fetch .. HEAD:wt &&
+ git -C bare.git fetch -u .. HEAD:wt
+'
+
+test_expect_success 'refuse to push a hidden ref, and make sure do not pollute the repository' '
+ mk_empty testrepo &&
+ git -C testrepo config receive.hiderefs refs/hidden &&
+ git -C testrepo config receive.unpackLimit 1 &&
+ test_must_fail git push testrepo HEAD:refs/hidden/foo &&
+ test_dir_is_empty testrepo/.git/objects/pack
+'
+
+test_expect_success 'push with config push.useBitmaps' '
+ mk_test testrepo heads/main &&
+ git checkout main &&
+ test_unconfig push.useBitmaps &&
+ GIT_TRACE2_EVENT="$PWD/default" \
+ git push --quiet testrepo main:test &&
+ test_subcommand git pack-objects --all-progress-implied --revs --stdout \
+ --thin --delta-base-offset -q <default &&
+
+ test_config push.useBitmaps true &&
+ GIT_TRACE2_EVENT="$PWD/true" \
+ git push --quiet testrepo main:test2 &&
+ test_subcommand git pack-objects --all-progress-implied --revs --stdout \
+ --thin --delta-base-offset -q <true &&
+
+ test_config push.useBitmaps false &&
+ GIT_TRACE2_EVENT="$PWD/false" \
+ git push --quiet testrepo main:test3 &&
+ test_subcommand git pack-objects --all-progress-implied --revs --stdout \
+ --thin --delta-base-offset -q --no-use-bitmap-index <false
+'
+
+test_done
diff --git a/t/t5517-push-mirror.sh b/t/t5517-push-mirror.sh
new file mode 100755
index 0000000..6d4944a
--- /dev/null
+++ b/t/t5517-push-mirror.sh
@@ -0,0 +1,282 @@
+#!/bin/sh
+
+test_description='pushing to a mirror repository'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+D=$(pwd)
+
+invert () {
+ if "$@"; then
+ return 1
+ else
+ return 0
+ fi
+}
+
+mk_repo_pair () {
+ rm -rf main mirror &&
+ mkdir mirror &&
+ (
+ cd mirror &&
+ git init &&
+ git config receive.denyCurrentBranch warn
+ ) &&
+ mkdir main &&
+ (
+ cd main &&
+ git init &&
+ git remote add $1 up ../mirror
+ )
+}
+
+
+# BRANCH tests
+test_expect_success 'push mirror creates new branches' '
+
+ mk_repo_pair &&
+ (
+ cd main &&
+ echo one >foo && git add foo && git commit -m one &&
+ git push --mirror up
+ ) &&
+ main_main=$(cd main && git show-ref -s --verify refs/heads/main) &&
+ mirror_main=$(cd mirror && git show-ref -s --verify refs/heads/main) &&
+ test "$main_main" = "$mirror_main"
+
+'
+
+test_expect_success 'push mirror updates existing branches' '
+
+ mk_repo_pair &&
+ (
+ cd main &&
+ echo one >foo && git add foo && git commit -m one &&
+ git push --mirror up &&
+ echo two >foo && git add foo && git commit -m two &&
+ git push --mirror up
+ ) &&
+ main_main=$(cd main && git show-ref -s --verify refs/heads/main) &&
+ mirror_main=$(cd mirror && git show-ref -s --verify refs/heads/main) &&
+ test "$main_main" = "$mirror_main"
+
+'
+
+test_expect_success 'push mirror force updates existing branches' '
+
+ mk_repo_pair &&
+ (
+ cd main &&
+ echo one >foo && git add foo && git commit -m one &&
+ git push --mirror up &&
+ echo two >foo && git add foo && git commit -m two &&
+ git push --mirror up &&
+ git reset --hard HEAD^ &&
+ git push --mirror up
+ ) &&
+ main_main=$(cd main && git show-ref -s --verify refs/heads/main) &&
+ mirror_main=$(cd mirror && git show-ref -s --verify refs/heads/main) &&
+ test "$main_main" = "$mirror_main"
+
+'
+
+test_expect_success 'push mirror removes branches' '
+
+ mk_repo_pair &&
+ (
+ cd main &&
+ echo one >foo && git add foo && git commit -m one &&
+ git branch remove main &&
+ git push --mirror up &&
+ git branch -D remove &&
+ git push --mirror up
+ ) &&
+ (
+ cd mirror &&
+ invert git show-ref -s --verify refs/heads/remove
+ )
+
+'
+
+test_expect_success 'push mirror adds, updates and removes branches together' '
+
+ mk_repo_pair &&
+ (
+ cd main &&
+ echo one >foo && git add foo && git commit -m one &&
+ git branch remove main &&
+ git push --mirror up &&
+ git branch -D remove &&
+ git branch add main &&
+ echo two >foo && git add foo && git commit -m two &&
+ git push --mirror up
+ ) &&
+ main_main=$(cd main && git show-ref -s --verify refs/heads/main) &&
+ main_add=$(cd main && git show-ref -s --verify refs/heads/add) &&
+ mirror_main=$(cd mirror && git show-ref -s --verify refs/heads/main) &&
+ mirror_add=$(cd mirror && git show-ref -s --verify refs/heads/add) &&
+ test "$main_main" = "$mirror_main" &&
+ test "$main_add" = "$mirror_add" &&
+ (
+ cd mirror &&
+ invert git show-ref -s --verify refs/heads/remove
+ )
+
+'
+
+
+# TAG tests
+test_expect_success 'push mirror creates new tags' '
+
+ mk_repo_pair &&
+ (
+ cd main &&
+ echo one >foo && git add foo && git commit -m one &&
+ git tag -f tmain main &&
+ git push --mirror up
+ ) &&
+ main_main=$(cd main && git show-ref -s --verify refs/tags/tmain) &&
+ mirror_main=$(cd mirror && git show-ref -s --verify refs/tags/tmain) &&
+ test "$main_main" = "$mirror_main"
+
+'
+
+test_expect_success 'push mirror updates existing tags' '
+
+ mk_repo_pair &&
+ (
+ cd main &&
+ echo one >foo && git add foo && git commit -m one &&
+ git tag -f tmain main &&
+ git push --mirror up &&
+ echo two >foo && git add foo && git commit -m two &&
+ git tag -f tmain main &&
+ git push --mirror up
+ ) &&
+ main_main=$(cd main && git show-ref -s --verify refs/tags/tmain) &&
+ mirror_main=$(cd mirror && git show-ref -s --verify refs/tags/tmain) &&
+ test "$main_main" = "$mirror_main"
+
+'
+
+test_expect_success 'push mirror force updates existing tags' '
+
+ mk_repo_pair &&
+ (
+ cd main &&
+ echo one >foo && git add foo && git commit -m one &&
+ git tag -f tmain main &&
+ git push --mirror up &&
+ echo two >foo && git add foo && git commit -m two &&
+ git tag -f tmain main &&
+ git push --mirror up &&
+ git reset --hard HEAD^ &&
+ git tag -f tmain main &&
+ git push --mirror up
+ ) &&
+ main_main=$(cd main && git show-ref -s --verify refs/tags/tmain) &&
+ mirror_main=$(cd mirror && git show-ref -s --verify refs/tags/tmain) &&
+ test "$main_main" = "$mirror_main"
+
+'
+
+test_expect_success 'push mirror removes tags' '
+
+ mk_repo_pair &&
+ (
+ cd main &&
+ echo one >foo && git add foo && git commit -m one &&
+ git tag -f tremove main &&
+ git push --mirror up &&
+ git tag -d tremove &&
+ git push --mirror up
+ ) &&
+ (
+ cd mirror &&
+ invert git show-ref -s --verify refs/tags/tremove
+ )
+
+'
+
+test_expect_success 'push mirror adds, updates and removes tags together' '
+
+ mk_repo_pair &&
+ (
+ cd main &&
+ echo one >foo && git add foo && git commit -m one &&
+ git tag -f tmain main &&
+ git tag -f tremove main &&
+ git push --mirror up &&
+ git tag -d tremove &&
+ git tag tadd main &&
+ echo two >foo && git add foo && git commit -m two &&
+ git tag -f tmain main &&
+ git push --mirror up
+ ) &&
+ main_main=$(cd main && git show-ref -s --verify refs/tags/tmain) &&
+ main_add=$(cd main && git show-ref -s --verify refs/tags/tadd) &&
+ mirror_main=$(cd mirror && git show-ref -s --verify refs/tags/tmain) &&
+ mirror_add=$(cd mirror && git show-ref -s --verify refs/tags/tadd) &&
+ test "$main_main" = "$mirror_main" &&
+ test "$main_add" = "$mirror_add" &&
+ (
+ cd mirror &&
+ invert git show-ref -s --verify refs/tags/tremove
+ )
+
+'
+
+test_expect_success 'remote.foo.mirror adds and removes branches' '
+
+ mk_repo_pair --mirror &&
+ (
+ cd main &&
+ echo one >foo && git add foo && git commit -m one &&
+ git branch keep main &&
+ git branch remove main &&
+ git push up &&
+ git branch -D remove &&
+ git push up
+ ) &&
+ (
+ cd mirror &&
+ git show-ref -s --verify refs/heads/keep &&
+ invert git show-ref -s --verify refs/heads/remove
+ )
+
+'
+
+test_expect_success 'remote.foo.mirror=no has no effect' '
+
+ mk_repo_pair &&
+ (
+ cd main &&
+ echo one >foo && git add foo && git commit -m one &&
+ git config --add remote.up.mirror no &&
+ git branch keep main &&
+ git push --mirror up &&
+ git branch -D keep &&
+ git push up :
+ ) &&
+ (
+ cd mirror &&
+ git show-ref -s --verify refs/heads/keep
+ )
+
+'
+
+test_expect_success 'push to mirrored repository with refspec fails' '
+ mk_repo_pair &&
+ (
+ cd main &&
+ echo one >foo && git add foo && git commit -m one &&
+ git config --add remote.up.mirror true &&
+ test_must_fail git push up main
+ )
+'
+
+test_done
diff --git a/t/t5518-fetch-exit-status.sh b/t/t5518-fetch-exit-status.sh
new file mode 100755
index 0000000..c131200
--- /dev/null
+++ b/t/t5518-fetch-exit-status.sh
@@ -0,0 +1,41 @@
+#!/bin/sh
+#
+# Copyright (c) 2008 Dmitry V. Levin
+#
+
+test_description='fetch exit status test'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+
+ >file &&
+ git add file &&
+ git commit -m initial &&
+
+ git checkout -b side &&
+ echo side >file &&
+ git commit -a -m side &&
+
+ git checkout main &&
+ echo next >file &&
+ git commit -a -m next
+'
+
+test_expect_success 'non-fast-forward fetch' '
+
+ test_must_fail git fetch . main:side
+
+'
+
+test_expect_success 'forced update' '
+
+ git fetch . +main:side
+
+'
+
+test_done
diff --git a/t/t5519-push-alternates.sh b/t/t5519-push-alternates.sh
new file mode 100755
index 0000000..20ba604
--- /dev/null
+++ b/t/t5519-push-alternates.sh
@@ -0,0 +1,146 @@
+#!/bin/sh
+
+test_description='push to a repository that borrows from elsewhere'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success setup '
+ mkdir alice-pub &&
+ (
+ cd alice-pub &&
+ GIT_DIR=. git init
+ ) &&
+ mkdir alice-work &&
+ (
+ cd alice-work &&
+ git init &&
+ >file &&
+ git add . &&
+ git commit -m initial &&
+ git push ../alice-pub main
+ ) &&
+
+ # Project Bob is a fork of project Alice
+ mkdir bob-pub &&
+ (
+ cd bob-pub &&
+ GIT_DIR=. git init &&
+ mkdir -p objects/info &&
+ echo ../../alice-pub/objects >objects/info/alternates
+ ) &&
+ git clone alice-pub bob-work &&
+ (
+ cd bob-work &&
+ git push ../bob-pub main
+ )
+'
+
+test_expect_success 'alice works and pushes' '
+ (
+ cd alice-work &&
+ echo more >file &&
+ git commit -a -m second &&
+ git push ../alice-pub :
+ )
+'
+
+test_expect_success 'bob fetches from alice, works and pushes' '
+ (
+ # Bob acquires what Alice did in his work tree first.
+ # Even though these objects are not directly in
+ # the public repository of Bob, this push does not
+ # need to send the commit Bob received from Alice
+ # to his public repository, as all the object Alice
+ # has at her public repository are available to it
+ # via its alternates.
+ cd bob-work &&
+ git pull ../alice-pub main &&
+ echo more bob >file &&
+ git commit -a -m third &&
+ git push ../bob-pub :
+ ) &&
+
+ # Check that the second commit by Alice is not sent
+ # to ../bob-pub
+ (
+ cd bob-pub &&
+ second=$(git rev-parse HEAD^) &&
+ rm -f objects/info/alternates &&
+ test_must_fail git cat-file -t $second &&
+ echo ../../alice-pub/objects >objects/info/alternates
+ )
+'
+
+test_expect_success 'clean-up in case the previous failed' '
+ (
+ cd bob-pub &&
+ echo ../../alice-pub/objects >objects/info/alternates
+ )
+'
+
+test_expect_success 'alice works and pushes again' '
+ (
+ # Alice does not care what Bob does. She does not
+ # even have to be aware of his existence. She just
+ # keeps working and pushing
+ cd alice-work &&
+ echo more alice >file &&
+ git commit -a -m fourth &&
+ git push ../alice-pub :
+ )
+'
+
+test_expect_success 'bob works and pushes' '
+ (
+ # This time Bob does not pull from Alice, and
+ # the main branch at her public repository points
+ # at a commit Bob does not know about. This should
+ # not prevent the push by Bob from succeeding.
+ cd bob-work &&
+ echo yet more bob >file &&
+ git commit -a -m fifth &&
+ git push ../bob-pub :
+ )
+'
+
+test_expect_success 'alice works and pushes yet again' '
+ (
+ # Alice does not care what Bob does. She does not
+ # even have to be aware of his existence. She just
+ # keeps working and pushing
+ cd alice-work &&
+ echo more and more alice >file &&
+ git commit -a -m sixth.1 &&
+ echo more and more alice >>file &&
+ git commit -a -m sixth.2 &&
+ echo more and more alice >>file &&
+ git commit -a -m sixth.3 &&
+ git push ../alice-pub :
+ )
+'
+
+test_expect_success 'bob works and pushes again' '
+ (
+ cd alice-pub &&
+ git cat-file commit main >../bob-work/commit
+ ) &&
+ (
+ # This time Bob does not pull from Alice, and
+ # the main branch at her public repository points
+ # at a commit Bob does not fully know about, but
+ # he happens to have the commit object (but not the
+ # necessary tree) in his repository from Alice.
+ # This should not prevent the push by Bob from
+ # succeeding.
+ cd bob-work &&
+ git hash-object -t commit -w commit &&
+ echo even more bob >file &&
+ git commit -a -m seventh &&
+ git push ../bob-pub :
+ )
+'
+
+test_done
diff --git a/t/t5520-pull.sh b/t/t5520-pull.sh
new file mode 100755
index 0000000..47534f1
--- /dev/null
+++ b/t/t5520-pull.sh
@@ -0,0 +1,831 @@
+#!/bin/sh
+
+test_description='pulling into void'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+modify () {
+ sed -e "$1" "$2" >"$2.x" &&
+ mv "$2.x" "$2"
+}
+
+test_pull_autostash () {
+ expect_parent_num="$1" &&
+ shift &&
+ git reset --hard before-rebase &&
+ echo dirty >new_file &&
+ git add new_file &&
+ git pull "$@" . copy &&
+ test_cmp_rev HEAD^"$expect_parent_num" copy &&
+ echo dirty >expect &&
+ test_cmp expect new_file &&
+ echo "modified again" >expect &&
+ test_cmp expect file
+}
+
+test_pull_autostash_fail () {
+ git reset --hard before-rebase &&
+ echo dirty >new_file &&
+ git add new_file &&
+ test_must_fail git pull "$@" . copy 2>err &&
+ test_grep -E "uncommitted changes.|overwritten by merge:" err
+}
+
+test_expect_success setup '
+ echo file >file &&
+ git add file &&
+ git commit -a -m original
+'
+
+test_expect_success 'pulling into void' '
+ git init cloned &&
+ (
+ cd cloned &&
+ git pull ..
+ ) &&
+ test_path_is_file file &&
+ test_path_is_file cloned/file &&
+ test_cmp file cloned/file
+'
+
+test_expect_success 'pulling into void using main:main' '
+ git init cloned-uho &&
+ (
+ cd cloned-uho &&
+ git pull .. main:main
+ ) &&
+ test_path_is_file file &&
+ test_path_is_file cloned-uho/file &&
+ test_cmp file cloned-uho/file
+'
+
+test_expect_success 'pulling into void does not overwrite untracked files' '
+ git init cloned-untracked &&
+ (
+ cd cloned-untracked &&
+ echo untracked >file &&
+ test_must_fail git pull .. main &&
+ echo untracked >expect &&
+ test_cmp expect file
+ )
+'
+
+test_expect_success 'pulling into void does not overwrite staged files' '
+ git init cloned-staged-colliding &&
+ (
+ cd cloned-staged-colliding &&
+ echo "alternate content" >file &&
+ git add file &&
+ test_must_fail git pull .. main &&
+ echo "alternate content" >expect &&
+ test_cmp expect file &&
+ git cat-file blob :file >file.index &&
+ test_cmp expect file.index
+ )
+'
+
+test_expect_success 'pulling into void does not remove new staged files' '
+ git init cloned-staged-new &&
+ (
+ cd cloned-staged-new &&
+ echo "new tracked file" >newfile &&
+ git add newfile &&
+ git pull .. main &&
+ echo "new tracked file" >expect &&
+ test_cmp expect newfile &&
+ git cat-file blob :newfile >newfile.index &&
+ test_cmp expect newfile.index
+ )
+'
+
+test_expect_success 'pulling into void must not create an octopus' '
+ git init cloned-octopus &&
+ (
+ cd cloned-octopus &&
+ test_must_fail git pull .. main main &&
+ test_path_is_missing file
+ )
+'
+
+test_expect_success 'test . as a remote' '
+ git branch copy main &&
+ git config branch.copy.remote . &&
+ git config branch.copy.merge refs/heads/main &&
+ echo updated >file &&
+ git commit -a -m updated &&
+ git checkout copy &&
+ echo file >expect &&
+ test_cmp expect file &&
+ git pull &&
+ echo updated >expect &&
+ test_cmp expect file &&
+ git reflog -1 >reflog.actual &&
+ sed "s/^[0-9a-f][0-9a-f]*/OBJID/" reflog.actual >reflog.fuzzy &&
+ echo "OBJID HEAD@{0}: pull: Fast-forward" >reflog.expected &&
+ test_cmp reflog.expected reflog.fuzzy
+'
+
+test_expect_success 'the default remote . should not break explicit pull' '
+ git checkout -b second main^ &&
+ echo modified >file &&
+ git commit -a -m modified &&
+ git checkout copy &&
+ git reset --hard HEAD^ &&
+ echo file >expect &&
+ test_cmp expect file &&
+ git pull --no-rebase . second &&
+ echo modified >expect &&
+ test_cmp expect file &&
+ git reflog -1 >reflog.actual &&
+ sed "s/^[0-9a-f][0-9a-f]*/OBJID/" reflog.actual >reflog.fuzzy &&
+ echo "OBJID HEAD@{0}: pull --no-rebase . second: Fast-forward" >reflog.expected &&
+ test_cmp reflog.expected reflog.fuzzy
+'
+
+test_expect_success 'fail if wildcard spec does not match any refs' '
+ git checkout -b test copy^ &&
+ test_when_finished "git checkout -f copy && git branch -D test" &&
+ echo file >expect &&
+ test_cmp expect file &&
+ test_must_fail git pull . "refs/nonexisting1/*:refs/nonexisting2/*" 2>err &&
+ test_grep "no candidates for merging" err &&
+ test_cmp expect file
+'
+
+test_expect_success 'fail if no branches specified with non-default remote' '
+ git remote add test_remote . &&
+ test_when_finished "git remote remove test_remote" &&
+ git checkout -b test copy^ &&
+ test_when_finished "git checkout -f copy && git branch -D test" &&
+ echo file >expect &&
+ test_cmp expect file &&
+ test_config branch.test.remote origin &&
+ test_must_fail git pull test_remote 2>err &&
+ test_grep "specify a branch on the command line" err &&
+ test_cmp expect file
+'
+
+test_expect_success 'fail if not on a branch' '
+ git remote add origin . &&
+ test_when_finished "git remote remove origin" &&
+ git checkout HEAD^ &&
+ test_when_finished "git checkout -f copy" &&
+ echo file >expect &&
+ test_cmp expect file &&
+ test_must_fail git pull 2>err &&
+ test_grep "not currently on a branch" err &&
+ test_cmp expect file
+'
+
+test_expect_success 'fail if no configuration for current branch' '
+ git remote add test_remote . &&
+ test_when_finished "git remote remove test_remote" &&
+ git checkout -b test copy^ &&
+ test_when_finished "git checkout -f copy && git branch -D test" &&
+ test_config branch.test.remote test_remote &&
+ echo file >expect &&
+ test_cmp expect file &&
+ test_must_fail git pull 2>err &&
+ test_grep "no tracking information" err &&
+ test_cmp expect file
+'
+
+test_expect_success 'pull --all: fail if no configuration for current branch' '
+ git remote add test_remote . &&
+ test_when_finished "git remote remove test_remote" &&
+ git checkout -b test copy^ &&
+ test_when_finished "git checkout -f copy && git branch -D test" &&
+ test_config branch.test.remote test_remote &&
+ echo file >expect &&
+ test_cmp expect file &&
+ test_must_fail git pull --all 2>err &&
+ test_grep "There is no tracking information" err &&
+ test_cmp expect file
+'
+
+test_expect_success 'fail if upstream branch does not exist' '
+ git checkout -b test copy^ &&
+ test_when_finished "git checkout -f copy && git branch -D test" &&
+ test_config branch.test.remote . &&
+ test_config branch.test.merge refs/heads/nonexisting &&
+ echo file >expect &&
+ test_cmp expect file &&
+ test_must_fail git pull 2>err &&
+ test_grep "no such ref was fetched" err &&
+ test_cmp expect file
+'
+
+test_expect_success 'fetch upstream branch even if refspec excludes it' '
+ # the branch names are not important here except that
+ # the first one must not be a prefix of the second,
+ # since otherwise the ref-prefix protocol extension
+ # would match both
+ git branch in-refspec HEAD^ &&
+ git branch not-in-refspec HEAD &&
+ git init -b in-refspec downstream &&
+ git -C downstream remote add -t in-refspec origin "file://$(pwd)/.git" &&
+ git -C downstream config branch.in-refspec.remote origin &&
+ git -C downstream config branch.in-refspec.merge refs/heads/not-in-refspec &&
+ git -C downstream pull &&
+ git rev-parse --verify not-in-refspec >expect &&
+ git -C downstream rev-parse --verify HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'fail if the index has unresolved entries' '
+ git checkout -b third second^ &&
+ test_when_finished "git checkout -f copy && git branch -D third" &&
+ echo file >expect &&
+ test_cmp expect file &&
+ test_commit modified2 file &&
+ git ls-files -u >unmerged &&
+ test_must_be_empty unmerged &&
+ test_must_fail git pull --no-rebase . second &&
+ git ls-files -u >unmerged &&
+ test_file_not_empty unmerged &&
+ cp file expected &&
+ test_must_fail git pull . second 2>err &&
+ test_grep "Pulling is not possible because you have unmerged files." err &&
+ test_cmp expected file &&
+ git add file &&
+ git ls-files -u >unmerged &&
+ test_must_be_empty unmerged &&
+ test_must_fail git pull . second 2>err &&
+ test_grep "You have not concluded your merge" err &&
+ test_cmp expected file
+'
+
+test_expect_success 'fast-forwards working tree if branch head is updated' '
+ git checkout -b third second^ &&
+ test_when_finished "git checkout -f copy && git branch -D third" &&
+ echo file >expect &&
+ test_cmp expect file &&
+ git pull . second:third 2>err &&
+ test_grep "fetch updated the current branch head" err &&
+ echo modified >expect &&
+ test_cmp expect file &&
+ test_cmp_rev third second
+'
+
+test_expect_success 'fast-forward fails with conflicting work tree' '
+ git checkout -b third second^ &&
+ test_when_finished "git checkout -f copy && git branch -D third" &&
+ echo file >expect &&
+ test_cmp expect file &&
+ echo conflict >file &&
+ test_must_fail git pull . second:third 2>err &&
+ test_grep "Cannot fast-forward your working tree" err &&
+ echo conflict >expect &&
+ test_cmp expect file &&
+ test_cmp_rev third second
+'
+
+test_expect_success '--rebase' '
+ git branch to-rebase &&
+ echo modified again >file &&
+ git commit -m file file &&
+ git checkout to-rebase &&
+ echo new >file2 &&
+ git add file2 &&
+ git commit -m "new file" &&
+ git tag before-rebase &&
+ git pull --rebase . copy &&
+ test_cmp_rev HEAD^ copy &&
+ echo new >expect &&
+ git show HEAD:file2 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--rebase (merge) fast forward' '
+ git reset --hard before-rebase &&
+ git checkout -b ff &&
+ echo another modification >file &&
+ git commit -m third file &&
+
+ git checkout to-rebase &&
+ git -c rebase.backend=merge pull --rebase . ff &&
+ test_cmp_rev HEAD ff &&
+
+ # The above only validates the result. Did we actually bypass rebase?
+ git reflog -1 >reflog.actual &&
+ sed "s/^[0-9a-f][0-9a-f]*/OBJID/" reflog.actual >reflog.fuzzy &&
+ echo "OBJID HEAD@{0}: pull --rebase . ff: Fast-forward" >reflog.expected &&
+ test_cmp reflog.expected reflog.fuzzy
+'
+
+test_expect_success '--rebase (am) fast forward' '
+ git reset --hard before-rebase &&
+
+ git -c rebase.backend=apply pull --rebase . ff &&
+ test_cmp_rev HEAD ff &&
+
+ # The above only validates the result. Did we actually bypass rebase?
+ git reflog -1 >reflog.actual &&
+ sed "s/^[0-9a-f][0-9a-f]*/OBJID/" reflog.actual >reflog.fuzzy &&
+ echo "OBJID HEAD@{0}: pull --rebase . ff: Fast-forward" >reflog.expected &&
+ test_cmp reflog.expected reflog.fuzzy
+'
+
+test_expect_success '--rebase --autostash fast forward' '
+ test_when_finished "
+ git reset --hard
+ git checkout to-rebase
+ git branch -D to-rebase-ff
+ git branch -D behind" &&
+ git branch behind &&
+ git checkout -b to-rebase-ff &&
+ echo another modification >>file &&
+ git add file &&
+ git commit -m mod &&
+
+ git checkout behind &&
+ echo dirty >file &&
+ git pull --rebase --autostash . to-rebase-ff &&
+ test_cmp_rev HEAD to-rebase-ff
+'
+
+test_expect_success '--rebase with rebase.autostash succeeds on ff' '
+ test_when_finished "rm -fr src dst actual" &&
+ git init src &&
+ test_commit -C src "initial" file "content" &&
+ git clone src dst &&
+ test_commit -C src --printf "more_content" file "more content\ncontent\n" &&
+ echo "dirty" >>dst/file &&
+ test_config -C dst rebase.autostash true &&
+ git -C dst pull --rebase >actual 2>&1 &&
+ grep -q "Fast-forward" actual &&
+ grep -q "Applied autostash." actual
+'
+
+test_expect_success '--rebase with conflicts shows advice' '
+ test_when_finished "git rebase --abort; git checkout -f to-rebase" &&
+ git checkout -b seq &&
+ test_seq 5 >seq.txt &&
+ git add seq.txt &&
+ test_tick &&
+ git commit -m "Add seq.txt" &&
+ echo 6 >>seq.txt &&
+ test_tick &&
+ git commit -m "Append to seq.txt" seq.txt &&
+ git checkout -b with-conflicts HEAD^ &&
+ echo conflicting >>seq.txt &&
+ test_tick &&
+ git commit -m "Create conflict" seq.txt &&
+ test_must_fail git pull --rebase . seq 2>err >out &&
+ test_grep "Resolve all conflicts manually" err
+'
+
+test_expect_success 'failed --rebase shows advice' '
+ test_when_finished "git rebase --abort; git checkout -f to-rebase" &&
+ git checkout -b diverging &&
+ test_commit attributes .gitattributes "* text=auto" attrs &&
+ sha1="$(printf "1\\r\\n" | git hash-object -w --stdin)" &&
+ git update-index --cacheinfo 0644 $sha1 file &&
+ git commit -m v1-with-cr &&
+ # force checkout because `git reset --hard` will not leave clean `file`
+ git checkout -f -b fails-to-rebase HEAD^ &&
+ test_commit v2-without-cr file "2" file2-lf &&
+ test_must_fail git pull --rebase . diverging 2>err >out &&
+ test_grep "Resolve all conflicts manually" err
+'
+
+test_expect_success '--rebase fails with multiple branches' '
+ git reset --hard before-rebase &&
+ test_must_fail git pull --rebase . copy main 2>err &&
+ test_cmp_rev HEAD before-rebase &&
+ test_grep "Cannot rebase onto multiple branches" err &&
+ echo modified >expect &&
+ git show HEAD:file >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'pull --rebase succeeds with dirty working directory and rebase.autostash set' '
+ test_config rebase.autostash true &&
+ test_pull_autostash 1 --rebase
+'
+
+test_expect_success 'pull --rebase --autostash & rebase.autostash=true' '
+ test_config rebase.autostash true &&
+ test_pull_autostash 1 --rebase --autostash
+'
+
+test_expect_success 'pull --rebase --autostash & rebase.autostash=false' '
+ test_config rebase.autostash false &&
+ test_pull_autostash 1 --rebase --autostash
+'
+
+test_expect_success 'pull --rebase --autostash & rebase.autostash unset' '
+ test_unconfig rebase.autostash &&
+ test_pull_autostash 1 --rebase --autostash
+'
+
+test_expect_success 'pull --rebase --no-autostash & rebase.autostash=true' '
+ test_config rebase.autostash true &&
+ test_pull_autostash_fail --rebase --no-autostash
+'
+
+test_expect_success 'pull --rebase --no-autostash & rebase.autostash=false' '
+ test_config rebase.autostash false &&
+ test_pull_autostash_fail --rebase --no-autostash
+'
+
+test_expect_success 'pull --rebase --no-autostash & rebase.autostash unset' '
+ test_unconfig rebase.autostash &&
+ test_pull_autostash_fail --rebase --no-autostash
+'
+
+test_expect_success 'pull succeeds with dirty working directory and merge.autostash set' '
+ test_config merge.autostash true &&
+ test_pull_autostash 2 --no-rebase
+'
+
+test_expect_success 'pull --autostash & merge.autostash=true' '
+ test_config merge.autostash true &&
+ test_pull_autostash 2 --autostash --no-rebase
+'
+
+test_expect_success 'pull --autostash & merge.autostash=false' '
+ test_config merge.autostash false &&
+ test_pull_autostash 2 --autostash --no-rebase
+'
+
+test_expect_success 'pull --autostash & merge.autostash unset' '
+ test_unconfig merge.autostash &&
+ test_pull_autostash 2 --autostash --no-rebase
+'
+
+test_expect_success 'pull --no-autostash & merge.autostash=true' '
+ test_config merge.autostash true &&
+ test_pull_autostash_fail --no-autostash --no-rebase
+'
+
+test_expect_success 'pull --no-autostash & merge.autostash=false' '
+ test_config merge.autostash false &&
+ test_pull_autostash_fail --no-autostash --no-rebase
+'
+
+test_expect_success 'pull --no-autostash & merge.autostash unset' '
+ test_unconfig merge.autostash &&
+ test_pull_autostash_fail --no-autostash --no-rebase
+'
+
+test_expect_success 'pull.rebase' '
+ git reset --hard before-rebase &&
+ test_config pull.rebase true &&
+ git pull . copy &&
+ test_cmp_rev HEAD^ copy &&
+ echo new >expect &&
+ git show HEAD:file2 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'pull --autostash & pull.rebase=true' '
+ test_config pull.rebase true &&
+ test_pull_autostash 1 --autostash
+'
+
+test_expect_success 'pull --no-autostash & pull.rebase=true' '
+ test_config pull.rebase true &&
+ test_pull_autostash_fail --no-autostash
+'
+
+test_expect_success 'branch.to-rebase.rebase' '
+ git reset --hard before-rebase &&
+ test_config branch.to-rebase.rebase true &&
+ git pull . copy &&
+ test_cmp_rev HEAD^ copy &&
+ echo new >expect &&
+ git show HEAD:file2 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'branch.to-rebase.rebase should override pull.rebase' '
+ git reset --hard before-rebase &&
+ test_config pull.rebase true &&
+ test_config branch.to-rebase.rebase false &&
+ git pull . copy &&
+ test_cmp_rev ! HEAD^ copy &&
+ echo new >expect &&
+ git show HEAD:file2 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'pull --rebase warns on --verify-signatures' '
+ git reset --hard before-rebase &&
+ git pull --rebase --verify-signatures . copy 2>err &&
+ test_cmp_rev HEAD^ copy &&
+ echo new >expect &&
+ git show HEAD:file2 >actual &&
+ test_cmp expect actual &&
+ test_grep "ignoring --verify-signatures for rebase" err
+'
+
+test_expect_success 'pull --rebase does not warn on --no-verify-signatures' '
+ git reset --hard before-rebase &&
+ git pull --rebase --no-verify-signatures . copy 2>err &&
+ test_cmp_rev HEAD^ copy &&
+ echo new >expect &&
+ git show HEAD:file2 >actual &&
+ test_cmp expect actual &&
+ test_grep ! "verify-signatures" err
+'
+
+# add a feature branch, keep-merge, that is merged into main, so the
+# test can try preserving the merge commit (or not) with various
+# --rebase flags/pull.rebase settings.
+test_expect_success 'preserve merge setup' '
+ git reset --hard before-rebase &&
+ git checkout -b keep-merge second^ &&
+ test_commit file3 &&
+ git checkout to-rebase &&
+ git merge keep-merge &&
+ git tag before-preserve-rebase
+'
+
+test_expect_success 'pull.rebase=false create a new merge commit' '
+ git reset --hard before-preserve-rebase &&
+ test_config pull.rebase false &&
+ git pull . copy &&
+ test_cmp_rev HEAD^1 before-preserve-rebase &&
+ test_cmp_rev HEAD^2 copy &&
+ echo file3 >expect &&
+ git show HEAD:file3.t >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'pull.rebase=true flattens keep-merge' '
+ git reset --hard before-preserve-rebase &&
+ test_config pull.rebase true &&
+ git pull . copy &&
+ test_cmp_rev HEAD^^ copy &&
+ echo file3 >expect &&
+ git show HEAD:file3.t >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'pull.rebase=1 is treated as true and flattens keep-merge' '
+ git reset --hard before-preserve-rebase &&
+ test_config pull.rebase 1 &&
+ git pull . copy &&
+ test_cmp_rev HEAD^^ copy &&
+ echo file3 >expect &&
+ git show HEAD:file3.t >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'pull.rebase=interactive' '
+ write_script "$TRASH_DIRECTORY/fake-editor" <<-\EOF &&
+ echo I was here >fake.out &&
+ false
+ EOF
+ test_set_editor "$TRASH_DIRECTORY/fake-editor" &&
+ test_when_finished "test_might_fail git rebase --abort" &&
+ test_must_fail git pull --rebase=interactive . copy &&
+ echo "I was here" >expect &&
+ test_cmp expect fake.out
+'
+
+test_expect_success 'pull --rebase=i' '
+ write_script "$TRASH_DIRECTORY/fake-editor" <<-\EOF &&
+ echo I was here, too >fake.out &&
+ false
+ EOF
+ test_set_editor "$TRASH_DIRECTORY/fake-editor" &&
+ test_when_finished "test_might_fail git rebase --abort" &&
+ test_must_fail git pull --rebase=i . copy &&
+ echo "I was here, too" >expect &&
+ test_cmp expect fake.out
+'
+
+test_expect_success 'pull.rebase=invalid fails' '
+ git reset --hard before-preserve-rebase &&
+ test_config pull.rebase invalid &&
+ test_must_fail git pull . copy
+'
+
+test_expect_success '--rebase=false create a new merge commit' '
+ git reset --hard before-preserve-rebase &&
+ test_config pull.rebase true &&
+ git pull --rebase=false . copy &&
+ test_cmp_rev HEAD^1 before-preserve-rebase &&
+ test_cmp_rev HEAD^2 copy &&
+ echo file3 >expect &&
+ git show HEAD:file3.t >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--rebase=true rebases and flattens keep-merge' '
+ git reset --hard before-preserve-rebase &&
+ test_config pull.rebase merges &&
+ git pull --rebase=true . copy &&
+ test_cmp_rev HEAD^^ copy &&
+ echo file3 >expect &&
+ git show HEAD:file3.t >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--rebase=invalid fails' '
+ git reset --hard before-preserve-rebase &&
+ test_must_fail git pull --rebase=invalid . copy
+'
+
+test_expect_success '--rebase overrides pull.rebase=merges and flattens keep-merge' '
+ git reset --hard before-preserve-rebase &&
+ test_config pull.rebase merges &&
+ git pull --rebase . copy &&
+ test_cmp_rev HEAD^^ copy &&
+ echo file3 >expect &&
+ git show HEAD:file3.t >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--rebase with rebased upstream' '
+ git remote add -f me . &&
+ git checkout copy &&
+ git tag copy-orig &&
+ git reset --hard HEAD^ &&
+ echo conflicting modification >file &&
+ git commit -m conflict file &&
+ git checkout to-rebase &&
+ echo file >file2 &&
+ git commit -m to-rebase file2 &&
+ git tag to-rebase-orig &&
+ git pull --rebase me copy &&
+ echo "conflicting modification" >expect &&
+ test_cmp expect file &&
+ echo file >expect &&
+ test_cmp expect file2
+'
+
+test_expect_success '--rebase -f with rebased upstream' '
+ test_when_finished "test_might_fail git rebase --abort" &&
+ git reset --hard to-rebase-orig &&
+ git pull --rebase -f me copy &&
+ echo "conflicting modification" >expect &&
+ test_cmp expect file &&
+ echo file >expect &&
+ test_cmp expect file2
+'
+
+test_expect_success '--rebase with rebased default upstream' '
+ git update-ref refs/remotes/me/copy copy-orig &&
+ git checkout --track -b to-rebase2 me/copy &&
+ git reset --hard to-rebase-orig &&
+ git pull --rebase &&
+ echo "conflicting modification" >expect &&
+ test_cmp expect file &&
+ echo file >expect &&
+ test_cmp expect file2
+'
+
+test_expect_success 'rebased upstream + fetch + pull --rebase' '
+
+ git update-ref refs/remotes/me/copy copy-orig &&
+ git reset --hard to-rebase-orig &&
+ git checkout --track -b to-rebase3 me/copy &&
+ git reset --hard to-rebase-orig &&
+ git fetch &&
+ git pull --rebase &&
+ echo "conflicting modification" >expect &&
+ test_cmp expect file &&
+ echo file >expect &&
+ test_cmp expect file2
+
+'
+
+test_expect_success 'pull --rebase dies early with dirty working directory' '
+ git checkout to-rebase &&
+ git update-ref refs/remotes/me/copy copy^ &&
+ COPY="$(git rev-parse --verify me/copy)" &&
+ git rebase --onto $COPY copy &&
+ test_config branch.to-rebase.remote me &&
+ test_config branch.to-rebase.merge refs/heads/copy &&
+ test_config branch.to-rebase.rebase true &&
+ echo dirty >>file &&
+ git add file &&
+ test_must_fail git pull &&
+ test_cmp_rev "$COPY" me/copy &&
+ git checkout HEAD -- file &&
+ git pull &&
+ test_cmp_rev ! "$COPY" me/copy
+'
+
+test_expect_success 'pull --rebase works on branch yet to be born' '
+ git rev-parse main >expect &&
+ mkdir empty_repo &&
+ (
+ cd empty_repo &&
+ git init &&
+ git pull --rebase .. main &&
+ git rev-parse HEAD >../actual
+ ) &&
+ test_cmp expect actual
+'
+
+test_expect_success 'pull --rebase fails on unborn branch with staged changes' '
+ test_when_finished "rm -rf empty_repo2" &&
+ git init empty_repo2 &&
+ (
+ cd empty_repo2 &&
+ echo staged-file >staged-file &&
+ git add staged-file &&
+ echo staged-file >expect &&
+ git ls-files >actual &&
+ test_cmp expect actual &&
+ test_must_fail git pull --rebase .. main 2>err &&
+ git ls-files >actual &&
+ test_cmp expect actual &&
+ git show :staged-file >actual &&
+ test_cmp expect actual &&
+ test_grep "unborn branch with changes added to the index" err
+ )
+'
+
+test_expect_success 'pull --rebase fails on corrupt HEAD' '
+ test_when_finished "rm -rf corrupt" &&
+ git init corrupt &&
+ (
+ cd corrupt &&
+ test_commit one &&
+ git rev-parse --verify HEAD >head &&
+ obj=$(sed "s#^..#&/#" head) &&
+ rm -f .git/objects/$obj &&
+ test_must_fail git pull --rebase
+ )
+'
+
+test_expect_success 'setup for detecting upstreamed changes' '
+ test_create_repo src &&
+ test_commit -C src --printf one stuff "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" &&
+ git clone src dst &&
+ (
+ cd src &&
+ modify s/5/43/ stuff &&
+ git commit -a -m "5->43" &&
+ modify s/6/42/ stuff &&
+ git commit -a -m "Make it bigger"
+ ) &&
+ (
+ cd dst &&
+ modify s/5/43/ stuff &&
+ git commit -a -m "Independent discovery of 5->43"
+ )
+'
+
+test_expect_success 'git pull --rebase detects upstreamed changes' '
+ (
+ cd dst &&
+ git pull --rebase &&
+ git ls-files -u >untracked &&
+ test_must_be_empty untracked
+ )
+'
+
+test_expect_success 'setup for avoiding reapplying old patches' '
+ (
+ cd dst &&
+ test_might_fail git rebase --abort &&
+ git reset --hard origin/main
+ ) &&
+ git clone --bare src src-replace.git &&
+ rm -rf src &&
+ mv src-replace.git src &&
+ (
+ cd dst &&
+ modify s/2/22/ stuff &&
+ git commit -a -m "Change 2" &&
+ modify s/3/33/ stuff &&
+ git commit -a -m "Change 3" &&
+ modify s/4/44/ stuff &&
+ git commit -a -m "Change 4" &&
+ git push &&
+
+ modify s/44/55/ stuff &&
+ git commit --amend -a -m "Modified Change 4"
+ )
+'
+
+test_expect_success 'git pull --rebase does not reapply old patches' '
+ (
+ cd dst &&
+ test_must_fail git pull --rebase &&
+ cat .git/rebase-merge/done .git/rebase-merge/git-rebase-todo >work &&
+ grep -v -e \# -e ^$ work >patches &&
+ test_line_count = 1 patches &&
+ rm -f work
+ )
+'
+
+test_expect_success 'git pull --rebase against local branch' '
+ git checkout -b copy2 to-rebase-orig &&
+ git pull --rebase . to-rebase &&
+ echo "conflicting modification" >expect &&
+ test_cmp expect file &&
+ echo file >expect &&
+ test_cmp expect file2
+'
+
+test_done
diff --git a/t/t5521-pull-options.sh b/t/t5521-pull-options.sh
new file mode 100755
index 0000000..db00c43
--- /dev/null
+++ b/t/t5521-pull-options.sh
@@ -0,0 +1,256 @@
+#!/bin/sh
+
+test_description='pull options'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ mkdir parent &&
+ (cd parent && git init &&
+ echo one >file && git add file &&
+ git commit -m one)
+'
+
+test_expect_success 'git pull -q --no-rebase' '
+ mkdir clonedq &&
+ (cd clonedq && git init &&
+ git pull -q --no-rebase "../parent" >out 2>err &&
+ test_must_be_empty err &&
+ test_must_be_empty out)
+'
+
+test_expect_success 'git pull -q --rebase' '
+ mkdir clonedqrb &&
+ (cd clonedqrb && git init &&
+ git pull -q --rebase "../parent" >out 2>err &&
+ test_must_be_empty err &&
+ test_must_be_empty out &&
+ git pull -q --rebase "../parent" >out 2>err &&
+ test_must_be_empty err &&
+ test_must_be_empty out)
+'
+
+test_expect_success 'git pull --no-rebase' '
+ mkdir cloned &&
+ (cd cloned && git init &&
+ git pull --no-rebase "../parent" >out 2>err &&
+ test -s err &&
+ test_must_be_empty out)
+'
+
+test_expect_success 'git pull --rebase' '
+ mkdir clonedrb &&
+ (cd clonedrb && git init &&
+ git pull --rebase "../parent" >out 2>err &&
+ test -s err &&
+ test_must_be_empty out)
+'
+
+test_expect_success 'git pull -v --no-rebase' '
+ mkdir clonedv &&
+ (cd clonedv && git init &&
+ git pull -v --no-rebase "../parent" >out 2>err &&
+ test -s err &&
+ test_must_be_empty out)
+'
+
+test_expect_success 'git pull -v --rebase' '
+ mkdir clonedvrb &&
+ (cd clonedvrb && git init &&
+ git pull -v --rebase "../parent" >out 2>err &&
+ test -s err &&
+ test_must_be_empty out)
+'
+
+test_expect_success 'git pull -v -q --no-rebase' '
+ mkdir clonedvq &&
+ (cd clonedvq && git init &&
+ git pull -v -q --no-rebase "../parent" >out 2>err &&
+ test_must_be_empty out &&
+ test_must_be_empty err)
+'
+
+test_expect_success 'git pull -q -v --no-rebase' '
+ mkdir clonedqv &&
+ (cd clonedqv && git init &&
+ git pull -q -v --no-rebase "../parent" >out 2>err &&
+ test_must_be_empty out &&
+ test -s err)
+'
+test_expect_success 'git pull --cleanup errors early on invalid argument' '
+ mkdir clonedcleanup &&
+ (cd clonedcleanup && git init &&
+ test_must_fail git pull --no-rebase --cleanup invalid "../parent" >out 2>err &&
+ test_must_be_empty out &&
+ test -s err)
+'
+
+test_expect_success 'git pull --no-write-fetch-head fails' '
+ mkdir clonedwfh &&
+ (cd clonedwfh && git init &&
+ test_expect_code 129 git pull --no-write-fetch-head "../parent" >out 2>err &&
+ test_must_be_empty out &&
+ test_grep "no-write-fetch-head" err)
+'
+
+test_expect_success 'git pull --force' '
+ mkdir clonedoldstyle &&
+ (cd clonedoldstyle && git init &&
+ cat >>.git/config <<-\EOF &&
+ [remote "one"]
+ url = ../parent
+ fetch = refs/heads/main:refs/heads/mirror
+ [remote "two"]
+ url = ../parent
+ fetch = refs/heads/main:refs/heads/origin
+ [branch "main"]
+ remote = two
+ merge = refs/heads/main
+ EOF
+ git pull two &&
+ test_commit A &&
+ git branch -f origin &&
+ git pull --no-rebase --all --force
+ )
+'
+
+test_expect_success 'git pull --all' '
+ mkdir clonedmulti &&
+ (cd clonedmulti && git init &&
+ cat >>.git/config <<-\EOF &&
+ [remote "one"]
+ url = ../parent
+ fetch = refs/heads/*:refs/remotes/one/*
+ [remote "two"]
+ url = ../parent
+ fetch = refs/heads/*:refs/remotes/two/*
+ [branch "main"]
+ remote = one
+ merge = refs/heads/main
+ EOF
+ git pull --all
+ )
+'
+
+test_expect_success 'git pull --dry-run' '
+ test_when_finished "rm -rf clonedry" &&
+ git init clonedry &&
+ (
+ cd clonedry &&
+ git pull --dry-run ../parent &&
+ test_path_is_missing .git/FETCH_HEAD &&
+ test_ref_missing refs/heads/main &&
+ test_path_is_missing .git/index &&
+ test_path_is_missing file
+ )
+'
+
+test_expect_success 'git pull --all --dry-run' '
+ test_when_finished "rm -rf cloneddry" &&
+ git init clonedry &&
+ (
+ cd clonedry &&
+ git remote add origin ../parent &&
+ git pull --all --dry-run &&
+ test_path_is_missing .git/FETCH_HEAD &&
+ test_ref_missing refs/remotes/origin/main &&
+ test_path_is_missing .git/index &&
+ test_path_is_missing file
+ )
+'
+
+test_expect_success 'git pull --allow-unrelated-histories' '
+ test_when_finished "rm -fr src dst" &&
+ git init src &&
+ (
+ cd src &&
+ test_commit one &&
+ test_commit two
+ ) &&
+ git clone src dst &&
+ (
+ cd src &&
+ git checkout --orphan side HEAD^ &&
+ test_commit three
+ ) &&
+ (
+ cd dst &&
+ test_must_fail git pull ../src side &&
+ git pull --no-rebase --allow-unrelated-histories ../src side
+ )
+'
+
+test_expect_success 'git pull does not add a sign-off line' '
+ test_when_finished "rm -fr src dst actual" &&
+ git init src &&
+ test_commit -C src one &&
+ git clone src dst &&
+ test_commit -C src two &&
+ git -C dst pull --no-ff &&
+ git -C dst show -s --pretty="format:%(trailers)" HEAD >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'git pull --no-signoff does not add sign-off line' '
+ test_when_finished "rm -fr src dst actual" &&
+ git init src &&
+ test_commit -C src one &&
+ git clone src dst &&
+ test_commit -C src two &&
+ git -C dst pull --no-signoff --no-ff &&
+ git -C dst show -s --pretty="format:%(trailers)" HEAD >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'git pull --signoff add a sign-off line' '
+ test_when_finished "rm -fr src dst expected actual" &&
+ echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>" >expected &&
+ git init src &&
+ test_commit -C src one &&
+ git clone src dst &&
+ test_commit -C src two &&
+ git -C dst pull --signoff --no-ff &&
+ git -C dst show -s --pretty="format:%(trailers)" HEAD >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'git pull --no-signoff flag cancels --signoff flag' '
+ test_when_finished "rm -fr src dst actual" &&
+ git init src &&
+ test_commit -C src one &&
+ git clone src dst &&
+ test_commit -C src two &&
+ git -C dst pull --signoff --no-signoff --no-ff &&
+ git -C dst show -s --pretty="format:%(trailers)" HEAD >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'git pull --no-verify flag passed to merge' '
+ test_when_finished "rm -fr src dst actual" &&
+ git init src &&
+ test_commit -C src one &&
+ git clone src dst &&
+ test_hook -C dst commit-msg <<-\EOF &&
+ false
+ EOF
+ test_commit -C src two &&
+ git -C dst pull --no-ff --no-verify
+'
+
+test_expect_success 'git pull --no-verify --verify passed to merge' '
+ test_when_finished "rm -fr src dst actual" &&
+ git init src &&
+ test_commit -C src one &&
+ git clone src dst &&
+ test_hook -C dst commit-msg <<-\EOF &&
+ false
+ EOF
+ test_commit -C src two &&
+ test_must_fail git -C dst pull --no-ff --no-verify --verify
+'
+
+test_done
diff --git a/t/t5522-pull-symlink.sh b/t/t5522-pull-symlink.sh
new file mode 100755
index 0000000..cc5496e
--- /dev/null
+++ b/t/t5522-pull-symlink.sh
@@ -0,0 +1,87 @@
+#!/bin/sh
+
+test_description='pulling from symlinked subdir'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# The scenario we are building:
+#
+# trash\ directory/
+# clone-repo/
+# subdir/
+# bar
+# subdir-link -> clone-repo/subdir/
+#
+# The working directory is subdir-link.
+
+test_expect_success SYMLINKS setup '
+ mkdir subdir &&
+ echo file >subdir/file &&
+ git add subdir/file &&
+ git commit -q -m file &&
+ git clone -q . clone-repo &&
+ ln -s clone-repo/subdir/ subdir-link &&
+ (
+ cd clone-repo &&
+ git config receive.denyCurrentBranch warn
+ ) &&
+ git config receive.denyCurrentBranch warn
+'
+
+# Demonstrate that things work if we just avoid the symlink
+#
+test_expect_success SYMLINKS 'pulling from real subdir' '
+ (
+ echo real >subdir/file &&
+ git commit -m real subdir/file &&
+ cd clone-repo/subdir/ &&
+ git pull &&
+ test real = $(cat file)
+ )
+'
+
+# From subdir-link, pulling should work as it does from
+# clone-repo/subdir/.
+#
+# Instead, the error pull gave was:
+#
+# fatal: 'origin': unable to chdir or not a git archive
+# fatal: The remote end hung up unexpectedly
+#
+# because git would find the .git/config for the "trash directory"
+# repo, not for the clone-repo repo. The "trash directory" repo
+# had no entry for origin. Git found the wrong .git because
+# git rev-parse --show-cdup printed a path relative to
+# clone-repo/subdir/, not subdir-link/. Git rev-parse --show-cdup
+# used the correct .git, but when the git pull shell script did
+# "cd $(git rev-parse --show-cdup)", it ended up in the wrong
+# directory. A POSIX shell's "cd" works a little differently
+# than chdir() in C; "cd -P" is much closer to chdir().
+#
+test_expect_success SYMLINKS 'pulling from symlinked subdir' '
+ (
+ echo link >subdir/file &&
+ git commit -m link subdir/file &&
+ cd subdir-link/ &&
+ git pull &&
+ test link = $(cat file)
+ )
+'
+
+# Prove that the remote end really is a repo, and other commands
+# work fine in this context. It's just that "git pull" breaks.
+#
+test_expect_success SYMLINKS 'pushing from symlinked subdir' '
+ (
+ cd subdir-link/ &&
+ echo push >file &&
+ git commit -m push ./file &&
+ git push
+ ) &&
+ echo push >expect &&
+ git show HEAD:subdir/file >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t5523-push-upstream.sh b/t/t5523-push-upstream.sh
new file mode 100755
index 0000000..1f859ad
--- /dev/null
+++ b/t/t5523-push-upstream.sh
@@ -0,0 +1,138 @@
+#!/bin/sh
+
+test_description='push with --set-upstream'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-terminal.sh
+
+ensure_fresh_upstream() {
+ rm -rf parent && git init --bare parent
+}
+
+test_expect_success 'setup bare parent' '
+ ensure_fresh_upstream &&
+ git remote add upstream parent
+'
+
+test_expect_success 'setup local commit' '
+ echo content >file &&
+ git add file &&
+ git commit -m one
+'
+
+check_config() {
+ (echo $2; echo $3) >expect.$1
+ (git config branch.$1.remote
+ git config branch.$1.merge) >actual.$1
+ test_cmp expect.$1 actual.$1
+}
+
+test_expect_success 'push -u main:main' '
+ git push -u upstream main:main &&
+ check_config main upstream refs/heads/main
+'
+
+test_expect_success 'push -u main:other' '
+ git push -u upstream main:other &&
+ check_config main upstream refs/heads/other
+'
+
+test_expect_success 'push -u --dry-run main:otherX' '
+ git push -u --dry-run upstream main:otherX &&
+ check_config main upstream refs/heads/other
+'
+
+test_expect_success 'push -u topic_2:topic_2' '
+ git branch topic_2 &&
+ git push -u upstream topic_2:topic_2 &&
+ check_config topic_2 upstream refs/heads/topic_2
+'
+
+test_expect_success 'push -u topic_2:other2' '
+ git push -u upstream topic_2:other2 &&
+ check_config topic_2 upstream refs/heads/other2
+'
+
+test_expect_success 'push -u :topic_2' '
+ git push -u upstream :topic_2 &&
+ check_config topic_2 upstream refs/heads/other2
+'
+
+test_expect_success 'push -u --all(the same behavior with--branches)' '
+ git branch all1 &&
+ git branch all2 &&
+ git push -u --all &&
+ check_config all1 upstream refs/heads/all1 &&
+ check_config all2 upstream refs/heads/all2 &&
+ git config --get-regexp branch.all* > expect &&
+ git config --remove-section branch.all1 &&
+ git config --remove-section branch.all2 &&
+ git push -u --branches &&
+ check_config all1 upstream refs/heads/all1 &&
+ check_config all2 upstream refs/heads/all2 &&
+ git config --get-regexp branch.all* > actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'push -u HEAD' '
+ git checkout -b headbranch &&
+ git push -u upstream HEAD &&
+ check_config headbranch upstream refs/heads/headbranch
+'
+
+test_expect_success TTY 'progress messages go to tty' '
+ ensure_fresh_upstream &&
+
+ test_terminal git push -u upstream main >out 2>err &&
+ test_grep "Writing objects" err
+'
+
+test_expect_success 'progress messages do not go to non-tty' '
+ ensure_fresh_upstream &&
+
+ # skip progress messages, since stderr is non-tty
+ git push -u upstream main >out 2>err &&
+ test_grep ! "Writing objects" err
+'
+
+test_expect_success 'progress messages go to non-tty (forced)' '
+ ensure_fresh_upstream &&
+
+ # force progress messages to stderr, even though it is non-tty
+ git push -u --progress upstream main >out 2>err &&
+ test_grep "Writing objects" err
+'
+
+test_expect_success TTY 'push -q suppresses progress' '
+ ensure_fresh_upstream &&
+
+ test_terminal git push -u -q upstream main >out 2>err &&
+ test_grep ! "Writing objects" err
+'
+
+test_expect_success TTY 'push --no-progress suppresses progress' '
+ ensure_fresh_upstream &&
+
+ test_terminal git push -u --no-progress upstream main >out 2>err &&
+ test_grep ! "Unpacking objects" err &&
+ test_grep ! "Writing objects" err
+'
+
+test_expect_success TTY 'quiet push' '
+ ensure_fresh_upstream &&
+
+ test_terminal git push --quiet --no-progress upstream main 2>&1 | tee output &&
+ test_must_be_empty output
+'
+
+test_expect_success TTY 'quiet push -u' '
+ ensure_fresh_upstream &&
+
+ test_terminal git push --quiet -u --no-progress upstream main 2>&1 | tee output &&
+ test_must_be_empty output
+'
+
+test_done
diff --git a/t/t5524-pull-msg.sh b/t/t5524-pull-msg.sh
new file mode 100755
index 0000000..56716e2
--- /dev/null
+++ b/t/t5524-pull-msg.sh
@@ -0,0 +1,53 @@
+#!/bin/sh
+
+test_description='git pull message generation'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+dollar='$Dollar'
+
+test_expect_success setup '
+ test_commit initial afile original &&
+ git clone . cloned &&
+ (
+ cd cloned &&
+ echo added >bfile &&
+ git add bfile &&
+ test_tick &&
+ git commit -m "add bfile"
+ ) &&
+ test_tick && test_tick &&
+ echo "second" >afile &&
+ git add afile &&
+ git commit -m "second commit" &&
+ echo "original $dollar" >afile &&
+ git add afile &&
+ git commit -m "do not clobber $dollar signs"
+'
+
+test_expect_success pull '
+(
+ cd cloned &&
+ git pull --no-rebase --log &&
+ git log -2 &&
+ git cat-file commit HEAD >result &&
+ grep Dollar result
+)
+'
+
+test_expect_success '--log=1 limits shortlog length' '
+(
+ cd cloned &&
+ git reset --hard HEAD^ &&
+ test "$(cat afile)" = original &&
+ test "$(cat bfile)" = added &&
+ git pull --no-rebase --log=1 &&
+ git log -3 &&
+ git cat-file commit HEAD >result &&
+ grep Dollar result &&
+ ! grep "second commit" result
+)
+'
+
+test_done
diff --git a/t/t5525-fetch-tagopt.sh b/t/t5525-fetch-tagopt.sh
new file mode 100755
index 0000000..3a28f1d
--- /dev/null
+++ b/t/t5525-fetch-tagopt.sh
@@ -0,0 +1,55 @@
+#!/bin/sh
+
+test_description='tagopt variable affects "git fetch" and is overridden by commandline.'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+setup_clone () {
+ git clone --mirror . $1 &&
+ git remote add remote_$1 $1 &&
+ (cd $1 &&
+ git tag tag_$1 &&
+ git branch branch_$1)
+}
+
+test_expect_success setup '
+ test_commit test &&
+ setup_clone one &&
+ git config remote.remote_one.tagopt --no-tags &&
+ setup_clone two &&
+ git config remote.remote_two.tagopt --tags
+ '
+
+test_expect_success "fetch with tagopt=--no-tags does not get tag" '
+ git fetch remote_one &&
+ test_must_fail git show-ref tag_one &&
+ git show-ref remote_one/branch_one
+ '
+
+test_expect_success "fetch --tags with tagopt=--no-tags gets tag" '
+ (
+ cd one &&
+ git branch second_branch_one
+ ) &&
+ git fetch --tags remote_one &&
+ git show-ref tag_one &&
+ git show-ref remote_one/second_branch_one
+ '
+
+test_expect_success "fetch --no-tags with tagopt=--tags does not get tag" '
+ git fetch --no-tags remote_two &&
+ test_must_fail git show-ref tag_two &&
+ git show-ref remote_two/branch_two
+ '
+
+test_expect_success "fetch with tagopt=--tags gets tag" '
+ (
+ cd two &&
+ git branch second_branch_two
+ ) &&
+ git fetch remote_two &&
+ git show-ref tag_two &&
+ git show-ref remote_two/second_branch_two
+ '
+test_done
diff --git a/t/t5526-fetch-submodules.sh b/t/t5526-fetch-submodules.sh
new file mode 100755
index 0000000..26e933f
--- /dev/null
+++ b/t/t5526-fetch-submodules.sh
@@ -0,0 +1,1196 @@
+#!/bin/sh
+# Copyright (c) 2010, Jens Lehmann
+
+test_description='Recursive "git fetch" for submodules'
+
+GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB=1
+export GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB
+
+. ./test-lib.sh
+
+pwd=$(pwd)
+
+write_expected_sub () {
+ NEW_HEAD=$1 &&
+ SUPER_HEAD=$2 &&
+ cat >"$pwd/expect.err.sub" <<-EOF
+ Fetching submodule submodule${SUPER_HEAD:+ at commit $SUPER_HEAD}
+ From $pwd/submodule
+ OLD_HEAD..$NEW_HEAD sub -> origin/sub
+ EOF
+}
+
+write_expected_sub2 () {
+ NEW_HEAD=$1 &&
+ SUPER_HEAD=$2 &&
+ cat >"$pwd/expect.err.sub2" <<-EOF
+ Fetching submodule submodule2${SUPER_HEAD:+ at commit $SUPER_HEAD}
+ From $pwd/submodule2
+ OLD_HEAD..$NEW_HEAD sub2 -> origin/sub2
+ EOF
+}
+
+write_expected_deep () {
+ NEW_HEAD=$1 &&
+ SUB_HEAD=$2 &&
+ cat >"$pwd/expect.err.deep" <<-EOF
+ Fetching submodule submodule/subdir/deepsubmodule${SUB_HEAD:+ at commit $SUB_HEAD}
+ From $pwd/deepsubmodule
+ OLD_HEAD..$NEW_HEAD deep -> origin/deep
+ EOF
+}
+
+write_expected_super () {
+ NEW_HEAD=$1 &&
+ cat >"$pwd/expect.err.super" <<-EOF
+ From $pwd/.
+ OLD_HEAD..$NEW_HEAD super -> origin/super
+ EOF
+}
+
+# For each submodule in the test setup, this creates a commit and writes
+# a file that contains the expected err if that new commit were fetched.
+# These output files get concatenated in the right order by
+# verify_fetch_result().
+add_submodule_commits () {
+ (
+ cd submodule &&
+ echo new >> subfile &&
+ test_tick &&
+ git add subfile &&
+ git commit -m new subfile &&
+ new_head=$(git rev-parse --short HEAD) &&
+ write_expected_sub $new_head
+ ) &&
+ (
+ cd deepsubmodule &&
+ echo new >> deepsubfile &&
+ test_tick &&
+ git add deepsubfile &&
+ git commit -m new deepsubfile &&
+ new_head=$(git rev-parse --short HEAD) &&
+ write_expected_deep $new_head
+ )
+}
+
+# For each superproject in the test setup, update its submodule, add the
+# submodule and create a new commit with the submodule change.
+#
+# This requires add_submodule_commits() to be called first, otherwise
+# the submodules will not have changed and cannot be "git add"-ed.
+add_superproject_commits () {
+ (
+ cd submodule &&
+ (
+ cd subdir/deepsubmodule &&
+ git fetch &&
+ git checkout -q FETCH_HEAD
+ ) &&
+ git add subdir/deepsubmodule &&
+ git commit -m "new deep submodule"
+ ) &&
+ git add submodule &&
+ git commit -m "new submodule" &&
+ super_head=$(git rev-parse --short HEAD) &&
+ sub_head=$(git -C submodule rev-parse --short HEAD) &&
+ write_expected_super $super_head &&
+ write_expected_sub $sub_head
+}
+
+# Verifies that the expected repositories were fetched. This is done by
+# concatenating the files expect.err.[super|sub|deep] in the correct
+# order and comparing it to the actual stderr.
+#
+# If a repo should not be fetched in the test, its corresponding
+# expect.err file should be rm-ed.
+verify_fetch_result () {
+ ACTUAL_ERR=$1 &&
+ rm -f expect.err.combined &&
+ if test -f expect.err.super
+ then
+ cat expect.err.super >>expect.err.combined
+ fi &&
+ if test -f expect.err.sub
+ then
+ cat expect.err.sub >>expect.err.combined
+ fi &&
+ if test -f expect.err.deep
+ then
+ cat expect.err.deep >>expect.err.combined
+ fi &&
+ if test -f expect.err.sub2
+ then
+ cat expect.err.sub2 >>expect.err.combined
+ fi &&
+ sed -e 's/[0-9a-f][0-9a-f]*\.\./OLD_HEAD\.\./' "$ACTUAL_ERR" >actual.err.cmp &&
+ test_cmp expect.err.combined actual.err.cmp
+}
+
+test_expect_success setup '
+ git config --global protocol.file.allow always &&
+ mkdir deepsubmodule &&
+ (
+ cd deepsubmodule &&
+ git init &&
+ echo deepsubcontent > deepsubfile &&
+ git add deepsubfile &&
+ git commit -m new deepsubfile &&
+ git branch -M deep
+ ) &&
+ mkdir submodule &&
+ (
+ cd submodule &&
+ git init &&
+ echo subcontent > subfile &&
+ git add subfile &&
+ git submodule add "$pwd/deepsubmodule" subdir/deepsubmodule &&
+ git commit -a -m new &&
+ git branch -M sub
+ ) &&
+ git submodule add "$pwd/submodule" submodule &&
+ git commit -am initial &&
+ git branch -M super &&
+ git clone . downstream &&
+ (
+ cd downstream &&
+ git submodule update --init --recursive
+ )
+'
+
+test_expect_success "fetch --recurse-submodules recurses into submodules" '
+ add_submodule_commits &&
+ (
+ cd downstream &&
+ git fetch --recurse-submodules >../actual.out 2>../actual.err
+ ) &&
+ test_must_be_empty actual.out &&
+ verify_fetch_result actual.err
+'
+
+test_expect_success "fetch --recurse-submodules honors --no-write-fetch-head" '
+ (
+ cd downstream &&
+ git submodule foreach --recursive \
+ sh -c "cd \"\$(git rev-parse --git-dir)\" && rm -f FETCH_HEAD" &&
+
+ git fetch --recurse-submodules --no-write-fetch-head &&
+
+ git submodule foreach --recursive \
+ sh -c "cd \"\$(git rev-parse --git-dir)\" && ! test -f FETCH_HEAD"
+ )
+'
+
+test_expect_success "submodule.recurse option triggers recursive fetch" '
+ add_submodule_commits &&
+ (
+ cd downstream &&
+ git -c submodule.recurse fetch >../actual.out 2>../actual.err
+ ) &&
+ test_must_be_empty actual.out &&
+ verify_fetch_result actual.err
+'
+
+test_expect_success "fetch --recurse-submodules -j2 has the same output behaviour" '
+ test_when_finished "rm -f trace.out" &&
+ add_submodule_commits &&
+ (
+ cd downstream &&
+ GIT_TRACE="$TRASH_DIRECTORY/trace.out" git fetch --recurse-submodules -j2 2>../actual.err
+ ) &&
+ test_must_be_empty actual.out &&
+ verify_fetch_result actual.err &&
+ grep "2 tasks" trace.out
+'
+
+test_expect_success "fetch alone only fetches superproject" '
+ add_submodule_commits &&
+ (
+ cd downstream &&
+ git fetch >../actual.out 2>../actual.err
+ ) &&
+ test_must_be_empty actual.out &&
+ test_must_be_empty actual.err
+'
+
+test_expect_success "fetch --no-recurse-submodules only fetches superproject" '
+ (
+ cd downstream &&
+ git fetch --no-recurse-submodules >../actual.out 2>../actual.err
+ ) &&
+ test_must_be_empty actual.out &&
+ test_must_be_empty actual.err
+'
+
+test_expect_success "using fetchRecurseSubmodules=true in .gitmodules recurses into submodules" '
+ (
+ cd downstream &&
+ git config -f .gitmodules submodule.submodule.fetchRecurseSubmodules true &&
+ git fetch >../actual.out 2>../actual.err
+ ) &&
+ test_must_be_empty actual.out &&
+ verify_fetch_result actual.err
+'
+
+test_expect_success "--no-recurse-submodules overrides .gitmodules config" '
+ add_submodule_commits &&
+ (
+ cd downstream &&
+ git fetch --no-recurse-submodules >../actual.out 2>../actual.err
+ ) &&
+ test_must_be_empty actual.out &&
+ test_must_be_empty actual.err
+'
+
+test_expect_success "using fetchRecurseSubmodules=false in .git/config overrides setting in .gitmodules" '
+ (
+ cd downstream &&
+ git config submodule.submodule.fetchRecurseSubmodules false &&
+ git fetch >../actual.out 2>../actual.err
+ ) &&
+ test_must_be_empty actual.out &&
+ test_must_be_empty actual.err
+'
+
+test_expect_success "--recurse-submodules overrides fetchRecurseSubmodules setting from .git/config" '
+ (
+ cd downstream &&
+ git fetch --recurse-submodules >../actual.out 2>../actual.err &&
+ git config --unset -f .gitmodules submodule.submodule.fetchRecurseSubmodules &&
+ git config --unset submodule.submodule.fetchRecurseSubmodules
+ ) &&
+ test_must_be_empty actual.out &&
+ verify_fetch_result actual.err
+'
+
+test_expect_success "--quiet propagates to submodules" '
+ (
+ cd downstream &&
+ git fetch --recurse-submodules --quiet >../actual.out 2>../actual.err
+ ) &&
+ test_must_be_empty actual.out &&
+ test_must_be_empty actual.err
+'
+
+test_expect_success "--quiet propagates to parallel submodules" '
+ (
+ cd downstream &&
+ git fetch --recurse-submodules -j 2 --quiet >../actual.out 2>../actual.err
+ ) &&
+ test_must_be_empty actual.out &&
+ test_must_be_empty actual.err
+'
+
+test_expect_success "--dry-run propagates to submodules" '
+ add_submodule_commits &&
+ (
+ cd downstream &&
+ git fetch --recurse-submodules --dry-run >../actual.out 2>../actual.err
+ ) &&
+ test_must_be_empty actual.out &&
+ verify_fetch_result actual.err
+'
+
+test_expect_success "Without --dry-run propagates to submodules" '
+ (
+ cd downstream &&
+ git fetch --recurse-submodules >../actual.out 2>../actual.err
+ ) &&
+ test_must_be_empty actual.out &&
+ verify_fetch_result actual.err
+'
+
+test_expect_success "recurseSubmodules=true propagates into submodules" '
+ add_submodule_commits &&
+ (
+ cd downstream &&
+ git config fetch.recurseSubmodules true &&
+ git fetch >../actual.out 2>../actual.err
+ ) &&
+ test_must_be_empty actual.out &&
+ verify_fetch_result actual.err
+'
+
+test_expect_success "--recurse-submodules overrides config in submodule" '
+ add_submodule_commits &&
+ (
+ cd downstream &&
+ (
+ cd submodule &&
+ git config fetch.recurseSubmodules false
+ ) &&
+ git fetch --recurse-submodules >../actual.out 2>../actual.err
+ ) &&
+ test_must_be_empty actual.out &&
+ verify_fetch_result actual.err
+'
+
+test_expect_success "--no-recurse-submodules overrides config setting" '
+ add_submodule_commits &&
+ (
+ cd downstream &&
+ git config fetch.recurseSubmodules true &&
+ git fetch --no-recurse-submodules >../actual.out 2>../actual.err
+ ) &&
+ test_must_be_empty actual.out &&
+ test_must_be_empty actual.err
+'
+
+test_expect_success "Recursion doesn't happen when no new commits are fetched in the superproject" '
+ (
+ cd downstream &&
+ (
+ cd submodule &&
+ git config --unset fetch.recurseSubmodules
+ ) &&
+ git config --unset fetch.recurseSubmodules &&
+ git fetch >../actual.out 2>../actual.err
+ ) &&
+ test_must_be_empty actual.out &&
+ test_must_be_empty actual.err
+'
+
+test_expect_success "Recursion stops when no new submodule commits are fetched" '
+ git add submodule &&
+ git commit -m "new submodule" &&
+ new_head=$(git rev-parse --short HEAD) &&
+ write_expected_super $new_head &&
+ rm expect.err.deep &&
+ (
+ cd downstream &&
+ git fetch >../actual.out 2>../actual.err
+ ) &&
+ verify_fetch_result actual.err &&
+ test_must_be_empty actual.out
+'
+
+test_expect_success "Recursion doesn't happen when new superproject commits don't change any submodules" '
+ add_submodule_commits &&
+ echo a > file &&
+ git add file &&
+ git commit -m "new file" &&
+ new_head=$(git rev-parse --short HEAD) &&
+ write_expected_super $new_head &&
+ rm expect.err.sub &&
+ rm expect.err.deep &&
+ (
+ cd downstream &&
+ git fetch >../actual.out 2>../actual.err
+ ) &&
+ test_must_be_empty actual.out &&
+ verify_fetch_result actual.err
+'
+
+test_expect_success "Recursion picks up config in submodule" '
+ (
+ cd downstream &&
+ git fetch --recurse-submodules &&
+ (
+ cd submodule &&
+ git config fetch.recurseSubmodules true
+ )
+ ) &&
+ add_submodule_commits &&
+ git add submodule &&
+ git commit -m "new submodule" &&
+ new_head=$(git rev-parse --short HEAD) &&
+ write_expected_super $new_head &&
+ (
+ cd downstream &&
+ git fetch >../actual.out 2>../actual.err &&
+ (
+ cd submodule &&
+ git config --unset fetch.recurseSubmodules
+ )
+ ) &&
+ verify_fetch_result actual.err &&
+ test_must_be_empty actual.out
+'
+
+test_expect_success "Recursion picks up all submodules when necessary" '
+ add_submodule_commits &&
+ add_superproject_commits &&
+ (
+ cd downstream &&
+ git fetch >../actual.out 2>../actual.err
+ ) &&
+ verify_fetch_result actual.err &&
+ test_must_be_empty actual.out
+'
+
+test_expect_success "'--recurse-submodules=on-demand' doesn't recurse when no new commits are fetched in the superproject (and ignores config)" '
+ add_submodule_commits &&
+ (
+ cd downstream &&
+ git config fetch.recurseSubmodules true &&
+ git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err &&
+ git config --unset fetch.recurseSubmodules
+ ) &&
+ test_must_be_empty actual.out &&
+ test_must_be_empty actual.err
+'
+
+test_expect_success "'--recurse-submodules=on-demand' recurses as deep as necessary (and ignores config)" '
+ add_submodule_commits &&
+ add_superproject_commits &&
+ (
+ cd downstream &&
+ git config fetch.recurseSubmodules false &&
+ (
+ cd submodule &&
+ git config -f .gitmodules submodule.subdir/deepsubmodule.fetchRecursive false
+ ) &&
+ git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err &&
+ git config --unset fetch.recurseSubmodules &&
+ (
+ cd submodule &&
+ git config --unset -f .gitmodules submodule.subdir/deepsubmodule.fetchRecursive
+ )
+ ) &&
+ test_must_be_empty actual.out &&
+ verify_fetch_result actual.err
+'
+
+# These tests verify that we can fetch submodules that aren't in the
+# index.
+#
+# First, test the simple case where the index is empty and we only fetch
+# submodules that are not in the index.
+test_expect_success 'setup downstream branch without submodules' '
+ (
+ cd downstream &&
+ git checkout --recurse-submodules -b no-submodules &&
+ git rm .gitmodules &&
+ git rm submodule &&
+ git commit -m "no submodules" &&
+ git checkout --recurse-submodules super
+ )
+'
+
+test_expect_success "'--recurse-submodules=on-demand' should fetch submodule commits if the submodule is changed but the index has no submodules" '
+ add_submodule_commits &&
+ add_superproject_commits &&
+ # Fetch the new superproject commit
+ (
+ cd downstream &&
+ git switch --recurse-submodules no-submodules &&
+ git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err
+ ) &&
+ super_head=$(git rev-parse --short HEAD) &&
+ sub_head=$(git -C submodule rev-parse --short HEAD) &&
+ deep_head=$(git -C submodule/subdir/deepsubmodule rev-parse --short HEAD) &&
+
+ # assert that these are fetched from commits, not the index
+ write_expected_sub $sub_head $super_head &&
+ write_expected_deep $deep_head $sub_head &&
+
+ test_must_be_empty actual.out &&
+ verify_fetch_result actual.err
+'
+
+test_expect_success "'--recurse-submodules' should fetch submodule commits if the submodule is changed but the index has no submodules" '
+ add_submodule_commits &&
+ add_superproject_commits &&
+ # Fetch the new superproject commit
+ (
+ cd downstream &&
+ git switch --recurse-submodules no-submodules &&
+ git fetch --recurse-submodules >../actual.out 2>../actual.err
+ ) &&
+ super_head=$(git rev-parse --short HEAD) &&
+ sub_head=$(git -C submodule rev-parse --short HEAD) &&
+ deep_head=$(git -C submodule/subdir/deepsubmodule rev-parse --short HEAD) &&
+
+ # assert that these are fetched from commits, not the index
+ write_expected_sub $sub_head $super_head &&
+ write_expected_deep $deep_head $sub_head &&
+
+ test_must_be_empty actual.out &&
+ verify_fetch_result actual.err
+'
+
+test_expect_success "'--recurse-submodules' should ignore changed, inactive submodules" '
+ add_submodule_commits &&
+ add_superproject_commits &&
+
+ # Fetch the new superproject commit
+ (
+ cd downstream &&
+ git switch --recurse-submodules no-submodules &&
+ git -c submodule.submodule.active=false fetch --recurse-submodules >../actual.out 2>../actual.err
+ ) &&
+ test_must_be_empty actual.out &&
+ super_head=$(git rev-parse --short HEAD) &&
+ write_expected_super $super_head &&
+ # Neither should be fetched because the submodule is inactive
+ rm expect.err.sub &&
+ rm expect.err.deep &&
+ verify_fetch_result actual.err
+'
+
+# Now that we know we can fetch submodules that are not in the index,
+# test that we can fetch index and non-index submodules in the same
+# operation.
+test_expect_success 'setup downstream branch with other submodule' '
+ mkdir submodule2 &&
+ (
+ cd submodule2 &&
+ git init &&
+ echo sub2content >sub2file &&
+ git add sub2file &&
+ git commit -a -m new &&
+ git branch -M sub2
+ ) &&
+ git checkout -b super-sub2-only &&
+ git submodule add "$pwd/submodule2" submodule2 &&
+ git commit -m "add sub2" &&
+ git checkout super &&
+ (
+ cd downstream &&
+ git fetch --recurse-submodules origin &&
+ git checkout super-sub2-only &&
+ # Explicitly run "git submodule update" because sub2 is new
+ # and has not been cloned.
+ git submodule update --init &&
+ git checkout --recurse-submodules super
+ )
+'
+
+test_expect_success "'--recurse-submodules' should fetch submodule commits in changed submodules and the index" '
+ test_when_finished "rm expect.err.sub2" &&
+ # Create new commit in origin/super
+ add_submodule_commits &&
+ add_superproject_commits &&
+
+ # Create new commit in origin/super-sub2-only
+ git checkout super-sub2-only &&
+ (
+ cd submodule2 &&
+ test_commit --no-tag foo
+ ) &&
+ git add submodule2 &&
+ git commit -m "new submodule2" &&
+
+ git checkout super &&
+ (
+ cd downstream &&
+ git fetch --recurse-submodules >../actual.out 2>../actual.err
+ ) &&
+ test_must_be_empty actual.out &&
+ sub2_head=$(git -C submodule2 rev-parse --short HEAD) &&
+ super_head=$(git rev-parse --short super) &&
+ super_sub2_only_head=$(git rev-parse --short super-sub2-only) &&
+ write_expected_sub2 $sub2_head $super_sub2_only_head &&
+
+ # write_expected_super cannot handle >1 branch. Since this is a
+ # one-off, construct expect.err.super manually.
+ cat >"$pwd/expect.err.super" <<-EOF &&
+ From $pwd/.
+ OLD_HEAD..$super_head super -> origin/super
+ OLD_HEAD..$super_sub2_only_head super-sub2-only -> origin/super-sub2-only
+ EOF
+ verify_fetch_result actual.err
+'
+
+test_expect_success "'--recurse-submodules=on-demand' stops when no new submodule commits are found in the superproject (and ignores config)" '
+ add_submodule_commits &&
+ echo a >> file &&
+ git add file &&
+ git commit -m "new file" &&
+ new_head=$(git rev-parse --short HEAD) &&
+ write_expected_super $new_head &&
+ rm expect.err.sub &&
+ rm expect.err.deep &&
+ (
+ cd downstream &&
+ git fetch --recurse-submodules=on-demand >../actual.out 2>../actual.err
+ ) &&
+ test_must_be_empty actual.out &&
+ verify_fetch_result actual.err
+'
+
+test_expect_success "'fetch.recurseSubmodules=on-demand' overrides global config" '
+ (
+ cd downstream &&
+ git fetch --recurse-submodules
+ ) &&
+ add_submodule_commits &&
+ git config --global fetch.recurseSubmodules false &&
+ git add submodule &&
+ git commit -m "new submodule" &&
+ new_head=$(git rev-parse --short HEAD) &&
+ write_expected_super $new_head &&
+ rm expect.err.deep &&
+ (
+ cd downstream &&
+ git config fetch.recurseSubmodules on-demand &&
+ git fetch >../actual.out 2>../actual.err
+ ) &&
+ git config --global --unset fetch.recurseSubmodules &&
+ (
+ cd downstream &&
+ git config --unset fetch.recurseSubmodules
+ ) &&
+ test_must_be_empty actual.out &&
+ verify_fetch_result actual.err
+'
+
+test_expect_success "'submodule.<sub>.fetchRecurseSubmodules=on-demand' overrides fetch.recurseSubmodules" '
+ (
+ cd downstream &&
+ git fetch --recurse-submodules
+ ) &&
+ add_submodule_commits &&
+ git config fetch.recurseSubmodules false &&
+ git add submodule &&
+ git commit -m "new submodule" &&
+ new_head=$(git rev-parse --short HEAD) &&
+ write_expected_super $new_head &&
+ rm expect.err.deep &&
+ (
+ cd downstream &&
+ git config submodule.submodule.fetchRecurseSubmodules on-demand &&
+ git fetch >../actual.out 2>../actual.err
+ ) &&
+ git config --unset fetch.recurseSubmodules &&
+ (
+ cd downstream &&
+ git config --unset submodule.submodule.fetchRecurseSubmodules
+ ) &&
+ test_must_be_empty actual.out &&
+ verify_fetch_result actual.err
+'
+
+test_expect_success "don't fetch submodule when newly recorded commits are already present" '
+ (
+ cd submodule &&
+ git checkout -q HEAD^^
+ ) &&
+ git add submodule &&
+ git commit -m "submodule rewound" &&
+ new_head=$(git rev-parse --short HEAD) &&
+ write_expected_super $new_head &&
+ rm expect.err.sub &&
+ # This file does not exist, but rm -f for readability
+ rm -f expect.err.deep &&
+ (
+ cd downstream &&
+ git fetch >../actual.out 2>../actual.err
+ ) &&
+ test_must_be_empty actual.out &&
+ verify_fetch_result actual.err &&
+ (
+ cd submodule &&
+ git checkout -q sub
+ )
+'
+
+test_expect_success "'fetch.recurseSubmodules=on-demand' works also without .gitmodules entry" '
+ (
+ cd downstream &&
+ git fetch --recurse-submodules
+ ) &&
+ add_submodule_commits &&
+ git add submodule &&
+ git rm .gitmodules &&
+ git commit -m "new submodule without .gitmodules" &&
+ new_head=$(git rev-parse --short HEAD) &&
+ write_expected_super $new_head &&
+ rm expect.err.deep &&
+ (
+ cd downstream &&
+ rm .gitmodules &&
+ git config fetch.recurseSubmodules on-demand &&
+ # fake submodule configuration to avoid skipping submodule handling
+ git config -f .gitmodules submodule.fake.path fake &&
+ git config -f .gitmodules submodule.fake.url fakeurl &&
+ git add .gitmodules &&
+ git config --unset submodule.submodule.url &&
+ git fetch >../actual.out 2>../actual.err &&
+ # cleanup
+ git config --unset fetch.recurseSubmodules &&
+ git reset --hard
+ ) &&
+ test_must_be_empty actual.out &&
+ verify_fetch_result actual.err &&
+ git checkout HEAD^ -- .gitmodules &&
+ git add .gitmodules &&
+ git commit -m "new submodule restored .gitmodules"
+'
+
+test_expect_success 'fetching submodules respects parallel settings' '
+ git config fetch.recurseSubmodules true &&
+ test_when_finished "rm -f downstream/trace.out" &&
+ (
+ cd downstream &&
+ GIT_TRACE=$(pwd)/trace.out git fetch &&
+ grep "1 tasks" trace.out &&
+ >trace.out &&
+
+ GIT_TRACE=$(pwd)/trace.out git fetch --jobs 7 &&
+ grep "7 tasks" trace.out &&
+ >trace.out &&
+
+ git config submodule.fetchJobs 8 &&
+ GIT_TRACE=$(pwd)/trace.out git fetch &&
+ grep "8 tasks" trace.out &&
+ >trace.out &&
+
+ GIT_TRACE=$(pwd)/trace.out git fetch --jobs 9 &&
+ grep "9 tasks" trace.out &&
+ >trace.out &&
+
+ GIT_TRACE=$(pwd)/trace.out git -c submodule.fetchJobs=0 fetch &&
+ grep "preparing to run up to [0-9]* tasks" trace.out &&
+ ! grep "up to 0 tasks" trace.out &&
+ >trace.out
+ )
+'
+
+test_expect_success 'fetching submodule into a broken repository' '
+ # Prepare src and src/sub nested in it
+ git init src &&
+ (
+ cd src &&
+ git init sub &&
+ git -C sub commit --allow-empty -m "initial in sub" &&
+ git submodule add -- ./sub sub &&
+ git commit -m "initial in top"
+ ) &&
+
+ # Clone the old-fashoned way
+ git clone src dst &&
+ git -C dst clone ../src/sub sub &&
+
+ # Make sure that old-fashoned layout is still supported
+ git -C dst status &&
+
+ # "diff" would find no change
+ git -C dst diff --exit-code &&
+
+ # Recursive-fetch works fine
+ git -C dst fetch --recurse-submodules &&
+
+ # Break the receiving submodule
+ rm -f dst/sub/.git/HEAD &&
+
+ # NOTE: without the fix the following tests will recurse forever!
+ # They should terminate with an error.
+
+ test_must_fail git -C dst status &&
+ test_must_fail git -C dst diff &&
+ test_must_fail git -C dst fetch --recurse-submodules
+'
+
+test_expect_success "fetch new commits when submodule got renamed" '
+ git clone . downstream_rename &&
+ (
+ cd downstream_rename &&
+ git submodule update --init --recursive &&
+ git checkout -b rename &&
+ git mv submodule submodule_renamed &&
+ (
+ cd submodule_renamed &&
+ git checkout -b rename_sub &&
+ echo a >a &&
+ git add a &&
+ git commit -ma &&
+ git push origin rename_sub &&
+ git rev-parse HEAD >../../expect
+ ) &&
+ git add submodule_renamed &&
+ git commit -m "update renamed submodule" &&
+ git push origin rename
+ ) &&
+ (
+ cd downstream &&
+ git fetch --recurse-submodules=on-demand &&
+ (
+ cd submodule &&
+ git rev-parse origin/rename_sub >../../actual
+ )
+ ) &&
+ test_cmp expect actual
+'
+
+test_expect_success "fetch new submodule commits on-demand outside standard refspec" '
+ # add a second submodule and ensure it is around in downstream first
+ git clone submodule sub1 &&
+ git submodule add ./sub1 &&
+ git commit -m "adding a second submodule" &&
+ git -C downstream pull &&
+ git -C downstream submodule update --init --recursive &&
+
+ git checkout --detach &&
+
+ C=$(git -C submodule commit-tree -m "new change outside refs/heads" HEAD^{tree}) &&
+ git -C submodule update-ref refs/changes/1 $C &&
+ git update-index --cacheinfo 160000 $C submodule &&
+ test_tick &&
+
+ D=$(git -C sub1 commit-tree -m "new change outside refs/heads" HEAD^{tree}) &&
+ git -C sub1 update-ref refs/changes/2 $D &&
+ git update-index --cacheinfo 160000 $D sub1 &&
+
+ git commit -m "updated submodules outside of refs/heads" &&
+ E=$(git rev-parse HEAD) &&
+ git update-ref refs/changes/3 $E &&
+ (
+ cd downstream &&
+ git fetch --recurse-submodules origin refs/changes/3:refs/heads/my_branch &&
+ git -C submodule cat-file -t $C &&
+ git -C sub1 cat-file -t $D &&
+ git checkout --recurse-submodules FETCH_HEAD
+ )
+'
+
+test_expect_success 'fetch new submodule commit on-demand in FETCH_HEAD' '
+ # depends on the previous test for setup
+
+ C=$(git -C submodule commit-tree -m "another change outside refs/heads" HEAD^{tree}) &&
+ git -C submodule update-ref refs/changes/4 $C &&
+ git update-index --cacheinfo 160000 $C submodule &&
+ test_tick &&
+
+ D=$(git -C sub1 commit-tree -m "another change outside refs/heads" HEAD^{tree}) &&
+ git -C sub1 update-ref refs/changes/5 $D &&
+ git update-index --cacheinfo 160000 $D sub1 &&
+
+ git commit -m "updated submodules outside of refs/heads" &&
+ E=$(git rev-parse HEAD) &&
+ git update-ref refs/changes/6 $E &&
+ (
+ cd downstream &&
+ git fetch --recurse-submodules origin refs/changes/6 &&
+ git -C submodule cat-file -t $C &&
+ git -C sub1 cat-file -t $D &&
+ git checkout --recurse-submodules FETCH_HEAD
+ )
+'
+
+test_expect_success 'fetch new submodule commits on-demand without .gitmodules entry' '
+ # depends on the previous test for setup
+
+ git config -f .gitmodules --remove-section submodule.sub1 &&
+ git add .gitmodules &&
+ git commit -m "delete gitmodules file" &&
+ git checkout -B super &&
+ git -C downstream fetch &&
+ git -C downstream checkout origin/super &&
+
+ C=$(git -C submodule commit-tree -m "yet another change outside refs/heads" HEAD^{tree}) &&
+ git -C submodule update-ref refs/changes/7 $C &&
+ git update-index --cacheinfo 160000 $C submodule &&
+ test_tick &&
+
+ D=$(git -C sub1 commit-tree -m "yet another change outside refs/heads" HEAD^{tree}) &&
+ git -C sub1 update-ref refs/changes/8 $D &&
+ git update-index --cacheinfo 160000 $D sub1 &&
+
+ git commit -m "updated submodules outside of refs/heads" &&
+ E=$(git rev-parse HEAD) &&
+ git update-ref refs/changes/9 $E &&
+ (
+ cd downstream &&
+ git fetch --recurse-submodules origin refs/changes/9 &&
+ git -C submodule cat-file -t $C &&
+ git -C sub1 cat-file -t $D &&
+ git checkout --recurse-submodules FETCH_HEAD
+ )
+'
+
+test_expect_success 'fetch new submodule commit intermittently referenced by superproject' '
+ # depends on the previous test for setup
+
+ D=$(git -C sub1 commit-tree -m "change 10 outside refs/heads" HEAD^{tree}) &&
+ E=$(git -C sub1 commit-tree -m "change 11 outside refs/heads" HEAD^{tree}) &&
+ F=$(git -C sub1 commit-tree -m "change 12 outside refs/heads" HEAD^{tree}) &&
+
+ git -C sub1 update-ref refs/changes/10 $D &&
+ git update-index --cacheinfo 160000 $D sub1 &&
+ git commit -m "updated submodules outside of refs/heads" &&
+
+ git -C sub1 update-ref refs/changes/11 $E &&
+ git update-index --cacheinfo 160000 $E sub1 &&
+ git commit -m "updated submodules outside of refs/heads" &&
+
+ git -C sub1 update-ref refs/changes/12 $F &&
+ git update-index --cacheinfo 160000 $F sub1 &&
+ git commit -m "updated submodules outside of refs/heads" &&
+
+ G=$(git rev-parse HEAD) &&
+ git update-ref refs/changes/13 $G &&
+ (
+ cd downstream &&
+ git fetch --recurse-submodules origin refs/changes/13 &&
+
+ git -C sub1 cat-file -t $D &&
+ git -C sub1 cat-file -t $E &&
+ git -C sub1 cat-file -t $F
+ )
+'
+
+add_commit_push () {
+ dir="$1" &&
+ msg="$2" &&
+ shift 2 &&
+ git -C "$dir" add "$@" &&
+ git -C "$dir" commit -a -m "$msg" &&
+ git -C "$dir" push
+}
+
+compare_refs_in_dir () {
+ fail= &&
+ if test "x$1" = 'x!'
+ then
+ fail='!' &&
+ shift
+ fi &&
+ git -C "$1" rev-parse --verify "$2" >expect &&
+ git -C "$3" rev-parse --verify "$4" >actual &&
+ eval $fail test_cmp expect actual
+}
+
+
+test_expect_success 'setup nested submodule fetch test' '
+ # does not depend on any previous test setups
+
+ for repo in outer middle inner
+ do
+ git init --bare $repo &&
+ git clone $repo ${repo}_content &&
+ echo "$repo" >"${repo}_content/file" &&
+ add_commit_push ${repo}_content "initial" file ||
+ return 1
+ done &&
+
+ git clone outer A &&
+ git -C A submodule add "$pwd/middle" &&
+ git -C A/middle/ submodule add "$pwd/inner" &&
+ add_commit_push A/middle/ "adding inner sub" .gitmodules inner &&
+ add_commit_push A/ "adding middle sub" .gitmodules middle &&
+
+ git clone outer B &&
+ git -C B/ submodule update --init middle &&
+
+ compare_refs_in_dir A HEAD B HEAD &&
+ compare_refs_in_dir A/middle HEAD B/middle HEAD &&
+ test_path_is_file B/file &&
+ test_path_is_file B/middle/file &&
+ test_path_is_missing B/middle/inner/file &&
+
+ echo "change on inner repo of A" >"A/middle/inner/file" &&
+ add_commit_push A/middle/inner "change on inner" file &&
+ add_commit_push A/middle "change on inner" inner &&
+ add_commit_push A "change on inner" middle
+'
+
+test_expect_success 'fetching a superproject containing an uninitialized sub/sub project' '
+ # depends on previous test for setup
+
+ git -C B/ fetch &&
+ compare_refs_in_dir A origin/HEAD B origin/HEAD
+'
+
+fetch_with_recursion_abort () {
+ # In a regression the following git call will run into infinite recursion.
+ # To handle that, we connect the sed command to the git call by a pipe
+ # so that sed can kill the infinite recursion when detected.
+ # The recursion creates git output like:
+ # Fetching submodule sub
+ # Fetching submodule sub/sub <-- [1]
+ # Fetching submodule sub/sub/sub
+ # ...
+ # [1] sed will stop reading and cause git to eventually stop and die
+
+ git -C "$1" fetch --recurse-submodules 2>&1 |
+ sed "/Fetching submodule $2[^$]/q" >out &&
+ ! grep "Fetching submodule $2[^$]" out
+}
+
+test_expect_success 'setup recursive fetch with uninit submodule' '
+ # does not depend on any previous test setups
+
+ test_create_repo super &&
+ test_commit -C super initial &&
+ test_create_repo sub &&
+ test_commit -C sub initial &&
+ git -C sub rev-parse HEAD >expect &&
+
+ git -C super submodule add ../sub &&
+ git -C super commit -m "add sub" &&
+
+ git clone super superclone &&
+ git -C superclone submodule status >out &&
+ sed -e "s/^-//" -e "s/ sub.*$//" out >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'recursive fetch with uninit submodule' '
+ # depends on previous test for setup
+
+ fetch_with_recursion_abort superclone sub &&
+ git -C superclone submodule status >out &&
+ sed -e "s/^-//" -e "s/ sub$//" out >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'recursive fetch after deinit a submodule' '
+ # depends on previous test for setup
+
+ git -C superclone submodule update --init sub &&
+ git -C superclone submodule deinit -f sub &&
+
+ fetch_with_recursion_abort superclone sub &&
+ git -C superclone submodule status >out &&
+ sed -e "s/^-//" -e "s/ sub$//" out >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'setup repo with upstreams that share a submodule name' '
+ mkdir same-name-1 &&
+ (
+ cd same-name-1 &&
+ git init -b main &&
+ test_commit --no-tag a
+ ) &&
+ git clone same-name-1 same-name-2 &&
+ # same-name-1 and same-name-2 both add a submodule with the
+ # name "submodule"
+ (
+ cd same-name-1 &&
+ mkdir submodule &&
+ git -C submodule init -b main &&
+ test_commit -C submodule --no-tag a1 &&
+ git submodule add "$pwd/same-name-1/submodule" &&
+ git add submodule &&
+ git commit -m "super-a1"
+ ) &&
+ (
+ cd same-name-2 &&
+ mkdir submodule &&
+ git -C submodule init -b main &&
+ test_commit -C submodule --no-tag a2 &&
+ git submodule add "$pwd/same-name-2/submodule" &&
+ git add submodule &&
+ git commit -m "super-a2"
+ ) &&
+ git clone same-name-1 -o same-name-1 same-name-downstream &&
+ (
+ cd same-name-downstream &&
+ git remote add same-name-2 ../same-name-2 &&
+ git fetch --all &&
+ # init downstream with same-name-1
+ git submodule update --init
+ )
+'
+
+test_expect_success 'fetch --recurse-submodules updates name-conflicted, populated submodule' '
+ test_when_finished "git -C same-name-downstream checkout main" &&
+ (
+ cd same-name-1 &&
+ test_commit -C submodule --no-tag b1 &&
+ git add submodule &&
+ git commit -m "super-b1"
+ ) &&
+ (
+ cd same-name-2 &&
+ test_commit -C submodule --no-tag b2 &&
+ git add submodule &&
+ git commit -m "super-b2"
+ ) &&
+ (
+ cd same-name-downstream &&
+ # even though the .gitmodules is correct, we cannot
+ # fetch from same-name-2
+ git checkout same-name-2/main &&
+ git fetch --recurse-submodules same-name-1 &&
+ test_must_fail git fetch --recurse-submodules same-name-2
+ ) &&
+ super_head1=$(git -C same-name-1 rev-parse HEAD) &&
+ git -C same-name-downstream cat-file -e $super_head1 &&
+
+ super_head2=$(git -C same-name-2 rev-parse HEAD) &&
+ git -C same-name-downstream cat-file -e $super_head2 &&
+
+ sub_head1=$(git -C same-name-1/submodule rev-parse HEAD) &&
+ git -C same-name-downstream/submodule cat-file -e $sub_head1 &&
+
+ sub_head2=$(git -C same-name-2/submodule rev-parse HEAD) &&
+ test_must_fail git -C same-name-downstream/submodule cat-file -e $sub_head2
+'
+
+test_expect_success 'fetch --recurse-submodules updates name-conflicted, unpopulated submodule' '
+ (
+ cd same-name-1 &&
+ test_commit -C submodule --no-tag c1 &&
+ git add submodule &&
+ git commit -m "super-c1"
+ ) &&
+ (
+ cd same-name-2 &&
+ test_commit -C submodule --no-tag c2 &&
+ git add submodule &&
+ git commit -m "super-c2"
+ ) &&
+ (
+ cd same-name-downstream &&
+ git checkout main &&
+ git rm .gitmodules &&
+ git rm submodule &&
+ git commit -m "no submodules" &&
+ git fetch --recurse-submodules same-name-1
+ ) &&
+ head1=$(git -C same-name-1/submodule rev-parse HEAD) &&
+ head2=$(git -C same-name-2/submodule rev-parse HEAD) &&
+ (
+ cd same-name-downstream/.git/modules/submodule &&
+ # The submodule has core.worktree pointing to the "git
+ # rm"-ed directory, overwrite the invalid value. See
+ # comment in get_fetch_task_from_changed() for more
+ # information.
+ git --work-tree=. cat-file -e $head1 &&
+ test_must_fail git --work-tree=. cat-file -e $head2
+ )
+'
+
+test_expect_success 'fetch --all with --recurse-submodules' '
+ test_when_finished "rm -fr src_clone" &&
+ git clone --recurse-submodules src src_clone &&
+ (
+ cd src_clone &&
+ git config submodule.recurse true &&
+ git config fetch.parallel 0 &&
+ git fetch --all 2>../fetch-log
+ ) &&
+ grep "^Fetching submodule sub$" fetch-log >fetch-subs &&
+ test_line_count = 1 fetch-subs
+'
+
+test_expect_success 'fetch --all with --recurse-submodules with multiple' '
+ test_when_finished "rm -fr src_clone" &&
+ git clone --recurse-submodules src src_clone &&
+ (
+ cd src_clone &&
+ git remote add secondary ../src &&
+ git config submodule.recurse true &&
+ git config fetch.parallel 0 &&
+ git fetch --all 2>../fetch-log
+ ) &&
+ grep "Fetching submodule sub" fetch-log >fetch-subs &&
+ test_line_count = 2 fetch-subs
+'
+
+test_expect_success "fetch --all with --no-recurse-submodules only fetches superproject" '
+ test_when_finished "rm -rf src_clone" &&
+
+ git clone --recurse-submodules src src_clone &&
+ (
+ cd src_clone &&
+ git remote add secondary ../src &&
+ git config submodule.recurse true &&
+ git fetch --all --no-recurse-submodules 2>../fetch-log
+ ) &&
+ ! grep "Fetching submodule" fetch-log
+'
+
+test_done
diff --git a/t/t5527-fetch-odd-refs.sh b/t/t5527-fetch-odd-refs.sh
new file mode 100755
index 0000000..98ece27
--- /dev/null
+++ b/t/t5527-fetch-odd-refs.sh
@@ -0,0 +1,66 @@
+#!/bin/sh
+
+test_description='test fetching of oddly-named refs'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# afterwards we will have:
+# HEAD - two
+# refs/for/refs/heads/main - one
+# refs/heads/main - three
+test_expect_success 'setup repo with odd suffix ref' '
+ echo content >file &&
+ git add . &&
+ git commit -m one &&
+ git update-ref refs/for/refs/heads/main HEAD &&
+ echo content >>file &&
+ git commit -a -m two &&
+ echo content >>file &&
+ git commit -a -m three &&
+ git checkout HEAD^
+'
+
+test_expect_success 'suffix ref is ignored during fetch' '
+ git clone --bare file://"$PWD" suffix &&
+ echo three >expect &&
+ git --git-dir=suffix log -1 --format=%s refs/heads/main >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'try to create repo with absurdly long refname' '
+ ref240=$ZERO_OID/$ZERO_OID/$ZERO_OID/$ZERO_OID/$ZERO_OID/$ZERO_OID &&
+ ref1440=$ref240/$ref240/$ref240/$ref240/$ref240/$ref240 &&
+ git init long &&
+ (
+ cd long &&
+ test_commit long &&
+ test_commit main
+ ) &&
+ if git -C long update-ref refs/heads/$ref1440 long; then
+ test_set_prereq LONG_REF
+ else
+ echo >&2 "long refs not supported"
+ fi
+'
+
+test_expect_success LONG_REF 'fetch handles extremely long refname' '
+ git fetch long refs/heads/*:refs/remotes/long/* &&
+ cat >expect <<-\EOF &&
+ long
+ main
+ EOF
+ git for-each-ref --format="%(subject)" refs/remotes/long >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success LONG_REF 'push handles extremely long refname' '
+ git push long :refs/heads/$ref1440 &&
+ git -C long for-each-ref --format="%(subject)" refs/heads >actual &&
+ echo main >expect &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t5528-push-default.sh b/t/t5528-push-default.sh
new file mode 100755
index 0000000..14f7ece
--- /dev/null
+++ b/t/t5528-push-default.sh
@@ -0,0 +1,297 @@
+#!/bin/sh
+
+test_description='check various push.default settings'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success 'setup bare remotes' '
+ git init --bare repo1 &&
+ git remote add parent1 repo1 &&
+ git init --bare repo2 &&
+ git remote add parent2 repo2 &&
+ test_commit one &&
+ git push parent1 HEAD &&
+ git push parent2 HEAD
+'
+
+# $1 = local revision
+# $2 = remote revision (tested to be equal to the local one)
+# $3 = [optional] repo to check for actual output (repo1 by default)
+check_pushed_commit () {
+ git log -1 --format='%h %s' "$1" >expect &&
+ git --git-dir="${3:-repo1}" log -1 --format='%h %s' "$2" >actual &&
+ test_cmp expect actual
+}
+
+# $1 = push.default value
+# $2 = expected target branch for the push
+# $3 = [optional] repo to check for actual output (repo1 by default)
+test_push_success () {
+ git ${1:+-c} ${1:+push.default="$1"} push &&
+ check_pushed_commit HEAD "$2" "$3"
+}
+
+# $1 = push.default value
+# check that push fails and does not modify any remote branch
+test_push_failure () {
+ git --git-dir=repo1 log --no-walk --format='%h %s' --all >expect &&
+ test_must_fail git ${1:+-c} ${1:+push.default="$1"} push &&
+ git --git-dir=repo1 log --no-walk --format='%h %s' --all >actual &&
+ test_cmp expect actual
+}
+
+# $1 = success or failure
+# $2 = push.default value
+# $3 = branch to check for actual output (main or foo)
+# $4 = [optional] switch to triangular workflow
+test_pushdefault_workflow () {
+ workflow=central
+ pushdefault=parent1
+ if test -n "${4-}"; then
+ workflow=triangular
+ pushdefault=parent2
+ fi
+ test_expect_success "push.default = $2 $1 in $workflow workflows" "
+ test_config branch.main.remote parent1 &&
+ test_config branch.main.merge refs/heads/foo &&
+ test_config remote.pushdefault $pushdefault &&
+ test_commit commit-for-$2${4+-triangular} &&
+ test_push_$1 $2 $3 ${4+repo2}
+ "
+}
+
+test_expect_success '"upstream" pushes to configured upstream' '
+ git checkout main &&
+ test_config branch.main.remote parent1 &&
+ test_config branch.main.merge refs/heads/foo &&
+ test_commit two &&
+ test_push_success upstream foo
+'
+
+test_expect_success '"upstream" does not push on unconfigured remote' '
+ git checkout main &&
+ test_unconfig branch.main.remote &&
+ test_commit three &&
+ test_push_failure upstream
+'
+
+test_expect_success '"upstream" does not push on unconfigured branch' '
+ git checkout main &&
+ test_config branch.main.remote parent1 &&
+ test_unconfig branch.main.merge &&
+ test_commit four &&
+ test_push_failure upstream
+'
+
+test_expect_success '"upstream" does not push when remotes do not match' '
+ git checkout main &&
+ test_config branch.main.remote parent1 &&
+ test_config branch.main.merge refs/heads/foo &&
+ test_config push.default upstream &&
+ test_commit five &&
+ test_must_fail git push parent2
+'
+
+test_expect_success '"current" does not push when multiple remotes and none origin' '
+ git checkout main &&
+ test_config push.default current &&
+ test_commit current-multi &&
+ test_must_fail git push
+'
+
+test_expect_success '"current" pushes when remote explicitly specified' '
+ git checkout main &&
+ test_config push.default current &&
+ test_commit current-specified &&
+ git push parent1
+'
+
+test_expect_success '"current" pushes to origin when no remote specified among multiple' '
+ git checkout main &&
+ test_config remote.origin.url repo1 &&
+ test_config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" &&
+ test_commit current-origin &&
+ test_push_success current main
+'
+
+test_expect_success '"current" pushes to single remote even when not specified' '
+ git checkout main &&
+ test_when_finished git remote add parent1 repo1 &&
+ git remote remove parent1 &&
+ test_commit current-implied &&
+ test_push_success current main repo2
+'
+
+test_expect_success 'push from/to new branch with non-defaulted remote fails with upstream, matching, current and simple ' '
+ git checkout -b new-branch &&
+ test_push_failure simple &&
+ test_push_failure matching &&
+ test_push_failure upstream &&
+ test_push_failure current
+'
+
+test_expect_success 'push from/to new branch fails with upstream and simple ' '
+ git checkout -b new-branch-1 &&
+ test_config branch.new-branch-1.remote parent1 &&
+ test_push_failure simple &&
+ test_push_failure upstream
+'
+
+# The behavior here is surprising but not entirely wrong:
+# - the current branch is used to determine the target remote
+# - the "matching" push default pushes matching branches, *ignoring* the
+# current new branch as it does not have upstream tracking
+# - the default push succeeds
+#
+# A previous test expected this to fail, but for the wrong reasons:
+# it expected a fail becaause the branch is new and cannot be pushed, but
+# in fact it was failing because of an ambiguous remote
+#
+test_expect_failure 'push from/to new branch fails with matching ' '
+ git checkout -b new-branch-2 &&
+ test_config branch.new-branch-2.remote parent1 &&
+ test_push_failure matching
+'
+
+test_expect_success 'push from/to branch with tracking fails with nothing ' '
+ git checkout -b tracked-branch &&
+ test_config branch.tracked-branch.remote parent1 &&
+ test_config branch.tracked-branch.merge refs/heads/tracked-branch &&
+ test_push_failure nothing
+'
+
+test_expect_success 'push from/to new branch succeeds with upstream if push.autoSetupRemote' '
+ git checkout -b new-branch-a &&
+ test_config push.autoSetupRemote true &&
+ test_config branch.new-branch-a.remote parent1 &&
+ test_push_success upstream new-branch-a
+'
+
+test_expect_success 'push from/to new branch succeeds with simple if push.autoSetupRemote' '
+ git checkout -b new-branch-c &&
+ test_config push.autoSetupRemote true &&
+ test_config branch.new-branch-c.remote parent1 &&
+ test_push_success simple new-branch-c
+'
+
+test_expect_success '"matching" fails if none match' '
+ git init --bare empty &&
+ test_must_fail git push empty : 2>actual &&
+ test_grep "Perhaps you should specify a branch" actual
+'
+
+test_expect_success 'push ambiguously named branch with upstream, matching and simple' '
+ git checkout -b ambiguous &&
+ test_config branch.ambiguous.remote parent1 &&
+ test_config branch.ambiguous.merge refs/heads/ambiguous &&
+ git tag ambiguous &&
+ test_push_success simple ambiguous &&
+ test_push_success matching ambiguous &&
+ test_push_success upstream ambiguous
+'
+
+test_expect_success 'push from/to new branch with current creates remote branch' '
+ test_config branch.new-branch.remote repo1 &&
+ git checkout new-branch &&
+ test_push_success current new-branch
+'
+
+test_expect_success 'push to existing branch, with no upstream configured' '
+ test_config branch.main.remote repo1 &&
+ git checkout main &&
+ test_push_failure simple &&
+ test_push_failure upstream
+'
+
+test_expect_success 'push to existing branch, upstream configured with same name' '
+ test_config branch.main.remote repo1 &&
+ test_config branch.main.merge refs/heads/main &&
+ git checkout main &&
+ test_commit six &&
+ test_push_success upstream main &&
+ test_commit seven &&
+ test_push_success simple main
+'
+
+test_expect_success 'push to existing branch, upstream configured with different name' '
+ test_config branch.main.remote repo1 &&
+ test_config branch.main.merge refs/heads/other-name &&
+ git checkout main &&
+ test_commit eight &&
+ test_push_success upstream other-name &&
+ test_commit nine &&
+ test_push_failure simple &&
+ git --git-dir=repo1 log -1 --format="%h %s" "other-name" >expect-other-name &&
+ test_push_success current main &&
+ git --git-dir=repo1 log -1 --format="%h %s" "other-name" >actual-other-name &&
+ test_cmp expect-other-name actual-other-name
+'
+
+# We are on 'main', which integrates with 'foo' from parent1
+# remote (set in test_pushdefault_workflow helper). Push to
+# parent1 in centralized, and push to parent2 in triangular workflow.
+# The parent1 repository has 'main' and 'foo' branches, while
+# the parent2 repository has only 'main' branch.
+#
+# test_pushdefault_workflow() arguments:
+# $1 = success or failure
+# $2 = push.default value
+# $3 = branch to check for actual output (main or foo)
+# $4 = [optional] switch to triangular workflow
+
+# update parent1's main (which is not our upstream)
+test_pushdefault_workflow success current main
+
+# update parent1's foo (which is our upstream)
+test_pushdefault_workflow success upstream foo
+
+# upstream is foo which is not the name of the current branch
+test_pushdefault_workflow failure simple main
+
+# main and foo are updated
+test_pushdefault_workflow success matching main
+
+# main is updated
+test_pushdefault_workflow success current main triangular
+
+# upstream mode cannot be used in triangular
+test_pushdefault_workflow failure upstream foo triangular
+
+# in triangular, 'simple' works as 'current' and update the branch
+# with the same name.
+test_pushdefault_workflow success simple main triangular
+
+# main is updated (parent2 does not have foo)
+test_pushdefault_workflow success matching main triangular
+
+# default tests, when no push-default is specified. This
+# should behave the same as "simple" in non-triangular
+# settings, and as "current" otherwise.
+
+test_expect_success 'default behavior allows "simple" push' '
+ test_config branch.main.remote parent1 &&
+ test_config branch.main.merge refs/heads/main &&
+ test_config remote.pushdefault parent1 &&
+ test_commit default-main-main &&
+ test_push_success "" main
+'
+
+test_expect_success 'default behavior rejects non-simple push' '
+ test_config branch.main.remote parent1 &&
+ test_config branch.main.merge refs/heads/foo &&
+ test_config remote.pushdefault parent1 &&
+ test_commit default-main-foo &&
+ test_push_failure ""
+'
+
+test_expect_success 'default triangular behavior acts like "current"' '
+ test_config branch.main.remote parent1 &&
+ test_config branch.main.merge refs/heads/foo &&
+ test_config remote.pushdefault parent2 &&
+ test_commit default-triangular &&
+ test_push_success "" main repo2
+'
+
+test_done
diff --git a/t/t5529-push-errors.sh b/t/t5529-push-errors.sh
new file mode 100755
index 0000000..0247137
--- /dev/null
+++ b/t/t5529-push-errors.sh
@@ -0,0 +1,50 @@
+#!/bin/sh
+
+test_description='detect some push errors early (before contacting remote)'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup commits' '
+ test_commit one
+'
+
+test_expect_success 'setup remote' '
+ git init --bare remote.git &&
+ git remote add origin remote.git
+'
+
+test_expect_success 'setup fake receive-pack' '
+ FAKE_RP_ROOT=$(pwd) &&
+ export FAKE_RP_ROOT &&
+ write_script fake-rp <<-\EOF &&
+ echo yes >"$FAKE_RP_ROOT"/rp-ran
+ exit 1
+ EOF
+ git config remote.origin.receivepack "\"\$FAKE_RP_ROOT/fake-rp\""
+'
+
+test_expect_success 'detect missing branches early' '
+ echo no >rp-ran &&
+ echo no >expect &&
+ test_must_fail git push origin missing &&
+ test_cmp expect rp-ran
+'
+
+test_expect_success 'detect missing sha1 expressions early' '
+ echo no >rp-ran &&
+ echo no >expect &&
+ test_must_fail git push origin main~2:main &&
+ test_cmp expect rp-ran
+'
+
+test_expect_success 'detect ambiguous refs early' '
+ git branch foo &&
+ git tag foo &&
+ echo no >rp-ran &&
+ echo no >expect &&
+ test_must_fail git push origin foo &&
+ test_cmp expect rp-ran
+'
+
+test_done
diff --git a/t/t5530-upload-pack-error.sh b/t/t5530-upload-pack-error.sh
new file mode 100755
index 0000000..7172780
--- /dev/null
+++ b/t/t5530-upload-pack-error.sh
@@ -0,0 +1,123 @@
+#!/bin/sh
+
+test_description='errors in upload-pack'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+D=$(pwd)
+
+corrupt_repo () {
+ object_sha1=$(git rev-parse "$1") &&
+ ob=$(expr "$object_sha1" : "\(..\)") &&
+ ject=$(expr "$object_sha1" : "..\(..*\)") &&
+ rm -f ".git/objects/$ob/$ject"
+}
+
+test_expect_success 'setup and corrupt repository' '
+ echo file >file &&
+ git add file &&
+ git rev-parse :file &&
+ git commit -a -m original &&
+ test_tick &&
+ echo changed >file &&
+ git commit -a -m changed &&
+ corrupt_repo HEAD:file
+
+'
+
+test_expect_success 'fsck fails' '
+ test_must_fail git fsck
+'
+
+test_expect_success 'upload-pack fails due to error in pack-objects packing' '
+ head=$(git rev-parse HEAD) &&
+ hexsz=$(test_oid hexsz) &&
+ printf "%04xwant %s\n00000009done\n0000" \
+ $(($hexsz + 10)) $head >input &&
+ test_must_fail git upload-pack . <input >/dev/null 2>output.err &&
+ test_grep "unable to read" output.err &&
+ test_grep "pack-objects died" output.err
+'
+
+test_expect_success 'corrupt repo differently' '
+
+ git hash-object -w file &&
+ corrupt_repo HEAD^^{tree}
+
+'
+
+test_expect_success 'fsck fails' '
+ test_must_fail git fsck
+'
+test_expect_success 'upload-pack fails due to error in rev-list' '
+
+ printf "%04xwant %s\n%04xshallow %s00000009done\n0000" \
+ $(($hexsz + 10)) $(git rev-parse HEAD) \
+ $(($hexsz + 12)) $(git rev-parse HEAD^) >input &&
+ test_must_fail git upload-pack . <input >/dev/null 2>output.err &&
+ grep "bad tree object" output.err
+'
+
+test_expect_success 'upload-pack fails due to bad want (no object)' '
+
+ printf "%04xwant %s multi_ack_detailed\n00000009done\n0000" \
+ $(($hexsz + 29)) $(test_oid deadbeef) >input &&
+ test_must_fail git upload-pack . <input >output 2>output.err &&
+ grep "not our ref" output.err &&
+ grep "ERR" output &&
+ ! grep multi_ack_detailed output.err
+'
+
+test_expect_success 'upload-pack fails due to bad want (not tip)' '
+
+ oid=$(echo an object we have | git hash-object -w --stdin) &&
+ printf "%04xwant %s multi_ack_detailed\n00000009done\n0000" \
+ $(($hexsz + 29)) "$oid" >input &&
+ test_must_fail git upload-pack . <input >output 2>output.err &&
+ grep "not our ref" output.err &&
+ grep "ERR" output &&
+ ! grep multi_ack_detailed output.err
+'
+
+test_expect_success 'upload-pack fails due to error in pack-objects enumeration' '
+
+ printf "%04xwant %s\n00000009done\n0000" \
+ $((hexsz + 10)) $(git rev-parse HEAD) >input &&
+ test_must_fail git upload-pack . <input >/dev/null 2>output.err &&
+ grep "bad tree object" output.err &&
+ grep "pack-objects died" output.err
+'
+
+test_expect_success 'upload-pack tolerates EOF just after stateless client wants' '
+ test_commit initial &&
+ head=$(git rev-parse HEAD) &&
+
+ {
+ packetize "want $head" &&
+ packetize "shallow $head" &&
+ packetize "deepen 1" &&
+ printf "0000"
+ } >request &&
+
+ printf "0000" >expect &&
+
+ git upload-pack --stateless-rpc . <request >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'create empty repository' '
+
+ mkdir foo &&
+ cd foo &&
+ git init
+
+'
+
+test_expect_success 'fetch fails' '
+
+ test_must_fail git fetch .. main
+
+'
+
+test_done
diff --git a/t/t5531-deep-submodule-push.sh b/t/t5531-deep-submodule-push.sh
new file mode 100755
index 0000000..f3fff55
--- /dev/null
+++ b/t/t5531-deep-submodule-push.sh
@@ -0,0 +1,640 @@
+#!/bin/sh
+
+test_description='test push with submodules'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB=1
+export GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB
+
+. ./test-lib.sh
+
+test_expect_success setup '
+ mkdir pub.git &&
+ GIT_DIR=pub.git git init --bare &&
+ GIT_DIR=pub.git git config receive.fsckobjects true &&
+ mkdir work &&
+ (
+ cd work &&
+ git init &&
+ git config push.default matching &&
+ mkdir -p gar/bage &&
+ (
+ cd gar/bage &&
+ git init &&
+ git config push.default matching &&
+ >junk &&
+ git add junk &&
+ git commit -m "Initial junk"
+ ) &&
+ git add gar/bage &&
+ git commit -m "Initial superproject"
+ )
+'
+
+test_expect_success 'push works with recorded gitlink' '
+ (
+ cd work &&
+ git push ../pub.git main
+ )
+'
+
+test_expect_success 'push if submodule has no remote' '
+ (
+ cd work/gar/bage &&
+ >junk2 &&
+ git add junk2 &&
+ git commit -m "Second junk"
+ ) &&
+ (
+ cd work &&
+ git add gar/bage &&
+ git commit -m "Second commit for gar/bage" &&
+ git push --recurse-submodules=check ../pub.git main
+ )
+'
+
+test_expect_success 'push fails if submodule commit not on remote' '
+ (
+ cd work/gar &&
+ git clone --bare bage ../../submodule.git &&
+ cd bage &&
+ git remote add origin ../../../submodule.git &&
+ git fetch &&
+ >junk3 &&
+ git add junk3 &&
+ git commit -m "Third junk"
+ ) &&
+ (
+ cd work &&
+ git add gar/bage &&
+ git commit -m "Third commit for gar/bage" &&
+ # the push should fail with --recurse-submodules=check
+ # on the command line...
+ test_must_fail git push --recurse-submodules=check ../pub.git main &&
+
+ # ...or if specified in the configuration..
+ test_must_fail git -c push.recurseSubmodules=check push ../pub.git main
+ )
+'
+
+test_expect_success 'push succeeds after commit was pushed to remote' '
+ (
+ cd work/gar/bage &&
+ git push origin main
+ ) &&
+ (
+ cd work &&
+ git push --recurse-submodules=check ../pub.git main
+ )
+'
+
+test_expect_success 'push succeeds if submodule commit not on remote but using on-demand on command line' '
+ (
+ cd work/gar/bage &&
+ >recurse-on-demand-on-command-line &&
+ git add recurse-on-demand-on-command-line &&
+ git commit -m "Recurse on-demand on command line junk"
+ ) &&
+ (
+ cd work &&
+ git add gar/bage &&
+ git commit -m "Recurse on-demand on command line for gar/bage" &&
+ git push --recurse-submodules=on-demand ../pub.git main &&
+ # Check that the supermodule commit got there
+ git fetch ../pub.git &&
+ git diff --quiet FETCH_HEAD main &&
+ # Check that the submodule commit got there too
+ cd gar/bage &&
+ git diff --quiet origin/main main
+ )
+'
+
+test_expect_success 'push succeeds if submodule commit not on remote but using on-demand from config' '
+ (
+ cd work/gar/bage &&
+ >recurse-on-demand-from-config &&
+ git add recurse-on-demand-from-config &&
+ git commit -m "Recurse on-demand from config junk"
+ ) &&
+ (
+ cd work &&
+ git add gar/bage &&
+ git commit -m "Recurse on-demand from config for gar/bage" &&
+ git -c push.recurseSubmodules=on-demand push ../pub.git main &&
+ # Check that the supermodule commit got there
+ git fetch ../pub.git &&
+ git diff --quiet FETCH_HEAD main &&
+ # Check that the submodule commit got there too
+ cd gar/bage &&
+ git diff --quiet origin/main main
+ )
+'
+
+test_expect_success 'push succeeds if submodule commit not on remote but using auto-on-demand via submodule.recurse config' '
+ (
+ cd work/gar/bage &&
+ >recurse-on-demand-from-submodule-recurse-config &&
+ git add recurse-on-demand-from-submodule-recurse-config &&
+ git commit -m "Recurse submodule.recurse from config junk"
+ ) &&
+ (
+ cd work &&
+ git add gar/bage &&
+ git commit -m "Recurse submodule.recurse from config for gar/bage" &&
+ git -c submodule.recurse push ../pub.git main &&
+ # Check that the supermodule commit got there
+ git fetch ../pub.git &&
+ git diff --quiet FETCH_HEAD main &&
+ # Check that the submodule commit got there too
+ cd gar/bage &&
+ git diff --quiet origin/main main
+ )
+'
+
+test_expect_success 'push recurse-submodules on command line overrides config' '
+ (
+ cd work/gar/bage &&
+ >recurse-check-on-command-line-overriding-config &&
+ git add recurse-check-on-command-line-overriding-config &&
+ git commit -m "Recurse on command-line overriding config junk"
+ ) &&
+ (
+ cd work &&
+ git add gar/bage &&
+ git commit -m "Recurse on command-line overriding config for gar/bage" &&
+
+ # Ensure that we can override on-demand in the config
+ # to just check submodules
+ test_must_fail git -c push.recurseSubmodules=on-demand push --recurse-submodules=check ../pub.git main &&
+ # Check that the supermodule commit did not get there
+ git fetch ../pub.git &&
+ git diff --quiet FETCH_HEAD main^ &&
+ # Check that the submodule commit did not get there
+ (cd gar/bage && git diff --quiet origin/main main^) &&
+
+ # Ensure that we can override check in the config to
+ # disable submodule recursion entirely
+ (cd gar/bage && git diff --quiet origin/main main^) &&
+ git -c push.recurseSubmodules=on-demand push --recurse-submodules=no ../pub.git main &&
+ git fetch ../pub.git &&
+ git diff --quiet FETCH_HEAD main &&
+ (cd gar/bage && git diff --quiet origin/main main^) &&
+
+ # Ensure that we can override check in the config to
+ # disable submodule recursion entirely (alternative form)
+ git -c push.recurseSubmodules=on-demand push --no-recurse-submodules ../pub.git main &&
+ git fetch ../pub.git &&
+ git diff --quiet FETCH_HEAD main &&
+ (cd gar/bage && git diff --quiet origin/main main^) &&
+
+ # Ensure that we can override check in the config to
+ # push the submodule too
+ git -c push.recurseSubmodules=check push --recurse-submodules=on-demand ../pub.git main &&
+ git fetch ../pub.git &&
+ git diff --quiet FETCH_HEAD main &&
+ (cd gar/bage && git diff --quiet origin/main main)
+ )
+'
+
+test_expect_success 'push recurse-submodules last one wins on command line' '
+ (
+ cd work/gar/bage &&
+ >recurse-check-on-command-line-overriding-earlier-command-line &&
+ git add recurse-check-on-command-line-overriding-earlier-command-line &&
+ git commit -m "Recurse on command-line overridiing earlier command-line junk"
+ ) &&
+ (
+ cd work &&
+ git add gar/bage &&
+ git commit -m "Recurse on command-line overriding earlier command-line for gar/bage" &&
+
+ # should result in "check"
+ test_must_fail git push --recurse-submodules=on-demand --recurse-submodules=check ../pub.git main &&
+ # Check that the supermodule commit did not get there
+ git fetch ../pub.git &&
+ git diff --quiet FETCH_HEAD main^ &&
+ # Check that the submodule commit did not get there
+ (cd gar/bage && git diff --quiet origin/main main^) &&
+
+ # should result in "no"
+ git push --recurse-submodules=on-demand --recurse-submodules=no ../pub.git main &&
+ # Check that the supermodule commit did get there
+ git fetch ../pub.git &&
+ git diff --quiet FETCH_HEAD main &&
+ # Check that the submodule commit did not get there
+ (cd gar/bage && git diff --quiet origin/main main^) &&
+
+ # should result in "no"
+ git push --recurse-submodules=on-demand --no-recurse-submodules ../pub.git main &&
+ # Check that the submodule commit did not get there
+ (cd gar/bage && git diff --quiet origin/main main^) &&
+
+ # But the options in the other order should push the submodule
+ git push --recurse-submodules=check --recurse-submodules=on-demand ../pub.git main &&
+ # Check that the submodule commit did get there
+ git fetch ../pub.git &&
+ (cd gar/bage && git diff --quiet origin/main main)
+ )
+'
+
+test_expect_success 'push succeeds if submodule commit not on remote using on-demand from cmdline overriding config' '
+ (
+ cd work/gar/bage &&
+ >recurse-on-demand-on-command-line-overriding-config &&
+ git add recurse-on-demand-on-command-line-overriding-config &&
+ git commit -m "Recurse on-demand on command-line overriding config junk"
+ ) &&
+ (
+ cd work &&
+ git add gar/bage &&
+ git commit -m "Recurse on-demand on command-line overriding config for gar/bage" &&
+ git -c push.recurseSubmodules=check push --recurse-submodules=on-demand ../pub.git main &&
+ # Check that the supermodule commit got there
+ git fetch ../pub.git &&
+ git diff --quiet FETCH_HEAD main &&
+ # Check that the submodule commit got there
+ cd gar/bage &&
+ git diff --quiet origin/main main
+ )
+'
+
+test_expect_success 'push succeeds if submodule commit disabling recursion from cmdline overriding config' '
+ (
+ cd work/gar/bage &&
+ >recurse-disable-on-command-line-overriding-config &&
+ git add recurse-disable-on-command-line-overriding-config &&
+ git commit -m "Recurse disable on command-line overriding config junk"
+ ) &&
+ (
+ cd work &&
+ git add gar/bage &&
+ git commit -m "Recurse disable on command-line overriding config for gar/bage" &&
+ git -c push.recurseSubmodules=check push --recurse-submodules=no ../pub.git main &&
+ # Check that the supermodule commit got there
+ git fetch ../pub.git &&
+ git diff --quiet FETCH_HEAD main &&
+ # But that the submodule commit did not
+ ( cd gar/bage && git diff --quiet origin/main main^ ) &&
+ # Now push it to avoid confusing future tests
+ git push --recurse-submodules=on-demand ../pub.git main
+ )
+'
+
+test_expect_success 'push succeeds if submodule commit disabling recursion from cmdline (alternative form) overriding config' '
+ (
+ cd work/gar/bage &&
+ >recurse-disable-on-command-line-alt-overriding-config &&
+ git add recurse-disable-on-command-line-alt-overriding-config &&
+ git commit -m "Recurse disable on command-line alternative overriding config junk"
+ ) &&
+ (
+ cd work &&
+ git add gar/bage &&
+ git commit -m "Recurse disable on command-line alternative overriding config for gar/bage" &&
+ git -c push.recurseSubmodules=check push --no-recurse-submodules ../pub.git main &&
+ # Check that the supermodule commit got there
+ git fetch ../pub.git &&
+ git diff --quiet FETCH_HEAD main &&
+ # But that the submodule commit did not
+ ( cd gar/bage && git diff --quiet origin/main main^ ) &&
+ # Now push it to avoid confusing future tests
+ git push --recurse-submodules=on-demand ../pub.git main
+ )
+'
+
+test_expect_success 'submodule entry pointing at a tag is error' '
+ git -C work/gar/bage tag -a test1 -m "tag" &&
+ tag=$(git -C work/gar/bage rev-parse test1^{tag}) &&
+ git -C work update-index --cacheinfo 160000 "$tag" gar/bage &&
+ git -C work commit -m "bad commit" &&
+ test_when_finished "git -C work reset --hard HEAD^" &&
+ test_must_fail git -C work push --recurse-submodules=on-demand ../pub.git main 2>err &&
+ test_grep "is a tag, not a commit" err
+'
+
+test_expect_success 'push fails if recurse submodules option passed as yes' '
+ (
+ cd work/gar/bage &&
+ >recurse-push-fails-if-recurse-submodules-passed-as-yes &&
+ git add recurse-push-fails-if-recurse-submodules-passed-as-yes &&
+ git commit -m "Recurse push fails if recurse submodules option passed as yes"
+ ) &&
+ (
+ cd work &&
+ git add gar/bage &&
+ git commit -m "Recurse push fails if recurse submodules option passed as yes for gar/bage" &&
+ test_must_fail git push --recurse-submodules=yes ../pub.git main &&
+ test_must_fail git -c push.recurseSubmodules=yes push ../pub.git main &&
+ git push --recurse-submodules=on-demand ../pub.git main
+ )
+'
+
+test_expect_success 'push fails when commit on multiple branches if one branch has no remote' '
+ (
+ cd work/gar/bage &&
+ >junk4 &&
+ git add junk4 &&
+ git commit -m "Fourth junk"
+ ) &&
+ (
+ cd work &&
+ git branch branch2 &&
+ git add gar/bage &&
+ git commit -m "Fourth commit for gar/bage" &&
+ git checkout branch2 &&
+ (
+ cd gar/bage &&
+ git checkout HEAD~1
+ ) &&
+ >junk1 &&
+ git add junk1 &&
+ git commit -m "First junk" &&
+ test_must_fail git push --recurse-submodules=check ../pub.git
+ )
+'
+
+test_expect_success 'push succeeds if submodule has no remote and is on the first superproject commit' '
+ git init --bare a &&
+ git clone a a1 &&
+ (
+ cd a1 &&
+ git init b &&
+ (
+ cd b &&
+ >junk &&
+ git add junk &&
+ git commit -m "initial"
+ ) &&
+ git add b &&
+ git commit -m "added submodule" &&
+ git push --recurse-submodules=check origin main
+ )
+'
+
+test_expect_success 'push unpushed submodules when not needed' '
+ (
+ cd work &&
+ (
+ cd gar/bage &&
+ git checkout main &&
+ >junk5 &&
+ git add junk5 &&
+ git commit -m "Fifth junk" &&
+ git push &&
+ git rev-parse origin/main >../../../expected
+ ) &&
+ git checkout main &&
+ git add gar/bage &&
+ git commit -m "Fifth commit for gar/bage" &&
+ git push --recurse-submodules=on-demand ../pub.git main
+ ) &&
+ (
+ cd submodule.git &&
+ git rev-parse main >../actual
+ ) &&
+ test_cmp expected actual
+'
+
+test_expect_success 'push unpushed submodules when not needed 2' '
+ (
+ cd submodule.git &&
+ git rev-parse main >../expected
+ ) &&
+ (
+ cd work &&
+ (
+ cd gar/bage &&
+ >junk6 &&
+ git add junk6 &&
+ git commit -m "Sixth junk"
+ ) &&
+ >junk2 &&
+ git add junk2 &&
+ git commit -m "Second junk for work" &&
+ git push --recurse-submodules=on-demand ../pub.git main
+ ) &&
+ (
+ cd submodule.git &&
+ git rev-parse main >../actual
+ ) &&
+ test_cmp expected actual
+'
+
+test_expect_success 'push unpushed submodules recursively' '
+ (
+ cd work &&
+ (
+ cd gar/bage &&
+ git checkout main &&
+ > junk7 &&
+ git add junk7 &&
+ git commit -m "Seventh junk" &&
+ git rev-parse main >../../../expected
+ ) &&
+ git checkout main &&
+ git add gar/bage &&
+ git commit -m "Seventh commit for gar/bage" &&
+ git push --recurse-submodules=on-demand ../pub.git main
+ ) &&
+ (
+ cd submodule.git &&
+ git rev-parse main >../actual
+ ) &&
+ test_cmp expected actual
+'
+
+test_expect_success 'push unpushable submodule recursively fails' '
+ (
+ cd work &&
+ (
+ cd gar/bage &&
+ git rev-parse origin/main >../../../expected &&
+ git checkout main~0 &&
+ > junk8 &&
+ git add junk8 &&
+ git commit -m "Eighth junk"
+ ) &&
+ git add gar/bage &&
+ git commit -m "Eighth commit for gar/bage" &&
+ test_must_fail git push --recurse-submodules=on-demand ../pub.git main
+ ) &&
+ (
+ cd submodule.git &&
+ git rev-parse main >../actual
+ ) &&
+ test_when_finished git -C work reset --hard main^ &&
+ test_cmp expected actual
+'
+
+test_expect_success 'push --dry-run does not recursively update submodules' '
+ (
+ cd work/gar/bage &&
+ git checkout main &&
+ git rev-parse main >../../../expected_submodule &&
+ > junk9 &&
+ git add junk9 &&
+ git commit -m "Ninth junk" &&
+
+ # Go up to 'work' directory
+ cd ../.. &&
+ git checkout main &&
+ git rev-parse main >../expected_pub &&
+ git add gar/bage &&
+ git commit -m "Ninth commit for gar/bage" &&
+ git push --dry-run --recurse-submodules=on-demand ../pub.git main
+ ) &&
+ git -C submodule.git rev-parse main >actual_submodule &&
+ git -C pub.git rev-parse main >actual_pub &&
+ test_cmp expected_pub actual_pub &&
+ test_cmp expected_submodule actual_submodule
+'
+
+test_expect_success 'push --dry-run does not recursively update submodules' '
+ git -C work push --dry-run --recurse-submodules=only ../pub.git main &&
+
+ git -C submodule.git rev-parse main >actual_submodule &&
+ git -C pub.git rev-parse main >actual_pub &&
+ test_cmp expected_pub actual_pub &&
+ test_cmp expected_submodule actual_submodule
+'
+
+test_expect_success 'push only unpushed submodules recursively' '
+ git -C work/gar/bage rev-parse main >expected_submodule &&
+ git -C pub.git rev-parse main >expected_pub &&
+
+ git -C work push --recurse-submodules=only ../pub.git main &&
+
+ git -C submodule.git rev-parse main >actual_submodule &&
+ git -C pub.git rev-parse main >actual_pub &&
+ test_cmp expected_submodule actual_submodule &&
+ test_cmp expected_pub actual_pub
+'
+
+setup_subsub () {
+ git init upstream &&
+ git init upstream/sub &&
+ git init upstream/sub/deepsub &&
+ test_commit -C upstream/sub/deepsub innermost &&
+ git -C upstream/sub submodule add ./deepsub deepsub &&
+ git -C upstream/sub commit -m middle &&
+ git -C upstream submodule add ./sub sub &&
+ git -C upstream commit -m outermost &&
+
+ git -c protocol.file.allow=always clone --recurse-submodules upstream downstream &&
+ git -C downstream/sub/deepsub checkout -b downstream-branch &&
+ git -C downstream/sub checkout -b downstream-branch &&
+ git -C downstream checkout -b downstream-branch
+}
+
+new_downstream_commits () {
+ test_commit -C downstream/sub/deepsub new-innermost &&
+ git -C downstream/sub add deepsub &&
+ git -C downstream/sub commit -m new-middle &&
+ git -C downstream add sub &&
+ git -C downstream commit -m new-outermost
+}
+
+test_expect_success 'push with push.recurseSubmodules=only on superproject' '
+ test_when_finished rm -rf upstream downstream &&
+ setup_subsub &&
+ new_downstream_commits &&
+ git -C downstream config push.recurseSubmodules only &&
+ git -C downstream push origin downstream-branch &&
+
+ test_must_fail git -C upstream rev-parse refs/heads/downstream-branch &&
+ git -C upstream/sub rev-parse refs/heads/downstream-branch &&
+ test_must_fail git -C upstream/sub/deepsub rev-parse refs/heads/downstream-branch
+'
+
+test_expect_success 'push with push.recurseSubmodules=only on superproject and top-level submodule' '
+ test_when_finished rm -rf upstream downstream &&
+ setup_subsub &&
+ new_downstream_commits &&
+ git -C downstream config push.recurseSubmodules only &&
+ git -C downstream/sub config push.recurseSubmodules only &&
+ git -C downstream push origin downstream-branch 2> err &&
+
+ test_must_fail git -C upstream rev-parse refs/heads/downstream-branch &&
+ git -C upstream/sub rev-parse refs/heads/downstream-branch &&
+ git -C upstream/sub/deepsub rev-parse refs/heads/downstream-branch &&
+ grep "recursing into submodule with push.recurseSubmodules=only; using on-demand instead" err
+'
+
+test_expect_success 'push propagating the remotes name to a submodule' '
+ git -C work remote add origin ../pub.git &&
+ git -C work remote add pub ../pub.git &&
+
+ > work/gar/bage/junk10 &&
+ git -C work/gar/bage add junk10 &&
+ git -C work/gar/bage commit -m "Tenth junk" &&
+ git -C work add gar/bage &&
+ git -C work commit -m "Tenth junk added to gar/bage" &&
+
+ # Fails when submodule does not have a matching remote
+ test_must_fail git -C work push --recurse-submodules=on-demand pub main &&
+ # Succeeds when submodules has matching remote and refspec
+ git -C work push --recurse-submodules=on-demand origin main &&
+
+ git -C submodule.git rev-parse main >actual_submodule &&
+ git -C pub.git rev-parse main >actual_pub &&
+ git -C work/gar/bage rev-parse main >expected_submodule &&
+ git -C work rev-parse main >expected_pub &&
+ test_cmp expected_submodule actual_submodule &&
+ test_cmp expected_pub actual_pub
+'
+
+test_expect_success 'push propagating refspec to a submodule' '
+ > work/gar/bage/junk11 &&
+ git -C work/gar/bage add junk11 &&
+ git -C work/gar/bage commit -m "Eleventh junk" &&
+
+ git -C work checkout branch2 &&
+ git -C work add gar/bage &&
+ git -C work commit -m "updating gar/bage in branch2" &&
+
+ # Fails when submodule does not have a matching branch
+ test_must_fail git -C work push --recurse-submodules=on-demand origin branch2 &&
+ # Fails when refspec includes an object id
+ test_must_fail git -C work push --recurse-submodules=on-demand origin \
+ "$(git -C work rev-parse branch2):refs/heads/branch2" &&
+ # Fails when refspec includes HEAD and parent and submodule do not
+ # have the same named branch checked out
+ test_must_fail git -C work push --recurse-submodules=on-demand origin \
+ HEAD:refs/heads/branch2 &&
+
+ git -C work/gar/bage branch branch2 main &&
+ git -C work push --recurse-submodules=on-demand origin branch2 &&
+
+ git -C submodule.git rev-parse branch2 >actual_submodule &&
+ git -C pub.git rev-parse branch2 >actual_pub &&
+ git -C work/gar/bage rev-parse branch2 >expected_submodule &&
+ git -C work rev-parse branch2 >expected_pub &&
+ test_cmp expected_submodule actual_submodule &&
+ test_cmp expected_pub actual_pub
+'
+
+test_expect_success 'push propagating HEAD refspec to a submodule' '
+ git -C work/gar/bage checkout branch2 &&
+ > work/gar/bage/junk12 &&
+ git -C work/gar/bage add junk12 &&
+ git -C work/gar/bage commit -m "Twelfth junk" &&
+
+ git -C work checkout branch2 &&
+ git -C work add gar/bage &&
+ git -C work commit -m "updating gar/bage in branch2" &&
+
+ # Passes since the superproject and submodules HEAD are both on branch2
+ git -C work push --recurse-submodules=on-demand origin \
+ HEAD:refs/heads/branch2 &&
+
+ git -C submodule.git rev-parse branch2 >actual_submodule &&
+ git -C pub.git rev-parse branch2 >actual_pub &&
+ git -C work/gar/bage rev-parse branch2 >expected_submodule &&
+ git -C work rev-parse branch2 >expected_pub &&
+ test_cmp expected_submodule actual_submodule &&
+ test_cmp expected_pub actual_pub
+'
+
+test_done
diff --git a/t/t5532-fetch-proxy.sh b/t/t5532-fetch-proxy.sh
new file mode 100755
index 0000000..d664912
--- /dev/null
+++ b/t/t5532-fetch-proxy.sh
@@ -0,0 +1,53 @@
+#!/bin/sh
+
+test_description='fetching via git:// using core.gitproxy'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup remote repo' '
+ git init remote &&
+ (cd remote &&
+ echo content >file &&
+ git add file &&
+ git commit -m one
+ )
+'
+
+test_expect_success 'setup proxy script' '
+ write_script proxy-get-cmd "$PERL_PATH" <<-\EOF &&
+ read(STDIN, $buf, 4);
+ my $n = hex($buf) - 4;
+ read(STDIN, $buf, $n);
+ my ($cmd, $other) = split /\0/, $buf;
+ # drop absolute-path on repo name
+ $cmd =~ s{ /}{ };
+ print $cmd;
+ EOF
+
+ write_script proxy <<-\EOF
+ echo >&2 "proxying for $*"
+ cmd=$(./proxy-get-cmd)
+ echo >&2 "Running $cmd"
+ exec $cmd
+ EOF
+'
+
+test_expect_success 'setup local repo' '
+ git remote add fake git://example.com/remote &&
+ git config core.gitproxy ./proxy
+'
+
+test_expect_success 'fetch through proxy works' '
+ git fetch fake &&
+ echo one >expect &&
+ git log -1 --format=%s FETCH_HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'funny hostnames are rejected before running proxy' '
+ test_must_fail git fetch git://-remote/repo.git 2>stderr &&
+ ! grep "proxying for" stderr
+'
+
+test_done
diff --git a/t/t5533-push-cas.sh b/t/t5533-push-cas.sh
new file mode 100755
index 0000000..cba26a8
--- /dev/null
+++ b/t/t5533-push-cas.sh
@@ -0,0 +1,399 @@
+#!/bin/sh
+
+test_description='compare & swap push force/delete safety'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+setup_srcdst_basic () {
+ rm -fr src dst &&
+ git clone --no-local . src &&
+ git clone --no-local src dst &&
+ (
+ cd src && git checkout HEAD^0
+ )
+}
+
+# For tests with "--force-if-includes".
+setup_src_dup_dst () {
+ rm -fr src dup dst &&
+ git init --bare dst &&
+ git clone --no-local dst src &&
+ git clone --no-local dst dup
+ (
+ cd src &&
+ test_commit A &&
+ test_commit B &&
+ test_commit C &&
+ git push origin
+ ) &&
+ (
+ cd dup &&
+ git fetch &&
+ git merge origin/main &&
+ git switch -c branch main~2 &&
+ test_commit D &&
+ test_commit E &&
+ git push origin --all
+ ) &&
+ (
+ cd src &&
+ git switch main &&
+ git fetch --all &&
+ git branch branch --track origin/branch &&
+ git rebase origin/main
+ ) &&
+ (
+ cd dup &&
+ git switch main &&
+ test_commit F &&
+ test_commit G &&
+ git switch branch &&
+ test_commit H &&
+ git push origin --all
+ )
+}
+
+test_expect_success setup '
+ # create template repository
+ test_commit A &&
+ test_commit B &&
+ test_commit C
+'
+
+test_expect_success 'push to update (protected)' '
+ setup_srcdst_basic &&
+ (
+ cd dst &&
+ test_commit D &&
+ test_must_fail git push --force-with-lease=main:main origin main 2>err &&
+ grep "stale info" err
+ ) &&
+ git ls-remote . refs/heads/main >expect &&
+ git ls-remote src refs/heads/main >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'push to update (protected, forced)' '
+ setup_srcdst_basic &&
+ (
+ cd dst &&
+ test_commit D &&
+ git push --force --force-with-lease=main:main origin main 2>err &&
+ grep "forced update" err
+ ) &&
+ git ls-remote dst refs/heads/main >expect &&
+ git ls-remote src refs/heads/main >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'push to update (protected, tracking)' '
+ setup_srcdst_basic &&
+ (
+ cd src &&
+ git checkout main &&
+ test_commit D &&
+ git checkout HEAD^0
+ ) &&
+ git ls-remote src refs/heads/main >expect &&
+ (
+ cd dst &&
+ test_commit E &&
+ git ls-remote . refs/remotes/origin/main >expect &&
+ test_must_fail git push --force-with-lease=main origin main &&
+ git ls-remote . refs/remotes/origin/main >actual &&
+ test_cmp expect actual
+ ) &&
+ git ls-remote src refs/heads/main >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'push to update (protected, tracking, forced)' '
+ setup_srcdst_basic &&
+ (
+ cd src &&
+ git checkout main &&
+ test_commit D &&
+ git checkout HEAD^0
+ ) &&
+ (
+ cd dst &&
+ test_commit E &&
+ git ls-remote . refs/remotes/origin/main >expect &&
+ git push --force --force-with-lease=main origin main
+ ) &&
+ git ls-remote dst refs/heads/main >expect &&
+ git ls-remote src refs/heads/main >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'push to update (allowed)' '
+ setup_srcdst_basic &&
+ (
+ cd dst &&
+ test_commit D &&
+ git push --force-with-lease=main:main^ origin main
+ ) &&
+ git ls-remote dst refs/heads/main >expect &&
+ git ls-remote src refs/heads/main >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'push to update (allowed, tracking)' '
+ setup_srcdst_basic &&
+ (
+ cd dst &&
+ test_commit D &&
+ git push --force-with-lease=main origin main 2>err &&
+ ! grep "forced update" err
+ ) &&
+ git ls-remote dst refs/heads/main >expect &&
+ git ls-remote src refs/heads/main >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'push to update (allowed even though no-ff)' '
+ setup_srcdst_basic &&
+ (
+ cd dst &&
+ git reset --hard HEAD^ &&
+ test_commit D &&
+ git push --force-with-lease=main origin main 2>err &&
+ grep "forced update" err
+ ) &&
+ git ls-remote dst refs/heads/main >expect &&
+ git ls-remote src refs/heads/main >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'push to delete (protected)' '
+ setup_srcdst_basic &&
+ git ls-remote src refs/heads/main >expect &&
+ (
+ cd dst &&
+ test_must_fail git push --force-with-lease=main:main^ origin :main
+ ) &&
+ git ls-remote src refs/heads/main >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'push to delete (protected, forced)' '
+ setup_srcdst_basic &&
+ (
+ cd dst &&
+ git push --force --force-with-lease=main:main^ origin :main
+ ) &&
+ git ls-remote src refs/heads/main >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'push to delete (allowed)' '
+ setup_srcdst_basic &&
+ (
+ cd dst &&
+ git push --force-with-lease=main origin :main 2>err &&
+ grep deleted err
+ ) &&
+ git ls-remote src refs/heads/main >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'cover everything with default force-with-lease (protected)' '
+ setup_srcdst_basic &&
+ (
+ cd src &&
+ git branch nain main^
+ ) &&
+ git ls-remote src refs/heads/\* >expect &&
+ (
+ cd dst &&
+ test_must_fail git push --force-with-lease origin main main:nain
+ ) &&
+ git ls-remote src refs/heads/\* >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'cover everything with default force-with-lease (allowed)' '
+ setup_srcdst_basic &&
+ (
+ cd src &&
+ git branch nain main^
+ ) &&
+ (
+ cd dst &&
+ git fetch &&
+ git push --force-with-lease origin main main:nain
+ ) &&
+ git ls-remote dst refs/heads/main |
+ sed -e "s/main/nain/" >expect &&
+ git ls-remote src refs/heads/nain >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'new branch covered by force-with-lease' '
+ setup_srcdst_basic &&
+ (
+ cd dst &&
+ git branch branch main &&
+ git push --force-with-lease=branch origin branch
+ ) &&
+ git ls-remote dst refs/heads/branch >expect &&
+ git ls-remote src refs/heads/branch >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'new branch covered by force-with-lease (explicit)' '
+ setup_srcdst_basic &&
+ (
+ cd dst &&
+ git branch branch main &&
+ git push --force-with-lease=branch: origin branch
+ ) &&
+ git ls-remote dst refs/heads/branch >expect &&
+ git ls-remote src refs/heads/branch >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'new branch already exists' '
+ setup_srcdst_basic &&
+ (
+ cd src &&
+ git checkout -b branch main &&
+ test_commit F
+ ) &&
+ (
+ cd dst &&
+ git branch branch main &&
+ test_must_fail git push --force-with-lease=branch: origin branch
+ )
+'
+
+test_expect_success 'background updates of REMOTE can be mitigated with a non-updated REMOTE-push' '
+ rm -rf src dst &&
+ git init --bare src.bare &&
+ test_when_finished "rm -rf src.bare" &&
+ git clone --no-local src.bare dst &&
+ test_when_finished "rm -rf dst" &&
+ (
+ cd dst &&
+ test_commit G &&
+ git remote add origin-push ../src.bare &&
+ git push origin-push main:main
+ ) &&
+ git clone --no-local src.bare dst2 &&
+ test_when_finished "rm -rf dst2" &&
+ (
+ cd dst2 &&
+ test_commit H &&
+ git push
+ ) &&
+ (
+ cd dst &&
+ test_commit I &&
+ git fetch origin &&
+ test_must_fail git push --force-with-lease origin-push &&
+ git fetch origin-push &&
+ git push --force-with-lease origin-push
+ )
+'
+
+test_expect_success 'background updates to remote can be mitigated with "--force-if-includes"' '
+ setup_src_dup_dst &&
+ test_when_finished "rm -fr dst src dup" &&
+ git ls-remote dst refs/heads/main >expect.main &&
+ git ls-remote dst refs/heads/branch >expect.branch &&
+ (
+ cd src &&
+ git switch branch &&
+ test_commit I &&
+ git switch main &&
+ test_commit J &&
+ git fetch --all &&
+ test_must_fail git push --force-with-lease --force-if-includes --all
+ ) &&
+ git ls-remote dst refs/heads/main >actual.main &&
+ git ls-remote dst refs/heads/branch >actual.branch &&
+ test_cmp expect.main actual.main &&
+ test_cmp expect.branch actual.branch
+'
+
+test_expect_success 'background updates to remote can be mitigated with "push.useForceIfIncludes"' '
+ setup_src_dup_dst &&
+ test_when_finished "rm -fr dst src dup" &&
+ git ls-remote dst refs/heads/main >expect.main &&
+ (
+ cd src &&
+ git switch branch &&
+ test_commit I &&
+ git switch main &&
+ test_commit J &&
+ git fetch --all &&
+ git config --local push.useForceIfIncludes true &&
+ test_must_fail git push --force-with-lease=main origin main
+ ) &&
+ git ls-remote dst refs/heads/main >actual.main &&
+ test_cmp expect.main actual.main
+'
+
+test_expect_success '"--force-if-includes" should be disabled for --force-with-lease="<refname>:<expect>"' '
+ setup_src_dup_dst &&
+ test_when_finished "rm -fr dst src dup" &&
+ git ls-remote dst refs/heads/main >expect.main &&
+ (
+ cd src &&
+ git switch branch &&
+ test_commit I &&
+ git switch main &&
+ test_commit J &&
+ remote_head="$(git rev-parse refs/remotes/origin/main)" &&
+ git fetch --all &&
+ test_must_fail git push --force-if-includes --force-with-lease="main:$remote_head" 2>err &&
+ grep "stale info" err
+ ) &&
+ git ls-remote dst refs/heads/main >actual.main &&
+ test_cmp expect.main actual.main
+'
+
+test_expect_success '"--force-if-includes" should allow forced update after a rebase ("pull --rebase")' '
+ setup_src_dup_dst &&
+ test_when_finished "rm -fr dst src dup" &&
+ (
+ cd src &&
+ git switch branch &&
+ test_commit I &&
+ git switch main &&
+ test_commit J &&
+ git pull --rebase origin main &&
+ git push --force-if-includes --force-with-lease="main"
+ )
+'
+
+test_expect_success '"--force-if-includes" should allow forced update after a rebase ("pull --rebase", local rebase)' '
+ setup_src_dup_dst &&
+ test_when_finished "rm -fr dst src dup" &&
+ (
+ cd src &&
+ git switch branch &&
+ test_commit I &&
+ git switch main &&
+ test_commit J &&
+ git pull --rebase origin main &&
+ git rebase --onto HEAD~4 HEAD~1 &&
+ git push --force-if-includes --force-with-lease="main"
+ )
+'
+
+test_expect_success '"--force-if-includes" should allow deletes' '
+ setup_src_dup_dst &&
+ test_when_finished "rm -fr dst src dup" &&
+ (
+ cd src &&
+ git switch branch &&
+ git pull --rebase origin branch &&
+ git push --force-if-includes --force-with-lease="branch" origin :branch
+ )
+'
+
+test_done
diff --git a/t/t5534-push-signed.sh b/t/t5534-push-signed.sh
new file mode 100755
index 0000000..b4bc246
--- /dev/null
+++ b/t/t5534-push-signed.sh
@@ -0,0 +1,392 @@
+#!/bin/sh
+
+test_description='signed push'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-gpg.sh
+
+prepare_dst () {
+ rm -fr dst &&
+ test_create_repo dst &&
+
+ git push dst main:noop main:ff main:noff
+}
+
+test_expect_success setup '
+ # main, ff and noff branches pointing at the same commit
+ test_tick &&
+ git commit --allow-empty -m initial &&
+
+ git checkout -b noop &&
+ git checkout -b ff &&
+ git checkout -b noff &&
+
+ # noop stays the same, ff advances, noff rewrites
+ test_tick &&
+ git commit --allow-empty --amend -m rewritten &&
+ git checkout ff &&
+
+ test_tick &&
+ git commit --allow-empty -m second
+'
+
+test_expect_success 'unsigned push does not send push certificate' '
+ prepare_dst &&
+ test_hook -C dst post-receive <<-\EOF &&
+ # discard the update list
+ cat >/dev/null
+ # record the push certificate
+ if test -n "${GIT_PUSH_CERT-}"
+ then
+ git cat-file blob $GIT_PUSH_CERT >../push-cert
+ fi
+ EOF
+
+ git push dst noop ff +noff &&
+ ! test -f dst/push-cert
+'
+
+test_expect_success 'talking with a receiver without push certificate support' '
+ prepare_dst &&
+ test_hook -C dst post-receive <<-\EOF &&
+ # discard the update list
+ cat >/dev/null
+ # record the push certificate
+ if test -n "${GIT_PUSH_CERT-}"
+ then
+ git cat-file blob $GIT_PUSH_CERT >../push-cert
+ fi
+ EOF
+
+ git push dst noop ff +noff &&
+ ! test -f dst/push-cert
+'
+
+test_expect_success 'push --signed fails with a receiver without push certificate support' '
+ prepare_dst &&
+ test_must_fail git push --signed dst noop ff +noff 2>err &&
+ test_grep "the receiving end does not support" err
+'
+
+test_expect_success 'push --signed=1 is accepted' '
+ prepare_dst &&
+ test_must_fail git push --signed=1 dst noop ff +noff 2>err &&
+ test_grep "the receiving end does not support" err
+'
+
+test_expect_success GPG 'no certificate for a signed push with no update' '
+ prepare_dst &&
+ test_hook -C dst post-receive <<-\EOF &&
+ if test -n "${GIT_PUSH_CERT-}"
+ then
+ git cat-file blob $GIT_PUSH_CERT >../push-cert
+ fi
+ EOF
+ git push dst noop &&
+ ! test -f dst/push-cert
+'
+
+test_expect_success GPG 'signed push sends push certificate' '
+ prepare_dst &&
+ git -C dst config receive.certnonceseed sekrit &&
+ test_hook -C dst post-receive <<-\EOF &&
+ # discard the update list
+ cat >/dev/null
+ # record the push certificate
+ if test -n "${GIT_PUSH_CERT-}"
+ then
+ git cat-file blob $GIT_PUSH_CERT >../push-cert
+ fi &&
+
+ cat >../push-cert-status <<E_O_F
+ SIGNER=${GIT_PUSH_CERT_SIGNER-nobody}
+ KEY=${GIT_PUSH_CERT_KEY-nokey}
+ STATUS=${GIT_PUSH_CERT_STATUS-nostatus}
+ NONCE_STATUS=${GIT_PUSH_CERT_NONCE_STATUS-nononcestatus}
+ NONCE=${GIT_PUSH_CERT_NONCE-nononce}
+ E_O_F
+
+ EOF
+
+ git push --signed dst noop ff +noff &&
+
+ (
+ cat <<-\EOF &&
+ SIGNER=C O Mitter <committer@example.com>
+ KEY=13B6F51ECDDE430D
+ STATUS=G
+ NONCE_STATUS=OK
+ EOF
+ sed -n -e "s/^nonce /NONCE=/p" -e "/^$/q" dst/push-cert
+ ) >expect &&
+
+ noop=$(git rev-parse noop) &&
+ ff=$(git rev-parse ff) &&
+ noff=$(git rev-parse noff) &&
+ grep "$noop $ff refs/heads/ff" dst/push-cert &&
+ grep "$noop $noff refs/heads/noff" dst/push-cert &&
+ test_cmp expect dst/push-cert-status
+'
+
+test_expect_success GPGSSH 'ssh signed push sends push certificate' '
+ prepare_dst &&
+ git -C dst config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ git -C dst config receive.certnonceseed sekrit &&
+ test_hook -C dst post-receive <<-\EOF &&
+ # discard the update list
+ cat >/dev/null
+ # record the push certificate
+ if test -n "${GIT_PUSH_CERT-}"
+ then
+ git cat-file blob $GIT_PUSH_CERT >../push-cert
+ fi &&
+
+ cat >../push-cert-status <<E_O_F
+ SIGNER=${GIT_PUSH_CERT_SIGNER-nobody}
+ KEY=${GIT_PUSH_CERT_KEY-nokey}
+ STATUS=${GIT_PUSH_CERT_STATUS-nostatus}
+ NONCE_STATUS=${GIT_PUSH_CERT_NONCE_STATUS-nononcestatus}
+ NONCE=${GIT_PUSH_CERT_NONCE-nononce}
+ E_O_F
+
+ EOF
+
+ test_config gpg.format ssh &&
+ test_config user.signingkey "${GPGSSH_KEY_PRIMARY}" &&
+ FINGERPRINT=$(ssh-keygen -lf "${GPGSSH_KEY_PRIMARY}" | awk "{print \$2;}") &&
+ git push --signed dst noop ff +noff &&
+
+ (
+ cat <<-\EOF &&
+ SIGNER=principal with number 1
+ KEY=FINGERPRINT
+ STATUS=G
+ NONCE_STATUS=OK
+ EOF
+ sed -n -e "s/^nonce /NONCE=/p" -e "/^$/q" dst/push-cert
+ ) | sed -e "s|FINGERPRINT|$FINGERPRINT|" >expect &&
+
+ noop=$(git rev-parse noop) &&
+ ff=$(git rev-parse ff) &&
+ noff=$(git rev-parse noff) &&
+ grep "$noop $ff refs/heads/ff" dst/push-cert &&
+ grep "$noop $noff refs/heads/noff" dst/push-cert &&
+ test_cmp expect dst/push-cert-status
+'
+
+test_expect_success GPG 'inconsistent push options in signed push not allowed' '
+ # First, invoke receive-pack with dummy input to obtain its preamble.
+ prepare_dst &&
+ git -C dst config receive.certnonceseed sekrit &&
+ git -C dst config receive.advertisepushoptions 1 &&
+ printf xxxx | test_might_fail git receive-pack dst >preamble &&
+
+ # Then, invoke push. Simulate a receive-pack that sends the preamble we
+ # obtained, followed by a dummy packet.
+ write_script myscript <<-\EOF &&
+ cat preamble &&
+ printf xxxx &&
+ cat >push
+ EOF
+ test_might_fail git push --push-option="foo" --push-option="bar" \
+ --receive-pack="\"$(pwd)/myscript\"" --signed dst --delete ff &&
+
+ # Replay the push output on a fresh dst, checking that ff is truly
+ # deleted.
+ prepare_dst &&
+ git -C dst config receive.certnonceseed sekrit &&
+ git -C dst config receive.advertisepushoptions 1 &&
+ git receive-pack dst <push &&
+ test_must_fail git -C dst rev-parse ff &&
+
+ # Tweak the push output to make the push option outside the cert
+ # different, then replay it on a fresh dst, checking that ff is not
+ # deleted.
+ perl -pe "s/([^ ])bar/\$1baz/" push >push.tweak &&
+ prepare_dst &&
+ git -C dst config receive.certnonceseed sekrit &&
+ git -C dst config receive.advertisepushoptions 1 &&
+ git receive-pack dst <push.tweak >out &&
+ git -C dst rev-parse ff &&
+ grep "inconsistent push options" out
+'
+
+test_expect_success GPG 'fail without key and heed user.signingkey' '
+ prepare_dst &&
+ git -C dst config receive.certnonceseed sekrit &&
+ test_hook -C dst post-receive <<-\EOF &&
+ # discard the update list
+ cat >/dev/null
+ # record the push certificate
+ if test -n "${GIT_PUSH_CERT-}"
+ then
+ git cat-file blob $GIT_PUSH_CERT >../push-cert
+ fi &&
+
+ cat >../push-cert-status <<E_O_F
+ SIGNER=${GIT_PUSH_CERT_SIGNER-nobody}
+ KEY=${GIT_PUSH_CERT_KEY-nokey}
+ STATUS=${GIT_PUSH_CERT_STATUS-nostatus}
+ NONCE_STATUS=${GIT_PUSH_CERT_NONCE_STATUS-nononcestatus}
+ NONCE=${GIT_PUSH_CERT_NONCE-nononce}
+ E_O_F
+
+ EOF
+
+ test_config user.email hasnokey@nowhere.com &&
+ (
+ sane_unset GIT_COMMITTER_EMAIL &&
+ test_must_fail git push --signed dst noop ff +noff
+ ) &&
+ test_config user.signingkey $GIT_COMMITTER_EMAIL &&
+ git push --signed dst noop ff +noff &&
+
+ (
+ cat <<-\EOF &&
+ SIGNER=C O Mitter <committer@example.com>
+ KEY=13B6F51ECDDE430D
+ STATUS=G
+ NONCE_STATUS=OK
+ EOF
+ sed -n -e "s/^nonce /NONCE=/p" -e "/^$/q" dst/push-cert
+ ) >expect &&
+
+ noop=$(git rev-parse noop) &&
+ ff=$(git rev-parse ff) &&
+ noff=$(git rev-parse noff) &&
+ grep "$noop $ff refs/heads/ff" dst/push-cert &&
+ grep "$noop $noff refs/heads/noff" dst/push-cert &&
+ test_cmp expect dst/push-cert-status
+'
+
+test_expect_success GPGSM 'fail without key and heed user.signingkey x509' '
+ test_config gpg.format x509 &&
+ prepare_dst &&
+ git -C dst config receive.certnonceseed sekrit &&
+ test_hook -C dst post-receive <<-\EOF &&
+ # discard the update list
+ cat >/dev/null
+ # record the push certificate
+ if test -n "${GIT_PUSH_CERT-}"
+ then
+ git cat-file blob $GIT_PUSH_CERT >../push-cert
+ fi &&
+
+ cat >../push-cert-status <<E_O_F
+ SIGNER=${GIT_PUSH_CERT_SIGNER-nobody}
+ KEY=${GIT_PUSH_CERT_KEY-nokey}
+ STATUS=${GIT_PUSH_CERT_STATUS-nostatus}
+ NONCE_STATUS=${GIT_PUSH_CERT_NONCE_STATUS-nononcestatus}
+ NONCE=${GIT_PUSH_CERT_NONCE-nononce}
+ E_O_F
+
+ EOF
+
+ test_config user.email hasnokey@nowhere.com &&
+ test_config user.signingkey "" &&
+ (
+ sane_unset GIT_COMMITTER_EMAIL &&
+ test_must_fail git push --signed dst noop ff +noff
+ ) &&
+ test_config user.signingkey $GIT_COMMITTER_EMAIL &&
+ git push --signed dst noop ff +noff &&
+
+ (
+ cat <<-\EOF &&
+ SIGNER=/CN=C O Mitter/O=Example/SN=C O/GN=Mitter
+ KEY=
+ STATUS=G
+ NONCE_STATUS=OK
+ EOF
+ sed -n -e "s/^nonce /NONCE=/p" -e "/^$/q" dst/push-cert
+ ) >expect.in &&
+ key=$(cat "${GNUPGHOME}/trustlist.txt" | cut -d" " -f1 | tr -d ":") &&
+ sed -e "s/^KEY=/KEY=${key}/" expect.in >expect &&
+
+ noop=$(git rev-parse noop) &&
+ ff=$(git rev-parse ff) &&
+ noff=$(git rev-parse noff) &&
+ grep "$noop $ff refs/heads/ff" dst/push-cert &&
+ grep "$noop $noff refs/heads/noff" dst/push-cert &&
+ test_cmp expect dst/push-cert-status
+'
+
+test_expect_success GPGSSH 'fail without key and heed user.signingkey ssh' '
+ test_config gpg.format ssh &&
+ prepare_dst &&
+ git -C dst config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ git -C dst config receive.certnonceseed sekrit &&
+ test_hook -C dst post-receive <<-\EOF &&
+ # discard the update list
+ cat >/dev/null
+ # record the push certificate
+ if test -n "${GIT_PUSH_CERT-}"
+ then
+ git cat-file blob $GIT_PUSH_CERT >../push-cert
+ fi &&
+
+ cat >../push-cert-status <<E_O_F
+ SIGNER=${GIT_PUSH_CERT_SIGNER-nobody}
+ KEY=${GIT_PUSH_CERT_KEY-nokey}
+ STATUS=${GIT_PUSH_CERT_STATUS-nostatus}
+ NONCE_STATUS=${GIT_PUSH_CERT_NONCE_STATUS-nononcestatus}
+ NONCE=${GIT_PUSH_CERT_NONCE-nononce}
+ E_O_F
+
+ EOF
+
+ test_config user.email hasnokey@nowhere.com &&
+ test_config gpg.format ssh &&
+ test_config user.signingkey "" &&
+ (
+ sane_unset GIT_COMMITTER_EMAIL &&
+ test_must_fail git push --signed dst noop ff +noff
+ ) &&
+ test_config user.signingkey "${GPGSSH_KEY_PRIMARY}" &&
+ FINGERPRINT=$(ssh-keygen -lf "${GPGSSH_KEY_PRIMARY}" | awk "{print \$2;}") &&
+ git push --signed dst noop ff +noff &&
+
+ (
+ cat <<-\EOF &&
+ SIGNER=principal with number 1
+ KEY=FINGERPRINT
+ STATUS=G
+ NONCE_STATUS=OK
+ EOF
+ sed -n -e "s/^nonce /NONCE=/p" -e "/^$/q" dst/push-cert
+ ) | sed -e "s|FINGERPRINT|$FINGERPRINT|" >expect &&
+
+ noop=$(git rev-parse noop) &&
+ ff=$(git rev-parse ff) &&
+ noff=$(git rev-parse noff) &&
+ grep "$noop $ff refs/heads/ff" dst/push-cert &&
+ grep "$noop $noff refs/heads/noff" dst/push-cert &&
+ test_cmp expect dst/push-cert-status
+'
+
+test_expect_success GPG 'failed atomic push does not execute GPG' '
+ prepare_dst &&
+ git -C dst config receive.certnonceseed sekrit &&
+ write_script gpg <<-EOF &&
+ # should check atomic push locally before running GPG.
+ exit 1
+ EOF
+ test_must_fail env PATH="$TRASH_DIRECTORY:$PATH" git push \
+ --signed --atomic --porcelain \
+ dst noop ff noff >out 2>err &&
+
+ test_grep ! "gpg failed to sign" err &&
+ cat >expect <<-EOF &&
+ To dst
+ = refs/heads/noop:refs/heads/noop [up to date]
+ ! refs/heads/ff:refs/heads/ff [rejected] (atomic push failed)
+ ! refs/heads/noff:refs/heads/noff [rejected] (non-fast-forward)
+ Done
+ EOF
+ test_cmp expect out
+'
+
+test_done
diff --git a/t/t5535-fetch-push-symref.sh b/t/t5535-fetch-push-symref.sh
new file mode 100755
index 0000000..e8f6d23
--- /dev/null
+++ b/t/t5535-fetch-push-symref.sh
@@ -0,0 +1,42 @@
+#!/bin/sh
+
+test_description='avoiding conflicting update through symref aliasing'
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ test_commit one &&
+ git clone . src &&
+ git clone src dst1 &&
+ git clone src dst2 &&
+ test_commit two &&
+ ( cd src && git pull )
+'
+
+test_expect_success 'push' '
+ (
+ cd src &&
+ git push ../dst1 "refs/remotes/*:refs/remotes/*"
+ ) &&
+ git ls-remote src "refs/remotes/*" >expect &&
+ git ls-remote dst1 "refs/remotes/*" >actual &&
+ test_cmp expect actual &&
+ ( cd src && git symbolic-ref refs/remotes/origin/HEAD ) >expect &&
+ ( cd dst1 && git symbolic-ref refs/remotes/origin/HEAD ) >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'fetch' '
+ (
+ cd dst2 &&
+ git fetch ../src "refs/remotes/*:refs/remotes/*"
+ ) &&
+ git ls-remote src "refs/remotes/*" >expect &&
+ git ls-remote dst2 "refs/remotes/*" >actual &&
+ test_cmp expect actual &&
+ ( cd src && git symbolic-ref refs/remotes/origin/HEAD ) >expect &&
+ ( cd dst2 && git symbolic-ref refs/remotes/origin/HEAD ) >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t5536-fetch-conflicts.sh b/t/t5536-fetch-conflicts.sh
new file mode 100755
index 0000000..23bf696
--- /dev/null
+++ b/t/t5536-fetch-conflicts.sh
@@ -0,0 +1,86 @@
+#!/bin/sh
+
+test_description='fetch handles conflicting refspecs correctly'
+
+. ./test-lib.sh
+
+D=$(pwd)
+
+setup_repository () {
+ git init "$1" && (
+ cd "$1" &&
+ git config remote.origin.url "$D" &&
+ shift &&
+ for refspec in "$@"
+ do
+ git config --add remote.origin.fetch "$refspec"
+ done
+ )
+}
+
+test_expect_success 'setup' '
+ git commit --allow-empty -m "Initial" &&
+ git branch branch1 &&
+ git tag tag1 &&
+ git commit --allow-empty -m "First" &&
+ git branch branch2 &&
+ git tag tag2
+'
+
+test_expect_success 'fetch with no conflict' '
+ setup_repository ok "+refs/heads/*:refs/remotes/origin/*" && (
+ cd ok &&
+ git fetch origin
+ )
+'
+
+test_expect_success 'fetch conflict: config vs. config' '
+ setup_repository ccc \
+ "+refs/heads/branch1:refs/remotes/origin/branch1" \
+ "+refs/heads/branch2:refs/remotes/origin/branch1" && (
+ cd ccc &&
+ test_must_fail git fetch origin 2>error &&
+ test_grep "fatal: Cannot fetch both refs/heads/branch1 and refs/heads/branch2 to refs/remotes/origin/branch1" error
+ )
+'
+
+test_expect_success 'fetch duplicate: config vs. config' '
+ setup_repository dcc \
+ "+refs/heads/*:refs/remotes/origin/*" \
+ "+refs/heads/branch1:refs/remotes/origin/branch1" && (
+ cd dcc &&
+ git fetch origin
+ )
+'
+
+test_expect_success 'fetch conflict: arg overrides config' '
+ setup_repository aoc \
+ "+refs/heads/*:refs/remotes/origin/*" && (
+ cd aoc &&
+ git fetch origin refs/heads/branch2:refs/remotes/origin/branch1
+ )
+'
+
+test_expect_success 'fetch conflict: arg vs. arg' '
+ setup_repository caa && (
+ cd caa &&
+ test_must_fail git fetch origin \
+ refs/heads/*:refs/remotes/origin/* \
+ refs/heads/branch2:refs/remotes/origin/branch1 2>error &&
+ test_grep "fatal: Cannot fetch both refs/heads/branch1 and refs/heads/branch2 to refs/remotes/origin/branch1" error
+ )
+'
+
+test_expect_success 'fetch conflict: criss-cross args' '
+ setup_repository xaa \
+ "+refs/heads/*:refs/remotes/origin/*" && (
+ cd xaa &&
+ git fetch origin \
+ refs/heads/branch1:refs/remotes/origin/branch2 \
+ refs/heads/branch2:refs/remotes/origin/branch1 2>error &&
+ test_grep "warning: refs/remotes/origin/branch1 usually tracks refs/heads/branch1, not refs/heads/branch2" error &&
+ test_grep "warning: refs/remotes/origin/branch2 usually tracks refs/heads/branch2, not refs/heads/branch1" error
+ )
+'
+
+test_done
diff --git a/t/t5537-fetch-shallow.sh b/t/t5537-fetch-shallow.sh
new file mode 100755
index 0000000..37f7547
--- /dev/null
+++ b/t/t5537-fetch-shallow.sh
@@ -0,0 +1,299 @@
+#!/bin/sh
+
+test_description='fetch/clone from a shallow clone'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+commit() {
+ echo "$1" >tracked &&
+ git add tracked &&
+ git commit -m "$1"
+}
+
+test_expect_success 'setup' '
+ commit 1 &&
+ commit 2 &&
+ commit 3 &&
+ commit 4 &&
+ git config --global transfer.fsckObjects true &&
+ test_oid_cache <<-\EOF
+ perl sha1:s/0034shallow %s/0036unshallow %s/
+ perl sha256:s/004cshallow %s/004eunshallow %s/
+ EOF
+'
+
+test_expect_success 'setup shallow clone' '
+ git clone --no-local --depth=2 .git shallow &&
+ git --git-dir=shallow/.git log --format=%s >actual &&
+ test_write_lines 4 3 >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'clone from shallow clone' '
+ git clone --no-local shallow shallow2 &&
+ (
+ cd shallow2 &&
+ git fsck &&
+ git log --format=%s >actual &&
+ test_write_lines 4 3 >expect &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'fetch from shallow clone' '
+ (
+ cd shallow &&
+ commit 5
+ ) &&
+ (
+ cd shallow2 &&
+ git fetch &&
+ git fsck &&
+ git log --format=%s origin/main >actual &&
+ test_write_lines 5 4 3 >expect &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'fetch --depth from shallow clone' '
+ (
+ cd shallow &&
+ commit 6
+ ) &&
+ (
+ cd shallow2 &&
+ git fetch --depth=2 &&
+ git fsck &&
+ git log --format=%s origin/main >actual &&
+ test_write_lines 6 5 >expect &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'fetch --unshallow from shallow clone' '
+ (
+ cd shallow2 &&
+ git fetch --unshallow &&
+ git fsck &&
+ git log --format=%s origin/main >actual &&
+ test_write_lines 6 5 4 3 >expect &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'fetch --unshallow from a full clone' '
+ git clone --no-local --depth=2 .git shallow3 &&
+ (
+ cd shallow3 &&
+ git log --format=%s >actual &&
+ test_write_lines 4 3 >expect &&
+ test_cmp expect actual &&
+ git -c fetch.writeCommitGraph fetch --unshallow &&
+ git log origin/main --format=%s >actual &&
+ test_write_lines 4 3 2 1 >expect &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'fetch something upstream has but hidden by clients shallow boundaries' '
+ # the blob "1" is available in .git but hidden by the
+ # shallow2/.git/shallow and it should be resent
+ ! git --git-dir=shallow2/.git cat-file blob $(echo 1|git hash-object --stdin) >/dev/null &&
+ echo 1 >1.t &&
+ git add 1.t &&
+ git commit -m add-1-back &&
+ (
+ cd shallow2 &&
+ git fetch ../.git +refs/heads/main:refs/remotes/top/main &&
+ git fsck &&
+ git log --format=%s top/main >actual &&
+ test_write_lines add-1-back 4 3 >expect &&
+ test_cmp expect actual
+ ) &&
+ git --git-dir=shallow2/.git cat-file blob $(echo 1|git hash-object --stdin) >/dev/null
+'
+
+test_expect_success 'fetch that requires changes in .git/shallow is filtered' '
+ (
+ cd shallow &&
+ git checkout --orphan no-shallow &&
+ commit no-shallow
+ ) &&
+ git init notshallow &&
+ (
+ cd notshallow &&
+ git fetch ../shallow/.git refs/heads/*:refs/remotes/shallow/* &&
+ git for-each-ref --format="%(refname)" >actual.refs &&
+ echo refs/remotes/shallow/no-shallow >expect.refs &&
+ test_cmp expect.refs actual.refs &&
+ git log --format=%s shallow/no-shallow >actual &&
+ echo no-shallow >expect &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'fetch --update-shallow' '
+ (
+ cd shallow &&
+ git checkout main &&
+ commit 7 &&
+ git tag -m foo heavy-tag HEAD^ &&
+ git tag light-tag HEAD^:tracked
+ ) &&
+ (
+ cd notshallow &&
+ git fetch --update-shallow ../shallow/.git refs/heads/*:refs/remotes/shallow/* &&
+ git fsck &&
+ git for-each-ref --sort=refname --format="%(refname)" >actual.refs &&
+ cat <<-\EOF >expect.refs &&
+ refs/remotes/shallow/main
+ refs/remotes/shallow/no-shallow
+ refs/tags/heavy-tag
+ refs/tags/light-tag
+ EOF
+ test_cmp expect.refs actual.refs &&
+ git log --format=%s shallow/main >actual &&
+ test_write_lines 7 6 5 4 3 >expect &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'fetch --update-shallow into a repo with submodules' '
+ test_config_global protocol.file.allow always &&
+
+ git init a-submodule &&
+ test_commit -C a-submodule foo &&
+
+ test_when_finished "rm -rf repo-with-sub" &&
+ git init repo-with-sub &&
+ git -C repo-with-sub submodule add ../a-submodule a-submodule &&
+ git -C repo-with-sub commit -m "added submodule" &&
+ git -C repo-with-sub fetch --update-shallow ../shallow/.git refs/heads/*:refs/remotes/shallow/*
+'
+
+test_expect_success 'fetch --update-shallow a commit that is also a shallow point into a repo with submodules' '
+ test_when_finished "rm -rf repo-with-sub" &&
+ git init repo-with-sub &&
+ git -c protocol.file.allow=always -C repo-with-sub \
+ submodule add ../a-submodule a-submodule &&
+ git -C repo-with-sub commit -m "added submodule" &&
+
+ SHALLOW=$(cat shallow/.git/shallow) &&
+ git -C repo-with-sub fetch --update-shallow ../shallow/.git "$SHALLOW":refs/heads/a-shallow
+'
+
+test_expect_success 'fetch --update-shallow (with fetch.writeCommitGraph)' '
+ (
+ cd shallow &&
+ git checkout main &&
+ commit 8 &&
+ git tag -m foo heavy-tag-for-graph HEAD^ &&
+ git tag light-tag-for-graph HEAD^:tracked
+ ) &&
+ test_config -C notshallow fetch.writeCommitGraph true &&
+ (
+ cd notshallow &&
+ git fetch --update-shallow ../shallow/.git refs/heads/*:refs/remotes/shallow/* &&
+ git fsck &&
+ git for-each-ref --sort=refname --format="%(refname)" >actual.refs &&
+ cat <<-EOF >expect.refs &&
+ refs/remotes/shallow/main
+ refs/remotes/shallow/no-shallow
+ refs/tags/heavy-tag
+ refs/tags/heavy-tag-for-graph
+ refs/tags/light-tag
+ refs/tags/light-tag-for-graph
+ EOF
+ test_cmp expect.refs actual.refs &&
+ git log --format=%s shallow/main >actual &&
+ test_write_lines 8 7 6 5 4 3 >expect &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success POSIXPERM,SANITY 'shallow fetch from a read-only repo' '
+ cp -R .git read-only.git &&
+ test_when_finished "find read-only.git -type d -print | xargs chmod +w" &&
+ find read-only.git -print | xargs chmod -w &&
+ git clone --no-local --depth=2 read-only.git from-read-only &&
+ git --git-dir=from-read-only/.git log --format=%s >actual &&
+ test_write_lines add-1-back 4 >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success '.git/shallow is edited by repack' '
+ git init shallow-server &&
+ test_commit -C shallow-server A &&
+ test_commit -C shallow-server B &&
+ git -C shallow-server checkout -b branch &&
+ test_commit -C shallow-server C &&
+ test_commit -C shallow-server E &&
+ test_commit -C shallow-server D &&
+ d="$(git -C shallow-server rev-parse --verify D^0)" &&
+ git -C shallow-server checkout main &&
+
+ git clone --depth=1 --no-tags --no-single-branch \
+ "file://$PWD/shallow-server" shallow-client &&
+
+ : now remove the branch and fetch with prune &&
+ git -C shallow-server branch -D branch &&
+ git -C shallow-client fetch --prune --depth=1 \
+ origin "+refs/heads/*:refs/remotes/origin/*" &&
+ git -C shallow-client repack -adfl &&
+ test_must_fail git -C shallow-client rev-parse --verify $d^0 &&
+ ! grep $d shallow-client/.git/shallow &&
+
+ git -C shallow-server branch branch-orig $d &&
+ git -C shallow-client fetch --prune --depth=2 \
+ origin "+refs/heads/*:refs/remotes/origin/*"
+'
+
+. "$TEST_DIRECTORY"/lib-httpd.sh
+start_httpd
+
+REPO="$HTTPD_DOCUMENT_ROOT_PATH/repo"
+
+test_expect_success 'shallow fetches check connectivity before writing shallow file' '
+ rm -rf "$REPO" client &&
+
+ git init "$REPO" &&
+ test_commit -C "$REPO" one &&
+ test_commit -C "$REPO" two &&
+ test_commit -C "$REPO" three &&
+
+ git init client &&
+
+ # Use protocol v2 to ensure that shallow information is sent exactly
+ # once by the server, since we are planning to manipulate it.
+ git -C "$REPO" config protocol.version 2 &&
+ git -C client config protocol.version 2 &&
+
+ git -C client fetch --depth=2 "$HTTPD_URL/one_time_perl/repo" main:a_branch &&
+
+ # Craft a situation in which the server sends back an unshallow request
+ # with an empty packfile. This is done by refetching with a shorter
+ # depth (to ensure that the packfile is empty), and overwriting the
+ # shallow line in the response with the unshallow line we want.
+ printf "$(test_oid perl)" \
+ "$(git -C "$REPO" rev-parse HEAD)" \
+ "$(git -C "$REPO" rev-parse HEAD^)" \
+ >"$HTTPD_ROOT_PATH/one-time-perl" &&
+ test_must_fail env GIT_TEST_SIDEBAND_ALL=0 git -C client \
+ fetch --depth=1 "$HTTPD_URL/one_time_perl/repo" \
+ main:a_branch &&
+
+ # Ensure that the one-time-perl script was used.
+ ! test -e "$HTTPD_ROOT_PATH/one-time-perl" &&
+
+ # Ensure that the resulting repo is consistent, despite our failure to
+ # fetch.
+ git -C client fsck
+'
+
+# DO NOT add non-httpd-specific tests here, because the last part of this
+# test script is only executed when httpd is available and enabled.
+
+test_done
diff --git a/t/t5538-push-shallow.sh b/t/t5538-push-shallow.sh
new file mode 100755
index 0000000..e91fcc1
--- /dev/null
+++ b/t/t5538-push-shallow.sh
@@ -0,0 +1,126 @@
+#!/bin/sh
+
+test_description='push from/to a shallow clone'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+commit() {
+ echo "$1" >tracked &&
+ git add tracked &&
+ git commit -m "$1"
+}
+
+test_expect_success 'setup' '
+ git config --global transfer.fsckObjects true &&
+ commit 1 &&
+ commit 2 &&
+ commit 3 &&
+ commit 4 &&
+ git clone . full &&
+ (
+ git init full-abc &&
+ cd full-abc &&
+ commit a &&
+ commit b &&
+ commit c
+ ) &&
+ git clone --no-local --depth=2 .git shallow &&
+ git --git-dir=shallow/.git log --format=%s >actual &&
+ cat <<EOF >expect &&
+4
+3
+EOF
+ test_cmp expect actual &&
+ git clone --no-local --depth=2 full-abc/.git shallow2 &&
+ git --git-dir=shallow2/.git log --format=%s >actual &&
+ cat <<EOF >expect &&
+c
+b
+EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'push from shallow clone' '
+ (
+ cd shallow &&
+ commit 5 &&
+ git push ../.git +main:refs/remotes/shallow/main
+ ) &&
+ git log --format=%s shallow/main >actual &&
+ git fsck &&
+ cat <<EOF >expect &&
+5
+4
+3
+2
+1
+EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'push from shallow clone, with grafted roots' '
+ (
+ cd shallow2 &&
+ test_must_fail git push ../.git +main:refs/remotes/shallow2/main 2>err &&
+ grep "shallow2/main.*shallow update not allowed" err
+ ) &&
+ test_must_fail git rev-parse shallow2/main &&
+ git fsck
+'
+
+test_expect_success 'add new shallow root with receive.updateshallow on' '
+ test_config receive.shallowupdate true &&
+ (
+ cd shallow2 &&
+ git push ../.git +main:refs/remotes/shallow2/main
+ ) &&
+ git log --format=%s shallow2/main >actual &&
+ git fsck &&
+ cat <<EOF >expect &&
+c
+b
+EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'push from shallow to shallow' '
+ (
+ cd shallow &&
+ git --git-dir=../shallow2/.git config receive.shallowupdate true &&
+ git push ../shallow2/.git +main:refs/remotes/shallow/main &&
+ git --git-dir=../shallow2/.git config receive.shallowupdate false
+ ) &&
+ (
+ cd shallow2 &&
+ git log --format=%s shallow/main >actual &&
+ git fsck &&
+ cat <<EOF >expect &&
+5
+4
+3
+EOF
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'push from full to shallow' '
+ ! git --git-dir=shallow2/.git cat-file blob $(echo 1|git hash-object --stdin) &&
+ commit 1 &&
+ git push shallow2/.git +main:refs/remotes/top/main &&
+ (
+ cd shallow2 &&
+ git log --format=%s top/main >actual &&
+ git fsck &&
+ cat <<EOF >expect &&
+1
+4
+3
+EOF
+ test_cmp expect actual &&
+ git cat-file blob $(echo 1|git hash-object --stdin) >/dev/null
+ )
+'
+test_done
diff --git a/t/t5539-fetch-http-shallow.sh b/t/t5539-fetch-http-shallow.sh
new file mode 100755
index 0000000..3ea75d3
--- /dev/null
+++ b/t/t5539-fetch-http-shallow.sh
@@ -0,0 +1,157 @@
+#!/bin/sh
+
+test_description='fetch/clone from a shallow clone over http'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-httpd.sh
+start_httpd
+
+commit() {
+ echo "$1" >tracked &&
+ git add tracked &&
+ test_tick &&
+ git commit -m "$1"
+}
+
+test_expect_success 'setup shallow clone' '
+ test_tick=1500000000 &&
+ commit 1 &&
+ commit 2 &&
+ commit 3 &&
+ commit 4 &&
+ commit 5 &&
+ commit 6 &&
+ commit 7 &&
+ git clone --no-local --depth=5 .git shallow &&
+ git config --global transfer.fsckObjects true
+'
+
+test_expect_success 'clone http repository' '
+ git clone --bare --no-local shallow "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
+ git clone $HTTPD_URL/smart/repo.git clone &&
+ (
+ cd clone &&
+ git fsck &&
+ git log --format=%s origin/main >actual &&
+ cat <<EOF >expect &&
+7
+6
+5
+4
+3
+EOF
+ test_cmp expect actual
+ )
+'
+
+# This test is tricky. We need large enough "have"s that fetch-pack
+# will put pkt-flush in between. Then we need a "have" the server
+# does not have, it'll send "ACK %s ready"
+test_expect_success 'no shallow lines after receiving ACK ready' '
+ (
+ cd shallow &&
+ for i in $(test_seq 15)
+ do
+ git checkout --orphan unrelated$i &&
+ test_commit unrelated$i &&
+ git push -q "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
+ refs/heads/unrelated$i:refs/heads/unrelated$i &&
+ git push -q ../clone/.git \
+ refs/heads/unrelated$i:refs/heads/unrelated$i ||
+ exit 1
+ done &&
+ git checkout main &&
+ test_commit new &&
+ git push "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" main
+ ) &&
+ (
+ cd clone &&
+ git checkout --orphan newnew &&
+ test_tick=1400000000 &&
+ test_commit new-too &&
+ # NEEDSWORK: If the overspecification of the expected result is reduced, we
+ # might be able to run this test in all protocol versions.
+ GIT_TRACE_PACKET="$TRASH_DIRECTORY/trace" GIT_TEST_PROTOCOL_VERSION=0 \
+ git fetch --depth=2 &&
+ grep "fetch-pack< ACK .* ready" ../trace &&
+ ! grep "fetch-pack> done" ../trace
+ )
+'
+
+test_expect_success 'clone shallow since ...' '
+ test_create_repo shallow-since &&
+ (
+ cd shallow-since &&
+ GIT_COMMITTER_DATE="100000000 +0700" git commit --allow-empty -m one &&
+ GIT_COMMITTER_DATE="200000000 +0700" git commit --allow-empty -m two &&
+ GIT_COMMITTER_DATE="300000000 +0700" git commit --allow-empty -m three &&
+ mv .git "$HTTPD_DOCUMENT_ROOT_PATH/shallow-since.git" &&
+ git clone --shallow-since "300000000 +0700" $HTTPD_URL/smart/shallow-since.git ../shallow11 &&
+ git -C ../shallow11 log --pretty=tformat:%s HEAD >actual &&
+ echo three >expected &&
+ test_cmp expected actual
+ )
+'
+
+test_expect_success 'fetch shallow since ...' '
+ git -C shallow11 fetch --shallow-since "200000000 +0700" origin &&
+ git -C shallow11 log --pretty=tformat:%s origin/main >actual &&
+ cat >expected <<-\EOF &&
+ three
+ two
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'shallow clone exclude tag two' '
+ test_create_repo shallow-exclude &&
+ (
+ cd shallow-exclude &&
+ test_commit one &&
+ test_commit two &&
+ test_commit three &&
+ mv .git "$HTTPD_DOCUMENT_ROOT_PATH/shallow-exclude.git" &&
+ git clone --shallow-exclude two $HTTPD_URL/smart/shallow-exclude.git ../shallow12 &&
+ git -C ../shallow12 log --pretty=tformat:%s HEAD >actual &&
+ echo three >expected &&
+ test_cmp expected actual
+ )
+'
+
+test_expect_success 'fetch exclude tag one' '
+ git -C shallow12 fetch --shallow-exclude one origin &&
+ git -C shallow12 log --pretty=tformat:%s origin/main >actual &&
+ test_write_lines three two >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'fetching deepen' '
+ test_create_repo shallow-deepen &&
+ (
+ cd shallow-deepen &&
+ test_commit one &&
+ test_commit two &&
+ test_commit three &&
+ mv .git "$HTTPD_DOCUMENT_ROOT_PATH/shallow-deepen.git" &&
+ git clone --depth 1 $HTTPD_URL/smart/shallow-deepen.git deepen &&
+ mv "$HTTPD_DOCUMENT_ROOT_PATH/shallow-deepen.git" .git &&
+ test_commit four &&
+ git -C deepen log --pretty=tformat:%s main >actual &&
+ echo three >expected &&
+ test_cmp expected actual &&
+ mv .git "$HTTPD_DOCUMENT_ROOT_PATH/shallow-deepen.git" &&
+ git -C deepen fetch --deepen=1 &&
+ git -C deepen log --pretty=tformat:%s origin/main >actual &&
+ cat >expected <<-\EOF &&
+ four
+ three
+ two
+ EOF
+ test_cmp expected actual
+ )
+'
+
+test_done
diff --git a/t/t5540-http-push-webdav.sh b/t/t5540-http-push-webdav.sh
new file mode 100755
index 0000000..37db3de
--- /dev/null
+++ b/t/t5540-http-push-webdav.sh
@@ -0,0 +1,204 @@
+#!/bin/sh
+#
+# Copyright (c) 2008 Clemens Buchacher <drizzd@aon.at>
+#
+
+test_description='test WebDAV http-push
+
+This test runs various sanity checks on http-push.'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+if git http-push > /dev/null 2>&1 || [ $? -eq 128 ]
+then
+ skip_all="skipping test, USE_CURL_MULTI is not defined"
+ test_done
+fi
+
+if test_have_prereq !REFFILES
+then
+ skip_all='skipping test; dumb HTTP protocol not supported with reftable.'
+ test_done
+fi
+
+LIB_HTTPD_DAV=t
+. "$TEST_DIRECTORY"/lib-httpd.sh
+ROOT_PATH="$PWD"
+start_httpd
+
+test_expect_success 'setup remote repository' '
+ cd "$ROOT_PATH" &&
+ mkdir test_repo &&
+ cd test_repo &&
+ git init &&
+ : >path1 &&
+ git add path1 &&
+ test_tick &&
+ git commit -m initial &&
+ cd - &&
+ git clone --bare test_repo test_repo.git &&
+ cd test_repo.git &&
+ git --bare update-server-info &&
+ test_hook --setup post-update <<-\EOF &&
+ exec git update-server-info
+ EOF
+ ORIG_HEAD=$(git rev-parse --verify HEAD) &&
+ cd - &&
+ mv test_repo.git "$HTTPD_DOCUMENT_ROOT_PATH"
+'
+
+test_expect_success 'create password-protected repository' '
+ mkdir -p "$HTTPD_DOCUMENT_ROOT_PATH/auth/dumb" &&
+ cp -Rf "$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git" \
+ "$HTTPD_DOCUMENT_ROOT_PATH/auth/dumb/test_repo.git"
+'
+
+setup_askpass_helper
+
+test_expect_success 'clone remote repository' '
+ cd "$ROOT_PATH" &&
+ git clone $HTTPD_URL/dumb/test_repo.git test_repo_clone
+'
+
+test_expect_success 'push to remote repository with packed refs' '
+ cd "$ROOT_PATH"/test_repo_clone &&
+ : >path2 &&
+ git add path2 &&
+ test_tick &&
+ git commit -m path2 &&
+ HEAD=$(git rev-parse --verify HEAD) &&
+ git push &&
+ (cd "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git &&
+ test $HEAD = $(git rev-parse --verify HEAD))
+'
+
+test_expect_success 'push already up-to-date' '
+ git push
+'
+
+test_expect_success 'push to remote repository with unpacked refs' '
+ (cd "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git &&
+ rm packed-refs &&
+ git update-ref refs/heads/main $ORIG_HEAD &&
+ git --bare update-server-info) &&
+ git push &&
+ (cd "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git &&
+ test $HEAD = $(git rev-parse --verify HEAD))
+'
+
+test_expect_success 'http-push fetches unpacked objects' '
+ cp -R "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git \
+ "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo_unpacked.git &&
+
+ git clone $HTTPD_URL/dumb/test_repo_unpacked.git \
+ "$ROOT_PATH"/fetch_unpacked &&
+
+ # By reset, we force git to retrieve the object
+ (cd "$ROOT_PATH"/fetch_unpacked &&
+ git reset --hard HEAD^ &&
+ git remote rm origin &&
+ git reflog expire --expire=0 --all &&
+ git prune &&
+ git push -f -v $HTTPD_URL/dumb/test_repo_unpacked.git main)
+'
+
+test_expect_success 'http-push fetches packed objects' '
+ cp -R "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git \
+ "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo_packed.git &&
+
+ git clone $HTTPD_URL/dumb/test_repo_packed.git \
+ "$ROOT_PATH"/test_repo_clone_packed &&
+
+ (cd "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo_packed.git &&
+ git --bare repack &&
+ git --bare prune-packed) &&
+
+ # By reset, we force git to retrieve the packed object
+ (cd "$ROOT_PATH"/test_repo_clone_packed &&
+ git reset --hard HEAD^ &&
+ git remote remove origin &&
+ git reflog expire --expire=0 --all &&
+ git prune &&
+ git push -f -v $HTTPD_URL/dumb/test_repo_packed.git main)
+'
+
+test_expect_success 'create and delete remote branch' '
+ cd "$ROOT_PATH"/test_repo_clone &&
+ git checkout -b dev &&
+ : >path3 &&
+ git add path3 &&
+ test_tick &&
+ git commit -m dev &&
+ git push origin dev &&
+ git push origin :dev &&
+ test_must_fail git show-ref --verify refs/remotes/origin/dev
+'
+
+test_expect_success 'non-force push fails if not up to date' '
+ git init --bare "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo_conflict.git &&
+ git -C "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo_conflict.git update-server-info &&
+ git clone $HTTPD_URL/dumb/test_repo_conflict.git "$ROOT_PATH"/c1 &&
+ git clone $HTTPD_URL/dumb/test_repo_conflict.git "$ROOT_PATH"/c2 &&
+ test_commit -C "$ROOT_PATH/c1" path1 &&
+ git -C "$ROOT_PATH/c1" push origin HEAD &&
+ git -C "$ROOT_PATH/c2" pull &&
+ test_commit -C "$ROOT_PATH/c1" path2 &&
+ git -C "$ROOT_PATH/c1" push origin HEAD &&
+ test_commit -C "$ROOT_PATH/c2" path3 &&
+ git -C "$ROOT_PATH/c1" log --graph --all &&
+ git -C "$ROOT_PATH/c2" log --graph --all &&
+ test_must_fail git -C "$ROOT_PATH/c2" push origin HEAD
+'
+
+test_expect_success 'MKCOL sends directory names with trailing slashes' '
+
+ ! grep "\"MKCOL.*[^/] HTTP/[^ ]*\"" < "$HTTPD_ROOT_PATH"/access.log
+
+'
+
+x1="[0-9a-f]"
+x2="$x1$x1"
+xtrunc=$(echo $OID_REGEX | sed -e "s/\[0-9a-f\]\[0-9a-f\]//")
+
+test_expect_success 'PUT and MOVE sends object to URLs with SHA-1 hash suffix' '
+ sed \
+ -e "s/PUT /OP /" \
+ -e "s/MOVE /OP /" \
+ -e "s|/objects/$x2/${xtrunc}_$OID_REGEX|WANTED_PATH_REQUEST|" \
+ "$HTTPD_ROOT_PATH"/access.log |
+ grep -e "\"OP .*WANTED_PATH_REQUEST HTTP/[.0-9]*\" 20[0-9] "
+
+'
+
+test_http_push_nonff "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git \
+ "$ROOT_PATH"/test_repo_clone main
+
+test_expect_success 'push to password-protected repository (user in URL)' '
+ test_commit pw-user &&
+ set_askpass user@host pass@host &&
+ git push "$HTTPD_URL_USER/auth/dumb/test_repo.git" HEAD &&
+ git rev-parse --verify HEAD >expect &&
+ git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/auth/dumb/test_repo.git" \
+ rev-parse --verify HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_failure 'user was prompted only once for password' '
+ expect_askpass pass user@host
+'
+
+test_expect_failure 'push to password-protected repository (no user in URL)' '
+ test_commit pw-nouser &&
+ set_askpass user@host pass@host &&
+ git push "$HTTPD_URL/auth/dumb/test_repo.git" HEAD &&
+ expect_askpass both user@host &&
+ git rev-parse --verify HEAD >expect &&
+ git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/auth/dumb/test_repo.git" \
+ rev-parse --verify HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t5541-http-push-smart.sh b/t/t5541-http-push-smart.sh
new file mode 100755
index 0000000..df758e1
--- /dev/null
+++ b/t/t5541-http-push-smart.sh
@@ -0,0 +1,514 @@
+#!/bin/sh
+#
+# Copyright (c) 2008 Clemens Buchacher <drizzd@aon.at>
+#
+
+test_description='test smart pushing over http via http-backend'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+ROOT_PATH="$PWD"
+. "$TEST_DIRECTORY"/lib-gpg.sh
+. "$TEST_DIRECTORY"/lib-httpd.sh
+. "$TEST_DIRECTORY"/lib-terminal.sh
+start_httpd
+
+test_expect_success 'setup remote repository' '
+ cd "$ROOT_PATH" &&
+ mkdir test_repo &&
+ cd test_repo &&
+ git init &&
+ : >path1 &&
+ git add path1 &&
+ test_tick &&
+ git commit -m initial &&
+ cd - &&
+ git clone --bare test_repo test_repo.git &&
+ cd test_repo.git &&
+ git config http.receivepack true &&
+ git config core.logallrefupdates true &&
+ ORIG_HEAD=$(git rev-parse --verify HEAD) &&
+ cd - &&
+ mv test_repo.git "$HTTPD_DOCUMENT_ROOT_PATH"
+'
+
+setup_askpass_helper
+
+test_expect_success 'clone remote repository' '
+ rm -rf test_repo_clone &&
+ git clone $HTTPD_URL/smart/test_repo.git test_repo_clone &&
+ (
+ cd test_repo_clone && git config push.default matching
+ )
+'
+
+test_expect_success 'push to remote repository (standard)' '
+ # Clear the log, so that the "used receive-pack service" test below
+ # sees just what we did here.
+ >"$HTTPD_ROOT_PATH"/access.log &&
+
+ cd "$ROOT_PATH"/test_repo_clone &&
+ : >path2 &&
+ git add path2 &&
+ test_tick &&
+ git commit -m path2 &&
+ HEAD=$(git rev-parse --verify HEAD) &&
+ GIT_TRACE_CURL=true git push -v -v 2>err &&
+ ! grep "Expect: 100-continue" err &&
+ grep "POST git-receive-pack ([0-9]* bytes)" err &&
+ (cd "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git &&
+ test $HEAD = $(git rev-parse --verify HEAD))
+'
+
+test_expect_success 'used receive-pack service' '
+ cat >exp <<-\EOF &&
+ GET /smart/test_repo.git/info/refs?service=git-receive-pack HTTP/1.1 200
+ POST /smart/test_repo.git/git-receive-pack HTTP/1.1 200
+ EOF
+
+ check_access_log exp
+'
+
+test_expect_success 'push to remote repository (standard) with sending Accept-Language' '
+ cat >exp <<-\EOF &&
+ => Send header: Accept-Language: ko-KR, *;q=0.9
+ => Send header: Accept-Language: ko-KR, *;q=0.9
+ EOF
+
+ cd "$ROOT_PATH"/test_repo_clone &&
+ : >path_lang &&
+ git add path_lang &&
+ test_tick &&
+ git commit -m path_lang &&
+ HEAD=$(git rev-parse --verify HEAD) &&
+ GIT_TRACE_CURL=true LANGUAGE="ko_KR.UTF-8" git push -v -v 2>err &&
+ ! grep "Expect: 100-continue" err &&
+
+ grep "=> Send header: Accept-Language:" err >err.language &&
+ test_cmp exp err.language
+'
+
+test_expect_success 'push already up-to-date' '
+ git push
+'
+
+test_expect_success 'create and delete remote branch' '
+ cd "$ROOT_PATH"/test_repo_clone &&
+ git checkout -b dev &&
+ : >path3 &&
+ git add path3 &&
+ test_tick &&
+ git commit -m dev &&
+ git push origin dev &&
+ git push origin :dev &&
+ test_must_fail git show-ref --verify refs/remotes/origin/dev
+'
+
+test_expect_success 'setup rejected update hook' '
+ test_hook --setup -C "$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git" update <<-\EOF &&
+ exit 1
+ EOF
+
+ cat >exp <<-EOF
+ remote: error: hook declined to update refs/heads/dev2
+ To http://127.0.0.1:$LIB_HTTPD_PORT/smart/test_repo.git
+ ! [remote rejected] dev2 -> dev2 (hook declined)
+ error: failed to push some refs to '\''http://127.0.0.1:$LIB_HTTPD_PORT/smart/test_repo.git'\''
+ EOF
+'
+
+test_expect_success 'rejected update prints status' '
+ cd "$ROOT_PATH"/test_repo_clone &&
+ git checkout -b dev2 &&
+ : >path4 &&
+ git add path4 &&
+ test_tick &&
+ git commit -m dev2 &&
+ test_must_fail git push origin dev2 2>act &&
+ sed -e "/^remote: /s/ *$//" <act >cmp &&
+ test_cmp exp cmp
+'
+rm -f "$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git/hooks/update"
+
+test_http_push_nonff "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git \
+ "$ROOT_PATH"/test_repo_clone main success
+
+test_expect_success 'push fails for non-fast-forward refs unmatched by remote helper' '
+ # create a dissimilarly-named remote ref so that git is unable to match the
+ # two refs (viz. local, remote) unless an explicit refspec is provided.
+ git push origin main:niam &&
+
+ echo "change changed" > path2 &&
+ git commit -a -m path2 --amend &&
+
+ # push main too; this ensures there is at least one '"'push'"' command to
+ # the remote helper and triggers interaction with the helper.
+ test_must_fail git push -v origin +main main:niam >output 2>&1'
+
+test_expect_success 'push fails for non-fast-forward refs unmatched by remote helper: remote output' '
+ grep "^ + [a-f0-9]*\.\.\.[a-f0-9]* *main -> main (forced update)$" output &&
+ grep "^ ! \[rejected\] *main -> niam (non-fast-forward)$" output
+'
+
+test_expect_success 'push fails for non-fast-forward refs unmatched by remote helper: our output' '
+ test_grep "Updates were rejected because" \
+ output
+'
+
+test_expect_success 'push (chunked)' '
+ git checkout main &&
+ test_commit commit path3 &&
+ HEAD=$(git rev-parse --verify HEAD) &&
+ test_config http.postbuffer 4 &&
+ git push -v -v origin $BRANCH 2>err &&
+ grep "POST git-receive-pack (chunked)" err &&
+ (cd "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git &&
+ test $HEAD = $(git rev-parse --verify HEAD))
+'
+
+## References of remote: atomic1(1) main(2) collateral(2) other(2)
+## References of local : atomic2(2) main(1) collateral(3) other(2) collateral1(3) atomic(1)
+## Atomic push : main(1) collateral(3) atomic(1)
+test_expect_success 'push --atomic also prevents branch creation, reports collateral' '
+ # Setup upstream repo - empty for now
+ d=$HTTPD_DOCUMENT_ROOT_PATH/atomic-branches.git &&
+ git init --bare "$d" &&
+ test_config -C "$d" http.receivepack true &&
+ up="$HTTPD_URL"/smart/atomic-branches.git &&
+
+ # Tell "$up" about three branches for now
+ test_commit atomic1 &&
+ test_commit atomic2 &&
+ git branch collateral &&
+ git branch other &&
+ git push "$up" atomic1 main collateral other &&
+ git tag -d atomic1 &&
+
+ # collateral is a valid push, but should be failed by atomic push
+ git checkout collateral &&
+ test_commit collateral1 &&
+
+ # Make main incompatible with upstream to provoke atomic
+ git checkout main &&
+ git reset --hard HEAD^ &&
+
+ # Add a new branch which should be failed by atomic push. This is a
+ # regression case.
+ git branch atomic &&
+
+ # --atomic should cause entire push to be rejected
+ test_must_fail git push --atomic "$up" main atomic collateral 2>output &&
+
+ # the new branch should not have been created upstream
+ test_must_fail git -C "$d" show-ref --verify refs/heads/atomic &&
+
+ # upstream should still reflect atomic2, the last thing we pushed
+ # successfully
+ git rev-parse atomic2 >expected &&
+ # on main...
+ git -C "$d" rev-parse refs/heads/main >actual &&
+ test_cmp expected actual &&
+ # ...and collateral.
+ git -C "$d" rev-parse refs/heads/collateral >actual &&
+ test_cmp expected actual &&
+
+ # the failed refs should be indicated to the user
+ grep "^ ! .*rejected.* main -> main" output &&
+
+ # the collateral failure refs should be indicated to the user
+ grep "^ ! .*rejected.* atomic -> atomic .*atomic push failed" output &&
+ grep "^ ! .*rejected.* collateral -> collateral .*atomic push failed" output &&
+
+ # never report what we do not push
+ ! grep "^ ! .*rejected.* atomic1 " output &&
+ ! grep "^ ! .*rejected.* other " output
+'
+
+test_expect_success 'push --atomic fails on server-side errors' '
+ # Use previously set up repository
+ d=$HTTPD_DOCUMENT_ROOT_PATH/atomic-branches.git &&
+ test_config -C "$d" http.receivepack true &&
+ up="$HTTPD_URL"/smart/atomic-branches.git &&
+
+ # break ref updates for other on the remote site
+ mkdir "$d/refs/heads/other.lock" &&
+
+ # add the new commit to other
+ git branch -f other collateral &&
+
+ # --atomic should cause entire push to be rejected
+ test_must_fail git push --atomic "$up" atomic other 2>output &&
+
+ # the new branch should not have been created upstream
+ test_must_fail git -C "$d" show-ref --verify refs/heads/atomic &&
+
+ # upstream should still reflect atomic2, the last thing we pushed
+ # successfully
+ git rev-parse atomic2 >expected &&
+ # ...to other.
+ git -C "$d" rev-parse refs/heads/other >actual &&
+ test_cmp expected actual &&
+
+ # the new branch should not have been created upstream
+ test_must_fail git -C "$d" show-ref --verify refs/heads/atomic &&
+
+ # the failed refs should be indicated to the user
+ grep "^ ! .*rejected.* other -> other .*atomic transaction failed" output &&
+
+ # the collateral failure refs should be indicated to the user
+ grep "^ ! .*rejected.* atomic -> atomic .*atomic transaction failed" output
+'
+
+test_expect_success 'push --all can push to empty repo' '
+ d=$HTTPD_DOCUMENT_ROOT_PATH/empty-all.git &&
+ git init --bare "$d" &&
+ git --git-dir="$d" config http.receivepack true &&
+ git push --all "$HTTPD_URL"/smart/empty-all.git
+'
+
+test_expect_success 'push --mirror can push to empty repo' '
+ d=$HTTPD_DOCUMENT_ROOT_PATH/empty-mirror.git &&
+ git init --bare "$d" &&
+ git --git-dir="$d" config http.receivepack true &&
+ git push --mirror "$HTTPD_URL"/smart/empty-mirror.git
+'
+
+test_expect_success 'push --all to repo with alternates' '
+ s=$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git &&
+ d=$HTTPD_DOCUMENT_ROOT_PATH/alternates-all.git &&
+ git clone --bare --shared "$s" "$d" &&
+ git --git-dir="$d" config http.receivepack true &&
+ git --git-dir="$d" repack -adl &&
+ git push --all "$HTTPD_URL"/smart/alternates-all.git
+'
+
+test_expect_success 'push --mirror to repo with alternates' '
+ s=$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git &&
+ d=$HTTPD_DOCUMENT_ROOT_PATH/alternates-mirror.git &&
+ git clone --bare --shared "$s" "$d" &&
+ git --git-dir="$d" config http.receivepack true &&
+ git --git-dir="$d" repack -adl &&
+ git push --mirror "$HTTPD_URL"/smart/alternates-mirror.git
+'
+
+test_expect_success TTY 'push shows progress when stderr is a tty' '
+ cd "$ROOT_PATH"/test_repo_clone &&
+ test_commit noisy &&
+ test_terminal git push >output 2>&1 &&
+ test_grep "^Writing objects" output
+'
+
+test_expect_success TTY 'push --quiet silences status and progress' '
+ cd "$ROOT_PATH"/test_repo_clone &&
+ test_commit quiet &&
+ test_terminal git push --quiet >output 2>&1 &&
+ test_must_be_empty output
+'
+
+test_expect_success TTY 'push --no-progress silences progress but not status' '
+ cd "$ROOT_PATH"/test_repo_clone &&
+ test_commit no-progress &&
+ test_terminal git push --no-progress >output 2>&1 &&
+ test_grep "^To http" output &&
+ test_grep ! "^Writing objects" output
+'
+
+test_expect_success 'push --progress shows progress to non-tty' '
+ cd "$ROOT_PATH"/test_repo_clone &&
+ test_commit progress &&
+ git push --progress >output 2>&1 &&
+ test_grep "^To http" output &&
+ test_grep "^Writing objects" output
+'
+
+test_expect_success 'http push gives sane defaults to reflog' '
+ cd "$ROOT_PATH"/test_repo_clone &&
+ test_commit reflog-test &&
+ git push "$HTTPD_URL"/smart/test_repo.git &&
+ git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git" \
+ log -g -1 --format="%gn <%ge>" >actual &&
+ echo "anonymous <anonymous@http.127.0.0.1>" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'http push respects GIT_COMMITTER_* in reflog' '
+ cd "$ROOT_PATH"/test_repo_clone &&
+ test_commit custom-reflog-test &&
+ git push "$HTTPD_URL"/smart_custom_env/test_repo.git &&
+ git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git" \
+ log -g -1 --format="%gn <%ge>" >actual &&
+ echo "Custom User <custom@example.com>" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'push over smart http with auth' '
+ cd "$ROOT_PATH/test_repo_clone" &&
+ echo push-auth-test >expect &&
+ test_commit push-auth-test &&
+ set_askpass user@host pass@host &&
+ git push "$HTTPD_URL"/auth/smart/test_repo.git &&
+ git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git" \
+ log -1 --format=%s >actual &&
+ expect_askpass both user@host &&
+ test_cmp expect actual
+'
+
+test_expect_success 'push to auth-only-for-push repo' '
+ cd "$ROOT_PATH/test_repo_clone" &&
+ echo push-half-auth >expect &&
+ test_commit push-half-auth &&
+ set_askpass user@host pass@host &&
+ git push "$HTTPD_URL"/auth-push/smart/test_repo.git &&
+ git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/test_repo.git" \
+ log -1 --format=%s >actual &&
+ expect_askpass both user@host &&
+ test_cmp expect actual
+'
+
+test_expect_success 'create repo without http.receivepack set' '
+ cd "$ROOT_PATH" &&
+ git init half-auth &&
+ (
+ cd half-auth &&
+ test_commit one
+ ) &&
+ git clone --bare half-auth "$HTTPD_DOCUMENT_ROOT_PATH/half-auth.git"
+'
+
+test_expect_success 'clone via half-auth-complete does not need password' '
+ cd "$ROOT_PATH" &&
+ set_askpass wrong &&
+ git clone "$HTTPD_URL"/half-auth-complete/smart/half-auth.git \
+ half-auth-clone &&
+ expect_askpass none
+'
+
+test_expect_success 'push into half-auth-complete requires password' '
+ cd "$ROOT_PATH/half-auth-clone" &&
+ echo two >expect &&
+ test_commit two &&
+ set_askpass user@host pass@host &&
+ git push "$HTTPD_URL/half-auth-complete/smart/half-auth.git" &&
+ git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/half-auth.git" \
+ log -1 --format=%s >actual &&
+ expect_askpass both user@host &&
+ test_cmp expect actual
+'
+
+test_expect_success CMDLINE_LIMIT 'push 2000 tags over http' '
+ sha1=$(git rev-parse HEAD) &&
+ test_seq 2000 |
+ sort |
+ sed "s|.*|$sha1 refs/tags/really-long-tag-name-&|" \
+ >.git/packed-refs &&
+ run_with_limited_cmdline git push --mirror
+'
+
+test_expect_success GPG 'push with post-receive to inspect certificate' '
+ test_hook -C "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git post-receive <<-\EOF &&
+ # discard the update list
+ cat >/dev/null
+ # record the push certificate
+ if test -n "${GIT_PUSH_CERT-}"
+ then
+ git cat-file blob $GIT_PUSH_CERT >../push-cert
+ fi &&
+ cat >../push-cert-status <<E_O_F
+ SIGNER=${GIT_PUSH_CERT_SIGNER-nobody}
+ KEY=${GIT_PUSH_CERT_KEY-nokey}
+ STATUS=${GIT_PUSH_CERT_STATUS-nostatus}
+ NONCE_STATUS=${GIT_PUSH_CERT_NONCE_STATUS-nononcestatus}
+ NONCE=${GIT_PUSH_CERT_NONCE-nononce}
+ E_O_F
+ EOF
+ (
+ cd "$HTTPD_DOCUMENT_ROOT_PATH"/test_repo.git &&
+ git config receive.certnonceseed sekrit &&
+ git config receive.certnonceslop 30
+ ) &&
+ cd "$ROOT_PATH/test_repo_clone" &&
+ test_commit cert-test &&
+ git push --signed "$HTTPD_URL/smart/test_repo.git" &&
+ (
+ cd "$HTTPD_DOCUMENT_ROOT_PATH" &&
+ cat <<-\EOF &&
+ SIGNER=C O Mitter <committer@example.com>
+ KEY=13B6F51ECDDE430D
+ STATUS=G
+ NONCE_STATUS=OK
+ EOF
+ sed -n -e "s/^nonce /NONCE=/p" -e "/^$/q" push-cert
+ ) >expect &&
+ test_cmp expect "$HTTPD_DOCUMENT_ROOT_PATH/push-cert-status"
+'
+
+test_expect_success 'push status output scrubs password' '
+ cd "$ROOT_PATH/test_repo_clone" &&
+ git push --porcelain \
+ "$HTTPD_URL_USER_PASS/smart/test_repo.git" \
+ +HEAD:scrub >status &&
+ # should have been scrubbed down to vanilla URL
+ grep "^To $HTTPD_URL/smart/test_repo.git" status
+'
+
+test_expect_success 'clone/fetch scrubs password from reflogs' '
+ cd "$ROOT_PATH" &&
+ git clone "$HTTPD_URL_USER_PASS/smart/test_repo.git" \
+ reflog-test &&
+ cd reflog-test &&
+ test_commit prepare-for-force-fetch &&
+ git switch -c away &&
+ git fetch "$HTTPD_URL_USER_PASS/smart/test_repo.git" \
+ +main:main &&
+ # should have been scrubbed down to vanilla URL
+ git log -g main >reflog &&
+ grep "$HTTPD_URL" reflog &&
+ ! grep "$HTTPD_URL_USER_PASS" reflog
+'
+
+test_expect_success 'Non-ASCII branch name can be used with --force-with-lease' '
+ cd "$ROOT_PATH" &&
+ git clone "$HTTPD_URL_USER_PASS/smart/test_repo.git" non-ascii &&
+ cd non-ascii &&
+ git checkout -b rama-de-árbol &&
+ test_commit F &&
+ git push --force-with-lease origin rama-de-árbol &&
+ git ls-remote origin refs/heads/rama-de-árbol >actual &&
+ git ls-remote . refs/heads/rama-de-árbol >expect &&
+ test_cmp expect actual &&
+ git push --delete --force-with-lease origin rama-de-árbol &&
+ git ls-remote origin refs/heads/rama-de-árbol >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'colorize errors/hints' '
+ cd "$ROOT_PATH"/test_repo_clone &&
+ test_must_fail git -c color.transport=always -c color.advice=always \
+ -c color.push=always \
+ push origin origin/main^:main 2>act &&
+ test_decode_color <act >decoded &&
+ test_grep "<RED>.*rejected.*<RESET>" decoded &&
+ test_grep "<RED>error: failed to push some refs" decoded &&
+ test_grep "<YELLOW>hint: " decoded &&
+ test_grep ! "^hint: " decoded
+'
+
+test_expect_success 'report error server does not provide ref status' '
+ git init "$HTTPD_DOCUMENT_ROOT_PATH/no_report" &&
+ git -C "$HTTPD_DOCUMENT_ROOT_PATH/no_report" config http.receivepack true &&
+ test_must_fail git push --porcelain \
+ $HTTPD_URL_USER_PASS/smart/no_report \
+ HEAD:refs/tags/will-fail >actual &&
+ test_must_fail git -C "$HTTPD_DOCUMENT_ROOT_PATH/no_report" \
+ rev-parse --verify refs/tags/will-fail &&
+ cat >expect <<-EOF &&
+ To $HTTPD_URL/smart/no_report
+ ! HEAD:refs/tags/will-fail [remote failure] (remote failed to report status)
+ Done
+ EOF
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t5542-push-http-shallow.sh b/t/t5542-push-http-shallow.sh
new file mode 100755
index 0000000..c2cc831
--- /dev/null
+++ b/t/t5542-push-http-shallow.sh
@@ -0,0 +1,96 @@
+#!/bin/sh
+
+test_description='push from/to a shallow clone over http'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-httpd.sh
+start_httpd
+
+commit() {
+ echo "$1" >tracked &&
+ git add tracked &&
+ git commit -m "$1"
+}
+
+test_expect_success 'setup' '
+ git config --global transfer.fsckObjects true &&
+ commit 1 &&
+ commit 2 &&
+ commit 3 &&
+ commit 4 &&
+ git clone . full &&
+ (
+ git init full-abc &&
+ cd full-abc &&
+ commit a &&
+ commit b &&
+ commit c
+ ) &&
+ git clone --no-local --depth=2 .git shallow &&
+ git --git-dir=shallow/.git log --format=%s >actual &&
+ cat <<EOF >expect &&
+4
+3
+EOF
+ test_cmp expect actual &&
+ git clone --no-local --depth=2 full-abc/.git shallow2 &&
+ git --git-dir=shallow2/.git log --format=%s >actual &&
+ cat <<EOF >expect &&
+c
+b
+EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'push to shallow repo via http' '
+ git clone --bare --no-local shallow "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
+ (
+ cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
+ git config http.receivepack true
+ ) &&
+ (
+ cd full &&
+ commit 9 &&
+ git push $HTTPD_URL/smart/repo.git +main:refs/remotes/top/main
+ ) &&
+ (
+ cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
+ git fsck &&
+ git log --format=%s top/main >actual &&
+ cat <<EOF >expect &&
+9
+4
+3
+EOF
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'push from shallow repo via http' '
+ mv "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" shallow-upstream.git &&
+ git clone --bare --no-local full "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
+ (
+ cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
+ git config http.receivepack true
+ ) &&
+ commit 10 &&
+ git push $HTTPD_URL/smart/repo.git +main:refs/remotes/top/main &&
+ (
+ cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
+ git fsck &&
+ git log --format=%s top/main >actual &&
+ cat <<EOF >expect &&
+10
+4
+3
+2
+1
+EOF
+ test_cmp expect actual
+ )
+'
+
+test_done
diff --git a/t/t5543-atomic-push.sh b/t/t5543-atomic-push.sh
new file mode 100755
index 0000000..04b47ad
--- /dev/null
+++ b/t/t5543-atomic-push.sh
@@ -0,0 +1,283 @@
+#!/bin/sh
+
+test_description='pushing to a repository using the atomic push option'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+mk_repo_pair () {
+ rm -rf workbench upstream &&
+ test_create_repo upstream &&
+ test_create_repo workbench &&
+ (
+ cd upstream &&
+ git config receive.denyCurrentBranch warn
+ ) &&
+ (
+ cd workbench &&
+ git remote add up ../upstream
+ )
+}
+
+# Compare the ref ($1) in upstream with a ref value from workbench ($2)
+# i.e. test_refs second HEAD@{2}
+test_refs () {
+ test $# = 2 &&
+ git -C upstream rev-parse --verify "$1" >expect &&
+ git -C workbench rev-parse --verify "$2" >actual &&
+ test_cmp expect actual
+}
+
+fmt_status_report () {
+ sed -n \
+ -e "/^To / { s/ */ /g; p; }" \
+ -e "/^ ! / { s/ */ /g; p; }"
+}
+
+test_expect_success 'atomic push works for a single branch' '
+ mk_repo_pair &&
+ (
+ cd workbench &&
+ test_commit one &&
+ git push --mirror up &&
+ test_commit two &&
+ git push --atomic up main
+ ) &&
+ test_refs main main
+'
+
+test_expect_success 'atomic push works for two branches' '
+ mk_repo_pair &&
+ (
+ cd workbench &&
+ test_commit one &&
+ git branch second &&
+ git push --mirror up &&
+ test_commit two &&
+ git checkout second &&
+ test_commit three &&
+ git push --atomic up main second
+ ) &&
+ test_refs main main &&
+ test_refs second second
+'
+
+test_expect_success 'atomic push works in combination with --mirror' '
+ mk_repo_pair &&
+ (
+ cd workbench &&
+ test_commit one &&
+ git checkout -b second &&
+ test_commit two &&
+ git push --atomic --mirror up
+ ) &&
+ test_refs main main &&
+ test_refs second second
+'
+
+test_expect_success 'atomic push works in combination with --force' '
+ mk_repo_pair &&
+ (
+ cd workbench &&
+ test_commit one &&
+ git branch second main &&
+ test_commit two_a &&
+ git checkout second &&
+ test_commit two_b &&
+ test_commit three_b &&
+ test_commit four &&
+ git push --mirror up &&
+ # The actual test is below
+ git checkout main &&
+ test_commit three_a &&
+ git checkout second &&
+ git reset --hard HEAD^ &&
+ git push --force --atomic up main second
+ ) &&
+ test_refs main main &&
+ test_refs second second
+'
+
+# set up two branches where main can be pushed but second can not
+# (non-fast-forward). Since second can not be pushed the whole operation
+# will fail and leave main untouched.
+test_expect_success 'atomic push fails if one branch fails' '
+ mk_repo_pair &&
+ (
+ cd workbench &&
+ test_commit one &&
+ git checkout -b second main &&
+ test_commit two &&
+ test_commit three &&
+ test_commit four &&
+ git push --mirror up &&
+ git reset --hard HEAD~2 &&
+ test_commit five &&
+ git checkout main &&
+ test_commit six &&
+ test_must_fail git push --atomic --all up >output-all 2>&1 &&
+ # --all and --branches have the same behavior when be combined with --atomic
+ test_must_fail git push --atomic --branches up >output-branches 2>&1 &&
+ test_cmp output-all output-branches
+ ) &&
+ test_refs main HEAD@{7} &&
+ test_refs second HEAD@{4}
+'
+
+test_expect_success 'atomic push fails if one tag fails remotely' '
+ # prepare the repo
+ mk_repo_pair &&
+ (
+ cd workbench &&
+ test_commit one &&
+ git checkout -b second main &&
+ test_commit two &&
+ git push --mirror up
+ ) &&
+ # a third party modifies the server side:
+ (
+ cd upstream &&
+ git checkout second &&
+ git tag test_tag second
+ ) &&
+ # see if we can now push both branches.
+ (
+ cd workbench &&
+ git checkout main &&
+ test_commit three &&
+ git checkout second &&
+ test_commit four &&
+ git tag test_tag &&
+ test_must_fail git push --tags --atomic up main second
+ ) &&
+ test_refs main HEAD@{3} &&
+ test_refs second HEAD@{1}
+'
+
+test_expect_success 'atomic push obeys update hook preventing a branch to be pushed' '
+ mk_repo_pair &&
+ (
+ cd workbench &&
+ test_commit one &&
+ git checkout -b second main &&
+ test_commit two &&
+ git push --mirror up
+ ) &&
+ test_hook -C upstream update <<-\EOF &&
+ # only allow update to main from now on
+ test "$1" = "refs/heads/main"
+ EOF
+ (
+ cd workbench &&
+ git checkout main &&
+ test_commit three &&
+ git checkout second &&
+ test_commit four &&
+ test_must_fail git push --atomic up main second
+ ) &&
+ test_refs main HEAD@{3} &&
+ test_refs second HEAD@{1}
+'
+
+test_expect_success 'atomic push is not advertised if configured' '
+ mk_repo_pair &&
+ (
+ cd upstream &&
+ git config receive.advertiseatomic 0
+ ) &&
+ (
+ cd workbench &&
+ test_commit one &&
+ git push --mirror up &&
+ test_commit two &&
+ test_must_fail git push --atomic up main
+ ) &&
+ test_refs main HEAD@{1}
+'
+
+# References in upstream : main(1) one(1) foo(1)
+# References in workbench: main(2) foo(1) two(2) bar(2)
+# Atomic push : main(2) two(2) bar(2)
+test_expect_success 'atomic push reports (reject by update hook)' '
+ mk_repo_pair &&
+ (
+ cd workbench &&
+ test_commit one &&
+ git branch foo &&
+ git push up main one foo &&
+ git tag -d one
+ ) &&
+ (
+ mkdir -p upstream/.git/hooks &&
+ cat >upstream/.git/hooks/update <<-EOF &&
+ #!/bin/sh
+
+ if test "\$1" = "refs/heads/bar"
+ then
+ echo >&2 "Pusing to branch bar is prohibited"
+ exit 1
+ fi
+ EOF
+ chmod a+x upstream/.git/hooks/update
+ ) &&
+ (
+ cd workbench &&
+ test_commit two &&
+ git branch bar
+ ) &&
+ test_must_fail git -C workbench \
+ push --atomic up main two bar >out 2>&1 &&
+ fmt_status_report <out >actual &&
+ cat >expect <<-EOF &&
+ To ../upstream
+ ! [remote rejected] main -> main (atomic push failure)
+ ! [remote rejected] two -> two (atomic push failure)
+ ! [remote rejected] bar -> bar (hook declined)
+ EOF
+ test_cmp expect actual
+'
+
+# References in upstream : main(1) one(1) foo(1)
+# References in workbench: main(2) foo(1) two(2) bar(2)
+test_expect_success 'atomic push reports (mirror, but reject by update hook)' '
+ (
+ cd workbench &&
+ git remote remove up &&
+ git remote add up ../upstream
+ ) &&
+ test_must_fail git -C workbench \
+ push --atomic --mirror up >out 2>&1 &&
+ fmt_status_report <out >actual &&
+ cat >expect <<-EOF &&
+ To ../upstream
+ ! [remote rejected] main -> main (atomic push failure)
+ ! [remote rejected] one (atomic push failure)
+ ! [remote rejected] bar -> bar (hook declined)
+ ! [remote rejected] two -> two (atomic push failure)
+ EOF
+ test_cmp expect actual
+'
+
+# References in upstream : main(2) one(1) foo(1)
+# References in workbench: main(1) foo(1) two(2) bar(2)
+test_expect_success 'atomic push reports (reject by non-ff)' '
+ rm upstream/.git/hooks/update &&
+ (
+ cd workbench &&
+ git push up main &&
+ git reset --hard HEAD^
+ ) &&
+ test_must_fail git -C workbench \
+ push --atomic up main foo bar >out 2>&1 &&
+ fmt_status_report <out >actual &&
+ cat >expect <<-EOF &&
+ To ../upstream
+ ! [rejected] main -> main (non-fast-forward)
+ ! [rejected] bar -> bar (atomic push failed)
+ EOF
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t5544-pack-objects-hook.sh b/t/t5544-pack-objects-hook.sh
new file mode 100755
index 0000000..1a9e14b
--- /dev/null
+++ b/t/t5544-pack-objects-hook.sh
@@ -0,0 +1,79 @@
+#!/bin/sh
+
+test_description='test custom script in place of pack-objects'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'create some history to fetch' '
+ test_commit one &&
+ test_commit two
+'
+
+test_expect_success 'create debugging hook script' '
+ write_script .git/hook <<-\EOF
+ echo >&2 "hook running"
+ echo "$*" >hook.args
+ cat >hook.stdin
+ "$@" <hook.stdin >hook.stdout
+ cat hook.stdout
+ EOF
+'
+
+clear_hook_results () {
+ rm -rf .git/hook.* dst.git
+}
+
+test_expect_success 'hook runs via global config' '
+ clear_hook_results &&
+ test_config_global uploadpack.packObjectsHook ./hook &&
+ git clone --no-local . dst.git 2>stderr &&
+ grep "hook running" stderr
+'
+
+test_expect_success 'hook outputs are sane' '
+ # check that we recorded a usable pack
+ git index-pack --stdin <.git/hook.stdout &&
+
+ # check that we recorded args and stdin. We do not check
+ # the full argument list or the exact pack contents, as it would make
+ # the test brittle. So just sanity check that we could replay
+ # the packing procedure.
+ grep "^git" .git/hook.args &&
+ $(cat .git/hook.args) <.git/hook.stdin >replay
+'
+
+test_expect_success 'hook runs from -c config' '
+ clear_hook_results &&
+ git clone --no-local \
+ -u "git -c uploadpack.packObjectsHook=./hook upload-pack" \
+ . dst.git 2>stderr &&
+ grep "hook running" stderr
+'
+
+test_expect_success 'hook does not run from repo config' '
+ clear_hook_results &&
+ test_config uploadpack.packObjectsHook "./hook" &&
+ git clone --no-local . dst.git 2>stderr &&
+ ! grep "hook running" stderr &&
+ test_path_is_missing .git/hook.args &&
+ test_path_is_missing .git/hook.stdin &&
+ test_path_is_missing .git/hook.stdout &&
+
+ # check that global config is used instead
+ test_config_global uploadpack.packObjectsHook ./hook &&
+ git clone --no-local . dst2.git 2>stderr &&
+ grep "hook running" stderr
+'
+
+test_expect_success 'hook works with partial clone' '
+ clear_hook_results &&
+ test_config_global uploadpack.packObjectsHook ./hook &&
+ test_config_global uploadpack.allowFilter true &&
+ git clone --bare --no-local --filter=blob:none . dst.git &&
+ git -C dst.git rev-list --objects --missing=allow-any --no-object-names --all >objects &&
+ git -C dst.git cat-file --batch-check="%(objecttype)" <objects >types &&
+ ! grep blob types
+'
+
+test_done
diff --git a/t/t5545-push-options.sh b/t/t5545-push-options.sh
new file mode 100755
index 0000000..fb13549
--- /dev/null
+++ b/t/t5545-push-options.sh
@@ -0,0 +1,291 @@
+#!/bin/sh
+
+test_description='pushing to a repository using push options'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB=1
+export GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB
+
+. ./test-lib.sh
+
+mk_repo_pair () {
+ rm -rf workbench upstream &&
+ test_create_repo upstream &&
+ test_create_repo workbench &&
+ (
+ cd upstream &&
+ git config receive.denyCurrentBranch warn &&
+ mkdir -p .git/hooks &&
+ cat >.git/hooks/pre-receive <<-'EOF' &&
+ #!/bin/sh
+ if test -n "$GIT_PUSH_OPTION_COUNT"; then
+ i=0
+ >hooks/pre-receive.push_options
+ while test "$i" -lt "$GIT_PUSH_OPTION_COUNT"; do
+ eval "value=\$GIT_PUSH_OPTION_$i"
+ echo $value >>hooks/pre-receive.push_options
+ i=$((i + 1))
+ done
+ fi
+ EOF
+ chmod u+x .git/hooks/pre-receive
+
+ cat >.git/hooks/post-receive <<-'EOF' &&
+ #!/bin/sh
+ if test -n "$GIT_PUSH_OPTION_COUNT"; then
+ i=0
+ >hooks/post-receive.push_options
+ while test "$i" -lt "$GIT_PUSH_OPTION_COUNT"; do
+ eval "value=\$GIT_PUSH_OPTION_$i"
+ echo $value >>hooks/post-receive.push_options
+ i=$((i + 1))
+ done
+ fi
+ EOF
+ chmod u+x .git/hooks/post-receive
+ ) &&
+ (
+ cd workbench &&
+ git remote add up ../upstream
+ )
+}
+
+# Compare the ref ($1) in upstream with a ref value from workbench ($2)
+# i.e. test_refs second HEAD@{2}
+test_refs () {
+ test $# = 2 &&
+ git -C upstream rev-parse --verify "$1" >expect &&
+ git -C workbench rev-parse --verify "$2" >actual &&
+ test_cmp expect actual
+}
+
+test_expect_success 'one push option works for a single branch' '
+ mk_repo_pair &&
+ git -C upstream config receive.advertisePushOptions true &&
+ (
+ cd workbench &&
+ test_commit one &&
+ git push --mirror up &&
+ test_commit two &&
+ git push --push-option=asdf up main
+ ) &&
+ test_refs main main &&
+ echo "asdf" >expect &&
+ test_cmp expect upstream/.git/hooks/pre-receive.push_options &&
+ test_cmp expect upstream/.git/hooks/post-receive.push_options
+'
+
+test_expect_success 'push option denied by remote' '
+ mk_repo_pair &&
+ git -C upstream config receive.advertisePushOptions false &&
+ (
+ cd workbench &&
+ test_commit one &&
+ git push --mirror up &&
+ test_commit two &&
+ test_must_fail git push --push-option=asdf up main
+ ) &&
+ test_refs main HEAD@{1}
+'
+
+test_expect_success 'two push options work' '
+ mk_repo_pair &&
+ git -C upstream config receive.advertisePushOptions true &&
+ (
+ cd workbench &&
+ test_commit one &&
+ git push --mirror up &&
+ test_commit two &&
+ git push --push-option=asdf --push-option="more structured text" up main
+ ) &&
+ test_refs main main &&
+ printf "asdf\nmore structured text\n" >expect &&
+ test_cmp expect upstream/.git/hooks/pre-receive.push_options &&
+ test_cmp expect upstream/.git/hooks/post-receive.push_options
+'
+
+test_expect_success 'push options and submodules' '
+ test_when_finished "rm -rf parent" &&
+ test_when_finished "rm -rf parent_upstream" &&
+ mk_repo_pair &&
+ git -C upstream config receive.advertisePushOptions true &&
+ cp -r upstream parent_upstream &&
+ test_commit -C upstream one &&
+
+ test_create_repo parent &&
+ git -C parent remote add up ../parent_upstream &&
+ test_commit -C parent one &&
+ git -C parent push --mirror up &&
+
+ test_config_global protocol.file.allow always &&
+ git -C parent submodule add ../upstream workbench &&
+ git -C parent/workbench remote add up ../../upstream &&
+ git -C parent commit -m "add submodule" &&
+
+ test_commit -C parent/workbench two &&
+ git -C parent add workbench &&
+ git -C parent commit -m "update workbench" &&
+
+ git -C parent push \
+ --push-option=asdf --push-option="more structured text" \
+ --recurse-submodules=on-demand up main &&
+
+ git -C upstream rev-parse --verify main >expect &&
+ git -C parent/workbench rev-parse --verify main >actual &&
+ test_cmp expect actual &&
+
+ git -C parent_upstream rev-parse --verify main >expect &&
+ git -C parent rev-parse --verify main >actual &&
+ test_cmp expect actual &&
+
+ printf "asdf\nmore structured text\n" >expect &&
+ test_cmp expect upstream/.git/hooks/pre-receive.push_options &&
+ test_cmp expect upstream/.git/hooks/post-receive.push_options &&
+ test_cmp expect parent_upstream/.git/hooks/pre-receive.push_options &&
+ test_cmp expect parent_upstream/.git/hooks/post-receive.push_options
+'
+
+test_expect_success 'default push option' '
+ mk_repo_pair &&
+ git -C upstream config receive.advertisePushOptions true &&
+ (
+ cd workbench &&
+ test_commit one &&
+ git push --mirror up &&
+ test_commit two &&
+ git -c push.pushOption=default push up main
+ ) &&
+ test_refs main main &&
+ echo "default" >expect &&
+ test_cmp expect upstream/.git/hooks/pre-receive.push_options &&
+ test_cmp expect upstream/.git/hooks/post-receive.push_options
+'
+
+test_expect_success 'two default push options' '
+ mk_repo_pair &&
+ git -C upstream config receive.advertisePushOptions true &&
+ (
+ cd workbench &&
+ test_commit one &&
+ git push --mirror up &&
+ test_commit two &&
+ git -c push.pushOption=default1 -c push.pushOption=default2 push up main
+ ) &&
+ test_refs main main &&
+ printf "default1\ndefault2\n" >expect &&
+ test_cmp expect upstream/.git/hooks/pre-receive.push_options &&
+ test_cmp expect upstream/.git/hooks/post-receive.push_options
+'
+
+test_expect_success 'push option from command line overrides from-config push option' '
+ mk_repo_pair &&
+ git -C upstream config receive.advertisePushOptions true &&
+ (
+ cd workbench &&
+ test_commit one &&
+ git push --mirror up &&
+ test_commit two &&
+ git -c push.pushOption=default push --push-option=manual up main
+ ) &&
+ test_refs main main &&
+ echo "manual" >expect &&
+ test_cmp expect upstream/.git/hooks/pre-receive.push_options &&
+ test_cmp expect upstream/.git/hooks/post-receive.push_options
+'
+
+test_expect_success 'empty value of push.pushOption in config clears the list' '
+ mk_repo_pair &&
+ git -C upstream config receive.advertisePushOptions true &&
+ (
+ cd workbench &&
+ test_commit one &&
+ git push --mirror up &&
+ test_commit two &&
+ git -c push.pushOption=default1 -c push.pushOption= -c push.pushOption=default2 push up main
+ ) &&
+ test_refs main main &&
+ echo "default2" >expect &&
+ test_cmp expect upstream/.git/hooks/pre-receive.push_options &&
+ test_cmp expect upstream/.git/hooks/post-receive.push_options
+'
+
+test_expect_success 'invalid push option in config' '
+ mk_repo_pair &&
+ git -C upstream config receive.advertisePushOptions true &&
+ (
+ cd workbench &&
+ test_commit one &&
+ git push --mirror up &&
+ test_commit two &&
+ test_must_fail git -c push.pushOption push up main
+ ) &&
+ test_refs main HEAD@{1}
+'
+
+test_expect_success 'push options keep quoted characters intact (direct)' '
+ mk_repo_pair &&
+ git -C upstream config receive.advertisePushOptions true &&
+ test_commit -C workbench one &&
+ git -C workbench push --push-option="\"embedded quotes\"" up main &&
+ echo "\"embedded quotes\"" >expect &&
+ test_cmp expect upstream/.git/hooks/pre-receive.push_options
+'
+
+. "$TEST_DIRECTORY"/lib-httpd.sh
+start_httpd
+
+# set up http repository for fetching/pushing, with push options config
+# bool set to $1
+mk_http_pair () {
+ test_when_finished "rm -rf test_http_clone" &&
+ test_when_finished 'rm -rf "$HTTPD_DOCUMENT_ROOT_PATH"/upstream.git' &&
+ mk_repo_pair &&
+ git -C upstream config receive.advertisePushOptions "$1" &&
+ git -C upstream config http.receivepack true &&
+ cp -R upstream/.git "$HTTPD_DOCUMENT_ROOT_PATH"/upstream.git &&
+ git clone "$HTTPD_URL"/smart/upstream test_http_clone
+}
+
+test_expect_success 'push option denied properly by http server' '
+ mk_http_pair false &&
+ test_commit -C test_http_clone one &&
+ test_must_fail git -C test_http_clone push --push-option=asdf origin main 2>actual &&
+ test_grep "the receiving end does not support push options" actual &&
+ git -C test_http_clone push origin main
+'
+
+test_expect_success 'push options work properly across http' '
+ mk_http_pair true &&
+
+ test_commit -C test_http_clone one &&
+ git -C test_http_clone push origin main &&
+ git -C "$HTTPD_DOCUMENT_ROOT_PATH"/upstream.git rev-parse --verify main >expect &&
+ git -C test_http_clone rev-parse --verify main >actual &&
+ test_cmp expect actual &&
+
+ test_commit -C test_http_clone two &&
+ git -C test_http_clone push --push-option=asdf --push-option="more structured text" origin main &&
+ printf "asdf\nmore structured text\n" >expect &&
+ test_cmp expect "$HTTPD_DOCUMENT_ROOT_PATH"/upstream.git/hooks/pre-receive.push_options &&
+ test_cmp expect "$HTTPD_DOCUMENT_ROOT_PATH"/upstream.git/hooks/post-receive.push_options &&
+
+ git -C "$HTTPD_DOCUMENT_ROOT_PATH"/upstream.git rev-parse --verify main >expect &&
+ git -C test_http_clone rev-parse --verify main >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'push options keep quoted characters intact (http)' '
+ mk_http_pair true &&
+
+ test_commit -C test_http_clone one &&
+ git -C test_http_clone push --push-option="\"embedded quotes\"" origin main &&
+ echo "\"embedded quotes\"" >expect &&
+ test_cmp expect "$HTTPD_DOCUMENT_ROOT_PATH"/upstream.git/hooks/pre-receive.push_options
+'
+
+# DO NOT add non-httpd-specific tests here, because the last part of this
+# test script is only executed when httpd is available and enabled.
+
+test_done
diff --git a/t/t5546-receive-limits.sh b/t/t5546-receive-limits.sh
new file mode 100755
index 0000000..9fc9ba5
--- /dev/null
+++ b/t/t5546-receive-limits.sh
@@ -0,0 +1,86 @@
+#!/bin/sh
+
+test_description='check receive input limits'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# Let's run tests with different unpack limits: 1 and 10000
+# When the limit is 1, `git receive-pack` will call `git index-pack`.
+# When the limit is 10000, `git receive-pack` will call `git unpack-objects`.
+
+validate_store_type () {
+ git -C dest count-objects -v >actual &&
+ case "$store_type" in
+ index)
+ grep "^count: 0$" actual ;;
+ unpack)
+ grep "^packs: 0$" actual ;;
+ esac || {
+ echo "store_type is $store_type"
+ cat actual
+ false;
+ }
+}
+
+test_pack_input_limit () {
+ store_type=$1
+
+ case "$store_type" in
+ index) unpack_limit=1 other_limit=10000 ;;
+ unpack) unpack_limit=10000 other_limit=1 ;;
+ esac
+
+ test_expect_success 'prepare destination repository' '
+ rm -fr dest &&
+ git --bare init dest
+ '
+
+ test_expect_success "set unpacklimit to $unpack_limit" '
+ git --git-dir=dest config receive.unpacklimit "$unpack_limit"
+ '
+
+ test_expect_success 'setting receive.maxInputSize to 512 rejects push' '
+ git --git-dir=dest config receive.maxInputSize 512 &&
+ test_must_fail git push dest HEAD
+ '
+
+ test_expect_success 'bumping limit to 4k allows push' '
+ git --git-dir=dest config receive.maxInputSize 4k &&
+ git push dest HEAD
+ '
+
+ test_expect_success 'prepare destination repository (again)' '
+ rm -fr dest &&
+ git --bare init dest
+ '
+
+ test_expect_success 'lifting the limit allows push' '
+ git --git-dir=dest config receive.maxInputSize 0 &&
+ git push dest HEAD
+ '
+
+ test_expect_success 'prepare destination repository (once more)' '
+ rm -fr dest &&
+ git --bare init dest
+ '
+
+ test_expect_success 'receive trumps transfer' '
+ git --git-dir=dest config receive.unpacklimit "$unpack_limit" &&
+ git --git-dir=dest config transfer.unpacklimit "$other_limit" &&
+ git push dest HEAD &&
+ validate_store_type
+ '
+
+}
+
+test_expect_success "create known-size (1024 bytes) commit" '
+ test-tool genrandom foo 1024 >one-k &&
+ git add one-k &&
+ test_commit one-k
+'
+
+test_pack_input_limit index
+test_pack_input_limit unpack
+
+test_done
diff --git a/t/t5547-push-quarantine.sh b/t/t5547-push-quarantine.sh
new file mode 100755
index 0000000..9f899b8
--- /dev/null
+++ b/t/t5547-push-quarantine.sh
@@ -0,0 +1,74 @@
+#!/bin/sh
+
+test_description='check quarantine of objects during push'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'create picky dest repo' '
+ git init --bare dest.git &&
+ test_hook --setup -C dest.git pre-receive <<-\EOF
+ while read old new ref; do
+ test "$(git log -1 --format=%s $new)" = reject && exit 1
+ done
+ exit 0
+ EOF
+'
+
+test_expect_success 'accepted objects work' '
+ test_commit ok &&
+ git push dest.git HEAD &&
+ commit=$(git rev-parse HEAD) &&
+ git --git-dir=dest.git cat-file commit $commit
+'
+
+test_expect_success 'rejected objects are not installed' '
+ test_commit reject &&
+ commit=$(git rev-parse HEAD) &&
+ test_must_fail git push dest.git reject &&
+ test_must_fail git --git-dir=dest.git cat-file commit $commit
+'
+
+test_expect_success 'rejected objects are removed' '
+ echo "incoming-*" >expect &&
+ (cd dest.git/objects && echo incoming-*) >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'push to repo path with path separator (colon)' '
+ # The interesting failure case here is when the
+ # receiving end cannot access its original object directory,
+ # so make it likely for us to generate a delta by having
+ # a non-trivial file with multiple versions.
+
+ test-tool genrandom foo 4096 >file.bin &&
+ git add file.bin &&
+ git commit -m bin &&
+
+ if test_have_prereq MINGW
+ then
+ pathsep=";"
+ else
+ pathsep=":"
+ fi &&
+ git clone --bare . "xxx${pathsep}yyy.git" &&
+
+ echo change >>file.bin &&
+ git commit -am change &&
+ # Note that we have to use the full path here, or it gets confused
+ # with the ssh host:path syntax.
+ git push "$(pwd)/xxx${pathsep}yyy.git" HEAD
+'
+
+test_expect_success 'updating a ref from quarantine is forbidden' '
+ git init --bare update.git &&
+ test_hook -C update.git pre-receive <<-\EOF &&
+ read old new refname
+ git update-ref refs/heads/unrelated $new
+ exit 1
+ EOF
+ test_must_fail git push update.git HEAD &&
+ git -C update.git fsck
+'
+
+test_done
diff --git a/t/t5548-push-porcelain.sh b/t/t5548-push-porcelain.sh
new file mode 100755
index 0000000..6282728
--- /dev/null
+++ b/t/t5548-push-porcelain.sh
@@ -0,0 +1,278 @@
+#!/bin/sh
+#
+# Copyright (c) 2020 Jiang Xin
+#
+test_description='Test git push porcelain output'
+
+. ./test-lib.sh
+
+# Create commits in <repo> and assign each commit's oid to shell variables
+# given in the arguments (A, B, and C). E.g.:
+#
+# create_commits_in <repo> A B C
+#
+# NOTE: Never calling this function from a subshell since variable
+# assignments will disappear when subshell exits.
+create_commits_in () {
+ repo="$1" && test -d "$repo" ||
+ error "Repository $repo does not exist."
+ shift &&
+ while test $# -gt 0
+ do
+ name=$1 &&
+ shift &&
+ test_commit -C "$repo" --no-tag "$name" &&
+ eval $name=$(git -C "$repo" rev-parse HEAD)
+ done
+}
+
+get_abbrev_oid () {
+ oid=$1 &&
+ suffix=${oid#???????} &&
+ oid=${oid%$suffix} &&
+ if test -n "$oid"
+ then
+ echo "$oid"
+ else
+ echo "undefined-oid"
+ fi
+}
+
+# Format the output of git-push, git-show-ref and other commands to make a
+# user-friendly and stable text. We can easily prepare the expect text
+# without having to worry about future changes of the commit ID and spaces
+# of the output.
+make_user_friendly_and_stable_output () {
+ sed \
+ -e "s/$(get_abbrev_oid $A)[0-9a-f]*/<COMMIT-A>/g" \
+ -e "s/$(get_abbrev_oid $B)[0-9a-f]*/<COMMIT-B>/g" \
+ -e "s/$ZERO_OID/<ZERO-OID>/g" \
+ -e "s#To $URL_PREFIX/upstream.git#To <URL/of/upstream.git>#"
+}
+
+format_and_save_expect () {
+ sed -e 's/^> //' -e 's/Z$//' >expect
+}
+
+setup_upstream_and_workbench () {
+ # Upstream after setup : main(B) foo(A) bar(A) baz(A)
+ # Workbench after setup : main(A)
+ test_expect_success "setup upstream repository and workbench" '
+ rm -rf upstream.git workbench &&
+ git init --bare upstream.git &&
+ git init workbench &&
+ create_commits_in workbench A B &&
+ (
+ cd workbench &&
+ # Try to make a stable fixed width for abbreviated commit ID,
+ # this fixed-width oid will be replaced with "<OID>".
+ git config core.abbrev 7 &&
+ git remote add origin ../upstream.git &&
+ git update-ref refs/heads/main $A &&
+ git push origin \
+ $B:refs/heads/main \
+ $A:refs/heads/foo \
+ $A:refs/heads/bar \
+ $A:refs/heads/baz
+ ) &&
+ git -C "workbench" config advice.pushUpdateRejected false &&
+ upstream=upstream.git
+ '
+}
+
+run_git_push_porcelain_output_test() {
+ case $1 in
+ http)
+ PROTOCOL="HTTP protocol"
+ URL_PREFIX="http://.*"
+ ;;
+ file)
+ PROTOCOL="builtin protocol"
+ URL_PREFIX="\.\."
+ ;;
+ esac
+
+ # Refs of upstream : main(B) foo(A) bar(A) baz(A)
+ # Refs of workbench: main(A) baz(A) next(A)
+ # git-push : main(A) NULL (B) baz(A) next(A)
+ test_expect_success "porcelain output of successful git-push ($PROTOCOL)" '
+ (
+ cd workbench &&
+ git update-ref refs/heads/main $A &&
+ git update-ref refs/heads/baz $A &&
+ git update-ref refs/heads/next $A &&
+ git push --porcelain --force origin \
+ main \
+ :refs/heads/foo \
+ $B:bar \
+ baz \
+ next
+ ) >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ format_and_save_expect <<-EOF &&
+ > To <URL/of/upstream.git>
+ > = refs/heads/baz:refs/heads/baz [up to date]
+ > <COMMIT-B>:refs/heads/bar <COMMIT-A>..<COMMIT-B>
+ > - :refs/heads/foo [deleted]
+ > + refs/heads/main:refs/heads/main <COMMIT-B>...<COMMIT-A> (forced update)
+ > * refs/heads/next:refs/heads/next [new branch]
+ > Done
+ EOF
+ test_cmp expect actual &&
+
+ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-B> refs/heads/bar
+ <COMMIT-A> refs/heads/baz
+ <COMMIT-A> refs/heads/main
+ <COMMIT-A> refs/heads/next
+ EOF
+ test_cmp expect actual
+ '
+
+ # Refs of upstream : main(A) bar(B) baz(A) next(A)
+ # Refs of workbench: main(B) bar(A) baz(A) next(A)
+ # git-push : main(B) bar(A) NULL next(A)
+ test_expect_success "atomic push failed ($PROTOCOL)" '
+ (
+ cd workbench &&
+ git update-ref refs/heads/main $B &&
+ git update-ref refs/heads/bar $A &&
+ test_must_fail git push --atomic --porcelain origin \
+ main \
+ bar \
+ :baz \
+ next
+ ) >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ format_and_save_expect <<-EOF &&
+ To <URL/of/upstream.git>
+ > = refs/heads/next:refs/heads/next [up to date]
+ > ! refs/heads/bar:refs/heads/bar [rejected] (non-fast-forward)
+ > ! (delete):refs/heads/baz [rejected] (atomic push failed)
+ > ! refs/heads/main:refs/heads/main [rejected] (atomic push failed)
+ Done
+ EOF
+ test_cmp expect actual &&
+
+ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-B> refs/heads/bar
+ <COMMIT-A> refs/heads/baz
+ <COMMIT-A> refs/heads/main
+ <COMMIT-A> refs/heads/next
+ EOF
+ test_cmp expect actual
+ '
+
+ test_expect_success "prepare pre-receive hook ($PROTOCOL)" '
+ test_hook --setup -C "$upstream" pre-receive <<-EOF
+ exit 1
+ EOF
+ '
+
+ # Refs of upstream : main(A) bar(B) baz(A) next(A)
+ # Refs of workbench: main(B) bar(A) baz(A) next(A)
+ # git-push : main(B) bar(A) NULL next(A)
+ test_expect_success "pre-receive hook declined ($PROTOCOL)" '
+ (
+ cd workbench &&
+ git update-ref refs/heads/main $B &&
+ git update-ref refs/heads/bar $A &&
+ test_must_fail git push --porcelain --force origin \
+ main \
+ bar \
+ :baz \
+ next
+ ) >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ format_and_save_expect <<-EOF &&
+ To <URL/of/upstream.git>
+ > = refs/heads/next:refs/heads/next [up to date]
+ > ! refs/heads/bar:refs/heads/bar [remote rejected] (pre-receive hook declined)
+ > ! :refs/heads/baz [remote rejected] (pre-receive hook declined)
+ > ! refs/heads/main:refs/heads/main [remote rejected] (pre-receive hook declined)
+ Done
+ EOF
+ test_cmp expect actual &&
+
+ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-B> refs/heads/bar
+ <COMMIT-A> refs/heads/baz
+ <COMMIT-A> refs/heads/main
+ <COMMIT-A> refs/heads/next
+ EOF
+ test_cmp expect actual
+ '
+
+ test_expect_success "remove pre-receive hook ($PROTOCOL)" '
+ rm "$upstream/hooks/pre-receive"
+ '
+
+ # Refs of upstream : main(A) bar(B) baz(A) next(A)
+ # Refs of workbench: main(B) bar(A) baz(A) next(A)
+ # git-push : main(B) bar(A) NULL next(A)
+ test_expect_success "non-fastforward push ($PROTOCOL)" '
+ (
+ cd workbench &&
+ test_must_fail git push --porcelain origin \
+ main \
+ bar \
+ :baz \
+ next
+ ) >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ format_and_save_expect <<-EOF &&
+ To <URL/of/upstream.git>
+ > = refs/heads/next:refs/heads/next [up to date]
+ > - :refs/heads/baz [deleted]
+ > refs/heads/main:refs/heads/main <COMMIT-A>..<COMMIT-B>
+ > ! refs/heads/bar:refs/heads/bar [rejected] (non-fast-forward)
+ Done
+ EOF
+ test_cmp expect actual &&
+
+ git -C "$upstream" show-ref >out &&
+ make_user_friendly_and_stable_output <out >actual &&
+ cat >expect <<-EOF &&
+ <COMMIT-B> refs/heads/bar
+ <COMMIT-B> refs/heads/main
+ <COMMIT-A> refs/heads/next
+ EOF
+ test_cmp expect actual
+ '
+}
+
+# Initialize the upstream repository and local workbench.
+setup_upstream_and_workbench
+
+# Run git-push porcelain test on builtin protocol
+run_git_push_porcelain_output_test file
+
+ROOT_PATH="$PWD"
+. "$TEST_DIRECTORY"/lib-gpg.sh
+. "$TEST_DIRECTORY"/lib-httpd.sh
+. "$TEST_DIRECTORY"/lib-terminal.sh
+start_httpd
+
+# Re-initialize the upstream repository and local workbench.
+setup_upstream_and_workbench
+
+test_expect_success "setup for http" '
+ git -C upstream.git config http.receivepack true &&
+ upstream="$HTTPD_DOCUMENT_ROOT_PATH/upstream.git" &&
+ mv upstream.git "$upstream" &&
+
+ git -C workbench remote set-url origin $HTTPD_URL/smart/upstream.git
+'
+
+setup_askpass_helper
+
+# Run git-push porcelain test on HTTP protocol
+run_git_push_porcelain_output_test http
+
+test_done
diff --git a/t/t5549-fetch-push-http.sh b/t/t5549-fetch-push-http.sh
new file mode 100755
index 0000000..2cdebcb
--- /dev/null
+++ b/t/t5549-fetch-push-http.sh
@@ -0,0 +1,72 @@
+#!/bin/sh
+
+test_description='fetch/push functionality using the HTTP protocol'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-httpd.sh
+start_httpd
+
+SERVER="$HTTPD_DOCUMENT_ROOT_PATH/server"
+URI="$HTTPD_URL/smart/server"
+
+grep_wrote () {
+ object_count=$1
+ file_name=$2
+ grep 'write_pack_file/wrote.*"value":"'$1'"' $2
+}
+
+setup_client_and_server () {
+ git init client &&
+ test_when_finished 'rm -rf client' &&
+ test_commit -C client first_commit &&
+ test_commit -C client second_commit &&
+
+ git init "$SERVER" &&
+ test_when_finished 'rm -rf "$SERVER"' &&
+ test_config -C "$SERVER" http.receivepack true &&
+ test_commit -C "$SERVER" unrelated_commit &&
+ git -C client push "$URI" first_commit:refs/remotes/origin/first_commit &&
+ git -C "$SERVER" config receive.hideRefs refs/remotes/origin/first_commit
+}
+
+test_expect_success 'push without negotiation (for comparing object counts with the next test)' '
+ setup_client_and_server &&
+
+ GIT_TRACE2_EVENT="$(pwd)/event" git -C client -c protocol.version=2 \
+ push "$URI" refs/heads/main:refs/remotes/origin/main &&
+ test_when_finished "rm -f event" &&
+ grep_wrote 6 event # 2 commits, 2 trees, 2 blobs
+'
+
+test_expect_success 'push with negotiation' '
+ setup_client_and_server &&
+
+ GIT_TRACE2_EVENT="$(pwd)/event" git -C client -c protocol.version=2 -c push.negotiate=1 \
+ push "$URI" refs/heads/main:refs/remotes/origin/main &&
+ test_when_finished "rm -f event" &&
+ grep_wrote 3 event # 1 commit, 1 tree, 1 blob
+'
+
+test_expect_success 'push with negotiation proceeds anyway even if negotiation fails' '
+ setup_client_and_server &&
+
+ # Use protocol v0 to make negotiation fail (because protocol v0 does
+ # not support the "wait-for-done" capability, which is required for
+ # push negotiation)
+ GIT_TEST_PROTOCOL_VERSION=0 GIT_TRACE2_EVENT="$(pwd)/event" git -C client -c push.negotiate=1 \
+ push "$URI" refs/heads/main:refs/remotes/origin/main 2>err &&
+ test_when_finished "rm -f event" &&
+ grep_wrote 6 event && # 2 commits, 2 trees, 2 blobs
+
+ cat >warning-expect <<-EOF &&
+ warning: --negotiate-only requires protocol v2
+ warning: push negotiation failed; proceeding anyway with push
+EOF
+ grep warning: err >warning-actual &&
+ test_cmp warning-expect warning-actual
+'
+
+test_done
diff --git a/t/t5550-http-fetch-dumb.sh b/t/t5550-http-fetch-dumb.sh
new file mode 100755
index 0000000..e444b30
--- /dev/null
+++ b/t/t5550-http-fetch-dumb.sh
@@ -0,0 +1,488 @@
+#!/bin/sh
+
+test_description='test dumb fetching over http via static file'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+if test_have_prereq !REFFILES
+then
+ skip_all='skipping test; dumb HTTP protocol not supported with reftable.'
+ test_done
+fi
+
+. "$TEST_DIRECTORY"/lib-httpd.sh
+start_httpd
+
+test_expect_success 'setup repository' '
+ git config push.default matching &&
+ echo content1 >file &&
+ git add file &&
+ git commit -m one &&
+ echo content2 >file &&
+ git add file &&
+ git commit -m two
+'
+
+setup_post_update_server_info_hook () {
+ test_hook --setup -C "$1" post-update <<-\EOF &&
+ exec git update-server-info
+ EOF
+ git -C "$1" update-server-info
+}
+
+test_expect_success 'create http-accessible bare repository with loose objects' '
+ cp -R .git "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
+ git -C "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" config core.bare true &&
+ setup_post_update_server_info_hook "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
+ git remote add public "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
+ git push public main:main
+'
+
+test_expect_success 'clone http repository' '
+ git clone $HTTPD_URL/dumb/repo.git clone-tmpl &&
+ cp -R clone-tmpl clone &&
+ test_cmp file clone/file
+'
+
+test_expect_success 'list refs from outside any repository' '
+ cat >expect <<-EOF &&
+ $(git rev-parse main) HEAD
+ $(git rev-parse main) refs/heads/main
+ EOF
+ nongit git ls-remote "$HTTPD_URL/dumb/repo.git" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'create password-protected repository' '
+ mkdir -p "$HTTPD_DOCUMENT_ROOT_PATH/auth/dumb/" &&
+ cp -Rf "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
+ "$HTTPD_DOCUMENT_ROOT_PATH/auth/dumb/repo.git"
+'
+
+test_expect_success 'create empty remote repository' '
+ git init --bare "$HTTPD_DOCUMENT_ROOT_PATH/empty.git" &&
+ setup_post_update_server_info_hook "$HTTPD_DOCUMENT_ROOT_PATH/empty.git"
+'
+
+test_expect_success 'empty dumb HTTP repository has default hash algorithm' '
+ test_when_finished "rm -fr clone-empty" &&
+ git clone $HTTPD_URL/dumb/empty.git clone-empty &&
+ git -C clone-empty rev-parse --show-object-format >empty-format &&
+ test "$(cat empty-format)" = "$(test_oid algo)"
+'
+
+setup_askpass_helper
+
+test_expect_success 'cloning password-protected repository can fail' '
+ set_askpass wrong &&
+ test_must_fail git clone "$HTTPD_URL/auth/dumb/repo.git" clone-auth-fail &&
+ expect_askpass both wrong
+'
+
+test_expect_success 'http auth can use user/pass in URL' '
+ set_askpass wrong &&
+ git clone "$HTTPD_URL_USER_PASS/auth/dumb/repo.git" clone-auth-none &&
+ expect_askpass none
+'
+
+test_expect_success 'http auth can use just user in URL' '
+ set_askpass wrong pass@host &&
+ git clone "$HTTPD_URL_USER/auth/dumb/repo.git" clone-auth-pass &&
+ expect_askpass pass user@host
+'
+
+test_expect_success 'http auth can request both user and pass' '
+ set_askpass user@host pass@host &&
+ git clone "$HTTPD_URL/auth/dumb/repo.git" clone-auth-both &&
+ expect_askpass both user@host
+'
+
+test_expect_success 'http auth respects credential helper config' '
+ test_config_global credential.helper "!f() {
+ cat >/dev/null
+ echo username=user@host
+ echo password=pass@host
+ }; f" &&
+ set_askpass wrong &&
+ git clone "$HTTPD_URL/auth/dumb/repo.git" clone-auth-helper &&
+ expect_askpass none
+'
+
+test_expect_success 'http auth can get username from config' '
+ test_config_global "credential.$HTTPD_URL.username" user@host &&
+ set_askpass wrong pass@host &&
+ git clone "$HTTPD_URL/auth/dumb/repo.git" clone-auth-user &&
+ expect_askpass pass user@host
+'
+
+test_expect_success 'configured username does not override URL' '
+ test_config_global "credential.$HTTPD_URL.username" wrong &&
+ set_askpass wrong pass@host &&
+ git clone "$HTTPD_URL_USER/auth/dumb/repo.git" clone-auth-user2 &&
+ expect_askpass pass user@host
+'
+
+test_expect_success 'set up repo with http submodules' '
+ git init super &&
+ set_askpass user@host pass@host &&
+ (
+ cd super &&
+ git submodule add "$HTTPD_URL/auth/dumb/repo.git" sub &&
+ git commit -m "add submodule"
+ )
+'
+
+test_expect_success 'cmdline credential config passes to submodule via clone' '
+ set_askpass wrong pass@host &&
+ test_must_fail git clone --recursive super super-clone &&
+ rm -rf super-clone &&
+
+ set_askpass wrong pass@host &&
+ git -c "credential.$HTTPD_URL.username=user@host" \
+ clone --recursive super super-clone &&
+ expect_askpass pass user@host
+'
+
+test_expect_success 'cmdline credential config passes submodule via fetch' '
+ set_askpass wrong pass@host &&
+ test_must_fail git -C super-clone fetch --recurse-submodules &&
+
+ set_askpass wrong pass@host &&
+ git -C super-clone \
+ -c "credential.$HTTPD_URL.username=user@host" \
+ fetch --recurse-submodules &&
+ expect_askpass pass user@host
+'
+
+test_expect_success 'cmdline credential config passes submodule update' '
+ # advance the submodule HEAD so that a fetch is required
+ git commit --allow-empty -m foo &&
+ git push "$HTTPD_DOCUMENT_ROOT_PATH/auth/dumb/repo.git" HEAD &&
+ sha1=$(git rev-parse HEAD) &&
+ git -C super-clone update-index --cacheinfo 160000,$sha1,sub &&
+
+ set_askpass wrong pass@host &&
+ test_must_fail git -C super-clone submodule update &&
+
+ set_askpass wrong pass@host &&
+ git -C super-clone \
+ -c "credential.$HTTPD_URL.username=user@host" \
+ submodule update &&
+ expect_askpass pass user@host
+'
+
+test_expect_success 'fetch changes via http' '
+ echo content >>file &&
+ git commit -a -m two &&
+ git push public &&
+ (cd clone && git pull) &&
+ test_cmp file clone/file
+'
+
+test_expect_success 'fetch changes via manual http-fetch' '
+ cp -R clone-tmpl clone2 &&
+
+ HEAD=$(git rev-parse --verify HEAD) &&
+ (cd clone2 &&
+ git http-fetch -a -w heads/main-new $HEAD $(git config remote.origin.url) &&
+ git checkout main-new &&
+ test $HEAD = $(git rev-parse --verify HEAD)) &&
+ test_cmp file clone2/file
+'
+
+test_expect_success 'manual http-fetch without -a works just as well' '
+ cp -R clone-tmpl clone3 &&
+
+ HEAD=$(git rev-parse --verify HEAD) &&
+ (cd clone3 &&
+ git http-fetch -w heads/main-new $HEAD $(git config remote.origin.url) &&
+ git checkout main-new &&
+ test $HEAD = $(git rev-parse --verify HEAD)) &&
+ test_cmp file clone3/file
+'
+
+test_expect_success 'http remote detects correct HEAD' '
+ git push public main:other &&
+ (cd clone &&
+ git remote set-head origin -d &&
+ git remote set-head origin -a &&
+ git symbolic-ref refs/remotes/origin/HEAD > output &&
+ echo refs/remotes/origin/main > expect &&
+ test_cmp expect output
+ )
+'
+
+test_expect_success 'fetch packed objects' '
+ cp -R "$HTTPD_DOCUMENT_ROOT_PATH"/repo.git "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git &&
+ (cd "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git &&
+ git --bare repack -a -d
+ ) &&
+ git clone $HTTPD_URL/dumb/repo_pack.git
+'
+
+test_expect_success 'http-fetch --packfile' '
+ # Arbitrary hash. Use rev-parse so that we get one of the correct
+ # length.
+ ARBITRARY=$(git -C "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git rev-parse HEAD) &&
+
+ git init packfileclient &&
+ p=$(cd "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git && ls objects/pack/pack-*.pack) &&
+ git -C packfileclient http-fetch --packfile=$ARBITRARY \
+ --index-pack-arg=index-pack --index-pack-arg=--stdin \
+ --index-pack-arg=--keep \
+ "$HTTPD_URL"/dumb/repo_pack.git/$p >out &&
+
+ grep -E "^keep.[0-9a-f]{16,}$" out &&
+ cut -c6- out >packhash &&
+
+ # Ensure that the expected files are generated
+ test -e "packfileclient/.git/objects/pack/pack-$(cat packhash).pack" &&
+ test -e "packfileclient/.git/objects/pack/pack-$(cat packhash).idx" &&
+ test -e "packfileclient/.git/objects/pack/pack-$(cat packhash).keep" &&
+
+ # Ensure that it has the HEAD of repo_pack, at least
+ HASH=$(git -C "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git rev-parse HEAD) &&
+ git -C packfileclient cat-file -e "$HASH"
+'
+
+test_expect_success 'fetch notices corrupt pack' '
+ cp -R "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git "$HTTPD_DOCUMENT_ROOT_PATH"/repo_bad1.git &&
+ (cd "$HTTPD_DOCUMENT_ROOT_PATH"/repo_bad1.git &&
+ p=$(ls objects/pack/pack-*.pack) &&
+ chmod u+w $p &&
+ printf %0256d 0 | dd of=$p bs=256 count=1 seek=1 conv=notrunc
+ ) &&
+ mkdir repo_bad1.git &&
+ (cd repo_bad1.git &&
+ git --bare init &&
+ test_must_fail git --bare fetch $HTTPD_URL/dumb/repo_bad1.git &&
+ test 0 = $(ls objects/pack/pack-*.pack | wc -l)
+ )
+'
+
+test_expect_success 'http-fetch --packfile with corrupt pack' '
+ rm -rf packfileclient &&
+ git init packfileclient &&
+ p=$(cd "$HTTPD_DOCUMENT_ROOT_PATH"/repo_bad1.git && ls objects/pack/pack-*.pack) &&
+ test_must_fail git -C packfileclient http-fetch --packfile \
+ "$HTTPD_URL"/dumb/repo_bad1.git/$p
+'
+
+test_expect_success 'fetch notices corrupt idx' '
+ cp -R "$HTTPD_DOCUMENT_ROOT_PATH"/repo_pack.git "$HTTPD_DOCUMENT_ROOT_PATH"/repo_bad2.git &&
+ (cd "$HTTPD_DOCUMENT_ROOT_PATH"/repo_bad2.git &&
+ p=$(ls objects/pack/pack-*.idx) &&
+ chmod u+w $p &&
+ printf %0256d 0 | dd of=$p bs=256 count=1 seek=1 conv=notrunc
+ ) &&
+ mkdir repo_bad2.git &&
+ (cd repo_bad2.git &&
+ git --bare init &&
+ test_must_fail git --bare fetch $HTTPD_URL/dumb/repo_bad2.git &&
+ test 0 = $(ls objects/pack | wc -l)
+ )
+'
+
+test_expect_success 'fetch can handle previously-fetched .idx files' '
+ git checkout --orphan branch1 &&
+ echo base >file &&
+ git add file &&
+ git commit -m base &&
+ git --bare init "$HTTPD_DOCUMENT_ROOT_PATH"/repo_packed_branches.git &&
+ git push "$HTTPD_DOCUMENT_ROOT_PATH"/repo_packed_branches.git branch1 &&
+ git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH"/repo_packed_branches.git repack -d &&
+ git checkout -b branch2 branch1 &&
+ echo b2 >>file &&
+ git commit -a -m b2 &&
+ git push "$HTTPD_DOCUMENT_ROOT_PATH"/repo_packed_branches.git branch2 &&
+ git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH"/repo_packed_branches.git repack -d &&
+ git --bare init clone_packed_branches.git &&
+ git --git-dir=clone_packed_branches.git fetch "$HTTPD_URL"/dumb/repo_packed_branches.git branch1:branch1 &&
+ git --git-dir=clone_packed_branches.git fetch "$HTTPD_URL"/dumb/repo_packed_branches.git branch2:branch2
+'
+
+test_expect_success 'did not use upload-pack service' '
+ ! grep "/git-upload-pack" "$HTTPD_ROOT_PATH/access.log"
+'
+
+test_expect_success 'git client shows text/plain errors' '
+ test_must_fail git clone "$HTTPD_URL/error/text" 2>stderr &&
+ grep "this is the error message" stderr
+'
+
+test_expect_success 'git client does not show html errors' '
+ test_must_fail git clone "$HTTPD_URL/error/html" 2>stderr &&
+ ! grep "this is the error message" stderr
+'
+
+test_expect_success 'git client shows text/plain with a charset' '
+ test_must_fail git clone "$HTTPD_URL/error/charset" 2>stderr &&
+ grep "this is the error message" stderr
+'
+
+test_expect_success 'http error messages are reencoded' '
+ test_must_fail git clone "$HTTPD_URL/error/utf16" 2>stderr &&
+ grep "this is the error message" stderr
+'
+
+test_expect_success 'reencoding is robust to whitespace oddities' '
+ test_must_fail git clone "$HTTPD_URL/error/odd-spacing" 2>stderr &&
+ grep "this is the error message" stderr
+'
+
+check_language () {
+ case "$2" in
+ '')
+ >expect
+ ;;
+ ?*)
+ echo "=> Send header: Accept-Language: $1" >expect
+ ;;
+ esac &&
+ GIT_TRACE_CURL=true \
+ LANGUAGE=$2 \
+ git ls-remote "$HTTPD_URL/dumb/repo.git" >output 2>&1 &&
+ tr -d '\015' <output |
+ sort -u |
+ sed -ne '/^=> Send header: Accept-Language:/ p' >actual &&
+ test_cmp expect actual
+}
+
+test_expect_success 'git client sends Accept-Language based on LANGUAGE' '
+ check_language "ko-KR, *;q=0.9" ko_KR.UTF-8'
+
+test_expect_success 'git client sends Accept-Language correctly with unordinary LANGUAGE' '
+ check_language "ko-KR, *;q=0.9" "ko_KR:" &&
+ check_language "ko-KR, en-US;q=0.9, *;q=0.8" "ko_KR::en_US" &&
+ check_language "ko-KR, *;q=0.9" ":::ko_KR" &&
+ check_language "ko-KR, en-US;q=0.9, *;q=0.8" "ko_KR!!:en_US" &&
+ check_language "ko-KR, ja-JP;q=0.9, *;q=0.8" "ko_KR en_US:ja_JP"'
+
+test_expect_success 'git client sends Accept-Language with many preferred languages' '
+ check_language "ko-KR, en-US;q=0.9, fr-CA;q=0.8, de;q=0.7, sr;q=0.6, \
+ja;q=0.5, zh;q=0.4, sv;q=0.3, pt;q=0.2, *;q=0.1" \
+ ko_KR.EUC-KR:en_US.UTF-8:fr_CA:de.UTF-8@euro:sr@latin:ja:zh:sv:pt &&
+ check_language "ko-KR, en-US;q=0.99, fr-CA;q=0.98, de;q=0.97, sr;q=0.96, \
+ja;q=0.95, zh;q=0.94, sv;q=0.93, pt;q=0.92, nb;q=0.91, *;q=0.90" \
+ ko_KR.EUC-KR:en_US.UTF-8:fr_CA:de.UTF-8@euro:sr@latin:ja:zh:sv:pt:nb
+'
+
+test_expect_success 'git client send an empty Accept-Language' '
+ GIT_TRACE_CURL=true LANGUAGE= git ls-remote "$HTTPD_URL/dumb/repo.git" 2>stderr &&
+ ! grep "^=> Send header: Accept-Language:" stderr
+'
+
+test_expect_success 'remote-http complains cleanly about malformed urls' '
+ test_must_fail git remote-http http::/example.com/repo.git 2>stderr &&
+ test_grep "url has no scheme" stderr
+'
+
+# NEEDSWORK: Writing commands to git-remote-curl can race against the latter
+# erroring out, producing SIGPIPE. Remove "ok=sigpipe" once transport-helper has
+# learned to handle early remote helper failures more cleanly.
+test_expect_success 'remote-http complains cleanly about empty scheme' '
+ test_must_fail ok=sigpipe git ls-remote \
+ http::${HTTPD_URL#http}/dumb/repo.git 2>stderr &&
+ test_grep "url has no scheme" stderr
+'
+
+test_expect_success 'redirects can be forbidden/allowed' '
+ test_must_fail git -c http.followRedirects=false \
+ clone $HTTPD_URL/dumb-redir/repo.git dumb-redir &&
+ git -c http.followRedirects=true \
+ clone $HTTPD_URL/dumb-redir/repo.git dumb-redir 2>stderr
+'
+
+test_expect_success 'redirects are reported to stderr' '
+ # just look for a snippet of the redirected-to URL
+ test_grep /dumb/ stderr
+'
+
+test_expect_success 'non-initial redirects can be forbidden' '
+ test_must_fail git -c http.followRedirects=initial \
+ clone $HTTPD_URL/redir-objects/repo.git redir-objects &&
+ git -c http.followRedirects=true \
+ clone $HTTPD_URL/redir-objects/repo.git redir-objects
+'
+
+test_expect_success 'http.followRedirects defaults to "initial"' '
+ test_must_fail git clone $HTTPD_URL/redir-objects/repo.git default
+'
+
+# The goal is for a clone of the "evil" repository, which has no objects
+# itself, to cause the client to fetch objects from the "victim" repository.
+test_expect_success 'set up evil alternates scheme' '
+ victim=$HTTPD_DOCUMENT_ROOT_PATH/victim.git &&
+ git init --bare "$victim" &&
+ git -C "$victim" --work-tree=. commit --allow-empty -m secret &&
+ git -C "$victim" repack -ad &&
+ git -C "$victim" update-server-info &&
+ sha1=$(git -C "$victim" rev-parse HEAD) &&
+
+ evil=$HTTPD_DOCUMENT_ROOT_PATH/evil.git &&
+ git init --template= --bare "$evil" &&
+ mkdir "$evil/info" &&
+ # do this by hand to avoid object existence check
+ printf "%s\\t%s\\n" $sha1 refs/heads/main >"$evil/info/refs"
+'
+
+# Here we'll just redirect via HTTP. In a real-world attack these would be on
+# different servers, but we should reject it either way.
+test_expect_success 'http-alternates is a non-initial redirect' '
+ echo "$HTTPD_URL/dumb/victim.git/objects" \
+ >"$evil/objects/info/http-alternates" &&
+ test_must_fail git -c http.followRedirects=initial \
+ clone $HTTPD_URL/dumb/evil.git evil-initial &&
+ git -c http.followRedirects=true \
+ clone $HTTPD_URL/dumb/evil.git evil-initial
+'
+
+# Curl supports a lot of protocols that we'd prefer not to allow
+# http-alternates to use, but it's hard to test whether curl has
+# accessed, say, the SMTP protocol, because we are not running an SMTP server.
+# But we can check that it does not allow access to file://, which would
+# otherwise allow this clone to complete.
+test_expect_success 'http-alternates cannot point at funny protocols' '
+ echo "file://$victim/objects" >"$evil/objects/info/http-alternates" &&
+ test_must_fail git -c http.followRedirects=true \
+ clone "$HTTPD_URL/dumb/evil.git" evil-file
+'
+
+test_expect_success 'http-alternates triggers not-from-user protocol check' '
+ echo "$HTTPD_URL/dumb/victim.git/objects" \
+ >"$evil/objects/info/http-alternates" &&
+ test_config_global http.followRedirects true &&
+ test_must_fail git -c protocol.http.allow=user \
+ clone $HTTPD_URL/dumb/evil.git evil-user &&
+ git -c protocol.http.allow=always \
+ clone $HTTPD_URL/dumb/evil.git evil-user
+'
+
+test_expect_success 'can redirect through non-"info/refs?service=git-upload-pack" URL' '
+ git clone "$HTTPD_URL/redir-to/dumb/repo.git"
+'
+
+test_expect_success 'print HTTP error when any intermediate redirect throws error' '
+ test_must_fail git clone "$HTTPD_URL/redir-to/502" 2> stderr &&
+ test_grep "unable to access.*/redir-to/502" stderr
+'
+
+test_expect_success 'fetching via http alternates works' '
+ parent=$HTTPD_DOCUMENT_ROOT_PATH/alt-parent.git &&
+ git init --bare "$parent" &&
+ git -C "$parent" --work-tree=. commit --allow-empty -m foo &&
+ git -C "$parent" update-server-info &&
+ commit=$(git -C "$parent" rev-parse HEAD) &&
+
+ child=$HTTPD_DOCUMENT_ROOT_PATH/alt-child.git &&
+ git init --bare "$child" &&
+ echo "../../alt-parent.git/objects" >"$child/objects/info/alternates" &&
+ git -C "$child" update-ref HEAD $commit &&
+ git -C "$child" update-server-info &&
+
+ git -c http.followredirects=true clone "$HTTPD_URL/dumb/alt-child.git"
+'
+
+test_done
diff --git a/t/t5551-http-fetch-smart.sh b/t/t5551-http-fetch-smart.sh
new file mode 100755
index 0000000..8a41adf
--- /dev/null
+++ b/t/t5551-http-fetch-smart.sh
@@ -0,0 +1,734 @@
+#!/bin/sh
+
+: ${HTTP_PROTO:=HTTP/1.1}
+test_description="test smart fetching over http via http-backend ($HTTP_PROTO)"
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-httpd.sh
+test "$HTTP_PROTO" = "HTTP/2" && enable_http2
+start_httpd
+
+test_expect_success HTTP2 'enable client-side http/2' '
+ git config --global http.version HTTP/2
+'
+
+test_expect_success 'setup repository' '
+ git config push.default matching &&
+ echo content >file &&
+ git add file &&
+ git commit -m one
+'
+
+test_expect_success 'create http-accessible bare repository' '
+ mkdir "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
+ (cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
+ git --bare init
+ ) &&
+ git remote add public "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
+ git push public main:main
+'
+
+setup_askpass_helper
+
+test_expect_success 'clone http repository' '
+ if test_have_prereq HTTP2 && test "$HTTPD_PROTO" = "https"
+ then
+ # ALPN lets us immediately use HTTP/2; likewise, POSTs with
+ # bodies can use it because they do not need to upgrade
+ INITIAL_PROTO=HTTP/2
+ else
+ # either we are not using HTTP/2, or the initial
+ # request is sent via HTTP/1.1 and asks for upgrade
+ INITIAL_PROTO=HTTP/1.1
+ fi &&
+
+ cat >exp.raw <<-EOF &&
+ > GET /smart/repo.git/info/refs?service=git-upload-pack $INITIAL_PROTO
+ > accept: */*
+ > accept-encoding: ENCODINGS
+ > accept-language: ko-KR, *;q=0.9
+ > pragma: no-cache
+ {V2} > git-protocol: version=2
+ < $HTTP_PROTO 200 OK
+ < pragma: no-cache
+ < cache-control: no-cache, max-age=0, must-revalidate
+ < content-type: application/x-git-upload-pack-advertisement
+ > POST /smart/repo.git/git-upload-pack $INITIAL_PROTO
+ > accept-encoding: ENCODINGS
+ > content-type: application/x-git-upload-pack-request
+ > accept: application/x-git-upload-pack-result
+ > accept-language: ko-KR, *;q=0.9
+ {V2} > git-protocol: version=2
+ > content-length: xxx
+ < $INITIAL_PROTO 200 OK
+ < pragma: no-cache
+ < cache-control: no-cache, max-age=0, must-revalidate
+ < content-type: application/x-git-upload-pack-result
+ {V2} > POST /smart/repo.git/git-upload-pack $INITIAL_PROTO
+ {V2} > accept-encoding: ENCODINGS
+ {V2} > content-type: application/x-git-upload-pack-request
+ {V2} > accept: application/x-git-upload-pack-result
+ {V2} > accept-language: ko-KR, *;q=0.9
+ {V2} > git-protocol: version=2
+ {V2} > content-length: xxx
+ {V2} < $INITIAL_PROTO 200 OK
+ {V2} < pragma: no-cache
+ {V2} < cache-control: no-cache, max-age=0, must-revalidate
+ {V2} < content-type: application/x-git-upload-pack-result
+ EOF
+
+ if test "$GIT_TEST_PROTOCOL_VERSION" = 0
+ then
+ sed "/^{V2}/d" <exp.raw >exp
+ else
+ sed "s/^{V2} //" <exp.raw >exp
+ fi &&
+
+ GIT_TRACE_CURL=true LANGUAGE="ko_KR.UTF-8" \
+ git clone --quiet $HTTPD_URL/smart/repo.git clone 2>err &&
+ test_cmp file clone/file &&
+ tr '\''\015'\'' Q <err |
+ perl -pe '\''
+ s/(Send|Recv) header: ([A-Za-z0-9-]+):/
+ "$1 header: " . lc($2) . ":"
+ /e;
+ '\'' |
+ sed -e "
+ s/Q\$//
+ /^[^<=]/d
+ /^== Info:/d
+ /^=> Send header, /d
+ /^=> Send header:$/d
+ /^<= Recv header, /d
+ /^<= Recv header:$/d
+ s/=> Send header: //
+ s/= Recv header://
+ /^<= Recv data/d
+ /^=> Send data/d
+ /^<= Recv SSL data/d
+ /^=> Send SSL data/d
+ /^$/d
+ /^< $/d
+
+ /^[^><]/{
+ s/^/> /
+ }
+
+ /^< HTTP/ {
+ s/200$/200 OK/
+ }
+ /^< HTTP\\/1.1 101/d
+ /^[><] connection: /d
+ /^[><] upgrade: /d
+ /^> http2-settings: /d
+
+ /^> user-agent: /d
+ /^> host: /d
+ /^> POST /,$ {
+ /^> Accept: [*]\\/[*]/d
+ }
+ s/^> content-length: .*/> content-length: xxx/
+ /^> 00..want /d
+ /^> 00.*done/d
+
+ /^< server: /d
+ /^< expires: /d
+ /^< date: /d
+ /^< content-length: /d
+ /^< transfer-encoding: /d
+ " >actual &&
+
+ sed -e "s/^> accept-encoding: .*/> accept-encoding: ENCODINGS/" \
+ actual >actual.smudged &&
+ test_cmp exp actual.smudged &&
+
+ grep "accept-encoding:.*gzip" actual >actual.gzip
+'
+
+test_expect_success 'fetch changes via http' '
+ echo content >>file &&
+ git commit -a -m two &&
+ git push public &&
+ (cd clone && git pull) &&
+ test_cmp file clone/file
+'
+
+test_expect_success 'used upload-pack service' '
+ strip_access_log >log &&
+ grep "GET /smart/repo.git/info/refs?service=git-upload-pack HTTP/[0-9.]* 200" log &&
+ grep "POST /smart/repo.git/git-upload-pack HTTP/[0-9.]* 200" log
+'
+
+test_expect_success 'follow redirects (301)' '
+ git clone $HTTPD_URL/smart-redir-perm/repo.git --quiet repo-p
+'
+
+test_expect_success 'follow redirects (302)' '
+ git clone $HTTPD_URL/smart-redir-temp/repo.git --quiet repo-t
+'
+
+test_expect_success 'redirects re-root further requests' '
+ git clone $HTTPD_URL/smart-redir-limited/repo.git repo-redir-limited
+'
+
+test_expect_success 're-rooting dies on insane schemes' '
+ test_must_fail git clone $HTTPD_URL/insane-redir/repo.git insane
+'
+
+test_expect_success 'clone from password-protected repository' '
+ echo two >expect &&
+ set_askpass user@host pass@host &&
+ git clone --bare "$HTTPD_URL/auth/smart/repo.git" smart-auth &&
+ expect_askpass both user@host &&
+ git --git-dir=smart-auth log -1 --format=%s >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'clone from auth-only-for-push repository' '
+ echo two >expect &&
+ set_askpass wrong &&
+ git clone --bare "$HTTPD_URL/auth-push/smart/repo.git" smart-noauth &&
+ expect_askpass none &&
+ git --git-dir=smart-noauth log -1 --format=%s >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'clone from auth-only-for-objects repository' '
+ echo two >expect &&
+ set_askpass user@host pass@host &&
+ git clone --bare "$HTTPD_URL/auth-fetch/smart/repo.git" half-auth &&
+ expect_askpass both user@host &&
+ git --git-dir=half-auth log -1 --format=%s >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'no-op half-auth fetch does not require a password' '
+ set_askpass wrong &&
+
+ # NEEDSWORK: When using HTTP(S), protocol v0 supports a "half-auth"
+ # configuration with authentication required only when downloading
+ # objects and not refs, by having the HTTP server only require
+ # authentication for the "git-upload-pack" path and not "info/refs".
+ # This is not possible with protocol v2, since both objects and refs
+ # are obtained from the "git-upload-pack" path. A solution to this is
+ # to teach the server and client to be able to inline ls-refs requests
+ # as an Extra Parameter (see "git help gitformat-pack-protocol"), so that
+ # "info/refs" can serve refs, just like it does in protocol v0.
+ GIT_TEST_PROTOCOL_VERSION=0 git --git-dir=half-auth fetch &&
+ expect_askpass none
+'
+
+test_expect_success 'redirects send auth to new location' '
+ set_askpass user@host pass@host &&
+ git -c credential.useHttpPath=true \
+ clone $HTTPD_URL/smart-redir-auth/repo.git repo-redir-auth &&
+ expect_askpass both user@host auth/smart/repo.git
+'
+
+test_expect_success 'GIT_TRACE_CURL redacts auth details' '
+ rm -rf redact-auth trace &&
+ set_askpass user@host pass@host &&
+ GIT_TRACE_CURL="$(pwd)/trace" git clone --bare "$HTTPD_URL/auth/smart/repo.git" redact-auth &&
+ expect_askpass both user@host &&
+
+ # Ensure that there is no "Basic" followed by a base64 string, but that
+ # the auth details are redacted
+ ! grep -i "Authorization: Basic [0-9a-zA-Z+/]" trace &&
+ grep -i "Authorization: Basic <redacted>" trace
+'
+
+test_expect_success 'GIT_CURL_VERBOSE redacts auth details' '
+ rm -rf redact-auth trace &&
+ set_askpass user@host pass@host &&
+ GIT_CURL_VERBOSE=1 git clone --bare "$HTTPD_URL/auth/smart/repo.git" redact-auth 2>trace &&
+ expect_askpass both user@host &&
+
+ # Ensure that there is no "Basic" followed by a base64 string, but that
+ # the auth details are redacted
+ ! grep -i "Authorization: Basic [0-9a-zA-Z+/]" trace &&
+ grep -i "Authorization: Basic <redacted>" trace
+'
+
+test_expect_success 'GIT_TRACE_CURL does not redact auth details if GIT_TRACE_REDACT=0' '
+ rm -rf redact-auth trace &&
+ set_askpass user@host pass@host &&
+ GIT_TRACE_REDACT=0 GIT_TRACE_CURL="$(pwd)/trace" \
+ git clone --bare "$HTTPD_URL/auth/smart/repo.git" redact-auth &&
+ expect_askpass both user@host &&
+
+ grep -i "Authorization: Basic [0-9a-zA-Z+/]" trace
+'
+
+test_expect_success 'disable dumb http on server' '
+ git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
+ config http.getanyfile false
+'
+
+test_expect_success 'GIT_SMART_HTTP can disable smart http' '
+ (GIT_SMART_HTTP=0 &&
+ export GIT_SMART_HTTP &&
+ cd clone &&
+ test_must_fail git fetch)
+'
+
+test_expect_success 'invalid Content-Type rejected' '
+ test_must_fail git clone $HTTPD_URL/broken_smart/repo.git 2>actual &&
+ test_grep "not valid:" actual
+'
+
+test_expect_success 'create namespaced refs' '
+ test_commit namespaced &&
+ git push public HEAD:refs/namespaces/ns/refs/heads/main &&
+ git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
+ symbolic-ref refs/namespaces/ns/HEAD refs/namespaces/ns/refs/heads/main
+'
+
+test_expect_success 'smart clone respects namespace' '
+ git clone "$HTTPD_URL/smart_namespace/repo.git" ns-smart &&
+ echo namespaced >expect &&
+ git --git-dir=ns-smart/.git log -1 --format=%s >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'dumb clone via http-backend respects namespace' '
+ git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
+ config http.getanyfile true &&
+ GIT_SMART_HTTP=0 git clone \
+ "$HTTPD_URL/smart_namespace/repo.git" ns-dumb &&
+ echo namespaced >expect &&
+ git --git-dir=ns-dumb/.git log -1 --format=%s >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'cookies stored in http.cookiefile when http.savecookies set' '
+ cat >cookies.txt <<-\EOF &&
+ 127.0.0.1 FALSE /smart_cookies/ FALSE 0 othername othervalue
+ EOF
+ sort >expect_cookies.txt <<-\EOF &&
+ 127.0.0.1 FALSE /smart_cookies/ FALSE 0 othername othervalue
+ 127.0.0.1 FALSE /smart_cookies/repo.git/ FALSE 0 name value
+ 127.0.0.1 FALSE /smart_cookies/repo.git/info/ FALSE 0 name value
+ EOF
+ git config http.cookiefile cookies.txt &&
+ git config http.savecookies true &&
+
+ test_when_finished "
+ git --git-dir=\"\$HTTPD_DOCUMENT_ROOT_PATH/repo.git\" \
+ tag -d cookie-tag
+ " &&
+ git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
+ tag -m "foo" cookie-tag &&
+ git fetch $HTTPD_URL/smart_cookies/repo.git cookie-tag &&
+
+ grep "^[^#]" cookies.txt | sort >cookies_stripped.txt &&
+ test_cmp expect_cookies.txt cookies_stripped.txt
+'
+
+test_expect_success 'transfer.hiderefs works over smart-http' '
+ test_commit hidden &&
+ test_commit visible &&
+ git push public HEAD^:refs/heads/a HEAD:refs/heads/b &&
+ git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" \
+ config transfer.hiderefs refs/heads/a &&
+ git clone --bare "$HTTPD_URL/smart/repo.git" hidden.git &&
+ test_must_fail git -C hidden.git rev-parse --verify a &&
+ git -C hidden.git rev-parse --verify b
+'
+
+# create an arbitrary number of tags, numbered from tag-$1 to tag-$2
+create_tags () {
+ rm -f marks &&
+ for i in $(test_seq "$1" "$2")
+ do
+ # don't use here-doc, because it requires a process
+ # per loop iteration
+ echo "commit refs/heads/too-many-refs-$1" &&
+ echo "mark :$i" &&
+ echo "committer git <git@example.com> $i +0000" &&
+ echo "data 0" &&
+ echo "M 644 inline bla.txt" &&
+ echo "data 4" &&
+ echo "bla" &&
+ # make every commit dangling by always
+ # rewinding the branch after each commit
+ echo "reset refs/heads/too-many-refs-$1" &&
+ echo "from :$1"
+ done | git fast-import --export-marks=marks &&
+
+ # now assign tags to all the dangling commits we created above
+ tag=$(perl -e "print \"bla\" x 30") &&
+ sed -e "s|^:\([^ ]*\) \(.*\)$|\2 refs/tags/$tag-\1|" <marks >>packed-refs
+}
+
+test_expect_success 'create 2,000 tags in the repo' '
+ (
+ cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
+ create_tags 1 2000
+ )
+'
+
+test_expect_success CMDLINE_LIMIT \
+ 'clone the 2,000 tag repo to check OS command line overflow' '
+ run_with_limited_cmdline git clone $HTTPD_URL/smart/repo.git too-many-refs &&
+ (
+ cd too-many-refs &&
+ git for-each-ref refs/tags >actual &&
+ test_line_count = 2000 actual
+ )
+'
+
+test_expect_success 'large fetch-pack requests can be sent using chunked encoding' '
+ GIT_TRACE_CURL=true git -c http.postbuffer=65536 \
+ clone --bare "$HTTPD_URL/smart/repo.git" split.git 2>err &&
+ {
+ test_have_prereq HTTP2 ||
+ grep "^=> Send header: Transfer-Encoding: chunked" err
+ }
+'
+
+test_expect_success 'test allowreachablesha1inwant' '
+ test_when_finished "rm -rf test_reachable.git" &&
+ server="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
+ main_sha=$(git -C "$server" rev-parse refs/heads/main) &&
+ git -C "$server" config uploadpack.allowreachablesha1inwant 1 &&
+
+ git init --bare test_reachable.git &&
+ git -C test_reachable.git remote add origin "$HTTPD_URL/smart/repo.git" &&
+ git -C test_reachable.git fetch origin "$main_sha"
+'
+
+test_expect_success 'test allowreachablesha1inwant with unreachable' '
+ test_when_finished "rm -rf test_reachable.git; git reset --hard $(git rev-parse HEAD)" &&
+
+ #create unreachable sha
+ echo content >file2 &&
+ git add file2 &&
+ git commit -m two &&
+ git push public HEAD:refs/heads/doomed &&
+ git push public :refs/heads/doomed &&
+
+ server="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
+ main_sha=$(git -C "$server" rev-parse refs/heads/main) &&
+ git -C "$server" config uploadpack.allowreachablesha1inwant 1 &&
+
+ git init --bare test_reachable.git &&
+ git -C test_reachable.git remote add origin "$HTTPD_URL/smart/repo.git" &&
+ # Some protocol versions (e.g. 2) support fetching
+ # unadvertised objects, so restrict this test to v0.
+ test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
+ git -C test_reachable.git fetch origin "$(git rev-parse HEAD)"
+'
+
+test_expect_success 'test allowanysha1inwant with unreachable' '
+ test_when_finished "rm -rf test_reachable.git; git reset --hard $(git rev-parse HEAD)" &&
+
+ #create unreachable sha
+ echo content >file2 &&
+ git add file2 &&
+ git commit -m two &&
+ git push public HEAD:refs/heads/doomed &&
+ git push public :refs/heads/doomed &&
+
+ server="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
+ main_sha=$(git -C "$server" rev-parse refs/heads/main) &&
+ git -C "$server" config uploadpack.allowreachablesha1inwant 1 &&
+
+ git init --bare test_reachable.git &&
+ git -C test_reachable.git remote add origin "$HTTPD_URL/smart/repo.git" &&
+ # Some protocol versions (e.g. 2) support fetching
+ # unadvertised objects, so restrict this test to v0.
+ test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
+ git -C test_reachable.git fetch origin "$(git rev-parse HEAD)" &&
+
+ git -C "$server" config uploadpack.allowanysha1inwant 1 &&
+ git -C test_reachable.git fetch origin "$(git rev-parse HEAD)"
+'
+
+test_expect_success EXPENSIVE 'http can handle enormous ref negotiation' '
+ (
+ cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
+ create_tags 2001 50000
+ ) &&
+ git -C too-many-refs fetch -q --tags &&
+ (
+ cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
+ create_tags 50001 100000
+ ) &&
+ git -C too-many-refs fetch -q --tags &&
+ git -C too-many-refs for-each-ref refs/tags >tags &&
+ test_line_count = 100000 tags
+'
+
+test_expect_success 'custom http headers' '
+ test_must_fail git -c http.extraheader="x-magic-two: cadabra" \
+ fetch "$HTTPD_URL/smart_headers/repo.git" &&
+ git -c http.extraheader="x-magic-one: abra" \
+ -c http.extraheader="x-magic-two: cadabra" \
+ fetch "$HTTPD_URL/smart_headers/repo.git" &&
+ git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
+ git config -f .gitmodules submodule.sub.path sub &&
+ git config -f .gitmodules submodule.sub.url \
+ "$HTTPD_URL/smart_headers/repo.git" &&
+ git submodule init sub &&
+ test_must_fail git submodule update sub &&
+ git -c http.extraheader="x-magic-one: abra" \
+ -c http.extraheader="x-magic-two: cadabra" \
+ submodule update sub
+'
+
+test_expect_success 'using fetch command in remote-curl updates refs' '
+ SERVER="$HTTPD_DOCUMENT_ROOT_PATH/twobranch" &&
+ rm -rf "$SERVER" client &&
+
+ git init "$SERVER" &&
+ test_commit -C "$SERVER" foo &&
+ git -C "$SERVER" update-ref refs/heads/anotherbranch foo &&
+
+ git clone $HTTPD_URL/smart/twobranch client &&
+
+ test_commit -C "$SERVER" bar &&
+ git -C client -c protocol.version=0 fetch &&
+
+ git -C "$SERVER" rev-parse main >expect &&
+ git -C client rev-parse origin/main >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'fetch by SHA-1 without tag following' '
+ SERVER="$HTTPD_DOCUMENT_ROOT_PATH/server" &&
+ rm -rf "$SERVER" client &&
+
+ git init "$SERVER" &&
+ test_commit -C "$SERVER" foo &&
+
+ git clone $HTTPD_URL/smart/server client &&
+
+ test_commit -C "$SERVER" bar &&
+ git -C "$SERVER" rev-parse bar >bar_hash &&
+ git -C client -c protocol.version=0 fetch \
+ --no-tags origin $(cat bar_hash)
+'
+
+test_expect_success 'cookies are redacted by default' '
+ rm -rf clone &&
+ echo "Set-Cookie: Foo=1" >cookies &&
+ echo "Set-Cookie: Bar=2" >>cookies &&
+ GIT_TRACE_CURL=true \
+ git -c "http.cookieFile=$(pwd)/cookies" clone \
+ $HTTPD_URL/smart/repo.git clone 2>err &&
+ grep -i "Cookie:.*Foo=<redacted>" err &&
+ grep -i "Cookie:.*Bar=<redacted>" err &&
+ ! grep -i "Cookie:.*Foo=1" err &&
+ ! grep -i "Cookie:.*Bar=2" err
+'
+
+test_expect_success 'empty values of cookies are also redacted' '
+ rm -rf clone &&
+ echo "Set-Cookie: Foo=" >cookies &&
+ GIT_TRACE_CURL=true \
+ git -c "http.cookieFile=$(pwd)/cookies" clone \
+ $HTTPD_URL/smart/repo.git clone 2>err &&
+ grep -i "Cookie:.*Foo=<redacted>" err
+'
+
+test_expect_success 'GIT_TRACE_REDACT=0 disables cookie redaction' '
+ rm -rf clone &&
+ echo "Set-Cookie: Foo=1" >cookies &&
+ echo "Set-Cookie: Bar=2" >>cookies &&
+ GIT_TRACE_REDACT=0 GIT_TRACE_CURL=true \
+ git -c "http.cookieFile=$(pwd)/cookies" clone \
+ $HTTPD_URL/smart/repo.git clone 2>err &&
+ grep -i "Cookie:.*Foo=1" err &&
+ grep -i "Cookie:.*Bar=2" err
+'
+
+test_expect_success 'GIT_TRACE_CURL_NO_DATA prevents data from being traced' '
+ rm -rf clone &&
+ GIT_TRACE_CURL=true \
+ git clone $HTTPD_URL/smart/repo.git clone 2>err &&
+ grep "=> Send data" err &&
+
+ rm -rf clone &&
+ GIT_TRACE_CURL=true GIT_TRACE_CURL_NO_DATA=1 \
+ git clone $HTTPD_URL/smart/repo.git clone 2>err &&
+ ! grep "=> Send data" err
+'
+
+test_expect_success 'server-side error detected' '
+ test_must_fail git clone $HTTPD_URL/error_smart/repo.git 2>actual &&
+ test_grep "server-side error" actual
+'
+
+test_expect_success 'http auth remembers successful credentials' '
+ rm -f .git-credentials &&
+ test_config credential.helper store &&
+
+ # the first request prompts the user...
+ set_askpass user@host pass@host &&
+ git ls-remote "$HTTPD_URL/auth/smart/repo.git" >/dev/null &&
+ expect_askpass both user@host &&
+
+ # ...and the second one uses the stored value rather than
+ # prompting the user.
+ set_askpass bogus-user bogus-pass &&
+ git ls-remote "$HTTPD_URL/auth/smart/repo.git" >/dev/null &&
+ expect_askpass none
+'
+
+test_expect_success 'http auth forgets bogus credentials' '
+ # seed credential store with bogus values. In real life,
+ # this would probably come from a password which worked
+ # for a previous request.
+ rm -f .git-credentials &&
+ test_config credential.helper store &&
+ {
+ echo "url=$HTTPD_URL" &&
+ echo "username=bogus" &&
+ echo "password=bogus"
+ } | git credential approve &&
+
+ # we expect this to use the bogus values and fail, never even
+ # prompting the user...
+ set_askpass user@host pass@host &&
+ test_must_fail git ls-remote "$HTTPD_URL/auth/smart/repo.git" >/dev/null &&
+ expect_askpass none &&
+
+ # ...but now we should have forgotten the bad value, causing
+ # us to prompt the user again.
+ set_askpass user@host pass@host &&
+ git ls-remote "$HTTPD_URL/auth/smart/repo.git" >/dev/null &&
+ expect_askpass both user@host
+'
+
+test_expect_success 'client falls back from v2 to v0 to match server' '
+ GIT_TRACE_PACKET=$PWD/trace \
+ GIT_TEST_PROTOCOL_VERSION=2 \
+ git clone $HTTPD_URL/smart_v0/repo.git repo-v0 &&
+ # check for v0; there the HEAD symref is communicated in the capability
+ # line; v2 uses a different syntax on each ref advertisement line
+ grep symref=HEAD:refs/heads/ trace
+'
+
+test_expect_success 'create empty http-accessible SHA-256 repository' '
+ mkdir "$HTTPD_DOCUMENT_ROOT_PATH/sha256.git" &&
+ (cd "$HTTPD_DOCUMENT_ROOT_PATH/sha256.git" &&
+ git --bare init --object-format=sha256
+ )
+'
+
+test_expect_success 'clone empty SHA-256 repository with protocol v2' '
+ rm -fr sha256 &&
+ echo sha256 >expected &&
+ git -c protocol.version=2 clone "$HTTPD_URL/smart/sha256.git" &&
+ git -C sha256 rev-parse --show-object-format >actual &&
+ test_cmp actual expected &&
+ git ls-remote "$HTTPD_URL/smart/sha256.git" >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'clone empty SHA-256 repository with protocol v0' '
+ rm -fr sha256 &&
+ echo sha256 >expected &&
+ GIT_TRACE=1 GIT_TRACE_PACKET=1 git -c protocol.version=0 clone "$HTTPD_URL/smart/sha256.git" &&
+ git -C sha256 rev-parse --show-object-format >actual &&
+ test_cmp actual expected &&
+ git ls-remote "$HTTPD_URL/smart/sha256.git" >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'passing hostname resolution information works' '
+ BOGUS_HOST=gitbogusexamplehost.invalid &&
+ BOGUS_HTTPD_URL=$HTTPD_PROTO://$BOGUS_HOST:$LIB_HTTPD_PORT &&
+ test_must_fail git ls-remote "$BOGUS_HTTPD_URL/smart/repo.git" >/dev/null &&
+ git -c "http.curloptResolve=$BOGUS_HOST:$LIB_HTTPD_PORT:127.0.0.1" ls-remote "$BOGUS_HTTPD_URL/smart/repo.git" >/dev/null
+'
+
+# here user%40host is the URL-encoded version of user@host,
+# which is our intentionally-odd username to catch parsing errors
+url_user=$HTTPD_URL_USER/auth/smart/repo.git
+url_userpass=$HTTPD_URL_USER_PASS/auth/smart/repo.git
+url_userblank=$HTTPD_PROTO://user%40host:@$HTTPD_DEST/auth/smart/repo.git
+message="URL .*:<redacted>@.* uses plaintext credentials"
+
+test_expect_success 'clone warns or fails when using username:password' '
+ test_when_finished "rm -rf attempt*" &&
+
+ git -c transfer.credentialsInUrl=allow \
+ clone $url_userpass attempt1 2>err &&
+ ! grep "$message" err &&
+
+ git -c transfer.credentialsInUrl=warn \
+ clone $url_userpass attempt2 2>err &&
+ grep "warning: $message" err >warnings &&
+ test_line_count -ge 1 warnings &&
+
+ test_must_fail git -c transfer.credentialsInUrl=die \
+ clone $url_userpass attempt3 2>err &&
+ grep "fatal: $message" err >warnings &&
+ test_line_count -ge 1 warnings &&
+
+ test_must_fail git -c transfer.credentialsInUrl=die \
+ clone $url_userblank attempt4 2>err &&
+ grep "fatal: $message" err >warnings &&
+ test_line_count -ge 1 warnings
+'
+
+test_expect_success 'clone does not detect username:password when it is https://username@domain:port/' '
+ test_when_finished "rm -rf attempt1" &&
+
+ # we are relying on lib-httpd for url construction, so document our
+ # assumptions
+ case "$HTTPD_URL_USER" in
+ *:[0-9]*) : ok ;;
+ *) BUG "httpd url does not have port: $HTTPD_URL_USER"
+ esac &&
+
+ git -c transfer.credentialsInUrl=warn clone $url_user attempt1 2>err &&
+ ! grep "uses plaintext credentials" err
+'
+
+test_expect_success 'fetch warns or fails when using username:password' '
+ git -c transfer.credentialsInUrl=allow fetch $url_userpass 2>err &&
+ ! grep "$message" err &&
+
+ git -c transfer.credentialsInUrl=warn fetch $url_userpass 2>err &&
+ grep "warning: $message" err >warnings &&
+ test_line_count -ge 1 warnings &&
+
+ test_must_fail git -c transfer.credentialsInUrl=die \
+ fetch $url_userpass 2>err &&
+ grep "fatal: $message" err >warnings &&
+ test_line_count -ge 1 warnings &&
+
+ test_must_fail git -c transfer.credentialsInUrl=die \
+ fetch $url_userblank 2>err &&
+ grep "fatal: $message" err >warnings &&
+ test_line_count -ge 1 warnings
+'
+
+
+test_expect_success 'push warns or fails when using username:password' '
+ git -c transfer.credentialsInUrl=allow push $url_userpass 2>err &&
+ ! grep "$message" err &&
+
+ git -c transfer.credentialsInUrl=warn push $url_userpass 2>err &&
+ grep "warning: $message" err >warnings &&
+
+ test_must_fail git -c transfer.credentialsInUrl=die \
+ push $url_userpass 2>err &&
+ grep "fatal: $message" err >warnings &&
+ test_line_count -ge 1 warnings
+'
+
+test_expect_success 'no empty path components' '
+ # In the URL, add a trailing slash, and see if git appends yet another
+ # slash.
+ git clone $HTTPD_URL/smart/repo.git/ clone-with-slash &&
+
+ strip_access_log >log &&
+ ! grep "//" log
+'
+
+test_done
diff --git a/t/t5552-skipping-fetch-negotiator.sh b/t/t5552-skipping-fetch-negotiator.sh
new file mode 100755
index 0000000..b55a9f6
--- /dev/null
+++ b/t/t5552-skipping-fetch-negotiator.sh
@@ -0,0 +1,225 @@
+#!/bin/sh
+
+test_description='test skipping fetch negotiator'
+. ./test-lib.sh
+
+test_expect_success 'fetch.negotiationalgorithm config' '
+ test_when_finished "rm -rf repo" &&
+ git init repo &&
+ cat >repo/.git/config <<-\EOF &&
+ [fetch]
+ negotiationAlgorithm
+ EOF
+ cat >expect <<-\EOF &&
+ error: missing value for '\''fetch.negotiationalgorithm'\''
+ fatal: bad config variable '\''fetch.negotiationalgorithm'\'' in file '\''.git/config'\'' at line 2
+ EOF
+ test_expect_code 128 git -C repo fetch >out 2>actual &&
+ test_must_be_empty out &&
+ test_cmp expect actual
+'
+
+have_sent () {
+ while test "$#" -ne 0
+ do
+ grep "fetch> have $(git -C client rev-parse $1)" trace
+ if test $? -ne 0
+ then
+ echo "No have $(git -C client rev-parse $1) ($1)"
+ return 1
+ fi
+ shift
+ done
+}
+
+have_not_sent () {
+ while test "$#" -ne 0
+ do
+ grep "fetch> have $(git -C client rev-parse $1)" trace
+ if test $? -eq 0
+ then
+ return 1
+ fi
+ shift
+ done
+}
+
+# trace_fetch <client_dir> <server_dir> [args]
+#
+# Trace the packet output of fetch, but make sure we disable the variable
+# in the child upload-pack, so we don't combine the results in the same file.
+trace_fetch () {
+ client=$1; shift
+ server=$1; shift
+ GIT_TRACE_PACKET="$(pwd)/trace" \
+ git -C "$client" fetch \
+ --upload-pack 'unset GIT_TRACE_PACKET; git-upload-pack' \
+ "$server" "$@"
+}
+
+test_expect_success 'commits with no parents are sent regardless of skip distance' '
+ git init server &&
+ test_commit -C server to_fetch &&
+
+ git init client &&
+ for i in $(test_seq 7)
+ do
+ test_commit -C client c$i || return 1
+ done &&
+
+ # We send: "c7" (skip 1) "c5" (skip 2) "c2" (skip 4). After that, since
+ # "c1" has no parent, it is still sent as "have" even though it would
+ # normally be skipped.
+ test_config -C client fetch.negotiationalgorithm skipping &&
+ trace_fetch client "$(pwd)/server" &&
+ have_sent c7 c5 c2 c1 &&
+ have_not_sent c6 c4 c3
+'
+
+test_expect_success 'when two skips collide, favor the larger one' '
+ rm -rf server client trace &&
+ git init server &&
+ test_commit -C server to_fetch &&
+
+ git init client &&
+ for i in $(test_seq 11)
+ do
+ test_commit -C client c$i || return 1
+ done &&
+ git -C client checkout c5 &&
+ test_commit -C client c5side &&
+
+ # Before reaching c5, we send "c5side" (skip 1) and "c11" (skip 1) "c9"
+ # (skip 2) "c6" (skip 4). The larger skip (skip 4) takes precedence, so
+ # the next "have" sent will be "c1" (from "c6" skip 4) and not "c4"
+ # (from "c5side" skip 1).
+ test_config -C client fetch.negotiationalgorithm skipping &&
+ trace_fetch client "$(pwd)/server" &&
+ have_sent c5side c11 c9 c6 c1 &&
+ have_not_sent c10 c8 c7 c5 c4 c3 c2
+'
+
+test_expect_success 'use ref advertisement to filter out commits' '
+ rm -rf server client trace &&
+ git init server &&
+ test_commit -C server c1 &&
+ test_commit -C server c2 &&
+ test_commit -C server c3 &&
+ git -C server tag -d c1 c2 c3 &&
+
+ git clone server client &&
+ test_commit -C client c4 &&
+ test_commit -C client c5 &&
+ git -C client checkout c4^^ &&
+ test_commit -C client c2side &&
+
+ git -C server checkout --orphan anotherbranch &&
+ test_commit -C server to_fetch &&
+
+ # The server advertising "c3" (as "refs/heads/main") means that we do
+ # not need to send any ancestors of "c3", but we still need to send "c3"
+ # itself.
+ test_config -C client fetch.negotiationalgorithm skipping &&
+
+ # The ref advertisement itself is filtered when protocol v2 is used, so
+ # use v0.
+ (
+ GIT_TEST_PROTOCOL_VERSION=0 &&
+ export GIT_TEST_PROTOCOL_VERSION &&
+ trace_fetch client origin to_fetch
+ ) &&
+ have_sent c5 c4^ c2side &&
+ have_not_sent c4 c4^^ c4^^^
+'
+
+test_expect_success 'handle clock skew' '
+ rm -rf server client trace &&
+ git init server &&
+ test_commit -C server to_fetch &&
+
+ git init client &&
+
+ # 2 regular commits
+ test_tick=2000000000 &&
+ test_commit -C client c1 &&
+ test_commit -C client c2 &&
+
+ # 4 old commits
+ test_tick=1000000000 &&
+ git -C client checkout c1 &&
+ test_commit -C client old1 &&
+ test_commit -C client old2 &&
+ test_commit -C client old3 &&
+ test_commit -C client old4 &&
+
+ # "c2" and "c1" are popped first, then "old4" to "old1". "old1" would
+ # normally be skipped, but is treated as a commit without a parent here
+ # and sent, because (due to clock skew) its only parent has already been
+ # popped off the priority queue.
+ test_config -C client fetch.negotiationalgorithm skipping &&
+ trace_fetch client "$(pwd)/server" &&
+ have_sent c2 c1 old4 old2 old1 &&
+ have_not_sent old3
+'
+
+test_expect_success 'do not send "have" with ancestors of commits that server ACKed' '
+ rm -rf server client trace &&
+ git init server &&
+ test_commit -C server to_fetch &&
+
+ git init client &&
+ for i in $(test_seq 8)
+ do
+ git -C client checkout --orphan b$i &&
+ test_commit -C client b$i.c0 || return 1
+ done &&
+ for j in $(test_seq 19)
+ do
+ for i in $(test_seq 8)
+ do
+ git -C client checkout b$i &&
+ test_commit -C client b$i.c$j || return 1
+ done
+ done &&
+
+ # Copy this branch over to the server and add a commit on it so that it
+ # is reachable but not advertised.
+ git -C server fetch --no-tags "$(pwd)/client" b1:refs/heads/b1 &&
+ git -C server checkout b1 &&
+ test_commit -C server commit-on-b1 &&
+
+ test_config -C client fetch.negotiationalgorithm skipping &&
+
+ # NEEDSWORK: The number of "have"s sent depends on whether the transport
+ # is stateful. If the overspecification of the result were reduced, this
+ # test could be used for both stateful and stateless transports.
+ (
+ # Force protocol v0, in which local transport is stateful (in
+ # protocol v2 it is stateless).
+ GIT_TEST_PROTOCOL_VERSION=0 &&
+ export GIT_TEST_PROTOCOL_VERSION &&
+ trace_fetch client "$(pwd)/server" to_fetch
+ ) &&
+ grep " fetch" trace &&
+
+ # fetch-pack sends 2 requests each containing 16 "have" lines before
+ # processing the first response. In these 2 requests, 4 commits from
+ # each branch are sent. Just check the first branch.
+ have_sent b1.c19 b1.c17 b1.c14 b1.c9 &&
+ have_not_sent b1.c18 b1.c16 b1.c15 b1.c13 b1.c12 b1.c11 b1.c10 &&
+
+ # While fetch-pack is processing the first response, it should read that
+ # the server ACKs b1.c19 and b1.c17.
+ grep "fetch< ACK $(git -C client rev-parse b1.c19) common" trace &&
+ grep "fetch< ACK $(git -C client rev-parse b1.c17) common" trace &&
+
+ # fetch-pack should thus not send any more commits in the b1 branch, but
+ # should still send the others (in this test, just check b2).
+ for i in $(test_seq 0 8)
+ do
+ have_not_sent b1.c$i || return 1
+ done &&
+ have_sent b2.c1 b2.c0
+'
+
+test_done
diff --git a/t/t5553-set-upstream.sh b/t/t5553-set-upstream.sh
new file mode 100755
index 0000000..4805016
--- /dev/null
+++ b/t/t5553-set-upstream.sh
@@ -0,0 +1,203 @@
+#!/bin/sh
+
+test_description='"git fetch/pull --set-upstream" basic tests.'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+check_config () {
+ printf "%s\n" "$2" "$3" >"expect.$1" &&
+ {
+ git config "branch.$1.remote" && git config "branch.$1.merge"
+ } >"actual.$1" &&
+ test_cmp "expect.$1" "actual.$1"
+}
+
+check_config_missing () {
+ test_expect_code 1 git config "branch.$1.remote" &&
+ test_expect_code 1 git config "branch.$1.merge"
+}
+
+clear_config () {
+ for branch in "$@"; do
+ test_might_fail git config --unset-all "branch.$branch.remote"
+ test_might_fail git config --unset-all "branch.$branch.merge"
+ done
+}
+
+ensure_fresh_upstream () {
+ rm -rf parent && git init --bare parent
+}
+
+test_expect_success 'setup bare parent fetch' '
+ ensure_fresh_upstream &&
+ git remote add upstream parent
+'
+
+test_expect_success 'setup commit on main and other fetch' '
+ test_commit one &&
+ git push upstream main &&
+ git checkout -b other &&
+ test_commit two &&
+ git push upstream other
+'
+
+# tests for fetch --set-upstream
+
+test_expect_success 'fetch --set-upstream does not set upstream w/o branch' '
+ clear_config main other &&
+ git checkout main &&
+ git fetch --set-upstream upstream &&
+ check_config_missing main &&
+ check_config_missing other
+'
+
+test_expect_success 'fetch --set-upstream upstream main sets branch main but not other' '
+ clear_config main other &&
+ git fetch --set-upstream upstream main &&
+ check_config main upstream refs/heads/main &&
+ check_config_missing other
+'
+
+test_expect_success 'fetch --set-upstream upstream other sets branch other' '
+ clear_config main other &&
+ git fetch --set-upstream upstream other &&
+ check_config main upstream refs/heads/other &&
+ check_config_missing other
+'
+
+test_expect_success 'fetch --set-upstream main:other does not set the branch other2' '
+ clear_config other2 &&
+ git fetch --set-upstream upstream main:other2 &&
+ check_config_missing other2
+'
+
+test_expect_success 'fetch --set-upstream http://nosuchdomain.example.com fails with invalid url' '
+ # main explicitly not cleared, we check that it is not touched from previous value
+ clear_config other other2 &&
+ test_must_fail git fetch --set-upstream http://nosuchdomain.example.com &&
+ check_config main upstream refs/heads/other &&
+ check_config_missing other &&
+ check_config_missing other2
+'
+
+test_expect_success 'fetch --set-upstream with valid URL sets upstream to URL' '
+ clear_config other other2 &&
+ url="file://$PWD" &&
+ git fetch --set-upstream "$url" &&
+ check_config main "$url" HEAD &&
+ check_config_missing other &&
+ check_config_missing other2
+'
+
+test_expect_success 'fetch --set-upstream with a detached HEAD' '
+ git checkout HEAD^0 &&
+ test_when_finished "git checkout -" &&
+ cat >expect <<-\EOF &&
+ warning: could not set upstream of HEAD to '"'"'main'"'"' from '"'"'upstream'"'"' when it does not point to any branch.
+ EOF
+ git fetch --set-upstream upstream main 2>actual.raw &&
+ grep ^warning: actual.raw >actual &&
+ test_cmp expect actual
+'
+
+# tests for pull --set-upstream
+
+test_expect_success 'setup bare parent pull' '
+ git remote rm upstream &&
+ ensure_fresh_upstream &&
+ git remote add upstream parent
+'
+
+test_expect_success 'setup commit on main and other pull' '
+ test_commit three &&
+ git push --tags upstream main &&
+ test_commit four &&
+ git push upstream other
+'
+
+test_expect_success 'pull --set-upstream upstream main sets branch main but not other' '
+ clear_config main other &&
+ git pull --no-rebase --set-upstream upstream main &&
+ check_config main upstream refs/heads/main &&
+ check_config_missing other
+'
+
+test_expect_success 'pull --set-upstream main:other2 does not set the branch other2' '
+ clear_config other2 &&
+ git pull --no-rebase --set-upstream upstream main:other2 &&
+ check_config_missing other2
+'
+
+test_expect_success 'pull --set-upstream upstream other sets branch main' '
+ clear_config main other &&
+ git pull --no-rebase --set-upstream upstream other &&
+ check_config main upstream refs/heads/other &&
+ check_config_missing other
+'
+
+test_expect_success 'pull --set-upstream upstream tag does not set the tag' '
+ clear_config three &&
+ git pull --no-rebase --tags --set-upstream upstream three &&
+ check_config_missing three
+'
+
+test_expect_success 'pull --set-upstream http://nosuchdomain.example.com fails with invalid url' '
+ # main explicitly not cleared, we check that it is not touched from previous value
+ clear_config other other2 three &&
+ test_must_fail git pull --set-upstream http://nosuchdomain.example.com &&
+ check_config main upstream refs/heads/other &&
+ check_config_missing other &&
+ check_config_missing other2 &&
+ check_config_missing three
+'
+
+test_expect_success 'pull --set-upstream upstream HEAD sets branch HEAD' '
+ clear_config main other &&
+ git pull --no-rebase --set-upstream upstream HEAD &&
+ check_config main upstream HEAD &&
+ git checkout other &&
+ git pull --no-rebase --set-upstream upstream HEAD &&
+ check_config other upstream HEAD
+'
+
+test_expect_success 'pull --set-upstream upstream with more than one branch does nothing' '
+ clear_config main three &&
+ git pull --no-rebase --set-upstream upstream main three &&
+ check_config_missing main &&
+ check_config_missing three
+'
+
+test_expect_success 'pull --set-upstream with valid URL sets upstream to URL' '
+ clear_config main other other2 &&
+ git checkout main &&
+ url="file://$PWD" &&
+ git pull --set-upstream "$url" &&
+ check_config main "$url" HEAD &&
+ check_config_missing other &&
+ check_config_missing other2
+'
+
+test_expect_success 'pull --set-upstream with valid URL and branch sets branch' '
+ clear_config main other other2 &&
+ git checkout main &&
+ url="file://$PWD" &&
+ git pull --set-upstream "$url" main &&
+ check_config main "$url" refs/heads/main &&
+ check_config_missing other &&
+ check_config_missing other2
+'
+
+test_expect_success 'pull --set-upstream with a detached HEAD' '
+ git checkout HEAD^0 &&
+ test_when_finished "git checkout -" &&
+ cat >expect <<-\EOF &&
+ warning: could not set upstream of HEAD to '"'"'main'"'"' from '"'"'upstream'"'"' when it does not point to any branch.
+ EOF
+ git pull --no-rebase --set-upstream upstream main 2>actual.raw &&
+ grep ^warning: actual.raw >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t5554-noop-fetch-negotiator.sh b/t/t5554-noop-fetch-negotiator.sh
new file mode 100755
index 0000000..06991e8
--- /dev/null
+++ b/t/t5554-noop-fetch-negotiator.sh
@@ -0,0 +1,24 @@
+#!/bin/sh
+
+test_description='test noop fetch negotiator'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'noop negotiator does not emit any "have"' '
+ rm -f trace &&
+
+ test_create_repo server &&
+ test_commit -C server to_fetch &&
+
+ test_create_repo client &&
+ test_commit -C client we_have &&
+
+ test_config -C client fetch.negotiationalgorithm noop &&
+ GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch "$(pwd)/server" &&
+
+ ! grep "fetch> have" trace &&
+ grep "fetch> done" trace
+'
+
+test_done
diff --git a/t/t5555-http-smart-common.sh b/t/t5555-http-smart-common.sh
new file mode 100755
index 0000000..b1cfe8b
--- /dev/null
+++ b/t/t5555-http-smart-common.sh
@@ -0,0 +1,162 @@
+#!/bin/sh
+
+test_description='test functionality common to smart fetch & push'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ test_commit --no-tag initial
+'
+
+test_expect_success 'git upload-pack --http-backend-info-refs and --advertise-refs are aliased' '
+ git upload-pack --http-backend-info-refs . >expected 2>err.expected &&
+ git upload-pack --advertise-refs . >actual 2>err.actual &&
+ test_cmp err.expected err.actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'git receive-pack --http-backend-info-refs and --advertise-refs are aliased' '
+ git receive-pack --http-backend-info-refs . >expected 2>err.expected &&
+ git receive-pack --advertise-refs . >actual 2>err.actual &&
+ test_cmp err.expected err.actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'git upload-pack --advertise-refs' '
+ cat >expect <<-EOF &&
+ $(git rev-parse HEAD) HEAD
+ $(git rev-parse HEAD) $(git symbolic-ref HEAD)
+ 0000
+ EOF
+
+ # We only care about GIT_PROTOCOL, not GIT_TEST_PROTOCOL_VERSION
+ sane_unset GIT_PROTOCOL &&
+ GIT_TEST_PROTOCOL_VERSION=2 \
+ git upload-pack --advertise-refs . >out 2>err &&
+
+ test-tool pkt-line unpack <out >actual &&
+ test_must_be_empty err &&
+ test_cmp actual expect &&
+
+ # The --advertise-refs alias works
+ git upload-pack --advertise-refs . >out 2>err &&
+
+ test-tool pkt-line unpack <out >actual &&
+ test_must_be_empty err &&
+ test_cmp actual expect
+'
+
+test_expect_success 'git upload-pack --advertise-refs: v0' '
+ # With no specified protocol
+ cat >expect <<-EOF &&
+ $(git rev-parse HEAD) HEAD
+ $(git rev-parse HEAD) $(git symbolic-ref HEAD)
+ 0000
+ EOF
+
+ git upload-pack --advertise-refs . >out 2>err &&
+ test-tool pkt-line unpack <out >actual &&
+ test_must_be_empty err &&
+ test_cmp actual expect &&
+
+ # With explicit v0
+ GIT_PROTOCOL=version=0 \
+ git upload-pack --advertise-refs . >out 2>err &&
+ test-tool pkt-line unpack <out >actual 2>err &&
+ test_must_be_empty err &&
+ test_cmp actual expect
+
+'
+
+test_expect_success 'git receive-pack --advertise-refs: v0' '
+ # With no specified protocol
+ cat >expect <<-EOF &&
+ $(git rev-parse HEAD) $(git symbolic-ref HEAD)
+ 0000
+ EOF
+
+ git receive-pack --advertise-refs . >out 2>err &&
+ test-tool pkt-line unpack <out >actual &&
+ test_must_be_empty err &&
+ test_cmp actual expect &&
+
+ # With explicit v0
+ GIT_PROTOCOL=version=0 \
+ git receive-pack --advertise-refs . >out 2>err &&
+ test-tool pkt-line unpack <out >actual 2>err &&
+ test_must_be_empty err &&
+ test_cmp actual expect
+
+'
+
+test_expect_success 'git upload-pack --advertise-refs: v1' '
+ # With no specified protocol
+ cat >expect <<-EOF &&
+ version 1
+ $(git rev-parse HEAD) HEAD
+ $(git rev-parse HEAD) $(git symbolic-ref HEAD)
+ 0000
+ EOF
+
+ GIT_PROTOCOL=version=1 \
+ git upload-pack --advertise-refs . >out &&
+
+ test-tool pkt-line unpack <out >actual 2>err &&
+ test_must_be_empty err &&
+ test_cmp actual expect
+'
+
+test_expect_success 'git receive-pack --advertise-refs: v1' '
+ # With no specified protocol
+ cat >expect <<-EOF &&
+ version 1
+ $(git rev-parse HEAD) $(git symbolic-ref HEAD)
+ 0000
+ EOF
+
+ GIT_PROTOCOL=version=1 \
+ git receive-pack --advertise-refs . >out &&
+
+ test-tool pkt-line unpack <out >actual 2>err &&
+ test_must_be_empty err &&
+ test_cmp actual expect
+'
+
+test_expect_success 'git upload-pack --advertise-refs: v2' '
+ cat >expect <<-EOF &&
+ version 2
+ agent=FAKE
+ ls-refs=unborn
+ fetch=shallow wait-for-done
+ server-option
+ object-format=$(test_oid algo)
+ object-info
+ 0000
+ EOF
+
+ GIT_PROTOCOL=version=2 \
+ GIT_USER_AGENT=FAKE \
+ git upload-pack --advertise-refs . >out 2>err &&
+
+ test-tool pkt-line unpack <out >actual &&
+ test_must_be_empty err &&
+ test_cmp actual expect
+'
+
+test_expect_success 'git receive-pack --advertise-refs: v2' '
+ # There is no v2 yet for receive-pack, implicit v0
+ cat >expect <<-EOF &&
+ $(git rev-parse HEAD) $(git symbolic-ref HEAD)
+ 0000
+ EOF
+
+ GIT_PROTOCOL=version=2 \
+ git receive-pack --advertise-refs . >out 2>err &&
+
+ test-tool pkt-line unpack <out >actual &&
+ test_must_be_empty err &&
+ test_cmp actual expect
+'
+
+test_done
diff --git a/t/t5557-http-get.sh b/t/t5557-http-get.sh
new file mode 100755
index 0000000..76a4bbd
--- /dev/null
+++ b/t/t5557-http-get.sh
@@ -0,0 +1,39 @@
+#!/bin/sh
+
+test_description='test downloading a file by URL'
+
+TEST_PASSES_SANITIZE_LEAK=true
+
+. ./test-lib.sh
+
+. "$TEST_DIRECTORY"/lib-httpd.sh
+start_httpd
+
+test_expect_success 'get by URL: 404' '
+ test_when_finished "rm -f file.temp" &&
+ url="$HTTPD_URL/none.txt" &&
+ cat >input <<-EOF &&
+ capabilities
+ get $url file1
+ EOF
+
+ test_must_fail git remote-http $url <input 2>err &&
+ test_path_is_missing file1 &&
+ grep "failed to download file at URL" err
+'
+
+test_expect_success 'get by URL: 200' '
+ echo data >"$HTTPD_DOCUMENT_ROOT_PATH/exists.txt" &&
+
+ url="$HTTPD_URL/exists.txt" &&
+ cat >input <<-EOF &&
+ capabilities
+ get $url file2
+
+ EOF
+
+ git remote-http $url <input &&
+ test_cmp "$HTTPD_DOCUMENT_ROOT_PATH/exists.txt" file2
+'
+
+test_done
diff --git a/t/t5558-clone-bundle-uri.sh b/t/t5558-clone-bundle-uri.sh
new file mode 100755
index 0000000..996a08e
--- /dev/null
+++ b/t/t5558-clone-bundle-uri.sh
@@ -0,0 +1,1058 @@
+#!/bin/sh
+
+test_description='test fetching bundles with --bundle-uri'
+
+. ./test-lib.sh
+
+test_expect_success 'fail to clone from non-existent file' '
+ test_when_finished rm -rf test &&
+ git clone --bundle-uri="$(pwd)/does-not-exist" . test 2>err &&
+ grep "failed to download bundle from URI" err
+'
+
+test_expect_success 'fail to clone from non-bundle file' '
+ test_when_finished rm -rf test &&
+ echo bogus >bogus &&
+ git clone --bundle-uri="$(pwd)/bogus" . test 2>err &&
+ grep "is not a bundle" err
+'
+
+test_expect_success 'create bundle' '
+ git init clone-from &&
+ git -C clone-from checkout -b topic &&
+ test_commit -C clone-from A &&
+ test_commit -C clone-from B &&
+ git -C clone-from bundle create B.bundle topic
+'
+
+test_expect_success 'clone with path bundle' '
+ git clone --bundle-uri="clone-from/B.bundle" \
+ clone-from clone-path &&
+ git -C clone-path rev-parse refs/bundles/topic >actual &&
+ git -C clone-from rev-parse topic >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'clone with file:// bundle' '
+ git clone --bundle-uri="file://$(pwd)/clone-from/B.bundle" \
+ clone-from clone-file &&
+ git -C clone-file rev-parse refs/bundles/topic >actual &&
+ git -C clone-from rev-parse topic >expect &&
+ test_cmp expect actual
+'
+
+# To get interesting tests for bundle lists, we need to construct a
+# somewhat-interesting commit history.
+#
+# ---------------- bundle-4
+#
+# 4
+# / \
+# ----|---|------- bundle-3
+# | |
+# | 3
+# | |
+# ----|---|------- bundle-2
+# | |
+# 2 |
+# | |
+# ----|---|------- bundle-1
+# \ /
+# 1
+# |
+# (previous commits)
+test_expect_success 'construct incremental bundle list' '
+ (
+ cd clone-from &&
+ git checkout -b base &&
+ test_commit 1 &&
+ git checkout -b left &&
+ test_commit 2 &&
+ git checkout -b right base &&
+ test_commit 3 &&
+ git checkout -b merge left &&
+ git merge right -m "4" &&
+
+ git bundle create bundle-1.bundle base &&
+ git bundle create bundle-2.bundle base..left &&
+ git bundle create bundle-3.bundle base..right &&
+ git bundle create bundle-4.bundle merge --not left right
+ )
+'
+
+test_expect_success 'clone bundle list (file, no heuristic)' '
+ cat >bundle-list <<-EOF &&
+ [bundle]
+ version = 1
+ mode = all
+
+ [bundle "bundle-1"]
+ uri = file://$(pwd)/clone-from/bundle-1.bundle
+
+ [bundle "bundle-2"]
+ uri = file://$(pwd)/clone-from/bundle-2.bundle
+
+ [bundle "bundle-3"]
+ uri = file://$(pwd)/clone-from/bundle-3.bundle
+
+ [bundle "bundle-4"]
+ uri = file://$(pwd)/clone-from/bundle-4.bundle
+ EOF
+
+ git clone --bundle-uri="file://$(pwd)/bundle-list" \
+ clone-from clone-list-file 2>err &&
+ ! grep "Repository lacks these prerequisite commits" err &&
+
+ git -C clone-from for-each-ref --format="%(objectname)" >oids &&
+ git -C clone-list-file cat-file --batch-check <oids &&
+
+ git -C clone-list-file for-each-ref --format="%(refname)" >refs &&
+ grep "refs/bundles/" refs >actual &&
+ cat >expect <<-\EOF &&
+ refs/bundles/base
+ refs/bundles/left
+ refs/bundles/merge
+ refs/bundles/right
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'clone bundle list (file, all mode, some failures)' '
+ cat >bundle-list <<-EOF &&
+ [bundle]
+ version = 1
+ mode = all
+
+ # Does not exist. Should be skipped.
+ [bundle "bundle-0"]
+ uri = file://$(pwd)/clone-from/bundle-0.bundle
+
+ [bundle "bundle-1"]
+ uri = file://$(pwd)/clone-from/bundle-1.bundle
+
+ [bundle "bundle-2"]
+ uri = file://$(pwd)/clone-from/bundle-2.bundle
+
+ # No bundle-3 means bundle-4 will not apply.
+
+ [bundle "bundle-4"]
+ uri = file://$(pwd)/clone-from/bundle-4.bundle
+
+ # Does not exist. Should be skipped.
+ [bundle "bundle-5"]
+ uri = file://$(pwd)/clone-from/bundle-5.bundle
+ EOF
+
+ GIT_TRACE2_PERF=1 \
+ git clone --bundle-uri="file://$(pwd)/bundle-list" \
+ clone-from clone-all-some 2>err &&
+ ! grep "Repository lacks these prerequisite commits" err &&
+ ! grep "fatal" err &&
+ grep "warning: failed to download bundle from URI" err &&
+
+ git -C clone-from for-each-ref --format="%(objectname)" >oids &&
+ git -C clone-all-some cat-file --batch-check <oids &&
+
+ git -C clone-all-some for-each-ref --format="%(refname)" >refs &&
+ grep "refs/bundles/" refs >actual &&
+ cat >expect <<-\EOF &&
+ refs/bundles/base
+ refs/bundles/left
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'clone bundle list (file, all mode, all failures)' '
+ cat >bundle-list <<-EOF &&
+ [bundle]
+ version = 1
+ mode = all
+
+ # Does not exist. Should be skipped.
+ [bundle "bundle-0"]
+ uri = file://$(pwd)/clone-from/bundle-0.bundle
+
+ # Does not exist. Should be skipped.
+ [bundle "bundle-5"]
+ uri = file://$(pwd)/clone-from/bundle-5.bundle
+ EOF
+
+ git clone --bundle-uri="file://$(pwd)/bundle-list" \
+ clone-from clone-all-fail 2>err &&
+ ! grep "Repository lacks these prerequisite commits" err &&
+ ! grep "fatal" err &&
+ grep "warning: failed to download bundle from URI" err &&
+
+ git -C clone-from for-each-ref --format="%(objectname)" >oids &&
+ git -C clone-all-fail cat-file --batch-check <oids &&
+
+ git -C clone-all-fail for-each-ref --format="%(refname)" >refs &&
+ ! grep "refs/bundles/" refs
+'
+
+test_expect_success 'clone bundle list (file, any mode)' '
+ cat >bundle-list <<-EOF &&
+ [bundle]
+ version = 1
+ mode = any
+
+ # Does not exist. Should be skipped.
+ [bundle "bundle-0"]
+ uri = file://$(pwd)/clone-from/bundle-0.bundle
+
+ [bundle "bundle-1"]
+ uri = file://$(pwd)/clone-from/bundle-1.bundle
+
+ # Does not exist. Should be skipped.
+ [bundle "bundle-5"]
+ uri = file://$(pwd)/clone-from/bundle-5.bundle
+ EOF
+
+ git clone --bundle-uri="file://$(pwd)/bundle-list" \
+ clone-from clone-any-file 2>err &&
+ ! grep "Repository lacks these prerequisite commits" err &&
+
+ git -C clone-from for-each-ref --format="%(objectname)" >oids &&
+ git -C clone-any-file cat-file --batch-check <oids &&
+
+ git -C clone-any-file for-each-ref --format="%(refname)" >refs &&
+ grep "refs/bundles/" refs >actual &&
+ cat >expect <<-\EOF &&
+ refs/bundles/base
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'clone bundle list (file, any mode, all failures)' '
+ cat >bundle-list <<-EOF &&
+ [bundle]
+ version = 1
+ mode = any
+
+ # Does not exist. Should be skipped.
+ [bundle "bundle-0"]
+ uri = $HTTPD_URL/bundle-0.bundle
+
+ # Does not exist. Should be skipped.
+ [bundle "bundle-5"]
+ uri = $HTTPD_URL/bundle-5.bundle
+ EOF
+
+ git clone --bundle-uri="file://$(pwd)/bundle-list" \
+ clone-from clone-any-fail 2>err &&
+ ! grep "fatal" err &&
+ grep "warning: failed to download bundle from URI" err &&
+
+ git -C clone-from for-each-ref --format="%(objectname)" >oids &&
+ git -C clone-any-fail cat-file --batch-check <oids &&
+
+ git -C clone-any-fail for-each-ref --format="%(refname)" >refs &&
+ ! grep "refs/bundles/" refs
+'
+
+#########################################################################
+# HTTP tests begin here
+
+. "$TEST_DIRECTORY"/lib-httpd.sh
+start_httpd
+
+test_expect_success 'fail to fetch from non-existent HTTP URL' '
+ test_when_finished rm -rf test &&
+ git clone --bundle-uri="$HTTPD_URL/does-not-exist" . test 2>err &&
+ grep "failed to download bundle from URI" err
+'
+
+test_expect_success 'fail to fetch from non-bundle HTTP URL' '
+ test_when_finished rm -rf test &&
+ echo bogus >"$HTTPD_DOCUMENT_ROOT_PATH/bogus" &&
+ git clone --bundle-uri="$HTTPD_URL/bogus" . test 2>err &&
+ grep "is not a bundle" err
+'
+
+test_expect_success 'clone HTTP bundle' '
+ cp clone-from/B.bundle "$HTTPD_DOCUMENT_ROOT_PATH/B.bundle" &&
+
+ git clone --no-local --mirror clone-from \
+ "$HTTPD_DOCUMENT_ROOT_PATH/fetch.git" &&
+
+ git clone --bundle-uri="$HTTPD_URL/B.bundle" \
+ "$HTTPD_URL/smart/fetch.git" clone-http &&
+ git -C clone-http rev-parse refs/bundles/topic >actual &&
+ git -C clone-from rev-parse topic >expect &&
+ test_cmp expect actual &&
+
+ test_config -C clone-http log.excludedecoration refs/bundle/
+'
+
+test_expect_success 'clone bundle list (HTTP, no heuristic)' '
+ test_when_finished rm -f trace*.txt &&
+
+ cp clone-from/bundle-*.bundle "$HTTPD_DOCUMENT_ROOT_PATH/" &&
+ cat >"$HTTPD_DOCUMENT_ROOT_PATH/bundle-list" <<-EOF &&
+ [bundle]
+ version = 1
+ mode = all
+
+ [bundle "bundle-1"]
+ uri = $HTTPD_URL/bundle-1.bundle
+
+ [bundle "bundle-2"]
+ uri = $HTTPD_URL/bundle-2.bundle
+
+ [bundle "bundle-3"]
+ uri = $HTTPD_URL/bundle-3.bundle
+
+ [bundle "bundle-4"]
+ uri = $HTTPD_URL/bundle-4.bundle
+ EOF
+
+ GIT_TRACE2_EVENT="$(pwd)/trace-clone.txt" \
+ git clone --bundle-uri="$HTTPD_URL/bundle-list" \
+ clone-from clone-list-http 2>err &&
+ ! grep "Repository lacks these prerequisite commits" err &&
+
+ git -C clone-from for-each-ref --format="%(objectname)" >oids &&
+ git -C clone-list-http cat-file --batch-check <oids &&
+
+ cat >expect <<-EOF &&
+ $HTTPD_URL/bundle-1.bundle
+ $HTTPD_URL/bundle-2.bundle
+ $HTTPD_URL/bundle-3.bundle
+ $HTTPD_URL/bundle-4.bundle
+ $HTTPD_URL/bundle-list
+ EOF
+
+ # Sort the list, since the order is not well-defined
+ # without a heuristic.
+ test_remote_https_urls <trace-clone.txt | sort >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'clone bundle list (HTTP, any mode)' '
+ cp clone-from/bundle-*.bundle "$HTTPD_DOCUMENT_ROOT_PATH/" &&
+ cat >"$HTTPD_DOCUMENT_ROOT_PATH/bundle-list" <<-EOF &&
+ [bundle]
+ version = 1
+ mode = any
+
+ # Does not exist. Should be skipped.
+ [bundle "bundle-0"]
+ uri = $HTTPD_URL/bundle-0.bundle
+
+ [bundle "bundle-1"]
+ uri = $HTTPD_URL/bundle-1.bundle
+
+ # Does not exist. Should be skipped.
+ [bundle "bundle-5"]
+ uri = $HTTPD_URL/bundle-5.bundle
+ EOF
+
+ git clone --bundle-uri="$HTTPD_URL/bundle-list" \
+ clone-from clone-any-http 2>err &&
+ ! grep "fatal" err &&
+ grep "warning: failed to download bundle from URI" err &&
+
+ git -C clone-from for-each-ref --format="%(objectname)" >oids &&
+ git -C clone-any-http cat-file --batch-check <oids &&
+
+ git -C clone-list-file for-each-ref --format="%(refname)" >refs &&
+ grep "refs/bundles/" refs >actual &&
+ cat >expect <<-\EOF &&
+ refs/bundles/base
+ refs/bundles/left
+ refs/bundles/merge
+ refs/bundles/right
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'clone bundle list (http, creationToken)' '
+ test_when_finished rm -f trace*.txt &&
+
+ cp clone-from/bundle-*.bundle "$HTTPD_DOCUMENT_ROOT_PATH/" &&
+ cat >"$HTTPD_DOCUMENT_ROOT_PATH/bundle-list" <<-EOF &&
+ [bundle]
+ version = 1
+ mode = all
+ heuristic = creationToken
+
+ [bundle "bundle-1"]
+ uri = bundle-1.bundle
+ creationToken = 1
+
+ [bundle "bundle-2"]
+ uri = bundle-2.bundle
+ creationToken = 2
+
+ [bundle "bundle-3"]
+ uri = bundle-3.bundle
+ creationToken = 3
+
+ [bundle "bundle-4"]
+ uri = bundle-4.bundle
+ creationToken = 4
+ EOF
+
+ GIT_TRACE2_EVENT="$(pwd)/trace-clone.txt" git \
+ clone --bundle-uri="$HTTPD_URL/bundle-list" \
+ "$HTTPD_URL/smart/fetch.git" clone-list-http-2 &&
+
+ git -C clone-from for-each-ref --format="%(objectname)" >oids &&
+ git -C clone-list-http-2 cat-file --batch-check <oids &&
+
+ cat >expect <<-EOF &&
+ $HTTPD_URL/bundle-list
+ $HTTPD_URL/bundle-4.bundle
+ $HTTPD_URL/bundle-3.bundle
+ $HTTPD_URL/bundle-2.bundle
+ $HTTPD_URL/bundle-1.bundle
+ EOF
+
+ test_remote_https_urls <trace-clone.txt >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'clone incomplete bundle list (http, creationToken)' '
+ test_when_finished rm -f trace*.txt &&
+
+ cp clone-from/bundle-*.bundle "$HTTPD_DOCUMENT_ROOT_PATH/" &&
+ cat >"$HTTPD_DOCUMENT_ROOT_PATH/bundle-list" <<-EOF &&
+ [bundle]
+ version = 1
+ mode = all
+ heuristic = creationToken
+
+ [bundle "bundle-1"]
+ uri = bundle-1.bundle
+ creationToken = 1
+ EOF
+
+ GIT_TRACE2_EVENT=$(pwd)/trace-clone.txt \
+ git clone --bundle-uri="$HTTPD_URL/bundle-list" \
+ --single-branch --branch=base --no-tags \
+ "$HTTPD_URL/smart/fetch.git" clone-token-http &&
+
+ test_cmp_config -C clone-token-http "$HTTPD_URL/bundle-list" fetch.bundleuri &&
+ test_cmp_config -C clone-token-http 1 fetch.bundlecreationtoken &&
+
+ cat >expect <<-EOF &&
+ $HTTPD_URL/bundle-list
+ $HTTPD_URL/bundle-1.bundle
+ EOF
+
+ test_remote_https_urls <trace-clone.txt >actual &&
+ test_cmp expect actual &&
+
+ # We now have only one bundle ref.
+ git -C clone-token-http for-each-ref --format="%(refname)" "refs/bundles/*" >refs &&
+ cat >expect <<-\EOF &&
+ refs/bundles/base
+ EOF
+ test_cmp expect refs &&
+
+ # Add remaining bundles, exercising the "deepening" strategy
+ # for downloading via the creationToken heurisitc.
+ cat >>"$HTTPD_DOCUMENT_ROOT_PATH/bundle-list" <<-EOF &&
+ [bundle "bundle-2"]
+ uri = bundle-2.bundle
+ creationToken = 2
+
+ [bundle "bundle-3"]
+ uri = bundle-3.bundle
+ creationToken = 3
+
+ [bundle "bundle-4"]
+ uri = bundle-4.bundle
+ creationToken = 4
+ EOF
+
+ GIT_TRACE2_EVENT="$(pwd)/trace1.txt" \
+ git -C clone-token-http fetch origin --no-tags \
+ refs/heads/merge:refs/heads/merge &&
+ test_cmp_config -C clone-token-http 4 fetch.bundlecreationtoken &&
+
+ cat >expect <<-EOF &&
+ $HTTPD_URL/bundle-list
+ $HTTPD_URL/bundle-4.bundle
+ $HTTPD_URL/bundle-3.bundle
+ $HTTPD_URL/bundle-2.bundle
+ EOF
+
+ test_remote_https_urls <trace1.txt >actual &&
+ test_cmp expect actual &&
+
+ # We now have all bundle refs.
+ git -C clone-token-http for-each-ref --format="%(refname)" "refs/bundles/*" >refs &&
+
+ cat >expect <<-\EOF &&
+ refs/bundles/base
+ refs/bundles/left
+ refs/bundles/merge
+ refs/bundles/right
+ EOF
+ test_cmp expect refs
+'
+
+test_expect_success 'http clone with bundle.heuristic creates fetch.bundleURI' '
+ test_when_finished rm -rf fetch-http-4 trace*.txt &&
+
+ cat >"$HTTPD_DOCUMENT_ROOT_PATH/bundle-list" <<-EOF &&
+ [bundle]
+ version = 1
+ mode = all
+ heuristic = creationToken
+
+ [bundle "bundle-1"]
+ uri = bundle-1.bundle
+ creationToken = 1
+ EOF
+
+ GIT_TRACE2_EVENT="$(pwd)/trace-clone.txt" \
+ git clone --single-branch --branch=base \
+ --bundle-uri="$HTTPD_URL/bundle-list" \
+ "$HTTPD_URL/smart/fetch.git" fetch-http-4 &&
+
+ test_cmp_config -C fetch-http-4 "$HTTPD_URL/bundle-list" fetch.bundleuri &&
+ test_cmp_config -C fetch-http-4 1 fetch.bundlecreationtoken &&
+
+ cat >expect <<-EOF &&
+ $HTTPD_URL/bundle-list
+ $HTTPD_URL/bundle-1.bundle
+ EOF
+
+ test_remote_https_urls <trace-clone.txt >actual &&
+ test_cmp expect actual &&
+
+ # only received base ref from bundle-1
+ git -C fetch-http-4 for-each-ref --format="%(refname)" "refs/bundles/*" >refs &&
+ cat >expect <<-\EOF &&
+ refs/bundles/base
+ EOF
+ test_cmp expect refs &&
+
+ cat >>"$HTTPD_DOCUMENT_ROOT_PATH/bundle-list" <<-EOF &&
+ [bundle "bundle-2"]
+ uri = bundle-2.bundle
+ creationToken = 2
+ EOF
+
+ # Fetch the objects for bundle-2 _and_ bundle-3.
+ GIT_TRACE2_EVENT="$(pwd)/trace1.txt" \
+ git -C fetch-http-4 fetch origin --no-tags \
+ refs/heads/left:refs/heads/left \
+ refs/heads/right:refs/heads/right &&
+ test_cmp_config -C fetch-http-4 2 fetch.bundlecreationtoken &&
+
+ cat >expect <<-EOF &&
+ $HTTPD_URL/bundle-list
+ $HTTPD_URL/bundle-2.bundle
+ EOF
+
+ test_remote_https_urls <trace1.txt >actual &&
+ test_cmp expect actual &&
+
+ # received left from bundle-2
+ git -C fetch-http-4 for-each-ref --format="%(refname)" "refs/bundles/*" >refs &&
+ cat >expect <<-\EOF &&
+ refs/bundles/base
+ refs/bundles/left
+ EOF
+ test_cmp expect refs &&
+
+ # No-op fetch
+ GIT_TRACE2_EVENT="$(pwd)/trace1b.txt" \
+ git -C fetch-http-4 fetch origin --no-tags \
+ refs/heads/left:refs/heads/left \
+ refs/heads/right:refs/heads/right &&
+
+ cat >expect <<-EOF &&
+ $HTTPD_URL/bundle-list
+ EOF
+ test_remote_https_urls <trace1b.txt >actual &&
+ test_cmp expect actual &&
+
+ cat >>"$HTTPD_DOCUMENT_ROOT_PATH/bundle-list" <<-EOF &&
+ [bundle "bundle-3"]
+ uri = bundle-3.bundle
+ creationToken = 3
+
+ [bundle "bundle-4"]
+ uri = bundle-4.bundle
+ creationToken = 4
+ EOF
+
+ # This fetch should skip bundle-3.bundle, since its objects are
+ # already local (we have the requisite commits for bundle-4.bundle).
+ GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
+ git -C fetch-http-4 fetch origin --no-tags \
+ refs/heads/merge:refs/heads/merge &&
+ test_cmp_config -C fetch-http-4 4 fetch.bundlecreationtoken &&
+
+ cat >expect <<-EOF &&
+ $HTTPD_URL/bundle-list
+ $HTTPD_URL/bundle-4.bundle
+ EOF
+
+ test_remote_https_urls <trace2.txt >actual &&
+ test_cmp expect actual &&
+
+ # received merge ref from bundle-4, but right is missing
+ # because we did not download bundle-3.
+ git -C fetch-http-4 for-each-ref --format="%(refname)" "refs/bundles/*" >refs &&
+
+ cat >expect <<-\EOF &&
+ refs/bundles/base
+ refs/bundles/left
+ refs/bundles/merge
+ EOF
+ test_cmp expect refs &&
+
+ # No-op fetch
+ GIT_TRACE2_EVENT="$(pwd)/trace2b.txt" \
+ git -C fetch-http-4 fetch origin &&
+
+ cat >expect <<-EOF &&
+ $HTTPD_URL/bundle-list
+ EOF
+ test_remote_https_urls <trace2b.txt >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'creationToken heuristic with failed downloads (clone)' '
+ test_when_finished rm -rf download-* trace*.txt &&
+
+ # Case 1: base bundle does not exist, nothing can unbundle
+ cat >"$HTTPD_DOCUMENT_ROOT_PATH/bundle-list" <<-EOF &&
+ [bundle]
+ version = 1
+ mode = all
+ heuristic = creationToken
+
+ [bundle "bundle-1"]
+ uri = fake.bundle
+ creationToken = 1
+
+ [bundle "bundle-2"]
+ uri = bundle-2.bundle
+ creationToken = 2
+
+ [bundle "bundle-3"]
+ uri = bundle-3.bundle
+ creationToken = 3
+
+ [bundle "bundle-4"]
+ uri = bundle-4.bundle
+ creationToken = 4
+ EOF
+
+ GIT_TRACE2_EVENT="$(pwd)/trace-clone-1.txt" \
+ git clone --single-branch --branch=base \
+ --bundle-uri="$HTTPD_URL/bundle-list" \
+ "$HTTPD_URL/smart/fetch.git" download-1 &&
+
+ # Bundle failure does not set these configs.
+ test_must_fail git -C download-1 config fetch.bundleuri &&
+ test_must_fail git -C download-1 config fetch.bundlecreationtoken &&
+
+ cat >expect <<-EOF &&
+ $HTTPD_URL/bundle-list
+ $HTTPD_URL/bundle-4.bundle
+ $HTTPD_URL/bundle-3.bundle
+ $HTTPD_URL/bundle-2.bundle
+ $HTTPD_URL/fake.bundle
+ EOF
+ test_remote_https_urls <trace-clone-1.txt >actual &&
+ test_cmp expect actual &&
+
+ # All bundles failed to unbundle
+ git -C download-1 for-each-ref --format="%(refname)" "refs/bundles/*" >refs &&
+ test_must_be_empty refs &&
+
+ # Case 2: middle bundle does not exist, only two bundles can unbundle
+ cat >"$HTTPD_DOCUMENT_ROOT_PATH/bundle-list" <<-EOF &&
+ [bundle]
+ version = 1
+ mode = all
+ heuristic = creationToken
+
+ [bundle "bundle-1"]
+ uri = bundle-1.bundle
+ creationToken = 1
+
+ [bundle "bundle-2"]
+ uri = fake.bundle
+ creationToken = 2
+
+ [bundle "bundle-3"]
+ uri = bundle-3.bundle
+ creationToken = 3
+
+ [bundle "bundle-4"]
+ uri = bundle-4.bundle
+ creationToken = 4
+ EOF
+
+ GIT_TRACE2_EVENT="$(pwd)/trace-clone-2.txt" \
+ git clone --single-branch --branch=base \
+ --bundle-uri="$HTTPD_URL/bundle-list" \
+ "$HTTPD_URL/smart/fetch.git" download-2 &&
+
+ # Bundle failure does not set these configs.
+ test_must_fail git -C download-2 config fetch.bundleuri &&
+ test_must_fail git -C download-2 config fetch.bundlecreationtoken &&
+
+ cat >expect <<-EOF &&
+ $HTTPD_URL/bundle-list
+ $HTTPD_URL/bundle-4.bundle
+ $HTTPD_URL/bundle-3.bundle
+ $HTTPD_URL/fake.bundle
+ $HTTPD_URL/bundle-1.bundle
+ EOF
+ test_remote_https_urls <trace-clone-2.txt >actual &&
+ test_cmp expect actual &&
+
+ # bundle-1 and bundle-3 could unbundle, but bundle-4 could not
+ git -C download-2 for-each-ref --format="%(refname)" "refs/bundles/*" >refs &&
+ cat >expect <<-EOF &&
+ refs/bundles/base
+ refs/bundles/right
+ EOF
+ test_cmp expect refs &&
+
+ # Case 3: top bundle does not exist, rest unbundle fine.
+ cat >"$HTTPD_DOCUMENT_ROOT_PATH/bundle-list" <<-EOF &&
+ [bundle]
+ version = 1
+ mode = all
+ heuristic = creationToken
+
+ [bundle "bundle-1"]
+ uri = bundle-1.bundle
+ creationToken = 1
+
+ [bundle "bundle-2"]
+ uri = bundle-2.bundle
+ creationToken = 2
+
+ [bundle "bundle-3"]
+ uri = bundle-3.bundle
+ creationToken = 3
+
+ [bundle "bundle-4"]
+ uri = fake.bundle
+ creationToken = 4
+ EOF
+
+ GIT_TRACE2_EVENT="$(pwd)/trace-clone-3.txt" \
+ git clone --single-branch --branch=base \
+ --bundle-uri="$HTTPD_URL/bundle-list" \
+ "$HTTPD_URL/smart/fetch.git" download-3 &&
+
+ # As long as we have continguous successful downloads,
+ # we _do_ set these configs.
+ test_cmp_config -C download-3 "$HTTPD_URL/bundle-list" fetch.bundleuri &&
+ test_cmp_config -C download-3 3 fetch.bundlecreationtoken &&
+
+ cat >expect <<-EOF &&
+ $HTTPD_URL/bundle-list
+ $HTTPD_URL/fake.bundle
+ $HTTPD_URL/bundle-3.bundle
+ $HTTPD_URL/bundle-2.bundle
+ $HTTPD_URL/bundle-1.bundle
+ EOF
+ test_remote_https_urls <trace-clone-3.txt >actual &&
+ test_cmp expect actual &&
+
+ # fake.bundle did not unbundle, but the others did.
+ git -C download-3 for-each-ref --format="%(refname)" "refs/bundles/*" >refs &&
+ cat >expect <<-EOF &&
+ refs/bundles/base
+ refs/bundles/left
+ refs/bundles/right
+ EOF
+ test_cmp expect refs
+'
+
+# Expand the bundle list to include other interesting shapes, specifically
+# interesting for use when fetching from a previous state.
+#
+# ---------------- bundle-7
+# 7
+# _/|\_
+# ---/--|--\------ bundle-6
+# 5 | 6
+# --|---|---|----- bundle-4
+# | 4 |
+# | / \ /
+# --|-|---|/------ bundle-3 (the client will be caught up to this point.)
+# \ | 3
+# ---\|---|------- bundle-2
+# 2 |
+# ----|---|------- bundle-1
+# \ /
+# 1
+# |
+# (previous commits)
+test_expect_success 'expand incremental bundle list' '
+ (
+ cd clone-from &&
+ git checkout -b lefter left &&
+ test_commit 5 &&
+ git checkout -b righter right &&
+ test_commit 6 &&
+ git checkout -b top lefter &&
+ git merge -m "7" merge righter &&
+
+ git bundle create bundle-6.bundle lefter righter --not left right &&
+ git bundle create bundle-7.bundle top --not lefter merge righter &&
+
+ cp bundle-*.bundle "$HTTPD_DOCUMENT_ROOT_PATH/"
+ ) &&
+ git -C "$HTTPD_DOCUMENT_ROOT_PATH/fetch.git" fetch origin +refs/heads/*:refs/heads/*
+'
+
+test_expect_success 'creationToken heuristic with failed downloads (fetch)' '
+ test_when_finished rm -rf download-* trace*.txt &&
+
+ cat >"$HTTPD_DOCUMENT_ROOT_PATH/bundle-list" <<-EOF &&
+ [bundle]
+ version = 1
+ mode = all
+ heuristic = creationToken
+
+ [bundle "bundle-1"]
+ uri = bundle-1.bundle
+ creationToken = 1
+
+ [bundle "bundle-2"]
+ uri = bundle-2.bundle
+ creationToken = 2
+
+ [bundle "bundle-3"]
+ uri = bundle-3.bundle
+ creationToken = 3
+ EOF
+
+ git clone --single-branch --branch=left \
+ --bundle-uri="$HTTPD_URL/bundle-list" \
+ "$HTTPD_URL/smart/fetch.git" fetch-base &&
+ test_cmp_config -C fetch-base "$HTTPD_URL/bundle-list" fetch.bundleURI &&
+ test_cmp_config -C fetch-base 3 fetch.bundleCreationToken &&
+
+ # Case 1: all bundles exist: successful unbundling of all bundles
+ cat >"$HTTPD_DOCUMENT_ROOT_PATH/bundle-list" <<-EOF &&
+ [bundle]
+ version = 1
+ mode = all
+ heuristic = creationToken
+
+ [bundle "bundle-1"]
+ uri = bundle-1.bundle
+ creationToken = 1
+
+ [bundle "bundle-2"]
+ uri = bundle-2.bundle
+ creationToken = 2
+
+ [bundle "bundle-3"]
+ uri = bundle-3.bundle
+ creationToken = 3
+
+ [bundle "bundle-4"]
+ uri = bundle-4.bundle
+ creationToken = 4
+
+ [bundle "bundle-6"]
+ uri = bundle-6.bundle
+ creationToken = 6
+
+ [bundle "bundle-7"]
+ uri = bundle-7.bundle
+ creationToken = 7
+ EOF
+
+ cp -r fetch-base fetch-1 &&
+ GIT_TRACE2_EVENT="$(pwd)/trace-fetch-1.txt" \
+ git -C fetch-1 fetch origin &&
+ test_cmp_config -C fetch-1 7 fetch.bundlecreationtoken &&
+
+ cat >expect <<-EOF &&
+ $HTTPD_URL/bundle-list
+ $HTTPD_URL/bundle-7.bundle
+ $HTTPD_URL/bundle-6.bundle
+ $HTTPD_URL/bundle-4.bundle
+ EOF
+ test_remote_https_urls <trace-fetch-1.txt >actual &&
+ test_cmp expect actual &&
+
+ # Check which bundles have unbundled by refs
+ git -C fetch-1 for-each-ref --format="%(refname)" "refs/bundles/*" >refs &&
+ cat >expect <<-EOF &&
+ refs/bundles/base
+ refs/bundles/left
+ refs/bundles/lefter
+ refs/bundles/merge
+ refs/bundles/right
+ refs/bundles/righter
+ refs/bundles/top
+ EOF
+ test_cmp expect refs &&
+
+ # Case 2: middle bundle does not exist, only bundle-4 can unbundle
+ cat >"$HTTPD_DOCUMENT_ROOT_PATH/bundle-list" <<-EOF &&
+ [bundle]
+ version = 1
+ mode = all
+ heuristic = creationToken
+
+ [bundle "bundle-1"]
+ uri = bundle-1.bundle
+ creationToken = 1
+
+ [bundle "bundle-2"]
+ uri = bundle-2.bundle
+ creationToken = 2
+
+ [bundle "bundle-3"]
+ uri = bundle-3.bundle
+ creationToken = 3
+
+ [bundle "bundle-4"]
+ uri = bundle-4.bundle
+ creationToken = 4
+
+ [bundle "bundle-6"]
+ uri = fake.bundle
+ creationToken = 6
+
+ [bundle "bundle-7"]
+ uri = bundle-7.bundle
+ creationToken = 7
+ EOF
+
+ cp -r fetch-base fetch-2 &&
+ GIT_TRACE2_EVENT="$(pwd)/trace-fetch-2.txt" \
+ git -C fetch-2 fetch origin &&
+
+ # Since bundle-7 fails to unbundle, do not update creation token.
+ test_cmp_config -C fetch-2 3 fetch.bundlecreationtoken &&
+
+ cat >expect <<-EOF &&
+ $HTTPD_URL/bundle-list
+ $HTTPD_URL/bundle-7.bundle
+ $HTTPD_URL/fake.bundle
+ $HTTPD_URL/bundle-4.bundle
+ EOF
+ test_remote_https_urls <trace-fetch-2.txt >actual &&
+ test_cmp expect actual &&
+
+ # Check which bundles have unbundled by refs
+ git -C fetch-2 for-each-ref --format="%(refname)" "refs/bundles/*" >refs &&
+ cat >expect <<-EOF &&
+ refs/bundles/base
+ refs/bundles/left
+ refs/bundles/merge
+ refs/bundles/right
+ EOF
+ test_cmp expect refs &&
+
+ # Case 3: top bundle does not exist, rest unbundle fine.
+ cat >"$HTTPD_DOCUMENT_ROOT_PATH/bundle-list" <<-EOF &&
+ [bundle]
+ version = 1
+ mode = all
+ heuristic = creationToken
+
+ [bundle "bundle-1"]
+ uri = bundle-1.bundle
+ creationToken = 1
+
+ [bundle "bundle-2"]
+ uri = bundle-2.bundle
+ creationToken = 2
+
+ [bundle "bundle-3"]
+ uri = bundle-3.bundle
+ creationToken = 3
+
+ [bundle "bundle-4"]
+ uri = bundle-4.bundle
+ creationToken = 4
+
+ [bundle "bundle-6"]
+ uri = bundle-6.bundle
+ creationToken = 6
+
+ [bundle "bundle-7"]
+ uri = fake.bundle
+ creationToken = 7
+ EOF
+
+ cp -r fetch-base fetch-3 &&
+ GIT_TRACE2_EVENT="$(pwd)/trace-fetch-3.txt" \
+ git -C fetch-3 fetch origin &&
+
+ # As long as we have continguous successful downloads,
+ # we _do_ set the maximum creation token.
+ test_cmp_config -C fetch-3 6 fetch.bundlecreationtoken &&
+
+ # NOTE: the fetch skips bundle-4 since bundle-6 successfully
+ # unbundles itself and bundle-7 failed to download.
+ cat >expect <<-EOF &&
+ $HTTPD_URL/bundle-list
+ $HTTPD_URL/fake.bundle
+ $HTTPD_URL/bundle-6.bundle
+ EOF
+ test_remote_https_urls <trace-fetch-3.txt >actual &&
+ test_cmp expect actual &&
+
+ # Check which bundles have unbundled by refs
+ git -C fetch-3 for-each-ref --format="%(refname)" "refs/bundles/*" >refs &&
+ cat >expect <<-EOF &&
+ refs/bundles/base
+ refs/bundles/left
+ refs/bundles/lefter
+ refs/bundles/right
+ refs/bundles/righter
+ EOF
+ test_cmp expect refs
+'
+
+test_expect_success 'bundles are downloaded once during fetch --all' '
+ test_when_finished rm -rf download-* trace*.txt fetch-mult &&
+
+ cat >"$HTTPD_DOCUMENT_ROOT_PATH/bundle-list" <<-EOF &&
+ [bundle]
+ version = 1
+ mode = all
+ heuristic = creationToken
+
+ [bundle "bundle-1"]
+ uri = bundle-1.bundle
+ creationToken = 1
+
+ [bundle "bundle-2"]
+ uri = bundle-2.bundle
+ creationToken = 2
+
+ [bundle "bundle-3"]
+ uri = bundle-3.bundle
+ creationToken = 3
+ EOF
+
+ git clone --single-branch --branch=left \
+ --bundle-uri="$HTTPD_URL/bundle-list" \
+ "$HTTPD_URL/smart/fetch.git" fetch-mult &&
+ git -C fetch-mult remote add dup1 "$HTTPD_URL/smart/fetch.git" &&
+ git -C fetch-mult remote add dup2 "$HTTPD_URL/smart/fetch.git" &&
+
+ GIT_TRACE2_EVENT="$(pwd)/trace-mult.txt" \
+ git -C fetch-mult fetch --all &&
+ grep "\"child_start\".*\"git-remote-https\",\"$HTTPD_URL/bundle-list\"" \
+ trace-mult.txt >bundle-fetches &&
+ test_line_count = 1 bundle-fetches
+'
+# Do not add tests here unless they use the HTTP server, as they will
+# not run unless the HTTP dependencies exist.
+
+test_done
diff --git a/t/t5559-http-fetch-smart-http2.sh b/t/t5559-http-fetch-smart-http2.sh
new file mode 100755
index 0000000..54aa9d3
--- /dev/null
+++ b/t/t5559-http-fetch-smart-http2.sh
@@ -0,0 +1,5 @@
+#!/bin/sh
+
+HTTP_PROTO=HTTP/2
+LIB_HTTPD_SSL=1
+. ./t5551-http-fetch-smart.sh
diff --git a/t/t5560-http-backend-noserver.sh b/t/t5560-http-backend-noserver.sh
new file mode 100755
index 0000000..f75068d
--- /dev/null
+++ b/t/t5560-http-backend-noserver.sh
@@ -0,0 +1,78 @@
+#!/bin/sh
+
+test_description='test git-http-backend-noserver'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+HTTPD_DOCUMENT_ROOT_PATH="$TRASH_DIRECTORY"
+
+if test_have_prereq GREP_STRIPS_CR
+then
+ GREP_OPTIONS=-U
+ export GREP_OPTIONS
+fi
+
+run_backend() {
+ echo "$2" |
+ QUERY_STRING="${1#*[?]}" \
+ PATH_TRANSLATED="$HTTPD_DOCUMENT_ROOT_PATH/${1%%[?]*}" \
+ git http-backend >act.out 2>act.err
+}
+
+GET() {
+ REQUEST_METHOD="GET" && export REQUEST_METHOD &&
+ run_backend "/repo.git/$1" &&
+ sane_unset REQUEST_METHOD &&
+ if ! grep "Status" act.out >act
+ then
+ printf "Status: 200 OK\r\n" >act
+ fi
+ printf "Status: $2\r\n" >exp &&
+ test_cmp exp act
+}
+
+POST() {
+ REQUEST_METHOD="POST" && export REQUEST_METHOD &&
+ CONTENT_TYPE="application/x-$1-request" && export CONTENT_TYPE &&
+ run_backend "/repo.git/$1" "$2" &&
+ sane_unset REQUEST_METHOD &&
+ sane_unset CONTENT_TYPE &&
+ if ! grep "Status" act.out >act
+ then
+ printf "Status: 200 OK\r\n" >act
+ fi
+ printf "Status: $3\r\n" >exp &&
+ test_cmp exp act
+}
+
+. "$TEST_DIRECTORY"/t556x_common
+
+expect_aliased() {
+ REQUEST_METHOD="GET" && export REQUEST_METHOD &&
+ if test $1 = 0; then
+ run_backend "$2"
+ else
+ run_backend "$2" &&
+ echo "fatal: '$2': aliased" >exp.err &&
+ test_cmp exp.err act.err
+ fi
+ unset REQUEST_METHOD
+}
+
+test_expect_success 'http-backend blocks bad PATH_INFO' '
+ config http.getanyfile true &&
+
+ expect_aliased 0 /repo.git/HEAD &&
+
+ expect_aliased 1 /repo.git/../HEAD &&
+ expect_aliased 1 /../etc/passwd &&
+ expect_aliased 1 ../etc/passwd &&
+ expect_aliased 1 /etc//passwd &&
+ expect_aliased 1 /etc/./passwd &&
+ expect_aliased 1 //domain/data.txt
+'
+
+test_done
diff --git a/t/t5561-http-backend.sh b/t/t5561-http-backend.sh
new file mode 100755
index 0000000..e1d3b8c
--- /dev/null
+++ b/t/t5561-http-backend.sh
@@ -0,0 +1,139 @@
+#!/bin/sh
+
+test_description='test git-http-backend'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-httpd.sh
+
+if ! test_have_prereq CURL; then
+ skip_all='skipping raw http-backend tests, curl not available'
+ test_done
+fi
+
+start_httpd
+
+GET() {
+ curl --include "$HTTPD_URL/$SMART/repo.git/$1" >out &&
+ tr '\015' Q <out |
+ sed '
+ s/Q$//
+ 1q
+ ' >act &&
+ echo "HTTP/1.1 $2" >exp &&
+ test_cmp exp act
+}
+
+POST() {
+ curl --include --data "$2" \
+ --header "Content-Type: application/x-$1-request" \
+ "$HTTPD_URL/smart/repo.git/$1" >out &&
+ tr '\015' Q <out |
+ sed '
+ s/Q$//
+ 1q
+ ' >act &&
+ echo "HTTP/1.1 $3" >exp &&
+ test_cmp exp act
+}
+
+. "$TEST_DIRECTORY"/t556x_common
+
+grep '^[^#]' >exp <<EOF
+
+### refs/heads/main
+###
+GET /smart/repo.git/refs/heads/main HTTP/1.1 404 -
+
+### getanyfile default
+###
+GET /smart/repo.git/HEAD HTTP/1.1 200
+GET /smart/repo.git/info/refs HTTP/1.1 200
+GET /smart/repo.git/objects/info/packs HTTP/1.1 200
+GET /smart/repo.git/objects/info/alternates HTTP/1.1 200 -
+GET /smart/repo.git/objects/info/http-alternates HTTP/1.1 200 -
+GET /smart/repo.git/$LOOSE_URL HTTP/1.1 200
+GET /smart/repo.git/$PACK_URL HTTP/1.1 200
+GET /smart/repo.git/$IDX_URL HTTP/1.1 200
+
+### no git-daemon-export-ok
+###
+GET /smart_noexport/repo.git/HEAD HTTP/1.1 404 -
+GET /smart_noexport/repo.git/info/refs HTTP/1.1 404 -
+GET /smart_noexport/repo.git/objects/info/packs HTTP/1.1 404 -
+GET /smart_noexport/repo.git/objects/info/alternates HTTP/1.1 404 -
+GET /smart_noexport/repo.git/objects/info/http-alternates HTTP/1.1 404 -
+GET /smart_noexport/repo.git/$LOOSE_URL HTTP/1.1 404 -
+GET /smart_noexport/repo.git/$PACK_URL HTTP/1.1 404 -
+GET /smart_noexport/repo.git/$IDX_URL HTTP/1.1 404 -
+
+### git-daemon-export-ok
+###
+GET /smart_noexport/repo.git/HEAD HTTP/1.1 200
+GET /smart_noexport/repo.git/info/refs HTTP/1.1 200
+GET /smart_noexport/repo.git/objects/info/packs HTTP/1.1 200
+GET /smart_noexport/repo.git/objects/info/alternates HTTP/1.1 200 -
+GET /smart_noexport/repo.git/objects/info/http-alternates HTTP/1.1 200 -
+GET /smart_noexport/repo.git/$LOOSE_URL HTTP/1.1 200
+GET /smart_noexport/repo.git/$PACK_URL HTTP/1.1 200
+GET /smart_noexport/repo.git/$IDX_URL HTTP/1.1 200
+
+### getanyfile true
+###
+GET /smart/repo.git/HEAD HTTP/1.1 200
+GET /smart/repo.git/info/refs HTTP/1.1 200
+GET /smart/repo.git/objects/info/packs HTTP/1.1 200
+GET /smart/repo.git/objects/info/alternates HTTP/1.1 200 -
+GET /smart/repo.git/objects/info/http-alternates HTTP/1.1 200 -
+GET /smart/repo.git/$LOOSE_URL HTTP/1.1 200
+GET /smart/repo.git/$PACK_URL HTTP/1.1 200
+GET /smart/repo.git/$IDX_URL HTTP/1.1 200
+
+### getanyfile false
+###
+GET /smart/repo.git/HEAD HTTP/1.1 403 -
+GET /smart/repo.git/info/refs HTTP/1.1 403 -
+GET /smart/repo.git/objects/info/packs HTTP/1.1 403 -
+GET /smart/repo.git/objects/info/alternates HTTP/1.1 403 -
+GET /smart/repo.git/objects/info/http-alternates HTTP/1.1 403 -
+GET /smart/repo.git/$LOOSE_URL HTTP/1.1 403 -
+GET /smart/repo.git/$PACK_URL HTTP/1.1 403 -
+GET /smart/repo.git/$IDX_URL HTTP/1.1 403 -
+
+### uploadpack default
+###
+GET /smart/repo.git/info/refs?service=git-upload-pack HTTP/1.1 200
+POST /smart/repo.git/git-upload-pack HTTP/1.1 200 -
+
+### uploadpack true
+###
+GET /smart/repo.git/info/refs?service=git-upload-pack HTTP/1.1 200
+POST /smart/repo.git/git-upload-pack HTTP/1.1 200 -
+
+### uploadpack false
+###
+GET /smart/repo.git/info/refs?service=git-upload-pack HTTP/1.1 403 -
+POST /smart/repo.git/git-upload-pack HTTP/1.1 403 -
+
+### receivepack default
+###
+GET /smart/repo.git/info/refs?service=git-receive-pack HTTP/1.1 403 -
+POST /smart/repo.git/git-receive-pack HTTP/1.1 403 -
+
+### receivepack true
+###
+GET /smart/repo.git/info/refs?service=git-receive-pack HTTP/1.1 200
+POST /smart/repo.git/git-receive-pack HTTP/1.1 200 -
+
+### receivepack false
+###
+GET /smart/repo.git/info/refs?service=git-receive-pack HTTP/1.1 403 -
+POST /smart/repo.git/git-receive-pack HTTP/1.1 403 -
+EOF
+test_expect_success 'server request log matches test results' '
+ check_access_log exp
+'
+
+test_done
diff --git a/t/t5562-http-backend-content-length.sh b/t/t5562-http-backend-content-length.sh
new file mode 100755
index 0000000..7ee9858
--- /dev/null
+++ b/t/t5562-http-backend-content-length.sh
@@ -0,0 +1,175 @@
+#!/bin/sh
+
+test_description='test git-http-backend respects CONTENT_LENGTH'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_lazy_prereq GZIP 'gzip --version'
+
+verify_http_result() {
+ # some fatal errors still produce status 200
+ # so check if there is the error message
+ if grep 'fatal:' act.err.$test_count
+ then
+ return 1
+ fi
+
+ if ! grep "Status" act.out.$test_count >act
+ then
+ printf "Status: 200 OK\r\n" >act
+ fi
+ printf "Status: $1\r\n" >exp &&
+ test_cmp exp act
+}
+
+test_http_env() {
+ handler_type="$1"
+ request_body="$2"
+ shift
+ env \
+ CONTENT_TYPE="application/x-git-$handler_type-pack-request" \
+ QUERY_STRING="/repo.git/git-$handler_type-pack" \
+ PATH_TRANSLATED="$PWD/.git/git-$handler_type-pack" \
+ GIT_HTTP_EXPORT_ALL=TRUE \
+ REQUEST_METHOD=POST \
+ "$PERL_PATH" \
+ "$TEST_DIRECTORY"/t5562/invoke-with-content-length.pl \
+ "$request_body" git http-backend >act.out.$test_count 2>act.err.$test_count
+}
+
+ssize_b100dots() {
+ # hardcoded ((size_t) SSIZE_MAX) + 1
+ case "$(build_option sizeof-size_t)" in
+ 8) echo 9223372036854775808;;
+ 4) echo 2147483648;;
+ *) die "Unexpected ssize_t size: $(build_option sizeof-size_t)";;
+ esac
+}
+
+test_expect_success 'setup' '
+ HTTP_CONTENT_ENCODING="identity" &&
+ export HTTP_CONTENT_ENCODING &&
+ git config http.receivepack true &&
+ test_commit c0 &&
+ test_commit c1 &&
+ hash_head=$(git rev-parse HEAD) &&
+ hash_prev=$(git rev-parse HEAD~1) &&
+ {
+ packetize "want $hash_head" &&
+ printf 0000 &&
+ packetize "have $hash_prev" &&
+ packetize "done"
+ } >fetch_body &&
+ test_copy_bytes 10 <fetch_body >fetch_body.trunc &&
+ hash_next=$(git commit-tree -p HEAD -m next HEAD^{tree}) &&
+ {
+ printf "%s %s refs/heads/newbranch\\0report-status object-format=%s\\n" \
+ "$ZERO_OID" "$hash_next" "$(test_oid algo)" | packetize_raw &&
+ printf 0000 &&
+ echo "$hash_next" | git pack-objects --stdout
+ } >push_body &&
+ test_copy_bytes 10 <push_body >push_body.trunc &&
+ : >empty_body
+'
+
+test_expect_success GZIP 'setup, compression related' '
+ gzip -c fetch_body >fetch_body.gz &&
+ test_copy_bytes 10 <fetch_body.gz >fetch_body.gz.trunc &&
+ gzip -c push_body >push_body.gz &&
+ test_copy_bytes 10 <push_body.gz >push_body.gz.trunc
+'
+
+test_expect_success 'fetch plain' '
+ test_http_env upload fetch_body &&
+ verify_http_result "200 OK"
+'
+
+test_expect_success 'fetch plain truncated' '
+ test_http_env upload fetch_body.trunc &&
+ ! verify_http_result "200 OK"
+'
+
+test_expect_success 'fetch plain empty' '
+ test_http_env upload empty_body &&
+ ! verify_http_result "200 OK"
+'
+
+test_expect_success GZIP 'fetch gzipped' '
+ test_env HTTP_CONTENT_ENCODING="gzip" test_http_env upload fetch_body.gz &&
+ verify_http_result "200 OK"
+'
+
+test_expect_success GZIP 'fetch gzipped truncated' '
+ test_env HTTP_CONTENT_ENCODING="gzip" test_http_env upload fetch_body.gz.trunc &&
+ ! verify_http_result "200 OK"
+'
+
+test_expect_success GZIP 'fetch gzipped empty' '
+ test_env HTTP_CONTENT_ENCODING="gzip" test_http_env upload empty_body &&
+ ! verify_http_result "200 OK"
+'
+
+test_expect_success GZIP 'push plain' '
+ test_when_finished "git branch -D newbranch" &&
+ test_http_env receive push_body &&
+ verify_http_result "200 OK" &&
+ git rev-parse newbranch >act.head &&
+ echo "$hash_next" >exp.head &&
+ test_cmp act.head exp.head
+'
+
+test_expect_success 'push plain truncated' '
+ test_http_env receive push_body.trunc &&
+ ! verify_http_result "200 OK"
+'
+
+test_expect_success 'push plain empty' '
+ test_http_env receive empty_body &&
+ ! verify_http_result "200 OK"
+'
+
+test_expect_success GZIP 'push gzipped' '
+ test_when_finished "git branch -D newbranch" &&
+ test_env HTTP_CONTENT_ENCODING="gzip" test_http_env receive push_body.gz &&
+ verify_http_result "200 OK" &&
+ git rev-parse newbranch >act.head &&
+ echo "$hash_next" >exp.head &&
+ test_cmp act.head exp.head
+'
+
+test_expect_success GZIP 'push gzipped truncated' '
+ test_env HTTP_CONTENT_ENCODING="gzip" test_http_env receive push_body.gz.trunc &&
+ ! verify_http_result "200 OK"
+'
+
+test_expect_success GZIP 'push gzipped empty' '
+ test_env HTTP_CONTENT_ENCODING="gzip" test_http_env receive empty_body &&
+ ! verify_http_result "200 OK"
+'
+
+test_expect_success 'CONTENT_LENGTH overflow ssite_t' '
+ NOT_FIT_IN_SSIZE=$(ssize_b100dots) &&
+ env \
+ CONTENT_TYPE=application/x-git-upload-pack-request \
+ QUERY_STRING=/repo.git/git-upload-pack \
+ PATH_TRANSLATED="$PWD"/.git/git-upload-pack \
+ GIT_HTTP_EXPORT_ALL=TRUE \
+ REQUEST_METHOD=POST \
+ CONTENT_LENGTH="$NOT_FIT_IN_SSIZE" \
+ git http-backend </dev/null >/dev/null 2>err &&
+ grep "fatal:.*CONTENT_LENGTH" err
+'
+
+test_expect_success 'empty CONTENT_LENGTH' '
+ env \
+ QUERY_STRING="service=git-receive-pack" \
+ PATH_TRANSLATED="$PWD"/.git/info/refs \
+ GIT_HTTP_EXPORT_ALL=TRUE \
+ REQUEST_METHOD=GET \
+ CONTENT_LENGTH="" \
+ git http-backend <empty_body >act.out.$test_count 2>act.err.$test_count &&
+ verify_http_result "200 OK"
+'
+
+test_done
diff --git a/t/t5562/invoke-with-content-length.pl b/t/t5562/invoke-with-content-length.pl
new file mode 100644
index 0000000..718dd9b
--- /dev/null
+++ b/t/t5562/invoke-with-content-length.pl
@@ -0,0 +1,36 @@
+use 5.008;
+use strict;
+use warnings;
+
+my $body_filename = $ARGV[0];
+my @command = @ARGV[1 .. $#ARGV];
+
+# read data
+my $body_size = -s $body_filename;
+$ENV{"CONTENT_LENGTH"} = $body_size;
+open(my $body_fh, "<", $body_filename) or die "Cannot open $body_filename: $!";
+my $body_data;
+defined read($body_fh, $body_data, $body_size) or die "Cannot read $body_filename: $!";
+close($body_fh);
+
+# write data
+my $pid = open(my $out, "|-", @command);
+{
+ # disable buffering at $out
+ my $old_selected = select;
+ select $out;
+ $| = 1;
+ select $old_selected;
+}
+print $out $body_data or die "Cannot write data: $!";
+
+$SIG{ALRM} = sub {
+ kill 'KILL', $pid;
+ die "Command did not exit after reading whole body";
+};
+alarm 60;
+
+my $ret = waitpid($pid, 0);
+if ($ret != $pid) {
+ die "confusing return from waitpid: $ret";
+}
diff --git a/t/t5563-simple-http-auth.sh b/t/t5563-simple-http-auth.sh
new file mode 100755
index 0000000..ab8a721
--- /dev/null
+++ b/t/t5563-simple-http-auth.sh
@@ -0,0 +1,329 @@
+#!/bin/sh
+
+test_description='test http auth header and credential helper interop'
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-httpd.sh
+
+enable_cgipassauth
+if ! test_have_prereq CGIPASSAUTH
+then
+ skip_all="no CGIPassAuth support"
+ test_done
+fi
+start_httpd
+
+test_expect_success 'setup_credential_helper' '
+ mkdir "$TRASH_DIRECTORY/bin" &&
+ PATH=$PATH:"$TRASH_DIRECTORY/bin" &&
+ export PATH &&
+
+ CREDENTIAL_HELPER="$TRASH_DIRECTORY/bin/git-credential-test-helper" &&
+ write_script "$CREDENTIAL_HELPER" <<-\EOF
+ cmd=$1
+ teefile=$cmd-query.cred
+ catfile=$cmd-reply.cred
+ sed -n -e "/^$/q" -e "p" >>$teefile
+ if test "$cmd" = "get"
+ then
+ cat $catfile
+ fi
+ EOF
+'
+
+set_credential_reply () {
+ cat >"$TRASH_DIRECTORY/$1-reply.cred"
+}
+
+expect_credential_query () {
+ cat >"$TRASH_DIRECTORY/$1-expect.cred" &&
+ test_cmp "$TRASH_DIRECTORY/$1-expect.cred" \
+ "$TRASH_DIRECTORY/$1-query.cred"
+}
+
+per_test_cleanup () {
+ rm -f *.cred &&
+ rm -f "$HTTPD_ROOT_PATH"/custom-auth.valid \
+ "$HTTPD_ROOT_PATH"/custom-auth.challenge
+}
+
+test_expect_success 'setup repository' '
+ test_commit foo &&
+ git init --bare "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
+ git push --mirror "$HTTPD_DOCUMENT_ROOT_PATH/repo.git"
+'
+
+test_expect_success 'access using basic auth' '
+ test_when_finished "per_test_cleanup" &&
+
+ set_credential_reply get <<-EOF &&
+ username=alice
+ password=secret-passwd
+ EOF
+
+ # Basic base64(alice:secret-passwd)
+ cat >"$HTTPD_ROOT_PATH/custom-auth.valid" <<-EOF &&
+ Basic YWxpY2U6c2VjcmV0LXBhc3N3ZA==
+ EOF
+
+ cat >"$HTTPD_ROOT_PATH/custom-auth.challenge" <<-EOF &&
+ WWW-Authenticate: Basic realm="example.com"
+ EOF
+
+ test_config_global credential.helper test-helper &&
+ git ls-remote "$HTTPD_URL/custom_auth/repo.git" &&
+
+ expect_credential_query get <<-EOF &&
+ protocol=http
+ host=$HTTPD_DEST
+ wwwauth[]=Basic realm="example.com"
+ EOF
+
+ expect_credential_query store <<-EOF
+ protocol=http
+ host=$HTTPD_DEST
+ username=alice
+ password=secret-passwd
+ EOF
+'
+
+test_expect_success 'access using basic auth invalid credentials' '
+ test_when_finished "per_test_cleanup" &&
+
+ set_credential_reply get <<-EOF &&
+ username=baduser
+ password=wrong-passwd
+ EOF
+
+ # Basic base64(alice:secret-passwd)
+ cat >"$HTTPD_ROOT_PATH/custom-auth.valid" <<-EOF &&
+ Basic YWxpY2U6c2VjcmV0LXBhc3N3ZA==
+ EOF
+
+ cat >"$HTTPD_ROOT_PATH/custom-auth.challenge" <<-EOF &&
+ WWW-Authenticate: Basic realm="example.com"
+ EOF
+
+ test_config_global credential.helper test-helper &&
+ test_must_fail git ls-remote "$HTTPD_URL/custom_auth/repo.git" &&
+
+ expect_credential_query get <<-EOF &&
+ protocol=http
+ host=$HTTPD_DEST
+ wwwauth[]=Basic realm="example.com"
+ EOF
+
+ expect_credential_query erase <<-EOF
+ protocol=http
+ host=$HTTPD_DEST
+ username=baduser
+ password=wrong-passwd
+ wwwauth[]=Basic realm="example.com"
+ EOF
+'
+
+test_expect_success 'access using basic auth with extra challenges' '
+ test_when_finished "per_test_cleanup" &&
+
+ set_credential_reply get <<-EOF &&
+ username=alice
+ password=secret-passwd
+ EOF
+
+ # Basic base64(alice:secret-passwd)
+ cat >"$HTTPD_ROOT_PATH/custom-auth.valid" <<-EOF &&
+ Basic YWxpY2U6c2VjcmV0LXBhc3N3ZA==
+ EOF
+
+ cat >"$HTTPD_ROOT_PATH/custom-auth.challenge" <<-EOF &&
+ WWW-Authenticate: FooBar param1="value1" param2="value2"
+ WWW-Authenticate: Bearer authorize_uri="id.example.com" p=1 q=0
+ WWW-Authenticate: Basic realm="example.com"
+ EOF
+
+ test_config_global credential.helper test-helper &&
+ git ls-remote "$HTTPD_URL/custom_auth/repo.git" &&
+
+ expect_credential_query get <<-EOF &&
+ protocol=http
+ host=$HTTPD_DEST
+ wwwauth[]=FooBar param1="value1" param2="value2"
+ wwwauth[]=Bearer authorize_uri="id.example.com" p=1 q=0
+ wwwauth[]=Basic realm="example.com"
+ EOF
+
+ expect_credential_query store <<-EOF
+ protocol=http
+ host=$HTTPD_DEST
+ username=alice
+ password=secret-passwd
+ EOF
+'
+
+test_expect_success 'access using basic auth mixed-case wwwauth header name' '
+ test_when_finished "per_test_cleanup" &&
+
+ set_credential_reply get <<-EOF &&
+ username=alice
+ password=secret-passwd
+ EOF
+
+ # Basic base64(alice:secret-passwd)
+ cat >"$HTTPD_ROOT_PATH/custom-auth.valid" <<-EOF &&
+ Basic YWxpY2U6c2VjcmV0LXBhc3N3ZA==
+ EOF
+
+ cat >"$HTTPD_ROOT_PATH/custom-auth.challenge" <<-EOF &&
+ www-authenticate: foobar param1="value1" param2="value2"
+ WWW-AUTHENTICATE: BEARER authorize_uri="id.example.com" p=1 q=0
+ WwW-aUtHeNtIcAtE: baSiC realm="example.com"
+ EOF
+
+ test_config_global credential.helper test-helper &&
+ git ls-remote "$HTTPD_URL/custom_auth/repo.git" &&
+
+ expect_credential_query get <<-EOF &&
+ protocol=http
+ host=$HTTPD_DEST
+ wwwauth[]=foobar param1="value1" param2="value2"
+ wwwauth[]=BEARER authorize_uri="id.example.com" p=1 q=0
+ wwwauth[]=baSiC realm="example.com"
+ EOF
+
+ expect_credential_query store <<-EOF
+ protocol=http
+ host=$HTTPD_DEST
+ username=alice
+ password=secret-passwd
+ EOF
+'
+
+test_expect_success 'access using basic auth with wwwauth header continuations' '
+ test_when_finished "per_test_cleanup" &&
+
+ set_credential_reply get <<-EOF &&
+ username=alice
+ password=secret-passwd
+ EOF
+
+ # Basic base64(alice:secret-passwd)
+ cat >"$HTTPD_ROOT_PATH/custom-auth.valid" <<-EOF &&
+ Basic YWxpY2U6c2VjcmV0LXBhc3N3ZA==
+ EOF
+
+ # Note that leading and trailing whitespace is important to correctly
+ # simulate a continuation/folded header.
+ cat >"$HTTPD_ROOT_PATH/custom-auth.challenge" <<-EOF &&
+ WWW-Authenticate: FooBar param1="value1"
+ param2="value2"
+ WWW-Authenticate: Bearer authorize_uri="id.example.com"
+ p=1
+ q=0
+ WWW-Authenticate: Basic realm="example.com"
+ EOF
+
+ test_config_global credential.helper test-helper &&
+ git ls-remote "$HTTPD_URL/custom_auth/repo.git" &&
+
+ expect_credential_query get <<-EOF &&
+ protocol=http
+ host=$HTTPD_DEST
+ wwwauth[]=FooBar param1="value1" param2="value2"
+ wwwauth[]=Bearer authorize_uri="id.example.com" p=1 q=0
+ wwwauth[]=Basic realm="example.com"
+ EOF
+
+ expect_credential_query store <<-EOF
+ protocol=http
+ host=$HTTPD_DEST
+ username=alice
+ password=secret-passwd
+ EOF
+'
+
+test_expect_success 'access using basic auth with wwwauth header empty continuations' '
+ test_when_finished "per_test_cleanup" &&
+
+ set_credential_reply get <<-EOF &&
+ username=alice
+ password=secret-passwd
+ EOF
+
+ # Basic base64(alice:secret-passwd)
+ cat >"$HTTPD_ROOT_PATH/custom-auth.valid" <<-EOF &&
+ Basic YWxpY2U6c2VjcmV0LXBhc3N3ZA==
+ EOF
+
+ CHALLENGE="$HTTPD_ROOT_PATH/custom-auth.challenge" &&
+
+ # Note that leading and trailing whitespace is important to correctly
+ # simulate a continuation/folded header.
+ printf "WWW-Authenticate: FooBar param1=\"value1\"\r\n" >"$CHALLENGE" &&
+ printf " \r\n" >>"$CHALLENGE" &&
+ printf " param2=\"value2\"\r\n" >>"$CHALLENGE" &&
+ printf "WWW-Authenticate: Bearer authorize_uri=\"id.example.com\"\r\n" >>"$CHALLENGE" &&
+ printf " p=1\r\n" >>"$CHALLENGE" &&
+ printf " \r\n" >>"$CHALLENGE" &&
+ printf " q=0\r\n" >>"$CHALLENGE" &&
+ printf "WWW-Authenticate: Basic realm=\"example.com\"\r\n" >>"$CHALLENGE" &&
+
+ test_config_global credential.helper test-helper &&
+ git ls-remote "$HTTPD_URL/custom_auth/repo.git" &&
+
+ expect_credential_query get <<-EOF &&
+ protocol=http
+ host=$HTTPD_DEST
+ wwwauth[]=FooBar param1="value1" param2="value2"
+ wwwauth[]=Bearer authorize_uri="id.example.com" p=1 q=0
+ wwwauth[]=Basic realm="example.com"
+ EOF
+
+ expect_credential_query store <<-EOF
+ protocol=http
+ host=$HTTPD_DEST
+ username=alice
+ password=secret-passwd
+ EOF
+'
+
+test_expect_success 'access using basic auth with wwwauth header mixed line-endings' '
+ test_when_finished "per_test_cleanup" &&
+
+ set_credential_reply get <<-EOF &&
+ username=alice
+ password=secret-passwd
+ EOF
+
+ # Basic base64(alice:secret-passwd)
+ cat >"$HTTPD_ROOT_PATH/custom-auth.valid" <<-EOF &&
+ Basic YWxpY2U6c2VjcmV0LXBhc3N3ZA==
+ EOF
+
+ CHALLENGE="$HTTPD_ROOT_PATH/custom-auth.challenge" &&
+
+ # Note that leading and trailing whitespace is important to correctly
+ # simulate a continuation/folded header.
+ printf "WWW-Authenticate: FooBar param1=\"value1\"\r\n" >"$CHALLENGE" &&
+ printf " \r\n" >>"$CHALLENGE" &&
+ printf "\tparam2=\"value2\"\r\n" >>"$CHALLENGE" &&
+ printf "WWW-Authenticate: Basic realm=\"example.com\"" >>"$CHALLENGE" &&
+
+ test_config_global credential.helper test-helper &&
+ git ls-remote "$HTTPD_URL/custom_auth/repo.git" &&
+
+ expect_credential_query get <<-EOF &&
+ protocol=http
+ host=$HTTPD_DEST
+ wwwauth[]=FooBar param1="value1" param2="value2"
+ wwwauth[]=Basic realm="example.com"
+ EOF
+
+ expect_credential_query store <<-EOF
+ protocol=http
+ host=$HTTPD_DEST
+ username=alice
+ password=secret-passwd
+ EOF
+'
+
+test_done
diff --git a/t/t5564-http-proxy.sh b/t/t5564-http-proxy.sh
new file mode 100755
index 0000000..9da5134
--- /dev/null
+++ b/t/t5564-http-proxy.sh
@@ -0,0 +1,41 @@
+#!/bin/sh
+
+test_description="test fetching through http proxy"
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-httpd.sh
+
+LIB_HTTPD_PROXY=1
+start_httpd
+
+test_expect_success 'setup repository' '
+ test_commit foo &&
+ git init --bare "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
+ git push --mirror "$HTTPD_DOCUMENT_ROOT_PATH/repo.git"
+'
+
+setup_askpass_helper
+
+# sanity check that our test setup is correctly using proxy
+test_expect_success 'proxy requires password' '
+ test_config_global http.proxy $HTTPD_DEST &&
+ test_must_fail git clone $HTTPD_URL/smart/repo.git 2>err &&
+ grep "error.*407" err
+'
+
+test_expect_success 'clone through proxy with auth' '
+ test_when_finished "rm -rf clone" &&
+ test_config_global http.proxy http://proxuser:proxpass@$HTTPD_DEST &&
+ GIT_TRACE_CURL=$PWD/trace git clone $HTTPD_URL/smart/repo.git clone &&
+ grep -i "Proxy-Authorization: Basic <redacted>" trace
+'
+
+test_expect_success 'clone can prompt for proxy password' '
+ test_when_finished "rm -rf clone" &&
+ test_config_global http.proxy http://proxuser@$HTTPD_DEST &&
+ set_askpass nobody proxpass &&
+ GIT_TRACE_CURL=$PWD/trace git clone $HTTPD_URL/smart/repo.git clone &&
+ expect_askpass pass proxuser
+'
+
+test_done
diff --git a/t/t556x_common b/t/t556x_common
new file mode 100755
index 0000000..670fb89
--- /dev/null
+++ b/t/t556x_common
@@ -0,0 +1,110 @@
+#!/bin/sh
+
+find_file() {
+ cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
+ find $1 -type f |
+ sed -e 1q
+}
+
+config() {
+ git --git-dir="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" config $1 $2
+}
+
+test_expect_success 'setup repository' '
+ echo content >file &&
+ git add file &&
+ git commit -m one &&
+
+ mkdir "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
+ (cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
+ git --bare init &&
+ : >objects/info/alternates &&
+ : >objects/info/http-alternates
+ ) &&
+ git remote add public "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
+ git push public main:main &&
+
+ (cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
+ git repack -a -d
+ ) &&
+
+ echo other >file &&
+ git add file &&
+ git commit -m two &&
+ git push public main:main &&
+
+ LOOSE_URL=$(find_file objects/??) &&
+ PACK_URL=$(find_file objects/pack/*.pack) &&
+ IDX_URL=$(find_file objects/pack/*.idx)
+'
+
+get_static_files() {
+ GET HEAD "$1" &&
+ GET info/refs "$1" &&
+ GET objects/info/packs "$1" &&
+ GET objects/info/alternates "$1" &&
+ GET objects/info/http-alternates "$1" &&
+ GET $LOOSE_URL "$1" &&
+ GET $PACK_URL "$1" &&
+ GET $IDX_URL "$1"
+}
+
+SMART=smart
+GIT_HTTP_EXPORT_ALL=1 && export GIT_HTTP_EXPORT_ALL
+test_expect_success 'direct refs/heads/main not found' '
+ GET refs/heads/main "404 Not Found"
+'
+test_expect_success 'static file is ok' '
+ get_static_files "200 OK"
+'
+SMART=smart_noexport
+unset GIT_HTTP_EXPORT_ALL
+test_expect_success 'no export by default' '
+ get_static_files "404 Not Found"
+'
+test_expect_success 'export if git-daemon-export-ok' '
+ (cd "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
+ touch git-daemon-export-ok
+ ) &&
+ get_static_files "200 OK"
+'
+SMART=smart
+GIT_HTTP_EXPORT_ALL=1 && export GIT_HTTP_EXPORT_ALL
+test_expect_success 'static file if http.getanyfile true is ok' '
+ config http.getanyfile true &&
+ get_static_files "200 OK"
+'
+test_expect_success 'static file if http.getanyfile false fails' '
+ config http.getanyfile false &&
+ get_static_files "403 Forbidden"
+'
+
+test_expect_success 'http.uploadpack default enabled' '
+ GET info/refs?service=git-upload-pack "200 OK" &&
+ POST git-upload-pack 0000 "200 OK"
+'
+test_expect_success 'http.uploadpack true' '
+ config http.uploadpack true &&
+ GET info/refs?service=git-upload-pack "200 OK" &&
+ POST git-upload-pack 0000 "200 OK"
+'
+test_expect_success 'http.uploadpack false' '
+ config http.uploadpack false &&
+ GET info/refs?service=git-upload-pack "403 Forbidden" &&
+ POST git-upload-pack 0000 "403 Forbidden"
+'
+
+test_expect_success 'http.receivepack default disabled' '
+ GET info/refs?service=git-receive-pack "403 Forbidden" &&
+ POST git-receive-pack 0000 "403 Forbidden"
+'
+test_expect_success 'http.receivepack true' '
+ config http.receivepack true &&
+ GET info/refs?service=git-receive-pack "200 OK" &&
+ POST git-receive-pack 0000 "200 OK"
+'
+test_expect_success 'http.receivepack false' '
+ config http.receivepack false &&
+ GET info/refs?service=git-receive-pack "403 Forbidden" &&
+ POST git-receive-pack 0000 "403 Forbidden"
+'
diff --git a/t/t5570-git-daemon.sh b/t/t5570-git-daemon.sh
new file mode 100755
index 0000000..f9a9bf9
--- /dev/null
+++ b/t/t5570-git-daemon.sh
@@ -0,0 +1,210 @@
+#!/bin/sh
+
+test_description='test fetching over git protocol'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+. "$TEST_DIRECTORY"/lib-git-daemon.sh
+start_git_daemon
+
+check_verbose_connect () {
+ test_grep -F "Looking up 127.0.0.1 ..." stderr &&
+ test_grep -F "Connecting to 127.0.0.1 (port " stderr &&
+ test_grep -F "done." stderr
+}
+
+test_expect_success 'setup repository' '
+ git config push.default matching &&
+ echo content >file &&
+ git add file &&
+ git commit -m one
+'
+
+test_expect_success 'create git-accessible bare repository' '
+ mkdir "$GIT_DAEMON_DOCUMENT_ROOT_PATH/repo.git" &&
+ (cd "$GIT_DAEMON_DOCUMENT_ROOT_PATH/repo.git" &&
+ git --bare init &&
+ : >git-daemon-export-ok
+ ) &&
+ git remote add public "$GIT_DAEMON_DOCUMENT_ROOT_PATH/repo.git" &&
+ git push public main:main
+'
+
+test_expect_success 'clone git repository' '
+ git clone -v "$GIT_DAEMON_URL/repo.git" clone 2>stderr &&
+ check_verbose_connect &&
+ test_cmp file clone/file
+'
+
+test_expect_success 'fetch changes via git protocol' '
+ echo content >>file &&
+ git commit -a -m two &&
+ git push public &&
+ (cd clone && git pull -v) 2>stderr &&
+ check_verbose_connect &&
+ test_cmp file clone/file
+'
+
+test_expect_success 'no-op fetch -v stderr is as expected' '
+ (cd clone && git fetch -v) 2>stderr &&
+ check_verbose_connect
+'
+
+test_expect_success 'no-op fetch without "-v" is quiet' '
+ (cd clone && git fetch 2>../stderr) &&
+ test_must_be_empty stderr
+'
+
+test_expect_success 'remote detects correct HEAD' '
+ git push public main:other &&
+ (cd clone &&
+ git remote set-head -d origin &&
+ git remote set-head -a origin &&
+ git symbolic-ref refs/remotes/origin/HEAD > output &&
+ echo refs/remotes/origin/main > expect &&
+ test_cmp expect output
+ )
+'
+
+test_expect_success 'prepare pack objects' '
+ cp -R "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo.git "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_pack.git &&
+ (cd "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_pack.git &&
+ git --bare repack -a -d
+ )
+'
+
+test_expect_success 'fetch notices corrupt pack' '
+ cp -R "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_pack.git "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_bad1.git &&
+ (cd "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_bad1.git &&
+ p=$(ls objects/pack/pack-*.pack) &&
+ chmod u+w $p &&
+ printf %0256d 0 | dd of=$p bs=256 count=1 seek=1 conv=notrunc
+ ) &&
+ mkdir repo_bad1.git &&
+ (cd repo_bad1.git &&
+ git --bare init &&
+ test_must_fail git --bare fetch "$GIT_DAEMON_URL/repo_bad1.git" &&
+ test 0 = $(ls objects/pack/pack-*.pack | wc -l)
+ )
+'
+
+test_expect_success 'fetch notices corrupt idx' '
+ cp -R "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_pack.git "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_bad2.git &&
+ (cd "$GIT_DAEMON_DOCUMENT_ROOT_PATH"/repo_bad2.git &&
+ rm -f objects/pack/multi-pack-index &&
+ p=$(ls objects/pack/pack-*.idx) &&
+ chmod u+w $p &&
+ printf %0256d 0 | dd of=$p bs=256 count=1 seek=1 conv=notrunc
+ ) &&
+ mkdir repo_bad2.git &&
+ (cd repo_bad2.git &&
+ git --bare init &&
+ test_must_fail git --bare fetch "$GIT_DAEMON_URL/repo_bad2.git" &&
+ test 0 = $(ls objects/pack | wc -l)
+ )
+'
+
+test_expect_success 'client refuses to ask for repo with newline' '
+ test_must_fail git clone "$GIT_DAEMON_URL/repo$LF.git" dst 2>stderr &&
+ test_grep newline.is.forbidden stderr
+'
+
+test_remote_error()
+{
+ do_export=YesPlease
+ while test $# -gt 0
+ do
+ case $1 in
+ -x)
+ shift
+ chmod -x "$GIT_DAEMON_DOCUMENT_ROOT_PATH/repo.git"
+ ;;
+ -n)
+ shift
+ do_export=
+ ;;
+ *)
+ break
+ esac
+ done
+
+ msg=$1
+ shift
+ cmd=$1
+ shift
+ repo=$1
+ shift || error "invalid number of arguments"
+
+ if test -x "$GIT_DAEMON_DOCUMENT_ROOT_PATH/$repo"
+ then
+ if test -n "$do_export"
+ then
+ : >"$GIT_DAEMON_DOCUMENT_ROOT_PATH/$repo/git-daemon-export-ok"
+ else
+ rm -f "$GIT_DAEMON_DOCUMENT_ROOT_PATH/$repo/git-daemon-export-ok"
+ fi
+ fi
+
+ test_must_fail git "$cmd" "$GIT_DAEMON_URL/$repo" "$@" 2>output &&
+ test_grep "fatal: remote error: $msg: /$repo" output &&
+ ret=$?
+ chmod +x "$GIT_DAEMON_DOCUMENT_ROOT_PATH/repo.git"
+ (exit $ret)
+}
+
+msg="access denied or repository not exported"
+test_expect_success 'clone non-existent' "test_remote_error '$msg' clone nowhere.git"
+test_expect_success 'push disabled' "test_remote_error '$msg' push repo.git main"
+test_expect_success 'read access denied' "test_remote_error -x '$msg' fetch repo.git"
+test_expect_success 'not exported' "test_remote_error -n '$msg' fetch repo.git"
+
+stop_git_daemon
+start_git_daemon --informative-errors
+
+test_expect_success 'clone non-existent' "test_remote_error 'no such repository' clone nowhere.git"
+test_expect_success 'push disabled' "test_remote_error 'service not enabled' push repo.git main"
+test_expect_success 'read access denied' "test_remote_error -x 'no such repository' fetch repo.git"
+test_expect_success 'not exported' "test_remote_error -n 'repository not exported' fetch repo.git"
+
+stop_git_daemon
+start_git_daemon --interpolated-path="$GIT_DAEMON_DOCUMENT_ROOT_PATH/%H%D"
+
+test_expect_success 'access repo via interpolated hostname' '
+ repo="$GIT_DAEMON_DOCUMENT_ROOT_PATH/localhost/interp.git" &&
+ git init --bare "$repo" &&
+ git push "$repo" HEAD &&
+ >"$repo"/git-daemon-export-ok &&
+ GIT_OVERRIDE_VIRTUAL_HOST=localhost \
+ git ls-remote "$GIT_DAEMON_URL/interp.git" &&
+ GIT_OVERRIDE_VIRTUAL_HOST=LOCALHOST \
+ git ls-remote "$GIT_DAEMON_URL/interp.git"
+'
+
+test_expect_success 'hostname cannot break out of directory' '
+ repo="$GIT_DAEMON_DOCUMENT_ROOT_PATH/../escape.git" &&
+ git init --bare "$repo" &&
+ git push "$repo" HEAD &&
+ >"$repo"/git-daemon-export-ok &&
+ test_must_fail \
+ env GIT_OVERRIDE_VIRTUAL_HOST=.. \
+ git ls-remote "$GIT_DAEMON_URL/escape.git"
+'
+
+test_expect_success FAKENC 'hostname interpolation works after LF-stripping' '
+ {
+ printf "git-upload-pack /interp.git\n\0host=localhost" | packetize_raw &&
+ printf "0000"
+ } >input &&
+ fake_nc "$GIT_DAEMON_HOST_PORT" <input >output &&
+ depacketize <output >output.raw &&
+
+ # just pick out the value of main, which avoids any protocol
+ # particulars
+ perl -lne "print \$1 if m{^(\\S+) refs/heads/main}" <output.raw >actual &&
+ git -C "$repo" rev-parse main >expect &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t5571-pre-push-hook.sh b/t/t5571-pre-push-hook.sh
new file mode 100755
index 0000000..448134c
--- /dev/null
+++ b/t/t5571-pre-push-hook.sh
@@ -0,0 +1,142 @@
+#!/bin/sh
+
+test_description='check pre-push hooks'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ test_hook pre-push <<-\EOF &&
+ cat >actual
+ EOF
+
+ git config push.default upstream &&
+ git init --bare repo1 &&
+ git remote add parent1 repo1 &&
+ test_commit one &&
+ cat >expect <<-EOF &&
+ HEAD $(git rev-parse HEAD) refs/heads/foreign $(test_oid zero)
+ EOF
+
+ test_when_finished "rm actual" &&
+ git push parent1 HEAD:foreign &&
+ test_cmp expect actual
+'
+
+COMMIT1="$(git rev-parse HEAD)"
+export COMMIT1
+
+test_expect_success 'push with failing hook' '
+ test_hook pre-push <<-\EOF &&
+ cat >actual &&
+ exit 1
+ EOF
+
+ test_commit two &&
+ cat >expect <<-EOF &&
+ HEAD $(git rev-parse HEAD) refs/heads/main $(test_oid zero)
+ EOF
+
+ test_when_finished "rm actual" &&
+ test_must_fail git push parent1 HEAD &&
+ test_cmp expect actual
+'
+
+test_expect_success '--no-verify bypasses hook' '
+ git push --no-verify parent1 HEAD &&
+ test_path_is_missing actual
+'
+
+COMMIT2="$(git rev-parse HEAD)"
+export COMMIT2
+
+test_expect_success 'push with hook' '
+ test_hook --setup pre-push <<-\EOF &&
+ echo "$1" >actual
+ echo "$2" >>actual
+ cat >>actual
+ EOF
+
+ cat >expect <<-EOF &&
+ parent1
+ repo1
+ refs/heads/main $COMMIT2 refs/heads/foreign $COMMIT1
+ EOF
+
+ git push parent1 main:foreign &&
+ test_cmp expect actual
+'
+
+test_expect_success 'add a branch' '
+ git checkout -b other parent1/foreign &&
+ test_commit three
+'
+
+COMMIT3="$(git rev-parse HEAD)"
+export COMMIT3
+
+test_expect_success 'push to default' '
+ cat >expect <<-EOF &&
+ parent1
+ repo1
+ refs/heads/other $COMMIT3 refs/heads/foreign $COMMIT2
+ EOF
+ git push &&
+ test_cmp expect actual
+'
+
+test_expect_success 'push non-branches' '
+ cat >expect <<-EOF &&
+ parent1
+ repo1
+ refs/tags/one $COMMIT1 refs/tags/tag1 $ZERO_OID
+ HEAD~ $COMMIT2 refs/heads/prev $ZERO_OID
+ EOF
+
+ git push parent1 one:tag1 HEAD~:refs/heads/prev &&
+ test_cmp expect actual
+'
+
+test_expect_success 'push delete' '
+ cat >expect <<-EOF &&
+ parent1
+ repo1
+ (delete) $ZERO_OID refs/heads/prev $COMMIT2
+ EOF
+
+ git push parent1 :prev &&
+ test_cmp expect actual
+'
+
+test_expect_success 'push to URL' '
+ cat >expect <<-EOF &&
+ repo1
+ repo1
+ HEAD $COMMIT3 refs/heads/other $ZERO_OID
+ EOF
+
+ git push repo1 HEAD &&
+ test_cmp expect actual
+'
+
+test_expect_success 'set up many-ref tests' '
+ {
+ nr=1000 &&
+ while test $nr -lt 2000
+ do
+ nr=$(( $nr + 1 )) &&
+ echo "create refs/heads/b/$nr $COMMIT3" || return 1
+ done
+ } | git update-ref --stdin
+'
+
+test_expect_success 'sigpipe does not cause pre-push hook failure' '
+ test_hook --clobber pre-push <<-\EOF &&
+ exit 0
+ EOF
+ git push parent1 "refs/heads/b/*:refs/heads/b/*"
+'
+
+test_done
diff --git a/t/t5572-pull-submodule.sh b/t/t5572-pull-submodule.sh
new file mode 100755
index 0000000..5174452
--- /dev/null
+++ b/t/t5572-pull-submodule.sh
@@ -0,0 +1,248 @@
+#!/bin/sh
+
+test_description='pull can handle submodules'
+
+GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB=1
+export GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB
+
+. ./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" &&
+ may_only_be_test_must_fail "$2" &&
+ $2 git pull
+}
+
+# pulls without conflicts
+test_submodule_switch_func "git_pull"
+
+git_pull_ff () {
+ reset_branch_to_HEAD "$1" &&
+ may_only_be_test_must_fail "$2" &&
+ $2 git pull --ff
+}
+
+test_submodule_switch_func "git_pull_ff"
+
+git_pull_ff_only () {
+ reset_branch_to_HEAD "$1" &&
+ may_only_be_test_must_fail "$2" &&
+ $2 git pull --ff-only
+}
+
+test_submodule_switch_func "git_pull_ff_only"
+
+git_pull_noff () {
+ reset_branch_to_HEAD "$1" &&
+ may_only_be_test_must_fail "$2" &&
+ $2 git pull --no-ff
+}
+
+if test "$GIT_TEST_MERGE_ALGORITHM" != ort
+then
+ KNOWN_FAILURE_NOFF_MERGE_DOESNT_CREATE_EMPTY_SUBMODULE_DIR=1
+ KNOWN_FAILURE_NOFF_MERGE_ATTEMPTS_TO_MERGE_REMOVED_SUBMODULE_FILES=1
+fi
+test_submodule_switch_func "git_pull_noff"
+
+test_expect_success 'setup' '
+ git config --global protocol.file.allow always
+'
+
+test_expect_success 'pull --recurse-submodule setup' '
+ test_create_repo child &&
+ test_commit -C child bar &&
+
+ test_create_repo parent &&
+ test_commit -C child foo &&
+
+ git -C parent submodule add ../child sub &&
+ git -C parent commit -m "add submodule" &&
+
+ git clone --recurse-submodules parent super
+'
+
+test_expect_success 'recursive pull updates working tree' '
+ test_commit -C child merge_strategy &&
+ git -C parent submodule update --remote &&
+ git -C parent add sub &&
+ git -C parent commit -m "update submodule" &&
+
+ git -C super pull --no-rebase --recurse-submodules &&
+ test_path_is_file super/sub/merge_strategy.t
+'
+
+test_expect_success "submodule.recurse option triggers recursive pull" '
+ test_commit -C child merge_strategy_2 &&
+ git -C parent submodule update --remote &&
+ git -C parent add sub &&
+ git -C parent commit -m "update submodule" &&
+
+ git -C super -c submodule.recurse pull --no-rebase &&
+ test_path_is_file super/sub/merge_strategy_2.t
+'
+
+test_expect_success " --[no-]recurse-submodule and submodule.recurse" '
+ test_commit -C child merge_strategy_3 &&
+ git -C parent submodule update --remote &&
+ git -C parent add sub &&
+ git -C parent commit -m "update submodule" &&
+
+ git -C super -c submodule.recurse pull --no-recurse-submodules --no-rebase &&
+ test_path_is_missing super/sub/merge_strategy_3.t &&
+ git -C super -c submodule.recurse=false pull --recurse-submodules --no-rebase &&
+ test_path_is_file super/sub/merge_strategy_3.t &&
+
+ test_commit -C child merge_strategy_4 &&
+ git -C parent submodule update --remote &&
+ git -C parent add sub &&
+ git -C parent commit -m "update submodule" &&
+
+ git -C super -c submodule.recurse=false pull --no-recurse-submodules --no-rebase &&
+ test_path_is_missing super/sub/merge_strategy_4.t &&
+ git -C super -c submodule.recurse=true pull --recurse-submodules --no-rebase &&
+ test_path_is_file super/sub/merge_strategy_4.t
+'
+
+test_expect_success "fetch.recurseSubmodules option triggers recursive fetch (but not recursive update)" '
+ test_commit -C child merge_strategy_5 &&
+ # Omit the parent commit, otherwise this passes with the
+ # default "pull" behavior.
+
+ git -C super -c fetch.recursesubmodules=true pull --no-rebase &&
+ # Check that the submodule commit was fetched
+ sub_oid=$(git -C child rev-parse HEAD) &&
+ git -C super/sub cat-file -e $sub_oid &&
+ # Check that the submodule worktree did not update
+ test_path_is_missing super/sub/merge_strategy_5.t
+'
+
+test_expect_success "fetch.recurseSubmodules takes precedence over submodule.recurse" '
+ test_commit -C child merge_strategy_6 &&
+ # Omit the parent commit, otherwise this passes with the
+ # default "pull" behavior.
+
+ git -C super -c submodule.recurse=false -c fetch.recursesubmodules=true pull --no-rebase &&
+ # Check that the submodule commit was fetched
+ sub_oid=$(git -C child rev-parse HEAD) &&
+ git -C super/sub cat-file -e $sub_oid &&
+ # Check that the submodule worktree did not update
+ test_path_is_missing super/sub/merge_strategy_6.t
+'
+
+test_expect_success 'pull --rebase --recurse-submodules (remote superproject submodule changes, local submodule changes)' '
+ # This tests the following scenario :
+ # - local submodule has new commits
+ # - local superproject does not have new commits
+ # - upstream superproject has new commits that change the submodule pointer
+
+ # change upstream
+ test_commit -C child rebase_strategy &&
+ git -C parent submodule update --remote &&
+ git -C parent add sub &&
+ git -C parent commit -m "update submodule" &&
+
+ # also have local commits
+ test_commit -C super/sub local_stuff &&
+
+ git -C super pull --rebase --recurse-submodules &&
+ test_path_is_file super/sub/rebase_strategy.t &&
+ test_path_is_file super/sub/local_stuff.t
+'
+
+test_expect_success 'pull --rebase --recurse-submodules fails if both sides record submodule changes' '
+ # This tests the following scenario :
+ # - local superproject has new commits that change the submodule pointer
+ # - upstream superproject has new commits that change the submodule pointer
+
+ # local changes in submodule recorded in superproject:
+ test_commit -C super/sub local_stuff_2 &&
+ git -C super add sub &&
+ git -C super commit -m "local update submodule" &&
+
+ # and in the remote as well:
+ test_commit -C child important_upstream_work &&
+ git -C parent submodule update --remote &&
+ git -C parent add sub &&
+ git -C parent commit -m "remote update submodule" &&
+
+ # Unfortunately we fail here, despite no conflict in the
+ # submodule itself, but the merge strategy in submodules
+ # does not support rebase:
+ test_must_fail git -C super pull --rebase --recurse-submodules 2>err &&
+ test_grep "locally recorded submodule modifications" err
+'
+
+test_expect_success 'pull --rebase --recurse-submodules (no submodule changes, no fork-point)' '
+ # This tests the following scenario :
+ # - local submodule does not have new commits
+ # - local superproject has new commits that *do not* change the submodule pointer
+ # - upstream superproject has new commits that *do not* change the submodule pointer
+ # - local superproject branch has no fork-point with its remote-tracking counter-part
+
+ # create upstream superproject
+ test_create_repo submodule &&
+ test_commit -C submodule first_in_sub &&
+
+ test_create_repo superprojet &&
+ test_commit -C superprojet first_in_super &&
+ git -C superprojet submodule add ../submodule &&
+ git -C superprojet commit -m "add submodule" &&
+ test_commit -C superprojet third_in_super &&
+
+ # clone superproject
+ git clone --recurse-submodules superprojet superclone &&
+
+ # add commits upstream
+ test_commit -C superprojet fourth_in_super &&
+
+ # create topic branch in clone, not based on any remote-tracking branch
+ git -C superclone checkout -b feat HEAD~1 &&
+ test_commit -C superclone first_on_feat &&
+ git -C superclone pull --rebase --recurse-submodules origin HEAD
+'
+
+# NOTE:
+#
+# This test is particular because there is only a single commit in the upstream superproject
+# 'parent' (which adds the submodule 'a-submodule'). The clone of the superproject
+# ('child') hard-resets its branch to a new root commit with the same tree as the one
+# from the upstream superproject, so that its branch has no merge-base with its
+# remote-tracking counterpart, and then calls 'git pull --recurse-submodules --rebase'.
+# The result is that the local branch is reset to the remote-tracking branch (as it was
+# originally before the hard-reset).
+
+# The only commit in the range generated by 'submodule.c::submodule_touches_in_range' and
+# passed to 'submodule.c::collect_changed_submodules' is the new (regenerated) initial commit,
+# which adds the submodule.
+# However, 'submodule_touches_in_range' does not error (even though this commit adds the submodule)
+# because 'combine-diff.c::diff_tree_combined' returns early, as the initial commit has no parents.
+test_expect_success 'branch has no merge base with remote-tracking counterpart' '
+ rm -rf parent child &&
+
+ test_create_repo a-submodule &&
+ test_commit -C a-submodule foo &&
+
+ test_create_repo parent &&
+ git -C parent submodule add "$(pwd)/a-submodule" &&
+ git -C parent commit -m foo &&
+
+ git clone parent child &&
+
+ # Reset the current branch so that it has no merge base with
+ # the remote-tracking branch.
+ OTHER=$(git -C child commit-tree -m bar \
+ $(git -C child rev-parse HEAD^{tree})) &&
+ git -C child reset --hard "$OTHER" &&
+
+ git -C child pull --recurse-submodules --rebase
+'
+
+test_done
diff --git a/t/t5573-pull-verify-signatures.sh b/t/t5573-pull-verify-signatures.sh
new file mode 100755
index 0000000..ab05f38
--- /dev/null
+++ b/t/t5573-pull-verify-signatures.sh
@@ -0,0 +1,154 @@
+#!/bin/sh
+
+test_description='pull signature verification tests'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY/lib-gpg.sh"
+
+test_expect_success GPG 'create repositories with signed commits' '
+ echo 1 >a && git add a &&
+ test_tick && git commit -m initial &&
+ git tag initial &&
+
+ git clone . signed &&
+ (
+ cd signed &&
+ echo 2 >b && git add b &&
+ test_tick && git commit -S -m "signed"
+ ) &&
+
+ git clone . unsigned &&
+ (
+ cd unsigned &&
+ echo 3 >c && git add c &&
+ test_tick && git commit -m "unsigned"
+ ) &&
+
+ git clone . bad &&
+ (
+ cd bad &&
+ echo 4 >d && git add d &&
+ test_tick && git commit -S -m "bad" &&
+ git cat-file commit HEAD >raw &&
+ sed -e "s/^bad/forged bad/" raw >forged &&
+ git hash-object -w -t commit forged >forged.commit &&
+ git checkout $(cat forged.commit)
+ ) &&
+
+ git clone . untrusted &&
+ (
+ cd untrusted &&
+ echo 5 >e && git add e &&
+ test_tick && git commit -SB7227189 -m "untrusted"
+ )
+'
+
+test_expect_success GPG 'pull unsigned commit with --verify-signatures' '
+ test_when_finished "git reset --hard && git checkout initial" &&
+ test_must_fail git pull --ff-only --verify-signatures unsigned 2>pullerror &&
+ test_grep "does not have a GPG signature" pullerror
+'
+
+test_expect_success GPG 'pull commit with bad signature with --verify-signatures' '
+ test_when_finished "git reset --hard && git checkout initial" &&
+ test_must_fail git pull --ff-only --verify-signatures bad 2>pullerror &&
+ test_grep "has a bad GPG signature" pullerror
+'
+
+test_expect_success GPG 'pull commit with untrusted signature with --verify-signatures' '
+ test_when_finished "git reset --hard && git checkout initial" &&
+ test_must_fail git pull --ff-only --verify-signatures untrusted 2>pullerror &&
+ test_grep "has an untrusted GPG signature" pullerror
+'
+
+test_expect_success GPG 'pull commit with untrusted signature with --verify-signatures and minTrustLevel=ultimate' '
+ test_when_finished "git reset --hard && git checkout initial" &&
+ test_config gpg.minTrustLevel ultimate &&
+ test_must_fail git pull --ff-only --verify-signatures untrusted 2>pullerror &&
+ test_grep "has an untrusted GPG signature" pullerror
+'
+
+test_expect_success GPG 'pull commit with untrusted signature with --verify-signatures and minTrustLevel=marginal' '
+ test_when_finished "git reset --hard && git checkout initial" &&
+ test_config gpg.minTrustLevel marginal &&
+ test_must_fail git pull --ff-only --verify-signatures untrusted 2>pullerror &&
+ test_grep "has an untrusted GPG signature" pullerror
+'
+
+test_expect_success GPG 'pull commit with untrusted signature with --verify-signatures and minTrustLevel=undefined' '
+ test_when_finished "git reset --hard && git checkout initial" &&
+ test_config gpg.minTrustLevel undefined &&
+ git pull --ff-only --verify-signatures untrusted >pulloutput &&
+ test_grep "has a good GPG signature" pulloutput
+'
+
+test_expect_success GPG 'pull signed commit with --verify-signatures' '
+ test_when_finished "git reset --hard && git checkout initial" &&
+ git pull --verify-signatures signed >pulloutput &&
+ test_grep "has a good GPG signature" pulloutput
+'
+
+test_expect_success GPG 'pull commit with bad signature without verification' '
+ test_when_finished "git reset --hard && git checkout initial" &&
+ git pull --ff-only bad 2>pullerror
+'
+
+test_expect_success GPG 'pull commit with bad signature with --no-verify-signatures' '
+ test_when_finished "git reset --hard && git checkout initial" &&
+ test_config merge.verifySignatures true &&
+ test_config pull.verifySignatures true &&
+ git pull --ff-only --no-verify-signatures bad 2>pullerror
+'
+
+test_expect_success GPG 'pull unsigned commit into unborn branch' '
+ test_when_finished "rm -rf empty-repo" &&
+ git init empty-repo &&
+ test_must_fail \
+ git -C empty-repo pull --verify-signatures .. 2>pullerror &&
+ test_grep "does not have a GPG signature" pullerror
+'
+
+test_expect_success GPG 'pull commit into unborn branch with bad signature and --verify-signatures' '
+ test_when_finished "rm -rf empty-repo" &&
+ git init empty-repo &&
+ test_must_fail \
+ git -C empty-repo pull --ff-only --verify-signatures ../bad 2>pullerror &&
+ test_grep "has a bad GPG signature" pullerror
+'
+
+test_expect_success GPG 'pull commit into unborn branch with untrusted signature and --verify-signatures' '
+ test_when_finished "rm -rf empty-repo" &&
+ git init empty-repo &&
+ test_must_fail \
+ git -C empty-repo pull --ff-only --verify-signatures ../untrusted 2>pullerror &&
+ test_grep "has an untrusted GPG signature" pullerror
+'
+
+test_expect_success GPG 'pull commit into unborn branch with untrusted signature and --verify-signatures and minTrustLevel=ultimate' '
+ test_when_finished "rm -rf empty-repo" &&
+ git init empty-repo &&
+ test_config_global gpg.minTrustLevel ultimate &&
+ test_must_fail \
+ git -C empty-repo pull --ff-only --verify-signatures ../untrusted 2>pullerror &&
+ test_grep "has an untrusted GPG signature" pullerror
+'
+
+test_expect_success GPG 'pull commit into unborn branch with untrusted signature and --verify-signatures and minTrustLevel=marginal' '
+ test_when_finished "rm -rf empty-repo" &&
+ git init empty-repo &&
+ test_config_global gpg.minTrustLevel marginal &&
+ test_must_fail \
+ git -C empty-repo pull --ff-only --verify-signatures ../untrusted 2>pullerror &&
+ test_grep "has an untrusted GPG signature" pullerror
+'
+
+test_expect_success GPG 'pull commit into unborn branch with untrusted signature and --verify-signatures and minTrustLevel=undefined' '
+ test_when_finished "rm -rf empty-repo" &&
+ git init empty-repo &&
+ test_config_global gpg.minTrustLevel undefined &&
+ git -C empty-repo pull --ff-only --verify-signatures ../untrusted >pulloutput &&
+ test_grep "has a good GPG signature" pulloutput
+'
+
+test_done
diff --git a/t/t5574-fetch-output.sh b/t/t5574-fetch-output.sh
new file mode 100755
index 0000000..a9b06b2
--- /dev/null
+++ b/t/t5574-fetch-output.sh
@@ -0,0 +1,293 @@
+#!/bin/sh
+
+test_description='git fetch output format'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success 'fetch with invalid output format configuration' '
+ test_when_finished "rm -rf clone" &&
+ git clone . clone &&
+
+ test_must_fail git -C clone -c fetch.output fetch origin 2>actual.err &&
+ cat >expect <<-EOF &&
+ error: missing value for ${SQ}fetch.output${SQ}
+ fatal: unable to parse ${SQ}fetch.output${SQ} from command-line config
+ EOF
+ test_cmp expect actual.err &&
+
+ test_must_fail git -C clone -c fetch.output= fetch origin 2>actual.err &&
+ cat >expect <<-EOF &&
+ fatal: invalid value for ${SQ}fetch.output${SQ}: ${SQ}${SQ}
+ EOF
+ test_cmp expect actual.err &&
+
+ test_must_fail git -C clone -c fetch.output=garbage fetch origin 2>actual.err &&
+ cat >expect <<-EOF &&
+ fatal: invalid value for ${SQ}fetch.output${SQ}: ${SQ}garbage${SQ}
+ EOF
+ test_cmp expect actual.err
+'
+
+test_expect_success 'fetch aligned output' '
+ git clone . full-output &&
+ test_commit looooooooooooong-tag &&
+ (
+ cd full-output &&
+ git -c fetch.output=full fetch origin >actual 2>&1 &&
+ grep -e "->" actual | cut -c 22- >../actual
+ ) &&
+ cat >expect <<-\EOF &&
+ main -> origin/main
+ looooooooooooong-tag -> looooooooooooong-tag
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'fetch compact output' '
+ git clone . compact &&
+ test_commit extraaa &&
+ (
+ cd compact &&
+ git -c fetch.output=compact fetch origin >actual 2>&1 &&
+ grep -e "->" actual | cut -c 22- >../actual
+ ) &&
+ cat >expect <<-\EOF &&
+ main -> origin/*
+ extraaa -> *
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'fetch porcelain output' '
+ test_when_finished "rm -rf porcelain" &&
+
+ # Set up a bunch of references that we can use to demonstrate different
+ # kinds of flag symbols in the output format.
+ MAIN_OLD=$(git rev-parse HEAD) &&
+ git branch "fast-forward" &&
+ git branch "deleted-branch" &&
+ git checkout -b force-updated &&
+ test_commit --no-tag force-update-old &&
+ FORCE_UPDATED_OLD=$(git rev-parse HEAD) &&
+ git checkout main &&
+
+ # Clone and pre-seed the repositories. We fetch references into two
+ # namespaces so that we can test that rejected and force-updated
+ # references are reported properly.
+ refspecs="refs/heads/*:refs/unforced/* +refs/heads/*:refs/forced/*" &&
+ git clone . porcelain &&
+ git -C porcelain fetch origin $refspecs &&
+
+ # Now that we have set up the client repositories we can change our
+ # local references.
+ git branch new-branch &&
+ git branch -d deleted-branch &&
+ git checkout fast-forward &&
+ test_commit --no-tag fast-forward-new &&
+ FAST_FORWARD_NEW=$(git rev-parse HEAD) &&
+ git checkout force-updated &&
+ git reset --hard HEAD~ &&
+ test_commit --no-tag force-update-new &&
+ FORCE_UPDATED_NEW=$(git rev-parse HEAD) &&
+
+ cat >expect <<-EOF &&
+ - $MAIN_OLD $ZERO_OID refs/forced/deleted-branch
+ - $MAIN_OLD $ZERO_OID refs/unforced/deleted-branch
+ $MAIN_OLD $FAST_FORWARD_NEW refs/unforced/fast-forward
+ ! $FORCE_UPDATED_OLD $FORCE_UPDATED_NEW refs/unforced/force-updated
+ * $ZERO_OID $MAIN_OLD refs/unforced/new-branch
+ $MAIN_OLD $FAST_FORWARD_NEW refs/forced/fast-forward
+ + $FORCE_UPDATED_OLD $FORCE_UPDATED_NEW refs/forced/force-updated
+ * $ZERO_OID $MAIN_OLD refs/forced/new-branch
+ $MAIN_OLD $FAST_FORWARD_NEW refs/remotes/origin/fast-forward
+ + $FORCE_UPDATED_OLD $FORCE_UPDATED_NEW refs/remotes/origin/force-updated
+ * $ZERO_OID $MAIN_OLD refs/remotes/origin/new-branch
+ EOF
+
+ # Execute a dry-run fetch first. We do this to assert that the dry-run
+ # and non-dry-run fetches produces the same output. Execution of the
+ # fetch is expected to fail as we have a rejected reference update.
+ test_must_fail git -C porcelain fetch \
+ --porcelain --dry-run --prune origin $refspecs >actual &&
+ test_cmp expect actual &&
+
+ # And now we perform a non-dry-run fetch.
+ test_must_fail git -C porcelain fetch \
+ --porcelain --prune origin $refspecs >actual 2>stderr &&
+ test_cmp expect actual &&
+ test_must_be_empty stderr
+'
+
+test_expect_success 'fetch porcelain with multiple remotes' '
+ test_when_finished "rm -rf porcelain" &&
+
+ git switch --create multiple-remotes &&
+ git clone . porcelain &&
+ git -C porcelain remote add second-remote "$PWD" &&
+ git -C porcelain fetch second-remote &&
+
+ test_commit --no-tag multi-commit &&
+ old_commit=$(git rev-parse HEAD~) &&
+ new_commit=$(git rev-parse HEAD) &&
+
+ cat >expect <<-EOF &&
+ $old_commit $new_commit refs/remotes/origin/multiple-remotes
+ $old_commit $new_commit refs/remotes/second-remote/multiple-remotes
+ EOF
+
+ git -C porcelain fetch --porcelain --all >actual 2>stderr &&
+ test_cmp expect actual &&
+ test_must_be_empty stderr
+'
+
+test_expect_success 'fetch porcelain refuses to work with submodules' '
+ test_when_finished "rm -rf porcelain" &&
+
+ cat >expect <<-EOF &&
+ fatal: options ${SQ}--porcelain${SQ} and ${SQ}--recurse-submodules${SQ} cannot be used together
+ EOF
+
+ git init porcelain &&
+ test_must_fail git -C porcelain fetch --porcelain --recurse-submodules=yes 2>stderr &&
+ test_cmp expect stderr &&
+
+ test_must_fail git -C porcelain fetch --porcelain --recurse-submodules=on-demand 2>stderr &&
+ test_cmp expect stderr
+'
+
+test_expect_success 'fetch porcelain overrides fetch.output config' '
+ test_when_finished "rm -rf porcelain" &&
+
+ git switch --create config-override &&
+ git clone . porcelain &&
+ test_commit new-commit &&
+ old_commit=$(git rev-parse HEAD~) &&
+ new_commit=$(git rev-parse HEAD) &&
+
+ cat >expect <<-EOF &&
+ $old_commit $new_commit refs/remotes/origin/config-override
+ * $ZERO_OID $new_commit refs/tags/new-commit
+ EOF
+
+ git -C porcelain -c fetch.output=compact fetch --porcelain >stdout 2>stderr &&
+ test_must_be_empty stderr &&
+ test_cmp expect stdout
+'
+
+test_expect_success 'fetch --no-porcelain overrides previous --porcelain' '
+ test_when_finished "rm -rf no-porcelain" &&
+
+ git switch --create no-porcelain &&
+ git clone . no-porcelain &&
+ test_commit --no-tag no-porcelain &&
+ old_commit=$(git rev-parse --short HEAD~) &&
+ new_commit=$(git rev-parse --short HEAD) &&
+
+ cat >expect <<-EOF &&
+ From $(test-tool path-utils real_path .)/.
+ $old_commit..$new_commit no-porcelain -> origin/no-porcelain
+ EOF
+
+ git -C no-porcelain fetch --porcelain --no-porcelain >stdout 2>stderr &&
+ test_cmp expect stderr &&
+ test_must_be_empty stdout
+'
+
+test_expect_success 'fetch output with HEAD' '
+ test_when_finished "rm -rf head" &&
+ git clone . head &&
+
+ git -C head fetch --dry-run origin HEAD >actual.out 2>actual.err &&
+ cat >expect <<-EOF &&
+ From $(test-tool path-utils real_path .)/.
+ * branch HEAD -> FETCH_HEAD
+ EOF
+ test_must_be_empty actual.out &&
+ test_cmp expect actual.err &&
+
+ git -C head fetch origin HEAD >actual.out 2>actual.err &&
+ test_must_be_empty actual.out &&
+ test_cmp expect actual.err &&
+
+ git -C head fetch --dry-run origin HEAD:foo >actual.out 2>actual.err &&
+ cat >expect <<-EOF &&
+ From $(test-tool path-utils real_path .)/.
+ * [new ref] HEAD -> foo
+ EOF
+ test_must_be_empty actual.out &&
+ test_cmp expect actual.err &&
+
+ git -C head fetch origin HEAD:foo >actual.out 2>actual.err &&
+ test_must_be_empty actual.out &&
+ test_cmp expect actual.err
+'
+
+test_expect_success 'fetch porcelain output with HEAD' '
+ test_when_finished "rm -rf head" &&
+ git clone . head &&
+ COMMIT_ID=$(git rev-parse HEAD) &&
+
+ git -C head fetch --porcelain --dry-run origin HEAD >actual &&
+ cat >expect <<-EOF &&
+ * $ZERO_OID $COMMIT_ID FETCH_HEAD
+ EOF
+ test_cmp expect actual &&
+
+ git -C head fetch --porcelain origin HEAD >actual &&
+ test_cmp expect actual &&
+
+ git -C head fetch --porcelain --dry-run origin HEAD:foo >actual &&
+ cat >expect <<-EOF &&
+ * $ZERO_OID $COMMIT_ID refs/heads/foo
+ EOF
+ test_cmp expect actual &&
+
+ git -C head fetch --porcelain origin HEAD:foo >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'fetch output with object ID' '
+ test_when_finished "rm -rf object-id" &&
+ git clone . object-id &&
+ commit=$(git rev-parse HEAD) &&
+
+ git -C object-id fetch --dry-run origin $commit:object-id >actual.out 2>actual.err &&
+ cat >expect <<-EOF &&
+ From $(test-tool path-utils real_path .)/.
+ * [new ref] $commit -> object-id
+ EOF
+ test_must_be_empty actual.out &&
+ test_cmp expect actual.err &&
+
+ git -C object-id fetch origin $commit:object-id >actual.out 2>actual.err &&
+ test_must_be_empty actual.out &&
+ test_cmp expect actual.err
+'
+
+test_expect_success '--no-show-forced-updates' '
+ mkdir forced-updates &&
+ (
+ cd forced-updates &&
+ git init &&
+ test_commit 1 &&
+ test_commit 2
+ ) &&
+ git clone forced-updates forced-update-clone &&
+ git clone forced-updates no-forced-update-clone &&
+ git -C forced-updates reset --hard HEAD~1 &&
+ (
+ cd forced-update-clone &&
+ git fetch --show-forced-updates origin 2>output &&
+ test_grep "(forced update)" output
+ ) &&
+ (
+ cd no-forced-update-clone &&
+ git fetch --no-show-forced-updates origin 2>output &&
+ test_grep ! "(forced update)" output
+ )
+'
+
+test_done
diff --git a/t/t5580-unc-paths.sh b/t/t5580-unc-paths.sh
new file mode 100755
index 0000000..d7537a1
--- /dev/null
+++ b/t/t5580-unc-paths.sh
@@ -0,0 +1,93 @@
+#!/bin/sh
+
+test_description='various Windows-only path tests'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+if test_have_prereq CYGWIN
+then
+ alias winpwd='cygpath -aw .'
+elif test_have_prereq MINGW
+then
+ alias winpwd=pwd
+else
+ skip_all='skipping Windows-only path tests'
+ test_done
+fi
+
+UNCPATH="$(winpwd)"
+case "$UNCPATH" in
+[A-Z]:*)
+ # Use administrative share e.g. \\localhost\C$\git-sdk-64\usr\src\git
+ # (we use forward slashes here because MSYS2 and Git accept them, and
+ # they are easier on the eyes)
+ UNCPATH="//localhost/${UNCPATH%%:*}\$/${UNCPATH#?:}"
+ test -d "$UNCPATH" || {
+ skip_all='could not access administrative share; skipping'
+ test_done
+ }
+ ;;
+*)
+ skip_all='skipping UNC path tests, cannot determine current path as UNC'
+ test_done
+ ;;
+esac
+
+test_expect_success setup '
+ test_commit initial
+'
+
+test_expect_success clone '
+ git clone "file://$UNCPATH" clone
+'
+
+test_expect_success 'clone without file://' '
+ git clone "$UNCPATH" clone-without-file
+'
+
+test_expect_success 'clone with backslashed path' '
+ BACKSLASHED="$(echo "$UNCPATH" | tr / \\\\)" &&
+ git clone "$BACKSLASHED" backslashed
+'
+
+test_expect_success fetch '
+ git init to-fetch &&
+ (
+ cd to-fetch &&
+ git fetch "$UNCPATH" main
+ )
+'
+
+test_expect_success push '
+ (
+ cd clone &&
+ git checkout -b to-push &&
+ test_commit to-push &&
+ git push origin HEAD
+ ) &&
+ rev="$(git -C clone rev-parse --verify refs/heads/to-push)" &&
+ test "$rev" = "$(git rev-parse --verify refs/heads/to-push)"
+'
+
+test_expect_success MINGW 'remote nick cannot contain backslashes' '
+ BACKSLASHED="$(winpwd | tr / \\\\)" &&
+ git ls-remote "$BACKSLASHED" 2>err &&
+ test_grep ! "unable to access" err
+'
+
+test_expect_success 'unc alternates' '
+ tree="$(git rev-parse HEAD:)" &&
+ mkdir test-unc-alternate &&
+ (
+ cd test-unc-alternate &&
+ git init &&
+ test_must_fail git show $tree &&
+ echo "$UNCPATH/.git/objects" >.git/objects/info/alternates &&
+ git show $tree
+ )
+'
+
+test_done
diff --git a/t/t5581-http-curl-verbose.sh b/t/t5581-http-curl-verbose.sh
new file mode 100755
index 0000000..cded79c
--- /dev/null
+++ b/t/t5581-http-curl-verbose.sh
@@ -0,0 +1,29 @@
+#!/bin/sh
+
+test_description='test GIT_CURL_VERBOSE'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-httpd.sh
+start_httpd
+
+test_expect_success 'setup repository' '
+ mkdir "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
+ git -C "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" --bare init &&
+ git config push.default matching &&
+ echo content >file &&
+ git add file &&
+ git commit -m one &&
+ git remote add public "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
+ git push public main:main
+'
+
+test_expect_success 'failure in git-upload-pack is shown' '
+ test_might_fail env GIT_CURL_VERBOSE=1 \
+ git clone "$HTTPD_URL/error_git_upload_pack/smart/repo.git" \
+ 2>curl_log &&
+ grep "<= Recv header: HTTP/1.1 500 Intentional Breakage" curl_log
+'
+
+test_done
diff --git a/t/t5582-fetch-negative-refspec.sh b/t/t5582-fetch-negative-refspec.sh
new file mode 100755
index 0000000..7a80e47
--- /dev/null
+++ b/t/t5582-fetch-negative-refspec.sh
@@ -0,0 +1,285 @@
+#!/bin/sh
+# Copyright (c) 2020, Jacob Keller.
+
+test_description='"git fetch" with negative refspecs.
+
+'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success setup '
+ echo >file original &&
+ git add file &&
+ git commit -a -m original
+'
+
+test_expect_success "clone and setup child repos" '
+ git clone . one &&
+ (
+ cd one &&
+ echo >file updated by one &&
+ git commit -a -m "updated by one" &&
+ git switch -c alternate &&
+ echo >file updated again by one &&
+ git commit -a -m "updated by one again" &&
+ git switch main
+ ) &&
+ git clone . two &&
+ (
+ cd two &&
+ git config branch.main.remote one &&
+ git config remote.one.url ../one/.git/ &&
+ git config remote.one.fetch +refs/heads/*:refs/remotes/one/* &&
+ git config --add remote.one.fetch ^refs/heads/alternate
+ ) &&
+ git clone . three
+'
+
+test_expect_success "fetch one" '
+ echo >file updated by origin &&
+ git commit -a -m "updated by origin" &&
+ (
+ cd two &&
+ test_must_fail git rev-parse --verify refs/remotes/one/alternate &&
+ git fetch one &&
+ test_must_fail git rev-parse --verify refs/remotes/one/alternate &&
+ git rev-parse --verify refs/remotes/one/main &&
+ mine=$(git rev-parse refs/remotes/one/main) &&
+ his=$(cd ../one && git rev-parse refs/heads/main) &&
+ test "z$mine" = "z$his"
+ )
+'
+
+test_expect_success "fetch with negative refspec on commandline" '
+ echo >file updated by origin again &&
+ git commit -a -m "updated by origin again" &&
+ (
+ cd three &&
+ alternate_in_one=$(cd ../one && git rev-parse refs/heads/alternate) &&
+ echo $alternate_in_one >expect &&
+ git fetch ../one/.git refs/heads/*:refs/remotes/one/* ^refs/heads/main &&
+ cut -f -1 .git/FETCH_HEAD >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success "fetch with negative sha1 refspec fails" '
+ echo >file updated by origin yet again &&
+ git commit -a -m "updated by origin yet again" &&
+ (
+ cd three &&
+ main_in_one=$(cd ../one && git rev-parse refs/heads/main) &&
+ test_must_fail git fetch ../one/.git refs/heads/*:refs/remotes/one/* ^$main_in_one
+ )
+'
+
+test_expect_success "fetch with negative pattern refspec" '
+ echo >file updated by origin once more &&
+ git commit -a -m "updated by origin once more" &&
+ (
+ cd three &&
+ alternate_in_one=$(cd ../one && git rev-parse refs/heads/alternate) &&
+ echo $alternate_in_one >expect &&
+ git fetch ../one/.git refs/heads/*:refs/remotes/one/* ^refs/heads/m* &&
+ cut -f -1 .git/FETCH_HEAD >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success "fetch with negative pattern refspec does not expand prefix" '
+ echo >file updated by origin another time &&
+ git commit -a -m "updated by origin another time" &&
+ (
+ cd three &&
+ alternate_in_one=$(cd ../one && git rev-parse refs/heads/alternate) &&
+ main_in_one=$(cd ../one && git rev-parse refs/heads/main) &&
+ echo $alternate_in_one >expect &&
+ echo $main_in_one >>expect &&
+ git fetch ../one/.git refs/heads/*:refs/remotes/one/* ^main &&
+ cut -f -1 .git/FETCH_HEAD >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success "fetch with negative refspec avoids duplicate conflict" '
+ (
+ cd one &&
+ git branch dups/a &&
+ git branch dups/b &&
+ git branch dups/c &&
+ git branch other/a &&
+ git rev-parse --verify refs/heads/other/a >../expect &&
+ git rev-parse --verify refs/heads/dups/b >>../expect &&
+ git rev-parse --verify refs/heads/dups/c >>../expect
+ ) &&
+ (
+ cd three &&
+ git fetch ../one/.git ^refs/heads/dups/a refs/heads/dups/*:refs/dups/* refs/heads/other/a:refs/dups/a &&
+ git rev-parse --verify refs/dups/a >../actual &&
+ git rev-parse --verify refs/dups/b >>../actual &&
+ git rev-parse --verify refs/dups/c >>../actual
+ ) &&
+ test_cmp expect actual
+'
+
+test_expect_success "push --prune with negative refspec" '
+ (
+ cd two &&
+ git branch prune/a &&
+ git branch prune/b &&
+ git branch prune/c &&
+ git push ../three refs/heads/prune/* &&
+ git branch -d prune/a &&
+ git branch -d prune/b &&
+ git push --prune ../three refs/heads/prune/* ^refs/heads/prune/b
+ ) &&
+ (
+ cd three &&
+ test_write_lines b c >expect &&
+ git for-each-ref --format="%(refname:lstrip=3)" refs/heads/prune/ >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success "push --prune with negative refspec apply to the destination" '
+ (
+ cd two &&
+ git branch ours/a &&
+ git branch ours/b &&
+ git branch ours/c &&
+ git push ../three refs/heads/ours/*:refs/heads/theirs/* &&
+ git branch -d ours/a &&
+ git branch -d ours/b &&
+ git push --prune ../three refs/heads/ours/*:refs/heads/theirs/* ^refs/heads/theirs/b
+ ) &&
+ (
+ cd three &&
+ test_write_lines b c >expect &&
+ git for-each-ref --format="%(refname:lstrip=3)" refs/heads/theirs/ >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success "fetch --prune with negative refspec" '
+ (
+ cd two &&
+ git branch fetch/a &&
+ git branch fetch/b &&
+ git branch fetch/c
+ ) &&
+ (
+ cd three &&
+ git fetch ../two/.git refs/heads/fetch/*:refs/heads/copied/*
+ ) &&
+ (
+ cd two &&
+ git branch -d fetch/a &&
+ git branch -d fetch/b
+ ) &&
+ (
+ cd three &&
+ test_write_lines b c >expect &&
+ git fetch -v ../two/.git --prune refs/heads/fetch/*:refs/heads/copied/* ^refs/heads/fetch/b &&
+ git for-each-ref --format="%(refname:lstrip=3)" refs/heads/copied/ >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success "push with matching : and negative refspec" '
+ # Manually handle cleanup, since test_config is not
+ # prepared to take arbitrary options like --add
+ test_when_finished "test_unconfig -C two remote.one.push" &&
+
+ # For convenience, we use "master" to refer to the name of
+ # the branch created by default in the following.
+ #
+ # Repositories two and one have branches other than "master"
+ # but they have no overlap---"master" is the only one that
+ # is shared between them. And the master branch at two is
+ # behind the master branch at one by one commit.
+ git -C two config --add remote.one.push : &&
+
+ # A matching push tries to update master, fails due to non-ff
+ test_must_fail git -C two push one &&
+
+ # "master" may actually not be "master"---find it out.
+ current=$(git symbolic-ref HEAD) &&
+
+ # If master is in negative refspec, then the command will not attempt
+ # to push and succeed.
+ git -C two config --add remote.one.push "^$current" &&
+
+ # With "master" excluded, this push is a no-op. Nothing gets
+ # pushed and it succeeds.
+ git -C two push -v one
+'
+
+test_expect_success "push with matching +: and negative refspec" '
+ test_when_finished "test_unconfig -C two remote.one.push" &&
+
+ # The same set-up as above, whose side-effect was a no-op.
+ git -C two config --add remote.one.push +: &&
+
+ # The push refuses to update the "master" branch that is checked
+ # out in the "one" repository, even when it is forced with +:
+ test_must_fail git -C two push one &&
+
+ # "master" may actually not be "master"---find it out.
+ current=$(git symbolic-ref HEAD) &&
+
+ # If master is in negative refspec, then the command will not attempt
+ # to push and succeed
+ git -C two config --add remote.one.push "^$current" &&
+
+ # With "master" excluded, this push is a no-op. Nothing gets
+ # pushed and it succeeds.
+ git -C two push -v one
+'
+
+test_expect_success '--prefetch correctly modifies refspecs' '
+ git -C one config --unset-all remote.origin.fetch &&
+ git -C one config --add remote.origin.fetch ^refs/heads/bogus/ignore &&
+ git -C one config --add remote.origin.fetch "refs/tags/*:refs/tags/*" &&
+ git -C one config --add remote.origin.fetch "refs/heads/bogus/*:bogus/*" &&
+
+ git tag -a -m never never-fetch-tag HEAD &&
+
+ git branch bogus/fetched HEAD~1 &&
+ git branch bogus/ignore HEAD &&
+
+ git -C one fetch --prefetch --no-tags &&
+ test_must_fail git -C one rev-parse never-fetch-tag &&
+ git -C one rev-parse refs/prefetch/bogus/fetched &&
+ test_must_fail git -C one rev-parse refs/prefetch/bogus/ignore &&
+
+ # correctly handle when refspec set becomes empty
+ # after removing the refs/tags/* refspec.
+ git -C one config --unset-all remote.origin.fetch &&
+ git -C one config --add remote.origin.fetch "refs/tags/*:refs/tags/*" &&
+
+ git -C one fetch --prefetch --no-tags &&
+ test_must_fail git -C one rev-parse never-fetch-tag &&
+
+ # The refspec for refs that are not fully qualified
+ # are filtered multiple times.
+ git -C one rev-parse refs/prefetch/bogus/fetched &&
+ test_must_fail git -C one rev-parse refs/prefetch/bogus/ignore
+'
+
+test_expect_success '--prefetch succeeds when refspec becomes empty' '
+ git checkout bogus/fetched &&
+ test_commit extra &&
+
+ git -C one config --unset-all remote.origin.fetch &&
+ git -C one config --unset branch.main.remote &&
+ git -C one config remote.origin.fetch "+refs/tags/extra" &&
+ git -C one config remote.origin.skipfetchall true &&
+ git -C one config remote.origin.tagopt "--no-tags" &&
+
+ git -C one fetch --prefetch
+'
+
+test_done
diff --git a/t/t5583-push-branches.sh b/t/t5583-push-branches.sh
new file mode 100755
index 0000000..320f49c
--- /dev/null
+++ b/t/t5583-push-branches.sh
@@ -0,0 +1,116 @@
+#!/bin/sh
+
+test_description='check the consisitency of behavior of --all and --branches'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+delete_refs() {
+ dir=$1
+ shift
+ rm -rf deletes
+ for arg in $*
+ do
+ echo "delete ${arg}" >>deletes
+ done
+ git -C $dir update-ref --stdin < deletes
+}
+
+test_expect_success 'setup bare remote' '
+ git init --bare remote-1 &&
+ git -C remote-1 config gc.auto 0 &&
+ test_commit one &&
+ git push remote-1 HEAD
+'
+
+test_expect_success 'setup different types of references' '
+ cat >refs <<-EOF &&
+ update refs/heads/branch-1 HEAD
+ update refs/heads/branch-2 HEAD
+ EOF
+
+ git tag -a -m "annotated" annotated-1 HEAD &&
+ git tag -a -m "annotated" annotated-2 HEAD &&
+ git update-ref --stdin < refs
+'
+
+test_expect_success '--all and --branches have the same behavior' '
+ test_when_finished "delete_refs remote-1 \
+ refs/heads/branch-1 \
+ refs/heads/branch-2" &&
+ git push remote-1 --all &&
+ commit=$(git rev-parse HEAD) &&
+ cat >expect <<-EOF &&
+ $commit refs/heads/branch-1
+ $commit refs/heads/branch-2
+ $commit refs/heads/main
+ EOF
+
+ git -C remote-1 show-ref --heads >actual.all &&
+ delete_refs remote-1 refs/heads/branch-1 refs/heads/branch-2 &&
+ git push remote-1 --branches &&
+ git -C remote-1 show-ref --heads >actual.branches &&
+ test_cmp actual.all actual.branches &&
+ test_cmp expect actual.all
+'
+
+test_expect_success '--all or --branches can not be combined with refspecs' '
+ test_must_fail git push remote-1 --all main >actual.all 2>&1 &&
+ test_must_fail git push remote-1 --branches main >actual.branches 2>&1 &&
+ test_cmp actual.all actual.branches &&
+ grep "be combined with refspecs" actual.all
+'
+
+test_expect_success '--all or --branches can not be combined with --mirror' '
+ test_must_fail git push remote-1 --all --mirror >actual.all 2>&1 &&
+ test_must_fail git push remote-1 --branches --mirror >actual.branches 2>&1 &&
+ test_cmp actual.all actual.branches &&
+ grep "cannot be used together" actual.all
+'
+
+test_expect_success '--all or --branches can not be combined with --tags' '
+ test_must_fail git push remote-1 --all --tags >actual.all 2>&1 &&
+ test_must_fail git push remote-1 --branches --tags >actual.branches 2>&1 &&
+ test_cmp actual.all actual.branches &&
+ grep "cannot be used together" actual.all
+'
+
+
+test_expect_success '--all or --branches can not be combined with --delete' '
+ test_must_fail git push remote-1 --all --delete >actual.all 2>&1 &&
+ test_must_fail git push remote-1 --branches --delete >actual.branches 2>&1 &&
+ test_cmp actual.all actual.branches &&
+ grep "cannot be used together" actual.all
+'
+
+test_expect_success '--all or --branches combines with --follow-tags have same behavior' '
+ test_when_finished "delete_refs remote-1 \
+ refs/heads/branch-1 \
+ refs/heads/branch-2 \
+ refs/tags/annotated-1 \
+ refs/tags/annotated-2" &&
+ git push remote-1 --all --follow-tags &&
+ git -C remote-1 show-ref > actual.all &&
+ cat >expect <<-EOF &&
+ $commit refs/heads/branch-1
+ $commit refs/heads/branch-2
+ $commit refs/heads/main
+ $(git rev-parse annotated-1) refs/tags/annotated-1
+ $(git rev-parse annotated-2) refs/tags/annotated-2
+ EOF
+
+ delete_refs remote-1 \
+ refs/heads/branch-1 \
+ refs/heads/branch-2 \
+ refs/tags/annotated-1 \
+ refs/tags/annotated-2 &&
+ git push remote-1 --branches --follow-tags &&
+ git -C remote-1 show-ref >actual.branches &&
+ test_cmp actual.all actual.branches &&
+ test_cmp expect actual.all
+'
+
+test_done
diff --git a/t/t5600-clone-fail-cleanup.sh b/t/t5600-clone-fail-cleanup.sh
new file mode 100755
index 0000000..c814afa
--- /dev/null
+++ b/t/t5600-clone-fail-cleanup.sh
@@ -0,0 +1,110 @@
+#!/bin/sh
+#
+# Copyright (C) 2006 Carl D. Worth <cworth@cworth.org>
+#
+
+test_description='test git clone to cleanup after failure
+
+This test covers the fact that if git clone fails, it should remove
+the directory it created, to avoid the user having to manually
+remove the directory before attempting a clone again.
+
+Unless the directory already exists, in which case we clean up only what we
+wrote.
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+corrupt_repo () {
+ test_when_finished "rmdir foo/.git/objects.bak" &&
+ mkdir foo/.git/objects.bak/ &&
+ test_when_finished "mv foo/.git/objects.bak/* foo/.git/objects/" &&
+ mv foo/.git/objects/* foo/.git/objects.bak/
+}
+
+test_expect_success 'clone of non-existent source should fail' '
+ test_must_fail git clone foo bar
+'
+
+test_expect_success 'failed clone should not leave a directory' '
+ test_path_is_missing bar
+'
+
+test_expect_success 'create a repo to clone' '
+ test_create_repo foo
+'
+
+test_expect_success 'create objects in repo for later corruption' '
+ test_commit -C foo file &&
+ git -C foo checkout --detach &&
+ test_commit -C foo detached
+'
+
+# source repository given to git clone should be relative to the
+# current path not to the target dir
+test_expect_success 'clone of non-existent (relative to $PWD) source should fail' '
+ test_must_fail git clone ../foo baz
+'
+
+test_expect_success 'clone should work now that source exists' '
+ git clone foo bar
+'
+
+test_expect_success 'successful clone must leave the directory' '
+ test_path_is_dir bar
+'
+
+test_expect_success 'failed clone --separate-git-dir should not leave any directories' '
+ corrupt_repo &&
+ test_must_fail git clone --separate-git-dir gitdir foo worktree &&
+ test_path_is_missing gitdir &&
+ test_path_is_missing worktree
+'
+
+test_expect_success 'failed clone into empty leaves directory (vanilla)' '
+ mkdir -p empty &&
+ corrupt_repo &&
+ test_must_fail git clone foo empty &&
+ test_dir_is_empty empty
+'
+
+test_expect_success 'failed clone into empty leaves directory (bare)' '
+ mkdir -p empty &&
+ corrupt_repo &&
+ test_must_fail git clone --bare foo empty &&
+ test_dir_is_empty empty
+'
+
+test_expect_success 'failed clone into empty leaves directory (separate)' '
+ mkdir -p empty-git empty-wt &&
+ corrupt_repo &&
+ test_must_fail git clone --separate-git-dir empty-git foo empty-wt &&
+ test_dir_is_empty empty-git &&
+ test_dir_is_empty empty-wt
+'
+
+test_expect_success 'failed clone into empty leaves directory (separate, git)' '
+ mkdir -p empty-git &&
+ corrupt_repo &&
+ test_must_fail git clone --separate-git-dir empty-git foo no-wt &&
+ test_dir_is_empty empty-git &&
+ test_path_is_missing no-wt
+'
+
+test_expect_success 'failed clone into empty leaves directory (separate, wt)' '
+ mkdir -p empty-wt &&
+ corrupt_repo &&
+ test_must_fail git clone --separate-git-dir no-git foo empty-wt &&
+ test_path_is_missing no-git &&
+ test_dir_is_empty empty-wt
+'
+
+test_expect_success 'transport failure cleans up directory' '
+ test_must_fail git clone --no-local \
+ -u "f() { git-upload-pack \"\$@\"; return 1; }; f" \
+ foo broken-clone &&
+ test_path_is_missing broken-clone
+'
+
+test_done
diff --git a/t/t5601-clone.sh b/t/t5601-clone.sh
new file mode 100755
index 0000000..47eae64
--- /dev/null
+++ b/t/t5601-clone.sh
@@ -0,0 +1,883 @@
+#!/bin/sh
+
+test_description=clone
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+X=
+test_have_prereq !MINGW || X=.exe
+
+test_expect_success setup '
+
+ rm -fr .git &&
+ test_create_repo src &&
+ (
+ cd src &&
+ >file &&
+ git add file &&
+ git commit -m initial &&
+ echo 1 >file &&
+ git add file &&
+ git commit -m updated
+ )
+
+'
+
+test_expect_success 'clone with excess parameters (1)' '
+
+ rm -fr dst &&
+ test_must_fail git clone -n src dst junk
+
+'
+
+test_expect_success 'clone with excess parameters (2)' '
+
+ rm -fr dst &&
+ test_must_fail git clone -n "file://$(pwd)/src" dst junk
+
+'
+
+test_expect_success 'output from clone' '
+ rm -fr dst &&
+ git clone -n "file://$(pwd)/src" dst >output 2>&1 &&
+ test $(grep Clon output | wc -l) = 1
+'
+
+test_expect_success 'clone does not keep pack' '
+
+ rm -fr dst &&
+ git clone -n "file://$(pwd)/src" dst &&
+ ! test -f dst/file &&
+ ! (echo dst/.git/objects/pack/pack-* | grep "\.keep")
+
+'
+
+test_expect_success 'clone checks out files' '
+
+ rm -fr dst &&
+ git clone src dst &&
+ test -f dst/file
+
+'
+
+test_expect_success 'clone respects GIT_WORK_TREE' '
+
+ GIT_WORK_TREE=worktree git clone src bare &&
+ test -f bare/config &&
+ test -f worktree/file
+
+'
+
+test_expect_success 'clone from hooks' '
+
+ test_create_repo r0 &&
+ cd r0 &&
+ test_commit initial &&
+ cd .. &&
+ git init r1 &&
+ cd r1 &&
+ test_hook pre-commit <<-\EOF &&
+ git clone ../r0 ../r2
+ exit 1
+ EOF
+ : >file &&
+ git add file &&
+ test_must_fail git commit -m invoke-hook &&
+ cd .. &&
+ test_cmp r0/.git/HEAD r2/.git/HEAD &&
+ test_cmp r0/initial.t r2/initial.t
+
+'
+
+test_expect_success 'clone creates intermediate directories' '
+
+ git clone src long/path/to/dst &&
+ test -f long/path/to/dst/file
+
+'
+
+test_expect_success 'clone creates intermediate directories for bare repo' '
+
+ git clone --bare src long/path/to/bare/dst &&
+ test -f long/path/to/bare/dst/config
+
+'
+
+test_expect_success 'clone --mirror' '
+
+ git clone --mirror src mirror &&
+ test -f mirror/HEAD &&
+ test ! -f mirror/file &&
+ FETCH="$(cd mirror && git config remote.origin.fetch)" &&
+ test "+refs/*:refs/*" = "$FETCH" &&
+ MIRROR="$(cd mirror && git config --bool remote.origin.mirror)" &&
+ test "$MIRROR" = true
+
+'
+
+test_expect_success 'clone --mirror with detached HEAD' '
+
+ ( cd src && git checkout HEAD^ && git rev-parse HEAD >../expected ) &&
+ git clone --mirror src mirror.detached &&
+ ( cd src && git checkout - ) &&
+ GIT_DIR=mirror.detached git rev-parse HEAD >actual &&
+ test_cmp expected actual
+
+'
+
+test_expect_success 'clone --bare with detached HEAD' '
+
+ ( cd src && git checkout HEAD^ && git rev-parse HEAD >../expected ) &&
+ git clone --bare src bare.detached &&
+ ( cd src && git checkout - ) &&
+ GIT_DIR=bare.detached git rev-parse HEAD >actual &&
+ test_cmp expected actual
+
+'
+
+test_expect_success 'clone --bare names the local repository <name>.git' '
+
+ git clone --bare src &&
+ test -d src.git
+
+'
+
+test_expect_success 'clone --mirror does not repeat tags' '
+
+ (cd src &&
+ git tag some-tag HEAD) &&
+ git clone --mirror src mirror2 &&
+ (cd mirror2 &&
+ git show-ref 2> clone.err > clone.out) &&
+ ! grep Duplicate mirror2/clone.err &&
+ grep some-tag mirror2/clone.out
+
+'
+
+test_expect_success 'clone to destination with trailing /' '
+
+ git clone src target-1/ &&
+ T=$( cd target-1 && git rev-parse HEAD ) &&
+ S=$( cd src && git rev-parse HEAD ) &&
+ test "$T" = "$S"
+
+'
+
+test_expect_success 'clone to destination with extra trailing /' '
+
+ git clone src target-2/// &&
+ T=$( cd target-2 && git rev-parse HEAD ) &&
+ S=$( cd src && git rev-parse HEAD ) &&
+ test "$T" = "$S"
+
+'
+
+test_expect_success 'clone to an existing empty directory' '
+ mkdir target-3 &&
+ git clone src target-3 &&
+ T=$( cd target-3 && git rev-parse HEAD ) &&
+ S=$( cd src && git rev-parse HEAD ) &&
+ test "$T" = "$S"
+'
+
+test_expect_success 'clone to an existing non-empty directory' '
+ mkdir target-4 &&
+ >target-4/Fakefile &&
+ test_must_fail git clone src target-4
+'
+
+test_expect_success 'clone to an existing path' '
+ >target-5 &&
+ test_must_fail git clone src target-5
+'
+
+test_expect_success 'clone a void' '
+ mkdir src-0 &&
+ (
+ cd src-0 && git init
+ ) &&
+ git clone "file://$(pwd)/src-0" target-6 2>err-6 &&
+ ! grep "fatal:" err-6 &&
+ (
+ cd src-0 && test_commit A
+ ) &&
+ git clone "file://$(pwd)/src-0" target-7 2>err-7 &&
+ ! grep "fatal:" err-7 &&
+ # There is no reason to insist they are bit-for-bit
+ # identical, but this test should suffice for now.
+ test_cmp target-6/.git/config target-7/.git/config
+'
+
+test_expect_success 'clone respects global branch.autosetuprebase' '
+ (
+ test_config="$HOME/.gitconfig" &&
+ git config -f "$test_config" branch.autosetuprebase remote &&
+ rm -fr dst &&
+ git clone src dst &&
+ cd dst &&
+ actual="z$(git config branch.main.rebase)" &&
+ test ztrue = $actual
+ )
+'
+
+test_expect_success 'respect url-encoding of file://' '
+ git init x+y &&
+ git clone "file://$PWD/x+y" xy-url-1 &&
+ git clone "file://$PWD/x%2By" xy-url-2
+'
+
+test_expect_success 'do not query-string-decode + in URLs' '
+ rm -rf x+y &&
+ git init "x y" &&
+ test_must_fail git clone "file://$PWD/x+y" xy-no-plus
+'
+
+test_expect_success 'do not respect url-encoding of non-url path' '
+ git init x+y &&
+ test_must_fail git clone x%2By xy-regular &&
+ git clone x+y xy-regular
+'
+
+test_expect_success 'clone separate gitdir' '
+ rm -rf dst &&
+ git clone --separate-git-dir realgitdir src dst &&
+ test -d realgitdir/refs
+'
+
+test_expect_success 'clone separate gitdir: output' '
+ echo "gitdir: $(pwd)/realgitdir" >expected &&
+ test_cmp expected dst/.git
+'
+
+test_expect_success 'clone from .git file' '
+ git clone dst/.git dst2
+'
+
+test_expect_success 'fetch from .git gitfile' '
+ (
+ cd dst2 &&
+ git fetch ../dst/.git
+ )
+'
+
+test_expect_success 'fetch from gitfile parent' '
+ (
+ cd dst2 &&
+ git fetch ../dst
+ )
+'
+
+test_expect_success 'clone separate gitdir where target already exists' '
+ rm -rf dst &&
+ echo foo=bar >>realgitdir/config &&
+ test_must_fail git clone --separate-git-dir realgitdir src dst &&
+ grep foo=bar realgitdir/config
+'
+
+test_expect_success 'clone --reference from original' '
+ git clone --shared --bare src src-1 &&
+ git clone --bare src src-2 &&
+ git clone --reference=src-2 --bare src-1 target-8 &&
+ grep /src-2/ target-8/objects/info/alternates
+'
+
+test_expect_success 'clone with more than one --reference' '
+ git clone --bare src src-3 &&
+ git clone --bare src src-4 &&
+ git clone --reference=src-3 --reference=src-4 src target-9 &&
+ grep /src-3/ target-9/.git/objects/info/alternates &&
+ grep /src-4/ target-9/.git/objects/info/alternates
+'
+
+test_expect_success 'clone from original with relative alternate' '
+ mkdir nest &&
+ git clone --bare src nest/src-5 &&
+ echo ../../../src/.git/objects >nest/src-5/objects/info/alternates &&
+ git clone --bare nest/src-5 target-10 &&
+ grep /src/\\.git/objects target-10/objects/info/alternates
+'
+
+test_expect_success 'clone checking out a tag' '
+ git clone --branch=some-tag src dst.tag &&
+ GIT_DIR=src/.git git rev-parse some-tag >expected &&
+ GIT_DIR=dst.tag/.git git rev-parse HEAD >actual &&
+ test_cmp expected actual &&
+ GIT_DIR=dst.tag/.git git config remote.origin.fetch >fetch.actual &&
+ echo "+refs/heads/*:refs/remotes/origin/*" >fetch.expected &&
+ test_cmp fetch.expected fetch.actual
+'
+
+test_expect_success 'set up ssh wrapper' '
+ cp "$GIT_BUILD_DIR/t/helper/test-fake-ssh$X" \
+ "$TRASH_DIRECTORY/ssh$X" &&
+ GIT_SSH="$TRASH_DIRECTORY/ssh$X" &&
+ export GIT_SSH &&
+ export TRASH_DIRECTORY &&
+ >"$TRASH_DIRECTORY"/ssh-output
+'
+
+copy_ssh_wrapper_as () {
+ rm -f "${1%$X}$X" &&
+ cp "$TRASH_DIRECTORY/ssh$X" "${1%$X}$X" &&
+ test_when_finished "rm $(git rev-parse --sq-quote "${1%$X}$X")" &&
+ GIT_SSH="${1%$X}$X" &&
+ test_when_finished "GIT_SSH=\"\$TRASH_DIRECTORY/ssh\$X\""
+}
+
+expect_ssh () {
+ test_when_finished '
+ (cd "$TRASH_DIRECTORY" && rm -f ssh-expect && >ssh-output)
+ ' &&
+ {
+ case "$#" in
+ 1)
+ ;;
+ 2)
+ echo "ssh: $1 git-upload-pack '$2'"
+ ;;
+ 3)
+ echo "ssh: $1 $2 git-upload-pack '$3'"
+ ;;
+ *)
+ echo "ssh: $1 $2 git-upload-pack '$3' $4"
+ esac
+ } >"$TRASH_DIRECTORY/ssh-expect" &&
+ (cd "$TRASH_DIRECTORY" && test_cmp ssh-expect ssh-output)
+}
+
+test_expect_success 'clone myhost:src uses ssh' '
+ GIT_TEST_PROTOCOL_VERSION=0 git clone myhost:src ssh-clone &&
+ expect_ssh myhost src
+'
+
+test_expect_success !MINGW,!CYGWIN 'clone local path foo:bar' '
+ cp -R src "foo:bar" &&
+ git clone "foo:bar" foobar &&
+ expect_ssh none
+'
+
+test_expect_success 'bracketed hostnames are still ssh' '
+ GIT_TEST_PROTOCOL_VERSION=0 git clone "[myhost:123]:src" ssh-bracket-clone &&
+ expect_ssh "-p 123" myhost src
+'
+
+test_expect_success 'OpenSSH variant passes -4' '
+ GIT_TEST_PROTOCOL_VERSION=0 git clone -4 "[myhost:123]:src" ssh-ipv4-clone &&
+ expect_ssh "-4 -p 123" myhost src
+'
+
+test_expect_success 'variant can be overridden' '
+ copy_ssh_wrapper_as "$TRASH_DIRECTORY/putty" &&
+ git -c ssh.variant=putty clone -4 "[myhost:123]:src" ssh-putty-clone &&
+ expect_ssh "-4 -P 123" myhost src
+'
+
+test_expect_success 'variant=auto picks based on basename' '
+ copy_ssh_wrapper_as "$TRASH_DIRECTORY/plink" &&
+ git -c ssh.variant=auto clone -4 "[myhost:123]:src" ssh-auto-clone &&
+ expect_ssh "-4 -P 123" myhost src
+'
+
+test_expect_success 'simple does not support -4/-6' '
+ copy_ssh_wrapper_as "$TRASH_DIRECTORY/simple" &&
+ test_must_fail git clone -4 "myhost:src" ssh-4-clone-simple
+'
+
+test_expect_success 'simple does not support port' '
+ copy_ssh_wrapper_as "$TRASH_DIRECTORY/simple" &&
+ test_must_fail git clone "[myhost:123]:src" ssh-bracket-clone-simple
+'
+
+test_expect_success 'uplink is treated as simple' '
+ copy_ssh_wrapper_as "$TRASH_DIRECTORY/uplink" &&
+ test_must_fail git clone "[myhost:123]:src" ssh-bracket-clone-uplink &&
+ git clone "myhost:src" ssh-clone-uplink &&
+ expect_ssh myhost src
+'
+
+test_expect_success 'OpenSSH-like uplink is treated as ssh' '
+ write_script "$TRASH_DIRECTORY/uplink" <<-EOF &&
+ if test "\$1" = "-G"
+ then
+ exit 0
+ fi &&
+ exec "\$TRASH_DIRECTORY/ssh$X" "\$@"
+ EOF
+ test_when_finished "rm -f \"\$TRASH_DIRECTORY/uplink\"" &&
+ GIT_SSH="$TRASH_DIRECTORY/uplink" &&
+ test_when_finished "GIT_SSH=\"\$TRASH_DIRECTORY/ssh\$X\"" &&
+ GIT_TEST_PROTOCOL_VERSION=0 git clone "[myhost:123]:src" ssh-bracket-clone-sshlike-uplink &&
+ expect_ssh "-p 123" myhost src
+'
+
+test_expect_success 'plink is treated specially (as putty)' '
+ copy_ssh_wrapper_as "$TRASH_DIRECTORY/plink" &&
+ git clone "[myhost:123]:src" ssh-bracket-clone-plink-0 &&
+ expect_ssh "-P 123" myhost src
+'
+
+test_expect_success 'plink.exe is treated specially (as putty)' '
+ copy_ssh_wrapper_as "$TRASH_DIRECTORY/plink.exe" &&
+ git clone "[myhost:123]:src" ssh-bracket-clone-plink-1 &&
+ expect_ssh "-P 123" myhost src
+'
+
+test_expect_success 'tortoiseplink is like putty, with extra arguments' '
+ copy_ssh_wrapper_as "$TRASH_DIRECTORY/tortoiseplink" &&
+ git clone "[myhost:123]:src" ssh-bracket-clone-plink-2 &&
+ expect_ssh "-batch -P 123" myhost src
+'
+
+test_expect_success 'double quoted plink.exe in GIT_SSH_COMMAND' '
+ copy_ssh_wrapper_as "$TRASH_DIRECTORY/plink.exe" &&
+ GIT_SSH_COMMAND="\"$TRASH_DIRECTORY/plink.exe\" -v" \
+ git clone "[myhost:123]:src" ssh-bracket-clone-plink-3 &&
+ expect_ssh "-v -P 123" myhost src
+'
+
+test_expect_success 'single quoted plink.exe in GIT_SSH_COMMAND' '
+ copy_ssh_wrapper_as "$TRASH_DIRECTORY/plink.exe" &&
+ GIT_SSH_COMMAND="$SQ$TRASH_DIRECTORY/plink.exe$SQ -v" \
+ git clone "[myhost:123]:src" ssh-bracket-clone-plink-4 &&
+ expect_ssh "-v -P 123" myhost src
+'
+
+test_expect_success 'GIT_SSH_VARIANT overrides plink detection' '
+ copy_ssh_wrapper_as "$TRASH_DIRECTORY/plink" &&
+ GIT_TEST_PROTOCOL_VERSION=0 GIT_SSH_VARIANT=ssh \
+ git clone "[myhost:123]:src" ssh-bracket-clone-variant-1 &&
+ expect_ssh "-p 123" myhost src
+'
+
+test_expect_success 'ssh.variant overrides plink detection' '
+ copy_ssh_wrapper_as "$TRASH_DIRECTORY/plink" &&
+ GIT_TEST_PROTOCOL_VERSION=0 git -c ssh.variant=ssh \
+ clone "[myhost:123]:src" ssh-bracket-clone-variant-2 &&
+ expect_ssh "-p 123" myhost src
+'
+
+test_expect_success 'GIT_SSH_VARIANT overrides plink detection to plink' '
+ copy_ssh_wrapper_as "$TRASH_DIRECTORY/plink" &&
+ GIT_SSH_VARIANT=plink \
+ git clone "[myhost:123]:src" ssh-bracket-clone-variant-3 &&
+ expect_ssh "-P 123" myhost src
+'
+
+test_expect_success 'GIT_SSH_VARIANT overrides plink to tortoiseplink' '
+ copy_ssh_wrapper_as "$TRASH_DIRECTORY/plink" &&
+ GIT_SSH_VARIANT=tortoiseplink \
+ git clone "[myhost:123]:src" ssh-bracket-clone-variant-4 &&
+ expect_ssh "-batch -P 123" myhost src
+'
+
+test_expect_success 'clean failure on broken quoting' '
+ test_must_fail \
+ env GIT_SSH_COMMAND="${SQ}plink.exe -v" \
+ git clone "[myhost:123]:src" sq-failure
+'
+
+counter=0
+# $1 url
+# $2 none|host
+# $3 path
+test_clone_url () {
+ counter=$(($counter + 1))
+ test_might_fail env GIT_TEST_PROTOCOL_VERSION=0 git clone "$1" tmp$counter &&
+ shift &&
+ expect_ssh "$@"
+}
+
+test_expect_success !MINGW,!CYGWIN 'clone c:temp is ssl' '
+ test_clone_url c:temp c temp
+'
+
+test_expect_success MINGW 'clone c:temp is dos drive' '
+ test_clone_url c:temp none
+'
+
+#ip v4
+for repo in rep rep/home/project 123
+do
+ test_expect_success "clone host:$repo" '
+ test_clone_url host:$repo host $repo
+ '
+done
+
+#ipv6
+for repo in rep rep/home/project 123
+do
+ test_expect_success "clone [::1]:$repo" '
+ test_clone_url [::1]:$repo ::1 "$repo"
+ '
+done
+#home directory
+test_expect_success "clone host:/~repo" '
+ test_clone_url host:/~repo host "~repo"
+'
+
+test_expect_success "clone [::1]:/~repo" '
+ test_clone_url [::1]:/~repo ::1 "~repo"
+'
+
+# Corner cases
+for url in foo/bar:baz [foo]bar/baz:qux [foo/bar]:baz
+do
+ test_expect_success "clone $url is not ssh" '
+ test_clone_url $url none
+ '
+done
+
+#with ssh:// scheme
+#ignore trailing colon
+for tcol in "" :
+do
+ test_expect_success "clone ssh://host.xz$tcol/home/user/repo" '
+ test_clone_url "ssh://host.xz$tcol/home/user/repo" host.xz /home/user/repo
+ '
+ # from home directory
+ test_expect_success "clone ssh://host.xz$tcol/~repo" '
+ test_clone_url "ssh://host.xz$tcol/~repo" host.xz "~repo"
+'
+done
+
+# with port number
+test_expect_success 'clone ssh://host.xz:22/home/user/repo' '
+ test_clone_url "ssh://host.xz:22/home/user/repo" "-p 22 host.xz" "/home/user/repo"
+'
+
+# from home directory with port number
+test_expect_success 'clone ssh://host.xz:22/~repo' '
+ test_clone_url "ssh://host.xz:22/~repo" "-p 22 host.xz" "~repo"
+'
+
+#IPv6
+for tuah in ::1 [::1] [::1]: user@::1 user@[::1] user@[::1]: [user@::1] [user@::1]:
+do
+ ehost=$(echo $tuah | sed -e "s/1]:/1]/" | tr -d "[]")
+ test_expect_success "clone ssh://$tuah/home/user/repo" "
+ test_clone_url ssh://$tuah/home/user/repo $ehost /home/user/repo
+ "
+done
+
+#IPv6 from home directory
+for tuah in ::1 [::1] user@::1 user@[::1] [user@::1]
+do
+ euah=$(echo $tuah | tr -d "[]")
+ test_expect_success "clone ssh://$tuah/~repo" "
+ test_clone_url ssh://$tuah/~repo $euah '~repo'
+ "
+done
+
+#IPv6 with port number
+for tuah in [::1] user@[::1] [user@::1]
+do
+ euah=$(echo $tuah | tr -d "[]")
+ test_expect_success "clone ssh://$tuah:22/home/user/repo" "
+ test_clone_url ssh://$tuah:22/home/user/repo '-p 22' $euah /home/user/repo
+ "
+done
+
+#IPv6 from home directory with port number
+for tuah in [::1] user@[::1] [user@::1]
+do
+ euah=$(echo $tuah | tr -d "[]")
+ test_expect_success "clone ssh://$tuah:22/~repo" "
+ test_clone_url ssh://$tuah:22/~repo '-p 22' $euah '~repo'
+ "
+done
+
+test_expect_success 'clone from a repository with two identical branches' '
+
+ (
+ cd src &&
+ git checkout -b another main
+ ) &&
+ git clone src target-11 &&
+ test "z$( cd target-11 && git symbolic-ref HEAD )" = zrefs/heads/another
+
+'
+
+test_expect_success 'shallow clone locally' '
+ git clone --depth=1 --no-local src ssrrcc &&
+ git clone ssrrcc ddsstt &&
+ test_cmp ssrrcc/.git/shallow ddsstt/.git/shallow &&
+ ( cd ddsstt && git fsck )
+'
+
+test_expect_success 'GIT_TRACE_PACKFILE produces a usable pack' '
+ rm -rf dst.git &&
+ GIT_TRACE_PACKFILE=$PWD/tmp.pack git clone --no-local --bare src dst.git &&
+ git init --bare replay.git &&
+ git -C replay.git index-pack -v --stdin <tmp.pack
+'
+
+test_expect_success 'clone on case-insensitive fs' '
+ git init icasefs &&
+ (
+ cd icasefs &&
+ o=$(git hash-object -w --stdin </dev/null | hex2oct) &&
+ t=$(printf "100644 X\0${o}100644 x\0${o}" |
+ git hash-object -w -t tree --stdin) &&
+ c=$(git commit-tree -m bogus $t) &&
+ git update-ref refs/heads/bogus $c &&
+ git clone -b bogus . bogus 2>warning
+ )
+'
+
+test_expect_success CASE_INSENSITIVE_FS 'colliding file detection' '
+ grep X icasefs/warning &&
+ grep x icasefs/warning &&
+ test_grep "the following paths have collided" icasefs/warning
+'
+
+test_expect_success 'clone with GIT_DEFAULT_HASH' '
+ (
+ sane_unset GIT_DEFAULT_HASH &&
+ git init --object-format=sha1 test-sha1 &&
+ git init --object-format=sha256 test-sha256
+ ) &&
+ test_commit -C test-sha1 foo &&
+ test_commit -C test-sha256 foo &&
+ GIT_DEFAULT_HASH=sha1 git clone test-sha256 test-clone-sha256 &&
+ GIT_DEFAULT_HASH=sha256 git clone test-sha1 test-clone-sha1 &&
+ git -C test-clone-sha1 status &&
+ git -C test-clone-sha256 status
+'
+
+partial_clone_server () {
+ SERVER="$1" &&
+
+ rm -rf "$SERVER" client &&
+ test_create_repo "$SERVER" &&
+ test_commit -C "$SERVER" one &&
+ HASH1=$(git -C "$SERVER" hash-object one.t) &&
+ git -C "$SERVER" revert HEAD &&
+ test_commit -C "$SERVER" two &&
+ HASH2=$(git -C "$SERVER" hash-object two.t) &&
+ test_config -C "$SERVER" uploadpack.allowfilter 1 &&
+ test_config -C "$SERVER" uploadpack.allowanysha1inwant 1
+}
+
+partial_clone () {
+ SERVER="$1" &&
+ URL="$2" &&
+
+ partial_clone_server "${SERVER}" &&
+ git clone --filter=blob:limit=0 "$URL" client &&
+
+ git -C client fsck &&
+
+ # Ensure that unneeded blobs are not inadvertently fetched.
+ test_config -C client remote.origin.promisor "false" &&
+ git -C client config --unset remote.origin.partialclonefilter &&
+ test_must_fail git -C client cat-file -e "$HASH1" &&
+
+ # But this blob was fetched, because clone performs an initial checkout
+ git -C client cat-file -e "$HASH2"
+}
+
+test_expect_success 'partial clone' '
+ partial_clone server "file://$(pwd)/server"
+'
+
+test_expect_success 'partial clone with -o' '
+ partial_clone_server server &&
+ git clone -o blah --filter=blob:limit=0 "file://$(pwd)/server" client &&
+ test_cmp_config -C client "blob:limit=0" --get-all remote.blah.partialclonefilter
+'
+
+test_expect_success 'partial clone: warn if server does not support object filtering' '
+ rm -rf server client &&
+ test_create_repo server &&
+ test_commit -C server one &&
+
+ git clone --filter=blob:limit=0 "file://$(pwd)/server" client 2> err &&
+
+ test_grep "filtering not recognized by server" err
+'
+
+test_expect_success 'batch missing blob request during checkout' '
+ rm -rf server client &&
+
+ test_create_repo server &&
+ echo a >server/a &&
+ echo b >server/b &&
+ git -C server add a b &&
+
+ git -C server commit -m x &&
+ echo aa >server/a &&
+ echo bb >server/b &&
+ git -C server add a b &&
+ git -C server commit -m x &&
+
+ test_config -C server uploadpack.allowfilter 1 &&
+ test_config -C server uploadpack.allowanysha1inwant 1 &&
+
+ git clone --filter=blob:limit=0 "file://$(pwd)/server" client &&
+
+ # Ensure that there is only one negotiation by checking that there is
+ # only "done" line sent. ("done" marks the end of negotiation.)
+ GIT_TRACE_PACKET="$(pwd)/trace" \
+ GIT_TRACE2_EVENT="$(pwd)/trace2_event" \
+ git -C client -c trace2.eventNesting=5 checkout HEAD^ &&
+ grep \"key\":\"total_rounds\",\"value\":\"1\" trace2_event >trace_lines &&
+ test_line_count = 1 trace_lines &&
+ grep "fetch> done" trace >done_lines &&
+ test_line_count = 1 done_lines
+'
+
+test_expect_success 'batch missing blob request does not inadvertently try to fetch gitlinks' '
+ rm -rf server client &&
+
+ test_create_repo repo_for_submodule &&
+ test_commit -C repo_for_submodule x &&
+
+ test_create_repo server &&
+ echo a >server/a &&
+ echo b >server/b &&
+ git -C server add a b &&
+ git -C server commit -m x &&
+
+ echo aa >server/a &&
+ echo bb >server/b &&
+ # Also add a gitlink pointing to an arbitrary repository
+ test_config_global protocol.file.allow always &&
+ git -C server submodule add "$(pwd)/repo_for_submodule" c &&
+ git -C server add a b c &&
+ git -C server commit -m x &&
+
+ test_config -C server uploadpack.allowfilter 1 &&
+ test_config -C server uploadpack.allowanysha1inwant 1 &&
+
+ # Make sure that it succeeds
+ git clone --filter=blob:limit=0 "file://$(pwd)/server" client
+'
+
+. "$TEST_DIRECTORY"/lib-httpd.sh
+start_httpd
+
+test_expect_success 'partial clone using HTTP' '
+ partial_clone "$HTTPD_DOCUMENT_ROOT_PATH/server" "$HTTPD_URL/smart/server"
+'
+
+test_expect_success 'reject cloning shallow repository using HTTP' '
+ test_when_finished "rm -rf repo" &&
+ git clone --bare --no-local --depth=1 src "$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
+ test_must_fail git -c protocol.version=2 clone --reject-shallow $HTTPD_URL/smart/repo.git repo 2>err &&
+ test_grep -e "source repository is shallow, reject to clone." err &&
+
+ git clone --no-reject-shallow $HTTPD_URL/smart/repo.git repo
+'
+
+test_expect_success 'auto-discover bundle URI from HTTP clone' '
+ test_when_finished rm -rf trace.txt repo2 "$HTTPD_DOCUMENT_ROOT_PATH/repo2.git" &&
+ git -C src bundle create "$HTTPD_DOCUMENT_ROOT_PATH/everything.bundle" --all &&
+ git clone --bare --no-local src "$HTTPD_DOCUMENT_ROOT_PATH/repo2.git" &&
+
+ git -C "$HTTPD_DOCUMENT_ROOT_PATH/repo2.git" config \
+ uploadpack.advertiseBundleURIs true &&
+ git -C "$HTTPD_DOCUMENT_ROOT_PATH/repo2.git" config \
+ bundle.version 1 &&
+ git -C "$HTTPD_DOCUMENT_ROOT_PATH/repo2.git" config \
+ bundle.mode all &&
+ git -C "$HTTPD_DOCUMENT_ROOT_PATH/repo2.git" config \
+ bundle.everything.uri "$HTTPD_URL/everything.bundle" &&
+
+ GIT_TRACE2_EVENT="$(pwd)/trace.txt" \
+ git -c protocol.version=2 \
+ -c transfer.bundleURI=true clone \
+ $HTTPD_URL/smart/repo2.git repo2 &&
+ cat >pattern <<-EOF &&
+ "event":"child_start".*"argv":\["git-remote-https","$HTTPD_URL/everything.bundle"\]
+ EOF
+ grep -f pattern trace.txt
+'
+
+test_expect_success 'auto-discover multiple bundles from HTTP clone' '
+ test_when_finished rm -rf trace.txt repo3 "$HTTPD_DOCUMENT_ROOT_PATH/repo3.git" &&
+
+ test_commit -C src new &&
+ git -C src bundle create "$HTTPD_DOCUMENT_ROOT_PATH/new.bundle" HEAD~1..HEAD &&
+ git clone --bare --no-local src "$HTTPD_DOCUMENT_ROOT_PATH/repo3.git" &&
+
+ git -C "$HTTPD_DOCUMENT_ROOT_PATH/repo3.git" config \
+ uploadpack.advertiseBundleURIs true &&
+ git -C "$HTTPD_DOCUMENT_ROOT_PATH/repo3.git" config \
+ bundle.version 1 &&
+ git -C "$HTTPD_DOCUMENT_ROOT_PATH/repo3.git" config \
+ bundle.mode all &&
+
+ git -C "$HTTPD_DOCUMENT_ROOT_PATH/repo3.git" config \
+ bundle.everything.uri "$HTTPD_URL/everything.bundle" &&
+ git -C "$HTTPD_DOCUMENT_ROOT_PATH/repo3.git" config \
+ bundle.new.uri "$HTTPD_URL/new.bundle" &&
+
+ GIT_TRACE2_EVENT="$(pwd)/trace.txt" \
+ git -c protocol.version=2 \
+ -c transfer.bundleURI=true clone \
+ $HTTPD_URL/smart/repo3.git repo3 &&
+
+ # We should fetch _both_ bundles
+ cat >pattern <<-EOF &&
+ "event":"child_start".*"argv":\["git-remote-https","$HTTPD_URL/everything.bundle"\]
+ EOF
+ grep -f pattern trace.txt &&
+ cat >pattern <<-EOF &&
+ "event":"child_start".*"argv":\["git-remote-https","$HTTPD_URL/new.bundle"\]
+ EOF
+ grep -f pattern trace.txt
+'
+
+test_expect_success 'auto-discover multiple bundles from HTTP clone: creationToken heuristic' '
+ test_when_finished rm -rf "$HTTPD_DOCUMENT_ROOT_PATH/repo4.git" &&
+ test_when_finished rm -rf clone-heuristic trace*.txt &&
+
+ test_commit -C src newest &&
+ git -C src bundle create "$HTTPD_DOCUMENT_ROOT_PATH/newest.bundle" HEAD~1..HEAD &&
+ git clone --bare --no-local src "$HTTPD_DOCUMENT_ROOT_PATH/repo4.git" &&
+
+ cat >>"$HTTPD_DOCUMENT_ROOT_PATH/repo4.git/config" <<-EOF &&
+ [uploadPack]
+ advertiseBundleURIs = true
+
+ [bundle]
+ version = 1
+ mode = all
+ heuristic = creationToken
+
+ [bundle "everything"]
+ uri = $HTTPD_URL/everything.bundle
+ creationtoken = 1
+
+ [bundle "new"]
+ uri = $HTTPD_URL/new.bundle
+ creationtoken = 2
+
+ [bundle "newest"]
+ uri = $HTTPD_URL/newest.bundle
+ creationtoken = 3
+ EOF
+
+ GIT_TRACE2_EVENT="$(pwd)/trace-clone.txt" \
+ git -c protocol.version=2 \
+ -c transfer.bundleURI=true clone \
+ "$HTTPD_URL/smart/repo4.git" clone-heuristic &&
+
+ cat >expect <<-EOF &&
+ $HTTPD_URL/newest.bundle
+ $HTTPD_URL/new.bundle
+ $HTTPD_URL/everything.bundle
+ EOF
+
+ # We should fetch all bundles in the expected order.
+ test_remote_https_urls <trace-clone.txt >actual &&
+ test_cmp expect actual
+'
+
+# DO NOT add non-httpd-specific tests here, because the last part of this
+# test script is only executed when httpd is available and enabled.
+
+test_done
diff --git a/t/t5602-clone-remote-exec.sh b/t/t5602-clone-remote-exec.sh
new file mode 100755
index 0000000..56329aa
--- /dev/null
+++ b/t/t5602-clone-remote-exec.sh
@@ -0,0 +1,28 @@
+#!/bin/sh
+
+test_description=clone
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ echo "#!/bin/sh" > not_ssh &&
+ echo "echo \"\$*\" > not_ssh_output" >> not_ssh &&
+ echo "exit 1" >> not_ssh &&
+ chmod +x not_ssh
+'
+
+test_expect_success 'clone calls git upload-pack unqualified with no -u option' '
+ test_must_fail env GIT_SSH=./not_ssh git clone localhost:/path/to/repo junk &&
+ echo "localhost git-upload-pack '\''/path/to/repo'\''" >expected &&
+ test_cmp expected not_ssh_output
+'
+
+test_expect_success 'clone calls specified git upload-pack with -u option' '
+ test_must_fail env GIT_SSH=./not_ssh \
+ git clone -u ./something/bin/git-upload-pack localhost:/path/to/repo junk &&
+ echo "localhost ./something/bin/git-upload-pack '\''/path/to/repo'\''" >expected &&
+ test_cmp expected not_ssh_output
+'
+
+test_done
diff --git a/t/t5603-clone-dirname.sh b/t/t5603-clone-dirname.sh
new file mode 100755
index 0000000..8ca1f09
--- /dev/null
+++ b/t/t5603-clone-dirname.sh
@@ -0,0 +1,110 @@
+#!/bin/sh
+
+test_description='check output directory names used by git-clone'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# we use a fake ssh wrapper that ignores the arguments
+# entirely; we really only care that we get _some_ repo,
+# as the real test is what clone does on the local side
+test_expect_success 'setup ssh wrapper' '
+ write_script "$TRASH_DIRECTORY/ssh-wrapper" <<-\EOF &&
+ git upload-pack "$TRASH_DIRECTORY"
+ EOF
+ GIT_SSH="$TRASH_DIRECTORY/ssh-wrapper" &&
+ GIT_SSH_VARIANT=ssh &&
+ export GIT_SSH &&
+ export GIT_SSH_VARIANT &&
+ export TRASH_DIRECTORY
+'
+
+# make sure that cloning $1 results in local directory $2
+test_clone_dir () {
+ url=$1; shift
+ dir=$1; shift
+ expect=success
+ bare=non-bare
+ clone_opts=
+ for i in "$@"
+ do
+ case "$i" in
+ fail)
+ expect=failure
+ ;;
+ bare)
+ bare=bare
+ clone_opts=--bare
+ ;;
+ esac
+ done
+ test_expect_$expect "clone of $url goes to $dir ($bare)" "
+ rm -rf $dir &&
+ git clone $clone_opts $url &&
+ test_path_is_dir $dir
+ "
+}
+
+# basic syntax with bare and non-bare variants
+test_clone_dir host:foo foo
+test_clone_dir host:foo foo.git bare
+test_clone_dir host:foo.git foo
+test_clone_dir host:foo.git foo.git bare
+test_clone_dir host:foo/.git foo
+test_clone_dir host:foo/.git foo.git bare
+
+# similar, but using ssh URL rather than host:path syntax
+test_clone_dir ssh://host/foo foo
+test_clone_dir ssh://host/foo foo.git bare
+test_clone_dir ssh://host/foo.git foo
+test_clone_dir ssh://host/foo.git foo.git bare
+test_clone_dir ssh://host/foo/.git foo
+test_clone_dir ssh://host/foo/.git foo.git bare
+
+# we should remove trailing slashes and .git suffixes
+test_clone_dir ssh://host/foo/ foo
+test_clone_dir ssh://host/foo/// foo
+test_clone_dir ssh://host/foo/.git/ foo
+test_clone_dir ssh://host/foo.git/ foo
+test_clone_dir ssh://host/foo.git/// foo
+test_clone_dir ssh://host/foo///.git/ foo
+test_clone_dir ssh://host/foo/.git/// foo
+
+test_clone_dir host:foo/ foo
+test_clone_dir host:foo/// foo
+test_clone_dir host:foo.git/ foo
+test_clone_dir host:foo/.git/ foo
+test_clone_dir host:foo.git/// foo
+test_clone_dir host:foo///.git/ foo
+test_clone_dir host:foo/.git/// foo
+
+# omitting the path should default to the hostname
+test_clone_dir ssh://host/ host
+test_clone_dir ssh://host:1234/ host
+test_clone_dir ssh://user@host/ host
+test_clone_dir host:/ host
+
+# auth materials should be redacted
+test_clone_dir ssh://user:password@host/ host
+test_clone_dir ssh://user:password@host:1234/ host
+test_clone_dir ssh://user:passw@rd@host:1234/ host
+test_clone_dir user@host:/ host
+test_clone_dir user:password@host:/ host
+test_clone_dir user:passw@rd@host:/ host
+
+# auth-like material should not be dropped
+test_clone_dir ssh://host/foo@bar foo@bar
+test_clone_dir ssh://host/foo@bar.git foo@bar
+test_clone_dir ssh://user:password@host/foo@bar foo@bar
+test_clone_dir ssh://user:passw@rd@host/foo@bar.git foo@bar
+
+test_clone_dir host:/foo@bar foo@bar
+test_clone_dir host:/foo@bar.git foo@bar
+test_clone_dir user:password@host:/foo@bar foo@bar
+test_clone_dir user:passw@rd@host:/foo@bar.git foo@bar
+
+# trailing port-like numbers should not be stripped for paths
+test_clone_dir ssh://user:password@host/test:1234 1234
+test_clone_dir ssh://user:password@host/test:1234.git 1234
+
+test_done
diff --git a/t/t5604-clone-reference.sh b/t/t5604-clone-reference.sh
new file mode 100755
index 0000000..9b32db8
--- /dev/null
+++ b/t/t5604-clone-reference.sh
@@ -0,0 +1,364 @@
+#!/bin/sh
+#
+# Copyright (C) 2006 Martin Waitz <tali@admingilde.org>
+#
+
+test_description='test clone --reference'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+base_dir=$(pwd)
+
+U=$base_dir/UPLOAD_LOG
+
+# create a commit in repo $1 with name $2
+commit_in () {
+ (
+ cd "$1" &&
+ echo "$2" >"$2" &&
+ git add "$2" &&
+ git commit -m "$2"
+ )
+}
+
+# check that there are $2 loose objects in repo $1
+test_objcount () {
+ echo "$2" >expect &&
+ git -C "$1" count-objects >actual.raw &&
+ cut -d' ' -f1 <actual.raw >actual &&
+ test_cmp expect actual
+}
+
+test_expect_success 'preparing first repository' '
+ test_create_repo A &&
+ commit_in A file1
+'
+
+test_expect_success 'preparing second repository' '
+ git clone A B &&
+ commit_in B file2 &&
+ git -C B repack -ad &&
+ git -C B prune
+'
+
+test_expect_success 'cloning with reference (-l -s)' '
+ git clone -l -s --reference B A C
+'
+
+test_expect_success 'existence of info/alternates' '
+ test_line_count = 2 C/.git/objects/info/alternates
+'
+
+test_expect_success 'pulling from reference' '
+ git -C C pull ../B main
+'
+
+test_expect_success 'that reference gets used' '
+ test_objcount C 0
+'
+
+test_expect_success 'cloning with reference (no -l -s)' '
+ GIT_TRACE_PACKET=$U.D git clone --reference B "file://$(pwd)/A" D
+'
+
+test_expect_success 'fetched no objects' '
+ test -s "$U.D" &&
+ ! grep " want" "$U.D"
+'
+
+test_expect_success 'existence of info/alternates' '
+ test_line_count = 1 D/.git/objects/info/alternates
+'
+
+test_expect_success 'pulling from reference' '
+ git -C D pull ../B main
+'
+
+test_expect_success 'that reference gets used' '
+ test_objcount D 0
+'
+
+test_expect_success 'updating origin' '
+ commit_in A file3 &&
+ git -C A repack -ad &&
+ git -C A prune
+'
+
+test_expect_success 'pulling changes from origin' '
+ git -C C pull --no-rebase origin
+'
+
+# the 2 local objects are commit and tree from the merge
+test_expect_success 'that alternate to origin gets used' '
+ test_objcount C 2
+'
+
+test_expect_success 'pulling changes from origin' '
+ git -C D pull --no-rebase origin
+'
+
+# the 5 local objects are expected; file3 blob, commit in A to add it
+# and its tree, and 2 are our tree and the merge commit.
+test_expect_success 'check objects expected to exist locally' '
+ test_objcount D 5
+'
+
+test_expect_success 'preparing alternate repository #1' '
+ test_create_repo F &&
+ commit_in F file1
+'
+
+test_expect_success 'cloning alternate repo #2 and adding changes to repo #1' '
+ git clone F G &&
+ commit_in F file2
+'
+
+test_expect_success 'cloning alternate repo #1, using #2 as reference' '
+ git clone --reference G F H
+'
+
+test_expect_success 'cloning with reference being subset of source (-l -s)' '
+ git clone -l -s --reference A B E
+'
+
+test_expect_success 'cloning with multiple references drops duplicates' '
+ git clone -s --reference B --reference A --reference B A dups &&
+ test_line_count = 2 dups/.git/objects/info/alternates
+'
+
+test_expect_success 'clone with reference from a tagged repository' '
+ (
+ cd A && git tag -a -m tagged HEAD
+ ) &&
+ git clone --reference=A A I
+'
+
+test_expect_success 'prepare branched repository' '
+ git clone A J &&
+ (
+ cd J &&
+ git checkout -b other main^ &&
+ echo other >otherfile &&
+ git add otherfile &&
+ git commit -m other &&
+ git checkout main
+ )
+'
+
+test_expect_success 'fetch with incomplete alternates' '
+ git init K &&
+ echo "$base_dir/A/.git/objects" >K/.git/objects/info/alternates &&
+ (
+ cd K &&
+ git remote add J "file://$base_dir/J" &&
+ GIT_TRACE_PACKET=$U.K git fetch J
+ ) &&
+ main_object=$(cd A && git for-each-ref --format="%(objectname)" refs/heads/main) &&
+ test -s "$U.K" &&
+ ! grep " want $main_object" "$U.K" &&
+ tag_object=$(cd A && git for-each-ref --format="%(objectname)" refs/tags/HEAD) &&
+ ! grep " want $tag_object" "$U.K"
+'
+
+test_expect_success 'clone using repo with gitfile as a reference' '
+ git clone --separate-git-dir=L A M &&
+ git clone --reference=M A N &&
+ echo "$base_dir/L/objects" >expected &&
+ test_cmp expected "$base_dir/N/.git/objects/info/alternates"
+'
+
+test_expect_success 'clone using repo pointed at by gitfile as reference' '
+ git clone --reference=M/.git A O &&
+ echo "$base_dir/L/objects" >expected &&
+ test_cmp expected "$base_dir/O/.git/objects/info/alternates"
+'
+
+test_expect_success 'clone and dissociate from reference' '
+ git init P &&
+ (
+ cd P && test_commit one
+ ) &&
+ git clone P Q &&
+ (
+ cd Q && test_commit two
+ ) &&
+ git clone --no-local --reference=P Q R &&
+ git clone --no-local --reference=P --dissociate Q S &&
+ # removing the reference P would corrupt R but not S
+ rm -fr P &&
+ test_must_fail git -C R fsck &&
+ git -C S fsck
+'
+test_expect_success 'clone, dissociate from partial reference and repack' '
+ rm -fr P Q R &&
+ git init P &&
+ (
+ cd P &&
+ test_commit one &&
+ git repack &&
+ test_commit two &&
+ git repack
+ ) &&
+ git clone --bare P Q &&
+ (
+ cd P &&
+ git checkout -b second &&
+ test_commit three &&
+ git repack
+ ) &&
+ git clone --bare --dissociate --reference=P Q R &&
+ ls R/objects/pack/*.pack >packs.txt &&
+ test_line_count = 1 packs.txt
+'
+
+test_expect_success 'clone, dissociate from alternates' '
+ rm -fr A B C &&
+ test_create_repo A &&
+ commit_in A file1 &&
+ git clone --reference=A A B &&
+ test_line_count = 1 B/.git/objects/info/alternates &&
+ git clone --local --dissociate B C &&
+ ! test -f C/.git/objects/info/alternates &&
+ ( cd C && git fsck )
+'
+
+test_expect_success 'setup repo with garbage in objects/*' '
+ git init S &&
+ (
+ cd S &&
+ test_commit A &&
+
+ cd .git/objects &&
+ >.some-hidden-file &&
+ >some-file &&
+ mkdir .some-hidden-dir &&
+ >.some-hidden-dir/some-file &&
+ >.some-hidden-dir/.some-dot-file &&
+ mkdir some-dir &&
+ >some-dir/some-file &&
+ >some-dir/.some-dot-file
+ )
+'
+
+test_expect_success 'clone a repo with garbage in objects/*' '
+ for option in --local --no-hardlinks --shared --dissociate
+ do
+ git clone $option S S$option || return 1 &&
+ git -C S$option fsck || return 1
+ done &&
+ find S-* -name "*some*" | sort >actual &&
+ cat >expected <<-EOF &&
+ S--dissociate/.git/objects/.some-hidden-dir
+ S--dissociate/.git/objects/.some-hidden-dir/.some-dot-file
+ S--dissociate/.git/objects/.some-hidden-dir/some-file
+ S--dissociate/.git/objects/.some-hidden-file
+ S--dissociate/.git/objects/some-dir
+ S--dissociate/.git/objects/some-dir/.some-dot-file
+ S--dissociate/.git/objects/some-dir/some-file
+ S--dissociate/.git/objects/some-file
+ S--local/.git/objects/.some-hidden-dir
+ S--local/.git/objects/.some-hidden-dir/.some-dot-file
+ S--local/.git/objects/.some-hidden-dir/some-file
+ S--local/.git/objects/.some-hidden-file
+ S--local/.git/objects/some-dir
+ S--local/.git/objects/some-dir/.some-dot-file
+ S--local/.git/objects/some-dir/some-file
+ S--local/.git/objects/some-file
+ S--no-hardlinks/.git/objects/.some-hidden-dir
+ S--no-hardlinks/.git/objects/.some-hidden-dir/.some-dot-file
+ S--no-hardlinks/.git/objects/.some-hidden-dir/some-file
+ S--no-hardlinks/.git/objects/.some-hidden-file
+ S--no-hardlinks/.git/objects/some-dir
+ S--no-hardlinks/.git/objects/some-dir/.some-dot-file
+ S--no-hardlinks/.git/objects/some-dir/some-file
+ S--no-hardlinks/.git/objects/some-file
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success SYMLINKS 'setup repo with manually symlinked or unknown files at objects/' '
+ git init T &&
+ (
+ cd T &&
+ git config gc.auto 0 &&
+ test_commit A &&
+ git gc &&
+ test_commit B &&
+
+ cd .git/objects &&
+ mv pack packs &&
+ ln -s packs pack &&
+ find ?? -type d >loose-dirs &&
+ last_loose=$(tail -n 1 loose-dirs) &&
+ mv $last_loose a-loose-dir &&
+ ln -s a-loose-dir $last_loose &&
+ first_loose=$(head -n 1 loose-dirs) &&
+ rm -f loose-dirs &&
+
+ cd $first_loose &&
+ obj=$(ls *) &&
+ mv $obj ../an-object &&
+ ln -s ../an-object $obj &&
+
+ cd ../ &&
+ echo unknown_content >unknown_file
+ ) &&
+ git -C T fsck &&
+ git -C T rev-list --all --objects >T.objects
+'
+
+
+test_expect_success SYMLINKS 'clone repo with symlinked or unknown files at objects/' '
+ # None of these options work when cloning locally, since T has
+ # symlinks in its `$GIT_DIR/objects` directory
+ for option in --local --no-hardlinks --dissociate
+ do
+ test_must_fail git clone $option T T$option 2>err || return 1 &&
+ test_grep "symlink.*exists" err || return 1
+ done &&
+
+ # But `--shared` clones should still work, even when specifying
+ # a local path *and* that repository has symlinks present in its
+ # `$GIT_DIR/objects` directory.
+ git clone --shared T T--shared &&
+ git -C T--shared fsck &&
+ git -C T--shared rev-list --all --objects >T--shared.objects &&
+ test_cmp T.objects T--shared.objects &&
+ (
+ cd T--shared/.git/objects &&
+ find . -type f | sort >../../../T--shared.objects-files.raw &&
+ find . -type l | sort >../../../T--shared.objects-symlinks.raw
+ ) &&
+
+ for raw in $(ls T*.raw)
+ do
+ sed -e "s!/../!/Y/!; s![0-9a-f]\{38,\}!Z!" -e "/commit-graph/d" \
+ -e "/multi-pack-index/d" -e "/rev/d" <$raw >$raw.de-sha-1 &&
+ sort $raw.de-sha-1 >$raw.de-sha || return 1
+ done &&
+
+ echo ./info/alternates >expected-files &&
+ test_cmp expected-files T--shared.objects-files.raw &&
+ test_must_be_empty T--shared.objects-symlinks.raw
+'
+
+test_expect_success SYMLINKS 'clone repo with symlinked objects directory' '
+ test_when_finished "rm -fr sensitive malicious" &&
+
+ mkdir -p sensitive &&
+ echo "secret" >sensitive/file &&
+
+ git init malicious &&
+ rm -fr malicious/.git/objects &&
+ ln -s "$(pwd)/sensitive" ./malicious/.git/objects &&
+
+ test_must_fail git clone --local malicious clone 2>err &&
+
+ test_path_is_missing clone &&
+ grep "is a symlink, refusing to clone with --local" err
+'
+
+test_done
diff --git a/t/t5605-clone-local.sh b/t/t5605-clone-local.sh
new file mode 100755
index 0000000..946c575
--- /dev/null
+++ b/t/t5605-clone-local.sh
@@ -0,0 +1,169 @@
+#!/bin/sh
+
+test_description='test local clone'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+repo_is_hardlinked() {
+ find "$1/objects" -type f -links 1 >output &&
+ test_line_count = 0 output
+}
+
+test_expect_success 'preparing origin repository' '
+ : >file && git add . && git commit -m1 &&
+ git clone --bare . a.git &&
+ git clone --bare . x &&
+ echo true >expect &&
+ git -C a.git config --bool core.bare >actual &&
+ test_cmp expect actual &&
+ echo true >expect &&
+ git -C x config --bool core.bare >actual &&
+ test_cmp expect actual &&
+ git bundle create b1.bundle --all &&
+ git bundle create b2.bundle main &&
+ mkdir dir &&
+ cp b1.bundle dir/b3 &&
+ cp b1.bundle b4 &&
+ git branch not-main main &&
+ git bundle create b5.bundle not-main
+'
+
+test_expect_success 'local clone without .git suffix' '
+ git clone -l -s a b &&
+ (cd b &&
+ echo false >expect &&
+ git config --bool core.bare >actual &&
+ test_cmp expect actual &&
+ git fetch)
+'
+
+test_expect_success 'local clone with .git suffix' '
+ git clone -l -s a.git c &&
+ (cd c && git fetch)
+'
+
+test_expect_success 'local clone from x' '
+ git clone -l -s x y &&
+ (cd y && git fetch)
+'
+
+test_expect_success 'local clone from x.git that does not exist' '
+ test_must_fail git clone -l -s x.git z
+'
+
+test_expect_success 'With -no-hardlinks, local will make a copy' '
+ git clone --bare --no-hardlinks x w &&
+ ! repo_is_hardlinked w
+'
+
+test_expect_success 'Even without -l, local will make a hardlink' '
+ rm -fr w &&
+ git clone -l --bare x w &&
+ repo_is_hardlinked w
+'
+
+test_expect_success 'local clone of repo with nonexistent ref in HEAD' '
+ echo "ref: refs/heads/nonexistent" > a.git/HEAD &&
+ git clone a d &&
+ (cd d &&
+ git fetch &&
+ test_ref_missing refs/remotes/origin/HEAD)
+'
+
+test_expect_success 'bundle clone without .bundle suffix' '
+ git clone dir/b3 &&
+ (cd b3 && git fetch)
+'
+
+test_expect_success 'bundle clone with .bundle suffix' '
+ git clone b1.bundle &&
+ (cd b1 && git fetch)
+'
+
+test_expect_success 'bundle clone from b4' '
+ git clone b4 bdl &&
+ (cd bdl && git fetch)
+'
+
+test_expect_success 'bundle clone from b4.bundle that does not exist' '
+ test_must_fail git clone b4.bundle bb
+'
+
+test_expect_success 'bundle clone with nonexistent HEAD (match default)' '
+ git clone b2.bundle b2 &&
+ (cd b2 &&
+ git fetch &&
+ git rev-parse --verify refs/heads/main)
+'
+
+test_expect_success 'bundle clone with nonexistent HEAD (no match default)' '
+ git clone b5.bundle b5 &&
+ (cd b5 &&
+ git fetch &&
+ test_must_fail git rev-parse --verify refs/heads/main &&
+ test_must_fail git rev-parse --verify refs/heads/not-main)
+'
+
+test_expect_success 'clone empty repository' '
+ mkdir empty &&
+ (cd empty &&
+ git init &&
+ git config receive.denyCurrentBranch warn) &&
+ git clone empty empty-clone &&
+ test_tick &&
+ (cd empty-clone &&
+ echo "content" >> foo &&
+ git add foo &&
+ git commit -m "Initial commit" &&
+ git push origin main &&
+ expected=$(git rev-parse main) &&
+ actual=$(git --git-dir=../empty/.git rev-parse main) &&
+ test $actual = $expected)
+'
+
+test_expect_success 'clone empty repository, and then push should not segfault.' '
+ rm -fr empty/ empty-clone/ &&
+ mkdir empty &&
+ (cd empty && git init) &&
+ git clone empty empty-clone &&
+ (cd empty-clone &&
+ test_must_fail git push)
+'
+
+test_expect_success 'cloning non-existent directory fails' '
+ rm -rf does-not-exist &&
+ test_must_fail git clone does-not-exist
+'
+
+test_expect_success 'cloning non-git directory fails' '
+ rm -rf not-a-git-repo not-a-git-repo-clone &&
+ mkdir not-a-git-repo &&
+ test_must_fail git clone not-a-git-repo not-a-git-repo-clone
+'
+
+test_expect_success 'cloning file:// does not hardlink' '
+ git clone --bare file://"$(pwd)"/a non-local &&
+ ! repo_is_hardlinked non-local
+'
+
+test_expect_success 'cloning a local path with --no-local does not hardlink' '
+ git clone --bare --no-local a force-nonlocal &&
+ ! repo_is_hardlinked force-nonlocal
+'
+
+test_expect_success 'cloning locally respects "-u" for fetching refs' '
+ test_must_fail git clone --bare -u false a should_not_work.git
+'
+
+test_expect_success 'local clone from repo with corrupt refs fails gracefully' '
+ git init corrupt &&
+ test_commit -C corrupt one &&
+ echo a >corrupt/.git/refs/heads/topic &&
+
+ test_must_fail git clone corrupt working 2>err &&
+ grep "has a null OID" err
+'
+
+test_done
diff --git a/t/t5606-clone-options.sh b/t/t5606-clone-options.sh
new file mode 100755
index 0000000..fc4bbd9
--- /dev/null
+++ b/t/t5606-clone-options.sh
@@ -0,0 +1,201 @@
+#!/bin/sh
+
+test_description='basic clone options'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+
+ mkdir parent &&
+ (cd parent && git init &&
+ echo one >file && git add file &&
+ git commit -m one) &&
+ git clone --depth=1 --no-local parent shallow-repo
+
+'
+
+test_expect_success 'submodule.stickyRecursiveClone flag manipulates submodule.recurse value' '
+
+ test_config_global submodule.stickyRecursiveClone true &&
+ git clone --recurse-submodules parent clone_recurse_true &&
+ test_cmp_config -C clone_recurse_true true submodule.recurse &&
+
+ test_config_global submodule.stickyRecursiveClone false &&
+ git clone --recurse-submodules parent clone_recurse_false &&
+ test_expect_code 1 git -C clone_recurse_false config --get submodule.recurse
+
+'
+
+test_expect_success 'clone -o' '
+
+ git clone -o foo parent clone-o &&
+ git -C clone-o rev-parse --verify refs/remotes/foo/main
+
+'
+
+test_expect_success 'rejects invalid -o/--origin' '
+
+ test_must_fail git clone -o "bad...name" parent clone-bad-name 2>err &&
+ test_grep "'\''bad...name'\'' is not a valid remote name" err
+
+'
+
+test_expect_success 'clone --bare -o' '
+
+ git clone -o foo --bare parent clone-bare-o &&
+ (cd parent && pwd) >expect &&
+ git -C clone-bare-o config remote.foo.url >actual &&
+ test_cmp expect actual
+
+'
+
+test_expect_success 'disallows --bare with --separate-git-dir' '
+
+ test_must_fail git clone --bare --separate-git-dir dot-git-destiation parent clone-bare-sgd 2>err &&
+ test_debug "cat err" &&
+ test_grep -e "options .--bare. and .--separate-git-dir. cannot be used together" err
+
+'
+
+test_expect_success 'disallows --bundle-uri with shallow options' '
+ for option in --depth=1 --shallow-since=01-01-2000 --shallow-exclude=HEAD
+ do
+ test_must_fail git clone --bundle-uri=bundle $option from to 2>err &&
+ grep "bundle-uri is incompatible" err || return 1
+ done
+'
+
+test_expect_success 'reject cloning shallow repository' '
+ test_when_finished "rm -rf repo" &&
+ test_must_fail git clone --reject-shallow shallow-repo out 2>err &&
+ test_grep -e "source repository is shallow, reject to clone." err &&
+
+ git clone --no-reject-shallow shallow-repo repo
+'
+
+test_expect_success 'reject cloning non-local shallow repository' '
+ test_when_finished "rm -rf repo" &&
+ test_must_fail git clone --reject-shallow --no-local shallow-repo out 2>err &&
+ test_grep -e "source repository is shallow, reject to clone." err &&
+
+ git clone --no-reject-shallow --no-local shallow-repo repo
+'
+
+test_expect_success 'succeed cloning normal repository' '
+ test_when_finished "rm -rf chilad1 child2 child3 child4 " &&
+ git clone --reject-shallow parent child1 &&
+ git clone --reject-shallow --no-local parent child2 &&
+ git clone --no-reject-shallow parent child3 &&
+ git clone --no-reject-shallow --no-local parent child4
+'
+
+test_expect_success 'uses "origin" for default remote name' '
+
+ git clone parent clone-default-origin &&
+ git -C clone-default-origin rev-parse --verify refs/remotes/origin/main
+
+'
+
+test_expect_success 'prefers --template config over normal config' '
+
+ template="$TRASH_DIRECTORY/template-with-config" &&
+ mkdir "$template" &&
+ git config --file "$template/config" foo.bar from_template &&
+ test_config_global foo.bar from_global &&
+ git clone "--template=$template" parent clone-template-config &&
+ test "$(git -C clone-template-config config --local foo.bar)" = "from_template"
+
+'
+
+test_expect_success 'prefers -c config over --template config' '
+
+ template="$TRASH_DIRECTORY/template-with-ignored-config" &&
+ mkdir "$template" &&
+ git config --file "$template/config" foo.bar from_template &&
+ git clone "--template=$template" -c foo.bar=inline parent clone-template-inline-config &&
+ test "$(git -C clone-template-inline-config config --local foo.bar)" = "inline"
+
+'
+
+test_expect_failure 'prefers --template config even for core.bare' '
+
+ template="$TRASH_DIRECTORY/template-with-bare-config" &&
+ mkdir "$template" &&
+ git config --file "$template/config" core.bare true &&
+ git clone "--template=$template" parent clone-bare-config &&
+ test "$(git -C clone-bare-config config --local core.bare)" = "true" &&
+ test_path_is_file clone-bare-config/HEAD
+'
+
+test_expect_success 'prefers config "clone.defaultRemoteName" over default' '
+
+ test_config_global clone.defaultRemoteName from_config &&
+ git clone parent clone-config-origin &&
+ git -C clone-config-origin rev-parse --verify refs/remotes/from_config/main
+
+'
+
+test_expect_success 'prefers --origin over -c config' '
+
+ git clone -c clone.defaultRemoteName=inline --origin from_option parent clone-o-and-inline-config &&
+ git -C clone-o-and-inline-config rev-parse --verify refs/remotes/from_option/main
+
+'
+
+test_expect_success 'redirected clone does not show progress' '
+
+ git clone "file://$(pwd)/parent" clone-redirected >out 2>err &&
+ ! grep % err &&
+ test_grep ! "Checking connectivity" err
+
+'
+
+test_expect_success 'redirected clone -v does show progress' '
+
+ git clone --progress "file://$(pwd)/parent" clone-redirected-progress \
+ >out 2>err &&
+ grep % err
+
+'
+
+test_expect_success 'clone does not segfault with --bare and core.bare=false' '
+ test_config_global core.bare false &&
+ git clone --bare parent clone-bare &&
+ echo true >expect &&
+ git -C clone-bare rev-parse --is-bare-repository >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'chooses correct default initial branch name' '
+ GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME= \
+ git -c init.defaultBranch=foo init --bare empty &&
+ test_config -C empty lsrefs.unborn advertise &&
+ GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME= \
+ git -c init.defaultBranch=up -c protocol.version=2 clone empty whats-up &&
+ test refs/heads/foo = $(git -C whats-up symbolic-ref HEAD) &&
+ test refs/heads/foo = $(git -C whats-up config branch.foo.merge)
+'
+
+test_expect_success 'guesses initial branch name correctly' '
+ git init --initial-branch=guess initial-branch &&
+ test_commit -C initial-branch no-spoilers &&
+ git -C initial-branch branch abc guess &&
+ git clone initial-branch is-it &&
+ test refs/heads/guess = $(git -C is-it symbolic-ref HEAD) &&
+
+ git -c init.defaultBranch=none init --bare no-head &&
+ git -C initial-branch push ../no-head guess abc &&
+ GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME= \
+ git clone no-head is-it2 &&
+ test_must_fail git -C is-it2 symbolic-ref refs/remotes/origin/HEAD &&
+ git -C no-head update-ref --no-deref HEAD refs/heads/guess &&
+ GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME= \
+ git -c init.defaultBranch=guess clone no-head is-it3 &&
+ test refs/remotes/origin/guess = \
+ $(git -C is-it3 symbolic-ref refs/remotes/origin/HEAD)
+'
+
+test_done
diff --git a/t/t5607-clone-bundle.sh b/t/t5607-clone-bundle.sh
new file mode 100755
index 0000000..0d1e92d
--- /dev/null
+++ b/t/t5607-clone-bundle.sh
@@ -0,0 +1,172 @@
+#!/bin/sh
+
+test_description='some bundle related tests'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ test_oid_cache <<-EOF &&
+ version sha1:2
+ version sha256:3
+ EOF
+ test_commit initial &&
+ test_tick &&
+ git tag -m tag tag &&
+ test_commit second &&
+ test_commit third &&
+ git tag -d initial &&
+ git tag -d second &&
+ git tag -d third
+'
+
+test_expect_success '"verify" needs a worktree' '
+ git bundle create tip.bundle -1 main &&
+ nongit test_must_fail git bundle verify ../tip.bundle 2>err &&
+ test_grep "need a repository" err
+'
+
+test_expect_success 'annotated tags can be excluded by rev-list options' '
+ git bundle create bundle --all --since=7.Apr.2005.15:14:00.-0700 &&
+ cat >expect <<-EOF &&
+ $(git rev-parse HEAD) HEAD
+ $(git rev-parse tag) refs/tags/tag
+ $(git rev-parse main) refs/heads/main
+ EOF
+ git ls-remote bundle >actual &&
+ test_cmp expect actual &&
+
+ git bundle create bundle --all --since=7.Apr.2005.15:16:00.-0700 &&
+ cat >expect <<-EOF &&
+ $(git rev-parse HEAD) HEAD
+ $(git rev-parse main) refs/heads/main
+ EOF
+ git ls-remote bundle >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'die if bundle file cannot be created' '
+ mkdir adir &&
+ test_must_fail git bundle create adir --all
+'
+
+test_expect_success 'bundle --stdin' '
+ echo main | git bundle create stdin-bundle.bdl --stdin &&
+ cat >expect <<-EOF &&
+ $(git rev-parse main) refs/heads/main
+ EOF
+ git ls-remote stdin-bundle.bdl >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'bundle --stdin <rev-list options>' '
+ echo main | git bundle create hybrid-bundle.bdl --stdin tag &&
+ cat >expect <<-EOF &&
+ $(git rev-parse main) refs/heads/main
+ EOF
+ git ls-remote stdin-bundle.bdl >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'empty bundle file is rejected' '
+ >empty-bundle &&
+ test_must_fail git fetch empty-bundle
+'
+
+# This triggers a bug in older versions where the resulting line (with
+# --pretty=oneline) was longer than a 1024-char buffer.
+test_expect_success 'ridiculously long subject in boundary' '
+ >file4 &&
+ test_tick &&
+ git add file4 &&
+ printf "%01200d\n" 0 | git commit -F - &&
+ test_commit fifth &&
+ git bundle create long-subject-bundle.bdl HEAD^..HEAD &&
+ cat >expect <<-EOF &&
+ $(git rev-parse main) HEAD
+ EOF
+ git bundle list-heads long-subject-bundle.bdl >actual &&
+ test_cmp expect actual &&
+
+ git fetch long-subject-bundle.bdl &&
+
+ algo=$(test_oid algo) &&
+ if test "$algo" != sha1
+ then
+ echo "@object-format=sha256"
+ fi >expect &&
+ cat >>expect <<-EOF &&
+ -$(git log --pretty=format:"%H %s" -1 HEAD^)
+ $(git rev-parse HEAD) HEAD
+ EOF
+
+ if test "$algo" = sha1
+ then
+ head -n 3 long-subject-bundle.bdl
+ else
+ head -n 4 long-subject-bundle.bdl
+ fi | grep -v "^#" >actual &&
+
+ test_cmp expect actual
+'
+
+test_expect_success 'prerequisites with an empty commit message' '
+ >file1 &&
+ git add file1 &&
+ test_tick &&
+ git commit --allow-empty-message -m "" &&
+ test_commit file2 &&
+ git bundle create bundle HEAD^.. &&
+ git bundle verify bundle
+'
+
+test_expect_success 'failed bundle creation does not leave cruft' '
+ # This fails because the bundle would be empty.
+ test_must_fail git bundle create fail.bundle main..main &&
+ test_path_is_missing fail.bundle.lock
+'
+
+test_expect_success 'fetch SHA-1 from bundle' '
+ test_create_repo foo &&
+ test_commit -C foo x &&
+ git -C foo bundle create tip.bundle -1 main &&
+ git -C foo rev-parse HEAD >hash &&
+
+ # Exercise to ensure that fetching a SHA-1 from a bundle works with no
+ # errors
+ git fetch --no-tags foo/tip.bundle "$(cat hash)"
+'
+
+test_expect_success 'git bundle uses expected default format' '
+ git bundle create bundle HEAD^.. &&
+ cat >expect <<-EOF &&
+ # v$(test_oid version) git bundle
+ EOF
+ head -n1 bundle >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git bundle v3 has expected contents' '
+ git branch side HEAD &&
+ git bundle create --version=3 bundle HEAD^..side &&
+ head -n2 bundle >actual &&
+ cat >expect <<-EOF &&
+ # v3 git bundle
+ @object-format=$(test_oid algo)
+ EOF
+ test_cmp expect actual &&
+ git bundle verify bundle
+'
+
+test_expect_success 'git bundle v3 rejects unknown capabilities' '
+ cat >new <<-EOF &&
+ # v3 git bundle
+ @object-format=$(test_oid algo)
+ @unknown=silly
+ EOF
+ test_must_fail git bundle verify new 2>output &&
+ test_grep "unknown capability .unknown=silly." output
+'
+
+test_done
diff --git a/t/t5608-clone-2gb.sh b/t/t5608-clone-2gb.sh
new file mode 100755
index 0000000..87a8cd9
--- /dev/null
+++ b/t/t5608-clone-2gb.sh
@@ -0,0 +1,52 @@
+#!/bin/sh
+
+test_description='Test cloning a repository larger than 2 gigabyte'
+. ./test-lib.sh
+
+if ! test_bool_env GIT_TEST_CLONE_2GB false
+then
+ skip_all='expensive 2GB clone test; enable with GIT_TEST_CLONE_2GB=true'
+ test_done
+fi
+
+test_expect_success 'setup' '
+
+ git config pack.compression 0 &&
+ git config pack.depth 0 &&
+ blobsize=$((100*1024*1024)) &&
+ blobcount=$((2*1024*1024*1024/$blobsize+1)) &&
+ i=1 &&
+ (while test $i -le $blobcount
+ do
+ printf "Generating blob $i/$blobcount\r" >&2 &&
+ printf "blob\nmark :$i\ndata $blobsize\n" &&
+ #test-tool genrandom $i $blobsize &&
+ printf "%-${blobsize}s" $i &&
+ echo "M 100644 :$i $i" >> commit &&
+ i=$(($i+1)) ||
+ echo $? > exit-status
+ done &&
+ echo "commit refs/heads/main" &&
+ echo "author A U Thor <author@email.com> 123456789 +0000" &&
+ echo "committer C O Mitter <committer@email.com> 123456789 +0000" &&
+ echo "data 5" &&
+ echo ">2gb" &&
+ cat commit) |
+ git fast-import --big-file-threshold=2 &&
+ test ! -f exit-status
+
+'
+
+test_expect_success 'clone - bare' '
+
+ git clone --bare --no-hardlinks . clone-bare
+
+'
+
+test_expect_success 'clone - with worktree, file:// protocol' '
+
+ git clone "file://$(pwd)" clone-wt
+
+'
+
+test_done
diff --git a/t/t5609-clone-branch.sh b/t/t5609-clone-branch.sh
new file mode 100755
index 0000000..252e1f7
--- /dev/null
+++ b/t/t5609-clone-branch.sh
@@ -0,0 +1,74 @@
+#!/bin/sh
+
+test_description='clone --branch option'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+check_HEAD() {
+ echo refs/heads/"$1" >expect &&
+ git symbolic-ref HEAD >actual &&
+ test_cmp expect actual
+}
+
+check_file() {
+ echo "$1" >expect &&
+ test_cmp expect file
+}
+
+test_expect_success 'setup' '
+ mkdir parent &&
+ (cd parent && git init &&
+ echo one >file && git add file && git commit -m one &&
+ git checkout -b two &&
+ echo two >file && git add file && git commit -m two &&
+ git checkout main) &&
+ mkdir empty &&
+ (cd empty && git init)
+'
+
+test_expect_success 'vanilla clone chooses HEAD' '
+ git clone parent clone &&
+ (cd clone &&
+ check_HEAD main &&
+ check_file one
+ )
+'
+
+test_expect_success 'clone -b chooses specified branch' '
+ git clone -b two parent clone-two &&
+ (cd clone-two &&
+ check_HEAD two &&
+ check_file two
+ )
+'
+
+test_expect_success 'clone -b sets up tracking' '
+ (cd clone-two &&
+ echo origin >expect &&
+ git config branch.two.remote >actual &&
+ echo refs/heads/two >>expect &&
+ git config branch.two.merge >>actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'clone -b does not munge remotes/origin/HEAD' '
+ (cd clone-two &&
+ echo refs/remotes/origin/main >expect &&
+ git symbolic-ref refs/remotes/origin/HEAD >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'clone -b with bogus branch' '
+ test_must_fail git clone -b bogus parent clone-bogus
+'
+
+test_expect_success 'clone -b not allowed with empty repos' '
+ test_must_fail git clone -b branch empty clone-branch-empty
+'
+
+test_done
diff --git a/t/t5610-clone-detached.sh b/t/t5610-clone-detached.sh
new file mode 100755
index 0000000..022ed3d
--- /dev/null
+++ b/t/t5610-clone-detached.sh
@@ -0,0 +1,80 @@
+#!/bin/sh
+
+test_description='test cloning a repository with detached HEAD'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+head_is_detached() {
+ git --git-dir=$1/.git rev-parse --verify HEAD &&
+ test_must_fail git --git-dir=$1/.git symbolic-ref HEAD
+}
+
+test_expect_success 'setup' '
+ echo one >file &&
+ git add file &&
+ git commit -m one &&
+ echo two >file &&
+ git commit -a -m two &&
+ git tag two &&
+ echo three >file &&
+ git commit -a -m three
+'
+
+test_expect_success 'clone repo (detached HEAD points to branch)' '
+ git checkout main^0 &&
+ git clone "file://$PWD" detached-branch
+'
+test_expect_success 'cloned HEAD matches' '
+ echo three >expect &&
+ git --git-dir=detached-branch/.git log -1 --format=%s >actual &&
+ test_cmp expect actual
+'
+test_expect_failure 'cloned HEAD is detached' '
+ head_is_detached detached-branch
+'
+
+test_expect_success 'clone repo (detached HEAD points to tag)' '
+ git checkout two^0 &&
+ git clone "file://$PWD" detached-tag
+'
+test_expect_success 'cloned HEAD matches' '
+ echo two >expect &&
+ git --git-dir=detached-tag/.git log -1 --format=%s >actual &&
+ test_cmp expect actual
+'
+test_expect_success 'cloned HEAD is detached' '
+ head_is_detached detached-tag
+'
+
+test_expect_success 'clone repo (detached HEAD points to history)' '
+ git checkout two^ &&
+ git clone "file://$PWD" detached-history
+'
+test_expect_success 'cloned HEAD matches' '
+ echo one >expect &&
+ git --git-dir=detached-history/.git log -1 --format=%s >actual &&
+ test_cmp expect actual
+'
+test_expect_success 'cloned HEAD is detached' '
+ head_is_detached detached-history
+'
+
+test_expect_success 'clone repo (orphan detached HEAD)' '
+ git checkout main^0 &&
+ echo four >file &&
+ git commit -a -m four &&
+ git clone "file://$PWD" detached-orphan
+'
+test_expect_success 'cloned HEAD matches' '
+ echo four >expect &&
+ git --git-dir=detached-orphan/.git log -1 --format=%s >actual &&
+ test_cmp expect actual
+'
+test_expect_success 'cloned HEAD is detached' '
+ head_is_detached detached-orphan
+'
+
+test_done
diff --git a/t/t5611-clone-config.sh b/t/t5611-clone-config.sh
new file mode 100755
index 0000000..298d4be
--- /dev/null
+++ b/t/t5611-clone-config.sh
@@ -0,0 +1,137 @@
+#!/bin/sh
+
+test_description='tests for git clone -c key=value'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'clone -c sets config in cloned repo' '
+ rm -rf child &&
+ git clone -c core.foo=bar . child &&
+ echo bar >expect &&
+ git --git-dir=child/.git config core.foo >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'clone -c can set multi-keys' '
+ rm -rf child &&
+ git clone -c core.foo=bar -c core.foo=baz . child &&
+ test_write_lines bar baz >expect &&
+ git --git-dir=child/.git config --get-all core.foo >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'clone -c can set multi-keys, including some empty' '
+ rm -rf child &&
+ git clone -c credential.helper= -c credential.helper=hi . child &&
+ printf "%s\n" "" hi >expect &&
+ git --git-dir=child/.git config --get-all credential.helper >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'clone -c without a value is boolean true' '
+ rm -rf child &&
+ git clone -c core.foo . child &&
+ echo true >expect &&
+ git --git-dir=child/.git config --bool core.foo >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'clone -c config is available during clone' '
+ echo content >file &&
+ git add file &&
+ git commit -m one &&
+ rm -rf child &&
+ git clone -c core.autocrlf . child &&
+ printf "content\\r\\n" >expect &&
+ test_cmp expect child/file
+'
+
+test_expect_success 'clone -c remote.origin.fetch=<refspec> works' '
+ rm -rf child &&
+ git update-ref refs/grab/it refs/heads/main &&
+ git update-ref refs/leave/out refs/heads/main &&
+ git clone -c "remote.origin.fetch=+refs/grab/*:refs/grab/*" . child &&
+ git -C child for-each-ref --format="%(refname)" >actual &&
+
+ cat >expect <<-\EOF &&
+ refs/grab/it
+ refs/heads/main
+ refs/remotes/origin/HEAD
+ refs/remotes/origin/main
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'git -c remote.origin.fetch=<refspec> clone works' '
+ rm -rf child &&
+ git -c "remote.origin.fetch=+refs/grab/*:refs/grab/*" clone . child &&
+ git -C child for-each-ref --format="%(refname)" >actual &&
+
+ cat >expect <<-\EOF &&
+ refs/grab/it
+ refs/heads/main
+ refs/remotes/origin/HEAD
+ refs/remotes/origin/main
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'clone -c remote.<remote>.fetch=<refspec> --origin=<name>' '
+ rm -rf child &&
+ git clone --origin=upstream \
+ -c "remote.upstream.fetch=+refs/grab/*:refs/grab/*" \
+ -c "remote.origin.fetch=+refs/leave/*:refs/leave/*" \
+ . child &&
+ git -C child for-each-ref --format="%(refname)" >actual &&
+
+ cat >expect <<-\EOF &&
+ refs/grab/it
+ refs/heads/main
+ refs/remotes/upstream/HEAD
+ refs/remotes/upstream/main
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'set up shallow repository' '
+ git clone --depth=1 --no-local . shallow-repo
+'
+
+test_expect_success 'clone.rejectshallow=true should reject cloning shallow repo' '
+ test_when_finished "rm -rf out" &&
+ test_must_fail git -c clone.rejectshallow=true clone --no-local shallow-repo out 2>err &&
+ test_grep -e "source repository is shallow, reject to clone." err &&
+
+ git -c clone.rejectshallow=false clone --no-local shallow-repo out
+'
+
+test_expect_success 'option --[no-]reject-shallow override clone.rejectshallow config' '
+ test_when_finished "rm -rf out" &&
+ test_must_fail git -c clone.rejectshallow=false clone --reject-shallow --no-local shallow-repo out 2>err &&
+ test_grep -e "source repository is shallow, reject to clone." err &&
+
+ git -c clone.rejectshallow=true clone --no-reject-shallow --no-local shallow-repo out
+'
+
+test_expect_success 'clone.rejectshallow=true should succeed cloning normal repo' '
+ test_when_finished "rm -rf out" &&
+ git -c clone.rejectshallow=true clone --no-local . out
+'
+
+test_expect_success MINGW 'clone -c core.hideDotFiles' '
+ test_commit attributes .gitattributes "" &&
+ rm -rf child &&
+ git clone -c core.hideDotFiles=false . child &&
+ ! test_path_is_hidden child/.gitattributes &&
+ rm -rf child &&
+ git clone -c core.hideDotFiles=dotGitOnly . child &&
+ ! test_path_is_hidden child/.gitattributes &&
+ rm -rf child &&
+ git clone -c core.hideDotFiles=true . child &&
+ test_path_is_hidden child/.gitattributes
+'
+
+test_done
diff --git a/t/t5612-clone-refspec.sh b/t/t5612-clone-refspec.sh
new file mode 100755
index 0000000..3126cfd
--- /dev/null
+++ b/t/t5612-clone-refspec.sh
@@ -0,0 +1,238 @@
+#!/bin/sh
+
+test_description='test refspec written by clone-command'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ # Make two branches, "main" and "side"
+ echo one >file &&
+ git add file &&
+ git commit -m one &&
+ echo two >file &&
+ git commit -a -m two &&
+ git tag two &&
+ echo three >file &&
+ git commit -a -m three &&
+ git checkout -b side &&
+ echo four >file &&
+ git commit -a -m four &&
+ git checkout main &&
+ git tag five &&
+
+ # default clone
+ git clone . dir_all &&
+
+ # default clone --no-tags
+ git clone --no-tags . dir_all_no_tags &&
+
+ # default --single that follows HEAD=main
+ git clone --single-branch . dir_main &&
+
+ # default --single that follows HEAD=main with no tags
+ git clone --single-branch --no-tags . dir_main_no_tags &&
+
+ # default --single that follows HEAD=side
+ git checkout side &&
+ git clone --single-branch . dir_side &&
+
+ # explicit --single that follows side
+ git checkout main &&
+ git clone --single-branch --branch side . dir_side2 &&
+
+ # default --single with --mirror
+ git clone --single-branch --mirror . dir_mirror &&
+
+ # default --single with --branch and --mirror
+ git clone --single-branch --mirror --branch side . dir_mirror_side &&
+
+ # --single that does not know what branch to follow
+ git checkout two^ &&
+ git clone --single-branch . dir_detached &&
+
+ # explicit --single with tag
+ git clone --single-branch --branch two . dir_tag &&
+
+ # explicit --single with tag and --no-tags
+ git clone --single-branch --no-tags --branch two . dir_tag_no_tags &&
+
+ # advance both "main" and "side" branches
+ git checkout side &&
+ echo five >file &&
+ git commit -a -m five &&
+ git checkout main &&
+ echo six >file &&
+ git commit -a -m six &&
+
+ # update tag
+ git tag -d two && git tag two
+'
+
+test_expect_success 'by default all branches will be kept updated' '
+ (
+ cd dir_all &&
+ git fetch &&
+ git for-each-ref refs/remotes/origin >refs &&
+ sed -e "/HEAD$/d" \
+ -e "s|/remotes/origin/|/heads/|" refs >../actual
+ ) &&
+ # follow both main and side
+ git for-each-ref refs/heads >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'by default no tags will be kept updated' '
+ (
+ cd dir_all &&
+ git fetch &&
+ git for-each-ref refs/tags >../actual
+ ) &&
+ git for-each-ref refs/tags >expect &&
+ ! test_cmp expect actual &&
+ test_line_count = 2 actual
+'
+
+test_expect_success 'clone with --no-tags' '
+ (
+ cd dir_all_no_tags &&
+ grep tagOpt .git/config &&
+ git fetch &&
+ git for-each-ref refs/tags >../actual
+ ) &&
+ test_must_be_empty actual
+'
+
+test_expect_success '--single-branch while HEAD pointing at main' '
+ (
+ cd dir_main &&
+ git fetch --force &&
+ git for-each-ref refs/remotes/origin >refs &&
+ sed -e "/HEAD$/d" \
+ -e "s|/remotes/origin/|/heads/|" refs >../actual
+ ) &&
+ # only follow main
+ git for-each-ref refs/heads/main >expect &&
+ # get & check latest tags
+ test_cmp expect actual &&
+ (
+ cd dir_main &&
+ git fetch --tags --force &&
+ git for-each-ref refs/tags >../actual
+ ) &&
+ git for-each-ref refs/tags >expect &&
+ test_cmp expect actual &&
+ test_line_count = 2 actual
+'
+
+test_expect_success '--single-branch while HEAD pointing at main and --no-tags' '
+ (
+ cd dir_main_no_tags &&
+ git fetch &&
+ git for-each-ref refs/remotes/origin >refs &&
+ sed -e "/HEAD$/d" \
+ -e "s|/remotes/origin/|/heads/|" refs >../actual
+ ) &&
+ # only follow main
+ git for-each-ref refs/heads/main >expect &&
+ test_cmp expect actual &&
+ # get tags (noop)
+ (
+ cd dir_main_no_tags &&
+ git fetch &&
+ git for-each-ref refs/tags >../actual
+ ) &&
+ test_must_be_empty actual &&
+ test_line_count = 0 actual &&
+ # get tags with --tags overrides tagOpt
+ (
+ cd dir_main_no_tags &&
+ git fetch --tags &&
+ git for-each-ref refs/tags >../actual
+ ) &&
+ git for-each-ref refs/tags >expect &&
+ test_cmp expect actual &&
+ test_line_count = 2 actual
+'
+
+test_expect_success '--single-branch while HEAD pointing at side' '
+ (
+ cd dir_side &&
+ git fetch &&
+ git for-each-ref refs/remotes/origin >refs &&
+ sed -e "/HEAD$/d" \
+ -e "s|/remotes/origin/|/heads/|" refs >../actual
+ ) &&
+ # only follow side
+ git for-each-ref refs/heads/side >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success '--single-branch with explicit --branch side' '
+ (
+ cd dir_side2 &&
+ git fetch &&
+ git for-each-ref refs/remotes/origin >refs &&
+ sed -e "/HEAD$/d" \
+ -e "s|/remotes/origin/|/heads/|" refs >../actual
+ ) &&
+ # only follow side
+ git for-each-ref refs/heads/side >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success '--single-branch with explicit --branch with tag fetches updated tag' '
+ (
+ cd dir_tag &&
+ git fetch &&
+ git for-each-ref refs/tags >../actual
+ ) &&
+ git for-each-ref refs/tags >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success '--single-branch with explicit --branch with tag fetches updated tag despite --no-tags' '
+ (
+ cd dir_tag_no_tags &&
+ git fetch &&
+ git for-each-ref refs/tags >../actual
+ ) &&
+ git for-each-ref refs/tags/two >expect &&
+ test_cmp expect actual &&
+ test_line_count = 1 actual
+'
+
+test_expect_success '--single-branch with --mirror' '
+ (
+ cd dir_mirror &&
+ git fetch &&
+ git for-each-ref refs > ../actual
+ ) &&
+ git for-each-ref refs >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success '--single-branch with explicit --branch and --mirror' '
+ (
+ cd dir_mirror_side &&
+ git fetch &&
+ git for-each-ref refs > ../actual
+ ) &&
+ git for-each-ref refs >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success '--single-branch with detached' '
+ (
+ cd dir_detached &&
+ git fetch &&
+ git for-each-ref refs/remotes/origin >refs &&
+ sed -e "/HEAD$/d" \
+ -e "s|/remotes/origin/|/heads/|" refs >../actual
+ ) &&
+ # nothing
+ test_must_be_empty actual
+'
+
+test_done
diff --git a/t/t5613-info-alternate.sh b/t/t5613-info-alternate.sh
new file mode 100755
index 0000000..7708cba
--- /dev/null
+++ b/t/t5613-info-alternate.sh
@@ -0,0 +1,141 @@
+#!/bin/sh
+#
+# Copyright (C) 2006 Martin Waitz <tali@admingilde.org>
+#
+
+test_description='test transitive info/alternate entries'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'preparing first repository' '
+ test_create_repo A && (
+ cd A &&
+ echo "Hello World" > file1 &&
+ git add file1 &&
+ git commit -m "Initial commit" file1 &&
+ git repack -a -d &&
+ git prune
+ )
+'
+
+test_expect_success 'preparing second repository' '
+ git clone -l -s A B && (
+ cd B &&
+ echo "foo bar" > file2 &&
+ git add file2 &&
+ git commit -m "next commit" file2 &&
+ git repack -a -d -l &&
+ git prune
+ )
+'
+
+test_expect_success 'preparing third repository' '
+ git clone -l -s B C && (
+ cd C &&
+ echo "Goodbye, cruel world" > file3 &&
+ git add file3 &&
+ git commit -m "one more" file3 &&
+ git repack -a -d -l &&
+ git prune
+ )
+'
+
+test_expect_success 'count-objects shows the alternates' '
+ cat >expect <<-EOF &&
+ alternate: $(pwd)/B/.git/objects
+ alternate: $(pwd)/A/.git/objects
+ EOF
+ git -C C count-objects -v >actual &&
+ grep ^alternate: actual >actual.alternates &&
+ test_cmp expect actual.alternates
+'
+
+# Note: These tests depend on the hard-coded value of 5 as the maximum depth
+# we will follow recursion. We start the depth at 0 and count links, not
+# repositories. This means that in a chain like:
+#
+# A --> B --> C --> D --> E --> F --> G --> H
+# 0 1 2 3 4 5 6
+#
+# we are OK at "G", but break at "H", even though "H" is actually the 8th
+# repository, not the 6th, which you might expect. Counting the links allows
+# N+1 repositories, and counting from 0 to 5 inclusive allows 6 links.
+#
+# Note also that we must use "--bare -l" to make the link to H. The "-l"
+# ensures we do not do a connectivity check, and the "--bare" makes sure
+# we do not try to checkout the result (which needs objects), either of
+# which would cause the clone to fail.
+test_expect_success 'creating too deep nesting' '
+ git clone -l -s C D &&
+ git clone -l -s D E &&
+ git clone -l -s E F &&
+ git clone -l -s F G &&
+ git clone --bare -l -s G H
+'
+
+test_expect_success 'validity of seventh repository' '
+ git -C G fsck
+'
+
+test_expect_success 'invalidity of eighth repository' '
+ test_must_fail git -C H fsck
+'
+
+test_expect_success 'breaking of loops' '
+ echo "$(pwd)"/B/.git/objects >>A/.git/objects/info/alternates &&
+ git -C C fsck
+'
+
+test_expect_success 'that info/alternates is necessary' '
+ rm -f C/.git/objects/info/alternates &&
+ test_must_fail git -C C fsck
+'
+
+test_expect_success 'that relative alternate is possible for current dir' '
+ echo "../../../B/.git/objects" >C/.git/objects/info/alternates &&
+ git fsck
+'
+
+test_expect_success 'that relative alternate is recursive' '
+ git -C D fsck
+'
+
+# we can reach "A" from our new repo both directly, and via "C".
+# The deep/subdir is there to make sure we are not doing a stupid
+# pure-text comparison of the alternate names.
+test_expect_success 'relative duplicates are eliminated' '
+ mkdir -p deep/subdir &&
+ git init --bare deep/subdir/duplicate.git &&
+ cat >deep/subdir/duplicate.git/objects/info/alternates <<-\EOF &&
+ ../../../../C/.git/objects
+ ../../../../A/.git/objects
+ EOF
+ cat >expect <<-EOF &&
+ alternate: $(pwd)/C/.git/objects
+ alternate: $(pwd)/B/.git/objects
+ alternate: $(pwd)/A/.git/objects
+ EOF
+ git -C deep/subdir/duplicate.git count-objects -v >actual &&
+ grep ^alternate: actual >actual.alternates &&
+ test_cmp expect actual.alternates
+'
+
+test_expect_success CASE_INSENSITIVE_FS 'dup finding can be case-insensitive' '
+ git init --bare insensitive.git &&
+ # the previous entry for "A" will have used uppercase
+ cat >insensitive.git/objects/info/alternates <<-\EOF &&
+ ../../C/.git/objects
+ ../../a/.git/objects
+ EOF
+ cat >expect <<-EOF &&
+ alternate: $(pwd)/C/.git/objects
+ alternate: $(pwd)/B/.git/objects
+ alternate: $(pwd)/A/.git/objects
+ EOF
+ git -C insensitive.git count-objects -v >actual &&
+ grep ^alternate: actual >actual.alternates &&
+ test_cmp expect actual.alternates
+'
+
+test_done
diff --git a/t/t5614-clone-submodules-shallow.sh b/t/t5614-clone-submodules-shallow.sh
new file mode 100755
index 0000000..c2a2bb4
--- /dev/null
+++ b/t/t5614-clone-submodules-shallow.sh
@@ -0,0 +1,131 @@
+#!/bin/sh
+
+test_description='Test shallow cloning of repos with submodules'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+pwd=$(pwd)
+
+test_expect_success 'setup' '
+ git checkout -b main &&
+ test_commit commit1 &&
+ test_commit commit2 &&
+ mkdir sub &&
+ (
+ cd sub &&
+ git init &&
+ test_commit subcommit1 &&
+ test_commit subcommit2 &&
+ test_commit subcommit3
+ ) &&
+ git submodule add "file://$pwd/sub" sub &&
+ git commit -m "add submodule"
+'
+
+test_expect_success 'nonshallow clone implies nonshallow submodule' '
+ test_when_finished "rm -rf super_clone" &&
+ test_config_global protocol.file.allow always &&
+ git clone --recurse-submodules "file://$pwd/." super_clone &&
+ git -C super_clone log --oneline >lines &&
+ test_line_count = 3 lines &&
+ git -C super_clone/sub log --oneline >lines &&
+ test_line_count = 3 lines
+'
+
+test_expect_success 'shallow clone with shallow submodule' '
+ test_when_finished "rm -rf super_clone" &&
+ test_config_global protocol.file.allow always &&
+ git clone --recurse-submodules --depth 2 --shallow-submodules "file://$pwd/." super_clone &&
+ git -C super_clone log --oneline >lines &&
+ test_line_count = 2 lines &&
+ git -C super_clone/sub log --oneline >lines &&
+ test_line_count = 1 lines
+'
+
+test_expect_success 'shallow clone does not imply shallow submodule' '
+ test_when_finished "rm -rf super_clone" &&
+ test_config_global protocol.file.allow always &&
+ git clone --recurse-submodules --depth 2 "file://$pwd/." super_clone &&
+ git -C super_clone log --oneline >lines &&
+ test_line_count = 2 lines &&
+ git -C super_clone/sub log --oneline >lines &&
+ test_line_count = 3 lines
+'
+
+test_expect_success 'shallow clone with non shallow submodule' '
+ test_when_finished "rm -rf super_clone" &&
+ test_config_global protocol.file.allow always &&
+ git clone --recurse-submodules --depth 2 --no-shallow-submodules "file://$pwd/." super_clone &&
+ git -C super_clone log --oneline >lines &&
+ test_line_count = 2 lines &&
+ git -C super_clone/sub log --oneline >lines &&
+ test_line_count = 3 lines
+'
+
+test_expect_success 'non shallow clone with shallow submodule' '
+ test_when_finished "rm -rf super_clone" &&
+ test_config_global protocol.file.allow always &&
+ git clone --recurse-submodules --no-local --shallow-submodules "file://$pwd/." super_clone &&
+ git -C super_clone log --oneline >lines &&
+ test_line_count = 3 lines &&
+ git -C super_clone/sub log --oneline >lines &&
+ test_line_count = 1 lines
+'
+
+test_expect_success 'clone follows shallow recommendation' '
+ test_when_finished "rm -rf super_clone" &&
+ test_config_global protocol.file.allow always &&
+ git config -f .gitmodules submodule.sub.shallow true &&
+ git add .gitmodules &&
+ git commit -m "recommend shallow for sub" &&
+ git clone --recurse-submodules --no-local "file://$pwd/." super_clone &&
+ (
+ cd super_clone &&
+ git log --oneline >lines &&
+ test_line_count = 4 lines
+ ) &&
+ (
+ cd super_clone/sub &&
+ git log --oneline >lines &&
+ test_line_count = 1 lines
+ )
+'
+
+test_expect_success 'get unshallow recommended shallow submodule' '
+ test_when_finished "rm -rf super_clone" &&
+ test_config_global protocol.file.allow always &&
+ git clone --no-local "file://$pwd/." super_clone &&
+ (
+ cd super_clone &&
+ git submodule update --init --no-recommend-shallow &&
+ git log --oneline >lines &&
+ test_line_count = 4 lines
+ ) &&
+ (
+ cd super_clone/sub &&
+ git log --oneline >lines &&
+ test_line_count = 3 lines
+ )
+'
+
+test_expect_success 'clone follows non shallow recommendation' '
+ test_when_finished "rm -rf super_clone" &&
+ test_config_global protocol.file.allow always &&
+ git config -f .gitmodules submodule.sub.shallow false &&
+ git add .gitmodules &&
+ git commit -m "recommend non shallow for sub" &&
+ git clone --recurse-submodules --no-local "file://$pwd/." super_clone &&
+ (
+ cd super_clone &&
+ git log --oneline >lines &&
+ test_line_count = 5 lines
+ ) &&
+ (
+ cd super_clone/sub &&
+ git log --oneline >lines &&
+ test_line_count = 3 lines
+ )
+'
+
+test_done
diff --git a/t/t5615-alternate-env.sh b/t/t5615-alternate-env.sh
new file mode 100755
index 0000000..83513e4
--- /dev/null
+++ b/t/t5615-alternate-env.sh
@@ -0,0 +1,92 @@
+#!/bin/sh
+
+test_description='handling of alternates in environment variables'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+check_obj () {
+ alt=$1; shift
+ while read obj expect
+ do
+ echo "$obj" >&5 &&
+ echo "$obj $expect" >&6
+ done 5>input 6>expect &&
+ GIT_ALTERNATE_OBJECT_DIRECTORIES=$alt \
+ git "$@" cat-file --batch-check='%(objectname) %(objecttype)' \
+ <input >actual &&
+ test_cmp expect actual
+}
+
+test_expect_success 'create alternate repositories' '
+ git init --bare one.git &&
+ one=$(echo one | git -C one.git hash-object -w --stdin) &&
+ git init --bare two.git &&
+ two=$(echo two | git -C two.git hash-object -w --stdin)
+'
+
+test_expect_success 'objects inaccessible without alternates' '
+ check_obj "" <<-EOF
+ $one missing
+ $two missing
+ EOF
+'
+
+test_expect_success 'access alternate via absolute path' '
+ check_obj "$PWD/one.git/objects" <<-EOF
+ $one blob
+ $two missing
+ EOF
+'
+
+test_expect_success 'access multiple alternates' '
+ check_obj "$PWD/one.git/objects:$PWD/two.git/objects" <<-EOF
+ $one blob
+ $two blob
+ EOF
+'
+
+# bare paths are relative from $GIT_DIR
+test_expect_success 'access alternate via relative path (bare)' '
+ git init --bare bare.git &&
+ check_obj "../one.git/objects" -C bare.git <<-EOF
+ $one blob
+ EOF
+'
+
+# non-bare paths are relative to top of worktree
+test_expect_success 'access alternate via relative path (worktree)' '
+ git init worktree &&
+ check_obj "../one.git/objects" -C worktree <<-EOF
+ $one blob
+ EOF
+'
+
+# path is computed after moving to top-level of worktree
+test_expect_success 'access alternate via relative path (subdir)' '
+ mkdir subdir &&
+ check_obj "one.git/objects" -C subdir <<-EOF
+ $one blob
+ EOF
+'
+
+# set variables outside test to avoid quote insanity; the \057 is '/',
+# which doesn't need quoting, but just confirms that de-quoting
+# is working.
+quoted='"one.git\057objects"'
+unquoted='two.git/objects'
+test_expect_success 'mix of quoted and unquoted alternates' '
+ check_obj "$quoted:$unquoted" <<-EOF
+ $one blob
+ $two blob
+ EOF
+'
+
+test_expect_success !MINGW 'broken quoting falls back to interpreting raw' '
+ mv one.git \"one.git &&
+ check_obj \"one.git/objects <<-EOF
+ $one blob
+ EOF
+'
+
+test_done
diff --git a/t/t5616-partial-clone.sh b/t/t5616-partial-clone.sh
new file mode 100755
index 0000000..2da7291
--- /dev/null
+++ b/t/t5616-partial-clone.sh
@@ -0,0 +1,879 @@
+#!/bin/sh
+
+test_description='git partial clone'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+# create a normal "src" repo where we can later create new commits.
+# expect_1.oids will contain a list of the OIDs of all blobs.
+test_expect_success 'setup normal src repo' '
+ echo "{print \$1}" >print_1.awk &&
+ echo "{print \$2}" >print_2.awk &&
+
+ git init src &&
+ for n in 1 2 3 4
+ do
+ echo "This is file: $n" > src/file.$n.txt &&
+ git -C src add file.$n.txt &&
+ git -C src commit -m "file $n" &&
+ git -C src ls-files -s file.$n.txt >>temp || return 1
+ done &&
+ awk -f print_2.awk <temp | sort >expect_1.oids &&
+ test_line_count = 4 expect_1.oids
+'
+
+# bare clone "src" giving "srv.bare" for use as our server.
+test_expect_success 'setup bare clone for server' '
+ git clone --bare "file://$(pwd)/src" srv.bare &&
+ git -C srv.bare config --local uploadpack.allowfilter 1 &&
+ git -C srv.bare config --local uploadpack.allowanysha1inwant 1
+'
+
+# do basic partial clone from "srv.bare"
+# confirm we are missing all of the known blobs.
+# confirm partial clone was registered in the local config.
+test_expect_success 'do partial clone 1' '
+ git clone --no-checkout --filter=blob:none "file://$(pwd)/srv.bare" pc1 &&
+
+ git -C pc1 rev-list --quiet --objects --missing=print HEAD >revs &&
+ awk -f print_1.awk revs |
+ sed "s/?//" |
+ sort >observed.oids &&
+
+ test_cmp expect_1.oids observed.oids &&
+ test "$(git -C pc1 config --local core.repositoryformatversion)" = "1" &&
+ test "$(git -C pc1 config --local remote.origin.promisor)" = "true" &&
+ test "$(git -C pc1 config --local remote.origin.partialclonefilter)" = "blob:none"
+'
+
+test_expect_success 'rev-list --missing=allow-promisor on partial clone' '
+ git -C pc1 rev-list --objects --missing=allow-promisor HEAD >actual &&
+ git -C pc1 rev-list --objects --missing=print HEAD >expect.raw &&
+ grep -v "^?" expect.raw >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'verify that .promisor file contains refs fetched' '
+ ls pc1/.git/objects/pack/pack-*.promisor >promisorlist &&
+ test_line_count = 1 promisorlist &&
+ git -C srv.bare rev-parse --verify HEAD >headhash &&
+ grep "$(cat headhash) HEAD" $(cat promisorlist) &&
+ grep "$(cat headhash) refs/heads/main" $(cat promisorlist)
+'
+
+# checkout main to force dynamic object fetch of blobs at HEAD.
+test_expect_success 'verify checkout with dynamic object fetch' '
+ git -C pc1 rev-list --quiet --objects --missing=print HEAD >observed &&
+ test_line_count = 4 observed &&
+ git -C pc1 checkout main &&
+ git -C pc1 rev-list --quiet --objects --missing=print HEAD >observed &&
+ test_line_count = 0 observed
+'
+
+# create new commits in "src" repo to establish a blame history on file.1.txt
+# and push to "srv.bare".
+test_expect_success 'push new commits to server' '
+ git -C src remote add srv "file://$(pwd)/srv.bare" &&
+ for x in a b c d e
+ do
+ echo "Mod file.1.txt $x" >>src/file.1.txt &&
+ git -C src add file.1.txt &&
+ git -C src commit -m "mod $x" || return 1
+ done &&
+ git -C src blame main -- file.1.txt >expect.blame &&
+ git -C src push -u srv main
+'
+
+# (partial) fetch in the partial clone repo from the promisor remote.
+# verify that fetch inherited the filter-spec from the config and DOES NOT
+# have the new blobs.
+test_expect_success 'partial fetch inherits filter settings' '
+ git -C pc1 fetch origin &&
+ git -C pc1 rev-list --quiet --objects --missing=print \
+ main..origin/main >observed &&
+ test_line_count = 5 observed
+'
+
+# force dynamic object fetch using diff.
+# we should only get 1 new blob (for the file in origin/main).
+test_expect_success 'verify diff causes dynamic object fetch' '
+ git -C pc1 diff main..origin/main -- file.1.txt &&
+ git -C pc1 rev-list --quiet --objects --missing=print \
+ main..origin/main >observed &&
+ test_line_count = 4 observed
+'
+
+# force full dynamic object fetch of the file's history using blame.
+# we should get the intermediate blobs for the file.
+test_expect_success 'verify blame causes dynamic object fetch' '
+ git -C pc1 blame origin/main -- file.1.txt >observed.blame &&
+ test_cmp expect.blame observed.blame &&
+ git -C pc1 rev-list --quiet --objects --missing=print \
+ main..origin/main >observed &&
+ test_line_count = 0 observed
+'
+
+# create new commits in "src" repo to establish a history on file.2.txt
+# and push to "srv.bare".
+test_expect_success 'push new commits to server for file.2.txt' '
+ for x in a b c d e f
+ do
+ echo "Mod file.2.txt $x" >>src/file.2.txt &&
+ git -C src add file.2.txt &&
+ git -C src commit -m "mod $x" || return 1
+ done &&
+ git -C src push -u srv main
+'
+
+# Do FULL fetch by disabling inherited filter-spec using --no-filter.
+# Verify we have all the new blobs.
+test_expect_success 'override inherited filter-spec using --no-filter' '
+ git -C pc1 fetch --no-filter origin &&
+ git -C pc1 rev-list --quiet --objects --missing=print \
+ main..origin/main >observed &&
+ test_line_count = 0 observed
+'
+
+# create new commits in "src" repo to establish a history on file.3.txt
+# and push to "srv.bare".
+test_expect_success 'push new commits to server for file.3.txt' '
+ for x in a b c d e f
+ do
+ echo "Mod file.3.txt $x" >>src/file.3.txt &&
+ git -C src add file.3.txt &&
+ git -C src commit -m "mod $x" || return 1
+ done &&
+ git -C src push -u srv main
+'
+
+# Do a partial fetch and then try to manually fetch the missing objects.
+# This can be used as the basis of a pre-command hook to bulk fetch objects
+# perhaps combined with a command in dry-run mode.
+test_expect_success 'manual prefetch of missing objects' '
+ git -C pc1 fetch --filter=blob:none origin &&
+
+ git -C pc1 rev-list --quiet --objects --missing=print \
+ main..origin/main >revs &&
+ awk -f print_1.awk revs |
+ sed "s/?//" |
+ sort >observed.oids &&
+
+ test_line_count = 6 observed.oids &&
+ git -C pc1 fetch-pack --stdin "file://$(pwd)/srv.bare" <observed.oids &&
+
+ git -C pc1 rev-list --quiet --objects --missing=print \
+ main..origin/main >revs &&
+ awk -f print_1.awk revs |
+ sed "s/?//" |
+ sort >observed.oids &&
+
+ test_line_count = 0 observed.oids
+'
+
+# create new commits in "src" repo to establish a history on file.4.txt
+# and push to "srv.bare".
+test_expect_success 'push new commits to server for file.4.txt' '
+ for x in a b c d e f
+ do
+ echo "Mod file.4.txt $x" >src/file.4.txt &&
+ if list_contains "a,b" "$x"; then
+ printf "%10000s" X >>src/file.4.txt
+ fi &&
+ if list_contains "c,d" "$x"; then
+ printf "%20000s" X >>src/file.4.txt
+ fi &&
+ git -C src add file.4.txt &&
+ git -C src commit -m "mod $x" || return 1
+ done &&
+ git -C src push -u srv main
+'
+
+# Do partial fetch to fetch smaller files; then verify that without --refetch
+# applying a new filter does not refetch missing large objects. Then use
+# --refetch to apply the new filter on existing commits. Test it under both
+# protocol v2 & v0.
+test_expect_success 'apply a different filter using --refetch' '
+ git -C pc1 fetch --filter=blob:limit=999 origin &&
+ git -C pc1 rev-list --quiet --objects --missing=print \
+ main..origin/main >observed &&
+ test_line_count = 4 observed &&
+
+ git -C pc1 fetch --filter=blob:limit=19999 --refetch origin &&
+ git -C pc1 rev-list --quiet --objects --missing=print \
+ main..origin/main >observed &&
+ test_line_count = 2 observed &&
+
+ git -c protocol.version=0 -C pc1 fetch --filter=blob:limit=29999 \
+ --refetch origin &&
+ git -C pc1 rev-list --quiet --objects --missing=print \
+ main..origin/main >observed &&
+ test_line_count = 0 observed
+'
+
+test_expect_success 'fetch --refetch works with a shallow clone' '
+ git clone --no-checkout --depth=1 --filter=blob:none "file://$(pwd)/srv.bare" pc1s &&
+ git -C pc1s rev-list --objects --missing=print HEAD >observed &&
+ test_line_count = 6 observed &&
+
+ GIT_TRACE=1 git -C pc1s fetch --filter=blob:limit=999 --refetch origin &&
+ git -C pc1s rev-list --objects --missing=print HEAD >observed &&
+ test_line_count = 6 observed
+'
+
+test_expect_success 'fetch --refetch triggers repacking' '
+ GIT_TRACE2_CONFIG_PARAMS=gc.autoPackLimit,maintenance.incremental-repack.auto &&
+ export GIT_TRACE2_CONFIG_PARAMS &&
+
+ GIT_TRACE2_EVENT="$PWD/trace1.event" \
+ git -C pc1 fetch --refetch origin &&
+ test_subcommand git maintenance run --auto --no-quiet <trace1.event &&
+ grep \"param\":\"gc.autopacklimit\",\"value\":\"1\" trace1.event &&
+ grep \"param\":\"maintenance.incremental-repack.auto\",\"value\":\"-1\" trace1.event &&
+
+ GIT_TRACE2_EVENT="$PWD/trace2.event" \
+ git -c protocol.version=0 \
+ -c gc.autoPackLimit=0 \
+ -c maintenance.incremental-repack.auto=1234 \
+ -C pc1 fetch --refetch origin &&
+ test_subcommand git maintenance run --auto --no-quiet <trace2.event &&
+ grep \"param\":\"gc.autopacklimit\",\"value\":\"0\" trace2.event &&
+ grep \"param\":\"maintenance.incremental-repack.auto\",\"value\":\"-1\" trace2.event &&
+
+ GIT_TRACE2_EVENT="$PWD/trace3.event" \
+ git -c protocol.version=0 \
+ -c gc.autoPackLimit=1234 \
+ -c maintenance.incremental-repack.auto=0 \
+ -C pc1 fetch --refetch origin &&
+ test_subcommand git maintenance run --auto --no-quiet <trace3.event &&
+ grep \"param\":\"gc.autopacklimit\",\"value\":\"1\" trace3.event &&
+ grep \"param\":\"maintenance.incremental-repack.auto\",\"value\":\"0\" trace3.event
+'
+
+test_expect_success 'partial clone with transfer.fsckobjects=1 works with submodules' '
+ test_create_repo submodule &&
+ test_commit -C submodule mycommit &&
+
+ test_create_repo src_with_sub &&
+ git -C src_with_sub config uploadpack.allowfilter 1 &&
+ git -C src_with_sub config uploadpack.allowanysha1inwant 1 &&
+
+ test_config_global protocol.file.allow always &&
+
+ git -C src_with_sub submodule add "file://$(pwd)/submodule" mysub &&
+ git -C src_with_sub commit -m "commit with submodule" &&
+
+ git -c transfer.fsckobjects=1 \
+ clone --filter="blob:none" "file://$(pwd)/src_with_sub" dst &&
+ test_when_finished rm -rf dst
+'
+
+test_expect_success 'lazily fetched .gitmodules works' '
+ git clone --filter="blob:none" --no-checkout "file://$(pwd)/src_with_sub" dst &&
+ git -C dst fetch &&
+ test_when_finished rm -rf dst
+'
+
+test_expect_success 'partial clone with transfer.fsckobjects=1 uses index-pack --fsck-objects' '
+ git init src &&
+ test_commit -C src x &&
+ test_config -C src uploadpack.allowfilter 1 &&
+ test_config -C src uploadpack.allowanysha1inwant 1 &&
+
+ GIT_TRACE="$(pwd)/trace" git -c transfer.fsckobjects=1 \
+ clone --filter="blob:none" "file://$(pwd)/src" dst &&
+ grep "git index-pack.*--fsck-objects" trace
+'
+
+test_expect_success 'use fsck before and after manually fetching a missing subtree' '
+ # push new commit so server has a subtree
+ mkdir src/dir &&
+ echo "in dir" >src/dir/file.txt &&
+ git -C src add dir/file.txt &&
+ git -C src commit -m "file in dir" &&
+ git -C src push -u srv main &&
+ SUBTREE=$(git -C src rev-parse HEAD:dir) &&
+
+ rm -rf dst &&
+ git clone --no-checkout --filter=tree:0 "file://$(pwd)/srv.bare" dst &&
+ git -C dst fsck &&
+
+ # Make sure we only have commits, and all trees and blobs are missing.
+ git -C dst rev-list --missing=allow-any --objects main \
+ >fetched_objects &&
+ awk -f print_1.awk fetched_objects |
+ xargs -n1 git -C dst cat-file -t >fetched_types &&
+
+ sort -u fetched_types >unique_types.observed &&
+ echo commit >unique_types.expected &&
+ test_cmp unique_types.expected unique_types.observed &&
+
+ # Auto-fetch a tree with cat-file.
+ git -C dst cat-file -p $SUBTREE >tree_contents &&
+ grep file.txt tree_contents &&
+
+ # fsck still works after an auto-fetch of a tree.
+ git -C dst fsck &&
+
+ # Auto-fetch all remaining trees and blobs with --missing=error
+ git -C dst rev-list --missing=error --objects main >fetched_objects &&
+ test_line_count = 88 fetched_objects &&
+
+ awk -f print_1.awk fetched_objects |
+ xargs -n1 git -C dst cat-file -t >fetched_types &&
+
+ sort -u fetched_types >unique_types.observed &&
+ test_write_lines blob commit tree >unique_types.expected &&
+ test_cmp unique_types.expected unique_types.observed
+'
+
+test_expect_success 'implicitly construct combine: filter with repeated flags' '
+ GIT_TRACE=$(pwd)/trace git clone --bare \
+ --filter=blob:none --filter=tree:1 \
+ "file://$(pwd)/srv.bare" pc2 &&
+ grep "trace:.* git pack-objects .*--filter=combine:blob:none+tree:1" \
+ trace &&
+ git -C pc2 rev-list --objects --missing=allow-any HEAD >objects &&
+
+ # We should have gotten some root trees.
+ grep " $" objects &&
+ # Should not have gotten any non-root trees or blobs.
+ ! grep " ." objects &&
+
+ xargs -n 1 git -C pc2 cat-file -t <objects >types &&
+ sort -u types >unique_types.actual &&
+ test_write_lines commit tree >unique_types.expected &&
+ test_cmp unique_types.expected unique_types.actual
+'
+
+test_expect_success 'upload-pack complains of bogus filter config' '
+ printf 0000 |
+ test_must_fail git \
+ -c uploadpackfilter.tree.maxdepth \
+ upload-pack . >/dev/null 2>err &&
+ test_grep "unable to parse.*tree.maxdepth" err
+'
+
+test_expect_success 'upload-pack fails banned object filters' '
+ test_config -C srv.bare uploadpackfilter.blob:none.allow false &&
+ test_must_fail ok=sigpipe git clone --no-checkout --filter=blob:none \
+ "file://$(pwd)/srv.bare" pc3 2>err &&
+ test_grep "filter '\''blob:none'\'' not supported" err
+'
+
+test_expect_success 'upload-pack fails banned combine object filters' '
+ test_config -C srv.bare uploadpackfilter.allow false &&
+ test_config -C srv.bare uploadpackfilter.combine.allow true &&
+ test_config -C srv.bare uploadpackfilter.tree.allow true &&
+ test_config -C srv.bare uploadpackfilter.blob:none.allow false &&
+ test_must_fail ok=sigpipe git clone --no-checkout --filter=tree:1 \
+ --filter=blob:none "file://$(pwd)/srv.bare" pc3 2>err &&
+ test_grep "filter '\''blob:none'\'' not supported" err
+'
+
+test_expect_success 'upload-pack fails banned object filters with fallback' '
+ test_config -C srv.bare uploadpackfilter.allow false &&
+ test_must_fail ok=sigpipe git clone --no-checkout --filter=blob:none \
+ "file://$(pwd)/srv.bare" pc3 2>err &&
+ test_grep "filter '\''blob:none'\'' not supported" err
+'
+
+test_expect_success 'upload-pack limits tree depth filters' '
+ test_config -C srv.bare uploadpackfilter.allow false &&
+ test_config -C srv.bare uploadpackfilter.tree.allow true &&
+ test_config -C srv.bare uploadpackfilter.tree.maxDepth 0 &&
+ test_must_fail ok=sigpipe git clone --no-checkout --filter=tree:1 \
+ "file://$(pwd)/srv.bare" pc3 2>err &&
+ test_grep "tree filter allows max depth 0, but got 1" err &&
+
+ git clone --no-checkout --filter=tree:0 "file://$(pwd)/srv.bare" pc4 &&
+
+ test_config -C srv.bare uploadpackfilter.tree.maxDepth 5 &&
+ git clone --no-checkout --filter=tree:5 "file://$(pwd)/srv.bare" pc5 &&
+ test_must_fail ok=sigpipe git clone --no-checkout --filter=tree:6 \
+ "file://$(pwd)/srv.bare" pc6 2>err &&
+ test_grep "tree filter allows max depth 5, but got 6" err
+'
+
+test_expect_success 'partial clone fetches blobs pointed to by refs even if normally filtered out' '
+ rm -rf src dst &&
+ git init src &&
+ test_commit -C src x &&
+ test_config -C src uploadpack.allowfilter 1 &&
+ test_config -C src uploadpack.allowanysha1inwant 1 &&
+
+ # Create a tag pointing to a blob.
+ BLOB=$(echo blob-contents | git -C src hash-object --stdin -w) &&
+ git -C src tag myblob "$BLOB" &&
+
+ git clone --filter="blob:none" "file://$(pwd)/src" dst 2>err &&
+ ! grep "does not point to a valid object" err &&
+ git -C dst fsck
+'
+
+test_expect_success 'fetch what is specified on CLI even if already promised' '
+ rm -rf src dst.git &&
+ git init src &&
+ test_commit -C src foo &&
+ test_config -C src uploadpack.allowfilter 1 &&
+ test_config -C src uploadpack.allowanysha1inwant 1 &&
+
+ git hash-object --stdin <src/foo.t >blob &&
+
+ git clone --bare --filter=blob:none "file://$(pwd)/src" dst.git &&
+ git -C dst.git rev-list --objects --quiet --missing=print HEAD >missing_before &&
+ grep "?$(cat blob)" missing_before &&
+ git -C dst.git fetch origin $(cat blob) &&
+ git -C dst.git rev-list --objects --quiet --missing=print HEAD >missing_after &&
+ ! grep "?$(cat blob)" missing_after
+'
+
+test_expect_success 'setup src repo for sparse filter' '
+ git init sparse-src &&
+ git -C sparse-src config --local uploadpack.allowfilter 1 &&
+ git -C sparse-src config --local uploadpack.allowanysha1inwant 1 &&
+ test_commit -C sparse-src one &&
+ test_commit -C sparse-src two &&
+ echo /one.t >sparse-src/only-one &&
+ git -C sparse-src add . &&
+ git -C sparse-src commit -m "add sparse checkout files"
+'
+
+test_expect_success 'partial clone with sparse filter succeeds' '
+ rm -rf dst.git &&
+ git clone --no-local --bare \
+ --filter=sparse:oid=main:only-one \
+ sparse-src dst.git &&
+ (
+ cd dst.git &&
+ git rev-list --objects --missing=print HEAD >out &&
+ grep "^$(git rev-parse HEAD:one.t)" out &&
+ grep "^?$(git rev-parse HEAD:two.t)" out
+ )
+'
+
+test_expect_success 'partial clone with unresolvable sparse filter fails cleanly' '
+ rm -rf dst.git &&
+ test_must_fail git clone --no-local --bare \
+ --filter=sparse:oid=main:no-such-name \
+ sparse-src dst.git 2>err &&
+ test_grep "unable to access sparse blob in .main:no-such-name" err &&
+ test_must_fail git clone --no-local --bare \
+ --filter=sparse:oid=main \
+ sparse-src dst.git 2>err &&
+ test_grep "unable to parse sparse filter data in" err
+'
+
+setup_triangle () {
+ rm -rf big-blob.txt server client promisor-remote &&
+
+ printf "line %d\n" $(test_seq 1 100) >big-blob.txt &&
+
+ # Create a server with 2 commits: a commit with a big tree and a child
+ # commit with an incremental change. Also, create a partial clone
+ # client that only contains the first commit.
+ git init server &&
+ git -C server config --local uploadpack.allowfilter 1 &&
+ for i in $(test_seq 1 100)
+ do
+ echo "make the tree big" >server/file$i &&
+ git -C server add file$i || return 1
+ done &&
+ git -C server commit -m "initial" &&
+ git clone --bare --filter=tree:0 "file://$(pwd)/server" client &&
+ echo another line >>server/file1 &&
+ git -C server commit -am "incremental change" &&
+
+ # Create a promisor remote that only contains the tree and blob from
+ # the first commit.
+ git init promisor-remote &&
+ git -C server config --local uploadpack.allowanysha1inwant 1 &&
+ TREE_HASH=$(git -C server rev-parse HEAD~1^{tree}) &&
+ git -C promisor-remote fetch --keep "file://$(pwd)/server" "$TREE_HASH" &&
+ git -C promisor-remote count-objects -v >object-count &&
+ test_grep "count: 0" object-count &&
+ test_grep "in-pack: 2" object-count &&
+
+ # Set it as the promisor remote of client. Thus, whenever
+ # the client lazy fetches, the lazy fetch will succeed only if it is
+ # for this tree or blob.
+ test_commit -C promisor-remote one && # so that ref advertisement is not empty
+ git -C promisor-remote config --local uploadpack.allowanysha1inwant 1 &&
+ git -C client remote set-url origin "file://$(pwd)/promisor-remote"
+}
+
+# NEEDSWORK: The tests beginning with "fetch lazy-fetches" below only
+# test that "fetch" avoid fetching trees and blobs, but not commits or
+# tags. Revisit this if Git is ever taught to support partial clones
+# with commits and/or tags filtered out.
+
+test_expect_success 'fetch lazy-fetches only to resolve deltas' '
+ setup_triangle &&
+
+ # Exercise to make sure it works. Git will not fetch anything from the
+ # promisor remote other than for the big tree (because it needs to
+ # resolve the delta).
+ GIT_TRACE_PACKET="$(pwd)/trace" git -C client \
+ fetch "file://$(pwd)/server" main &&
+
+ # Verify the assumption that the client needed to fetch the delta base
+ # to resolve the delta.
+ git -C server rev-parse HEAD~1^{tree} >hash &&
+ grep "want $(cat hash)" trace
+'
+
+test_expect_success 'fetch lazy-fetches only to resolve deltas, protocol v2' '
+ setup_triangle &&
+
+ git -C server config --local protocol.version 2 &&
+ git -C client config --local protocol.version 2 &&
+ git -C promisor-remote config --local protocol.version 2 &&
+
+ # Exercise to make sure it works. Git will not fetch anything from the
+ # promisor remote other than for the big blob (because it needs to
+ # resolve the delta).
+ GIT_TRACE_PACKET="$(pwd)/trace" git -C client \
+ fetch "file://$(pwd)/server" main &&
+
+ # Verify that protocol version 2 was used.
+ grep "fetch< version 2" trace &&
+
+ # Verify the assumption that the client needed to fetch the delta base
+ # to resolve the delta.
+ git -C server rev-parse HEAD~1^{tree} >hash &&
+ grep "want $(cat hash)" trace
+'
+
+test_expect_success 'fetch does not lazy-fetch missing targets of its refs' '
+ rm -rf server client trace &&
+
+ test_create_repo server &&
+ test_config -C server uploadpack.allowfilter 1 &&
+ test_config -C server uploadpack.allowanysha1inwant 1 &&
+ test_commit -C server foo &&
+
+ git clone --filter=blob:none "file://$(pwd)/server" client &&
+ # Make all refs point to nothing by deleting all objects.
+ rm client/.git/objects/pack/* &&
+
+ test_commit -C server bar &&
+ GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch \
+ --no-tags --recurse-submodules=no \
+ origin refs/tags/bar &&
+ FOO_HASH=$(git -C server rev-parse foo) &&
+ ! grep "want $FOO_HASH" trace
+'
+
+# The following two tests must be in this order. It is important that
+# the srv.bare repository did not have tags during clone, but has tags
+# in the fetch.
+
+test_expect_success 'verify fetch succeeds when asking for new tags' '
+ git clone --filter=blob:none "file://$(pwd)/srv.bare" tag-test &&
+ for i in I J K
+ do
+ test_commit -C src $i &&
+ git -C src branch $i || return 1
+ done &&
+ git -C srv.bare fetch --tags origin +refs/heads/*:refs/heads/* &&
+ git -C tag-test -c protocol.version=2 fetch --tags origin
+'
+
+test_expect_success 'verify fetch downloads only one pack when updating refs' '
+ git clone --filter=blob:none "file://$(pwd)/srv.bare" pack-test &&
+ ls pack-test/.git/objects/pack/*pack >pack-list &&
+ test_line_count = 2 pack-list &&
+ for i in A B C
+ do
+ test_commit -C src $i &&
+ git -C src branch $i || return 1
+ done &&
+ git -C srv.bare fetch origin +refs/heads/*:refs/heads/* &&
+ git -C pack-test fetch origin &&
+ ls pack-test/.git/objects/pack/*pack >pack-list &&
+ test_line_count = 3 pack-list
+'
+
+test_expect_success 'single-branch tag following respects partial clone' '
+ git clone --single-branch -b B --filter=blob:none \
+ "file://$(pwd)/srv.bare" single &&
+ git -C single rev-parse --verify refs/tags/B &&
+ git -C single rev-parse --verify refs/tags/A &&
+ test_must_fail git -C single rev-parse --verify refs/tags/C
+'
+
+test_expect_success 'fetch from a partial clone, protocol v0' '
+ rm -rf server client trace &&
+
+ # Pretend that the server is a partial clone
+ git init server &&
+ git -C server remote add a_remote "file://$(pwd)/" &&
+ test_config -C server core.repositoryformatversion 1 &&
+ test_config -C server extensions.partialclone a_remote &&
+ test_config -C server protocol.version 0 &&
+ test_commit -C server foo &&
+
+ # Fetch from the server
+ git init client &&
+ test_config -C client protocol.version 0 &&
+ test_commit -C client bar &&
+ GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch "file://$(pwd)/server" &&
+ ! grep "version 2" trace
+'
+
+test_expect_success 'fetch from a partial clone, protocol v2' '
+ rm -rf server client trace &&
+
+ # Pretend that the server is a partial clone
+ git init server &&
+ git -C server remote add a_remote "file://$(pwd)/" &&
+ test_config -C server core.repositoryformatversion 1 &&
+ test_config -C server extensions.partialclone a_remote &&
+ test_config -C server protocol.version 2 &&
+ test_commit -C server foo &&
+
+ # Fetch from the server
+ git init client &&
+ test_config -C client protocol.version 2 &&
+ test_commit -C client bar &&
+ GIT_TRACE_PACKET="$(pwd)/trace" git -C client fetch "file://$(pwd)/server" &&
+ grep "version 2" trace
+'
+
+test_expect_success 'repack does not loosen promisor objects' '
+ rm -rf client trace &&
+ git clone --bare --filter=blob:none "file://$(pwd)/srv.bare" client &&
+ test_when_finished "rm -rf client trace" &&
+ GIT_TRACE2_PERF="$(pwd)/trace" git -C client repack -A -d &&
+ grep "loosen_unused_packed_objects/loosened:0" trace
+'
+
+test_expect_success 'lazy-fetch in submodule succeeds' '
+ # setup
+ test_config_global protocol.file.allow always &&
+
+ test_when_finished "rm -rf src-sub" &&
+ git init src-sub &&
+ git -C src-sub config uploadpack.allowfilter 1 &&
+ git -C src-sub config uploadpack.allowanysha1inwant 1 &&
+
+ # This blob must be missing in the subsequent commit.
+ echo foo >src-sub/file &&
+ git -C src-sub add file &&
+ git -C src-sub commit -m "submodule one" &&
+ SUB_ONE=$(git -C src-sub rev-parse HEAD) &&
+
+ echo bar >src-sub/file &&
+ git -C src-sub add file &&
+ git -C src-sub commit -m "submodule two" &&
+ SUB_TWO=$(git -C src-sub rev-parse HEAD) &&
+
+ test_when_finished "rm -rf src-super" &&
+ git init src-super &&
+ git -C src-super config uploadpack.allowfilter 1 &&
+ git -C src-super config uploadpack.allowanysha1inwant 1 &&
+ git -C src-super submodule add ../src-sub src-sub &&
+
+ git -C src-super/src-sub checkout $SUB_ONE &&
+ git -C src-super add src-sub &&
+ git -C src-super commit -m "superproject one" &&
+
+ git -C src-super/src-sub checkout $SUB_TWO &&
+ git -C src-super add src-sub &&
+ git -C src-super commit -m "superproject two" &&
+
+ # the fetch
+ test_when_finished "rm -rf client" &&
+ git clone --filter=blob:none --also-filter-submodules \
+ --recurse-submodules "file://$(pwd)/src-super" client &&
+
+ # Trigger lazy-fetch from the superproject
+ git -C client restore --recurse-submodules --source=HEAD^ :/
+'
+
+. "$TEST_DIRECTORY"/lib-httpd.sh
+start_httpd
+
+# Converts bytes into their hexadecimal representation. For example,
+# "printf 'ab\r\n' | hex_unpack" results in '61620d0a'.
+hex_unpack () {
+ perl -e '$/ = undef; $input = <>; print unpack("H2" x length($input), $input)'
+}
+
+# Inserts $1 at the start of the string and every 2 characters thereafter.
+intersperse () {
+ sed 's/\(..\)/'$1'\1/g'
+}
+
+# Create a one-time-perl command to replace the existing packfile with $1.
+replace_packfile () {
+ # The protocol requires that the packfile be sent in sideband 1, hence
+ # the extra \x01 byte at the beginning.
+ cp $1 "$HTTPD_ROOT_PATH/one-time-pack" &&
+ echo 'if (/packfile/) {
+ print;
+ my $length = -s "one-time-pack";
+ printf "%04x\x01", $length + 5;
+ print `cat one-time-pack` . "0000";
+ last
+ }' >"$HTTPD_ROOT_PATH/one-time-perl"
+}
+
+test_expect_success 'upon cloning, check that all refs point to objects' '
+ SERVER="$HTTPD_DOCUMENT_ROOT_PATH/server" &&
+ rm -rf "$SERVER" repo &&
+ test_create_repo "$SERVER" &&
+ test_commit -C "$SERVER" foo &&
+ test_config -C "$SERVER" uploadpack.allowfilter 1 &&
+ test_config -C "$SERVER" uploadpack.allowanysha1inwant 1 &&
+
+ # Create a tag pointing to a blob.
+ BLOB=$(echo blob-contents | git -C "$SERVER" hash-object --stdin -w) &&
+ git -C "$SERVER" tag myblob "$BLOB" &&
+
+ # Craft a packfile not including that blob.
+ git -C "$SERVER" rev-parse HEAD |
+ git -C "$SERVER" pack-objects --stdout >incomplete.pack &&
+
+ # Replace the existing packfile with the crafted one. The protocol
+ # requires that the packfile be sent in sideband 1, hence the extra
+ # \x01 byte at the beginning.
+ replace_packfile incomplete.pack &&
+
+ # Use protocol v2 because the perl command looks for the "packfile"
+ # section header.
+ test_config -C "$SERVER" protocol.version 2 &&
+ test_must_fail git -c protocol.version=2 clone \
+ --filter=blob:none $HTTPD_URL/one_time_perl/server repo 2>err &&
+
+ test_grep "did not send all necessary objects" err &&
+
+ # Ensure that the one-time-perl script was used.
+ ! test -e "$HTTPD_ROOT_PATH/one-time-perl"
+'
+
+test_expect_success 'when partial cloning, tolerate server not sending target of tag' '
+ SERVER="$HTTPD_DOCUMENT_ROOT_PATH/server" &&
+ rm -rf "$SERVER" repo &&
+ test_create_repo "$SERVER" &&
+ test_commit -C "$SERVER" foo &&
+ test_config -C "$SERVER" uploadpack.allowfilter 1 &&
+ test_config -C "$SERVER" uploadpack.allowanysha1inwant 1 &&
+
+ # Create an annotated tag pointing to a blob.
+ BLOB=$(echo blob-contents | git -C "$SERVER" hash-object --stdin -w) &&
+ git -C "$SERVER" tag -m message -a myblob "$BLOB" &&
+
+ # Craft a packfile including the tag, but not the blob it points to.
+ # Also, omit objects referenced from HEAD in order to force a second
+ # fetch (to fetch missing objects) upon the automatic checkout that
+ # happens after a clone.
+ printf "%s\n%s\n--not\n%s\n%s\n" \
+ $(git -C "$SERVER" rev-parse HEAD) \
+ $(git -C "$SERVER" rev-parse myblob) \
+ $(git -C "$SERVER" rev-parse HEAD^{tree}) \
+ $(git -C "$SERVER" rev-parse myblob^{blob}) |
+ git -C "$SERVER" pack-objects --thin --stdout >incomplete.pack &&
+
+ # Replace the existing packfile with the crafted one. The protocol
+ # requires that the packfile be sent in sideband 1, hence the extra
+ # \x01 byte at the beginning.
+ replace_packfile incomplete.pack &&
+
+ # Use protocol v2 because the perl command looks for the "packfile"
+ # section header.
+ test_config -C "$SERVER" protocol.version 2 &&
+
+ # Exercise to make sure it works.
+ git -c protocol.version=2 clone \
+ --filter=blob:none $HTTPD_URL/one_time_perl/server repo 2> err &&
+ ! grep "missing object referenced by" err &&
+
+ # Ensure that the one-time-perl script was used.
+ ! test -e "$HTTPD_ROOT_PATH/one-time-perl"
+'
+
+test_expect_success 'tolerate server sending REF_DELTA against missing promisor objects' '
+ SERVER="$HTTPD_DOCUMENT_ROOT_PATH/server" &&
+ rm -rf "$SERVER" repo &&
+ test_create_repo "$SERVER" &&
+ test_config -C "$SERVER" uploadpack.allowfilter 1 &&
+ test_config -C "$SERVER" uploadpack.allowanysha1inwant 1 &&
+
+ # Create a commit with 2 blobs to be used as delta bases.
+ for i in $(test_seq 10)
+ do
+ echo "this is a line" >>"$SERVER/foo.txt" &&
+ echo "this is another line" >>"$SERVER/have.txt" || return 1
+ done &&
+ git -C "$SERVER" add foo.txt have.txt &&
+ git -C "$SERVER" commit -m bar &&
+ git -C "$SERVER" rev-parse HEAD:foo.txt >deltabase_missing &&
+ git -C "$SERVER" rev-parse HEAD:have.txt >deltabase_have &&
+
+ # Clone. The client has deltabase_have but not deltabase_missing.
+ git -c protocol.version=2 clone --no-checkout \
+ --filter=blob:none $HTTPD_URL/one_time_perl/server repo &&
+ git -C repo hash-object -w -- "$SERVER/have.txt" &&
+
+ # Sanity check to ensure that the client does not have
+ # deltabase_missing.
+ git -C repo rev-list --objects --ignore-missing \
+ -- $(cat deltabase_missing) >objlist &&
+ test_line_count = 0 objlist &&
+
+ # Another commit. This commit will be fetched by the client.
+ echo "abcdefghijklmnopqrstuvwxyz" >>"$SERVER/foo.txt" &&
+ echo "abcdefghijklmnopqrstuvwxyz" >>"$SERVER/have.txt" &&
+ git -C "$SERVER" add foo.txt have.txt &&
+ git -C "$SERVER" commit -m baz &&
+
+ # Pack a thin pack containing, among other things, HEAD:foo.txt
+ # delta-ed against HEAD^:foo.txt and HEAD:have.txt delta-ed against
+ # HEAD^:have.txt.
+ printf "%s\n--not\n%s\n" \
+ $(git -C "$SERVER" rev-parse HEAD) \
+ $(git -C "$SERVER" rev-parse HEAD^) |
+ git -C "$SERVER" pack-objects --thin --stdout >thin.pack &&
+
+ # Ensure that the pack contains one delta against HEAD^:foo.txt. Since
+ # the delta contains at least 26 novel characters, the size cannot be
+ # contained in 4 bits, so the object header will take up 2 bytes. The
+ # most significant nybble of the first byte is 0b1111 (0b1 to indicate
+ # that the header continues, and 0b111 to indicate REF_DELTA), followed
+ # by any 3 nybbles, then the OID of the delta base.
+ printf "f.,..%s" $(intersperse "," <deltabase_missing) >want &&
+ hex_unpack <thin.pack | intersperse "," >have &&
+ grep $(cat want) have &&
+
+ # Ensure that the pack contains one delta against HEAD^:have.txt,
+ # similar to the above.
+ printf "f.,..%s" $(intersperse "," <deltabase_have) >want &&
+ hex_unpack <thin.pack | intersperse "," >have &&
+ grep $(cat want) have &&
+
+ replace_packfile thin.pack &&
+
+ # Use protocol v2 because the perl command looks for the "packfile"
+ # section header.
+ test_config -C "$SERVER" protocol.version 2 &&
+
+ # Fetch the thin pack and ensure that index-pack is able to handle the
+ # REF_DELTA object with a missing promisor delta base.
+ GIT_TRACE_PACKET="$(pwd)/trace" git -C repo -c protocol.version=2 fetch &&
+
+ # Ensure that the missing delta base was directly fetched, but not the
+ # one that the client has.
+ grep "want $(cat deltabase_missing)" trace &&
+ ! grep "want $(cat deltabase_have)" trace &&
+
+ # Ensure that the one-time-perl script was used.
+ ! test -e "$HTTPD_ROOT_PATH/one-time-perl"
+'
+
+# DO NOT add non-httpd-specific tests here, because the last part of this
+# test script is only executed when httpd is available and enabled.
+
+test_done
diff --git a/t/t5617-clone-submodules-remote.sh b/t/t5617-clone-submodules-remote.sh
new file mode 100755
index 0000000..5a4d793
--- /dev/null
+++ b/t/t5617-clone-submodules-remote.sh
@@ -0,0 +1,111 @@
+#!/bin/sh
+
+test_description='Test cloning repos with submodules using remote-tracking branches'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+pwd=$(pwd)
+
+test_expect_success 'setup' '
+ git config --global protocol.file.allow always &&
+ git checkout -b main &&
+ test_commit commit1 &&
+ mkdir sub &&
+ (
+ cd sub &&
+ git init &&
+ test_commit subcommit1 &&
+ git tag sub_when_added_to_super &&
+ git branch other
+ ) &&
+ git submodule add "file://$pwd/sub" sub &&
+ git commit -m "add submodule" &&
+ (
+ cd sub &&
+ test_commit subcommit2
+ )
+'
+
+# bare clone giving "srv.bare" for use as our server.
+test_expect_success 'setup bare clone for server' '
+ git clone --bare "file://$(pwd)/." srv.bare &&
+ git -C srv.bare config --local uploadpack.allowfilter 1 &&
+ git -C srv.bare config --local uploadpack.allowanysha1inwant 1
+'
+
+test_expect_success 'clone with --no-remote-submodules' '
+ test_when_finished "rm -rf super_clone" &&
+ git clone --recurse-submodules --no-remote-submodules "file://$pwd/." super_clone &&
+ (
+ cd super_clone/sub &&
+ git diff --exit-code sub_when_added_to_super
+ )
+'
+
+test_expect_success 'clone with --remote-submodules' '
+ test_when_finished "rm -rf super_clone" &&
+ git clone --recurse-submodules --remote-submodules "file://$pwd/." super_clone &&
+ (
+ cd super_clone/sub &&
+ git diff --exit-code remotes/origin/main
+ )
+'
+
+test_expect_success 'check the default is --no-remote-submodules' '
+ test_when_finished "rm -rf super_clone" &&
+ git clone --recurse-submodules "file://$pwd/." super_clone &&
+ (
+ cd super_clone/sub &&
+ git diff --exit-code sub_when_added_to_super
+ )
+'
+
+test_expect_success 'clone with --single-branch' '
+ test_when_finished "rm -rf super_clone" &&
+ git clone --recurse-submodules --single-branch "file://$pwd/." super_clone &&
+ (
+ cd super_clone/sub &&
+ git rev-parse --verify origin/main &&
+ test_must_fail git rev-parse --verify origin/other
+ )
+'
+
+# do basic partial clone from "srv.bare"
+# confirm partial clone was registered in the local config for super and sub.
+test_expect_success 'clone with --filter' '
+ git clone --recurse-submodules \
+ --filter blob:none --also-filter-submodules \
+ "file://$pwd/srv.bare" super_clone &&
+ test_cmp_config -C super_clone true remote.origin.promisor &&
+ test_cmp_config -C super_clone blob:none remote.origin.partialclonefilter &&
+ test_cmp_config -C super_clone/sub true remote.origin.promisor &&
+ test_cmp_config -C super_clone/sub blob:none remote.origin.partialclonefilter
+'
+
+# check that clone.filterSubmodules works (--also-filter-submodules can be
+# omitted)
+test_expect_success 'filters applied with clone.filterSubmodules' '
+ test_config_global clone.filterSubmodules true &&
+ git clone --recurse-submodules --filter blob:none \
+ "file://$pwd/srv.bare" super_clone2 &&
+ test_cmp_config -C super_clone2 true remote.origin.promisor &&
+ test_cmp_config -C super_clone2 blob:none remote.origin.partialclonefilter &&
+ test_cmp_config -C super_clone2/sub true remote.origin.promisor &&
+ test_cmp_config -C super_clone2/sub blob:none remote.origin.partialclonefilter
+'
+
+test_expect_success '--no-also-filter-submodules overrides clone.filterSubmodules=true' '
+ test_config_global clone.filterSubmodules true &&
+ git clone --recurse-submodules --filter blob:none \
+ --no-also-filter-submodules \
+ "file://$pwd/srv.bare" super_clone3 &&
+ test_cmp_config -C super_clone3 true remote.origin.promisor &&
+ test_cmp_config -C super_clone3 blob:none remote.origin.partialclonefilter &&
+ test_cmp_config -C super_clone3/sub false --default false remote.origin.promisor
+'
+
+test_done
diff --git a/t/t5618-alternate-refs.sh b/t/t5618-alternate-refs.sh
new file mode 100755
index 0000000..f905db0
--- /dev/null
+++ b/t/t5618-alternate-refs.sh
@@ -0,0 +1,62 @@
+#!/bin/sh
+
+test_description='test handling of --alternate-refs traversal'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# Avoid test_commit because we want a specific and known set of refs:
+#
+# base -- one
+# \ \
+# two -- merged
+#
+# where "one" and "two" are on separate refs, and "merged" is available only in
+# the dependent child repository.
+test_expect_success 'set up local refs' '
+ git checkout -b one &&
+ test_tick &&
+ git commit --allow-empty -m base &&
+ test_tick &&
+ git commit --allow-empty -m one &&
+ git checkout -b two HEAD^ &&
+ test_tick &&
+ git commit --allow-empty -m two
+'
+
+# We'll enter the child repository after it's set up since that's where
+# all of the subsequent tests will want to run (and it's easy to forget a
+# "-C child" and get nonsense results).
+test_expect_success 'set up shared clone' '
+ git clone -s . child &&
+ cd child &&
+ git merge origin/one
+'
+
+test_expect_success 'rev-list --alternate-refs' '
+ git rev-list --remotes=origin >expect &&
+ git rev-list --alternate-refs >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rev-list --not --alternate-refs' '
+ git rev-parse HEAD >expect &&
+ git rev-list HEAD --not --alternate-refs >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'limiting with alternateRefsPrefixes' '
+ test_config core.alternateRefsPrefixes refs/heads/one &&
+ git rev-list origin/one >expect &&
+ git rev-list --alternate-refs >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log --source shows .alternate marker' '
+ git log --oneline --source --remotes=origin >expect.orig &&
+ sed "s/origin.* /.alternate /" <expect.orig >expect &&
+ git log --oneline --source --alternate-refs >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t5619-clone-local-ambiguous-transport.sh b/t/t5619-clone-local-ambiguous-transport.sh
new file mode 100755
index 0000000..cce62bf
--- /dev/null
+++ b/t/t5619-clone-local-ambiguous-transport.sh
@@ -0,0 +1,70 @@
+#!/bin/sh
+
+test_description='test local clone with ambiguous transport'
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY/lib-httpd.sh"
+
+if ! test_have_prereq SYMLINKS
+then
+ skip_all='skipping test, symlink support unavailable'
+ test_done
+fi
+
+start_httpd
+
+REPO="$HTTPD_DOCUMENT_ROOT_PATH/sub.git"
+URI="$HTTPD_URL/dumb/sub.git"
+
+test_expect_success 'setup' '
+ mkdir -p sensitive &&
+ echo "secret" >sensitive/secret &&
+
+ git init --bare "$REPO" &&
+ test_commit_bulk -C "$REPO" --ref=main 1 &&
+
+ git -C "$REPO" update-ref HEAD main &&
+ git -C "$REPO" update-server-info &&
+
+ git init malicious &&
+ (
+ cd malicious &&
+
+ git submodule add "$URI" &&
+
+ mkdir -p repo/refs &&
+ touch repo/refs/.gitkeep &&
+ printf "ref: refs/heads/a" >repo/HEAD &&
+ ln -s "$(cd .. && pwd)/sensitive" repo/objects &&
+
+ mkdir -p "$HTTPD_URL/dumb" &&
+ ln -s "../../../.git/modules/sub/../../../repo/" "$URI" &&
+
+ git add . &&
+ git commit -m "initial commit"
+ ) &&
+
+ # Delete all of the references in our malicious submodule to
+ # avoid the client attempting to checkout any objects (which
+ # will be missing, and thus will cause the clone to fail before
+ # we can trigger the exploit).
+ git -C "$REPO" for-each-ref --format="delete %(refname)" >in &&
+ git -C "$REPO" update-ref --stdin <in &&
+ git -C "$REPO" update-server-info
+'
+
+test_expect_success 'ambiguous transport does not lead to arbitrary file-inclusion' '
+ git clone malicious clone &&
+ test_must_fail git -C clone submodule update --init 2>err &&
+
+ test_path_is_missing clone/.git/modules/sub/objects/secret &&
+ # We would actually expect "transport .file. not allowed" here,
+ # but due to quirks of the URL detection in Git, we mis-parse
+ # the absolute path as a bogus URL and die before that step.
+ #
+ # This works for now, and if we ever fix the URL detection, it
+ # is OK to change this to detect the transport error.
+ grep "protocol .* is not supported" err
+'
+
+test_done
diff --git a/t/t5700-protocol-v1.sh b/t/t5700-protocol-v1.sh
new file mode 100755
index 0000000..a73b4d4
--- /dev/null
+++ b/t/t5700-protocol-v1.sh
@@ -0,0 +1,344 @@
+#!/bin/sh
+
+test_description='test git wire-protocol transition'
+
+TEST_NO_CREATE_REPO=1
+
+# This is a protocol-specific test.
+GIT_TEST_PROTOCOL_VERSION=0
+export GIT_TEST_PROTOCOL_VERSION
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+# Test protocol v1 with 'git://' transport
+#
+. "$TEST_DIRECTORY"/lib-git-daemon.sh
+start_git_daemon --export-all --enable=receive-pack
+daemon_parent=$GIT_DAEMON_DOCUMENT_ROOT_PATH/parent
+
+test_expect_success 'create repo to be served by git-daemon' '
+ git init "$daemon_parent" &&
+ test_commit -C "$daemon_parent" one
+'
+
+test_expect_success 'clone with git:// using protocol v1' '
+ GIT_TRACE_PACKET=1 git -c protocol.version=1 \
+ clone "$GIT_DAEMON_URL/parent" daemon_child 2>log &&
+
+ git -C daemon_child log -1 --format=%s >actual &&
+ git -C "$daemon_parent" log -1 --format=%s >expect &&
+ test_cmp expect actual &&
+
+ # Client requested to use protocol v1
+ grep "clone> .*\\\0\\\0version=1\\\0$" log &&
+ # Server responded using protocol v1
+ grep "clone< version 1" log
+'
+
+test_expect_success 'fetch with git:// using protocol v1' '
+ test_commit -C "$daemon_parent" two &&
+
+ GIT_TRACE_PACKET=1 git -C daemon_child -c protocol.version=1 \
+ fetch 2>log &&
+
+ git -C daemon_child log -1 --format=%s origin/main >actual &&
+ git -C "$daemon_parent" log -1 --format=%s >expect &&
+ test_cmp expect actual &&
+
+ # Client requested to use protocol v1
+ grep "fetch> .*\\\0\\\0version=1\\\0$" log &&
+ # Server responded using protocol v1
+ grep "fetch< version 1" log
+'
+
+test_expect_success 'pull with git:// using protocol v1' '
+ GIT_TRACE_PACKET=1 git -C daemon_child -c protocol.version=1 \
+ pull 2>log &&
+
+ git -C daemon_child log -1 --format=%s >actual &&
+ git -C "$daemon_parent" log -1 --format=%s >expect &&
+ test_cmp expect actual &&
+
+ # Client requested to use protocol v1
+ grep "fetch> .*\\\0\\\0version=1\\\0$" log &&
+ # Server responded using protocol v1
+ grep "fetch< version 1" log
+'
+
+test_expect_success 'push with git:// using protocol v1' '
+ test_commit -C daemon_child three &&
+
+ # Push to another branch, as the target repository has the
+ # main branch checked out and we cannot push into it.
+ GIT_TRACE_PACKET=1 git -C daemon_child -c protocol.version=1 \
+ push origin HEAD:client_branch 2>log &&
+
+ git -C daemon_child log -1 --format=%s >actual &&
+ git -C "$daemon_parent" log -1 --format=%s client_branch >expect &&
+ test_cmp expect actual &&
+
+ # Client requested to use protocol v1
+ grep "push> .*\\\0\\\0version=1\\\0$" log &&
+ # Server responded using protocol v1
+ grep "push< version 1" log
+'
+
+stop_git_daemon
+
+# Test protocol v1 with 'file://' transport
+#
+test_expect_success 'create repo to be served by file:// transport' '
+ git init file_parent &&
+ test_commit -C file_parent one
+'
+
+test_expect_success 'clone with file:// using protocol v1' '
+ GIT_TRACE_PACKET=1 git -c protocol.version=1 \
+ clone "file://$(pwd)/file_parent" file_child 2>log &&
+
+ git -C file_child log -1 --format=%s >actual &&
+ git -C file_parent log -1 --format=%s >expect &&
+ test_cmp expect actual &&
+
+ # Server responded using protocol v1
+ grep "clone< version 1" log
+'
+
+test_expect_success 'fetch with file:// using protocol v1' '
+ test_commit -C file_parent two &&
+
+ GIT_TRACE_PACKET=1 git -C file_child -c protocol.version=1 \
+ fetch 2>log &&
+
+ git -C file_child log -1 --format=%s origin/main >actual &&
+ git -C file_parent log -1 --format=%s >expect &&
+ test_cmp expect actual &&
+
+ # Server responded using protocol v1
+ grep "fetch< version 1" log
+'
+
+test_expect_success 'pull with file:// using protocol v1' '
+ GIT_TRACE_PACKET=1 git -C file_child -c protocol.version=1 \
+ pull 2>log &&
+
+ git -C file_child log -1 --format=%s >actual &&
+ git -C file_parent log -1 --format=%s >expect &&
+ test_cmp expect actual &&
+
+ # Server responded using protocol v1
+ grep "fetch< version 1" log
+'
+
+test_expect_success 'push with file:// using protocol v1' '
+ test_commit -C file_child three &&
+
+ # Push to another branch, as the target repository has the
+ # main branch checked out and we cannot push into it.
+ GIT_TRACE_PACKET=1 git -C file_child -c protocol.version=1 \
+ push origin HEAD:client_branch 2>log &&
+
+ git -C file_child log -1 --format=%s >actual &&
+ git -C file_parent log -1 --format=%s client_branch >expect &&
+ test_cmp expect actual &&
+
+ # Server responded using protocol v1
+ grep "push< version 1" log
+'
+
+test_expect_success 'cloning branchless tagless but not refless remote' '
+ rm -rf server client &&
+
+ git -c init.defaultbranch=main init server &&
+ echo foo >server/foo.txt &&
+ git -C server add foo.txt &&
+ git -C server commit -m "message" &&
+ git -C server update-ref refs/notbranch/alsonottag HEAD &&
+ git -C server checkout --detach &&
+ git -C server branch -D main &&
+ git -C server symbolic-ref HEAD refs/heads/nonexistentbranch &&
+
+ git -c protocol.version=1 clone "file://$(pwd)/server" client
+'
+
+# Test protocol v1 with 'ssh://' transport
+#
+test_expect_success 'setup ssh wrapper' '
+ GIT_SSH="$GIT_BUILD_DIR/t/helper/test-fake-ssh" &&
+ export GIT_SSH &&
+ GIT_SSH_VARIANT=ssh &&
+ export GIT_SSH_VARIANT &&
+ export TRASH_DIRECTORY &&
+ >"$TRASH_DIRECTORY"/ssh-output
+'
+
+expect_ssh () {
+ test_when_finished '(cd "$TRASH_DIRECTORY" && rm -f ssh-expect && >ssh-output)' &&
+ echo "ssh: -o SendEnv=GIT_PROTOCOL myhost $1 '$PWD/ssh_parent'" >"$TRASH_DIRECTORY/ssh-expect" &&
+ (cd "$TRASH_DIRECTORY" && test_cmp ssh-expect ssh-output)
+}
+
+test_expect_success 'create repo to be served by ssh:// transport' '
+ git init ssh_parent &&
+ test_commit -C ssh_parent one
+'
+
+test_expect_success 'clone with ssh:// using protocol v1' '
+ GIT_TRACE_PACKET=1 git -c protocol.version=1 \
+ clone "ssh://myhost:$(pwd)/ssh_parent" ssh_child 2>log &&
+ expect_ssh git-upload-pack &&
+
+ git -C ssh_child log -1 --format=%s >actual &&
+ git -C ssh_parent log -1 --format=%s >expect &&
+ test_cmp expect actual &&
+
+ # Server responded using protocol v1
+ grep "clone< version 1" log
+'
+
+test_expect_success 'fetch with ssh:// using protocol v1' '
+ test_commit -C ssh_parent two &&
+
+ GIT_TRACE_PACKET=1 git -C ssh_child -c protocol.version=1 \
+ fetch 2>log &&
+ expect_ssh git-upload-pack &&
+
+ git -C ssh_child log -1 --format=%s origin/main >actual &&
+ git -C ssh_parent log -1 --format=%s >expect &&
+ test_cmp expect actual &&
+
+ # Server responded using protocol v1
+ grep "fetch< version 1" log
+'
+
+test_expect_success 'pull with ssh:// using protocol v1' '
+ GIT_TRACE_PACKET=1 git -C ssh_child -c protocol.version=1 \
+ pull 2>log &&
+ expect_ssh git-upload-pack &&
+
+ git -C ssh_child log -1 --format=%s >actual &&
+ git -C ssh_parent log -1 --format=%s >expect &&
+ test_cmp expect actual &&
+
+ # Server responded using protocol v1
+ grep "fetch< version 1" log
+'
+
+test_expect_success 'push with ssh:// using protocol v1' '
+ test_commit -C ssh_child three &&
+
+ # Push to another branch, as the target repository has the
+ # main branch checked out and we cannot push into it.
+ GIT_TRACE_PACKET=1 git -C ssh_child -c protocol.version=1 \
+ push origin HEAD:client_branch 2>log &&
+ expect_ssh git-receive-pack &&
+
+ git -C ssh_child log -1 --format=%s >actual &&
+ git -C ssh_parent log -1 --format=%s client_branch >expect &&
+ test_cmp expect actual &&
+
+ # Server responded using protocol v1
+ grep "push< version 1" log
+'
+
+test_expect_success 'clone propagates object-format from empty repo' '
+ test_when_finished "rm -fr src256 dst256" &&
+
+ echo sha256 >expect &&
+ git init --object-format=sha256 src256 &&
+ git clone --no-local src256 dst256 &&
+ git -C dst256 rev-parse --show-object-format >actual &&
+
+ test_cmp expect actual
+'
+
+# Test protocol v1 with 'http://' transport
+#
+. "$TEST_DIRECTORY"/lib-httpd.sh
+start_httpd
+
+test_expect_success 'create repos to be served by http:// transport' '
+ git init "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
+ git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" config http.receivepack true &&
+ test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" one &&
+ git init --object-format=sha256 "$HTTPD_DOCUMENT_ROOT_PATH/sha256" &&
+ git -C "$HTTPD_DOCUMENT_ROOT_PATH/sha256" config http.receivepack true
+'
+
+test_expect_success 'clone with http:// using protocol v1' '
+ GIT_TRACE_PACKET=1 GIT_TRACE_CURL=1 git -c protocol.version=1 \
+ clone "$HTTPD_URL/smart/http_parent" http_child 2>log &&
+
+ git -C http_child log -1 --format=%s >actual &&
+ git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" log -1 --format=%s >expect &&
+ test_cmp expect actual &&
+
+ # Client requested to use protocol v1
+ grep "Git-Protocol: version=1" log &&
+ # Server responded using protocol v1
+ grep "git< version 1" log
+'
+
+test_expect_success 'clone with http:// using protocol v1 with empty SHA-256 repo' '
+ GIT_TRACE_PACKET=1 GIT_TRACE_CURL=1 git -c protocol.version=1 \
+ clone "$HTTPD_URL/smart/sha256" sha256 2>log &&
+
+ echo sha256 >expect &&
+ git -C sha256 rev-parse --show-object-format >actual &&
+ test_cmp expect actual &&
+
+ # Client requested to use protocol v1
+ grep "Git-Protocol: version=1" log &&
+ # Server responded using protocol v1
+ grep "git< version 1" log
+'
+
+test_expect_success 'fetch with http:// using protocol v1' '
+ test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" two &&
+
+ GIT_TRACE_PACKET=1 git -C http_child -c protocol.version=1 \
+ fetch 2>log &&
+
+ git -C http_child log -1 --format=%s origin/main >actual &&
+ git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" log -1 --format=%s >expect &&
+ test_cmp expect actual &&
+
+ # Server responded using protocol v1
+ grep "git< version 1" log
+'
+
+test_expect_success 'pull with http:// using protocol v1' '
+ GIT_TRACE_PACKET=1 git -C http_child -c protocol.version=1 \
+ pull 2>log &&
+
+ git -C http_child log -1 --format=%s >actual &&
+ git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" log -1 --format=%s >expect &&
+ test_cmp expect actual &&
+
+ # Server responded using protocol v1
+ grep "git< version 1" log
+'
+
+test_expect_success 'push with http:// using protocol v1' '
+ test_commit -C http_child three &&
+
+ # Push to another branch, as the target repository has the
+ # main branch checked out and we cannot push into it.
+ GIT_TRACE_PACKET=1 git -C http_child -c protocol.version=1 \
+ push origin HEAD:client_branch && #2>log &&
+
+ git -C http_child log -1 --format=%s >actual &&
+ git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" log -1 --format=%s client_branch >expect &&
+ test_cmp expect actual &&
+
+ # Server responded using protocol v1
+ grep "git< version 1" log
+'
+
+# DO NOT add non-httpd-specific tests here, because the last part of this
+# test script is only executed when httpd is available and enabled.
+
+test_done
diff --git a/t/t5701-git-serve.sh b/t/t5701-git-serve.sh
new file mode 100755
index 0000000..3591bc2
--- /dev/null
+++ b/t/t5701-git-serve.sh
@@ -0,0 +1,383 @@
+#!/bin/sh
+
+test_description='test protocol v2 server commands'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'test capability advertisement' '
+ test_oid_cache <<-EOF &&
+ wrong_algo sha1:sha256
+ wrong_algo sha256:sha1
+ EOF
+ cat >expect.base <<-EOF &&
+ version 2
+ agent=git/$(git version | cut -d" " -f3)
+ ls-refs=unborn
+ fetch=shallow wait-for-done
+ server-option
+ object-format=$(test_oid algo)
+ object-info
+ EOF
+ cat >expect.trailer <<-EOF &&
+ 0000
+ EOF
+ cat expect.base expect.trailer >expect &&
+
+ GIT_TEST_SIDEBAND_ALL=0 test-tool serve-v2 \
+ --advertise-capabilities >out &&
+ test-tool pkt-line unpack <out >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'stateless-rpc flag does not list capabilities' '
+ # Empty request
+ test-tool pkt-line pack >in <<-EOF &&
+ 0000
+ EOF
+ test-tool serve-v2 --stateless-rpc >out <in &&
+ test_must_be_empty out &&
+
+ # EOF
+ test-tool serve-v2 --stateless-rpc >out &&
+ test_must_be_empty out
+'
+
+test_expect_success 'request invalid capability' '
+ test-tool pkt-line pack >in <<-EOF &&
+ foobar
+ 0000
+ EOF
+ test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
+ test_grep "unknown capability" err
+'
+
+test_expect_success 'request with no command' '
+ test-tool pkt-line pack >in <<-EOF &&
+ agent=git/test
+ object-format=$(test_oid algo)
+ 0000
+ EOF
+ test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
+ test_grep "no command requested" err
+'
+
+test_expect_success 'request invalid command' '
+ test-tool pkt-line pack >in <<-EOF &&
+ command=foo
+ object-format=$(test_oid algo)
+ agent=git/test
+ 0000
+ EOF
+ test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
+ test_grep "invalid command" err
+'
+
+test_expect_success 'request capability as command' '
+ test-tool pkt-line pack >in <<-EOF &&
+ command=agent
+ object-format=$(test_oid algo)
+ 0000
+ EOF
+ test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
+ grep invalid.command.*agent err
+'
+
+test_expect_success 'request command as capability' '
+ test-tool pkt-line pack >in <<-EOF &&
+ command=ls-refs
+ object-format=$(test_oid algo)
+ fetch
+ 0000
+ EOF
+ test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
+ grep unknown.capability err
+'
+
+test_expect_success 'requested command is command=value' '
+ test-tool pkt-line pack >in <<-EOF &&
+ command=ls-refs=whatever
+ object-format=$(test_oid algo)
+ 0000
+ EOF
+ test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
+ grep invalid.command.*ls-refs=whatever err
+'
+
+test_expect_success 'wrong object-format' '
+ test-tool pkt-line pack >in <<-EOF &&
+ command=fetch
+ agent=git/test
+ object-format=$(test_oid wrong_algo)
+ 0000
+ EOF
+ test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
+ test_grep "mismatched object format" err
+'
+
+# Test the basics of ls-refs
+#
+test_expect_success 'setup some refs and tags' '
+ test_commit one &&
+ git branch dev main &&
+ test_commit two &&
+ git symbolic-ref refs/heads/release refs/heads/main &&
+ git tag -a -m "annotated tag" annotated-tag
+'
+
+test_expect_success 'basics of ls-refs' '
+ test-tool pkt-line pack >in <<-EOF &&
+ command=ls-refs
+ object-format=$(test_oid algo)
+ 0000
+ EOF
+
+ cat >expect <<-EOF &&
+ $(git rev-parse HEAD) HEAD
+ $(git rev-parse refs/heads/dev) refs/heads/dev
+ $(git rev-parse refs/heads/main) refs/heads/main
+ $(git rev-parse refs/heads/release) refs/heads/release
+ $(git rev-parse refs/tags/annotated-tag) refs/tags/annotated-tag
+ $(git rev-parse refs/tags/one) refs/tags/one
+ $(git rev-parse refs/tags/two) refs/tags/two
+ 0000
+ EOF
+
+ test-tool serve-v2 --stateless-rpc <in >out &&
+ test-tool pkt-line unpack <out >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'ls-refs complains about unknown options' '
+ test-tool pkt-line pack >in <<-EOF &&
+ command=ls-refs
+ object-format=$(test_oid algo)
+ 0001
+ no-such-arg
+ 0000
+ EOF
+
+ test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
+ grep unexpected.line.*no-such-arg err
+'
+
+test_expect_success 'basic ref-prefixes' '
+ test-tool pkt-line pack >in <<-EOF &&
+ command=ls-refs
+ object-format=$(test_oid algo)
+ 0001
+ ref-prefix refs/heads/main
+ ref-prefix refs/tags/one
+ 0000
+ EOF
+
+ cat >expect <<-EOF &&
+ $(git rev-parse refs/heads/main) refs/heads/main
+ $(git rev-parse refs/tags/one) refs/tags/one
+ 0000
+ EOF
+
+ test-tool serve-v2 --stateless-rpc <in >out &&
+ test-tool pkt-line unpack <out >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'refs/heads prefix' '
+ test-tool pkt-line pack >in <<-EOF &&
+ command=ls-refs
+ object-format=$(test_oid algo)
+ 0001
+ ref-prefix refs/heads/
+ 0000
+ EOF
+
+ cat >expect <<-EOF &&
+ $(git rev-parse refs/heads/dev) refs/heads/dev
+ $(git rev-parse refs/heads/main) refs/heads/main
+ $(git rev-parse refs/heads/release) refs/heads/release
+ 0000
+ EOF
+
+ test-tool serve-v2 --stateless-rpc <in >out &&
+ test-tool pkt-line unpack <out >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'ignore very large set of prefixes' '
+ # generate a large number of ref-prefixes that we expect
+ # to match nothing; the value here exceeds TOO_MANY_PREFIXES
+ # from ls-refs.c.
+ {
+ echo command=ls-refs &&
+ echo object-format=$(test_oid algo) &&
+ echo 0001 &&
+ perl -le "print \"ref-prefix refs/heads/\$_\" for (1..65536)" &&
+ echo 0000
+ } |
+ test-tool pkt-line pack >in &&
+
+ # and then confirm that we see unmatched prefixes anyway (i.e.,
+ # that the prefix was not applied).
+ cat >expect <<-EOF &&
+ $(git rev-parse HEAD) HEAD
+ $(git rev-parse refs/heads/dev) refs/heads/dev
+ $(git rev-parse refs/heads/main) refs/heads/main
+ $(git rev-parse refs/heads/release) refs/heads/release
+ $(git rev-parse refs/tags/annotated-tag) refs/tags/annotated-tag
+ $(git rev-parse refs/tags/one) refs/tags/one
+ $(git rev-parse refs/tags/two) refs/tags/two
+ 0000
+ EOF
+
+ test-tool serve-v2 --stateless-rpc <in >out &&
+ test-tool pkt-line unpack <out >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'peel parameter' '
+ test-tool pkt-line pack >in <<-EOF &&
+ command=ls-refs
+ object-format=$(test_oid algo)
+ 0001
+ peel
+ ref-prefix refs/tags/
+ 0000
+ EOF
+
+ cat >expect <<-EOF &&
+ $(git rev-parse refs/tags/annotated-tag) refs/tags/annotated-tag peeled:$(git rev-parse refs/tags/annotated-tag^{})
+ $(git rev-parse refs/tags/one) refs/tags/one
+ $(git rev-parse refs/tags/two) refs/tags/two
+ 0000
+ EOF
+
+ test-tool serve-v2 --stateless-rpc <in >out &&
+ test-tool pkt-line unpack <out >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'symrefs parameter' '
+ test-tool pkt-line pack >in <<-EOF &&
+ command=ls-refs
+ object-format=$(test_oid algo)
+ 0001
+ symrefs
+ ref-prefix refs/heads/
+ 0000
+ EOF
+
+ cat >expect <<-EOF &&
+ $(git rev-parse refs/heads/dev) refs/heads/dev
+ $(git rev-parse refs/heads/main) refs/heads/main
+ $(git rev-parse refs/heads/release) refs/heads/release symref-target:refs/heads/main
+ 0000
+ EOF
+
+ test-tool serve-v2 --stateless-rpc <in >out &&
+ test-tool pkt-line unpack <out >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'sending server-options' '
+ test-tool pkt-line pack >in <<-EOF &&
+ command=ls-refs
+ object-format=$(test_oid algo)
+ server-option=hello
+ server-option=world
+ 0001
+ ref-prefix HEAD
+ 0000
+ EOF
+
+ cat >expect <<-EOF &&
+ $(git rev-parse HEAD) HEAD
+ 0000
+ EOF
+
+ test-tool serve-v2 --stateless-rpc <in >out &&
+ test-tool pkt-line unpack <out >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'unexpected lines are not allowed in fetch request' '
+ git init server &&
+
+ test-tool pkt-line pack >in <<-EOF &&
+ command=fetch
+ object-format=$(test_oid algo)
+ 0001
+ this-is-not-a-command
+ 0000
+ EOF
+
+ (
+ cd server &&
+ test_must_fail test-tool serve-v2 --stateless-rpc
+ ) <in >/dev/null 2>err &&
+ grep "unexpected line: .this-is-not-a-command." err
+'
+
+# Test the basics of object-info
+#
+test_expect_success 'basics of object-info' '
+ test-tool pkt-line pack >in <<-EOF &&
+ command=object-info
+ object-format=$(test_oid algo)
+ 0001
+ size
+ oid $(git rev-parse two:two.t)
+ oid $(git rev-parse two:two.t)
+ 0000
+ EOF
+
+ cat >expect <<-EOF &&
+ size
+ $(git rev-parse two:two.t) $(wc -c <two.t | xargs)
+ $(git rev-parse two:two.t) $(wc -c <two.t | xargs)
+ 0000
+ EOF
+
+ test-tool serve-v2 --stateless-rpc <in >out &&
+ test-tool pkt-line unpack <out >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'test capability advertisement with uploadpack.advertiseBundleURIs' '
+ test_config uploadpack.advertiseBundleURIs true &&
+
+ cat >expect.extra <<-EOF &&
+ bundle-uri
+ EOF
+ cat expect.base \
+ expect.extra \
+ expect.trailer >expect &&
+
+ GIT_TEST_SIDEBAND_ALL=0 test-tool serve-v2 \
+ --advertise-capabilities >out &&
+ test-tool pkt-line unpack <out >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'basics of bundle-uri: dies if not enabled' '
+ test-tool pkt-line pack >in <<-EOF &&
+ command=bundle-uri
+ 0000
+ EOF
+
+ cat >err.expect <<-\EOF &&
+ fatal: invalid command '"'"'bundle-uri'"'"'
+ EOF
+
+ cat >expect <<-\EOF &&
+ ERR serve: invalid command '"'"'bundle-uri'"'"'
+ EOF
+
+ test_must_fail test-tool serve-v2 --stateless-rpc <in >out 2>err.actual &&
+ test_cmp err.expect err.actual &&
+ test_must_be_empty out
+'
+
+test_done
diff --git a/t/t5702-protocol-v2.sh b/t/t5702-protocol-v2.sh
new file mode 100755
index 0000000..3c0c604
--- /dev/null
+++ b/t/t5702-protocol-v2.sh
@@ -0,0 +1,1319 @@
+#!/bin/sh
+
+test_description='test git wire-protocol version 2'
+
+TEST_NO_CREATE_REPO=1
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+# Test protocol v2 with 'git://' transport
+#
+. "$TEST_DIRECTORY"/lib-git-daemon.sh
+start_git_daemon --export-all --enable=receive-pack
+daemon_parent=$GIT_DAEMON_DOCUMENT_ROOT_PATH/parent
+
+test_expect_success 'create repo to be served by git-daemon' '
+ git init "$daemon_parent" &&
+ test_commit -C "$daemon_parent" one
+'
+
+test_expect_success 'list refs with git:// using protocol v2' '
+ test_when_finished "rm -f log" &&
+
+ GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
+ ls-remote --symref "$GIT_DAEMON_URL/parent" >actual &&
+
+ # Client requested to use protocol v2
+ grep "ls-remote> .*\\\0\\\0version=2\\\0$" log &&
+ # Server responded using protocol v2
+ grep "ls-remote< version 2" log &&
+
+ git ls-remote --symref "$GIT_DAEMON_URL/parent" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'ref advertisement is filtered with ls-remote using protocol v2' '
+ test_when_finished "rm -f log" &&
+
+ GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
+ ls-remote "$GIT_DAEMON_URL/parent" main >actual &&
+
+ cat >expect <<-EOF &&
+ $(git -C "$daemon_parent" rev-parse refs/heads/main)$(printf "\t")refs/heads/main
+ EOF
+
+ test_cmp expect actual
+'
+
+test_expect_success 'clone with git:// using protocol v2' '
+ test_when_finished "rm -f log" &&
+
+ GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
+ clone "$GIT_DAEMON_URL/parent" daemon_child &&
+
+ git -C daemon_child log -1 --format=%s >actual &&
+ git -C "$daemon_parent" log -1 --format=%s >expect &&
+ test_cmp expect actual &&
+
+ # Client requested to use protocol v2
+ grep "clone> .*\\\0\\\0version=2\\\0$" log &&
+ # Server responded using protocol v2
+ grep "clone< version 2" log
+'
+
+test_expect_success 'fetch with git:// using protocol v2' '
+ test_when_finished "rm -f log" &&
+
+ test_commit -C "$daemon_parent" two &&
+
+ GIT_TRACE_PACKET="$(pwd)/log" git -C daemon_child -c protocol.version=2 \
+ fetch &&
+
+ git -C daemon_child log -1 --format=%s origin/main >actual &&
+ git -C "$daemon_parent" log -1 --format=%s >expect &&
+ test_cmp expect actual &&
+
+ # Client requested to use protocol v2
+ grep "fetch> .*\\\0\\\0version=2\\\0$" log &&
+ # Server responded using protocol v2
+ grep "fetch< version 2" log
+'
+
+test_expect_success 'fetch by hash without tag following with protocol v2 does not list refs' '
+ test_when_finished "rm -f log" &&
+
+ test_commit -C "$daemon_parent" two_a &&
+ git -C "$daemon_parent" rev-parse two_a >two_a_hash &&
+
+ GIT_TRACE_PACKET="$(pwd)/log" git -C daemon_child -c protocol.version=2 \
+ fetch --no-tags origin $(cat two_a_hash) &&
+
+ grep "fetch< version 2" log &&
+ ! grep "fetch> command=ls-refs" log
+'
+
+test_expect_success 'pull with git:// using protocol v2' '
+ test_when_finished "rm -f log" &&
+
+ GIT_TRACE_PACKET="$(pwd)/log" git -C daemon_child -c protocol.version=2 \
+ pull &&
+
+ git -C daemon_child log -1 --format=%s >actual &&
+ git -C "$daemon_parent" log -1 --format=%s >expect &&
+ test_cmp expect actual &&
+
+ # Client requested to use protocol v2
+ grep "fetch> .*\\\0\\\0version=2\\\0$" log &&
+ # Server responded using protocol v2
+ grep "fetch< version 2" log
+'
+
+test_expect_success 'push with git:// and a config of v2 does not request v2' '
+ test_when_finished "rm -f log" &&
+
+ # Till v2 for push is designed, make sure that if a client has
+ # protocol.version configured to use v2, that the client instead falls
+ # back and uses v0.
+
+ test_commit -C daemon_child three &&
+
+ # Push to another branch, as the target repository has the
+ # main branch checked out and we cannot push into it.
+ GIT_TRACE_PACKET="$(pwd)/log" git -C daemon_child -c protocol.version=2 \
+ push origin HEAD:client_branch &&
+
+ git -C daemon_child log -1 --format=%s >actual &&
+ git -C "$daemon_parent" log -1 --format=%s client_branch >expect &&
+ test_cmp expect actual &&
+
+ # Client requested to use protocol v2
+ ! grep "push> .*\\\0\\\0version=2\\\0$" log &&
+ # Server responded using protocol v2
+ ! grep "push< version 2" log
+'
+
+stop_git_daemon
+
+# Test protocol v2 with 'file://' transport
+#
+test_expect_success 'create repo to be served by file:// transport' '
+ git init file_parent &&
+ test_commit -C file_parent one
+'
+
+test_expect_success 'list refs with file:// using protocol v2' '
+ test_when_finished "rm -f log" &&
+
+ GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
+ ls-remote --symref "file://$(pwd)/file_parent" >actual &&
+
+ # Server responded using protocol v2
+ grep "ls-remote< version 2" log &&
+
+ git ls-remote --symref "file://$(pwd)/file_parent" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'ref advertisement is filtered with ls-remote using protocol v2' '
+ test_when_finished "rm -f log" &&
+
+ GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
+ ls-remote "file://$(pwd)/file_parent" main >actual &&
+
+ cat >expect <<-EOF &&
+ $(git -C file_parent rev-parse refs/heads/main)$(printf "\t")refs/heads/main
+ EOF
+
+ test_cmp expect actual
+'
+
+test_expect_success 'server-options are sent when using ls-remote' '
+ test_when_finished "rm -f log" &&
+
+ GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
+ ls-remote -o hello -o world "file://$(pwd)/file_parent" main >actual &&
+
+ cat >expect <<-EOF &&
+ $(git -C file_parent rev-parse refs/heads/main)$(printf "\t")refs/heads/main
+ EOF
+
+ test_cmp expect actual &&
+ grep "server-option=hello" log &&
+ grep "server-option=world" log
+'
+
+test_expect_success 'warn if using server-option with ls-remote with legacy protocol' '
+ test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 git -c protocol.version=0 \
+ ls-remote -o hello -o world "file://$(pwd)/file_parent" main 2>err &&
+
+ test_grep "see protocol.version in" err &&
+ test_grep "server options require protocol version 2 or later" err
+'
+
+test_expect_success 'clone with file:// using protocol v2' '
+ test_when_finished "rm -f log" &&
+
+ GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
+ clone "file://$(pwd)/file_parent" file_child &&
+
+ git -C file_child log -1 --format=%s >actual &&
+ git -C file_parent log -1 --format=%s >expect &&
+ test_cmp expect actual &&
+
+ # Server responded using protocol v2
+ grep "clone< version 2" log &&
+
+ # Client sent ref-prefixes to filter the ref-advertisement
+ grep "ref-prefix HEAD" log &&
+ grep "ref-prefix refs/heads/" log &&
+ grep "ref-prefix refs/tags/" log
+'
+
+test_expect_success 'clone of empty repo propagates name of default branch' '
+ test_when_finished "rm -rf file_empty_parent file_empty_child" &&
+
+ GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME= \
+ git -c init.defaultBranch=mydefaultbranch init file_empty_parent &&
+
+ GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME= \
+ git -c init.defaultBranch=main -c protocol.version=2 \
+ clone "file://$(pwd)/file_empty_parent" file_empty_child &&
+ grep "refs/heads/mydefaultbranch" file_empty_child/.git/HEAD
+'
+
+test_expect_success '...but not if explicitly forbidden by config' '
+ test_when_finished "rm -rf file_empty_parent file_empty_child" &&
+
+ GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME= \
+ git -c init.defaultBranch=mydefaultbranch init file_empty_parent &&
+ test_config -C file_empty_parent lsrefs.unborn ignore &&
+
+ GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME= \
+ git -c init.defaultBranch=main -c protocol.version=2 \
+ clone "file://$(pwd)/file_empty_parent" file_empty_child &&
+ ! grep "refs/heads/mydefaultbranch" file_empty_child/.git/HEAD
+'
+
+test_expect_success 'bare clone propagates empty default branch' '
+ test_when_finished "rm -rf file_empty_parent file_empty_child.git" &&
+
+ GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME= \
+ git -c init.defaultBranch=mydefaultbranch init file_empty_parent &&
+
+ GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME= \
+ git -c init.defaultBranch=main -c protocol.version=2 \
+ clone --bare \
+ "file://$(pwd)/file_empty_parent" file_empty_child.git &&
+ grep "refs/heads/mydefaultbranch" file_empty_child.git/HEAD
+'
+
+test_expect_success 'clone propagates unborn HEAD from non-empty repo' '
+ test_when_finished "rm -rf file_unborn_parent file_unborn_child" &&
+
+ git init file_unborn_parent &&
+ (
+ cd file_unborn_parent &&
+ git checkout -b branchwithstuff &&
+ test_commit --no-tag stuff &&
+ git symbolic-ref HEAD refs/heads/mydefaultbranch
+ ) &&
+
+ GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME= \
+ git -c init.defaultBranch=main -c protocol.version=2 \
+ clone "file://$(pwd)/file_unborn_parent" \
+ file_unborn_child 2>stderr &&
+ grep "refs/heads/mydefaultbranch" file_unborn_child/.git/HEAD &&
+ grep "warning: remote HEAD refers to nonexistent ref" stderr
+'
+
+test_expect_success 'clone propagates object-format from empty repo' '
+ test_when_finished "rm -fr src256 dst256" &&
+
+ echo sha256 >expect &&
+ git init --object-format=sha256 src256 &&
+ git clone src256 dst256 &&
+ git -C dst256 rev-parse --show-object-format >actual &&
+
+ test_cmp expect actual
+'
+
+test_expect_success 'bare clone propagates unborn HEAD from non-empty repo' '
+ test_when_finished "rm -rf file_unborn_parent file_unborn_child.git" &&
+
+ git init file_unborn_parent &&
+ (
+ cd file_unborn_parent &&
+ git checkout -b branchwithstuff &&
+ test_commit --no-tag stuff &&
+ git symbolic-ref HEAD refs/heads/mydefaultbranch
+ ) &&
+
+ GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME= \
+ git -c init.defaultBranch=main -c protocol.version=2 \
+ clone --bare "file://$(pwd)/file_unborn_parent" \
+ file_unborn_child.git 2>stderr &&
+ grep "refs/heads/mydefaultbranch" file_unborn_child.git/HEAD &&
+ ! grep "warning:" stderr
+'
+
+test_expect_success 'defaulted HEAD uses remote branch if available' '
+ test_when_finished "rm -rf file_unborn_parent file_unborn_child" &&
+
+ git init file_unborn_parent &&
+ (
+ cd file_unborn_parent &&
+ git config lsrefs.unborn ignore &&
+ git checkout -b branchwithstuff &&
+ test_commit --no-tag stuff &&
+ git symbolic-ref HEAD refs/heads/mydefaultbranch
+ ) &&
+
+ GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME= \
+ git -c init.defaultBranch=branchwithstuff -c protocol.version=2 \
+ clone "file://$(pwd)/file_unborn_parent" \
+ file_unborn_child 2>stderr &&
+ grep "refs/heads/branchwithstuff" file_unborn_child/.git/HEAD &&
+ test_path_is_file file_unborn_child/stuff.t &&
+ ! grep "warning:" stderr
+'
+
+test_expect_success 'fetch with file:// using protocol v2' '
+ test_when_finished "rm -f log" &&
+
+ test_commit -C file_parent two &&
+
+ GIT_TRACE_PACKET="$(pwd)/log" git -C file_child -c protocol.version=2 \
+ fetch origin &&
+
+ git -C file_child log -1 --format=%s origin/main >actual &&
+ git -C file_parent log -1 --format=%s >expect &&
+ test_cmp expect actual &&
+
+ # Server responded using protocol v2
+ grep "fetch< version 2" log
+'
+
+test_expect_success 'ref advertisement is filtered during fetch using protocol v2' '
+ test_when_finished "rm -f log" &&
+
+ test_commit -C file_parent three &&
+ git -C file_parent branch unwanted-branch three &&
+
+ GIT_TRACE_PACKET="$(pwd)/log" git -C file_child -c protocol.version=2 \
+ fetch origin main &&
+
+ git -C file_child log -1 --format=%s origin/main >actual &&
+ git -C file_parent log -1 --format=%s >expect &&
+ test_cmp expect actual &&
+
+ grep "refs/heads/main" log &&
+ ! grep "refs/heads/unwanted-branch" log
+'
+
+test_expect_success 'server-options are sent when fetching' '
+ test_when_finished "rm -f log" &&
+
+ test_commit -C file_parent four &&
+
+ GIT_TRACE_PACKET="$(pwd)/log" git -C file_child -c protocol.version=2 \
+ fetch -o hello -o world origin main &&
+
+ git -C file_child log -1 --format=%s origin/main >actual &&
+ git -C file_parent log -1 --format=%s >expect &&
+ test_cmp expect actual &&
+
+ grep "server-option=hello" log &&
+ grep "server-option=world" log
+'
+
+test_expect_success 'warn if using server-option with fetch with legacy protocol' '
+ test_when_finished "rm -rf temp_child" &&
+
+ git init temp_child &&
+
+ test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 git -C temp_child -c protocol.version=0 \
+ fetch -o hello -o world "file://$(pwd)/file_parent" main 2>err &&
+
+ test_grep "see protocol.version in" err &&
+ test_grep "server options require protocol version 2 or later" err
+'
+
+test_expect_success 'server-options are sent when cloning' '
+ test_when_finished "rm -rf log myclone" &&
+
+ GIT_TRACE_PACKET="$(pwd)/log" git -c protocol.version=2 \
+ clone --server-option=hello --server-option=world \
+ "file://$(pwd)/file_parent" myclone &&
+
+ grep "server-option=hello" log &&
+ grep "server-option=world" log
+'
+
+test_expect_success 'warn if using server-option with clone with legacy protocol' '
+ test_when_finished "rm -rf myclone" &&
+
+ test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 git -c protocol.version=0 \
+ clone --server-option=hello --server-option=world \
+ "file://$(pwd)/file_parent" myclone 2>err &&
+
+ test_grep "see protocol.version in" err &&
+ test_grep "server options require protocol version 2 or later" err
+'
+
+test_expect_success 'upload-pack respects config using protocol v2' '
+ git init server &&
+ write_script server/.git/hook <<-\EOF &&
+ touch hookout
+ "$@"
+ EOF
+ test_commit -C server one &&
+
+ test_config_global uploadpack.packobjectshook ./hook &&
+ test_path_is_missing server/.git/hookout &&
+ git -c protocol.version=2 clone "file://$(pwd)/server" client &&
+ test_path_is_file server/.git/hookout
+'
+
+test_expect_success 'setup filter tests' '
+ rm -rf server client &&
+ git init server &&
+
+ # 1 commit to create a file, and 1 commit to modify it
+ test_commit -C server message1 a.txt &&
+ test_commit -C server message2 a.txt &&
+ git -C server config protocol.version 2 &&
+ git -C server config uploadpack.allowfilter 1 &&
+ git -C server config uploadpack.allowanysha1inwant 1 &&
+ git -C server config protocol.version 2
+'
+
+test_expect_success 'partial clone' '
+ GIT_TRACE_PACKET="$(pwd)/trace" git -c protocol.version=2 \
+ clone --filter=blob:none "file://$(pwd)/server" client &&
+ grep "version 2" trace &&
+
+ # Ensure that the old version of the file is missing
+ git -C client rev-list --quiet --objects --missing=print main \
+ >observed.oids &&
+ grep "$(git -C server rev-parse message1:a.txt)" observed.oids &&
+
+ # Ensure that client passes fsck
+ git -C client fsck
+'
+
+test_expect_success 'dynamically fetch missing object' '
+ rm "$(pwd)/trace" &&
+ GIT_TRACE_PACKET="$(pwd)/trace" git -C client -c protocol.version=2 \
+ cat-file -p $(git -C server rev-parse message1:a.txt) &&
+ grep "version 2" trace
+'
+
+test_expect_success 'when dynamically fetching missing object, do not list refs' '
+ ! grep "git> command=ls-refs" trace
+'
+
+test_expect_success 'partial fetch' '
+ rm -rf client "$(pwd)/trace" &&
+ git init client &&
+ SERVER="file://$(pwd)/server" &&
+
+ GIT_TRACE_PACKET="$(pwd)/trace" git -C client -c protocol.version=2 \
+ fetch --filter=blob:none "$SERVER" main:refs/heads/other &&
+ grep "version 2" trace &&
+
+ # Ensure that the old version of the file is missing
+ git -C client rev-list --quiet --objects --missing=print other \
+ >observed.oids &&
+ grep "$(git -C server rev-parse message1:a.txt)" observed.oids &&
+
+ # Ensure that client passes fsck
+ git -C client fsck
+'
+
+test_expect_success 'do not advertise filter if not configured to do so' '
+ SERVER="file://$(pwd)/server" &&
+
+ rm "$(pwd)/trace" &&
+ git -C server config uploadpack.allowfilter 1 &&
+ GIT_TRACE_PACKET="$(pwd)/trace" git -c protocol.version=2 \
+ ls-remote "$SERVER" &&
+ grep "fetch=.*filter" trace &&
+
+ rm "$(pwd)/trace" &&
+ git -C server config uploadpack.allowfilter 0 &&
+ GIT_TRACE_PACKET="$(pwd)/trace" git -c protocol.version=2 \
+ ls-remote "$SERVER" &&
+ grep "fetch=" trace >fetch_capabilities &&
+ ! grep filter fetch_capabilities
+'
+
+test_expect_success 'partial clone warns if filter is not advertised' '
+ rm -rf client &&
+ git -C server config uploadpack.allowfilter 0 &&
+ git -c protocol.version=2 \
+ clone --filter=blob:none "file://$(pwd)/server" client 2>err &&
+ test_grep "filtering not recognized by server, ignoring" err
+'
+
+test_expect_success 'even with handcrafted request, filter does not work if not advertised' '
+ git -C server config uploadpack.allowfilter 0 &&
+
+ # Custom request that tries to filter even though it is not advertised.
+ test-tool pkt-line pack >in <<-EOF &&
+ command=fetch
+ object-format=$(test_oid algo)
+ 0001
+ want $(git -C server rev-parse main)
+ filter blob:none
+ 0000
+ EOF
+
+ test_must_fail test-tool -C server serve-v2 --stateless-rpc \
+ <in >/dev/null 2>err &&
+ grep "unexpected line: .filter blob:none." err &&
+
+ # Exercise to ensure that if advertised, filter works
+ git -C server config uploadpack.allowfilter 1 &&
+ test-tool -C server serve-v2 --stateless-rpc <in >/dev/null
+'
+
+test_expect_success 'default refspec is used to filter ref when fetchcing' '
+ test_when_finished "rm -f log" &&
+
+ GIT_TRACE_PACKET="$(pwd)/log" git -C file_child -c protocol.version=2 \
+ fetch origin &&
+
+ git -C file_child log -1 --format=%s three >actual &&
+ git -C file_parent log -1 --format=%s three >expect &&
+ test_cmp expect actual &&
+
+ grep "ref-prefix refs/heads/" log &&
+ grep "ref-prefix refs/tags/" log
+'
+
+test_expect_success 'fetch supports various ways of have lines' '
+ rm -rf server client trace &&
+ git init server &&
+ test_commit -C server dwim &&
+ TREE=$(git -C server rev-parse HEAD^{tree}) &&
+ git -C server tag exact \
+ $(git -C server commit-tree -m a "$TREE") &&
+ git -C server tag dwim-unwanted \
+ $(git -C server commit-tree -m b "$TREE") &&
+ git -C server tag exact-unwanted \
+ $(git -C server commit-tree -m c "$TREE") &&
+ git -C server tag prefix1 \
+ $(git -C server commit-tree -m d "$TREE") &&
+ git -C server tag prefix2 \
+ $(git -C server commit-tree -m e "$TREE") &&
+ git -C server tag fetch-by-sha1 \
+ $(git -C server commit-tree -m f "$TREE") &&
+ git -C server tag completely-unrelated \
+ $(git -C server commit-tree -m g "$TREE") &&
+
+ git init client &&
+ GIT_TRACE_PACKET="$(pwd)/trace" git -C client -c protocol.version=2 \
+ fetch "file://$(pwd)/server" \
+ dwim \
+ refs/tags/exact \
+ refs/tags/prefix*:refs/tags/prefix* \
+ "$(git -C server rev-parse fetch-by-sha1)" &&
+
+ # Ensure that the appropriate prefixes are sent (using a sample)
+ grep "fetch> ref-prefix dwim" trace &&
+ grep "fetch> ref-prefix refs/heads/dwim" trace &&
+ grep "fetch> ref-prefix refs/tags/prefix" trace &&
+
+ # Ensure that the correct objects are returned
+ git -C client cat-file -e $(git -C server rev-parse dwim) &&
+ git -C client cat-file -e $(git -C server rev-parse exact) &&
+ git -C client cat-file -e $(git -C server rev-parse prefix1) &&
+ git -C client cat-file -e $(git -C server rev-parse prefix2) &&
+ git -C client cat-file -e $(git -C server rev-parse fetch-by-sha1) &&
+ test_must_fail git -C client cat-file -e \
+ $(git -C server rev-parse dwim-unwanted) &&
+ test_must_fail git -C client cat-file -e \
+ $(git -C server rev-parse exact-unwanted) &&
+ test_must_fail git -C client cat-file -e \
+ $(git -C server rev-parse completely-unrelated)
+'
+
+test_expect_success 'fetch supports include-tag and tag following' '
+ rm -rf server client trace &&
+ git init server &&
+
+ test_commit -C server to_fetch &&
+ git -C server tag -a annotated_tag -m message &&
+
+ git init client &&
+ GIT_TRACE_PACKET="$(pwd)/trace" git -C client -c protocol.version=2 \
+ fetch "$(pwd)/server" to_fetch:to_fetch &&
+
+ grep "fetch> ref-prefix to_fetch" trace &&
+ grep "fetch> ref-prefix refs/tags/" trace &&
+ grep "fetch> include-tag" trace &&
+
+ git -C client cat-file -e $(git -C client rev-parse annotated_tag)
+'
+
+test_expect_success 'upload-pack respects client shallows' '
+ rm -rf server client trace &&
+
+ git init server &&
+ test_commit -C server base &&
+ test_commit -C server client_has &&
+
+ git clone --depth=1 "file://$(pwd)/server" client &&
+
+ # Add extra commits to the client so that the whole fetch takes more
+ # than 1 request (due to negotiation)
+ test_commit_bulk -C client --id=c 32 &&
+
+ git -C server checkout -b newbranch base &&
+ test_commit -C server client_wants &&
+
+ GIT_TRACE_PACKET="$(pwd)/trace" git -C client -c protocol.version=2 \
+ fetch origin newbranch &&
+ # Ensure that protocol v2 is used
+ grep "fetch< version 2" trace
+'
+
+test_expect_success 'ensure that multiple fetches in same process from a shallow repo works' '
+ rm -rf server client trace &&
+
+ test_create_repo server &&
+ test_commit -C server one &&
+ test_commit -C server two &&
+ test_commit -C server three &&
+ git clone --shallow-exclude two "file://$(pwd)/server" client &&
+
+ git -C server tag -a -m "an annotated tag" twotag two &&
+
+ # Triggers tag following (thus, 2 fetches in one process)
+ GIT_TRACE_PACKET="$(pwd)/trace" git -C client -c protocol.version=2 \
+ fetch --shallow-exclude one origin &&
+ # Ensure that protocol v2 is used
+ grep "fetch< version 2" trace
+'
+
+test_expect_success 'deepen-relative' '
+ rm -rf server client trace &&
+
+ test_create_repo server &&
+ test_commit -C server one &&
+ test_commit -C server two &&
+ test_commit -C server three &&
+ git clone --depth 1 "file://$(pwd)/server" client &&
+ test_commit -C server four &&
+
+ # Sanity check that only "three" is downloaded
+ git -C client log --pretty=tformat:%s main >actual &&
+ echo three >expected &&
+ test_cmp expected actual &&
+
+ GIT_TRACE_PACKET="$(pwd)/trace" git -C client -c protocol.version=2 \
+ fetch --deepen=1 origin &&
+ # Ensure that protocol v2 is used
+ grep "fetch< version 2" trace &&
+
+ git -C client log --pretty=tformat:%s origin/main >actual &&
+ cat >expected <<-\EOF &&
+ four
+ three
+ two
+ EOF
+ test_cmp expected actual
+'
+
+setup_negotiate_only () {
+ SERVER="$1"
+ URI="$2"
+
+ rm -rf "$SERVER" client
+
+ git init "$SERVER"
+ test_commit -C "$SERVER" one
+ test_commit -C "$SERVER" two
+
+ git clone "$URI" client
+ test_commit -C client three
+}
+
+test_expect_success 'usage: --negotiate-only without --negotiation-tip' '
+ SERVER="server" &&
+ URI="file://$(pwd)/server" &&
+
+ setup_negotiate_only "$SERVER" "$URI" &&
+
+ cat >err.expect <<-\EOF &&
+ fatal: --negotiate-only needs one or more --negotiation-tip=*
+ EOF
+
+ test_must_fail git -c protocol.version=2 -C client fetch \
+ --negotiate-only \
+ origin 2>err.actual &&
+ test_cmp err.expect err.actual
+'
+
+test_expect_success 'usage: --negotiate-only with --recurse-submodules' '
+ cat >err.expect <<-\EOF &&
+ fatal: options '\''--negotiate-only'\'' and '\''--recurse-submodules'\'' cannot be used together
+ EOF
+
+ test_must_fail git -c protocol.version=2 -C client fetch \
+ --negotiate-only \
+ --recurse-submodules \
+ origin 2>err.actual &&
+ test_cmp err.expect err.actual
+'
+
+test_expect_success 'file:// --negotiate-only' '
+ SERVER="server" &&
+ URI="file://$(pwd)/server" &&
+
+ setup_negotiate_only "$SERVER" "$URI" &&
+
+ git -c protocol.version=2 -C client fetch \
+ --no-tags \
+ --negotiate-only \
+ --negotiation-tip=$(git -C client rev-parse HEAD) \
+ origin >out &&
+ COMMON=$(git -C "$SERVER" rev-parse two) &&
+ grep "$COMMON" out
+'
+
+test_expect_success 'file:// --negotiate-only with protocol v0' '
+ SERVER="server" &&
+ URI="file://$(pwd)/server" &&
+
+ setup_negotiate_only "$SERVER" "$URI" &&
+
+ test_must_fail git -c protocol.version=0 -C client fetch \
+ --no-tags \
+ --negotiate-only \
+ --negotiation-tip=$(git -C client rev-parse HEAD) \
+ origin 2>err &&
+ test_grep "negotiate-only requires protocol v2" err
+'
+
+test_expect_success 'push with custom path does not request v2' '
+ rm -f env.trace &&
+ git -C client push \
+ --receive-pack="env >../env.trace; git-receive-pack" \
+ origin HEAD:refs/heads/custom-push-test &&
+ test_path_is_file env.trace &&
+ ! grep ^GIT_PROTOCOL env.trace
+'
+
+test_expect_success 'fetch with custom path does request v2' '
+ rm -f env.trace &&
+ git -C client fetch \
+ --upload-pack="env >../env.trace; git-upload-pack" \
+ origin HEAD &&
+ grep ^GIT_PROTOCOL=version=2 env.trace
+'
+
+test_expect_success 'archive with custom path does not request v2' '
+ rm -f env.trace &&
+ git -C client archive \
+ --exec="env >../env.trace; git-upload-archive" \
+ --remote=origin \
+ HEAD >/dev/null &&
+ test_path_is_file env.trace &&
+ ! grep ^GIT_PROTOCOL env.trace
+'
+
+# Test protocol v2 with 'http://' transport
+#
+. "$TEST_DIRECTORY"/lib-httpd.sh
+start_httpd
+
+test_expect_success 'create repo to be served by http:// transport' '
+ git init "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
+ git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" config http.receivepack true &&
+ test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" one
+'
+
+test_expect_success 'clone with http:// using protocol v2' '
+ test_when_finished "rm -f log" &&
+
+ GIT_TRACE_PACKET="$(pwd)/log" GIT_TRACE_CURL="$(pwd)/log" git -c protocol.version=2 \
+ clone "$HTTPD_URL/smart/http_parent" http_child &&
+
+ git -C http_child log -1 --format=%s >actual &&
+ git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" log -1 --format=%s >expect &&
+ test_cmp expect actual &&
+
+ # Client requested to use protocol v2
+ grep "Git-Protocol: version=2" log &&
+ # Server responded using protocol v2
+ grep "git< version 2" log &&
+ # Verify that the chunked encoding sending codepath is NOT exercised
+ ! grep "Send header: Transfer-Encoding: chunked" log
+'
+
+test_expect_success 'clone repository with http:// using protocol v2 with incomplete pktline length' '
+ test_when_finished "rm -f log" &&
+
+ git init "$HTTPD_DOCUMENT_ROOT_PATH/incomplete_length" &&
+ test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/incomplete_length" file &&
+
+ test_must_fail env GIT_TRACE_PACKET="$(pwd)/log" GIT_TRACE_CURL="$(pwd)/log" git -c protocol.version=2 \
+ clone "$HTTPD_URL/smart/incomplete_length" incomplete_length_child 2>err &&
+
+ # Client requested to use protocol v2
+ grep "Git-Protocol: version=2" log &&
+ # Server responded using protocol v2
+ grep "git< version 2" log &&
+ # Client reported appropriate failure
+ test_grep "bytes of length header were received" err
+'
+
+test_expect_success 'clone repository with http:// using protocol v2 with incomplete pktline body' '
+ test_when_finished "rm -f log" &&
+
+ git init "$HTTPD_DOCUMENT_ROOT_PATH/incomplete_body" &&
+ test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/incomplete_body" file &&
+
+ test_must_fail env GIT_TRACE_PACKET="$(pwd)/log" GIT_TRACE_CURL="$(pwd)/log" git -c protocol.version=2 \
+ clone "$HTTPD_URL/smart/incomplete_body" incomplete_body_child 2>err &&
+
+ # Client requested to use protocol v2
+ grep "Git-Protocol: version=2" log &&
+ # Server responded using protocol v2
+ grep "git< version 2" log &&
+ # Client reported appropriate failure
+ test_grep "bytes of body are still expected" err
+'
+
+test_expect_success 'clone with http:// using protocol v2 and invalid parameters' '
+ test_when_finished "rm -f log" &&
+
+ test_must_fail env GIT_TRACE_PACKET="$(pwd)/log" GIT_TRACE_CURL="$(pwd)/log" \
+ git -c protocol.version=2 \
+ clone --shallow-since=20151012 "$HTTPD_URL/smart/http_parent" http_child_invalid &&
+
+ # Client requested to use protocol v2
+ grep "Git-Protocol: version=2" log &&
+ # Server responded using protocol v2
+ grep "git< version 2" log
+'
+
+test_expect_success 'clone big repository with http:// using protocol v2' '
+ test_when_finished "rm -f log" &&
+
+ git init "$HTTPD_DOCUMENT_ROOT_PATH/big" &&
+ # Ensure that the list of wants is greater than http.postbuffer below
+ for i in $(test_seq 1 1500)
+ do
+ # do not use here-doc, because it requires a process
+ # per loop iteration
+ echo "commit refs/heads/too-many-refs-$i" &&
+ echo "committer git <git@example.com> $i +0000" &&
+ echo "data 0" &&
+ echo "M 644 inline bla.txt" &&
+ echo "data 4" &&
+ echo "bla" || return 1
+ done | git -C "$HTTPD_DOCUMENT_ROOT_PATH/big" fast-import &&
+
+ GIT_TRACE_PACKET="$(pwd)/log" GIT_TRACE_CURL="$(pwd)/log" git \
+ -c protocol.version=2 -c http.postbuffer=65536 \
+ clone "$HTTPD_URL/smart/big" big_child &&
+
+ # Client requested to use protocol v2
+ grep "Git-Protocol: version=2" log &&
+ # Server responded using protocol v2
+ grep "git< version 2" log &&
+ # Verify that the chunked encoding sending codepath is exercised
+ grep "Send header: Transfer-Encoding: chunked" log
+'
+
+test_expect_success 'fetch with http:// using protocol v2' '
+ test_when_finished "rm -f log" &&
+
+ test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" two &&
+
+ GIT_TRACE_PACKET="$(pwd)/log" git -C http_child -c protocol.version=2 \
+ fetch &&
+
+ git -C http_child log -1 --format=%s origin/main >actual &&
+ git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" log -1 --format=%s >expect &&
+ test_cmp expect actual &&
+
+ # Server responded using protocol v2
+ grep "git< version 2" log
+'
+
+test_expect_success 'fetch with http:// by hash without tag following with protocol v2 does not list refs' '
+ test_when_finished "rm -f log" &&
+
+ test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" two_a &&
+ git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" rev-parse two_a >two_a_hash &&
+
+ GIT_TRACE_PACKET="$(pwd)/log" git -C http_child -c protocol.version=2 \
+ fetch --no-tags origin $(cat two_a_hash) &&
+
+ grep "fetch< version 2" log &&
+ ! grep "fetch> command=ls-refs" log
+'
+
+test_expect_success 'fetch from namespaced repo respects namespaces' '
+ test_when_finished "rm -f log" &&
+
+ git init "$HTTPD_DOCUMENT_ROOT_PATH/nsrepo" &&
+ test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/nsrepo" one &&
+ test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/nsrepo" two &&
+ git -C "$HTTPD_DOCUMENT_ROOT_PATH/nsrepo" \
+ update-ref refs/namespaces/ns/refs/heads/main one &&
+
+ GIT_TRACE_PACKET="$(pwd)/log" git -C http_child -c protocol.version=2 \
+ fetch "$HTTPD_URL/smart_namespace/nsrepo" \
+ refs/heads/main:refs/heads/theirs &&
+
+ # Server responded using protocol v2
+ grep "fetch< version 2" log &&
+
+ git -C "$HTTPD_DOCUMENT_ROOT_PATH/nsrepo" rev-parse one >expect &&
+ git -C http_child rev-parse theirs >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'ls-remote with v2 http sends only one POST' '
+ test_when_finished "rm -f log" &&
+
+ git ls-remote "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" >expect &&
+ GIT_TRACE_CURL="$(pwd)/log" git -c protocol.version=2 \
+ ls-remote "$HTTPD_URL/smart/http_parent" >actual &&
+ test_cmp expect actual &&
+
+ grep "Send header: POST" log >posts &&
+ test_line_count = 1 posts
+'
+
+test_expect_success 'push with http:// and a config of v2 does not request v2' '
+ test_when_finished "rm -f log" &&
+ # Till v2 for push is designed, make sure that if a client has
+ # protocol.version configured to use v2, that the client instead falls
+ # back and uses v0.
+
+ test_commit -C http_child three &&
+
+ # Push to another branch, as the target repository has the
+ # main branch checked out and we cannot push into it.
+ GIT_TRACE_PACKET="$(pwd)/log" git -C http_child -c protocol.version=2 \
+ push origin HEAD:client_branch &&
+
+ git -C http_child log -1 --format=%s >actual &&
+ git -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" log -1 --format=%s client_branch >expect &&
+ test_cmp expect actual &&
+
+ # Client did not request to use protocol v2
+ ! grep "Git-Protocol: version=2" log &&
+ # Server did not respond using protocol v2
+ ! grep "git< version 2" log
+'
+
+test_expect_success 'when server sends "ready", expect DELIM' '
+ rm -rf "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" http_child &&
+
+ git init "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
+ test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" one &&
+
+ git clone "$HTTPD_URL/smart/http_parent" http_child &&
+
+ test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" two &&
+
+ # After "ready" in the acknowledgments section, pretend that a FLUSH
+ # (0000) was sent instead of a DELIM (0001).
+ printf "\$ready = 1 if /ready/; \$ready && s/0001/0000/" \
+ >"$HTTPD_ROOT_PATH/one-time-perl" &&
+
+ test_must_fail git -C http_child -c protocol.version=2 \
+ fetch "$HTTPD_URL/one_time_perl/http_parent" 2> err &&
+ test_grep "expected packfile to be sent after .ready." err
+'
+
+test_expect_success 'when server does not send "ready", expect FLUSH' '
+ rm -rf "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" http_child log &&
+
+ git init "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
+ test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" one &&
+
+ git clone "$HTTPD_URL/smart/http_parent" http_child &&
+
+ test_commit -C "$HTTPD_DOCUMENT_ROOT_PATH/http_parent" two &&
+
+ # Create many commits to extend the negotiation phase across multiple
+ # requests, so that the server does not send "ready" in the first
+ # request.
+ test_commit_bulk -C http_child --id=c 32 &&
+
+ # After the acknowledgments section, pretend that a DELIM
+ # (0001) was sent instead of a FLUSH (0000).
+ printf "\$ack = 1 if /acknowledgments/; \$ack && s/0000/0001/" \
+ >"$HTTPD_ROOT_PATH/one-time-perl" &&
+
+ test_must_fail env GIT_TRACE_PACKET="$(pwd)/log" git -C http_child \
+ -c protocol.version=2 \
+ fetch "$HTTPD_URL/one_time_perl/http_parent" 2> err &&
+ grep "fetch< .*acknowledgments" log &&
+ ! grep "fetch< .*ready" log &&
+ test_grep "expected no other sections to be sent after no .ready." err
+'
+
+configure_exclusion () {
+ git -C "$1" hash-object "$2" >objh &&
+ git -C "$1" pack-objects "$HTTPD_DOCUMENT_ROOT_PATH/mypack" <objh >packh &&
+ git -C "$1" config --add \
+ "uploadpack.blobpackfileuri" \
+ "$(cat objh) $(cat packh) $HTTPD_URL/dumb/mypack-$(cat packh).pack" &&
+ cat objh
+}
+
+test_expect_success 'part of packfile response provided as URI' '
+ P="$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
+ rm -rf "$P" http_child log &&
+
+ git init "$P" &&
+ git -C "$P" config "uploadpack.allowsidebandall" "true" &&
+
+ echo my-blob >"$P/my-blob" &&
+ git -C "$P" add my-blob &&
+ echo other-blob >"$P/other-blob" &&
+ git -C "$P" add other-blob &&
+ git -C "$P" commit -m x &&
+
+ configure_exclusion "$P" my-blob >h &&
+ configure_exclusion "$P" other-blob >h2 &&
+
+ GIT_TRACE=1 GIT_TRACE_PACKET="$(pwd)/log" GIT_TEST_SIDEBAND_ALL=1 \
+ git -c protocol.version=2 \
+ -c fetch.uriprotocols=http,https \
+ clone "$HTTPD_URL/smart/http_parent" http_child &&
+
+ # Ensure that my-blob and other-blob are in separate packfiles.
+ for idx in http_child/.git/objects/pack/*.idx
+ do
+ git verify-pack --object-format=$(test_oid algo) --verbose $idx >out &&
+ {
+ grep -E "^[0-9a-f]{16,} " out || :
+ } >out.objectlist &&
+ if test_line_count = 1 out.objectlist
+ then
+ if grep $(cat h) out
+ then
+ >hfound
+ fi &&
+ if grep $(cat h2) out
+ then
+ >h2found
+ fi
+ fi || return 1
+ done &&
+ test -f hfound &&
+ test -f h2found &&
+
+ # Ensure that there are exactly 3 packfiles with associated .idx
+ ls http_child/.git/objects/pack/*.pack \
+ http_child/.git/objects/pack/*.idx >filelist &&
+ test_line_count = 6 filelist
+'
+
+test_expect_success 'packfile URIs with fetch instead of clone' '
+ P="$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
+ rm -rf "$P" http_child log &&
+
+ git init "$P" &&
+ git -C "$P" config "uploadpack.allowsidebandall" "true" &&
+
+ echo my-blob >"$P/my-blob" &&
+ git -C "$P" add my-blob &&
+ git -C "$P" commit -m x &&
+
+ configure_exclusion "$P" my-blob >h &&
+
+ git init http_child &&
+
+ GIT_TEST_SIDEBAND_ALL=1 \
+ git -C http_child -c protocol.version=2 \
+ -c fetch.uriprotocols=http,https \
+ fetch "$HTTPD_URL/smart/http_parent"
+'
+
+test_expect_success 'fetching with valid packfile URI but invalid hash fails' '
+ P="$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
+ rm -rf "$P" http_child log &&
+
+ git init "$P" &&
+ git -C "$P" config "uploadpack.allowsidebandall" "true" &&
+
+ echo my-blob >"$P/my-blob" &&
+ git -C "$P" add my-blob &&
+ echo other-blob >"$P/other-blob" &&
+ git -C "$P" add other-blob &&
+ git -C "$P" commit -m x &&
+
+ configure_exclusion "$P" my-blob >h &&
+ # Configure a URL for other-blob. Just reuse the hash of the object as
+ # the hash of the packfile, since the hash does not matter for this
+ # test as long as it is not the hash of the pack, and it is of the
+ # expected length.
+ git -C "$P" hash-object other-blob >objh &&
+ git -C "$P" pack-objects "$HTTPD_DOCUMENT_ROOT_PATH/mypack" <objh >packh &&
+ git -C "$P" config --add \
+ "uploadpack.blobpackfileuri" \
+ "$(cat objh) $(cat objh) $HTTPD_URL/dumb/mypack-$(cat packh).pack" &&
+
+ test_must_fail env GIT_TEST_SIDEBAND_ALL=1 \
+ git -c protocol.version=2 \
+ -c fetch.uriprotocols=http,https \
+ clone "$HTTPD_URL/smart/http_parent" http_child 2>err &&
+ test_grep "pack downloaded from.*does not match expected hash" err
+'
+
+test_expect_success 'packfile-uri with transfer.fsckobjects' '
+ P="$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
+ rm -rf "$P" http_child log &&
+
+ git init "$P" &&
+ git -C "$P" config "uploadpack.allowsidebandall" "true" &&
+
+ echo my-blob >"$P/my-blob" &&
+ git -C "$P" add my-blob &&
+ git -C "$P" commit -m x &&
+
+ configure_exclusion "$P" my-blob >h &&
+
+ sane_unset GIT_TEST_SIDEBAND_ALL &&
+ git -c protocol.version=2 -c transfer.fsckobjects=1 \
+ -c fetch.uriprotocols=http,https \
+ clone "$HTTPD_URL/smart/http_parent" http_child &&
+
+ # Ensure that there are exactly 2 packfiles with associated .idx
+ ls http_child/.git/objects/pack/*.pack \
+ http_child/.git/objects/pack/*.idx >filelist &&
+ test_line_count = 4 filelist
+'
+
+test_expect_success 'packfile-uri with transfer.fsckobjects fails on bad object' '
+ P="$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
+ rm -rf "$P" http_child log &&
+
+ git init "$P" &&
+ git -C "$P" config "uploadpack.allowsidebandall" "true" &&
+
+ cat >bogus-commit <<-EOF &&
+ tree $EMPTY_TREE
+ author Bugs Bunny 1234567890 +0000
+ committer Bugs Bunny <bugs@bun.ni> 1234567890 +0000
+
+ This commit object intentionally broken
+ EOF
+ BOGUS=$(git -C "$P" hash-object -t commit -w --stdin --literally <bogus-commit) &&
+ git -C "$P" branch bogus-branch "$BOGUS" &&
+
+ echo my-blob >"$P/my-blob" &&
+ git -C "$P" add my-blob &&
+ git -C "$P" commit -m x &&
+
+ configure_exclusion "$P" my-blob >h &&
+
+ sane_unset GIT_TEST_SIDEBAND_ALL &&
+ test_must_fail git -c protocol.version=2 -c transfer.fsckobjects=1 \
+ -c fetch.uriprotocols=http,https \
+ clone "$HTTPD_URL/smart/http_parent" http_child 2>error &&
+ test_grep "invalid author/committer line - missing email" error
+'
+
+test_expect_success 'packfile-uri with transfer.fsckobjects succeeds when .gitmodules is separate from tree' '
+ P="$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
+ rm -rf "$P" http_child &&
+
+ git init "$P" &&
+ git -C "$P" config "uploadpack.allowsidebandall" "true" &&
+
+ echo "[submodule libfoo]" >"$P/.gitmodules" &&
+ echo "path = include/foo" >>"$P/.gitmodules" &&
+ echo "url = git://example.com/git/lib.git" >>"$P/.gitmodules" &&
+ git -C "$P" add .gitmodules &&
+ git -C "$P" commit -m x &&
+
+ configure_exclusion "$P" .gitmodules >h &&
+
+ sane_unset GIT_TEST_SIDEBAND_ALL &&
+ git -c protocol.version=2 -c transfer.fsckobjects=1 \
+ -c fetch.uriprotocols=http,https \
+ clone "$HTTPD_URL/smart/http_parent" http_child &&
+
+ # Ensure that there are exactly 2 packfiles with associated .idx
+ ls http_child/.git/objects/pack/*.pack \
+ http_child/.git/objects/pack/*.idx >filelist &&
+ test_line_count = 4 filelist
+'
+
+test_expect_success 'packfile-uri with transfer.fsckobjects fails when .gitmodules separate from tree is invalid' '
+ P="$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
+ rm -rf "$P" http_child err &&
+
+ git init "$P" &&
+ git -C "$P" config "uploadpack.allowsidebandall" "true" &&
+
+ echo "[submodule \"..\"]" >"$P/.gitmodules" &&
+ echo "path = include/foo" >>"$P/.gitmodules" &&
+ echo "url = git://example.com/git/lib.git" >>"$P/.gitmodules" &&
+ git -C "$P" add .gitmodules &&
+ git -C "$P" commit -m x &&
+
+ configure_exclusion "$P" .gitmodules >h &&
+
+ sane_unset GIT_TEST_SIDEBAND_ALL &&
+ test_must_fail git -c protocol.version=2 -c transfer.fsckobjects=1 \
+ -c fetch.uriprotocols=http,https \
+ clone "$HTTPD_URL/smart/http_parent" http_child 2>err &&
+ test_grep "disallowed submodule name" err
+'
+
+test_expect_success 'packfile-uri path redacted in trace' '
+ P="$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
+ rm -rf "$P" http_child log &&
+
+ git init "$P" &&
+ git -C "$P" config "uploadpack.allowsidebandall" "true" &&
+
+ echo my-blob >"$P/my-blob" &&
+ git -C "$P" add my-blob &&
+ git -C "$P" commit -m x &&
+
+ git -C "$P" hash-object my-blob >objh &&
+ git -C "$P" pack-objects "$HTTPD_DOCUMENT_ROOT_PATH/mypack" <objh >packh &&
+ git -C "$P" config --add \
+ "uploadpack.blobpackfileuri" \
+ "$(cat objh) $(cat packh) $HTTPD_URL/dumb/mypack-$(cat packh).pack" &&
+
+ GIT_TRACE_PACKET="$(pwd)/log" \
+ git -c protocol.version=2 \
+ -c fetch.uriprotocols=http,https \
+ clone "$HTTPD_URL/smart/http_parent" http_child &&
+
+ grep -F "clone< \\1$(cat packh) $HTTPD_URL/<redacted>" log
+'
+
+test_expect_success 'packfile-uri path not redacted in trace when GIT_TRACE_REDACT=0' '
+ P="$HTTPD_DOCUMENT_ROOT_PATH/http_parent" &&
+ rm -rf "$P" http_child log &&
+
+ git init "$P" &&
+ git -C "$P" config "uploadpack.allowsidebandall" "true" &&
+
+ echo my-blob >"$P/my-blob" &&
+ git -C "$P" add my-blob &&
+ git -C "$P" commit -m x &&
+
+ git -C "$P" hash-object my-blob >objh &&
+ git -C "$P" pack-objects "$HTTPD_DOCUMENT_ROOT_PATH/mypack" <objh >packh &&
+ git -C "$P" config --add \
+ "uploadpack.blobpackfileuri" \
+ "$(cat objh) $(cat packh) $HTTPD_URL/dumb/mypack-$(cat packh).pack" &&
+
+ GIT_TRACE_PACKET="$(pwd)/log" \
+ GIT_TRACE_REDACT=0 \
+ git -c protocol.version=2 \
+ -c fetch.uriprotocols=http,https \
+ clone "$HTTPD_URL/smart/http_parent" http_child &&
+
+ grep -F "clone< \\1$(cat packh) $HTTPD_URL/dumb/mypack-$(cat packh).pack" log
+'
+
+test_expect_success 'http:// --negotiate-only' '
+ SERVER="$HTTPD_DOCUMENT_ROOT_PATH/server" &&
+ URI="$HTTPD_URL/smart/server" &&
+
+ setup_negotiate_only "$SERVER" "$URI" &&
+
+ git -c protocol.version=2 -C client fetch \
+ --no-tags \
+ --negotiate-only \
+ --negotiation-tip=$(git -C client rev-parse HEAD) \
+ origin >out &&
+ COMMON=$(git -C "$SERVER" rev-parse two) &&
+ grep "$COMMON" out
+'
+
+test_expect_success 'http:// --negotiate-only without wait-for-done support' '
+ SERVER="server" &&
+ URI="$HTTPD_URL/one_time_perl/server" &&
+
+ setup_negotiate_only "$SERVER" "$URI" &&
+
+ echo "s/ wait-for-done/ xxxx-xxx-xxxx/" \
+ >"$HTTPD_ROOT_PATH/one-time-perl" &&
+
+ test_must_fail git -c protocol.version=2 -C client fetch \
+ --no-tags \
+ --negotiate-only \
+ --negotiation-tip=$(git -C client rev-parse HEAD) \
+ origin 2>err &&
+ test_grep "server does not support wait-for-done" err
+'
+
+test_expect_success 'http:// --negotiate-only with protocol v0' '
+ SERVER="$HTTPD_DOCUMENT_ROOT_PATH/server" &&
+ URI="$HTTPD_URL/smart/server" &&
+
+ setup_negotiate_only "$SERVER" "$URI" &&
+
+ test_must_fail git -c protocol.version=0 -C client fetch \
+ --no-tags \
+ --negotiate-only \
+ --negotiation-tip=$(git -C client rev-parse HEAD) \
+ origin 2>err &&
+ test_grep "negotiate-only requires protocol v2" err
+'
+
+# DO NOT add non-httpd-specific tests here, because the last part of this
+# test script is only executed when httpd is available and enabled.
+
+test_done
diff --git a/t/t5703-upload-pack-ref-in-want.sh b/t/t5703-upload-pack-ref-in-want.sh
new file mode 100755
index 0000000..1910971
--- /dev/null
+++ b/t/t5703-upload-pack-ref-in-want.sh
@@ -0,0 +1,539 @@
+#!/bin/sh
+
+test_description='upload-pack ref-in-want'
+
+. ./test-lib.sh
+
+get_actual_refs () {
+ sed -n -e '/wanted-refs/,/0001/{
+ /wanted-refs/d
+ /0001/d
+ p
+ }' <out | test-tool pkt-line unpack >actual_refs
+}
+
+get_actual_commits () {
+ test-tool pkt-line unpack-sideband <out >o.pack &&
+ git index-pack o.pack &&
+ git verify-pack -v o.idx >objs &&
+ sed -n -e 's/\([0-9a-f][0-9a-f]*\) commit .*/\1/p' objs >objs.sed &&
+ sort >actual_commits <objs.sed
+}
+
+check_output () {
+ get_actual_refs &&
+ test_cmp expected_refs actual_refs &&
+ get_actual_commits &&
+ sort expected_commits >sorted_commits &&
+ test_cmp sorted_commits actual_commits
+}
+
+write_command () {
+ echo "command=$1"
+
+ if test "$(test_oid algo)" != sha1
+ then
+ echo "object-format=$(test_oid algo)"
+ fi
+}
+
+# Write a complete fetch command to stdout, suitable for use with `test-tool
+# pkt-line`. "want-ref", "want", and "have" lines are read from stdin.
+#
+# Examples:
+#
+# write_fetch_command <<-EOF
+# want-ref refs/heads/main
+# have $(git rev-parse a)
+# EOF
+#
+# write_fetch_command <<-EOF
+# want $(git rev-parse b)
+# have $(git rev-parse a)
+# EOF
+#
+write_fetch_command () {
+ write_command fetch &&
+ echo "0001" &&
+ echo "no-progress" &&
+ cat &&
+ echo "done" &&
+ echo "0000"
+}
+
+# c(o/foo) d(o/bar)
+# \ /
+# b e(baz) f(main)
+# \__ | __/
+# \ | /
+# a
+test_expect_success 'setup repository' '
+ test_commit a &&
+ git branch -M main &&
+ git checkout -b o/foo &&
+ test_commit b &&
+ test_commit c &&
+ git checkout -b o/bar b &&
+ test_commit d &&
+ git checkout -b baz a &&
+ test_commit e &&
+ git checkout main &&
+ test_commit f
+'
+
+test_expect_success 'config controls ref-in-want advertisement' '
+ test-tool serve-v2 --advertise-capabilities >out &&
+ perl -ne "/ref-in-want/ and print" out >out.filter &&
+ test_must_be_empty out.filter &&
+
+ git config uploadpack.allowRefInWant false &&
+ test-tool serve-v2 --advertise-capabilities >out &&
+ perl -ne "/ref-in-want/ and print" out >out.filter &&
+ test_must_be_empty out.filter &&
+
+ git config uploadpack.allowRefInWant true &&
+ test-tool serve-v2 --advertise-capabilities >out &&
+ perl -ne "/ref-in-want/ and print" out >out.filter &&
+ test_file_not_empty out.filter
+'
+
+test_expect_success 'invalid want-ref line' '
+ write_fetch_command >pkt <<-EOF &&
+ want-ref refs/heads/non-existent
+ EOF
+
+ test-tool pkt-line pack <pkt >in &&
+ test_must_fail test-tool serve-v2 --stateless-rpc 2>out <in &&
+ grep "unknown ref" out
+'
+
+test_expect_success 'basic want-ref' '
+ oid=$(git rev-parse f) &&
+ cat >expected_refs <<-EOF &&
+ $oid refs/heads/main
+ EOF
+ git rev-parse f >expected_commits &&
+
+ write_fetch_command >pkt <<-EOF &&
+ want-ref refs/heads/main
+ have $(git rev-parse a)
+ EOF
+ test-tool pkt-line pack <pkt >in &&
+
+ test-tool serve-v2 --stateless-rpc >out <in &&
+ check_output
+'
+
+test_expect_success 'multiple want-ref lines' '
+ oid_c=$(git rev-parse c) &&
+ oid_d=$(git rev-parse d) &&
+ cat >expected_refs <<-EOF &&
+ $oid_c refs/heads/o/foo
+ $oid_d refs/heads/o/bar
+ EOF
+ git rev-parse c d >expected_commits &&
+
+ write_fetch_command >pkt <<-EOF &&
+ want-ref refs/heads/o/foo
+ want-ref refs/heads/o/bar
+ have $(git rev-parse b)
+ EOF
+ test-tool pkt-line pack <pkt >in &&
+
+ test-tool serve-v2 --stateless-rpc >out <in &&
+ check_output
+'
+
+test_expect_success 'mix want and want-ref' '
+ oid=$(git rev-parse f) &&
+ cat >expected_refs <<-EOF &&
+ $oid refs/heads/main
+ EOF
+ git rev-parse e f >expected_commits &&
+
+ write_fetch_command >pkt <<-EOF &&
+ want-ref refs/heads/main
+ want $(git rev-parse e)
+ have $(git rev-parse a)
+ EOF
+ test-tool pkt-line pack <pkt >in &&
+
+ test-tool serve-v2 --stateless-rpc >out <in &&
+ check_output
+'
+
+test_expect_success 'want-ref with ref we already have commit for' '
+ oid=$(git rev-parse c) &&
+ cat >expected_refs <<-EOF &&
+ $oid refs/heads/o/foo
+ EOF
+ >expected_commits &&
+
+ write_fetch_command >pkt <<-EOF &&
+ want-ref refs/heads/o/foo
+ have $(git rev-parse c)
+ EOF
+ test-tool pkt-line pack <pkt >in &&
+
+ test-tool serve-v2 --stateless-rpc >out <in &&
+ check_output
+'
+
+REPO="$(pwd)/repo"
+LOCAL_PRISTINE="$(pwd)/local_pristine"
+
+# $REPO
+# c(o/foo) d(o/bar)
+# \ /
+# b e(baz) f(main)
+# \__ | __/
+# \ | /
+# a
+#
+# $LOCAL_PRISTINE
+# s32(side)
+# |
+# .
+# .
+# |
+# a(main)
+test_expect_success 'setup repos for fetching with ref-in-want tests' '
+ (
+ git init -b main "$REPO" &&
+ cd "$REPO" &&
+ test_commit a &&
+
+ # Local repo with many commits (so that negotiation will take
+ # more than 1 request/response pair)
+ rm -rf "$LOCAL_PRISTINE" &&
+ git clone "file://$REPO" "$LOCAL_PRISTINE" &&
+ cd "$LOCAL_PRISTINE" &&
+ git checkout -b side &&
+ test_commit_bulk --id=s 33 &&
+
+ # Add novel commits to upstream
+ git checkout main &&
+ cd "$REPO" &&
+ git checkout -b o/foo &&
+ test_commit b &&
+ test_commit c &&
+ git checkout -b o/bar b &&
+ test_commit d &&
+ git checkout -b baz a &&
+ test_commit e &&
+ git checkout main &&
+ test_commit f
+ ) &&
+ git -C "$REPO" config uploadpack.allowRefInWant true &&
+ git -C "$LOCAL_PRISTINE" config protocol.version 2
+'
+
+test_expect_success 'fetching with exact OID' '
+ test_when_finished "rm -f log trace2" &&
+
+ rm -rf local &&
+ cp -r "$LOCAL_PRISTINE" local &&
+ oid=$(git -C "$REPO" rev-parse d) &&
+ GIT_TRACE_PACKET="$(pwd)/log" GIT_TRACE2_EVENT="$(pwd)/trace2" \
+ git -C local fetch origin \
+ "$oid":refs/heads/actual &&
+
+ grep \"key\":\"total_rounds\",\"value\":\"2\" trace2 &&
+ git -C "$REPO" rev-parse "d" >expected &&
+ git -C local rev-parse refs/heads/actual >actual &&
+ test_cmp expected actual &&
+ grep "want $oid" log
+'
+
+test_expect_success 'fetching multiple refs' '
+ test_when_finished "rm -f log" &&
+
+ rm -rf local &&
+ cp -r "$LOCAL_PRISTINE" local &&
+ GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin main baz &&
+
+ git -C "$REPO" rev-parse "main" "baz" >expected &&
+ git -C local rev-parse refs/remotes/origin/main refs/remotes/origin/baz >actual &&
+ test_cmp expected actual &&
+ grep "want-ref refs/heads/main" log &&
+ grep "want-ref refs/heads/baz" log
+'
+
+test_expect_success 'fetching ref and exact OID' '
+ test_when_finished "rm -f log" &&
+
+ rm -rf local &&
+ cp -r "$LOCAL_PRISTINE" local &&
+ oid=$(git -C "$REPO" rev-parse b) &&
+ GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin \
+ main "$oid":refs/heads/actual &&
+
+ git -C "$REPO" rev-parse "main" "b" >expected &&
+ git -C local rev-parse refs/remotes/origin/main refs/heads/actual >actual &&
+ test_cmp expected actual &&
+ grep "want $oid" log &&
+ grep "want-ref refs/heads/main" log
+'
+
+test_expect_success 'fetching with wildcard that does not match any refs' '
+ test_when_finished "rm -f log" &&
+
+ rm -rf local &&
+ cp -r "$LOCAL_PRISTINE" local &&
+ git -C local fetch origin refs/heads/none*:refs/heads/* >out &&
+ test_must_be_empty out
+'
+
+test_expect_success 'fetching with wildcard that matches multiple refs' '
+ test_when_finished "rm -f log" &&
+
+ rm -rf local &&
+ cp -r "$LOCAL_PRISTINE" local &&
+ GIT_TRACE_PACKET="$(pwd)/log" git -C local fetch origin refs/heads/o*:refs/heads/o* &&
+
+ git -C "$REPO" rev-parse "o/foo" "o/bar" >expected &&
+ git -C local rev-parse "o/foo" "o/bar" >actual &&
+ test_cmp expected actual &&
+ grep "want-ref refs/heads/o/foo" log &&
+ grep "want-ref refs/heads/o/bar" log
+'
+
+REPO="$(pwd)/repo-ns"
+
+test_expect_success 'setup namespaced repo' '
+ (
+ git init -b main "$REPO" &&
+ cd "$REPO" &&
+ test_commit a &&
+ test_commit b &&
+ git checkout a &&
+ test_commit c &&
+ git checkout a &&
+ test_commit d &&
+ git update-ref refs/heads/ns-no b &&
+ git update-ref refs/namespaces/ns/refs/heads/ns-yes c &&
+ git update-ref refs/namespaces/ns/refs/heads/hidden d
+ ) &&
+ git -C "$REPO" config uploadpack.allowRefInWant true
+'
+
+test_expect_success 'with namespace: want-ref is considered relative to namespace' '
+ wanted_ref=refs/heads/ns-yes &&
+
+ oid=$(git -C "$REPO" rev-parse "refs/namespaces/ns/$wanted_ref") &&
+ cat >expected_refs <<-EOF &&
+ $oid $wanted_ref
+ EOF
+ cat >expected_commits <<-EOF &&
+ $oid
+ $(git -C "$REPO" rev-parse a)
+ EOF
+
+ write_fetch_command >pkt <<-EOF &&
+ want-ref $wanted_ref
+ EOF
+ test-tool pkt-line pack <pkt >in &&
+
+ GIT_NAMESPACE=ns test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
+ check_output
+'
+
+test_expect_success 'with namespace: want-ref outside namespace is unknown' '
+ wanted_ref=refs/heads/ns-no &&
+
+ write_fetch_command >pkt <<-EOF &&
+ want-ref $wanted_ref
+ EOF
+ test-tool pkt-line pack <pkt >in &&
+
+ test_must_fail env GIT_NAMESPACE=ns \
+ test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
+ grep "unknown ref" out
+'
+
+# Cross-check refs/heads/ns-no indeed exists
+test_expect_success 'without namespace: want-ref outside namespace succeeds' '
+ wanted_ref=refs/heads/ns-no &&
+
+ oid=$(git -C "$REPO" rev-parse $wanted_ref) &&
+ cat >expected_refs <<-EOF &&
+ $oid $wanted_ref
+ EOF
+ cat >expected_commits <<-EOF &&
+ $oid
+ $(git -C "$REPO" rev-parse a)
+ EOF
+
+ write_fetch_command >pkt <<-EOF &&
+ want-ref $wanted_ref
+ EOF
+ test-tool pkt-line pack <pkt >in &&
+
+ test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
+ check_output
+'
+
+test_expect_success 'with namespace: hideRefs is matched, relative to namespace' '
+ wanted_ref=refs/heads/hidden &&
+ git -C "$REPO" config transfer.hideRefs $wanted_ref &&
+
+ write_fetch_command >pkt <<-EOF &&
+ want-ref $wanted_ref
+ EOF
+ test-tool pkt-line pack <pkt >in &&
+
+ test_must_fail env GIT_NAMESPACE=ns \
+ test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
+ grep "unknown ref" out
+'
+
+# Cross-check refs/heads/hidden indeed exists
+test_expect_success 'with namespace: want-ref succeeds if hideRefs is removed' '
+ wanted_ref=refs/heads/hidden &&
+ git -C "$REPO" config --unset transfer.hideRefs $wanted_ref &&
+
+ oid=$(git -C "$REPO" rev-parse "refs/namespaces/ns/$wanted_ref") &&
+ cat >expected_refs <<-EOF &&
+ $oid $wanted_ref
+ EOF
+ cat >expected_commits <<-EOF &&
+ $oid
+ $(git -C "$REPO" rev-parse a)
+ EOF
+
+ write_fetch_command >pkt <<-EOF &&
+ want-ref $wanted_ref
+ EOF
+ test-tool pkt-line pack <pkt >in &&
+
+ GIT_NAMESPACE=ns test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
+ check_output
+'
+
+test_expect_success 'without namespace: relative hideRefs does not match' '
+ wanted_ref=refs/namespaces/ns/refs/heads/hidden &&
+ git -C "$REPO" config transfer.hideRefs refs/heads/hidden &&
+
+ oid=$(git -C "$REPO" rev-parse $wanted_ref) &&
+ cat >expected_refs <<-EOF &&
+ $oid $wanted_ref
+ EOF
+ cat >expected_commits <<-EOF &&
+ $oid
+ $(git -C "$REPO" rev-parse a)
+ EOF
+
+ write_fetch_command >pkt <<-EOF &&
+ want-ref $wanted_ref
+ EOF
+ test-tool pkt-line pack <pkt >in &&
+
+ test-tool -C "$REPO" serve-v2 --stateless-rpc >out <in &&
+ check_output
+'
+
+
+. "$TEST_DIRECTORY"/lib-httpd.sh
+start_httpd
+
+REPO="$HTTPD_DOCUMENT_ROOT_PATH/repo"
+LOCAL_PRISTINE="$(pwd)/local_pristine"
+
+test_expect_success 'setup repos for change-while-negotiating test' '
+ (
+ git init -b main "$REPO" &&
+ cd "$REPO" &&
+ >.git/git-daemon-export-ok &&
+ test_commit m1 &&
+ git tag -d m1 &&
+
+ # Local repo with many commits (so that negotiation will take
+ # more than 1 request/response pair)
+ rm -rf "$LOCAL_PRISTINE" &&
+ git clone "http://127.0.0.1:$LIB_HTTPD_PORT/smart/repo" "$LOCAL_PRISTINE" &&
+ cd "$LOCAL_PRISTINE" &&
+ git checkout -b side &&
+ test_commit_bulk --id=s 33 &&
+
+ # Add novel commits to upstream
+ git checkout main &&
+ cd "$REPO" &&
+ test_commit m2 &&
+ test_commit m3 &&
+ git tag -d m2 m3
+ ) &&
+ git -C "$LOCAL_PRISTINE" remote set-url origin "http://127.0.0.1:$LIB_HTTPD_PORT/one_time_perl/repo" &&
+ git -C "$LOCAL_PRISTINE" config protocol.version 2
+'
+
+inconsistency () {
+ # Simulate that the server initially reports $2 as the ref
+ # corresponding to $1, and after that, $1 as the ref corresponding to
+ # $1. This corresponds to the real-life situation where the server's
+ # repository appears to change during negotiation, for example, when
+ # different servers in a load-balancing arrangement serve (stateless)
+ # RPCs during a single negotiation.
+ oid1=$(git -C "$REPO" rev-parse $1) &&
+ oid2=$(git -C "$REPO" rev-parse $2) &&
+ echo "s/$oid1/$oid2/" >"$HTTPD_ROOT_PATH/one-time-perl"
+}
+
+test_expect_success 'server is initially ahead - no ref in want' '
+ git -C "$REPO" config uploadpack.allowRefInWant false &&
+ rm -rf local &&
+ cp -r "$LOCAL_PRISTINE" local &&
+ inconsistency main $(test_oid numeric) &&
+ test_must_fail git -C local fetch 2>err &&
+ test_grep "fatal: remote error: upload-pack: not our ref" err
+'
+
+test_expect_success 'server is initially ahead - ref in want' '
+ git -C "$REPO" config uploadpack.allowRefInWant true &&
+ rm -rf local &&
+ cp -r "$LOCAL_PRISTINE" local &&
+ inconsistency main $(test_oid numeric) &&
+ git -C local fetch &&
+
+ git -C "$REPO" rev-parse --verify main >expected &&
+ git -C local rev-parse --verify refs/remotes/origin/main >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'server is initially behind - no ref in want' '
+ git -C "$REPO" config uploadpack.allowRefInWant false &&
+ rm -rf local &&
+ cp -r "$LOCAL_PRISTINE" local &&
+ inconsistency main "main^" &&
+ git -C local fetch &&
+
+ git -C "$REPO" rev-parse --verify "main^" >expected &&
+ git -C local rev-parse --verify refs/remotes/origin/main >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'server is initially behind - ref in want' '
+ git -C "$REPO" config uploadpack.allowRefInWant true &&
+ rm -rf local &&
+ cp -r "$LOCAL_PRISTINE" local &&
+ inconsistency main "main^" &&
+ git -C local fetch &&
+
+ git -C "$REPO" rev-parse --verify "main" >expected &&
+ git -C local rev-parse --verify refs/remotes/origin/main >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'server loses a ref - ref in want' '
+ git -C "$REPO" config uploadpack.allowRefInWant true &&
+ rm -rf local &&
+ cp -r "$LOCAL_PRISTINE" local &&
+ echo "s/main/rain/" >"$HTTPD_ROOT_PATH/one-time-perl" &&
+ test_must_fail git -C local fetch 2>err &&
+
+ test_grep "fatal: remote error: unknown ref refs/heads/rain" err
+'
+
+# DO NOT add non-httpd-specific tests here, because the last part of this
+# test script is only executed when httpd is available and enabled.
+
+test_done
diff --git a/t/t5704-protocol-violations.sh b/t/t5704-protocol-violations.sh
new file mode 100755
index 0000000..11be64f
--- /dev/null
+++ b/t/t5704-protocol-violations.sh
@@ -0,0 +1,52 @@
+#!/bin/sh
+
+test_description='Test responses to violations of the network protocol. In most
+of these cases it will generally be acceptable for one side to break off
+communications if the other side says something unexpected. We are mostly
+making sure that we do not segfault or otherwise behave badly.'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'extra delim packet in v2 ls-refs args' '
+ {
+ packetize command=ls-refs &&
+ packetize "object-format=$(test_oid algo)" &&
+ printf 0001 &&
+ # protocol expects 0000 flush here
+ printf 0001
+ } >input &&
+ test_must_fail env GIT_PROTOCOL=version=2 \
+ git upload-pack . <input 2>err &&
+ test_grep "expected flush after ls-refs arguments" err
+'
+
+test_expect_success 'extra delim packet in v2 fetch args' '
+ {
+ packetize command=fetch &&
+ packetize "object-format=$(test_oid algo)" &&
+ printf 0001 &&
+ # protocol expects 0000 flush here
+ printf 0001
+ } >input &&
+ test_must_fail env GIT_PROTOCOL=version=2 \
+ git upload-pack . <input 2>err &&
+ test_grep "expected flush after fetch arguments" err
+'
+
+test_expect_success 'bogus symref in v0 capabilities' '
+ test_commit foo &&
+ oid=$(git rev-parse HEAD) &&
+ dst=refs/heads/foo &&
+ {
+ printf "%s HEAD\0symref object-format=%s symref=HEAD:%s\n" \
+ "$oid" "$GIT_DEFAULT_HASH" "$dst" |
+ test-tool pkt-line pack-raw-stdin &&
+ printf "0000"
+ } >input &&
+ git ls-remote --symref --upload-pack="cat input; read junk;:" . >actual &&
+ printf "ref: %s\tHEAD\n%s\tHEAD\n" "$dst" "$oid" >expect &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t5705-session-id-in-capabilities.sh b/t/t5705-session-id-in-capabilities.sh
new file mode 100755
index 0000000..b8a722e
--- /dev/null
+++ b/t/t5705-session-id-in-capabilities.sh
@@ -0,0 +1,88 @@
+#!/bin/sh
+
+test_description='session ID in capabilities'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+REPO="$(pwd)/repo"
+LOCAL_PRISTINE="$(pwd)/local_pristine"
+
+test_expect_success 'setup repos for session ID capability tests' '
+ git init "$REPO" &&
+ test_commit -C "$REPO" a &&
+ git clone "file://$REPO" "$LOCAL_PRISTINE" &&
+ test_commit -C "$REPO" b
+'
+
+for PROTO in 0 1 2
+do
+ test_expect_success "session IDs not advertised by default (fetch v${PROTO})" '
+ test_when_finished "rm -rf local tr2-client-events tr2-server-events" &&
+ cp -r "$LOCAL_PRISTINE" local &&
+ GIT_TRACE2_EVENT="$(pwd)/tr2-client-events" \
+ git -c protocol.version=$PROTO -C local fetch \
+ --upload-pack "GIT_TRACE2_EVENT=\"$(pwd)/tr2-server-events\" git-upload-pack" \
+ origin &&
+ test -z "$(grep \"key\":\"server-sid\" tr2-client-events)" &&
+ test -z "$(grep \"key\":\"client-sid\" tr2-server-events)"
+ '
+
+ test_expect_success "session IDs not advertised by default (push v${PROTO})" '
+ test_when_finished "rm -rf local tr2-client-events tr2-server-events" &&
+ test_when_finished "git -C local push --delete origin new-branch" &&
+ cp -r "$LOCAL_PRISTINE" local &&
+ git -C local pull --no-rebase origin &&
+ GIT_TRACE2_EVENT="$(pwd)/tr2-client-events" \
+ git -c protocol.version=$PROTO -C local push \
+ --receive-pack "GIT_TRACE2_EVENT=\"$(pwd)/tr2-server-events\" git-receive-pack" \
+ origin HEAD:new-branch &&
+ test -z "$(grep \"key\":\"server-sid\" tr2-client-events)" &&
+ test -z "$(grep \"key\":\"client-sid\" tr2-server-events)"
+ '
+done
+
+test_expect_success 'enable SID advertisement' '
+ git -C "$REPO" config transfer.advertiseSID true &&
+ git -C "$LOCAL_PRISTINE" config transfer.advertiseSID true
+'
+
+for PROTO in 0 1 2
+do
+ test_expect_success "session IDs advertised (fetch v${PROTO})" '
+ test_when_finished "rm -rf local tr2-client-events tr2-server-events" &&
+ cp -r "$LOCAL_PRISTINE" local &&
+ GIT_TRACE2_EVENT="$(pwd)/tr2-client-events" \
+ git -c protocol.version=$PROTO -C local fetch \
+ --upload-pack "GIT_TRACE2_EVENT=\"$(pwd)/tr2-server-events\" git-upload-pack" \
+ origin &&
+ grep \"key\":\"server-sid\" tr2-client-events &&
+ grep \"key\":\"client-sid\" tr2-server-events
+ '
+
+ test_expect_success "session IDs advertised (push v${PROTO})" '
+ test_when_finished "rm -rf local tr2-client-events tr2-server-events" &&
+ test_when_finished "git -C local push --delete origin new-branch" &&
+ cp -r "$LOCAL_PRISTINE" local &&
+ git -C local pull --no-rebase origin &&
+ GIT_TRACE2_EVENT="$(pwd)/tr2-client-events" \
+ git -c protocol.version=$PROTO -C local push \
+ --receive-pack "GIT_TRACE2_EVENT=\"$(pwd)/tr2-server-events\" git-receive-pack" \
+ origin HEAD:new-branch &&
+ grep \"key\":\"server-sid\" tr2-client-events &&
+ grep \"key\":\"client-sid\" tr2-server-events
+ '
+
+ test_expect_success "client & server log negotiated version (v${PROTO})" '
+ test_when_finished "rm -rf local tr2-client-events tr2-server-events" &&
+ cp -r "$LOCAL_PRISTINE" local &&
+ GIT_TRACE2_EVENT="$(pwd)/tr2-client-events" \
+ git -c protocol.version=$PROTO -C local fetch \
+ --upload-pack "GIT_TRACE2_EVENT=\"$(pwd)/tr2-server-events\" git-upload-pack" \
+ origin &&
+ grep \"key\":\"negotiated-version\",\"value\":\"$PROTO\" tr2-client-events &&
+ grep \"key\":\"negotiated-version\",\"value\":\"$PROTO\" tr2-server-events
+ '
+done
+
+test_done
diff --git a/t/t5730-protocol-v2-bundle-uri-file.sh b/t/t5730-protocol-v2-bundle-uri-file.sh
new file mode 100755
index 0000000..37bdb72
--- /dev/null
+++ b/t/t5730-protocol-v2-bundle-uri-file.sh
@@ -0,0 +1,17 @@
+#!/bin/sh
+
+test_description="Test bundle-uri with protocol v2 and 'file://' transport"
+
+TEST_NO_CREATE_REPO=1
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+# Test protocol v2 with 'file://' transport
+#
+BUNDLE_URI_PROTOCOL=file
+. "$TEST_DIRECTORY"/lib-bundle-uri-protocol.sh
+
+test_done
diff --git a/t/t5731-protocol-v2-bundle-uri-git.sh b/t/t5731-protocol-v2-bundle-uri-git.sh
new file mode 100755
index 0000000..8add1b3
--- /dev/null
+++ b/t/t5731-protocol-v2-bundle-uri-git.sh
@@ -0,0 +1,17 @@
+#!/bin/sh
+
+test_description="Test bundle-uri with protocol v2 and 'git://' transport"
+
+TEST_NO_CREATE_REPO=1
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+# Test protocol v2 with 'git://' transport
+#
+BUNDLE_URI_PROTOCOL=git
+. "$TEST_DIRECTORY"/lib-bundle-uri-protocol.sh
+
+test_done
diff --git a/t/t5732-protocol-v2-bundle-uri-http.sh b/t/t5732-protocol-v2-bundle-uri-http.sh
new file mode 100755
index 0000000..129daa0
--- /dev/null
+++ b/t/t5732-protocol-v2-bundle-uri-http.sh
@@ -0,0 +1,17 @@
+#!/bin/sh
+
+test_description="Test bundle-uri with protocol v2 and 'http://' transport"
+
+TEST_NO_CREATE_REPO=1
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+# Test protocol v2 with 'http://' transport
+#
+BUNDLE_URI_PROTOCOL=http
+. "$TEST_DIRECTORY"/lib-bundle-uri-protocol.sh
+
+test_done
diff --git a/t/t5750-bundle-uri-parse.sh b/t/t5750-bundle-uri-parse.sh
new file mode 100755
index 0000000..81bdf58
--- /dev/null
+++ b/t/t5750-bundle-uri-parse.sh
@@ -0,0 +1,290 @@
+#!/bin/sh
+
+test_description="Test bundle-uri bundle_uri_parse_line()"
+
+TEST_NO_CREATE_REPO=1
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'bundle_uri_parse_line() just URIs' '
+ cat >in <<-\EOF &&
+ bundle.one.uri=http://example.com/bundle.bdl
+ bundle.two.uri=https://example.com/bundle.bdl
+ bundle.three.uri=file:///usr/share/git/bundle.bdl
+ EOF
+
+ cat >expect <<-\EOF &&
+ [bundle]
+ version = 1
+ mode = all
+ [bundle "one"]
+ uri = http://example.com/bundle.bdl
+ [bundle "two"]
+ uri = https://example.com/bundle.bdl
+ [bundle "three"]
+ uri = file:///usr/share/git/bundle.bdl
+ EOF
+
+ test-tool bundle-uri parse-key-values in >actual 2>err &&
+ test_must_be_empty err &&
+ test_cmp_config_output expect actual
+'
+
+test_expect_success 'bundle_uri_parse_line(): relative URIs' '
+ cat >in <<-\EOF &&
+ bundle.one.uri=bundle.bdl
+ bundle.two.uri=../bundle.bdl
+ bundle.three.uri=sub/dir/bundle.bdl
+ EOF
+
+ cat >expect <<-\EOF &&
+ [bundle]
+ version = 1
+ mode = all
+ [bundle "one"]
+ uri = <uri>/bundle.bdl
+ [bundle "two"]
+ uri = bundle.bdl
+ [bundle "three"]
+ uri = <uri>/sub/dir/bundle.bdl
+ EOF
+
+ test-tool bundle-uri parse-key-values in >actual 2>err &&
+ test_must_be_empty err &&
+ test_cmp_config_output expect actual
+'
+
+test_expect_success 'bundle_uri_parse_line(): relative URIs and parent paths' '
+ cat >in <<-\EOF &&
+ bundle.one.uri=bundle.bdl
+ bundle.two.uri=../bundle.bdl
+ bundle.three.uri=../../bundle.bdl
+ EOF
+
+ cat >expect <<-\EOF &&
+ [bundle]
+ version = 1
+ mode = all
+ [bundle "one"]
+ uri = <uri>/bundle.bdl
+ [bundle "two"]
+ uri = bundle.bdl
+ [bundle "three"]
+ uri = <uri>/../bundle.bdl
+ EOF
+
+ # TODO: We would prefer if parsing a bundle list would not cause
+ # a die() and instead would give a warning and allow the rest of
+ # a Git command to continue. This test_must_fail is necessary for
+ # now until the interface for relative_url() allows for reporting
+ # an error instead of die()ing.
+ test_must_fail test-tool bundle-uri parse-key-values in >actual 2>err &&
+ grep "fatal: cannot strip one component off url" err
+'
+
+test_expect_success 'bundle_uri_parse_line() parsing edge cases: empty key or value' '
+ cat >in <<-\EOF &&
+ =bogus-value
+ bogus-key=
+ EOF
+
+ cat >err.expect <<-EOF &&
+ error: bundle-uri: line has empty key or value
+ error: bad line: '\''=bogus-value'\''
+ error: bundle-uri: line has empty key or value
+ error: bad line: '\''bogus-key='\''
+ EOF
+
+ cat >expect <<-\EOF &&
+ [bundle]
+ version = 1
+ mode = all
+ EOF
+
+ test_must_fail test-tool bundle-uri parse-key-values in >actual 2>err &&
+ test_cmp err.expect err &&
+ test_cmp_config_output expect actual
+'
+
+test_expect_success 'bundle_uri_parse_line() parsing edge cases: empty lines' '
+ cat >in <<-\EOF &&
+ bundle.one.uri=http://example.com/bundle.bdl
+
+ bundle.two.uri=https://example.com/bundle.bdl
+
+ bundle.three.uri=file:///usr/share/git/bundle.bdl
+ EOF
+
+ cat >err.expect <<-\EOF &&
+ error: bundle-uri: got an empty line
+ error: bad line: '\'''\''
+ error: bundle-uri: got an empty line
+ error: bad line: '\'''\''
+ EOF
+
+ # We fail, but try to continue parsing regardless
+ cat >expect <<-\EOF &&
+ [bundle]
+ version = 1
+ mode = all
+ [bundle "one"]
+ uri = http://example.com/bundle.bdl
+ [bundle "two"]
+ uri = https://example.com/bundle.bdl
+ [bundle "three"]
+ uri = file:///usr/share/git/bundle.bdl
+ EOF
+
+ test_must_fail test-tool bundle-uri parse-key-values in >actual 2>err &&
+ test_cmp err.expect err &&
+ test_cmp_config_output expect actual
+'
+
+test_expect_success 'bundle_uri_parse_line() parsing edge cases: duplicate lines' '
+ cat >in <<-\EOF &&
+ bundle.one.uri=http://example.com/bundle.bdl
+ bundle.two.uri=https://example.com/bundle.bdl
+ bundle.one.uri=https://example.com/bundle-2.bdl
+ bundle.three.uri=file:///usr/share/git/bundle.bdl
+ EOF
+
+ cat >err.expect <<-\EOF &&
+ error: bad line: '\''bundle.one.uri=https://example.com/bundle-2.bdl'\''
+ EOF
+
+ # We fail, but try to continue parsing regardless
+ cat >expect <<-\EOF &&
+ [bundle]
+ version = 1
+ mode = all
+ [bundle "one"]
+ uri = http://example.com/bundle.bdl
+ [bundle "two"]
+ uri = https://example.com/bundle.bdl
+ [bundle "three"]
+ uri = file:///usr/share/git/bundle.bdl
+ EOF
+
+ test_must_fail test-tool bundle-uri parse-key-values in >actual 2>err &&
+ test_cmp err.expect err &&
+ test_cmp_config_output expect actual
+'
+
+test_expect_success 'parse config format: just URIs' '
+ cat >expect <<-\EOF &&
+ [bundle]
+ version = 1
+ mode = all
+ [bundle "one"]
+ uri = http://example.com/bundle.bdl
+ [bundle "two"]
+ uri = https://example.com/bundle.bdl
+ [bundle "three"]
+ uri = file:///usr/share/git/bundle.bdl
+ EOF
+
+ test-tool bundle-uri parse-config expect >actual 2>err &&
+ test_must_be_empty err &&
+ test_cmp_config_output expect actual
+'
+
+test_expect_success 'parse config format: relative URIs' '
+ cat >in <<-\EOF &&
+ [bundle]
+ version = 1
+ mode = all
+ [bundle "one"]
+ uri = bundle.bdl
+ [bundle "two"]
+ uri = ../bundle.bdl
+ [bundle "three"]
+ uri = sub/dir/bundle.bdl
+ EOF
+
+ cat >expect <<-\EOF &&
+ [bundle]
+ version = 1
+ mode = all
+ [bundle "one"]
+ uri = <uri>/bundle.bdl
+ [bundle "two"]
+ uri = bundle.bdl
+ [bundle "three"]
+ uri = <uri>/sub/dir/bundle.bdl
+ EOF
+
+ test-tool bundle-uri parse-config in >actual 2>err &&
+ test_must_be_empty err &&
+ test_cmp_config_output expect actual
+'
+
+test_expect_success 'parse config format edge cases: empty key or value' '
+ cat >in1 <<-\EOF &&
+ = bogus-value
+ EOF
+
+ cat >err1 <<-EOF &&
+ error: bad config line 1 in file in1
+ EOF
+
+ cat >expect <<-\EOF &&
+ [bundle]
+ version = 1
+ mode = all
+ EOF
+
+ test_must_fail test-tool bundle-uri parse-config in1 >actual 2>err &&
+ test_cmp err1 err &&
+ test_cmp_config_output expect actual &&
+
+ cat >in2 <<-\EOF &&
+ bogus-key =
+ EOF
+
+ cat >err2 <<-EOF &&
+ error: bad config line 1 in file in2
+ EOF
+
+ test_must_fail test-tool bundle-uri parse-config in2 >actual 2>err &&
+ test_cmp err2 err &&
+ test_cmp_config_output expect actual
+'
+
+test_expect_success 'parse config format: creationToken heuristic' '
+ cat >expect <<-\EOF &&
+ [bundle]
+ version = 1
+ mode = all
+ heuristic = creationToken
+ [bundle "one"]
+ uri = http://example.com/bundle.bdl
+ creationToken = 123456
+ [bundle "two"]
+ uri = https://example.com/bundle.bdl
+ creationToken = 12345678901234567890
+ [bundle "three"]
+ uri = file:///usr/share/git/bundle.bdl
+ creationToken = 1
+ EOF
+
+ test-tool bundle-uri parse-config expect >actual 2>err &&
+ test_must_be_empty err &&
+ test_cmp_config_output expect actual
+'
+
+test_expect_success 'parse config format edge cases: creationToken heuristic' '
+ cat >expect <<-\EOF &&
+ [bundle]
+ version = 1
+ mode = all
+ heuristic = creationToken
+ [bundle "one"]
+ uri = http://example.com/bundle.bdl
+ creationToken = bogus
+ EOF
+
+ test-tool bundle-uri parse-config expect >actual 2>err &&
+ grep "could not parse bundle list key creationToken with value '\''bogus'\''" err
+'
+
+test_done
diff --git a/t/t5801-remote-helpers.sh b/t/t5801-remote-helpers.sh
new file mode 100755
index 0000000..4e0a77f
--- /dev/null
+++ b/t/t5801-remote-helpers.sh
@@ -0,0 +1,324 @@
+#!/bin/sh
+#
+# Copyright (c) 2010 Sverre Rabbelier
+#
+
+test_description='Test remote-helper import and export commands'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-gpg.sh
+
+PATH="$TEST_DIRECTORY/t5801:$PATH"
+
+compare_refs() {
+ fail= &&
+ if test "x$1" = 'x!'
+ then
+ fail='!' &&
+ shift
+ fi &&
+ git --git-dir="$1/.git" rev-parse --verify $2 >expect &&
+ git --git-dir="$3/.git" rev-parse --verify $4 >actual &&
+ eval $fail test_cmp expect actual
+}
+
+test_expect_success 'setup repository' '
+ git init server &&
+ (cd server &&
+ echo content >file &&
+ git add file &&
+ git commit -m one)
+'
+
+test_expect_success 'cloning from local repo' '
+ git clone "testgit::${PWD}/server" local &&
+ test_cmp server/file local/file
+'
+
+test_expect_success 'create new commit on remote' '
+ (cd server &&
+ echo content >>file &&
+ git commit -a -m two)
+'
+
+test_expect_success 'pulling from local repo' '
+ (cd local && git pull) &&
+ test_cmp server/file local/file
+'
+
+test_expect_success 'pushing to local repo' '
+ (cd local &&
+ echo content >>file &&
+ git commit -a -m three &&
+ git push) &&
+ compare_refs local HEAD server HEAD
+'
+
+test_expect_success 'fetch new branch' '
+ (cd server &&
+ git reset --hard &&
+ git checkout -b new &&
+ echo content >>file &&
+ git commit -a -m five
+ ) &&
+ (cd local &&
+ git fetch origin new
+ ) &&
+ compare_refs server HEAD local FETCH_HEAD
+'
+
+test_expect_success 'fetch multiple branches' '
+ (cd local &&
+ git fetch
+ ) &&
+ compare_refs server main local refs/remotes/origin/main &&
+ compare_refs server new local refs/remotes/origin/new
+'
+
+test_expect_success 'push when remote has extra refs' '
+ (cd local &&
+ git reset --hard origin/main &&
+ echo content >>file &&
+ git commit -a -m six &&
+ git push
+ ) &&
+ compare_refs local main server main
+'
+
+test_expect_success 'push new branch by name' '
+ (cd local &&
+ git checkout -b new-name &&
+ echo content >>file &&
+ git commit -a -m seven &&
+ git push origin new-name
+ ) &&
+ compare_refs local HEAD server refs/heads/new-name
+'
+
+test_expect_success 'push new branch with old:new refspec' '
+ (cd local &&
+ git push origin new-name:new-refspec
+ ) &&
+ compare_refs local HEAD server refs/heads/new-refspec
+'
+
+test_expect_success 'push new branch with HEAD:new refspec' '
+ (cd local &&
+ git checkout new-name &&
+ git push origin HEAD:new-refspec-2
+ ) &&
+ compare_refs local HEAD server refs/heads/new-refspec-2
+'
+
+test_expect_success 'push delete branch' '
+ (cd local &&
+ git push origin :new-name
+ ) &&
+ test_must_fail git --git-dir="server/.git" \
+ rev-parse --verify refs/heads/new-name
+'
+
+test_expect_success 'forced push' '
+ (cd local &&
+ git checkout -b force-test &&
+ echo content >> file &&
+ git commit -a -m eight &&
+ git push origin force-test &&
+ echo content >> file &&
+ git commit -a --amend -m eight-modified &&
+ git push --force origin force-test
+ ) &&
+ compare_refs local refs/heads/force-test server refs/heads/force-test
+'
+
+test_expect_success 'cloning without refspec' '
+ GIT_REMOTE_TESTGIT_NOREFSPEC=1 \
+ git clone "testgit::${PWD}/server" local2 2>error &&
+ test_grep "this remote helper should implement refspec capability" error &&
+ compare_refs local2 HEAD server HEAD
+'
+
+test_expect_success 'pulling without refspecs' '
+ (cd local2 &&
+ git reset --hard &&
+ GIT_REMOTE_TESTGIT_NOREFSPEC=1 git pull 2>../error) &&
+ test_grep "this remote helper should implement refspec capability" error &&
+ compare_refs local2 HEAD server HEAD
+'
+
+test_expect_success 'pushing without refspecs' '
+ test_when_finished "(cd local2 && git reset --hard origin)" &&
+ (cd local2 &&
+ echo content >>file &&
+ git commit -a -m ten &&
+ GIT_REMOTE_TESTGIT_NOREFSPEC=1 &&
+ export GIT_REMOTE_TESTGIT_NOREFSPEC &&
+ test_must_fail git push 2>../error) &&
+ test_grep "remote-helper doesn.t support push; refspec needed" error
+'
+
+test_expect_success 'pulling without marks' '
+ (cd local2 &&
+ GIT_REMOTE_TESTGIT_NO_MARKS=1 git pull) &&
+ compare_refs local2 HEAD server HEAD
+'
+
+test_expect_failure 'pushing without marks' '
+ test_when_finished "(cd local2 && git reset --hard origin)" &&
+ (cd local2 &&
+ echo content >>file &&
+ git commit -a -m twelve &&
+ GIT_REMOTE_TESTGIT_NO_MARKS=1 git push) &&
+ compare_refs local2 HEAD server HEAD
+'
+
+test_expect_success 'push all with existing object' '
+ (cd local &&
+ git branch dup2 main &&
+ git push origin --all
+ ) &&
+ compare_refs local dup2 server dup2
+'
+
+test_expect_success 'push ref with existing object' '
+ (cd local &&
+ git branch dup main &&
+ git push origin dup
+ ) &&
+ compare_refs local dup server dup
+'
+
+test_expect_success GPG 'push signed tag' '
+ (cd local &&
+ git checkout main &&
+ git tag -s -m signed-tag signed-tag &&
+ git push origin signed-tag
+ ) &&
+ compare_refs local signed-tag^{} server signed-tag^{} &&
+ compare_refs ! local signed-tag server signed-tag
+'
+
+test_expect_success GPG 'push signed tag with signed-tags capability' '
+ (cd local &&
+ git checkout main &&
+ git tag -s -m signed-tag signed-tag-2 &&
+ GIT_REMOTE_TESTGIT_SIGNED_TAGS=1 git push origin signed-tag-2
+ ) &&
+ compare_refs local signed-tag-2 server signed-tag-2
+'
+
+test_expect_success 'push update refs' '
+ (cd local &&
+ git checkout -b update main &&
+ echo update >>file &&
+ git commit -a -m update &&
+ git push origin update &&
+ git rev-parse --verify remotes/origin/update >expect &&
+ git rev-parse --verify testgit/origin/heads/update >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'push update refs disabled by no-private-update' '
+ (cd local &&
+ echo more-update >>file &&
+ git commit -a -m more-update &&
+ git rev-parse --verify testgit/origin/heads/update >expect &&
+ GIT_REMOTE_TESTGIT_NO_PRIVATE_UPDATE=t git push origin update &&
+ git rev-parse --verify testgit/origin/heads/update >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'push update refs failure' '
+ (cd local &&
+ git checkout update &&
+ echo "update fail" >>file &&
+ git commit -a -m "update fail" &&
+ git rev-parse --verify testgit/origin/heads/update >expect &&
+ test_expect_code 1 env GIT_REMOTE_TESTGIT_FAILURE="non-fast forward" \
+ git push origin update &&
+ git rev-parse --verify testgit/origin/heads/update >actual &&
+ test_cmp expect actual
+ )
+'
+
+clean_mark () {
+ cut -f 2 -d ' ' "$1" |
+ git cat-file --batch-check |
+ grep commit |
+ sort >$(basename "$1")
+}
+
+test_expect_success 'proper failure checks for fetching' '
+ (cd local &&
+ test_must_fail env GIT_REMOTE_TESTGIT_FAILURE=1 git fetch 2>error &&
+ test_grep -q "error while running fast-import" error
+ )
+'
+
+test_expect_success 'proper failure checks for pushing' '
+ test_when_finished "rm -rf local/git.marks local/testgit.marks" &&
+ (cd local &&
+ git checkout -b crash main &&
+ echo crash >>file &&
+ git commit -a -m crash &&
+ test_must_fail env GIT_REMOTE_TESTGIT_FAILURE=1 git push --all &&
+ clean_mark ".git/testgit/origin/git.marks" &&
+ clean_mark ".git/testgit/origin/testgit.marks" &&
+ test_cmp git.marks testgit.marks
+ )
+'
+
+test_expect_success 'push messages' '
+ (cd local &&
+ git checkout -b new_branch main &&
+ echo new >>file &&
+ git commit -a -m new &&
+ git push origin new_branch &&
+ git fetch origin &&
+ echo new >>file &&
+ git commit -a -m new &&
+ git push origin new_branch 2> msg &&
+ ! grep "\[new branch\]" msg
+ )
+'
+
+test_expect_success 'fetch HEAD' '
+ (cd server &&
+ git checkout main &&
+ echo more >>file &&
+ git commit -a -m more
+ ) &&
+ (cd local &&
+ git fetch origin HEAD
+ ) &&
+ compare_refs server HEAD local FETCH_HEAD
+'
+
+test_expect_success 'fetch url' '
+ (cd server &&
+ git checkout main &&
+ echo more >>file &&
+ git commit -a -m more
+ ) &&
+ (cd local &&
+ git fetch "testgit::${PWD}/../server"
+ ) &&
+ compare_refs server HEAD local FETCH_HEAD
+'
+
+test_expect_success 'fetch tag' '
+ (cd server &&
+ git tag v1.0
+ ) &&
+ (cd local &&
+ git fetch
+ ) &&
+ compare_refs local v1.0 server v1.0
+'
+
+test_done
diff --git a/t/t5801/git-remote-testgit b/t/t5801/git-remote-testgit
new file mode 100755
index 0000000..1544d6d
--- /dev/null
+++ b/t/t5801/git-remote-testgit
@@ -0,0 +1,157 @@
+#!/bin/sh
+# Copyright (c) 2012 Felipe Contreras
+
+# The first argument can be a url when the fetch/push command was a url
+# instead of a configured remote. In this case, use a generic alias.
+if test "$1" = "testgit::$2"; then
+ alias=_
+else
+ alias=$1
+fi
+url=$2
+
+dir="$GIT_DIR/testgit/$alias"
+
+h_refspec="refs/heads/*:refs/testgit/$alias/heads/*"
+t_refspec="refs/tags/*:refs/testgit/$alias/tags/*"
+
+if test -n "$GIT_REMOTE_TESTGIT_NOREFSPEC"
+then
+ h_refspec=""
+ t_refspec=""
+fi
+
+GIT_DIR="$url/.git"
+export GIT_DIR
+
+force=
+
+mkdir -p "$dir"
+
+if test -z "$GIT_REMOTE_TESTGIT_NO_MARKS"
+then
+ gitmarks="$dir/git.marks"
+ testgitmarks="$dir/testgit.marks"
+ test -e "$gitmarks" || >"$gitmarks"
+ test -e "$testgitmarks" || >"$testgitmarks"
+fi
+
+while read line
+do
+ case $line in
+ capabilities)
+ echo 'import'
+ echo 'export'
+ test -n "$h_refspec" && echo "refspec $h_refspec"
+ test -n "$t_refspec" && echo "refspec $t_refspec"
+ if test -n "$gitmarks"
+ then
+ echo "*import-marks $gitmarks"
+ echo "*export-marks $gitmarks"
+ fi
+ test -n "$GIT_REMOTE_TESTGIT_SIGNED_TAGS" && echo "signed-tags"
+ test -n "$GIT_REMOTE_TESTGIT_NO_PRIVATE_UPDATE" && echo "no-private-update"
+ echo 'option'
+ echo 'object-format'
+ echo
+ ;;
+ list)
+ echo ":object-format $(git rev-parse --show-object-format=storage)"
+ git for-each-ref --format='? %(refname)' 'refs/heads/' 'refs/tags/'
+ head=$(git symbolic-ref HEAD)
+ echo "@$head HEAD"
+ echo
+ ;;
+ import*)
+ # read all import lines
+ while true
+ do
+ ref="${line#* }"
+ refs="$refs $ref"
+ read line
+ test "${line%% *}" != "import" && break
+ done
+
+ if test -n "$gitmarks"
+ then
+ echo "feature import-marks=$gitmarks"
+ echo "feature export-marks=$gitmarks"
+ fi
+
+ if test -n "$GIT_REMOTE_TESTGIT_FAILURE"
+ then
+ echo "feature done"
+ exit 1
+ fi
+
+ echo "feature done"
+ git fast-export \
+ ${h_refspec:+"--refspec=$h_refspec"} \
+ ${t_refspec:+"--refspec=$t_refspec"} \
+ ${testgitmarks:+"--import-marks=$testgitmarks"} \
+ ${testgitmarks:+"--export-marks=$testgitmarks"} \
+ $refs
+ echo "done"
+ ;;
+ export)
+ if test -n "$GIT_REMOTE_TESTGIT_FAILURE"
+ then
+ # consume input so fast-export doesn't get SIGPIPE;
+ # git would also notice that case, but we want
+ # to make sure we are exercising the later
+ # error checks
+ while read line; do
+ test "done" = "$line" && break
+ done
+ exit 1
+ fi
+
+ before=$(git for-each-ref --format=' %(refname) %(objectname) ')
+
+ git fast-import \
+ ${force:+--force} \
+ ${testgitmarks:+"--import-marks=$testgitmarks"} \
+ ${testgitmarks:+"--export-marks=$testgitmarks"} \
+ --quiet
+
+ # figure out which refs were updated
+ git for-each-ref --format='%(refname) %(objectname)' |
+ while read ref a
+ do
+ case "$before" in
+ *" $ref $a "*)
+ continue ;; # unchanged
+ esac
+ if test -z "$GIT_REMOTE_TESTGIT_PUSH_ERROR"
+ then
+ echo "ok $ref"
+ else
+ echo "error $ref $GIT_REMOTE_TESTGIT_PUSH_ERROR"
+ fi
+ done
+
+ echo
+ ;;
+ option\ *)
+ read cmd opt val <<-EOF
+ $line
+ EOF
+ case $opt in
+ force)
+ test $val = "true" && force="true" || force=
+ echo "ok"
+ ;;
+ object-format)
+ test $val = "true" && object_format="true" || object_format=
+ echo "ok"
+ ;;
+ *)
+ echo "unsupported"
+ ;;
+ esac
+ ;;
+ '')
+ exit
+ ;;
+ esac
+done
diff --git a/t/t5802-connect-helper.sh b/t/t5802-connect-helper.sh
new file mode 100755
index 0000000..c6c2661
--- /dev/null
+++ b/t/t5802-connect-helper.sh
@@ -0,0 +1,101 @@
+#!/bin/sh
+
+test_description='ext::cmd remote "connect" helper'
+. ./test-lib.sh
+
+test_expect_success setup '
+ git config --global protocol.ext.allow user &&
+ test_tick &&
+ git commit --allow-empty -m initial &&
+ test_tick &&
+ git commit --allow-empty -m second &&
+ test_tick &&
+ git commit --allow-empty -m third &&
+ test_tick &&
+ git tag -a -m "tip three" three &&
+
+ test_tick &&
+ git commit --allow-empty -m fourth
+'
+
+test_expect_success clone '
+ cmd=$(echo "echo >&2 ext::sh invoked && %S .." | sed -e "s/ /% /g") &&
+ git clone "ext::sh -c %S% ." dst &&
+ git for-each-ref refs/heads/ refs/tags/ >expect &&
+ (
+ cd dst &&
+ git config remote.origin.url "ext::sh -c $cmd" &&
+ git for-each-ref refs/heads/ refs/tags/
+ ) >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'update following tag' '
+ test_tick &&
+ git commit --allow-empty -m fifth &&
+ test_tick &&
+ git tag -a -m "tip five" five &&
+ git for-each-ref refs/heads/ refs/tags/ >expect &&
+ (
+ cd dst &&
+ git pull &&
+ git for-each-ref refs/heads/ refs/tags/ >../actual
+ ) &&
+ test_cmp expect actual
+'
+
+test_expect_success 'update backfilled tag' '
+ test_tick &&
+ git commit --allow-empty -m sixth &&
+ test_tick &&
+ git tag -a -m "tip two" two three^1 &&
+ git for-each-ref refs/heads/ refs/tags/ >expect &&
+ (
+ cd dst &&
+ git pull &&
+ git for-each-ref refs/heads/ refs/tags/ >../actual
+ ) &&
+ test_cmp expect actual
+'
+
+test_expect_success 'update backfilled tag without primary transfer' '
+ test_tick &&
+ git tag -a -m "tip one " one two^1 &&
+ git for-each-ref refs/heads/ refs/tags/ >expect &&
+ (
+ cd dst &&
+ git pull &&
+ git for-each-ref refs/heads/ refs/tags/ >../actual
+ ) &&
+ test_cmp expect actual
+'
+
+
+test_expect_success 'set up fake git-daemon' '
+ mkdir remote &&
+ git init --bare remote/one.git &&
+ mkdir remote/host &&
+ git init --bare remote/host/two.git &&
+ write_script fake-daemon <<-\EOF &&
+ git daemon --inetd \
+ --informative-errors \
+ --export-all \
+ --base-path="$TRASH_DIRECTORY/remote" \
+ --interpolated-path="$TRASH_DIRECTORY/remote/%H%D" \
+ "$TRASH_DIRECTORY/remote"
+ EOF
+ export TRASH_DIRECTORY &&
+ PATH=$TRASH_DIRECTORY:$PATH
+'
+
+test_expect_success 'ext command can connect to git daemon (no vhost)' '
+ rm -rf dst &&
+ git clone "ext::fake-daemon %G/one.git" dst
+'
+
+test_expect_success 'ext command can connect to git daemon (vhost)' '
+ rm -rf dst &&
+ git clone "ext::fake-daemon %G/two.git %Vhost" dst
+'
+
+test_done
diff --git a/t/t5810-proto-disable-local.sh b/t/t5810-proto-disable-local.sh
new file mode 100755
index 0000000..8626102
--- /dev/null
+++ b/t/t5810-proto-disable-local.sh
@@ -0,0 +1,39 @@
+#!/bin/sh
+
+test_description='test disabling of local paths in clone/fetch'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY/lib-proto-disable.sh"
+
+test_expect_success 'setup repository to clone' '
+ test_commit one
+'
+
+test_proto "file://" file "file://$PWD"
+test_proto "path" file .
+
+test_expect_success 'setup repo with dash' '
+ git init --bare repo.git &&
+ git push repo.git HEAD &&
+ mv repo.git "$PWD/-repo.git"
+'
+
+# This will fail even without our rejection because upload-pack will
+# complain about the bogus option. So let's make sure that GIT_TRACE
+# doesn't show us even running upload-pack.
+#
+# We must also be sure to use "fetch" and not "clone" here, as the latter
+# actually canonicalizes our input into an absolute path (which is fine
+# to allow).
+test_expect_success 'repo names starting with dash are rejected' '
+ rm -f trace.out &&
+ test_must_fail env GIT_TRACE="$PWD/trace.out" git fetch -- -repo.git &&
+ ! grep upload-pack trace.out
+'
+
+test_expect_success 'full paths still work' '
+ git fetch "$PWD/-repo.git"
+'
+
+test_done
diff --git a/t/t5811-proto-disable-git.sh b/t/t5811-proto-disable-git.sh
new file mode 100755
index 0000000..ed773e7
--- /dev/null
+++ b/t/t5811-proto-disable-git.sh
@@ -0,0 +1,22 @@
+#!/bin/sh
+
+test_description='test disabling of git-over-tcp in clone/fetch'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY/lib-proto-disable.sh"
+. "$TEST_DIRECTORY/lib-git-daemon.sh"
+start_git_daemon
+
+test_expect_success 'create git-accessible repo' '
+ bare="$GIT_DAEMON_DOCUMENT_ROOT_PATH/repo.git" &&
+ test_commit one &&
+ git --bare init "$bare" &&
+ git push "$bare" HEAD &&
+ >"$bare/git-daemon-export-ok" &&
+ git -C "$bare" config daemon.receivepack true
+'
+
+test_proto "git://" git "$GIT_DAEMON_URL/repo.git"
+
+test_done
diff --git a/t/t5812-proto-disable-http.sh b/t/t5812-proto-disable-http.sh
new file mode 100755
index 0000000..769c717
--- /dev/null
+++ b/t/t5812-proto-disable-http.sh
@@ -0,0 +1,37 @@
+#!/bin/sh
+
+test_description='test disabling of git-over-http in clone/fetch'
+. ./test-lib.sh
+. "$TEST_DIRECTORY/lib-proto-disable.sh"
+. "$TEST_DIRECTORY/lib-httpd.sh"
+start_httpd
+
+test_expect_success 'create git-accessible repo' '
+ bare="$HTTPD_DOCUMENT_ROOT_PATH/repo.git" &&
+ test_commit one &&
+ git --bare init "$bare" &&
+ git push "$bare" HEAD &&
+ git -C "$bare" config http.receivepack true
+'
+
+test_proto "smart http" http "$HTTPD_URL/smart/repo.git"
+
+test_expect_success 'http(s) transport respects GIT_ALLOW_PROTOCOL' '
+ test_must_fail env GIT_ALLOW_PROTOCOL=http:https \
+ GIT_SMART_HTTP=0 \
+ git clone "$HTTPD_URL/ftp-redir/repo.git" 2>stderr &&
+ test_grep -E "(ftp.*disabled|your curl version is too old)" stderr
+'
+
+test_expect_success 'curl limits redirects' '
+ test_must_fail git clone "$HTTPD_URL/loop-redir/smart/repo.git"
+'
+
+test_expect_success 'http can be limited to from-user' '
+ git -c protocol.http.allow=user \
+ clone "$HTTPD_URL/smart/repo.git" plain.git &&
+ test_must_fail git -c protocol.http.allow=user \
+ clone "$HTTPD_URL/smart-redir-perm/repo.git" redir.git
+'
+
+test_done
diff --git a/t/t5813-proto-disable-ssh.sh b/t/t5813-proto-disable-ssh.sh
new file mode 100755
index 0000000..2e975dc
--- /dev/null
+++ b/t/t5813-proto-disable-ssh.sh
@@ -0,0 +1,45 @@
+#!/bin/sh
+
+test_description='test disabling of git-over-ssh in clone/fetch'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY/lib-proto-disable.sh"
+
+setup_ssh_wrapper
+
+test_expect_success 'setup repository to clone' '
+ test_commit one &&
+ mkdir remote &&
+ git init --bare remote/repo.git &&
+ git push remote/repo.git HEAD
+'
+
+test_proto "host:path" ssh "remote:repo.git"
+test_proto "ssh://" ssh "ssh://remote$PWD/remote/repo.git"
+test_proto "git+ssh://" ssh "git+ssh://remote$PWD/remote/repo.git"
+
+# Don't even bother setting up a "-remote" directory, as ssh would generally
+# complain about the bogus option rather than completing our request. Our
+# fake wrapper actually _can_ handle this case, but it's more robust to
+# simply confirm from its output that it did not run at all.
+test_expect_success 'hostnames starting with dash are rejected' '
+ test_must_fail git clone ssh://-remote/repo.git dash-host 2>stderr &&
+ ! grep ^ssh: stderr
+'
+
+test_expect_success 'setup repo with dash' '
+ git init --bare remote/-repo.git &&
+ git push remote/-repo.git HEAD
+'
+
+test_expect_success 'repo names starting with dash are rejected' '
+ test_must_fail git clone remote:-repo.git dash-path 2>stderr &&
+ ! grep ^ssh: stderr
+'
+
+test_expect_success 'full paths still work' '
+ git clone "remote:$PWD/remote/-repo.git" dash-path
+'
+
+test_done
diff --git a/t/t5814-proto-disable-ext.sh b/t/t5814-proto-disable-ext.sh
new file mode 100755
index 0000000..9d6f7df
--- /dev/null
+++ b/t/t5814-proto-disable-ext.sh
@@ -0,0 +1,18 @@
+#!/bin/sh
+
+test_description='test disabling of remote-helper paths in clone/fetch'
+. ./test-lib.sh
+. "$TEST_DIRECTORY/lib-proto-disable.sh"
+
+setup_ext_wrapper
+
+test_expect_success 'setup repository to clone' '
+ test_commit one &&
+ mkdir remote &&
+ git init --bare remote/repo.git &&
+ git push remote/repo.git HEAD
+'
+
+test_proto "remote-helper" ext "ext::fake-remote %S repo.git"
+
+test_done
diff --git a/t/t5815-submodule-protos.sh b/t/t5815-submodule-protos.sh
new file mode 100755
index 0000000..4d5956c
--- /dev/null
+++ b/t/t5815-submodule-protos.sh
@@ -0,0 +1,43 @@
+#!/bin/sh
+
+test_description='test protocol filtering with submodules'
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-proto-disable.sh
+
+setup_ext_wrapper
+setup_ssh_wrapper
+
+test_expect_success 'setup repository with submodules' '
+ mkdir remote &&
+ git init remote/repo.git &&
+ (cd remote/repo.git && test_commit one) &&
+ # submodule-add should probably trust what we feed it on the cmdline,
+ # but its implementation is overly conservative.
+ GIT_ALLOW_PROTOCOL=ssh git submodule add remote:repo.git ssh-module &&
+ GIT_ALLOW_PROTOCOL=ext git submodule add "ext::fake-remote %S repo.git" ext-module &&
+ git commit -m "add submodules"
+'
+
+test_expect_success 'clone with recurse-submodules fails' '
+ test_must_fail git clone --recurse-submodules . dst
+'
+
+test_expect_success 'setup individual updates' '
+ rm -rf dst &&
+ git clone . dst &&
+ git -C dst submodule init
+'
+
+test_expect_success 'update of ssh allowed' '
+ git -C dst submodule update ssh-module
+'
+
+test_expect_success 'update of ext not allowed' '
+ test_must_fail git -C dst submodule update ext-module
+'
+
+test_expect_success 'user can filter protocols with GIT_ALLOW_PROTOCOL' '
+ GIT_ALLOW_PROTOCOL=ext git -C dst submodule update ext-module
+'
+
+test_done
diff --git a/t/t5900-repo-selection.sh b/t/t5900-repo-selection.sh
new file mode 100755
index 0000000..a84faac
--- /dev/null
+++ b/t/t5900-repo-selection.sh
@@ -0,0 +1,102 @@
+#!/bin/sh
+
+test_description='selecting remote repo in ambiguous cases'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+reset() {
+ rm -rf foo foo.git fetch clone
+}
+
+make_tree() {
+ git init "$1" &&
+ (cd "$1" && test_commit "$1")
+}
+
+make_bare() {
+ git init --bare "$1" &&
+ (cd "$1" &&
+ tree=$(git hash-object -w -t tree /dev/null) &&
+ commit=$(echo "$1" | git commit-tree $tree) &&
+ git update-ref HEAD $commit
+ )
+}
+
+get() {
+ git init --bare fetch &&
+ (cd fetch && git fetch "../$1") &&
+ git clone "$1" clone
+}
+
+check() {
+ echo "$1" >expect &&
+ (cd fetch && git log -1 --format=%s FETCH_HEAD) >actual.fetch &&
+ (cd clone && git log -1 --format=%s HEAD) >actual.clone &&
+ test_cmp expect actual.fetch &&
+ test_cmp expect actual.clone
+}
+
+test_expect_success 'find .git dir in worktree' '
+ reset &&
+ make_tree foo &&
+ get foo &&
+ check foo
+'
+
+test_expect_success 'automagically add .git suffix' '
+ reset &&
+ make_bare foo.git &&
+ get foo &&
+ check foo.git
+'
+
+test_expect_success 'automagically add .git suffix to worktree' '
+ reset &&
+ make_tree foo.git &&
+ get foo &&
+ check foo.git
+'
+
+test_expect_success 'prefer worktree foo over bare foo.git' '
+ reset &&
+ make_tree foo &&
+ make_bare foo.git &&
+ get foo &&
+ check foo
+'
+
+test_expect_success 'prefer bare foo over bare foo.git' '
+ reset &&
+ make_bare foo &&
+ make_bare foo.git &&
+ get foo &&
+ check foo
+'
+
+test_expect_success 'disambiguate with full foo.git' '
+ reset &&
+ make_bare foo &&
+ make_bare foo.git &&
+ get foo.git &&
+ check foo.git
+'
+
+test_expect_success 'we are not fooled by non-git foo directory' '
+ reset &&
+ make_bare foo.git &&
+ mkdir foo &&
+ get foo &&
+ check foo.git
+'
+
+test_expect_success 'prefer inner .git over outer bare' '
+ reset &&
+ make_tree foo &&
+ make_bare foo.git &&
+ mv foo/.git foo.git &&
+ get foo.git &&
+ check foo
+'
+
+test_done
diff --git a/t/t6000-rev-list-misc.sh b/t/t6000-rev-list-misc.sh
new file mode 100755
index 0000000..6289a2e
--- /dev/null
+++ b/t/t6000-rev-list-misc.sh
@@ -0,0 +1,185 @@
+#!/bin/sh
+
+test_description='miscellaneous rev-list tests'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success setup '
+ echo content1 >wanted_file &&
+ echo content2 >unwanted_file &&
+ git add wanted_file unwanted_file &&
+ test_tick &&
+ git commit -m one
+'
+
+test_expect_success 'rev-list --objects heeds pathspecs' '
+ git rev-list --objects HEAD -- wanted_file >output &&
+ grep wanted_file output &&
+ ! grep unwanted_file output
+'
+
+test_expect_success 'rev-list --objects with pathspecs and deeper paths' '
+ mkdir foo &&
+ >foo/file &&
+ git add foo/file &&
+ test_tick &&
+ git commit -m two &&
+
+ git rev-list --objects HEAD -- foo >output &&
+ grep foo/file output &&
+
+ git rev-list --objects HEAD -- foo/file >output &&
+ grep foo/file output &&
+ ! grep unwanted_file output
+'
+
+test_expect_success 'rev-list --objects with pathspecs and copied files' '
+ git checkout --orphan junio-testcase &&
+ git rm -rf . &&
+
+ mkdir two &&
+ echo frotz >one &&
+ cp one two/three &&
+ git add one two/three &&
+ test_tick &&
+ git commit -m that &&
+
+ ONE=$(git rev-parse HEAD:one) &&
+ git rev-list --objects HEAD two >output &&
+ grep "$ONE two/three" output &&
+ ! grep one output
+'
+
+test_expect_success 'rev-list --objects --no-object-names has no space/names' '
+ git rev-list --objects --no-object-names HEAD >output &&
+ ! grep wanted_file output &&
+ ! grep unwanted_file output &&
+ ! grep " " output
+'
+
+test_expect_success 'rev-list --objects --no-object-names works with cat-file' '
+ git rev-list --objects --no-object-names --all >list-output &&
+ git cat-file --batch-check <list-output >cat-output &&
+ ! grep missing cat-output
+'
+
+test_expect_success '--no-object-names and --object-names are last-one-wins' '
+ git rev-list --objects --no-object-names --object-names --all >output &&
+ grep wanted_file output &&
+ git rev-list --objects --object-names --no-object-names --all >output &&
+ ! grep wanted_file output
+'
+
+test_expect_success 'rev-list A..B and rev-list ^A B are the same' '
+ test_tick &&
+ git commit --allow-empty -m another &&
+ git tag -a -m "annotated" v1.0 &&
+ git rev-list --objects ^v1.0^ v1.0 >expect &&
+ git rev-list --objects v1.0^..v1.0 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'propagate uninteresting flag down correctly' '
+ git rev-list --objects ^HEAD^{tree} HEAD^{tree} >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'symleft flag bit is propagated down from tag' '
+ git log --format="%m %s" --left-right v1.0...main >actual &&
+ cat >expect <<-\EOF &&
+ < another
+ < that
+ > two
+ > one
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'rev-list can show index objects' '
+ # Of the blobs and trees in the index, note:
+ #
+ # - we do not show two/three, because it is the
+ # same blob as "one", and we show objects only once
+ #
+ # - we do show the tree "two", because it has a valid cache tree
+ # from the last commit
+ #
+ # - we do not show the root tree; since we updated the index, it
+ # does not have a valid cache tree
+ #
+ echo only-in-index >only-in-index &&
+ test_when_finished "git reset --hard" &&
+ rev1=$(git rev-parse HEAD:one) &&
+ rev2=$(git rev-parse HEAD:two) &&
+ revi=$(git hash-object only-in-index) &&
+ cat >expect <<-EOF &&
+ $rev1 one
+ $revi only-in-index
+ $rev2 two
+ EOF
+ git add only-in-index &&
+ git rev-list --objects --indexed-objects >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rev-list can negate index objects' '
+ git rev-parse HEAD >expect &&
+ git rev-list -1 --objects HEAD --not --indexed-objects >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--bisect and --first-parent can be combined' '
+ git rev-list --bisect --first-parent HEAD
+'
+
+test_expect_success '--header shows a NUL after each commit' '
+ # We know that there is no Q in the true payload; names and
+ # addresses of the authors and the committers do not have
+ # any, and object names or header names do not, either.
+ git rev-list --header --max-count=2 HEAD |
+ nul_to_q |
+ grep "^Q" >actual &&
+ cat >expect <<-EOF &&
+ Q$(git rev-parse HEAD~1)
+ Q
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'rev-list --end-of-options' '
+ git update-ref refs/heads/--output=yikes HEAD &&
+ git rev-list --end-of-options --output=yikes >actual &&
+ test_path_is_missing yikes &&
+ git rev-list HEAD >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rev-list --count' '
+ count=$(git rev-list --count HEAD) &&
+ git rev-list HEAD >actual &&
+ test_line_count = $count actual
+'
+
+test_expect_success 'rev-list --count --objects' '
+ count=$(git rev-list --count --objects HEAD) &&
+ git rev-list --objects HEAD >actual &&
+ test_line_count = $count actual
+'
+
+test_expect_success 'rev-list --unpacked' '
+ git repack -ad &&
+ test_commit unpacked &&
+
+ git rev-list --objects --no-object-names unpacked^.. >expect.raw &&
+ sort expect.raw >expect &&
+
+ git rev-list --all --objects --unpacked --no-object-names >actual.raw &&
+ sort actual.raw >actual &&
+
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t6001-rev-list-graft.sh b/t/t6001-rev-list-graft.sh
new file mode 100755
index 0000000..73a2465
--- /dev/null
+++ b/t/t6001-rev-list-graft.sh
@@ -0,0 +1,127 @@
+#!/bin/sh
+
+test_description='Revision traversal vs grafts and path limiter'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success setup '
+ mkdir subdir &&
+ echo >fileA fileA &&
+ echo >subdir/fileB fileB &&
+ git add fileA subdir/fileB &&
+ git commit -a -m "Initial in one history." &&
+ A0=$(git rev-parse --verify HEAD) &&
+
+ echo >fileA fileA modified &&
+ git commit -a -m "Second in one history." &&
+ A1=$(git rev-parse --verify HEAD) &&
+
+ echo >subdir/fileB fileB modified &&
+ git commit -a -m "Third in one history." &&
+ A2=$(git rev-parse --verify HEAD) &&
+
+ git update-ref -d refs/heads/main &&
+ rm -f .git/index &&
+
+ echo >fileA fileA again &&
+ echo >subdir/fileB fileB again &&
+ git add fileA subdir/fileB &&
+ git commit -a -m "Initial in alternate history." &&
+ B0=$(git rev-parse --verify HEAD) &&
+
+ echo >fileA fileA modified in alternate history &&
+ git commit -a -m "Second in alternate history." &&
+ B1=$(git rev-parse --verify HEAD) &&
+
+ echo >subdir/fileB fileB modified in alternate history &&
+ git commit -a -m "Third in alternate history." &&
+ B2=$(git rev-parse --verify HEAD) &&
+ : done
+'
+
+check () {
+ type=$1
+ shift
+
+ arg=
+ which=arg
+ rm -f test.expect
+ for a
+ do
+ if test "z$a" = z--
+ then
+ which=expect
+ child=
+ continue
+ fi
+ if test "$which" = arg
+ then
+ arg="$arg$a "
+ continue
+ fi
+ if test "$type" = basic
+ then
+ echo "$a"
+ else
+ if test "z$child" != z
+ then
+ echo "$child $a"
+ fi
+ child="$a"
+ fi
+ done >test.expect
+ if test "$type" != basic && test "z$child" != z
+ then
+ echo >>test.expect $child
+ fi
+ if test $type = basic
+ then
+ git rev-list $arg >test.actual
+ elif test $type = parents
+ then
+ git rev-list --parents $arg >test.actual
+ elif test $type = parents-raw
+ then
+ git rev-list --parents --pretty=raw $arg |
+ sed -n -e 's/^commit //p' >test.actual
+ fi
+ test_cmp test.expect test.actual
+}
+
+for type in basic parents parents-raw
+do
+ test_expect_success 'without grafts' "
+ rm -f .git/info/grafts &&
+ check $type $B2 -- $B2 $B1 $B0
+ "
+
+ test_expect_success 'with grafts' "
+ mkdir -p .git/info &&
+ echo '$B0 $A2' >.git/info/grafts &&
+ check $type $B2 -- $B2 $B1 $B0 $A2 $A1 $A0
+ "
+
+ test_expect_success 'without grafts, with pathlimit' "
+ rm -f .git/info/grafts &&
+ check $type $B2 subdir -- $B2 $B0
+ "
+
+ test_expect_success 'with grafts, with pathlimit' "
+ echo '$B0 $A2' >.git/info/grafts &&
+ check $type $B2 subdir -- $B2 $B0 $A2 $A0
+ "
+
+done
+
+test_expect_success 'show advice that grafts are deprecated' '
+ git show HEAD 2>err &&
+ test_grep "git replace" err &&
+ test_config advice.graftFileDeprecated false &&
+ git show HEAD 2>err &&
+ test_grep ! "git replace" err
+'
+
+test_done
diff --git a/t/t6002-rev-list-bisect.sh b/t/t6002-rev-list-bisect.sh
new file mode 100755
index 0000000..162cf50
--- /dev/null
+++ b/t/t6002-rev-list-bisect.sh
@@ -0,0 +1,312 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Jon Seymour
+#
+test_description='Tests git rev-list --bisect functionality'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-t6000.sh # t6xxx specific functions
+
+# usage: test_bisection max-diff bisect-option head ^prune...
+#
+# e.g. test_bisection 1 --bisect l1 ^l0
+#
+test_bisection_diff()
+{
+ _max_diff=$1
+ _bisect_option=$2
+ shift 2
+ _bisection=$(git rev-list $_bisect_option "$@")
+ _list_size=$(git rev-list "$@" | wc -l)
+ _head=$1
+ shift 1
+ _bisection_size=$(git rev-list $_bisection "$@" | wc -l)
+ [ -n "$_list_size" -a -n "$_bisection_size" ] ||
+ error "test_bisection_diff failed"
+
+ # Test if bisection size is close to half of list size within
+ # tolerance.
+ #
+ _bisect_err=$(expr $_list_size - $_bisection_size \* 2)
+ test "$_bisect_err" -lt 0 && _bisect_err=$(expr 0 - $_bisect_err)
+ _bisect_err=$(expr $_bisect_err / 2) ; # floor
+
+ test_expect_success \
+ "bisection diff $_bisect_option $_head $* <= $_max_diff" \
+ 'test $_bisect_err -le $_max_diff'
+}
+
+date >path0
+git update-index --add path0
+save_tag tree git write-tree
+on_committer_date "00:00" hide_error save_tag root unique_commit root tree
+on_committer_date "00:01" save_tag l0 unique_commit l0 tree -p root
+on_committer_date "00:02" save_tag l1 unique_commit l1 tree -p l0
+on_committer_date "00:03" save_tag l2 unique_commit l2 tree -p l1
+on_committer_date "00:04" save_tag a0 unique_commit a0 tree -p l2
+on_committer_date "00:05" save_tag a1 unique_commit a1 tree -p a0
+on_committer_date "00:06" save_tag b1 unique_commit b1 tree -p a0
+on_committer_date "00:07" save_tag c1 unique_commit c1 tree -p b1
+on_committer_date "00:08" save_tag b2 unique_commit b2 tree -p b1
+on_committer_date "00:09" save_tag b3 unique_commit b2 tree -p b2
+on_committer_date "00:10" save_tag c2 unique_commit c2 tree -p c1 -p b2
+on_committer_date "00:11" save_tag c3 unique_commit c3 tree -p c2
+on_committer_date "00:12" save_tag a2 unique_commit a2 tree -p a1
+on_committer_date "00:13" save_tag a3 unique_commit a3 tree -p a2
+on_committer_date "00:14" save_tag b4 unique_commit b4 tree -p b3 -p a3
+on_committer_date "00:15" save_tag a4 unique_commit a4 tree -p a3 -p b4 -p c3
+on_committer_date "00:16" save_tag l3 unique_commit l3 tree -p a4
+on_committer_date "00:17" save_tag l4 unique_commit l4 tree -p l3
+on_committer_date "00:18" save_tag l5 unique_commit l5 tree -p l4
+git update-ref HEAD $(tag l5)
+
+
+# E
+# / \
+# e1 |
+# | |
+# e2 |
+# | |
+# e3 |
+# | |
+# e4 |
+# | |
+# | f1
+# | |
+# | f2
+# | |
+# | f3
+# | |
+# | f4
+# | |
+# e5 |
+# | |
+# e6 |
+# | |
+# e7 |
+# | |
+# e8 |
+# \ /
+# F
+
+
+on_committer_date "00:00" hide_error save_tag F unique_commit F tree
+on_committer_date "00:01" save_tag e8 unique_commit e8 tree -p F
+on_committer_date "00:02" save_tag e7 unique_commit e7 tree -p e8
+on_committer_date "00:03" save_tag e6 unique_commit e6 tree -p e7
+on_committer_date "00:04" save_tag e5 unique_commit e5 tree -p e6
+on_committer_date "00:05" save_tag f4 unique_commit f4 tree -p F
+on_committer_date "00:06" save_tag f3 unique_commit f3 tree -p f4
+on_committer_date "00:07" save_tag f2 unique_commit f2 tree -p f3
+on_committer_date "00:08" save_tag f1 unique_commit f1 tree -p f2
+on_committer_date "00:09" save_tag e4 unique_commit e4 tree -p e5
+on_committer_date "00:10" save_tag e3 unique_commit e3 tree -p e4
+on_committer_date "00:11" save_tag e2 unique_commit e2 tree -p e3
+on_committer_date "00:12" save_tag e1 unique_commit e1 tree -p e2
+on_committer_date "00:13" save_tag E unique_commit E tree -p e1 -p f1
+
+on_committer_date "00:00" hide_error save_tag U unique_commit U tree
+on_committer_date "00:01" save_tag u0 unique_commit u0 tree -p U
+on_committer_date "00:01" save_tag u1 unique_commit u1 tree -p u0
+on_committer_date "00:02" save_tag u2 unique_commit u2 tree -p u0
+on_committer_date "00:03" save_tag u3 unique_commit u3 tree -p u0
+on_committer_date "00:04" save_tag u4 unique_commit u4 tree -p u0
+on_committer_date "00:05" save_tag u5 unique_commit u5 tree -p u0
+on_committer_date "00:06" save_tag V unique_commit V tree -p u1 -p u2 -p u3 -p u4 -p u5
+
+test_sequence()
+{
+ _bisect_option=$1
+
+ test_bisection_diff 0 $_bisect_option l0 ^root
+ test_bisection_diff 0 $_bisect_option l1 ^root
+ test_bisection_diff 0 $_bisect_option l2 ^root
+ test_bisection_diff 0 $_bisect_option a0 ^root
+ test_bisection_diff 0 $_bisect_option a1 ^root
+ test_bisection_diff 0 $_bisect_option a2 ^root
+ test_bisection_diff 0 $_bisect_option a3 ^root
+ test_bisection_diff 0 $_bisect_option b1 ^root
+ test_bisection_diff 0 $_bisect_option b2 ^root
+ test_bisection_diff 0 $_bisect_option b3 ^root
+ test_bisection_diff 0 $_bisect_option c1 ^root
+ test_bisection_diff 0 $_bisect_option c2 ^root
+ test_bisection_diff 0 $_bisect_option c3 ^root
+ test_bisection_diff 0 $_bisect_option E ^F
+ test_bisection_diff 0 $_bisect_option e1 ^F
+ test_bisection_diff 0 $_bisect_option e2 ^F
+ test_bisection_diff 0 $_bisect_option e3 ^F
+ test_bisection_diff 0 $_bisect_option e4 ^F
+ test_bisection_diff 0 $_bisect_option e5 ^F
+ test_bisection_diff 0 $_bisect_option e6 ^F
+ test_bisection_diff 0 $_bisect_option e7 ^F
+ test_bisection_diff 0 $_bisect_option f1 ^F
+ test_bisection_diff 0 $_bisect_option f2 ^F
+ test_bisection_diff 0 $_bisect_option f3 ^F
+ test_bisection_diff 0 $_bisect_option f4 ^F
+ test_bisection_diff 0 $_bisect_option E ^F
+
+ test_bisection_diff 1 $_bisect_option V ^U
+ test_bisection_diff 0 $_bisect_option V ^U ^u1 ^u2 ^u3
+ test_bisection_diff 0 $_bisect_option u1 ^U
+ test_bisection_diff 0 $_bisect_option u2 ^U
+ test_bisection_diff 0 $_bisect_option u3 ^U
+ test_bisection_diff 0 $_bisect_option u4 ^U
+ test_bisection_diff 0 $_bisect_option u5 ^U
+
+#
+# the following illustrates Linus' binary bug blatt idea.
+#
+# assume the bug is actually at l3, but you don't know that - all you know is that l3 is broken
+# and it wasn't broken before
+#
+# keep bisecting the list, advancing the "bad" head and accumulating "good" heads until
+# the bisection point is the head - this is the bad point.
+#
+
+test_output_expect_success "$_bisect_option l5 ^root" 'git rev-list $_bisect_option l5 ^root' <<EOF
+c3
+EOF
+
+test_output_expect_success "$_bisect_option l5 ^root ^c3" 'git rev-list $_bisect_option l5 ^root ^c3' <<EOF
+b4
+EOF
+
+test_output_expect_success "$_bisect_option l5 ^root ^c3 ^b4" 'git rev-list $_bisect_option l5 ^c3 ^b4' <<EOF
+l3
+EOF
+
+test_output_expect_success "$_bisect_option l3 ^root ^c3 ^b4" 'git rev-list $_bisect_option l3 ^root ^c3 ^b4' <<EOF
+a4
+EOF
+
+test_output_expect_success "$_bisect_option l5 ^b3 ^a3 ^b4 ^a4" 'git rev-list $_bisect_option l3 ^b3 ^a3 ^a4' <<EOF
+l3
+EOF
+
+#
+# if l3 is bad, then l4 is bad too - so advance the bad pointer by making b4 the known bad head
+#
+
+test_output_expect_success "$_bisect_option l4 ^a2 ^a3 ^b ^a4" 'git rev-list $_bisect_option l4 ^a2 ^a3 ^a4' <<EOF
+l3
+EOF
+
+test_output_expect_success "$_bisect_option l3 ^a2 ^a3 ^b ^a4" 'git rev-list $_bisect_option l3 ^a2 ^a3 ^a4' <<EOF
+l3
+EOF
+
+# found!
+
+#
+# as another example, let's consider a4 to be the bad head, in which case
+#
+
+test_output_expect_success "$_bisect_option a4 ^a2 ^a3 ^b4" 'git rev-list $_bisect_option a4 ^a2 ^a3 ^b4' <<EOF
+c2
+EOF
+
+test_output_expect_success "$_bisect_option a4 ^a2 ^a3 ^b4 ^c2" 'git rev-list $_bisect_option a4 ^a2 ^a3 ^b4 ^c2' <<EOF
+c3
+EOF
+
+test_output_expect_success "$_bisect_option a4 ^a2 ^a3 ^b4 ^c2 ^c3" 'git rev-list $_bisect_option a4 ^a2 ^a3 ^b4 ^c2 ^c3' <<EOF
+a4
+EOF
+
+# found!
+
+#
+# or consider c3 to be the bad head
+#
+
+test_output_expect_success "$_bisect_option a4 ^a2 ^a3 ^b4" 'git rev-list $_bisect_option a4 ^a2 ^a3 ^b4' <<EOF
+c2
+EOF
+
+test_output_expect_success "$_bisect_option c3 ^a2 ^a3 ^b4 ^c2" 'git rev-list $_bisect_option c3 ^a2 ^a3 ^b4 ^c2' <<EOF
+c3
+EOF
+
+# found!
+
+}
+
+test_sequence "--bisect"
+
+#
+#
+
+test_expect_success 'set up fake --bisect refs' '
+ git update-ref refs/bisect/bad c3 &&
+ good=$(git rev-parse b1) &&
+ git update-ref refs/bisect/good-$good $good &&
+ good=$(git rev-parse c1) &&
+ git update-ref refs/bisect/good-$good $good
+'
+
+test_expect_success 'rev-list --bisect can default to good/bad refs' '
+ # the only thing between c3 and c1 is c2
+ git rev-parse c2 >expect &&
+ git rev-list --bisect >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rev-parse --bisect can default to good/bad refs' '
+ git rev-parse c3 ^b1 ^c1 >expect &&
+ git rev-parse --bisect >actual &&
+
+ # output order depends on the refnames, which in turn depends on
+ # the exact sha1s. We just want to make sure we have the same set
+ # of lines in any order.
+ sort <expect >expect.sorted &&
+ sort <actual >actual.sorted &&
+ test_cmp expect.sorted actual.sorted
+'
+
+test_output_expect_success '--bisect --first-parent' 'git rev-list --bisect --first-parent E ^F' <<EOF
+e4
+EOF
+
+test_output_expect_success '--first-parent' 'git rev-list --first-parent E ^F' <<EOF
+E
+e1
+e2
+e3
+e4
+e5
+e6
+e7
+e8
+EOF
+
+test_output_expect_success '--bisect-vars --first-parent' 'git rev-list --bisect-vars --first-parent E ^F' <<EOF
+bisect_rev='e5'
+bisect_nr=4
+bisect_good=4
+bisect_bad=3
+bisect_all=9
+bisect_steps=2
+EOF
+
+test_expect_success '--bisect-all --first-parent' '
+ cat >expect.unsorted <<-EOF &&
+ $(git rev-parse E) (tag: E, dist=0)
+ $(git rev-parse e1) (tag: e1, dist=1)
+ $(git rev-parse e2) (tag: e2, dist=2)
+ $(git rev-parse e3) (tag: e3, dist=3)
+ $(git rev-parse e4) (tag: e4, dist=4)
+ $(git rev-parse e5) (tag: e5, dist=4)
+ $(git rev-parse e6) (tag: e6, dist=3)
+ $(git rev-parse e7) (tag: e7, dist=2)
+ $(git rev-parse e8) (tag: e8, dist=1)
+ EOF
+
+ # expect results to be ordered by distance (descending),
+ # commit hash (ascending)
+ sort -k4,4r -k1,1 expect.unsorted >expect &&
+ git rev-list --bisect-all --first-parent E ^F >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t6003-rev-list-topo-order.sh b/t/t6003-rev-list-topo-order.sh
new file mode 100755
index 0000000..5cf2cee
--- /dev/null
+++ b/t/t6003-rev-list-topo-order.sh
@@ -0,0 +1,445 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Jon Seymour
+#
+
+test_description='Tests git rev-list --topo-order functionality'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-t6000.sh # t6xxx specific functions
+
+list_duplicates()
+{
+ "$@" | sort | uniq -d
+}
+
+date >path0
+git update-index --add path0
+save_tag tree git write-tree
+on_dates "00:00" "00:00" hide_error save_tag root unique_commit root tree
+on_dates "00:01" "00:01" save_tag l0 unique_commit l0 tree -p root
+on_dates "00:02" "00:02" save_tag l1 unique_commit l1 tree -p l0
+on_dates "00:03" "00:03" save_tag l2 unique_commit l2 tree -p l1
+on_dates "00:04" "00:04" save_tag a0 unique_commit a0 tree -p l2
+on_dates "00:05" "00:05" save_tag a1 unique_commit a1 tree -p a0
+on_dates "00:06" "00:06" save_tag b1 unique_commit b1 tree -p a0
+on_dates "00:07" "00:07" save_tag c1 unique_commit c1 tree -p b1
+on_dates "00:08" "00:08" as_author foobar@example.com save_tag b2 unique_commit b2 tree -p b1
+on_dates "00:09" "00:09" save_tag b3 unique_commit b3 tree -p b2
+on_dates "00:10" "00:10" save_tag c2 unique_commit c2 tree -p c1 -p b2
+on_dates "00:11" "00:11" save_tag c3 unique_commit c3 tree -p c2
+on_dates "00:12" "00:00" save_tag a2 unique_commit a2 tree -p a1
+on_dates "00:13" "00:01" save_tag a3 unique_commit a3 tree -p a2
+on_dates "00:14" "00:14" save_tag b4 unique_commit b4 tree -p b3 -p a3
+on_dates "00:15" "00:15" save_tag a4 unique_commit a4 tree -p a3 -p b4 -p c3
+on_dates "00:16" "00:16" save_tag l3 unique_commit l3 tree -p a4
+on_dates "00:17" "00:17" save_tag l4 unique_commit l4 tree -p l3
+on_dates "00:18" "00:18" save_tag l5 unique_commit l5 tree -p l4
+on_dates "00:19" "00:19" save_tag m1 unique_commit m1 tree -p a4 -p c3
+on_dates "00:20" "00:20" save_tag m2 unique_commit m2 tree -p c3 -p a4
+on_dates "00:21" "00:21" hide_error save_tag alt_root unique_commit alt_root tree
+on_dates "00:22" "00:22" save_tag r0 unique_commit r0 tree -p alt_root
+on_dates "00:23" "00:23" save_tag r1 unique_commit r1 tree -p r0
+on_dates "00:24" "00:24" save_tag l5r1 unique_commit l5r1 tree -p l5 -p r1
+on_dates "00:25" "00:25" save_tag r1l5 unique_commit r1l5 tree -p r1 -p l5
+
+
+hide_error save_tag e1 as_author e@example.com unique_commit e1 tree
+save_tag e2 as_author e@example.com unique_commit e2 tree -p e1
+save_tag f1 as_author f@example.com unique_commit f1 tree -p e1
+save_tag e3 as_author e@example.com unique_commit e3 tree -p e2
+save_tag f2 as_author f@example.com unique_commit f2 tree -p f1
+save_tag e4 as_author e@example.com unique_commit e4 tree -p e3 -p f2
+save_tag e5 as_author e@example.com unique_commit e5 tree -p e4
+save_tag f3 as_author f@example.com unique_commit f3 tree -p f2
+save_tag f4 as_author f@example.com unique_commit f4 tree -p f3
+save_tag e6 as_author e@example.com unique_commit e6 tree -p e5 -p f4
+save_tag f5 as_author f@example.com unique_commit f5 tree -p f4
+save_tag f6 as_author f@example.com unique_commit f6 tree -p f5 -p e6
+save_tag e7 as_author e@example.com unique_commit e7 tree -p e6
+save_tag e8 as_author e@example.com unique_commit e8 tree -p e7
+save_tag e9 as_author e@example.com unique_commit e9 tree -p e8
+save_tag f7 as_author f@example.com unique_commit f7 tree -p f6
+save_tag f8 as_author f@example.com unique_commit f8 tree -p f7
+save_tag f9 as_author f@example.com unique_commit f9 tree -p f8
+save_tag e10 as_author e@example.com unique_commit e1 tree -p e9 -p f8
+
+hide_error save_tag g0 unique_commit g0 tree
+save_tag g1 unique_commit g1 tree -p g0
+save_tag h1 unique_commit g2 tree -p g0
+save_tag g2 unique_commit g3 tree -p g1 -p h1
+save_tag h2 unique_commit g4 tree -p g2
+save_tag g3 unique_commit g5 tree -p g2
+save_tag g4 unique_commit g6 tree -p g3 -p h2
+
+git update-ref HEAD $(tag l5)
+
+test_output_expect_success 'rev-list has correct number of entries' 'git rev-list HEAD | wc -l | tr -d \" \"' <<EOF
+19
+EOF
+
+test_output_expect_success 'simple topo order' 'git rev-list --topo-order HEAD' <<EOF
+l5
+l4
+l3
+a4
+c3
+c2
+c1
+b4
+a3
+a2
+a1
+b3
+b2
+b1
+a0
+l2
+l1
+l0
+root
+EOF
+
+test_output_expect_success 'simple date order' 'git rev-list --date-order HEAD' <<EOF
+l5
+l4
+l3
+a4
+b4
+a3
+a2
+c3
+c2
+b3
+b2
+c1
+b1
+a1
+a0
+l2
+l1
+l0
+root
+EOF
+
+test_output_expect_success 'simple author-date order' 'git rev-list --author-date-order HEAD' <<EOF
+l5
+l4
+l3
+a4
+b4
+c3
+c2
+b3
+b2
+c1
+b1
+a3
+a2
+a1
+a0
+l2
+l1
+l0
+root
+EOF
+
+test_output_expect_success 'two diamonds topo order (g6)' 'git rev-list --topo-order g4' <<EOF
+g4
+h2
+g3
+g2
+h1
+g1
+g0
+EOF
+
+test_output_expect_success 'multiple heads' 'git rev-list --topo-order a3 b3 c3' <<EOF
+a3
+a2
+a1
+c3
+c2
+c1
+b3
+b2
+b1
+a0
+l2
+l1
+l0
+root
+EOF
+
+test_output_expect_success 'multiple heads, prune at a1' 'git rev-list --topo-order a3 b3 c3 ^a1' <<EOF
+a3
+a2
+c3
+c2
+c1
+b3
+b2
+b1
+EOF
+
+test_output_expect_success 'multiple heads, prune at l1' 'git rev-list --topo-order a3 b3 c3 ^l1' <<EOF
+a3
+a2
+a1
+c3
+c2
+c1
+b3
+b2
+b1
+a0
+l2
+EOF
+
+test_output_expect_success 'cross-epoch, head at l5, prune at l1' 'git rev-list --topo-order l5 ^l1' <<EOF
+l5
+l4
+l3
+a4
+c3
+c2
+c1
+b4
+a3
+a2
+a1
+b3
+b2
+b1
+a0
+l2
+EOF
+
+test_output_expect_success 'duplicated head arguments' 'git rev-list --topo-order l5 l5 ^l1' <<EOF
+l5
+l4
+l3
+a4
+c3
+c2
+c1
+b4
+a3
+a2
+a1
+b3
+b2
+b1
+a0
+l2
+EOF
+
+test_output_expect_success 'prune near topo' 'git rev-list --topo-order a4 ^c3' <<EOF
+a4
+b4
+a3
+a2
+a1
+b3
+EOF
+
+test_output_expect_success "head has no parent" 'git rev-list --topo-order root' <<EOF
+root
+EOF
+
+test_output_expect_success "two nodes - one head, one base" 'git rev-list --topo-order l0' <<EOF
+l0
+root
+EOF
+
+test_output_expect_success "three nodes one head, one internal, one base" 'git rev-list --topo-order l1' <<EOF
+l1
+l0
+root
+EOF
+
+test_output_expect_success "linear prune l2 ^root" 'git rev-list --topo-order l2 ^root' <<EOF
+l2
+l1
+l0
+EOF
+
+test_output_expect_success "linear prune l2 ^l0" 'git rev-list --topo-order l2 ^l0' <<EOF
+l2
+l1
+EOF
+
+test_output_expect_success "linear prune l2 ^l1" 'git rev-list --topo-order l2 ^l1' <<EOF
+l2
+EOF
+
+test_output_expect_success "linear prune l5 ^a4" 'git rev-list --topo-order l5 ^a4' <<EOF
+l5
+l4
+l3
+EOF
+
+test_output_expect_success "linear prune l5 ^l3" 'git rev-list --topo-order l5 ^l3' <<EOF
+l5
+l4
+EOF
+
+test_output_expect_success "linear prune l5 ^l4" 'git rev-list --topo-order l5 ^l4' <<EOF
+l5
+EOF
+
+test_output_expect_success "max-count 10 - topo order" 'git rev-list --topo-order --max-count=10 l5' <<EOF
+l5
+l4
+l3
+a4
+c3
+c2
+c1
+b4
+a3
+a2
+EOF
+
+test_output_expect_success "max-count 10 - non topo order" 'git rev-list --max-count=10 l5' <<EOF
+l5
+l4
+l3
+a4
+b4
+a3
+a2
+c3
+c2
+b3
+EOF
+
+test_output_expect_success '--max-age=c3, no --topo-order' "git rev-list --max-age=$(commit_date c3) l5" <<EOF
+l5
+l4
+l3
+a4
+b4
+a3
+a2
+c3
+EOF
+
+test_output_expect_success '--max-age=c3, --topo-order' "git rev-list --topo-order --max-age=$(commit_date c3) l5" <<EOF
+l5
+l4
+l3
+a4
+c3
+b4
+a3
+a2
+EOF
+
+test_output_expect_success 'one specified head reachable from another a4, c3, --topo-order' "list_duplicates git rev-list --topo-order a4 c3" <<EOF
+EOF
+
+test_output_expect_success 'one specified head reachable from another c3, a4, --topo-order' "list_duplicates git rev-list --topo-order c3 a4" <<EOF
+EOF
+
+test_output_expect_success 'one specified head reachable from another a4, c3, no --topo-order' "list_duplicates git rev-list a4 c3" <<EOF
+EOF
+
+test_output_expect_success 'one specified head reachable from another c3, a4, no --topo-order' "list_duplicates git rev-list c3 a4" <<EOF
+EOF
+
+test_output_expect_success 'graph with c3 and a4 parents of head' "list_duplicates git rev-list m1" <<EOF
+EOF
+
+test_output_expect_success 'graph with a4 and c3 parents of head' "list_duplicates git rev-list m2" <<EOF
+EOF
+
+test_expect_success "head ^head --topo-order" 'git rev-list --topo-order a3 ^a3' <<EOF
+EOF
+
+test_expect_success "head ^head no --topo-order" 'git rev-list a3 ^a3' <<EOF
+EOF
+
+test_output_expect_success 'simple topo order (l5r1)' 'git rev-list --topo-order l5r1' <<EOF
+l5r1
+r1
+r0
+alt_root
+l5
+l4
+l3
+a4
+c3
+c2
+c1
+b4
+a3
+a2
+a1
+b3
+b2
+b1
+a0
+l2
+l1
+l0
+root
+EOF
+
+test_output_expect_success 'simple topo order (r1l5)' 'git rev-list --topo-order r1l5' <<EOF
+r1l5
+l5
+l4
+l3
+a4
+c3
+c2
+c1
+b4
+a3
+a2
+a1
+b3
+b2
+b1
+a0
+l2
+l1
+l0
+root
+r1
+r0
+alt_root
+EOF
+
+test_output_expect_success "don't print things unreachable from one branch" "git rev-list a3 ^b3 --topo-order" <<EOF
+a3
+a2
+a1
+EOF
+
+test_output_expect_success "--topo-order a4 l3" "git rev-list --topo-order a4 l3" <<EOF
+l3
+a4
+c3
+c2
+c1
+b4
+a3
+a2
+a1
+b3
+b2
+b1
+a0
+l2
+l1
+l0
+root
+EOF
+
+#
+#
+
+test_done
diff --git a/t/t6004-rev-list-path-optim.sh b/t/t6004-rev-list-path-optim.sh
new file mode 100755
index 0000000..cd4f420
--- /dev/null
+++ b/t/t6004-rev-list-path-optim.sh
@@ -0,0 +1,99 @@
+#!/bin/sh
+
+test_description='git rev-list trivial path optimization test
+
+ d/z1
+ b0 b1
+ o------------------------*----o main
+ / /
+ o---------o----o----o----o side
+ a0 c0 c1 a1 c2
+ d/f0 d/f1
+ d/z0
+
+'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success setup '
+ echo Hello >a &&
+ mkdir d &&
+ echo World >d/f &&
+ echo World >d/z &&
+ git add a d &&
+ test_tick &&
+ git commit -m "Initial commit" &&
+ git rev-parse --verify HEAD &&
+ git tag initial
+'
+
+test_expect_success path-optimization '
+ test_tick &&
+ commit=$(echo "Unchanged tree" | git commit-tree "HEAD^{tree}" -p HEAD) &&
+ test $(git rev-list $commit | wc -l) = 2 &&
+ test $(git rev-list $commit -- . | wc -l) = 1
+'
+
+test_expect_success 'further setup' '
+ git checkout -b side &&
+ echo Irrelevant >c &&
+ echo Irrelevant >d/f &&
+ git add c d/f &&
+ test_tick &&
+ git commit -m "Side makes an irrelevant commit" &&
+ git tag side_c0 &&
+ echo "More Irrelevancy" >c &&
+ git add c &&
+ test_tick &&
+ git commit -m "Side makes another irrelevant commit" &&
+ echo Bye >a &&
+ git add a &&
+ test_tick &&
+ git commit -m "Side touches a" &&
+ git tag side_a1 &&
+ echo "Yet more Irrelevancy" >c &&
+ git add c &&
+ test_tick &&
+ git commit -m "Side makes yet another irrelevant commit" &&
+ git checkout main &&
+ echo Another >b &&
+ echo Munged >d/z &&
+ git add b d/z &&
+ test_tick &&
+ git commit -m "Main touches b" &&
+ git tag main_b0 &&
+ git merge side &&
+ echo Touched >b &&
+ git add b &&
+ test_tick &&
+ git commit -m "Main touches b again"
+'
+
+test_expect_success 'path optimization 2' '
+ git rev-parse side_a1 initial >expected &&
+ git rev-list HEAD -- a >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'pathspec with leading path' '
+ git rev-parse main^ main_b0 side_c0 initial >expected &&
+ git rev-list HEAD -- d >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'pathspec with glob (1)' '
+ git rev-parse main^ main_b0 side_c0 initial >expected &&
+ git rev-list HEAD -- "d/*" >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'pathspec with glob (2)' '
+ git rev-parse side_c0 initial >expected &&
+ git rev-list HEAD -- "d/[a-m]*" >actual &&
+ test_cmp expected actual
+'
+
+test_done
diff --git a/t/t6005-rev-list-count.sh b/t/t6005-rev-list-count.sh
new file mode 100755
index 0000000..0729f80
--- /dev/null
+++ b/t/t6005-rev-list-count.sh
@@ -0,0 +1,52 @@
+#!/bin/sh
+
+test_description='git rev-list --max-count and --skip test'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ for n in 1 2 3 4 5 ; do
+ echo $n > a &&
+ git add a &&
+ git commit -m "$n" || return 1
+ done
+'
+
+test_expect_success 'no options' '
+ test_stdout_line_count = 5 git rev-list HEAD
+'
+
+test_expect_success '--max-count' '
+ test_stdout_line_count = 0 git rev-list HEAD --max-count=0 &&
+ test_stdout_line_count = 3 git rev-list HEAD --max-count=3 &&
+ test_stdout_line_count = 5 git rev-list HEAD --max-count=5 &&
+ test_stdout_line_count = 5 git rev-list HEAD --max-count=10
+'
+
+test_expect_success '--max-count all forms' '
+ test_stdout_line_count = 1 git rev-list HEAD --max-count=1 &&
+ test_stdout_line_count = 1 git rev-list HEAD -1 &&
+ test_stdout_line_count = 1 git rev-list HEAD -n1 &&
+ test_stdout_line_count = 1 git rev-list HEAD -n 1
+'
+
+test_expect_success '--skip' '
+ test_stdout_line_count = 5 git rev-list HEAD --skip=0 &&
+ test_stdout_line_count = 2 git rev-list HEAD --skip=3 &&
+ test_stdout_line_count = 0 git rev-list HEAD --skip=5 &&
+ test_stdout_line_count = 0 git rev-list HEAD --skip=10
+'
+
+test_expect_success '--skip --max-count' '
+ test_stdout_line_count = 0 git rev-list HEAD --skip=0 --max-count=0 &&
+ test_stdout_line_count = 5 git rev-list HEAD --skip=0 --max-count=10 &&
+ test_stdout_line_count = 0 git rev-list HEAD --skip=3 --max-count=0 &&
+ test_stdout_line_count = 1 git rev-list HEAD --skip=3 --max-count=1 &&
+ test_stdout_line_count = 2 git rev-list HEAD --skip=3 --max-count=2 &&
+ test_stdout_line_count = 2 git rev-list HEAD --skip=3 --max-count=10 &&
+ test_stdout_line_count = 0 git rev-list HEAD --skip=5 --max-count=10 &&
+ test_stdout_line_count = 0 git rev-list HEAD --skip=10 --max-count=10
+'
+
+test_done
diff --git a/t/t6006-rev-list-format.sh b/t/t6006-rev-list-format.sh
new file mode 100755
index 0000000..573eb97
--- /dev/null
+++ b/t/t6006-rev-list-format.sh
@@ -0,0 +1,609 @@
+#!/bin/sh
+
+# Copyright (c) 2009 Jens Lehmann
+# Copyright (c) 2011 Alexey Shumkin (+ non-UTF-8 commit encoding tests)
+
+test_description='git rev-list --pretty=format test'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-terminal.sh
+
+test_tick
+# Tested non-UTF-8 encoding
+test_encoding="ISO8859-1"
+
+# String "added" in German
+# (translated with Google Translate),
+# encoded in UTF-8, used as a commit log message below.
+added_utf8_part=$(printf "\303\274")
+added_utf8_part_iso88591=$(echo "$added_utf8_part" | iconv -f utf-8 -t $test_encoding)
+added=$(printf "added (hinzugef${added_utf8_part}gt) foo")
+added_iso88591=$(echo "$added" | iconv -f utf-8 -t $test_encoding)
+# same but "changed"
+changed_utf8_part=$(printf "\303\244")
+changed_utf8_part_iso88591=$(echo "$changed_utf8_part" | iconv -f utf-8 -t $test_encoding)
+changed=$(printf "changed (ge${changed_utf8_part}ndert) foo")
+changed_iso88591=$(echo "$changed" | iconv -f utf-8 -t $test_encoding)
+
+# Count of char to truncate
+# Number is chosen so, that non-ACSII characters
+# (see $added_utf8_part and $changed_utf8_part)
+# fall into truncated parts of appropriate words both from left and right
+truncate_count=20
+
+test_expect_success 'setup' '
+ : >foo &&
+ git add foo &&
+ git config i18n.commitEncoding $test_encoding &&
+ echo "$added_iso88591" | git commit -F - &&
+ head1=$(git rev-parse --verify HEAD) &&
+ head1_short=$(git rev-parse --verify --short $head1) &&
+ head1_short4=$(git rev-parse --verify --short=4 $head1) &&
+ tree1=$(git rev-parse --verify HEAD:) &&
+ tree1_short=$(git rev-parse --verify --short $tree1) &&
+ echo "$changed" > foo &&
+ echo "$changed_iso88591" | git commit -a -F - &&
+ head2=$(git rev-parse --verify HEAD) &&
+ head2_short=$(git rev-parse --verify --short $head2) &&
+ head2_short4=$(git rev-parse --verify --short=4 $head2) &&
+ tree2=$(git rev-parse --verify HEAD:) &&
+ tree2_short=$(git rev-parse --verify --short $tree2) &&
+ git config --unset i18n.commitEncoding
+'
+
+# usage: test_format [argument...] name format_string [failure] <expected_output
+test_format () {
+ local args=
+ while true
+ do
+ case "$1" in
+ --*)
+ args="$args $1"
+ shift;;
+ *)
+ break;;
+ esac
+ done
+ cat >expect.$1
+ test_expect_${3:-success} "format $1" "
+ git rev-list $args --pretty=format:'$2' main >output.$1 &&
+ test_cmp expect.$1 output.$1
+ "
+}
+
+# usage: test_pretty [argument...] name format_name [failure] <expected_output
+test_pretty () {
+ local args=
+ while true
+ do
+ case "$1" in
+ --*)
+ args="$args $1"
+ shift;;
+ *)
+ break;;
+ esac
+ done
+ cat >expect.$1
+ test_expect_${3:-success} "pretty $1 (without --no-commit-header)" "
+ git rev-list $args --pretty='$2' main >output.$1 &&
+ test_cmp expect.$1 output.$1
+ "
+ test_expect_${3:-success} "pretty $1 (with --no-commit-header)" "
+ git rev-list $args --no-commit-header --pretty='$2' main >output.$1 &&
+ test_cmp expect.$1 output.$1
+ "
+}
+
+# Feed to --format to provide predictable colored sequences.
+BASIC_COLOR='%Credfoo%Creset'
+COLOR='%C(red)foo%C(reset)'
+AUTO_COLOR='%C(auto,red)foo%C(auto,reset)'
+ALWAYS_COLOR='%C(always,red)foo%C(always,reset)'
+has_color () {
+ test_decode_color <"$1" >decoded &&
+ echo "<RED>foo<RESET>" >expect &&
+ test_cmp expect decoded
+}
+
+has_no_color () {
+ echo foo >expect &&
+ test_cmp expect "$1"
+}
+
+test_format percent %%h <<EOF
+commit $head2
+%h
+commit $head1
+%h
+EOF
+
+test_format hash %H%n%h <<EOF
+commit $head2
+$head2
+$head2_short
+commit $head1
+$head1
+$head1_short
+EOF
+
+test_format --no-commit-header hash-no-header %H%n%h <<EOF
+$head2
+$head2_short
+$head1
+$head1_short
+EOF
+
+test_format --abbrev-commit --abbrev=0 --no-commit-header hash-no-header-abbrev %H%n%h <<EOF
+$head2
+$head2_short4
+$head1
+$head1_short4
+EOF
+
+test_format tree %T%n%t <<EOF
+commit $head2
+$tree2
+$tree2_short
+commit $head1
+$tree1
+$tree1_short
+EOF
+
+test_format parents %P%n%p <<EOF
+commit $head2
+$head1
+$head1_short
+commit $head1
+
+
+EOF
+
+# we don't test relative here
+test_format author %an%n%ae%n%al%n%ad%n%aD%n%at <<EOF
+commit $head2
+$GIT_AUTHOR_NAME
+$GIT_AUTHOR_EMAIL
+$TEST_AUTHOR_LOCALNAME
+Thu Apr 7 15:13:13 2005 -0700
+Thu, 7 Apr 2005 15:13:13 -0700
+1112911993
+commit $head1
+$GIT_AUTHOR_NAME
+$GIT_AUTHOR_EMAIL
+$TEST_AUTHOR_LOCALNAME
+Thu Apr 7 15:13:13 2005 -0700
+Thu, 7 Apr 2005 15:13:13 -0700
+1112911993
+EOF
+
+test_format committer %cn%n%ce%n%cl%n%cd%n%cD%n%ct <<EOF
+commit $head2
+$GIT_COMMITTER_NAME
+$GIT_COMMITTER_EMAIL
+$TEST_COMMITTER_LOCALNAME
+Thu Apr 7 15:13:13 2005 -0700
+Thu, 7 Apr 2005 15:13:13 -0700
+1112911993
+commit $head1
+$GIT_COMMITTER_NAME
+$GIT_COMMITTER_EMAIL
+$TEST_COMMITTER_LOCALNAME
+Thu Apr 7 15:13:13 2005 -0700
+Thu, 7 Apr 2005 15:13:13 -0700
+1112911993
+EOF
+
+test_format encoding %e <<EOF
+commit $head2
+$test_encoding
+commit $head1
+$test_encoding
+EOF
+
+test_format subject %s <<EOF
+commit $head2
+$changed
+commit $head1
+$added
+EOF
+
+test_format subject-truncated "%<($truncate_count,trunc)%s" <<EOF
+commit $head2
+changed (ge${changed_utf8_part}ndert)..
+commit $head1
+added (hinzugef${added_utf8_part}gt..
+EOF
+
+test_format body %b <<EOF
+commit $head2
+commit $head1
+EOF
+
+test_format raw-body %B <<EOF
+commit $head2
+$changed
+
+commit $head1
+$added
+
+EOF
+
+test_format --no-commit-header raw-body-no-header %B <<EOF
+$changed
+
+$added
+
+EOF
+
+test_pretty oneline oneline <<EOF
+$head2 $changed
+$head1 $added
+EOF
+
+test_pretty short short <<EOF
+commit $head2
+Author: $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>
+
+ $changed
+
+commit $head1
+Author: $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL>
+
+ $added
+
+EOF
+
+test_expect_success 'basic colors' '
+ cat >expect <<-EOF &&
+ commit $head2
+ <RED>foo<GREEN>bar<BLUE>baz<RESET>xyzzy
+ EOF
+ format="%Credfoo%Cgreenbar%Cbluebaz%Cresetxyzzy" &&
+ git rev-list --color --format="$format" -1 main >actual.raw &&
+ test_decode_color <actual.raw >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '%S is not a placeholder for rev-list yet' '
+ git rev-list --format="%S" -1 main | grep "%S"
+'
+
+test_expect_success 'advanced colors' '
+ cat >expect <<-EOF &&
+ commit $head2
+ <BOLD;RED;BYELLOW>foo<RESET>
+ EOF
+ format="%C(red yellow bold)foo%C(reset)" &&
+ git rev-list --color --format="$format" -1 main >actual.raw &&
+ test_decode_color <actual.raw >actual &&
+ test_cmp expect actual
+'
+
+for spec in \
+ "%Cred:$BASIC_COLOR" \
+ "%C(...):$COLOR" \
+ "%C(auto,...):$AUTO_COLOR"
+do
+ desc=${spec%%:*}
+ color=${spec#*:}
+ test_expect_success "$desc does not enable color by default" '
+ git log --format=$color -1 >actual &&
+ has_no_color actual
+ '
+
+ test_expect_success "$desc enables colors for color.diff" '
+ git -c color.diff=always log --format=$color -1 >actual &&
+ has_color actual
+ '
+
+ test_expect_success "$desc enables colors for color.ui" '
+ git -c color.ui=always log --format=$color -1 >actual &&
+ has_color actual
+ '
+
+ test_expect_success "$desc respects --color" '
+ git log --format=$color -1 --color >actual &&
+ has_color actual
+ '
+
+ test_expect_success "$desc respects --no-color" '
+ git -c color.ui=always log --format=$color -1 --no-color >actual &&
+ has_no_color actual
+ '
+
+ test_expect_success TTY "$desc respects --color=auto (stdout is tty)" '
+ test_terminal git log --format=$color -1 --color=auto >actual &&
+ has_color actual
+ '
+
+ test_expect_success "$desc respects --color=auto (stdout not tty)" '
+ (
+ TERM=vt100 && export TERM &&
+ git log --format=$color -1 --color=auto >actual &&
+ has_no_color actual
+ )
+ '
+done
+
+test_expect_success '%C(always,...) enables color even without tty' '
+ git log --format=$ALWAYS_COLOR -1 >actual &&
+ has_color actual
+'
+
+test_expect_success '%C(auto) respects --color' '
+ git log --color --format="%C(auto)%H" -1 >actual.raw &&
+ test_decode_color <actual.raw >actual &&
+ echo "<YELLOW>$(git rev-parse HEAD)<RESET>" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success '%C(auto) respects --no-color' '
+ git log --no-color --format="%C(auto)%H" -1 >actual &&
+ git rev-parse HEAD >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rev-list %C(auto,...) respects --color' '
+ git rev-list --color --format="%C(auto,green)foo%C(auto,reset)" \
+ -1 HEAD >actual.raw &&
+ test_decode_color <actual.raw >actual &&
+ cat >expect <<-EOF &&
+ commit $(git rev-parse HEAD)
+ <GREEN>foo<RESET>
+ EOF
+ test_cmp expect actual
+'
+
+iconv -f utf-8 -t $test_encoding > commit-msg <<EOF
+Test printing of complex bodies
+
+This commit message is much longer than the others,
+and it will be encoded in $test_encoding. We should therefore
+include an ISO8859 character: ¡bueno!
+EOF
+
+test_expect_success 'setup complex body' '
+ git config i18n.commitencoding $test_encoding &&
+ echo change2 >foo && git commit -a -F commit-msg &&
+ head3=$(git rev-parse --verify HEAD) &&
+ head3_short=$(git rev-parse --short $head3)
+'
+
+test_format complex-encoding %e <<EOF
+commit $head3
+$test_encoding
+commit $head2
+$test_encoding
+commit $head1
+$test_encoding
+EOF
+
+test_format complex-subject %s <<EOF
+commit $head3
+Test printing of complex bodies
+commit $head2
+$changed_iso88591
+commit $head1
+$added_iso88591
+EOF
+
+test_format complex-subject-trunc "%<($truncate_count,trunc)%s" <<EOF
+commit $head3
+Test printing of c..
+commit $head2
+changed (ge${changed_utf8_part_iso88591}ndert)..
+commit $head1
+added (hinzugef${added_utf8_part_iso88591}gt..
+EOF
+
+test_format complex-subject-mtrunc "%<($truncate_count,mtrunc)%s" <<EOF
+commit $head3
+Test prin..ex bodies
+commit $head2
+changed (..dert) foo
+commit $head1
+added (hi..f${added_utf8_part_iso88591}gt) foo
+EOF
+
+test_format complex-subject-ltrunc "%<($truncate_count,ltrunc)%s" <<EOF
+commit $head3
+.. of complex bodies
+commit $head2
+..ged (ge${changed_utf8_part_iso88591}ndert) foo
+commit $head1
+.. (hinzugef${added_utf8_part_iso88591}gt) foo
+EOF
+
+test_expect_success 'setup expected messages (for test %b)' '
+ cat <<-EOF >expected.utf-8 &&
+ commit $head3
+ This commit message is much longer than the others,
+ and it will be encoded in $test_encoding. We should therefore
+ include an ISO8859 character: ¡bueno!
+
+ commit $head2
+ commit $head1
+ EOF
+ iconv -f utf-8 -t $test_encoding expected.utf-8 >expected.ISO8859-1
+'
+
+test_format complex-body %b <expected.ISO8859-1
+
+# Git uses i18n.commitEncoding if no i18n.logOutputEncoding set
+# so unset i18n.commitEncoding to test encoding conversion
+git config --unset i18n.commitEncoding
+
+test_format complex-subject-commitencoding-unset %s <<EOF
+commit $head3
+Test printing of complex bodies
+commit $head2
+$changed
+commit $head1
+$added
+EOF
+
+test_format complex-subject-commitencoding-unset-trunc "%<($truncate_count,trunc)%s" <<EOF
+commit $head3
+Test printing of c..
+commit $head2
+changed (ge${changed_utf8_part}ndert)..
+commit $head1
+added (hinzugef${added_utf8_part}gt..
+EOF
+
+test_format complex-subject-commitencoding-unset-mtrunc "%<($truncate_count,mtrunc)%s" <<EOF
+commit $head3
+Test prin..ex bodies
+commit $head2
+changed (..dert) foo
+commit $head1
+added (hi..f${added_utf8_part}gt) foo
+EOF
+
+test_format complex-subject-commitencoding-unset-ltrunc "%<($truncate_count,ltrunc)%s" <<EOF
+commit $head3
+.. of complex bodies
+commit $head2
+..ged (ge${changed_utf8_part}ndert) foo
+commit $head1
+.. (hinzugef${added_utf8_part}gt) foo
+EOF
+
+test_format complex-body-commitencoding-unset %b <expected.utf-8
+
+test_expect_success '%x00 shows NUL' '
+ echo >expect commit $head3 &&
+ echo >>expect fooQbar &&
+ git rev-list -1 --format=foo%x00bar HEAD >actual.nul &&
+ nul_to_q <actual.nul >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '%ad respects --date=' '
+ echo 2005-04-07 >expect.ad-short &&
+ git log -1 --date=short --pretty=tformat:%ad >output.ad-short main &&
+ test_cmp expect.ad-short output.ad-short
+'
+
+test_expect_success 'empty email' '
+ test_tick &&
+ C=$(GIT_AUTHOR_EMAIL= git commit-tree HEAD^{tree} </dev/null) &&
+ A=$(git show --pretty=format:%an,%ae,%ad%n -s $C) &&
+ test "$A" = "$GIT_AUTHOR_NAME,,Thu Apr 7 15:14:13 2005 -0700"
+'
+
+test_expect_success 'del LF before empty (1)' '
+ git show -s --pretty=format:"%s%n%-b%nThanks%n" HEAD^^ >actual &&
+ test_line_count = 2 actual
+'
+
+test_expect_success 'del LF before empty (2)' '
+ git show -s --pretty=format:"%s%n%-b%nThanks%n" HEAD >actual &&
+ test_line_count = 6 actual &&
+ grep "^$" actual
+'
+
+test_expect_success 'add LF before non-empty (1)' '
+ git show -s --pretty=format:"%s%+b%nThanks%n" HEAD^^ >actual &&
+ test_line_count = 2 actual
+'
+
+test_expect_success 'add LF before non-empty (2)' '
+ git show -s --pretty=format:"%s%+b%nThanks%n" HEAD >actual &&
+ test_line_count = 6 actual &&
+ grep "^$" actual
+'
+
+test_expect_success 'add SP before non-empty (1)' '
+ git show -s --pretty=format:"%s% bThanks" HEAD^^ >actual &&
+ test $(wc -w <actual) = 3
+'
+
+test_expect_success 'add SP before non-empty (2)' '
+ git show -s --pretty=format:"%s% sThanks" HEAD^^ >actual &&
+ test $(wc -w <actual) = 6
+'
+
+test_expect_success '--abbrev' '
+ echo SHORT SHORT SHORT >expect2 &&
+ echo LONG LONG LONG >expect3 &&
+ git log -1 --format="%h %h %h" HEAD >actual1 &&
+ git log -1 --abbrev=5 --format="%h %h %h" HEAD >actual2 &&
+ git log -1 --abbrev=5 --format="%H %H %H" HEAD >actual3 &&
+ sed -e "s/$OID_REGEX/LONG/g" -e "s/$_x05/SHORT/g" <actual2 >fuzzy2 &&
+ sed -e "s/$OID_REGEX/LONG/g" -e "s/$_x05/SHORT/g" <actual3 >fuzzy3 &&
+ test_cmp expect2 fuzzy2 &&
+ test_cmp expect3 fuzzy3 &&
+ ! test_cmp actual1 actual2
+'
+
+test_expect_success '%H is not affected by --abbrev-commit' '
+ expected=$(($(test_oid hexsz) + 1)) &&
+ git log -1 --format=%H --abbrev-commit --abbrev=20 HEAD >actual &&
+ len=$(wc -c <actual) &&
+ test $len = $expected
+'
+
+test_expect_success '%h is not affected by --abbrev-commit' '
+ git log -1 --format=%h --abbrev-commit --abbrev=20 HEAD >actual &&
+ len=$(wc -c <actual) &&
+ test $len = 21
+'
+
+test_expect_success '"%h %gD: %gs" is same as git-reflog' '
+ git reflog >expect &&
+ git log -g --format="%h %gD: %gs" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '"%h %gD: %gs" is same as git-reflog (with date)' '
+ git reflog --date=raw >expect &&
+ git log -g --format="%h %gD: %gs" --date=raw >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '"%h %gD: %gs" is same as git-reflog (with --abbrev)' '
+ git reflog --abbrev=13 --date=raw >expect &&
+ git log -g --abbrev=13 --format="%h %gD: %gs" --date=raw >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '%gd shortens ref name' '
+ echo "main@{0}" >expect.gd-short &&
+ git log -g -1 --format=%gd refs/heads/main >actual.gd-short &&
+ test_cmp expect.gd-short actual.gd-short
+'
+
+test_expect_success 'reflog identity' '
+ echo "$GIT_COMMITTER_NAME:$GIT_COMMITTER_EMAIL" >expect &&
+ git log -g -1 --format="%gn:%ge" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'oneline with empty message' '
+ git commit --allow-empty --cleanup=verbatim -m "$LF" &&
+ git commit --allow-empty --allow-empty-message &&
+ git rev-list --oneline HEAD >test.txt &&
+ test_line_count = 5 test.txt &&
+ git rev-list --oneline --graph HEAD >testg.txt &&
+ test_line_count = 5 testg.txt
+'
+
+test_expect_success 'single-character name is parsed correctly' '
+ git commit --author="a <a@example.com>" --allow-empty -m foo &&
+ echo "a <a@example.com>" >expect &&
+ git log -1 --format="%an <%ae>" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'unused %G placeholders are passed through' '
+ echo "%GX %G" >expect &&
+ git log -1 --format="%GX %G" >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t6007-rev-list-cherry-pick-file.sh b/t/t6007-rev-list-cherry-pick-file.sh
new file mode 100755
index 0000000..6f3e543
--- /dev/null
+++ b/t/t6007-rev-list-cherry-pick-file.sh
@@ -0,0 +1,284 @@
+#!/bin/sh
+
+test_description='test git rev-list --cherry-pick -- file'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+# A---B---D---F
+# \
+# \
+# C---E
+#
+# B changes a file foo.c, adding a line of text. C changes foo.c as
+# well as bar.c, but the change in foo.c was identical to change B.
+# D and C change bar in the same way, E and F differently.
+
+test_expect_success setup '
+ echo Hallo > foo &&
+ git add foo &&
+ test_tick &&
+ git commit -m "A" &&
+ git tag A &&
+ git checkout -b branch &&
+ echo Bello > foo &&
+ echo Cello > bar &&
+ git add foo bar &&
+ test_tick &&
+ git commit -m "C" &&
+ git tag C &&
+ echo Dello > bar &&
+ git add bar &&
+ test_tick &&
+ git commit -m "E" &&
+ git tag E &&
+ git checkout main &&
+ git checkout branch foo &&
+ test_tick &&
+ git commit -m "B" &&
+ git tag B &&
+ echo Cello > bar &&
+ git add bar &&
+ test_tick &&
+ git commit -m "D" &&
+ git tag D &&
+ echo Nello > bar &&
+ git add bar &&
+ test_tick &&
+ git commit -m "F" &&
+ git tag F
+'
+
+cat >expect <<EOF
+<tags/B
+>tags/C
+EOF
+
+test_expect_success '--left-right' '
+ git rev-list --left-right B...C > actual &&
+ git name-rev --annotate-stdin --name-only --refs="*tags/*" \
+ < actual > actual.named &&
+ test_cmp expect actual.named
+'
+
+test_expect_success '--count' '
+ git rev-list --count B...C > actual &&
+ test "$(cat actual)" = 2
+'
+
+test_expect_success '--cherry-pick foo comes up empty' '
+ test -z "$(git rev-list --left-right --cherry-pick B...C -- foo)"
+'
+
+cat >expect <<EOF
+>tags/C
+EOF
+
+test_expect_success '--cherry-pick bar does not come up empty' '
+ git rev-list --left-right --cherry-pick B...C -- bar > actual &&
+ git name-rev --annotate-stdin --name-only --refs="*tags/*" \
+ < actual > actual.named &&
+ test_cmp expect actual.named
+'
+
+test_expect_success 'bar does not come up empty' '
+ git rev-list --left-right B...C -- bar > actual &&
+ git name-rev --annotate-stdin --name-only --refs="*tags/*" \
+ < actual > actual.named &&
+ test_cmp expect actual.named
+'
+
+cat >expect <<EOF
+<tags/F
+>tags/E
+EOF
+
+test_expect_success '--cherry-pick bar does not come up empty (II)' '
+ git rev-list --left-right --cherry-pick F...E -- bar > actual &&
+ git name-rev --annotate-stdin --name-only --refs="*tags/*" \
+ < actual > actual.named &&
+ test_cmp expect actual.named
+'
+
+test_expect_success 'name-rev multiple --refs combine inclusive' '
+ git rev-list --left-right --cherry-pick F...E -- bar >actual &&
+ git name-rev --annotate-stdin --name-only --refs="*tags/F" --refs="*tags/E" \
+ <actual >actual.named &&
+ test_cmp expect actual.named
+'
+
+cat >expect <<EOF
+<tags/F
+EOF
+
+test_expect_success 'name-rev --refs excludes non-matched patterns' '
+ git rev-list --left-right --right-only --cherry-pick F...E -- bar >>expect &&
+ git rev-list --left-right --cherry-pick F...E -- bar >actual &&
+ git name-rev --annotate-stdin --name-only --refs="*tags/F" \
+ <actual >actual.named &&
+ test_cmp expect actual.named
+'
+
+cat >expect <<EOF
+<tags/F
+EOF
+
+test_expect_success 'name-rev --exclude excludes matched patterns' '
+ git rev-list --left-right --right-only --cherry-pick F...E -- bar >>expect &&
+ git rev-list --left-right --cherry-pick F...E -- bar >actual &&
+ git name-rev --annotate-stdin --name-only --refs="*tags/*" --exclude="*E" \
+ <actual >actual.named &&
+ test_cmp expect actual.named
+'
+
+test_expect_success 'name-rev --no-refs clears the refs list' '
+ git rev-list --left-right --cherry-pick F...E -- bar >expect &&
+ git name-rev --annotate-stdin --name-only --refs="*tags/F" --refs="*tags/E" --no-refs --refs="*tags/G" \
+ <expect >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<EOF
++tags/F
+=tags/D
++tags/E
+=tags/C
+EOF
+
+test_expect_success '--cherry-mark' '
+ git rev-list --cherry-mark F...E -- bar > actual &&
+ git name-rev --annotate-stdin --name-only --refs="*tags/*" \
+ < actual > actual.named &&
+ test_cmp expect actual.named
+'
+
+cat >expect <<EOF
+<tags/F
+=tags/D
+>tags/E
+=tags/C
+EOF
+
+test_expect_success '--cherry-mark --left-right' '
+ git rev-list --cherry-mark --left-right F...E -- bar > actual &&
+ git name-rev --annotate-stdin --name-only --refs="*tags/*" \
+ < actual > actual.named &&
+ test_cmp expect actual.named
+'
+
+cat >expect <<EOF
+tags/E
+EOF
+
+test_expect_success '--cherry-pick --right-only' '
+ git rev-list --cherry-pick --right-only F...E -- bar > actual &&
+ git name-rev --annotate-stdin --name-only --refs="*tags/*" \
+ < actual > actual.named &&
+ test_cmp expect actual.named
+'
+
+test_expect_success '--cherry-pick --left-only' '
+ git rev-list --cherry-pick --left-only E...F -- bar > actual &&
+ git name-rev --annotate-stdin --name-only --refs="*tags/*" \
+ < actual > actual.named &&
+ test_cmp expect actual.named
+'
+
+cat >expect <<EOF
++tags/E
+=tags/C
+EOF
+
+test_expect_success '--cherry' '
+ git rev-list --cherry F...E -- bar > actual &&
+ git name-rev --annotate-stdin --name-only --refs="*tags/*" \
+ < actual > actual.named &&
+ test_cmp expect actual.named
+'
+
+cat >expect <<EOF
+1 1
+EOF
+
+test_expect_success '--cherry --count' '
+ git rev-list --cherry --count F...E -- bar > actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<EOF
+2 2
+EOF
+
+test_expect_success '--cherry-mark --count' '
+ git rev-list --cherry-mark --count F...E -- bar > actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<EOF
+1 1 2
+EOF
+
+test_expect_success '--cherry-mark --left-right --count' '
+ git rev-list --cherry-mark --left-right --count F...E -- bar > actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--cherry-pick with independent, but identical branches' '
+ git symbolic-ref HEAD refs/heads/independent &&
+ rm .git/index &&
+ echo Hallo > foo &&
+ git add foo &&
+ test_tick &&
+ git commit -m "independent" &&
+ echo Bello > foo &&
+ test_tick &&
+ git commit -m "independent, too" foo &&
+ test -z "$(git rev-list --left-right --cherry-pick \
+ HEAD...main -- foo)"
+'
+
+cat >expect <<EOF
+1 2
+EOF
+
+test_expect_success '--count --left-right' '
+ git rev-list --count --left-right C...D > actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--cherry-pick with duplicates on each side' '
+ git checkout -b dup-orig &&
+ test_commit dup-base &&
+ git revert dup-base &&
+ git cherry-pick dup-base &&
+ git checkout -b dup-side HEAD~3 &&
+ test_tick &&
+ git cherry-pick -3 dup-orig &&
+ git rev-list --cherry-pick dup-orig...dup-side >actual &&
+ test_must_be_empty actual
+'
+
+# Corrupt the object store deliberately to make sure
+# the object is not even checked for its existence.
+remove_loose_object () {
+ sha1="$(git rev-parse "$1")" &&
+ remainder=${sha1#??} &&
+ firsttwo=${sha1%$remainder} &&
+ rm .git/objects/$firsttwo/$remainder
+}
+
+test_expect_success '--cherry-pick avoids looking at full diffs' '
+ git checkout -b shy-diff &&
+ test_commit dont-look-at-me &&
+ echo Hello >dont-look-at-me.t &&
+ test_tick &&
+ git commit -m tip dont-look-at-me.t &&
+ git checkout -b mainline HEAD^ &&
+ test_commit to-cherry-pick &&
+ remove_loose_object shy-diff^:dont-look-at-me.t &&
+ git rev-list --cherry-pick ...shy-diff
+'
+
+test_done
diff --git a/t/t6008-rev-list-submodule.sh b/t/t6008-rev-list-submodule.sh
new file mode 100755
index 0000000..2cdef6f
--- /dev/null
+++ b/t/t6008-rev-list-submodule.sh
@@ -0,0 +1,46 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Johannes E. Schindelin
+#
+
+test_description='git rev-list involving submodules that this repo has'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ : > file &&
+ git add file &&
+ test_tick &&
+ git commit -m initial &&
+ echo 1 > file &&
+ test_tick &&
+ git commit -m second file &&
+ echo 2 > file &&
+ test_tick &&
+ git commit -m third file &&
+
+ rm .git/index &&
+
+ : > super-file &&
+ git add super-file &&
+ git -c protocol.file.allow=always submodule add "$(pwd)" sub &&
+ git symbolic-ref HEAD refs/heads/super &&
+ test_tick &&
+ git commit -m super-initial &&
+ echo 1 > super-file &&
+ test_tick &&
+ git commit -m super-first super-file &&
+ echo 2 > super-file &&
+ test_tick &&
+ git commit -m super-second super-file
+'
+
+test_expect_success "Ilari's test" '
+ git rev-list --objects super main ^super^
+'
+
+test_done
diff --git a/t/t6009-rev-list-parent.sh b/t/t6009-rev-list-parent.sh
new file mode 100755
index 0000000..ced4015
--- /dev/null
+++ b/t/t6009-rev-list-parent.sh
@@ -0,0 +1,153 @@
+#!/bin/sh
+
+test_description='ancestor culling and limiting by parent number'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+check_revlist () {
+ rev_list_args="$1" &&
+ shift &&
+ git rev-parse "$@" >expect &&
+ git rev-list $rev_list_args --all >actual &&
+ test_cmp expect actual
+}
+
+test_expect_success setup '
+
+ touch file &&
+ git add file &&
+
+ test_commit one &&
+
+ test_tick=$(($test_tick - 2400)) &&
+
+ test_commit two &&
+ test_commit three &&
+ test_commit four &&
+
+ git log --pretty=oneline --abbrev-commit
+'
+
+test_expect_success 'one is ancestor of others and should not be shown' '
+
+ git rev-list one --not four >result &&
+ test_must_be_empty result
+
+'
+
+test_expect_success 'setup roots, merges and octopuses' '
+
+ git checkout --orphan newroot &&
+ test_commit five &&
+ git checkout -b sidebranch two &&
+ test_commit six &&
+ git checkout -b anotherbranch three &&
+ test_commit seven &&
+ git checkout -b yetanotherbranch four &&
+ test_commit eight &&
+ git checkout main &&
+ test_tick &&
+ git merge --allow-unrelated-histories -m normalmerge newroot &&
+ git tag normalmerge &&
+ test_tick &&
+ git merge -m tripus sidebranch anotherbranch &&
+ git tag tripus &&
+ git checkout -b tetrabranch normalmerge &&
+ test_tick &&
+ git merge -m tetrapus sidebranch anotherbranch yetanotherbranch &&
+ git tag tetrapus &&
+ git checkout main
+'
+
+test_expect_success 'rev-list roots' '
+
+ check_revlist "--max-parents=0" one five
+'
+
+test_expect_success 'rev-list no merges' '
+
+ check_revlist "--max-parents=1" one eight seven six five four three two &&
+ check_revlist "--no-merges" one eight seven six five four three two
+'
+
+test_expect_success 'rev-list no octopuses' '
+
+ check_revlist "--max-parents=2" one normalmerge eight seven six five four three two
+'
+
+test_expect_success 'rev-list no roots' '
+
+ check_revlist "--min-parents=1" tetrapus tripus normalmerge eight seven six four three two
+'
+
+test_expect_success 'rev-list merges' '
+
+ check_revlist "--min-parents=2" tetrapus tripus normalmerge &&
+ check_revlist "--merges" tetrapus tripus normalmerge
+'
+
+test_expect_success 'rev-list octopus' '
+
+ check_revlist "--min-parents=3" tetrapus tripus
+'
+
+test_expect_success 'rev-list ordinary commits' '
+
+ check_revlist "--min-parents=1 --max-parents=1" eight seven six four three two
+'
+
+test_expect_success 'rev-list --merges --no-merges yields empty set' '
+
+ check_revlist "--min-parents=2 --no-merges" &&
+ check_revlist "--merges --no-merges" &&
+ check_revlist "--no-merges --merges"
+'
+
+test_expect_success 'rev-list override and infinities' '
+
+ check_revlist "--min-parents=2 --max-parents=1 --max-parents=3" tripus normalmerge &&
+ check_revlist "--min-parents=1 --min-parents=2 --max-parents=7" tetrapus tripus normalmerge &&
+ check_revlist "--min-parents=2 --max-parents=8" tetrapus tripus normalmerge &&
+ check_revlist "--min-parents=2 --max-parents=-1" tetrapus tripus normalmerge &&
+ check_revlist "--min-parents=2 --no-max-parents" tetrapus tripus normalmerge &&
+ check_revlist "--max-parents=0 --min-parents=1 --no-min-parents" one five
+'
+
+test_expect_success 'dodecapus' '
+
+ roots= &&
+ for i in 1 2 3 4 5 6 7 8 9 10 11
+ do
+ git checkout -b root$i five &&
+ test_commit $i &&
+ roots="$roots root$i" ||
+ return 1
+ done &&
+ git checkout main &&
+ test_tick &&
+ git merge -m dodecapus $roots &&
+ git tag dodecapus &&
+
+ check_revlist "--min-parents=4" dodecapus tetrapus &&
+ check_revlist "--min-parents=8" dodecapus &&
+ check_revlist "--min-parents=12" dodecapus &&
+ check_revlist "--min-parents=13" &&
+ check_revlist "--min-parents=4 --max-parents=11" tetrapus
+'
+
+test_expect_success 'ancestors with the same commit time' '
+
+ test_tick_keep=$test_tick &&
+ for i in 1 2 3 4 5 6 7 8; do
+ test_tick=$test_tick_keep &&
+ test_commit t$i || return 1
+ done &&
+ git rev-list t1^! --not t$i >result &&
+ test_must_be_empty result
+'
+
+test_done
diff --git a/t/t6010-merge-base.sh b/t/t6010-merge-base.sh
new file mode 100755
index 0000000..44c726e
--- /dev/null
+++ b/t/t6010-merge-base.sh
@@ -0,0 +1,308 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+
+test_description='Merge base and parent list computation.
+'
+
+. ./test-lib.sh
+
+M=1130000000
+Z=+0000
+
+GIT_COMMITTER_EMAIL=git@comm.iter.xz
+GIT_COMMITTER_NAME='C O Mmiter'
+GIT_AUTHOR_NAME='A U Thor'
+GIT_AUTHOR_EMAIL=git@au.thor.xz
+export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
+
+doit () {
+ OFFSET=$1 &&
+ NAME=$2 &&
+ shift 2 &&
+
+ PARENTS= &&
+ for P
+ do
+ PARENTS="${PARENTS}-p $P "
+ done &&
+
+ GIT_COMMITTER_DATE="$(($M + $OFFSET)) $Z" &&
+ GIT_AUTHOR_DATE=$GIT_COMMITTER_DATE &&
+ export GIT_COMMITTER_DATE GIT_AUTHOR_DATE &&
+
+ commit=$(echo $NAME | git commit-tree $T $PARENTS) &&
+
+ git update-ref "refs/tags/$NAME" "$commit" &&
+ echo $commit
+}
+
+test_expect_success 'setup' '
+ T=$(git mktree </dev/null)
+'
+
+test_expect_success 'set up G and H' '
+ # E---D---C---B---A
+ # \"-_ \ \
+ # \ `---------G \
+ # \ \
+ # F----------------H
+ E=$(doit 5 E) &&
+ D=$(doit 4 D $E) &&
+ F=$(doit 6 F $E) &&
+ C=$(doit 3 C $D) &&
+ B=$(doit 2 B $C) &&
+ A=$(doit 1 A $B) &&
+ G=$(doit 7 G $B $E) &&
+ H=$(doit 8 H $A $F)
+'
+
+test_expect_success 'merge-base G H' '
+ git name-rev $B >expected &&
+
+ MB=$(git merge-base G H) &&
+ git name-rev "$MB" >actual.single &&
+
+ MB=$(git merge-base --all G H) &&
+ git name-rev "$MB" >actual.all &&
+
+ MB=$(git show-branch --merge-base G H) &&
+ git name-rev "$MB" >actual.sb &&
+
+ test_cmp expected actual.single &&
+ test_cmp expected actual.all &&
+ test_cmp expected actual.sb
+'
+
+test_expect_success 'merge-base/show-branch --independent' '
+ git name-rev "$H" >expected1 &&
+ git name-rev "$H" "$G" >expected2 &&
+
+ parents=$(git merge-base --independent H) &&
+ git name-rev $parents >actual1.mb &&
+ parents=$(git merge-base --independent A H G) &&
+ git name-rev $parents >actual2.mb &&
+
+ parents=$(git show-branch --independent H) &&
+ git name-rev $parents >actual1.sb &&
+ parents=$(git show-branch --independent A H G) &&
+ git name-rev $parents >actual2.sb &&
+
+ test_cmp expected1 actual1.mb &&
+ test_cmp expected2 actual2.mb &&
+ test_cmp expected1 actual1.sb &&
+ test_cmp expected2 actual2.sb
+'
+
+test_expect_success 'unsynchronized clocks' '
+ # This test is to demonstrate that relying on timestamps in a distributed
+ # SCM to provide a _consistent_ partial ordering of commits leads to
+ # insanity.
+ #
+ # Relative
+ # Structure timestamps
+ #
+ # PL PR +4 +4
+ # / \/ \ / \/ \
+ # L2 C2 R2 +3 -1 +3
+ # | | | | | |
+ # L1 C1 R1 +2 -2 +2
+ # | | | | | |
+ # L0 C0 R0 +1 -3 +1
+ # \ | / \ | /
+ # S 0
+ #
+ # The left and right chains of commits can be of any length and complexity as
+ # long as all of the timestamps are greater than that of S.
+
+ S=$(doit 0 S) &&
+
+ C0=$(doit -3 C0 $S) &&
+ C1=$(doit -2 C1 $C0) &&
+ C2=$(doit -1 C2 $C1) &&
+
+ L0=$(doit 1 L0 $S) &&
+ L1=$(doit 2 L1 $L0) &&
+ L2=$(doit 3 L2 $L1) &&
+
+ R0=$(doit 1 R0 $S) &&
+ R1=$(doit 2 R1 $R0) &&
+ R2=$(doit 3 R2 $R1) &&
+
+ PL=$(doit 4 PL $L2 $C2) &&
+ PR=$(doit 4 PR $C2 $R2) &&
+
+ git name-rev $C2 >expected &&
+
+ MB=$(git merge-base PL PR) &&
+ git name-rev "$MB" >actual.single &&
+
+ MB=$(git merge-base --all PL PR) &&
+ git name-rev "$MB" >actual.all &&
+
+ test_cmp expected actual.single &&
+ test_cmp expected actual.all
+'
+
+test_expect_success '--independent with unsynchronized clocks' '
+ IB=$(doit 0 IB) &&
+ I1=$(doit -10 I1 $IB) &&
+ I2=$(doit -9 I2 $I1) &&
+ I3=$(doit -8 I3 $I2) &&
+ I4=$(doit -7 I4 $I3) &&
+ I5=$(doit -6 I5 $I4) &&
+ I6=$(doit -5 I6 $I5) &&
+ I7=$(doit -4 I7 $I6) &&
+ I8=$(doit -3 I8 $I7) &&
+ IH=$(doit -2 IH $I8) &&
+
+ echo $IH >expected &&
+ git merge-base --independent IB IH >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'merge-base for octopus-step (setup)' '
+ # Another set to demonstrate base between one commit and a merge
+ # in the documentation.
+ #
+ # * C (MMC) * B (MMB) * A (MMA)
+ # * o * o * o
+ # * o * o * o
+ # * o * o * o
+ # * o | _______/
+ # | |/
+ # | * 1 (MM1)
+ # | _______/
+ # |/
+ # * root (MMR)
+
+ test_commit MMR &&
+ test_commit MM1 &&
+ test_commit MM-o &&
+ test_commit MM-p &&
+ test_commit MM-q &&
+ test_commit MMA &&
+ git checkout MM1 &&
+ test_commit MM-r &&
+ test_commit MM-s &&
+ test_commit MM-t &&
+ test_commit MMB &&
+ git checkout MMR &&
+ test_commit MM-u &&
+ test_commit MM-v &&
+ test_commit MM-w &&
+ test_commit MM-x &&
+ test_commit MMC
+'
+
+test_expect_success 'merge-base A B C' '
+ git rev-parse --verify MM1 >expected &&
+ git rev-parse --verify MMR >expected.sb &&
+
+ git merge-base --all MMA MMB MMC >actual &&
+ git merge-base --all --octopus MMA MMB MMC >actual.common &&
+ git show-branch --merge-base MMA MMB MMC >actual.sb &&
+
+ test_cmp expected actual &&
+ test_cmp expected.sb actual.common &&
+ test_cmp expected.sb actual.sb
+'
+
+test_expect_success 'criss-cross merge-base for octopus-step' '
+ git reset --hard MMR &&
+ test_commit CC1 &&
+ git reset --hard E &&
+ test_commit CC2 &&
+ test_tick &&
+ # E is a root commit unrelated to MMR root on which CC1 is based
+ git merge -s ours --allow-unrelated-histories CC1 &&
+ test_commit CC-o &&
+ test_commit CCB &&
+ git reset --hard CC1 &&
+ # E is a root commit unrelated to MMR root on which CC1 is based
+ git merge -s ours --allow-unrelated-histories CC2 &&
+ test_commit CCA &&
+
+ git rev-parse CC1 CC2 >expected &&
+ git merge-base --all CCB CCA^^ CCA^^2 >actual &&
+
+ sort expected >expected.sorted &&
+ sort actual >actual.sorted &&
+ test_cmp expected.sorted actual.sorted
+'
+
+test_expect_success 'using reflog to find the fork point' '
+ git reset --hard &&
+ git checkout -b base $E &&
+
+ (
+ for count in 1 2 3
+ do
+ git commit --allow-empty -m "Base commit #$count" &&
+ git rev-parse HEAD >expect$count &&
+ git checkout -B derived &&
+ git commit --allow-empty -m "Derived #$count" &&
+ git rev-parse HEAD >derived$count &&
+ git checkout -B base $E || exit 1
+ done &&
+
+ for count in 1 2 3
+ do
+ git merge-base --fork-point base $(cat derived$count) >actual &&
+ test_cmp expect$count actual || exit 1
+ done
+
+ ) &&
+ # check that we correctly default to HEAD
+ git checkout derived &&
+ git merge-base --fork-point base >actual &&
+ test_cmp expect3 actual
+'
+
+test_expect_success '--fork-point works with empty reflog' '
+ git -c core.logallrefupdates=false branch no-reflog base &&
+ git merge-base --fork-point no-reflog derived &&
+ test_cmp expect3 actual
+'
+
+test_expect_success 'merge-base --octopus --all for complex tree' '
+ # Best common ancestor for JE, JAA and JDD is JC
+ # JE
+ # / |
+ # / |
+ # / |
+ # JAA / |
+ # |\ / |
+ # | \ | JDD |
+ # | \ |/ | |
+ # | JC JD |
+ # | | /| |
+ # | |/ | |
+ # JA | | |
+ # |\ /| | |
+ # X JB | X X
+ # \ \ | / /
+ # \__\|/___/
+ # J
+ test_commit J &&
+ test_commit JB &&
+ git reset --hard J &&
+ test_commit JC &&
+ git reset --hard J &&
+ test_commit JTEMP1 &&
+ test_merge JA JB &&
+ test_merge JAA JC &&
+ git reset --hard J &&
+ test_commit JTEMP2 &&
+ test_merge JD JB &&
+ test_merge JDD JC &&
+ git reset --hard J &&
+ test_commit JTEMP3 &&
+ test_merge JE JC &&
+ git rev-parse JC >expected &&
+ git merge-base --all --octopus JAA JDD JE >actual &&
+ test_cmp expected actual
+'
+
+test_done
diff --git a/t/t6011-rev-list-with-bad-commit.sh b/t/t6011-rev-list-with-bad-commit.sh
new file mode 100755
index 0000000..b2e422c
--- /dev/null
+++ b/t/t6011-rev-list-with-bad-commit.sh
@@ -0,0 +1,60 @@
+#!/bin/sh
+
+test_description='git rev-list should notice bad commits'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# Note:
+# - compression level is set to zero to make "corruptions" easier to perform
+# - reflog is disabled to avoid extra references which would twart the test
+
+test_expect_success 'setup' \
+ '
+ git init &&
+ git config core.compression 0 &&
+ git config core.logallrefupdates false &&
+ echo "foo" > foo &&
+ git add foo &&
+ git commit -m "first commit" &&
+ echo "bar" > bar &&
+ git add bar &&
+ git commit -m "second commit" &&
+ echo "baz" > baz &&
+ git add baz &&
+ git commit -m "third commit" &&
+ echo "foo again" >> foo &&
+ git add foo &&
+ git commit -m "fourth commit" &&
+ git repack -a -f -d
+ '
+
+test_expect_success 'verify number of revisions' \
+ '
+ revs=$(git rev-list --all | wc -l) &&
+ test $revs -eq 4 &&
+ first_commit=$(git rev-parse HEAD~3)
+ '
+
+test_expect_success 'corrupt second commit object' \
+ '
+ perl -i.bak -pe "s/second commit/socond commit/" .git/objects/pack/*.pack &&
+ test_must_fail git fsck --full
+ '
+
+test_expect_success 'rev-list should fail' '
+ test_must_fail env GIT_TEST_COMMIT_GRAPH=0 git -c core.commitGraph=false rev-list --all > /dev/null
+'
+
+test_expect_success 'git repack _MUST_ fail' \
+ '
+ test_must_fail git repack -a -f -d
+ '
+
+test_expect_success 'first commit is still available' \
+ '
+ git log $first_commit
+ '
+
+test_done
+
diff --git a/t/t6012-rev-list-simplify.sh b/t/t6012-rev-list-simplify.sh
new file mode 100755
index 0000000..de1e87f
--- /dev/null
+++ b/t/t6012-rev-list-simplify.sh
@@ -0,0 +1,288 @@
+#!/bin/sh
+
+test_description='merge simplification'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+note () {
+ git tag "$1"
+}
+
+unnote () {
+ test_when_finished "rm -f tmp" &&
+ git name-rev --tags --annotate-stdin >tmp &&
+ sed -e "s|$OID_REGEX (tags/\([^)]*\)) |\1 |g" <tmp
+}
+
+#
+# Create a test repo with an interesting commit graph:
+#
+# A-----B-----G--H--I--K--L
+# \ \ / /
+# \ \ / /
+# C--D--E--F J
+#
+# The commits are laid out from left-to-right starting with
+# the root commit A and terminating at the tip commit L.
+#
+# There are a few places where we adjust the commit date or
+# author date to make the --topo-order, --date-order, and
+# --author-date-order flags produce different output.
+
+test_expect_success setup '
+ echo "Hi there" >file &&
+ echo "initial" >lost &&
+ git add file lost &&
+ test_tick && git commit -m "Initial file and lost" &&
+ note A &&
+
+ git branch other-branch &&
+
+ git symbolic-ref HEAD refs/heads/unrelated &&
+ git rm -f "*" &&
+ echo "Unrelated branch" >side &&
+ git add side &&
+ test_tick && git commit -m "Side root" &&
+ note J &&
+ git checkout main &&
+
+ echo "Hello" >file &&
+ echo "second" >lost &&
+ git add file lost &&
+ test_tick && GIT_AUTHOR_DATE=$(($test_tick + 120)) git commit -m "Modified file and lost" &&
+ note B &&
+
+ git checkout other-branch &&
+
+ echo "Hello" >file &&
+ >lost &&
+ git add file lost &&
+ test_tick && git commit -m "Modified the file identically" &&
+ note C &&
+
+ echo "This is a stupid example" >another-file &&
+ git add another-file &&
+ test_tick && git commit -m "Add another file" &&
+ note D &&
+
+ test_tick &&
+ test_must_fail git merge -m "merge" main &&
+ >lost && git commit -a -m "merge" &&
+ note E &&
+
+ echo "Yet another" >elif &&
+ git add elif &&
+ test_tick && git commit -m "Irrelevant change" &&
+ note F &&
+
+ git checkout main &&
+ echo "Yet another" >elif &&
+ git add elif &&
+ test_tick && git commit -m "Another irrelevant change" &&
+ note G &&
+
+ test_tick && git merge -m "merge" other-branch &&
+ note H &&
+
+ echo "Final change" >file &&
+ test_tick && git commit -a -m "Final change" &&
+ note I &&
+
+ git checkout main &&
+ test_tick && git merge --allow-unrelated-histories -m "Coolest" unrelated &&
+ note K &&
+
+ echo "Immaterial" >elif &&
+ git add elif &&
+ test_tick && git commit -m "Last" &&
+ note L
+'
+
+FMT='tformat:%P %H | %s'
+
+check_outcome () {
+ outcome=$1
+ shift
+ for c in $1
+ do
+ echo "$c"
+ done >expect &&
+ shift &&
+ param="$*" &&
+ test_expect_$outcome "log $param" '
+ git log --pretty="$FMT" --parents $param >out &&
+ unnote >actual <out &&
+ sed -e "s/^.* \([^ ]*\) .*/\1/" >check <actual &&
+ test_cmp expect check
+ '
+}
+
+check_result () {
+ check_outcome success "$@"
+}
+
+check_result 'L K J I H F E D C G B A' --full-history --topo-order
+check_result 'L K I H G F E D C B J A' --full-history
+check_result 'L K I H G F E D C B J A' --full-history --date-order
+check_result 'L K I H G F E D B C J A' --full-history --author-date-order
+check_result 'K I H E C B A' --full-history -- file
+check_result 'K I H E C B A' --full-history --topo-order -- file
+check_result 'K I H E C B A' --full-history --date-order -- file
+check_result 'K I H E B C A' --full-history --author-date-order -- file
+check_result 'I E C B A' --simplify-merges -- file
+check_result 'I E C B A' --simplify-merges --topo-order -- file
+check_result 'I E C B A' --simplify-merges --date-order -- file
+check_result 'I E B C A' --simplify-merges --author-date-order -- file
+check_result 'I B A' -- file
+check_result 'I B A' --topo-order -- file
+check_result 'I B A' --date-order -- file
+check_result 'I B A' --author-date-order -- file
+check_result 'H' --first-parent -- another-file
+check_result 'H' --first-parent --topo-order -- another-file
+
+check_result 'L K I H G B A' --first-parent L
+check_result 'F E D C' --exclude-first-parent-only F ^L
+check_result '' F ^L
+check_result 'L K I H G J' L ^F
+check_result 'L K I H G B J' --exclude-first-parent-only L ^F
+check_result 'L K I H G B' --exclude-first-parent-only --first-parent L ^F
+
+check_result 'E C B A' --full-history E -- lost
+test_expect_success 'full history simplification without parent' '
+ printf "%s\n" E C B A >expect &&
+ git log --pretty="$FMT" --full-history E -- lost >out &&
+ unnote >actual <out &&
+ sed -e "s/^.* \([^ ]*\) .*/\1/" >check <actual &&
+ test_cmp expect check
+'
+
+test_expect_success '--full-diff is not affected by --parents' '
+ git log -p --pretty="%H" --full-diff -- file >expected &&
+ git log -p --pretty="%H" --full-diff --parents -- file >actual &&
+ test_cmp expected actual
+'
+
+#
+# Create a new history to demonstrate the value of --show-pulls
+# with respect to the subtleties of simplified history, --full-history,
+# and --simplify-merges.
+#
+# .-A---M-----C--N---O---P
+# / / \ \ \/ / /
+# I B \ R-'`-Z' /
+# \ / \/ /
+# \ / /\ /
+# `---X--' `---Y--'
+#
+# This example is explained in Documentation/rev-list-options.txt
+
+test_expect_success 'setup rebuild repo' '
+ rm -rf .git * &&
+ git init &&
+ git switch -c topic &&
+
+ echo base >file &&
+ git add file &&
+ test_commit I &&
+
+ echo A >file &&
+ git add file &&
+ test_commit A &&
+
+ git switch -c branchB I &&
+ echo B >file &&
+ git add file &&
+ test_commit B &&
+
+ git switch topic &&
+ test_must_fail git merge -m "M" B &&
+ echo A >file &&
+ echo B >>file &&
+ git add file &&
+ git merge --continue &&
+ note M &&
+
+ echo C >other &&
+ git add other &&
+ test_commit C &&
+
+ git switch -c branchX I &&
+ echo X >file &&
+ git add file &&
+ test_commit X &&
+
+ git switch -c branchR M &&
+ git merge -m R -Xtheirs X &&
+ note R &&
+
+ git switch topic &&
+ git merge -m N R &&
+ note N &&
+
+ git switch -c branchY M &&
+ echo Y >y &&
+ git add y &&
+ test_commit Y &&
+
+ git switch -c branchZ C &&
+ echo Z >z &&
+ git add z &&
+ test_commit Z &&
+
+ git switch topic &&
+ git merge -m O Z &&
+ note O &&
+
+ git merge -m P Y &&
+ note P
+'
+
+check_result 'X I' -- file
+check_result 'N R X I' --show-pulls -- file
+
+check_result 'P O N R X M B A I' --full-history --topo-order -- file
+check_result 'N R X M B A I' --simplify-merges --topo-order --show-pulls -- file
+check_result 'R X M B A I' --simplify-merges --topo-order -- file
+check_result 'N M A I' --first-parent -- file
+check_result 'N M A I' --first-parent --show-pulls -- file
+
+# --ancestry-path implies --full-history
+check_result 'P O N R M' --topo-order \
+ --ancestry-path A..HEAD -- file
+check_result 'P O N R M' --topo-order \
+ --show-pulls \
+ --ancestry-path A..HEAD -- file
+check_result 'P O N R M' --topo-order \
+ --full-history \
+ --ancestry-path A..HEAD -- file
+check_result 'R M' --topo-order \
+ --simplify-merges \
+ --ancestry-path A..HEAD -- file
+check_result 'N R M' --topo-order \
+ --simplify-merges --show-pulls \
+ --ancestry-path A..HEAD -- file
+
+test_expect_success 'log --graph --simplify-merges --show-pulls' '
+ cat >expect <<-\EOF &&
+ * N
+ * R
+ |\
+ | * X
+ * | M
+ |\ \
+ | * | B
+ | |/
+ * / A
+ |/
+ * I
+ EOF
+ git log --graph --pretty="%s" \
+ --simplify-merges --show-pulls \
+ -- file >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t6013-rev-list-reverse-parents.sh b/t/t6013-rev-list-reverse-parents.sh
new file mode 100755
index 0000000..39793cb
--- /dev/null
+++ b/t/t6013-rev-list-reverse-parents.sh
@@ -0,0 +1,45 @@
+#!/bin/sh
+
+test_description='--reverse combines with --parents'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+
+commit () {
+ test_tick &&
+ echo $1 > foo &&
+ git add foo &&
+ git commit -m "$1"
+}
+
+test_expect_success 'set up --reverse example' '
+ commit one &&
+ git tag root &&
+ commit two &&
+ git checkout -b side HEAD^ &&
+ commit three &&
+ git checkout main &&
+ git merge -s ours side &&
+ commit five
+ '
+
+test_expect_success '--reverse --parents --full-history combines correctly' '
+ git rev-list --parents --full-history main -- foo |
+ perl -e "print reverse <>" > expected &&
+ git rev-list --reverse --parents --full-history main -- foo \
+ > actual &&
+ test_cmp expected actual
+ '
+
+test_expect_success '--boundary does too' '
+ git rev-list --boundary --parents --full-history main ^root -- foo |
+ perl -e "print reverse <>" > expected &&
+ git rev-list --boundary --reverse --parents --full-history \
+ main ^root -- foo > actual &&
+ test_cmp expected actual
+ '
+
+test_done
diff --git a/t/t6014-rev-list-all.sh b/t/t6014-rev-list-all.sh
new file mode 100755
index 0000000..16b8bd1
--- /dev/null
+++ b/t/t6014-rev-list-all.sh
@@ -0,0 +1,43 @@
+#!/bin/sh
+
+test_description='--all includes detached HEADs'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+
+commit () {
+ test_tick &&
+ echo $1 > foo &&
+ git add foo &&
+ git commit -m "$1"
+}
+
+test_expect_success 'setup' '
+
+ commit one &&
+ commit two &&
+ git checkout HEAD^ &&
+ commit detached
+
+'
+
+test_expect_success 'rev-list --all lists detached HEAD' '
+
+ test 3 = $(git rev-list --all | wc -l)
+
+'
+
+test_expect_success 'repack does not lose detached HEAD' '
+
+ git gc &&
+ git prune --expire=now &&
+ git show HEAD
+
+'
+
+test_expect_success 'rev-list --graph --no-walk is forbidden' '
+ test_must_fail git rev-list --graph --no-walk HEAD
+'
+
+test_done
diff --git a/t/t6016-rev-list-graph-simplify-history.sh b/t/t6016-rev-list-graph-simplify-history.sh
new file mode 100755
index 0000000..54b0a6f
--- /dev/null
+++ b/t/t6016-rev-list-graph-simplify-history.sh
@@ -0,0 +1,246 @@
+#!/bin/sh
+
+# There's more than one "correct" way to represent the history graphically.
+# These tests depend on the current behavior of the graphing code. If the
+# graphing code is ever changed to draw the output differently, these tests
+# cases will need to be updated to know about the new layout.
+
+test_description='--graph and simplified history'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-log-graph.sh
+
+check_graph () {
+ cat >expect &&
+ lib_test_cmp_graph --format=%s "$@"
+}
+
+test_expect_success 'set up rev-list --graph test' '
+ # 3 commits on branch A
+ test_commit A1 foo.txt &&
+ test_commit A2 bar.txt &&
+ test_commit A3 bar.txt &&
+ git branch -m main A &&
+
+ # 2 commits on branch B, started from A1
+ git checkout -b B A1 &&
+ test_commit B1 foo.txt &&
+ test_commit B2 abc.txt &&
+
+ # 2 commits on branch C, started from A2
+ git checkout -b C A2 &&
+ test_commit C1 xyz.txt &&
+ test_commit C2 xyz.txt &&
+
+ # Octopus merge B and C into branch A
+ git checkout A &&
+ git merge B C -m A4 &&
+ git tag A4 &&
+
+ test_commit A5 bar.txt &&
+
+ # More commits on C, then merge C into A
+ git checkout C &&
+ test_commit C3 foo.txt &&
+ test_commit C4 bar.txt &&
+ git checkout A &&
+ git merge -s ours C -m A6 &&
+ git tag A6 &&
+
+ test_commit A7 bar.txt
+'
+
+test_expect_success '--graph --all' '
+ check_graph --all <<-\EOF
+ * A7
+ * A6
+ |\
+ | * C4
+ | * C3
+ * | A5
+ | |
+ | \
+ *-. | A4
+ |\ \|
+ | | * C2
+ | | * C1
+ | * | B2
+ | * | B1
+ * | | A3
+ | |/
+ |/|
+ * | A2
+ |/
+ * A1
+ EOF
+'
+
+# Make sure the graph_is_interesting() code still realizes
+# that undecorated merges are interesting, even with --simplify-by-decoration
+test_expect_success '--graph --simplify-by-decoration' '
+ git tag -d A4 &&
+ check_graph --all --simplify-by-decoration <<-\EOF
+ * A7
+ * A6
+ |\
+ | * C4
+ | * C3
+ * | A5
+ | |
+ | \
+ *-. | A4
+ |\ \|
+ | | * C2
+ | | * C1
+ | * | B2
+ | * | B1
+ * | | A3
+ | |/
+ |/|
+ * | A2
+ |/
+ * A1
+ EOF
+'
+
+test_expect_success 'setup: get rid of decorations on B' '
+ git tag -d B2 &&
+ git tag -d B1 &&
+ git branch -d B
+'
+
+# Graph with branch B simplified away
+test_expect_success '--graph --simplify-by-decoration prune branch B' '
+ check_graph --simplify-by-decoration --all <<-\EOF
+ * A7
+ * A6
+ |\
+ | * C4
+ | * C3
+ * | A5
+ * | A4
+ |\|
+ | * C2
+ | * C1
+ * | A3
+ |/
+ * A2
+ * A1
+ EOF
+'
+
+test_expect_success '--graph --full-history -- bar.txt' '
+ check_graph --full-history --all -- bar.txt <<-\EOF
+ * A7
+ * A6
+ |\
+ | * C4
+ * | A5
+ * | A4
+ |\|
+ * | A3
+ |/
+ * A2
+ EOF
+'
+
+test_expect_success '--graph --full-history --simplify-merges -- bar.txt' '
+ check_graph --full-history --simplify-merges --all -- bar.txt <<-\EOF
+ * A7
+ * A6
+ |\
+ | * C4
+ * | A5
+ * | A3
+ |/
+ * A2
+ EOF
+'
+
+test_expect_success '--graph -- bar.txt' '
+ check_graph --all -- bar.txt <<-\EOF
+ * A7
+ * A5
+ * A3
+ | * C4
+ |/
+ * A2
+ EOF
+'
+
+test_expect_success '--graph --sparse -- bar.txt' '
+ check_graph --sparse --all -- bar.txt <<-\EOF
+ * A7
+ * A6
+ * A5
+ * A4
+ * A3
+ | * C4
+ | * C3
+ | * C2
+ | * C1
+ |/
+ * A2
+ * A1
+ EOF
+'
+
+test_expect_success '--graph ^C4' '
+ check_graph --all ^C4 <<-\EOF
+ * A7
+ * A6
+ * A5
+ * A4
+ |\
+ | * B2
+ | * B1
+ * A3
+ EOF
+'
+
+test_expect_success '--graph ^C3' '
+ check_graph --all ^C3 <<-\EOF
+ * A7
+ * A6
+ |\
+ | * C4
+ * A5
+ * A4
+ |\
+ | * B2
+ | * B1
+ * A3
+ EOF
+'
+
+# I don't think the ordering of the boundary commits is really
+# that important, but this test depends on it. If the ordering ever changes
+# in the code, we'll need to update this test.
+test_expect_success '--graph --boundary ^C3' '
+ check_graph --boundary --all ^C3 <<-\EOF
+ * A7
+ * A6
+ |\
+ | * C4
+ * | A5
+ | |
+ | \
+ *-. \ A4
+ |\ \ \
+ | * | | B2
+ | * | | B1
+ * | | | A3
+ o | | | A2
+ |/ / /
+ o / / A1
+ / /
+ | o C3
+ |/
+ o C2
+ EOF
+'
+
+test_done
diff --git a/t/t6017-rev-list-stdin.sh b/t/t6017-rev-list-stdin.sh
new file mode 100755
index 0000000..4821b90
--- /dev/null
+++ b/t/t6017-rev-list-stdin.sh
@@ -0,0 +1,151 @@
+#!/bin/sh
+#
+# Copyright (c) 2009, Junio C Hamano
+#
+
+test_description='log family learns --stdin'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+check () {
+ for cmd in rev-list "log --stat"
+ do
+ for i in "$@"
+ do
+ printf "%s\n" $i
+ done >input &&
+ test_expect_success "check $cmd $*" '
+ git $cmd $(cat input) >expect &&
+ git $cmd --stdin <input >actual &&
+ sed -e "s/^/input /" input &&
+ sed -e "s/^/output /" expect &&
+ test_cmp expect actual
+ '
+ done
+}
+
+them='1 2 3 4 5 6 7'
+
+test_expect_success setup '
+ (
+ for i in 0 $them
+ do
+ for j in $them
+ do
+ echo $i.$j >file-$j &&
+ git add file-$j || exit
+ done &&
+ test_tick &&
+ git commit -m $i || exit
+ done &&
+ for i in $them
+ do
+ git checkout -b side-$i main~$i &&
+ echo updated $i >file-$i &&
+ git add file-$i &&
+ test_tick &&
+ git commit -m side-$i || exit
+ done &&
+
+ git update-ref refs/heads/-dashed-branch HEAD
+ )
+'
+
+check main
+check side-1 ^side-4
+check side-1 ^side-7 --
+check side-1 ^side-7 -- file-1
+check side-1 ^side-7 -- file-2
+check side-3 ^side-4 -- file-3
+check side-3 ^side-2
+check side-3 ^side-2 -- file-1
+check --all
+check --all --not --branches
+check --glob=refs/heads
+check --glob=refs/heads --
+check --glob=refs/heads -- file-1
+check --end-of-options -dashed-branch
+check --all --not refs/heads/main
+
+test_expect_success 'not only --stdin' '
+ cat >expect <<-EOF &&
+ 7
+
+ file-1
+ file-2
+ EOF
+ cat >input <<-EOF &&
+ ^main^
+ --
+ file-2
+ EOF
+ git log --pretty=tformat:%s --name-only --stdin main -- file-1 \
+ <input >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'pseudo-opt with missing value' '
+ cat >input <<-EOF &&
+ --glob
+ refs/heads
+ EOF
+
+ cat >expect <<-EOF &&
+ fatal: Option ${SQ}--glob${SQ} requires a value
+ EOF
+
+ test_must_fail git rev-list --stdin <input 2>error &&
+ test_cmp expect error
+'
+
+test_expect_success 'pseudo-opt with invalid value' '
+ cat >input <<-EOF &&
+ --no-walk=garbage
+ EOF
+
+ cat >expect <<-EOF &&
+ error: invalid argument to --no-walk
+ fatal: invalid option ${SQ}--no-walk=garbage${SQ} in --stdin mode
+ EOF
+
+ test_must_fail git rev-list --stdin <input 2>error &&
+ test_cmp expect error
+'
+
+test_expect_success 'unknown option without --end-of-options' '
+ cat >input <<-EOF &&
+ -dashed-branch
+ EOF
+
+ cat >expect <<-EOF &&
+ fatal: invalid option ${SQ}-dashed-branch${SQ} in --stdin mode
+ EOF
+
+ test_must_fail git rev-list --stdin <input 2>error &&
+ test_cmp expect error
+'
+
+test_expect_success '--not on command line does not influence revisions read via --stdin' '
+ cat >input <<-EOF &&
+ refs/heads/main
+ EOF
+ git rev-list refs/heads/main >expect &&
+
+ git rev-list refs/heads/main --not --stdin <input >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--not via stdin does not influence revisions from command line' '
+ cat >input <<-EOF &&
+ --not
+ EOF
+ git rev-list refs/heads/main >expect &&
+
+ git rev-list refs/heads/main --stdin refs/heads/main <input >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t6018-rev-list-glob.sh b/t/t6018-rev-list-glob.sh
new file mode 100755
index 0000000..67d523d
--- /dev/null
+++ b/t/t6018-rev-list-glob.sh
@@ -0,0 +1,429 @@
+#!/bin/sh
+
+test_description='rev-list/rev-parse --glob'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+commit () {
+ test_tick &&
+ echo $1 > foo &&
+ git add foo &&
+ git commit -m "$1"
+}
+
+compare () {
+ # Split arguments on whitespace.
+ git $1 $2 >expected &&
+ git $1 $3 >actual &&
+ test_cmp expected actual
+}
+
+test_expect_success 'setup' '
+
+ commit main &&
+ git checkout -b subspace/one main &&
+ commit one &&
+ git checkout -b subspace/two main &&
+ commit two &&
+ git checkout -b subspace-x main &&
+ commit subspace-x &&
+ git checkout -b other/three main &&
+ commit three &&
+ git checkout -b someref main &&
+ commit some &&
+ git checkout main &&
+ commit topic_2 &&
+ git tag foo/bar main &&
+ commit topic_3 &&
+ git update-ref refs/remotes/foo/baz main &&
+ commit topic_4 &&
+ git update-ref refs/remotes/upstream/one subspace/one &&
+ git update-ref refs/remotes/upstream/two subspace/two &&
+ git update-ref refs/remotes/upstream/x subspace-x &&
+ git tag qux/one subspace/one &&
+ git tag qux/two subspace/two &&
+ git tag qux/x subspace-x
+'
+
+test_expect_success 'rev-parse --glob=refs/heads/subspace/*' '
+
+ compare rev-parse "subspace/one subspace/two" "--glob=refs/heads/subspace/*"
+
+'
+
+test_expect_success 'rev-parse --glob=heads/subspace/*' '
+
+ compare rev-parse "subspace/one subspace/two" "--glob=heads/subspace/*"
+
+'
+
+test_expect_success 'rev-parse --glob=refs/heads/subspace/' '
+
+ compare rev-parse "subspace/one subspace/two" "--glob=refs/heads/subspace/"
+
+'
+
+test_expect_success 'rev-parse --glob=heads/subspace/' '
+
+ compare rev-parse "subspace/one subspace/two" "--glob=heads/subspace/"
+
+'
+
+test_expect_success 'rev-parse --glob=heads/subspace' '
+
+ compare rev-parse "subspace/one subspace/two" "--glob=heads/subspace"
+
+'
+
+test_expect_failure 'rev-parse accepts --glob as detached option' '
+
+ compare rev-parse "subspace/one subspace/two" "--glob heads/subspace"
+
+'
+
+test_expect_failure 'rev-parse is not confused by option-like glob' '
+
+ compare rev-parse "main" "--glob --symbolic main"
+
+'
+
+test_expect_success 'rev-parse --branches=subspace/*' '
+
+ compare rev-parse "subspace/one subspace/two" "--branches=subspace/*"
+
+'
+
+test_expect_success 'rev-parse --branches=subspace/' '
+
+ compare rev-parse "subspace/one subspace/two" "--branches=subspace/"
+
+'
+
+test_expect_success 'rev-parse --branches=subspace' '
+
+ compare rev-parse "subspace/one subspace/two" "--branches=subspace"
+
+'
+
+test_expect_success 'rev-parse --glob=heads/subspace/* --glob=heads/other/*' '
+
+ compare rev-parse "subspace/one subspace/two other/three" "--glob=heads/subspace/* --glob=heads/other/*"
+
+'
+
+test_expect_success 'rev-parse --glob=heads/someref/* main' '
+
+ compare rev-parse "main" "--glob=heads/someref/* main"
+
+'
+
+test_expect_success 'rev-parse --glob=heads/*' '
+
+ compare rev-parse "main other/three someref subspace-x subspace/one subspace/two" "--glob=heads/*"
+
+'
+
+test_expect_success 'rev-parse --tags=foo' '
+
+ compare rev-parse "foo/bar" "--tags=foo"
+
+'
+
+test_expect_success 'rev-parse --remotes=foo' '
+
+ compare rev-parse "foo/baz" "--remotes=foo"
+
+'
+
+test_expect_success 'rev-parse --exclude with --branches' '
+ compare rev-parse "--exclude=*/* --branches" "main someref subspace-x"
+'
+
+test_expect_success 'rev-parse --exclude with --all' '
+ compare rev-parse "--exclude=refs/remotes/* --all" "--branches --tags"
+'
+
+test_expect_success 'rev-parse accumulates multiple --exclude' '
+ compare rev-parse "--exclude=refs/remotes/* --exclude=refs/tags/* --all" --branches
+'
+
+test_expect_success 'rev-parse --branches clears --exclude' '
+ compare rev-parse "--exclude=* --branches --branches" "--branches"
+'
+
+test_expect_success 'rev-parse --tags clears --exclude' '
+ compare rev-parse "--exclude=* --tags --tags" "--tags"
+'
+
+test_expect_success 'rev-parse --all clears --exclude' '
+ compare rev-parse "--exclude=* --all --all" "--all"
+'
+
+test_expect_success 'rev-parse --exclude=glob with --branches=glob' '
+ compare rev-parse "--exclude=subspace-* --branches=sub*" "subspace/one subspace/two"
+'
+
+test_expect_success 'rev-parse --exclude=glob with --tags=glob' '
+ compare rev-parse "--exclude=qux/? --tags=qux/*" "qux/one qux/two"
+'
+
+test_expect_success 'rev-parse --exclude=glob with --remotes=glob' '
+ compare rev-parse "--exclude=upstream/? --remotes=upstream/*" "upstream/one upstream/two"
+'
+
+test_expect_success 'rev-parse --exclude=ref with --branches=glob' '
+ compare rev-parse "--exclude=subspace-x --branches=sub*" "subspace/one subspace/two"
+'
+
+test_expect_success 'rev-parse --exclude=ref with --tags=glob' '
+ compare rev-parse "--exclude=qux/x --tags=qux/*" "qux/one qux/two"
+'
+
+test_expect_success 'rev-parse --exclude=ref with --remotes=glob' '
+ compare rev-parse "--exclude=upstream/x --remotes=upstream/*" "upstream/one upstream/two"
+'
+
+for section in fetch receive uploadpack
+do
+ test_expect_success "rev-parse --exclude-hidden=$section with --all" '
+ compare "-c transfer.hideRefs=refs/remotes/ rev-parse" "--branches --tags" "--exclude-hidden=$section --all"
+ '
+
+ test_expect_success "rev-parse --exclude-hidden=$section with --all" '
+ compare "-c transfer.hideRefs=refs/heads/subspace/ rev-parse" "--exclude=refs/heads/subspace/* --all" "--exclude-hidden=$section --all"
+ '
+
+ test_expect_success "rev-parse --exclude-hidden=$section with --glob" '
+ compare "-c transfer.hideRefs=refs/heads/subspace/ rev-parse" "--exclude=refs/heads/subspace/* --glob=refs/heads/*" "--exclude-hidden=$section --glob=refs/heads/*"
+ '
+
+ test_expect_success "rev-parse --exclude-hidden=$section can be passed once per pseudo-ref" '
+ compare "-c transfer.hideRefs=refs/remotes/ rev-parse" "--branches --tags --branches --tags" "--exclude-hidden=$section --all --exclude-hidden=$section --all"
+ '
+
+ test_expect_success "rev-parse --exclude-hidden=$section can only be passed once per pseudo-ref" '
+ echo "fatal: --exclude-hidden= passed more than once" >expected &&
+ test_must_fail git rev-parse --exclude-hidden=$section --exclude-hidden=$section 2>err &&
+ test_cmp expected err
+ '
+
+ for pseudoopt in branches tags remotes
+ do
+ test_expect_success "rev-parse --exclude-hidden=$section fails with --$pseudoopt" '
+ echo "error: --exclude-hidden cannot be used together with --$pseudoopt" >expected &&
+ test_must_fail git rev-parse --exclude-hidden=$section --$pseudoopt 2>err &&
+ test_cmp expected err
+ '
+
+ test_expect_success "rev-parse --exclude-hidden=$section fails with --$pseudoopt=pattern" '
+ echo "error: --exclude-hidden cannot be used together with --$pseudoopt" >expected &&
+ test_must_fail git rev-parse --exclude-hidden=$section --$pseudoopt=pattern 2>err &&
+ test_cmp expected err
+ '
+ done
+done
+
+test_expect_success 'rev-list --exclude=glob with --branches=glob' '
+ compare rev-list "--exclude=subspace-* --branches=sub*" "subspace/one subspace/two"
+'
+
+test_expect_success 'rev-list --exclude=glob with --tags=glob' '
+ compare rev-list "--exclude=qux/? --tags=qux/*" "qux/one qux/two"
+'
+
+test_expect_success 'rev-list --exclude=glob with --remotes=glob' '
+ compare rev-list "--exclude=upstream/? --remotes=upstream/*" "upstream/one upstream/two"
+'
+
+test_expect_success 'rev-list --exclude=ref with --branches=glob' '
+ compare rev-list "--exclude=subspace-x --branches=sub*" "subspace/one subspace/two"
+'
+
+test_expect_success 'rev-list --exclude=ref with --tags=glob' '
+ compare rev-list "--exclude=qux/x --tags=qux/*" "qux/one qux/two"
+'
+
+test_expect_success 'rev-list --exclude=ref with --remotes=glob' '
+ compare rev-list "--exclude=upstream/x --remotes=upstream/*" "upstream/one upstream/two"
+'
+
+test_expect_success 'rev-list --glob=refs/heads/subspace/*' '
+
+ compare rev-list "subspace/one subspace/two" "--glob=refs/heads/subspace/*"
+
+'
+
+test_expect_success 'rev-list --glob refs/heads/subspace/*' '
+
+ compare rev-list "subspace/one subspace/two" "--glob refs/heads/subspace/*"
+
+'
+
+test_expect_success 'rev-list not confused by option-like --glob arg' '
+
+ compare rev-list "main" "--glob -0 main"
+
+'
+
+test_expect_success 'rev-list --glob=heads/subspace/*' '
+
+ compare rev-list "subspace/one subspace/two" "--glob=heads/subspace/*"
+
+'
+
+test_expect_success 'rev-list --glob=refs/heads/subspace/' '
+
+ compare rev-list "subspace/one subspace/two" "--glob=refs/heads/subspace/"
+
+'
+
+test_expect_success 'rev-list --glob=heads/subspace/' '
+
+ compare rev-list "subspace/one subspace/two" "--glob=heads/subspace/"
+
+'
+
+test_expect_success 'rev-list --glob=heads/subspace' '
+
+ compare rev-list "subspace/one subspace/two" "--glob=heads/subspace"
+
+'
+
+test_expect_success 'rev-list --branches=subspace/*' '
+
+ compare rev-list "subspace/one subspace/two" "--branches=subspace/*"
+
+'
+
+test_expect_success 'rev-list --branches=subspace/' '
+
+ compare rev-list "subspace/one subspace/two" "--branches=subspace/"
+
+'
+
+test_expect_success 'rev-list --branches=subspace' '
+
+ compare rev-list "subspace/one subspace/two" "--branches=subspace"
+
+'
+
+test_expect_success 'rev-list --branches' '
+
+ compare rev-list "main subspace-x someref other/three subspace/one subspace/two" "--branches"
+
+'
+
+test_expect_success 'rev-list --glob=heads/someref/* main' '
+
+ compare rev-list "main" "--glob=heads/someref/* main"
+
+'
+
+test_expect_success 'rev-list --glob=heads/subspace/* --glob=heads/other/*' '
+
+ compare rev-list "subspace/one subspace/two other/three" "--glob=heads/subspace/* --glob=heads/other/*"
+
+'
+
+test_expect_success 'rev-list --glob=heads/*' '
+
+ compare rev-list "main other/three someref subspace-x subspace/one subspace/two" "--glob=heads/*"
+
+'
+
+test_expect_success 'rev-list --tags=foo' '
+
+ compare rev-list "foo/bar" "--tags=foo"
+
+'
+
+test_expect_success 'rev-list --tags' '
+
+ compare rev-list "foo/bar qux/x qux/two qux/one" "--tags"
+
+'
+
+test_expect_success 'rev-list --remotes=foo' '
+
+ compare rev-list "foo/baz" "--remotes=foo"
+
+'
+
+test_expect_success 'rev-list --exclude with --branches' '
+ compare rev-list "--exclude=*/* --branches" "main someref subspace-x"
+'
+
+test_expect_success 'rev-list --exclude with --all' '
+ compare rev-list "--exclude=refs/remotes/* --all" "--branches --tags"
+'
+
+test_expect_success 'rev-list accumulates multiple --exclude' '
+ compare rev-list "--exclude=refs/remotes/* --exclude=refs/tags/* --all" --branches
+'
+
+test_expect_success 'rev-list should succeed with empty output on empty stdin' '
+ git rev-list --stdin </dev/null >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'rev-list should succeed with empty output with all refs excluded' '
+ git rev-list --exclude=* --all >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'rev-list should succeed with empty output with empty --all' '
+ (
+ test_create_repo empty &&
+ cd empty &&
+ git rev-list --all >actual &&
+ test_must_be_empty actual
+ )
+'
+
+test_expect_success 'rev-list should succeed with empty output with empty glob' '
+ git rev-list --glob=does-not-match-anything >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'rev-list should succeed with empty output when ignoring missing' '
+ git rev-list --ignore-missing $ZERO_OID >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'shortlog accepts --glob/--tags/--remotes' '
+
+ compare shortlog "subspace/one subspace/two" --branches=subspace &&
+ compare shortlog \
+ "main subspace-x someref other/three subspace/one subspace/two" \
+ --branches &&
+ compare shortlog main "--glob=heads/someref/* main" &&
+ compare shortlog "subspace/one subspace/two other/three" \
+ "--glob=heads/subspace/* --glob=heads/other/*" &&
+ compare shortlog \
+ "main other/three someref subspace-x subspace/one subspace/two" \
+ "--glob=heads/*" &&
+ compare shortlog foo/bar --tags=foo &&
+ compare shortlog "foo/bar qux/one qux/two qux/x" --tags &&
+ compare shortlog foo/baz --remotes=foo
+
+'
+
+test_expect_failure 'shortlog accepts --glob as detached option' '
+
+ compare shortlog \
+ "main other/three someref subspace-x subspace/one subspace/two" \
+ "--glob heads/*"
+
+'
+
+test_expect_failure 'shortlog --glob is not confused by option-like argument' '
+
+ compare shortlog main "--glob -e main"
+
+'
+
+test_done
diff --git a/t/t6019-rev-list-ancestry-path.sh b/t/t6019-rev-list-ancestry-path.sh
new file mode 100755
index 0000000..738da23
--- /dev/null
+++ b/t/t6019-rev-list-ancestry-path.sh
@@ -0,0 +1,132 @@
+#!/bin/sh
+
+test_description='--ancestry-path'
+
+# D---E-------F
+# / \ \
+# B---C---G---H---I---J
+# / \
+# A-------K---------------L--M
+#
+# D..M == E F G H I J K L M
+# --ancestry-path D..M == E F H I J L M
+# --ancestry-path=F D..M == E F J L M
+# --ancestry-path=G D..M == G H I J L M
+# --ancestry-path=H D..M == E G H I J L M
+# --ancestry-path=K D..M == K L M
+# --ancestry-path=K --ancestry-path=F D..M == E F J K L M
+#
+# D..M -- M.t == M
+# --ancestry-path D..M -- M.t == M
+#
+# F...I == F G H I
+# --ancestry-path F...I == F H I
+#
+# G..M -- G.t == [nothing - was dropped in "-s ours" merge L]
+# --ancestry-path G..M -- G.t == L
+# --ancestry-path --simplify-merges G^..M -- G.t == G L
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_merge () {
+ test_tick &&
+ git merge -s ours -m "$2" "$1" &&
+ git tag "$2"
+}
+
+test_expect_success setup '
+ test_commit A &&
+ test_commit B &&
+ test_commit C &&
+ test_commit D &&
+ test_commit E &&
+ test_commit F &&
+ git reset --hard C &&
+ test_commit G &&
+ test_merge E H &&
+ test_commit I &&
+ test_merge F J &&
+ git reset --hard A &&
+ test_commit K &&
+ test_merge J L &&
+ test_commit M
+'
+
+test_ancestry () {
+ args=$1
+ expected=$2
+ test_expect_success "log $args" "
+ test_write_lines $expected >expect &&
+ git log --format=%s $args >raw &&
+
+ if test -n \"$expected\"
+ then
+ sort raw >actual &&
+ test_cmp expect actual
+ else
+ test_must_be_empty raw
+ fi
+ "
+}
+
+test_ancestry "D..M" "E F G H I J K L M"
+
+test_ancestry "--ancestry-path D..M" "E F H I J L M"
+test_ancestry "--ancestry-path=F D..M" "E F J L M"
+test_ancestry "--ancestry-path=G D..M" "G H I J L M"
+test_ancestry "--ancestry-path=H D..M" "E G H I J L M"
+test_ancestry "--ancestry-path=K D..M" "K L M"
+test_ancestry "--ancestry-path=F --ancestry-path=K D..M" "E F J K L M"
+
+test_ancestry "D..M -- M.t" "M"
+test_ancestry "--ancestry-path D..M -- M.t" "M"
+
+test_ancestry "F...I" "F G H I"
+test_ancestry "--ancestry-path F...I" "F H I"
+
+test_ancestry "G..M -- G.t" ""
+test_ancestry "--ancestry-path G..M -- G.t" "L"
+test_ancestry "--ancestry-path --simplify-merges G^..M -- G.t" "G L"
+
+# b---bc
+# / \ /
+# a X
+# \ / \
+# c---cb
+#
+# All refnames prefixed with 'x' to avoid confusion with the tags
+# generated by test_commit on case-insensitive systems.
+test_expect_success 'setup criss-cross' '
+ mkdir criss-cross &&
+ (cd criss-cross &&
+ git init &&
+ test_commit A &&
+ git checkout -b xb main &&
+ test_commit B &&
+ git checkout -b xc main &&
+ test_commit C &&
+ git checkout -b xbc xb -- &&
+ git merge xc &&
+ git checkout -b xcb xc -- &&
+ git merge xb &&
+ git checkout main)
+'
+
+# no commits in bc descend from cb
+test_expect_success 'criss-cross: rev-list --ancestry-path cb..bc' '
+ (cd criss-cross &&
+ git rev-list --ancestry-path xcb..xbc > actual &&
+ test_must_be_empty actual)
+'
+
+# no commits in repository descend from cb
+test_expect_success 'criss-cross: rev-list --ancestry-path --all ^cb' '
+ (cd criss-cross &&
+ git rev-list --ancestry-path --all ^xcb > actual &&
+ test_must_be_empty actual)
+'
+
+test_done
diff --git a/t/t6020-bundle-misc.sh b/t/t6020-bundle-misc.sh
new file mode 100755
index 0000000..3e6bcbf
--- /dev/null
+++ b/t/t6020-bundle-misc.sh
@@ -0,0 +1,654 @@
+#!/bin/sh
+#
+# Copyright (c) 2021 Jiang Xin
+#
+
+test_description='Test git-bundle'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-bundle.sh
+. "$TEST_DIRECTORY"/lib-terminal.sh
+
+for cmd in create verify list-heads unbundle
+do
+ test_expect_success "usage: git bundle $cmd needs an argument" '
+ test_expect_code 129 git bundle $cmd
+ '
+done
+
+# Create a commit or tag and set the variable with the object ID.
+test_commit_setvar () {
+ notick=
+ signoff=
+ indir=
+ merge=
+ tag=
+ var=
+
+ while test $# != 0
+ do
+ case "$1" in
+ --merge)
+ merge=t
+ ;;
+ --tag)
+ tag=t
+ ;;
+ --notick)
+ notick=t
+ ;;
+ --signoff)
+ signoff="$1"
+ ;;
+ -C)
+ shift
+ indir="$1"
+ ;;
+ -*)
+ echo >&2 "error: unknown option $1"
+ return 1
+ ;;
+ *)
+ break
+ ;;
+ esac
+ shift
+ done
+ if test $# -lt 2
+ then
+ echo >&2 "error: test_commit_setvar must have at least 2 arguments"
+ return 1
+ fi
+ var=$1
+ shift
+ indir=${indir:+"$indir"/}
+ if test -z "$notick"
+ then
+ test_tick
+ fi &&
+ if test -n "$merge"
+ then
+ git ${indir:+ -C "$indir"} merge --no-edit --no-ff \
+ ${2:+-m "$2"} "$1" &&
+ oid=$(git ${indir:+ -C "$indir"} rev-parse HEAD)
+ elif test -n "$tag"
+ then
+ git ${indir:+ -C "$indir"} tag -m "$1" "$1" "${2:-HEAD}" &&
+ oid=$(git ${indir:+ -C "$indir"} rev-parse "$1")
+ else
+ file=${2:-"$1.t"} &&
+ echo "${3-$1}" >"$indir$file" &&
+ git ${indir:+ -C "$indir"} add "$file" &&
+ git ${indir:+ -C "$indir"} commit $signoff -m "$1" &&
+ oid=$(git ${indir:+ -C "$indir"} rev-parse HEAD)
+ fi &&
+ eval $var=$oid
+}
+
+get_abbrev_oid () {
+ oid=$1 &&
+ suffix=${oid#???????} &&
+ oid=${oid%$suffix} &&
+ if test -n "$oid"
+ then
+ echo "$oid"
+ else
+ echo "undefined-oid"
+ fi
+}
+
+# Format the output of git commands to make a user-friendly and stable
+# text. We can easily prepare the expect text without having to worry
+# about future changes of the commit ID.
+make_user_friendly_and_stable_output () {
+ sed \
+ -e "s/$(get_abbrev_oid $A)[0-9a-f]*/<COMMIT-A>/g" \
+ -e "s/$(get_abbrev_oid $B)[0-9a-f]*/<COMMIT-B>/g" \
+ -e "s/$(get_abbrev_oid $C)[0-9a-f]*/<COMMIT-C>/g" \
+ -e "s/$(get_abbrev_oid $D)[0-9a-f]*/<COMMIT-D>/g" \
+ -e "s/$(get_abbrev_oid $E)[0-9a-f]*/<COMMIT-E>/g" \
+ -e "s/$(get_abbrev_oid $F)[0-9a-f]*/<COMMIT-F>/g" \
+ -e "s/$(get_abbrev_oid $G)[0-9a-f]*/<COMMIT-G>/g" \
+ -e "s/$(get_abbrev_oid $H)[0-9a-f]*/<COMMIT-H>/g" \
+ -e "s/$(get_abbrev_oid $I)[0-9a-f]*/<COMMIT-I>/g" \
+ -e "s/$(get_abbrev_oid $J)[0-9a-f]*/<COMMIT-J>/g" \
+ -e "s/$(get_abbrev_oid $K)[0-9a-f]*/<COMMIT-K>/g" \
+ -e "s/$(get_abbrev_oid $L)[0-9a-f]*/<COMMIT-L>/g" \
+ -e "s/$(get_abbrev_oid $M)[0-9a-f]*/<COMMIT-M>/g" \
+ -e "s/$(get_abbrev_oid $N)[0-9a-f]*/<COMMIT-N>/g" \
+ -e "s/$(get_abbrev_oid $O)[0-9a-f]*/<COMMIT-O>/g" \
+ -e "s/$(get_abbrev_oid $P)[0-9a-f]*/<COMMIT-P>/g" \
+ -e "s/$(get_abbrev_oid $TAG1)[0-9a-f]*/<TAG-1>/g" \
+ -e "s/$(get_abbrev_oid $TAG2)[0-9a-f]*/<TAG-2>/g" \
+ -e "s/$(get_abbrev_oid $TAG3)[0-9a-f]*/<TAG-3>/g"
+}
+
+format_and_save_expect () {
+ sed -e 's/Z$//' >expect
+}
+
+HASH_MESSAGE="The bundle uses this hash algorithm: $GIT_DEFAULT_HASH"
+
+# (C) (D, pull/1/head, topic/1)
+# o --- o
+# / \ (L)
+# / \ o (H, topic/2) (M, tag:v2)
+# / (F) \ / (N, tag:v3)
+# / o --------- o (G, pull/2/head) o --- o --- o (release)
+# / / \ \ / \
+# o --- o --- o -------- o -- o ------------------ o ------- o --- o (main)
+# (A) (B) (E, tag:v1) (I) (J) (K) (O) (P)
+#
+test_expect_success 'setup' '
+ # Try to make a stable fixed width for abbreviated commit ID,
+ # this fixed-width oid will be replaced with "<OID>".
+ git config core.abbrev 7 &&
+
+ # branch main: commit A & B
+ test_commit_setvar A "Commit A" main.txt &&
+ test_commit_setvar B "Commit B" main.txt &&
+
+ # branch topic/1: commit C & D, refs/pull/1/head
+ git checkout -b topic/1 &&
+ test_commit_setvar C "Commit C" topic-1.txt &&
+ test_commit_setvar D "Commit D" topic-1.txt &&
+ git update-ref refs/pull/1/head HEAD &&
+
+ # branch topic/1: commit E, tag v1
+ git checkout main &&
+ test_commit_setvar E "Commit E" main.txt &&
+ test_commit_setvar --tag TAG1 v1 &&
+
+ # branch topic/2: commit F & G, refs/pull/2/head
+ git checkout -b topic/2 &&
+ test_commit_setvar F "Commit F" topic-2.txt &&
+ test_commit_setvar G "Commit G" topic-2.txt &&
+ git update-ref refs/pull/2/head HEAD &&
+ test_commit_setvar H "Commit H" topic-2.txt &&
+
+ # branch main: merge commit I & J
+ git checkout main &&
+ test_commit_setvar --merge I topic/1 "Merge commit I" &&
+ test_commit_setvar --merge J refs/pull/2/head "Merge commit J" &&
+
+ # branch main: commit K
+ git checkout main &&
+ test_commit_setvar K "Commit K" main.txt &&
+
+ # branch release:
+ git checkout -b release &&
+ test_commit_setvar L "Commit L" release.txt &&
+ test_commit_setvar M "Commit M" release.txt &&
+ test_commit_setvar --tag TAG2 v2 &&
+ test_commit_setvar N "Commit N" release.txt &&
+ test_commit_setvar --tag TAG3 v3 &&
+
+ # branch main: merge commit O, commit P
+ git checkout main &&
+ test_commit_setvar --merge O tags/v2 "Merge commit O" &&
+ test_commit_setvar P "Commit P" main.txt
+'
+
+test_expect_success 'create bundle from special rev: main^!' '
+ git bundle create special-rev.bdl "main^!" &&
+
+ git bundle list-heads special-rev.bdl |
+ make_user_friendly_and_stable_output >actual &&
+ cat >expect <<-\EOF &&
+ <COMMIT-P> refs/heads/main
+ EOF
+ test_cmp expect actual &&
+
+ git bundle verify special-rev.bdl |
+ make_user_friendly_and_stable_output >actual &&
+ format_and_save_expect <<-EOF &&
+ The bundle contains this ref:
+ <COMMIT-P> refs/heads/main
+ The bundle requires this ref:
+ <COMMIT-O> Z
+ $HASH_MESSAGE
+ EOF
+ test_cmp expect actual &&
+
+ test_bundle_object_count special-rev.bdl 3
+'
+
+test_expect_success 'create bundle with --max-count option' '
+ git bundle create max-count.bdl --max-count 1 \
+ main \
+ "^release" \
+ refs/tags/v1 \
+ refs/pull/1/head \
+ refs/pull/2/head &&
+
+ git bundle verify max-count.bdl |
+ make_user_friendly_and_stable_output >actual &&
+ format_and_save_expect <<-EOF &&
+ The bundle contains these 2 refs:
+ <COMMIT-P> refs/heads/main
+ <TAG-1> refs/tags/v1
+ The bundle requires this ref:
+ <COMMIT-O> Z
+ $HASH_MESSAGE
+ EOF
+ test_cmp expect actual &&
+
+ test_bundle_object_count max-count.bdl 4
+'
+
+test_expect_success 'create bundle with --since option' '
+ git log -1 --pretty="%ad" $M >actual &&
+ cat >expect <<-\EOF &&
+ Thu Apr 7 15:26:13 2005 -0700
+ EOF
+ test_cmp expect actual &&
+
+ git bundle create since.bdl \
+ --since "Thu Apr 7 15:27:00 2005 -0700" \
+ --all &&
+
+ git bundle verify since.bdl |
+ make_user_friendly_and_stable_output >actual &&
+ format_and_save_expect <<-EOF &&
+ The bundle contains these 5 refs:
+ <COMMIT-P> refs/heads/main
+ <COMMIT-N> refs/heads/release
+ <TAG-2> refs/tags/v2
+ <TAG-3> refs/tags/v3
+ <COMMIT-P> HEAD
+ The bundle requires these 2 refs:
+ <COMMIT-M> Z
+ <COMMIT-K> Z
+ $HASH_MESSAGE
+ EOF
+ test_cmp expect actual &&
+
+ test_bundle_object_count --thin since.bdl 13
+'
+
+test_expect_success 'create bundle 1 - no prerequisites' '
+ # create bundle from args
+ git bundle create 1.bdl topic/1 topic/2 &&
+
+ # create bundle from stdin
+ cat >input <<-\EOF &&
+ topic/1
+ topic/2
+ EOF
+ git bundle create stdin-1.bdl --stdin <input &&
+
+ format_and_save_expect <<-EOF &&
+ The bundle contains these 2 refs:
+ <COMMIT-D> refs/heads/topic/1
+ <COMMIT-H> refs/heads/topic/2
+ The bundle records a complete history.
+ $HASH_MESSAGE
+ EOF
+
+ # verify bundle, which has no prerequisites
+ git bundle verify 1.bdl |
+ make_user_friendly_and_stable_output >actual &&
+ test_cmp expect actual &&
+
+ git bundle verify stdin-1.bdl |
+ make_user_friendly_and_stable_output >actual &&
+ test_cmp expect actual &&
+
+ test_bundle_object_count 1.bdl 24 &&
+ test_bundle_object_count stdin-1.bdl 24
+'
+
+test_expect_success 'create bundle 2 - has prerequisites' '
+ # create bundle from args
+ git bundle create 2.bdl \
+ --ignore-missing \
+ ^topic/deleted \
+ ^$D \
+ ^topic/2 \
+ release &&
+
+ # create bundle from stdin
+ # input has a non-exist reference: "topic/deleted"
+ cat >input <<-EOF &&
+ ^topic/deleted
+ ^$D
+ ^topic/2
+ EOF
+ git bundle create stdin-2.bdl \
+ --ignore-missing \
+ --stdin \
+ release <input &&
+
+ format_and_save_expect <<-EOF &&
+ The bundle contains this ref:
+ <COMMIT-N> refs/heads/release
+ The bundle requires these 3 refs:
+ <COMMIT-D> Z
+ <COMMIT-E> Z
+ <COMMIT-G> Z
+ $HASH_MESSAGE
+ EOF
+
+ git bundle verify 2.bdl |
+ make_user_friendly_and_stable_output >actual &&
+ test_cmp expect actual &&
+
+ git bundle verify stdin-2.bdl |
+ make_user_friendly_and_stable_output >actual &&
+ test_cmp expect actual &&
+
+ test_bundle_object_count 2.bdl 16 &&
+ test_bundle_object_count stdin-2.bdl 16
+'
+
+test_expect_success 'fail to verify bundle without prerequisites' '
+ git init --bare test1.git &&
+
+ format_and_save_expect <<-\EOF &&
+ error: Repository lacks these prerequisite commits:
+ error: <COMMIT-D> Z
+ error: <COMMIT-E> Z
+ error: <COMMIT-G> Z
+ EOF
+
+ test_must_fail git -C test1.git bundle verify ../2.bdl 2>&1 |
+ make_user_friendly_and_stable_output >actual &&
+ test_cmp expect actual &&
+
+ test_must_fail git -C test1.git bundle verify ../stdin-2.bdl 2>&1 |
+ make_user_friendly_and_stable_output >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'create bundle 3 - two refs, same object' '
+ # create bundle from args
+ git bundle create --version=3 3.bdl \
+ ^release \
+ ^topic/1 \
+ ^topic/2 \
+ main \
+ HEAD &&
+
+ # create bundle from stdin
+ cat >input <<-\EOF &&
+ ^release
+ ^topic/1
+ ^topic/2
+ EOF
+ git bundle create --version=3 stdin-3.bdl \
+ --stdin \
+ main HEAD <input &&
+
+ format_and_save_expect <<-EOF &&
+ The bundle contains these 2 refs:
+ <COMMIT-P> refs/heads/main
+ <COMMIT-P> HEAD
+ The bundle requires these 2 refs:
+ <COMMIT-M> Z
+ <COMMIT-K> Z
+ $HASH_MESSAGE
+ EOF
+
+ git bundle verify 3.bdl |
+ make_user_friendly_and_stable_output >actual &&
+ test_cmp expect actual &&
+
+ git bundle verify stdin-3.bdl |
+ make_user_friendly_and_stable_output >actual &&
+ test_cmp expect actual &&
+
+ test_bundle_object_count 3.bdl 4 &&
+ test_bundle_object_count stdin-3.bdl 4
+'
+
+test_expect_success 'create bundle 4 - with tags' '
+ # create bundle from args
+ git bundle create 4.bdl \
+ ^main \
+ ^release \
+ ^topic/1 \
+ ^topic/2 \
+ --all &&
+
+ # create bundle from stdin
+ cat >input <<-\EOF &&
+ ^main
+ ^release
+ ^topic/1
+ ^topic/2
+ EOF
+ git bundle create stdin-4.bdl \
+ --ignore-missing \
+ --stdin \
+ --all <input &&
+
+ cat >expect <<-EOF &&
+ The bundle contains these 3 refs:
+ <TAG-1> refs/tags/v1
+ <TAG-2> refs/tags/v2
+ <TAG-3> refs/tags/v3
+ The bundle records a complete history.
+ $HASH_MESSAGE
+ EOF
+
+ git bundle verify 4.bdl |
+ make_user_friendly_and_stable_output >actual &&
+ test_cmp expect actual &&
+
+ git bundle verify stdin-4.bdl |
+ make_user_friendly_and_stable_output >actual &&
+ test_cmp expect actual &&
+
+ test_bundle_object_count 4.bdl 3 &&
+ test_bundle_object_count stdin-4.bdl 3
+'
+
+test_expect_success 'clone from bundle' '
+ git clone --mirror 1.bdl mirror.git &&
+ git -C mirror.git show-ref |
+ make_user_friendly_and_stable_output >actual &&
+ cat >expect <<-\EOF &&
+ <COMMIT-D> refs/heads/topic/1
+ <COMMIT-H> refs/heads/topic/2
+ EOF
+ test_cmp expect actual &&
+
+ git -C mirror.git fetch ../2.bdl "+refs/*:refs/*" &&
+ git -C mirror.git show-ref |
+ make_user_friendly_and_stable_output >actual &&
+ cat >expect <<-\EOF &&
+ <COMMIT-N> refs/heads/release
+ <COMMIT-D> refs/heads/topic/1
+ <COMMIT-H> refs/heads/topic/2
+ EOF
+ test_cmp expect actual &&
+
+ git -C mirror.git fetch ../3.bdl "+refs/*:refs/*" &&
+ git -C mirror.git show-ref |
+ make_user_friendly_and_stable_output >actual &&
+ cat >expect <<-\EOF &&
+ <COMMIT-P> refs/heads/main
+ <COMMIT-N> refs/heads/release
+ <COMMIT-D> refs/heads/topic/1
+ <COMMIT-H> refs/heads/topic/2
+ EOF
+ test_cmp expect actual &&
+
+ git -C mirror.git fetch ../4.bdl "+refs/*:refs/*" &&
+ git -C mirror.git show-ref |
+ make_user_friendly_and_stable_output >actual &&
+ cat >expect <<-\EOF &&
+ <COMMIT-P> refs/heads/main
+ <COMMIT-N> refs/heads/release
+ <COMMIT-D> refs/heads/topic/1
+ <COMMIT-H> refs/heads/topic/2
+ <TAG-1> refs/tags/v1
+ <TAG-2> refs/tags/v2
+ <TAG-3> refs/tags/v3
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'unfiltered bundle with --objects' '
+ git bundle create all-objects.bdl \
+ --all --objects &&
+ git bundle create all.bdl \
+ --all &&
+
+ # Compare the headers of these files.
+ sed -n -e "/^$/q" -e "p" all.bdl >expect &&
+ sed -n -e "/^$/q" -e "p" all-objects.bdl >actual &&
+ test_cmp expect actual
+'
+
+for filter in "blob:none" "tree:0" "tree:1" "blob:limit=100"
+do
+ test_expect_success "filtered bundle: $filter" '
+ test_when_finished rm -rf .git/objects/pack cloned unbundled &&
+ git bundle create partial.bdl \
+ --all \
+ --filter=$filter &&
+
+ git bundle verify partial.bdl >unfiltered &&
+ make_user_friendly_and_stable_output <unfiltered >actual &&
+
+ cat >expect <<-EOF &&
+ The bundle contains these 10 refs:
+ <COMMIT-P> refs/heads/main
+ <COMMIT-N> refs/heads/release
+ <COMMIT-D> refs/heads/topic/1
+ <COMMIT-H> refs/heads/topic/2
+ <COMMIT-D> refs/pull/1/head
+ <COMMIT-G> refs/pull/2/head
+ <TAG-1> refs/tags/v1
+ <TAG-2> refs/tags/v2
+ <TAG-3> refs/tags/v3
+ <COMMIT-P> HEAD
+ The bundle records a complete history.
+ $HASH_MESSAGE
+ The bundle uses this filter: $filter
+ EOF
+ test_cmp expect actual &&
+
+ test_config uploadpack.allowfilter 1 &&
+ test_config uploadpack.allowanysha1inwant 1 &&
+ git clone --no-local --filter=$filter --bare "file://$(pwd)" cloned &&
+
+ git init unbundled &&
+ git -C unbundled bundle unbundle ../partial.bdl >ref-list.txt &&
+ ls unbundled/.git/objects/pack/pack-*.promisor >promisor &&
+ test_line_count = 1 promisor &&
+
+ # Count the same number of reachable objects.
+ reflist=$(git for-each-ref --format="%(objectname)") &&
+ git rev-list --objects --filter=$filter --missing=allow-any \
+ $reflist >expect &&
+ for repo in cloned unbundled
+ do
+ git -C $repo rev-list --objects --missing=allow-any \
+ $reflist >actual &&
+ test_cmp expect actual || return 1
+ done
+ '
+done
+
+# NEEDSWORK: 'git clone --bare' should be able to clone from a filtered
+# bundle, but that requires a change to promisor/filter config options.
+# For now, we fail gracefully with a helpful error. This behavior can be
+# changed in the future to succeed as much as possible.
+test_expect_success 'cloning from filtered bundle has useful error' '
+ git bundle create partial.bdl \
+ --all \
+ --filter=blob:none &&
+ test_must_fail git clone --bare partial.bdl partial 2>err &&
+ grep "cannot clone from filtered bundle" err
+'
+
+test_expect_success 'verify catches unreachable, broken prerequisites' '
+ test_when_finished rm -rf clone-from clone-to &&
+ git init clone-from &&
+ (
+ cd clone-from &&
+ git checkout -b base &&
+ test_commit A &&
+ git checkout -b tip &&
+ git commit --allow-empty -m "will drop by shallow" &&
+ git commit --allow-empty -m "will keep by shallow" &&
+ git commit --allow-empty -m "for bundle, not clone" &&
+ git bundle create tip.bundle tip~1..tip &&
+ git reset --hard HEAD~1 &&
+ git checkout base
+ ) &&
+ BAD_OID=$(git -C clone-from rev-parse tip~1) &&
+ TIP_OID=$(git -C clone-from rev-parse tip) &&
+ git clone --depth=1 --no-single-branch \
+ "file://$(pwd)/clone-from" clone-to &&
+ (
+ cd clone-to &&
+
+ # Set up broken history by removing shallow markers
+ git update-ref -d refs/remotes/origin/tip &&
+ rm .git/shallow &&
+
+ # Verify should fail
+ test_must_fail git bundle verify \
+ ../clone-from/tip.bundle 2>err &&
+ grep "some prerequisite commits .* are not connected" err &&
+ test_line_count = 1 err &&
+
+ # Unbundling should fail
+ test_must_fail git bundle unbundle \
+ ../clone-from/tip.bundle 2>err &&
+ grep "some prerequisite commits .* are not connected" err &&
+ test_line_count = 1 err
+ )
+'
+
+test_expect_success 'bundle progress includes write phase' '
+ GIT_PROGRESS_DELAY=0 \
+ git bundle create --progress out.bundle --all 2>err &&
+ grep 'Writing' err
+'
+
+test_expect_success TTY 'create --quiet disables all bundle progress' '
+ test_terminal env GIT_PROGRESS_DELAY=0 \
+ git bundle create --quiet out.bundle --all 2>err &&
+ test_must_be_empty err
+'
+
+test_expect_success 'bundle progress with --no-quiet' '
+ GIT_PROGRESS_DELAY=0 \
+ git bundle create --no-quiet out.bundle --all 2>err &&
+ grep "%" err
+'
+
+test_expect_success 'read bundle over stdin' '
+ git bundle create some.bundle HEAD &&
+
+ git bundle verify - <some.bundle 2>err &&
+ grep "<stdin> is okay" err &&
+
+ git bundle list-heads some.bundle >expect &&
+ git bundle list-heads - <some.bundle >actual &&
+ test_cmp expect actual &&
+
+ git bundle unbundle some.bundle >expect &&
+ git bundle unbundle - <some.bundle >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'send a bundle to standard output' '
+ git bundle create - --all HEAD >bundle-one &&
+ mkdir -p down &&
+ git -C down bundle create - --all HEAD >bundle-two &&
+ git bundle verify bundle-one &&
+ git bundle verify bundle-two &&
+ git ls-remote bundle-one >expect &&
+ git ls-remote bundle-two >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t6021-rev-list-exclude-hidden.sh b/t/t6021-rev-list-exclude-hidden.sh
new file mode 100755
index 0000000..cdf7aa9
--- /dev/null
+++ b/t/t6021-rev-list-exclude-hidden.sh
@@ -0,0 +1,164 @@
+#!/bin/sh
+
+test_description='git rev-list --exclude-hidden test'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ test_commit_bulk --id=commit --ref=refs/heads/branch 1 &&
+ COMMIT=$(git rev-parse refs/heads/branch) &&
+ test_commit_bulk --id=tag --ref=refs/tags/lightweight 1 &&
+ TAG=$(git rev-parse refs/tags/lightweight) &&
+ test_commit_bulk --id=hidden --ref=refs/hidden/commit 1 &&
+ HIDDEN=$(git rev-parse refs/hidden/commit) &&
+ test_commit_bulk --id=namespace --ref=refs/namespaces/namespace/refs/namespaced/commit 1 &&
+ NAMESPACE=$(git rev-parse refs/namespaces/namespace/refs/namespaced/commit)
+'
+
+test_expect_success 'invalid section' '
+ echo "fatal: unsupported section for hidden refs: unsupported" >expected &&
+ test_must_fail git rev-list --exclude-hidden=unsupported 2>err &&
+ test_cmp expected err
+'
+
+for section in fetch receive uploadpack
+do
+ test_expect_success "$section: passed multiple times" '
+ echo "fatal: --exclude-hidden= passed more than once" >expected &&
+ test_must_fail git rev-list --exclude-hidden=$section --exclude-hidden=$section 2>err &&
+ test_cmp expected err
+ '
+
+ test_expect_success "$section: without hiddenRefs" '
+ git rev-list --exclude-hidden=$section --all >out &&
+ cat >expected <<-EOF &&
+ $NAMESPACE
+ $HIDDEN
+ $TAG
+ $COMMIT
+ EOF
+ test_cmp expected out
+ '
+
+ test_expect_success "$section: hidden via transfer.hideRefs" '
+ git -c transfer.hideRefs=refs/hidden/ rev-list --exclude-hidden=$section --all >out &&
+ cat >expected <<-EOF &&
+ $NAMESPACE
+ $TAG
+ $COMMIT
+ EOF
+ test_cmp expected out
+ '
+
+ test_expect_success "$section: hidden via $section.hideRefs" '
+ git -c $section.hideRefs=refs/hidden/ rev-list --exclude-hidden=$section --all >out &&
+ cat >expected <<-EOF &&
+ $NAMESPACE
+ $TAG
+ $COMMIT
+ EOF
+ test_cmp expected out
+ '
+
+ test_expect_success "$section: respects both transfer.hideRefs and $section.hideRefs" '
+ git -c transfer.hideRefs=refs/tags/ -c $section.hideRefs=refs/hidden/ rev-list --exclude-hidden=$section --all >out &&
+ cat >expected <<-EOF &&
+ $NAMESPACE
+ $COMMIT
+ EOF
+ test_cmp expected out
+ '
+
+ test_expect_success "$section: negation without hidden refs marks everything as uninteresting" '
+ git rev-list --all --exclude-hidden=$section --not --all >out &&
+ test_must_be_empty out
+ '
+
+ test_expect_success "$section: negation with hidden refs marks them as interesting" '
+ git -c transfer.hideRefs=refs/hidden/ rev-list --all --exclude-hidden=$section --not --all >out &&
+ cat >expected <<-EOF &&
+ $HIDDEN
+ EOF
+ test_cmp expected out
+ '
+
+ test_expect_success "$section: hidden refs and excludes work together" '
+ git -c transfer.hideRefs=refs/hidden/ rev-list --exclude=refs/tags/* --exclude-hidden=$section --all >out &&
+ cat >expected <<-EOF &&
+ $NAMESPACE
+ $COMMIT
+ EOF
+ test_cmp expected out
+ '
+
+ test_expect_success "$section: excluded hidden refs get reset" '
+ git -c transfer.hideRefs=refs/ rev-list --exclude-hidden=$section --all --all >out &&
+ cat >expected <<-EOF &&
+ $NAMESPACE
+ $HIDDEN
+ $TAG
+ $COMMIT
+ EOF
+ test_cmp expected out
+ '
+
+ test_expect_success "$section: excluded hidden refs can be used with multiple pseudo-refs" '
+ git -c transfer.hideRefs=refs/ rev-list --exclude-hidden=$section --all --exclude-hidden=$section --all >out &&
+ test_must_be_empty out
+ '
+
+ test_expect_success "$section: works with --glob" '
+ git -c transfer.hideRefs=refs/hidden/ rev-list --exclude-hidden=$section --glob=refs/h* >out &&
+ cat >expected <<-EOF &&
+ $COMMIT
+ EOF
+ test_cmp expected out
+ '
+
+ test_expect_success "$section: operates on stripped refs by default" '
+ GIT_NAMESPACE=namespace git -c transfer.hideRefs=refs/namespaced/ rev-list --exclude-hidden=$section --all >out &&
+ cat >expected <<-EOF &&
+ $HIDDEN
+ $TAG
+ $COMMIT
+ EOF
+ test_cmp expected out
+ '
+
+ test_expect_success "$section: does not hide namespace by default" '
+ GIT_NAMESPACE=namespace git -c transfer.hideRefs=refs/namespaces/namespace/ rev-list --exclude-hidden=$section --all >out &&
+ cat >expected <<-EOF &&
+ $NAMESPACE
+ $HIDDEN
+ $TAG
+ $COMMIT
+ EOF
+ test_cmp expected out
+ '
+
+ test_expect_success "$section: can operate on unstripped refs" '
+ GIT_NAMESPACE=namespace git -c transfer.hideRefs=^refs/namespaces/namespace/ rev-list --exclude-hidden=$section --all >out &&
+ cat >expected <<-EOF &&
+ $HIDDEN
+ $TAG
+ $COMMIT
+ EOF
+ test_cmp expected out
+ '
+
+ for pseudoopt in remotes branches tags
+ do
+ test_expect_success "$section: fails with --$pseudoopt" '
+ test_must_fail git rev-list --exclude-hidden=$section --$pseudoopt 2>err &&
+ test_grep "error: --exclude-hidden cannot be used together with --$pseudoopt" err
+ '
+
+ test_expect_success "$section: fails with --$pseudoopt=pattern" '
+ test_must_fail git rev-list --exclude-hidden=$section --$pseudoopt=pattern 2>err &&
+ test_grep "error: --exclude-hidden cannot be used together with --$pseudoopt" err
+ '
+ done
+done
+
+test_done
diff --git a/t/t6022-rev-list-missing.sh b/t/t6022-rev-list-missing.sh
new file mode 100755
index 0000000..40265a4
--- /dev/null
+++ b/t/t6022-rev-list-missing.sh
@@ -0,0 +1,74 @@
+#!/bin/sh
+
+test_description='handling of missing objects in rev-list'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# We setup the repository with two commits, this way HEAD is always
+# available and we can hide commit 1.
+test_expect_success 'create repository and alternate directory' '
+ test_commit 1 &&
+ test_commit 2 &&
+ test_commit 3
+'
+
+for obj in "HEAD~1" "HEAD~1^{tree}" "HEAD:1.t"
+do
+ test_expect_success "rev-list --missing=error fails with missing object $obj" '
+ oid="$(git rev-parse $obj)" &&
+ path=".git/objects/$(test_oid_to_path $oid)" &&
+
+ mv "$path" "$path.hidden" &&
+ test_when_finished "mv $path.hidden $path" &&
+
+ test_must_fail git rev-list --missing=error --objects \
+ --no-object-names HEAD
+ '
+done
+
+for obj in "HEAD~1" "HEAD~1^{tree}" "HEAD:1.t"
+do
+ for action in "allow-any" "print"
+ do
+ test_expect_success "rev-list --missing=$action with missing $obj" '
+ oid="$(git rev-parse $obj)" &&
+ path=".git/objects/$(test_oid_to_path $oid)" &&
+
+ # Before the object is made missing, we use rev-list to
+ # get the expected oids.
+ git rev-list --objects --no-object-names \
+ HEAD ^$obj >expect.raw &&
+
+ # Blobs are shared by all commits, so evethough a commit/tree
+ # might be skipped, its blob must be accounted for.
+ if [ $obj != "HEAD:1.t" ]; then
+ echo $(git rev-parse HEAD:1.t) >>expect.raw &&
+ echo $(git rev-parse HEAD:2.t) >>expect.raw
+ fi &&
+
+ mv "$path" "$path.hidden" &&
+ test_when_finished "mv $path.hidden $path" &&
+
+ git rev-list --missing=$action --objects --no-object-names \
+ HEAD >actual.raw &&
+
+ # When the action is to print, we should also add the missing
+ # oid to the expect list.
+ case $action in
+ allow-any)
+ ;;
+ print)
+ grep ?$oid actual.raw &&
+ echo ?$oid >>expect.raw
+ ;;
+ esac &&
+
+ sort actual.raw >actual &&
+ sort expect.raw >expect &&
+ test_cmp expect actual
+ '
+ done
+done
+
+test_done
diff --git a/t/t6030-bisect-porcelain.sh b/t/t6030-bisect-porcelain.sh
new file mode 100755
index 0000000..2a5b7d8
--- /dev/null
+++ b/t/t6030-bisect-porcelain.sh
@@ -0,0 +1,1256 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Christian Couder
+#
+test_description='Tests git bisect functionality'
+
+exec </dev/null
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+add_line_into_file()
+{
+ _line=$1
+ _file=$2
+
+ if [ -f "$_file" ]; then
+ echo "$_line" >> $_file || return $?
+ MSG="Add <$_line> into <$_file>."
+ else
+ echo "$_line" > $_file || return $?
+ git add $_file || return $?
+ MSG="Create file <$_file> with <$_line> inside."
+ fi
+
+ test_tick
+ git commit --quiet -m "$MSG" $_file
+}
+
+HASH1=
+HASH2=
+HASH3=
+HASH4=
+
+test_bisect_usage () {
+ local code="$1" &&
+ shift &&
+ cat >expect &&
+ test_expect_code $code "$@" >out 2>actual &&
+ test_must_be_empty out &&
+ test_cmp expect actual
+}
+
+test_expect_success 'bisect usage' "
+ test_bisect_usage 1 git bisect reset extra1 extra2 <<-\EOF &&
+ error: 'git bisect reset' requires either no argument or a commit
+ EOF
+ test_bisect_usage 1 git bisect terms extra1 extra2 <<-\EOF &&
+ error: 'git bisect terms' requires 0 or 1 argument
+ EOF
+ test_bisect_usage 1 git bisect next extra1 <<-\EOF &&
+ error: 'git bisect next' requires 0 arguments
+ EOF
+ test_bisect_usage 1 git bisect log extra1 <<-\EOF &&
+ error: We are not bisecting.
+ EOF
+ test_bisect_usage 1 git bisect replay <<-\EOF &&
+ error: no logfile given
+ EOF
+ test_bisect_usage 1 git bisect run <<-\EOF
+ error: 'git bisect run' failed: no command provided.
+ EOF
+"
+
+test_expect_success 'set up basic repo with 1 file (hello) and 4 commits' '
+ add_line_into_file "1: Hello World" hello &&
+ HASH1=$(git rev-parse --verify HEAD) &&
+ add_line_into_file "2: A new day for git" hello &&
+ HASH2=$(git rev-parse --verify HEAD) &&
+ add_line_into_file "3: Another new day for git" hello &&
+ HASH3=$(git rev-parse --verify HEAD) &&
+ add_line_into_file "4: Ciao for now" hello &&
+ HASH4=$(git rev-parse --verify HEAD)
+'
+
+test_expect_success 'bisect starts with only one bad' '
+ git bisect reset &&
+ git bisect start &&
+ git bisect bad $HASH4 &&
+ git bisect next
+'
+
+test_expect_success 'bisect does not start with only one good' '
+ git bisect reset &&
+ git bisect start &&
+ git bisect good $HASH1 &&
+ test_must_fail git bisect next
+'
+
+test_expect_success 'bisect start with one bad and good' '
+ git bisect reset &&
+ git bisect start &&
+ git bisect good $HASH1 &&
+ git bisect bad $HASH4 &&
+ git bisect next
+'
+
+test_expect_success 'bisect fails if given any junk instead of revs' '
+ git bisect reset &&
+ test_must_fail git bisect start foo $HASH1 -- &&
+ test_must_fail git bisect start $HASH4 $HASH1 bar -- &&
+ test -z "$(git for-each-ref "refs/bisect/*")" &&
+ test -z "$(ls .git/BISECT_* 2>/dev/null)" &&
+ git bisect start &&
+ test_must_fail git bisect good foo $HASH1 &&
+ test_must_fail git bisect good $HASH1 bar &&
+ test_must_fail git bisect bad frotz &&
+ test_must_fail git bisect bad $HASH3 $HASH4 &&
+ test_must_fail git bisect skip bar $HASH3 &&
+ test_must_fail git bisect skip $HASH1 foo &&
+ test -z "$(git for-each-ref "refs/bisect/*")" &&
+ git bisect good $HASH1 &&
+ git bisect bad $HASH4
+'
+
+test_expect_success 'bisect start without -- takes unknown arg as pathspec' '
+ git bisect reset &&
+ git bisect start foo bar &&
+ grep foo ".git/BISECT_NAMES" &&
+ grep bar ".git/BISECT_NAMES"
+'
+
+test_expect_success 'bisect reset: back in a branch checked out also elsewhere' '
+ echo "shared" > branch.expect &&
+ test_bisect_reset() {
+ git -C $1 bisect start &&
+ git -C $1 bisect good $HASH1 &&
+ git -C $1 bisect bad $HASH3 &&
+ git -C $1 bisect reset &&
+ git -C $1 branch --show-current > branch.output &&
+ cmp branch.expect branch.output
+ } &&
+ test_when_finished "
+ git worktree remove wt1 &&
+ git worktree remove wt2 &&
+ git branch -d shared
+ " &&
+ git worktree add wt1 -b shared &&
+ git worktree add wt2 -f shared &&
+ # we test in both worktrees to ensure that works
+ # as expected with "first" and "next" worktrees
+ test_bisect_reset wt1 &&
+ test_bisect_reset wt2
+'
+
+test_expect_success 'bisect reset: back in the main branch' '
+ git bisect reset &&
+ echo "* main" > branch.expect &&
+ git branch > branch.output &&
+ cmp branch.expect branch.output
+'
+
+test_expect_success 'bisect reset: back in another branch' '
+ git checkout -b other &&
+ git bisect start &&
+ git bisect good $HASH1 &&
+ git bisect bad $HASH3 &&
+ git bisect reset &&
+ echo " main" > branch.expect &&
+ echo "* other" >> branch.expect &&
+ git branch > branch.output &&
+ cmp branch.expect branch.output
+'
+
+test_expect_success 'bisect reset when not bisecting' '
+ git bisect reset &&
+ git branch > branch.output &&
+ cmp branch.expect branch.output
+'
+
+test_expect_success 'bisect reset removes packed refs' '
+ git bisect reset &&
+ git bisect start &&
+ git bisect good $HASH1 &&
+ git bisect bad $HASH3 &&
+ git pack-refs --all --prune &&
+ git bisect next &&
+ git bisect reset &&
+ test -z "$(git for-each-ref "refs/bisect/*")" &&
+ test -z "$(git for-each-ref "refs/heads/bisect")"
+'
+
+test_expect_success 'bisect reset removes bisect state after --no-checkout' '
+ git bisect reset &&
+ git bisect start --no-checkout &&
+ git bisect good $HASH1 &&
+ git bisect bad $HASH3 &&
+ git bisect next &&
+ git bisect reset &&
+ test -z "$(git for-each-ref "refs/bisect/*")" &&
+ test -z "$(git for-each-ref "refs/heads/bisect")" &&
+ test -z "$(git for-each-ref "BISECT_HEAD")"
+'
+
+test_expect_success 'bisect start: back in good branch' '
+ git branch > branch.output &&
+ grep "* other" branch.output > /dev/null &&
+ git bisect start $HASH4 $HASH1 -- &&
+ git bisect good &&
+ git bisect start $HASH4 $HASH1 -- &&
+ git bisect bad &&
+ git bisect reset &&
+ git branch > branch.output &&
+ grep "* other" branch.output > /dev/null
+'
+
+test_expect_success 'bisect start: no ".git/BISECT_START" created if junk rev' '
+ git bisect reset &&
+ test_must_fail git bisect start $HASH4 foo -- &&
+ git branch > branch.output &&
+ grep "* other" branch.output > /dev/null &&
+ test_path_is_missing .git/BISECT_START
+'
+
+test_expect_success 'bisect start: existing ".git/BISECT_START" not modified if junk rev' '
+ git bisect start $HASH4 $HASH1 -- &&
+ git bisect good &&
+ cp .git/BISECT_START saved &&
+ test_must_fail git bisect start $HASH4 foo -- &&
+ git branch > branch.output &&
+ test_grep "* (no branch, bisect started on other)" branch.output > /dev/null &&
+ test_cmp saved .git/BISECT_START
+'
+test_expect_success 'bisect start: no ".git/BISECT_START" if mistaken rev' '
+ git bisect start $HASH4 $HASH1 -- &&
+ git bisect good &&
+ test_must_fail git bisect start $HASH1 $HASH4 -- &&
+ git branch > branch.output &&
+ grep "* other" branch.output > /dev/null &&
+ test_path_is_missing .git/BISECT_START
+'
+
+test_expect_success 'bisect start: no ".git/BISECT_START" if checkout error' '
+ echo "temp stuff" > hello &&
+ test_must_fail git bisect start $HASH4 $HASH1 -- &&
+ git branch &&
+ git branch > branch.output &&
+ grep "* other" branch.output > /dev/null &&
+ test_path_is_missing .git/BISECT_START &&
+ test -z "$(git for-each-ref "refs/bisect/*")" &&
+ git checkout HEAD hello
+'
+
+# $HASH1 is good, $HASH4 is bad, we skip $HASH3
+# but $HASH2 is bad,
+# so we should find $HASH2 as the first bad commit
+test_expect_success 'bisect skip: successful result' '
+ test_when_finished git bisect reset &&
+ git bisect reset &&
+ git bisect start $HASH4 $HASH1 &&
+ git bisect skip &&
+ git bisect bad > my_bisect_log.txt &&
+ grep "$HASH2 is the first bad commit" my_bisect_log.txt
+'
+
+# $HASH1 is good, $HASH4 is bad, we skip $HASH3 and $HASH2
+# so we should not be able to tell the first bad commit
+# among $HASH2, $HASH3 and $HASH4
+test_expect_success 'bisect skip: cannot tell between 3 commits' '
+ test_when_finished git bisect reset &&
+ git bisect start $HASH4 $HASH1 &&
+ git bisect skip &&
+ test_expect_code 2 git bisect skip >my_bisect_log.txt &&
+ grep "first bad commit could be any of" my_bisect_log.txt &&
+ ! grep $HASH1 my_bisect_log.txt &&
+ grep $HASH2 my_bisect_log.txt &&
+ grep $HASH3 my_bisect_log.txt &&
+ grep $HASH4 my_bisect_log.txt
+'
+
+# $HASH1 is good, $HASH4 is bad, we skip $HASH3
+# but $HASH2 is good,
+# so we should not be able to tell the first bad commit
+# among $HASH3 and $HASH4
+test_expect_success 'bisect skip: cannot tell between 2 commits' '
+ test_when_finished git bisect reset &&
+ git bisect start $HASH4 $HASH1 &&
+ git bisect skip &&
+ test_expect_code 2 git bisect good >my_bisect_log.txt &&
+ grep "first bad commit could be any of" my_bisect_log.txt &&
+ ! grep $HASH1 my_bisect_log.txt &&
+ ! grep $HASH2 my_bisect_log.txt &&
+ grep $HASH3 my_bisect_log.txt &&
+ grep $HASH4 my_bisect_log.txt
+'
+
+# $HASH1 is good, $HASH4 is both skipped and bad, we skip $HASH3
+# and $HASH2 is good,
+# so we should not be able to tell the first bad commit
+# among $HASH3 and $HASH4
+test_expect_success 'bisect skip: with commit both bad and skipped' '
+ test_when_finished git bisect reset &&
+ git bisect start &&
+ git bisect skip &&
+ git bisect bad &&
+ git bisect good $HASH1 &&
+ git bisect skip &&
+ test_expect_code 2 git bisect good >my_bisect_log.txt &&
+ grep "first bad commit could be any of" my_bisect_log.txt &&
+ ! grep $HASH1 my_bisect_log.txt &&
+ ! grep $HASH2 my_bisect_log.txt &&
+ grep $HASH3 my_bisect_log.txt &&
+ grep $HASH4 my_bisect_log.txt
+'
+
+test_bisect_run_args () {
+ test_when_finished "rm -f run.sh actual" &&
+ >actual &&
+ cat >expect.args &&
+ cat <&6 >expect.out &&
+ cat <&7 >expect.err &&
+ write_script run.sh <<-\EOF &&
+ while test $# != 0
+ do
+ echo "<$1>" &&
+ shift
+ done >actual.args
+ EOF
+
+ test_when_finished "git bisect reset" &&
+ git bisect start &&
+ git bisect good $HASH1 &&
+ git bisect bad $HASH4 &&
+ git bisect run ./run.sh $@ >actual.out.raw 2>actual.err &&
+ # Prune just the log output
+ sed -n \
+ -e '/^Author:/d' \
+ -e '/^Date:/d' \
+ -e '/^$/d' \
+ -e '/^commit /d' \
+ -e '/^ /d' \
+ -e 'p' \
+ <actual.out.raw >actual.out &&
+ test_cmp expect.out actual.out &&
+ test_cmp expect.err actual.err &&
+ test_cmp expect.args actual.args
+}
+
+test_expect_success 'git bisect run: args, stdout and stderr with no arguments' "
+ test_bisect_run_args <<-'EOF_ARGS' 6<<-EOF_OUT 7<<-'EOF_ERR'
+ EOF_ARGS
+ running './run.sh'
+ $HASH4 is the first bad commit
+ bisect found first bad commit
+ EOF_OUT
+ EOF_ERR
+"
+
+test_expect_success 'git bisect run: args, stdout and stderr: "--" argument' "
+ test_bisect_run_args -- <<-'EOF_ARGS' 6<<-EOF_OUT 7<<-'EOF_ERR'
+ <-->
+ EOF_ARGS
+ running './run.sh' '--'
+ $HASH4 is the first bad commit
+ bisect found first bad commit
+ EOF_OUT
+ EOF_ERR
+"
+
+test_expect_success 'git bisect run: args, stdout and stderr: "--log foo --no-log bar" arguments' "
+ test_bisect_run_args --log foo --no-log bar <<-'EOF_ARGS' 6<<-EOF_OUT 7<<-'EOF_ERR'
+ <--log>
+ <foo>
+ <--no-log>
+ <bar>
+ EOF_ARGS
+ running './run.sh' '--log' 'foo' '--no-log' 'bar'
+ $HASH4 is the first bad commit
+ bisect found first bad commit
+ EOF_OUT
+ EOF_ERR
+"
+
+test_expect_success 'git bisect run: args, stdout and stderr: "--bisect-start" argument' "
+ test_bisect_run_args --bisect-start <<-'EOF_ARGS' 6<<-EOF_OUT 7<<-'EOF_ERR'
+ <--bisect-start>
+ EOF_ARGS
+ running './run.sh' '--bisect-start'
+ $HASH4 is the first bad commit
+ bisect found first bad commit
+ EOF_OUT
+ EOF_ERR
+"
+
+test_expect_success 'git bisect run: negative exit code' "
+ write_script fail.sh <<-'EOF' &&
+ exit 255
+ EOF
+ cat <<-'EOF' >expect &&
+ bisect run failed: exit code -1 from './fail.sh' is < 0 or >= 128
+ EOF
+ test_when_finished 'git bisect reset' &&
+ git bisect start &&
+ git bisect good $HASH1 &&
+ git bisect bad $HASH4 &&
+ ! git bisect run ./fail.sh 2>err &&
+ sed -En 's/.*(bisect.*code) (-?[0-9]+) (from.*)/\1 -1 \3/p' err >actual &&
+ test_cmp expect actual
+"
+
+test_expect_success 'git bisect run: unable to verify on good' "
+ write_script fail.sh <<-'EOF' &&
+ head=\$(git rev-parse --verify HEAD)
+ good=\$(git rev-parse --verify $HASH1)
+ if test "\$head" = "\$good"
+ then
+ exit 255
+ else
+ exit 127
+ fi
+ EOF
+ cat <<-'EOF' >expect &&
+ unable to verify './fail.sh' on good revision
+ EOF
+ test_when_finished 'git bisect reset' &&
+ git bisect start &&
+ git bisect good $HASH1 &&
+ git bisect bad $HASH4 &&
+ ! git bisect run ./fail.sh 2>err &&
+ sed -n 's/.*\(unable to verify.*\)/\1/p' err >actual &&
+ test_cmp expect actual
+"
+
+# We want to automatically find the commit that
+# added "Another" into hello.
+test_expect_success '"git bisect run" simple case' '
+ write_script test_script.sh <<-\EOF &&
+ ! grep Another hello >/dev/null
+ EOF
+ git bisect start &&
+ git bisect good $HASH1 &&
+ git bisect bad $HASH4 &&
+ git bisect run ./test_script.sh >my_bisect_log.txt &&
+ grep "$HASH3 is the first bad commit" my_bisect_log.txt &&
+ git bisect reset
+'
+
+# We want to make sure no arguments has been eaten
+test_expect_success '"git bisect run" simple case' '
+ git bisect start &&
+ git bisect good $HASH1 &&
+ git bisect bad $HASH4 &&
+ git bisect run printf "%s %s\n" reset --bisect-skip >my_bisect_log.txt &&
+ grep -e "reset --bisect-skip" my_bisect_log.txt &&
+ git bisect reset
+'
+
+# We want to automatically find the commit that
+# added "Ciao" into hello.
+test_expect_success '"git bisect run" with more complex "git bisect start"' '
+ write_script test_script.sh <<-\EOF &&
+ ! grep Ciao hello >/dev/null
+ EOF
+ git bisect start $HASH4 $HASH1 &&
+ git bisect run ./test_script.sh >my_bisect_log.txt &&
+ grep "$HASH4 is the first bad commit" my_bisect_log.txt &&
+ git bisect reset
+'
+
+test_expect_success 'bisect run accepts exit code 126 as bad' '
+ test_when_finished "git bisect reset" &&
+ write_script test_script.sh <<-\EOF &&
+ ! grep Another hello || exit 126 >/dev/null
+ EOF
+ git bisect start &&
+ git bisect good $HASH1 &&
+ git bisect bad $HASH4 &&
+ git bisect run ./test_script.sh >my_bisect_log.txt &&
+ grep "$HASH3 is the first bad commit" my_bisect_log.txt
+'
+
+test_expect_success POSIXPERM 'bisect run fails with non-executable test script' '
+ test_when_finished "git bisect reset" &&
+ >not-executable.sh &&
+ chmod -x not-executable.sh &&
+ git bisect start &&
+ git bisect good $HASH1 &&
+ git bisect bad $HASH4 &&
+ test_must_fail git bisect run ./not-executable.sh >my_bisect_log.txt &&
+ ! grep "is the first bad commit" my_bisect_log.txt
+'
+
+test_expect_success 'bisect run accepts exit code 127 as bad' '
+ test_when_finished "git bisect reset" &&
+ write_script test_script.sh <<-\EOF &&
+ ! grep Another hello || exit 127 >/dev/null
+ EOF
+ git bisect start &&
+ git bisect good $HASH1 &&
+ git bisect bad $HASH4 &&
+ git bisect run ./test_script.sh >my_bisect_log.txt &&
+ grep "$HASH3 is the first bad commit" my_bisect_log.txt
+'
+
+test_expect_success 'bisect run fails with missing test script' '
+ test_when_finished "git bisect reset" &&
+ rm -f does-not-exist.sh &&
+ git bisect start &&
+ git bisect good $HASH1 &&
+ git bisect bad $HASH4 &&
+ test_must_fail git bisect run ./does-not-exist.sh >my_bisect_log.txt &&
+ ! grep "is the first bad commit" my_bisect_log.txt
+'
+
+# $HASH1 is good, $HASH5 is bad, we skip $HASH3
+# but $HASH4 is good,
+# so we should find $HASH5 as the first bad commit
+HASH5=
+test_expect_success 'bisect skip: add line and then a new test' '
+ add_line_into_file "5: Another new line." hello &&
+ HASH5=$(git rev-parse --verify HEAD) &&
+ git bisect start $HASH5 $HASH1 &&
+ git bisect skip &&
+ git bisect good > my_bisect_log.txt &&
+ grep "$HASH5 is the first bad commit" my_bisect_log.txt &&
+ git bisect log > log_to_replay.txt &&
+ git bisect reset
+'
+
+test_expect_success 'bisect skip and bisect replay' '
+ git bisect replay log_to_replay.txt > my_bisect_log.txt &&
+ grep "$HASH5 is the first bad commit" my_bisect_log.txt &&
+ git bisect reset
+'
+
+HASH6=
+test_expect_success 'bisect run & skip: cannot tell between 2' '
+ add_line_into_file "6: Yet a line." hello &&
+ HASH6=$(git rev-parse --verify HEAD) &&
+ write_script test_script.sh <<-\EOF &&
+ sed -ne \$p hello | grep Ciao >/dev/null && exit 125
+ ! grep line hello >/dev/null
+ EOF
+ git bisect start $HASH6 $HASH1 &&
+ test_expect_code 2 git bisect run ./test_script.sh >my_bisect_log.txt &&
+ grep "first bad commit could be any of" my_bisect_log.txt &&
+ ! grep $HASH3 my_bisect_log.txt &&
+ ! grep $HASH6 my_bisect_log.txt &&
+ grep $HASH4 my_bisect_log.txt &&
+ grep $HASH5 my_bisect_log.txt
+'
+
+HASH7=
+test_expect_success 'bisect run & skip: find first bad' '
+ git bisect reset &&
+ add_line_into_file "7: Should be the last line." hello &&
+ HASH7=$(git rev-parse --verify HEAD) &&
+ write_script test_script.sh <<-\EOF &&
+ sed -ne \$p hello | grep Ciao >/dev/null && exit 125
+ sed -ne \$p hello | grep day >/dev/null && exit 125
+ ! grep Yet hello >/dev/null
+ EOF
+ git bisect start $HASH7 $HASH1 &&
+ git bisect run ./test_script.sh >my_bisect_log.txt &&
+ grep "$HASH6 is the first bad commit" my_bisect_log.txt
+'
+
+test_expect_success 'bisect skip only one range' '
+ git bisect reset &&
+ git bisect start $HASH7 $HASH1 &&
+ git bisect skip $HASH1..$HASH5 &&
+ test "$HASH6" = "$(git rev-parse --verify HEAD)" &&
+ test_must_fail git bisect bad > my_bisect_log.txt &&
+ grep "first bad commit could be any of" my_bisect_log.txt
+'
+
+test_expect_success 'bisect skip many ranges' '
+ git bisect start $HASH7 $HASH1 &&
+ test "$HASH4" = "$(git rev-parse --verify HEAD)" &&
+ git bisect skip $HASH2 $HASH2.. ..$HASH5 &&
+ test "$HASH6" = "$(git rev-parse --verify HEAD)" &&
+ test_must_fail git bisect bad > my_bisect_log.txt &&
+ grep "first bad commit could be any of" my_bisect_log.txt
+'
+
+test_expect_success 'bisect starting with a detached HEAD' '
+ git bisect reset &&
+ git checkout main^ &&
+ HEAD=$(git rev-parse --verify HEAD) &&
+ git bisect start &&
+ test $HEAD = $(cat .git/BISECT_START) &&
+ git bisect reset &&
+ test $HEAD = $(git rev-parse --verify HEAD)
+'
+
+test_expect_success 'bisect errors out if bad and good are mistaken' '
+ git bisect reset &&
+ test_must_fail git bisect start $HASH2 $HASH4 2> rev_list_error &&
+ test_grep "mistook good and bad" rev_list_error &&
+ git bisect reset
+'
+
+test_expect_success 'bisect does not create a "bisect" branch' '
+ git bisect reset &&
+ git bisect start $HASH7 $HASH1 &&
+ git branch bisect &&
+ rev_hash4=$(git rev-parse --verify HEAD) &&
+ test "$rev_hash4" = "$HASH4" &&
+ git branch -D bisect &&
+ git bisect good &&
+ git branch bisect &&
+ rev_hash6=$(git rev-parse --verify HEAD) &&
+ test "$rev_hash6" = "$HASH6" &&
+ git bisect good > my_bisect_log.txt &&
+ grep "$HASH7 is the first bad commit" my_bisect_log.txt &&
+ git bisect reset &&
+ rev_hash6=$(git rev-parse --verify bisect) &&
+ test "$rev_hash6" = "$HASH6" &&
+ git branch -D bisect
+'
+
+# This creates a "side" branch to test "siblings" cases.
+#
+# H1-H2-H3-H4-H5-H6-H7 <--other
+# \
+# S5-S6-S7 <--side
+#
+test_expect_success 'side branch creation' '
+ git bisect reset &&
+ git checkout -b side $HASH4 &&
+ add_line_into_file "5(side): first line on a side branch" hello2 &&
+ SIDE_HASH5=$(git rev-parse --verify HEAD) &&
+ add_line_into_file "6(side): second line on a side branch" hello2 &&
+ SIDE_HASH6=$(git rev-parse --verify HEAD) &&
+ add_line_into_file "7(side): third line on a side branch" hello2 &&
+ SIDE_HASH7=$(git rev-parse --verify HEAD)
+'
+
+test_expect_success 'good merge base when good and bad are siblings' '
+ git bisect start "$HASH7" "$SIDE_HASH7" > my_bisect_log.txt &&
+ test_grep "merge base must be tested" my_bisect_log.txt &&
+ grep $HASH4 my_bisect_log.txt &&
+ git bisect good > my_bisect_log.txt &&
+ ! grep "merge base must be tested" my_bisect_log.txt &&
+ grep $HASH6 my_bisect_log.txt &&
+ git bisect reset
+'
+test_expect_success 'skipped merge base when good and bad are siblings' '
+ git bisect start "$SIDE_HASH7" "$HASH7" > my_bisect_log.txt &&
+ test_grep "merge base must be tested" my_bisect_log.txt &&
+ grep $HASH4 my_bisect_log.txt &&
+ git bisect skip > my_bisect_log.txt 2>&1 &&
+ grep "warning" my_bisect_log.txt &&
+ grep $SIDE_HASH6 my_bisect_log.txt &&
+ git bisect reset
+'
+
+test_expect_success 'bad merge base when good and bad are siblings' '
+ git bisect start "$HASH7" HEAD > my_bisect_log.txt &&
+ test_grep "merge base must be tested" my_bisect_log.txt &&
+ grep $HASH4 my_bisect_log.txt &&
+ test_must_fail git bisect bad > my_bisect_log.txt 2>&1 &&
+ test_grep "merge base $HASH4 is bad" my_bisect_log.txt &&
+ test_grep "fixed between $HASH4 and \[$SIDE_HASH7\]" my_bisect_log.txt &&
+ git bisect reset
+'
+
+# This creates a few more commits (A and B) to test "siblings" cases
+# when a good and a bad rev have many merge bases.
+#
+# We should have the following:
+#
+# H1-H2-H3-H4-H5-H6-H7
+# \ \ \
+# S5-A \
+# \ \
+# S6-S7----B
+#
+# And there A and B have 2 merge bases (S5 and H5) that should be
+# reported by "git merge-base --all A B".
+#
+test_expect_success 'many merge bases creation' '
+ git checkout "$SIDE_HASH5" &&
+ git merge -m "merge HASH5 and SIDE_HASH5" "$HASH5" &&
+ A_HASH=$(git rev-parse --verify HEAD) &&
+ git checkout side &&
+ git merge -m "merge HASH7 and SIDE_HASH7" "$HASH7" &&
+ B_HASH=$(git rev-parse --verify HEAD) &&
+ git merge-base --all "$A_HASH" "$B_HASH" > merge_bases.txt &&
+ test_line_count = 2 merge_bases.txt &&
+ grep "$HASH5" merge_bases.txt &&
+ grep "$SIDE_HASH5" merge_bases.txt
+'
+
+# We want to automatically find the merge that
+# added "line" into hello.
+test_expect_success '"git bisect run --first-parent" simple case' '
+ git rev-list --first-parent $B_HASH ^$HASH4 >first_parent_chain.txt &&
+ write_script test_script.sh <<-\EOF &&
+ grep $(git rev-parse HEAD) first_parent_chain.txt || exit -1
+ ! grep line hello >/dev/null
+ EOF
+ git bisect start --first-parent &&
+ test_path_is_file ".git/BISECT_FIRST_PARENT" &&
+ git bisect good $HASH4 &&
+ git bisect bad $B_HASH &&
+ git bisect run ./test_script.sh >my_bisect_log.txt &&
+ grep "$B_HASH is the first bad commit" my_bisect_log.txt &&
+ git bisect reset &&
+ test_path_is_missing .git/BISECT_FIRST_PARENT
+'
+
+test_expect_success 'good merge bases when good and bad are siblings' '
+ git bisect start "$B_HASH" "$A_HASH" > my_bisect_log.txt &&
+ test_grep "merge base must be tested" my_bisect_log.txt &&
+ git bisect good > my_bisect_log2.txt &&
+ test_grep "merge base must be tested" my_bisect_log2.txt &&
+ {
+ {
+ grep "$SIDE_HASH5" my_bisect_log.txt &&
+ grep "$HASH5" my_bisect_log2.txt
+ } || {
+ grep "$SIDE_HASH5" my_bisect_log2.txt &&
+ grep "$HASH5" my_bisect_log.txt
+ }
+ } &&
+ git bisect reset
+'
+
+test_expect_success 'optimized merge base checks' '
+ git bisect start "$HASH7" "$SIDE_HASH7" > my_bisect_log.txt &&
+ test_grep "merge base must be tested" my_bisect_log.txt &&
+ grep "$HASH4" my_bisect_log.txt &&
+ git bisect good > my_bisect_log2.txt &&
+ test -f ".git/BISECT_ANCESTORS_OK" &&
+ test "$HASH6" = $(git rev-parse --verify HEAD) &&
+ git bisect bad &&
+ git bisect good "$A_HASH" > my_bisect_log4.txt &&
+ test_grep "merge base must be tested" my_bisect_log4.txt &&
+ test_path_is_missing ".git/BISECT_ANCESTORS_OK"
+'
+
+# This creates another side branch called "parallel" with some files
+# in some directories, to test bisecting with paths.
+#
+# We should have the following:
+#
+# P1-P2-P3-P4-P5-P6-P7
+# / / /
+# H1-H2-H3-H4-H5-H6-H7
+# \ \ \
+# S5-A \
+# \ \
+# S6-S7----B
+#
+test_expect_success '"parallel" side branch creation' '
+ git bisect reset &&
+ git checkout -b parallel $HASH1 &&
+ mkdir dir1 dir2 &&
+ add_line_into_file "1(para): line 1 on parallel branch" dir1/file1 &&
+ PARA_HASH1=$(git rev-parse --verify HEAD) &&
+ add_line_into_file "2(para): line 2 on parallel branch" dir2/file2 &&
+ PARA_HASH2=$(git rev-parse --verify HEAD) &&
+ add_line_into_file "3(para): line 3 on parallel branch" dir2/file3 &&
+ PARA_HASH3=$(git rev-parse --verify HEAD) &&
+ git merge -m "merge HASH4 and PARA_HASH3" "$HASH4" &&
+ PARA_HASH4=$(git rev-parse --verify HEAD) &&
+ add_line_into_file "5(para): add line on parallel branch" dir1/file1 &&
+ PARA_HASH5=$(git rev-parse --verify HEAD) &&
+ add_line_into_file "6(para): add line on parallel branch" dir2/file2 &&
+ PARA_HASH6=$(git rev-parse --verify HEAD) &&
+ git merge -m "merge HASH7 and PARA_HASH6" "$HASH7" &&
+ PARA_HASH7=$(git rev-parse --verify HEAD)
+'
+
+test_expect_success 'restricting bisection on one dir' '
+ git bisect reset &&
+ git bisect start HEAD $HASH1 -- dir1 &&
+ para1=$(git rev-parse --verify HEAD) &&
+ test "$para1" = "$PARA_HASH1" &&
+ git bisect bad > my_bisect_log.txt &&
+ grep "$PARA_HASH1 is the first bad commit" my_bisect_log.txt
+'
+
+test_expect_success 'restricting bisection on one dir and a file' '
+ git bisect reset &&
+ git bisect start HEAD $HASH1 -- dir1 hello &&
+ para4=$(git rev-parse --verify HEAD) &&
+ test "$para4" = "$PARA_HASH4" &&
+ git bisect bad &&
+ hash3=$(git rev-parse --verify HEAD) &&
+ test "$hash3" = "$HASH3" &&
+ git bisect good &&
+ hash4=$(git rev-parse --verify HEAD) &&
+ test "$hash4" = "$HASH4" &&
+ git bisect good &&
+ para1=$(git rev-parse --verify HEAD) &&
+ test "$para1" = "$PARA_HASH1" &&
+ git bisect good > my_bisect_log.txt &&
+ grep "$PARA_HASH4 is the first bad commit" my_bisect_log.txt
+'
+
+test_expect_success 'skipping away from skipped commit' '
+ git bisect start $PARA_HASH7 $HASH1 &&
+ para4=$(git rev-parse --verify HEAD) &&
+ test "$para4" = "$PARA_HASH4" &&
+ git bisect skip &&
+ hash7=$(git rev-parse --verify HEAD) &&
+ test "$hash7" = "$HASH7" &&
+ git bisect skip &&
+ para3=$(git rev-parse --verify HEAD) &&
+ test "$para3" = "$PARA_HASH3"
+'
+
+test_expect_success 'erroring out when using bad path arguments' '
+ test_must_fail git bisect start $PARA_HASH7 $HASH1 -- foobar 2> error.txt &&
+ test_grep "bad path arguments" error.txt
+'
+
+test_expect_success 'test bisection on bare repo - --no-checkout specified' '
+ git clone --bare . bare.nocheckout &&
+ (
+ cd bare.nocheckout &&
+ git bisect start --no-checkout &&
+ git bisect good $HASH1 &&
+ git bisect bad $HASH4 &&
+ git bisect run eval \
+ "test \$(git rev-list BISECT_HEAD ^$HASH2 --max-count=1 | wc -l) = 0" \
+ >../nocheckout.log
+ ) &&
+ grep "$HASH3 is the first bad commit" nocheckout.log
+'
+
+
+test_expect_success 'test bisection on bare repo - --no-checkout defaulted' '
+ git clone --bare . bare.defaulted &&
+ (
+ cd bare.defaulted &&
+ git bisect start &&
+ git bisect good $HASH1 &&
+ git bisect bad $HASH4 &&
+ git bisect run eval \
+ "test \$(git rev-list BISECT_HEAD ^$HASH2 --max-count=1 | wc -l) = 0" \
+ >../defaulted.log
+ ) &&
+ grep "$HASH3 is the first bad commit" defaulted.log
+'
+
+#
+# This creates a broken branch which cannot be checked out because
+# the tree created has been deleted.
+#
+# H1-H2-H3-H4-H5-H6-H7 <--other
+# \
+# S5-S6'-S7'-S8'-S9 <--broken
+#
+# Commits marked with ' have a missing tree.
+#
+test_expect_success 'broken branch creation' '
+ git bisect reset &&
+ git checkout -b broken $HASH4 &&
+ git tag BROKEN_HASH4 $HASH4 &&
+ add_line_into_file "5(broken): first line on a broken branch" hello2 &&
+ git tag BROKEN_HASH5 &&
+ mkdir missing &&
+ :> missing/MISSING &&
+ git add missing/MISSING &&
+ git commit -m "6(broken): Added file that will be deleted" &&
+ git tag BROKEN_HASH6 &&
+ deleted=$(git rev-parse --verify HEAD:missing) &&
+ add_line_into_file "7(broken): second line on a broken branch" hello2 &&
+ git tag BROKEN_HASH7 &&
+ add_line_into_file "8(broken): third line on a broken branch" hello2 &&
+ git tag BROKEN_HASH8 &&
+ git rm missing/MISSING &&
+ git commit -m "9(broken): Remove missing file" &&
+ git tag BROKEN_HASH9 &&
+ rm .git/objects/$(test_oid_to_path $deleted)
+'
+
+echo "" > expected.ok
+cat > expected.missing-tree.default <<EOF
+fatal: unable to read tree $deleted
+EOF
+
+test_expect_success 'bisect fails if tree is broken on start commit' '
+ git bisect reset &&
+ test_must_fail git bisect start BROKEN_HASH7 BROKEN_HASH4 2>error.txt &&
+ test_cmp expected.missing-tree.default error.txt
+'
+
+test_expect_success 'bisect fails if tree is broken on trial commit' '
+ git bisect reset &&
+ test_must_fail git bisect start BROKEN_HASH9 BROKEN_HASH4 2>error.txt &&
+ git reset --hard broken &&
+ git checkout broken &&
+ test_cmp expected.missing-tree.default error.txt
+'
+
+check_same()
+{
+ echo "Checking $1 is the same as $2" &&
+ test_cmp_rev "$1" "$2"
+}
+
+test_expect_success 'bisect: --no-checkout - start commit bad' '
+ git bisect reset &&
+ git bisect start BROKEN_HASH7 BROKEN_HASH4 --no-checkout &&
+ check_same BROKEN_HASH6 BISECT_HEAD &&
+ git bisect reset
+'
+
+test_expect_success 'bisect: --no-checkout - trial commit bad' '
+ git bisect reset &&
+ git bisect start broken BROKEN_HASH4 --no-checkout &&
+ check_same BROKEN_HASH6 BISECT_HEAD &&
+ git bisect reset
+'
+
+test_expect_success 'bisect: --no-checkout - target before breakage' '
+ git bisect reset &&
+ git bisect start broken BROKEN_HASH4 --no-checkout &&
+ check_same BROKEN_HASH6 BISECT_HEAD &&
+ git bisect bad BISECT_HEAD &&
+ check_same BROKEN_HASH5 BISECT_HEAD &&
+ git bisect bad BISECT_HEAD &&
+ check_same BROKEN_HASH5 bisect/bad &&
+ git bisect reset
+'
+
+test_expect_success 'bisect: --no-checkout - target in breakage' '
+ git bisect reset &&
+ git bisect start broken BROKEN_HASH4 --no-checkout &&
+ check_same BROKEN_HASH6 BISECT_HEAD &&
+ git bisect bad BISECT_HEAD &&
+ check_same BROKEN_HASH5 BISECT_HEAD &&
+ test_must_fail git bisect good BISECT_HEAD &&
+ check_same BROKEN_HASH6 bisect/bad &&
+ git bisect reset
+'
+
+test_expect_success 'bisect: --no-checkout - target after breakage' '
+ git bisect reset &&
+ git bisect start broken BROKEN_HASH4 --no-checkout &&
+ check_same BROKEN_HASH6 BISECT_HEAD &&
+ git bisect good BISECT_HEAD &&
+ check_same BROKEN_HASH8 BISECT_HEAD &&
+ test_must_fail git bisect good BISECT_HEAD &&
+ check_same BROKEN_HASH9 bisect/bad &&
+ git bisect reset
+'
+
+test_expect_success 'bisect: demonstrate identification of damage boundary' "
+ git bisect reset &&
+ git checkout broken &&
+ git bisect start broken main --no-checkout &&
+ test_must_fail git bisect run \"\$SHELL_PATH\" -c '
+ GOOD=\$(git for-each-ref \"--format=%(objectname)\" refs/bisect/good-*) &&
+ git rev-list --objects BISECT_HEAD --not \$GOOD >tmp.\$\$ &&
+ git pack-objects --stdout >/dev/null < tmp.\$\$
+ rc=\$?
+ rm -f tmp.\$\$
+ test \$rc = 0' &&
+ check_same BROKEN_HASH6 bisect/bad &&
+ git bisect reset
+"
+
+cat > expected.bisect-log <<EOF
+# bad: [$HASH4] Add <4: Ciao for now> into <hello>.
+# good: [$HASH2] Add <2: A new day for git> into <hello>.
+git bisect start '$HASH4' '$HASH2'
+# good: [$HASH3] Add <3: Another new day for git> into <hello>.
+git bisect good $HASH3
+# first bad commit: [$HASH4] Add <4: Ciao for now> into <hello>.
+EOF
+
+test_expect_success 'bisect log: successful result' '
+ git bisect reset &&
+ git bisect start $HASH4 $HASH2 &&
+ git bisect good &&
+ git bisect log >bisect-log.txt &&
+ test_cmp expected.bisect-log bisect-log.txt &&
+ git bisect reset
+'
+
+cat > expected.bisect-skip-log <<EOF
+# bad: [$HASH4] Add <4: Ciao for now> into <hello>.
+# good: [$HASH2] Add <2: A new day for git> into <hello>.
+git bisect start '$HASH4' '$HASH2'
+# skip: [$HASH3] Add <3: Another new day for git> into <hello>.
+git bisect skip $HASH3
+# only skipped commits left to test
+# possible first bad commit: [$HASH4] Add <4: Ciao for now> into <hello>.
+# possible first bad commit: [$HASH3] Add <3: Another new day for git> into <hello>.
+EOF
+
+test_expect_success 'bisect log: only skip commits left' '
+ git bisect reset &&
+ git bisect start $HASH4 $HASH2 &&
+ test_must_fail git bisect skip &&
+ git bisect log >bisect-skip-log.txt &&
+ test_cmp expected.bisect-skip-log bisect-skip-log.txt &&
+ git bisect reset
+'
+
+test_expect_success '"git bisect bad HEAD" behaves as "git bisect bad"' '
+ git checkout parallel &&
+ git bisect start HEAD $HASH1 &&
+ git bisect good HEAD &&
+ git bisect bad HEAD &&
+ test "$HASH6" = $(git rev-parse --verify HEAD) &&
+ git bisect reset
+'
+
+test_expect_success 'bisect starts with only one new' '
+ git bisect reset &&
+ git bisect start &&
+ git bisect new $HASH4 &&
+ git bisect next
+'
+
+test_expect_success 'bisect does not start with only one old' '
+ git bisect reset &&
+ git bisect start &&
+ git bisect old $HASH1 &&
+ test_must_fail git bisect next
+'
+
+test_expect_success 'bisect start with one new and old' '
+ git bisect reset &&
+ git bisect start &&
+ git bisect old $HASH1 &&
+ git bisect new $HASH4 &&
+ git bisect new &&
+ git bisect new >bisect_result &&
+ grep "$HASH2 is the first new commit" bisect_result &&
+ git bisect log >log_to_replay.txt &&
+ git bisect reset
+'
+
+test_expect_success 'bisect replay with old and new' '
+ git bisect replay log_to_replay.txt >bisect_result &&
+ grep "$HASH2 is the first new commit" bisect_result &&
+ git bisect reset
+'
+
+test_expect_success 'bisect replay with CRLF log' '
+ append_cr <log_to_replay.txt >log_to_replay_crlf.txt &&
+ git bisect replay log_to_replay_crlf.txt >bisect_result_crlf &&
+ grep "$HASH2 is the first new commit" bisect_result_crlf &&
+ git bisect reset
+'
+
+test_expect_success 'bisect cannot mix old/new and good/bad' '
+ git bisect start &&
+ git bisect bad $HASH4 &&
+ test_must_fail git bisect old $HASH1
+'
+
+test_expect_success 'bisect terms needs 0 or 1 argument' '
+ git bisect reset &&
+ test_must_fail git bisect terms only-one &&
+ test_must_fail git bisect terms 1 2 &&
+ test_must_fail git bisect terms 2>actual &&
+ echo "error: no terms defined" >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'bisect terms shows good/bad after start' '
+ git bisect reset &&
+ git bisect start HEAD $HASH1 &&
+ git bisect terms --term-good >actual &&
+ echo good >expected &&
+ test_cmp expected actual &&
+ git bisect terms --term-bad >actual &&
+ echo bad >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'bisect start with one term1 and term2' '
+ git bisect reset &&
+ git bisect start --term-old term2 --term-new term1 &&
+ git bisect term2 $HASH1 &&
+ git bisect term1 $HASH4 &&
+ git bisect term1 &&
+ git bisect term1 >bisect_result &&
+ grep "$HASH2 is the first term1 commit" bisect_result &&
+ git bisect log >log_to_replay.txt &&
+ git bisect reset
+'
+
+test_expect_success 'bogus command does not start bisect' '
+ git bisect reset &&
+ test_must_fail git bisect --bisect-terms 1 2 2>out &&
+ ! grep "You need to start" out &&
+ test_must_fail git bisect --bisect-terms 2>out &&
+ ! grep "You need to start" out &&
+ grep "git bisect.*visualize" out &&
+ git bisect reset
+'
+
+test_expect_success 'bisect replay with term1 and term2' '
+ git bisect replay log_to_replay.txt >bisect_result &&
+ grep "$HASH2 is the first term1 commit" bisect_result &&
+ git bisect reset
+'
+
+test_expect_success 'bisect start term1 term2' '
+ git bisect reset &&
+ git bisect start --term-new term1 --term-old term2 $HASH4 $HASH1 &&
+ git bisect term1 &&
+ git bisect term1 >bisect_result &&
+ grep "$HASH2 is the first term1 commit" bisect_result &&
+ git bisect log >log_to_replay.txt &&
+ git bisect reset
+'
+
+test_expect_success 'bisect cannot mix terms' '
+ git bisect reset &&
+ git bisect start --term-good term1 --term-bad term2 $HASH4 $HASH1 &&
+ test_must_fail git bisect a &&
+ test_must_fail git bisect b &&
+ test_must_fail git bisect bad &&
+ test_must_fail git bisect good &&
+ test_must_fail git bisect new &&
+ test_must_fail git bisect old
+'
+
+test_expect_success 'bisect terms rejects invalid terms' '
+ git bisect reset &&
+ test_must_fail git bisect start --term-good &&
+ test_must_fail git bisect start --term-good invalid..term &&
+ test_must_fail git bisect start --term-bad &&
+ test_must_fail git bisect terms --term-bad invalid..term &&
+ test_must_fail git bisect terms --term-good bad &&
+ test_must_fail git bisect terms --term-good old &&
+ test_must_fail git bisect terms --term-good skip &&
+ test_must_fail git bisect terms --term-good reset &&
+ test_path_is_missing .git/BISECT_TERMS
+'
+
+test_expect_success 'bisect start --term-* does store terms' '
+ git bisect reset &&
+ git bisect start --term-bad=one --term-good=two &&
+ git bisect terms >actual &&
+ cat <<-EOF >expected &&
+ Your current terms are two for the old state
+ and one for the new state.
+ EOF
+ test_cmp expected actual &&
+ git bisect terms --term-bad >actual &&
+ echo one >expected &&
+ test_cmp expected actual &&
+ git bisect terms --term-good >actual &&
+ echo two >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'bisect start takes options and revs in any order' '
+ git bisect reset &&
+ git bisect start --term-good one $HASH4 \
+ --term-good two --term-bad bad-term \
+ $HASH1 --term-good three -- &&
+ (git bisect terms --term-bad && git bisect terms --term-good) >actual &&
+ printf "%s\n%s\n" bad-term three >expected &&
+ test_cmp expected actual
+'
+
+# Bisect is started with --term-new and --term-old arguments,
+# then skip. The HEAD should be changed.
+test_expect_success 'bisect skip works with --term*' '
+ git bisect reset &&
+ git bisect start --term-new=fixed --term-old=unfixed HEAD $HASH1 &&
+ hash_skipped_from=$(git rev-parse --verify HEAD) &&
+ git bisect skip &&
+ hash_skipped_to=$(git rev-parse --verify HEAD) &&
+ test "$hash_skipped_from" != "$hash_skipped_to"
+'
+
+test_expect_success 'git bisect reset cleans bisection state properly' '
+ git bisect reset &&
+ git bisect start &&
+ git bisect good $HASH1 &&
+ git bisect bad $HASH4 &&
+ git bisect reset &&
+ test -z "$(git for-each-ref "refs/bisect/*")" &&
+ test_path_is_missing ".git/BISECT_EXPECTED_REV" &&
+ test_path_is_missing ".git/BISECT_ANCESTORS_OK" &&
+ test_path_is_missing ".git/BISECT_LOG" &&
+ test_path_is_missing ".git/BISECT_RUN" &&
+ test_path_is_missing ".git/BISECT_TERMS" &&
+ test_path_is_missing ".git/BISECT_HEAD" &&
+ test_path_is_missing ".git/BISECT_START"
+'
+
+test_expect_success 'bisect handles annotated tags' '
+ test_commit commit-one &&
+ git tag -m foo tag-one &&
+ test_commit commit-two &&
+ git tag -m foo tag-two &&
+ git bisect start &&
+ git bisect good tag-one &&
+ git bisect bad tag-two >output &&
+ bad=$(git rev-parse --verify tag-two^{commit}) &&
+ grep "$bad is the first bad commit" output
+'
+
+test_expect_success 'bisect run fails with exit code equals or greater than 128' '
+ write_script test_script.sh <<-\EOF &&
+ exit 128
+ EOF
+ test_must_fail git bisect run ./test_script.sh &&
+ write_script test_script.sh <<-\EOF &&
+ exit 255
+ EOF
+ test_must_fail git bisect run ./test_script.sh
+'
+
+test_expect_success 'bisect visualize with a filename with dash and space' '
+ echo "My test line" >>"./-hello 2" &&
+ git add -- "./-hello 2" &&
+ git commit --quiet -m "Add test line" -- "./-hello 2" &&
+ git bisect visualize -p -- "-hello 2"
+'
+
+test_expect_success 'bisect state output with multiple good commits' '
+ git bisect reset &&
+ git bisect start >output &&
+ grep "waiting for both good and bad commits" output &&
+ git bisect log >output &&
+ grep "waiting for both good and bad commits" output &&
+ git bisect good "$HASH1" >output &&
+ grep "waiting for bad commit, 1 good commit known" output &&
+ git bisect log >output &&
+ grep "waiting for bad commit, 1 good commit known" output &&
+ git bisect good "$HASH2" >output &&
+ grep "waiting for bad commit, 2 good commits known" output &&
+ git bisect log >output &&
+ grep "waiting for bad commit, 2 good commits known" output
+'
+
+test_expect_success 'bisect state output with bad commit' '
+ git bisect reset &&
+ git bisect start >output &&
+ grep "waiting for both good and bad commits" output &&
+ git bisect log >output &&
+ grep "waiting for both good and bad commits" output &&
+ git bisect bad "$HASH4" >output &&
+ grep -F "waiting for good commit(s), bad commit known" output &&
+ git bisect log >output &&
+ grep -F "waiting for good commit(s), bad commit known" output
+'
+
+test_expect_success 'verify correct error message' '
+ git bisect reset &&
+ git bisect start $HASH4 $HASH1 &&
+ write_script test_script.sh <<-\EOF &&
+ rm .git/BISECT*
+ EOF
+ test_must_fail git bisect run ./test_script.sh 2>error &&
+ grep "git bisect good.*exited with error code" error
+'
+
+test_done
diff --git a/t/t6040-tracking-info.sh b/t/t6040-tracking-info.sh
new file mode 100755
index 0000000..acc281c
--- /dev/null
+++ b/t/t6040-tracking-info.sh
@@ -0,0 +1,296 @@
+#!/bin/sh
+
+test_description='remote tracking stats'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+advance () {
+ echo "$1" >"$1" &&
+ git add "$1" &&
+ test_tick &&
+ git commit -m "$1"
+}
+
+test_expect_success setup '
+ advance a &&
+ advance b &&
+ advance c &&
+ git clone . test &&
+ (
+ cd test &&
+ git checkout -b b1 origin &&
+ git reset --hard HEAD^ &&
+ advance d &&
+ git checkout -b b2 origin &&
+ git reset --hard b1 &&
+ git checkout -b b3 origin &&
+ git reset --hard HEAD^ &&
+ git checkout -b b4 origin &&
+ advance e &&
+ advance f &&
+ git checkout -b brokenbase origin &&
+ git checkout -b b5 --track brokenbase &&
+ advance g &&
+ git branch -d brokenbase &&
+ git checkout -b b6 origin
+ ) &&
+ git checkout -b follower --track main &&
+ advance h
+'
+
+t6040_script='s/^..\(b.\) *[0-9a-f]* \(.*\)$/\1 \2/p'
+cat >expect <<\EOF
+b1 [ahead 1, behind 1] d
+b2 [ahead 1, behind 1] d
+b3 [behind 1] b
+b4 [ahead 2] f
+b5 [gone] g
+b6 c
+EOF
+
+test_expect_success 'branch -v' '
+ (
+ cd test &&
+ git branch -v
+ ) |
+ sed -n -e "$t6040_script" >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<\EOF
+b1 [origin/main: ahead 1, behind 1] d
+b2 [origin/main: ahead 1, behind 1] d
+b3 [origin/main: behind 1] b
+b4 [origin/main: ahead 2] f
+b5 [brokenbase: gone] g
+b6 [origin/main] c
+EOF
+
+test_expect_success 'branch -vv' '
+ (
+ cd test &&
+ git branch -vv
+ ) |
+ sed -n -e "$t6040_script" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'checkout (diverged from upstream)' '
+ (
+ cd test && git checkout b1
+ ) >actual &&
+ test_grep "have 1 and 1 different" actual
+'
+
+test_expect_success 'checkout with local tracked branch' '
+ git checkout main &&
+ git checkout follower >actual &&
+ test_grep "is ahead of" actual
+'
+
+test_expect_success 'checkout (upstream is gone)' '
+ (
+ cd test &&
+ git checkout b5
+ ) >actual &&
+ test_grep "is based on .*, but the upstream is gone." actual
+'
+
+test_expect_success 'checkout (up-to-date with upstream)' '
+ (
+ cd test && git checkout b6
+ ) >actual &&
+ test_grep "Your branch is up to date with .origin/main" actual
+'
+
+test_expect_success 'status (diverged from upstream)' '
+ (
+ cd test &&
+ git checkout b1 >/dev/null &&
+ # reports nothing to commit
+ test_must_fail git commit --dry-run
+ ) >actual &&
+ test_grep "have 1 and 1 different" actual
+'
+
+test_expect_success 'status (upstream is gone)' '
+ (
+ cd test &&
+ git checkout b5 >/dev/null &&
+ # reports nothing to commit
+ test_must_fail git commit --dry-run
+ ) >actual &&
+ test_grep "is based on .*, but the upstream is gone." actual
+'
+
+test_expect_success 'status (up-to-date with upstream)' '
+ (
+ cd test &&
+ git checkout b6 >/dev/null &&
+ # reports nothing to commit
+ test_must_fail git commit --dry-run
+ ) >actual &&
+ test_grep "Your branch is up to date with .origin/main" actual
+'
+
+cat >expect <<\EOF
+## b1...origin/main [ahead 1, behind 1]
+EOF
+
+test_expect_success 'status -s -b (diverged from upstream)' '
+ (
+ cd test &&
+ git checkout b1 >/dev/null &&
+ git status -s -b | head -1
+ ) >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<\EOF
+## b1...origin/main [different]
+EOF
+
+test_expect_success 'status -s -b --no-ahead-behind (diverged from upstream)' '
+ (
+ cd test &&
+ git checkout b1 >/dev/null &&
+ git status -s -b --no-ahead-behind | head -1
+ ) >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<\EOF
+## b1...origin/main [different]
+EOF
+
+test_expect_success 'status.aheadbehind=false status -s -b (diverged from upstream)' '
+ (
+ cd test &&
+ git checkout b1 >/dev/null &&
+ git -c status.aheadbehind=false status -s -b | head -1
+ ) >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<\EOF
+On branch b1
+Your branch and 'origin/main' have diverged,
+and have 1 and 1 different commits each, respectively.
+EOF
+
+test_expect_success 'status --long --branch' '
+ (
+ cd test &&
+ git checkout b1 >/dev/null &&
+ git status --long -b | head -3
+ ) >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'status --long --branch' '
+ (
+ cd test &&
+ git checkout b1 >/dev/null &&
+ git -c status.aheadbehind=true status --long -b | head -3
+ ) >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<\EOF
+On branch b1
+Your branch and 'origin/main' refer to different commits.
+EOF
+
+test_expect_success 'status --long --branch --no-ahead-behind' '
+ (
+ cd test &&
+ git checkout b1 >/dev/null &&
+ git status --long -b --no-ahead-behind | head -2
+ ) >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'status.aheadbehind=false status --long --branch' '
+ (
+ cd test &&
+ git checkout b1 >/dev/null &&
+ git -c status.aheadbehind=false status --long -b | head -2
+ ) >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<\EOF
+## b5...brokenbase [gone]
+EOF
+
+test_expect_success 'status -s -b (upstream is gone)' '
+ (
+ cd test &&
+ git checkout b5 >/dev/null &&
+ git status -s -b | head -1
+ ) >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<\EOF
+## b6...origin/main
+EOF
+
+test_expect_success 'status -s -b (up-to-date with upstream)' '
+ (
+ cd test &&
+ git checkout b6 >/dev/null &&
+ git status -s -b | head -1
+ ) >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'fail to track lightweight tags' '
+ git checkout main &&
+ git tag light &&
+ test_must_fail git branch --track lighttrack light >actual &&
+ test_grep ! "set up to track" actual &&
+ test_must_fail git checkout lighttrack
+'
+
+test_expect_success 'fail to track annotated tags' '
+ git checkout main &&
+ git tag -m heavy heavy &&
+ test_must_fail git branch --track heavytrack heavy >actual &&
+ test_grep ! "set up to track" actual &&
+ test_must_fail git checkout heavytrack
+'
+
+test_expect_success '--set-upstream-to does not change branch' '
+ git branch from-main main &&
+ git branch --set-upstream-to main from-main &&
+ git branch from-topic_2 main &&
+ test_must_fail git config branch.from-topic_2.merge > actual &&
+ git rev-list from-topic_2 &&
+ git update-ref refs/heads/from-topic_2 from-topic_2^ &&
+ git rev-parse from-topic_2 >expect2 &&
+ git branch --set-upstream-to main from-topic_2 &&
+ git config branch.from-main.merge > actual &&
+ git rev-parse from-topic_2 >actual2 &&
+ grep -q "^refs/heads/main$" actual &&
+ cmp expect2 actual2
+'
+
+test_expect_success '--set-upstream-to @{-1}' '
+ git checkout follower &&
+ git checkout from-topic_2 &&
+ git config branch.from-topic_2.merge > expect2 &&
+ git branch --set-upstream-to @{-1} from-main &&
+ git config branch.from-main.merge > actual &&
+ git config branch.from-topic_2.merge > actual2 &&
+ git branch --set-upstream-to follower from-main &&
+ git config branch.from-main.merge > expect &&
+ test_cmp expect2 actual2 &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t6041-bisect-submodule.sh b/t/t6041-bisect-submodule.sh
new file mode 100755
index 0000000..82013fc
--- /dev/null
+++ b/t/t6041-bisect-submodule.sh
@@ -0,0 +1,37 @@
+#!/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" cf "$TRASH_DIRECTORY/tmp.tar" * &&
+ GOOD=$(git rev-parse --verify HEAD) &&
+ may_only_be_test_must_fail "$2" &&
+ $2 git checkout "$1" &&
+ if test -n "$2"
+ then
+ return
+ fi &&
+ 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" xf "$TRASH_DIRECTORY/tmp.tar" &&
+ git status -su >actual &&
+ ls -1pR * >>actual &&
+ test_cmp expect actual &&
+ git bisect bad $BAD
+}
+
+test_submodule_switch_func "git_bisect"
+
+test_done
diff --git a/t/t6050-replace.sh b/t/t6050-replace.sh
new file mode 100755
index 0000000..c6e9b33
--- /dev/null
+++ b/t/t6050-replace.sh
@@ -0,0 +1,510 @@
+#!/bin/sh
+#
+# Copyright (c) 2008 Christian Couder
+#
+test_description='Tests replace refs functionality'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY/lib-gpg.sh"
+
+add_and_commit_file ()
+{
+ _file="$1"
+ _msg="$2"
+
+ git add $_file || return $?
+ test_tick || return $?
+ git commit --quiet -m "$_file: $_msg"
+}
+
+commit_buffer_contains_parents ()
+{
+ git cat-file commit "$1" >payload &&
+ sed -n -e '/^$/q' -e '/^parent /p' <payload >actual &&
+ shift &&
+ for _parent
+ do
+ echo "parent $_parent"
+ done >expected &&
+ test_cmp expected actual
+}
+
+commit_peeling_shows_parents ()
+{
+ _parent_number=1
+ _commit="$1"
+ shift &&
+ for _parent
+ do
+ _found=$(git rev-parse --verify $_commit^$_parent_number) || return 1
+ test "$_found" = "$_parent" || return 1
+ _parent_number=$(( $_parent_number + 1 ))
+ done &&
+ test_must_fail git rev-parse --verify $_commit^$_parent_number 2>err &&
+ test_grep "Needed a single revision" err
+}
+
+commit_has_parents ()
+{
+ commit_buffer_contains_parents "$@" &&
+ commit_peeling_shows_parents "$@"
+}
+
+HASH1=
+HASH2=
+HASH3=
+HASH4=
+HASH5=
+HASH6=
+HASH7=
+
+test_expect_success 'set up buggy branch' '
+ echo "line 1" >>hello &&
+ echo "line 2" >>hello &&
+ echo "line 3" >>hello &&
+ echo "line 4" >>hello &&
+ add_and_commit_file hello "4 lines" &&
+ HASH1=$(git rev-parse --verify HEAD) &&
+ echo "line BUG" >>hello &&
+ echo "line 6" >>hello &&
+ echo "line 7" >>hello &&
+ echo "line 8" >>hello &&
+ add_and_commit_file hello "4 more lines with a BUG" &&
+ HASH2=$(git rev-parse --verify HEAD) &&
+ echo "line 9" >>hello &&
+ echo "line 10" >>hello &&
+ add_and_commit_file hello "2 more lines" &&
+ HASH3=$(git rev-parse --verify HEAD) &&
+ echo "line 11" >>hello &&
+ add_and_commit_file hello "1 more line" &&
+ HASH4=$(git rev-parse --verify HEAD) &&
+ sed -e "s/BUG/5/" hello >hello.new &&
+ mv hello.new hello &&
+ add_and_commit_file hello "BUG fixed" &&
+ HASH5=$(git rev-parse --verify HEAD) &&
+ echo "line 12" >>hello &&
+ echo "line 13" >>hello &&
+ add_and_commit_file hello "2 more lines" &&
+ HASH6=$(git rev-parse --verify HEAD) &&
+ echo "line 14" >>hello &&
+ echo "line 15" >>hello &&
+ echo "line 16" >>hello &&
+ add_and_commit_file hello "again 3 more lines" &&
+ HASH7=$(git rev-parse --verify HEAD)
+'
+
+test_expect_success 'replace the author' '
+ git cat-file commit $HASH2 | grep "author A U Thor" &&
+ R=$(git cat-file commit $HASH2 | sed -e "s/A U/O/" | git hash-object -t commit --stdin -w) &&
+ git cat-file commit $R | grep "author O Thor" &&
+ git update-ref refs/replace/$HASH2 $R &&
+ git show HEAD~5 | grep "O Thor" &&
+ git show $HASH2 | grep "O Thor"
+'
+
+test_expect_success 'test --no-replace-objects option' '
+ git cat-file commit $HASH2 | grep "author O Thor" &&
+ git --no-replace-objects cat-file commit $HASH2 | grep "author A U Thor" &&
+ git show $HASH2 | grep "O Thor" &&
+ git --no-replace-objects show $HASH2 | grep "A U Thor"
+'
+
+test_expect_success 'test GIT_NO_REPLACE_OBJECTS env variable' '
+ GIT_NO_REPLACE_OBJECTS=1 git cat-file commit $HASH2 | grep "author A U Thor" &&
+ GIT_NO_REPLACE_OBJECTS=1 git show $HASH2 | grep "A U Thor"
+'
+
+test_expect_success 'test core.usereplacerefs config option' '
+ test_config core.usereplacerefs false &&
+ git cat-file commit $HASH2 | grep "author A U Thor" &&
+ git show $HASH2 | grep "A U Thor"
+'
+
+cat >tag.sig <<EOF
+object $HASH2
+type commit
+tag mytag
+tagger T A Gger <> 0 +0000
+
+EOF
+
+test_expect_success 'tag replaced commit' '
+ git update-ref refs/tags/mytag $(git mktag <tag.sig)
+'
+
+test_expect_success '"git fsck" works' '
+ git fsck main >fsck_main.out &&
+ test_grep "dangling commit $R" fsck_main.out &&
+ test_grep "dangling tag $(git show-ref -s refs/tags/mytag)" fsck_main.out &&
+ test -z "$(git fsck)"
+'
+
+test_expect_success 'repack, clone and fetch work' '
+ git repack -a -d &&
+ git clone --no-hardlinks . clone_dir &&
+ (
+ cd clone_dir &&
+ git show HEAD~5 | grep "A U Thor" &&
+ git show $HASH2 | grep "A U Thor" &&
+ git cat-file commit $R &&
+ git repack -a -d &&
+ test_must_fail git cat-file commit $R &&
+ git fetch ../ "refs/replace/*:refs/replace/*" &&
+ git show HEAD~5 | grep "O Thor" &&
+ git show $HASH2 | grep "O Thor" &&
+ git cat-file commit $R
+ )
+'
+
+test_expect_success '"git replace" listing and deleting' '
+ test "$HASH2" = "$(git replace -l)" &&
+ test "$HASH2" = "$(git replace)" &&
+ aa=${HASH2%??????????????????????????????????????} &&
+ test "$HASH2" = "$(git replace --list "$aa*")" &&
+ test_must_fail git replace -d $R &&
+ test_must_fail git replace --delete &&
+ test_must_fail git replace -l -d $HASH2 &&
+ git replace -d $HASH2 &&
+ git show $HASH2 | grep "A U Thor" &&
+ test -z "$(git replace -l)"
+'
+
+test_expect_success '"git replace" replacing' '
+ git replace $HASH2 $R &&
+ git show $HASH2 | grep "O Thor" &&
+ test_must_fail git replace $HASH2 $R &&
+ git replace -f $HASH2 $R &&
+ test_must_fail git replace -f &&
+ test "$HASH2" = "$(git replace)"
+'
+
+test_expect_success '"git replace" resolves sha1' '
+ SHORTHASH2=$(git rev-parse --short=8 $HASH2) &&
+ git replace -d $SHORTHASH2 &&
+ git replace $SHORTHASH2 $R &&
+ git show $HASH2 | grep "O Thor" &&
+ test_must_fail git replace $HASH2 $R &&
+ git replace -f $HASH2 $R &&
+ test_must_fail git replace --force &&
+ test "$HASH2" = "$(git replace)"
+'
+
+# This creates a side branch where the bug in H2
+# does not appear because P2 is created by applying
+# H2 and squashing H5 into it.
+# P3, P4 and P6 are created by cherry-picking H3, H4
+# and H6 respectively.
+#
+# At this point, we should have the following:
+#
+# P2--P3--P4--P6
+# /
+# H1-H2-H3-H4-H5-H6-H7
+#
+# Then we replace H6 with P6.
+#
+test_expect_success 'create parallel branch without the bug' '
+ git replace -d $HASH2 &&
+ git show $HASH2 | grep "A U Thor" &&
+ git checkout $HASH1 &&
+ git cherry-pick $HASH2 &&
+ git show $HASH5 | git apply &&
+ git commit --amend -m "hello: 4 more lines WITHOUT the bug" hello &&
+ PARA2=$(git rev-parse --verify HEAD) &&
+ git cherry-pick $HASH3 &&
+ PARA3=$(git rev-parse --verify HEAD) &&
+ git cherry-pick $HASH4 &&
+ PARA4=$(git rev-parse --verify HEAD) &&
+ git cherry-pick $HASH6 &&
+ PARA6=$(git rev-parse --verify HEAD) &&
+ git replace $HASH6 $PARA6 &&
+ git checkout main &&
+ cur=$(git rev-parse --verify HEAD) &&
+ test "$cur" = "$HASH7" &&
+ git log --pretty=oneline | grep $PARA2 &&
+ git remote add cloned ./clone_dir
+'
+
+test_expect_success 'push to cloned repo' '
+ git push cloned $HASH6^:refs/heads/parallel &&
+ (
+ cd clone_dir &&
+ git checkout parallel &&
+ git log --pretty=oneline | grep $PARA2
+ )
+'
+
+test_expect_success 'push branch with replacement' '
+ git cat-file commit $PARA3 | grep "author A U Thor" &&
+ S=$(git cat-file commit $PARA3 | sed -e "s/A U/O/" | git hash-object -t commit --stdin -w) &&
+ git cat-file commit $S | grep "author O Thor" &&
+ git replace $PARA3 $S &&
+ git show $HASH6~2 | grep "O Thor" &&
+ git show $PARA3 | grep "O Thor" &&
+ git push cloned $HASH6^:refs/heads/parallel2 &&
+ (
+ cd clone_dir &&
+ git checkout parallel2 &&
+ git log --pretty=oneline | grep $PARA3 &&
+ git show $PARA3 | grep "A U Thor"
+ )
+'
+
+test_expect_success 'fetch branch with replacement' '
+ git branch tofetch $HASH6 &&
+ (
+ cd clone_dir &&
+ git fetch origin refs/heads/tofetch:refs/heads/parallel3 &&
+ git log --pretty=oneline parallel3 >output.txt &&
+ ! grep $PARA3 output.txt &&
+ git show $PARA3 >para3.txt &&
+ grep "A U Thor" para3.txt &&
+ git fetch origin "refs/replace/*:refs/replace/*" &&
+ git log --pretty=oneline parallel3 >output.txt &&
+ grep $PARA3 output.txt &&
+ git show $PARA3 >para3.txt &&
+ grep "O Thor" para3.txt
+ )
+'
+
+test_expect_success 'bisect and replacements' '
+ git bisect start $HASH7 $HASH1 &&
+ test "$PARA3" = "$(git rev-parse --verify HEAD)" &&
+ git bisect reset &&
+ GIT_NO_REPLACE_OBJECTS=1 git bisect start $HASH7 $HASH1 &&
+ test "$HASH4" = "$(git rev-parse --verify HEAD)" &&
+ git bisect reset &&
+ git --no-replace-objects bisect start $HASH7 $HASH1 &&
+ test "$HASH4" = "$(git rev-parse --verify HEAD)" &&
+ git bisect reset
+'
+
+test_expect_success 'index-pack and replacements' '
+ git --no-replace-objects rev-list --objects HEAD |
+ git --no-replace-objects pack-objects test- &&
+ git index-pack test-*.pack
+'
+
+test_expect_success 'not just commits' '
+ echo replaced >file &&
+ git add file &&
+ REPLACED=$(git rev-parse :file) &&
+ mv file file.replaced &&
+
+ echo original >file &&
+ git add file &&
+ ORIGINAL=$(git rev-parse :file) &&
+ git update-ref refs/replace/$ORIGINAL $REPLACED &&
+ mv file file.original &&
+
+ git checkout file &&
+ test_cmp file.replaced file
+'
+
+test_expect_success 'replaced and replacement objects must be of the same type' '
+ test_must_fail git replace mytag $HASH1 &&
+ test_must_fail git replace HEAD^{tree} HEAD~1 &&
+ BLOB=$(git rev-parse :file) &&
+ test_must_fail git replace HEAD^ $BLOB
+'
+
+test_expect_success '-f option bypasses the type check' '
+ git replace -f mytag $HASH1 &&
+ git replace --force HEAD^{tree} HEAD~1 &&
+ git replace -f HEAD^ $BLOB
+'
+
+test_expect_success 'git cat-file --batch works on replace objects' '
+ git replace | grep $PARA3 &&
+ echo $PARA3 | git cat-file --batch
+'
+
+test_expect_success 'test --format bogus' '
+ test_must_fail git replace --format bogus >/dev/null 2>&1
+'
+
+test_expect_success 'test --format short' '
+ git replace --format=short >actual &&
+ git replace >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'test --format medium' '
+ H1=$(git --no-replace-objects rev-parse HEAD~1) &&
+ HT=$(git --no-replace-objects rev-parse HEAD^{tree}) &&
+ MYTAG=$(git --no-replace-objects rev-parse mytag) &&
+ {
+ echo "$H1 -> $BLOB" &&
+ echo "$BLOB -> $REPLACED" &&
+ echo "$HT -> $H1" &&
+ echo "$PARA3 -> $S" &&
+ echo "$MYTAG -> $HASH1"
+ } | sort >expected &&
+ git replace -l --format medium | sort >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'test --format long' '
+ {
+ echo "$H1 (commit) -> $BLOB (blob)" &&
+ echo "$BLOB (blob) -> $REPLACED (blob)" &&
+ echo "$HT (tree) -> $H1 (commit)" &&
+ echo "$PARA3 (commit) -> $S (commit)" &&
+ echo "$MYTAG (tag) -> $HASH1 (commit)"
+ } | sort >expected &&
+ git replace --format=long | sort >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'setup fake editors' '
+ write_script fakeeditor <<-\EOF &&
+ sed -e "s/A U Thor/A fake Thor/" "$1" >"$1.new"
+ mv "$1.new" "$1"
+ EOF
+ write_script failingfakeeditor <<-\EOF
+ ./fakeeditor "$@"
+ false
+ EOF
+'
+
+test_expect_success '--edit with and without already replaced object' '
+ test_must_fail env GIT_EDITOR=./fakeeditor git replace --edit "$PARA3" &&
+ GIT_EDITOR=./fakeeditor git replace --force --edit "$PARA3" &&
+ git replace -l | grep "$PARA3" &&
+ git cat-file commit "$PARA3" | grep "A fake Thor" &&
+ git replace -d "$PARA3" &&
+ GIT_EDITOR=./fakeeditor git replace --edit "$PARA3" &&
+ git replace -l | grep "$PARA3" &&
+ git cat-file commit "$PARA3" | grep "A fake Thor"
+'
+
+test_expect_success '--edit and change nothing or command failed' '
+ git replace -d "$PARA3" &&
+ test_must_fail env GIT_EDITOR=true git replace --edit "$PARA3" &&
+ test_must_fail env GIT_EDITOR="./failingfakeeditor" git replace --edit "$PARA3" &&
+ GIT_EDITOR=./fakeeditor git replace --edit "$PARA3" &&
+ git replace -l | grep "$PARA3" &&
+ git cat-file commit "$PARA3" | grep "A fake Thor"
+'
+
+test_expect_success 'replace ref cleanup' '
+ test -n "$(git replace)" &&
+ git replace -d $(git replace) &&
+ test -z "$(git replace)"
+'
+
+test_expect_success '--graft with and without already replaced object' '
+ git log --oneline >log &&
+ test_line_count = 7 log &&
+ git replace --graft $HASH5 &&
+ git log --oneline >log &&
+ test_line_count = 3 log &&
+ commit_has_parents $HASH5 &&
+ test_must_fail git replace --graft $HASH5 $HASH4 $HASH3 &&
+ git replace --force -g $HASH5 $HASH4 $HASH3 &&
+ commit_has_parents $HASH5 $HASH4 $HASH3 &&
+ git replace -d $HASH5
+'
+
+test_expect_success '--graft using a tag as the new parent' '
+ git tag new_parent $HASH5 &&
+ git replace --graft $HASH7 new_parent &&
+ commit_has_parents $HASH7 $HASH5 &&
+ git replace -d $HASH7 &&
+ git tag -a -m "annotated new parent tag" annotated_new_parent $HASH5 &&
+ git replace --graft $HASH7 annotated_new_parent &&
+ commit_has_parents $HASH7 $HASH5 &&
+ git replace -d $HASH7
+'
+
+test_expect_success '--graft using a tag as the replaced object' '
+ git tag replaced_object $HASH7 &&
+ git replace --graft replaced_object $HASH5 &&
+ commit_has_parents $HASH7 $HASH5 &&
+ git replace -d $HASH7 &&
+ git tag -a -m "annotated replaced object tag" annotated_replaced_object $HASH7 &&
+ git replace --graft annotated_replaced_object $HASH5 &&
+ commit_has_parents $HASH7 $HASH5 &&
+ git replace -d $HASH7
+'
+
+test_expect_success GPG 'set up a signed commit' '
+ echo "line 17" >>hello &&
+ echo "line 18" >>hello &&
+ git add hello &&
+ test_tick &&
+ git commit --quiet -S -m "hello: 2 more lines in a signed commit" &&
+ HASH8=$(git rev-parse --verify HEAD) &&
+ git verify-commit $HASH8
+'
+
+test_expect_success GPG '--graft with a signed commit' '
+ git cat-file commit $HASH8 >orig &&
+ git replace --graft $HASH8 &&
+ git cat-file commit $HASH8 >repl &&
+ commit_has_parents $HASH8 &&
+ test_must_fail git verify-commit $HASH8 &&
+ sed -n -e "/^tree /p" -e "/^author /p" -e "/^committer /p" orig >expected &&
+ echo >>expected &&
+ sed -e "/^$/q" repl >actual &&
+ test_cmp expected actual &&
+ git replace -d $HASH8
+'
+
+test_expect_success GPG 'set up a merge commit with a mergetag' '
+ git reset --hard HEAD &&
+ git checkout -b test_branch HEAD~2 &&
+ echo "line 1 from test branch" >>hello &&
+ echo "line 2 from test branch" >>hello &&
+ git add hello &&
+ test_tick &&
+ git commit -m "hello: 2 more lines from a test branch" &&
+ HASH9=$(git rev-parse --verify HEAD) &&
+ git tag -s -m "tag for testing with a mergetag" test_tag HEAD &&
+ git checkout main &&
+ git merge -s ours test_tag &&
+ HASH10=$(git rev-parse --verify HEAD) &&
+ git cat-file commit $HASH10 | grep "^mergetag object"
+'
+
+test_expect_success GPG '--graft on a commit with a mergetag' '
+ test_must_fail git replace --graft $HASH10 $HASH8^1 &&
+ git replace --graft $HASH10 $HASH8^1 $HASH9 &&
+ git replace -d $HASH10
+'
+
+test_expect_success '--convert-graft-file' '
+ git checkout -b with-graft-file &&
+ test_commit root2 &&
+ git reset --hard root2^ &&
+ test_commit root1 &&
+ test_commit after-root1 &&
+ test_tick &&
+ git merge -m merge-root2 root2 &&
+
+ : add and convert graft file &&
+ printf "%s\n%s %s\n\n# comment\n%s\n" \
+ $(git rev-parse HEAD^^ HEAD^ HEAD^^ HEAD^2) \
+ >.git/info/grafts &&
+ git status 2>stderr &&
+ test_grep "hint:.*grafts is deprecated" stderr &&
+ git replace --convert-graft-file 2>stderr &&
+ test_grep ! "hint:.*grafts is deprecated" stderr &&
+ test_path_is_missing .git/info/grafts &&
+
+ : verify that the history is now "grafted" &&
+ git rev-list HEAD >out &&
+ test_line_count = 4 out &&
+
+ : create invalid graft file and verify that it is not deleted &&
+ test_when_finished "rm -f .git/info/grafts" &&
+ echo $EMPTY_BLOB $EMPTY_TREE >.git/info/grafts &&
+ test_must_fail git replace --convert-graft-file 2>err &&
+ test_grep "$EMPTY_BLOB $EMPTY_TREE" err &&
+ test_grep "$EMPTY_BLOB $EMPTY_TREE" .git/info/grafts
+'
+
+test_done
diff --git a/t/t6060-merge-index.sh b/t/t6060-merge-index.sh
new file mode 100755
index 0000000..1a8b64c
--- /dev/null
+++ b/t/t6060-merge-index.sh
@@ -0,0 +1,99 @@
+#!/bin/sh
+
+test_description='basic git merge-index / git-merge-one-file tests'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup diverging branches' '
+ test_write_lines 1 2 3 4 5 6 7 8 9 10 >file &&
+ git add file &&
+ git commit -m base &&
+ git tag base &&
+ sed s/2/two/ <file >tmp &&
+ mv tmp file &&
+ git commit -a -m two &&
+ git tag two &&
+ git checkout -b other HEAD^ &&
+ sed s/10/ten/ <file >tmp &&
+ mv tmp file &&
+ git commit -a -m ten &&
+ git tag ten
+'
+
+cat >expect-merged <<'EOF'
+1
+two
+3
+4
+5
+6
+7
+8
+9
+ten
+EOF
+
+test_expect_success 'read-tree does not resolve content merge' '
+ git read-tree -i -m base ten two &&
+ echo file >expect &&
+ git diff-files --name-only --diff-filter=U >unmerged &&
+ test_cmp expect unmerged
+'
+
+test_expect_success 'git merge-index git-merge-one-file resolves' '
+ git merge-index git-merge-one-file -a &&
+ git diff-files --name-only --diff-filter=U >unmerged &&
+ test_must_be_empty unmerged &&
+ test_cmp expect-merged file &&
+ git cat-file blob :file >file-index &&
+ test_cmp expect-merged file-index
+'
+
+test_expect_success 'setup bare merge' '
+ git clone --bare . bare.git &&
+ (cd bare.git &&
+ GIT_INDEX_FILE=$PWD/merge.index &&
+ export GIT_INDEX_FILE &&
+ git read-tree -i -m base ten two
+ )
+'
+
+test_expect_success 'merge-one-file fails without a work tree' '
+ (cd bare.git &&
+ GIT_INDEX_FILE=$PWD/merge.index &&
+ export GIT_INDEX_FILE &&
+ test_must_fail git merge-index git-merge-one-file -a
+ )
+'
+
+test_expect_success 'merge-one-file respects GIT_WORK_TREE' '
+ (cd bare.git &&
+ mkdir work &&
+ GIT_WORK_TREE=$PWD/work &&
+ export GIT_WORK_TREE &&
+ GIT_INDEX_FILE=$PWD/merge.index &&
+ export GIT_INDEX_FILE &&
+ git merge-index git-merge-one-file -a &&
+ git cat-file blob :file >work/file-index
+ ) &&
+ test_cmp expect-merged bare.git/work/file &&
+ test_cmp expect-merged bare.git/work/file-index
+'
+
+test_expect_success 'merge-one-file respects core.worktree' '
+ mkdir subdir &&
+ git clone . subdir/child &&
+ (cd subdir &&
+ GIT_DIR=$PWD/child/.git &&
+ export GIT_DIR &&
+ git config core.worktree "$PWD/child" &&
+ git read-tree -i -m base ten two &&
+ git merge-index git-merge-one-file -a &&
+ git cat-file blob :file >file-index
+ ) &&
+ test_cmp expect-merged subdir/child/file &&
+ test_cmp expect-merged subdir/file-index
+'
+
+test_done
diff --git a/t/t6100-rev-list-in-order.sh b/t/t6100-rev-list-in-order.sh
new file mode 100755
index 0000000..88ed7bd
--- /dev/null
+++ b/t/t6100-rev-list-in-order.sh
@@ -0,0 +1,78 @@
+#!/bin/sh
+
+test_description='rev-list testing in-commit-order'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup a commit history with trees, blobs' '
+ for x in one two three four
+ do
+ echo $x >$x &&
+ git add $x &&
+ git commit -m "add file $x" ||
+ return 1
+ done &&
+ for x in four three
+ do
+ git rm $x &&
+ git commit -m "remove $x" ||
+ return 1
+ done
+'
+
+test_expect_success 'rev-list --in-commit-order' '
+ git rev-list --in-commit-order --objects HEAD >actual.raw &&
+ cut -d" " -f1 >actual <actual.raw &&
+
+ git cat-file --batch-check="%(objectname)" >expect.raw <<-\EOF &&
+ HEAD^{commit}
+ HEAD^{tree}
+ HEAD^{tree}:one
+ HEAD^{tree}:two
+ HEAD~1^{commit}
+ HEAD~1^{tree}
+ HEAD~1^{tree}:three
+ HEAD~2^{commit}
+ HEAD~2^{tree}
+ HEAD~2^{tree}:four
+ HEAD~3^{commit}
+ # HEAD~3^{tree} skipped, same as HEAD~1^{tree}
+ HEAD~4^{commit}
+ # HEAD~4^{tree} skipped, same as HEAD^{tree}
+ HEAD~5^{commit}
+ HEAD~5^{tree}
+ EOF
+ grep -v "#" >expect <expect.raw &&
+
+ test_cmp expect actual
+'
+
+test_expect_success 'rev-list lists blobs and trees after commits' '
+ git rev-list --objects HEAD >actual.raw &&
+ cut -d" " -f1 >actual <actual.raw &&
+
+ git cat-file --batch-check="%(objectname)" >expect.raw <<-\EOF &&
+ HEAD^{commit}
+ HEAD~1^{commit}
+ HEAD~2^{commit}
+ HEAD~3^{commit}
+ HEAD~4^{commit}
+ HEAD~5^{commit}
+ HEAD^{tree}
+ HEAD^{tree}:one
+ HEAD^{tree}:two
+ HEAD~1^{tree}
+ HEAD~1^{tree}:three
+ HEAD~2^{tree}
+ HEAD~2^{tree}:four
+ # HEAD~3^{tree} skipped, same as HEAD~1^{tree}
+ # HEAD~4^{tree} skipped, same as HEAD^{tree}
+ HEAD~5^{tree}
+ EOF
+ grep -v "#" >expect <expect.raw &&
+
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t6101-rev-parse-parents.sh b/t/t6101-rev-parse-parents.sh
new file mode 100755
index 0000000..d20723d
--- /dev/null
+++ b/t/t6101-rev-parse-parents.sh
@@ -0,0 +1,231 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Johannes Schindelin
+#
+
+test_description='Test git rev-parse with different parent options'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+TEST_CREATE_REPO_NO_TEMPLATE=1
+. ./test-lib.sh
+
+test_cmp_rev_output () {
+ git rev-parse --verify "$1" >expect &&
+ eval "$2" >actual &&
+ test_cmp expect actual
+}
+
+test_expect_success 'setup' '
+ test_commit start &&
+ test_commit second &&
+ git checkout --orphan tmp &&
+ test_commit start2 &&
+ git checkout main &&
+ git merge -m next --allow-unrelated-histories start2 &&
+ test_commit final &&
+
+ mkdir .git/info &&
+ test_seq 40 |
+ while read i
+ do
+ git checkout --orphan "b$i" &&
+ test_tick &&
+ git commit --allow-empty -m "$i" &&
+ commit=$(git rev-parse --verify HEAD) &&
+ printf "$commit " >>.git/info/grafts || return 1
+ done
+'
+
+test_expect_success 'start is valid' '
+ git rev-parse start | grep "^$OID_REGEX$"
+'
+
+test_expect_success 'start^0' '
+ test_cmp_rev_output tags/start "git rev-parse start^0"
+'
+
+test_expect_success 'start^1 not valid' '
+ test_must_fail git rev-parse --verify start^1
+'
+
+test_expect_success 'second^1 = second^' '
+ test_cmp_rev_output second^ "git rev-parse second^1"
+'
+
+test_expect_success 'final^1^1^1' '
+ test_cmp_rev_output start "git rev-parse final^1^1^1"
+'
+
+test_expect_success 'final^1^1^1 = final^^^' '
+ test_cmp_rev_output final^^^ "git rev-parse final^1^1^1"
+'
+
+test_expect_success 'final^1^2' '
+ test_cmp_rev_output start2 "git rev-parse final^1^2"
+'
+
+test_expect_success 'final^1^2 != final^1^1' '
+ test $(git rev-parse final^1^2) != $(git rev-parse final^1^1)
+'
+
+test_expect_success 'final^1^3 not valid' '
+ test_must_fail git rev-parse --verify final^1^3
+'
+
+test_expect_success '--verify start2^1' '
+ test_must_fail git rev-parse --verify start2^1
+'
+
+test_expect_success '--verify start2^0' '
+ git rev-parse --verify start2^0
+'
+
+test_expect_success 'final^1^@ = final^1^1 final^1^2' '
+ git rev-parse final^1^1 final^1^2 >expect &&
+ git rev-parse final^1^@ >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'symbolic final^1^@ = final^1^1 final^1^2' '
+ git rev-parse --symbolic final^1^1 final^1^2 >expect &&
+ git rev-parse --symbolic final^1^@ >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'final^1^! = final^1 ^final^1^1 ^final^1^2' '
+ git rev-parse final^1 ^final^1^1 ^final^1^2 >expect &&
+ git rev-parse final^1^! >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'symbolic final^1^! = final^1 ^final^1^1 ^final^1^2' '
+ git rev-parse --symbolic final^1 ^final^1^1 ^final^1^2 >expect &&
+ git rev-parse --symbolic final^1^! >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'large graft octopus' '
+ test_cmp_rev_output b31 "git rev-parse --verify b1^30"
+'
+
+test_expect_success 'repack for next test' '
+ git repack -a -d
+'
+
+test_expect_success 'short SHA-1 works' '
+ start=$(git rev-parse --verify start) &&
+ test_cmp_rev_output start "git rev-parse ${start%?}"
+'
+
+# rev^- tests; we can use a simpler setup for these
+
+test_expect_success 'setup for rev^- tests' '
+ test_commit one &&
+ test_commit two &&
+ test_commit three &&
+
+ # Merge in a branch for testing rev^-
+ git checkout -b branch &&
+ git checkout HEAD^^ &&
+ git merge -m merge --no-edit --no-ff branch &&
+ git checkout -b merge
+'
+
+# The merged branch has 2 commits + the merge
+test_expect_success 'rev-list --count merge^- = merge^..merge' '
+ git rev-list --count merge^..merge >expect &&
+ echo 3 >actual &&
+ test_cmp expect actual
+'
+
+# All rev^- rev-parse tests
+
+test_expect_success 'rev-parse merge^- = merge^..merge' '
+ git rev-parse merge^..merge >expect &&
+ git rev-parse merge^- >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rev-parse merge^-1 = merge^..merge' '
+ git rev-parse merge^1..merge >expect &&
+ git rev-parse merge^-1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rev-parse merge^-2 = merge^2..merge' '
+ git rev-parse merge^2..merge >expect &&
+ git rev-parse merge^-2 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'symbolic merge^-1 = merge^1..merge' '
+ git rev-parse --symbolic merge^1..merge >expect &&
+ git rev-parse --symbolic merge^-1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rev-parse merge^-0 (invalid parent)' '
+ test_must_fail git rev-parse merge^-0
+'
+
+test_expect_success 'rev-parse merge^-3 (invalid parent)' '
+ test_must_fail git rev-parse merge^-3
+'
+
+test_expect_success 'rev-parse merge^-^ (garbage after ^-)' '
+ test_must_fail git rev-parse merge^-^
+'
+
+test_expect_success 'rev-parse merge^-1x (garbage after ^-1)' '
+ test_must_fail git rev-parse merge^-1x
+'
+
+# All rev^- rev-list tests (should be mostly the same as rev-parse; the reason
+# for the duplication is that rev-parse and rev-list use different parsers).
+
+test_expect_success 'rev-list merge^- = merge^..merge' '
+ git rev-list merge^..merge >expect &&
+ git rev-list merge^- >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rev-list merge^-1 = merge^1..merge' '
+ git rev-list merge^1..merge >expect &&
+ git rev-list merge^-1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rev-list merge^-2 = merge^2..merge' '
+ git rev-list merge^2..merge >expect &&
+ git rev-list merge^-2 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rev-list merge^-0 (invalid parent)' '
+ test_must_fail git rev-list merge^-0
+'
+
+test_expect_success 'rev-list merge^-3 (invalid parent)' '
+ test_must_fail git rev-list merge^-3
+'
+
+test_expect_success 'rev-list merge^-^ (garbage after ^-)' '
+ test_must_fail git rev-list merge^-^
+'
+
+test_expect_success 'rev-list merge^-1x (garbage after ^-1)' '
+ test_must_fail git rev-list merge^-1x
+'
+
+test_expect_success 'rev-parse $garbage^@ does not segfault' '
+ test_must_fail git rev-parse $EMPTY_TREE^@
+'
+
+test_expect_success 'rev-parse $garbage...$garbage does not segfault' '
+ test_must_fail git rev-parse $EMPTY_TREE...$EMPTY_BLOB
+'
+
+test_done
diff --git a/t/t6102-rev-list-unexpected-objects.sh b/t/t6102-rev-list-unexpected-objects.sh
new file mode 100755
index 0000000..5d28507
--- /dev/null
+++ b/t/t6102-rev-list-unexpected-objects.sh
@@ -0,0 +1,133 @@
+#!/bin/sh
+
+test_description='git rev-list should handle unexpected object types'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup well-formed objects' '
+ blob="$(printf "foo" | git hash-object -w --stdin)" &&
+ tree="$(printf "100644 blob $blob\tfoo" | git mktree)" &&
+ commit="$(git commit-tree $tree -m "first commit")" &&
+ git cat-file commit $commit >good-commit
+'
+
+test_expect_success 'setup unexpected non-blob entry' '
+ printf "100644 foo\0$(echo $tree | hex2oct)" >broken-tree &&
+ broken_tree="$(git hash-object -w --literally -t tree broken-tree)"
+'
+
+test_expect_success 'TODO (should fail!): traverse unexpected non-blob entry (lone)' '
+ sed "s/Z$//" >expect <<-EOF &&
+ $broken_tree Z
+ $tree foo
+ EOF
+ git rev-list --objects $broken_tree >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'traverse unexpected non-blob entry (seen)' '
+ test_must_fail git rev-list --objects $tree $broken_tree >output 2>&1 &&
+ test_grep "is not a blob" output
+'
+
+test_expect_success 'setup unexpected non-tree entry' '
+ printf "40000 foo\0$(echo $blob | hex2oct)" >broken-tree &&
+ broken_tree="$(git hash-object -w --literally -t tree broken-tree)"
+'
+
+test_expect_success 'traverse unexpected non-tree entry (lone)' '
+ test_must_fail git rev-list --objects $broken_tree
+'
+
+test_expect_success 'traverse unexpected non-tree entry (seen)' '
+ test_must_fail git rev-list --objects $blob $broken_tree >output 2>&1 &&
+ test_grep "is not a tree" output
+'
+
+test_expect_success 'setup unexpected non-commit parent' '
+ sed "/^author/ { h; s/.*/parent $blob/; G; }" <good-commit \
+ >broken-commit &&
+ broken_commit="$(git hash-object -w --literally -t commit \
+ broken-commit)"
+'
+
+test_expect_success 'traverse unexpected non-commit parent (lone)' '
+ test_must_fail git rev-list --objects $broken_commit >output 2>&1 &&
+ test_grep "not a commit" output
+'
+
+test_expect_success 'traverse unexpected non-commit parent (seen)' '
+ test_must_fail git rev-list --objects $blob $broken_commit \
+ >output 2>&1 &&
+ test_grep "not a commit" output
+'
+
+test_expect_success 'setup unexpected non-tree root' '
+ sed -e "s/$tree/$blob/" <good-commit >broken-commit &&
+ broken_commit="$(git hash-object -w --literally -t commit \
+ broken-commit)"
+'
+
+test_expect_success 'traverse unexpected non-tree root (lone)' '
+ test_must_fail git rev-list --objects $broken_commit
+'
+
+test_expect_success 'traverse unexpected non-tree root (seen)' '
+ test_must_fail git rev-list --objects $blob $broken_commit \
+ >output 2>&1 &&
+ test_grep "not a tree" output
+'
+
+test_expect_success 'setup unexpected non-commit tag' '
+ git tag -a -m "tagged commit" tag $commit &&
+ git cat-file tag tag >good-tag &&
+ test_when_finished "git tag -d tag" &&
+ sed -e "s/$commit/$blob/" <good-tag >broken-tag &&
+ tag=$(git hash-object -w --literally -t tag broken-tag)
+'
+
+test_expect_success 'traverse unexpected non-commit tag (lone)' '
+ test_must_fail git rev-list --objects $tag
+'
+
+test_expect_success 'traverse unexpected non-commit tag (seen)' '
+ test_must_fail git rev-list --objects $blob $tag >output 2>&1 &&
+ test_grep "not a commit" output
+'
+
+test_expect_success 'setup unexpected non-tree tag' '
+ git tag -a -m "tagged tree" tag $tree &&
+ git cat-file tag tag >good-tag &&
+ test_when_finished "git tag -d tag" &&
+ sed -e "s/$tree/$blob/" <good-tag >broken-tag &&
+ tag=$(git hash-object -w --literally -t tag broken-tag)
+'
+
+test_expect_success 'traverse unexpected non-tree tag (lone)' '
+ test_must_fail git rev-list --objects $tag
+'
+
+test_expect_success 'traverse unexpected non-tree tag (seen)' '
+ test_must_fail git rev-list --objects $blob $tag >output 2>&1 &&
+ test_grep "not a tree" output
+'
+
+test_expect_success 'setup unexpected non-blob tag' '
+ git tag -a -m "tagged blob" tag $blob &&
+ git cat-file tag tag >good-tag &&
+ test_when_finished "git tag -d tag" &&
+ sed -e "s/$blob/$commit/" <good-tag >broken-tag &&
+ tag=$(git hash-object -w --literally -t tag broken-tag)
+'
+
+test_expect_success 'traverse unexpected non-blob tag (lone)' '
+ test_must_fail git rev-list --objects $tag
+'
+
+test_expect_success 'traverse unexpected non-blob tag (seen)' '
+ test_must_fail git rev-list --objects $commit $tag >output 2>&1 &&
+ test_grep "not a blob" output
+'
+
+test_done
diff --git a/t/t6110-rev-list-sparse.sh b/t/t6110-rev-list-sparse.sh
new file mode 100755
index 0000000..ddefc7f
--- /dev/null
+++ b/t/t6110-rev-list-sparse.sh
@@ -0,0 +1,24 @@
+#!/bin/sh
+
+test_description='operations that cull histories in unusual ways'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ test_commit A &&
+ test_commit B &&
+ test_commit C &&
+ git checkout -b side HEAD^ &&
+ test_commit D &&
+ test_commit E &&
+ git merge main
+'
+
+test_expect_success 'rev-list --first-parent --boundary' '
+ git rev-list --first-parent --boundary HEAD^..
+'
+
+test_done
diff --git a/t/t6111-rev-list-treesame.sh b/t/t6111-rev-list-treesame.sh
new file mode 100755
index 0000000..90ff141
--- /dev/null
+++ b/t/t6111-rev-list-treesame.sh
@@ -0,0 +1,197 @@
+#!/bin/sh
+#
+# ,---E--. *H----------. * marks !TREESAME parent paths
+# / \ / \*
+# *A--*B---D--*F-*G---------K-*L-*M
+# \ /* \ /
+# `-C-' `-*I-*J
+#
+# A creates "file", B and F change it.
+# Odd merge G takes the old version from B.
+# I changes it, but J reverts it, so K is TREESAME to both parents.
+# H and L both change "file", and M merges those changes.
+
+test_description='TREESAME and limiting'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+note () {
+ git tag "$1"
+}
+
+unnote () {
+ git name-rev --tags --annotate-stdin | \
+ sed -e "s|$OID_REGEX (tags/\([^)]*\))\([ ]\)|\1\2|g"
+}
+
+test_expect_success setup '
+ test_commit "Initial file" file "Hi there" A &&
+ git branch other-branch &&
+
+ test_commit "file=Hello" file "Hello" B &&
+ git branch third-branch &&
+
+ git checkout other-branch &&
+ test_commit "Added other" other "Hello" C &&
+
+ git checkout main &&
+ test_merge D other-branch &&
+
+ git checkout third-branch &&
+ test_commit "Third file" third "Nothing" E &&
+
+ git checkout main &&
+ test_commit "file=Blah" file "Blah" F &&
+
+ test_tick && git merge --no-commit third-branch &&
+ git checkout third-branch file &&
+ git commit &&
+ note G &&
+ git branch fiddler-branch &&
+
+ git checkout -b part2-branch &&
+ test_commit "file=Part 2" file "Part 2" H &&
+
+ git checkout fiddler-branch &&
+ test_commit "Bad commit" file "Silly" I &&
+
+ test_tick && git revert I && note J &&
+
+ git checkout main &&
+ test_tick && git merge --no-ff fiddler-branch &&
+ note K &&
+
+ test_commit "file=Part 1" file "Part 1" L &&
+
+ test_tick && test_must_fail git merge part2-branch &&
+ test_commit M file "Parts 1+2"
+'
+
+check_outcome () {
+ outcome=$1
+ shift
+
+ case "$1" in
+ *"("*)
+ FMT="%P %H | %s"
+ munge_actual="
+ s/^\([^ ]*\) \([^ ]*\) .*/(\1)\2/
+ s/ //g
+ s/()//
+ "
+ ;;
+ *)
+ FMT="%H | %s"
+ munge_actual="s/^\([^ ]*\) .*/\1/"
+ ;;
+ esac &&
+ printf "%s\n" $1 >expect &&
+ shift
+
+ param="$*" &&
+ test_expect_$outcome "log $param" '
+ git log --format="$FMT" $param |
+ unnote >actual &&
+ sed -e "$munge_actual" <actual >check &&
+ test_cmp expect check
+ '
+}
+
+check_result () {
+ check_outcome success "$@"
+}
+
+# Odd merge G drops a change in F. Important that G is listed in all
+# except the most basic list. Achieving this means normal merge D will also be
+# shown in normal full-history, as we can't distinguish unless we do a
+# simplification pass. After simplification, D is dropped but G remains.
+# Also, merge simplification of G should not drop the parent B that the default
+# simple history follows.
+check_result 'M L K J I H G F E D C B A'
+check_result '(LH)M (K)L (GJ)K (I)J (G)I (G)H (FE)G (D)F (B)E (BC)D (A)C (A)B A'
+check_result 'M H L K J I G E F D C B A' --topo-order
+check_result 'M L H B A' -- file
+check_result '(LH)M (B)L (B)H (A)B A' --parents -- file
+check_result 'M L J I H G F D B A' --full-history -- file
+check_result '(LH)M (K)L (GJ)K (I)J (G)I (G)H (FB)G (D)F (BA)D (A)B A' --full-history --parents -- file
+check_result '(LH)M (G)H (J)L (I)J (G)I (FB)G (B)F (A)B A' --simplify-merges -- file
+check_result 'M L K G F D B A' --first-parent
+check_result 'M L G F B A' --first-parent -- file
+
+# Check that odd merge G remains shown when F is the bottom.
+check_result 'M L K J I H G E' F..M
+check_result 'M H L K J I G E' F..M --topo-order
+check_result 'M L H' F..M -- file
+check_result '(LH)M (B)L (B)H' --parents F..M -- file
+check_result 'M L J I H G' F..M --full-history -- file
+check_result '(LH)M (K)L (GJ)K (I)J (G)I (G)H (FB)G' F..M --full-history --parents -- file
+check_result '(LH)M (G)H (J)L (I)J (G)I (FB)G' F..M --simplify-merges -- file
+check_result 'M L K J I H G' F..M --ancestry-path
+check_result 'M L J I H G' F..M --ancestry-path -- file
+check_result '(LH)M (K)L (GJ)K (I)J (G)I (G)H (FE)G' F..M --ancestry-path --parents -- file
+check_result '(LH)M (G)H (J)L (I)J (G)I (FE)G' F..M --ancestry-path --simplify-merges -- file
+check_result 'M L K G' F..M --first-parent
+check_result 'M L G' F..M --first-parent -- file
+
+# Note that G is pruned when E is the bottom, even if it's the same commit list
+# If we want history since E, then we're quite happy to ignore G that took E.
+check_result 'M L K J I H G' E..M --ancestry-path
+check_result 'M L J I H' E..M --ancestry-path -- file
+check_result '(LH)M (K)L (EJ)K (I)J (E)I (E)H' E..M --ancestry-path --parents -- file
+check_result '(LH)M (E)H (J)L (I)J (E)I' E..M --ancestry-path --simplify-merges -- file
+
+# Should still be able to ignore I-J branch in simple log, despite limiting
+# to G.
+check_result 'M L K J I H' G..M
+check_result 'M H L K J I' G..M --topo-order
+check_result 'M L H' G..M -- file
+check_result '(LH)M (G)L (G)H' G..M --parents -- file
+check_result 'M L J I H' G..M --full-history -- file
+check_result 'M L K J I H' G..M --full-history --parents -- file
+check_result 'M H L J I' G..M --simplify-merges -- file
+check_result 'M L K J I H' G..M --ancestry-path
+check_result 'M L J I H' G..M --ancestry-path -- file
+check_result 'M L K J I H' G..M --ancestry-path --parents -- file
+check_result 'M H L J I' G..M --ancestry-path --simplify-merges -- file
+
+# B..F should be able to simplify the merge D from irrelevant side branch C.
+# Default log should also be free to follow B-D, and ignore C.
+# But --full-history shouldn't drop D on its own - without simplification,
+# we can't decide if the merge from INTERESTING commit C was sensible.
+check_result 'F D C' B..F
+check_result 'F' B..F -- file
+check_result '(B)F' B..F --parents -- file
+check_result 'F D' B..F --full-history -- file
+check_result '(D)F (BA)D' B..F --full-history --parents -- file
+check_result '(B)F' B..F --simplify-merges -- file
+check_result 'F D' B..F --ancestry-path
+check_result 'F' B..F --ancestry-path -- file
+check_result 'F' B..F --ancestry-path --parents -- file
+check_result 'F' B..F --ancestry-path --simplify-merges -- file
+check_result 'F D' B..F --first-parent
+check_result 'F' B..F --first-parent -- file
+
+# E...F should be equivalent to E F ^B, and be able to drop D as above.
+check_result 'F' E F ^B -- file # includes D
+check_result 'F' E...F -- file # includes D
+
+# Any sort of full history of C..F should show D, as it's the connection to C,
+# and it differs from it.
+check_result 'F D B' C..F
+check_result 'F B' C..F -- file
+check_result '(B)F (A)B' C..F --parents -- file
+check_result 'F D B' C..F --full-history -- file
+check_result '(D)F (BC)D (A)B' C..F --full-history --parents -- file
+check_result '(D)F (BC)D (A)B' C..F --simplify-merges -- file
+check_result 'F D' C..F --ancestry-path
+check_result 'F D' C..F --ancestry-path -- file
+check_result 'F D' C..F --ancestry-path --parents -- file
+check_result 'F D' C..F --ancestry-path --simplify-merges -- file
+check_result 'F D B' C..F --first-parent
+check_result 'F B' C..F --first-parent -- file
+
+
+test_done
diff --git a/t/t6112-rev-list-filters-objects.sh b/t/t6112-rev-list-filters-objects.sh
new file mode 100755
index 0000000..52822b9
--- /dev/null
+++ b/t/t6112-rev-list-filters-objects.sh
@@ -0,0 +1,704 @@
+#!/bin/sh
+
+test_description='git rev-list using object filtering'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+# Test the blob:none filter.
+
+test_expect_success 'setup r1' '
+ echo "{print \$1}" >print_1.awk &&
+ echo "{print \$2}" >print_2.awk &&
+
+ git init r1 &&
+ for n in 1 2 3 4 5
+ do
+ echo "This is file: $n" > r1/file.$n &&
+ git -C r1 add file.$n &&
+ git -C r1 commit -m "$n" || return 1
+ done
+'
+
+test_expect_success 'verify blob:none omits all 5 blobs' '
+ git -C r1 ls-files -s file.1 file.2 file.3 file.4 file.5 \
+ >ls_files_result &&
+ awk -f print_2.awk ls_files_result |
+ sort >expected &&
+
+ git -C r1 rev-list --quiet --objects --filter-print-omitted \
+ --filter=blob:none HEAD >revs &&
+ awk -f print_1.awk revs |
+ sed "s/~//" |
+ sort >observed &&
+
+ test_cmp expected observed
+'
+
+test_expect_success 'specify blob explicitly prevents filtering' '
+ file_3=$(git -C r1 ls-files -s file.3 |
+ awk -f print_2.awk) &&
+
+ file_4=$(git -C r1 ls-files -s file.4 |
+ awk -f print_2.awk) &&
+
+ git -C r1 rev-list --objects --filter=blob:none HEAD $file_3 >observed &&
+ grep "$file_3" observed &&
+ ! grep "$file_4" observed
+'
+
+test_expect_success 'verify emitted+omitted == all' '
+ git -C r1 rev-list --objects HEAD >revs &&
+ awk -f print_1.awk revs |
+ sort >expected &&
+
+ git -C r1 rev-list --objects --filter-print-omitted --filter=blob:none \
+ HEAD >revs &&
+ awk -f print_1.awk revs |
+ sed "s/~//" |
+ sort >observed &&
+
+ test_cmp expected observed
+'
+
+
+# Test blob:limit=<n>[kmg] filter.
+# We boundary test around the size parameter. The filter is strictly less than
+# the value, so size 500 and 1000 should have the same results, but 1001 should
+# filter more.
+
+test_expect_success 'setup r2' '
+ git init r2 &&
+ for n in 1000 10000
+ do
+ printf "%"$n"s" X > r2/large.$n &&
+ git -C r2 add large.$n &&
+ git -C r2 commit -m "$n" || return 1
+ done
+'
+
+test_expect_success 'verify blob:limit=500 omits all blobs' '
+ git -C r2 ls-files -s large.1000 large.10000 >ls_files_result &&
+ awk -f print_2.awk ls_files_result |
+ sort >expected &&
+
+ git -C r2 rev-list --quiet --objects --filter-print-omitted \
+ --filter=blob:limit=500 HEAD >revs &&
+ awk -f print_1.awk revs |
+ sed "s/~//" |
+ sort >observed &&
+
+ test_cmp expected observed
+'
+
+test_expect_success 'verify emitted+omitted == all' '
+ git -C r2 rev-list --objects HEAD >revs &&
+ awk -f print_1.awk revs |
+ sort >expected &&
+
+ git -C r2 rev-list --objects --filter-print-omitted \
+ --filter=blob:limit=500 HEAD >revs &&
+ awk -f print_1.awk revs |
+ sed "s/~//" |
+ sort >observed &&
+
+ test_cmp expected observed
+'
+
+test_expect_success 'verify blob:limit=1000' '
+ git -C r2 ls-files -s large.1000 large.10000 >ls_files_result &&
+ awk -f print_2.awk ls_files_result |
+ sort >expected &&
+
+ git -C r2 rev-list --quiet --objects --filter-print-omitted \
+ --filter=blob:limit=1000 HEAD >revs &&
+ awk -f print_1.awk revs |
+ sed "s/~//" |
+ sort >observed &&
+
+ test_cmp expected observed
+'
+
+test_expect_success 'verify blob:limit=1001' '
+ git -C r2 ls-files -s large.10000 >ls_files_result &&
+ awk -f print_2.awk ls_files_result |
+ sort >expected &&
+
+ git -C r2 rev-list --quiet --objects --filter-print-omitted \
+ --filter=blob:limit=1001 HEAD >revs &&
+ awk -f print_1.awk revs |
+ sed "s/~//" |
+ sort >observed &&
+
+ test_cmp expected observed
+'
+
+test_expect_success 'verify blob:limit=1k' '
+ git -C r2 ls-files -s large.10000 >ls_files_result &&
+ awk -f print_2.awk ls_files_result |
+ sort >expected &&
+
+ git -C r2 rev-list --quiet --objects --filter-print-omitted \
+ --filter=blob:limit=1k HEAD >revs &&
+ awk -f print_1.awk revs |
+ sed "s/~//" |
+ sort >observed &&
+
+ test_cmp expected observed
+'
+
+test_expect_success 'verify blob:limit=1m' '
+ git -C r2 rev-list --quiet --objects --filter-print-omitted \
+ --filter=blob:limit=1m HEAD >revs &&
+ awk -f print_1.awk revs |
+ sed "s/~//" |
+ sort >observed &&
+
+ test_must_be_empty observed
+'
+
+# Test object:type=<type> filter.
+
+test_expect_success 'setup object-type' '
+ test_create_repo object-type &&
+ test_commit --no-tag -C object-type message blob &&
+ git -C object-type tag tag -m tag-message
+'
+
+test_expect_success 'verify object:type= fails with invalid type' '
+ test_must_fail git -C object-type rev-list --objects --filter=object:type= HEAD &&
+ test_must_fail git -C object-type rev-list --objects --filter=object:type=invalid HEAD
+'
+
+test_expect_success 'verify object:type=blob prints blob and commit' '
+ git -C object-type rev-parse HEAD >expected &&
+ printf "%s blob\n" $(git -C object-type rev-parse HEAD:blob) >>expected &&
+ git -C object-type rev-list --objects --filter=object:type=blob HEAD >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'verify object:type=tree prints tree and commit' '
+ (
+ git -C object-type rev-parse HEAD &&
+ printf "%s \n" $(git -C object-type rev-parse HEAD^{tree})
+ ) >expected &&
+ git -C object-type rev-list --objects --filter=object:type=tree HEAD >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'verify object:type=commit prints commit' '
+ git -C object-type rev-parse HEAD >expected &&
+ git -C object-type rev-list --objects --filter=object:type=commit HEAD >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'verify object:type=tag prints tag' '
+ (
+ git -C object-type rev-parse HEAD &&
+ printf "%s tag\n" $(git -C object-type rev-parse tag)
+ ) >expected &&
+ git -C object-type rev-list --objects --filter=object:type=tag tag >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'verify object:type=blob prints only blob with --filter-provided-objects' '
+ printf "%s blob\n" $(git -C object-type rev-parse HEAD:blob) >expected &&
+ git -C object-type rev-list --objects \
+ --filter=object:type=blob --filter-provided-objects HEAD >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'verify object:type=tree prints only tree with --filter-provided-objects' '
+ printf "%s \n" $(git -C object-type rev-parse HEAD^{tree}) >expected &&
+ git -C object-type rev-list --objects \
+ --filter=object:type=tree HEAD --filter-provided-objects >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'verify object:type=commit prints only commit with --filter-provided-objects' '
+ git -C object-type rev-parse HEAD >expected &&
+ git -C object-type rev-list --objects \
+ --filter=object:type=commit --filter-provided-objects HEAD >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'verify object:type=tag prints only tag with --filter-provided-objects' '
+ printf "%s tag\n" $(git -C object-type rev-parse tag) >expected &&
+ git -C object-type rev-list --objects \
+ --filter=object:type=tag --filter-provided-objects tag >actual &&
+ test_cmp expected actual
+'
+
+# Test sparse:path=<path> filter.
+# !!!!
+# NOTE: sparse:path filter support has been dropped for security reasons,
+# so the tests have been changed to make sure that using it fails.
+# !!!!
+# Use a local file containing a sparse-checkout specification to filter
+# out blobs not required for the corresponding sparse-checkout. We do not
+# require sparse-checkout to actually be enabled.
+
+test_expect_success 'setup r3' '
+ git init r3 &&
+ mkdir r3/dir1 &&
+ for n in sparse1 sparse2
+ do
+ echo "This is file: $n" > r3/$n &&
+ git -C r3 add $n &&
+ echo "This is file: dir1/$n" > r3/dir1/$n &&
+ git -C r3 add dir1/$n || return 1
+ done &&
+ git -C r3 commit -m "sparse" &&
+ echo dir1/ >pattern1 &&
+ echo sparse1 >pattern2
+'
+
+test_expect_success 'verify sparse:path=pattern1 fails' '
+ test_must_fail git -C r3 rev-list --quiet --objects \
+ --filter-print-omitted --filter=sparse:path=../pattern1 HEAD
+'
+
+test_expect_success 'verify sparse:path=pattern2 fails' '
+ test_must_fail git -C r3 rev-list --quiet --objects \
+ --filter-print-omitted --filter=sparse:path=../pattern2 HEAD
+'
+
+# Test sparse:oid=<oid-ish> filter.
+# Use a blob containing a sparse-checkout specification to filter
+# out blobs not required for the corresponding sparse-checkout. We do not
+# require sparse-checkout to actually be enabled.
+
+test_expect_success 'setup r3 part 2' '
+ echo dir1/ >r3/pattern &&
+ git -C r3 add pattern &&
+ git -C r3 commit -m "pattern"
+'
+
+test_expect_success 'verify sparse:oid=OID omits top-level files' '
+ git -C r3 ls-files -s pattern sparse1 sparse2 >ls_files_result &&
+ awk -f print_2.awk ls_files_result |
+ sort >expected &&
+
+ oid=$(git -C r3 ls-files -s pattern | awk -f print_2.awk) &&
+
+ git -C r3 rev-list --quiet --objects --filter-print-omitted \
+ --filter=sparse:oid=$oid HEAD >revs &&
+ awk -f print_1.awk revs |
+ sed "s/~//" |
+ sort >observed &&
+
+ test_cmp expected observed
+'
+
+test_expect_success 'verify sparse:oid=oid-ish omits top-level files' '
+ git -C r3 ls-files -s pattern sparse1 sparse2 >ls_files_result &&
+ awk -f print_2.awk ls_files_result |
+ sort >expected &&
+
+ git -C r3 rev-list --quiet --objects --filter-print-omitted \
+ --filter=sparse:oid=main:pattern HEAD >revs &&
+ awk -f print_1.awk revs |
+ sed "s/~//" |
+ sort >observed &&
+
+ test_cmp expected observed
+'
+
+test_expect_success 'rev-list W/ --missing=print and --missing=allow-any for trees' '
+ TREE=$(git -C r3 rev-parse HEAD:dir1) &&
+
+ # Create a spare repo because we will be deleting objects from this one.
+ git clone r3 r3.b &&
+
+ rm r3.b/.git/objects/$(echo $TREE | sed "s|^..|&/|") &&
+
+ git -C r3.b rev-list --quiet --missing=print --objects HEAD \
+ >missing_objs 2>rev_list_err &&
+ echo "?$TREE" >expected &&
+ test_cmp expected missing_objs &&
+
+ # do not complain when a missing tree cannot be parsed
+ test_must_be_empty rev_list_err &&
+
+ git -C r3.b rev-list --missing=allow-any --objects HEAD \
+ >objs 2>rev_list_err &&
+ ! grep $TREE objs &&
+ test_must_be_empty rev_list_err
+'
+
+# Test tree:0 filter.
+
+test_expect_success 'verify tree:0 includes trees in "filtered" output' '
+ git -C r3 rev-list --quiet --objects --filter-print-omitted \
+ --filter=tree:0 HEAD >revs &&
+
+ awk -f print_1.awk revs |
+ sed s/~// |
+ xargs -n1 git -C r3 cat-file -t >unsorted_filtered_types &&
+
+ sort -u unsorted_filtered_types >filtered_types &&
+ test_write_lines blob tree >expected &&
+ test_cmp expected filtered_types
+'
+
+# Make sure tree:0 does not iterate through any trees.
+
+test_expect_success 'verify skipping tree iteration when not collecting omits' '
+ GIT_TRACE=1 git -C r3 rev-list \
+ --objects --filter=tree:0 HEAD 2>filter_trace &&
+ grep "Skipping contents of tree [.][.][.]" filter_trace >actual &&
+ # One line for each commit traversed.
+ test_line_count = 2 actual &&
+
+ # Make sure no other trees were considered besides the root.
+ ! grep "Skipping contents of tree [^.]" filter_trace &&
+
+ # Try this again with "combine:". If both sub-filters are skipping
+ # trees, the composite filter should also skip trees. This is not
+ # important unless the user does combine:tree:X+tree:Y or another filter
+ # besides "tree:" is implemented in the future which can skip trees.
+ GIT_TRACE=1 git -C r3 rev-list \
+ --objects --filter=combine:tree:1+tree:3 HEAD 2>filter_trace &&
+
+ # Only skip the dir1/ tree, which is shared between the two commits.
+ grep "Skipping contents of tree " filter_trace >actual &&
+ test_write_lines "Skipping contents of tree dir1/..." >expected &&
+ test_cmp expected actual
+'
+
+# Test tree:# filters.
+
+expect_has () {
+ commit=$1 &&
+ name=$2 &&
+
+ hash=$(git -C r3 rev-parse $commit:$name) &&
+ grep "^$hash $name$" actual
+}
+
+test_expect_success 'verify tree:1 includes root trees' '
+ git -C r3 rev-list --objects --filter=tree:1 HEAD >actual &&
+
+ # We should get two root directories and two commits.
+ expect_has HEAD "" &&
+ expect_has HEAD~1 "" &&
+ test_line_count = 4 actual
+'
+
+test_expect_success 'verify tree:2 includes root trees and immediate children' '
+ git -C r3 rev-list --objects --filter=tree:2 HEAD >actual &&
+
+ expect_has HEAD "" &&
+ expect_has HEAD~1 "" &&
+ expect_has HEAD dir1 &&
+ expect_has HEAD pattern &&
+ expect_has HEAD sparse1 &&
+ expect_has HEAD sparse2 &&
+
+ # There are also 2 commit objects
+ test_line_count = 8 actual
+'
+
+test_expect_success 'verify tree:3 includes everything expected' '
+ git -C r3 rev-list --objects --filter=tree:3 HEAD >actual &&
+
+ expect_has HEAD "" &&
+ expect_has HEAD~1 "" &&
+ expect_has HEAD dir1 &&
+ expect_has HEAD dir1/sparse1 &&
+ expect_has HEAD dir1/sparse2 &&
+ expect_has HEAD pattern &&
+ expect_has HEAD sparse1 &&
+ expect_has HEAD sparse2 &&
+
+ # There are also 2 commit objects
+ test_line_count = 10 actual
+'
+
+test_expect_success 'combine:... for a simple combination' '
+ git -C r3 rev-list --objects --filter=combine:tree:2+blob:none HEAD \
+ >actual &&
+
+ expect_has HEAD "" &&
+ expect_has HEAD~1 "" &&
+ expect_has HEAD dir1 &&
+
+ # There are also 2 commit objects
+ test_line_count = 5 actual &&
+
+ cp actual expected &&
+
+ # Try again using repeated --filter - this is equivalent to a manual
+ # combine with "combine:...+..."
+ git -C r3 rev-list --objects --filter=combine:tree:2 \
+ --filter=blob:none HEAD >actual &&
+
+ test_cmp expected actual
+'
+
+test_expect_success 'combine:... with URL encoding' '
+ git -C r3 rev-list --objects \
+ --filter=combine:tree%3a2+blob:%6Eon%65 HEAD >actual &&
+
+ expect_has HEAD "" &&
+ expect_has HEAD~1 "" &&
+ expect_has HEAD dir1 &&
+
+ # There are also 2 commit objects
+ test_line_count = 5 actual
+'
+
+expect_invalid_filter_spec () {
+ spec="$1" &&
+ err="$2" &&
+
+ test_must_fail git -C r3 rev-list --objects --filter="$spec" HEAD \
+ >actual 2>actual_stderr &&
+ test_must_be_empty actual &&
+ test_grep "$err" actual_stderr
+}
+
+test_expect_success 'combine:... while URL-encoding things that should not be' '
+ expect_invalid_filter_spec combine%3Atree:2+blob:none \
+ "invalid filter-spec"
+'
+
+test_expect_success 'combine: with nothing after the :' '
+ expect_invalid_filter_spec combine: "expected something after combine:"
+'
+
+test_expect_success 'parse error in first sub-filter in combine:' '
+ expect_invalid_filter_spec combine:tree:asdf+blob:none \
+ "expected .tree:<depth>."
+'
+
+test_expect_success 'combine:... with non-encoded reserved chars' '
+ expect_invalid_filter_spec combine:tree:2+sparse:@xyz \
+ "must escape char in sub-filter-spec: .@." &&
+ expect_invalid_filter_spec combine:tree:2+sparse:\` \
+ "must escape char in sub-filter-spec: .\`." &&
+ expect_invalid_filter_spec combine:tree:2+sparse:~abc \
+ "must escape char in sub-filter-spec: .\~."
+'
+
+test_expect_success 'validate err msg for "combine:<valid-filter>+"' '
+ expect_invalid_filter_spec combine:tree:2+ "expected .tree:<depth>."
+'
+
+test_expect_success 'combine:... with edge-case hex digits: Ff Aa 0 9' '
+ git -C r3 rev-list --objects --filter="combine:tree:2+bl%6Fb:n%6fne" \
+ HEAD >actual &&
+ test_line_count = 5 actual &&
+ git -C r3 rev-list --objects --filter="combine:tree%3A2+blob%3anone" \
+ HEAD >actual &&
+ test_line_count = 5 actual &&
+ git -C r3 rev-list --objects --filter="combine:tree:%30" HEAD >actual &&
+ test_line_count = 2 actual &&
+ git -C r3 rev-list --objects --filter="combine:tree:%39+blob:none" \
+ HEAD >actual &&
+ test_line_count = 5 actual
+'
+
+test_expect_success 'add sparse pattern blobs whose paths have reserved chars' '
+ cp r3/pattern r3/pattern1+renamed% &&
+ cp r3/pattern "r3/p;at%ter+n" &&
+ cp r3/pattern r3/^~pattern &&
+ git -C r3 add pattern1+renamed% "p;at%ter+n" ^~pattern &&
+ git -C r3 commit -m "add sparse pattern files with reserved chars"
+'
+
+test_expect_success 'combine:... with more than two sub-filters' '
+ git -C r3 rev-list --objects \
+ --filter=combine:tree:3+blob:limit=40+sparse:oid=main:pattern \
+ HEAD >actual &&
+
+ expect_has HEAD "" &&
+ expect_has HEAD~1 "" &&
+ expect_has HEAD~2 "" &&
+ expect_has HEAD dir1 &&
+ expect_has HEAD dir1/sparse1 &&
+ expect_has HEAD dir1/sparse2 &&
+
+ # Should also have 3 commits
+ test_line_count = 9 actual &&
+
+ # Try again, this time making sure the last sub-filter is only
+ # URL-decoded once.
+ cp actual expect &&
+
+ git -C r3 rev-list --objects \
+ --filter=combine:tree:3+blob:limit=40+sparse:oid=main:pattern1%2brenamed%25 \
+ HEAD >actual &&
+ test_cmp expect actual &&
+
+ # Use the same composite filter again, but with a pattern file name that
+ # requires encoding multiple characters, and use implicit filter
+ # combining.
+ test_when_finished "rm -f trace1" &&
+ GIT_TRACE=$(pwd)/trace1 git -C r3 rev-list --objects \
+ --filter=tree:3 --filter=blob:limit=40 \
+ --filter=sparse:oid="main:p;at%ter+n" \
+ HEAD >actual &&
+
+ test_cmp expect actual &&
+ grep "Add to combine filter-spec: sparse:oid=main:p%3bat%25ter%2bn" \
+ trace1 &&
+
+ # Repeat the above test, but this time, the characters to encode are in
+ # the LHS of the combined filter.
+ test_when_finished "rm -f trace2" &&
+ GIT_TRACE=$(pwd)/trace2 git -C r3 rev-list --objects \
+ --filter=sparse:oid=main:^~pattern \
+ --filter=tree:3 --filter=blob:limit=40 \
+ HEAD >actual &&
+
+ test_cmp expect actual &&
+ grep "Add to combine filter-spec: sparse:oid=main:%5e%7epattern" \
+ trace2
+'
+
+# Test provisional omit collection logic with a repo that has objects appearing
+# at multiple depths - first deeper than the filter's threshold, then shallow.
+
+test_expect_success 'setup r4' '
+ git init r4 &&
+
+ echo foo > r4/foo &&
+ mkdir r4/subdir &&
+ echo bar > r4/subdir/bar &&
+
+ mkdir r4/filt &&
+ cp -r r4/foo r4/subdir r4/filt &&
+
+ git -C r4 add foo subdir filt &&
+ git -C r4 commit -m "commit msg"
+'
+
+expect_has_with_different_name () {
+ repo=$1 &&
+ name=$2 &&
+
+ hash=$(git -C $repo rev-parse HEAD:$name) &&
+ ! grep "^$hash $name$" actual &&
+ grep "^$hash " actual &&
+ ! grep "~$hash" actual
+}
+
+test_expect_success 'test tree:# filter provisional omit for blob and tree' '
+ git -C r4 rev-list --objects --filter-print-omitted --filter=tree:2 \
+ HEAD >actual &&
+ expect_has_with_different_name r4 filt/foo &&
+ expect_has_with_different_name r4 filt/subdir
+'
+
+test_expect_success 'verify skipping tree iteration when collecting omits' '
+ GIT_TRACE=1 git -C r4 rev-list --filter-print-omitted \
+ --objects --filter=tree:0 HEAD 2>filter_trace &&
+ grep "^Skipping contents of tree " filter_trace >actual &&
+
+ echo "Skipping contents of tree subdir/..." >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'setup r5' '
+ git init r5 &&
+ mkdir -p r5/subdir &&
+
+ echo 1 >r5/short-root &&
+ echo 12345 >r5/long-root &&
+ echo a >r5/subdir/short-subdir &&
+ echo abcde >r5/subdir/long-subdir &&
+
+ git -C r5 add short-root long-root subdir &&
+ git -C r5 commit -m "commit msg"
+'
+
+test_expect_success 'verify collecting omits in combined: filter' '
+ # Note that this test guards against the naive implementation of simply
+ # giving both filters the same "omits" set and expecting it to
+ # automatically merge them.
+ git -C r5 rev-list --objects --quiet --filter-print-omitted \
+ --filter=combine:tree:2+blob:limit=3 HEAD >actual &&
+
+ # Expect 0 trees/commits, 3 blobs omitted (all blobs except short-root)
+ omitted_1=$(echo 12345 | git hash-object --stdin) &&
+ omitted_2=$(echo a | git hash-object --stdin) &&
+ omitted_3=$(echo abcde | git hash-object --stdin) &&
+
+ grep ~$omitted_1 actual &&
+ grep ~$omitted_2 actual &&
+ grep ~$omitted_3 actual &&
+ test_line_count = 3 actual
+'
+
+# Test tree:<depth> where a tree is iterated to twice - once where a subentry is
+# too deep to be included, and again where the blob inside it is shallow enough
+# to be included. This makes sure we don't use LOFR_MARK_SEEN incorrectly (we
+# can't use it because a tree can be iterated over again at a lower depth).
+
+test_expect_success 'tree:<depth> where we iterate over tree at two levels' '
+ git init r5 &&
+
+ mkdir -p r5/a/subdir/b &&
+ echo foo > r5/a/subdir/b/foo &&
+
+ mkdir -p r5/subdir/b &&
+ echo foo > r5/subdir/b/foo &&
+
+ git -C r5 add a subdir &&
+ git -C r5 commit -m "commit msg" &&
+
+ git -C r5 rev-list --objects --filter=tree:4 HEAD >actual &&
+ expect_has_with_different_name r5 a/subdir/b/foo
+'
+
+test_expect_success 'tree:<depth> which filters out blob but given as arg' '
+ blob_hash=$(git -C r4 rev-parse HEAD:subdir/bar) &&
+
+ git -C r4 rev-list --objects --filter=tree:1 HEAD $blob_hash >actual &&
+ grep ^$blob_hash actual
+'
+
+# Delete some loose objects and use rev-list, but WITHOUT any filtering.
+# This models previously omitted objects that we did not receive.
+
+test_expect_success 'rev-list W/ --missing=print' '
+ git -C r1 ls-files -s file.1 file.2 file.3 file.4 file.5 \
+ >ls_files_result &&
+ awk -f print_2.awk ls_files_result |
+ sort >expected &&
+
+ for id in `cat expected | sed "s|..|&/|"`
+ do
+ rm r1/.git/objects/$id || return 1
+ done &&
+
+ git -C r1 rev-list --quiet --missing=print --objects HEAD >revs &&
+ awk -f print_1.awk revs |
+ sed "s/?//" |
+ sort >observed &&
+
+ test_cmp expected observed
+'
+
+test_expect_success 'rev-list W/O --missing fails' '
+ test_must_fail git -C r1 rev-list --quiet --objects HEAD
+'
+
+test_expect_success 'rev-list W/ missing=allow-any' '
+ git -C r1 rev-list --quiet --missing=allow-any --objects HEAD
+'
+
+# Test expansion of filter specs.
+
+test_expect_success 'expand blob limit in protocol' '
+ git -C r2 config --local uploadpack.allowfilter 1 &&
+ GIT_TRACE_PACKET="$(pwd)/trace" git -c protocol.version=2 clone \
+ --filter=blob:limit=1k "file://$(pwd)/r2" limit &&
+ ! grep "blob:limit=1k" trace &&
+ grep "blob:limit=1024" trace
+'
+
+test_done
diff --git a/t/t6113-rev-list-bitmap-filters.sh b/t/t6113-rev-list-bitmap-filters.sh
new file mode 100755
index 0000000..86c7052
--- /dev/null
+++ b/t/t6113-rev-list-bitmap-filters.sh
@@ -0,0 +1,157 @@
+#!/bin/sh
+
+test_description='rev-list combining bitmaps and filters'
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-bitmap.sh
+
+test_expect_success 'set up bitmapped repo' '
+ # one commit will have bitmaps, the other will not
+ test_commit one &&
+ test_commit much-larger-blob-one &&
+ git repack -adb &&
+ test_commit two &&
+ test_commit much-larger-blob-two &&
+ git tag tag
+'
+
+test_expect_success 'filters fallback to non-bitmap traversal' '
+ # use a path-based filter, since they are inherently incompatible with
+ # bitmaps (i.e., this test will never get confused by later code to
+ # combine the features)
+ filter=$(echo "!one" | git hash-object -w --stdin) &&
+ git rev-list --objects --filter=sparse:oid=$filter HEAD >expect &&
+ git rev-list --use-bitmap-index \
+ --objects --filter=sparse:oid=$filter HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'blob:none filter' '
+ git rev-list --objects --filter=blob:none HEAD >expect &&
+ git rev-list --use-bitmap-index \
+ --objects --filter=blob:none HEAD >actual &&
+ test_bitmap_traversal expect actual
+'
+
+test_expect_success 'blob:none filter with specified blob' '
+ git rev-list --objects --filter=blob:none HEAD HEAD:two.t >expect &&
+ git rev-list --use-bitmap-index \
+ --objects --filter=blob:none HEAD HEAD:two.t >actual &&
+ test_bitmap_traversal expect actual
+'
+
+test_expect_success 'blob:limit filter' '
+ git rev-list --objects --filter=blob:limit=5 HEAD >expect &&
+ git rev-list --use-bitmap-index \
+ --objects --filter=blob:limit=5 HEAD >actual &&
+ test_bitmap_traversal expect actual
+'
+
+test_expect_success 'blob:limit filter with specified blob' '
+ git rev-list --objects --filter=blob:limit=5 \
+ HEAD HEAD:much-larger-blob-two.t >expect &&
+ git rev-list --use-bitmap-index \
+ --objects --filter=blob:limit=5 \
+ HEAD HEAD:much-larger-blob-two.t >actual &&
+ test_bitmap_traversal expect actual
+'
+
+test_expect_success 'tree:0 filter' '
+ git rev-list --objects --filter=tree:0 HEAD >expect &&
+ git rev-list --use-bitmap-index \
+ --objects --filter=tree:0 HEAD >actual &&
+ test_bitmap_traversal expect actual
+'
+
+test_expect_success 'tree:0 filter with specified blob, tree' '
+ git rev-list --objects --filter=tree:0 HEAD HEAD:two.t >expect &&
+ git rev-list --use-bitmap-index \
+ --objects --filter=tree:0 HEAD HEAD:two.t >actual &&
+ test_bitmap_traversal expect actual
+'
+
+test_expect_success 'tree:1 filter' '
+ git rev-list --objects --filter=tree:1 HEAD >expect &&
+ git rev-list --use-bitmap-index \
+ --objects --filter=tree:1 HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'object:type filter' '
+ git rev-list --objects --filter=object:type=tag tag >expect &&
+ git rev-list --use-bitmap-index \
+ --objects --filter=object:type=tag tag >actual &&
+ test_cmp expect actual &&
+
+ git rev-list --objects --filter=object:type=commit tag >expect &&
+ git rev-list --use-bitmap-index \
+ --objects --filter=object:type=commit tag >actual &&
+ test_bitmap_traversal expect actual &&
+
+ git rev-list --objects --filter=object:type=tree tag >expect &&
+ git rev-list --use-bitmap-index \
+ --objects --filter=object:type=tree tag >actual &&
+ test_bitmap_traversal expect actual &&
+
+ git rev-list --objects --filter=object:type=blob tag >expect &&
+ git rev-list --use-bitmap-index \
+ --objects --filter=object:type=blob tag >actual &&
+ test_bitmap_traversal expect actual
+'
+
+test_expect_success 'object:type filter with --filter-provided-objects' '
+ git rev-list --objects --filter-provided-objects --filter=object:type=tag tag >expect &&
+ git rev-list --use-bitmap-index \
+ --objects --filter-provided-objects --filter=object:type=tag tag >actual &&
+ test_cmp expect actual &&
+
+ git rev-list --objects --filter-provided-objects --filter=object:type=commit tag >expect &&
+ git rev-list --use-bitmap-index \
+ --objects --filter-provided-objects --filter=object:type=commit tag >actual &&
+ test_bitmap_traversal expect actual &&
+
+ git rev-list --objects --filter-provided-objects --filter=object:type=tree tag >expect &&
+ git rev-list --use-bitmap-index \
+ --objects --filter-provided-objects --filter=object:type=tree tag >actual &&
+ test_bitmap_traversal expect actual &&
+
+ git rev-list --objects --filter-provided-objects --filter=object:type=blob tag >expect &&
+ git rev-list --use-bitmap-index \
+ --objects --filter-provided-objects --filter=object:type=blob tag >actual &&
+ test_bitmap_traversal expect actual
+'
+
+test_expect_success 'combine filter' '
+ git rev-list --objects --filter=blob:limit=1000 --filter=object:type=blob tag >expect &&
+ git rev-list --use-bitmap-index \
+ --objects --filter=blob:limit=1000 --filter=object:type=blob tag >actual &&
+ test_bitmap_traversal expect actual
+'
+
+test_expect_success 'combine filter with --filter-provided-objects' '
+ git rev-list --objects --filter-provided-objects --filter=blob:limit=1000 --filter=object:type=blob tag >expect &&
+ git rev-list --use-bitmap-index \
+ --objects --filter-provided-objects --filter=blob:limit=1000 --filter=object:type=blob tag >actual &&
+ test_bitmap_traversal expect actual &&
+
+ git cat-file --batch-check="%(objecttype) %(objectsize)" <actual >objects &&
+ while read objecttype objectsize
+ do
+ test "$objecttype" = blob || return 1
+ test "$objectsize" -le 1000 || return 1
+ done <objects
+'
+
+test_expect_success 'bitmap traversal with --unpacked' '
+ git repack -adb &&
+ test_commit unpacked &&
+
+ git rev-list --objects --no-object-names unpacked^.. >expect.raw &&
+ sort expect.raw >expect &&
+
+ git rev-list --use-bitmap-index --objects --all --unpacked >actual.raw &&
+ sort actual.raw >actual &&
+
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t6114-keep-packs.sh b/t/t6114-keep-packs.sh
new file mode 100755
index 0000000..44246f8
--- /dev/null
+++ b/t/t6114-keep-packs.sh
@@ -0,0 +1,71 @@
+#!/bin/sh
+
+test_description='rev-list with .keep packs'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ test_commit loose &&
+ test_commit packed &&
+ test_commit kept &&
+
+ KEPT_PACK=$(git pack-objects --revs .git/objects/pack/pack <<-EOF
+ refs/tags/kept
+ ^refs/tags/packed
+ EOF
+ ) &&
+ MISC_PACK=$(git pack-objects --revs .git/objects/pack/pack <<-EOF
+ refs/tags/packed
+ ^refs/tags/loose
+ EOF
+ ) &&
+
+ touch .git/objects/pack/pack-$KEPT_PACK.keep
+'
+
+rev_list_objects () {
+ git rev-list "$@" >out &&
+ sort out
+}
+
+idx_objects () {
+ git show-index <$1 >expect-idx &&
+ cut -d" " -f2 <expect-idx | sort
+}
+
+test_expect_success '--no-kept-objects excludes trees and blobs in .keep packs' '
+ rev_list_objects --objects --all --no-object-names >kept &&
+ rev_list_objects --objects --all --no-object-names --no-kept-objects >no-kept &&
+
+ idx_objects .git/objects/pack/pack-$KEPT_PACK.idx >expect &&
+ comm -3 kept no-kept >actual &&
+
+ test_cmp expect actual
+'
+
+test_expect_success '--no-kept-objects excludes kept non-MIDX object' '
+ test_config core.multiPackIndex true &&
+
+ # Create a pack with just the commit object in pack, and do not mark it
+ # as kept (even though it appears in $KEPT_PACK, which does have a .keep
+ # file).
+ MIDX_PACK=$(git pack-objects .git/objects/pack/pack <<-EOF
+ $(git rev-parse kept)
+ EOF
+ ) &&
+
+ # Write a MIDX containing all packs, but use the version of the commit
+ # at "kept" in a non-kept pack by touching $MIDX_PACK.
+ touch .git/objects/pack/pack-$MIDX_PACK.pack &&
+ git multi-pack-index write &&
+
+ rev_list_objects --objects --no-object-names --no-kept-objects HEAD >actual &&
+ (
+ idx_objects .git/objects/pack/pack-$MISC_PACK.idx &&
+ git rev-list --objects --no-object-names refs/tags/loose
+ ) | sort >expect &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t6115-rev-list-du.sh b/t/t6115-rev-list-du.sh
new file mode 100755
index 0000000..c0cfda6
--- /dev/null
+++ b/t/t6115-rev-list-du.sh
@@ -0,0 +1,80 @@
+#!/bin/sh
+
+test_description='basic tests of rev-list --disk-usage'
+. ./test-lib.sh
+
+# we want a mix of reachable and unreachable, as well as
+# objects in the bitmapped pack and some outside of it
+test_expect_success 'set up repository' '
+ test_commit --no-tag one &&
+ test_commit --no-tag two &&
+ git repack -adb &&
+ git reset --hard HEAD^ &&
+ test_commit --no-tag three &&
+ test_commit --no-tag four &&
+ git reset --hard HEAD^
+'
+
+# We don't want to hardcode sizes, because they depend on the exact details of
+# packing, zlib, etc. We'll assume that the regular rev-list and cat-file
+# machinery works and compare the --disk-usage output to that.
+disk_usage_slow () {
+ git rev-list --no-object-names "$@" |
+ git cat-file --batch-check="%(objectsize:disk)" |
+ perl -lne '$total += $_; END { print $total}'
+}
+
+# check behavior with given rev-list options; note that
+# whitespace is not preserved in args
+check_du () {
+ args=$*
+
+ test_expect_success "generate expected size ($args)" "
+ disk_usage_slow $args >expect
+ "
+
+ test_expect_success "rev-list --disk-usage without bitmaps ($args)" "
+ git rev-list --disk-usage $args >actual &&
+ test_cmp expect actual
+ "
+
+ test_expect_success "rev-list --disk-usage with bitmaps ($args)" "
+ git rev-list --disk-usage --use-bitmap-index $args >actual &&
+ test_cmp expect actual
+ "
+}
+
+check_du HEAD
+check_du --objects HEAD
+check_du --objects HEAD^..HEAD
+
+test_expect_success 'setup for --unpacked tests' '
+ git repack -adb &&
+ test_commit unpacked
+'
+
+check_du --all --objects --unpacked
+
+# As mentioned above, don't use hardcode sizes as actual size, but use the
+# output from git cat-file.
+test_expect_success 'rev-list --disk-usage=human' '
+ git rev-list --objects HEAD --disk-usage=human >actual &&
+ disk_usage_slow --objects HEAD >actual_size &&
+ grep "$(cat actual_size) bytes" actual
+'
+
+test_expect_success 'rev-list --disk-usage=human with bitmaps' '
+ git rev-list --objects HEAD --use-bitmap-index --disk-usage=human >actual &&
+ disk_usage_slow --objects HEAD >actual_size &&
+ grep "$(cat actual_size) bytes" actual
+'
+
+test_expect_success 'rev-list use --disk-usage unproperly' '
+ test_must_fail git rev-list --objects HEAD --disk-usage=typo 2>err &&
+ cat >expect <<-\EOF &&
+ fatal: invalid value for '\''--disk-usage=<format>'\'': '\''typo'\'', the only allowed format is '\''human'\''
+ EOF
+ test_cmp err expect
+'
+
+test_done
diff --git a/t/t6120-describe.sh b/t/t6120-describe.sh
new file mode 100755
index 0000000..e78315d
--- /dev/null
+++ b/t/t6120-describe.sh
@@ -0,0 +1,674 @@
+#!/bin/sh
+
+test_description='test describe'
+
+# o---o-----o----o----o-------o----x
+# \ D,R e /
+# \---o-------------o-'
+# \ B /
+# `-o----o----o-'
+# A c
+#
+# First parent of a merge commit is on the same line, second parent below.
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+check_describe () {
+ indir= &&
+ while test $# != 0
+ do
+ case "$1" in
+ -C)
+ indir="$2"
+ shift
+ ;;
+ *)
+ break
+ ;;
+ esac
+ shift
+ done &&
+ indir=${indir:+"$indir"/} &&
+ expect="$1"
+ shift
+ describe_opts="$@"
+ test_expect_success "describe $describe_opts" '
+ git ${indir:+ -C "$indir"} describe $describe_opts >raw &&
+ sed -e "s/-g[0-9a-f]*\$/-gHASH/" <raw >actual &&
+ echo "$expect" >expect &&
+ test_cmp expect actual
+ '
+}
+
+test_expect_success setup '
+ test_commit initial file one &&
+ test_commit second file two &&
+ test_commit third file three &&
+ test_commit --annotate A file A &&
+ test_commit c file c &&
+
+ git reset --hard second &&
+ test_commit --annotate B side B &&
+
+ test_tick &&
+ git merge -m Merged c &&
+ merged=$(git rev-parse HEAD) &&
+
+ git reset --hard second &&
+ test_commit --no-tag D another D &&
+
+ test_tick &&
+ git tag -a -m R R &&
+
+ test_commit e another DD &&
+ test_commit --no-tag "yet another" another DDD &&
+
+ test_tick &&
+ git merge -m Merged $merged &&
+
+ test_commit --no-tag x file
+'
+
+check_describe A-8-gHASH HEAD
+check_describe A-7-gHASH HEAD^
+check_describe R-2-gHASH HEAD^^
+check_describe A-3-gHASH HEAD^^2
+check_describe B HEAD^^2^
+check_describe R-1-gHASH HEAD^^^
+
+check_describe c-7-gHASH --tags HEAD
+check_describe c-6-gHASH --tags HEAD^
+check_describe e-1-gHASH --tags HEAD^^
+check_describe c-2-gHASH --tags HEAD^^2
+check_describe B --tags HEAD^^2^
+check_describe e --tags HEAD^^^
+check_describe e --tags --exact-match HEAD^^^
+
+check_describe heads/main --all HEAD
+check_describe tags/c-6-gHASH --all HEAD^
+check_describe tags/e --all HEAD^^^
+
+check_describe B-0-gHASH --long HEAD^^2^
+check_describe A-3-gHASH --long HEAD^^2
+
+check_describe c-7-gHASH --tags
+check_describe e-3-gHASH --first-parent --tags
+
+check_describe c-7-gHASH --tags --no-exact-match HEAD
+check_describe e-3-gHASH --first-parent --tags --no-exact-match HEAD
+
+test_expect_success '--exact-match failure' '
+ test_must_fail git describe --exact-match HEAD 2>err
+'
+
+test_expect_success 'describe --contains defaults to HEAD without commit-ish' '
+ echo "A^0" >expect &&
+ git checkout A &&
+ test_when_finished "git checkout -" &&
+ git describe --contains >actual &&
+ test_cmp expect actual
+'
+
+check_describe tags/A --all A^0
+
+test_expect_success 'renaming tag A to Q locally produces a warning' "
+ git update-ref refs/tags/Q $(git rev-parse refs/tags/A) &&
+ git update-ref -d refs/tags/A &&
+ git describe HEAD 2>err >out &&
+ cat >expected <<-\EOF &&
+ warning: tag 'Q' is externally known as 'A'
+ EOF
+ test_cmp expected err &&
+ grep -E '^A-8-g[0-9a-f]+$' out
+"
+
+test_expect_success 'misnamed annotated tag forces long output' '
+ description=$(git describe --no-long Q^0) &&
+ expr "$description" : "A-0-g[0-9a-f]*$" &&
+ git rev-parse --verify "$description" >actual &&
+ git rev-parse --verify Q^0 >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'abbrev=0 will not break misplaced tag (1)' '
+ description=$(git describe --abbrev=0 Q^0) &&
+ expr "$description" : "A-0-g[0-9a-f]*$"
+'
+
+test_expect_success 'abbrev=0 will not break misplaced tag (2)' '
+ description=$(git describe --abbrev=0 c^0) &&
+ expr "$description" : "A-1-g[0-9a-f]*$"
+'
+
+test_expect_success 'rename tag Q back to A' '
+ git update-ref refs/tags/A $(git rev-parse refs/tags/Q) &&
+ git update-ref -d refs/tags/Q
+'
+
+test_expect_success 'pack tag refs' 'git pack-refs'
+check_describe A-8-gHASH HEAD
+
+test_expect_success 'describe works from outside repo using --git-dir' '
+ git clone --bare "$TRASH_DIRECTORY" "$TRASH_DIRECTORY/bare" &&
+ git --git-dir "$TRASH_DIRECTORY/bare" describe >out &&
+ grep -E "^A-8-g[0-9a-f]+$" out
+'
+
+check_describe "A-8-gHASH" --dirty
+
+test_expect_success 'describe --dirty with --work-tree' '
+ (
+ cd "$TEST_DIRECTORY" &&
+ git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --dirty >"$TRASH_DIRECTORY/out"
+ ) &&
+ grep -E "^A-8-g[0-9a-f]+$" out
+'
+
+test_expect_success 'set-up dirty work tree' '
+ echo >>file
+'
+
+test_expect_success 'describe --dirty with --work-tree (dirty)' '
+ git describe --dirty >expected &&
+ (
+ cd "$TEST_DIRECTORY" &&
+ git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --dirty >"$TRASH_DIRECTORY/out"
+ ) &&
+ grep -E "^A-8-g[0-9a-f]+-dirty$" out &&
+ test_cmp expected out
+'
+
+test_expect_success 'describe --dirty=.mod with --work-tree (dirty)' '
+ git describe --dirty=.mod >expected &&
+ (
+ cd "$TEST_DIRECTORY" &&
+ git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --dirty=.mod >"$TRASH_DIRECTORY/out"
+ ) &&
+ grep -E "^A-8-g[0-9a-f]+.mod$" out &&
+ test_cmp expected out
+'
+
+test_expect_success 'describe --dirty HEAD' '
+ test_must_fail git describe --dirty HEAD
+'
+
+test_expect_success 'set-up matching pattern tests' '
+ git tag -a -m test-annotated test-annotated &&
+ echo >>file &&
+ test_tick &&
+ git commit -a -m "one more" &&
+ git tag test1-lightweight &&
+ echo >>file &&
+ test_tick &&
+ git commit -a -m "yet another" &&
+ git tag test2-lightweight &&
+ echo >>file &&
+ test_tick &&
+ git commit -a -m "even more"
+
+'
+
+check_describe "test-annotated-3-gHASH" --match="test-*"
+
+check_describe "test1-lightweight-2-gHASH" --tags --match="test1-*"
+
+check_describe "test2-lightweight-1-gHASH" --tags --match="test2-*"
+
+check_describe "test2-lightweight-0-gHASH" --long --tags --match="test2-*" HEAD^
+
+check_describe "test2-lightweight-0-gHASH" --long --tags --match="test1-*" --match="test2-*" HEAD^
+
+check_describe "test2-lightweight-0-gHASH" --long --tags --match="test1-*" --no-match --match="test2-*" HEAD^
+
+check_describe "test1-lightweight-2-gHASH" --long --tags --match="test1-*" --match="test3-*" HEAD
+
+check_describe "test1-lightweight-2-gHASH" --long --tags --match="test3-*" --match="test1-*" HEAD
+
+test_expect_success 'set-up branches' '
+ git branch branch_A A &&
+ git branch branch_C c &&
+ git update-ref refs/remotes/origin/remote_branch_A "A^{commit}" &&
+ git update-ref refs/remotes/origin/remote_branch_C "c^{commit}" &&
+ git update-ref refs/original/original_branch_A test-annotated~2
+'
+
+check_describe "heads/branch_A-11-gHASH" --all --match="branch_*" --exclude="branch_C" HEAD
+
+check_describe "remotes/origin/remote_branch_A-11-gHASH" --all --match="origin/remote_branch_*" --exclude="origin/remote_branch_C" HEAD
+
+check_describe "original/original_branch_A-6-gHASH" --all test-annotated~1
+
+test_expect_success '--match does not work for other types' '
+ test_must_fail git describe --all --match="*original_branch_*" test-annotated~1
+'
+
+test_expect_success '--exclude does not work for other types' '
+ R=$(git describe --all --exclude="any_pattern_even_not_matching" test-annotated~1) &&
+ case "$R" in
+ *original_branch_A*) echo "fail: Found unknown reference $R with --exclude"
+ false;;
+ *) echo ok: Found some known type;;
+ esac
+'
+
+test_expect_success 'name-rev with exact tags' '
+ echo A >expect &&
+ tag_object=$(git rev-parse refs/tags/A) &&
+ git name-rev --tags --name-only $tag_object >actual &&
+ test_cmp expect actual &&
+
+ echo "A^0" >expect &&
+ tagged_commit=$(git rev-parse "refs/tags/A^0") &&
+ git name-rev --tags --name-only $tagged_commit >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'name-rev --all' '
+ >expect.unsorted &&
+ for rev in $(git rev-list --all)
+ do
+ git name-rev $rev >>expect.unsorted || return 1
+ done &&
+ sort <expect.unsorted >expect &&
+ git name-rev --all >actual.unsorted &&
+ sort <actual.unsorted >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'name-rev --annotate-stdin' '
+ >expect.unsorted &&
+ for rev in $(git rev-list --all)
+ do
+ name=$(git name-rev --name-only $rev) &&
+ echo "$rev ($name)" >>expect.unsorted || return 1
+ done &&
+ sort <expect.unsorted >expect &&
+ git rev-list --all | git name-rev --annotate-stdin >actual.unsorted &&
+ sort <actual.unsorted >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'name-rev --stdin deprecated' "
+ git rev-list --all | git name-rev --stdin 2>actual &&
+ grep -E 'warning: --stdin is deprecated' actual
+"
+
+test_expect_success 'describe --contains with the exact tags' '
+ echo "A^0" >expect &&
+ tag_object=$(git rev-parse refs/tags/A) &&
+ git describe --contains $tag_object >actual &&
+ test_cmp expect actual &&
+
+ echo "A^0" >expect &&
+ tagged_commit=$(git rev-parse "refs/tags/A^0") &&
+ git describe --contains $tagged_commit >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'describe --contains and --match' '
+ echo "A^0" >expect &&
+ tagged_commit=$(git rev-parse "refs/tags/A^0") &&
+ test_must_fail git describe --contains --match="B" $tagged_commit &&
+ git describe --contains --match="B" --match="A" $tagged_commit >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'describe --exclude' '
+ echo "c~1" >expect &&
+ tagged_commit=$(git rev-parse "refs/tags/A^0") &&
+ test_must_fail git describe --contains --match="B" $tagged_commit &&
+ git describe --contains --match="?" --exclude="A" $tagged_commit >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'describe --contains and --no-match' '
+ echo "A^0" >expect &&
+ tagged_commit=$(git rev-parse "refs/tags/A^0") &&
+ git describe --contains --match="B" --no-match $tagged_commit >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'setup and absorb a submodule' '
+ test_create_repo sub1 &&
+ test_commit -C sub1 initial &&
+ git submodule add ./sub1 &&
+ git submodule absorbgitdirs &&
+ git commit -a -m "add submodule" &&
+ git describe --dirty >expect &&
+ git describe --broken >out &&
+ test_cmp expect out
+'
+
+test_expect_success 'describe chokes on severely broken submodules' '
+ mv .git/modules/sub1/ .git/modules/sub_moved &&
+ test_must_fail git describe --dirty
+'
+
+test_expect_success 'describe ignoring a broken submodule' '
+ git describe --broken >out &&
+ grep broken out
+'
+
+test_expect_success 'describe with --work-tree ignoring a broken submodule' '
+ (
+ cd "$TEST_DIRECTORY" &&
+ git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --broken >"$TRASH_DIRECTORY/out"
+ ) &&
+ test_when_finished "mv .git/modules/sub_moved .git/modules/sub1" &&
+ grep broken out
+'
+
+test_expect_success 'describe a blob at a directly tagged commit' '
+ echo "make it a unique blob" >file &&
+ git add file && git commit -m "content in file" &&
+ git tag -a -m "latest annotated tag" unique-file &&
+ git describe HEAD:file >actual &&
+ echo "unique-file:file" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'describe a blob with its first introduction' '
+ git commit --allow-empty -m "empty commit" &&
+ git rm file &&
+ git commit -m "delete blob" &&
+ git revert HEAD &&
+ git commit --allow-empty -m "empty commit" &&
+ git describe HEAD:file >actual &&
+ echo "unique-file:file" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'describe directly tagged blob' '
+ git tag test-blob unique-file:file &&
+ git describe test-blob >actual &&
+ echo "unique-file:file" >expect &&
+ # suboptimal: we rather want to see "test-blob"
+ test_cmp expect actual
+'
+
+test_expect_success 'describe tag object' '
+ git tag test-blob-1 -a -m msg unique-file:file &&
+ test_must_fail git describe test-blob-1 2>actual &&
+ test_grep "fatal: test-blob-1 is neither a commit nor blob" actual
+'
+
+test_expect_success ULIMIT_STACK_SIZE 'name-rev works in a deep repo' '
+ i=1 &&
+ while test $i -lt 8000
+ do
+ echo "commit refs/heads/main
+committer A U Thor <author@example.com> $((1000000000 + $i * 100)) +0200
+data <<EOF
+commit #$i
+EOF" &&
+ if test $i = 1
+ then
+ echo "from refs/heads/main^0"
+ fi &&
+ i=$(($i + 1)) || return 1
+ done | git fast-import &&
+ git checkout main &&
+ git tag far-far-away HEAD^ &&
+ echo "HEAD~4000 tags/far-far-away~3999" >expect &&
+ git name-rev HEAD~4000 >actual &&
+ test_cmp expect actual &&
+ run_with_limited_stack git name-rev HEAD~4000 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success ULIMIT_STACK_SIZE 'describe works in a deep repo' '
+ git tag -f far-far-away HEAD~7999 &&
+ echo "far-far-away" >expect &&
+ git describe --tags --abbrev=0 HEAD~4000 >actual &&
+ test_cmp expect actual &&
+ run_with_limited_stack git describe --tags --abbrev=0 HEAD~4000 >actual &&
+ test_cmp expect actual
+'
+
+check_describe tags/A --all A
+check_describe tags/c --all c
+check_describe heads/branch_A --all --match='branch_*' branch_A
+
+test_expect_success 'describe complains about tree object' '
+ test_must_fail git describe HEAD^{tree}
+'
+
+test_expect_success 'describe complains about missing object' '
+ test_must_fail git describe $ZERO_OID
+'
+
+test_expect_success 'name-rev a rev shortly after epoch' '
+ test_when_finished "git checkout main" &&
+
+ git checkout --orphan no-timestamp-underflow &&
+ # Any date closer to epoch than the CUTOFF_DATE_SLOP constant
+ # in builtin/name-rev.c.
+ GIT_COMMITTER_DATE="@1234 +0000" \
+ git commit -m "committer date shortly after epoch" &&
+ old_commit_oid=$(git rev-parse HEAD) &&
+
+ echo "$old_commit_oid no-timestamp-underflow" >expect &&
+ git name-rev $old_commit_oid >actual &&
+ test_cmp expect actual
+'
+
+# A--------------main
+# \ /
+# \----------M2
+# \ /
+# \---M1-C
+# \ /
+# B
+test_expect_success 'name-rev covers all conditions while looking at parents' '
+ git init repo &&
+ (
+ cd repo &&
+
+ echo A >file &&
+ git add file &&
+ git commit -m A &&
+ A=$(git rev-parse HEAD) &&
+
+ git checkout --detach &&
+ echo B >file &&
+ git commit -m B file &&
+ B=$(git rev-parse HEAD) &&
+
+ git checkout $A &&
+ git merge --no-ff $B && # M1
+
+ echo C >file &&
+ git commit -m C file &&
+
+ git checkout $A &&
+ git merge --no-ff HEAD@{1} && # M2
+
+ git checkout main &&
+ git merge --no-ff HEAD@{1} &&
+
+ echo "$B main^2^2~1^2" >expect &&
+ git name-rev $B >actual &&
+
+ test_cmp expect actual
+ )
+'
+
+# A-B-C-D-E-main
+#
+# Where C has a non-monotonically increasing commit timestamp w.r.t. other
+# commits
+test_expect_success 'non-monotonic commit dates setup' '
+ UNIX_EPOCH_ZERO="@0 +0000" &&
+ git init non-monotonic &&
+ test_commit -C non-monotonic A &&
+ test_commit -C non-monotonic --no-tag B &&
+ test_commit -C non-monotonic --no-tag --date "$UNIX_EPOCH_ZERO" C &&
+ test_commit -C non-monotonic D &&
+ test_commit -C non-monotonic E
+'
+
+test_expect_success 'name-rev with commitGraph handles non-monotonic timestamps' '
+ test_config -C non-monotonic core.commitGraph true &&
+ (
+ cd non-monotonic &&
+
+ git commit-graph write --reachable &&
+
+ echo "main~3 tags/D~2" >expect &&
+ git name-rev --tags main~3 >actual &&
+
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'name-rev --all works with non-monotonic timestamps' '
+ test_config -C non-monotonic core.commitGraph false &&
+ (
+ cd non-monotonic &&
+
+ rm -rf .git/info/commit-graph* &&
+
+ cat >tags <<-\EOF &&
+ tags/E
+ tags/D
+ tags/D~1
+ tags/D~2
+ tags/A
+ EOF
+
+ git log --pretty=%H >revs &&
+
+ paste -d" " revs tags | sort >expect &&
+
+ git name-rev --tags --all | sort >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'name-rev --annotate-stdin works with non-monotonic timestamps' '
+ test_config -C non-monotonic core.commitGraph false &&
+ (
+ cd non-monotonic &&
+
+ rm -rf .git/info/commit-graph* &&
+
+ cat >expect <<-\EOF &&
+ E
+ D
+ D~1
+ D~2
+ A
+ EOF
+
+ git log --pretty=%H >revs &&
+ git name-rev --tags --annotate-stdin --name-only <revs >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'name-rev --all works with commitGraph' '
+ test_config -C non-monotonic core.commitGraph true &&
+ (
+ cd non-monotonic &&
+
+ git commit-graph write --reachable &&
+
+ cat >tags <<-\EOF &&
+ tags/E
+ tags/D
+ tags/D~1
+ tags/D~2
+ tags/A
+ EOF
+
+ git log --pretty=%H >revs &&
+
+ paste -d" " revs tags | sort >expect &&
+
+ git name-rev --tags --all | sort >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'name-rev --annotate-stdin works with commitGraph' '
+ test_config -C non-monotonic core.commitGraph true &&
+ (
+ cd non-monotonic &&
+
+ git commit-graph write --reachable &&
+
+ cat >expect <<-\EOF &&
+ E
+ D
+ D~1
+ D~2
+ A
+ EOF
+
+ git log --pretty=%H >revs &&
+ git name-rev --tags --annotate-stdin --name-only <revs >actual &&
+ test_cmp expect actual
+ )
+'
+
+# B
+# o
+# \
+# o-----o---o----x
+# A
+#
+test_expect_success 'setup: describe commits with disjoint bases' '
+ git init disjoint1 &&
+ (
+ cd disjoint1 &&
+
+ echo o >> file && git add file && git commit -m o &&
+ echo A >> file && git add file && git commit -m A &&
+ git tag A -a -m A &&
+ echo o >> file && git add file && git commit -m o &&
+
+ git checkout --orphan branch && rm file &&
+ echo B > file2 && git add file2 && git commit -m B &&
+ git tag B -a -m B &&
+ git merge --no-ff --allow-unrelated-histories main -m x
+ )
+'
+
+check_describe -C disjoint1 "A-3-gHASH" HEAD
+
+# B
+# o---o---o------------.
+# \
+# o---o---x
+# A
+#
+test_expect_success 'setup: describe commits with disjoint bases 2' '
+ git init disjoint2 &&
+ (
+ cd disjoint2 &&
+
+ echo A >> file && git add file && GIT_COMMITTER_DATE="2020-01-01 18:00" git commit -m A &&
+ git tag A -a -m A &&
+ echo o >> file && git add file && GIT_COMMITTER_DATE="2020-01-01 18:01" git commit -m o &&
+
+ git checkout --orphan branch &&
+ echo o >> file2 && git add file2 && GIT_COMMITTER_DATE="2020-01-01 15:00" git commit -m o &&
+ echo o >> file2 && git add file2 && GIT_COMMITTER_DATE="2020-01-01 15:01" git commit -m o &&
+ echo B >> file2 && git add file2 && GIT_COMMITTER_DATE="2020-01-01 15:02" git commit -m B &&
+ git tag B -a -m B &&
+ git merge --no-ff --allow-unrelated-histories main -m x
+ )
+'
+
+check_describe -C disjoint2 "B-3-gHASH" HEAD
+
+test_expect_success 'setup misleading taggerdates' '
+ GIT_COMMITTER_DATE="2006-12-12 12:31" git tag -a -m "another tag" newer-tag-older-commit unique-file~1
+'
+
+check_describe newer-tag-older-commit~1 --contains unique-file~2
+
+test_done
diff --git a/t/t6130-pathspec-noglob.sh b/t/t6130-pathspec-noglob.sh
new file mode 100755
index 0000000..ba7902c
--- /dev/null
+++ b/t/t6130-pathspec-noglob.sh
@@ -0,0 +1,160 @@
+#!/bin/sh
+
+test_description='test globbing (and noglob) of pathspec limiting'
+. ./test-lib.sh
+
+test_expect_success 'create commits with glob characters' '
+ test_commit unrelated bar &&
+ test_commit vanilla foo &&
+ # insert file "f*" in the commit, but in a way that avoids
+ # the name "f*" in the worktree, because it is not allowed
+ # on Windows (the tests below do not depend on the presence
+ # of the file in the worktree)
+ git config core.protectNTFS false &&
+ git update-index --add --cacheinfo 100644 "$(git rev-parse HEAD:foo)" "f*" &&
+ test_tick &&
+ git commit -m star &&
+ test_commit bracket "f[o][o]"
+'
+
+test_expect_success 'vanilla pathspec matches literally' '
+ echo vanilla >expect &&
+ git log --format=%s -- foo >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'star pathspec globs' '
+ cat >expect <<-\EOF &&
+ bracket
+ star
+ vanilla
+ EOF
+ git log --format=%s -- "f*" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'star pathspec globs' '
+ cat >expect <<-\EOF &&
+ bracket
+ star
+ vanilla
+ EOF
+ git log --format=%s -- ":(glob)f*" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'bracket pathspec globs and matches literal brackets' '
+ cat >expect <<-\EOF &&
+ bracket
+ vanilla
+ EOF
+ git log --format=%s -- "f[o][o]" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'bracket pathspec globs and matches literal brackets' '
+ cat >expect <<-\EOF &&
+ bracket
+ vanilla
+ EOF
+ git log --format=%s -- ":(glob)f[o][o]" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'no-glob option matches literally (vanilla)' '
+ echo vanilla >expect &&
+ git --literal-pathspecs log --format=%s -- foo >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'no-glob option matches literally (vanilla)' '
+ echo vanilla >expect &&
+ git log --format=%s -- ":(literal)foo" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'no-glob option matches literally (star)' '
+ echo star >expect &&
+ git --literal-pathspecs log --format=%s -- "f*" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'no-glob option matches literally (star)' '
+ echo star >expect &&
+ git log --format=%s -- ":(literal)f*" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'no-glob option matches literally (bracket)' '
+ echo bracket >expect &&
+ git --literal-pathspecs log --format=%s -- "f[o][o]" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'no-glob option matches literally (bracket)' '
+ echo bracket >expect &&
+ git log --format=%s -- ":(literal)f[o][o]" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'no-glob option disables :(literal)' '
+ git --literal-pathspecs log --format=%s -- ":(literal)foo" >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'no-glob environment variable works' '
+ echo star >expect &&
+ GIT_LITERAL_PATHSPECS=1 git log --format=%s -- "f*" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'blame takes global pathspec flags' '
+ git --literal-pathspecs blame -- foo &&
+ git --icase-pathspecs blame -- foo &&
+ git --glob-pathspecs blame -- foo &&
+ git --noglob-pathspecs blame -- foo
+'
+
+test_expect_success 'setup xxx/bar' '
+ mkdir xxx &&
+ test_commit xxx xxx/bar
+'
+
+test_expect_success '**/ works with :(glob)' '
+ cat >expect <<-\EOF &&
+ xxx
+ unrelated
+ EOF
+ git log --format=%s -- ":(glob)**/bar" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '**/ does not work with --noglob-pathspecs' '
+ git --noglob-pathspecs log --format=%s -- "**/bar" >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success '**/ works with :(glob) and --noglob-pathspecs' '
+ cat >expect <<-\EOF &&
+ xxx
+ unrelated
+ EOF
+ git --noglob-pathspecs log --format=%s -- ":(glob)**/bar" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '**/ works with --glob-pathspecs' '
+ cat >expect <<-\EOF &&
+ xxx
+ unrelated
+ EOF
+ git --glob-pathspecs log --format=%s -- "**/bar" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '**/ does not work with :(literal) and --glob-pathspecs' '
+ git --glob-pathspecs log --format=%s -- ":(literal)**/bar" >actual &&
+ test_must_be_empty actual
+'
+
+test_done
diff --git a/t/t6131-pathspec-icase.sh b/t/t6131-pathspec-icase.sh
new file mode 100755
index 0000000..770cce0
--- /dev/null
+++ b/t/t6131-pathspec-icase.sh
@@ -0,0 +1,111 @@
+#!/bin/sh
+
+test_description='test case insensitive pathspec limiting'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+if test_have_prereq CASE_INSENSITIVE_FS
+then
+ skip_all='skipping case sensitive tests - case insensitive file system'
+ test_done
+fi
+
+test_expect_success 'create commits with glob characters' '
+ test_commit bar bar &&
+ test_commit bAr bAr &&
+ test_commit BAR BAR &&
+ mkdir foo &&
+ test_commit foo/bar foo/bar &&
+ test_commit foo/bAr foo/bAr &&
+ test_commit foo/BAR foo/BAR &&
+ mkdir fOo &&
+ test_commit fOo/bar fOo/bar &&
+ test_commit fOo/bAr fOo/bAr &&
+ test_commit fOo/BAR fOo/BAR &&
+ mkdir FOO &&
+ test_commit FOO/bar FOO/bar &&
+ test_commit FOO/bAr FOO/bAr &&
+ test_commit FOO/BAR FOO/BAR
+'
+
+test_expect_success 'tree_entry_interesting matches bar' '
+ echo bar >expect &&
+ git log --format=%s -- "bar" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'tree_entry_interesting matches :(icase)bar' '
+ cat <<-EOF >expect &&
+ BAR
+ bAr
+ bar
+ EOF
+ git log --format=%s -- ":(icase)bar" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'tree_entry_interesting matches :(icase)bar with prefix' '
+ cat <<-EOF >expect &&
+ fOo/BAR
+ fOo/bAr
+ fOo/bar
+ EOF
+ ( cd fOo && git log --format=%s -- ":(icase)bar" ) >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'tree_entry_interesting matches :(icase)bar with empty prefix' '
+ cat <<-EOF >expect &&
+ FOO/BAR
+ FOO/bAr
+ FOO/bar
+ fOo/BAR
+ fOo/bAr
+ fOo/bar
+ foo/BAR
+ foo/bAr
+ foo/bar
+ EOF
+ ( cd fOo && git log --format=%s -- ":(icase)../foo/bar" ) >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'match_pathspec matches :(icase)bar' '
+ cat <<-EOF >expect &&
+ BAR
+ bAr
+ bar
+ EOF
+ git ls-files ":(icase)bar" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'match_pathspec matches :(icase)bar with prefix' '
+ cat <<-EOF >expect &&
+ fOo/BAR
+ fOo/bAr
+ fOo/bar
+ EOF
+ ( cd fOo && git ls-files --full-name ":(icase)bar" ) >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'match_pathspec matches :(icase)bar with empty prefix' '
+ cat <<-EOF >expect &&
+ bar
+ fOo/BAR
+ fOo/bAr
+ fOo/bar
+ EOF
+ ( cd fOo && git ls-files --full-name ":(icase)bar" ../bar ) >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '"git diff" can take magic :(icase) pathspec' '
+ echo FOO/BAR >expect &&
+ git diff --name-only HEAD^ HEAD -- ":(icase)foo/bar" >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t6132-pathspec-exclude.sh b/t/t6132-pathspec-exclude.sh
new file mode 100755
index 0000000..9fdafeb
--- /dev/null
+++ b/t/t6132-pathspec-exclude.sh
@@ -0,0 +1,428 @@
+#!/bin/sh
+
+test_description='test case exclude pathspec'
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ for p in file sub/file sub/sub/file sub/file2 sub/sub/sub/file sub2/file; do
+ if echo $p | grep /; then
+ mkdir -p $(dirname $p)
+ fi &&
+ : >$p &&
+ git add $p &&
+ git commit -m $p || return 1
+ done &&
+ git log --oneline --format=%s >actual &&
+ cat <<EOF >expect &&
+sub2/file
+sub/sub/sub/file
+sub/file2
+sub/sub/file
+sub/file
+file
+EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'exclude only pathspec uses default implicit pathspec' '
+ git log --oneline --format=%s -- . ":(exclude)sub" >expect &&
+ git log --oneline --format=%s -- ":(exclude)sub" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 't_e_i() exclude sub' '
+ git log --oneline --format=%s -- . ":(exclude)sub" >actual &&
+ cat <<EOF >expect &&
+sub2/file
+file
+EOF
+ test_cmp expect actual
+'
+
+test_expect_success 't_e_i() exclude sub/sub/file' '
+ git log --oneline --format=%s -- . ":(exclude)sub/sub/file" >actual &&
+ cat <<EOF >expect &&
+sub2/file
+sub/sub/sub/file
+sub/file2
+sub/file
+file
+EOF
+ test_cmp expect actual
+'
+
+test_expect_success 't_e_i() exclude sub using mnemonic' '
+ git log --oneline --format=%s -- . ":!sub" >actual &&
+ cat <<EOF >expect &&
+sub2/file
+file
+EOF
+ test_cmp expect actual
+'
+
+test_expect_success 't_e_i() exclude :(icase)SUB' '
+ git log --oneline --format=%s -- . ":(exclude,icase)SUB" >actual &&
+ cat <<EOF >expect &&
+sub2/file
+file
+EOF
+ test_cmp expect actual
+'
+
+test_expect_success 't_e_i() exclude sub2 from sub' '
+ (
+ cd sub &&
+ git log --oneline --format=%s -- :/ ":/!sub2" >actual &&
+ cat <<EOF >expect &&
+sub/sub/sub/file
+sub/file2
+sub/sub/file
+sub/file
+file
+EOF
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 't_e_i() exclude sub/*file' '
+ git log --oneline --format=%s -- . ":(exclude)sub/*file" >actual &&
+ cat <<EOF >expect &&
+sub2/file
+sub/file2
+file
+EOF
+ test_cmp expect actual
+'
+
+test_expect_success 't_e_i() exclude :(glob)sub/*/file' '
+ git log --oneline --format=%s -- . ":(exclude,glob)sub/*/file" >actual &&
+ cat <<EOF >expect &&
+sub2/file
+sub/sub/sub/file
+sub/file2
+sub/file
+file
+EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'm_p_d() exclude sub' '
+ git ls-files -- . ":(exclude)sub" >actual &&
+ cat <<EOF >expect &&
+file
+sub2/file
+EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'm_p_d() exclude sub/sub/file' '
+ git ls-files -- . ":(exclude)sub/sub/file" >actual &&
+ cat <<EOF >expect &&
+file
+sub/file
+sub/file2
+sub/sub/sub/file
+sub2/file
+EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'm_p_d() exclude sub using mnemonic' '
+ git ls-files -- . ":!sub" >actual &&
+ cat <<EOF >expect &&
+file
+sub2/file
+EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'm_p_d() exclude :(icase)SUB' '
+ git ls-files -- . ":(exclude,icase)SUB" >actual &&
+ cat <<EOF >expect &&
+file
+sub2/file
+EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'm_p_d() exclude sub2 from sub' '
+ (
+ cd sub &&
+ git ls-files -- :/ ":/!sub2" >actual &&
+ cat <<EOF >expect &&
+../file
+file
+file2
+sub/file
+sub/sub/file
+EOF
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'm_p_d() exclude sub/*file' '
+ git ls-files -- . ":(exclude)sub/*file" >actual &&
+ cat <<EOF >expect &&
+file
+sub/file2
+sub2/file
+EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'm_p_d() exclude :(glob)sub/*/file' '
+ git ls-files -- . ":(exclude,glob)sub/*/file" >actual &&
+ cat <<EOF >expect &&
+file
+sub/file
+sub/file2
+sub/sub/sub/file
+sub2/file
+EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'multiple exclusions' '
+ git ls-files -- ":^*/file2" ":^sub2" >actual &&
+ cat <<-\EOF >expect &&
+ file
+ sub/file
+ sub/sub/file
+ sub/sub/sub/file
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 't_e_i() exclude case #8' '
+ test_when_finished "rm -fr case8" &&
+ git init case8 &&
+ (
+ cd case8 &&
+ echo file >file1 &&
+ echo file >file2 &&
+ git add file1 file2 &&
+ git commit -m twofiles &&
+ git grep -l file HEAD :^file2 >actual &&
+ echo HEAD:file1 >expected &&
+ test_cmp expected actual &&
+ git grep -l file HEAD :^file1 >actual &&
+ echo HEAD:file2 >expected &&
+ test_cmp expected actual
+ )
+'
+
+test_expect_success 'grep --untracked PATTERN' '
+ # This test is not an actual test of exclude patterns, rather it
+ # is here solely to ensure that if any tests are inserted, deleted, or
+ # changed above, that we still have untracked files with the expected
+ # contents for the NEXT two tests.
+ cat <<-\EOF >expect-grep &&
+ actual
+ expect
+ sub/actual
+ sub/expect
+ EOF
+ git grep -l --untracked file -- >actual-grep &&
+ test_cmp expect-grep actual-grep
+'
+
+test_expect_success 'grep --untracked PATTERN :(exclude)DIR' '
+ cat <<-\EOF >expect-grep &&
+ actual
+ expect
+ EOF
+ git grep -l --untracked file -- ":(exclude)sub" >actual-grep &&
+ test_cmp expect-grep actual-grep
+'
+
+test_expect_success 'grep --untracked PATTERN :(exclude)*FILE' '
+ cat <<-\EOF >expect-grep &&
+ actual
+ sub/actual
+ EOF
+ git grep -l --untracked file -- ":(exclude)*expect" >actual-grep &&
+ test_cmp expect-grep actual-grep
+'
+
+# Depending on the command, all negative pathspec needs to subtract
+# either from the full tree, or from the current directory.
+#
+# The sample tree checked out at this point has:
+# file
+# sub/file
+# sub/file2
+# sub/sub/file
+# sub/sub/sub/file
+# sub2/file
+#
+# but there may also be some cruft that interferes with "git clean"
+# and "git add" tests.
+
+test_expect_success 'archive with all negative' '
+ git reset --hard &&
+ git clean -f &&
+ git -C sub archive --format=tar HEAD -- ":!sub/" >archive &&
+ "$TAR" tf archive >actual &&
+ cat >expect <<-\EOF &&
+ file
+ file2
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'add with all negative' '
+ H=$(git rev-parse HEAD) &&
+ git reset --hard $H &&
+ git clean -f &&
+ test_when_finished "git reset --hard $H" &&
+ for path in file sub/file sub/sub/file sub2/file
+ do
+ echo smudge >>"$path" || return 1
+ done &&
+ git -C sub add -- ":!sub/" &&
+ git diff --name-only --no-renames --cached >actual &&
+ cat >expect <<-\EOF &&
+ file
+ sub/file
+ sub2/file
+ EOF
+ test_cmp expect actual &&
+ git diff --name-only --no-renames >actual &&
+ echo sub/sub/file >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'add -p with all negative' '
+ H=$(git rev-parse HEAD) &&
+ git reset --hard $H &&
+ git clean -f &&
+ test_when_finished "git reset --hard $H" &&
+ for path in file sub/file sub/sub/file sub2/file
+ do
+ echo smudge >>"$path" || return 1
+ done &&
+ yes | git -C sub add -p -- ":!sub/" &&
+ git diff --name-only --no-renames --cached >actual &&
+ cat >expect <<-\EOF &&
+ file
+ sub/file
+ sub2/file
+ EOF
+ test_cmp expect actual &&
+ git diff --name-only --no-renames >actual &&
+ echo sub/sub/file >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'clean with all negative' '
+ H=$(git rev-parse HEAD) &&
+ git reset --hard $H &&
+ test_when_finished "git reset --hard $H && git clean -f" &&
+ git clean -f &&
+ for path in file9 sub/file9 sub/sub/file9 sub2/file9
+ do
+ echo cruft >"$path" || return 1
+ done &&
+ git -C sub clean -f -- ":!sub" &&
+ test_path_is_file file9 &&
+ test_path_is_missing sub/file9 &&
+ test_path_is_file sub/sub/file9 &&
+ test_path_is_file sub2/file9
+'
+
+test_expect_success 'commit with all negative' '
+ H=$(git rev-parse HEAD) &&
+ git reset --hard $H &&
+ test_when_finished "git reset --hard $H" &&
+ for path in file sub/file sub/sub/file sub2/file
+ do
+ echo smudge >>"$path" || return 1
+ done &&
+ git -C sub commit -m sample -- ":!sub/" &&
+ git diff --name-only --no-renames HEAD^ HEAD >actual &&
+ cat >expect <<-\EOF &&
+ file
+ sub/file
+ sub2/file
+ EOF
+ test_cmp expect actual &&
+ git diff --name-only --no-renames HEAD >actual &&
+ echo sub/sub/file >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'reset with all negative' '
+ H=$(git rev-parse HEAD) &&
+ git reset --hard $H &&
+ test_when_finished "git reset --hard $H" &&
+ for path in file sub/file sub/sub/file sub2/file
+ do
+ echo smudge >>"$path" &&
+ git add "$path" || return 1
+ done &&
+ git -C sub reset --quiet -- ":!sub/" &&
+ git diff --name-only --no-renames --cached >actual &&
+ echo sub/sub/file >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'grep with all negative' '
+ H=$(git rev-parse HEAD) &&
+ git reset --hard $H &&
+ test_when_finished "git reset --hard $H" &&
+ for path in file sub/file sub/sub/file sub2/file
+ do
+ echo "needle $path" >>"$path" || return 1
+ done &&
+ git -C sub grep -h needle -- ":!sub/" >actual &&
+ cat >expect <<-\EOF &&
+ needle sub/file
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'ls-files with all negative' '
+ git reset --hard &&
+ git -C sub ls-files -- ":!sub/" >actual &&
+ cat >expect <<-\EOF &&
+ file
+ file2
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'rm with all negative' '
+ git reset --hard &&
+ test_when_finished "git reset --hard" &&
+ git -C sub rm -r --cached -- ":!sub/" >actual &&
+ git diff --name-only --no-renames --diff-filter=D --cached >actual &&
+ cat >expect <<-\EOF &&
+ sub/file
+ sub/file2
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'stash with all negative' '
+ H=$(git rev-parse HEAD) &&
+ git reset --hard $H &&
+ test_when_finished "git reset --hard $H" &&
+ for path in file sub/file sub/sub/file sub2/file
+ do
+ echo smudge >>"$path" || return 1
+ done &&
+ git -C sub stash push -m sample -- ":!sub/" &&
+ git diff --name-only --no-renames HEAD >actual &&
+ echo sub/sub/file >expect &&
+ test_cmp expect actual &&
+ git stash show --name-only >actual &&
+ cat >expect <<-\EOF &&
+ file
+ sub/file
+ sub2/file
+ EOF
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t6133-pathspec-rev-dwim.sh b/t/t6133-pathspec-rev-dwim.sh
new file mode 100755
index 0000000..a290ffc
--- /dev/null
+++ b/t/t6133-pathspec-rev-dwim.sh
@@ -0,0 +1,48 @@
+#!/bin/sh
+
+test_description='test dwim of revs versus pathspecs in revision parser'
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ test_commit base &&
+ echo content >"br[ack]ets" &&
+ git add . &&
+ test_tick &&
+ git commit -m brackets
+'
+
+test_expect_success 'non-rev wildcard dwims to pathspec' '
+ git log -- "*.t" >expect &&
+ git log "*.t" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'tree:path with metacharacters dwims to rev' '
+ git show "HEAD:br[ack]ets" -- >expect &&
+ git show "HEAD:br[ack]ets" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '^{foo} with metacharacters dwims to rev' '
+ git log "HEAD^{/b.*}" -- >expect &&
+ git log "HEAD^{/b.*}" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '@{foo} with metacharacters dwims to rev' '
+ git log "HEAD@{now [or thereabouts]}" -- >expect &&
+ git log "HEAD@{now [or thereabouts]}" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success ':/*.t from a subdir dwims to a pathspec' '
+ mkdir subdir &&
+ (
+ cd subdir &&
+ git log -- ":/*.t" >expect &&
+ git log ":/*.t" >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_done
diff --git a/t/t6134-pathspec-in-submodule.sh b/t/t6134-pathspec-in-submodule.sh
new file mode 100755
index 0000000..16ce4cf
--- /dev/null
+++ b/t/t6134-pathspec-in-submodule.sh
@@ -0,0 +1,33 @@
+#!/bin/sh
+
+test_description='test case exclude pathspec'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup a submodule' '
+ test_create_repo pretzel &&
+ : >pretzel/a &&
+ git -C pretzel add a &&
+ git -C pretzel commit -m "add a file" -- a &&
+ git -c protocol.file.allow=always submodule add ./pretzel sub &&
+ git commit -a -m "add submodule" &&
+ git submodule deinit --all
+'
+
+cat <<EOF >expect
+fatal: Pathspec 'sub/a' is in submodule 'sub'
+EOF
+
+test_expect_success 'error message for path inside submodule' '
+ echo a >sub/a &&
+ test_must_fail git add sub/a 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'error message for path inside submodule from within submodule' '
+ test_must_fail git -C sub add . 2>actual &&
+ test_grep "in unpopulated submodule" actual
+'
+
+test_done
diff --git a/t/t6135-pathspec-with-attrs.sh b/t/t6135-pathspec-with-attrs.sh
new file mode 100755
index 0000000..a9c1e4e
--- /dev/null
+++ b/t/t6135-pathspec-with-attrs.sh
@@ -0,0 +1,298 @@
+#!/bin/sh
+
+test_description='test labels in pathspecs'
+. ./test-lib.sh
+
+test_expect_success 'setup a tree' '
+ cat <<-\EOF >expect &&
+ fileA
+ fileAB
+ fileAC
+ fileB
+ fileBC
+ fileC
+ fileNoLabel
+ fileSetLabel
+ fileUnsetLabel
+ fileValue
+ fileWrongLabel
+ sub/fileA
+ sub/fileAB
+ sub/fileAC
+ sub/fileB
+ sub/fileBC
+ sub/fileC
+ sub/fileNoLabel
+ sub/fileSetLabel
+ sub/fileUnsetLabel
+ sub/fileValue
+ sub/fileWrongLabel
+ EOF
+ mkdir sub &&
+ while read path
+ do
+ echo content >$path &&
+ git add $path || return 1
+ done <expect &&
+ git commit -m "initial commit" &&
+ git ls-files >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'pathspec with no attr' '
+ test_must_fail git ls-files ":(attr:)"
+'
+
+test_expect_success 'pathspec with labels and non existent .gitattributes' '
+ git ls-files ":(attr:label)" >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'pathspec with labels and non existent .gitattributes (2)' '
+ test_must_fail git grep content HEAD -- ":(attr:label)"
+'
+
+test_expect_success 'setup .gitattributes' '
+ cat <<-\EOF >.gitattributes &&
+ fileA labelA
+ fileB labelB
+ fileC labelC
+ fileAB labelA labelB
+ fileAC labelA labelC
+ fileBC labelB labelC
+ fileUnsetLabel -label
+ fileSetLabel label
+ fileValue label=foo
+ fileWrongLabel label☺
+ EOF
+ echo fileSetLabel label1 >sub/.gitattributes &&
+ git add .gitattributes sub/.gitattributes &&
+ git commit -m "add attributes"
+'
+
+test_expect_success 'check specific set attr' '
+ cat <<-\EOF >expect &&
+ fileSetLabel
+ sub/fileSetLabel
+ EOF
+ git ls-files ":(attr:label)" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'check set attr with pathspec pattern' '
+ echo sub/fileSetLabel >expect &&
+
+ git ls-files ":(attr:label)sub" >actual &&
+ test_cmp expect actual &&
+
+ git ls-files ":(attr:label)sub/" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'check specific set attr in tree-ish' '
+ cat <<-\EOF >expect &&
+ HEAD:fileSetLabel
+ HEAD:sub/fileSetLabel
+ EOF
+ git grep -l content HEAD ":(attr:label)" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'check specific set attr with pathspec pattern in tree-ish' '
+ echo HEAD:sub/fileSetLabel >expect &&
+
+ git grep -l content HEAD ":(attr:label)sub" >actual &&
+ test_cmp expect actual &&
+
+ git grep -l content HEAD ":(attr:label)sub/" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'check specific unset attr' '
+ cat <<-\EOF >expect &&
+ fileUnsetLabel
+ sub/fileUnsetLabel
+ EOF
+ git ls-files ":(attr:-label)" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'check specific unset attr (2)' '
+ cat <<-\EOF >expect &&
+ HEAD:fileUnsetLabel
+ HEAD:sub/fileUnsetLabel
+ EOF
+ git grep -l content HEAD ":(attr:-label)" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'check specific value attr' '
+ cat <<-\EOF >expect &&
+ fileValue
+ sub/fileValue
+ EOF
+ git ls-files ":(attr:label=foo)" >actual &&
+ test_cmp expect actual &&
+ git ls-files ":(attr:label=bar)" >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'check specific value attr (2)' '
+ cat <<-\EOF >expect &&
+ HEAD:fileValue
+ HEAD:sub/fileValue
+ EOF
+ git grep -l content HEAD ":(attr:label=foo)" >actual &&
+ test_cmp expect actual &&
+ test_must_fail git grep -l content HEAD ":(attr:label=bar)"
+'
+
+test_expect_success 'check unspecified attr' '
+ cat <<-\EOF >expect &&
+ .gitattributes
+ fileA
+ fileAB
+ fileAC
+ fileB
+ fileBC
+ fileC
+ fileNoLabel
+ fileWrongLabel
+ sub/.gitattributes
+ sub/fileA
+ sub/fileAB
+ sub/fileAC
+ sub/fileB
+ sub/fileBC
+ sub/fileC
+ sub/fileNoLabel
+ sub/fileWrongLabel
+ EOF
+ git ls-files ":(attr:!label)" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'check unspecified attr (2)' '
+ cat <<-\EOF >expect &&
+ HEAD:.gitattributes
+ HEAD:fileA
+ HEAD:fileAB
+ HEAD:fileAC
+ HEAD:fileB
+ HEAD:fileBC
+ HEAD:fileC
+ HEAD:fileNoLabel
+ HEAD:fileWrongLabel
+ HEAD:sub/.gitattributes
+ HEAD:sub/fileA
+ HEAD:sub/fileAB
+ HEAD:sub/fileAC
+ HEAD:sub/fileB
+ HEAD:sub/fileBC
+ HEAD:sub/fileC
+ HEAD:sub/fileNoLabel
+ HEAD:sub/fileWrongLabel
+ EOF
+ git grep -l ^ HEAD ":(attr:!label)" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'check multiple unspecified attr' '
+ cat <<-\EOF >expect &&
+ .gitattributes
+ fileC
+ fileNoLabel
+ fileWrongLabel
+ sub/.gitattributes
+ sub/fileC
+ sub/fileNoLabel
+ sub/fileWrongLabel
+ EOF
+ git ls-files ":(attr:!labelB !labelA !label)" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'check label with more labels but excluded path' '
+ cat <<-\EOF >expect &&
+ fileAB
+ fileB
+ fileBC
+ EOF
+ git ls-files ":(attr:labelB)" ":(exclude)sub/" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'check label excluding other labels' '
+ cat <<-\EOF >expect &&
+ fileAB
+ fileB
+ fileBC
+ sub/fileAB
+ sub/fileB
+ EOF
+ git ls-files ":(attr:labelB)" ":(exclude,attr:labelC)sub/" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'fail on multiple attr specifiers in one pathspec item' '
+ test_must_fail git ls-files . ":(attr:labelB,attr:labelC)" 2>actual &&
+ test_grep "Only one" actual
+'
+
+test_expect_success 'fail if attr magic is used places not implemented' '
+ # The main purpose of this test is to check that we actually fail
+ # when you attempt to use attr magic in commands that do not implement
+ # attr magic. This test does not advocate git-add to stay that way,
+ # though, but git-add is convenient as it has its own internal pathspec
+ # parsing.
+ test_must_fail git add ":(attr:labelB)" 2>actual &&
+ test_grep "magic not supported" actual
+'
+
+test_expect_success 'abort on giving invalid label on the command line' '
+ test_must_fail git ls-files . ":(attr:☺)"
+'
+
+test_expect_success 'abort on asking for wrong magic' '
+ test_must_fail git ls-files . ":(attr:-label=foo)" &&
+ test_must_fail git ls-files . ":(attr:!label=foo)"
+'
+
+test_expect_success 'check attribute list' '
+ cat <<-EOF >>.gitattributes &&
+ * whitespace=indent,trail,space
+ EOF
+ git ls-files ":(attr:whitespace=indent\,trail\,space)" >actual &&
+ git ls-files >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'backslash cannot be the last character' '
+ test_must_fail git ls-files ":(attr:label=foo\\ labelA=bar)" 2>actual &&
+ test_grep "not allowed as last character in attr value" actual
+'
+
+test_expect_success 'backslash cannot be used as a value' '
+ test_must_fail git ls-files ":(attr:label=f\\\oo)" 2>actual &&
+ test_grep "for value matching" actual
+'
+
+test_expect_success 'reading from .gitattributes in a subdirectory (1)' '
+ git ls-files ":(attr:label1)" >actual &&
+ test_write_lines "sub/fileSetLabel" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'reading from .gitattributes in a subdirectory (2)' '
+ git ls-files ":(attr:label1)sub" >actual &&
+ test_write_lines "sub/fileSetLabel" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'reading from .gitattributes in a subdirectory (3)' '
+ git ls-files ":(attr:label1)sub/" >actual &&
+ test_write_lines "sub/fileSetLabel" >expect &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t6136-pathspec-in-bare.sh b/t/t6136-pathspec-in-bare.sh
new file mode 100755
index 0000000..2db37a6
--- /dev/null
+++ b/t/t6136-pathspec-in-bare.sh
@@ -0,0 +1,39 @@
+#!/bin/sh
+
+test_description='diagnosing out-of-scope pathspec'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup a bare and non-bare repository' '
+ test_commit file1 &&
+ git clone --bare . bare
+'
+
+test_expect_success 'log and ls-files in a bare repository' '
+ (
+ cd bare &&
+ test_must_fail git log -- .. >out 2>err &&
+ test_must_be_empty out &&
+ test_grep "outside repository" err &&
+
+ test_must_fail git ls-files -- .. >out 2>err &&
+ test_must_be_empty out &&
+ test_grep "outside repository" err
+ )
+'
+
+test_expect_success 'log and ls-files in .git directory' '
+ (
+ cd .git &&
+ test_must_fail git log -- .. >out 2>err &&
+ test_must_be_empty out &&
+ test_grep "outside repository" err &&
+
+ test_must_fail git ls-files -- .. >out 2>err &&
+ test_must_be_empty out &&
+ test_grep "outside repository" err
+ )
+'
+
+test_done
diff --git a/t/t6200-fmt-merge-msg.sh b/t/t6200-fmt-merge-msg.sh
new file mode 100755
index 0000000..5a221f8
--- /dev/null
+++ b/t/t6200-fmt-merge-msg.sh
@@ -0,0 +1,704 @@
+#!/bin/sh
+#
+# Copyright (c) 2006, Junio C Hamano
+#
+
+test_description='fmt-merge-msg test'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY/lib-gpg.sh"
+
+test_expect_success setup '
+ echo one >one &&
+ git add one &&
+ test_tick &&
+ git commit -m "Initial" &&
+
+ git clone . remote &&
+
+ echo uno >one &&
+ echo dos >two &&
+ git add two &&
+ test_tick &&
+ git commit -a -m "Second" &&
+
+ git checkout -b left &&
+
+ echo "c1" >one &&
+ test_tick &&
+ git commit -a -m "Common #1" &&
+
+ echo "c2" >one &&
+ test_tick &&
+ git commit -a -m "Common #2" &&
+
+ git branch right &&
+
+ echo "l3" >two &&
+ test_tick &&
+ GIT_COMMITTER_NAME="Another Committer" \
+ GIT_AUTHOR_NAME="Another Author" git commit -a -m "Left #3" &&
+
+ echo "l4" >two &&
+ test_tick &&
+ GIT_COMMITTER_NAME="Another Committer" \
+ GIT_AUTHOR_NAME="Another Author" git commit -a -m "Left #4" &&
+
+ echo "l5" >two &&
+ test_tick &&
+ GIT_COMMITTER_NAME="Another Committer" \
+ GIT_AUTHOR_NAME="Another Author" git commit -a -m "Left #5" &&
+ git tag tag-l5 &&
+
+ git checkout right &&
+
+ echo "r3" >three &&
+ git add three &&
+ test_tick &&
+ git commit -a -m "Right #3" &&
+ git tag tag-r3 &&
+
+ echo "r4" >three &&
+ test_tick &&
+ git commit -a -m "Right #4" &&
+
+ echo "r5" >three &&
+ test_tick &&
+ git commit -a -m "Right #5" &&
+
+ git checkout -b long &&
+ test_commit_bulk --start=0 --message=%s --filename=one 30 &&
+
+ git show-branch &&
+
+ apos="'\''"
+'
+
+test_expect_success GPG 'set up a signed tag' '
+ git tag -s -m signed-tag-msg signed-good-tag left
+'
+
+test_expect_success GPGSSH 'created ssh signed commit and tag' '
+ test_config gpg.format ssh &&
+ git checkout -b signed-ssh &&
+ touch file &&
+ git add file &&
+ git commit -m "ssh signed" -S"${GPGSSH_KEY_PRIMARY}" &&
+ git tag -s -u"${GPGSSH_KEY_PRIMARY}" -m signed-ssh-tag-msg signed-good-ssh-tag left &&
+ git tag -s -u"${GPGSSH_KEY_UNTRUSTED}" -m signed-ssh-tag-msg-untrusted signed-untrusted-ssh-tag left
+'
+
+test_expect_success GPGSSH,GPGSSH_VERIFYTIME 'create signed tags with keys having defined lifetimes' '
+ test_when_finished "test_unconfig commit.gpgsign" &&
+ test_config gpg.format ssh &&
+ git checkout -b signed-expiry-ssh &&
+ touch file &&
+ git add file &&
+
+ echo expired >file && test_tick && git commit -a -m expired -S"${GPGSSH_KEY_EXPIRED}" &&
+ git tag -s -u "${GPGSSH_KEY_EXPIRED}" -m expired-signed expired-signed &&
+
+ echo notyetvalid >file && test_tick && git commit -a -m notyetvalid -S"${GPGSSH_KEY_NOTYETVALID}" &&
+ git tag -s -u "${GPGSSH_KEY_NOTYETVALID}" -m notyetvalid-signed notyetvalid-signed &&
+
+ echo timeboxedvalid >file && test_tick && git commit -a -m timeboxedvalid -S"${GPGSSH_KEY_TIMEBOXEDVALID}" &&
+ git tag -s -u "${GPGSSH_KEY_TIMEBOXEDVALID}" -m timeboxedvalid-signed timeboxedvalid-signed &&
+
+ echo timeboxedinvalid >file && test_tick && git commit -a -m timeboxedinvalid -S"${GPGSSH_KEY_TIMEBOXEDINVALID}" &&
+ git tag -s -u "${GPGSSH_KEY_TIMEBOXEDINVALID}" -m timeboxedinvalid-signed timeboxedinvalid-signed
+'
+
+test_expect_success 'message for merging local branch' '
+ echo "Merge branch ${apos}left${apos}" >expected &&
+
+ git checkout main &&
+ git fetch . left &&
+
+ git fmt-merge-msg <.git/FETCH_HEAD >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success GPG 'message for merging local tag signed by good key' '
+ git checkout main &&
+ git fetch . signed-good-tag &&
+ git fmt-merge-msg <.git/FETCH_HEAD >actual &&
+ grep "^Merge tag ${apos}signed-good-tag${apos}" actual &&
+ grep "^signed-tag-msg" actual &&
+ grep "^# gpg: Signature made" actual &&
+ grep "^# gpg: Good signature from" actual
+'
+
+test_expect_success GPG 'message for merging local tag signed by unknown key' '
+ git checkout main &&
+ git fetch . signed-good-tag &&
+ GNUPGHOME=. git fmt-merge-msg <.git/FETCH_HEAD >actual &&
+ grep "^Merge tag ${apos}signed-good-tag${apos}" actual &&
+ grep "^signed-tag-msg" actual &&
+ grep "^# gpg: Signature made" actual &&
+ grep -E "^# gpg: Can${apos}t check signature: (public key not found|No public key)" actual
+'
+
+test_expect_success GPGSSH 'message for merging local tag signed by good ssh key' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ git checkout main &&
+ git fetch . signed-good-ssh-tag &&
+ git fmt-merge-msg <.git/FETCH_HEAD >actual &&
+ grep "^Merge tag ${apos}signed-good-ssh-tag${apos}" actual &&
+ grep "^signed-ssh-tag-msg" actual &&
+ grep "${GPGSSH_GOOD_SIGNATURE_TRUSTED}" actual &&
+ ! grep "${GPGSSH_BAD_SIGNATURE}" actual
+'
+
+test_expect_success GPGSSH 'message for merging local tag signed by unknown ssh key' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ git checkout main &&
+ git fetch . signed-untrusted-ssh-tag &&
+ git fmt-merge-msg <.git/FETCH_HEAD >actual &&
+ grep "^Merge tag ${apos}signed-untrusted-ssh-tag${apos}" actual &&
+ grep "^signed-ssh-tag-msg-untrusted" actual &&
+ grep "${GPGSSH_GOOD_SIGNATURE_UNTRUSTED}" actual &&
+ ! grep "${GPGSSH_BAD_SIGNATURE}" actual &&
+ grep "${GPGSSH_KEY_NOT_TRUSTED}" actual
+'
+
+test_expect_success GPGSSH,GPGSSH_VERIFYTIME 'message for merging local tag signed by expired ssh key' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ git checkout main &&
+ git fetch . expired-signed &&
+ git fmt-merge-msg <.git/FETCH_HEAD >actual &&
+ grep "^Merge tag ${apos}expired-signed${apos}" actual &&
+ grep "^expired-signed" actual &&
+ ! grep "${GPGSSH_GOOD_SIGNATURE_TRUSTED}" actual
+'
+
+test_expect_success GPGSSH,GPGSSH_VERIFYTIME 'message for merging local tag signed by not yet valid ssh key' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ git checkout main &&
+ git fetch . notyetvalid-signed &&
+ git fmt-merge-msg <.git/FETCH_HEAD >actual &&
+ grep "^Merge tag ${apos}notyetvalid-signed${apos}" actual &&
+ grep "^notyetvalid-signed" actual &&
+ ! grep "${GPGSSH_GOOD_SIGNATURE_TRUSTED}" actual
+'
+
+test_expect_success GPGSSH,GPGSSH_VERIFYTIME 'message for merging local tag signed by valid timeboxed ssh key' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ git checkout main &&
+ git fetch . timeboxedvalid-signed &&
+ git fmt-merge-msg <.git/FETCH_HEAD >actual &&
+ grep "^Merge tag ${apos}timeboxedvalid-signed${apos}" actual &&
+ grep "^timeboxedvalid-signed" actual &&
+ grep "${GPGSSH_GOOD_SIGNATURE_TRUSTED}" actual &&
+ ! grep "${GPGSSH_BAD_SIGNATURE}" actual
+'
+
+test_expect_success GPGSSH,GPGSSH_VERIFYTIME 'message for merging local tag signed by invalid timeboxed ssh key' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ git checkout main &&
+ git fetch . timeboxedinvalid-signed &&
+ git fmt-merge-msg <.git/FETCH_HEAD >actual &&
+ grep "^Merge tag ${apos}timeboxedinvalid-signed${apos}" actual &&
+ grep "^timeboxedinvalid-signed" actual &&
+ ! grep "${GPGSSH_GOOD_SIGNATURE_TRUSTED}" actual
+'
+
+test_expect_success 'message for merging external branch' '
+ echo "Merge branch ${apos}left${apos} of $(pwd)" >expected &&
+
+ git checkout main &&
+ git fetch "$(pwd)" left &&
+
+ git fmt-merge-msg <.git/FETCH_HEAD >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success '[merge] summary/log configuration' '
+ cat >expected <<-EOF &&
+ Merge branch ${apos}left${apos}
+
+ # By Another Author (3) and A U Thor (2)
+ # Via Another Committer
+ * left:
+ Left #5
+ Left #4
+ Left #3
+ Common #2
+ Common #1
+ EOF
+
+ test_config merge.log true &&
+ test_unconfig merge.summary &&
+
+ git checkout main &&
+ test_tick &&
+ git fetch . left &&
+
+ git fmt-merge-msg <.git/FETCH_HEAD >actual1 &&
+
+ test_unconfig merge.log &&
+ test_config merge.summary true &&
+
+ git checkout main &&
+ test_tick &&
+ git fetch . left &&
+
+ git fmt-merge-msg <.git/FETCH_HEAD >actual2 &&
+
+ test_cmp expected actual1 &&
+ test_cmp expected actual2
+'
+
+test_expect_success 'setup FETCH_HEAD' '
+ git checkout main &&
+ test_tick &&
+ git fetch . left
+'
+
+test_expect_success 'merge.log=3 limits shortlog length' '
+ cat >expected <<-EOF &&
+ Merge branch ${apos}left${apos}
+
+ # By Another Author (3) and A U Thor (2)
+ # Via Another Committer
+ * left: (5 commits)
+ Left #5
+ Left #4
+ Left #3
+ ...
+ EOF
+
+ git -c merge.log=3 fmt-merge-msg <.git/FETCH_HEAD >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'merge.log=5 shows all 5 commits' '
+ cat >expected <<-EOF &&
+ Merge branch ${apos}left${apos}
+
+ # By Another Author (3) and A U Thor (2)
+ # Via Another Committer
+ * left:
+ Left #5
+ Left #4
+ Left #3
+ Common #2
+ Common #1
+ EOF
+
+ git -c merge.log=5 fmt-merge-msg <.git/FETCH_HEAD >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success '--log=5 with custom comment character' '
+ cat >expected <<-EOF &&
+ Merge branch ${apos}left${apos}
+
+ x By Another Author (3) and A U Thor (2)
+ x Via Another Committer
+ * left:
+ Left #5
+ Left #4
+ Left #3
+ Common #2
+ Common #1
+ EOF
+
+ git -c core.commentchar="x" fmt-merge-msg --log=5 <.git/FETCH_HEAD >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'merge.log=0 disables shortlog' '
+ echo "Merge branch ${apos}left${apos}" >expected &&
+ git -c merge.log=0 fmt-merge-msg <.git/FETCH_HEAD >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success '--log=3 limits shortlog length' '
+ cat >expected <<-EOF &&
+ Merge branch ${apos}left${apos}
+
+ # By Another Author (3) and A U Thor (2)
+ # Via Another Committer
+ * left: (5 commits)
+ Left #5
+ Left #4
+ Left #3
+ ...
+ EOF
+
+ git fmt-merge-msg --log=3 <.git/FETCH_HEAD >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success '--log=5 shows all 5 commits' '
+ cat >expected <<-EOF &&
+ Merge branch ${apos}left${apos}
+
+ # By Another Author (3) and A U Thor (2)
+ # Via Another Committer
+ * left:
+ Left #5
+ Left #4
+ Left #3
+ Common #2
+ Common #1
+ EOF
+
+ git fmt-merge-msg --log=5 <.git/FETCH_HEAD >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success '--no-log disables shortlog' '
+ echo "Merge branch ${apos}left${apos}" >expected &&
+ git fmt-merge-msg --no-log <.git/FETCH_HEAD >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success '--log=0 disables shortlog' '
+ echo "Merge branch ${apos}left${apos}" >expected &&
+ git fmt-merge-msg --no-log <.git/FETCH_HEAD >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'fmt-merge-msg -m' '
+ echo "Sync with left" >expected &&
+ cat >expected.log <<-EOF &&
+ Sync with left
+
+ # By Another Author (3) and A U Thor (2)
+ # Via Another Committer
+ * ${apos}left${apos} of $(pwd):
+ Left #5
+ Left #4
+ Left #3
+ Common #2
+ Common #1
+ EOF
+
+ test_unconfig merge.log &&
+ test_unconfig merge.summary &&
+ git checkout main &&
+ git fetch "$(pwd)" left &&
+ git fmt-merge-msg -m "Sync with left" <.git/FETCH_HEAD >actual &&
+ git fmt-merge-msg --log -m "Sync with left" \
+ <.git/FETCH_HEAD >actual.log &&
+ test_config merge.log true &&
+ git fmt-merge-msg -m "Sync with left" \
+ <.git/FETCH_HEAD >actual.log-config &&
+ git fmt-merge-msg --no-log -m "Sync with left" \
+ <.git/FETCH_HEAD >actual.nolog &&
+
+ test_cmp expected actual &&
+ test_cmp expected.log actual.log &&
+ test_cmp expected.log actual.log-config &&
+ test_cmp expected actual.nolog
+'
+
+test_expect_success 'setup: expected shortlog for two branches' '
+ cat >expected <<-EOF
+ Merge branches ${apos}left${apos} and ${apos}right${apos}
+
+ # By Another Author (3) and A U Thor (2)
+ # Via Another Committer
+ * left:
+ Left #5
+ Left #4
+ Left #3
+ Common #2
+ Common #1
+
+ * right:
+ Right #5
+ Right #4
+ Right #3
+ Common #2
+ Common #1
+ EOF
+'
+
+test_expect_success 'shortlog for two branches' '
+ test_config merge.log true &&
+ test_unconfig merge.summary &&
+ git checkout main &&
+ test_tick &&
+ git fetch . left right &&
+ git fmt-merge-msg <.git/FETCH_HEAD >actual1 &&
+
+ test_unconfig merge.log &&
+ test_config merge.summary true &&
+ git checkout main &&
+ test_tick &&
+ git fetch . left right &&
+ git fmt-merge-msg <.git/FETCH_HEAD >actual2 &&
+
+ test_config merge.log yes &&
+ test_unconfig merge.summary &&
+ git checkout main &&
+ test_tick &&
+ git fetch . left right &&
+ git fmt-merge-msg <.git/FETCH_HEAD >actual3 &&
+
+ test_unconfig merge.log &&
+ test_config merge.summary yes &&
+ git checkout main &&
+ test_tick &&
+ git fetch . left right &&
+ git fmt-merge-msg <.git/FETCH_HEAD >actual4 &&
+
+ test_cmp expected actual1 &&
+ test_cmp expected actual2 &&
+ test_cmp expected actual3 &&
+ test_cmp expected actual4
+'
+
+test_expect_success 'merge-msg -F' '
+ test_unconfig merge.log &&
+ test_config merge.summary yes &&
+ git checkout main &&
+ test_tick &&
+ git fetch . left right &&
+ git fmt-merge-msg -F .git/FETCH_HEAD >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'merge-msg -F in subdirectory' '
+ test_unconfig merge.log &&
+ test_config merge.summary yes &&
+ git checkout main &&
+ test_tick &&
+ git fetch . left right &&
+ mkdir sub &&
+ cp .git/FETCH_HEAD sub/FETCH_HEAD &&
+ (
+ cd sub &&
+ git fmt-merge-msg -F FETCH_HEAD >../actual
+ ) &&
+ test_cmp expected actual
+'
+
+test_expect_success 'merge-msg with nothing to merge' '
+ test_unconfig merge.log &&
+ test_config merge.summary yes &&
+
+ (
+ cd remote &&
+ git checkout -b unrelated &&
+ test_tick &&
+ git fetch origin &&
+ git fmt-merge-msg <.git/FETCH_HEAD >../actual
+ ) &&
+
+ test_must_be_empty actual
+'
+
+test_expect_success 'merge-msg tag' '
+ cat >expected <<-EOF &&
+ Merge tag ${apos}tag-r3${apos}
+
+ * tag ${apos}tag-r3${apos}:
+ Right #3
+ Common #2
+ Common #1
+ EOF
+
+ test_unconfig merge.log &&
+ test_config merge.summary yes &&
+
+ git checkout main &&
+ test_tick &&
+ git fetch . tag tag-r3 &&
+
+ git fmt-merge-msg <.git/FETCH_HEAD >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'merge-msg two tags' '
+ cat >expected <<-EOF &&
+ Merge tags ${apos}tag-r3${apos} and ${apos}tag-l5${apos}
+
+ * tag ${apos}tag-r3${apos}:
+ Right #3
+ Common #2
+ Common #1
+
+ # By Another Author (3) and A U Thor (2)
+ # Via Another Committer
+ * tag ${apos}tag-l5${apos}:
+ Left #5
+ Left #4
+ Left #3
+ Common #2
+ Common #1
+ EOF
+
+ test_unconfig merge.log &&
+ test_config merge.summary yes &&
+
+ git checkout main &&
+ test_tick &&
+ git fetch . tag tag-r3 tag tag-l5 &&
+
+ git fmt-merge-msg <.git/FETCH_HEAD >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'merge-msg tag and branch' '
+ cat >expected <<-EOF &&
+ Merge branch ${apos}left${apos}, tag ${apos}tag-r3${apos}
+
+ * tag ${apos}tag-r3${apos}:
+ Right #3
+ Common #2
+ Common #1
+
+ # By Another Author (3) and A U Thor (2)
+ # Via Another Committer
+ * left:
+ Left #5
+ Left #4
+ Left #3
+ Common #2
+ Common #1
+ EOF
+
+ test_unconfig merge.log &&
+ test_config merge.summary yes &&
+
+ git checkout main &&
+ test_tick &&
+ git fetch . tag tag-r3 left &&
+
+ git fmt-merge-msg <.git/FETCH_HEAD >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'merge-msg lots of commits' '
+ {
+ cat <<-EOF &&
+ Merge branch ${apos}long${apos}
+
+ * long: (35 commits)
+ EOF
+
+ i=29 &&
+ while test $i -gt 9
+ do
+ echo " $i" &&
+ i=$(($i-1)) || return 1
+ done &&
+ echo " ..."
+ } >expected &&
+
+ test_config merge.summary yes &&
+
+ git checkout main &&
+ test_tick &&
+ git fetch . long &&
+
+ git fmt-merge-msg <.git/FETCH_HEAD >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'merge-msg with "merging" an annotated tag' '
+ test_config merge.log true &&
+
+ git checkout main^0 &&
+ git commit --allow-empty -m "One step ahead" &&
+ git tag -a -m "An annotated one" annote HEAD &&
+
+ git checkout main &&
+ git fetch . annote &&
+
+ git fmt-merge-msg <.git/FETCH_HEAD >actual &&
+ {
+ cat <<-\EOF
+ Merge tag '\''annote'\''
+
+ An annotated one
+
+ * tag '\''annote'\'':
+ One step ahead
+ EOF
+ } >expected &&
+ test_cmp expected actual &&
+
+ test_when_finished "git reset --hard" &&
+ annote=$(git rev-parse annote) &&
+ git merge --no-commit --no-ff $annote &&
+ {
+ cat <<-EOF
+ Merge tag '\''$annote'\''
+
+ An annotated one
+
+ * tag '\''$annote'\'':
+ One step ahead
+ EOF
+ } >expected &&
+ test_cmp expected .git/MERGE_MSG
+'
+
+test_expect_success 'merge --into-name=<name>' '
+ test_when_finished "git checkout main" &&
+ git checkout -B side main &&
+ git commit --allow-empty -m "One step ahead" &&
+
+ git checkout --detach main &&
+ git merge --no-ff side &&
+ git show -s --format="%s" >full.0 &&
+ head -n1 full.0 >actual &&
+ # expect that HEAD is shown as-is
+ grep -e "Merge branch .side. into HEAD$" actual &&
+
+ git reset --hard main &&
+ git merge --no-ff --into-name=main side &&
+ git show -s --format="%s" >full.1 &&
+ head -n1 full.1 >actual &&
+ # expect that we pretend to be merging to main, that is suppressed
+ grep -e "Merge branch .side.$" actual &&
+
+ git checkout -b throwaway main &&
+ git merge --no-ff --into-name=main side &&
+ git show -s --format="%s" >full.2 &&
+ head -n1 full.2 >actual &&
+ # expect that we pretend to be merging to main, that is suppressed
+ grep -e "Merge branch .side.$" actual
+'
+
+test_expect_success 'merge.suppressDest configuration' '
+ test_when_finished "git checkout main" &&
+ git checkout -B side main &&
+ git commit --allow-empty -m "One step ahead" &&
+ git checkout main &&
+ git fetch . side &&
+
+ git -c merge.suppressDest="" fmt-merge-msg <.git/FETCH_HEAD >full.1 &&
+ head -n1 full.1 >actual &&
+ grep -e "Merge branch .side. into main" actual &&
+
+ git -c merge.suppressDest="mast" fmt-merge-msg <.git/FETCH_HEAD >full.2 &&
+ head -n1 full.2 >actual &&
+ grep -e "Merge branch .side. into main$" actual &&
+
+ git -c merge.suppressDest="ma?*[rn]" fmt-merge-msg <.git/FETCH_HEAD >full.3 &&
+ head -n1 full.3 >actual &&
+ grep -e "Merge branch .side." actual &&
+ ! grep -e " into main$" actual &&
+
+ git checkout --detach HEAD &&
+ git -c merge.suppressDest="main" fmt-merge-msg <.git/FETCH_HEAD >full.4 &&
+ head -n1 full.4 >actual &&
+ grep -e "Merge branch .side. into HEAD$" actual &&
+
+ git -c merge.suppressDest="main" fmt-merge-msg \
+ --into-name=main <.git/FETCH_HEAD >full.5 &&
+ head -n1 full.5 >actual &&
+ grep -e "Merge branch .side." actual &&
+ ! grep -e " into main$" actual &&
+ ! grep -e " into HEAD$" actual
+'
+
+test_done
diff --git a/t/t6300-for-each-ref.sh b/t/t6300-for-each-ref.sh
new file mode 100755
index 0000000..00a060d
--- /dev/null
+++ b/t/t6300-for-each-ref.sh
@@ -0,0 +1,2009 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Andy Parkins
+#
+
+test_description='for-each-ref test'
+
+. ./test-lib.sh
+GNUPGHOME_NOT_USED=$GNUPGHOME
+. "$TEST_DIRECTORY"/lib-gpg.sh
+. "$TEST_DIRECTORY"/lib-terminal.sh
+
+# Mon Jul 3 23:18:43 2006 +0000
+datestamp=1151968723
+setdate_and_increment () {
+ GIT_COMMITTER_DATE="$datestamp +0200"
+ datestamp=$(expr "$datestamp" + 1)
+ GIT_AUTHOR_DATE="$datestamp +0200"
+ datestamp=$(expr "$datestamp" + 1)
+ export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
+}
+
+test_expect_success setup '
+ test_oid_cache <<-EOF &&
+ disklen sha1:138
+ disklen sha256:154
+ EOF
+
+ # setup .mailmap
+ cat >.mailmap <<-EOF &&
+ A Thor <athor@example.com> A U Thor <author@example.com>
+ C Mitter <cmitter@example.com> C O Mitter <committer@example.com>
+ EOF
+
+ setdate_and_increment &&
+ echo "Using $datestamp" > one &&
+ git add one &&
+ git commit -m "Initial" &&
+ git branch -M main &&
+ setdate_and_increment &&
+ git tag -a -m "Tagging at $datestamp" testtag &&
+ git update-ref refs/remotes/origin/main main &&
+ git remote add origin nowhere &&
+ git config branch.main.remote origin &&
+ git config branch.main.merge refs/heads/main &&
+ git remote add myfork elsewhere &&
+ git config remote.pushdefault myfork &&
+ git config push.default current
+'
+
+test_atom () {
+ case "$1" in
+ head) ref=refs/heads/main ;;
+ tag) ref=refs/tags/testtag ;;
+ sym) ref=refs/heads/sym ;;
+ *) ref=$1 ;;
+ esac
+ format=$2
+ test_do=test_expect_${4:-success}
+
+ printf '%s\n' "$3" >expected
+ $test_do $PREREQ "basic atom: $ref $format" '
+ git for-each-ref --format="%($format)" "$ref" >actual &&
+ sanitize_pgp <actual >actual.clean &&
+ test_cmp expected actual.clean
+ '
+
+ # Automatically test "contents:size" atom after testing "contents"
+ if test "$format" = "contents"
+ then
+ # for commit leg, $3 is changed there
+ expect=$(printf '%s' "$3" | wc -c)
+ $test_do $PREREQ "basic atom: $ref contents:size" '
+ type=$(git cat-file -t "$ref") &&
+ case $type in
+ tag)
+ # We cannot use $3 as it expects sanitize_pgp to run
+ git cat-file tag $ref >out &&
+ expect=$(tail -n +6 out | wc -c) &&
+ rm -f out ;;
+ tree | blob)
+ expect="" ;;
+ commit)
+ : "use the calculated expect" ;;
+ *)
+ BUG "unknown object type" ;;
+ esac &&
+ # Leave $expect unquoted to lose possible leading whitespaces
+ echo $expect >expected &&
+ git for-each-ref --format="%(contents:size)" "$ref" >actual &&
+ test_cmp expected actual
+ '
+ fi
+}
+
+hexlen=$(test_oid hexsz)
+disklen=$(test_oid disklen)
+
+test_atom head refname refs/heads/main
+test_atom head refname: refs/heads/main
+test_atom head refname:short main
+test_atom head refname:lstrip=1 heads/main
+test_atom head refname:lstrip=2 main
+test_atom head refname:lstrip=-1 main
+test_atom head refname:lstrip=-2 heads/main
+test_atom head refname:rstrip=1 refs/heads
+test_atom head refname:rstrip=2 refs
+test_atom head refname:rstrip=-1 refs
+test_atom head refname:rstrip=-2 refs/heads
+test_atom head refname:strip=1 heads/main
+test_atom head refname:strip=2 main
+test_atom head refname:strip=-1 main
+test_atom head refname:strip=-2 heads/main
+test_atom head upstream refs/remotes/origin/main
+test_atom head upstream:short origin/main
+test_atom head upstream:lstrip=2 origin/main
+test_atom head upstream:lstrip=-2 origin/main
+test_atom head upstream:rstrip=2 refs/remotes
+test_atom head upstream:rstrip=-2 refs/remotes
+test_atom head upstream:strip=2 origin/main
+test_atom head upstream:strip=-2 origin/main
+test_atom head push refs/remotes/myfork/main
+test_atom head push:short myfork/main
+test_atom head push:lstrip=1 remotes/myfork/main
+test_atom head push:lstrip=-1 main
+test_atom head push:rstrip=1 refs/remotes/myfork
+test_atom head push:rstrip=-1 refs
+test_atom head push:strip=1 remotes/myfork/main
+test_atom head push:strip=-1 main
+test_atom head objecttype commit
+test_atom head objectsize $((131 + hexlen))
+test_atom head objectsize:disk $disklen
+test_atom head deltabase $ZERO_OID
+test_atom head objectname $(git rev-parse refs/heads/main)
+test_atom head objectname:short $(git rev-parse --short refs/heads/main)
+test_atom head objectname:short=1 $(git rev-parse --short=1 refs/heads/main)
+test_atom head objectname:short=10 $(git rev-parse --short=10 refs/heads/main)
+test_atom head tree $(git rev-parse refs/heads/main^{tree})
+test_atom head tree:short $(git rev-parse --short refs/heads/main^{tree})
+test_atom head tree:short=1 $(git rev-parse --short=1 refs/heads/main^{tree})
+test_atom head tree:short=10 $(git rev-parse --short=10 refs/heads/main^{tree})
+test_atom head parent ''
+test_atom head parent:short ''
+test_atom head parent:short=1 ''
+test_atom head parent:short=10 ''
+test_atom head numparent 0
+test_atom head object ''
+test_atom head type ''
+test_atom head raw "$(git cat-file commit refs/heads/main)
+"
+test_atom head '*objectname' ''
+test_atom head '*objecttype' ''
+test_atom head author 'A U Thor <author@example.com> 1151968724 +0200'
+test_atom head authorname 'A U Thor'
+test_atom head authorname:mailmap 'A Thor'
+test_atom head authoremail '<author@example.com>'
+test_atom head authoremail:trim 'author@example.com'
+test_atom head authoremail:localpart 'author'
+test_atom head authoremail:trim,localpart 'author'
+test_atom head authoremail:mailmap '<athor@example.com>'
+test_atom head authoremail:mailmap,trim 'athor@example.com'
+test_atom head authoremail:trim,mailmap 'athor@example.com'
+test_atom head authoremail:mailmap,localpart 'athor'
+test_atom head authoremail:localpart,mailmap 'athor'
+test_atom head authoremail:mailmap,trim,localpart,mailmap,trim 'athor'
+test_atom head authordate 'Tue Jul 4 01:18:44 2006 +0200'
+test_atom head committer 'C O Mitter <committer@example.com> 1151968723 +0200'
+test_atom head committername 'C O Mitter'
+test_atom head committername:mailmap 'C Mitter'
+test_atom head committeremail '<committer@example.com>'
+test_atom head committeremail:trim 'committer@example.com'
+test_atom head committeremail:localpart 'committer'
+test_atom head committeremail:localpart,trim 'committer'
+test_atom head committeremail:mailmap '<cmitter@example.com>'
+test_atom head committeremail:mailmap,trim 'cmitter@example.com'
+test_atom head committeremail:trim,mailmap 'cmitter@example.com'
+test_atom head committeremail:mailmap,localpart 'cmitter'
+test_atom head committeremail:localpart,mailmap 'cmitter'
+test_atom head committeremail:trim,mailmap,trim,trim,localpart 'cmitter'
+test_atom head committerdate 'Tue Jul 4 01:18:43 2006 +0200'
+test_atom head tag ''
+test_atom head tagger ''
+test_atom head taggername ''
+test_atom head taggeremail ''
+test_atom head taggeremail:trim ''
+test_atom head taggeremail:localpart ''
+test_atom head taggerdate ''
+test_atom head creator 'C O Mitter <committer@example.com> 1151968723 +0200'
+test_atom head creatordate 'Tue Jul 4 01:18:43 2006 +0200'
+test_atom head subject 'Initial'
+test_atom head subject:sanitize 'Initial'
+test_atom head contents:subject 'Initial'
+test_atom head body ''
+test_atom head contents:body ''
+test_atom head contents:signature ''
+test_atom head contents 'Initial
+'
+test_atom head HEAD '*'
+
+test_atom tag refname refs/tags/testtag
+test_atom tag refname:short testtag
+test_atom tag upstream ''
+test_atom tag push ''
+test_atom tag objecttype tag
+test_atom tag objectsize $((114 + hexlen))
+test_atom tag objectsize:disk $disklen
+test_atom tag '*objectsize:disk' $disklen
+test_atom tag deltabase $ZERO_OID
+test_atom tag '*deltabase' $ZERO_OID
+test_atom tag objectname $(git rev-parse refs/tags/testtag)
+test_atom tag objectname:short $(git rev-parse --short refs/tags/testtag)
+test_atom head objectname:short=1 $(git rev-parse --short=1 refs/heads/main)
+test_atom head objectname:short=10 $(git rev-parse --short=10 refs/heads/main)
+test_atom tag tree ''
+test_atom tag tree:short ''
+test_atom tag tree:short=1 ''
+test_atom tag tree:short=10 ''
+test_atom tag parent ''
+test_atom tag parent:short ''
+test_atom tag parent:short=1 ''
+test_atom tag parent:short=10 ''
+test_atom tag numparent ''
+test_atom tag object $(git rev-parse refs/tags/testtag^0)
+test_atom tag type 'commit'
+test_atom tag '*objectname' $(git rev-parse refs/tags/testtag^{})
+test_atom tag '*objecttype' 'commit'
+test_atom tag author ''
+test_atom tag authorname ''
+test_atom tag authorname:mailmap ''
+test_atom tag authoremail ''
+test_atom tag authoremail:trim ''
+test_atom tag authoremail:localpart ''
+test_atom tag authoremail:trim,localpart ''
+test_atom tag authoremail:mailmap ''
+test_atom tag authoremail:mailmap,trim ''
+test_atom tag authoremail:trim,mailmap ''
+test_atom tag authoremail:mailmap,localpart ''
+test_atom tag authoremail:localpart,mailmap ''
+test_atom tag authoremail:mailmap,trim,localpart,mailmap,trim ''
+test_atom tag authordate ''
+test_atom tag committer ''
+test_atom tag committername ''
+test_atom tag committername:mailmap ''
+test_atom tag committeremail ''
+test_atom tag committeremail:trim ''
+test_atom tag committeremail:localpart ''
+test_atom tag committeremail:localpart,trim ''
+test_atom tag committeremail:mailmap ''
+test_atom tag committeremail:mailmap,trim ''
+test_atom tag committeremail:trim,mailmap ''
+test_atom tag committeremail:mailmap,localpart ''
+test_atom tag committeremail:localpart,mailmap ''
+test_atom tag committeremail:trim,mailmap,trim,trim,localpart ''
+test_atom tag committerdate ''
+test_atom tag tag 'testtag'
+test_atom tag tagger 'C O Mitter <committer@example.com> 1151968725 +0200'
+test_atom tag taggername 'C O Mitter'
+test_atom tag taggername:mailmap 'C Mitter'
+test_atom tag taggeremail '<committer@example.com>'
+test_atom tag taggeremail:trim 'committer@example.com'
+test_atom tag taggeremail:localpart 'committer'
+test_atom tag taggeremail:trim,localpart 'committer'
+test_atom tag taggeremail:mailmap '<cmitter@example.com>'
+test_atom tag taggeremail:mailmap,trim 'cmitter@example.com'
+test_atom tag taggeremail:trim,mailmap 'cmitter@example.com'
+test_atom tag taggeremail:mailmap,localpart 'cmitter'
+test_atom tag taggeremail:localpart,mailmap 'cmitter'
+test_atom tag taggeremail:trim,mailmap,trim,localpart,localpart 'cmitter'
+test_atom tag taggerdate 'Tue Jul 4 01:18:45 2006 +0200'
+test_atom tag creator 'C O Mitter <committer@example.com> 1151968725 +0200'
+test_atom tag creatordate 'Tue Jul 4 01:18:45 2006 +0200'
+test_atom tag subject 'Tagging at 1151968727'
+test_atom tag subject:sanitize 'Tagging-at-1151968727'
+test_atom tag contents:subject 'Tagging at 1151968727'
+test_atom tag body ''
+test_atom tag contents:body ''
+test_atom tag contents:signature ''
+test_atom tag contents 'Tagging at 1151968727
+'
+test_atom tag HEAD ' '
+
+test_expect_success 'basic atom: refs/tags/testtag *raw' '
+ git cat-file commit refs/tags/testtag^{} >expected &&
+ git for-each-ref --format="%(*raw)" refs/tags/testtag >actual &&
+ sanitize_pgp <expected >expected.clean &&
+ echo >>expected.clean &&
+ sanitize_pgp <actual >actual.clean &&
+ test_cmp expected.clean actual.clean
+'
+
+test_expect_success 'Check invalid atoms names are errors' '
+ test_must_fail git for-each-ref --format="%(INVALID)" refs/heads
+'
+
+test_expect_success 'Check format specifiers are ignored in naming date atoms' '
+ git for-each-ref --format="%(authordate)" refs/heads &&
+ git for-each-ref --format="%(authordate:default) %(authordate)" refs/heads &&
+ git for-each-ref --format="%(authordate) %(authordate:default)" refs/heads &&
+ git for-each-ref --format="%(authordate:default) %(authordate:default)" refs/heads
+'
+
+test_expect_success 'Check valid format specifiers for date fields' '
+ git for-each-ref --format="%(authordate:default)" refs/heads &&
+ git for-each-ref --format="%(authordate:relative)" refs/heads &&
+ git for-each-ref --format="%(authordate:short)" refs/heads &&
+ git for-each-ref --format="%(authordate:local)" refs/heads &&
+ git for-each-ref --format="%(authordate:iso8601)" refs/heads &&
+ git for-each-ref --format="%(authordate:rfc2822)" refs/heads
+'
+
+test_expect_success 'Check invalid format specifiers are errors' '
+ test_must_fail git for-each-ref --format="%(authordate:INVALID)" refs/heads
+'
+
+test_expect_success 'arguments to %(objectname:short=) must be positive integers' '
+ test_must_fail git for-each-ref --format="%(objectname:short=0)" &&
+ test_must_fail git for-each-ref --format="%(objectname:short=-1)" &&
+ test_must_fail git for-each-ref --format="%(objectname:short=foo)"
+'
+
+test_bad_atom () {
+ case "$1" in
+ head) ref=refs/heads/main ;;
+ tag) ref=refs/tags/testtag ;;
+ sym) ref=refs/heads/sym ;;
+ *) ref=$1 ;;
+ esac
+ format=$2
+ test_do=test_expect_${4:-success}
+
+ printf '%s\n' "$3" >expect
+ $test_do $PREREQ "err basic atom: $ref $format" '
+ test_must_fail git for-each-ref \
+ --format="%($format)" "$ref" 2>error &&
+ test_cmp expect error
+ '
+}
+
+test_bad_atom head 'authoremail:foo' \
+ 'fatal: unrecognized %(authoremail) argument: foo'
+
+test_bad_atom head 'authoremail:mailmap,trim,bar' \
+ 'fatal: unrecognized %(authoremail) argument: bar'
+
+test_bad_atom head 'authoremail:trim,' \
+ 'fatal: unrecognized %(authoremail) argument: '
+
+test_bad_atom head 'authoremail:mailmaptrim' \
+ 'fatal: unrecognized %(authoremail) argument: trim'
+
+test_bad_atom head 'committeremail: ' \
+ 'fatal: unrecognized %(committeremail) argument: '
+
+test_bad_atom head 'committeremail: trim,foo' \
+ 'fatal: unrecognized %(committeremail) argument: trim,foo'
+
+test_bad_atom head 'committeremail:mailmap,localpart ' \
+ 'fatal: unrecognized %(committeremail) argument: '
+
+test_bad_atom head 'committeremail:trim_localpart' \
+ 'fatal: unrecognized %(committeremail) argument: _localpart'
+
+test_bad_atom head 'committeremail:localpart,,,trim' \
+ 'fatal: unrecognized %(committeremail) argument: ,,trim'
+
+test_bad_atom tag 'taggeremail:mailmap,trim, foo ' \
+ 'fatal: unrecognized %(taggeremail) argument: foo '
+
+test_bad_atom tag 'taggeremail:trim,localpart,' \
+ 'fatal: unrecognized %(taggeremail) argument: '
+
+test_bad_atom tag 'taggeremail:mailmap;localpart trim' \
+ 'fatal: unrecognized %(taggeremail) argument: ;localpart trim'
+
+test_bad_atom tag 'taggeremail:localpart trim' \
+ 'fatal: unrecognized %(taggeremail) argument: trim'
+
+test_bad_atom tag 'taggeremail:mailmap,mailmap,trim,qux,localpart,trim' \
+ 'fatal: unrecognized %(taggeremail) argument: qux,localpart,trim'
+
+test_date () {
+ f=$1 &&
+ committer_date=$2 &&
+ author_date=$3 &&
+ tagger_date=$4 &&
+ cat >expected <<-EOF &&
+ 'refs/heads/main' '$committer_date' '$author_date'
+ 'refs/tags/testtag' '$tagger_date'
+ EOF
+ (
+ git for-each-ref --shell \
+ --format="%(refname) %(committerdate${f:+:$f}) %(authordate${f:+:$f})" \
+ refs/heads &&
+ git for-each-ref --shell \
+ --format="%(refname) %(taggerdate${f:+:$f})" \
+ refs/tags
+ ) >actual &&
+ test_cmp expected actual
+}
+
+test_expect_success 'Check unformatted date fields output' '
+ test_date "" \
+ "Tue Jul 4 01:18:43 2006 +0200" \
+ "Tue Jul 4 01:18:44 2006 +0200" \
+ "Tue Jul 4 01:18:45 2006 +0200"
+'
+
+test_expect_success 'Check format "default" formatted date fields output' '
+ test_date default \
+ "Tue Jul 4 01:18:43 2006 +0200" \
+ "Tue Jul 4 01:18:44 2006 +0200" \
+ "Tue Jul 4 01:18:45 2006 +0200"
+'
+
+test_expect_success 'Check format "default-local" date fields output' '
+ test_date default-local "Mon Jul 3 23:18:43 2006" "Mon Jul 3 23:18:44 2006" "Mon Jul 3 23:18:45 2006"
+'
+
+# Don't know how to do relative check because I can't know when this script
+# is going to be run and can't fake the current time to git, and hence can't
+# provide expected output. Instead, I'll just make sure that "relative"
+# doesn't exit in error
+test_expect_success 'Check format "relative" date fields output' '
+ f=relative &&
+ (git for-each-ref --shell --format="%(refname) %(committerdate:$f) %(authordate:$f)" refs/heads &&
+ git for-each-ref --shell --format="%(refname) %(taggerdate:$f)" refs/tags) >actual
+'
+
+# We just check that this is the same as "relative" for now.
+test_expect_success 'Check format "relative-local" date fields output' '
+ test_date relative-local \
+ "$(git for-each-ref --format="%(committerdate:relative)" refs/heads)" \
+ "$(git for-each-ref --format="%(authordate:relative)" refs/heads)" \
+ "$(git for-each-ref --format="%(taggerdate:relative)" refs/tags)"
+'
+
+test_expect_success 'Check format "short" date fields output' '
+ test_date short 2006-07-04 2006-07-04 2006-07-04
+'
+
+test_expect_success 'Check format "short-local" date fields output' '
+ test_date short-local 2006-07-03 2006-07-03 2006-07-03
+'
+
+test_expect_success 'Check format "local" date fields output' '
+ test_date local \
+ "Mon Jul 3 23:18:43 2006" \
+ "Mon Jul 3 23:18:44 2006" \
+ "Mon Jul 3 23:18:45 2006"
+'
+
+test_expect_success 'Check format "iso8601" date fields output' '
+ test_date iso8601 \
+ "2006-07-04 01:18:43 +0200" \
+ "2006-07-04 01:18:44 +0200" \
+ "2006-07-04 01:18:45 +0200"
+'
+
+test_expect_success 'Check format "iso8601-local" date fields output' '
+ test_date iso8601-local "2006-07-03 23:18:43 +0000" "2006-07-03 23:18:44 +0000" "2006-07-03 23:18:45 +0000"
+'
+
+test_expect_success 'Check format "rfc2822" date fields output' '
+ test_date rfc2822 \
+ "Tue, 4 Jul 2006 01:18:43 +0200" \
+ "Tue, 4 Jul 2006 01:18:44 +0200" \
+ "Tue, 4 Jul 2006 01:18:45 +0200"
+'
+
+test_expect_success 'Check format "rfc2822-local" date fields output' '
+ test_date rfc2822-local "Mon, 3 Jul 2006 23:18:43 +0000" "Mon, 3 Jul 2006 23:18:44 +0000" "Mon, 3 Jul 2006 23:18:45 +0000"
+'
+
+test_expect_success 'Check format "raw" date fields output' '
+ test_date raw "1151968723 +0200" "1151968724 +0200" "1151968725 +0200"
+'
+
+test_expect_success 'Check format "raw-local" date fields output' '
+ test_date raw-local "1151968723 +0000" "1151968724 +0000" "1151968725 +0000"
+'
+
+test_expect_success 'Check format of strftime date fields' '
+ echo "my date is 2006-07-04" >expected &&
+ git for-each-ref \
+ --format="%(authordate:format:my date is %Y-%m-%d)" \
+ refs/heads >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'Check format of strftime-local date fields' '
+ echo "my date is 2006-07-03" >expected &&
+ git for-each-ref \
+ --format="%(authordate:format-local:my date is %Y-%m-%d)" \
+ refs/heads >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'exercise strftime with odd fields' '
+ echo >expected &&
+ git for-each-ref --format="%(authordate:format:)" refs/heads >actual &&
+ test_cmp expected actual &&
+ long="long format -- $ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID$ZERO_OID" &&
+ echo $long >expected &&
+ git for-each-ref --format="%(authordate:format:$long)" refs/heads >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<\EOF
+refs/heads/main
+refs/remotes/origin/main
+refs/tags/testtag
+EOF
+
+test_expect_success 'Verify ascending sort' '
+ git for-each-ref --format="%(refname)" --sort=refname >actual &&
+ test_cmp expected actual
+'
+
+
+cat >expected <<\EOF
+refs/tags/testtag
+refs/remotes/origin/main
+refs/heads/main
+EOF
+
+test_expect_success 'Verify descending sort' '
+ git for-each-ref --format="%(refname)" --sort=-refname >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'Give help even with invalid sort atoms' '
+ test_expect_code 129 git for-each-ref --sort=bogus -h >actual 2>&1 &&
+ grep "^usage: git for-each-ref" actual
+'
+
+cat >expected <<\EOF
+refs/tags/testtag
+refs/tags/testtag-2
+EOF
+
+test_expect_success 'exercise patterns with prefixes' '
+ git tag testtag-2 &&
+ test_when_finished "git tag -d testtag-2" &&
+ git for-each-ref --format="%(refname)" \
+ refs/tags/testtag refs/tags/testtag-2 >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<\EOF
+refs/tags/testtag
+refs/tags/testtag-2
+EOF
+
+test_expect_success 'exercise glob patterns with prefixes' '
+ git tag testtag-2 &&
+ test_when_finished "git tag -d testtag-2" &&
+ git for-each-ref --format="%(refname)" \
+ refs/tags/testtag "refs/tags/testtag-*" >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<\EOF
+refs/tags/bar
+refs/tags/baz
+refs/tags/testtag
+EOF
+
+test_expect_success 'exercise patterns with prefix exclusions' '
+ for tag in foo/one foo/two foo/three bar baz
+ do
+ git tag "$tag" || return 1
+ done &&
+ test_when_finished "git tag -d foo/one foo/two foo/three bar baz" &&
+ git for-each-ref --format="%(refname)" \
+ refs/tags/ --exclude=refs/tags/foo >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<\EOF
+refs/tags/bar
+refs/tags/baz
+refs/tags/foo/one
+refs/tags/testtag
+EOF
+
+test_expect_success 'exercise patterns with pattern exclusions' '
+ for tag in foo/one foo/two foo/three bar baz
+ do
+ git tag "$tag" || return 1
+ done &&
+ test_when_finished "git tag -d foo/one foo/two foo/three bar baz" &&
+ git for-each-ref --format="%(refname)" \
+ refs/tags/ --exclude="refs/tags/foo/t*" >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<\EOF
+'refs/heads/main'
+'refs/remotes/origin/main'
+'refs/tags/testtag'
+EOF
+
+test_expect_success 'Quoting style: shell' '
+ git for-each-ref --shell --format="%(refname)" >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'Quoting style: perl' '
+ git for-each-ref --perl --format="%(refname)" >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'Quoting style: python' '
+ git for-each-ref --python --format="%(refname)" >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<\EOF
+"refs/heads/main"
+"refs/remotes/origin/main"
+"refs/tags/testtag"
+EOF
+
+test_expect_success 'Quoting style: tcl' '
+ git for-each-ref --tcl --format="%(refname)" >actual &&
+ test_cmp expected actual
+'
+
+for i in "--perl --shell" "-s --python" "--python --tcl" "--tcl --perl"; do
+ test_expect_success "more than one quoting style: $i" "
+ test_must_fail git for-each-ref $i 2>err &&
+ grep '^error: more than one quoting style' err
+ "
+done
+
+test_expect_success 'setup for upstream:track[short]' '
+ test_commit two
+'
+
+test_atom head upstream:track '[ahead 1]'
+test_atom head upstream:trackshort '>'
+test_atom head upstream:track,nobracket 'ahead 1'
+test_atom head upstream:nobracket,track 'ahead 1'
+
+test_expect_success 'setup for push:track[short]' '
+ test_commit third &&
+ git update-ref refs/remotes/myfork/main main &&
+ git reset main~1
+'
+
+test_atom head push:track '[behind 1]'
+test_atom head push:trackshort '<'
+
+test_expect_success 'Check that :track[short] cannot be used with other atoms' '
+ test_must_fail git for-each-ref --format="%(refname:track)" 2>/dev/null &&
+ test_must_fail git for-each-ref --format="%(refname:trackshort)" 2>/dev/null
+'
+
+test_expect_success 'Check that :track[short] works when upstream is invalid' '
+ cat >expected <<-\EOF &&
+ [gone]
+
+ EOF
+ test_when_finished "git config branch.main.merge refs/heads/main" &&
+ git config branch.main.merge refs/heads/does-not-exist &&
+ git for-each-ref \
+ --format="%(upstream:track)$LF%(upstream:trackshort)" \
+ refs/heads >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'Check for invalid refname format' '
+ test_must_fail git for-each-ref --format="%(refname:INVALID)"
+'
+
+test_expect_success 'set up color tests' '
+ cat >expected.color <<-EOF &&
+ $(git rev-parse --short refs/heads/main) <GREEN>main<RESET>
+ $(git rev-parse --short refs/remotes/myfork/main) <GREEN>myfork/main<RESET>
+ $(git rev-parse --short refs/remotes/origin/main) <GREEN>origin/main<RESET>
+ $(git rev-parse --short refs/tags/testtag) <GREEN>testtag<RESET>
+ $(git rev-parse --short refs/tags/third) <GREEN>third<RESET>
+ $(git rev-parse --short refs/tags/two) <GREEN>two<RESET>
+ EOF
+ sed "s/<[^>]*>//g" <expected.color >expected.bare &&
+ color_format="%(objectname:short) %(color:green)%(refname:short)"
+'
+
+test_expect_success TTY '%(color) shows color with a tty' '
+ test_terminal git for-each-ref --format="$color_format" >actual.raw &&
+ test_decode_color <actual.raw >actual &&
+ test_cmp expected.color actual
+'
+
+test_expect_success '%(color) does not show color without tty' '
+ TERM=vt100 git for-each-ref --format="$color_format" >actual &&
+ test_cmp expected.bare actual
+'
+
+test_expect_success '--color can override tty check' '
+ git for-each-ref --color --format="$color_format" >actual.raw &&
+ test_decode_color <actual.raw >actual &&
+ test_cmp expected.color actual
+'
+
+test_expect_success 'color.ui=always does not override tty check' '
+ git -c color.ui=always for-each-ref --format="$color_format" >actual &&
+ test_cmp expected.bare actual
+'
+
+test_expect_success 'setup for describe atom tests' '
+ git init -b master describe-repo &&
+ (
+ cd describe-repo &&
+
+ test_commit --no-tag one &&
+ git tag tagone &&
+
+ test_commit --no-tag two &&
+ git tag -a -m "tag two" tagtwo
+ )
+'
+
+test_expect_success 'describe atom vs git describe' '
+ (
+ cd describe-repo &&
+
+ git for-each-ref --format="%(objectname)" \
+ refs/tags/ >obj &&
+ while read hash
+ do
+ if desc=$(git describe $hash)
+ then
+ : >expect-contains-good
+ else
+ : >expect-contains-bad
+ fi &&
+ echo "$hash $desc" || return 1
+ done <obj >expect &&
+ test_path_exists expect-contains-good &&
+ test_path_exists expect-contains-bad &&
+
+ git for-each-ref --format="%(objectname) %(describe)" \
+ refs/tags/ >actual 2>err &&
+ test_cmp expect actual &&
+ test_must_be_empty err
+ )
+'
+
+test_expect_success 'describe:tags vs describe --tags' '
+ (
+ cd describe-repo &&
+ git describe --tags >expect &&
+ git for-each-ref --format="%(describe:tags)" \
+ refs/heads/master >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'describe:abbrev=... vs describe --abbrev=...' '
+ (
+ cd describe-repo &&
+
+ # Case 1: We have commits between HEAD and the most
+ # recent tag reachable from it
+ test_commit --no-tag file &&
+ git describe --abbrev=14 >expect &&
+ git for-each-ref --format="%(describe:abbrev=14)" \
+ refs/heads/master >actual &&
+ test_cmp expect actual &&
+
+ # Make sure the hash used is atleast 14 digits long
+ sed -e "s/^.*-g\([0-9a-f]*\)$/\1/" <actual >hexpart &&
+ test 15 -le $(wc -c <hexpart) &&
+
+ # Case 2: We have a tag at HEAD, describe directly gives
+ # the name of the tag
+ git tag -a -m tagged tagname &&
+ git describe --abbrev=14 >expect &&
+ git for-each-ref --format="%(describe:abbrev=14)" \
+ refs/heads/master >actual &&
+ test_cmp expect actual &&
+ test tagname = $(cat actual)
+ )
+'
+
+test_expect_success 'describe:match=... vs describe --match ...' '
+ (
+ cd describe-repo &&
+ git tag -a -m "tag foo" tag-foo &&
+ git describe --match "*-foo" >expect &&
+ git for-each-ref --format="%(describe:match="*-foo")" \
+ refs/heads/master >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'describe:exclude:... vs describe --exclude ...' '
+ (
+ cd describe-repo &&
+ git tag -a -m "tag bar" tag-bar &&
+ git describe --exclude "*-bar" >expect &&
+ git for-each-ref --format="%(describe:exclude="*-bar")" \
+ refs/heads/master >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'deref with describe atom' '
+ (
+ cd describe-repo &&
+ cat >expect <<-\EOF &&
+
+ tagname
+ tagname
+ tagname
+
+ tagtwo
+ EOF
+ git for-each-ref --format="%(*describe)" >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'err on bad describe atom arg' '
+ (
+ cd describe-repo &&
+
+ # The bad arg is the only arg passed to describe atom
+ cat >expect <<-\EOF &&
+ fatal: unrecognized %(describe) argument: baz
+ EOF
+ test_must_fail git for-each-ref --format="%(describe:baz)" \
+ refs/heads/master 2>actual &&
+ test_cmp expect actual &&
+
+ # The bad arg is in the middle of the option string
+ # passed to the describe atom
+ cat >expect <<-\EOF &&
+ fatal: unrecognized %(describe) argument: qux=1,abbrev=14
+ EOF
+ test_must_fail git for-each-ref \
+ --format="%(describe:tags,qux=1,abbrev=14)" \
+ ref/heads/master 2>actual &&
+ test_cmp expect actual
+ )
+'
+
+cat >expected <<\EOF
+heads/main
+tags/main
+EOF
+
+test_expect_success 'Check ambiguous head and tag refs (strict)' '
+ git config --bool core.warnambiguousrefs true &&
+ git checkout -b newtag &&
+ echo "Using $datestamp" > one &&
+ git add one &&
+ git commit -m "Branch" &&
+ setdate_and_increment &&
+ git tag -m "Tagging at $datestamp" main &&
+ git for-each-ref --format "%(refname:short)" refs/heads/main refs/tags/main >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<\EOF
+heads/main
+main
+EOF
+
+test_expect_success 'Check ambiguous head and tag refs (loose)' '
+ git config --bool core.warnambiguousrefs false &&
+ git for-each-ref --format "%(refname:short)" refs/heads/main refs/tags/main >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<\EOF
+heads/ambiguous
+ambiguous
+EOF
+
+test_expect_success 'Check ambiguous head and tag refs II (loose)' '
+ git checkout main &&
+ git tag ambiguous testtag^0 &&
+ git branch ambiguous testtag^0 &&
+ git for-each-ref --format "%(refname:short)" refs/heads/ambiguous refs/tags/ambiguous >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'create tag without tagger' '
+ git tag -a -m "Broken tag" taggerless &&
+ git tag -f taggerless $(git cat-file tag taggerless |
+ sed -e "/^tagger /d" |
+ git hash-object --literally --stdin -w -t tag)
+'
+
+test_atom refs/tags/taggerless type 'commit'
+test_atom refs/tags/taggerless tag 'taggerless'
+test_atom refs/tags/taggerless tagger ''
+test_atom refs/tags/taggerless taggername ''
+test_atom refs/tags/taggerless taggeremail ''
+test_atom refs/tags/taggerless taggeremail:trim ''
+test_atom refs/tags/taggerless taggeremail:localpart ''
+test_atom refs/tags/taggerless taggerdate ''
+test_atom refs/tags/taggerless committer ''
+test_atom refs/tags/taggerless committername ''
+test_atom refs/tags/taggerless committeremail ''
+test_atom refs/tags/taggerless committeremail:trim ''
+test_atom refs/tags/taggerless committeremail:localpart ''
+test_atom refs/tags/taggerless committerdate ''
+test_atom refs/tags/taggerless subject 'Broken tag'
+
+test_expect_success 'an unusual tag with an incomplete line' '
+
+ git tag -m "bogo" bogo &&
+ bogo=$(git cat-file tag bogo) &&
+ bogo=$(printf "%s" "$bogo" | git mktag) &&
+ git tag -f bogo "$bogo" &&
+ git for-each-ref --format "%(body)" refs/tags/bogo
+
+'
+
+test_expect_success 'create tag with subject and body content' '
+ cat >>msg <<-\EOF &&
+ the subject line
+
+ first body line
+ second body line
+ EOF
+ git tag -F msg subject-body
+'
+test_atom refs/tags/subject-body subject 'the subject line'
+test_atom refs/tags/subject-body subject:sanitize 'the-subject-line'
+test_atom refs/tags/subject-body body 'first body line
+second body line
+'
+test_atom refs/tags/subject-body contents 'the subject line
+
+first body line
+second body line
+'
+
+test_expect_success 'create tag with multiline subject' '
+ cat >msg <<-\EOF &&
+ first subject line
+ second subject line
+
+ first body line
+ second body line
+ EOF
+ git tag -F msg multiline
+'
+test_atom refs/tags/multiline subject 'first subject line second subject line'
+test_atom refs/tags/multiline subject:sanitize 'first-subject-line-second-subject-line'
+test_atom refs/tags/multiline contents:subject 'first subject line second subject line'
+test_atom refs/tags/multiline body 'first body line
+second body line
+'
+test_atom refs/tags/multiline contents:body 'first body line
+second body line
+'
+test_atom refs/tags/multiline contents:signature ''
+test_atom refs/tags/multiline contents 'first subject line
+second subject line
+
+first body line
+second body line
+'
+
+test_expect_success GPG 'create signed tags' '
+ git tag -s -m "" signed-empty &&
+ git tag -s -m "subject line" signed-short &&
+ cat >msg <<-\EOF &&
+ subject line
+
+ body contents
+ EOF
+ git tag -s -F msg signed-long
+'
+
+sig='-----BEGIN PGP SIGNATURE-----
+-----END PGP SIGNATURE-----
+'
+
+PREREQ=GPG
+test_atom refs/tags/signed-empty subject ''
+test_atom refs/tags/signed-empty subject:sanitize ''
+test_atom refs/tags/signed-empty contents:subject ''
+test_atom refs/tags/signed-empty body "$sig"
+test_atom refs/tags/signed-empty contents:body ''
+test_atom refs/tags/signed-empty contents:signature "$sig"
+test_atom refs/tags/signed-empty contents "$sig"
+
+test_expect_success GPG 'basic atom: refs/tags/signed-empty raw' '
+ git cat-file tag refs/tags/signed-empty >expected &&
+ git for-each-ref --format="%(raw)" refs/tags/signed-empty >actual &&
+ sanitize_pgp <expected >expected.clean &&
+ echo >>expected.clean &&
+ sanitize_pgp <actual >actual.clean &&
+ test_cmp expected.clean actual.clean
+'
+
+test_atom refs/tags/signed-short subject 'subject line'
+test_atom refs/tags/signed-short subject:sanitize 'subject-line'
+test_atom refs/tags/signed-short contents:subject 'subject line'
+test_atom refs/tags/signed-short body "$sig"
+test_atom refs/tags/signed-short contents:body ''
+test_atom refs/tags/signed-short contents:signature "$sig"
+test_atom refs/tags/signed-short contents "subject line
+$sig"
+
+test_expect_success GPG 'basic atom: refs/tags/signed-short raw' '
+ git cat-file tag refs/tags/signed-short >expected &&
+ git for-each-ref --format="%(raw)" refs/tags/signed-short >actual &&
+ sanitize_pgp <expected >expected.clean &&
+ echo >>expected.clean &&
+ sanitize_pgp <actual >actual.clean &&
+ test_cmp expected.clean actual.clean
+'
+
+test_atom refs/tags/signed-long subject 'subject line'
+test_atom refs/tags/signed-long subject:sanitize 'subject-line'
+test_atom refs/tags/signed-long contents:subject 'subject line'
+test_atom refs/tags/signed-long body "body contents
+$sig"
+test_atom refs/tags/signed-long contents:body 'body contents
+'
+test_atom refs/tags/signed-long contents:signature "$sig"
+test_atom refs/tags/signed-long contents "subject line
+
+body contents
+$sig"
+
+test_expect_success GPG 'basic atom: refs/tags/signed-long raw' '
+ git cat-file tag refs/tags/signed-long >expected &&
+ git for-each-ref --format="%(raw)" refs/tags/signed-long >actual &&
+ sanitize_pgp <expected >expected.clean &&
+ echo >>expected.clean &&
+ sanitize_pgp <actual >actual.clean &&
+ test_cmp expected.clean actual.clean
+'
+
+test_expect_success 'set up refs pointing to tree and blob' '
+ git update-ref refs/mytrees/first refs/heads/main^{tree} &&
+ git update-ref refs/myblobs/first refs/heads/main:one
+'
+
+test_atom refs/mytrees/first subject ""
+test_atom refs/mytrees/first contents:subject ""
+test_atom refs/mytrees/first body ""
+test_atom refs/mytrees/first contents:body ""
+test_atom refs/mytrees/first contents:signature ""
+test_atom refs/mytrees/first contents ""
+
+test_expect_success 'basic atom: refs/mytrees/first raw' '
+ git cat-file tree refs/mytrees/first >expected &&
+ echo >>expected &&
+ git for-each-ref --format="%(raw)" refs/mytrees/first >actual &&
+ test_cmp expected actual &&
+ git cat-file -s refs/mytrees/first >expected &&
+ git for-each-ref --format="%(raw:size)" refs/mytrees/first >actual &&
+ test_cmp expected actual
+'
+
+test_atom refs/myblobs/first subject ""
+test_atom refs/myblobs/first contents:subject ""
+test_atom refs/myblobs/first body ""
+test_atom refs/myblobs/first contents:body ""
+test_atom refs/myblobs/first contents:signature ""
+test_atom refs/myblobs/first contents ""
+
+test_expect_success 'basic atom: refs/myblobs/first raw' '
+ git cat-file blob refs/myblobs/first >expected &&
+ echo >>expected &&
+ git for-each-ref --format="%(raw)" refs/myblobs/first >actual &&
+ test_cmp expected actual &&
+ git cat-file -s refs/myblobs/first >expected &&
+ git for-each-ref --format="%(raw:size)" refs/myblobs/first >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'set up refs pointing to binary blob' '
+ printf "a\0b\0c" >blob1 &&
+ printf "a\0c\0b" >blob2 &&
+ printf "\0a\0b\0c" >blob3 &&
+ printf "abc" >blob4 &&
+ printf "\0 \0 \0 " >blob5 &&
+ printf "\0 \0a\0 " >blob6 &&
+ printf " " >blob7 &&
+ >blob8 &&
+ obj=$(git hash-object -w blob1) &&
+ git update-ref refs/myblobs/blob1 "$obj" &&
+ obj=$(git hash-object -w blob2) &&
+ git update-ref refs/myblobs/blob2 "$obj" &&
+ obj=$(git hash-object -w blob3) &&
+ git update-ref refs/myblobs/blob3 "$obj" &&
+ obj=$(git hash-object -w blob4) &&
+ git update-ref refs/myblobs/blob4 "$obj" &&
+ obj=$(git hash-object -w blob5) &&
+ git update-ref refs/myblobs/blob5 "$obj" &&
+ obj=$(git hash-object -w blob6) &&
+ git update-ref refs/myblobs/blob6 "$obj" &&
+ obj=$(git hash-object -w blob7) &&
+ git update-ref refs/myblobs/blob7 "$obj" &&
+ obj=$(git hash-object -w blob8) &&
+ git update-ref refs/myblobs/blob8 "$obj"
+'
+
+test_expect_success 'Verify sorts with raw' '
+ cat >expected <<-EOF &&
+ refs/myblobs/blob8
+ refs/myblobs/blob5
+ refs/myblobs/blob6
+ refs/myblobs/blob3
+ refs/myblobs/blob7
+ refs/mytrees/first
+ refs/myblobs/first
+ refs/myblobs/blob1
+ refs/myblobs/blob2
+ refs/myblobs/blob4
+ refs/heads/main
+ EOF
+ git for-each-ref --format="%(refname)" --sort=raw \
+ refs/heads/main refs/myblobs/ refs/mytrees/first >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'Verify sorts with raw:size' '
+ cat >expected <<-EOF &&
+ refs/myblobs/blob8
+ refs/myblobs/blob7
+ refs/myblobs/blob4
+ refs/myblobs/blob1
+ refs/myblobs/blob2
+ refs/myblobs/blob3
+ refs/myblobs/blob5
+ refs/myblobs/blob6
+ refs/myblobs/first
+ refs/mytrees/first
+ refs/heads/main
+ EOF
+ git for-each-ref --format="%(refname)" --sort=raw:size \
+ refs/heads/main refs/myblobs/ refs/mytrees/first >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'validate raw atom with %(if:equals)' '
+ cat >expected <<-EOF &&
+ not equals
+ not equals
+ not equals
+ not equals
+ not equals
+ not equals
+ refs/myblobs/blob4
+ not equals
+ not equals
+ not equals
+ not equals
+ not equals
+ EOF
+ git for-each-ref --format="%(if:equals=abc)%(raw)%(then)%(refname)%(else)not equals%(end)" \
+ refs/myblobs/ refs/heads/ >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'validate raw atom with %(if:notequals)' '
+ cat >expected <<-EOF &&
+ refs/heads/ambiguous
+ refs/heads/main
+ refs/heads/newtag
+ refs/myblobs/blob1
+ refs/myblobs/blob2
+ refs/myblobs/blob3
+ equals
+ refs/myblobs/blob5
+ refs/myblobs/blob6
+ refs/myblobs/blob7
+ refs/myblobs/blob8
+ refs/myblobs/first
+ EOF
+ git for-each-ref --format="%(if:notequals=abc)%(raw)%(then)%(refname)%(else)equals%(end)" \
+ refs/myblobs/ refs/heads/ >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'empty raw refs with %(if)' '
+ cat >expected <<-EOF &&
+ refs/myblobs/blob1 not empty
+ refs/myblobs/blob2 not empty
+ refs/myblobs/blob3 not empty
+ refs/myblobs/blob4 not empty
+ refs/myblobs/blob5 not empty
+ refs/myblobs/blob6 not empty
+ refs/myblobs/blob7 empty
+ refs/myblobs/blob8 empty
+ refs/myblobs/first not empty
+ EOF
+ git for-each-ref --format="%(refname) %(if)%(raw)%(then)not empty%(else)empty%(end)" \
+ refs/myblobs/ >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success '%(raw) with --python must fail' '
+ test_must_fail git for-each-ref --format="%(raw)" --python
+'
+
+test_expect_success '%(raw) with --tcl must fail' '
+ test_must_fail git for-each-ref --format="%(raw)" --tcl
+'
+
+test_expect_success '%(raw) with --perl' '
+ git for-each-ref --format="\$name= %(raw);
+print \"\$name\"" refs/myblobs/blob1 --perl | perl >actual &&
+ cmp blob1 actual &&
+ git for-each-ref --format="\$name= %(raw);
+print \"\$name\"" refs/myblobs/blob3 --perl | perl >actual &&
+ cmp blob3 actual &&
+ git for-each-ref --format="\$name= %(raw);
+print \"\$name\"" refs/myblobs/blob8 --perl | perl >actual &&
+ cmp blob8 actual &&
+ git for-each-ref --format="\$name= %(raw);
+print \"\$name\"" refs/myblobs/first --perl | perl >actual &&
+ cmp one actual &&
+ git cat-file tree refs/mytrees/first > expected &&
+ git for-each-ref --format="\$name= %(raw);
+print \"\$name\"" refs/mytrees/first --perl | perl >actual &&
+ cmp expected actual
+'
+
+test_expect_success '%(raw) with --shell must fail' '
+ test_must_fail git for-each-ref --format="%(raw)" --shell
+'
+
+test_expect_success '%(raw) with --shell and --sort=raw must fail' '
+ test_must_fail git for-each-ref --format="%(raw)" --sort=raw --shell
+'
+
+test_expect_success '%(raw:size) with --shell' '
+ git for-each-ref --format="%(raw:size)" | sed "s/^/$SQ/;s/$/$SQ/" >expect &&
+ git for-each-ref --format="%(raw:size)" --shell >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'for-each-ref --format compare with cat-file --batch' '
+ git rev-parse refs/mytrees/first | git cat-file --batch >expected &&
+ git for-each-ref --format="%(objectname) %(objecttype) %(objectsize)
+%(raw)" refs/mytrees/first >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'verify sorts with contents:size' '
+ cat >expect <<-\EOF &&
+ refs/heads/main
+ refs/heads/newtag
+ refs/heads/ambiguous
+ EOF
+ git for-each-ref --format="%(refname)" \
+ --sort=contents:size refs/heads/ >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'set up multiple-sort tags' '
+ for when in 100000 200000
+ do
+ for email in user1 user2
+ do
+ for ref in ref1 ref2
+ do
+ GIT_COMMITTER_DATE="@$when +0000" \
+ GIT_COMMITTER_EMAIL="$email@example.com" \
+ git tag -m "tag $ref-$when-$email" \
+ multi-$ref-$when-$email || return 1
+ done
+ done
+ done
+'
+
+test_expect_success 'Verify sort with multiple keys' '
+ cat >expected <<-\EOF &&
+ 100000 <user1@example.com> refs/tags/multi-ref2-100000-user1
+ 100000 <user1@example.com> refs/tags/multi-ref1-100000-user1
+ 100000 <user2@example.com> refs/tags/multi-ref2-100000-user2
+ 100000 <user2@example.com> refs/tags/multi-ref1-100000-user2
+ 200000 <user1@example.com> refs/tags/multi-ref2-200000-user1
+ 200000 <user1@example.com> refs/tags/multi-ref1-200000-user1
+ 200000 <user2@example.com> refs/tags/multi-ref2-200000-user2
+ 200000 <user2@example.com> refs/tags/multi-ref1-200000-user2
+ EOF
+ git for-each-ref \
+ --format="%(taggerdate:unix) %(taggeremail) %(refname)" \
+ --sort=-refname \
+ --sort=taggeremail \
+ --sort=taggerdate \
+ "refs/tags/multi-*" >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'equivalent sorts fall back on refname' '
+ cat >expected <<-\EOF &&
+ 100000 <user1@example.com> refs/tags/multi-ref1-100000-user1
+ 100000 <user2@example.com> refs/tags/multi-ref1-100000-user2
+ 100000 <user1@example.com> refs/tags/multi-ref2-100000-user1
+ 100000 <user2@example.com> refs/tags/multi-ref2-100000-user2
+ 200000 <user1@example.com> refs/tags/multi-ref1-200000-user1
+ 200000 <user2@example.com> refs/tags/multi-ref1-200000-user2
+ 200000 <user1@example.com> refs/tags/multi-ref2-200000-user1
+ 200000 <user2@example.com> refs/tags/multi-ref2-200000-user2
+ EOF
+ git for-each-ref \
+ --format="%(taggerdate:unix) %(taggeremail) %(refname)" \
+ --sort=taggerdate \
+ "refs/tags/multi-*" >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success '--no-sort cancels the previous sort keys' '
+ cat >expected <<-\EOF &&
+ 100000 <user1@example.com> refs/tags/multi-ref1-100000-user1
+ 100000 <user2@example.com> refs/tags/multi-ref1-100000-user2
+ 100000 <user1@example.com> refs/tags/multi-ref2-100000-user1
+ 100000 <user2@example.com> refs/tags/multi-ref2-100000-user2
+ 200000 <user1@example.com> refs/tags/multi-ref1-200000-user1
+ 200000 <user2@example.com> refs/tags/multi-ref1-200000-user2
+ 200000 <user1@example.com> refs/tags/multi-ref2-200000-user1
+ 200000 <user2@example.com> refs/tags/multi-ref2-200000-user2
+ EOF
+ git for-each-ref \
+ --format="%(taggerdate:unix) %(taggeremail) %(refname)" \
+ --sort=-refname \
+ --sort=taggeremail \
+ --no-sort \
+ --sort=taggerdate \
+ "refs/tags/multi-*" >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'do not dereference NULL upon %(HEAD) on unborn branch' '
+ test_when_finished "git checkout main" &&
+ git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
+ sed -e "s/^\* / /" actual >expect &&
+ git checkout --orphan orphaned-branch &&
+ git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
+ test_cmp expect actual
+'
+
+cat >trailers <<EOF
+Reviewed-by: A U Thor <author@example.com>
+Signed-off-by: A U Thor <author@example.com>
+[ v2 updated patch description ]
+Acked-by: A U Thor
+ <author@example.com>
+EOF
+
+unfold () {
+ perl -0pe 's/\n\s+/ /g'
+}
+
+test_expect_success 'set up trailers for next test' '
+ echo "Some contents" > two &&
+ git add two &&
+ git commit -F - <<-EOF
+ trailers: this commit message has trailers
+
+ Some message contents
+
+ $(cat trailers)
+ EOF
+'
+
+test_trailer_option () {
+ title=$1 option=$2
+ cat >expect
+ test_expect_success "$title" '
+ git for-each-ref --format="%($option)" refs/heads/main >actual &&
+ test_cmp expect actual &&
+ git for-each-ref --format="%(contents:$option)" refs/heads/main >actual &&
+ test_cmp expect actual
+ '
+}
+
+test_trailer_option '%(trailers:unfold) unfolds trailers' \
+ 'trailers:unfold' <<-EOF
+ $(unfold <trailers)
+
+ EOF
+
+test_trailer_option '%(trailers:only) shows only "key: value" trailers' \
+ 'trailers:only' <<-EOF
+ $(grep -v patch.description <trailers)
+
+ EOF
+
+test_trailer_option '%(trailers:only=no,only=true) shows only "key: value" trailers' \
+ 'trailers:only=no,only=true' <<-EOF
+ $(grep -v patch.description <trailers)
+
+ EOF
+
+test_trailer_option '%(trailers:only=yes) shows only "key: value" trailers' \
+ 'trailers:only=yes' <<-EOF
+ $(grep -v patch.description <trailers)
+
+ EOF
+
+test_trailer_option '%(trailers:only=no) shows all trailers' \
+ 'trailers:only=no' <<-EOF
+ $(cat trailers)
+
+ EOF
+
+test_trailer_option '%(trailers:only) and %(trailers:unfold) work together' \
+ 'trailers:only,unfold' <<-EOF
+ $(grep -v patch.description <trailers | unfold)
+
+ EOF
+
+test_trailer_option '%(trailers:unfold) and %(trailers:only) work together' \
+ 'trailers:unfold,only' <<-EOF
+ $(grep -v patch.description <trailers | unfold)
+
+ EOF
+
+test_trailer_option '%(trailers:key=foo) shows that trailer' \
+ 'trailers:key=Signed-off-by' <<-EOF
+ Signed-off-by: A U Thor <author@example.com>
+
+ EOF
+
+test_trailer_option '%(trailers:key=foo) is case insensitive' \
+ 'trailers:key=SiGned-oFf-bY' <<-EOF
+ Signed-off-by: A U Thor <author@example.com>
+
+ EOF
+
+test_trailer_option '%(trailers:key=foo:) trailing colon also works' \
+ 'trailers:key=Signed-off-by:' <<-EOF
+ Signed-off-by: A U Thor <author@example.com>
+
+ EOF
+
+test_trailer_option '%(trailers:key=foo) multiple keys' \
+ 'trailers:key=Reviewed-by:,key=Signed-off-by' <<-EOF
+ Reviewed-by: A U Thor <author@example.com>
+ Signed-off-by: A U Thor <author@example.com>
+
+ EOF
+
+test_trailer_option '%(trailers:key=nonexistent) becomes empty' \
+ 'trailers:key=Shined-off-by:' <<-EOF
+
+ EOF
+
+test_trailer_option '%(trailers:key=foo) handles multiple lines even if folded' \
+ 'trailers:key=Acked-by' <<-EOF
+ $(grep -v patch.description <trailers | grep -v Signed-off-by | grep -v Reviewed-by)
+
+ EOF
+
+test_trailer_option '%(trailers:key=foo,unfold) properly unfolds' \
+ 'trailers:key=Signed-Off-by,unfold' <<-EOF
+ $(unfold <trailers | grep Signed-off-by)
+
+ EOF
+
+test_trailer_option '%(trailers:key=foo,only=no) also includes nontrailer lines' \
+ 'trailers:key=Signed-off-by,only=no' <<-EOF
+ Signed-off-by: A U Thor <author@example.com>
+ $(grep patch.description <trailers)
+
+ EOF
+
+test_trailer_option '%(trailers:key=foo,valueonly) shows only value' \
+ 'trailers:key=Signed-off-by,valueonly' <<-EOF
+ A U Thor <author@example.com>
+
+ EOF
+
+test_trailer_option '%(trailers:separator) changes separator' \
+ 'trailers:separator=%x2C,key=Reviewed-by,key=Signed-off-by:' <<-EOF
+ Reviewed-by: A U Thor <author@example.com>,Signed-off-by: A U Thor <author@example.com>
+ EOF
+
+test_trailer_option '%(trailers:key_value_separator) changes key-value separator' \
+ 'trailers:key_value_separator=%x2C,key=Reviewed-by,key=Signed-off-by:' <<-EOF
+ Reviewed-by,A U Thor <author@example.com>
+ Signed-off-by,A U Thor <author@example.com>
+
+ EOF
+
+test_trailer_option '%(trailers:separator,key_value_separator) changes both separators' \
+ 'trailers:separator=%x2C,key_value_separator=%x2C,key=Reviewed-by,key=Signed-off-by:' <<-EOF
+ Reviewed-by,A U Thor <author@example.com>,Signed-off-by,A U Thor <author@example.com>
+ EOF
+
+test_failing_trailer_option () {
+ title=$1 option=$2
+ cat >expect
+ test_expect_success "$title" '
+ # error message cannot be checked under i18n
+ test_must_fail git for-each-ref --format="%($option)" refs/heads/main 2>actual &&
+ test_cmp expect actual &&
+ test_must_fail git for-each-ref --format="%(contents:$option)" refs/heads/main 2>actual &&
+ test_cmp expect actual
+ '
+}
+
+test_failing_trailer_option '%(trailers) rejects unknown trailers arguments' \
+ 'trailers:unsupported' <<-\EOF
+ fatal: unknown %(trailers) argument: unsupported
+ EOF
+
+test_failing_trailer_option '%(trailers:key) without value is error' \
+ 'trailers:key' <<-\EOF
+ fatal: expected %(trailers:key=<value>)
+ EOF
+
+test_expect_success 'if arguments, %(contents:trailers) shows error if colon is missing' '
+ cat >expect <<-EOF &&
+ fatal: unrecognized %(contents) argument: trailersonly
+ EOF
+ test_must_fail git for-each-ref --format="%(contents:trailersonly)" 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'basic atom: head contents:trailers' '
+ git for-each-ref --format="%(contents:trailers)" refs/heads/main >actual &&
+ sanitize_pgp <actual >actual.clean &&
+ # git for-each-ref ends with a blank line
+ cat >expect <<-EOF &&
+ $(cat trailers)
+
+ EOF
+ test_cmp expect actual.clean
+'
+
+test_expect_success 'basic atom: rest must fail' '
+ test_must_fail git for-each-ref --format="%(rest)" refs/heads/main
+'
+
+test_expect_success 'HEAD atom does not take arguments' '
+ test_must_fail git for-each-ref --format="%(HEAD:foo)" 2>err &&
+ echo "fatal: %(HEAD) does not take arguments" >expect &&
+ test_cmp expect err
+'
+
+test_expect_success 'subject atom rejects unknown arguments' '
+ test_must_fail git for-each-ref --format="%(subject:foo)" 2>err &&
+ echo "fatal: unrecognized %(subject) argument: foo" >expect &&
+ test_cmp expect err
+'
+
+test_expect_success 'refname atom rejects unknown arguments' '
+ test_must_fail git for-each-ref --format="%(refname:foo)" 2>err &&
+ echo "fatal: unrecognized %(refname) argument: foo" >expect &&
+ test_cmp expect err
+'
+
+test_expect_success 'trailer parsing not fooled by --- line' '
+ git commit --allow-empty -F - <<-\EOF &&
+ this is the subject
+
+ This is the body. The message has a "---" line which would confuse a
+ message+patch parser. But here we know we have only a commit message,
+ so we get it right.
+
+ trailer: wrong
+ ---
+ This is more body.
+
+ trailer: right
+ EOF
+
+ {
+ echo "trailer: right" &&
+ echo
+ } >expect &&
+ git for-each-ref --format="%(trailers)" refs/heads/main >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Add symbolic ref for the following tests' '
+ git symbolic-ref refs/heads/sym refs/heads/main
+'
+
+cat >expected <<EOF
+refs/heads/main
+EOF
+
+test_expect_success 'Verify usage of %(symref) atom' '
+ git for-each-ref --format="%(symref)" refs/heads/sym >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<EOF
+heads/main
+EOF
+
+test_expect_success 'Verify usage of %(symref:short) atom' '
+ git for-each-ref --format="%(symref:short)" refs/heads/sym >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<EOF
+main
+heads/main
+EOF
+
+test_expect_success 'Verify usage of %(symref:lstrip) atom' '
+ git for-each-ref --format="%(symref:lstrip=2)" refs/heads/sym > actual &&
+ git for-each-ref --format="%(symref:lstrip=-2)" refs/heads/sym >> actual &&
+ test_cmp expected actual &&
+
+ git for-each-ref --format="%(symref:strip=2)" refs/heads/sym > actual &&
+ git for-each-ref --format="%(symref:strip=-2)" refs/heads/sym >> actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<EOF
+refs
+refs/heads
+EOF
+
+test_expect_success 'Verify usage of %(symref:rstrip) atom' '
+ git for-each-ref --format="%(symref:rstrip=2)" refs/heads/sym > actual &&
+ git for-each-ref --format="%(symref:rstrip=-2)" refs/heads/sym >> actual &&
+ test_cmp expected actual
+'
+
+test_expect_success ':remotename and :remoteref' '
+ git init remote-tests &&
+ (
+ cd remote-tests &&
+ test_commit initial &&
+ git branch -M main &&
+ git remote add from fifth.coffee:blub &&
+ git config branch.main.remote from &&
+ git config branch.main.merge refs/heads/stable &&
+ git remote add to southridge.audio:repo &&
+ git config remote.to.push "refs/heads/*:refs/heads/pushed/*" &&
+ git config branch.main.pushRemote to &&
+ for pair in "%(upstream)=refs/remotes/from/stable" \
+ "%(upstream:remotename)=from" \
+ "%(upstream:remoteref)=refs/heads/stable" \
+ "%(push)=refs/remotes/to/pushed/main" \
+ "%(push:remotename)=to" \
+ "%(push:remoteref)=refs/heads/pushed/main"
+ do
+ echo "${pair#*=}" >expect &&
+ git for-each-ref --format="${pair%=*}" \
+ refs/heads/main >actual &&
+ test_cmp expect actual || exit 1
+ done &&
+ git branch push-simple &&
+ git config branch.push-simple.pushRemote from &&
+ actual="$(git for-each-ref \
+ --format="%(push:remotename),%(push:remoteref)" \
+ refs/heads/push-simple)" &&
+ test from, = "$actual"
+ )
+'
+
+test_expect_success 'for-each-ref --ignore-case ignores case' '
+ git for-each-ref --format="%(refname)" refs/heads/MAIN >actual &&
+ test_must_be_empty actual &&
+
+ echo refs/heads/main >expect &&
+ git for-each-ref --format="%(refname)" --ignore-case \
+ refs/heads/MAIN >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'for-each-ref --omit-empty works' '
+ git for-each-ref --format="%(refname)" >actual &&
+ test_line_count -gt 1 actual &&
+ git for-each-ref --format="%(if:equals=refs/heads/main)%(refname)%(then)%(refname)%(end)" --omit-empty >actual &&
+ echo refs/heads/main >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'for-each-ref --ignore-case works on multiple sort keys' '
+ # name refs numerically to avoid case-insensitive filesystem conflicts
+ nr=0 &&
+ for email in a A b B
+ do
+ for subject in a A b B
+ do
+ GIT_COMMITTER_EMAIL="$email@example.com" \
+ git tag -m "tag $subject" icase-$(printf %02d $nr) &&
+ nr=$((nr+1))||
+ return 1
+ done
+ done &&
+ git for-each-ref --ignore-case \
+ --format="%(taggeremail) %(subject) %(refname)" \
+ --sort=refname \
+ --sort=subject \
+ --sort=taggeremail \
+ refs/tags/icase-* >actual &&
+ cat >expect <<-\EOF &&
+ <a@example.com> tag a refs/tags/icase-00
+ <a@example.com> tag A refs/tags/icase-01
+ <A@example.com> tag a refs/tags/icase-04
+ <A@example.com> tag A refs/tags/icase-05
+ <a@example.com> tag b refs/tags/icase-02
+ <a@example.com> tag B refs/tags/icase-03
+ <A@example.com> tag b refs/tags/icase-06
+ <A@example.com> tag B refs/tags/icase-07
+ <b@example.com> tag a refs/tags/icase-08
+ <b@example.com> tag A refs/tags/icase-09
+ <B@example.com> tag a refs/tags/icase-12
+ <B@example.com> tag A refs/tags/icase-13
+ <b@example.com> tag b refs/tags/icase-10
+ <b@example.com> tag B refs/tags/icase-11
+ <B@example.com> tag b refs/tags/icase-14
+ <B@example.com> tag B refs/tags/icase-15
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'for-each-ref reports broken tags' '
+ git tag -m "good tag" broken-tag-good HEAD &&
+ git cat-file tag broken-tag-good >good &&
+ sed s/commit/blob/ <good >bad &&
+ bad=$(git hash-object -w -t tag bad) &&
+ git update-ref refs/tags/broken-tag-bad $bad &&
+ test_must_fail git for-each-ref --format="%(*objectname)" \
+ refs/tags/broken-tag-*
+'
+
+test_expect_success 'set up tag with signature and no blank lines' '
+ git tag -F - fake-sig-no-blanks <<-\EOF
+ this is the subject
+ -----BEGIN PGP SIGNATURE-----
+ not a real signature, but we just care about the
+ subject/body parsing. It is important here that
+ there are no blank lines in the signature.
+ -----END PGP SIGNATURE-----
+ EOF
+'
+
+test_atom refs/tags/fake-sig-no-blanks contents:subject 'this is the subject'
+test_atom refs/tags/fake-sig-no-blanks contents:body ''
+test_atom refs/tags/fake-sig-no-blanks contents:signature "$sig"
+
+test_expect_success 'set up tag with CRLF signature' '
+ append_cr <<-\EOF |
+ this is the subject
+ -----BEGIN PGP SIGNATURE-----
+
+ not a real signature, but we just care about
+ the subject/body parsing. It is important here
+ that there is a blank line separating this
+ from the signature header.
+ -----END PGP SIGNATURE-----
+ EOF
+ git tag -F - --cleanup=verbatim fake-sig-crlf
+'
+
+test_atom refs/tags/fake-sig-crlf contents:subject 'this is the subject'
+test_atom refs/tags/fake-sig-crlf contents:body ''
+
+# CRLF is retained in the signature, so we have to pass our expected value
+# through append_cr. But test_atom requires a shell string, which means command
+# substitution, and the shell will strip trailing newlines from the output of
+# the substitution. Hack around it by adding and then removing a dummy line.
+sig_crlf="$(printf "%s" "$sig" | append_cr; echo dummy)"
+sig_crlf=${sig_crlf%dummy}
+test_atom refs/tags/fake-sig-crlf contents:signature "$sig_crlf"
+
+test_expect_success 'git for-each-ref --stdin: empty' '
+ >in &&
+ git for-each-ref --format="%(refname)" --stdin <in >actual &&
+ git for-each-ref --format="%(refname)" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git for-each-ref --stdin: fails if extra args' '
+ >in &&
+ test_must_fail git for-each-ref --format="%(refname)" \
+ --stdin refs/heads/extra <in 2>err &&
+ grep "unknown arguments supplied with --stdin" err
+'
+
+test_expect_success 'git for-each-ref --stdin: matches' '
+ cat >in <<-EOF &&
+ refs/tags/multi*
+ refs/heads/amb*
+ EOF
+
+ cat >expect <<-EOF &&
+ refs/heads/ambiguous
+ refs/tags/multi-ref1-100000-user1
+ refs/tags/multi-ref1-100000-user2
+ refs/tags/multi-ref1-200000-user1
+ refs/tags/multi-ref1-200000-user2
+ refs/tags/multi-ref2-100000-user1
+ refs/tags/multi-ref2-100000-user2
+ refs/tags/multi-ref2-200000-user1
+ refs/tags/multi-ref2-200000-user2
+ refs/tags/multiline
+ EOF
+
+ git for-each-ref --format="%(refname)" --stdin <in >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git for-each-ref with non-existing refs' '
+ cat >in <<-EOF &&
+ refs/heads/this-ref-does-not-exist
+ refs/tags/bogus
+ EOF
+
+ git for-each-ref --format="%(refname)" --stdin <in >actual &&
+ test_must_be_empty actual &&
+
+ xargs git for-each-ref --format="%(refname)" <in >actual &&
+ test_must_be_empty actual
+'
+
+GRADE_FORMAT="%(signature:grade)%0a%(signature:key)%0a%(signature:signer)%0a%(signature:fingerprint)%0a%(signature:primarykeyfingerprint)"
+TRUSTLEVEL_FORMAT="%(signature:trustlevel)%0a%(signature:key)%0a%(signature:signer)%0a%(signature:fingerprint)%0a%(signature:primarykeyfingerprint)"
+
+test_expect_success GPG 'setup for signature atom using gpg' '
+ git checkout -b signed &&
+
+ test_when_finished "test_unconfig commit.gpgSign" &&
+
+ echo "1" >file &&
+ git add file &&
+ test_tick &&
+ git commit -S -m "file: 1" &&
+ git tag first-signed &&
+
+ echo "2" >file &&
+ test_tick &&
+ git commit -a -m "file: 2" &&
+ git tag second-unsigned &&
+
+ git config commit.gpgSign 1 &&
+ echo "3" >file &&
+ test_tick &&
+ git commit -a --no-gpg-sign -m "file: 3" &&
+ git tag third-unsigned &&
+
+ test_tick &&
+ git rebase -f HEAD^^ && git tag second-signed HEAD^ &&
+ git tag third-signed &&
+
+ echo "4" >file &&
+ test_tick &&
+ git commit -a -SB7227189 -m "file: 4" &&
+ git tag fourth-signed &&
+
+ echo "5" >file &&
+ test_tick &&
+ git commit -a --no-gpg-sign -m "file: 5" &&
+ git tag fifth-unsigned &&
+
+ echo "6" >file &&
+ test_tick &&
+ git commit -a --no-gpg-sign -m "file: 6" &&
+
+ test_tick &&
+ git rebase -f HEAD^^ &&
+ git tag fifth-signed HEAD^ &&
+ git tag sixth-signed &&
+
+ echo "7" >file &&
+ test_tick &&
+ git commit -a --no-gpg-sign -m "file: 7" &&
+ git tag seventh-unsigned
+'
+
+test_expect_success GPGSSH 'setup for signature atom using ssh' '
+ test_when_finished "test_unconfig gpg.format user.signingkey" &&
+
+ test_config gpg.format ssh &&
+ test_config user.signingkey "${GPGSSH_KEY_PRIMARY}" &&
+ echo "8" >file &&
+ test_tick &&
+ git add file &&
+ git commit -S -m "file: 8" &&
+ git tag eighth-signed-ssh
+'
+
+test_expect_success GPG2 'bare signature atom' '
+ git verify-commit first-signed 2>expect &&
+ echo >>expect &&
+ git for-each-ref refs/tags/first-signed \
+ --format="%(signature)" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success GPG 'show good signature with custom format' '
+ git verify-commit first-signed &&
+ cat >expect <<-\EOF &&
+ G
+ 13B6F51ECDDE430D
+ C O Mitter <committer@example.com>
+ 73D758744BE721698EC54E8713B6F51ECDDE430D
+ 73D758744BE721698EC54E8713B6F51ECDDE430D
+ EOF
+ git for-each-ref refs/tags/first-signed \
+ --format="$GRADE_FORMAT" >actual &&
+ test_cmp expect actual
+'
+test_expect_success GPGSSH 'show good signature with custom format
+ with ssh' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ FINGERPRINT=$(ssh-keygen -lf "${GPGSSH_KEY_PRIMARY}" | awk "{print \$2;}") &&
+ cat >expect.tmpl <<-\EOF &&
+ G
+ FINGERPRINT
+ principal with number 1
+ FINGERPRINT
+
+ EOF
+ sed "s|FINGERPRINT|$FINGERPRINT|g" expect.tmpl >expect &&
+ git for-each-ref refs/tags/eighth-signed-ssh \
+ --format="$GRADE_FORMAT" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success GPG 'signature atom with grade option and bad signature' '
+ git cat-file commit third-signed >raw &&
+ sed -e "s/^file: 3/file: 3 forged/" raw >forged1 &&
+ FORGED1=$(git hash-object -w -t commit forged1) &&
+ git update-ref refs/tags/third-signed "$FORGED1" &&
+ test_must_fail git verify-commit "$FORGED1" &&
+
+ cat >expect <<-\EOF &&
+ B
+ 13B6F51ECDDE430D
+ C O Mitter <committer@example.com>
+
+
+ EOF
+ git for-each-ref refs/tags/third-signed \
+ --format="$GRADE_FORMAT" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success GPG 'show untrusted signature with custom format' '
+ cat >expect <<-\EOF &&
+ U
+ 65A0EEA02E30CAD7
+ Eris Discordia <discord@example.net>
+ F8364A59E07FFE9F4D63005A65A0EEA02E30CAD7
+ D4BE22311AD3131E5EDA29A461092E85B7227189
+ EOF
+ git for-each-ref refs/tags/fourth-signed \
+ --format="$GRADE_FORMAT" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success GPG 'show untrusted signature with undefined trust level' '
+ cat >expect <<-\EOF &&
+ undefined
+ 65A0EEA02E30CAD7
+ Eris Discordia <discord@example.net>
+ F8364A59E07FFE9F4D63005A65A0EEA02E30CAD7
+ D4BE22311AD3131E5EDA29A461092E85B7227189
+ EOF
+ git for-each-ref refs/tags/fourth-signed \
+ --format="$TRUSTLEVEL_FORMAT" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success GPG 'show untrusted signature with ultimate trust level' '
+ cat >expect <<-\EOF &&
+ ultimate
+ 13B6F51ECDDE430D
+ C O Mitter <committer@example.com>
+ 73D758744BE721698EC54E8713B6F51ECDDE430D
+ 73D758744BE721698EC54E8713B6F51ECDDE430D
+ EOF
+ git for-each-ref refs/tags/sixth-signed \
+ --format="$TRUSTLEVEL_FORMAT" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success GPG 'show unknown signature with custom format' '
+ cat >expect <<-\EOF &&
+ E
+ 13B6F51ECDDE430D
+
+
+
+ EOF
+ GNUPGHOME="$GNUPGHOME_NOT_USED" git for-each-ref \
+ refs/tags/sixth-signed --format="$GRADE_FORMAT" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success GPG 'show lack of signature with custom format' '
+ cat >expect <<-\EOF &&
+ N
+
+
+
+
+ EOF
+ git for-each-ref refs/tags/seventh-unsigned \
+ --format="$GRADE_FORMAT" >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t6301-for-each-ref-errors.sh b/t/t6301-for-each-ref-errors.sh
new file mode 100755
index 0000000..2667dd1
--- /dev/null
+++ b/t/t6301-for-each-ref-errors.sh
@@ -0,0 +1,71 @@
+#!/bin/sh
+
+test_description='for-each-ref errors for broken refs'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+ZEROS=$ZERO_OID
+
+test_expect_success setup '
+ MISSING=$(test_oid deadbeef) &&
+ git commit --allow-empty -m "Initial" &&
+ git tag testtag &&
+ git for-each-ref >full-list &&
+ git for-each-ref --format="%(objectname) %(refname)" >brief-list
+'
+
+test_expect_success 'Broken refs are reported correctly' '
+ r=refs/heads/bogus &&
+ : >.git/$r &&
+ test_when_finished "rm -f .git/$r" &&
+ echo "warning: ignoring broken ref $r" >broken-err &&
+ git for-each-ref >out 2>err &&
+ test_cmp full-list out &&
+ test_cmp broken-err err
+'
+
+test_expect_success 'NULL_SHA1 refs are reported correctly' '
+ r=refs/heads/zeros &&
+ echo $ZEROS >.git/$r &&
+ test_when_finished "rm -f .git/$r" &&
+ echo "warning: ignoring broken ref $r" >zeros-err &&
+ git for-each-ref >out 2>err &&
+ test_cmp full-list out &&
+ test_cmp zeros-err err &&
+ git for-each-ref --format="%(objectname) %(refname)" >brief-out 2>brief-err &&
+ test_cmp brief-list brief-out &&
+ test_cmp zeros-err brief-err
+'
+
+test_expect_success 'Missing objects are reported correctly' '
+ r=refs/heads/missing &&
+ echo $MISSING >.git/$r &&
+ test_when_finished "rm -f .git/$r" &&
+ echo "fatal: missing object $MISSING for $r" >missing-err &&
+ test_must_fail git for-each-ref 2>err &&
+ test_cmp missing-err err &&
+ (
+ cat brief-list &&
+ echo "$MISSING $r"
+ ) | sort -k 2 >missing-brief-expected &&
+ git for-each-ref --format="%(objectname) %(refname)" >brief-out 2>brief-err &&
+ test_cmp missing-brief-expected brief-out &&
+ test_must_be_empty brief-err
+'
+
+test_expect_success 'ahead-behind requires an argument' '
+ test_must_fail git for-each-ref \
+ --format="%(ahead-behind)" 2>err &&
+ echo "fatal: expected format: %(ahead-behind:<committish>)" >expect &&
+ test_cmp expect err
+'
+
+test_expect_success 'missing ahead-behind base' '
+ test_must_fail git for-each-ref \
+ --format="%(ahead-behind:refs/heads/missing)" 2>err &&
+ echo "fatal: failed to find '\''refs/heads/missing'\''" >expect &&
+ test_cmp expect err
+'
+
+test_done
diff --git a/t/t6302-for-each-ref-filter.sh b/t/t6302-for-each-ref-filter.sh
new file mode 100755
index 0000000..af223e4
--- /dev/null
+++ b/t/t6302-for-each-ref-filter.sh
@@ -0,0 +1,479 @@
+#!/bin/sh
+
+test_description='test for-each-refs usage of ref-filter APIs'
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-gpg.sh
+
+test_expect_success 'setup some history and refs' '
+ test_commit one &&
+ git branch -M main &&
+ test_commit two &&
+ test_commit three &&
+ git checkout -b side &&
+ test_commit four &&
+ git tag -m "An annotated tag" annotated-tag &&
+ git tag -m "Annonated doubly" doubly-annotated-tag annotated-tag &&
+
+ # Note that these "signed" tags might not actually be signed.
+ # Tests which care about the distinction should be marked
+ # with the GPG prereq.
+ if test_have_prereq GPG
+ then
+ sign=-s
+ else
+ sign=
+ fi &&
+ git tag $sign -m "A signed tag" signed-tag &&
+ git tag $sign -m "Signed doubly" doubly-signed-tag signed-tag &&
+
+ git checkout main &&
+ git update-ref refs/odd/spot main
+'
+
+test_expect_success 'filtering with --points-at' '
+ cat >expect <<-\EOF &&
+ refs/heads/main
+ refs/odd/spot
+ refs/tags/three
+ EOF
+ git for-each-ref --format="%(refname)" --points-at=main >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'check signed tags with --points-at' '
+ sed -e "s/Z$//" >expect <<-\EOF &&
+ refs/heads/side Z
+ refs/tags/annotated-tag four
+ refs/tags/doubly-annotated-tag An annotated tag
+ refs/tags/doubly-signed-tag A signed tag
+ refs/tags/four Z
+ refs/tags/signed-tag four
+ EOF
+ git for-each-ref --format="%(refname) %(*subject)" --points-at=side >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'filtering with --merged' '
+ cat >expect <<-\EOF &&
+ refs/heads/main
+ refs/odd/spot
+ refs/tags/one
+ refs/tags/three
+ refs/tags/two
+ EOF
+ git for-each-ref --format="%(refname)" --merged=main >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'filtering with --no-merged' '
+ cat >expect <<-\EOF &&
+ refs/heads/side
+ refs/tags/annotated-tag
+ refs/tags/doubly-annotated-tag
+ refs/tags/doubly-signed-tag
+ refs/tags/four
+ refs/tags/signed-tag
+ EOF
+ git for-each-ref --format="%(refname)" --no-merged=main >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'filtering with --contains' '
+ cat >expect <<-\EOF &&
+ refs/heads/main
+ refs/heads/side
+ refs/odd/spot
+ refs/tags/annotated-tag
+ refs/tags/doubly-annotated-tag
+ refs/tags/doubly-signed-tag
+ refs/tags/four
+ refs/tags/signed-tag
+ refs/tags/three
+ refs/tags/two
+ EOF
+ git for-each-ref --format="%(refname)" --contains=two >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'filtering with --no-contains' '
+ cat >expect <<-\EOF &&
+ refs/tags/one
+ EOF
+ git for-each-ref --format="%(refname)" --no-contains=two >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'filtering with --contains and --no-contains' '
+ cat >expect <<-\EOF &&
+ refs/tags/two
+ EOF
+ git for-each-ref --format="%(refname)" --contains=two --no-contains=three >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '%(color) must fail' '
+ test_must_fail git for-each-ref --format="%(color)%(refname)"
+'
+
+test_expect_success '%(color:#aa22ac) must succeed' '
+ test_when_finished rm -rf test &&
+ git init test &&
+ (
+ cd test &&
+ test_commit initial &&
+ git branch -M main &&
+ cat >expect <<-\EOF &&
+ refs/heads/main
+ refs/tags/initial
+ EOF
+ git remote add origin nowhere &&
+ git config branch.main.remote origin &&
+ git config branch.main.merge refs/heads/main &&
+ git for-each-ref --format="%(color:#aa22ac)%(refname)" >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'left alignment is default' '
+ cat >expect <<-\EOF &&
+ refname is refs/heads/main |refs/heads/main
+ refname is refs/heads/side |refs/heads/side
+ refname is refs/odd/spot |refs/odd/spot
+ refname is refs/tags/annotated-tag|refs/tags/annotated-tag
+ refname is refs/tags/doubly-annotated-tag|refs/tags/doubly-annotated-tag
+ refname is refs/tags/doubly-signed-tag|refs/tags/doubly-signed-tag
+ refname is refs/tags/four |refs/tags/four
+ refname is refs/tags/one |refs/tags/one
+ refname is refs/tags/signed-tag|refs/tags/signed-tag
+ refname is refs/tags/three |refs/tags/three
+ refname is refs/tags/two |refs/tags/two
+ EOF
+ git for-each-ref --format="%(align:30)refname is %(refname)%(end)|%(refname)" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'middle alignment' '
+ cat >expect <<-\EOF &&
+ | refname is refs/heads/main |refs/heads/main
+ | refname is refs/heads/side |refs/heads/side
+ | refname is refs/odd/spot |refs/odd/spot
+ |refname is refs/tags/annotated-tag|refs/tags/annotated-tag
+ |refname is refs/tags/doubly-annotated-tag|refs/tags/doubly-annotated-tag
+ |refname is refs/tags/doubly-signed-tag|refs/tags/doubly-signed-tag
+ | refname is refs/tags/four |refs/tags/four
+ | refname is refs/tags/one |refs/tags/one
+ |refname is refs/tags/signed-tag|refs/tags/signed-tag
+ | refname is refs/tags/three |refs/tags/three
+ | refname is refs/tags/two |refs/tags/two
+ EOF
+ git for-each-ref --format="|%(align:middle,30)refname is %(refname)%(end)|%(refname)" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'right alignment' '
+ cat >expect <<-\EOF &&
+ | refname is refs/heads/main|refs/heads/main
+ | refname is refs/heads/side|refs/heads/side
+ | refname is refs/odd/spot|refs/odd/spot
+ |refname is refs/tags/annotated-tag|refs/tags/annotated-tag
+ |refname is refs/tags/doubly-annotated-tag|refs/tags/doubly-annotated-tag
+ |refname is refs/tags/doubly-signed-tag|refs/tags/doubly-signed-tag
+ | refname is refs/tags/four|refs/tags/four
+ | refname is refs/tags/one|refs/tags/one
+ |refname is refs/tags/signed-tag|refs/tags/signed-tag
+ | refname is refs/tags/three|refs/tags/three
+ | refname is refs/tags/two|refs/tags/two
+ EOF
+ git for-each-ref --format="|%(align:30,right)refname is %(refname)%(end)|%(refname)" >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<-\EOF
+| refname is refs/heads/main |refs/heads/main
+| refname is refs/heads/side |refs/heads/side
+| refname is refs/odd/spot |refs/odd/spot
+| refname is refs/tags/annotated-tag |refs/tags/annotated-tag
+|refname is refs/tags/doubly-annotated-tag |refs/tags/doubly-annotated-tag
+| refname is refs/tags/doubly-signed-tag |refs/tags/doubly-signed-tag
+| refname is refs/tags/four |refs/tags/four
+| refname is refs/tags/one |refs/tags/one
+| refname is refs/tags/signed-tag |refs/tags/signed-tag
+| refname is refs/tags/three |refs/tags/three
+| refname is refs/tags/two |refs/tags/two
+EOF
+
+test_align_permutations() {
+ while read -r option
+ do
+ test_expect_success "align:$option" '
+ git for-each-ref --format="|%(align:$option)refname is %(refname)%(end)|%(refname)" >actual &&
+ test_cmp expect actual
+ '
+ done
+}
+
+test_align_permutations <<-\EOF
+ middle,42
+ 42,middle
+ position=middle,42
+ 42,position=middle
+ middle,width=42
+ width=42,middle
+ position=middle,width=42
+ width=42,position=middle
+EOF
+
+# Last one wins (silently) when multiple arguments of the same type are given
+
+test_align_permutations <<-\EOF
+ 32,width=42,middle
+ width=30,42,middle
+ width=42,position=right,middle
+ 42,right,position=middle
+EOF
+
+# Individual atoms inside %(align:...) and %(end) must not be quoted.
+
+test_expect_success 'alignment with format quote' "
+ cat >expect <<-\EOF &&
+ |' '\''main| A U Thor'\'' '|
+ |' '\''side| A U Thor'\'' '|
+ |' '\''odd/spot| A U Thor'\'' '|
+ |' '\''annotated-tag| '\'' '|
+ |' '\''doubly-annotated-tag| '\'' '|
+ |' '\''doubly-signed-tag| '\'' '|
+ |' '\''four| A U Thor'\'' '|
+ |' '\''one| A U Thor'\'' '|
+ |' '\''signed-tag| '\'' '|
+ |' '\''three| A U Thor'\'' '|
+ |' '\''two| A U Thor'\'' '|
+ EOF
+ git for-each-ref --shell --format=\"|%(align:30,middle)'%(refname:short)| %(authorname)'%(end)|\" >actual &&
+ test_cmp expect actual
+"
+
+test_expect_success 'nested alignment with quote formatting' "
+ cat >expect <<-\EOF &&
+ |' main '|
+ |' side '|
+ |' odd/spot '|
+ |' annotated-tag '|
+ |'doubly-annotated-tag '|
+ |'doubly-signed-tag '|
+ |' four '|
+ |' one '|
+ |' signed-tag '|
+ |' three '|
+ |' two '|
+ EOF
+ git for-each-ref --shell --format='|%(align:30,left)%(align:15,right)%(refname:short)%(end)%(end)|' >actual &&
+ test_cmp expect actual
+"
+
+test_expect_success 'check `%(contents:lines=1)`' '
+ cat >expect <<-\EOF &&
+ main |three
+ side |four
+ odd/spot |three
+ annotated-tag |An annotated tag
+ doubly-annotated-tag |Annonated doubly
+ doubly-signed-tag |Signed doubly
+ four |four
+ one |one
+ signed-tag |A signed tag
+ three |three
+ two |two
+ EOF
+ git for-each-ref --format="%(refname:short) |%(contents:lines=1)" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'check `%(contents:lines=0)`' '
+ cat >expect <<-\EOF &&
+ main |
+ side |
+ odd/spot |
+ annotated-tag |
+ doubly-annotated-tag |
+ doubly-signed-tag |
+ four |
+ one |
+ signed-tag |
+ three |
+ two |
+ EOF
+ git for-each-ref --format="%(refname:short) |%(contents:lines=0)" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'check `%(contents:lines=99999)`' '
+ cat >expect <<-\EOF &&
+ main |three
+ side |four
+ odd/spot |three
+ annotated-tag |An annotated tag
+ doubly-annotated-tag |Annonated doubly
+ doubly-signed-tag |Signed doubly
+ four |four
+ one |one
+ signed-tag |A signed tag
+ three |three
+ two |two
+ EOF
+ git for-each-ref --format="%(refname:short) |%(contents:lines=99999)" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '`%(contents:lines=-1)` should fail' '
+ test_must_fail git for-each-ref --format="%(refname:short) |%(contents:lines=-1)"
+'
+
+test_expect_success 'setup for version sort' '
+ test_commit foo1.3 &&
+ test_commit foo1.6 &&
+ test_commit foo1.10
+'
+
+test_expect_success 'version sort' '
+ git for-each-ref --sort=version:refname --format="%(refname:short)" refs/tags/ | grep "foo" >actual &&
+ cat >expect <<-\EOF &&
+ foo1.3
+ foo1.6
+ foo1.10
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'version sort (shortened)' '
+ git for-each-ref --sort=v:refname --format="%(refname:short)" refs/tags/ | grep "foo" >actual &&
+ cat >expect <<-\EOF &&
+ foo1.3
+ foo1.6
+ foo1.10
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'reverse version sort' '
+ git for-each-ref --sort=-version:refname --format="%(refname:short)" refs/tags/ | grep "foo" >actual &&
+ cat >expect <<-\EOF &&
+ foo1.10
+ foo1.6
+ foo1.3
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'improper usage of %(if), %(then), %(else) and %(end) atoms' '
+ test_must_fail git for-each-ref --format="%(if)" &&
+ test_must_fail git for-each-ref --format="%(then) %(end)" &&
+ test_must_fail git for-each-ref --format="%(else) %(end)" &&
+ test_must_fail git for-each-ref --format="%(if) %(else) %(end)" &&
+ test_must_fail git for-each-ref --format="%(if) %(then) %(then) %(end)" &&
+ test_must_fail git for-each-ref --format="%(then) %(else) %(end)" &&
+ test_must_fail git for-each-ref --format="%(if) %(else) %(end)" &&
+ test_must_fail git for-each-ref --format="%(if) %(then) %(else)" &&
+ test_must_fail git for-each-ref --format="%(if) %(else) %(then) %(end)" &&
+ test_must_fail git for-each-ref --format="%(if) %(then) %(else) %(else) %(end)" &&
+ test_must_fail git for-each-ref --format="%(if) %(end)"
+'
+
+test_expect_success 'check %(if)...%(then)...%(end) atoms' '
+ git for-each-ref --format="%(refname)%(if)%(authorname)%(then) Author: %(authorname)%(end)" >actual &&
+ cat >expect <<-\EOF &&
+ refs/heads/main Author: A U Thor
+ refs/heads/side Author: A U Thor
+ refs/odd/spot Author: A U Thor
+ refs/tags/annotated-tag
+ refs/tags/doubly-annotated-tag
+ refs/tags/doubly-signed-tag
+ refs/tags/foo1.10 Author: A U Thor
+ refs/tags/foo1.3 Author: A U Thor
+ refs/tags/foo1.6 Author: A U Thor
+ refs/tags/four Author: A U Thor
+ refs/tags/one Author: A U Thor
+ refs/tags/signed-tag
+ refs/tags/three Author: A U Thor
+ refs/tags/two Author: A U Thor
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'check %(if)...%(then)...%(else)...%(end) atoms' '
+ git for-each-ref --format="%(if)%(authorname)%(then)%(authorname)%(else)No author%(end): %(refname)" >actual &&
+ cat >expect <<-\EOF &&
+ A U Thor: refs/heads/main
+ A U Thor: refs/heads/side
+ A U Thor: refs/odd/spot
+ No author: refs/tags/annotated-tag
+ No author: refs/tags/doubly-annotated-tag
+ No author: refs/tags/doubly-signed-tag
+ A U Thor: refs/tags/foo1.10
+ A U Thor: refs/tags/foo1.3
+ A U Thor: refs/tags/foo1.6
+ A U Thor: refs/tags/four
+ A U Thor: refs/tags/one
+ No author: refs/tags/signed-tag
+ A U Thor: refs/tags/three
+ A U Thor: refs/tags/two
+ EOF
+ test_cmp expect actual
+'
+test_expect_success 'ignore spaces in %(if) atom usage' '
+ git for-each-ref --format="%(refname:short): %(if)%(HEAD)%(then)Head ref%(else)Not Head ref%(end)" >actual &&
+ cat >expect <<-\EOF &&
+ main: Head ref
+ side: Not Head ref
+ odd/spot: Not Head ref
+ annotated-tag: Not Head ref
+ doubly-annotated-tag: Not Head ref
+ doubly-signed-tag: Not Head ref
+ foo1.10: Not Head ref
+ foo1.3: Not Head ref
+ foo1.6: Not Head ref
+ four: Not Head ref
+ one: Not Head ref
+ signed-tag: Not Head ref
+ three: Not Head ref
+ two: Not Head ref
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'check %(if:equals=<string>)' '
+ git for-each-ref --format="%(if:equals=main)%(refname:short)%(then)Found main%(else)Not main%(end)" refs/heads/ >actual &&
+ cat >expect <<-\EOF &&
+ Found main
+ Not main
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'check %(if:notequals=<string>)' '
+ git for-each-ref --format="%(if:notequals=main)%(refname:short)%(then)Not main%(else)Found main%(end)" refs/heads/ >actual &&
+ cat >expect <<-\EOF &&
+ Found main
+ Not main
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success '--merged is compatible with --no-merged' '
+ git for-each-ref --merged HEAD --no-merged HEAD
+'
+
+test_expect_success 'validate worktree atom' '
+ cat >expect <<-EOF &&
+ main: $(pwd)
+ main_worktree: $(pwd)/worktree_dir
+ side: not checked out
+ EOF
+ git worktree add -b main_worktree worktree_dir main &&
+ git for-each-ref --format="%(refname:short): %(if)%(worktreepath)%(then)%(worktreepath)%(else)not checked out%(end)" refs/heads/ >actual &&
+ rm -r worktree_dir &&
+ git worktree prune &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t6400-merge-df.sh b/t/t6400-merge-df.sh
new file mode 100755
index 0000000..3de4ef6
--- /dev/null
+++ b/t/t6400-merge-df.sh
@@ -0,0 +1,163 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Fredrik Kuivinen
+#
+
+test_description='Test merge with directory/file conflicts'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success 'prepare repository' '
+ echo Hello >init &&
+ git add init &&
+ git commit -m initial &&
+
+ git branch B &&
+ mkdir dir &&
+ echo foo >dir/foo &&
+ git add dir/foo &&
+ git commit -m "File: dir/foo" &&
+
+ git checkout B &&
+ echo file dir >dir &&
+ git add dir &&
+ git commit -m "File: dir"
+'
+
+test_expect_success 'Merge with d/f conflicts' '
+ test_expect_code 1 git merge -m "merge msg" main
+'
+
+test_expect_success 'F/D conflict' '
+ git reset --hard &&
+ git checkout main &&
+ rm .git/index &&
+
+ mkdir before &&
+ echo FILE >before/one &&
+ echo FILE >after &&
+ git add . &&
+ git commit -m first &&
+
+ rm -f after &&
+ git mv before after &&
+ git commit -m move &&
+
+ git checkout -b para HEAD^ &&
+ echo COMPLETELY ANOTHER FILE >another &&
+ git add . &&
+ git commit -m para &&
+
+ git merge main
+'
+
+test_expect_success 'setup modify/delete + directory/file conflict' '
+ git checkout --orphan modify &&
+ git rm -rf . &&
+ git clean -fdqx &&
+
+ printf "a\nb\nc\nd\ne\nf\ng\nh\n" >letters &&
+ git add letters &&
+ git commit -m initial &&
+
+ # Throw in letters.txt for sorting order fun
+ # ("letters.txt" sorts between "letters" and "letters/file")
+ echo i >>letters &&
+ echo "version 2" >letters.txt &&
+ git add letters letters.txt &&
+ git commit -m modified &&
+
+ git checkout -b delete HEAD^ &&
+ git rm letters &&
+ mkdir letters &&
+ >letters/file &&
+ echo "version 1" >letters.txt &&
+ git add letters letters.txt &&
+ git commit -m deleted
+'
+
+test_expect_success 'modify/delete + directory/file conflict' '
+ git checkout delete^0 &&
+ test_must_fail git merge modify &&
+
+ test_stdout_line_count = 5 git ls-files -s &&
+ test_stdout_line_count = 4 git ls-files -u &&
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ test_stdout_line_count = 0 git ls-files -o
+ else
+ test_stdout_line_count = 1 git ls-files -o
+ fi &&
+
+ test_path_is_file letters/file &&
+ test_path_is_file letters.txt &&
+ test_path_is_file letters~modify
+'
+
+test_expect_success 'modify/delete + directory/file conflict; other way' '
+ git reset --hard &&
+ git clean -f &&
+ git checkout modify^0 &&
+
+ test_must_fail git merge delete &&
+
+ test_stdout_line_count = 5 git ls-files -s &&
+ test_stdout_line_count = 4 git ls-files -u &&
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ test_stdout_line_count = 0 git ls-files -o
+ else
+ test_stdout_line_count = 1 git ls-files -o
+ fi &&
+
+ test_path_is_file letters/file &&
+ test_path_is_file letters.txt &&
+ test_path_is_file letters~HEAD
+'
+
+test_expect_success 'Simple merge in repo with interesting pathnames' '
+ # Simple lexicographic ordering of files and directories would be:
+ # foo
+ # foo/bar
+ # foo/bar-2
+ # foo/bar/baz
+ # foo/bar-2/baz
+ # The fact that foo/bar-2 appears between foo/bar and foo/bar/baz
+ # can trip up some codepaths, and is the point of this test.
+ git init name-ordering &&
+ (
+ cd name-ordering &&
+
+ mkdir -p foo/bar &&
+ mkdir -p foo/bar-2 &&
+ >foo/bar/baz &&
+ >foo/bar-2/baz &&
+ git add . &&
+ git commit -m initial &&
+
+ git branch topic &&
+ git branch other &&
+
+ git checkout other &&
+ echo other >foo/bar-2/baz &&
+ git add -u &&
+ git commit -m other &&
+
+ git checkout topic &&
+ echo topic >foo/bar/baz &&
+ git add -u &&
+ git commit -m topic &&
+
+ git merge other &&
+ git ls-files -s >out &&
+ test_line_count = 2 out &&
+ git rev-parse :0:foo/bar/baz :0:foo/bar-2/baz >actual &&
+ git rev-parse HEAD~1:foo/bar/baz other:foo/bar-2/baz >expect &&
+ test_cmp expect actual
+ )
+
+'
+
+test_done
diff --git a/t/t6401-merge-criss-cross.sh b/t/t6401-merge-criss-cross.sh
new file mode 100755
index 0000000..1962310
--- /dev/null
+++ b/t/t6401-merge-criss-cross.sh
@@ -0,0 +1,69 @@
+#!/bin/sh
+#
+# Copyright (c) 2005 Fredrik Kuivinen
+#
+
+# See https://lore.kernel.org/git/Pine.LNX.4.44.0504271254120.4678-100000@wax.eds.org/ for a
+# nice description of what this is about.
+
+
+test_description='Test criss-cross merge'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'prepare repository' '
+ test_write_lines 1 2 3 4 5 6 7 8 9 >file &&
+ git add file &&
+ git commit -m "Initial commit" file &&
+
+ git branch A &&
+ git branch B &&
+ git checkout A &&
+
+ test_write_lines 1 2 3 4 5 6 7 "8 changed in B8, branch A" 9 >file &&
+ git commit -m "B8" file &&
+ git checkout B &&
+
+ test_write_lines 1 2 "3 changed in C3, branch B" 4 5 6 7 8 9 >file &&
+ git commit -m "C3" file &&
+ git branch C3 &&
+
+ git merge -m "pre E3 merge" A &&
+
+ test_write_lines 1 2 "3 changed in E3, branch B. New file size" 4 5 6 7 "8 changed in B8, branch A" 9 >file &&
+ git commit -m "E3" file &&
+
+ git checkout A &&
+ git merge -m "pre D8 merge" C3 &&
+ test_write_lines 1 2 "3 changed in C3, branch B" 4 5 6 7 "8 changed in D8, branch A. New file size 2" 9 >file &&
+
+ git commit -m D8 file
+'
+
+test_expect_success 'Criss-cross merge' '
+ git merge -m "final merge" B
+'
+
+test_expect_success 'Criss-cross merge result' '
+ cat <<-\EOF >file-expect &&
+ 1
+ 2
+ 3 changed in E3, branch B. New file size
+ 4
+ 5
+ 6
+ 7
+ 8 changed in D8, branch A. New file size 2
+ 9
+ EOF
+
+ test_cmp file-expect file
+'
+
+test_expect_success 'Criss-cross merge fails (-s resolve)' '
+ git reset --hard A^ &&
+ test_must_fail git merge -s resolve -m "final merge" B
+'
+
+test_done
diff --git a/t/t6402-merge-rename.sh b/t/t6402-merge-rename.sh
new file mode 100755
index 0000000..2738b50
--- /dev/null
+++ b/t/t6402-merge-rename.sh
@@ -0,0 +1,973 @@
+#!/bin/sh
+
+test_description='Merge-recursive merging renames'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+modify () {
+ sed -e "$1" <"$2" >"$2.x" &&
+ mv "$2.x" "$2"
+}
+
+test_expect_success 'setup' '
+ cat >A <<-\EOF &&
+ a aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+ b bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
+ c cccccccccccccccccccccccccccccccccccccccccccccccc
+ d dddddddddddddddddddddddddddddddddddddddddddddddd
+ e eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
+ f ffffffffffffffffffffffffffffffffffffffffffffffff
+ g gggggggggggggggggggggggggggggggggggggggggggggggg
+ h hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
+ i iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
+ j jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
+ k kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
+ l llllllllllllllllllllllllllllllllllllllllllllllll
+ m mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
+ n nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
+ o oooooooooooooooooooooooooooooooooooooooooooooooo
+ EOF
+
+ cat >M <<-\EOF &&
+ A AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ B BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
+ C CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
+ D DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
+ E EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
+ F FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
+ G GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG
+ H HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
+ I IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
+ J JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ
+ K KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK
+ L LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL
+ M MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
+ N NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
+ O OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
+ EOF
+
+ git add A M &&
+ git commit -m "initial has A and M" &&
+ git branch white &&
+ git branch red &&
+ git branch blue &&
+ git branch yellow &&
+ git branch change &&
+ git branch change+rename &&
+
+ sed -e "/^g /s/.*/g : main changes a line/" <A >A+ &&
+ mv A+ A &&
+ git commit -a -m "main updates A" &&
+
+ git checkout yellow &&
+ rm -f M &&
+ git commit -a -m "yellow removes M" &&
+
+ git checkout white &&
+ sed -e "/^g /s/.*/g : white changes a line/" <A >B &&
+ sed -e "/^G /s/.*/G : colored branch changes a line/" <M >N &&
+ rm -f A M &&
+ git update-index --add --remove A B M N &&
+ git commit -m "white renames A->B, M->N" &&
+
+ git checkout red &&
+ sed -e "/^g /s/.*/g : red changes a line/" <A >B &&
+ sed -e "/^G /s/.*/G : colored branch changes a line/" <M >N &&
+ rm -f A M &&
+ git update-index --add --remove A B M N &&
+ git commit -m "red renames A->B, M->N" &&
+
+ git checkout blue &&
+ sed -e "/^g /s/.*/g : blue changes a line/" <A >C &&
+ sed -e "/^G /s/.*/G : colored branch changes a line/" <M >N &&
+ rm -f A M &&
+ git update-index --add --remove A C M N &&
+ git commit -m "blue renames A->C, M->N" &&
+
+ git checkout change &&
+ sed -e "/^g /s/.*/g : changed line/" <A >A+ &&
+ mv A+ A &&
+ git commit -q -a -m "changed" &&
+
+ git checkout change+rename &&
+ sed -e "/^g /s/.*/g : changed line/" <A >B &&
+ rm A &&
+ git update-index --add B &&
+ git commit -q -a -m "changed and renamed" &&
+
+ git checkout main
+'
+
+test_expect_success 'pull renaming branch into unrenaming one' \
+'
+ git show-branch &&
+ test_expect_code 1 git pull --no-rebase . white &&
+ git ls-files -s &&
+ test_stdout_line_count = 3 git ls-files -u B &&
+ test_stdout_line_count = 1 git ls-files -s N &&
+ sed -ne "/^g/{
+ p
+ q
+ }" B | grep main &&
+ git diff --exit-code white N
+'
+
+test_expect_success 'pull renaming branch into another renaming one' \
+'
+ rm -f B &&
+ git reset --hard &&
+ git checkout red &&
+ test_expect_code 1 git pull --no-rebase . white &&
+ test_stdout_line_count = 3 git ls-files -u B &&
+ test_stdout_line_count = 1 git ls-files -s N &&
+ sed -ne "/^g/{
+ p
+ q
+ }" B | grep red &&
+ git diff --exit-code white N
+'
+
+test_expect_success 'pull unrenaming branch into renaming one' \
+'
+ git reset --hard &&
+ git show-branch &&
+ test_expect_code 1 git pull --no-rebase . main &&
+ test_stdout_line_count = 3 git ls-files -u B &&
+ test_stdout_line_count = 1 git ls-files -s N &&
+ sed -ne "/^g/{
+ p
+ q
+ }" B | grep red &&
+ git diff --exit-code white N
+'
+
+test_expect_success 'pull conflicting renames' \
+'
+ git reset --hard &&
+ git show-branch &&
+ test_expect_code 1 git pull --no-rebase . blue &&
+ test_stdout_line_count = 1 git ls-files -u A &&
+ test_stdout_line_count = 1 git ls-files -u B &&
+ test_stdout_line_count = 1 git ls-files -u C &&
+ test_stdout_line_count = 1 git ls-files -s N &&
+ sed -ne "/^g/{
+ p
+ q
+ }" B | grep red &&
+ git diff --exit-code white N
+'
+
+test_expect_success 'interference with untracked working tree file' '
+ git reset --hard &&
+ git show-branch &&
+ echo >A this file should not matter &&
+ test_expect_code 1 git pull --no-rebase . white &&
+ test_path_is_file A
+'
+
+test_expect_success 'interference with untracked working tree file' '
+ git reset --hard &&
+ git checkout white &&
+ git show-branch &&
+ rm -f A &&
+ echo >A this file should not matter &&
+ test_expect_code 1 git pull --no-rebase . red &&
+ test_path_is_file A
+'
+
+test_expect_success 'interference with untracked working tree file' '
+ git reset --hard &&
+ rm -f A M &&
+ git checkout -f main &&
+ git tag -f anchor &&
+ git show-branch &&
+ git pull --no-rebase . yellow &&
+ test_path_is_missing M &&
+ git reset --hard anchor
+'
+
+test_expect_success 'updated working tree file should prevent the merge' '
+ git reset --hard &&
+ rm -f A M &&
+ git checkout -f main &&
+ git tag -f anchor &&
+ git show-branch &&
+ echo >>M one line addition &&
+ cat M >M.saved &&
+ test_expect_code 128 git pull . yellow &&
+ test_cmp M M.saved &&
+ rm -f M.saved
+'
+
+test_expect_success 'updated working tree file should prevent the merge' '
+ git reset --hard &&
+ rm -f A M &&
+ git checkout -f main &&
+ git tag -f anchor &&
+ git show-branch &&
+ echo >>M one line addition &&
+ cat M >M.saved &&
+ git update-index M &&
+ test_expect_code 2 git pull --no-rebase . yellow &&
+ test_cmp M M.saved &&
+ rm -f M.saved
+'
+
+test_expect_success 'interference with untracked working tree file' '
+ git reset --hard &&
+ rm -f A M &&
+ git checkout -f yellow &&
+ git tag -f anchor &&
+ git show-branch &&
+ echo >M this file should not matter &&
+ git pull --no-rebase . main &&
+ test_path_is_file M &&
+ ! {
+ git ls-files -s |
+ grep M
+ } &&
+ git reset --hard anchor
+'
+
+test_expect_success 'merge of identical changes in a renamed file' '
+ rm -f A M N &&
+ git reset --hard &&
+ git checkout change+rename &&
+
+ test-tool chmtime --get -3600 B >old-mtime &&
+ GIT_MERGE_VERBOSITY=3 git merge change >out &&
+
+ test-tool chmtime --get B >new-mtime &&
+ test_cmp old-mtime new-mtime &&
+
+ git reset --hard HEAD^ &&
+ git checkout change &&
+
+ # A will be renamed to B; we check mtimes and file presence
+ test_path_is_missing B &&
+ test-tool chmtime --get -3600 A >old-mtime &&
+ GIT_MERGE_VERBOSITY=3 git merge change+rename >out &&
+
+ test_path_is_missing A &&
+ test-tool chmtime --get B >new-mtime &&
+ test $(cat old-mtime) -lt $(cat new-mtime)
+'
+
+test_expect_success 'setup for rename + d/f conflicts' '
+ git reset --hard &&
+ git checkout --orphan dir-in-way &&
+ git rm -rf . &&
+ git clean -fdqx &&
+
+ mkdir sub &&
+ mkdir dir &&
+ printf "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" >sub/file &&
+ echo foo >dir/file-in-the-way &&
+ git add -A &&
+ git commit -m "Common commit" &&
+
+ echo 11 >>sub/file &&
+ echo more >>dir/file-in-the-way &&
+ git add -u &&
+ git commit -m "Commit to merge, with dir in the way" &&
+
+ git checkout -b dir-not-in-way &&
+ git reset --soft HEAD^ &&
+ git rm -rf dir &&
+ git commit -m "Commit to merge, with dir removed" -- dir sub/file &&
+
+ git checkout -b renamed-file-has-no-conflicts dir-in-way~1 &&
+ git rm -rf dir &&
+ git rm sub/file &&
+ printf "1\n2\n3\n4\n5555\n6\n7\n8\n9\n10\n" >dir &&
+ git add dir &&
+ git commit -m "Independent change" &&
+
+ git checkout -b renamed-file-has-conflicts dir-in-way~1 &&
+ git rm -rf dir &&
+ git mv sub/file dir &&
+ echo 12 >>dir &&
+ git add dir &&
+ git commit -m "Conflicting change"
+'
+
+test_expect_success 'Rename+D/F conflict; renamed file merges + dir not in way' '
+ git reset --hard &&
+ git checkout -q renamed-file-has-no-conflicts^0 &&
+
+ git merge --strategy=recursive dir-not-in-way &&
+
+ git diff --quiet &&
+ test_path_is_file dir &&
+ test_write_lines 1 2 3 4 5555 6 7 8 9 10 11 >expected &&
+ test_cmp expected dir
+'
+
+test_expect_success 'Rename+D/F conflict; renamed file merges but dir in way' '
+ git reset --hard &&
+ rm -rf dir~* &&
+ git checkout -q renamed-file-has-no-conflicts^0 &&
+ test_must_fail git merge --strategy=recursive dir-in-way >output &&
+
+ test_grep "CONFLICT (modify/delete): dir/file-in-the-way" output &&
+ test_grep "Auto-merging dir" output &&
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ test_grep "moving it to dir~HEAD instead" output
+ else
+ test_grep "Adding as dir~HEAD instead" output
+ fi &&
+
+ test_stdout_line_count = 3 git ls-files -u &&
+ test_stdout_line_count = 2 git ls-files -u dir/file-in-the-way &&
+
+ test_must_fail git diff --quiet &&
+ test_must_fail git diff --cached --quiet &&
+
+ test_path_is_file dir/file-in-the-way &&
+ test_path_is_file dir~HEAD &&
+ test_cmp expected dir~HEAD
+'
+
+test_expect_success 'Same as previous, but merged other way' '
+ git reset --hard &&
+ rm -rf dir~* &&
+ git checkout -q dir-in-way^0 &&
+ test_must_fail git merge --strategy=recursive renamed-file-has-no-conflicts >output 2>errors &&
+
+ ! grep "error: refusing to lose untracked file at" errors &&
+ test_grep "CONFLICT (modify/delete): dir/file-in-the-way" output &&
+ test_grep "Auto-merging dir" output &&
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ test_grep "moving it to dir~renamed-file-has-no-conflicts instead" output
+ else
+ test_grep "Adding as dir~renamed-file-has-no-conflicts instead" output
+ fi &&
+
+ test_stdout_line_count = 3 git ls-files -u &&
+ test_stdout_line_count = 2 git ls-files -u dir/file-in-the-way &&
+
+ test_must_fail git diff --quiet &&
+ test_must_fail git diff --cached --quiet &&
+
+ test_path_is_file dir/file-in-the-way &&
+ test_path_is_file dir~renamed-file-has-no-conflicts &&
+ test_cmp expected dir~renamed-file-has-no-conflicts
+'
+
+test_expect_success 'Rename+D/F conflict; renamed file cannot merge, dir not in way' '
+ git reset --hard &&
+ rm -rf dir~* &&
+ git checkout -q renamed-file-has-conflicts^0 &&
+ test_must_fail git merge --strategy=recursive dir-not-in-way &&
+
+ test_stdout_line_count = 3 git ls-files -u &&
+ test_stdout_line_count = 3 git ls-files -u dir &&
+
+ test_must_fail git diff --quiet &&
+ test_must_fail git diff --cached --quiet &&
+
+ test_path_is_file dir &&
+ cat >expected <<-\EOF &&
+ 1
+ 2
+ 3
+ 4
+ 5
+ 6
+ 7
+ 8
+ 9
+ 10
+ <<<<<<< HEAD:dir
+ 12
+ =======
+ 11
+ >>>>>>> dir-not-in-way:sub/file
+ EOF
+ test_cmp expected dir
+'
+
+test_expect_success 'Rename+D/F conflict; renamed file cannot merge and dir in the way' '
+ modify s/dir-not-in-way/dir-in-way/ expected &&
+
+ git reset --hard &&
+ rm -rf dir~* &&
+ git checkout -q renamed-file-has-conflicts^0 &&
+ test_must_fail git merge --strategy=recursive dir-in-way &&
+
+ test_stdout_line_count = 5 git ls-files -u &&
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ test_stdout_line_count = 3 git ls-files -u dir~HEAD
+ else
+ git ls-files -u dir >out &&
+ test 3 -eq $(grep -v file-in-the-way out | wc -l) &&
+ rm -f out
+ fi &&
+ test_stdout_line_count = 2 git ls-files -u dir/file-in-the-way &&
+
+ test_must_fail git diff --quiet &&
+ test_must_fail git diff --cached --quiet &&
+
+ test_path_is_file dir/file-in-the-way &&
+ test_path_is_file dir~HEAD &&
+ test_cmp expected dir~HEAD
+'
+
+test_expect_success 'Same as previous, but merged other way' '
+ git reset --hard &&
+ rm -rf dir~* &&
+ git checkout -q dir-in-way^0 &&
+ test_must_fail git merge --strategy=recursive renamed-file-has-conflicts &&
+
+ test_stdout_line_count = 5 git ls-files -u &&
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ test_stdout_line_count = 3 git ls-files -u dir~renamed-file-has-conflicts
+ else
+ git ls-files -u dir >out &&
+ test 3 -eq $(grep -v file-in-the-way out | wc -l) &&
+ rm -f out
+ fi &&
+ test_stdout_line_count = 2 git ls-files -u dir/file-in-the-way &&
+
+ test_must_fail git diff --quiet &&
+ test_must_fail git diff --cached --quiet &&
+
+ test_path_is_file dir/file-in-the-way &&
+ test_path_is_file dir~renamed-file-has-conflicts &&
+ cat >expected <<-\EOF &&
+ 1
+ 2
+ 3
+ 4
+ 5
+ 6
+ 7
+ 8
+ 9
+ 10
+ <<<<<<< HEAD:sub/file
+ 11
+ =======
+ 12
+ >>>>>>> renamed-file-has-conflicts:dir
+ EOF
+ test_cmp expected dir~renamed-file-has-conflicts
+'
+
+test_expect_success 'setup both rename source and destination involved in D/F conflict' '
+ git reset --hard &&
+ git checkout --orphan rename-dest &&
+ git rm -rf . &&
+ git clean -fdqx &&
+
+ mkdir one &&
+ echo stuff >one/file &&
+ git add -A &&
+ git commit -m "Common commit" &&
+
+ git mv one/file destdir &&
+ git commit -m "Renamed to destdir" &&
+
+ git checkout -b source-conflict HEAD~1 &&
+ git rm -rf one &&
+ mkdir destdir &&
+ touch one destdir/foo &&
+ git add -A &&
+ git commit -m "Conflicts in the way"
+'
+
+test_expect_success 'both rename source and destination involved in D/F conflict' '
+ git reset --hard &&
+ rm -rf dir~* &&
+ git checkout -q rename-dest^0 &&
+ test_must_fail git merge --strategy=recursive source-conflict &&
+
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ test_stdout_line_count = 2 git ls-files -u
+ else
+ test_stdout_line_count = 1 git ls-files -u
+ fi &&
+
+ test_must_fail git diff --quiet &&
+
+ test_path_is_file destdir/foo &&
+ test_path_is_file one &&
+ test_path_is_file destdir~HEAD &&
+ test "stuff" = "$(cat destdir~HEAD)"
+'
+
+test_expect_success 'setup pair rename to parent of other (D/F conflicts)' '
+ git reset --hard &&
+ git checkout --orphan rename-two &&
+ git rm -rf . &&
+ git clean -fdqx &&
+
+ mkdir one &&
+ mkdir two &&
+ echo stuff >one/file &&
+ echo other >two/file &&
+ git add -A &&
+ git commit -m "Common commit" &&
+
+ git rm -rf one &&
+ git mv two/file one &&
+ git commit -m "Rename two/file -> one" &&
+
+ git checkout -b rename-one HEAD~1 &&
+ git rm -rf two &&
+ git mv one/file two &&
+ rm -r one &&
+ git commit -m "Rename one/file -> two"
+'
+
+if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+then
+ test_expect_success 'pair rename to parent of other (D/F conflicts) w/ untracked dir' '
+ git checkout -q rename-one^0 &&
+ mkdir one &&
+ test_must_fail git merge --strategy=recursive rename-two &&
+
+ test_stdout_line_count = 4 git ls-files -u &&
+ test_stdout_line_count = 2 git ls-files -u one &&
+ test_stdout_line_count = 2 git ls-files -u two &&
+
+ test_must_fail git diff --quiet &&
+
+ test 3 -eq $(find . | grep -v .git | wc -l) &&
+
+ test_path_is_file one &&
+ test_path_is_file two &&
+ test "other" = $(cat one) &&
+ test "stuff" = $(cat two)
+ '
+else
+ test_expect_success 'pair rename to parent of other (D/F conflicts) w/ untracked dir' '
+ git checkout -q rename-one^0 &&
+ mkdir one &&
+ test_must_fail git merge --strategy=recursive rename-two &&
+
+ test_stdout_line_count = 2 git ls-files -u &&
+ test_stdout_line_count = 1 git ls-files -u one &&
+ test_stdout_line_count = 1 git ls-files -u two &&
+
+ test_must_fail git diff --quiet &&
+
+ test 4 -eq $(find . | grep -v .git | wc -l) &&
+
+ test_path_is_dir one &&
+ test_path_is_file one~rename-two &&
+ test_path_is_file two &&
+ test "other" = $(cat one~rename-two) &&
+ test "stuff" = $(cat two)
+ '
+fi
+
+test_expect_success 'pair rename to parent of other (D/F conflicts) w/ clean start' '
+ git reset --hard &&
+ git clean -fdqx &&
+ test_must_fail git merge --strategy=recursive rename-two &&
+
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ test_stdout_line_count = 4 git ls-files -u &&
+ test_stdout_line_count = 2 git ls-files -u one &&
+ test_stdout_line_count = 2 git ls-files -u two
+ else
+ test_stdout_line_count = 2 git ls-files -u &&
+ test_stdout_line_count = 1 git ls-files -u one &&
+ test_stdout_line_count = 1 git ls-files -u two
+ fi &&
+
+ test_must_fail git diff --quiet &&
+
+ test 3 -eq $(find . | grep -v .git | wc -l) &&
+
+ test_path_is_file one &&
+ test_path_is_file two &&
+ test "other" = $(cat one) &&
+ test "stuff" = $(cat two)
+'
+
+test_expect_success 'setup rename of one file to two, with directories in the way' '
+ git reset --hard &&
+ git checkout --orphan first-rename &&
+ git rm -rf . &&
+ git clean -fdqx &&
+
+ echo stuff >original &&
+ git add -A &&
+ git commit -m "Common commit" &&
+
+ mkdir two &&
+ >two/file &&
+ git add two/file &&
+ git mv original one &&
+ git commit -m "Put two/file in the way, rename to one" &&
+
+ git checkout -b second-rename HEAD~1 &&
+ mkdir one &&
+ >one/file &&
+ git add one/file &&
+ git mv original two &&
+ git commit -m "Put one/file in the way, rename to two"
+'
+
+test_expect_success 'check handling of differently renamed file with D/F conflicts' '
+ git checkout -q first-rename^0 &&
+ test_must_fail git merge --strategy=recursive second-rename &&
+
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ test_stdout_line_count = 5 git ls-files -s &&
+ test_stdout_line_count = 3 git ls-files -u &&
+ test_stdout_line_count = 1 git ls-files -u one~HEAD &&
+ test_stdout_line_count = 1 git ls-files -u two~second-rename &&
+ test_stdout_line_count = 1 git ls-files -u original &&
+ test_stdout_line_count = 0 git ls-files -o
+ else
+ test_stdout_line_count = 5 git ls-files -s &&
+ test_stdout_line_count = 3 git ls-files -u &&
+ test_stdout_line_count = 1 git ls-files -u one &&
+ test_stdout_line_count = 1 git ls-files -u two &&
+ test_stdout_line_count = 1 git ls-files -u original &&
+ test_stdout_line_count = 2 git ls-files -o
+ fi &&
+
+ test_path_is_file one/file &&
+ test_path_is_file two/file &&
+ test_path_is_file one~HEAD &&
+ test_path_is_file two~second-rename &&
+ test_path_is_missing original
+'
+
+test_expect_success 'setup rename one file to two; directories moving out of the way' '
+ git reset --hard &&
+ git checkout --orphan first-rename-redo &&
+ git rm -rf . &&
+ git clean -fdqx &&
+
+ echo stuff >original &&
+ mkdir one two &&
+ touch one/file two/file &&
+ git add -A &&
+ git commit -m "Common commit" &&
+
+ git rm -rf one &&
+ git mv original one &&
+ git commit -m "Rename to one" &&
+
+ git checkout -b second-rename-redo HEAD~1 &&
+ git rm -rf two &&
+ git mv original two &&
+ git commit -m "Rename to two"
+'
+
+test_expect_success 'check handling of differently renamed file with D/F conflicts' '
+ git checkout -q first-rename-redo^0 &&
+ test_must_fail git merge --strategy=recursive second-rename-redo &&
+
+ test_stdout_line_count = 3 git ls-files -u &&
+ test_stdout_line_count = 1 git ls-files -u one &&
+ test_stdout_line_count = 1 git ls-files -u two &&
+ test_stdout_line_count = 1 git ls-files -u original &&
+ test_stdout_line_count = 0 git ls-files -o &&
+
+ test_path_is_file one &&
+ test_path_is_file two &&
+ test_path_is_missing original
+'
+
+test_expect_success 'setup avoid unnecessary update, normal rename' '
+ git reset --hard &&
+ git checkout --orphan avoid-unnecessary-update-1 &&
+ git rm -rf . &&
+ git clean -fdqx &&
+
+ printf "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" >original &&
+ git add -A &&
+ git commit -m "Common commit" &&
+
+ git mv original rename &&
+ echo 11 >>rename &&
+ git add -u &&
+ git commit -m "Renamed and modified" &&
+
+ git checkout -b merge-branch-1 HEAD~1 &&
+ echo "random content" >random-file &&
+ git add -A &&
+ git commit -m "Random, unrelated changes"
+'
+
+test_expect_success 'avoid unnecessary update, normal rename' '
+ git checkout -q avoid-unnecessary-update-1^0 &&
+ test-tool chmtime --get -3600 rename >expect &&
+ git merge merge-branch-1 &&
+ test-tool chmtime --get rename >actual &&
+ test_cmp expect actual # "rename" should have stayed intact
+'
+
+test_expect_success 'setup to test avoiding unnecessary update, with D/F conflict' '
+ git reset --hard &&
+ git checkout --orphan avoid-unnecessary-update-2 &&
+ git rm -rf . &&
+ git clean -fdqx &&
+
+ mkdir df &&
+ printf "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n" >df/file &&
+ git add -A &&
+ git commit -m "Common commit" &&
+
+ git mv df/file temp &&
+ rm -rf df &&
+ git mv temp df &&
+ echo 11 >>df &&
+ git add -u &&
+ git commit -m "Renamed and modified" &&
+
+ git checkout -b merge-branch-2 HEAD~1 &&
+ >unrelated-change &&
+ git add unrelated-change &&
+ git commit -m "Only unrelated changes"
+'
+
+test_expect_success 'avoid unnecessary update, with D/F conflict' '
+ git checkout -q avoid-unnecessary-update-2^0 &&
+ test-tool chmtime --get -3600 df >expect &&
+ git merge merge-branch-2 &&
+ test-tool chmtime --get df >actual &&
+ test_cmp expect actual # "df" should have stayed intact
+'
+
+test_expect_success 'setup avoid unnecessary update, dir->(file,nothing)' '
+ git rm -rf . &&
+ git clean -fdqx &&
+ rm -rf .git &&
+ git init &&
+
+ >irrelevant &&
+ mkdir df &&
+ >df/file &&
+ git add -A &&
+ git commit -mA &&
+
+ git checkout -b side &&
+ git rm -rf df &&
+ git commit -mB &&
+
+ git checkout main &&
+ git rm -rf df &&
+ echo bla >df &&
+ git add -A &&
+ git commit -m "Add a newfile"
+'
+
+test_expect_success 'avoid unnecessary update, dir->(file,nothing)' '
+ git checkout -q main^0 &&
+ test-tool chmtime --get -3600 df >expect &&
+ git merge side &&
+ test-tool chmtime --get df >actual &&
+ test_cmp expect actual # "df" should have stayed intact
+'
+
+test_expect_success 'setup avoid unnecessary update, modify/delete' '
+ git rm -rf . &&
+ git clean -fdqx &&
+ rm -rf .git &&
+ git init &&
+
+ >irrelevant &&
+ >file &&
+ git add -A &&
+ git commit -mA &&
+
+ git checkout -b side &&
+ git rm -f file &&
+ git commit -m "Delete file" &&
+
+ git checkout main &&
+ echo bla >file &&
+ git add -A &&
+ git commit -m "Modify file"
+'
+
+test_expect_success 'avoid unnecessary update, modify/delete' '
+ git checkout -q main^0 &&
+ test-tool chmtime --get -3600 file >expect &&
+ test_must_fail git merge side &&
+ test-tool chmtime --get file >actual &&
+ test_cmp expect actual # "file" should have stayed intact
+'
+
+test_expect_success 'setup avoid unnecessary update, rename/add-dest' '
+ git rm -rf . &&
+ git clean -fdqx &&
+ rm -rf .git &&
+ git init &&
+
+ printf "1\n2\n3\n4\n5\n6\n7\n8\n" >file &&
+ git add -A &&
+ git commit -mA &&
+
+ git checkout -b side &&
+ cp file newfile &&
+ git add -A &&
+ git commit -m "Add file copy" &&
+
+ git checkout main &&
+ git mv file newfile &&
+ git commit -m "Rename file"
+'
+
+test_expect_success 'avoid unnecessary update, rename/add-dest' '
+ git checkout -q main^0 &&
+ test-tool chmtime --get -3600 newfile >expect &&
+ git merge side &&
+ test-tool chmtime --get newfile >actual &&
+ test_cmp expect actual # "file" should have stayed intact
+'
+
+test_expect_success 'setup merge of rename + small change' '
+ git reset --hard &&
+ git checkout --orphan rename-plus-small-change &&
+ git rm -rf . &&
+ git clean -fdqx &&
+
+ echo ORIGINAL >file &&
+ git add file &&
+
+ test_tick &&
+ git commit -m Initial &&
+ git checkout -b rename_branch &&
+ git mv file renamed_file &&
+ git commit -m Rename &&
+ git checkout rename-plus-small-change &&
+ echo NEW-VERSION >file &&
+ git commit -a -m Reformat
+'
+
+test_expect_success 'merge rename + small change' '
+ git merge rename_branch &&
+
+ test_stdout_line_count = 1 git ls-files -s &&
+ test_stdout_line_count = 0 git ls-files -o &&
+ newhash=$(git rev-parse HEAD:renamed_file) &&
+ oldhash=$(git rev-parse HEAD~1:file) &&
+ test $newhash = $oldhash
+'
+
+test_expect_success 'setup for use of extended merge markers' '
+ git rm -rf . &&
+ git clean -fdqx &&
+ rm -rf .git &&
+ git init &&
+
+ printf "1\n2\n3\n4\n5\n6\n7\n8\n" >original_file &&
+ git add original_file &&
+ git commit -mA &&
+
+ git checkout -b rename &&
+ echo 9 >>original_file &&
+ git add original_file &&
+ git mv original_file renamed_file &&
+ git commit -mB &&
+
+ git checkout main &&
+ echo 8.5 >>original_file &&
+ git add original_file &&
+ git commit -mC
+'
+
+test_expect_success 'merge main into rename has correct extended markers' '
+ git checkout rename^0 &&
+ test_must_fail git merge -s recursive main^0 &&
+
+ cat >expected <<-\EOF &&
+ 1
+ 2
+ 3
+ 4
+ 5
+ 6
+ 7
+ 8
+ <<<<<<< HEAD:renamed_file
+ 9
+ =======
+ 8.5
+ >>>>>>> main^0:original_file
+ EOF
+ test_cmp expected renamed_file
+'
+
+test_expect_success 'merge rename into main has correct extended markers' '
+ git reset --hard &&
+ git checkout main^0 &&
+ test_must_fail git merge -s recursive rename^0 &&
+
+ cat >expected <<-\EOF &&
+ 1
+ 2
+ 3
+ 4
+ 5
+ 6
+ 7
+ 8
+ <<<<<<< HEAD:original_file
+ 8.5
+ =======
+ 9
+ >>>>>>> rename^0:renamed_file
+ EOF
+ test_cmp expected renamed_file
+'
+
+test_expect_success 'setup spurious "refusing to lose untracked" message' '
+ git rm -rf . &&
+ git clean -fdqx &&
+ rm -rf .git &&
+ git init &&
+
+ > irrelevant_file &&
+ printf "1\n2\n3\n4\n5\n6\n7\n8\n" >original_file &&
+ git add irrelevant_file original_file &&
+ git commit -mA &&
+
+ git checkout -b rename &&
+ git mv original_file renamed_file &&
+ git commit -mB &&
+
+ git checkout main &&
+ git rm original_file &&
+ git commit -mC
+'
+
+test_expect_success 'no spurious "refusing to lose untracked" message' '
+ git checkout main^0 &&
+ test_must_fail git merge rename^0 2>errors.txt &&
+ ! grep "refusing to lose untracked file" errors.txt
+'
+
+test_expect_success 'do not follow renames for empty files' '
+ git checkout -f -b empty-base &&
+ >empty1 &&
+ git add empty1 &&
+ git commit -m base &&
+ echo content >empty1 &&
+ git add empty1 &&
+ git commit -m fill &&
+ git checkout -b empty-topic HEAD^ &&
+ git mv empty1 empty2 &&
+ git commit -m rename &&
+ test_must_fail git merge empty-base &&
+ test_must_be_empty empty2
+'
+
+test_done
diff --git a/t/t6403-merge-file.sh b/t/t6403-merge-file.sh
new file mode 100755
index 0000000..2c92209
--- /dev/null
+++ b/t/t6403-merge-file.sh
@@ -0,0 +1,450 @@
+#!/bin/sh
+
+test_description='RCS merge replacement: merge-file'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ cat >orig.txt <<-\EOF &&
+ Dominus regit me,
+ et nihil mihi deerit.
+ In loco pascuae ibi me collocavit,
+ super aquam refectionis educavit me;
+ animam meam convertit,
+ deduxit me super semitas jusitiae,
+ propter nomen suum.
+ EOF
+
+ cat >new1.txt <<-\EOF &&
+ Dominus regit me,
+ et nihil mihi deerit.
+ In loco pascuae ibi me collocavit,
+ super aquam refectionis educavit me;
+ animam meam convertit,
+ deduxit me super semitas jusitiae,
+ propter nomen suum.
+ Nam et si ambulavero in medio umbrae mortis,
+ non timebo mala, quoniam tu mecum es:
+ virga tua et baculus tuus ipsa me consolata sunt.
+ EOF
+
+ cat >new2.txt <<-\EOF &&
+ Dominus regit me, et nihil mihi deerit.
+ In loco pascuae ibi me collocavit,
+ super aquam refectionis educavit me;
+ animam meam convertit,
+ deduxit me super semitas jusitiae,
+ propter nomen suum.
+ EOF
+
+ cat >new3.txt <<-\EOF &&
+ DOMINUS regit me,
+ et nihil mihi deerit.
+ In loco pascuae ibi me collocavit,
+ super aquam refectionis educavit me;
+ animam meam convertit,
+ deduxit me super semitas jusitiae,
+ propter nomen suum.
+ EOF
+
+ cat >new4.txt <<-\EOF &&
+ Dominus regit me, et nihil mihi deerit.
+ In loco pascuae ibi me collocavit,
+ super aquam refectionis educavit me;
+ animam meam convertit,
+ deduxit me super semitas jusitiae,
+ EOF
+
+ printf "propter nomen suum." >>new4.txt
+'
+
+test_expect_success 'merge with no changes' '
+ cp orig.txt test.txt &&
+ git merge-file test.txt orig.txt orig.txt &&
+ test_cmp test.txt orig.txt
+'
+
+test_expect_success 'merge with no changes with --object-id' '
+ git add orig.txt &&
+ git merge-file -p --object-id :orig.txt :orig.txt :orig.txt >actual &&
+ test_cmp actual orig.txt
+'
+
+test_expect_success "merge without conflict" '
+ cp new1.txt test.txt &&
+ git merge-file test.txt orig.txt new2.txt
+'
+
+test_expect_success 'merge without conflict with --object-id' '
+ git add orig.txt new2.txt &&
+ git merge-file --object-id :orig.txt :orig.txt :new2.txt >actual &&
+ git rev-parse :new2.txt >expected &&
+ test_cmp actual expected
+'
+
+test_expect_success 'can accept object ID with --object-id' '
+ git merge-file --object-id $(test_oid empty_blob) $(test_oid empty_blob) :new2.txt >actual &&
+ git rev-parse :new2.txt >expected &&
+ test_cmp actual expected
+'
+
+test_expect_success 'works in subdirectory' '
+ mkdir dir &&
+ cp new1.txt dir/a.txt &&
+ cp orig.txt dir/o.txt &&
+ cp new2.txt dir/b.txt &&
+ ( cd dir && git merge-file a.txt o.txt b.txt ) &&
+ test_path_is_missing a.txt
+'
+
+test_expect_success "merge without conflict (--quiet)" '
+ cp new1.txt test.txt &&
+ git merge-file --quiet test.txt orig.txt new2.txt
+'
+
+test_expect_failure "merge without conflict (missing LF at EOF)" '
+ cp new1.txt test2.txt &&
+ git merge-file test2.txt orig.txt new4.txt
+'
+
+test_expect_failure "merge result added missing LF" '
+ test_cmp test.txt test2.txt
+'
+
+test_expect_success "merge without conflict (missing LF at EOF, away from change in the other file)" '
+ cp new4.txt test3.txt &&
+ git merge-file --quiet test3.txt new2.txt new3.txt
+'
+
+test_expect_success "merge does not add LF away of change" '
+ cat >expect.txt <<-\EOF &&
+ DOMINUS regit me,
+ et nihil mihi deerit.
+ In loco pascuae ibi me collocavit,
+ super aquam refectionis educavit me;
+ animam meam convertit,
+ deduxit me super semitas jusitiae,
+ EOF
+ printf "propter nomen suum." >>expect.txt &&
+
+ test_cmp expect.txt test3.txt
+'
+
+test_expect_success "merge with conflicts" '
+ cp test.txt backup.txt &&
+ test_must_fail git merge-file test.txt orig.txt new3.txt
+'
+
+test_expect_success "expected conflict markers" '
+ cat >expect.txt <<-\EOF &&
+ <<<<<<< test.txt
+ Dominus regit me, et nihil mihi deerit.
+ =======
+ DOMINUS regit me,
+ et nihil mihi deerit.
+ >>>>>>> new3.txt
+ In loco pascuae ibi me collocavit,
+ super aquam refectionis educavit me;
+ animam meam convertit,
+ deduxit me super semitas jusitiae,
+ propter nomen suum.
+ Nam et si ambulavero in medio umbrae mortis,
+ non timebo mala, quoniam tu mecum es:
+ virga tua et baculus tuus ipsa me consolata sunt.
+ EOF
+
+ test_cmp expect.txt test.txt
+'
+
+test_expect_success "merge with conflicts with --object-id" '
+ git add backup.txt orig.txt new3.txt &&
+ test_must_fail git merge-file -p --object-id :backup.txt :orig.txt :new3.txt >actual &&
+ sed -e "s/<< test.txt/<< :backup.txt/" \
+ -e "s/>> new3.txt/>> :new3.txt/" \
+ expect.txt >expect &&
+ test_cmp expect actual &&
+ test_must_fail git merge-file --object-id :backup.txt :orig.txt :new3.txt >oid &&
+ git cat-file blob "$(cat oid)" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success "merge with conflicts with --object-id with labels" '
+ git add backup.txt orig.txt new3.txt &&
+ test_must_fail git merge-file -p --object-id \
+ -L test.txt -L orig.txt -L new3.txt \
+ :backup.txt :orig.txt :new3.txt >actual &&
+ test_cmp expect.txt actual &&
+ test_must_fail git merge-file --object-id \
+ -L test.txt -L orig.txt -L new3.txt \
+ :backup.txt :orig.txt :new3.txt >oid &&
+ git cat-file blob "$(cat oid)" >actual &&
+ test_cmp expect.txt actual
+'
+
+test_expect_success "merge conflicting with --ours" '
+ cp backup.txt test.txt &&
+
+ cat >expect.txt <<-\EOF &&
+ Dominus regit me, et nihil mihi deerit.
+ In loco pascuae ibi me collocavit,
+ super aquam refectionis educavit me;
+ animam meam convertit,
+ deduxit me super semitas jusitiae,
+ propter nomen suum.
+ Nam et si ambulavero in medio umbrae mortis,
+ non timebo mala, quoniam tu mecum es:
+ virga tua et baculus tuus ipsa me consolata sunt.
+ EOF
+
+ git merge-file --ours test.txt orig.txt new3.txt &&
+ test_cmp expect.txt test.txt
+'
+
+test_expect_success "merge conflicting with --theirs" '
+ cp backup.txt test.txt &&
+
+ cat >expect.txt <<-\EOF &&
+ DOMINUS regit me,
+ et nihil mihi deerit.
+ In loco pascuae ibi me collocavit,
+ super aquam refectionis educavit me;
+ animam meam convertit,
+ deduxit me super semitas jusitiae,
+ propter nomen suum.
+ Nam et si ambulavero in medio umbrae mortis,
+ non timebo mala, quoniam tu mecum es:
+ virga tua et baculus tuus ipsa me consolata sunt.
+ EOF
+
+ git merge-file --theirs test.txt orig.txt new3.txt &&
+ test_cmp expect.txt test.txt
+'
+
+test_expect_success "merge conflicting with --union" '
+ cp backup.txt test.txt &&
+
+ cat >expect.txt <<-\EOF &&
+ Dominus regit me, et nihil mihi deerit.
+ DOMINUS regit me,
+ et nihil mihi deerit.
+ In loco pascuae ibi me collocavit,
+ super aquam refectionis educavit me;
+ animam meam convertit,
+ deduxit me super semitas jusitiae,
+ propter nomen suum.
+ Nam et si ambulavero in medio umbrae mortis,
+ non timebo mala, quoniam tu mecum es:
+ virga tua et baculus tuus ipsa me consolata sunt.
+ EOF
+
+ git merge-file --union test.txt orig.txt new3.txt &&
+ test_cmp expect.txt test.txt
+'
+
+test_expect_success "merge with conflicts, using -L" '
+ cp backup.txt test.txt &&
+
+ test_must_fail git merge-file -L 1 -L 2 test.txt orig.txt new3.txt
+'
+
+test_expect_success "expected conflict markers, with -L" '
+ cat >expect.txt <<-\EOF &&
+ <<<<<<< 1
+ Dominus regit me, et nihil mihi deerit.
+ =======
+ DOMINUS regit me,
+ et nihil mihi deerit.
+ >>>>>>> new3.txt
+ In loco pascuae ibi me collocavit,
+ super aquam refectionis educavit me;
+ animam meam convertit,
+ deduxit me super semitas jusitiae,
+ propter nomen suum.
+ Nam et si ambulavero in medio umbrae mortis,
+ non timebo mala, quoniam tu mecum es:
+ virga tua et baculus tuus ipsa me consolata sunt.
+ EOF
+
+ test_cmp expect.txt test.txt
+'
+
+test_expect_success "conflict in removed tail" '
+ sed "s/ tu / TU /" <new1.txt >new5.txt &&
+ test_must_fail git merge-file -p orig.txt new1.txt new5.txt >out
+'
+
+test_expect_success "expected conflict markers" '
+ cat >expect <<-\EOF &&
+ Dominus regit me,
+ et nihil mihi deerit.
+ In loco pascuae ibi me collocavit,
+ super aquam refectionis educavit me;
+ animam meam convertit,
+ deduxit me super semitas jusitiae,
+ propter nomen suum.
+ <<<<<<< orig.txt
+ =======
+ Nam et si ambulavero in medio umbrae mortis,
+ non timebo mala, quoniam TU mecum es:
+ virga tua et baculus tuus ipsa me consolata sunt.
+ >>>>>>> new5.txt
+ EOF
+
+ test_cmp expect out
+'
+
+test_expect_success 'binary files cannot be merged' '
+ test_must_fail git merge-file -p \
+ orig.txt "$TEST_DIRECTORY"/test-binary-1.png new1.txt 2> merge.err &&
+ grep "Cannot merge binary files" merge.err
+'
+
+test_expect_success 'binary files cannot be merged with --object-id' '
+ cp "$TEST_DIRECTORY"/test-binary-1.png . &&
+ git add orig.txt new1.txt test-binary-1.png &&
+ test_must_fail git merge-file --object-id \
+ :orig.txt :test-binary-1.png :new1.txt 2> merge.err &&
+ grep "Cannot merge binary files" merge.err
+'
+
+test_expect_success 'MERGE_ZEALOUS simplifies non-conflicts' '
+ sed -e "s/deerit.\$/deerit;/" -e "s/me;\$/me./" <new5.txt >new6.txt &&
+ sed -e "s/deerit.\$/deerit,/" -e "s/me;\$/me,/" <new5.txt >new7.txt &&
+
+ test_must_fail git merge-file -p new6.txt new5.txt new7.txt > output &&
+ test 1 = $(grep ======= <output | wc -l)
+'
+
+test_expect_success 'ZEALOUS_ALNUM' '
+ sed -e "s/deerit./&%%%%/" -e "s/locavit,/locavit;/" <new6.txt | tr % "\012" >new8.txt &&
+ sed -e "s/deerit./&%%%%/" -e "s/locavit,/locavit --/" <new7.txt | tr % "\012" >new9.txt &&
+
+ test_must_fail git merge-file -p \
+ new8.txt new5.txt new9.txt >merge.out &&
+ test 1 = $(grep ======= <merge.out | wc -l)
+'
+
+test_expect_success '"diff3 -m" style output (1)' '
+ cat >expect <<-\EOF &&
+ Dominus regit me,
+ <<<<<<< new8.txt
+ et nihil mihi deerit;
+
+
+
+
+ In loco pascuae ibi me collocavit;
+ super aquam refectionis educavit me.
+ ||||||| new5.txt
+ et nihil mihi deerit.
+ In loco pascuae ibi me collocavit,
+ super aquam refectionis educavit me;
+ =======
+ et nihil mihi deerit,
+
+
+
+
+ In loco pascuae ibi me collocavit --
+ super aquam refectionis educavit me,
+ >>>>>>> new9.txt
+ animam meam convertit,
+ deduxit me super semitas jusitiae,
+ propter nomen suum.
+ Nam et si ambulavero in medio umbrae mortis,
+ non timebo mala, quoniam TU mecum es:
+ virga tua et baculus tuus ipsa me consolata sunt.
+ EOF
+
+ test_must_fail git merge-file -p --diff3 \
+ new8.txt new5.txt new9.txt >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '"diff3 -m" style output (2)' '
+ git config merge.conflictstyle diff3 &&
+ test_must_fail git merge-file -p \
+ new8.txt new5.txt new9.txt >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'marker size' '
+ cat >expect <<-\EOF &&
+ Dominus regit me,
+ <<<<<<<<<< new8.txt
+ et nihil mihi deerit;
+
+
+
+
+ In loco pascuae ibi me collocavit;
+ super aquam refectionis educavit me.
+ |||||||||| new5.txt
+ et nihil mihi deerit.
+ In loco pascuae ibi me collocavit,
+ super aquam refectionis educavit me;
+ ==========
+ et nihil mihi deerit,
+
+
+
+
+ In loco pascuae ibi me collocavit --
+ super aquam refectionis educavit me,
+ >>>>>>>>>> new9.txt
+ animam meam convertit,
+ deduxit me super semitas jusitiae,
+ propter nomen suum.
+ Nam et si ambulavero in medio umbrae mortis,
+ non timebo mala, quoniam TU mecum es:
+ virga tua et baculus tuus ipsa me consolata sunt.
+ EOF
+
+ test_must_fail git merge-file -p --marker-size=10 \
+ new8.txt new5.txt new9.txt >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'conflict at EOF without LF resolved by --ours' '
+ printf "line1\nline2\nline3" >nolf-orig.txt &&
+ printf "line1\nline2\nline3x" >nolf-diff1.txt &&
+ printf "line1\nline2\nline3y" >nolf-diff2.txt &&
+
+ git merge-file -p --ours nolf-diff1.txt nolf-orig.txt nolf-diff2.txt >output.txt &&
+ printf "line1\nline2\nline3x" >expect.txt &&
+ test_cmp expect.txt output.txt
+'
+
+test_expect_success 'conflict at EOF without LF resolved by --theirs' '
+ git merge-file -p --theirs nolf-diff1.txt nolf-orig.txt nolf-diff2.txt >output.txt &&
+ printf "line1\nline2\nline3y" >expect.txt &&
+ test_cmp expect.txt output.txt
+'
+
+test_expect_success 'conflict at EOF without LF resolved by --union' '
+ git merge-file -p --union nolf-diff1.txt nolf-orig.txt nolf-diff2.txt >output.txt &&
+ printf "line1\nline2\nline3x\nline3y" >expect.txt &&
+ test_cmp expect.txt output.txt
+'
+
+test_expect_success 'conflict sections match existing line endings' '
+ printf "1\\r\\n2\\r\\n3" >crlf-orig.txt &&
+ printf "1\\r\\n2\\r\\n4" >crlf-diff1.txt &&
+ printf "1\\r\\n2\\r\\n5" >crlf-diff2.txt &&
+ test_must_fail git -c core.eol=crlf merge-file -p \
+ crlf-diff1.txt crlf-orig.txt crlf-diff2.txt >crlf.txt &&
+ test $(tr "\015" Q <crlf.txt | grep "^[<=>].*Q$" | wc -l) = 3 &&
+ test $(tr "\015" Q <crlf.txt | grep "[345]Q$" | wc -l) = 3 &&
+ test_must_fail git -c core.eol=crlf merge-file -p \
+ nolf-diff1.txt nolf-orig.txt nolf-diff2.txt >nolf.txt &&
+ test $(tr "\015" Q <nolf.txt | grep "^[<=>].*Q$" | wc -l) = 0
+'
+
+test_expect_success '--object-id fails without repository' '
+ empty="$(test_oid empty_blob)" &&
+ nongit test_must_fail git merge-file --object-id $empty $empty $empty 2>err &&
+ grep "not a git repository" err
+'
+
+test_done
diff --git a/t/t6404-recursive-merge.sh b/t/t6404-recursive-merge.sh
new file mode 100755
index 0000000..3621551
--- /dev/null
+++ b/t/t6404-recursive-merge.sh
@@ -0,0 +1,153 @@
+#!/bin/sh
+
+test_description='Test merge without common ancestors'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# This scenario is based on a real-world repository of Shawn Pearce.
+
+# 1 - A - D - F
+# \ X /
+# B X
+# X \
+# 2 - C - E - G
+
+GIT_COMMITTER_DATE="2006-12-12 23:28:00 +0100"
+export GIT_COMMITTER_DATE
+
+test_expect_success 'setup tests' '
+ GIT_TEST_COMMIT_GRAPH=0 &&
+ export GIT_TEST_COMMIT_GRAPH &&
+ echo 1 >a1 &&
+ git add a1 &&
+ GIT_AUTHOR_DATE="2006-12-12 23:00:00" git commit -m 1 a1 &&
+
+ git checkout -b A main &&
+ echo A >a1 &&
+ GIT_AUTHOR_DATE="2006-12-12 23:00:01" git commit -m A a1 &&
+
+ git checkout -b B main &&
+ echo B >a1 &&
+ GIT_AUTHOR_DATE="2006-12-12 23:00:02" git commit -m B a1 &&
+
+ git checkout -b D A &&
+ git rev-parse B >.git/MERGE_HEAD &&
+ echo D >a1 &&
+ git update-index a1 &&
+ GIT_AUTHOR_DATE="2006-12-12 23:00:03" git commit -m D &&
+
+ git symbolic-ref HEAD refs/heads/other &&
+ echo 2 >a1 &&
+ GIT_AUTHOR_DATE="2006-12-12 23:00:04" git commit -m 2 a1 &&
+
+ git checkout -b C &&
+ echo C >a1 &&
+ GIT_AUTHOR_DATE="2006-12-12 23:00:05" git commit -m C a1 &&
+
+ git checkout -b E C &&
+ git rev-parse B >.git/MERGE_HEAD &&
+ echo E >a1 &&
+ git update-index a1 &&
+ GIT_AUTHOR_DATE="2006-12-12 23:00:06" git commit -m E &&
+
+ git checkout -b G E &&
+ git rev-parse A >.git/MERGE_HEAD &&
+ echo G >a1 &&
+ git update-index a1 &&
+ GIT_AUTHOR_DATE="2006-12-12 23:00:07" git commit -m G &&
+
+ git checkout -b F D &&
+ git rev-parse C >.git/MERGE_HEAD &&
+ echo F >a1 &&
+ git update-index a1 &&
+ GIT_AUTHOR_DATE="2006-12-12 23:00:08" git commit -m F &&
+
+ test_oid_cache <<-EOF
+ idxstage1 sha1:ec3fe2a791706733f2d8fa7ad45d9a9672031f5e
+ idxstage1 sha256:b3c8488929903aaebdeb22270cb6d36e5b8724b01ae0d4da24632f158c99676f
+ EOF
+'
+
+test_expect_success 'combined merge conflicts' '
+ test_must_fail git merge -m final G
+'
+
+test_expect_success 'result contains a conflict' '
+ cat >expect <<-\EOF &&
+ <<<<<<< HEAD
+ F
+ =======
+ G
+ >>>>>>> G
+ EOF
+
+ test_cmp expect a1
+'
+
+test_expect_success 'virtual trees were processed' '
+ # TODO: fragile test, relies on ambigious merge-base resolution
+ git ls-files --stage >out &&
+
+ cat >expect <<-EOF &&
+ 100644 $(test_oid idxstage1) 1 a1
+ 100644 $(git rev-parse F:a1) 2 a1
+ 100644 $(git rev-parse G:a1) 3 a1
+ EOF
+
+ test_cmp expect out
+'
+
+test_expect_success 'refuse to merge binary files' '
+ git reset --hard &&
+ printf "\0" >binary-file &&
+ git add binary-file &&
+ git commit -m binary &&
+ git checkout G &&
+ printf "\0\0" >binary-file &&
+ git add binary-file &&
+ git commit -m binary2 &&
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ test_must_fail git merge F >merge_output
+ else
+ test_must_fail git merge F 2>merge_output
+ fi &&
+ grep "Cannot merge binary files: binary-file (HEAD vs. F)" merge_output
+'
+
+test_expect_success 'mark rename/delete as unmerged' '
+
+ git reset --hard &&
+ git checkout -b delete &&
+ git rm a1 &&
+ test_tick &&
+ git commit -m delete &&
+ git checkout -b rename HEAD^ &&
+ git mv a1 a2 &&
+ test_tick &&
+ git commit -m rename &&
+ test_must_fail git merge delete &&
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ test 2 = $(git ls-files --unmerged | wc -l)
+ else
+ test 1 = $(git ls-files --unmerged | wc -l)
+ fi &&
+ git rev-parse --verify :2:a2 &&
+ test_must_fail git rev-parse --verify :3:a2 &&
+ git checkout -f delete &&
+ test_must_fail git merge rename &&
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ test 2 = $(git ls-files --unmerged | wc -l)
+ else
+ test 1 = $(git ls-files --unmerged | wc -l)
+ fi &&
+ test_must_fail git rev-parse --verify :2:a2 &&
+ git rev-parse --verify :3:a2
+'
+
+test_done
diff --git a/t/t6405-merge-symlinks.sh b/t/t6405-merge-symlinks.sh
new file mode 100755
index 0000000..29e2b25
--- /dev/null
+++ b/t/t6405-merge-symlinks.sh
@@ -0,0 +1,66 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Johannes Sixt
+#
+
+test_description='merging symlinks on filesystem w/o symlink support.
+
+This tests that git merge-recursive writes merge results as plain files
+if core.symlinks is false.'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ git config core.symlinks false &&
+ >file &&
+ git add file &&
+ git commit -m initial &&
+ git branch b-symlink &&
+ git branch b-file &&
+ l=$(printf file | git hash-object -t blob -w --stdin) &&
+ echo "120000 $l symlink" | git update-index --index-info &&
+ git commit -m main &&
+ git checkout b-symlink &&
+ l=$(printf file-different | git hash-object -t blob -w --stdin) &&
+ echo "120000 $l symlink" | git update-index --index-info &&
+ git commit -m b-symlink &&
+ git checkout b-file &&
+ echo plain-file >symlink &&
+ git add symlink &&
+ git commit -m b-file
+'
+
+test_expect_success 'merge main into b-symlink, which has a different symbolic link' '
+ git checkout b-symlink &&
+ test_must_fail git merge main
+'
+
+test_expect_success 'the merge result must be a file' '
+ test_path_is_file symlink
+'
+
+test_expect_success 'merge main into b-file, which has a file instead of a symbolic link' '
+ git reset --hard &&
+ git checkout b-file &&
+ test_must_fail git merge main
+'
+
+test_expect_success 'the merge result must be a file' '
+ test_path_is_file symlink
+'
+
+test_expect_success 'merge b-file, which has a file instead of a symbolic link, into main' '
+ git reset --hard &&
+ git checkout main &&
+ test_must_fail git merge b-file
+'
+
+test_expect_success 'the merge result must be a file' '
+ test_path_is_file symlink
+'
+
+test_done
diff --git a/t/t6406-merge-attr.sh b/t/t6406-merge-attr.sh
new file mode 100755
index 0000000..72f8c17
--- /dev/null
+++ b/t/t6406-merge-attr.sh
@@ -0,0 +1,258 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Junio C Hamano
+#
+
+test_description='per path merge controlled by merge attribute'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+
+ for f in text binary union
+ do
+ echo Initial >$f && git add $f || return 1
+ done &&
+ test_tick &&
+ git commit -m Initial &&
+
+ git branch side &&
+ for f in text binary union
+ do
+ echo Main >>$f && git add $f || return 1
+ done &&
+ test_tick &&
+ git commit -m Main &&
+
+ git checkout side &&
+ for f in text binary union
+ do
+ echo Side >>$f && git add $f || return 1
+ done &&
+ test_tick &&
+ git commit -m Side &&
+
+ git tag anchor &&
+
+ cat >./custom-merge <<-\EOF &&
+ #!/bin/sh
+
+ orig="$1" ours="$2" theirs="$3" exit="$4" path=$5
+ (
+ echo "orig is $orig"
+ echo "ours is $ours"
+ echo "theirs is $theirs"
+ echo "path is $path"
+ echo "=== orig ==="
+ cat "$orig"
+ echo "=== ours ==="
+ cat "$ours"
+ echo "=== theirs ==="
+ cat "$theirs"
+ ) >"$ours+"
+ cat "$ours+" >"$ours"
+ rm -f "$ours+"
+
+ if test -f ./please-abort
+ then
+ echo >>./please-abort killing myself
+ kill -9 $$
+ fi
+ exit "$exit"
+ EOF
+ chmod +x ./custom-merge
+'
+
+test_expect_success merge '
+
+ cat >.gitattributes <<-\EOF &&
+ binary -merge
+ union merge=union
+ EOF
+
+ if git merge main
+ then
+ echo Gaah, should have conflicted
+ false
+ else
+ echo Ok, conflicted.
+ fi
+'
+
+test_expect_success 'check merge result in index' '
+
+ git ls-files -u | grep binary &&
+ git ls-files -u | grep text &&
+ ! (git ls-files -u | grep union)
+
+'
+
+test_expect_success 'check merge result in working tree' '
+
+ git cat-file -p HEAD:binary >binary-orig &&
+ grep "<<<<<<<" text &&
+ cmp binary-orig binary &&
+ ! grep "<<<<<<<" union &&
+ grep Main union &&
+ grep Side union
+
+'
+
+test_expect_success 'retry the merge with longer context' '
+ echo text conflict-marker-size=32 >>.gitattributes &&
+ git checkout -m text &&
+ sed -ne "/^\([<=>]\)\1\1\1*/{
+ s/ .*$//
+ p
+ }" >actual text &&
+ grep ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" actual &&
+ grep "================================" actual &&
+ grep "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" actual
+'
+
+test_expect_success 'custom merge backend' '
+
+ echo "* merge=union" >.gitattributes &&
+ echo "text merge=custom" >>.gitattributes &&
+
+ git reset --hard anchor &&
+ git config --replace-all \
+ merge.custom.driver "./custom-merge %O %A %B 0 %P" &&
+ git config --replace-all \
+ merge.custom.name "custom merge driver for testing" &&
+
+ git merge main &&
+
+ cmp binary union &&
+ sed -e 1,3d text >check-1 &&
+ o=$(git unpack-file main^:text) &&
+ a=$(git unpack-file side^:text) &&
+ b=$(git unpack-file main:text) &&
+ sh -c "./custom-merge $o $a $b 0 text" &&
+ sed -e 1,3d $a >check-2 &&
+ cmp check-1 check-2 &&
+ rm -f $o $a $b
+'
+
+test_expect_success 'custom merge backend' '
+
+ git reset --hard anchor &&
+ git config --replace-all \
+ merge.custom.driver "./custom-merge %O %A %B 1 %P" &&
+ git config --replace-all \
+ merge.custom.name "custom merge driver for testing" &&
+
+ if git merge main
+ then
+ echo "Eh? should have conflicted"
+ false
+ else
+ echo "Ok, conflicted"
+ fi &&
+
+ cmp binary union &&
+ sed -e 1,3d text >check-1 &&
+ o=$(git unpack-file main^:text) &&
+ a=$(git unpack-file anchor:text) &&
+ b=$(git unpack-file main:text) &&
+ sh -c "./custom-merge $o $a $b 0 text" &&
+ sed -e 1,3d $a >check-2 &&
+ cmp check-1 check-2 &&
+ sed -e 1,3d -e 4q $a >check-3 &&
+ echo "path is text" >expect &&
+ cmp expect check-3 &&
+ rm -f $o $a $b
+'
+
+test_expect_success !WINDOWS 'custom merge driver that is killed with a signal' '
+ test_when_finished "rm -f output please-abort" &&
+
+ git reset --hard anchor &&
+ git config --replace-all \
+ merge.custom.driver "./custom-merge %O %A %B 0 %P" &&
+ git config --replace-all \
+ merge.custom.name "custom merge driver for testing" &&
+
+ >./please-abort &&
+ echo "* merge=custom" >.gitattributes &&
+ test_must_fail git merge main 2>err &&
+ grep "^error: failed to execute internal merge" err &&
+ git ls-files -u >output &&
+ git diff --name-only HEAD >>output &&
+ test_must_be_empty output
+'
+
+test_expect_success 'up-to-date merge without common ancestor' '
+ git init repo1 &&
+ git init repo2 &&
+ test_tick &&
+ (
+ cd repo1 &&
+ >a &&
+ git add a &&
+ git commit -m initial
+ ) &&
+ test_tick &&
+ (
+ cd repo2 &&
+ git commit --allow-empty -m initial
+ ) &&
+ test_tick &&
+ (
+ cd repo1 &&
+ git fetch ../repo2 main &&
+ git merge --allow-unrelated-histories FETCH_HEAD
+ )
+'
+
+test_expect_success 'custom merge does not lock index' '
+ git reset --hard anchor &&
+ write_script sleep-an-hour.sh <<-\EOF &&
+ sleep 3600 &
+ echo $! >sleep.pid
+ EOF
+
+ test_write_lines >.gitattributes \
+ "* merge=ours" "text merge=sleep-an-hour" &&
+ test_config merge.ours.driver true &&
+ test_config merge.sleep-an-hour.driver ./sleep-an-hour.sh &&
+
+ # We are testing that the custom merge driver does not block
+ # index.lock on Windows due to an inherited file handle.
+ # To ensure that the backgrounded process ran sufficiently
+ # long (and has been started in the first place), we do not
+ # ignore the result of the kill command.
+ # By packaging the command in test_when_finished, we get both
+ # the correctness check and the clean-up.
+ test_when_finished "kill \$(cat sleep.pid)" &&
+ git merge main
+'
+
+test_expect_success 'binary files with union attribute' '
+ git checkout -b bin-main &&
+ printf "base\0" >bin.txt &&
+ echo "bin.txt merge=union" >.gitattributes &&
+ git add bin.txt .gitattributes &&
+ git commit -m base &&
+
+ printf "one\0" >bin.txt &&
+ git commit -am one &&
+
+ git checkout -b bin-side HEAD^ &&
+ printf "two\0" >bin.txt &&
+ git commit -am two &&
+
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ test_must_fail git merge bin-main >output
+ else
+ test_must_fail git merge bin-main 2>output
+ fi &&
+ grep -i "warning.*cannot merge.*HEAD vs. bin-main" output
+'
+
+test_done
diff --git a/t/t6407-merge-binary.sh b/t/t6407-merge-binary.sh
new file mode 100755
index 0000000..0753fc9
--- /dev/null
+++ b/t/t6407-merge-binary.sh
@@ -0,0 +1,61 @@
+#!/bin/sh
+
+test_description='ask merge-recursive to merge binary files'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+
+ cat "$TEST_DIRECTORY"/test-binary-1.png >m &&
+ git add m &&
+ git ls-files -s | sed -e "s/ 0 / 1 /" >E1 &&
+ test_tick &&
+ git commit -m "initial" &&
+
+ git branch side &&
+ echo frotz >a &&
+ git add a &&
+ echo nitfol >>m &&
+ git add a m &&
+ git ls-files -s a >E0 &&
+ git ls-files -s m | sed -e "s/ 0 / 3 /" >E3 &&
+ test_tick &&
+ git commit -m "main adds some" &&
+
+ git checkout side &&
+ echo rezrov >>m &&
+ git add m &&
+ git ls-files -s m | sed -e "s/ 0 / 2 /" >E2 &&
+ test_tick &&
+ git commit -m "side modifies" &&
+
+ git tag anchor &&
+
+ cat E0 E1 E2 E3 >expect
+'
+
+test_expect_success resolve '
+
+ rm -f a* m* &&
+ git reset --hard anchor &&
+
+ test_must_fail git merge -s resolve main &&
+ git ls-files -s >current &&
+ test_cmp expect current
+'
+
+test_expect_success recursive '
+
+ rm -f a* m* &&
+ git reset --hard anchor &&
+
+ test_must_fail git merge -s recursive main &&
+ git ls-files -s >current &&
+ test_cmp expect current
+'
+
+test_done
diff --git a/t/t6408-merge-up-to-date.sh b/t/t6408-merge-up-to-date.sh
new file mode 100755
index 0000000..8a1ba6d
--- /dev/null
+++ b/t/t6408-merge-up-to-date.sh
@@ -0,0 +1,93 @@
+#!/bin/sh
+
+test_description='merge fast-forward and up to date'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ >file &&
+ git add file &&
+ test_tick &&
+ git commit -m initial &&
+ git tag c0 &&
+
+ echo second >file &&
+ git add file &&
+ test_tick &&
+ git commit -m second &&
+ git tag c1 &&
+ git branch test &&
+ echo third >file &&
+ git add file &&
+ test_tick &&
+ git commit -m third &&
+ git tag c2
+'
+
+test_expect_success 'merge -s recursive up-to-date' '
+
+ git reset --hard c1 &&
+ test_tick &&
+ git merge -s recursive c0 &&
+ expect=$(git rev-parse c1) &&
+ current=$(git rev-parse HEAD) &&
+ test "$expect" = "$current"
+
+'
+
+test_expect_success 'merge -s recursive fast-forward' '
+
+ git reset --hard c0 &&
+ test_tick &&
+ git merge -s recursive c1 &&
+ expect=$(git rev-parse c1) &&
+ current=$(git rev-parse HEAD) &&
+ test "$expect" = "$current"
+
+'
+
+test_expect_success 'merge -s ours up-to-date' '
+
+ git reset --hard c1 &&
+ test_tick &&
+ git merge -s ours c0 &&
+ expect=$(git rev-parse c1) &&
+ current=$(git rev-parse HEAD) &&
+ test "$expect" = "$current"
+
+'
+
+test_expect_success 'merge -s ours fast-forward' '
+
+ git reset --hard c0 &&
+ test_tick &&
+ git merge -s ours c1 &&
+ expect=$(git rev-parse c0^{tree}) &&
+ current=$(git rev-parse HEAD^{tree}) &&
+ test "$expect" = "$current"
+
+'
+
+test_expect_success 'merge -s subtree up-to-date' '
+
+ git reset --hard c1 &&
+ test_tick &&
+ git merge -s subtree c0 &&
+ expect=$(git rev-parse c1) &&
+ current=$(git rev-parse HEAD) &&
+ test "$expect" = "$current"
+
+'
+
+test_expect_success 'merge fast-forward octopus' '
+
+ git reset --hard c0 &&
+ test_tick &&
+ git merge c1 c2 &&
+ expect=$(git rev-parse c2) &&
+ current=$(git rev-parse HEAD) &&
+ test "$expect" = "$current"
+'
+
+test_done
diff --git a/t/t6409-merge-subtree.sh b/t/t6409-merge-subtree.sh
new file mode 100755
index 0000000..e9ba6f1
--- /dev/null
+++ b/t/t6409-merge-subtree.sh
@@ -0,0 +1,155 @@
+#!/bin/sh
+
+test_description='subtree merge strategy'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success setup '
+
+ s="1 2 3 4 5 6 7 8" &&
+ test_write_lines $s >hello &&
+ git add hello &&
+ git commit -m initial &&
+ git checkout -b side &&
+ echo >>hello world &&
+ git add hello &&
+ git commit -m second &&
+ git checkout main &&
+ test_write_lines mundo $s >hello &&
+ git add hello &&
+ git commit -m main
+
+'
+
+test_expect_success 'subtree available and works like recursive' '
+
+ git merge -s subtree side &&
+ test_write_lines mundo $s world >expect &&
+ test_cmp expect hello
+
+'
+
+test_expect_success 'setup branch sub' '
+ git checkout --orphan sub &&
+ git rm -rf . &&
+ test_commit foo
+'
+
+test_expect_success 'setup topic branch' '
+ git checkout -b topic main &&
+ git merge -s ours --no-commit --allow-unrelated-histories sub &&
+ git read-tree --prefix=dir/ -u sub &&
+ git commit -m "initial merge of sub into topic" &&
+ test_path_is_file dir/foo.t &&
+ test_path_is_file hello
+'
+
+test_expect_success 'update branch sub' '
+ git checkout sub &&
+ test_commit bar
+'
+
+test_expect_success 'update topic branch' '
+ git checkout topic &&
+ git merge -s subtree sub -m "second merge of sub into topic" &&
+ test_path_is_file dir/bar.t &&
+ test_path_is_file dir/foo.t &&
+ test_path_is_file hello
+'
+
+test_expect_success 'setup' '
+ mkdir git-gui &&
+ cd git-gui &&
+ git init &&
+ echo git-gui > git-gui.sh &&
+ o1=$(git hash-object git-gui.sh) &&
+ git add git-gui.sh &&
+ git commit -m "initial git-gui" &&
+ cd .. &&
+ mkdir git &&
+ cd git &&
+ git init &&
+ echo git >git.c &&
+ o2=$(git hash-object git.c) &&
+ git add git.c &&
+ git commit -m "initial git"
+'
+
+test_expect_success 'initial merge' '
+ git remote add -f gui ../git-gui &&
+ git merge -s ours --no-commit --allow-unrelated-histories gui/main &&
+ git read-tree --prefix=git-gui/ -u gui/main &&
+ git commit -m "Merge git-gui as our subdirectory" &&
+ git checkout -b work &&
+ git ls-files -s >actual &&
+ (
+ echo "100644 $o1 0 git-gui/git-gui.sh" &&
+ echo "100644 $o2 0 git.c"
+ ) >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'merge update' '
+ cd ../git-gui &&
+ echo git-gui2 > git-gui.sh &&
+ o3=$(git hash-object git-gui.sh) &&
+ git add git-gui.sh &&
+ git checkout -b topic_2 &&
+ git commit -m "update git-gui" &&
+ cd ../git &&
+ git pull --no-rebase -s subtree gui topic_2 &&
+ git ls-files -s >actual &&
+ (
+ echo "100644 $o3 0 git-gui/git-gui.sh" &&
+ echo "100644 $o2 0 git.c"
+ ) >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'initial ambiguous subtree' '
+ cd ../git &&
+ git reset --hard main &&
+ git checkout -b topic_2 &&
+ git merge -s ours --no-commit gui/main &&
+ git read-tree --prefix=git-gui2/ -u gui/main &&
+ git commit -m "Merge git-gui2 as our subdirectory" &&
+ git checkout -b work2 &&
+ git ls-files -s >actual &&
+ (
+ echo "100644 $o1 0 git-gui/git-gui.sh" &&
+ echo "100644 $o1 0 git-gui2/git-gui.sh" &&
+ echo "100644 $o2 0 git.c"
+ ) >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'merge using explicit' '
+ cd ../git &&
+ git reset --hard topic_2 &&
+ git pull --no-rebase -Xsubtree=git-gui gui topic_2 &&
+ git ls-files -s >actual &&
+ (
+ echo "100644 $o3 0 git-gui/git-gui.sh" &&
+ echo "100644 $o1 0 git-gui2/git-gui.sh" &&
+ echo "100644 $o2 0 git.c"
+ ) >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'merge2 using explicit' '
+ cd ../git &&
+ git reset --hard topic_2 &&
+ git pull --no-rebase -Xsubtree=git-gui2 gui topic_2 &&
+ git ls-files -s >actual &&
+ (
+ echo "100644 $o1 0 git-gui/git-gui.sh" &&
+ echo "100644 $o3 0 git-gui2/git-gui.sh" &&
+ echo "100644 $o2 0 git.c"
+ ) >expected &&
+ test_cmp expected actual
+'
+
+test_done
diff --git a/t/t6411-merge-filemode.sh b/t/t6411-merge-filemode.sh
new file mode 100755
index 0000000..b618272
--- /dev/null
+++ b/t/t6411-merge-filemode.sh
@@ -0,0 +1,104 @@
+#!/bin/sh
+
+test_description='merge: handle file mode'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'set up mode change in one branch' '
+ : >file1 &&
+ git add file1 &&
+ git commit -m initial &&
+ git checkout -b a1 main &&
+ : >dummy &&
+ git add dummy &&
+ git commit -m a &&
+ git checkout -b b1 main &&
+ test_chmod +x file1 &&
+ git add file1 &&
+ git commit -m b1
+'
+
+do_one_mode () {
+ strategy=$1
+ us=$2
+ them=$3
+ test_expect_success "resolve single mode change ($strategy, $us)" '
+ git checkout -f $us &&
+ git merge -s $strategy $them &&
+ git ls-files -s file1 | grep ^100755
+ '
+
+ test_expect_success FILEMODE "verify executable bit on file ($strategy, $us)" '
+ test -x file1
+ '
+}
+
+do_one_mode recursive a1 b1
+do_one_mode recursive b1 a1
+do_one_mode resolve a1 b1
+do_one_mode resolve b1 a1
+
+test_expect_success 'set up mode change in both branches' '
+ git reset --hard HEAD &&
+ git checkout -b a2 main &&
+ : >file2 &&
+ H=$(git hash-object file2) &&
+ test_chmod +x file2 &&
+ git commit -m a2 &&
+ git checkout -b b2 main &&
+ : >file2 &&
+ git add file2 &&
+ git commit -m b2 &&
+ cat >expect <<-EOF
+ 100755 $H 2 file2
+ 100644 $H 3 file2
+ EOF
+'
+
+do_both_modes () {
+ strategy=$1
+ test_expect_success "detect conflict on double mode change ($strategy)" '
+ git reset --hard &&
+ git checkout -f a2 &&
+ test_must_fail git merge -s $strategy b2 &&
+ git ls-files -u >actual &&
+ test_cmp expect actual &&
+ git ls-files -s file2 | grep ^100755
+ '
+
+ test_expect_success FILEMODE "verify executable bit on file ($strategy)" '
+ test -x file2
+ '
+}
+
+# both sides are equivalent, so no need to run both ways
+do_both_modes recursive
+do_both_modes resolve
+
+test_expect_success 'set up delete/modechange scenario' '
+ git reset --hard &&
+ git checkout -b deletion main &&
+ git rm file1 &&
+ git commit -m deletion
+'
+
+do_delete_modechange () {
+ strategy=$1
+ us=$2
+ them=$3
+ test_expect_success "detect delete/modechange conflict ($strategy, $us)" '
+ git reset --hard &&
+ git checkout $us &&
+ test_must_fail git merge -s $strategy $them
+ '
+}
+
+do_delete_modechange recursive b1 deletion
+do_delete_modechange recursive deletion b1
+do_delete_modechange resolve b1 deletion
+do_delete_modechange resolve deletion b1
+
+test_done
diff --git a/t/t6412-merge-large-rename.sh b/t/t6412-merge-large-rename.sh
new file mode 100755
index 0000000..ca018d1
--- /dev/null
+++ b/t/t6412-merge-large-rename.sh
@@ -0,0 +1,106 @@
+#!/bin/sh
+
+test_description='merging with large rename matrix'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+count() {
+ i=1
+ while test $i -le $1; do
+ echo $i
+ i=$(($i + 1))
+ done
+}
+
+test_expect_success 'setup (initial)' '
+ touch file &&
+ git add . &&
+ git commit -m initial &&
+ git tag initial
+'
+
+make_text() {
+ echo $1: $2
+ for i in $(count 20); do
+ echo $1: $i
+ done
+ echo $1: $3
+}
+
+test_rename() {
+ test_expect_success "rename ($1, $2)" '
+ n='$1' &&
+ expect='$2' &&
+ git checkout -f main &&
+ test_might_fail git branch -D test$n &&
+ git reset --hard initial &&
+ for i in $(count $n); do
+ make_text $i initial initial >$i || return 1
+ done &&
+ git add . &&
+ git commit -m add=$n &&
+ for i in $(count $n); do
+ make_text $i changed initial >$i || return 1
+ done &&
+ git commit -a -m change=$n &&
+ git checkout -b test$n HEAD^ &&
+ for i in $(count $n); do
+ git rm $i &&
+ make_text $i initial changed >$i.moved || return 1
+ done &&
+ git add . &&
+ git commit -m change+rename=$n &&
+ case "$expect" in
+ ok) git merge main ;;
+ *) test_must_fail git merge main ;;
+ esac
+ '
+}
+
+test_rename 5 ok
+
+test_expect_success 'set diff.renamelimit to 4' '
+ git config diff.renamelimit 4
+'
+test_rename 4 ok
+test_rename 5 fail
+
+test_expect_success 'set merge.renamelimit to 5' '
+ git config merge.renamelimit 5
+'
+test_rename 5 ok
+test_rename 6 fail
+
+test_expect_success 'setup large simple rename' '
+ git config --unset merge.renamelimit &&
+ git config --unset diff.renamelimit &&
+
+ git reset --hard initial &&
+ for i in $(count 200); do
+ make_text foo bar baz >$i || return 1
+ done &&
+ git add . &&
+ git commit -m create-files &&
+
+ git branch simple-change &&
+ git checkout -b simple-rename &&
+
+ mkdir builtin &&
+ git mv [0-9]* builtin/ &&
+ git commit -m renamed &&
+
+ git checkout simple-change &&
+ >unrelated-change &&
+ git add unrelated-change &&
+ git commit -m unrelated-change
+'
+
+test_expect_success 'massive simple rename does not spam added files' '
+ sane_unset GIT_MERGE_VERBOSITY &&
+ git merge --no-stat simple-rename | grep -v Removing >output &&
+ test_line_count -lt 5 output
+'
+
+test_done
diff --git a/t/t6413-merge-crlf.sh b/t/t6413-merge-crlf.sh
new file mode 100755
index 0000000..b4f4a31
--- /dev/null
+++ b/t/t6413-merge-crlf.sh
@@ -0,0 +1,48 @@
+#!/bin/sh
+
+test_description='merge conflict in crlf repo
+
+ b---M
+ / /
+ initial---a
+
+'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ git config core.autocrlf true &&
+ echo foo | append_cr >file &&
+ git add file &&
+ git commit -m "Initial" &&
+ git tag initial &&
+ git branch side &&
+ echo line from a | append_cr >file &&
+ git commit -m "add line from a" file &&
+ git tag a &&
+ git checkout side &&
+ echo line from b | append_cr >file &&
+ git commit -m "add line from b" file &&
+ git tag b &&
+ git checkout main
+'
+
+test_expect_success 'Check "ours" is CRLF' '
+ git reset --hard initial &&
+ git merge side -s ours &&
+ cat file | remove_cr | append_cr >file.temp &&
+ test_cmp file file.temp
+'
+
+test_expect_success 'Check that conflict file is CRLF' '
+ git reset --hard a &&
+ test_must_fail git merge side &&
+ cat file | remove_cr | append_cr >file.temp &&
+ test_cmp file file.temp
+'
+
+test_done
diff --git a/t/t6414-merge-rename-nocruft.sh b/t/t6414-merge-rename-nocruft.sh
new file mode 100755
index 0000000..69fc1c9
--- /dev/null
+++ b/t/t6414-merge-rename-nocruft.sh
@@ -0,0 +1,101 @@
+#!/bin/sh
+
+test_description='Merge-recursive merging renames'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ cat >A <<-\EOF &&
+ a aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+ b bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
+ c cccccccccccccccccccccccccccccccccccccccccccccccc
+ d dddddddddddddddddddddddddddddddddddddddddddddddd
+ e eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
+ f ffffffffffffffffffffffffffffffffffffffffffffffff
+ g gggggggggggggggggggggggggggggggggggggggggggggggg
+ h hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
+ i iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
+ j jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
+ k kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
+ l llllllllllllllllllllllllllllllllllllllllllllllll
+ m mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
+ n nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
+ o oooooooooooooooooooooooooooooooooooooooooooooooo
+ EOF
+
+ cat >M <<-\EOF &&
+ A AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
+ B BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
+ C CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
+ D DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
+ E EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE
+ F FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
+ G GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG
+ H HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
+ I IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
+ J JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ
+ K KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK
+ L LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL
+ M MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
+ N NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
+ O OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
+ EOF
+
+ git add A M &&
+ git commit -m "initial has A and M" &&
+ git branch white &&
+ git branch red &&
+ git branch blue &&
+
+ git checkout white &&
+ sed -e "/^g /s/.*/g : white changes a line/" <A >B &&
+ sed -e "/^G /s/.*/G : colored branch changes a line/" <M >N &&
+ rm -f A M &&
+ git update-index --add --remove A B M N &&
+ git commit -m "white renames A->B, M->N" &&
+
+ git checkout red &&
+ echo created by red >R &&
+ git update-index --add R &&
+ git commit -m "red creates R" &&
+
+ git checkout blue &&
+ sed -e "/^o /s/.*/g : blue changes a line/" <A >B &&
+ rm -f A &&
+ mv B A &&
+ git update-index A &&
+ git commit -m "blue modify A" &&
+
+ git checkout main
+'
+
+# This test broke in 65ac6e9c3f47807cb603af07a6a9e1a43bc119ae
+test_expect_success 'merge white into red (A->B,M->N)' '
+ git checkout -b red-white red &&
+ git merge white &&
+ git write-tree &&
+ test_path_is_file B &&
+ test_path_is_file N &&
+ test_path_is_file R &&
+ test_path_is_missing A &&
+ test_path_is_missing M
+'
+
+# This test broke in 8371234ecaaf6e14fe3f2082a855eff1bbd79ae9
+test_expect_success 'merge blue into white (A->B, mod A, A untracked)' '
+ git checkout -b white-blue white &&
+ echo dirty >A &&
+ git merge blue &&
+ git write-tree &&
+ test_path_is_file A &&
+ echo dirty >expect &&
+ test_cmp expect A &&
+ test_path_is_file B &&
+ test_path_is_file N &&
+ test_path_is_missing M
+'
+
+test_done
diff --git a/t/t6415-merge-dir-to-symlink.sh b/t/t6415-merge-dir-to-symlink.sh
new file mode 100755
index 0000000..ae00492
--- /dev/null
+++ b/t/t6415-merge-dir-to-symlink.sh
@@ -0,0 +1,178 @@
+#!/bin/sh
+
+test_description='merging when a directory was replaced with a symlink'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'create a commit where dir a/b changed to symlink' '
+ mkdir -p a/b/c a/b-2/c &&
+ > a/b/c/d &&
+ > a/b-2/c/d &&
+ > a/x &&
+ git add -A &&
+ git commit -m base &&
+ git tag start &&
+ rm -rf a/b &&
+ git add -A &&
+ test_ln_s_add b-2 a/b &&
+ git commit -m "dir to symlink"
+'
+
+test_expect_success 'checkout does not clobber untracked symlink' '
+ git checkout HEAD^0 &&
+ git reset --hard main &&
+ git rm --cached a/b &&
+ git commit -m "untracked symlink remains" &&
+ test_must_fail git checkout start^0 &&
+ git clean -fd # Do not leave the untracked symlink in the way
+'
+
+test_expect_success 'a/b-2/c/d is kept when clobbering symlink b' '
+ git checkout HEAD^0 &&
+ git reset --hard main &&
+ git rm --cached a/b &&
+ git commit -m "untracked symlink remains" &&
+ git checkout -f start^0 &&
+ test_path_is_file a/b-2/c/d &&
+ git clean -fd # Do not leave the untracked symlink in the way
+'
+
+test_expect_success 'checkout should not have deleted a/b-2/c/d' '
+ git checkout HEAD^0 &&
+ git reset --hard main &&
+ git checkout start^0 &&
+ test_path_is_file a/b-2/c/d
+'
+
+test_expect_success 'setup for merge test' '
+ git reset --hard &&
+ test_path_is_file a/b-2/c/d &&
+ echo x > a/x &&
+ git add a/x &&
+ git commit -m x &&
+ git tag baseline
+'
+
+test_expect_success 'Handle D/F conflict, do not lose a/b-2/c/d in merge (resolve)' '
+ git reset --hard &&
+ git checkout baseline^0 &&
+ git merge -s resolve main &&
+ test_path_is_file a/b-2/c/d
+'
+
+test_expect_success SYMLINKS 'a/b was resolved as symlink' '
+ test -h a/b
+'
+
+test_expect_success 'Handle D/F conflict, do not lose a/b-2/c/d in merge (recursive)' '
+ git reset --hard &&
+ git checkout baseline^0 &&
+ git merge -s recursive main &&
+ test_path_is_file a/b-2/c/d
+'
+
+test_expect_success SYMLINKS 'a/b was resolved as symlink' '
+ test -h a/b
+'
+
+test_expect_success 'Handle F/D conflict, do not lose a/b-2/c/d in merge (resolve)' '
+ git reset --hard &&
+ git checkout main^0 &&
+ git merge -s resolve baseline^0 &&
+ test_path_is_file a/b-2/c/d
+'
+
+test_expect_success SYMLINKS 'a/b was resolved as symlink' '
+ test -h a/b
+'
+
+test_expect_success 'Handle F/D conflict, do not lose a/b-2/c/d in merge (recursive)' '
+ git reset --hard &&
+ git checkout main^0 &&
+ git merge -s recursive baseline^0 &&
+ test_path_is_file a/b-2/c/d
+'
+
+test_expect_success SYMLINKS 'a/b was resolved as symlink' '
+ test -h a/b
+'
+
+test_expect_failure 'do not lose untracked in merge (resolve)' '
+ git reset --hard &&
+ git checkout baseline^0 &&
+ >a/b/c/e &&
+ test_must_fail git merge -s resolve main &&
+ test_path_is_file a/b/c/e &&
+ test_path_is_file a/b-2/c/d
+'
+
+test_expect_success 'do not lose untracked in merge (recursive)' '
+ git reset --hard &&
+ git checkout baseline^0 &&
+ >a/b/c/e &&
+ test_must_fail git merge -s recursive main &&
+ test_path_is_file a/b/c/e &&
+ test_path_is_file a/b-2/c/d
+'
+
+test_expect_success 'do not lose modifications in merge (resolve)' '
+ git reset --hard &&
+ git checkout baseline^0 &&
+ echo more content >>a/b/c/d &&
+ test_must_fail git merge -s resolve main
+'
+
+test_expect_success 'do not lose modifications in merge (recursive)' '
+ git reset --hard &&
+ git checkout baseline^0 &&
+ echo more content >>a/b/c/d &&
+ test_must_fail git merge -s recursive main
+'
+
+test_expect_success 'setup a merge where dir a/b-2 changed to symlink' '
+ git reset --hard &&
+ git checkout start^0 &&
+ rm -rf a/b-2 &&
+ git add -A &&
+ test_ln_s_add b a/b-2 &&
+ git commit -m "dir a/b-2 to symlink" &&
+ git tag test2
+'
+
+test_expect_success 'merge should not have D/F conflicts (resolve)' '
+ git reset --hard &&
+ git checkout baseline^0 &&
+ git merge -s resolve test2 &&
+ test_path_is_file a/b/c/d
+'
+
+test_expect_success SYMLINKS 'a/b-2 was resolved as symlink' '
+ test -h a/b-2
+'
+
+test_expect_success 'merge should not have D/F conflicts (recursive)' '
+ git reset --hard &&
+ git checkout baseline^0 &&
+ git merge -s recursive test2 &&
+ test_path_is_file a/b/c/d
+'
+
+test_expect_success SYMLINKS 'a/b-2 was resolved as symlink' '
+ test -h a/b-2
+'
+
+test_expect_success 'merge should not have F/D conflicts (recursive)' '
+ git reset --hard &&
+ git checkout -b foo test2 &&
+ git merge -s recursive baseline^0 &&
+ test_path_is_file a/b/c/d
+'
+
+test_expect_success SYMLINKS 'a/b-2 was resolved as symlink' '
+ test -h a/b-2
+'
+
+test_done
diff --git a/t/t6416-recursive-corner-cases.sh b/t/t6416-recursive-corner-cases.sh
new file mode 100755
index 0000000..5f414ab
--- /dev/null
+++ b/t/t6416-recursive-corner-cases.sh
@@ -0,0 +1,1886 @@
+#!/bin/sh
+
+test_description='recursive merge corner cases involving criss-cross merges'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-merge.sh
+
+#
+# L1 L2
+# o---o
+# / \ / \
+# o X ?
+# \ / \ /
+# o---o
+# R1 R2
+#
+
+test_expect_success 'setup basic criss-cross + rename with no modifications' '
+ git init basic-rename &&
+ (
+ cd basic-rename &&
+
+ ten="0 1 2 3 4 5 6 7 8 9" &&
+ printf "line %d in a sample file\n" $ten >one &&
+ printf "line %d in another sample file\n" $ten >two &&
+ git add one two &&
+ test_tick && git commit -m initial &&
+
+ git branch L1 &&
+ git checkout -b R1 &&
+ git mv one three &&
+ test_tick && git commit -m R1 &&
+
+ git checkout L1 &&
+ git mv two three &&
+ test_tick && git commit -m L1 &&
+
+ git checkout L1^0 &&
+ test_tick && git merge -s ours R1 &&
+ git tag L2 &&
+
+ git checkout R1^0 &&
+ test_tick && git merge -s ours L1 &&
+ git tag R2
+ )
+'
+
+test_expect_success 'merge simple rename+criss-cross with no modifications' '
+ (
+ cd basic-rename &&
+
+ git reset --hard &&
+ git checkout L2^0 &&
+
+ test_must_fail git merge -s recursive R2^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 5 out &&
+ git ls-files -u >out &&
+ test_line_count = 3 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >expect \
+ L2:three R2:three &&
+ git rev-parse >actual \
+ :2:three :3:three &&
+ test_cmp expect actual
+ )
+'
+
+#
+# Same as before, but modify L1 slightly:
+#
+# L1m L2
+# o---o
+# / \ / \
+# o X ?
+# \ / \ /
+# o---o
+# R1 R2
+#
+
+test_expect_success 'setup criss-cross + rename merges with basic modification' '
+ git init rename-modify &&
+ (
+ cd rename-modify &&
+
+ ten="0 1 2 3 4 5 6 7 8 9" &&
+ printf "line %d in a sample file\n" $ten >one &&
+ printf "line %d in another sample file\n" $ten >two &&
+ git add one two &&
+ test_tick && git commit -m initial &&
+
+ git branch L1 &&
+ git checkout -b R1 &&
+ git mv one three &&
+ echo more >>two &&
+ git add two &&
+ test_tick && git commit -m R1 &&
+
+ git checkout L1 &&
+ git mv two three &&
+ test_tick && git commit -m L1 &&
+
+ git checkout L1^0 &&
+ test_tick && git merge -s ours R1 &&
+ git tag L2 &&
+
+ git checkout R1^0 &&
+ test_tick && git merge -s ours L1 &&
+ git tag R2
+ )
+'
+
+test_expect_success 'merge criss-cross + rename merges with basic modification' '
+ (
+ cd rename-modify &&
+
+ git checkout L2^0 &&
+
+ test_must_fail git merge -s recursive R2^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 5 out &&
+ git ls-files -u >out &&
+ test_line_count = 3 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >expect \
+ L2:three R2:three &&
+ git rev-parse >actual \
+ :2:three :3:three &&
+ test_cmp expect actual
+ )
+'
+
+#
+# For the next test, we start with three commits in two lines of development
+# which setup a rename/add conflict:
+# Commit A: File 'a' exists
+# Commit B: Rename 'a' -> 'new_a'
+# Commit C: Modify 'a', create different 'new_a'
+# Later, two different people merge and resolve differently:
+# Commit D: Merge B & C, ignoring separately created 'new_a'
+# Commit E: Merge B & C making use of some piece of secondary 'new_a'
+# Finally, someone goes to merge D & E. Does git detect the conflict?
+#
+# B D
+# o---o
+# / \ / \
+# A o X ? F
+# \ / \ /
+# o---o
+# C E
+#
+
+test_expect_success 'setup differently handled merges of rename/add conflict' '
+ git init rename-add &&
+ (
+ cd rename-add &&
+
+ printf "0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n" >a &&
+ git add a &&
+ test_tick && git commit -m A &&
+
+ git branch B &&
+ git checkout -b C &&
+ echo 10 >>a &&
+ test_write_lines 0 1 2 3 4 5 6 7 foobar >new_a &&
+ git add a new_a &&
+ test_tick && git commit -m C &&
+
+ git checkout B &&
+ git mv a new_a &&
+ test_tick && git commit -m B &&
+
+ git checkout B^0 &&
+ test_must_fail git merge C &&
+ git show :2:new_a >new_a &&
+ git add new_a &&
+ test_tick && git commit -m D &&
+ git tag D &&
+
+ git checkout C^0 &&
+ test_must_fail git merge B &&
+ test_write_lines 0 1 2 3 4 5 6 7 bad_merge >new_a &&
+ git add -u &&
+ test_tick && git commit -m E &&
+ git tag E
+ )
+'
+
+test_expect_success 'git detects differently handled merges conflict' '
+ (
+ cd rename-add &&
+
+ git checkout D^0 &&
+
+ test_must_fail git merge -s recursive E^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 3 out &&
+ git ls-files -u >out &&
+ test_line_count = 3 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git cat-file -p C:new_a >ours &&
+ git cat-file -p C:a >theirs &&
+ >empty &&
+ test_must_fail git merge-file \
+ -L "Temporary merge branch 1" \
+ -L "" \
+ -L "Temporary merge branch 2" \
+ ours empty theirs &&
+ sed -e "s/^\([<=>]\)/\1\1\1/" ours >ours-tweaked &&
+ git hash-object ours-tweaked >expect &&
+ git rev-parse >>expect \
+ D:new_a E:new_a &&
+ git rev-parse >actual \
+ :1:new_a :2:new_a :3:new_a &&
+ test_cmp expect actual &&
+
+ # Test that the two-way merge in new_a is as expected
+ git cat-file -p D:new_a >ours &&
+ git cat-file -p E:new_a >theirs &&
+ >empty &&
+ test_must_fail git merge-file \
+ -L "HEAD" \
+ -L "" \
+ -L "E^0" \
+ ours empty theirs &&
+ sed -e "s/^\([<=>]\)/\1\1\1/" ours >expect &&
+ git hash-object new_a >actual &&
+ git hash-object ours >expect &&
+ test_cmp expect actual
+ )
+'
+
+# Repeat the above testcase with precisely the same setup, other than with
+# the two merge bases having different orderings of commit timestamps so
+# that they are reversed in the order they are provided to merge-recursive,
+# so that we can improve code coverage.
+test_expect_success 'git detects differently handled merges conflict, swapped' '
+ (
+ cd rename-add &&
+
+ # Difference #1: Do cleanup from previous testrun
+ git reset --hard &&
+ git clean -fdqx &&
+
+ # Difference #2: Change commit timestamps
+ btime=$(git log --no-walk --date=raw --format=%cd B | awk "{print \$1}") &&
+ ctime=$(git log --no-walk --date=raw --format=%cd C | awk "{print \$1}") &&
+ newctime=$(($btime+1)) &&
+ git fast-export --no-data --all | sed -e s/$ctime/$newctime/ | git fast-import --force --quiet &&
+ # End of most differences; rest is copy-paste of last test,
+ # other than swapping C:a and C:new_a due to order switch
+
+ git checkout D^0 &&
+ test_must_fail git merge -s recursive E^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 3 out &&
+ git ls-files -u >out &&
+ test_line_count = 3 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git cat-file -p C:a >ours &&
+ git cat-file -p C:new_a >theirs &&
+ >empty &&
+ test_must_fail git merge-file \
+ -L "Temporary merge branch 1" \
+ -L "" \
+ -L "Temporary merge branch 2" \
+ ours empty theirs &&
+ sed -e "s/^\([<=>]\)/\1\1\1/" ours >ours-tweaked &&
+ git hash-object ours-tweaked >expect &&
+ git rev-parse >>expect \
+ D:new_a E:new_a &&
+ git rev-parse >actual \
+ :1:new_a :2:new_a :3:new_a &&
+ test_cmp expect actual &&
+
+ # Test that the two-way merge in new_a is as expected
+ git cat-file -p D:new_a >ours &&
+ git cat-file -p E:new_a >theirs &&
+ >empty &&
+ test_must_fail git merge-file \
+ -L "HEAD" \
+ -L "" \
+ -L "E^0" \
+ ours empty theirs &&
+ sed -e "s/^\([<=>]\)/\1\1\1/" ours >expect &&
+ git hash-object new_a >actual &&
+ git hash-object ours >expect &&
+ test_cmp expect actual
+ )
+'
+
+#
+# criss-cross + modify/delete:
+#
+# B D
+# o---o
+# / \ / \
+# A o X ? F
+# \ / \ /
+# o---o
+# C E
+#
+# Commit A: file with contents 'A\n'
+# Commit B: file with contents 'B\n'
+# Commit C: file not present
+# Commit D: file with contents 'B\n'
+# Commit E: file not present
+#
+# Merging commits D & E should result in modify/delete conflict.
+
+test_expect_success 'setup criss-cross + modify/delete resolved differently' '
+ git init modify-delete &&
+ (
+ cd modify-delete &&
+
+ echo A >file &&
+ git add file &&
+ test_tick &&
+ git commit -m A &&
+
+ git branch B &&
+ git checkout -b C &&
+ git rm file &&
+ test_tick &&
+ git commit -m C &&
+
+ git checkout B &&
+ echo B >file &&
+ git add file &&
+ test_tick &&
+ git commit -m B &&
+
+ git checkout B^0 &&
+ test_must_fail git merge C &&
+ echo B >file &&
+ git add file &&
+ test_tick &&
+ git commit -m D &&
+ git tag D &&
+
+ git checkout C^0 &&
+ test_must_fail git merge B &&
+ git rm file &&
+ test_tick &&
+ git commit -m E &&
+ git tag E
+ )
+'
+
+test_expect_success 'git detects conflict merging criss-cross+modify/delete' '
+ (
+ cd modify-delete &&
+
+ git checkout D^0 &&
+
+ test_must_fail git merge -s recursive E^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 2 out &&
+ git ls-files -u >out &&
+ test_line_count = 2 out &&
+
+ git rev-parse >expect \
+ main:file B:file &&
+ git rev-parse >actual \
+ :1:file :2:file &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'git detects conflict merging criss-cross+modify/delete, reverse direction' '
+ (
+ cd modify-delete &&
+
+ git reset --hard &&
+ git checkout E^0 &&
+
+ test_must_fail git merge -s recursive D^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 2 out &&
+ git ls-files -u >out &&
+ test_line_count = 2 out &&
+
+ git rev-parse >expect \
+ main:file B:file &&
+ git rev-parse >actual \
+ :1:file :3:file &&
+ test_cmp expect actual
+ )
+'
+
+# SORRY FOR THE SUPER LONG DESCRIPTION, BUT THIS NEXT ONE IS HAIRY
+#
+# criss-cross + d/f conflict via add/add:
+# Commit A: Neither file 'a' nor directory 'a/' exists.
+# Commit B: Introduce 'a'
+# Commit C: Introduce 'a/file'
+# Commit D1: Merge B & C, keeping 'a' and deleting 'a/'
+# Commit E1: Merge B & C, deleting 'a' but keeping 'a/file'
+#
+# B D1 or D2
+# o---o
+# / \ / \
+# A o X ? F
+# \ / \ /
+# o---o
+# C E1 or E2 or E3
+#
+# I'll describe D2, E2, & E3 (which are alternatives for D1 & E1) more below...
+#
+# Merging D1 & E1 requires we first create a virtual merge base X from
+# merging A & B in memory. There are several possibilities for the merge-base:
+# 1: Keep both 'a' and 'a/file' (assuming crazy filesystem allowing a tree
+# with a directory and file at same path): results in merge of D1 & E1
+# being clean with both files deleted. Bad (no conflict detected).
+# 2: Keep 'a' but not 'a/file': Merging D1 & E1 is clean and matches E1. Bad.
+# 3: Keep 'a/file' but not 'a': Merging D1 & E1 is clean and matches D1. Bad.
+# 4: Keep neither file: Merging D1 & E1 reports the D/F add/add conflict.
+#
+# So 4 sounds good for this case, but if we were to merge D1 & E3, where E3
+# is defined as:
+# Commit E3: Merge B & C, keeping modified a, and deleting a/
+# then we'd get an add/add conflict for 'a', which seems suboptimal. A little
+# creativity leads us to an alternate choice:
+# 5: Keep 'a' as 'a~$UNIQUE' and a/file; results:
+# Merge D1 & E1: rename/delete conflict for 'a'; a/file silently deleted
+# Merge D1 & E3 is clean, as expected.
+#
+# So choice 5 at least provides some kind of conflict for the original case,
+# and can merge cleanly as expected with D1 and E3. It also made things just
+# slightly funny for merging D1 and E4, where E4 is defined as:
+# Commit E4: Merge B & C, modifying 'a' and renaming to 'a2', and deleting 'a/'
+# in this case, we'll get a rename/rename(1to2) conflict because a~$UNIQUE
+# gets renamed to 'a' in D1 and to 'a2' in E4. But that's better than having
+# two files (both 'a' and 'a2') sitting around without the user being notified
+# that we could detect they were related and need to be merged. Also, choice
+# 5 makes the handling of 'a/file' seem suboptimal. What if we were to merge
+# D2 and E4, where D2 is:
+# Commit D2: Merge B & C, renaming 'a'->'a2', keeping 'a/file'
+# This would result in a clean merge with 'a2' having three-way merged
+# contents (good), and deleting 'a/' (bad) -- it doesn't detect the
+# conflict in how the different sides treated a/file differently.
+# Continuing down the creative route:
+# 6: Keep 'a' as 'a~$UNIQUE1' and keep 'a/' as 'a~$UNIQUE2/'; results:
+# Merge D1 & E1: rename/delete conflict for 'a' and each path under 'a/'.
+# Merge D1 & E3: clean, as expected.
+# Merge D1 & E4: rename/rename(1to2) conflict on 'a' vs 'a2'.
+# Merge D2 & E4: clean for 'a2', rename/delete for a/file
+#
+# Choice 6 could cause rename detection to take longer (providing more targets
+# that need to be searched). Also, the conflict message for each path under
+# 'a/' might be annoying unless we can detect it at the directory level, print
+# it once, and then suppress it for individual filepaths underneath.
+#
+#
+# As of time of writing, git uses choice 5. Directory rename detection and
+# rename detection performance improvements might make choice 6 a desirable
+# improvement. But we can at least document where we fall short for now...
+#
+#
+# Historically, this testcase also used:
+# Commit E2: Merge B & C, deleting 'a' but keeping slightly modified 'a/file'
+# The merge of D1 & E2 is very similar to D1 & E1 -- it has similar issues for
+# path 'a', but should always result in a modify/delete conflict for path
+# 'a/file'. These tests ran the two merges
+# D1 & E1
+# D1 & E2
+# in both directions, to check for directional issues with D/F conflict
+# handling. Later we added
+# D1 & E3
+# D1 & E4
+# D2 & E4
+# for good measure, though we only ran those one way because we had pretty
+# good confidence in merge-recursive's directional handling of D/F issues.
+#
+# Just to summarize all the intermediate merge commits:
+# Commit D1: Merge B & C, keeping a and deleting a/
+# Commit D2: Merge B & C, renaming a->a2, keeping a/file
+# Commit E1: Merge B & C, deleting a but keeping a/file
+# Commit E2: Merge B & C, deleting a but keeping slightly modified a/file
+# Commit E3: Merge B & C, keeping modified a, and deleting a/
+# Commit E4: Merge B & C, modifying 'a' and renaming to 'a2', and deleting 'a/'
+#
+
+test_expect_success 'setup differently handled merges of directory/file conflict' '
+ git init directory-file &&
+ (
+ cd directory-file &&
+
+ >ignore-me &&
+ git add ignore-me &&
+ test_tick &&
+ git commit -m A &&
+ git tag A &&
+
+ git branch B &&
+ git checkout -b C &&
+ mkdir a &&
+ test_write_lines a b c d e f g >a/file &&
+ git add a/file &&
+ test_tick &&
+ git commit -m C &&
+
+ git checkout B &&
+ test_write_lines 1 2 3 4 5 6 7 >a &&
+ git add a &&
+ test_tick &&
+ git commit -m B &&
+
+ git checkout B^0 &&
+ git merge -s ours -m D1 C^0 &&
+ git tag D1 &&
+
+ git checkout B^0 &&
+ test_must_fail git merge C^0 &&
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ git rm -rf a/ &&
+ git rm a~HEAD
+ else
+ git clean -fd &&
+ git rm -rf a/ &&
+ git rm a
+ fi &&
+ git cat-file -p B:a >a2 &&
+ git add a2 &&
+ git commit -m D2 &&
+ git tag D2 &&
+
+ git checkout C^0 &&
+ git merge -s ours -m E1 B^0 &&
+ git tag E1 &&
+
+ git checkout C^0 &&
+ git merge -s ours -m E2 B^0 &&
+ test_write_lines a b c d e f g h >a/file &&
+ git add a/file &&
+ git commit --amend -C HEAD &&
+ git tag E2 &&
+
+ git checkout C^0 &&
+ test_must_fail git merge B^0 &&
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ git rm a~B^0
+ else
+ git clean -fd
+ fi &&
+ git rm -rf a/ &&
+ test_write_lines 1 2 3 4 5 6 7 8 >a &&
+ git add a &&
+ git commit -m E3 &&
+ git tag E3 &&
+
+ git checkout C^0 &&
+ test_must_fail git merge B^0 &&
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ git rm -rf a/ &&
+ git rm a~B^0
+ else
+ git clean -fd &&
+ git rm -rf a/ &&
+ git rm a
+ fi &&
+ test_write_lines 1 2 3 4 5 6 7 8 >a2 &&
+ git add a2 &&
+ git commit -m E4 &&
+ git tag E4
+ )
+'
+
+test_expect_success 'merge of D1 & E1 fails but has appropriate contents' '
+ test_when_finished "git -C directory-file reset --hard" &&
+ test_when_finished "git -C directory-file clean -fdqx" &&
+ (
+ cd directory-file &&
+
+ git checkout D1^0 &&
+
+ test_must_fail git merge -s recursive E1^0 &&
+
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ git ls-files -s >out &&
+ test_line_count = 3 out &&
+ git ls-files -u >out &&
+ test_line_count = 2 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >expect \
+ A:ignore-me B:a D1:a &&
+ git rev-parse >actual \
+ :0:ignore-me :1:a :2:a &&
+ test_cmp expect actual
+ else
+ git ls-files -s >out &&
+ test_line_count = 2 out &&
+ git ls-files -u >out &&
+ test_line_count = 1 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >expect \
+ A:ignore-me B:a &&
+ git rev-parse >actual \
+ :0:ignore-me :2:a &&
+ test_cmp expect actual
+ fi
+ )
+'
+
+test_expect_success 'merge of E1 & D1 fails but has appropriate contents' '
+ test_when_finished "git -C directory-file reset --hard" &&
+ test_when_finished "git -C directory-file clean -fdqx" &&
+ (
+ cd directory-file &&
+
+ git checkout E1^0 &&
+
+ test_must_fail git merge -s recursive D1^0 &&
+
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ git ls-files -s >out &&
+ test_line_count = 3 out &&
+ git ls-files -u >out &&
+ test_line_count = 2 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >expect \
+ A:ignore-me B:a D1:a &&
+ git rev-parse >actual \
+ :0:ignore-me :1:a :3:a &&
+ test_cmp expect actual
+ else
+ git ls-files -s >out &&
+ test_line_count = 2 out &&
+ git ls-files -u >out &&
+ test_line_count = 1 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >expect \
+ A:ignore-me B:a &&
+ git rev-parse >actual \
+ :0:ignore-me :3:a &&
+ test_cmp expect actual
+ fi
+ )
+'
+
+test_expect_success 'merge of D1 & E2 fails but has appropriate contents' '
+ test_when_finished "git -C directory-file reset --hard" &&
+ test_when_finished "git -C directory-file clean -fdqx" &&
+ (
+ cd directory-file &&
+
+ git checkout D1^0 &&
+
+ test_must_fail git merge -s recursive E2^0 &&
+
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ git ls-files -s >out &&
+ test_line_count = 5 out &&
+ git ls-files -u >out &&
+ test_line_count = 4 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >expect \
+ B:a D1:a E2:a/file C:a/file A:ignore-me &&
+ git rev-parse >actual \
+ :1:a~HEAD :2:a~HEAD :3:a/file :1:a/file :0:ignore-me
+ else
+ git ls-files -s >out &&
+ test_line_count = 4 out &&
+ git ls-files -u >out &&
+ test_line_count = 3 out &&
+ git ls-files -o >out &&
+ test_line_count = 2 out &&
+
+ git rev-parse >expect \
+ B:a E2:a/file C:a/file A:ignore-me &&
+ git rev-parse >actual \
+ :2:a :3:a/file :1:a/file :0:ignore-me
+ fi &&
+ test_cmp expect actual &&
+
+ test_path_is_file a~HEAD
+ )
+'
+
+test_expect_success 'merge of E2 & D1 fails but has appropriate contents' '
+ test_when_finished "git -C directory-file reset --hard" &&
+ test_when_finished "git -C directory-file clean -fdqx" &&
+ (
+ cd directory-file &&
+
+ git checkout E2^0 &&
+
+ test_must_fail git merge -s recursive D1^0 &&
+
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ git ls-files -s >out &&
+ test_line_count = 5 out &&
+ git ls-files -u >out &&
+ test_line_count = 4 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >expect \
+ B:a D1:a E2:a/file C:a/file A:ignore-me &&
+ git rev-parse >actual \
+ :1:a~D1^0 :3:a~D1^0 :2:a/file :1:a/file :0:ignore-me
+ else
+ git ls-files -s >out &&
+ test_line_count = 4 out &&
+ git ls-files -u >out &&
+ test_line_count = 3 out &&
+ git ls-files -o >out &&
+ test_line_count = 2 out &&
+
+ git rev-parse >expect \
+ B:a E2:a/file C:a/file A:ignore-me &&
+ git rev-parse >actual \
+ :3:a :2:a/file :1:a/file :0:ignore-me
+ fi &&
+ test_cmp expect actual &&
+
+ test_path_is_file a~D1^0
+ )
+'
+
+test_expect_success 'merge of D1 & E3 succeeds' '
+ test_when_finished "git -C directory-file reset --hard" &&
+ test_when_finished "git -C directory-file clean -fdqx" &&
+ (
+ cd directory-file &&
+
+ git checkout D1^0 &&
+
+ git merge -s recursive E3^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 2 out &&
+ git ls-files -u >out &&
+ test_line_count = 0 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >expect \
+ A:ignore-me E3:a &&
+ git rev-parse >actual \
+ :0:ignore-me :0:a &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_merge_algorithm failure success 'merge of D1 & E4 puts merge of a and a2 in both a and a2' '
+ test_when_finished "git -C directory-file reset --hard" &&
+ test_when_finished "git -C directory-file clean -fdqx" &&
+ (
+ cd directory-file &&
+
+ git checkout D1^0 &&
+
+ test_must_fail git merge -s recursive E4^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 4 out &&
+ git ls-files -u >out &&
+ test_line_count = 3 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >expect \
+ A:ignore-me B:a E4:a2 E4:a2 &&
+ git rev-parse >actual \
+ :0:ignore-me :1:a~Temporary\ merge\ branch\ 2 :2:a :3:a2 &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_failure 'merge of D2 & E4 merges a2s & reports conflict for a/file' '
+ test_when_finished "git -C directory-file reset --hard" &&
+ test_when_finished "git -C directory-file clean -fdqx" &&
+ (
+ cd directory-file &&
+
+ git checkout D2^0 &&
+
+ test_must_fail git merge -s recursive E4^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 3 out &&
+ git ls-files -u >out &&
+ test_line_count = 1 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >expect \
+ A:ignore-me E4:a2 D2:a/file &&
+ git rev-parse >actual \
+ :0:ignore-me :0:a2 :2:a/file &&
+ test_cmp expect actual
+ )
+'
+
+#
+# criss-cross with rename/rename(1to2)/modify followed by
+# rename/rename(2to1)/modify:
+#
+# B D
+# o---o
+# / \ / \
+# A o X ? F
+# \ / \ /
+# o---o
+# C E
+#
+# Commit A: new file: a
+# Commit B: rename a->b, modifying by adding a line
+# Commit C: rename a->c
+# Commit D: merge B&C, resolving conflict by keeping contents in newname
+# Commit E: merge B&C, resolving conflict similar to D but adding another line
+#
+# There is a conflict merging B & C, but one of filename not of file
+# content. Whoever created D and E chose specific resolutions for that
+# conflict resolution. Now, since: (1) there is no content conflict
+# merging B & C, (2) D does not modify that merged content further, and (3)
+# both D & E resolve the name conflict in the same way, the modification to
+# newname in E should not cause any conflicts when it is merged with D.
+# (Note that this can be accomplished by having the virtual merge base have
+# the merged contents of b and c stored in a file named a, which seems like
+# the most logical choice anyway.)
+#
+# Comment from Junio: I do not necessarily agree with the choice "a", but
+# it feels sound to say "B and C do not agree what the final pathname
+# should be, but we know this content was derived from the common A:a so we
+# use one path whose name is arbitrary in the virtual merge base X between
+# D and E" and then further let the rename detection to notice that that
+# arbitrary path gets renamed between X-D to "newname" and X-E also to
+# "newname" to resolve it as both sides renaming it to the same new
+# name. It is akin to what we do at the content level, i.e. "B and C do not
+# agree what the final contents should be, so we leave the conflict marker
+# but that may cancel out at the final merge stage".
+
+test_expect_success 'setup rename/rename(1to2)/modify followed by what looks like rename/rename(2to1)/modify' '
+ git init rename-squared-squared &&
+ (
+ cd rename-squared-squared &&
+
+ printf "1\n2\n3\n4\n5\n6\n" >a &&
+ git add a &&
+ git commit -m A &&
+ git tag A &&
+
+ git checkout -b B A &&
+ git mv a b &&
+ echo 7 >>b &&
+ git add -u &&
+ git commit -m B &&
+
+ git checkout -b C A &&
+ git mv a c &&
+ git commit -m C &&
+
+ git checkout -q B^0 &&
+ git merge --no-commit -s ours C^0 &&
+ git mv b newname &&
+ git commit -m "Merge commit C^0 into HEAD" &&
+ git tag D &&
+
+ git checkout -q C^0 &&
+ git merge --no-commit -s ours B^0 &&
+ git mv c newname &&
+ printf "7\n8\n" >>newname &&
+ git add -u &&
+ git commit -m "Merge commit B^0 into HEAD" &&
+ git tag E
+ )
+'
+
+test_expect_success 'handle rename/rename(1to2)/modify followed by what looks like rename/rename(2to1)/modify' '
+ (
+ cd rename-squared-squared &&
+
+ git checkout D^0 &&
+
+ git merge -s recursive E^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 1 out &&
+ git ls-files -u >out &&
+ test_line_count = 0 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ test $(git rev-parse HEAD:newname) = $(git rev-parse E:newname)
+ )
+'
+
+#
+# criss-cross with rename/rename(1to2)/add-source + resolvable modify/modify:
+#
+# B D
+# o---o
+# / \ / \
+# A o X ? F
+# \ / \ /
+# o---o
+# C E
+#
+# Commit A: new file: a
+# Commit B: rename a->b
+# Commit C: rename a->c, add different a
+# Commit D: merge B&C, keeping b&c and (new) a modified at beginning
+# Commit E: merge B&C, keeping b&c and (new) a modified at end
+#
+# Merging commits D & E should result in no conflict; doing so correctly
+# requires getting the virtual merge base (from merging B&C) right, handling
+# renaming carefully (both in the virtual merge base and later), and getting
+# content merge handled.
+
+test_expect_success 'setup criss-cross + rename/rename/add-source + modify/modify' '
+ git init rename-rename-add-source &&
+ (
+ cd rename-rename-add-source &&
+
+ printf "lots\nof\nwords\nand\ncontent\n" >a &&
+ git add a &&
+ git commit -m A &&
+ git tag A &&
+
+ git checkout -b B A &&
+ git mv a b &&
+ git commit -m B &&
+
+ git checkout -b C A &&
+ git mv a c &&
+ printf "2\n3\n4\n5\n6\n7\n" >a &&
+ git add a &&
+ git commit -m C &&
+
+ git checkout B^0 &&
+ git merge --no-commit -s ours C^0 &&
+ git checkout C -- a c &&
+ mv a old_a &&
+ echo 1 >a &&
+ cat old_a >>a &&
+ rm old_a &&
+ git add -u &&
+ git commit -m "Merge commit C^0 into HEAD" &&
+ git tag D &&
+
+ git checkout C^0 &&
+ git merge --no-commit -s ours B^0 &&
+ git checkout B -- b &&
+ echo 8 >>a &&
+ git add -u &&
+ git commit -m "Merge commit B^0 into HEAD" &&
+ git tag E
+ )
+'
+
+test_expect_failure 'detect rename/rename/add-source for virtual merge-base' '
+ (
+ cd rename-rename-add-source &&
+
+ git checkout D^0 &&
+
+ git merge -s recursive E^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 3 out &&
+ git ls-files -u >out &&
+ test_line_count = 0 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ printf "1\n2\n3\n4\n5\n6\n7\n8\n" >correct &&
+ git rev-parse >expect \
+ A:a A:a \
+ correct &&
+ git rev-parse >actual \
+ :0:b :0:c &&
+ git hash-object >>actual \
+ a &&
+ test_cmp expect actual
+ )
+'
+
+#
+# criss-cross with rename/rename(1to2)/add-dest + simple modify:
+#
+# B D
+# o---o
+# / \ / \
+# A o X ? F
+# \ / \ /
+# o---o
+# C E
+#
+# Commit A: new file: a
+# Commit B: rename a->b, add c
+# Commit C: rename a->c
+# Commit D: merge B&C, keeping A:a and B:c
+# Commit E: merge B&C, keeping A:a and slightly modified c from B
+#
+# Merging commits D & E should result in no conflict. The virtual merge
+# base of B & C needs to not delete B:c for that to work, though...
+
+test_expect_success 'setup criss-cross+rename/rename/add-dest + simple modify' '
+ git init rename-rename-add-dest &&
+ (
+ cd rename-rename-add-dest &&
+
+ >a &&
+ git add a &&
+ git commit -m A &&
+ git tag A &&
+
+ git checkout -b B A &&
+ git mv a b &&
+ printf "1\n2\n3\n4\n5\n6\n7\n" >c &&
+ git add c &&
+ git commit -m B &&
+
+ git checkout -b C A &&
+ git mv a c &&
+ git commit -m C &&
+
+ git checkout B^0 &&
+ git merge --no-commit -s ours C^0 &&
+ git mv b a &&
+ git commit -m "D is like B but renames b back to a" &&
+ git tag D &&
+
+ git checkout B^0 &&
+ git merge --no-commit -s ours C^0 &&
+ git mv b a &&
+ echo 8 >>c &&
+ git add c &&
+ git commit -m "E like D but has mod in c" &&
+ git tag E
+ )
+'
+
+test_expect_success 'virtual merge base handles rename/rename(1to2)/add-dest' '
+ (
+ cd rename-rename-add-dest &&
+
+ git checkout D^0 &&
+
+ git merge -s recursive E^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 2 out &&
+ git ls-files -u >out &&
+ test_line_count = 0 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >expect \
+ A:a E:c &&
+ git rev-parse >actual \
+ :0:a :0:c &&
+ test_cmp expect actual
+ )
+'
+
+#
+# criss-cross with modify/modify on a symlink:
+#
+# B D
+# o---o
+# / \ / \
+# A o X ? F
+# \ / \ /
+# o---o
+# C E
+#
+# Commit A: simple simlink fickle->lagoon
+# Commit B: redirect fickle->disneyland
+# Commit C: redirect fickle->home
+# Commit D: merge B&C, resolving in favor of B
+# Commit E: merge B&C, resolving in favor of C
+#
+# This is an obvious modify/modify conflict for the symlink 'fickle'. Can
+# git detect it?
+
+test_expect_success 'setup symlink modify/modify' '
+ git init symlink-modify-modify &&
+ (
+ cd symlink-modify-modify &&
+
+ test_ln_s_add lagoon fickle &&
+ git commit -m A &&
+ git tag A &&
+
+ git checkout -b B A &&
+ git rm fickle &&
+ test_ln_s_add disneyland fickle &&
+ git commit -m B &&
+
+ git checkout -b C A &&
+ git rm fickle &&
+ test_ln_s_add home fickle &&
+ git add fickle &&
+ git commit -m C &&
+
+ git checkout -q B^0 &&
+ git merge -s ours -m D C^0 &&
+ git tag D &&
+
+ git checkout -q C^0 &&
+ git merge -s ours -m E B^0 &&
+ git tag E
+ )
+'
+
+test_expect_merge_algorithm failure success 'check symlink modify/modify' '
+ (
+ cd symlink-modify-modify &&
+
+ git checkout D^0 &&
+
+ test_must_fail git merge -s recursive E^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 3 out &&
+ git ls-files -u >out &&
+ test_line_count = 3 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out
+ )
+'
+
+#
+# criss-cross with add/add of a symlink:
+#
+# B D
+# o---o
+# / \ / \
+# A o X ? F
+# \ / \ /
+# o---o
+# C E
+#
+# Commit A: No symlink or path exists yet
+# Commit B: set up symlink: fickle->disneyland
+# Commit C: set up symlink: fickle->home
+# Commit D: merge B&C, resolving in favor of B
+# Commit E: merge B&C, resolving in favor of C
+#
+# This is an obvious add/add conflict for the symlink 'fickle'. Can
+# git detect it?
+
+test_expect_success 'setup symlink add/add' '
+ git init symlink-add-add &&
+ (
+ cd symlink-add-add &&
+
+ touch ignoreme &&
+ git add ignoreme &&
+ git commit -m A &&
+ git tag A &&
+
+ git checkout -b B A &&
+ test_ln_s_add disneyland fickle &&
+ git commit -m B &&
+
+ git checkout -b C A &&
+ test_ln_s_add home fickle &&
+ git add fickle &&
+ git commit -m C &&
+
+ git checkout -q B^0 &&
+ git merge -s ours -m D C^0 &&
+ git tag D &&
+
+ git checkout -q C^0 &&
+ git merge -s ours -m E B^0 &&
+ git tag E
+ )
+'
+
+test_expect_merge_algorithm failure success 'check symlink add/add' '
+ (
+ cd symlink-add-add &&
+
+ git checkout D^0 &&
+
+ test_must_fail git merge -s recursive E^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 3 out &&
+ git ls-files -u >out &&
+ test_line_count = 2 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out
+ )
+'
+
+#
+# criss-cross with modify/modify on a submodule:
+#
+# B D
+# o---o
+# / \ / \
+# A o X ? F
+# \ / \ /
+# o---o
+# C E
+#
+# Commit A: simple submodule repo
+# Commit B: update repo
+# Commit C: update repo differently
+# Commit D: merge B&C, resolving in favor of B
+# Commit E: merge B&C, resolving in favor of C
+#
+# This is an obvious modify/modify conflict for the submodule 'repo'. Can
+# git detect it?
+
+test_expect_success 'setup submodule modify/modify' '
+ git init submodule-modify-modify &&
+ (
+ cd submodule-modify-modify &&
+
+ git init submod &&
+ (
+ cd submod &&
+ touch file-A &&
+ git add file-A &&
+ git commit -m A &&
+ git tag A &&
+
+ git checkout -b B A &&
+ touch file-B &&
+ git add file-B &&
+ git commit -m B &&
+ git tag B &&
+
+ git checkout -b C A &&
+ touch file-C &&
+ git add file-C &&
+ git commit -m C &&
+ git tag C
+ ) &&
+
+ git -C submod reset --hard A &&
+ git add submod &&
+ git commit -m A &&
+ git tag A &&
+
+ git checkout -b B A &&
+ git -C submod reset --hard B &&
+ git add submod &&
+ git commit -m B &&
+
+ git checkout -b C A &&
+ git -C submod reset --hard C &&
+ git add submod &&
+ git commit -m C &&
+
+ git checkout -q B^0 &&
+ git merge -s ours -m D C^0 &&
+ git tag D &&
+
+ git checkout -q C^0 &&
+ git merge -s ours -m E B^0 &&
+ git tag E
+ )
+'
+
+test_expect_merge_algorithm failure success 'check submodule modify/modify' '
+ (
+ cd submodule-modify-modify &&
+
+ git checkout D^0 &&
+
+ test_must_fail git merge -s recursive E^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 3 out &&
+ git ls-files -u >out &&
+ test_line_count = 3 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out
+ )
+'
+
+#
+# criss-cross with add/add on a submodule:
+#
+# B D
+# o---o
+# / \ / \
+# A o X ? F
+# \ / \ /
+# o---o
+# C E
+#
+# Commit A: nothing of note
+# Commit B: introduce submodule repo
+# Commit C: introduce submodule repo at different commit
+# Commit D: merge B&C, resolving in favor of B
+# Commit E: merge B&C, resolving in favor of C
+#
+# This is an obvious add/add conflict for the submodule 'repo'. Can
+# git detect it?
+
+test_expect_success 'setup submodule add/add' '
+ git init submodule-add-add &&
+ (
+ cd submodule-add-add &&
+
+ git init submod &&
+ (
+ cd submod &&
+ touch file-A &&
+ git add file-A &&
+ git commit -m A &&
+ git tag A &&
+
+ git checkout -b B A &&
+ touch file-B &&
+ git add file-B &&
+ git commit -m B &&
+ git tag B &&
+
+ git checkout -b C A &&
+ touch file-C &&
+ git add file-C &&
+ git commit -m C &&
+ git tag C
+ ) &&
+
+ touch irrelevant-file &&
+ git add irrelevant-file &&
+ git commit -m A &&
+ git tag A &&
+
+ git checkout -b B A &&
+ git -C submod reset --hard B &&
+ git add submod &&
+ git commit -m B &&
+
+ git checkout -b C A &&
+ git -C submod reset --hard C &&
+ git add submod &&
+ git commit -m C &&
+
+ git checkout -q B^0 &&
+ git merge -s ours -m D C^0 &&
+ git tag D &&
+
+ git checkout -q C^0 &&
+ git merge -s ours -m E B^0 &&
+ git tag E
+ )
+'
+
+test_expect_merge_algorithm failure success 'check submodule add/add' '
+ (
+ cd submodule-add-add &&
+
+ git checkout D^0 &&
+
+ test_must_fail git merge -s recursive E^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 3 out &&
+ git ls-files -u >out &&
+ test_line_count = 2 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out
+ )
+'
+
+#
+# criss-cross with conflicting entry types:
+#
+# B D
+# o---o
+# / \ / \
+# A o X ? F
+# \ / \ /
+# o---o
+# C E
+#
+# Commit A: nothing of note
+# Commit B: introduce submodule 'path'
+# Commit C: introduce symlink 'path'
+# Commit D: merge B&C, resolving in favor of B
+# Commit E: merge B&C, resolving in favor of C
+#
+# This is an obvious add/add conflict for 'path'. Can git detect it?
+
+test_expect_success 'setup conflicting entry types (submodule vs symlink)' '
+ git init submodule-symlink-add-add &&
+ (
+ cd submodule-symlink-add-add &&
+
+ git init path &&
+ (
+ cd path &&
+ touch file-B &&
+ git add file-B &&
+ git commit -m B &&
+ git tag B
+ ) &&
+
+ touch irrelevant-file &&
+ git add irrelevant-file &&
+ git commit -m A &&
+ git tag A &&
+
+ git checkout -b B A &&
+ git -C path reset --hard B &&
+ git add path &&
+ git commit -m B &&
+
+ git checkout -b C A &&
+ rm -rf path/ &&
+ test_ln_s_add irrelevant-file path &&
+ git commit -m C &&
+
+ git checkout -q B^0 &&
+ git merge -s ours -m D C^0 &&
+ git tag D &&
+
+ git checkout -q C^0 &&
+ git merge -s ours -m E B^0 &&
+ git tag E
+ )
+'
+
+test_expect_merge_algorithm failure success 'check conflicting entry types (submodule vs symlink)' '
+ (
+ cd submodule-symlink-add-add &&
+
+ git checkout D^0 &&
+
+ test_must_fail git merge -s recursive E^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 3 out &&
+ git ls-files -u >out &&
+ test_line_count = 2 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out
+ )
+'
+
+#
+# criss-cross with regular files that have conflicting modes:
+#
+# B D
+# o---o
+# / \ / \
+# A o X ? F
+# \ / \ /
+# o---o
+# C E
+#
+# Commit A: nothing of note
+# Commit B: introduce file source_me.bash, not executable
+# Commit C: introduce file source_me.bash, executable
+# Commit D: merge B&C, resolving in favor of B
+# Commit E: merge B&C, resolving in favor of C
+#
+# This is an obvious add/add mode conflict. Can git detect it?
+
+test_expect_success 'setup conflicting modes for regular file' '
+ git init regular-file-mode-conflict &&
+ (
+ cd regular-file-mode-conflict &&
+
+ touch irrelevant-file &&
+ git add irrelevant-file &&
+ git commit -m A &&
+ git tag A &&
+
+ git checkout -b B A &&
+ echo "command_to_run" >source_me.bash &&
+ git add source_me.bash &&
+ git commit -m B &&
+
+ git checkout -b C A &&
+ echo "command_to_run" >source_me.bash &&
+ git add source_me.bash &&
+ test_chmod +x source_me.bash &&
+ git commit -m C &&
+
+ git checkout -q B^0 &&
+ git merge -s ours -m D C^0 &&
+ git tag D &&
+
+ git checkout -q C^0 &&
+ git merge -s ours -m E B^0 &&
+ git tag E
+ )
+'
+
+test_expect_failure 'check conflicting modes for regular file' '
+ (
+ cd regular-file-mode-conflict &&
+
+ git checkout D^0 &&
+
+ test_must_fail git merge -s recursive E^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 3 out &&
+ git ls-files -u >out &&
+ test_line_count = 2 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out
+ )
+'
+
+# Setup:
+# L1---L2
+# / \ / \
+# main X ?
+# \ / \ /
+# R1---R2
+#
+# Where:
+# main has two files, named 'b' and 'a'
+# branches L1 and R1 both modify each of the two files in conflicting ways
+#
+# L2 is a merge of R1 into L1; more on it later.
+# R2 is a merge of L1 into R1; more on it later.
+#
+# X is an auto-generated merge-base used when merging L2 and R2.
+# since X is a merge of L1 and R1, it has conflicting versions of each file
+#
+# More about L2 and R2:
+# - both resolve the conflicts in 'b' and 'a' differently
+# - L2 renames 'b' to 'm'
+# - R2 renames 'a' to 'm'
+#
+# In the end, in file 'm' we have four different conflicting files (from
+# two versions of 'b' and two of 'a'). In addition, if
+# merge.conflictstyle is diff3, then the base version also has
+# conflict markers of its own, leading to a total of three levels of
+# conflict markers. This is a pretty weird corner case, but we just want
+# to ensure that we handle it as well as practical.
+
+test_expect_success 'setup nested conflicts' '
+ git init nested_conflicts &&
+ (
+ cd nested_conflicts &&
+
+ # Create some related files now
+ printf "Random base content line %d\n" $(test_seq 1 10) >initial &&
+
+ cp initial b_L1 &&
+ cp initial b_R1 &&
+ cp initial b_L2 &&
+ cp initial b_R2 &&
+ cp initial a_L1 &&
+ cp initial a_R1 &&
+ cp initial a_L2 &&
+ cp initial a_R2 &&
+
+ test_write_lines b b_L1 >>b_L1 &&
+ test_write_lines b b_R1 >>b_R1 &&
+ test_write_lines b b_L2 >>b_L2 &&
+ test_write_lines b b_R2 >>b_R2 &&
+ test_write_lines a a_L1 >>a_L1 &&
+ test_write_lines a a_R1 >>a_R1 &&
+ test_write_lines a a_L2 >>a_L2 &&
+ test_write_lines a a_R2 >>a_R2 &&
+
+ # Setup original commit (or merge-base), consisting of
+ # files named "b" and "a"
+ cp initial b &&
+ cp initial a &&
+ echo b >>b &&
+ echo a >>a &&
+ git add b a &&
+ test_tick && git commit -m initial &&
+
+ git branch L &&
+ git branch R &&
+
+ # Handle the left side
+ git checkout L &&
+ mv -f b_L1 b &&
+ mv -f a_L1 a &&
+ git add b a &&
+ test_tick && git commit -m "version L1 of files" &&
+ git tag L1 &&
+
+ # Handle the right side
+ git checkout R &&
+ mv -f b_R1 b &&
+ mv -f a_R1 a &&
+ git add b a &&
+ test_tick && git commit -m "version R1 of files" &&
+ git tag R1 &&
+
+ # Create first merge on left side
+ git checkout L &&
+ test_must_fail git merge R1 &&
+ mv -f b_L2 b &&
+ mv -f a_L2 a &&
+ git add b a &&
+ git mv b m &&
+ test_tick && git commit -m "left merge, rename b->m" &&
+ git tag L2 &&
+
+ # Create first merge on right side
+ git checkout R &&
+ test_must_fail git merge L1 &&
+ mv -f b_R2 b &&
+ mv -f a_R2 a &&
+ git add b a &&
+ git mv a m &&
+ test_tick && git commit -m "right merge, rename a->m" &&
+ git tag R2
+ )
+'
+
+test_expect_success 'check nested conflicts' '
+ (
+ cd nested_conflicts &&
+
+ git clean -f &&
+ MAIN=$(git rev-parse --short main) &&
+ git checkout L2^0 &&
+
+ # Merge must fail; there is a conflict
+ test_must_fail git -c merge.conflictstyle=diff3 merge -s recursive R2^0 &&
+
+ # Make sure the index has the right number of entries
+ git ls-files -s >out &&
+ test_line_count = 2 out &&
+ git ls-files -u >out &&
+ test_line_count = 2 out &&
+ # Ensure we have the correct number of untracked files
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ # Create a and b from virtual merge base X
+ git cat-file -p main:a >base &&
+ git cat-file -p L1:a >ours &&
+ git cat-file -p R1:a >theirs &&
+ test_must_fail git merge-file --diff3 \
+ -L "Temporary merge branch 1" \
+ -L "$MAIN" \
+ -L "Temporary merge branch 2" \
+ ours \
+ base \
+ theirs &&
+ sed -e "s/^\([<|=>]\)/\1\1/" ours >vmb_a &&
+
+ git cat-file -p main:b >base &&
+ git cat-file -p L1:b >ours &&
+ git cat-file -p R1:b >theirs &&
+ test_must_fail git merge-file --diff3 \
+ -L "Temporary merge branch 1" \
+ -L "$MAIN" \
+ -L "Temporary merge branch 2" \
+ ours \
+ base \
+ theirs &&
+ sed -e "s/^\([<|=>]\)/\1\1/" ours >vmb_b &&
+
+ # Compare :2:m to expected values
+ git cat-file -p L2:m >ours &&
+ git cat-file -p R2:b >theirs &&
+ test_must_fail git merge-file --diff3 \
+ -L "HEAD:m" \
+ -L "merged common ancestors:b" \
+ -L "R2^0:b" \
+ ours \
+ vmb_b \
+ theirs &&
+ sed -e "s/^\([<|=>]\)/\1\1/" ours >m_stage_2 &&
+ git cat-file -p :2:m >actual &&
+ test_cmp m_stage_2 actual &&
+
+ # Compare :3:m to expected values
+ git cat-file -p L2:a >ours &&
+ git cat-file -p R2:m >theirs &&
+ test_must_fail git merge-file --diff3 \
+ -L "HEAD:a" \
+ -L "merged common ancestors:a" \
+ -L "R2^0:m" \
+ ours \
+ vmb_a \
+ theirs &&
+ sed -e "s/^\([<|=>]\)/\1\1/" ours >m_stage_3 &&
+ git cat-file -p :3:m >actual &&
+ test_cmp m_stage_3 actual &&
+
+ # Compare m to expected contents
+ >empty &&
+ cp m_stage_2 expected_final_m &&
+ test_must_fail git merge-file --diff3 \
+ -L "HEAD" \
+ -L "merged common ancestors" \
+ -L "R2^0" \
+ expected_final_m \
+ empty \
+ m_stage_3 &&
+ test_cmp expected_final_m m
+ )
+'
+
+# Setup:
+# L1---L2---L3
+# / \ / \ / \
+# main X1 X2 ?
+# \ / \ / \ /
+# R1---R2---R3
+#
+# Where:
+# main has one file named 'content'
+# branches L1 and R1 both modify each of the two files in conflicting ways
+#
+# L<n> (n>1) is a merge of R<n-1> into L<n-1>
+# R<n> (n>1) is a merge of L<n-1> into R<n-1>
+# L<n> and R<n> resolve the conflicts differently.
+#
+# X<n> is an auto-generated merge-base used when merging L<n+1> and R<n+1>.
+# By construction, X1 has conflict markers due to conflicting versions.
+# X2, due to using merge.conflictstyle=3, has nested conflict markers.
+#
+# So, merging R3 into L3 using merge.conflictstyle=3 should show the
+# nested conflict markers from X2 in the base version -- that means we
+# have three levels of conflict markers. Can we distinguish all three?
+
+test_expect_success 'setup virtual merge base with nested conflicts' '
+ git init virtual_merge_base_has_nested_conflicts &&
+ (
+ cd virtual_merge_base_has_nested_conflicts &&
+
+ # Create some related files now
+ printf "Random base content line %d\n" $(test_seq 1 10) >content &&
+
+ # Setup original commit
+ git add content &&
+ test_tick && git commit -m initial &&
+
+ git branch L &&
+ git branch R &&
+
+ # Create L1
+ git checkout L &&
+ echo left >>content &&
+ git add content &&
+ test_tick && git commit -m "version L1 of content" &&
+ git tag L1 &&
+
+ # Create R1
+ git checkout R &&
+ echo right >>content &&
+ git add content &&
+ test_tick && git commit -m "version R1 of content" &&
+ git tag R1 &&
+
+ # Create L2
+ git checkout L &&
+ test_must_fail git -c merge.conflictstyle=diff3 merge R1 &&
+ git checkout L1 content &&
+ test_tick && git commit -m "version L2 of content" &&
+ git tag L2 &&
+
+ # Create R2
+ git checkout R &&
+ test_must_fail git -c merge.conflictstyle=diff3 merge L1 &&
+ git checkout R1 content &&
+ test_tick && git commit -m "version R2 of content" &&
+ git tag R2 &&
+
+ # Create L3
+ git checkout L &&
+ test_must_fail git -c merge.conflictstyle=diff3 merge R2 &&
+ git checkout L1 content &&
+ test_tick && git commit -m "version L3 of content" &&
+ git tag L3 &&
+
+ # Create R3
+ git checkout R &&
+ test_must_fail git -c merge.conflictstyle=diff3 merge L2 &&
+ git checkout R1 content &&
+ test_tick && git commit -m "version R3 of content" &&
+ git tag R3
+ )
+'
+
+test_expect_success 'check virtual merge base with nested conflicts' '
+ (
+ cd virtual_merge_base_has_nested_conflicts &&
+
+ MAIN=$(git rev-parse --short main) &&
+ git checkout L3^0 &&
+
+ # Merge must fail; there is a conflict
+ test_must_fail git -c merge.conflictstyle=diff3 merge -s recursive R3^0 &&
+
+ # Make sure the index has the right number of entries
+ git ls-files -s >out &&
+ test_line_count = 3 out &&
+ git ls-files -u >out &&
+ test_line_count = 3 out &&
+ # Ensure we have the correct number of untracked files
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ # Compare :[23]:content to expected values
+ git rev-parse L1:content R1:content >expect &&
+ git rev-parse :2:content :3:content >actual &&
+ test_cmp expect actual &&
+
+ # Imitate X1 merge base, except without long enough conflict
+ # markers because a subsequent sed will modify them. Put
+ # result into vmb.
+ git cat-file -p main:content >base &&
+ git cat-file -p L:content >left &&
+ git cat-file -p R:content >right &&
+ cp left merged-once &&
+ test_must_fail git merge-file --diff3 \
+ -L "Temporary merge branch 1" \
+ -L "$MAIN" \
+ -L "Temporary merge branch 2" \
+ merged-once \
+ base \
+ right &&
+ sed -e "s/^\([<|=>]\)/\1\1\1/" merged-once >vmb &&
+
+ # Imitate X2 merge base, overwriting vmb. Note that we
+ # extend both sets of conflict markers to make them longer
+ # with the sed command.
+ cp left merged-twice &&
+ test_must_fail git merge-file --diff3 \
+ -L "Temporary merge branch 1" \
+ -L "merged common ancestors" \
+ -L "Temporary merge branch 2" \
+ merged-twice \
+ vmb \
+ right &&
+ sed -e "s/^\([<|=>]\)/\1\1\1/" merged-twice >vmb &&
+
+ # Compare :1:content to expected value
+ git cat-file -p :1:content >actual &&
+ test_cmp vmb actual &&
+
+ # Determine expected content in final outer merge, compare to
+ # what the merge generated.
+ cp -f left expect &&
+ test_must_fail git merge-file --diff3 \
+ -L "HEAD" -L "merged common ancestors" -L "R3^0" \
+ expect vmb right &&
+ test_cmp expect content
+ )
+'
+
+test_done
diff --git a/t/t6417-merge-ours-theirs.sh b/t/t6417-merge-ours-theirs.sh
new file mode 100755
index 0000000..482b73a
--- /dev/null
+++ b/t/t6417-merge-ours-theirs.sh
@@ -0,0 +1,109 @@
+#!/bin/sh
+
+test_description='Merge-recursive ours and theirs variants'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ test_write_lines 1 2 3 4 5 6 7 8 9 >file &&
+ git add file &&
+ cp file elif &&
+ git commit -m initial &&
+
+ sed -e "s/1/one/" -e "s/9/nine/" >file <elif &&
+ git commit -a -m ours &&
+
+ git checkout -b side HEAD^ &&
+
+ sed -e "s/9/nueve/" >file <elif &&
+ git commit -a -m theirs &&
+
+ git checkout main^0
+'
+
+test_expect_success 'plain recursive - should conflict' '
+ git reset --hard main &&
+ test_must_fail git merge -s recursive side &&
+ grep nine file &&
+ grep nueve file &&
+ ! grep 9 file &&
+ grep one file &&
+ ! grep 1 file
+'
+
+test_expect_success 'recursive favouring theirs' '
+ git reset --hard main &&
+ git merge -s recursive -Xtheirs side &&
+ ! grep nine file &&
+ grep nueve file &&
+ ! grep 9 file &&
+ grep one file &&
+ ! grep 1 file
+'
+
+test_expect_success 'recursive favouring ours' '
+ git reset --hard main &&
+ git merge -s recursive -X ours side &&
+ grep nine file &&
+ ! grep nueve file &&
+ ! grep 9 file &&
+ grep one file &&
+ ! grep 1 file
+'
+
+test_expect_success 'binary file with -Xours/-Xtheirs' '
+ echo file binary >.gitattributes &&
+
+ git reset --hard main &&
+ git merge -s recursive -X theirs side &&
+ git diff --exit-code side HEAD -- file &&
+
+ git reset --hard main &&
+ git merge -s recursive -X ours side &&
+ git diff --exit-code main HEAD -- file
+'
+
+test_expect_success 'pull passes -X to underlying merge' '
+ git reset --hard main && git pull --no-rebase -s recursive -Xours . side &&
+ git reset --hard main && git pull --no-rebase -s recursive -X ours . side &&
+ git reset --hard main && git pull --no-rebase -s recursive -Xtheirs . side &&
+ git reset --hard main && git pull --no-rebase -s recursive -X theirs . side &&
+ git reset --hard main && test_must_fail git pull --no-rebase -s recursive -X bork . side
+'
+
+test_expect_success SYMLINKS 'symlink with -Xours/-Xtheirs' '
+ git reset --hard main &&
+ git checkout -b two main &&
+ ln -s target-zero link &&
+ git add link &&
+ git commit -m "add link pointing to zero" &&
+
+ ln -f -s target-two link &&
+ git commit -m "add link pointing to two" link &&
+
+ git checkout -b one HEAD^ &&
+ ln -f -s target-one link &&
+ git commit -m "add link pointing to one" link &&
+
+ # we expect symbolic links not to resolve automatically, of course
+ git checkout one^0 &&
+ test_must_fail git merge -s recursive two &&
+
+ # favor theirs to resolve to target-two?
+ git reset --hard &&
+ git checkout one^0 &&
+ git merge -s recursive -X theirs two &&
+ git diff --exit-code two HEAD link &&
+
+ # favor ours to resolve to target-one?
+ git reset --hard &&
+ git checkout one^0 &&
+ git merge -s recursive -X ours two &&
+ git diff --exit-code one HEAD link
+
+'
+
+test_done
diff --git a/t/t6418-merge-text-auto.sh b/t/t6418-merge-text-auto.sh
new file mode 100755
index 0000000..41288a6
--- /dev/null
+++ b/t/t6418-merge-text-auto.sh
@@ -0,0 +1,233 @@
+#!/bin/sh
+
+test_description='CRLF merge conflict across text=auto change
+
+* [main] remove .gitattributes
+ ! [side] add line from b
+--
+ + [side] add line from b
+* [main] remove .gitattributes
+* [main^] add line from a
+* [main~2] normalize file
+*+ [side^] Initial
+'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_have_prereq SED_STRIPS_CR && SED_OPTIONS=-b
+
+compare_files () {
+ tr '\015\000' QN <"$1" >"$1".expect &&
+ tr '\015\000' QN <"$2" >"$2".actual &&
+ test_cmp "$1".expect "$2".actual &&
+ rm "$1".expect "$2".actual
+}
+
+test_expect_success setup '
+ git config core.autocrlf false &&
+
+ echo first line | append_cr >file &&
+ echo first line >control_file &&
+ echo only line >inert_file &&
+
+ git add file control_file inert_file &&
+ test_tick &&
+ git commit -m "Initial" &&
+ git tag initial &&
+ git branch side &&
+
+ echo "* text=auto" >.gitattributes &&
+ echo first line >file &&
+ git add .gitattributes file &&
+ test_tick &&
+ git commit -m "normalize file" &&
+
+ echo same line | append_cr >>file &&
+ echo same line >>control_file &&
+ git add file control_file &&
+ test_tick &&
+ git commit -m "add line from a" &&
+ git tag a &&
+
+ git rm .gitattributes &&
+ rm file &&
+ git checkout file &&
+ test_tick &&
+ git commit -m "remove .gitattributes" &&
+ git tag c &&
+
+ git checkout side &&
+ echo same line | append_cr >>file &&
+ echo same line >>control_file &&
+ git add file control_file &&
+ test_tick &&
+ git commit -m "add line from b" &&
+ git tag b &&
+
+ git checkout main
+'
+
+test_expect_success 'set up fuzz_conflict() helper' '
+ fuzz_conflict() {
+ sed $SED_OPTIONS -e "s/^\([<>=]......\) .*/\1/" "$@"
+ }
+'
+
+test_expect_success 'Merge after setting text=auto' '
+ cat <<-\EOF >expected &&
+ first line
+ same line
+ EOF
+
+ if test_have_prereq NATIVE_CRLF; then
+ append_cr <expected >expected.temp &&
+ mv expected.temp expected
+ fi &&
+ git config merge.renormalize true &&
+ git rm -fr . &&
+ rm -f .gitattributes &&
+ git reset --hard a &&
+ git merge b &&
+ compare_files expected file
+'
+
+test_expect_success 'Merge addition of text=auto eol=LF' '
+ git config core.eol lf &&
+ cat <<-\EOF >expected &&
+ first line
+ same line
+ EOF
+
+ git config merge.renormalize true &&
+ git rm -fr . &&
+ rm -f .gitattributes &&
+ git reset --hard b &&
+ git merge a &&
+ compare_files expected file
+'
+
+test_expect_success 'Merge addition of text=auto eol=CRLF' '
+ git config core.eol crlf &&
+ cat <<-\EOF >expected &&
+ first line
+ same line
+ EOF
+
+ append_cr <expected >expected.temp &&
+ mv expected.temp expected &&
+ git config merge.renormalize true &&
+ git rm -fr . &&
+ rm -f .gitattributes &&
+ git reset --hard b &&
+ echo >&2 "After git reset --hard b" &&
+ git ls-files -s --eol >&2 &&
+ git merge a &&
+ compare_files expected file
+'
+
+test_expect_success 'Detect CRLF/LF conflict after setting text=auto' '
+ git config core.eol native &&
+ echo "<<<<<<<" >expected &&
+ echo first line >>expected &&
+ echo same line >>expected &&
+ echo ======= >>expected &&
+ echo first line | append_cr >>expected &&
+ echo same line | append_cr >>expected &&
+ echo ">>>>>>>" >>expected &&
+ git config merge.renormalize false &&
+ rm -f .gitattributes &&
+ git reset --hard a &&
+ test_must_fail git merge b &&
+ fuzz_conflict file >file.fuzzy &&
+ compare_files expected file.fuzzy
+'
+
+test_expect_success 'Detect LF/CRLF conflict from addition of text=auto' '
+ echo "<<<<<<<" >expected &&
+ echo first line | append_cr >>expected &&
+ echo same line | append_cr >>expected &&
+ echo ======= >>expected &&
+ echo first line >>expected &&
+ echo same line >>expected &&
+ echo ">>>>>>>" >>expected &&
+ git config merge.renormalize false &&
+ rm -f .gitattributes &&
+ git reset --hard b &&
+ test_must_fail git merge a &&
+ fuzz_conflict file >file.fuzzy &&
+ compare_files expected file.fuzzy
+'
+
+test_expect_success 'checkout -m after setting text=auto' '
+ cat <<-\EOF >expected &&
+ first line
+ same line
+ EOF
+
+ git config merge.renormalize true &&
+ git rm -fr . &&
+ rm -f .gitattributes &&
+ git reset --hard initial &&
+ git restore --source=a -- . &&
+ git checkout -m b &&
+ git diff --no-index --ignore-cr-at-eol expected file
+'
+
+test_expect_success 'checkout -m addition of text=auto' '
+ cat <<-\EOF >expected &&
+ first line
+ same line
+ EOF
+
+ git config merge.renormalize true &&
+ git rm -fr . &&
+ rm -f .gitattributes file &&
+ git reset --hard initial &&
+ git restore --source=b -- . &&
+ git checkout -m a &&
+ git diff --no-index --ignore-cr-at-eol expected file
+'
+
+test_expect_success 'Test delete/normalize conflict' '
+ git checkout -f side &&
+ git rm -fr . &&
+ rm -f .gitattributes &&
+ git reset --hard initial &&
+ git rm file &&
+ git commit -m "remove file" &&
+ git checkout main &&
+ git reset --hard a^ &&
+ git merge side &&
+ test_path_is_missing file
+'
+
+test_expect_success 'rename/delete vs. renormalization' '
+ git init subrepo &&
+ (
+ cd subrepo &&
+ echo foo >oldfile &&
+ git add oldfile &&
+ git commit -m original &&
+
+ git branch rename &&
+ git branch nuke &&
+
+ git checkout rename &&
+ git mv oldfile newfile &&
+ git commit -m renamed &&
+
+ git checkout nuke &&
+ git rm oldfile &&
+ git commit -m deleted &&
+
+ git checkout rename^0 &&
+ test_must_fail git -c merge.renormalize=true merge nuke >out &&
+
+ grep "rename/delete" out
+ )
+'
+
+test_done
diff --git a/t/t6419-merge-ignorecase.sh b/t/t6419-merge-ignorecase.sh
new file mode 100755
index 0000000..b64b75a
--- /dev/null
+++ b/t/t6419-merge-ignorecase.sh
@@ -0,0 +1,56 @@
+#!/bin/sh
+
+test_description='git-merge with case-changing rename on case-insensitive file system'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+if ! test_have_prereq CASE_INSENSITIVE_FS
+then
+ skip_all='skipping case insensitive tests - case sensitive file system'
+ test_done
+fi
+
+test_expect_success 'merge with case-changing rename' '
+ test $(git config core.ignorecase) = true &&
+ >TestCase &&
+ git add TestCase &&
+ git commit -m "add TestCase" &&
+ git tag baseline &&
+ git checkout -b with-camel &&
+ >foo &&
+ git add foo &&
+ git commit -m "intervening commit" &&
+ git checkout main &&
+ git rm TestCase &&
+ >testcase &&
+ git add testcase &&
+ git commit -m "rename to testcase" &&
+ git checkout with-camel &&
+ git merge main -m "merge" &&
+ test_path_is_file testcase
+'
+
+test_expect_success 'merge with case-changing rename on both sides' '
+ git checkout main &&
+ git reset --hard baseline &&
+ git branch -D with-camel &&
+ git checkout -b with-camel &&
+ git mv TestCase testcase &&
+ git commit -m "recase on branch" &&
+ >foo &&
+ git add foo &&
+ git commit -m "intervening commit" &&
+ git checkout main &&
+ git rm TestCase &&
+ >testcase &&
+ git add testcase &&
+ git commit -m "rename to testcase" &&
+ git checkout with-camel &&
+ git merge main -m "merge" &&
+ test_path_is_file testcase
+'
+
+test_done
diff --git a/t/t6421-merge-partial-clone.sh b/t/t6421-merge-partial-clone.sh
new file mode 100755
index 0000000..711b709
--- /dev/null
+++ b/t/t6421-merge-partial-clone.sh
@@ -0,0 +1,440 @@
+#!/bin/sh
+
+test_description="limiting blob downloads when merging with partial clones"
+# Uses a methodology similar to
+# t6042: corner cases with renames but not criss-cross merges
+# t6036: corner cases with both renames and criss-cross merges
+# t6423: directory rename detection
+#
+# The setup for all of them, pictorially, is:
+#
+# A
+# o
+# / \
+# O o ?
+# \ /
+# o
+# B
+#
+# To help make it easier to follow the flow of tests, they have been
+# divided into sections and each test will start with a quick explanation
+# of what commits O, A, and B contain.
+#
+# Notation:
+# z/{b,c} means files z/b and z/c both exist
+# x/d_1 means file x/d exists with content d1. (Purpose of the
+# underscore notation is to differentiate different
+# files that might be renamed into each other's paths.)
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-merge.sh
+
+test_setup_repo () {
+ test -d server && return
+ git init server &&
+ (
+ cd server &&
+
+ git config uploadpack.allowfilter 1 &&
+ git config uploadpack.allowanysha1inwant 1 &&
+
+ mkdir -p general &&
+ test_seq 2 9 >general/leap1 &&
+ cp general/leap1 general/leap2 &&
+ echo leap2 >>general/leap2 &&
+
+ mkdir -p basename &&
+ cp general/leap1 basename/numbers &&
+ cp general/leap1 basename/sequence &&
+ cp general/leap1 basename/values &&
+ echo numbers >>basename/numbers &&
+ echo sequence >>basename/sequence &&
+ echo values >>basename/values &&
+
+ mkdir -p dir/unchanged &&
+ mkdir -p dir/subdir/tweaked &&
+ echo a >dir/subdir/a &&
+ echo b >dir/subdir/b &&
+ echo c >dir/subdir/c &&
+ echo d >dir/subdir/d &&
+ echo e >dir/subdir/e &&
+ cp general/leap1 dir/subdir/Makefile &&
+ echo toplevel makefile >>dir/subdir/Makefile &&
+ echo f >dir/subdir/tweaked/f &&
+ echo g >dir/subdir/tweaked/g &&
+ echo h >dir/subdir/tweaked/h &&
+ echo subdirectory makefile >dir/subdir/tweaked/Makefile &&
+ for i in $(test_seq 1 88)
+ do
+ echo content $i >dir/unchanged/file_$i
+ done &&
+ git add . &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B-single &&
+ git branch B-dir &&
+ git branch B-many &&
+
+ git switch A &&
+
+ git rm general/leap* &&
+ mkdir general/ &&
+ test_seq 1 9 >general/jump1 &&
+ cp general/jump1 general/jump2 &&
+ echo leap2 >>general/jump2 &&
+
+ rm basename/numbers basename/sequence basename/values &&
+ mkdir -p basename/subdir/
+ cp general/jump1 basename/subdir/numbers &&
+ cp general/jump1 basename/subdir/sequence &&
+ cp general/jump1 basename/subdir/values &&
+ echo numbers >>basename/subdir/numbers &&
+ echo sequence >>basename/subdir/sequence &&
+ echo values >>basename/subdir/values &&
+
+ git rm dir/subdir/tweaked/f &&
+ echo more >>dir/subdir/e &&
+ echo more >>dir/subdir/Makefile &&
+ echo more >>dir/subdir/tweaked/Makefile &&
+ mkdir dir/subdir/newsubdir &&
+ echo rust code >dir/subdir/newsubdir/newfile.rs &&
+ git mv dir/subdir/e dir/subdir/newsubdir/ &&
+ git mv dir folder &&
+ git add . &&
+ git commit -m "A" &&
+
+ git switch B-single &&
+ echo new first line >dir/subdir/Makefile &&
+ cat general/leap1 >>dir/subdir/Makefile &&
+ echo toplevel makefile >>dir/subdir/Makefile &&
+ echo perl code >general/newfile.pl &&
+ git add . &&
+ git commit -m "B-single" &&
+
+ git switch B-dir &&
+ echo java code >dir/subdir/newfile.java &&
+ echo scala code >dir/subdir/newfile.scala &&
+ echo groovy code >dir/subdir/newfile.groovy &&
+ git add . &&
+ git commit -m "B-dir" &&
+
+ git switch B-many &&
+ test_seq 2 10 >general/leap1 &&
+ rm general/leap2 &&
+ cp general/leap1 general/leap2 &&
+ echo leap2 >>general/leap2 &&
+
+ rm basename/numbers basename/sequence basename/values &&
+ mkdir -p basename/subdir/
+ cp general/leap1 basename/subdir/numbers &&
+ cp general/leap1 basename/subdir/sequence &&
+ cp general/leap1 basename/subdir/values &&
+ echo numbers >>basename/subdir/numbers &&
+ echo sequence >>basename/subdir/sequence &&
+ echo values >>basename/subdir/values &&
+
+ mkdir dir/subdir/newsubdir/ &&
+ echo c code >dir/subdir/newfile.c &&
+ echo python code >dir/subdir/newsubdir/newfile.py &&
+ git add . &&
+ git commit -m "B-many" &&
+
+ git switch A
+ )
+}
+
+# Testcase: Objects downloaded for single relevant rename
+# Commit O:
+# general/{leap1_O, leap2_O}
+# basename/{numbers_O, sequence_O, values_O}
+# dir/subdir/{a,b,c,d,e_O,Makefile_TOP_O}
+# dir/subdir/tweaked/{f,g,h,Makefile_SUB_O}
+# dir/unchanged/<LOTS OF FILES>
+# Commit A:
+# (Rename leap->jump, rename basename/ -> basename/subdir/, rename dir/
+# -> folder/, move e into newsubdir, add newfile.rs, remove f, modify
+# both Makefiles and jumps)
+# general/{jump1_A, jump2_A}
+# basename/subdir/{numbers_A, sequence_A, values_A}
+# folder/subdir/{a,b,c,d,Makefile_TOP_A}
+# folder/subdir/newsubdir/{e_A,newfile.rs}
+# folder/subdir/tweaked/{g,h,Makefile_SUB_A}
+# folder/unchanged/<LOTS OF FILES>
+# Commit B(-single):
+# (add newfile.pl, tweak Makefile_TOP)
+# general/{leap1_O, leap2_O,newfile.pl}
+# basename/{numbers_O, sequence_O, values_O}
+# dir/{a,b,c,d,e_O,Makefile_TOP_B}
+# dir/tweaked/{f,g,h,Makefile_SUB_O}
+# dir/unchanged/<LOTS OF FILES>
+# Expected:
+# general/{jump1_A, jump2_A,newfile.pl}
+# basename/subdir/{numbers_A, sequence_A, values_A}
+# folder/subdir/{a,b,c,d,Makefile_TOP_Merged}
+# folder/subdir/newsubdir/{e_A,newfile.rs}
+# folder/subdir/tweaked/{g,h,Makefile_SUB_A}
+# folder/unchanged/<LOTS OF FILES>
+#
+# Objects that need to be fetched:
+# Rename detection:
+# Side1 (O->A):
+# Basename-matches rename detection only needs to fetch these objects:
+# Makefile_TOP_O, Makefile_TOP_A
+# (Despite many renames, all others are content irrelevant. They
+# are also location irrelevant because newfile.rs was added on
+# the side doing the directory rename, and newfile.pl was added to
+# a directory that was not renamed on either side.)
+# General rename detection only needs to fetch these objects:
+# <None>
+# (Even though newfile.rs, jump[12], basename/subdir/*, and e
+# could all be used as destinations in rename detection, the
+# basename detection for Makefile matches up all relevant
+# sources, so these other files never end up needing to be
+# used)
+# Side2 (O->B):
+# Basename-matches rename detection only needs to fetch these objects:
+# <None>
+# (there are no deleted files, so no possible sources)
+# General rename detection only needs to fetch these objects:
+# <None>
+# (there are no deleted files, so no possible sources)
+# Merge:
+# 3-way content merge needs to grab these objects:
+# Makefile_TOP_B
+# Nothing else needs to fetch objects
+#
+# Summary: 2 fetches (1 for 2 objects, 1 for 1 object)
+#
+test_expect_merge_algorithm failure success 'Objects downloaded for single relevant rename' '
+ test_setup_repo &&
+ git clone --sparse --filter=blob:none "file://$(pwd)/server" objects-single &&
+ (
+ cd objects-single &&
+
+ git rev-list --objects --all --missing=print |
+ grep "^?" | sort >missing-objects-before &&
+
+ git checkout -q origin/A &&
+
+ GIT_TRACE2_PERF="$(pwd)/trace.output" git \
+ -c merge.directoryRenames=true merge --no-stat \
+ --no-progress origin/B-single &&
+
+ # Check the number of objects we reported we would fetch
+ cat >expect <<-EOF &&
+ fetch_count:2
+ fetch_count:1
+ EOF
+ grep fetch_count trace.output | cut -d "|" -f 9 | tr -d " ." >actual &&
+ test_cmp expect actual &&
+
+ # Check the number of fetch commands exec-ed
+ grep d0.*fetch.negotiationAlgorithm trace.output >fetches &&
+ test_line_count = 2 fetches &&
+
+ git rev-list --objects --all --missing=print |
+ grep "^?" | sort >missing-objects-after &&
+ comm -2 -3 missing-objects-before missing-objects-after >old &&
+ comm -1 -3 missing-objects-before missing-objects-after >new &&
+ # No new missing objects
+ test_must_be_empty new &&
+ # Fetched 2 + 1 = 3 objects
+ test_line_count = 3 old
+ )
+'
+
+# Testcase: Objects downloaded for directory rename
+# Commit O:
+# general/{leap1_O, leap2_O}
+# basename/{numbers_O, sequence_O, values_O}
+# dir/subdir/{a,b,c,d,e_O,Makefile_TOP_O}
+# dir/subdir/tweaked/{f,g,h,Makefile_SUB_O}
+# dir/unchanged/<LOTS OF FILES>
+# Commit A:
+# (Rename leap->jump, rename basename/ -> basename/subdir/, rename dir/ ->
+# folder/, move e into newsubdir, add newfile.rs, remove f, modify
+# both Makefiles and jumps)
+# general/{jump1_A, jump2_A}
+# basename/subdir/{numbers_A, sequence_A, values_A}
+# folder/subdir/{a,b,c,d,Makefile_TOP_A}
+# folder/subdir/newsubdir/{e_A,newfile.rs}
+# folder/subdir/tweaked/{g,h,Makefile_SUB_A}
+# folder/unchanged/<LOTS OF FILES>
+# Commit B(-dir):
+# (add dir/subdir/newfile.{java,scala,groovy}
+# general/{leap1_O, leap2_O}
+# basename/{numbers_O, sequence_O, values_O}
+# dir/subdir/{a,b,c,d,e_O,Makefile_TOP_O,
+# newfile.java,newfile.scala,newfile.groovy}
+# dir/subdir/tweaked/{f,g,h,Makefile_SUB_O}
+# dir/unchanged/<LOTS OF FILES>
+# Expected:
+# general/{jump1_A, jump2_A}
+# basename/subdir/{numbers_A, sequence_A, values_A}
+# folder/subdir/{a,b,c,d,Makefile_TOP_A,
+# newfile.java,newfile.scala,newfile.groovy}
+# folder/subdir/newsubdir/{e_A,newfile.rs}
+# folder/subdir/tweaked/{g,h,Makefile_SUB_A}
+# folder/unchanged/<LOTS OF FILES>
+#
+# Objects that need to be fetched:
+# Makefile_TOP_O, Makefile_TOP_A
+# Makefile_SUB_O, Makefile_SUB_A
+# e_O, e_A
+# * Despite A's rename of jump->leap, those renames are irrelevant.
+# * Despite A's rename of basename/ -> basename/subdir/, those renames are
+# irrelevant.
+# * Because of A's rename of dir/ -> folder/ and B-dir's addition of
+# newfile.* into dir/subdir/, we need to determine directory renames.
+# (Technically, there are enough exact renames to determine directory
+# rename detection, but the current implementation always does
+# basename searching before directory rename detection. Running it
+# also before basename searching would mean doing directory rename
+# detection twice, but it's a bit expensive to do that and cases like
+# this are not all that common.)
+# Summary: 1 fetches for 6 objects
+#
+test_expect_merge_algorithm failure success 'Objects downloaded when a directory rename triggered' '
+ test_setup_repo &&
+ git clone --sparse --filter=blob:none "file://$(pwd)/server" objects-dir &&
+ (
+ cd objects-dir &&
+
+ git rev-list --objects --all --missing=print |
+ grep "^?" | sort >missing-objects-before &&
+
+ git checkout -q origin/A &&
+
+ GIT_TRACE2_PERF="$(pwd)/trace.output" git \
+ -c merge.directoryRenames=true merge --no-stat \
+ --no-progress origin/B-dir &&
+
+ # Check the number of objects we reported we would fetch
+ cat >expect <<-EOF &&
+ fetch_count:6
+ EOF
+ grep fetch_count trace.output | cut -d "|" -f 9 | tr -d " ." >actual &&
+ test_cmp expect actual &&
+
+ # Check the number of fetch commands exec-ed
+ grep d0.*fetch.negotiationAlgorithm trace.output >fetches &&
+ test_line_count = 1 fetches &&
+
+ git rev-list --objects --all --missing=print |
+ grep "^?" | sort >missing-objects-after &&
+ comm -2 -3 missing-objects-before missing-objects-after >old &&
+ comm -1 -3 missing-objects-before missing-objects-after >new &&
+ # No new missing objects
+ test_must_be_empty new &&
+ # Fetched 6 objects
+ test_line_count = 6 old
+ )
+'
+
+# Testcase: Objects downloaded with lots of renames and modifications
+# Commit O:
+# general/{leap1_O, leap2_O}
+# basename/{numbers_O, sequence_O, values_O}
+# dir/subdir/{a,b,c,d,e_O,Makefile_TOP_O}
+# dir/subdir/tweaked/{f,g,h,Makefile_SUB_O}
+# dir/unchanged/<LOTS OF FILES>
+# Commit A:
+# (Rename leap->jump, rename basename/ -> basename/subdir/, rename dir/
+# -> folder/, move e into newsubdir, add newfile.rs, remove f, modify
+# both Makefiles and jumps)
+# general/{jump1_A, jump2_A}
+# basename/subdir/{numbers_A, sequence_A, values_A}
+# folder/subdir/{a,b,c,d,Makefile_TOP_A}
+# folder/subdir/newsubdir/{e_A,newfile.rs}
+# folder/subdir/tweaked/{g,h,Makefile_SUB_A}
+# folder/unchanged/<LOTS OF FILES>
+# Commit B(-minimal):
+# (modify both leaps, rename basename/ -> basename/subdir/, add
+# newfile.{c,py})
+# general/{leap1_B, leap2_B}
+# basename/subdir/{numbers_B, sequence_B, values_B}
+# dir/{a,b,c,d,e_O,Makefile_TOP_O,newfile.c}
+# dir/tweaked/{f,g,h,Makefile_SUB_O,newfile.py}
+# dir/unchanged/<LOTS OF FILES>
+# Expected:
+# general/{jump1_Merged, jump2_Merged}
+# basename/subdir/{numbers_Merged, sequence_Merged, values_Merged}
+# folder/subdir/{a,b,c,d,Makefile_TOP_A,newfile.c}
+# folder/subdir/newsubdir/e_A
+# folder/subdir/tweaked/{g,h,Makefile_SUB_A,newfile.py}
+# folder/unchanged/<LOTS OF FILES>
+#
+# Objects that need to be fetched:
+# Rename detection:
+# Side1 (O->A):
+# Basename-matches rename detection only needs to fetch these objects:
+# numbers_O, numbers_A
+# sequence_O, sequence_A
+# values_O, values_A
+# Makefile_TOP_O, Makefile_TOP_A
+# Makefile_SUB_O, Makefile_SUB_A
+# e_O, e_A
+# General rename detection only needs to fetch these objects:
+# leap1_O, leap2_O
+# jump1_A, jump2_A, newfile.rs
+# (only need remaining relevant sources, but any relevant sources need
+# to be matched against all possible unpaired destinations)
+# Side2 (O->B):
+# Basename-matches rename detection only needs to fetch these objects:
+# numbers_B
+# sequence_B
+# values_B
+# (because numbers_O, sequence_O, and values_O already fetched above)
+# General rename detection only needs to fetch these objects:
+# <None>
+# Merge:
+# 3-way content merge needs to grab these objects:
+# leap1_B
+# leap2_B
+# Nothing else needs to fetch objects
+#
+# Summary: 4 fetches (1 for 6 objects, 1 for 8, 1 for 3, 1 for 2)
+#
+test_expect_merge_algorithm failure success 'Objects downloaded with lots of renames and modifications' '
+ test_setup_repo &&
+ git clone --sparse --filter=blob:none "file://$(pwd)/server" objects-many &&
+ (
+ cd objects-many &&
+
+ git rev-list --objects --all --missing=print |
+ grep "^?" | sort >missing-objects-before &&
+
+ git checkout -q origin/A &&
+
+ GIT_TRACE2_PERF="$(pwd)/trace.output" git \
+ -c merge.directoryRenames=true merge --no-stat \
+ --no-progress origin/B-many &&
+
+ # Check the number of objects we reported we would fetch
+ cat >expect <<-EOF &&
+ fetch_count:12
+ fetch_count:5
+ fetch_count:3
+ fetch_count:2
+ EOF
+ grep fetch_count trace.output | cut -d "|" -f 9 | tr -d " ." >actual &&
+ test_cmp expect actual &&
+
+ # Check the number of fetch commands exec-ed
+ grep d0.*fetch.negotiationAlgorithm trace.output >fetches &&
+ test_line_count = 4 fetches &&
+
+ git rev-list --objects --all --missing=print |
+ grep "^?" | sort >missing-objects-after &&
+ comm -2 -3 missing-objects-before missing-objects-after >old &&
+ comm -1 -3 missing-objects-before missing-objects-after >new &&
+ # No new missing objects
+ test_must_be_empty new &&
+ # Fetched 12 + 5 + 3 + 2 = 22 objects
+ test_line_count = 22 old
+ )
+'
+
+test_done
diff --git a/t/t6422-merge-rename-corner-cases.sh b/t/t6422-merge-rename-corner-cases.sh
new file mode 100755
index 0000000..80d7b5e
--- /dev/null
+++ b/t/t6422-merge-rename-corner-cases.sh
@@ -0,0 +1,1466 @@
+#!/bin/sh
+
+test_description="recursive merge corner cases w/ renames but not criss-crosses"
+# t6036 has corner cases that involve both criss-cross merges and renames
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-merge.sh
+
+test_setup_rename_delete_untracked () {
+ git init rename-delete-untracked &&
+ (
+ cd rename-delete-untracked &&
+
+ echo "A pretty inscription" >ring &&
+ git add ring &&
+ test_tick &&
+ git commit -m beginning &&
+
+ git branch people &&
+ git checkout -b rename-the-ring &&
+ git mv ring one-ring-to-rule-them-all &&
+ test_tick &&
+ git commit -m fullname &&
+
+ git checkout people &&
+ git rm ring &&
+ echo gollum >owner &&
+ git add owner &&
+ test_tick &&
+ git commit -m track-people-instead-of-objects &&
+ echo "Myyy PRECIOUSSS" >ring
+ )
+}
+
+test_expect_success "Does git preserve Gollum's precious artifact?" '
+ test_setup_rename_delete_untracked &&
+ (
+ cd rename-delete-untracked &&
+
+ test_must_fail git merge -s recursive rename-the-ring &&
+
+ # Make sure git did not delete an untracked file
+ test_path_is_file ring
+ )
+'
+
+# Testcase setup for rename/modify/add-source:
+# Commit A: new file: a
+# Commit B: modify a slightly
+# Commit C: rename a->b, add completely different a
+#
+# We should be able to merge B & C cleanly
+
+test_setup_rename_modify_add_source () {
+ git init rename-modify-add-source &&
+ (
+ cd rename-modify-add-source &&
+
+ printf "1\n2\n3\n4\n5\n6\n7\n" >a &&
+ git add a &&
+ git commit -m A &&
+ git tag A &&
+
+ git checkout -b B A &&
+ echo 8 >>a &&
+ git add a &&
+ git commit -m B &&
+
+ git checkout -b C A &&
+ git mv a b &&
+ echo something completely different >a &&
+ git add a &&
+ git commit -m C
+ )
+}
+
+test_expect_failure 'rename/modify/add-source conflict resolvable' '
+ test_setup_rename_modify_add_source &&
+ (
+ cd rename-modify-add-source &&
+
+ git checkout B^0 &&
+
+ git merge -s recursive C^0 &&
+
+ git rev-parse >expect \
+ B:a C:a &&
+ git rev-parse >actual \
+ b c &&
+ test_cmp expect actual
+ )
+'
+
+test_setup_break_detection_1 () {
+ git init break-detection-1 &&
+ (
+ cd break-detection-1 &&
+
+ printf "1\n2\n3\n4\n5\n" >a &&
+ echo foo >b &&
+ git add a b &&
+ git commit -m A &&
+ git tag A &&
+
+ git checkout -b B A &&
+ git mv a c &&
+ echo "Completely different content" >a &&
+ git add a &&
+ git commit -m B &&
+
+ git checkout -b C A &&
+ echo 6 >>a &&
+ git add a &&
+ git commit -m C
+ )
+}
+
+test_expect_failure 'conflict caused if rename not detected' '
+ test_setup_break_detection_1 &&
+ (
+ cd break-detection-1 &&
+
+ git checkout -q C^0 &&
+ git merge -s recursive B^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 3 out &&
+ git ls-files -u >out &&
+ test_line_count = 0 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ test_line_count = 6 c &&
+ git rev-parse >expect \
+ B:a A:b &&
+ git rev-parse >actual \
+ :0:a :0:b &&
+ test_cmp expect actual
+ )
+'
+
+test_setup_break_detection_2 () {
+ git init break-detection-2 &&
+ (
+ cd break-detection-2 &&
+
+ printf "1\n2\n3\n4\n5\n" >a &&
+ echo foo >b &&
+ git add a b &&
+ git commit -m A &&
+ git tag A &&
+
+ git checkout -b D A &&
+ echo 7 >>a &&
+ git add a &&
+ git mv a c &&
+ echo "Completely different content" >a &&
+ git add a &&
+ git commit -m D &&
+
+ git checkout -b E A &&
+ git rm a &&
+ echo "Completely different content" >>a &&
+ git add a &&
+ git commit -m E
+ )
+}
+
+test_expect_failure 'missed conflict if rename not detected' '
+ test_setup_break_detection_2 &&
+ (
+ cd break-detection-2 &&
+
+ git checkout -q E^0 &&
+ test_must_fail git merge -s recursive D^0
+ )
+'
+
+# Tests for undetected rename/add-source causing a file to erroneously be
+# deleted (and for mishandled rename/rename(1to1) causing the same issue).
+#
+# This test uses a rename/rename(1to1)+add-source conflict (1to1 means the
+# same file is renamed on both sides to the same thing; it should trigger
+# the 1to2 logic, which it would do if the add-source didn't cause issues
+# for git's rename detection):
+# Commit A: new file: a
+# Commit B: rename a->b
+# Commit C: rename a->b, add unrelated a
+
+test_setup_break_detection_3 () {
+ git init break-detection-3 &&
+ (
+ cd break-detection-3 &&
+
+ printf "1\n2\n3\n4\n5\n" >a &&
+ git add a &&
+ git commit -m A &&
+ git tag A &&
+
+ git checkout -b B A &&
+ git mv a b &&
+ git commit -m B &&
+
+ git checkout -b C A &&
+ git mv a b &&
+ echo foobar >a &&
+ git add a &&
+ git commit -m C
+ )
+}
+
+test_expect_failure 'detect rename/add-source and preserve all data' '
+ test_setup_break_detection_3 &&
+ (
+ cd break-detection-3 &&
+
+ git checkout B^0 &&
+
+ git merge -s recursive C^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 2 out &&
+ git ls-files -u >out &&
+ test_line_count = 2 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ test_path_is_file a &&
+ test_path_is_file b &&
+
+ git rev-parse >expect \
+ A:a C:a &&
+ git rev-parse >actual \
+ :0:b :0:a &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_failure 'detect rename/add-source and preserve all data, merge other way' '
+ test_setup_break_detection_3 &&
+ (
+ cd break-detection-3 &&
+
+ git checkout C^0 &&
+
+ git merge -s recursive B^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 2 out &&
+ git ls-files -u >out &&
+ test_line_count = 2 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ test_path_is_file a &&
+ test_path_is_file b &&
+
+ git rev-parse >expect \
+ A:a C:a &&
+ git rev-parse >actual \
+ :0:b :0:a &&
+ test_cmp expect actual
+ )
+'
+
+test_setup_rename_directory () {
+ git init rename-directory-$1 &&
+ (
+ cd rename-directory-$1 &&
+
+ printf "1\n2\n3\n4\n5\n6\n" >file &&
+ git add file &&
+ test_tick &&
+ git commit -m base &&
+ git tag base &&
+
+ git checkout -b right &&
+ echo 7 >>file &&
+ mkdir newfile &&
+ echo junk >newfile/realfile &&
+ git add file newfile/realfile &&
+ test_tick &&
+ git commit -m right &&
+
+ git checkout -b left-conflict base &&
+ echo 8 >>file &&
+ git add file &&
+ git mv file newfile &&
+ test_tick &&
+ git commit -m left &&
+
+ git checkout -b left-clean base &&
+ echo 0 >newfile &&
+ cat file >>newfile &&
+ git add newfile &&
+ git rm file &&
+ test_tick &&
+ git commit -m left
+ )
+}
+
+test_expect_success 'rename/directory conflict + clean content merge' '
+ test_setup_rename_directory 1a &&
+ (
+ cd rename-directory-1a &&
+
+ git checkout left-clean^0 &&
+
+ test_must_fail git merge -s recursive right^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 2 out &&
+ git ls-files -u >out &&
+ test_line_count = 1 out &&
+ git ls-files -o >out &&
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ test_line_count = 1 out
+ else
+ test_line_count = 2 out
+ fi &&
+
+ echo 0 >expect &&
+ git cat-file -p base:file >>expect &&
+ echo 7 >>expect &&
+ test_cmp expect newfile~HEAD &&
+
+ test_path_is_file newfile/realfile &&
+ test_path_is_file newfile~HEAD
+ )
+'
+
+test_expect_success 'rename/directory conflict + content merge conflict' '
+ test_setup_rename_directory 1b &&
+ (
+ cd rename-directory-1b &&
+
+ git reset --hard &&
+ git clean -fdqx &&
+
+ git checkout left-conflict^0 &&
+
+ test_must_fail git merge -s recursive right^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 4 out &&
+ git ls-files -u >out &&
+ test_line_count = 3 out &&
+ git ls-files -o >out &&
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ test_line_count = 1 out
+ else
+ test_line_count = 2 out
+ fi &&
+
+ git cat-file -p left-conflict:newfile >left &&
+ git cat-file -p base:file >base &&
+ git cat-file -p right:file >right &&
+ test_must_fail git merge-file \
+ -L "HEAD:newfile" \
+ -L "" \
+ -L "right^0:file" \
+ left base right &&
+ test_cmp left newfile~HEAD &&
+
+ git rev-parse >expect \
+ base:file left-conflict:newfile right:file &&
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ git rev-parse >actual \
+ :1:newfile~HEAD :2:newfile~HEAD :3:newfile~HEAD
+ else
+ git rev-parse >actual \
+ :1:newfile :2:newfile :3:newfile
+ fi &&
+ test_cmp expect actual &&
+
+ test_path_is_file newfile/realfile &&
+ test_path_is_file newfile~HEAD
+ )
+'
+
+test_setup_rename_directory_2 () {
+ git init rename-directory-2 &&
+ (
+ cd rename-directory-2 &&
+
+ mkdir sub &&
+ printf "1\n2\n3\n4\n5\n6\n" >sub/file &&
+ git add sub/file &&
+ test_tick &&
+ git commit -m base &&
+ git tag base &&
+
+ git checkout -b right &&
+ echo 7 >>sub/file &&
+ git add sub/file &&
+ test_tick &&
+ git commit -m right &&
+
+ git checkout -b left base &&
+ echo 0 >newfile &&
+ cat sub/file >>newfile &&
+ git rm sub/file &&
+ mv newfile sub &&
+ git add sub &&
+ test_tick &&
+ git commit -m left
+ )
+}
+
+test_expect_success 'disappearing dir in rename/directory conflict handled' '
+ test_setup_rename_directory_2 &&
+ (
+ cd rename-directory-2 &&
+
+ git checkout left^0 &&
+
+ git merge -s recursive right^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 1 out &&
+ git ls-files -u >out &&
+ test_line_count = 0 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ echo 0 >expect &&
+ git cat-file -p base:sub/file >>expect &&
+ echo 7 >>expect &&
+ test_cmp expect sub &&
+
+ test_path_is_file sub
+ )
+'
+
+# Test for basic rename/add-dest conflict, with rename needing content merge:
+# Commit O: a
+# Commit A: rename a->b, modifying b too
+# Commit B: modify a, add different b
+
+test_setup_rename_with_content_merge_and_add () {
+ git init rename-with-content-merge-and-add-$1 &&
+ (
+ cd rename-with-content-merge-and-add-$1 &&
+
+ test_seq 1 5 >a &&
+ git add a &&
+ git commit -m O &&
+ git tag O &&
+
+ git checkout -b A O &&
+ git mv a b &&
+ test_seq 0 5 >b &&
+ git add b &&
+ git commit -m A &&
+
+ git checkout -b B O &&
+ echo 6 >>a &&
+ echo hello world >b &&
+ git add a b &&
+ git commit -m B
+ )
+}
+
+test_expect_success 'handle rename-with-content-merge vs. add' '
+ test_setup_rename_with_content_merge_and_add AB &&
+ (
+ cd rename-with-content-merge-and-add-AB &&
+
+ git checkout A^0 &&
+
+ test_must_fail git merge -s recursive B^0 >out &&
+ test_grep "CONFLICT (.*/add)" out &&
+
+ git ls-files -s >out &&
+ test_line_count = 2 out &&
+ git ls-files -u >out &&
+ test_line_count = 2 out &&
+ # Also, make sure both unmerged entries are for "b"
+ git ls-files -u b >out &&
+ test_line_count = 2 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ test_path_is_missing a &&
+ test_path_is_file b &&
+
+ test_seq 0 6 >tmp &&
+ git hash-object tmp >expect &&
+ git rev-parse B:b >>expect &&
+ git rev-parse >actual \
+ :2:b :3:b &&
+ test_cmp expect actual &&
+
+ # Test that the two-way merge in b is as expected
+ git cat-file -p :2:b >>ours &&
+ git cat-file -p :3:b >>theirs &&
+ >empty &&
+ test_must_fail git merge-file \
+ -L "HEAD" \
+ -L "" \
+ -L "B^0" \
+ ours empty theirs &&
+ test_cmp ours b
+ )
+'
+
+test_expect_success 'handle rename-with-content-merge vs. add, merge other way' '
+ test_setup_rename_with_content_merge_and_add BA &&
+ (
+ cd rename-with-content-merge-and-add-BA &&
+
+ git reset --hard &&
+ git clean -fdx &&
+
+ git checkout B^0 &&
+
+ test_must_fail git merge -s recursive A^0 >out &&
+ test_grep "CONFLICT (.*/add)" out &&
+
+ git ls-files -s >out &&
+ test_line_count = 2 out &&
+ git ls-files -u >out &&
+ test_line_count = 2 out &&
+ # Also, make sure both unmerged entries are for "b"
+ git ls-files -u b >out &&
+ test_line_count = 2 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ test_path_is_missing a &&
+ test_path_is_file b &&
+
+ test_seq 0 6 >tmp &&
+ git rev-parse B:b >expect &&
+ git hash-object tmp >>expect &&
+ git rev-parse >actual \
+ :2:b :3:b &&
+ test_cmp expect actual &&
+
+ # Test that the two-way merge in b is as expected
+ git cat-file -p :2:b >>ours &&
+ git cat-file -p :3:b >>theirs &&
+ >empty &&
+ test_must_fail git merge-file \
+ -L "HEAD" \
+ -L "" \
+ -L "A^0" \
+ ours empty theirs &&
+ test_cmp ours b
+ )
+'
+
+# Test for all kinds of things that can go wrong with rename/rename (2to1):
+# Commit A: new files: a & b
+# Commit B: rename a->c, modify b
+# Commit C: rename b->c, modify a
+#
+# Merging of B & C should NOT be clean. Questions:
+# * Both a & b should be removed by the merge; are they?
+# * The two c's should contain modifications to a & b; do they?
+# * The index should contain two files, both for c; does it?
+# * The working copy should have two files, both of form c~<unique>; does it?
+# * Nothing else should be present. Is anything?
+
+test_setup_rename_rename_2to1 () {
+ git init rename-rename-2to1 &&
+ (
+ cd rename-rename-2to1 &&
+
+ printf "1\n2\n3\n4\n5\n" >a &&
+ printf "5\n4\n3\n2\n1\n" >b &&
+ git add a b &&
+ git commit -m A &&
+ git tag A &&
+
+ git checkout -b B A &&
+ git mv a c &&
+ echo 0 >>b &&
+ git add b &&
+ git commit -m B &&
+
+ git checkout -b C A &&
+ git mv b c &&
+ echo 6 >>a &&
+ git add a &&
+ git commit -m C
+ )
+}
+
+test_expect_success 'handle rename/rename (2to1) conflict correctly' '
+ test_setup_rename_rename_2to1 &&
+ (
+ cd rename-rename-2to1 &&
+
+ git checkout B^0 &&
+
+ test_must_fail git merge -s recursive C^0 >out &&
+ test_grep "CONFLICT (\(.*\)/\1)" out &&
+
+ git ls-files -s >out &&
+ test_line_count = 2 out &&
+ git ls-files -u >out &&
+ test_line_count = 2 out &&
+ git ls-files -u c >out &&
+ test_line_count = 2 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ test_path_is_missing a &&
+ test_path_is_missing b &&
+
+ git rev-parse >expect \
+ C:a B:b &&
+ git rev-parse >actual \
+ :2:c :3:c &&
+ test_cmp expect actual &&
+
+ # Test that the two-way merge in new_a is as expected
+ git cat-file -p :2:c >>ours &&
+ git cat-file -p :3:c >>theirs &&
+ >empty &&
+ test_must_fail git merge-file \
+ -L "HEAD" \
+ -L "" \
+ -L "C^0" \
+ ours empty theirs &&
+ git hash-object c >actual &&
+ git hash-object ours >expect &&
+ test_cmp expect actual
+ )
+'
+
+# Testcase setup for simple rename/rename (1to2) conflict:
+# Commit A: new file: a
+# Commit B: rename a->b
+# Commit C: rename a->c
+test_setup_rename_rename_1to2 () {
+ git init rename-rename-1to2 &&
+ (
+ cd rename-rename-1to2 &&
+
+ echo stuff >a &&
+ git add a &&
+ test_tick &&
+ git commit -m A &&
+ git tag A &&
+
+ git checkout -b B A &&
+ git mv a b &&
+ test_tick &&
+ git commit -m B &&
+
+ git checkout -b C A &&
+ git mv a c &&
+ test_tick &&
+ git commit -m C
+ )
+}
+
+test_expect_success 'merge has correct working tree contents' '
+ test_setup_rename_rename_1to2 &&
+ (
+ cd rename-rename-1to2 &&
+
+ git checkout C^0 &&
+
+ test_must_fail git merge -s recursive B^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 3 out &&
+ git ls-files -u >out &&
+ test_line_count = 3 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ test_path_is_missing a &&
+ git rev-parse >expect \
+ A:a A:a A:a \
+ A:a A:a &&
+ git rev-parse >actual \
+ :1:a :3:b :2:c &&
+ git hash-object >>actual \
+ b c &&
+ test_cmp expect actual
+ )
+'
+
+# Testcase setup for rename/rename(1to2)/add-source conflict:
+# Commit A: new file: a
+# Commit B: rename a->b
+# Commit C: rename a->c, add completely different a
+#
+# Merging of B & C should NOT be clean; there's a rename/rename conflict
+
+test_setup_rename_rename_1to2_add_source_1 () {
+ git init rename-rename-1to2-add-source-1 &&
+ (
+ cd rename-rename-1to2-add-source-1 &&
+
+ printf "1\n2\n3\n4\n5\n6\n7\n" >a &&
+ git add a &&
+ git commit -m A &&
+ git tag A &&
+
+ git checkout -b B A &&
+ git mv a b &&
+ git commit -m B &&
+
+ git checkout -b C A &&
+ git mv a c &&
+ echo something completely different >a &&
+ git add a &&
+ git commit -m C
+ )
+}
+
+test_expect_failure 'detect conflict with rename/rename(1to2)/add-source merge' '
+ test_setup_rename_rename_1to2_add_source_1 &&
+ (
+ cd rename-rename-1to2-add-source-1 &&
+
+ git checkout B^0 &&
+
+ test_must_fail git merge -s recursive C^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 4 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >expect \
+ C:a A:a B:b C:C &&
+ git rev-parse >actual \
+ :3:a :1:a :2:b :3:c &&
+ test_cmp expect actual &&
+
+ test_path_is_file a &&
+ test_path_is_file b &&
+ test_path_is_file c
+ )
+'
+
+test_setup_rename_rename_1to2_add_source_2 () {
+ git init rename-rename-1to2-add-source-2 &&
+ (
+ cd rename-rename-1to2-add-source-2 &&
+
+ >a &&
+ git add a &&
+ test_tick &&
+ git commit -m base &&
+ git tag A &&
+
+ git checkout -b B A &&
+ git mv a b &&
+ test_tick &&
+ git commit -m one &&
+
+ git checkout -b C A &&
+ git mv a b &&
+ echo important-info >a &&
+ git add a &&
+ test_tick &&
+ git commit -m two
+ )
+}
+
+test_expect_failure 'rename/rename/add-source still tracks new a file' '
+ test_setup_rename_rename_1to2_add_source_2 &&
+ (
+ cd rename-rename-1to2-add-source-2 &&
+
+ git checkout C^0 &&
+ git merge -s recursive B^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 2 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >expect \
+ C:a A:a &&
+ git rev-parse >actual \
+ :0:a :0:b &&
+ test_cmp expect actual
+ )
+'
+
+test_setup_rename_rename_1to2_add_dest () {
+ git init rename-rename-1to2-add-dest &&
+ (
+ cd rename-rename-1to2-add-dest &&
+
+ echo stuff >a &&
+ git add a &&
+ test_tick &&
+ git commit -m base &&
+ git tag A &&
+
+ git checkout -b B A &&
+ git mv a b &&
+ echo precious-data >c &&
+ git add c &&
+ test_tick &&
+ git commit -m one &&
+
+ git checkout -b C A &&
+ git mv a c &&
+ echo important-info >b &&
+ git add b &&
+ test_tick &&
+ git commit -m two
+ )
+}
+
+test_expect_success 'rename/rename/add-dest merge still knows about conflicting file versions' '
+ test_setup_rename_rename_1to2_add_dest &&
+ (
+ cd rename-rename-1to2-add-dest &&
+
+ git checkout C^0 &&
+ test_must_fail git merge -s recursive B^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 5 out &&
+ git ls-files -u b >out &&
+ test_line_count = 2 out &&
+ git ls-files -u c >out &&
+ test_line_count = 2 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >expect \
+ A:a C:b B:b C:c B:c &&
+ git rev-parse >actual \
+ :1:a :2:b :3:b :2:c :3:c &&
+ test_cmp expect actual &&
+
+ # Record some contents for re-doing merges
+ git cat-file -p A:a >stuff &&
+ git cat-file -p C:b >important_info &&
+ git cat-file -p B:c >precious_data &&
+ >empty &&
+
+ # Test the merge in b
+ test_must_fail git merge-file \
+ -L "HEAD" \
+ -L "" \
+ -L "B^0" \
+ important_info empty stuff &&
+ test_cmp important_info b &&
+
+ # Test the merge in c
+ test_must_fail git merge-file \
+ -L "HEAD" \
+ -L "" \
+ -L "B^0" \
+ stuff empty precious_data &&
+ test_cmp stuff c
+ )
+'
+
+# Testcase rad, rename/add/delete
+# Commit O: foo
+# Commit A: rm foo, add different bar
+# Commit B: rename foo->bar
+# Expected: CONFLICT (rename/add/delete), two-way merged bar
+
+test_setup_rad () {
+ git init rad &&
+ (
+ cd rad &&
+ echo "original file" >foo &&
+ git add foo &&
+ git commit -m "original" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git rm foo &&
+ echo "different file" >bar &&
+ git add bar &&
+ git commit -m "Remove foo, add bar" &&
+
+ git checkout B &&
+ git mv foo bar &&
+ git commit -m "rename foo to bar"
+ )
+}
+
+test_expect_merge_algorithm failure success 'rad-check: rename/add/delete conflict' '
+ test_setup_rad &&
+ (
+ cd rad &&
+
+ git checkout B^0 &&
+ test_must_fail git merge -s recursive A^0 >out 2>err &&
+
+ # Instead of requiring the output to contain one combined line
+ # CONFLICT (rename/add/delete)
+ # or perhaps two lines:
+ # CONFLICT (rename/add): new file collides with rename target
+ # CONFLICT (rename/delete): rename source removed on other side
+ # and instead of requiring "rename/add" instead of "add/add",
+ # be flexible in the type of console output message(s) reported
+ # for this particular case; we will be more stringent about the
+ # contents of the index and working directory.
+ test_grep "CONFLICT (.*/add)" out &&
+ test_grep "CONFLICT (rename.*/delete)" out &&
+ test_must_be_empty err &&
+
+ git ls-files -s >file_count &&
+ test_line_count = 2 file_count &&
+ git ls-files -u >file_count &&
+ test_line_count = 2 file_count &&
+ git ls-files -o >file_count &&
+ test_line_count = 3 file_count &&
+
+ git rev-parse >actual \
+ :2:bar :3:bar &&
+ git rev-parse >expect \
+ B:bar A:bar &&
+
+ test_path_is_missing foo &&
+ # bar should have two-way merged contents of the different
+ # versions of bar; check that content from both sides is
+ # present.
+ grep original bar &&
+ grep different bar
+ )
+'
+
+# Testcase rrdd, rename/rename(2to1)/delete/delete
+# Commit O: foo, bar
+# Commit A: rename foo->baz, rm bar
+# Commit B: rename bar->baz, rm foo
+# Expected: CONFLICT (rename/rename/delete/delete), two-way merged baz
+
+test_setup_rrdd () {
+ git init rrdd &&
+ (
+ cd rrdd &&
+ echo foo >foo &&
+ echo bar >bar &&
+ git add foo bar &&
+ git commit -m O &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git mv foo baz &&
+ git rm bar &&
+ git commit -m "Rename foo, remove bar" &&
+
+ git checkout B &&
+ git mv bar baz &&
+ git rm foo &&
+ git commit -m "Rename bar, remove foo"
+ )
+}
+
+test_expect_merge_algorithm failure success 'rrdd-check: rename/rename(2to1)/delete/delete conflict' '
+ test_setup_rrdd &&
+ (
+ cd rrdd &&
+
+ git checkout A^0 &&
+ test_must_fail git merge -s recursive B^0 >out 2>err &&
+
+ # Instead of requiring the output to contain one combined line
+ # CONFLICT (rename/rename/delete/delete)
+ # or perhaps two lines:
+ # CONFLICT (rename/rename): ...
+ # CONFLICT (rename/delete): info about pair 1
+ # CONFLICT (rename/delete): info about pair 2
+ # and instead of requiring "rename/rename" instead of "add/add",
+ # be flexible in the type of console output message(s) reported
+ # for this particular case; we will be more stringent about the
+ # contents of the index and working directory.
+ test_grep "CONFLICT (\(.*\)/\1)" out &&
+ test_grep "CONFLICT (rename.*delete)" out &&
+ test_must_be_empty err &&
+
+ git ls-files -s >file_count &&
+ test_line_count = 2 file_count &&
+ git ls-files -u >file_count &&
+ test_line_count = 2 file_count &&
+ git ls-files -o >file_count &&
+ test_line_count = 3 file_count &&
+
+ git rev-parse >actual \
+ :2:baz :3:baz &&
+ git rev-parse >expect \
+ O:foo O:bar &&
+
+ test_path_is_missing foo &&
+ test_path_is_missing bar &&
+ # baz should have two-way merged contents of the original
+ # contents of foo and bar; check that content from both sides
+ # is present.
+ grep foo baz &&
+ grep bar baz
+ )
+'
+
+# Testcase mod6, chains of rename/rename(1to2) and rename/rename(2to1)
+# Commit O: one, three, five
+# Commit A: one->two, three->four, five->six
+# Commit B: one->six, three->two, five->four
+# Expected: six CONFLICT(rename/rename) messages, each path in two of the
+# multi-way merged contents found in two, four, six
+
+test_setup_mod6 () {
+ git init mod6 &&
+ (
+ cd mod6 &&
+ test_seq 11 19 >one &&
+ test_seq 31 39 >three &&
+ test_seq 51 59 >five &&
+ git add . &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ test_seq 10 19 >one &&
+ echo 40 >>three &&
+ git add one three &&
+ git mv one two &&
+ git mv three four &&
+ git mv five six &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ echo 20 >>one &&
+ echo forty >>three &&
+ echo 60 >>five &&
+ git add one three five &&
+ git mv one six &&
+ git mv three two &&
+ git mv five four &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_merge_algorithm failure success 'mod6-check: chains of rename/rename(1to2) and rename/rename(2to1)' '
+ test_setup_mod6 &&
+ (
+ cd mod6 &&
+
+ git checkout A^0 &&
+
+ test_must_fail git merge -s recursive B^0 >out 2>err &&
+
+ test_grep "CONFLICT (rename/rename)" out &&
+ test_must_be_empty err &&
+
+ git ls-files -s >file_count &&
+ test_line_count = 9 file_count &&
+ git ls-files -u >file_count &&
+ test_line_count = 9 file_count &&
+ git ls-files -o >file_count &&
+ test_line_count = 3 file_count &&
+
+ test_seq 10 20 >merged-one &&
+ test_seq 51 60 >merged-five &&
+ # Determine what the merge of three would give us.
+ test_seq 31 39 >three-base &&
+ test_seq 31 40 >three-side-A &&
+ test_seq 31 39 >three-side-B &&
+ echo forty >>three-side-B &&
+ test_must_fail git merge-file \
+ -L "HEAD:four" \
+ -L "" \
+ -L "B^0:two" \
+ three-side-A three-base three-side-B &&
+ sed -e "s/^\([<=>]\)/\1\1/" three-side-A >merged-three &&
+
+ # Verify the index is as expected
+ git rev-parse >actual \
+ :2:two :3:two \
+ :2:four :3:four \
+ :2:six :3:six &&
+ git hash-object >expect \
+ merged-one merged-three \
+ merged-three merged-five \
+ merged-five merged-one &&
+ test_cmp expect actual &&
+
+ git cat-file -p :2:two >expect &&
+ git cat-file -p :3:two >other &&
+ >empty &&
+ test_must_fail git merge-file \
+ -L "HEAD" -L "" -L "B^0" \
+ expect empty other &&
+ test_cmp expect two &&
+
+ git cat-file -p :2:four >expect &&
+ git cat-file -p :3:four >other &&
+ test_must_fail git merge-file \
+ -L "HEAD" -L "" -L "B^0" \
+ expect empty other &&
+ test_cmp expect four &&
+
+ git cat-file -p :2:six >expect &&
+ git cat-file -p :3:six >other &&
+ test_must_fail git merge-file \
+ -L "HEAD" -L "" -L "B^0" \
+ expect empty other &&
+ test_cmp expect six
+ )
+'
+
+test_conflicts_with_adds_and_renames() {
+ sideL=$1
+ sideR=$2
+
+ # Setup:
+ # L
+ # / \
+ # main ?
+ # \ /
+ # R
+ #
+ # Where:
+ # Both L and R have files named 'three' which collide. Each of
+ # the colliding files could have been involved in a rename, in
+ # which case there was a file named 'one' or 'two' that was
+ # modified on the opposite side of history and renamed into the
+ # collision on this side of history.
+ #
+ # Questions:
+ # 1) The index should contain both a stage 2 and stage 3 entry
+ # for the colliding file. Does it?
+ # 2) When renames are involved, the content merges are clean, so
+ # the index should reflect the content merges, not merely the
+ # version of the colliding file from the prior commit. Does
+ # it?
+ # 3) There should be a file in the worktree named 'three'
+ # containing the two-way merged contents of the content-merged
+ # versions of 'three' from each of the two colliding
+ # files. Is it present?
+ # 4) There should not be any three~* files in the working
+ # tree
+ test_setup_collision_conflict () {
+ git init simple_${sideL}_${sideR} &&
+ (
+ cd simple_${sideL}_${sideR} &&
+
+ # Create some related files now
+ for i in $(test_seq 1 10)
+ do
+ echo Random base content line $i
+ done >file_v1 &&
+ cp file_v1 file_v2 &&
+ echo modification >>file_v2 &&
+
+ cp file_v1 file_v3 &&
+ echo more stuff >>file_v3 &&
+ cp file_v3 file_v4 &&
+ echo yet more stuff >>file_v4 &&
+
+ # Use a tag to record both these files for simple
+ # access, and clean out these untracked files
+ git tag file_v1 $(git hash-object -w file_v1) &&
+ git tag file_v2 $(git hash-object -w file_v2) &&
+ git tag file_v3 $(git hash-object -w file_v3) &&
+ git tag file_v4 $(git hash-object -w file_v4) &&
+ git clean -f &&
+
+ # Setup original commit (or merge-base), consisting of
+ # files named "one" and "two" if renames were involved.
+ touch irrelevant_file &&
+ git add irrelevant_file &&
+ if [ $sideL = "rename" ]
+ then
+ git show file_v1 >one &&
+ git add one
+ fi &&
+ if [ $sideR = "rename" ]
+ then
+ git show file_v3 >two &&
+ git add two
+ fi &&
+ test_tick && git commit -m initial &&
+
+ git branch L &&
+ git branch R &&
+
+ # Handle the left side
+ git checkout L &&
+ if [ $sideL = "rename" ]
+ then
+ git mv one three
+ else
+ git show file_v2 >three &&
+ git add three
+ fi &&
+ if [ $sideR = "rename" ]
+ then
+ git show file_v4 >two &&
+ git add two
+ fi &&
+ test_tick && git commit -m L &&
+
+ # Handle the right side
+ git checkout R &&
+ if [ $sideL = "rename" ]
+ then
+ git show file_v2 >one &&
+ git add one
+ fi &&
+ if [ $sideR = "rename" ]
+ then
+ git mv two three
+ else
+ git show file_v4 >three &&
+ git add three
+ fi &&
+ test_tick && git commit -m R
+ )
+ }
+
+ test_expect_success "check simple $sideL/$sideR conflict" '
+ test_setup_collision_conflict &&
+ (
+ cd simple_${sideL}_${sideR} &&
+
+ git checkout L^0 &&
+
+ # Merge must fail; there is a conflict
+ test_must_fail git merge -s recursive R^0 &&
+
+ # Make sure the index has the right number of entries
+ git ls-files -s >out &&
+ test_line_count = 3 out &&
+ git ls-files -u >out &&
+ test_line_count = 2 out &&
+ # Ensure we have the correct number of untracked files
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ # Nothing should have touched irrelevant_file
+ git rev-parse >actual \
+ :0:irrelevant_file \
+ :2:three \
+ :3:three &&
+ git rev-parse >expected \
+ main:irrelevant_file \
+ file_v2 \
+ file_v4 &&
+ test_cmp expected actual &&
+
+ # Make sure we have the correct merged contents for
+ # three
+ git show file_v1 >expected &&
+ cat <<-\EOF >>expected &&
+ <<<<<<< HEAD
+ modification
+ =======
+ more stuff
+ yet more stuff
+ >>>>>>> R^0
+ EOF
+
+ test_cmp expected three
+ )
+ '
+}
+
+test_conflicts_with_adds_and_renames rename rename
+test_conflicts_with_adds_and_renames rename add
+test_conflicts_with_adds_and_renames add rename
+test_conflicts_with_adds_and_renames add add
+
+# Setup:
+# L
+# / \
+# main ?
+# \ /
+# R
+#
+# Where:
+# main has two files, named 'one' and 'two'.
+# branches L and R both modify 'one', in conflicting ways.
+# branches L and R both modify 'two', in conflicting ways.
+# branch L also renames 'one' to 'three'.
+# branch R also renames 'two' to 'three'.
+#
+# So, we have four different conflicting files that all end up at path
+# 'three'.
+test_setup_nested_conflicts_from_rename_rename () {
+ git init nested_conflicts_from_rename_rename &&
+ (
+ cd nested_conflicts_from_rename_rename &&
+
+ # Create some related files now
+ for i in $(test_seq 1 10)
+ do
+ echo Random base content line $i
+ done >file_v1 &&
+
+ cp file_v1 file_v2 &&
+ cp file_v1 file_v3 &&
+ cp file_v1 file_v4 &&
+ cp file_v1 file_v5 &&
+ cp file_v1 file_v6 &&
+
+ echo one >>file_v1 &&
+ echo uno >>file_v2 &&
+ echo eins >>file_v3 &&
+
+ echo two >>file_v4 &&
+ echo dos >>file_v5 &&
+ echo zwei >>file_v6 &&
+
+ # Setup original commit (or merge-base), consisting of
+ # files named "one" and "two".
+ mv file_v1 one &&
+ mv file_v4 two &&
+ git add one two &&
+ test_tick && git commit -m english &&
+
+ git branch L &&
+ git branch R &&
+
+ # Handle the left side
+ git checkout L &&
+ git rm one two &&
+ mv -f file_v2 three &&
+ mv -f file_v5 two &&
+ git add two three &&
+ test_tick && git commit -m spanish &&
+
+ # Handle the right side
+ git checkout R &&
+ git rm one two &&
+ mv -f file_v3 one &&
+ mv -f file_v6 three &&
+ git add one three &&
+ test_tick && git commit -m german
+ )
+}
+
+test_expect_success 'check nested conflicts from rename/rename(2to1)' '
+ test_setup_nested_conflicts_from_rename_rename &&
+ (
+ cd nested_conflicts_from_rename_rename &&
+
+ git checkout L^0 &&
+
+ # Merge must fail; there is a conflict
+ test_must_fail git merge -s recursive R^0 &&
+
+ # Make sure the index has the right number of entries
+ git ls-files -s >out &&
+ test_line_count = 2 out &&
+ git ls-files -u >out &&
+ test_line_count = 2 out &&
+ # Ensure we have the correct number of untracked files
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ # Compare :2:three to expected values
+ git cat-file -p main:one >base &&
+ git cat-file -p L:three >ours &&
+ git cat-file -p R:one >theirs &&
+ test_must_fail git merge-file \
+ -L "HEAD:three" -L "" -L "R^0:one" \
+ ours base theirs &&
+ sed -e "s/^\([<=>]\)/\1\1/" ours >L-three &&
+ git cat-file -p :2:three >expect &&
+ test_cmp expect L-three &&
+
+ # Compare :2:three to expected values
+ git cat-file -p main:two >base &&
+ git cat-file -p L:two >ours &&
+ git cat-file -p R:three >theirs &&
+ test_must_fail git merge-file \
+ -L "HEAD:two" -L "" -L "R^0:three" \
+ ours base theirs &&
+ sed -e "s/^\([<=>]\)/\1\1/" ours >R-three &&
+ git cat-file -p :3:three >expect &&
+ test_cmp expect R-three &&
+
+ # Compare three to expected contents
+ >empty &&
+ test_must_fail git merge-file \
+ -L "HEAD" -L "" -L "R^0" \
+ L-three empty R-three &&
+ test_cmp three L-three
+ )
+'
+
+# Testcase rename/rename(1to2) of a binary file
+# Commit O: orig
+# Commit A: orig-A
+# Commit B: orig-B
+# Expected: CONFLICT(rename/rename) message, three unstaged entries in the
+# index, and contents of orig-[AB] at path orig-[AB]
+test_setup_rename_rename_1_to_2_binary () {
+ git init rename_rename_1_to_2_binary &&
+ (
+ cd rename_rename_1_to_2_binary &&
+
+ echo '* binary' >.gitattributes &&
+ git add .gitattributes &&
+
+ test_seq 1 10 >orig &&
+ git add orig &&
+ git commit -m orig &&
+
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git mv orig orig-A &&
+ test_seq 1 11 >orig-A &&
+ git add orig-A &&
+ git commit -m orig-A &&
+
+ git checkout B &&
+ git mv orig orig-B &&
+ test_seq 0 10 >orig-B &&
+ git add orig-B &&
+ git commit -m orig-B
+
+ )
+}
+
+test_expect_success 'rename/rename(1to2) with a binary file' '
+ test_setup_rename_rename_1_to_2_binary &&
+ (
+ cd rename_rename_1_to_2_binary &&
+
+ git checkout A^0 &&
+
+ test_must_fail git merge -s recursive B^0 &&
+
+ # Make sure the index has the right number of entries
+ git ls-files -s >actual &&
+ test_line_count = 4 actual &&
+
+ git rev-parse A:orig-A B:orig-B >expect &&
+ git hash-object orig-A orig-B >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_done
diff --git a/t/t6423-merge-rename-directories.sh b/t/t6423-merge-rename-directories.sh
new file mode 100755
index 0000000..88d1cf2
--- /dev/null
+++ b/t/t6423-merge-rename-directories.sh
@@ -0,0 +1,5835 @@
+#!/bin/sh
+
+test_description="recursive merge with directory renames"
+# includes checking of many corner cases, with a similar methodology to:
+# t6042: corner cases with renames but not criss-cross merges
+# t6036: corner cases with both renames and criss-cross merges
+#
+# The setup for all of them, pictorially, is:
+#
+# A
+# o
+# / \
+# O o ?
+# \ /
+# o
+# B
+#
+# To help make it easier to follow the flow of tests, they have been
+# divided into sections and each test will start with a quick explanation
+# of what commits O, A, and B contain.
+#
+# Notation:
+# z/{b,c} means files z/b and z/c both exist
+# x/d_1 means file x/d exists with content d1. (Purpose of the
+# underscore notation is to differentiate different
+# files that might be renamed into each other's paths.)
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-merge.sh
+
+
+###########################################################################
+# SECTION 1: Basic cases we should be able to handle
+###########################################################################
+
+# Testcase 1a, Basic directory rename.
+# Commit O: z/{b,c}
+# Commit A: y/{b,c}
+# Commit B: z/{b,c,d,e/f}
+# Expected: y/{b,c,d,e/f}
+
+test_setup_1a () {
+ git init 1a &&
+ (
+ cd 1a &&
+
+ mkdir z &&
+ echo b >z/b &&
+ echo c >z/c &&
+ git add z &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git mv z y &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ echo d >z/d &&
+ mkdir z/e &&
+ echo f >z/e/f &&
+ git add z/d z/e/f &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '1a: Simple directory rename detection' '
+ test_setup_1a &&
+ (
+ cd 1a &&
+
+ git checkout A^0 &&
+
+ git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
+
+ git ls-files -s >out &&
+ test_line_count = 4 out &&
+
+ git rev-parse >actual \
+ HEAD:y/b HEAD:y/c HEAD:y/d HEAD:y/e/f &&
+ git rev-parse >expect \
+ O:z/b O:z/c B:z/d B:z/e/f &&
+ test_cmp expect actual &&
+
+ git hash-object y/d >actual &&
+ git rev-parse B:z/d >expect &&
+ test_cmp expect actual &&
+
+ test_must_fail git rev-parse HEAD:z/d &&
+ test_must_fail git rev-parse HEAD:z/e/f &&
+ test_path_is_missing z/d &&
+ test_path_is_missing z/e/f
+ )
+'
+
+# Testcase 1b, Merge a directory with another
+# Commit O: z/{b,c}, y/d
+# Commit A: z/{b,c,e}, y/d
+# Commit B: y/{b,c,d}
+# Expected: y/{b,c,d,e}
+
+test_setup_1b () {
+ git init 1b &&
+ (
+ cd 1b &&
+
+ mkdir z &&
+ echo b >z/b &&
+ echo c >z/c &&
+ mkdir y &&
+ echo d >y/d &&
+ git add z y &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ echo e >z/e &&
+ git add z/e &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ git mv z/b y &&
+ git mv z/c y &&
+ rmdir z &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '1b: Merge a directory with another' '
+ test_setup_1b &&
+ (
+ cd 1b &&
+
+ git checkout A^0 &&
+
+ git -c merge.directoryRenames=true merge -s recursive B^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 4 out &&
+
+ git rev-parse >actual \
+ HEAD:y/b HEAD:y/c HEAD:y/d HEAD:y/e &&
+ git rev-parse >expect \
+ O:z/b O:z/c O:y/d A:z/e &&
+ test_cmp expect actual &&
+ test_must_fail git rev-parse HEAD:z/e
+ )
+'
+
+# Testcase 1c, Transitive renaming
+# (Related to testcases 3a and 6d -- when should a transitive rename apply?)
+# (Related to testcases 9c and 9d -- can transitivity repeat?)
+# (Related to testcase 12b -- joint-transitivity?)
+# Commit O: z/{b,c}, x/d
+# Commit A: y/{b,c}, x/d
+# Commit B: z/{b,c,d}
+# Expected: y/{b,c,d} (because x/d -> z/d -> y/d)
+
+test_setup_1c () {
+ git init 1c &&
+ (
+ cd 1c &&
+
+ mkdir z &&
+ echo b >z/b &&
+ echo c >z/c &&
+ mkdir x &&
+ echo d >x/d &&
+ git add z x &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git mv z y &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ git mv x/d z/d &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '1c: Transitive renaming' '
+ test_setup_1c &&
+ (
+ cd 1c &&
+
+ git checkout A^0 &&
+
+ git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
+
+ git ls-files -s >out &&
+ test_line_count = 3 out &&
+
+ git rev-parse >actual \
+ HEAD:y/b HEAD:y/c HEAD:y/d &&
+ git rev-parse >expect \
+ O:z/b O:z/c O:x/d &&
+ test_cmp expect actual &&
+ test_must_fail git rev-parse HEAD:x/d &&
+ test_must_fail git rev-parse HEAD:z/d &&
+ test_path_is_missing z/d
+ )
+'
+
+# Testcase 1d, Directory renames (merging two directories into one new one)
+# cause a rename/rename(2to1) conflict
+# (Related to testcases 1c and 7b)
+# Commit O. z/{b,c}, y/{d,e}
+# Commit A. x/{b,c}, y/{d,e,m,wham_1}
+# Commit B. z/{b,c,n,wham_2}, x/{d,e}
+# Expected: x/{b,c,d,e,m,n}, CONFLICT:(y/wham_1 & z/wham_2 -> x/wham)
+# Note: y/m & z/n should definitely move into x. By the same token, both
+# y/wham_1 & z/wham_2 should too...giving us a conflict.
+
+test_setup_1d () {
+ git init 1d &&
+ (
+ cd 1d &&
+
+ mkdir z &&
+ echo b >z/b &&
+ echo c >z/c &&
+ mkdir y &&
+ echo d >y/d &&
+ echo e >y/e &&
+ git add z y &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git mv z x &&
+ echo m >y/m &&
+ echo wham1 >y/wham &&
+ git add y &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ git mv y x &&
+ echo n >z/n &&
+ echo wham2 >z/wham &&
+ git add z &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '1d: Directory renames cause a rename/rename(2to1) conflict' '
+ test_setup_1d &&
+ (
+ cd 1d &&
+
+ git checkout A^0 &&
+
+ test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
+ test_grep "CONFLICT (\(.*\)/\1)" out &&
+
+ git ls-files -s >out &&
+ test_line_count = 8 out &&
+ git ls-files -u >out &&
+ test_line_count = 2 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >actual \
+ :0:x/b :0:x/c :0:x/d :0:x/e :0:x/m :0:x/n &&
+ git rev-parse >expect \
+ O:z/b O:z/c O:y/d O:y/e A:y/m B:z/n &&
+ test_cmp expect actual &&
+
+ test_must_fail git rev-parse :0:x/wham &&
+ git rev-parse >actual \
+ :2:x/wham :3:x/wham &&
+ git rev-parse >expect \
+ A:y/wham B:z/wham &&
+ test_cmp expect actual &&
+
+ # Test that the two-way merge in x/wham is as expected
+ git cat-file -p :2:x/wham >expect &&
+ git cat-file -p :3:x/wham >other &&
+ >empty &&
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ test_must_fail git merge-file \
+ -L "HEAD:y/wham" \
+ -L "" \
+ -L "B^0:z/wham" \
+ expect empty other
+ else
+ test_must_fail git merge-file \
+ -L "HEAD" \
+ -L "" \
+ -L "B^0" \
+ expect empty other
+ fi &&
+ test_cmp expect x/wham
+ )
+'
+
+# Testcase 1e, Renamed directory, with all filenames being renamed too
+# (Related to testcases 9f & 9g)
+# Commit O: z/{oldb,oldc}
+# Commit A: y/{newb,newc}
+# Commit B: z/{oldb,oldc,d}
+# Expected: y/{newb,newc,d}
+
+test_setup_1e () {
+ git init 1e &&
+ (
+ cd 1e &&
+
+ mkdir z &&
+ echo b >z/oldb &&
+ echo c >z/oldc &&
+ git add z &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ mkdir y &&
+ git mv z/oldb y/newb &&
+ git mv z/oldc y/newc &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ echo d >z/d &&
+ git add z/d &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '1e: Renamed directory, with all files being renamed too' '
+ test_setup_1e &&
+ (
+ cd 1e &&
+
+ git checkout A^0 &&
+
+ git -c merge.directoryRenames=true merge -s recursive B^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 3 out &&
+
+ git rev-parse >actual \
+ HEAD:y/newb HEAD:y/newc HEAD:y/d &&
+ git rev-parse >expect \
+ O:z/oldb O:z/oldc B:z/d &&
+ test_cmp expect actual &&
+ test_must_fail git rev-parse HEAD:z/d
+ )
+'
+
+# Testcase 1f, Split a directory into two other directories
+# (Related to testcases 3a, all of section 2, and all of section 4)
+# Commit O: z/{b,c,d,e,f}
+# Commit A: z/{b,c,d,e,f,g}
+# Commit B: y/{b,c}, x/{d,e,f}
+# Expected: y/{b,c}, x/{d,e,f,g}
+
+test_setup_1f () {
+ git init 1f &&
+ (
+ cd 1f &&
+
+ mkdir z &&
+ echo b >z/b &&
+ echo c >z/c &&
+ echo d >z/d &&
+ echo e >z/e &&
+ echo f >z/f &&
+ git add z &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ echo g >z/g &&
+ git add z/g &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ mkdir y &&
+ mkdir x &&
+ git mv z/b y/ &&
+ git mv z/c y/ &&
+ git mv z/d x/ &&
+ git mv z/e x/ &&
+ git mv z/f x/ &&
+ rmdir z &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '1f: Split a directory into two other directories' '
+ test_setup_1f &&
+ (
+ cd 1f &&
+
+ git checkout A^0 &&
+
+ git -c merge.directoryRenames=true merge -s recursive B^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 6 out &&
+
+ git rev-parse >actual \
+ HEAD:y/b HEAD:y/c HEAD:x/d HEAD:x/e HEAD:x/f HEAD:x/g &&
+ git rev-parse >expect \
+ O:z/b O:z/c O:z/d O:z/e O:z/f A:z/g &&
+ test_cmp expect actual &&
+ test_path_is_missing z/g &&
+ test_must_fail git rev-parse HEAD:z/g
+ )
+'
+
+###########################################################################
+# Rules suggested by testcases in section 1:
+#
+# We should still detect the directory rename even if it wasn't just
+# the directory renamed, but the files within it. (see 1b)
+#
+# If renames split a directory into two or more others, the directory
+# with the most renames, "wins" (see 1f). However, see the testcases
+# in section 2, plus testcases 3a and 4a.
+###########################################################################
+
+
+###########################################################################
+# SECTION 2: Split into multiple directories, with equal number of paths
+#
+# Explore the splitting-a-directory rules a bit; what happens in the
+# edge cases?
+#
+# Note that there is a closely related case of a directory not being
+# split on either side of history, but being renamed differently on
+# each side. See testcase 8e for that.
+###########################################################################
+
+# Testcase 2a, Directory split into two on one side, with equal numbers of paths
+# Commit O: z/{b,c}
+# Commit A: y/b, w/c
+# Commit B: z/{b,c,d}
+# Expected: y/b, w/c, z/d, with warning about z/ -> (y/ vs. w/) conflict
+test_setup_2a () {
+ git init 2a &&
+ (
+ cd 2a &&
+
+ mkdir z &&
+ echo b >z/b &&
+ echo c >z/c &&
+ git add z &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ mkdir y &&
+ mkdir w &&
+ git mv z/b y/ &&
+ git mv z/c w/ &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ echo d >z/d &&
+ git add z/d &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '2a: Directory split into two on one side, with equal numbers of paths' '
+ test_setup_2a &&
+ (
+ cd 2a &&
+
+ git checkout A^0 &&
+
+ test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
+ test_grep "CONFLICT.*directory rename split" out &&
+
+ git ls-files -s >out &&
+ test_line_count = 3 out &&
+ git ls-files -u >out &&
+ test_line_count = 0 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >actual \
+ :0:y/b :0:w/c :0:z/d &&
+ git rev-parse >expect \
+ O:z/b O:z/c B:z/d &&
+ test_cmp expect actual
+ )
+'
+
+# Testcase 2b, Directory split into two on one side, with equal numbers of paths
+# Commit O: z/{b,c}
+# Commit A: y/b, w/c
+# Commit B: z/{b,c}, x/d
+# Expected: y/b, w/c, x/d; No warning about z/ -> (y/ vs. w/) conflict
+test_setup_2b () {
+ git init 2b &&
+ (
+ cd 2b &&
+
+ mkdir z &&
+ echo b >z/b &&
+ echo c >z/c &&
+ git add z &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ mkdir y &&
+ mkdir w &&
+ git mv z/b y/ &&
+ git mv z/c w/ &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ mkdir x &&
+ echo d >x/d &&
+ git add x/d &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '2b: Directory split into two on one side, with equal numbers of paths' '
+ test_setup_2b &&
+ (
+ cd 2b &&
+
+ git checkout A^0 &&
+
+ git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
+
+ git ls-files -s >out &&
+ test_line_count = 3 out &&
+ git ls-files -u >out &&
+ test_line_count = 0 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >actual \
+ :0:y/b :0:w/c :0:x/d &&
+ git rev-parse >expect \
+ O:z/b O:z/c B:x/d &&
+ test_cmp expect actual &&
+ test_grep ! "CONFLICT.*directory rename split" out
+ )
+'
+
+###########################################################################
+# Rules suggested by section 2:
+#
+# None; the rule was already covered in section 1. These testcases are
+# here just to make sure the conflict resolution and necessary warning
+# messages are handled correctly.
+###########################################################################
+
+
+###########################################################################
+# SECTION 3: Path in question is the source path for some rename already
+#
+# Combining cases from Section 1 and trying to handle them could lead to
+# directory renaming detection being over-applied. So, this section
+# provides some good testcases to check that the implementation doesn't go
+# too far.
+###########################################################################
+
+# Testcase 3a, Avoid implicit rename if involved as source on other side
+# (Related to testcases 1c, 1f, and 9h)
+# Commit O: z/{b,c,d}
+# Commit A: z/{b,c,d} (no change)
+# Commit B: y/{b,c}, x/d
+# Expected: y/{b,c}, x/d
+test_setup_3a () {
+ git init 3a &&
+ (
+ cd 3a &&
+
+ mkdir z &&
+ echo b >z/b &&
+ echo c >z/c &&
+ echo d >z/d &&
+ git add z &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ test_tick &&
+ git commit --allow-empty -m "A" &&
+
+ git checkout B &&
+ mkdir y &&
+ mkdir x &&
+ git mv z/b y/ &&
+ git mv z/c y/ &&
+ git mv z/d x/ &&
+ rmdir z &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '3a: Avoid implicit rename if involved as source on other side' '
+ test_setup_3a &&
+ (
+ cd 3a &&
+
+ git checkout A^0 &&
+
+ git -c merge.directoryRenames=true merge -s recursive B^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 3 out &&
+
+ git rev-parse >actual \
+ HEAD:y/b HEAD:y/c HEAD:x/d &&
+ git rev-parse >expect \
+ O:z/b O:z/c O:z/d &&
+ test_cmp expect actual
+ )
+'
+
+# Testcase 3b, Avoid implicit rename if involved as source on other side
+# (Related to testcases 5c and 7c, also kind of 1e and 1f)
+# Commit O: z/{b,c,d}
+# Commit A: y/{b,c}, x/d
+# Commit B: z/{b,c}, w/d
+# Expected: y/{b,c}, CONFLICT:(z/d -> x/d vs. w/d)
+# NOTE: We're particularly checking that since z/d is already involved as
+# a source in a file rename on the same side of history, that we don't
+# get it involved in directory rename detection. If it were, we might
+# end up with CONFLICT:(z/d -> y/d vs. x/d vs. w/d), i.e. a
+# rename/rename/rename(1to3) conflict, which is just weird.
+test_setup_3b () {
+ git init 3b &&
+ (
+ cd 3b &&
+
+ mkdir z &&
+ echo b >z/b &&
+ echo c >z/c &&
+ echo d >z/d &&
+ git add z &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ mkdir y &&
+ mkdir x &&
+ git mv z/b y/ &&
+ git mv z/c y/ &&
+ git mv z/d x/ &&
+ rmdir z &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ mkdir w &&
+ git mv z/d w/ &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '3b: Avoid implicit rename if involved as source on current side' '
+ test_setup_3b &&
+ (
+ cd 3b &&
+
+ git checkout A^0 &&
+
+ test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
+ test_grep CONFLICT.*rename/rename.*z/d.*x/d.*w/d out &&
+ test_grep ! CONFLICT.*rename/rename.*y/d out &&
+
+ git ls-files -s >out &&
+ test_line_count = 5 out &&
+ git ls-files -u >out &&
+ test_line_count = 3 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >actual \
+ :0:y/b :0:y/c :1:z/d :2:x/d :3:w/d &&
+ git rev-parse >expect \
+ O:z/b O:z/c O:z/d O:z/d O:z/d &&
+ test_cmp expect actual &&
+
+ test_path_is_missing z/d &&
+ git hash-object >actual \
+ x/d w/d &&
+ git rev-parse >expect \
+ O:z/d O:z/d &&
+ test_cmp expect actual
+ )
+'
+
+###########################################################################
+# Rules suggested by section 3:
+#
+# Avoid directory-rename-detection for a path, if that path is the source
+# of a rename on either side of a merge.
+###########################################################################
+
+
+###########################################################################
+# SECTION 4: Partially renamed directory; still exists on both sides of merge
+#
+# What if we were to attempt to do directory rename detection when someone
+# "mostly" moved a directory but still left some files around, or,
+# equivalently, fully renamed a directory in one commit and then recreated
+# that directory in a later commit adding some new files and then tried to
+# merge?
+#
+# It's hard to divine user intent in these cases, because you can make an
+# argument that, depending on the intermediate history of the side being
+# merged, that some users will want files in that directory to
+# automatically be detected and renamed, while users with a different
+# intermediate history wouldn't want that rename to happen.
+#
+# I think that it is best to simply not have directory rename detection
+# apply to such cases. My reasoning for this is four-fold: (1) it's
+# easiest for users in general to figure out what happened if we don't
+# apply directory rename detection in any such case, (2) it's an easy rule
+# to explain ["We don't do directory rename detection if the directory
+# still exists on both sides of the merge"], (3) we can get some hairy
+# edge/corner cases that would be really confusing and possibly not even
+# representable in the index if we were to even try, and [related to 3] (4)
+# attempting to resolve this issue of divining user intent by examining
+# intermediate history goes against the spirit of three-way merges and is a
+# path towards crazy corner cases that are far more complex than what we're
+# already dealing with.
+#
+# Note that the wording of the rule ("We don't do directory rename
+# detection if the directory still exists on both sides of the merge.")
+# also excludes "renaming" of a directory into a subdirectory of itself
+# (e.g. /some/dir/* -> /some/dir/subdir/*). It may be possible to carve
+# out an exception for "renaming"-beneath-itself cases without opening
+# weird edge/corner cases for other partial directory renames, but for now
+# we are keeping the rule simple.
+#
+# This section contains a test for a partially-renamed-directory case.
+###########################################################################
+
+# Testcase 4a, Directory split, with original directory still present
+# (Related to testcase 1f)
+# Commit O: z/{b,c,d,e}
+# Commit A: y/{b,c,d}, z/e
+# Commit B: z/{b,c,d,e,f}
+# Expected: y/{b,c,d}, z/{e,f}
+# NOTE: Even though most files from z moved to y, we don't want f to follow.
+
+test_setup_4a () {
+ git init 4a &&
+ (
+ cd 4a &&
+
+ mkdir z &&
+ echo b >z/b &&
+ echo c >z/c &&
+ echo d >z/d &&
+ echo e >z/e &&
+ git add z &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ mkdir y &&
+ git mv z/b y/ &&
+ git mv z/c y/ &&
+ git mv z/d y/ &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ echo f >z/f &&
+ git add z/f &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '4a: Directory split, with original directory still present' '
+ test_setup_4a &&
+ (
+ cd 4a &&
+
+ git checkout A^0 &&
+
+ git -c merge.directoryRenames=true merge -s recursive B^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 5 out &&
+ git ls-files -u >out &&
+ test_line_count = 0 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >actual \
+ HEAD:y/b HEAD:y/c HEAD:y/d HEAD:z/e HEAD:z/f &&
+ git rev-parse >expect \
+ O:z/b O:z/c O:z/d O:z/e B:z/f &&
+ test_cmp expect actual
+ )
+'
+
+###########################################################################
+# Rules suggested by section 4:
+#
+# Directory-rename-detection should be turned off for any directories (as
+# a source for renames) that exist on both sides of the merge. (The "as
+# a source for renames" clarification is due to cases like 1c where
+# the target directory exists on both sides and we do want the rename
+# detection.) But, sadly, see testcase 8b.
+###########################################################################
+
+
+###########################################################################
+# SECTION 5: Files/directories in the way of subset of to-be-renamed paths
+#
+# Implicitly renaming files due to a detected directory rename could run
+# into problems if there are files or directories in the way of the paths
+# we want to rename. Explore such cases in this section.
+###########################################################################
+
+# Testcase 5a, Merge directories, other side adds files to original and target
+# Commit O: z/{b,c}, y/d
+# Commit A: z/{b,c,e_1,f}, y/{d,e_2}
+# Commit B: y/{b,c,d}
+# Expected: z/e_1, y/{b,c,d,e_2,f} + CONFLICT warning
+# NOTE: While directory rename detection is active here causing z/f to
+# become y/f, we did not apply this for z/e_1 because that would
+# give us an add/add conflict for y/e_1 vs y/e_2. This problem with
+# this add/add, is that both versions of y/e are from the same side
+# of history, giving us no way to represent this conflict in the
+# index.
+
+test_setup_5a () {
+ git init 5a &&
+ (
+ cd 5a &&
+
+ mkdir z &&
+ echo b >z/b &&
+ echo c >z/c &&
+ mkdir y &&
+ echo d >y/d &&
+ git add z y &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ echo e1 >z/e &&
+ echo f >z/f &&
+ echo e2 >y/e &&
+ git add z/e z/f y/e &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ git mv z/b y/ &&
+ git mv z/c y/ &&
+ rmdir z &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '5a: Merge directories, other side adds files to original and target' '
+ test_setup_5a &&
+ (
+ cd 5a &&
+
+ git checkout A^0 &&
+
+ test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
+ test_grep "CONFLICT.*implicit dir rename" out &&
+
+ git ls-files -s >out &&
+ test_line_count = 6 out &&
+ git ls-files -u >out &&
+ test_line_count = 0 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >actual \
+ :0:y/b :0:y/c :0:y/d :0:y/e :0:z/e :0:y/f &&
+ git rev-parse >expect \
+ O:z/b O:z/c O:y/d A:y/e A:z/e A:z/f &&
+ test_cmp expect actual
+ )
+'
+
+# Testcase 5b, Rename/delete in order to get add/add/add conflict
+# (Related to testcase 8d; these may appear slightly inconsistent to users;
+# Also related to testcases 7d and 7e)
+# Commit O: z/{b,c,d_1}
+# Commit A: y/{b,c,d_2}
+# Commit B: z/{b,c,d_1,e}, y/d_3
+# Expected: y/{b,c,e}, CONFLICT(add/add: y/d_2 vs. y/d_3)
+# NOTE: If z/d_1 in commit B were to be involved in dir rename detection, as
+# we normally would since z/ is being renamed to y/, then this would be
+# a rename/delete (z/d_1 -> y/d_1 vs. deleted) AND an add/add/add
+# conflict of y/d_1 vs. y/d_2 vs. y/d_3. Add/add/add is not
+# representable in the index, so the existence of y/d_3 needs to
+# cause us to bail on directory rename detection for that path, falling
+# back to git behavior without the directory rename detection.
+
+test_setup_5b () {
+ git init 5b &&
+ (
+ cd 5b &&
+
+ mkdir z &&
+ echo b >z/b &&
+ echo c >z/c &&
+ echo d1 >z/d &&
+ git add z &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git rm z/d &&
+ git mv z y &&
+ echo d2 >y/d &&
+ git add y/d &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ mkdir y &&
+ echo d3 >y/d &&
+ echo e >z/e &&
+ git add y/d z/e &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '5b: Rename/delete in order to get add/add/add conflict' '
+ test_setup_5b &&
+ (
+ cd 5b &&
+
+ git checkout A^0 &&
+
+ test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
+ test_grep "CONFLICT (add/add).* y/d" out &&
+
+ git ls-files -s >out &&
+ test_line_count = 5 out &&
+ git ls-files -u >out &&
+ test_line_count = 2 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >actual \
+ :0:y/b :0:y/c :0:y/e :2:y/d :3:y/d &&
+ git rev-parse >expect \
+ O:z/b O:z/c B:z/e A:y/d B:y/d &&
+ test_cmp expect actual &&
+
+ test_must_fail git rev-parse :1:y/d &&
+ test_path_is_file y/d
+ )
+'
+
+# Testcase 5c, Transitive rename would cause rename/rename/rename/add/add/add
+# (Directory rename detection would result in transitive rename vs.
+# rename/rename(1to2) and turn it into a rename/rename(1to3). Further,
+# rename paths conflict with separate adds on the other side)
+# (Related to testcases 3b and 7c)
+# Commit O: z/{b,c}, x/d_1
+# Commit A: y/{b,c,d_2}, w/d_1
+# Commit B: z/{b,c,d_1,e}, w/d_3, y/d_4
+# Expected: A mess, but only a rename/rename(1to2)/add/add mess. Use the
+# presence of y/d_4 in B to avoid doing transitive rename of
+# x/d_1 -> z/d_1 -> y/d_1, so that the only paths we have at
+# y/d are y/d_2 and y/d_4. We still do the move from z/e to y/e,
+# though, because it doesn't have anything in the way.
+
+test_setup_5c () {
+ git init 5c &&
+ (
+ cd 5c &&
+
+ mkdir z &&
+ echo b >z/b &&
+ echo c >z/c &&
+ mkdir x &&
+ echo d1 >x/d &&
+ git add z x &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git mv z y &&
+ echo d2 >y/d &&
+ git add y/d &&
+ git mv x w &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ git mv x/d z/ &&
+ mkdir w &&
+ mkdir y &&
+ echo d3 >w/d &&
+ echo d4 >y/d &&
+ echo e >z/e &&
+ git add w/ y/ z/e &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '5c: Transitive rename would cause rename/rename/rename/add/add/add' '
+ test_setup_5c &&
+ (
+ cd 5c &&
+
+ git checkout A^0 &&
+
+ test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
+ test_grep "CONFLICT (rename/rename).*x/d.*w/d.*z/d" out &&
+ test_grep "CONFLICT (add/add).* y/d" out &&
+
+ git ls-files -s >out &&
+ test_line_count = 9 out &&
+ git ls-files -u >out &&
+ test_line_count = 6 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >actual \
+ :0:y/b :0:y/c :0:y/e &&
+ git rev-parse >expect \
+ O:z/b O:z/c B:z/e &&
+ test_cmp expect actual &&
+
+ test_must_fail git rev-parse :1:y/d &&
+ git rev-parse >actual \
+ :2:w/d :3:w/d :1:x/d :2:y/d :3:y/d :3:z/d &&
+ git rev-parse >expect \
+ O:x/d B:w/d O:x/d A:y/d B:y/d O:x/d &&
+ test_cmp expect actual &&
+
+ git hash-object >actual \
+ z/d &&
+ git rev-parse >expect \
+ O:x/d &&
+ test_cmp expect actual &&
+ test_path_is_missing x/d &&
+ test_path_is_file y/d &&
+ grep -q "<<<<" y/d # conflict markers should be present
+ )
+'
+
+# Testcase 5d, Directory/file/file conflict due to directory rename
+# Commit O: z/{b,c}
+# Commit A: y/{b,c,d_1}
+# Commit B: z/{b,c,d_2,f}, y/d/e
+# Expected: y/{b,c,d/e,f}, z/d_2, CONFLICT(file/directory), y/d_1~HEAD
+# Note: The fact that y/d/ exists in B makes us bail on directory rename
+# detection for z/d_2, but that doesn't prevent us from applying the
+# directory rename detection for z/f -> y/f.
+
+test_setup_5d () {
+ git init 5d &&
+ (
+ cd 5d &&
+
+ mkdir z &&
+ echo b >z/b &&
+ echo c >z/c &&
+ git add z &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git mv z y &&
+ echo d1 >y/d &&
+ git add y/d &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ mkdir -p y/d &&
+ echo e >y/d/e &&
+ echo d2 >z/d &&
+ echo f >z/f &&
+ git add y/d/e z/d z/f &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '5d: Directory/file/file conflict due to directory rename' '
+ test_setup_5d &&
+ (
+ cd 5d &&
+
+ git checkout A^0 &&
+
+ test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
+ test_grep "CONFLICT (file/directory).*y/d" out &&
+
+ git ls-files -s >out &&
+ test_line_count = 6 out &&
+ git ls-files -u >out &&
+ test_line_count = 1 out &&
+ git ls-files -o >out &&
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ test_line_count = 1 out &&
+
+ git rev-parse >actual \
+ :0:y/b :0:y/c :0:z/d :0:y/f :2:y/d~HEAD :0:y/d/e
+ else
+ test_line_count = 2 out &&
+
+ git rev-parse >actual \
+ :0:y/b :0:y/c :0:z/d :0:y/f :2:y/d :0:y/d/e
+ fi &&
+ git rev-parse >expect \
+ O:z/b O:z/c B:z/d B:z/f A:y/d B:y/d/e &&
+ test_cmp expect actual &&
+
+ git hash-object y/d~HEAD >actual &&
+ git rev-parse A:y/d >expect &&
+ test_cmp expect actual
+ )
+'
+
+###########################################################################
+# Rules suggested by section 5:
+#
+# If a subset of to-be-renamed files have a file or directory in the way,
+# "turn off" the directory rename for those specific sub-paths, falling
+# back to old handling. But, sadly, see testcases 8a and 8b.
+###########################################################################
+
+
+###########################################################################
+# SECTION 6: Same side of the merge was the one that did the rename
+#
+# It may sound obvious that you only want to apply implicit directory
+# renames to directories if the _other_ side of history did the renaming.
+# If you did make an implementation that didn't explicitly enforce this
+# rule, the majority of cases that would fall under this section would
+# also be solved by following the rules from the above sections. But
+# there are still a few that stick out, so this section covers them just
+# to make sure we also get them right.
+###########################################################################
+
+# Testcase 6a, Tricky rename/delete
+# Commit O: z/{b,c,d}
+# Commit A: z/b
+# Commit B: y/{b,c}, z/d
+# Expected: y/b, CONFLICT(rename/delete, z/c -> y/c vs. NULL)
+# Note: We're just checking here that the rename of z/b and z/c to put
+# them under y/ doesn't accidentally catch z/d and make it look like
+# it is also involved in a rename/delete conflict.
+
+test_setup_6a () {
+ git init 6a &&
+ (
+ cd 6a &&
+
+ mkdir z &&
+ echo b >z/b &&
+ echo c >z/c &&
+ echo d >z/d &&
+ git add z &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git rm z/c &&
+ git rm z/d &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ mkdir y &&
+ git mv z/b y/ &&
+ git mv z/c y/ &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '6a: Tricky rename/delete' '
+ test_setup_6a &&
+ (
+ cd 6a &&
+
+ git checkout A^0 &&
+
+ test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
+ test_grep "CONFLICT (rename/delete).*z/c.*y/c" out &&
+
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ git ls-files -s >out &&
+ test_line_count = 3 out &&
+ git ls-files -u >out &&
+ test_line_count = 2 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >actual \
+ :0:y/b :1:y/c :3:y/c &&
+ git rev-parse >expect \
+ O:z/b O:z/c O:z/c
+ else
+ git ls-files -s >out &&
+ test_line_count = 2 out &&
+ git ls-files -u >out &&
+ test_line_count = 1 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >actual \
+ :0:y/b :3:y/c &&
+ git rev-parse >expect \
+ O:z/b O:z/c
+ fi &&
+ test_cmp expect actual
+ )
+'
+
+# Testcase 6b1, Same rename done on both sides
+# (Related to testcase 6b2 and 8e)
+# Commit O: z/{b,c,d,e}
+# Commit A: y/{b,c,d}, x/e
+# Commit B: y/{b,c,d}, z/{e,f}
+# Expected: y/{b,c,d,f}, x/e
+# Note: Directory rename detection says A renamed z/ -> y/ (3 paths renamed
+# to y/ and only 1 renamed to x/), therefore the new file 'z/f' in B
+# should be moved to 'y/f'.
+#
+# This is a bit of an edge case where any behavior might surprise users,
+# whether that is treating A as renaming z/ -> y/, treating A as renaming
+# z/ -> x/, or treating A as not doing any directory rename. However, I
+# think this answer is the least confusing and most consistent with the
+# rules elsewhere.
+#
+# A note about z/ -> x/, since it may not be clear how that could come
+# about: If we were to ignore files renamed by both sides
+# (i.e. z/{b,c,d}), as directory rename detection did in git-2.18 thru
+# at least git-2.28, then we would note there are no renames from z/ to
+# y/ and one rename from z/ to x/ and thus come to the conclusion that
+# A renamed z/ -> x/. This seems more confusing for end users than a
+# rename of z/ to y/, it makes directory rename detection behavior
+# harder for them to predict. As such, we modified the rule, changed
+# the behavior on testcases 6b2 and 8e, and introduced this 6b1 testcase.
+
+test_setup_6b1 () {
+ git init 6b1 &&
+ (
+ cd 6b1 &&
+
+ mkdir z &&
+ echo b >z/b &&
+ echo c >z/c &&
+ echo d >z/d &&
+ echo e >z/e &&
+ git add z &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git mv z y &&
+ mkdir x &&
+ git mv y/e x/e &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ git mv z y &&
+ mkdir z &&
+ git mv y/e z/e &&
+ echo f >z/f &&
+ git add z/f &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_merge_algorithm failure success '6b1: Same renames done on both sides, plus another rename' '
+ test_setup_6b1 &&
+ (
+ cd 6b1 &&
+
+ git checkout A^0 &&
+
+ git -c merge.directoryRenames=true merge -s recursive B^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 5 out &&
+ git ls-files -u >out &&
+ test_line_count = 0 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >actual \
+ HEAD:y/b HEAD:y/c HEAD:y/d HEAD:x/e HEAD:y/f &&
+ git rev-parse >expect \
+ O:z/b O:z/c O:z/d O:z/e B:z/f &&
+ test_cmp expect actual
+ )
+'
+
+# Testcase 6b2, Same rename done on both sides
+# (Related to testcases 6c and 8e)
+# Commit O: z/{b,c}
+# Commit A: y/{b,c}
+# Commit B: y/{b,c}, z/d
+# Expected: y/{b,c,d}
+# Alternate: y/{b,c}, z/d
+# Note: Directory rename detection says A renamed z/ -> y/, therefore the new
+# file 'z/d' in B should be moved to 'y/d'.
+#
+# We could potentially ignore the renames of z/{b,c} on side A since
+# those were renamed on both sides. However, it's a bit of a corner
+# case because what if there was also a z/e that side A moved to x/e
+# and side B left alone? If we used the "ignore renames done on both
+# sides" logic, then we'd compute that A renamed z/ -> x/, and move
+# z/d to x/d. That seems more surprising and uglier than allowing
+# the z/ -> y/ rename.
+
+test_setup_6b2 () {
+ git init 6b2 &&
+ (
+ cd 6b2 &&
+
+ mkdir z &&
+ echo b >z/b &&
+ echo c >z/c &&
+ git add z &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git mv z y &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ git mv z y &&
+ mkdir z &&
+ echo d >z/d &&
+ git add z/d &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_merge_algorithm failure success '6b2: Same rename done on both sides' '
+ test_setup_6b2 &&
+ (
+ cd 6b2 &&
+
+ git checkout A^0 &&
+
+ git -c merge.directoryRenames=true merge -s recursive B^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 3 out &&
+ git ls-files -u >out &&
+ test_line_count = 0 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >actual \
+ HEAD:y/b HEAD:y/c HEAD:y/d &&
+ git rev-parse >expect \
+ O:z/b O:z/c B:z/d &&
+ test_cmp expect actual
+ )
+'
+
+# Testcase 6c, Rename only done on same side
+# (Related to testcases 6b1, 6b2, and 8e)
+# Commit O: z/{b,c}
+# Commit A: z/{b,c} (no change)
+# Commit B: y/{b,c}, z/d
+# Expected: y/{b,c}, z/d
+# NOTE: Seems obvious, but just checking that the implementation doesn't
+# "accidentally detect a rename" and give us y/{b,c,d}.
+
+test_setup_6c () {
+ git init 6c &&
+ (
+ cd 6c &&
+
+ mkdir z &&
+ echo b >z/b &&
+ echo c >z/c &&
+ git add z &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ test_tick &&
+ git commit --allow-empty -m "A" &&
+
+ git checkout B &&
+ git mv z y &&
+ mkdir z &&
+ echo d >z/d &&
+ git add z/d &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '6c: Rename only done on same side' '
+ test_setup_6c &&
+ (
+ cd 6c &&
+
+ git checkout A^0 &&
+
+ git -c merge.directoryRenames=true merge -s recursive B^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 3 out &&
+ git ls-files -u >out &&
+ test_line_count = 0 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >actual \
+ HEAD:y/b HEAD:y/c HEAD:z/d &&
+ git rev-parse >expect \
+ O:z/b O:z/c B:z/d &&
+ test_cmp expect actual
+ )
+'
+
+# Testcase 6d, We don't always want transitive renaming
+# (Related to testcase 1c)
+# Commit O: z/{b,c}, x/d
+# Commit A: z/{b,c}, x/d (no change)
+# Commit B: y/{b,c}, z/d
+# Expected: y/{b,c}, z/d
+# NOTE: Again, this seems obvious but just checking that the implementation
+# doesn't "accidentally detect a rename" and give us y/{b,c,d}.
+
+test_setup_6d () {
+ git init 6d &&
+ (
+ cd 6d &&
+
+ mkdir z &&
+ echo b >z/b &&
+ echo c >z/c &&
+ mkdir x &&
+ echo d >x/d &&
+ git add z x &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ test_tick &&
+ git commit --allow-empty -m "A" &&
+
+ git checkout B &&
+ git mv z y &&
+ git mv x z &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '6d: We do not always want transitive renaming' '
+ test_setup_6d &&
+ (
+ cd 6d &&
+
+ git checkout A^0 &&
+
+ git -c merge.directoryRenames=true merge -s recursive B^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 3 out &&
+ git ls-files -u >out &&
+ test_line_count = 0 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >actual \
+ HEAD:y/b HEAD:y/c HEAD:z/d &&
+ git rev-parse >expect \
+ O:z/b O:z/c O:x/d &&
+ test_cmp expect actual
+ )
+'
+
+# Testcase 6e, Add/add from one-side
+# Commit O: z/{b,c}
+# Commit A: z/{b,c} (no change)
+# Commit B: y/{b,c,d_1}, z/d_2
+# Expected: y/{b,c,d_1}, z/d_2
+# NOTE: Again, this seems obvious but just checking that the implementation
+# doesn't "accidentally detect a rename" and give us y/{b,c} +
+# add/add conflict on y/d_1 vs y/d_2.
+
+test_setup_6e () {
+ git init 6e &&
+ (
+ cd 6e &&
+
+ mkdir z &&
+ echo b >z/b &&
+ echo c >z/c &&
+ git add z &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ test_tick &&
+ git commit --allow-empty -m "A" &&
+
+ git checkout B &&
+ git mv z y &&
+ echo d1 > y/d &&
+ mkdir z &&
+ echo d2 > z/d &&
+ git add y/d z/d &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '6e: Add/add from one side' '
+ test_setup_6e &&
+ (
+ cd 6e &&
+
+ git checkout A^0 &&
+
+ git -c merge.directoryRenames=true merge -s recursive B^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 4 out &&
+ git ls-files -u >out &&
+ test_line_count = 0 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >actual \
+ HEAD:y/b HEAD:y/c HEAD:y/d HEAD:z/d &&
+ git rev-parse >expect \
+ O:z/b O:z/c B:y/d B:z/d &&
+ test_cmp expect actual
+ )
+'
+
+###########################################################################
+# Rules suggested by section 6:
+#
+# Only apply implicit directory renames to directories if the other
+# side of history is the one doing the renaming.
+###########################################################################
+
+
+###########################################################################
+# SECTION 7: More involved Edge/Corner cases
+#
+# The ruleset we have generated in the above sections seems to provide
+# well-defined merges. But can we find edge/corner cases that either (a)
+# are harder for users to understand, or (b) have a resolution that is
+# non-intuitive or suboptimal?
+#
+# The testcases in this section dive into cases that I've tried to craft in
+# a way to find some that might be surprising to users or difficult for
+# them to understand (the next section will look at non-intuitive or
+# suboptimal merge results). Some of the testcases are similar to ones
+# from past sections, but have been simplified to try to highlight error
+# messages using a "modified" path (due to the directory rename). Are
+# users okay with these?
+#
+# In my opinion, testcases that are difficult to understand from this
+# section is due to difficulty in the testcase rather than the directory
+# renaming (similar to how t6042 and t6036 have difficult resolutions due
+# to the problem setup itself being complex). And I don't think the
+# error messages are a problem.
+#
+# On the other hand, the testcases in section 8 worry me slightly more...
+###########################################################################
+
+# Testcase 7a, rename-dir vs. rename-dir (NOT split evenly) PLUS add-other-file
+# Commit O: z/{b,c}
+# Commit A: y/{b,c}
+# Commit B: w/b, x/c, z/d
+# Expected: y/d, CONFLICT(rename/rename for both z/b and z/c)
+# NOTE: There's a rename of z/ here, y/ has more renames, so z/d -> y/d.
+
+test_setup_7a () {
+ git init 7a &&
+ (
+ cd 7a &&
+
+ mkdir z &&
+ echo b >z/b &&
+ echo c >z/c &&
+ git add z &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git mv z y &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ mkdir w &&
+ mkdir x &&
+ git mv z/b w/ &&
+ git mv z/c x/ &&
+ echo d > z/d &&
+ git add z/d &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '7a: rename-dir vs. rename-dir (NOT split evenly) PLUS add-other-file' '
+ test_setup_7a &&
+ (
+ cd 7a &&
+
+ git checkout A^0 &&
+
+ test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
+ test_grep "CONFLICT (rename/rename).*z/b.*y/b.*w/b" out &&
+ test_grep "CONFLICT (rename/rename).*z/c.*y/c.*x/c" out &&
+
+ git ls-files -s >out &&
+ test_line_count = 7 out &&
+ git ls-files -u >out &&
+ test_line_count = 6 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >actual \
+ :1:z/b :2:y/b :3:w/b :1:z/c :2:y/c :3:x/c :0:y/d &&
+ git rev-parse >expect \
+ O:z/b O:z/b O:z/b O:z/c O:z/c O:z/c B:z/d &&
+ test_cmp expect actual &&
+
+ git hash-object >actual \
+ y/b w/b y/c x/c &&
+ git rev-parse >expect \
+ O:z/b O:z/b O:z/c O:z/c &&
+ test_cmp expect actual
+ )
+'
+
+# Testcase 7b, rename/rename(2to1), but only due to transitive rename
+# (Related to testcase 1d)
+# Commit O: z/{b,c}, x/d_1, w/d_2
+# Commit A: y/{b,c,d_2}, x/d_1
+# Commit B: z/{b,c,d_1}, w/d_2
+# Expected: y/{b,c}, CONFLICT(rename/rename(2to1): x/d_1, w/d_2 -> y_d)
+
+test_setup_7b () {
+ git init 7b &&
+ (
+ cd 7b &&
+
+ mkdir z &&
+ mkdir x &&
+ mkdir w &&
+ echo b >z/b &&
+ echo c >z/c &&
+ echo d1 > x/d &&
+ echo d2 > w/d &&
+ git add z x w &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git mv z y &&
+ git mv w/d y/ &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ git mv x/d z/ &&
+ rmdir x &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '7b: rename/rename(2to1), but only due to transitive rename' '
+ test_setup_7b &&
+ (
+ cd 7b &&
+
+ git checkout A^0 &&
+
+ test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
+ test_grep "CONFLICT (\(.*\)/\1)" out &&
+
+ git ls-files -s >out &&
+ test_line_count = 4 out &&
+ git ls-files -u >out &&
+ test_line_count = 2 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >actual \
+ :0:y/b :0:y/c :2:y/d :3:y/d &&
+ git rev-parse >expect \
+ O:z/b O:z/c O:w/d O:x/d &&
+ test_cmp expect actual &&
+
+ # Test that the two-way merge in y/d is as expected
+ git cat-file -p :2:y/d >expect &&
+ git cat-file -p :3:y/d >other &&
+ >empty &&
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ test_must_fail git merge-file \
+ -L "HEAD:y/d" \
+ -L "" \
+ -L "B^0:z/d" \
+ expect empty other
+ else
+ test_must_fail git merge-file \
+ -L "HEAD" \
+ -L "" \
+ -L "B^0" \
+ expect empty other
+ fi &&
+ test_cmp expect y/d
+ )
+'
+
+# Testcase 7c, rename/rename(1to...2or3); transitive rename may add complexity
+# (Related to testcases 3b and 5c)
+# Commit O: z/{b,c}, x/d
+# Commit A: y/{b,c}, w/d
+# Commit B: z/{b,c,d}
+# Expected: y/{b,c}, CONFLICT(x/d -> w/d vs. y/d)
+# NOTE: z/ was renamed to y/ so we do want to report
+# neither CONFLICT(x/d -> w/d vs. z/d)
+# nor CONFLiCT x/d -> w/d vs. y/d vs. z/d)
+
+test_setup_7c () {
+ git init 7c &&
+ (
+ cd 7c &&
+
+ mkdir z &&
+ echo b >z/b &&
+ echo c >z/c &&
+ mkdir x &&
+ echo d >x/d &&
+ git add z x &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git mv z y &&
+ git mv x w &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ git mv x/d z/ &&
+ rmdir x &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '7c: rename/rename(1to...2or3); transitive rename may add complexity' '
+ test_setup_7c &&
+ (
+ cd 7c &&
+
+ git checkout A^0 &&
+
+ test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
+ test_grep "CONFLICT (rename/rename).*x/d.*w/d.*y/d" out &&
+
+ git ls-files -s >out &&
+ test_line_count = 5 out &&
+ git ls-files -u >out &&
+ test_line_count = 3 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >actual \
+ :0:y/b :0:y/c :1:x/d :2:w/d :3:y/d &&
+ git rev-parse >expect \
+ O:z/b O:z/c O:x/d O:x/d O:x/d &&
+ test_cmp expect actual
+ )
+'
+
+# Testcase 7d, transitive rename involved in rename/delete; how is it reported?
+# (Related somewhat to testcases 5b and 8d)
+# Commit O: z/{b,c}, x/d
+# Commit A: y/{b,c}
+# Commit B: z/{b,c,d}
+# Expected: y/{b,c}, CONFLICT(delete x/d vs rename to y/d)
+# NOTE: z->y so NOT CONFLICT(delete x/d vs rename to z/d)
+
+test_setup_7d () {
+ git init 7d &&
+ (
+ cd 7d &&
+
+ mkdir z &&
+ echo b >z/b &&
+ echo c >z/c &&
+ mkdir x &&
+ echo d >x/d &&
+ git add z x &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git mv z y &&
+ git rm -rf x &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ git mv x/d z/ &&
+ rmdir x &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '7d: transitive rename involved in rename/delete; how is it reported?' '
+ test_setup_7d &&
+ (
+ cd 7d &&
+
+ git checkout A^0 &&
+
+ test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
+ test_grep "CONFLICT (rename/delete).*x/d.*y/d" out &&
+
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ git ls-files -s >out &&
+ test_line_count = 4 out &&
+ git ls-files -u >out &&
+ test_line_count = 2 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >actual \
+ :0:y/b :0:y/c :1:y/d :3:y/d &&
+ git rev-parse >expect \
+ O:z/b O:z/c O:x/d O:x/d
+ else
+ git ls-files -s >out &&
+ test_line_count = 3 out &&
+ git ls-files -u >out &&
+ test_line_count = 1 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >actual \
+ :0:y/b :0:y/c :3:y/d &&
+ git rev-parse >expect \
+ O:z/b O:z/c O:x/d
+ fi &&
+ test_cmp expect actual
+ )
+'
+
+# Testcase 7e, transitive rename in rename/delete AND dirs in the way
+# (Very similar to 'both rename source and destination involved in D/F conflict' from t6022-merge-rename.sh)
+# (Also related to testcases 9c and 9d)
+# Commit O: z/{b,c}, x/d_1
+# Commit A: y/{b,c,d/g}, x/d/f
+# Commit B: z/{b,c,d_1}
+# Expected: rename/delete(x/d_1->y/d_1 vs. None) + D/F conflict on y/d
+# y/{b,c,d/g}, y/d_1~B^0, x/d/f
+
+# NOTE: The main path of interest here is d_1 and where it ends up, but
+# this is actually a case that has two potential directory renames
+# involved and D/F conflict(s), so it makes sense to walk through
+# each step.
+#
+# Commit A renames z/ -> y/. Thus everything that B adds to z/
+# should be instead moved to y/. This gives us the D/F conflict on
+# y/d because x/d_1 -> z/d_1 -> y/d_1 conflicts with y/d/g.
+#
+# Further, commit B renames x/ -> z/, thus everything A adds to x/
+# should instead be moved to z/...BUT we removed z/ and renamed it
+# to y/, so maybe everything should move not from x/ to z/, but
+# from x/ to z/ to y/. Doing so might make sense from the logic so
+# far, but note that commit A had both an x/ and a y/; it did the
+# renaming of z/ to y/ and created x/d/f and it clearly made these
+# things separate, so it doesn't make much sense to push these
+# together. Doing so is what I'd call a doubly transitive rename;
+# see testcases 9c and 9d for further discussion of this issue and
+# how it's resolved.
+
+test_setup_7e () {
+ git init 7e &&
+ (
+ cd 7e &&
+
+ mkdir z &&
+ echo b >z/b &&
+ echo c >z/c &&
+ mkdir x &&
+ echo d1 >x/d &&
+ git add z x &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git mv z y &&
+ git rm x/d &&
+ mkdir -p x/d &&
+ mkdir -p y/d &&
+ echo f >x/d/f &&
+ echo g >y/d/g &&
+ git add x/d/f y/d/g &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ git mv x/d z/ &&
+ rmdir x &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '7e: transitive rename in rename/delete AND dirs in the way' '
+ test_setup_7e &&
+ (
+ cd 7e &&
+
+ git checkout A^0 &&
+
+ test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
+ test_grep "CONFLICT (rename/delete).*x/d.*y/d" out &&
+
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ git ls-files -s >out &&
+ test_line_count = 6 out &&
+ git ls-files -u >out &&
+ test_line_count = 2 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >actual \
+ :0:x/d/f :0:y/d/g :0:y/b :0:y/c :1:y/d~B^0 :3:y/d~B^0 &&
+ git rev-parse >expect \
+ A:x/d/f A:y/d/g O:z/b O:z/c O:x/d O:x/d
+ else
+ git ls-files -s >out &&
+ test_line_count = 5 out &&
+ git ls-files -u >out &&
+ test_line_count = 1 out &&
+ git ls-files -o >out &&
+ test_line_count = 2 out &&
+
+ git rev-parse >actual \
+ :0:x/d/f :0:y/d/g :0:y/b :0:y/c :3:y/d &&
+ git rev-parse >expect \
+ A:x/d/f A:y/d/g O:z/b O:z/c O:x/d
+ fi &&
+ test_cmp expect actual &&
+
+ git hash-object y/d~B^0 >actual &&
+ git rev-parse O:x/d >expect &&
+ test_cmp expect actual
+ )
+'
+
+###########################################################################
+# SECTION 8: Suboptimal merges
+#
+# As alluded to in the last section, the ruleset we have built up for
+# detecting directory renames unfortunately has some special cases where it
+# results in slightly suboptimal or non-intuitive behavior. This section
+# explores these cases.
+#
+# To be fair, we already had non-intuitive or suboptimal behavior for most
+# of these cases in git before introducing implicit directory rename
+# detection, but it'd be nice if there was a modified ruleset out there
+# that handled these cases a bit better.
+###########################################################################
+
+# Testcase 8a, Dual-directory rename, one into the others' way
+# Commit O. x/{a,b}, y/{c,d}
+# Commit A. x/{a,b,e}, y/{c,d,f}
+# Commit B. y/{a,b}, z/{c,d}
+#
+# Possible Resolutions:
+# w/o dir-rename detection: y/{a,b,f}, z/{c,d}, x/e
+# Currently expected: y/{a,b,e,f}, z/{c,d}
+# Optimal: y/{a,b,e}, z/{c,d,f}
+#
+# Note: Both x and y got renamed and it'd be nice to detect both, and we do
+# better with directory rename detection than git did without, but the
+# simple rule from section 5 prevents me from handling this as optimally as
+# we potentially could.
+
+test_setup_8a () {
+ git init 8a &&
+ (
+ cd 8a &&
+
+ mkdir x &&
+ mkdir y &&
+ echo a >x/a &&
+ echo b >x/b &&
+ echo c >y/c &&
+ echo d >y/d &&
+ git add x y &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ echo e >x/e &&
+ echo f >y/f &&
+ git add x/e y/f &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ git mv y z &&
+ git mv x y &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '8a: Dual-directory rename, one into the others way' '
+ test_setup_8a &&
+ (
+ cd 8a &&
+
+ git checkout A^0 &&
+
+ git -c merge.directoryRenames=true merge -s recursive B^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 6 out &&
+ git ls-files -u >out &&
+ test_line_count = 0 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >actual \
+ HEAD:y/a HEAD:y/b HEAD:y/e HEAD:y/f HEAD:z/c HEAD:z/d &&
+ git rev-parse >expect \
+ O:x/a O:x/b A:x/e A:y/f O:y/c O:y/d &&
+ test_cmp expect actual
+ )
+'
+
+# Testcase 8b, Dual-directory rename, one into the others' way, with conflicting filenames
+# Commit O. x/{a_1,b_1}, y/{a_2,b_2}
+# Commit A. x/{a_1,b_1,e_1}, y/{a_2,b_2,e_2}
+# Commit B. y/{a_1,b_1}, z/{a_2,b_2}
+#
+# w/o dir-rename detection: y/{a_1,b_1,e_2}, z/{a_2,b_2}, x/e_1
+# Currently expected: <same>
+# Scary: y/{a_1,b_1}, z/{a_2,b_2}, CONFLICT(add/add, e_1 vs. e_2)
+# Optimal: y/{a_1,b_1,e_1}, z/{a_2,b_2,e_2}
+#
+# Note: Very similar to 8a, except instead of 'e' and 'f' in directories x and
+# y, both are named 'e'. Without directory rename detection, neither file
+# moves directories. Implement directory rename detection suboptimally, and
+# you get an add/add conflict, but both files were added in commit A, so this
+# is an add/add conflict where one side of history added both files --
+# something we can't represent in the index. Obviously, we'd prefer the last
+# resolution, but our previous rules are too coarse to allow it. Using both
+# the rules from section 4 and section 5 save us from the Scary resolution,
+# making us fall back to pre-directory-rename-detection behavior for both
+# e_1 and e_2.
+
+test_setup_8b () {
+ git init 8b &&
+ (
+ cd 8b &&
+
+ mkdir x &&
+ mkdir y &&
+ echo a1 >x/a &&
+ echo b1 >x/b &&
+ echo a2 >y/a &&
+ echo b2 >y/b &&
+ git add x y &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ echo e1 >x/e &&
+ echo e2 >y/e &&
+ git add x/e y/e &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ git mv y z &&
+ git mv x y &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '8b: Dual-directory rename, one into the others way, with conflicting filenames' '
+ test_setup_8b &&
+ (
+ cd 8b &&
+
+ git checkout A^0 &&
+
+ git -c merge.directoryRenames=true merge -s recursive B^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 6 out &&
+ git ls-files -u >out &&
+ test_line_count = 0 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >actual \
+ HEAD:y/a HEAD:y/b HEAD:z/a HEAD:z/b HEAD:x/e HEAD:y/e &&
+ git rev-parse >expect \
+ O:x/a O:x/b O:y/a O:y/b A:x/e A:y/e &&
+ test_cmp expect actual
+ )
+'
+
+# Testcase 8c, modify/delete or rename+modify/delete?
+# (Related to testcases 5b, 8d, and 9h)
+# Commit O: z/{b,c,d}
+# Commit A: y/{b,c}
+# Commit B: z/{b,c,d_modified,e}
+# Expected: y/{b,c,e}, CONFLICT(modify/delete: on z/d)
+#
+# Note: It could easily be argued that the correct resolution here is
+# y/{b,c,e}, CONFLICT(rename/delete: z/d -> y/d vs deleted)
+# and that the modified version of d should be present in y/ after
+# the merge, just marked as conflicted. Indeed, I previously did
+# argue that. But applying directory renames to the side of
+# history where a file is merely modified results in spurious
+# rename/rename(1to2) conflicts -- see testcase 9h. See also
+# notes in 8d.
+
+test_setup_8c () {
+ git init 8c &&
+ (
+ cd 8c &&
+
+ mkdir z &&
+ echo b >z/b &&
+ echo c >z/c &&
+ test_seq 1 10 >z/d &&
+ git add z &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git rm z/d &&
+ git mv z y &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ echo 11 >z/d &&
+ test_chmod +x z/d &&
+ echo e >z/e &&
+ git add z/d z/e &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '8c: modify/delete or rename+modify/delete' '
+ test_setup_8c &&
+ (
+ cd 8c &&
+
+ git checkout A^0 &&
+
+ test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
+ test_grep "CONFLICT (modify/delete).* z/d" out &&
+
+ git ls-files -s >out &&
+ test_line_count = 5 out &&
+ git ls-files -u >out &&
+ test_line_count = 2 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >actual \
+ :0:y/b :0:y/c :0:y/e :1:z/d :3:z/d &&
+ git rev-parse >expect \
+ O:z/b O:z/c B:z/e O:z/d B:z/d &&
+ test_cmp expect actual &&
+
+ test_must_fail git rev-parse :2:z/d &&
+ git ls-files -s z/d | grep ^100755 &&
+ test_path_is_file z/d &&
+ test_path_is_missing y/d
+ )
+'
+
+# Testcase 8d, rename/delete...or not?
+# (Related to testcase 5b; these may appear slightly inconsistent to users;
+# Also related to testcases 7d and 7e)
+# Commit O: z/{b,c,d}
+# Commit A: y/{b,c}
+# Commit B: z/{b,c,d,e}
+# Expected: y/{b,c,e}
+#
+# Note: It would also be somewhat reasonable to resolve this as
+# y/{b,c,e}, CONFLICT(rename/delete: x/d -> y/d or deleted)
+#
+# In this case, I'm leaning towards: commit A was the one that deleted z/d
+# and it did the rename of z to y, so the two "conflicts" (rename vs.
+# delete) are both coming from commit A, which is illogical. Conflicts
+# during merging are supposed to be about opposite sides doing things
+# differently.
+
+test_setup_8d () {
+ git init 8d &&
+ (
+ cd 8d &&
+
+ mkdir z &&
+ echo b >z/b &&
+ echo c >z/c &&
+ test_seq 1 10 >z/d &&
+ git add z &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git rm z/d &&
+ git mv z y &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ echo e >z/e &&
+ git add z/e &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '8d: rename/delete...or not?' '
+ test_setup_8d &&
+ (
+ cd 8d &&
+
+ git checkout A^0 &&
+
+ git -c merge.directoryRenames=true merge -s recursive B^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 3 out &&
+
+ git rev-parse >actual \
+ HEAD:y/b HEAD:y/c HEAD:y/e &&
+ git rev-parse >expect \
+ O:z/b O:z/c B:z/e &&
+ test_cmp expect actual
+ )
+'
+
+# Testcase 8e, Both sides rename, one side adds to original directory
+# Commit O: z/{b,c}
+# Commit A: y/{b,c}
+# Commit B: w/{b,c}, z/d
+#
+# Possible Resolutions:
+# if z not considered renamed: z/d, CONFLICT(z/b -> y/b vs. w/b),
+# CONFLICT(z/c -> y/c vs. w/c)
+# if z->y rename considered: y/d, CONFLICT(z/b -> y/b vs. w/b),
+# CONFLICT(z/c -> y/c vs. w/c)
+# Optimal: ??
+#
+# Notes: In commit A, directory z got renamed to y. In commit B, directory z
+# did NOT get renamed; the directory is still present; instead it is
+# considered to have just renamed a subset of paths in directory z
+# elsewhere. This is much like testcase 6b2 (where commit B moves all
+# the original paths out of z/ but opted to keep d within z/).
+#
+# It was not clear in the past what should be done with this testcase;
+# in fact, I noted that I "just picked one" previously. However,
+# following the new logic for testcase 6b2, we should take the rename
+# and move z/d to y/d.
+#
+# 6b1, 6b2, and this case are definitely somewhat fuzzy in terms of
+# whether they are optimal for end users, but (a) the default for
+# directory rename detection is to mark these all as conflicts
+# anyway, (b) it feels like this is less prone to higher order corner
+# case confusion, and (c) the current algorithm requires less global
+# knowledge (i.e. less coupling in the algorithm between renames done
+# on both sides) which thus means users are better able to predict
+# the behavior, and predict it without computing as many details.
+
+test_setup_8e () {
+ git init 8e &&
+ (
+ cd 8e &&
+
+ mkdir z &&
+ echo b >z/b &&
+ echo c >z/c &&
+ git add z &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git mv z y &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ git mv z w &&
+ mkdir z &&
+ echo d >z/d &&
+ git add z/d &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '8e: Both sides rename, one side adds to original directory' '
+ test_setup_8e &&
+ (
+ cd 8e &&
+
+ git checkout A^0 &&
+
+ test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
+ test_grep CONFLICT.*rename/rename.*z/c.*y/c.*w/c out &&
+ test_grep CONFLICT.*rename/rename.*z/b.*y/b.*w/b out &&
+
+ git ls-files -s >out &&
+ test_line_count = 7 out &&
+ git ls-files -u >out &&
+ test_line_count = 6 out &&
+ git ls-files -o >out &&
+ test_line_count = 2 out &&
+
+ git rev-parse >actual \
+ :1:z/b :2:y/b :3:w/b :1:z/c :2:y/c :3:w/c :0:y/d &&
+ git rev-parse >expect \
+ O:z/b O:z/b O:z/b O:z/c O:z/c O:z/c B:z/d &&
+ test_cmp expect actual &&
+
+ git hash-object >actual \
+ y/b w/b y/c w/c &&
+ git rev-parse >expect \
+ O:z/b O:z/b O:z/c O:z/c &&
+ test_cmp expect actual &&
+
+ test_path_is_missing z/b &&
+ test_path_is_missing z/c
+ )
+'
+
+###########################################################################
+# SECTION 9: Other testcases
+#
+# This section consists of miscellaneous testcases I thought of during
+# the implementation which round out the testing.
+###########################################################################
+
+# Testcase 9a, Inner renamed directory within outer renamed directory
+# (Related to testcase 1f)
+# Commit O: z/{b,c,d/{e,f,g}}
+# Commit A: y/{b,c}, x/w/{e,f,g}
+# Commit B: z/{b,c,d/{e,f,g,h},i}
+# Expected: y/{b,c,i}, x/w/{e,f,g,h}
+# NOTE: The only reason this one is interesting is because when a directory
+# is split into multiple other directories, we determine by the weight
+# of which one had the most paths going to it. A naive implementation
+# of that could take the new file in commit B at z/i to x/w/i or x/i.
+
+test_setup_9a () {
+ git init 9a &&
+ (
+ cd 9a &&
+
+ mkdir -p z/d &&
+ echo b >z/b &&
+ echo c >z/c &&
+ echo e >z/d/e &&
+ echo f >z/d/f &&
+ echo g >z/d/g &&
+ git add z &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ mkdir x &&
+ git mv z/d x/w &&
+ git mv z y &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ echo h >z/d/h &&
+ echo i >z/i &&
+ git add z &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '9a: Inner renamed directory within outer renamed directory' '
+ test_setup_9a &&
+ (
+ cd 9a &&
+
+ git checkout A^0 &&
+
+ git -c merge.directoryRenames=true merge -s recursive B^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 7 out &&
+ git ls-files -u >out &&
+ test_line_count = 0 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >actual \
+ HEAD:y/b HEAD:y/c HEAD:y/i &&
+ git rev-parse >expect \
+ O:z/b O:z/c B:z/i &&
+ test_cmp expect actual &&
+
+ git rev-parse >actual \
+ HEAD:x/w/e HEAD:x/w/f HEAD:x/w/g HEAD:x/w/h &&
+ git rev-parse >expect \
+ O:z/d/e O:z/d/f O:z/d/g B:z/d/h &&
+ test_cmp expect actual
+ )
+'
+
+# Testcase 9b, Transitive rename with content merge
+# (Related to testcase 1c)
+# Commit O: z/{b,c}, x/d_1
+# Commit A: y/{b,c}, x/d_2
+# Commit B: z/{b,c,d_3}
+# Expected: y/{b,c,d_merged}
+
+test_setup_9b () {
+ git init 9b &&
+ (
+ cd 9b &&
+
+ mkdir z &&
+ echo b >z/b &&
+ echo c >z/c &&
+ mkdir x &&
+ test_seq 1 10 >x/d &&
+ git add z x &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git mv z y &&
+ test_seq 1 11 >x/d &&
+ git add x/d &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ test_seq 0 10 >x/d &&
+ git mv x/d z/d &&
+ git add z/d &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '9b: Transitive rename with content merge' '
+ test_setup_9b &&
+ (
+ cd 9b &&
+
+ git checkout A^0 &&
+
+ git -c merge.directoryRenames=true merge -s recursive B^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 3 out &&
+
+ test_seq 0 11 >expected &&
+ test_cmp expected y/d &&
+ git add expected &&
+ git rev-parse >actual \
+ HEAD:y/b HEAD:y/c HEAD:y/d &&
+ git rev-parse >expect \
+ O:z/b O:z/c :0:expected &&
+ test_cmp expect actual &&
+ test_must_fail git rev-parse HEAD:x/d &&
+ test_must_fail git rev-parse HEAD:z/d &&
+ test_path_is_missing z/d &&
+
+ test $(git rev-parse HEAD:y/d) != $(git rev-parse O:x/d) &&
+ test $(git rev-parse HEAD:y/d) != $(git rev-parse A:x/d) &&
+ test $(git rev-parse HEAD:y/d) != $(git rev-parse B:z/d)
+ )
+'
+
+# Testcase 9c, Doubly transitive rename?
+# (Related to testcase 1c, 7e, and 9d)
+# Commit O: z/{b,c}, x/{d,e}, w/f
+# Commit A: y/{b,c}, x/{d,e,f,g}
+# Commit B: z/{b,c,d,e}, w/f
+# Expected: y/{b,c,d,e}, x/{f,g}
+#
+# NOTE: x/f and x/g may be slightly confusing here. The rename from w/f to
+# x/f is clear. Let's look beyond that. Here's the logic:
+# Commit B renamed x/ -> z/
+# Commit A renamed z/ -> y/
+# So, we could possibly further rename x/f to z/f to y/f, a doubly
+# transient rename. However, where does it end? We can chain these
+# indefinitely (see testcase 9d). What if there is a D/F conflict
+# at z/f/ or y/f/? Or just another file conflict at one of those
+# paths? In the case of an N-long chain of transient renamings,
+# where do we "abort" the rename at? Can the user make sense of
+# the resulting conflict and resolve it?
+#
+# To avoid this confusion I use the simple rule that if the other side
+# of history did a directory rename to a path that your side renamed
+# away, then ignore that particular rename from the other side of
+# history for any implicit directory renames.
+
+test_setup_9c () {
+ git init 9c &&
+ (
+ cd 9c &&
+
+ mkdir z &&
+ echo b >z/b &&
+ echo c >z/c &&
+ mkdir x &&
+ echo d >x/d &&
+ echo e >x/e &&
+ mkdir w &&
+ echo f >w/f &&
+ git add z x w &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git mv z y &&
+ git mv w/f x/ &&
+ echo g >x/g &&
+ git add x/g &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ git mv x/d z/d &&
+ git mv x/e z/e &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '9c: Doubly transitive rename?' '
+ test_setup_9c &&
+ (
+ cd 9c &&
+
+ git checkout A^0 &&
+
+ git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
+ test_grep "WARNING: Avoiding applying x -> z rename to x/f" out &&
+
+ git ls-files -s >out &&
+ test_line_count = 6 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >actual \
+ HEAD:y/b HEAD:y/c HEAD:y/d HEAD:y/e HEAD:x/f HEAD:x/g &&
+ git rev-parse >expect \
+ O:z/b O:z/c O:x/d O:x/e O:w/f A:x/g &&
+ test_cmp expect actual
+ )
+'
+
+# Testcase 9d, N-fold transitive rename?
+# (Related to testcase 9c...and 1c and 7e)
+# Commit O: z/a, y/b, x/c, w/d, v/e, u/f
+# Commit A: y/{a,b}, w/{c,d}, u/{e,f}
+# Commit B: z/{a,t}, x/{b,c}, v/{d,e}, u/f
+# Expected: <see NOTE first>
+#
+# NOTE: z/ -> y/ (in commit A)
+# y/ -> x/ (in commit B)
+# x/ -> w/ (in commit A)
+# w/ -> v/ (in commit B)
+# v/ -> u/ (in commit A)
+# So, if we add a file to z, say z/t, where should it end up? In u?
+# What if there's another file or directory named 't' in one of the
+# intervening directories and/or in u itself? Also, shouldn't the
+# same logic that places 't' in u/ also move ALL other files to u/?
+# What if there are file or directory conflicts in any of them? If
+# we attempted to do N-way (N-fold? N-ary? N-uple?) transitive renames
+# like this, would the user have any hope of understanding any
+# conflicts or how their working tree ended up? I think not, so I'm
+# ruling out N-ary transitive renames for N>1.
+#
+# Therefore our expected result is:
+# z/t, y/a, x/b, w/c, u/d, u/e, u/f
+# The reason that v/d DOES get transitively renamed to u/d is that u/ isn't
+# renamed somewhere. A slightly sub-optimal result, but it uses fairly
+# simple rules that are consistent with what we need for all the other
+# testcases and simplifies things for the user.
+
+test_setup_9d () {
+ git init 9d &&
+ (
+ cd 9d &&
+
+ mkdir z y x w v u &&
+ echo a >z/a &&
+ echo b >y/b &&
+ echo c >x/c &&
+ echo d >w/d &&
+ echo e >v/e &&
+ echo f >u/f &&
+ git add z y x w v u &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git mv z/a y/ &&
+ git mv x/c w/ &&
+ git mv v/e u/ &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ echo t >z/t &&
+ git mv y/b x/ &&
+ git mv w/d v/ &&
+ git add z/t &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '9d: N-way transitive rename?' '
+ test_setup_9d &&
+ (
+ cd 9d &&
+
+ git checkout A^0 &&
+
+ git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
+ test_grep "WARNING: Avoiding applying z -> y rename to z/t" out &&
+ test_grep "WARNING: Avoiding applying y -> x rename to y/a" out &&
+ test_grep "WARNING: Avoiding applying x -> w rename to x/b" out &&
+ test_grep "WARNING: Avoiding applying w -> v rename to w/c" out &&
+
+ git ls-files -s >out &&
+ test_line_count = 7 out &&
+ git ls-files -o >out &&
+ test_line_count = 1 out &&
+
+ git rev-parse >actual \
+ HEAD:z/t \
+ HEAD:y/a HEAD:x/b HEAD:w/c \
+ HEAD:u/d HEAD:u/e HEAD:u/f &&
+ git rev-parse >expect \
+ B:z/t \
+ O:z/a O:y/b O:x/c \
+ O:w/d O:v/e A:u/f &&
+ test_cmp expect actual
+ )
+'
+
+# Testcase 9e, N-to-1 whammo
+# (Related to testcase 9c...and 1c and 7e)
+# Commit O: dir1/{a,b}, dir2/{d,e}, dir3/{g,h}, dirN/{j,k}
+# Commit A: dir1/{a,b,c,yo}, dir2/{d,e,f,yo}, dir3/{g,h,i,yo}, dirN/{j,k,l,yo}
+# Commit B: combined/{a,b,d,e,g,h,j,k}
+# Expected: combined/{a,b,c,d,e,f,g,h,i,j,k,l}, CONFLICT(Nto1) warnings,
+# dir1/yo, dir2/yo, dir3/yo, dirN/yo
+
+test_setup_9e () {
+ git init 9e &&
+ (
+ cd 9e &&
+
+ mkdir dir1 dir2 dir3 dirN &&
+ echo a >dir1/a &&
+ echo b >dir1/b &&
+ echo d >dir2/d &&
+ echo e >dir2/e &&
+ echo g >dir3/g &&
+ echo h >dir3/h &&
+ echo j >dirN/j &&
+ echo k >dirN/k &&
+ git add dir* &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ echo c >dir1/c &&
+ echo yo >dir1/yo &&
+ echo f >dir2/f &&
+ echo yo >dir2/yo &&
+ echo i >dir3/i &&
+ echo yo >dir3/yo &&
+ echo l >dirN/l &&
+ echo yo >dirN/yo &&
+ git add dir* &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ git mv dir1 combined &&
+ git mv dir2/* combined/ &&
+ git mv dir3/* combined/ &&
+ git mv dirN/* combined/ &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '9e: N-to-1 whammo' '
+ test_setup_9e &&
+ (
+ cd 9e &&
+
+ git checkout A^0 &&
+
+ test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out &&
+ grep "CONFLICT (implicit dir rename): Cannot map more than one path to combined/yo" out >error_line &&
+ grep -q dir1/yo error_line &&
+ grep -q dir2/yo error_line &&
+ grep -q dir3/yo error_line &&
+ grep -q dirN/yo error_line &&
+
+ git ls-files -s >out &&
+ test_line_count = 16 out &&
+ git ls-files -u >out &&
+ test_line_count = 0 out &&
+ git ls-files -o >out &&
+ test_line_count = 2 out &&
+
+ git rev-parse >actual \
+ :0:combined/a :0:combined/b :0:combined/c \
+ :0:combined/d :0:combined/e :0:combined/f \
+ :0:combined/g :0:combined/h :0:combined/i \
+ :0:combined/j :0:combined/k :0:combined/l &&
+ git rev-parse >expect \
+ O:dir1/a O:dir1/b A:dir1/c \
+ O:dir2/d O:dir2/e A:dir2/f \
+ O:dir3/g O:dir3/h A:dir3/i \
+ O:dirN/j O:dirN/k A:dirN/l &&
+ test_cmp expect actual &&
+
+ git rev-parse >actual \
+ :0:dir1/yo :0:dir2/yo :0:dir3/yo :0:dirN/yo &&
+ git rev-parse >expect \
+ A:dir1/yo A:dir2/yo A:dir3/yo A:dirN/yo &&
+ test_cmp expect actual
+ )
+'
+
+# Testcase 9f, Renamed directory that only contained immediate subdirs
+# (Related to testcases 1e & 9g)
+# Commit O: goal/{a,b}/$more_files
+# Commit A: priority/{a,b}/$more_files
+# Commit B: goal/{a,b}/$more_files, goal/c
+# Expected: priority/{a,b}/$more_files, priority/c
+
+test_setup_9f () {
+ git init 9f &&
+ (
+ cd 9f &&
+
+ mkdir -p goal/a &&
+ mkdir -p goal/b &&
+ echo foo >goal/a/foo &&
+ echo bar >goal/b/bar &&
+ echo baz >goal/b/baz &&
+ git add goal &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git mv goal/ priority &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ echo c >goal/c &&
+ git add goal/c &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '9f: Renamed directory that only contained immediate subdirs' '
+ test_setup_9f &&
+ (
+ cd 9f &&
+
+ git checkout A^0 &&
+
+ git -c merge.directoryRenames=true merge -s recursive B^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 4 out &&
+
+ git rev-parse >actual \
+ HEAD:priority/a/foo \
+ HEAD:priority/b/bar \
+ HEAD:priority/b/baz \
+ HEAD:priority/c &&
+ git rev-parse >expect \
+ O:goal/a/foo \
+ O:goal/b/bar \
+ O:goal/b/baz \
+ B:goal/c &&
+ test_cmp expect actual &&
+ test_must_fail git rev-parse HEAD:goal/c
+ )
+'
+
+# Testcase 9g, Renamed directory that only contained immediate subdirs, immediate subdirs renamed
+# (Related to testcases 1e & 9f)
+# Commit O: goal/{a,b}/$more_files
+# Commit A: priority/{alpha,bravo}/$more_files
+# Commit B: goal/{a,b}/$more_files, goal/c
+# Expected: priority/{alpha,bravo}/$more_files, priority/c
+# We currently fail this test because the directory renames we detect are
+# goal/a/ -> priority/alpha/
+# goal/b/ -> priority/bravo/
+# We do not detect
+# goal/ -> priority/
+# because of no files found within goal/, and the fact that "a" != "alpha"
+# and "b" != "bravo". But I'm not sure it's really a failure given that
+# viewpoint...
+
+test_setup_9g () {
+ git init 9g &&
+ (
+ cd 9g &&
+
+ mkdir -p goal/a &&
+ mkdir -p goal/b &&
+ echo foo >goal/a/foo &&
+ echo bar >goal/b/bar &&
+ echo baz >goal/b/baz &&
+ git add goal &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ mkdir priority &&
+ git mv goal/a/ priority/alpha &&
+ git mv goal/b/ priority/beta &&
+ rmdir goal/ &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ echo c >goal/c &&
+ git add goal/c &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_failure '9g: Renamed directory that only contained immediate subdirs, immediate subdirs renamed' '
+ test_setup_9g &&
+ (
+ cd 9g &&
+
+ git checkout A^0 &&
+
+ git -c merge.directoryRenames=true merge -s recursive B^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 4 out &&
+
+ git rev-parse >actual \
+ HEAD:priority/alpha/foo \
+ HEAD:priority/beta/bar \
+ HEAD:priority/beta/baz \
+ HEAD:priority/c &&
+ git rev-parse >expect \
+ O:goal/a/foo \
+ O:goal/b/bar \
+ O:goal/b/baz \
+ B:goal/c &&
+ test_cmp expect actual &&
+ test_must_fail git rev-parse HEAD:goal/c
+ )
+'
+
+# Testcase 9h, Avoid implicit rename if involved as source on other side
+# (Extremely closely related to testcase 3a)
+# Commit O: z/{b,c,d_1}
+# Commit A: z/{b,c,d_2}
+# Commit B: y/{b,c}, x/d_1
+# Expected: y/{b,c}, x/d_2
+# NOTE: If we applied the z/ -> y/ rename to z/d, then we'd end up with
+# a rename/rename(1to2) conflict (z/d -> y/d vs. x/d)
+test_setup_9h () {
+ git init 9h &&
+ (
+ cd 9h &&
+
+ mkdir z &&
+ echo b >z/b &&
+ echo c >z/c &&
+ printf "1\n2\n3\n4\n5\n6\n7\n8\nd\n" >z/d &&
+ git add z &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ test_tick &&
+ echo more >>z/d &&
+ git add z/d &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ mkdir y &&
+ mkdir x &&
+ git mv z/b y/ &&
+ git mv z/c y/ &&
+ git mv z/d x/ &&
+ rmdir z &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '9h: Avoid dir rename on merely modified path' '
+ test_setup_9h &&
+ (
+ cd 9h &&
+
+ git checkout A^0 &&
+
+ git -c merge.directoryRenames=true merge -s recursive B^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 3 out &&
+
+ git rev-parse >actual \
+ HEAD:y/b HEAD:y/c HEAD:x/d &&
+ git rev-parse >expect \
+ O:z/b O:z/c A:z/d &&
+ test_cmp expect actual
+ )
+'
+
+###########################################################################
+# Rules suggested by section 9:
+#
+# If the other side of history did a directory rename to a path that your
+# side renamed away, then ignore that particular rename from the other
+# side of history for any implicit directory renames.
+###########################################################################
+
+###########################################################################
+# SECTION 10: Handling untracked files
+#
+# unpack_trees(), upon which the recursive merge algorithm is based, aborts
+# the operation if untracked or dirty files would be deleted or overwritten
+# by the merge. Unfortunately, unpack_trees() does not understand renames,
+# and if it doesn't abort, then it muddies up the working directory before
+# we even get to the point of detecting renames, so we need some special
+# handling, at least in the case of directory renames.
+###########################################################################
+
+# Testcase 10a, Overwrite untracked: normal rename/delete
+# Commit O: z/{b,c_1}
+# Commit A: z/b + untracked z/c + untracked z/d
+# Commit B: z/{b,d_1}
+# Expected: Aborted Merge +
+# ERROR_MSG(untracked working tree files would be overwritten by merge)
+
+test_setup_10a () {
+ git init 10a &&
+ (
+ cd 10a &&
+
+ mkdir z &&
+ echo b >z/b &&
+ echo c >z/c &&
+ git add z &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git rm z/c &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ git mv z/c z/d &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '10a: Overwrite untracked with normal rename/delete' '
+ test_setup_10a &&
+ (
+ cd 10a &&
+
+ git checkout A^0 &&
+ echo very >z/c &&
+ echo important >z/d &&
+
+ test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
+ test_path_is_missing .git/MERGE_HEAD &&
+ test_grep "The following untracked working tree files would be overwritten by merge" err &&
+
+ git ls-files -s >out &&
+ test_line_count = 1 out &&
+ git ls-files -o >out &&
+ test_line_count = 4 out &&
+
+ echo very >expect &&
+ test_cmp expect z/c &&
+
+ echo important >expect &&
+ test_cmp expect z/d &&
+
+ git rev-parse HEAD:z/b >actual &&
+ git rev-parse O:z/b >expect &&
+ test_cmp expect actual
+ )
+'
+
+# Testcase 10b, Overwrite untracked: dir rename + delete
+# Commit O: z/{b,c_1}
+# Commit A: y/b + untracked y/{c,d,e}
+# Commit B: z/{b,d_1,e}
+# Expected: Failed Merge; y/b + untracked y/c + untracked y/d on disk +
+# z/c_1 -> z/d_1 rename recorded at stage 3 for y/d +
+# ERROR_MSG(refusing to lose untracked file at 'y/d')
+
+test_setup_10b () {
+ git init 10b &&
+ (
+ cd 10b &&
+
+ mkdir z &&
+ echo b >z/b &&
+ echo c >z/c &&
+ git add z &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git rm z/c &&
+ git mv z/ y/ &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ git mv z/c z/d &&
+ echo e >z/e &&
+ git add z/e &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '10b: Overwrite untracked with dir rename + delete' '
+ test_setup_10b &&
+ (
+ cd 10b &&
+
+ git checkout A^0 &&
+ echo very >y/c &&
+ echo important >y/d &&
+ echo contents >y/e &&
+
+ test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ test_path_is_missing .git/MERGE_HEAD &&
+ test_grep "error: The following untracked working tree files would be overwritten by merge" err &&
+
+ git ls-files -s >out &&
+ test_line_count = 1 out &&
+ git ls-files -u >out &&
+ test_line_count = 0 out &&
+ git ls-files -o >out &&
+ test_line_count = 5 out
+ else
+ test_grep "CONFLICT (rename/delete).*Version B\^0 of y/d left in tree at y/d~B\^0" out &&
+ test_grep "Error: Refusing to lose untracked file at y/e; writing to y/e~B\^0 instead" out &&
+
+ git ls-files -s >out &&
+ test_line_count = 3 out &&
+ git ls-files -u >out &&
+ test_line_count = 2 out &&
+ git ls-files -o >out &&
+ test_line_count = 5 out &&
+
+ git rev-parse >actual \
+ :0:y/b :3:y/d :3:y/e &&
+ git rev-parse >expect \
+ O:z/b O:z/c B:z/e &&
+ test_cmp expect actual
+ fi &&
+
+ echo very >expect &&
+ test_cmp expect y/c &&
+
+ echo important >expect &&
+ test_cmp expect y/d &&
+
+ echo contents >expect &&
+ test_cmp expect y/e
+ )
+'
+
+# Testcase 10c, Overwrite untracked: dir rename/rename(1to2)
+# Commit O: z/{a,b}, x/{c,d}
+# Commit A: y/{a,b}, w/c, x/d + different untracked y/c
+# Commit B: z/{a,b,c}, x/d
+# Expected: Failed Merge; y/{a,b} + x/d + untracked y/c +
+# CONFLICT(rename/rename) x/c -> w/c vs y/c +
+# y/c~B^0 +
+# ERROR_MSG(Refusing to lose untracked file at y/c)
+
+test_setup_10c () {
+ git init 10c_$1 &&
+ (
+ cd 10c_$1 &&
+
+ mkdir z x &&
+ echo a >z/a &&
+ echo b >z/b &&
+ echo c >x/c &&
+ echo d >x/d &&
+ git add z x &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ mkdir w &&
+ git mv x/c w/c &&
+ git mv z/ y/ &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ git mv x/c z/ &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '10c1: Overwrite untracked with dir rename/rename(1to2)' '
+ test_setup_10c 1 &&
+ (
+ cd 10c_1 &&
+
+ git checkout A^0 &&
+ echo important >y/c &&
+
+ test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ test_path_is_missing .git/MERGE_HEAD &&
+ test_grep "error: The following untracked working tree files would be overwritten by merge" err &&
+
+ git ls-files -s >out &&
+ test_line_count = 4 out &&
+ git ls-files -u >out &&
+ test_line_count = 0 out &&
+ git ls-files -o >out &&
+ test_line_count = 3 out
+ else
+ test_grep "CONFLICT (rename/rename)" out &&
+ test_grep "Refusing to lose untracked file at y/c; adding as y/c~B\^0 instead" out &&
+
+ git ls-files -s >out &&
+ test_line_count = 6 out &&
+ git ls-files -u >out &&
+ test_line_count = 3 out &&
+ git ls-files -o >out &&
+ test_line_count = 3 out &&
+
+ git rev-parse >actual \
+ :0:y/a :0:y/b :0:x/d :1:x/c :2:w/c :3:y/c &&
+ git rev-parse >expect \
+ O:z/a O:z/b O:x/d O:x/c O:x/c O:x/c &&
+ test_cmp expect actual &&
+
+ git hash-object y/c~B^0 >actual &&
+ git rev-parse O:x/c >expect &&
+ test_cmp expect actual
+ fi &&
+
+ echo important >expect &&
+ test_cmp expect y/c
+ )
+'
+
+test_expect_success '10c2: Overwrite untracked with dir rename/rename(1to2), other direction' '
+ test_setup_10c 2 &&
+ (
+ cd 10c_2 &&
+
+ git reset --hard &&
+ git clean -fdqx &&
+
+ git checkout B^0 &&
+ mkdir y &&
+ echo important >y/c &&
+
+ test_must_fail git -c merge.directoryRenames=true merge -s recursive A^0 >out 2>err &&
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ test_path_is_missing .git/MERGE_HEAD &&
+ test_grep "error: The following untracked working tree files would be overwritten by merge" err &&
+
+ git ls-files -s >out &&
+ test_line_count = 4 out &&
+ git ls-files -u >out &&
+ test_line_count = 0 out &&
+ git ls-files -o >out &&
+ test_line_count = 3 out
+ else
+ test_grep "CONFLICT (rename/rename)" out &&
+ test_grep "Refusing to lose untracked file at y/c; adding as y/c~HEAD instead" out &&
+
+ git ls-files -s >out &&
+ test_line_count = 6 out &&
+ git ls-files -u >out &&
+ test_line_count = 3 out &&
+ git ls-files -o >out &&
+ test_line_count = 3 out &&
+
+ git rev-parse >actual \
+ :0:y/a :0:y/b :0:x/d :1:x/c :3:w/c :2:y/c &&
+ git rev-parse >expect \
+ O:z/a O:z/b O:x/d O:x/c O:x/c O:x/c &&
+ test_cmp expect actual &&
+
+ git hash-object y/c~HEAD >actual &&
+ git rev-parse O:x/c >expect &&
+ test_cmp expect actual
+ fi &&
+
+ echo important >expect &&
+ test_cmp expect y/c
+ )
+'
+
+# Testcase 10d, Delete untracked w/ dir rename/rename(2to1)
+# Commit O: z/{a,b,c_1}, x/{d,e,f_2}
+# Commit A: y/{a,b}, x/{d,e,f_2,wham_1} + untracked y/wham
+# Commit B: z/{a,b,c_1,wham_2}, y/{d,e}
+# Expected: Failed Merge; y/{a,b,d,e} + untracked y/{wham,wham~merged}+
+# CONFLICT(rename/rename) z/c_1 vs x/f_2 -> y/wham
+# ERROR_MSG(Refusing to lose untracked file at y/wham)
+
+test_setup_10d () {
+ git init 10d &&
+ (
+ cd 10d &&
+
+ mkdir z x &&
+ echo a >z/a &&
+ echo b >z/b &&
+ echo c >z/c &&
+ echo d >x/d &&
+ echo e >x/e &&
+ echo f >x/f &&
+ git add z x &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git mv z/c x/wham &&
+ git mv z/ y/ &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ git mv x/f z/wham &&
+ git mv x/ y/ &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '10d: Delete untracked with dir rename/rename(2to1)' '
+ test_setup_10d &&
+ (
+ cd 10d &&
+
+ git checkout A^0 &&
+ echo important >y/wham &&
+
+ test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ test_path_is_missing .git/MERGE_HEAD &&
+ test_grep "error: The following untracked working tree files would be overwritten by merge" err &&
+
+ git ls-files -s >out &&
+ test_line_count = 6 out &&
+ git ls-files -u >out &&
+ test_line_count = 0 out &&
+ git ls-files -o >out &&
+ test_line_count = 3 out
+ else
+ test_grep "CONFLICT (rename/rename)" out &&
+ test_grep "Refusing to lose untracked file at y/wham" out &&
+
+ git ls-files -s >out &&
+ test_line_count = 6 out &&
+ git ls-files -u >out &&
+ test_line_count = 2 out &&
+ git ls-files -o >out &&
+ test_line_count = 3 out &&
+
+ git rev-parse >actual \
+ :0:y/a :0:y/b :0:y/d :0:y/e :2:y/wham :3:y/wham &&
+ git rev-parse >expect \
+ O:z/a O:z/b O:x/d O:x/e O:z/c O:x/f &&
+ test_cmp expect actual &&
+
+ test_must_fail git rev-parse :1:y/wham &&
+
+ # Test that two-way merge in y/wham~merged is as expected
+ git cat-file -p :2:y/wham >expect &&
+ git cat-file -p :3:y/wham >other &&
+ >empty &&
+ test_must_fail git merge-file \
+ -L "HEAD" \
+ -L "" \
+ -L "B^0" \
+ expect empty other &&
+ test_cmp expect y/wham~merged
+ fi &&
+
+ echo important >expect &&
+ test_cmp expect y/wham
+ )
+'
+
+# Testcase 10e, Does git complain about untracked file that's not in the way?
+# Commit O: z/{a,b}
+# Commit A: y/{a,b} + untracked z/c
+# Commit B: z/{a,b,c}
+# Expected: y/{a,b,c} + untracked z/c
+
+test_setup_10e () {
+ git init 10e &&
+ (
+ cd 10e &&
+
+ mkdir z &&
+ echo a >z/a &&
+ echo b >z/b &&
+ git add z &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git mv z/ y/ &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ echo c >z/c &&
+ git add z/c &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_merge_algorithm failure success '10e: Does git complain about untracked file that is not really in the way?' '
+ test_setup_10e &&
+ (
+ cd 10e &&
+
+ git checkout A^0 &&
+ mkdir z &&
+ echo random >z/c &&
+
+ git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
+ test_grep ! "following untracked working tree files would be overwritten by merge" err &&
+
+ git ls-files -s >out &&
+ test_line_count = 3 out &&
+ git ls-files -u >out &&
+ test_line_count = 0 out &&
+ git ls-files -o >out &&
+ test_line_count = 3 out &&
+
+ git rev-parse >actual \
+ :0:y/a :0:y/b :0:y/c &&
+ git rev-parse >expect \
+ O:z/a O:z/b B:z/c &&
+ test_cmp expect actual &&
+
+ echo random >expect &&
+ test_cmp expect z/c
+ )
+'
+
+###########################################################################
+# SECTION 11: Handling dirty (not up-to-date) files
+#
+# unpack_trees(), upon which the recursive merge algorithm is based, aborts
+# the operation if untracked or dirty files would be deleted or overwritten
+# by the merge. Unfortunately, unpack_trees() does not understand renames,
+# and if it doesn't abort, then it muddies up the working directory before
+# we even get to the point of detecting renames, so we need some special
+# handling. This was true even of normal renames, but there are additional
+# codepaths that need special handling with directory renames. Add
+# testcases for both renamed-by-directory-rename-detection and standard
+# rename cases.
+###########################################################################
+
+# Testcase 11a, Avoid losing dirty contents with simple rename
+# Commit O: z/{a,b_v1},
+# Commit A: z/{a,c_v1}, and z/c_v1 has uncommitted mods
+# Commit B: z/{a,b_v2}
+# Expected: ERROR_MSG(Refusing to lose dirty file at z/c) +
+# z/a, staged version of z/c has sha1sum matching B:z/b_v2,
+# z/c~HEAD with contents of B:z/b_v2,
+# z/c with uncommitted mods on top of A:z/c_v1
+
+test_setup_11a () {
+ git init 11a &&
+ (
+ cd 11a &&
+
+ mkdir z &&
+ echo a >z/a &&
+ test_seq 1 10 >z/b &&
+ git add z &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git mv z/b z/c &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ echo 11 >>z/b &&
+ git add z/b &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '11a: Avoid losing dirty contents with simple rename' '
+ test_setup_11a &&
+ (
+ cd 11a &&
+
+ git checkout A^0 &&
+ echo stuff >>z/c &&
+
+ test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ test_path_is_missing .git/MERGE_HEAD &&
+ test_grep "error: Your local changes to the following files would be overwritten by merge" err
+ else
+ test_grep "Refusing to lose dirty file at z/c" out &&
+
+ git ls-files -s >out &&
+ test_line_count = 2 out &&
+ git ls-files -u >out &&
+ test_line_count = 1 out &&
+ git ls-files -o >out &&
+ test_line_count = 3 out &&
+
+ git rev-parse >actual \
+ :0:z/a :2:z/c &&
+ git rev-parse >expect \
+ O:z/a B:z/b &&
+ test_cmp expect actual &&
+
+ git hash-object z/c~HEAD >actual &&
+ git rev-parse B:z/b >expect &&
+ test_cmp expect actual
+ fi &&
+
+ test_seq 1 10 >expected &&
+ echo stuff >>expected &&
+ test_cmp expected z/c
+
+ )
+'
+
+# Testcase 11b, Avoid losing dirty file involved in directory rename
+# Commit O: z/a, x/{b,c_v1}
+# Commit A: z/{a,c_v1}, x/b, and z/c_v1 has uncommitted mods
+# Commit B: y/a, x/{b,c_v2}
+# Expected: y/{a,c_v2}, x/b, z/c_v1 with uncommitted mods untracked,
+# ERROR_MSG(Refusing to lose dirty file at z/c)
+
+
+test_setup_11b () {
+ git init 11b &&
+ (
+ cd 11b &&
+
+ mkdir z x &&
+ echo a >z/a &&
+ echo b >x/b &&
+ test_seq 1 10 >x/c &&
+ git add z x &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git mv x/c z/c &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ git mv z y &&
+ echo 11 >>x/c &&
+ git add x/c &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '11b: Avoid losing dirty file involved in directory rename' '
+ test_setup_11b &&
+ (
+ cd 11b &&
+
+ git checkout A^0 &&
+ echo stuff >>z/c &&
+
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
+ test_path_is_missing .git/MERGE_HEAD &&
+ test_grep "error: Your local changes to the following files would be overwritten by merge" err
+ else
+ git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
+ test_grep "Refusing to lose dirty file at z/c" out &&
+
+ git ls-files -s >out &&
+ test_line_count = 3 out &&
+ git ls-files -u >out &&
+ test_line_count = 0 out &&
+ git ls-files -m >out &&
+ test_line_count = 0 out &&
+ git ls-files -o >out &&
+ test_line_count = 3 out &&
+
+ git rev-parse >actual \
+ :0:x/b :0:y/a :0:y/c &&
+ git rev-parse >expect \
+ O:x/b O:z/a B:x/c &&
+ test_cmp expect actual &&
+
+ git hash-object y/c >actual &&
+ git rev-parse B:x/c >expect &&
+ test_cmp expect actual
+ fi &&
+
+ grep -q stuff z/c &&
+ test_seq 1 10 >expected &&
+ echo stuff >>expected &&
+ test_cmp expected z/c
+ )
+'
+
+# Testcase 11c, Avoid losing not-up-to-date with rename + D/F conflict
+# Commit O: y/a, x/{b,c_v1}
+# Commit A: y/{a,c_v1}, x/b, and y/c_v1 has uncommitted mods
+# Commit B: y/{a,c/d}, x/{b,c_v2}
+# Expected: Abort_msg("following files would be overwritten by merge") +
+# y/c left untouched (still has uncommitted mods)
+
+test_setup_11c () {
+ git init 11c &&
+ (
+ cd 11c &&
+
+ mkdir y x &&
+ echo a >y/a &&
+ echo b >x/b &&
+ test_seq 1 10 >x/c &&
+ git add y x &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git mv x/c y/c &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ mkdir y/c &&
+ echo d >y/c/d &&
+ echo 11 >>x/c &&
+ git add x/c y/c/d &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '11c: Avoid losing not-uptodate with rename + D/F conflict' '
+ test_setup_11c &&
+ (
+ cd 11c &&
+
+ git checkout A^0 &&
+ echo stuff >>y/c &&
+
+ test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ test_path_is_missing .git/MERGE_HEAD &&
+ test_grep "error: Your local changes to the following files would be overwritten by merge" err
+ else
+ test_grep "following files would be overwritten by merge" err
+ fi &&
+
+ grep -q stuff y/c &&
+ test_seq 1 10 >expected &&
+ echo stuff >>expected &&
+ test_cmp expected y/c &&
+
+ git ls-files -s >out &&
+ test_line_count = 3 out &&
+ git ls-files -u >out &&
+ test_line_count = 0 out &&
+ git ls-files -m >out &&
+ test_line_count = 1 out &&
+ git ls-files -o >out &&
+ test_line_count = 3 out
+ )
+'
+
+# Testcase 11d, Avoid losing not-up-to-date with rename + D/F conflict
+# Commit O: z/a, x/{b,c_v1}
+# Commit A: z/{a,c_v1}, x/b, and z/c_v1 has uncommitted mods
+# Commit B: y/{a,c/d}, x/{b,c_v2}
+# Expected: D/F: y/c_v2 vs y/c/d) +
+# Warning_Msg("Refusing to lose dirty file at z/c) +
+# y/{a,c~HEAD,c/d}, x/b, now-untracked z/c_v1 with uncommitted mods
+
+test_setup_11d () {
+ git init 11d &&
+ (
+ cd 11d &&
+
+ mkdir z x &&
+ echo a >z/a &&
+ echo b >x/b &&
+ test_seq 1 10 >x/c &&
+ git add z x &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git mv x/c z/c &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ git mv z y &&
+ mkdir y/c &&
+ echo d >y/c/d &&
+ echo 11 >>x/c &&
+ git add x/c y/c/d &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '11d: Avoid losing not-uptodate with rename + D/F conflict' '
+ test_setup_11d &&
+ (
+ cd 11d &&
+
+ git checkout A^0 &&
+ echo stuff >>z/c &&
+
+ test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ test_path_is_missing .git/MERGE_HEAD &&
+ test_grep "error: Your local changes to the following files would be overwritten by merge" err
+ else
+ test_grep "Refusing to lose dirty file at z/c" out &&
+
+ git ls-files -s >out &&
+ test_line_count = 4 out &&
+ git ls-files -u >out &&
+ test_line_count = 1 out &&
+ git ls-files -o >out &&
+ test_line_count = 4 out &&
+
+ git rev-parse >actual \
+ :0:x/b :0:y/a :0:y/c/d :3:y/c &&
+ git rev-parse >expect \
+ O:x/b O:z/a B:y/c/d B:x/c &&
+ test_cmp expect actual &&
+
+ git hash-object y/c~HEAD >actual &&
+ git rev-parse B:x/c >expect &&
+ test_cmp expect actual
+ fi &&
+
+ grep -q stuff z/c &&
+ test_seq 1 10 >expected &&
+ echo stuff >>expected &&
+ test_cmp expected z/c
+ )
+'
+
+# Testcase 11e, Avoid deleting not-up-to-date with dir rename/rename(1to2)/add
+# Commit O: z/{a,b}, x/{c_1,d}
+# Commit A: y/{a,b,c_2}, x/d, w/c_1, and y/c_2 has uncommitted mods
+# Commit B: z/{a,b,c_1}, x/d
+# Expected: Failed Merge; y/{a,b} + x/d +
+# CONFLICT(rename/rename) x/c_1 -> w/c_1 vs y/c_1 +
+# ERROR_MSG(Refusing to lose dirty file at y/c)
+# y/c~B^0 has O:x/c_1 contents
+# y/c~HEAD has A:y/c_2 contents
+# y/c has dirty file from before merge
+
+test_setup_11e () {
+ git init 11e &&
+ (
+ cd 11e &&
+
+ mkdir z x &&
+ echo a >z/a &&
+ echo b >z/b &&
+ echo c >x/c &&
+ echo d >x/d &&
+ git add z x &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git mv z/ y/ &&
+ echo different >y/c &&
+ mkdir w &&
+ git mv x/c w/ &&
+ git add y/c &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ git mv x/c z/ &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '11e: Avoid deleting not-uptodate with dir rename/rename(1to2)/add' '
+ test_setup_11e &&
+ (
+ cd 11e &&
+
+ git checkout A^0 &&
+ echo mods >>y/c &&
+
+ test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ test_path_is_missing .git/MERGE_HEAD &&
+ test_grep "error: Your local changes to the following files would be overwritten by merge" err
+ else
+ test_grep "CONFLICT (rename/rename)" out &&
+ test_grep "Refusing to lose dirty file at y/c" out &&
+
+ git ls-files -s >out &&
+ test_line_count = 7 out &&
+ git ls-files -u >out &&
+ test_line_count = 4 out &&
+ git ls-files -o >out &&
+ test_line_count = 3 out &&
+
+ git rev-parse >actual \
+ :0:y/a :0:y/b :0:x/d :1:x/c :2:w/c :2:y/c :3:y/c &&
+ git rev-parse >expect \
+ O:z/a O:z/b O:x/d O:x/c O:x/c A:y/c O:x/c &&
+ test_cmp expect actual &&
+
+ # See if y/c~merged has expected contents; requires manually
+ # doing the expected file merge
+ git cat-file -p A:y/c >c1 &&
+ git cat-file -p B:z/c >c2 &&
+ >empty &&
+ test_must_fail git merge-file \
+ -L "HEAD" \
+ -L "" \
+ -L "B^0" \
+ c1 empty c2 &&
+ test_cmp c1 y/c~merged
+ fi &&
+
+ echo different >expected &&
+ echo mods >>expected &&
+ test_cmp expected y/c
+ )
+'
+
+# Testcase 11f, Avoid deleting not-up-to-date w/ dir rename/rename(2to1)
+# Commit O: z/{a,b}, x/{c_1,d_2}
+# Commit A: y/{a,b,wham_1}, x/d_2, except y/wham has uncommitted mods
+# Commit B: z/{a,b,wham_2}, x/c_1
+# Expected: Failed Merge; y/{a,b} + untracked y/{wham~merged} +
+# y/wham with dirty changes from before merge +
+# CONFLICT(rename/rename) x/c vs x/d -> y/wham
+# ERROR_MSG(Refusing to lose dirty file at y/wham)
+
+test_setup_11f () {
+ git init 11f &&
+ (
+ cd 11f &&
+
+ mkdir z x &&
+ echo a >z/a &&
+ echo b >z/b &&
+ test_seq 1 10 >x/c &&
+ echo d >x/d &&
+ git add z x &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git mv z/ y/ &&
+ git mv x/c y/wham &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ git mv x/d z/wham &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '11f: Avoid deleting not-uptodate with dir rename/rename(2to1)' '
+ test_setup_11f &&
+ (
+ cd 11f &&
+
+ git checkout A^0 &&
+ echo important >>y/wham &&
+
+ test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ test_path_is_missing .git/MERGE_HEAD &&
+ test_grep "error: Your local changes to the following files would be overwritten by merge" err
+ else
+ test_grep "CONFLICT (rename/rename)" out &&
+ test_grep "Refusing to lose dirty file at y/wham" out &&
+
+ git ls-files -s >out &&
+ test_line_count = 4 out &&
+ git ls-files -u >out &&
+ test_line_count = 2 out &&
+ git ls-files -o >out &&
+ test_line_count = 3 out &&
+
+ test_must_fail git rev-parse :1:y/wham &&
+
+ git rev-parse >actual \
+ :0:y/a :0:y/b :2:y/wham :3:y/wham &&
+ git rev-parse >expect \
+ O:z/a O:z/b O:x/c O:x/d &&
+ test_cmp expect actual &&
+
+ # Test that two-way merge in y/wham~merged is as expected
+ git cat-file -p :2:y/wham >expect &&
+ git cat-file -p :3:y/wham >other &&
+ >empty &&
+ test_must_fail git merge-file \
+ -L "HEAD" \
+ -L "" \
+ -L "B^0" \
+ expect empty other &&
+ test_cmp expect y/wham~merged
+ fi &&
+
+ test_seq 1 10 >expected &&
+ echo important >>expected &&
+ test_cmp expected y/wham
+ )
+'
+
+###########################################################################
+# SECTION 12: Everything else
+#
+# Tests suggested by others. Tests added after implementation completed
+# and submitted. Grab bag.
+###########################################################################
+
+# Testcase 12a, Moving one directory hierarchy into another
+# (Related to testcase 9a)
+# Commit O: node1/{leaf1,leaf2}, node2/{leaf3,leaf4}
+# Commit A: node1/{leaf1,leaf2,node2/{leaf3,leaf4}}
+# Commit B: node1/{leaf1,leaf2,leaf5}, node2/{leaf3,leaf4,leaf6}
+# Expected: node1/{leaf1,leaf2,leaf5,node2/{leaf3,leaf4,leaf6}}
+
+test_setup_12a () {
+ git init 12a &&
+ (
+ cd 12a &&
+
+ mkdir -p node1 node2 &&
+ echo leaf1 >node1/leaf1 &&
+ echo leaf2 >node1/leaf2 &&
+ echo leaf3 >node2/leaf3 &&
+ echo leaf4 >node2/leaf4 &&
+ git add node1 node2 &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git mv node2/ node1/ &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ echo leaf5 >node1/leaf5 &&
+ echo leaf6 >node2/leaf6 &&
+ git add node1 node2 &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '12a: Moving one directory hierarchy into another' '
+ test_setup_12a &&
+ (
+ cd 12a &&
+
+ git checkout A^0 &&
+
+ git -c merge.directoryRenames=true merge -s recursive B^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 6 out &&
+
+ git rev-parse >actual \
+ HEAD:node1/leaf1 HEAD:node1/leaf2 HEAD:node1/leaf5 \
+ HEAD:node1/node2/leaf3 \
+ HEAD:node1/node2/leaf4 \
+ HEAD:node1/node2/leaf6 &&
+ git rev-parse >expect \
+ O:node1/leaf1 O:node1/leaf2 B:node1/leaf5 \
+ O:node2/leaf3 \
+ O:node2/leaf4 \
+ B:node2/leaf6 &&
+ test_cmp expect actual
+ )
+'
+
+# Testcase 12b1, Moving two directory hierarchies into each other
+# (Related to testcases 1c and 12c)
+# Commit O: node1/{leaf1, leaf2}, node2/{leaf3, leaf4}
+# Commit A: node1/{leaf1, leaf2, node2/{leaf3, leaf4}}
+# Commit B: node2/{leaf3, leaf4, node1/{leaf1, leaf2}}
+# Expected: node1/node2/{leaf3, leaf4}
+# node2/node1/{leaf1, leaf2}
+# NOTE: If there were new files added to the old node1/ or node2/ directories,
+# then we would need to detect renames for those directories and would
+# find that:
+# commit A renames node2/ -> node1/node2/
+# commit B renames node1/ -> node2/node1/
+# Applying those directory renames to the initial result (making all
+# four paths experience a transitive renaming), yields
+# node1/node2/node1/{leaf1, leaf2}
+# node2/node1/node2/{leaf3, leaf4}
+# as the result. It may be really weird to have two directories
+# rename each other, but simple rules give weird results when given
+# weird inputs. HOWEVER, the "If" at the beginning of those NOTE was
+# false; there were no new files added and thus there is no directory
+# rename detection to perform. As such, we just have simple renames
+# and the expected answer is:
+# node1/node2/{leaf3, leaf4}
+# node2/node1/{leaf1, leaf2}
+
+test_setup_12b1 () {
+ git init 12b1 &&
+ (
+ cd 12b1 &&
+
+ mkdir -p node1 node2 &&
+ echo leaf1 >node1/leaf1 &&
+ echo leaf2 >node1/leaf2 &&
+ echo leaf3 >node2/leaf3 &&
+ echo leaf4 >node2/leaf4 &&
+ git add node1 node2 &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git mv node2/ node1/ &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ git mv node1/ node2/ &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_merge_algorithm failure success '12b1: Moving two directory hierarchies into each other' '
+ test_setup_12b1 &&
+ (
+ cd 12b1 &&
+
+ git checkout A^0 &&
+
+ git -c merge.directoryRenames=true merge -s recursive B^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 4 out &&
+
+ git rev-parse >actual \
+ HEAD:node2/node1/leaf1 \
+ HEAD:node2/node1/leaf2 \
+ HEAD:node1/node2/leaf3 \
+ HEAD:node1/node2/leaf4 &&
+ git rev-parse >expect \
+ O:node1/leaf1 \
+ O:node1/leaf2 \
+ O:node2/leaf3 \
+ O:node2/leaf4 &&
+ test_cmp expect actual
+ )
+'
+
+# Testcase 12b2, Moving two directory hierarchies into each other
+# (Related to testcases 1c and 12c)
+# Commit O: node1/{leaf1, leaf2}, node2/{leaf3, leaf4}
+# Commit A: node1/{leaf1, leaf2, leaf5, node2/{leaf3, leaf4}}
+# Commit B: node2/{leaf3, leaf4, leaf6, node1/{leaf1, leaf2}}
+# Expected: node1/node2/{node1/{leaf1, leaf2}, leaf6}
+# node2/node1/{node2/{leaf3, leaf4}, leaf5}
+# NOTE: Without directory renames, we would expect
+# A: node2/leaf3 -> node1/node2/leaf3
+# A: node2/leaf1 -> node1/node2/leaf4
+# A: Adds node1/leaf5
+# B: node1/leaf1 -> node2/node1/leaf1
+# B: node1/leaf2 -> node2/node1/leaf2
+# B: Adds node2/leaf6
+# with directory rename detection, we note that
+# commit A renames node2/ -> node1/node2/
+# commit B renames node1/ -> node2/node1/
+# therefore, applying A's directory rename to the paths added in B gives:
+# B: node1/leaf1 -> node1/node2/node1/leaf1
+# B: node1/leaf2 -> node1/node2/node1/leaf2
+# B: Adds node1/node2/leaf6
+# and applying B's directory rename to the paths added in A gives:
+# A: node2/leaf3 -> node2/node1/node2/leaf3
+# A: node2/leaf1 -> node2/node1/node2/leaf4
+# A: Adds node2/node1/leaf5
+# resulting in the expected
+# node1/node2/{node1/{leaf1, leaf2}, leaf6}
+# node2/node1/{node2/{leaf3, leaf4}, leaf5}
+#
+# You may ask, is it weird to have two directories rename each other?
+# To which, I can do no more than shrug my shoulders and say that
+# even simple rules give weird results when given weird inputs.
+
+test_setup_12b2 () {
+ git init 12b2 &&
+ (
+ cd 12b2 &&
+
+ mkdir -p node1 node2 &&
+ echo leaf1 >node1/leaf1 &&
+ echo leaf2 >node1/leaf2 &&
+ echo leaf3 >node2/leaf3 &&
+ echo leaf4 >node2/leaf4 &&
+ git add node1 node2 &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git mv node2/ node1/ &&
+ echo leaf5 >node1/leaf5 &&
+ git add node1/leaf5 &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ git mv node1/ node2/ &&
+ echo leaf6 >node2/leaf6 &&
+ git add node2/leaf6 &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '12b2: Moving two directory hierarchies into each other' '
+ test_setup_12b2 &&
+ (
+ cd 12b2 &&
+
+ git checkout A^0 &&
+
+ git -c merge.directoryRenames=true merge -s recursive B^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 6 out &&
+
+ git rev-parse >actual \
+ HEAD:node1/node2/node1/leaf1 \
+ HEAD:node1/node2/node1/leaf2 \
+ HEAD:node2/node1/node2/leaf3 \
+ HEAD:node2/node1/node2/leaf4 \
+ HEAD:node2/node1/leaf5 \
+ HEAD:node1/node2/leaf6 &&
+ git rev-parse >expect \
+ O:node1/leaf1 \
+ O:node1/leaf2 \
+ O:node2/leaf3 \
+ O:node2/leaf4 \
+ A:node1/leaf5 \
+ B:node2/leaf6 &&
+ test_cmp expect actual
+ )
+'
+
+# Testcase 12c1, Moving two directory hierarchies into each other w/ content merge
+# (Related to testcase 12b)
+# Commit O: node1/{ leaf1_1, leaf2_1}, node2/{leaf3_1, leaf4_1}
+# Commit A: node1/{ leaf1_2, leaf2_2, node2/{leaf3_2, leaf4_2}}
+# Commit B: node2/{node1/{leaf1_3, leaf2_3}, leaf3_3, leaf4_3}
+# Expected: Content merge conflicts for each of:
+# node1/node2/node1/{leaf1, leaf2},
+# node2/node1/node2/{leaf3, leaf4}
+# NOTE: This is *exactly* like 12b1, except that every path is modified on
+# each side of the merge.
+
+test_setup_12c1 () {
+ git init 12c1 &&
+ (
+ cd 12c1 &&
+
+ mkdir -p node1 node2 &&
+ printf "1\n2\n3\n4\n5\n6\n7\n8\nleaf1\n" >node1/leaf1 &&
+ printf "1\n2\n3\n4\n5\n6\n7\n8\nleaf2\n" >node1/leaf2 &&
+ printf "1\n2\n3\n4\n5\n6\n7\n8\nleaf3\n" >node2/leaf3 &&
+ printf "1\n2\n3\n4\n5\n6\n7\n8\nleaf4\n" >node2/leaf4 &&
+ git add node1 node2 &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git mv node2/ node1/ &&
+ for i in $(git ls-files); do echo side A >>$i; done &&
+ git add -u &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ git mv node1/ node2/ &&
+ for i in $(git ls-files); do echo side B >>$i; done &&
+ git add -u &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_merge_algorithm failure success '12c1: Moving one directory hierarchy into another w/ content merge' '
+ test_setup_12c1 &&
+ (
+ cd 12c1 &&
+
+ git checkout A^0 &&
+
+ test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 &&
+
+ git ls-files -u >out &&
+ test_line_count = 12 out &&
+
+ git rev-parse >actual \
+ :1:node2/node1/leaf1 \
+ :1:node2/node1/leaf2 \
+ :1:node1/node2/leaf3 \
+ :1:node1/node2/leaf4 \
+ :2:node2/node1/leaf1 \
+ :2:node2/node1/leaf2 \
+ :2:node1/node2/leaf3 \
+ :2:node1/node2/leaf4 \
+ :3:node2/node1/leaf1 \
+ :3:node2/node1/leaf2 \
+ :3:node1/node2/leaf3 \
+ :3:node1/node2/leaf4 &&
+ git rev-parse >expect \
+ O:node1/leaf1 \
+ O:node1/leaf2 \
+ O:node2/leaf3 \
+ O:node2/leaf4 \
+ A:node1/leaf1 \
+ A:node1/leaf2 \
+ A:node1/node2/leaf3 \
+ A:node1/node2/leaf4 \
+ B:node2/node1/leaf1 \
+ B:node2/node1/leaf2 \
+ B:node2/leaf3 \
+ B:node2/leaf4 &&
+ test_cmp expect actual
+ )
+'
+
+# Testcase 12c2, Moving two directory hierarchies into each other w/ content merge
+# (Related to testcase 12b)
+# Commit O: node1/{ leaf1_1, leaf2_1}, node2/{leaf3_1, leaf4_1}
+# Commit A: node1/{ leaf1_2, leaf2_2, node2/{leaf3_2, leaf4_2}, leaf5}
+# Commit B: node2/{node1/{leaf1_3, leaf2_3}, leaf3_3, leaf4_3, leaf6}
+# Expected: Content merge conflicts for each of:
+# node1/node2/node1/{leaf1, leaf2}
+# node2/node1/node2/{leaf3, leaf4}
+# plus
+# node2/node1/leaf5
+# node1/node2/leaf6
+# NOTE: This is *exactly* like 12b2, except that every path from O is modified
+# on each side of the merge.
+
+test_setup_12c2 () {
+ git init 12c2 &&
+ (
+ cd 12c2 &&
+
+ mkdir -p node1 node2 &&
+ printf "1\n2\n3\n4\n5\n6\n7\n8\nleaf1\n" >node1/leaf1 &&
+ printf "1\n2\n3\n4\n5\n6\n7\n8\nleaf2\n" >node1/leaf2 &&
+ printf "1\n2\n3\n4\n5\n6\n7\n8\nleaf3\n" >node2/leaf3 &&
+ printf "1\n2\n3\n4\n5\n6\n7\n8\nleaf4\n" >node2/leaf4 &&
+ git add node1 node2 &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git mv node2/ node1/ &&
+ for i in $(git ls-files); do echo side A >>$i; done &&
+ git add -u &&
+ echo leaf5 >node1/leaf5 &&
+ git add node1/leaf5 &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ git mv node1/ node2/ &&
+ for i in $(git ls-files); do echo side B >>$i; done &&
+ git add -u &&
+ echo leaf6 >node2/leaf6 &&
+ git add node2/leaf6 &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '12c2: Moving one directory hierarchy into another w/ content merge' '
+ test_setup_12c2 &&
+ (
+ cd 12c2 &&
+
+ git checkout A^0 &&
+
+ test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 14 out &&
+ git ls-files -u >out &&
+ test_line_count = 12 out &&
+
+ git rev-parse >actual \
+ :1:node1/node2/node1/leaf1 \
+ :1:node1/node2/node1/leaf2 \
+ :1:node2/node1/node2/leaf3 \
+ :1:node2/node1/node2/leaf4 \
+ :2:node1/node2/node1/leaf1 \
+ :2:node1/node2/node1/leaf2 \
+ :2:node2/node1/node2/leaf3 \
+ :2:node2/node1/node2/leaf4 \
+ :3:node1/node2/node1/leaf1 \
+ :3:node1/node2/node1/leaf2 \
+ :3:node2/node1/node2/leaf3 \
+ :3:node2/node1/node2/leaf4 \
+ :0:node2/node1/leaf5 \
+ :0:node1/node2/leaf6 &&
+ git rev-parse >expect \
+ O:node1/leaf1 \
+ O:node1/leaf2 \
+ O:node2/leaf3 \
+ O:node2/leaf4 \
+ A:node1/leaf1 \
+ A:node1/leaf2 \
+ A:node1/node2/leaf3 \
+ A:node1/node2/leaf4 \
+ B:node2/node1/leaf1 \
+ B:node2/node1/leaf2 \
+ B:node2/leaf3 \
+ B:node2/leaf4 \
+ A:node1/leaf5 \
+ B:node2/leaf6 &&
+ test_cmp expect actual
+ )
+'
+
+# Testcase 12d, Rename/merge of subdirectory into the root
+# Commit O: a/b/subdir/foo
+# Commit A: subdir/foo
+# Commit B: a/b/subdir/foo, a/b/bar
+# Expected: subdir/foo, bar
+
+test_setup_12d () {
+ git init 12d &&
+ (
+ cd 12d &&
+
+ mkdir -p a/b/subdir &&
+ test_commit a/b/subdir/foo &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ mkdir subdir &&
+ git mv a/b/subdir/foo.t subdir/foo.t &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ test_commit a/b/bar
+ )
+}
+
+test_expect_success '12d: Rename/merge subdir into the root, variant 1' '
+ test_setup_12d &&
+ (
+ cd 12d &&
+
+ git checkout A^0 &&
+
+ git -c merge.directoryRenames=true merge -s recursive B^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 2 out &&
+
+ git rev-parse >actual \
+ HEAD:subdir/foo.t HEAD:bar.t &&
+ git rev-parse >expect \
+ O:a/b/subdir/foo.t B:a/b/bar.t &&
+ test_cmp expect actual &&
+
+ git hash-object bar.t >actual &&
+ git rev-parse B:a/b/bar.t >expect &&
+ test_cmp expect actual &&
+
+ test_must_fail git rev-parse HEAD:a/b/subdir/foo.t &&
+ test_must_fail git rev-parse HEAD:a/b/bar.t &&
+ test_path_is_missing a/ &&
+ test_path_is_file bar.t
+ )
+'
+
+# Testcase 12e, Rename/merge of subdirectory into the root
+# Commit O: a/b/foo
+# Commit A: foo
+# Commit B: a/b/foo, a/b/bar
+# Expected: foo, bar
+
+test_setup_12e () {
+ git init 12e &&
+ (
+ cd 12e &&
+
+ mkdir -p a/b &&
+ test_commit a/b/foo &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ mkdir subdir &&
+ git mv a/b/foo.t foo.t &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ test_commit a/b/bar
+ )
+}
+
+test_expect_success '12e: Rename/merge subdir into the root, variant 2' '
+ test_setup_12e &&
+ (
+ cd 12e &&
+
+ git checkout A^0 &&
+
+ git -c merge.directoryRenames=true merge -s recursive B^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 2 out &&
+
+ git rev-parse >actual \
+ HEAD:foo.t HEAD:bar.t &&
+ git rev-parse >expect \
+ O:a/b/foo.t B:a/b/bar.t &&
+ test_cmp expect actual &&
+
+ git hash-object bar.t >actual &&
+ git rev-parse B:a/b/bar.t >expect &&
+ test_cmp expect actual &&
+
+ test_must_fail git rev-parse HEAD:a/b/foo.t &&
+ test_must_fail git rev-parse HEAD:a/b/bar.t &&
+ test_path_is_missing a/ &&
+ test_path_is_file bar.t
+ )
+'
+
+# Testcase 12f, Rebase of patches with big directory rename
+# Commit O:
+# dir/subdir/{a,b,c,d,e_O,Makefile_TOP_O}
+# dir/subdir/tweaked/{f,g,h,Makefile_SUB_O}
+# dir/unchanged/<LOTS OF FILES>
+# Commit A:
+# (Remove f & g, move e into newsubdir, rename dir/->folder/, modify files)
+# folder/subdir/{a,b,c,d,Makefile_TOP_A}
+# folder/subdir/newsubdir/e_A
+# folder/subdir/tweaked/{h,Makefile_SUB_A}
+# folder/unchanged/<LOTS OF FILES>
+# Commit B1:
+# (add newfile.{c,py}, modify underscored files)
+# dir/{a,b,c,d,e_B1,Makefile_TOP_B1,newfile.c}
+# dir/tweaked/{f,g,h,Makefile_SUB_B1,newfile.py}
+# dir/unchanged/<LOTS OF FILES>
+# Commit B2:
+# (Modify e further, add newfile.rs)
+# dir/{a,b,c,d,e_B2,Makefile_TOP_B1,newfile.c,newfile.rs}
+# dir/tweaked/{f,g,h,Makefile_SUB_B1,newfile.py}
+# dir/unchanged/<LOTS OF FILES>
+# Expected:
+# B1-picked:
+# folder/subdir/{a,b,c,d,Makefile_TOP_Merge1,newfile.c}
+# folder/subdir/newsubdir/e_Merge1
+# folder/subdir/tweaked/{h,Makefile_SUB_Merge1,newfile.py}
+# folder/unchanged/<LOTS OF FILES>
+# B2-picked:
+# folder/subdir/{a,b,c,d,Makefile_TOP_Merge1,newfile.c,newfile.rs}
+# folder/subdir/newsubdir/e_Merge2
+# folder/subdir/tweaked/{h,Makefile_SUB_Merge1,newfile.py}
+# folder/unchanged/<LOTS OF FILES>
+# Things being checked here:
+# 1. dir/subdir/newfile.c does not get pushed into folder/subdir/newsubdir/.
+# dir/subdir/{a,b,c,d} -> folder/subdir/{a,b,c,d} looks like
+# dir/ -> folder/,
+# whereas dir/subdir/e -> folder/subdir/newsubdir/e looks like
+# dir/subdir/ -> folder/subdir/newsubdir/
+# and if we note that newfile.c is found in dir/subdir/, we might overlook
+# the dir/ -> folder/ rule that has more weight. Older git versions did
+# this.
+# 2. The code to do trivial directory resolves. Note that
+# dir/subdir/unchanged/ is unchanged and can be deleted, and files in the
+# new folder/subdir/unchanged/ are not needed as a target to any renames.
+# Thus, in the second collect_merge_info_callback() we can just resolve
+# these two directories trivially without recursing.)
+# 3. Exercising the codepaths for caching renames and deletes from one cherry
+# pick and re-applying them in the subsequent one.
+
+test_setup_12f () {
+ git init 12f &&
+ (
+ cd 12f &&
+
+ mkdir -p dir/unchanged &&
+ mkdir -p dir/subdir/tweaked &&
+ echo a >dir/subdir/a &&
+ echo b >dir/subdir/b &&
+ echo c >dir/subdir/c &&
+ echo d >dir/subdir/d &&
+ test_seq 1 10 >dir/subdir/e &&
+ test_seq 10 20 >dir/subdir/Makefile &&
+ echo f >dir/subdir/tweaked/f &&
+ echo g >dir/subdir/tweaked/g &&
+ echo h >dir/subdir/tweaked/h &&
+ test_seq 20 30 >dir/subdir/tweaked/Makefile &&
+ for i in $(test_seq 1 88); do
+ echo content $i >dir/unchanged/file_$i
+ done &&
+ git add . &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git switch A &&
+ git rm dir/subdir/tweaked/f dir/subdir/tweaked/g &&
+ test_seq 2 10 >dir/subdir/e &&
+ test_seq 11 20 >dir/subdir/Makefile &&
+ test_seq 21 30 >dir/subdir/tweaked/Makefile &&
+ mkdir dir/subdir/newsubdir &&
+ git mv dir/subdir/e dir/subdir/newsubdir/ &&
+ git mv dir folder &&
+ git add . &&
+ git commit -m "A" &&
+
+ git switch B &&
+ mkdir dir/subdir/newsubdir/ &&
+ echo c code >dir/subdir/newfile.c &&
+ echo python code >dir/subdir/newsubdir/newfile.py &&
+ test_seq 1 11 >dir/subdir/e &&
+ test_seq 10 21 >dir/subdir/Makefile &&
+ test_seq 20 31 >dir/subdir/tweaked/Makefile &&
+ git add . &&
+ git commit -m "B1" &&
+
+ echo rust code >dir/subdir/newfile.rs &&
+ test_seq 1 12 >dir/subdir/e &&
+ git add . &&
+ git commit -m "B2"
+ )
+}
+
+test_expect_merge_algorithm failure success '12f: Trivial directory resolve, caching, all kinds of fun' '
+ test_setup_12f &&
+ (
+ cd 12f &&
+
+ git checkout A^0 &&
+ git branch Bmod B &&
+
+ GIT_TRACE2_PERF="$(pwd)/trace.output" git -c merge.directoryRenames=true rebase A Bmod &&
+
+ echo Checking the pick of B1... &&
+
+ test_must_fail git rev-parse Bmod~1:dir &&
+
+ git ls-tree -r Bmod~1 >out &&
+ test_line_count = 98 out &&
+
+ git diff --name-status A Bmod~1 >actual &&
+ q_to_tab >expect <<-\EOF &&
+ MQfolder/subdir/Makefile
+ AQfolder/subdir/newfile.c
+ MQfolder/subdir/newsubdir/e
+ AQfolder/subdir/newsubdir/newfile.py
+ MQfolder/subdir/tweaked/Makefile
+ EOF
+ test_cmp expect actual &&
+
+ # Three-way merged files
+ test_seq 2 11 >e_Merge1 &&
+ test_seq 11 21 >Makefile_TOP &&
+ test_seq 21 31 >Makefile_SUB &&
+ git hash-object >expect \
+ e_Merge1 \
+ Makefile_TOP \
+ Makefile_SUB &&
+ git rev-parse >actual \
+ Bmod~1:folder/subdir/newsubdir/e \
+ Bmod~1:folder/subdir/Makefile \
+ Bmod~1:folder/subdir/tweaked/Makefile &&
+ test_cmp expect actual &&
+
+ # New files showed up at the right location with right contents
+ git rev-parse >expect \
+ B~1:dir/subdir/newfile.c \
+ B~1:dir/subdir/newsubdir/newfile.py &&
+ git rev-parse >actual \
+ Bmod~1:folder/subdir/newfile.c \
+ Bmod~1:folder/subdir/newsubdir/newfile.py &&
+ test_cmp expect actual &&
+
+ # Removed files
+ test_path_is_missing folder/subdir/tweaked/f &&
+ test_path_is_missing folder/subdir/tweaked/g &&
+
+ # Unchanged files or directories
+ git rev-parse >actual \
+ Bmod~1:folder/subdir/a \
+ Bmod~1:folder/subdir/b \
+ Bmod~1:folder/subdir/c \
+ Bmod~1:folder/subdir/d \
+ Bmod~1:folder/unchanged \
+ Bmod~1:folder/subdir/tweaked/h &&
+ git rev-parse >expect \
+ O:dir/subdir/a \
+ O:dir/subdir/b \
+ O:dir/subdir/c \
+ O:dir/subdir/d \
+ O:dir/unchanged \
+ O:dir/subdir/tweaked/h &&
+ test_cmp expect actual &&
+
+ echo Checking the pick of B2... &&
+
+ test_must_fail git rev-parse Bmod:dir &&
+
+ git ls-tree -r Bmod >out &&
+ test_line_count = 99 out &&
+
+ git diff --name-status Bmod~1 Bmod >actual &&
+ q_to_tab >expect <<-\EOF &&
+ AQfolder/subdir/newfile.rs
+ MQfolder/subdir/newsubdir/e
+ EOF
+ test_cmp expect actual &&
+
+ # Three-way merged file
+ test_seq 2 12 >e_Merge2 &&
+ git hash-object e_Merge2 >expect &&
+ git rev-parse Bmod:folder/subdir/newsubdir/e >actual &&
+ test_cmp expect actual &&
+
+ grep region_enter.*collect_merge_info trace.output >collect &&
+ test_line_count = 4 collect &&
+ grep region_enter.*process_entries$ trace.output >process &&
+ test_line_count = 2 process
+ )
+'
+
+# Testcase 12g, Testcase with two kinds of "relevant" renames
+# Commit O: somefile_O, subdir/{a_O,b_O}
+# Commit A: somefile_A, subdir/{a_O,b_O,c_A}
+# Commit B: newfile_B, newdir/{a_B,b_B}
+# Expected: newfile_{merged}, newdir/{a_B,b_B,c_A}
+
+test_setup_12g () {
+ git init 12g &&
+ (
+ cd 12g &&
+
+ mkdir -p subdir &&
+ test_write_lines upon a time there was a >somefile &&
+ test_write_lines 1 2 3 4 5 6 7 8 9 10 >subdir/a &&
+ test_write_lines one two three four five six >subdir/b &&
+ git add . &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git switch A &&
+ test_write_lines once upon a time there was a >somefile &&
+ > subdir/c &&
+ git add somefile subdir/c &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ git mv somefile newfile &&
+ git mv subdir newdir &&
+ echo repo >>newfile &&
+ test_write_lines 1 2 3 4 5 6 7 8 9 10 11 >newdir/a &&
+ test_write_lines one two three four five six seven >newdir/b &&
+ git add newfile newdir &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '12g: Testcase with two kinds of "relevant" renames' '
+ test_setup_12g &&
+ (
+ cd 12g &&
+
+ git checkout A^0 &&
+
+ git -c merge.directoryRenames=true merge -s recursive B^0 &&
+
+ test_write_lines once upon a time there was a repo >expect &&
+ test_cmp expect newfile &&
+
+ git ls-files -s >out &&
+ test_line_count = 4 out &&
+
+ git rev-parse >actual \
+ HEAD:newdir/a HEAD:newdir/b HEAD:newdir/c &&
+ git rev-parse >expect \
+ B:newdir/a B:newdir/b A:subdir/c &&
+ test_cmp expect actual &&
+
+ test_must_fail git rev-parse HEAD:subdir/a &&
+ test_must_fail git rev-parse HEAD:subdir/b &&
+ test_must_fail git rev-parse HEAD:subdir/c &&
+ test_path_is_missing subdir/ &&
+ test_path_is_file newdir/c
+ )
+'
+
+# Testcase 12h, Testcase with two kinds of "relevant" renames
+# Commit O: olddir/{a_1, b}
+# Commit A: newdir/{a_2, b}
+# Commit B: olddir/{alpha_1, b}
+# Expected: newdir/{alpha_2, b}
+
+test_setup_12h () {
+ git init 12h &&
+ (
+ cd 12h &&
+
+ mkdir olddir &&
+ test_seq 3 8 >olddir/a &&
+ >olddir/b &&
+ git add olddir &&
+ git commit -m orig &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git switch A &&
+ test_seq 3 10 >olddir/a &&
+ git add olddir/a &&
+ git mv olddir newdir &&
+ git commit -m A &&
+
+ git switch B &&
+
+ git mv olddir/a olddir/alpha &&
+ git commit -m B
+ )
+}
+
+test_expect_failure '12h: renaming a file within a renamed directory' '
+ test_setup_12h &&
+ (
+ cd 12h &&
+
+ git checkout A^0 &&
+
+ test_might_fail git -c merge.directoryRenames=true merge -s recursive B^0 &&
+
+ git ls-files >tracked &&
+ test_line_count = 2 tracked &&
+
+ test_path_is_missing olddir/a &&
+ test_path_is_file newdir/alpha &&
+ test_path_is_file newdir/b &&
+
+ git rev-parse >actual \
+ HEAD:newdir/alpha HEAD:newdir/b &&
+ git rev-parse >expect \
+ A:newdir/a O:oldir/b &&
+ test_cmp expect actual
+ )
+'
+
+# Testcase 12i, Directory rename causes rename-to-self
+# Commit O: source/{subdir/foo, bar, baz_1}
+# Commit A: source/{foo, bar, baz_1}
+# Commit B: source/{subdir/{foo, bar}, baz_2}
+# Expected: source/{foo, bar, baz_2}, with conflicts on
+# source/bar vs. source/subdir/bar
+
+test_setup_12i () {
+ git init 12i &&
+ (
+ cd 12i &&
+
+ mkdir -p source/subdir &&
+ echo foo >source/subdir/foo &&
+ echo bar >source/bar &&
+ echo baz >source/baz &&
+ git add source &&
+ git commit -m orig &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git switch A &&
+ git mv source/subdir/foo source/foo &&
+ git commit -m A &&
+
+ git switch B &&
+ git mv source/bar source/subdir/bar &&
+ echo more baz >>source/baz &&
+ git commit -m B
+ )
+}
+
+test_expect_success '12i: Directory rename causes rename-to-self' '
+ test_setup_12i &&
+ (
+ cd 12i &&
+
+ git checkout A^0 &&
+
+ test_must_fail git -c merge.directoryRenames=conflict merge -s recursive B^0 &&
+
+ test_path_is_missing source/subdir &&
+ test_path_is_file source/bar &&
+ test_path_is_file source/baz &&
+
+ git ls-files | uniq >tracked &&
+ test_line_count = 3 tracked &&
+
+ git status --porcelain -uno >actual &&
+ cat >expect <<-\EOF &&
+ UU source/bar
+ M source/baz
+ EOF
+ test_cmp expect actual
+ )
+'
+
+# Testcase 12j, Directory rename to root causes rename-to-self
+# Commit O: {subdir/foo, bar, baz_1}
+# Commit A: {foo, bar, baz_1}
+# Commit B: {subdir/{foo, bar}, baz_2}
+# Expected: {foo, bar, baz_2}, with conflicts on bar vs. subdir/bar
+
+test_setup_12j () {
+ git init 12j &&
+ (
+ cd 12j &&
+
+ mkdir -p subdir &&
+ echo foo >subdir/foo &&
+ echo bar >bar &&
+ echo baz >baz &&
+ git add . &&
+ git commit -m orig &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git switch A &&
+ git mv subdir/foo foo &&
+ git commit -m A &&
+
+ git switch B &&
+ git mv bar subdir/bar &&
+ echo more baz >>baz &&
+ git commit -m B
+ )
+}
+
+test_expect_success '12j: Directory rename to root causes rename-to-self' '
+ test_setup_12j &&
+ (
+ cd 12j &&
+
+ git checkout A^0 &&
+
+ test_must_fail git -c merge.directoryRenames=conflict merge -s recursive B^0 &&
+
+ test_path_is_missing subdir &&
+ test_path_is_file bar &&
+ test_path_is_file baz &&
+
+ git ls-files | uniq >tracked &&
+ test_line_count = 3 tracked &&
+
+ git status --porcelain -uno >actual &&
+ cat >expect <<-\EOF &&
+ UU bar
+ M baz
+ EOF
+ test_cmp expect actual
+ )
+'
+
+# Testcase 12k, Directory rename with sibling causes rename-to-self
+# Commit O: dirB/foo, dirA/{bar, baz_1}
+# Commit A: dirA/{foo, bar, baz_1}
+# Commit B: dirB/{foo, bar}, dirA/baz_2
+# Expected: dirA/{foo, bar, baz_2}, with conflicts on dirA/bar vs. dirB/bar
+
+test_setup_12k () {
+ git init 12k &&
+ (
+ cd 12k &&
+
+ mkdir dirA dirB &&
+ echo foo >dirB/foo &&
+ echo bar >dirA/bar &&
+ echo baz >dirA/baz &&
+ git add . &&
+ git commit -m orig &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git switch A &&
+ git mv dirB/* dirA/ &&
+ git commit -m A &&
+
+ git switch B &&
+ git mv dirA/bar dirB/bar &&
+ echo more baz >>dirA/baz &&
+ git commit -m B
+ )
+}
+
+test_expect_success '12k: Directory rename with sibling causes rename-to-self' '
+ test_setup_12k &&
+ (
+ cd 12k &&
+
+ git checkout A^0 &&
+
+ test_must_fail git -c merge.directoryRenames=conflict merge -s recursive B^0 &&
+
+ test_path_is_missing dirB &&
+ test_path_is_file dirA/bar &&
+ test_path_is_file dirA/baz &&
+
+ git ls-files | uniq >tracked &&
+ test_line_count = 3 tracked &&
+
+ git status --porcelain -uno >actual &&
+ cat >expect <<-\EOF &&
+ UU dirA/bar
+ M dirA/baz
+ EOF
+ test_cmp expect actual
+ )
+'
+
+# Testcase 12l, Both sides rename a directory into the other side, both add
+# a file which after directory renames are the same filename
+# Commit O: sub1/file, sub2/other
+# Commit A: sub3/file, sub2/{other, new_add_add_file_1}
+# Commit B: sub1/{file, newfile}, sub1/sub2/{other, new_add_add_file_2}
+#
+# In words:
+# A: sub1/ -> sub3/, add sub2/new_add_add_file_1
+# B: sub2/ -> sub1/sub2, add sub1/newfile, add sub1/sub2/new_add_add_file_2
+#
+# Expected: sub3/{file, newfile, sub2/other}
+# CONFLICT (add/add): sub1/sub2/new_add_add_file
+#
+# Note that sub1/newfile is not extraneous. Directory renames are only
+# detected if they are needed, and they are only needed if the old directory
+# had a new file added on the opposite side of history. So sub1/newfile
+# is needed for there to be a sub1/ -> sub3/ rename.
+
+test_setup_12l () {
+ git init 12l_$1 &&
+ (
+ cd 12l_$1 &&
+
+ mkdir sub1 sub2
+ echo file >sub1/file &&
+ echo other >sub2/other &&
+ git add sub1 sub2 &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git mv sub1 sub3 &&
+ echo conflicting >sub2/new_add_add_file &&
+ git add sub2 &&
+ test_tick &&
+ git add -u &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ echo dissimilar >sub2/new_add_add_file &&
+ echo brand >sub1/newfile &&
+ git add sub1 sub2 &&
+ git mv sub2 sub1 &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_merge_algorithm failure success '12l (B into A): Rename into each other + add/add conflict' '
+ test_setup_12l BintoA &&
+ (
+ cd 12l_BintoA &&
+
+ git checkout -q A^0 &&
+
+ test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 &&
+
+ test_stdout_line_count = 5 git ls-files -s &&
+
+ git rev-parse >actual \
+ :0:sub3/file :0:sub3/newfile :0:sub3/sub2/other \
+ :2:sub1/sub2/new_add_add_file \
+ :3:sub1/sub2/new_add_add_file &&
+ git rev-parse >expect \
+ O:sub1/file B:sub1/newfile O:sub2/other \
+ A:sub2/new_add_add_file \
+ B:sub1/sub2/new_add_add_file &&
+ test_cmp expect actual &&
+
+ git ls-files -o >actual &&
+ test_write_lines actual expect >expect &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_merge_algorithm failure success '12l (A into B): Rename into each other + add/add conflict' '
+ test_setup_12l AintoB &&
+ (
+ cd 12l_AintoB &&
+
+ git checkout -q B^0 &&
+
+ test_must_fail git -c merge.directoryRenames=true merge -s recursive A^0 &&
+
+ test_stdout_line_count = 5 git ls-files -s &&
+
+ git rev-parse >actual \
+ :0:sub3/file :0:sub3/newfile :0:sub3/sub2/other \
+ :2:sub1/sub2/new_add_add_file \
+ :3:sub1/sub2/new_add_add_file &&
+ git rev-parse >expect \
+ O:sub1/file B:sub1/newfile O:sub2/other \
+ B:sub1/sub2/new_add_add_file \
+ A:sub2/new_add_add_file &&
+ test_cmp expect actual &&
+
+ git ls-files -o >actual &&
+ test_write_lines actual expect >expect &&
+ test_cmp expect actual
+ )
+'
+
+# Testcase 12m, Directory rename, plus change of parent dir to symlink
+# Commit O: dir/subdir/file
+# Commit A: renamed-dir/subdir/file
+# Commit B: dir/subdir
+# In words:
+# A: dir/subdir/ -> renamed-dir/subdir
+# B: delete dir/subdir/file, add dir/subdir as symlink
+#
+# Expected: CONFLICT (rename/delete): renamed-dir/subdir/file,
+# CONFLICT (file location): renamed-dir/subdir vs. dir/subdir
+# CONFLICT (directory/file): renamed-dir/subdir symlink has
+# renamed-dir/subdir in the way
+
+test_setup_12m () {
+ git init 12m &&
+ (
+ cd 12m &&
+
+ mkdir -p dir/subdir &&
+ echo 1 >dir/subdir/file &&
+ git add . &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git switch A &&
+ git mv dir/ renamed-dir/ &&
+ git add . &&
+ git commit -m "A" &&
+
+ git switch B &&
+ git rm dir/subdir/file &&
+ mkdir dir &&
+ ln -s /dev/null dir/subdir &&
+ git add . &&
+ git commit -m "B"
+ )
+}
+
+test_expect_merge_algorithm failure success '12m: Change parent of renamed-dir to symlink on other side' '
+ test_setup_12m &&
+ (
+ cd 12m &&
+
+ git checkout -q A^0 &&
+
+ test_must_fail git -c merge.directoryRenames=conflict merge -s recursive B^0 &&
+
+ test_stdout_line_count = 3 git ls-files -s &&
+ test_stdout_line_count = 2 ls -1 renamed-dir &&
+ test_path_is_missing dir
+ )
+'
+
+###########################################################################
+# SECTION 13: Checking informational and conflict messages
+#
+# A year after directory rename detection became the default, it was
+# instead decided to report conflicts on the pathname on the basis that
+# some users may expect the new files added or moved into a directory to
+# be unrelated to all the other files in that directory, and thus that
+# directory rename detection is unexpected. Test that the messages printed
+# match our expectation.
+###########################################################################
+
+# Testcase 13a, Basic directory rename with newly added files
+# Commit O: z/{b,c}
+# Commit A: y/{b,c}
+# Commit B: z/{b,c,d,e/f}
+# Expected: y/{b,c,d,e/f}, with notices/conflicts for both y/d and y/e/f
+
+test_setup_13a () {
+ git init 13a_$1 &&
+ (
+ cd 13a_$1 &&
+
+ mkdir z &&
+ echo b >z/b &&
+ echo c >z/c &&
+ git add z &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git mv z y &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ echo d >z/d &&
+ mkdir z/e &&
+ echo f >z/e/f &&
+ git add z/d z/e/f &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '13a(conflict): messages for newly added files' '
+ test_setup_13a conflict &&
+ (
+ cd 13a_conflict &&
+
+ git checkout A^0 &&
+
+ test_must_fail git merge -s recursive B^0 >out 2>err &&
+
+ test_grep CONFLICT..file.location.*z/e/f.added.in.B^0.*y/e/f out &&
+ test_grep CONFLICT..file.location.*z/d.added.in.B^0.*y/d out &&
+
+ git ls-files >paths &&
+ ! grep z/ paths &&
+ grep "y/[de]" paths &&
+
+ test_path_is_missing z/d &&
+ test_path_is_file y/d &&
+ test_path_is_missing z/e/f &&
+ test_path_is_file y/e/f
+ )
+'
+
+test_expect_success '13a(info): messages for newly added files' '
+ test_setup_13a info &&
+ (
+ cd 13a_info &&
+
+ git reset --hard &&
+ git checkout A^0 &&
+
+ git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
+
+ test_grep Path.updated:.*z/e/f.added.in.B^0.*y/e/f out &&
+ test_grep Path.updated:.*z/d.added.in.B^0.*y/d out &&
+
+ git ls-files >paths &&
+ ! grep z/ paths &&
+ grep "y/[de]" paths &&
+
+ test_path_is_missing z/d &&
+ test_path_is_file y/d &&
+ test_path_is_missing z/e/f &&
+ test_path_is_file y/e/f
+ )
+'
+
+# Testcase 13b, Transitive rename with conflicted content merge and default
+# "conflict" setting
+# (Related to testcase 1c, 9b)
+# Commit O: z/{b,c}, x/d_1
+# Commit A: y/{b,c}, x/d_2
+# Commit B: z/{b,c,d_3}
+# Expected: y/{b,c,d_merged}, with two conflict messages for y/d,
+# one about content, and one about file location
+
+test_setup_13b () {
+ git init 13b_$1 &&
+ (
+ cd 13b_$1 &&
+
+ mkdir x &&
+ mkdir z &&
+ test_seq 1 10 >x/d &&
+ echo b >z/b &&
+ echo c >z/c &&
+ git add x z &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git mv z y &&
+ echo 11 >>x/d &&
+ git add x/d &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ echo eleven >>x/d &&
+ git mv x/d z/d &&
+ git add z/d &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '13b(conflict): messages for transitive rename with conflicted content' '
+ test_setup_13b conflict &&
+ (
+ cd 13b_conflict &&
+
+ git checkout A^0 &&
+
+ test_must_fail git merge -s recursive B^0 >out 2>err &&
+
+ test_grep CONFLICT.*content.*Merge.conflict.in.y/d out &&
+ test_grep CONFLICT..file.location.*x/d.renamed.to.z/d.*moved.to.y/d out &&
+
+ git ls-files >paths &&
+ ! grep z/ paths &&
+ grep "y/d" paths &&
+
+ test_path_is_missing z/d &&
+ test_path_is_file y/d
+ )
+'
+
+test_expect_success '13b(info): messages for transitive rename with conflicted content' '
+ test_setup_13b info &&
+ (
+ cd 13b_info &&
+
+ git reset --hard &&
+ git checkout A^0 &&
+
+ test_must_fail git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
+
+ test_grep CONFLICT.*content.*Merge.conflict.in.y/d out &&
+ test_grep Path.updated:.*x/d.renamed.to.z/d.in.B^0.*moving.it.to.y/d out &&
+
+ git ls-files >paths &&
+ ! grep z/ paths &&
+ grep "y/d" paths &&
+
+ test_path_is_missing z/d &&
+ test_path_is_file y/d
+ )
+'
+
+# Testcase 13c, Rename/rename(1to1) due to directory rename
+# Commit O: z/{b,c}, x/{d,e}
+# Commit A: y/{b,c,d}, x/e
+# Commit B: z/{b,c,d}, x/e
+# Expected: y/{b,c,d}, x/e, with info or conflict messages for d
+# A: renamed x/d -> z/d; B: renamed z/ -> y/ AND renamed x/d to y/d
+# One could argue A had partial knowledge of what was done with
+# d and B had full knowledge, but that's a slippery slope as
+# shown in testcase 13d.
+
+test_setup_13c () {
+ git init 13c_$1 &&
+ (
+ cd 13c_$1 &&
+
+ mkdir x &&
+ mkdir z &&
+ test_seq 1 10 >x/d &&
+ echo e >x/e &&
+ echo b >z/b &&
+ echo c >z/c &&
+ git add x z &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git mv z y &&
+ git mv x/d y/ &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ git mv x/d z/d &&
+ git add z/d &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '13c(conflict): messages for rename/rename(1to1) via transitive rename' '
+ test_setup_13c conflict &&
+ (
+ cd 13c_conflict &&
+
+ git checkout A^0 &&
+
+ test_must_fail git merge -s recursive B^0 >out 2>err &&
+
+ test_grep CONFLICT..file.location.*x/d.renamed.to.z/d.*moved.to.y/d out &&
+
+ git ls-files >paths &&
+ ! grep z/ paths &&
+ grep "y/d" paths &&
+
+ test_path_is_missing z/d &&
+ test_path_is_file y/d
+ )
+'
+
+test_expect_success '13c(info): messages for rename/rename(1to1) via transitive rename' '
+ test_setup_13c info &&
+ (
+ cd 13c_info &&
+
+ git reset --hard &&
+ git checkout A^0 &&
+
+ git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
+
+ test_grep Path.updated:.*x/d.renamed.to.z/d.in.B^0.*moving.it.to.y/d out &&
+
+ git ls-files >paths &&
+ ! grep z/ paths &&
+ grep "y/d" paths &&
+
+ test_path_is_missing z/d &&
+ test_path_is_file y/d
+ )
+'
+
+# Testcase 13d, Rename/rename(1to1) due to directory rename on both sides
+# Commit O: a/{z,y}, b/x, c/w
+# Commit A: a/z, b/{y,x}, d/w
+# Commit B: a/z, d/x, c/{y,w}
+# Expected: a/z, d/{y,x,w} with no file location conflict for x
+# Easy cases:
+# * z is always in a; so it stays in a.
+# * x starts in b, only modified on one side to move into d/
+# * w starts in c, only modified on one side to move into d/
+# Hard case:
+# * A renames a/y to b/y, and B renames b/->d/ => a/y -> d/y
+# * B renames a/y to c/y, and A renames c/->d/ => a/y -> d/y
+# No conflict in where a/y ends up, so put it in d/y.
+
+test_setup_13d () {
+ git init 13d_$1 &&
+ (
+ cd 13d_$1 &&
+
+ mkdir a &&
+ mkdir b &&
+ mkdir c &&
+ echo z >a/z &&
+ echo y >a/y &&
+ echo x >b/x &&
+ echo w >c/w &&
+ git add a b c &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git mv a/y b/ &&
+ git mv c/ d/ &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ git mv a/y c/ &&
+ git mv b/ d/ &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '13d(conflict): messages for rename/rename(1to1) via dual transitive rename' '
+ test_setup_13d conflict &&
+ (
+ cd 13d_conflict &&
+
+ git checkout A^0 &&
+
+ test_must_fail git merge -s recursive B^0 >out 2>err &&
+
+ test_grep CONFLICT..file.location.*a/y.renamed.to.b/y.*moved.to.d/y out &&
+ test_grep CONFLICT..file.location.*a/y.renamed.to.c/y.*moved.to.d/y out &&
+
+ git ls-files >paths &&
+ ! grep b/ paths &&
+ ! grep c/ paths &&
+ grep "d/y" paths &&
+
+ test_path_is_missing b/y &&
+ test_path_is_missing c/y &&
+ test_path_is_file d/y
+ )
+'
+
+test_expect_success '13d(info): messages for rename/rename(1to1) via dual transitive rename' '
+ test_setup_13d info &&
+ (
+ cd 13d_info &&
+
+ git reset --hard &&
+ git checkout A^0 &&
+
+ git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
+
+ test_grep Path.updated.*a/y.renamed.to.b/y.*moving.it.to.d/y out &&
+ test_grep Path.updated.*a/y.renamed.to.c/y.*moving.it.to.d/y out &&
+
+ git ls-files >paths &&
+ ! grep b/ paths &&
+ ! grep c/ paths &&
+ grep "d/y" paths &&
+
+ test_path_is_missing b/y &&
+ test_path_is_missing c/y &&
+ test_path_is_file d/y
+ )
+'
+
+# Testcase 13e, directory rename in virtual merge base
+#
+# This testcase has a slightly different setup than all the above cases, in
+# order to include a recursive case:
+#
+# A C
+# o - o
+# / \ / \
+# O o X ?
+# \ / \ /
+# o o
+# B D
+#
+# Commit O: a/{z,y}
+# Commit A: b/{z,y}
+# Commit B: a/{z,y,x}
+# Commit C: b/{z,y,x}
+# Commit D: b/{z,y}, a/x
+# Expected: b/{z,y,x} (sort of; see below for why this might not be expected)
+#
+# NOTES: 'X' represents a virtual merge base. With the default of
+# directory rename detection yielding conflicts, merging A and B
+# results in a conflict complaining about whether 'x' should be
+# under 'a/' or 'b/'. However, when creating the virtual merge
+# base 'X', since virtual merge bases need to be written out as a
+# tree, we cannot have a conflict, so some resolution has to be
+# picked.
+#
+# In choosing the right resolution, it's worth noting here that
+# commits C & D are merges of A & B that choose different
+# locations for 'x' (i.e. they resolve the conflict differently),
+# and so it would be nice when merging C & D if git could detect
+# this difference of opinion and report a conflict. But the only
+# way to do so that I can think of would be to have the virtual
+# merge base place 'x' in some directory other than either 'a/' or
+# 'b/', which seems a little weird -- especially since it'd result
+# in a rename/rename(1to2) conflict with a source path that never
+# existed in any version.
+#
+# So, for now, when directory rename detection is set to
+# 'conflict' just avoid doing directory rename detection at all in
+# the recursive case. This will not allow us to detect a conflict
+# in the outer merge for this special kind of setup, but it at
+# least avoids hitting a BUG().
+#
+test_setup_13e () {
+ git init 13e &&
+ (
+ cd 13e &&
+
+ mkdir a &&
+ echo z >a/z &&
+ echo y >a/y &&
+ git add a &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git mv a/ b/ &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ echo x >a/x &&
+ git add a &&
+ test_tick &&
+ git commit -m "B" &&
+
+ git branch C A &&
+ git branch D B &&
+
+ git checkout C &&
+ test_must_fail git -c merge.directoryRenames=conflict merge B &&
+ git add b/x &&
+ test_tick &&
+ git commit -m "C" &&
+
+
+ git checkout D &&
+ test_must_fail git -c merge.directoryRenames=conflict merge A &&
+ git add b/x &&
+ mkdir a &&
+ git mv b/x a/x &&
+ test_tick &&
+ git commit -m "D"
+ )
+}
+
+test_expect_success '13e: directory rename detection in recursive case' '
+ test_setup_13e &&
+ (
+ cd 13e &&
+
+ git checkout --quiet D^0 &&
+
+ git -c merge.directoryRenames=conflict merge -s recursive C^0 >out 2>err &&
+
+ test_grep ! CONFLICT out &&
+ test_grep ! BUG: err &&
+ test_grep ! core.dumped err &&
+ test_must_be_empty err &&
+
+ git ls-files >paths &&
+ ! grep a/x paths &&
+ grep b/x paths
+ )
+'
+
+test_done
diff --git a/t/t6424-merge-unrelated-index-changes.sh b/t/t6424-merge-unrelated-index-changes.sh
new file mode 100755
index 0000000..7677c5f
--- /dev/null
+++ b/t/t6424-merge-unrelated-index-changes.sh
@@ -0,0 +1,310 @@
+#!/bin/sh
+
+test_description="merges with unrelated index changes"
+
+. ./test-lib.sh
+
+# Testcase for some simple merges
+# A
+# o-------o B
+# \
+# \-----o C
+# \
+# \---o D
+# \
+# \-o E
+# \
+# o F
+# Commit A: some file a
+# Commit B: adds file b, modifies end of a
+# Commit C: adds file c
+# Commit D: adds file d, modifies beginning of a
+# Commit E: renames a->subdir/a, adds subdir/e
+# Commit F: empty commit
+
+test_expect_success 'setup trivial merges' '
+ test_seq 1 10 >a &&
+ git add a &&
+ test_tick && git commit -m A &&
+
+ git branch A &&
+ git branch B &&
+ git branch C &&
+ git branch D &&
+ git branch E &&
+ git branch F &&
+
+ git checkout B &&
+ echo b >b &&
+ echo 11 >>a &&
+ git add a b &&
+ test_tick && git commit -m B &&
+
+ git checkout C &&
+ echo c >c &&
+ git add c &&
+ test_tick && git commit -m C &&
+
+ git checkout D &&
+ test_seq 2 10 >a &&
+ echo d >d &&
+ git add a d &&
+ test_tick && git commit -m D &&
+
+ git checkout E &&
+ mkdir subdir &&
+ git mv a subdir/a &&
+ echo e >subdir/e &&
+ git add subdir &&
+ test_tick && git commit -m E &&
+
+ git checkout F &&
+ test_tick && git commit --allow-empty -m F
+'
+
+test_expect_success 'ff update' '
+ git reset --hard &&
+ git checkout A^0 &&
+
+ touch random_file && git add random_file &&
+
+ git merge E^0 &&
+
+ test_must_fail git rev-parse HEAD:random_file &&
+ test "$(git diff --name-only --cached E)" = "random_file" &&
+ test_path_is_file random_file &&
+ git rev-parse --verify :random_file
+'
+
+test_expect_success 'ff update, important file modified' '
+ git reset --hard &&
+ git checkout A^0 &&
+
+ mkdir subdir &&
+ touch subdir/e &&
+ git add subdir/e &&
+
+ test_must_fail git merge E^0 &&
+ test_path_is_file subdir/e &&
+ git rev-parse --verify :subdir/e &&
+ test_path_is_missing .git/MERGE_HEAD
+'
+
+test_expect_success 'resolve, trivial' '
+ git reset --hard &&
+ git checkout B^0 &&
+
+ touch random_file && git add random_file &&
+
+ test_must_fail git merge -s resolve C^0 &&
+ test_path_is_file random_file &&
+ git rev-parse --verify :random_file &&
+ test_path_is_missing .git/MERGE_HEAD
+'
+
+test_expect_success 'resolve, non-trivial' '
+ git reset --hard &&
+ git checkout B^0 &&
+
+ touch random_file && git add random_file &&
+
+ test_must_fail git merge -s resolve D^0 &&
+ test_path_is_file random_file &&
+ git rev-parse --verify :random_file &&
+ test_path_is_missing .git/MERGE_HEAD
+'
+
+test_expect_success 'resolve, trivial, related file removed' '
+ git reset --hard &&
+ git checkout B^0 &&
+
+ git rm a &&
+ test_path_is_missing a &&
+
+ test_must_fail git merge -s resolve C^0 &&
+
+ test_path_is_missing a &&
+ test_path_is_missing .git/MERGE_HEAD
+'
+
+test_expect_success 'resolve, non-trivial, related file removed' '
+ git reset --hard &&
+ git checkout B^0 &&
+
+ git rm a &&
+ test_path_is_missing a &&
+
+ # We also ask for recursive in order to turn off the "allow_trivial"
+ # setting in builtin/merge.c, and ensure that resolve really does
+ # correctly fail the merge (I guess this also tests that recursive
+ # correctly fails the merge, but the main thing we are attempting
+ # to test here is resolve and are just using the side effect of
+ # adding recursive to ensure that resolve is actually tested rather
+ # than the trivial merge codepath)
+ test_must_fail git merge -s resolve -s recursive D^0 &&
+
+ test_path_is_missing a &&
+ test_path_is_missing .git/MERGE_HEAD
+'
+
+test_expect_success 'recursive' '
+ git reset --hard &&
+ git checkout B^0 &&
+
+ touch random_file && git add random_file &&
+
+ test_must_fail git merge -s recursive C^0 &&
+ test_path_is_file random_file &&
+ git rev-parse --verify :random_file &&
+ test_path_is_missing .git/MERGE_HEAD
+'
+
+test_expect_success 'recursive, when merge branch matches merge base' '
+ git reset --hard &&
+ git checkout B^0 &&
+
+ touch random_file && git add random_file &&
+
+ test_must_fail git merge -s recursive F^0 &&
+ test_path_is_missing .git/MERGE_HEAD
+'
+
+test_expect_success 'merge-recursive, when index==head but head!=HEAD' '
+ git reset --hard &&
+ git checkout C^0 &&
+
+ # Make index match B
+ git diff C B -- | git apply --cached &&
+ test_when_finished "git clean -fd" && # Do not leave untracked around
+ # Merge B & F, with B as "head"
+ git merge-recursive A -- B F > out &&
+ test_grep "Already up to date" out
+'
+
+test_expect_success 'recursive, when file has staged changes not matching HEAD nor what a merge would give' '
+ git reset --hard &&
+ git checkout B^0 &&
+
+ mkdir subdir &&
+ test_seq 1 10 >subdir/a &&
+ git add subdir/a &&
+ git rev-parse --verify :subdir/a >expect &&
+
+ # We have staged changes; merge should error out
+ test_must_fail git merge -s recursive E^0 2>err &&
+ git rev-parse --verify :subdir/a >actual &&
+ test_cmp expect actual &&
+ test_grep "changes to the following files would be overwritten" err
+'
+
+test_expect_success 'recursive, when file has staged changes matching what a merge would give' '
+ git reset --hard &&
+ git checkout B^0 &&
+
+ mkdir subdir &&
+ test_seq 1 11 >subdir/a &&
+ git add subdir/a &&
+ git rev-parse --verify :subdir/a >expect &&
+
+ # We have staged changes; merge should error out
+ test_must_fail git merge -s recursive E^0 2>err &&
+ git rev-parse --verify :subdir/a >actual &&
+ test_cmp expect actual &&
+ test_grep "changes to the following files would be overwritten" err
+'
+
+test_expect_success 'octopus, unrelated file touched' '
+ git reset --hard &&
+ git checkout B^0 &&
+
+ touch random_file && git add random_file &&
+
+ test_must_fail git merge C^0 D^0 &&
+ test_path_is_missing .git/MERGE_HEAD &&
+ git rev-parse --verify :random_file &&
+ test_path_exists random_file
+'
+
+test_expect_success 'octopus, related file removed' '
+ git reset --hard &&
+ git checkout B^0 &&
+
+ git rm b &&
+
+ test_must_fail git merge C^0 D^0 &&
+ test_path_is_missing b &&
+ test_must_fail git rev-parse --verify :b &&
+ test_path_is_missing .git/MERGE_HEAD
+'
+
+test_expect_success 'octopus, related file modified' '
+ git reset --hard &&
+ git checkout B^0 &&
+
+ echo 12 >>a && git add a &&
+ git rev-parse --verify :a >expect &&
+
+ test_must_fail git merge C^0 D^0 &&
+ test_path_is_file a &&
+ git rev-parse --verify :a >actual &&
+ test_cmp expect actual &&
+ test_path_is_missing .git/MERGE_HEAD
+'
+
+test_expect_success 'ours' '
+ git reset --hard &&
+ git checkout B^0 &&
+
+ touch random_file && git add random_file &&
+
+ test_must_fail git merge -s ours C^0 &&
+ test_path_is_file random_file &&
+ git rev-parse --verify :random_file &&
+ test_path_is_missing .git/MERGE_HEAD
+'
+
+test_expect_success 'subtree' '
+ git reset --hard &&
+ git checkout B^0 &&
+
+ touch random_file && git add random_file &&
+
+ test_must_fail git merge -s subtree E^0 &&
+ test_path_is_file random_file &&
+ git rev-parse --verify :random_file &&
+ test_path_is_missing .git/MERGE_HEAD
+'
+
+test_expect_success 'avoid failure due to stat-dirty files' '
+ git reset --hard &&
+ git checkout B^0 &&
+
+ # Make "a" be stat-dirty
+ test-tool chmtime =+1 a &&
+
+ # stat-dirty file should not prevent stash creation in builtin/merge.c
+ git merge -s resolve -s recursive D^0
+'
+
+test_expect_success 'with multiple strategies, recursive or ort failure do not early abort' '
+ git reset --hard &&
+ git checkout B^0 &&
+
+ test_seq 0 10 >a &&
+ git add a &&
+ git rev-parse :a >expect &&
+
+ sane_unset GIT_TEST_MERGE_ALGORITHM &&
+ test_must_fail git merge -s recursive -s ort -s octopus C^0 >output 2>&1 &&
+
+ grep "Trying merge strategy recursive..." output &&
+ grep "Trying merge strategy ort..." output &&
+ grep "Trying merge strategy octopus..." output &&
+ grep "No merge strategy handled the merge." output &&
+
+ # Changes to "a" should remain staged
+ git rev-parse :a >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t6425-merge-rename-delete.sh b/t/t6425-merge-rename-delete.sh
new file mode 100755
index 0000000..b95b064
--- /dev/null
+++ b/t/t6425-merge-rename-delete.sh
@@ -0,0 +1,28 @@
+#!/bin/sh
+
+test_description='Merge-recursive rename/delete conflict message'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'rename/delete' '
+ echo foo >A &&
+ git add A &&
+ git commit -m "initial" &&
+
+ git checkout -b rename &&
+ git mv A B &&
+ git commit -m "rename" &&
+
+ git checkout main &&
+ git rm A &&
+ git commit -m "delete" &&
+
+ test_must_fail git merge --strategy=recursive rename >output &&
+ test_grep "CONFLICT (rename/delete): A.* renamed .*to B.* in rename" output &&
+ test_grep "CONFLICT (rename/delete): A.*deleted in HEAD." output
+'
+
+test_done
diff --git a/t/t6426-merge-skip-unneeded-updates.sh b/t/t6426-merge-skip-unneeded-updates.sh
new file mode 100755
index 0000000..b059475
--- /dev/null
+++ b/t/t6426-merge-skip-unneeded-updates.sh
@@ -0,0 +1,767 @@
+#!/bin/sh
+
+test_description="merge cases"
+
+# The setup for all of them, pictorially, is:
+#
+# A
+# o
+# / \
+# O o ?
+# \ /
+# o
+# B
+#
+# To help make it easier to follow the flow of tests, they have been
+# divided into sections and each test will start with a quick explanation
+# of what commits O, A, and B contain.
+#
+# Notation:
+# z/{b,c} means files z/b and z/c both exist
+# x/d_1 means file x/d exists with content d1. (Purpose of the
+# underscore notation is to differentiate different
+# files that might be renamed into each other's paths.)
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-merge.sh
+
+
+###########################################################################
+# SECTION 1: Cases involving no renames (one side has subset of changes of
+# the other side)
+###########################################################################
+
+# Testcase 1a, Changes on A, subset of changes on B
+# Commit O: b_1
+# Commit A: b_2
+# Commit B: b_3
+# Expected: b_2
+
+test_setup_1a () {
+ git init 1a_$1 &&
+ (
+ cd 1a_$1 &&
+
+ test_write_lines 1 2 3 4 5 6 7 8 9 10 >b &&
+ git add b &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ test_write_lines 1 2 3 4 5 5.5 6 7 8 9 10 10.5 >b &&
+ git add b &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ test_write_lines 1 2 3 4 5 5.5 6 7 8 9 10 >b &&
+ git add b &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '1a-L: Modify(A)/Modify(B), change on B subset of A' '
+ test_setup_1a L &&
+ (
+ cd 1a_L &&
+
+ git checkout A^0 &&
+
+ test-tool chmtime --get -3600 b >old-mtime &&
+
+ GIT_MERGE_VERBOSITY=3 git merge -s recursive B^0 >out 2>err &&
+
+ test_must_be_empty err &&
+
+ # Make sure b was NOT updated
+ test-tool chmtime --get b >new-mtime &&
+ test_cmp old-mtime new-mtime &&
+
+ git ls-files -s >index_files &&
+ test_line_count = 1 index_files &&
+
+ git rev-parse >actual HEAD:b &&
+ git rev-parse >expect A:b &&
+ test_cmp expect actual &&
+
+ git hash-object b >actual &&
+ git rev-parse A:b >expect &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success '1a-R: Modify(A)/Modify(B), change on B subset of A' '
+ test_setup_1a R &&
+ (
+ cd 1a_R &&
+
+ git checkout B^0 &&
+
+ test-tool chmtime --get -3600 b >old-mtime &&
+ GIT_MERGE_VERBOSITY=3 git merge -s recursive A^0 >out 2>err &&
+
+ # Make sure b WAS updated
+ test-tool chmtime --get b >new-mtime &&
+ test $(cat old-mtime) -lt $(cat new-mtime) &&
+
+ test_must_be_empty err &&
+
+ git ls-files -s >index_files &&
+ test_line_count = 1 index_files &&
+
+ git rev-parse >actual HEAD:b &&
+ git rev-parse >expect A:b &&
+ test_cmp expect actual &&
+
+ git hash-object b >actual &&
+ git rev-parse A:b >expect &&
+ test_cmp expect actual
+ )
+'
+
+
+###########################################################################
+# SECTION 2: Cases involving basic renames
+###########################################################################
+
+# Testcase 2a, Changes on A, rename on B
+# Commit O: b_1
+# Commit A: b_2
+# Commit B: c_1
+# Expected: c_2
+
+test_setup_2a () {
+ git init 2a_$1 &&
+ (
+ cd 2a_$1 &&
+
+ test_seq 1 10 >b &&
+ git add b &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ test_seq 1 11 >b &&
+ git add b &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ git mv b c &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '2a-L: Modify/rename, merge into modify side' '
+ test_setup_2a L &&
+ (
+ cd 2a_L &&
+
+ git checkout A^0 &&
+
+ test_path_is_missing c &&
+ GIT_MERGE_VERBOSITY=3 git merge -s recursive B^0 >out 2>err &&
+
+ test_path_is_file c &&
+
+ git ls-files -s >index_files &&
+ test_line_count = 1 index_files &&
+
+ git rev-parse >actual HEAD:c &&
+ git rev-parse >expect A:b &&
+ test_cmp expect actual &&
+
+ git hash-object c >actual &&
+ git rev-parse A:b >expect &&
+ test_cmp expect actual &&
+
+ test_must_fail git rev-parse HEAD:b &&
+ test_path_is_missing b
+ )
+'
+
+test_expect_success '2a-R: Modify/rename, merge into rename side' '
+ test_setup_2a R &&
+ (
+ cd 2a_R &&
+
+ git checkout B^0 &&
+
+ test-tool chmtime --get -3600 c >old-mtime &&
+ GIT_MERGE_VERBOSITY=3 git merge -s recursive A^0 >out 2>err &&
+
+ # Make sure c WAS updated
+ test-tool chmtime --get c >new-mtime &&
+ test $(cat old-mtime) -lt $(cat new-mtime) &&
+
+ test_must_be_empty err &&
+
+ git ls-files -s >index_files &&
+ test_line_count = 1 index_files &&
+
+ git rev-parse >actual HEAD:c &&
+ git rev-parse >expect A:b &&
+ test_cmp expect actual &&
+
+ git hash-object c >actual &&
+ git rev-parse A:b >expect &&
+ test_cmp expect actual &&
+
+ test_must_fail git rev-parse HEAD:b &&
+ test_path_is_missing b
+ )
+'
+
+# Testcase 2b, Changed and renamed on A, subset of changes on B
+# Commit O: b_1
+# Commit A: c_2
+# Commit B: b_3
+# Expected: c_2
+
+test_setup_2b () {
+ git init 2b_$1 &&
+ (
+ cd 2b_$1 &&
+
+ test_write_lines 1 2 3 4 5 6 7 8 9 10 >b &&
+ git add b &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ test_write_lines 1 2 3 4 5 5.5 6 7 8 9 10 10.5 >b &&
+ git add b &&
+ git mv b c &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ test_write_lines 1 2 3 4 5 5.5 6 7 8 9 10 >b &&
+ git add b &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '2b-L: Rename+Mod(A)/Mod(B), B mods subset of A' '
+ test_setup_2b L &&
+ (
+ cd 2b_L &&
+
+ git checkout A^0 &&
+
+ test-tool chmtime --get -3600 c >old-mtime &&
+ GIT_MERGE_VERBOSITY=3 git merge -s recursive B^0 >out 2>err &&
+
+ test_must_be_empty err &&
+
+ # Make sure c WAS updated
+ test-tool chmtime --get c >new-mtime &&
+ test_cmp old-mtime new-mtime &&
+
+ git ls-files -s >index_files &&
+ test_line_count = 1 index_files &&
+
+ git rev-parse >actual HEAD:c &&
+ git rev-parse >expect A:c &&
+ test_cmp expect actual &&
+
+ git hash-object c >actual &&
+ git rev-parse A:c >expect &&
+ test_cmp expect actual &&
+
+ test_must_fail git rev-parse HEAD:b &&
+ test_path_is_missing b
+ )
+'
+
+test_expect_success '2b-R: Rename+Mod(A)/Mod(B), B mods subset of A' '
+ test_setup_2b R &&
+ (
+ cd 2b_R &&
+
+ git checkout B^0 &&
+
+ test_path_is_missing c &&
+ GIT_MERGE_VERBOSITY=3 git merge -s recursive A^0 >out 2>err &&
+
+ # Make sure c now present (and thus was updated)
+ test_path_is_file c &&
+
+ test_must_be_empty err &&
+
+ git ls-files -s >index_files &&
+ test_line_count = 1 index_files &&
+
+ git rev-parse >actual HEAD:c &&
+ git rev-parse >expect A:c &&
+ test_cmp expect actual &&
+
+ git hash-object c >actual &&
+ git rev-parse A:c >expect &&
+ test_cmp expect actual &&
+
+ test_must_fail git rev-parse HEAD:b &&
+ test_path_is_missing b
+ )
+'
+
+# Testcase 2c, Changes on A, rename on B
+# Commit O: b_1
+# Commit A: b_2, c_3
+# Commit B: c_1
+# Expected: rename/add conflict c_2 vs c_3
+#
+# NOTE: Since A modified b_1->b_2, and B renamed b_1->c_1, the threeway
+# merge of those files should result in c_2. We then should have a
+# rename/add conflict between c_2 and c_3. However, if we note in
+# merge_content() that A had the right contents (b_2 has same
+# contents as c_2, just at a different name), and that A had the
+# right path present (c_3 existed) and thus decides that it can
+# skip the update, then we're in trouble. This test verifies we do
+# not make that particular mistake.
+
+test_setup_2c () {
+ git init 2c &&
+ (
+ cd 2c &&
+
+ test_seq 1 10 >b &&
+ git add b &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ test_seq 1 11 >b &&
+ echo whatever >c &&
+ git add b c &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ git mv b c &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '2c: Modify b & add c VS rename b->c' '
+ test_setup_2c &&
+ (
+ cd 2c &&
+
+ git checkout A^0 &&
+
+ test-tool chmtime --get -3600 c >old-mtime &&
+ GIT_MERGE_VERBOSITY=3 &&
+ export GIT_MERGE_VERBOSITY &&
+ test_must_fail git merge -s recursive B^0 >out 2>err &&
+
+ test_grep "CONFLICT (.*/add):" out &&
+ test_must_be_empty err &&
+
+ git ls-files -s >index_files &&
+ test_line_count = 2 index_files &&
+
+ # Ensure b was removed
+ test_path_is_missing b &&
+
+ # Make sure c WAS updated...
+ test-tool chmtime --get c >new-mtime &&
+ test $(cat old-mtime) -lt $(cat new-mtime) &&
+
+ # ...and has correct index entries and working tree contents
+ git rev-parse >actual :2:c :3:c &&
+ git rev-parse >expect A:c A:b &&
+ test_cmp expect actual &&
+
+ git cat-file -p A:b >>merge-me &&
+ git cat-file -p A:c >>merged &&
+ >empty &&
+ test_must_fail git merge-file \
+ -L "HEAD" \
+ -L "" \
+ -L "B^0" \
+ merged empty merge-me &&
+ test_cmp merged c
+ )
+'
+
+
+###########################################################################
+# SECTION 3: Cases involving directory renames
+#
+# NOTE:
+# Directory renames only apply when one side renames a directory, and the
+# other side adds or renames a path into that directory. Applying the
+# directory rename to that new path creates a new pathname that didn't
+# exist on either side of history. Thus, it is impossible for the
+# merge contents to already be at the right path, so all of these checks
+# exist just to make sure that updates are not skipped.
+###########################################################################
+
+# Testcase 3a, Change + rename into dir foo on A, dir rename foo->bar on B
+# Commit O: bq_1, foo/whatever
+# Commit A: foo/{bq_2, whatever}
+# Commit B: bq_1, bar/whatever
+# Expected: bar/{bq_2, whatever}
+
+test_setup_3a () {
+ git init 3a_$1 &&
+ (
+ cd 3a_$1 &&
+
+ mkdir foo &&
+ test_seq 1 10 >bq &&
+ test_write_lines a b c d e f g h i j k >foo/whatever &&
+ git add bq foo/whatever &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ test_seq 1 11 >bq &&
+ git add bq &&
+ git mv bq foo/ &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ git mv foo/ bar/ &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '3a-L: bq_1->foo/bq_2 on A, foo/->bar/ on B' '
+ test_setup_3a L &&
+ (
+ cd 3a_L &&
+
+ git checkout A^0 &&
+
+ test_path_is_missing bar/bq &&
+ GIT_MERGE_VERBOSITY=3 git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
+
+ test_must_be_empty err &&
+
+ test_path_is_file bar/bq &&
+
+ git ls-files -s >index_files &&
+ test_line_count = 2 index_files &&
+
+ git rev-parse >actual HEAD:bar/bq HEAD:bar/whatever &&
+ git rev-parse >expect A:foo/bq A:foo/whatever &&
+ test_cmp expect actual &&
+
+ git hash-object bar/bq bar/whatever >actual &&
+ git rev-parse A:foo/bq A:foo/whatever >expect &&
+ test_cmp expect actual &&
+
+ test_must_fail git rev-parse HEAD:bq HEAD:foo/bq &&
+ test_path_is_missing bq &&
+ test_path_is_missing foo/bq &&
+ test_path_is_missing foo/whatever
+ )
+'
+
+test_expect_success '3a-R: bq_1->foo/bq_2 on A, foo/->bar/ on B' '
+ test_setup_3a R &&
+ (
+ cd 3a_R &&
+
+ git checkout B^0 &&
+
+ test_path_is_missing bar/bq &&
+ GIT_MERGE_VERBOSITY=3 git -c merge.directoryRenames=true merge -s recursive A^0 >out 2>err &&
+
+ test_must_be_empty err &&
+
+ test_path_is_file bar/bq &&
+
+ git ls-files -s >index_files &&
+ test_line_count = 2 index_files &&
+
+ git rev-parse >actual HEAD:bar/bq HEAD:bar/whatever &&
+ git rev-parse >expect A:foo/bq A:foo/whatever &&
+ test_cmp expect actual &&
+
+ git hash-object bar/bq bar/whatever >actual &&
+ git rev-parse A:foo/bq A:foo/whatever >expect &&
+ test_cmp expect actual &&
+
+ test_must_fail git rev-parse HEAD:bq HEAD:foo/bq &&
+ test_path_is_missing bq &&
+ test_path_is_missing foo/bq &&
+ test_path_is_missing foo/whatever
+ )
+'
+
+# Testcase 3b, rename into dir foo on A, dir rename foo->bar + change on B
+# Commit O: bq_1, foo/whatever
+# Commit A: foo/{bq_1, whatever}
+# Commit B: bq_2, bar/whatever
+# Expected: bar/{bq_2, whatever}
+
+test_setup_3b () {
+ git init 3b_$1 &&
+ (
+ cd 3b_$1 &&
+
+ mkdir foo &&
+ test_seq 1 10 >bq &&
+ test_write_lines a b c d e f g h i j k >foo/whatever &&
+ git add bq foo/whatever &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ git mv bq foo/ &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ test_seq 1 11 >bq &&
+ git add bq &&
+ git mv foo/ bar/ &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '3b-L: bq_1->foo/bq_2 on A, foo/->bar/ on B' '
+ test_setup_3b L &&
+ (
+ cd 3b_L &&
+
+ git checkout A^0 &&
+
+ test_path_is_missing bar/bq &&
+ GIT_MERGE_VERBOSITY=3 git -c merge.directoryRenames=true merge -s recursive B^0 >out 2>err &&
+
+ test_must_be_empty err &&
+
+ test_path_is_file bar/bq &&
+
+ git ls-files -s >index_files &&
+ test_line_count = 2 index_files &&
+
+ git rev-parse >actual HEAD:bar/bq HEAD:bar/whatever &&
+ git rev-parse >expect B:bq A:foo/whatever &&
+ test_cmp expect actual &&
+
+ git hash-object bar/bq bar/whatever >actual &&
+ git rev-parse B:bq A:foo/whatever >expect &&
+ test_cmp expect actual &&
+
+ test_must_fail git rev-parse HEAD:bq HEAD:foo/bq &&
+ test_path_is_missing bq &&
+ test_path_is_missing foo/bq &&
+ test_path_is_missing foo/whatever
+ )
+'
+
+test_expect_success '3b-R: bq_1->foo/bq_2 on A, foo/->bar/ on B' '
+ test_setup_3b R &&
+ (
+ cd 3b_R &&
+
+ git checkout B^0 &&
+
+ test_path_is_missing bar/bq &&
+ GIT_MERGE_VERBOSITY=3 git -c merge.directoryRenames=true merge -s recursive A^0 >out 2>err &&
+
+ test_must_be_empty err &&
+
+ test_path_is_file bar/bq &&
+
+ git ls-files -s >index_files &&
+ test_line_count = 2 index_files &&
+
+ git rev-parse >actual HEAD:bar/bq HEAD:bar/whatever &&
+ git rev-parse >expect B:bq A:foo/whatever &&
+ test_cmp expect actual &&
+
+ git hash-object bar/bq bar/whatever >actual &&
+ git rev-parse B:bq A:foo/whatever >expect &&
+ test_cmp expect actual &&
+
+ test_must_fail git rev-parse HEAD:bq HEAD:foo/bq &&
+ test_path_is_missing bq &&
+ test_path_is_missing foo/bq &&
+ test_path_is_missing foo/whatever
+ )
+'
+
+###########################################################################
+# SECTION 4: Cases involving dirty changes
+###########################################################################
+
+# Testcase 4a, Changed on A, subset of changes on B, locally modified
+# Commit O: b_1
+# Commit A: b_2
+# Commit B: b_3
+# Working copy: b_4
+# Expected: b_2 for merge, b_4 in working copy
+
+test_setup_4a () {
+ git init 4a &&
+ (
+ cd 4a &&
+
+ test_write_lines 1 2 3 4 5 6 7 8 9 10 >b &&
+ git add b &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ test_write_lines 1 2 3 4 5 5.5 6 7 8 9 10 10.5 >b &&
+ git add b &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ test_write_lines 1 2 3 4 5 5.5 6 7 8 9 10 >b &&
+ git add b &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+# NOTE: For as long as we continue using unpack_trees() without index_only
+# set to true, it will error out on a case like this claiming that the locally
+# modified file would be overwritten by the merge. Getting this testcase
+# correct requires doing the merge in-memory first, then realizing that no
+# updates to the file are necessary, and thus that we can just leave the path
+# alone.
+test_expect_merge_algorithm failure success '4a: Change on A, change on B subset of A, dirty mods present' '
+ test_setup_4a &&
+ (
+ cd 4a &&
+
+ git checkout A^0 &&
+ echo "File rewritten" >b &&
+
+ test-tool chmtime --get -3600 b >old-mtime &&
+
+ GIT_MERGE_VERBOSITY=3 git merge -s recursive B^0 >out 2>err &&
+
+ test_must_be_empty err &&
+
+ # Make sure b was NOT updated
+ test-tool chmtime --get b >new-mtime &&
+ test_cmp old-mtime new-mtime &&
+
+ git ls-files -s >index_files &&
+ test_line_count = 1 index_files &&
+
+ git rev-parse >actual :0:b &&
+ git rev-parse >expect A:b &&
+ test_cmp expect actual &&
+
+ git hash-object b >actual &&
+ echo "File rewritten" | git hash-object --stdin >expect &&
+ test_cmp expect actual
+ )
+'
+
+# Testcase 4b, Changed+renamed on A, subset of changes on B, locally modified
+# Commit O: b_1
+# Commit A: c_2
+# Commit B: b_3
+# Working copy: c_4
+# Expected: c_2
+
+test_setup_4b () {
+ git init 4b &&
+ (
+ cd 4b &&
+
+ test_write_lines 1 2 3 4 5 6 7 8 9 10 >b &&
+ git add b &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ test_write_lines 1 2 3 4 5 5.5 6 7 8 9 10 10.5 >b &&
+ git add b &&
+ git mv b c &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ test_write_lines 1 2 3 4 5 5.5 6 7 8 9 10 >b &&
+ git add b &&
+ test_tick &&
+ git commit -m "B"
+ )
+}
+
+test_expect_success '4b: Rename+Mod(A)/Mod(B), change on B subset of A, dirty mods present' '
+ test_setup_4b &&
+ (
+ cd 4b &&
+
+ git checkout A^0 &&
+ echo "File rewritten" >c &&
+
+ test-tool chmtime --get -3600 c >old-mtime &&
+
+ GIT_MERGE_VERBOSITY=3 git merge -s recursive B^0 >out 2>err &&
+
+ test_must_be_empty err &&
+
+ # Make sure c was NOT updated
+ test-tool chmtime --get c >new-mtime &&
+ test_cmp old-mtime new-mtime &&
+
+ git ls-files -s >index_files &&
+ test_line_count = 1 index_files &&
+
+ git rev-parse >actual :0:c &&
+ git rev-parse >expect A:c &&
+ test_cmp expect actual &&
+
+ git hash-object c >actual &&
+ echo "File rewritten" | git hash-object --stdin >expect &&
+ test_cmp expect actual &&
+
+ test_must_fail git rev-parse HEAD:b &&
+ test_path_is_missing b
+ )
+'
+
+test_done
diff --git a/t/t6427-diff3-conflict-markers.sh b/t/t6427-diff3-conflict-markers.sh
new file mode 100755
index 0000000..dd5fe6a
--- /dev/null
+++ b/t/t6427-diff3-conflict-markers.sh
@@ -0,0 +1,304 @@
+#!/bin/sh
+
+test_description='recursive merge diff3 style conflict markers'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+# Setup:
+# L1
+# \
+# ?
+# /
+# R1
+#
+# Where:
+# L1 and R1 both have a file named 'content' but have no common history
+#
+
+test_expect_success 'setup no merge base' '
+ git init no_merge_base &&
+ (
+ cd no_merge_base &&
+
+ git checkout -b L &&
+ test_commit A content A &&
+
+ git checkout --orphan R &&
+ test_commit B content B
+ )
+'
+
+test_expect_success 'check no merge base' '
+ (
+ cd no_merge_base &&
+
+ git checkout L^0 &&
+
+ test_must_fail git -c merge.conflictstyle=diff3 merge --allow-unrelated-histories -s recursive R^0 &&
+
+ grep "|||||| empty tree" content
+ )
+'
+
+# Setup:
+# L1
+# / \
+# main ?
+# \ /
+# R1
+#
+# Where:
+# L1 and R1 have modified the same file ('content') in conflicting ways
+#
+
+test_expect_success 'setup unique merge base' '
+ git init unique_merge_base &&
+ (
+ cd unique_merge_base &&
+
+ test_commit base content "1
+2
+3
+4
+5
+" &&
+
+ git branch L &&
+ git branch R &&
+
+ git checkout L &&
+ test_commit L content "1
+2
+3
+4
+5
+7" &&
+
+ git checkout R &&
+ git rm content &&
+ test_commit R renamed "1
+2
+3
+4
+5
+six"
+ )
+'
+
+test_expect_success 'check unique merge base' '
+ (
+ cd unique_merge_base &&
+
+ git checkout L^0 &&
+ MAIN=$(git rev-parse --short main) &&
+
+ test_must_fail git -c merge.conflictstyle=diff3 merge -s recursive R^0 &&
+
+ grep "|||||| $MAIN:content" renamed
+ )
+'
+
+# Setup:
+# L1---L2--L3
+# / \ / \
+# main X1 ?
+# \ / \ /
+# R1---R2--R3
+#
+# Where:
+# commits L1 and R1 have modified the same file in non-conflicting ways
+# X1 is an auto-generated merge-base used when merging L1 and R1
+# commits L2 and R2 are merges of R1 and L1 into L1 and R1, respectively
+# commits L3 and R3 both modify 'content' in conflicting ways
+#
+
+test_expect_success 'setup multiple merge bases' '
+ git init multiple_merge_bases &&
+ (
+ cd multiple_merge_bases &&
+
+ test_commit initial content "1
+2
+3
+4
+5" &&
+
+ git branch L &&
+ git branch R &&
+
+ # Create L1
+ git checkout L &&
+ test_commit L1 content "0
+1
+2
+3
+4
+5" &&
+
+ # Create R1
+ git checkout R &&
+ test_commit R1 content "1
+2
+3
+4
+5
+6" &&
+
+ # Create L2
+ git checkout L &&
+ git merge R1 &&
+
+ # Create R2
+ git checkout R &&
+ git merge L1 &&
+
+ # Create L3
+ git checkout L &&
+ test_commit L3 content "0
+1
+2
+3
+4
+5
+A" &&
+
+ # Create R3
+ git checkout R &&
+ git rm content &&
+ test_commit R3 renamed "0
+2
+3
+4
+5
+six"
+ )
+'
+
+test_expect_success 'check multiple merge bases' '
+ (
+ cd multiple_merge_bases &&
+
+ git checkout L^0 &&
+
+ test_must_fail git -c merge.conflictstyle=diff3 merge -s recursive R^0 &&
+
+ grep "|||||| merged common ancestors:content" renamed
+ )
+'
+
+test_expect_success 'rebase --merge describes parent of commit being picked' '
+ git init rebase &&
+ (
+ cd rebase &&
+ test_commit base file &&
+ test_commit main file &&
+ git checkout -b side HEAD^ &&
+ test_commit side file &&
+ test_must_fail git -c merge.conflictstyle=diff3 rebase --merge main &&
+ grep "||||||| parent of" file
+ )
+'
+
+test_expect_success 'rebase --apply describes fake ancestor base' '
+ (
+ cd rebase &&
+ git rebase --abort &&
+ test_must_fail git -c merge.conflictstyle=diff3 rebase --apply main &&
+ grep "||||||| constructed merge base" file
+ )
+'
+
+test_setup_zdiff3 () {
+ git init zdiff3 &&
+ (
+ cd zdiff3 &&
+
+ test_write_lines 1 2 3 4 5 6 7 8 9 >basic &&
+ test_write_lines 1 2 3 AA 4 5 BB 6 7 8 >middle-common &&
+ test_write_lines 1 2 3 4 5 6 7 8 9 >interesting &&
+ test_write_lines 1 2 3 4 5 6 7 8 9 >evil &&
+
+ git add basic middle-common interesting evil &&
+ git commit -m base &&
+
+ git branch left &&
+ git branch right &&
+
+ git checkout left &&
+ test_write_lines 1 2 3 4 A B C D E 7 8 9 >basic &&
+ test_write_lines 1 2 3 CC 4 5 DD 6 7 8 >middle-common &&
+ test_write_lines 1 2 3 4 A B C D E F G H I J 7 8 9 >interesting &&
+ test_write_lines 1 2 3 4 X A B C 7 8 9 >evil &&
+ git add -u &&
+ git commit -m letters &&
+
+ git checkout right &&
+ test_write_lines 1 2 3 4 A X C Y E 7 8 9 >basic &&
+ test_write_lines 1 2 3 EE 4 5 FF 6 7 8 >middle-common &&
+ test_write_lines 1 2 3 4 A B C 5 6 G H I J 7 8 9 >interesting &&
+ test_write_lines 1 2 3 4 Y A B C B C 7 8 9 >evil &&
+ git add -u &&
+ git commit -m permuted
+ )
+}
+
+test_expect_success 'check zdiff3 markers' '
+ test_setup_zdiff3 &&
+ (
+ cd zdiff3 &&
+
+ git checkout left^0 &&
+
+ base=$(git rev-parse --short HEAD^1) &&
+ test_must_fail git -c merge.conflictstyle=zdiff3 merge -s recursive right^0 &&
+
+ test_write_lines 1 2 3 4 A \
+ "<<<<<<< HEAD" B C D \
+ "||||||| $base" 5 6 \
+ ======= X C Y \
+ ">>>>>>> right^0" \
+ E 7 8 9 \
+ >expect &&
+ test_cmp expect basic &&
+
+ test_write_lines 1 2 3 \
+ "<<<<<<< HEAD" CC \
+ "||||||| $base" AA \
+ ======= EE \
+ ">>>>>>> right^0" \
+ 4 5 \
+ "<<<<<<< HEAD" DD \
+ "||||||| $base" BB \
+ ======= FF \
+ ">>>>>>> right^0" \
+ 6 7 8 \
+ >expect &&
+ test_cmp expect middle-common &&
+
+ test_write_lines 1 2 3 4 A B C \
+ "<<<<<<< HEAD" D E F \
+ "||||||| $base" 5 6 \
+ ======= 5 6 \
+ ">>>>>>> right^0" \
+ G H I J 7 8 9 \
+ >expect &&
+ test_cmp expect interesting &&
+
+ # Not passing this one yet; the common "B C" lines is still
+ # being left in the conflict blocks on the left and right
+ # sides.
+ test_write_lines 1 2 3 4 \
+ "<<<<<<< HEAD" X A \
+ "||||||| $base" 5 6 \
+ ======= Y A B C \
+ ">>>>>>> right^0" \
+ B C 7 8 9 \
+ >expect &&
+ test_cmp expect evil
+ )
+'
+
+test_done
diff --git a/t/t6428-merge-conflicts-sparse.sh b/t/t6428-merge-conflicts-sparse.sh
new file mode 100755
index 0000000..9919c3f
--- /dev/null
+++ b/t/t6428-merge-conflicts-sparse.sh
@@ -0,0 +1,145 @@
+#!/bin/sh
+
+test_description="merge cases"
+
+# The setup for all of them, pictorially, is:
+#
+# A
+# o
+# / \
+# O o ?
+# \ /
+# o
+# B
+#
+# To help make it easier to follow the flow of tests, they have been
+# divided into sections and each test will start with a quick explanation
+# of what commits O, A, and B contain.
+#
+# Notation:
+# z/{b,c} means files z/b and z/c both exist
+# x/d_1 means file x/d exists with content d1. (Purpose of the
+# underscore notation is to differentiate different
+# files that might be renamed into each other's paths.)
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-merge.sh
+
+
+# Testcase basic, conflicting changes in 'numerals'
+
+test_setup_numerals () {
+ git init numerals_$1 &&
+ (
+ cd numerals_$1 &&
+
+ >README &&
+ test_write_lines I II III >numerals &&
+ git add README numerals &&
+ test_tick &&
+ git commit -m "O" &&
+
+ git branch O &&
+ git branch A &&
+ git branch B &&
+
+ git checkout A &&
+ test_write_lines I II III IIII >numerals &&
+ git add numerals &&
+ test_tick &&
+ git commit -m "A" &&
+
+ git checkout B &&
+ test_write_lines I II III IV >numerals &&
+ git add numerals &&
+ test_tick &&
+ git commit -m "B" &&
+
+ cat <<-EOF >expected-index &&
+ H README
+ M numerals
+ M numerals
+ M numerals
+ EOF
+
+ cat <<-EOF >expected-merge
+ I
+ II
+ III
+ <<<<<<< HEAD
+ IIII
+ =======
+ IV
+ >>>>>>> B^0
+ EOF
+
+ )
+}
+
+test_expect_success 'conflicting entries written to worktree even if sparse' '
+ test_setup_numerals plain &&
+ (
+ cd numerals_plain &&
+
+ git checkout A^0 &&
+
+ test_path_is_file README &&
+ test_path_is_file numerals &&
+
+ git sparse-checkout init &&
+ git sparse-checkout set --no-cone README &&
+
+ test_path_is_file README &&
+ test_path_is_missing numerals &&
+
+ test_must_fail git merge -s recursive B^0 &&
+
+ git ls-files -t >index_files &&
+ test_cmp expected-index index_files &&
+
+ test_path_is_file README &&
+ test_path_is_file numerals &&
+
+ test_cmp expected-merge numerals &&
+
+ # 4 other files:
+ # * expected-merge
+ # * expected-index
+ # * index_files
+ # * others
+ git ls-files -o >others &&
+ test_line_count = 4 others
+ )
+'
+
+test_expect_success 'present-despite-SKIP_WORKTREE handled reasonably' '
+ test_setup_numerals in_the_way &&
+ (
+ cd numerals_in_the_way &&
+
+ git checkout A^0 &&
+
+ test_path_is_file README &&
+ test_path_is_file numerals &&
+
+ git sparse-checkout init &&
+ git sparse-checkout set --no-cone README &&
+
+ test_path_is_file README &&
+ test_path_is_missing numerals &&
+
+ echo foobar >numerals &&
+
+ test_must_fail git merge -s recursive B^0 &&
+
+ test_path_is_missing .git/MERGE_HEAD &&
+
+ test_path_is_file numerals &&
+
+ # numerals should still have "foobar" in it
+ echo foobar >expect &&
+ test_cmp expect numerals
+ )
+'
+
+test_done
diff --git a/t/t6429-merge-sequence-rename-caching.sh b/t/t6429-merge-sequence-rename-caching.sh
new file mode 100755
index 0000000..d02fa16
--- /dev/null
+++ b/t/t6429-merge-sequence-rename-caching.sh
@@ -0,0 +1,767 @@
+#!/bin/sh
+
+test_description="remember regular & dir renames in sequence of merges"
+
+. ./test-lib.sh
+
+#
+# NOTE 1: this testfile tends to not only rename files, but modify on both
+# sides; without modifying on both sides, optimizations can kick in
+# which make rename detection irrelevant or trivial. We want to make
+# sure that we are triggering rename caching rather than rename
+# bypassing.
+#
+# NOTE 2: this testfile uses 'test-tool fast-rebase' instead of either
+# cherry-pick or rebase. sequencer.c is only superficially
+# integrated with merge-ort; it calls merge_switch_to_result()
+# after EACH merge, which updates the index and working copy AND
+# throws away the cached results (because merge_switch_to_result()
+# is only supposed to be called at the end of the sequence).
+# Integrating them more deeply is a big task, so for now the tests
+# use 'test-tool fast-rebase'.
+#
+
+
+#
+# In the following simple testcase:
+# Base: numbers_1, values_1
+# Upstream: numbers_2, values_2
+# Topic_1: sequence_3
+# Topic_2: scruples_3
+# or, in english, rename numbers -> sequence in the first commit, and rename
+# values -> scruples in the second commit.
+#
+# This shouldn't be a challenge, it's just verifying that cached renames isn't
+# preventing us from finding new renames.
+#
+test_expect_success 'caching renames does not preclude finding new ones' '
+ git init caching-renames-and-new-renames &&
+ (
+ cd caching-renames-and-new-renames &&
+
+ test_seq 2 10 >numbers &&
+ test_seq 2 10 >values &&
+ git add numbers values &&
+ git commit -m orig &&
+
+ git branch upstream &&
+ git branch topic &&
+
+ git switch upstream &&
+ test_seq 1 10 >numbers &&
+ test_seq 1 10 >values &&
+ git add numbers values &&
+ git commit -m "Tweaked both files" &&
+
+ git switch topic &&
+
+ test_seq 2 12 >numbers &&
+ git add numbers &&
+ git mv numbers sequence &&
+ git commit -m A &&
+
+ test_seq 2 12 >values &&
+ git add values &&
+ git mv values scruples &&
+ git commit -m B &&
+
+ #
+ # Actual testing
+ #
+
+ git switch upstream &&
+
+ test-tool fast-rebase --onto HEAD upstream~1 topic &&
+ #git cherry-pick upstream~1..topic
+
+ git ls-files >tracked-files &&
+ test_line_count = 2 tracked-files &&
+ test_seq 1 12 >expect &&
+ test_cmp expect sequence &&
+ test_cmp expect scruples
+ )
+'
+
+#
+# In the following testcase:
+# Base: numbers_1
+# Upstream: rename numbers_1 -> sequence_2
+# Topic_1: numbers_3
+# Topic_2: numbers_1
+# or, in english, the first commit on the topic branch modifies numbers by
+# shrinking it (dramatically) and the second commit on topic reverts its
+# parent.
+#
+# Can git apply both patches?
+#
+# Traditional cherry-pick/rebase will fail to apply the second commit, the
+# one that reverted its parent, because despite detecting the rename from
+# 'numbers' to 'sequence' for the first commit, it fails to detect that
+# rename when picking the second commit. That's "reasonable" given the
+# dramatic change in size of the file, but remembering the rename and
+# reusing it is reasonable too.
+#
+# We do test here that we expect rename detection to only be run once total
+# (the topic side of history doesn't need renames, and with caching we
+# should be able to only run rename detection on the upstream side one
+# time.)
+test_expect_success 'cherry-pick both a commit and its immediate revert' '
+ git init pick-commit-and-its-immediate-revert &&
+ (
+ cd pick-commit-and-its-immediate-revert &&
+
+ test_seq 11 30 >numbers &&
+ git add numbers &&
+ git commit -m orig &&
+
+ git branch upstream &&
+ git branch topic &&
+
+ git switch upstream &&
+ test_seq 1 30 >numbers &&
+ git add numbers &&
+ git mv numbers sequence &&
+ git commit -m "Renamed (and modified) numbers -> sequence" &&
+
+ git switch topic &&
+
+ test_seq 11 13 >numbers &&
+ git add numbers &&
+ git commit -m A &&
+
+ git revert HEAD &&
+
+ #
+ # Actual testing
+ #
+
+ git switch upstream &&
+
+ GIT_TRACE2_PERF="$(pwd)/trace.output" &&
+ export GIT_TRACE2_PERF &&
+
+ test-tool fast-rebase --onto HEAD upstream~1 topic &&
+ #git cherry-pick upstream~1..topic &&
+
+ grep region_enter.*diffcore_rename trace.output >calls &&
+ test_line_count = 1 calls
+ )
+'
+
+#
+# In the following testcase:
+# Base: sequence_1
+# Upstream: rename sequence_1 -> values_2
+# Topic_1: rename sequence_1 -> values_3
+# Topic_2: add unrelated sequence_4
+# or, in english, both sides rename sequence -> values, and then the second
+# commit on the topic branch adds an unrelated file called sequence.
+#
+# This testcase presents no problems for git traditionally, but having both
+# sides do the same rename in effect "uses it up" and if it remains cached,
+# could cause a spurious rename/add conflict.
+#
+test_expect_success 'rename same file identically, then reintroduce it' '
+ git init rename-rename-1to1-then-add-old-filename &&
+ (
+ cd rename-rename-1to1-then-add-old-filename &&
+
+ test_seq 3 8 >sequence &&
+ git add sequence &&
+ git commit -m orig &&
+
+ git branch upstream &&
+ git branch topic &&
+
+ git switch upstream &&
+ test_seq 1 8 >sequence &&
+ git add sequence &&
+ git mv sequence values &&
+ git commit -m "Renamed (and modified) sequence -> values" &&
+
+ git switch topic &&
+
+ test_seq 3 10 >sequence &&
+ git add sequence &&
+ git mv sequence values &&
+ git commit -m A &&
+
+ test_write_lines A B C D E F G H I J >sequence &&
+ git add sequence &&
+ git commit -m B &&
+
+ #
+ # Actual testing
+ #
+
+ git switch upstream &&
+
+ GIT_TRACE2_PERF="$(pwd)/trace.output" &&
+ export GIT_TRACE2_PERF &&
+
+ test-tool fast-rebase --onto HEAD upstream~1 topic &&
+ #git cherry-pick upstream~1..topic &&
+
+ git ls-files >tracked &&
+ test_line_count = 2 tracked &&
+ test_path_is_file values &&
+ test_path_is_file sequence &&
+
+ grep region_enter.*diffcore_rename trace.output >calls &&
+ test_line_count = 2 calls
+ )
+'
+
+#
+# In the following testcase:
+# Base: olddir/{valuesZ_1, valuesY_1, valuesX_1}
+# Upstream: rename olddir/valuesZ_1 -> dirA/valuesZ_2
+# rename olddir/valuesY_1 -> dirA/valuesY_2
+# rename olddir/valuesX_1 -> dirB/valuesX_2
+# Topic_1: rename olddir/valuesZ_1 -> dirA/valuesZ_3
+# rename olddir/valuesY_1 -> dirA/valuesY_3
+# Topic_2: add olddir/newfile
+# Expected Pick1: dirA/{valuesZ, valuesY}, dirB/valuesX
+# Expected Pick2: dirA/{valuesZ, valuesY}, dirB/{valuesX, newfile}
+#
+# This testcase presents no problems for git traditionally, but having both
+# sides do the same renames in effect "use it up" but if the renames remain
+# cached, the directory rename could put newfile in the wrong directory.
+#
+test_expect_success 'rename same file identically, then add file to old dir' '
+ git init rename-rename-1to1-then-add-file-to-old-dir &&
+ (
+ cd rename-rename-1to1-then-add-file-to-old-dir &&
+
+ mkdir olddir/ &&
+ test_seq 3 8 >olddir/valuesZ &&
+ test_seq 3 8 >olddir/valuesY &&
+ test_seq 3 8 >olddir/valuesX &&
+ git add olddir &&
+ git commit -m orig &&
+
+ git branch upstream &&
+ git branch topic &&
+
+ git switch upstream &&
+ test_seq 1 8 >olddir/valuesZ &&
+ test_seq 1 8 >olddir/valuesY &&
+ test_seq 1 8 >olddir/valuesX &&
+ git add olddir &&
+ mkdir dirA &&
+ git mv olddir/valuesZ olddir/valuesY dirA &&
+ git mv olddir/ dirB/ &&
+ git commit -m "Renamed (and modified) values*" &&
+
+ git switch topic &&
+
+ test_seq 3 10 >olddir/valuesZ &&
+ test_seq 3 10 >olddir/valuesY &&
+ git add olddir &&
+ mkdir dirA &&
+ git mv olddir/valuesZ olddir/valuesY dirA &&
+ git commit -m A &&
+
+ >olddir/newfile &&
+ git add olddir/newfile &&
+ git commit -m B &&
+
+ #
+ # Actual testing
+ #
+
+ git switch upstream &&
+ git config merge.directoryRenames true &&
+
+ GIT_TRACE2_PERF="$(pwd)/trace.output" &&
+ export GIT_TRACE2_PERF &&
+
+ test-tool fast-rebase --onto HEAD upstream~1 topic &&
+ #git cherry-pick upstream~1..topic &&
+
+ git ls-files >tracked &&
+ test_line_count = 4 tracked &&
+ test_path_is_file dirA/valuesZ &&
+ test_path_is_file dirA/valuesY &&
+ test_path_is_file dirB/valuesX &&
+ test_path_is_file dirB/newfile &&
+
+ grep region_enter.*diffcore_rename trace.output >calls &&
+ test_line_count = 3 calls
+ )
+'
+
+#
+# In the following testcase, upstream renames a directory, and the topic branch
+# first adds a file to the directory, then later renames the directory
+# differently:
+# Base: olddir/a
+# olddir/b
+# Upstream: rename olddir/ -> newdir/
+# Topic_1: add olddir/newfile
+# Topic_2: rename olddir/ -> otherdir/
+#
+# Here we are just concerned that cached renames might prevent us from seeing
+# the rename conflict, and we want to ensure that we do get a conflict.
+#
+# While at it, though, we do test that we only try to detect renames 2
+# times and not three. (The first merge needs to detect renames on the
+# upstream side. Traditionally, the second merge would need to detect
+# renames on both sides of history, but our caching of upstream renames
+# should avoid the need to re-detect upstream renames.)
+#
+test_expect_success 'cached dir rename does not prevent noticing later conflict' '
+ git init dir-rename-cache-not-occluding-later-conflict &&
+ (
+ cd dir-rename-cache-not-occluding-later-conflict &&
+
+ mkdir olddir &&
+ test_seq 3 10 >olddir/a &&
+ test_seq 3 10 >olddir/b &&
+ git add olddir &&
+ git commit -m orig &&
+
+ git branch upstream &&
+ git branch topic &&
+
+ git switch upstream &&
+ test_seq 3 10 >olddir/a &&
+ test_seq 3 10 >olddir/b &&
+ git add olddir &&
+ git mv olddir newdir &&
+ git commit -m "Dir renamed" &&
+
+ git switch topic &&
+
+ >olddir/newfile &&
+ git add olddir/newfile &&
+ git commit -m A &&
+
+ test_seq 1 8 >olddir/a &&
+ test_seq 1 8 >olddir/b &&
+ git add olddir &&
+ git mv olddir otherdir &&
+ git commit -m B &&
+
+ #
+ # Actual testing
+ #
+
+ git switch upstream &&
+ git config merge.directoryRenames true &&
+
+ GIT_TRACE2_PERF="$(pwd)/trace.output" &&
+ export GIT_TRACE2_PERF &&
+
+ test_must_fail test-tool fast-rebase --onto HEAD upstream~1 topic >output &&
+ #git cherry-pick upstream..topic &&
+
+ grep CONFLICT..rename/rename output &&
+
+ grep region_enter.*diffcore_rename trace.output >calls &&
+ test_line_count = 2 calls
+ )
+'
+
+# Helper for the next two tests
+test_setup_upstream_rename () {
+ git init $1 &&
+ (
+ cd $1 &&
+
+ test_seq 3 8 >somefile &&
+ test_seq 3 8 >relevant-rename &&
+ git add somefile relevant-rename &&
+ mkdir olddir &&
+ test_write_lines a b c d e f g >olddir/a &&
+ test_write_lines z y x w v u t >olddir/b &&
+ git add olddir &&
+ git commit -m orig &&
+
+ git branch upstream &&
+ git branch topic &&
+
+ git switch upstream &&
+ test_seq 1 8 >somefile &&
+ test_seq 1 8 >relevant-rename &&
+ git add somefile relevant-rename &&
+ git mv relevant-rename renamed &&
+ echo h >>olddir/a &&
+ echo s >>olddir/b &&
+ git add olddir &&
+ git mv olddir newdir &&
+ git commit -m "Dir renamed"
+ )
+}
+
+#
+# In the following testcase, upstream renames a file in the toplevel directory
+# as well as its only directory:
+# Base: relevant-rename_1
+# somefile
+# olddir/a
+# olddir/b
+# Upstream: rename relevant-rename_1 -> renamed_2
+# rename olddir/ -> newdir/
+# Topic_1: relevant-rename_3
+# Topic_2: olddir/newfile_1
+# Topic_3: olddir/newfile_2
+#
+# In this testcase, since the first commit being picked only modifies a
+# file in the toplevel directory, the directory rename is irrelevant for
+# that first merge. However, we need to notice the directory rename for
+# the merge that picks the second commit, and we don't want the third
+# commit to mess up its location either. We want to make sure that
+# olddir/newfile doesn't exist in the result and that newdir/newfile does.
+#
+# We also test that we only do rename detection twice. We never need
+# rename detection on the topic side of history, but we do need it twice on
+# the upstream side of history. For the first topic commit, we only need
+# the
+# relevant-rename -> renamed
+# rename, because olddir is unmodified by Topic_1. For Topic_2, however,
+# the new file being added to olddir means files that were previously
+# irrelevant for rename detection are now relevant, forcing us to repeat
+# rename detection for the paths we don't already have cached. Topic_3 also
+# tweaks olddir/newfile, but the renames in olddir/ will have been cached
+# from the second rename detection run.
+#
+test_expect_success 'dir rename unneeded, then add new file to old dir' '
+ test_setup_upstream_rename dir-rename-unneeded-until-new-file &&
+ (
+ cd dir-rename-unneeded-until-new-file &&
+
+ git switch topic &&
+
+ test_seq 3 10 >relevant-rename &&
+ git add relevant-rename &&
+ git commit -m A &&
+
+ echo foo >olddir/newfile &&
+ git add olddir/newfile &&
+ git commit -m B &&
+
+ echo bar >>olddir/newfile &&
+ git add olddir/newfile &&
+ git commit -m C &&
+
+ #
+ # Actual testing
+ #
+
+ git switch upstream &&
+ git config merge.directoryRenames true &&
+
+ GIT_TRACE2_PERF="$(pwd)/trace.output" &&
+ export GIT_TRACE2_PERF &&
+
+ test-tool fast-rebase --onto HEAD upstream~1 topic &&
+ #git cherry-pick upstream..topic &&
+
+ grep region_enter.*diffcore_rename trace.output >calls &&
+ test_line_count = 2 calls &&
+
+ git ls-files >tracked &&
+ test_line_count = 5 tracked &&
+ test_path_is_missing olddir/newfile &&
+ test_path_is_file newdir/newfile
+ )
+'
+
+#
+# The following testcase is *very* similar to the last one, but instead of
+# adding a new olddir/newfile, it renames somefile -> olddir/newfile:
+# Base: relevant-rename_1
+# somefile_1
+# olddir/a
+# olddir/b
+# Upstream: rename relevant-rename_1 -> renamed_2
+# rename olddir/ -> newdir/
+# Topic_1: relevant-rename_3
+# Topic_2: rename somefile -> olddir/newfile_2
+# Topic_3: modify olddir/newfile_3
+#
+# In this testcase, since the first commit being picked only modifies a
+# file in the toplevel directory, the directory rename is irrelevant for
+# that first merge. However, we need to notice the directory rename for
+# the merge that picks the second commit, and we don't want the third
+# commit to mess up its location either. We want to make sure that
+# neither somefile or olddir/newfile exists in the result and that
+# newdir/newfile does.
+#
+# This testcase needs one more call to rename detection than the last
+# testcase, because of the somefile -> olddir/newfile rename in Topic_2.
+test_expect_success 'dir rename unneeded, then rename existing file into old dir' '
+ test_setup_upstream_rename dir-rename-unneeded-until-file-moved-inside &&
+ (
+ cd dir-rename-unneeded-until-file-moved-inside &&
+
+ git switch topic &&
+
+ test_seq 3 10 >relevant-rename &&
+ git add relevant-rename &&
+ git commit -m A &&
+
+ test_seq 1 10 >somefile &&
+ git add somefile &&
+ git mv somefile olddir/newfile &&
+ git commit -m B &&
+
+ test_seq 1 12 >olddir/newfile &&
+ git add olddir/newfile &&
+ git commit -m C &&
+
+ #
+ # Actual testing
+ #
+
+ git switch upstream &&
+ git config merge.directoryRenames true &&
+
+ GIT_TRACE2_PERF="$(pwd)/trace.output" &&
+ export GIT_TRACE2_PERF &&
+
+ test-tool fast-rebase --onto HEAD upstream~1 topic &&
+ #git cherry-pick upstream..topic &&
+
+ grep region_enter.*diffcore_rename trace.output >calls &&
+ test_line_count = 3 calls &&
+
+ test_path_is_missing somefile &&
+ test_path_is_missing olddir/newfile &&
+ test_path_is_file newdir/newfile &&
+ git ls-files >tracked &&
+ test_line_count = 4 tracked
+ )
+'
+
+# Helper for the next two tests
+test_setup_topic_rename () {
+ git init $1 &&
+ (
+ cd $1 &&
+
+ test_seq 3 8 >somefile &&
+ mkdir olddir &&
+ test_seq 3 8 >olddir/a &&
+ echo b >olddir/b &&
+ git add olddir somefile &&
+ git commit -m orig &&
+
+ git branch upstream &&
+ git branch topic &&
+
+ git switch topic &&
+ test_seq 1 8 >somefile &&
+ test_seq 1 8 >olddir/a &&
+ git add somefile olddir/a &&
+ git mv olddir newdir &&
+ git commit -m "Dir renamed" &&
+
+ test_seq 1 10 >somefile &&
+ git add somefile &&
+ mkdir olddir &&
+ >olddir/unrelated-file &&
+ git add olddir &&
+ git commit -m "Unrelated file in recreated old dir"
+ )
+}
+
+#
+# In the following testcase, the first commit on the topic branch renames
+# a directory, while the second recreates the old directory and places a
+# file into it:
+# Base: somefile
+# olddir/a
+# olddir/b
+# Upstream: olddir/newfile
+# Topic_1: somefile_2
+# rename olddir/ -> newdir/
+# Topic_2: olddir/unrelated-file
+#
+# Note that the first pick should merge:
+# Base: somefile
+# olddir/{a,b}
+# Upstream: olddir/newfile
+# Topic_1: rename olddir/ -> newdir/
+# For which the expected result (assuming merge.directoryRenames=true) is
+# clearly:
+# Result: somefile
+# newdir/{a, b, newfile}
+#
+# While the second pick does the following three-way merge:
+# Base (Topic_1): somefile
+# newdir/{a,b}
+# Upstream (Result from 1): same files as base, but adds newdir/newfile
+# Topic_2: same files as base, but adds olddir/unrelated-file
+#
+# The second merge is pretty trivial; upstream adds newdir/newfile, and
+# topic_2 adds olddir/unrelated-file. We're just testing that we don't
+# accidentally cache directory renames somehow and rename
+# olddir/unrelated-file to newdir/unrelated-file.
+#
+# This testcase should only need one call to diffcore_rename_extended().
+test_expect_success 'caching renames only on upstream side, part 1' '
+ test_setup_topic_rename cache-renames-only-upstream-add-file &&
+ (
+ cd cache-renames-only-upstream-add-file &&
+
+ git switch upstream &&
+
+ >olddir/newfile &&
+ git add olddir/newfile &&
+ git commit -m "Add newfile" &&
+
+ #
+ # Actual testing
+ #
+
+ git switch upstream &&
+
+ git config merge.directoryRenames true &&
+
+ GIT_TRACE2_PERF="$(pwd)/trace.output" &&
+ export GIT_TRACE2_PERF &&
+
+ test-tool fast-rebase --onto HEAD upstream~1 topic &&
+ #git cherry-pick upstream..topic &&
+
+ grep region_enter.*diffcore_rename trace.output >calls &&
+ test_line_count = 1 calls &&
+
+ git ls-files >tracked &&
+ test_line_count = 5 tracked &&
+ test_path_is_missing newdir/unrelated-file &&
+ test_path_is_file olddir/unrelated-file &&
+ test_path_is_file newdir/newfile &&
+ test_path_is_file newdir/b &&
+ test_path_is_file newdir/a &&
+ test_path_is_file somefile
+ )
+'
+
+#
+# The following testcase is *very* similar to the last one, but instead of
+# adding a new olddir/newfile, it renames somefile -> olddir/newfile:
+# Base: somefile
+# olddir/a
+# olddir/b
+# Upstream: somefile_1 -> olddir/newfile
+# Topic_1: rename olddir/ -> newdir/
+# somefile_2
+# Topic_2: olddir/unrelated-file
+# somefile_3
+#
+# Much like the previous test, this case is actually trivial and we are just
+# making sure there isn't some spurious directory rename caching going on
+# for the wrong side of history.
+#
+#
+# This testcase should only need two calls to diffcore_rename_extended(),
+# both for the first merge, one for each side of history.
+#
+test_expect_success 'caching renames only on upstream side, part 2' '
+ test_setup_topic_rename cache-renames-only-upstream-rename-file &&
+ (
+ cd cache-renames-only-upstream-rename-file &&
+
+ git switch upstream &&
+
+ git mv somefile olddir/newfile &&
+ git commit -m "Add newfile" &&
+
+ #
+ # Actual testing
+ #
+
+ git switch upstream &&
+
+ git config merge.directoryRenames true &&
+
+ GIT_TRACE2_PERF="$(pwd)/trace.output" &&
+ export GIT_TRACE2_PERF &&
+
+ test-tool fast-rebase --onto HEAD upstream~1 topic &&
+ #git cherry-pick upstream..topic &&
+
+ grep region_enter.*diffcore_rename trace.output >calls &&
+ test_line_count = 2 calls &&
+
+ git ls-files >tracked &&
+ test_line_count = 4 tracked &&
+ test_path_is_missing newdir/unrelated-file &&
+ test_path_is_file olddir/unrelated-file &&
+ test_path_is_file newdir/newfile &&
+ test_path_is_file newdir/b &&
+ test_path_is_file newdir/a
+ )
+'
+
+#
+# The following testcase just creates two simple renames (slightly modified
+# on both sides but without conflicting changes), and a directory full of
+# files that are otherwise uninteresting. The setup is as follows:
+#
+# base: unrelated/<BUNCH OF FILES>
+# numbers
+# values
+# upstream: modify: numbers
+# modify: values
+# topic: add: unrelated/foo
+# modify: numbers
+# modify: values
+# rename: numbers -> sequence
+# rename: values -> progression
+#
+# This is a trivial rename case, but we're curious what happens with a very
+# low renameLimit interacting with the restart optimization trying to notice
+# that unrelated/ looks like a trivial merge candidate.
+#
+test_expect_success 'avoid assuming we detected renames' '
+ git init redo-weirdness &&
+ (
+ cd redo-weirdness &&
+
+ mkdir unrelated &&
+ for i in $(test_seq 1 10)
+ do
+ >unrelated/$i || exit 1
+ done &&
+ test_seq 2 10 >numbers &&
+ test_seq 12 20 >values &&
+ git add numbers values unrelated/ &&
+ git commit -m orig &&
+
+ git branch upstream &&
+ git branch topic &&
+
+ git switch upstream &&
+ test_seq 1 10 >numbers &&
+ test_seq 11 20 >values &&
+ git add numbers &&
+ git commit -m "Some tweaks" &&
+
+ git switch topic &&
+
+ >unrelated/foo &&
+ test_seq 2 12 >numbers &&
+ test_seq 12 22 >values &&
+ git add numbers values unrelated/ &&
+ git mv numbers sequence &&
+ git mv values progression &&
+ git commit -m A &&
+
+ #
+ # Actual testing
+ #
+
+ git switch --detach topic^0 &&
+
+ test_must_fail git -c merge.renameLimit=1 rebase upstream &&
+
+ git ls-files -u >actual &&
+ test_line_count = 2 actual
+ )
+'
+
+test_done
diff --git a/t/t6430-merge-recursive.sh b/t/t6430-merge-recursive.sh
new file mode 100755
index 0000000..ca15e6d
--- /dev/null
+++ b/t/t6430-merge-recursive.sh
@@ -0,0 +1,776 @@
+#!/bin/sh
+
+test_description='merge-recursive backend test'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-merge.sh
+
+test_expect_success 'setup 1' '
+
+ echo hello >a &&
+ o0=$(git hash-object a) &&
+ cp a b &&
+ cp a c &&
+ mkdir d &&
+ cp a d/e &&
+
+ test_tick &&
+ git add a b c d/e &&
+ git commit -m initial &&
+ c0=$(git rev-parse --verify HEAD) &&
+ git branch side &&
+ git branch df-1 &&
+ git branch df-2 &&
+ git branch df-3 &&
+ git branch remove &&
+ git branch submod &&
+ git branch copy &&
+ git branch rename &&
+ git branch rename-ln &&
+
+ echo hello >>a &&
+ cp a d/e &&
+ o1=$(git hash-object a) &&
+
+ git add a d/e &&
+
+ test_tick &&
+ git commit -m "main modifies a and d/e" &&
+ c1=$(git rev-parse --verify HEAD) &&
+ ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
+ (
+ echo "100644 blob $o1 a" &&
+ echo "100644 blob $o0 b" &&
+ echo "100644 blob $o0 c" &&
+ echo "100644 blob $o1 d/e" &&
+ echo "100644 $o1 0 a" &&
+ echo "100644 $o0 0 b" &&
+ echo "100644 $o0 0 c" &&
+ echo "100644 $o1 0 d/e"
+ ) >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'setup 2' '
+
+ rm -rf [abcd] &&
+ git checkout side &&
+ ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
+ (
+ echo "100644 blob $o0 a" &&
+ echo "100644 blob $o0 b" &&
+ echo "100644 blob $o0 c" &&
+ echo "100644 blob $o0 d/e" &&
+ echo "100644 $o0 0 a" &&
+ echo "100644 $o0 0 b" &&
+ echo "100644 $o0 0 c" &&
+ echo "100644 $o0 0 d/e"
+ ) >expected &&
+ test_cmp expected actual &&
+
+ echo goodbye >>a &&
+ o2=$(git hash-object a) &&
+
+ git add a &&
+
+ test_tick &&
+ git commit -m "side modifies a" &&
+ c2=$(git rev-parse --verify HEAD) &&
+ ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
+ (
+ echo "100644 blob $o2 a" &&
+ echo "100644 blob $o0 b" &&
+ echo "100644 blob $o0 c" &&
+ echo "100644 blob $o0 d/e" &&
+ echo "100644 $o2 0 a" &&
+ echo "100644 $o0 0 b" &&
+ echo "100644 $o0 0 c" &&
+ echo "100644 $o0 0 d/e"
+ ) >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'setup 3' '
+
+ rm -rf [abcd] &&
+ git checkout df-1 &&
+ ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
+ (
+ echo "100644 blob $o0 a" &&
+ echo "100644 blob $o0 b" &&
+ echo "100644 blob $o0 c" &&
+ echo "100644 blob $o0 d/e" &&
+ echo "100644 $o0 0 a" &&
+ echo "100644 $o0 0 b" &&
+ echo "100644 $o0 0 c" &&
+ echo "100644 $o0 0 d/e"
+ ) >expected &&
+ test_cmp expected actual &&
+
+ rm -f b && mkdir b && echo df-1 >b/c && git add b/c &&
+ o3=$(git hash-object b/c) &&
+
+ test_tick &&
+ git commit -m "df-1 makes b/c" &&
+ c3=$(git rev-parse --verify HEAD) &&
+ ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
+ (
+ echo "100644 blob $o0 a" &&
+ echo "100644 blob $o3 b/c" &&
+ echo "100644 blob $o0 c" &&
+ echo "100644 blob $o0 d/e" &&
+ echo "100644 $o0 0 a" &&
+ echo "100644 $o3 0 b/c" &&
+ echo "100644 $o0 0 c" &&
+ echo "100644 $o0 0 d/e"
+ ) >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'setup 4' '
+
+ rm -rf [abcd] &&
+ git checkout df-2 &&
+ ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
+ (
+ echo "100644 blob $o0 a" &&
+ echo "100644 blob $o0 b" &&
+ echo "100644 blob $o0 c" &&
+ echo "100644 blob $o0 d/e" &&
+ echo "100644 $o0 0 a" &&
+ echo "100644 $o0 0 b" &&
+ echo "100644 $o0 0 c" &&
+ echo "100644 $o0 0 d/e"
+ ) >expected &&
+ test_cmp expected actual &&
+
+ rm -f a && mkdir a && echo df-2 >a/c && git add a/c &&
+ o4=$(git hash-object a/c) &&
+
+ test_tick &&
+ git commit -m "df-2 makes a/c" &&
+ c4=$(git rev-parse --verify HEAD) &&
+ ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
+ (
+ echo "100644 blob $o4 a/c" &&
+ echo "100644 blob $o0 b" &&
+ echo "100644 blob $o0 c" &&
+ echo "100644 blob $o0 d/e" &&
+ echo "100644 $o4 0 a/c" &&
+ echo "100644 $o0 0 b" &&
+ echo "100644 $o0 0 c" &&
+ echo "100644 $o0 0 d/e"
+ ) >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'setup 5' '
+
+ rm -rf [abcd] &&
+ git checkout remove &&
+ ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
+ (
+ echo "100644 blob $o0 a" &&
+ echo "100644 blob $o0 b" &&
+ echo "100644 blob $o0 c" &&
+ echo "100644 blob $o0 d/e" &&
+ echo "100644 $o0 0 a" &&
+ echo "100644 $o0 0 b" &&
+ echo "100644 $o0 0 c" &&
+ echo "100644 $o0 0 d/e"
+ ) >expected &&
+ test_cmp expected actual &&
+
+ rm -f b &&
+ echo remove-conflict >a &&
+
+ git add a &&
+ git rm b &&
+ o5=$(git hash-object a) &&
+
+ test_tick &&
+ git commit -m "remove removes b and modifies a" &&
+ c5=$(git rev-parse --verify HEAD) &&
+ ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
+ (
+ echo "100644 blob $o5 a" &&
+ echo "100644 blob $o0 c" &&
+ echo "100644 blob $o0 d/e" &&
+ echo "100644 $o5 0 a" &&
+ echo "100644 $o0 0 c" &&
+ echo "100644 $o0 0 d/e"
+ ) >expected &&
+ test_cmp expected actual
+
+'
+
+test_expect_success 'setup 6' '
+
+ rm -rf [abcd] &&
+ git checkout df-3 &&
+ ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
+ (
+ echo "100644 blob $o0 a" &&
+ echo "100644 blob $o0 b" &&
+ echo "100644 blob $o0 c" &&
+ echo "100644 blob $o0 d/e" &&
+ echo "100644 $o0 0 a" &&
+ echo "100644 $o0 0 b" &&
+ echo "100644 $o0 0 c" &&
+ echo "100644 $o0 0 d/e"
+ ) >expected &&
+ test_cmp expected actual &&
+
+ rm -fr d && echo df-3 >d && git add d &&
+ o6=$(git hash-object d) &&
+
+ test_tick &&
+ git commit -m "df-3 makes d" &&
+ c6=$(git rev-parse --verify HEAD) &&
+ ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
+ (
+ echo "100644 blob $o0 a" &&
+ echo "100644 blob $o0 b" &&
+ echo "100644 blob $o0 c" &&
+ echo "100644 blob $o6 d" &&
+ echo "100644 $o0 0 a" &&
+ echo "100644 $o0 0 b" &&
+ echo "100644 $o0 0 c" &&
+ echo "100644 $o6 0 d"
+ ) >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'setup 7' '
+
+ git checkout submod &&
+ git rm d/e &&
+ test_tick &&
+ git commit -m "remove d/e" &&
+ git update-index --add --cacheinfo 160000 $c1 d &&
+ test_tick &&
+ git commit -m "make d/ a submodule"
+'
+
+test_expect_success 'setup 8' '
+ git checkout rename &&
+ git mv a e &&
+ git add e &&
+ test_tick &&
+ git commit -m "rename a->e" &&
+ c7=$(git rev-parse --verify HEAD) &&
+ git checkout rename-ln &&
+ git mv a e &&
+ test_ln_s_add e a &&
+ test_tick &&
+ git commit -m "rename a->e, symlink a->e" &&
+ oln=$(printf e | git hash-object --stdin)
+'
+
+test_expect_success 'setup 9' '
+ git checkout copy &&
+ cp a e &&
+ git add e &&
+ test_tick &&
+ git commit -m "copy a->e"
+'
+
+test_expect_success 'merge-recursive simple' '
+
+ rm -fr [abcd] &&
+ git checkout -f "$c2" &&
+
+ test_expect_code 1 git merge-recursive "$c0" -- "$c2" "$c1"
+'
+
+test_expect_success 'merge-recursive result' '
+
+ git ls-files -s >actual &&
+ (
+ echo "100644 $o0 1 a" &&
+ echo "100644 $o2 2 a" &&
+ echo "100644 $o1 3 a" &&
+ echo "100644 $o0 0 b" &&
+ echo "100644 $o0 0 c" &&
+ echo "100644 $o1 0 d/e"
+ ) >expected &&
+ test_cmp expected actual
+
+'
+
+test_expect_success 'fail if the index has unresolved entries' '
+
+ rm -fr [abcd] &&
+ git checkout -f "$c1" &&
+
+ test_must_fail git merge "$c5" &&
+ test_must_fail git merge "$c5" 2> out &&
+ test_grep "not possible because you have unmerged files" out &&
+ git add -u &&
+ test_must_fail git merge "$c5" 2> out &&
+ test_grep "You have not concluded your merge" out &&
+ rm -f .git/MERGE_HEAD &&
+ test_must_fail git merge "$c5" 2> out &&
+ test_grep "Your local changes to the following files would be overwritten by merge:" out
+'
+
+test_expect_success 'merge-recursive remove conflict' '
+
+ rm -fr [abcd] &&
+ git checkout -f "$c1" &&
+
+ test_expect_code 1 git merge-recursive "$c0" -- "$c1" "$c5"
+'
+
+test_expect_success 'merge-recursive remove conflict' '
+
+ git ls-files -s >actual &&
+ (
+ echo "100644 $o0 1 a" &&
+ echo "100644 $o1 2 a" &&
+ echo "100644 $o5 3 a" &&
+ echo "100644 $o0 0 c" &&
+ echo "100644 $o1 0 d/e"
+ ) >expected &&
+ test_cmp expected actual
+
+'
+
+test_expect_success 'merge-recursive d/f simple' '
+ rm -fr [abcd] &&
+ git reset --hard &&
+ git checkout -f "$c1" &&
+
+ git merge-recursive "$c0" -- "$c1" "$c3"
+'
+
+test_expect_success 'merge-recursive result' '
+
+ git ls-files -s >actual &&
+ (
+ echo "100644 $o1 0 a" &&
+ echo "100644 $o3 0 b/c" &&
+ echo "100644 $o0 0 c" &&
+ echo "100644 $o1 0 d/e"
+ ) >expected &&
+ test_cmp expected actual
+
+'
+
+test_expect_success 'merge-recursive d/f conflict' '
+
+ rm -fr [abcd] &&
+ git reset --hard &&
+ git checkout -f "$c1" &&
+
+ test_expect_code 1 git merge-recursive "$c0" -- "$c1" "$c4"
+'
+
+test_expect_success 'merge-recursive d/f conflict result' '
+
+ git ls-files -s >actual &&
+ (
+ echo "100644 $o0 1 a" &&
+ echo "100644 $o1 2 a" &&
+ echo "100644 $o4 0 a/c" &&
+ echo "100644 $o0 0 b" &&
+ echo "100644 $o0 0 c" &&
+ echo "100644 $o1 0 d/e"
+ ) >expected &&
+ test_cmp expected actual
+
+'
+
+test_expect_success 'merge-recursive d/f conflict the other way' '
+
+ rm -fr [abcd] &&
+ git reset --hard &&
+ git checkout -f "$c4" &&
+
+ test_expect_code 1 git merge-recursive "$c0" -- "$c4" "$c1"
+'
+
+test_expect_success 'merge-recursive d/f conflict result the other way' '
+
+ git ls-files -s >actual &&
+ (
+ echo "100644 $o0 1 a" &&
+ echo "100644 $o1 3 a" &&
+ echo "100644 $o4 0 a/c" &&
+ echo "100644 $o0 0 b" &&
+ echo "100644 $o0 0 c" &&
+ echo "100644 $o1 0 d/e"
+ ) >expected &&
+ test_cmp expected actual
+
+'
+
+test_expect_success 'merge-recursive d/f conflict' '
+
+ rm -fr [abcd] &&
+ git reset --hard &&
+ git checkout -f "$c1" &&
+
+ test_expect_code 1 git merge-recursive "$c0" -- "$c1" "$c6"
+'
+
+test_expect_success 'merge-recursive d/f conflict result' '
+
+ git ls-files -s >actual &&
+ (
+ echo "100644 $o1 0 a" &&
+ echo "100644 $o0 0 b" &&
+ echo "100644 $o0 0 c" &&
+ echo "100644 $o6 3 d" &&
+ echo "100644 $o0 1 d/e" &&
+ echo "100644 $o1 2 d/e"
+ ) >expected &&
+ test_cmp expected actual
+
+'
+
+test_expect_success 'merge-recursive d/f conflict' '
+
+ rm -fr [abcd] &&
+ git reset --hard &&
+ git checkout -f "$c6" &&
+
+ test_expect_code 1 git merge-recursive "$c0" -- "$c6" "$c1"
+'
+
+test_expect_success 'merge-recursive d/f conflict result' '
+
+ git ls-files -s >actual &&
+ (
+ echo "100644 $o1 0 a" &&
+ echo "100644 $o0 0 b" &&
+ echo "100644 $o0 0 c" &&
+ echo "100644 $o6 2 d" &&
+ echo "100644 $o0 1 d/e" &&
+ echo "100644 $o1 3 d/e"
+ ) >expected &&
+ test_cmp expected actual
+
+'
+
+test_expect_success SYMLINKS 'dir in working tree with symlink ancestor does not produce d/f conflict' '
+ git init sym &&
+ (
+ cd sym &&
+ ln -s . foo &&
+ mkdir bar &&
+ >bar/file &&
+ git add foo bar/file &&
+ git commit -m "foo symlink" &&
+
+ git checkout -b branch1 &&
+ git commit --allow-empty -m "empty commit" &&
+
+ git checkout main &&
+ git rm foo &&
+ mkdir foo &&
+ >foo/bar &&
+ git add foo/bar &&
+ git commit -m "replace foo symlink with real foo dir and foo/bar file" &&
+
+ git checkout branch1 &&
+
+ git cherry-pick main &&
+ test_path_is_dir foo &&
+ test_path_is_file foo/bar
+ )
+'
+
+test_expect_success 'reset and 3-way merge' '
+
+ git reset --hard "$c2" &&
+ git read-tree -m "$c0" "$c2" "$c1"
+
+'
+
+test_expect_success 'reset and bind merge' '
+
+ git reset --hard main &&
+ git read-tree --prefix=M/ main &&
+ git ls-files -s >actual &&
+ (
+ echo "100644 $o1 0 M/a" &&
+ echo "100644 $o0 0 M/b" &&
+ echo "100644 $o0 0 M/c" &&
+ echo "100644 $o1 0 M/d/e" &&
+ echo "100644 $o1 0 a" &&
+ echo "100644 $o0 0 b" &&
+ echo "100644 $o0 0 c" &&
+ echo "100644 $o1 0 d/e"
+ ) >expected &&
+ test_cmp expected actual &&
+
+ git read-tree --prefix=a1/ main &&
+ git ls-files -s >actual &&
+ (
+ echo "100644 $o1 0 M/a" &&
+ echo "100644 $o0 0 M/b" &&
+ echo "100644 $o0 0 M/c" &&
+ echo "100644 $o1 0 M/d/e" &&
+ echo "100644 $o1 0 a" &&
+ echo "100644 $o1 0 a1/a" &&
+ echo "100644 $o0 0 a1/b" &&
+ echo "100644 $o0 0 a1/c" &&
+ echo "100644 $o1 0 a1/d/e" &&
+ echo "100644 $o0 0 b" &&
+ echo "100644 $o0 0 c" &&
+ echo "100644 $o1 0 d/e"
+ ) >expected &&
+ test_cmp expected actual &&
+
+ git read-tree --prefix=z/ main &&
+ git ls-files -s >actual &&
+ (
+ echo "100644 $o1 0 M/a" &&
+ echo "100644 $o0 0 M/b" &&
+ echo "100644 $o0 0 M/c" &&
+ echo "100644 $o1 0 M/d/e" &&
+ echo "100644 $o1 0 a" &&
+ echo "100644 $o1 0 a1/a" &&
+ echo "100644 $o0 0 a1/b" &&
+ echo "100644 $o0 0 a1/c" &&
+ echo "100644 $o1 0 a1/d/e" &&
+ echo "100644 $o0 0 b" &&
+ echo "100644 $o0 0 c" &&
+ echo "100644 $o1 0 d/e" &&
+ echo "100644 $o1 0 z/a" &&
+ echo "100644 $o0 0 z/b" &&
+ echo "100644 $o0 0 z/c" &&
+ echo "100644 $o1 0 z/d/e"
+ ) >expected &&
+ test_cmp expected actual
+
+'
+
+test_expect_success 'merge-recursive w/ empty work tree - ours has rename' '
+ (
+ GIT_WORK_TREE="$PWD/ours-has-rename-work" &&
+ export GIT_WORK_TREE &&
+ GIT_INDEX_FILE="$PWD/ours-has-rename-index" &&
+ export GIT_INDEX_FILE &&
+ mkdir "$GIT_WORK_TREE" &&
+ git read-tree -i -m $c7 2>actual-err &&
+ test_must_be_empty actual-err &&
+ git update-index --ignore-missing --refresh 2>actual-err &&
+ test_must_be_empty actual-err &&
+ git merge-recursive $c0 -- $c7 $c3 2>actual-err &&
+ test_must_be_empty actual-err &&
+ git ls-files -s >actual-files 2>actual-err &&
+ test_must_be_empty actual-err
+ ) &&
+ cat >expected-files <<-EOF &&
+ 100644 $o3 0 b/c
+ 100644 $o0 0 c
+ 100644 $o0 0 d/e
+ 100644 $o0 0 e
+ EOF
+ test_cmp expected-files actual-files
+'
+
+test_expect_success 'merge-recursive w/ empty work tree - theirs has rename' '
+ (
+ GIT_WORK_TREE="$PWD/theirs-has-rename-work" &&
+ export GIT_WORK_TREE &&
+ GIT_INDEX_FILE="$PWD/theirs-has-rename-index" &&
+ export GIT_INDEX_FILE &&
+ mkdir "$GIT_WORK_TREE" &&
+ git read-tree -i -m $c3 2>actual-err &&
+ test_must_be_empty actual-err &&
+ git update-index --ignore-missing --refresh 2>actual-err &&
+ test_must_be_empty actual-err &&
+ git merge-recursive $c0 -- $c3 $c7 2>actual-err &&
+ test_must_be_empty actual-err &&
+ git ls-files -s >actual-files 2>actual-err &&
+ test_must_be_empty actual-err
+ ) &&
+ cat >expected-files <<-EOF &&
+ 100644 $o3 0 b/c
+ 100644 $o0 0 c
+ 100644 $o0 0 d/e
+ 100644 $o0 0 e
+ EOF
+ test_cmp expected-files actual-files
+'
+
+test_expect_success 'merge removes empty directories' '
+
+ git reset --hard main &&
+ git checkout -b rm &&
+ git rm d/e &&
+ git commit -mremoved-d/e &&
+ git checkout main &&
+ git merge -s recursive rm &&
+ test_path_is_missing d
+'
+
+test_expect_success 'merge-recursive simple w/submodule' '
+
+ git checkout submod &&
+ git merge remove
+'
+
+test_expect_success 'merge-recursive simple w/submodule result' '
+
+ git ls-files -s >actual &&
+ (
+ echo "100644 $o5 0 a" &&
+ echo "100644 $o0 0 c" &&
+ echo "160000 $c1 0 d"
+ ) >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'merge-recursive copy vs. rename' '
+ git checkout -f copy &&
+ git merge rename &&
+ ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
+ (
+ echo "100644 blob $o0 b" &&
+ echo "100644 blob $o0 c" &&
+ echo "100644 blob $o0 d/e" &&
+ echo "100644 blob $o0 e" &&
+ echo "100644 $o0 0 b" &&
+ echo "100644 $o0 0 c" &&
+ echo "100644 $o0 0 d/e" &&
+ echo "100644 $o0 0 e"
+ ) >expected &&
+ test_cmp expected actual
+'
+
+test_expect_merge_algorithm failure success 'merge-recursive rename vs. rename/symlink' '
+
+ git checkout -f rename &&
+ git merge rename-ln &&
+ ( git ls-tree -r HEAD && git ls-files -s ) >actual &&
+ (
+ echo "120000 blob $oln a" &&
+ echo "100644 blob $o0 b" &&
+ echo "100644 blob $o0 c" &&
+ echo "100644 blob $o0 d/e" &&
+ echo "100644 blob $o0 e" &&
+ echo "120000 $oln 0 a" &&
+ echo "100644 $o0 0 b" &&
+ echo "100644 $o0 0 c" &&
+ echo "100644 $o0 0 d/e" &&
+ echo "100644 $o0 0 e"
+ ) >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'merging with triple rename across D/F conflict' '
+ git reset --hard HEAD &&
+ git checkout -b topic &&
+ git rm -rf . &&
+
+ echo "just a file" >sub1 &&
+ mkdir -p sub2 &&
+ echo content1 >sub2/file1 &&
+ echo content2 >sub2/file2 &&
+ echo content3 >sub2/file3 &&
+ mkdir simple &&
+ echo base >simple/bar &&
+ git add -A &&
+ test_tick &&
+ git commit -m base &&
+
+ git checkout -b other &&
+ echo more >>simple/bar &&
+ test_tick &&
+ git commit -a -m changesimplefile &&
+
+ git checkout topic &&
+ git rm sub1 &&
+ git mv sub2 sub1 &&
+ test_tick &&
+ git commit -m changefiletodir &&
+
+ test_tick &&
+ git merge other
+'
+
+test_expect_success 'merge-recursive remembers the names of all base trees' '
+ git reset --hard HEAD &&
+
+ # make the index match $c1 so that merge-recursive below does not
+ # fail early
+ git diff --binary HEAD $c1 -- | git apply --cached &&
+
+ # more trees than static slots used by oid_to_hex()
+ for commit in $c0 $c2 $c4 $c5 $c6 $c7
+ do
+ git rev-parse "$commit^{tree}" || return 1
+ done >trees &&
+
+ # ignore the return code; it only fails because the input is weird...
+ test_must_fail git -c merge.verbosity=5 merge-recursive $(cat trees) -- $c1 $c3 >out &&
+
+ # ...but make sure it fails in the expected way
+ test_grep CONFLICT.*rename/rename out &&
+
+ # merge-recursive prints in reverse order, but we do not care
+ sort <trees >expect &&
+ sed -n "s/^virtual //p" out | sort >actual &&
+ test_cmp expect actual &&
+
+ git clean -fd
+'
+
+test_expect_success 'merge-recursive internal merge resolves to the sameness' '
+ git reset --hard HEAD &&
+
+ # We are going to create a history leading to two criss-cross
+ # branches A and B. The common ancestor at the bottom, O0,
+ # has two child commits O1 and O2, both of which will be merge
+ # base between A and B, like so:
+ #
+ # O1---A
+ # / \ /
+ # O0 .
+ # \ / \
+ # O2---B
+ #
+ # The recently added "check to see if the index is different from
+ # the tree into which something else is getting merged" check must
+ # NOT kick in when an inner merge between O1 and O2 is made. Both
+ # O1 and O2 happen to have the same tree as O0 in this test to
+ # trigger the bug---whether the inner merge is made by merging O2
+ # into O1 or O1 into O2, their common ancestor O0 and the branch
+ # being merged have the same tree. We should not trigger the "is
+ # the index dirty?" check in this case.
+
+ echo "zero" >file &&
+ git add file &&
+ test_tick &&
+ git commit -m "O0" &&
+ O0=$(git rev-parse HEAD) &&
+
+ test_tick &&
+ git commit --allow-empty -m "O1" &&
+ O1=$(git rev-parse HEAD) &&
+
+ git reset --hard $O0 &&
+ test_tick &&
+ git commit --allow-empty -m "O2" &&
+ O2=$(git rev-parse HEAD) &&
+
+ test_tick &&
+ git merge -s ours $O1 &&
+ B=$(git rev-parse HEAD) &&
+
+ git reset --hard $O1 &&
+ test_tick &&
+ git merge -s ours $O2 &&
+ A=$(git rev-parse HEAD) &&
+
+ git merge $B
+'
+
+test_done
diff --git a/t/t6431-merge-criscross.sh b/t/t6431-merge-criscross.sh
new file mode 100755
index 0000000..3fe14cd
--- /dev/null
+++ b/t/t6431-merge-criscross.sh
@@ -0,0 +1,96 @@
+#!/bin/sh
+
+test_description='merge-recursive backend test'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# A <- create some files
+# / \
+# B C <- cause rename/delete conflicts between B and C
+# / \
+# |\ /|
+# | D E |
+# | \ / |
+# | X |
+# | / \ |
+# | / \ |
+# |/ \|
+# F G <- merge E into B, D into C
+# \ /
+# \ /
+# \ /
+# H <- recursive merge crashes
+#
+
+# initialize
+test_expect_success 'setup repo with criss-cross history' '
+ mkdir data &&
+
+ # create a bunch of files
+ n=1 &&
+ while test $n -le 10
+ do
+ echo $n > data/$n &&
+ n=$(($n+1)) ||
+ return 1
+ done &&
+
+ # check them in
+ git add data &&
+ git commit -m A &&
+ git branch A &&
+
+ # a file in one branch
+ git checkout -b B A &&
+ git rm data/9 &&
+ git add data &&
+ git commit -m B &&
+
+ # with a branch off of it
+ git branch D &&
+
+ # put some commits on D
+ git checkout D &&
+ echo testD > data/testD &&
+ git add data &&
+ git commit -m D &&
+
+ # back up to the top, create another branch and cause
+ # a rename conflict with the file we deleted earlier
+ git checkout -b C A &&
+ git mv data/9 data/new-9 &&
+ git add data &&
+ git commit -m C &&
+
+ # with a branch off of it
+ git branch E &&
+
+ # put a commit on E
+ git checkout E &&
+ echo testE > data/testE &&
+ git add data &&
+ git commit -m E &&
+
+ # now, merge E into B
+ git checkout B &&
+ test_must_fail git merge E &&
+ # force-resolve
+ git add data &&
+ git commit -m F &&
+ git branch F &&
+
+ # and merge D into C
+ git checkout C &&
+ test_must_fail git merge D &&
+ # force-resolve
+ git add data &&
+ git commit -m G &&
+ git branch G
+'
+
+test_expect_success 'recursive merge between F and G does not cause segfault' '
+ git merge F
+'
+
+test_done
diff --git a/t/t6432-merge-recursive-space-options.sh b/t/t6432-merge-recursive-space-options.sh
new file mode 100755
index 0000000..db4b77e
--- /dev/null
+++ b/t/t6432-merge-recursive-space-options.sh
@@ -0,0 +1,210 @@
+#!/bin/sh
+
+test_description='merge-recursive space options
+
+* [main] Clarify
+ ! [remote] Remove cruft
+--
+ + [remote] Remove cruft
+* [main] Clarify
+*+ [remote^] Initial revision
+* ok 1: setup
+'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_have_prereq SED_STRIPS_CR && SED_OPTIONS=-b
+if test_have_prereq GREP_STRIPS_CR
+then
+ GREP_OPTIONS=-U
+ export GREP_OPTIONS
+fi
+
+test_expect_success 'setup' '
+ conflict_hunks () {
+ sed $SED_OPTIONS -n -e "
+ /^<<<</ b conflict
+ b
+ : conflict
+ p
+ /^>>>>/ b
+ n
+ b conflict
+ " "$@"
+ } &&
+
+ cat <<-\EOF >text.txt &&
+ Hope, he says, cherishes the soul of him who lives in
+ justice and holiness and is the nurse of his age and the
+ companion of his journey;--hope which is mightiest to sway
+ the restless soul of man.
+
+ How admirable are his words! And the great blessing of riches, I do
+ not say to every man, but to a good man, is, that he has had no
+ occasion to deceive or to defraud others, either intentionally or
+ unintentionally; and when he departs to the world below he is not in
+ any apprehension about offerings due to the gods or debts which he owes
+ to men. Now to this peace of mind the possession of wealth greatly
+ contributes; and therefore I say, that, setting one thing against
+ another, of the many advantages which wealth has to give, to a man of
+ sense this is in my opinion the greatest.
+
+ Well said, Cephalus, I replied; but as concerning justice, what is
+ it?--to speak the truth and to pay your debts--no more than this? And
+ even to this are there not exceptions? Suppose that a friend when in
+ his right mind has deposited arms with me and he asks for them when he
+ is not in his right mind, ought I to give them back to him? No one
+ would say that I ought or that I should be right in doing so, any more
+ than they would say that I ought always to speak the truth to one who
+ is in his condition.
+
+ You are quite right, he replied.
+
+ But then, I said, speaking the truth and paying your debts is not a
+ correct definition of justice.
+
+ CEPHALUS - SOCRATES - POLEMARCHUS
+
+ Quite correct, Socrates, if Simonides is to be believed, said
+ Polemarchus interposing.
+
+ I fear, said Cephalus, that I must go now, for I have to look after the
+ sacrifices, and I hand over the argument to Polemarchus and the company.
+ EOF
+ git add text.txt &&
+ test_tick &&
+ git commit -m "Initial revision" &&
+
+ git checkout -b remote &&
+ sed -e "
+ s/\. /\. /g
+ s/[?] /? /g
+ s/ / /g
+ s/--/---/g
+ s/but as concerning/but as con cerning/
+ /CEPHALUS - SOCRATES - POLEMARCHUS/ d
+ " text.txt >text.txt+ &&
+ mv text.txt+ text.txt &&
+ git commit -a -m "Remove cruft" &&
+
+ git checkout main &&
+ sed -e "
+ s/\(not in his right mind\),\(.*\)/\1;\2Q/
+ s/Quite correct\(.*\)/It is too correct\1Q/
+ s/unintentionally/un intentionally/
+ /un intentionally/ s/$/Q/
+ s/Polemarchus interposing./Polemarchus, interposing.Q/
+ /justice and holiness/ s/$/Q/
+ /pay your debts/ s/$/Q/
+ " text.txt | q_to_cr >text.txt+ &&
+ mv text.txt+ text.txt &&
+ git commit -a -m "Clarify" &&
+ git show-branch --all
+'
+
+test_expect_success 'naive merge fails' '
+ git read-tree --reset -u HEAD &&
+ test_must_fail git merge-recursive HEAD^ -- HEAD remote &&
+ test_must_fail git update-index --refresh &&
+ grep "<<<<<<" text.txt
+'
+
+test_expect_success '--ignore-space-change makes merge succeed' '
+ git read-tree --reset -u HEAD &&
+ git merge-recursive --ignore-space-change HEAD^ -- HEAD remote
+'
+
+test_expect_success 'naive cherry-pick fails' '
+ git read-tree --reset -u HEAD &&
+ test_must_fail git cherry-pick --no-commit remote &&
+ git read-tree --reset -u HEAD &&
+ test_must_fail git cherry-pick remote &&
+ test_must_fail git update-index --refresh &&
+ grep "<<<<<<" text.txt
+'
+
+test_expect_success '-Xignore-space-change makes cherry-pick succeed' '
+ git read-tree --reset -u HEAD &&
+ git cherry-pick --no-commit -Xignore-space-change remote
+'
+
+test_expect_success '--ignore-space-change: our w/s-only change wins' '
+ q_to_cr <<-\EOF >expected &&
+ justice and holiness and is the nurse of his age and theQ
+ EOF
+
+ git read-tree --reset -u HEAD &&
+ git merge-recursive --ignore-space-change HEAD^ -- HEAD remote &&
+ grep "justice and holiness" text.txt >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success '--ignore-space-change: their real change wins over w/s' '
+ cat <<-\EOF >expected &&
+ it?---to speak the truth and to pay your debts---no more than this? And
+ EOF
+
+ git read-tree --reset -u HEAD &&
+ git merge-recursive --ignore-space-change HEAD^ -- HEAD remote &&
+ grep "pay your debts" text.txt >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success '--ignore-space-change: does not ignore new spaces' '
+ cat <<-\EOF >expected1 &&
+ Well said, Cephalus, I replied; but as con cerning justice, what is
+ EOF
+ q_to_cr <<-\EOF >expected2 &&
+ un intentionally; and when he departs to the world below he is not inQ
+ EOF
+
+ git read-tree --reset -u HEAD &&
+ git merge-recursive --ignore-space-change HEAD^ -- HEAD remote &&
+ grep "Well said" text.txt >actual1 &&
+ grep "when he departs" text.txt >actual2 &&
+ test_cmp expected1 actual1 &&
+ test_cmp expected2 actual2
+'
+
+test_expect_success '--ignore-all-space drops their new spaces' '
+ cat <<-\EOF >expected &&
+ Well said, Cephalus, I replied; but as concerning justice, what is
+ EOF
+
+ git read-tree --reset -u HEAD &&
+ git merge-recursive --ignore-all-space HEAD^ -- HEAD remote &&
+ grep "Well said" text.txt >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success '--ignore-all-space keeps our new spaces' '
+ q_to_cr <<-\EOF >expected &&
+ un intentionally; and when he departs to the world below he is not inQ
+ EOF
+
+ git read-tree --reset -u HEAD &&
+ git merge-recursive --ignore-all-space HEAD^ -- HEAD remote &&
+ grep "when he departs" text.txt >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success '--ignore-space-at-eol' '
+ q_to_cr <<-\EOF >expected &&
+ <<<<<<< HEAD
+ is not in his right mind; ought I to give them back to him? No oneQ
+ =======
+ is not in his right mind, ought I to give them back to him? No one
+ >>>>>>> remote
+ EOF
+
+ git read-tree --reset -u HEAD &&
+ test_must_fail git merge-recursive --ignore-space-at-eol \
+ HEAD^ -- HEAD remote &&
+ conflict_hunks text.txt >actual &&
+ test_cmp expected actual
+'
+
+test_done
diff --git a/t/t6433-merge-toplevel.sh b/t/t6433-merge-toplevel.sh
new file mode 100755
index 0000000..ed7866d
--- /dev/null
+++ b/t/t6433-merge-toplevel.sh
@@ -0,0 +1,178 @@
+#!/bin/sh
+
+test_description='"git merge" top-level frontend'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+t3033_reset () {
+ git checkout -B main two &&
+ git branch -f left three &&
+ git branch -f right four
+}
+
+test_expect_success setup '
+ test_commit one &&
+ git branch left &&
+ git branch right &&
+ test_commit two &&
+ git checkout left &&
+ test_commit three &&
+ git checkout right &&
+ test_commit four &&
+ git checkout --orphan newroot &&
+ test_commit five &&
+ git checkout main
+'
+
+# Local branches
+
+test_expect_success 'merge an octopus into void' '
+ t3033_reset &&
+ git checkout --orphan test &&
+ git rm -fr . &&
+ test_must_fail git merge left right &&
+ test_must_fail git rev-parse --verify HEAD &&
+ git diff --quiet &&
+ test_must_fail git rev-parse HEAD
+'
+
+test_expect_success 'merge an octopus, fast-forward (ff)' '
+ t3033_reset &&
+ git reset --hard one &&
+ git merge left right &&
+ # one is ancestor of three (left) and four (right)
+ test_must_fail git rev-parse --verify HEAD^3 &&
+ git rev-parse HEAD^1 HEAD^2 | sort >actual &&
+ git rev-parse three four | sort >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'merge octopus, non-fast-forward (ff)' '
+ t3033_reset &&
+ git reset --hard one &&
+ git merge --no-ff left right &&
+ # one is ancestor of three (left) and four (right)
+ test_must_fail git rev-parse --verify HEAD^4 &&
+ git rev-parse HEAD^1 HEAD^2 HEAD^3 | sort >actual &&
+ git rev-parse one three four | sort >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'merge octopus, fast-forward (does not ff)' '
+ t3033_reset &&
+ git merge left right &&
+ # two (main) is not an ancestor of three (left) and four (right)
+ test_must_fail git rev-parse --verify HEAD^4 &&
+ git rev-parse HEAD^1 HEAD^2 HEAD^3 | sort >actual &&
+ git rev-parse two three four | sort >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'merge octopus, non-fast-forward' '
+ t3033_reset &&
+ git merge --no-ff left right &&
+ test_must_fail git rev-parse --verify HEAD^4 &&
+ git rev-parse HEAD^1 HEAD^2 HEAD^3 | sort >actual &&
+ git rev-parse two three four | sort >expect &&
+ test_cmp expect actual
+'
+
+# The same set with FETCH_HEAD
+
+test_expect_success 'merge FETCH_HEAD octopus into void' '
+ t3033_reset &&
+ git checkout --orphan test &&
+ git rm -fr . &&
+ git fetch . left right &&
+ test_must_fail git merge FETCH_HEAD &&
+ test_must_fail git rev-parse --verify HEAD &&
+ git diff --quiet &&
+ test_must_fail git rev-parse HEAD
+'
+
+test_expect_success 'merge FETCH_HEAD octopus fast-forward (ff)' '
+ t3033_reset &&
+ git reset --hard one &&
+ git fetch . left right &&
+ git merge FETCH_HEAD &&
+ # one is ancestor of three (left) and four (right)
+ test_must_fail git rev-parse --verify HEAD^3 &&
+ git rev-parse HEAD^1 HEAD^2 | sort >actual &&
+ git rev-parse three four | sort >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'merge FETCH_HEAD octopus non-fast-forward (ff)' '
+ t3033_reset &&
+ git reset --hard one &&
+ git fetch . left right &&
+ git merge --no-ff FETCH_HEAD &&
+ # one is ancestor of three (left) and four (right)
+ test_must_fail git rev-parse --verify HEAD^4 &&
+ git rev-parse HEAD^1 HEAD^2 HEAD^3 | sort >actual &&
+ git rev-parse one three four | sort >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'merge FETCH_HEAD octopus fast-forward (does not ff)' '
+ t3033_reset &&
+ git fetch . left right &&
+ git merge FETCH_HEAD &&
+ # two (main) is not an ancestor of three (left) and four (right)
+ test_must_fail git rev-parse --verify HEAD^4 &&
+ git rev-parse HEAD^1 HEAD^2 HEAD^3 | sort >actual &&
+ git rev-parse two three four | sort >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'merge FETCH_HEAD octopus non-fast-forward' '
+ t3033_reset &&
+ git fetch . left right &&
+ git merge --no-ff FETCH_HEAD &&
+ test_must_fail git rev-parse --verify HEAD^4 &&
+ git rev-parse HEAD^1 HEAD^2 HEAD^3 | sort >actual &&
+ git rev-parse two three four | sort >expect &&
+ test_cmp expect actual
+'
+
+# two-project merge
+test_expect_success 'refuse two-project merge by default' '
+ t3033_reset &&
+ git reset --hard four &&
+ test_must_fail git merge five
+'
+
+test_expect_success 'refuse two-project merge by default, quit before --autostash happens' '
+ t3033_reset &&
+ git reset --hard four &&
+ echo change >>one.t &&
+ git diff >expect &&
+ test_must_fail git merge --autostash five 2>err &&
+ test_grep ! "stash" err &&
+ git diff >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'two-project merge with --allow-unrelated-histories' '
+ t3033_reset &&
+ git reset --hard four &&
+ git merge --allow-unrelated-histories five &&
+ git diff --exit-code five
+'
+
+test_expect_success 'two-project merge with --allow-unrelated-histories with --autostash' '
+ t3033_reset &&
+ git reset --hard four &&
+ echo change >>one.t &&
+ git diff one.t >expect &&
+ git merge --allow-unrelated-histories --autostash five 2>err &&
+ test_grep "Applied autostash." err &&
+ git diff one.t >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t6434-merge-recursive-rename-options.sh b/t/t6434-merge-recursive-rename-options.sh
new file mode 100755
index 0000000..a117078
--- /dev/null
+++ b/t/t6434-merge-recursive-rename-options.sh
@@ -0,0 +1,333 @@
+#!/bin/sh
+
+test_description='merge-recursive rename options
+
+Test rename detection by examining rename/delete conflicts.
+
+* (HEAD -> rename) rename
+| * (main) delete
+|/
+* base
+
+git diff --name-status base main
+D 0-old
+D 1-old
+D 2-old
+D 3-old
+
+git diff --name-status -M01 base rename
+R025 0-old 0-new
+R050 1-old 1-new
+R075 2-old 2-new
+R100 3-old 3-new
+
+Actual similarity indices are parsed from diff output. We rely on the fact that
+they are rounded down (see, e.g., Documentation/diff-generate-patch.txt, which
+mentions this in a different context).
+'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+get_expected_stages () {
+ git checkout rename -- $1-new &&
+ git ls-files --stage $1-new >expected-stages-undetected-$1 &&
+ sed "s/ 0 / 2 /" <expected-stages-undetected-$1 \
+ >expected-stages-detected-$1 &&
+ git read-tree -u --reset HEAD
+}
+
+rename_detected () {
+ git ls-files --stage $1-old $1-new >stages-actual-$1 &&
+ test_cmp expected-stages-detected-$1 stages-actual-$1
+}
+
+rename_undetected () {
+ git ls-files --stage $1-old $1-new >stages-actual-$1 &&
+ test_cmp expected-stages-undetected-$1 stages-actual-$1
+}
+
+check_common () {
+ git ls-files --stage >stages-actual &&
+ test_line_count = 4 stages-actual
+}
+
+check_threshold_0 () {
+ check_common &&
+ rename_detected 0 &&
+ rename_detected 1 &&
+ rename_detected 2 &&
+ rename_detected 3
+}
+
+check_threshold_1 () {
+ check_common &&
+ rename_undetected 0 &&
+ rename_detected 1 &&
+ rename_detected 2 &&
+ rename_detected 3
+}
+
+check_threshold_2 () {
+ check_common &&
+ rename_undetected 0 &&
+ rename_undetected 1 &&
+ rename_detected 2 &&
+ rename_detected 3
+}
+
+check_exact_renames () {
+ check_common &&
+ rename_undetected 0 &&
+ rename_undetected 1 &&
+ rename_undetected 2 &&
+ rename_detected 3
+}
+
+check_no_renames () {
+ check_common &&
+ rename_undetected 0 &&
+ rename_undetected 1 &&
+ rename_undetected 2 &&
+ rename_undetected 3
+}
+
+test_expect_success 'setup repo' '
+ cat <<-\EOF >3-old &&
+ 33a
+ 33b
+ 33c
+ 33d
+ EOF
+ sed s/33/22/ <3-old >2-old &&
+ sed s/33/11/ <3-old >1-old &&
+ sed s/33/00/ <3-old >0-old &&
+ git add [0-3]-old &&
+ git commit -m base &&
+ git rm [0-3]-old &&
+ git commit -m delete &&
+ git checkout -b rename HEAD^ &&
+ cp 3-old 3-new &&
+ sed 1,1s/./x/ <2-old >2-new &&
+ sed 1,2s/./x/ <1-old >1-new &&
+ sed 1,3s/./x/ <0-old >0-new &&
+ git add [0-3]-new &&
+ git rm [0-3]-old &&
+ git commit -m rename &&
+ get_expected_stages 0 &&
+ get_expected_stages 1 &&
+ get_expected_stages 2 &&
+ get_expected_stages 3 &&
+ check_50="false" &&
+ tail="HEAD^ -- HEAD main"
+'
+
+test_expect_success 'setup thresholds' '
+ git diff --name-status -M01 HEAD^ HEAD >diff-output &&
+ test_debug "cat diff-output" &&
+ test_line_count = 4 diff-output &&
+ grep "R[0-9][0-9][0-9] \([0-3]\)-old \1-new" diff-output \
+ >grep-output &&
+ test_cmp diff-output grep-output &&
+ th0=$(sed -n "s/R\(...\) 0-old 0-new/\1/p" <diff-output) &&
+ th1=$(sed -n "s/R\(...\) 1-old 1-new/\1/p" <diff-output) &&
+ th2=$(sed -n "s/R\(...\) 2-old 2-new/\1/p" <diff-output) &&
+ th3=$(sed -n "s/R\(...\) 3-old 3-new/\1/p" <diff-output) &&
+ test "$th0" -lt "$th1" &&
+ test "$th1" -lt "$th2" &&
+ test "$th2" -lt "$th3" &&
+ test "$th3" = 100 &&
+ if test 50 -le "$th0"
+ then
+ check_50=check_threshold_0
+ elif test 50 -le "$th1"
+ then
+ check_50=check_threshold_1
+ elif test 50 -le "$th2"
+ then
+ check_50=check_threshold_2
+ fi &&
+ th0="$th0%" &&
+ th1="$th1%" &&
+ th2="$th2%" &&
+ th3="$th3%"
+'
+
+test_expect_success 'assumption for tests: rename detection with diff' '
+ git diff --name-status -M$th0 --diff-filter=R HEAD^ HEAD \
+ >diff-output-0 &&
+ git diff --name-status -M$th1 --diff-filter=R HEAD^ HEAD \
+ >diff-output-1 &&
+ git diff --name-status -M$th2 --diff-filter=R HEAD^ HEAD \
+ >diff-output-2 &&
+ git diff --name-status -M100% --diff-filter=R HEAD^ HEAD \
+ >diff-output-3 &&
+ test_line_count = 4 diff-output-0 &&
+ test_line_count = 3 diff-output-1 &&
+ test_line_count = 2 diff-output-2 &&
+ test_line_count = 1 diff-output-3
+'
+
+test_expect_success 'default similarity threshold is 50%' '
+ git read-tree --reset -u HEAD &&
+ test_must_fail git merge-recursive $tail &&
+ $check_50
+'
+
+test_expect_success 'low rename threshold' '
+ git read-tree --reset -u HEAD &&
+ test_must_fail git merge-recursive --find-renames=$th0 $tail &&
+ check_threshold_0
+'
+
+test_expect_success 'medium rename threshold' '
+ git read-tree --reset -u HEAD &&
+ test_must_fail git merge-recursive --find-renames=$th1 $tail &&
+ check_threshold_1
+'
+
+test_expect_success 'high rename threshold' '
+ git read-tree --reset -u HEAD &&
+ test_must_fail git merge-recursive --find-renames=$th2 $tail &&
+ check_threshold_2
+'
+
+test_expect_success 'exact renames only' '
+ git read-tree --reset -u HEAD &&
+ test_must_fail git merge-recursive --find-renames=100% $tail &&
+ check_exact_renames
+'
+
+test_expect_success 'rename threshold is truncated' '
+ git read-tree --reset -u HEAD &&
+ test_must_fail git merge-recursive --find-renames=200% $tail &&
+ check_exact_renames
+'
+
+test_expect_success 'disabled rename detection' '
+ git read-tree --reset -u HEAD &&
+ git merge-recursive --no-renames $tail &&
+ check_no_renames
+'
+
+test_expect_success 'last wins in --find-renames=<m> --find-renames=<n>' '
+ git read-tree --reset -u HEAD &&
+ test_must_fail git merge-recursive \
+ --find-renames=$th0 --find-renames=$th2 $tail &&
+ check_threshold_2
+'
+
+test_expect_success '--find-renames resets threshold' '
+ git read-tree --reset -u HEAD &&
+ test_must_fail git merge-recursive \
+ --find-renames=$th0 --find-renames $tail &&
+ $check_50
+'
+
+test_expect_success 'last wins in --no-renames --find-renames' '
+ git read-tree --reset -u HEAD &&
+ test_must_fail git merge-recursive --no-renames --find-renames $tail &&
+ $check_50
+'
+
+test_expect_success 'last wins in --find-renames --no-renames' '
+ git read-tree --reset -u HEAD &&
+ git merge-recursive --find-renames --no-renames $tail &&
+ check_no_renames
+'
+
+test_expect_success 'assumption for further tests: trivial merge succeeds' '
+ git read-tree --reset -u HEAD &&
+ git merge-recursive HEAD -- HEAD HEAD &&
+ git diff --quiet --cached &&
+ git merge-recursive --find-renames=$th0 HEAD -- HEAD HEAD &&
+ git diff --quiet --cached &&
+ git merge-recursive --find-renames=$th2 HEAD -- HEAD HEAD &&
+ git diff --quiet --cached &&
+ git merge-recursive --find-renames=100% HEAD -- HEAD HEAD &&
+ git diff --quiet --cached &&
+ git merge-recursive --no-renames HEAD -- HEAD HEAD &&
+ git diff --quiet --cached
+'
+
+test_expect_success '--find-renames rejects negative argument' '
+ git read-tree --reset -u HEAD &&
+ test_must_fail git merge-recursive --find-renames=-25 \
+ HEAD -- HEAD HEAD &&
+ git diff --quiet --cached
+'
+
+test_expect_success '--find-renames rejects non-numbers' '
+ git read-tree --reset -u HEAD &&
+ test_must_fail git merge-recursive --find-renames=0xf \
+ HEAD -- HEAD HEAD &&
+ git diff --quiet --cached
+'
+
+test_expect_success 'rename-threshold=<n> is a synonym for find-renames=<n>' '
+ git read-tree --reset -u HEAD &&
+ test_must_fail git merge-recursive --rename-threshold=$th0 $tail &&
+ check_threshold_0
+'
+
+test_expect_success 'last wins in --no-renames --rename-threshold=<n>' '
+ git read-tree --reset -u HEAD &&
+ test_must_fail git merge-recursive --no-renames --rename-threshold=$th0 $tail &&
+ check_threshold_0
+'
+
+test_expect_success 'last wins in --rename-threshold=<n> --no-renames' '
+ git read-tree --reset -u HEAD &&
+ git merge-recursive --rename-threshold=$th0 --no-renames $tail &&
+ check_no_renames
+'
+
+test_expect_success '--rename-threshold=<n> rejects negative argument' '
+ git read-tree --reset -u HEAD &&
+ test_must_fail git merge-recursive --rename-threshold=-25 \
+ HEAD -- HEAD HEAD &&
+ git diff --quiet --cached
+'
+
+test_expect_success '--rename-threshold=<n> rejects non-numbers' '
+ git read-tree --reset -u HEAD &&
+ test_must_fail git merge-recursive --rename-threshold=0xf \
+ HEAD -- HEAD HEAD &&
+ git diff --quiet --cached
+'
+
+test_expect_success 'last wins in --rename-threshold=<m> --find-renames=<n>' '
+ git read-tree --reset -u HEAD &&
+ test_must_fail git merge-recursive \
+ --rename-threshold=$th0 --find-renames=$th2 $tail &&
+ check_threshold_2
+'
+
+test_expect_success 'last wins in --find-renames=<m> --rename-threshold=<n>' '
+ git read-tree --reset -u HEAD &&
+ test_must_fail git merge-recursive \
+ --find-renames=$th2 --rename-threshold=$th0 $tail &&
+ check_threshold_0
+'
+
+test_expect_success 'merge.renames disables rename detection' '
+ git read-tree --reset -u HEAD &&
+ git -c merge.renames=false merge-recursive $tail &&
+ check_no_renames
+'
+
+test_expect_success 'merge.renames defaults to diff.renames' '
+ git read-tree --reset -u HEAD &&
+ git -c diff.renames=false merge-recursive $tail &&
+ check_no_renames
+'
+
+test_expect_success 'merge.renames overrides diff.renames' '
+ git read-tree --reset -u HEAD &&
+ test_must_fail git -c diff.renames=false -c merge.renames=true merge-recursive $tail &&
+ $check_50
+'
+
+test_done
diff --git a/t/t6435-merge-sparse.sh b/t/t6435-merge-sparse.sh
new file mode 100755
index 0000000..78628fb
--- /dev/null
+++ b/t/t6435-merge-sparse.sh
@@ -0,0 +1,61 @@
+#!/bin/sh
+
+test_description='merge with sparse files'
+
+TEST_CREATE_REPO_NO_TEMPLATE=1
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# test_file $filename $content
+test_file () {
+ echo "$2" > "$1" &&
+ git add "$1"
+}
+
+# test_commit_this $message_and_tag
+test_commit_this () {
+ git commit -m "$1" &&
+ git tag "$1"
+}
+
+test_expect_success 'setup' '
+ test_file checked-out init &&
+ test_file modify_delete modify_delete_init &&
+ test_commit_this init &&
+ test_file modify_delete modify_delete_theirs &&
+ test_commit_this theirs &&
+ git reset --hard init &&
+ git rm modify_delete &&
+ test_commit_this ours &&
+ git config core.sparseCheckout true &&
+ mkdir .git/info &&
+ echo "/checked-out" >.git/info/sparse-checkout &&
+ git reset --hard &&
+ test_must_fail git merge theirs
+'
+
+test_expect_success 'reset --hard works after the conflict' '
+ git reset --hard
+'
+
+test_expect_success 'is reset properly' '
+ git status --porcelain -- modify_delete >out &&
+ test_must_be_empty out &&
+ test_path_is_missing modify_delete
+'
+
+test_expect_success 'setup: conflict back' '
+ test_must_fail git merge theirs
+'
+
+test_expect_success 'Merge abort works after the conflict' '
+ git merge --abort
+'
+
+test_expect_success 'is aborted properly' '
+ git status --porcelain -- modify_delete >out &&
+ test_must_be_empty out &&
+ test_path_is_missing modify_delete
+'
+
+test_done
diff --git a/t/t6436-merge-overwrite.sh b/t/t6436-merge-overwrite.sh
new file mode 100755
index 0000000..4f43764
--- /dev/null
+++ b/t/t6436-merge-overwrite.sh
@@ -0,0 +1,207 @@
+#!/bin/sh
+
+test_description='git-merge
+
+Do not overwrite changes.'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ test_commit c0 c0.c &&
+ test_commit c1 c1.c &&
+ test_commit c1a c1.c "c1 a" &&
+ git reset --hard c0 &&
+ test_commit c2 c2.c &&
+ git reset --hard c0 &&
+ mkdir sub &&
+ echo "sub/f" > sub/f &&
+ mkdir sub2 &&
+ echo "sub2/f" > sub2/f &&
+ git add sub/f sub2/f &&
+ git commit -m sub &&
+ git tag sub &&
+ echo "VERY IMPORTANT CHANGES" > important
+'
+
+test_expect_success 'will not overwrite untracked file' '
+ git reset --hard c1 &&
+ cp important c2.c &&
+ test_must_fail git merge c2 &&
+ test_path_is_missing .git/MERGE_HEAD &&
+ test_cmp important c2.c
+'
+
+test_expect_success 'will overwrite tracked file' '
+ git reset --hard c1 &&
+ cp important c2.c &&
+ git add c2.c &&
+ git commit -m important &&
+ git checkout c2
+'
+
+test_expect_success 'will not overwrite new file' '
+ git reset --hard c1 &&
+ cp important c2.c &&
+ git add c2.c &&
+ test_must_fail git merge c2 &&
+ test_path_is_missing .git/MERGE_HEAD &&
+ test_cmp important c2.c
+'
+
+test_expect_success 'will not overwrite staged changes' '
+ git reset --hard c1 &&
+ cp important c2.c &&
+ git add c2.c &&
+ rm c2.c &&
+ test_must_fail git merge c2 &&
+ test_path_is_missing .git/MERGE_HEAD &&
+ git checkout c2.c &&
+ test_cmp important c2.c
+'
+
+test_expect_success 'will not overwrite removed file' '
+ git reset --hard c1 &&
+ git rm c1.c &&
+ git commit -m "rm c1.c" &&
+ cp important c1.c &&
+ test_must_fail git merge c1a &&
+ test_cmp important c1.c &&
+ rm c1.c # Do not leave untracked file in way of future tests
+'
+
+test_expect_success 'will not overwrite re-added file' '
+ git reset --hard c1 &&
+ git rm c1.c &&
+ git commit -m "rm c1.c" &&
+ cp important c1.c &&
+ git add c1.c &&
+ test_must_fail git merge c1a &&
+ test_path_is_missing .git/MERGE_HEAD &&
+ test_cmp important c1.c
+'
+
+test_expect_success 'will not overwrite removed file with staged changes' '
+ git reset --hard c1 &&
+ git rm c1.c &&
+ git commit -m "rm c1.c" &&
+ cp important c1.c &&
+ git add c1.c &&
+ rm c1.c &&
+ test_must_fail git merge c1a &&
+ test_path_is_missing .git/MERGE_HEAD &&
+ git checkout c1.c &&
+ test_cmp important c1.c
+'
+
+test_expect_success 'will not overwrite unstaged changes in renamed file' '
+ git reset --hard c1 &&
+ git mv c1.c other.c &&
+ git commit -m rename &&
+ cp important other.c &&
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ test_must_fail git merge c1a >out 2>err &&
+ test_grep "would be overwritten by merge" err &&
+ test_cmp important other.c &&
+ test_path_is_missing .git/MERGE_HEAD
+ else
+ test_must_fail git merge c1a >out &&
+ test_grep "Refusing to lose dirty file at other.c" out &&
+ test_path_is_file other.c~HEAD &&
+ test $(git hash-object other.c~HEAD) = $(git rev-parse c1a:c1.c) &&
+ test_cmp important other.c
+ fi
+'
+
+test_expect_success 'will not overwrite untracked subtree' '
+ git reset --hard c0 &&
+ rm -rf sub &&
+ mkdir -p sub/f &&
+ cp important sub/f/important &&
+ test_must_fail git merge sub &&
+ test_path_is_missing .git/MERGE_HEAD &&
+ test_cmp important sub/f/important
+'
+
+cat >expect <<\EOF
+error: The following untracked working tree files would be overwritten by merge:
+ sub
+ sub2
+Please move or remove them before you merge.
+Aborting
+EOF
+
+test_expect_success 'will not overwrite untracked file in leading path' '
+ git reset --hard c0 &&
+ rm -rf sub &&
+ cp important sub &&
+ cp important sub2 &&
+ test_must_fail git merge sub 2>out &&
+ test_cmp out expect &&
+ test_path_is_missing .git/MERGE_HEAD &&
+ test_cmp important sub &&
+ test_cmp important sub2 &&
+ rm -f sub sub2
+'
+
+test_expect_success SYMLINKS 'will not overwrite untracked symlink in leading path' '
+ git reset --hard c0 &&
+ rm -rf sub &&
+ mkdir sub2 &&
+ ln -s sub2 sub &&
+ test_must_fail git merge sub &&
+ test_path_is_missing .git/MERGE_HEAD
+'
+
+test_expect_success 'will not be confused by symlink in leading path' '
+ git reset --hard c0 &&
+ rm -rf sub &&
+ test_ln_s_add sub2 sub &&
+ git commit -m ln &&
+ git checkout sub
+'
+
+cat >expect <<\EOF
+error: Untracked working tree file 'c0.c' would be overwritten by merge.
+fatal: read-tree failed
+EOF
+
+test_expect_success 'will not overwrite untracked file on unborn branch' '
+ git reset --hard c0 &&
+ git rm -fr . &&
+ git checkout --orphan new &&
+ cp important c0.c &&
+ test_must_fail git merge c0 2>out &&
+ test_cmp out expect
+'
+
+test_expect_success 'will not overwrite untracked file on unborn branch .git/MERGE_HEAD sanity etc.' '
+ test_when_finished "rm c0.c" &&
+ test_path_is_missing .git/MERGE_HEAD &&
+ test_cmp important c0.c
+'
+
+test_expect_success 'failed merge leaves unborn branch in the womb' '
+ test_must_fail git rev-parse --verify HEAD
+'
+
+test_expect_success 'set up unborn branch and content' '
+ git symbolic-ref HEAD refs/heads/unborn &&
+ rm -f .git/index &&
+ echo foo > tracked-file &&
+ git add tracked-file &&
+ echo bar > untracked-file
+'
+
+test_expect_success 'will not clobber WT/index when merging into unborn' '
+ git merge main &&
+ grep foo tracked-file &&
+ git show :tracked-file >expect &&
+ grep foo expect &&
+ grep bar untracked-file
+'
+
+test_done
diff --git a/t/t6437-submodule-merge.sh b/t/t6437-submodule-merge.sh
new file mode 100755
index 0000000..7065052
--- /dev/null
+++ b/t/t6437-submodule-merge.sh
@@ -0,0 +1,546 @@
+#!/bin/sh
+
+test_description='merging with submodules'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB=1
+export GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-merge.sh
+
+#
+# history
+#
+# a --- c
+# / \ /
+# root X
+# \ / \
+# b --- d
+#
+
+test_expect_success setup '
+
+ mkdir sub &&
+ (cd sub &&
+ git init &&
+ echo original > file &&
+ git add file &&
+ test_tick &&
+ git commit -m sub-root) &&
+ git add sub &&
+ test_tick &&
+ git commit -m root &&
+
+ git checkout -b a main &&
+ (cd sub &&
+ echo A > file &&
+ git add file &&
+ test_tick &&
+ git commit -m sub-a) &&
+ git add sub &&
+ test_tick &&
+ git commit -m a &&
+
+ git checkout -b b main &&
+ (cd sub &&
+ echo B > file &&
+ git add file &&
+ test_tick &&
+ git commit -m sub-b) &&
+ git add sub &&
+ test_tick &&
+ git commit -m b &&
+
+ git checkout -b c a &&
+ git merge -s ours b &&
+
+ git checkout -b d b &&
+ git merge -s ours a
+'
+
+# History setup
+#
+# b
+# / \
+# init -- a d
+# \ \ /
+# g c
+#
+# a in the main repository records to sub-a in the submodule and
+# analogous b and c. d should be automatically found by merging c into
+# b in the main repository.
+test_expect_success 'setup for merge search' '
+ mkdir merge-search &&
+ (cd merge-search &&
+ git init &&
+ mkdir sub &&
+ (cd sub &&
+ git init &&
+ echo "file-a" > file-a &&
+ git add file-a &&
+ git commit -m "sub-a" &&
+ git branch sub-a) &&
+ git commit --allow-empty -m init &&
+ git branch init &&
+ git add sub &&
+ git commit -m "a" &&
+ git branch a &&
+
+ git checkout -b b &&
+ (cd sub &&
+ git checkout -b sub-b &&
+ echo "file-b" > file-b &&
+ git add file-b &&
+ git commit -m "sub-b") &&
+ git commit -a -m "b" &&
+
+ git checkout -b c a &&
+ (cd sub &&
+ git checkout -b sub-c sub-a &&
+ echo "file-c" > file-c &&
+ git add file-c &&
+ git commit -m "sub-c") &&
+ git commit -a -m "c")
+'
+
+test_expect_success 'merging should conflict for non fast-forward' '
+ test_when_finished "git -C merge-search reset --hard" &&
+ (cd merge-search &&
+ git checkout -b test-nonforward-a b &&
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ test_must_fail git merge c >actual &&
+ sub_expect="go to submodule (sub), and either merge commit $(git -C sub rev-parse --short sub-c)" &&
+ grep "$sub_expect" actual
+ else
+ test_must_fail git merge c 2> actual
+ fi)
+'
+
+test_expect_success 'finish setup for merge-search' '
+ (cd merge-search &&
+ git checkout -b d a &&
+ (cd sub &&
+ git checkout -b sub-d sub-b &&
+ git merge sub-c) &&
+ git commit -a -m "d" &&
+ git branch test b &&
+
+ git checkout -b g init &&
+ (cd sub &&
+ git checkout -b sub-g sub-c) &&
+ git add sub &&
+ git commit -a -m "g")
+'
+
+test_expect_success 'merge with one side as a fast-forward of the other' '
+ (cd merge-search &&
+ git checkout -b test-forward b &&
+ git merge d &&
+ git ls-tree test-forward sub | cut -f1 | cut -f3 -d" " > actual &&
+ (cd sub &&
+ git rev-parse sub-d > ../expect) &&
+ test_cmp expect actual)
+'
+
+test_expect_success 'merging should conflict for non fast-forward (resolution exists)' '
+ (cd merge-search &&
+ git checkout -b test-nonforward-b b &&
+ (cd sub &&
+ git rev-parse --short sub-d > ../expect) &&
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ test_must_fail git merge c >actual &&
+ sub_expect="go to submodule (sub), and either merge commit $(git -C sub rev-parse --short sub-c)" &&
+ grep "$sub_expect" actual
+ else
+ test_must_fail git merge c 2> actual
+ fi &&
+ grep $(cat expect) actual > /dev/null &&
+ git reset --hard)
+'
+
+test_expect_success 'merging should fail for ambiguous common parent' '
+ (cd merge-search &&
+ git checkout -b test-ambiguous b &&
+ (cd sub &&
+ git checkout -b ambiguous sub-b &&
+ git merge sub-c &&
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ git rev-parse --short sub-d >../expect1 &&
+ git rev-parse --short ambiguous >../expect2
+ else
+ git rev-parse sub-d > ../expect1 &&
+ git rev-parse ambiguous > ../expect2
+ fi
+ ) &&
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ test_must_fail git merge c >actual &&
+ sub_expect="go to submodule (sub), and either merge commit $(git -C sub rev-parse --short sub-c)" &&
+ grep "$sub_expect" actual
+ else
+ test_must_fail git merge c 2> actual
+ fi &&
+ grep $(cat expect1) actual > /dev/null &&
+ grep $(cat expect2) actual > /dev/null &&
+ git reset --hard)
+'
+
+# in a situation like this
+#
+# submodule tree:
+#
+# sub-a --- sub-b --- sub-d
+#
+# main tree:
+#
+# e (sub-a)
+# /
+# bb (sub-b)
+# \
+# f (sub-d)
+#
+# A merge between e and f should fail because one of the submodule
+# commits (sub-a) does not descend from the submodule merge-base (sub-b).
+#
+test_expect_success 'merging should fail for changes that are backwards' '
+ (cd merge-search &&
+ git checkout -b bb a &&
+ (cd sub &&
+ git checkout sub-b) &&
+ git commit -a -m "bb" &&
+
+ git checkout -b e bb &&
+ (cd sub &&
+ git checkout sub-a) &&
+ git commit -a -m "e" &&
+
+ git checkout -b f bb &&
+ (cd sub &&
+ git checkout sub-d) &&
+ git commit -a -m "f" &&
+
+ git checkout -b test-backward e &&
+ test_must_fail git merge f >actual &&
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ sub_expect="go to submodule (sub), and either merge commit $(git -C sub rev-parse --short sub-d)" &&
+ grep "$sub_expect" actual
+ fi)
+'
+
+
+# Check that the conflicting submodule is detected when it is
+# in the common ancestor. status should be 'U00...00"
+test_expect_success 'git submodule status should display the merge conflict properly with merge base' '
+ (cd merge-search &&
+ cat >.gitmodules <<EOF &&
+[submodule "sub"]
+ path = sub
+ url = $TRASH_DIRECTORY/sub
+EOF
+ cat >expect <<EOF &&
+U$ZERO_OID sub
+EOF
+ git submodule status > actual &&
+ test_cmp expect actual &&
+ git reset --hard)
+'
+
+# Check that the conflicting submodule is detected when it is
+# not in the common ancestor. status should be 'U00...00"
+test_expect_success 'git submodule status should display the merge conflict properly without merge-base' '
+ (cd merge-search &&
+ git checkout -b test-no-merge-base g &&
+ test_must_fail git merge b &&
+ cat >.gitmodules <<EOF &&
+[submodule "sub"]
+ path = sub
+ url = $TRASH_DIRECTORY/sub
+EOF
+ cat >expect <<EOF &&
+U$ZERO_OID sub
+EOF
+ git submodule status > actual &&
+ test_cmp expect actual &&
+ git reset --hard)
+'
+
+
+test_expect_success 'merging with a modify/modify conflict between merge bases' '
+ git reset --hard HEAD &&
+ git checkout -b test2 c &&
+ git merge d
+'
+
+# canonical criss-cross history in top and submodule
+test_expect_success 'setup for recursive merge with submodule' '
+ mkdir merge-recursive &&
+ (cd merge-recursive &&
+ git init &&
+ mkdir sub &&
+ (cd sub &&
+ git init &&
+ test_commit a &&
+ git checkout -b sub-b main &&
+ test_commit b &&
+ git checkout -b sub-c main &&
+ test_commit c &&
+ git checkout -b sub-bc sub-b &&
+ git merge sub-c &&
+ git checkout -b sub-cb sub-c &&
+ git merge sub-b &&
+ git checkout main) &&
+ git add sub &&
+ git commit -m a &&
+ git checkout -b top-b main &&
+ (cd sub && git checkout sub-b) &&
+ git add sub &&
+ git commit -m b &&
+ git checkout -b top-c main &&
+ (cd sub && git checkout sub-c) &&
+ git add sub &&
+ git commit -m c &&
+ git checkout -b top-bc top-b &&
+ git merge -s ours --no-commit top-c &&
+ (cd sub && git checkout sub-bc) &&
+ git add sub &&
+ git commit -m bc &&
+ git checkout -b top-cb top-c &&
+ git merge -s ours --no-commit top-b &&
+ (cd sub && git checkout sub-cb) &&
+ git add sub &&
+ git commit -m cb)
+'
+
+# merge should leave submodule unmerged in index
+test_expect_success 'recursive merge with submodule' '
+ (cd merge-recursive &&
+ test_must_fail git merge top-bc &&
+ echo "160000 $(git rev-parse top-cb:sub) 2 sub" > expect2 &&
+ echo "160000 $(git rev-parse top-bc:sub) 3 sub" > expect3 &&
+ git ls-files -u > actual &&
+ grep "$(cat expect2)" actual > /dev/null &&
+ grep "$(cat expect3)" actual > /dev/null)
+'
+
+# File/submodule conflict
+# Commit O: <empty>
+# Commit A: path (submodule)
+# Commit B: path
+# Expected: path/ is submodule and file contents for B's path are somewhere
+
+test_expect_success 'setup file/submodule conflict' '
+ git init file-submodule &&
+ (
+ cd file-submodule &&
+
+ git commit --allow-empty -m O &&
+
+ git branch A &&
+ git branch B &&
+
+ git checkout B &&
+ echo content >path &&
+ git add path &&
+ git commit -m B &&
+
+ git checkout A &&
+ git init path &&
+ test_commit -C path world &&
+ git submodule add ./path &&
+ git commit -m A
+ )
+'
+
+test_expect_merge_algorithm failure success 'file/submodule conflict' '
+ test_when_finished "git -C file-submodule reset --hard" &&
+ (
+ cd file-submodule &&
+
+ git checkout A^0 &&
+ test_must_fail git merge B^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 3 out &&
+ git ls-files -u >out &&
+ test_line_count = 2 out &&
+
+ # path/ is still a submodule
+ test_path_is_dir path/.git &&
+
+ # There is a submodule at "path", so B:path cannot be written
+ # there. We expect it to be written somewhere in the same
+ # directory, though, so just grep for its content in all
+ # files, and ignore "grep: path: Is a directory" message
+ echo Checking if contents from B:path showed up anywhere &&
+ grep -q content * 2>/dev/null
+ )
+'
+
+test_expect_success 'file/submodule conflict; merge --abort works afterward' '
+ test_when_finished "git -C file-submodule reset --hard" &&
+ (
+ cd file-submodule &&
+
+ git checkout A^0 &&
+ test_must_fail git merge B^0 >out 2>err &&
+
+ test_path_is_file .git/MERGE_HEAD &&
+ git merge --abort
+ )
+'
+
+# Directory/submodule conflict
+# Commit O: <empty>
+# Commit A: path (submodule), with sole tracked file named 'world'
+# Commit B1: path/file
+# Commit B2: path/world
+#
+# Expected from merge of A & B1:
+# Contents under path/ from commit B1 are renamed elsewhere; we do not
+# want to write files from one of our tracked directories into a submodule
+#
+# Expected from merge of A & B2:
+# Similar to last merge, but with a slight twist: we don't want paths
+# under the submodule to be treated as untracked or in the way.
+
+test_expect_success 'setup directory/submodule conflict' '
+ git init directory-submodule &&
+ (
+ cd directory-submodule &&
+
+ git commit --allow-empty -m O &&
+
+ git branch A &&
+ git branch B1 &&
+ git branch B2 &&
+
+ git checkout B1 &&
+ mkdir path &&
+ echo contents >path/file &&
+ git add path/file &&
+ git commit -m B1 &&
+
+ git checkout B2 &&
+ mkdir path &&
+ echo contents >path/world &&
+ git add path/world &&
+ git commit -m B2 &&
+
+ git checkout A &&
+ git init path &&
+ test_commit -C path hello world &&
+ git submodule add ./path &&
+ git commit -m A
+ )
+'
+
+test_expect_failure 'directory/submodule conflict; keep submodule clean' '
+ test_when_finished "git -C directory-submodule reset --hard" &&
+ (
+ cd directory-submodule &&
+
+ git checkout A^0 &&
+ test_must_fail git merge B1^0 &&
+
+ git ls-files -s >out &&
+ test_line_count = 3 out &&
+ git ls-files -u >out &&
+ test_line_count = 1 out &&
+
+ # path/ is still a submodule
+ test_path_is_dir path/.git &&
+
+ echo Checking if contents from B1:path/file showed up &&
+ # Would rather use grep -r, but that is GNU extension...
+ git ls-files -co | xargs grep -q contents 2>/dev/null &&
+
+ # However, B1:path/file should NOT have shown up at path/file,
+ # because we should not write into the submodule
+ test_path_is_missing path/file
+ )
+'
+
+test_expect_merge_algorithm failure success !FAIL_PREREQS 'directory/submodule conflict; should not treat submodule files as untracked or in the way' '
+ test_when_finished "git -C directory-submodule/path reset --hard" &&
+ test_when_finished "git -C directory-submodule reset --hard" &&
+ (
+ cd directory-submodule &&
+
+ git checkout A^0 &&
+ test_must_fail git merge B2^0 >out 2>err &&
+
+ # We do not want files within the submodule to prevent the
+ # merge from starting; we should not be writing to such paths
+ # anyway.
+ test_grep ! "refusing to lose untracked file at" err
+ )
+'
+
+test_expect_failure 'directory/submodule conflict; merge --abort works afterward' '
+ test_when_finished "git -C directory-submodule/path reset --hard" &&
+ test_when_finished "git -C directory-submodule reset --hard" &&
+ (
+ cd directory-submodule &&
+
+ git checkout A^0 &&
+ test_must_fail git merge B2^0 &&
+ test_path_is_file .git/MERGE_HEAD &&
+
+ # merge --abort should succeed, should clear .git/MERGE_HEAD,
+ # and should not leave behind any conflicted files
+ git merge --abort &&
+ test_path_is_missing .git/MERGE_HEAD &&
+ git ls-files -u >conflicts &&
+ test_must_be_empty conflicts
+ )
+'
+
+# Setup:
+# - Submodule has 2 commits: a and b
+# - Superproject branch 'a' adds and commits submodule pointing to 'commit a'
+# - Superproject branch 'b' adds and commits submodule pointing to 'commit b'
+# If these two branches are now merged, there is no merge base
+test_expect_success 'setup for null merge base' '
+ mkdir no-merge-base &&
+ (cd no-merge-base &&
+ git init &&
+ mkdir sub &&
+ (cd sub &&
+ git init &&
+ echo "file-a" > file-a &&
+ git add file-a &&
+ git commit -m "commit a") &&
+ git commit --allow-empty -m init &&
+ git branch init &&
+ git checkout -b a init &&
+ git add sub &&
+ git commit -m "a" &&
+ git switch main &&
+ (cd sub &&
+ echo "file-b" > file-b &&
+ git add file-b &&
+ git commit -m "commit b"))
+'
+
+test_expect_success 'merging should fail with no merge base' '
+ (cd no-merge-base &&
+ git checkout -b b init &&
+ git add sub &&
+ git commit -m "b" &&
+ test_must_fail git merge a >actual &&
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ sub_expect="go to submodule (sub), and either merge commit $(git -C sub rev-parse --short HEAD^1)" &&
+ grep "$sub_expect" actual
+ fi)
+'
+
+test_done
diff --git a/t/t6438-submodule-directory-file-conflicts.sh b/t/t6438-submodule-directory-file-conflicts.sh
new file mode 100755
index 0000000..8df67a0
--- /dev/null
+++ b/t/t6438-submodule-directory-file-conflicts.sh
@@ -0,0 +1,22 @@
+#!/bin/sh
+
+test_description='merge can handle submodules'
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-submodule-update.sh
+
+# merges without conflicts
+test_submodule_switch "merge"
+
+test_submodule_switch "merge --ff"
+
+test_submodule_switch "merge --ff-only"
+
+if test "$GIT_TEST_MERGE_ALGORITHM" != ort
+then
+ KNOWN_FAILURE_NOFF_MERGE_DOESNT_CREATE_EMPTY_SUBMODULE_DIR=1
+ KNOWN_FAILURE_NOFF_MERGE_ATTEMPTS_TO_MERGE_REMOVED_SUBMODULE_FILES=1
+fi
+test_submodule_switch "merge --no-ff"
+
+test_done
diff --git a/t/t6439-merge-co-error-msgs.sh b/t/t6439-merge-co-error-msgs.sh
new file mode 100755
index 0000000..0cbec57
--- /dev/null
+++ b/t/t6439-merge-co-error-msgs.sh
@@ -0,0 +1,143 @@
+#!/bin/sh
+
+test_description='unpack-trees error messages'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+
+test_expect_success 'setup' '
+ echo one >one &&
+ git add one &&
+ git commit -a -m First &&
+
+ git checkout -b branch &&
+ echo two >two &&
+ echo three >three &&
+ echo four >four &&
+ echo five >five &&
+ git add two three four five &&
+ git commit -m Second &&
+
+ git checkout main &&
+ echo other >two &&
+ echo other >three &&
+ echo other >four &&
+ echo other >five
+'
+
+cat >expect <<\EOF
+error: The following untracked working tree files would be overwritten by merge:
+ five
+ four
+ three
+ two
+Please move or remove them before you merge.
+Aborting
+EOF
+
+test_expect_success 'untracked files overwritten by merge (fast and non-fast forward)' '
+ test_must_fail git merge branch 2>out &&
+ test_cmp out expect &&
+ git commit --allow-empty -m empty &&
+ (
+ GIT_MERGE_VERBOSITY=0 &&
+ export GIT_MERGE_VERBOSITY &&
+ test_must_fail git merge branch 2>out2
+ ) &&
+ echo "Merge with strategy ${GIT_TEST_MERGE_ALGORITHM:-ort} failed." >>expect &&
+ test_cmp out2 expect &&
+ git reset --hard HEAD^
+'
+
+cat >expect <<\EOF
+error: Your local changes to the following files would be overwritten by merge:
+ four
+ three
+ two
+Please commit your changes or stash them before you merge.
+error: The following untracked working tree files would be overwritten by merge:
+ five
+Please move or remove them before you merge.
+Aborting
+EOF
+
+test_expect_success 'untracked files or local changes ovewritten by merge' '
+ git add two &&
+ git add three &&
+ git add four &&
+ test_must_fail git merge branch 2>out &&
+ test_cmp out expect
+'
+
+cat >expect <<\EOF
+error: Your local changes to the following files would be overwritten by checkout:
+ rep/one
+ rep/two
+Please commit your changes or stash them before you switch branches.
+Aborting
+EOF
+
+test_expect_success 'cannot switch branches because of local changes' '
+ git add five &&
+ mkdir rep &&
+ echo one >rep/one &&
+ echo two >rep/two &&
+ git add rep/one rep/two &&
+ git commit -m Fourth &&
+ git checkout main &&
+ echo uno >rep/one &&
+ echo dos >rep/two &&
+ test_must_fail git checkout branch 2>out &&
+ test_cmp out expect
+'
+
+cat >expect <<\EOF
+error: Your local changes to the following files would be overwritten by checkout:
+ rep/one
+ rep/two
+Please commit your changes or stash them before you switch branches.
+Aborting
+EOF
+
+test_expect_success 'not uptodate file porcelain checkout error' '
+ git add rep/one rep/two &&
+ test_must_fail git checkout branch 2>out &&
+ test_cmp out expect
+'
+
+cat >expect <<\EOF
+error: Updating the following directories would lose untracked files in them:
+ rep
+ rep2
+
+Aborting
+EOF
+
+test_expect_success 'not_uptodate_dir porcelain checkout error' '
+ git init uptodate &&
+ cd uptodate &&
+ mkdir rep &&
+ mkdir rep2 &&
+ touch rep/foo &&
+ touch rep2/foo &&
+ git add rep/foo rep2/foo &&
+ git commit -m init &&
+ git checkout -b branch &&
+ git rm rep -r &&
+ git rm rep2 -r &&
+ >rep &&
+ >rep2 &&
+ git add rep rep2 &&
+ git commit -m "added test as a file" &&
+ git checkout main &&
+ >rep/untracked-file &&
+ >rep2/untracked-file &&
+ test_must_fail git checkout branch 2>out &&
+ test_cmp out ../expect
+'
+
+test_done
diff --git a/t/t6500-gc.sh b/t/t6500-gc.sh
new file mode 100755
index 0000000..18fe1c2
--- /dev/null
+++ b/t/t6500-gc.sh
@@ -0,0 +1,419 @@
+#!/bin/sh
+
+test_description='basic git gc tests
+'
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-terminal.sh
+
+test_expect_success 'setup' '
+ # do not let the amount of physical memory affects gc
+ # behavior, make sure we always pack everything to one pack by
+ # default
+ git config gc.bigPackThreshold 2g &&
+
+ # These are simply values which, when hashed as a blob with a newline,
+ # produce a hash where the first byte is 0x17 in their respective
+ # algorithms.
+ test_oid_cache <<-EOF
+ obj1 sha1:263
+ obj1 sha256:34
+
+ obj2 sha1:410
+ obj2 sha256:174
+
+ obj3 sha1:523
+ obj3 sha256:313
+
+ obj4 sha1:790
+ obj4 sha256:481
+ EOF
+'
+
+test_expect_success 'gc empty repository' '
+ git gc
+'
+
+test_expect_success 'gc does not leave behind pid file' '
+ git gc &&
+ test_path_is_missing .git/gc.pid
+'
+
+test_expect_success 'gc --gobbledegook' '
+ test_expect_code 129 git gc --nonsense 2>err &&
+ test_grep "[Uu]sage: git gc" err
+'
+
+test_expect_success 'gc -h with invalid configuration' '
+ mkdir broken &&
+ (
+ cd broken &&
+ git init &&
+ echo "[gc] pruneexpire = CORRUPT" >>.git/config &&
+ test_expect_code 129 git gc -h >usage 2>&1
+ ) &&
+ test_grep "[Uu]sage" broken/usage
+'
+
+test_expect_success 'gc is not aborted due to a stale symref' '
+ git init remote &&
+ (
+ cd remote &&
+ test_commit initial &&
+ git clone . ../client &&
+ git branch -m develop &&
+ cd ../client &&
+ git fetch --prune &&
+ git gc
+ )
+'
+
+test_expect_success 'gc --keep-largest-pack' '
+ test_create_repo keep-pack &&
+ (
+ cd keep-pack &&
+ test_commit one &&
+ test_commit two &&
+ test_commit three &&
+ git gc &&
+ ( cd .git/objects/pack && ls *.pack ) >pack-list &&
+ test_line_count = 1 pack-list &&
+ cp pack-list base-pack-list &&
+ test_commit four &&
+ git repack -d &&
+ test_commit five &&
+ git repack -d &&
+ ( cd .git/objects/pack && ls *.pack ) >pack-list &&
+ test_line_count = 3 pack-list &&
+ git gc --keep-largest-pack &&
+ ( cd .git/objects/pack && ls *.pack ) >pack-list &&
+ test_line_count = 2 pack-list &&
+ awk "/^P /{print \$2}" <.git/objects/info/packs >pack-info &&
+ test_line_count = 2 pack-info &&
+ test_path_is_file .git/objects/pack/$(cat base-pack-list) &&
+ git fsck
+ )
+'
+
+test_expect_success 'pre-auto-gc hook can stop auto gc' '
+ cat >err.expect <<-\EOF &&
+ no gc for you
+ EOF
+
+ git init pre-auto-gc-hook &&
+ test_hook -C pre-auto-gc-hook pre-auto-gc <<-\EOF &&
+ echo >&2 no gc for you &&
+ exit 1
+ EOF
+ (
+ cd pre-auto-gc-hook &&
+
+ git config gc.auto 3 &&
+ git config gc.autoDetach false &&
+
+ # We need to create two object whose sha1s start with 17
+ # since this is what git gc counts. As it happens, these
+ # two blobs will do so.
+ test_commit "$(test_oid obj1)" &&
+ test_commit "$(test_oid obj2)" &&
+
+ git gc --auto >../out.actual 2>../err.actual
+ ) &&
+ test_must_be_empty out.actual &&
+ test_cmp err.expect err.actual &&
+
+ cat >err.expect <<-\EOF &&
+ will gc for you
+ Auto packing the repository for optimum performance.
+ See "git help gc" for manual housekeeping.
+ EOF
+
+ test_hook -C pre-auto-gc-hook --clobber pre-auto-gc <<-\EOF &&
+ echo >&2 will gc for you &&
+ exit 0
+ EOF
+
+ git -C pre-auto-gc-hook gc --auto >out.actual 2>err.actual &&
+
+ test_must_be_empty out.actual &&
+ test_cmp err.expect err.actual
+'
+
+test_expect_success 'auto gc with too many loose objects does not attempt to create bitmaps' '
+ test_config gc.auto 3 &&
+ test_config gc.autodetach false &&
+ test_config pack.writebitmaps true &&
+ # We need to create two object whose sha1s start with 17
+ # since this is what git gc counts. As it happens, these
+ # two blobs will do so.
+ test_commit "$(test_oid obj1)" &&
+ test_commit "$(test_oid obj2)" &&
+ # Our first gc will create a pack; our second will create a second pack
+ git gc --auto &&
+ ls .git/objects/pack/pack-*.pack | sort >existing_packs &&
+ test_commit "$(test_oid obj3)" &&
+ test_commit "$(test_oid obj4)" &&
+
+ git gc --auto 2>err &&
+ test_grep ! "^warning:" err &&
+ ls .git/objects/pack/pack-*.pack | sort >post_packs &&
+ comm -1 -3 existing_packs post_packs >new &&
+ comm -2 -3 existing_packs post_packs >del &&
+ test_line_count = 0 del && # No packs are deleted
+ test_line_count = 1 new # There is one new pack
+'
+
+test_expect_success 'gc --no-quiet' '
+ GIT_PROGRESS_DELAY=0 git -c gc.writeCommitGraph=true gc --no-quiet >stdout 2>stderr &&
+ test_must_be_empty stdout &&
+ test_grep "Computing commit graph generation numbers" stderr
+'
+
+test_expect_success TTY 'with TTY: gc --no-quiet' '
+ test_terminal env GIT_PROGRESS_DELAY=0 \
+ git -c gc.writeCommitGraph=true gc --no-quiet >stdout 2>stderr &&
+ test_must_be_empty stdout &&
+ test_grep "Enumerating objects" stderr &&
+ test_grep "Computing commit graph generation numbers" stderr
+'
+
+test_expect_success 'gc --quiet' '
+ git -c gc.writeCommitGraph=true gc --quiet >stdout 2>stderr &&
+ test_must_be_empty stdout &&
+ test_must_be_empty stderr
+'
+
+test_expect_success 'gc.reflogExpire{Unreachable,}=never skips "expire" via "gc"' '
+ test_config gc.reflogExpire never &&
+ test_config gc.reflogExpireUnreachable never &&
+
+ GIT_TRACE=$(pwd)/trace.out git gc &&
+
+ # Check that git-pack-refs is run as a sanity check (done via
+ # gc_before_repack()) but that git-expire is not.
+ grep -E "^trace: (built-in|exec|run_command): git pack-refs --" trace.out &&
+ ! grep -E "^trace: (built-in|exec|run_command): git reflog expire --" trace.out
+'
+
+test_expect_success 'one of gc.reflogExpire{Unreachable,}=never does not skip "expire" via "gc"' '
+ >trace.out &&
+ test_config gc.reflogExpire never &&
+ GIT_TRACE=$(pwd)/trace.out git gc &&
+ grep -E "^trace: (built-in|exec|run_command): git reflog expire --" trace.out
+'
+
+test_expect_success 'gc.repackFilter launches repack with a filter' '
+ git clone --no-local --bare . bare.git &&
+
+ git -C bare.git -c gc.cruftPacks=false gc &&
+ test_stdout_line_count = 1 ls bare.git/objects/pack/*.pack &&
+
+ GIT_TRACE=$(pwd)/trace.out git -C bare.git -c gc.repackFilter=blob:none \
+ -c repack.writeBitmaps=false -c gc.cruftPacks=false gc &&
+ test_stdout_line_count = 2 ls bare.git/objects/pack/*.pack &&
+ grep -E "^trace: (built-in|exec|run_command): git repack .* --filter=blob:none ?.*" trace.out
+'
+
+test_expect_success 'gc.repackFilterTo store filtered out objects' '
+ test_when_finished "rm -rf bare.git filtered.git" &&
+
+ git init --bare filtered.git &&
+ git -C bare.git -c gc.repackFilter=blob:none \
+ -c gc.repackFilterTo=../filtered.git/objects/pack/pack \
+ -c repack.writeBitmaps=false -c gc.cruftPacks=false gc &&
+
+ test_stdout_line_count = 1 ls bare.git/objects/pack/*.pack &&
+ test_stdout_line_count = 1 ls filtered.git/objects/pack/*.pack
+'
+
+prepare_cruft_history () {
+ test_commit base &&
+
+ test_commit --no-tag foo &&
+ test_commit --no-tag bar &&
+ git reset HEAD^^
+}
+
+assert_no_cruft_packs () {
+ find .git/objects/pack -name "*.mtimes" >mtimes &&
+ test_must_be_empty mtimes
+}
+
+for argv in \
+ "gc" \
+ "-c gc.cruftPacks=true gc" \
+ "-c gc.cruftPacks=false gc --cruft"
+do
+ test_expect_success "git $argv generates a cruft pack" '
+ test_when_finished "rm -fr repo" &&
+ git init repo &&
+ (
+ cd repo &&
+
+ prepare_cruft_history &&
+ git $argv &&
+
+ find .git/objects/pack -name "*.mtimes" >mtimes &&
+ sed -e 's/\.mtimes$/\.pack/g' mtimes >packs &&
+
+ test_file_not_empty packs &&
+ while read pack
+ do
+ test_path_is_file "$pack" || return 1
+ done <packs
+ )
+ '
+done
+
+for argv in \
+ "gc --no-cruft" \
+ "-c gc.cruftPacks=false gc" \
+ "-c gc.cruftPacks=true gc --no-cruft"
+do
+ test_expect_success "git $argv does not generate a cruft pack" '
+ test_when_finished "rm -fr repo" &&
+ git init repo &&
+ (
+ cd repo &&
+
+ prepare_cruft_history &&
+ git $argv &&
+
+ assert_no_cruft_packs
+ )
+ '
+done
+
+test_expect_success '--keep-largest-pack ignores cruft packs' '
+ test_when_finished "rm -fr repo" &&
+ git init repo &&
+ (
+ cd repo &&
+
+ # Generate a pack for reachable objects (of which there
+ # are 3), and one for unreachable objects (of which
+ # there are 6).
+ prepare_cruft_history &&
+ git gc --cruft &&
+
+ mtimes="$(find .git/objects/pack -type f -name "pack-*.mtimes")" &&
+ sz="$(test_file_size "${mtimes%.mtimes}.pack")" &&
+
+ # Ensure that the cruft pack gets removed (due to
+ # `--prune=now`) despite it being the largest pack.
+ git -c gc.bigPackThreshold=$sz gc --cruft --prune=now &&
+
+ assert_no_cruft_packs
+ )
+'
+
+test_expect_success 'gc.bigPackThreshold ignores cruft packs' '
+ test_when_finished "rm -fr repo" &&
+ git init repo &&
+ (
+ cd repo &&
+
+ # Generate a pack for reachable objects (of which there
+ # are 3), and one for unreachable objects (of which
+ # there are 6).
+ prepare_cruft_history &&
+ git gc --cruft &&
+
+ # Ensure that the cruft pack gets removed (due to
+ # `--prune=now`) despite it being the largest pack.
+ git gc --cruft --prune=now --keep-largest-pack &&
+
+ assert_no_cruft_packs
+ )
+'
+
+cruft_max_size_opts="git repack -d -l --cruft --cruft-expiration=2.weeks.ago"
+
+test_expect_success 'setup for --max-cruft-size tests' '
+ git init cruft--max-size &&
+ (
+ cd cruft--max-size &&
+ prepare_cruft_history
+ )
+'
+
+test_expect_success '--max-cruft-size sets appropriate repack options' '
+ GIT_TRACE2_EVENT=$(pwd)/trace2.txt git -C cruft--max-size \
+ gc --cruft --max-cruft-size=1M &&
+ test_subcommand $cruft_max_size_opts --max-cruft-size=1048576 <trace2.txt
+'
+
+test_expect_success 'gc.maxCruftSize sets appropriate repack options' '
+ GIT_TRACE2_EVENT=$(pwd)/trace2.txt \
+ git -C cruft--max-size -c gc.maxCruftSize=2M gc --cruft &&
+ test_subcommand $cruft_max_size_opts --max-cruft-size=2097152 <trace2.txt &&
+
+ GIT_TRACE2_EVENT=$(pwd)/trace2.txt \
+ git -C cruft--max-size -c gc.maxCruftSize=2M gc --cruft \
+ --max-cruft-size=3M &&
+ test_subcommand $cruft_max_size_opts --max-cruft-size=3145728 <trace2.txt
+'
+
+run_and_wait_for_auto_gc () {
+ # We read stdout from gc for the side effect of waiting until the
+ # background gc process exits, closing its fd 9. Furthermore, the
+ # variable assignment from a command substitution preserves the
+ # exit status of the main gc process.
+ # Note: this fd trickery doesn't work on Windows, but there is no
+ # need to, because on Win the auto gc always runs in the foreground.
+ doesnt_matter=$(git gc --auto 9>&1)
+}
+
+test_expect_success 'background auto gc does not run if gc.log is present and recent but does if it is old' '
+ test_commit foo &&
+ test_commit bar &&
+ git repack &&
+ test_config gc.autopacklimit 1 &&
+ test_config gc.autodetach true &&
+ echo fleem >.git/gc.log &&
+ git gc --auto 2>err &&
+ test_grep "^warning:" err &&
+ test_config gc.logexpiry 5.days &&
+ test-tool chmtime =-345600 .git/gc.log &&
+ git gc --auto &&
+ test_config gc.logexpiry 2.days &&
+ run_and_wait_for_auto_gc &&
+ ls .git/objects/pack/pack-*.pack >packs &&
+ test_line_count = 1 packs
+'
+
+test_expect_success 'background auto gc respects lock for all operations' '
+ # make sure we run a background auto-gc
+ test_commit make-pack &&
+ git repack &&
+ test_config gc.autopacklimit 1 &&
+ test_config gc.autodetach true &&
+
+ # create a ref whose loose presence we can use to detect a pack-refs run
+ git update-ref refs/heads/should-be-loose HEAD &&
+ (ls -1 .git/refs/heads .git/reftable >expect || true) &&
+
+ # now fake a concurrent gc that holds the lock; we can use our
+ # shell pid so that it looks valid.
+ hostname=$(hostname || echo unknown) &&
+ shell_pid=$$ &&
+ if test_have_prereq MINGW && test -f /proc/$shell_pid/winpid
+ then
+ # In Git for Windows, Bash (actually, the MSYS2 runtime) has a
+ # different idea of PIDs than git.exe (actually Windows). Use
+ # the Windows PID in this case.
+ shell_pid=$(cat /proc/$shell_pid/winpid)
+ fi &&
+ printf "%d %s" "$shell_pid" "$hostname" >.git/gc.pid &&
+
+ # our gc should exit zero without doing anything
+ run_and_wait_for_auto_gc &&
+ (ls -1 .git/refs/heads .git/reftable >actual || true) &&
+ test_cmp expect actual
+'
+
+# DO NOT leave a detached auto gc process running near the end of the
+# test script: it can run long enough in the background to racily
+# interfere with the cleanup in 'test_done'.
+
+test_done
diff --git a/t/t6501-freshen-objects.sh b/t/t6501-freshen-objects.sh
new file mode 100755
index 0000000..4521508
--- /dev/null
+++ b/t/t6501-freshen-objects.sh
@@ -0,0 +1,184 @@
+#!/bin/sh
+#
+# This test covers the handling of objects which might have old
+# mtimes in the filesystem (because they were used previously)
+# and are just now becoming referenced again.
+#
+# We're going to do two things that are a little bit "fake" to
+# help make our simulation easier:
+#
+# 1. We'll turn off reflogs. You can still run into
+# problems with reflogs on, but your objects
+# don't get pruned until both the reflog expiration
+# has passed on their references, _and_ they are out
+# of prune's expiration period. Dropping reflogs
+# means we only have to deal with one variable in our tests,
+# but the results generalize.
+#
+# 2. We'll use a temporary index file to create our
+# works-in-progress. Most workflows would mention
+# referenced objects in the index, which prune takes
+# into account. However, many operations don't. For
+# example, a partial commit with "git commit foo"
+# will use a temporary index. Or they may not need
+# an index at all (e.g., creating a new commit
+# to refer to an existing tree).
+
+test_description='check pruning of dependent objects'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# We care about reachability, so we do not want to use
+# the normal test_commit, which creates extra tags.
+add () {
+ echo "$1" >"$1" &&
+ git add "$1"
+}
+commit () {
+ test_tick &&
+ add "$1" &&
+ git commit -m "$1"
+}
+
+maybe_repack () {
+ case "$title" in
+ loose)
+ : skip repack
+ ;;
+ repack)
+ git repack -ad
+ ;;
+ bitmap)
+ git repack -adb
+ ;;
+ *)
+ echo >&2 "unknown test type in maybe_repack"
+ return 1
+ ;;
+ esac
+}
+
+for title in loose repack bitmap
+do
+ test_expect_success "make repo completely empty ($title)" '
+ rm -rf .git &&
+ git init
+ '
+
+ test_expect_success "disable reflogs ($title)" '
+ git config core.logallrefupdates false &&
+ git reflog expire --expire=all --all
+ '
+
+ test_expect_success "setup basic history ($title)" '
+ commit base
+ '
+
+ test_expect_success "create and abandon some objects ($title)" '
+ git checkout -b experiment &&
+ commit abandon &&
+ maybe_repack &&
+ git checkout main &&
+ git branch -D experiment
+ '
+
+ test_expect_success "simulate time passing ($title)" '
+ test-tool chmtime --get -86400 $(find .git/objects -type f)
+ '
+
+ test_expect_success "start writing new commit with old blob ($title)" '
+ tree=$(
+ GIT_INDEX_FILE=index.tmp &&
+ export GIT_INDEX_FILE &&
+ git read-tree HEAD &&
+ add unrelated &&
+ add abandon &&
+ git write-tree
+ )
+ '
+
+ test_expect_success "simultaneous gc ($title)" '
+ git gc --no-cruft --prune=12.hours.ago
+ '
+
+ test_expect_success "finish writing out commit ($title)" '
+ commit=$(echo foo | git commit-tree -p HEAD $tree) &&
+ git update-ref HEAD $commit
+ '
+
+ # "abandon" blob should have been rescued by reference from new tree
+ test_expect_success "repository passes fsck ($title)" '
+ git fsck
+ '
+
+ test_expect_success "abandon objects again ($title)" '
+ git reset --hard HEAD^ &&
+ test-tool chmtime --get -86400 $(find .git/objects -type f)
+ '
+
+ test_expect_success "start writing new commit with same tree ($title)" '
+ tree=$(
+ GIT_INDEX_FILE=index.tmp &&
+ export GIT_INDEX_FILE &&
+ git read-tree HEAD &&
+ add abandon &&
+ add unrelated &&
+ git write-tree
+ )
+ '
+
+ test_expect_success "simultaneous gc ($title)" '
+ git gc --no-cruft --prune=12.hours.ago
+ '
+
+ # tree should have been refreshed by write-tree
+ test_expect_success "finish writing out commit ($title)" '
+ commit=$(echo foo | git commit-tree -p HEAD $tree) &&
+ git update-ref HEAD $commit
+ '
+done
+
+test_expect_success 'do not complain about existing broken links (commit)' '
+ cat >broken-commit <<-EOF &&
+ tree $(test_oid 001)
+ parent $(test_oid 002)
+ author whatever <whatever@example.com> 1234 -0000
+ committer whatever <whatever@example.com> 1234 -0000
+
+ some message
+ EOF
+ commit=$(git hash-object -t commit -w broken-commit) &&
+ git gc --no-cruft -q 2>stderr &&
+ git cat-file -e $commit &&
+ test_must_be_empty stderr
+'
+
+test_expect_success 'do not complain about existing broken links (tree)' '
+ cat >broken-tree <<-EOF &&
+ 100644 blob $(test_oid 003) foo
+ EOF
+ tree=$(git mktree --missing <broken-tree) &&
+ git gc --no-cruft -q 2>stderr &&
+ git cat-file -e $tree &&
+ test_must_be_empty stderr
+'
+
+test_expect_success 'do not complain about existing broken links (tag)' '
+ cat >broken-tag <<-EOF &&
+ object $(test_oid 004)
+ type commit
+ tag broken
+ tagger whatever <whatever@example.com> 1234 -0000
+
+ this is a broken tag
+ EOF
+ tag=$(git hash-object -t tag -w broken-tag) &&
+ git gc --no-cruft -q 2>stderr &&
+ git cat-file -e $tag &&
+ test_must_be_empty stderr
+'
+
+test_done
diff --git a/t/t6600-test-reach.sh b/t/t6600-test-reach.sh
new file mode 100755
index 0000000..b330945
--- /dev/null
+++ b/t/t6600-test-reach.sh
@@ -0,0 +1,615 @@
+#!/bin/sh
+
+test_description='basic commit reachability tests'
+
+. ./test-lib.sh
+
+# Construct a grid-like commit graph with points (x,y)
+# with 1 <= x <= 10, 1 <= y <= 10, where (x,y) has
+# parents (x-1, y) and (x, y-1), keeping in mind that
+# we drop a parent if a coordinate is nonpositive.
+#
+# (10,10)
+# / \
+# (10,9) (9,10)
+# / \ / \
+# (10,8) (9,9) (8,10)
+# / \ / \ / \
+# ( continued...)
+# \ / \ / \ /
+# (3,1) (2,2) (1,3)
+# \ / \ /
+# (2,1) (2,1)
+# \ /
+# (1,1)
+#
+# We use branch 'commit-x-y' to refer to (x,y).
+# This grid allows interesting reachability and
+# non-reachability queries: (x,y) can reach (x',y')
+# if and only if x' <= x and y' <= y.
+test_expect_success 'setup' '
+ for i in $(test_seq 1 10)
+ do
+ test_commit "1-$i" &&
+ git branch -f commit-1-$i &&
+ git tag -a -m "1-$i" tag-1-$i commit-1-$i || return 1
+ done &&
+ for j in $(test_seq 1 9)
+ do
+ git reset --hard commit-$j-1 &&
+ x=$(($j + 1)) &&
+ test_commit "$x-1" &&
+ git branch -f commit-$x-1 &&
+ git tag -a -m "$x-1" tag-$x-1 commit-$x-1 &&
+
+ for i in $(test_seq 2 10)
+ do
+ git merge commit-$j-$i -m "$x-$i" &&
+ git branch -f commit-$x-$i &&
+ git tag -a -m "$x-$i" tag-$x-$i commit-$x-$i || return 1
+ done
+ done &&
+ git commit-graph write --reachable &&
+ mv .git/objects/info/commit-graph commit-graph-full &&
+ chmod u+w commit-graph-full &&
+ git show-ref -s commit-5-5 | git commit-graph write --stdin-commits &&
+ mv .git/objects/info/commit-graph commit-graph-half &&
+ chmod u+w commit-graph-half &&
+ git -c commitGraph.generationVersion=1 commit-graph write --reachable &&
+ mv .git/objects/info/commit-graph commit-graph-no-gdat &&
+ chmod u+w commit-graph-no-gdat &&
+ git config core.commitGraph true
+'
+
+run_all_modes () {
+ test_when_finished rm -rf .git/objects/info/commit-graph &&
+ "$@" <input >actual &&
+ test_cmp expect actual &&
+ cp commit-graph-full .git/objects/info/commit-graph &&
+ "$@" <input >actual &&
+ test_cmp expect actual &&
+ cp commit-graph-half .git/objects/info/commit-graph &&
+ "$@" <input >actual &&
+ test_cmp expect actual &&
+ cp commit-graph-no-gdat .git/objects/info/commit-graph &&
+ "$@" <input >actual &&
+ test_cmp expect actual
+}
+
+test_all_modes () {
+ run_all_modes test-tool reach "$@"
+}
+
+test_expect_success 'ref_newer:miss' '
+ cat >input <<-\EOF &&
+ A:commit-5-7
+ B:commit-4-9
+ EOF
+ echo "ref_newer(A,B):0" >expect &&
+ test_all_modes ref_newer
+'
+
+test_expect_success 'ref_newer:hit' '
+ cat >input <<-\EOF &&
+ A:commit-5-7
+ B:commit-2-3
+ EOF
+ echo "ref_newer(A,B):1" >expect &&
+ test_all_modes ref_newer
+'
+
+test_expect_success 'in_merge_bases:hit' '
+ cat >input <<-\EOF &&
+ A:commit-5-7
+ B:commit-8-8
+ EOF
+ echo "in_merge_bases(A,B):1" >expect &&
+ test_all_modes in_merge_bases
+'
+
+test_expect_success 'in_merge_bases:miss' '
+ cat >input <<-\EOF &&
+ A:commit-6-8
+ B:commit-5-9
+ EOF
+ echo "in_merge_bases(A,B):0" >expect &&
+ test_all_modes in_merge_bases
+'
+
+test_expect_success 'in_merge_bases_many:hit' '
+ cat >input <<-\EOF &&
+ A:commit-6-8
+ X:commit-6-9
+ X:commit-5-7
+ EOF
+ echo "in_merge_bases_many(A,X):1" >expect &&
+ test_all_modes in_merge_bases_many
+'
+
+test_expect_success 'in_merge_bases_many:miss' '
+ cat >input <<-\EOF &&
+ A:commit-6-8
+ X:commit-7-7
+ X:commit-8-6
+ EOF
+ echo "in_merge_bases_many(A,X):0" >expect &&
+ test_all_modes in_merge_bases_many
+'
+
+test_expect_success 'in_merge_bases_many:miss-heuristic' '
+ cat >input <<-\EOF &&
+ A:commit-6-8
+ X:commit-7-5
+ X:commit-6-6
+ EOF
+ echo "in_merge_bases_many(A,X):0" >expect &&
+ test_all_modes in_merge_bases_many
+'
+
+test_expect_success 'is_descendant_of:hit' '
+ cat >input <<-\EOF &&
+ A:commit-5-7
+ X:commit-4-8
+ X:commit-6-6
+ X:commit-1-1
+ EOF
+ echo "is_descendant_of(A,X):1" >expect &&
+ test_all_modes is_descendant_of
+'
+
+test_expect_success 'is_descendant_of:miss' '
+ cat >input <<-\EOF &&
+ A:commit-6-8
+ X:commit-5-9
+ X:commit-4-10
+ X:commit-7-6
+ EOF
+ echo "is_descendant_of(A,X):0" >expect &&
+ test_all_modes is_descendant_of
+'
+
+test_expect_success 'get_merge_bases_many' '
+ cat >input <<-\EOF &&
+ A:commit-5-7
+ X:commit-4-8
+ X:commit-6-6
+ X:commit-8-3
+ EOF
+ {
+ echo "get_merge_bases_many(A,X):" &&
+ git rev-parse commit-5-6 \
+ commit-4-7 | sort
+ } >expect &&
+ test_all_modes get_merge_bases_many
+'
+
+test_expect_success 'reduce_heads' '
+ cat >input <<-\EOF &&
+ X:commit-1-10
+ X:commit-2-8
+ X:commit-3-6
+ X:commit-4-4
+ X:commit-1-7
+ X:commit-2-5
+ X:commit-3-3
+ X:commit-5-1
+ EOF
+ {
+ echo "reduce_heads(X):" &&
+ git rev-parse commit-5-1 \
+ commit-4-4 \
+ commit-3-6 \
+ commit-2-8 \
+ commit-1-10 | sort
+ } >expect &&
+ test_all_modes reduce_heads
+'
+
+test_expect_success 'can_all_from_reach:hit' '
+ cat >input <<-\EOF &&
+ X:commit-2-10
+ X:commit-3-9
+ X:commit-4-8
+ X:commit-5-7
+ X:commit-6-6
+ X:commit-7-5
+ X:commit-8-4
+ X:commit-9-3
+ Y:commit-1-9
+ Y:commit-2-8
+ Y:commit-3-7
+ Y:commit-4-6
+ Y:commit-5-5
+ Y:commit-6-4
+ Y:commit-7-3
+ Y:commit-8-1
+ EOF
+ echo "can_all_from_reach(X,Y):1" >expect &&
+ test_all_modes can_all_from_reach
+'
+
+test_expect_success 'can_all_from_reach:miss' '
+ cat >input <<-\EOF &&
+ X:commit-2-10
+ X:commit-3-9
+ X:commit-4-8
+ X:commit-5-7
+ X:commit-6-6
+ X:commit-7-5
+ X:commit-8-4
+ X:commit-9-3
+ Y:commit-1-9
+ Y:commit-2-8
+ Y:commit-3-7
+ Y:commit-4-6
+ Y:commit-5-5
+ Y:commit-6-4
+ Y:commit-8-5
+ EOF
+ echo "can_all_from_reach(X,Y):0" >expect &&
+ test_all_modes can_all_from_reach
+'
+
+test_expect_success 'can_all_from_reach_with_flag: tags case' '
+ cat >input <<-\EOF &&
+ X:tag-2-10
+ X:tag-3-9
+ X:tag-4-8
+ X:commit-5-7
+ X:commit-6-6
+ X:commit-7-5
+ X:commit-8-4
+ X:commit-9-3
+ Y:tag-1-9
+ Y:tag-2-8
+ Y:tag-3-7
+ Y:commit-4-6
+ Y:commit-5-5
+ Y:commit-6-4
+ Y:commit-7-3
+ Y:commit-8-1
+ EOF
+ echo "can_all_from_reach_with_flag(X,_,_,0,0):1" >expect &&
+ test_all_modes can_all_from_reach_with_flag
+'
+
+test_expect_success 'commit_contains:hit' '
+ cat >input <<-\EOF &&
+ A:commit-7-7
+ X:commit-2-10
+ X:commit-3-9
+ X:commit-4-8
+ X:commit-5-7
+ X:commit-6-6
+ X:commit-7-5
+ X:commit-8-4
+ X:commit-9-3
+ EOF
+ echo "commit_contains(_,A,X,_):1" >expect &&
+ test_all_modes commit_contains &&
+ test_all_modes commit_contains --tag
+'
+
+test_expect_success 'commit_contains:miss' '
+ cat >input <<-\EOF &&
+ A:commit-6-5
+ X:commit-2-10
+ X:commit-3-9
+ X:commit-4-8
+ X:commit-5-7
+ X:commit-6-6
+ X:commit-7-5
+ X:commit-8-4
+ X:commit-9-3
+ EOF
+ echo "commit_contains(_,A,X,_):0" >expect &&
+ test_all_modes commit_contains &&
+ test_all_modes commit_contains --tag
+'
+
+test_expect_success 'rev-list: basic topo-order' '
+ git rev-parse \
+ commit-6-6 commit-5-6 commit-4-6 commit-3-6 commit-2-6 commit-1-6 \
+ commit-6-5 commit-5-5 commit-4-5 commit-3-5 commit-2-5 commit-1-5 \
+ commit-6-4 commit-5-4 commit-4-4 commit-3-4 commit-2-4 commit-1-4 \
+ commit-6-3 commit-5-3 commit-4-3 commit-3-3 commit-2-3 commit-1-3 \
+ commit-6-2 commit-5-2 commit-4-2 commit-3-2 commit-2-2 commit-1-2 \
+ commit-6-1 commit-5-1 commit-4-1 commit-3-1 commit-2-1 commit-1-1 \
+ >expect &&
+ run_all_modes git rev-list --topo-order commit-6-6
+'
+
+test_expect_success 'rev-list: first-parent topo-order' '
+ git rev-parse \
+ commit-6-6 \
+ commit-6-5 \
+ commit-6-4 \
+ commit-6-3 \
+ commit-6-2 \
+ commit-6-1 commit-5-1 commit-4-1 commit-3-1 commit-2-1 commit-1-1 \
+ >expect &&
+ run_all_modes git rev-list --first-parent --topo-order commit-6-6
+'
+
+test_expect_success 'rev-list: range topo-order' '
+ git rev-parse \
+ commit-6-6 commit-5-6 commit-4-6 commit-3-6 commit-2-6 commit-1-6 \
+ commit-6-5 commit-5-5 commit-4-5 commit-3-5 commit-2-5 commit-1-5 \
+ commit-6-4 commit-5-4 commit-4-4 commit-3-4 commit-2-4 commit-1-4 \
+ commit-6-3 commit-5-3 commit-4-3 \
+ commit-6-2 commit-5-2 commit-4-2 \
+ commit-6-1 commit-5-1 commit-4-1 \
+ >expect &&
+ run_all_modes git rev-list --topo-order commit-3-3..commit-6-6
+'
+
+test_expect_success 'rev-list: range topo-order' '
+ git rev-parse \
+ commit-6-6 commit-5-6 commit-4-6 \
+ commit-6-5 commit-5-5 commit-4-5 \
+ commit-6-4 commit-5-4 commit-4-4 \
+ commit-6-3 commit-5-3 commit-4-3 \
+ commit-6-2 commit-5-2 commit-4-2 \
+ commit-6-1 commit-5-1 commit-4-1 \
+ >expect &&
+ run_all_modes git rev-list --topo-order commit-3-8..commit-6-6
+'
+
+test_expect_success 'rev-list: first-parent range topo-order' '
+ git rev-parse \
+ commit-6-6 \
+ commit-6-5 \
+ commit-6-4 \
+ commit-6-3 \
+ commit-6-2 \
+ commit-6-1 commit-5-1 commit-4-1 \
+ >expect &&
+ run_all_modes git rev-list --first-parent --topo-order commit-3-8..commit-6-6
+'
+
+test_expect_success 'rev-list: ancestry-path topo-order' '
+ git rev-parse \
+ commit-6-6 commit-5-6 commit-4-6 commit-3-6 \
+ commit-6-5 commit-5-5 commit-4-5 commit-3-5 \
+ commit-6-4 commit-5-4 commit-4-4 commit-3-4 \
+ commit-6-3 commit-5-3 commit-4-3 \
+ >expect &&
+ run_all_modes git rev-list --topo-order --ancestry-path commit-3-3..commit-6-6
+'
+
+test_expect_success 'rev-list: symmetric difference topo-order' '
+ git rev-parse \
+ commit-6-6 commit-5-6 commit-4-6 \
+ commit-6-5 commit-5-5 commit-4-5 \
+ commit-6-4 commit-5-4 commit-4-4 \
+ commit-6-3 commit-5-3 commit-4-3 \
+ commit-6-2 commit-5-2 commit-4-2 \
+ commit-6-1 commit-5-1 commit-4-1 \
+ commit-3-8 commit-2-8 commit-1-8 \
+ commit-3-7 commit-2-7 commit-1-7 \
+ >expect &&
+ run_all_modes git rev-list --topo-order commit-3-8...commit-6-6
+'
+
+test_expect_success 'get_reachable_subset:all' '
+ cat >input <<-\EOF &&
+ X:commit-9-1
+ X:commit-8-3
+ X:commit-7-5
+ X:commit-6-6
+ X:commit-1-7
+ Y:commit-3-3
+ Y:commit-1-7
+ Y:commit-5-6
+ EOF
+ (
+ echo "get_reachable_subset(X,Y)" &&
+ git rev-parse commit-3-3 \
+ commit-1-7 \
+ commit-5-6 | sort
+ ) >expect &&
+ test_all_modes get_reachable_subset
+'
+
+test_expect_success 'get_reachable_subset:some' '
+ cat >input <<-\EOF &&
+ X:commit-9-1
+ X:commit-8-3
+ X:commit-7-5
+ X:commit-1-7
+ Y:commit-3-3
+ Y:commit-1-7
+ Y:commit-5-6
+ EOF
+ (
+ echo "get_reachable_subset(X,Y)" &&
+ git rev-parse commit-3-3 \
+ commit-1-7 | sort
+ ) >expect &&
+ test_all_modes get_reachable_subset
+'
+
+test_expect_success 'get_reachable_subset:none' '
+ cat >input <<-\EOF &&
+ X:commit-9-1
+ X:commit-8-3
+ X:commit-7-5
+ X:commit-1-7
+ Y:commit-9-3
+ Y:commit-7-6
+ Y:commit-2-8
+ EOF
+ echo "get_reachable_subset(X,Y)" >expect &&
+ test_all_modes get_reachable_subset
+'
+
+test_expect_success 'for-each-ref ahead-behind:linear' '
+ cat >input <<-\EOF &&
+ refs/heads/commit-1-1
+ refs/heads/commit-1-3
+ refs/heads/commit-1-5
+ refs/heads/commit-1-8
+ EOF
+ cat >expect <<-\EOF &&
+ refs/heads/commit-1-1 0 8
+ refs/heads/commit-1-3 0 6
+ refs/heads/commit-1-5 0 4
+ refs/heads/commit-1-8 0 1
+ EOF
+ run_all_modes git for-each-ref \
+ --format="%(refname) %(ahead-behind:commit-1-9)" --stdin
+'
+
+test_expect_success 'for-each-ref ahead-behind:all' '
+ cat >input <<-\EOF &&
+ refs/heads/commit-1-1
+ refs/heads/commit-2-4
+ refs/heads/commit-4-2
+ refs/heads/commit-4-4
+ EOF
+ cat >expect <<-\EOF &&
+ refs/heads/commit-1-1 0 24
+ refs/heads/commit-2-4 0 17
+ refs/heads/commit-4-2 0 17
+ refs/heads/commit-4-4 0 9
+ EOF
+ run_all_modes git for-each-ref \
+ --format="%(refname) %(ahead-behind:commit-5-5)" --stdin
+'
+
+test_expect_success 'for-each-ref ahead-behind:some' '
+ cat >input <<-\EOF &&
+ refs/heads/commit-1-1
+ refs/heads/commit-5-3
+ refs/heads/commit-4-8
+ refs/heads/commit-9-9
+ EOF
+ cat >expect <<-\EOF &&
+ refs/heads/commit-1-1 0 53
+ refs/heads/commit-4-8 8 30
+ refs/heads/commit-5-3 0 39
+ refs/heads/commit-9-9 27 0
+ EOF
+ run_all_modes git for-each-ref \
+ --format="%(refname) %(ahead-behind:commit-9-6)" --stdin
+'
+
+test_expect_success 'for-each-ref ahead-behind:some, multibase' '
+ cat >input <<-\EOF &&
+ refs/heads/commit-1-1
+ refs/heads/commit-5-3
+ refs/heads/commit-7-8
+ refs/heads/commit-4-8
+ refs/heads/commit-9-9
+ EOF
+ cat >expect <<-\EOF &&
+ refs/heads/commit-1-1 0 53 0 53
+ refs/heads/commit-4-8 8 30 0 22
+ refs/heads/commit-5-3 0 39 0 39
+ refs/heads/commit-7-8 14 12 8 6
+ refs/heads/commit-9-9 27 0 27 0
+ EOF
+ run_all_modes git for-each-ref \
+ --format="%(refname) %(ahead-behind:commit-9-6) %(ahead-behind:commit-6-9)" \
+ --stdin
+'
+
+test_expect_success 'for-each-ref ahead-behind:none' '
+ cat >input <<-\EOF &&
+ refs/heads/commit-7-5
+ refs/heads/commit-4-8
+ refs/heads/commit-9-9
+ EOF
+ cat >expect <<-\EOF &&
+ refs/heads/commit-4-8 16 16
+ refs/heads/commit-7-5 7 4
+ refs/heads/commit-9-9 49 0
+ EOF
+ run_all_modes git for-each-ref \
+ --format="%(refname) %(ahead-behind:commit-8-4)" --stdin
+'
+
+test_expect_success 'for-each-ref merged:linear' '
+ cat >input <<-\EOF &&
+ refs/heads/commit-1-1
+ refs/heads/commit-1-3
+ refs/heads/commit-1-5
+ refs/heads/commit-1-8
+ refs/heads/commit-2-1
+ refs/heads/commit-5-1
+ refs/heads/commit-9-1
+ EOF
+ cat >expect <<-\EOF &&
+ refs/heads/commit-1-1
+ refs/heads/commit-1-3
+ refs/heads/commit-1-5
+ refs/heads/commit-1-8
+ EOF
+ run_all_modes git for-each-ref --merged=commit-1-9 \
+ --format="%(refname)" --stdin
+'
+
+test_expect_success 'for-each-ref merged:all' '
+ cat >input <<-\EOF &&
+ refs/heads/commit-1-1
+ refs/heads/commit-2-4
+ refs/heads/commit-4-2
+ refs/heads/commit-4-4
+ EOF
+ cat >expect <<-\EOF &&
+ refs/heads/commit-1-1
+ refs/heads/commit-2-4
+ refs/heads/commit-4-2
+ refs/heads/commit-4-4
+ EOF
+ run_all_modes git for-each-ref --merged=commit-5-5 \
+ --format="%(refname)" --stdin
+'
+
+test_expect_success 'for-each-ref ahead-behind:some' '
+ cat >input <<-\EOF &&
+ refs/heads/commit-1-1
+ refs/heads/commit-5-3
+ refs/heads/commit-4-8
+ refs/heads/commit-9-9
+ EOF
+ cat >expect <<-\EOF &&
+ refs/heads/commit-1-1
+ refs/heads/commit-5-3
+ EOF
+ run_all_modes git for-each-ref --merged=commit-9-6 \
+ --format="%(refname)" --stdin
+'
+
+test_expect_success 'for-each-ref merged:some, multibase' '
+ cat >input <<-\EOF &&
+ refs/heads/commit-1-1
+ refs/heads/commit-5-3
+ refs/heads/commit-7-8
+ refs/heads/commit-4-8
+ refs/heads/commit-9-9
+ EOF
+ cat >expect <<-\EOF &&
+ refs/heads/commit-1-1
+ refs/heads/commit-4-8
+ refs/heads/commit-5-3
+ EOF
+ run_all_modes git for-each-ref \
+ --merged=commit-5-8 \
+ --merged=commit-8-5 \
+ --format="%(refname)" \
+ --stdin
+'
+
+test_expect_success 'for-each-ref merged:none' '
+ cat >input <<-\EOF &&
+ refs/heads/commit-7-5
+ refs/heads/commit-4-8
+ refs/heads/commit-9-9
+ EOF
+ >expect &&
+ run_all_modes git for-each-ref --merged=commit-8-4 \
+ --format="%(refname)" --stdin
+'
+
+test_done
diff --git a/t/t6700-tree-depth.sh b/t/t6700-tree-depth.sh
new file mode 100755
index 0000000..9e70a7c
--- /dev/null
+++ b/t/t6700-tree-depth.sh
@@ -0,0 +1,95 @@
+#!/bin/sh
+
+test_description='handling of deep trees in various commands'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# We'll test against two depths here: a small one that will let us check the
+# behavior of the config setting easily, and a large one that should be
+# forbidden by default. Testing the default depth will let us know whether our
+# default is enough to prevent segfaults on systems that run the tests.
+small_depth=50
+big_depth=4100
+
+small_ok="-c core.maxtreedepth=$small_depth"
+small_no="-c core.maxtreedepth=$((small_depth-1))"
+
+# usage: mkdeep <name> <depth>
+# Create a tag <name> containing a file whose path has depth <depth>.
+#
+# We'll use fast-import here for two reasons:
+#
+# 1. It's faster than creating $big_depth tree objects.
+#
+# 2. As we tighten tree limits, it's more likely to allow large sizes
+# than trying to stuff a deep path into the index.
+mkdeep () {
+ {
+ echo "commit refs/tags/$1" &&
+ echo "committer foo <foo@example.com> 1234 -0000" &&
+ echo "data <<EOF" &&
+ echo "the commit message" &&
+ echo "EOF" &&
+
+ printf 'M 100644 inline ' &&
+ i=0 &&
+ while test $i -lt $2
+ do
+ printf 'a/'
+ i=$((i+1))
+ done &&
+ echo "file" &&
+
+ echo "data <<EOF" &&
+ echo "the file contents" &&
+ echo "EOF" &&
+ echo
+ } | git fast-import
+}
+
+test_expect_success 'create small tree' '
+ mkdeep small $small_depth
+'
+
+test_expect_success 'create big tree' '
+ mkdeep big $big_depth
+'
+
+test_expect_success 'limit recursion of git-archive' '
+ git $small_ok archive small >/dev/null &&
+ test_must_fail git $small_no archive small >/dev/null
+'
+
+test_expect_success 'default limit for git-archive fails gracefully' '
+ test_must_fail git archive big >/dev/null
+'
+
+test_expect_success 'limit recursion of ls-tree -r' '
+ git $small_ok ls-tree -r small &&
+ test_must_fail git $small_no ls-tree -r small
+'
+
+test_expect_success 'default limit for ls-tree fails gracefully' '
+ test_must_fail git ls-tree -r big >/dev/null
+'
+
+test_expect_success 'limit recursion of rev-list --objects' '
+ git $small_ok rev-list --objects small >/dev/null &&
+ test_must_fail git $small_no rev-list --objects small >/dev/null
+'
+
+test_expect_success 'default limit for rev-list fails gracefully' '
+ test_must_fail git rev-list --objects big >/dev/null
+'
+
+test_expect_success 'limit recursion of diff-tree -r' '
+ git $small_ok diff-tree -r $EMPTY_TREE small &&
+ test_must_fail git $small_no diff-tree -r $EMPTY_TREE small
+'
+
+test_expect_success 'default limit for diff-tree fails gracefully' '
+ test_must_fail git diff-tree -r $EMPTY_TREE big
+'
+
+test_done
diff --git a/t/t7001-mv.sh b/t/t7001-mv.sh
new file mode 100755
index 0000000..879a6dc
--- /dev/null
+++ b/t/t7001-mv.sh
@@ -0,0 +1,552 @@
+#!/bin/sh
+
+test_description='git mv in subdirs'
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-diff-data.sh
+
+index_at_path () {
+ git ls-files --format='%(objectmode) %(objectname) %(stage)' "$@"
+}
+
+test_expect_success 'mv -f refreshes updated index entry' '
+ echo test >bar &&
+ git add bar &&
+ git commit -m test &&
+
+ echo foo >foo &&
+ git add foo &&
+
+ # Wait one second to ensure ctime of rename will differ from original
+ # file creation ctime.
+ sleep 1 &&
+ git mv -f foo bar &&
+ git reset --merge HEAD &&
+
+ # Verify the index has been reset
+ git diff-files >out &&
+ test_must_be_empty out
+'
+
+test_expect_success 'prepare reference tree' '
+ mkdir path0 path1 &&
+ COPYING_test_data >path0/COPYING &&
+ git add path0/COPYING &&
+ git commit -m add -a
+'
+
+test_expect_success 'moving the file out of subdirectory' '
+ git -C path0 mv COPYING ../path1/COPYING
+'
+
+# in path0 currently
+test_expect_success 'commiting the change' '
+ git commit -m move-out -a
+'
+
+test_expect_success 'checking the commit' '
+ git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
+ grep "^R100..*path0/COPYING..*path1/COPYING" actual
+'
+
+test_expect_success 'moving the file back into subdirectory' '
+ git -C path0 mv ../path1/COPYING COPYING
+'
+
+# in path0 currently
+test_expect_success 'commiting the change' '
+ git commit -m move-in -a
+'
+
+test_expect_success 'checking the commit' '
+ git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
+ grep "^R100..*path1/COPYING..*path0/COPYING" actual
+'
+
+test_expect_success 'mv --dry-run does not move file' '
+ git mv -n path0/COPYING MOVED &&
+ test_path_is_file path0/COPYING &&
+ test_path_is_missing MOVED
+'
+
+test_expect_success 'checking -k on non-existing file' '
+ git mv -k idontexist path0
+'
+
+test_expect_success 'checking -k on untracked file' '
+ >untracked1 &&
+ git mv -k untracked1 path0 &&
+ test_path_is_file untracked1 &&
+ test_path_is_missing path0/untracked1
+'
+
+test_expect_success 'checking -k on multiple untracked files' '
+ >untracked2 &&
+ git mv -k untracked1 untracked2 path0 &&
+ test_path_is_file untracked1 &&
+ test_path_is_file untracked2 &&
+ test_path_is_missing path0/untracked1 &&
+ test_path_is_missing path0/untracked2
+'
+
+test_expect_success 'checking -f on untracked file with existing target' '
+ >path0/untracked1 &&
+ test_must_fail git mv -f untracked1 path0 &&
+ test_path_is_missing .git/index.lock &&
+ test_path_is_file untracked1 &&
+ test_path_is_file path0/untracked1
+'
+
+# clean up the mess in case bad things happen
+rm -f idontexist untracked1 untracked2 \
+ path0/idontexist path0/untracked1 path0/untracked2 \
+ .git/index.lock
+rmdir path1
+
+test_expect_success 'moving to absent target with trailing slash' '
+ test_must_fail git mv path0/COPYING no-such-dir/ &&
+ test_must_fail git mv path0/COPYING no-such-dir// &&
+ git mv path0/ no-such-dir/ &&
+ test_path_is_dir no-such-dir
+'
+
+test_expect_success 'clean up' '
+ git reset --hard
+'
+
+test_expect_success 'moving to existing untracked target with trailing slash' '
+ mkdir path1 &&
+ git mv path0/ path1/ &&
+ test_path_is_dir path1/path0/
+'
+
+test_expect_success 'moving to existing tracked target with trailing slash' '
+ mkdir path2 &&
+ >path2/file && git add path2/file &&
+ git mv path1/path0/ path2/ &&
+ test_path_is_dir path2/path0/
+'
+
+test_expect_success 'clean up' '
+ git reset --hard
+'
+
+test_expect_success 'adding another file' '
+ COPYING_test_data | tr A-Za-z N-ZA-Mn-za-m >path0/README &&
+ git add path0/README &&
+ git commit -m add2 -a
+'
+
+test_expect_success 'moving whole subdirectory' '
+ git mv path0 path2
+'
+
+test_expect_success 'commiting the change' '
+ git commit -m dir-move -a
+'
+
+test_expect_success 'checking the commit' '
+ git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
+ grep "^R100..*path0/COPYING..*path2/COPYING" actual &&
+ grep "^R100..*path0/README..*path2/README" actual
+'
+
+test_expect_success 'succeed when source is a prefix of destination' '
+ git mv path2/COPYING path2/COPYING-renamed
+'
+
+test_expect_success 'moving whole subdirectory into subdirectory' '
+ git mv path2 path1
+'
+
+test_expect_success 'commiting the change' '
+ git commit -m dir-move -a
+'
+
+test_expect_success 'checking the commit' '
+ git diff-tree -r -M --name-status HEAD^ HEAD >actual &&
+ grep "^R100..*path2/COPYING..*path1/path2/COPYING" actual &&
+ grep "^R100..*path2/README..*path1/path2/README" actual
+'
+
+test_expect_success 'do not move directory over existing directory' '
+ mkdir path0 &&
+ mkdir path0/path2 &&
+ test_must_fail git mv path2 path0
+'
+
+test_expect_success 'rename directory to non-existing directory' '
+ mkdir dir-a &&
+ >dir-a/f &&
+ git add dir-a &&
+ git mv dir-a non-existing-dir
+'
+
+test_expect_success 'move into "."' '
+ git mv path1/path2/ .
+'
+
+test_expect_success "Michael Cassar's test case" '
+ rm -fr .git papers partA &&
+ git init &&
+ mkdir -p papers/unsorted papers/all-papers partA &&
+ echo a >papers/unsorted/Thesis.pdf &&
+ echo b >partA/outline.txt &&
+ echo c >papers/unsorted/_another &&
+ git add papers partA &&
+ T1=$(git write-tree) &&
+
+ git mv papers/unsorted/Thesis.pdf papers/all-papers/moo-blah.pdf &&
+
+ T=$(git write-tree) &&
+ git ls-tree -r $T >out &&
+ grep partA/outline.txt out
+'
+
+rm -fr papers partA path?
+
+test_expect_success "Sergey Vlasov's test case" '
+ rm -fr .git &&
+ git init &&
+ mkdir ab &&
+ date >ab.c &&
+ date >ab/d &&
+ git add ab.c ab &&
+ git commit -m "initial" &&
+ git mv ab a
+'
+
+test_expect_success 'absolute pathname' '
+ (
+ rm -fr mine &&
+ mkdir mine &&
+ cd mine &&
+ test_create_repo one &&
+ cd one &&
+ mkdir sub &&
+ >sub/file &&
+ git add sub/file &&
+
+ git mv sub "$(pwd)/in" &&
+ test_path_is_missing sub &&
+ test_path_is_dir in &&
+ git ls-files --error-unmatch in/file
+ )
+'
+
+test_expect_success 'absolute pathname outside should fail' '
+ (
+ rm -fr mine &&
+ mkdir mine &&
+ cd mine &&
+ out=$(pwd) &&
+ test_create_repo one &&
+ cd one &&
+ mkdir sub &&
+ >sub/file &&
+ git add sub/file &&
+
+ test_must_fail git mv sub "$out/out" &&
+ test_path_is_dir sub &&
+ test_path_is_missing ../in &&
+ git ls-files --error-unmatch sub/file
+ )
+'
+
+test_expect_success 'git mv to move multiple sources into a directory' '
+ rm -fr .git && git init &&
+ mkdir dir other &&
+ >dir/a.txt &&
+ >dir/b.txt &&
+ git add dir/?.txt &&
+ git mv dir/a.txt dir/b.txt other &&
+ git ls-files >actual &&
+ cat >expect <<-\EOF &&
+ other/a.txt
+ other/b.txt
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'git mv should not change sha1 of moved cache entry' '
+ rm -fr .git &&
+ git init &&
+ echo 1 >dirty &&
+ git add dirty &&
+ entry="$(index_at_path dirty)" &&
+ git mv dirty dirty2 &&
+ test "$entry" = "$(index_at_path dirty2)" &&
+ echo 2 >dirty2 &&
+ git mv dirty2 dirty &&
+ test "$entry" = "$(index_at_path dirty)"
+'
+
+rm -f dirty dirty2
+
+# NB: This test is about the error message
+# as well as the failure.
+test_expect_success 'git mv error on conflicted file' '
+ rm -fr .git &&
+ git init &&
+ >conflict &&
+ test_when_finished "rm -f conflict" &&
+ cfhash=$(git hash-object -w conflict) &&
+ q_to_tab <<-EOF | git update-index --index-info &&
+ 0 $cfhash 0Qconflict
+ 100644 $cfhash 1Qconflict
+ EOF
+
+ test_must_fail git mv conflict newname 2>actual &&
+ test_grep "conflicted" actual
+'
+
+test_expect_success 'git mv should overwrite symlink to a file' '
+ rm -fr .git &&
+ git init &&
+ echo 1 >moved &&
+ test_ln_s_add moved symlink &&
+ git add moved &&
+ test_must_fail git mv moved symlink &&
+ git mv -f moved symlink &&
+ test_path_is_missing moved &&
+ test_path_is_file symlink &&
+ test "$(cat symlink)" = 1 &&
+ git update-index --refresh &&
+ git diff-files --quiet
+'
+
+rm -f moved symlink
+
+test_expect_success 'git mv should overwrite file with a symlink' '
+ rm -fr .git &&
+ git init &&
+ echo 1 >moved &&
+ test_ln_s_add moved symlink &&
+ git add moved &&
+ test_must_fail git mv symlink moved &&
+ git mv -f symlink moved &&
+ test_path_is_missing symlink &&
+ git update-index --refresh &&
+ git diff-files --quiet
+'
+
+test_expect_success SYMLINKS 'check moved symlink' '
+ test_path_is_symlink moved
+'
+
+rm -f moved symlink
+
+test_expect_success 'setup submodule' '
+ test_config_global protocol.file.allow always &&
+ git commit -m initial &&
+ git reset --hard &&
+ git submodule add ./. sub &&
+ echo content >file &&
+ git add file &&
+ git commit -m "added sub and file" &&
+ mkdir -p deep/directory/hierarchy &&
+ git submodule add ./. deep/directory/hierarchy/sub &&
+ git commit -m "added another submodule" &&
+ git branch submodule
+'
+
+test_expect_success 'git mv cannot move a submodule in a file' '
+ test_must_fail git mv sub file
+'
+
+test_expect_success 'git mv moves a submodule with a .git directory and no .gitmodules' '
+ entry="$(index_at_path sub)" &&
+ git rm .gitmodules &&
+ (
+ cd sub &&
+ rm -f .git &&
+ cp -R -P -p ../.git/modules/sub .git &&
+ GIT_WORK_TREE=. git config --unset core.worktree
+ ) &&
+ mkdir mod &&
+ git mv sub mod/sub &&
+ test_path_is_missing sub &&
+ test "$entry" = "$(index_at_path mod/sub)" &&
+ git -C mod/sub status &&
+ git update-index --refresh &&
+ git diff-files --quiet
+'
+
+test_expect_success 'git mv moves a submodule with a .git directory and .gitmodules' '
+ rm -rf mod &&
+ git reset --hard &&
+ git submodule update &&
+ entry="$(index_at_path sub)" &&
+ (
+ cd sub &&
+ rm -f .git &&
+ cp -R -P -p ../.git/modules/sub .git &&
+ GIT_WORK_TREE=. git config --unset core.worktree
+ ) &&
+ mkdir mod &&
+ git mv sub mod/sub &&
+ test_path_is_missing sub &&
+ test "$entry" = "$(index_at_path mod/sub)" &&
+ git -C mod/sub status &&
+ echo mod/sub >expected &&
+ git config -f .gitmodules submodule.sub.path >actual &&
+ test_cmp expected actual &&
+ git update-index --refresh &&
+ git diff-files --quiet
+'
+
+test_expect_success 'git mv moves a submodule with gitfile' '
+ rm -rf mod &&
+ git reset --hard &&
+ git submodule update &&
+ entry="$(index_at_path sub)" &&
+ mkdir mod &&
+ git -C mod mv ../sub/ . &&
+ test_path_is_missing sub &&
+ test "$entry" = "$(index_at_path mod/sub)" &&
+ git -C mod/sub status &&
+ echo mod/sub >expected &&
+ git config -f .gitmodules submodule.sub.path >actual &&
+ test_cmp expected actual &&
+ git update-index --refresh &&
+ git diff-files --quiet
+'
+
+test_expect_success 'mv does not complain when no .gitmodules file is found' '
+ rm -rf mod &&
+ git reset --hard &&
+ git submodule update &&
+ git rm .gitmodules &&
+ entry="$(index_at_path sub)" &&
+ mkdir mod &&
+ git mv sub mod/sub 2>actual.err &&
+ test_must_be_empty actual.err &&
+ test_path_is_missing sub &&
+ test "$entry" = "$(index_at_path mod/sub)" &&
+ git -C mod/sub status &&
+ git update-index --refresh &&
+ git diff-files --quiet
+'
+
+test_expect_success 'mv will error out on a modified .gitmodules file unless staged' '
+ rm -rf mod &&
+ git reset --hard &&
+ git submodule update &&
+ git config -f .gitmodules foo.bar true &&
+ entry="$(index_at_path sub)" &&
+ mkdir mod &&
+ test_must_fail git mv sub mod/sub 2>actual.err &&
+ test_file_not_empty actual.err &&
+ test_path_exists sub &&
+ git diff-files --quiet -- sub &&
+ git add .gitmodules &&
+ git mv sub mod/sub 2>actual.err &&
+ test_must_be_empty actual.err &&
+ test_path_is_missing sub &&
+ test "$entry" = "$(index_at_path mod/sub)" &&
+ git -C mod/sub status &&
+ git update-index --refresh &&
+ git diff-files --quiet
+'
+
+test_expect_success 'mv issues a warning when section is not found in .gitmodules' '
+ rm -rf mod &&
+ git reset --hard &&
+ git submodule update &&
+ git config -f .gitmodules --remove-section submodule.sub &&
+ git add .gitmodules &&
+ entry="$(index_at_path sub)" &&
+ echo "warning: Could not find section in .gitmodules where path=sub" >expect.err &&
+ mkdir mod &&
+ git mv sub mod/sub 2>actual.err &&
+ test_cmp expect.err actual.err &&
+ test_path_is_missing sub &&
+ test "$entry" = "$(index_at_path mod/sub)" &&
+ git -C mod/sub status &&
+ git update-index --refresh &&
+ git diff-files --quiet
+'
+
+test_expect_success 'mv --dry-run does not touch the submodule or .gitmodules' '
+ rm -rf mod &&
+ git reset --hard &&
+ git submodule update &&
+ mkdir mod &&
+ git mv -n sub mod/sub 2>actual.err &&
+ test_path_is_file sub/.git &&
+ git diff-index --exit-code HEAD &&
+ git update-index --refresh &&
+ git diff-files --quiet -- sub .gitmodules
+'
+
+test_expect_success 'checking out a commit before submodule moved needs manual updates' '
+ git mv sub sub2 &&
+ git commit -m "moved sub to sub2" &&
+ git checkout -q HEAD^ 2>actual &&
+ test_grep "^warning: unable to rmdir '\''sub2'\'':" actual &&
+ git status -s sub2 >actual &&
+ echo "?? sub2/" >expected &&
+ test_cmp expected actual &&
+ test_path_is_missing sub/.git &&
+ test_path_is_file sub2/.git &&
+ git submodule update &&
+ test_path_is_file sub/.git &&
+ rm -rf sub2 &&
+ git diff-index --exit-code HEAD &&
+ git update-index --refresh &&
+ git diff-files --quiet -- sub .gitmodules &&
+ git status -s sub2 >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'mv -k does not accidentally destroy submodules' '
+ git checkout submodule &&
+ mkdir dummy dest &&
+ git mv -k dummy sub dest &&
+ git status --porcelain >actual &&
+ grep "^R sub -> dest/sub" actual &&
+ git reset --hard &&
+ git checkout .
+'
+
+test_expect_success 'moving a submodule in nested directories' '
+ (
+ cd deep &&
+ git mv directory ../ &&
+ # git status would fail if the update of linking git dir to
+ # work dir of the submodule failed.
+ git status &&
+ git config -f ../.gitmodules submodule.deep/directory/hierarchy/sub.path >../actual &&
+ echo "directory/hierarchy/sub" >../expect
+ ) &&
+ test_cmp expect actual
+'
+
+test_expect_success 'moving nested submodules' '
+ test_config_global protocol.file.allow always &&
+ git commit -am "cleanup commit" &&
+ mkdir sub_nested_nested &&
+ (
+ cd sub_nested_nested &&
+ >nested_level2 &&
+ git init &&
+ git add . &&
+ git commit -m "nested level 2"
+ ) &&
+ mkdir sub_nested &&
+ (
+ cd sub_nested &&
+ >nested_level1 &&
+ git init &&
+ git add . &&
+ git commit -m "nested level 1" &&
+ git submodule add ../sub_nested_nested &&
+ git commit -m "add nested level 2"
+ ) &&
+ git submodule add ./sub_nested nested_move &&
+ git commit -m "add nested_move" &&
+ git submodule update --init --recursive &&
+ git mv nested_move sub_nested_moved &&
+ git status
+'
+
+test_done
diff --git a/t/t7002-mv-sparse-checkout.sh b/t/t7002-mv-sparse-checkout.sh
new file mode 100755
index 0000000..26582ae
--- /dev/null
+++ b/t/t7002-mv-sparse-checkout.sh
@@ -0,0 +1,517 @@
+#!/bin/sh
+
+test_description='git mv in sparse working trees'
+
+. ./test-lib.sh
+
+setup_sparse_checkout () {
+ mkdir folder1 &&
+ touch folder1/file1 &&
+ git add folder1 &&
+ git sparse-checkout set --cone sub
+}
+
+cleanup_sparse_checkout () {
+ git sparse-checkout disable &&
+ git reset --hard
+}
+
+test_expect_success 'setup' "
+ mkdir -p sub/dir sub/dir2 &&
+ touch a b c sub/d sub/dir/e sub/dir2/e &&
+ git add -A &&
+ git commit -m files &&
+
+ cat >sparse_error_header <<-EOF &&
+ The following paths and/or pathspecs matched paths that exist
+ outside of your sparse-checkout definition, so will not be
+ updated in the index:
+ EOF
+
+ cat >sparse_hint <<-EOF &&
+ hint: If you intend to update such entries, try one of the following:
+ hint: * Use the --sparse option.
+ hint: * Disable or modify the sparsity rules.
+ hint: Disable this message with \"git config advice.updateSparsePath false\"
+ EOF
+
+ cat >dirty_error_header <<-EOF &&
+ The following paths have been moved outside the
+ sparse-checkout definition but are not sparse due to local
+ modifications.
+ EOF
+
+ cat >dirty_hint <<-EOF
+ hint: To correct the sparsity of these paths, do the following:
+ hint: * Use \"git add --sparse <paths>\" to update the index
+ hint: * Use \"git sparse-checkout reapply\" to apply the sparsity rules
+ hint: Disable this message with \"git config advice.updateSparsePath false\"
+ EOF
+"
+
+test_expect_success 'mv refuses to move sparse-to-sparse' '
+ test_when_finished rm -f e &&
+ git reset --hard &&
+ git sparse-checkout set --no-cone a &&
+ touch b &&
+ test_must_fail git mv b e 2>stderr &&
+ cat sparse_error_header >expect &&
+ echo b >>expect &&
+ echo e >>expect &&
+ cat sparse_hint >>expect &&
+ test_cmp expect stderr &&
+ git mv --sparse b e 2>stderr &&
+ test_must_be_empty stderr
+'
+
+test_expect_success 'mv refuses to move sparse-to-sparse, ignores failure' '
+ test_when_finished rm -f b c e &&
+ git reset --hard &&
+ git sparse-checkout set a &&
+
+ # tracked-to-untracked
+ touch b &&
+ git mv -k b e 2>stderr &&
+ test_path_exists b &&
+ test_path_is_missing e &&
+ cat sparse_error_header >expect &&
+ echo b >>expect &&
+ echo e >>expect &&
+ cat sparse_hint >>expect &&
+ test_cmp expect stderr &&
+
+ git mv --sparse b e 2>stderr &&
+ test_must_be_empty stderr &&
+ test_path_is_missing b &&
+ test_path_exists e &&
+
+ # tracked-to-tracked
+ git reset --hard &&
+ touch b &&
+ git mv -k b c 2>stderr &&
+ test_path_exists b &&
+ test_path_is_missing c &&
+ cat sparse_error_header >expect &&
+ echo b >>expect &&
+ echo c >>expect &&
+ cat sparse_hint >>expect &&
+ test_cmp expect stderr &&
+
+ git mv --sparse b c 2>stderr &&
+ test_must_be_empty stderr &&
+ test_path_is_missing b &&
+ test_path_exists c
+'
+
+test_expect_success 'mv refuses to move non-sparse-to-sparse' '
+ test_when_finished rm -f b c e &&
+ git reset --hard &&
+ git sparse-checkout set a &&
+
+ # tracked-to-untracked
+ test_must_fail git mv a e 2>stderr &&
+ test_path_exists a &&
+ test_path_is_missing e &&
+ cat sparse_error_header >expect &&
+ echo e >>expect &&
+ cat sparse_hint >>expect &&
+ test_cmp expect stderr &&
+ git mv --sparse a e 2>stderr &&
+ test_must_be_empty stderr &&
+ test_path_is_missing a &&
+ test_path_exists e &&
+
+ # tracked-to-tracked
+ rm e &&
+ git reset --hard &&
+ test_must_fail git mv a c 2>stderr &&
+ test_path_exists a &&
+ test_path_is_missing c &&
+ cat sparse_error_header >expect &&
+ echo c >>expect &&
+ cat sparse_hint >>expect &&
+ test_cmp expect stderr &&
+ git mv --sparse a c 2>stderr &&
+ test_must_be_empty stderr &&
+ test_path_is_missing a &&
+ test_path_exists c
+'
+
+test_expect_success 'mv refuses to move sparse-to-non-sparse' '
+ test_when_finished rm -f b c e &&
+ git reset --hard &&
+ git sparse-checkout set a e &&
+
+ # tracked-to-untracked
+ touch b &&
+ test_must_fail git mv b e 2>stderr &&
+ cat sparse_error_header >expect &&
+ echo b >>expect &&
+ cat sparse_hint >>expect &&
+ test_cmp expect stderr &&
+ git mv --sparse b e 2>stderr &&
+ test_must_be_empty stderr
+'
+
+test_expect_success 'recursive mv refuses to move (possible) sparse' '
+ test_when_finished rm -rf b c e sub2 &&
+ git reset --hard &&
+ # Without cone mode, "sub" and "sub2" do not match
+ git sparse-checkout set sub/dir sub2/dir &&
+
+ # Add contained contents to ensure we avoid non-existence errors
+ mkdir sub/dir2 &&
+ touch sub/d sub/dir2/e &&
+
+ test_must_fail git mv sub sub2 2>stderr &&
+ cat sparse_error_header >expect &&
+ cat >>expect <<-\EOF &&
+ sub/d
+ sub2/d
+ sub/dir2/e
+ sub2/dir2/e
+ EOF
+ cat sparse_hint >>expect &&
+ test_cmp expect stderr &&
+ git mv --sparse sub sub2 2>stderr &&
+ test_must_be_empty stderr &&
+ git commit -m "moved sub to sub2" &&
+ git rev-parse HEAD~1:sub >expect &&
+ git rev-parse HEAD:sub2 >actual &&
+ test_cmp expect actual &&
+ git reset --hard HEAD~1
+'
+
+test_expect_success 'recursive mv refuses to move sparse' '
+ git reset --hard &&
+ # Use cone mode so "sub/" matches the sparse-checkout patterns
+ git sparse-checkout init --cone &&
+ git sparse-checkout set sub/dir sub2/dir &&
+
+ # Add contained contents to ensure we avoid non-existence errors
+ mkdir sub/dir2 &&
+ touch sub/dir2/e &&
+
+ test_must_fail git mv sub sub2 2>stderr &&
+ cat sparse_error_header >expect &&
+ cat >>expect <<-\EOF &&
+ sub/dir2/e
+ sub2/dir2/e
+ EOF
+ cat sparse_hint >>expect &&
+ test_cmp expect stderr &&
+ git mv --sparse sub sub2 2>stderr &&
+ test_must_be_empty stderr &&
+ git commit -m "moved sub to sub2" &&
+ git rev-parse HEAD~1:sub >expect &&
+ git rev-parse HEAD:sub2 >actual &&
+ test_cmp expect actual &&
+ git reset --hard HEAD~1
+'
+
+test_expect_success 'can move files to non-sparse dir' '
+ git reset --hard &&
+ git sparse-checkout init --no-cone &&
+ git sparse-checkout set a b c w !/x y/ &&
+ mkdir -p w x/y &&
+
+ git mv a w/new-a 2>stderr &&
+ git mv b x/y/new-b 2>stderr &&
+ test_must_be_empty stderr
+'
+
+test_expect_success 'refuse to move file to non-skip-worktree sparse path' '
+ test_when_finished "cleanup_sparse_checkout" &&
+ git reset --hard &&
+ git sparse-checkout init --no-cone &&
+ git sparse-checkout set a !/x y/ !x/y/z &&
+ mkdir -p x/y/z &&
+
+ test_must_fail git mv a x/y/z/new-a 2>stderr &&
+ echo x/y/z/new-a | cat sparse_error_header - sparse_hint >expect &&
+ test_cmp expect stderr
+'
+
+test_expect_success 'refuse to move out-of-cone directory without --sparse' '
+ test_when_finished "cleanup_sparse_checkout" &&
+ setup_sparse_checkout &&
+
+ test_must_fail git mv folder1 sub 2>stderr &&
+ cat sparse_error_header >expect &&
+ echo folder1/file1 >>expect &&
+ cat sparse_hint >>expect &&
+ test_cmp expect stderr
+'
+
+test_expect_success 'can move out-of-cone directory with --sparse' '
+ test_when_finished "cleanup_sparse_checkout" &&
+ setup_sparse_checkout &&
+
+ git mv --sparse folder1 sub 2>stderr &&
+ test_must_be_empty stderr &&
+
+ test_path_is_dir sub/folder1 &&
+ test_path_is_file sub/folder1/file1
+'
+
+test_expect_success 'refuse to move out-of-cone file without --sparse' '
+ test_when_finished "cleanup_sparse_checkout" &&
+ setup_sparse_checkout &&
+
+ test_must_fail git mv folder1/file1 sub 2>stderr &&
+ cat sparse_error_header >expect &&
+ echo folder1/file1 >>expect &&
+ cat sparse_hint >>expect &&
+ test_cmp expect stderr
+'
+
+test_expect_success 'can move out-of-cone file with --sparse' '
+ test_when_finished "cleanup_sparse_checkout" &&
+ setup_sparse_checkout &&
+
+ git mv --sparse folder1/file1 sub 2>stderr &&
+ test_must_be_empty stderr &&
+
+ test_path_is_file sub/file1
+'
+
+test_expect_success 'refuse to move sparse file to existing destination' '
+ test_when_finished "cleanup_sparse_checkout" &&
+ mkdir folder1 &&
+ touch folder1/file1 &&
+ touch sub/file1 &&
+ git add folder1 sub/file1 &&
+ git sparse-checkout set --cone sub &&
+
+ test_must_fail git mv --sparse folder1/file1 sub 2>stderr &&
+ echo "fatal: destination exists, source=folder1/file1, destination=sub/file1" >expect &&
+ test_cmp expect stderr
+'
+
+test_expect_success 'move sparse file to existing destination with --force and --sparse' '
+ test_when_finished "cleanup_sparse_checkout" &&
+ mkdir folder1 &&
+ touch folder1/file1 &&
+ touch sub/file1 &&
+ echo "overwrite" >folder1/file1 &&
+ git add folder1 sub/file1 &&
+ git sparse-checkout set --cone sub &&
+
+ git mv --sparse --force folder1/file1 sub 2>stderr &&
+ test_must_be_empty stderr &&
+ echo "overwrite" >expect &&
+ test_cmp expect sub/file1
+'
+
+test_expect_success 'move clean path from in-cone to out-of-cone' '
+ test_when_finished "cleanup_sparse_checkout" &&
+ setup_sparse_checkout &&
+
+ test_must_fail git mv sub/d folder1 2>stderr &&
+ cat sparse_error_header >expect &&
+ echo "folder1/d" >>expect &&
+ cat sparse_hint >>expect &&
+ test_cmp expect stderr &&
+
+ git mv --sparse sub/d folder1 2>stderr &&
+ test_must_be_empty stderr &&
+
+ test_path_is_missing sub/d &&
+ test_path_is_missing folder1/d &&
+ git ls-files -t >actual &&
+ ! grep "^H sub/d\$" actual &&
+ grep "S folder1/d" actual
+'
+
+test_expect_success 'move clean path from in-cone to out-of-cone overwrite' '
+ test_when_finished "cleanup_sparse_checkout" &&
+ setup_sparse_checkout &&
+ echo "sub/file1 overwrite" >sub/file1 &&
+ git add sub/file1 &&
+
+ test_must_fail git mv sub/file1 folder1 2>stderr &&
+ cat sparse_error_header >expect &&
+ echo "folder1/file1" >>expect &&
+ cat sparse_hint >>expect &&
+ test_cmp expect stderr &&
+
+ test_must_fail git mv --sparse sub/file1 folder1 2>stderr &&
+ echo "fatal: destination exists in the index, source=sub/file1, destination=folder1/file1" \
+ >expect &&
+ test_cmp expect stderr &&
+
+ git mv --sparse -f sub/file1 folder1 2>stderr &&
+ test_must_be_empty stderr &&
+
+ test_path_is_missing sub/file1 &&
+ test_path_is_missing folder1/file1 &&
+ git ls-files -t >actual &&
+ ! grep "H sub/file1" actual &&
+ grep "S folder1/file1" actual &&
+
+ # compare file content before move and after move
+ echo "sub/file1 overwrite" >expect &&
+ git ls-files -s -- folder1/file1 | awk "{print \$2}" >oid &&
+ git cat-file blob $(cat oid) >actual &&
+ test_cmp expect actual
+'
+
+# This test is testing the same behavior as the
+# "move clean path from in-cone to out-of-cone overwrite" above.
+# The only difference is the <destination> changes from "folder1" to "folder1/file1"
+test_expect_success 'move clean path from in-cone to out-of-cone file overwrite' '
+ test_when_finished "cleanup_sparse_checkout" &&
+ setup_sparse_checkout &&
+ echo "sub/file1 overwrite" >sub/file1 &&
+ git add sub/file1 &&
+
+ test_must_fail git mv sub/file1 folder1/file1 2>stderr &&
+ cat sparse_error_header >expect &&
+ echo "folder1/file1" >>expect &&
+ cat sparse_hint >>expect &&
+ test_cmp expect stderr &&
+
+ test_must_fail git mv --sparse sub/file1 folder1/file1 2>stderr &&
+ echo "fatal: destination exists in the index, source=sub/file1, destination=folder1/file1" \
+ >expect &&
+ test_cmp expect stderr &&
+
+ git mv --sparse -f sub/file1 folder1/file1 2>stderr &&
+ test_must_be_empty stderr &&
+
+ test_path_is_missing sub/file1 &&
+ test_path_is_missing folder1/file1 &&
+ git ls-files -t >actual &&
+ ! grep "H sub/file1" actual &&
+ grep "S folder1/file1" actual &&
+
+ # compare file content before move and after move
+ echo "sub/file1 overwrite" >expect &&
+ git ls-files -s -- folder1/file1 | awk "{print \$2}" >oid &&
+ git cat-file blob $(cat oid) >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'move directory with one of the files overwrite' '
+ test_when_finished "cleanup_sparse_checkout" &&
+ mkdir -p folder1/dir &&
+ touch folder1/dir/file1 &&
+ git add folder1 &&
+ git sparse-checkout set --cone sub &&
+
+ echo test >sub/dir/file1 &&
+ git add sub/dir/file1 &&
+
+ test_must_fail git mv sub/dir folder1 2>stderr &&
+ cat sparse_error_header >expect &&
+ echo "folder1/dir/e" >>expect &&
+ echo "folder1/dir/file1" >>expect &&
+ cat sparse_hint >>expect &&
+ test_cmp expect stderr &&
+
+ test_must_fail git mv --sparse sub/dir folder1 2>stderr &&
+ echo "fatal: destination exists in the index, source=sub/dir/file1, destination=folder1/dir/file1" \
+ >expect &&
+ test_cmp expect stderr &&
+
+ git mv --sparse -f sub/dir folder1 2>stderr &&
+ test_must_be_empty stderr &&
+
+ test_path_is_missing sub/dir/file1 &&
+ test_path_is_missing sub/dir/e &&
+ test_path_is_missing folder1/file1 &&
+ git ls-files -t >actual &&
+ ! grep "H sub/dir/file1" actual &&
+ ! grep "H sub/dir/e" actual &&
+ grep "S folder1/dir/file1" actual &&
+
+ # compare file content before move and after move
+ echo test >expect &&
+ git ls-files -s -- folder1/dir/file1 | awk "{print \$2}" >oid &&
+ git cat-file blob $(cat oid) >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'move dirty path from in-cone to out-of-cone' '
+ test_when_finished "cleanup_sparse_checkout" &&
+ setup_sparse_checkout &&
+ echo "modified" >>sub/d &&
+
+ test_must_fail git mv sub/d folder1 2>stderr &&
+ cat sparse_error_header >expect &&
+ echo "folder1/d" >>expect &&
+ cat sparse_hint >>expect &&
+ test_cmp expect stderr &&
+
+ git mv --sparse sub/d folder1 2>stderr &&
+ cat dirty_error_header >expect &&
+ echo "folder1/d" >>expect &&
+ cat dirty_hint >>expect &&
+ test_cmp expect stderr &&
+
+ test_path_is_missing sub/d &&
+ test_path_is_file folder1/d &&
+ git ls-files -t >actual &&
+ ! grep "^H sub/d\$" actual &&
+ grep "H folder1/d" actual
+'
+
+test_expect_success 'move dir from in-cone to out-of-cone' '
+ test_when_finished "cleanup_sparse_checkout" &&
+ setup_sparse_checkout &&
+ mkdir sub/dir/deep &&
+
+ test_must_fail git mv sub/dir folder1 2>stderr &&
+ cat sparse_error_header >expect &&
+ echo "folder1/dir/e" >>expect &&
+ cat sparse_hint >>expect &&
+ test_cmp expect stderr &&
+
+ git mv --sparse sub/dir folder1 2>stderr &&
+ test_must_be_empty stderr &&
+
+ test_path_is_missing sub/dir &&
+ test_path_is_missing folder1 &&
+ git ls-files -t >actual &&
+ ! grep "H sub/dir/e" actual &&
+ grep "S folder1/dir/e" actual
+'
+
+test_expect_success 'move partially-dirty dir from in-cone to out-of-cone' '
+ test_when_finished "cleanup_sparse_checkout" &&
+ setup_sparse_checkout &&
+ mkdir sub/dir/deep &&
+ touch sub/dir/e2 sub/dir/e3 &&
+ git add sub/dir/e2 sub/dir/e3 &&
+ echo "modified" >>sub/dir/e2 &&
+ echo "modified" >>sub/dir/e3 &&
+
+ test_must_fail git mv sub/dir folder1 2>stderr &&
+ cat sparse_error_header >expect &&
+ echo "folder1/dir/e" >>expect &&
+ echo "folder1/dir/e2" >>expect &&
+ echo "folder1/dir/e3" >>expect &&
+ cat sparse_hint >>expect &&
+ test_cmp expect stderr &&
+
+ git mv --sparse sub/dir folder1 2>stderr &&
+ cat dirty_error_header >expect &&
+ echo "folder1/dir/e2" >>expect &&
+ echo "folder1/dir/e3" >>expect &&
+ cat dirty_hint >>expect &&
+ test_cmp expect stderr &&
+
+ test_path_is_missing sub/dir &&
+ test_path_is_missing folder1/dir/e &&
+ test_path_is_file folder1/dir/e2 &&
+ test_path_is_file folder1/dir/e3 &&
+ git ls-files -t >actual &&
+ ! grep "H sub/dir/e" actual &&
+ ! grep "H sub/dir/e2" actual &&
+ ! grep "H sub/dir/e3" actual &&
+ grep "S folder1/dir/e" actual &&
+ grep "H folder1/dir/e2" actual &&
+ grep "H folder1/dir/e3" actual
+'
+
+test_done
diff --git a/t/t7003-filter-branch.sh b/t/t7003-filter-branch.sh
new file mode 100755
index 0000000..f6aebe9
--- /dev/null
+++ b/t/t7003-filter-branch.sh
@@ -0,0 +1,543 @@
+#!/bin/sh
+
+test_description='git filter-branch'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY/lib-gpg.sh"
+
+test_expect_success 'setup' '
+ test_commit A &&
+ GIT_COMMITTER_DATE="@0 +0000" GIT_AUTHOR_DATE="@0 +0000" &&
+ test_commit --notick B &&
+ git checkout -b branch B &&
+ test_commit D &&
+ mkdir dir &&
+ test_commit dir/D &&
+ test_commit E &&
+ git checkout main &&
+ test_commit C &&
+ git checkout branch &&
+ git merge C &&
+ git tag F &&
+ test_commit G &&
+ test_commit H
+'
+# * (HEAD, branch) H
+# * G
+# * Merge commit 'C' into branch
+# |\
+# | * (main) C
+# * | E
+# * | dir/D
+# * | D
+# |/
+# * B
+# * A
+
+
+H=$(git rev-parse H)
+
+test_expect_success 'rewrite identically' '
+ git filter-branch branch
+'
+test_expect_success 'result is really identical' '
+ test $H = $(git rev-parse HEAD)
+'
+
+test_expect_success 'rewrite bare repository identically' '
+ (git config core.bare true && cd .git &&
+ git filter-branch branch > filter-output 2>&1 &&
+ ! grep fatal filter-output)
+'
+git config core.bare false
+test_expect_success 'result is really identical' '
+ test $H = $(git rev-parse HEAD)
+'
+
+TRASHDIR=$(pwd)
+test_expect_success 'correct GIT_DIR while using -d' '
+ mkdir drepo &&
+ ( cd drepo &&
+ git init &&
+ test_commit drepo &&
+ git filter-branch -d "$TRASHDIR/dfoo" \
+ --index-filter "cp \"$TRASHDIR\"/dfoo/backup-refs \"$TRASHDIR\"" \
+ ) &&
+ grep drepo "$TRASHDIR/backup-refs"
+'
+
+test_expect_success 'tree-filter works with -d' '
+ git init drepo-tree &&
+ (
+ cd drepo-tree &&
+ test_commit one &&
+ git filter-branch -d "$TRASHDIR/dfoo" \
+ --tree-filter "echo changed >one.t" &&
+ echo changed >expect &&
+ git cat-file blob HEAD:one.t >actual &&
+ test_cmp expect actual &&
+ test_cmp one.t actual
+ )
+'
+
+test_expect_success 'Fail if commit filter fails' '
+ test_must_fail git filter-branch -f --commit-filter "exit 1" HEAD
+'
+
+test_expect_success 'rewrite, renaming a specific file' '
+ git filter-branch -f --tree-filter "mv D.t doh || :" HEAD
+'
+
+test_expect_success 'test that the file was renamed' '
+ test D = "$(git show HEAD:doh --)" &&
+ ! test -f D.t &&
+ test -f doh &&
+ test D = "$(cat doh)"
+'
+
+test_expect_success 'rewrite, renaming a specific directory' '
+ git filter-branch -f --tree-filter "mv dir diroh || :" HEAD
+'
+
+test_expect_success 'test that the directory was renamed' '
+ test dir/D = "$(git show HEAD:diroh/D.t --)" &&
+ ! test -d dir &&
+ test -d diroh &&
+ ! test -d diroh/dir &&
+ test -f diroh/D.t &&
+ test dir/D = "$(cat diroh/D.t)"
+'
+
+V=$(git rev-parse HEAD)
+
+test_expect_success 'populate --state-branch' '
+ git filter-branch --state-branch state -f --tree-filter "touch file || :" HEAD
+'
+
+W=$(git rev-parse HEAD)
+
+test_expect_success 'using --state-branch to skip already rewritten commits' '
+ test_when_finished git reset --hard $V &&
+ git reset --hard $V &&
+ git filter-branch --state-branch state -f --tree-filter "touch file || :" HEAD &&
+ test_cmp_rev $W HEAD
+'
+
+git tag oldD HEAD~4
+test_expect_success 'rewrite one branch, keeping a side branch' '
+ git branch modD oldD &&
+ git filter-branch -f --tree-filter "mv B.t boh || :" D..modD
+'
+
+test_expect_success 'common ancestor is still common (unchanged)' '
+ test "$(git merge-base modD D)" = "$(git rev-parse B)"
+'
+
+test_expect_success 'filter subdirectory only' '
+ mkdir subdir &&
+ touch subdir/new &&
+ git add subdir/new &&
+ test_tick &&
+ git commit -m "subdir" &&
+ echo H > A.t &&
+ test_tick &&
+ git commit -m "not subdir" A.t &&
+ echo A > subdir/new &&
+ test_tick &&
+ git commit -m "again subdir" subdir/new &&
+ git rm A.t &&
+ test_tick &&
+ git commit -m "again not subdir" &&
+ git branch sub &&
+ git branch sub-earlier HEAD~2 &&
+ git filter-branch -f --subdirectory-filter subdir \
+ refs/heads/sub refs/heads/sub-earlier
+'
+
+test_expect_success 'subdirectory filter result looks okay' '
+ test 2 = $(git rev-list sub | wc -l) &&
+ git show sub:new &&
+ test_must_fail git show sub:subdir &&
+ git show sub-earlier:new &&
+ test_must_fail git show sub-earlier:subdir
+'
+
+test_expect_success 'more setup' '
+ git checkout main &&
+ mkdir subdir &&
+ echo A > subdir/new &&
+ git add subdir/new &&
+ test_tick &&
+ git commit -m "subdir on main" subdir/new &&
+ git rm A.t &&
+ test_tick &&
+ git commit -m "again subdir on main" &&
+ git merge branch
+'
+
+test_expect_success 'use index-filter to move into a subdirectory' '
+ git branch directorymoved &&
+ git filter-branch -f --index-filter \
+ "git ls-files -s | sed \"s- -&newsubdir/-\" |
+ GIT_INDEX_FILE=\$GIT_INDEX_FILE.new \
+ git update-index --index-info &&
+ mv \"\$GIT_INDEX_FILE.new\" \"\$GIT_INDEX_FILE\"" directorymoved &&
+ git diff --exit-code HEAD directorymoved:newsubdir
+'
+
+test_expect_success 'stops when msg filter fails' '
+ old=$(git rev-parse HEAD) &&
+ test_must_fail git filter-branch -f --msg-filter false HEAD &&
+ test $old = $(git rev-parse HEAD) &&
+ rm -rf .git-rewrite
+'
+
+test_expect_success 'author information is preserved' '
+ : > i &&
+ git add i &&
+ test_tick &&
+ GIT_AUTHOR_NAME="B V Uips" git commit -m bvuips &&
+ git branch preserved-author &&
+ (sane_unset GIT_AUTHOR_NAME &&
+ git filter-branch -f --msg-filter "cat; \
+ test \$GIT_COMMIT != $(git rev-parse main) || \
+ echo Hallo" \
+ preserved-author) &&
+ git rev-list --author="B V Uips" preserved-author >actual &&
+ test_line_count = 1 actual
+'
+
+test_expect_success "remove a certain author's commits" '
+ echo i > i &&
+ test_tick &&
+ git commit -m i i &&
+ git branch removed-author &&
+ git filter-branch -f --commit-filter "\
+ if [ \"\$GIT_AUTHOR_NAME\" = \"B V Uips\" ];\
+ then\
+ skip_commit \"\$@\";
+ else\
+ git commit-tree \"\$@\";\
+ fi" removed-author &&
+ cnt1=$(git rev-list main | wc -l) &&
+ cnt2=$(git rev-list removed-author | wc -l) &&
+ test $cnt1 -eq $(($cnt2 + 1)) &&
+ git rev-list --author="B V Uips" removed-author >actual &&
+ test_line_count = 0 actual
+'
+
+test_expect_success 'barf on invalid name' '
+ test_must_fail git filter-branch -f main xy-problem &&
+ test_must_fail git filter-branch -f HEAD^
+'
+
+test_expect_success '"map" works in commit filter' '
+ git filter-branch -f --commit-filter "\
+ parent=\$(git rev-parse \$GIT_COMMIT^) &&
+ mapped=\$(map \$parent) &&
+ actual=\$(echo \"\$@\" | sed \"s/^.*-p //\") &&
+ test \$mapped = \$actual &&
+ git commit-tree \"\$@\";" main~2..main &&
+ git rev-parse --verify main
+'
+
+test_expect_success 'Name needing quotes' '
+
+ git checkout -b rerere A &&
+ mkdir foo &&
+ name="れれれ" &&
+ >foo/$name &&
+ git add foo &&
+ git commit -m "Adding a file" &&
+ git filter-branch --tree-filter "rm -fr foo" &&
+ test_must_fail git ls-files --error-unmatch "foo/$name" &&
+ test $(git rev-parse --verify rerere) != $(git rev-parse --verify A)
+
+'
+
+test_expect_success 'Subdirectory filter with disappearing trees' '
+ git reset --hard &&
+ git checkout main &&
+
+ mkdir foo &&
+ touch foo/bar &&
+ git add foo &&
+ test_tick &&
+ git commit -m "Adding foo" &&
+
+ git rm -r foo &&
+ test_tick &&
+ git commit -m "Removing foo" &&
+
+ mkdir foo &&
+ touch foo/bar &&
+ git add foo &&
+ test_tick &&
+ git commit -m "Re-adding foo" &&
+
+ git filter-branch -f --subdirectory-filter foo &&
+ git rev-list main >actual &&
+ test_line_count = 3 actual
+'
+
+test_expect_success 'Tag name filtering retains tag message' '
+ git tag -m atag T &&
+ git cat-file tag T > expect &&
+ git filter-branch -f --tag-name-filter cat &&
+ git cat-file tag T > actual &&
+ test_cmp expect actual
+'
+
+faux_gpg_tag='object XXXXXX
+type commit
+tag S
+tagger T A Gger <tagger@example.com> 1206026339 -0500
+
+This is a faux gpg signed tag.
+-----BEGIN PGP SIGNATURE-----
+Version: FauxGPG v0.0.0 (FAUX/Linux)
+
+gdsfoewhxu/6l06f1kxyxhKdZkrcbaiOMtkJUA9ITAc1mlamh0ooasxkH1XwMbYQ
+acmwXaWET20H0GeAGP+7vow=
+=agpO
+-----END PGP SIGNATURE-----
+'
+test_expect_success 'Tag name filtering strips gpg signature' '
+ sha1=$(git rev-parse HEAD) &&
+ sha1t=$(echo "$faux_gpg_tag" | sed -e s/XXXXXX/$sha1/ | git mktag) &&
+ git update-ref "refs/tags/S" "$sha1t" &&
+ echo "$faux_gpg_tag" | sed -e s/XXXXXX/$sha1/ | head -n 6 > expect &&
+ git filter-branch -f --tag-name-filter cat &&
+ git cat-file tag S > actual &&
+ test_cmp expect actual
+'
+
+test_expect_success GPG 'Filtering retains message of gpg signed commit' '
+ mkdir gpg &&
+ touch gpg/foo &&
+ git add gpg &&
+ test_tick &&
+ git commit -S -m "Adding gpg" &&
+
+ git log -1 --format="%s" > expect &&
+ git filter-branch -f --msg-filter "cat" &&
+ git log -1 --format="%s" > actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Tag name filtering allows slashes in tag names' '
+ git tag -m tag-with-slash X/1 &&
+ git cat-file tag X/1 | sed -e s,X/1,X/2, > expect &&
+ git filter-branch -f --tag-name-filter "echo X/2" &&
+ git cat-file tag X/2 > actual &&
+ test_cmp expect actual
+'
+test_expect_success 'setup --prune-empty comparisons' '
+ git checkout --orphan main-no-a &&
+ git rm -rf . &&
+ unset test_tick &&
+ test_tick &&
+ GIT_COMMITTER_DATE="@0 +0000" GIT_AUTHOR_DATE="@0 +0000" &&
+ test_commit --notick B B.t B Bx &&
+ git checkout -b branch-no-a Bx &&
+ test_commit D D.t D Dx &&
+ mkdir dir &&
+ test_commit dir/D dir/D.t dir/D dir/Dx &&
+ test_commit E E.t E Ex &&
+ git checkout main-no-a &&
+ test_commit C C.t C Cx &&
+ git checkout branch-no-a &&
+ git merge Cx -m "Merge tag '\''C'\'' into branch" &&
+ git tag Fx &&
+ test_commit G G.t G Gx &&
+ test_commit H H.t H Hx &&
+ git checkout branch
+'
+
+test_expect_success 'Prune empty commits' '
+ git rev-list HEAD > expect &&
+ test_commit to_remove &&
+ git filter-branch -f --index-filter "git update-index --remove to_remove.t" --prune-empty HEAD &&
+ git rev-list HEAD > actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'prune empty collapsed merges' '
+ test_config merge.ff false &&
+ git rev-list HEAD >expect &&
+ test_commit to_remove_2 &&
+ git reset --hard HEAD^ &&
+ test_merge non-ff to_remove_2 &&
+ git filter-branch -f --index-filter "git update-index --remove to_remove_2.t" --prune-empty HEAD &&
+ git rev-list HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'prune empty works even without index/tree filters' '
+ git rev-list HEAD >expect &&
+ git commit --allow-empty -m empty &&
+ git filter-branch -f --prune-empty HEAD &&
+ git rev-list HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--prune-empty is able to prune root commit' '
+ git rev-list branch-no-a >expect &&
+ git branch testing H &&
+ git filter-branch -f --prune-empty --index-filter "git update-index --remove A.t" testing &&
+ git rev-list testing >actual &&
+ git branch -D testing &&
+ test_cmp expect actual
+'
+
+test_expect_success '--prune-empty is able to prune entire branch' '
+ git branch prune-entire B &&
+ git filter-branch -f --prune-empty --index-filter "git update-index --remove A.t B.t" prune-entire &&
+ test_must_fail git rev-parse refs/heads/prune-entire &&
+ if test_have_prereq REFFILES
+ then
+ test_must_fail git reflog exists refs/heads/prune-entire
+ fi
+'
+
+test_expect_success '--remap-to-ancestor with filename filters' '
+ git checkout main &&
+ git reset --hard A &&
+ test_commit add-foo foo 1 &&
+ git branch moved-foo &&
+ test_commit add-bar bar a &&
+ git branch invariant &&
+ orig_invariant=$(git rev-parse invariant) &&
+ git branch moved-bar &&
+ test_commit change-foo foo 2 &&
+ git filter-branch -f --remap-to-ancestor \
+ moved-foo moved-bar A..main \
+ -- -- foo &&
+ test $(git rev-parse moved-foo) = $(git rev-parse moved-bar) &&
+ test $(git rev-parse moved-foo) = $(git rev-parse main^) &&
+ test $orig_invariant = $(git rev-parse invariant)
+'
+
+test_expect_success 'automatic remapping to ancestor with filename filters' '
+ git checkout main &&
+ git reset --hard A &&
+ test_commit add-foo2 foo 1 &&
+ git branch moved-foo2 &&
+ test_commit add-bar2 bar a &&
+ git branch invariant2 &&
+ orig_invariant=$(git rev-parse invariant2) &&
+ git branch moved-bar2 &&
+ test_commit change-foo2 foo 2 &&
+ git filter-branch -f \
+ moved-foo2 moved-bar2 A..main \
+ -- -- foo &&
+ test $(git rev-parse moved-foo2) = $(git rev-parse moved-bar2) &&
+ test $(git rev-parse moved-foo2) = $(git rev-parse main^) &&
+ test $orig_invariant = $(git rev-parse invariant2)
+'
+
+test_expect_success 'setup submodule' '
+ rm -fr ?* .git &&
+ git init &&
+ test_commit file &&
+ mkdir submod &&
+ submodurl="$PWD/submod" &&
+ ( cd submod &&
+ git init &&
+ test_commit file-in-submod ) &&
+ git submodule add "$submodurl" &&
+ git commit -m "added submodule" &&
+ test_commit add-file &&
+ ( cd submod && test_commit add-in-submodule ) &&
+ git add submod &&
+ git commit -m "changed submodule" &&
+ git branch original HEAD
+'
+
+orig_head=$(git show-ref --hash --head HEAD)
+
+test_expect_success 'rewrite submodule with another content' '
+ git filter-branch --tree-filter "test -d submod && {
+ rm -rf submod &&
+ git rm -rf --quiet submod &&
+ mkdir submod &&
+ : > submod/file
+ } || :" HEAD &&
+ test $orig_head != $(git show-ref --hash --head HEAD)
+'
+
+test_expect_success 'replace submodule revision' '
+ invalid=$(test_oid numeric) &&
+ git reset --hard original &&
+ git filter-branch -f --tree-filter \
+ "if git ls-files --error-unmatch -- submod > /dev/null 2>&1
+ then git update-index --cacheinfo 160000 $invalid submod
+ fi" HEAD &&
+ test $orig_head != $(git show-ref --hash --head HEAD)
+'
+
+test_expect_success 'filter commit message without trailing newline' '
+ git reset --hard original &&
+ commit=$(printf "no newline" | git commit-tree HEAD^{tree}) &&
+ git update-ref refs/heads/no-newline $commit &&
+ git filter-branch -f refs/heads/no-newline &&
+ echo $commit >expect &&
+ git rev-parse refs/heads/no-newline >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'tree-filter deals with object name vs pathname ambiguity' '
+ test_when_finished "git reset --hard original" &&
+ ambiguous=$(git rev-list -1 HEAD) &&
+ git filter-branch --tree-filter "mv file.t $ambiguous" HEAD^.. &&
+ git show HEAD:$ambiguous
+'
+
+test_expect_success 'rewrite repository including refs that point at non-commit object' '
+ test_when_finished "git reset --hard original" &&
+ tree=$(git rev-parse HEAD^{tree}) &&
+ test_when_finished "git replace -d $tree" &&
+ echo A >new &&
+ git add new &&
+ new_tree=$(git write-tree) &&
+ git replace $tree $new_tree &&
+ git tag -a -m "tag to a tree" treetag $new_tree &&
+ git reset --hard HEAD &&
+ git filter-branch -f -- --all >filter-output 2>&1 &&
+ ! grep fatal filter-output
+'
+
+test_expect_success 'filter-branch handles ref deletion' '
+ git switch --orphan empty-commit &&
+ git commit --allow-empty -m "empty commit" &&
+ git tag empty &&
+ git branch to-delete &&
+ git filter-branch -f --prune-empty to-delete >out 2>&1 &&
+ grep "to-delete.*was deleted" out &&
+ test_must_fail git rev-parse --verify to-delete
+'
+
+test_expect_success 'filter-branch handles ref rewrite' '
+ git checkout empty &&
+ test_commit to-drop &&
+ git branch rewrite &&
+ git filter-branch -f \
+ --index-filter "git rm --ignore-unmatch --cached to-drop.t" \
+ rewrite >out 2>&1 &&
+ grep "rewrite.*was rewritten" out &&
+ ! grep -i warning out &&
+ git diff-tree empty rewrite
+'
+
+test_expect_success 'filter-branch handles ancestor rewrite' '
+ test_commit to-exclude &&
+ git branch ancestor &&
+ git filter-branch -f ancestor -- :^to-exclude.t >out 2>&1 &&
+ grep "ancestor.*was rewritten" out &&
+ ! grep -i warning out &&
+ git diff-tree HEAD^ ancestor
+'
+
+test_done
diff --git a/t/t7004-tag.sh b/t/t7004-tag.sh
new file mode 100755
index 0000000..e689db4
--- /dev/null
+++ b/t/t7004-tag.sh
@@ -0,0 +1,2210 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Carlos Rica
+#
+
+test_description='git tag
+
+Tests for operations with tags.'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-gpg.sh
+. "$TEST_DIRECTORY"/lib-terminal.sh
+
+# creating and listing lightweight tags:
+
+tag_exists () {
+ git show-ref --quiet --verify refs/tags/"$1"
+}
+
+test_expect_success 'setup' '
+ test_oid_cache <<-EOM
+ othersigheader sha1:gpgsig-sha256
+ othersigheader sha256:gpgsig
+ EOM
+'
+
+test_expect_success 'listing all tags in an empty tree should succeed' '
+ git tag -l &&
+ git tag
+'
+
+test_expect_success 'listing all tags in an empty tree should output nothing' '
+ test $(git tag -l | wc -l) -eq 0 &&
+ test $(git tag | wc -l) -eq 0
+'
+
+test_expect_success 'sort tags, ignore case' '
+ (
+ git init sort &&
+ cd sort &&
+ test_commit initial &&
+ git tag tag-one &&
+ git tag TAG-two &&
+ git tag -l >actual &&
+ cat >expected <<-\EOF &&
+ TAG-two
+ initial
+ tag-one
+ EOF
+ test_cmp expected actual &&
+ git tag -l -i >actual &&
+ cat >expected <<-\EOF &&
+ initial
+ tag-one
+ TAG-two
+ EOF
+ test_cmp expected actual
+ )
+'
+
+test_expect_success 'looking for a tag in an empty tree should fail' \
+ '! (tag_exists mytag)'
+
+test_expect_success 'creating a tag in an empty tree should fail' '
+ test_must_fail git tag mynotag &&
+ ! tag_exists mynotag
+'
+
+test_expect_success 'creating a tag for HEAD in an empty tree should fail' '
+ test_must_fail git tag mytaghead HEAD &&
+ ! tag_exists mytaghead
+'
+
+test_expect_success 'creating a tag for an unknown revision should fail' '
+ test_must_fail git tag mytagnorev aaaaaaaaaaa &&
+ ! tag_exists mytagnorev
+'
+
+# commit used in the tests, test_tick is also called here to freeze the date:
+test_expect_success 'creating a tag using default HEAD should succeed' '
+ test_config core.logAllRefUpdates true &&
+ test_tick &&
+ echo foo >foo &&
+ git add foo &&
+ git commit -m Foo &&
+ git tag mytag &&
+ test_must_fail git reflog exists refs/tags/mytag
+'
+
+test_expect_success 'creating a tag with --create-reflog should create reflog' '
+ git log -1 \
+ --format="format:tag: tagging %h (%s, %cd)%n" \
+ --date=format:%Y-%m-%d >expected &&
+ test_when_finished "git tag -d tag_with_reflog1" &&
+ git tag --create-reflog tag_with_reflog1 &&
+ git reflog exists refs/tags/tag_with_reflog1 &&
+ test-tool ref-store main for-each-reflog-ent refs/tags/tag_with_reflog1 | sed -e "s/^.* //" >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'annotated tag with --create-reflog has correct message' '
+ git log -1 \
+ --format="format:tag: tagging %h (%s, %cd)%n" \
+ --date=format:%Y-%m-%d >expected &&
+ test_when_finished "git tag -d tag_with_reflog2" &&
+ git tag -m "annotated tag" --create-reflog tag_with_reflog2 &&
+ git reflog exists refs/tags/tag_with_reflog2 &&
+ test-tool ref-store main for-each-reflog-ent refs/tags/tag_with_reflog2 | sed -e "s/^.* //" >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success '--create-reflog does not create reflog on failure' '
+ test_must_fail git tag --create-reflog mytag &&
+ test_must_fail git reflog exists refs/tags/mytag
+'
+
+test_expect_success 'option core.logAllRefUpdates=always creates reflog' '
+ test_when_finished "git tag -d tag_with_reflog3" &&
+ test_config core.logAllRefUpdates always &&
+ git tag tag_with_reflog3 &&
+ git reflog exists refs/tags/tag_with_reflog3
+'
+
+test_expect_success 'listing all tags if one exists should succeed' '
+ git tag -l &&
+ git tag
+'
+
+cat >expect <<EOF
+mytag
+EOF
+test_expect_success 'Multiple -l or --list options are equivalent to one -l option' '
+ git tag -l -l >actual &&
+ test_cmp expect actual &&
+ git tag --list --list >actual &&
+ test_cmp expect actual &&
+ git tag --list -l --list >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'listing all tags if one exists should output that tag' '
+ test $(git tag -l) = mytag &&
+ test $(git tag) = mytag
+'
+
+# pattern matching:
+
+test_expect_success 'listing a tag using a matching pattern should succeed' \
+ 'git tag -l mytag'
+
+test_expect_success 'listing a tag with --ignore-case' \
+ 'test $(git tag -l --ignore-case MYTAG) = mytag'
+
+test_expect_success \
+ 'listing a tag using a matching pattern should output that tag' \
+ 'test $(git tag -l mytag) = mytag'
+
+test_expect_success \
+ 'listing tags using a non-matching pattern should succeed' \
+ 'git tag -l xxx'
+
+test_expect_success \
+ 'listing tags using a non-matching pattern should output nothing' \
+ 'test $(git tag -l xxx | wc -l) -eq 0'
+
+# special cases for creating tags:
+
+test_expect_success \
+ 'trying to create a tag with the name of one existing should fail' \
+ 'test_must_fail git tag mytag'
+
+test_expect_success \
+ 'trying to create a tag with a non-valid name should fail' '
+ test $(git tag -l | wc -l) -eq 1 &&
+ test_must_fail git tag "" &&
+ test_must_fail git tag .othertag &&
+ test_must_fail git tag "other tag" &&
+ test_must_fail git tag "othertag^" &&
+ test_must_fail git tag "other~tag" &&
+ test $(git tag -l | wc -l) -eq 1
+'
+
+test_expect_success 'creating a tag using HEAD directly should succeed' '
+ git tag myhead HEAD &&
+ tag_exists myhead
+'
+
+test_expect_success '--force can create a tag with the name of one existing' '
+ tag_exists mytag &&
+ git tag --force mytag &&
+ tag_exists mytag'
+
+test_expect_success '--force is moot with a non-existing tag name' '
+ test_when_finished git tag -d newtag forcetag &&
+ git tag newtag >expect &&
+ git tag --force forcetag >actual &&
+ test_cmp expect actual
+'
+
+# deleting tags:
+
+test_expect_success 'trying to delete an unknown tag should fail' '
+ ! tag_exists unknown-tag &&
+ test_must_fail git tag -d unknown-tag
+'
+
+cat >expect <<EOF
+myhead
+mytag
+EOF
+test_expect_success \
+ 'trying to delete tags without params should succeed and do nothing' '
+ git tag -l > actual && test_cmp expect actual &&
+ git tag -d &&
+ git tag -l > actual && test_cmp expect actual
+'
+
+test_expect_success \
+ 'deleting two existing tags in one command should succeed' '
+ tag_exists mytag &&
+ tag_exists myhead &&
+ git tag -d mytag myhead &&
+ ! tag_exists mytag &&
+ ! tag_exists myhead
+'
+
+test_expect_success \
+ 'creating a tag with the name of another deleted one should succeed' '
+ ! tag_exists mytag &&
+ git tag mytag &&
+ tag_exists mytag
+'
+
+test_expect_success \
+ 'trying to delete two tags, existing and not, should fail in the 2nd' '
+ tag_exists mytag &&
+ ! tag_exists nonexistingtag &&
+ test_must_fail git tag -d mytag nonexistingtag &&
+ ! tag_exists mytag &&
+ ! tag_exists nonexistingtag
+'
+
+test_expect_success 'trying to delete an already deleted tag should fail' \
+ 'test_must_fail git tag -d mytag'
+
+# listing various tags with pattern matching:
+
+cat >expect <<EOF
+a1
+aa1
+cba
+t210
+t211
+v0.2.1
+v1.0
+v1.0.1
+v1.1.3
+EOF
+test_expect_success 'listing all tags should print them ordered' '
+ git tag v1.0.1 &&
+ git tag t211 &&
+ git tag aa1 &&
+ git tag v0.2.1 &&
+ git tag v1.1.3 &&
+ git tag cba &&
+ git tag a1 &&
+ git tag v1.0 &&
+ git tag t210 &&
+ git tag -l > actual &&
+ test_cmp expect actual &&
+ git tag > actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<EOF
+a1
+aa1
+cba
+EOF
+test_expect_success \
+ 'listing tags with substring as pattern must print those matching' '
+ rm *a* &&
+ git tag -l "*a*" > current &&
+ test_cmp expect current
+'
+
+cat >expect <<EOF
+v0.2.1
+v1.0.1
+EOF
+test_expect_success \
+ 'listing tags with a suffix as pattern must print those matching' '
+ git tag -l "*.1" > actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<EOF
+t210
+t211
+EOF
+test_expect_success \
+ 'listing tags with a prefix as pattern must print those matching' '
+ git tag -l "t21*" > actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<EOF
+a1
+EOF
+test_expect_success \
+ 'listing tags using a name as pattern must print that one matching' '
+ git tag -l a1 > actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<EOF
+v1.0
+EOF
+test_expect_success \
+ 'listing tags using a name as pattern must print that one matching' '
+ git tag -l v1.0 > actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<EOF
+v1.0.1
+v1.1.3
+EOF
+test_expect_success \
+ 'listing tags with ? in the pattern should print those matching' '
+ git tag -l "v1.?.?" > actual &&
+ test_cmp expect actual
+'
+
+test_expect_success \
+ 'listing tags using v.* should print nothing because none have v.' '
+ git tag -l "v.*" > actual &&
+ test_must_be_empty actual
+'
+
+cat >expect <<EOF
+v0.2.1
+v1.0
+v1.0.1
+v1.1.3
+EOF
+test_expect_success \
+ 'listing tags using v* should print only those having v' '
+ git tag -l "v*" > actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'tag -l can accept multiple patterns' '
+ git tag -l "v1*" "v0*" >actual &&
+ test_cmp expect actual
+'
+
+# Between v1.7.7 & v2.13.0 a fair reading of the git-tag documentation
+# could leave you with the impression that "-l <pattern> -l <pattern>"
+# was how we wanted to accept multiple patterns.
+#
+# This test should not imply that this is a sane thing to support. but
+# since the documentation was worded like it was let's at least find
+# out if we're going to break this long-documented form of taking
+# multiple patterns.
+test_expect_success 'tag -l <pattern> -l <pattern> works, as our buggy documentation previously suggested' '
+ git tag -l "v1*" -l "v0*" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'listing tags in column' '
+ COLUMNS=41 git tag -l --column=row >actual &&
+ cat >expected <<\EOF &&
+a1 aa1 cba t210 t211
+v0.2.1 v1.0 v1.0.1 v1.1.3
+EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'listing tags in column with column.*' '
+ test_config column.tag row &&
+ test_config column.ui dense &&
+ COLUMNS=40 git tag -l >actual &&
+ cat >expected <<\EOF &&
+a1 aa1 cba t210 t211
+v0.2.1 v1.0 v1.0.1 v1.1.3
+EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'listing tag with -n --column should fail' '
+ test_must_fail git tag --column -n
+'
+
+test_expect_success 'listing tags -n in column with column.ui ignored' '
+ test_config column.ui "row dense" &&
+ COLUMNS=40 git tag -l -n >actual &&
+ cat >expected <<\EOF &&
+a1 Foo
+aa1 Foo
+cba Foo
+t210 Foo
+t211 Foo
+v0.2.1 Foo
+v1.0 Foo
+v1.0.1 Foo
+v1.1.3 Foo
+EOF
+ test_cmp expected actual
+'
+
+# creating and verifying lightweight tags:
+
+test_expect_success \
+ 'a non-annotated tag created without parameters should point to HEAD' '
+ git tag non-annotated-tag &&
+ test $(git cat-file -t non-annotated-tag) = commit &&
+ test $(git rev-parse non-annotated-tag) = $(git rev-parse HEAD)
+'
+
+test_expect_success 'trying to verify an unknown tag should fail' \
+ 'test_must_fail git tag -v unknown-tag'
+
+test_expect_success \
+ 'trying to verify a non-annotated and non-signed tag should fail' \
+ 'test_must_fail git tag -v non-annotated-tag'
+
+test_expect_success \
+ 'trying to verify many non-annotated or unknown tags, should fail' \
+ 'test_must_fail git tag -v unknown-tag1 non-annotated-tag unknown-tag2'
+
+# creating annotated tags:
+
+get_tag_msg () {
+ git cat-file tag "$1" | sed -e "/BEGIN PGP/q"
+}
+
+# run test_tick before committing always gives the time in that timezone
+get_tag_header () {
+cat <<EOF
+object $2
+type $3
+tag $1
+tagger C O Mitter <committer@example.com> $4 -0700
+
+EOF
+}
+
+commit=$(git rev-parse HEAD)
+time=$test_tick
+
+get_tag_header annotated-tag $commit commit $time >expect
+echo "A message" >>expect
+test_expect_success \
+ 'creating an annotated tag with -m message should succeed' '
+ git tag -m "A message" annotated-tag &&
+ get_tag_msg annotated-tag >actual &&
+ test_cmp expect actual
+'
+
+get_tag_header annotated-tag-edit $commit commit $time >expect
+echo "An edited message" >>expect
+test_expect_success 'set up editor' '
+ write_script fakeeditor <<-\EOF
+ sed -e "s/A message/An edited message/g" <"$1" >"$1-"
+ mv "$1-" "$1"
+ EOF
+'
+test_expect_success \
+ 'creating an annotated tag with -m message --edit should succeed' '
+ GIT_EDITOR=./fakeeditor git tag -m "A message" --edit annotated-tag-edit &&
+ get_tag_msg annotated-tag-edit >actual &&
+ test_cmp expect actual
+'
+
+cat >msgfile <<EOF
+Another message
+in a file.
+EOF
+get_tag_header file-annotated-tag $commit commit $time >expect
+cat msgfile >>expect
+test_expect_success \
+ 'creating an annotated tag with -F messagefile should succeed' '
+ git tag -F msgfile file-annotated-tag &&
+ get_tag_msg file-annotated-tag >actual &&
+ test_cmp expect actual
+'
+
+get_tag_header file-annotated-tag-edit $commit commit $time >expect
+sed -e "s/Another message/Another edited message/g" msgfile >>expect
+test_expect_success 'set up editor' '
+ write_script fakeeditor <<-\EOF
+ sed -e "s/Another message/Another edited message/g" <"$1" >"$1-"
+ mv "$1-" "$1"
+ EOF
+'
+test_expect_success \
+ 'creating an annotated tag with -F messagefile --edit should succeed' '
+ GIT_EDITOR=./fakeeditor git tag -F msgfile --edit file-annotated-tag-edit &&
+ get_tag_msg file-annotated-tag-edit >actual &&
+ test_cmp expect actual
+'
+
+cat >inputmsg <<EOF
+A message from the
+standard input
+EOF
+get_tag_header stdin-annotated-tag $commit commit $time >expect
+cat inputmsg >>expect
+test_expect_success 'creating an annotated tag with -F - should succeed' '
+ git tag -F - stdin-annotated-tag <inputmsg &&
+ get_tag_msg stdin-annotated-tag >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success \
+ 'trying to create a tag with a non-existing -F file should fail' '
+ ! test -f nonexistingfile &&
+ ! tag_exists notag &&
+ test_must_fail git tag -F nonexistingfile notag &&
+ ! tag_exists notag
+'
+
+test_expect_success \
+ 'trying to create tags giving both -m or -F options should fail' '
+ echo "message file 1" >msgfile1 &&
+ ! tag_exists msgtag &&
+ test_must_fail git tag -m "message 1" -F msgfile1 msgtag &&
+ ! tag_exists msgtag &&
+ test_must_fail git tag -F msgfile1 -m "message 1" msgtag &&
+ ! tag_exists msgtag &&
+ test_must_fail git tag -m "message 1" -F msgfile1 \
+ -m "message 2" msgtag &&
+ ! tag_exists msgtag
+'
+
+# blank and empty messages:
+
+get_tag_header empty-annotated-tag $commit commit $time >expect
+test_expect_success \
+ 'creating a tag with an empty -m message should succeed' '
+ git tag -m "" empty-annotated-tag &&
+ get_tag_msg empty-annotated-tag >actual &&
+ test_cmp expect actual
+'
+
+>emptyfile
+get_tag_header emptyfile-annotated-tag $commit commit $time >expect
+test_expect_success \
+ 'creating a tag with an empty -F messagefile should succeed' '
+ git tag -F emptyfile emptyfile-annotated-tag &&
+ get_tag_msg emptyfile-annotated-tag >actual &&
+ test_cmp expect actual
+'
+
+printf '\n\n \n\t\nLeading blank lines\n' >blanksfile
+printf '\n\t \t \nRepeated blank lines\n' >>blanksfile
+printf '\n\n\nTrailing spaces \t \n' >>blanksfile
+printf '\nTrailing blank lines\n\n\t \n\n' >>blanksfile
+get_tag_header blanks-annotated-tag $commit commit $time >expect
+cat >>expect <<EOF
+Leading blank lines
+
+Repeated blank lines
+
+Trailing spaces
+
+Trailing blank lines
+EOF
+test_expect_success \
+ 'extra blanks in the message for an annotated tag should be removed' '
+ git tag -F blanksfile blanks-annotated-tag &&
+ get_tag_msg blanks-annotated-tag >actual &&
+ test_cmp expect actual
+'
+
+get_tag_header blank-annotated-tag $commit commit $time >expect
+test_expect_success \
+ 'creating a tag with blank -m message with spaces should succeed' '
+ git tag -m " " blank-annotated-tag &&
+ get_tag_msg blank-annotated-tag >actual &&
+ test_cmp expect actual
+'
+
+echo ' ' >blankfile
+echo '' >>blankfile
+echo ' ' >>blankfile
+get_tag_header blankfile-annotated-tag $commit commit $time >expect
+test_expect_success \
+ 'creating a tag with blank -F messagefile with spaces should succeed' '
+ git tag -F blankfile blankfile-annotated-tag &&
+ get_tag_msg blankfile-annotated-tag >actual &&
+ test_cmp expect actual
+'
+
+printf ' ' >blanknonlfile
+get_tag_header blanknonlfile-annotated-tag $commit commit $time >expect
+test_expect_success \
+ 'creating a tag with -F file of spaces and no newline should succeed' '
+ git tag -F blanknonlfile blanknonlfile-annotated-tag &&
+ get_tag_msg blanknonlfile-annotated-tag >actual &&
+ test_cmp expect actual
+'
+
+# messages with commented lines:
+
+cat >commentsfile <<EOF
+# A comment
+
+############
+The message.
+############
+One line.
+
+
+# commented lines
+# commented lines
+
+Another line.
+# comments
+
+Last line.
+EOF
+get_tag_header comments-annotated-tag $commit commit $time >expect
+cat >>expect <<EOF
+The message.
+One line.
+
+Another line.
+
+Last line.
+EOF
+test_expect_success \
+ 'creating a tag using a -F messagefile with #comments should succeed' '
+ git tag -F commentsfile comments-annotated-tag &&
+ get_tag_msg comments-annotated-tag >actual &&
+ test_cmp expect actual
+'
+
+get_tag_header comment-annotated-tag $commit commit $time >expect
+test_expect_success \
+ 'creating a tag with a #comment in the -m message should succeed' '
+ git tag -m "#comment" comment-annotated-tag &&
+ get_tag_msg comment-annotated-tag >actual &&
+ test_cmp expect actual
+'
+
+echo '#comment' >commentfile
+echo '' >>commentfile
+echo '####' >>commentfile
+get_tag_header commentfile-annotated-tag $commit commit $time >expect
+test_expect_success \
+ 'creating a tag with #comments in the -F messagefile should succeed' '
+ git tag -F commentfile commentfile-annotated-tag &&
+ get_tag_msg commentfile-annotated-tag >actual &&
+ test_cmp expect actual
+'
+
+printf '#comment' >commentnonlfile
+get_tag_header commentnonlfile-annotated-tag $commit commit $time >expect
+test_expect_success \
+ 'creating a tag with a file of #comment and no newline should succeed' '
+ git tag -F commentnonlfile commentnonlfile-annotated-tag &&
+ get_tag_msg commentnonlfile-annotated-tag >actual &&
+ test_cmp expect actual
+'
+
+# listing messages for annotated non-signed tags:
+
+test_expect_success \
+ 'listing the one-line message of a non-signed tag should succeed' '
+ git tag -m "A msg" tag-one-line &&
+
+ echo "tag-one-line" >expect &&
+ git tag -l | grep "^tag-one-line" >actual &&
+ test_cmp expect actual &&
+ git tag -n0 -l | grep "^tag-one-line" >actual &&
+ test_cmp expect actual &&
+ git tag -n0 -l tag-one-line >actual &&
+ test_cmp expect actual &&
+
+ git tag -n0 | grep "^tag-one-line" >actual &&
+ test_cmp expect actual &&
+ git tag -n0 tag-one-line >actual &&
+ test_cmp expect actual &&
+
+ echo "tag-one-line A msg" >expect &&
+ git tag -n1 -l | grep "^tag-one-line" >actual &&
+ test_cmp expect actual &&
+ git tag -n -l | grep "^tag-one-line" >actual &&
+ test_cmp expect actual &&
+ git tag -n1 -l tag-one-line >actual &&
+ test_cmp expect actual &&
+ git tag -n2 -l tag-one-line >actual &&
+ test_cmp expect actual &&
+ git tag -n999 -l tag-one-line >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'The -n 100 invocation means -n --list 100, not -n100' '
+ git tag -n 100 >actual &&
+ test_must_be_empty actual &&
+
+ git tag -m "A msg" 100 &&
+ echo "100 A msg" >expect &&
+ git tag -n 100 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success \
+ 'listing the zero-lines message of a non-signed tag should succeed' '
+ git tag -m "" tag-zero-lines &&
+
+ echo "tag-zero-lines" >expect &&
+ git tag -l | grep "^tag-zero-lines" >actual &&
+ test_cmp expect actual &&
+ git tag -n0 -l | grep "^tag-zero-lines" >actual &&
+ test_cmp expect actual &&
+ git tag -n0 -l tag-zero-lines >actual &&
+ test_cmp expect actual &&
+
+ echo "tag-zero-lines " >expect &&
+ git tag -n1 -l | grep "^tag-zero-lines" >actual &&
+ test_cmp expect actual &&
+ git tag -n -l | grep "^tag-zero-lines" >actual &&
+ test_cmp expect actual &&
+ git tag -n1 -l tag-zero-lines >actual &&
+ test_cmp expect actual &&
+ git tag -n2 -l tag-zero-lines >actual &&
+ test_cmp expect actual &&
+ git tag -n999 -l tag-zero-lines >actual &&
+ test_cmp expect actual
+'
+
+echo 'tag line one' >annotagmsg
+echo 'tag line two' >>annotagmsg
+echo 'tag line three' >>annotagmsg
+test_expect_success \
+ 'listing many message lines of a non-signed tag should succeed' '
+ git tag -F annotagmsg tag-lines &&
+
+ echo "tag-lines" >expect &&
+ git tag -l | grep "^tag-lines" >actual &&
+ test_cmp expect actual &&
+ git tag -n0 -l | grep "^tag-lines" >actual &&
+ test_cmp expect actual &&
+ git tag -n0 -l tag-lines >actual &&
+ test_cmp expect actual &&
+
+ echo "tag-lines tag line one" >expect &&
+ git tag -n1 -l | grep "^tag-lines" >actual &&
+ test_cmp expect actual &&
+ git tag -n -l | grep "^tag-lines" >actual &&
+ test_cmp expect actual &&
+ git tag -n1 -l tag-lines >actual &&
+ test_cmp expect actual &&
+
+ echo " tag line two" >>expect &&
+ git tag -n2 -l | grep "^ *tag.line" >actual &&
+ test_cmp expect actual &&
+ git tag -n2 -l tag-lines >actual &&
+ test_cmp expect actual &&
+
+ echo " tag line three" >>expect &&
+ git tag -n3 -l | grep "^ *tag.line" >actual &&
+ test_cmp expect actual &&
+ git tag -n3 -l tag-lines >actual &&
+ test_cmp expect actual &&
+ git tag -n4 -l | grep "^ *tag.line" >actual &&
+ test_cmp expect actual &&
+ git tag -n4 -l tag-lines >actual &&
+ test_cmp expect actual &&
+ git tag -n99 -l | grep "^ *tag.line" >actual &&
+ test_cmp expect actual &&
+ git tag -n99 -l tag-lines >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'annotations for blobs are empty' '
+ blob=$(git hash-object -w --stdin <<-\EOF
+ Blob paragraph 1.
+
+ Blob paragraph 2.
+ EOF
+ ) &&
+ git tag tag-blob $blob &&
+ echo "tag-blob " >expect &&
+ git tag -n1 -l tag-blob >actual &&
+ test_cmp expect actual
+'
+
+# Run this before doing any signing, so the test has the same results
+# regardless of the GPG prereq.
+test_expect_success 'git tag --format with ahead-behind' '
+ test_when_finished git reset --hard tag-one-line &&
+ git commit --allow-empty -m "left" &&
+ git tag -a -m left tag-left &&
+ git reset --hard HEAD~1 &&
+ git commit --allow-empty -m "right" &&
+ git tag -a -m left tag-right &&
+
+ # Use " !" at the end to demonstrate whitespace
+ # around empty ahead-behind token for tag-blob.
+ cat >expect <<-EOF &&
+ refs/tags/tag-blob !
+ refs/tags/tag-left 1 1 !
+ refs/tags/tag-lines 0 1 !
+ refs/tags/tag-one-line 0 1 !
+ refs/tags/tag-right 0 0 !
+ refs/tags/tag-zero-lines 0 1 !
+ EOF
+ git tag -l --format="%(refname) %(ahead-behind:HEAD) !" >actual 2>err &&
+ grep "refs/tags/tag" actual >actual.focus &&
+ test_cmp expect actual.focus &&
+
+ # Error reported for tags that point to non-commits.
+ grep "error: object [0-9a-f]* is a blob, not a commit" err
+'
+
+# trying to verify annotated non-signed tags:
+
+test_expect_success GPG \
+ 'trying to verify an annotated non-signed tag should fail' '
+ tag_exists annotated-tag &&
+ test_must_fail git tag -v annotated-tag
+'
+
+test_expect_success GPG \
+ 'trying to verify a file-annotated non-signed tag should fail' '
+ tag_exists file-annotated-tag &&
+ test_must_fail git tag -v file-annotated-tag
+'
+
+test_expect_success GPG \
+ 'trying to verify two annotated non-signed tags should fail' '
+ tag_exists annotated-tag file-annotated-tag &&
+ test_must_fail git tag -v annotated-tag file-annotated-tag
+'
+
+# creating and verifying signed tags:
+
+get_tag_header signed-tag $commit commit $time >expect
+echo 'A signed tag message' >>expect
+echo '-----BEGIN PGP SIGNATURE-----' >>expect
+test_expect_success GPG 'creating a signed tag with -m message should succeed' '
+ git tag -s -m "A signed tag message" signed-tag &&
+ get_tag_msg signed-tag >actual &&
+ test_cmp expect actual
+'
+
+get_tag_header u-signed-tag $commit commit $time >expect
+echo 'Another message' >>expect
+echo '-----BEGIN PGP SIGNATURE-----' >>expect
+test_expect_success GPG 'sign with a given key id' '
+
+ git tag -u committer@example.com -m "Another message" u-signed-tag &&
+ get_tag_msg u-signed-tag >actual &&
+ test_cmp expect actual
+
+'
+
+test_expect_success GPG 'sign with an unknown id (1)' '
+
+ test_must_fail git tag -u author@example.com \
+ -m "Another message" o-signed-tag
+
+'
+
+test_expect_success GPG 'sign with an unknown id (2)' '
+
+ test_must_fail git tag -u DEADBEEF -m "Another message" o-signed-tag
+
+'
+
+cat >fakeeditor <<'EOF'
+#!/bin/sh
+test -n "$1" && exec >"$1"
+echo A signed tag message
+echo from a fake editor.
+EOF
+chmod +x fakeeditor
+
+get_tag_header implied-sign $commit commit $time >expect
+./fakeeditor >>expect
+echo '-----BEGIN PGP SIGNATURE-----' >>expect
+test_expect_success GPG '-u implies signed tag' '
+ GIT_EDITOR=./fakeeditor git tag -u CDDE430D implied-sign &&
+ get_tag_msg implied-sign >actual &&
+ test_cmp expect actual
+'
+
+cat >sigmsgfile <<EOF
+Another signed tag
+message in a file.
+EOF
+get_tag_header file-signed-tag $commit commit $time >expect
+cat sigmsgfile >>expect
+echo '-----BEGIN PGP SIGNATURE-----' >>expect
+test_expect_success GPG \
+ 'creating a signed tag with -F messagefile should succeed' '
+ git tag -s -F sigmsgfile file-signed-tag &&
+ get_tag_msg file-signed-tag >actual &&
+ test_cmp expect actual
+'
+
+cat >siginputmsg <<EOF
+A signed tag message from
+the standard input
+EOF
+get_tag_header stdin-signed-tag $commit commit $time >expect
+cat siginputmsg >>expect
+echo '-----BEGIN PGP SIGNATURE-----' >>expect
+test_expect_success GPG 'creating a signed tag with -F - should succeed' '
+ git tag -s -F - stdin-signed-tag <siginputmsg &&
+ get_tag_msg stdin-signed-tag >actual &&
+ test_cmp expect actual
+'
+
+get_tag_header implied-annotate $commit commit $time >expect
+./fakeeditor >>expect
+echo '-----BEGIN PGP SIGNATURE-----' >>expect
+test_expect_success GPG '-s implies annotated tag' '
+ GIT_EDITOR=./fakeeditor git tag -s implied-annotate &&
+ get_tag_msg implied-annotate >actual &&
+ test_cmp expect actual
+'
+
+get_tag_header forcesignannotated-implied-sign $commit commit $time >expect
+echo "A message" >>expect
+echo '-----BEGIN PGP SIGNATURE-----' >>expect
+test_expect_success GPG \
+ 'git tag -s implied if configured with tag.forcesignannotated' \
+ 'test_config tag.forcesignannotated true &&
+ git tag -m "A message" forcesignannotated-implied-sign &&
+ get_tag_msg forcesignannotated-implied-sign >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success GPG \
+ 'lightweight with no message when configured with tag.forcesignannotated' \
+ 'test_config tag.forcesignannotated true &&
+ git tag forcesignannotated-lightweight &&
+ tag_exists forcesignannotated-lightweight &&
+ test_must_fail git tag -v forcesignannotated-no-message
+'
+
+get_tag_header forcesignannotated-annotate $commit commit $time >expect
+echo "A message" >>expect
+test_expect_success GPG \
+ 'git tag -a disable configured tag.forcesignannotated' \
+ 'test_config tag.forcesignannotated true &&
+ git tag -a -m "A message" forcesignannotated-annotate &&
+ get_tag_msg forcesignannotated-annotate >actual &&
+ test_cmp expect actual &&
+ test_must_fail git tag -v forcesignannotated-annotate
+'
+
+get_tag_header forcesignannotated-disabled $commit commit $time >expect
+echo "A message" >>expect
+echo '-----BEGIN PGP SIGNATURE-----' >>expect
+test_expect_success GPG \
+ 'git tag --sign enable GPG sign' \
+ 'test_config tag.forcesignannotated false &&
+ git tag --sign -m "A message" forcesignannotated-disabled &&
+ get_tag_msg forcesignannotated-disabled >actual &&
+ test_cmp expect actual
+'
+
+get_tag_header gpgsign-enabled $commit commit $time >expect
+echo "A message" >>expect
+echo '-----BEGIN PGP SIGNATURE-----' >>expect
+test_expect_success GPG \
+ 'git tag configured tag.gpgsign enables GPG sign' \
+ 'test_config tag.gpgsign true &&
+ git tag -m "A message" gpgsign-enabled &&
+ get_tag_msg gpgsign-enabled>actual &&
+ test_cmp expect actual
+'
+
+get_tag_header no-sign $commit commit $time >expect
+echo "A message" >>expect
+test_expect_success GPG \
+ 'git tag --no-sign configured tag.gpgsign skip GPG sign' \
+ 'test_config tag.gpgsign true &&
+ git tag -a --no-sign -m "A message" no-sign &&
+ get_tag_msg no-sign>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success GPG \
+ 'trying to create a signed tag with non-existing -F file should fail' '
+ ! test -f nonexistingfile &&
+ ! tag_exists nosigtag &&
+ test_must_fail git tag -s -F nonexistingfile nosigtag &&
+ ! tag_exists nosigtag
+'
+
+test_expect_success GPG 'verifying a signed tag should succeed' \
+ 'git tag -v signed-tag'
+
+test_expect_success GPG 'verifying two signed tags in one command should succeed' \
+ 'git tag -v signed-tag file-signed-tag'
+
+test_expect_success GPG \
+ 'verifying many signed and non-signed tags should fail' '
+ test_must_fail git tag -v signed-tag annotated-tag &&
+ test_must_fail git tag -v file-annotated-tag file-signed-tag &&
+ test_must_fail git tag -v annotated-tag \
+ file-signed-tag file-annotated-tag &&
+ test_must_fail git tag -v signed-tag annotated-tag file-signed-tag
+'
+
+test_expect_success GPG 'verifying a forged tag should fail' '
+ forged=$(git cat-file tag signed-tag |
+ sed -e "s/signed-tag/forged-tag/" |
+ git mktag) &&
+ git tag forged-tag $forged &&
+ test_must_fail git tag -v forged-tag
+'
+
+test_expect_success GPG 'verifying a proper tag with --format pass and format accordingly' '
+ cat >expect <<-\EOF &&
+ tagname : signed-tag
+ EOF
+ git tag -v --format="tagname : %(tag)" "signed-tag" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success GPG 'verifying a forged tag with --format should fail silently' '
+ test_must_fail git tag -v --format="tagname : %(tag)" "forged-tag" >actual &&
+ test_must_be_empty actual
+'
+
+# blank and empty messages for signed tags:
+
+get_tag_header empty-signed-tag $commit commit $time >expect
+echo '-----BEGIN PGP SIGNATURE-----' >>expect
+test_expect_success GPG \
+ 'creating a signed tag with an empty -m message should succeed' '
+ git tag -s -m "" empty-signed-tag &&
+ get_tag_msg empty-signed-tag >actual &&
+ test_cmp expect actual &&
+ git tag -v empty-signed-tag
+'
+
+>sigemptyfile
+get_tag_header emptyfile-signed-tag $commit commit $time >expect
+echo '-----BEGIN PGP SIGNATURE-----' >>expect
+test_expect_success GPG \
+ 'creating a signed tag with an empty -F messagefile should succeed' '
+ git tag -s -F sigemptyfile emptyfile-signed-tag &&
+ get_tag_msg emptyfile-signed-tag >actual &&
+ test_cmp expect actual &&
+ git tag -v emptyfile-signed-tag
+'
+
+printf '\n\n \n\t\nLeading blank lines\n' > sigblanksfile
+printf '\n\t \t \nRepeated blank lines\n' >>sigblanksfile
+printf '\n\n\nTrailing spaces \t \n' >>sigblanksfile
+printf '\nTrailing blank lines\n\n\t \n\n' >>sigblanksfile
+get_tag_header blanks-signed-tag $commit commit $time >expect
+cat >>expect <<EOF
+Leading blank lines
+
+Repeated blank lines
+
+Trailing spaces
+
+Trailing blank lines
+EOF
+echo '-----BEGIN PGP SIGNATURE-----' >>expect
+test_expect_success GPG \
+ 'extra blanks in the message for a signed tag should be removed' '
+ git tag -s -F sigblanksfile blanks-signed-tag &&
+ get_tag_msg blanks-signed-tag >actual &&
+ test_cmp expect actual &&
+ git tag -v blanks-signed-tag
+'
+
+get_tag_header blank-signed-tag $commit commit $time >expect
+echo '-----BEGIN PGP SIGNATURE-----' >>expect
+test_expect_success GPG \
+ 'creating a signed tag with a blank -m message should succeed' '
+ git tag -s -m " " blank-signed-tag &&
+ get_tag_msg blank-signed-tag >actual &&
+ test_cmp expect actual &&
+ git tag -v blank-signed-tag
+'
+
+echo ' ' >sigblankfile
+echo '' >>sigblankfile
+echo ' ' >>sigblankfile
+get_tag_header blankfile-signed-tag $commit commit $time >expect
+echo '-----BEGIN PGP SIGNATURE-----' >>expect
+test_expect_success GPG \
+ 'creating a signed tag with blank -F file with spaces should succeed' '
+ git tag -s -F sigblankfile blankfile-signed-tag &&
+ get_tag_msg blankfile-signed-tag >actual &&
+ test_cmp expect actual &&
+ git tag -v blankfile-signed-tag
+'
+
+printf ' ' >sigblanknonlfile
+get_tag_header blanknonlfile-signed-tag $commit commit $time >expect
+echo '-----BEGIN PGP SIGNATURE-----' >>expect
+test_expect_success GPG \
+ 'creating a signed tag with spaces and no newline should succeed' '
+ git tag -s -F sigblanknonlfile blanknonlfile-signed-tag &&
+ get_tag_msg blanknonlfile-signed-tag >actual &&
+ test_cmp expect actual &&
+ git tag -v blanknonlfile-signed-tag
+'
+
+test_expect_success GPG 'signed tag with embedded PGP message' '
+ cat >msg <<-\EOF &&
+ -----BEGIN PGP MESSAGE-----
+
+ this is not a real PGP message
+ -----END PGP MESSAGE-----
+ EOF
+ git tag -s -F msg confusing-pgp-message &&
+ git tag -v confusing-pgp-message
+'
+
+# messages with commented lines for signed tags:
+
+cat >sigcommentsfile <<EOF
+# A comment
+
+############
+The message.
+############
+One line.
+
+
+# commented lines
+# commented lines
+
+Another line.
+# comments
+
+Last line.
+EOF
+get_tag_header comments-signed-tag $commit commit $time >expect
+cat >>expect <<EOF
+The message.
+One line.
+
+Another line.
+
+Last line.
+EOF
+echo '-----BEGIN PGP SIGNATURE-----' >>expect
+test_expect_success GPG \
+ 'creating a signed tag with a -F file with #comments should succeed' '
+ git tag -s -F sigcommentsfile comments-signed-tag &&
+ get_tag_msg comments-signed-tag >actual &&
+ test_cmp expect actual &&
+ git tag -v comments-signed-tag
+'
+
+get_tag_header comment-signed-tag $commit commit $time >expect
+echo '-----BEGIN PGP SIGNATURE-----' >>expect
+test_expect_success GPG \
+ 'creating a signed tag with #commented -m message should succeed' '
+ git tag -s -m "#comment" comment-signed-tag &&
+ get_tag_msg comment-signed-tag >actual &&
+ test_cmp expect actual &&
+ git tag -v comment-signed-tag
+'
+
+echo '#comment' >sigcommentfile
+echo '' >>sigcommentfile
+echo '####' >>sigcommentfile
+get_tag_header commentfile-signed-tag $commit commit $time >expect
+echo '-----BEGIN PGP SIGNATURE-----' >>expect
+test_expect_success GPG \
+ 'creating a signed tag with #commented -F messagefile should succeed' '
+ git tag -s -F sigcommentfile commentfile-signed-tag &&
+ get_tag_msg commentfile-signed-tag >actual &&
+ test_cmp expect actual &&
+ git tag -v commentfile-signed-tag
+'
+
+printf '#comment' >sigcommentnonlfile
+get_tag_header commentnonlfile-signed-tag $commit commit $time >expect
+echo '-----BEGIN PGP SIGNATURE-----' >>expect
+test_expect_success GPG \
+ 'creating a signed tag with a #comment and no newline should succeed' '
+ git tag -s -F sigcommentnonlfile commentnonlfile-signed-tag &&
+ get_tag_msg commentnonlfile-signed-tag >actual &&
+ test_cmp expect actual &&
+ git tag -v commentnonlfile-signed-tag
+'
+
+# listing messages for signed tags:
+
+test_expect_success GPG \
+ 'listing the one-line message of a signed tag should succeed' '
+ git tag -s -m "A message line signed" stag-one-line &&
+
+ echo "stag-one-line" >expect &&
+ git tag -l | grep "^stag-one-line" >actual &&
+ test_cmp expect actual &&
+ git tag -n0 -l | grep "^stag-one-line" >actual &&
+ test_cmp expect actual &&
+ git tag -n0 -l stag-one-line >actual &&
+ test_cmp expect actual &&
+
+ echo "stag-one-line A message line signed" >expect &&
+ git tag -n1 -l | grep "^stag-one-line" >actual &&
+ test_cmp expect actual &&
+ git tag -n -l | grep "^stag-one-line" >actual &&
+ test_cmp expect actual &&
+ git tag -n1 -l stag-one-line >actual &&
+ test_cmp expect actual &&
+ git tag -n2 -l stag-one-line >actual &&
+ test_cmp expect actual &&
+ git tag -n999 -l stag-one-line >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success GPG \
+ 'listing the zero-lines message of a signed tag should succeed' '
+ git tag -s -m "" stag-zero-lines &&
+
+ echo "stag-zero-lines" >expect &&
+ git tag -l | grep "^stag-zero-lines" >actual &&
+ test_cmp expect actual &&
+ git tag -n0 -l | grep "^stag-zero-lines" >actual &&
+ test_cmp expect actual &&
+ git tag -n0 -l stag-zero-lines >actual &&
+ test_cmp expect actual &&
+
+ echo "stag-zero-lines " >expect &&
+ git tag -n1 -l | grep "^stag-zero-lines" >actual &&
+ test_cmp expect actual &&
+ git tag -n -l | grep "^stag-zero-lines" >actual &&
+ test_cmp expect actual &&
+ git tag -n1 -l stag-zero-lines >actual &&
+ test_cmp expect actual &&
+ git tag -n2 -l stag-zero-lines >actual &&
+ test_cmp expect actual &&
+ git tag -n999 -l stag-zero-lines >actual &&
+ test_cmp expect actual
+'
+
+echo 'stag line one' >sigtagmsg
+echo 'stag line two' >>sigtagmsg
+echo 'stag line three' >>sigtagmsg
+test_expect_success GPG \
+ 'listing many message lines of a signed tag should succeed' '
+ git tag -s -F sigtagmsg stag-lines &&
+
+ echo "stag-lines" >expect &&
+ git tag -l | grep "^stag-lines" >actual &&
+ test_cmp expect actual &&
+ git tag -n0 -l | grep "^stag-lines" >actual &&
+ test_cmp expect actual &&
+ git tag -n0 -l stag-lines >actual &&
+ test_cmp expect actual &&
+
+ echo "stag-lines stag line one" >expect &&
+ git tag -n1 -l | grep "^stag-lines" >actual &&
+ test_cmp expect actual &&
+ git tag -n -l | grep "^stag-lines" >actual &&
+ test_cmp expect actual &&
+ git tag -n1 -l stag-lines >actual &&
+ test_cmp expect actual &&
+
+ echo " stag line two" >>expect &&
+ git tag -n2 -l | grep "^ *stag.line" >actual &&
+ test_cmp expect actual &&
+ git tag -n2 -l stag-lines >actual &&
+ test_cmp expect actual &&
+
+ echo " stag line three" >>expect &&
+ git tag -n3 -l | grep "^ *stag.line" >actual &&
+ test_cmp expect actual &&
+ git tag -n3 -l stag-lines >actual &&
+ test_cmp expect actual &&
+ git tag -n4 -l | grep "^ *stag.line" >actual &&
+ test_cmp expect actual &&
+ git tag -n4 -l stag-lines >actual &&
+ test_cmp expect actual &&
+ git tag -n99 -l | grep "^ *stag.line" >actual &&
+ test_cmp expect actual &&
+ git tag -n99 -l stag-lines >actual &&
+ test_cmp expect actual
+'
+
+# tags pointing to objects different from commits:
+
+tree=$(git rev-parse HEAD^{tree})
+blob=$(git rev-parse HEAD:foo)
+tag=$(git rev-parse signed-tag 2>/dev/null)
+
+get_tag_header tree-signed-tag $tree tree $time >expect
+echo "A message for a tree" >>expect
+echo '-----BEGIN PGP SIGNATURE-----' >>expect
+test_expect_success GPG \
+ 'creating a signed tag pointing to a tree should succeed' '
+ git tag -s -m "A message for a tree" tree-signed-tag HEAD^{tree} &&
+ get_tag_msg tree-signed-tag >actual &&
+ test_cmp expect actual
+'
+
+get_tag_header blob-signed-tag $blob blob $time >expect
+echo "A message for a blob" >>expect
+echo '-----BEGIN PGP SIGNATURE-----' >>expect
+test_expect_success GPG \
+ 'creating a signed tag pointing to a blob should succeed' '
+ git tag -s -m "A message for a blob" blob-signed-tag HEAD:foo &&
+ get_tag_msg blob-signed-tag >actual &&
+ test_cmp expect actual
+'
+
+get_tag_header tag-signed-tag $tag tag $time >expect
+echo "A message for another tag" >>expect
+echo '-----BEGIN PGP SIGNATURE-----' >>expect
+test_expect_success GPG \
+ 'creating a signed tag pointing to another tag should succeed' '
+ git tag -s -m "A message for another tag" tag-signed-tag signed-tag &&
+ get_tag_msg tag-signed-tag >actual &&
+ test_cmp expect actual
+'
+
+# usage with rfc1991 signatures
+get_tag_header rfc1991-signed-tag $commit commit $time >expect
+echo "RFC1991 signed tag" >>expect
+echo '-----BEGIN PGP MESSAGE-----' >>expect
+test_expect_success GPG,RFC1991 \
+ 'creating a signed tag with rfc1991' '
+ echo "rfc1991" >gpghome/gpg.conf &&
+ git tag -s -m "RFC1991 signed tag" rfc1991-signed-tag $commit &&
+ get_tag_msg rfc1991-signed-tag >actual &&
+ test_cmp expect actual
+'
+
+cat >fakeeditor <<'EOF'
+#!/bin/sh
+cp "$1" actual
+EOF
+chmod +x fakeeditor
+
+test_expect_success GPG,RFC1991 \
+ 'reediting a signed tag body omits signature' '
+ echo "rfc1991" >gpghome/gpg.conf &&
+ echo "RFC1991 signed tag" >expect &&
+ GIT_EDITOR=./fakeeditor git tag -f -s rfc1991-signed-tag $commit &&
+ test_cmp expect actual
+'
+
+test_expect_success GPG,RFC1991 \
+ 'verifying rfc1991 signature' '
+ echo "rfc1991" >gpghome/gpg.conf &&
+ git tag -v rfc1991-signed-tag
+'
+
+test_expect_success GPG,RFC1991 \
+ 'list tag with rfc1991 signature' '
+ echo "rfc1991" >gpghome/gpg.conf &&
+ echo "rfc1991-signed-tag RFC1991 signed tag" >expect &&
+ git tag -l -n1 rfc1991-signed-tag >actual &&
+ test_cmp expect actual &&
+ git tag -l -n2 rfc1991-signed-tag >actual &&
+ test_cmp expect actual &&
+ git tag -l -n999 rfc1991-signed-tag >actual &&
+ test_cmp expect actual
+'
+
+rm -f gpghome/gpg.conf
+
+test_expect_success GPG,RFC1991 \
+ 'verifying rfc1991 signature without --rfc1991' '
+ git tag -v rfc1991-signed-tag
+'
+
+test_expect_success GPG,RFC1991 \
+ 'list tag with rfc1991 signature without --rfc1991' '
+ echo "rfc1991-signed-tag RFC1991 signed tag" >expect &&
+ git tag -l -n1 rfc1991-signed-tag >actual &&
+ test_cmp expect actual &&
+ git tag -l -n2 rfc1991-signed-tag >actual &&
+ test_cmp expect actual &&
+ git tag -l -n999 rfc1991-signed-tag >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success GPG,RFC1991 \
+ 'reediting a signed tag body omits signature' '
+ echo "RFC1991 signed tag" >expect &&
+ GIT_EDITOR=./fakeeditor git tag -f -s rfc1991-signed-tag $commit &&
+ test_cmp expect actual
+'
+
+# try to sign with bad user.signingkey
+test_expect_success GPG \
+ 'git tag -s fails if gpg is misconfigured (bad key)' \
+ 'test_config user.signingkey BobTheMouse &&
+ test_must_fail git tag -s -m tail tag-gpg-failure'
+
+# try to produce invalid signature
+test_expect_success GPG \
+ 'git tag -s fails if gpg is misconfigured (bad signature format)' \
+ 'test_config gpg.program echo &&
+ test_must_fail git tag -s -m tail tag-gpg-failure'
+
+# try to produce invalid signature
+test_expect_success GPG 'git verifies tag is valid with double signature' '
+ git tag -s -m tail tag-gpg-double-sig &&
+ git cat-file tag tag-gpg-double-sig >tag &&
+ othersigheader=$(test_oid othersigheader) &&
+ sed -ne "/^\$/q;p" tag >new-tag &&
+ cat <<-EOM >>new-tag &&
+ $othersigheader -----BEGIN PGP SIGNATURE-----
+ someinvaliddata
+ -----END PGP SIGNATURE-----
+ EOM
+ sed -e "1,/^tagger/d" tag >>new-tag &&
+ new_tag=$(git hash-object -t tag -w new-tag) &&
+ git update-ref refs/tags/tag-gpg-double-sig $new_tag &&
+ git verify-tag tag-gpg-double-sig &&
+ git fsck
+'
+
+# try to sign with bad user.signingkey
+test_expect_success GPGSM \
+ 'git tag -s fails if gpgsm is misconfigured (bad key)' \
+ 'test_config user.signingkey BobTheMouse &&
+ test_config gpg.format x509 &&
+ test_must_fail git tag -s -m tail tag-gpg-failure'
+
+# try to produce invalid signature
+test_expect_success GPGSM \
+ 'git tag -s fails if gpgsm is misconfigured (bad signature format)' \
+ 'test_config gpg.x509.program echo &&
+ test_config gpg.format x509 &&
+ test_must_fail git tag -s -m tail tag-gpg-failure'
+
+# try to verify without gpg:
+
+rm -rf gpghome
+test_expect_success GPG \
+ 'verify signed tag fails when public key is not present' \
+ 'test_must_fail git tag -v signed-tag'
+
+test_expect_success \
+ 'git tag -a fails if tag annotation is empty' '
+ ! (GIT_EDITOR=cat git tag -a initial-comment)
+'
+
+test_expect_success \
+ 'message in editor has initial comment' '
+ ! (GIT_EDITOR=cat git tag -a initial-comment > actual)
+'
+
+test_expect_success 'message in editor has initial comment: first line' '
+ # check the first line --- should be empty
+ echo >first.expect &&
+ sed -e 1q <actual >first.actual &&
+ test_cmp first.expect first.actual
+'
+
+test_expect_success \
+ 'message in editor has initial comment: remainder' '
+ # remove commented lines from the remainder -- should be empty
+ sed -e 1d -e "/^#/d" <actual >rest.actual &&
+ test_must_be_empty rest.actual
+'
+
+get_tag_header reuse $commit commit $time >expect
+echo "An annotation to be reused" >> expect
+test_expect_success \
+ 'overwriting an annotated tag should use its previous body' '
+ git tag -a -m "An annotation to be reused" reuse &&
+ GIT_EDITOR=true git tag -f -a reuse &&
+ get_tag_msg reuse >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'filename for the message is relative to cwd' '
+ mkdir subdir &&
+ echo "Tag message in top directory" >msgfile-5 &&
+ echo "Tag message in sub directory" >subdir/msgfile-5 &&
+ (
+ cd subdir &&
+ git tag -a -F msgfile-5 tag-from-subdir
+ ) &&
+ git cat-file tag tag-from-subdir | grep "in sub directory"
+'
+
+test_expect_success 'filename for the message is relative to cwd' '
+ echo "Tag message in sub directory" >subdir/msgfile-6 &&
+ (
+ cd subdir &&
+ git tag -a -F msgfile-6 tag-from-subdir-2
+ ) &&
+ git cat-file tag tag-from-subdir-2 | grep "in sub directory"
+'
+
+# create a few more commits to test --contains
+
+hash1=$(git rev-parse HEAD)
+
+test_expect_success 'creating second commit and tag' '
+ echo foo-2.0 >foo &&
+ git add foo &&
+ git commit -m second &&
+ git tag v2.0
+'
+
+hash2=$(git rev-parse HEAD)
+
+test_expect_success 'creating third commit without tag' '
+ echo foo-dev >foo &&
+ git add foo &&
+ git commit -m third
+'
+
+hash3=$(git rev-parse HEAD)
+
+# simple linear checks of --continue
+
+cat > expected <<EOF
+v0.2.1
+v1.0
+v1.0.1
+v1.1.3
+v2.0
+EOF
+
+test_expect_success 'checking that first commit is in all tags (hash)' "
+ git tag -l --contains $hash1 v* >actual &&
+ test_cmp expected actual
+"
+
+# other ways of specifying the commit
+test_expect_success 'checking that first commit is in all tags (tag)' "
+ git tag -l --contains v1.0 v* >actual &&
+ test_cmp expected actual
+"
+
+test_expect_success 'checking that first commit is in all tags (relative)' "
+ git tag -l --contains HEAD~2 v* >actual &&
+ test_cmp expected actual
+"
+
+# All the --contains tests above, but with --no-contains
+test_expect_success 'checking that first commit is not listed in any tag with --no-contains (hash)' "
+ git tag -l --no-contains $hash1 v* >actual &&
+ test_must_be_empty actual
+"
+
+test_expect_success 'checking that first commit is in all tags (tag)' "
+ git tag -l --no-contains v1.0 v* >actual &&
+ test_must_be_empty actual
+"
+
+test_expect_success 'checking that first commit is in all tags (relative)' "
+ git tag -l --no-contains HEAD~2 v* >actual &&
+ test_must_be_empty actual
+"
+
+cat > expected <<EOF
+v2.0
+EOF
+
+test_expect_success 'checking that second commit only has one tag' "
+ git tag -l --contains $hash2 v* >actual &&
+ test_cmp expected actual
+"
+
+cat > expected <<EOF
+v0.2.1
+v1.0
+v1.0.1
+v1.1.3
+EOF
+
+test_expect_success 'inverse of the last test, with --no-contains' "
+ git tag -l --no-contains $hash2 v* >actual &&
+ test_cmp expected actual
+"
+
+test_expect_success 'checking that third commit has no tags' "
+ git tag -l --contains $hash3 v* >actual &&
+ test_must_be_empty actual
+"
+
+cat > expected <<EOF
+v0.2.1
+v1.0
+v1.0.1
+v1.1.3
+v2.0
+EOF
+
+test_expect_success 'conversely --no-contains on the third commit lists all tags' "
+ git tag -l --no-contains $hash3 v* >actual &&
+ test_cmp expected actual
+"
+
+# how about a simple merge?
+
+test_expect_success 'creating simple branch' '
+ git branch stable v2.0 &&
+ git checkout stable &&
+ echo foo-3.0 > foo &&
+ git commit foo -m fourth &&
+ git tag v3.0
+'
+
+hash4=$(git rev-parse HEAD)
+
+cat > expected <<EOF
+v3.0
+EOF
+
+test_expect_success 'checking that branch head only has one tag' "
+ git tag -l --contains $hash4 v* >actual &&
+ test_cmp expected actual
+"
+
+cat > expected <<EOF
+v0.2.1
+v1.0
+v1.0.1
+v1.1.3
+v2.0
+EOF
+
+test_expect_success 'checking that branch head with --no-contains lists all but one tag' "
+ git tag -l --no-contains $hash4 v* >actual &&
+ test_cmp expected actual
+"
+
+test_expect_success 'merging original branch into this branch' '
+ git merge --strategy=ours main &&
+ git tag v4.0
+'
+
+cat > expected <<EOF
+v4.0
+EOF
+
+test_expect_success 'checking that original branch head has one tag now' "
+ git tag -l --contains $hash3 v* >actual &&
+ test_cmp expected actual
+"
+
+cat > expected <<EOF
+v0.2.1
+v1.0
+v1.0.1
+v1.1.3
+v2.0
+v3.0
+EOF
+
+test_expect_success 'checking that original branch head with --no-contains lists all but one tag now' "
+ git tag -l --no-contains $hash3 v* >actual &&
+ test_cmp expected actual
+"
+
+cat > expected <<EOF
+v0.2.1
+v1.0
+v1.0.1
+v1.1.3
+v2.0
+v3.0
+v4.0
+EOF
+
+test_expect_success 'checking that initial commit is in all tags' "
+ git tag -l --contains $hash1 v* >actual &&
+ test_cmp expected actual
+"
+
+test_expect_success 'checking that --contains can be used in non-list mode' '
+ git tag --contains $hash1 v* >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'checking that initial commit is in all tags with --no-contains' "
+ git tag -l --no-contains $hash1 v* >actual &&
+ test_must_be_empty actual
+"
+
+# mixing modes and options:
+
+test_expect_success 'mixing incompatibles modes and options is forbidden' '
+ test_must_fail git tag -a &&
+ test_must_fail git tag -a -l &&
+ test_must_fail git tag -s &&
+ test_must_fail git tag -s -l &&
+ test_must_fail git tag -m &&
+ test_must_fail git tag -m -l &&
+ test_must_fail git tag -m "hlagh" &&
+ test_must_fail git tag -m "hlagh" -l &&
+ test_must_fail git tag -F &&
+ test_must_fail git tag -F -l &&
+ test_must_fail git tag -f &&
+ test_must_fail git tag -f -l &&
+ test_must_fail git tag -a -s -m -F &&
+ test_must_fail git tag -a -s -m -F -l &&
+ test_must_fail git tag -l -v &&
+ test_must_fail git tag -l -d &&
+ test_must_fail git tag -l -v -d &&
+ test_must_fail git tag -n 100 -v &&
+ test_must_fail git tag -l -m msg &&
+ test_must_fail git tag -l -F some file &&
+ test_must_fail git tag -v -s &&
+ test_must_fail git tag --contains tag-tree &&
+ test_must_fail git tag --contains tag-blob &&
+ test_must_fail git tag --no-contains tag-tree &&
+ test_must_fail git tag --no-contains tag-blob &&
+ test_must_fail git tag --contains --no-contains &&
+ test_must_fail git tag --no-with HEAD &&
+ test_must_fail git tag --no-without HEAD
+'
+
+for option in --contains --with --no-contains --without --merged --no-merged --points-at
+do
+ test_expect_success "mixing incompatible modes with $option is forbidden" "
+ test_must_fail git tag -d $option HEAD &&
+ test_must_fail git tag -d $option HEAD some-tag &&
+ test_must_fail git tag -v $option HEAD
+ "
+ test_expect_success "Doing 'git tag --list-like $option <commit> <pattern> is permitted" "
+ git tag -n $option HEAD HEAD &&
+ git tag $option HEAD HEAD &&
+ git tag $option
+ "
+done
+
+# check points-at
+
+test_expect_success '--points-at can be used in non-list mode' '
+ echo v4.0 >expect &&
+ git tag --points-at=v4.0 "v*" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--points-at is a synonym for --points-at HEAD' '
+ echo v4.0 >expect &&
+ git tag --points-at >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--points-at finds lightweight tags' '
+ echo v4.0 >expect &&
+ git tag --points-at v4.0 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--points-at finds annotated tags of commits' '
+ git tag -m "v4.0, annotated" annotated-v4.0 v4.0 &&
+ echo annotated-v4.0 >expect &&
+ git tag -l --points-at v4.0 "annotated*" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--points-at finds annotated tags of tags' '
+ git tag -m "describing the v4.0 tag object" \
+ annotated-again-v4.0 annotated-v4.0 &&
+ cat >expect <<-\EOF &&
+ annotated-again-v4.0
+ annotated-v4.0
+ EOF
+ git tag --points-at=annotated-v4.0 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'recursive tagging should give advice' '
+ sed -e "s/|$//" <<-EOF >expect &&
+ hint: You have created a nested tag. The object referred to by your new tag is
+ hint: already a tag. If you meant to tag the object that it points to, use:
+ hint: |
+ hint: git tag -f nested annotated-v4.0^{}
+ hint: Disable this message with "git config advice.nestedTag false"
+ EOF
+ git tag -m nested nested annotated-v4.0 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'multiple --points-at are OR-ed together' '
+ cat >expect <<-\EOF &&
+ v2.0
+ v3.0
+ EOF
+ git tag --points-at=v2.0 --points-at=v3.0 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'lexical sort' '
+ git tag foo1.3 &&
+ git tag foo1.6 &&
+ git tag foo1.10 &&
+ git tag -l --sort=refname "foo*" >actual &&
+ cat >expect <<-\EOF &&
+ foo1.10
+ foo1.3
+ foo1.6
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'version sort' '
+ git tag -l --sort=version:refname "foo*" >actual &&
+ cat >expect <<-\EOF &&
+ foo1.3
+ foo1.6
+ foo1.10
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'reverse version sort' '
+ git tag -l --sort=-version:refname "foo*" >actual &&
+ cat >expect <<-\EOF &&
+ foo1.10
+ foo1.6
+ foo1.3
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'reverse lexical sort' '
+ git tag -l --sort=-refname "foo*" >actual &&
+ cat >expect <<-\EOF &&
+ foo1.6
+ foo1.3
+ foo1.10
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'configured lexical sort' '
+ test_config tag.sort "v:refname" &&
+ git tag -l "foo*" >actual &&
+ cat >expect <<-\EOF &&
+ foo1.3
+ foo1.6
+ foo1.10
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'option override configured sort' '
+ test_config tag.sort "v:refname" &&
+ git tag -l --sort=-refname "foo*" >actual &&
+ cat >expect <<-\EOF &&
+ foo1.6
+ foo1.3
+ foo1.10
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'invalid sort parameter on command line' '
+ test_must_fail git tag -l --sort=notvalid "foo*" >actual
+'
+
+test_expect_success 'invalid sort parameter in configuratoin' '
+ test_config tag.sort "v:notvalid" &&
+ test_must_fail git tag -l "foo*"
+'
+
+test_expect_success 'version sort handles empty value for versionsort.{prereleaseSuffix,suffix}' '
+ cp .git/config .git/config.orig &&
+ test_when_finished mv .git/config.orig .git/config &&
+
+ cat >>.git/config <<-\EOF &&
+ [versionsort]
+ prereleaseSuffix
+ suffix
+ EOF
+ cat >expect <<-\EOF &&
+ error: missing value for '\''versionsort.suffix'\''
+ error: missing value for '\''versionsort.prereleasesuffix'\''
+ EOF
+ git tag -l --sort=version:refname 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'version sort with prerelease reordering' '
+ test_config versionsort.prereleaseSuffix -rc &&
+ git tag foo1.6-rc1 &&
+ git tag foo1.6-rc2 &&
+ git tag -l --sort=version:refname "foo*" >actual &&
+ cat >expect <<-\EOF &&
+ foo1.3
+ foo1.6-rc1
+ foo1.6-rc2
+ foo1.6
+ foo1.10
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'reverse version sort with prerelease reordering' '
+ test_config versionsort.prereleaseSuffix -rc &&
+ git tag -l --sort=-version:refname "foo*" >actual &&
+ cat >expect <<-\EOF &&
+ foo1.10
+ foo1.6
+ foo1.6-rc2
+ foo1.6-rc1
+ foo1.3
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'version sort with prerelease reordering and common leading character' '
+ test_config versionsort.prereleaseSuffix -before &&
+ git tag foo1.7-before1 &&
+ git tag foo1.7 &&
+ git tag foo1.7-after1 &&
+ git tag -l --sort=version:refname "foo1.7*" >actual &&
+ cat >expect <<-\EOF &&
+ foo1.7-before1
+ foo1.7
+ foo1.7-after1
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'version sort with prerelease reordering, multiple suffixes and common leading character' '
+ test_config versionsort.prereleaseSuffix -before &&
+ git config --add versionsort.prereleaseSuffix -after &&
+ git tag -l --sort=version:refname "foo1.7*" >actual &&
+ cat >expect <<-\EOF &&
+ foo1.7-before1
+ foo1.7-after1
+ foo1.7
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'version sort with prerelease reordering, multiple suffixes match the same tag' '
+ test_config versionsort.prereleaseSuffix -bar &&
+ git config --add versionsort.prereleaseSuffix -foo-baz &&
+ git config --add versionsort.prereleaseSuffix -foo-bar &&
+ git tag foo1.8-foo-bar &&
+ git tag foo1.8-foo-baz &&
+ git tag foo1.8 &&
+ git tag -l --sort=version:refname "foo1.8*" >actual &&
+ cat >expect <<-\EOF &&
+ foo1.8-foo-baz
+ foo1.8-foo-bar
+ foo1.8
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'version sort with prerelease reordering, multiple suffixes match starting at the same position' '
+ test_config versionsort.prereleaseSuffix -pre &&
+ git config --add versionsort.prereleaseSuffix -prerelease &&
+ git tag foo1.9-pre1 &&
+ git tag foo1.9-pre2 &&
+ git tag foo1.9-prerelease1 &&
+ git tag -l --sort=version:refname "foo1.9*" >actual &&
+ cat >expect <<-\EOF &&
+ foo1.9-pre1
+ foo1.9-pre2
+ foo1.9-prerelease1
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'version sort with general suffix reordering' '
+ test_config versionsort.suffix -alpha &&
+ git config --add versionsort.suffix -beta &&
+ git config --add versionsort.suffix "" &&
+ git config --add versionsort.suffix -gamma &&
+ git config --add versionsort.suffix -delta &&
+ git tag foo1.10-alpha &&
+ git tag foo1.10-beta &&
+ git tag foo1.10-gamma &&
+ git tag foo1.10-delta &&
+ git tag foo1.10-unlisted-suffix &&
+ git tag -l --sort=version:refname "foo1.10*" >actual &&
+ cat >expect <<-\EOF &&
+ foo1.10-alpha
+ foo1.10-beta
+ foo1.10
+ foo1.10-unlisted-suffix
+ foo1.10-gamma
+ foo1.10-delta
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'versionsort.suffix overrides versionsort.prereleaseSuffix' '
+ test_config versionsort.suffix -before &&
+ test_config versionsort.prereleaseSuffix -after &&
+ git tag -l --sort=version:refname "foo1.7*" >actual &&
+ cat >expect <<-\EOF &&
+ foo1.7-before1
+ foo1.7
+ foo1.7-after1
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'version sort with very long prerelease suffix' '
+ test_config versionsort.prereleaseSuffix -very-looooooooooooooooooooooooong-prerelease-suffix &&
+ git tag -l --sort=version:refname
+'
+
+test_expect_success ULIMIT_STACK_SIZE '--contains and --no-contains work in a deep repo' '
+ i=1 &&
+ while test $i -lt 8000
+ do
+ echo "commit refs/heads/main
+committer A U Thor <author@example.com> $((1000000000 + $i * 100)) +0200
+data <<EOF
+commit #$i
+EOF" &&
+ if test $i = 1
+ then
+ echo "from refs/heads/main^0"
+ fi &&
+ i=$(($i + 1)) || return 1
+ done | git fast-import &&
+ git checkout main &&
+ git tag far-far-away HEAD^ &&
+ run_with_limited_stack git tag --contains HEAD >actual &&
+ test_must_be_empty actual &&
+ run_with_limited_stack git tag --no-contains HEAD >actual &&
+ test_line_count "-gt" 10 actual
+'
+
+test_expect_success '--format should list tags as per format given' '
+ cat >expect <<-\EOF &&
+ refname : refs/tags/v1.0
+ refname : refs/tags/v1.0.1
+ refname : refs/tags/v1.1.3
+ EOF
+ git tag -l --format="refname : %(refname)" "v1*" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--format --omit-empty works' '
+ cat >expect <<-\EOF &&
+ refname : refs/tags/v1.0
+
+ refname : refs/tags/v1.1.3
+ EOF
+ git tag -l --format="%(if:notequals=refs/tags/v1.0.1)%(refname)%(then)refname : %(refname)%(end)" "v1*" >actual &&
+ test_cmp expect actual &&
+ cat >expect <<-\EOF &&
+ refname : refs/tags/v1.0
+ refname : refs/tags/v1.1.3
+ EOF
+ git tag -l --omit-empty --format="%(if:notequals=refs/tags/v1.0.1)%(refname)%(then)refname : %(refname)%(end)" "v1*" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git tag -l with --format="%(rest)" must fail' '
+ test_must_fail git tag -l --format="%(rest)" "v1*"
+'
+
+test_expect_success "set up color tests" '
+ echo "<RED>v1.0<RESET>" >expect.color &&
+ echo "v1.0" >expect.bare &&
+ color_args="--format=%(color:red)%(refname:short) --list v1.0"
+'
+
+test_expect_success '%(color) omitted without tty' '
+ TERM=vt100 git tag $color_args >actual.raw &&
+ test_decode_color <actual.raw >actual &&
+ test_cmp expect.bare actual
+'
+
+test_expect_success TTY '%(color) present with tty' '
+ test_terminal git tag $color_args >actual.raw &&
+ test_decode_color <actual.raw >actual &&
+ test_cmp expect.color actual
+'
+
+test_expect_success '--color overrides auto-color' '
+ git tag --color $color_args >actual.raw &&
+ test_decode_color <actual.raw >actual &&
+ test_cmp expect.color actual
+'
+
+test_expect_success 'color.ui=always overrides auto-color' '
+ git -c color.ui=always tag $color_args >actual.raw &&
+ test_decode_color <actual.raw >actual &&
+ test_cmp expect.color actual
+'
+
+test_expect_success 'setup --merged test tags' '
+ git tag mergetest-1 HEAD~2 &&
+ git tag mergetest-2 HEAD~1 &&
+ git tag mergetest-3 HEAD
+'
+
+test_expect_success '--merged can be used in non-list mode' '
+ cat >expect <<-\EOF &&
+ mergetest-1
+ mergetest-2
+ EOF
+ git tag --merged=mergetest-2 "mergetest*" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--merged is compatible with --no-merged' '
+ git tag --merged HEAD --no-merged HEAD
+'
+
+test_expect_success '--merged shows merged tags' '
+ cat >expect <<-\EOF &&
+ mergetest-1
+ mergetest-2
+ EOF
+ git tag -l --merged=mergetest-2 mergetest-* >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--no-merged show unmerged tags' '
+ cat >expect <<-\EOF &&
+ mergetest-3
+ EOF
+ git tag -l --no-merged=mergetest-2 mergetest-* >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--no-merged can be used in non-list mode' '
+ git tag --no-merged=mergetest-2 mergetest-* >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'ambiguous branch/tags not marked' '
+ git tag ambiguous &&
+ git branch ambiguous &&
+ echo ambiguous >expect &&
+ git tag -l ambiguous >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--contains combined with --no-contains' '
+ (
+ git init no-contains &&
+ cd no-contains &&
+ test_commit v0.1 &&
+ test_commit v0.2 &&
+ test_commit v0.3 &&
+ test_commit v0.4 &&
+ test_commit v0.5 &&
+ cat >expected <<-\EOF &&
+ v0.2
+ v0.3
+ v0.4
+ EOF
+ git tag --contains v0.2 --no-contains v0.5 >actual &&
+ test_cmp expected actual
+ )
+'
+
+# As the docs say, list tags which contain a specified *commit*. We
+# don't recurse down to tags for trees or blobs pointed to by *those*
+# commits.
+test_expect_success 'Does --[no-]contains stop at commits? Yes!' '
+ cd no-contains &&
+ blob=$(git rev-parse v0.3:v0.3.t) &&
+ tree=$(git rev-parse v0.3^{tree}) &&
+ git tag tag-blob $blob &&
+ git tag tag-tree $tree &&
+ git tag --contains v0.3 >actual &&
+ cat >expected <<-\EOF &&
+ v0.3
+ v0.4
+ v0.5
+ EOF
+ test_cmp expected actual &&
+ git tag --no-contains v0.3 >actual &&
+ cat >expected <<-\EOF &&
+ v0.1
+ v0.2
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'If tag is created then tag message file is unlinked' '
+ test_when_finished "git tag -d foo" &&
+ write_script fakeeditor <<-\EOF &&
+ echo Message >.git/TAG_EDITMSG
+ EOF
+ GIT_EDITOR=./fakeeditor git tag -a foo &&
+ test_path_is_missing .git/TAG_EDITMSG
+'
+
+test_expect_success 'If tag cannot be created then tag message file is not unlinked' '
+ test_when_finished "git tag -d foo/bar && rm .git/TAG_EDITMSG" &&
+ write_script fakeeditor <<-\EOF &&
+ echo Message >.git/TAG_EDITMSG
+ EOF
+ git tag foo/bar &&
+ test_must_fail env GIT_EDITOR=./fakeeditor git tag -a foo &&
+ test_path_exists .git/TAG_EDITMSG
+'
+
+test_done
diff --git a/t/t7005-editor.sh b/t/t7005-editor.sh
new file mode 100755
index 0000000..5fcf281
--- /dev/null
+++ b/t/t7005-editor.sh
@@ -0,0 +1,131 @@
+#!/bin/sh
+
+test_description='GIT_EDITOR, core.editor, and stuff'
+
+. ./test-lib.sh
+
+unset EDITOR VISUAL GIT_EDITOR
+
+test_expect_success 'determine default editor' '
+
+ vi=$(TERM=vt100 git var GIT_EDITOR) &&
+ test -n "$vi"
+
+'
+
+if ! expr "$vi" : '[a-z]*$' >/dev/null
+then
+ vi=
+fi
+
+for i in GIT_EDITOR core_editor EDITOR VISUAL $vi
+do
+ cat >e-$i.sh <<-EOF
+ #!$SHELL_PATH
+ echo "Edited by $i" >"\$1"
+ EOF
+ chmod +x e-$i.sh
+done
+
+if ! test -z "$vi"
+then
+ mv e-$vi.sh $vi
+fi
+
+test_expect_success setup '
+
+ msg="Hand-edited" &&
+ test_commit "$msg" &&
+ echo "$msg" >expect &&
+ git show -s --format=%s > actual &&
+ test_cmp expect actual
+
+'
+
+TERM=dumb
+export TERM
+test_expect_success 'dumb should error out when falling back on vi' '
+
+ if git commit --amend
+ then
+ echo "Oops?"
+ false
+ else
+ : happy
+ fi
+'
+
+test_expect_success 'dumb should prefer EDITOR to VISUAL' '
+
+ EDITOR=./e-EDITOR.sh &&
+ VISUAL=./e-VISUAL.sh &&
+ export EDITOR VISUAL &&
+ git commit --amend &&
+ test "$(git show -s --format=%s)" = "Edited by EDITOR"
+
+'
+
+TERM=vt100
+export TERM
+for i in $vi EDITOR VISUAL core_editor GIT_EDITOR
+do
+ echo "Edited by $i" >expect
+ unset EDITOR VISUAL GIT_EDITOR
+ git config --unset-all core.editor
+ case "$i" in
+ core_editor)
+ git config core.editor ./e-core_editor.sh
+ ;;
+ [A-Z]*)
+ eval "$i=./e-$i.sh"
+ export $i
+ ;;
+ esac
+ test_expect_success "Using $i" '
+ git --exec-path=. commit --amend &&
+ git show -s --pretty=oneline |
+ sed -e "s/^[0-9a-f]* //" >actual &&
+ test_cmp expect actual
+ '
+done
+
+unset EDITOR VISUAL GIT_EDITOR
+git config --unset-all core.editor
+for i in $vi EDITOR VISUAL core_editor GIT_EDITOR
+do
+ echo "Edited by $i" >expect
+ case "$i" in
+ core_editor)
+ git config core.editor ./e-core_editor.sh
+ ;;
+ [A-Z]*)
+ eval "$i=./e-$i.sh"
+ export $i
+ ;;
+ esac
+ test_expect_success "Using $i (override)" '
+ git --exec-path=. commit --amend &&
+ git show -s --pretty=oneline |
+ sed -e "s/^[0-9a-f]* //" >actual &&
+ test_cmp expect actual
+ '
+done
+
+test_expect_success 'editor with a space' '
+ echo "echo space >\"\$1\"" >"e space.sh" &&
+ chmod a+x "e space.sh" &&
+ GIT_EDITOR="./e\ space.sh" git commit --amend &&
+ test space = "$(git show -s --pretty=format:%s)"
+
+'
+
+unset GIT_EDITOR
+test_expect_success 'core.editor with a space' '
+
+ git config core.editor \"./e\ space.sh\" &&
+ git commit --amend &&
+ test space = "$(git show -s --pretty=format:%s)"
+
+'
+
+test_done
diff --git a/t/t7006-pager.sh b/t/t7006-pager.sh
new file mode 100755
index 0000000..e56ca5b
--- /dev/null
+++ b/t/t7006-pager.sh
@@ -0,0 +1,768 @@
+#!/bin/sh
+
+test_description='Test automatic use of a pager.'
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-pager.sh
+. "$TEST_DIRECTORY"/lib-terminal.sh
+
+test_expect_success 'setup' '
+ sane_unset GIT_PAGER GIT_PAGER_IN_USE &&
+ test_unconfig core.pager &&
+
+ PAGER="cat >paginated.out" &&
+ export PAGER &&
+
+ test_commit initial
+'
+
+test_expect_success TTY 'some commands use a pager' '
+ rm -f paginated.out &&
+ test_terminal git log &&
+ test_path_is_file paginated.out
+'
+
+test_expect_failure TTY 'pager runs from subdir' '
+ echo subdir/paginated.out >expected &&
+ mkdir -p subdir &&
+ rm -f paginated.out subdir/paginated.out &&
+ (
+ cd subdir &&
+ test_terminal git log
+ ) &&
+ {
+ ls paginated.out subdir/paginated.out ||
+ :
+ } >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success TTY 'LESS and LV envvars are set for pagination' '
+ (
+ sane_unset LESS LV &&
+ PAGER="env >pager-env.out; wc" &&
+ export PAGER &&
+
+ test_terminal git log
+ ) &&
+ grep ^LESS= pager-env.out &&
+ grep ^LV= pager-env.out
+'
+
+test_expect_success !MINGW,TTY 'LESS and LV envvars set by git-sh-setup' '
+ (
+ sane_unset LESS LV &&
+ PAGER="env >pager-env.out; wc" &&
+ export PAGER &&
+ PATH="$(git --exec-path):$PATH" &&
+ export PATH &&
+ test_terminal sh -c ". git-sh-setup && git_pager"
+ ) &&
+ grep ^LESS= pager-env.out &&
+ grep ^LV= pager-env.out
+'
+
+test_expect_success TTY 'some commands do not use a pager' '
+ rm -f paginated.out &&
+ test_terminal git rev-list HEAD &&
+ test_path_is_missing paginated.out
+'
+
+test_expect_success 'no pager when stdout is a pipe' '
+ rm -f paginated.out &&
+ git log | cat &&
+ test_path_is_missing paginated.out
+'
+
+test_expect_success 'no pager when stdout is a regular file' '
+ rm -f paginated.out &&
+ git log >file &&
+ test_path_is_missing paginated.out
+'
+
+test_expect_success TTY 'git --paginate rev-list uses a pager' '
+ rm -f paginated.out &&
+ test_terminal git --paginate rev-list HEAD &&
+ test_path_is_file paginated.out
+'
+
+test_expect_success 'no pager even with --paginate when stdout is a pipe' '
+ rm -f file paginated.out &&
+ git --paginate log | cat &&
+ test_path_is_missing paginated.out
+'
+
+test_expect_success TTY 'no pager with --no-pager' '
+ rm -f paginated.out &&
+ test_terminal git --no-pager log &&
+ test_path_is_missing paginated.out
+'
+
+test_expect_success TTY 'configuration can disable pager' '
+ rm -f paginated.out &&
+ test_unconfig pager.grep &&
+ test_terminal git grep initial &&
+ test_path_is_file paginated.out &&
+
+ rm -f paginated.out &&
+ test_config pager.grep false &&
+ test_terminal git grep initial &&
+ test_path_is_missing paginated.out
+'
+
+test_expect_success TTY 'configuration can enable pager (from subdir)' '
+ rm -f paginated.out &&
+ mkdir -p subdir &&
+ test_config pager.bundle true &&
+
+ git bundle create test.bundle --all &&
+ rm -f paginated.out subdir/paginated.out &&
+ (
+ cd subdir &&
+ test_terminal git bundle unbundle ../test.bundle
+ ) &&
+ {
+ test_path_is_file paginated.out ||
+ test_path_is_file subdir/paginated.out
+ }
+'
+
+test_expect_success TTY 'git tag -l defaults to paging' '
+ rm -f paginated.out &&
+ test_terminal git tag -l &&
+ test_path_is_file paginated.out
+'
+
+test_expect_success TTY 'git tag -l respects pager.tag' '
+ rm -f paginated.out &&
+ test_terminal git -c pager.tag=false tag -l &&
+ test_path_is_missing paginated.out
+'
+
+test_expect_success TTY 'git tag -l respects --no-pager' '
+ rm -f paginated.out &&
+ test_terminal git -c pager.tag --no-pager tag -l &&
+ test_path_is_missing paginated.out
+'
+
+test_expect_success TTY 'git tag with no args defaults to paging' '
+ # no args implies -l so this should page like -l
+ rm -f paginated.out &&
+ test_terminal git tag &&
+ test_path_is_file paginated.out
+'
+
+test_expect_success TTY 'git tag with no args respects pager.tag' '
+ # no args implies -l so this should page like -l
+ rm -f paginated.out &&
+ test_terminal git -c pager.tag=false tag &&
+ test_path_is_missing paginated.out
+'
+
+test_expect_success TTY 'git tag --contains defaults to paging' '
+ # --contains implies -l so this should page like -l
+ rm -f paginated.out &&
+ test_terminal git tag --contains &&
+ test_path_is_file paginated.out
+'
+
+test_expect_success TTY 'git tag --contains respects pager.tag' '
+ # --contains implies -l so this should page like -l
+ rm -f paginated.out &&
+ test_terminal git -c pager.tag=false tag --contains &&
+ test_path_is_missing paginated.out
+'
+
+test_expect_success TTY 'git tag -a defaults to not paging' '
+ test_when_finished "git tag -d newtag" &&
+ rm -f paginated.out &&
+ test_terminal git tag -am message newtag &&
+ test_path_is_missing paginated.out
+'
+
+test_expect_success TTY 'git tag -a ignores pager.tag' '
+ test_when_finished "git tag -d newtag" &&
+ rm -f paginated.out &&
+ test_terminal git -c pager.tag tag -am message newtag &&
+ test_path_is_missing paginated.out
+'
+
+test_expect_success TTY 'git tag -a respects --paginate' '
+ test_when_finished "git tag -d newtag" &&
+ rm -f paginated.out &&
+ test_terminal git --paginate tag -am message newtag &&
+ test_path_is_file paginated.out
+'
+
+test_expect_success TTY 'git tag as alias ignores pager.tag with -a' '
+ test_when_finished "git tag -d newtag" &&
+ rm -f paginated.out &&
+ test_terminal git -c pager.tag -c alias.t=tag t -am message newtag &&
+ test_path_is_missing paginated.out
+'
+
+test_expect_success TTY 'git tag as alias respects pager.tag with -l' '
+ rm -f paginated.out &&
+ test_terminal git -c pager.tag=false -c alias.t=tag t -l &&
+ test_path_is_missing paginated.out
+'
+
+test_expect_success TTY 'git branch defaults to paging' '
+ rm -f paginated.out &&
+ test_terminal git branch &&
+ test_path_is_file paginated.out
+'
+
+test_expect_success TTY 'git branch respects pager.branch' '
+ rm -f paginated.out &&
+ test_terminal git -c pager.branch=false branch &&
+ test_path_is_missing paginated.out
+'
+
+test_expect_success TTY 'git branch respects --no-pager' '
+ rm -f paginated.out &&
+ test_terminal git --no-pager branch &&
+ test_path_is_missing paginated.out
+'
+
+test_expect_success TTY 'git branch --edit-description ignores pager.branch' '
+ rm -f paginated.out editor.used &&
+ write_script editor <<-\EOF &&
+ echo "New description" >"$1"
+ touch editor.used
+ EOF
+ EDITOR=./editor test_terminal git -c pager.branch branch --edit-description &&
+ test_path_is_missing paginated.out &&
+ test_path_is_file editor.used
+'
+
+test_expect_success TTY 'git branch --set-upstream-to ignores pager.branch' '
+ rm -f paginated.out &&
+ git branch other &&
+ test_when_finished "git branch -D other" &&
+ test_terminal git -c pager.branch branch --set-upstream-to=other &&
+ test_when_finished "git branch --unset-upstream" &&
+ test_path_is_missing paginated.out
+'
+
+test_expect_success TTY 'git config ignores pager.config when setting' '
+ rm -f paginated.out &&
+ test_terminal git -c pager.config config foo.bar bar &&
+ test_path_is_missing paginated.out
+'
+
+test_expect_success TTY 'git config --edit ignores pager.config' '
+ rm -f paginated.out editor.used &&
+ write_script editor <<-\EOF &&
+ touch editor.used
+ EOF
+ EDITOR=./editor test_terminal git -c pager.config config --edit &&
+ test_path_is_missing paginated.out &&
+ test_path_is_file editor.used
+'
+
+test_expect_success TTY 'git config --get ignores pager.config' '
+ rm -f paginated.out &&
+ test_terminal git -c pager.config config --get foo.bar &&
+ test_path_is_missing paginated.out
+'
+
+test_expect_success TTY 'git config --get-urlmatch defaults to paging' '
+ rm -f paginated.out &&
+ test_terminal git -c http."https://foo.com/".bar=foo \
+ config --get-urlmatch http https://foo.com &&
+ test_path_is_file paginated.out
+'
+
+test_expect_success TTY 'git config --get-all respects pager.config' '
+ rm -f paginated.out &&
+ test_terminal git -c pager.config=false config --get-all foo.bar &&
+ test_path_is_missing paginated.out
+'
+
+test_expect_success TTY 'git config --list defaults to paging' '
+ rm -f paginated.out &&
+ test_terminal git config --list &&
+ test_path_is_file paginated.out
+'
+
+
+# A colored commit log will begin with an appropriate ANSI escape
+# for the first color; the text "commit" comes later.
+colorful() {
+ read firstline <$1
+ ! expr "$firstline" : "[a-zA-Z]" >/dev/null
+}
+
+test_expect_success 'tests can detect color' '
+ rm -f colorful.log colorless.log &&
+ git log --no-color >colorless.log &&
+ git log --color >colorful.log &&
+ ! colorful colorless.log &&
+ colorful colorful.log
+'
+
+test_expect_success 'no color when stdout is a regular file' '
+ rm -f colorless.log &&
+ test_config color.ui auto &&
+ git log >colorless.log &&
+ ! colorful colorless.log
+'
+
+test_expect_success TTY 'color when writing to a pager' '
+ rm -f paginated.out &&
+ test_config color.ui auto &&
+ test_terminal git log &&
+ colorful paginated.out
+'
+
+test_expect_success TTY 'colors are suppressed by color.pager' '
+ rm -f paginated.out &&
+ test_config color.ui auto &&
+ test_config color.pager false &&
+ test_terminal git log &&
+ ! colorful paginated.out
+'
+
+test_expect_success 'color when writing to a file intended for a pager' '
+ rm -f colorful.log &&
+ test_config color.ui auto &&
+ (
+ TERM=vt100 &&
+ GIT_PAGER_IN_USE=true &&
+ export TERM GIT_PAGER_IN_USE &&
+ git log >colorful.log
+ ) &&
+ colorful colorful.log
+'
+
+test_expect_success TTY 'colors are sent to pager for external commands' '
+ test_config alias.externallog "!git log" &&
+ test_config color.ui auto &&
+ test_terminal git -p externallog &&
+ colorful paginated.out
+'
+
+# Use this helper to make it easy for the caller of your
+# terminal-using function to specify whether it should fail.
+# If you write
+#
+# your_test() {
+# parse_args "$@"
+#
+# $test_expectation "$cmd - behaves well" "
+# ...
+# $full_command &&
+# ...
+# "
+# }
+#
+# then your test can be used like this:
+#
+# your_test expect_(success|failure) [test_must_fail] 'git foo'
+#
+parse_args() {
+ test_expectation="test_$1"
+ shift
+ if test "$1" = test_must_fail
+ then
+ full_command="test_must_fail test_terminal "
+ shift
+ else
+ full_command="test_terminal "
+ fi
+ cmd=$1
+ full_command="$full_command $1"
+}
+
+test_default_pager() {
+ parse_args "$@"
+
+ $test_expectation SIMPLEPAGER,TTY "$cmd - default pager is used by default" "
+ sane_unset PAGER GIT_PAGER &&
+ test_unconfig core.pager &&
+ rm -f default_pager_used &&
+ cat >\$less <<-\EOF &&
+ #!/bin/sh
+ wc >default_pager_used
+ EOF
+ chmod +x \$less &&
+ (
+ PATH=.:\$PATH &&
+ export PATH &&
+ $full_command
+ ) &&
+ test_path_is_file default_pager_used
+ "
+}
+
+test_PAGER_overrides() {
+ parse_args "$@"
+
+ $test_expectation TTY "$cmd - PAGER overrides default pager" "
+ sane_unset GIT_PAGER &&
+ test_unconfig core.pager &&
+ rm -f PAGER_used &&
+ PAGER='wc >PAGER_used' &&
+ export PAGER &&
+ $full_command &&
+ test_path_is_file PAGER_used
+ "
+}
+
+test_core_pager_overrides() {
+ if_local_config=
+ used_if_wanted='overrides PAGER'
+ test_core_pager "$@"
+}
+
+test_local_config_ignored() {
+ if_local_config='! '
+ used_if_wanted='is not used'
+ test_core_pager "$@"
+}
+
+test_core_pager() {
+ parse_args "$@"
+
+ $test_expectation TTY "$cmd - repository-local core.pager setting $used_if_wanted" "
+ sane_unset GIT_PAGER &&
+ rm -f core.pager_used &&
+ PAGER=wc &&
+ export PAGER &&
+ test_config core.pager 'wc >core.pager_used' &&
+ $full_command &&
+ ${if_local_config}test_path_is_file core.pager_used
+ "
+}
+
+test_core_pager_subdir() {
+ if_local_config=
+ used_if_wanted='overrides PAGER'
+ test_pager_subdir_helper "$@"
+}
+
+test_no_local_config_subdir() {
+ if_local_config='! '
+ used_if_wanted='is not used'
+ test_pager_subdir_helper "$@"
+}
+
+test_pager_subdir_helper() {
+ parse_args "$@"
+
+ $test_expectation TTY "$cmd - core.pager $used_if_wanted from subdirectory" "
+ sane_unset GIT_PAGER &&
+ rm -f core.pager_used &&
+ rm -fr sub &&
+ PAGER=wc &&
+ stampname=\$(pwd)/core.pager_used &&
+ export PAGER stampname &&
+ test_config core.pager 'wc >\"\$stampname\"' &&
+ mkdir sub &&
+ (
+ cd sub &&
+ $full_command
+ ) &&
+ ${if_local_config}test_path_is_file core.pager_used
+ "
+}
+
+test_GIT_PAGER_overrides() {
+ parse_args "$@"
+
+ $test_expectation TTY "$cmd - GIT_PAGER overrides core.pager" "
+ rm -f GIT_PAGER_used &&
+ test_config core.pager wc &&
+ GIT_PAGER='wc >GIT_PAGER_used' &&
+ export GIT_PAGER &&
+ $full_command &&
+ test_path_is_file GIT_PAGER_used
+ "
+}
+
+test_doesnt_paginate() {
+ parse_args "$@"
+
+ $test_expectation TTY "no pager for '$cmd'" "
+ rm -f GIT_PAGER_used &&
+ GIT_PAGER='wc >GIT_PAGER_used' &&
+ export GIT_PAGER &&
+ $full_command &&
+ test_path_is_missing GIT_PAGER_used
+ "
+}
+
+test_pager_choices() {
+ test_default_pager expect_success "$@"
+ test_PAGER_overrides expect_success "$@"
+ test_core_pager_overrides expect_success "$@"
+ test_core_pager_subdir expect_success "$@"
+ test_GIT_PAGER_overrides expect_success "$@"
+}
+
+test_expect_success 'setup: some aliases' '
+ git config alias.aliasedlog log &&
+ git config alias.true "!true"
+'
+
+test_pager_choices 'git log'
+test_pager_choices 'git -p log'
+test_pager_choices 'git aliasedlog'
+
+test_default_pager expect_success 'git -p aliasedlog'
+test_PAGER_overrides expect_success 'git -p aliasedlog'
+test_core_pager_overrides expect_success 'git -p aliasedlog'
+test_core_pager_subdir expect_success 'git -p aliasedlog'
+test_GIT_PAGER_overrides expect_success 'git -p aliasedlog'
+
+test_default_pager expect_success 'git -p true'
+test_PAGER_overrides expect_success 'git -p true'
+test_core_pager_overrides expect_success 'git -p true'
+test_core_pager_subdir expect_success 'git -p true'
+test_GIT_PAGER_overrides expect_success 'git -p true'
+
+test_default_pager expect_success test_must_fail 'git -p request-pull'
+test_PAGER_overrides expect_success test_must_fail 'git -p request-pull'
+test_core_pager_overrides expect_success test_must_fail 'git -p request-pull'
+test_core_pager_subdir expect_success test_must_fail 'git -p request-pull'
+test_GIT_PAGER_overrides expect_success test_must_fail 'git -p request-pull'
+
+test_default_pager expect_success test_must_fail 'git -p'
+test_PAGER_overrides expect_success test_must_fail 'git -p'
+test_local_config_ignored expect_failure test_must_fail 'git -p'
+test_GIT_PAGER_overrides expect_success test_must_fail 'git -p'
+
+test_expect_success TTY 'core.pager in repo config works and retains cwd' '
+ sane_unset GIT_PAGER &&
+ test_config core.pager "cat >cwd-retained" &&
+ (
+ cd sub &&
+ rm -f cwd-retained &&
+ test_terminal git -p rev-parse HEAD &&
+ test_path_is_file cwd-retained
+ )
+'
+
+test_expect_success TTY 'core.pager is found via alias in subdirectory' '
+ sane_unset GIT_PAGER &&
+ test_config core.pager "cat >via-alias" &&
+ (
+ cd sub &&
+ rm -f via-alias &&
+ test_terminal git -c alias.r="-p rev-parse" r HEAD &&
+ test_path_is_file via-alias
+ )
+'
+
+test_doesnt_paginate expect_failure test_must_fail 'git -p nonsense'
+
+test_pager_choices 'git shortlog'
+test_expect_success 'setup: configure shortlog not to paginate' '
+ git config pager.shortlog false
+'
+test_doesnt_paginate expect_success 'git shortlog'
+test_no_local_config_subdir expect_success 'git shortlog'
+test_default_pager expect_success 'git -p shortlog'
+test_core_pager_subdir expect_success 'git -p shortlog'
+
+test_core_pager_subdir expect_success test_must_fail \
+ 'git -p apply </dev/null'
+
+test_expect_success TTY 'command-specific pager' '
+ sane_unset PAGER GIT_PAGER &&
+ echo "foo:initial" >expect &&
+ >actual &&
+ test_unconfig core.pager &&
+ test_config pager.log "sed s/^/foo:/ >actual" &&
+ test_terminal git log --format=%s -1 &&
+ test_cmp expect actual
+'
+
+test_expect_success TTY 'command-specific pager overrides core.pager' '
+ sane_unset PAGER GIT_PAGER &&
+ echo "foo:initial" >expect &&
+ >actual &&
+ test_config core.pager "exit 1" &&
+ test_config pager.log "sed s/^/foo:/ >actual" &&
+ test_terminal git log --format=%s -1 &&
+ test_cmp expect actual
+'
+
+test_expect_success TTY 'command-specific pager overridden by environment' '
+ GIT_PAGER="sed s/^/foo:/ >actual" && export GIT_PAGER &&
+ >actual &&
+ echo "foo:initial" >expect &&
+ test_config pager.log "exit 1" &&
+ test_terminal git log --format=%s -1 &&
+ test_cmp expect actual
+'
+
+test_expect_success 'setup external command' '
+ cat >git-external <<-\EOF &&
+ #!/bin/sh
+ git "$@"
+ EOF
+ chmod +x git-external
+'
+
+test_expect_success TTY 'command-specific pager works for external commands' '
+ sane_unset PAGER GIT_PAGER &&
+ echo "foo:initial" >expect &&
+ >actual &&
+ test_config pager.external "sed s/^/foo:/ >actual" &&
+ test_terminal git --exec-path="$(pwd)" external log --format=%s -1 &&
+ test_cmp expect actual
+'
+
+test_expect_success TTY 'sub-commands of externals use their own pager' '
+ sane_unset PAGER GIT_PAGER &&
+ echo "foo:initial" >expect &&
+ >actual &&
+ test_config pager.log "sed s/^/foo:/ >actual" &&
+ test_terminal git --exec-path=. external log --format=%s -1 &&
+ test_cmp expect actual
+'
+
+test_expect_success TTY 'external command pagers override sub-commands' '
+ sane_unset PAGER GIT_PAGER &&
+ >actual &&
+ test_config pager.external false &&
+ test_config pager.log "sed s/^/log:/ >actual" &&
+ test_terminal git --exec-path=. external log --format=%s -1 &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'command with underscores does not complain' '
+ write_script git-under_score <<-\EOF &&
+ echo ok
+ EOF
+ git --exec-path=. under_score >actual 2>&1 &&
+ echo ok >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success TTY 'git tag with auto-columns ' '
+ test_commit one &&
+ test_commit two &&
+ test_commit three &&
+ test_commit four &&
+ test_commit five &&
+ cat >expect <<-\EOF &&
+ initial one two three four five
+ EOF
+ test_terminal env PAGER="cat >actual" COLUMNS=80 \
+ git -c column.ui=auto tag --sort=authordate &&
+ test_cmp expect actual
+'
+
+test_expect_success 'setup trace2' '
+ GIT_TRACE2_BRIEF=1 &&
+ export GIT_TRACE2_BRIEF
+'
+
+test_expect_success 'setup large log output' '
+ perl -e "
+ print \"this is a long commit message\" x 50000
+ " >commit-msg &&
+ git commit --allow-empty -F commit-msg
+'
+
+test_expect_success TTY 'git returns SIGPIPE on early pager exit' '
+ test_when_finished "rm pager-used trace.normal" &&
+ test_config core.pager ">pager-used; head -n 1; exit 0" &&
+ GIT_TRACE2="$(pwd)/trace.normal" &&
+ export GIT_TRACE2 &&
+ test_when_finished "unset GIT_TRACE2" &&
+
+ if test_have_prereq !MINGW
+ then
+ { test_terminal git log >/dev/null; OUT=$?; } &&
+ test_match_signal 13 "$OUT"
+ else
+ test_terminal git log
+ fi &&
+
+ grep child_exit trace.normal >child-exits &&
+ test_line_count = 1 child-exits &&
+ grep " code:0 " child-exits &&
+ test_path_is_file pager-used
+'
+
+test_expect_success TTY 'git returns SIGPIPE on early pager non-zero exit' '
+ test_when_finished "rm pager-used trace.normal" &&
+ test_config core.pager ">pager-used; head -n 1; exit 1" &&
+ GIT_TRACE2="$(pwd)/trace.normal" &&
+ export GIT_TRACE2 &&
+ test_when_finished "unset GIT_TRACE2" &&
+
+ if test_have_prereq !MINGW
+ then
+ { test_terminal git log >/dev/null; OUT=$?; } &&
+ test_match_signal 13 "$OUT"
+ else
+ test_terminal git log
+ fi &&
+
+ grep child_exit trace.normal >child-exits &&
+ test_line_count = 1 child-exits &&
+ grep " code:1 " child-exits &&
+ test_path_is_file pager-used
+'
+
+test_expect_success TTY 'git discards pager non-zero exit without SIGPIPE' '
+ test_when_finished "rm pager-used trace.normal" &&
+ test_config core.pager "wc >pager-used; exit 1" &&
+ GIT_TRACE2="$(pwd)/trace.normal" &&
+ export GIT_TRACE2 &&
+ test_when_finished "unset GIT_TRACE2" &&
+
+ test_terminal git log &&
+
+ grep child_exit trace.normal >child-exits &&
+ test_line_count = 1 child-exits &&
+ grep " code:1 " child-exits &&
+ test_path_is_file pager-used
+'
+
+test_expect_success TTY 'git skips paging nonexisting command' '
+ test_when_finished "rm trace.normal" &&
+ test_config core.pager "does-not-exist" &&
+ GIT_TRACE2="$(pwd)/trace.normal" &&
+ export GIT_TRACE2 &&
+ test_when_finished "unset GIT_TRACE2" &&
+
+ test_terminal git log &&
+
+ grep child_exit trace.normal >child-exits &&
+ test_line_count = 1 child-exits &&
+ grep " code:-1 " child-exits
+'
+
+test_expect_success TTY 'git returns SIGPIPE on propagated signals from pager' '
+ test_when_finished "rm pager-used trace.normal" &&
+ test_config core.pager ">pager-used; exec test-tool sigchain" &&
+ GIT_TRACE2="$(pwd)/trace.normal" &&
+ export GIT_TRACE2 &&
+ test_when_finished "unset GIT_TRACE2" &&
+
+ if test_have_prereq !MINGW
+ then
+ { test_terminal git log >/dev/null; OUT=$?; } &&
+ test_match_signal 13 "$OUT"
+ else
+ test_terminal git log
+ fi &&
+
+ grep child_exit trace.normal >child-exits &&
+ test_line_count = 1 child-exits &&
+ grep " code:143 " child-exits &&
+ test_path_is_file pager-used
+'
+
+test_expect_success TTY 'non-existent pager doesnt cause crash' '
+ test_config pager.show invalid-pager &&
+ test_terminal git show
+'
+
+test_done
diff --git a/t/t7007-show.sh b/t/t7007-show.sh
new file mode 100755
index 0000000..f908a4d
--- /dev/null
+++ b/t/t7007-show.sh
@@ -0,0 +1,171 @@
+#!/bin/sh
+
+test_description='git show'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ echo hello world >foo &&
+ H=$(git hash-object -w foo) &&
+ git tag -a foo-tag -m "Tags $H" $H &&
+ HH=$(expr "$H" : "\(..\)") &&
+ H38=$(expr "$H" : "..\(.*\)") &&
+ rm -f .git/objects/$HH/$H38
+'
+
+test_expect_success 'showing a tag that point at a missing object' '
+ test_must_fail git --no-pager show foo-tag
+'
+
+test_expect_success 'set up a bit of history' '
+ test_commit main1 &&
+ test_commit main2 &&
+ test_commit main3 &&
+ git tag -m "annotated tag" annotated &&
+ git checkout -b side HEAD^^ &&
+ test_commit side2 &&
+ test_commit side3 &&
+ test_merge merge main3
+'
+
+test_expect_success 'showing two commits' '
+ cat >expect <<-EOF &&
+ commit $(git rev-parse main2)
+ commit $(git rev-parse main3)
+ EOF
+ git show main2 main3 >actual &&
+ grep ^commit actual >actual.filtered &&
+ test_cmp expect actual.filtered
+'
+
+test_expect_success 'showing a tree' '
+ cat >expected <<-EOF &&
+ tree main1:
+
+ main1.t
+ EOF
+ git show main1: >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'showing two trees' '
+ cat >expected <<-EOF &&
+ tree main1^{tree}
+
+ main1.t
+
+ tree main2^{tree}
+
+ main1.t
+ main2.t
+ EOF
+ git show main1^{tree} main2^{tree} >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'showing a trees is not recursive' '
+ git worktree add not-recursive main1 &&
+ mkdir not-recursive/a &&
+ test_commit -C not-recursive a/file &&
+ cat >expected <<-EOF &&
+ tree HEAD^{tree}
+
+ a/
+ main1.t
+ EOF
+ git -C not-recursive show HEAD^{tree} >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'showing a range walks (linear)' '
+ cat >expect <<-EOF &&
+ commit $(git rev-parse main3)
+ commit $(git rev-parse main2)
+ EOF
+ git show main1..main3 >actual &&
+ grep ^commit actual >actual.filtered &&
+ test_cmp expect actual.filtered
+'
+
+test_expect_success 'showing a range walks (Y shape, ^ first)' '
+ cat >expect <<-EOF &&
+ commit $(git rev-parse main3)
+ commit $(git rev-parse main2)
+ EOF
+ git show ^side3 main3 >actual &&
+ grep ^commit actual >actual.filtered &&
+ test_cmp expect actual.filtered
+'
+
+test_expect_success 'showing a range walks (Y shape, ^ last)' '
+ cat >expect <<-EOF &&
+ commit $(git rev-parse main3)
+ commit $(git rev-parse main2)
+ EOF
+ git show main3 ^side3 >actual &&
+ grep ^commit actual >actual.filtered &&
+ test_cmp expect actual.filtered
+'
+
+test_expect_success 'showing with -N walks' '
+ cat >expect <<-EOF &&
+ commit $(git rev-parse main3)
+ commit $(git rev-parse main2)
+ EOF
+ git show -2 main3 >actual &&
+ grep ^commit actual >actual.filtered &&
+ test_cmp expect actual.filtered
+'
+
+test_expect_success 'showing annotated tag' '
+ cat >expect <<-EOF &&
+ tag annotated
+ commit $(git rev-parse annotated^{commit})
+ EOF
+ git show annotated >actual &&
+ grep -E "^(commit|tag)" actual >actual.filtered &&
+ test_cmp expect actual.filtered
+'
+
+test_expect_success 'showing annotated tag plus commit' '
+ cat >expect <<-EOF &&
+ tag annotated
+ commit $(git rev-parse annotated^{commit})
+ commit $(git rev-parse side3)
+ EOF
+ git show annotated side3 >actual &&
+ grep -E "^(commit|tag)" actual >actual.filtered &&
+ test_cmp expect actual.filtered
+'
+
+test_expect_success 'showing range' '
+ cat >expect <<-EOF &&
+ commit $(git rev-parse main3)
+ commit $(git rev-parse main2)
+ EOF
+ git show ^side3 annotated >actual &&
+ grep -E "^(commit|tag)" actual >actual.filtered &&
+ test_cmp expect actual.filtered
+'
+
+test_expect_success '-s suppresses diff' '
+ cat >expect <<-\EOF &&
+ merge
+ main3
+ EOF
+ git show -s --format=%s merge main3 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--quiet suppresses diff' '
+ echo main3 >expect &&
+ git show --quiet --format=%s main3 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'show --graph is forbidden' '
+ test_must_fail git show --graph HEAD
+'
+
+test_done
diff --git a/t/t7008-filter-branch-null-sha1.sh b/t/t7008-filter-branch-null-sha1.sh
new file mode 100755
index 0000000..93fbc92
--- /dev/null
+++ b/t/t7008-filter-branch-null-sha1.sh
@@ -0,0 +1,56 @@
+#!/bin/sh
+
+test_description='filter-branch removal of trees with null sha1'
+
+. ./test-lib.sh
+
+test_expect_success 'setup: base commits' '
+ test_commit one &&
+ test_commit two &&
+ test_commit three
+'
+
+test_expect_success 'setup: a commit with a bogus null sha1 in the tree' '
+ {
+ git ls-tree HEAD &&
+ printf "160000 commit $ZERO_OID\\tbroken\\n"
+ } >broken-tree &&
+ echo "add broken entry" >msg &&
+
+ tree=$(git mktree <broken-tree) &&
+ test_tick &&
+ commit=$(git commit-tree $tree -p HEAD <msg) &&
+ git update-ref HEAD "$commit"
+'
+
+# we have to make one more commit on top removing the broken
+# entry, since otherwise our index does not match HEAD (and filter-branch will
+# complain). We could make the index match HEAD, but doing so would involve
+# writing a null sha1 into the index.
+test_expect_success 'setup: bring HEAD and index in sync' '
+ test_tick &&
+ git commit -a -m "back to normal"
+'
+
+test_expect_success 'noop filter-branch complains' '
+ test_must_fail git filter-branch \
+ --force --prune-empty \
+ --index-filter "true"
+'
+
+test_expect_success 'filter commands are still checked' '
+ test_must_fail git filter-branch \
+ --force --prune-empty \
+ --index-filter "git rm --cached --ignore-unmatch three.t"
+'
+
+test_expect_success 'removing the broken entry works' '
+ echo three >expect &&
+ git filter-branch \
+ --force --prune-empty \
+ --index-filter "git rm --cached --ignore-unmatch broken" &&
+ git log -1 --format=%s >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t7010-setup.sh b/t/t7010-setup.sh
new file mode 100755
index 0000000..520f96d
--- /dev/null
+++ b/t/t7010-setup.sh
@@ -0,0 +1,161 @@
+#!/bin/sh
+
+test_description='setup taking and sanitizing funny paths'
+
+. ./test-lib.sh
+
+test_expect_success setup '
+
+ mkdir -p a/b/c a/e &&
+ D=$(pwd) &&
+ >a/b/c/d &&
+ >a/e/f
+
+'
+
+test_expect_success 'git add (absolute)' '
+
+ git add "$D/a/b/c/d" &&
+ git ls-files >current &&
+ echo a/b/c/d >expect &&
+ test_cmp expect current
+
+'
+
+
+test_expect_success 'git add (funny relative)' '
+
+ rm -f .git/index &&
+ (
+ cd a/b &&
+ git add "../e/./f"
+ ) &&
+ git ls-files >current &&
+ echo a/e/f >expect &&
+ test_cmp expect current
+
+'
+
+test_expect_success 'git rm (absolute)' '
+
+ rm -f .git/index &&
+ git add a &&
+ git rm -f --cached "$D/a/b/c/d" &&
+ git ls-files >current &&
+ echo a/e/f >expect &&
+ test_cmp expect current
+
+'
+
+test_expect_success 'git rm (funny relative)' '
+
+ rm -f .git/index &&
+ git add a &&
+ (
+ cd a/b &&
+ git rm -f --cached "../e/./f"
+ ) &&
+ git ls-files >current &&
+ echo a/b/c/d >expect &&
+ test_cmp expect current
+
+'
+
+test_expect_success 'git ls-files (absolute)' '
+
+ rm -f .git/index &&
+ git add a &&
+ git ls-files "$D/a/e/../b" >current &&
+ echo a/b/c/d >expect &&
+ test_cmp expect current
+
+'
+
+test_expect_success 'git ls-files (relative #1)' '
+
+ rm -f .git/index &&
+ git add a &&
+ (
+ cd a/b &&
+ git ls-files "../b/c"
+ ) >current &&
+ echo c/d >expect &&
+ test_cmp expect current
+
+'
+
+test_expect_success 'git ls-files (relative #2)' '
+
+ rm -f .git/index &&
+ git add a &&
+ (
+ cd a/b &&
+ git ls-files --full-name "../e/f"
+ ) >current &&
+ echo a/e/f >expect &&
+ test_cmp expect current
+
+'
+
+test_expect_success 'git ls-files (relative #3)' '
+
+ rm -f .git/index &&
+ git add a &&
+ (
+ cd a/b &&
+ git ls-files "../e/f"
+ ) >current &&
+ echo ../e/f >expect &&
+ test_cmp expect current
+
+'
+
+test_expect_success 'commit using absolute path names' '
+ git commit -m "foo" &&
+ echo aa >>a/b/c/d &&
+ git commit -m "aa" "$(pwd)/a/b/c/d"
+'
+
+test_expect_success 'log using absolute path names' '
+ echo bb >>a/b/c/d &&
+ git commit -m "bb" "$(pwd)/a/b/c/d" &&
+
+ git log a/b/c/d >f1.txt &&
+ git log "$(pwd)/a/b/c/d" >f2.txt &&
+ test_cmp f1.txt f2.txt
+'
+
+test_expect_success 'blame using absolute path names' '
+ git blame a/b/c/d >f1.txt &&
+ git blame "$(pwd)/a/b/c/d" >f2.txt &&
+ test_cmp f1.txt f2.txt
+'
+
+test_expect_success 'setup deeper work tree' '
+ test_create_repo tester
+'
+
+test_expect_success 'add a directory outside the work tree' '(
+ cd tester &&
+ d1="$(cd .. && pwd)" &&
+ test_must_fail git add "$d1"
+)'
+
+
+test_expect_success 'add a file outside the work tree, nasty case 1' '(
+ cd tester &&
+ f="$(pwd)x" &&
+ echo "$f" &&
+ touch "$f" &&
+ test_must_fail git add "$f"
+)'
+
+test_expect_success 'add a file outside the work tree, nasty case 2' '(
+ cd tester &&
+ f="$(pwd | sed "s/.$//")x" &&
+ echo "$f" &&
+ touch "$f" &&
+ test_must_fail git add "$f"
+)'
+
+test_done
diff --git a/t/t7011-skip-worktree-reading.sh b/t/t7011-skip-worktree-reading.sh
new file mode 100755
index 0000000..4adac5a
--- /dev/null
+++ b/t/t7011-skip-worktree-reading.sh
@@ -0,0 +1,148 @@
+#!/bin/sh
+#
+# Copyright (c) 2008 Nguyá»…n Thái Ngá»c Duy
+#
+
+test_description='skip-worktree bit test'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+cat >expect.full <<EOF
+H 1
+H 2
+H init.t
+H sub/1
+H sub/2
+EOF
+
+cat >expect.skip <<EOF
+S 1
+H 2
+H init.t
+S sub/1
+H sub/2
+EOF
+
+setup_absent() {
+ test -f 1 && rm 1
+ git update-index --remove 1 &&
+ git update-index --add --cacheinfo 100644 $EMPTY_BLOB 1 &&
+ git update-index --skip-worktree 1
+}
+
+test_absent() {
+ echo "100644 $EMPTY_BLOB 0 1" > expected &&
+ git ls-files --stage 1 > result &&
+ test_cmp expected result &&
+ test ! -f 1
+}
+
+setup_dirty() {
+ git update-index --force-remove 1 &&
+ echo dirty > 1 &&
+ git update-index --add --cacheinfo 100644 $EMPTY_BLOB 1 &&
+ git update-index --skip-worktree 1
+}
+
+test_dirty() {
+ echo "100644 $EMPTY_BLOB 0 1" > expected &&
+ git ls-files --stage 1 > result &&
+ test_cmp expected result &&
+ echo dirty > expected
+ test_cmp expected 1
+}
+
+test_expect_success 'setup' '
+ test_commit init &&
+ mkdir sub &&
+ touch ./1 ./2 sub/1 sub/2 &&
+ git add 1 2 sub/1 sub/2 &&
+ git update-index --skip-worktree 1 sub/1 &&
+ git ls-files -t > result &&
+ test_cmp expect.skip result
+'
+
+test_expect_success 'update-index' '
+ setup_absent &&
+ git update-index 1 &&
+ test_absent
+'
+
+test_expect_success 'update-index' '
+ setup_dirty &&
+ git update-index 1 &&
+ test_dirty
+'
+
+test_expect_success 'update-index --remove' '
+ setup_absent &&
+ git update-index --remove 1 &&
+ test -z "$(git ls-files 1)" &&
+ test ! -f 1
+'
+
+test_expect_success 'update-index --remove' '
+ setup_dirty &&
+ git update-index --remove 1 &&
+ test -z "$(git ls-files 1)" &&
+ echo dirty > expected &&
+ test_cmp expected 1
+'
+
+test_expect_success 'ls-files --deleted' '
+ setup_absent &&
+ test -z "$(git ls-files -d)"
+'
+
+test_expect_success 'ls-files --deleted' '
+ setup_dirty &&
+ test -z "$(git ls-files -d)"
+'
+
+test_expect_success 'ls-files --modified' '
+ setup_absent &&
+ test -z "$(git ls-files -m)"
+'
+
+test_expect_success 'ls-files --modified' '
+ setup_dirty &&
+ test -z "$(git ls-files -m)"
+'
+
+echo ":000000 100644 $ZERO_OID $EMPTY_BLOB A 1" > expected
+test_expect_success 'diff-index does not examine skip-worktree absent entries' '
+ setup_absent &&
+ git diff-index HEAD -- 1 > result &&
+ test_cmp expected result
+'
+
+test_expect_success 'diff-index does not examine skip-worktree dirty entries' '
+ setup_dirty &&
+ git diff-index HEAD -- 1 > result &&
+ test_cmp expected result
+'
+
+test_expect_success 'diff-files does not examine skip-worktree absent entries' '
+ setup_absent &&
+ test -z "$(git diff-files -- one)"
+'
+
+test_expect_success 'diff-files does not examine skip-worktree dirty entries' '
+ setup_dirty &&
+ test -z "$(git diff-files -- one)"
+'
+
+test_expect_success 'commit on skip-worktree absent entries' '
+ git reset &&
+ setup_absent &&
+ test_must_fail git commit -m null 1
+'
+
+test_expect_success 'commit on skip-worktree dirty entries' '
+ git reset &&
+ setup_dirty &&
+ test_must_fail git commit -m null 1
+'
+
+test_done
diff --git a/t/t7012-skip-worktree-writing.sh b/t/t7012-skip-worktree-writing.sh
new file mode 100755
index 0000000..cd5c20f
--- /dev/null
+++ b/t/t7012-skip-worktree-writing.sh
@@ -0,0 +1,198 @@
+#!/bin/sh
+#
+# Copyright (c) 2008 Nguyá»…n Thái Ngá»c Duy
+#
+
+test_description='test worktree writing operations when skip-worktree is used'
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ test_commit init &&
+ echo modified >> init.t &&
+ touch added &&
+ git add init.t added &&
+ git commit -m "modified and added" &&
+ git tag top
+'
+
+test_expect_success 'read-tree updates worktree, absent case' '
+ git checkout -f top &&
+ git update-index --skip-worktree init.t &&
+ rm init.t &&
+ git read-tree -m -u HEAD^ &&
+ echo init > expected &&
+ test_cmp expected init.t
+'
+
+test_expect_success 'read-tree updates worktree, dirty case' '
+ git checkout -f top &&
+ git update-index --skip-worktree init.t &&
+ echo dirty >> init.t &&
+ test_must_fail git read-tree -m -u HEAD^ &&
+ grep -q dirty init.t &&
+ test "$(git ls-files -t init.t)" = "S init.t" &&
+ git update-index --no-skip-worktree init.t
+'
+
+test_expect_success 'read-tree removes worktree, absent case' '
+ git checkout -f top &&
+ git update-index --skip-worktree added &&
+ rm added &&
+ git read-tree -m -u HEAD^ &&
+ test ! -f added
+'
+
+test_expect_success 'read-tree removes worktree, dirty case' '
+ git checkout -f top &&
+ git update-index --skip-worktree added &&
+ echo dirty >> added &&
+ test_must_fail git read-tree -m -u HEAD^ &&
+ grep -q dirty added &&
+ test "$(git ls-files -t added)" = "S added" &&
+ git update-index --no-skip-worktree added
+'
+
+setup_absent() {
+ test -f 1 && rm 1
+ git update-index --remove 1 &&
+ git update-index --add --cacheinfo 100644 $EMPTY_BLOB 1 &&
+ git update-index --skip-worktree 1
+}
+
+setup_dirty() {
+ git update-index --force-remove 1 &&
+ echo dirty > 1 &&
+ git update-index --add --cacheinfo 100644 $EMPTY_BLOB 1 &&
+ git update-index --skip-worktree 1
+}
+
+test_dirty() {
+ echo "100644 $EMPTY_BLOB 0 1" > expected &&
+ git ls-files --stage 1 > result &&
+ test_cmp expected result &&
+ echo dirty > expected
+ test_cmp expected 1
+}
+
+cat >expected <<EOF
+S 1
+H 2
+H init.t
+S sub/1
+H sub/2
+EOF
+
+test_expect_success 'index setup' '
+ git checkout -f init &&
+ mkdir sub &&
+ touch ./1 ./2 sub/1 sub/2 &&
+ git add 1 2 sub/1 sub/2 &&
+ git update-index --skip-worktree 1 sub/1 &&
+ git ls-files -t > result &&
+ test_cmp expected result
+'
+
+test_expect_success 'git-rm fails if worktree is dirty' '
+ setup_dirty &&
+ test_must_fail git rm 1 &&
+ test_dirty
+'
+
+cat >expected <<EOF
+Would remove expected
+Would remove result
+EOF
+test_expect_success 'git-clean, absent case' '
+ setup_absent &&
+ git clean -n > result &&
+ test_cmp expected result
+'
+
+test_expect_success 'git-clean, dirty case' '
+ setup_dirty &&
+ git clean -n > result &&
+ test_cmp expected result
+'
+
+test_expect_success '--ignore-skip-worktree-entries leaves worktree alone' '
+ test_commit keep-me &&
+ git update-index --skip-worktree keep-me.t &&
+ rm keep-me.t &&
+
+ : ignoring the worktree &&
+ git update-index --remove --ignore-skip-worktree-entries keep-me.t &&
+ git diff-index --cached --exit-code HEAD &&
+
+ : not ignoring the worktree, a deletion is staged &&
+ git update-index --remove keep-me.t &&
+ test_must_fail git diff-index --cached --exit-code HEAD \
+ --diff-filter=D -- keep-me.t
+'
+
+test_expect_success 'stash restore in sparse checkout' '
+ test_create_repo stash-restore &&
+ (
+ cd stash-restore &&
+
+ mkdir subdir &&
+ echo A >subdir/A &&
+ echo untouched >untouched &&
+ echo removeme >removeme &&
+ echo modified >modified &&
+ git add . &&
+ git commit -m Initial &&
+
+ echo AA >>subdir/A &&
+ echo addme >addme &&
+ echo tweaked >>modified &&
+ rm removeme &&
+ git add addme &&
+
+ git stash push &&
+
+ git sparse-checkout set --no-cone subdir &&
+
+ # Ensure after sparse-checkout we only have expected files
+ cat >expect <<-EOF &&
+ S modified
+ S removeme
+ H subdir/A
+ S untouched
+ EOF
+ git ls-files -t >actual &&
+ test_cmp expect actual &&
+
+ test_path_is_missing addme &&
+ test_path_is_missing modified &&
+ test_path_is_missing removeme &&
+ test_path_is_file subdir/A &&
+ test_path_is_missing untouched &&
+
+ # Put a file in the working directory in the way
+ echo in the way >modified &&
+ test_must_fail git stash apply 2>error&&
+
+ grep "changes.*would be overwritten by merge" error &&
+
+ echo in the way >expect &&
+ test_cmp expect modified &&
+ git diff --quiet HEAD ":!modified" &&
+
+ # ...and that working directory reflects the files correctly
+ test_path_is_missing addme &&
+ test_path_is_file modified &&
+ test_path_is_missing removeme &&
+ test_path_is_file subdir/A &&
+ test_path_is_missing untouched
+ )
+'
+
+#TODO test_expect_failure 'git-apply adds file' false
+#TODO test_expect_failure 'git-apply updates file' false
+#TODO test_expect_failure 'git-apply removes file' false
+#TODO test_expect_failure 'git-mv to skip-worktree' false
+#TODO test_expect_failure 'git-mv from skip-worktree' false
+#TODO test_expect_failure 'git-checkout' false
+
+test_done
diff --git a/t/t7030-verify-tag.sh b/t/t7030-verify-tag.sh
new file mode 100755
index 0000000..6f526c3
--- /dev/null
+++ b/t/t7030-verify-tag.sh
@@ -0,0 +1,206 @@
+#!/bin/sh
+
+test_description='signed tag tests'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY/lib-gpg.sh"
+
+test_expect_success GPG 'create signed tags' '
+ echo 1 >file && git add file &&
+ test_tick && git commit -m initial &&
+ git tag -s -m initial initial &&
+ git branch side &&
+
+ echo 2 >file && test_tick && git commit -a -m second &&
+ git tag -s -m second second &&
+
+ git checkout side &&
+ echo 3 >elif && git add elif &&
+ test_tick && git commit -m "third on side" &&
+
+ git checkout main &&
+ test_tick && git merge -S side &&
+ git tag -s -m merge merge &&
+
+ echo 4 >file && test_tick && git commit -a -S -m "fourth unsigned" &&
+ git tag -a -m fourth-unsigned fourth-unsigned &&
+
+ test_tick && git commit --amend -S -m "fourth signed" &&
+ git tag -s -m fourth fourth-signed &&
+
+ echo 5 >file && test_tick && git commit -a -m "fifth" &&
+ git tag fifth-unsigned &&
+
+ git config commit.gpgsign true &&
+ echo 6 >file && test_tick && git commit -a -m "sixth" &&
+ git tag -a -m sixth sixth-unsigned &&
+
+ test_tick && git rebase -f HEAD^^ && git tag -s -m 6th sixth-signed HEAD^ &&
+ git tag -m seventh -s seventh-signed &&
+
+ echo 8 >file && test_tick && git commit -a -m eighth &&
+ git tag -uB7227189 -m eighth eighth-signed-alt
+'
+
+test_expect_success GPGSM 'create signed tags x509 ' '
+ test_config gpg.format x509 &&
+ test_config user.signingkey $GIT_COMMITTER_EMAIL &&
+ echo 9 >file && test_tick && git commit -a -m "ninth gpgsm-signed" &&
+ git tag -s -m ninth ninth-signed-x509
+'
+
+test_expect_success GPG 'verify and show signatures' '
+ (
+ for tag in initial second merge fourth-signed sixth-signed seventh-signed
+ do
+ git verify-tag $tag 2>actual &&
+ grep "Good signature from" actual &&
+ ! grep "BAD signature from" actual &&
+ echo $tag OK || exit 1
+ done
+ ) &&
+ (
+ for tag in fourth-unsigned fifth-unsigned sixth-unsigned
+ do
+ test_must_fail git verify-tag $tag 2>actual &&
+ ! grep "Good signature from" actual &&
+ ! grep "BAD signature from" actual &&
+ echo $tag OK || exit 1
+ done
+ ) &&
+ (
+ for tag in eighth-signed-alt
+ do
+ git verify-tag $tag 2>actual &&
+ grep "Good signature from" actual &&
+ ! grep "BAD signature from" actual &&
+ grep "not certified" actual &&
+ echo $tag OK || exit 1
+ done
+ )
+'
+
+test_expect_success GPGSM 'verify and show signatures x509' '
+ git verify-tag ninth-signed-x509 2>actual &&
+ grep "Good signature from" actual &&
+ ! grep "BAD signature from" actual &&
+ echo ninth-signed-x509 OK
+'
+
+test_expect_success GPGSM 'verify and show signatures x509 with low minTrustLevel' '
+ test_config gpg.minTrustLevel undefined &&
+ git verify-tag ninth-signed-x509 2>actual &&
+ grep "Good signature from" actual &&
+ ! grep "BAD signature from" actual &&
+ echo ninth-signed-x509 OK
+'
+
+test_expect_success GPGSM 'verify and show signatures x509 with matching minTrustLevel' '
+ test_config gpg.minTrustLevel fully &&
+ git verify-tag ninth-signed-x509 2>actual &&
+ grep "Good signature from" actual &&
+ ! grep "BAD signature from" actual &&
+ echo ninth-signed-x509 OK
+'
+
+test_expect_success GPGSM 'verify and show signatures x509 with high minTrustLevel' '
+ test_config gpg.minTrustLevel ultimate &&
+ test_must_fail git verify-tag ninth-signed-x509 2>actual &&
+ grep "Good signature from" actual &&
+ ! grep "BAD signature from" actual &&
+ echo ninth-signed-x509 OK
+'
+
+test_expect_success GPG 'detect fudged signature' '
+ git cat-file tag seventh-signed >raw &&
+ sed -e "/^tag / s/seventh/7th-forged/" raw >forged1 &&
+ git hash-object -w -t tag forged1 >forged1.tag &&
+ test_must_fail git verify-tag $(cat forged1.tag) 2>actual1 &&
+ grep "BAD signature from" actual1 &&
+ ! grep "Good signature from" actual1
+'
+
+test_expect_success GPG 'verify signatures with --raw' '
+ (
+ for tag in initial second merge fourth-signed sixth-signed seventh-signed
+ do
+ git verify-tag --raw $tag 2>actual &&
+ grep "GOODSIG" actual &&
+ ! grep "BADSIG" actual &&
+ echo $tag OK || exit 1
+ done
+ ) &&
+ (
+ for tag in fourth-unsigned fifth-unsigned sixth-unsigned
+ do
+ test_must_fail git verify-tag --raw $tag 2>actual &&
+ ! grep "GOODSIG" actual &&
+ ! grep "BADSIG" actual &&
+ echo $tag OK || exit 1
+ done
+ ) &&
+ (
+ for tag in eighth-signed-alt
+ do
+ git verify-tag --raw $tag 2>actual &&
+ grep "GOODSIG" actual &&
+ ! grep "BADSIG" actual &&
+ grep "TRUST_UNDEFINED" actual &&
+ echo $tag OK || exit 1
+ done
+ )
+'
+
+test_expect_success GPGSM 'verify signatures with --raw x509' '
+ git verify-tag --raw ninth-signed-x509 2>actual &&
+ grep "GOODSIG" actual &&
+ ! grep "BADSIG" actual &&
+ echo ninth-signed-x509 OK
+'
+
+test_expect_success GPG 'verify multiple tags' '
+ tags="fourth-signed sixth-signed seventh-signed" &&
+ for i in $tags
+ do
+ git verify-tag -v --raw $i || return 1
+ done >expect.stdout 2>expect.stderr.1 &&
+ grep "^.GNUPG:." <expect.stderr.1 >expect.stderr &&
+ git verify-tag -v --raw $tags >actual.stdout 2>actual.stderr.1 &&
+ grep "^.GNUPG:." <actual.stderr.1 >actual.stderr &&
+ test_cmp expect.stdout actual.stdout &&
+ test_cmp expect.stderr actual.stderr
+'
+
+test_expect_success GPGSM 'verify multiple tags x509' '
+ tags="seventh-signed ninth-signed-x509" &&
+ for i in $tags
+ do
+ git verify-tag -v --raw $i || return 1
+ done >expect.stdout 2>expect.stderr.1 &&
+ grep "^.GNUPG:." <expect.stderr.1 >expect.stderr &&
+ git verify-tag -v --raw $tags >actual.stdout 2>actual.stderr.1 &&
+ grep "^.GNUPG:." <actual.stderr.1 >actual.stderr &&
+ test_cmp expect.stdout actual.stdout &&
+ test_cmp expect.stderr actual.stderr
+'
+
+test_expect_success GPG 'verifying tag with --format' '
+ cat >expect <<-\EOF &&
+ tagname : fourth-signed
+ EOF
+ git verify-tag --format="tagname : %(tag)" "fourth-signed" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success GPG 'verifying tag with --format="%(rest)" must fail' '
+ test_must_fail git verify-tag --format="%(rest)" "fourth-signed"
+'
+
+test_expect_success GPG 'verifying a forged tag with --format should fail silently' '
+ test_must_fail git verify-tag --format="tagname : %(tag)" $(cat forged1.tag) >actual-forged &&
+ test_must_be_empty actual-forged
+'
+
+test_done
diff --git a/t/t7031-verify-tag-signed-ssh.sh b/t/t7031-verify-tag-signed-ssh.sh
new file mode 100755
index 0000000..20913b3
--- /dev/null
+++ b/t/t7031-verify-tag-signed-ssh.sh
@@ -0,0 +1,213 @@
+#!/bin/sh
+
+test_description='signed tag tests'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY/lib-gpg.sh"
+
+test_expect_success GPGSSH 'create signed tags ssh' '
+ test_when_finished "test_unconfig commit.gpgsign" &&
+ test_config gpg.format ssh &&
+ test_config user.signingkey "${GPGSSH_KEY_PRIMARY}" &&
+
+ echo 1 >file && git add file &&
+ test_tick && git commit -m initial &&
+ git tag -s -m initial initial &&
+ git branch side &&
+
+ echo 2 >file && test_tick && git commit -a -m second &&
+ git tag -s -m second second &&
+
+ git checkout side &&
+ echo 3 >elif && git add elif &&
+ test_tick && git commit -m "third on side" &&
+
+ git checkout main &&
+ test_tick && git merge -S side &&
+ git tag -s -m merge merge &&
+
+ echo 4 >file && test_tick && git commit -a -S -m "fourth unsigned" &&
+ git tag -a -m fourth-unsigned fourth-unsigned &&
+
+ test_tick && git commit --amend -S -m "fourth signed" &&
+ git tag -s -m fourth fourth-signed &&
+
+ echo 5 >file && test_tick && git commit -a -m "fifth" &&
+ git tag fifth-unsigned &&
+
+ git config commit.gpgsign true &&
+ echo 6 >file && test_tick && git commit -a -m "sixth" &&
+ git tag -a -m sixth sixth-unsigned &&
+
+ test_tick && git rebase -f HEAD^^ && git tag -s -m 6th sixth-signed HEAD^ &&
+ git tag -m seventh -s seventh-signed &&
+
+ echo 8 >file && test_tick && git commit -a -m eighth &&
+ git tag -u"${GPGSSH_KEY_UNTRUSTED}" -m eighth eighth-signed-alt
+'
+
+test_expect_success GPGSSH,GPGSSH_VERIFYTIME 'create signed tags with keys having defined lifetimes' '
+ test_when_finished "test_unconfig commit.gpgsign" &&
+ test_config gpg.format ssh &&
+
+ echo expired >file && test_tick && git commit -a -m expired -S"${GPGSSH_KEY_EXPIRED}" &&
+ git tag -s -u "${GPGSSH_KEY_EXPIRED}" -m expired-signed expired-signed &&
+
+ echo notyetvalid >file && test_tick && git commit -a -m notyetvalid -S"${GPGSSH_KEY_NOTYETVALID}" &&
+ git tag -s -u "${GPGSSH_KEY_NOTYETVALID}" -m notyetvalid-signed notyetvalid-signed &&
+
+ echo timeboxedvalid >file && test_tick && git commit -a -m timeboxedvalid -S"${GPGSSH_KEY_TIMEBOXEDVALID}" &&
+ git tag -s -u "${GPGSSH_KEY_TIMEBOXEDVALID}" -m timeboxedvalid-signed timeboxedvalid-signed &&
+
+ echo timeboxedinvalid >file && test_tick && git commit -a -m timeboxedinvalid -S"${GPGSSH_KEY_TIMEBOXEDINVALID}" &&
+ git tag -s -u "${GPGSSH_KEY_TIMEBOXEDINVALID}" -m timeboxedinvalid-signed timeboxedinvalid-signed
+'
+
+test_expect_success GPGSSH 'verify and show ssh signatures' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ (
+ for tag in initial second merge fourth-signed sixth-signed seventh-signed
+ do
+ git verify-tag $tag 2>actual &&
+ grep "${GPGSSH_GOOD_SIGNATURE_TRUSTED}" actual &&
+ ! grep "${GPGSSH_BAD_SIGNATURE}" actual &&
+ echo $tag OK || exit 1
+ done
+ ) &&
+ (
+ for tag in fourth-unsigned fifth-unsigned sixth-unsigned
+ do
+ test_must_fail git verify-tag $tag 2>actual &&
+ ! grep "${GPGSSH_GOOD_SIGNATURE_TRUSTED}" actual &&
+ ! grep "${GPGSSH_BAD_SIGNATURE}" actual &&
+ echo $tag OK || exit 1
+ done
+ ) &&
+ (
+ for tag in eighth-signed-alt
+ do
+ test_must_fail git verify-tag $tag 2>actual &&
+ grep "${GPGSSH_GOOD_SIGNATURE_UNTRUSTED}" actual &&
+ ! grep "${GPGSSH_BAD_SIGNATURE}" actual &&
+ grep "${GPGSSH_KEY_NOT_TRUSTED}" actual &&
+ echo $tag OK || exit 1
+ done
+ )
+'
+
+test_expect_success GPGSSH,GPGSSH_VERIFYTIME 'verify-tag exits failure on expired signature key' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ test_must_fail git verify-tag expired-signed 2>actual &&
+ ! grep "${GPGSSH_GOOD_SIGNATURE_TRUSTED}" actual
+'
+
+test_expect_success GPGSSH,GPGSSH_VERIFYTIME 'verify-tag exits failure on not yet valid signature key' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ test_must_fail git verify-tag notyetvalid-signed 2>actual &&
+ ! grep "${GPGSSH_GOOD_SIGNATURE_TRUSTED}" actual
+'
+
+test_expect_success GPGSSH,GPGSSH_VERIFYTIME 'verify-tag succeeds with tag date and key validity matching' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ git verify-tag timeboxedvalid-signed 2>actual &&
+ grep "${GPGSSH_GOOD_SIGNATURE_TRUSTED}" actual &&
+ ! grep "${GPGSSH_BAD_SIGNATURE}" actual
+'
+
+test_expect_success GPGSSH,GPGSSH_VERIFYTIME 'verify-tag failes with tag date outside of key validity' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ test_must_fail git verify-tag timeboxedinvalid-signed 2>actual &&
+ ! grep "${GPGSSH_GOOD_SIGNATURE_TRUSTED}" actual
+'
+
+test_expect_success GPGSSH 'detect fudged ssh signature' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ git cat-file tag seventh-signed >raw &&
+ sed -e "/^tag / s/seventh/7th-forged/" raw >forged1 &&
+ git hash-object -w -t tag forged1 >forged1.tag &&
+ test_must_fail git verify-tag $(cat forged1.tag) 2>actual1 &&
+ grep "${GPGSSH_BAD_SIGNATURE}" actual1 &&
+ ! grep "${GPGSSH_GOOD_SIGNATURE_TRUSTED}" actual1 &&
+ ! grep "${GPGSSH_GOOD_SIGNATURE_UNTRUSTED}" actual1
+'
+
+test_expect_success GPGSSH 'verify ssh signatures with --raw' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ (
+ for tag in initial second merge fourth-signed sixth-signed seventh-signed
+ do
+ git verify-tag --raw $tag 2>actual &&
+ grep "${GPGSSH_GOOD_SIGNATURE_TRUSTED}" actual &&
+ ! grep "${GPGSSH_BAD_SIGNATURE}" actual &&
+ echo $tag OK || exit 1
+ done
+ ) &&
+ (
+ for tag in fourth-unsigned fifth-unsigned sixth-unsigned
+ do
+ test_must_fail git verify-tag --raw $tag 2>actual &&
+ ! grep "${GPGSSH_GOOD_SIGNATURE_TRUSTED}" actual &&
+ ! grep "${GPGSSH_BAD_SIGNATURE}" actual &&
+ echo $tag OK || exit 1
+ done
+ ) &&
+ (
+ for tag in eighth-signed-alt
+ do
+ test_must_fail git verify-tag --raw $tag 2>actual &&
+ grep "${GPGSSH_GOOD_SIGNATURE_UNTRUSTED}" actual &&
+ ! grep "${GPGSSH_BAD_SIGNATURE}" actual &&
+ echo $tag OK || exit 1
+ done
+ )
+'
+
+test_expect_success GPGSSH 'verify signatures with --raw ssh' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ git verify-tag --raw sixth-signed 2>actual &&
+ grep "${GPGSSH_GOOD_SIGNATURE_TRUSTED}" actual &&
+ ! grep "${GPGSSH_BAD_SIGNATURE}" actual &&
+ echo sixth-signed OK
+'
+
+test_expect_success GPGSSH 'verify multiple tags ssh' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ tags="seventh-signed sixth-signed" &&
+ for i in $tags
+ do
+ git verify-tag -v --raw $i || return 1
+ done >expect.stdout 2>expect.stderr.1 &&
+ grep "^${GPGSSH_GOOD_SIGNATURE_TRUSTED}" <expect.stderr.1 >expect.stderr &&
+ git verify-tag -v --raw $tags >actual.stdout 2>actual.stderr.1 &&
+ grep "^${GPGSSH_GOOD_SIGNATURE_TRUSTED}" <actual.stderr.1 >actual.stderr &&
+ test_cmp expect.stdout actual.stdout &&
+ test_cmp expect.stderr actual.stderr
+'
+
+test_expect_success GPGSSH 'verifying tag with --format - ssh' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ cat >expect <<-\EOF &&
+ tagname : fourth-signed
+ EOF
+ git verify-tag --format="tagname : %(tag)" "fourth-signed" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success GPGSSH 'verifying a forged tag with --format should fail silently - ssh' '
+ test_must_fail git verify-tag --format="tagname : %(tag)" $(cat forged1.tag) >actual-forged &&
+ test_must_be_empty actual-forged
+'
+
+test_expect_success GPGSSH 'rev-list --format=%G' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ git rev-list -1 --format="%G? %H" sixth-signed >actual &&
+ cat >expect <<-EOF &&
+ commit $(git rev-parse sixth-signed^0)
+ G $(git rev-parse sixth-signed^0)
+ EOF
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t7060-wtstatus.sh b/t/t7060-wtstatus.sh
new file mode 100755
index 0000000..aaeb4a5
--- /dev/null
+++ b/t/t7060-wtstatus.sh
@@ -0,0 +1,255 @@
+#!/bin/sh
+
+test_description='basic work tree status reporting'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ git config --global advice.statusuoption false &&
+ test_commit A &&
+ test_commit B oneside added &&
+ git checkout A^0 &&
+ test_commit C oneside created
+'
+
+test_expect_success 'A/A conflict' '
+ git checkout B^0 &&
+ test_must_fail git merge C
+'
+
+test_expect_success 'Report path with conflict' '
+ git diff --cached --name-status >actual &&
+ echo "U oneside" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Report new path with conflict' '
+ git diff --cached --name-status HEAD^ >actual &&
+ echo "U oneside" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'M/D conflict does not segfault' '
+ cat >expect <<EOF &&
+On branch side
+You have unmerged paths.
+ (fix conflicts and run "git commit")
+ (use "git merge --abort" to abort the merge)
+
+Unmerged paths:
+ (use "git add/rm <file>..." as appropriate to mark resolution)
+ deleted by us: foo
+
+no changes added to commit (use "git add" and/or "git commit -a")
+EOF
+ mkdir mdconflict &&
+ (
+ cd mdconflict &&
+ git init &&
+ test_commit initial foo "" &&
+ test_commit modify foo foo &&
+ git checkout -b side HEAD^ &&
+ git rm foo &&
+ git commit -m delete &&
+ test_must_fail git merge main &&
+ test_must_fail git commit --dry-run >../actual &&
+ test_cmp ../expect ../actual &&
+ git status >../actual &&
+ test_cmp ../expect ../actual
+ )
+'
+
+test_expect_success 'rename & unmerged setup' '
+ git rm -f -r . &&
+ cat "$TEST_DIRECTORY/README" >ONE &&
+ git add ONE &&
+ test_tick &&
+ git commit -m "One commit with ONE" &&
+
+ echo Modified >TWO &&
+ cat ONE >>TWO &&
+ cat ONE >>THREE &&
+ git add TWO THREE &&
+ sha1=$(git rev-parse :ONE) &&
+ git rm --cached ONE &&
+ (
+ echo "100644 $sha1 1 ONE" &&
+ echo "100644 $sha1 2 ONE" &&
+ echo "100644 $sha1 3 ONE"
+ ) | git update-index --index-info &&
+ echo Further >>THREE
+'
+
+test_expect_success 'rename & unmerged status' '
+ git status -suno >actual &&
+ cat >expect <<-EOF &&
+ UU ONE
+ AM THREE
+ A TWO
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'git diff-index --cached shows 2 added + 1 unmerged' '
+ cat >expected <<-EOF &&
+ U ONE
+ A THREE
+ A TWO
+ EOF
+ git diff-index --cached --name-status HEAD >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'git diff-index --cached -M shows 2 added + 1 unmerged' '
+ cat >expected <<-EOF &&
+ U ONE
+ A THREE
+ A TWO
+ EOF
+ git diff-index --cached -M --name-status HEAD >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'git diff-index --cached -C shows 2 copies + 1 unmerged' '
+ cat >expected <<-EOF &&
+ U ONE
+ C ONE THREE
+ C ONE TWO
+ EOF
+ git diff-index --cached -C --name-status HEAD |
+ sed "s/^C[0-9]*/C/g" >actual &&
+ test_cmp expected actual
+'
+
+
+test_expect_success 'status when conflicts with add and rm advice (deleted by them)' '
+ git reset --hard &&
+ git checkout main &&
+ test_commit init main.txt init &&
+ git checkout -b second_branch &&
+ git rm main.txt &&
+ git commit -m "main.txt deleted on second_branch" &&
+ test_commit second conflict.txt second &&
+ git checkout main &&
+ test_commit on_second main.txt on_second &&
+ test_commit main conflict.txt main &&
+ test_must_fail git merge second_branch &&
+ cat >expected <<\EOF &&
+On branch main
+You have unmerged paths.
+ (fix conflicts and run "git commit")
+ (use "git merge --abort" to abort the merge)
+
+Unmerged paths:
+ (use "git add/rm <file>..." as appropriate to mark resolution)
+ both added: conflict.txt
+ deleted by them: main.txt
+
+no changes added to commit (use "git add" and/or "git commit -a")
+EOF
+ git status --untracked-files=no >actual &&
+ test_cmp expected actual
+'
+
+
+test_expect_success 'prepare for conflicts' '
+ git reset --hard &&
+ git checkout -b conflict &&
+ test_commit one main.txt one &&
+ git branch conflict_second &&
+ git mv main.txt sub_main.txt &&
+ git commit -m "main.txt renamed in sub_main.txt" &&
+ git checkout conflict_second &&
+ git mv main.txt sub_second.txt &&
+ git commit -m "main.txt renamed in sub_second.txt"
+'
+
+
+test_expect_success 'status when conflicts with add and rm advice (both deleted)' '
+ test_must_fail git merge conflict &&
+ cat >expected <<\EOF &&
+On branch conflict_second
+You have unmerged paths.
+ (fix conflicts and run "git commit")
+ (use "git merge --abort" to abort the merge)
+
+Unmerged paths:
+ (use "git add/rm <file>..." as appropriate to mark resolution)
+ both deleted: main.txt
+ added by them: sub_main.txt
+ added by us: sub_second.txt
+
+no changes added to commit (use "git add" and/or "git commit -a")
+EOF
+ git status --untracked-files=no >actual &&
+ test_cmp expected actual
+'
+
+
+test_expect_success 'status when conflicts with only rm advice (both deleted)' '
+ git reset --hard conflict_second &&
+ test_must_fail git merge conflict &&
+ git add sub_main.txt &&
+ git add sub_second.txt &&
+ cat >expected <<\EOF &&
+On branch conflict_second
+You have unmerged paths.
+ (fix conflicts and run "git commit")
+ (use "git merge --abort" to abort the merge)
+
+Changes to be committed:
+ new file: sub_main.txt
+
+Unmerged paths:
+ (use "git rm <file>..." to mark resolution)
+ both deleted: main.txt
+
+Untracked files not listed (use -u option to show untracked files)
+EOF
+ git status --untracked-files=no >actual &&
+ test_cmp expected actual &&
+ git reset --hard &&
+ git checkout main
+'
+
+test_expect_success 'status --branch with detached HEAD' '
+ git reset --hard &&
+ git checkout main^0 &&
+ git status --branch --porcelain >actual &&
+ cat >expected <<-EOF &&
+ ## HEAD (no branch)
+ ?? .gitconfig
+ ?? actual
+ ?? expect
+ ?? expected
+ ?? mdconflict/
+ EOF
+ test_cmp expected actual
+'
+
+## Duplicate the above test and verify --porcelain=v1 arg parsing.
+test_expect_success 'status --porcelain=v1 --branch with detached HEAD' '
+ git reset --hard &&
+ git checkout main^0 &&
+ git status --branch --porcelain=v1 >actual &&
+ cat >expected <<-EOF &&
+ ## HEAD (no branch)
+ ?? .gitconfig
+ ?? actual
+ ?? expect
+ ?? expected
+ ?? mdconflict/
+ EOF
+ test_cmp expected actual
+'
+
+## Verify parser error on invalid --porcelain argument.
+test_expect_success 'status --porcelain=bogus' '
+ test_must_fail git status --porcelain=bogus
+'
+
+test_done
diff --git a/t/t7061-wtstatus-ignore.sh b/t/t7061-wtstatus-ignore.sh
new file mode 100755
index 0000000..2f9bea9
--- /dev/null
+++ b/t/t7061-wtstatus-ignore.sh
@@ -0,0 +1,316 @@
+#!/bin/sh
+
+test_description='git-status ignored files'
+
+. ./test-lib.sh
+
+cat >expected <<\EOF
+?? .gitignore
+?? actual
+?? expected
+?? untracked/
+!! untracked/ignored
+EOF
+
+test_expect_success 'status untracked directory with --ignored' '
+ echo "ignored" >.gitignore &&
+ mkdir untracked &&
+ : >untracked/ignored &&
+ : >untracked/uncommitted &&
+ git status --porcelain --ignored >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'same with gitignore starting with BOM' '
+ printf "\357\273\277ignored\n" >.gitignore &&
+ mkdir -p untracked &&
+ : >untracked/ignored &&
+ : >untracked/uncommitted &&
+ git status --porcelain --ignored >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'status untracked files --ignored with pathspec (no match)' '
+ git status --porcelain --ignored -- untracked/i >actual &&
+ test_must_be_empty actual &&
+ git status --porcelain --ignored -- untracked/u >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'status untracked files --ignored with pathspec (literal match)' '
+ git status --porcelain --ignored -- untracked/ignored >actual &&
+ echo "!! untracked/ignored" >expected &&
+ test_cmp expected actual &&
+ git status --porcelain --ignored -- untracked/uncommitted >actual &&
+ echo "?? untracked/uncommitted" >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'status untracked files --ignored with pathspec (glob match)' '
+ git status --porcelain --ignored -- untracked/i\* >actual &&
+ echo "!! untracked/ignored" >expected &&
+ test_cmp expected actual &&
+ git status --porcelain --ignored -- untracked/u\* >actual &&
+ echo "?? untracked/uncommitted" >expected &&
+ test_cmp expected actual
+'
+
+cat >expected <<\EOF
+?? .gitignore
+?? actual
+?? expected
+?? untracked/uncommitted
+!! untracked/ignored
+EOF
+
+test_expect_success 'status untracked directory with --ignored -u' '
+ git status --porcelain --ignored -u >actual &&
+ test_cmp expected actual
+'
+cat >expected <<\EOF
+?? untracked/
+!! untracked/ignored
+EOF
+
+test_expect_success 'status of untracked directory with --ignored works with or without prefix' '
+ git status --porcelain --ignored >tmp &&
+ grep untracked/ tmp >actual &&
+ rm tmp &&
+ test_cmp expected actual &&
+
+ git status --porcelain --ignored untracked/ >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<\EOF
+?? untracked/uncommitted
+!! untracked/ignored
+EOF
+
+test_expect_success 'status prefixed untracked sub-directory with --ignored -u' '
+ git status --porcelain --ignored -u untracked/ >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<\EOF
+?? .gitignore
+?? actual
+?? expected
+!! ignored/
+EOF
+
+test_expect_success 'status ignored directory with --ignore' '
+ rm -rf untracked &&
+ mkdir ignored &&
+ : >ignored/uncommitted &&
+ git status --porcelain --ignored >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<\EOF
+?? .gitignore
+?? actual
+?? expected
+!! ignored/uncommitted
+EOF
+
+test_expect_success 'status ignored directory with --ignore -u' '
+ git status --porcelain --ignored -u >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<\EOF
+?? .gitignore
+?? actual
+?? expected
+EOF
+
+test_expect_success 'status empty untracked directory with --ignore' '
+ rm -rf ignored &&
+ mkdir untracked-ignored &&
+ mkdir untracked-ignored/test &&
+ git status --porcelain --ignored >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<\EOF
+?? .gitignore
+?? actual
+?? expected
+EOF
+
+test_expect_success 'status empty untracked directory with --ignore -u' '
+ git status --porcelain --ignored -u >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<\EOF
+?? .gitignore
+?? actual
+?? expected
+!! untracked-ignored/
+EOF
+
+test_expect_success 'status untracked directory with ignored files with --ignore' '
+ : >untracked-ignored/ignored &&
+ : >untracked-ignored/test/ignored &&
+ git status --porcelain --ignored >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<\EOF
+?? .gitignore
+?? actual
+?? expected
+!! untracked-ignored/ignored
+!! untracked-ignored/test/ignored
+EOF
+
+test_expect_success 'status untracked directory with ignored files with --ignore -u' '
+ git status --porcelain --ignored -u >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<\EOF
+?? .gitignore
+?? actual
+?? expected
+EOF
+
+test_expect_success 'status ignored tracked directory with --ignore' '
+ rm -rf untracked-ignored &&
+ mkdir tracked &&
+ : >tracked/committed &&
+ git add tracked/committed &&
+ git commit -m. &&
+ echo "tracked" >.gitignore &&
+ git status --porcelain --ignored >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<\EOF
+?? .gitignore
+?? actual
+?? expected
+EOF
+
+test_expect_success 'status ignored tracked directory with --ignore -u' '
+ git status --porcelain --ignored -u >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<\EOF
+?? .gitignore
+?? actual
+?? expected
+EOF
+
+test_expect_success 'status ignored tracked directory and ignored file with --ignore' '
+ echo "committed" >>.gitignore &&
+ git status --porcelain --ignored >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<\EOF
+?? .gitignore
+?? actual
+?? expected
+EOF
+
+test_expect_success 'status ignored tracked directory and ignored file with --ignore -u' '
+ git status --porcelain --ignored -u >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<\EOF
+?? .gitignore
+?? actual
+?? expected
+!! tracked/uncommitted
+EOF
+
+test_expect_success 'status ignored tracked directory and uncommitted file with --ignore' '
+ echo "tracked" >.gitignore &&
+ : >tracked/uncommitted &&
+ git status --porcelain --ignored >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<\EOF
+?? .gitignore
+?? actual
+?? expected
+!! tracked/uncommitted
+EOF
+
+test_expect_success 'status ignored tracked directory and uncommitted file with --ignore -u' '
+ git status --porcelain --ignored -u >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<\EOF
+?? .gitignore
+?? actual
+?? expected
+!! tracked/ignored/
+EOF
+
+test_expect_success 'status ignored tracked directory with uncommitted file in untracked subdir with --ignore' '
+ rm -rf tracked/uncommitted &&
+ mkdir tracked/ignored &&
+ : >tracked/ignored/uncommitted &&
+ git status --porcelain --ignored >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<\EOF
+?? .gitignore
+?? actual
+?? expected
+!! tracked/ignored/uncommitted
+EOF
+
+test_expect_success 'status ignored tracked directory with uncommitted file in untracked subdir with --ignore -u' '
+ git status --porcelain --ignored -u >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<\EOF
+?? .gitignore
+?? actual
+?? expected
+!! tracked/ignored/uncommitted
+EOF
+
+test_expect_success 'status ignored tracked directory with uncommitted file in tracked subdir with --ignore' '
+ : >tracked/ignored/committed &&
+ git add -f tracked/ignored/committed &&
+ git commit -m. &&
+ git status --porcelain --ignored >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<\EOF
+?? .gitignore
+?? actual
+?? expected
+!! tracked/ignored/uncommitted
+EOF
+
+test_expect_success 'status ignored tracked directory with uncommitted file in tracked subdir with --ignore -u' '
+ git status --porcelain --ignored -u >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<\EOF
+!! tracked/submodule/
+EOF
+
+test_expect_success 'status ignores submodule in excluded directory' '
+ git init tracked/submodule &&
+ test_commit -C tracked/submodule initial &&
+ git status --porcelain --ignored -u tracked/submodule >actual &&
+ test_cmp expected actual
+'
+
+test_done
diff --git a/t/t7062-wtstatus-ignorecase.sh b/t/t7062-wtstatus-ignorecase.sh
new file mode 100755
index 0000000..caf372a
--- /dev/null
+++ b/t/t7062-wtstatus-ignorecase.sh
@@ -0,0 +1,21 @@
+#!/bin/sh
+
+test_description='git-status with core.ignorecase=true'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'status with hash collisions' '
+ # note: "V/", "V/XQANY/" and "WURZAUP/" produce the same hash code
+ # in name-hash.c::hash_name
+ mkdir V &&
+ mkdir V/XQANY &&
+ mkdir WURZAUP &&
+ touch V/XQANY/test &&
+ git config core.ignorecase true &&
+ git add . &&
+ # test is successful if git status completes (no endless loop)
+ git status
+'
+
+test_done
diff --git a/t/t7063-status-untracked-cache.sh b/t/t7063-status-untracked-cache.sh
new file mode 100755
index 0000000..8929ef4
--- /dev/null
+++ b/t/t7063-status-untracked-cache.sh
@@ -0,0 +1,994 @@
+#!/bin/sh
+
+test_description='test untracked cache'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+# On some filesystems (e.g. FreeBSD's ext2 and ufs) directory mtime
+# is updated lazily after contents in the directory changes, which
+# forces the untracked cache code to take the slow path. A test
+# that wants to make sure that the fast path works correctly should
+# call this helper to make mtime of the containing directory in sync
+# with the reality before checking the fast path behaviour.
+#
+# See <20160803174522.5571-1-pclouds@gmail.com> if you want to know
+# more.
+
+GIT_FORCE_UNTRACKED_CACHE=true
+export GIT_FORCE_UNTRACKED_CACHE
+
+sync_mtime () {
+ find . -type d -exec ls -ld {} + >/dev/null
+}
+
+avoid_racy() {
+ sleep 1
+}
+
+status_is_clean() {
+ git status --porcelain >../status.actual &&
+ test_must_be_empty ../status.actual
+}
+
+# Ignore_Untracked_Cache, abbreviated to 3 letters because then people can
+# compare commands side-by-side, e.g.
+# iuc status --porcelain >expect &&
+# git status --porcelain >actual &&
+# test_cmp expect actual
+iuc () {
+ git ls-files -s >../current-index-entries
+ git ls-files -t | sed -ne s/^S.//p >../current-sparse-entries
+
+ GIT_INDEX_FILE=.git/tmp_index
+ export GIT_INDEX_FILE
+ git update-index --index-info <../current-index-entries
+ git update-index --skip-worktree $(cat ../current-sparse-entries)
+
+ git -c core.untrackedCache=false "$@"
+ ret=$?
+
+ rm ../current-index-entries
+ rm $GIT_INDEX_FILE
+ unset GIT_INDEX_FILE
+
+ return $ret
+}
+
+get_relevant_traces () {
+ # From the GIT_TRACE2_PERF data of the form
+ # $TIME $FILE:$LINE | d0 | main | data | r1 | ? | ? | read_directo | $RELEVANT_STAT
+ # extract the $RELEVANT_STAT fields. We don't care about region_enter
+ # or region_leave, or stats for things outside read_directory.
+ INPUT_FILE=$1
+ OUTPUT_FILE=$2
+ grep data.*read_directo $INPUT_FILE |
+ cut -d "|" -f 9 |
+ grep -v visited \
+ >"$OUTPUT_FILE"
+}
+
+
+test_lazy_prereq UNTRACKED_CACHE '
+ { git update-index --test-untracked-cache; ret=$?; } &&
+ test $ret -ne 1
+'
+
+if ! test_have_prereq UNTRACKED_CACHE; then
+ skip_all='This system does not support untracked cache'
+ test_done
+fi
+
+test_expect_success 'core.untrackedCache is unset' '
+ test_must_fail git config --get core.untrackedCache
+'
+
+test_expect_success 'setup' '
+ git init --template= worktree &&
+ cd worktree &&
+ mkdir done dtwo dthree &&
+ touch one two three done/one dtwo/two dthree/three &&
+ test-tool chmtime =-300 one two three done/one dtwo/two dthree/three &&
+ test-tool chmtime =-300 done dtwo dthree &&
+ test-tool chmtime =-300 . &&
+ git add one two done/one &&
+ mkdir .git/info &&
+ : >.git/info/exclude &&
+ git update-index --untracked-cache &&
+ test_oid_cache <<-EOF
+ root sha1:e6fcc8f2ee31bae321d66afd183fcb7237afae6e
+ root sha256:b90c672088c015b9c83876e919da311bad4cd39639fb139f988af6a11493b974
+
+ exclude sha1:13263c0978fb9fad16b2d580fb800b6d811c3ff0
+ exclude sha256:fe4aaa1bbbbce4cb8f73426748a14c5ad6026b26f90505a0bf2494b165a5b76c
+
+ done sha1:1946f0437f90c5005533cbe1736a6451ca301714
+ done sha256:7f079501d79f665b3acc50f5e0e9e94509084d5032ac20113a37dd5029b757cc
+ EOF
+'
+
+test_expect_success 'untracked cache is empty' '
+ test-tool dump-untracked-cache >../actual &&
+ cat >../expect-empty <<EOF &&
+info/exclude $ZERO_OID
+core.excludesfile $ZERO_OID
+exclude_per_dir .gitignore
+flags 00000006
+EOF
+ test_cmp ../expect-empty ../actual
+'
+
+cat >../status.expect <<EOF &&
+A done/one
+A one
+A two
+?? dthree/
+?? dtwo/
+?? three
+EOF
+
+cat >../dump.expect <<EOF &&
+info/exclude $EMPTY_BLOB
+core.excludesfile $ZERO_OID
+exclude_per_dir .gitignore
+flags 00000006
+/ $ZERO_OID recurse valid
+dthree/
+dtwo/
+three
+/done/ $ZERO_OID recurse valid
+/dthree/ $ZERO_OID recurse check_only valid
+three
+/dtwo/ $ZERO_OID recurse check_only valid
+two
+EOF
+
+test_expect_success 'status first time (empty cache)' '
+ : >../trace.output &&
+ GIT_TRACE2_PERF="$TRASH_DIRECTORY/trace.output" \
+ git status --porcelain >../actual &&
+ iuc status --porcelain >../status.iuc &&
+ test_cmp ../status.expect ../status.iuc &&
+ test_cmp ../status.expect ../actual &&
+ get_relevant_traces ../trace.output ../trace.relevant &&
+ cat >../trace.expect <<EOF &&
+ ....path:
+ ....node-creation:3
+ ....gitignore-invalidation:1
+ ....directory-invalidation:0
+ ....opendir:4
+EOF
+ test_cmp ../trace.expect ../trace.relevant
+'
+
+test_expect_success 'untracked cache after first status' '
+ test-tool dump-untracked-cache >../actual &&
+ test_cmp ../dump.expect ../actual
+'
+
+test_expect_success 'status second time (fully populated cache)' '
+ : >../trace.output &&
+ GIT_TRACE2_PERF="$TRASH_DIRECTORY/trace.output" \
+ git status --porcelain >../actual &&
+ iuc status --porcelain >../status.iuc &&
+ test_cmp ../status.expect ../status.iuc &&
+ test_cmp ../status.expect ../actual &&
+ get_relevant_traces ../trace.output ../trace.relevant &&
+ cat >../trace.expect <<EOF &&
+ ....path:
+ ....node-creation:0
+ ....gitignore-invalidation:0
+ ....directory-invalidation:0
+ ....opendir:0
+EOF
+ test_cmp ../trace.expect ../trace.relevant
+'
+
+test_expect_success 'untracked cache after second status' '
+ test-tool dump-untracked-cache >../actual &&
+ test_cmp ../dump.expect ../actual
+'
+
+cat >../status_uall.expect <<EOF &&
+A done/one
+A one
+A two
+?? dthree/three
+?? dtwo/two
+?? three
+EOF
+
+# Bypassing the untracked cache here is not desirable from an
+# end-user perspective, but is expected in the current design.
+# The untracked cache data stored for a -unormal run cannot be
+# correctly used in a -uall run - it would yield incorrect output.
+test_expect_success 'untracked cache is bypassed with -uall' '
+ : >../trace.output &&
+ GIT_TRACE2_PERF="$TRASH_DIRECTORY/trace.output" \
+ git status -uall --porcelain >../actual &&
+ iuc status -uall --porcelain >../status.iuc &&
+ test_cmp ../status_uall.expect ../status.iuc &&
+ test_cmp ../status_uall.expect ../actual &&
+ get_relevant_traces ../trace.output ../trace.relevant &&
+ cat >../trace.expect <<EOF &&
+ ....path:
+EOF
+ test_cmp ../trace.expect ../trace.relevant
+'
+
+test_expect_success 'untracked cache remains after bypass' '
+ test-tool dump-untracked-cache >../actual &&
+ test_cmp ../dump.expect ../actual
+'
+
+test_expect_success 'if -uall is configured, untracked cache gets populated by default' '
+ test_config status.showuntrackedfiles all &&
+ : >../trace.output &&
+ GIT_TRACE2_PERF="$TRASH_DIRECTORY/trace.output" \
+ git status --porcelain >../actual &&
+ iuc status --porcelain >../status.iuc &&
+ test_cmp ../status_uall.expect ../status.iuc &&
+ test_cmp ../status_uall.expect ../actual &&
+ get_relevant_traces ../trace.output ../trace.relevant &&
+ cat >../trace.expect <<EOF &&
+ ....path:
+ ....node-creation:3
+ ....gitignore-invalidation:1
+ ....directory-invalidation:0
+ ....opendir:4
+EOF
+ test_cmp ../trace.expect ../trace.relevant
+'
+
+cat >../dump_uall.expect <<EOF &&
+info/exclude $EMPTY_BLOB
+core.excludesfile $ZERO_OID
+exclude_per_dir .gitignore
+flags 00000000
+/ $ZERO_OID recurse valid
+three
+/done/ $ZERO_OID recurse valid
+/dthree/ $ZERO_OID recurse valid
+three
+/dtwo/ $ZERO_OID recurse valid
+two
+EOF
+
+test_expect_success 'if -uall was configured, untracked cache is populated' '
+ test-tool dump-untracked-cache >../actual &&
+ test_cmp ../dump_uall.expect ../actual
+'
+
+test_expect_success 'if -uall is configured, untracked cache is used by default' '
+ test_config status.showuntrackedfiles all &&
+ : >../trace.output &&
+ GIT_TRACE2_PERF="$TRASH_DIRECTORY/trace.output" \
+ git status --porcelain >../actual &&
+ iuc status --porcelain >../status.iuc &&
+ test_cmp ../status_uall.expect ../status.iuc &&
+ test_cmp ../status_uall.expect ../actual &&
+ get_relevant_traces ../trace.output ../trace.relevant &&
+ cat >../trace.expect <<EOF &&
+ ....path:
+ ....node-creation:0
+ ....gitignore-invalidation:0
+ ....directory-invalidation:0
+ ....opendir:0
+EOF
+ test_cmp ../trace.expect ../trace.relevant
+'
+
+# Bypassing the untracked cache here is not desirable from an
+# end-user perspective, but is expected in the current design.
+# The untracked cache data stored for a -all run cannot be
+# correctly used in a -unormal run - it would yield incorrect
+# output.
+test_expect_success 'if -uall is configured, untracked cache is bypassed with -unormal' '
+ test_config status.showuntrackedfiles all &&
+ : >../trace.output &&
+ GIT_TRACE2_PERF="$TRASH_DIRECTORY/trace.output" \
+ git status -unormal --porcelain >../actual &&
+ iuc status -unormal --porcelain >../status.iuc &&
+ test_cmp ../status.expect ../status.iuc &&
+ test_cmp ../status.expect ../actual &&
+ get_relevant_traces ../trace.output ../trace.relevant &&
+ cat >../trace.expect <<EOF &&
+ ....path:
+EOF
+ test_cmp ../trace.expect ../trace.relevant
+'
+
+test_expect_success 'repopulate untracked cache for -unormal' '
+ git status --porcelain
+'
+
+test_expect_success 'modify in root directory, one dir invalidation' '
+ : >four &&
+ test-tool chmtime =-240 four &&
+ : >../trace.output &&
+ GIT_TRACE2_PERF="$TRASH_DIRECTORY/trace.output" \
+ git status --porcelain >../actual &&
+ iuc status --porcelain >../status.iuc &&
+ cat >../status.expect <<EOF &&
+A done/one
+A one
+A two
+?? dthree/
+?? dtwo/
+?? four
+?? three
+EOF
+ test_cmp ../status.expect ../status.iuc &&
+ test_cmp ../status.expect ../actual &&
+ get_relevant_traces ../trace.output ../trace.relevant &&
+ cat >../trace.expect <<EOF &&
+ ....path:
+ ....node-creation:0
+ ....gitignore-invalidation:0
+ ....directory-invalidation:1
+ ....opendir:1
+EOF
+ test_cmp ../trace.expect ../trace.relevant
+
+'
+
+test_expect_success 'verify untracked cache dump' '
+ test-tool dump-untracked-cache >../actual &&
+ cat >../expect <<EOF &&
+info/exclude $EMPTY_BLOB
+core.excludesfile $ZERO_OID
+exclude_per_dir .gitignore
+flags 00000006
+/ $ZERO_OID recurse valid
+dthree/
+dtwo/
+four
+three
+/done/ $ZERO_OID recurse valid
+/dthree/ $ZERO_OID recurse check_only valid
+three
+/dtwo/ $ZERO_OID recurse check_only valid
+two
+EOF
+ test_cmp ../expect ../actual
+'
+
+test_expect_success 'new .gitignore invalidates recursively' '
+ echo four >.gitignore &&
+ : >../trace.output &&
+ GIT_TRACE2_PERF="$TRASH_DIRECTORY/trace.output" \
+ git status --porcelain >../actual &&
+ iuc status --porcelain >../status.iuc &&
+ cat >../status.expect <<EOF &&
+A done/one
+A one
+A two
+?? .gitignore
+?? dthree/
+?? dtwo/
+?? three
+EOF
+ test_cmp ../status.expect ../status.iuc &&
+ test_cmp ../status.expect ../actual &&
+ get_relevant_traces ../trace.output ../trace.relevant &&
+ cat >../trace.expect <<EOF &&
+ ....path:
+ ....node-creation:0
+ ....gitignore-invalidation:1
+ ....directory-invalidation:1
+ ....opendir:4
+EOF
+ test_cmp ../trace.expect ../trace.relevant
+
+'
+
+test_expect_success 'verify untracked cache dump' '
+ test-tool dump-untracked-cache >../actual &&
+ cat >../expect <<EOF &&
+info/exclude $EMPTY_BLOB
+core.excludesfile $ZERO_OID
+exclude_per_dir .gitignore
+flags 00000006
+/ $(test_oid root) recurse valid
+.gitignore
+dthree/
+dtwo/
+three
+/done/ $ZERO_OID recurse valid
+/dthree/ $ZERO_OID recurse check_only valid
+three
+/dtwo/ $ZERO_OID recurse check_only valid
+two
+EOF
+ test_cmp ../expect ../actual
+'
+
+test_expect_success 'new info/exclude invalidates everything' '
+ echo three >>.git/info/exclude &&
+ : >../trace.output &&
+ GIT_TRACE2_PERF="$TRASH_DIRECTORY/trace.output" \
+ git status --porcelain >../actual &&
+ iuc status --porcelain >../status.iuc &&
+ cat >../status.expect <<EOF &&
+A done/one
+A one
+A two
+?? .gitignore
+?? dtwo/
+EOF
+ test_cmp ../status.expect ../status.iuc &&
+ test_cmp ../status.expect ../actual &&
+ get_relevant_traces ../trace.output ../trace.relevant &&
+ cat >../trace.expect <<EOF &&
+ ....path:
+ ....node-creation:0
+ ....gitignore-invalidation:1
+ ....directory-invalidation:0
+ ....opendir:4
+EOF
+ test_cmp ../trace.expect ../trace.relevant
+'
+
+test_expect_success 'verify untracked cache dump' '
+ test-tool dump-untracked-cache >../actual &&
+ cat >../expect <<EOF &&
+info/exclude $(test_oid exclude)
+core.excludesfile $ZERO_OID
+exclude_per_dir .gitignore
+flags 00000006
+/ $(test_oid root) recurse valid
+.gitignore
+dtwo/
+/done/ $ZERO_OID recurse valid
+/dthree/ $ZERO_OID recurse check_only valid
+/dtwo/ $ZERO_OID recurse check_only valid
+two
+EOF
+ test_cmp ../expect ../actual
+'
+
+test_expect_success 'move two from tracked to untracked' '
+ git rm --cached two &&
+ test-tool dump-untracked-cache >../actual &&
+ cat >../expect <<EOF &&
+info/exclude $(test_oid exclude)
+core.excludesfile $ZERO_OID
+exclude_per_dir .gitignore
+flags 00000006
+/ $(test_oid root) recurse
+/done/ $ZERO_OID recurse valid
+/dthree/ $ZERO_OID recurse check_only valid
+/dtwo/ $ZERO_OID recurse check_only valid
+two
+EOF
+ test_cmp ../expect ../actual
+'
+
+test_expect_success 'status after the move' '
+ : >../trace.output &&
+ GIT_TRACE2_PERF="$TRASH_DIRECTORY/trace.output" \
+ git status --porcelain >../actual &&
+ iuc status --porcelain >../status.iuc &&
+ cat >../status.expect <<EOF &&
+A done/one
+A one
+?? .gitignore
+?? dtwo/
+?? two
+EOF
+ test_cmp ../status.expect ../status.iuc &&
+ test_cmp ../status.expect ../actual &&
+ get_relevant_traces ../trace.output ../trace.relevant &&
+ cat >../trace.expect <<EOF &&
+ ....path:
+ ....node-creation:0
+ ....gitignore-invalidation:0
+ ....directory-invalidation:0
+ ....opendir:1
+EOF
+ test_cmp ../trace.expect ../trace.relevant
+'
+
+test_expect_success 'verify untracked cache dump' '
+ test-tool dump-untracked-cache >../actual &&
+ cat >../expect <<EOF &&
+info/exclude $(test_oid exclude)
+core.excludesfile $ZERO_OID
+exclude_per_dir .gitignore
+flags 00000006
+/ $(test_oid root) recurse valid
+.gitignore
+dtwo/
+two
+/done/ $ZERO_OID recurse valid
+/dthree/ $ZERO_OID recurse check_only valid
+/dtwo/ $ZERO_OID recurse check_only valid
+two
+EOF
+ test_cmp ../expect ../actual
+'
+
+test_expect_success 'move two from untracked to tracked' '
+ git add two &&
+ test-tool dump-untracked-cache >../actual &&
+ cat >../expect <<EOF &&
+info/exclude $(test_oid exclude)
+core.excludesfile $ZERO_OID
+exclude_per_dir .gitignore
+flags 00000006
+/ $(test_oid root) recurse
+/done/ $ZERO_OID recurse valid
+/dthree/ $ZERO_OID recurse check_only valid
+/dtwo/ $ZERO_OID recurse check_only valid
+two
+EOF
+ test_cmp ../expect ../actual
+'
+
+test_expect_success 'status after the move' '
+ : >../trace.output &&
+ GIT_TRACE2_PERF="$TRASH_DIRECTORY/trace.output" \
+ git status --porcelain >../actual &&
+ iuc status --porcelain >../status.iuc &&
+ cat >../status.expect <<EOF &&
+A done/one
+A one
+A two
+?? .gitignore
+?? dtwo/
+EOF
+ test_cmp ../status.expect ../status.iuc &&
+ test_cmp ../status.expect ../actual &&
+ get_relevant_traces ../trace.output ../trace.relevant &&
+ cat >../trace.expect <<EOF &&
+ ....path:
+ ....node-creation:0
+ ....gitignore-invalidation:0
+ ....directory-invalidation:0
+ ....opendir:1
+EOF
+ test_cmp ../trace.expect ../trace.relevant
+'
+
+test_expect_success 'verify untracked cache dump' '
+ test-tool dump-untracked-cache >../actual &&
+ cat >../expect <<EOF &&
+info/exclude $(test_oid exclude)
+core.excludesfile $ZERO_OID
+exclude_per_dir .gitignore
+flags 00000006
+/ $(test_oid root) recurse valid
+.gitignore
+dtwo/
+/done/ $ZERO_OID recurse valid
+/dthree/ $ZERO_OID recurse check_only valid
+/dtwo/ $ZERO_OID recurse check_only valid
+two
+EOF
+ test_cmp ../expect ../actual
+'
+
+test_expect_success 'set up for sparse checkout testing' '
+ echo two >done/.gitignore &&
+ echo three >>done/.gitignore &&
+ echo two >done/two &&
+ git add -f done/two done/.gitignore &&
+ git commit -m "first commit"
+'
+
+test_expect_success 'status after commit' '
+ : >../trace.output &&
+ GIT_TRACE2_PERF="$TRASH_DIRECTORY/trace.output" \
+ git status --porcelain >../actual &&
+ iuc status --porcelain >../status.iuc &&
+ cat >../status.expect <<EOF &&
+?? .gitignore
+?? dtwo/
+EOF
+ test_cmp ../status.expect ../status.iuc &&
+ test_cmp ../status.expect ../actual &&
+ get_relevant_traces ../trace.output ../trace.relevant &&
+ cat >../trace.expect <<EOF &&
+ ....path:
+ ....node-creation:0
+ ....gitignore-invalidation:0
+ ....directory-invalidation:0
+ ....opendir:2
+EOF
+ test_cmp ../trace.expect ../trace.relevant
+'
+
+test_expect_success 'untracked cache correct after commit' '
+ test-tool dump-untracked-cache >../actual &&
+ cat >../expect <<EOF &&
+info/exclude $(test_oid exclude)
+core.excludesfile $ZERO_OID
+exclude_per_dir .gitignore
+flags 00000006
+/ $(test_oid root) recurse valid
+.gitignore
+dtwo/
+/done/ $ZERO_OID recurse valid
+/dthree/ $ZERO_OID recurse check_only valid
+/dtwo/ $ZERO_OID recurse check_only valid
+two
+EOF
+ test_cmp ../expect ../actual
+'
+
+test_expect_success 'set up sparse checkout' '
+ echo "done/[a-z]*" >.git/info/sparse-checkout &&
+ test_config core.sparsecheckout true &&
+ git checkout main &&
+ git update-index --force-untracked-cache &&
+ git status --porcelain >/dev/null && # prime the cache
+ test_path_is_missing done/.gitignore &&
+ test_path_is_file done/one
+'
+
+test_expect_success 'create/modify files, some of which are gitignored' '
+ echo two bis >done/two &&
+ echo three >done/three && # three is gitignored
+ echo four >done/four && # four is gitignored at a higher level
+ echo five >done/five && # five is not gitignored
+ test-tool chmtime =-180 done/two done/three done/four done/five done &&
+ # we need to ensure that the root dir is touched (in the past);
+ test-tool chmtime =-180 . &&
+ sync_mtime
+'
+
+test_expect_success 'test sparse status with untracked cache' '
+ : >../trace.output &&
+ GIT_TRACE2_PERF="$TRASH_DIRECTORY/trace.output" \
+ git status --porcelain >../status.actual &&
+ iuc status --porcelain >../status.iuc &&
+ cat >../status.expect <<EOF &&
+ M done/two
+?? .gitignore
+?? done/five
+?? dtwo/
+EOF
+ test_cmp ../status.expect ../status.iuc &&
+ test_cmp ../status.expect ../status.actual &&
+ get_relevant_traces ../trace.output ../trace.relevant &&
+ cat >../trace.expect <<EOF &&
+ ....path:
+ ....node-creation:0
+ ....gitignore-invalidation:1
+ ....directory-invalidation:2
+ ....opendir:2
+EOF
+ test_cmp ../trace.expect ../trace.relevant
+'
+
+test_expect_success 'untracked cache correct after status' '
+ test-tool dump-untracked-cache >../actual &&
+ cat >../expect <<EOF &&
+info/exclude $(test_oid exclude)
+core.excludesfile $ZERO_OID
+exclude_per_dir .gitignore
+flags 00000006
+/ $(test_oid root) recurse valid
+.gitignore
+dtwo/
+/done/ $(test_oid done) recurse valid
+five
+/dthree/ $ZERO_OID recurse check_only valid
+/dtwo/ $ZERO_OID recurse check_only valid
+two
+EOF
+ test_cmp ../expect ../actual
+'
+
+test_expect_success 'test sparse status again with untracked cache' '
+ : >../trace.output &&
+ GIT_TRACE2_PERF="$TRASH_DIRECTORY/trace.output" \
+ git status --porcelain >../status.actual &&
+ iuc status --porcelain >../status.iuc &&
+ cat >../status.expect <<EOF &&
+ M done/two
+?? .gitignore
+?? done/five
+?? dtwo/
+EOF
+ test_cmp ../status.expect ../status.iuc &&
+ test_cmp ../status.expect ../status.actual &&
+ get_relevant_traces ../trace.output ../trace.relevant &&
+ cat >../trace.expect <<EOF &&
+ ....path:
+ ....node-creation:0
+ ....gitignore-invalidation:0
+ ....directory-invalidation:0
+ ....opendir:0
+EOF
+ test_cmp ../trace.expect ../trace.relevant
+'
+
+test_expect_success 'set up for test of subdir and sparse checkouts' '
+ mkdir done/sub &&
+ mkdir done/sub/sub &&
+ echo "sub" > done/sub/sub/file &&
+ test-tool chmtime =-120 done/sub/sub/file done/sub/sub done/sub done
+'
+
+test_expect_success 'test sparse status with untracked cache and subdir' '
+ : >../trace.output &&
+ GIT_TRACE2_PERF="$TRASH_DIRECTORY/trace.output" \
+ git status --porcelain >../status.actual &&
+ iuc status --porcelain >../status.iuc &&
+ cat >../status.expect <<EOF &&
+ M done/two
+?? .gitignore
+?? done/five
+?? done/sub/
+?? dtwo/
+EOF
+ test_cmp ../status.expect ../status.iuc &&
+ test_cmp ../status.expect ../status.actual &&
+ get_relevant_traces ../trace.output ../trace.relevant &&
+ cat >../trace.expect <<EOF &&
+ ....path:
+ ....node-creation:2
+ ....gitignore-invalidation:0
+ ....directory-invalidation:1
+ ....opendir:3
+EOF
+ test_cmp ../trace.expect ../trace.relevant
+'
+
+test_expect_success 'verify untracked cache dump (sparse/subdirs)' '
+ test-tool dump-untracked-cache >../actual &&
+ cat >../expect-from-test-dump <<EOF &&
+info/exclude $(test_oid exclude)
+core.excludesfile $ZERO_OID
+exclude_per_dir .gitignore
+flags 00000006
+/ $(test_oid root) recurse valid
+.gitignore
+dtwo/
+/done/ $(test_oid done) recurse valid
+five
+sub/
+/done/sub/ $ZERO_OID recurse check_only valid
+sub/
+/done/sub/sub/ $ZERO_OID recurse check_only valid
+file
+/dthree/ $ZERO_OID recurse check_only valid
+/dtwo/ $ZERO_OID recurse check_only valid
+two
+EOF
+ test_cmp ../expect-from-test-dump ../actual
+'
+
+test_expect_success 'test sparse status again with untracked cache and subdir' '
+ : >../trace.output &&
+ GIT_TRACE2_PERF="$TRASH_DIRECTORY/trace.output" \
+ git status --porcelain >../status.actual &&
+ iuc status --porcelain >../status.iuc &&
+ test_cmp ../status.expect ../status.iuc &&
+ test_cmp ../status.expect ../status.actual &&
+ get_relevant_traces ../trace.output ../trace.relevant &&
+ cat >../trace.expect <<EOF &&
+ ....path:
+ ....node-creation:0
+ ....gitignore-invalidation:0
+ ....directory-invalidation:0
+ ....opendir:0
+EOF
+ test_cmp ../trace.expect ../trace.relevant
+'
+
+test_expect_success 'move entry in subdir from untracked to cached' '
+ git add dtwo/two &&
+ git status --porcelain >../status.actual &&
+ iuc status --porcelain >../status.iuc &&
+ cat >../status.expect <<EOF &&
+ M done/two
+A dtwo/two
+?? .gitignore
+?? done/five
+?? done/sub/
+EOF
+ test_cmp ../status.expect ../status.iuc &&
+ test_cmp ../status.expect ../status.actual
+'
+
+test_expect_success 'move entry in subdir from cached to untracked' '
+ git rm --cached dtwo/two &&
+ git status --porcelain >../status.actual &&
+ iuc status --porcelain >../status.iuc &&
+ cat >../status.expect <<EOF &&
+ M done/two
+?? .gitignore
+?? done/five
+?? done/sub/
+?? dtwo/
+EOF
+ test_cmp ../status.expect ../status.iuc &&
+ test_cmp ../status.expect ../status.actual
+'
+
+test_expect_success '--no-untracked-cache removes the cache' '
+ git update-index --no-untracked-cache &&
+ test-tool dump-untracked-cache >../actual &&
+ echo "no untracked cache" >../expect-no-uc &&
+ test_cmp ../expect-no-uc ../actual
+'
+
+test_expect_success 'git status does not change anything' '
+ git status &&
+ test-tool dump-untracked-cache >../actual &&
+ test_cmp ../expect-no-uc ../actual
+'
+
+test_expect_success 'setting core.untrackedCache to true and using git status creates the cache' '
+ git config core.untrackedCache true &&
+ test-tool dump-untracked-cache >../actual &&
+ test_cmp ../expect-no-uc ../actual &&
+ git status &&
+ test-tool dump-untracked-cache >../actual &&
+ test_cmp ../expect-from-test-dump ../actual
+'
+
+test_expect_success 'using --no-untracked-cache does not fail when core.untrackedCache is true' '
+ git update-index --no-untracked-cache &&
+ test-tool dump-untracked-cache >../actual &&
+ test_cmp ../expect-no-uc ../actual &&
+ git update-index --untracked-cache &&
+ test-tool dump-untracked-cache >../actual &&
+ test_cmp ../expect-empty ../actual
+'
+
+test_expect_success 'setting core.untrackedCache to false and using git status removes the cache' '
+ git config core.untrackedCache false &&
+ test-tool dump-untracked-cache >../actual &&
+ test_cmp ../expect-empty ../actual &&
+ git status &&
+ test-tool dump-untracked-cache >../actual &&
+ test_cmp ../expect-no-uc ../actual
+'
+
+test_expect_success 'using --untracked-cache does not fail when core.untrackedCache is false' '
+ git update-index --untracked-cache &&
+ test-tool dump-untracked-cache >../actual &&
+ test_cmp ../expect-empty ../actual
+'
+
+test_expect_success 'setting core.untrackedCache to keep' '
+ git config core.untrackedCache keep &&
+ git update-index --untracked-cache &&
+ test-tool dump-untracked-cache >../actual &&
+ test_cmp ../expect-empty ../actual &&
+ git status &&
+ test-tool dump-untracked-cache >../actual &&
+ test_cmp ../expect-from-test-dump ../actual &&
+ git update-index --no-untracked-cache &&
+ test-tool dump-untracked-cache >../actual &&
+ test_cmp ../expect-no-uc ../actual &&
+ git update-index --force-untracked-cache &&
+ test-tool dump-untracked-cache >../actual &&
+ test_cmp ../expect-empty ../actual &&
+ git status &&
+ test-tool dump-untracked-cache >../actual &&
+ test_cmp ../expect-from-test-dump ../actual
+'
+
+test_expect_success 'test ident field is working' '
+ mkdir ../other_worktree &&
+ cp -R done dthree dtwo four three ../other_worktree &&
+ GIT_WORK_TREE=../other_worktree git status 2>../err &&
+ echo "warning: untracked cache is disabled on this system or location" >../expect &&
+ test_cmp ../expect ../err
+'
+
+test_expect_success 'untracked cache survives a checkout' '
+ git commit --allow-empty -m empty &&
+ test-tool dump-untracked-cache >../before &&
+ test_when_finished "git checkout main" &&
+ git checkout -b other_branch &&
+ test-tool dump-untracked-cache >../after &&
+ test_cmp ../before ../after &&
+ test_commit test &&
+ test-tool dump-untracked-cache >../before &&
+ git checkout main &&
+ test-tool dump-untracked-cache >../after &&
+ test_cmp ../before ../after
+'
+
+test_expect_success 'untracked cache survives a commit' '
+ test-tool dump-untracked-cache >../before &&
+ git add done/two &&
+ git commit -m commit &&
+ test-tool dump-untracked-cache >../after &&
+ test_cmp ../before ../after
+'
+
+test_expect_success 'teardown worktree' '
+ cd ..
+'
+
+test_expect_success SYMLINKS 'setup worktree for symlink test' '
+ git init worktree-symlink &&
+ cd worktree-symlink &&
+ git config core.untrackedCache true &&
+ mkdir one two &&
+ touch one/file two/file &&
+ git add one/file two/file &&
+ git commit -m"first commit" &&
+ git rm -rf one &&
+ ln -s two one &&
+ git add one &&
+ git commit -m"second commit"
+'
+
+test_expect_success SYMLINKS '"status" after symlink replacement should be clean with UC=true' '
+ git checkout HEAD~ &&
+ status_is_clean &&
+ status_is_clean &&
+ git checkout main &&
+ avoid_racy &&
+ status_is_clean &&
+ status_is_clean
+'
+
+test_expect_success SYMLINKS '"status" after symlink replacement should be clean with UC=false' '
+ git config core.untrackedCache false &&
+ git checkout HEAD~ &&
+ status_is_clean &&
+ status_is_clean &&
+ git checkout main &&
+ avoid_racy &&
+ status_is_clean &&
+ status_is_clean
+'
+
+test_expect_success 'setup worktree for non-symlink test' '
+ git init worktree-non-symlink &&
+ cd worktree-non-symlink &&
+ git config core.untrackedCache true &&
+ mkdir one two &&
+ touch one/file two/file &&
+ git add one/file two/file &&
+ git commit -m"first commit" &&
+ git rm -rf one &&
+ cp two/file one &&
+ git add one &&
+ git commit -m"second commit"
+'
+
+test_expect_success '"status" after file replacement should be clean with UC=true' '
+ git checkout HEAD~ &&
+ status_is_clean &&
+ status_is_clean &&
+ git checkout main &&
+ avoid_racy &&
+ status_is_clean &&
+ test-tool dump-untracked-cache >../actual &&
+ grep -F "recurse valid" ../actual >../actual.grep &&
+ cat >../expect.grep <<EOF &&
+/ $ZERO_OID recurse valid
+/two/ $ZERO_OID recurse valid
+EOF
+ status_is_clean &&
+ test_cmp ../expect.grep ../actual.grep
+'
+
+test_expect_success '"status" after file replacement should be clean with UC=false' '
+ git config core.untrackedCache false &&
+ git checkout HEAD~ &&
+ status_is_clean &&
+ status_is_clean &&
+ git checkout main &&
+ avoid_racy &&
+ status_is_clean &&
+ status_is_clean
+'
+
+test_expect_success 'empty repo (no index) and core.untrackedCache' '
+ git init emptyrepo &&
+ git -C emptyrepo -c core.untrackedCache=true write-tree
+'
+
+test_done
diff --git a/t/t7064-wtstatus-pv2.sh b/t/t7064-wtstatus-pv2.sh
new file mode 100755
index 0000000..11884d2
--- /dev/null
+++ b/t/t7064-wtstatus-pv2.sh
@@ -0,0 +1,673 @@
+#!/bin/sh
+
+test_description='git status --porcelain=v2
+
+This test exercises porcelain V2 output for git status.'
+
+. ./test-lib.sh
+
+
+test_expect_success setup '
+ git checkout -f --orphan initial-branch &&
+ test_tick &&
+ git config core.autocrlf false &&
+ echo x >file_x &&
+ echo y >file_y &&
+ echo z >file_z &&
+ mkdir dir1 &&
+ echo a >dir1/file_a &&
+ echo b >dir1/file_b
+'
+
+test_expect_success 'before initial commit, nothing added, only untracked' '
+ cat >expect <<-EOF &&
+ # branch.oid (initial)
+ # branch.head initial-branch
+ ? actual
+ ? dir1/
+ ? expect
+ ? file_x
+ ? file_y
+ ? file_z
+ EOF
+
+ git status --porcelain=v2 --branch --untracked-files=normal >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'before initial commit, things added' '
+ git add file_x file_y file_z dir1 &&
+ OID_A=$(git hash-object -t blob -- dir1/file_a) &&
+ OID_B=$(git hash-object -t blob -- dir1/file_b) &&
+ OID_X=$(git hash-object -t blob -- file_x) &&
+ OID_Y=$(git hash-object -t blob -- file_y) &&
+ OID_Z=$(git hash-object -t blob -- file_z) &&
+
+ cat >expect <<-EOF &&
+ # branch.oid (initial)
+ # branch.head initial-branch
+ 1 A. N... 000000 100644 100644 $ZERO_OID $OID_A dir1/file_a
+ 1 A. N... 000000 100644 100644 $ZERO_OID $OID_B dir1/file_b
+ 1 A. N... 000000 100644 100644 $ZERO_OID $OID_X file_x
+ 1 A. N... 000000 100644 100644 $ZERO_OID $OID_Y file_y
+ 1 A. N... 000000 100644 100644 $ZERO_OID $OID_Z file_z
+ ? actual
+ ? expect
+ EOF
+
+ git status --porcelain=v2 --branch --untracked-files=all >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'before initial commit, things added (-z)' '
+ lf_to_nul >expect <<-EOF &&
+ # branch.oid (initial)
+ # branch.head initial-branch
+ 1 A. N... 000000 100644 100644 $ZERO_OID $OID_A dir1/file_a
+ 1 A. N... 000000 100644 100644 $ZERO_OID $OID_B dir1/file_b
+ 1 A. N... 000000 100644 100644 $ZERO_OID $OID_X file_x
+ 1 A. N... 000000 100644 100644 $ZERO_OID $OID_Y file_y
+ 1 A. N... 000000 100644 100644 $ZERO_OID $OID_Z file_z
+ ? actual
+ ? expect
+ EOF
+
+ git status -z --porcelain=v2 --branch --untracked-files=all >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'make first commit, comfirm HEAD oid and branch' '
+ git commit -m initial &&
+ H0=$(git rev-parse HEAD) &&
+ cat >expect <<-EOF &&
+ # branch.oid $H0
+ # branch.head initial-branch
+ ? actual
+ ? expect
+ EOF
+
+ git status --porcelain=v2 --branch --untracked-files=all >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'after first commit, create unstaged changes' '
+ echo x >>file_x &&
+ OID_X1=$(git hash-object -t blob -- file_x) &&
+ rm file_z &&
+ H0=$(git rev-parse HEAD) &&
+
+ cat >expect <<-EOF &&
+ # branch.oid $H0
+ # branch.head initial-branch
+ 1 .M N... 100644 100644 100644 $OID_X $OID_X file_x
+ 1 .D N... 100644 100644 000000 $OID_Z $OID_Z file_z
+ ? actual
+ ? expect
+ EOF
+
+ git status --porcelain=v2 --branch --untracked-files=all >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'after first commit, stash existing changes' '
+ cat >expect <<-EOF &&
+ # branch.oid $H0
+ # branch.head initial-branch
+ # stash 2
+ EOF
+
+ test_when_finished "git stash pop && git stash pop" &&
+
+ git stash -- file_x &&
+ git stash &&
+ git status --porcelain=v2 --branch --show-stash --untracked-files=no >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'after first commit but omit untracked files and branch' '
+ cat >expect <<-EOF &&
+ 1 .M N... 100644 100644 100644 $OID_X $OID_X file_x
+ 1 .D N... 100644 100644 000000 $OID_Z $OID_Z file_z
+ EOF
+
+ git status --porcelain=v2 --untracked-files=no >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'after first commit, stage existing changes' '
+ git add file_x &&
+ git rm file_z &&
+ H0=$(git rev-parse HEAD) &&
+
+ cat >expect <<-EOF &&
+ # branch.oid $H0
+ # branch.head initial-branch
+ 1 M. N... 100644 100644 100644 $OID_X $OID_X1 file_x
+ 1 D. N... 100644 000000 000000 $OID_Z $ZERO_OID file_z
+ ? actual
+ ? expect
+ EOF
+
+ git status --porcelain=v2 --branch --untracked-files=all >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rename causes 2 path lines' '
+ git mv file_y renamed_y &&
+ H0=$(git rev-parse HEAD) &&
+
+ q_to_tab >expect <<-EOF &&
+ # branch.oid $H0
+ # branch.head initial-branch
+ 1 M. N... 100644 100644 100644 $OID_X $OID_X1 file_x
+ 1 D. N... 100644 000000 000000 $OID_Z $ZERO_OID file_z
+ 2 R. N... 100644 100644 100644 $OID_Y $OID_Y R100 renamed_yQfile_y
+ ? actual
+ ? expect
+ EOF
+
+ git status --porcelain=v2 --branch --untracked-files=all >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rename causes 2 path lines (-z)' '
+ H0=$(git rev-parse HEAD) &&
+
+ ## Lines use NUL path separator and line terminator, so double transform here.
+ q_to_nul <<-EOF | lf_to_nul >expect &&
+ # branch.oid $H0
+ # branch.head initial-branch
+ 1 M. N... 100644 100644 100644 $OID_X $OID_X1 file_x
+ 1 D. N... 100644 000000 000000 $OID_Z $ZERO_OID file_z
+ 2 R. N... 100644 100644 100644 $OID_Y $OID_Y R100 renamed_yQfile_y
+ ? actual
+ ? expect
+ EOF
+
+ git status --porcelain=v2 --branch --untracked-files=all -z >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'make second commit, confirm clean and new HEAD oid' '
+ git commit -m second &&
+ H1=$(git rev-parse HEAD) &&
+
+ cat >expect <<-EOF &&
+ # branch.oid $H1
+ # branch.head initial-branch
+ ? actual
+ ? expect
+ EOF
+
+ git status --porcelain=v2 --branch --untracked-files=all >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'confirm ignored files are not printed' '
+ test_when_finished "rm -f x.ign .gitignore" &&
+ echo x.ign >.gitignore &&
+ echo "ignore me" >x.ign &&
+
+ cat >expect <<-EOF &&
+ ? .gitignore
+ ? actual
+ ? expect
+ EOF
+
+ git status --porcelain=v2 --untracked-files=all >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'ignored files are printed with --ignored' '
+ test_when_finished "rm -f x.ign .gitignore" &&
+ echo x.ign >.gitignore &&
+ echo "ignore me" >x.ign &&
+
+ cat >expect <<-EOF &&
+ ? .gitignore
+ ? actual
+ ? expect
+ ! x.ign
+ EOF
+
+ git status --porcelain=v2 --ignored --untracked-files=all >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'create and commit permanent ignore file' '
+ cat >.gitignore <<-EOF &&
+ actual*
+ expect*
+ EOF
+
+ git add .gitignore &&
+ git commit -m ignore_trash &&
+ H1=$(git rev-parse HEAD) &&
+
+ cat >expect <<-EOF &&
+ # branch.oid $H1
+ # branch.head initial-branch
+ EOF
+
+ git status --porcelain=v2 --branch >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'verify --intent-to-add output' '
+ test_when_finished "git rm -f intent1.add intent2.add" &&
+ touch intent1.add &&
+ echo test >intent2.add &&
+
+ git add --intent-to-add intent1.add intent2.add &&
+
+ cat >expect <<-EOF &&
+ 1 .A N... 000000 000000 100644 $ZERO_OID $ZERO_OID intent1.add
+ 1 .A N... 000000 000000 100644 $ZERO_OID $ZERO_OID intent2.add
+ EOF
+
+ git status --porcelain=v2 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'verify AA (add-add) conflict' '
+ test_when_finished "git reset --hard" &&
+
+ git branch AA_A initial-branch &&
+ git checkout AA_A &&
+ echo "Branch AA_A" >conflict.txt &&
+ OID_AA_A=$(git hash-object -t blob -- conflict.txt) &&
+ git add conflict.txt &&
+ git commit -m "branch aa_a" &&
+
+ git branch AA_B initial-branch &&
+ git checkout AA_B &&
+ echo "Branch AA_B" >conflict.txt &&
+ OID_AA_B=$(git hash-object -t blob -- conflict.txt) &&
+ git add conflict.txt &&
+ git commit -m "branch aa_b" &&
+
+ git branch AA_M AA_B &&
+ git checkout AA_M &&
+ test_must_fail git merge AA_A &&
+
+ HM=$(git rev-parse HEAD) &&
+
+ cat >expect <<-EOF &&
+ # branch.oid $HM
+ # branch.head AA_M
+ u AA N... 000000 100644 100644 100644 $ZERO_OID $OID_AA_B $OID_AA_A conflict.txt
+ EOF
+
+ git status --porcelain=v2 --branch --untracked-files=all >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'verify UU (edit-edit) conflict' '
+ test_when_finished "git reset --hard" &&
+
+ git branch UU_ANC initial-branch &&
+ git checkout UU_ANC &&
+ echo "Ancestor" >conflict.txt &&
+ OID_UU_ANC=$(git hash-object -t blob -- conflict.txt) &&
+ git add conflict.txt &&
+ git commit -m "UU_ANC" &&
+
+ git branch UU_A UU_ANC &&
+ git checkout UU_A &&
+ echo "Branch UU_A" >conflict.txt &&
+ OID_UU_A=$(git hash-object -t blob -- conflict.txt) &&
+ git add conflict.txt &&
+ git commit -m "branch uu_a" &&
+
+ git branch UU_B UU_ANC &&
+ git checkout UU_B &&
+ echo "Branch UU_B" >conflict.txt &&
+ OID_UU_B=$(git hash-object -t blob -- conflict.txt) &&
+ git add conflict.txt &&
+ git commit -m "branch uu_b" &&
+
+ git branch UU_M UU_B &&
+ git checkout UU_M &&
+ test_must_fail git merge UU_A &&
+
+ HM=$(git rev-parse HEAD) &&
+
+ cat >expect <<-EOF &&
+ # branch.oid $HM
+ # branch.head UU_M
+ u UU N... 100644 100644 100644 100644 $OID_UU_ANC $OID_UU_B $OID_UU_A conflict.txt
+ EOF
+
+ git status --porcelain=v2 --branch --untracked-files=all >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'verify upstream fields in branch header' '
+ git checkout initial-branch &&
+ test_when_finished "rm -rf sub_repo" &&
+ git clone . sub_repo &&
+ (
+ ## Confirm local initial-branch tracks remote initial-branch.
+ cd sub_repo &&
+ HUF=$(git rev-parse HEAD) &&
+
+ cat >expect <<-EOF &&
+ # branch.oid $HUF
+ # branch.head initial-branch
+ # branch.upstream origin/initial-branch
+ # branch.ab +0 -0
+ EOF
+
+ git status --porcelain=v2 --branch --untracked-files=all >actual &&
+ test_cmp expect actual &&
+
+ ## Test ahead/behind.
+ echo xyz >file_xyz &&
+ git add file_xyz &&
+ git commit -m xyz &&
+
+ HUF=$(git rev-parse HEAD) &&
+
+ cat >expect <<-EOF &&
+ # branch.oid $HUF
+ # branch.head initial-branch
+ # branch.upstream origin/initial-branch
+ # branch.ab +1 -0
+ EOF
+
+ git status --porcelain=v2 --branch --untracked-files=all >actual &&
+ test_cmp expect actual &&
+
+ ## Repeat the above but without --branch.
+ git status --porcelain=v2 --untracked-files=all >actual &&
+ test_must_be_empty actual &&
+
+ ## Test upstream-gone case. Fake this by pointing
+ ## origin/initial-branch at a non-existing commit.
+ git update-ref -d refs/remotes/origin/initial-branch &&
+
+ HUF=$(git rev-parse HEAD) &&
+
+ cat >expect <<-EOF &&
+ # branch.oid $HUF
+ # branch.head initial-branch
+ # branch.upstream origin/initial-branch
+ EOF
+
+ git status --porcelain=v2 --branch --untracked-files=all >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'verify --[no-]ahead-behind with V2 format' '
+ git checkout initial-branch &&
+ test_when_finished "rm -rf sub_repo" &&
+ git clone . sub_repo &&
+ (
+ ## Confirm local initial-branch tracks remote initial-branch.
+ cd sub_repo &&
+ HUF=$(git rev-parse HEAD) &&
+
+ # Confirm --no-ahead-behind reports traditional branch.ab with 0/0 for equal branches.
+ cat >expect <<-EOF &&
+ # branch.oid $HUF
+ # branch.head initial-branch
+ # branch.upstream origin/initial-branch
+ # branch.ab +0 -0
+ EOF
+
+ git status --no-ahead-behind --porcelain=v2 --branch --untracked-files=all >actual &&
+ test_cmp expect actual &&
+
+ # Confirm --ahead-behind reports traditional branch.ab with 0/0.
+ cat >expect <<-EOF &&
+ # branch.oid $HUF
+ # branch.head initial-branch
+ # branch.upstream origin/initial-branch
+ # branch.ab +0 -0
+ EOF
+
+ git status --ahead-behind --porcelain=v2 --branch --untracked-files=all >actual &&
+ test_cmp expect actual &&
+
+ ## Test non-equal ahead/behind.
+ echo xyz >file_xyz &&
+ git add file_xyz &&
+ git commit -m xyz &&
+
+ HUF=$(git rev-parse HEAD) &&
+
+ # Confirm --no-ahead-behind reports branch.ab with ?/? for non-equal branches.
+ cat >expect <<-EOF &&
+ # branch.oid $HUF
+ # branch.head initial-branch
+ # branch.upstream origin/initial-branch
+ # branch.ab +? -?
+ EOF
+
+ git status --no-ahead-behind --porcelain=v2 --branch --untracked-files=all >actual &&
+ test_cmp expect actual &&
+
+ # Confirm --ahead-behind reports traditional branch.ab with 1/0.
+ cat >expect <<-EOF &&
+ # branch.oid $HUF
+ # branch.head initial-branch
+ # branch.upstream origin/initial-branch
+ # branch.ab +1 -0
+ EOF
+
+ git status --ahead-behind --porcelain=v2 --branch --untracked-files=all >actual &&
+ test_cmp expect actual &&
+
+ # Confirm that "status.aheadbehind" DOES NOT work on V2 format.
+ git -c status.aheadbehind=false status --porcelain=v2 --branch --untracked-files=all >actual &&
+ test_cmp expect actual &&
+
+ # Confirm that "status.aheadbehind" DOES NOT work on V2 format.
+ git -c status.aheadbehind=true status --porcelain=v2 --branch --untracked-files=all >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'create and add submodule, submodule appears clean (A. S...)' '
+ git checkout initial-branch &&
+ git clone . sub_repo &&
+ git clone . super_repo &&
+ test_config_global protocol.file.allow always &&
+ ( cd super_repo &&
+ git submodule add ../sub_repo sub1 &&
+
+ ## Confirm stage/add of clean submodule.
+ HMOD=$(git hash-object -t blob -- .gitmodules) &&
+ HSUP=$(git rev-parse HEAD) &&
+ HSUB=$HSUP &&
+
+ cat >expect <<-EOF &&
+ # branch.oid $HSUP
+ # branch.head initial-branch
+ # branch.upstream origin/initial-branch
+ # branch.ab +0 -0
+ 1 A. N... 000000 100644 100644 $ZERO_OID $HMOD .gitmodules
+ 1 A. S... 000000 160000 160000 $ZERO_OID $HSUB sub1
+ EOF
+
+ git status --porcelain=v2 --branch --untracked-files=all >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'untracked changes in added submodule (AM S..U)' '
+ ( cd super_repo &&
+ ## create untracked file in the submodule.
+ ( cd sub1 &&
+ echo "xxxx" >file_in_sub
+ ) &&
+
+ HMOD=$(git hash-object -t blob -- .gitmodules) &&
+ HSUP=$(git rev-parse HEAD) &&
+ HSUB=$HSUP &&
+
+ cat >expect <<-EOF &&
+ # branch.oid $HSUP
+ # branch.head initial-branch
+ # branch.upstream origin/initial-branch
+ # branch.ab +0 -0
+ 1 A. N... 000000 100644 100644 $ZERO_OID $HMOD .gitmodules
+ 1 AM S..U 000000 160000 160000 $ZERO_OID $HSUB sub1
+ EOF
+
+ git status --porcelain=v2 --branch --untracked-files=all >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'staged changes in added submodule (AM S.M.)' '
+ ( cd super_repo &&
+ ## stage the changes in the submodule.
+ ( cd sub1 &&
+ git add file_in_sub
+ ) &&
+
+ HMOD=$(git hash-object -t blob -- .gitmodules) &&
+ HSUP=$(git rev-parse HEAD) &&
+ HSUB=$HSUP &&
+
+ cat >expect <<-EOF &&
+ # branch.oid $HSUP
+ # branch.head initial-branch
+ # branch.upstream origin/initial-branch
+ # branch.ab +0 -0
+ 1 A. N... 000000 100644 100644 $ZERO_OID $HMOD .gitmodules
+ 1 AM S.M. 000000 160000 160000 $ZERO_OID $HSUB sub1
+ EOF
+
+ git status --porcelain=v2 --branch --untracked-files=all >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'staged and unstaged changes in added (AM S.M.)' '
+ ( cd super_repo &&
+ ( cd sub1 &&
+ ## make additional unstaged changes (on the same file) in the submodule.
+ ## This does not cause us to get S.MU (because the submodule does not report
+ ## a "?" line for the unstaged changes).
+ echo "more changes" >>file_in_sub
+ ) &&
+
+ HMOD=$(git hash-object -t blob -- .gitmodules) &&
+ HSUP=$(git rev-parse HEAD) &&
+ HSUB=$HSUP &&
+
+ cat >expect <<-EOF &&
+ # branch.oid $HSUP
+ # branch.head initial-branch
+ # branch.upstream origin/initial-branch
+ # branch.ab +0 -0
+ 1 A. N... 000000 100644 100644 $ZERO_OID $HMOD .gitmodules
+ 1 AM S.M. 000000 160000 160000 $ZERO_OID $HSUB sub1
+ EOF
+
+ git status --porcelain=v2 --branch --untracked-files=all >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'staged and untracked changes in added submodule (AM S.MU)' '
+ ( cd super_repo &&
+ ( cd sub1 &&
+ ## stage new changes in tracked file.
+ git add file_in_sub &&
+ ## create new untracked file.
+ echo "yyyy" >>another_file_in_sub
+ ) &&
+
+ HMOD=$(git hash-object -t blob -- .gitmodules) &&
+ HSUP=$(git rev-parse HEAD) &&
+ HSUB=$HSUP &&
+
+ cat >expect <<-EOF &&
+ # branch.oid $HSUP
+ # branch.head initial-branch
+ # branch.upstream origin/initial-branch
+ # branch.ab +0 -0
+ 1 A. N... 000000 100644 100644 $ZERO_OID $HMOD .gitmodules
+ 1 AM S.MU 000000 160000 160000 $ZERO_OID $HSUB sub1
+ EOF
+
+ git status --porcelain=v2 --branch --untracked-files=all >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'commit within the submodule appears as new commit in super (AM SC..)' '
+ ( cd super_repo &&
+ ( cd sub1 &&
+ ## Make a new commit in the submodule.
+ git add file_in_sub &&
+ rm -f another_file_in_sub &&
+ git commit -m "new commit"
+ ) &&
+
+ HMOD=$(git hash-object -t blob -- .gitmodules) &&
+ HSUP=$(git rev-parse HEAD) &&
+ HSUB=$HSUP &&
+
+ cat >expect <<-EOF &&
+ # branch.oid $HSUP
+ # branch.head initial-branch
+ # branch.upstream origin/initial-branch
+ # branch.ab +0 -0
+ 1 A. N... 000000 100644 100644 $ZERO_OID $HMOD .gitmodules
+ 1 AM SC.. 000000 160000 160000 $ZERO_OID $HSUB sub1
+ EOF
+
+ git status --porcelain=v2 --branch --untracked-files=all >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'stage submodule in super and commit' '
+ ( cd super_repo &&
+ ## Stage the new submodule commit in the super.
+ git add sub1 &&
+ ## Commit the super so that the sub no longer appears as added.
+ git commit -m "super commit" &&
+
+ HSUP=$(git rev-parse HEAD) &&
+
+ cat >expect <<-EOF &&
+ # branch.oid $HSUP
+ # branch.head initial-branch
+ # branch.upstream origin/initial-branch
+ # branch.ab +1 -0
+ EOF
+
+ git status --porcelain=v2 --branch --untracked-files=all >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'make unstaged changes in existing submodule (.M S.M.)' '
+ ( cd super_repo &&
+ ( cd sub1 &&
+ echo "zzzz" >>file_in_sub
+ ) &&
+
+ HSUP=$(git rev-parse HEAD) &&
+ HSUB=$(cd sub1 && git rev-parse HEAD) &&
+
+ cat >expect <<-EOF &&
+ # branch.oid $HSUP
+ # branch.head initial-branch
+ # branch.upstream origin/initial-branch
+ # branch.ab +1 -0
+ 1 .M S.M. 160000 160000 160000 $HSUB $HSUB sub1
+ EOF
+
+ git status --porcelain=v2 --branch --untracked-files=all >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_done
diff --git a/t/t7101-reset-empty-subdirs.sh b/t/t7101-reset-empty-subdirs.sh
new file mode 100755
index 0000000..89cf98b
--- /dev/null
+++ b/t/t7101-reset-empty-subdirs.sh
@@ -0,0 +1,66 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Shawn Pearce
+#
+
+test_description='git reset should cull empty subdirs'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-diff-data.sh
+
+test_expect_success 'creating initial files' '
+ mkdir path0 &&
+ COPYING_test_data >path0/COPYING &&
+ git add path0/COPYING &&
+ git commit -m add -a
+'
+
+test_expect_success 'creating second files' '
+ mkdir path1 &&
+ mkdir path1/path2 &&
+ COPYING_test_data >path1/path2/COPYING &&
+ COPYING_test_data >path1/COPYING &&
+ COPYING_test_data >COPYING &&
+ COPYING_test_data >path0/COPYING-TOO &&
+ git add path1/path2/COPYING &&
+ git add path1/COPYING &&
+ git add COPYING &&
+ git add path0/COPYING-TOO &&
+ git commit -m change -a
+'
+
+test_expect_success 'resetting tree HEAD^' '
+ git reset --hard HEAD^
+'
+
+test_expect_success 'checking initial files exist after rewind' '
+ test -d path0 &&
+ test -f path0/COPYING
+'
+
+test_expect_success 'checking lack of path1/path2/COPYING' '
+ ! test -f path1/path2/COPYING
+'
+
+test_expect_success 'checking lack of path1/COPYING' '
+ ! test -f path1/COPYING
+'
+
+test_expect_success 'checking lack of COPYING' '
+ ! test -f COPYING
+'
+
+test_expect_success 'checking checking lack of path1/COPYING-TOO' '
+ ! test -f path0/COPYING-TOO
+'
+
+test_expect_success 'checking lack of path1/path2' '
+ ! test -d path1/path2
+'
+
+test_expect_success 'checking lack of path1' '
+ ! test -d path1
+'
+
+test_done
diff --git a/t/t7102-reset.sh b/t/t7102-reset.sh
new file mode 100755
index 0000000..4287863
--- /dev/null
+++ b/t/t7102-reset.sh
@@ -0,0 +1,619 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Carlos Rica
+#
+
+test_description='git reset
+
+Documented tests for git reset'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+commit_msg () {
+ # String "modify 2nd file (changed)" partly in German
+ # (translated with Google Translate),
+ # encoded in UTF-8, used as a commit log message below.
+ msg="modify 2nd file (ge\303\244ndert)\n"
+ if test -n "$1"
+ then
+ printf "$msg" | iconv -f utf-8 -t "$1"
+ else
+ printf "$msg"
+ fi
+}
+
+# Tested non-UTF-8 encoding
+test_encoding="ISO8859-1"
+
+test_expect_success 'creating initial files and commits' '
+ test_tick &&
+ echo "1st file" >first &&
+ git add first &&
+ git commit -m "create 1st file" &&
+
+ echo "2nd file" >second &&
+ git add second &&
+ git commit -m "create 2nd file" &&
+
+ echo "2nd line 1st file" >>first &&
+ git commit -a -m "modify 1st file" &&
+ head5p2=$(git rev-parse --verify HEAD) &&
+ head5p2f=$(git rev-parse --short HEAD:first) &&
+
+ git rm first &&
+ git mv second secondfile &&
+ git commit -a -m "remove 1st and rename 2nd" &&
+ head5p1=$(git rev-parse --verify HEAD) &&
+ head5p1s=$(git rev-parse --short HEAD:secondfile) &&
+
+ echo "1st line 2nd file" >secondfile &&
+ echo "2nd line 2nd file" >>secondfile &&
+ # "git commit -m" would break MinGW, as Windows refuse to pass
+ # $test_encoding encoded parameter to git.
+ commit_msg $test_encoding | git -c "i18n.commitEncoding=$test_encoding" commit -a -F - &&
+ head5=$(git rev-parse --verify HEAD) &&
+ head5s=$(git rev-parse --short HEAD:secondfile) &&
+ head5sl=$(git rev-parse HEAD:secondfile)
+'
+# git log --pretty=oneline # to see those SHA1 involved
+
+check_changes () {
+ test "$(git rev-parse HEAD)" = "$1" &&
+ git diff | test_cmp .diff_expect - &&
+ git diff --cached | test_cmp .cached_expect - &&
+ for FILE in *
+ do
+ echo $FILE':'
+ cat $FILE || return
+ done | test_cmp .cat_expect -
+}
+
+# no negated form for various type of resets
+for opt in soft mixed hard merge keep
+do
+ test_expect_success "no 'git reset --no-$opt'" '
+ test_when_finished "rm -f err" &&
+ test_must_fail git reset --no-$opt 2>err &&
+ grep "error: unknown option .no-$opt." err
+ '
+done
+
+test_expect_success 'reset --hard message' '
+ hex=$(git log -1 --format="%h") &&
+ git reset --hard >.actual &&
+ echo HEAD is now at $hex $(commit_msg) >.expected &&
+ test_cmp .expected .actual
+'
+
+test_expect_success 'reset --hard message (ISO8859-1 logoutputencoding)' '
+ hex=$(git log -1 --format="%h") &&
+ git -c "i18n.logOutputEncoding=$test_encoding" reset --hard >.actual &&
+ echo HEAD is now at $hex $(commit_msg $test_encoding) >.expected &&
+ test_cmp .expected .actual
+'
+
+test_expect_success 'giving a non existing revision should fail' '
+ >.diff_expect &&
+ >.cached_expect &&
+ cat >.cat_expect <<-\EOF &&
+ secondfile:
+ 1st line 2nd file
+ 2nd line 2nd file
+ EOF
+
+ test_must_fail git reset aaaaaa &&
+ test_must_fail git reset --mixed aaaaaa &&
+ test_must_fail git reset --soft aaaaaa &&
+ test_must_fail git reset --hard aaaaaa &&
+ check_changes $head5
+'
+
+test_expect_success 'reset --soft with unmerged index should fail' '
+ touch .git/MERGE_HEAD &&
+ echo "100644 $head5sl 1 un" |
+ git update-index --index-info &&
+ test_must_fail git reset --soft HEAD &&
+ rm .git/MERGE_HEAD &&
+ git rm --cached -- un
+'
+
+test_expect_success 'giving paths with options different than --mixed should fail' '
+ test_must_fail git reset --soft -- first &&
+ test_must_fail git reset --hard -- first &&
+ test_must_fail git reset --soft HEAD^ -- first &&
+ test_must_fail git reset --hard HEAD^ -- first &&
+ check_changes $head5
+'
+
+test_expect_success 'giving unrecognized options should fail' '
+ test_must_fail git reset --other &&
+ test_must_fail git reset -o &&
+ test_must_fail git reset --mixed --other &&
+ test_must_fail git reset --mixed -o &&
+ test_must_fail git reset --soft --other &&
+ test_must_fail git reset --soft -o &&
+ test_must_fail git reset --hard --other &&
+ test_must_fail git reset --hard -o &&
+ check_changes $head5
+'
+
+test_expect_success 'trying to do reset --soft with pending merge should fail' '
+ git branch branch1 &&
+ git branch branch2 &&
+
+ git checkout branch1 &&
+ echo "3rd line in branch1" >>secondfile &&
+ git commit -a -m "change in branch1" &&
+
+ git checkout branch2 &&
+ echo "3rd line in branch2" >>secondfile &&
+ git commit -a -m "change in branch2" &&
+
+ test_must_fail git merge branch1 &&
+ test_must_fail git reset --soft &&
+
+ printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile &&
+ git commit -a -m "the change in branch2" &&
+
+ git checkout main &&
+ git branch -D branch1 branch2 &&
+ check_changes $head5
+'
+
+test_expect_success 'trying to do reset --soft with pending checkout merge should fail' '
+ git branch branch3 &&
+ git branch branch4 &&
+
+ git checkout branch3 &&
+ echo "3rd line in branch3" >>secondfile &&
+ git commit -a -m "line in branch3" &&
+
+ git checkout branch4 &&
+ echo "3rd line in branch4" >>secondfile &&
+
+ git checkout -m branch3 &&
+ test_must_fail git reset --soft &&
+
+ printf "1st line 2nd file\n2nd line 2nd file\n3rd line" >secondfile &&
+ git commit -a -m "the line in branch3" &&
+
+ git checkout main &&
+ git branch -D branch3 branch4 &&
+ check_changes $head5
+'
+
+test_expect_success 'resetting to HEAD with no changes should succeed and do nothing' '
+ git reset --hard &&
+ check_changes $head5 &&
+ git reset --hard HEAD &&
+ check_changes $head5 &&
+ git reset --soft &&
+ check_changes $head5 &&
+ git reset --soft HEAD &&
+ check_changes $head5 &&
+ git reset --mixed &&
+ check_changes $head5 &&
+ git reset --mixed HEAD &&
+ check_changes $head5 &&
+ git reset &&
+ check_changes $head5 &&
+ git reset HEAD &&
+ check_changes $head5
+'
+
+test_expect_success '--soft reset only should show changes in diff --cached' '
+ >.diff_expect &&
+ cat >.cached_expect <<-EOF &&
+ diff --git a/secondfile b/secondfile
+ index $head5p1s..$head5s 100644
+ --- a/secondfile
+ +++ b/secondfile
+ @@ -1 +1,2 @@
+ -2nd file
+ +1st line 2nd file
+ +2nd line 2nd file
+ EOF
+ cat >.cat_expect <<-\EOF &&
+ secondfile:
+ 1st line 2nd file
+ 2nd line 2nd file
+ EOF
+ git reset --soft HEAD^ &&
+ check_changes $head5p1 &&
+ test "$(git rev-parse ORIG_HEAD)" = \
+ $head5
+'
+
+test_expect_success 'changing files and redo the last commit should succeed' '
+ >.diff_expect &&
+ >.cached_expect &&
+ cat >.cat_expect <<-\EOF &&
+ secondfile:
+ 1st line 2nd file
+ 2nd line 2nd file
+ 3rd line 2nd file
+ EOF
+ echo "3rd line 2nd file" >>secondfile &&
+ git commit -a -C ORIG_HEAD &&
+ head4=$(git rev-parse --verify HEAD) &&
+ check_changes $head4 &&
+ test "$(git rev-parse ORIG_HEAD)" = \
+ $head5
+'
+
+test_expect_success '--hard reset should change the files and undo commits permanently' '
+ >.diff_expect &&
+ >.cached_expect &&
+ cat >.cat_expect <<-\EOF &&
+ first:
+ 1st file
+ 2nd line 1st file
+ second:
+ 2nd file
+ EOF
+ git reset --hard HEAD~2 &&
+ check_changes $head5p2 &&
+ test "$(git rev-parse ORIG_HEAD)" = \
+ $head4
+'
+
+test_expect_success 'redoing changes adding them without commit them should succeed' '
+ >.diff_expect &&
+ cat >.cached_expect <<-EOF &&
+ diff --git a/first b/first
+ deleted file mode 100644
+ index $head5p2f..0000000
+ --- a/first
+ +++ /dev/null
+ @@ -1,2 +0,0 @@
+ -1st file
+ -2nd line 1st file
+ diff --git a/second b/second
+ deleted file mode 100644
+ index $head5p1s..0000000
+ --- a/second
+ +++ /dev/null
+ @@ -1 +0,0 @@
+ -2nd file
+ diff --git a/secondfile b/secondfile
+ new file mode 100644
+ index 0000000..$head5s
+ --- /dev/null
+ +++ b/secondfile
+ @@ -0,0 +1,2 @@
+ +1st line 2nd file
+ +2nd line 2nd file
+ EOF
+ cat >.cat_expect <<-\EOF &&
+ secondfile:
+ 1st line 2nd file
+ 2nd line 2nd file
+ EOF
+ git rm first &&
+ git mv second secondfile &&
+
+ echo "1st line 2nd file" >secondfile &&
+ echo "2nd line 2nd file" >>secondfile &&
+ git add secondfile &&
+ check_changes $head5p2
+'
+
+test_expect_success '--mixed reset to HEAD should unadd the files' '
+ cat >.diff_expect <<-EOF &&
+ diff --git a/first b/first
+ deleted file mode 100644
+ index $head5p2f..0000000
+ --- a/first
+ +++ /dev/null
+ @@ -1,2 +0,0 @@
+ -1st file
+ -2nd line 1st file
+ diff --git a/second b/second
+ deleted file mode 100644
+ index $head5p1s..0000000
+ --- a/second
+ +++ /dev/null
+ @@ -1 +0,0 @@
+ -2nd file
+ EOF
+ >.cached_expect &&
+ cat >.cat_expect <<-\EOF &&
+ secondfile:
+ 1st line 2nd file
+ 2nd line 2nd file
+ EOF
+ git reset &&
+ check_changes $head5p2 &&
+ test "$(git rev-parse ORIG_HEAD)" = $head5p2
+'
+
+test_expect_success 'redoing the last two commits should succeed' '
+ >.diff_expect &&
+ >.cached_expect &&
+ cat >.cat_expect <<-\EOF &&
+ secondfile:
+ 1st line 2nd file
+ 2nd line 2nd file
+ EOF
+ git add secondfile &&
+ git reset --hard $head5p2 &&
+ git rm first &&
+ git mv second secondfile &&
+ git commit -a -m "remove 1st and rename 2nd" &&
+
+ echo "1st line 2nd file" >secondfile &&
+ echo "2nd line 2nd file" >>secondfile &&
+ # "git commit -m" would break MinGW, as Windows refuse to pass
+ # $test_encoding encoded parameter to git.
+ commit_msg $test_encoding | git -c "i18n.commitEncoding=$test_encoding" commit -a -F - &&
+ check_changes $head5
+'
+
+test_expect_success '--hard reset to HEAD should clear a failed merge' '
+ >.diff_expect &&
+ >.cached_expect &&
+ cat >.cat_expect <<-\EOF &&
+ secondfile:
+ 1st line 2nd file
+ 2nd line 2nd file
+ 3rd line in branch2
+ EOF
+ git branch branch1 &&
+ git branch branch2 &&
+
+ git checkout branch1 &&
+ echo "3rd line in branch1" >>secondfile &&
+ git commit -a -m "change in branch1" &&
+
+ git checkout branch2 &&
+ echo "3rd line in branch2" >>secondfile &&
+ git commit -a -m "change in branch2" &&
+ head3=$(git rev-parse --verify HEAD) &&
+
+ test_must_fail git pull . branch1 &&
+ git reset --hard &&
+ check_changes $head3
+'
+
+test_expect_success '--hard reset to ORIG_HEAD should clear a fast-forward merge' '
+ >.diff_expect &&
+ >.cached_expect &&
+ cat >.cat_expect <<-\EOF &&
+ secondfile:
+ 1st line 2nd file
+ 2nd line 2nd file
+ EOF
+ git reset --hard HEAD^ &&
+ check_changes $head5 &&
+
+ git pull . branch1 &&
+ git reset --hard ORIG_HEAD &&
+ check_changes $head5 &&
+
+ git checkout main &&
+ git branch -D branch1 branch2 &&
+ check_changes $head5
+'
+
+test_expect_success 'test --mixed <paths>' '
+ echo 1 >file1 &&
+ echo 2 >file2 &&
+ git add file1 file2 &&
+ test_tick &&
+ git commit -m files &&
+ before1=$(git rev-parse --short HEAD:file1) &&
+ before2=$(git rev-parse --short HEAD:file2) &&
+ git rm file2 &&
+ echo 3 >file3 &&
+ echo 4 >file4 &&
+ echo 5 >file1 &&
+ after1=$(git rev-parse --short $(git hash-object file1)) &&
+ after4=$(git rev-parse --short $(git hash-object file4)) &&
+ git add file1 file3 file4 &&
+ git reset HEAD -- file1 file2 file3 &&
+ test_must_fail git diff --quiet &&
+ git diff >output &&
+
+ cat >expect <<-EOF &&
+ diff --git a/file1 b/file1
+ index $before1..$after1 100644
+ --- a/file1
+ +++ b/file1
+ @@ -1 +1 @@
+ -1
+ +5
+ diff --git a/file2 b/file2
+ deleted file mode 100644
+ index $before2..0000000
+ --- a/file2
+ +++ /dev/null
+ @@ -1 +0,0 @@
+ -2
+ EOF
+
+ test_cmp expect output &&
+ git diff --cached >output &&
+
+ cat >cached_expect <<-EOF &&
+ diff --git a/file4 b/file4
+ new file mode 100644
+ index 0000000..$after4
+ --- /dev/null
+ +++ b/file4
+ @@ -0,0 +1 @@
+ +4
+ EOF
+
+ test_cmp cached_expect output
+'
+
+test_expect_success 'test resetting the index at give paths' '
+ mkdir sub &&
+ >sub/file1 &&
+ >sub/file2 &&
+ git update-index --add sub/file1 sub/file2 &&
+ T=$(git write-tree) &&
+ git reset HEAD sub/file2 &&
+ test_must_fail git diff --quiet &&
+ U=$(git write-tree) &&
+ echo "$T" &&
+ echo "$U" &&
+ test_must_fail git diff-index --cached --exit-code "$T" &&
+ test "$T" != "$U"
+'
+
+test_expect_success 'resetting an unmodified path is a no-op' '
+ git reset --hard &&
+ git reset -- file1 &&
+ git diff-files --exit-code &&
+ git diff-index --cached --exit-code HEAD
+'
+
+test_reset_refreshes_index () {
+
+ # To test whether the index is refreshed in `git reset --mixed` with
+ # the given options, create a scenario where we clearly see different
+ # results depending on whether the refresh occurred or not.
+
+ # Step 0: start with a clean index
+ git reset --hard HEAD &&
+
+ # Step 1: remove file2, but only in the index (no change to worktree)
+ git rm --cached file2 &&
+
+ # Step 2: reset index & leave worktree unchanged from HEAD
+ git $1 reset $2 --mixed HEAD &&
+
+ # Step 3: verify whether the index is refreshed by checking whether
+ # file2 still has staged changes in the index differing from HEAD (if
+ # the refresh occurred, there should be no such changes)
+ git diff-files >output.log &&
+ test_must_be_empty output.log
+}
+
+test_expect_success '--mixed refreshes the index' '
+ # Verify default behavior (without --[no-]refresh or reset.refresh)
+ test_reset_refreshes_index &&
+
+ # With --quiet
+ test_reset_refreshes_index "" --quiet
+'
+
+test_expect_success '--mixed --[no-]refresh sets refresh behavior' '
+ # Verify that --[no-]refresh controls index refresh
+ test_reset_refreshes_index "" --refresh &&
+ ! test_reset_refreshes_index "" --no-refresh
+'
+
+test_expect_success '--mixed preserves skip-worktree' '
+ echo 123 >>file2 &&
+ git add file2 &&
+ git update-index --skip-worktree file2 &&
+ git reset --mixed HEAD >output &&
+ test_must_be_empty output &&
+
+ cat >expect <<-\EOF &&
+ Unstaged changes after reset:
+ M file2
+ EOF
+ git update-index --no-skip-worktree file2 &&
+ git add file2 &&
+ git reset --mixed HEAD >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'resetting specific path that is unmerged' '
+ git rm --cached file2 &&
+ F1=$(git rev-parse HEAD:file1) &&
+ F2=$(git rev-parse HEAD:file2) &&
+ F3=$(git rev-parse HEAD:secondfile) &&
+ {
+ echo "100644 $F1 1 file2" &&
+ echo "100644 $F2 2 file2" &&
+ echo "100644 $F3 3 file2"
+ } | git update-index --index-info &&
+ git ls-files -u &&
+ git reset HEAD file2 &&
+ test_must_fail git diff --quiet &&
+ git diff-index --exit-code --cached HEAD
+'
+
+test_expect_success 'disambiguation (1)' '
+ git reset --hard &&
+ >secondfile &&
+ git add secondfile &&
+ git reset secondfile &&
+ test_must_fail git diff --quiet -- secondfile &&
+ test -z "$(git diff --cached --name-only)" &&
+ test -f secondfile &&
+ test_must_be_empty secondfile
+'
+
+test_expect_success 'disambiguation (2)' '
+ git reset --hard &&
+ >secondfile &&
+ git add secondfile &&
+ rm -f secondfile &&
+ test_must_fail git reset secondfile &&
+ test -n "$(git diff --cached --name-only -- secondfile)" &&
+ test ! -f secondfile
+'
+
+test_expect_success 'disambiguation (3)' '
+ git reset --hard &&
+ >secondfile &&
+ git add secondfile &&
+ rm -f secondfile &&
+ git reset HEAD secondfile &&
+ test_must_fail git diff --quiet &&
+ test -z "$(git diff --cached --name-only)" &&
+ test ! -f secondfile
+'
+
+test_expect_success 'disambiguation (4)' '
+ git reset --hard &&
+ >secondfile &&
+ git add secondfile &&
+ rm -f secondfile &&
+ git reset -- secondfile &&
+ test_must_fail git diff --quiet &&
+ test -z "$(git diff --cached --name-only)" &&
+ test ! -f secondfile
+'
+
+test_expect_success 'reset with paths accepts tree' '
+ # for simpler tests, drop last commit containing added files
+ git reset --hard HEAD^ &&
+ git reset HEAD^^{tree} -- . &&
+ git diff --cached HEAD^ --exit-code &&
+ git diff HEAD --exit-code
+'
+
+test_expect_success 'reset -N keeps removed files as intent-to-add' '
+ echo new-file >new-file &&
+ git add new-file &&
+ git reset -N HEAD &&
+
+ tree=$(git write-tree) &&
+ git ls-tree $tree new-file >actual &&
+ test_must_be_empty actual &&
+
+ git diff --name-only >actual &&
+ echo new-file >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'reset --mixed sets up work tree' '
+ git init mixed_worktree &&
+ (
+ cd mixed_worktree &&
+ test_commit dummy
+ ) &&
+ git --git-dir=mixed_worktree/.git --work-tree=mixed_worktree reset >actual &&
+ test_must_be_empty actual
+'
+
+test_done
diff --git a/t/t7103-reset-bare.sh b/t/t7103-reset-bare.sh
new file mode 100755
index 0000000..18bbd99
--- /dev/null
+++ b/t/t7103-reset-bare.sh
@@ -0,0 +1,74 @@
+#!/bin/sh
+
+test_description='git reset in a bare repository'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup non-bare' '
+ echo one >file &&
+ git add file &&
+ git commit -m one &&
+ echo two >file &&
+ git commit -a -m two
+'
+
+test_expect_success '"hard" reset requires a worktree' '
+ (cd .git &&
+ test_must_fail git reset --hard)
+'
+
+test_expect_success '"merge" reset requires a worktree' '
+ (cd .git &&
+ test_must_fail git reset --merge)
+'
+
+test_expect_success '"keep" reset requires a worktree' '
+ (cd .git &&
+ test_must_fail git reset --keep)
+'
+
+test_expect_success '"mixed" reset is ok' '
+ (cd .git && git reset)
+'
+
+test_expect_success '"soft" reset is ok' '
+ (cd .git && git reset --soft)
+'
+
+test_expect_success 'hard reset works with GIT_WORK_TREE' '
+ mkdir worktree &&
+ GIT_WORK_TREE=$PWD/worktree GIT_DIR=$PWD/.git git reset --hard &&
+ test_cmp file worktree/file
+'
+
+test_expect_success 'setup bare' '
+ git clone --bare . bare.git &&
+ cd bare.git
+'
+
+test_expect_success '"hard" reset is not allowed in bare' '
+ test_must_fail git reset --hard HEAD^
+'
+
+test_expect_success '"merge" reset is not allowed in bare' '
+ test_must_fail git reset --merge HEAD^
+'
+
+test_expect_success '"keep" reset is not allowed in bare' '
+ test_must_fail git reset --keep HEAD^
+'
+
+test_expect_success '"mixed" reset is not allowed in bare' '
+ test_must_fail git reset --mixed HEAD^
+'
+
+test_expect_success '"soft" reset is allowed in bare' '
+ git reset --soft HEAD^ &&
+ git show --pretty=format:%s >out &&
+ echo one >expect &&
+ head -n 1 out >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t7104-reset-hard.sh b/t/t7104-reset-hard.sh
new file mode 100755
index 0000000..cf9697e
--- /dev/null
+++ b/t/t7104-reset-hard.sh
@@ -0,0 +1,47 @@
+#!/bin/sh
+
+test_description='reset --hard unmerged'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+
+ mkdir before later &&
+ >before/1 &&
+ >before/2 &&
+ >hello &&
+ >later/3 &&
+ git add before hello later &&
+ git commit -m world &&
+
+ H=$(git rev-parse :hello) &&
+ git rm --cached hello &&
+ echo "100644 $H 2 hello" | git update-index --index-info &&
+
+ rm -f hello &&
+ mkdir -p hello &&
+ >hello/world &&
+ test "$(git ls-files -o)" = hello/world
+
+'
+
+test_expect_success 'reset --hard should restore unmerged ones' '
+
+ git reset --hard &&
+ git ls-files --error-unmatch before/1 before/2 hello later/3 &&
+ test -f hello
+
+'
+
+test_expect_success 'reset --hard did not corrupt index or cache-tree' '
+
+ T=$(git write-tree) &&
+ rm -f .git/index &&
+ git add before hello later &&
+ U=$(git write-tree) &&
+ test "$T" = "$U"
+
+'
+
+test_done
diff --git a/t/t7105-reset-patch.sh b/t/t7105-reset-patch.sh
new file mode 100755
index 0000000..05079c7
--- /dev/null
+++ b/t/t7105-reset-patch.sh
@@ -0,0 +1,94 @@
+#!/bin/sh
+
+test_description='git reset --patch'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./lib-patch-mode.sh
+
+test_expect_success PERL 'setup' '
+ mkdir dir &&
+ echo parent > dir/foo &&
+ echo dummy > bar &&
+ git add dir &&
+ git commit -m initial &&
+ test_tick &&
+ test_commit second dir/foo head &&
+ set_and_save_state bar bar_work bar_index &&
+ save_head
+'
+
+# note: bar sorts before foo, so the first 'n' is always to skip 'bar'
+
+test_expect_success PERL 'saying "n" does nothing' '
+ set_and_save_state dir/foo work work &&
+ test_write_lines n n | git reset -p &&
+ verify_saved_state dir/foo &&
+ verify_saved_state bar
+'
+
+test_expect_success PERL 'git reset -p' '
+ test_write_lines n y | git reset -p >output &&
+ verify_state dir/foo work head &&
+ verify_saved_state bar &&
+ test_grep "Unstage" output
+'
+
+test_expect_success PERL 'git reset -p HEAD^' '
+ test_write_lines n y | git reset -p HEAD^ >output &&
+ verify_state dir/foo work parent &&
+ verify_saved_state bar &&
+ test_grep "Apply" output
+'
+
+test_expect_success PERL 'git reset -p HEAD^^{tree}' '
+ test_write_lines n y | git reset -p HEAD^^{tree} >output &&
+ verify_state dir/foo work parent &&
+ verify_saved_state bar &&
+ test_grep "Apply" output
+'
+
+test_expect_success PERL 'git reset -p HEAD^:dir/foo (blob fails)' '
+ set_and_save_state dir/foo work work &&
+ test_must_fail git reset -p HEAD^:dir/foo &&
+ verify_saved_state dir/foo &&
+ verify_saved_state bar
+'
+
+test_expect_success PERL 'git reset -p aaaaaaaa (unknown fails)' '
+ set_and_save_state dir/foo work work &&
+ test_must_fail git reset -p aaaaaaaa &&
+ verify_saved_state dir/foo &&
+ verify_saved_state bar
+'
+
+# The idea in the rest is that bar sorts first, so we always say 'y'
+# first and if the path limiter fails it'll apply to bar instead of
+# dir/foo. There's always an extra 'n' to reject edits to dir/foo in
+# the failure case (and thus get out of the loop).
+
+test_expect_success PERL 'git reset -p dir' '
+ set_state dir/foo work work &&
+ test_write_lines y n | git reset -p dir &&
+ verify_state dir/foo work head &&
+ verify_saved_state bar
+'
+
+test_expect_success PERL 'git reset -p -- foo (inside dir)' '
+ set_state dir/foo work work &&
+ test_write_lines y n | (cd dir && git reset -p -- foo) &&
+ verify_state dir/foo work head &&
+ verify_saved_state bar
+'
+
+test_expect_success PERL 'git reset -p HEAD^ -- dir' '
+ test_write_lines y n | git reset -p HEAD^ -- dir &&
+ verify_state dir/foo work parent &&
+ verify_saved_state bar
+'
+
+test_expect_success PERL 'none of this moved HEAD' '
+ verify_saved_head
+'
+
+
+test_done
diff --git a/t/t7106-reset-unborn-branch.sh b/t/t7106-reset-unborn-branch.sh
new file mode 100755
index 0000000..d20e570
--- /dev/null
+++ b/t/t7106-reset-unborn-branch.sh
@@ -0,0 +1,69 @@
+#!/bin/sh
+
+test_description='git reset should work on unborn branch'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ echo a >a &&
+ echo b >b
+'
+
+test_expect_success 'reset' '
+ git add a b &&
+ git reset &&
+
+ git ls-files >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'reset HEAD' '
+ rm .git/index &&
+ git add a b &&
+ test_must_fail git reset HEAD
+'
+
+test_expect_success 'reset $file' '
+ rm .git/index &&
+ git add a b &&
+ git reset a &&
+
+ echo b >expect &&
+ git ls-files >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success PERL 'reset -p' '
+ rm .git/index &&
+ git add a &&
+ echo y >yes &&
+ git reset -p <yes >output &&
+
+ git ls-files >actual &&
+ test_must_be_empty actual &&
+ test_grep "Unstage" output
+'
+
+test_expect_success 'reset --soft is a no-op' '
+ rm .git/index &&
+ git add a &&
+ git reset --soft &&
+
+ echo a >expect &&
+ git ls-files >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'reset --hard' '
+ rm .git/index &&
+ git add a &&
+ test_when_finished "echo a >a" &&
+ git reset --hard &&
+
+ git ls-files >actual &&
+ test_must_be_empty actual &&
+ test_path_is_missing a
+'
+
+test_done
diff --git a/t/t7107-reset-pathspec-file.sh b/t/t7107-reset-pathspec-file.sh
new file mode 100755
index 0000000..020db20
--- /dev/null
+++ b/t/t7107-reset-pathspec-file.sh
@@ -0,0 +1,179 @@
+#!/bin/sh
+
+test_description='reset --pathspec-from-file'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_tick
+
+test_expect_success setup '
+ echo A >fileA.t &&
+ echo B >fileB.t &&
+ echo C >fileC.t &&
+ echo D >fileD.t &&
+ git add . &&
+ git commit --include . -m "Commit" &&
+ git tag checkpoint
+'
+
+restore_checkpoint () {
+ git reset --hard checkpoint
+}
+
+verify_expect () {
+ git status --porcelain -- fileA.t fileB.t fileC.t fileD.t >actual &&
+ if test "x$1" = 'x!'
+ then
+ ! test_cmp expect actual
+ else
+ test_cmp expect actual
+ fi
+}
+
+test_expect_success '--pathspec-from-file from stdin' '
+ restore_checkpoint &&
+
+ git rm fileA.t &&
+ echo fileA.t | git reset --pathspec-from-file=- &&
+
+ cat >expect <<-\EOF &&
+ D fileA.t
+ EOF
+ verify_expect
+'
+
+test_expect_success '--pathspec-from-file from file' '
+ restore_checkpoint &&
+
+ git rm fileA.t &&
+ echo fileA.t >list &&
+ git reset --pathspec-from-file=list &&
+
+ cat >expect <<-\EOF &&
+ D fileA.t
+ EOF
+ verify_expect
+'
+
+test_expect_success 'NUL delimiters' '
+ restore_checkpoint &&
+
+ git rm fileA.t fileB.t &&
+ printf "fileA.t\0fileB.t\0" | git reset --pathspec-from-file=- --pathspec-file-nul &&
+
+ cat >expect <<-\EOF &&
+ D fileA.t
+ D fileB.t
+ EOF
+ verify_expect
+'
+
+test_expect_success 'LF delimiters' '
+ restore_checkpoint &&
+
+ git rm fileA.t fileB.t &&
+ printf "fileA.t\nfileB.t\n" | git reset --pathspec-from-file=- &&
+
+ cat >expect <<-\EOF &&
+ D fileA.t
+ D fileB.t
+ EOF
+ verify_expect
+'
+
+test_expect_success 'no trailing delimiter' '
+ restore_checkpoint &&
+
+ git rm fileA.t fileB.t &&
+ printf "fileA.t\nfileB.t" | git reset --pathspec-from-file=- &&
+
+ cat >expect <<-\EOF &&
+ D fileA.t
+ D fileB.t
+ EOF
+ verify_expect
+'
+
+test_expect_success 'CRLF delimiters' '
+ restore_checkpoint &&
+
+ git rm fileA.t fileB.t &&
+ printf "fileA.t\r\nfileB.t\r\n" | git reset --pathspec-from-file=- &&
+
+ cat >expect <<-\EOF &&
+ D fileA.t
+ D fileB.t
+ EOF
+ verify_expect
+'
+
+test_expect_success 'quotes' '
+ restore_checkpoint &&
+
+ cat >list <<-\EOF &&
+ "file\101.t"
+ EOF
+
+ git rm fileA.t &&
+ git reset --pathspec-from-file=list &&
+
+ cat >expect <<-\EOF &&
+ D fileA.t
+ EOF
+ verify_expect
+'
+
+test_expect_success 'quotes not compatible with --pathspec-file-nul' '
+ restore_checkpoint &&
+
+ cat >list <<-\EOF &&
+ "file\101.t"
+ EOF
+
+ # Note: "git reset" has not yet learned to fail on wrong pathspecs
+ git reset --pathspec-from-file=list --pathspec-file-nul &&
+
+ cat >expect <<-\EOF &&
+ D fileA.t
+ EOF
+ verify_expect !
+'
+
+test_expect_success 'only touches what was listed' '
+ restore_checkpoint &&
+
+ git rm fileA.t fileB.t fileC.t fileD.t &&
+ printf "fileB.t\nfileC.t\n" | git reset --pathspec-from-file=- &&
+
+ cat >expect <<-\EOF &&
+ D fileA.t
+ D fileB.t
+ D fileC.t
+ D fileD.t
+ EOF
+ verify_expect
+'
+
+test_expect_success 'error conditions' '
+ restore_checkpoint &&
+ echo fileA.t >list &&
+ git rm fileA.t &&
+
+ test_must_fail git reset --pathspec-from-file=list --patch 2>err &&
+ test_grep -e "options .--pathspec-from-file. and .--patch. cannot be used together" err &&
+
+ test_must_fail git reset --pathspec-from-file=list -- fileA.t 2>err &&
+ test_grep -e ".--pathspec-from-file. and pathspec arguments cannot be used together" err &&
+
+ test_must_fail git reset --pathspec-file-nul 2>err &&
+ test_grep -e "the option .--pathspec-file-nul. requires .--pathspec-from-file." err &&
+
+ test_must_fail git reset --soft --pathspec-from-file=list 2>err &&
+ test_grep -e "fatal: Cannot do soft reset with paths" err &&
+
+ test_must_fail git reset --hard --pathspec-from-file=list 2>err &&
+ test_grep -e "fatal: Cannot do hard reset with paths" err
+'
+
+test_done
diff --git a/t/t7110-reset-merge.sh b/t/t7110-reset-merge.sh
new file mode 100755
index 0000000..7ee180f
--- /dev/null
+++ b/t/t7110-reset-merge.sh
@@ -0,0 +1,296 @@
+#!/bin/sh
+#
+# Copyright (c) 2009 Christian Couder
+#
+
+test_description='Tests for "git reset" with "--merge" and "--keep" options'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ printf "line %d\n" 1 2 3 >file1 &&
+ cat file1 >file2 &&
+ git add file1 file2 &&
+ test_tick &&
+ git commit -m "Initial commit" &&
+ git tag initial &&
+ echo line 4 >>file1 &&
+ cat file1 >file2 &&
+ test_tick &&
+ git commit -m "add line 4 to file1" file1 &&
+ git tag second
+'
+
+# The next test will test the following:
+#
+# working index HEAD target working index HEAD
+# ----------------------------------------------------
+# file1: C C C D --merge D D D
+# file2: C D D D --merge C D D
+test_expect_success 'reset --merge is ok with changes in file it does not touch' '
+ git reset --merge HEAD^ &&
+ ! grep 4 file1 &&
+ grep 4 file2 &&
+ test "$(git rev-parse HEAD)" = "$(git rev-parse initial)" &&
+ test -z "$(git diff --cached)"
+'
+
+test_expect_success 'reset --merge is ok when switching back' '
+ git reset --merge second &&
+ grep 4 file1 &&
+ grep 4 file2 &&
+ test "$(git rev-parse HEAD)" = "$(git rev-parse second)" &&
+ test -z "$(git diff --cached)"
+'
+
+# The next test will test the following:
+#
+# working index HEAD target working index HEAD
+# ----------------------------------------------------
+# file1: C C C D --keep D D D
+# file2: C D D D --keep C D D
+test_expect_success 'reset --keep is ok with changes in file it does not touch' '
+ git reset --hard second &&
+ cat file1 >file2 &&
+ git reset --keep HEAD^ &&
+ ! grep 4 file1 &&
+ grep 4 file2 &&
+ test "$(git rev-parse HEAD)" = "$(git rev-parse initial)" &&
+ test -z "$(git diff --cached)"
+'
+
+test_expect_success 'reset --keep is ok when switching back' '
+ git reset --keep second &&
+ grep 4 file1 &&
+ grep 4 file2 &&
+ test "$(git rev-parse HEAD)" = "$(git rev-parse second)" &&
+ test -z "$(git diff --cached)"
+'
+
+# The next test will test the following:
+#
+# working index HEAD target working index HEAD
+# ----------------------------------------------------
+# file1: B B C D --merge D D D
+# file2: C D D D --merge C D D
+test_expect_success 'reset --merge discards changes added to index (1)' '
+ git reset --hard second &&
+ cat file1 >file2 &&
+ echo "line 5" >> file1 &&
+ git add file1 &&
+ git reset --merge HEAD^ &&
+ ! grep 4 file1 &&
+ ! grep 5 file1 &&
+ grep 4 file2 &&
+ test "$(git rev-parse HEAD)" = "$(git rev-parse initial)" &&
+ test -z "$(git diff --cached)"
+'
+
+test_expect_success 'reset --merge is ok again when switching back (1)' '
+ git reset --hard initial &&
+ echo "line 5" >> file2 &&
+ git add file2 &&
+ git reset --merge second &&
+ ! grep 4 file2 &&
+ ! grep 5 file1 &&
+ grep 4 file1 &&
+ test "$(git rev-parse HEAD)" = "$(git rev-parse second)" &&
+ test -z "$(git diff --cached)"
+'
+
+# The next test will test the following:
+#
+# working index HEAD target working index HEAD
+# ----------------------------------------------------
+# file1: B B C D --keep (disallowed)
+test_expect_success 'reset --keep fails with changes in index in files it touches' '
+ git reset --hard second &&
+ echo "line 5" >> file1 &&
+ git add file1 &&
+ test_must_fail git reset --keep HEAD^
+'
+
+# The next test will test the following:
+#
+# working index HEAD target working index HEAD
+# ----------------------------------------------------
+# file1: C C C D --merge D D D
+# file2: C C D D --merge D D D
+test_expect_success 'reset --merge discards changes added to index (2)' '
+ git reset --hard second &&
+ echo "line 4" >> file2 &&
+ git add file2 &&
+ git reset --merge HEAD^ &&
+ ! grep 4 file2 &&
+ test "$(git rev-parse HEAD)" = "$(git rev-parse initial)" &&
+ test -z "$(git diff)" &&
+ test -z "$(git diff --cached)"
+'
+
+test_expect_success 'reset --merge is ok again when switching back (2)' '
+ git reset --hard initial &&
+ git reset --merge second &&
+ ! grep 4 file2 &&
+ grep 4 file1 &&
+ test "$(git rev-parse HEAD)" = "$(git rev-parse second)" &&
+ test -z "$(git diff --cached)"
+'
+
+# The next test will test the following:
+#
+# working index HEAD target working index HEAD
+# ----------------------------------------------------
+# file1: C C C D --keep D D D
+# file2: C C D D --keep C D D
+test_expect_success 'reset --keep keeps changes it does not touch' '
+ git reset --hard second &&
+ echo "line 4" >> file2 &&
+ git add file2 &&
+ git reset --keep HEAD^ &&
+ grep 4 file2 &&
+ test "$(git rev-parse HEAD)" = "$(git rev-parse initial)" &&
+ test -z "$(git diff --cached)"
+'
+
+test_expect_success 'reset --keep keeps changes when switching back' '
+ git reset --keep second &&
+ grep 4 file2 &&
+ grep 4 file1 &&
+ test "$(git rev-parse HEAD)" = "$(git rev-parse second)" &&
+ test -z "$(git diff --cached)"
+'
+
+# The next test will test the following:
+#
+# working index HEAD target working index HEAD
+# ----------------------------------------------------
+# file1: A B B C --merge (disallowed)
+test_expect_success 'reset --merge fails with changes in file it touches' '
+ git reset --hard second &&
+ echo "line 5" >> file1 &&
+ test_tick &&
+ git commit -m "add line 5" file1 &&
+ sed -e "s/line 1/changed line 1/" <file1 >file3 &&
+ mv file3 file1 &&
+ test_must_fail git reset --merge HEAD^ 2>err.log &&
+ grep file1 err.log | grep "not uptodate"
+'
+
+# The next test will test the following:
+#
+# working index HEAD target working index HEAD
+# ----------------------------------------------------
+# file1: A B B C --keep (disallowed)
+test_expect_success 'reset --keep fails with changes in file it touches' '
+ git reset --hard second &&
+ echo "line 5" >> file1 &&
+ test_tick &&
+ git commit -m "add line 5" file1 &&
+ sed -e "s/line 1/changed line 1/" <file1 >file3 &&
+ mv file3 file1 &&
+ test_must_fail git reset --keep HEAD^ 2>err.log &&
+ grep file1 err.log | grep "not uptodate"
+'
+
+test_expect_success 'setup 3 different branches' '
+ git reset --hard second &&
+ git branch branch1 &&
+ git branch branch2 &&
+ git branch branch3 &&
+ git checkout branch1 &&
+ echo "line 5 in branch1" >> file1 &&
+ test_tick &&
+ git commit -a -m "change in branch1" &&
+ git checkout branch2 &&
+ echo "line 5 in branch2" >> file1 &&
+ test_tick &&
+ git commit -a -m "change in branch2" &&
+ git tag third &&
+ git checkout branch3 &&
+ echo a new file >file3 &&
+ rm -f file1 &&
+ git add file3 &&
+ test_tick &&
+ git commit -a -m "change in branch3"
+'
+
+# The next test will test the following:
+#
+# working index HEAD target working index HEAD
+# ----------------------------------------------------
+# file1: X U B C --merge C C C
+test_expect_success '"reset --merge HEAD^" is ok with pending merge' '
+ git checkout third &&
+ test_must_fail git merge branch1 &&
+ git reset --merge HEAD^ &&
+ test "$(git rev-parse HEAD)" = "$(git rev-parse second)" &&
+ test -z "$(git diff --cached)" &&
+ test -z "$(git diff)"
+'
+
+# The next test will test the following:
+#
+# working index HEAD target working index HEAD
+# ----------------------------------------------------
+# file1: X U B C --keep (disallowed)
+test_expect_success '"reset --keep HEAD^" fails with pending merge' '
+ git reset --hard third &&
+ test_must_fail git merge branch1 &&
+ test_must_fail git reset --keep HEAD^ 2>err.log &&
+ test_grep "middle of a merge" err.log
+'
+
+# The next test will test the following:
+#
+# working index HEAD target working index HEAD
+# ----------------------------------------------------
+# file1: X U B B --merge B B B
+test_expect_success '"reset --merge HEAD" is ok with pending merge' '
+ git reset --hard third &&
+ test_must_fail git merge branch1 &&
+ git reset --merge HEAD &&
+ test "$(git rev-parse HEAD)" = "$(git rev-parse third)" &&
+ test -z "$(git diff --cached)" &&
+ test -z "$(git diff)"
+'
+
+# The next test will test the following:
+#
+# working index HEAD target working index HEAD
+# ----------------------------------------------------
+# file1: X U B B --keep (disallowed)
+test_expect_success '"reset --keep HEAD" fails with pending merge' '
+ git reset --hard third &&
+ test_must_fail git merge branch1 &&
+ test_must_fail git reset --keep HEAD 2>err.log &&
+ test_grep "middle of a merge" err.log
+'
+
+test_expect_success '--merge is ok with added/deleted merge' '
+ git reset --hard third &&
+ rm -f file2 &&
+ test_must_fail git merge branch3 &&
+ ! test -f file2 &&
+ test -f file3 &&
+ git diff --exit-code file3 &&
+ git diff --exit-code branch3 file3 &&
+ git reset --merge HEAD &&
+ ! test -f file3 &&
+ ! test -f file2 &&
+ git diff --exit-code --cached
+'
+
+test_expect_success '--keep fails with added/deleted merge' '
+ git reset --hard third &&
+ rm -f file2 &&
+ test_must_fail git merge branch3 &&
+ ! test -f file2 &&
+ test -f file3 &&
+ git diff --exit-code file3 &&
+ git diff --exit-code branch3 file3 &&
+ test_must_fail git reset --keep HEAD 2>err.log &&
+ test_grep "middle of a merge" err.log
+'
+
+test_done
diff --git a/t/t7111-reset-table.sh b/t/t7111-reset-table.sh
new file mode 100755
index 0000000..01b7c35
--- /dev/null
+++ b/t/t7111-reset-table.sh
@@ -0,0 +1,122 @@
+#!/bin/sh
+#
+# Copyright (c) 2010 Christian Couder
+#
+
+test_description='Tests to check that "reset" options follow a known table'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+
+test_expect_success 'creating initial commits' '
+ test_commit E file1 &&
+ test_commit D file1 &&
+ test_commit C file1
+'
+
+while read W1 I1 H1 T opt W2 I2 H2
+do
+ test_expect_success "check: $W1 $I1 $H1 $T --$opt $W2 $I2 $H2" '
+ git reset --hard C &&
+ if test "$I1" != "$H1"
+ then
+ echo "$I1" >file1 &&
+ git add file1
+ fi &&
+ if test "$W1" != "$I1"
+ then
+ echo "$W1" >file1
+ fi &&
+ if test "$W2" != "XXXXX"
+ then
+ git reset --$opt $T &&
+ test "$(cat file1)" = "$W2" &&
+ git checkout-index -f -- file1 &&
+ test "$(cat file1)" = "$I2" &&
+ git checkout -f HEAD -- file1 &&
+ test "$(cat file1)" = "$H2"
+ else
+ test_must_fail git reset --$opt $T
+ fi
+ '
+done <<\EOF
+A B C D soft A B D
+A B C D mixed A D D
+A B C D hard D D D
+A B C D merge XXXXX
+A B C D keep XXXXX
+A B C C soft A B C
+A B C C mixed A C C
+A B C C hard C C C
+A B C C merge XXXXX
+A B C C keep A C C
+B B C D soft B B D
+B B C D mixed B D D
+B B C D hard D D D
+B B C D merge D D D
+B B C D keep XXXXX
+B B C C soft B B C
+B B C C mixed B C C
+B B C C hard C C C
+B B C C merge C C C
+B B C C keep B C C
+B C C D soft B C D
+B C C D mixed B D D
+B C C D hard D D D
+B C C D merge XXXXX
+B C C D keep XXXXX
+B C C C soft B C C
+B C C C mixed B C C
+B C C C hard C C C
+B C C C merge B C C
+B C C C keep B C C
+EOF
+
+test_expect_success 'setting up branches to test with unmerged entries' '
+ git reset --hard C &&
+ git branch branch1 &&
+ git branch branch2 &&
+ git checkout branch1 &&
+ test_commit B1 file1 &&
+ git checkout branch2 &&
+ test_commit B file1
+'
+
+while read W1 I1 H1 T opt W2 I2 H2
+do
+ test_expect_success "check: $W1 $I1 $H1 $T --$opt $W2 $I2 $H2" '
+ git reset --hard B &&
+ test_must_fail git merge branch1 &&
+ cat file1 >X_file1 &&
+ if test "$W2" != "XXXXX"
+ then
+ git reset --$opt $T &&
+ if test "$W2" = "X"
+ then
+ test_cmp file1 X_file1
+ else
+ test "$(cat file1)" = "$W2"
+ fi &&
+ git checkout-index -f -- file1 &&
+ test "$(cat file1)" = "$I2" &&
+ git checkout -f HEAD -- file1 &&
+ test "$(cat file1)" = "$H2"
+ else
+ test_must_fail git reset --$opt $T
+ fi
+ '
+done <<\EOF
+X U B C soft XXXXX
+X U B C mixed X C C
+X U B C hard C C C
+X U B C merge C C C
+X U B C keep XXXXX
+X U B B soft XXXXX
+X U B B mixed X B B
+X U B B hard B B B
+X U B B merge B B B
+X U B B keep XXXXX
+EOF
+
+test_done
diff --git a/t/t7112-reset-submodule.sh b/t/t7112-reset-submodule.sh
new file mode 100755
index 0000000..a3e2413
--- /dev/null
+++ b/t/t7112-reset-submodule.sh
@@ -0,0 +1,20 @@
+#!/bin/sh
+
+test_description='reset can handle submodules'
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-submodule-update.sh
+
+KNOWN_FAILURE_DIRECTORY_SUBMODULE_CONFLICTS=1
+
+test_submodule_switch_recursing_with_args "reset --keep"
+
+test_submodule_forced_switch_recursing_with_args "reset --hard"
+
+test_submodule_switch "reset --keep"
+
+test_submodule_switch "reset --merge"
+
+test_submodule_forced_switch "reset --hard"
+
+test_done
diff --git a/t/t7113-post-index-change-hook.sh b/t/t7113-post-index-change-hook.sh
new file mode 100755
index 0000000..58e55a7
--- /dev/null
+++ b/t/t7113-post-index-change-hook.sh
@@ -0,0 +1,147 @@
+#!/bin/sh
+
+test_description='post index change hook'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ mkdir -p dir1 &&
+ touch dir1/file1.txt &&
+ echo testing >dir1/file2.txt &&
+ git add . &&
+ git commit -m "initial"
+'
+
+test_expect_success 'test status, add, commit, others trigger hook without flags set' '
+ test_hook post-index-change <<-\EOF &&
+ if test "$1" -eq 1; then
+ echo "Invalid combination of flags passed to hook; updated_workdir is set." >testfailure
+ exit 1
+ fi
+ if test "$2" -eq 1; then
+ echo "Invalid combination of flags passed to hook; updated_skipworktree is set." >testfailure
+ exit 1
+ fi
+ if test -f ".git/index.lock"; then
+ echo ".git/index.lock exists" >testfailure
+ exit 3
+ fi
+ if ! test -f ".git/index"; then
+ echo ".git/index does not exist" >testfailure
+ exit 3
+ fi
+ echo "success" >testsuccess
+ EOF
+ mkdir -p dir2 &&
+ touch dir2/file1.txt &&
+ touch dir2/file2.txt &&
+ : force index to be dirty &&
+ test-tool chmtime +60 dir1/file1.txt &&
+ git status &&
+ test_path_is_file testsuccess && rm -f testsuccess &&
+ test_path_is_missing testfailure &&
+ git add . &&
+ test_path_is_file testsuccess && rm -f testsuccess &&
+ test_path_is_missing testfailure &&
+ git commit -m "second" &&
+ test_path_is_file testsuccess && rm -f testsuccess &&
+ test_path_is_missing testfailure &&
+ git checkout -- dir1/file1.txt &&
+ test_path_is_file testsuccess && rm -f testsuccess &&
+ test_path_is_missing testfailure &&
+ git update-index &&
+ test_path_is_missing testsuccess &&
+ test_path_is_missing testfailure &&
+ git reset --soft &&
+ test_path_is_missing testsuccess &&
+ test_path_is_missing testfailure
+'
+
+test_expect_success 'test checkout and reset trigger the hook' '
+ test_hook post-index-change <<-\EOF &&
+ if test "$1" -eq 1 && test "$2" -eq 1; then
+ echo "Invalid combination of flags passed to hook; updated_workdir and updated_skipworktree are both set." >testfailure
+ exit 1
+ fi
+ if test "$1" -eq 0 && test "$2" -eq 0; then
+ echo "Invalid combination of flags passed to hook; neither updated_workdir or updated_skipworktree are set." >testfailure
+ exit 2
+ fi
+ if test "$1" -eq 1; then
+ if test -f ".git/index.lock"; then
+ echo "updated_workdir set but .git/index.lock exists" >testfailure
+ exit 3
+ fi
+ if ! test -f ".git/index"; then
+ echo "updated_workdir set but .git/index does not exist" >testfailure
+ exit 3
+ fi
+ else
+ echo "update_workdir should be set for checkout" >testfailure
+ exit 4
+ fi
+ echo "success" >testsuccess
+ EOF
+ : force index to be dirty &&
+ test-tool chmtime +60 dir1/file1.txt &&
+ git checkout main &&
+ test_path_is_file testsuccess && rm -f testsuccess &&
+ test_path_is_missing testfailure &&
+ test-tool chmtime +60 dir1/file1.txt &&
+ git checkout HEAD &&
+ test_path_is_file testsuccess && rm -f testsuccess &&
+ test_path_is_missing testfailure &&
+ test-tool chmtime +60 dir1/file1.txt &&
+ git reset --hard &&
+ test_path_is_file testsuccess && rm -f testsuccess &&
+ test_path_is_missing testfailure &&
+ git checkout -B test &&
+ test_path_is_file testsuccess && rm -f testsuccess &&
+ test_path_is_missing testfailure
+'
+
+test_expect_success 'test reset --mixed and update-index triggers the hook' '
+ test_hook post-index-change <<-\EOF &&
+ if test "$1" -eq 1 && test "$2" -eq 1; then
+ echo "Invalid combination of flags passed to hook; updated_workdir and updated_skipworktree are both set." >testfailure
+ exit 1
+ fi
+ if test "$1" -eq 0 && test "$2" -eq 0; then
+ echo "Invalid combination of flags passed to hook; neither updated_workdir or updated_skipworktree are set." >testfailure
+ exit 2
+ fi
+ if test "$2" -eq 1; then
+ if test -f ".git/index.lock"; then
+ echo "updated_skipworktree set but .git/index.lock exists" >testfailure
+ exit 3
+ fi
+ if ! test -f ".git/index"; then
+ echo "updated_skipworktree set but .git/index does not exist" >testfailure
+ exit 3
+ fi
+ else
+ echo "updated_skipworktree should be set for reset --mixed and update-index" >testfailure
+ exit 4
+ fi
+ echo "success" >testsuccess
+ EOF
+ : force index to be dirty &&
+ test-tool chmtime +60 dir1/file1.txt &&
+ git reset --mixed --quiet HEAD~1 &&
+ test_path_is_file testsuccess && rm -f testsuccess &&
+ test_path_is_missing testfailure &&
+ git hash-object -w --stdin <dir1/file2.txt >expect &&
+ git update-index --cacheinfo 100644 "$(cat expect)" dir1/file1.txt &&
+ test_path_is_file testsuccess && rm -f testsuccess &&
+ test_path_is_missing testfailure &&
+ git update-index --skip-worktree dir1/file2.txt &&
+ git update-index --remove dir1/file2.txt &&
+ test_path_is_file testsuccess && rm -f testsuccess &&
+ test_path_is_missing testfailure
+'
+
+test_done
diff --git a/t/t7201-co.sh b/t/t7201-co.sh
new file mode 100755
index 0000000..10cc6c4
--- /dev/null
+++ b/t/t7201-co.sh
@@ -0,0 +1,725 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Junio C Hamano
+#
+
+test_description='git checkout tests.
+
+Creates main, forks renamer and side branches from it.
+Test switching across them.
+
+ ! [main] Initial A one, A two
+ * [renamer] Renamer R one->uno, M two
+ ! [side] Side M one, D two, A three
+ ! [simple] Simple D one, M two
+ ----
+ + [simple] Simple D one, M two
+ + [side] Side M one, D two, A three
+ * [renamer] Renamer R one->uno, M two
+ +*++ [main] Initial A one, A two
+
+'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_tick
+
+fill () {
+ for i
+ do
+ echo "$i"
+ done
+}
+
+
+test_expect_success setup '
+ fill x y z >same &&
+ fill 1 2 3 4 5 6 7 8 >one &&
+ fill a b c d e >two &&
+ git add same one two &&
+ git commit -m "Initial A one, A two" &&
+
+ git checkout -b renamer &&
+ rm -f one &&
+ fill 1 3 4 5 6 7 8 >uno &&
+ git add uno &&
+ fill a b c d e f >two &&
+ git commit -a -m "Renamer R one->uno, M two" &&
+
+ git checkout -b side main &&
+ fill 1 2 3 4 5 6 7 >one &&
+ fill A B C D E >three &&
+ rm -f two &&
+ git update-index --add --remove one two three &&
+ git commit -m "Side M one, D two, A three" &&
+
+ git checkout -b simple main &&
+ rm -f one &&
+ fill a c e >two &&
+ git commit -a -m "Simple D one, M two" &&
+
+ git checkout main
+'
+
+test_expect_success 'checkout from non-existing branch' '
+ git checkout -b delete-me main &&
+ git update-ref -d --no-deref refs/heads/delete-me &&
+ test refs/heads/delete-me = "$(git symbolic-ref HEAD)" &&
+ git checkout main &&
+ test refs/heads/main = "$(git symbolic-ref HEAD)"
+'
+
+test_expect_success 'checkout with dirty tree without -m' '
+ fill 0 1 2 3 4 5 6 7 8 >one &&
+ if git checkout side
+ then
+ echo Not happy
+ false
+ else
+ echo "happy - failed correctly"
+ fi
+'
+
+test_expect_success 'checkout with unrelated dirty tree without -m' '
+ git checkout -f main &&
+ fill 0 1 2 3 4 5 6 7 8 >same &&
+ cp same kept &&
+ git checkout side >messages &&
+ test_cmp same kept &&
+ printf "M\t%s\n" same >messages.expect &&
+ test_cmp messages.expect messages
+'
+
+test_expect_success 'checkout -m with dirty tree' '
+ git checkout -f main &&
+ git clean -f &&
+
+ fill 0 1 2 3 4 5 6 7 8 >one &&
+ git checkout -m side >messages &&
+
+ test "$(git symbolic-ref HEAD)" = "refs/heads/side" &&
+
+ printf "M\t%s\n" one >expect.messages &&
+ test_cmp expect.messages messages &&
+
+ fill "M one" "A three" "D two" >expect.main &&
+ git diff --name-status main >current.main &&
+ test_cmp expect.main current.main &&
+
+ fill "M one" >expect.side &&
+ git diff --name-status side >current.side &&
+ test_cmp expect.side current.side &&
+
+ git diff --cached >current.index &&
+ test_must_be_empty current.index
+'
+
+test_expect_success 'checkout -m with dirty tree, renamed' '
+ git checkout -f main && git clean -f &&
+
+ fill 1 2 3 4 5 7 8 >one &&
+ if git checkout renamer
+ then
+ echo Not happy
+ false
+ else
+ echo "happy - failed correctly"
+ fi &&
+
+ git checkout -m renamer &&
+ fill 1 3 4 5 7 8 >expect &&
+ test_cmp expect uno &&
+ ! test -f one &&
+ git diff --cached >current &&
+ test_must_be_empty current
+'
+
+test_expect_success 'checkout -m with merge conflict' '
+ git checkout -f main && git clean -f &&
+
+ fill 1 T 3 4 5 6 S 8 >one &&
+ if git checkout renamer
+ then
+ echo Not happy
+ false
+ else
+ echo "happy - failed correctly"
+ fi &&
+
+ git checkout -m renamer &&
+
+ git diff main:one :3:uno |
+ sed -e "1,/^@@/d" -e "/^ /d" -e "s/^-/d/" -e "s/^+/a/" >current &&
+ fill d2 aT d7 aS >expect &&
+ test_cmp expect current &&
+ git diff --cached two >current &&
+ test_must_be_empty current
+'
+
+test_expect_success 'format of merge conflict from checkout -m' '
+ git checkout -f main &&
+ git clean -f &&
+
+ fill b d >two &&
+ git checkout -m simple &&
+
+ git ls-files >current &&
+ fill same two two two >expect &&
+ test_cmp expect current &&
+
+ cat <<-EOF >expect &&
+ <<<<<<< simple
+ a
+ c
+ e
+ =======
+ b
+ d
+ >>>>>>> local
+ EOF
+ test_cmp expect two
+'
+
+test_expect_success 'checkout --merge --conflict=diff3 <branch>' '
+ git checkout -f main &&
+ git reset --hard &&
+ git clean -f &&
+
+ fill b d >two &&
+ git checkout --merge --conflict=diff3 simple &&
+
+ cat <<-EOF >expect &&
+ <<<<<<< simple
+ a
+ c
+ e
+ ||||||| main
+ a
+ b
+ c
+ d
+ e
+ =======
+ b
+ d
+ >>>>>>> local
+ EOF
+ test_cmp expect two
+'
+
+test_expect_success 'switch to another branch while carrying a deletion' '
+ git checkout -f main &&
+ git reset --hard &&
+ git clean -f &&
+ git rm two &&
+
+ test_must_fail git checkout simple 2>errs &&
+ test_grep overwritten errs &&
+
+ test_must_fail git read-tree --quiet -m -u HEAD simple 2>errs &&
+ test_must_be_empty errs
+'
+
+test_expect_success 'checkout to detach HEAD (with advice declined)' '
+ git config advice.detachedHead false &&
+ rev=$(git rev-parse --short renamer^) &&
+ git checkout -f renamer &&
+ git clean -f &&
+ git checkout renamer^ 2>messages &&
+ test_grep "HEAD is now at $rev" messages &&
+ test_line_count = 1 messages &&
+ H=$(git rev-parse --verify HEAD) &&
+ M=$(git show-ref -s --verify refs/heads/main) &&
+ test "z$H" = "z$M" &&
+ if git symbolic-ref HEAD >/dev/null 2>&1
+ then
+ echo "OOPS, HEAD is still symbolic???"
+ false
+ else
+ : happy
+ fi
+'
+
+test_expect_success 'checkout to detach HEAD' '
+ git config advice.detachedHead true &&
+ rev=$(git rev-parse --short renamer^) &&
+ git checkout -f renamer &&
+ git clean -f &&
+ git checkout renamer^ 2>messages &&
+ grep "HEAD is now at $rev" messages &&
+ test_line_count -gt 1 messages &&
+ H=$(git rev-parse --verify HEAD) &&
+ M=$(git show-ref -s --verify refs/heads/main) &&
+ test "z$H" = "z$M" &&
+ if git symbolic-ref HEAD >/dev/null 2>&1
+ then
+ echo "OOPS, HEAD is still symbolic???"
+ false
+ else
+ : happy
+ fi
+'
+
+test_expect_success 'checkout to detach HEAD with branchname^' '
+ git checkout -f main &&
+ git clean -f &&
+ git checkout renamer^ &&
+ H=$(git rev-parse --verify HEAD) &&
+ M=$(git show-ref -s --verify refs/heads/main) &&
+ test "z$H" = "z$M" &&
+ if git symbolic-ref HEAD >/dev/null 2>&1
+ then
+ echo "OOPS, HEAD is still symbolic???"
+ false
+ else
+ : happy
+ fi
+'
+
+test_expect_success 'checkout to detach HEAD with :/message' '
+ git checkout -f main &&
+ git clean -f &&
+ git checkout ":/Initial" &&
+ H=$(git rev-parse --verify HEAD) &&
+ M=$(git show-ref -s --verify refs/heads/main) &&
+ test "z$H" = "z$M" &&
+ if git symbolic-ref HEAD >/dev/null 2>&1
+ then
+ echo "OOPS, HEAD is still symbolic???"
+ false
+ else
+ : happy
+ fi
+'
+
+test_expect_success 'checkout to detach HEAD with HEAD^0' '
+ git checkout -f main &&
+ git clean -f &&
+ git checkout HEAD^0 &&
+ H=$(git rev-parse --verify HEAD) &&
+ M=$(git show-ref -s --verify refs/heads/main) &&
+ test "z$H" = "z$M" &&
+ if git symbolic-ref HEAD >/dev/null 2>&1
+ then
+ echo "OOPS, HEAD is still symbolic???"
+ false
+ else
+ : happy
+ fi
+'
+
+test_expect_success 'checkout with ambiguous tag/branch names' '
+ git tag both side &&
+ git branch both main &&
+ git reset --hard &&
+ git checkout main &&
+
+ git checkout both &&
+ H=$(git rev-parse --verify HEAD) &&
+ M=$(git show-ref -s --verify refs/heads/main) &&
+ test "z$H" = "z$M" &&
+ name=$(git symbolic-ref HEAD 2>/dev/null) &&
+ test "z$name" = zrefs/heads/both
+'
+
+test_expect_success 'checkout with ambiguous tag/branch names' '
+ git reset --hard &&
+ git checkout main &&
+
+ git tag frotz side &&
+ git branch frotz main &&
+ git reset --hard &&
+ git checkout main &&
+
+ git checkout tags/frotz &&
+ H=$(git rev-parse --verify HEAD) &&
+ S=$(git show-ref -s --verify refs/heads/side) &&
+ test "z$H" = "z$S" &&
+ if name=$(git symbolic-ref HEAD 2>/dev/null)
+ then
+ echo "Bad -- should have detached"
+ false
+ else
+ : happy
+ fi
+'
+
+test_expect_success 'switch branches while in subdirectory' '
+ git reset --hard &&
+ git checkout main &&
+
+ mkdir subs &&
+ git -C subs checkout side &&
+ ! test -f subs/one &&
+ rm -fr subs
+'
+
+test_expect_success 'checkout specific path while in subdirectory' '
+ git reset --hard &&
+ git checkout side &&
+ mkdir subs &&
+ >subs/bero &&
+ git add subs/bero &&
+ git commit -m "add subs/bero" &&
+
+ git checkout main &&
+ mkdir -p subs &&
+ git -C subs checkout side -- bero &&
+ test -f subs/bero
+'
+
+test_expect_success 'checkout w/--track sets up tracking' '
+ git config branch.autosetupmerge false &&
+ git checkout main &&
+ git checkout --track -b track1 &&
+ test "$(git config branch.track1.remote)" &&
+ test "$(git config branch.track1.merge)"
+'
+
+test_expect_success 'checkout w/autosetupmerge=always sets up tracking' '
+ test_when_finished git config branch.autosetupmerge false &&
+ git config branch.autosetupmerge always &&
+ git checkout main &&
+ git checkout -b track2 &&
+ test "$(git config branch.track2.remote)" &&
+ test "$(git config branch.track2.merge)"
+'
+
+test_expect_success 'checkout w/--track from non-branch HEAD fails' '
+ git checkout main^0 &&
+ test_must_fail git symbolic-ref HEAD &&
+ test_must_fail git checkout --track -b track &&
+ test_must_fail git rev-parse --verify track &&
+ test_must_fail git symbolic-ref HEAD &&
+ test "z$(git rev-parse main^0)" = "z$(git rev-parse HEAD)"
+'
+
+test_expect_success 'checkout w/--track from tag fails' '
+ git checkout main^0 &&
+ test_must_fail git symbolic-ref HEAD &&
+ test_must_fail git checkout --track -b track frotz &&
+ test_must_fail git rev-parse --verify track &&
+ test_must_fail git symbolic-ref HEAD &&
+ test "z$(git rev-parse main^0)" = "z$(git rev-parse HEAD)"
+'
+
+test_expect_success 'detach a symbolic link HEAD' '
+ git checkout main &&
+ git config --bool core.prefersymlinkrefs yes &&
+ git checkout side &&
+ git checkout main &&
+ it=$(git symbolic-ref HEAD) &&
+ test "z$it" = zrefs/heads/main &&
+ here=$(git rev-parse --verify refs/heads/main) &&
+ git checkout side^ &&
+ test "z$(git rev-parse --verify refs/heads/main)" = "z$here"
+'
+
+test_expect_success 'checkout with --track fakes a sensible -b <name>' '
+ git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" &&
+ git update-ref refs/remotes/origin/koala/bear renamer &&
+
+ git checkout --track origin/koala/bear &&
+ test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
+ test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)" &&
+
+ git checkout main && git branch -D koala/bear &&
+
+ git checkout --track refs/remotes/origin/koala/bear &&
+ test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
+ test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)" &&
+
+ git checkout main && git branch -D koala/bear &&
+
+ git checkout --track remotes/origin/koala/bear &&
+ test "refs/heads/koala/bear" = "$(git symbolic-ref HEAD)" &&
+ test "$(git rev-parse HEAD)" = "$(git rev-parse renamer)"
+'
+
+test_expect_success 'checkout with --track, but without -b, fails with too short tracked name' '
+ test_must_fail git checkout --track renamer
+'
+
+setup_conflicting_index () {
+ rm -f .git/index &&
+ O=$(echo original | git hash-object -w --stdin) &&
+ A=$(echo ourside | git hash-object -w --stdin) &&
+ B=$(echo theirside | git hash-object -w --stdin) &&
+ (
+ echo "100644 $A 0 fild" &&
+ echo "100644 $O 1 file" &&
+ echo "100644 $A 2 file" &&
+ echo "100644 $B 3 file" &&
+ echo "100644 $A 0 filf"
+ ) | git update-index --index-info
+}
+
+test_expect_success 'checkout an unmerged path should fail' '
+ setup_conflicting_index &&
+ echo "none of the above" >sample &&
+ cat sample >fild &&
+ cat sample >file &&
+ cat sample >filf &&
+ test_must_fail git checkout fild file filf &&
+ test_cmp sample fild &&
+ test_cmp sample filf &&
+ test_cmp sample file
+'
+
+test_expect_success 'checkout with an unmerged path can be ignored' '
+ setup_conflicting_index &&
+ echo "none of the above" >sample &&
+ echo ourside >expect &&
+ cat sample >fild &&
+ cat sample >file &&
+ cat sample >filf &&
+ git checkout -f fild file filf &&
+ test_cmp expect fild &&
+ test_cmp expect filf &&
+ test_cmp sample file
+'
+
+test_expect_success 'checkout unmerged stage' '
+ setup_conflicting_index &&
+ echo "none of the above" >sample &&
+ echo ourside >expect &&
+ cat sample >fild &&
+ cat sample >file &&
+ cat sample >filf &&
+ git checkout --ours . &&
+ test_cmp expect fild &&
+ test_cmp expect filf &&
+ test_cmp expect file &&
+ git checkout --theirs file &&
+ test ztheirside = "z$(cat file)"
+'
+
+test_expect_success 'checkout path with --merge from tree-ish is a no-no' '
+ setup_conflicting_index &&
+ test_must_fail git checkout -m HEAD -- file
+'
+
+test_expect_success 'checkout with --merge' '
+ setup_conflicting_index &&
+ echo "none of the above" >sample &&
+ echo ourside >expect &&
+ cat sample >fild &&
+ cat sample >file &&
+ cat sample >filf &&
+ git checkout -m -- fild file filf &&
+ (
+ echo "<<<<<<< ours" &&
+ echo ourside &&
+ echo "=======" &&
+ echo theirside &&
+ echo ">>>>>>> theirs"
+ ) >merged &&
+ test_cmp expect fild &&
+ test_cmp expect filf &&
+ test_cmp merged file
+'
+
+test_expect_success 'checkout -m works after (mistaken) resolution' '
+ setup_conflicting_index &&
+ echo "none of the above" >sample &&
+ cat sample >fild &&
+ cat sample >file &&
+ cat sample >filf &&
+ # resolve to something
+ git add file &&
+ git checkout --merge -- fild file filf &&
+ {
+ echo "<<<<<<< ours" &&
+ echo ourside &&
+ echo "=======" &&
+ echo theirside &&
+ echo ">>>>>>> theirs"
+ } >merged &&
+ test_cmp expect fild &&
+ test_cmp expect filf &&
+ test_cmp merged file
+'
+
+test_expect_success 'checkout -m works after (mistaken) resolution to remove' '
+ setup_conflicting_index &&
+ echo "none of the above" >sample &&
+ cat sample >fild &&
+ cat sample >file &&
+ cat sample >filf &&
+ # resolve to remove
+ git rm file &&
+ git checkout --merge -- fild file filf &&
+ {
+ echo "<<<<<<< ours" &&
+ echo ourside &&
+ echo "=======" &&
+ echo theirside &&
+ echo ">>>>>>> theirs"
+ } >merged &&
+ test_cmp expect fild &&
+ test_cmp expect filf &&
+ test_cmp merged file
+'
+
+test_expect_success 'checkout with --merge, in diff3 -m style' '
+ git config merge.conflictstyle diff3 &&
+ setup_conflicting_index &&
+ echo "none of the above" >sample &&
+ echo ourside >expect &&
+ cat sample >fild &&
+ cat sample >file &&
+ cat sample >filf &&
+ git checkout -m -- fild file filf &&
+ (
+ echo "<<<<<<< ours" &&
+ echo ourside &&
+ echo "||||||| base" &&
+ echo original &&
+ echo "=======" &&
+ echo theirside &&
+ echo ">>>>>>> theirs"
+ ) >merged &&
+ test_cmp expect fild &&
+ test_cmp expect filf &&
+ test_cmp merged file
+'
+
+test_expect_success 'checkout --conflict=merge, overriding config' '
+ git config merge.conflictstyle diff3 &&
+ setup_conflicting_index &&
+ echo "none of the above" >sample &&
+ echo ourside >expect &&
+ cat sample >fild &&
+ cat sample >file &&
+ cat sample >filf &&
+ git checkout --conflict=merge -- fild file filf &&
+ (
+ echo "<<<<<<< ours" &&
+ echo ourside &&
+ echo "=======" &&
+ echo theirside &&
+ echo ">>>>>>> theirs"
+ ) >merged &&
+ test_cmp expect fild &&
+ test_cmp expect filf &&
+ test_cmp merged file
+'
+
+test_expect_success 'checkout --conflict=diff3' '
+ test_unconfig merge.conflictstyle &&
+ setup_conflicting_index &&
+ echo "none of the above" >sample &&
+ echo ourside >expect &&
+ cat sample >fild &&
+ cat sample >file &&
+ cat sample >filf &&
+ git checkout --conflict=diff3 -- fild file filf &&
+ (
+ echo "<<<<<<< ours" &&
+ echo ourside &&
+ echo "||||||| base" &&
+ echo original &&
+ echo "=======" &&
+ echo theirside &&
+ echo ">>>>>>> theirs"
+ ) >merged &&
+ test_cmp expect fild &&
+ test_cmp expect filf &&
+ test_cmp merged file
+'
+
+test_expect_success 'failing checkout -b should not break working tree' '
+ git clean -fd && # Remove untracked files in the way
+ git reset --hard main &&
+ git symbolic-ref HEAD refs/heads/main &&
+ test_must_fail git checkout -b renamer side^ &&
+ test $(git symbolic-ref HEAD) = refs/heads/main &&
+ git diff --exit-code &&
+ git diff --cached --exit-code
+'
+
+test_expect_success 'switch out of non-branch' '
+ git reset --hard main &&
+ git checkout main^0 &&
+ echo modified >one &&
+ test_must_fail git checkout renamer 2>error.log &&
+ ! grep "^Previous HEAD" error.log
+'
+
+(
+ echo "#!$SHELL_PATH"
+ cat <<\EOF
+O=$1 A=$2 B=$3
+cat "$A" >.tmp
+exec >"$A"
+echo '<<<<<<< filfre-theirs'
+cat "$B"
+echo '||||||| filfre-common'
+cat "$O"
+echo '======='
+cat ".tmp"
+echo '>>>>>>> filfre-ours'
+rm -f .tmp
+exit 1
+EOF
+) >filfre.sh
+chmod +x filfre.sh
+
+test_expect_success 'custom merge driver with checkout -m' '
+ git reset --hard &&
+
+ git config merge.filfre.driver "./filfre.sh %O %A %B" &&
+ git config merge.filfre.name "Feel-free merge driver" &&
+ git config merge.filfre.recursive binary &&
+ echo "arm merge=filfre" >.gitattributes &&
+
+ git checkout -b left &&
+ echo neutral >arm &&
+ git add arm .gitattributes &&
+ test_tick &&
+ git commit -m neutral &&
+ git branch right &&
+
+ echo left >arm &&
+ test_tick &&
+ git commit -a -m left &&
+ git checkout right &&
+
+ echo right >arm &&
+ test_tick &&
+ git commit -a -m right &&
+
+ test_must_fail git merge left &&
+ (
+ for t in filfre-common left right
+ do
+ grep $t arm || exit 1
+ done
+ ) &&
+
+ mv arm expect &&
+ git checkout -m arm &&
+ test_cmp expect arm
+'
+
+test_expect_success 'tracking info copied with autoSetupMerge=inherit' '
+ git reset --hard main &&
+ # default config does not copy tracking info
+ git checkout -b foo-no-inherit koala/bear &&
+ test_cmp_config "" --default "" branch.foo-no-inherit.remote &&
+ test_cmp_config "" --default "" branch.foo-no-inherit.merge &&
+ # with autoSetupMerge=inherit, we copy tracking info from koala/bear
+ test_config branch.autoSetupMerge inherit &&
+ git checkout -b foo koala/bear &&
+ test_cmp_config origin branch.foo.remote &&
+ test_cmp_config refs/heads/koala/bear branch.foo.merge &&
+ # no tracking info to inherit from main
+ git checkout -b main2 main &&
+ test_cmp_config "" --default "" branch.main2.remote &&
+ test_cmp_config "" --default "" branch.main2.merge
+'
+
+test_done
diff --git a/t/t7300-clean.sh b/t/t7300-clean.sh
new file mode 100755
index 0000000..1a310a4
--- /dev/null
+++ b/t/t7300-clean.sh
@@ -0,0 +1,792 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Michael Spang
+#
+
+test_description='git clean basic tests'
+
+. ./test-lib.sh
+
+git config clean.requireForce no
+
+test_expect_success 'setup' '
+
+ mkdir -p src &&
+ touch src/part1.c Makefile &&
+ echo build >.gitignore &&
+ echo \*.o >>.gitignore &&
+ git add . &&
+ git commit -m setup &&
+ touch src/part2.c README &&
+ git add .
+
+'
+
+test_expect_success 'git clean with skip-worktree .gitignore' '
+ git update-index --skip-worktree .gitignore &&
+ rm .gitignore &&
+ mkdir -p build docs &&
+ touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
+ git clean &&
+ test -f Makefile &&
+ test -f README &&
+ test -f src/part1.c &&
+ test -f src/part2.c &&
+ test ! -f a.out &&
+ test ! -f src/part3.c &&
+ test -f docs/manual.txt &&
+ test -f obj.o &&
+ test -f build/lib.so &&
+ git update-index --no-skip-worktree .gitignore &&
+ git checkout .gitignore
+'
+
+test_expect_success 'git clean' '
+
+ mkdir -p build docs &&
+ touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
+ git clean &&
+ test -f Makefile &&
+ test -f README &&
+ test -f src/part1.c &&
+ test -f src/part2.c &&
+ test ! -f a.out &&
+ test ! -f src/part3.c &&
+ test -f docs/manual.txt &&
+ test -f obj.o &&
+ test -f build/lib.so
+
+'
+
+test_expect_success 'git clean src/' '
+
+ mkdir -p build docs &&
+ touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
+ git clean src/ &&
+ test -f Makefile &&
+ test -f README &&
+ test -f src/part1.c &&
+ test -f src/part2.c &&
+ test -f a.out &&
+ test ! -f src/part3.c &&
+ test -f docs/manual.txt &&
+ test -f obj.o &&
+ test -f build/lib.so
+
+'
+
+test_expect_success 'git clean src/ src/' '
+
+ mkdir -p build docs &&
+ touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
+ git clean src/ src/ &&
+ test -f Makefile &&
+ test -f README &&
+ test -f src/part1.c &&
+ test -f src/part2.c &&
+ test -f a.out &&
+ test ! -f src/part3.c &&
+ test -f docs/manual.txt &&
+ test -f obj.o &&
+ test -f build/lib.so
+
+'
+
+test_expect_success 'git clean with prefix' '
+
+ mkdir -p build docs src/test &&
+ touch a.out src/part3.c docs/manual.txt obj.o build/lib.so src/test/1.c &&
+ (cd src/ && git clean) &&
+ test -f Makefile &&
+ test -f README &&
+ test -f src/part1.c &&
+ test -f src/part2.c &&
+ test -f a.out &&
+ test ! -f src/part3.c &&
+ test -f src/test/1.c &&
+ test -f docs/manual.txt &&
+ test -f obj.o &&
+ test -f build/lib.so
+
+'
+
+test_expect_success 'git clean with relative prefix' '
+
+ mkdir -p build docs &&
+ touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
+ would_clean=$(
+ cd docs &&
+ git clean -n ../src |
+ grep part3 |
+ sed -n -e "s|^Would remove ||p"
+ ) &&
+ test "$would_clean" = ../src/part3.c
+'
+
+test_expect_success 'git clean with absolute path' '
+
+ mkdir -p build docs &&
+ touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
+ would_clean=$(
+ cd docs &&
+ git clean -n "$(pwd)/../src" |
+ grep part3 |
+ sed -n -e "s|^Would remove ||p"
+ ) &&
+ test "$would_clean" = ../src/part3.c
+'
+
+test_expect_success 'git clean with out of work tree relative path' '
+
+ mkdir -p build docs &&
+ touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
+ (
+ cd docs &&
+ test_must_fail git clean -n ../..
+ )
+'
+
+test_expect_success 'git clean with out of work tree absolute path' '
+
+ mkdir -p build docs &&
+ touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
+ dd=$(cd .. && pwd) &&
+ (
+ cd docs &&
+ test_must_fail git clean -n $dd
+ )
+'
+
+test_expect_success 'git clean -d with prefix and path' '
+
+ mkdir -p build docs src/feature &&
+ touch a.out src/part3.c src/feature/file.c docs/manual.txt obj.o build/lib.so &&
+ (cd src/ && git clean -d feature/) &&
+ test -f Makefile &&
+ test -f README &&
+ test -f src/part1.c &&
+ test -f src/part2.c &&
+ test -f a.out &&
+ test -f src/part3.c &&
+ test ! -f src/feature/file.c &&
+ test -f docs/manual.txt &&
+ test -f obj.o &&
+ test -f build/lib.so
+
+'
+
+test_expect_success SYMLINKS 'git clean symbolic link' '
+
+ mkdir -p build docs &&
+ touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
+ ln -s docs/manual.txt src/part4.c &&
+ git clean &&
+ test -f Makefile &&
+ test -f README &&
+ test -f src/part1.c &&
+ test -f src/part2.c &&
+ test ! -f a.out &&
+ test ! -f src/part3.c &&
+ test ! -f src/part4.c &&
+ test -f docs/manual.txt &&
+ test -f obj.o &&
+ test -f build/lib.so
+
+'
+
+test_expect_success 'git clean with wildcard' '
+
+ touch a.clean b.clean other.c &&
+ git clean "*.clean" &&
+ test -f Makefile &&
+ test -f README &&
+ test -f src/part1.c &&
+ test -f src/part2.c &&
+ test ! -f a.clean &&
+ test ! -f b.clean &&
+ test -f other.c
+
+'
+
+test_expect_success 'git clean -n' '
+
+ mkdir -p build docs &&
+ touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
+ git clean -n &&
+ test -f Makefile &&
+ test -f README &&
+ test -f src/part1.c &&
+ test -f src/part2.c &&
+ test -f a.out &&
+ test -f src/part3.c &&
+ test -f docs/manual.txt &&
+ test -f obj.o &&
+ test -f build/lib.so
+
+'
+
+test_expect_success 'git clean -d' '
+
+ mkdir -p build docs &&
+ touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
+ git clean -d &&
+ test -f Makefile &&
+ test -f README &&
+ test -f src/part1.c &&
+ test -f src/part2.c &&
+ test ! -f a.out &&
+ test ! -f src/part3.c &&
+ test ! -d docs &&
+ test -f obj.o &&
+ test -f build/lib.so
+
+'
+
+test_expect_success 'git clean -d src/ examples/' '
+
+ mkdir -p build docs examples &&
+ touch a.out src/part3.c docs/manual.txt obj.o build/lib.so examples/1.c &&
+ git clean -d src/ examples/ &&
+ test -f Makefile &&
+ test -f README &&
+ test -f src/part1.c &&
+ test -f src/part2.c &&
+ test -f a.out &&
+ test ! -f src/part3.c &&
+ test ! -f examples/1.c &&
+ test -f docs/manual.txt &&
+ test -f obj.o &&
+ test -f build/lib.so
+
+'
+
+test_expect_success 'git clean -x' '
+
+ mkdir -p build docs &&
+ touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
+ git clean -x &&
+ test -f Makefile &&
+ test -f README &&
+ test -f src/part1.c &&
+ test -f src/part2.c &&
+ test ! -f a.out &&
+ test ! -f src/part3.c &&
+ test -f docs/manual.txt &&
+ test ! -f obj.o &&
+ test -f build/lib.so
+
+'
+
+test_expect_success 'git clean -d -x' '
+
+ mkdir -p build docs &&
+ touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
+ git clean -d -x &&
+ test -f Makefile &&
+ test -f README &&
+ test -f src/part1.c &&
+ test -f src/part2.c &&
+ test ! -f a.out &&
+ test ! -f src/part3.c &&
+ test ! -d docs &&
+ test ! -f obj.o &&
+ test ! -d build
+
+'
+
+test_expect_success 'git clean -d -x with ignored tracked directory' '
+
+ mkdir -p build docs &&
+ touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
+ git clean -d -x -e src &&
+ test -f Makefile &&
+ test -f README &&
+ test -f src/part1.c &&
+ test -f src/part2.c &&
+ test ! -f a.out &&
+ test -f src/part3.c &&
+ test ! -d docs &&
+ test ! -f obj.o &&
+ test ! -d build
+
+'
+
+test_expect_success 'git clean -X' '
+
+ mkdir -p build docs &&
+ touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
+ git clean -X &&
+ test -f Makefile &&
+ test -f README &&
+ test -f src/part1.c &&
+ test -f src/part2.c &&
+ test -f a.out &&
+ test -f src/part3.c &&
+ test -f docs/manual.txt &&
+ test ! -f obj.o &&
+ test -f build/lib.so
+
+'
+
+test_expect_success 'git clean -d -X' '
+
+ mkdir -p build docs &&
+ touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
+ git clean -d -X &&
+ test -f Makefile &&
+ test -f README &&
+ test -f src/part1.c &&
+ test -f src/part2.c &&
+ test -f a.out &&
+ test -f src/part3.c &&
+ test -f docs/manual.txt &&
+ test ! -f obj.o &&
+ test ! -d build
+
+'
+
+test_expect_success 'git clean -d -X with ignored tracked directory' '
+
+ mkdir -p build docs &&
+ touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
+ git clean -d -X -e src &&
+ test -f Makefile &&
+ test -f README &&
+ test -f src/part1.c &&
+ test -f src/part2.c &&
+ test -f a.out &&
+ test ! -f src/part3.c &&
+ test -f docs/manual.txt &&
+ test ! -f obj.o &&
+ test ! -d build
+
+'
+
+test_expect_success 'clean.requireForce defaults to true' '
+
+ git config --unset clean.requireForce &&
+ test_must_fail git clean
+
+'
+
+test_expect_success 'clean.requireForce' '
+
+ git config clean.requireForce true &&
+ test_must_fail git clean
+
+'
+
+test_expect_success 'clean.requireForce and -n' '
+
+ mkdir -p build docs &&
+ touch a.out src/part3.c docs/manual.txt obj.o build/lib.so &&
+ git clean -n &&
+ test -f Makefile &&
+ test -f README &&
+ test -f src/part1.c &&
+ test -f src/part2.c &&
+ test -f a.out &&
+ test -f src/part3.c &&
+ test -f docs/manual.txt &&
+ test -f obj.o &&
+ test -f build/lib.so
+
+'
+
+test_expect_success 'clean.requireForce and -f' '
+
+ git clean -f &&
+ test -f README &&
+ test -f src/part1.c &&
+ test -f src/part2.c &&
+ test ! -f a.out &&
+ test ! -f src/part3.c &&
+ test -f docs/manual.txt &&
+ test -f obj.o &&
+ test -f build/lib.so
+
+'
+
+test_expect_success 'core.excludesfile' '
+
+ echo excludes >excludes &&
+ echo included >included &&
+ git config core.excludesfile excludes &&
+ output=$(git clean -n excludes included 2>&1) &&
+ expr "$output" : ".*included" >/dev/null &&
+ ! expr "$output" : ".*excludes" >/dev/null
+
+'
+
+test_expect_success SANITY 'removal failure' '
+
+ mkdir foo &&
+ touch foo/bar &&
+ test_when_finished "chmod 755 foo" &&
+ (exec <foo/bar &&
+ chmod 0 foo &&
+ test_must_fail git clean -f -d)
+'
+
+test_expect_success 'nested git work tree' '
+ rm -fr foo bar baz &&
+ mkdir -p foo bar baz/boo &&
+ (
+ cd foo &&
+ git init &&
+ test_commit nested hello.world
+ ) &&
+ (
+ cd bar &&
+ >goodbye.people
+ ) &&
+ (
+ cd baz/boo &&
+ git init &&
+ test_commit deeply.nested deeper.world
+ ) &&
+ git clean -f -d &&
+ test -f foo/.git/index &&
+ test -f foo/hello.world &&
+ test -f baz/boo/.git/index &&
+ test -f baz/boo/deeper.world &&
+ ! test -d bar
+'
+
+test_expect_success 'should clean things that almost look like git but are not' '
+ rm -fr almost_git almost_bare_git almost_submodule &&
+ mkdir -p almost_git/.git/objects &&
+ mkdir -p almost_git/.git/refs &&
+ cat >almost_git/.git/HEAD <<-\EOF &&
+ garbage
+ EOF
+ cp -r almost_git/.git/ almost_bare_git &&
+ mkdir almost_submodule/ &&
+ cat >almost_submodule/.git <<-\EOF &&
+ garbage
+ EOF
+ test_when_finished "rm -rf almost_*" &&
+ git clean -f -d &&
+ test_path_is_missing almost_git &&
+ test_path_is_missing almost_bare_git &&
+ test_path_is_missing almost_submodule
+'
+
+test_expect_success 'should not clean submodules' '
+ rm -fr repo to_clean sub1 sub2 &&
+ mkdir repo to_clean &&
+ (
+ cd repo &&
+ git init &&
+ test_commit msg hello.world
+ ) &&
+ test_config_global protocol.file.allow always &&
+ git submodule add ./repo/.git sub1 &&
+ git commit -m "sub1" &&
+ git branch before_sub2 &&
+ git submodule add ./repo/.git sub2 &&
+ git commit -m "sub2" &&
+ git checkout before_sub2 &&
+ >to_clean/should_clean.this &&
+ git clean -f -d &&
+ test_path_is_file repo/.git/index &&
+ test_path_is_file repo/hello.world &&
+ test_path_is_file sub1/.git &&
+ test_path_is_file sub1/hello.world &&
+ test_path_is_file sub2/.git &&
+ test_path_is_file sub2/hello.world &&
+ test_path_is_missing to_clean
+'
+
+test_expect_success POSIXPERM,SANITY 'should avoid cleaning possible submodules' '
+ rm -fr to_clean possible_sub1 &&
+ mkdir to_clean possible_sub1 &&
+ test_when_finished "rm -rf possible_sub*" &&
+ echo "gitdir: foo" >possible_sub1/.git &&
+ >possible_sub1/hello.world &&
+ chmod 0 possible_sub1/.git &&
+ >to_clean/should_clean.this &&
+ git clean -f -d &&
+ test_path_is_file possible_sub1/.git &&
+ test_path_is_file possible_sub1/hello.world &&
+ test_path_is_missing to_clean
+'
+
+test_expect_success 'nested (empty) git should be kept' '
+ rm -fr empty_repo to_clean &&
+ git init empty_repo &&
+ mkdir to_clean &&
+ >to_clean/should_clean.this &&
+ git clean -f -d &&
+ test_path_is_file empty_repo/.git/HEAD &&
+ test_path_is_missing to_clean
+'
+
+test_expect_success 'nested bare repositories should be cleaned' '
+ rm -fr bare1 bare2 subdir &&
+ git init --bare bare1 &&
+ git clone --local --bare . bare2 &&
+ mkdir subdir &&
+ cp -r bare2 subdir/bare3 &&
+ git clean -f -d &&
+ test_path_is_missing bare1 &&
+ test_path_is_missing bare2 &&
+ test_path_is_missing subdir
+'
+
+test_expect_failure 'nested (empty) bare repositories should be cleaned even when in .git' '
+ rm -fr strange_bare &&
+ mkdir strange_bare &&
+ git init --bare strange_bare/.git &&
+ git clean -f -d &&
+ test_path_is_missing strange_bare
+'
+
+test_expect_failure 'nested (non-empty) bare repositories should be cleaned even when in .git' '
+ rm -fr strange_bare &&
+ mkdir strange_bare &&
+ git clone --local --bare . strange_bare/.git &&
+ git clean -f -d &&
+ test_path_is_missing strange_bare
+'
+
+test_expect_success 'giving path in nested git work tree will NOT remove it' '
+ rm -fr repo &&
+ mkdir repo &&
+ (
+ cd repo &&
+ git init &&
+ mkdir -p bar/baz &&
+ test_commit msg bar/baz/hello.world
+ ) &&
+ git clean -f -d repo/bar/baz &&
+ test_path_is_file repo/.git/HEAD &&
+ test_path_is_dir repo/bar/ &&
+ test_path_is_file repo/bar/baz/hello.world
+'
+
+test_expect_success 'giving path to nested .git will not remove it' '
+ rm -fr repo &&
+ mkdir repo untracked &&
+ (
+ cd repo &&
+ git init &&
+ test_commit msg hello.world
+ ) &&
+ git clean -f -d repo/.git &&
+ test_path_is_file repo/.git/HEAD &&
+ test_path_is_dir repo/.git/refs &&
+ test_path_is_dir repo/.git/objects &&
+ test_path_is_dir untracked/
+'
+
+test_expect_success 'giving path to nested .git/ will NOT remove contents' '
+ rm -fr repo untracked &&
+ mkdir repo untracked &&
+ (
+ cd repo &&
+ git init &&
+ test_commit msg hello.world
+ ) &&
+ git clean -f -d repo/.git/ &&
+ test_path_is_dir repo/.git &&
+ test_path_is_file repo/.git/HEAD &&
+ test_path_is_dir untracked/
+'
+
+test_expect_success 'force removal of nested git work tree' '
+ rm -fr foo bar baz &&
+ mkdir -p foo bar baz/boo &&
+ (
+ cd foo &&
+ git init &&
+ test_commit nested hello.world
+ ) &&
+ (
+ cd bar &&
+ >goodbye.people
+ ) &&
+ (
+ cd baz/boo &&
+ git init &&
+ test_commit deeply.nested deeper.world
+ ) &&
+ git clean -f -f -d &&
+ ! test -d foo &&
+ ! test -d bar &&
+ ! test -d baz
+'
+
+test_expect_success 'git clean -e' '
+ rm -fr repo &&
+ mkdir repo &&
+ (
+ cd repo &&
+ git init &&
+ touch known 1 2 3 &&
+ git add known &&
+ git clean -f -e 1 -e 2 &&
+ test -e 1 &&
+ test -e 2 &&
+ ! (test -e 3) &&
+ test -e known
+ )
+'
+
+test_expect_success SANITY 'git clean -d with an unreadable empty directory' '
+ mkdir foo &&
+ chmod a= foo &&
+ git clean -dfx foo &&
+ ! test -d foo
+'
+
+test_expect_success 'git clean -d respects pathspecs (dir is prefix of pathspec)' '
+ mkdir -p foo &&
+ mkdir -p foobar &&
+ git clean -df foobar &&
+ test_path_is_dir foo &&
+ test_path_is_missing foobar
+'
+
+test_expect_success 'git clean -d respects pathspecs (pathspec is prefix of dir)' '
+ mkdir -p foo &&
+ mkdir -p foobar &&
+ git clean -df foo &&
+ test_path_is_missing foo &&
+ test_path_is_dir foobar
+'
+
+test_expect_success 'git clean -d skips untracked dirs containing ignored files' '
+ echo /foo/bar >.gitignore &&
+ echo ignoreme >>.gitignore &&
+ rm -rf foo &&
+ mkdir -p foo/a/aa/aaa foo/b/bb/bbb &&
+ touch foo/bar foo/baz foo/a/aa/ignoreme foo/b/ignoreme foo/b/bb/1 foo/b/bb/2 &&
+ git clean -df &&
+ test_path_is_dir foo &&
+ test_path_is_file foo/bar &&
+ test_path_is_missing foo/baz &&
+ test_path_is_file foo/a/aa/ignoreme &&
+ test_path_is_missing foo/a/aa/aaa &&
+ test_path_is_file foo/b/ignoreme &&
+ test_path_is_missing foo/b/bb
+'
+
+test_expect_success 'git clean -d skips nested repo containing ignored files' '
+ test_when_finished "rm -rf nested-repo-with-ignored-file" &&
+
+ git init nested-repo-with-ignored-file &&
+ (
+ cd nested-repo-with-ignored-file &&
+ >file &&
+ git add file &&
+ git commit -m Initial &&
+
+ # This file is ignored by a .gitignore rule in the outer repo
+ # added in the previous test.
+ >ignoreme
+ ) &&
+
+ git clean -fd &&
+
+ test_path_is_file nested-repo-with-ignored-file/.git/index &&
+ test_path_is_file nested-repo-with-ignored-file/ignoreme &&
+ test_path_is_file nested-repo-with-ignored-file/file
+'
+
+test_expect_success 'git clean handles being told what to clean' '
+ mkdir -p d1 d2 &&
+ touch d1/ut d2/ut &&
+ git clean -f */ut &&
+ test_path_is_missing d1/ut &&
+ test_path_is_missing d2/ut
+'
+
+test_expect_success 'git clean handles being told what to clean, with -d' '
+ mkdir -p d1 d2 &&
+ touch d1/ut d2/ut &&
+ git clean -ffd */ut &&
+ test_path_is_missing d1/ut &&
+ test_path_is_missing d2/ut
+'
+
+test_expect_success 'git clean works if a glob is passed without -d' '
+ mkdir -p d1 d2 &&
+ touch d1/ut d2/ut &&
+ git clean -f "*ut" &&
+ test_path_is_missing d1/ut &&
+ test_path_is_missing d2/ut
+'
+
+test_expect_success 'git clean works if a glob is passed with -d' '
+ mkdir -p d1 d2 &&
+ touch d1/ut d2/ut &&
+ git clean -ffd "*ut" &&
+ test_path_is_missing d1/ut &&
+ test_path_is_missing d2/ut
+'
+
+test_expect_success MINGW 'handle clean & core.longpaths = false nicely' '
+ test_config core.longpaths false &&
+ a50=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&
+ mkdir -p $a50$a50/$a50$a50/$a50$a50 &&
+ : >"$a50$a50/test.txt" 2>"$a50$a50/$a50$a50/$a50$a50/test.txt" &&
+ # create a temporary outside the working tree to hide from "git clean"
+ test_must_fail git clean -xdf 2>.git/err &&
+ # grepping for a strerror string is unportable but it is OK here with
+ # MINGW prereq
+ test_grep "too long" .git/err
+'
+
+test_expect_success 'clean untracked paths by pathspec' '
+ git init untracked &&
+ mkdir untracked/dir &&
+ echo >untracked/dir/file.txt &&
+ git -C untracked clean -f dir/file.txt &&
+ ls untracked/dir >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'avoid traversing into ignored directories' '
+ test_when_finished rm -f output error trace.* &&
+ test_create_repo avoid-traversing-deep-hierarchy &&
+ (
+ cd avoid-traversing-deep-hierarchy &&
+
+ mkdir -p untracked/subdir/with/a &&
+ >untracked/subdir/with/a/random-file.txt &&
+
+ GIT_TRACE2_PERF="$TRASH_DIRECTORY/trace.output" \
+ git clean -ffdxn -e untracked
+ ) &&
+
+ # Make sure we only visited into the top-level directory, and did
+ # not traverse into the "untracked" subdirectory since it was excluded
+ grep data.*read_directo.*directories-visited trace.output |
+ cut -d "|" -f 9 >trace.relevant &&
+ cat >trace.expect <<-EOF &&
+ ..directories-visited:1
+ EOF
+ test_cmp trace.expect trace.relevant
+'
+
+test_expect_success 'traverse into directories that may have ignored entries' '
+ test_when_finished rm -f output &&
+ test_create_repo need-to-traverse-into-hierarchy &&
+ (
+ cd need-to-traverse-into-hierarchy &&
+ mkdir -p modules/foobar/src/generated &&
+ > modules/foobar/src/generated/code.c &&
+ > modules/foobar/Makefile &&
+ echo "/modules/**/src/generated/" >.gitignore &&
+
+ git clean -fX modules/foobar >../output &&
+
+ grep Removing ../output &&
+
+ test_path_is_missing modules/foobar/src/generated/code.c &&
+ test_path_is_file modules/foobar/Makefile
+ )
+'
+
+test_done
diff --git a/t/t7301-clean-interactive.sh b/t/t7301-clean-interactive.sh
new file mode 100755
index 0000000..d82a321
--- /dev/null
+++ b/t/t7301-clean-interactive.sh
@@ -0,0 +1,486 @@
+#!/bin/sh
+
+test_description='git clean -i basic tests'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-terminal.sh
+
+test_expect_success 'setup' '
+
+ mkdir -p src &&
+ touch src/part1.c Makefile &&
+ echo build >.gitignore &&
+ echo \*.o >>.gitignore &&
+ git add . &&
+ git commit -m setup &&
+ touch src/part2.c README &&
+ git add .
+
+'
+
+test_expect_success 'git clean -i (c: clean hotkey)' '
+
+ mkdir -p build docs &&
+ touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
+ docs/manual.txt obj.o build/lib.so &&
+ echo c | git clean -i &&
+ test -f Makefile &&
+ test -f README &&
+ test -f src/part1.c &&
+ test -f src/part2.c &&
+ test ! -f a.out &&
+ test -f docs/manual.txt &&
+ test ! -f src/part3.c &&
+ test ! -f src/part3.h &&
+ test ! -f src/part4.c &&
+ test ! -f src/part4.h &&
+ test -f obj.o &&
+ test -f build/lib.so
+
+'
+
+test_expect_success 'git clean -i (cl: clean prefix)' '
+
+ mkdir -p build docs &&
+ touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
+ docs/manual.txt obj.o build/lib.so &&
+ echo cl | git clean -i &&
+ test -f Makefile &&
+ test -f README &&
+ test -f src/part1.c &&
+ test -f src/part2.c &&
+ test ! -f a.out &&
+ test -f docs/manual.txt &&
+ test ! -f src/part3.c &&
+ test ! -f src/part3.h &&
+ test ! -f src/part4.c &&
+ test ! -f src/part4.h &&
+ test -f obj.o &&
+ test -f build/lib.so
+
+'
+
+test_expect_success 'git clean -i (quit)' '
+
+ mkdir -p build docs &&
+ touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
+ docs/manual.txt obj.o build/lib.so &&
+ echo quit | git clean -i &&
+ test -f Makefile &&
+ test -f README &&
+ test -f src/part1.c &&
+ test -f src/part2.c &&
+ test -f a.out &&
+ test -f docs/manual.txt &&
+ test -f src/part3.c &&
+ test -f src/part3.h &&
+ test -f src/part4.c &&
+ test -f src/part4.h &&
+ test -f obj.o &&
+ test -f build/lib.so
+
+'
+
+test_expect_success 'git clean -i (Ctrl+D)' '
+
+ mkdir -p build docs &&
+ touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
+ docs/manual.txt obj.o build/lib.so &&
+ echo "\04" | git clean -i &&
+ test -f Makefile &&
+ test -f README &&
+ test -f src/part1.c &&
+ test -f src/part2.c &&
+ test -f a.out &&
+ test -f docs/manual.txt &&
+ test -f src/part3.c &&
+ test -f src/part3.h &&
+ test -f src/part4.c &&
+ test -f src/part4.h &&
+ test -f obj.o &&
+ test -f build/lib.so
+
+'
+
+test_expect_success 'git clean -id (filter all)' '
+
+ mkdir -p build docs &&
+ touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
+ docs/manual.txt obj.o build/lib.so &&
+ test_write_lines f "*" "" c |
+ git clean -id &&
+ test -f Makefile &&
+ test -f README &&
+ test -f src/part1.c &&
+ test -f src/part2.c &&
+ test -f a.out &&
+ test -f docs/manual.txt &&
+ test -f src/part3.c &&
+ test -f src/part3.h &&
+ test -f src/part4.c &&
+ test -f src/part4.h &&
+ test -f obj.o &&
+ test -f build/lib.so
+
+'
+
+test_expect_success 'git clean -id (filter patterns)' '
+
+ mkdir -p build docs &&
+ touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
+ docs/manual.txt obj.o build/lib.so &&
+ test_write_lines f "part3.* *.out" "" c |
+ git clean -id &&
+ test -f Makefile &&
+ test -f README &&
+ test -f src/part1.c &&
+ test -f src/part2.c &&
+ test -f a.out &&
+ test ! -f docs/manual.txt &&
+ test -f src/part3.c &&
+ test -f src/part3.h &&
+ test ! -f src/part4.c &&
+ test ! -f src/part4.h &&
+ test -f obj.o &&
+ test -f build/lib.so
+
+'
+
+test_expect_success 'git clean -id (filter patterns 2)' '
+
+ mkdir -p build docs &&
+ touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
+ docs/manual.txt obj.o build/lib.so &&
+ test_write_lines f "* !*.out" "" c |
+ git clean -id &&
+ test -f Makefile &&
+ test -f README &&
+ test -f src/part1.c &&
+ test -f src/part2.c &&
+ test ! -f a.out &&
+ test -f docs/manual.txt &&
+ test -f src/part3.c &&
+ test -f src/part3.h &&
+ test -f src/part4.c &&
+ test -f src/part4.h &&
+ test -f obj.o &&
+ test -f build/lib.so
+
+'
+
+test_expect_success 'git clean -id (select - all)' '
+
+ mkdir -p build docs &&
+ touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
+ docs/manual.txt obj.o build/lib.so &&
+ test_write_lines s "*" "" c |
+ git clean -id &&
+ test -f Makefile &&
+ test -f README &&
+ test -f src/part1.c &&
+ test -f src/part2.c &&
+ test ! -f a.out &&
+ test ! -f docs/manual.txt &&
+ test ! -f src/part3.c &&
+ test ! -f src/part3.h &&
+ test ! -f src/part4.c &&
+ test ! -f src/part4.h &&
+ test -f obj.o &&
+ test -f build/lib.so
+
+'
+
+test_expect_success 'git clean -id (select - none)' '
+
+ mkdir -p build docs &&
+ touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
+ docs/manual.txt obj.o build/lib.so &&
+ test_write_lines s "" c |
+ git clean -id &&
+ test -f Makefile &&
+ test -f README &&
+ test -f src/part1.c &&
+ test -f src/part2.c &&
+ test -f a.out &&
+ test -f docs/manual.txt &&
+ test -f src/part3.c &&
+ test -f src/part3.h &&
+ test -f src/part4.c &&
+ test -f src/part4.h &&
+ test -f obj.o &&
+ test -f build/lib.so
+
+'
+
+test_expect_success 'git clean -id (select - number)' '
+
+ mkdir -p build docs &&
+ touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
+ docs/manual.txt obj.o build/lib.so &&
+ test_write_lines s 3 "" c |
+ git clean -id &&
+ test -f Makefile &&
+ test -f README &&
+ test -f src/part1.c &&
+ test -f src/part2.c &&
+ test -f a.out &&
+ test -f docs/manual.txt &&
+ test ! -f src/part3.c &&
+ test -f src/part3.h &&
+ test -f src/part4.c &&
+ test -f src/part4.h &&
+ test -f obj.o &&
+ test -f build/lib.so
+
+'
+
+test_expect_success 'git clean -id (select - number 2)' '
+
+ mkdir -p build docs &&
+ touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
+ docs/manual.txt obj.o build/lib.so &&
+ test_write_lines s "2 3" 5 "" c |
+ git clean -id &&
+ test -f Makefile &&
+ test -f README &&
+ test -f src/part1.c &&
+ test -f src/part2.c &&
+ test -f a.out &&
+ test ! -f docs/manual.txt &&
+ test ! -f src/part3.c &&
+ test -f src/part3.h &&
+ test ! -f src/part4.c &&
+ test -f src/part4.h &&
+ test -f obj.o &&
+ test -f build/lib.so
+
+'
+
+test_expect_success 'git clean -id (select - number 3)' '
+
+ mkdir -p build docs &&
+ touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
+ docs/manual.txt obj.o build/lib.so &&
+ test_write_lines s "3,4 5" "" c |
+ git clean -id &&
+ test -f Makefile &&
+ test -f README &&
+ test -f src/part1.c &&
+ test -f src/part2.c &&
+ test -f a.out &&
+ test -f docs/manual.txt &&
+ test ! -f src/part3.c &&
+ test ! -f src/part3.h &&
+ test ! -f src/part4.c &&
+ test -f src/part4.h &&
+ test -f obj.o &&
+ test -f build/lib.so
+
+'
+
+test_expect_success 'git clean -id (select - filenames)' '
+
+ mkdir -p build docs &&
+ touch a.out foo.txt bar.txt baz.txt &&
+ test_write_lines s "a.out fo ba bar" "" c |
+ git clean -id &&
+ test -f Makefile &&
+ test ! -f a.out &&
+ test ! -f foo.txt &&
+ test ! -f bar.txt &&
+ test -f baz.txt &&
+ rm baz.txt
+
+'
+
+test_expect_success 'git clean -id (select - range)' '
+
+ mkdir -p build docs &&
+ touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
+ docs/manual.txt obj.o build/lib.so &&
+ test_write_lines s "1,3-4" 2 "" c |
+ git clean -id &&
+ test -f Makefile &&
+ test -f README &&
+ test -f src/part1.c &&
+ test -f src/part2.c &&
+ test ! -f a.out &&
+ test ! -f src/part3.c &&
+ test ! -f src/part3.h &&
+ test -f src/part4.c &&
+ test -f src/part4.h &&
+ test ! -f docs/manual.txt &&
+ test -f obj.o &&
+ test -f build/lib.so
+
+'
+
+test_expect_success 'git clean -id (select - range 2)' '
+
+ mkdir -p build docs &&
+ touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
+ docs/manual.txt obj.o build/lib.so &&
+ test_write_lines s "4- 1" "" c |
+ git clean -id &&
+ test -f Makefile &&
+ test -f README &&
+ test -f src/part1.c &&
+ test -f src/part2.c &&
+ test ! -f a.out &&
+ test -f docs/manual.txt &&
+ test -f src/part3.c &&
+ test ! -f src/part3.h &&
+ test ! -f src/part4.c &&
+ test ! -f src/part4.h &&
+ test -f obj.o &&
+ test -f build/lib.so
+
+'
+
+test_expect_success 'git clean -id (inverse select)' '
+
+ mkdir -p build docs &&
+ touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
+ docs/manual.txt obj.o build/lib.so &&
+ test_write_lines s "*" "-5- 1 -2" "" c |
+ git clean -id &&
+ test -f Makefile &&
+ test -f README &&
+ test -f src/part1.c &&
+ test -f src/part2.c &&
+ test ! -f a.out &&
+ test -f docs/manual.txt &&
+ test ! -f src/part3.c &&
+ test ! -f src/part3.h &&
+ test -f src/part4.c &&
+ test -f src/part4.h &&
+ test -f obj.o &&
+ test -f build/lib.so
+
+'
+
+test_expect_success 'git clean -id (ask)' '
+
+ mkdir -p build docs &&
+ touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
+ docs/manual.txt obj.o build/lib.so &&
+ test_write_lines a Y y no yes bad "" |
+ git clean -id &&
+ test -f Makefile &&
+ test -f README &&
+ test -f src/part1.c &&
+ test -f src/part2.c &&
+ test ! -f a.out &&
+ test ! -f docs/manual.txt &&
+ test -f src/part3.c &&
+ test ! -f src/part3.h &&
+ test -f src/part4.c &&
+ test -f src/part4.h &&
+ test -f obj.o &&
+ test -f build/lib.so
+
+'
+
+test_expect_success 'git clean -id (ask - Ctrl+D)' '
+
+ mkdir -p build docs &&
+ touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
+ docs/manual.txt obj.o build/lib.so &&
+ test_write_lines a Y no yes "\04" |
+ git clean -id &&
+ test -f Makefile &&
+ test -f README &&
+ test -f src/part1.c &&
+ test -f src/part2.c &&
+ test ! -f a.out &&
+ test -f docs/manual.txt &&
+ test ! -f src/part3.c &&
+ test -f src/part3.h &&
+ test -f src/part4.c &&
+ test -f src/part4.h &&
+ test -f obj.o &&
+ test -f build/lib.so
+
+'
+
+test_expect_success 'git clean -id with prefix and path (filter)' '
+
+ mkdir -p build docs &&
+ touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
+ docs/manual.txt obj.o build/lib.so &&
+ (cd build/ &&
+ test_write_lines f docs "*.h" "" c |
+ git clean -id ..) &&
+ test -f Makefile &&
+ test -f README &&
+ test -f src/part1.c &&
+ test -f src/part2.c &&
+ test ! -f a.out &&
+ test -f docs/manual.txt &&
+ test ! -f src/part3.c &&
+ test -f src/part3.h &&
+ test ! -f src/part4.c &&
+ test -f src/part4.h &&
+ test -f obj.o &&
+ test -f build/lib.so
+
+'
+
+test_expect_success 'git clean -id with prefix and path (select by name)' '
+
+ mkdir -p build docs &&
+ touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
+ docs/manual.txt obj.o build/lib.so &&
+ (cd build/ &&
+ test_write_lines s ../docs/ ../src/part3.c ../src/part4.c "" c |
+ git clean -id ..) &&
+ test -f Makefile &&
+ test -f README &&
+ test -f src/part1.c &&
+ test -f src/part2.c &&
+ test -f a.out &&
+ test ! -f docs/manual.txt &&
+ test ! -f src/part3.c &&
+ test -f src/part3.h &&
+ test ! -f src/part4.c &&
+ test -f src/part4.h &&
+ test -f obj.o &&
+ test -f build/lib.so
+
+'
+
+test_expect_success 'git clean -id with prefix and path (ask)' '
+
+ mkdir -p build docs &&
+ touch a.out src/part3.c src/part3.h src/part4.c src/part4.h \
+ docs/manual.txt obj.o build/lib.so &&
+ (cd build/ &&
+ test_write_lines a Y y no yes bad "" |
+ git clean -id ..) &&
+ test -f Makefile &&
+ test -f README &&
+ test -f src/part1.c &&
+ test -f src/part2.c &&
+ test ! -f a.out &&
+ test ! -f docs/manual.txt &&
+ test -f src/part3.c &&
+ test ! -f src/part3.h &&
+ test -f src/part4.c &&
+ test -f src/part4.h &&
+ test -f obj.o &&
+ test -f build/lib.so
+
+'
+
+test_expect_success TTY 'git clean -i paints the header in HEADER color' '
+ >a.out &&
+ echo q |
+ test_terminal git clean -i |
+ test_decode_color |
+ head -n 1 >header &&
+ # not i18ngrep
+ grep "^<BOLD>" header
+'
+
+test_done
diff --git a/t/t7400-submodule-basic.sh b/t/t7400-submodule-basic.sh
new file mode 100755
index 0000000..00c1f1a
--- /dev/null
+++ b/t/t7400-submodule-basic.sh
@@ -0,0 +1,1455 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Lars Hjemli
+#
+
+test_description='Basic porcelain support for submodules
+
+This test tries to verify basic sanity of the init, update and status
+subcommands of git submodule.
+'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success 'setup - enable local submodules' '
+ git config --global protocol.file.allow always
+'
+
+test_expect_success 'submodule usage: -h' '
+ git submodule -h >out 2>err &&
+ grep "^usage: git submodule" out &&
+ test_must_be_empty err
+'
+
+test_expect_success 'submodule usage: --recursive' '
+ test_expect_code 1 git submodule --recursive >out 2>err &&
+ grep "^usage: git submodule" err &&
+ test_must_be_empty out
+'
+
+test_expect_success 'submodule usage: status --' '
+ test_expect_code 1 git submodule -- &&
+ test_expect_code 1 git submodule --end-of-options
+'
+
+for opt in '--quiet' '--cached'
+do
+ test_expect_success "submodule usage: status $opt" '
+ git submodule $opt &&
+ git submodule status $opt &&
+ git submodule $opt status
+ '
+done
+
+test_expect_success 'submodule deinit works on empty repository' '
+ git submodule deinit --all
+'
+
+test_expect_success 'setup - initial commit' '
+ >t &&
+ git add t &&
+ git commit -m "initial commit" &&
+ git branch initial
+'
+
+test_expect_success 'submodule init aborts on missing .gitmodules file' '
+ test_when_finished "git update-index --remove sub" &&
+ git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
+ # missing the .gitmodules file here
+ test_must_fail git submodule init 2>actual &&
+ test_grep "No url found for submodule path" actual
+'
+
+test_expect_success 'submodule update aborts on missing .gitmodules file' '
+ test_when_finished "git update-index --remove sub" &&
+ git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
+ # missing the .gitmodules file here
+ git submodule update sub 2>actual &&
+ test_grep "Submodule path .sub. not initialized" actual
+'
+
+test_expect_success 'submodule update aborts on missing gitmodules url' '
+ test_when_finished "git update-index --remove sub" &&
+ git update-index --add --cacheinfo 160000,$(git rev-parse HEAD),sub &&
+ test_when_finished "rm -f .gitmodules" &&
+ git config -f .gitmodules submodule.s.path sub &&
+ test_must_fail git submodule init
+'
+
+test_expect_success 'add aborts on repository with no commits' '
+ cat >expect <<-\EOF &&
+ fatal: '"'repo-no-commits'"' does not have a commit checked out
+ EOF
+ git init repo-no-commits &&
+ test_must_fail git submodule add ../a ./repo-no-commits 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'status should ignore inner git repo when not added' '
+ rm -fr inner &&
+ mkdir inner &&
+ (
+ cd inner &&
+ git init &&
+ >t &&
+ git add t &&
+ git commit -m "initial"
+ ) &&
+ test_must_fail git submodule status inner 2>output.err &&
+ rm -fr inner &&
+ test_grep "^error: .*did not match any file(s) known to git" output.err
+'
+
+test_expect_success 'setup - repository in init subdirectory' '
+ mkdir init &&
+ (
+ cd init &&
+ git init &&
+ echo a >a &&
+ git add a &&
+ git commit -m "submodule commit 1" &&
+ git tag -a -m "rev-1" rev-1
+ )
+'
+
+test_expect_success 'setup - commit with gitlink' '
+ echo a >a &&
+ echo z >z &&
+ git add a init z &&
+ git commit -m "super commit 1"
+'
+
+test_expect_success 'setup - hide init subdirectory' '
+ mv init .subrepo
+'
+
+test_expect_success 'setup - repository to add submodules to' '
+ git init addtest &&
+ git init addtest-ignore
+'
+
+# The 'submodule add' tests need some repository to add as a submodule.
+# The trash directory is a good one as any. We need to canonicalize
+# the name, though, as some tests compare it to the absolute path git
+# generates, which will expand symbolic links.
+submodurl=$(pwd -P)
+
+listbranches() {
+ git for-each-ref --format='%(refname)' 'refs/heads/*'
+}
+
+inspect() {
+ dir=$1 &&
+ dotdot="${2:-..}" &&
+
+ (
+ cd "$dir" &&
+ listbranches >"$dotdot/heads" &&
+ { git symbolic-ref HEAD || :; } >"$dotdot/head" &&
+ git rev-parse HEAD >"$dotdot/head-sha1" &&
+ git update-index --refresh &&
+ git diff-files --exit-code &&
+ git clean -n -d -x >"$dotdot/untracked"
+ )
+}
+
+test_expect_success 'submodule add' '
+ echo "refs/heads/main" >expect &&
+
+ (
+ cd addtest &&
+ git submodule add -q "$submodurl" submod >actual &&
+ test_must_be_empty actual &&
+ echo "gitdir: ../.git/modules/submod" >expect &&
+ test_cmp expect submod/.git &&
+ (
+ cd submod &&
+ git config core.worktree >actual &&
+ echo "../../../submod" >expect &&
+ test_cmp expect actual &&
+ rm -f actual expect
+ ) &&
+ git submodule init
+ ) &&
+
+ rm -f heads head untracked &&
+ inspect addtest/submod ../.. &&
+ test_cmp expect heads &&
+ test_cmp expect head &&
+ test_must_be_empty untracked
+'
+
+test_expect_success !WINDOWS 'submodule add (absolute path)' '
+ test_when_finished "git reset --hard" &&
+ git submodule add "$submodurl" "$submodurl/add-abs"
+'
+
+test_expect_success 'setup parent and one repository' '
+ test_create_repo parent &&
+ test_commit -C parent one
+'
+
+test_expect_success 'redirected submodule add does not show progress' '
+ git -C addtest submodule add "file://$submodurl/parent" submod-redirected \
+ 2>err &&
+ ! grep % err &&
+ test_grep ! "Checking connectivity" err
+'
+
+test_expect_success 'redirected submodule add --progress does show progress' '
+ git -C addtest submodule add --progress "file://$submodurl/parent" \
+ submod-redirected-progress 2>err && \
+ grep % err
+'
+
+test_expect_success 'submodule add to .gitignored path fails' '
+ (
+ cd addtest-ignore &&
+ cat <<-\EOF >expect &&
+ The following paths are ignored by one of your .gitignore files:
+ submod
+ hint: Use -f if you really want to add them.
+ hint: Turn this message off by running
+ hint: "git config advice.addIgnoredFile false"
+ EOF
+ # Does not use test_commit due to the ignore
+ echo "*" > .gitignore &&
+ git add --force .gitignore &&
+ git commit -m"Ignore everything" &&
+ ! git submodule add "$submodurl" submod >actual 2>&1 &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'submodule add to .gitignored path with --force' '
+ (
+ cd addtest-ignore &&
+ git submodule add --force "$submodurl" submod
+ )
+'
+
+test_expect_success 'submodule add to path with tracked content fails' '
+ (
+ cd addtest &&
+ echo "fatal: '\''dir-tracked'\'' already exists in the index" >expect &&
+ mkdir dir-tracked &&
+ test_commit foo dir-tracked/bar &&
+ test_must_fail git submodule add "$submodurl" dir-tracked >actual 2>&1 &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'submodule add to reconfigure existing submodule with --force' '
+ (
+ cd addtest-ignore &&
+ bogus_url="$(pwd)/bogus-url" &&
+ git submodule add --force "$bogus_url" submod &&
+ git submodule add --force -b initial "$submodurl" submod-branch &&
+ test "$bogus_url" = "$(git config -f .gitmodules submodule.submod.url)" &&
+ test "$bogus_url" = "$(git config submodule.submod.url)" &&
+ # Restore the url
+ git submodule add --force "$submodurl" submod &&
+ test "$submodurl" = "$(git config -f .gitmodules submodule.submod.url)" &&
+ test "$submodurl" = "$(git config submodule.submod.url)"
+ )
+'
+
+test_expect_success 'submodule add relays add --dry-run stderr' '
+ test_when_finished "rm -rf addtest/.git/index.lock" &&
+ (
+ cd addtest &&
+ : >.git/index.lock &&
+ ! git submodule add "$submodurl" sub-while-locked 2>output.err &&
+ test_grep "^fatal: .*index\.lock" output.err &&
+ test_path_is_missing sub-while-locked
+ )
+'
+
+test_expect_success 'submodule add --branch' '
+ echo "refs/heads/initial" >expect-head &&
+ cat <<-\EOF >expect-heads &&
+ refs/heads/initial
+ refs/heads/main
+ EOF
+
+ (
+ cd addtest &&
+ git submodule add -b initial "$submodurl" submod-branch &&
+ test "initial" = "$(git config -f .gitmodules submodule.submod-branch.branch)" &&
+ git submodule init
+ ) &&
+
+ rm -f heads head untracked &&
+ inspect addtest/submod-branch ../.. &&
+ test_cmp expect-heads heads &&
+ test_cmp expect-head head &&
+ test_must_be_empty untracked
+'
+
+test_expect_success 'submodule add with ./ in path' '
+ echo "refs/heads/main" >expect &&
+
+ (
+ cd addtest &&
+ git submodule add "$submodurl" ././dotsubmod/./frotz/./ &&
+ git submodule init
+ ) &&
+
+ rm -f heads head untracked &&
+ inspect addtest/dotsubmod/frotz ../../.. &&
+ test_cmp expect heads &&
+ test_cmp expect head &&
+ test_must_be_empty untracked
+'
+
+test_expect_success 'submodule add with /././ in path' '
+ echo "refs/heads/main" >expect &&
+
+ (
+ cd addtest &&
+ git submodule add "$submodurl" dotslashdotsubmod/././frotz/./ &&
+ git submodule init
+ ) &&
+
+ rm -f heads head untracked &&
+ inspect addtest/dotslashdotsubmod/frotz ../../.. &&
+ test_cmp expect heads &&
+ test_cmp expect head &&
+ test_must_be_empty untracked
+'
+
+test_expect_success 'submodule add with // in path' '
+ echo "refs/heads/main" >expect &&
+
+ (
+ cd addtest &&
+ git submodule add "$submodurl" slashslashsubmod///frotz// &&
+ git submodule init
+ ) &&
+
+ rm -f heads head untracked &&
+ inspect addtest/slashslashsubmod/frotz ../../.. &&
+ test_cmp expect heads &&
+ test_cmp expect head &&
+ test_must_be_empty untracked
+'
+
+test_expect_success 'submodule add with /.. in path' '
+ echo "refs/heads/main" >expect &&
+
+ (
+ cd addtest &&
+ git submodule add "$submodurl" dotdotsubmod/../realsubmod/frotz/.. &&
+ git submodule init
+ ) &&
+
+ rm -f heads head untracked &&
+ inspect addtest/realsubmod ../.. &&
+ test_cmp expect heads &&
+ test_cmp expect head &&
+ test_must_be_empty untracked
+'
+
+test_expect_success 'submodule add with ./, /.. and // in path' '
+ echo "refs/heads/main" >expect &&
+
+ (
+ cd addtest &&
+ git submodule add "$submodurl" dot/dotslashsubmod/./../..////realsubmod2/a/b/c/d/../../../../frotz//.. &&
+ git submodule init
+ ) &&
+
+ rm -f heads head untracked &&
+ inspect addtest/realsubmod2 ../.. &&
+ test_cmp expect heads &&
+ test_cmp expect head &&
+ test_must_be_empty untracked
+'
+
+test_expect_success !CYGWIN 'submodule add with \\ in path' '
+ test_when_finished "rm -rf parent sub\\with\\backslash" &&
+
+ # Initialize a repo with a backslash in its name
+ git init sub\\with\\backslash &&
+ touch sub\\with\\backslash/empty.file &&
+ git -C sub\\with\\backslash add empty.file &&
+ git -C sub\\with\\backslash commit -m "Added empty.file" &&
+
+ # Add that repository as a submodule
+ git init parent &&
+ git -C parent submodule add ../sub\\with\\backslash
+'
+
+test_expect_success 'submodule add in subdirectory' '
+ echo "refs/heads/main" >expect &&
+
+ mkdir addtest/sub &&
+ (
+ cd addtest/sub &&
+ git submodule add "$submodurl" ../realsubmod3 &&
+ git submodule init
+ ) &&
+
+ rm -f heads head untracked &&
+ inspect addtest/realsubmod3 ../.. &&
+ test_cmp expect heads &&
+ test_cmp expect head &&
+ test_must_be_empty untracked
+'
+
+test_expect_success 'submodule add in subdirectory with relative path should fail' '
+ (
+ cd addtest/sub &&
+ test_must_fail git submodule add ../../ submod3 2>../../output.err
+ ) &&
+ test_grep toplevel output.err
+'
+
+test_expect_success 'setup - add an example entry to .gitmodules' '
+ git config --file=.gitmodules submodule.example.url git://example.com/init.git
+'
+
+test_expect_success 'status should fail for unmapped paths' '
+ test_must_fail git submodule status
+'
+
+test_expect_success 'setup - map path in .gitmodules' '
+ cat <<\EOF >expect &&
+[submodule "example"]
+ url = git://example.com/init.git
+ path = init
+EOF
+
+ git config --file=.gitmodules submodule.example.path init &&
+
+ test_cmp expect .gitmodules
+'
+
+test_expect_success 'status should only print one line' '
+ git submodule status >lines &&
+ test_line_count = 1 lines
+'
+
+test_expect_success 'status from subdirectory should have the same SHA1' '
+ test_when_finished "rmdir addtest/subdir" &&
+ (
+ cd addtest &&
+ mkdir subdir &&
+ git submodule status >output &&
+ awk "{print \$1}" <output >expect &&
+ cd subdir &&
+ git submodule status >../output &&
+ awk "{print \$1}" <../output >../actual &&
+ test_cmp ../expect ../actual &&
+ git -C ../submod checkout HEAD^ &&
+ git submodule status >../output &&
+ awk "{print \$1}" <../output >../actual2 &&
+ cd .. &&
+ git submodule status >output &&
+ awk "{print \$1}" <output >expect2 &&
+ test_cmp expect2 actual2 &&
+ ! test_cmp actual actual2
+ )
+'
+
+test_expect_success 'setup - fetch commit name from submodule' '
+ rev1=$(cd .subrepo && git rev-parse HEAD) &&
+ printf "rev1: %s\n" "$rev1" &&
+ test -n "$rev1"
+'
+
+test_expect_success 'status should initially be "missing"' '
+ git submodule status >lines &&
+ grep "^-$rev1" lines
+'
+
+test_expect_success 'init should register submodule url in .git/config' '
+ echo git://example.com/init.git >expect &&
+
+ git submodule init &&
+ git config submodule.example.url >url &&
+ git config submodule.example.url ./.subrepo &&
+
+ test_cmp expect url
+'
+
+test_expect_success 'status should still be "missing" after initializing' '
+ rm -fr init &&
+ mkdir init &&
+ git submodule status >lines &&
+ rm -fr init &&
+ grep "^-$rev1" lines
+'
+
+test_failure_with_unknown_submodule () {
+ test_must_fail git submodule $1 no-such-submodule 2>output.err &&
+ test_grep "^error: .*no-such-submodule" output.err
+}
+
+test_expect_success 'init should fail with unknown submodule' '
+ test_failure_with_unknown_submodule init
+'
+
+test_expect_success 'update should fail with unknown submodule' '
+ test_failure_with_unknown_submodule update
+'
+
+test_expect_success 'status should fail with unknown submodule' '
+ test_failure_with_unknown_submodule status
+'
+
+test_expect_success 'sync should fail with unknown submodule' '
+ test_failure_with_unknown_submodule sync
+'
+
+test_expect_success 'update should fail when path is used by a file' '
+ echo hello >expect &&
+
+ echo "hello" >init &&
+ test_must_fail git submodule update &&
+
+ test_cmp expect init
+'
+
+test_expect_success 'update should fail when path is used by a nonempty directory' '
+ echo hello >expect &&
+
+ rm -fr init &&
+ mkdir init &&
+ echo "hello" >init/a &&
+
+ test_must_fail git submodule update &&
+
+ test_cmp expect init/a
+'
+
+test_expect_success 'update should work when path is an empty dir' '
+ rm -fr init &&
+ rm -f head-sha1 &&
+ echo "$rev1" >expect &&
+
+ mkdir init &&
+ git submodule update -q >update.out &&
+ test_must_be_empty update.out &&
+
+ inspect init &&
+ test_cmp expect head-sha1
+'
+
+test_expect_success 'status should be "up-to-date" after update' '
+ git submodule status >list &&
+ grep "^ $rev1" list
+'
+
+test_expect_success 'status "up-to-date" from subdirectory' '
+ mkdir -p sub &&
+ (
+ cd sub &&
+ git submodule status >../list
+ ) &&
+ grep "^ $rev1" list &&
+ grep "\\.\\./init" list
+'
+
+test_expect_success 'status "up-to-date" from subdirectory with path' '
+ mkdir -p sub &&
+ (
+ cd sub &&
+ git submodule status ../init >../list
+ ) &&
+ grep "^ $rev1" list &&
+ grep "\\.\\./init" list
+'
+
+test_expect_success 'status should be "modified" after submodule commit' '
+ (
+ cd init &&
+ echo b >b &&
+ git add b &&
+ git commit -m "submodule commit 2"
+ ) &&
+
+ rev2=$(cd init && git rev-parse HEAD) &&
+ test -n "$rev2" &&
+ git submodule status >list &&
+
+ grep "^+$rev2" list
+'
+
+test_expect_success '"submodule --cached" command forms should be identical' '
+ git submodule status --cached >expect &&
+
+ git submodule --cached >actual &&
+ test_cmp expect actual &&
+
+ git submodule --cached status >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'the --cached sha1 should be rev1' '
+ git submodule --cached status >list &&
+ grep "^+$rev1" list
+'
+
+test_expect_success 'git diff should report the SHA1 of the new submodule commit' '
+ git diff >diff &&
+ grep "^+Subproject commit $rev2" diff
+'
+
+test_expect_success 'update should checkout rev1' '
+ rm -f head-sha1 &&
+ echo "$rev1" >expect &&
+
+ git submodule update init &&
+ inspect init &&
+
+ test_cmp expect head-sha1
+'
+
+test_expect_success 'status should be "up-to-date" after update' '
+ git submodule status >list &&
+ grep "^ $rev1" list
+'
+
+test_expect_success 'checkout superproject with subproject already present' '
+ git checkout initial &&
+ git checkout main
+'
+
+test_expect_success 'apply submodule diff' '
+ git branch second &&
+ (
+ cd init &&
+ echo s >s &&
+ git add s &&
+ git commit -m "change subproject"
+ ) &&
+ git update-index --add init &&
+ git commit -m "change init" &&
+ git format-patch -1 --stdout >P.diff &&
+ git checkout second &&
+ git apply --index P.diff &&
+
+ git diff --cached main >staged &&
+ test_must_be_empty staged
+'
+
+test_expect_success 'update --init' '
+ mv init init2 &&
+ git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
+ git config --remove-section submodule.example &&
+ test_must_fail git config submodule.example.url &&
+
+ git submodule update init 2> update.out &&
+ test_grep "not initialized" update.out &&
+ test_must_fail git rev-parse --resolve-git-dir init/.git &&
+
+ git submodule update --init init &&
+ git rev-parse --resolve-git-dir init/.git
+'
+
+test_expect_success 'update --init from subdirectory' '
+ mv init init2 &&
+ git config -f .gitmodules submodule.example.url "$(pwd)/init2" &&
+ git config --remove-section submodule.example &&
+ test_must_fail git config submodule.example.url &&
+
+ mkdir -p sub &&
+ (
+ cd sub &&
+ git submodule update ../init 2>update.out &&
+ test_grep "not initialized" update.out &&
+ test_must_fail git rev-parse --resolve-git-dir ../init/.git &&
+
+ git submodule update --init ../init
+ ) &&
+ git rev-parse --resolve-git-dir init/.git
+'
+
+test_expect_success 'do not add files from a submodule' '
+
+ git reset --hard &&
+ test_must_fail git add init/a
+
+'
+
+test_expect_success 'gracefully add/reset submodule with a trailing slash' '
+
+ git reset --hard &&
+ git commit -m "commit subproject" init &&
+ (cd init &&
+ echo b > a) &&
+ git add init/ &&
+ git diff --exit-code --cached init &&
+ commit=$(cd init &&
+ git commit -m update a >/dev/null &&
+ git rev-parse HEAD) &&
+ git add init/ &&
+ test_must_fail git diff --exit-code --cached init &&
+ test $commit = $(git ls-files --stage |
+ sed -n "s/^160000 \([^ ]*\).*/\1/p") &&
+ git reset init/ &&
+ git diff --exit-code --cached init
+
+'
+
+test_expect_success 'ls-files gracefully handles trailing slash' '
+
+ test "init" = "$(git ls-files init/)"
+
+'
+
+test_expect_success 'moving to a commit without submodule does not leave empty dir' '
+ rm -rf init &&
+ mkdir init &&
+ git reset --hard &&
+ git checkout initial &&
+ test ! -d init &&
+ git checkout second
+'
+
+test_expect_success 'submodule <invalid-subcommand> fails' '
+ test_must_fail git submodule no-such-subcommand
+'
+
+test_expect_success 'add submodules without specifying an explicit path' '
+ mkdir repo &&
+ (
+ cd repo &&
+ git init &&
+ echo r >r &&
+ git add r &&
+ git commit -m "repo commit 1"
+ ) &&
+ git clone --bare repo/ bare.git &&
+ (
+ cd addtest &&
+ git submodule add "$submodurl/repo" &&
+ git config -f .gitmodules submodule.repo.path repo &&
+ git submodule add "$submodurl/bare.git" &&
+ git config -f .gitmodules submodule.bare.path bare
+ )
+'
+
+test_expect_success 'add should fail when path is used by a file' '
+ (
+ cd addtest &&
+ touch file &&
+ test_must_fail git submodule add "$submodurl/repo" file
+ )
+'
+
+test_expect_success 'add should fail when path is used by an existing directory' '
+ (
+ cd addtest &&
+ mkdir empty-dir &&
+ test_must_fail git submodule add "$submodurl/repo" empty-dir
+ )
+'
+
+test_expect_success 'use superproject as upstream when path is relative and no url is set there' '
+ (
+ cd addtest &&
+ git submodule add ../repo relative &&
+ test "$(git config -f .gitmodules submodule.relative.url)" = ../repo &&
+ git submodule sync relative &&
+ test "$(git config submodule.relative.url)" = "$submodurl/repo"
+ )
+'
+
+test_expect_success 'set up for relative path tests' '
+ mkdir reltest &&
+ (
+ cd reltest &&
+ git init &&
+ mkdir sub &&
+ (
+ cd sub &&
+ git init &&
+ test_commit foo
+ ) &&
+ git add sub &&
+ git config -f .gitmodules submodule.sub.path sub &&
+ git config -f .gitmodules submodule.sub.url ../subrepo &&
+ cp .git/config pristine-.git-config &&
+ cp .gitmodules pristine-.gitmodules
+ )
+'
+
+test_expect_success '../subrepo works with URL - ssh://hostname/repo' '
+ (
+ cd reltest &&
+ cp pristine-.git-config .git/config &&
+ cp pristine-.gitmodules .gitmodules &&
+ git config remote.origin.url ssh://hostname/repo &&
+ git submodule init &&
+ test "$(git config submodule.sub.url)" = ssh://hostname/subrepo
+ )
+'
+
+test_expect_success '../subrepo works with port-qualified URL - ssh://hostname:22/repo' '
+ (
+ cd reltest &&
+ cp pristine-.git-config .git/config &&
+ cp pristine-.gitmodules .gitmodules &&
+ git config remote.origin.url ssh://hostname:22/repo &&
+ git submodule init &&
+ test "$(git config submodule.sub.url)" = ssh://hostname:22/subrepo
+ )
+'
+
+# About the choice of the path in the next test:
+# - double-slash side-steps path mangling issues on Windows
+# - it is still an absolute local path
+# - there cannot be a server with a blank in its name just in case the
+# path is used erroneously to access a //server/share style path
+test_expect_success '../subrepo path works with local path - //somewhere else/repo' '
+ (
+ cd reltest &&
+ cp pristine-.git-config .git/config &&
+ cp pristine-.gitmodules .gitmodules &&
+ git config remote.origin.url "//somewhere else/repo" &&
+ git submodule init &&
+ test "$(git config submodule.sub.url)" = "//somewhere else/subrepo"
+ )
+'
+
+test_expect_success '../subrepo works with file URL - file:///tmp/repo' '
+ (
+ cd reltest &&
+ cp pristine-.git-config .git/config &&
+ cp pristine-.gitmodules .gitmodules &&
+ git config remote.origin.url file:///tmp/repo &&
+ git submodule init &&
+ test "$(git config submodule.sub.url)" = file:///tmp/subrepo
+ )
+'
+
+test_expect_success '../subrepo works with helper URL- helper:://hostname/repo' '
+ (
+ cd reltest &&
+ cp pristine-.git-config .git/config &&
+ cp pristine-.gitmodules .gitmodules &&
+ git config remote.origin.url helper:://hostname/repo &&
+ git submodule init &&
+ test "$(git config submodule.sub.url)" = helper:://hostname/subrepo
+ )
+'
+
+test_expect_success '../subrepo works with scp-style URL - user@host:repo' '
+ (
+ cd reltest &&
+ cp pristine-.git-config .git/config &&
+ git config remote.origin.url user@host:repo &&
+ git submodule init &&
+ test "$(git config submodule.sub.url)" = user@host:subrepo
+ )
+'
+
+test_expect_success '../subrepo works with scp-style URL - user@host:path/to/repo' '
+ (
+ cd reltest &&
+ cp pristine-.git-config .git/config &&
+ cp pristine-.gitmodules .gitmodules &&
+ git config remote.origin.url user@host:path/to/repo &&
+ git submodule init &&
+ test "$(git config submodule.sub.url)" = user@host:path/to/subrepo
+ )
+'
+
+test_expect_success '../subrepo works with relative local path - foo' '
+ (
+ cd reltest &&
+ cp pristine-.git-config .git/config &&
+ cp pristine-.gitmodules .gitmodules &&
+ git config remote.origin.url foo &&
+ # actual: fails with an error
+ git submodule init &&
+ test "$(git config submodule.sub.url)" = subrepo
+ )
+'
+
+test_expect_success '../subrepo works with relative local path - foo/bar' '
+ (
+ cd reltest &&
+ cp pristine-.git-config .git/config &&
+ cp pristine-.gitmodules .gitmodules &&
+ git config remote.origin.url foo/bar &&
+ git submodule init &&
+ test "$(git config submodule.sub.url)" = foo/subrepo
+ )
+'
+
+test_expect_success '../subrepo works with relative local path - ./foo' '
+ (
+ cd reltest &&
+ cp pristine-.git-config .git/config &&
+ cp pristine-.gitmodules .gitmodules &&
+ git config remote.origin.url ./foo &&
+ git submodule init &&
+ test "$(git config submodule.sub.url)" = subrepo
+ )
+'
+
+test_expect_success '../subrepo works with relative local path - ./foo/bar' '
+ (
+ cd reltest &&
+ cp pristine-.git-config .git/config &&
+ cp pristine-.gitmodules .gitmodules &&
+ git config remote.origin.url ./foo/bar &&
+ git submodule init &&
+ test "$(git config submodule.sub.url)" = foo/subrepo
+ )
+'
+
+test_expect_success '../subrepo works with relative local path - ../foo' '
+ (
+ cd reltest &&
+ cp pristine-.git-config .git/config &&
+ cp pristine-.gitmodules .gitmodules &&
+ git config remote.origin.url ../foo &&
+ git submodule init &&
+ test "$(git config submodule.sub.url)" = ../subrepo
+ )
+'
+
+test_expect_success '../subrepo works with relative local path - ../foo/bar' '
+ (
+ cd reltest &&
+ cp pristine-.git-config .git/config &&
+ cp pristine-.gitmodules .gitmodules &&
+ git config remote.origin.url ../foo/bar &&
+ git submodule init &&
+ test "$(git config submodule.sub.url)" = ../foo/subrepo
+ )
+'
+
+test_expect_success '../bar/a/b/c works with relative local path - ../foo/bar.git' '
+ (
+ cd reltest &&
+ cp pristine-.git-config .git/config &&
+ cp pristine-.gitmodules .gitmodules &&
+ mkdir -p a/b/c &&
+ (cd a/b/c && git init && test_commit msg) &&
+ git config remote.origin.url ../foo/bar.git &&
+ git submodule add ../bar/a/b/c ./a/b/c &&
+ git submodule init &&
+ test "$(git config submodule.a/b/c.url)" = ../foo/bar/a/b/c
+ )
+'
+
+test_expect_success 'moving the superproject does not break submodules' '
+ (
+ cd addtest &&
+ git submodule status >expect
+ ) &&
+ mv addtest addtest2 &&
+ (
+ cd addtest2 &&
+ git submodule status >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'moving the submodule does not break the superproject' '
+ (
+ cd addtest2 &&
+ git submodule status
+ ) >actual &&
+ sed -e "s/^ \([^ ]* repo\) .*/-\1/" <actual >expect &&
+ mv addtest2/repo addtest2/repo.bak &&
+ test_when_finished "mv addtest2/repo.bak addtest2/repo" &&
+ (
+ cd addtest2 &&
+ git submodule status
+ ) >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'submodule add --name allows to replace a submodule with another at the same path' '
+ (
+ cd addtest2 &&
+ (
+ cd repo &&
+ echo "$submodurl/repo" >expect &&
+ git config remote.origin.url >actual &&
+ test_cmp expect actual &&
+ echo "gitdir: ../.git/modules/repo" >expect &&
+ test_cmp expect .git
+ ) &&
+ rm -rf repo &&
+ git rm repo &&
+ git submodule add -q --name repo_new "$submodurl/bare.git" repo >actual &&
+ test_must_be_empty actual &&
+ echo "gitdir: ../.git/modules/submod" >expect &&
+ test_cmp expect submod/.git &&
+ (
+ cd repo &&
+ echo "$submodurl/bare.git" >expect &&
+ git config remote.origin.url >actual &&
+ test_cmp expect actual &&
+ echo "gitdir: ../.git/modules/repo_new" >expect &&
+ test_cmp expect .git
+ ) &&
+ echo "repo" >expect &&
+ test_must_fail git config -f .gitmodules submodule.repo.path &&
+ git config -f .gitmodules submodule.repo_new.path >actual &&
+ test_cmp expect actual &&
+ echo "$submodurl/repo" >expect &&
+ test_must_fail git config -f .gitmodules submodule.repo.url &&
+ echo "$submodurl/bare.git" >expect &&
+ git config -f .gitmodules submodule.repo_new.url >actual &&
+ test_cmp expect actual &&
+ echo "$submodurl/repo" >expect &&
+ git config submodule.repo.url >actual &&
+ test_cmp expect actual &&
+ echo "$submodurl/bare.git" >expect &&
+ git config submodule.repo_new.url >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'recursive relative submodules stay relative' '
+ test_when_finished "rm -rf super clone2 subsub sub3" &&
+ mkdir subsub &&
+ (
+ cd subsub &&
+ git init &&
+ >t &&
+ git add t &&
+ git commit -m "initial commit"
+ ) &&
+ mkdir sub3 &&
+ (
+ cd sub3 &&
+ git init &&
+ >t &&
+ git add t &&
+ git commit -m "initial commit" &&
+ git submodule add ../subsub dirdir/subsub &&
+ git commit -m "add submodule subsub"
+ ) &&
+ mkdir super &&
+ (
+ cd super &&
+ git init &&
+ >t &&
+ git add t &&
+ git commit -m "initial commit" &&
+ git submodule add ../sub3 &&
+ git commit -m "add submodule sub"
+ ) &&
+ git clone super clone2 &&
+ (
+ cd clone2 &&
+ git submodule update --init --recursive &&
+ echo "gitdir: ../.git/modules/sub3" >./sub3/.git_expect &&
+ echo "gitdir: ../../../.git/modules/sub3/modules/dirdir/subsub" >./sub3/dirdir/subsub/.git_expect
+ ) &&
+ test_cmp clone2/sub3/.git_expect clone2/sub3/.git &&
+ test_cmp clone2/sub3/dirdir/subsub/.git_expect clone2/sub3/dirdir/subsub/.git
+'
+
+test_expect_success 'submodule add with an existing name fails unless forced' '
+ (
+ cd addtest2 &&
+ rm -rf repo &&
+ git rm repo &&
+ test_must_fail git submodule add -q --name repo_new "$submodurl/repo.git" repo &&
+ test ! -d repo &&
+ test_must_fail git config -f .gitmodules submodule.repo_new.path &&
+ test_must_fail git config -f .gitmodules submodule.repo_new.url &&
+ echo "$submodurl/bare.git" >expect &&
+ git config submodule.repo_new.url >actual &&
+ test_cmp expect actual &&
+ git submodule add -f -q --name repo_new "$submodurl/repo.git" repo &&
+ test -d repo &&
+ echo "repo" >expect &&
+ git config -f .gitmodules submodule.repo_new.path >actual &&
+ test_cmp expect actual &&
+ echo "$submodurl/repo.git" >expect &&
+ git config -f .gitmodules submodule.repo_new.url >actual &&
+ test_cmp expect actual &&
+ echo "$submodurl/repo.git" >expect &&
+ git config submodule.repo_new.url >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'set up a second submodule' '
+ git submodule add ./init2 example2 &&
+ git commit -m "submodule example2 added"
+'
+
+test_expect_success 'submodule deinit works on repository without submodules' '
+ test_when_finished "rm -rf newdirectory" &&
+ mkdir newdirectory &&
+ (
+ cd newdirectory &&
+ git init &&
+ >file &&
+ git add file &&
+ git commit -m "repo should not be empty" &&
+ git submodule deinit . &&
+ git submodule deinit --all
+ )
+'
+
+test_expect_success 'submodule deinit should remove the whole submodule section from .git/config' '
+ git config submodule.example.foo bar &&
+ git config submodule.example2.frotz nitfol &&
+ git submodule deinit init &&
+ test -z "$(git config --get-regexp "submodule\.example\.")" &&
+ test -n "$(git config --get-regexp "submodule\.example2\.")" &&
+ test -f example2/.git &&
+ rmdir init
+'
+
+test_expect_success 'submodule deinit should unset core.worktree' '
+ test_path_is_file .git/modules/example/config &&
+ test_must_fail git config -f .git/modules/example/config core.worktree
+'
+
+test_expect_success 'submodule deinit from subdirectory' '
+ git submodule update --init &&
+ git config submodule.example.foo bar &&
+ mkdir -p sub &&
+ (
+ cd sub &&
+ git submodule deinit ../init >../output
+ ) &&
+ test_grep "\\.\\./init" output &&
+ test -z "$(git config --get-regexp "submodule\.example\.")" &&
+ test -n "$(git config --get-regexp "submodule\.example2\.")" &&
+ test -f example2/.git &&
+ rmdir init
+'
+
+test_expect_success 'submodule deinit . deinits all initialized submodules' '
+ git submodule update --init &&
+ git config submodule.example.foo bar &&
+ git config submodule.example2.frotz nitfol &&
+ test_must_fail git submodule deinit &&
+ git submodule deinit . >actual &&
+ test -z "$(git config --get-regexp "submodule\.example\.")" &&
+ test -z "$(git config --get-regexp "submodule\.example2\.")" &&
+ test_grep "Cleared directory .init" actual &&
+ test_grep "Cleared directory .example2" actual &&
+ rmdir init example2
+'
+
+test_expect_success 'submodule deinit --all deinits all initialized submodules' '
+ git submodule update --init &&
+ git config submodule.example.foo bar &&
+ git config submodule.example2.frotz nitfol &&
+ test_must_fail git submodule deinit &&
+ git submodule deinit --all >actual &&
+ test -z "$(git config --get-regexp "submodule\.example\.")" &&
+ test -z "$(git config --get-regexp "submodule\.example2\.")" &&
+ test_grep "Cleared directory .init" actual &&
+ test_grep "Cleared directory .example2" actual &&
+ rmdir init example2
+'
+
+test_expect_success 'submodule deinit deinits a submodule when its work tree is missing or empty' '
+ git submodule update --init &&
+ rm -rf init example2/* example2/.git &&
+ git submodule deinit init example2 >actual &&
+ test -z "$(git config --get-regexp "submodule\.example\.")" &&
+ test -z "$(git config --get-regexp "submodule\.example2\.")" &&
+ test_grep ! "Cleared directory .init" actual &&
+ test_grep "Cleared directory .example2" actual &&
+ rmdir init
+'
+
+test_expect_success 'submodule deinit fails when the submodule contains modifications unless forced' '
+ git submodule update --init &&
+ echo X >>init/s &&
+ test_must_fail git submodule deinit init &&
+ test -n "$(git config --get-regexp "submodule\.example\.")" &&
+ test -f example2/.git &&
+ git submodule deinit -f init >actual &&
+ test -z "$(git config --get-regexp "submodule\.example\.")" &&
+ test_grep "Cleared directory .init" actual &&
+ rmdir init
+'
+
+test_expect_success 'submodule deinit fails when the submodule contains untracked files unless forced' '
+ git submodule update --init &&
+ echo X >>init/untracked &&
+ test_must_fail git submodule deinit init &&
+ test -n "$(git config --get-regexp "submodule\.example\.")" &&
+ test -f example2/.git &&
+ git submodule deinit -f init >actual &&
+ test -z "$(git config --get-regexp "submodule\.example\.")" &&
+ test_grep "Cleared directory .init" actual &&
+ rmdir init
+'
+
+test_expect_success 'submodule deinit fails when the submodule HEAD does not match unless forced' '
+ git submodule update --init &&
+ (
+ cd init &&
+ git checkout HEAD^
+ ) &&
+ test_must_fail git submodule deinit init &&
+ test -n "$(git config --get-regexp "submodule\.example\.")" &&
+ test -f example2/.git &&
+ git submodule deinit -f init >actual &&
+ test -z "$(git config --get-regexp "submodule\.example\.")" &&
+ test_grep "Cleared directory .init" actual &&
+ rmdir init
+'
+
+test_expect_success 'submodule deinit is silent when used on an uninitialized submodule' '
+ git submodule update --init &&
+ git submodule deinit init >actual &&
+ test_grep "Submodule .example. (.*) unregistered for path .init" actual &&
+ test_grep "Cleared directory .init" actual &&
+ git submodule deinit init >actual &&
+ test_grep ! "Submodule .example. (.*) unregistered for path .init" actual &&
+ test_grep "Cleared directory .init" actual &&
+ git submodule deinit . >actual &&
+ test_grep ! "Submodule .example. (.*) unregistered for path .init" actual &&
+ test_grep "Submodule .example2. (.*) unregistered for path .example2" actual &&
+ test_grep "Cleared directory .init" actual &&
+ git submodule deinit . >actual &&
+ test_grep ! "Submodule .example. (.*) unregistered for path .init" actual &&
+ test_grep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
+ test_grep "Cleared directory .init" actual &&
+ git submodule deinit --all >actual &&
+ test_grep ! "Submodule .example. (.*) unregistered for path .init" actual &&
+ test_grep ! "Submodule .example2. (.*) unregistered for path .example2" actual &&
+ test_grep "Cleared directory .init" actual &&
+ rmdir init example2
+'
+
+test_expect_success 'submodule deinit absorbs .git directory if .git is a directory' '
+ git submodule update --init &&
+ (
+ cd init &&
+ rm .git &&
+ mv ../.git/modules/example .git &&
+ GIT_WORK_TREE=. git config --unset core.worktree
+ ) &&
+ git submodule deinit init &&
+ test_path_is_missing init/.git &&
+ test -z "$(git config --get-regexp "submodule\.example\.")"
+'
+
+test_expect_success 'submodule with UTF-8 name' '
+ svname=$(printf "\303\245 \303\244\303\266") &&
+ mkdir "$svname" &&
+ (
+ cd "$svname" &&
+ git init &&
+ >sub &&
+ git add sub &&
+ git commit -m "init sub"
+ ) &&
+ git submodule add ./"$svname" &&
+ git submodule >&2 &&
+ test -n "$(git submodule | grep "$svname")"
+'
+
+test_expect_success 'submodule add clone shallow submodule' '
+ mkdir super &&
+ pwd=$(pwd) &&
+ (
+ cd super &&
+ git init &&
+ git submodule add --depth=1 file://"$pwd"/example2 submodule &&
+ (
+ cd submodule &&
+ test 1 = $(git log --oneline | wc -l)
+ )
+ )
+'
+
+test_expect_success 'setup superproject with submodules' '
+ git init sub1 &&
+ test_commit -C sub1 test &&
+ test_commit -C sub1 test2 &&
+ git init multisuper &&
+ git -C multisuper submodule add ../sub1 sub0 &&
+ git -C multisuper submodule add ../sub1 sub1 &&
+ git -C multisuper submodule add ../sub1 sub2 &&
+ git -C multisuper submodule add ../sub1 sub3 &&
+ git -C multisuper commit -m "add some submodules"
+'
+
+cat >expect <<-EOF
+-sub0
+ sub1 (test2)
+ sub2 (test2)
+ sub3 (test2)
+EOF
+
+test_expect_success 'submodule update --init with a specification' '
+ test_when_finished "rm -rf multisuper_clone" &&
+ pwd=$(pwd) &&
+ git clone file://"$pwd"/multisuper multisuper_clone &&
+ git -C multisuper_clone submodule update --init . ":(exclude)sub0" &&
+ git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'submodule update --init with submodule.active set' '
+ test_when_finished "rm -rf multisuper_clone" &&
+ pwd=$(pwd) &&
+ git clone file://"$pwd"/multisuper multisuper_clone &&
+ git -C multisuper_clone config submodule.active "." &&
+ git -C multisuper_clone config --add submodule.active ":(exclude)sub0" &&
+ git -C multisuper_clone submodule update --init &&
+ git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'submodule update and setting submodule.<name>.active' '
+ test_when_finished "rm -rf multisuper_clone" &&
+ pwd=$(pwd) &&
+ git clone file://"$pwd"/multisuper multisuper_clone &&
+ git -C multisuper_clone config --bool submodule.sub0.active "true" &&
+ git -C multisuper_clone config --bool submodule.sub1.active "false" &&
+ git -C multisuper_clone config --bool submodule.sub2.active "true" &&
+
+ cat >expect <<-\EOF &&
+ sub0 (test2)
+ -sub1
+ sub2 (test2)
+ -sub3
+ EOF
+ git -C multisuper_clone submodule update &&
+ git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'clone active submodule without submodule url set' '
+ test_when_finished "rm -rf test/test" &&
+ mkdir test &&
+ # another dir breaks accidental relative paths still being correct
+ git clone file://"$pwd"/multisuper test/test &&
+ (
+ cd test/test &&
+ git config submodule.active "." &&
+
+ # do not pass --init flag, as the submodule is already active:
+ git submodule update &&
+ git submodule status >actual_raw &&
+
+ cut -d" " -f3- actual_raw >actual &&
+ cat >expect <<-\EOF &&
+ sub0 (test2)
+ sub1 (test2)
+ sub2 (test2)
+ sub3 (test2)
+ EOF
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'update submodules without url set in .gitconfig' '
+ test_when_finished "rm -rf multisuper_clone" &&
+ git clone file://"$pwd"/multisuper multisuper_clone &&
+
+ git -C multisuper_clone submodule init &&
+ for s in sub0 sub1 sub2 sub3
+ do
+ key=submodule.$s.url &&
+ git -C multisuper_clone config --local --unset $key &&
+ git -C multisuper_clone config --file .gitmodules --unset $key || return 1
+ done &&
+
+ test_must_fail git -C multisuper_clone submodule update 2>err &&
+ grep "cannot clone submodule .sub[0-3]. without a URL" err
+'
+
+test_expect_success 'clone --recurse-submodules with a pathspec works' '
+ test_when_finished "rm -rf multisuper_clone" &&
+ cat >expected <<-\EOF &&
+ sub0 (test2)
+ -sub1
+ -sub2
+ -sub3
+ EOF
+
+ git clone --recurse-submodules="sub0" multisuper multisuper_clone &&
+ git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'clone with multiple --recurse-submodules options' '
+ test_when_finished "rm -rf multisuper_clone" &&
+ cat >expect <<-\EOF &&
+ -sub0
+ sub1 (test2)
+ -sub2
+ sub3 (test2)
+ EOF
+
+ git clone --recurse-submodules="." \
+ --recurse-submodules=":(exclude)sub0" \
+ --recurse-submodules=":(exclude)sub2" \
+ multisuper multisuper_clone &&
+ git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'clone and subsequent updates correctly auto-initialize submodules' '
+ test_when_finished "rm -rf multisuper_clone" &&
+ cat <<-\EOF >expect &&
+ -sub0
+ sub1 (test2)
+ -sub2
+ sub3 (test2)
+ EOF
+
+ cat <<-\EOF >expect2 &&
+ -sub0
+ sub1 (test2)
+ -sub2
+ sub3 (test2)
+ -sub4
+ sub5 (test2)
+ EOF
+
+ git clone --recurse-submodules="." \
+ --recurse-submodules=":(exclude)sub0" \
+ --recurse-submodules=":(exclude)sub2" \
+ --recurse-submodules=":(exclude)sub4" \
+ multisuper multisuper_clone &&
+
+ git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
+ test_cmp expect actual &&
+
+ git -C multisuper submodule add ../sub1 sub4 &&
+ git -C multisuper submodule add ../sub1 sub5 &&
+ git -C multisuper commit -m "add more submodules" &&
+ # obtain the new superproject
+ git -C multisuper_clone pull &&
+ git -C multisuper_clone submodule update --init &&
+ git -C multisuper_clone submodule status | sed "s/$OID_REGEX //" >actual &&
+ test_cmp expect2 actual
+'
+
+test_expect_success 'init properly sets the config' '
+ test_when_finished "rm -rf multisuper_clone" &&
+ git clone --recurse-submodules="." \
+ --recurse-submodules=":(exclude)sub0" \
+ multisuper multisuper_clone &&
+
+ git -C multisuper_clone submodule init -- sub0 sub1 &&
+ git -C multisuper_clone config --get submodule.sub0.active &&
+ test_must_fail git -C multisuper_clone config --get submodule.sub1.active
+'
+
+test_expect_success 'recursive clone respects -q' '
+ test_when_finished "rm -rf multisuper_clone" &&
+ git clone -q --recurse-submodules multisuper multisuper_clone >actual &&
+ test_must_be_empty actual
+'
+
+test_done
diff --git a/t/t7401-submodule-summary.sh b/t/t7401-submodule-summary.sh
new file mode 100755
index 0000000..542b333
--- /dev/null
+++ b/t/t7401-submodule-summary.sh
@@ -0,0 +1,316 @@
+#!/bin/sh
+#
+# Copyright (c) 2008 Ping Yin
+#
+
+test_description='Summary support for submodules
+
+This test script tries to verify the sanity of summary subcommand of git submodule.
+'
+
+# NOTE: This test script uses 'git add' instead of 'git submodule add' to add
+# submodules to the superproject. Some submodule subcommands such as init and
+# deinit might not work as expected in this script. t7421 does not have this
+# caveat.
+#
+# NEEDSWORK: This test script is old fashioned and may need a big cleanup due to
+# various reasons, one of them being that there are lots of commands taking place
+# outside of 'test_expect_success' block, which is no longer in good-style.
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+add_file () {
+ sm=$1
+ shift
+ owd=$(pwd)
+ cd "$sm"
+ for name; do
+ echo "$name" >"$name" &&
+ git add "$name" &&
+ test_tick &&
+ git commit -m "Add $name"
+ done >/dev/null
+ git rev-parse --short HEAD
+ cd "$owd"
+}
+commit_file () {
+ test_tick &&
+ git commit "$@" -m "Commit $*" >/dev/null
+}
+
+test_create_repo sm1 &&
+add_file . foo >/dev/null
+
+head1=$(add_file sm1 foo1 foo2)
+
+test_expect_success 'added submodule' "
+ git add sm1 &&
+ git submodule summary >actual &&
+ cat >expected <<-EOF &&
+ * sm1 0000000...$head1 (2):
+ > Add foo2
+
+ EOF
+ test_cmp expected actual
+"
+
+test_expect_success 'added submodule (subdirectory)' "
+ mkdir sub &&
+ (
+ cd sub &&
+ git submodule summary >../actual
+ ) &&
+ cat >expected <<-EOF &&
+ * ../sm1 0000000...$head1 (2):
+ > Add foo2
+
+ EOF
+ test_cmp expected actual
+"
+
+test_expect_success 'added submodule (subdirectory only)' "
+ (
+ cd sub &&
+ git submodule summary . >../actual
+ ) &&
+ test_must_be_empty actual
+"
+
+test_expect_success 'added submodule (subdirectory with explicit path)' "
+ (
+ cd sub &&
+ git submodule summary ../sm1 >../actual
+ ) &&
+ cat >expected <<-EOF &&
+ * ../sm1 0000000...$head1 (2):
+ > Add foo2
+
+ EOF
+ test_cmp expected actual
+"
+
+commit_file sm1 &&
+head2=$(add_file sm1 foo3)
+
+test_expect_success 'modified submodule(forward)' "
+ git submodule summary >actual &&
+ cat >expected <<-EOF &&
+ * sm1 $head1...$head2 (1):
+ > Add foo3
+
+ EOF
+ test_cmp expected actual
+"
+
+test_expect_success 'modified submodule(forward), --files' "
+ git submodule summary --files >actual &&
+ cat >expected <<-EOF &&
+ * sm1 $head1...$head2 (1):
+ > Add foo3
+
+ EOF
+ test_cmp expected actual
+"
+
+test_expect_success 'no ignore=all setting has any effect' "
+ git config -f .gitmodules submodule.sm1.path sm1 &&
+ git config -f .gitmodules submodule.sm1.ignore all &&
+ git config submodule.sm1.ignore all &&
+ git config diff.ignoreSubmodules all &&
+ git submodule summary >actual &&
+ cat >expected <<-EOF &&
+ * sm1 $head1...$head2 (1):
+ > Add foo3
+
+ EOF
+ test_cmp expected actual &&
+ git config --unset diff.ignoreSubmodules &&
+ git config --remove-section submodule.sm1 &&
+ git config -f .gitmodules --remove-section submodule.sm1
+"
+
+
+commit_file sm1 &&
+head3=$(
+ cd sm1 &&
+ git reset --hard HEAD~2 >/dev/null &&
+ git rev-parse --short HEAD
+)
+
+test_expect_success 'modified submodule(backward)' "
+ git submodule summary >actual &&
+ cat >expected <<-EOF &&
+ * sm1 $head2...$head3 (2):
+ < Add foo3
+ < Add foo2
+
+ EOF
+ test_cmp expected actual
+"
+
+head4=$(add_file sm1 foo4 foo5) &&
+head4_full=$(GIT_DIR=sm1/.git git rev-parse --verify HEAD)
+test_expect_success 'modified submodule(backward and forward)' "
+ git submodule summary >actual &&
+ cat >expected <<-EOF &&
+ * sm1 $head2...$head4 (4):
+ > Add foo5
+ > Add foo4
+ < Add foo3
+ < Add foo2
+
+ EOF
+ test_cmp expected actual
+"
+
+test_expect_success '--summary-limit' "
+ git submodule summary -n 3 >actual &&
+ cat >expected <<-EOF &&
+ * sm1 $head2...$head4 (4):
+ > Add foo5
+ > Add foo4
+ < Add foo3
+
+ EOF
+ test_cmp expected actual
+"
+
+commit_file sm1 &&
+mv sm1 sm1-bak &&
+echo sm1 >sm1 &&
+head5=$(git hash-object sm1 | cut -c1-7) &&
+git add sm1 &&
+rm -f sm1 &&
+mv sm1-bak sm1
+
+test_expect_success 'typechanged submodule(submodule->blob), --cached' "
+ git submodule summary --cached >actual &&
+ cat >expected <<-EOF &&
+ * sm1 $head4(submodule)->$head5(blob) (3):
+ < Add foo5
+
+ EOF
+ test_cmp expected actual
+"
+
+test_expect_success 'typechanged submodule(submodule->blob), --files' "
+ git submodule summary --files >actual &&
+ cat >expected <<-EOF &&
+ * sm1 $head5(blob)->$head4(submodule) (3):
+ > Add foo5
+
+ EOF
+ test_cmp expected actual
+"
+
+rm -rf sm1 &&
+git checkout-index sm1
+test_expect_success 'typechanged submodule(submodule->blob)' "
+ git submodule summary >actual &&
+ cat >expected <<-EOF &&
+ * sm1 $head4(submodule)->$head5(blob):
+
+ EOF
+ test_cmp expected actual
+"
+
+rm -f sm1 &&
+test_create_repo sm1 &&
+head6=$(add_file sm1 foo6 foo7)
+test_expect_success 'nonexistent commit' "
+ git submodule summary >actual &&
+ cat >expected <<-EOF &&
+ * sm1 $head4...$head6:
+ Warn: sm1 doesn't contain commit $head4_full
+
+ EOF
+ test_cmp expected actual
+"
+
+commit_file
+test_expect_success 'typechanged submodule(blob->submodule)' "
+ git submodule summary >actual &&
+ cat >expected <<-EOF &&
+ * sm1 $head5(blob)->$head6(submodule) (2):
+ > Add foo7
+
+ EOF
+ test_cmp expected actual
+"
+
+commit_file sm1 &&
+rm -rf sm1
+test_expect_success 'deleted submodule' "
+ git submodule summary >actual &&
+ cat >expected <<-EOF &&
+ * sm1 $head6...0000000:
+
+ EOF
+ test_cmp expected actual
+"
+
+test_expect_success 'create second submodule' '
+ test_create_repo sm2 &&
+ head7=$(add_file sm2 foo8 foo9) &&
+ git add sm2
+'
+
+test_expect_success 'multiple submodules' "
+ git submodule summary >actual &&
+ cat >expected <<-EOF &&
+ * sm1 $head6...0000000:
+
+ * sm2 0000000...$head7 (2):
+ > Add foo9
+
+ EOF
+ test_cmp expected actual
+"
+
+test_expect_success 'path filter' "
+ git submodule summary sm2 >actual &&
+ cat >expected <<-EOF &&
+ * sm2 0000000...$head7 (2):
+ > Add foo9
+
+ EOF
+ test_cmp expected actual
+"
+
+commit_file sm2
+test_expect_success 'given commit' "
+ git submodule summary HEAD^ >actual &&
+ cat >expected <<-EOF &&
+ * sm1 $head6...0000000:
+
+ * sm2 0000000...$head7 (2):
+ > Add foo9
+
+ EOF
+ test_cmp expected actual
+"
+
+test_expect_success '--for-status' "
+ git submodule summary --for-status HEAD^ >actual &&
+ test_cmp - actual <<-EOF
+ * sm1 $head6...0000000:
+
+ * sm2 0000000...$head7 (2):
+ > Add foo9
+
+ EOF
+"
+
+test_expect_success 'fail when using --files together with --cached' "
+ test_must_fail git submodule summary --files --cached
+"
+
+test_expect_success 'should not fail in an empty repo' "
+ git init xyzzy &&
+ cd xyzzy &&
+ git submodule summary >output 2>&1 &&
+ test_must_be_empty output
+"
+
+test_done
diff --git a/t/t7402-submodule-rebase.sh b/t/t7402-submodule-rebase.sh
new file mode 100755
index 0000000..2b3c363
--- /dev/null
+++ b/t/t7402-submodule-rebase.sh
@@ -0,0 +1,135 @@
+#!/bin/sh
+#
+# Copyright (c) 2008 Johannes Schindelin
+#
+
+test_description='Test rebasing, stashing, etc. with submodules'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+
+ echo file > file &&
+ git add file &&
+ test_tick &&
+ git commit -m initial &&
+ git clone . submodule &&
+ git add submodule &&
+ test_tick &&
+ git commit -m submodule &&
+ echo second line >> file &&
+ (cd submodule && git pull) &&
+ test_tick &&
+ git commit -m file-and-submodule -a &&
+ git branch added-submodule
+
+'
+
+test_expect_success 'rebase with a dirty submodule' '
+
+ (cd submodule &&
+ echo 3rd line >> file &&
+ test_tick &&
+ git commit -m fork -a) &&
+ echo unrelated >> file2 &&
+ git add file2 &&
+ test_tick &&
+ git commit -m unrelated file2 &&
+ echo other line >> file &&
+ test_tick &&
+ git commit -m update file &&
+ CURRENT=$(cd submodule && git rev-parse HEAD) &&
+ EXPECTED=$(git rev-parse HEAD~2:submodule) &&
+ GIT_TRACE=1 git rebase --onto HEAD~2 HEAD^ &&
+ STORED=$(git rev-parse HEAD:submodule) &&
+ test $EXPECTED = $STORED &&
+ test $CURRENT = $(cd submodule && git rev-parse HEAD)
+
+'
+
+cat > fake-editor.sh << \EOF
+#!/bin/sh
+echo $EDITOR_TEXT
+EOF
+chmod a+x fake-editor.sh
+
+test_expect_success 'interactive rebase with a dirty submodule' '
+
+ echo submodule >expect &&
+ git diff --name-only >actual &&
+ test_cmp expect actual &&
+ HEAD=$(git rev-parse HEAD) &&
+ GIT_EDITOR="\"$(pwd)/fake-editor.sh\"" EDITOR_TEXT="pick $HEAD" \
+ git rebase -i HEAD^ &&
+ echo submodule >expect &&
+ git diff --name-only >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'rebase with dirty file and submodule fails' '
+
+ echo yet another line >> file &&
+ test_tick &&
+ git commit -m next file &&
+ echo rewrite > file &&
+ test_tick &&
+ git commit -m rewrite file &&
+ echo dirty > file &&
+ test_must_fail git rebase --onto HEAD~2 HEAD^
+
+'
+
+test_expect_success 'stash with a dirty submodule' '
+
+ echo new > file &&
+ CURRENT=$(cd submodule && git rev-parse HEAD) &&
+ git stash &&
+ test new != $(cat file) &&
+ echo submodule >expect &&
+ git diff --name-only >actual &&
+ test_cmp expect actual &&
+
+ echo "$CURRENT" >expect &&
+ git -C submodule rev-parse HEAD >actual &&
+ test_cmp expect actual &&
+
+ git stash apply &&
+ test new = $(cat file) &&
+ echo "$CURRENT" >expect &&
+ git -C submodule rev-parse HEAD >actual &&
+ test_cmp expect actual
+
+'
+
+test_expect_success 'rebasing submodule that should conflict' '
+ git reset --hard &&
+ git checkout added-submodule &&
+ git add submodule &&
+ test_tick &&
+ git commit -m third &&
+ (
+ cd submodule &&
+ git commit --allow-empty -m extra
+ ) &&
+ git add submodule &&
+ test_tick &&
+ git commit -m fourth &&
+
+ test_must_fail git rebase --onto HEAD^^ HEAD^ HEAD^0 >actual_output &&
+ git ls-files -s submodule >actual &&
+ (
+ cd submodule &&
+ echo "160000 $(git rev-parse HEAD^) 1 submodule" &&
+ echo "160000 $(git rev-parse HEAD^^) 2 submodule" &&
+ echo "160000 $(git rev-parse HEAD) 3 submodule"
+ ) >expect &&
+ test_cmp expect actual &&
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ sub_expect="go to submodule (submodule), and either merge commit $(git -C submodule rev-parse --short HEAD^0)" &&
+ grep "$sub_expect" actual_output
+ fi
+'
+
+test_done
diff --git a/t/t7403-submodule-sync.sh b/t/t7403-submodule-sync.sh
new file mode 100755
index 0000000..19b6135
--- /dev/null
+++ b/t/t7403-submodule-sync.sh
@@ -0,0 +1,356 @@
+#!/bin/sh
+#
+# Copyright (c) 2008 David Aguilar
+#
+
+test_description='git submodule sync
+
+These tests exercise the "git submodule sync" subcommand.
+'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ git config --global protocol.file.allow always &&
+
+ echo file >file &&
+ git add file &&
+ test_tick &&
+ git commit -m upstream &&
+ git clone . super &&
+ git clone super submodule &&
+ (
+ cd submodule &&
+ git submodule add ../submodule sub-submodule &&
+ test_tick &&
+ git commit -m "sub-submodule"
+ ) &&
+ (
+ cd super &&
+ git submodule add ../submodule submodule &&
+ test_tick &&
+ git commit -m "submodule"
+ ) &&
+ git clone super super-clone &&
+ (
+ cd super-clone &&
+ git submodule update --init --recursive
+ ) &&
+ git clone super empty-clone &&
+ (
+ cd empty-clone &&
+ git submodule init
+ ) &&
+ git clone super top-only-clone &&
+ git clone super relative-clone &&
+ (
+ cd relative-clone &&
+ git submodule update --init --recursive
+ ) &&
+ git clone super recursive-clone &&
+ (
+ cd recursive-clone &&
+ git submodule update --init --recursive
+ )
+'
+
+test_expect_success 'change submodule' '
+ (
+ cd submodule &&
+ echo second line >>file &&
+ test_tick &&
+ git commit -a -m "change submodule"
+ )
+'
+
+reset_submodule_urls () {
+ (
+ root=$(pwd) &&
+ cd super-clone/submodule &&
+ git config remote.origin.url "$root/submodule"
+ ) &&
+ (
+ root=$(pwd) &&
+ cd super-clone/submodule/sub-submodule &&
+ git config remote.origin.url "$root/submodule"
+ )
+}
+
+test_expect_success 'change submodule url' '
+ (
+ cd super &&
+ cd submodule &&
+ git checkout main &&
+ git pull
+ ) &&
+ mv submodule moved-submodule &&
+ (
+ cd moved-submodule &&
+ git config -f .gitmodules submodule.sub-submodule.url ../moved-submodule &&
+ test_tick &&
+ git commit -a -m moved-sub-submodule
+ ) &&
+ (
+ cd super &&
+ git config -f .gitmodules submodule.submodule.url ../moved-submodule &&
+ test_tick &&
+ git commit -a -m moved-submodule
+ )
+'
+
+test_expect_success '"git submodule sync" should update submodule URLs' '
+ (
+ cd super-clone &&
+ git pull --no-recurse-submodules &&
+ git submodule sync
+ ) &&
+ test -d "$(
+ cd super-clone/submodule &&
+ git config remote.origin.url
+ )" &&
+ test ! -d "$(
+ cd super-clone/submodule/sub-submodule &&
+ git config remote.origin.url
+ )" &&
+ (
+ cd super-clone/submodule &&
+ git checkout main &&
+ git pull
+ ) &&
+ (
+ cd super-clone &&
+ test -d "$(git config submodule.submodule.url)"
+ )
+'
+
+test_expect_success '"git submodule sync --recursive" should update all submodule URLs' '
+ (
+ cd super-clone &&
+ (
+ cd submodule &&
+ git pull --no-recurse-submodules
+ ) &&
+ git submodule sync --recursive
+ ) &&
+ test -d "$(
+ cd super-clone/submodule &&
+ git config remote.origin.url
+ )" &&
+ test -d "$(
+ cd super-clone/submodule/sub-submodule &&
+ git config remote.origin.url
+ )" &&
+ (
+ cd super-clone/submodule/sub-submodule &&
+ git checkout main &&
+ git pull
+ )
+'
+
+test_expect_success 'reset submodule URLs' '
+ reset_submodule_urls super-clone
+'
+
+test_expect_success '"git submodule sync" should update submodule URLs - subdirectory' '
+ (
+ cd super-clone &&
+ git pull --no-recurse-submodules &&
+ mkdir -p sub &&
+ cd sub &&
+ git submodule sync >../../output
+ ) &&
+ test_grep "\\.\\./submodule" output &&
+ test -d "$(
+ cd super-clone/submodule &&
+ git config remote.origin.url
+ )" &&
+ test ! -d "$(
+ cd super-clone/submodule/sub-submodule &&
+ git config remote.origin.url
+ )" &&
+ (
+ cd super-clone/submodule &&
+ git checkout main &&
+ git pull
+ ) &&
+ (
+ cd super-clone &&
+ test -d "$(git config submodule.submodule.url)"
+ )
+'
+
+test_expect_success '"git submodule sync --recursive" should update all submodule URLs - subdirectory' '
+ (
+ cd super-clone &&
+ (
+ cd submodule &&
+ git pull --no-recurse-submodules
+ ) &&
+ mkdir -p sub &&
+ cd sub &&
+ git submodule sync --recursive >../../output
+ ) &&
+ test_grep "\\.\\./submodule/sub-submodule" output &&
+ test -d "$(
+ cd super-clone/submodule &&
+ git config remote.origin.url
+ )" &&
+ test -d "$(
+ cd super-clone/submodule/sub-submodule &&
+ git config remote.origin.url
+ )" &&
+ (
+ cd super-clone/submodule/sub-submodule &&
+ git checkout main &&
+ git pull
+ )
+'
+
+test_expect_success '"git submodule sync" should update known submodule URLs' '
+ (
+ cd empty-clone &&
+ git pull &&
+ git submodule sync &&
+ test -d "$(git config submodule.submodule.url)"
+ )
+'
+
+test_expect_success '"git submodule sync" should not vivify uninteresting submodule' '
+ (
+ cd top-only-clone &&
+ git pull &&
+ git submodule sync &&
+ test -z "$(git config submodule.submodule.url)" &&
+ git submodule sync submodule &&
+ test -z "$(git config submodule.submodule.url)"
+ )
+'
+
+test_expect_success '"git submodule sync" handles origin URL of the form foo' '
+ (
+ cd relative-clone &&
+ git remote set-url origin foo &&
+ git submodule sync &&
+ (
+ cd submodule &&
+ #actual fails with: "cannot strip off url foo
+ test "$(git config remote.origin.url)" = "../submodule"
+ )
+ )
+'
+
+test_expect_success '"git submodule sync" handles origin URL of the form foo/bar' '
+ (
+ cd relative-clone &&
+ git remote set-url origin foo/bar &&
+ git submodule sync &&
+ (
+ cd submodule &&
+ #actual foo/submodule
+ test "$(git config remote.origin.url)" = "../foo/submodule"
+ ) &&
+ (
+ cd submodule/sub-submodule &&
+ test "$(git config remote.origin.url)" != "../../foo/submodule"
+ )
+ )
+'
+
+test_expect_success '"git submodule sync --recursive" propagates changes in origin' '
+ (
+ cd recursive-clone &&
+ git remote set-url origin foo/bar &&
+ git submodule sync --recursive &&
+ (
+ cd submodule &&
+ #actual foo/submodule
+ test "$(git config remote.origin.url)" = "../foo/submodule"
+ ) &&
+ (
+ cd submodule/sub-submodule &&
+ test "$(git config remote.origin.url)" = "../../foo/submodule"
+ )
+ )
+'
+
+test_expect_success '"git submodule sync" handles origin URL of the form ./foo' '
+ (
+ cd relative-clone &&
+ git remote set-url origin ./foo &&
+ git submodule sync &&
+ (
+ cd submodule &&
+ #actual ./submodule
+ test "$(git config remote.origin.url)" = "../submodule"
+ )
+ )
+'
+
+test_expect_success '"git submodule sync" handles origin URL of the form ./foo/bar' '
+ (
+ cd relative-clone &&
+ git remote set-url origin ./foo/bar &&
+ git submodule sync &&
+ (
+ cd submodule &&
+ #actual ./foo/submodule
+ test "$(git config remote.origin.url)" = "../foo/submodule"
+ )
+ )
+'
+
+test_expect_success '"git submodule sync" handles origin URL of the form ../foo' '
+ (
+ cd relative-clone &&
+ git remote set-url origin ../foo &&
+ git submodule sync &&
+ (
+ cd submodule &&
+ #actual ../submodule
+ test "$(git config remote.origin.url)" = "../../submodule"
+ )
+ )
+'
+
+test_expect_success '"git submodule sync" handles origin URL of the form ../foo/bar' '
+ (
+ cd relative-clone &&
+ git remote set-url origin ../foo/bar &&
+ git submodule sync &&
+ (
+ cd submodule &&
+ #actual ../foo/submodule
+ test "$(git config remote.origin.url)" = "../../foo/submodule"
+ )
+ )
+'
+
+test_expect_success '"git submodule sync" handles origin URL of the form ../foo/bar with deeply nested submodule' '
+ (
+ cd relative-clone &&
+ git remote set-url origin ../foo/bar &&
+ mkdir -p a/b/c &&
+ (
+ cd a/b/c &&
+ git init &&
+ >.gitignore &&
+ git add .gitignore &&
+ test_tick &&
+ git commit -m "initial commit"
+ ) &&
+ git submodule add ../bar/a/b/c ./a/b/c &&
+ git submodule sync &&
+ (
+ cd a/b/c &&
+ #actual ../foo/bar/a/b/c
+ test "$(git config remote.origin.url)" = "../../../../foo/bar/a/b/c"
+ )
+ )
+'
+
+
+test_done
diff --git a/t/t7406-submodule-update.sh b/t/t7406-submodule-update.sh
new file mode 100755
index 0000000..8491b8c
--- /dev/null
+++ b/t/t7406-submodule-update.sh
@@ -0,0 +1,1205 @@
+#!/bin/sh
+#
+# Copyright (c) 2009 Red Hat, Inc.
+#
+
+test_description='Test updating submodules
+
+This test verifies that "git submodule update" detaches the HEAD of the
+submodule and "git submodule update --rebase/--merge" does not detach the HEAD.
+'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+
+compare_head()
+{
+ sha_main=$(git rev-list --max-count=1 main)
+ sha_head=$(git rev-list --max-count=1 HEAD)
+
+ test "$sha_main" = "$sha_head"
+}
+
+
+test_expect_success 'setup a submodule tree' '
+ git config --global protocol.file.allow always &&
+ echo file > file &&
+ git add file &&
+ test_tick &&
+ git commit -m upstream &&
+ git clone . super &&
+ git clone super submodule &&
+ git clone super rebasing &&
+ git clone super merging &&
+ git clone super none &&
+ (cd super &&
+ git submodule add ../submodule submodule &&
+ test_tick &&
+ git commit -m "submodule" &&
+ git submodule init submodule
+ ) &&
+ (cd submodule &&
+ echo "line2" > file &&
+ git add file &&
+ git commit -m "Commit 2"
+ ) &&
+ (cd super &&
+ (cd submodule &&
+ git pull --rebase origin
+ ) &&
+ git add submodule &&
+ git commit -m "submodule update"
+ ) &&
+ (cd super &&
+ git submodule add ../rebasing rebasing &&
+ test_tick &&
+ git commit -m "rebasing"
+ ) &&
+ (cd super &&
+ git submodule add ../merging merging &&
+ test_tick &&
+ git commit -m "rebasing"
+ ) &&
+ (cd super &&
+ git submodule add ../none none &&
+ test_tick &&
+ git commit -m "none"
+ ) &&
+ git clone . recursivesuper &&
+ ( cd recursivesuper &&
+ git submodule add ../super super
+ )
+'
+
+test_expect_success 'update --remote falls back to using HEAD' '
+ test_create_repo main-branch-submodule &&
+ test_commit -C main-branch-submodule initial &&
+
+ test_create_repo main-branch &&
+ git -C main-branch submodule add ../main-branch-submodule &&
+ git -C main-branch commit -m add-submodule &&
+
+ git -C main-branch-submodule switch -c hello &&
+ test_commit -C main-branch-submodule world &&
+
+ git clone --recursive main-branch main-branch-clone &&
+ git -C main-branch-clone submodule update --remote main-branch-submodule &&
+ test_path_exists main-branch-clone/main-branch-submodule/world.t
+'
+
+test_expect_success 'submodule update detaching the HEAD ' '
+ (cd super/submodule &&
+ git reset --hard HEAD~1
+ ) &&
+ (cd super &&
+ (cd submodule &&
+ compare_head
+ ) &&
+ git submodule update submodule &&
+ cd submodule &&
+ ! compare_head
+ )
+'
+
+test_expect_success 'submodule update from subdirectory' '
+ (cd super/submodule &&
+ git reset --hard HEAD~1
+ ) &&
+ mkdir super/sub &&
+ (cd super/sub &&
+ (cd ../submodule &&
+ compare_head
+ ) &&
+ git submodule update ../submodule &&
+ cd ../submodule &&
+ ! compare_head
+ )
+'
+
+supersha1=$(git -C super rev-parse HEAD)
+mergingsha1=$(git -C super/merging rev-parse HEAD)
+nonesha1=$(git -C super/none rev-parse HEAD)
+rebasingsha1=$(git -C super/rebasing rev-parse HEAD)
+submodulesha1=$(git -C super/submodule rev-parse HEAD)
+pwd=$(pwd)
+
+cat <<EOF >expect
+Submodule path '../super': checked out '$supersha1'
+Submodule path '../super/merging': checked out '$mergingsha1'
+Submodule path '../super/none': checked out '$nonesha1'
+Submodule path '../super/rebasing': checked out '$rebasingsha1'
+Submodule path '../super/submodule': checked out '$submodulesha1'
+EOF
+
+cat <<EOF >expect2
+Cloning into '$pwd/recursivesuper/super/merging'...
+Cloning into '$pwd/recursivesuper/super/none'...
+Cloning into '$pwd/recursivesuper/super/rebasing'...
+Cloning into '$pwd/recursivesuper/super/submodule'...
+Submodule 'merging' ($pwd/merging) registered for path '../super/merging'
+Submodule 'none' ($pwd/none) registered for path '../super/none'
+Submodule 'rebasing' ($pwd/rebasing) registered for path '../super/rebasing'
+Submodule 'submodule' ($pwd/submodule) registered for path '../super/submodule'
+done.
+done.
+done.
+done.
+EOF
+
+test_expect_success 'submodule update --init --recursive from subdirectory' '
+ git -C recursivesuper/super reset --hard HEAD^ &&
+ (cd recursivesuper &&
+ mkdir tmp &&
+ cd tmp &&
+ git submodule update --init --recursive ../super >../../actual 2>../../actual2
+ ) &&
+ test_cmp expect actual &&
+ sort actual2 >actual2.sorted &&
+ test_cmp expect2 actual2.sorted
+'
+
+cat <<EOF >expect2
+Submodule 'foo/sub' ($pwd/withsubs/../rebasing) registered for path 'sub'
+EOF
+
+test_expect_success 'submodule update --init from and of subdirectory' '
+ git init withsubs &&
+ (cd withsubs &&
+ mkdir foo &&
+ git submodule add "$(pwd)/../rebasing" foo/sub &&
+ (cd foo &&
+ git submodule deinit -f sub &&
+ git submodule update --init sub 2>../../actual2
+ )
+ ) &&
+ test_cmp expect2 actual2
+'
+
+test_expect_success 'submodule update does not fetch already present commits' '
+ (cd submodule &&
+ echo line3 >> file &&
+ git add file &&
+ test_tick &&
+ git commit -m "upstream line3"
+ ) &&
+ (cd super/submodule &&
+ head=$(git rev-parse --verify HEAD) &&
+ echo "Submodule path ${SQ}submodule$SQ: checked out $SQ$head$SQ" > ../../expected &&
+ git reset --hard HEAD~1
+ ) &&
+ (cd super &&
+ git submodule update > ../actual 2> ../actual.err
+ ) &&
+ test_cmp expected actual &&
+ test_must_be_empty actual.err
+'
+
+test_expect_success 'submodule update should fail due to local changes' '
+ (cd super/submodule &&
+ git reset --hard HEAD~1 &&
+ echo "local change" > file
+ ) &&
+ (cd super &&
+ (cd submodule &&
+ compare_head
+ ) &&
+ test_must_fail git submodule update submodule 2>../actual.raw
+ ) &&
+ sed "s/^> //" >expect <<-\EOF &&
+ > error: Your local changes to the following files would be overwritten by checkout:
+ > file
+ > Please commit your changes or stash them before you switch branches.
+ > Aborting
+ > fatal: Unable to checkout OID in submodule path '\''submodule'\''
+ EOF
+ sed -e "s/checkout $SQ[^$SQ]*$SQ/checkout OID/" <actual.raw >actual &&
+ test_cmp expect actual
+
+'
+test_expect_success 'submodule update should throw away changes with --force ' '
+ (cd super &&
+ (cd submodule &&
+ compare_head
+ ) &&
+ git submodule update --force submodule &&
+ cd submodule &&
+ ! compare_head
+ )
+'
+
+test_expect_success 'submodule update --force forcibly checks out submodules' '
+ (cd super &&
+ (cd submodule &&
+ rm -f file
+ ) &&
+ git submodule update --force submodule &&
+ (cd submodule &&
+ test "$(git status -s file)" = ""
+ )
+ )
+'
+
+test_expect_success 'submodule update --remote should fetch upstream changes' '
+ (cd submodule &&
+ echo line4 >> file &&
+ git add file &&
+ test_tick &&
+ git commit -m "upstream line4"
+ ) &&
+ (cd super &&
+ git submodule update --remote --force submodule &&
+ cd submodule &&
+ test "$(git log -1 --oneline)" = "$(GIT_DIR=../../submodule/.git git log -1 --oneline)"
+ )
+'
+
+test_expect_success 'submodule update --remote should fetch upstream changes with .' '
+ (
+ cd super &&
+ git config -f .gitmodules submodule."submodule".branch "." &&
+ git add .gitmodules &&
+ git commit -m "submodules: update from the respective superproject branch"
+ ) &&
+ (
+ cd submodule &&
+ echo line4a >> file &&
+ git add file &&
+ test_tick &&
+ git commit -m "upstream line4a" &&
+ git checkout -b test-branch &&
+ test_commit on-test-branch
+ ) &&
+ (
+ cd super &&
+ git submodule update --remote --force submodule &&
+ git -C submodule log -1 --oneline >actual &&
+ git -C ../submodule log -1 --oneline main >expect &&
+ test_cmp expect actual &&
+ git checkout -b test-branch &&
+ git submodule update --remote --force submodule &&
+ git -C submodule log -1 --oneline >actual &&
+ git -C ../submodule log -1 --oneline test-branch >expect &&
+ test_cmp expect actual &&
+ git checkout main &&
+ git branch -d test-branch &&
+ git reset --hard HEAD^
+ )
+'
+
+test_expect_success 'local config should override .gitmodules branch' '
+ (cd submodule &&
+ git checkout test-branch &&
+ echo line5 >> file &&
+ git add file &&
+ test_tick &&
+ git commit -m "upstream line5" &&
+ git checkout main
+ ) &&
+ (cd super &&
+ git config submodule.submodule.branch test-branch &&
+ git submodule update --remote --force submodule &&
+ cd submodule &&
+ test "$(git log -1 --oneline)" = "$(GIT_DIR=../../submodule/.git git log -1 --oneline test-branch)"
+ )
+'
+
+test_expect_success 'submodule update --rebase staying on main' '
+ (cd super/submodule &&
+ git checkout main
+ ) &&
+ (cd super &&
+ (cd submodule &&
+ compare_head
+ ) &&
+ git submodule update --rebase submodule &&
+ cd submodule &&
+ compare_head
+ )
+'
+
+test_expect_success 'submodule update --merge staying on main' '
+ (cd super/submodule &&
+ git reset --hard HEAD~1
+ ) &&
+ (cd super &&
+ (cd submodule &&
+ compare_head
+ ) &&
+ git submodule update --merge submodule &&
+ cd submodule &&
+ compare_head
+ )
+'
+
+test_expect_success 'submodule update - rebase in .git/config' '
+ (cd super &&
+ git config submodule.submodule.update rebase
+ ) &&
+ (cd super/submodule &&
+ git reset --hard HEAD~1
+ ) &&
+ (cd super &&
+ (cd submodule &&
+ compare_head
+ ) &&
+ git submodule update submodule &&
+ cd submodule &&
+ compare_head
+ )
+'
+
+test_expect_success 'submodule update - checkout in .git/config but --rebase given' '
+ (cd super &&
+ git config submodule.submodule.update checkout
+ ) &&
+ (cd super/submodule &&
+ git reset --hard HEAD~1
+ ) &&
+ (cd super &&
+ (cd submodule &&
+ compare_head
+ ) &&
+ git submodule update --rebase submodule &&
+ cd submodule &&
+ compare_head
+ )
+'
+
+test_expect_success 'submodule update - merge in .git/config' '
+ (cd super &&
+ git config submodule.submodule.update merge
+ ) &&
+ (cd super/submodule &&
+ git reset --hard HEAD~1
+ ) &&
+ (cd super &&
+ (cd submodule &&
+ compare_head
+ ) &&
+ git submodule update submodule &&
+ cd submodule &&
+ compare_head
+ )
+'
+
+test_expect_success 'submodule update - checkout in .git/config but --merge given' '
+ (cd super &&
+ git config submodule.submodule.update checkout
+ ) &&
+ (cd super/submodule &&
+ git reset --hard HEAD~1
+ ) &&
+ (cd super &&
+ (cd submodule &&
+ compare_head
+ ) &&
+ git submodule update --merge submodule &&
+ cd submodule &&
+ compare_head
+ )
+'
+
+test_expect_success 'submodule update - checkout in .git/config' '
+ (cd super &&
+ git config submodule.submodule.update checkout
+ ) &&
+ (cd super/submodule &&
+ git reset --hard HEAD^
+ ) &&
+ (cd super &&
+ (cd submodule &&
+ compare_head
+ ) &&
+ git submodule update submodule &&
+ cd submodule &&
+ ! compare_head
+ )
+'
+
+test_expect_success 'submodule update - command in .git/config' '
+ (cd super &&
+ git config submodule.submodule.update "!git checkout"
+ ) &&
+ (cd super/submodule &&
+ git reset --hard HEAD^
+ ) &&
+ (cd super &&
+ (cd submodule &&
+ compare_head
+ ) &&
+ git submodule update submodule &&
+ cd submodule &&
+ ! compare_head
+ )
+'
+
+test_expect_success 'submodule update - command in .gitmodules is rejected' '
+ test_when_finished "git -C super reset --hard HEAD^" &&
+ git -C super config -f .gitmodules submodule.submodule.update "!false" &&
+ git -C super commit -a -m "add command to .gitmodules file" &&
+ git -C super/submodule reset --hard $submodulesha1^ &&
+ test_must_fail git -C super submodule update submodule
+'
+
+test_expect_success 'fsck detects command in .gitmodules' '
+ git init command-in-gitmodules &&
+ (
+ cd command-in-gitmodules &&
+ git submodule add ../submodule submodule &&
+ test_commit adding-submodule &&
+
+ git config -f .gitmodules submodule.submodule.update "!false" &&
+ git add .gitmodules &&
+ test_commit configuring-update &&
+ test_must_fail git fsck
+ )
+'
+
+cat << EOF >expect
+fatal: Execution of 'false $submodulesha1' failed in submodule path 'submodule'
+EOF
+
+test_expect_success 'submodule update - command in .git/config catches failure' '
+ (cd super &&
+ git config submodule.submodule.update "!false"
+ ) &&
+ (cd super/submodule &&
+ git reset --hard $submodulesha1^
+ ) &&
+ (cd super &&
+ test_must_fail git submodule update submodule 2>../actual
+ ) &&
+ test_cmp actual expect
+'
+
+cat << EOF >expect
+fatal: Execution of 'false $submodulesha1' failed in submodule path '../submodule'
+EOF
+
+test_expect_success 'submodule update - command in .git/config catches failure -- subdirectory' '
+ (cd super &&
+ git config submodule.submodule.update "!false"
+ ) &&
+ (cd super/submodule &&
+ git reset --hard $submodulesha1^
+ ) &&
+ (cd super &&
+ mkdir tmp && cd tmp &&
+ test_must_fail git submodule update ../submodule 2>../../actual
+ ) &&
+ test_cmp actual expect
+'
+
+test_expect_success 'submodule update - command run for initial population of submodule' '
+ cat >expect <<-EOF &&
+ fatal: Execution of '\''false $submodulesha1'\'' failed in submodule path '\''submodule'\''
+ EOF
+ rm -rf super/submodule &&
+ test_must_fail git -C super submodule update 2>actual &&
+ test_cmp expect actual &&
+ git -C super submodule update --checkout
+'
+
+cat << EOF >expect
+fatal: Execution of 'false $submodulesha1' failed in submodule path '../super/submodule'
+fatal: Failed to recurse into submodule path '../super'
+EOF
+
+test_expect_success 'recursive submodule update - command in .git/config catches failure -- subdirectory' '
+ (cd recursivesuper &&
+ git submodule update --remote super &&
+ git add super &&
+ git commit -m "update to latest to have more than one commit in submodules"
+ ) &&
+ git -C recursivesuper/super config submodule.submodule.update "!false" &&
+ git -C recursivesuper/super/submodule reset --hard $submodulesha1^ &&
+ (cd recursivesuper &&
+ mkdir -p tmp && cd tmp &&
+ test_must_fail git submodule update --recursive ../super 2>../../actual
+ ) &&
+ test_cmp actual expect
+'
+
+test_expect_success 'submodule init does not copy command into .git/config' '
+ test_when_finished "git -C super update-index --force-remove submodule1" &&
+ test_when_finished git config -f super/.gitmodules \
+ --remove-section submodule.submodule1 &&
+ (cd super &&
+ git ls-files -s submodule >out &&
+ H=$(cut -d" " -f2 out) &&
+ mkdir submodule1 &&
+ git update-index --add --cacheinfo 160000 $H submodule1 &&
+ git config -f .gitmodules submodule.submodule1.path submodule1 &&
+ git config -f .gitmodules submodule.submodule1.url ../submodule &&
+ git config -f .gitmodules submodule.submodule1.update !false &&
+ test_must_fail git submodule init submodule1 &&
+ test_expect_code 1 git config submodule.submodule1.update >actual &&
+ test_must_be_empty actual
+ )
+'
+
+test_expect_success 'submodule init picks up rebase' '
+ (cd super &&
+ git config -f .gitmodules submodule.rebasing.update rebase &&
+ git submodule init rebasing &&
+ test "rebase" = "$(git config submodule.rebasing.update)"
+ )
+'
+
+test_expect_success 'submodule init picks up merge' '
+ (cd super &&
+ git config -f .gitmodules submodule.merging.update merge &&
+ git submodule init merging &&
+ test "merge" = "$(git config submodule.merging.update)"
+ )
+'
+
+test_expect_success 'submodule update --merge - ignores --merge for new submodules' '
+ test_config -C super submodule.submodule.update checkout &&
+ (cd super &&
+ rm -rf submodule &&
+ git submodule update submodule &&
+ git status -s submodule >expect &&
+ rm -rf submodule &&
+ git submodule update --merge submodule &&
+ git status -s submodule >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'submodule update --rebase - ignores --rebase for new submodules' '
+ test_config -C super submodule.submodule.update checkout &&
+ (cd super &&
+ rm -rf submodule &&
+ git submodule update submodule &&
+ git status -s submodule >expect &&
+ rm -rf submodule &&
+ git submodule update --rebase submodule &&
+ git status -s submodule >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'submodule update ignores update=merge config for new submodules' '
+ (cd super &&
+ rm -rf submodule &&
+ git submodule update submodule &&
+ git status -s submodule >expect &&
+ rm -rf submodule &&
+ git config submodule.submodule.update merge &&
+ git submodule update submodule &&
+ git status -s submodule >actual &&
+ git config --unset submodule.submodule.update &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'submodule update ignores update=rebase config for new submodules' '
+ (cd super &&
+ rm -rf submodule &&
+ git submodule update submodule &&
+ git status -s submodule >expect &&
+ rm -rf submodule &&
+ git config submodule.submodule.update rebase &&
+ git submodule update submodule &&
+ git status -s submodule >actual &&
+ git config --unset submodule.submodule.update &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'submodule init picks up update=none' '
+ (cd super &&
+ git config -f .gitmodules submodule.none.update none &&
+ git submodule init none &&
+ test "none" = "$(git config submodule.none.update)"
+ )
+'
+
+test_expect_success 'submodule update - update=none in .git/config' '
+ (cd super &&
+ git config submodule.submodule.update none &&
+ (cd submodule &&
+ git checkout main &&
+ compare_head
+ ) &&
+ git diff --name-only >out &&
+ grep ^submodule$ out &&
+ git submodule update &&
+ git diff --name-only >out &&
+ grep ^submodule$ out &&
+ (cd submodule &&
+ compare_head
+ ) &&
+ git config --unset submodule.submodule.update &&
+ git submodule update submodule
+ )
+'
+
+test_expect_success 'submodule update - update=none in .git/config but --checkout given' '
+ (cd super &&
+ git config submodule.submodule.update none &&
+ (cd submodule &&
+ git checkout main &&
+ compare_head
+ ) &&
+ git diff --name-only >out &&
+ grep ^submodule$ out &&
+ git submodule update --checkout &&
+ git diff --name-only >out &&
+ ! grep ^submodule$ out &&
+ (cd submodule &&
+ ! compare_head
+ ) &&
+ git config --unset submodule.submodule.update
+ )
+'
+
+test_expect_success 'submodule update --init skips submodule with update=none' '
+ (cd super &&
+ git add .gitmodules &&
+ git commit -m ".gitmodules"
+ ) &&
+ git clone super cloned &&
+ (cd cloned &&
+ git submodule update --init &&
+ test_path_exists submodule/.git &&
+ test_path_is_missing none/.git
+ )
+'
+
+test_expect_success 'submodule update with pathspec warns against uninitialized ones' '
+ test_when_finished "rm -fr selective" &&
+ git clone super selective &&
+ (
+ cd selective &&
+ git submodule init submodule &&
+
+ git submodule update submodule 2>err &&
+ ! grep "Submodule path .* not initialized" err &&
+
+ git submodule update rebasing 2>err &&
+ grep "Submodule path .rebasing. not initialized" err &&
+
+ test_path_exists submodule/.git &&
+ test_path_is_missing rebasing/.git
+ )
+
+'
+
+test_expect_success 'submodule update without pathspec updates only initialized ones' '
+ test_when_finished "rm -fr selective" &&
+ git clone super selective &&
+ (
+ cd selective &&
+ git submodule init submodule &&
+ git submodule update 2>err &&
+ test_path_exists submodule/.git &&
+ test_path_is_missing rebasing/.git &&
+ ! grep "Submodule path .* not initialized" err
+ )
+
+'
+
+test_expect_success 'submodule update continues after checkout error' '
+ (cd super &&
+ git reset --hard HEAD &&
+ git submodule add ../submodule submodule2 &&
+ git submodule init &&
+ git commit -am "new_submodule" &&
+ (cd submodule2 &&
+ git rev-parse --verify HEAD >../expect
+ ) &&
+ (cd submodule &&
+ test_commit "update_submodule" file
+ ) &&
+ (cd submodule2 &&
+ test_commit "update_submodule2" file
+ ) &&
+ git add submodule &&
+ git add submodule2 &&
+ git commit -m "two_new_submodule_commits" &&
+ (cd submodule &&
+ echo "" > file
+ ) &&
+ git checkout HEAD^ &&
+ test_must_fail git submodule update &&
+ (cd submodule2 &&
+ git rev-parse --verify HEAD >../actual
+ ) &&
+ test_cmp expect actual
+ )
+'
+test_expect_success 'submodule update continues after recursive checkout error' '
+ (cd super &&
+ git reset --hard HEAD &&
+ git checkout main &&
+ git submodule update &&
+ (cd submodule &&
+ git submodule add ../submodule subsubmodule &&
+ git submodule init &&
+ git commit -m "new_subsubmodule"
+ ) &&
+ git add submodule &&
+ git commit -m "update_submodule" &&
+ (cd submodule &&
+ (cd subsubmodule &&
+ test_commit "update_subsubmodule" file
+ ) &&
+ git add subsubmodule &&
+ test_commit "update_submodule_again" file &&
+ (cd subsubmodule &&
+ test_commit "update_subsubmodule_again" file
+ ) &&
+ test_commit "update_submodule_again_again" file
+ ) &&
+ (cd submodule2 &&
+ git rev-parse --verify HEAD >../expect &&
+ test_commit "update_submodule2_again" file
+ ) &&
+ git add submodule &&
+ git add submodule2 &&
+ git commit -m "new_commits" &&
+ git checkout HEAD^ &&
+ (cd submodule &&
+ git checkout HEAD^ &&
+ (cd subsubmodule &&
+ echo "" > file
+ )
+ ) &&
+ test_expect_code 1 git submodule update --recursive &&
+ (cd submodule2 &&
+ git rev-parse --verify HEAD >../actual
+ ) &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'submodule update exit immediately in case of merge conflict' '
+ (cd super &&
+ git checkout main &&
+ git reset --hard HEAD &&
+ (cd submodule &&
+ (cd subsubmodule &&
+ git reset --hard HEAD
+ )
+ ) &&
+ git submodule update --recursive &&
+ (cd submodule &&
+ test_commit "update_submodule_2" file
+ ) &&
+ (cd submodule2 &&
+ test_commit "update_submodule2_2" file
+ ) &&
+ git add submodule &&
+ git add submodule2 &&
+ git commit -m "two_new_submodule_commits" &&
+ (cd submodule &&
+ git checkout main &&
+ test_commit "conflict" file &&
+ echo "conflict" > file
+ ) &&
+ git checkout HEAD^ &&
+ (cd submodule2 &&
+ git rev-parse --verify HEAD >../expect
+ ) &&
+ git config submodule.submodule.update merge &&
+ test_must_fail git submodule update &&
+ (cd submodule2 &&
+ git rev-parse --verify HEAD >../actual
+ ) &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'submodule update exit immediately after recursive rebase error' '
+ (cd super &&
+ git checkout main &&
+ git reset --hard HEAD &&
+ (cd submodule &&
+ git reset --hard HEAD &&
+ git submodule update --recursive
+ ) &&
+ (cd submodule &&
+ test_commit "update_submodule_3" file
+ ) &&
+ (cd submodule2 &&
+ test_commit "update_submodule2_3" file
+ ) &&
+ git add submodule &&
+ git add submodule2 &&
+ git commit -m "two_new_submodule_commits" &&
+ (cd submodule &&
+ git checkout main &&
+ test_commit "conflict2" file &&
+ echo "conflict" > file
+ ) &&
+ git checkout HEAD^ &&
+ (cd submodule2 &&
+ git rev-parse --verify HEAD >../expect
+ ) &&
+ git config submodule.submodule.update rebase &&
+ test_must_fail git submodule update &&
+ (cd submodule2 &&
+ git rev-parse --verify HEAD >../actual
+ ) &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'add different submodules to the same path' '
+ (cd super &&
+ git submodule add ../submodule s1 &&
+ test_must_fail git submodule add ../merging s1
+ )
+'
+
+test_expect_success 'submodule add places git-dir in superprojects git-dir' '
+ (cd super &&
+ mkdir deeper &&
+ git submodule add ../submodule deeper/submodule &&
+ (cd deeper/submodule &&
+ git log > ../../expected
+ ) &&
+ (cd .git/modules/deeper/submodule &&
+ git log > ../../../../actual
+ ) &&
+ test_cmp expected actual
+ )
+'
+
+test_expect_success 'submodule update places git-dir in superprojects git-dir' '
+ (cd super &&
+ git commit -m "added submodule"
+ ) &&
+ git clone super super2 &&
+ (cd super2 &&
+ git submodule init deeper/submodule &&
+ git submodule update &&
+ (cd deeper/submodule &&
+ git log > ../../expected
+ ) &&
+ (cd .git/modules/deeper/submodule &&
+ git log > ../../../../actual
+ ) &&
+ test_cmp expected actual
+ )
+'
+
+test_expect_success 'submodule add places git-dir in superprojects git-dir recursive' '
+ (cd super2 &&
+ (cd deeper/submodule &&
+ git submodule add ../submodule subsubmodule &&
+ (cd subsubmodule &&
+ git log > ../../../expected
+ ) &&
+ git commit -m "added subsubmodule" &&
+ git push origin :
+ ) &&
+ (cd .git/modules/deeper/submodule/modules/subsubmodule &&
+ git log > ../../../../../actual
+ ) &&
+ git add deeper/submodule &&
+ git commit -m "update submodule" &&
+ git push origin : &&
+ test_cmp expected actual
+ )
+'
+
+test_expect_success 'submodule update places git-dir in superprojects git-dir recursive' '
+ mkdir super_update_r &&
+ (cd super_update_r &&
+ git init --bare
+ ) &&
+ mkdir subsuper_update_r &&
+ (cd subsuper_update_r &&
+ git init --bare
+ ) &&
+ mkdir subsubsuper_update_r &&
+ (cd subsubsuper_update_r &&
+ git init --bare
+ ) &&
+ git clone subsubsuper_update_r subsubsuper_update_r2 &&
+ (cd subsubsuper_update_r2 &&
+ test_commit "update_subsubsuper" file &&
+ git push origin main
+ ) &&
+ git clone subsuper_update_r subsuper_update_r2 &&
+ (cd subsuper_update_r2 &&
+ test_commit "update_subsuper" file &&
+ git submodule add ../subsubsuper_update_r subsubmodule &&
+ git commit -am "subsubmodule" &&
+ git push origin main
+ ) &&
+ git clone super_update_r super_update_r2 &&
+ (cd super_update_r2 &&
+ test_commit "update_super" file &&
+ git submodule add ../subsuper_update_r submodule &&
+ git commit -am "submodule" &&
+ git push origin main
+ ) &&
+ rm -rf super_update_r2 &&
+ git clone super_update_r super_update_r2 &&
+ (cd super_update_r2 &&
+ git submodule update --init --recursive >actual &&
+ test_grep "Submodule path .submodule/subsubmodule.: checked out" actual &&
+ (cd submodule/subsubmodule &&
+ git log > ../../expected
+ ) &&
+ (cd .git/modules/submodule/modules/subsubmodule &&
+ git log > ../../../../../actual
+ ) &&
+ test_cmp expected actual
+ )
+'
+
+test_expect_success 'submodule add properly re-creates deeper level submodules' '
+ (cd super &&
+ git reset --hard main &&
+ rm -rf deeper/ &&
+ git submodule add --force ../submodule deeper/submodule
+ )
+'
+
+test_expect_success 'submodule update properly revives a moved submodule' '
+ (cd super &&
+ H=$(git rev-parse --short HEAD) &&
+ git commit -am "pre move" &&
+ H2=$(git rev-parse --short HEAD) &&
+ git status >out &&
+ sed "s/$H/XXX/" out >expect &&
+ H=$(cd submodule2 && git rev-parse HEAD) &&
+ git rm --cached submodule2 &&
+ rm -rf submodule2 &&
+ mkdir -p "moved/sub module" &&
+ git update-index --add --cacheinfo 160000 $H "moved/sub module" &&
+ git config -f .gitmodules submodule.submodule2.path "moved/sub module" &&
+ git commit -am "post move" &&
+ git submodule update &&
+ git status > out &&
+ sed "s/$H2/XXX/" out >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success SYMLINKS 'submodule update can handle symbolic links in pwd' '
+ mkdir -p linked/dir &&
+ ln -s linked/dir linkto &&
+ (cd linkto &&
+ git clone "$TRASH_DIRECTORY"/super_update_r2 super &&
+ (cd super &&
+ git submodule update --init --recursive
+ )
+ )
+'
+
+test_expect_success 'submodule update clone shallow submodule' '
+ test_when_finished "rm -rf super3" &&
+ first=$(git -C cloned rev-parse HEAD:submodule) &&
+ second=$(git -C submodule rev-parse HEAD) &&
+ commit_count=$(git -C submodule rev-list --count $first^..$second) &&
+ git clone cloned super3 &&
+ pwd=$(pwd) &&
+ (
+ cd super3 &&
+ sed -e "s#url = ../#url = file://$pwd/#" <.gitmodules >.gitmodules.tmp &&
+ mv -f .gitmodules.tmp .gitmodules &&
+ git submodule update --init --depth=$commit_count &&
+ git -C submodule log --oneline >out &&
+ test_line_count = 1 out
+ )
+'
+
+test_expect_success 'submodule update clone shallow submodule outside of depth' '
+ test_when_finished "rm -rf super3" &&
+ git clone cloned super3 &&
+ pwd=$(pwd) &&
+ (
+ cd super3 &&
+ sed -e "s#url = ../#url = file://$pwd/#" <.gitmodules >.gitmodules.tmp &&
+ mv -f .gitmodules.tmp .gitmodules &&
+ # Some protocol versions (e.g. 2) support fetching
+ # unadvertised objects, so restrict this test to v0.
+ test_must_fail env GIT_TEST_PROTOCOL_VERSION=0 \
+ git submodule update --init --depth=1 2>actual &&
+ test_grep "Direct fetching of that commit failed." actual &&
+ git -C ../submodule config uploadpack.allowReachableSHA1InWant true &&
+ git submodule update --init --depth=1 >actual &&
+ git -C submodule log --oneline >out &&
+ test_line_count = 1 out
+ )
+'
+
+test_expect_success 'submodule update --recursive drops module name before recursing' '
+ (cd super2 &&
+ (cd deeper/submodule/subsubmodule &&
+ git checkout HEAD^
+ ) &&
+ git submodule update --recursive deeper/submodule >actual &&
+ test_grep "Submodule path .deeper/submodule/subsubmodule.: checked out" actual
+ )
+'
+
+test_expect_success 'submodule update can be run in parallel' '
+ (cd super2 &&
+ GIT_TRACE=$(pwd)/trace.out git submodule update --jobs 7 &&
+ grep "7 tasks" trace.out &&
+ git config submodule.fetchJobs 8 &&
+ GIT_TRACE=$(pwd)/trace.out git submodule update &&
+ grep "8 tasks" trace.out &&
+ GIT_TRACE=$(pwd)/trace.out git submodule update --jobs 9 &&
+ grep "9 tasks" trace.out
+ )
+'
+
+test_expect_success 'git clone passes the parallel jobs config on to submodules' '
+ test_when_finished "rm -rf super4" &&
+ GIT_TRACE=$(pwd)/trace.out git clone --recurse-submodules --jobs 7 . super4 &&
+ grep "7 tasks" trace.out &&
+ rm -rf super4 &&
+ git config --global submodule.fetchJobs 8 &&
+ GIT_TRACE=$(pwd)/trace.out git clone --recurse-submodules . super4 &&
+ grep "8 tasks" trace.out &&
+ rm -rf super4 &&
+ GIT_TRACE=$(pwd)/trace.out git clone --recurse-submodules --jobs 9 . super4 &&
+ grep "9 tasks" trace.out &&
+ rm -rf super4
+'
+
+test_expect_success 'submodule update --quiet passes quietness to merge/rebase' '
+ (cd super &&
+ test_commit -C rebasing message &&
+ git submodule update --rebase --quiet >out 2>err &&
+ test_must_be_empty out &&
+ test_must_be_empty err &&
+ git submodule update --rebase >out 2>err &&
+ test_file_not_empty out &&
+ test_must_be_empty err
+ )
+'
+
+test_expect_success 'submodule update --quiet passes quietness to fetch with a shallow clone' '
+ test_when_finished "rm -rf super4 super5 super6" &&
+ git clone . super4 &&
+ (cd super4 &&
+ git submodule add --quiet file://"$TRASH_DIRECTORY"/submodule submodule3 &&
+ git commit -am "setup submodule3"
+ ) &&
+ (cd submodule &&
+ test_commit line6 file
+ ) &&
+ git clone super4 super5 &&
+ (cd super5 &&
+ git submodule update --quiet --init --depth=1 submodule3 >out 2>err &&
+ test_must_be_empty out &&
+ test_must_be_empty err
+ ) &&
+ git clone super4 super6 &&
+ (cd super6 &&
+ git submodule update --init --depth=1 submodule3 >out 2>err &&
+ test_file_not_empty out &&
+ test_file_not_empty err
+ )
+'
+
+test_expect_success 'submodule update --filter requires --init' '
+ test_expect_code 129 git -C super submodule update --filter blob:none
+'
+
+test_expect_success 'submodule update --filter sets partial clone settings' '
+ test_when_finished "rm -rf super-filter" &&
+ git clone cloned super-filter &&
+ git -C super-filter submodule update --init --filter blob:none &&
+ test_cmp_config -C super-filter/submodule true remote.origin.promisor &&
+ test_cmp_config -C super-filter/submodule blob:none remote.origin.partialclonefilter
+'
+
+# NEEDSWORK: Clean up the tests so that we can reuse the test setup.
+# Don't reuse the existing repos because the earlier tests have
+# intentionally disruptive configurations.
+test_expect_success 'setup clean recursive superproject' '
+ git init bottom &&
+ test_commit -C bottom "bottom" &&
+ git init middle &&
+ git -C middle submodule add ../bottom bottom &&
+ git -C middle commit -m "middle" &&
+ git init top &&
+ git -C top submodule add ../middle middle &&
+ git -C top commit -m "top" &&
+ git clone --recurse-submodules top top-clean
+'
+
+test_expect_success 'submodule update should skip unmerged submodules' '
+ test_when_finished "rm -fr top-cloned" &&
+ cp -r top-clean top-cloned &&
+
+ # Create an upstream commit in each repo, starting with bottom
+ test_commit -C bottom upstream_commit &&
+ # Create middle commit
+ git -C middle/bottom fetch &&
+ git -C middle/bottom checkout -f FETCH_HEAD &&
+ git -C middle add bottom &&
+ git -C middle commit -m "upstream_commit" &&
+ # Create top commit
+ git -C top/middle fetch &&
+ git -C top/middle checkout -f FETCH_HEAD &&
+ git -C top add middle &&
+ git -C top commit -m "upstream_commit" &&
+
+ # Create a downstream conflict
+ test_commit -C top-cloned/middle/bottom downstream_commit &&
+ git -C top-cloned/middle add bottom &&
+ git -C top-cloned/middle commit -m "downstream_commit" &&
+ git -C top-cloned/middle fetch --recurse-submodules origin &&
+ test_must_fail git -C top-cloned/middle merge origin/main &&
+
+ # Make the update of "middle" a no-op, otherwise we error out
+ # because of its unmerged state
+ test_config -C top-cloned submodule.middle.update !true &&
+ git -C top-cloned submodule update --recursive 2>actual.err &&
+ cat >expect.err <<-\EOF &&
+ Skipping unmerged submodule middle/bottom
+ EOF
+ test_cmp expect.err actual.err
+'
+
+test_expect_success 'submodule update --recursive skip submodules with strategy=none' '
+ test_when_finished "rm -fr top-cloned" &&
+ cp -r top-clean top-cloned &&
+
+ test_commit -C top-cloned/middle/bottom downstream_commit &&
+ git -C top-cloned/middle config submodule.bottom.update none &&
+ git -C top-cloned submodule update --recursive 2>actual.err &&
+ cat >expect.err <<-\EOF &&
+ Skipping submodule '\''middle/bottom'\''
+ EOF
+ test_cmp expect.err actual.err
+'
+
+add_submodule_commit_and_validate () {
+ HASH=$(git rev-parse HEAD) &&
+ git update-index --add --cacheinfo 160000,$HASH,sub &&
+ git commit -m "create submodule" &&
+ echo "160000 commit $HASH sub" >expect &&
+ git ls-tree HEAD -- sub >actual &&
+ test_cmp expect actual
+}
+
+test_expect_success 'commit with staged submodule change' '
+ add_submodule_commit_and_validate
+'
+
+test_expect_success 'commit with staged submodule change with ignoreSubmodules dirty' '
+ test_config diff.ignoreSubmodules dirty &&
+ add_submodule_commit_and_validate
+'
+
+test_expect_success 'commit with staged submodule change with ignoreSubmodules all' '
+ test_config diff.ignoreSubmodules all &&
+ add_submodule_commit_and_validate
+'
+
+test_done
diff --git a/t/t7407-submodule-foreach.sh b/t/t7407-submodule-foreach.sh
new file mode 100755
index 0000000..8d7b234
--- /dev/null
+++ b/t/t7407-submodule-foreach.sh
@@ -0,0 +1,440 @@
+#!/bin/sh
+#
+# Copyright (c) 2009 Johan Herland
+#
+
+test_description='Test "git submodule foreach"
+
+This test verifies that "git submodule foreach" correctly visits all submodules
+that are currently checked out.
+'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+
+test_expect_success 'setup a submodule tree' '
+ git config --global protocol.file.allow always &&
+ echo file > file &&
+ git add file &&
+ test_tick &&
+ git commit -m upstream &&
+ git clone . super &&
+ git clone super submodule &&
+ (
+ cd super &&
+ git submodule add ../submodule sub1 &&
+ git submodule add ../submodule sub2 &&
+ git submodule add ../submodule sub3 &&
+ git config -f .gitmodules --rename-section \
+ submodule.sub1 submodule.foo1 &&
+ git config -f .gitmodules --rename-section \
+ submodule.sub2 submodule.foo2 &&
+ git config -f .gitmodules --rename-section \
+ submodule.sub3 submodule.foo3 &&
+ git add .gitmodules &&
+ test_tick &&
+ git commit -m "submodules" &&
+ git submodule init sub1 &&
+ git submodule init sub2 &&
+ git submodule init sub3
+ ) &&
+ (
+ cd submodule &&
+ echo different > file &&
+ git add file &&
+ test_tick &&
+ git commit -m "different"
+ ) &&
+ (
+ cd super &&
+ (
+ cd sub3 &&
+ git pull
+ ) &&
+ git add sub3 &&
+ test_tick &&
+ git commit -m "update sub3"
+ )
+'
+
+sub1sha1=$(cd super/sub1 && git rev-parse HEAD)
+sub3sha1=$(cd super/sub3 && git rev-parse HEAD)
+
+pwd=$(pwd)
+
+cat > expect <<EOF
+Entering 'sub1'
+$pwd/clone-foo1-sub1-$sub1sha1
+Entering 'sub3'
+$pwd/clone-foo3-sub3-$sub3sha1
+EOF
+
+test_expect_success 'test basic "submodule foreach" usage' '
+ git clone super clone &&
+ (
+ cd clone &&
+ git submodule update --init -- sub1 sub3 &&
+ git submodule foreach "echo \$toplevel-\$name-\$path-\$sha1" > ../actual &&
+ git config foo.bar zar &&
+ git submodule foreach "git config --file \"\$toplevel/.git/config\" foo.bar"
+ ) &&
+ test_cmp expect actual
+'
+
+cat >expect <<EOF
+Entering '../sub1'
+$pwd/clone-foo1-sub1-../sub1-$sub1sha1
+Entering '../sub3'
+$pwd/clone-foo3-sub3-../sub3-$sub3sha1
+EOF
+
+test_expect_success 'test "submodule foreach" from subdirectory' '
+ mkdir clone/sub &&
+ (
+ cd clone/sub &&
+ git submodule foreach "echo \$toplevel-\$name-\$sm_path-\$displaypath-\$sha1" >../../actual
+ ) &&
+ test_cmp expect actual
+'
+
+test_expect_success 'setup nested submodules' '
+ git clone submodule nested1 &&
+ git clone submodule nested2 &&
+ git clone submodule nested3 &&
+ (
+ cd nested3 &&
+ git submodule add ../submodule submodule &&
+ test_tick &&
+ git commit -m "submodule" &&
+ git submodule init submodule
+ ) &&
+ (
+ cd nested2 &&
+ git submodule add ../nested3 nested3 &&
+ test_tick &&
+ git commit -m "nested3" &&
+ git submodule init nested3
+ ) &&
+ (
+ cd nested1 &&
+ git submodule add ../nested2 nested2 &&
+ test_tick &&
+ git commit -m "nested2" &&
+ git submodule init nested2
+ ) &&
+ (
+ cd super &&
+ git submodule add ../nested1 nested1 &&
+ test_tick &&
+ git commit -m "nested1" &&
+ git submodule init nested1
+ )
+'
+
+test_expect_success 'use "submodule foreach" to checkout 2nd level submodule' '
+ git clone super clone2 &&
+ (
+ cd clone2 &&
+ test_must_fail git rev-parse --resolve-git-dir sub1/.git &&
+ test_must_fail git rev-parse --resolve-git-dir sub2/.git &&
+ test_must_fail git rev-parse --resolve-git-dir sub3/.git &&
+ test_must_fail git rev-parse --resolve-git-dir nested1/.git &&
+ git submodule update --init &&
+ git rev-parse --resolve-git-dir sub1/.git &&
+ git rev-parse --resolve-git-dir sub2/.git &&
+ git rev-parse --resolve-git-dir sub3/.git &&
+ git rev-parse --resolve-git-dir nested1/.git &&
+ test_must_fail git rev-parse --resolve-git-dir nested1/nested2/.git &&
+ git submodule foreach "git submodule update --init" &&
+ git rev-parse --resolve-git-dir nested1/nested2/.git &&
+ test_must_fail git rev-parse --resolve-git-dir nested1/nested2/nested3/.git
+ )
+'
+
+test_expect_success 'usage: foreach -- --not-an-option' '
+ test_expect_code 1 git submodule foreach -- --not-an-option &&
+ test_expect_code 1 git -C clone2 submodule foreach -- --not-an-option
+'
+
+test_expect_success 'use "foreach --recursive" to checkout all submodules' '
+ (
+ cd clone2 &&
+ git submodule foreach --recursive "git submodule update --init" &&
+ git rev-parse --resolve-git-dir nested1/nested2/nested3/.git &&
+ git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git
+ )
+'
+
+cat > expect <<EOF
+Entering 'nested1'
+Entering 'nested1/nested2'
+Entering 'nested1/nested2/nested3'
+Entering 'nested1/nested2/nested3/submodule'
+Entering 'sub1'
+Entering 'sub2'
+Entering 'sub3'
+EOF
+
+test_expect_success 'test messages from "foreach --recursive"' '
+ (
+ cd clone2 &&
+ git submodule foreach --recursive "true" > ../actual
+ ) &&
+ test_cmp expect actual
+'
+
+cat > expect <<EOF
+Entering '../nested1'
+Entering '../nested1/nested2'
+Entering '../nested1/nested2/nested3'
+Entering '../nested1/nested2/nested3/submodule'
+Entering '../sub1'
+Entering '../sub2'
+Entering '../sub3'
+EOF
+
+test_expect_success 'test messages from "foreach --recursive" from subdirectory' '
+ (
+ cd clone2 &&
+ mkdir untracked &&
+ cd untracked &&
+ git submodule foreach --recursive >../../actual
+ ) &&
+ test_cmp expect actual
+'
+sub1sha1=$(cd clone2/sub1 && git rev-parse HEAD)
+sub2sha1=$(cd clone2/sub2 && git rev-parse HEAD)
+sub3sha1=$(cd clone2/sub3 && git rev-parse HEAD)
+nested1sha1=$(cd clone2/nested1 && git rev-parse HEAD)
+nested2sha1=$(cd clone2/nested1/nested2 && git rev-parse HEAD)
+nested3sha1=$(cd clone2/nested1/nested2/nested3 && git rev-parse HEAD)
+submodulesha1=$(cd clone2/nested1/nested2/nested3/submodule && git rev-parse HEAD)
+
+cat >expect <<EOF
+Entering '../nested1'
+toplevel: $pwd/clone2 name: nested1 path: nested1 displaypath: ../nested1 hash: $nested1sha1
+Entering '../nested1/nested2'
+toplevel: $pwd/clone2/nested1 name: nested2 path: nested2 displaypath: ../nested1/nested2 hash: $nested2sha1
+Entering '../nested1/nested2/nested3'
+toplevel: $pwd/clone2/nested1/nested2 name: nested3 path: nested3 displaypath: ../nested1/nested2/nested3 hash: $nested3sha1
+Entering '../nested1/nested2/nested3/submodule'
+toplevel: $pwd/clone2/nested1/nested2/nested3 name: submodule path: submodule displaypath: ../nested1/nested2/nested3/submodule hash: $submodulesha1
+Entering '../sub1'
+toplevel: $pwd/clone2 name: foo1 path: sub1 displaypath: ../sub1 hash: $sub1sha1
+Entering '../sub2'
+toplevel: $pwd/clone2 name: foo2 path: sub2 displaypath: ../sub2 hash: $sub2sha1
+Entering '../sub3'
+toplevel: $pwd/clone2 name: foo3 path: sub3 displaypath: ../sub3 hash: $sub3sha1
+EOF
+
+test_expect_success 'test "submodule foreach --recursive" from subdirectory' '
+ (
+ cd clone2/untracked &&
+ git submodule foreach --recursive "echo toplevel: \$toplevel name: \$name path: \$sm_path displaypath: \$displaypath hash: \$sha1" >../../actual
+ ) &&
+ test_cmp expect actual
+'
+
+cat > expect <<EOF
+nested1-nested1
+nested2-nested2
+nested3-nested3
+submodule-submodule
+foo1-sub1
+foo2-sub2
+foo3-sub3
+EOF
+
+test_expect_success 'test "foreach --quiet --recursive"' '
+ (
+ cd clone2 &&
+ git submodule foreach -q --recursive "echo \$name-\$path" > ../actual
+ ) &&
+ test_cmp expect actual
+'
+
+test_expect_success 'use "update --recursive" to checkout all submodules' '
+ git clone super clone3 &&
+ (
+ cd clone3 &&
+ test_must_fail git rev-parse --resolve-git-dir sub1/.git &&
+ test_must_fail git rev-parse --resolve-git-dir sub2/.git &&
+ test_must_fail git rev-parse --resolve-git-dir sub3/.git &&
+ test_must_fail git rev-parse --resolve-git-dir nested1/.git &&
+ git submodule update --init --recursive &&
+ git rev-parse --resolve-git-dir sub1/.git &&
+ git rev-parse --resolve-git-dir sub2/.git &&
+ git rev-parse --resolve-git-dir sub3/.git &&
+ git rev-parse --resolve-git-dir nested1/.git &&
+ git rev-parse --resolve-git-dir nested1/nested2/.git &&
+ git rev-parse --resolve-git-dir nested1/nested2/nested3/.git &&
+ git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git
+ )
+'
+
+nested1sha1=$(cd clone3/nested1 && git rev-parse HEAD)
+nested2sha1=$(cd clone3/nested1/nested2 && git rev-parse HEAD)
+nested3sha1=$(cd clone3/nested1/nested2/nested3 && git rev-parse HEAD)
+submodulesha1=$(cd clone3/nested1/nested2/nested3/submodule && git rev-parse HEAD)
+sub1sha1=$(cd clone3/sub1 && git rev-parse HEAD)
+sub2sha1=$(cd clone3/sub2 && git rev-parse HEAD)
+sub3sha1=$(cd clone3/sub3 && git rev-parse HEAD)
+sub1sha1_short=$(cd clone3/sub1 && git rev-parse --short HEAD)
+sub2sha1_short=$(cd clone3/sub2 && git rev-parse --short HEAD)
+
+cat > expect <<EOF
+ $nested1sha1 nested1 (heads/main)
+ $nested2sha1 nested1/nested2 (heads/main)
+ $nested3sha1 nested1/nested2/nested3 (heads/main)
+ $submodulesha1 nested1/nested2/nested3/submodule (heads/main)
+ $sub1sha1 sub1 ($sub1sha1_short)
+ $sub2sha1 sub2 ($sub2sha1_short)
+ $sub3sha1 sub3 (heads/main)
+EOF
+
+test_expect_success 'test "status --recursive"' '
+ (
+ cd clone3 &&
+ git submodule status --recursive > ../actual
+ ) &&
+ test_cmp expect actual
+'
+
+cat > expect <<EOF
+ $nested1sha1 nested1 (heads/main)
++$nested2sha1 nested1/nested2 (file2~1)
+ $nested3sha1 nested1/nested2/nested3 (heads/main)
+ $submodulesha1 nested1/nested2/nested3/submodule (heads/main)
+EOF
+
+test_expect_success 'ensure "status --cached --recursive" preserves the --cached flag' '
+ (
+ cd clone3 &&
+ (
+ cd nested1/nested2 &&
+ test_commit file2
+ ) &&
+ git submodule status --cached --recursive -- nested1 > ../actual
+ ) &&
+ test_cmp expect actual
+'
+
+nested2sha1=$(git -C clone3/nested1/nested2 rev-parse HEAD)
+
+cat > expect <<EOF
+ $nested1sha1 ../nested1 (heads/main)
++$nested2sha1 ../nested1/nested2 (file2)
+ $nested3sha1 ../nested1/nested2/nested3 (heads/main)
+ $submodulesha1 ../nested1/nested2/nested3/submodule (heads/main)
+ $sub1sha1 ../sub1 ($sub1sha1_short)
+ $sub2sha1 ../sub2 ($sub2sha1_short)
+ $sub3sha1 ../sub3 (heads/main)
+EOF
+
+test_expect_success 'test "status --recursive" from sub directory' '
+ (
+ cd clone3 &&
+ mkdir tmp && cd tmp &&
+ git submodule status --recursive > ../../actual
+ ) &&
+ test_cmp expect actual
+'
+
+test_expect_success 'use "git clone --recursive" to checkout all submodules' '
+ git clone --recursive super clone4 &&
+ (
+ cd clone4 &&
+ git rev-parse --resolve-git-dir .git &&
+ git rev-parse --resolve-git-dir sub1/.git &&
+ git rev-parse --resolve-git-dir sub2/.git &&
+ git rev-parse --resolve-git-dir sub3/.git &&
+ git rev-parse --resolve-git-dir nested1/.git &&
+ git rev-parse --resolve-git-dir nested1/nested2/.git &&
+ git rev-parse --resolve-git-dir nested1/nested2/nested3/.git &&
+ git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git
+ )
+'
+
+test_expect_success 'test "update --recursive" with a flag with spaces' '
+ git clone super "common objects" &&
+ git clone super clone5 &&
+ (
+ cd clone5 &&
+ test_must_fail git rev-parse --resolve-git-dir d nested1/.git &&
+ git submodule update --init --recursive --reference="$(dirname "$PWD")/common objects" &&
+ git rev-parse --resolve-git-dir nested1/.git &&
+ git rev-parse --resolve-git-dir nested1/nested2/.git &&
+ git rev-parse --resolve-git-dir nested1/nested2/nested3/.git &&
+ test -f .git/modules/nested1/objects/info/alternates &&
+ test -f .git/modules/nested1/modules/nested2/objects/info/alternates &&
+ test -f .git/modules/nested1/modules/nested2/modules/nested3/objects/info/alternates
+ )
+'
+
+test_expect_success 'use "update --recursive nested1" to checkout all submodules rooted in nested1' '
+ git clone super clone6 &&
+ (
+ cd clone6 &&
+ test_must_fail git rev-parse --resolve-git-dir sub1/.git &&
+ test_must_fail git rev-parse --resolve-git-dir sub2/.git &&
+ test_must_fail git rev-parse --resolve-git-dir sub3/.git &&
+ test_must_fail git rev-parse --resolve-git-dir nested1/.git &&
+ git submodule update --init --recursive -- nested1 &&
+ test_must_fail git rev-parse --resolve-git-dir sub1/.git &&
+ test_must_fail git rev-parse --resolve-git-dir sub2/.git &&
+ test_must_fail git rev-parse --resolve-git-dir sub3/.git &&
+ git rev-parse --resolve-git-dir nested1/.git &&
+ git rev-parse --resolve-git-dir nested1/nested2/.git &&
+ git rev-parse --resolve-git-dir nested1/nested2/nested3/.git &&
+ git rev-parse --resolve-git-dir nested1/nested2/nested3/submodule/.git
+ )
+'
+
+test_expect_success 'command passed to foreach retains notion of stdin' '
+ (
+ cd super &&
+ git submodule foreach echo success >../expected &&
+ yes | git submodule foreach "read y && test \"x\$y\" = xy && echo success" >../actual
+ ) &&
+ test_cmp expected actual
+'
+
+test_expect_success 'command passed to foreach --recursive retains notion of stdin' '
+ (
+ cd clone2 &&
+ git submodule foreach --recursive echo success >../expected &&
+ yes | git submodule foreach --recursive "read y && test \"x\$y\" = xy && echo success" >../actual
+ ) &&
+ test_cmp expected actual
+'
+
+test_expect_success 'multi-argument command passed to foreach is not shell-evaluated twice' '
+ (
+ cd super &&
+ git submodule foreach "echo \\\"quoted\\\"" > ../expected &&
+ git submodule foreach echo \"quoted\" > ../actual
+ ) &&
+ test_cmp expected actual
+'
+
+test_expect_success 'option-like arguments passed to foreach commands are not lost' '
+ (
+ cd super &&
+ git submodule foreach "echo be --quiet" > ../expected &&
+ git submodule foreach echo be --quiet > ../actual
+ ) &&
+ grep -sq -e "--quiet" expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'option-like arguments passed to foreach recurse correctly' '
+ git -C clone2 submodule foreach --recursive "echo be --an-option" >expect &&
+ git -C clone2 submodule foreach --recursive echo be --an-option >actual &&
+ grep -e "--an-option" expect &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t7408-submodule-reference.sh b/t/t7408-submodule-reference.sh
new file mode 100755
index 0000000..d6040e0
--- /dev/null
+++ b/t/t7408-submodule-reference.sh
@@ -0,0 +1,227 @@
+#!/bin/sh
+#
+# Copyright (c) 2009, Red Hat Inc, Author: Michael S. Tsirkin (mst@redhat.com)
+#
+
+test_description='test clone --reference'
+. ./test-lib.sh
+
+base_dir=$(pwd)
+
+test_alternate_is_used () {
+ alternates_file="$1" &&
+ working_dir="$2" &&
+ test_line_count = 1 "$alternates_file" &&
+ echo "0 objects, 0 kilobytes" >expect &&
+ git -C "$working_dir" count-objects >actual &&
+ test_cmp expect actual
+}
+
+test_expect_success 'setup' '
+ git config --global protocol.file.allow always
+'
+
+test_expect_success 'preparing first repository' '
+ test_create_repo A &&
+ (
+ cd A &&
+ echo first >file1 &&
+ git add file1 &&
+ git commit -m A-initial
+ )
+'
+
+test_expect_success 'preparing second repository' '
+ git clone A B &&
+ (
+ cd B &&
+ echo second >file2 &&
+ git add file2 &&
+ git commit -m B-addition &&
+ git repack -a -d &&
+ git prune
+ )
+'
+
+test_expect_success 'preparing superproject' '
+ test_create_repo super &&
+ (
+ cd super &&
+ echo file >file &&
+ git add file &&
+ git commit -m B-super-initial
+ )
+'
+
+test_expect_success 'submodule add --reference uses alternates' '
+ (
+ cd super &&
+ git submodule add --reference ../B "file://$base_dir/A" sub &&
+ git commit -m B-super-added &&
+ git repack -ad
+ ) &&
+ test_alternate_is_used super/.git/modules/sub/objects/info/alternates super/sub
+'
+
+test_expect_success 'submodule add --reference with --dissociate does not use alternates' '
+ (
+ cd super &&
+ git submodule add --reference ../B --dissociate "file://$base_dir/A" sub-dissociate &&
+ git commit -m B-super-added &&
+ git repack -ad
+ ) &&
+ test_path_is_missing super/.git/modules/sub-dissociate/objects/info/alternates
+'
+
+test_expect_success 'that reference gets used with add' '
+ (
+ cd super/sub &&
+ echo "0 objects, 0 kilobytes" >expected &&
+ git count-objects >current &&
+ diff expected current
+ )
+'
+
+# The tests up to this point, and repositories created by them
+# (A, B, super and super/sub), are about setting up the stage
+# for subsequent tests and meant to be kept throughout the
+# remainder of the test.
+# Tests from here on, if they create their own test repository,
+# are expected to clean after themselves.
+
+test_expect_success 'updating superproject keeps alternates' '
+ test_when_finished "rm -rf super-clone" &&
+ git clone super super-clone &&
+ git -C super-clone submodule update --init --reference ../B &&
+ test_alternate_is_used super-clone/.git/modules/sub/objects/info/alternates super-clone/sub
+'
+
+test_expect_success 'updating superproject with --dissociate does not keep alternates' '
+ test_when_finished "rm -rf super-clone" &&
+ git clone super super-clone &&
+ git -C super-clone submodule update --init --reference ../B --dissociate &&
+ test_path_is_missing super-clone/.git/modules/sub/objects/info/alternates
+'
+
+test_expect_success 'submodules use alternates when cloning a superproject' '
+ test_when_finished "rm -rf super-clone" &&
+ git clone --reference super --recursive super super-clone &&
+ (
+ cd super-clone &&
+ # test superproject has alternates setup correctly
+ test_alternate_is_used .git/objects/info/alternates . &&
+ # test submodule has correct setup
+ test_alternate_is_used .git/modules/sub/objects/info/alternates sub
+ )
+'
+
+test_expect_success 'missing submodule alternate fails clone and submodule update' '
+ test_when_finished "rm -rf super-clone" &&
+ git clone super super2 &&
+ test_must_fail git clone --recursive --reference super2 super2 super-clone &&
+ (
+ cd super-clone &&
+ # test superproject has alternates setup correctly
+ test_alternate_is_used .git/objects/info/alternates . &&
+ # update of the submodule succeeds
+ test_must_fail git submodule update --init &&
+ # and we have no alternates:
+ test_path_is_missing .git/modules/sub/objects/info/alternates &&
+ test_path_is_missing sub/file1
+ )
+'
+
+test_expect_success 'ignoring missing submodule alternates passes clone and submodule update' '
+ test_when_finished "rm -rf super-clone" &&
+ git clone --reference-if-able super2 --recursive super2 super-clone &&
+ (
+ cd super-clone &&
+ # test superproject has alternates setup correctly
+ test_alternate_is_used .git/objects/info/alternates . &&
+ # update of the submodule succeeds
+ git submodule update --init &&
+ # and we have no alternates:
+ test_path_is_missing .git/modules/sub/objects/info/alternates &&
+ test_path_is_file sub/file1
+ )
+'
+
+test_expect_success 'preparing second superproject with a nested submodule plus partial clone' '
+ test_create_repo supersuper &&
+ (
+ cd supersuper &&
+ echo "I am super super." >file &&
+ git add file &&
+ git commit -m B-super-super-initial &&
+ git submodule add "file://$base_dir/super" subwithsub &&
+ git commit -m B-super-super-added &&
+ git submodule update --init --recursive &&
+ git repack -ad
+ ) &&
+ git clone supersuper supersuper2 &&
+ (
+ cd supersuper2 &&
+ git submodule update --init
+ )
+'
+
+# At this point there are three root-level positories: A, B, super and super2
+
+test_expect_success 'nested submodule alternate in works and is actually used' '
+ test_when_finished "rm -rf supersuper-clone" &&
+ git clone --recursive --reference supersuper supersuper supersuper-clone &&
+ (
+ cd supersuper-clone &&
+ # test superproject has alternates setup correctly
+ test_alternate_is_used .git/objects/info/alternates . &&
+ # immediate submodule has alternate:
+ test_alternate_is_used .git/modules/subwithsub/objects/info/alternates subwithsub &&
+ # nested submodule also has alternate:
+ test_alternate_is_used .git/modules/subwithsub/modules/sub/objects/info/alternates subwithsub/sub
+ )
+'
+
+check_that_two_of_three_alternates_are_used() {
+ test_alternate_is_used .git/objects/info/alternates . &&
+ # immediate submodule has alternate:
+ test_alternate_is_used .git/modules/subwithsub/objects/info/alternates subwithsub &&
+ # but nested submodule has no alternate:
+ test_path_is_missing .git/modules/subwithsub/modules/sub/objects/info/alternates
+}
+
+
+test_expect_success 'missing nested submodule alternate fails clone and submodule update' '
+ test_when_finished "rm -rf supersuper-clone" &&
+ test_must_fail git clone --recursive --reference supersuper2 supersuper2 supersuper-clone &&
+ (
+ cd supersuper-clone &&
+ check_that_two_of_three_alternates_are_used &&
+ # update of the submodule fails
+ cat >expect <<-\EOF &&
+ fatal: submodule '\''sub'\'' cannot add alternate: path ... does not exist
+ Failed to clone '\''sub'\''. Retry scheduled
+ fatal: submodule '\''sub-dissociate'\'' cannot add alternate: path ... does not exist
+ Failed to clone '\''sub-dissociate'\''. Retry scheduled
+ fatal: submodule '\''sub'\'' cannot add alternate: path ... does not exist
+ Failed to clone '\''sub'\'' a second time, aborting
+ fatal: Failed to recurse into submodule path ...
+ EOF
+ test_must_fail git submodule update --init --recursive 2>err &&
+ grep -e fatal: -e ^Failed err >actual.raw &&
+ sed -e "s/path $SQ[^$SQ]*$SQ/path .../" <actual.raw >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'missing nested submodule alternate in --reference-if-able mode' '
+ test_when_finished "rm -rf supersuper-clone" &&
+ git clone --recursive --reference-if-able supersuper2 supersuper2 supersuper-clone &&
+ (
+ cd supersuper-clone &&
+ check_that_two_of_three_alternates_are_used &&
+ # update of the submodule succeeds
+ git submodule update --init --recursive
+ )
+'
+
+test_done
diff --git a/t/t7409-submodule-detached-work-tree.sh b/t/t7409-submodule-detached-work-tree.sh
new file mode 100755
index 0000000..574a6fc
--- /dev/null
+++ b/t/t7409-submodule-detached-work-tree.sh
@@ -0,0 +1,92 @@
+#!/bin/sh
+#
+# Copyright (c) 2012 Daniel Graña
+#
+
+test_description='Test submodules on detached working tree
+
+This test verifies that "git submodule" initialization, update and addition works
+on detached working trees
+'
+
+TEST_NO_CREATE_REPO=1
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ git config --global protocol.file.allow always
+'
+
+test_expect_success 'submodule on detached working tree' '
+ git init --bare remote &&
+ test_create_repo bundle1 &&
+ (
+ cd bundle1 &&
+ test_commit "shoot" &&
+ git rev-parse --verify HEAD >../expect
+ ) &&
+ mkdir home &&
+ (
+ cd home &&
+ GIT_WORK_TREE="$(pwd)" &&
+ GIT_DIR="$(pwd)/.dotfiles" &&
+ export GIT_WORK_TREE GIT_DIR &&
+ git clone --bare ../remote .dotfiles &&
+ git submodule add ../bundle1 .vim/bundle/sogood &&
+ test_commit "sogood" &&
+ (
+ unset GIT_WORK_TREE GIT_DIR &&
+ cd .vim/bundle/sogood &&
+ git rev-parse --verify HEAD >actual &&
+ test_cmp ../../../../expect actual
+ ) &&
+ git push origin main
+ ) &&
+ mkdir home2 &&
+ (
+ cd home2 &&
+ git clone --bare ../remote .dotfiles &&
+ GIT_WORK_TREE="$(pwd)" &&
+ GIT_DIR="$(pwd)/.dotfiles" &&
+ export GIT_WORK_TREE GIT_DIR &&
+ git checkout main &&
+ git submodule update --init &&
+ (
+ unset GIT_WORK_TREE GIT_DIR &&
+ cd .vim/bundle/sogood &&
+ git rev-parse --verify HEAD >actual &&
+ test_cmp ../../../../expect actual
+ )
+ )
+'
+
+test_expect_success 'submodule on detached working pointed by core.worktree' '
+ mkdir home3 &&
+ (
+ cd home3 &&
+ GIT_DIR="$(pwd)/.dotfiles" &&
+ export GIT_DIR &&
+ git clone --bare ../remote "$GIT_DIR" &&
+ git config core.bare false &&
+ git config core.worktree .. &&
+ git checkout main &&
+ git submodule add ../bundle1 .vim/bundle/dupe &&
+ test_commit "dupe" &&
+ git push origin main
+ ) &&
+ (
+ cd home &&
+ GIT_DIR="$(pwd)/.dotfiles" &&
+ export GIT_DIR &&
+ git config core.bare false &&
+ git config core.worktree .. &&
+ git pull &&
+ git submodule update --init &&
+ test -f .vim/bundle/dupe/shoot.t
+ )
+'
+
+test_done
diff --git a/t/t7411-submodule-config.sh b/t/t7411-submodule-config.sh
new file mode 100755
index 0000000..31271f8
--- /dev/null
+++ b/t/t7411-submodule-config.sh
@@ -0,0 +1,261 @@
+#!/bin/sh
+#
+# Copyright (c) 2014 Heiko Voigt
+#
+
+test_description='Test submodules config cache infrastructure
+
+This test verifies that parsing .gitmodules configurations directly
+from the database and from the worktree works.
+'
+
+TEST_NO_CREATE_REPO=1
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ git config --global protocol.file.allow always
+'
+test_expect_success 'submodule config cache setup' '
+ mkdir submodule &&
+ (cd submodule &&
+ git init &&
+ echo a >a &&
+ git add . &&
+ git commit -ma
+ ) &&
+ mkdir super &&
+ (cd super &&
+ git init &&
+ git submodule add ../submodule &&
+ git submodule add ../submodule a &&
+ git commit -m "add as submodule and as a" &&
+ git mv a b &&
+ git commit -m "move a to b"
+ )
+'
+
+test_expect_success 'configuration parsing with error' '
+ test_when_finished "rm -rf repo" &&
+ test_create_repo repo &&
+ cat >repo/.gitmodules <<-\EOF &&
+ [submodule "s"]
+ path
+ ignore
+ EOF
+ (
+ cd repo &&
+ test_must_fail test-tool submodule-config "" s 2>actual &&
+ test_grep "bad config" actual
+ )
+'
+
+cat >super/expect <<EOF
+Submodule name: 'a' for path 'a'
+Submodule name: 'a' for path 'b'
+Submodule name: 'submodule' for path 'submodule'
+Submodule name: 'submodule' for path 'submodule'
+EOF
+
+test_expect_success 'test parsing and lookup of submodule config by path' '
+ (cd super &&
+ test-tool submodule-config \
+ HEAD^ a \
+ HEAD b \
+ HEAD^ submodule \
+ HEAD submodule \
+ >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'test parsing and lookup of submodule config by name' '
+ (cd super &&
+ test-tool submodule-config --name \
+ HEAD^ a \
+ HEAD a \
+ HEAD^ submodule \
+ HEAD submodule \
+ >actual &&
+ test_cmp expect actual
+ )
+'
+
+cat >super/expect_error <<EOF
+Submodule name: 'a' for path 'b'
+Submodule name: 'submodule' for path 'submodule'
+EOF
+
+test_expect_success 'error in history of one submodule config lets continue, stderr message contains blob ref' '
+ ORIG=$(git -C super rev-parse HEAD) &&
+ test_when_finished "git -C super reset --hard $ORIG" &&
+ (cd super &&
+ cp .gitmodules .gitmodules.bak &&
+ echo " value = \"" >>.gitmodules &&
+ git add .gitmodules &&
+ mv .gitmodules.bak .gitmodules &&
+ git commit -m "add error" &&
+ sha1=$(git rev-parse HEAD) &&
+ test-tool submodule-config \
+ HEAD b \
+ HEAD submodule \
+ >actual \
+ 2>actual_stderr &&
+ test_cmp expect_error actual &&
+ test_grep "submodule-blob $sha1:.gitmodules" actual_stderr >/dev/null
+ )
+'
+
+test_expect_success 'using different treeishs works' '
+ (
+ cd super &&
+ git tag new_tag &&
+ tree=$(git rev-parse HEAD^{tree}) &&
+ commit=$(git rev-parse HEAD^{commit}) &&
+ test-tool submodule-config $commit b >expect &&
+ test-tool submodule-config $tree b >actual.1 &&
+ test-tool submodule-config new_tag b >actual.2 &&
+ test_cmp expect actual.1 &&
+ test_cmp expect actual.2
+ )
+'
+
+test_expect_success 'error in history in fetchrecursesubmodule lets continue' '
+ ORIG=$(git -C super rev-parse HEAD) &&
+ test_when_finished "git -C super reset --hard $ORIG" &&
+ (cd super &&
+ git config -f .gitmodules \
+ submodule.submodule.fetchrecursesubmodules blabla &&
+ git add .gitmodules &&
+ git config --unset -f .gitmodules \
+ submodule.submodule.fetchrecursesubmodules &&
+ git commit -m "add error in fetchrecursesubmodules" &&
+ test-tool submodule-config \
+ HEAD b \
+ HEAD submodule \
+ >actual &&
+ test_cmp expect_error actual
+ )
+'
+
+test_expect_success 'reading submodules config from the working tree' '
+ (cd super &&
+ echo "../submodule" >expect &&
+ test-tool submodule config-list submodule.submodule.url >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'unsetting submodules config from the working tree' '
+ (cd super &&
+ test-tool submodule config-unset submodule.submodule.url &&
+ test-tool submodule config-list submodule.submodule.url >actual &&
+ test_must_be_empty actual
+ )
+'
+
+
+test_expect_success 'writing submodules config' '
+ (cd super &&
+ echo "new_url" >expect &&
+ test-tool submodule config-set submodule.submodule.url "new_url" &&
+ test-tool submodule config-list submodule.submodule.url >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'overwriting unstaged submodules config' '
+ test_when_finished "git -C super checkout .gitmodules" &&
+ (cd super &&
+ echo "newer_url" >expect &&
+ test-tool submodule config-set submodule.submodule.url "newer_url" &&
+ test-tool submodule config-list submodule.submodule.url >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'writeable .gitmodules when it is in the working tree' '
+ test-tool -C super submodule config-writeable
+'
+
+test_expect_success 'writeable .gitmodules when it is nowhere in the repository' '
+ ORIG=$(git -C super rev-parse HEAD) &&
+ test_when_finished "git -C super reset --hard $ORIG" &&
+ (cd super &&
+ git rm .gitmodules &&
+ git commit -m "remove .gitmodules from the current branch" &&
+ test-tool submodule config-writeable
+ )
+'
+
+test_expect_success 'non-writeable .gitmodules when it is in the index but not in the working tree' '
+ test_when_finished "git -C super checkout .gitmodules" &&
+ (cd super &&
+ rm -f .gitmodules &&
+ test_must_fail test-tool submodule config-writeable
+ )
+'
+
+test_expect_success 'non-writeable .gitmodules when it is in the current branch but not in the index' '
+ ORIG=$(git -C super rev-parse HEAD) &&
+ test_when_finished "git -C super reset --hard $ORIG" &&
+ (cd super &&
+ git rm .gitmodules &&
+ test_must_fail test-tool submodule config-writeable
+ )
+'
+
+test_expect_success 'reading submodules config from the index when .gitmodules is not in the working tree' '
+ ORIG=$(git -C super rev-parse HEAD) &&
+ test_when_finished "git -C super reset --hard $ORIG" &&
+ (cd super &&
+ test-tool submodule config-set submodule.submodule.url "staged_url" &&
+ git add .gitmodules &&
+ rm -f .gitmodules &&
+ echo "staged_url" >expect &&
+ test-tool submodule config-list submodule.submodule.url >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'reading submodules config from the current branch when .gitmodules is not in the index' '
+ ORIG=$(git -C super rev-parse HEAD) &&
+ test_when_finished "git -C super reset --hard $ORIG" &&
+ (cd super &&
+ git rm .gitmodules &&
+ echo "../submodule" >expect &&
+ test-tool submodule config-list submodule.submodule.url >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'reading nested submodules config' '
+ (cd super &&
+ git init submodule/nested_submodule &&
+ echo "a" >submodule/nested_submodule/a &&
+ git -C submodule/nested_submodule add a &&
+ git -C submodule/nested_submodule commit -m "add a" &&
+ git -C submodule submodule add ./nested_submodule &&
+ git -C submodule add nested_submodule &&
+ git -C submodule commit -m "added nested_submodule" &&
+ git add submodule &&
+ git commit -m "updated submodule" &&
+ echo "./nested_submodule" >expect &&
+ test-tool submodule-nested-repo-config \
+ submodule submodule.nested_submodule.url >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'reading nested submodules config when .gitmodules is not in the working tree' '
+ test_when_finished "git -C super/submodule checkout .gitmodules" &&
+ (cd super &&
+ echo "./nested_submodule" >expect &&
+ rm submodule/.gitmodules &&
+ test-tool submodule-nested-repo-config \
+ submodule submodule.nested_submodule.url >actual 2>warning &&
+ test_must_be_empty warning &&
+ test_cmp expect actual
+ )
+'
+
+test_done
diff --git a/t/t7412-submodule-absorbgitdirs.sh b/t/t7412-submodule-absorbgitdirs.sh
new file mode 100755
index 0000000..f778321
--- /dev/null
+++ b/t/t7412-submodule-absorbgitdirs.sh
@@ -0,0 +1,184 @@
+#!/bin/sh
+
+test_description='Test submodule absorbgitdirs
+
+This test verifies that `git submodue absorbgitdirs` moves a submodules git
+directory into the superproject.
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup a real submodule' '
+ cwd="$(pwd)" &&
+ git init sub1 &&
+ test_commit -C sub1 first &&
+ git submodule add ./sub1 &&
+ test_tick &&
+ git commit -m superproject
+'
+
+test_expect_success 'absorb the git dir' '
+ >expect &&
+ >actual &&
+ >expect.1 &&
+ >expect.2 &&
+ >actual.1 &&
+ >actual.2 &&
+ git status >expect.1 &&
+ git -C sub1 rev-parse HEAD >expect.2 &&
+ cat >expect <<-EOF &&
+ Migrating git directory of '\''sub1'\'' from
+ '\''$cwd/sub1/.git'\'' to
+ '\''$cwd/.git/modules/sub1'\''
+ EOF
+ git submodule absorbgitdirs 2>actual &&
+ test_cmp expect actual &&
+ git fsck &&
+ test -f sub1/.git &&
+ test -d .git/modules/sub1 &&
+ git status >actual.1 &&
+ git -C sub1 rev-parse HEAD >actual.2 &&
+ test_cmp expect.1 actual.1 &&
+ test_cmp expect.2 actual.2
+'
+
+test_expect_success 'absorbing does not fail for deinitialized submodules' '
+ test_when_finished "git submodule update --init" &&
+ git submodule deinit --all &&
+ git submodule absorbgitdirs 2>err &&
+ test_must_be_empty err &&
+ test -d .git/modules/sub1 &&
+ test -d sub1 &&
+ ! test -e sub1/.git
+'
+
+test_expect_success 'setup nested submodule' '
+ git init sub1/nested &&
+ test_commit -C sub1/nested first_nested &&
+ git -C sub1 submodule add ./nested &&
+ test_tick &&
+ git -C sub1 commit -m "add nested" &&
+ git add sub1 &&
+ git commit -m "sub1 to include nested submodule"
+'
+
+test_expect_success 'absorb the git dir in a nested submodule' '
+ git status >expect.1 &&
+ git -C sub1/nested rev-parse HEAD >expect.2 &&
+ cat >expect <<-EOF &&
+ Migrating git directory of '\''sub1/nested'\'' from
+ '\''$cwd/sub1/nested/.git'\'' to
+ '\''$cwd/.git/modules/sub1/modules/nested'\''
+ EOF
+ git submodule absorbgitdirs 2>actual &&
+ test_cmp expect actual &&
+ test -f sub1/nested/.git &&
+ test -d .git/modules/sub1/modules/nested &&
+ git status >actual.1 &&
+ git -C sub1/nested rev-parse HEAD >actual.2 &&
+ test_cmp expect.1 actual.1 &&
+ test_cmp expect.2 actual.2
+'
+
+test_expect_success 're-setup nested submodule' '
+ # un-absorb the direct submodule, to test if the nested submodule
+ # is still correct (needs a rewrite of the gitfile only)
+ rm -rf sub1/.git &&
+ mv .git/modules/sub1 sub1/.git &&
+ GIT_WORK_TREE=. git -C sub1 config --unset core.worktree &&
+ # fixup the nested submodule
+ echo "gitdir: ../.git/modules/nested" >sub1/nested/.git &&
+ GIT_WORK_TREE=../../../nested git -C sub1/.git/modules/nested config \
+ core.worktree "../../../nested" &&
+ # make sure this re-setup is correct
+ git status --ignore-submodules=none &&
+
+ # also make sure this old setup does not regress
+ git submodule update --init --recursive >out 2>err &&
+ test_must_be_empty out &&
+ test_must_be_empty err
+'
+
+test_expect_success 'absorb the git dir in a nested submodule' '
+ git status >expect.1 &&
+ git -C sub1/nested rev-parse HEAD >expect.2 &&
+ cat >expect <<-EOF &&
+ Migrating git directory of '\''sub1'\'' from
+ '\''$cwd/sub1/.git'\'' to
+ '\''$cwd/.git/modules/sub1'\''
+ EOF
+ git submodule absorbgitdirs 2>actual &&
+ test_cmp expect actual &&
+ test -f sub1/.git &&
+ test -f sub1/nested/.git &&
+ test -d .git/modules/sub1/modules/nested &&
+ git status >actual.1 &&
+ git -C sub1/nested rev-parse HEAD >actual.2 &&
+ test_cmp expect.1 actual.1 &&
+ test_cmp expect.2 actual.2
+'
+
+test_expect_success 'absorb the git dir outside of primary worktree' '
+ test_when_finished "rm -rf repo-bare.git" &&
+ git clone --bare . repo-bare.git &&
+ test_when_finished "rm -rf repo-wt" &&
+ git -C repo-bare.git worktree add ../repo-wt &&
+
+ test_when_finished "rm -f .gitconfig" &&
+ test_config_global protocol.file.allow always &&
+ git -C repo-wt submodule update --init &&
+ git init repo-wt/sub2 &&
+ test_commit -C repo-wt/sub2 A &&
+ git -C repo-wt submodule add ./sub2 sub2 &&
+ cat >expect <<-EOF &&
+ Migrating git directory of '\''sub2'\'' from
+ '\''$cwd/repo-wt/sub2/.git'\'' to
+ '\''$cwd/repo-bare.git/worktrees/repo-wt/modules/sub2'\''
+ EOF
+ git -C repo-wt submodule absorbgitdirs 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'setup a gitlink with missing .gitmodules entry' '
+ git init sub2 &&
+ test_commit -C sub2 first &&
+ git add sub2 &&
+ git commit -m superproject
+'
+
+test_expect_success 'absorbing the git dir fails for incomplete submodules' '
+ git status >expect.1 &&
+ git -C sub2 rev-parse HEAD >expect.2 &&
+ cat >expect <<-\EOF &&
+ fatal: could not lookup name for submodule '\''sub2'\''
+ EOF
+ test_must_fail git submodule absorbgitdirs 2>actual &&
+ test_cmp expect actual &&
+ git -C sub2 fsck &&
+ test -d sub2/.git &&
+ git status >actual &&
+ git -C sub2 rev-parse HEAD >actual.2 &&
+ test_cmp expect.1 actual.1 &&
+ test_cmp expect.2 actual.2
+'
+
+test_expect_success 'setup a submodule with multiple worktrees' '
+ # first create another unembedded git dir in a new submodule
+ git init sub3 &&
+ test_commit -C sub3 first &&
+ git submodule add ./sub3 &&
+ test_tick &&
+ git commit -m "add another submodule" &&
+ git -C sub3 worktree add ../sub3_second_work_tree
+'
+
+test_expect_success 'absorbing fails for a submodule with multiple worktrees' '
+ cat >expect <<-\EOF &&
+ fatal: could not lookup name for submodule '\''sub2'\''
+ EOF
+ test_must_fail git submodule absorbgitdirs 2>actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t7413-submodule-is-active.sh b/t/t7413-submodule-is-active.sh
new file mode 100755
index 0000000..887d181
--- /dev/null
+++ b/t/t7413-submodule-is-active.sh
@@ -0,0 +1,128 @@
+#!/bin/sh
+
+test_description='Test with test-tool submodule is-active
+
+This test verifies that `test-tool submodule is-active` correctly identifies
+submodules which are "active" and interesting to the user.
+
+This is a unit test of the submodule.c is_submodule_active() function,
+which is also indirectly tested elsewhere.
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ git config --global protocol.file.allow always &&
+ git init sub &&
+ test_commit -C sub initial &&
+ git init super &&
+ test_commit -C super initial &&
+ git -C super submodule add ../sub sub1 &&
+ git -C super submodule add ../sub sub2 &&
+
+ # Remove submodule.<name>.active entries in order to test in an
+ # environment where only URLs are present in the conifg
+ git -C super config --unset submodule.sub1.active &&
+ git -C super config --unset submodule.sub2.active &&
+
+ git -C super commit -a -m "add 2 submodules at sub{1,2}"
+'
+
+test_expect_success 'is-active works with urls' '
+ test-tool -C super submodule is-active sub1 &&
+ test-tool -C super submodule is-active sub2 &&
+
+ git -C super config --unset submodule.sub1.URL &&
+ test_must_fail test-tool -C super submodule is-active sub1 &&
+ git -C super config submodule.sub1.URL ../sub &&
+ test-tool -C super submodule is-active sub1
+'
+
+test_expect_success 'is-active works with submodule.<name>.active config' '
+ test_when_finished "git -C super config --unset submodule.sub1.active" &&
+ test_when_finished "git -C super config submodule.sub1.URL ../sub" &&
+
+ git -C super config --bool submodule.sub1.active "false" &&
+ test_must_fail test-tool -C super submodule is-active sub1 &&
+
+ git -C super config --bool submodule.sub1.active "true" &&
+ git -C super config --unset submodule.sub1.URL &&
+ test-tool -C super submodule is-active sub1
+'
+
+test_expect_success 'is-active handles submodule.active config missing a value' '
+ cp super/.git/config super/.git/config.orig &&
+ test_when_finished mv super/.git/config.orig super/.git/config &&
+
+ cat >>super/.git/config <<-\EOF &&
+ [submodule]
+ active
+ EOF
+
+ cat >expect <<-\EOF &&
+ error: missing value for '\''submodule.active'\''
+ EOF
+ test-tool -C super submodule is-active sub1 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'is-active works with basic submodule.active config' '
+ test_when_finished "git -C super config submodule.sub1.URL ../sub" &&
+ test_when_finished "git -C super config --unset-all submodule.active" &&
+
+ git -C super config --add submodule.active "." &&
+ git -C super config --unset submodule.sub1.URL &&
+
+ test-tool -C super submodule is-active sub1 &&
+ test-tool -C super submodule is-active sub2
+'
+
+test_expect_success 'is-active correctly works with paths that are not submodules' '
+ test_when_finished "git -C super config --unset-all submodule.active" &&
+
+ test_must_fail test-tool -C super submodule is-active not-a-submodule &&
+
+ git -C super config --add submodule.active "." &&
+ test_must_fail test-tool -C super submodule is-active not-a-submodule
+'
+
+test_expect_success 'is-active works with exclusions in submodule.active config' '
+ test_when_finished "git -C super config --unset-all submodule.active" &&
+
+ git -C super config --add submodule.active "." &&
+ git -C super config --add submodule.active ":(exclude)sub1" &&
+
+ test_must_fail test-tool -C super submodule is-active sub1 &&
+ test-tool -C super submodule is-active sub2
+'
+
+test_expect_success 'is-active with submodule.active and submodule.<name>.active' '
+ test_when_finished "git -C super config --unset-all submodule.active" &&
+ test_when_finished "git -C super config --unset submodule.sub1.active" &&
+ test_when_finished "git -C super config --unset submodule.sub2.active" &&
+
+ git -C super config --add submodule.active "sub1" &&
+ git -C super config --bool submodule.sub1.active "false" &&
+ git -C super config --bool submodule.sub2.active "true" &&
+
+ test_must_fail test-tool -C super submodule is-active sub1 &&
+ test-tool -C super submodule is-active sub2
+'
+
+test_expect_success 'is-active, submodule.active and submodule add' '
+ test_when_finished "rm -rf super2" &&
+ git init super2 &&
+ test_commit -C super2 initial &&
+ git -C super2 config --add submodule.active "sub*" &&
+
+ # submodule add should only add submodule.<name>.active
+ # to the config if not matched by the pathspec
+ git -C super2 submodule add ../sub sub1 &&
+ test_must_fail git -C super2 config --get submodule.sub1.active &&
+
+ git -C super2 submodule add ../sub mod &&
+ git -C super2 config --get submodule.mod.active
+'
+
+test_done
diff --git a/t/t7414-submodule-mistakes.sh b/t/t7414-submodule-mistakes.sh
new file mode 100755
index 0000000..24f30e3
--- /dev/null
+++ b/t/t7414-submodule-mistakes.sh
@@ -0,0 +1,40 @@
+#!/bin/sh
+
+test_description='handling of common mistakes people may make with submodules'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'create embedded repository' '
+ git init embed &&
+ test_commit -C embed one
+'
+
+test_expect_success 'git-add on embedded repository warns' '
+ test_when_finished "git rm --cached -f embed" &&
+ git add embed 2>stderr &&
+ test_grep warning stderr
+'
+
+test_expect_success '--no-warn-embedded-repo suppresses warning' '
+ test_when_finished "git rm --cached -f embed" &&
+ git add --no-warn-embedded-repo embed 2>stderr &&
+ test_grep ! warning stderr
+'
+
+test_expect_success 'no warning when updating entry' '
+ test_when_finished "git rm --cached -f embed" &&
+ git add embed &&
+ git -C embed commit --allow-empty -m two &&
+ git add embed 2>stderr &&
+ test_grep ! warning stderr
+'
+
+test_expect_success 'submodule add does not warn' '
+ test_when_finished "git rm -rf submodule .gitmodules" &&
+ git -c protocol.file.allow=always \
+ submodule add ./embed submodule 2>stderr &&
+ test_grep ! warning stderr
+'
+
+test_done
diff --git a/t/t7416-submodule-dash-url.sh b/t/t7416-submodule-dash-url.sh
new file mode 100755
index 0000000..2ab566e
--- /dev/null
+++ b/t/t7416-submodule-dash-url.sh
@@ -0,0 +1,225 @@
+#!/bin/sh
+
+test_description='check handling of disallowed .gitmodule urls'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ git config --global protocol.file.allow always
+'
+
+test_expect_success 'create submodule with protected dash in url' '
+ git init upstream &&
+ git -C upstream commit --allow-empty -m base &&
+ mv upstream ./-upstream &&
+ git submodule add ./-upstream sub &&
+ git add sub .gitmodules &&
+ git commit -m submodule
+'
+
+test_expect_success 'clone can recurse submodule' '
+ test_when_finished "rm -rf dst" &&
+ git clone --recurse-submodules . dst &&
+ echo base >expect &&
+ git -C dst/sub log -1 --format=%s >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'fsck accepts protected dash' '
+ test_when_finished "rm -rf dst" &&
+ git init --bare dst &&
+ git -C dst config transfer.fsckObjects true &&
+ git push dst HEAD
+'
+
+test_expect_success 'remove ./ protection from .gitmodules url' '
+ perl -i -pe "s{\./}{}" .gitmodules &&
+ git commit -am "drop protection"
+'
+
+test_expect_success 'clone rejects unprotected dash' '
+ test_when_finished "rm -rf dst" &&
+ test_must_fail git clone --recurse-submodules . dst 2>err &&
+ test_grep ignoring err
+'
+
+test_expect_success 'fsck rejects unprotected dash' '
+ test_when_finished "rm -rf dst" &&
+ git init --bare dst &&
+ git -C dst config transfer.fsckObjects true &&
+ test_must_fail git push dst HEAD 2>err &&
+ grep gitmodulesUrl err
+'
+
+test_expect_success 'trailing backslash is handled correctly' '
+ git init testmodule &&
+ test_commit -C testmodule c &&
+ git submodule add ./testmodule &&
+ : ensure that the name ends in a double backslash &&
+ sed -e "s|\\(submodule \"testmodule\\)\"|\\1\\\\\\\\\"|" \
+ -e "s|url = .*|url = \" --should-not-be-an-option\"|" \
+ <.gitmodules >.new &&
+ mv .new .gitmodules &&
+ git commit -am "Add testmodule" &&
+ test_must_fail git clone --verbose --recurse-submodules . dolly 2>err &&
+ test_grep ! "unknown option" err
+'
+
+test_expect_success 'fsck rejects missing URL scheme' '
+ git checkout --orphan missing-scheme &&
+ cat >.gitmodules <<-\EOF &&
+ [submodule "foo"]
+ url = http::one.example.com/foo.git
+ EOF
+ git add .gitmodules &&
+ test_tick &&
+ git commit -m "gitmodules with missing URL scheme" &&
+ test_when_finished "rm -rf dst" &&
+ git init --bare dst &&
+ git -C dst config transfer.fsckObjects true &&
+ test_must_fail git push dst HEAD 2>err &&
+ grep gitmodulesUrl err
+'
+
+test_expect_success 'fsck rejects relative URL resolving to missing scheme' '
+ git checkout --orphan relative-missing-scheme &&
+ cat >.gitmodules <<-\EOF &&
+ [submodule "foo"]
+ url = "..\\../.\\../:one.example.com/foo.git"
+ EOF
+ git add .gitmodules &&
+ test_tick &&
+ git commit -m "gitmodules with relative URL that strips off scheme" &&
+ test_when_finished "rm -rf dst" &&
+ git init --bare dst &&
+ git -C dst config transfer.fsckObjects true &&
+ test_must_fail git push dst HEAD 2>err &&
+ grep gitmodulesUrl err
+'
+
+test_expect_success 'fsck rejects empty URL scheme' '
+ git checkout --orphan empty-scheme &&
+ cat >.gitmodules <<-\EOF &&
+ [submodule "foo"]
+ url = http::://one.example.com/foo.git
+ EOF
+ git add .gitmodules &&
+ test_tick &&
+ git commit -m "gitmodules with empty URL scheme" &&
+ test_when_finished "rm -rf dst" &&
+ git init --bare dst &&
+ git -C dst config transfer.fsckObjects true &&
+ test_must_fail git push dst HEAD 2>err &&
+ grep gitmodulesUrl err
+'
+
+test_expect_success 'fsck rejects relative URL resolving to empty scheme' '
+ git checkout --orphan relative-empty-scheme &&
+ cat >.gitmodules <<-\EOF &&
+ [submodule "foo"]
+ url = ../../../:://one.example.com/foo.git
+ EOF
+ git add .gitmodules &&
+ test_tick &&
+ git commit -m "relative gitmodules URL resolving to empty scheme" &&
+ test_when_finished "rm -rf dst" &&
+ git init --bare dst &&
+ git -C dst config transfer.fsckObjects true &&
+ test_must_fail git push dst HEAD 2>err &&
+ grep gitmodulesUrl err
+'
+
+test_expect_success 'fsck rejects empty hostname' '
+ git checkout --orphan empty-host &&
+ cat >.gitmodules <<-\EOF &&
+ [submodule "foo"]
+ url = http:///one.example.com/foo.git
+ EOF
+ git add .gitmodules &&
+ test_tick &&
+ git commit -m "gitmodules with extra slashes" &&
+ test_when_finished "rm -rf dst" &&
+ git init --bare dst &&
+ git -C dst config transfer.fsckObjects true &&
+ test_must_fail git push dst HEAD 2>err &&
+ grep gitmodulesUrl err
+'
+
+test_expect_success 'fsck rejects relative url that produced empty hostname' '
+ git checkout --orphan messy-relative &&
+ cat >.gitmodules <<-\EOF &&
+ [submodule "foo"]
+ url = ../../..//one.example.com/foo.git
+ EOF
+ git add .gitmodules &&
+ test_tick &&
+ git commit -m "gitmodules abusing relative_path" &&
+ test_when_finished "rm -rf dst" &&
+ git init --bare dst &&
+ git -C dst config transfer.fsckObjects true &&
+ test_must_fail git push dst HEAD 2>err &&
+ grep gitmodulesUrl err
+'
+
+test_expect_success 'fsck permits embedded newline with unrecognized scheme' '
+ git checkout --orphan newscheme &&
+ cat >.gitmodules <<-\EOF &&
+ [submodule "foo"]
+ url = "data://acjbkd%0akajfdickajkd"
+ EOF
+ git add .gitmodules &&
+ git commit -m "gitmodules with unrecognized scheme" &&
+ test_when_finished "rm -rf dst" &&
+ git init --bare dst &&
+ git -C dst config transfer.fsckObjects true &&
+ git push dst HEAD
+'
+
+test_expect_success 'fsck rejects embedded newline in url' '
+ # create an orphan branch to avoid existing .gitmodules objects
+ git checkout --orphan newline &&
+ cat >.gitmodules <<-\EOF &&
+ [submodule "foo"]
+ url = "https://one.example.com?%0ahost=two.example.com/foo.git"
+ EOF
+ git add .gitmodules &&
+ git commit -m "gitmodules with newline" &&
+ test_when_finished "rm -rf dst" &&
+ git init --bare dst &&
+ git -C dst config transfer.fsckObjects true &&
+ test_must_fail git push dst HEAD 2>err &&
+ grep gitmodulesUrl err
+'
+
+test_expect_success 'fsck rejects embedded newline in relative url' '
+ git checkout --orphan relative-newline &&
+ cat >.gitmodules <<-\EOF &&
+ [submodule "foo"]
+ url = "./%0ahost=two.example.com/foo.git"
+ EOF
+ git add .gitmodules &&
+ git commit -m "relative url with newline" &&
+ test_when_finished "rm -rf dst" &&
+ git init --bare dst &&
+ git -C dst config transfer.fsckObjects true &&
+ test_must_fail git push dst HEAD 2>err &&
+ grep gitmodulesUrl err
+'
+
+test_expect_success 'fsck rejects embedded newline in git url' '
+ git checkout --orphan git-newline &&
+ cat >.gitmodules <<-\EOF &&
+ [submodule "foo"]
+ url = "git://example.com:1234/repo%0a.git"
+ EOF
+ git add .gitmodules &&
+ git commit -m "git url with newline" &&
+ test_when_finished "rm -rf dst" &&
+ git init --bare dst &&
+ git -C dst config transfer.fsckObjects true &&
+ test_must_fail git push dst HEAD 2>err &&
+ grep gitmodulesUrl err
+'
+
+test_done
diff --git a/t/t7417-submodule-path-url.sh b/t/t7417-submodule-path-url.sh
new file mode 100755
index 0000000..5e3051d
--- /dev/null
+++ b/t/t7417-submodule-path-url.sh
@@ -0,0 +1,52 @@
+#!/bin/sh
+
+test_description='check handling of .gitmodule path with dash'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ git config --global protocol.file.allow always
+'
+
+test_expect_success 'create submodule with dash in path' '
+ git init upstream &&
+ git -C upstream commit --allow-empty -m base &&
+ git submodule add ./upstream sub &&
+ git mv sub ./-sub &&
+ git commit -m submodule
+'
+
+test_expect_success 'clone rejects unprotected dash' '
+ test_when_finished "rm -rf dst" &&
+ git clone --recurse-submodules . dst 2>err &&
+ test_grep ignoring err
+'
+
+test_expect_success 'fsck rejects unprotected dash' '
+ test_when_finished "rm -rf dst" &&
+ git init --bare dst &&
+ git -C dst config transfer.fsckObjects true &&
+ test_must_fail git push dst HEAD 2>err &&
+ grep gitmodulesPath err
+'
+
+test_expect_success MINGW 'submodule paths disallows trailing spaces' '
+ git init super &&
+ test_must_fail git -C super submodule add ../upstream "sub " &&
+
+ : add "sub", then rename "sub" to "sub ", the hard way &&
+ git -C super submodule add ../upstream sub &&
+ tree=$(git -C super write-tree) &&
+ git -C super ls-tree $tree >tree &&
+ sed "s/sub/sub /" <tree >tree.new &&
+ tree=$(git -C super mktree <tree.new) &&
+ commit=$(echo with space | git -C super commit-tree $tree) &&
+ git -C super update-ref refs/heads/main $commit &&
+
+ test_must_fail git clone --recurse-submodules super dst 2>err &&
+ test_grep "sub " err
+'
+
+test_done
diff --git a/t/t7418-submodule-sparse-gitmodules.sh b/t/t7418-submodule-sparse-gitmodules.sh
new file mode 100755
index 0000000..dde11ec
--- /dev/null
+++ b/t/t7418-submodule-sparse-gitmodules.sh
@@ -0,0 +1,130 @@
+#!/bin/sh
+#
+# Copyright (C) 2018 Antonio Ospite <ao2@ao2.it>
+#
+
+test_description='Test reading/writing .gitmodules when not in the working tree
+
+This test verifies that, when .gitmodules is in the current branch but is not
+in the working tree reading from it still works but writing to it does not.
+
+The test setup uses a sparse checkout, however the same scenario can be set up
+also by committing .gitmodules and then just removing it from the filesystem.
+'
+
+GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB=1
+export GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ git config --global protocol.file.allow always
+'
+
+test_expect_success 'sparse checkout setup which hides .gitmodules' '
+ git init upstream &&
+ git init submodule &&
+ (cd submodule &&
+ echo file >file &&
+ git add file &&
+ test_tick &&
+ git commit -m "Add file"
+ ) &&
+ (cd upstream &&
+ git submodule add ../submodule &&
+ test_tick &&
+ git commit -m "Add submodule"
+ ) &&
+ git clone --template= upstream super &&
+ (cd super &&
+ mkdir .git/info &&
+ cat >.git/info/sparse-checkout <<-\EOF &&
+ /*
+ !/.gitmodules
+ EOF
+ git config core.sparsecheckout true &&
+ git read-tree -m -u HEAD &&
+ test_path_is_missing .gitmodules
+ )
+'
+
+test_expect_success 'reading gitmodules config file when it is not checked out' '
+ echo "../submodule" >expect &&
+ test-tool -C super submodule config-list submodule.submodule.url >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'not writing gitmodules config file when it is not checked out' '
+ test_must_fail test-tool -C super submodule config-set submodule.submodule.url newurl &&
+ test_path_is_missing super/.gitmodules
+'
+
+test_expect_success 'initialising submodule when the gitmodules config is not checked out' '
+ test_must_fail git -C super config submodule.submodule.url &&
+ git -C super submodule init &&
+ git -C super config submodule.submodule.url >actual &&
+ echo "$(pwd)/submodule" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'updating submodule when the gitmodules config is not checked out' '
+ test_path_is_missing super/submodule/file &&
+ git -C super submodule update &&
+ test_cmp submodule/file super/submodule/file
+'
+
+ORIG_SUBMODULE=$(git -C submodule rev-parse HEAD)
+ORIG_UPSTREAM=$(git -C upstream rev-parse HEAD)
+ORIG_SUPER=$(git -C super rev-parse HEAD)
+
+test_expect_success 're-updating submodule when the gitmodules config is not checked out' '
+ test_when_finished "git -C submodule reset --hard $ORIG_SUBMODULE;
+ git -C upstream reset --hard $ORIG_UPSTREAM;
+ git -C super reset --hard $ORIG_SUPER;
+ git -C upstream submodule update --remote;
+ git -C super pull;
+ git -C super submodule update --remote" &&
+ (cd submodule &&
+ echo file2 >file2 &&
+ git add file2 &&
+ test_tick &&
+ git commit -m "Add file2 to submodule"
+ ) &&
+ (cd upstream &&
+ git submodule update --remote &&
+ git add submodule &&
+ test_tick &&
+ git commit -m "Update submodule"
+ ) &&
+ git -C super pull &&
+ # The --for-status options reads the gitmodules config
+ git -C super submodule summary --for-status >actual &&
+ rev1=$(git -C submodule rev-parse --short HEAD) &&
+ rev2=$(git -C submodule rev-parse --short HEAD^) &&
+ cat >expect <<-EOF &&
+ * submodule ${rev1}...${rev2} (1):
+ < Add file2 to submodule
+
+ EOF
+ test_cmp expect actual &&
+ # Test that the update actually succeeds
+ test_path_is_missing super/submodule/file2 &&
+ git -C super submodule update &&
+ test_cmp submodule/file2 super/submodule/file2 &&
+ git -C super status --short >output &&
+ test_must_be_empty output
+'
+
+test_expect_success 'not adding submodules when the gitmodules config is not checked out' '
+ git clone submodule new_submodule &&
+ test_must_fail git -C super submodule add ../new_submodule &&
+ test_path_is_missing .gitmodules
+'
+
+# This test checks that the previous "git submodule add" did not leave the
+# repository in a spurious state when it failed.
+test_expect_success 'init submodule still works even after the previous add failed' '
+ git -C super submodule init
+'
+
+test_done
diff --git a/t/t7419-submodule-set-branch.sh b/t/t7419-submodule-set-branch.sh
new file mode 100755
index 0000000..a5d1bc5
--- /dev/null
+++ b/t/t7419-submodule-set-branch.sh
@@ -0,0 +1,131 @@
+#!/bin/sh
+#
+# Copyright (c) 2019 Denton Liu
+#
+
+test_description='Test submodules set-branch subcommand
+
+This test verifies that the set-branch subcommand of git-submodule is working
+as expected.
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+TEST_NO_CREATE_REPO=1
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ git config --global protocol.file.allow always
+'
+
+test_expect_success 'submodule config cache setup' '
+ mkdir submodule &&
+ (cd submodule &&
+ git init &&
+ echo a >a &&
+ git add . &&
+ git commit -ma &&
+ git checkout -b topic &&
+ echo b >a &&
+ git add . &&
+ git commit -mb &&
+ git checkout main
+ ) &&
+ mkdir super &&
+ (cd super &&
+ git init &&
+ git submodule add ../submodule &&
+ git submodule add --name thename ../submodule thepath &&
+ git commit -m "add submodules"
+ )
+'
+
+test_expect_success 'ensure submodule branch is unset' '
+ (cd super &&
+ test_cmp_config "" -f .gitmodules --default "" submodule.submodule.branch
+ )
+'
+
+test_expect_success 'test submodule set-branch --branch' '
+ (cd super &&
+ git submodule set-branch --branch topic submodule &&
+ test_cmp_config topic -f .gitmodules submodule.submodule.branch &&
+ git submodule update --remote &&
+ cat <<-\EOF >expect &&
+ b
+ EOF
+ git -C submodule show -s --pretty=%s >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'test submodule set-branch --default' '
+ (cd super &&
+ git submodule set-branch --default submodule &&
+ test_cmp_config "" -f .gitmodules --default "" submodule.submodule.branch &&
+ git submodule update --remote &&
+ cat <<-\EOF >expect &&
+ a
+ EOF
+ git -C submodule show -s --pretty=%s >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'test submodule set-branch -b' '
+ (cd super &&
+ git submodule set-branch -b topic submodule &&
+ test_cmp_config topic -f .gitmodules submodule.submodule.branch &&
+ git submodule update --remote &&
+ cat <<-\EOF >expect &&
+ b
+ EOF
+ git -C submodule show -s --pretty=%s >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'test submodule set-branch -d' '
+ (cd super &&
+ git submodule set-branch -d submodule &&
+ test_cmp_config "" -f .gitmodules --default "" submodule.submodule.branch &&
+ git submodule update --remote &&
+ cat <<-\EOF >expect &&
+ a
+ EOF
+ git -C submodule show -s --pretty=%s >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'test submodule set-branch --branch with named submodule' '
+ (cd super &&
+ git submodule set-branch --branch topic thepath &&
+ test_cmp_config topic -f .gitmodules submodule.thename.branch &&
+ test_cmp_config "" -f .gitmodules --default "" submodule.thepath.branch &&
+ git submodule update --remote &&
+ cat <<-\EOF >expect &&
+ b
+ EOF
+ git -C thepath show -s --pretty=%s >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'test submodule set-branch --default with named submodule' '
+ (cd super &&
+ git submodule set-branch --default thepath &&
+ test_cmp_config "" -f .gitmodules --default "" submodule.thename.branch &&
+ git submodule update --remote &&
+ cat <<-\EOF >expect &&
+ a
+ EOF
+ git -C thepath show -s --pretty=%s >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_done
diff --git a/t/t7420-submodule-set-url.sh b/t/t7420-submodule-set-url.sh
new file mode 100755
index 0000000..bf7f15e
--- /dev/null
+++ b/t/t7420-submodule-set-url.sh
@@ -0,0 +1,81 @@
+#!/bin/sh
+#
+# Copyright (c) 2019 Denton Liu
+#
+
+test_description='Test submodules set-url subcommand
+
+This test verifies that the set-url subcommand of git-submodule is working
+as expected.
+'
+
+TEST_NO_CREATE_REPO=1
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ git config --global protocol.file.allow always
+'
+
+test_expect_success 'submodule config cache setup' '
+ mkdir submodule &&
+ (
+ cd submodule &&
+ git init &&
+ echo a >file &&
+ git add file &&
+ git commit -ma
+ ) &&
+ mkdir namedsubmodule &&
+ (
+ cd namedsubmodule &&
+ git init &&
+ echo 1 >file &&
+ git add file &&
+ git commit -m1
+ ) &&
+ mkdir super &&
+ (
+ cd super &&
+ git init &&
+ git submodule add ../submodule &&
+ git submodule add --name thename ../namedsubmodule thepath &&
+ git commit -m "add submodules"
+ )
+'
+
+test_expect_success 'test submodule set-url' '
+ # add commits and move the submodules (change the urls)
+ (
+ cd submodule &&
+ echo b >>file &&
+ git add file &&
+ git commit -mb
+ ) &&
+ mv submodule newsubmodule &&
+
+ (
+ cd namedsubmodule &&
+ echo 2 >>file &&
+ git add file &&
+ git commit -m2
+ ) &&
+ mv namedsubmodule newnamedsubmodule &&
+
+ git -C newsubmodule show >expect &&
+ git -C newnamedsubmodule show >>expect &&
+ (
+ cd super &&
+ test_must_fail git submodule update --remote &&
+ git submodule set-url submodule ../newsubmodule &&
+ test_cmp_config ../newsubmodule -f .gitmodules submodule.submodule.url &&
+ git submodule set-url thepath ../newnamedsubmodule &&
+ test_cmp_config ../newnamedsubmodule -f .gitmodules submodule.thename.url &&
+ test_cmp_config "" -f .gitmodules --default "" submodule.thepath.url &&
+ git submodule update --remote
+ ) &&
+ git -C super/submodule show >actual &&
+ git -C super/thepath show >>actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t7421-submodule-summary-add.sh b/t/t7421-submodule-summary-add.sh
new file mode 100755
index 0000000..ce64d8b
--- /dev/null
+++ b/t/t7421-submodule-summary-add.sh
@@ -0,0 +1,73 @@
+#!/bin/sh
+#
+# Copyright (C) 2020 Shourya Shukla
+#
+
+test_description='Summary support for submodules, adding them using git submodule add
+
+This test script tries to verify the sanity of summary subcommand of git submodule
+while making sure to add submodules using `git submodule add` instead of
+`git add` as done in t7401.
+'
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ git config --global protocol.file.allow always
+'
+
+test_expect_success 'summary test environment setup' '
+ git init sm &&
+ test_commit -C sm "add file" file file-content file-tag &&
+
+ git submodule add ./sm my-subm &&
+ test_tick &&
+ git commit -m "add submodule"
+'
+
+test_expect_success 'submodule summary output for initialized submodule' '
+ test_commit -C sm "add file2" file2 file2-content file2-tag &&
+ git submodule update --remote &&
+ test_tick &&
+ git commit -m "update submodule" my-subm &&
+ git submodule summary HEAD^ >actual &&
+ rev1=$(git -C sm rev-parse --short HEAD^) &&
+ rev2=$(git -C sm rev-parse --short HEAD) &&
+ cat >expected <<-EOF &&
+ * my-subm ${rev1}...${rev2} (1):
+ > add file2
+
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'submodule summary output for deinitialized submodule' '
+ git submodule deinit my-subm &&
+ git submodule summary HEAD^ >actual &&
+ test_must_be_empty actual &&
+ git submodule update --init my-subm &&
+ git submodule summary HEAD^ >actual &&
+ rev1=$(git -C sm rev-parse --short HEAD^) &&
+ rev2=$(git -C sm rev-parse --short HEAD) &&
+ cat >expected <<-EOF &&
+ * my-subm ${rev1}...${rev2} (1):
+ > add file2
+
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'submodule summary output for submodules with changed paths' '
+ git mv my-subm subm &&
+ git commit -m "change submodule path" &&
+ rev=$(git -C sm rev-parse --short HEAD^) &&
+ git submodule summary HEAD^^ -- my-subm >actual 2>err &&
+ test_must_be_empty err &&
+ cat >expected <<-EOF &&
+ * my-subm ${rev}...0000000:
+
+ EOF
+ test_cmp expected actual
+'
+
+test_done
diff --git a/t/t7422-submodule-output.sh b/t/t7422-submodule-output.sh
new file mode 100755
index 0000000..ab946ec
--- /dev/null
+++ b/t/t7422-submodule-output.sh
@@ -0,0 +1,170 @@
+#!/bin/sh
+
+test_description='submodule --cached, --quiet etc. output'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-t3100.sh
+
+setup_sub () {
+ local d="$1" &&
+ shift &&
+ git $@ clone . "$d" &&
+ git $@ submodule add ./"$d"
+}
+
+normalize_status () {
+ sed -e 's/-g[0-9a-f]*/-gHASH/'
+}
+
+test_expect_success 'setup' '
+ test_commit A &&
+ test_commit B &&
+ setup_sub S &&
+ setup_sub S.D &&
+ setup_sub S.C &&
+ setup_sub S.C.D &&
+ setup_sub X &&
+ git add S* &&
+ test_commit C &&
+
+ # recursive in X/
+ git -C X pull &&
+ GIT_ALLOW_PROTOCOL=file git -C X submodule update --init &&
+
+ # dirty
+ for d in S.D X/S.D
+ do
+ echo dirty >"$d"/A.t || return 1
+ done &&
+
+ # commit (for --cached)
+ for d in S.C* X/S.C*
+ do
+ git -C "$d" reset --hard A || return 1
+ done &&
+
+ # dirty
+ for d in S*.D X/S*.D
+ do
+ echo dirty >"$d/C2.t" || return 1
+ done &&
+
+ for ref in A B C
+ do
+ # Not different with SHA-1 and SHA-256, just (ab)using
+ # test_oid_cache as a variable bag to avoid using
+ # $(git rev-parse ...).
+ oid=$(git rev-parse $ref) &&
+ test_oid_cache <<-EOF || return 1
+ $ref sha1:$oid
+ $ref sha256:$oid
+ EOF
+ done
+'
+
+for opts in "" "status"
+do
+ test_expect_success "git submodule $opts" '
+ sed -e "s/^>//" >expect <<-EOF &&
+ > $(test_oid B) S (B)
+ >+$(test_oid A) S.C (A)
+ >+$(test_oid A) S.C.D (A)
+ > $(test_oid B) S.D (B)
+ >+$(test_oid C) X (C)
+ EOF
+ git submodule $opts >actual.raw &&
+ normalize_status <actual.raw >actual &&
+ test_cmp expect actual
+ '
+done
+
+for opts in \
+ "status --recursive"
+do
+ test_expect_success "git submodule $opts" '
+ sed -e "s/^>//" >expect <<-EOF &&
+ > $(test_oid B) S (B)
+ >+$(test_oid A) S.C (A)
+ >+$(test_oid A) S.C.D (A)
+ > $(test_oid B) S.D (B)
+ >+$(test_oid C) X (C)
+ > $(test_oid B) X/S (B)
+ >+$(test_oid A) X/S.C (A)
+ >+$(test_oid A) X/S.C.D (A)
+ > $(test_oid B) X/S.D (B)
+ > $(test_oid B) X/X (B)
+ EOF
+ git submodule $opts >actual.raw &&
+ normalize_status <actual.raw >actual &&
+ test_cmp expect actual
+ '
+done
+
+for opts in \
+ "--quiet" \
+ "--quiet status" \
+ "status --quiet"
+do
+ test_expect_success "git submodule $opts" '
+ git submodule $opts >out &&
+ test_must_be_empty out
+ '
+done
+
+for opts in \
+ "--cached" \
+ "--cached status" \
+ "status --cached"
+do
+ test_expect_success "git submodule $opts" '
+ sed -e "s/^>//" >expect <<-EOF &&
+ > $(test_oid B) S (B)
+ >+$(test_oid B) S.C (B)
+ >+$(test_oid B) S.C.D (B)
+ > $(test_oid B) S.D (B)
+ >+$(test_oid B) X (B)
+ EOF
+ git submodule $opts >actual.raw &&
+ normalize_status <actual.raw >actual &&
+ test_cmp expect actual
+ '
+done
+
+for opts in \
+ "--cached --quiet" \
+ "--cached --quiet status" \
+ "--cached status --quiet" \
+ "--quiet status --cached" \
+ "status --cached --quiet"
+do
+ test_expect_success "git submodule $opts" '
+ git submodule $opts >out &&
+ test_must_be_empty out
+ '
+done
+
+for opts in \
+ "status --cached --recursive" \
+ "--cached status --recursive"
+do
+ test_expect_success "git submodule $opts" '
+ sed -e "s/^>//" >expect <<-EOF &&
+ > $(test_oid B) S (B)
+ >+$(test_oid B) S.C (B)
+ >+$(test_oid B) S.C.D (B)
+ > $(test_oid B) S.D (B)
+ >+$(test_oid B) X (B)
+ > $(test_oid B) X/S (B)
+ >+$(test_oid B) X/S.C (B)
+ >+$(test_oid B) X/S.C.D (B)
+ > $(test_oid B) X/S.D (B)
+ > $(test_oid B) X/X (B)
+ EOF
+ git submodule $opts >actual.raw &&
+ normalize_status <actual.raw >actual &&
+ test_cmp expect actual
+ '
+done
+
+test_done
diff --git a/t/t7450-bad-git-dotfiles.sh b/t/t7450-bad-git-dotfiles.sh
new file mode 100755
index 0000000..35a31ac
--- /dev/null
+++ b/t/t7450-bad-git-dotfiles.sh
@@ -0,0 +1,320 @@
+#!/bin/sh
+
+test_description='check broken or malicious patterns in .git* files
+
+Such as:
+
+ - presence of .. in submodule names;
+ Exercise the name-checking function on a variety of names, and then give a
+ real-world setup that confirms we catch this in practice.
+
+ - nested submodule names
+
+ - symlinked .gitmodules, etc
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-pack.sh
+
+test_expect_success 'setup' '
+ git config --global protocol.file.allow always
+'
+
+test_expect_success 'check names' '
+ cat >expect <<-\EOF &&
+ valid
+ valid/with/paths
+ EOF
+
+ test-tool submodule check-name >actual <<-\EOF &&
+ valid
+ valid/with/paths
+
+ ../foo
+ /../foo
+ ..\foo
+ \..\foo
+ foo/..
+ foo/../
+ foo\..
+ foo\..\
+ foo/../bar
+ EOF
+
+ test_cmp expect actual
+'
+
+test_expect_success 'create innocent subrepo' '
+ git init innocent &&
+ git -C innocent commit --allow-empty -m foo
+'
+
+test_expect_success 'submodule add refuses invalid names' '
+ test_must_fail \
+ git submodule add --name ../../modules/evil "$PWD/innocent" evil
+'
+
+test_expect_success 'add evil submodule' '
+ git submodule add "$PWD/innocent" evil &&
+
+ mkdir modules &&
+ cp -r .git/modules/evil modules &&
+ write_script modules/evil/hooks/post-checkout <<-\EOF &&
+ echo >&2 "RUNNING POST CHECKOUT"
+ EOF
+
+ git config -f .gitmodules submodule.evil.update checkout &&
+ git config -f .gitmodules --rename-section \
+ submodule.evil submodule.../../modules/evil &&
+ git add modules &&
+ git commit -am evil
+'
+
+# This step seems like it shouldn't be necessary, since the payload is
+# contained entirely in the evil submodule. But due to the vagaries of the
+# submodule code, checking out the evil module will fail unless ".git/modules"
+# exists. Adding another submodule (with a name that sorts before "evil") is an
+# easy way to make sure this is the case in the victim clone.
+test_expect_success 'add other submodule' '
+ git submodule add "$PWD/innocent" another-module &&
+ git add another-module &&
+ git commit -am another
+'
+
+test_expect_success 'clone evil superproject' '
+ git clone --recurse-submodules . victim >output 2>&1 &&
+ ! grep "RUNNING POST CHECKOUT" output
+'
+
+test_expect_success 'fsck detects evil superproject' '
+ test_must_fail git fsck
+'
+
+test_expect_success 'transfer.fsckObjects detects evil superproject (unpack)' '
+ rm -rf dst.git &&
+ git init --bare dst.git &&
+ git -C dst.git config transfer.fsckObjects true &&
+ test_must_fail git push dst.git HEAD
+'
+
+test_expect_success 'transfer.fsckObjects detects evil superproject (index)' '
+ rm -rf dst.git &&
+ git init --bare dst.git &&
+ git -C dst.git config transfer.fsckObjects true &&
+ git -C dst.git config transfer.unpackLimit 1 &&
+ test_must_fail git push dst.git HEAD
+'
+
+# Normally our packs contain commits followed by trees followed by blobs. This
+# reverses the order, which requires backtracking to find the context of a
+# blob. We'll start with a fresh gitmodules-only tree to make it simpler.
+test_expect_success 'create oddly ordered pack' '
+ git checkout --orphan odd &&
+ git rm -rf --cached . &&
+ git add .gitmodules &&
+ git commit -m odd &&
+ {
+ pack_header 3 &&
+ pack_obj $(git rev-parse HEAD:.gitmodules) &&
+ pack_obj $(git rev-parse HEAD^{tree}) &&
+ pack_obj $(git rev-parse HEAD)
+ } >odd.pack &&
+ pack_trailer odd.pack
+'
+
+test_expect_success 'transfer.fsckObjects handles odd pack (unpack)' '
+ rm -rf dst.git &&
+ git init --bare dst.git &&
+ test_must_fail git -C dst.git unpack-objects --strict <odd.pack
+'
+
+test_expect_success 'transfer.fsckObjects handles odd pack (index)' '
+ rm -rf dst.git &&
+ git init --bare dst.git &&
+ test_must_fail git -C dst.git index-pack --strict --stdin <odd.pack
+'
+
+test_expect_success 'index-pack --strict works for non-repo pack' '
+ rm -rf dst.git &&
+ git init --bare dst.git &&
+ cp odd.pack dst.git &&
+ test_must_fail git -C dst.git index-pack --strict odd.pack 2>output &&
+ # Make sure we fail due to bad gitmodules content, not because we
+ # could not read the blob in the first place.
+ grep gitmodulesName output
+'
+
+check_dotx_symlink () {
+ fsck_must_fail=test_must_fail
+ fsck_prefix=error
+ refuse_index=t
+ case "$1" in
+ --warning)
+ fsck_must_fail=
+ fsck_prefix=warning
+ refuse_index=
+ shift
+ ;;
+ esac
+
+ name=$1
+ type=$2
+ path=$3
+ dir=symlink-$name-$type
+
+ test_expect_success "set up repo with symlinked $name ($type)" '
+ git init $dir &&
+ (
+ cd $dir &&
+
+ # Make the tree directly to avoid index restrictions.
+ #
+ # Because symlinks store the target as a blob, choose
+ # a pathname that could be parsed as a .gitmodules file
+ # to trick naive non-symlink-aware checking.
+ tricky="[foo]bar=true" &&
+ content=$(git hash-object -w ../.gitmodules) &&
+ target=$(printf "$tricky" | git hash-object -w --stdin) &&
+ {
+ printf "100644 blob $content\t$tricky\n" &&
+ printf "120000 blob $target\t$path\n"
+ } >bad-tree
+ ) &&
+ tree=$(git -C $dir mktree <$dir/bad-tree)
+ '
+
+ test_expect_success "fsck detects symlinked $name ($type)" '
+ (
+ cd $dir &&
+
+ # Check not only that we fail, but that it is due to the
+ # symlink detector
+ $fsck_must_fail git fsck 2>output &&
+ grep "$fsck_prefix.*tree $tree: ${name}Symlink" output
+ )
+ '
+
+ test -n "$refuse_index" &&
+ test_expect_success "refuse to load symlinked $name into index ($type)" '
+ test_must_fail \
+ git -C $dir \
+ -c core.protectntfs \
+ -c core.protecthfs \
+ read-tree $tree 2>err &&
+ grep "invalid path.*$name" err &&
+ git -C $dir ls-files -s >out &&
+ test_must_be_empty out
+ '
+}
+
+check_dotx_symlink gitmodules vanilla .gitmodules
+check_dotx_symlink gitmodules ntfs ".gitmodules ."
+check_dotx_symlink gitmodules hfs ".${u200c}gitmodules"
+
+check_dotx_symlink --warning gitattributes vanilla .gitattributes
+check_dotx_symlink --warning gitattributes ntfs ".gitattributes ."
+check_dotx_symlink --warning gitattributes hfs ".${u200c}gitattributes"
+
+check_dotx_symlink --warning gitignore vanilla .gitignore
+check_dotx_symlink --warning gitignore ntfs ".gitignore ."
+check_dotx_symlink --warning gitignore hfs ".${u200c}gitignore"
+
+check_dotx_symlink --warning mailmap vanilla .mailmap
+check_dotx_symlink --warning mailmap ntfs ".mailmap ."
+check_dotx_symlink --warning mailmap hfs ".${u200c}mailmap"
+
+test_expect_success 'fsck detects non-blob .gitmodules' '
+ git init non-blob &&
+ (
+ cd non-blob &&
+
+ # As above, make the funny tree directly to avoid index
+ # restrictions.
+ mkdir subdir &&
+ cp ../.gitmodules subdir/file &&
+ git add subdir/file &&
+ git commit -m ok &&
+ git ls-tree HEAD | sed s/subdir/.gitmodules/ | git mktree &&
+
+ test_must_fail git fsck 2>output &&
+ test_grep gitmodulesBlob output
+ )
+'
+
+test_expect_success 'fsck detects corrupt .gitmodules' '
+ git init corrupt &&
+ (
+ cd corrupt &&
+
+ echo "[broken" >.gitmodules &&
+ git add .gitmodules &&
+ git commit -m "broken gitmodules" &&
+
+ git fsck 2>output &&
+ test_grep gitmodulesParse output &&
+ test_grep ! "bad config" output
+ )
+'
+
+test_expect_success WINDOWS 'prevent git~1 squatting on Windows' '
+ git init squatting &&
+ (
+ cd squatting &&
+ mkdir a &&
+ touch a/..git &&
+ git add a/..git &&
+ test_tick &&
+ git commit -m initial &&
+
+ modules="$(test_write_lines \
+ "[submodule \"b.\"]" "url = ." "path = c" \
+ "[submodule \"b\"]" "url = ." "path = d\\\\a" |
+ git hash-object -w --stdin)" &&
+ rev="$(git rev-parse --verify HEAD)" &&
+ hash="$(echo x | git hash-object -w --stdin)" &&
+ test_must_fail git update-index --add \
+ --cacheinfo 160000,$rev,d\\a 2>err &&
+ test_grep "Invalid path" err &&
+ git -c core.protectNTFS=false update-index --add \
+ --cacheinfo 100644,$modules,.gitmodules \
+ --cacheinfo 160000,$rev,c \
+ --cacheinfo 160000,$rev,d\\a \
+ --cacheinfo 100644,$hash,d./a/x \
+ --cacheinfo 100644,$hash,d./a/..git &&
+ test_tick &&
+ git -c core.protectNTFS=false commit -m "module"
+ ) &&
+ if test_have_prereq MINGW
+ then
+ test_must_fail git -c core.protectNTFS=false \
+ clone --recurse-submodules squatting squatting-clone 2>err &&
+ test_grep -e "directory not empty" -e "not an empty directory" err &&
+ ! grep gitdir squatting-clone/d/a/git~2
+ fi
+'
+
+test_expect_success 'git dirs of sibling submodules must not be nested' '
+ git init nested &&
+ test_commit -C nested nested &&
+ (
+ cd nested &&
+ cat >.gitmodules <<-EOF &&
+ [submodule "hippo"]
+ url = .
+ path = thing1
+ [submodule "hippo/hooks"]
+ url = .
+ path = thing2
+ EOF
+ git clone . thing1 &&
+ git clone . thing2 &&
+ git add .gitmodules thing1 thing2 &&
+ test_tick &&
+ git commit -m nested
+ ) &&
+ test_must_fail git clone --recurse-submodules nested clone 2>err &&
+ test_grep "is inside git dir" err
+'
+
+test_done
diff --git a/t/t7500-commit-template-squash-signoff.sh b/t/t7500-commit-template-squash-signoff.sh
new file mode 100755
index 0000000..4dca8d9
--- /dev/null
+++ b/t/t7500-commit-template-squash-signoff.sh
@@ -0,0 +1,561 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Steven Grimm
+#
+
+test_description='git commit
+
+Tests for template, signoff, squash and -F functions.'
+
+. ./test-lib.sh
+
+. "$TEST_DIRECTORY"/lib-rebase.sh
+
+commit_msg_is () {
+ expect=commit_msg_is.expect
+ actual=commit_msg_is.actual
+
+ printf "%s" "$(git log --pretty=format:%s%b -1)" >"$actual" &&
+ printf "%s" "$1" >"$expect" &&
+ test_cmp "$expect" "$actual"
+}
+
+# A sanity check to see if commit is working at all.
+test_expect_success 'a basic commit in an empty tree should succeed' '
+ echo content > foo &&
+ git add foo &&
+ git commit -m "initial commit"
+'
+
+test_expect_success 'nonexistent template file should return error' '
+ echo changes >> foo &&
+ git add foo &&
+ (
+ GIT_EDITOR="echo hello >\"\$1\"" &&
+ export GIT_EDITOR &&
+ test_must_fail git commit --template "$PWD"/notexist
+ )
+'
+
+test_expect_success 'nonexistent template file in config should return error' '
+ test_config commit.template "$PWD"/notexist &&
+ (
+ GIT_EDITOR="echo hello >\"\$1\"" &&
+ export GIT_EDITOR &&
+ test_must_fail git commit
+ )
+'
+
+# From now on we'll use a template file that exists.
+TEMPLATE="$PWD"/template
+
+test_expect_success 'unedited template should not commit' '
+ echo "template line" > "$TEMPLATE" &&
+ test_must_fail git commit --template "$TEMPLATE"
+'
+
+test_expect_success 'unedited template with comments should not commit' '
+ echo "# comment in template" >> "$TEMPLATE" &&
+ test_must_fail git commit --template "$TEMPLATE"
+'
+
+test_expect_success 'a Signed-off-by line by itself should not commit' '
+ (
+ test_set_editor "$TEST_DIRECTORY"/t7500/add-signed-off &&
+ test_must_fail git commit --template "$TEMPLATE"
+ )
+'
+
+test_expect_success 'adding comments to a template should not commit' '
+ (
+ test_set_editor "$TEST_DIRECTORY"/t7500/add-comments &&
+ test_must_fail git commit --template "$TEMPLATE"
+ )
+'
+
+test_expect_success 'adding real content to a template should commit' '
+ (
+ test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
+ git commit --template "$TEMPLATE"
+ ) &&
+ commit_msg_is "template linecommit message"
+'
+
+test_expect_success '-t option should be short for --template' '
+ echo "short template" > "$TEMPLATE" &&
+ echo "new content" >> foo &&
+ git add foo &&
+ (
+ test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
+ git commit -t "$TEMPLATE"
+ ) &&
+ commit_msg_is "short templatecommit message"
+'
+
+test_expect_success 'config-specified template should commit' '
+ echo "new template" > "$TEMPLATE" &&
+ test_config commit.template "$TEMPLATE" &&
+ echo "more content" >> foo &&
+ git add foo &&
+ (
+ test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
+ git commit
+ ) &&
+ commit_msg_is "new templatecommit message"
+'
+
+test_expect_success 'explicit commit message should override template' '
+ echo "still more content" >> foo &&
+ git add foo &&
+ GIT_EDITOR="$TEST_DIRECTORY"/t7500/add-content git commit --template "$TEMPLATE" \
+ -m "command line msg" &&
+ commit_msg_is "command line msg"
+'
+
+test_expect_success 'commit message from file should override template' '
+ echo "content galore" >> foo &&
+ git add foo &&
+ echo "standard input msg" |
+ (
+ test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
+ git commit --template "$TEMPLATE" --file -
+ ) &&
+ commit_msg_is "standard input msg"
+'
+
+cat >"$TEMPLATE" <<\EOF
+
+
+### template
+
+EOF
+test_expect_success 'commit message from template with whitespace issue' '
+ echo "content galore" >>foo &&
+ git add foo &&
+ GIT_EDITOR=\""$TEST_DIRECTORY"\"/t7500/add-whitespaced-content \
+ git commit --template "$TEMPLATE" &&
+ commit_msg_is "commit message"
+'
+
+test_expect_success 'using alternate GIT_INDEX_FILE (1)' '
+
+ cp .git/index saved-index &&
+ (
+ echo some new content >file &&
+ GIT_INDEX_FILE=.git/another_index &&
+ export GIT_INDEX_FILE &&
+ git add file &&
+ git commit -m "commit using another index" &&
+ git diff-index --exit-code HEAD &&
+ git diff-files --exit-code
+ ) &&
+ cmp .git/index saved-index >/dev/null
+
+'
+
+test_expect_success 'using alternate GIT_INDEX_FILE (2)' '
+
+ cp .git/index saved-index &&
+ (
+ rm -f .git/no-such-index &&
+ GIT_INDEX_FILE=.git/no-such-index &&
+ export GIT_INDEX_FILE &&
+ git commit -m "commit using nonexistent index" &&
+ test -z "$(git ls-files)" &&
+ test -z "$(git ls-tree HEAD)"
+
+ ) &&
+ cmp .git/index saved-index >/dev/null
+'
+
+cat > expect << EOF
+zort
+
+Signed-off-by: C O Mitter <committer@example.com>
+EOF
+
+test_expect_success '--signoff' '
+ echo "yet another content *narf*" >> foo &&
+ echo "zort" | git commit -s -F - foo &&
+ git cat-file commit HEAD | sed "1,/^\$/d" > output &&
+ test_cmp expect output
+'
+
+test_expect_success 'commit message from file (1)' '
+ mkdir subdir &&
+ echo "Log in top directory" >log &&
+ echo "Log in sub directory" >subdir/log &&
+ (
+ cd subdir &&
+ git commit --allow-empty -F log
+ ) &&
+ commit_msg_is "Log in sub directory"
+'
+
+test_expect_success 'commit message from file (2)' '
+ rm -f log &&
+ echo "Log in sub directory" >subdir/log &&
+ (
+ cd subdir &&
+ git commit --allow-empty -F log
+ ) &&
+ commit_msg_is "Log in sub directory"
+'
+
+test_expect_success 'commit message from stdin' '
+ (
+ cd subdir &&
+ echo "Log with foo word" | git commit --allow-empty -F -
+ ) &&
+ commit_msg_is "Log with foo word"
+'
+
+test_expect_success 'commit -F overrides -t' '
+ (
+ cd subdir &&
+ echo "-F log" > f.log &&
+ echo "-t template" > t.template &&
+ git commit --allow-empty -F f.log -t t.template
+ ) &&
+ commit_msg_is "-F log"
+'
+
+test_expect_success 'Commit without message is allowed with --allow-empty-message' '
+ echo "more content" >>foo &&
+ git add foo &&
+ >empty &&
+ git commit --allow-empty-message <empty &&
+ commit_msg_is "" &&
+ git tag empty-message-commit
+'
+
+test_expect_success 'Commit without message is no-no without --allow-empty-message' '
+ echo "more content" >>foo &&
+ git add foo &&
+ >empty &&
+ test_must_fail git commit <empty
+'
+
+test_expect_success 'Commit a message with --allow-empty-message' '
+ echo "even more content" >>foo &&
+ git add foo &&
+ git commit --allow-empty-message -m"hello there" &&
+ commit_msg_is "hello there"
+'
+
+test_expect_success 'commit -C empty respects --allow-empty-message' '
+ echo more >>foo &&
+ git add foo &&
+ test_must_fail git commit -C empty-message-commit &&
+ git commit -C empty-message-commit --allow-empty-message &&
+ commit_msg_is ""
+'
+
+commit_for_rebase_autosquash_setup () {
+ echo "first content line" >>foo &&
+ git add foo &&
+ cat >log <<EOF &&
+target message subject line
+
+target message body line 1
+target message body line 2
+EOF
+ git commit -F log &&
+ echo "second content line" >>foo &&
+ git add foo &&
+ git commit -m "intermediate commit" &&
+ echo "third content line" >>foo &&
+ git add foo
+}
+
+test_expect_success 'commit --fixup provides correct one-line commit message' '
+ commit_for_rebase_autosquash_setup &&
+ EDITOR="echo ignored >>" git commit --fixup HEAD~1 &&
+ commit_msg_is "fixup! target message subject line"
+'
+
+test_expect_success 'commit --fixup -m"something" -m"extra"' '
+ commit_for_rebase_autosquash_setup &&
+ git commit --fixup HEAD~1 -m"something" -m"extra" &&
+ commit_msg_is "fixup! target message subject linesomething
+
+extra"
+'
+test_expect_success 'commit --fixup --edit' '
+ commit_for_rebase_autosquash_setup &&
+ EDITOR="printf \"something\nextra\" >>" git commit --fixup HEAD~1 --edit &&
+ commit_msg_is "fixup! target message subject linesomething
+extra"
+'
+
+get_commit_msg () {
+ rev="$1" &&
+ git log -1 --pretty=format:"%B" "$rev"
+}
+
+test_expect_success 'commit --fixup=amend: creates amend! commit' '
+ commit_for_rebase_autosquash_setup &&
+ cat >expected <<-EOF &&
+ amend! $(git log -1 --format=%s HEAD~)
+
+ $(get_commit_msg HEAD~)
+
+ edited
+ EOF
+ (
+ set_fake_editor &&
+ FAKE_COMMIT_AMEND="edited" \
+ git commit --fixup=amend:HEAD~
+ ) &&
+ get_commit_msg HEAD >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success '--fixup=amend: --only ignores staged changes' '
+ commit_for_rebase_autosquash_setup &&
+ cat >expected <<-EOF &&
+ amend! $(git log -1 --format=%s HEAD~)
+
+ $(get_commit_msg HEAD~)
+
+ edited
+ EOF
+ (
+ set_fake_editor &&
+ FAKE_COMMIT_AMEND="edited" \
+ git commit --fixup=amend:HEAD~ --only
+ ) &&
+ get_commit_msg HEAD >actual &&
+ test_cmp expected actual &&
+ test_cmp_rev HEAD@{1}^{tree} HEAD^{tree} &&
+ test_cmp_rev HEAD@{1} HEAD^ &&
+ test_expect_code 1 git diff --cached --exit-code &&
+ git cat-file blob :foo >actual &&
+ test_cmp foo actual
+'
+
+test_expect_success '--fixup=reword: ignores staged changes' '
+ commit_for_rebase_autosquash_setup &&
+ cat >expected <<-EOF &&
+ amend! $(git log -1 --format=%s HEAD~)
+
+ $(get_commit_msg HEAD~)
+
+ edited
+ EOF
+ (
+ set_fake_editor &&
+ FAKE_COMMIT_AMEND="edited" \
+ git commit --fixup=reword:HEAD~
+ ) &&
+ get_commit_msg HEAD >actual &&
+ test_cmp expected actual &&
+ test_cmp_rev HEAD@{1}^{tree} HEAD^{tree} &&
+ test_cmp_rev HEAD@{1} HEAD^ &&
+ test_expect_code 1 git diff --cached --exit-code &&
+ git cat-file blob :foo >actual &&
+ test_cmp foo actual
+'
+
+test_expect_success '--fixup=reword: error out with -m option' '
+ commit_for_rebase_autosquash_setup &&
+ echo "fatal: options '\''-m'\'' and '\''--fixup:reword'\'' cannot be used together" >expect &&
+ test_must_fail git commit --fixup=reword:HEAD~ -m "reword commit message" 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--fixup=amend: error out with -m option' '
+ commit_for_rebase_autosquash_setup &&
+ echo "fatal: options '\''-m'\'' and '\''--fixup:amend'\'' cannot be used together" >expect &&
+ test_must_fail git commit --fixup=amend:HEAD~ -m "amend commit message" 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'consecutive amend! commits remove amend! line from commit msg body' '
+ commit_for_rebase_autosquash_setup &&
+ cat >expected <<-EOF &&
+ amend! amend! $(git log -1 --format=%s HEAD~)
+
+ $(get_commit_msg HEAD~)
+
+ edited 1
+
+ edited 2
+ EOF
+ echo "reword new commit message" >actual &&
+ (
+ set_fake_editor &&
+ FAKE_COMMIT_AMEND="edited 1" \
+ git commit --fixup=reword:HEAD~ &&
+ FAKE_COMMIT_AMEND="edited 2" \
+ git commit --fixup=reword:HEAD
+ ) &&
+ get_commit_msg HEAD >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'deny to create amend! commit if its commit msg body is empty' '
+ commit_for_rebase_autosquash_setup &&
+ echo "Aborting commit due to empty commit message body." >expected &&
+ (
+ set_fake_editor &&
+ test_must_fail env FAKE_COMMIT_MESSAGE="amend! target message subject line" \
+ git commit --fixup=amend:HEAD~ 2>actual
+ ) &&
+ test_cmp expected actual
+'
+
+test_expect_success 'amend! commit allows empty commit msg body with --allow-empty-message' '
+ commit_for_rebase_autosquash_setup &&
+ cat >expected <<-EOF &&
+ amend! $(git log -1 --format=%s HEAD~)
+ EOF
+ (
+ set_fake_editor &&
+ FAKE_COMMIT_MESSAGE="amend! target message subject line" \
+ git commit --fixup=amend:HEAD~ --allow-empty-message &&
+ get_commit_msg HEAD >actual
+ ) &&
+ test_cmp expected actual
+'
+
+test_fixup_reword_opt () {
+ test_expect_success "--fixup=reword: incompatible with $1" "
+ echo 'fatal: reword option of '\''--fixup'\'' and' \
+ ''\''--patch/--interactive/--all/--include/--only'\' \
+ 'cannot be used together' >expect &&
+ test_must_fail git commit --fixup=reword:HEAD~ $1 2>actual &&
+ test_cmp expect actual
+ "
+}
+
+for opt in --all --include --only --interactive --patch
+do
+ test_fixup_reword_opt $opt
+done
+
+test_expect_success '--fixup=reword: give error with pathsec' '
+ commit_for_rebase_autosquash_setup &&
+ echo "fatal: reword option of '\''--fixup'\'' and path '\''foo'\'' cannot be used together" >expect &&
+ test_must_fail git commit --fixup=reword:HEAD~ -- foo 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--fixup=reword: -F give error message' '
+ echo "fatal: options '\''-F'\'' and '\''--fixup'\'' cannot be used together" >expect &&
+ test_must_fail git commit --fixup=reword:HEAD~ -F msg 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'commit --squash works with -F' '
+ commit_for_rebase_autosquash_setup &&
+ echo "log message from file" >msgfile &&
+ git commit --squash HEAD~1 -F msgfile &&
+ commit_msg_is "squash! target message subject linelog message from file"
+'
+
+test_expect_success 'commit --squash works with -m' '
+ commit_for_rebase_autosquash_setup &&
+ git commit --squash HEAD~1 -m "foo bar\nbaz" &&
+ commit_msg_is "squash! target message subject linefoo bar\nbaz"
+'
+
+test_expect_success 'commit --squash works with -C' '
+ commit_for_rebase_autosquash_setup &&
+ git commit --squash HEAD~1 -C HEAD &&
+ commit_msg_is "squash! target message subject lineintermediate commit"
+'
+
+test_expect_success 'commit --squash works with -c' '
+ commit_for_rebase_autosquash_setup &&
+ test_set_editor "$TEST_DIRECTORY"/t7500/edit-content &&
+ git commit --squash HEAD~1 -c HEAD &&
+ commit_msg_is "squash! target message subject lineedited commit"
+'
+
+test_expect_success 'commit --squash works with -C for same commit' '
+ commit_for_rebase_autosquash_setup &&
+ git commit --squash HEAD -C HEAD &&
+ commit_msg_is "squash! intermediate commit"
+'
+
+test_expect_success 'commit --squash works with -c for same commit' '
+ commit_for_rebase_autosquash_setup &&
+ test_set_editor "$TEST_DIRECTORY"/t7500/edit-content &&
+ git commit --squash HEAD -c HEAD &&
+ commit_msg_is "squash! edited commit"
+'
+
+test_expect_success 'commit --squash works with editor' '
+ commit_for_rebase_autosquash_setup &&
+ test_set_editor "$TEST_DIRECTORY"/t7500/add-content &&
+ git commit --squash HEAD~1 &&
+ commit_msg_is "squash! target message subject linecommit message"
+'
+
+test_expect_success 'invalid message options when using --fixup' '
+ echo changes >>foo &&
+ echo "message" >log &&
+ git add foo &&
+ test_must_fail git commit --fixup HEAD~1 --squash HEAD~2 &&
+ test_must_fail git commit --fixup HEAD~1 -C HEAD~2 &&
+ test_must_fail git commit --fixup HEAD~1 -c HEAD~2 &&
+ test_must_fail git commit --fixup HEAD~1 -F log
+'
+
+cat >expected-template <<EOF
+
+# Please enter the commit message for your changes. Lines starting
+# with '#' will be ignored.
+#
+# Author: A U Thor <author@example.com>
+#
+# On branch commit-template-check
+# Changes to be committed:
+# new file: commit-template-check
+#
+# Untracked files not listed
+EOF
+
+test_expect_success 'new line found before status message in commit template' '
+ git checkout -b commit-template-check &&
+ git reset --hard HEAD &&
+ touch commit-template-check &&
+ git add commit-template-check &&
+ GIT_EDITOR="cat >editor-input" git commit --untracked-files=no --allow-empty-message &&
+ test_cmp expected-template editor-input
+'
+
+test_expect_success 'setup empty commit with unstaged rename and copy' '
+ test_create_repo unstaged_rename_and_copy &&
+ (
+ cd unstaged_rename_and_copy &&
+
+ echo content >orig &&
+ git add orig &&
+ test_commit orig &&
+
+ cp orig new_copy &&
+ mv orig new_rename &&
+ git add -N new_copy new_rename
+ )
+'
+
+test_expect_success 'check commit with unstaged rename and copy' '
+ (
+ cd unstaged_rename_and_copy &&
+
+ test_must_fail git -c diff.renames=copy commit
+ )
+'
+
+test_expect_success 'commit without staging files fails and displays hints' '
+ echo "initial" >file &&
+ git add file &&
+ git commit -m initial &&
+ echo "changes" >>file &&
+ test_must_fail git commit -m update >actual &&
+ test_grep "no changes added to commit (use \"git add\" and/or \"git commit -a\")" actual
+'
+
+test_done
diff --git a/t/t7500/add-comments b/t/t7500/add-comments
new file mode 100755
index 0000000..a72e65c
--- /dev/null
+++ b/t/t7500/add-comments
@@ -0,0 +1,4 @@
+#!/bin/sh
+echo "# this is a new comment" >> "$1"
+echo "# and so is this" >> "$1"
+exit 0
diff --git a/t/t7500/add-content b/t/t7500/add-content
new file mode 100755
index 0000000..2fa3d86
--- /dev/null
+++ b/t/t7500/add-content
@@ -0,0 +1,3 @@
+#!/bin/sh
+echo "commit message" >> "$1"
+exit 0
diff --git a/t/t7500/add-content-and-comment b/t/t7500/add-content-and-comment
new file mode 100755
index 0000000..c4dccff
--- /dev/null
+++ b/t/t7500/add-content-and-comment
@@ -0,0 +1,5 @@
+#!/bin/sh
+echo "commit message" >> "$1"
+echo "# comment" >> "$1"
+exit 0
+
diff --git a/t/t7500/add-signed-off b/t/t7500/add-signed-off
new file mode 100755
index 0000000..e1d856a
--- /dev/null
+++ b/t/t7500/add-signed-off
@@ -0,0 +1,3 @@
+#!/bin/sh
+echo "Signed-off-by: foo <bar@frotz>" >> "$1"
+exit 0
diff --git a/t/t7500/add-whitespaced-content b/t/t7500/add-whitespaced-content
new file mode 100755
index 0000000..ccf07c6
--- /dev/null
+++ b/t/t7500/add-whitespaced-content
@@ -0,0 +1,8 @@
+#!/bin/sh
+sed -e 's/|$//' >>"$1" <<\EOF
+
+ |
+commit message |
+
+EOF
+exit 0
diff --git a/t/t7500/edit-content b/t/t7500/edit-content
new file mode 100755
index 0000000..08db9fd
--- /dev/null
+++ b/t/t7500/edit-content
@@ -0,0 +1,4 @@
+#!/bin/sh
+sed -e "s/intermediate/edited/g" <"$1" >"$1-"
+mv "$1-" "$1"
+exit 0
diff --git a/t/t7501-commit-basic-functionality.sh b/t/t7501-commit-basic-functionality.sh
new file mode 100755
index 0000000..3d8500a
--- /dev/null
+++ b/t/t7501-commit-basic-functionality.sh
@@ -0,0 +1,716 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>
+#
+
+# FIXME: Test the various index usages, -i and -o, test reflog,
+# signoff
+
+test_description='git commit'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY/lib-diff.sh"
+
+author='The Real Author <someguy@his.email.org>'
+
+test_tick
+
+test_expect_success 'initial status' '
+ echo bongo bongo >file &&
+ git add file &&
+ git status >actual &&
+ test_grep "No commits yet" actual
+'
+
+test_expect_success 'fail initial amend' '
+ test_must_fail git commit --amend
+'
+
+test_expect_success 'setup: initial commit' '
+ git commit -m initial
+'
+
+test_expect_success '-m and -F do not mix' '
+ git checkout HEAD file && echo >>file && git add file &&
+ test_must_fail git commit -m foo -m bar -F file
+'
+
+test_expect_success '-m and -C do not mix' '
+ git checkout HEAD file && echo >>file && git add file &&
+ test_must_fail git commit -C HEAD -m illegal
+'
+
+test_expect_success 'paths and -a do not mix' '
+ echo King of the bongo >file &&
+ test_must_fail git commit -m foo -a file
+'
+
+test_expect_success PERL 'can use paths with --interactive' '
+ echo bong-o-bong >file &&
+ # 2: update, 1:st path, that is all, 7: quit
+ test_write_lines 2 1 "" 7 |
+ git commit -m foo --interactive file &&
+ git reset --hard HEAD^
+'
+
+test_expect_success 'removed files and relative paths' '
+ test_when_finished "rm -rf foo" &&
+ git init foo &&
+ >foo/foo.txt &&
+ git -C foo add foo.txt &&
+ git -C foo commit -m first &&
+ git -C foo rm foo.txt &&
+
+ mkdir -p foo/bar &&
+ git -C foo/bar commit -m second ../foo.txt
+'
+
+test_expect_success 'using invalid commit with -C' '
+ test_must_fail git commit --allow-empty -C bogus
+'
+
+test_expect_success 'nothing to commit' '
+ git reset --hard &&
+ test_must_fail git commit -m initial
+'
+
+test_expect_success '--dry-run fails with nothing to commit' '
+ test_must_fail git commit -m initial --dry-run
+'
+
+test_expect_success '--short fails with nothing to commit' '
+ test_must_fail git commit -m initial --short
+'
+
+test_expect_success '--porcelain fails with nothing to commit' '
+ test_must_fail git commit -m initial --porcelain
+'
+
+test_expect_success '--long fails with nothing to commit' '
+ test_must_fail git commit -m initial --long
+'
+
+test_expect_success 'setup: non-initial commit' '
+ echo bongo bongo bongo >file &&
+ git commit -m next -a
+'
+
+test_expect_success '--dry-run with stuff to commit returns ok' '
+ echo bongo bongo bongo >>file &&
+ git commit -m next -a --dry-run
+'
+
+test_expect_success '--short with stuff to commit returns ok' '
+ echo bongo bongo bongo >>file &&
+ git commit -m next -a --short
+'
+
+test_expect_success '--porcelain with stuff to commit returns ok' '
+ echo bongo bongo bongo >>file &&
+ git commit -m next -a --porcelain
+'
+
+test_expect_success '--long with stuff to commit returns ok' '
+ echo bongo bongo bongo >>file &&
+ git commit -m next -a --long
+'
+
+test_expect_success 'commit message from non-existing file' '
+ echo more bongo: bongo bongo bongo bongo >file &&
+ test_must_fail git commit -F gah -a
+'
+
+test_expect_success 'empty commit message' '
+ # Empty except stray tabs and spaces on a few lines.
+ sed -e "s/@//g" >msg <<-\EOF &&
+ @ @
+ @@
+ @ @
+ @Signed-off-by: hula@
+ EOF
+ test_must_fail git commit -F msg -a
+'
+
+test_expect_success 'template "emptyness" check does not kick in with -F' '
+ git checkout HEAD file && echo >>file && git add file &&
+ git commit -t file -F file
+'
+
+test_expect_success 'template "emptyness" check' '
+ git checkout HEAD file && echo >>file && git add file &&
+ test_must_fail git commit -t file 2>err &&
+ test_grep "did not edit" err
+'
+
+test_expect_success 'setup: commit message from file' '
+ git checkout HEAD file && echo >>file && git add file &&
+ echo this is the commit message, coming from a file >msg &&
+ git commit -F msg -a
+'
+
+test_expect_success 'amend commit' '
+ cat >editor <<-\EOF &&
+ #!/bin/sh
+ sed -e "s/a file/an amend commit/g" <"$1" >"$1-"
+ mv "$1-" "$1"
+ EOF
+ chmod 755 editor &&
+ EDITOR=./editor git commit --amend
+'
+
+test_expect_success 'amend --only ignores staged contents' '
+ cp file file.expect &&
+ echo changed >file &&
+ git add file &&
+ git commit --no-edit --amend --only &&
+ git cat-file blob HEAD:file >file.actual &&
+ test_cmp file.expect file.actual &&
+ git diff --exit-code
+'
+
+test_expect_success 'allow-empty --only ignores staged contents' '
+ echo changed-again >file &&
+ git add file &&
+ git commit --allow-empty --only -m "empty" &&
+ git cat-file blob HEAD:file >file.actual &&
+ test_cmp file.expect file.actual &&
+ git diff --exit-code
+'
+
+test_expect_success 'set up editor' '
+ cat >editor <<-\EOF &&
+ #!/bin/sh
+ sed -e "s/unamended/amended/g" <"$1" >"$1-"
+ mv "$1-" "$1"
+ EOF
+ chmod 755 editor
+'
+
+test_expect_success 'amend without launching editor' '
+ echo unamended >expect &&
+ git commit --allow-empty -m "unamended" &&
+ echo needs more bongo >file &&
+ git add file &&
+ EDITOR=./editor git commit --no-edit --amend &&
+ git diff --exit-code HEAD -- file &&
+ git diff-tree -s --format=%s HEAD >msg &&
+ test_cmp expect msg
+'
+
+test_expect_success '--amend --edit' '
+ echo amended >expect &&
+ git commit --allow-empty -m "unamended" &&
+ echo bongo again >file &&
+ git add file &&
+ EDITOR=./editor git commit --edit --amend &&
+ git diff-tree -s --format=%s HEAD >msg &&
+ test_cmp expect msg
+'
+
+test_expect_success '--amend --edit of empty message' '
+ cat >replace <<-\EOF &&
+ #!/bin/sh
+ echo "amended" >"$1"
+ EOF
+ chmod 755 replace &&
+ git commit --allow-empty --allow-empty-message -m "" &&
+ echo more bongo >file &&
+ git add file &&
+ EDITOR=./replace git commit --edit --amend &&
+ git diff-tree -s --format=%s HEAD >msg &&
+ ./replace expect &&
+ test_cmp expect msg
+'
+
+test_expect_success '--amend to set message to empty' '
+ echo bata >file &&
+ git add file &&
+ git commit -m "unamended" &&
+ git commit --amend --allow-empty-message -m "" &&
+ git diff-tree -s --format=%s HEAD >msg &&
+ echo "" >expect &&
+ test_cmp expect msg
+'
+
+test_expect_success '--amend to set empty message needs --allow-empty-message' '
+ echo conga >file &&
+ git add file &&
+ git commit -m "unamended" &&
+ test_must_fail git commit --amend -m "" &&
+ git diff-tree -s --format=%s HEAD >msg &&
+ echo "unamended" >expect &&
+ test_cmp expect msg
+'
+
+test_expect_success '-m --edit' '
+ echo amended >expect &&
+ git commit --allow-empty -m buffer &&
+ echo bongo bongo >file &&
+ git add file &&
+ EDITOR=./editor git commit -m unamended --edit &&
+ git diff-tree -s --format=%s HEAD >msg &&
+ test_cmp expect msg
+'
+
+test_expect_success '-m and -F do not mix' '
+ echo enough with the bongos >file &&
+ test_must_fail git commit -F msg -m amending .
+'
+
+test_expect_success 'using message from other commit' '
+ git commit -C HEAD^ .
+'
+
+test_expect_success 'editing message from other commit' '
+ cat >editor <<-\EOF &&
+ #!/bin/sh
+ sed -e "s/amend/older/g" <"$1" >"$1-"
+ mv "$1-" "$1"
+ EOF
+ chmod 755 editor &&
+ echo hula hula >file &&
+ EDITOR=./editor git commit -c HEAD^ -a
+'
+
+test_expect_success 'message from stdin' '
+ echo silly new contents >file &&
+ echo commit message from stdin |
+ git commit -F - -a
+'
+
+test_expect_success 'overriding author from command line' '
+ echo gak >file &&
+ git commit -m author \
+ --author "Rubber Duck <rduck@convoy.org>" -a >output 2>&1 &&
+ grep Rubber.Duck output
+'
+
+test_expect_success PERL 'interactive add' '
+ echo 7 | test_must_fail git commit --interactive >out &&
+ grep "What now" out
+'
+
+test_expect_success PERL "commit --interactive doesn't change index if editor aborts" '
+ echo zoo >file &&
+ test_must_fail git diff --exit-code >diff1 &&
+ test_write_lines u "*" q |
+ (
+ EDITOR=: &&
+ export EDITOR &&
+ test_must_fail git commit --interactive
+ ) &&
+ git diff >diff2 &&
+ compare_diff_patch diff1 diff2
+'
+
+test_expect_success 'editor not invoked if -F is given' '
+ cat >editor <<-\EOF &&
+ #!/bin/sh
+ sed -e s/good/bad/g <"$1" >"$1-"
+ mv "$1-" "$1"
+ EOF
+ chmod 755 editor &&
+
+ echo A good commit message. >msg &&
+ echo moo >file &&
+
+ EDITOR=./editor git commit -a -F msg &&
+ git show -s --pretty=format:%s >subject &&
+ grep -q good subject &&
+
+ echo quack >file &&
+ echo Another good message. |
+ EDITOR=./editor git commit -a -F - &&
+ git show -s --pretty=format:%s >subject &&
+ grep -q good subject
+'
+
+test_expect_success 'partial commit that involves removal (1)' '
+
+ git rm --cached file &&
+ mv file elif &&
+ git add elif &&
+ git commit -m "Partial: add elif" elif &&
+ git diff-tree --name-status HEAD^ HEAD >current &&
+ echo "A elif" >expected &&
+ test_cmp expected current
+
+'
+
+test_expect_success 'partial commit that involves removal (2)' '
+
+ git commit -m "Partial: remove file" file &&
+ git diff-tree --name-status HEAD^ HEAD >current &&
+ echo "D file" >expected &&
+ test_cmp expected current
+
+'
+
+test_expect_success 'partial commit that involves removal (3)' '
+
+ git rm --cached elif &&
+ echo elif >elif &&
+ git commit -m "Partial: modify elif" elif &&
+ git diff-tree --name-status HEAD^ HEAD >current &&
+ echo "M elif" >expected &&
+ test_cmp expected current
+
+'
+
+test_expect_success 'amend commit to fix author' '
+
+ oldtick=$GIT_AUTHOR_DATE &&
+ test_tick &&
+ git reset --hard &&
+ git cat-file -p HEAD >commit &&
+ sed -e "s/author.*/author $author $oldtick/" \
+ -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" \
+ commit >expected &&
+ git commit --amend --author="$author" &&
+ git cat-file -p HEAD >current &&
+ test_cmp expected current
+
+'
+
+test_expect_success 'amend commit to fix date' '
+
+ test_tick &&
+ newtick=$GIT_AUTHOR_DATE &&
+ git reset --hard &&
+ git cat-file -p HEAD >commit &&
+ sed -e "s/author.*/author $author $newtick/" \
+ -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" \
+ commit >expected &&
+ git commit --amend --date="$newtick" &&
+ git cat-file -p HEAD >current &&
+ test_cmp expected current
+
+'
+
+test_expect_success 'commit mentions forced date in output' '
+ git commit --amend --date=2010-01-02T03:04:05 >output &&
+ grep "Date: *Sat Jan 2 03:04:05 2010" output
+'
+
+test_expect_success 'commit complains about completely bogus dates' '
+ test_must_fail git commit --amend --date=seventeen
+'
+
+test_expect_success 'commit --date allows approxidate' '
+ git commit --amend \
+ --date="midnight the 12th of october, anno domini 1979" &&
+ echo "Fri Oct 12 00:00:00 1979 +0000" >expect &&
+ git log -1 --format=%ad >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'sign off (1)' '
+
+ echo 1 >positive &&
+ git add positive &&
+ git commit -s -m "thank you" &&
+ git cat-file commit HEAD >commit &&
+ sed -e "1,/^\$/d" commit >actual &&
+ (
+ echo thank you &&
+ echo &&
+ git var GIT_COMMITTER_IDENT >ident &&
+ sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
+ ) >expected &&
+ test_cmp expected actual
+
+'
+
+test_expect_success 'sign off (2)' '
+
+ echo 2 >positive &&
+ git add positive &&
+ existing="Signed-off-by: Watch This <watchthis@example.com>" &&
+ git commit -s -m "thank you
+
+$existing" &&
+ git cat-file commit HEAD >commit &&
+ sed -e "1,/^\$/d" commit >actual &&
+ (
+ echo thank you &&
+ echo &&
+ echo $existing &&
+ git var GIT_COMMITTER_IDENT >ident &&
+ sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
+ ) >expected &&
+ test_cmp expected actual
+
+'
+
+test_expect_success 'signoff gap' '
+
+ echo 3 >positive &&
+ git add positive &&
+ alt="Alt-RFC-822-Header: Value" &&
+ git commit -s -m "welcome
+
+$alt" &&
+ git cat-file commit HEAD >commit &&
+ sed -e "1,/^\$/d" commit >actual &&
+ (
+ echo welcome &&
+ echo &&
+ echo $alt &&
+ git var GIT_COMMITTER_IDENT >ident &&
+ sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
+ ) >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'signoff gap 2' '
+
+ echo 4 >positive &&
+ git add positive &&
+ alt="fixed: 34" &&
+ git commit -s -m "welcome
+
+We have now
+$alt" &&
+ git cat-file commit HEAD >commit &&
+ sed -e "1,/^\$/d" commit >actual &&
+ (
+ echo welcome &&
+ echo &&
+ echo We have now &&
+ echo $alt &&
+ echo &&
+ git var GIT_COMMITTER_IDENT >ident &&
+ sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
+ ) >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'signoff respects trailer config' '
+
+ echo 5 >positive &&
+ git add positive &&
+ git commit -s -m "subject
+
+non-trailer line
+Myfooter: x" &&
+ git cat-file commit HEAD >commit &&
+ sed -e "1,/^\$/d" commit >actual &&
+ (
+ echo subject &&
+ echo &&
+ echo non-trailer line &&
+ echo Myfooter: x &&
+ echo &&
+ echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
+ ) >expected &&
+ test_cmp expected actual &&
+
+ echo 6 >positive &&
+ git add positive &&
+ git -c "trailer.Myfooter.ifexists=add" commit -s -m "subject
+
+non-trailer line
+Myfooter: x" &&
+ git cat-file commit HEAD >commit &&
+ sed -e "1,/^\$/d" commit >actual &&
+ (
+ echo subject &&
+ echo &&
+ echo non-trailer line &&
+ echo Myfooter: x &&
+ echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
+ ) >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'signoff not confused by ---' '
+ cat >expected <<-EOF &&
+ subject
+
+ body
+ ---
+ these dashes confuse the parser!
+
+ Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
+ EOF
+ # should be a noop, since we already signed
+ git commit --allow-empty --signoff -F expected &&
+ git log -1 --pretty=format:%B >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'multiple -m' '
+
+ >negative &&
+ git add negative &&
+ git commit -m "one" -m "two" -m "three" &&
+ git cat-file commit HEAD >commit &&
+ sed -e "1,/^\$/d" commit >actual &&
+ (
+ echo one &&
+ echo &&
+ echo two &&
+ echo &&
+ echo three
+ ) >expected &&
+ test_cmp expected actual
+
+'
+
+test_expect_success 'amend commit to fix author' '
+
+ oldtick=$GIT_AUTHOR_DATE &&
+ test_tick &&
+ git reset --hard &&
+ git cat-file -p HEAD >commit &&
+ sed -e "s/author.*/author $author $oldtick/" \
+ -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" \
+ commit >expected &&
+ git commit --amend --author="$author" &&
+ git cat-file -p HEAD >current &&
+ test_cmp expected current
+
+'
+
+test_expect_success 'git commit <file> with dirty index' '
+ echo tacocat >elif &&
+ echo tehlulz >chz &&
+ git add chz &&
+ git commit elif -m "tacocat is a palindrome" &&
+ git show --stat >stat &&
+ grep elif stat &&
+ git diff --cached >diff &&
+ grep chz diff
+'
+
+test_expect_success 'same tree (single parent)' '
+
+ git reset --hard &&
+ test_must_fail git commit -m empty
+
+'
+
+test_expect_success 'same tree (single parent) --allow-empty' '
+
+ git commit --allow-empty -m "forced empty" &&
+ git cat-file commit HEAD >commit &&
+ grep forced commit
+
+'
+
+test_expect_success 'same tree (merge and amend merge)' '
+
+ git checkout -b side HEAD^ &&
+ echo zero >zero &&
+ git add zero &&
+ git commit -m "add zero" &&
+ git checkout main &&
+
+ git merge -s ours side -m "empty ok" &&
+ git diff HEAD^ HEAD >actual &&
+ test_must_be_empty actual &&
+
+ git commit --amend -m "empty really ok" &&
+ git diff HEAD^ HEAD >actual &&
+ test_must_be_empty actual
+
+'
+
+test_expect_success 'amend using the message from another commit' '
+
+ git reset --hard &&
+ test_tick &&
+ git commit --allow-empty -m "old commit" &&
+ old=$(git rev-parse --verify HEAD) &&
+ test_tick &&
+ git commit --allow-empty -m "new commit" &&
+ new=$(git rev-parse --verify HEAD) &&
+ test_tick &&
+ git commit --allow-empty --amend -C "$old" &&
+ git show --pretty="format:%ad %s" "$old" >expected &&
+ git show --pretty="format:%ad %s" HEAD >actual &&
+ test_cmp expected actual
+
+'
+
+test_expect_success 'amend using the message from a commit named with tag' '
+
+ git reset --hard &&
+ test_tick &&
+ git commit --allow-empty -m "old commit" &&
+ old=$(git rev-parse --verify HEAD) &&
+ git tag -a -m "tag on old" tagged-old HEAD &&
+ test_tick &&
+ git commit --allow-empty -m "new commit" &&
+ new=$(git rev-parse --verify HEAD) &&
+ test_tick &&
+ git commit --allow-empty --amend -C tagged-old &&
+ git show --pretty="format:%ad %s" "$old" >expected &&
+ git show --pretty="format:%ad %s" HEAD >actual &&
+ test_cmp expected actual
+
+'
+
+test_expect_success 'amend can copy notes' '
+
+ git config notes.rewrite.amend true &&
+ git config notes.rewriteRef "refs/notes/*" &&
+ test_commit foo &&
+ git notes add -m"a note" &&
+ test_tick &&
+ git commit --amend -m"new foo" &&
+ test "$(git notes show)" = "a note"
+
+'
+
+test_expect_success 'commit a file whose name is a dash' '
+ git reset --hard &&
+ test_write_lines 1 2 3 4 5 >./- &&
+ git add ./- &&
+ test_tick &&
+ git commit -m "add dash" >output </dev/null &&
+ test_grep " changed, 5 insertions" output
+'
+
+test_expect_success '--only works on to-be-born branch' '
+ # This test relies on having something in the index, as it
+ # would not otherwise actually prove much. So check this.
+ test -n "$(git ls-files)" &&
+ git checkout --orphan orphan &&
+ echo foo >newfile &&
+ git add newfile &&
+ git commit --only newfile -m"--only on unborn branch" &&
+ echo newfile >expected &&
+ git ls-tree -r --name-only HEAD >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success '--dry-run with conflicts fixed from a merge' '
+ # setup two branches with conflicting information
+ # in the same file, resolve the conflict,
+ # call commit with --dry-run
+ echo "Initial contents, unimportant" >test-file &&
+ git add test-file &&
+ git commit -m "Initial commit" &&
+ echo "commit-1-state" >test-file &&
+ git commit -m "commit 1" -i test-file &&
+ git tag commit-1 &&
+ git checkout -b branch-2 HEAD^1 &&
+ echo "commit-2-state" >test-file &&
+ git commit -m "commit 2" -i test-file &&
+ test_must_fail git merge --no-commit commit-1 &&
+ echo "commit-2-state" >test-file &&
+ git add test-file &&
+ git commit --dry-run &&
+ git commit -m "conflicts fixed from merge."
+'
+
+test_expect_success '--dry-run --short' '
+ >test-file &&
+ git add test-file &&
+ git commit --dry-run --short
+'
+
+test_done
diff --git a/t/t7502-commit-porcelain.sh b/t/t7502-commit-porcelain.sh
new file mode 100755
index 0000000..61c8e81
--- /dev/null
+++ b/t/t7502-commit-porcelain.sh
@@ -0,0 +1,962 @@
+#!/bin/sh
+
+test_description='git commit porcelain-ish'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+commit_msg_is () {
+ expect=commit_msg_is.expect
+ actual=commit_msg_is.actual
+
+ printf "%s" "$(git log --pretty=format:%s%b -1)" >$actual &&
+ printf "%s" "$1" >$expect &&
+ test_cmp $expect $actual
+}
+
+# Arguments: [<prefix] [<commit message>] [<commit options>]
+check_summary_oneline() {
+ test_tick &&
+ git commit ${3+"$3"} -m "$2" >raw &&
+ head -n 1 raw >act &&
+
+ # branch name
+ SUMMARY_PREFIX="$(git name-rev --name-only HEAD)" &&
+
+ # append the "special" prefix, like "root-commit", "detached HEAD"
+ if test -n "$1"
+ then
+ SUMMARY_PREFIX="$SUMMARY_PREFIX ($1)"
+ fi
+
+ # abbrev SHA-1
+ SUMMARY_POSTFIX="$(git log -1 --pretty='format:%h')"
+ echo "[$SUMMARY_PREFIX $SUMMARY_POSTFIX] $2" >exp &&
+
+ test_cmp exp act
+}
+
+trailer_commit_base () {
+ echo "fun" >>file &&
+ git add file &&
+ git commit -s --trailer "Signed-off-by=C1 E1 " \
+ --trailer "Helped-by:C2 E2 " \
+ --trailer "Reported-by=C3 E3" \
+ --trailer "Mentored-by:C4 E4" \
+ -m "hello"
+}
+
+test_expect_success 'output summary format' '
+
+ echo new >file1 &&
+ git add file1 &&
+ check_summary_oneline "root-commit" "initial" &&
+
+ echo change >>file1 &&
+ git add file1
+'
+
+test_expect_success 'output summary format: root-commit' '
+ check_summary_oneline "" "a change"
+'
+
+test_expect_success 'output summary format for commit with an empty diff' '
+
+ check_summary_oneline "" "empty" "--allow-empty"
+'
+
+test_expect_success 'output summary format for merges' '
+
+ git checkout -b recursive-base &&
+ test_commit base file1 &&
+
+ git checkout -b recursive-a recursive-base &&
+ test_commit commit-a file1 &&
+
+ git checkout -b recursive-b recursive-base &&
+ test_commit commit-b file1 &&
+
+ # conflict
+ git checkout recursive-a &&
+ test_must_fail git merge recursive-b &&
+ # resolve the conflict
+ echo commit-a >file1 &&
+ git add file1 &&
+ check_summary_oneline "" "Merge"
+'
+
+output_tests_cleanup() {
+ # this is needed for "do not fire editor in the presence of conflicts"
+ git checkout main &&
+
+ # this is needed for the "partial removal" test to pass
+ git rm file1 &&
+ git commit -m "cleanup"
+}
+
+test_expect_success 'the basics' '
+
+ output_tests_cleanup &&
+
+ echo doing partial >"commit is" &&
+ mkdir not &&
+ echo very much encouraged but we should >not/forbid &&
+ git add "commit is" not &&
+ echo update added "commit is" file >"commit is" &&
+ echo also update another >not/forbid &&
+ test_tick &&
+ git commit -a -m "initial with -a" &&
+
+ git cat-file blob HEAD:"commit is" >current.1 &&
+ git cat-file blob HEAD:not/forbid >current.2 &&
+
+ cmp current.1 "commit is" &&
+ cmp current.2 not/forbid
+
+'
+
+test_expect_success 'partial' '
+
+ echo another >"commit is" &&
+ echo another >not/forbid &&
+ test_tick &&
+ git commit -m "partial commit to handle a file" "commit is" &&
+
+ changed=$(git diff-tree --name-only HEAD^ HEAD) &&
+ test "$changed" = "commit is"
+
+'
+
+test_expect_success 'partial modification in a subdirectory' '
+
+ test_tick &&
+ git commit -m "partial commit to subdirectory" not &&
+
+ changed=$(git diff-tree -r --name-only HEAD^ HEAD) &&
+ test "$changed" = "not/forbid"
+
+'
+
+test_expect_success 'partial removal' '
+
+ git rm not/forbid &&
+ git commit -m "partial commit to remove not/forbid" not &&
+
+ changed=$(git diff-tree -r --name-only HEAD^ HEAD) &&
+ test "$changed" = "not/forbid" &&
+ remain=$(git ls-tree -r --name-only HEAD) &&
+ test "$remain" = "commit is"
+
+'
+
+test_expect_success 'sign off' '
+
+ >positive &&
+ git add positive &&
+ git commit -s -m "thank you" &&
+ git cat-file commit HEAD >commit.msg &&
+ sed -ne "s/Signed-off-by: //p" commit.msg >actual &&
+ git var GIT_COMMITTER_IDENT >ident &&
+ sed -e "s/>.*/>/" ident >expected &&
+ test_cmp expected actual
+
+'
+
+test_expect_success 'commit --trailer with "="' '
+ trailer_commit_base &&
+ cat >expected <<-\EOF &&
+ hello
+
+ Signed-off-by: C O Mitter <committer@example.com>
+ Signed-off-by: C1 E1
+ Helped-by: C2 E2
+ Reported-by: C3 E3
+ Mentored-by: C4 E4
+ EOF
+ git cat-file commit HEAD >commit.msg &&
+ sed -e "1,/^\$/d" commit.msg >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'commit --trailer with -c and "replace" as ifexists' '
+ trailer_commit_base &&
+ cat >expected <<-\EOF &&
+ hello
+
+ Signed-off-by: C O Mitter <committer@example.com>
+ Signed-off-by: C1 E1
+ Reported-by: C3 E3
+ Mentored-by: C4 E4
+ Helped-by: C3 E3
+ EOF
+ git -c trailer.ifexists="replace" \
+ commit --trailer "Mentored-by: C4 E4" \
+ --trailer "Helped-by: C3 E3" \
+ --amend &&
+ git cat-file commit HEAD >commit.msg &&
+ sed -e "1,/^\$/d" commit.msg >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'commit --trailer with -c and "add" as ifexists' '
+ trailer_commit_base &&
+ cat >expected <<-\EOF &&
+ hello
+
+ Signed-off-by: C O Mitter <committer@example.com>
+ Signed-off-by: C1 E1
+ Helped-by: C2 E2
+ Reported-by: C3 E3
+ Mentored-by: C4 E4
+ Reported-by: C3 E3
+ Mentored-by: C4 E4
+ EOF
+ git -c trailer.ifexists="add" \
+ commit --trailer "Reported-by: C3 E3" \
+ --trailer "Mentored-by: C4 E4" \
+ --amend &&
+ git cat-file commit HEAD >commit.msg &&
+ sed -e "1,/^\$/d" commit.msg >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'commit --trailer with -c and "donothing" as ifexists' '
+ trailer_commit_base &&
+ cat >expected <<-\EOF &&
+ hello
+
+ Signed-off-by: C O Mitter <committer@example.com>
+ Signed-off-by: C1 E1
+ Helped-by: C2 E2
+ Reported-by: C3 E3
+ Mentored-by: C4 E4
+ Reviewed-by: C6 E6
+ EOF
+ git -c trailer.ifexists="donothing" \
+ commit --trailer "Mentored-by: C5 E5" \
+ --trailer "Reviewed-by: C6 E6" \
+ --amend &&
+ git cat-file commit HEAD >commit.msg &&
+ sed -e "1,/^\$/d" commit.msg >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'commit --trailer with -c and "addIfDifferent" as ifexists' '
+ trailer_commit_base &&
+ cat >expected <<-\EOF &&
+ hello
+
+ Signed-off-by: C O Mitter <committer@example.com>
+ Signed-off-by: C1 E1
+ Helped-by: C2 E2
+ Reported-by: C3 E3
+ Mentored-by: C4 E4
+ Mentored-by: C5 E5
+ EOF
+ git -c trailer.ifexists="addIfDifferent" \
+ commit --trailer "Reported-by: C3 E3" \
+ --trailer "Mentored-by: C5 E5" \
+ --amend &&
+ git cat-file commit HEAD >commit.msg &&
+ sed -e "1,/^\$/d" commit.msg >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'commit --trailer with -c and "addIfDifferentNeighbor" as ifexists' '
+ trailer_commit_base &&
+ cat >expected <<-\EOF &&
+ hello
+
+ Signed-off-by: C O Mitter <committer@example.com>
+ Signed-off-by: C1 E1
+ Helped-by: C2 E2
+ Reported-by: C3 E3
+ Mentored-by: C4 E4
+ Reported-by: C3 E3
+ EOF
+ git -c trailer.ifexists="addIfDifferentNeighbor" \
+ commit --trailer "Mentored-by: C4 E4" \
+ --trailer "Reported-by: C3 E3" \
+ --amend &&
+ git cat-file commit HEAD >commit.msg &&
+ sed -e "1,/^\$/d" commit.msg >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'commit --trailer with -c and "end" as where' '
+ trailer_commit_base &&
+ cat >expected <<-\EOF &&
+ hello
+
+ Signed-off-by: C O Mitter <committer@example.com>
+ Signed-off-by: C1 E1
+ Helped-by: C2 E2
+ Reported-by: C3 E3
+ Mentored-by: C4 E4
+ Reported-by: C3 E3
+ Mentored-by: C4 E4
+ EOF
+ git -c trailer.where="end" \
+ commit --trailer "Reported-by: C3 E3" \
+ --trailer "Mentored-by: C4 E4" \
+ --amend &&
+ git cat-file commit HEAD >commit.msg &&
+ sed -e "1,/^\$/d" commit.msg >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'commit --trailer with -c and "start" as where' '
+ trailer_commit_base &&
+ cat >expected <<-\EOF &&
+ hello
+
+ Signed-off-by: C1 E1
+ Signed-off-by: C O Mitter <committer@example.com>
+ Signed-off-by: C1 E1
+ Helped-by: C2 E2
+ Reported-by: C3 E3
+ Mentored-by: C4 E4
+ EOF
+ git -c trailer.where="start" \
+ commit --trailer "Signed-off-by: C O Mitter <committer@example.com>" \
+ --trailer "Signed-off-by: C1 E1" \
+ --amend &&
+ git cat-file commit HEAD >commit.msg &&
+ sed -e "1,/^\$/d" commit.msg >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'commit --trailer with -c and "after" as where' '
+ trailer_commit_base &&
+ cat >expected <<-\EOF &&
+ hello
+
+ Signed-off-by: C O Mitter <committer@example.com>
+ Signed-off-by: C1 E1
+ Helped-by: C2 E2
+ Reported-by: C3 E3
+ Mentored-by: C4 E4
+ Mentored-by: C5 E5
+ EOF
+ git -c trailer.where="after" \
+ commit --trailer "Mentored-by: C4 E4" \
+ --trailer "Mentored-by: C5 E5" \
+ --amend &&
+ git cat-file commit HEAD >commit.msg &&
+ sed -e "1,/^\$/d" commit.msg >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'commit --trailer with -c and "before" as where' '
+ trailer_commit_base &&
+ cat >expected <<-\EOF &&
+ hello
+
+ Signed-off-by: C O Mitter <committer@example.com>
+ Signed-off-by: C1 E1
+ Helped-by: C2 E2
+ Reported-by: C3 E3
+ Mentored-by: C2 E2
+ Mentored-by: C3 E3
+ Mentored-by: C4 E4
+ EOF
+ git -c trailer.where="before" \
+ commit --trailer "Mentored-by: C3 E3" \
+ --trailer "Mentored-by: C2 E2" \
+ --amend &&
+ git cat-file commit HEAD >commit.msg &&
+ sed -e "1,/^\$/d" commit.msg >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'commit --trailer with -c and "donothing" as ifmissing' '
+ trailer_commit_base &&
+ cat >expected <<-\EOF &&
+ hello
+
+ Signed-off-by: C O Mitter <committer@example.com>
+ Signed-off-by: C1 E1
+ Helped-by: C2 E2
+ Reported-by: C3 E3
+ Mentored-by: C4 E4
+ Helped-by: C5 E5
+ EOF
+ git -c trailer.ifmissing="donothing" \
+ commit --trailer "Helped-by: C5 E5" \
+ --trailer "Based-by: C6 E6" \
+ --amend &&
+ git cat-file commit HEAD >commit.msg &&
+ sed -e "1,/^\$/d" commit.msg >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'commit --trailer with -c and "add" as ifmissing' '
+ trailer_commit_base &&
+ cat >expected <<-\EOF &&
+ hello
+
+ Signed-off-by: C O Mitter <committer@example.com>
+ Signed-off-by: C1 E1
+ Helped-by: C2 E2
+ Reported-by: C3 E3
+ Mentored-by: C4 E4
+ Helped-by: C5 E5
+ Based-by: C6 E6
+ EOF
+ git -c trailer.ifmissing="add" \
+ commit --trailer "Helped-by: C5 E5" \
+ --trailer "Based-by: C6 E6" \
+ --amend &&
+ git cat-file commit HEAD >commit.msg &&
+ sed -e "1,/^\$/d" commit.msg >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'commit --trailer with -c ack.key ' '
+ echo "fun" >>file1 &&
+ git add file1 &&
+ cat >expected <<-\EOF &&
+ hello
+
+ Acked-by: Peff
+ EOF
+ git -c trailer.ack.key="Acked-by" \
+ commit --trailer "ack = Peff" -m "hello" &&
+ git cat-file commit HEAD >commit.msg &&
+ sed -e "1,/^\$/d" commit.msg >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'commit --trailer with -c and ":=#" as separators' '
+ echo "fun" >>file1 &&
+ git add file1 &&
+ cat >expected <<-\EOF &&
+ I hate bug
+
+ Bug #42
+ EOF
+ git -c trailer.separators=":=#" \
+ -c trailer.bug.key="Bug #" \
+ commit --trailer "bug = 42" -m "I hate bug" &&
+ git cat-file commit HEAD >commit.msg &&
+ sed -e "1,/^\$/d" commit.msg >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'commit --trailer with -c and command' '
+ trailer_commit_base &&
+ cat >expected <<-\EOF &&
+ hello
+
+ Signed-off-by: C O Mitter <committer@example.com>
+ Signed-off-by: C1 E1
+ Helped-by: C2 E2
+ Mentored-by: C4 E4
+ Reported-by: A U Thor <author@example.com>
+ EOF
+ git -c trailer.report.key="Reported-by: " \
+ -c trailer.report.ifexists="replace" \
+ -c trailer.report.command="NAME=\"\$ARG\"; test -n \"\$NAME\" && \
+ git log --author=\"\$NAME\" -1 --format=\"format:%aN <%aE>\" || true" \
+ commit --trailer "report = author" --amend &&
+ git cat-file commit HEAD >commit.msg &&
+ sed -e "1,/^\$/d" commit.msg >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'commit --trailer not confused by --- separator' '
+ cat >msg <<-\EOF &&
+ subject
+
+ body with dashes
+ ---
+ in it
+ EOF
+ git commit --allow-empty --trailer="my-trailer: value" -F msg &&
+ {
+ cat msg &&
+ echo &&
+ echo "my-trailer: value"
+ } >expected &&
+ git cat-file commit HEAD >commit.msg &&
+ sed -e "1,/^\$/d" commit.msg >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'multiple -m' '
+
+ >negative &&
+ git add negative &&
+ git commit -m "one" -m "two" -m "three" &&
+ actual=$(git cat-file commit HEAD >tmp && sed -e "1,/^\$/d" tmp && rm tmp) &&
+ expected=$(test_write_lines "one" "" "two" "" "three") &&
+ test "z$actual" = "z$expected"
+
+'
+
+test_expect_success 'verbose' '
+
+ echo minus >negative &&
+ git add negative &&
+ git status -v >raw &&
+ sed -ne "/^diff --git /p" raw >actual &&
+ echo "diff --git a/negative b/negative" >expect &&
+ test_cmp expect actual
+
+'
+
+test_expect_success 'verbose respects diff config' '
+
+ test_config diff.noprefix true &&
+ git status -v >actual &&
+ grep "diff --git negative negative" actual
+'
+
+mesg_with_comment_and_newlines='
+# text
+
+'
+
+test_expect_success 'prepare file with comment line and trailing newlines' '
+ printf "%s" "$mesg_with_comment_and_newlines" >expect
+'
+
+test_expect_success 'cleanup commit messages (verbatim option,-t)' '
+
+ echo >>negative &&
+ git commit --cleanup=verbatim --no-status -t expect -a &&
+ git cat-file -p HEAD >raw &&
+ sed -e "1,/^\$/d" raw >actual &&
+ test_cmp expect actual
+
+'
+
+test_expect_success 'cleanup commit messages (verbatim option,-F)' '
+
+ echo >>negative &&
+ git commit --cleanup=verbatim -F expect -a &&
+ git cat-file -p HEAD >raw &&
+ sed -e "1,/^\$/d" raw >actual &&
+ test_cmp expect actual
+
+'
+
+test_expect_success 'cleanup commit messages (verbatim option,-m)' '
+
+ echo >>negative &&
+ git commit --cleanup=verbatim -m "$mesg_with_comment_and_newlines" -a &&
+ git cat-file -p HEAD >raw &&
+ sed -e "1,/^\$/d" raw >actual &&
+ test_cmp expect actual
+
+'
+
+test_expect_success 'cleanup commit messages (whitespace option,-F)' '
+
+ echo >>negative &&
+ test_write_lines "" "# text" "" >text &&
+ echo "# text" >expect &&
+ git commit --cleanup=whitespace -F text -a &&
+ git cat-file -p HEAD >raw &&
+ sed -e "1,/^\$/d" raw >actual &&
+ test_cmp expect actual
+
+'
+
+test_expect_success 'cleanup commit messages (scissors option,-F,-e)' '
+
+ echo >>negative &&
+ cat >text <<-\EOF &&
+
+ # to be kept
+
+ # ------------------------ >8 ------------------------
+ # to be kept, too
+ # ------------------------ >8 ------------------------
+ to be removed
+ # ------------------------ >8 ------------------------
+ to be removed, too
+ EOF
+
+ cat >expect <<-\EOF &&
+ # to be kept
+
+ # ------------------------ >8 ------------------------
+ # to be kept, too
+ EOF
+ git commit --cleanup=scissors -e -F text -a &&
+ git cat-file -p HEAD >raw &&
+ sed -e "1,/^\$/d" raw >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'cleanup commit messages (scissors option,-F,-e, scissors on first line)' '
+
+ echo >>negative &&
+ cat >text <<-\EOF &&
+ # ------------------------ >8 ------------------------
+ to be removed
+ EOF
+ git commit --cleanup=scissors -e -F text -a --allow-empty-message &&
+ git cat-file -p HEAD >raw &&
+ sed -e "1,/^\$/d" raw >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'cleanup commit messages (strip option,-F)' '
+
+ echo >>negative &&
+ test_write_lines "" "# text" "sample" "" >text &&
+ echo sample >expect &&
+ git commit --cleanup=strip -F text -a &&
+ git cat-file -p HEAD >raw &&
+ sed -e "1,/^\$/d" raw >actual &&
+ test_cmp expect actual
+
+'
+
+test_expect_success 'cleanup commit messages (strip option,-F,-e)' '
+
+ echo >>negative &&
+ test_write_lines "" "sample" "" >text &&
+ git commit -e -F text -a &&
+ head -n 4 .git/COMMIT_EDITMSG >actual
+'
+
+echo "sample
+
+# Please enter the commit message for your changes. Lines starting
+# with '#' will be ignored, and an empty message aborts the commit." >expect
+
+test_expect_success 'cleanup commit messages (strip option,-F,-e): output' '
+ test_cmp expect actual
+'
+
+test_expect_success 'cleanup commit message (fail on invalid cleanup mode option)' '
+ test_must_fail git commit --cleanup=non-existent
+'
+
+test_expect_success 'cleanup commit message (fail on invalid cleanup mode configuration)' '
+ test_must_fail git -c commit.cleanup=non-existent commit
+'
+
+test_expect_success 'cleanup commit message (no config and no option uses default)' '
+ echo content >>file &&
+ git add file &&
+ (
+ test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
+ git commit --no-status
+ ) &&
+ commit_msg_is "commit message"
+'
+
+test_expect_success 'cleanup commit message (option overrides default)' '
+ echo content >>file &&
+ git add file &&
+ (
+ test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
+ git commit --cleanup=whitespace --no-status
+ ) &&
+ commit_msg_is "commit message # comment"
+'
+
+test_expect_success 'cleanup commit message (config overrides default)' '
+ echo content >>file &&
+ git add file &&
+ (
+ test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
+ git -c commit.cleanup=whitespace commit --no-status
+ ) &&
+ commit_msg_is "commit message # comment"
+'
+
+test_expect_success 'cleanup commit message (option overrides config)' '
+ echo content >>file &&
+ git add file &&
+ (
+ test_set_editor "$TEST_DIRECTORY"/t7500/add-content-and-comment &&
+ git -c commit.cleanup=whitespace commit --cleanup=default
+ ) &&
+ commit_msg_is "commit message"
+'
+
+test_expect_success 'cleanup commit message (default, -m)' '
+ echo content >>file &&
+ git add file &&
+ git commit -m "message #comment " &&
+ commit_msg_is "message #comment"
+'
+
+test_expect_success 'cleanup commit message (whitespace option, -m)' '
+ echo content >>file &&
+ git add file &&
+ git commit --cleanup=whitespace --no-status -m "message #comment " &&
+ commit_msg_is "message #comment"
+'
+
+test_expect_success 'cleanup commit message (whitespace config, -m)' '
+ echo content >>file &&
+ git add file &&
+ git -c commit.cleanup=whitespace commit --no-status -m "message #comment " &&
+ commit_msg_is "message #comment"
+'
+
+test_expect_success 'message shows author when it is not equal to committer' '
+ echo >>negative &&
+ git commit -e -m "sample" -a &&
+ test_grep \
+ "^# Author: *A U Thor <author@example.com>\$" \
+ .git/COMMIT_EDITMSG
+'
+
+test_expect_success 'message shows date when it is explicitly set' '
+ git commit --allow-empty -e -m foo --date="2010-01-02T03:04:05" &&
+ test_grep \
+ "^# Date: *Sat Jan 2 03:04:05 2010 +0000" \
+ .git/COMMIT_EDITMSG
+'
+
+test_expect_success AUTOIDENT 'message shows committer when it is automatic' '
+
+ echo >>negative &&
+ (
+ sane_unset GIT_COMMITTER_EMAIL &&
+ sane_unset GIT_COMMITTER_NAME &&
+ git commit -e -m "sample" -a
+ ) &&
+ # the ident is calculated from the system, so we cannot
+ # check the actual value, only that it is there
+ test_grep "^# Committer: " .git/COMMIT_EDITMSG
+'
+
+write_script .git/FAKE_EDITOR <<EOF
+echo editor started >"$(pwd)/.git/result"
+exit 0
+EOF
+
+test_expect_success !FAIL_PREREQS,!AUTOIDENT 'do not fire editor when committer is bogus' '
+ >.git/result &&
+
+ echo >>negative &&
+ (
+ sane_unset GIT_COMMITTER_EMAIL &&
+ sane_unset GIT_COMMITTER_NAME &&
+ GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" &&
+ export GIT_EDITOR &&
+ test_must_fail git commit -e -m sample -a
+ ) &&
+ test_must_be_empty .git/result
+'
+
+test_expect_success 'do not fire editor if -m <msg> was given' '
+ echo tick >file &&
+ git add file &&
+ echo "editor not started" >.git/result &&
+ (GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" git commit -m tick) &&
+ test "$(cat .git/result)" = "editor not started"
+'
+
+test_expect_success 'do not fire editor if -m "" was given' '
+ echo tock >file &&
+ git add file &&
+ echo "editor not started" >.git/result &&
+ (GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" \
+ git commit -m "" --allow-empty-message) &&
+ test "$(cat .git/result)" = "editor not started"
+'
+
+test_expect_success 'do not fire editor in the presence of conflicts' '
+
+ git clean -f &&
+ echo f >g &&
+ git add g &&
+ git commit -m "add g" &&
+ git branch second &&
+ echo main >g &&
+ echo g >h &&
+ git add g h &&
+ git commit -m "modify g and add h" &&
+ git checkout second &&
+ echo second >g &&
+ git add g &&
+ git commit -m second &&
+ # Must fail due to conflict
+ test_must_fail git cherry-pick -n main &&
+ echo "editor not started" >.git/result &&
+ (
+ GIT_EDITOR="\"$(pwd)/.git/FAKE_EDITOR\"" &&
+ export GIT_EDITOR &&
+ test_must_fail git commit
+ ) &&
+ test "$(cat .git/result)" = "editor not started"
+'
+
+write_script .git/FAKE_EDITOR <<EOF
+# kill -TERM command added below.
+EOF
+
+test_expect_success EXECKEEPSPID 'a SIGTERM should break locks' '
+ echo >>negative &&
+ ! "$SHELL_PATH" -c '\''
+ echo kill -TERM $$ >>.git/FAKE_EDITOR
+ GIT_EDITOR=.git/FAKE_EDITOR
+ export GIT_EDITOR
+ exec git commit -a'\'' &&
+ test ! -f .git/index.lock
+'
+
+rm -f .git/MERGE_MSG .git/COMMIT_EDITMSG
+git reset -q --hard
+
+test_expect_success 'Hand committing of a redundant merge removes dups' '
+
+ git rev-parse second main >expect &&
+ test_must_fail git merge second main &&
+ git checkout main g &&
+ EDITOR=: git commit -a &&
+ git cat-file commit HEAD >raw &&
+ sed -n -e "s/^parent //p" -e "/^$/q" raw >actual &&
+ test_cmp expect actual
+
+'
+
+test_expect_success 'A single-liner subject with a token plus colon is not a footer' '
+
+ git reset --hard &&
+ git commit -s -m "hello: kitty" --allow-empty &&
+ git cat-file commit HEAD >raw &&
+ sed -e "1,/^$/d" raw >actual &&
+ test_line_count = 3 actual
+
+'
+
+test_expect_success 'commit -s places sob on third line after two empty lines' '
+ git commit -s --allow-empty --allow-empty-message &&
+ cat <<-EOF >expect &&
+
+
+ Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
+
+ EOF
+ sed -e "/^#/d" -e "s/^:.*//" .git/COMMIT_EDITMSG >actual &&
+ test_cmp expect actual
+'
+
+write_script .git/FAKE_EDITOR <<\EOF
+mv "$1" "$1.orig"
+(
+ echo message
+ cat "$1.orig"
+) >"$1"
+EOF
+
+echo '## Custom template' >template
+
+try_commit () {
+ git reset --hard &&
+ echo >>negative &&
+ GIT_EDITOR=.git/FAKE_EDITOR git commit -a $* $use_template &&
+ case "$use_template" in
+ '')
+ test_grep ! "^## Custom template" .git/COMMIT_EDITMSG ;;
+ *)
+ test_grep "^## Custom template" .git/COMMIT_EDITMSG ;;
+ esac
+}
+
+try_commit_status_combo () {
+
+ test_expect_success 'commit' '
+ try_commit "" &&
+ test_grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
+ '
+
+ test_expect_success 'commit --status' '
+ try_commit --status &&
+ test_grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
+ '
+
+ test_expect_success 'commit --no-status' '
+ try_commit --no-status &&
+ test_grep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
+ '
+
+ test_expect_success 'commit with commit.status = yes' '
+ test_config commit.status yes &&
+ try_commit "" &&
+ test_grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
+ '
+
+ test_expect_success 'commit with commit.status = no' '
+ test_config commit.status no &&
+ try_commit "" &&
+ test_grep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
+ '
+
+ test_expect_success 'commit --status with commit.status = yes' '
+ test_config commit.status yes &&
+ try_commit --status &&
+ test_grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
+ '
+
+ test_expect_success 'commit --no-status with commit.status = yes' '
+ test_config commit.status yes &&
+ try_commit --no-status &&
+ test_grep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
+ '
+
+ test_expect_success 'commit --status with commit.status = no' '
+ test_config commit.status no &&
+ try_commit --status &&
+ test_grep "^# Changes to be committed:" .git/COMMIT_EDITMSG
+ '
+
+ test_expect_success 'commit --no-status with commit.status = no' '
+ test_config commit.status no &&
+ try_commit --no-status &&
+ test_grep ! "^# Changes to be committed:" .git/COMMIT_EDITMSG
+ '
+
+}
+
+try_commit_status_combo
+
+use_template="-t template"
+
+try_commit_status_combo
+
+test_expect_success 'commit --status with custom comment character' '
+ test_config core.commentchar ";" &&
+ try_commit --status &&
+ test_grep "^; Changes to be committed:" .git/COMMIT_EDITMSG
+'
+
+test_expect_success 'switch core.commentchar' '
+ test_commit "#foo" foo &&
+ GIT_EDITOR=.git/FAKE_EDITOR git -c core.commentChar=auto commit --amend &&
+ test_grep "^; Changes to be committed:" .git/COMMIT_EDITMSG
+'
+
+test_expect_success 'switch core.commentchar but out of options' '
+ cat >text <<\EOF &&
+# 1
+; 2
+@ 3
+! 4
+$ 5
+% 6
+^ 7
+& 8
+| 9
+: 10
+EOF
+ git commit --amend -F text &&
+ (
+ test_set_editor .git/FAKE_EDITOR &&
+ test_must_fail git -c core.commentChar=auto commit --amend
+ )
+'
+
+test_done
diff --git a/t/t7503-pre-commit-and-pre-merge-commit-hooks.sh b/t/t7503-pre-commit-and-pre-merge-commit-hooks.sh
new file mode 100755
index 0000000..aa004b7
--- /dev/null
+++ b/t/t7503-pre-commit-and-pre-merge-commit-hooks.sh
@@ -0,0 +1,283 @@
+#!/bin/sh
+
+test_description='pre-commit and pre-merge-commit hooks'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'root commit' '
+ echo "root" >file &&
+ git add file &&
+ git commit -m "zeroth" &&
+ git checkout -b side &&
+ echo "foo" >foo &&
+ git add foo &&
+ git commit -m "make it non-ff" &&
+ git branch side-orig side &&
+ git checkout main
+'
+
+test_expect_success 'setup conflicting branches' '
+ test_when_finished "git checkout main" &&
+ git checkout -b conflicting-a main &&
+ echo a >conflicting &&
+ git add conflicting &&
+ git commit -m conflicting-a &&
+ git checkout -b conflicting-b main &&
+ echo b >conflicting &&
+ git add conflicting &&
+ git commit -m conflicting-b
+'
+
+test_expect_success 'with no hook' '
+ test_when_finished "rm -f actual_hooks" &&
+ echo "foo" >file &&
+ git add file &&
+ git commit -m "first" &&
+ test_path_is_missing actual_hooks
+'
+
+test_expect_success 'with no hook (merge)' '
+ test_when_finished "rm -f actual_hooks" &&
+ git branch -f side side-orig &&
+ git checkout side &&
+ git merge -m "merge main" main &&
+ git checkout main &&
+ test_path_is_missing actual_hooks
+'
+
+test_expect_success '--no-verify with no hook' '
+ test_when_finished "rm -f actual_hooks" &&
+ echo "bar" >file &&
+ git add file &&
+ git commit --no-verify -m "bar" &&
+ test_path_is_missing actual_hooks
+'
+
+test_expect_success '--no-verify with no hook (merge)' '
+ test_when_finished "rm -f actual_hooks" &&
+ git branch -f side side-orig &&
+ git checkout side &&
+ git merge --no-verify -m "merge main" main &&
+ git checkout main &&
+ test_path_is_missing actual_hooks
+'
+
+setup_success_hook () {
+ test_when_finished "rm -f actual_hooks expected_hooks" &&
+ echo "$1" >expected_hooks &&
+ test_hook "$1" <<-EOF
+ echo $1 >>actual_hooks
+ EOF
+}
+
+test_expect_success 'with succeeding hook' '
+ setup_success_hook "pre-commit" &&
+ echo "more" >>file &&
+ git add file &&
+ git commit -m "more" &&
+ test_cmp expected_hooks actual_hooks
+'
+
+test_expect_success 'with succeeding hook (merge)' '
+ setup_success_hook "pre-merge-commit" &&
+ git checkout side &&
+ git merge -m "merge main" main &&
+ git checkout main &&
+ test_cmp expected_hooks actual_hooks
+'
+
+test_expect_success 'automatic merge fails; both hooks are available' '
+ setup_success_hook "pre-commit" &&
+ setup_success_hook "pre-merge-commit" &&
+
+ git checkout conflicting-a &&
+ test_must_fail git merge -m "merge conflicting-b" conflicting-b &&
+ test_path_is_missing actual_hooks &&
+
+ echo "pre-commit" >expected_hooks &&
+ echo a+b >conflicting &&
+ git add conflicting &&
+ git commit -m "resolve conflict" &&
+ test_cmp expected_hooks actual_hooks
+'
+
+test_expect_success '--no-verify with succeeding hook' '
+ setup_success_hook "pre-commit" &&
+ echo "even more" >>file &&
+ git add file &&
+ git commit --no-verify -m "even more" &&
+ test_path_is_missing actual_hooks
+'
+
+test_expect_success '--no-verify with succeeding hook (merge)' '
+ setup_success_hook "pre-merge-commit" &&
+ git branch -f side side-orig &&
+ git checkout side &&
+ git merge --no-verify -m "merge main" main &&
+ git checkout main &&
+ test_path_is_missing actual_hooks
+'
+
+setup_failing_hook () {
+ test_when_finished "rm -f actual_hooks" &&
+ test_hook "$1" <<-EOF
+ echo $1-failing-hook >>actual_hooks
+ exit 1
+ EOF
+}
+
+test_expect_success 'with failing hook' '
+ setup_failing_hook "pre-commit" &&
+ test_when_finished "rm -f expected_hooks" &&
+ echo "pre-commit-failing-hook" >expected_hooks &&
+
+ echo "another" >>file &&
+ git add file &&
+ test_must_fail git commit -m "another" &&
+ test_cmp expected_hooks actual_hooks
+'
+
+test_expect_success '--no-verify with failing hook' '
+ setup_failing_hook "pre-commit" &&
+ echo "stuff" >>file &&
+ git add file &&
+ git commit --no-verify -m "stuff" &&
+ test_path_is_missing actual_hooks
+'
+
+test_expect_success 'with failing hook (merge)' '
+ setup_failing_hook "pre-merge-commit" &&
+ echo "pre-merge-commit-failing-hook" >expected_hooks &&
+ git checkout side &&
+ test_must_fail git merge -m "merge main" main &&
+ git checkout main &&
+ test_cmp expected_hooks actual_hooks
+'
+
+test_expect_success '--no-verify with failing hook (merge)' '
+ setup_failing_hook "pre-merge-commit" &&
+
+ git branch -f side side-orig &&
+ git checkout side &&
+ git merge --no-verify -m "merge main" main &&
+ git checkout main &&
+ test_path_is_missing actual_hooks
+'
+
+setup_non_exec_hook () {
+ test_when_finished "rm -f actual_hooks" &&
+ test_hook "$1" <<-\EOF &&
+ echo non-exec >>actual_hooks
+ exit 1
+ EOF
+ test_hook --disable "$1"
+}
+
+
+test_expect_success POSIXPERM 'with non-executable hook' '
+ setup_non_exec_hook "pre-commit" &&
+ echo "content" >>file &&
+ git add file &&
+ git commit -m "content" &&
+ test_path_is_missing actual_hooks
+'
+
+test_expect_success POSIXPERM '--no-verify with non-executable hook' '
+ setup_non_exec_hook "pre-commit" &&
+ echo "more content" >>file &&
+ git add file &&
+ git commit --no-verify -m "more content" &&
+ test_path_is_missing actual_hooks
+'
+
+test_expect_success POSIXPERM 'with non-executable hook (merge)' '
+ setup_non_exec_hook "pre-merge" &&
+ git branch -f side side-orig &&
+ git checkout side &&
+ git merge -m "merge main" main &&
+ git checkout main &&
+ test_path_is_missing actual_hooks
+'
+
+test_expect_success POSIXPERM '--no-verify with non-executable hook (merge)' '
+ setup_non_exec_hook "pre-merge" &&
+ git branch -f side side-orig &&
+ git checkout side &&
+ git merge --no-verify -m "merge main" main &&
+ git checkout main &&
+ test_path_is_missing actual_hooks
+'
+
+setup_require_prefix_hook () {
+ test_when_finished "rm -f expected_hooks" &&
+ echo require-prefix >expected_hooks &&
+ test_hook pre-commit <<-\EOF
+ echo require-prefix >>actual_hooks
+ test $GIT_PREFIX = "success/"
+ EOF
+}
+
+test_expect_success 'with hook requiring GIT_PREFIX' '
+ test_when_finished "rm -rf actual_hooks success" &&
+ setup_require_prefix_hook &&
+ echo "more content" >>file &&
+ git add file &&
+ mkdir success &&
+ (
+ cd success &&
+ git commit -m "hook requires GIT_PREFIX = success/"
+ ) &&
+ test_cmp expected_hooks actual_hooks
+'
+
+test_expect_success 'with failing hook requiring GIT_PREFIX' '
+ test_when_finished "rm -rf actual_hooks fail" &&
+ setup_require_prefix_hook &&
+ echo "more content" >>file &&
+ git add file &&
+ mkdir fail &&
+ (
+ cd fail &&
+ test_must_fail git commit -m "hook must fail"
+ ) &&
+ git checkout -- file &&
+ test_cmp expected_hooks actual_hooks
+'
+
+setup_require_author_hook () {
+ test_when_finished "rm -f expected_hooks actual_hooks" &&
+ echo check-author >expected_hooks &&
+ test_hook pre-commit <<-\EOF
+ echo check-author >>actual_hooks
+ test "$GIT_AUTHOR_NAME" = "New Author" &&
+ test "$GIT_AUTHOR_EMAIL" = "newauthor@example.com"
+ EOF
+}
+
+
+test_expect_success 'check the author in hook' '
+ setup_require_author_hook &&
+ cat >expected_hooks <<-EOF &&
+ check-author
+ check-author
+ check-author
+ EOF
+ test_must_fail git commit --allow-empty -m "by a.u.thor" &&
+ (
+ GIT_AUTHOR_NAME="New Author" &&
+ GIT_AUTHOR_EMAIL="newauthor@example.com" &&
+ export GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL &&
+ git commit --allow-empty -m "by new.author via env" &&
+ git show -s
+ ) &&
+ git commit --author="New Author <newauthor@example.com>" \
+ --allow-empty -m "by new.author via command line" &&
+ git show -s &&
+ test_cmp expected_hooks actual_hooks
+'
+
+test_done
diff --git a/t/t7504-commit-msg-hook.sh b/t/t7504-commit-msg-hook.sh
new file mode 100755
index 0000000..d125522
--- /dev/null
+++ b/t/t7504-commit-msg-hook.sh
@@ -0,0 +1,311 @@
+#!/bin/sh
+
+test_description='commit-msg hook'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'with no hook' '
+
+ echo "foo" > file &&
+ git add file &&
+ git commit -m "first"
+
+'
+
+# set up fake editor for interactive editing
+cat > fake-editor <<'EOF'
+#!/bin/sh
+cp FAKE_MSG "$1"
+exit 0
+EOF
+chmod +x fake-editor
+
+## Not using test_set_editor here so we can easily ensure the editor variable
+## is only set for the editor tests
+FAKE_EDITOR="$(pwd)/fake-editor"
+export FAKE_EDITOR
+
+test_expect_success 'with no hook (editor)' '
+
+ echo "more foo" >> file &&
+ git add file &&
+ echo "more foo" > FAKE_MSG &&
+ GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit
+
+'
+
+test_expect_success '--no-verify with no hook' '
+
+ echo "bar" > file &&
+ git add file &&
+ git commit --no-verify -m "bar"
+
+'
+
+test_expect_success '--no-verify with no hook (editor)' '
+
+ echo "more bar" > file &&
+ git add file &&
+ echo "more bar" > FAKE_MSG &&
+ GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
+
+'
+
+test_expect_success 'setup: commit-msg hook that always succeeds' '
+ test_hook --setup commit-msg <<-\EOF
+ exit 0
+ EOF
+'
+
+test_expect_success 'with succeeding hook' '
+
+ echo "more" >> file &&
+ git add file &&
+ git commit -m "more"
+
+'
+
+test_expect_success 'with succeeding hook (editor)' '
+
+ echo "more more" >> file &&
+ git add file &&
+ echo "more more" > FAKE_MSG &&
+ GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit
+
+'
+
+test_expect_success '--no-verify with succeeding hook' '
+
+ echo "even more" >> file &&
+ git add file &&
+ git commit --no-verify -m "even more"
+
+'
+
+test_expect_success '--no-verify with succeeding hook (editor)' '
+
+ echo "even more more" >> file &&
+ git add file &&
+ echo "even more more" > FAKE_MSG &&
+ GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
+
+'
+
+test_expect_success 'setup: commit-msg hook that always fails' '
+ test_hook --clobber commit-msg <<-\EOF
+ exit 1
+ EOF
+'
+
+commit_msg_is () {
+ printf "%s" "$1" >expect &&
+ git log --pretty=format:%s%b -1 >actual &&
+ test_cmp expect actual
+}
+
+test_expect_success 'with failing hook' '
+
+ echo "another" >> file &&
+ git add file &&
+ test_must_fail git commit -m "another"
+
+'
+
+test_expect_success 'with failing hook (editor)' '
+
+ echo "more another" >> file &&
+ git add file &&
+ echo "more another" > FAKE_MSG &&
+ ! (GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit)
+
+'
+
+test_expect_success '--no-verify with failing hook' '
+
+ echo "stuff" >> file &&
+ git add file &&
+ git commit --no-verify -m "stuff"
+
+'
+
+test_expect_success '-n followed by --verify with failing hook' '
+
+ echo "even more" >> file &&
+ git add file &&
+ test_must_fail git commit -n --verify -m "even more"
+
+'
+
+test_expect_success '--no-verify with failing hook (editor)' '
+
+ echo "more stuff" >> file &&
+ git add file &&
+ echo "more stuff" > FAKE_MSG &&
+ GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
+
+'
+
+test_expect_success 'merge fails with failing hook' '
+
+ test_when_finished "git branch -D newbranch" &&
+ test_when_finished "git checkout -f main" &&
+ git checkout --orphan newbranch &&
+ : >file2 &&
+ git add file2 &&
+ git commit --no-verify file2 -m in-side-branch &&
+ test_must_fail git merge --allow-unrelated-histories main &&
+ commit_msg_is "in-side-branch" # HEAD before merge
+
+'
+
+test_expect_success 'merge bypasses failing hook with --no-verify' '
+
+ test_when_finished "git branch -D newbranch" &&
+ test_when_finished "git checkout -f main" &&
+ git checkout --orphan newbranch &&
+ git rm -f file &&
+ : >file2 &&
+ git add file2 &&
+ git commit --no-verify file2 -m in-side-branch &&
+ git merge --no-verify --allow-unrelated-histories main &&
+ commit_msg_is "Merge branch '\''main'\'' into newbranch"
+'
+
+test_expect_success 'setup: commit-msg hook made non-executable' '
+ git_dir="$(git rev-parse --git-dir)" &&
+ chmod -x "$git_dir/hooks/commit-msg"
+'
+
+
+test_expect_success POSIXPERM 'with non-executable hook' '
+
+ echo "content" >file &&
+ git add file &&
+ git commit -m "content"
+
+'
+
+test_expect_success POSIXPERM 'with non-executable hook (editor)' '
+
+ echo "content again" >> file &&
+ git add file &&
+ echo "content again" > FAKE_MSG &&
+ GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -m "content again"
+
+'
+
+test_expect_success POSIXPERM '--no-verify with non-executable hook' '
+
+ echo "more content" >> file &&
+ git add file &&
+ git commit --no-verify -m "more content"
+
+'
+
+test_expect_success POSIXPERM '--no-verify with non-executable hook (editor)' '
+
+ echo "even more content" >> file &&
+ git add file &&
+ echo "even more content" > FAKE_MSG &&
+ GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify
+
+'
+
+test_expect_success 'setup: commit-msg hook that edits the commit message' '
+ test_hook --clobber commit-msg <<-\EOF
+ echo "new message" >"$1"
+ exit 0
+ EOF
+'
+
+test_expect_success 'hook edits commit message' '
+
+ echo "additional" >> file &&
+ git add file &&
+ git commit -m "additional" &&
+ commit_msg_is "new message"
+
+'
+
+test_expect_success 'hook edits commit message (editor)' '
+
+ echo "additional content" >> file &&
+ git add file &&
+ echo "additional content" > FAKE_MSG &&
+ GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit &&
+ commit_msg_is "new message"
+
+'
+
+test_expect_success "hook doesn't edit commit message" '
+
+ echo "plus" >> file &&
+ git add file &&
+ git commit --no-verify -m "plus" &&
+ commit_msg_is "plus"
+
+'
+
+test_expect_success "hook doesn't edit commit message (editor)" '
+
+ echo "more plus" >> file &&
+ git add file &&
+ echo "more plus" > FAKE_MSG &&
+ GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify &&
+ commit_msg_is "more plus"
+'
+
+test_expect_success 'hook called in git-merge picks up commit message' '
+ test_when_finished "git branch -D newbranch" &&
+ test_when_finished "git checkout -f main" &&
+ git checkout --orphan newbranch &&
+ git rm -f file &&
+ : >file2 &&
+ git add file2 &&
+ git commit --no-verify file2 -m in-side-branch &&
+ git merge --allow-unrelated-histories main &&
+ commit_msg_is "new message"
+'
+
+test_expect_failure 'merge --continue remembers --no-verify' '
+ test_when_finished "git branch -D newbranch" &&
+ test_when_finished "git checkout -f main" &&
+ git checkout main &&
+ echo a >file2 &&
+ git add file2 &&
+ git commit --no-verify -m "add file2 to main" &&
+ git checkout -b newbranch main^ &&
+ echo b >file2 &&
+ git add file2 &&
+ git commit --no-verify file2 -m in-side-branch &&
+ git merge --no-verify -m not-rewritten-by-hook main &&
+ # resolve conflict:
+ echo c >file2 &&
+ git add file2 &&
+ git merge --continue &&
+ commit_msg_is not-rewritten-by-hook
+'
+
+# set up fake editor to replace `pick` by `reword`
+cat > reword-editor <<'EOF'
+#!/bin/sh
+mv "$1" "$1".bup &&
+sed 's/^pick/reword/' <"$1".bup >"$1"
+EOF
+chmod +x reword-editor
+REWORD_EDITOR="$(pwd)/reword-editor"
+export REWORD_EDITOR
+
+test_expect_success 'hook is called for reword during `rebase -i`' '
+
+ GIT_SEQUENCE_EDITOR="\"$REWORD_EDITOR\"" git rebase -i HEAD^ &&
+ commit_msg_is "new message"
+
+'
+
+
+test_done
diff --git a/t/t7505-prepare-commit-msg-hook.sh b/t/t7505-prepare-commit-msg-hook.sh
new file mode 100755
index 0000000..2128142
--- /dev/null
+++ b/t/t7505-prepare-commit-msg-hook.sh
@@ -0,0 +1,311 @@
+#!/bin/sh
+
+test_description='prepare-commit-msg hook'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success 'set up commits for rebasing' '
+ test_commit root &&
+ test_commit a a a &&
+ test_commit b b b &&
+ git checkout -b rebase-me root &&
+ test_commit rebase-a a aa &&
+ test_commit rebase-b b bb &&
+ for i in $(test_seq 1 13)
+ do
+ test_commit rebase-$i c $i || return 1
+ done &&
+ git checkout main &&
+
+ cat >rebase-todo <<-EOF
+ pick $(git rev-parse rebase-a)
+ pick $(git rev-parse rebase-b)
+ fixup $(git rev-parse rebase-1)
+ fixup $(git rev-parse rebase-2)
+ pick $(git rev-parse rebase-3)
+ fixup $(git rev-parse rebase-4)
+ squash $(git rev-parse rebase-5)
+ reword $(git rev-parse rebase-6)
+ squash $(git rev-parse rebase-7)
+ fixup $(git rev-parse rebase-8)
+ fixup $(git rev-parse rebase-9)
+ edit $(git rev-parse rebase-10)
+ squash $(git rev-parse rebase-11)
+ squash $(git rev-parse rebase-12)
+ edit $(git rev-parse rebase-13)
+ EOF
+'
+
+test_expect_success 'with no hook' '
+
+ echo "foo" > file &&
+ git add file &&
+ git commit -m "first"
+
+'
+
+test_expect_success 'setup fake editor for interactive editing' '
+ write_script fake-editor <<-\EOF &&
+ exit 0
+ EOF
+
+ ## Not using test_set_editor here so we can easily ensure the editor variable
+ ## is only set for the editor tests
+ FAKE_EDITOR="$(pwd)/fake-editor" &&
+ export FAKE_EDITOR
+'
+
+test_expect_success 'setup prepare-commit-msg hook' '
+ test_hook --setup prepare-commit-msg <<\EOF
+GIT_DIR=$(git rev-parse --git-dir)
+if test -d "$GIT_DIR/rebase-merge"
+then
+ rebasing=1
+else
+ rebasing=0
+fi
+
+get_last_cmd () {
+ tail -n1 "$GIT_DIR/rebase-merge/done" | {
+ read cmd id _
+ git log --pretty="[$cmd %s]" -n1 $id
+ }
+}
+
+if test "$2" = commit
+then
+ if test $rebasing = 1
+ then
+ source="$3"
+ else
+ source=$(git rev-parse "$3")
+ fi
+else
+ source=${2-default}
+fi
+test "$GIT_EDITOR" = : && source="$source (no editor)"
+
+if test $rebasing = 1
+then
+ echo "$source $(get_last_cmd)" >"$1"
+else
+ sed -e "1s/.*/$source/" "$1" >msg.tmp
+ mv msg.tmp "$1"
+fi
+exit 0
+EOF
+'
+
+echo dummy template > "$(git rev-parse --git-dir)/template"
+
+test_expect_success 'with hook (-m)' '
+
+ echo "more" >> file &&
+ git add file &&
+ git commit -m "more" &&
+ test "$(git log -1 --pretty=format:%s)" = "message (no editor)"
+
+'
+
+test_expect_success 'with hook (-m editor)' '
+
+ echo "more" >> file &&
+ git add file &&
+ GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -e -m "more more" &&
+ test "$(git log -1 --pretty=format:%s)" = message
+
+'
+
+test_expect_success 'with hook (-t)' '
+
+ echo "more" >> file &&
+ git add file &&
+ git commit -t "$(git rev-parse --git-dir)/template" &&
+ test "$(git log -1 --pretty=format:%s)" = template
+
+'
+
+test_expect_success 'with hook (-F)' '
+
+ echo "more" >> file &&
+ git add file &&
+ (echo more | git commit -F -) &&
+ test "$(git log -1 --pretty=format:%s)" = "message (no editor)"
+
+'
+
+test_expect_success 'with hook (-F editor)' '
+
+ echo "more" >> file &&
+ git add file &&
+ (echo more more | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -e -F -) &&
+ test "$(git log -1 --pretty=format:%s)" = message
+
+'
+
+test_expect_success 'with hook (-C)' '
+
+ head=$(git rev-parse HEAD) &&
+ echo "more" >> file &&
+ git add file &&
+ git commit -C $head &&
+ test "$(git log -1 --pretty=format:%s)" = "$head (no editor)"
+
+'
+
+test_expect_success 'with hook (editor)' '
+
+ echo "more more" >> file &&
+ git add file &&
+ GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit &&
+ test "$(git log -1 --pretty=format:%s)" = default
+
+'
+
+test_expect_success 'with hook (--amend)' '
+
+ head=$(git rev-parse HEAD) &&
+ echo "more" >> file &&
+ git add file &&
+ GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --amend &&
+ test "$(git log -1 --pretty=format:%s)" = "$head"
+
+'
+
+test_expect_success 'with hook (-c)' '
+
+ head=$(git rev-parse HEAD) &&
+ echo "more" >> file &&
+ git add file &&
+ GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -c $head &&
+ test "$(git log -1 --pretty=format:%s)" = "$head"
+
+'
+
+test_expect_success 'with hook (merge)' '
+
+ test_when_finished "git checkout -f main" &&
+ git checkout -B other HEAD@{1} &&
+ echo "more" >>file &&
+ git add file &&
+ git commit -m other &&
+ git checkout - &&
+ git merge --no-ff other &&
+ test "$(git log -1 --pretty=format:%s)" = "merge (no editor)"
+'
+
+test_expect_success 'with hook and editor (merge)' '
+
+ test_when_finished "git checkout -f main" &&
+ git checkout -B other HEAD@{1} &&
+ echo "more" >>file &&
+ git add file &&
+ git commit -m other &&
+ git checkout - &&
+ env GIT_EDITOR="\"\$FAKE_EDITOR\"" git merge --no-ff -e other &&
+ test "$(git log -1 --pretty=format:%s)" = "merge"
+'
+
+test_rebase () {
+ expect=$1 &&
+ mode=$2 &&
+ test_expect_$expect "with hook (rebase ${mode:--i})" '
+ test_when_finished "\
+ git rebase --abort
+ git checkout -f main
+ git branch -D tmp" &&
+ git checkout -b tmp rebase-me &&
+ GIT_SEQUENCE_EDITOR="cp rebase-todo" &&
+ GIT_EDITOR="\"$FAKE_EDITOR\"" &&
+ (
+ export GIT_SEQUENCE_EDITOR GIT_EDITOR &&
+ test_must_fail git rebase -i $mode b &&
+ echo x >a &&
+ git add a &&
+ test_must_fail git rebase --continue &&
+ echo x >b &&
+ git add b &&
+ git commit &&
+ git rebase --continue &&
+ echo y >a &&
+ git add a &&
+ git commit &&
+ git rebase --continue &&
+ echo y >b &&
+ git add b &&
+ git rebase --continue
+ ) &&
+ git log --pretty=%s -g -n18 HEAD@{1} >actual &&
+ test_cmp "$TEST_DIRECTORY/t7505/expected-rebase${mode:--i}" actual
+ '
+}
+
+test_rebase success
+
+test_expect_success 'with hook (cherry-pick)' '
+ test_when_finished "git checkout -f main" &&
+ git checkout -B other b &&
+ git cherry-pick rebase-1 &&
+ test "$(git log -1 --pretty=format:%s)" = "message (no editor)"
+'
+
+test_expect_success 'with hook and editor (cherry-pick)' '
+ test_when_finished "git checkout -f main" &&
+ git checkout -B other b &&
+ git cherry-pick -e rebase-1 &&
+ test "$(git log -1 --pretty=format:%s)" = merge
+'
+
+test_expect_success 'setup: commit-msg hook that always fails' '
+ test_hook --setup --clobber prepare-commit-msg <<-\EOF
+ exit 1
+ EOF
+'
+
+test_expect_success 'with failing hook' '
+
+ test_when_finished "git checkout -f main" &&
+ head=$(git rev-parse HEAD) &&
+ echo "more" >> file &&
+ git add file &&
+ test_must_fail env GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -c $head
+
+'
+
+test_expect_success 'with failing hook (--no-verify)' '
+
+ test_when_finished "git checkout -f main" &&
+ head=$(git rev-parse HEAD) &&
+ echo "more" >> file &&
+ git add file &&
+ test_must_fail env GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify -c $head
+
+'
+
+test_expect_success 'with failing hook (merge)' '
+
+ test_when_finished "git checkout -f main" &&
+ git checkout -B other HEAD@{1} &&
+ echo "more" >> file &&
+ git add file &&
+ test_hook --remove prepare-commit-msg &&
+ git commit -m other &&
+ test_hook --setup prepare-commit-msg <<-\EOF &&
+ exit 1
+ EOF
+ git checkout - &&
+ test_must_fail git merge --no-ff other
+
+'
+
+test_expect_success 'with failing hook (cherry-pick)' '
+ test_when_finished "git checkout -f main" &&
+ git checkout -B other b &&
+ test_must_fail git cherry-pick rebase-1 2>actual &&
+ test $(grep -c prepare-commit-msg actual) = 1
+'
+
+test_done
diff --git a/t/t7505/expected-rebase-i b/t/t7505/expected-rebase-i
new file mode 100644
index 0000000..93bada5
--- /dev/null
+++ b/t/t7505/expected-rebase-i
@@ -0,0 +1,18 @@
+message [edit rebase-13]
+message (no editor) [edit rebase-13]
+message [squash rebase-12]
+message (no editor) [squash rebase-11]
+default [edit rebase-10]
+message (no editor) [edit rebase-10]
+message [fixup rebase-9]
+message (no editor) [fixup rebase-8]
+message (no editor) [squash rebase-7]
+HEAD [reword rebase-6]
+message (no editor) [reword rebase-6]
+message [squash rebase-5]
+message (no editor) [fixup rebase-4]
+message (no editor) [pick rebase-3]
+message (no editor) [fixup rebase-2]
+message (no editor) [fixup rebase-1]
+merge [pick rebase-b]
+message [pick rebase-a]
diff --git a/t/t7505/expected-rebase-p b/t/t7505/expected-rebase-p
new file mode 100644
index 0000000..93bada5
--- /dev/null
+++ b/t/t7505/expected-rebase-p
@@ -0,0 +1,18 @@
+message [edit rebase-13]
+message (no editor) [edit rebase-13]
+message [squash rebase-12]
+message (no editor) [squash rebase-11]
+default [edit rebase-10]
+message (no editor) [edit rebase-10]
+message [fixup rebase-9]
+message (no editor) [fixup rebase-8]
+message (no editor) [squash rebase-7]
+HEAD [reword rebase-6]
+message (no editor) [reword rebase-6]
+message [squash rebase-5]
+message (no editor) [fixup rebase-4]
+message (no editor) [pick rebase-3]
+message (no editor) [fixup rebase-2]
+message (no editor) [fixup rebase-1]
+merge [pick rebase-b]
+message [pick rebase-a]
diff --git a/t/t7506-status-submodule.sh b/t/t7506-status-submodule.sh
new file mode 100755
index 0000000..46566d5
--- /dev/null
+++ b/t/t7506-status-submodule.sh
@@ -0,0 +1,415 @@
+#!/bin/sh
+
+test_description='git status for submodule'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_create_repo_with_commit () {
+ test_create_repo "$1" &&
+ (
+ cd "$1" &&
+ : >bar &&
+ git add bar &&
+ git commit -m " Add bar" &&
+ : >foo &&
+ git add foo &&
+ git commit -m " Add foo"
+ )
+}
+
+sanitize_output () {
+ sed -e "s/$OID_REGEX/HASH/" -e "s/$OID_REGEX/HASH/" output >output2 &&
+ mv output2 output
+}
+
+sanitize_diff () {
+ sed -e "/^index [0-9a-f,]*\.\.[0-9a-f]*/d" "$1"
+}
+
+
+test_expect_success 'setup' '
+ test_create_repo_with_commit sub &&
+ echo output > .gitignore &&
+ git add sub .gitignore &&
+ git commit -m "Add submodule sub"
+'
+
+test_expect_success 'status clean' '
+ git status >output &&
+ test_grep "nothing to commit" output
+'
+
+test_expect_success 'commit --dry-run -a clean' '
+ test_must_fail git commit --dry-run -a >output &&
+ test_grep "nothing to commit" output
+'
+
+test_expect_success 'status with modified file in submodule' '
+ (cd sub && git reset --hard) &&
+ echo "changed" >sub/foo &&
+ git status >output &&
+ test_grep "modified: sub (modified content)" output
+'
+
+test_expect_success 'status with modified file in submodule (porcelain)' '
+ (cd sub && git reset --hard) &&
+ echo "changed" >sub/foo &&
+ git status --porcelain >output &&
+ diff output - <<-\EOF
+ M sub
+ EOF
+'
+
+test_expect_success 'status with modified file in submodule (short)' '
+ (cd sub && git reset --hard) &&
+ echo "changed" >sub/foo &&
+ git status --short >output &&
+ diff output - <<-\EOF
+ m sub
+ EOF
+'
+
+test_expect_success 'status with added file in submodule' '
+ (cd sub && git reset --hard && echo >foo && git add foo) &&
+ git status >output &&
+ test_grep "modified: sub (modified content)" output
+'
+
+test_expect_success 'status with added file in submodule (porcelain)' '
+ (cd sub && git reset --hard && echo >foo && git add foo) &&
+ git status --porcelain >output &&
+ diff output - <<-\EOF
+ M sub
+ EOF
+'
+
+test_expect_success 'status with added file in submodule (short)' '
+ (cd sub && git reset --hard && echo >foo && git add foo) &&
+ git status --short >output &&
+ diff output - <<-\EOF
+ m sub
+ EOF
+'
+
+test_expect_success 'status with untracked file in submodule' '
+ (cd sub && git reset --hard) &&
+ echo "content" >sub/new-file &&
+ git status >output &&
+ test_grep "modified: sub (untracked content)" output
+'
+
+test_expect_success 'status -uno with untracked file in submodule' '
+ git status -uno >output &&
+ test_grep "^nothing to commit" output
+'
+
+test_expect_success 'status with untracked file in submodule (porcelain)' '
+ git status --porcelain >output &&
+ diff output - <<-\EOF
+ M sub
+ EOF
+'
+
+test_expect_success 'status with untracked file in submodule (short)' '
+ git status --short >output &&
+ diff output - <<-\EOF
+ ? sub
+ EOF
+'
+
+test_expect_success 'status with added and untracked file in submodule' '
+ (cd sub && git reset --hard && echo >foo && git add foo) &&
+ echo "content" >sub/new-file &&
+ git status >output &&
+ test_grep "modified: sub (modified content, untracked content)" output
+'
+
+test_expect_success 'status with added and untracked file in submodule (porcelain)' '
+ (cd sub && git reset --hard && echo >foo && git add foo) &&
+ echo "content" >sub/new-file &&
+ git status --porcelain >output &&
+ diff output - <<-\EOF
+ M sub
+ EOF
+'
+
+test_expect_success 'status with modified file in modified submodule' '
+ (cd sub && git reset --hard) &&
+ rm sub/new-file &&
+ (cd sub && echo "next change" >foo && git commit -m "next change" foo) &&
+ echo "changed" >sub/foo &&
+ git status >output &&
+ test_grep "modified: sub (new commits, modified content)" output
+'
+
+test_expect_success 'status with modified file in modified submodule (porcelain)' '
+ (cd sub && git reset --hard) &&
+ echo "changed" >sub/foo &&
+ git status --porcelain >output &&
+ diff output - <<-\EOF
+ M sub
+ EOF
+'
+
+test_expect_success 'status with added file in modified submodule' '
+ (cd sub && git reset --hard && echo >foo && git add foo) &&
+ git status >output &&
+ test_grep "modified: sub (new commits, modified content)" output
+'
+
+test_expect_success 'status with added file in modified submodule (porcelain)' '
+ (cd sub && git reset --hard && echo >foo && git add foo) &&
+ git status --porcelain >output &&
+ diff output - <<-\EOF
+ M sub
+ EOF
+'
+
+test_expect_success 'status with untracked file in modified submodule' '
+ (cd sub && git reset --hard) &&
+ echo "content" >sub/new-file &&
+ git status >output &&
+ test_grep "modified: sub (new commits, untracked content)" output
+'
+
+test_expect_success 'status with untracked file in modified submodule (porcelain)' '
+ git status --porcelain >output &&
+ diff output - <<-\EOF
+ M sub
+ EOF
+'
+
+test_expect_success 'status with added and untracked file in modified submodule' '
+ (cd sub && git reset --hard && echo >foo && git add foo) &&
+ echo "content" >sub/new-file &&
+ git status >output &&
+ test_grep "modified: sub (new commits, modified content, untracked content)" output
+'
+
+test_expect_success 'status with added and untracked file in modified submodule (porcelain)' '
+ (cd sub && git reset --hard && echo >foo && git add foo) &&
+ echo "content" >sub/new-file &&
+ git status --porcelain >output &&
+ diff output - <<-\EOF
+ M sub
+ EOF
+'
+
+test_expect_success 'setup .git file for sub' '
+ (cd sub &&
+ rm -f new-file &&
+ REAL="$(pwd)/../.real" &&
+ mv .git "$REAL" &&
+ echo "gitdir: $REAL" >.git) &&
+ echo .real >>.gitignore &&
+ git commit -m "added .real to .gitignore" .gitignore
+'
+
+test_expect_success 'status with added file in modified submodule with .git file' '
+ (cd sub && git reset --hard && echo >foo && git add foo) &&
+ git status >output &&
+ test_grep "modified: sub (new commits, modified content)" output
+'
+
+test_expect_success 'status with a lot of untracked files in the submodule' '
+ (
+ cd sub &&
+ i=0 &&
+ while test $i -lt 1024
+ do
+ >some-file-$i &&
+ i=$(( $i + 1 )) || exit 1
+ done
+ ) &&
+ git status --porcelain sub 2>err.actual &&
+ test_must_be_empty err.actual &&
+ rm err.actual
+'
+
+test_expect_success 'rm submodule contents' '
+ rm -rf sub &&
+ mkdir sub
+'
+
+test_expect_success 'status clean (empty submodule dir)' '
+ git status >output &&
+ test_grep "nothing to commit" output
+'
+
+test_expect_success 'status -a clean (empty submodule dir)' '
+ test_must_fail git commit --dry-run -a >output &&
+ test_grep "nothing to commit" output
+'
+
+cat >status_expect <<\EOF
+AA .gitmodules
+A sub1
+EOF
+
+test_expect_success 'status with merge conflict in .gitmodules' '
+ git clone . super &&
+ test_create_repo_with_commit sub1 &&
+ test_tick &&
+ test_create_repo_with_commit sub2 &&
+ test_config_global protocol.file.allow always &&
+ (
+ cd super &&
+ prev=$(git rev-parse HEAD) &&
+ git checkout -b add_sub1 &&
+ git submodule add ../sub1 &&
+ git commit -m "add sub1" &&
+ git checkout -b add_sub2 $prev &&
+ git submodule add ../sub2 &&
+ git commit -m "add sub2" &&
+ git checkout -b merge_conflict_gitmodules &&
+ test_must_fail git merge add_sub1 &&
+ git status -s >../status_actual 2>&1
+ ) &&
+ test_cmp status_actual status_expect
+'
+
+sha1_merge_sub1=$(cd sub1 && git rev-parse HEAD)
+sha1_merge_sub2=$(cd sub2 && git rev-parse HEAD)
+short_sha1_merge_sub1=$(cd sub1 && git rev-parse --short HEAD)
+short_sha1_merge_sub2=$(cd sub2 && git rev-parse --short HEAD)
+cat >diff_expect <<\EOF
+diff --cc .gitmodules
+--- a/.gitmodules
++++ b/.gitmodules
+@@@ -1,3 -1,3 +1,9 @@@
+++<<<<<<< HEAD
+ +[submodule "sub2"]
+ + path = sub2
+ + url = ../sub2
+++=======
++ [submodule "sub1"]
++ path = sub1
++ url = ../sub1
+++>>>>>>> add_sub1
+EOF
+
+cat >diff_submodule_expect <<\EOF
+diff --cc .gitmodules
+--- a/.gitmodules
++++ b/.gitmodules
+@@@ -1,3 -1,3 +1,9 @@@
+++<<<<<<< HEAD
+ +[submodule "sub2"]
+ + path = sub2
+ + url = ../sub2
+++=======
++ [submodule "sub1"]
++ path = sub1
++ url = ../sub1
+++>>>>>>> add_sub1
+EOF
+
+test_expect_success 'diff with merge conflict in .gitmodules' '
+ (
+ cd super &&
+ git diff >../diff_actual 2>&1
+ ) &&
+ sanitize_diff diff_actual >diff_sanitized &&
+ test_cmp diff_expect diff_sanitized
+'
+
+test_expect_success 'diff --submodule with merge conflict in .gitmodules' '
+ (
+ cd super &&
+ git diff --submodule >../diff_submodule_actual 2>&1
+ ) &&
+ sanitize_diff diff_submodule_actual >diff_sanitized &&
+ test_cmp diff_submodule_expect diff_sanitized
+'
+
+# We'll setup different cases for further testing:
+# sub1 will contain a nested submodule,
+# sub2 will have an untracked file
+# sub3 will have an untracked repository
+test_expect_success 'setup superproject with untracked file in nested submodule' '
+ test_config_global protocol.file.allow always &&
+ (
+ cd super &&
+ git clean -dfx &&
+ git rm .gitmodules &&
+ git commit -m "remove .gitmodules" &&
+ git submodule add -f ./sub1 &&
+ git submodule add -f ./sub2 &&
+ git submodule add -f ./sub1 sub3 &&
+ git commit -a -m "messy merge in superproject" &&
+ (
+ cd sub1 &&
+ git submodule add ../sub2 &&
+ git commit -a -m "add sub2 to sub1"
+ ) &&
+ git add sub1 &&
+ git commit -a -m "update sub1 to contain nested sub"
+ ) &&
+ echo content >super/sub1/sub2/file &&
+ echo content >super/sub2/file &&
+ git -C super/sub3 clone ../../sub2 untracked_repository
+'
+
+test_expect_success 'status with untracked file in nested submodule (porcelain)' '
+ git -C super status --porcelain >output &&
+ diff output - <<-\EOF
+ M sub1
+ M sub2
+ M sub3
+ EOF
+'
+
+test_expect_success 'status with untracked file in nested submodule (porcelain=2)' '
+ git -C super status --porcelain=2 >output &&
+ sanitize_output output &&
+ diff output - <<-\EOF
+ 1 .M S..U 160000 160000 160000 HASH HASH sub1
+ 1 .M S..U 160000 160000 160000 HASH HASH sub2
+ 1 .M S..U 160000 160000 160000 HASH HASH sub3
+ EOF
+'
+
+test_expect_success 'status with untracked file in nested submodule (short)' '
+ git -C super status --short >output &&
+ diff output - <<-\EOF
+ ? sub1
+ ? sub2
+ ? sub3
+ EOF
+'
+
+test_expect_success 'setup superproject with modified file in nested submodule' '
+ git -C super/sub1/sub2 add file &&
+ git -C super/sub2 add file
+'
+
+test_expect_success 'status with added file in nested submodule (porcelain)' '
+ git -C super status --porcelain >output &&
+ diff output - <<-\EOF
+ M sub1
+ M sub2
+ M sub3
+ EOF
+'
+
+test_expect_success 'status with added file in nested submodule (porcelain=2)' '
+ git -C super status --porcelain=2 >output &&
+ sanitize_output output &&
+ diff output - <<-\EOF
+ 1 .M S.M. 160000 160000 160000 HASH HASH sub1
+ 1 .M S.M. 160000 160000 160000 HASH HASH sub2
+ 1 .M S..U 160000 160000 160000 HASH HASH sub3
+ EOF
+'
+
+test_expect_success 'status with added file in nested submodule (short)' '
+ git -C super status --short >output &&
+ diff output - <<-\EOF
+ m sub1
+ m sub2
+ ? sub3
+ EOF
+'
+
+test_done
diff --git a/t/t7507-commit-verbose.sh b/t/t7507-commit-verbose.sh
new file mode 100755
index 0000000..c3281b1
--- /dev/null
+++ b/t/t7507-commit-verbose.sh
@@ -0,0 +1,160 @@
+#!/bin/sh
+
+test_description='verbose commit template'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+write_script "check-for-diff" <<\EOF &&
+grep '^diff --git' "$1" >out
+exit 0
+EOF
+test_set_editor "$PWD/check-for-diff"
+
+cat >message <<'EOF'
+subject
+
+body
+EOF
+
+test_expect_success 'setup' '
+ echo content >file &&
+ git add file &&
+ git commit -F message
+'
+
+test_expect_success 'initial commit shows verbose diff' '
+ git commit --amend -v &&
+ test_line_count = 1 out
+'
+
+test_expect_success 'second commit' '
+ echo content modified >file &&
+ git add file &&
+ git commit -F message
+'
+
+check_message() {
+ git log -1 --pretty=format:%s%n%n%b >actual &&
+ test_cmp "$1" actual
+}
+
+test_expect_success 'verbose diff is stripped out' '
+ git commit --amend -v &&
+ check_message message &&
+ test_line_count = 1 out
+'
+
+test_expect_success 'verbose diff is stripped out (mnemonicprefix)' '
+ git config diff.mnemonicprefix true &&
+ git commit --amend -v &&
+ check_message message &&
+ test_line_count = 1 out
+'
+
+cat >diff <<'EOF'
+This is an example commit message that contains a diff.
+
+diff --git c/file i/file
+new file mode 100644
+index 0000000..f95c11d
+--- /dev/null
++++ i/file
+@@ -0,0 +1 @@
++this is some content
+EOF
+
+test_expect_success 'diff in message is retained without -v' '
+ git commit --amend -F diff &&
+ check_message diff
+'
+
+test_expect_success 'diff in message is retained with -v' '
+ git commit --amend -F diff -v &&
+ check_message diff
+'
+
+test_expect_success 'submodule log is stripped out too with -v' '
+ git config diff.submodule log &&
+ test_config_global protocol.file.allow always &&
+ git submodule add ./. sub &&
+ git commit -m "sub added" &&
+ (
+ cd sub &&
+ echo "more" >>file &&
+ git commit -a -m "submodule commit"
+ ) &&
+ (
+ GIT_EDITOR=cat &&
+ export GIT_EDITOR &&
+ test_must_fail git commit -a -v 2>err
+ ) &&
+ test_grep "Aborting commit due to empty commit message." err
+'
+
+test_expect_success 'verbose diff is stripped out with set core.commentChar' '
+ (
+ GIT_EDITOR=cat &&
+ export GIT_EDITOR &&
+ test_must_fail git -c core.commentchar=";" commit -a -v 2>err
+ ) &&
+ test_grep "Aborting commit due to empty commit message." err
+'
+
+test_expect_success 'status does not verbose without --verbose' '
+ git status >actual &&
+ ! grep "^diff --git" actual
+'
+
+test_expect_success 'setup -v -v' '
+ echo dirty >file
+'
+
+for i in true 1
+do
+ test_expect_success "commit.verbose=$i and --verbose omitted" "
+ git -c commit.verbose=$i commit --amend &&
+ test_line_count = 1 out
+ "
+done
+
+for i in false -2 -1 0
+do
+ test_expect_success "commit.verbose=$i and --verbose omitted" "
+ git -c commit.verbose=$i commit --amend &&
+ test_line_count = 0 out
+ "
+done
+
+for i in 2 3
+do
+ test_expect_success "commit.verbose=$i and --verbose omitted" "
+ git -c commit.verbose=$i commit --amend &&
+ test_line_count = 2 out
+ "
+done
+
+for i in true false -2 -1 0 1 2 3
+do
+ test_expect_success "commit.verbose=$i and --verbose" "
+ git -c commit.verbose=$i commit --amend --verbose &&
+ test_line_count = 1 out
+ "
+
+ test_expect_success "commit.verbose=$i and --no-verbose" "
+ git -c commit.verbose=$i commit --amend --no-verbose &&
+ test_line_count = 0 out
+ "
+
+ test_expect_success "commit.verbose=$i and -v -v" "
+ git -c commit.verbose=$i commit --amend -v -v &&
+ test_line_count = 2 out
+ "
+done
+
+test_expect_success "status ignores commit.verbose=true" '
+ git -c commit.verbose=true status >actual &&
+ ! grep "^diff --git actual"
+'
+
+test_done
diff --git a/t/t7508-status.sh b/t/t7508-status.sh
new file mode 100755
index 0000000..a3c18a4
--- /dev/null
+++ b/t/t7508-status.sh
@@ -0,0 +1,1764 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Johannes E. Schindelin
+#
+
+test_description='git status'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-terminal.sh
+
+test_expect_success 'status -h in broken repository' '
+ git config --global advice.statusuoption false &&
+ mkdir broken &&
+ test_when_finished "rm -fr broken" &&
+ (
+ cd broken &&
+ git init &&
+ echo "[status] showuntrackedfiles = CORRUPT" >>.git/config &&
+ test_expect_code 129 git status -h >usage 2>&1
+ ) &&
+ test_grep "[Uu]sage" broken/usage
+'
+
+test_expect_success 'commit -h in broken repository' '
+ mkdir broken &&
+ test_when_finished "rm -fr broken" &&
+ (
+ cd broken &&
+ git init &&
+ echo "[status] showuntrackedfiles = CORRUPT" >>.git/config &&
+ test_expect_code 129 git commit -h >usage 2>&1
+ ) &&
+ test_grep "[Uu]sage" broken/usage
+'
+
+test_expect_success 'create upstream branch' '
+ git checkout -b upstream &&
+ test_commit upstream1 &&
+ test_commit upstream2 &&
+ # leave the first commit on main as root because several
+ # tests depend on this case; for our upstream we only
+ # care about commit counts anyway, so a totally divergent
+ # history is OK
+ git checkout --orphan main
+'
+
+test_expect_success 'setup' '
+ : >tracked &&
+ : >modified &&
+ mkdir dir1 &&
+ : >dir1/tracked &&
+ : >dir1/modified &&
+ mkdir dir2 &&
+ : >dir1/tracked &&
+ : >dir1/modified &&
+ git add . &&
+
+ git status >output &&
+
+ test_tick &&
+ git commit -m initial &&
+ : >untracked &&
+ : >dir1/untracked &&
+ : >dir2/untracked &&
+ echo 1 >dir1/modified &&
+ echo 2 >dir2/modified &&
+ echo 3 >dir2/added &&
+ git add dir2/added &&
+
+ git branch --set-upstream-to=upstream
+'
+
+test_expect_success 'status (1)' '
+ test_grep "use \"git rm --cached <file>\.\.\.\" to unstage" output
+'
+
+strip_comments () {
+ tab=' '
+ sed "s/^\# //; s/^\#$//; s/^#$tab/$tab/" <"$1" >"$1".tmp &&
+ rm "$1" && mv "$1".tmp "$1"
+}
+
+cat >.gitignore <<\EOF
+.gitignore
+expect*
+output*
+EOF
+
+test_expect_success 'status --column' '
+ cat >expect <<\EOF &&
+# On branch main
+# Your branch and '\''upstream'\'' have diverged,
+# and have 1 and 2 different commits each, respectively.
+# (use "git pull" if you want to integrate the remote branch with yours)
+#
+# Changes to be committed:
+# (use "git restore --staged <file>..." to unstage)
+# new file: dir2/added
+#
+# Changes not staged for commit:
+# (use "git add <file>..." to update what will be committed)
+# (use "git restore <file>..." to discard changes in working directory)
+# modified: dir1/modified
+#
+# Untracked files:
+# (use "git add <file>..." to include in what will be committed)
+# dir1/untracked dir2/untracked
+# dir2/modified untracked
+#
+EOF
+ COLUMNS=50 git -c status.displayCommentPrefix=true status --column="column dense" >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'status --column status.displayCommentPrefix=false' '
+ strip_comments expect &&
+ COLUMNS=49 git -c status.displayCommentPrefix=false status --column="column dense" >output &&
+ test_cmp expect output
+'
+
+cat >expect <<\EOF
+# On branch main
+# Your branch and 'upstream' have diverged,
+# and have 1 and 2 different commits each, respectively.
+# (use "git pull" if you want to integrate the remote branch with yours)
+#
+# Changes to be committed:
+# (use "git restore --staged <file>..." to unstage)
+# new file: dir2/added
+#
+# Changes not staged for commit:
+# (use "git add <file>..." to update what will be committed)
+# (use "git restore <file>..." to discard changes in working directory)
+# modified: dir1/modified
+#
+# Untracked files:
+# (use "git add <file>..." to include in what will be committed)
+# dir1/untracked
+# dir2/modified
+# dir2/untracked
+# untracked
+#
+EOF
+
+test_expect_success 'status with status.displayCommentPrefix=true' '
+ git -c status.displayCommentPrefix=true status >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'status with status.displayCommentPrefix=false' '
+ strip_comments expect &&
+ git -c status.displayCommentPrefix=false status >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'status -v' '
+ (cat expect && git diff --cached) >expect-with-v &&
+ git status -v >output &&
+ test_cmp expect-with-v output
+'
+
+test_expect_success 'status -v -v' '
+ (cat expect &&
+ echo "Changes to be committed:" &&
+ git -c diff.mnemonicprefix=true diff --cached &&
+ echo "--------------------------------------------------" &&
+ echo "Changes not staged for commit:" &&
+ git -c diff.mnemonicprefix=true diff) >expect-with-v &&
+ git status -v -v >output &&
+ test_cmp expect-with-v output
+'
+
+test_expect_success 'setup fake editor' '
+ cat >.git/editor <<-\EOF &&
+ #! /bin/sh
+ cp "$1" output
+EOF
+ chmod 755 .git/editor
+'
+
+commit_template_commented () {
+ (
+ EDITOR=.git/editor &&
+ export EDITOR &&
+ # Fails due to empty message
+ test_must_fail git commit
+ ) &&
+ ! grep '^[^#]' output
+}
+
+test_expect_success 'commit ignores status.displayCommentPrefix=false in COMMIT_EDITMSG' '
+ commit_template_commented
+'
+
+cat >expect <<\EOF
+On branch main
+Your branch and 'upstream' have diverged,
+and have 1 and 2 different commits each, respectively.
+
+Changes to be committed:
+ new file: dir2/added
+
+Changes not staged for commit:
+ modified: dir1/modified
+
+Untracked files:
+ dir1/untracked
+ dir2/modified
+ dir2/untracked
+ untracked
+
+EOF
+
+test_expect_success 'status (advice.statusHints false)' '
+ test_config advice.statusHints false &&
+ git status >output &&
+ test_cmp expect output
+
+'
+
+cat >expect <<\EOF
+ M dir1/modified
+A dir2/added
+?? dir1/untracked
+?? dir2/modified
+?? dir2/untracked
+?? untracked
+EOF
+
+test_expect_success 'status -s' '
+
+ git status -s >output &&
+ test_cmp expect output
+
+'
+
+test_expect_success 'status with gitignore' '
+ {
+ echo ".gitignore" &&
+ echo "expect*" &&
+ echo "output" &&
+ echo "untracked"
+ } >.gitignore &&
+
+ cat >expect <<-\EOF &&
+ M dir1/modified
+ A dir2/added
+ ?? dir2/modified
+ EOF
+ git status -s >output &&
+ test_cmp expect output &&
+
+ cat >expect <<-\EOF &&
+ M dir1/modified
+ A dir2/added
+ ?? dir2/modified
+ !! .gitignore
+ !! dir1/untracked
+ !! dir2/untracked
+ !! expect
+ !! expect-with-v
+ !! output
+ !! untracked
+ EOF
+ git status -s --ignored >output &&
+ test_cmp expect output &&
+
+ cat >expect <<\EOF &&
+On branch main
+Your branch and '\''upstream'\'' have diverged,
+and have 1 and 2 different commits each, respectively.
+ (use "git pull" if you want to integrate the remote branch with yours)
+
+Changes to be committed:
+ (use "git restore --staged <file>..." to unstage)
+ new file: dir2/added
+
+Changes not staged for commit:
+ (use "git add <file>..." to update what will be committed)
+ (use "git restore <file>..." to discard changes in working directory)
+ modified: dir1/modified
+
+Untracked files:
+ (use "git add <file>..." to include in what will be committed)
+ dir2/modified
+
+Ignored files:
+ (use "git add -f <file>..." to include in what will be committed)
+ .gitignore
+ dir1/untracked
+ dir2/untracked
+ expect
+ expect-with-v
+ output
+ untracked
+
+EOF
+ git status --ignored >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'status with gitignore (nothing untracked)' '
+ {
+ echo ".gitignore" &&
+ echo "expect*" &&
+ echo "dir2/modified" &&
+ echo "output" &&
+ echo "untracked"
+ } >.gitignore &&
+
+ cat >expect <<-\EOF &&
+ M dir1/modified
+ A dir2/added
+ EOF
+ git status -s >output &&
+ test_cmp expect output &&
+
+ cat >expect <<-\EOF &&
+ M dir1/modified
+ A dir2/added
+ !! .gitignore
+ !! dir1/untracked
+ !! dir2/modified
+ !! dir2/untracked
+ !! expect
+ !! expect-with-v
+ !! output
+ !! untracked
+ EOF
+ git status -s --ignored >output &&
+ test_cmp expect output &&
+
+ cat >expect <<\EOF &&
+On branch main
+Your branch and '\''upstream'\'' have diverged,
+and have 1 and 2 different commits each, respectively.
+ (use "git pull" if you want to integrate the remote branch with yours)
+
+Changes to be committed:
+ (use "git restore --staged <file>..." to unstage)
+ new file: dir2/added
+
+Changes not staged for commit:
+ (use "git add <file>..." to update what will be committed)
+ (use "git restore <file>..." to discard changes in working directory)
+ modified: dir1/modified
+
+Ignored files:
+ (use "git add -f <file>..." to include in what will be committed)
+ .gitignore
+ dir1/untracked
+ dir2/modified
+ dir2/untracked
+ expect
+ expect-with-v
+ output
+ untracked
+
+EOF
+ git status --ignored >output &&
+ test_cmp expect output
+'
+
+cat >.gitignore <<\EOF
+.gitignore
+expect*
+output*
+EOF
+
+cat >expect <<\EOF
+## main...upstream [ahead 1, behind 2]
+ M dir1/modified
+A dir2/added
+?? dir1/untracked
+?? dir2/modified
+?? dir2/untracked
+?? untracked
+EOF
+
+test_expect_success 'status -s -b' '
+
+ git status -s -b >output &&
+ test_cmp expect output
+
+'
+
+test_expect_success 'status -s -z -b' '
+ tr "\\n" Q <expect >expect.q &&
+ mv expect.q expect &&
+ git status -s -z -b >output &&
+ nul_to_q <output >output.q &&
+ mv output.q output &&
+ test_cmp expect output
+'
+
+test_expect_success 'setup dir3' '
+ mkdir dir3 &&
+ : >dir3/untracked1 &&
+ : >dir3/untracked2
+'
+
+test_expect_success 'status -uno' '
+ cat >expect <<EOF &&
+On branch main
+Your branch and '\''upstream'\'' have diverged,
+and have 1 and 2 different commits each, respectively.
+ (use "git pull" if you want to integrate the remote branch with yours)
+
+Changes to be committed:
+ (use "git restore --staged <file>..." to unstage)
+ new file: dir2/added
+
+Changes not staged for commit:
+ (use "git add <file>..." to update what will be committed)
+ (use "git restore <file>..." to discard changes in working directory)
+ modified: dir1/modified
+
+Untracked files not listed (use -u option to show untracked files)
+EOF
+ git status -uno >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'status (status.showUntrackedFiles no)' '
+ test_config status.showuntrackedfiles no &&
+ git status >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'status -uno (advice.statusHints false)' '
+ cat >expect <<EOF &&
+On branch main
+Your branch and '\''upstream'\'' have diverged,
+and have 1 and 2 different commits each, respectively.
+
+Changes to be committed:
+ new file: dir2/added
+
+Changes not staged for commit:
+ modified: dir1/modified
+
+Untracked files not listed
+EOF
+ test_config advice.statusHints false &&
+ git status -uno >output &&
+ test_cmp expect output
+'
+
+cat >expect << EOF
+ M dir1/modified
+A dir2/added
+EOF
+test_expect_success 'status -s -uno' '
+ git status -s -uno >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'status -s (status.showUntrackedFiles no)' '
+ git config status.showuntrackedfiles no &&
+ git status -s >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'status -unormal' '
+ cat >expect <<EOF &&
+On branch main
+Your branch and '\''upstream'\'' have diverged,
+and have 1 and 2 different commits each, respectively.
+ (use "git pull" if you want to integrate the remote branch with yours)
+
+Changes to be committed:
+ (use "git restore --staged <file>..." to unstage)
+ new file: dir2/added
+
+Changes not staged for commit:
+ (use "git add <file>..." to update what will be committed)
+ (use "git restore <file>..." to discard changes in working directory)
+ modified: dir1/modified
+
+Untracked files:
+ (use "git add <file>..." to include in what will be committed)
+ dir1/untracked
+ dir2/modified
+ dir2/untracked
+ dir3/
+ untracked
+
+EOF
+ git status -unormal >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'status (status.showUntrackedFiles normal)' '
+ test_config status.showuntrackedfiles normal &&
+ git status >output &&
+ test_cmp expect output
+'
+
+cat >expect <<EOF
+ M dir1/modified
+A dir2/added
+?? dir1/untracked
+?? dir2/modified
+?? dir2/untracked
+?? dir3/
+?? untracked
+EOF
+test_expect_success 'status -s -unormal' '
+ git status -s -unormal >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'status -s (status.showUntrackedFiles normal)' '
+ git config status.showuntrackedfiles normal &&
+ git status -s >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'status -uall' '
+ cat >expect <<EOF &&
+On branch main
+Your branch and '\''upstream'\'' have diverged,
+and have 1 and 2 different commits each, respectively.
+ (use "git pull" if you want to integrate the remote branch with yours)
+
+Changes to be committed:
+ (use "git restore --staged <file>..." to unstage)
+ new file: dir2/added
+
+Changes not staged for commit:
+ (use "git add <file>..." to update what will be committed)
+ (use "git restore <file>..." to discard changes in working directory)
+ modified: dir1/modified
+
+Untracked files:
+ (use "git add <file>..." to include in what will be committed)
+ dir1/untracked
+ dir2/modified
+ dir2/untracked
+ dir3/untracked1
+ dir3/untracked2
+ untracked
+
+EOF
+ git status -uall >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'status (status.showUntrackedFiles all)' '
+ test_config status.showuntrackedfiles all &&
+ git status >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'teardown dir3' '
+ rm -rf dir3
+'
+
+cat >expect <<EOF
+ M dir1/modified
+A dir2/added
+?? dir1/untracked
+?? dir2/modified
+?? dir2/untracked
+?? untracked
+EOF
+test_expect_success 'status -s -uall' '
+ test_unconfig status.showuntrackedfiles &&
+ git status -s -uall >output &&
+ test_cmp expect output
+'
+test_expect_success 'status -s (status.showUntrackedFiles all)' '
+ test_config status.showuntrackedfiles all &&
+ git status -s >output &&
+ rm -rf dir3 &&
+ test_cmp expect output
+'
+
+test_expect_success 'status with relative paths' '
+ cat >expect <<\EOF &&
+On branch main
+Your branch and '\''upstream'\'' have diverged,
+and have 1 and 2 different commits each, respectively.
+ (use "git pull" if you want to integrate the remote branch with yours)
+
+Changes to be committed:
+ (use "git restore --staged <file>..." to unstage)
+ new file: ../dir2/added
+
+Changes not staged for commit:
+ (use "git add <file>..." to update what will be committed)
+ (use "git restore <file>..." to discard changes in working directory)
+ modified: modified
+
+Untracked files:
+ (use "git add <file>..." to include in what will be committed)
+ untracked
+ ../dir2/modified
+ ../dir2/untracked
+ ../untracked
+
+EOF
+ (cd dir1 && git status) >output &&
+ test_cmp expect output
+'
+
+cat >expect <<\EOF
+ M modified
+A ../dir2/added
+?? untracked
+?? ../dir2/modified
+?? ../dir2/untracked
+?? ../untracked
+EOF
+test_expect_success 'status -s with relative paths' '
+
+ (cd dir1 && git status -s) >output &&
+ test_cmp expect output
+
+'
+
+cat >expect <<\EOF
+ M dir1/modified
+A dir2/added
+?? dir1/untracked
+?? dir2/modified
+?? dir2/untracked
+?? untracked
+EOF
+
+test_expect_success 'status --porcelain ignores relative paths setting' '
+
+ (cd dir1 && git status --porcelain) >output &&
+ test_cmp expect output
+
+'
+
+test_expect_success 'setup unique colors' '
+
+ git config status.color.untracked blue &&
+ git config status.color.branch green &&
+ git config status.color.localBranch yellow &&
+ git config status.color.remoteBranch cyan
+
+'
+
+test_expect_success TTY 'status with color.ui' '
+ cat >expect <<\EOF &&
+On branch <GREEN>main<RESET>
+Your branch and '\''upstream'\'' have diverged,
+and have 1 and 2 different commits each, respectively.
+ (use "git pull" if you want to integrate the remote branch with yours)
+
+Changes to be committed:
+ (use "git restore --staged <file>..." to unstage)
+ <GREEN>new file: dir2/added<RESET>
+
+Changes not staged for commit:
+ (use "git add <file>..." to update what will be committed)
+ (use "git restore <file>..." to discard changes in working directory)
+ <RED>modified: dir1/modified<RESET>
+
+Untracked files:
+ (use "git add <file>..." to include in what will be committed)
+ <BLUE>dir1/untracked<RESET>
+ <BLUE>dir2/modified<RESET>
+ <BLUE>dir2/untracked<RESET>
+ <BLUE>untracked<RESET>
+
+EOF
+ test_config color.ui auto &&
+ test_terminal git status | test_decode_color >output &&
+ test_cmp expect output
+'
+
+test_expect_success TTY 'status with color.status' '
+ test_config color.status auto &&
+ test_terminal git status | test_decode_color >output &&
+ test_cmp expect output
+'
+
+cat >expect <<\EOF
+ <RED>M<RESET> dir1/modified
+<GREEN>A<RESET> dir2/added
+<BLUE>??<RESET> dir1/untracked
+<BLUE>??<RESET> dir2/modified
+<BLUE>??<RESET> dir2/untracked
+<BLUE>??<RESET> untracked
+EOF
+
+test_expect_success TTY 'status -s with color.ui' '
+
+ git config color.ui auto &&
+ test_terminal git status -s | test_decode_color >output &&
+ test_cmp expect output
+
+'
+
+test_expect_success TTY 'status -s with color.status' '
+
+ git config --unset color.ui &&
+ git config color.status auto &&
+ test_terminal git status -s | test_decode_color >output &&
+ test_cmp expect output
+
+'
+
+cat >expect <<\EOF
+## <YELLOW>main<RESET>...<CYAN>upstream<RESET> [ahead <YELLOW>1<RESET>, behind <CYAN>2<RESET>]
+ <RED>M<RESET> dir1/modified
+<GREEN>A<RESET> dir2/added
+<BLUE>??<RESET> dir1/untracked
+<BLUE>??<RESET> dir2/modified
+<BLUE>??<RESET> dir2/untracked
+<BLUE>??<RESET> untracked
+EOF
+
+test_expect_success TTY 'status -s -b with color.status' '
+
+ test_terminal git status -s -b | test_decode_color >output &&
+ test_cmp expect output
+
+'
+
+cat >expect <<\EOF
+ M dir1/modified
+A dir2/added
+?? dir1/untracked
+?? dir2/modified
+?? dir2/untracked
+?? untracked
+EOF
+
+test_expect_success TTY 'status --porcelain ignores color.ui' '
+
+ git config --unset color.status &&
+ git config color.ui auto &&
+ test_terminal git status --porcelain | test_decode_color >output &&
+ test_cmp expect output
+
+'
+
+test_expect_success TTY 'status --porcelain ignores color.status' '
+
+ git config --unset color.ui &&
+ git config color.status auto &&
+ test_terminal git status --porcelain | test_decode_color >output &&
+ test_cmp expect output
+
+'
+
+# recover unconditionally from color tests
+git config --unset color.status
+git config --unset color.ui
+
+test_expect_success 'status --porcelain respects -b' '
+
+ git status --porcelain -b >output &&
+ {
+ echo "## main...upstream [ahead 1, behind 2]" &&
+ cat expect
+ } >tmp &&
+ mv tmp expect &&
+ test_cmp expect output
+
+'
+
+
+
+test_expect_success 'status without relative paths' '
+ cat >expect <<\EOF &&
+On branch main
+Your branch and '\''upstream'\'' have diverged,
+and have 1 and 2 different commits each, respectively.
+ (use "git pull" if you want to integrate the remote branch with yours)
+
+Changes to be committed:
+ (use "git restore --staged <file>..." to unstage)
+ new file: dir2/added
+
+Changes not staged for commit:
+ (use "git add <file>..." to update what will be committed)
+ (use "git restore <file>..." to discard changes in working directory)
+ modified: dir1/modified
+
+Untracked files:
+ (use "git add <file>..." to include in what will be committed)
+ dir1/untracked
+ dir2/modified
+ dir2/untracked
+ untracked
+
+EOF
+ test_config status.relativePaths false &&
+ (cd dir1 && git status) >output &&
+ test_cmp expect output
+
+'
+
+cat >expect <<\EOF
+ M dir1/modified
+A dir2/added
+?? dir1/untracked
+?? dir2/modified
+?? dir2/untracked
+?? untracked
+EOF
+
+test_expect_success 'status -s without relative paths' '
+
+ test_config status.relativePaths false &&
+ (cd dir1 && git status -s) >output &&
+ test_cmp expect output
+
+'
+
+cat >expect <<\EOF
+ M dir1/modified
+A dir2/added
+A "file with spaces"
+?? dir1/untracked
+?? dir2/modified
+?? dir2/untracked
+?? "file with spaces 2"
+?? untracked
+EOF
+
+test_expect_success 'status -s without relative paths' '
+ test_when_finished "git rm --cached \"file with spaces\"; rm -f file*" &&
+ >"file with spaces" &&
+ >"file with spaces 2" &&
+ >"expect with spaces" &&
+ git add "file with spaces" &&
+
+ git status -s >output &&
+ test_cmp expect output &&
+
+ git status -s --ignored >output &&
+ grep "^!! \"expect with spaces\"$" output &&
+ grep -v "^!! " output >output-wo-ignored &&
+ test_cmp expect output-wo-ignored
+'
+
+test_expect_success 'dry-run of partial commit excluding new file in index' '
+ cat >expect <<EOF &&
+On branch main
+Your branch and '\''upstream'\'' have diverged,
+and have 1 and 2 different commits each, respectively.
+
+Changes to be committed:
+ (use "git restore --staged <file>..." to unstage)
+ modified: dir1/modified
+
+Untracked files:
+ (use "git add <file>..." to include in what will be committed)
+ dir1/untracked
+ dir2/
+ untracked
+
+EOF
+ git commit --dry-run dir1/modified >output &&
+ test_cmp expect output
+'
+
+cat >expect <<EOF
+:100644 100644 $EMPTY_BLOB $ZERO_OID M dir1/modified
+EOF
+test_expect_success 'status refreshes the index' '
+ touch dir2/added &&
+ git status &&
+ git diff-files >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'status shows detached HEAD properly after checking out non-local upstream branch' '
+ test_when_finished rm -rf upstream downstream actual &&
+
+ test_create_repo upstream &&
+ test_commit -C upstream foo &&
+
+ git clone upstream downstream &&
+ git -C downstream checkout @{u} &&
+ git -C downstream status >actual &&
+ grep -E "HEAD detached at [0-9a-f]+" actual
+'
+
+test_expect_success 'setup status submodule summary' '
+ test_create_repo sm && (
+ cd sm &&
+ >foo &&
+ git add foo &&
+ git commit -m "Add foo"
+ ) &&
+ git add sm
+'
+
+test_expect_success 'status submodule summary is disabled by default' '
+ cat >expect <<EOF &&
+On branch main
+Your branch and '\''upstream'\'' have diverged,
+and have 1 and 2 different commits each, respectively.
+ (use "git pull" if you want to integrate the remote branch with yours)
+
+Changes to be committed:
+ (use "git restore --staged <file>..." to unstage)
+ new file: dir2/added
+ new file: sm
+
+Changes not staged for commit:
+ (use "git add <file>..." to update what will be committed)
+ (use "git restore <file>..." to discard changes in working directory)
+ modified: dir1/modified
+
+Untracked files:
+ (use "git add <file>..." to include in what will be committed)
+ dir1/untracked
+ dir2/modified
+ dir2/untracked
+ untracked
+
+EOF
+ git status >output &&
+ test_cmp expect output
+'
+
+# we expect the same as the previous test
+test_expect_success 'status --untracked-files=all does not show submodule' '
+ git status --untracked-files=all >output &&
+ test_cmp expect output
+'
+
+cat >expect <<EOF
+ M dir1/modified
+A dir2/added
+A sm
+?? dir1/untracked
+?? dir2/modified
+?? dir2/untracked
+?? untracked
+EOF
+test_expect_success 'status -s submodule summary is disabled by default' '
+ git status -s >output &&
+ test_cmp expect output
+'
+
+# we expect the same as the previous test
+test_expect_success 'status -s --untracked-files=all does not show submodule' '
+ git status -s --untracked-files=all >output &&
+ test_cmp expect output
+'
+
+head=$(cd sm && git rev-parse --short=7 --verify HEAD)
+
+test_expect_success 'status submodule summary' '
+ cat >expect <<EOF &&
+On branch main
+Your branch and '\''upstream'\'' have diverged,
+and have 1 and 2 different commits each, respectively.
+ (use "git pull" if you want to integrate the remote branch with yours)
+
+Changes to be committed:
+ (use "git restore --staged <file>..." to unstage)
+ new file: dir2/added
+ new file: sm
+
+Changes not staged for commit:
+ (use "git add <file>..." to update what will be committed)
+ (use "git restore <file>..." to discard changes in working directory)
+ modified: dir1/modified
+
+Submodule changes to be committed:
+
+* sm 0000000...$head (1):
+ > Add foo
+
+Untracked files:
+ (use "git add <file>..." to include in what will be committed)
+ dir1/untracked
+ dir2/modified
+ dir2/untracked
+ untracked
+
+EOF
+ git config status.submodulesummary 10 &&
+ git status >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'status submodule summary with status.displayCommentPrefix=false' '
+ strip_comments expect &&
+ git -c status.displayCommentPrefix=false status >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'commit with submodule summary ignores status.displayCommentPrefix' '
+ commit_template_commented
+'
+
+cat >expect <<EOF
+ M dir1/modified
+A dir2/added
+A sm
+?? dir1/untracked
+?? dir2/modified
+?? dir2/untracked
+?? untracked
+EOF
+test_expect_success 'status -s submodule summary' '
+ git status -s >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'status submodule summary (clean submodule): commit' '
+ cat >expect-status <<EOF &&
+On branch main
+Your branch and '\''upstream'\'' have diverged,
+and have 2 and 2 different commits each, respectively.
+ (use "git pull" if you want to integrate the remote branch with yours)
+
+Changes not staged for commit:
+ (use "git add <file>..." to update what will be committed)
+ (use "git restore <file>..." to discard changes in working directory)
+ modified: dir1/modified
+
+Untracked files:
+ (use "git add <file>..." to include in what will be committed)
+ dir1/untracked
+ dir2/modified
+ dir2/untracked
+ untracked
+
+no changes added to commit (use "git add" and/or "git commit -a")
+EOF
+ sed "/git pull/d" expect-status > expect-commit &&
+ git commit -m "commit submodule" &&
+ git config status.submodulesummary 10 &&
+ test_must_fail git commit --dry-run >output &&
+ test_cmp expect-commit output &&
+ git status >output &&
+ test_cmp expect-status output
+'
+
+cat >expect <<EOF
+ M dir1/modified
+?? dir1/untracked
+?? dir2/modified
+?? dir2/untracked
+?? untracked
+EOF
+test_expect_success 'status -s submodule summary (clean submodule)' '
+ git status -s >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'status -z implies porcelain' '
+ git status --porcelain |
+ perl -pe "s/\012/\000/g" >expect &&
+ git status -z >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'commit --dry-run submodule summary (--amend)' '
+ cat >expect <<EOF &&
+On branch main
+Your branch and '\''upstream'\'' have diverged,
+and have 2 and 2 different commits each, respectively.
+
+Changes to be committed:
+ (use "git restore --source=HEAD^1 --staged <file>..." to unstage)
+ new file: dir2/added
+ new file: sm
+
+Changes not staged for commit:
+ (use "git add <file>..." to update what will be committed)
+ (use "git restore <file>..." to discard changes in working directory)
+ modified: dir1/modified
+
+Submodule changes to be committed:
+
+* sm 0000000...$head (1):
+ > Add foo
+
+Untracked files:
+ (use "git add <file>..." to include in what will be committed)
+ dir1/untracked
+ dir2/modified
+ dir2/untracked
+ untracked
+
+EOF
+ git config status.submodulesummary 10 &&
+ git commit --dry-run --amend >output &&
+ test_cmp expect output
+'
+
+test_expect_success POSIXPERM,SANITY 'status succeeds in a read-only repository' '
+ test_when_finished "chmod 775 .git" &&
+ (
+ chmod a-w .git &&
+ # make dir1/tracked stat-dirty
+ >dir1/tracked1 && mv -f dir1/tracked1 dir1/tracked &&
+ git status -s >output &&
+ ! grep dir1/tracked output &&
+ # make sure "status" succeeded without writing index out
+ git diff-files | grep dir1/tracked
+ )
+'
+
+(cd sm && echo > bar && git add bar && git commit -q -m 'Add bar') && git add sm
+new_head=$(cd sm && git rev-parse --short=7 --verify HEAD)
+touch .gitmodules
+
+test_expect_success '--ignore-submodules=untracked suppresses submodules with untracked content' '
+ cat > expect << EOF &&
+On branch main
+Your branch and '\''upstream'\'' have diverged,
+and have 2 and 2 different commits each, respectively.
+ (use "git pull" if you want to integrate the remote branch with yours)
+
+Changes to be committed:
+ (use "git restore --staged <file>..." to unstage)
+ modified: sm
+
+Changes not staged for commit:
+ (use "git add <file>..." to update what will be committed)
+ (use "git restore <file>..." to discard changes in working directory)
+ modified: dir1/modified
+
+Submodule changes to be committed:
+
+* sm $head...$new_head (1):
+ > Add bar
+
+Untracked files:
+ (use "git add <file>..." to include in what will be committed)
+ .gitmodules
+ dir1/untracked
+ dir2/modified
+ dir2/untracked
+ untracked
+
+EOF
+ echo modified sm/untracked &&
+ git status --ignore-submodules=untracked >output &&
+ test_cmp expect output
+'
+
+test_expect_success '.gitmodules ignore=untracked suppresses submodules with untracked content' '
+ test_config diff.ignoreSubmodules dirty &&
+ git status >output &&
+ test_cmp expect output &&
+ git config --add -f .gitmodules submodule.subname.ignore untracked &&
+ git config --add -f .gitmodules submodule.subname.path sm &&
+ git status >output &&
+ test_cmp expect output &&
+ git config -f .gitmodules --remove-section submodule.subname
+'
+
+test_expect_success '.git/config ignore=untracked suppresses submodules with untracked content' '
+ git config --add -f .gitmodules submodule.subname.ignore none &&
+ git config --add -f .gitmodules submodule.subname.path sm &&
+ git config --add submodule.subname.ignore untracked &&
+ git config --add submodule.subname.path sm &&
+ git status >output &&
+ test_cmp expect output &&
+ git config --remove-section submodule.subname &&
+ git config --remove-section -f .gitmodules submodule.subname
+'
+
+test_expect_success '--ignore-submodules=dirty suppresses submodules with untracked content' '
+ git status --ignore-submodules=dirty >output &&
+ test_cmp expect output
+'
+
+test_expect_success '.gitmodules ignore=dirty suppresses submodules with untracked content' '
+ test_config diff.ignoreSubmodules dirty &&
+ git status >output &&
+ ! test -s actual &&
+ git config --add -f .gitmodules submodule.subname.ignore dirty &&
+ git config --add -f .gitmodules submodule.subname.path sm &&
+ git status >output &&
+ test_cmp expect output &&
+ git config -f .gitmodules --remove-section submodule.subname
+'
+
+test_expect_success '.git/config ignore=dirty suppresses submodules with untracked content' '
+ git config --add -f .gitmodules submodule.subname.ignore none &&
+ git config --add -f .gitmodules submodule.subname.path sm &&
+ git config --add submodule.subname.ignore dirty &&
+ git config --add submodule.subname.path sm &&
+ git status >output &&
+ test_cmp expect output &&
+ git config --remove-section submodule.subname &&
+ git config -f .gitmodules --remove-section submodule.subname
+'
+
+test_expect_success '--ignore-submodules=dirty suppresses submodules with modified content' '
+ echo modified >sm/foo &&
+ git status --ignore-submodules=dirty >output &&
+ test_cmp expect output
+'
+
+test_expect_success '.gitmodules ignore=dirty suppresses submodules with modified content' '
+ git config --add -f .gitmodules submodule.subname.ignore dirty &&
+ git config --add -f .gitmodules submodule.subname.path sm &&
+ git status >output &&
+ test_cmp expect output &&
+ git config -f .gitmodules --remove-section submodule.subname
+'
+
+test_expect_success '.git/config ignore=dirty suppresses submodules with modified content' '
+ git config --add -f .gitmodules submodule.subname.ignore none &&
+ git config --add -f .gitmodules submodule.subname.path sm &&
+ git config --add submodule.subname.ignore dirty &&
+ git config --add submodule.subname.path sm &&
+ git status >output &&
+ test_cmp expect output &&
+ git config --remove-section submodule.subname &&
+ git config -f .gitmodules --remove-section submodule.subname
+'
+
+test_expect_success "--ignore-submodules=untracked doesn't suppress submodules with modified content" '
+ cat > expect << EOF &&
+On branch main
+Your branch and '\''upstream'\'' have diverged,
+and have 2 and 2 different commits each, respectively.
+ (use "git pull" if you want to integrate the remote branch with yours)
+
+Changes to be committed:
+ (use "git restore --staged <file>..." to unstage)
+ modified: sm
+
+Changes not staged for commit:
+ (use "git add <file>..." to update what will be committed)
+ (use "git restore <file>..." to discard changes in working directory)
+ (commit or discard the untracked or modified content in submodules)
+ modified: dir1/modified
+ modified: sm (modified content)
+
+Submodule changes to be committed:
+
+* sm $head...$new_head (1):
+ > Add bar
+
+Untracked files:
+ (use "git add <file>..." to include in what will be committed)
+ .gitmodules
+ dir1/untracked
+ dir2/modified
+ dir2/untracked
+ untracked
+
+EOF
+ git status --ignore-submodules=untracked > output &&
+ test_cmp expect output
+'
+
+test_expect_success ".gitmodules ignore=untracked doesn't suppress submodules with modified content" '
+ git config --add -f .gitmodules submodule.subname.ignore untracked &&
+ git config --add -f .gitmodules submodule.subname.path sm &&
+ git status >output &&
+ test_cmp expect output &&
+ git config -f .gitmodules --remove-section submodule.subname
+'
+
+test_expect_success ".git/config ignore=untracked doesn't suppress submodules with modified content" '
+ git config --add -f .gitmodules submodule.subname.ignore none &&
+ git config --add -f .gitmodules submodule.subname.path sm &&
+ git config --add submodule.subname.ignore untracked &&
+ git config --add submodule.subname.path sm &&
+ git status >output &&
+ test_cmp expect output &&
+ git config --remove-section submodule.subname &&
+ git config -f .gitmodules --remove-section submodule.subname
+'
+
+head2=$(cd sm && git commit -q -m "2nd commit" foo && git rev-parse --short=7 --verify HEAD)
+
+test_expect_success "--ignore-submodules=untracked doesn't suppress submodule summary" '
+ cat > expect << EOF &&
+On branch main
+Your branch and '\''upstream'\'' have diverged,
+and have 2 and 2 different commits each, respectively.
+ (use "git pull" if you want to integrate the remote branch with yours)
+
+Changes to be committed:
+ (use "git restore --staged <file>..." to unstage)
+ modified: sm
+
+Changes not staged for commit:
+ (use "git add <file>..." to update what will be committed)
+ (use "git restore <file>..." to discard changes in working directory)
+ modified: dir1/modified
+ modified: sm (new commits)
+
+Submodule changes to be committed:
+
+* sm $head...$new_head (1):
+ > Add bar
+
+Submodules changed but not updated:
+
+* sm $new_head...$head2 (1):
+ > 2nd commit
+
+Untracked files:
+ (use "git add <file>..." to include in what will be committed)
+ .gitmodules
+ dir1/untracked
+ dir2/modified
+ dir2/untracked
+ untracked
+
+EOF
+ git status --ignore-submodules=untracked > output &&
+ test_cmp expect output
+'
+
+test_expect_success ".gitmodules ignore=untracked doesn't suppress submodule summary" '
+ git config --add -f .gitmodules submodule.subname.ignore untracked &&
+ git config --add -f .gitmodules submodule.subname.path sm &&
+ git status >output &&
+ test_cmp expect output &&
+ git config -f .gitmodules --remove-section submodule.subname
+'
+
+test_expect_success ".git/config ignore=untracked doesn't suppress submodule summary" '
+ git config --add -f .gitmodules submodule.subname.ignore none &&
+ git config --add -f .gitmodules submodule.subname.path sm &&
+ git config --add submodule.subname.ignore untracked &&
+ git config --add submodule.subname.path sm &&
+ git status >output &&
+ test_cmp expect output &&
+ git config --remove-section submodule.subname &&
+ git config -f .gitmodules --remove-section submodule.subname
+'
+
+test_expect_success "--ignore-submodules=dirty doesn't suppress submodule summary" '
+ git status --ignore-submodules=dirty > output &&
+ test_cmp expect output
+'
+test_expect_success ".gitmodules ignore=dirty doesn't suppress submodule summary" '
+ git config --add -f .gitmodules submodule.subname.ignore dirty &&
+ git config --add -f .gitmodules submodule.subname.path sm &&
+ git status >output &&
+ test_cmp expect output &&
+ git config -f .gitmodules --remove-section submodule.subname
+'
+
+test_expect_success ".git/config ignore=dirty doesn't suppress submodule summary" '
+ git config --add -f .gitmodules submodule.subname.ignore none &&
+ git config --add -f .gitmodules submodule.subname.path sm &&
+ git config --add submodule.subname.ignore dirty &&
+ git config --add submodule.subname.path sm &&
+ git status >output &&
+ test_cmp expect output &&
+ git config --remove-section submodule.subname &&
+ git config -f .gitmodules --remove-section submodule.subname
+'
+
+cat > expect << EOF
+; On branch main
+; Your branch and 'upstream' have diverged,
+; and have 2 and 2 different commits each, respectively.
+; (use "git pull" if you want to integrate the remote branch with yours)
+;
+; Changes to be committed:
+; (use "git restore --staged <file>..." to unstage)
+; modified: sm
+;
+; Changes not staged for commit:
+; (use "git add <file>..." to update what will be committed)
+; (use "git restore <file>..." to discard changes in working directory)
+; modified: dir1/modified
+; modified: sm (new commits)
+;
+; Submodule changes to be committed:
+;
+; * sm $head...$new_head (1):
+; > Add bar
+;
+; Submodules changed but not updated:
+;
+; * sm $new_head...$head2 (1):
+; > 2nd commit
+;
+; Untracked files:
+; (use "git add <file>..." to include in what will be committed)
+; .gitmodules
+; dir1/untracked
+; dir2/modified
+; dir2/untracked
+; untracked
+;
+EOF
+
+test_expect_success "status (core.commentchar with submodule summary)" '
+ test_config core.commentchar ";" &&
+ git -c status.displayCommentPrefix=true status >output &&
+ test_cmp expect output
+'
+
+test_expect_success "status (core.commentchar with two chars with submodule summary)" '
+ test_config core.commentchar ";;" &&
+ test_must_fail git -c status.displayCommentPrefix=true status
+'
+
+test_expect_success "--ignore-submodules=all suppresses submodule summary" '
+ cat > expect << EOF &&
+On branch main
+Your branch and '\''upstream'\'' have diverged,
+and have 2 and 2 different commits each, respectively.
+ (use "git pull" if you want to integrate the remote branch with yours)
+
+Changes not staged for commit:
+ (use "git add <file>..." to update what will be committed)
+ (use "git restore <file>..." to discard changes in working directory)
+ modified: dir1/modified
+
+Untracked files:
+ (use "git add <file>..." to include in what will be committed)
+ .gitmodules
+ dir1/untracked
+ dir2/modified
+ dir2/untracked
+ untracked
+
+no changes added to commit (use "git add" and/or "git commit -a")
+EOF
+ git status --ignore-submodules=all > output &&
+ test_cmp expect output
+'
+
+test_expect_success '.gitmodules ignore=all suppresses unstaged submodule summary' '
+ cat > expect << EOF &&
+On branch main
+Your branch and '\''upstream'\'' have diverged,
+and have 2 and 2 different commits each, respectively.
+ (use "git pull" if you want to integrate the remote branch with yours)
+
+Changes to be committed:
+ (use "git restore --staged <file>..." to unstage)
+ modified: sm
+
+Changes not staged for commit:
+ (use "git add <file>..." to update what will be committed)
+ (use "git restore <file>..." to discard changes in working directory)
+ modified: dir1/modified
+
+Untracked files:
+ (use "git add <file>..." to include in what will be committed)
+ .gitmodules
+ dir1/untracked
+ dir2/modified
+ dir2/untracked
+ untracked
+
+EOF
+ git config --add -f .gitmodules submodule.subname.ignore all &&
+ git config --add -f .gitmodules submodule.subname.path sm &&
+ git status > output &&
+ test_cmp expect output &&
+ git config -f .gitmodules --remove-section submodule.subname
+'
+
+test_expect_success '.git/config ignore=all suppresses unstaged submodule summary' '
+ git config --add -f .gitmodules submodule.subname.ignore none &&
+ git config --add -f .gitmodules submodule.subname.path sm &&
+ git config --add submodule.subname.ignore all &&
+ git config --add submodule.subname.path sm &&
+ git status > output &&
+ test_cmp expect output &&
+ git config --remove-section submodule.subname &&
+ git config -f .gitmodules --remove-section submodule.subname
+'
+
+test_expect_success 'setup of test environment' '
+ git config status.showUntrackedFiles no &&
+ git status -s >expected_short &&
+ git status --no-short >expected_noshort
+'
+
+test_expect_success '"status.short=true" same as "-s"' '
+ git -c status.short=true status >actual &&
+ test_cmp expected_short actual
+'
+
+test_expect_success '"status.short=true" weaker than "--no-short"' '
+ git -c status.short=true status --no-short >actual &&
+ test_cmp expected_noshort actual
+'
+
+test_expect_success '"status.short=false" same as "--no-short"' '
+ git -c status.short=false status >actual &&
+ test_cmp expected_noshort actual
+'
+
+test_expect_success '"status.short=false" weaker than "-s"' '
+ git -c status.short=false status -s >actual &&
+ test_cmp expected_short actual
+'
+
+test_expect_success '"status.branch=true" same as "-b"' '
+ git status -sb >expected_branch &&
+ git -c status.branch=true status -s >actual &&
+ test_cmp expected_branch actual
+'
+
+test_expect_success '"status.branch=true" different from "--no-branch"' '
+ git status -s --no-branch >expected_nobranch &&
+ git -c status.branch=true status -s >actual &&
+ ! test_cmp expected_nobranch actual
+'
+
+test_expect_success '"status.branch=true" weaker than "--no-branch"' '
+ git -c status.branch=true status -s --no-branch >actual &&
+ test_cmp expected_nobranch actual
+'
+
+test_expect_success '"status.branch=true" weaker than "--porcelain"' '
+ git -c status.branch=true status --porcelain >actual &&
+ test_cmp expected_nobranch actual
+'
+
+test_expect_success '"status.branch=false" same as "--no-branch"' '
+ git -c status.branch=false status -s >actual &&
+ test_cmp expected_nobranch actual
+'
+
+test_expect_success '"status.branch=false" weaker than "-b"' '
+ git -c status.branch=false status -sb >actual &&
+ test_cmp expected_branch actual
+'
+
+test_expect_success 'Restore default test environment' '
+ git config --unset status.showUntrackedFiles
+'
+
+test_expect_success 'git commit will commit a staged but ignored submodule' '
+ git config --add -f .gitmodules submodule.subname.ignore all &&
+ git config --add -f .gitmodules submodule.subname.path sm &&
+ git config --add submodule.subname.ignore all &&
+ git status -s --ignore-submodules=dirty >output &&
+ test_grep "^M. sm" output &&
+ GIT_EDITOR="echo hello >>\"\$1\"" &&
+ export GIT_EDITOR &&
+ git commit -uno &&
+ git status -s --ignore-submodules=dirty >output &&
+ test_grep ! "^M. sm" output
+'
+
+test_expect_success 'git commit --dry-run will show a staged but ignored submodule' '
+ git reset HEAD^ &&
+ git add sm &&
+ cat >expect << EOF &&
+On branch main
+Your branch and '\''upstream'\'' have diverged,
+and have 2 and 2 different commits each, respectively.
+
+Changes to be committed:
+ (use "git restore --staged <file>..." to unstage)
+ modified: sm
+
+Changes not staged for commit:
+ (use "git add <file>..." to update what will be committed)
+ (use "git restore <file>..." to discard changes in working directory)
+ modified: dir1/modified
+
+Untracked files not listed (use -u option to show untracked files)
+EOF
+ git commit -uno --dry-run >output &&
+ test_cmp expect output &&
+ git status -s --ignore-submodules=dirty >output &&
+ test_grep "^M. sm" output
+'
+
+test_expect_success 'git commit -m will commit a staged but ignored submodule' '
+ git commit -uno -m message &&
+ git status -s --ignore-submodules=dirty >output &&
+ test_grep ! "^M. sm" output &&
+ git config --remove-section submodule.subname &&
+ git config -f .gitmodules --remove-section submodule.subname
+'
+
+test_expect_success 'show stash info with "--show-stash"' '
+ git reset --hard &&
+ git stash clear &&
+ echo 1 >file &&
+ git add file &&
+ git stash &&
+ git status >expected_default &&
+ git status --show-stash >expected_with_stash &&
+ test_grep "^Your stash currently has 1 entry$" expected_with_stash
+'
+
+test_expect_success 'no stash info with "--show-stash --no-show-stash"' '
+ git status --show-stash --no-show-stash >expected_without_stash &&
+ test_cmp expected_default expected_without_stash
+'
+
+test_expect_success '"status.showStash=false" weaker than "--show-stash"' '
+ git -c status.showStash=false status --show-stash >actual &&
+ test_cmp expected_with_stash actual
+'
+
+test_expect_success '"status.showStash=true" weaker than "--no-show-stash"' '
+ git -c status.showStash=true status --no-show-stash >actual &&
+ test_cmp expected_without_stash actual
+'
+
+test_expect_success 'no additional info if no stash entries' '
+ git stash clear &&
+ git -c status.showStash=true status >actual &&
+ test_cmp expected_without_stash actual
+'
+
+test_expect_success '"No commits yet" should be noted in status output' '
+ git checkout --orphan empty-branch-1 &&
+ git status >output &&
+ test_grep "No commits yet" output
+'
+
+test_expect_success '"No commits yet" should not be noted in status output' '
+ git checkout --orphan empty-branch-2 &&
+ test_commit test-commit-1 &&
+ git status >output &&
+ test_grep ! "No commits yet" output
+'
+
+test_expect_success '"Initial commit" should be noted in commit template' '
+ git checkout --orphan empty-branch-3 &&
+ touch to_be_committed_1 &&
+ git add to_be_committed_1 &&
+ git commit --dry-run >output &&
+ test_grep "Initial commit" output
+'
+
+test_expect_success '"Initial commit" should not be noted in commit template' '
+ git checkout --orphan empty-branch-4 &&
+ test_commit test-commit-2 &&
+ touch to_be_committed_2 &&
+ git add to_be_committed_2 &&
+ git commit --dry-run >output &&
+ test_grep ! "Initial commit" output
+'
+
+test_expect_success '--no-optional-locks prevents index update' '
+ test_set_magic_mtime .git/index &&
+ git --no-optional-locks status &&
+ test_is_magic_mtime .git/index &&
+ git status &&
+ ! test_is_magic_mtime .git/index
+'
+
+test_expect_success 'racy timestamps will be fixed for clean worktree' '
+ echo content >racy-dirty &&
+ echo content >racy-racy &&
+ git add racy* &&
+ git commit -m "racy test files" &&
+ # let status rewrite the index, if necessary; after that we expect
+ # no more index writes unless caused by racy timestamps; note that
+ # timestamps may already be racy now (depending on previous tests)
+ git status &&
+ test_set_magic_mtime .git/index &&
+ git status &&
+ ! test_is_magic_mtime .git/index
+'
+
+test_expect_success 'racy timestamps will be fixed for dirty worktree' '
+ echo content2 >racy-dirty &&
+ git status &&
+ test_set_magic_mtime .git/index &&
+ git status &&
+ ! test_is_magic_mtime .git/index
+'
+
+test_expect_success 'setup slow status advice' '
+ GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main git init slowstatus &&
+ (
+ cd slowstatus &&
+ cat >.gitignore <<-\EOF &&
+ /actual
+ /expected
+ /out
+ EOF
+ git add .gitignore &&
+ git commit -m "Add .gitignore" &&
+ git config advice.statusuoption true
+ )
+'
+
+test_expect_success 'slow status advice when core.untrackedCache and fsmonitor are unset' '
+ (
+ cd slowstatus &&
+ git config core.untrackedCache false &&
+ git config core.fsmonitor false &&
+ GIT_TEST_UF_DELAY_WARNING=1 git status >actual &&
+ cat >expected <<-\EOF &&
+ On branch main
+
+ It took 3.25 seconds to enumerate untracked files.
+ See '\''git help status'\'' for information on how to improve this.
+
+ nothing to commit, working tree clean
+ EOF
+ test_cmp expected actual
+ )
+'
+
+test_expect_success 'slow status advice when core.untrackedCache true, but not fsmonitor' '
+ (
+ cd slowstatus &&
+ git config core.untrackedCache true &&
+ git config core.fsmonitor false &&
+ GIT_TEST_UF_DELAY_WARNING=1 git status >actual &&
+ cat >expected <<-\EOF &&
+ On branch main
+
+ It took 3.25 seconds to enumerate untracked files.
+ See '\''git help status'\'' for information on how to improve this.
+
+ nothing to commit, working tree clean
+ EOF
+ test_cmp expected actual
+ )
+'
+
+test_expect_success 'slow status advice when core.untrackedCache true, and fsmonitor' '
+ (
+ cd slowstatus &&
+ git config core.untrackedCache true &&
+ git config core.fsmonitor true &&
+ GIT_TEST_UF_DELAY_WARNING=1 git status >actual &&
+ cat >expected <<-\EOF &&
+ On branch main
+
+ It took 3.25 seconds to enumerate untracked files,
+ but the results were cached, and subsequent runs may be faster.
+ See '\''git help status'\'' for information on how to improve this.
+
+ nothing to commit, working tree clean
+ EOF
+ test_cmp expected actual
+ )
+'
+
+test_expect_success EXPENSIVE 'status does not re-read unchanged 4 or 8 GiB file' '
+ (
+ mkdir large-file &&
+ cd large-file &&
+ # Files are 2 GiB, 4 GiB, and 8 GiB sparse files.
+ test-tool truncate file-a 0x080000000 &&
+ test-tool truncate file-b 0x100000000 &&
+ test-tool truncate file-c 0x200000000 &&
+ # This will be slow.
+ git add file-a file-b file-c &&
+ git commit -m "add large files" &&
+ git diff-index HEAD file-a file-b file-c >actual &&
+ test_must_be_empty actual
+ )
+'
+
+test_done
diff --git a/t/t7509-commit-authorship.sh b/t/t7509-commit-authorship.sh
new file mode 100755
index 0000000..fd8c8f8
--- /dev/null
+++ b/t/t7509-commit-authorship.sh
@@ -0,0 +1,175 @@
+#!/bin/sh
+#
+# Copyright (c) 2009 Erick Mattos
+#
+
+test_description='commit tests of various authorhip options. '
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+author_header () {
+ git cat-file commit "$1" |
+ sed -n -e '/^$/q' -e '/^author /p'
+}
+
+message_body () {
+ git cat-file commit "$1" |
+ sed -e '1,/^$/d'
+}
+
+test_expect_success '-C option copies authorship and message' '
+ test_commit --author Frigate\ \<flying@over.world\> \
+ "Initial Commit" foo Initial Initial &&
+ echo "Test 1" >>foo &&
+ test_tick &&
+ git commit -a -C Initial &&
+ author_header Initial >expect &&
+ author_header HEAD >actual &&
+ test_cmp expect actual &&
+
+ message_body Initial >expect &&
+ message_body HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '-C option copies only the message with --reset-author' '
+ echo "Test 2" >>foo &&
+ test_tick &&
+ git commit -a -C Initial --reset-author &&
+ echo "author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE" >expect &&
+ author_header HEAD >actual &&
+ test_cmp expect actual &&
+
+ message_body Initial >expect &&
+ message_body HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '-c option copies authorship and message' '
+ echo "Test 3" >>foo &&
+ test_tick &&
+ EDITOR=: VISUAL=: git commit -a -c Initial &&
+ author_header Initial >expect &&
+ author_header HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '-c option copies only the message with --reset-author' '
+ echo "Test 4" >>foo &&
+ test_tick &&
+ EDITOR=: VISUAL=: git commit -a -c Initial --reset-author &&
+ echo "author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE" >expect &&
+ author_header HEAD >actual &&
+ test_cmp expect actual &&
+
+ message_body Initial >expect &&
+ message_body HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--amend option copies authorship' '
+ git checkout Initial &&
+ echo "Test 5" >>foo &&
+ test_tick &&
+ git commit -a --amend -m "amend test" &&
+ author_header Initial >expect &&
+ author_header HEAD >actual &&
+ test_cmp expect actual &&
+
+ echo "amend test" >expect &&
+ message_body HEAD >actual &&
+ test_cmp expect actual
+'
+
+sha1_file() {
+ echo "$*" | sed "s#..#.git/objects/&/#"
+}
+remove_object() {
+ rm -f $(sha1_file "$*")
+}
+
+test_expect_success '--amend option with empty author' '
+ git cat-file commit Initial >tmp &&
+ sed "s/author [^<]* </author </" tmp >empty-author &&
+ sha=$(git hash-object -t commit -w empty-author) &&
+ test_when_finished "remove_object $sha" &&
+ git checkout $sha &&
+ test_when_finished "git checkout Initial" &&
+ echo "Empty author test" >>foo &&
+ test_tick &&
+ test_must_fail git commit -a -m "empty author" --amend 2>err &&
+ test_grep "empty ident" err
+'
+
+test_expect_success '--amend option with missing author' '
+ git cat-file commit Initial >tmp &&
+ sed "s/author [^<]* </author </" tmp >malformed &&
+ sha=$(git hash-object --literally -t commit -w malformed) &&
+ test_when_finished "remove_object $sha" &&
+ git checkout $sha &&
+ test_when_finished "git checkout Initial" &&
+ echo "Missing author test" >>foo &&
+ test_tick &&
+ test_must_fail git commit -a -m "malformed author" --amend 2>err &&
+ test_grep "empty ident" err
+'
+
+test_expect_success '--reset-author makes the commit ours even with --amend option' '
+ git checkout Initial &&
+ echo "Test 6" >>foo &&
+ test_tick &&
+ git commit -a --reset-author -m "Changed again" --amend &&
+ echo "author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE" >expect &&
+ author_header HEAD >actual &&
+ test_cmp expect actual &&
+
+ echo "Changed again" >expect &&
+ message_body HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--reset-author and --author are mutually exclusive' '
+ git checkout Initial &&
+ echo "Test 7" >>foo &&
+ test_tick &&
+ test_must_fail git commit -a --reset-author --author="Xyzzy <frotz@nitfol.xz>"
+'
+
+test_expect_success '--reset-author should be rejected without -c/-C/--amend' '
+ git checkout Initial &&
+ echo "Test 7" >>foo &&
+ test_tick &&
+ test_must_fail git commit -a --reset-author -m done
+'
+
+test_expect_success 'commit respects CHERRY_PICK_HEAD and MERGE_MSG' '
+ echo "cherry-pick 1a" >>foo &&
+ test_tick &&
+ git commit -am "cherry-pick 1" --author="Cherry <cherry@pick.er>" &&
+ git tag cherry-pick-head &&
+ git update-ref CHERRY_PICK_HEAD $(git rev-parse cherry-pick-head) &&
+ echo "This is a MERGE_MSG" >.git/MERGE_MSG &&
+ echo "cherry-pick 1b" >>foo &&
+ test_tick &&
+ git commit -a &&
+ author_header cherry-pick-head >expect &&
+ author_header HEAD >actual &&
+ test_cmp expect actual &&
+
+ echo "This is a MERGE_MSG" >expect &&
+ message_body HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--reset-author with CHERRY_PICK_HEAD' '
+ git update-ref CHERRY_PICK_HEAD $(git rev-parse cherry-pick-head) &&
+ echo "cherry-pick 2" >>foo &&
+ test_tick &&
+ git commit -am "cherry-pick 2" --reset-author &&
+ echo "author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> $GIT_AUTHOR_DATE" >expect &&
+ author_header HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t7510-signed-commit.sh b/t/t7510-signed-commit.sh
new file mode 100755
index 0000000..0d2dd29
--- /dev/null
+++ b/t/t7510-signed-commit.sh
@@ -0,0 +1,448 @@
+#!/bin/sh
+
+test_description='signed commit tests'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+GNUPGHOME_NOT_USED=$GNUPGHOME
+. "$TEST_DIRECTORY/lib-gpg.sh"
+
+test_expect_success GPG 'create signed commits' '
+ test_oid_cache <<-\EOF &&
+ header sha1:gpgsig
+ header sha256:gpgsig-sha256
+ EOF
+
+ test_when_finished "test_unconfig commit.gpgsign" &&
+
+ echo 1 >file && git add file &&
+ test_tick && git commit -S -m initial &&
+ git tag initial &&
+ git branch side &&
+
+ echo 2 >file && test_tick && git commit -a -S -m second &&
+ git tag second &&
+
+ git checkout side &&
+ echo 3 >elif && git add elif &&
+ test_tick && git commit -m "third on side" &&
+
+ git checkout main &&
+ test_tick && git merge -S side &&
+ git tag merge &&
+
+ echo 4 >file && test_tick && git commit -a -m "fourth unsigned" &&
+ git tag fourth-unsigned &&
+
+ test_tick && git commit --amend -S -m "fourth signed" &&
+ git tag fourth-signed &&
+
+ git config commit.gpgsign true &&
+ echo 5 >file && test_tick && git commit -a -m "fifth signed" &&
+ git tag fifth-signed &&
+
+ git config commit.gpgsign false &&
+ echo 6 >file && test_tick && git commit -a -m "sixth" &&
+ git tag sixth-unsigned &&
+
+ git config commit.gpgsign true &&
+ echo 7 >file && test_tick && git commit -a -m "seventh" --no-gpg-sign &&
+ git tag seventh-unsigned &&
+
+ test_tick && git rebase -f HEAD^^ && git tag sixth-signed HEAD^ &&
+ git tag seventh-signed &&
+
+ echo 8 >file && test_tick && git commit -a -m eighth -SB7227189 &&
+ git tag eighth-signed-alt &&
+
+ # commit.gpgsign is still on but this must not be signed
+ echo 9 | git commit-tree HEAD^{tree} >oid &&
+ test_line_count = 1 oid &&
+ git tag ninth-unsigned $(cat oid) &&
+ # explicit -S of course must sign.
+ echo 10 | git commit-tree -S HEAD^{tree} >oid &&
+ test_line_count = 1 oid &&
+ git tag tenth-signed $(cat oid) &&
+
+ # --gpg-sign[=<key-id>] must sign.
+ echo 11 | git commit-tree --gpg-sign HEAD^{tree} >oid &&
+ test_line_count = 1 oid &&
+ git tag eleventh-signed $(cat oid) &&
+ echo 12 | git commit-tree --gpg-sign=B7227189 HEAD^{tree} >oid &&
+ test_line_count = 1 oid &&
+ git tag twelfth-signed-alt $(cat oid)
+'
+
+test_expect_success GPG 'verify and show signatures' '
+ (
+ for commit in initial second merge fourth-signed \
+ fifth-signed sixth-signed seventh-signed tenth-signed \
+ eleventh-signed
+ do
+ git verify-commit $commit &&
+ git show --pretty=short --show-signature $commit >actual &&
+ grep "Good signature from" actual &&
+ ! grep "BAD signature from" actual &&
+ echo $commit OK || exit 1
+ done
+ ) &&
+ (
+ for commit in merge^2 fourth-unsigned sixth-unsigned \
+ seventh-unsigned ninth-unsigned
+ do
+ test_must_fail git verify-commit $commit &&
+ git show --pretty=short --show-signature $commit >actual &&
+ ! grep "Good signature from" actual &&
+ ! grep "BAD signature from" actual &&
+ echo $commit OK || exit 1
+ done
+ ) &&
+ (
+ for commit in eighth-signed-alt twelfth-signed-alt
+ do
+ git show --pretty=short --show-signature $commit >actual &&
+ grep "Good signature from" actual &&
+ ! grep "BAD signature from" actual &&
+ grep "not certified" actual &&
+ echo $commit OK || exit 1
+ done
+ )
+'
+
+test_expect_success GPG 'verify-commit exits failure on unknown signature' '
+ test_must_fail env GNUPGHOME="$GNUPGHOME_NOT_USED" git verify-commit initial 2>actual &&
+ ! grep "Good signature from" actual &&
+ ! grep "BAD signature from" actual &&
+ grep -q -F -e "No public key" -e "public key not found" actual
+'
+
+test_expect_success GPG 'verify-commit exits success on untrusted signature' '
+ git verify-commit eighth-signed-alt 2>actual &&
+ grep "Good signature from" actual &&
+ ! grep "BAD signature from" actual &&
+ grep "not certified" actual
+'
+
+test_expect_success GPG 'verify-commit exits success with matching minTrustLevel' '
+ test_config gpg.minTrustLevel ultimate &&
+ git verify-commit sixth-signed
+'
+
+test_expect_success GPG 'verify-commit exits success with low minTrustLevel' '
+ test_config gpg.minTrustLevel fully &&
+ git verify-commit sixth-signed
+'
+
+test_expect_success GPG 'verify-commit exits failure with high minTrustLevel' '
+ test_config gpg.minTrustLevel ultimate &&
+ test_must_fail git verify-commit eighth-signed-alt
+'
+
+test_expect_success GPG 'verify signatures with --raw' '
+ (
+ for commit in initial second merge fourth-signed fifth-signed sixth-signed seventh-signed
+ do
+ git verify-commit --raw $commit 2>actual &&
+ grep "GOODSIG" actual &&
+ ! grep "BADSIG" actual &&
+ echo $commit OK || exit 1
+ done
+ ) &&
+ (
+ for commit in merge^2 fourth-unsigned sixth-unsigned seventh-unsigned
+ do
+ test_must_fail git verify-commit --raw $commit 2>actual &&
+ ! grep "GOODSIG" actual &&
+ ! grep "BADSIG" actual &&
+ echo $commit OK || exit 1
+ done
+ ) &&
+ (
+ for commit in eighth-signed-alt
+ do
+ git verify-commit --raw $commit 2>actual &&
+ grep "GOODSIG" actual &&
+ ! grep "BADSIG" actual &&
+ grep "TRUST_UNDEFINED" actual &&
+ echo $commit OK || exit 1
+ done
+ )
+'
+
+test_expect_success GPG 'proper header is used for hash algorithm' '
+ git cat-file commit fourth-signed >output &&
+ grep "^$(test_oid header) -----BEGIN PGP SIGNATURE-----" output
+'
+
+test_expect_success GPG 'show signed commit with signature' '
+ git show -s initial >commit &&
+ git show -s --show-signature initial >show &&
+ git verify-commit -v initial >verify.1 2>verify.2 &&
+ git cat-file commit initial >cat &&
+ grep -v -e "gpg: " -e "Warning: " show >show.commit &&
+ grep -e "gpg: " -e "Warning: " show >show.gpg &&
+ grep -v "^ " cat | grep -v "^gpgsig.* " >cat.commit &&
+ test_cmp show.commit commit &&
+ test_cmp show.gpg verify.2 &&
+ test_cmp cat.commit verify.1
+'
+
+test_expect_success GPG 'detect fudged signature' '
+ git cat-file commit seventh-signed >raw &&
+ sed -e "s/^seventh/7th forged/" raw >forged1 &&
+ git hash-object -w -t commit forged1 >forged1.commit &&
+ test_must_fail git verify-commit $(cat forged1.commit) &&
+ git show --pretty=short --show-signature $(cat forged1.commit) >actual1 &&
+ grep "BAD signature from" actual1 &&
+ ! grep "Good signature from" actual1
+'
+
+test_expect_success GPG 'detect fudged signature with NUL' '
+ git cat-file commit seventh-signed >raw &&
+ cat raw >forged2 &&
+ echo Qwik | tr "Q" "\000" >>forged2 &&
+ git hash-object --literally -w -t commit forged2 >forged2.commit &&
+ test_must_fail git verify-commit $(cat forged2.commit) &&
+ git show --pretty=short --show-signature $(cat forged2.commit) >actual2 &&
+ grep "BAD signature from" actual2 &&
+ ! grep "Good signature from" actual2
+'
+
+test_expect_success GPG 'amending already signed commit' '
+ git checkout -f fourth-signed^0 &&
+ git commit --amend -S --no-edit &&
+ git verify-commit HEAD &&
+ git show -s --show-signature HEAD >actual &&
+ grep "Good signature from" actual &&
+ ! grep "BAD signature from" actual
+'
+
+test_expect_success GPG2 'bare signature' '
+ git verify-commit fifth-signed 2>expect &&
+ echo >>expect &&
+ git log -1 --format="%GG" fifth-signed >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success GPG 'show good signature with custom format' '
+ cat >expect <<-\EOF &&
+ G
+ ultimate
+ 13B6F51ECDDE430D
+ C O Mitter <committer@example.com>
+ 73D758744BE721698EC54E8713B6F51ECDDE430D
+ 73D758744BE721698EC54E8713B6F51ECDDE430D
+ EOF
+ git log -1 --format="%G?%n%GT%n%GK%n%GS%n%GF%n%GP" sixth-signed >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success GPG 'show bad signature with custom format' '
+ cat >expect <<-\EOF &&
+ B
+ undefined
+ 13B6F51ECDDE430D
+ C O Mitter <committer@example.com>
+
+
+ EOF
+ git log -1 --format="%G?%n%GT%n%GK%n%GS%n%GF%n%GP" $(cat forged1.commit) >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success GPG 'show untrusted signature with custom format' '
+ cat >expect <<-\EOF &&
+ U
+ undefined
+ 65A0EEA02E30CAD7
+ Eris Discordia <discord@example.net>
+ F8364A59E07FFE9F4D63005A65A0EEA02E30CAD7
+ D4BE22311AD3131E5EDA29A461092E85B7227189
+ EOF
+ git log -1 --format="%G?%n%GT%n%GK%n%GS%n%GF%n%GP" eighth-signed-alt >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success GPG 'show untrusted signature with undefined trust level' '
+ cat >expect <<-\EOF &&
+ U
+ undefined
+ 65A0EEA02E30CAD7
+ Eris Discordia <discord@example.net>
+ F8364A59E07FFE9F4D63005A65A0EEA02E30CAD7
+ D4BE22311AD3131E5EDA29A461092E85B7227189
+ EOF
+ git log -1 --format="%G?%n%GT%n%GK%n%GS%n%GF%n%GP" eighth-signed-alt >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success GPG 'show untrusted signature with ultimate trust level' '
+ cat >expect <<-\EOF &&
+ G
+ ultimate
+ 13B6F51ECDDE430D
+ C O Mitter <committer@example.com>
+ 73D758744BE721698EC54E8713B6F51ECDDE430D
+ 73D758744BE721698EC54E8713B6F51ECDDE430D
+ EOF
+ git log -1 --format="%G?%n%GT%n%GK%n%GS%n%GF%n%GP" sixth-signed >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success GPG 'show unknown signature with custom format' '
+ cat >expect <<-\EOF &&
+ E
+ undefined
+ 65A0EEA02E30CAD7
+
+
+
+ EOF
+ GNUPGHOME="$GNUPGHOME_NOT_USED" git log -1 --format="%G?%n%GT%n%GK%n%GS%n%GF%n%GP" eighth-signed-alt >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success GPG 'show lack of signature with custom format' '
+ cat >expect <<-\EOF &&
+ N
+ undefined
+
+
+
+
+ EOF
+ git log -1 --format="%G?%n%GT%n%GK%n%GS%n%GF%n%GP" seventh-unsigned >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success GPG 'log.showsignature behaves like --show-signature' '
+ test_config log.showsignature true &&
+ git show initial >actual &&
+ grep "gpg: Signature made" actual &&
+ grep "gpg: Good signature" actual
+'
+
+test_expect_success GPG 'check config gpg.format values' '
+ test_config gpg.format openpgp &&
+ git commit -S --amend -m "success" &&
+ test_config gpg.format OpEnPgP &&
+ test_must_fail git commit -S --amend -m "fail"
+'
+
+test_expect_success GPG 'detect fudged commit with double signature' '
+ sed -e "/gpgsig/,/END PGP/d" forged1 >double-base &&
+ sed -n -e "/gpgsig/,/END PGP/p" forged1 | \
+ sed -e "s/^$(test_oid header)//;s/^ //" | gpg --dearmor >double-sig1.sig &&
+ gpg -o double-sig2.sig -u 29472784 --detach-sign double-base &&
+ cat double-sig1.sig double-sig2.sig | gpg --enarmor >double-combined.asc &&
+ sed -e "s/^\(-.*\)ARMORED FILE/\1SIGNATURE/;1s/^/$(test_oid header) /;2,\$s/^/ /" \
+ double-combined.asc > double-gpgsig &&
+ sed -e "/committer/r double-gpgsig" double-base >double-commit &&
+ git hash-object -w -t commit double-commit >double-commit.commit &&
+ test_must_fail git verify-commit $(cat double-commit.commit) &&
+ git show --pretty=short --show-signature $(cat double-commit.commit) >double-actual &&
+ grep "BAD signature from" double-actual &&
+ grep "Good signature from" double-actual
+'
+
+test_expect_success GPG 'show double signature with custom format' '
+ cat >expect <<-\EOF &&
+ E
+
+
+
+
+ EOF
+ git log -1 --format="%G?%n%GK%n%GS%n%GF%n%GP" $(cat double-commit.commit) >actual &&
+ test_cmp expect actual
+'
+
+
+# NEEDSWORK: This test relies on the test_tick commit/author dates from the first
+# 'create signed commits' test even though it creates its own
+test_expect_success GPG 'verify-commit verifies multiply signed commits' '
+ git init multiply-signed &&
+ cd multiply-signed &&
+ test_commit first &&
+ echo 1 >second &&
+ git add second &&
+ tree=$(git write-tree) &&
+ parent=$(git rev-parse HEAD^{commit}) &&
+ git commit --gpg-sign -m second &&
+ git cat-file commit HEAD &&
+ # Avoid trailing whitespace.
+ sed -e "s/^Q//" -e "s/^Z/ /" >commit <<-EOF &&
+ Qtree $tree
+ Qparent $parent
+ Qauthor A U Thor <author@example.com> 1112912653 -0700
+ Qcommitter C O Mitter <committer@example.com> 1112912653 -0700
+ Qgpgsig -----BEGIN PGP SIGNATURE-----
+ QZ
+ Q iHQEABECADQWIQRz11h0S+chaY7FTocTtvUezd5DDQUCX/uBDRYcY29tbWl0dGVy
+ Q QGV4YW1wbGUuY29tAAoJEBO29R7N3kMNd+8AoK1I8mhLHviPH+q2I5fIVgPsEtYC
+ Q AKCTqBh+VabJceXcGIZuF0Ry+udbBQ==
+ Q =tQ0N
+ Q -----END PGP SIGNATURE-----
+ Qgpgsig-sha256 -----BEGIN PGP SIGNATURE-----
+ QZ
+ Q iHQEABECADQWIQRz11h0S+chaY7FTocTtvUezd5DDQUCX/uBIBYcY29tbWl0dGVy
+ Q QGV4YW1wbGUuY29tAAoJEBO29R7N3kMN/NEAn0XO9RYSBj2dFyozi0JKSbssYMtO
+ Q AJwKCQ1BQOtuwz//IjU8TiS+6S4iUw==
+ Q =pIwP
+ Q -----END PGP SIGNATURE-----
+ Q
+ Qsecond
+ EOF
+ head=$(git hash-object -t commit -w commit) &&
+ git reset --hard $head &&
+ git verify-commit $head 2>actual &&
+ grep "Good signature from" actual &&
+ ! grep "BAD signature from" actual
+'
+
+test_expect_success 'custom `gpg.program`' '
+ write_script fake-gpg <<-\EOF &&
+ args="$*"
+
+ # skip uninteresting options
+ while case "$1" in
+ --status-fd=*|--keyid-format=*) ;; # skip
+ *) break;;
+ esac; do shift; done
+
+ case "$1" in
+ -bsau)
+ test -z "$LET_GPG_PROGRAM_FAIL" || {
+ echo "zOMG signing failed!" >&2
+ exit 1
+ }
+ cat >sign.file
+ echo "[GNUPG:] SIG_CREATED $args" >&2
+ echo "-----BEGIN PGP MESSAGE-----"
+ echo "$args"
+ echo "-----END PGP MESSAGE-----"
+ ;;
+ --verify)
+ cat "$2" >verify.file
+ exit 0
+ ;;
+ *)
+ echo "Unhandled args: $*" >&2
+ exit 1
+ ;;
+ esac
+ EOF
+
+ test_config gpg.program "$(pwd)/fake-gpg" &&
+ git commit -S --allow-empty -m signed-commit &&
+ test_path_exists sign.file &&
+ git show --show-signature &&
+ test_path_exists verify.file &&
+
+ test_must_fail env LET_GPG_PROGRAM_FAIL=1 \
+ git commit -S --allow-empty -m must-fail 2>err &&
+ grep zOMG err
+'
+
+test_done
diff --git a/t/t7511-status-index.sh b/t/t7511-status-index.sh
new file mode 100755
index 0000000..4ffa45a
--- /dev/null
+++ b/t/t7511-status-index.sh
@@ -0,0 +1,51 @@
+#!/bin/sh
+
+test_description='git status with certain file name lengths'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+files="0 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v w x y z"
+
+check() {
+ len=$1
+ prefix=$2
+
+ for i in $files
+ do
+ : >$prefix$i
+ done
+
+ test_expect_success "status, filename length $len" "
+ git add $prefix* &&
+ git status
+ "
+ rm $prefix* .git/index
+}
+
+check 1
+check 2 p
+check 3 px
+check 4 pre
+check 5 pref
+check 6 prefi
+check 7 prefix
+check 8 prefix-
+check 9 prefix-p
+check 10 prefix-pr
+check 11 prefix-pre
+check 12 prefix-pref
+check 13 prefix-prefi
+check 14 prefix-prefix
+check 15 prefix-prefix-
+check 16 prefix-prefix-p
+check 17 prefix-prefix-pr
+check 18 prefix-prefix-pre
+check 19 prefix-prefix-pref
+check 20 prefix-prefix-prefi
+check 21 prefix-prefix-prefix
+check 22 prefix-prefix-prefix-
+check 23 prefix-prefix-prefix-p
+check 24 prefix-prefix-prefix-pr
+
+test_done
diff --git a/t/t7512-status-help.sh b/t/t7512-status-help.sh
new file mode 100755
index 0000000..c2ab8a4
--- /dev/null
+++ b/t/t7512-status-help.sh
@@ -0,0 +1,1036 @@
+#!/bin/sh
+#
+# Copyright (c) 2012 Valentin Duperray, Lucien Kong, Franck Jonas,
+# Thomas Nguy, Khoi Nguyen
+# Grenoble INP Ensimag
+#
+
+test_description='git status advice'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+. "$TEST_DIRECTORY"/lib-rebase.sh
+
+set_fake_editor
+
+test_expect_success 'prepare for conflicts' '
+ git config --global advice.statusuoption false &&
+ test_commit init main.txt init &&
+ git branch conflicts &&
+ test_commit on_main main.txt on_main &&
+ git checkout conflicts &&
+ test_commit on_conflicts main.txt on_conflicts
+'
+
+
+test_expect_success 'status when conflicts unresolved' '
+ test_must_fail git merge main &&
+ cat >expected <<\EOF &&
+On branch conflicts
+You have unmerged paths.
+ (fix conflicts and run "git commit")
+ (use "git merge --abort" to abort the merge)
+
+Unmerged paths:
+ (use "git add <file>..." to mark resolution)
+ both modified: main.txt
+
+no changes added to commit (use "git add" and/or "git commit -a")
+EOF
+ git status --untracked-files=no >actual &&
+ test_cmp expected actual
+'
+
+
+test_expect_success 'status when conflicts resolved before commit' '
+ git reset --hard conflicts &&
+ test_must_fail git merge main &&
+ echo one >main.txt &&
+ git add main.txt &&
+ cat >expected <<\EOF &&
+On branch conflicts
+All conflicts fixed but you are still merging.
+ (use "git commit" to conclude merge)
+
+Changes to be committed:
+ modified: main.txt
+
+Untracked files not listed (use -u option to show untracked files)
+EOF
+ git status --untracked-files=no >actual &&
+ test_cmp expected actual
+'
+
+
+test_expect_success 'prepare for rebase conflicts' '
+ git reset --hard main &&
+ git checkout -b rebase_conflicts &&
+ test_commit one_rebase main.txt one &&
+ test_commit two_rebase main.txt two &&
+ test_commit three_rebase main.txt three
+'
+
+
+test_expect_success 'status when rebase --apply in progress before resolving conflicts' '
+ test_when_finished "git rebase --abort" &&
+ ONTO=$(git rev-parse --short HEAD^^) &&
+ test_must_fail git rebase --apply HEAD^ --onto HEAD^^ &&
+ cat >expected <<EOF &&
+rebase in progress; onto $ONTO
+You are currently rebasing branch '\''rebase_conflicts'\'' on '\''$ONTO'\''.
+ (fix conflicts and then run "git rebase --continue")
+ (use "git rebase --skip" to skip this patch)
+ (use "git rebase --abort" to check out the original branch)
+
+Unmerged paths:
+ (use "git restore --staged <file>..." to unstage)
+ (use "git add <file>..." to mark resolution)
+ both modified: main.txt
+
+no changes added to commit (use "git add" and/or "git commit -a")
+EOF
+ git status --untracked-files=no >actual &&
+ test_cmp expected actual
+'
+
+
+test_expect_success 'status when rebase --apply in progress before rebase --continue' '
+ git reset --hard rebase_conflicts &&
+ test_when_finished "git rebase --abort" &&
+ ONTO=$(git rev-parse --short HEAD^^) &&
+ test_must_fail git rebase --apply HEAD^ --onto HEAD^^ &&
+ echo three >main.txt &&
+ git add main.txt &&
+ cat >expected <<EOF &&
+rebase in progress; onto $ONTO
+You are currently rebasing branch '\''rebase_conflicts'\'' on '\''$ONTO'\''.
+ (all conflicts fixed: run "git rebase --continue")
+
+Changes to be committed:
+ (use "git restore --staged <file>..." to unstage)
+ modified: main.txt
+
+Untracked files not listed (use -u option to show untracked files)
+EOF
+ git status --untracked-files=no >actual &&
+ test_cmp expected actual
+'
+
+
+test_expect_success 'prepare for rebase_i_conflicts' '
+ git reset --hard main &&
+ git checkout -b rebase_i_conflicts &&
+ test_commit one_unmerge main.txt one_unmerge &&
+ git branch rebase_i_conflicts_second &&
+ test_commit one_main main.txt one_main &&
+ git checkout rebase_i_conflicts_second &&
+ test_commit one_second main.txt one_second
+'
+
+
+test_expect_success 'status during rebase -i when conflicts unresolved' '
+ test_when_finished "git rebase --abort" &&
+ ONTO=$(git rev-parse --short rebase_i_conflicts) &&
+ LAST_COMMIT=$(git rev-parse --short rebase_i_conflicts_second) &&
+ test_must_fail git rebase -i rebase_i_conflicts &&
+ cat >expected <<EOF &&
+interactive rebase in progress; onto $ONTO
+Last command done (1 command done):
+ pick $LAST_COMMIT one_second
+No commands remaining.
+You are currently rebasing branch '\''rebase_i_conflicts_second'\'' on '\''$ONTO'\''.
+ (fix conflicts and then run "git rebase --continue")
+ (use "git rebase --skip" to skip this patch)
+ (use "git rebase --abort" to check out the original branch)
+
+Unmerged paths:
+ (use "git restore --staged <file>..." to unstage)
+ (use "git add <file>..." to mark resolution)
+ both modified: main.txt
+
+no changes added to commit (use "git add" and/or "git commit -a")
+EOF
+ git status --untracked-files=no >actual &&
+ test_cmp expected actual
+'
+
+
+test_expect_success 'status during rebase -i after resolving conflicts' '
+ git reset --hard rebase_i_conflicts_second &&
+ test_when_finished "git rebase --abort" &&
+ ONTO=$(git rev-parse --short rebase_i_conflicts) &&
+ LAST_COMMIT=$(git rev-parse --short rebase_i_conflicts_second) &&
+ test_must_fail git rebase -i rebase_i_conflicts &&
+ git add main.txt &&
+ cat >expected <<EOF &&
+interactive rebase in progress; onto $ONTO
+Last command done (1 command done):
+ pick $LAST_COMMIT one_second
+No commands remaining.
+You are currently rebasing branch '\''rebase_i_conflicts_second'\'' on '\''$ONTO'\''.
+ (all conflicts fixed: run "git rebase --continue")
+
+Changes to be committed:
+ (use "git restore --staged <file>..." to unstage)
+ modified: main.txt
+
+Untracked files not listed (use -u option to show untracked files)
+EOF
+ git status --untracked-files=no >actual &&
+ test_cmp expected actual
+'
+
+
+test_expect_success 'status when rebasing -i in edit mode' '
+ git reset --hard main &&
+ git checkout -b rebase_i_edit &&
+ test_commit one_rebase_i main.txt one &&
+ test_commit two_rebase_i main.txt two &&
+ COMMIT2=$(git rev-parse --short rebase_i_edit) &&
+ test_commit three_rebase_i main.txt three &&
+ COMMIT3=$(git rev-parse --short rebase_i_edit) &&
+ FAKE_LINES="1 edit 2" &&
+ export FAKE_LINES &&
+ test_when_finished "git rebase --abort" &&
+ ONTO=$(git rev-parse --short HEAD~2) &&
+ git rebase -i HEAD~2 &&
+ cat >expected <<EOF &&
+interactive rebase in progress; onto $ONTO
+Last commands done (2 commands done):
+ pick $COMMIT2 two_rebase_i
+ edit $COMMIT3 three_rebase_i
+No commands remaining.
+You are currently editing a commit while rebasing branch '\''rebase_i_edit'\'' on '\''$ONTO'\''.
+ (use "git commit --amend" to amend the current commit)
+ (use "git rebase --continue" once you are satisfied with your changes)
+
+nothing to commit (use -u to show untracked files)
+EOF
+ git status --untracked-files=no >actual &&
+ test_cmp expected actual
+'
+
+
+test_expect_success 'status when splitting a commit' '
+ git reset --hard main &&
+ git checkout -b split_commit &&
+ test_commit one_split main.txt one &&
+ test_commit two_split main.txt two &&
+ COMMIT2=$(git rev-parse --short split_commit) &&
+ test_commit three_split main.txt three &&
+ COMMIT3=$(git rev-parse --short split_commit) &&
+ test_commit four_split main.txt four &&
+ COMMIT4=$(git rev-parse --short split_commit) &&
+ FAKE_LINES="1 edit 2 3" &&
+ export FAKE_LINES &&
+ test_when_finished "git rebase --abort" &&
+ ONTO=$(git rev-parse --short HEAD~3) &&
+ git rebase -i HEAD~3 &&
+ git reset HEAD^ &&
+ cat >expected <<EOF &&
+interactive rebase in progress; onto $ONTO
+Last commands done (2 commands done):
+ pick $COMMIT2 two_split
+ edit $COMMIT3 three_split
+Next command to do (1 remaining command):
+ pick $COMMIT4 four_split
+ (use "git rebase --edit-todo" to view and edit)
+You are currently splitting a commit while rebasing branch '\''split_commit'\'' on '\''$ONTO'\''.
+ (Once your working directory is clean, run "git rebase --continue")
+
+Changes not staged for commit:
+ (use "git add <file>..." to update what will be committed)
+ (use "git restore <file>..." to discard changes in working directory)
+ modified: main.txt
+
+no changes added to commit (use "git add" and/or "git commit -a")
+EOF
+ git status --untracked-files=no >actual &&
+ test_cmp expected actual
+'
+
+
+test_expect_success 'status after editing the last commit with --amend during a rebase -i' '
+ git reset --hard main &&
+ git checkout -b amend_last &&
+ test_commit one_amend main.txt one &&
+ test_commit two_amend main.txt two &&
+ test_commit three_amend main.txt three &&
+ COMMIT3=$(git rev-parse --short amend_last) &&
+ test_commit four_amend main.txt four &&
+ COMMIT4=$(git rev-parse --short amend_last) &&
+ FAKE_LINES="1 2 edit 3" &&
+ export FAKE_LINES &&
+ test_when_finished "git rebase --abort" &&
+ ONTO=$(git rev-parse --short HEAD~3) &&
+ git rebase -i HEAD~3 &&
+ git commit --amend -m "foo" &&
+ cat >expected <<EOF &&
+interactive rebase in progress; onto $ONTO
+Last commands done (3 commands done):
+ pick $COMMIT3 three_amend
+ edit $COMMIT4 four_amend
+ (see more in file .git/rebase-merge/done)
+No commands remaining.
+You are currently editing a commit while rebasing branch '\''amend_last'\'' on '\''$ONTO'\''.
+ (use "git commit --amend" to amend the current commit)
+ (use "git rebase --continue" once you are satisfied with your changes)
+
+nothing to commit (use -u to show untracked files)
+EOF
+ git status --untracked-files=no >actual &&
+ test_cmp expected actual
+'
+
+
+test_expect_success 'prepare for several edits' '
+ git reset --hard main &&
+ git checkout -b several_edits &&
+ test_commit one_edits main.txt one &&
+ test_commit two_edits main.txt two &&
+ test_commit three_edits main.txt three &&
+ test_commit four_edits main.txt four
+'
+
+
+test_expect_success 'status: (continue first edit) second edit' '
+ FAKE_LINES="edit 1 edit 2 3" &&
+ export FAKE_LINES &&
+ test_when_finished "git rebase --abort" &&
+ COMMIT2=$(git rev-parse --short several_edits^^) &&
+ COMMIT3=$(git rev-parse --short several_edits^) &&
+ COMMIT4=$(git rev-parse --short several_edits) &&
+ ONTO=$(git rev-parse --short HEAD~3) &&
+ git rebase -i HEAD~3 &&
+ git rebase --continue &&
+ cat >expected <<EOF &&
+interactive rebase in progress; onto $ONTO
+Last commands done (2 commands done):
+ edit $COMMIT2 two_edits
+ edit $COMMIT3 three_edits
+Next command to do (1 remaining command):
+ pick $COMMIT4 four_edits
+ (use "git rebase --edit-todo" to view and edit)
+You are currently editing a commit while rebasing branch '\''several_edits'\'' on '\''$ONTO'\''.
+ (use "git commit --amend" to amend the current commit)
+ (use "git rebase --continue" once you are satisfied with your changes)
+
+nothing to commit (use -u to show untracked files)
+EOF
+ git status --untracked-files=no >actual &&
+ test_cmp expected actual
+'
+
+
+test_expect_success 'status: (continue first edit) second edit and split' '
+ git reset --hard several_edits &&
+ FAKE_LINES="edit 1 edit 2 3" &&
+ export FAKE_LINES &&
+ test_when_finished "git rebase --abort" &&
+ COMMIT2=$(git rev-parse --short several_edits^^) &&
+ COMMIT3=$(git rev-parse --short several_edits^) &&
+ COMMIT4=$(git rev-parse --short several_edits) &&
+ ONTO=$(git rev-parse --short HEAD~3) &&
+ git rebase -i HEAD~3 &&
+ git rebase --continue &&
+ git reset HEAD^ &&
+ cat >expected <<EOF &&
+interactive rebase in progress; onto $ONTO
+Last commands done (2 commands done):
+ edit $COMMIT2 two_edits
+ edit $COMMIT3 three_edits
+Next command to do (1 remaining command):
+ pick $COMMIT4 four_edits
+ (use "git rebase --edit-todo" to view and edit)
+You are currently splitting a commit while rebasing branch '\''several_edits'\'' on '\''$ONTO'\''.
+ (Once your working directory is clean, run "git rebase --continue")
+
+Changes not staged for commit:
+ (use "git add <file>..." to update what will be committed)
+ (use "git restore <file>..." to discard changes in working directory)
+ modified: main.txt
+
+no changes added to commit (use "git add" and/or "git commit -a")
+EOF
+ git status --untracked-files=no >actual &&
+ test_cmp expected actual
+'
+
+
+test_expect_success 'status: (continue first edit) second edit and amend' '
+ git reset --hard several_edits &&
+ FAKE_LINES="edit 1 edit 2 3" &&
+ export FAKE_LINES &&
+ test_when_finished "git rebase --abort" &&
+ COMMIT2=$(git rev-parse --short several_edits^^) &&
+ COMMIT3=$(git rev-parse --short several_edits^) &&
+ COMMIT4=$(git rev-parse --short several_edits) &&
+ ONTO=$(git rev-parse --short HEAD~3) &&
+ git rebase -i HEAD~3 &&
+ git rebase --continue &&
+ git commit --amend -m "foo" &&
+ cat >expected <<EOF &&
+interactive rebase in progress; onto $ONTO
+Last commands done (2 commands done):
+ edit $COMMIT2 two_edits
+ edit $COMMIT3 three_edits
+Next command to do (1 remaining command):
+ pick $COMMIT4 four_edits
+ (use "git rebase --edit-todo" to view and edit)
+You are currently editing a commit while rebasing branch '\''several_edits'\'' on '\''$ONTO'\''.
+ (use "git commit --amend" to amend the current commit)
+ (use "git rebase --continue" once you are satisfied with your changes)
+
+nothing to commit (use -u to show untracked files)
+EOF
+ git status --untracked-files=no >actual &&
+ test_cmp expected actual
+'
+
+
+test_expect_success 'status: (amend first edit) second edit' '
+ git reset --hard several_edits &&
+ FAKE_LINES="edit 1 edit 2 3" &&
+ export FAKE_LINES &&
+ test_when_finished "git rebase --abort" &&
+ COMMIT2=$(git rev-parse --short several_edits^^) &&
+ COMMIT3=$(git rev-parse --short several_edits^) &&
+ COMMIT4=$(git rev-parse --short several_edits) &&
+ ONTO=$(git rev-parse --short HEAD~3) &&
+ git rebase -i HEAD~3 &&
+ git commit --amend -m "a" &&
+ git rebase --continue &&
+ cat >expected <<EOF &&
+interactive rebase in progress; onto $ONTO
+Last commands done (2 commands done):
+ edit $COMMIT2 two_edits
+ edit $COMMIT3 three_edits
+Next command to do (1 remaining command):
+ pick $COMMIT4 four_edits
+ (use "git rebase --edit-todo" to view and edit)
+You are currently editing a commit while rebasing branch '\''several_edits'\'' on '\''$ONTO'\''.
+ (use "git commit --amend" to amend the current commit)
+ (use "git rebase --continue" once you are satisfied with your changes)
+
+nothing to commit (use -u to show untracked files)
+EOF
+ git status --untracked-files=no >actual &&
+ test_cmp expected actual
+'
+
+
+test_expect_success 'status: (amend first edit) second edit and split' '
+ git reset --hard several_edits &&
+ FAKE_LINES="edit 1 edit 2 3" &&
+ export FAKE_LINES &&
+ test_when_finished "git rebase --abort" &&
+ ONTO=$(git rev-parse --short HEAD~3) &&
+ COMMIT2=$(git rev-parse --short several_edits^^) &&
+ COMMIT3=$(git rev-parse --short several_edits^) &&
+ COMMIT4=$(git rev-parse --short several_edits) &&
+ git rebase -i HEAD~3 &&
+ git commit --amend -m "b" &&
+ git rebase --continue &&
+ git reset HEAD^ &&
+ cat >expected <<EOF &&
+interactive rebase in progress; onto $ONTO
+Last commands done (2 commands done):
+ edit $COMMIT2 two_edits
+ edit $COMMIT3 three_edits
+Next command to do (1 remaining command):
+ pick $COMMIT4 four_edits
+ (use "git rebase --edit-todo" to view and edit)
+You are currently splitting a commit while rebasing branch '\''several_edits'\'' on '\''$ONTO'\''.
+ (Once your working directory is clean, run "git rebase --continue")
+
+Changes not staged for commit:
+ (use "git add <file>..." to update what will be committed)
+ (use "git restore <file>..." to discard changes in working directory)
+ modified: main.txt
+
+no changes added to commit (use "git add" and/or "git commit -a")
+EOF
+ git status --untracked-files=no >actual &&
+ test_cmp expected actual
+'
+
+
+test_expect_success 'status: (amend first edit) second edit and amend' '
+ git reset --hard several_edits &&
+ FAKE_LINES="edit 1 edit 2 3" &&
+ export FAKE_LINES &&
+ test_when_finished "git rebase --abort" &&
+ COMMIT2=$(git rev-parse --short several_edits^^) &&
+ COMMIT3=$(git rev-parse --short several_edits^) &&
+ COMMIT4=$(git rev-parse --short several_edits) &&
+ ONTO=$(git rev-parse --short HEAD~3) &&
+ git rebase -i HEAD~3 &&
+ git commit --amend -m "c" &&
+ git rebase --continue &&
+ git commit --amend -m "d" &&
+ cat >expected <<EOF &&
+interactive rebase in progress; onto $ONTO
+Last commands done (2 commands done):
+ edit $COMMIT2 two_edits
+ edit $COMMIT3 three_edits
+Next command to do (1 remaining command):
+ pick $COMMIT4 four_edits
+ (use "git rebase --edit-todo" to view and edit)
+You are currently editing a commit while rebasing branch '\''several_edits'\'' on '\''$ONTO'\''.
+ (use "git commit --amend" to amend the current commit)
+ (use "git rebase --continue" once you are satisfied with your changes)
+
+nothing to commit (use -u to show untracked files)
+EOF
+ git status --untracked-files=no >actual &&
+ test_cmp expected actual
+'
+
+
+test_expect_success 'status: (split first edit) second edit' '
+ git reset --hard several_edits &&
+ FAKE_LINES="edit 1 edit 2 3" &&
+ export FAKE_LINES &&
+ test_when_finished "git rebase --abort" &&
+ COMMIT2=$(git rev-parse --short several_edits^^) &&
+ COMMIT3=$(git rev-parse --short several_edits^) &&
+ COMMIT4=$(git rev-parse --short several_edits) &&
+ ONTO=$(git rev-parse --short HEAD~3) &&
+ git rebase -i HEAD~3 &&
+ git reset HEAD^ &&
+ git add main.txt &&
+ git commit -m "e" &&
+ git rebase --continue &&
+ cat >expected <<EOF &&
+interactive rebase in progress; onto $ONTO
+Last commands done (2 commands done):
+ edit $COMMIT2 two_edits
+ edit $COMMIT3 three_edits
+Next command to do (1 remaining command):
+ pick $COMMIT4 four_edits
+ (use "git rebase --edit-todo" to view and edit)
+You are currently editing a commit while rebasing branch '\''several_edits'\'' on '\''$ONTO'\''.
+ (use "git commit --amend" to amend the current commit)
+ (use "git rebase --continue" once you are satisfied with your changes)
+
+nothing to commit (use -u to show untracked files)
+EOF
+ git status --untracked-files=no >actual &&
+ test_cmp expected actual
+'
+
+
+test_expect_success 'status: (split first edit) second edit and split' '
+ git reset --hard several_edits &&
+ FAKE_LINES="edit 1 edit 2 3" &&
+ export FAKE_LINES &&
+ test_when_finished "git rebase --abort" &&
+ COMMIT2=$(git rev-parse --short several_edits^^) &&
+ COMMIT3=$(git rev-parse --short several_edits^) &&
+ COMMIT4=$(git rev-parse --short several_edits) &&
+ ONTO=$(git rev-parse --short HEAD~3) &&
+ git rebase -i HEAD~3 &&
+ git reset HEAD^ &&
+ git add main.txt &&
+ git commit --amend -m "f" &&
+ git rebase --continue &&
+ git reset HEAD^ &&
+ cat >expected <<EOF &&
+interactive rebase in progress; onto $ONTO
+Last commands done (2 commands done):
+ edit $COMMIT2 two_edits
+ edit $COMMIT3 three_edits
+Next command to do (1 remaining command):
+ pick $COMMIT4 four_edits
+ (use "git rebase --edit-todo" to view and edit)
+You are currently splitting a commit while rebasing branch '\''several_edits'\'' on '\''$ONTO'\''.
+ (Once your working directory is clean, run "git rebase --continue")
+
+Changes not staged for commit:
+ (use "git add <file>..." to update what will be committed)
+ (use "git restore <file>..." to discard changes in working directory)
+ modified: main.txt
+
+no changes added to commit (use "git add" and/or "git commit -a")
+EOF
+ git status --untracked-files=no >actual &&
+ test_cmp expected actual
+'
+
+
+test_expect_success 'status: (split first edit) second edit and amend' '
+ git reset --hard several_edits &&
+ FAKE_LINES="edit 1 edit 2 3" &&
+ export FAKE_LINES &&
+ test_when_finished "git rebase --abort" &&
+ COMMIT2=$(git rev-parse --short several_edits^^) &&
+ COMMIT3=$(git rev-parse --short several_edits^) &&
+ COMMIT4=$(git rev-parse --short several_edits) &&
+ ONTO=$(git rev-parse --short HEAD~3) &&
+ git rebase -i HEAD~3 &&
+ git reset HEAD^ &&
+ git add main.txt &&
+ git commit --amend -m "g" &&
+ git rebase --continue &&
+ git commit --amend -m "h" &&
+ cat >expected <<EOF &&
+interactive rebase in progress; onto $ONTO
+Last commands done (2 commands done):
+ edit $COMMIT2 two_edits
+ edit $COMMIT3 three_edits
+Next command to do (1 remaining command):
+ pick $COMMIT4 four_edits
+ (use "git rebase --edit-todo" to view and edit)
+You are currently editing a commit while rebasing branch '\''several_edits'\'' on '\''$ONTO'\''.
+ (use "git commit --amend" to amend the current commit)
+ (use "git rebase --continue" once you are satisfied with your changes)
+
+nothing to commit (use -u to show untracked files)
+EOF
+ git status --untracked-files=no >actual &&
+ test_cmp expected actual
+'
+
+
+test_expect_success 'prepare am_session' '
+ git reset --hard main &&
+ git checkout -b am_session &&
+ test_commit one_am one.txt "one" &&
+ test_commit two_am two.txt "two" &&
+ test_commit three_am three.txt "three"
+'
+
+
+test_expect_success 'status in an am session: file already exists' '
+ git checkout -b am_already_exists &&
+ test_when_finished "rm Maildir/* && git am --abort" &&
+ git format-patch -1 -oMaildir &&
+ test_must_fail git am Maildir/*.patch &&
+ cat >expected <<\EOF &&
+On branch am_already_exists
+You are in the middle of an am session.
+ (fix conflicts and then run "git am --continue")
+ (use "git am --skip" to skip this patch)
+ (use "git am --abort" to restore the original branch)
+
+nothing to commit (use -u to show untracked files)
+EOF
+ git status --untracked-files=no >actual &&
+ test_cmp expected actual
+'
+
+
+test_expect_success 'status in an am session: file does not exist' '
+ git reset --hard am_session &&
+ git checkout -b am_not_exists &&
+ git rm three.txt &&
+ git commit -m "delete three.txt" &&
+ test_when_finished "rm Maildir/* && git am --abort" &&
+ git format-patch -1 -oMaildir &&
+ test_must_fail git am Maildir/*.patch &&
+ cat >expected <<\EOF &&
+On branch am_not_exists
+You are in the middle of an am session.
+ (fix conflicts and then run "git am --continue")
+ (use "git am --skip" to skip this patch)
+ (use "git am --abort" to restore the original branch)
+
+nothing to commit (use -u to show untracked files)
+EOF
+ git status --untracked-files=no >actual &&
+ test_cmp expected actual
+'
+
+
+test_expect_success 'status in an am session: empty patch' '
+ git reset --hard am_session &&
+ git checkout -b am_empty &&
+ test_when_finished "rm Maildir/* && git am --abort" &&
+ git format-patch -3 -oMaildir &&
+ git rm one.txt two.txt three.txt &&
+ git commit -m "delete all am_empty" &&
+ echo error >Maildir/0002-two_am.patch &&
+ test_must_fail git am Maildir/*.patch &&
+ cat >expected <<\EOF &&
+On branch am_empty
+You are in the middle of an am session.
+The current patch is empty.
+ (use "git am --skip" to skip this patch)
+ (use "git am --allow-empty" to record this patch as an empty commit)
+ (use "git am --abort" to restore the original branch)
+
+nothing to commit (use -u to show untracked files)
+EOF
+ git status --untracked-files=no >actual &&
+ test_cmp expected actual
+'
+
+
+test_expect_success 'status when bisecting' '
+ git reset --hard main &&
+ git checkout -b bisect &&
+ test_commit one_bisect main.txt one &&
+ test_commit two_bisect main.txt two &&
+ test_commit three_bisect main.txt three &&
+ test_when_finished "git bisect reset" &&
+ git bisect start &&
+ git bisect bad &&
+ git bisect good one_bisect &&
+ TGT=$(git rev-parse --short two_bisect) &&
+ cat >expected <<EOF &&
+HEAD detached at $TGT
+You are currently bisecting, started from branch '\''bisect'\''.
+ (use "git bisect reset" to get back to the original branch)
+
+nothing to commit (use -u to show untracked files)
+EOF
+ git status --untracked-files=no >actual &&
+ test_cmp expected actual
+'
+
+
+test_expect_success 'status when rebase --apply conflicts with statushints disabled' '
+ git reset --hard main &&
+ git checkout -b statushints_disabled &&
+ test_when_finished "git config --local advice.statushints true" &&
+ git config --local advice.statushints false &&
+ test_commit one_statushints main.txt one &&
+ test_commit two_statushints main.txt two &&
+ test_commit three_statushints main.txt three &&
+ test_when_finished "git rebase --abort" &&
+ ONTO=$(git rev-parse --short HEAD^^) &&
+ test_must_fail git rebase --apply HEAD^ --onto HEAD^^ &&
+ cat >expected <<EOF &&
+rebase in progress; onto $ONTO
+You are currently rebasing branch '\''statushints_disabled'\'' on '\''$ONTO'\''.
+
+Unmerged paths:
+ both modified: main.txt
+
+no changes added to commit
+EOF
+ git status --untracked-files=no >actual &&
+ test_cmp expected actual
+'
+
+
+test_expect_success 'prepare for cherry-pick conflicts' '
+ git reset --hard main &&
+ git checkout -b cherry_branch &&
+ test_commit one_cherry main.txt one &&
+ test_commit two_cherries main.txt two &&
+ git checkout -b cherry_branch_second &&
+ test_commit second_cherry main.txt second &&
+ git checkout cherry_branch &&
+ test_commit three_cherries main.txt three
+'
+
+
+test_expect_success 'status when cherry-picking before resolving conflicts' '
+ test_when_finished "git cherry-pick --abort" &&
+ test_must_fail git cherry-pick cherry_branch_second &&
+ TO_CHERRY_PICK=$(git rev-parse --short CHERRY_PICK_HEAD) &&
+ cat >expected <<EOF &&
+On branch cherry_branch
+You are currently cherry-picking commit $TO_CHERRY_PICK.
+ (fix conflicts and run "git cherry-pick --continue")
+ (use "git cherry-pick --skip" to skip this patch)
+ (use "git cherry-pick --abort" to cancel the cherry-pick operation)
+
+Unmerged paths:
+ (use "git add <file>..." to mark resolution)
+ both modified: main.txt
+
+no changes added to commit (use "git add" and/or "git commit -a")
+EOF
+ git status --untracked-files=no >actual &&
+ test_cmp expected actual
+'
+
+
+test_expect_success 'status when cherry-picking after resolving conflicts' '
+ git reset --hard cherry_branch &&
+ test_when_finished "git cherry-pick --abort" &&
+ test_must_fail git cherry-pick cherry_branch_second &&
+ TO_CHERRY_PICK=$(git rev-parse --short CHERRY_PICK_HEAD) &&
+ echo end >main.txt &&
+ git add main.txt &&
+ cat >expected <<EOF &&
+On branch cherry_branch
+You are currently cherry-picking commit $TO_CHERRY_PICK.
+ (all conflicts fixed: run "git cherry-pick --continue")
+ (use "git cherry-pick --skip" to skip this patch)
+ (use "git cherry-pick --abort" to cancel the cherry-pick operation)
+
+Changes to be committed:
+ modified: main.txt
+
+Untracked files not listed (use -u option to show untracked files)
+EOF
+ git status --untracked-files=no >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'status when cherry-picking multiple commits' '
+ git reset --hard cherry_branch &&
+ test_when_finished "git cherry-pick --abort" &&
+ test_must_fail git cherry-pick cherry_branch_second one_cherry &&
+ TO_CHERRY_PICK=$(git rev-parse --short CHERRY_PICK_HEAD) &&
+ cat >expected <<EOF &&
+On branch cherry_branch
+You are currently cherry-picking commit $TO_CHERRY_PICK.
+ (fix conflicts and run "git cherry-pick --continue")
+ (use "git cherry-pick --skip" to skip this patch)
+ (use "git cherry-pick --abort" to cancel the cherry-pick operation)
+
+Unmerged paths:
+ (use "git add <file>..." to mark resolution)
+ both modified: main.txt
+
+no changes added to commit (use "git add" and/or "git commit -a")
+EOF
+ git status --untracked-files=no >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'status when cherry-picking after committing conflict resolution' '
+ git reset --hard cherry_branch &&
+ test_when_finished "git cherry-pick --abort" &&
+ test_must_fail git cherry-pick cherry_branch_second one_cherry &&
+ echo end >main.txt &&
+ git commit -a &&
+ cat >expected <<EOF &&
+On branch cherry_branch
+Cherry-pick currently in progress.
+ (run "git cherry-pick --continue" to continue)
+ (use "git cherry-pick --skip" to skip this patch)
+ (use "git cherry-pick --abort" to cancel the cherry-pick operation)
+
+nothing to commit (use -u to show untracked files)
+EOF
+ git status --untracked-files=no >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'status shows cherry-pick with invalid oid' '
+ mkdir .git/sequencer &&
+ test_write_lines "pick invalid-oid" >.git/sequencer/todo &&
+ git status --untracked-files=no >actual 2>err &&
+ git cherry-pick --quit &&
+ test_must_be_empty err &&
+ test_cmp expected actual
+'
+
+test_expect_success 'status does not show error if .git/sequencer is a file' '
+ test_when_finished "rm .git/sequencer" &&
+ test_write_lines hello >.git/sequencer &&
+ git status --untracked-files=no 2>err &&
+ test_must_be_empty err
+'
+
+test_expect_success 'status showing detached at and from a tag' '
+ test_commit atag tagging &&
+ git checkout atag &&
+ cat >expected <<\EOF &&
+HEAD detached at atag
+nothing to commit (use -u to show untracked files)
+EOF
+ git status --untracked-files=no >actual &&
+ test_cmp expected actual &&
+
+ git reset --hard HEAD^ &&
+ cat >expected <<\EOF &&
+HEAD detached from atag
+nothing to commit (use -u to show untracked files)
+EOF
+ git status --untracked-files=no >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'status while reverting commit (conflicts)' '
+ git checkout main &&
+ echo before >to-revert.txt &&
+ test_commit before to-revert.txt &&
+ echo old >to-revert.txt &&
+ test_commit old to-revert.txt &&
+ echo new >to-revert.txt &&
+ test_commit new to-revert.txt &&
+ TO_REVERT=$(git rev-parse --short HEAD^) &&
+ test_must_fail git revert $TO_REVERT &&
+ cat >expected <<EOF &&
+On branch main
+You are currently reverting commit $TO_REVERT.
+ (fix conflicts and run "git revert --continue")
+ (use "git revert --skip" to skip this patch)
+ (use "git revert --abort" to cancel the revert operation)
+
+Unmerged paths:
+ (use "git restore --staged <file>..." to unstage)
+ (use "git add <file>..." to mark resolution)
+ both modified: to-revert.txt
+
+no changes added to commit (use "git add" and/or "git commit -a")
+EOF
+ git status --untracked-files=no >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'status while reverting commit (conflicts resolved)' '
+ echo reverted >to-revert.txt &&
+ git add to-revert.txt &&
+ cat >expected <<EOF &&
+On branch main
+You are currently reverting commit $TO_REVERT.
+ (all conflicts fixed: run "git revert --continue")
+ (use "git revert --skip" to skip this patch)
+ (use "git revert --abort" to cancel the revert operation)
+
+Changes to be committed:
+ (use "git restore --staged <file>..." to unstage)
+ modified: to-revert.txt
+
+Untracked files not listed (use -u option to show untracked files)
+EOF
+ git status --untracked-files=no >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'status after reverting commit' '
+ git revert --continue &&
+ cat >expected <<\EOF &&
+On branch main
+nothing to commit (use -u to show untracked files)
+EOF
+ git status --untracked-files=no >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'status while reverting after committing conflict resolution' '
+ test_when_finished "git revert --abort" &&
+ git reset --hard new &&
+ test_must_fail git revert old new &&
+ echo reverted >to-revert.txt &&
+ git commit -a &&
+ cat >expected <<EOF &&
+On branch main
+Revert currently in progress.
+ (run "git revert --continue" to continue)
+ (use "git revert --skip" to skip this patch)
+ (use "git revert --abort" to cancel the revert operation)
+
+nothing to commit (use -u to show untracked files)
+EOF
+ git status --untracked-files=no >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'prepare for different number of commits rebased' '
+ git reset --hard main &&
+ git checkout -b several_commits &&
+ test_commit one_commit main.txt one &&
+ test_commit two_commit main.txt two &&
+ test_commit three_commit main.txt three &&
+ test_commit four_commit main.txt four
+'
+
+test_expect_success 'status: one command done nothing remaining' '
+ FAKE_LINES="exec_exit_15" &&
+ export FAKE_LINES &&
+ test_when_finished "git rebase --abort" &&
+ ONTO=$(git rev-parse --short HEAD~3) &&
+ test_must_fail git rebase -i HEAD~3 &&
+ cat >expected <<EOF &&
+interactive rebase in progress; onto $ONTO
+Last command done (1 command done):
+ exec exit 15
+No commands remaining.
+You are currently editing a commit while rebasing branch '\''several_commits'\'' on '\''$ONTO'\''.
+ (use "git commit --amend" to amend the current commit)
+ (use "git rebase --continue" once you are satisfied with your changes)
+
+nothing to commit (use -u to show untracked files)
+EOF
+ git status --untracked-files=no >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'status: two commands done with some white lines in done file' '
+ FAKE_LINES="1 > exec_exit_15 2 3" &&
+ export FAKE_LINES &&
+ test_when_finished "git rebase --abort" &&
+ ONTO=$(git rev-parse --short HEAD~3) &&
+ COMMIT4=$(git rev-parse --short HEAD) &&
+ COMMIT3=$(git rev-parse --short HEAD^) &&
+ COMMIT2=$(git rev-parse --short HEAD^^) &&
+ test_must_fail git rebase -i HEAD~3 &&
+ cat >expected <<EOF &&
+interactive rebase in progress; onto $ONTO
+Last commands done (2 commands done):
+ pick $COMMIT2 two_commit
+ exec exit 15
+Next commands to do (2 remaining commands):
+ pick $COMMIT3 three_commit
+ pick $COMMIT4 four_commit
+ (use "git rebase --edit-todo" to view and edit)
+You are currently editing a commit while rebasing branch '\''several_commits'\'' on '\''$ONTO'\''.
+ (use "git commit --amend" to amend the current commit)
+ (use "git rebase --continue" once you are satisfied with your changes)
+
+nothing to commit (use -u to show untracked files)
+EOF
+ git status --untracked-files=no >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'status: two remaining commands with some white lines in todo file' '
+ FAKE_LINES="1 2 exec_exit_15 3 > 4" &&
+ export FAKE_LINES &&
+ test_when_finished "git rebase --abort" &&
+ ONTO=$(git rev-parse --short HEAD~4) &&
+ COMMIT4=$(git rev-parse --short HEAD) &&
+ COMMIT3=$(git rev-parse --short HEAD^) &&
+ COMMIT2=$(git rev-parse --short HEAD^^) &&
+ test_must_fail git rebase -i HEAD~4 &&
+ cat >expected <<EOF &&
+interactive rebase in progress; onto $ONTO
+Last commands done (3 commands done):
+ pick $COMMIT2 two_commit
+ exec exit 15
+ (see more in file .git/rebase-merge/done)
+Next commands to do (2 remaining commands):
+ pick $COMMIT3 three_commit
+ pick $COMMIT4 four_commit
+ (use "git rebase --edit-todo" to view and edit)
+You are currently editing a commit while rebasing branch '\''several_commits'\'' on '\''$ONTO'\''.
+ (use "git commit --amend" to amend the current commit)
+ (use "git rebase --continue" once you are satisfied with your changes)
+
+nothing to commit (use -u to show untracked files)
+EOF
+ git status --untracked-files=no >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'status: handle not-yet-started rebase -i gracefully' '
+ ONTO=$(git rev-parse --short HEAD^) &&
+ COMMIT=$(git rev-parse --short HEAD) &&
+ EDITOR="git status --untracked-files=no >actual" git rebase -i HEAD^ &&
+ cat >expected <<EOF &&
+On branch several_commits
+No commands done.
+Next command to do (1 remaining command):
+ pick $COMMIT four_commit
+ (use "git rebase --edit-todo" to view and edit)
+You are currently editing a commit while rebasing branch '\''several_commits'\'' on '\''$ONTO'\''.
+ (use "git commit --amend" to amend the current commit)
+ (use "git rebase --continue" once you are satisfied with your changes)
+
+nothing to commit (use -u to show untracked files)
+EOF
+ test_cmp expected actual
+'
+
+test_done
diff --git a/t/t7513-interpret-trailers.sh b/t/t7513-interpret-trailers.sh
new file mode 100755
index 0000000..832aff0
--- /dev/null
+++ b/t/t7513-interpret-trailers.sh
@@ -0,0 +1,1919 @@
+#!/bin/sh
+#
+# Copyright (c) 2013, 2014 Christian Couder
+#
+
+test_description='git interpret-trailers'
+
+. ./test-lib.sh
+
+# When we want one trailing space at the end of each line, let's use sed
+# to make sure that these spaces are not removed by any automatic tool.
+
+test_expect_success 'setup' '
+ : >empty &&
+ cat >basic_message <<-\EOF &&
+ subject
+
+ body
+ EOF
+ cat >complex_message_body <<-\EOF &&
+ my subject
+
+ my body which is long
+ and contains some special
+ chars like : = ? !
+
+ EOF
+ sed -e "s/ Z\$/ /" >complex_message_trailers <<-\EOF &&
+ Fixes: Z
+ Acked-by: Z
+ Reviewed-by: Z
+ Signed-off-by: Z
+ EOF
+ cat >basic_patch <<-\EOF
+ ---
+ foo.txt | 2 +-
+ 1 file changed, 1 insertion(+), 1 deletion(-)
+
+ diff --git a/foo.txt b/foo.txt
+ index 0353767..1d91aa1 100644
+ --- a/foo.txt
+ +++ b/foo.txt
+ @@ -1,3 +1,3 @@
+
+ -bar
+ +baz
+
+ --
+ 1.9.rc0.11.ga562ddc
+
+ EOF
+'
+
+test_expect_success 'with cmd' '
+ test_when_finished "git config --remove-section trailer.bug" &&
+ git config trailer.bug.key "Bug-maker: " &&
+ git config trailer.bug.ifExists "add" &&
+ git config trailer.bug.cmd "echo \"maybe is\"" &&
+ cat >expected2 <<-EOF &&
+
+ Bug-maker: maybe is him
+ Bug-maker: maybe is me
+ EOF
+ git interpret-trailers --trailer "bug: him" --trailer "bug:me" \
+ >actual2 &&
+ test_cmp expected2 actual2
+'
+
+test_expect_success 'with cmd and $1' '
+ test_when_finished "git config --remove-section trailer.bug" &&
+ git config trailer.bug.key "Bug-maker: " &&
+ git config trailer.bug.ifExists "add" &&
+ git config trailer.bug.cmd "echo \"\$1\" is" &&
+ cat >expected2 <<-EOF &&
+
+ Bug-maker: him is him
+ Bug-maker: me is me
+ EOF
+ git interpret-trailers --trailer "bug: him" --trailer "bug:me" \
+ >actual2 &&
+ test_cmp expected2 actual2
+'
+
+test_expect_success 'with cmd and $1 with sh -c' '
+ test_when_finished "git config --remove-section trailer.bug" &&
+ git config trailer.bug.key "Bug-maker: " &&
+ git config trailer.bug.ifExists "replace" &&
+ git config trailer.bug.cmd "sh -c \"echo who is \"\$1\"\"" &&
+ cat >expected2 <<-EOF &&
+
+ Bug-maker: who is me
+ EOF
+ git interpret-trailers --trailer "bug: him" --trailer "bug:me" \
+ >actual2 &&
+ test_cmp expected2 actual2
+'
+
+test_expect_success 'with cmd and $1 with shell script' '
+ test_when_finished "git config --remove-section trailer.bug" &&
+ git config trailer.bug.key "Bug-maker: " &&
+ git config trailer.bug.ifExists "replace" &&
+ git config trailer.bug.cmd "./echoscript" &&
+ cat >expected2 <<-EOF &&
+
+ Bug-maker: who is me
+ EOF
+ cat >echoscript <<-EOF &&
+ #!/bin/sh
+ echo who is "\$1"
+ EOF
+ chmod +x echoscript &&
+ git interpret-trailers --trailer "bug: him" --trailer "bug:me" \
+ >actual2 &&
+ test_cmp expected2 actual2
+'
+
+test_expect_success 'without config' '
+ sed -e "s/ Z\$/ /" >expected <<-\EOF &&
+
+ ack: Peff
+ Reviewed-by: Z
+ Acked-by: Johan
+ EOF
+ git interpret-trailers --trailer "ack = Peff" --trailer "Reviewed-by" \
+ --trailer "Acked-by: Johan" empty >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'without config in another order' '
+ sed -e "s/ Z\$/ /" >expected <<-\EOF &&
+
+ Acked-by: Johan
+ Reviewed-by: Z
+ ack: Peff
+ EOF
+ git interpret-trailers --trailer "Acked-by: Johan" --trailer "Reviewed-by" \
+ --trailer "ack = Peff" empty >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success '--trim-empty without config' '
+ cat >expected <<-\EOF &&
+
+ ack: Peff
+ Acked-by: Johan
+ EOF
+ git interpret-trailers --trim-empty --trailer ack=Peff \
+ --trailer "Reviewed-by" --trailer "Acked-by: Johan" \
+ --trailer "sob:" empty >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'with config option on the command line' '
+ cat >expected <<-\EOF &&
+
+ Acked-by: Johan
+ Reviewed-by: Peff
+ EOF
+ { echo && echo "Acked-by: Johan"; } |
+ git -c "trailer.Acked-by.ifexists=addifdifferent" interpret-trailers \
+ --trailer "Reviewed-by: Peff" --trailer "Acked-by: Johan" >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'with only a title in the message' '
+ cat >expected <<-\EOF &&
+ area: change
+
+ Reviewed-by: Peff
+ Acked-by: Johan
+ EOF
+ echo "area: change" |
+ git interpret-trailers --trailer "Reviewed-by: Peff" \
+ --trailer "Acked-by: Johan" >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'with multiline title in the message' '
+ cat >expected <<-\EOF &&
+ place of
+ code: change
+
+ Reviewed-by: Peff
+ Acked-by: Johan
+ EOF
+ printf "%s\n" "place of" "code: change" |
+ git interpret-trailers --trailer "Reviewed-by: Peff" \
+ --trailer "Acked-by: Johan" >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'with non-trailer lines mixed with Signed-off-by' '
+ cat >patch <<-\EOF &&
+
+ this is not a trailer
+ this is not a trailer
+ Signed-off-by: a <a@example.com>
+ this is not a trailer
+ EOF
+ cat >expected <<-\EOF &&
+
+ this is not a trailer
+ this is not a trailer
+ Signed-off-by: a <a@example.com>
+ this is not a trailer
+ token: value
+ EOF
+ git interpret-trailers --trailer "token: value" patch >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'with non-trailer lines mixed with cherry picked from' '
+ cat >patch <<-\EOF &&
+
+ this is not a trailer
+ this is not a trailer
+ (cherry picked from commit x)
+ this is not a trailer
+ EOF
+ cat >expected <<-\EOF &&
+
+ this is not a trailer
+ this is not a trailer
+ (cherry picked from commit x)
+ this is not a trailer
+ token: value
+ EOF
+ git interpret-trailers --trailer "token: value" patch >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'with non-trailer lines mixed with a configured trailer' '
+ cat >patch <<-\EOF &&
+
+ this is not a trailer
+ this is not a trailer
+ My-trailer: x
+ this is not a trailer
+ EOF
+ cat >expected <<-\EOF &&
+
+ this is not a trailer
+ this is not a trailer
+ My-trailer: x
+ this is not a trailer
+ token: value
+ EOF
+ test_config trailer.my.key "My-trailer: " &&
+ git interpret-trailers --trailer "token: value" patch >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'with non-trailer lines mixed with a non-configured trailer' '
+ cat >patch <<-\EOF &&
+
+ this is not a trailer
+ this is not a trailer
+ I-am-not-configured: x
+ this is not a trailer
+ EOF
+ cat >expected <<-\EOF &&
+
+ this is not a trailer
+ this is not a trailer
+ I-am-not-configured: x
+ this is not a trailer
+
+ token: value
+ EOF
+ test_config trailer.my.key "My-trailer: " &&
+ git interpret-trailers --trailer "token: value" patch >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'with all non-configured trailers' '
+ cat >patch <<-\EOF &&
+
+ I-am-not-configured: x
+ I-am-also-not-configured: x
+ EOF
+ cat >expected <<-\EOF &&
+
+ I-am-not-configured: x
+ I-am-also-not-configured: x
+ token: value
+ EOF
+ test_config trailer.my.key "My-trailer: " &&
+ git interpret-trailers --trailer "token: value" patch >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'with non-trailer lines only' '
+ cat >patch <<-\EOF &&
+
+ this is not a trailer
+ EOF
+ cat >expected <<-\EOF &&
+
+ this is not a trailer
+
+ token: value
+ EOF
+ git interpret-trailers --trailer "token: value" patch >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'line with leading whitespace is not trailer' '
+ q_to_tab >patch <<-\EOF &&
+
+ Qtoken: value
+ EOF
+ q_to_tab >expected <<-\EOF &&
+
+ Qtoken: value
+
+ token: value
+ EOF
+ git interpret-trailers --trailer "token: value" patch >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'multiline field treated as one trailer for 25% check' '
+ q_to_tab >patch <<-\EOF &&
+
+ Signed-off-by: a <a@example.com>
+ name: value on
+ Qmultiple lines
+ this is not a trailer
+ this is not a trailer
+ this is not a trailer
+ this is not a trailer
+ this is not a trailer
+ this is not a trailer
+ EOF
+ q_to_tab >expected <<-\EOF &&
+
+ Signed-off-by: a <a@example.com>
+ name: value on
+ Qmultiple lines
+ this is not a trailer
+ this is not a trailer
+ this is not a trailer
+ this is not a trailer
+ this is not a trailer
+ this is not a trailer
+ name: value
+ EOF
+ git interpret-trailers --trailer "name: value" patch >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'multiline field treated as atomic for placement' '
+ q_to_tab >patch <<-\EOF &&
+
+ another: trailer
+ name: value on
+ Qmultiple lines
+ another: trailer
+ EOF
+ q_to_tab >expected <<-\EOF &&
+
+ another: trailer
+ name: value on
+ Qmultiple lines
+ name: value
+ another: trailer
+ EOF
+ test_config trailer.name.where after &&
+ git interpret-trailers --trailer "name: value" patch >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'multiline field treated as atomic for replacement' '
+ q_to_tab >patch <<-\EOF &&
+
+ another: trailer
+ name: value on
+ Qmultiple lines
+ another: trailer
+ EOF
+ q_to_tab >expected <<-\EOF &&
+
+ another: trailer
+ another: trailer
+ name: value
+ EOF
+ test_config trailer.name.ifexists replace &&
+ git interpret-trailers --trailer "name: value" patch >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'multiline field treated as atomic for difference check' '
+ q_to_tab >patch <<-\EOF &&
+
+ another: trailer
+ name: first line
+ Qsecond line
+ another: trailer
+ EOF
+ test_config trailer.name.ifexists addIfDifferent &&
+
+ q_to_tab >trailer <<-\EOF &&
+ name: first line
+ Qsecond line
+ EOF
+ q_to_tab >expected <<-\EOF &&
+
+ another: trailer
+ name: first line
+ Qsecond line
+ another: trailer
+ EOF
+ git interpret-trailers --trailer "$(cat trailer)" patch >actual &&
+ test_cmp expected actual &&
+
+ q_to_tab >trailer <<-\EOF &&
+ name: first line
+ QQQQQsecond line
+ EOF
+ q_to_tab >expected <<-\EOF &&
+
+ another: trailer
+ name: first line
+ Qsecond line
+ another: trailer
+ name: first line
+ QQQQQsecond line
+ EOF
+ git interpret-trailers --trailer "$(cat trailer)" patch >actual &&
+ test_cmp expected actual &&
+
+ q_to_tab >trailer <<-\EOF &&
+ name: first line *DIFFERENT*
+ Qsecond line
+ EOF
+ q_to_tab >expected <<-\EOF &&
+
+ another: trailer
+ name: first line
+ Qsecond line
+ another: trailer
+ name: first line *DIFFERENT*
+ Qsecond line
+ EOF
+ git interpret-trailers --trailer "$(cat trailer)" patch >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'multiline field treated as atomic for neighbor check' '
+ q_to_tab >patch <<-\EOF &&
+
+ another: trailer
+ name: first line
+ Qsecond line
+ another: trailer
+ EOF
+ test_config trailer.name.where after &&
+ test_config trailer.name.ifexists addIfDifferentNeighbor &&
+
+ q_to_tab >trailer <<-\EOF &&
+ name: first line
+ Qsecond line
+ EOF
+ q_to_tab >expected <<-\EOF &&
+
+ another: trailer
+ name: first line
+ Qsecond line
+ another: trailer
+ EOF
+ git interpret-trailers --trailer "$(cat trailer)" patch >actual &&
+ test_cmp expected actual &&
+
+ q_to_tab >trailer <<-\EOF &&
+ name: first line
+ QQQQQsecond line
+ EOF
+ q_to_tab >expected <<-\EOF &&
+
+ another: trailer
+ name: first line
+ Qsecond line
+ name: first line
+ QQQQQsecond line
+ another: trailer
+ EOF
+ git interpret-trailers --trailer "$(cat trailer)" patch >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'with config setup' '
+ test_config trailer.ack.key "Acked-by: " &&
+ cat >expected <<-\EOF &&
+
+ Acked-by: Peff
+ EOF
+ git interpret-trailers --trim-empty --trailer "ack = Peff" empty >actual &&
+ test_cmp expected actual &&
+ git interpret-trailers --trim-empty --trailer "Acked-by = Peff" empty >actual &&
+ test_cmp expected actual &&
+ git interpret-trailers --trim-empty --trailer "Acked-by :Peff" empty >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'with config setup and ":=" as separators' '
+ test_config trailer.separators ":=" &&
+ test_config trailer.ack.key "Acked-by= " &&
+ cat >expected <<-\EOF &&
+
+ Acked-by= Peff
+ EOF
+ git interpret-trailers --trim-empty --trailer "ack = Peff" empty >actual &&
+ test_cmp expected actual &&
+ git interpret-trailers --trim-empty --trailer "Acked-by= Peff" empty >actual &&
+ test_cmp expected actual &&
+ git interpret-trailers --trim-empty --trailer "Acked-by : Peff" empty >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'with config setup and "%" as separators' '
+ test_config trailer.separators "%" &&
+ cat >expected <<-\EOF &&
+
+ bug% 42
+ count% 10
+ bug% 422
+ EOF
+ git interpret-trailers --trim-empty --trailer "bug = 42" \
+ --trailer count%10 --trailer "test: stuff" \
+ --trailer "bug % 422" empty >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'with "%" as separators and a message with trailers' '
+ test_config trailer.separators "%" &&
+ cat >special_message <<-\EOF &&
+ Special Message
+
+ bug% 42
+ count% 10
+ bug% 422
+ EOF
+ cat >expected <<-\EOF &&
+ Special Message
+
+ bug% 42
+ count% 10
+ bug% 422
+ count% 100
+ EOF
+ git interpret-trailers --trailer count%100 \
+ special_message >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'with config setup and ":=#" as separators' '
+ test_config trailer.separators ":=#" &&
+ test_config trailer.bug.key "Bug #" &&
+ cat >expected <<-\EOF &&
+
+ Bug #42
+ EOF
+ git interpret-trailers --trim-empty --trailer "bug = 42" empty >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'with commit basic message' '
+ cat basic_message >expected &&
+ echo >>expected &&
+ git interpret-trailers <basic_message >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'with basic patch' '
+ cat basic_message >input &&
+ cat basic_patch >>input &&
+ cat basic_message >expected &&
+ echo >>expected &&
+ cat basic_patch >>expected &&
+ git interpret-trailers <input >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'with commit complex message as argument' '
+ test_config trailer.separators ":=" &&
+ test_config trailer.ack.key "Acked-by= " &&
+ cat complex_message_body complex_message_trailers >complex_message &&
+ cat complex_message_body >expected &&
+ sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+ Fixes: Z
+ Acked-by= Z
+ Reviewed-by: Z
+ Signed-off-by: Z
+ EOF
+ git interpret-trailers complex_message >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'with 2 files arguments' '
+ test_config trailer.separators ":=" &&
+ test_config trailer.ack.key "Acked-by= " &&
+ cat basic_message >>expected &&
+ echo >>expected &&
+ cat basic_patch >>expected &&
+ git interpret-trailers complex_message input >actual &&
+ test_cmp expected actual
+'
+
+# Cover multiple comment characters with the same test input.
+for char in "#" ";"
+do
+ case "$char" in
+ "#")
+ # This is the default, so let's explicitly _not_
+ # set any config to make sure it behaves as we expect.
+ ;;
+ *)
+ config="-c core.commentChar=$char"
+ ;;
+ esac
+
+ test_expect_success "with message that has comments ($char)" '
+ cat basic_message >message_with_comments &&
+ sed -e "s/ Z\$/ /" \
+ -e "s/#/$char/g" >>message_with_comments <<-EOF &&
+ # comment
+
+ # other comment
+ Cc: Z
+ # yet another comment
+ Reviewed-by: Johan
+ Reviewed-by: Z
+ # last comment
+
+ EOF
+ cat basic_patch >>message_with_comments &&
+ cat basic_message >expected &&
+ sed -e "s/#/$char/g" >>expected <<-\EOF &&
+ # comment
+
+ Reviewed-by: Johan
+ Cc: Peff
+ # last comment
+
+ EOF
+ cat basic_patch >>expected &&
+ git $config interpret-trailers \
+ --trim-empty --trailer "Cc: Peff" \
+ message_with_comments >actual &&
+ test_cmp expected actual
+ '
+done
+
+test_expect_success 'with message that has an old style conflict block' '
+ cat basic_message >message_with_comments &&
+ sed -e "s/ Z\$/ /" >>message_with_comments <<-\EOF &&
+ # comment
+
+ # other comment
+ Cc: Z
+ # yet another comment
+ Reviewed-by: Johan
+ Reviewed-by: Z
+ # last comment
+
+ Conflicts:
+
+ EOF
+ cat basic_message >expected &&
+ cat >>expected <<-\EOF &&
+ # comment
+
+ Reviewed-by: Johan
+ Cc: Peff
+ # last comment
+
+ Conflicts:
+
+ EOF
+ git interpret-trailers --trim-empty --trailer "Cc: Peff" message_with_comments >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'with commit complex message and trailer args' '
+ test_config trailer.separators ":=#" &&
+ test_config trailer.ack.key "Acked-by= " &&
+ test_config trailer.bug.key "Bug #" &&
+ cat complex_message_body >expected &&
+ sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+ Fixes: Z
+ Acked-by= Z
+ Reviewed-by: Z
+ Signed-off-by: Z
+ Acked-by= Peff
+ Bug #42
+ EOF
+ git interpret-trailers --trailer "ack: Peff" \
+ --trailer "bug: 42" <complex_message >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'with complex patch, args and --trim-empty' '
+ test_config trailer.separators ":=#" &&
+ test_config trailer.ack.key "Acked-by= " &&
+ test_config trailer.bug.key "Bug #" &&
+ cat complex_message >complex_patch &&
+ cat basic_patch >>complex_patch &&
+ cat complex_message_body >expected &&
+ cat >>expected <<-\EOF &&
+ Acked-by= Peff
+ Bug #42
+ EOF
+ cat basic_patch >>expected &&
+ git interpret-trailers --trim-empty --trailer "ack: Peff" \
+ --trailer "bug: 42" <complex_patch >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'in-place editing with basic patch' '
+ cat basic_message >message &&
+ cat basic_patch >>message &&
+ cat basic_message >expected &&
+ echo >>expected &&
+ cat basic_patch >>expected &&
+ git interpret-trailers --in-place message &&
+ test_cmp expected message
+'
+
+test_expect_success 'in-place editing with additional trailer' '
+ cat basic_message >message &&
+ cat basic_patch >>message &&
+ cat basic_message >expected &&
+ echo >>expected &&
+ cat >>expected <<-\EOF &&
+ Reviewed-by: Alice
+ EOF
+ cat basic_patch >>expected &&
+ git interpret-trailers --trailer "Reviewed-by: Alice" --in-place message &&
+ test_cmp expected message
+'
+
+test_expect_success 'in-place editing on stdin disallowed' '
+ test_must_fail git interpret-trailers --trailer "Reviewed-by: Alice" --in-place < basic_message
+'
+
+test_expect_success 'in-place editing on non-existing file' '
+ test_must_fail git interpret-trailers --trailer "Reviewed-by: Alice" --in-place nonexisting &&
+ test_path_is_missing nonexisting
+'
+
+test_expect_success POSIXPERM,SANITY "in-place editing doesn't clobber original file on error" '
+ cat basic_message >message &&
+ chmod -r message &&
+ test_must_fail git interpret-trailers --trailer "Reviewed-by: Alice" --in-place message &&
+ chmod +r message &&
+ test_cmp message basic_message
+'
+
+test_expect_success 'using "where = before"' '
+ test_config trailer.separators ":=#" &&
+ test_config trailer.ack.key "Acked-by= " &&
+ test_config trailer.bug.key "Bug #" &&
+ test_config trailer.bug.where "before" &&
+ cat complex_message_body >expected &&
+ sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+ Bug #42
+ Fixes: Z
+ Acked-by= Z
+ Reviewed-by: Z
+ Signed-off-by: Z
+ Acked-by= Peff
+ EOF
+ git interpret-trailers --trailer "ack: Peff" \
+ --trailer "bug: 42" complex_message >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'overriding configuration with "--where after"' '
+ test_config trailer.separators ":=" &&
+ test_config trailer.ack.key "Acked-by= " &&
+ test_config trailer.ack.where "before" &&
+ cat complex_message_body >expected &&
+ sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+ Fixes: Z
+ Acked-by= Z
+ Acked-by= Peff
+ Reviewed-by: Z
+ Signed-off-by: Z
+ EOF
+ git interpret-trailers --where after --trailer "ack: Peff" \
+ complex_message >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'using "--where after" with "--no-where"' '
+ test_config trailer.ack.key "Acked-by= " &&
+ test_config trailer.ack.where "before" &&
+ test_config trailer.bug.key "Bug #" &&
+ test_config trailer.bug.where "before" &&
+ test_config trailer.separators ":=#" &&
+ cat complex_message_body >expected &&
+ sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+ Bug #42
+ Fixes: Z
+ Acked-by= Peff
+ Acked-by= Z
+ Reviewed-by: Z
+ Signed-off-by: Z
+ EOF
+ git interpret-trailers --where after --no-where --trailer "ack: Peff" \
+ --trailer "bug: 42" complex_message >actual &&
+ test_cmp expected actual
+'
+
+# Check whether using "--no-where" clears out only the "--where after", such
+# that we still use the configuration in trailer.where (which is different from
+# the hardcoded default (in WHERE_END) assuming the absence of .gitconfig).
+# Here, the "start" setting of trailer.where is respected, so the new "Acked-by"
+# and "Bug" trailers are placed at the beginning, and not at the end which is
+# the harcoded default.
+test_expect_success 'using "--where after" with "--no-where" defaults to configuration' '
+ test_config trailer.ack.key "Acked-by= " &&
+ test_config trailer.bug.key "Bug #" &&
+ test_config trailer.separators ":=#" &&
+ test_config trailer.where "start" &&
+ cat complex_message_body >expected &&
+ sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+ Bug #42
+ Acked-by= Peff
+ Fixes: Z
+ Acked-by= Z
+ Reviewed-by: Z
+ Signed-off-by: Z
+ EOF
+ git interpret-trailers --where after --no-where --trailer "ack: Peff" \
+ --trailer "bug: 42" complex_message >actual &&
+ test_cmp expected actual
+'
+
+# The "--where after" will only get respected for the trailer that came
+# immediately after it. For the next trailer (Bug #42), we default to using the
+# hardcoded WHERE_END because we don't have any "trailer.where" or
+# "trailer.bug.where" configured.
+test_expect_success 'using "--no-where" defaults to harcoded default if nothing configured' '
+ test_config trailer.ack.key "Acked-by= " &&
+ test_config trailer.bug.key "Bug #" &&
+ test_config trailer.separators ":=#" &&
+ cat complex_message_body >expected &&
+ sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+ Fixes: Z
+ Acked-by= Z
+ Acked-by= Peff
+ Reviewed-by: Z
+ Signed-off-by: Z
+ Bug #42
+ EOF
+ git interpret-trailers --where after --trailer "ack: Peff" --no-where \
+ --trailer "bug: 42" complex_message >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'using "where = after"' '
+ test_config trailer.ack.key "Acked-by= " &&
+ test_config trailer.ack.where "after" &&
+ test_config trailer.bug.key "Bug #" &&
+ test_config trailer.bug.where "before" &&
+ test_config trailer.separators ":=#" &&
+ cat complex_message_body >expected &&
+ sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+ Bug #42
+ Fixes: Z
+ Acked-by= Z
+ Acked-by= Peff
+ Reviewed-by: Z
+ Signed-off-by: Z
+ EOF
+ git interpret-trailers --trailer "ack: Peff" \
+ --trailer "bug: 42" complex_message >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'using "where = end"' '
+ test_config trailer.review.key "Reviewed-by" &&
+ test_config trailer.review.where "end" &&
+ test_config trailer.ack.key "Acked-by= " &&
+ test_config trailer.ack.where "after" &&
+ test_config trailer.separators ":=" &&
+ cat complex_message_body >expected &&
+ sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+ Fixes: Z
+ Acked-by= Z
+ Acked-by= Peff
+ Reviewed-by: Z
+ Signed-off-by: Z
+ Reviewed-by: Junio
+ Reviewed-by: Johannes
+ EOF
+ git interpret-trailers --trailer "ack: Peff" \
+ --trailer "Reviewed-by: Junio" --trailer "Reviewed-by: Johannes" \
+ complex_message >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'using "where = start"' '
+ test_config trailer.review.key "Reviewed-by" &&
+ test_config trailer.review.where "start" &&
+ test_config trailer.ack.key "Acked-by= " &&
+ test_config trailer.ack.where "after" &&
+ test_config trailer.separators ":=" &&
+ cat complex_message_body >expected &&
+ sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+ Reviewed-by: Johannes
+ Reviewed-by: Junio
+ Fixes: Z
+ Acked-by= Z
+ Acked-by= Peff
+ Reviewed-by: Z
+ Signed-off-by: Z
+ EOF
+ git interpret-trailers --trailer "ack: Peff" \
+ --trailer "Reviewed-by: Junio" --trailer "Reviewed-by: Johannes" \
+ complex_message >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'using "where = before" for a token in the middle of the message' '
+ test_config trailer.review.key "Reviewed-by:" &&
+ test_config trailer.review.where "before" &&
+ test_config trailer.ack.key "Acked-by= " &&
+ test_config trailer.ack.where "after" &&
+ test_config trailer.bug.key "Bug #" &&
+ test_config trailer.bug.where "before" &&
+ test_config trailer.separators ":=#" &&
+ cat complex_message_body >expected &&
+ sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+ Bug #42
+ Fixes: Z
+ Acked-by= Z
+ Acked-by= Peff
+ Reviewed-by:Johan
+ Reviewed-by:
+ Signed-off-by: Z
+ EOF
+ git interpret-trailers --trailer "ack: Peff" --trailer "bug: 42" \
+ --trailer "review: Johan" <complex_message >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'using "where = before" and --trim-empty' '
+ test_config trailer.ack.key "Acked-by= " &&
+ test_config trailer.ack.where "after" &&
+ test_config trailer.bug.key "Bug #" &&
+ test_config trailer.bug.where "before" &&
+ test_config trailer.review.key "Reviewed-by:" &&
+ test_config trailer.separators ":=#" &&
+ cat complex_message_body >expected &&
+ cat >>expected <<-\EOF &&
+ Bug #46
+ Bug #42
+ Acked-by= Peff
+ Reviewed-by:Johan
+ EOF
+ git interpret-trailers --trim-empty --trailer "ack: Peff" \
+ --trailer "bug: 42" --trailer "review: Johan" \
+ --trailer "Bug: 46" <complex_message >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'the default is "ifExists = addIfDifferentNeighbor"' '
+ test_config trailer.ack.key "Acked-by= " &&
+ test_config trailer.ack.where "after" &&
+ test_config trailer.bug.key "Bug #" &&
+ test_config trailer.bug.where "before" &&
+ test_config trailer.review.key "Reviewed-by:" &&
+ test_config trailer.review.where "before" &&
+ test_config trailer.separators ":=#" &&
+ cat complex_message_body >expected &&
+ sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+ Bug #42
+ Fixes: Z
+ Acked-by= Z
+ Acked-by= Peff
+ Acked-by= Junio
+ Acked-by= Peff
+ Reviewed-by:
+ Signed-off-by: Z
+ EOF
+ git interpret-trailers --trailer "ack: Peff" --trailer "review:" \
+ --trailer "ack: Junio" --trailer "bug: 42" --trailer "ack: Peff" \
+ --trailer "ack: Peff" <complex_message >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'default "ifExists" is now "addIfDifferent"' '
+ test_config trailer.ifexists "addIfDifferent" &&
+ test_config trailer.ack.key "Acked-by= " &&
+ test_config trailer.ack.where "after" &&
+ test_config trailer.bug.key "Bug #" &&
+ test_config trailer.bug.where "before" &&
+ test_config trailer.review.key "Reviewed-by:" &&
+ test_config trailer.separators ":=#" &&
+ cat complex_message_body >expected &&
+ sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+ Bug #42
+ Fixes: Z
+ Acked-by= Z
+ Acked-by= Peff
+ Acked-by= Junio
+ Reviewed-by:
+ Signed-off-by: Z
+ EOF
+ git interpret-trailers --trailer "ack: Peff" --trailer "review:" \
+ --trailer "ack: Junio" --trailer "bug: 42" --trailer "ack: Peff" \
+ --trailer "ack: Peff" <complex_message >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'using "ifExists = addIfDifferent" with "where = end"' '
+ test_config trailer.ack.ifExists "addIfDifferent" &&
+ test_config trailer.ack.key "Acked-by= " &&
+ test_config trailer.ack.where "end" &&
+ test_config trailer.bug.key "Bug #" &&
+ test_config trailer.bug.where "before" &&
+ test_config trailer.review.key "Reviewed-by:" &&
+ test_config trailer.ifexists "addIfDifferent" &&
+ test_config trailer.separators ":=#" &&
+ cat complex_message_body >expected &&
+ sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+ Bug #42
+ Fixes: Z
+ Acked-by= Z
+ Reviewed-by:
+ Signed-off-by: Z
+ Acked-by= Peff
+ EOF
+ git interpret-trailers --trailer "ack: Peff" --trailer "review:" \
+ --trailer "bug: 42" --trailer "ack: Peff" \
+ <complex_message >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'using "ifExists = addIfDifferent" with "where = before"' '
+ test_config trailer.ack.ifExists "addIfDifferent" &&
+ test_config trailer.ack.key "Acked-by= " &&
+ test_config trailer.ack.where "before" &&
+ test_config trailer.bug.key "Bug #" &&
+ test_config trailer.bug.where "before" &&
+ test_config trailer.review.key "Reviewed-by:" &&
+ test_config trailer.ifexists "addIfDifferent" &&
+ test_config trailer.separators ":=#" &&
+ cat complex_message_body >expected &&
+ sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+ Bug #42
+ Fixes: Z
+ Acked-by= Peff
+ Acked-by= Z
+ Reviewed-by:
+ Signed-off-by: Z
+ EOF
+ git interpret-trailers --trailer "ack: Peff" --trailer "review:" \
+ --trailer "bug: 42" --trailer "ack: Peff" \
+ <complex_message >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'using "ifExists = addIfDifferentNeighbor" with "where = end"' '
+ test_config trailer.ack.ifExists "addIfDifferentNeighbor" &&
+ test_config trailer.ack.key "Acked-by= " &&
+ test_config trailer.ack.where "end" &&
+ test_config trailer.bug.key "Bug #" &&
+ test_config trailer.bug.where "before" &&
+ test_config trailer.review.key "Reviewed-by:" &&
+ test_config trailer.ifexists "addIfDifferent" &&
+ test_config trailer.separators ":=#" &&
+ cat complex_message_body >expected &&
+ sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+ Bug #42
+ Fixes: Z
+ Acked-by= Z
+ Reviewed-by:
+ Signed-off-by: Z
+ Acked-by= Peff
+ Acked-by= Junio
+ Tested-by: Jakub
+ Acked-by= Junio
+ Acked-by= Peff
+ EOF
+ git interpret-trailers --trailer "ack: Peff" --trailer "review:" \
+ --trailer "ack: Junio" --trailer "bug: 42" \
+ --trailer "Tested-by: Jakub" --trailer "ack: Junio" \
+ --trailer "ack: Junio" --trailer "ack: Peff" <complex_message >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'using "ifExists = addIfDifferentNeighbor" with "where = after"' '
+ test_config trailer.ack.ifExists "addIfDifferentNeighbor" &&
+ test_config trailer.ack.key "Acked-by= " &&
+ test_config trailer.ack.where "after" &&
+ test_config trailer.bug.key "Bug #" &&
+ test_config trailer.bug.where "before" &&
+ test_config trailer.review.key "Reviewed-by:" &&
+ test_config trailer.ifexists "addIfDifferent" &&
+ test_config trailer.separators ":=#" &&
+ cat complex_message_body >expected &&
+ sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+ Bug #42
+ Fixes: Z
+ Acked-by= Z
+ Acked-by= Peff
+ Acked-by= Junio
+ Acked-by= Peff
+ Reviewed-by:
+ Signed-off-by: Z
+ Tested-by: Jakub
+ EOF
+ git interpret-trailers --trailer "ack: Peff" --trailer "review:" \
+ --trailer "ack: Junio" --trailer "bug: 42" \
+ --trailer "Tested-by: Jakub" --trailer "ack: Junio" \
+ --trailer "ack: Junio" --trailer "ack: Peff" <complex_message >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'using "ifExists = addIfDifferentNeighbor" and --trim-empty' '
+ test_config trailer.ack.ifExists "addIfDifferentNeighbor" &&
+ test_config trailer.ack.key "Acked-by= " &&
+ test_config trailer.bug.key "Bug #" &&
+ test_config trailer.bug.where "before" &&
+ test_config trailer.separators ":=#" &&
+ cat complex_message_body >expected &&
+ cat >>expected <<-\EOF &&
+ Bug #42
+ Acked-by= Peff
+ Acked-by= Junio
+ Acked-by= Peff
+ EOF
+ git interpret-trailers --trim-empty --trailer "ack: Peff" \
+ --trailer "Acked-by= Peff" --trailer "review:" \
+ --trailer "ack: Junio" --trailer "bug: 42" \
+ --trailer "ack: Peff" <complex_message >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'using "ifExists = add" with "where = end"' '
+ test_config trailer.ack.ifExists "add" &&
+ test_config trailer.ack.key "Acked-by= " &&
+ test_config trailer.ack.where "end" &&
+ test_config trailer.bug.key "Bug #" &&
+ test_config trailer.bug.where "before" &&
+ test_config trailer.review.key "Reviewed-by:" &&
+ test_config trailer.ifexists "addIfDifferent" &&
+ test_config trailer.separators ":=#" &&
+ cat complex_message_body >expected &&
+ sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+ Bug #42
+ Fixes: Z
+ Acked-by= Z
+ Reviewed-by:
+ Signed-off-by: Z
+ Acked-by= Peff
+ Acked-by= Peff
+ Tested-by: Jakub
+ Acked-by= Junio
+ Tested-by: Johannes
+ Acked-by= Peff
+ EOF
+ git interpret-trailers --trailer "ack: Peff" \
+ --trailer "Acked-by= Peff" --trailer "review:" \
+ --trailer "Tested-by: Jakub" --trailer "ack: Junio" \
+ --trailer "bug: 42" --trailer "Tested-by: Johannes" \
+ --trailer "ack: Peff" <complex_message >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'using "ifExists = add" with "where = after"' '
+ test_config trailer.ack.ifExists "add" &&
+ test_config trailer.ack.key "Acked-by= " &&
+ test_config trailer.ack.where "after" &&
+ test_config trailer.bug.key "Bug #" &&
+ test_config trailer.bug.where "before" &&
+ test_config trailer.review.key "Reviewed-by:" &&
+ test_config trailer.ifexists "addIfDifferent" &&
+ test_config trailer.separators ":=#" &&
+ cat complex_message_body >expected &&
+ sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+ Bug #42
+ Fixes: Z
+ Acked-by= Z
+ Acked-by= Peff
+ Acked-by= Peff
+ Acked-by= Junio
+ Acked-by= Peff
+ Reviewed-by:
+ Signed-off-by: Z
+ EOF
+ git interpret-trailers --trailer "ack: Peff" \
+ --trailer "Acked-by= Peff" --trailer "review:" \
+ --trailer "ack: Junio" --trailer "bug: 42" \
+ --trailer "ack: Peff" <complex_message >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'overriding configuration with "--if-exists replace"' '
+ test_config trailer.fix.key "Fixes: " &&
+ test_config trailer.fix.ifExists "add" &&
+ test_config trailer.ack.key "Acked-by= " &&
+ test_config trailer.ack.where "after" &&
+ test_config trailer.bug.key "Bug #" &&
+ test_config trailer.bug.where "before" &&
+ test_config trailer.review.key "Reviewed-by:" &&
+ test_config trailer.review.where "before" &&
+ test_config trailer.separators ":=#" &&
+ cat complex_message_body >expected &&
+ sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+ Bug #42
+ Acked-by= Z
+ Reviewed-by:
+ Signed-off-by: Z
+ Fixes: 22
+ EOF
+ git interpret-trailers --if-exists replace --trailer "review:" \
+ --trailer "fix=53" --trailer "fix=22" --trailer "bug: 42" \
+ <complex_message >actual &&
+ test_cmp expected actual
+'
+
+# "trailer.ifexists" is set to "doNothing", so using "--no-if-exists" defaults
+# to this "doNothing" behavior. So the "Fixes: 53" trailer does not get added.
+test_expect_success 'using "--if-exists replace" with "--no-if-exists" defaults to configuration' '
+ test_config trailer.ifexists "doNothing" &&
+ cat complex_message_body >expected &&
+ sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+ Fixes: Z
+ Acked-by: Z
+ Reviewed-by: Z
+ Signed-off-by: Z
+ EOF
+ git interpret-trailers --if-exists replace --no-if-exists --trailer "Fixes: 53" \
+ <complex_message >actual &&
+ test_cmp expected actual
+'
+
+# No "ifexists" configuration is set, so using "--no-if-exists" makes it default
+# to addIfDifferentNeighbor. Because we do have a different neighbor "Fixes: 53"
+# (because it got added by overriding with "--if-exists replace" earlier in the
+# arguments list), we add "Signed-off-by: addme".
+test_expect_success 'using "--no-if-exists" defaults to hardcoded default if nothing configured' '
+ cat complex_message_body >expected &&
+ sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+ Acked-by: Z
+ Reviewed-by: Z
+ Signed-off-by: Z
+ Fixes: 53
+ Signed-off-by: addme
+ EOF
+ git interpret-trailers --if-exists replace --trailer "Fixes: 53" --no-if-exists \
+ --trailer "Signed-off-by: addme" <complex_message >actual &&
+ test_cmp expected actual
+'
+
+# The second "Fixes: 53" trailer is discarded, because the "--no-if-exists" here
+# makes us default to addIfDifferentNeighbor, and we already added the "Fixes:
+# 53" trailer earlier in the argument list.
+test_expect_success 'using "--no-if-exists" defaults to hardcoded default if nothing configured (no addition)' '
+ cat complex_message_body >expected &&
+ sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+ Acked-by: Z
+ Reviewed-by: Z
+ Signed-off-by: Z
+ Fixes: 53
+ EOF
+ git interpret-trailers --if-exists replace --trailer "Fixes: 53" --no-if-exists \
+ --trailer "Fixes: 53" <complex_message >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'using "ifExists = replace"' '
+ test_config trailer.fix.key "Fixes: " &&
+ test_config trailer.fix.ifExists "replace" &&
+ test_config trailer.ack.key "Acked-by= " &&
+ test_config trailer.ack.where "after" &&
+ test_config trailer.bug.key "Bug #" &&
+ test_config trailer.bug.where "before" &&
+ test_config trailer.review.key "Reviewed-by:" &&
+ test_config trailer.ifexists "addIfDifferent" &&
+ test_config trailer.separators ":=#" &&
+ cat complex_message_body >expected &&
+ sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+ Bug #42
+ Acked-by= Z
+ Acked-by= Junio
+ Acked-by= Peff
+ Reviewed-by:
+ Signed-off-by: Z
+ Fixes: 22
+ EOF
+ git interpret-trailers --trailer "review:" \
+ --trailer "fix=53" --trailer "ack: Junio" --trailer "fix=22" \
+ --trailer "bug: 42" --trailer "ack: Peff" \
+ <complex_message >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'using "ifExists = replace" with "where = after"' '
+ test_config trailer.ack.key "Acked-by= " &&
+ test_config trailer.ack.where "after" &&
+ test_config trailer.bug.key "Bug #" &&
+ test_config trailer.bug.where "before" &&
+ test_config trailer.fix.key "Fixes: " &&
+ test_config trailer.fix.ifExists "replace" &&
+ test_config trailer.fix.where "after" &&
+ test_config trailer.review.key "Reviewed-by:" &&
+ test_config trailer.ifexists "addIfDifferent" &&
+ test_config trailer.separators ":=#" &&
+ cat complex_message_body >expected &&
+ sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+ Bug #42
+ Fixes: 22
+ Acked-by= Z
+ Acked-by= Junio
+ Acked-by= Peff
+ Reviewed-by:
+ Signed-off-by: Z
+ EOF
+ git interpret-trailers --trailer "review:" \
+ --trailer "fix=53" --trailer "ack: Junio" --trailer "fix=22" \
+ --trailer "bug: 42" --trailer "ack: Peff" \
+ <complex_message >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'using "ifExists = doNothing"' '
+ test_config trailer.fix.ifExists "doNothing" &&
+ test_config trailer.ack.key "Acked-by= " &&
+ test_config trailer.ack.where "after" &&
+ test_config trailer.bug.key "Bug #" &&
+ test_config trailer.bug.where "before" &&
+ test_config trailer.fix.key "Fixes: " &&
+ test_config trailer.review.key "Reviewed-by:" &&
+ test_config trailer.ifexists "addIfDifferent" &&
+ test_config trailer.separators ":=#" &&
+ cat complex_message_body >expected &&
+ sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+ Bug #42
+ Fixes: Z
+ Acked-by= Z
+ Acked-by= Junio
+ Acked-by= Peff
+ Reviewed-by:
+ Signed-off-by: Z
+ EOF
+ git interpret-trailers --trailer "review:" --trailer "fix=53" \
+ --trailer "ack: Junio" --trailer "fix=22" \
+ --trailer "bug: 42" --trailer "ack: Peff" \
+ <complex_message >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'the default is "ifMissing = add"' '
+ test_config trailer.ack.key "Acked-by= " &&
+ test_config trailer.ack.where "after" &&
+ test_config trailer.bug.key "Bug #" &&
+ test_config trailer.bug.where "before" &&
+ test_config trailer.cc.key "Cc: " &&
+ test_config trailer.cc.where "before" &&
+ test_config trailer.fix.key "Fixes: " &&
+ test_config trailer.fix.ifExists "doNothing" &&
+ test_config trailer.review.key "Reviewed-by:" &&
+ test_config trailer.ifexists "addIfDifferent" &&
+ test_config trailer.separators ":=#" &&
+ cat complex_message_body >expected &&
+ sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+ Bug #42
+ Cc: Linus
+ Fixes: Z
+ Acked-by= Z
+ Acked-by= Junio
+ Acked-by= Peff
+ Reviewed-by:
+ Signed-off-by: Z
+ EOF
+ git interpret-trailers --trailer "review:" --trailer "fix=53" \
+ --trailer "cc=Linus" --trailer "ack: Junio" \
+ --trailer "fix=22" --trailer "bug: 42" --trailer "ack: Peff" \
+ <complex_message >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'overriding configuration with "--if-missing doNothing"' '
+ test_config trailer.ack.key "Acked-by= " &&
+ test_config trailer.ack.where "after" &&
+ test_config trailer.fix.key "Fixes: " &&
+ test_config trailer.fix.ifExists "doNothing" &&
+ test_config trailer.review.key "Reviewed-by:" &&
+ test_config trailer.ifexists "addIfDifferent" &&
+ test_config trailer.ifmissing "add" &&
+ test_config trailer.separators ":=" &&
+ cat complex_message_body >expected &&
+ sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+ Fixes: Z
+ Acked-by= Z
+ Acked-by= Junio
+ Acked-by= Peff
+ Reviewed-by:
+ Signed-off-by: Z
+ EOF
+ git interpret-trailers --if-missing doNothing \
+ --trailer "review:" --trailer "fix=53" \
+ --trailer "cc=Linus" --trailer "ack: Junio" \
+ --trailer "fix=22" --trailer "bug: 42" --trailer "ack: Peff" \
+ <complex_message >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'when default "ifMissing" is "doNothing"' '
+ test_config trailer.ack.key "Acked-by= " &&
+ test_config trailer.ack.where "after" &&
+ test_config trailer.fix.ifExists "doNothing" &&
+ test_config trailer.review.key "Reviewed-by:" &&
+ test_config trailer.ifexists "addIfDifferent" &&
+ test_config trailer.ifmissing "doNothing" &&
+ test_config trailer.separators ":=" &&
+ cat complex_message_body >expected &&
+ sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+ Fixes: Z
+ Acked-by= Z
+ Acked-by= Junio
+ Acked-by= Peff
+ Reviewed-by:
+ Signed-off-by: Z
+ EOF
+ git interpret-trailers --trailer "review:" --trailer "fix=53" \
+ --trailer "cc=Linus" --trailer "ack: Junio" \
+ --trailer "fix=22" --trailer "bug: 42" --trailer "ack: Peff" \
+ <complex_message >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'using "ifMissing = add" with "where = end"' '
+ test_config trailer.ack.key "Acked-by= " &&
+ test_config trailer.ack.where "after" &&
+ test_config trailer.bug.key "Bug #" &&
+ test_config trailer.bug.where "before" &&
+ test_config trailer.cc.key "Cc: " &&
+ test_config trailer.cc.ifMissing "add" &&
+ test_config trailer.cc.where "end" &&
+ test_config trailer.fix.ifExists "doNothing" &&
+ test_config trailer.review.key "Reviewed-by:" &&
+ test_config trailer.ifexists "addIfDifferent" &&
+ test_config trailer.separators ":=#" &&
+ cat complex_message_body >expected &&
+ sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+ Bug #42
+ Fixes: Z
+ Acked-by= Z
+ Acked-by= Junio
+ Acked-by= Peff
+ Reviewed-by:
+ Signed-off-by: Z
+ Cc: Linus
+ EOF
+ git interpret-trailers --trailer "review:" --trailer "fix=53" \
+ --trailer "ack: Junio" --trailer "fix=22" \
+ --trailer "bug: 42" --trailer "cc=Linus" --trailer "ack: Peff" \
+ <complex_message >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'using "ifMissing = add" with "where = before"' '
+ test_config trailer.ack.key "Acked-by= " &&
+ test_config trailer.ack.where "after" &&
+ test_config trailer.bug.key "Bug #" &&
+ test_config trailer.bug.where "before" &&
+ test_config trailer.cc.key "Cc: " &&
+ test_config trailer.cc.ifMissing "add" &&
+ test_config trailer.cc.where "before" &&
+ test_config trailer.fix.ifExists "doNothing" &&
+ test_config trailer.review.key "Reviewed-by:" &&
+ test_config trailer.ifexists "addIfDifferent" &&
+ test_config trailer.separators ":=#" &&
+ cat complex_message_body >expected &&
+ sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+ Cc: Linus
+ Bug #42
+ Fixes: Z
+ Acked-by= Z
+ Acked-by= Junio
+ Acked-by= Peff
+ Reviewed-by:
+ Signed-off-by: Z
+ EOF
+ git interpret-trailers --trailer "review:" --trailer "fix=53" \
+ --trailer "ack: Junio" --trailer "fix=22" \
+ --trailer "bug: 42" --trailer "cc=Linus" --trailer "ack: Peff" \
+ <complex_message >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'using "ifMissing = doNothing"' '
+ test_config trailer.ack.key "Acked-by= " &&
+ test_config trailer.ack.where "after" &&
+ test_config trailer.bug.key "Bug #" &&
+ test_config trailer.bug.where "before" &&
+ test_config trailer.cc.ifMissing "doNothing" &&
+ test_config trailer.fix.ifExists "doNothing" &&
+ test_config trailer.review.key "Reviewed-by:" &&
+ test_config trailer.ifexists "addIfDifferent" &&
+ test_config trailer.separators ":=#" &&
+ cat complex_message_body >expected &&
+ sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+ Bug #42
+ Fixes: Z
+ Acked-by= Z
+ Acked-by= Junio
+ Acked-by= Peff
+ Reviewed-by:
+ Signed-off-by: Z
+ EOF
+ git interpret-trailers --trailer "review:" --trailer "fix=53" \
+ --trailer "cc=Linus" --trailer "ack: Junio" \
+ --trailer "fix=22" --trailer "bug: 42" --trailer "ack: Peff" \
+ <complex_message >actual &&
+ test_cmp expected actual
+'
+
+# Ignore the "IgnoredTrailer" because of "--if-missing doNothing", but also
+# ignore the "StillIgnoredTrailer" because we set "trailer.ifMissing" to
+# "doNothing" in configuration.
+test_expect_success 'using "--no-if-missing" defaults to configuration' '
+ test_config trailer.ifMissing "doNothing" &&
+ cat complex_message_body >expected &&
+ sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+ Fixes: Z
+ Acked-by: Z
+ Reviewed-by: Z
+ Signed-off-by: Z
+ EOF
+ git interpret-trailers --if-missing doNothing --trailer "IgnoredTrailer: ignoreme" --no-if-missing \
+ --trailer "StillIgnoredTrailer: ignoreme" <complex_message >actual &&
+ test_cmp expected actual
+'
+
+# Add the "AddedTrailer" because the "--no-if-missing" clears the "--if-missing
+# doNothing" from earlier in the argument list.
+test_expect_success 'using "--no-if-missing" defaults to hardcoded default if nothing configured' '
+ cat complex_message_body >expected &&
+ sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+ Fixes: Z
+ Acked-by: Z
+ Reviewed-by: Z
+ Signed-off-by: Z
+ AddedTrailer: addme
+ EOF
+ git interpret-trailers --if-missing doNothing --trailer "IgnoredTrailer: ignoreme" --no-if-missing \
+ --trailer "AddedTrailer: addme" <complex_message >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'default "where" is now "after"' '
+ git config trailer.where "after" &&
+ test_config trailer.ack.ifExists "add" &&
+ test_config trailer.ack.key "Acked-by= " &&
+ test_config trailer.ack.where "after" &&
+ test_config trailer.bug.key "Bug #" &&
+ test_config trailer.bug.where "before" &&
+ test_config trailer.fix.ifExists "doNothing" &&
+ test_config trailer.review.key "Reviewed-by:" &&
+ test_config trailer.ifexists "addIfDifferent" &&
+ test_config trailer.separators ":=#" &&
+ cat complex_message_body >expected &&
+ sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+ Bug #42
+ Fixes: Z
+ Acked-by= Z
+ Acked-by= Peff
+ Acked-by= Peff
+ Acked-by= Junio
+ Acked-by= Peff
+ Reviewed-by:
+ Signed-off-by: Z
+ Tested-by: Jakub
+ Tested-by: Johannes
+ EOF
+ git interpret-trailers --trailer "ack: Peff" \
+ --trailer "Acked-by= Peff" --trailer "review:" \
+ --trailer "Tested-by: Jakub" --trailer "ack: Junio" \
+ --trailer "bug: 42" --trailer "Tested-by: Johannes" \
+ --trailer "ack: Peff" <complex_message >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'with simple command' '
+ test_config trailer.ack.key "Acked-by= " &&
+ test_config trailer.fix.ifExists "doNothing" &&
+ test_config trailer.review.key "Reviewed-by:" &&
+ test_config trailer.sign.command "echo \"A U Thor <author@example.com>\"" &&
+ test_config trailer.sign.key "Signed-off-by: " &&
+ test_config trailer.sign.ifExists "addIfDifferentNeighbor" &&
+ test_config trailer.sign.where "after" &&
+ test_config trailer.ifexists "addIfDifferent" &&
+ test_config trailer.separators ":=" &&
+ cat complex_message_body >expected &&
+ sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+ Fixes: Z
+ Acked-by= Z
+ Reviewed-by:
+ Signed-off-by: Z
+ Signed-off-by: A U Thor <author@example.com>
+ EOF
+ git interpret-trailers --trailer "review:" --trailer "fix=22" \
+ <complex_message >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'with command using committer information' '
+ test_config trailer.ack.key "Acked-by= " &&
+ test_config trailer.fix.ifExists "doNothing" &&
+ test_config trailer.review.key "Reviewed-by:" &&
+ test_config trailer.sign.command "echo \"\$GIT_COMMITTER_NAME <\$GIT_COMMITTER_EMAIL>\"" &&
+ test_config trailer.sign.key "Signed-off-by: " &&
+ test_config trailer.sign.ifExists "addIfDifferent" &&
+ test_config trailer.ifexists "addIfDifferent" &&
+ test_config trailer.separators ":=" &&
+ cat complex_message_body >expected &&
+ sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+ Fixes: Z
+ Acked-by= Z
+ Reviewed-by:
+ Signed-off-by: Z
+ Signed-off-by: C O Mitter <committer@example.com>
+ EOF
+ git interpret-trailers --trailer "review:" --trailer "fix=22" \
+ <complex_message >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'with command using author information' '
+ test_config trailer.ack.key "Acked-by= " &&
+ test_config trailer.fix.ifExists "doNothing" &&
+ test_config trailer.review.key "Reviewed-by:" &&
+ test_config trailer.sign.command "echo \"\$GIT_AUTHOR_NAME <\$GIT_AUTHOR_EMAIL>\"" &&
+ test_config trailer.sign.key "Signed-off-by: " &&
+ test_config trailer.sign.ifExists "addIfDifferentNeighbor" &&
+ test_config trailer.sign.where "after" &&
+ test_config trailer.ifexists "addIfDifferent" &&
+ test_config trailer.separators ":=" &&
+ cat complex_message_body >expected &&
+ sed -e "s/ Z\$/ /" >>expected <<-\EOF &&
+ Fixes: Z
+ Acked-by= Z
+ Reviewed-by:
+ Signed-off-by: Z
+ Signed-off-by: A U Thor <author@example.com>
+ EOF
+ git interpret-trailers --trailer "review:" --trailer "fix=22" \
+ <complex_message >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'setup a commit' '
+ echo "Content of the first commit." > a.txt &&
+ git add a.txt &&
+ git commit -m "Add file a.txt"
+'
+
+test_expect_success 'cmd takes precedence over command' '
+ test_config trailer.ack.key "Acked-by= " &&
+ test_config trailer.fix.command "git log -1 --oneline --format=\"%h (%s)\" \
+ --abbrev-commit --abbrev=14 \$ARG" &&
+ test_config trailer.fix.cmd "test -n \"\$1\" && git log -1 --oneline --format=\"%h (%aN)\" \
+ --abbrev-commit --abbrev=14 \"\$1\" || true" &&
+ test_config trailer.fix.key "Fixes: " &&
+ test_config trailer.fix.ifExists "replace" &&
+ test_config trailer.fix.where "after" &&
+ test_config trailer.review.key "Reviewed-by:" &&
+ test_config trailer.sign.command "echo \"\$GIT_AUTHOR_NAME <\$GIT_AUTHOR_EMAIL>\"" &&
+ test_config trailer.sign.key "Signed-off-by: " &&
+ test_config trailer.ifexists "addIfDifferent" &&
+ test_config trailer.separators ":=" &&
+ FIXED=$(git log -1 --oneline --format="%h (%aN)" --abbrev-commit --abbrev=14 HEAD) &&
+ cat complex_message_body >expected2 &&
+ sed -e "s/ Z\$/ /" >>expected2 <<-EOF &&
+ Fixes: $FIXED
+ Acked-by= Z
+ Reviewed-by:
+ Signed-off-by: Z
+ Signed-off-by: A U Thor <author@example.com>
+ EOF
+ git interpret-trailers --trailer "review:" --trailer "fix=HEAD" \
+ <complex_message >actual2 &&
+ test_cmp expected2 actual2
+'
+
+test_expect_success 'with command using $ARG' '
+ test_config trailer.ack.key "Acked-by= " &&
+ test_config trailer.fix.command "git log -1 --oneline --format=\"%h (%s)\" --abbrev-commit --abbrev=14 \$ARG" &&
+ test_config trailer.fix.key "Fixes: " &&
+ test_config trailer.fix.ifExists "replace" &&
+ test_config trailer.fix.where "after" &&
+ test_config trailer.review.key "Reviewed-by:" &&
+ test_config trailer.sign.command "echo \"\$GIT_AUTHOR_NAME <\$GIT_AUTHOR_EMAIL>\"" &&
+ test_config trailer.sign.key "Signed-off-by: " &&
+ test_config trailer.ifexists "addIfDifferent" &&
+ test_config trailer.separators ":=" &&
+ FIXED=$(git log -1 --oneline --format="%h (%s)" --abbrev-commit --abbrev=14 HEAD) &&
+ cat complex_message_body >expected &&
+ sed -e "s/ Z\$/ /" >>expected <<-EOF &&
+ Fixes: $FIXED
+ Acked-by= Z
+ Reviewed-by:
+ Signed-off-by: Z
+ Signed-off-by: A U Thor <author@example.com>
+ EOF
+ git interpret-trailers --trailer "review:" --trailer "fix=HEAD" \
+ <complex_message >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'with failing command using $ARG' '
+ test_config trailer.ack.key "Acked-by= " &&
+ test_config trailer.fix.command "false \$ARG" &&
+ test_config trailer.fix.key "Fixes: " &&
+ test_config trailer.fix.ifExists "replace" &&
+ test_config trailer.fix.where "after" &&
+ test_config trailer.review.key "Reviewed-by:" &&
+ test_config trailer.sign.command "echo \"\$GIT_AUTHOR_NAME <\$GIT_AUTHOR_EMAIL>\"" &&
+ test_config trailer.sign.key "Signed-off-by: " &&
+ test_config trailer.ifexists "addIfDifferent" &&
+ test_config trailer.separators ":=" &&
+ cat complex_message_body >expected &&
+ sed -e "s/ Z\$/ /" >>expected <<-EOF &&
+ Fixes: Z
+ Acked-by= Z
+ Reviewed-by:
+ Signed-off-by: Z
+ Signed-off-by: A U Thor <author@example.com>
+ EOF
+ git interpret-trailers --trailer "review:" --trailer "fix=HEAD" \
+ <complex_message >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'with empty tokens' '
+ test_config trailer.sign.command "echo \"\$GIT_AUTHOR_NAME <\$GIT_AUTHOR_EMAIL>\"" &&
+ test_config trailer.sign.key "Signed-off-by: " &&
+ test_config trailer.ifexists "addIfDifferent" &&
+ cat >expected <<-EOF &&
+
+ Signed-off-by: A U Thor <author@example.com>
+ EOF
+ git interpret-trailers --trailer ":" --trailer ":test" >actual <<-EOF &&
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'with command but no key' '
+ test_config trailer.sign.command "echo \"\$GIT_AUTHOR_NAME <\$GIT_AUTHOR_EMAIL>\"" &&
+ test_config trailer.ifexists "addIfDifferent" &&
+ cat >expected <<-EOF &&
+
+ sign: A U Thor <author@example.com>
+ EOF
+ git interpret-trailers >actual <<-EOF &&
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'with no command and no key' '
+ test_config trailer.review.where "before" &&
+ test_config trailer.sign.command "echo \"\$GIT_AUTHOR_NAME <\$GIT_AUTHOR_EMAIL>\"" &&
+ test_config trailer.ifexists "addIfDifferent" &&
+ cat >expected <<-EOF &&
+
+ review: Junio
+ sign: A U Thor <author@example.com>
+ EOF
+ git interpret-trailers --trailer "review:Junio" >actual <<-EOF &&
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'with cut line' '
+ test_config trailer.review.where "before" &&
+ test_config trailer.sign.command "echo \"\$GIT_AUTHOR_NAME <\$GIT_AUTHOR_EMAIL>\"" &&
+ cat >expected <<-\EOF &&
+ my subject
+
+ review: Brian
+ sign: A U Thor <author@example.com>
+ # ------------------------ >8 ------------------------
+ ignore this
+ EOF
+ git interpret-trailers --trailer review:Brian >actual <<-\EOF &&
+ my subject
+ # ------------------------ >8 ------------------------
+ ignore this
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'only trailers' '
+ test_config trailer.sign.command "echo config-value" &&
+ test_config trailer.ifexists "addIfDifferent" &&
+ cat >expected <<-\EOF &&
+ existing: existing-value
+ sign: config-value
+ added: added-value
+ EOF
+ git interpret-trailers \
+ --trailer added:added-value \
+ --only-trailers >actual <<-\EOF &&
+ my subject
+
+ my body
+
+ existing: existing-value
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'only-trailers omits non-trailer in middle of block' '
+ test_config trailer.sign.command "echo config-value" &&
+ cat >expected <<-\EOF &&
+ Signed-off-by: nobody <nobody@nowhere>
+ Signed-off-by: somebody <somebody@somewhere>
+ sign: config-value
+ EOF
+ git interpret-trailers --only-trailers >actual <<-\EOF &&
+ subject
+
+ it is important that the trailers below are signed-off-by
+ so that they meet the "25% trailers Git knows about" heuristic
+
+ Signed-off-by: nobody <nobody@nowhere>
+ this is not a trailer
+ Signed-off-by: somebody <somebody@somewhere>
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'only input' '
+ test_config trailer.sign.command "echo config-value" &&
+ cat >expected <<-\EOF &&
+ existing: existing-value
+ EOF
+ git interpret-trailers \
+ --only-trailers --only-input >actual <<-\EOF &&
+ my subject
+
+ my body
+
+ existing: existing-value
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'unfold' '
+ cat >expected <<-\EOF &&
+ foo: continued across several lines
+ EOF
+ # pass through tr to make leading and trailing whitespace more obvious
+ tr _ " " <<-\EOF |
+ my subject
+
+ my body
+
+ foo:_
+ __continued
+ ___across
+ ____several
+ _____lines
+ ___
+ EOF
+ git interpret-trailers --only-trailers --only-input --unfold >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'handling of --- lines in input' '
+ echo "real-trailer: just right" >expected &&
+
+ git interpret-trailers --parse >actual <<-\EOF &&
+ subject
+
+ body
+
+ not-a-trailer: too soon
+ ------ this is just a line in the commit message with a bunch of
+ ------ dashes; it does not have any syntactic meaning.
+
+ real-trailer: just right
+ ---
+ below the dashed line may be a patch, etc.
+
+ not-a-trailer: too late
+ EOF
+
+ test_cmp expected actual
+'
+
+test_expect_success 'suppress --- handling' '
+ echo "real-trailer: just right" >expected &&
+
+ git interpret-trailers --parse --no-divider >actual <<-\EOF &&
+ subject
+
+ This commit message has a "---" in it, but because we tell
+ interpret-trailers not to respect that, it has no effect.
+
+ not-a-trailer: too soon
+ ---
+
+ This is still the commit message body.
+
+ real-trailer: just right
+ EOF
+
+ test_cmp expected actual
+'
+
+test_done
diff --git a/t/t7514-commit-patch.sh b/t/t7514-commit-patch.sh
new file mode 100755
index 0000000..998a210
--- /dev/null
+++ b/t/t7514-commit-patch.sh
@@ -0,0 +1,34 @@
+#!/bin/sh
+
+test_description='hunk edit with "commit -p -m"'
+. ./test-lib.sh
+
+if ! test_have_prereq PERL
+then
+ skip_all="skipping '$test_description' tests, perl not available"
+ test_done
+fi
+
+test_expect_success 'setup (initial)' '
+ echo line1 >file &&
+ git add file &&
+ git commit -m commit1
+'
+
+test_expect_success 'edit hunk "commit -p -m message"' '
+ test_when_finished "rm -f editor_was_started" &&
+ rm -f editor_was_started &&
+ echo more >>file &&
+ echo e | env GIT_EDITOR=": >editor_was_started" git commit -p -m commit2 file &&
+ test -r editor_was_started
+'
+
+test_expect_success 'edit hunk "commit --dry-run -p -m message"' '
+ test_when_finished "rm -f editor_was_started" &&
+ rm -f editor_was_started &&
+ echo more >>file &&
+ echo e | env GIT_EDITOR=": >editor_was_started" git commit -p -m commit3 file &&
+ test -r editor_was_started
+'
+
+test_done
diff --git a/t/t7515-status-symlinks.sh b/t/t7515-status-symlinks.sh
new file mode 100755
index 0000000..e3d6bb6
--- /dev/null
+++ b/t/t7515-status-symlinks.sh
@@ -0,0 +1,44 @@
+#!/bin/sh
+
+test_description='git status and symlinks'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ echo .gitignore >.gitignore &&
+ echo actual >>.gitignore &&
+ echo expect >>.gitignore &&
+ mkdir dir &&
+ echo x >dir/file1 &&
+ echo y >dir/file2 &&
+ git add dir &&
+ git commit -m initial &&
+ git tag initial
+'
+
+test_expect_success SYMLINKS 'symlink to a directory' '
+ test_when_finished "rm symlink" &&
+ ln -s dir symlink &&
+ echo "?? symlink" >expect &&
+ git status --porcelain >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success SYMLINKS 'symlink replacing a directory' '
+ test_when_finished "rm -rf copy && git reset --hard initial" &&
+ mkdir copy &&
+ cp dir/file1 copy/file1 &&
+ echo "changed in copy" >copy/file2 &&
+ git add copy &&
+ git commit -m second &&
+ rm -rf copy &&
+ ln -s dir copy &&
+ echo " D copy/file1" >expect &&
+ echo " D copy/file2" >>expect &&
+ echo "?? copy" >>expect &&
+ git status --porcelain >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t7516-commit-races.sh b/t/t7516-commit-races.sh
new file mode 100755
index 0000000..bb95f09
--- /dev/null
+++ b/t/t7516-commit-races.sh
@@ -0,0 +1,33 @@
+#!/bin/sh
+
+test_description='git commit races'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'race to create orphan commit' '
+ write_script hare-editor <<-\EOF &&
+ git commit --allow-empty -m hare
+ EOF
+ test_must_fail env EDITOR=./hare-editor git commit --allow-empty -m tortoise -e &&
+ git show -s --pretty=format:%s >subject &&
+ grep hare subject &&
+ git show -s --pretty=format:%P >out &&
+ test_must_be_empty out
+'
+
+test_expect_success 'race to create non-orphan commit' '
+ write_script airplane-editor <<-\EOF &&
+ git commit --allow-empty -m airplane
+ EOF
+ git checkout --orphan branch &&
+ git commit --allow-empty -m base &&
+ git rev-parse HEAD >base &&
+ test_must_fail env EDITOR=./airplane-editor git commit --allow-empty -m ship -e &&
+ git show -s --pretty=format:%s >subject &&
+ grep airplane subject &&
+ git rev-parse HEAD^ >parent &&
+ test_cmp base parent
+'
+
+test_done
diff --git a/t/t7517-per-repo-email.sh b/t/t7517-per-repo-email.sh
new file mode 100755
index 0000000..efc6496
--- /dev/null
+++ b/t/t7517-per-repo-email.sh
@@ -0,0 +1,153 @@
+#!/bin/sh
+#
+# Copyright (c) 2016 Dan Aloni
+# Copyright (c) 2016 Jeff King
+#
+
+test_description='per-repo forced setting of email address'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup a likely user.useConfigOnly use case' '
+ # we want to make sure a reflog is written, since that needs
+ # a non-strict ident. So be sure we have an actual commit.
+ test_commit foo &&
+
+ sane_unset GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL &&
+ sane_unset GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL &&
+ git config user.name "test" &&
+ git config --global user.useConfigOnly true
+'
+
+test_expect_success 'fails committing if clone email is not set' '
+ test_must_fail git commit --allow-empty -m msg
+'
+
+test_expect_success 'fails committing if clone email is not set, but EMAIL set' '
+ test_must_fail env EMAIL=test@fail.com git commit --allow-empty -m msg
+'
+
+test_expect_success 'succeeds committing if clone email is set' '
+ test_config user.email "test@ok.com" &&
+ git commit --allow-empty -m msg
+'
+
+test_expect_success 'succeeds cloning if global email is not set' '
+ git clone . clone
+'
+
+test_expect_success 'set up rebase scenarios' '
+ # temporarily enable an actual ident for this setup
+ test_config user.email foo@example.com &&
+ test_commit new &&
+ git branch side-without-commit HEAD^ &&
+ git checkout -b side-with-commit HEAD^ &&
+ test_commit side
+'
+
+test_expect_success 'fast-forward rebase does not care about ident' '
+ git checkout -B tmp side-without-commit &&
+ git rebase main
+'
+
+test_expect_success 'non-fast-forward rebase refuses to write commits' '
+ test_when_finished "git rebase --abort || true" &&
+ git checkout -B tmp side-with-commit &&
+ test_must_fail git rebase main
+'
+
+test_expect_success 'fast-forward rebase does not care about ident (interactive)' '
+ git checkout -B tmp side-without-commit &&
+ git rebase -i main
+'
+
+test_expect_success 'non-fast-forward rebase refuses to write commits (interactive)' '
+ test_when_finished "git rebase --abort || true" &&
+ git checkout -B tmp side-with-commit &&
+ test_must_fail git rebase -i main
+'
+
+test_expect_success 'noop interactive rebase does not care about ident' '
+ git checkout -B tmp side-with-commit &&
+ git rebase -i HEAD^
+'
+
+test_expect_success 'author.name overrides user.name' '
+ test_config user.name user &&
+ test_config user.email user@example.com &&
+ test_config author.name author &&
+ test_commit author-name-override-user &&
+ echo author user@example.com > expected-author &&
+ echo user user@example.com > expected-committer &&
+ git log --format="%an %ae" -1 > actual-author &&
+ git log --format="%cn %ce" -1 > actual-committer &&
+ test_cmp expected-author actual-author &&
+ test_cmp expected-committer actual-committer
+'
+
+test_expect_success 'author.email overrides user.email' '
+ test_config user.name user &&
+ test_config user.email user@example.com &&
+ test_config author.email author@example.com &&
+ test_commit author-email-override-user &&
+ echo user author@example.com > expected-author &&
+ echo user user@example.com > expected-committer &&
+ git log --format="%an %ae" -1 > actual-author &&
+ git log --format="%cn %ce" -1 > actual-committer &&
+ test_cmp expected-author actual-author &&
+ test_cmp expected-committer actual-committer
+'
+
+test_expect_success 'committer.name overrides user.name' '
+ test_config user.name user &&
+ test_config user.email user@example.com &&
+ test_config committer.name committer &&
+ test_commit committer-name-override-user &&
+ echo user user@example.com > expected-author &&
+ echo committer user@example.com > expected-committer &&
+ git log --format="%an %ae" -1 > actual-author &&
+ git log --format="%cn %ce" -1 > actual-committer &&
+ test_cmp expected-author actual-author &&
+ test_cmp expected-committer actual-committer
+'
+
+test_expect_success 'committer.email overrides user.email' '
+ test_config user.name user &&
+ test_config user.email user@example.com &&
+ test_config committer.email committer@example.com &&
+ test_commit committer-email-override-user &&
+ echo user user@example.com > expected-author &&
+ echo user committer@example.com > expected-committer &&
+ git log --format="%an %ae" -1 > actual-author &&
+ git log --format="%cn %ce" -1 > actual-committer &&
+ test_cmp expected-author actual-author &&
+ test_cmp expected-committer actual-committer
+'
+
+test_expect_success 'author and committer environment variables override config settings' '
+ test_config user.name user &&
+ test_config user.email user@example.com &&
+ test_config author.name author &&
+ test_config author.email author@example.com &&
+ test_config committer.name committer &&
+ test_config committer.email committer@example.com &&
+ GIT_AUTHOR_NAME=env_author && export GIT_AUTHOR_NAME &&
+ GIT_AUTHOR_EMAIL=env_author@example.com && export GIT_AUTHOR_EMAIL &&
+ GIT_COMMITTER_NAME=env_commit && export GIT_COMMITTER_NAME &&
+ GIT_COMMITTER_EMAIL=env_commit@example.com && export GIT_COMMITTER_EMAIL &&
+ test_commit env-override-conf &&
+ echo env_author env_author@example.com > expected-author &&
+ echo env_commit env_commit@example.com > expected-committer &&
+ git log --format="%an %ae" -1 > actual-author &&
+ git log --format="%cn %ce" -1 > actual-committer &&
+ sane_unset GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL &&
+ sane_unset GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL &&
+ test_cmp expected-author actual-author &&
+ test_cmp expected-committer actual-committer
+'
+
+test_done
diff --git a/t/t7518-ident-corner-cases.sh b/t/t7518-ident-corner-cases.sh
new file mode 100755
index 0000000..b37de0a
--- /dev/null
+++ b/t/t7518-ident-corner-cases.sh
@@ -0,0 +1,58 @@
+#!/bin/sh
+
+test_description='corner cases in ident strings'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# confirm that we do not segfault _and_ that we do not say "(null)", as
+# glibc systems will quietly handle our NULL pointer
+#
+# Note also that we can't use "env" here because we need to unset a variable,
+# and "-u" is not portable.
+test_expect_success 'empty name and missing email' '
+ (
+ sane_unset GIT_AUTHOR_EMAIL &&
+ GIT_AUTHOR_NAME= &&
+ test_must_fail git commit --allow-empty -m foo 2>err &&
+ test_grep ! "(null)" err
+ )
+'
+
+test_expect_success 'commit rejects all-crud name' '
+ test_must_fail env GIT_AUTHOR_NAME=" ,;<>" \
+ git commit --allow-empty -m foo
+'
+
+test_expect_success 'commit does not strip trailing dot' '
+ author_name="Pat Doe Jr." &&
+ env GIT_AUTHOR_NAME="$author_name" \
+ git commit --allow-empty -m foo &&
+ git log -1 --format=%an >actual &&
+ echo "$author_name" >expected &&
+ test_cmp actual expected
+'
+
+# We must test the actual error message here, as an unwanted
+# auto-detection could fail for other reasons.
+test_expect_success 'empty configured name does not auto-detect' '
+ (
+ sane_unset GIT_AUTHOR_NAME &&
+ test_must_fail \
+ git -c user.name= commit --allow-empty -m foo 2>err &&
+ test_grep "empty ident name" err &&
+ test_grep "Author identity unknown" err
+ )
+'
+
+test_expect_success 'empty configured name does not auto-detect for committer' '
+ (
+ sane_unset GIT_COMMITTER_NAME &&
+ test_must_fail \
+ git -c user.name= commit --allow-empty -m foo 2>err &&
+ test_grep "empty ident name" err &&
+ test_grep "Committer identity unknown" err
+ )
+'
+
+test_done
diff --git a/t/t7519-status-fsmonitor.sh b/t/t7519-status-fsmonitor.sh
new file mode 100755
index 0000000..7ee69ec
--- /dev/null
+++ b/t/t7519-status-fsmonitor.sh
@@ -0,0 +1,480 @@
+#!/bin/sh
+
+test_description='git status with file system watcher'
+
+. ./test-lib.sh
+
+# Note, after "git reset --hard HEAD" no extensions exist other than 'TREE'
+# "git update-index --fsmonitor" can be used to get the extension written
+# before testing the results.
+
+clean_repo () {
+ git reset --hard HEAD &&
+ git clean -fd
+}
+
+dirty_repo () {
+ : >untracked &&
+ : >dir1/untracked &&
+ : >dir2/untracked &&
+ echo 1 >modified &&
+ echo 2 >dir1/modified &&
+ echo 3 >dir2/modified &&
+ echo 4 >new &&
+ echo 5 >dir1/new &&
+ echo 6 >dir2/new
+}
+
+write_integration_script () {
+ test_hook --setup --clobber fsmonitor-test<<-\EOF
+ if test "$#" -ne 2
+ then
+ echo "$0: exactly 2 arguments expected"
+ exit 2
+ fi
+ if test "$1" != 2
+ then
+ echo "Unsupported core.fsmonitor hook version." >&2
+ exit 1
+ fi
+ printf "last_update_token\0"
+ printf "untracked\0"
+ printf "dir1/untracked\0"
+ printf "dir2/untracked\0"
+ printf "modified\0"
+ printf "dir1/modified\0"
+ printf "dir2/modified\0"
+ printf "new\0"
+ printf "dir1/new\0"
+ printf "dir2/new\0"
+ EOF
+}
+
+test_lazy_prereq UNTRACKED_CACHE '
+ { git update-index --test-untracked-cache; ret=$?; } &&
+ test $ret -ne 1
+'
+
+# Test that we detect and disallow repos that are incompatible with FSMonitor.
+test_expect_success 'incompatible bare repo' '
+ test_when_finished "rm -rf ./bare-clone actual expect" &&
+ git init --bare bare-clone &&
+
+ test_must_fail \
+ git -C ./bare-clone -c core.fsmonitor=foo \
+ update-index --fsmonitor 2>actual &&
+ grep "bare repository .* is incompatible with fsmonitor" actual &&
+
+ test_must_fail \
+ git -C ./bare-clone -c core.fsmonitor=true \
+ update-index --fsmonitor 2>actual &&
+ grep "bare repository .* is incompatible with fsmonitor" actual
+'
+
+test_expect_success FSMONITOR_DAEMON 'run fsmonitor-daemon in bare repo' '
+ test_when_finished "rm -rf ./bare-clone actual" &&
+ git init --bare bare-clone &&
+ test_must_fail git -C ./bare-clone fsmonitor--daemon run 2>actual &&
+ grep "bare repository .* is incompatible with fsmonitor" actual
+'
+
+test_expect_success MINGW,FSMONITOR_DAEMON 'run fsmonitor-daemon in virtual repo' '
+ test_when_finished "rm -rf ./fake-virtual-clone actual" &&
+ git init fake-virtual-clone &&
+ test_must_fail git -C ./fake-virtual-clone \
+ -c core.virtualfilesystem=true \
+ fsmonitor--daemon run 2>actual &&
+ grep "virtual repository .* is incompatible with fsmonitor" actual
+'
+
+test_expect_success 'setup' '
+ : >tracked &&
+ : >modified &&
+ mkdir dir1 &&
+ : >dir1/tracked &&
+ : >dir1/modified &&
+ mkdir dir2 &&
+ : >dir2/tracked &&
+ : >dir2/modified &&
+ git -c core.fsmonitor= add . &&
+ git -c core.fsmonitor= commit -m initial &&
+ git config core.fsmonitor .git/hooks/fsmonitor-test &&
+ cat >.gitignore <<-\EOF
+ .gitignore
+ expect*
+ actual*
+ marker*
+ trace2*
+ EOF
+'
+
+# test that the fsmonitor extension is off by default
+test_expect_success 'fsmonitor extension is off by default' '
+ test-tool dump-fsmonitor >actual &&
+ grep "^no fsmonitor" actual
+'
+
+# test that "update-index --fsmonitor" adds the fsmonitor extension
+test_expect_success 'update-index --fsmonitor" adds the fsmonitor extension' '
+ git update-index --fsmonitor &&
+ test-tool dump-fsmonitor >actual &&
+ grep "^fsmonitor last update" actual
+'
+
+# test that "update-index --no-fsmonitor" removes the fsmonitor extension
+test_expect_success 'update-index --no-fsmonitor" removes the fsmonitor extension' '
+ git update-index --no-fsmonitor &&
+ test-tool dump-fsmonitor >actual &&
+ grep "^no fsmonitor" actual
+'
+
+cat >expect <<EOF &&
+h dir1/modified
+H dir1/tracked
+h dir2/modified
+H dir2/tracked
+h modified
+H tracked
+EOF
+
+# test that "update-index --fsmonitor-valid" sets the fsmonitor valid bit
+test_expect_success 'update-index --fsmonitor-valid" sets the fsmonitor valid bit' '
+ test_hook fsmonitor-test<<-\EOF &&
+ printf "last_update_token\0"
+ EOF
+ git update-index --fsmonitor &&
+ git update-index --fsmonitor-valid dir1/modified &&
+ git update-index --fsmonitor-valid dir2/modified &&
+ git update-index --fsmonitor-valid modified &&
+ git ls-files -f >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<EOF &&
+H dir1/modified
+H dir1/tracked
+H dir2/modified
+H dir2/tracked
+H modified
+H tracked
+EOF
+
+# test that "update-index --no-fsmonitor-valid" clears the fsmonitor valid bit
+test_expect_success 'update-index --no-fsmonitor-valid" clears the fsmonitor valid bit' '
+ git update-index --no-fsmonitor-valid dir1/modified &&
+ git update-index --no-fsmonitor-valid dir2/modified &&
+ git update-index --no-fsmonitor-valid modified &&
+ git ls-files -f >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<EOF &&
+H dir1/modified
+H dir1/tracked
+H dir2/modified
+H dir2/tracked
+H modified
+H tracked
+EOF
+
+# test that all files returned by the script get flagged as invalid
+test_expect_success 'all files returned by integration script get flagged as invalid' '
+ write_integration_script &&
+ dirty_repo &&
+ git update-index --fsmonitor &&
+ git ls-files -f >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<EOF &&
+H dir1/modified
+h dir1/new
+H dir1/tracked
+H dir2/modified
+h dir2/new
+H dir2/tracked
+H modified
+h new
+H tracked
+EOF
+
+# test that newly added files are marked valid
+test_expect_success 'newly added files are marked valid' '
+ test_hook --setup --clobber fsmonitor-test<<-\EOF &&
+ printf "last_update_token\0"
+ EOF
+ git add new &&
+ git add dir1/new &&
+ git add dir2/new &&
+ git ls-files -f >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<EOF &&
+H dir1/modified
+h dir1/new
+h dir1/tracked
+H dir2/modified
+h dir2/new
+h dir2/tracked
+H modified
+h new
+h tracked
+EOF
+
+# test that all unmodified files get marked valid
+test_expect_success 'all unmodified files get marked valid' '
+ # modified files result in update-index returning 1
+ test_must_fail git update-index --refresh --force-write-index &&
+ git ls-files -f >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<EOF &&
+H dir1/modified
+h dir1/tracked
+h dir2/modified
+h dir2/tracked
+h modified
+h tracked
+EOF
+
+# test that *only* files returned by the integration script get flagged as invalid
+test_expect_success '*only* files returned by the integration script get flagged as invalid' '
+ test_hook --clobber fsmonitor-test<<-\EOF &&
+ printf "last_update_token\0"
+ printf "dir1/modified\0"
+ EOF
+ clean_repo &&
+ git update-index --refresh --force-write-index &&
+ echo 1 >modified &&
+ echo 2 >dir1/modified &&
+ echo 3 >dir2/modified &&
+ test_must_fail git update-index --refresh --force-write-index &&
+ git ls-files -f >actual &&
+ test_cmp expect actual
+'
+
+# Ensure commands that call refresh_index() to move the index back in time
+# properly invalidate the fsmonitor cache
+test_expect_success 'refresh_index() invalidates fsmonitor cache' '
+ clean_repo &&
+ dirty_repo &&
+ write_integration_script &&
+ git add . &&
+ test_hook --clobber fsmonitor-test<<-\EOF &&
+ EOF
+ git commit -m "to reset" &&
+ git reset HEAD~1 &&
+ git status >actual &&
+ git -c core.fsmonitor= status >expect &&
+ test_cmp expect actual
+'
+
+# test fsmonitor with and without preloadIndex
+preload_values="false true"
+for preload_val in $preload_values
+do
+ test_expect_success "setup preloadIndex to $preload_val" '
+ git config core.preloadIndex $preload_val &&
+ if test $preload_val = true
+ then
+ GIT_TEST_PRELOAD_INDEX=$preload_val && export GIT_TEST_PRELOAD_INDEX
+ else
+ sane_unset GIT_TEST_PRELOAD_INDEX
+ fi
+ '
+
+ # test fsmonitor with and without the untracked cache (if available)
+ uc_values="false"
+ test_have_prereq UNTRACKED_CACHE && uc_values="false true"
+ for uc_val in $uc_values
+ do
+ test_expect_success "setup untracked cache to $uc_val" '
+ git config core.untrackedcache $uc_val
+ '
+
+ # Status is well tested elsewhere so we'll just ensure that the results are
+ # the same when using core.fsmonitor.
+ test_expect_success 'compare status with and without fsmonitor' '
+ write_integration_script &&
+ clean_repo &&
+ dirty_repo &&
+ git add new &&
+ git add dir1/new &&
+ git add dir2/new &&
+ git status >actual &&
+ git -c core.fsmonitor= status >expect &&
+ test_cmp expect actual
+ '
+
+ # Make sure it's actually skipping the check for modified and untracked
+ # (if enabled) files unless it is told about them.
+ test_expect_success "status doesn't detect unreported modifications" '
+ test_hook --clobber fsmonitor-test<<-\EOF &&
+ printf "last_update_token\0"
+ :>marker
+ EOF
+ clean_repo &&
+ git status &&
+ test_path_is_file marker &&
+ dirty_repo &&
+ rm -f marker &&
+ git status >actual &&
+ test_path_is_file marker &&
+ test_grep ! "Changes not staged for commit:" actual &&
+ if test $uc_val = true
+ then
+ test_grep ! "Untracked files:" actual
+ fi &&
+ if test $uc_val = false
+ then
+ test_grep "Untracked files:" actual
+ fi &&
+ rm -f marker
+ '
+ done
+done
+
+# test that splitting the index doesn't interfere
+test_expect_success 'splitting the index results in the same state' '
+ write_integration_script &&
+ dirty_repo &&
+ git update-index --fsmonitor &&
+ git ls-files -f >expect &&
+ test-tool dump-fsmonitor >&2 && echo &&
+ git update-index --fsmonitor --split-index &&
+ test-tool dump-fsmonitor >&2 && echo &&
+ git ls-files -f >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success UNTRACKED_CACHE 'ignore .git changes when invalidating UNTR' '
+ test_create_repo dot-git &&
+ (
+ cd dot-git &&
+ : >tracked &&
+ test-tool chmtime =-60 tracked &&
+ : >modified &&
+ test-tool chmtime =-60 modified &&
+ mkdir dir1 &&
+ : >dir1/tracked &&
+ test-tool chmtime =-60 dir1/tracked &&
+ : >dir1/modified &&
+ test-tool chmtime =-60 dir1/modified &&
+ mkdir dir2 &&
+ : >dir2/tracked &&
+ test-tool chmtime =-60 dir2/tracked &&
+ : >dir2/modified &&
+ test-tool chmtime =-60 dir2/modified &&
+ write_integration_script &&
+ git config core.fsmonitor .git/hooks/fsmonitor-test &&
+ git update-index --untracked-cache &&
+ git update-index --fsmonitor &&
+ git status &&
+ GIT_TRACE2_PERF="$TRASH_DIRECTORY/trace-before" \
+ git status &&
+ test-tool dump-untracked-cache >../before
+ ) &&
+ cat >>dot-git/.git/hooks/fsmonitor-test <<-\EOF &&
+ printf ".git\0"
+ printf ".git/index\0"
+ printf "dir1/.git\0"
+ printf "dir1/.git/index\0"
+ EOF
+ (
+ cd dot-git &&
+ GIT_TRACE2_PERF="$TRASH_DIRECTORY/trace-after" \
+ git status &&
+ test-tool dump-untracked-cache >../after
+ ) &&
+ grep "directory-invalidation" trace-before | cut -d"|" -f 9 >>before &&
+ grep "directory-invalidation" trace-after | cut -d"|" -f 9 >>after &&
+ # UNTR extension unchanged, dir invalidation count unchanged
+ test_cmp before after
+'
+
+test_expect_success 'discard_index() also discards fsmonitor info' '
+ test_config core.fsmonitor "$TEST_DIRECTORY/t7519/fsmonitor-all" &&
+ test_might_fail git update-index --refresh &&
+ test-tool read-cache --print-and-refresh=tracked 2 >actual &&
+ printf "tracked is%s up to date\n" "" " not" >expect &&
+ test_cmp expect actual
+'
+
+# Test unstaging entries that:
+# - Are not flagged with CE_FSMONITOR_VALID
+# - Have a position in the index >= the number of entries present in the index
+# after unstaging.
+test_expect_success 'status succeeds after staging/unstaging' '
+ test_create_repo fsmonitor-stage-unstage &&
+ (
+ cd fsmonitor-stage-unstage &&
+ test_commit initial &&
+ git update-index --fsmonitor &&
+ removed=$(test_seq 1 100 | sed "s/^/z/") &&
+ touch $removed &&
+ git add $removed &&
+ git config core.fsmonitor "$TEST_DIRECTORY/t7519/fsmonitor-env" &&
+ FSMONITOR_LIST="$removed" git restore -S $removed &&
+ FSMONITOR_LIST="$removed" git status
+ )
+'
+
+# Usage:
+# check_sparse_index_behavior [!]
+# If "!" is supplied, then we verify that we do not call ensure_full_index
+# during a call to 'git status'. Otherwise, we verify that we _do_ call it.
+check_sparse_index_behavior () {
+ git -C full status --porcelain=v2 >expect &&
+ GIT_TRACE2_EVENT="$(pwd)/trace2.txt" \
+ git -C sparse status --porcelain=v2 >actual &&
+ test_region $1 index ensure_full_index trace2.txt &&
+ test_region fsm_hook query trace2.txt &&
+ test_cmp expect actual &&
+ rm trace2.txt
+}
+
+test_expect_success 'status succeeds with sparse index' '
+ (
+ sane_unset GIT_TEST_SPLIT_INDEX &&
+
+ git clone . full &&
+ git clone --sparse . sparse &&
+ git -C sparse sparse-checkout init --cone --sparse-index &&
+ git -C sparse sparse-checkout set dir1 dir2 &&
+
+ test_hook --clobber fsmonitor-test <<-\EOF &&
+ printf "last_update_token\0"
+ EOF
+ git -C full config core.fsmonitor ../.git/hooks/fsmonitor-test &&
+ git -C sparse config core.fsmonitor ../.git/hooks/fsmonitor-test &&
+ check_sparse_index_behavior ! &&
+
+ test_hook --clobber fsmonitor-test <<-\EOF &&
+ printf "last_update_token\0"
+ printf "dir1/modified\0"
+ EOF
+ check_sparse_index_behavior ! &&
+
+ git -C sparse sparse-checkout add dir1a &&
+
+ for repo in full sparse
+ do
+ cp -r $repo/dir1 $repo/dir1a &&
+ git -C $repo add dir1a &&
+ git -C $repo commit -m "add dir1a" || return 1
+ done &&
+ git -C sparse sparse-checkout set dir1 dir2 &&
+
+ # This one modifies outside the sparse-checkout definition
+ # and hence we expect to expand the sparse-index.
+ test_hook --clobber fsmonitor-test <<-\EOF &&
+ printf "last_update_token\0"
+ printf "dir1a/modified\0"
+ EOF
+ check_sparse_index_behavior
+ )
+'
+
+test_done
diff --git a/t/t7519/fsmonitor-all b/t/t7519/fsmonitor-all
new file mode 100755
index 0000000..94ab66b
--- /dev/null
+++ b/t/t7519/fsmonitor-all
@@ -0,0 +1,23 @@
+#!/bin/sh
+#
+# An test hook script to integrate with git to test fsmonitor.
+#
+# The hook is passed a version (currently 1) and a time in nanoseconds
+# formatted as a string and outputs to stdout all files that have been
+# modified since the given time. Paths must be relative to the root of
+# the working tree and separated by a single NUL.
+#
+#echo "$0 $*" >&2
+
+if test "$#" -ne 2
+then
+ echo "$0: exactly 2 arguments expected" >&2
+ exit 2
+fi
+
+if test "$1" != 1
+then
+ exit 1
+fi
+
+echo "/"
diff --git a/t/t7519/fsmonitor-all-v2 b/t/t7519/fsmonitor-all-v2
new file mode 100755
index 0000000..061907e
--- /dev/null
+++ b/t/t7519/fsmonitor-all-v2
@@ -0,0 +1,21 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+#
+# An test hook script to integrate with git to test fsmonitor.
+#
+# The hook is passed a version (currently 2) and since token
+# formatted as a string and outputs to stdout all files that have been
+# modified since the given time. Paths must be relative to the root of
+# the working tree and separated by a single NUL.
+#
+#echo "$0 $*" >&2
+my ($version, $last_update_token) = @ARGV;
+
+if ($version ne 2) {
+ print "Unsupported query-fsmonitor hook version '$version'.\n";
+ exit 1;
+}
+
+print "last_update_token\0/\0"
diff --git a/t/t7519/fsmonitor-env b/t/t7519/fsmonitor-env
new file mode 100755
index 0000000..8f1f7ab
--- /dev/null
+++ b/t/t7519/fsmonitor-env
@@ -0,0 +1,24 @@
+#!/bin/sh
+#
+# An test hook script to integrate with git to test fsmonitor.
+#
+# The hook is passed a version (currently 1) and a time in nanoseconds
+# formatted as a string and outputs to stdout all files that have been
+# modified since the given time. Paths must be relative to the root of
+# the working tree and separated by a single NUL.
+#
+#echo "$0 $*" >&2
+
+if test "$#" -ne 2
+then
+ echo "$0: exactly 2 arguments expected" >&2
+ exit 2
+fi
+
+if test "$1" != 1
+then
+ echo "Unsupported core.fsmonitor hook version." >&2
+ exit 1
+fi
+
+printf '%s\n' $FSMONITOR_LIST
diff --git a/t/t7519/fsmonitor-none b/t/t7519/fsmonitor-none
new file mode 100755
index 0000000..ed9cf5a
--- /dev/null
+++ b/t/t7519/fsmonitor-none
@@ -0,0 +1,22 @@
+#!/bin/sh
+#
+# An test hook script to integrate with git to test fsmonitor.
+#
+# The hook is passed a version (currently 1) and a time in nanoseconds
+# formatted as a string and outputs to stdout all files that have been
+# modified since the given time. Paths must be relative to the root of
+# the working tree and separated by a single NUL.
+#
+#echo "$0 $*" >&2
+
+if test "$#" -ne 2
+then
+ echo "$0: exactly 2 arguments expected" >&2
+ exit 2
+fi
+
+if test "$1" != 1
+then
+ echo "Unsupported core.fsmonitor hook version." >&2
+ exit 1
+fi
diff --git a/t/t7519/fsmonitor-watchman b/t/t7519/fsmonitor-watchman
new file mode 100755
index 0000000..264b9da
--- /dev/null
+++ b/t/t7519/fsmonitor-watchman
@@ -0,0 +1,127 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use IPC::Open2;
+
+# An example hook script to integrate Watchman
+# (https://facebook.github.io/watchman/) with git to speed up detecting
+# new and modified files.
+#
+# The hook is passed a version (currently 1) and a time in nanoseconds
+# formatted as a string and outputs to stdout all files that have been
+# modified since the given time. Paths must be relative to the root of
+# the working tree and separated by a single NUL.
+#
+# To enable this hook, rename this file to "query-watchman" and set
+# 'git config core.fsmonitor .git/hooks/query-watchman'
+#
+my ($version, $time) = @ARGV;
+#print STDERR "$0 $version $time\n";
+
+# Check the hook interface version
+
+if ($version == 1) {
+ # convert nanoseconds to seconds
+ # subtract one second to make sure watchman will return all changes
+ $time = int ($time / 1000000000) - 1;
+} else {
+ exit 1;
+}
+
+my $git_work_tree;
+if ($^O =~ 'msys' || $^O =~ 'cygwin') {
+ $git_work_tree = Win32::GetCwd();
+ $git_work_tree =~ tr/\\/\//;
+} else {
+ require Cwd;
+ $git_work_tree = Cwd::cwd();
+}
+
+my $retry = 1;
+
+launch_watchman();
+
+sub launch_watchman {
+
+ my $pid = open2(\*CHLD_OUT, \*CHLD_IN, 'watchman -j')
+ or die "open2() failed: $!\n" .
+ "Falling back to scanning...\n";
+
+ # In the query expression below we're asking for names of files that
+ # changed since $time but were not transient (ie created after
+ # $time but no longer exist).
+ #
+ # To accomplish this, we're using the "since" generator to use the
+ # recency index to select candidate nodes and "fields" to limit the
+ # output to file names only.
+
+ my $query = <<" END";
+ ["query", "$git_work_tree", {
+ "since": $time,
+ "fields": ["name"]
+ }]
+ END
+
+ open (my $fh, ">", ".git/watchman-query.json");
+ print $fh $query;
+ close $fh;
+
+ print CHLD_IN $query;
+ close CHLD_IN;
+ my $response = do {local $/; <CHLD_OUT>};
+
+ open ($fh, ">", ".git/watchman-response.json");
+ print $fh $response;
+ close $fh;
+
+ die "Watchman: command returned no output.\n" .
+ "Falling back to scanning...\n" if $response eq "";
+ die "Watchman: command returned invalid output: $response\n" .
+ "Falling back to scanning...\n" unless $response =~ /^\{/;
+
+ my $json_pkg;
+ eval {
+ require JSON::XS;
+ $json_pkg = "JSON::XS";
+ 1;
+ } or do {
+ require JSON::PP;
+ $json_pkg = "JSON::PP";
+ };
+
+ my $o = $json_pkg->new->utf8->decode($response);
+
+ if ($retry > 0 and $o->{error} and $o->{error} =~ m/unable to resolve root .* directory (.*) is not watched/) {
+ print STDERR "Adding '$git_work_tree' to watchman's watch list.\n";
+ $retry--;
+ qx/watchman watch "$git_work_tree"/;
+ die "Failed to make watchman watch '$git_work_tree'.\n" .
+ "Falling back to scanning...\n" if $? != 0;
+
+ # Watchman will always return all files on the first query so
+ # return the fast "everything is dirty" flag to git and do the
+ # Watchman query just to get it over with now so we won't pay
+ # the cost in git to look up each individual file.
+
+ open ($fh, ">", ".git/watchman-output.out");
+ print "/\0";
+ close $fh;
+
+ print "/\0";
+ eval { launch_watchman() };
+ exit 0;
+ }
+
+ die "Watchman: $o->{error}.\n" .
+ "Falling back to scanning...\n" if $o->{error};
+
+ open ($fh, ">", ".git/watchman-output.out");
+ binmode $fh, ":utf8";
+ print $fh @{$o->{files}};
+ close $fh;
+
+ binmode STDOUT, ":utf8";
+ local $, = "\0";
+ print @{$o->{files}};
+}
diff --git a/t/t7519/fsmonitor-watchman-v2 b/t/t7519/fsmonitor-watchman-v2
new file mode 100755
index 0000000..14ed0aa
--- /dev/null
+++ b/t/t7519/fsmonitor-watchman-v2
@@ -0,0 +1,173 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use IPC::Open2;
+
+# An example hook script to integrate Watchman
+# (https://facebook.github.io/watchman/) with git to speed up detecting
+# new and modified files.
+#
+# The hook is passed a version (currently 2) and last update token
+# formatted as a string and outputs to stdout a new update token and
+# all files that have been modified since the update token. Paths must
+# be relative to the root of the working tree and separated by a single NUL.
+#
+# To enable this hook, rename this file to "query-watchman" and set
+# 'git config core.fsmonitor .git/hooks/query-watchman'
+#
+my ($version, $last_update_token) = @ARGV;
+
+# Uncomment for debugging
+# print STDERR "$0 $version $last_update_token\n";
+
+# Check the hook interface version
+if ($version ne 2) {
+ die "Unsupported query-fsmonitor hook version '$version'.\n" .
+ "Falling back to scanning...\n";
+}
+
+my $git_work_tree = get_working_dir();
+
+my $retry = 1;
+
+my $json_pkg;
+eval {
+ require JSON::XS;
+ $json_pkg = "JSON::XS";
+ 1;
+} or do {
+ require JSON::PP;
+ $json_pkg = "JSON::PP";
+};
+
+launch_watchman();
+
+sub launch_watchman {
+ my $o = watchman_query();
+ if (is_work_tree_watched($o)) {
+ output_result($o->{clock}, @{$o->{files}});
+ }
+}
+
+sub output_result {
+ my ($clockid, @files) = @_;
+
+ # Uncomment for debugging watchman output
+ # open (my $fh, ">", ".git/watchman-output.out");
+ # binmode $fh, ":utf8";
+ # print $fh "$clockid\n@files\n";
+ # close $fh;
+
+ binmode STDOUT, ":utf8";
+ print $clockid;
+ print "\0";
+ local $, = "\0";
+ print @files;
+}
+
+sub watchman_clock {
+ my $response = qx/watchman clock "$git_work_tree"/;
+ die "Failed to get clock id on '$git_work_tree'.\n" .
+ "Falling back to scanning...\n" if $? != 0;
+
+ return $json_pkg->new->utf8->decode($response);
+}
+
+sub watchman_query {
+ my $pid = open2(\*CHLD_OUT, \*CHLD_IN, 'watchman -j --no-pretty')
+ or die "open2() failed: $!\n" .
+ "Falling back to scanning...\n";
+
+ # In the query expression below we're asking for names of files that
+ # changed since $last_update_token but not from the .git folder.
+ #
+ # To accomplish this, we're using the "since" generator to use the
+ # recency index to select candidate nodes and "fields" to limit the
+ # output to file names only. Then we're using the "expression" term to
+ # further constrain the results.
+ if (substr($last_update_token, 0, 1) eq "c") {
+ $last_update_token = "\"$last_update_token\"";
+ }
+ my $query = <<" END";
+ ["query", "$git_work_tree", {
+ "since": $last_update_token,
+ "fields": ["name"],
+ "expression": ["not", ["dirname", ".git"]]
+ }]
+ END
+
+ # Uncomment for debugging the watchman query
+ # open (my $fh, ">", ".git/watchman-query.json");
+ # print $fh $query;
+ # close $fh;
+
+ print CHLD_IN $query;
+ close CHLD_IN;
+ my $response = do {local $/; <CHLD_OUT>};
+
+ # Uncomment for debugging the watch response
+ # open ($fh, ">", ".git/watchman-response.json");
+ # print $fh $response;
+ # close $fh;
+
+ die "Watchman: command returned no output.\n" .
+ "Falling back to scanning...\n" if $response eq "";
+ die "Watchman: command returned invalid output: $response\n" .
+ "Falling back to scanning...\n" unless $response =~ /^\{/;
+
+ return $json_pkg->new->utf8->decode($response);
+}
+
+sub is_work_tree_watched {
+ my ($output) = @_;
+ my $error = $output->{error};
+ if ($retry > 0 and $error and $error =~ m/unable to resolve root .* directory (.*) is not watched/) {
+ $retry--;
+ my $response = qx/watchman watch "$git_work_tree"/;
+ die "Failed to make watchman watch '$git_work_tree'.\n" .
+ "Falling back to scanning...\n" if $? != 0;
+ $output = $json_pkg->new->utf8->decode($response);
+ $error = $output->{error};
+ die "Watchman: $error.\n" .
+ "Falling back to scanning...\n" if $error;
+
+ # Uncomment for debugging watchman output
+ # open (my $fh, ">", ".git/watchman-output.out");
+ # close $fh;
+
+ # Watchman will always return all files on the first query so
+ # return the fast "everything is dirty" flag to git and do the
+ # Watchman query just to get it over with now so we won't pay
+ # the cost in git to look up each individual file.
+ my $o = watchman_clock();
+ $error = $output->{error};
+
+ die "Watchman: $error.\n" .
+ "Falling back to scanning...\n" if $error;
+
+ output_result($o->{clock}, ("/"));
+ $last_update_token = $o->{clock};
+
+ eval { launch_watchman() };
+ return 0;
+ }
+
+ die "Watchman: $error.\n" .
+ "Falling back to scanning...\n" if $error;
+
+ return 1;
+}
+
+sub get_working_dir {
+ my $working_dir;
+ if ($^O =~ 'msys' || $^O =~ 'cygwin') {
+ $working_dir = Win32::GetCwd();
+ $working_dir =~ tr/\\/\//;
+ } else {
+ require Cwd;
+ $working_dir = Cwd::cwd();
+ }
+
+ return $working_dir;
+}
diff --git a/t/t7520-ignored-hook-warning.sh b/t/t7520-ignored-hook-warning.sh
new file mode 100755
index 0000000..3b63c34
--- /dev/null
+++ b/t/t7520-ignored-hook-warning.sh
@@ -0,0 +1,39 @@
+#!/bin/sh
+
+test_description='ignored hook warning'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ test_hook --setup pre-commit <<-\EOF
+ exit 0
+ EOF
+'
+
+test_expect_success 'no warning if hook is not ignored' '
+ git commit --allow-empty -m "more" 2>message &&
+ test_grep ! -e "hook was ignored" message
+'
+
+test_expect_success POSIXPERM 'warning if hook is ignored' '
+ test_hook --disable pre-commit &&
+ git commit --allow-empty -m "even more" 2>message &&
+ test_grep -e "hook was ignored" message
+'
+
+test_expect_success POSIXPERM 'no warning if advice.ignoredHook set to false' '
+ test_config advice.ignoredHook false &&
+ test_hook --disable pre-commit &&
+ git commit --allow-empty -m "even more" 2>message &&
+ test_grep ! -e "hook was ignored" message
+'
+
+test_expect_success 'no warning if unset advice.ignoredHook and hook removed' '
+ test_hook --remove pre-commit &&
+ test_unconfig advice.ignoredHook &&
+ git commit --allow-empty -m "even more" 2>message &&
+ test_grep ! -e "hook was ignored" message
+'
+
+test_done
diff --git a/t/t7521-ignored-mode.sh b/t/t7521-ignored-mode.sh
new file mode 100755
index 0000000..a88b02b
--- /dev/null
+++ b/t/t7521-ignored-mode.sh
@@ -0,0 +1,233 @@
+#!/bin/sh
+
+test_description='git status ignored modes'
+
+. ./test-lib.sh
+
+test_expect_success 'setup initial commit and ignore file' '
+ cat >.gitignore <<-\EOF &&
+ *.ign
+ ignored_dir/
+ !*.unignore
+ EOF
+ git add . &&
+ git commit -m "Initial commit"
+'
+
+test_expect_success 'Verify behavior of status on directories with ignored files' '
+ test_when_finished "git clean -fdx" &&
+ cat >expect <<-\EOF &&
+ ? expect
+ ? output
+ ! dir/ignored/ignored_1.ign
+ ! dir/ignored/ignored_2.ign
+ ! ignored/ignored_1.ign
+ ! ignored/ignored_2.ign
+ EOF
+
+ mkdir -p ignored dir/ignored &&
+ touch ignored/ignored_1.ign ignored/ignored_2.ign \
+ dir/ignored/ignored_1.ign dir/ignored/ignored_2.ign &&
+
+ git status --porcelain=v2 --ignored=matching --untracked-files=all >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'Verify status behavior on directory with tracked & ignored files' '
+ test_when_finished "git clean -fdx && git reset HEAD~1 --hard" &&
+ cat >expect <<-\EOF &&
+ ? expect
+ ? output
+ ! dir/tracked_ignored/ignored_1.ign
+ ! dir/tracked_ignored/ignored_2.ign
+ ! tracked_ignored/ignored_1.ign
+ ! tracked_ignored/ignored_2.ign
+ EOF
+
+ mkdir -p tracked_ignored dir/tracked_ignored &&
+ touch tracked_ignored/tracked_1 tracked_ignored/tracked_2 \
+ tracked_ignored/ignored_1.ign tracked_ignored/ignored_2.ign \
+ dir/tracked_ignored/tracked_1 dir/tracked_ignored/tracked_2 \
+ dir/tracked_ignored/ignored_1.ign dir/tracked_ignored/ignored_2.ign &&
+
+ git add tracked_ignored/tracked_1 tracked_ignored/tracked_2 \
+ dir/tracked_ignored/tracked_1 dir/tracked_ignored/tracked_2 &&
+ git commit -m "commit tracked files" &&
+
+ git status --porcelain=v2 --ignored=matching --untracked-files=all >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'Verify status behavior on directory with untracked and ignored files' '
+ test_when_finished "git clean -fdx" &&
+ cat >expect <<-\EOF &&
+ ? dir/untracked_ignored/untracked_1
+ ? dir/untracked_ignored/untracked_2
+ ? expect
+ ? output
+ ? untracked_ignored/untracked_1
+ ? untracked_ignored/untracked_2
+ ! dir/untracked_ignored/ignored_1.ign
+ ! dir/untracked_ignored/ignored_2.ign
+ ! untracked_ignored/ignored_1.ign
+ ! untracked_ignored/ignored_2.ign
+ EOF
+
+ mkdir -p untracked_ignored dir/untracked_ignored &&
+ touch untracked_ignored/untracked_1 untracked_ignored/untracked_2 \
+ untracked_ignored/ignored_1.ign untracked_ignored/ignored_2.ign \
+ dir/untracked_ignored/untracked_1 dir/untracked_ignored/untracked_2 \
+ dir/untracked_ignored/ignored_1.ign dir/untracked_ignored/ignored_2.ign &&
+
+ git status --porcelain=v2 --ignored=matching --untracked-files=all >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'Verify status matching ignored files on ignored directory' '
+ test_when_finished "git clean -fdx" &&
+ cat >expect <<-\EOF &&
+ ? expect
+ ? output
+ ! ignored_dir/
+ EOF
+
+ mkdir ignored_dir &&
+ touch ignored_dir/ignored_1 ignored_dir/ignored_2 \
+ ignored_dir/ignored_1.ign ignored_dir/ignored_2.ign &&
+
+ git status --porcelain=v2 --ignored=matching --untracked-files=all >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'Verify status behavior on ignored directory containing tracked file' '
+ test_when_finished "git clean -fdx && git reset HEAD~1 --hard" &&
+ cat >expect <<-\EOF &&
+ ? expect
+ ? output
+ ! ignored_dir/ignored_1
+ ! ignored_dir/ignored_1.ign
+ ! ignored_dir/ignored_2
+ ! ignored_dir/ignored_2.ign
+ EOF
+
+ mkdir ignored_dir &&
+ touch ignored_dir/ignored_1 ignored_dir/ignored_2 \
+ ignored_dir/ignored_1.ign ignored_dir/ignored_2.ign \
+ ignored_dir/tracked &&
+ git add -f ignored_dir/tracked &&
+ git commit -m "Force add file in ignored directory" &&
+ git status --porcelain=v2 --ignored=matching --untracked-files=all >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'Verify matching ignored files with --untracked-files=normal' '
+ test_when_finished "git clean -fdx" &&
+ cat >expect <<-\EOF &&
+ ? expect
+ ? output
+ ? untracked_dir/
+ ! ignored_dir/
+ ! ignored_files/ignored_1.ign
+ ! ignored_files/ignored_2.ign
+ EOF
+
+ mkdir ignored_dir ignored_files untracked_dir &&
+ touch ignored_dir/ignored_1 ignored_dir/ignored_2 \
+ ignored_files/ignored_1.ign ignored_files/ignored_2.ign \
+ untracked_dir/untracked &&
+ git status --porcelain=v2 --ignored=matching --untracked-files=normal >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'Verify matching ignored files with --untracked-files=normal' '
+ test_when_finished "git clean -fdx" &&
+ cat >expect <<-\EOF &&
+ ? expect
+ ? output
+ ? untracked_dir/
+ ! ignored_dir/
+ ! ignored_files/ignored_1.ign
+ ! ignored_files/ignored_2.ign
+ EOF
+
+ mkdir ignored_dir ignored_files untracked_dir &&
+ touch ignored_dir/ignored_1 ignored_dir/ignored_2 \
+ ignored_files/ignored_1.ign ignored_files/ignored_2.ign \
+ untracked_dir/untracked &&
+ git status --porcelain=v2 --ignored=matching --untracked-files=normal >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'Verify status behavior on ignored directory containing tracked file' '
+ test_when_finished "git clean -fdx && git reset HEAD~1 --hard" &&
+ cat >expect <<-\EOF &&
+ ? expect
+ ? output
+ ! ignored_dir/ignored_1
+ ! ignored_dir/ignored_1.ign
+ ! ignored_dir/ignored_2
+ ! ignored_dir/ignored_2.ign
+ EOF
+
+ mkdir ignored_dir &&
+ touch ignored_dir/ignored_1 ignored_dir/ignored_2 \
+ ignored_dir/ignored_1.ign ignored_dir/ignored_2.ign \
+ ignored_dir/tracked &&
+ git add -f ignored_dir/tracked &&
+ git commit -m "Force add file in ignored directory" &&
+ git status --porcelain=v2 --ignored=matching --untracked-files=normal >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'Verify behavior of status with --ignored=no' '
+ test_when_finished "git clean -fdx" &&
+ cat >expect <<-\EOF &&
+ ? expect
+ ? output
+ EOF
+
+ mkdir -p ignored dir/ignored &&
+ touch ignored/ignored_1.ign ignored/ignored_2.ign \
+ dir/ignored/ignored_1.ign dir/ignored/ignored_2.ign &&
+
+ git status --porcelain=v2 --ignored=no --untracked-files=all >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'Verify behavior of status with --ignored=traditional and --untracked-files=all' '
+ test_when_finished "git clean -fdx" &&
+ cat >expect <<-\EOF &&
+ ? expect
+ ? output
+ ! dir/ignored/ignored_1.ign
+ ! dir/ignored/ignored_2.ign
+ ! ignored/ignored_1.ign
+ ! ignored/ignored_2.ign
+ EOF
+
+ mkdir -p ignored dir/ignored &&
+ touch ignored/ignored_1.ign ignored/ignored_2.ign \
+ dir/ignored/ignored_1.ign dir/ignored/ignored_2.ign &&
+
+ git status --porcelain=v2 --ignored=traditional --untracked-files=all >output &&
+ test_cmp expect output
+'
+
+test_expect_success 'Verify behavior of status with --ignored=traditional and --untracked-files=normal' '
+ test_when_finished "git clean -fdx" &&
+ cat >expect <<-\EOF &&
+ ? expect
+ ? output
+ ! dir/
+ ! ignored/
+ EOF
+
+ mkdir -p ignored dir/ignored &&
+ touch ignored/ignored_1.ign ignored/ignored_2.ign \
+ dir/ignored/ignored_1.ign dir/ignored/ignored_2.ign &&
+
+ git status --porcelain=v2 --ignored=traditional --untracked-files=normal >output &&
+ test_cmp expect output
+'
+
+test_done
diff --git a/t/t7524-commit-summary.sh b/t/t7524-commit-summary.sh
new file mode 100755
index 0000000..47b2f1d
--- /dev/null
+++ b/t/t7524-commit-summary.sh
@@ -0,0 +1,31 @@
+#!/bin/sh
+
+test_description='git commit summary'
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ test_seq 101 200 >file &&
+ git add file &&
+ git commit -m initial &&
+ git tag initial
+'
+
+test_expect_success 'commit summary ignores rewrites' '
+ git reset --hard initial &&
+ test_seq 200 300 >file &&
+
+ git diff --stat >diffstat &&
+ git diff --stat --break-rewrites >diffstatrewrite &&
+
+ # make sure this scenario is a detectable rewrite
+ ! test_cmp_bin diffstat diffstatrewrite &&
+
+ git add file &&
+ git commit -m second >actual &&
+
+ grep "1 file" <actual >actual.total &&
+ grep "1 file" <diffstat >diffstat.total &&
+ test_cmp diffstat.total actual.total
+'
+
+test_done
diff --git a/t/t7525-status-rename.sh b/t/t7525-status-rename.sh
new file mode 100755
index 0000000..a9210d3
--- /dev/null
+++ b/t/t7525-status-rename.sh
@@ -0,0 +1,114 @@
+#!/bin/sh
+
+test_description='git status rename detection options'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ echo 1 >original &&
+ git add . &&
+ git commit -m"Adding original file." &&
+ mv original renamed &&
+ echo 2 >> renamed &&
+ git add . &&
+ cat >.gitignore <<-\EOF
+ .gitignore
+ expect*
+ actual*
+ EOF
+'
+
+test_expect_success 'status no-options' '
+ git status >actual &&
+ test_grep "renamed:" actual
+'
+
+test_expect_success 'status --no-renames' '
+ git status --no-renames >actual &&
+ test_grep "deleted:" actual &&
+ test_grep "new file:" actual
+'
+
+test_expect_success 'status.renames inherits from diff.renames false' '
+ git -c diff.renames=false status >actual &&
+ test_grep "deleted:" actual &&
+ test_grep "new file:" actual
+'
+
+test_expect_success 'status.renames inherits from diff.renames true' '
+ git -c diff.renames=true status >actual &&
+ test_grep "renamed:" actual
+'
+
+test_expect_success 'status.renames overrides diff.renames false' '
+ git -c diff.renames=true -c status.renames=false status >actual &&
+ test_grep "deleted:" actual &&
+ test_grep "new file:" actual
+'
+
+test_expect_success 'status.renames overrides from diff.renames true' '
+ git -c diff.renames=false -c status.renames=true status >actual &&
+ test_grep "renamed:" actual
+'
+
+test_expect_success 'status status.renames=false' '
+ git -c status.renames=false status >actual &&
+ test_grep "deleted:" actual &&
+ test_grep "new file:" actual
+'
+
+test_expect_success 'status status.renames=true' '
+ git -c status.renames=true status >actual &&
+ test_grep "renamed:" actual
+'
+
+test_expect_success 'commit honors status.renames=false' '
+ git -c status.renames=false commit --dry-run >actual &&
+ test_grep "deleted:" actual &&
+ test_grep "new file:" actual
+'
+
+test_expect_success 'commit honors status.renames=true' '
+ git -c status.renames=true commit --dry-run >actual &&
+ test_grep "renamed:" actual
+'
+
+test_expect_success 'status config overridden' '
+ git -c status.renames=true status --no-renames >actual &&
+ test_grep "deleted:" actual &&
+ test_grep "new file:" actual
+'
+
+test_expect_success 'status score=100%' '
+ git status -M=100% >actual &&
+ test_grep "deleted:" actual &&
+ test_grep "new file:" actual &&
+
+ git status --find-renames=100% >actual &&
+ test_grep "deleted:" actual &&
+ test_grep "new file:" actual
+'
+
+test_expect_success 'status score=01%' '
+ git status -M=01% >actual &&
+ test_grep "renamed:" actual &&
+
+ git status --find-renames=01% >actual &&
+ test_grep "renamed:" actual
+'
+
+test_expect_success 'copies not overridden by find-renames' '
+ cp renamed copy &&
+ git add copy &&
+
+ git -c status.renames=copies status -M=01% >actual &&
+ test_grep "copied:" actual &&
+ test_grep "renamed:" actual &&
+
+ git -c status.renames=copies status --find-renames=01% >actual &&
+ test_grep "copied:" actual &&
+ test_grep "renamed:" actual
+'
+
+test_done
diff --git a/t/t7526-commit-pathspec-file.sh b/t/t7526-commit-pathspec-file.sh
new file mode 100755
index 0000000..c97c550
--- /dev/null
+++ b/t/t7526-commit-pathspec-file.sh
@@ -0,0 +1,165 @@
+#!/bin/sh
+
+test_description='commit --pathspec-from-file'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_tick
+
+test_expect_success setup '
+ test_commit file0 &&
+ git tag checkpoint &&
+
+ echo A >fileA.t &&
+ echo B >fileB.t &&
+ echo C >fileC.t &&
+ echo D >fileD.t &&
+ git add fileA.t fileB.t fileC.t fileD.t
+'
+
+restore_checkpoint () {
+ git reset --soft checkpoint
+}
+
+verify_expect () {
+ git diff-tree --no-commit-id --name-status -r HEAD >actual &&
+ test_cmp expect actual
+}
+
+test_expect_success '--pathspec-from-file from stdin' '
+ restore_checkpoint &&
+
+ echo fileA.t | git commit --pathspec-from-file=- -m "Commit" &&
+
+ cat >expect <<-\EOF &&
+ A fileA.t
+ EOF
+ verify_expect
+'
+
+test_expect_success '--pathspec-from-file from file' '
+ restore_checkpoint &&
+
+ echo fileA.t >list &&
+ git commit --pathspec-from-file=list -m "Commit" &&
+
+ cat >expect <<-\EOF &&
+ A fileA.t
+ EOF
+ verify_expect
+'
+
+test_expect_success 'NUL delimiters' '
+ restore_checkpoint &&
+
+ printf "fileA.t\0fileB.t\0" | git commit --pathspec-from-file=- --pathspec-file-nul -m "Commit" &&
+
+ cat >expect <<-\EOF &&
+ A fileA.t
+ A fileB.t
+ EOF
+ verify_expect
+'
+
+test_expect_success 'LF delimiters' '
+ restore_checkpoint &&
+
+ printf "fileA.t\nfileB.t\n" | git commit --pathspec-from-file=- -m "Commit" &&
+
+ cat >expect <<-\EOF &&
+ A fileA.t
+ A fileB.t
+ EOF
+ verify_expect
+'
+
+test_expect_success 'no trailing delimiter' '
+ restore_checkpoint &&
+
+ printf "fileA.t\nfileB.t" | git commit --pathspec-from-file=- -m "Commit" &&
+
+ cat >expect <<-\EOF &&
+ A fileA.t
+ A fileB.t
+ EOF
+ verify_expect
+'
+
+test_expect_success 'CRLF delimiters' '
+ restore_checkpoint &&
+
+ printf "fileA.t\r\nfileB.t\r\n" | git commit --pathspec-from-file=- -m "Commit" &&
+
+ cat >expect <<-\EOF &&
+ A fileA.t
+ A fileB.t
+ EOF
+ verify_expect
+'
+
+test_expect_success 'quotes' '
+ restore_checkpoint &&
+
+ cat >list <<-\EOF &&
+ "file\101.t"
+ EOF
+
+ git commit --pathspec-from-file=list -m "Commit" &&
+
+ cat >expect <<-\EOF &&
+ A fileA.t
+ EOF
+ verify_expect expect
+'
+
+test_expect_success 'quotes not compatible with --pathspec-file-nul' '
+ restore_checkpoint &&
+
+ cat >list <<-\EOF &&
+ "file\101.t"
+ EOF
+
+ test_must_fail git commit --pathspec-from-file=list --pathspec-file-nul -m "Commit"
+'
+
+test_expect_success 'only touches what was listed' '
+ restore_checkpoint &&
+
+ printf "fileB.t\nfileC.t\n" | git commit --pathspec-from-file=- -m "Commit" &&
+
+ cat >expect <<-\EOF &&
+ A fileB.t
+ A fileC.t
+ EOF
+ verify_expect
+'
+
+test_expect_success 'error conditions' '
+ restore_checkpoint &&
+ echo fileA.t >list &&
+ >empty_list &&
+
+ test_must_fail git commit --pathspec-from-file=list --interactive -m "Commit" 2>err &&
+ test_grep -e "options .--pathspec-from-file. and .--interactive/--patch. cannot be used together" err &&
+
+ test_must_fail git commit --pathspec-from-file=list --patch -m "Commit" 2>err &&
+ test_grep -e "options .--pathspec-from-file. and .--interactive/--patch. cannot be used together" err &&
+
+ test_must_fail git commit --pathspec-from-file=list --all -m "Commit" 2>err &&
+ test_grep -e "options .--pathspec-from-file. and .-a. cannot be used together" err &&
+
+ test_must_fail git commit --pathspec-from-file=list -m "Commit" -- fileA.t 2>err &&
+ test_grep -e ".--pathspec-from-file. and pathspec arguments cannot be used together" err &&
+
+ test_must_fail git commit --pathspec-file-nul -m "Commit" 2>err &&
+ test_grep -e "the option .--pathspec-file-nul. requires .--pathspec-from-file." err &&
+
+ test_must_fail git commit --pathspec-from-file=empty_list --include -m "Commit" 2>err &&
+ test_grep -e "No paths with --include/--only does not make sense." err &&
+
+ test_must_fail git commit --pathspec-from-file=empty_list --only -m "Commit" 2>err &&
+ test_grep -e "No paths with --include/--only does not make sense." err
+'
+
+test_done
diff --git a/t/t7527-builtin-fsmonitor.sh b/t/t7527-builtin-fsmonitor.sh
new file mode 100755
index 0000000..7850315
--- /dev/null
+++ b/t/t7527-builtin-fsmonitor.sh
@@ -0,0 +1,1040 @@
+#!/bin/sh
+
+test_description='built-in file system watcher'
+
+. ./test-lib.sh
+
+if ! test_have_prereq FSMONITOR_DAEMON
+then
+ skip_all="fsmonitor--daemon is not supported on this platform"
+ test_done
+fi
+
+stop_daemon_delete_repo () {
+ r=$1 &&
+ test_might_fail git -C $r fsmonitor--daemon stop &&
+ rm -rf $1
+}
+
+start_daemon () {
+ r= tf= t2= tk= &&
+
+ while test "$#" -ne 0
+ do
+ case "$1" in
+ -C)
+ r="-C ${2?}"
+ shift
+ ;;
+ --tf)
+ tf="${2?}"
+ shift
+ ;;
+ --t2)
+ t2="${2?}"
+ shift
+ ;;
+ --tk)
+ tk="${2?}"
+ shift
+ ;;
+ -*)
+ BUG "error: unknown option: '$1'"
+ ;;
+ *)
+ BUG "error: unbound argument: '$1'"
+ ;;
+ esac
+ shift
+ done &&
+
+ (
+ if test -n "$tf"
+ then
+ GIT_TRACE_FSMONITOR="$tf"
+ export GIT_TRACE_FSMONITOR
+ fi &&
+
+ if test -n "$t2"
+ then
+ GIT_TRACE2_PERF="$t2"
+ export GIT_TRACE2_PERF
+ fi &&
+
+ if test -n "$tk"
+ then
+ GIT_TEST_FSMONITOR_TOKEN="$tk"
+ export GIT_TEST_FSMONITOR_TOKEN
+ fi &&
+
+ git $r fsmonitor--daemon start &&
+ git $r fsmonitor--daemon status
+ )
+}
+
+# Is a Trace2 data event present with the given catetory and key?
+# We do not care what the value is.
+#
+have_t2_data_event () {
+ c=$1 &&
+ k=$2 &&
+
+ grep -e '"event":"data".*"category":"'"$c"'".*"key":"'"$k"'"'
+}
+
+test_expect_success 'explicit daemon start and stop' '
+ test_when_finished "stop_daemon_delete_repo test_explicit" &&
+
+ git init test_explicit &&
+ start_daemon -C test_explicit &&
+
+ git -C test_explicit fsmonitor--daemon stop &&
+ test_must_fail git -C test_explicit fsmonitor--daemon status
+'
+
+test_expect_success 'implicit daemon start' '
+ test_when_finished "stop_daemon_delete_repo test_implicit" &&
+
+ git init test_implicit &&
+ test_must_fail git -C test_implicit fsmonitor--daemon status &&
+
+ # query will implicitly start the daemon.
+ #
+ # for test-script simplicity, we send a V1 timestamp rather than
+ # a V2 token. either way, the daemon response to any query contains
+ # a new V2 token. (the daemon may complain that we sent a V1 request,
+ # but this test case is only concerned with whether the daemon was
+ # implicitly started.)
+
+ GIT_TRACE2_EVENT="$PWD/.git/trace" \
+ test-tool -C test_implicit fsmonitor-client query --token 0 >actual &&
+ nul_to_q <actual >actual.filtered &&
+ grep "builtin:" actual.filtered &&
+
+ # confirm that a daemon was started in the background.
+ #
+ # since the mechanism for starting the background daemon is platform
+ # dependent, just confirm that the foreground command received a
+ # response from the daemon.
+
+ have_t2_data_event fsm_client query/response-length <.git/trace &&
+
+ git -C test_implicit fsmonitor--daemon status &&
+ git -C test_implicit fsmonitor--daemon stop &&
+ test_must_fail git -C test_implicit fsmonitor--daemon status
+'
+
+# Verify that the daemon has shutdown. Spin a few seconds to
+# make the test a little more robust during CI testing.
+#
+# We're looking for an implicit shutdown, such as when we delete or
+# rename the ".git" directory. Our delete/rename will cause a file
+# system event that the daemon will see and the daemon will
+# auto-shutdown as soon as it sees it. But this is racy with our `git
+# fsmonitor--daemon status` commands (and we cannot use a cookie file
+# here to help us). So spin a little and give the daemon a chance to
+# see the event. (This is primarily for underpowered CI build/test
+# machines (where it might take a moment to wake and reschedule the
+# daemon process) to avoid false alarms during test runs.)
+#
+IMPLICIT_TIMEOUT=5
+
+verify_implicit_shutdown () {
+ r=$1 &&
+
+ k=0 &&
+ while test "$k" -lt $IMPLICIT_TIMEOUT
+ do
+ git -C $r fsmonitor--daemon status || return 0
+
+ sleep 1
+ k=$(( $k + 1 ))
+ done &&
+
+ return 1
+}
+
+test_expect_success 'implicit daemon stop (delete .git)' '
+ test_when_finished "stop_daemon_delete_repo test_implicit_1" &&
+
+ git init test_implicit_1 &&
+
+ start_daemon -C test_implicit_1 &&
+
+ # deleting the .git directory will implicitly stop the daemon.
+ rm -rf test_implicit_1/.git &&
+
+ # [1] Create an empty .git directory so that the following Git
+ # command will stay relative to the `-C` directory.
+ #
+ # Without this, the Git command will override the requested
+ # -C argument and crawl out to the containing Git source tree.
+ # This would make the test result dependent upon whether we
+ # were using fsmonitor on our development worktree.
+ #
+ mkdir test_implicit_1/.git &&
+
+ verify_implicit_shutdown test_implicit_1
+'
+
+test_expect_success 'implicit daemon stop (rename .git)' '
+ test_when_finished "stop_daemon_delete_repo test_implicit_2" &&
+
+ git init test_implicit_2 &&
+
+ start_daemon -C test_implicit_2 &&
+
+ # renaming the .git directory will implicitly stop the daemon.
+ mv test_implicit_2/.git test_implicit_2/.xxx &&
+
+ # See [1] above.
+ #
+ mkdir test_implicit_2/.git &&
+
+ verify_implicit_shutdown test_implicit_2
+'
+
+# File systems on Windows may or may not have shortnames.
+# This is a volume-specific setting on modern systems.
+# "C:/" drives are required to have them enabled. Other
+# hard drives default to disabled.
+#
+# This is a crude test to see if shortnames are enabled
+# on the volume containing the test directory. It is
+# crude, but it does not require elevation like `fsutil`.
+#
+test_lazy_prereq SHORTNAMES '
+ mkdir .foo &&
+ test -d "FOO~1"
+'
+
+# Here we assume that the shortname of ".git" is "GIT~1".
+test_expect_success MINGW,SHORTNAMES 'implicit daemon stop (rename GIT~1)' '
+ test_when_finished "stop_daemon_delete_repo test_implicit_1s" &&
+
+ git init test_implicit_1s &&
+
+ start_daemon -C test_implicit_1s &&
+
+ # renaming the .git directory will implicitly stop the daemon.
+ # this moves {.git, GIT~1} to {.gitxyz, GITXYZ~1}.
+ # the rename-from FS Event will contain the shortname.
+ #
+ mv test_implicit_1s/GIT~1 test_implicit_1s/.gitxyz &&
+
+ # See [1] above.
+ # this moves {.gitxyz, GITXYZ~1} to {.git, GIT~1}.
+ mv test_implicit_1s/.gitxyz test_implicit_1s/.git &&
+
+ verify_implicit_shutdown test_implicit_1s
+'
+
+# Here we first create a file with LONGNAME of "GIT~1" before
+# we create the repo. This will cause the shortname of ".git"
+# to be "GIT~2".
+test_expect_success MINGW,SHORTNAMES 'implicit daemon stop (rename GIT~2)' '
+ test_when_finished "stop_daemon_delete_repo test_implicit_1s2" &&
+
+ mkdir test_implicit_1s2 &&
+ echo HELLO >test_implicit_1s2/GIT~1 &&
+ git init test_implicit_1s2 &&
+
+ test_path_is_file test_implicit_1s2/GIT~1 &&
+ test_path_is_dir test_implicit_1s2/GIT~2 &&
+
+ start_daemon -C test_implicit_1s2 &&
+
+ # renaming the .git directory will implicitly stop the daemon.
+ # the rename-from FS Event will contain the shortname.
+ #
+ mv test_implicit_1s2/GIT~2 test_implicit_1s2/.gitxyz &&
+
+ # See [1] above.
+ mv test_implicit_1s2/.gitxyz test_implicit_1s2/.git &&
+
+ verify_implicit_shutdown test_implicit_1s2
+'
+
+test_expect_success 'cannot start multiple daemons' '
+ test_when_finished "stop_daemon_delete_repo test_multiple" &&
+
+ git init test_multiple &&
+
+ start_daemon -C test_multiple &&
+
+ test_must_fail git -C test_multiple fsmonitor--daemon start 2>actual &&
+ grep "fsmonitor--daemon is already running" actual &&
+
+ git -C test_multiple fsmonitor--daemon stop &&
+ test_must_fail git -C test_multiple fsmonitor--daemon status
+'
+
+# These tests use the main repo in the trash directory
+
+test_expect_success 'setup' '
+ >tracked &&
+ >modified &&
+ >delete &&
+ >rename &&
+ mkdir dir1 &&
+ >dir1/tracked &&
+ >dir1/modified &&
+ >dir1/delete &&
+ >dir1/rename &&
+ mkdir dir2 &&
+ >dir2/tracked &&
+ >dir2/modified &&
+ >dir2/delete &&
+ >dir2/rename &&
+ mkdir dirtorename &&
+ >dirtorename/a &&
+ >dirtorename/b &&
+
+ cat >.gitignore <<-\EOF &&
+ .gitignore
+ expect*
+ actual*
+ flush*
+ trace*
+ EOF
+
+ mkdir -p T1/T2/T3/T4 &&
+ echo 1 >T1/F1 &&
+ echo 1 >T1/T2/F1 &&
+ echo 1 >T1/T2/T3/F1 &&
+ echo 1 >T1/T2/T3/T4/F1 &&
+ echo 2 >T1/F2 &&
+ echo 2 >T1/T2/F2 &&
+ echo 2 >T1/T2/T3/F2 &&
+ echo 2 >T1/T2/T3/T4/F2 &&
+
+ git -c core.fsmonitor=false add . &&
+ test_tick &&
+ git -c core.fsmonitor=false commit -m initial &&
+
+ git config core.fsmonitor true
+'
+
+# The test already explicitly stopped (or tried to stop) the daemon.
+# This is here in case something else fails first.
+#
+redundant_stop_daemon () {
+ test_might_fail git fsmonitor--daemon stop
+}
+
+test_expect_success 'update-index implicitly starts daemon' '
+ test_when_finished redundant_stop_daemon &&
+
+ test_must_fail git fsmonitor--daemon status &&
+
+ GIT_TRACE2_EVENT="$PWD/.git/trace_implicit_1" \
+ git update-index --fsmonitor &&
+
+ git fsmonitor--daemon status &&
+ test_might_fail git fsmonitor--daemon stop &&
+
+ # Confirm that the trace2 log contains a record of the
+ # daemon starting.
+ test_subcommand git fsmonitor--daemon start <.git/trace_implicit_1
+'
+
+test_expect_success 'status implicitly starts daemon' '
+ test_when_finished redundant_stop_daemon &&
+
+ test_must_fail git fsmonitor--daemon status &&
+
+ GIT_TRACE2_EVENT="$PWD/.git/trace_implicit_2" \
+ git status >actual &&
+
+ git fsmonitor--daemon status &&
+ test_might_fail git fsmonitor--daemon stop &&
+
+ # Confirm that the trace2 log contains a record of the
+ # daemon starting.
+ test_subcommand git fsmonitor--daemon start <.git/trace_implicit_2
+'
+
+edit_files () {
+ echo 1 >modified &&
+ echo 2 >dir1/modified &&
+ echo 3 >dir2/modified &&
+ >dir1/untracked
+}
+
+delete_files () {
+ rm -f delete &&
+ rm -f dir1/delete &&
+ rm -f dir2/delete
+}
+
+create_files () {
+ echo 1 >new &&
+ echo 2 >dir1/new &&
+ echo 3 >dir2/new
+}
+
+rename_files () {
+ mv rename renamed &&
+ mv dir1/rename dir1/renamed &&
+ mv dir2/rename dir2/renamed
+}
+
+file_to_directory () {
+ rm -f delete &&
+ mkdir delete &&
+ echo 1 >delete/new
+}
+
+directory_to_file () {
+ rm -rf dir1 &&
+ echo 1 >dir1
+}
+
+move_directory_contents_deeper() {
+ mkdir T1/_new_ &&
+ mv T1/[A-Z]* T1/_new_
+}
+
+move_directory_up() {
+ mv T1/T2/T3 T1
+}
+
+move_directory() {
+ mv T1/T2/T3 T1/T2/NewT3
+}
+
+# The next few test cases confirm that our fsmonitor daemon sees each type
+# of OS filesystem notification that we care about. At this layer we just
+# ensure we are getting the OS notifications and do not try to confirm what
+# is reported by `git status`.
+#
+# We run a simple query after modifying the filesystem just to introduce
+# a bit of a delay so that the trace logging from the daemon has time to
+# get flushed to disk.
+#
+# We `reset` and `clean` at the bottom of each test (and before stopping the
+# daemon) because these commands might implicitly restart the daemon.
+
+clean_up_repo_and_stop_daemon () {
+ git reset --hard HEAD &&
+ git clean -fd &&
+ test_might_fail git fsmonitor--daemon stop &&
+ rm -f .git/trace
+}
+
+test_expect_success 'edit some files' '
+ test_when_finished clean_up_repo_and_stop_daemon &&
+
+ start_daemon --tf "$PWD/.git/trace" &&
+
+ edit_files &&
+
+ test-tool fsmonitor-client query --token 0 &&
+
+ grep "^event: dir1/modified$" .git/trace &&
+ grep "^event: dir2/modified$" .git/trace &&
+ grep "^event: modified$" .git/trace &&
+ grep "^event: dir1/untracked$" .git/trace
+'
+
+test_expect_success 'create some files' '
+ test_when_finished clean_up_repo_and_stop_daemon &&
+
+ start_daemon --tf "$PWD/.git/trace" &&
+
+ create_files &&
+
+ test-tool fsmonitor-client query --token 0 &&
+
+ grep "^event: dir1/new$" .git/trace &&
+ grep "^event: dir2/new$" .git/trace &&
+ grep "^event: new$" .git/trace
+'
+
+test_expect_success 'delete some files' '
+ test_when_finished clean_up_repo_and_stop_daemon &&
+
+ start_daemon --tf "$PWD/.git/trace" &&
+
+ delete_files &&
+
+ test-tool fsmonitor-client query --token 0 &&
+
+ grep "^event: dir1/delete$" .git/trace &&
+ grep "^event: dir2/delete$" .git/trace &&
+ grep "^event: delete$" .git/trace
+'
+
+test_expect_success 'rename some files' '
+ test_when_finished clean_up_repo_and_stop_daemon &&
+
+ start_daemon --tf "$PWD/.git/trace" &&
+
+ rename_files &&
+
+ test-tool fsmonitor-client query --token 0 &&
+
+ grep "^event: dir1/rename$" .git/trace &&
+ grep "^event: dir2/rename$" .git/trace &&
+ grep "^event: rename$" .git/trace &&
+ grep "^event: dir1/renamed$" .git/trace &&
+ grep "^event: dir2/renamed$" .git/trace &&
+ grep "^event: renamed$" .git/trace
+'
+
+test_expect_success 'rename directory' '
+ test_when_finished clean_up_repo_and_stop_daemon &&
+
+ start_daemon --tf "$PWD/.git/trace" &&
+
+ mv dirtorename dirrenamed &&
+
+ test-tool fsmonitor-client query --token 0 &&
+
+ grep "^event: dirtorename/*$" .git/trace &&
+ grep "^event: dirrenamed/*$" .git/trace
+'
+
+test_expect_success 'file changes to directory' '
+ test_when_finished clean_up_repo_and_stop_daemon &&
+
+ start_daemon --tf "$PWD/.git/trace" &&
+
+ file_to_directory &&
+
+ test-tool fsmonitor-client query --token 0 &&
+
+ grep "^event: delete$" .git/trace &&
+ grep "^event: delete/new$" .git/trace
+'
+
+test_expect_success 'directory changes to a file' '
+ test_when_finished clean_up_repo_and_stop_daemon &&
+
+ start_daemon --tf "$PWD/.git/trace" &&
+
+ directory_to_file &&
+
+ test-tool fsmonitor-client query --token 0 &&
+
+ grep "^event: dir1$" .git/trace
+'
+
+# The next few test cases exercise the token-resync code. When filesystem
+# drops events (because of filesystem velocity or because the daemon isn't
+# polling fast enough), we need to discard the cached data (relative to the
+# current token) and start collecting events under a new token.
+#
+# the 'test-tool fsmonitor-client flush' command can be used to send a
+# "flush" message to a running daemon and ask it to do a flush/resync.
+
+test_expect_success 'flush cached data' '
+ test_when_finished "stop_daemon_delete_repo test_flush" &&
+
+ git init test_flush &&
+
+ start_daemon -C test_flush --tf "$PWD/.git/trace_daemon" --tk true &&
+
+ # The daemon should have an initial token with no events in _0 and
+ # then a few (probably platform-specific number of) events in _1.
+ # These should both have the same <token_id>.
+
+ test-tool -C test_flush fsmonitor-client query --token "builtin:test_00000001:0" >actual_0 &&
+ nul_to_q <actual_0 >actual_q0 &&
+
+ >test_flush/file_1 &&
+ >test_flush/file_2 &&
+
+ test-tool -C test_flush fsmonitor-client query --token "builtin:test_00000001:0" >actual_1 &&
+ nul_to_q <actual_1 >actual_q1 &&
+
+ grep "file_1" actual_q1 &&
+
+ # Force a flush. This will change the <token_id>, reset the <seq_nr>, and
+ # flush the file data. Then create some events and ensure that the file
+ # again appears in the cache. It should have the new <token_id>.
+
+ test-tool -C test_flush fsmonitor-client flush >flush_0 &&
+ nul_to_q <flush_0 >flush_q0 &&
+ grep "^builtin:test_00000002:0Q/Q$" flush_q0 &&
+
+ test-tool -C test_flush fsmonitor-client query --token "builtin:test_00000002:0" >actual_2 &&
+ nul_to_q <actual_2 >actual_q2 &&
+
+ grep "^builtin:test_00000002:0Q$" actual_q2 &&
+
+ >test_flush/file_3 &&
+
+ test-tool -C test_flush fsmonitor-client query --token "builtin:test_00000002:0" >actual_3 &&
+ nul_to_q <actual_3 >actual_q3 &&
+
+ grep "file_3" actual_q3
+'
+
+# The next few test cases create repos where the .git directory is NOT
+# inside the one of the working directory. That is, where .git is a file
+# that points to a directory elsewhere. This happens for submodules and
+# non-primary worktrees.
+
+test_expect_success 'setup worktree base' '
+ git init wt-base &&
+ echo 1 >wt-base/file1 &&
+ git -C wt-base add file1 &&
+ git -C wt-base commit -m "c1"
+'
+
+test_expect_success 'worktree with .git file' '
+ git -C wt-base worktree add ../wt-secondary &&
+
+ start_daemon -C wt-secondary \
+ --tf "$PWD/trace_wt_secondary" \
+ --t2 "$PWD/trace2_wt_secondary" &&
+
+ git -C wt-secondary fsmonitor--daemon stop &&
+ test_must_fail git -C wt-secondary fsmonitor--daemon status
+'
+
+# NEEDSWORK: Repeat one of the "edit" tests on wt-secondary and
+# confirm that we get the same events and behavior -- that is, that
+# fsmonitor--daemon correctly watches BOTH the working directory and
+# the external GITDIR directory and behaves the same as when ".git"
+# is a directory inside the working directory.
+
+test_expect_success 'cleanup worktrees' '
+ stop_daemon_delete_repo wt-secondary &&
+ stop_daemon_delete_repo wt-base
+'
+
+# The next few tests perform arbitrary/contrived file operations and
+# confirm that status is correct. That is, that the data (or lack of
+# data) from fsmonitor doesn't cause incorrect results. And doesn't
+# cause incorrect results when the untracked-cache is enabled.
+
+test_lazy_prereq UNTRACKED_CACHE '
+ git update-index --test-untracked-cache
+'
+
+test_expect_success 'Matrix: setup for untracked-cache,fsmonitor matrix' '
+ test_unconfig core.fsmonitor &&
+ git update-index --no-fsmonitor &&
+ test_might_fail git fsmonitor--daemon stop
+'
+
+matrix_clean_up_repo () {
+ git reset --hard HEAD &&
+ git clean -fd
+}
+
+matrix_try () {
+ uc=$1 &&
+ fsm=$2 &&
+ fn=$3 &&
+
+ if test $uc = true && test $fsm = false
+ then
+ # The untracked-cache is buggy when FSMonitor is
+ # DISABLED, so skip the tests for this matrix
+ # combination.
+ #
+ # We've observed random, occasional test failures on
+ # Windows and MacOS when the UC is turned on and FSM
+ # is turned off. These are rare, but they do happen
+ # indicating that it is probably a race condition within
+ # the untracked cache itself.
+ #
+ # It usually happens when a test does F/D trickery and
+ # then the NEXT test fails because of extra status
+ # output from stale UC data from the previous test.
+ #
+ # Since FSMonitor is not involved in the error, skip
+ # the tests for this matrix combination.
+ #
+ return 0
+ fi &&
+
+ test_expect_success "Matrix[uc:$uc][fsm:$fsm] $fn" '
+ matrix_clean_up_repo &&
+ $fn &&
+ if test $uc = false && test $fsm = false
+ then
+ git status --porcelain=v1 >.git/expect.$fn
+ else
+ git status --porcelain=v1 >.git/actual.$fn &&
+ test_cmp .git/expect.$fn .git/actual.$fn
+ fi
+ '
+}
+
+uc_values="false"
+test_have_prereq UNTRACKED_CACHE && uc_values="false true"
+for uc_val in $uc_values
+do
+ if test $uc_val = false
+ then
+ test_expect_success "Matrix[uc:$uc_val] disable untracked cache" '
+ git config core.untrackedcache false &&
+ git update-index --no-untracked-cache
+ '
+ else
+ test_expect_success "Matrix[uc:$uc_val] enable untracked cache" '
+ git config core.untrackedcache true &&
+ git update-index --untracked-cache
+ '
+ fi
+
+ fsm_values="false true"
+ for fsm_val in $fsm_values
+ do
+ if test $fsm_val = false
+ then
+ test_expect_success "Matrix[uc:$uc_val][fsm:$fsm_val] disable fsmonitor" '
+ test_unconfig core.fsmonitor &&
+ git update-index --no-fsmonitor &&
+ test_might_fail git fsmonitor--daemon stop
+ '
+ else
+ test_expect_success "Matrix[uc:$uc_val][fsm:$fsm_val] enable fsmonitor" '
+ git config core.fsmonitor true &&
+ git fsmonitor--daemon start &&
+ git update-index --fsmonitor
+ '
+ fi
+
+ matrix_try $uc_val $fsm_val edit_files
+ matrix_try $uc_val $fsm_val delete_files
+ matrix_try $uc_val $fsm_val create_files
+ matrix_try $uc_val $fsm_val rename_files
+ matrix_try $uc_val $fsm_val file_to_directory
+ matrix_try $uc_val $fsm_val directory_to_file
+
+ matrix_try $uc_val $fsm_val move_directory_contents_deeper
+ matrix_try $uc_val $fsm_val move_directory_up
+ matrix_try $uc_val $fsm_val move_directory
+
+ if test $fsm_val = true
+ then
+ test_expect_success "Matrix[uc:$uc_val][fsm:$fsm_val] disable fsmonitor at end" '
+ test_unconfig core.fsmonitor &&
+ git update-index --no-fsmonitor &&
+ test_might_fail git fsmonitor--daemon stop
+ '
+ fi
+ done
+done
+
+# Test Unicode UTF-8 characters in the pathname of the working
+# directory root. Use of "*A()" routines rather than "*W()" routines
+# on Windows can sometimes lead to odd failures.
+#
+u1=$(printf "u_c3_a6__\xC3\xA6")
+u2=$(printf "u_e2_99_ab__\xE2\x99\xAB")
+u_values="$u1 $u2"
+for u in $u_values
+do
+ test_expect_success "unicode in repo root path: $u" '
+ test_when_finished "stop_daemon_delete_repo $u" &&
+
+ git init "$u" &&
+ echo 1 >"$u"/file1 &&
+ git -C "$u" add file1 &&
+ git -C "$u" config core.fsmonitor true &&
+
+ start_daemon -C "$u" &&
+ git -C "$u" status >actual &&
+ grep "new file: file1" actual
+ '
+done
+
+# Test fsmonitor interaction with submodules.
+#
+# If we start the daemon in the super, it will see FS events for
+# everything in the working directory cone and this includes any
+# files/directories contained *within* the submodules.
+#
+# A `git status` at top level will get events for items within the
+# submodule and ignore them, since they aren't named in the index
+# of the super repo. This makes the fsmonitor response a little
+# noisy, but it doesn't alter the correctness of the state of the
+# super-proper.
+#
+# When we have submodules, `git status` normally does a recursive
+# status on each of the submodules and adds a summary row for any
+# dirty submodules. (See the "S..." bits in porcelain V2 output.)
+#
+# It is therefore important that the top level status not be tricked
+# by the FSMonitor response to skip those recursive calls. That is,
+# even if FSMonitor says that the mtime of the submodule directory
+# hasn't changed and it could be implicitly marked valid, we must
+# not take that shortcut. We need to force the recusion into the
+# submodule so that we get a summary of the status *within* the
+# submodule.
+
+create_super () {
+ super="$1" &&
+
+ git init "$super" &&
+ echo x >"$super/file_1" &&
+ echo y >"$super/file_2" &&
+ echo z >"$super/file_3" &&
+ mkdir "$super/dir_1" &&
+ echo a >"$super/dir_1/file_11" &&
+ echo b >"$super/dir_1/file_12" &&
+ mkdir "$super/dir_1/dir_2" &&
+ echo a >"$super/dir_1/dir_2/file_21" &&
+ echo b >"$super/dir_1/dir_2/file_22" &&
+ git -C "$super" add . &&
+ git -C "$super" commit -m "initial $super commit"
+}
+
+create_sub () {
+ sub="$1" &&
+
+ git init "$sub" &&
+ echo x >"$sub/file_x" &&
+ echo y >"$sub/file_y" &&
+ echo z >"$sub/file_z" &&
+ mkdir "$sub/dir_x" &&
+ echo a >"$sub/dir_x/file_a" &&
+ echo b >"$sub/dir_x/file_b" &&
+ mkdir "$sub/dir_x/dir_y" &&
+ echo a >"$sub/dir_x/dir_y/file_a" &&
+ echo b >"$sub/dir_x/dir_y/file_b" &&
+ git -C "$sub" add . &&
+ git -C "$sub" commit -m "initial $sub commit"
+}
+
+my_match_and_clean () {
+ git -C super --no-optional-locks status --porcelain=v2 >actual.with &&
+ git -C super --no-optional-locks -c core.fsmonitor=false \
+ status --porcelain=v2 >actual.without &&
+ test_cmp actual.with actual.without &&
+
+ git -C super --no-optional-locks diff-index --name-status HEAD >actual.with &&
+ git -C super --no-optional-locks -c core.fsmonitor=false \
+ diff-index --name-status HEAD >actual.without &&
+ test_cmp actual.with actual.without &&
+
+ git -C super/dir_1/dir_2/sub reset --hard &&
+ git -C super/dir_1/dir_2/sub clean -d -f
+}
+
+test_expect_success 'submodule setup' '
+ git config --global protocol.file.allow always
+'
+
+test_expect_success 'submodule always visited' '
+ test_when_finished "git -C super fsmonitor--daemon stop; \
+ rm -rf super; \
+ rm -rf sub" &&
+
+ create_super super &&
+ create_sub sub &&
+
+ git -C super submodule add ../sub ./dir_1/dir_2/sub &&
+ git -C super commit -m "add sub" &&
+
+ start_daemon -C super &&
+ git -C super config core.fsmonitor true &&
+ git -C super update-index --fsmonitor &&
+ git -C super status &&
+
+ # Now run pairs of commands w/ and w/o FSMonitor while we make
+ # some dirt in the submodule and confirm matching output.
+
+ # Completely clean status.
+ my_match_and_clean &&
+
+ # .M S..U
+ echo z >super/dir_1/dir_2/sub/dir_x/dir_y/foobar_u &&
+ my_match_and_clean &&
+
+ # .M S.M.
+ echo z >super/dir_1/dir_2/sub/dir_x/dir_y/foobar_m &&
+ git -C super/dir_1/dir_2/sub add . &&
+ my_match_and_clean &&
+
+ # .M S.M.
+ echo z >>super/dir_1/dir_2/sub/dir_x/dir_y/file_a &&
+ git -C super/dir_1/dir_2/sub add . &&
+ my_match_and_clean &&
+
+ # .M SC..
+ echo z >>super/dir_1/dir_2/sub/dir_x/dir_y/file_a &&
+ git -C super/dir_1/dir_2/sub add . &&
+ git -C super/dir_1/dir_2/sub commit -m "SC.." &&
+ my_match_and_clean
+'
+
+# If a submodule has a `sub/.git/` directory (rather than a file
+# pointing to the super's `.git/modules/sub`) and `core.fsmonitor`
+# turned on in the submodule and the daemon is not yet started in
+# the submodule, and someone does a `git submodule absorbgitdirs`
+# in the super, Git will recursively invoke `git submodule--helper`
+# to do the work and this may try to read the index. This will
+# try to start the daemon in the submodule.
+
+test_expect_success "submodule absorbgitdirs implicitly starts daemon" '
+ test_when_finished "rm -rf super; \
+ rm -rf sub; \
+ rm super-sub.trace" &&
+
+ create_super super &&
+ create_sub sub &&
+
+ # Copy rather than submodule add so that we get a .git dir.
+ cp -R ./sub ./super/dir_1/dir_2/sub &&
+
+ git -C super/dir_1/dir_2/sub config core.fsmonitor true &&
+
+ git -C super submodule add ../sub ./dir_1/dir_2/sub &&
+ git -C super commit -m "add sub" &&
+
+ test_path_is_dir super/dir_1/dir_2/sub/.git &&
+
+ cwd="$(cd super && pwd)" &&
+ cat >expect <<-EOF &&
+ Migrating git directory of '\''dir_1/dir_2/sub'\'' from
+ '\''$cwd/dir_1/dir_2/sub/.git'\'' to
+ '\''$cwd/.git/modules/dir_1/dir_2/sub'\''
+ EOF
+ GIT_TRACE2_EVENT="$PWD/super-sub.trace" \
+ git -C super submodule absorbgitdirs >out 2>actual &&
+ test_cmp expect actual &&
+ test_must_be_empty out &&
+
+ # Confirm that the trace2 log contains a record of the
+ # daemon starting.
+ test_subcommand git fsmonitor--daemon start <super-sub.trace
+'
+
+# On a case-insensitive file system, confirm that the daemon
+# notices when the .git directory is moved/renamed/deleted
+# regardless of how it is spelled in the FS event.
+# That is, does the FS event receive the spelling of the
+# operation or does it receive the spelling preserved with
+# the file/directory.
+#
+test_expect_success CASE_INSENSITIVE_FS 'case insensitive+preserving' '
+ test_when_finished "stop_daemon_delete_repo test_insensitive" &&
+
+ git init test_insensitive &&
+
+ start_daemon -C test_insensitive --tf "$PWD/insensitive.trace" &&
+
+ mkdir -p test_insensitive/abc/def &&
+ echo xyz >test_insensitive/ABC/DEF/xyz &&
+
+ test_path_is_dir test_insensitive/.git &&
+ test_path_is_dir test_insensitive/.GIT &&
+
+ # Rename .git using an alternate spelling to verify that
+ # the daemon detects it and automatically shuts down.
+ mv test_insensitive/.GIT test_insensitive/.FOO &&
+
+ # See [1] above.
+ mv test_insensitive/.FOO test_insensitive/.git &&
+
+ verify_implicit_shutdown test_insensitive &&
+
+ # Verify that events were reported using on-disk spellings of the
+ # directories and files that we touched. We may or may not get a
+ # trailing slash on modified directories.
+ #
+ grep -E "^event: abc/?$" ./insensitive.trace &&
+ grep -E "^event: abc/def/?$" ./insensitive.trace &&
+ grep -E "^event: abc/def/xyz$" ./insensitive.trace
+'
+
+# The variable "unicode_debug" is defined in the following library
+# script to dump information about how the (OS, FS) handles Unicode
+# composition. Uncomment the following line if you want to enable it.
+#
+# unicode_debug=true
+
+. "$TEST_DIRECTORY/lib-unicode-nfc-nfd.sh"
+
+# See if the OS or filesystem does NFC/NFD aliasing/munging.
+#
+# The daemon should err on the side of caution and send BOTH the
+# NFC and NFD forms. It does not know the original spelling of
+# the pathname (how the user thinks it should be spelled), so
+# emit both and let the client decide (when necessary). This is
+# similar to "core.precomposeUnicode".
+#
+test_expect_success !UNICODE_COMPOSITION_SENSITIVE 'Unicode nfc/nfd' '
+ test_when_finished "stop_daemon_delete_repo test_unicode" &&
+
+ git init test_unicode &&
+
+ start_daemon -C test_unicode --tf "$PWD/unicode.trace" &&
+
+ # Create a directory using an NFC spelling.
+ #
+ mkdir test_unicode/nfc &&
+ mkdir test_unicode/nfc/c_${utf8_nfc} &&
+
+ # Create a directory using an NFD spelling.
+ #
+ mkdir test_unicode/nfd &&
+ mkdir test_unicode/nfd/d_${utf8_nfd} &&
+
+ git -C test_unicode fsmonitor--daemon stop &&
+
+ if test_have_prereq UNICODE_NFC_PRESERVED
+ then
+ # We should have seen NFC event from OS.
+ # We should not have synthesized an NFD event.
+ grep -E "^event: nfc/c_${utf8_nfc}/?$" ./unicode.trace &&
+ grep -E -v "^event: nfc/c_${utf8_nfd}/?$" ./unicode.trace
+ else
+ # We should have seen NFD event from OS.
+ # We should have synthesized an NFC event.
+ grep -E "^event: nfc/c_${utf8_nfd}/?$" ./unicode.trace &&
+ grep -E "^event: nfc/c_${utf8_nfc}/?$" ./unicode.trace
+ fi &&
+
+ # We assume UNICODE_NFD_PRESERVED.
+ # We should have seen explicit NFD from OS.
+ # We should have synthesized an NFC event.
+ grep -E "^event: nfd/d_${utf8_nfd}/?$" ./unicode.trace &&
+ grep -E "^event: nfd/d_${utf8_nfc}/?$" ./unicode.trace
+'
+
+test_expect_success 'split-index and FSMonitor work well together' '
+ git init split-index &&
+ test_when_finished "git -C \"$PWD/split-index\" \
+ fsmonitor--daemon stop" &&
+ (
+ cd split-index &&
+ git config core.splitIndex true &&
+ # force split-index in most cases
+ git config splitIndex.maxPercentChange 99 &&
+ git config core.fsmonitor true &&
+
+ # Create the following commit topology:
+ #
+ # * merge three
+ # |\
+ # | * three
+ # * | merge two
+ # |\|
+ # | * two
+ # * | one
+ # |/
+ # * 5a5efd7 initial
+
+ test_commit initial &&
+ test_commit two &&
+ test_commit three &&
+ git reset --hard initial &&
+ test_commit one &&
+ test_tick &&
+ git merge two &&
+ test_tick &&
+ git merge three &&
+
+ git rebase --force-rebase -r one
+ )
+'
+
+test_done
diff --git a/t/t7528-signed-commit-ssh.sh b/t/t7528-signed-commit-ssh.sh
new file mode 100755
index 0000000..065f780
--- /dev/null
+++ b/t/t7528-signed-commit-ssh.sh
@@ -0,0 +1,462 @@
+#!/bin/sh
+
+test_description='ssh signed commit tests'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+GNUPGHOME_NOT_USED=$GNUPGHOME
+. "$TEST_DIRECTORY/lib-gpg.sh"
+
+test_expect_success GPGSSH 'create signed commits' '
+ test_oid_cache <<-\EOF &&
+ header sha1:gpgsig
+ header sha256:gpgsig-sha256
+ EOF
+
+ test_when_finished "test_unconfig commit.gpgsign" &&
+ test_config gpg.format ssh &&
+ test_config user.signingkey "${GPGSSH_KEY_PRIMARY}" &&
+
+ echo 1 >file && git add file &&
+ test_tick && git commit -S -m initial &&
+ git tag initial &&
+ git branch side &&
+
+ echo 2 >file && test_tick && git commit -a -S -m second &&
+ git tag second &&
+
+ git checkout side &&
+ echo 3 >elif && git add elif &&
+ test_tick && git commit -m "third on side" &&
+
+ git checkout main &&
+ test_tick && git merge -S side &&
+ git tag merge &&
+
+ echo 4 >file && test_tick && git commit -a -m "fourth unsigned" &&
+ git tag fourth-unsigned &&
+
+ test_tick && git commit --amend -S -m "fourth signed" &&
+ git tag fourth-signed &&
+
+ git config commit.gpgsign true &&
+ echo 5 >file && test_tick && git commit -a -m "fifth signed" &&
+ git tag fifth-signed &&
+
+ git config commit.gpgsign false &&
+ echo 6 >file && test_tick && git commit -a -m "sixth" &&
+ git tag sixth-unsigned &&
+
+ git config commit.gpgsign true &&
+ echo 7 >file && test_tick && git commit -a -m "seventh" --no-gpg-sign &&
+ git tag seventh-unsigned &&
+
+ test_tick && git rebase -f HEAD^^ && git tag sixth-signed HEAD^ &&
+ git tag seventh-signed &&
+
+ echo 8 >file && test_tick && git commit -a -m eighth -S"${GPGSSH_KEY_UNTRUSTED}" &&
+ git tag eighth-signed-alt &&
+
+ # commit.gpgsign is still on but this must not be signed
+ echo 9 | git commit-tree HEAD^{tree} >oid &&
+ test_line_count = 1 oid &&
+ git tag ninth-unsigned $(cat oid) &&
+ # explicit -S of course must sign.
+ echo 10 | git commit-tree -S HEAD^{tree} >oid &&
+ test_line_count = 1 oid &&
+ git tag tenth-signed $(cat oid) &&
+
+ # --gpg-sign[=<key-id>] must sign.
+ echo 11 | git commit-tree --gpg-sign HEAD^{tree} >oid &&
+ test_line_count = 1 oid &&
+ git tag eleventh-signed $(cat oid) &&
+ echo 12 | git commit-tree --gpg-sign="${GPGSSH_KEY_UNTRUSTED}" HEAD^{tree} >oid &&
+ test_line_count = 1 oid &&
+ git tag twelfth-signed-alt $(cat oid) &&
+
+ echo 13>file && test_tick && git commit -a -m thirteenth -S"${GPGSSH_KEY_ECDSA}" &&
+ git tag thirteenth-signed-ecdsa
+'
+
+test_expect_success GPGSSH 'sign commits using literal public keys with ssh-agent' '
+ test_when_finished "test_unconfig commit.gpgsign" &&
+ test_config gpg.format ssh &&
+ eval $(ssh-agent) &&
+ test_when_finished "kill ${SSH_AGENT_PID}" &&
+ ssh-add "${GPGSSH_KEY_PRIMARY}" &&
+ echo 1 >file && git add file &&
+ git commit -a -m rsa-inline -S"$(cat "${GPGSSH_KEY_PRIMARY}.pub")" &&
+ echo 2 >file &&
+ test_config user.signingkey "$(cat "${GPGSSH_KEY_PRIMARY}.pub")" &&
+ git commit -a -m rsa-config -S &&
+ ssh-add "${GPGSSH_KEY_ECDSA}" &&
+ echo 3 >file &&
+ git commit -a -m ecdsa-inline -S"key::$(cat "${GPGSSH_KEY_ECDSA}.pub")" &&
+ echo 4 >file &&
+ test_config user.signingkey "key::$(cat "${GPGSSH_KEY_ECDSA}.pub")" &&
+ git commit -a -m ecdsa-config -S
+'
+
+test_expect_success GPGSSH,GPGSSH_VERIFYTIME 'create signed commits with keys having defined lifetimes' '
+ test_when_finished "test_unconfig commit.gpgsign" &&
+ test_config gpg.format ssh &&
+
+ echo expired >file && test_tick && git commit -a -m expired -S"${GPGSSH_KEY_EXPIRED}" &&
+ git tag expired-signed &&
+
+ echo notyetvalid >file && test_tick && git commit -a -m notyetvalid -S"${GPGSSH_KEY_NOTYETVALID}" &&
+ git tag notyetvalid-signed &&
+
+ echo timeboxedvalid >file && test_tick && git commit -a -m timeboxedvalid -S"${GPGSSH_KEY_TIMEBOXEDVALID}" &&
+ git tag timeboxedvalid-signed &&
+
+ echo timeboxedinvalid >file && test_tick && git commit -a -m timeboxedinvalid -S"${GPGSSH_KEY_TIMEBOXEDINVALID}" &&
+ git tag timeboxedinvalid-signed
+'
+
+test_expect_success GPGSSH 'verify and show signatures' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ test_config gpg.mintrustlevel UNDEFINED &&
+ (
+ for commit in initial second merge fourth-signed \
+ fifth-signed sixth-signed seventh-signed tenth-signed \
+ eleventh-signed
+ do
+ git verify-commit $commit &&
+ git show --pretty=short --show-signature $commit >actual &&
+ grep "${GPGSSH_GOOD_SIGNATURE_TRUSTED}" actual &&
+ ! grep "${GPGSSH_BAD_SIGNATURE}" actual &&
+ echo $commit OK || exit 1
+ done
+ ) &&
+ (
+ for commit in merge^2 fourth-unsigned sixth-unsigned \
+ seventh-unsigned ninth-unsigned
+ do
+ test_must_fail git verify-commit $commit &&
+ git show --pretty=short --show-signature $commit >actual &&
+ ! grep "${GPGSSH_GOOD_SIGNATURE_TRUSTED}" actual &&
+ ! grep "${GPGSSH_BAD_SIGNATURE}" actual &&
+ echo $commit OK || exit 1
+ done
+ ) &&
+ (
+ for commit in eighth-signed-alt twelfth-signed-alt
+ do
+ git show --pretty=short --show-signature $commit >actual &&
+ grep "${GPGSSH_GOOD_SIGNATURE_UNTRUSTED}" actual &&
+ ! grep "${GPGSSH_BAD_SIGNATURE}" actual &&
+ grep "${GPGSSH_KEY_NOT_TRUSTED}" actual &&
+ echo $commit OK || exit 1
+ done
+ )
+'
+
+test_expect_success GPGSSH 'verify-commit exits failure on untrusted signature' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ test_must_fail git verify-commit eighth-signed-alt 2>actual &&
+ grep "${GPGSSH_GOOD_SIGNATURE_UNTRUSTED}" actual &&
+ ! grep "${GPGSSH_BAD_SIGNATURE}" actual &&
+ grep "${GPGSSH_KEY_NOT_TRUSTED}" actual
+'
+
+test_expect_success GPGSSH,GPGSSH_VERIFYTIME 'verify-commit exits failure on expired signature key' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ test_must_fail git verify-commit expired-signed 2>actual &&
+ ! grep "${GPGSSH_GOOD_SIGNATURE_TRUSTED}" actual
+'
+
+test_expect_success GPGSSH,GPGSSH_VERIFYTIME 'verify-commit exits failure on not yet valid signature key' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ test_must_fail git verify-commit notyetvalid-signed 2>actual &&
+ ! grep "${GPGSSH_GOOD_SIGNATURE_TRUSTED}" actual
+'
+
+test_expect_success GPGSSH,GPGSSH_VERIFYTIME 'verify-commit succeeds with commit date and key validity matching' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ git verify-commit timeboxedvalid-signed 2>actual &&
+ grep "${GPGSSH_GOOD_SIGNATURE_TRUSTED}" actual &&
+ ! grep "${GPGSSH_BAD_SIGNATURE}" actual
+'
+
+test_expect_success GPGSSH,GPGSSH_VERIFYTIME 'verify-commit exits failure with commit date outside of key validity' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ test_must_fail git verify-commit timeboxedinvalid-signed 2>actual &&
+ ! grep "${GPGSSH_GOOD_SIGNATURE_TRUSTED}" actual
+'
+
+test_expect_success GPGSSH 'verify-commit exits success with matching minTrustLevel' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ test_config gpg.minTrustLevel fully &&
+ git verify-commit sixth-signed
+'
+
+test_expect_success GPGSSH 'verify-commit exits success with low minTrustLevel' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ test_config gpg.minTrustLevel marginal &&
+ git verify-commit sixth-signed
+'
+
+test_expect_success GPGSSH 'verify-commit exits failure with high minTrustLevel' '
+ test_config gpg.minTrustLevel ultimate &&
+ test_must_fail git verify-commit eighth-signed-alt
+'
+
+test_expect_success GPGSSH 'verify signatures with --raw' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ (
+ for commit in initial second merge fourth-signed fifth-signed sixth-signed seventh-signed
+ do
+ git verify-commit --raw $commit 2>actual &&
+ grep "${GPGSSH_GOOD_SIGNATURE_TRUSTED}" actual &&
+ ! grep "${GPGSSH_BAD_SIGNATURE}" actual &&
+ echo $commit OK || exit 1
+ done
+ ) &&
+ (
+ for commit in merge^2 fourth-unsigned sixth-unsigned seventh-unsigned
+ do
+ test_must_fail git verify-commit --raw $commit 2>actual &&
+ ! grep "${GPGSSH_GOOD_SIGNATURE_TRUSTED}" actual &&
+ ! grep "${GPGSSH_BAD_SIGNATURE}" actual &&
+ echo $commit OK || exit 1
+ done
+ ) &&
+ (
+ for commit in eighth-signed-alt
+ do
+ test_must_fail git verify-commit --raw $commit 2>actual &&
+ grep "${GPGSSH_GOOD_SIGNATURE_UNTRUSTED}" actual &&
+ ! grep "${GPGSSH_BAD_SIGNATURE}" actual &&
+ echo $commit OK || exit 1
+ done
+ )
+'
+
+test_expect_success GPGSSH 'proper header is used for hash algorithm' '
+ git cat-file commit fourth-signed >output &&
+ grep "^$(test_oid header) -----BEGIN SSH SIGNATURE-----" output
+'
+
+test_expect_success GPGSSH 'show signed commit with signature' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ git show -s initial >commit &&
+ git show -s --show-signature initial >show &&
+ git verify-commit -v initial >verify.1 2>verify.2 &&
+ git cat-file commit initial >cat &&
+ grep -v -e "${GPGSSH_GOOD_SIGNATURE_TRUSTED}" -e "Warning: " show >show.commit &&
+ grep -e "${GPGSSH_GOOD_SIGNATURE_TRUSTED}" -e "Warning: " show >show.gpg &&
+ grep -v "^ " cat | grep -v "^gpgsig.* " >cat.commit &&
+ test_cmp show.commit commit &&
+ test_cmp show.gpg verify.2 &&
+ test_cmp cat.commit verify.1
+'
+
+test_expect_success GPGSSH 'detect fudged signature' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ git cat-file commit seventh-signed >raw &&
+ sed -e "s/^seventh/7th forged/" raw >forged1 &&
+ git hash-object -w -t commit forged1 >forged1.commit &&
+ test_must_fail git verify-commit $(cat forged1.commit) &&
+ git show --pretty=short --show-signature $(cat forged1.commit) >actual1 &&
+ grep "${GPGSSH_BAD_SIGNATURE}" actual1 &&
+ ! grep "${GPGSSH_GOOD_SIGNATURE_TRUSTED}" actual1 &&
+ ! grep "${GPGSSH_GOOD_SIGNATURE_UNTRUSTED}" actual1
+'
+
+test_expect_success GPGSSH 'detect fudged signature with NUL' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ git cat-file commit seventh-signed >raw &&
+ cat raw >forged2 &&
+ echo Qwik | tr "Q" "\000" >>forged2 &&
+ git hash-object --literally -w -t commit forged2 >forged2.commit &&
+ test_must_fail git verify-commit $(cat forged2.commit) &&
+ git show --pretty=short --show-signature $(cat forged2.commit) >actual2 &&
+ grep "${GPGSSH_BAD_SIGNATURE}" actual2 &&
+ ! grep "${GPGSSH_GOOD_SIGNATURE_TRUSTED}" actual2
+'
+
+test_expect_success GPGSSH 'amending already signed commit' '
+ test_config gpg.format ssh &&
+ test_config user.signingkey "${GPGSSH_KEY_PRIMARY}" &&
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ git checkout -f fourth-signed^0 &&
+ git commit --amend -S --no-edit &&
+ git verify-commit HEAD &&
+ git show -s --show-signature HEAD >actual &&
+ grep "${GPGSSH_GOOD_SIGNATURE_TRUSTED}" actual &&
+ ! grep "${GPGSSH_BAD_SIGNATURE}" actual
+'
+
+test_expect_success GPGSSH 'show good signature with custom format' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ FINGERPRINT=$(ssh-keygen -lf "${GPGSSH_KEY_PRIMARY}" | awk "{print \$2;}") &&
+ cat >expect.tmpl <<-\EOF &&
+ G
+ FINGERPRINT
+ principal with number 1
+ FINGERPRINT
+
+ EOF
+ sed "s|FINGERPRINT|$FINGERPRINT|g" expect.tmpl >expect &&
+ git log -1 --format="%G?%n%GK%n%GS%n%GF%n%GP" sixth-signed >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success GPGSSH 'show bad signature with custom format' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ cat >expect <<-\EOF &&
+ B
+
+
+
+
+ EOF
+ git log -1 --format="%G?%n%GK%n%GS%n%GF%n%GP" $(cat forged1.commit) >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success GPGSSH 'show untrusted signature with custom format' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ cat >expect.tmpl <<-\EOF &&
+ U
+ FINGERPRINT
+
+ FINGERPRINT
+
+ EOF
+ git log -1 --format="%G?%n%GK%n%GS%n%GF%n%GP" eighth-signed-alt >actual &&
+ FINGERPRINT=$(ssh-keygen -lf "${GPGSSH_KEY_UNTRUSTED}" | awk "{print \$2;}") &&
+ sed "s|FINGERPRINT|$FINGERPRINT|g" expect.tmpl >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success GPGSSH 'show untrusted signature with undefined trust level' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ cat >expect.tmpl <<-\EOF &&
+ undefined
+ FINGERPRINT
+
+ FINGERPRINT
+
+ EOF
+ git log -1 --format="%GT%n%GK%n%GS%n%GF%n%GP" eighth-signed-alt >actual &&
+ FINGERPRINT=$(ssh-keygen -lf "${GPGSSH_KEY_UNTRUSTED}" | awk "{print \$2;}") &&
+ sed "s|FINGERPRINT|$FINGERPRINT|g" expect.tmpl >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success GPGSSH 'show untrusted signature with ultimate trust level' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ cat >expect.tmpl <<-\EOF &&
+ fully
+ FINGERPRINT
+ principal with number 1
+ FINGERPRINT
+
+ EOF
+ git log -1 --format="%GT%n%GK%n%GS%n%GF%n%GP" sixth-signed >actual &&
+ FINGERPRINT=$(ssh-keygen -lf "${GPGSSH_KEY_PRIMARY}" | awk "{print \$2;}") &&
+ sed "s|FINGERPRINT|$FINGERPRINT|g" expect.tmpl >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success GPGSSH 'show lack of signature with custom format' '
+ cat >expect <<-\EOF &&
+ N
+
+
+
+
+ EOF
+ git log -1 --format="%G?%n%GK%n%GS%n%GF%n%GP" seventh-unsigned >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success GPGSSH 'log.showsignature behaves like --show-signature' '
+ test_config gpg.ssh.allowedSignersFile "${GPGSSH_ALLOWED_SIGNERS}" &&
+ test_config log.showsignature true &&
+ git show initial >actual &&
+ grep "${GPGSSH_GOOD_SIGNATURE_TRUSTED}" actual
+'
+
+test_expect_success GPGSSH 'check config gpg.format values' '
+ test_config gpg.format ssh &&
+ test_config user.signingkey "${GPGSSH_KEY_PRIMARY}" &&
+ test_config gpg.format ssh &&
+ git commit -S --amend -m "success" &&
+ test_config gpg.format OpEnPgP &&
+ test_must_fail git commit -S --amend -m "fail"
+'
+
+test_expect_failure GPGSSH 'detect fudged commit with double signature (TODO)' '
+ sed -e "/gpgsig/,/END PGP/d" forged1 >double-base &&
+ sed -n -e "/gpgsig/,/END PGP/p" forged1 | \
+ sed -e "s/^$(test_oid header)//;s/^ //" | gpg --dearmor >double-sig1.sig &&
+ gpg -o double-sig2.sig -u 29472784 --detach-sign double-base &&
+ cat double-sig1.sig double-sig2.sig | gpg --enarmor >double-combined.asc &&
+ sed -e "s/^\(-.*\)ARMORED FILE/\1SIGNATURE/;1s/^/$(test_oid header) /;2,\$s/^/ /" \
+ double-combined.asc > double-gpgsig &&
+ sed -e "/committer/r double-gpgsig" double-base >double-commit &&
+ git hash-object -w -t commit double-commit >double-commit.commit &&
+ test_must_fail git verify-commit $(cat double-commit.commit) &&
+ git show --pretty=short --show-signature $(cat double-commit.commit) >double-actual &&
+ grep "BAD signature from" double-actual &&
+ grep "Good signature from" double-actual
+'
+
+test_expect_failure GPGSSH 'show double signature with custom format (TODO)' '
+ cat >expect <<-\EOF &&
+ E
+
+
+
+
+ EOF
+ git log -1 --format="%G?%n%GK%n%GS%n%GF%n%GP" $(cat double-commit.commit) >actual &&
+ test_cmp expect actual
+'
+
+
+test_expect_failure GPGSSH 'verify-commit verifies multiply signed commits (TODO)' '
+ git init multiply-signed &&
+ cd multiply-signed &&
+ test_commit first &&
+ echo 1 >second &&
+ git add second &&
+ tree=$(git write-tree) &&
+ parent=$(git rev-parse HEAD^{commit}) &&
+ git commit --gpg-sign -m second &&
+ git cat-file commit HEAD &&
+ # Avoid trailing whitespace.
+ sed -e "s/^Q//" -e "s/^Z/ /" >commit <<-EOF &&
+ Qtree $tree
+ Qparent $parent
+ Qauthor A U Thor <author@example.com> 1112912653 -0700
+ Qcommitter C O Mitter <committer@example.com> 1112912653 -0700
+ Qgpgsig -----BEGIN PGP SIGNATURE-----
+ QZ
+ Q iHQEABECADQWIQRz11h0S+chaY7FTocTtvUezd5DDQUCX/uBDRYcY29tbWl0dGVy
+ Q QGV4YW1wbGUuY29tAAoJEBO29R7N3kMNd+8AoK1I8mhLHviPH+q2I5fIVgPsEtYC
+ Q AKCTqBh+VabJceXcGIZuF0Ry+udbBQ==
+ Q =tQ0N
+ Q -----END PGP SIGNATURE-----
+ Qgpgsig-sha256 -----BEGIN PGP SIGNATURE-----
+ QZ
+ Q iHQEABECADQWIQRz11h0S+chaY7FTocTtvUezd5DDQUCX/uBIBYcY29tbWl0dGVy
+ Q QGV4YW1wbGUuY29tAAoJEBO29R7N3kMN/NEAn0XO9RYSBj2dFyozi0JKSbssYMtO
+ Q AJwKCQ1BQOtuwz//IjU8TiS+6S4iUw==
+ Q =pIwP
+ Q -----END PGP SIGNATURE-----
+ Q
+ Qsecond
+ EOF
+ head=$(git hash-object -t commit -w commit) &&
+ git reset --hard $head &&
+ git verify-commit $head 2>actual &&
+ grep "Good signature from" actual &&
+ ! grep "BAD signature from" actual
+'
+
+test_done
diff --git a/t/t7600-merge.sh b/t/t7600-merge.sh
new file mode 100755
index 0000000..e5ff073
--- /dev/null
+++ b/t/t7600-merge.sh
@@ -0,0 +1,1086 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Lars Hjemli
+#
+
+test_description='git merge
+
+Testing basic merge operations/option parsing.
+
+! [c0] commit 0
+ ! [c1] commit 1
+ ! [c2] commit 2
+ ! [c3] commit 3
+ ! [c4] c4
+ ! [c5] c5
+ ! [c6] c6
+ * [main] Merge commit 'c1'
+--------
+ - [main] Merge commit 'c1'
+ + * [c1] commit 1
+ + [c6] c6
+ + [c5] c5
+ ++ [c4] c4
+ ++++ [c3] commit 3
+ + [c2] commit 2
++++++++* [c0] commit 0
+'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-gpg.sh
+
+test_write_lines 1 2 3 4 5 6 7 8 9 >file
+cp file file.orig
+test_write_lines '1 X' 2 3 4 5 6 7 8 9 >file.1
+test_write_lines 1 2 '3 X' 4 5 6 7 8 9 >file.3
+test_write_lines 1 2 3 4 '5 X' 6 7 8 9 >file.5
+test_write_lines 1 2 3 4 5 6 7 8 '9 X' >file.9
+test_write_lines 1 2 3 4 5 6 7 8 '9 Y' >file.9y
+test_write_lines '1 X' 2 3 4 5 6 7 8 9 >result.1
+test_write_lines '1 X' 2 3 4 '5 X' 6 7 8 9 >result.1-5
+test_write_lines '1 X' 2 3 4 5 6 7 8 '9 X' >result.1-9
+test_write_lines '1 X' 2 3 4 '5 X' 6 7 8 '9 X' >result.1-5-9
+test_write_lines '1 X' 2 '3 X' 4 '5 X' 6 7 8 '9 X' >result.1-3-5-9
+test_write_lines 1 2 3 4 5 6 7 8 '9 Z' >result.9z
+
+create_merge_msgs () {
+ echo "Merge tag 'c2'" >msg.1-5 &&
+ echo "Merge tags 'c2' and 'c3'" >msg.1-5-9 &&
+ {
+ echo "Squashed commit of the following:" &&
+ echo &&
+ git log --no-merges ^HEAD c1
+ } >squash.1 &&
+ {
+ echo "Squashed commit of the following:" &&
+ echo &&
+ git log --no-merges ^HEAD c2
+ } >squash.1-5 &&
+ {
+ echo "Squashed commit of the following:" &&
+ echo &&
+ git log --no-merges ^HEAD c2 c3
+ } >squash.1-5-9 &&
+ {
+ echo "* tag 'c3':" &&
+ echo " commit 3"
+ } >msg.log
+}
+
+verify_merge () {
+ test_cmp "$2" "$1" &&
+ git update-index --refresh &&
+ git diff --exit-code &&
+ if test -n "$3"
+ then
+ git show -s --pretty=tformat:%s HEAD >msg.act &&
+ test_cmp "$3" msg.act
+ fi
+}
+
+verify_head () {
+ echo "$1" >head.expected &&
+ git rev-parse HEAD >head.actual &&
+ test_cmp head.expected head.actual
+}
+
+verify_parents () {
+ test_write_lines "$@" >parents.expected &&
+ >parents.actual &&
+ i=1 &&
+ while test $i -le $#
+ do
+ git rev-parse HEAD^$i >>parents.actual &&
+ i=$(expr $i + 1) ||
+ return 1
+ done &&
+ test_must_fail git rev-parse --verify "HEAD^$i" &&
+ test_cmp parents.expected parents.actual
+}
+
+verify_mergeheads () {
+ test_write_lines "$@" >mergehead.expected &&
+ while read sha1 rest
+ do
+ git rev-parse $sha1 || return 1
+ done <.git/MERGE_HEAD >mergehead.actual &&
+ test_cmp mergehead.expected mergehead.actual
+}
+
+verify_no_mergehead () {
+ ! test -e .git/MERGE_HEAD
+}
+
+test_expect_success 'setup' '
+ git add file &&
+ test_tick &&
+ git commit -m "commit 0" &&
+ git tag c0 &&
+ c0=$(git rev-parse HEAD) &&
+ cp file.1 file &&
+ git add file &&
+ cp file.1 other &&
+ git add other &&
+ test_tick &&
+ git commit -m "commit 1" &&
+ git tag c1 &&
+ c1=$(git rev-parse HEAD) &&
+ git reset --hard "$c0" &&
+ cp file.5 file &&
+ git add file &&
+ test_tick &&
+ git commit -m "commit 2" &&
+ git tag c2 &&
+ c2=$(git rev-parse HEAD) &&
+ git reset --hard "$c0" &&
+ cp file.9y file &&
+ git add file &&
+ test_tick &&
+ git commit -m "commit 7" &&
+ git tag c7 &&
+ git reset --hard "$c0" &&
+ cp file.9 file &&
+ git add file &&
+ test_tick &&
+ git commit -m "commit 3" &&
+ git tag c3 &&
+ c3=$(git rev-parse HEAD) &&
+ git reset --hard "$c0" &&
+ create_merge_msgs
+'
+
+test_debug 'git log --graph --decorate --oneline --all'
+
+test_expect_success 'test option parsing' '
+ test_must_fail git merge -$ c1 &&
+ test_must_fail git merge --no-such c1 &&
+ test_must_fail git merge -s foobar c1 &&
+ test_must_fail git merge -s=foobar c1 &&
+ test_must_fail git merge -m &&
+ test_must_fail git merge --abort foobar &&
+ test_must_fail git merge --abort --quiet &&
+ test_must_fail git merge --continue foobar &&
+ test_must_fail git merge --continue --quiet &&
+ test_must_fail git merge
+'
+
+test_expect_success 'merge -h with invalid index' '
+ mkdir broken &&
+ (
+ cd broken &&
+ git init &&
+ >.git/index &&
+ test_expect_code 129 git merge -h 2>usage
+ ) &&
+ test_grep "[Uu]sage: git merge" broken/usage
+'
+
+test_expect_success 'reject non-strategy with a git-merge-foo name' '
+ test_must_fail git merge -s index c1
+'
+
+test_expect_success 'merge c0 with c1' '
+ echo "OBJID HEAD@{0}: merge c1: Fast-forward" >reflog.expected &&
+
+ git reset --hard c0 &&
+ git merge c1 &&
+ verify_merge file result.1 &&
+ verify_head "$c1" &&
+
+ git reflog -1 >reflog.actual &&
+ sed "s/$_x05[0-9a-f]*/OBJID/g" reflog.actual >reflog.fuzzy &&
+ test_cmp reflog.expected reflog.fuzzy
+'
+
+test_debug 'git log --graph --decorate --oneline --all'
+
+test_expect_success 'merge c0 with c1 with --ff-only' '
+ git reset --hard c0 &&
+ git merge --ff-only c1 &&
+ git merge --ff-only HEAD c0 c1 &&
+ verify_merge file result.1 &&
+ verify_head "$c1"
+'
+
+test_debug 'git log --graph --decorate --oneline --all'
+
+test_expect_success 'merge from unborn branch' '
+ git checkout -f main &&
+ test_might_fail git branch -D kid &&
+
+ echo "OBJID HEAD@{0}: initial pull" >reflog.expected &&
+
+ git checkout --orphan kid &&
+ test_when_finished "git checkout -f main" &&
+ git rm -fr . &&
+ test_tick &&
+ git merge --ff-only c1 &&
+ verify_merge file result.1 &&
+ verify_head "$c1" &&
+
+ git reflog -1 >reflog.actual &&
+ sed "s/$_x05[0-9a-f][0-9a-f]/OBJID/g" reflog.actual >reflog.fuzzy &&
+ test_cmp reflog.expected reflog.fuzzy
+'
+
+test_debug 'git log --graph --decorate --oneline --all'
+
+test_expect_success 'merge c1 with c2' '
+ git reset --hard c1 &&
+ test_tick &&
+ git merge c2 &&
+ verify_merge file result.1-5 msg.1-5 &&
+ verify_parents $c1 $c2
+'
+
+test_expect_success 'merge --squash c3 with c7' '
+ git reset --hard c3 &&
+ test_must_fail git merge --squash c7 &&
+ cat result.9z >file &&
+ git commit --no-edit -a &&
+
+ cat >expect <<-EOF &&
+ Squashed commit of the following:
+
+ $(git show -s c7)
+
+ # Conflicts:
+ # file
+ EOF
+ git cat-file commit HEAD >raw &&
+ sed -e "1,/^$/d" raw >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'merge --squash --autostash conflict does not attempt to apply autostash' '
+ git reset --hard c3 &&
+ >unrelated &&
+ git add unrelated &&
+ test_must_fail git merge --squash c7 --autostash >out 2>err &&
+ ! grep "Applying autostash resulted in conflicts." err &&
+ grep "When finished, apply stashed changes with \`git stash pop\`" out
+'
+
+test_expect_success 'merge c3 with c7 with commit.cleanup = scissors' '
+ git config commit.cleanup scissors &&
+ git reset --hard c3 &&
+ test_must_fail git merge c7 &&
+ cat result.9z >file &&
+ git commit --no-edit -a &&
+
+ cat >expect <<-\EOF &&
+ Merge tag '"'"'c7'"'"'
+
+ # ------------------------ >8 ------------------------
+ # Do not modify or remove the line above.
+ # Everything below it will be ignored.
+ #
+ # Conflicts:
+ # file
+ EOF
+ git cat-file commit HEAD >raw &&
+ sed -e "1,/^$/d" raw >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'merge c3 with c7 with --squash commit.cleanup = scissors' '
+ git config commit.cleanup scissors &&
+ git reset --hard c3 &&
+ test_must_fail git merge --squash c7 &&
+ cat result.9z >file &&
+ git commit --no-edit -a &&
+
+ cat >expect <<-EOF &&
+ Squashed commit of the following:
+
+ $(git show -s c7)
+
+ # ------------------------ >8 ------------------------
+ # Do not modify or remove the line above.
+ # Everything below it will be ignored.
+ #
+ # Conflicts:
+ # file
+ EOF
+ git cat-file commit HEAD >raw &&
+ sed -e "1,/^$/d" raw >actual &&
+ test_cmp expect actual
+'
+
+test_debug 'git log --graph --decorate --oneline --all'
+
+test_expect_success 'merge c1 with c2 and c3' '
+ git reset --hard c1 &&
+ test_tick &&
+ git merge c2 c3 &&
+ verify_merge file result.1-5-9 msg.1-5-9 &&
+ verify_parents $c1 $c2 $c3
+'
+
+test_debug 'git log --graph --decorate --oneline --all'
+
+test_expect_success 'merges with --ff-only' '
+ git reset --hard c1 &&
+ test_tick &&
+ test_must_fail git merge --ff-only c2 &&
+ test_must_fail git merge --ff-only c3 &&
+ test_must_fail git merge --ff-only c2 c3 &&
+ git reset --hard c0 &&
+ git merge c3 &&
+ verify_head $c3
+'
+
+test_expect_success 'merges with merge.ff=only' '
+ git reset --hard c1 &&
+ test_tick &&
+ test_config merge.ff "only" &&
+ test_must_fail git merge c2 &&
+ test_must_fail git merge c3 &&
+ test_must_fail git merge c2 c3 &&
+ git reset --hard c0 &&
+ git merge c3 &&
+ verify_head $c3
+'
+
+test_expect_success 'merge c0 with c1 (no-commit)' '
+ git reset --hard c0 &&
+ git merge --no-commit c1 &&
+ verify_merge file result.1 &&
+ verify_head $c1
+'
+
+test_debug 'git log --graph --decorate --oneline --all'
+
+test_expect_success 'merge c1 with c2 (no-commit)' '
+ git reset --hard c1 &&
+ git merge --no-commit c2 &&
+ verify_merge file result.1-5 &&
+ verify_head $c1 &&
+ verify_mergeheads $c2
+'
+
+test_debug 'git log --graph --decorate --oneline --all'
+
+test_expect_success 'merge c1 with c2 and c3 (no-commit)' '
+ git reset --hard c1 &&
+ git merge --no-commit c2 c3 &&
+ verify_merge file result.1-5-9 &&
+ verify_head $c1 &&
+ verify_mergeheads $c2 $c3
+'
+
+test_debug 'git log --graph --decorate --oneline --all'
+
+test_expect_success 'merge c0 with c1 (squash)' '
+ git reset --hard c0 &&
+ git merge --squash c1 &&
+ verify_merge file result.1 &&
+ verify_head $c0 &&
+ verify_no_mergehead &&
+ test_cmp squash.1 .git/SQUASH_MSG
+'
+
+test_debug 'git log --graph --decorate --oneline --all'
+
+test_expect_success 'merge c0 with c1 (squash, ff-only)' '
+ git reset --hard c0 &&
+ git merge --squash --ff-only c1 &&
+ verify_merge file result.1 &&
+ verify_head $c0 &&
+ verify_no_mergehead &&
+ test_cmp squash.1 .git/SQUASH_MSG
+'
+
+test_debug 'git log --graph --decorate --oneline --all'
+
+test_expect_success 'merge c1 with c2 (squash)' '
+ git reset --hard c1 &&
+ git merge --squash c2 &&
+ verify_merge file result.1-5 &&
+ verify_head $c1 &&
+ verify_no_mergehead &&
+ test_cmp squash.1-5 .git/SQUASH_MSG
+'
+
+test_debug 'git log --graph --decorate --oneline --all'
+
+test_expect_success 'unsuccessful merge of c1 with c2 (squash, ff-only)' '
+ git reset --hard c1 &&
+ test_must_fail git merge --squash --ff-only c2
+'
+
+test_debug 'git log --graph --decorate --oneline --all'
+
+test_expect_success 'merge c1 with c2 and c3 (squash)' '
+ git reset --hard c1 &&
+ git merge --squash c2 c3 &&
+ verify_merge file result.1-5-9 &&
+ verify_head $c1 &&
+ verify_no_mergehead &&
+ test_cmp squash.1-5-9 .git/SQUASH_MSG
+'
+
+test_debug 'git log --graph --decorate --oneline --all'
+
+test_expect_success 'merge c1 with c2 (no-commit in config)' '
+ git reset --hard c1 &&
+ test_config branch.main.mergeoptions "--no-commit" &&
+ git merge c2 &&
+ verify_merge file result.1-5 &&
+ verify_head $c1 &&
+ verify_mergeheads $c2
+'
+
+test_debug 'git log --graph --decorate --oneline --all'
+
+test_expect_success 'merge c1 with c2 (log in config)' '
+ git reset --hard c1 &&
+ git merge --log c2 &&
+ git show -s --pretty=tformat:%s%n%b >expect &&
+
+ test_config branch.main.mergeoptions "--log" &&
+ git reset --hard c1 &&
+ git merge c2 &&
+ git show -s --pretty=tformat:%s%n%b >actual &&
+
+ test_cmp expect actual
+'
+
+test_expect_success 'merge c1 with c2 (log in config gets overridden)' '
+ git reset --hard c1 &&
+ git merge c2 &&
+ git show -s --pretty=tformat:%s%n%b >expect &&
+
+ test_config branch.main.mergeoptions "--no-log" &&
+ test_config merge.log "true" &&
+ git reset --hard c1 &&
+ git merge c2 &&
+ git show -s --pretty=tformat:%s%n%b >actual &&
+
+ test_cmp expect actual
+'
+
+test_expect_success 'merge c1 with c2 (squash in config)' '
+ git reset --hard c1 &&
+ test_config branch.main.mergeoptions "--squash" &&
+ git merge c2 &&
+ verify_merge file result.1-5 &&
+ verify_head $c1 &&
+ verify_no_mergehead &&
+ test_cmp squash.1-5 .git/SQUASH_MSG
+'
+
+test_debug 'git log --graph --decorate --oneline --all'
+
+test_expect_success 'override config option -n with --summary' '
+ git reset --hard c1 &&
+ test_config branch.main.mergeoptions "-n" &&
+ test_tick &&
+ git merge --summary c2 >diffstat.txt &&
+ verify_merge file result.1-5 msg.1-5 &&
+ verify_parents $c1 $c2 &&
+ if ! grep "^ file | *2 +-$" diffstat.txt
+ then
+ echo "[OOPS] diffstat was not generated with --summary"
+ false
+ fi
+'
+
+test_expect_success 'override config option -n with --stat' '
+ git reset --hard c1 &&
+ test_config branch.main.mergeoptions "-n" &&
+ test_tick &&
+ git merge --stat c2 >diffstat.txt &&
+ verify_merge file result.1-5 msg.1-5 &&
+ verify_parents $c1 $c2 &&
+ if ! grep "^ file | *2 +-$" diffstat.txt
+ then
+ echo "[OOPS] diffstat was not generated with --stat"
+ false
+ fi
+'
+
+test_debug 'git log --graph --decorate --oneline --all'
+
+test_expect_success 'override config option --stat' '
+ git reset --hard c1 &&
+ test_config branch.main.mergeoptions "--stat" &&
+ test_tick &&
+ git merge -n c2 >diffstat.txt &&
+ verify_merge file result.1-5 msg.1-5 &&
+ verify_parents $c1 $c2 &&
+ if grep "^ file | *2 +-$" diffstat.txt
+ then
+ echo "[OOPS] diffstat was generated"
+ false
+ fi
+'
+
+test_debug 'git log --graph --decorate --oneline --all'
+
+test_expect_success 'merge c1 with c2 (override --no-commit)' '
+ git reset --hard c1 &&
+ test_config branch.main.mergeoptions "--no-commit" &&
+ test_tick &&
+ git merge --commit c2 &&
+ verify_merge file result.1-5 msg.1-5 &&
+ verify_parents $c1 $c2
+'
+
+test_debug 'git log --graph --decorate --oneline --all'
+
+test_expect_success 'merge c1 with c2 (override --squash)' '
+ git reset --hard c1 &&
+ test_config branch.main.mergeoptions "--squash" &&
+ test_tick &&
+ git merge --no-squash c2 &&
+ verify_merge file result.1-5 msg.1-5 &&
+ verify_parents $c1 $c2
+'
+
+test_debug 'git log --graph --decorate --oneline --all'
+
+test_expect_success 'merge c0 with c1 (no-ff)' '
+ git reset --hard c0 &&
+ test_tick &&
+ git merge --no-ff c1 &&
+ verify_merge file result.1 &&
+ verify_parents $c0 $c1
+'
+
+test_debug 'git log --graph --decorate --oneline --all'
+
+test_expect_success 'merge c0 with c1 (merge.ff=false)' '
+ git reset --hard c0 &&
+ test_config merge.ff "false" &&
+ test_tick &&
+ git merge c1 &&
+ verify_merge file result.1 &&
+ verify_parents $c0 $c1
+'
+test_debug 'git log --graph --decorate --oneline --all'
+
+test_expect_success 'combine branch.main.mergeoptions with merge.ff' '
+ git reset --hard c0 &&
+ test_config branch.main.mergeoptions "--ff" &&
+ test_config merge.ff "false" &&
+ test_tick &&
+ git merge c1 &&
+ verify_merge file result.1 &&
+ verify_parents "$c0"
+'
+
+test_expect_success 'tolerate unknown values for merge.ff' '
+ git reset --hard c0 &&
+ test_config merge.ff "something-new" &&
+ test_tick &&
+ git merge c1 2>message &&
+ verify_head "$c1" &&
+ test_must_be_empty message
+'
+
+test_expect_success 'combining --squash and --no-ff is refused' '
+ git reset --hard c0 &&
+ test_must_fail git merge --squash --no-ff c1 &&
+ test_must_fail git merge --no-ff --squash c1
+'
+
+test_expect_success 'combining --squash and --commit is refused' '
+ git reset --hard c0 &&
+ test_must_fail git merge --squash --commit c1 &&
+ test_must_fail git merge --commit --squash c1
+'
+
+test_expect_success 'option --ff-only overwrites --no-ff' '
+ git merge --no-ff --ff-only c1 &&
+ test_must_fail git merge --no-ff --ff-only c2
+'
+
+test_expect_success 'option --no-ff overrides merge.ff=only config' '
+ git reset --hard c0 &&
+ test_config merge.ff only &&
+ git merge --no-ff c1
+'
+
+test_expect_success 'merge c0 with c1 (ff overrides no-ff)' '
+ git reset --hard c0 &&
+ test_config branch.main.mergeoptions "--no-ff" &&
+ git merge --ff c1 &&
+ verify_merge file result.1 &&
+ verify_head $c1
+'
+
+test_expect_success 'merge log message' '
+ git reset --hard c0 &&
+ git merge --no-log c2 &&
+ git show -s --pretty=format:%b HEAD >msg.act &&
+ test_must_be_empty msg.act &&
+
+ git reset --hard c0 &&
+ test_config branch.main.mergeoptions "--no-ff" &&
+ git merge --no-log c2 &&
+ git show -s --pretty=format:%b HEAD >msg.act &&
+ test_must_be_empty msg.act &&
+
+ git merge --log c3 &&
+ git show -s --pretty=format:%b HEAD >msg.act &&
+ test_cmp msg.log msg.act &&
+
+ git reset --hard HEAD^ &&
+ test_config merge.log "yes" &&
+ git merge c3 &&
+ git show -s --pretty=format:%b HEAD >msg.act &&
+ test_cmp msg.log msg.act
+'
+
+test_debug 'git log --graph --decorate --oneline --all'
+
+test_expect_success 'merge c1 with c0, c2, c0, and c1' '
+ git reset --hard c1 &&
+ test_tick &&
+ git merge c0 c2 c0 c1 &&
+ verify_merge file result.1-5 &&
+ verify_parents $c1 $c2
+'
+
+test_debug 'git log --graph --decorate --oneline --all'
+
+test_expect_success 'merge c1 with c0, c2, c0, and c1' '
+ git reset --hard c1 &&
+ test_tick &&
+ git merge c0 c2 c0 c1 &&
+ verify_merge file result.1-5 &&
+ verify_parents $c1 $c2
+'
+
+test_debug 'git log --graph --decorate --oneline --all'
+
+test_expect_success 'merge c1 with c1 and c2' '
+ git reset --hard c1 &&
+ test_tick &&
+ git merge c1 c2 &&
+ verify_merge file result.1-5 &&
+ verify_parents $c1 $c2
+'
+
+test_debug 'git log --graph --decorate --oneline --all'
+
+test_expect_success 'merge fast-forward in a dirty tree' '
+ git reset --hard c0 &&
+ mv file file1 &&
+ cat file1 >file &&
+ rm -f file1 &&
+ git merge c2
+'
+
+test_debug 'git log --graph --decorate --oneline --all'
+
+test_expect_success 'in-index merge' '
+ git reset --hard c0 &&
+ git merge --no-ff -s resolve c1 >out &&
+ test_grep "Wonderful." out &&
+ verify_parents $c0 $c1
+'
+
+test_debug 'git log --graph --decorate --oneline --all'
+
+test_expect_success 'refresh the index before merging' '
+ git reset --hard c1 &&
+ cp file file.n && mv -f file.n file &&
+ git merge c3
+'
+
+test_expect_success 'merge with --autostash' '
+ git reset --hard c1 &&
+ git merge-file file file.orig file.9 &&
+ git merge --autostash c2 2>err &&
+ test_grep "Applied autostash." err &&
+ git show HEAD:file >merge-result &&
+ test_cmp result.1-5 merge-result &&
+ test_cmp result.1-5-9 file
+'
+
+test_expect_success 'merge with merge.autoStash' '
+ test_config merge.autoStash true &&
+ git reset --hard c1 &&
+ git merge-file file file.orig file.9 &&
+ git merge c2 2>err &&
+ test_grep "Applied autostash." err &&
+ git show HEAD:file >merge-result &&
+ test_cmp result.1-5 merge-result &&
+ test_cmp result.1-5-9 file
+'
+
+test_expect_success 'fast-forward merge with --autostash' '
+ git reset --hard c0 &&
+ git merge-file file file.orig file.5 &&
+ git merge --autostash c1 2>err &&
+ test_grep "Applied autostash." err &&
+ test_cmp result.1-5 file
+'
+
+test_expect_success 'failed fast-forward merge with --autostash' '
+ git reset --hard c0 &&
+ git merge-file file file.orig file.5 &&
+ cp file.5 other &&
+ test_when_finished "rm other" &&
+ test_must_fail git merge --autostash c1 2>err &&
+ test_grep "Applied autostash." err &&
+ test_cmp file.5 file
+'
+
+test_expect_success 'octopus merge with --autostash' '
+ git reset --hard c1 &&
+ git merge-file file file.orig file.3 &&
+ git merge --autostash c2 c3 2>err &&
+ test_grep "Applied autostash." err &&
+ git show HEAD:file >merge-result &&
+ test_cmp result.1-5-9 merge-result &&
+ test_cmp result.1-3-5-9 file
+'
+
+test_expect_success 'failed merge (exit 2) with --autostash' '
+ git reset --hard c1 &&
+ git merge-file file file.orig file.5 &&
+ test_must_fail git merge -s recursive --autostash c2 c3 2>err &&
+ test_grep "Applied autostash." err &&
+ test_cmp result.1-5 file
+'
+
+test_expect_success 'conflicted merge with --autostash, --abort restores stash' '
+ git reset --hard c3 &&
+ cp file.1 file &&
+ test_must_fail git merge --autostash c7 &&
+ git merge --abort 2>err &&
+ test_grep "Applied autostash." err &&
+ test_cmp file.1 file
+'
+
+test_expect_success 'completed merge (git commit) with --no-commit and --autostash' '
+ git reset --hard c1 &&
+ git merge-file file file.orig file.9 &&
+ git diff >expect &&
+ git merge --no-commit --autostash c2 &&
+ git stash show -p MERGE_AUTOSTASH >actual &&
+ test_cmp expect actual &&
+ git commit 2>err &&
+ test_grep "Applied autostash." err &&
+ git show HEAD:file >merge-result &&
+ test_cmp result.1-5 merge-result &&
+ test_cmp result.1-5-9 file
+'
+
+test_expect_success 'completed merge (git merge --continue) with --no-commit and --autostash' '
+ git reset --hard c1 &&
+ git merge-file file file.orig file.9 &&
+ git diff >expect &&
+ git merge --no-commit --autostash c2 &&
+ git stash show -p MERGE_AUTOSTASH >actual &&
+ test_cmp expect actual &&
+ git merge --continue 2>err &&
+ test_grep "Applied autostash." err &&
+ git show HEAD:file >merge-result &&
+ test_cmp result.1-5 merge-result &&
+ test_cmp result.1-5-9 file
+'
+
+test_expect_success 'aborted merge (merge --abort) with --no-commit and --autostash' '
+ git reset --hard c1 &&
+ git merge-file file file.orig file.9 &&
+ git diff >expect &&
+ git merge --no-commit --autostash c2 &&
+ git stash show -p MERGE_AUTOSTASH >actual &&
+ test_cmp expect actual &&
+ git merge --abort 2>err &&
+ test_grep "Applied autostash." err &&
+ git diff >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'aborted merge (reset --hard) with --no-commit and --autostash' '
+ git reset --hard c1 &&
+ git merge-file file file.orig file.9 &&
+ git diff >expect &&
+ git merge --no-commit --autostash c2 &&
+ git stash show -p MERGE_AUTOSTASH >actual &&
+ test_cmp expect actual &&
+ git reset --hard 2>err &&
+ test_grep "Autostash exists; creating a new stash entry." err &&
+ git diff --exit-code
+'
+
+test_expect_success 'quit merge with --no-commit and --autostash' '
+ git reset --hard c1 &&
+ git merge-file file file.orig file.9 &&
+ git diff >expect &&
+ git merge --no-commit --autostash c2 &&
+ git stash show -p MERGE_AUTOSTASH >actual &&
+ test_cmp expect actual &&
+ git diff HEAD >expect &&
+ git merge --quit 2>err &&
+ test_grep "Autostash exists; creating a new stash entry." err &&
+ git diff HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'merge with conflicted --autostash changes' '
+ git reset --hard c1 &&
+ git merge-file file file.orig file.9y &&
+ git diff >expect &&
+ test_when_finished "test_might_fail git stash drop" &&
+ git merge --autostash c3 2>err &&
+ test_grep "Applying autostash resulted in conflicts." err &&
+ git show HEAD:file >merge-result &&
+ test_cmp result.1-9 merge-result &&
+ git stash show -p >actual &&
+ test_cmp expect actual
+'
+
+cat >expected.branch <<\EOF
+Merge branch 'c5-branch' (early part)
+EOF
+cat >expected.tag <<\EOF
+Merge commit 'c5~1'
+EOF
+
+test_expect_success 'merge early part of c2' '
+ git reset --hard c3 &&
+ echo c4 >c4.c &&
+ git add c4.c &&
+ git commit -m c4 &&
+ git tag c4 &&
+ echo c5 >c5.c &&
+ git add c5.c &&
+ git commit -m c5 &&
+ git tag c5 &&
+ git reset --hard c3 &&
+ echo c6 >c6.c &&
+ git add c6.c &&
+ git commit -m c6 &&
+ git tag c6 &&
+ git branch -f c5-branch c5 &&
+ git merge c5-branch~1 &&
+ git show -s --pretty=tformat:%s HEAD >actual.branch &&
+ git reset --keep HEAD^ &&
+ git merge c5~1 &&
+ git show -s --pretty=tformat:%s HEAD >actual.tag &&
+ test_cmp expected.branch actual.branch &&
+ test_cmp expected.tag actual.tag
+'
+
+test_debug 'git log --graph --decorate --oneline --all'
+
+test_expect_success 'merge --no-ff --no-commit && commit' '
+ git reset --hard c0 &&
+ git merge --no-ff --no-commit c1 &&
+ EDITOR=: git commit &&
+ verify_parents $c0 $c1
+'
+
+test_debug 'git log --graph --decorate --oneline --all'
+
+test_expect_success 'amending no-ff merge commit' '
+ EDITOR=: git commit --amend &&
+ verify_parents $c0 $c1
+'
+
+test_debug 'git log --graph --decorate --oneline --all'
+
+cat >editor <<\EOF
+#!/bin/sh
+# Add a new message string that was not in the template
+(
+ echo "Merge work done on the side branch c1"
+ echo
+ cat "$1"
+) >"$1.tmp" && mv "$1.tmp" "$1"
+# strip comments and blank lines from end of message
+sed -e '/^#/d' "$1" | sed -e :a -e '/^\n*$/{$d;N;ba' -e '}' >expected
+EOF
+chmod 755 editor
+
+test_expect_success 'merge --no-ff --edit' '
+ git reset --hard c0 &&
+ EDITOR=./editor git merge --no-ff --edit c1 &&
+ verify_parents $c0 $c1 &&
+ git cat-file commit HEAD >raw &&
+ grep "work done on the side branch" raw &&
+ sed "1,/^$/d" >actual raw &&
+ test_cmp expected actual
+'
+
+test_expect_success 'merge annotated/signed tag w/o tracking' '
+ test_when_finished "rm -rf dst; git tag -d anno1" &&
+ git tag -a -m "anno c1" anno1 c1 &&
+ git init dst &&
+ git rev-parse c1 >dst/expect &&
+ (
+ # c0 fast-forwards to c1 but because this repository
+ # is not a "downstream" whose refs/tags follows along
+ # tag from the "upstream", this pull defaults to --no-ff
+ cd dst &&
+ git pull .. c0 &&
+ git pull .. anno1 &&
+ git rev-parse HEAD^2 >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'merge annotated/signed tag w/ tracking' '
+ test_when_finished "rm -rf dst; git tag -d anno1" &&
+ git tag -a -m "anno c1" anno1 c1 &&
+ git init dst &&
+ git rev-parse c1 >dst/expect &&
+ (
+ # c0 fast-forwards to c1 and because this repository
+ # is a "downstream" whose refs/tags follows along
+ # tag from the "upstream", this pull defaults to --ff
+ cd dst &&
+ git remote add origin .. &&
+ git pull origin c0 &&
+ git fetch origin &&
+ git merge anno1 &&
+ git rev-parse HEAD >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success GPG 'merge --ff-only tag' '
+ git reset --hard c0 &&
+ git commit --allow-empty -m "A newer commit" &&
+ git tag -s -m "A newer commit" signed &&
+ git reset --hard c0 &&
+
+ git merge --ff-only signed &&
+ git rev-parse signed^0 >expect &&
+ git rev-parse HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success GPG 'merge --no-edit tag should skip editor' '
+ git reset --hard c0 &&
+ git commit --allow-empty -m "A newer commit" &&
+ git tag -f -s -m "A newer commit" signed &&
+ git reset --hard c0 &&
+
+ EDITOR=false git merge --no-edit --no-ff signed &&
+ git rev-parse signed^0 >expect &&
+ git rev-parse HEAD^2 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'set up mod-256 conflict scenario' '
+ # 256 near-identical stanzas...
+ for i in $(test_seq 1 256); do
+ for j in 1 2 3 4 5; do
+ echo $i-$j || return 1
+ done
+ done >file &&
+ git add file &&
+ git commit -m base &&
+
+ # one side changes the first line of each to "main"
+ sed s/-1/-main/ file >tmp &&
+ mv tmp file &&
+ git commit -am main &&
+
+ # and the other to "side"; merging the two will
+ # yield 256 separate conflicts
+ git checkout -b side HEAD^ &&
+ sed s/-1/-side/ file >tmp &&
+ mv tmp file &&
+ git commit -am side
+'
+
+test_expect_success 'merge detects mod-256 conflicts (recursive)' '
+ git reset --hard &&
+ test_must_fail git merge -s recursive main
+'
+
+test_expect_success 'merge detects mod-256 conflicts (resolve)' '
+ git reset --hard &&
+ test_must_fail git merge -s resolve main
+'
+
+test_expect_success 'merge nothing into void' '
+ git init void &&
+ (
+ cd void &&
+ git remote add up .. &&
+ git fetch up &&
+ test_must_fail git merge FETCH_HEAD
+ )
+'
+
+test_expect_success 'merge can be completed with --continue' '
+ git reset --hard c0 &&
+ git merge --no-ff --no-commit c1 &&
+ git merge --continue &&
+ verify_parents $c0 $c1
+'
+
+write_script .git/FAKE_EDITOR <<EOF
+# kill -TERM command added below.
+EOF
+
+test_expect_success EXECKEEPSPID 'killed merge can be completed with --continue' '
+ git reset --hard c0 &&
+ ! "$SHELL_PATH" -c '\''
+ echo kill -TERM $$ >>.git/FAKE_EDITOR
+ GIT_EDITOR=.git/FAKE_EDITOR
+ export GIT_EDITOR
+ exec git merge --no-ff --edit c1'\'' &&
+ git merge --continue &&
+ verify_parents $c0 $c1
+'
+
+test_expect_success 'merge --quit' '
+ git init merge-quit &&
+ (
+ cd merge-quit &&
+ test_commit base &&
+ echo one >>base.t &&
+ git commit -am one &&
+ git branch one &&
+ git checkout base &&
+ echo two >>base.t &&
+ git commit -am two &&
+ test_must_fail git -c rerere.enabled=true merge one &&
+ test_path_is_file .git/MERGE_HEAD &&
+ test_path_is_file .git/MERGE_MODE &&
+ test_path_is_file .git/MERGE_MSG &&
+ git rerere status >rerere.before &&
+ git merge --quit &&
+ test_path_is_missing .git/MERGE_HEAD &&
+ test_path_is_missing .git/MERGE_MODE &&
+ test_path_is_missing .git/MERGE_MSG &&
+ git rerere status >rerere.after &&
+ test_must_be_empty rerere.after &&
+ ! test_cmp rerere.after rerere.before
+ )
+'
+
+test_expect_success 'merge suggests matching remote refname' '
+ git commit --allow-empty -m not-local &&
+ git update-ref refs/remotes/origin/not-local HEAD &&
+ git reset --hard HEAD^ &&
+
+ # This is white-box testing hackery; we happen to know
+ # that reading packed refs is more picky about the memory
+ # ownership of strings we pass to for_each_ref() callbacks.
+ git pack-refs --all --prune &&
+
+ test_must_fail git merge not-local 2>stderr &&
+ grep origin/not-local stderr
+'
+
+test_expect_success 'suggested names are not ambiguous' '
+ git update-ref refs/heads/origin/not-local HEAD &&
+ test_must_fail git merge not-local 2>stderr &&
+ grep remotes/origin/not-local stderr
+'
+
+test_done
diff --git a/t/t7601-merge-pull-config.sh b/t/t7601-merge-pull-config.sh
new file mode 100755
index 0000000..a94387a
--- /dev/null
+++ b/t/t7601-merge-pull-config.sh
@@ -0,0 +1,525 @@
+#!/bin/sh
+
+test_description='git merge
+
+Testing pull.* configuration parsing and other things.'
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ echo c0 >c0.c &&
+ git add c0.c &&
+ git commit -m c0 &&
+ git tag c0 &&
+ echo c1 >c1.c &&
+ git add c1.c &&
+ git commit -m c1 &&
+ git tag c1 &&
+ git reset --hard c0 &&
+ echo c2 >c2.c &&
+ git add c2.c &&
+ git commit -m c2 &&
+ git tag c2 &&
+ git reset --hard c0 &&
+ echo c3 >c3.c &&
+ git add c3.c &&
+ git commit -m c3 &&
+ git tag c3
+'
+
+test_expect_success 'pull.rebase not set, ff possible' '
+ git reset --hard c0 &&
+ git pull . c1 2>err &&
+ test_grep ! "You have divergent branches" err
+'
+
+test_expect_success 'pull.rebase not set and pull.ff=true' '
+ git reset --hard c0 &&
+ test_config pull.ff true &&
+ git pull . c1 2>err &&
+ test_grep ! "You have divergent branches" err
+'
+
+test_expect_success 'pull.rebase not set and pull.ff=false' '
+ git reset --hard c0 &&
+ test_config pull.ff false &&
+ git pull . c1 2>err &&
+ test_grep ! "You have divergent branches" err
+'
+
+test_expect_success 'pull.rebase not set and pull.ff=only' '
+ git reset --hard c0 &&
+ test_config pull.ff only &&
+ git pull . c1 2>err &&
+ test_grep ! "You have divergent branches" err
+'
+
+test_expect_success 'pull.rebase not set and --rebase given' '
+ git reset --hard c0 &&
+ git pull --rebase . c1 2>err &&
+ test_grep ! "You have divergent branches" err
+'
+
+test_expect_success 'pull.rebase not set and --no-rebase given' '
+ git reset --hard c0 &&
+ git pull --no-rebase . c1 2>err &&
+ test_grep ! "You have divergent branches" err
+'
+
+test_expect_success 'pull.rebase not set and --ff given' '
+ git reset --hard c0 &&
+ git pull --ff . c1 2>err &&
+ test_grep ! "You have divergent branches" err
+'
+
+test_expect_success 'pull.rebase not set and --no-ff given' '
+ git reset --hard c0 &&
+ git pull --no-ff . c1 2>err &&
+ test_grep ! "You have divergent branches" err
+'
+
+test_expect_success 'pull.rebase not set and --ff-only given' '
+ git reset --hard c0 &&
+ git pull --ff-only . c1 2>err &&
+ test_grep ! "You have divergent branches" err
+'
+
+test_expect_success 'pull.rebase not set (not-fast-forward)' '
+ git reset --hard c2 &&
+ test_must_fail git -c color.advice=always pull . c1 2>err &&
+ test_decode_color <err >decoded &&
+ test_grep "<YELLOW>hint: " decoded &&
+ test_grep "You have divergent branches" decoded
+'
+
+test_expect_success 'pull.rebase not set and pull.ff=true (not-fast-forward)' '
+ git reset --hard c2 &&
+ test_config pull.ff true &&
+ git pull . c1 2>err &&
+ test_grep ! "You have divergent branches" err
+'
+
+test_expect_success 'pull.rebase not set and pull.ff=false (not-fast-forward)' '
+ git reset --hard c2 &&
+ test_config pull.ff false &&
+ git pull . c1 2>err &&
+ test_grep ! "You have divergent branches" err
+'
+
+test_expect_success 'pull.rebase not set and pull.ff=only (not-fast-forward)' '
+ git reset --hard c2 &&
+ test_config pull.ff only &&
+ test_must_fail git pull . c1 2>err &&
+ test_grep ! "You have divergent branches" err
+'
+
+test_expect_success 'pull.rebase not set and --rebase given (not-fast-forward)' '
+ git reset --hard c2 &&
+ git pull --rebase . c1 2>err &&
+ test_grep ! "You have divergent branches" err
+'
+
+test_expect_success 'pull.rebase not set and --no-rebase given (not-fast-forward)' '
+ git reset --hard c2 &&
+ git pull --no-rebase . c1 2>err &&
+ test_grep ! "You have divergent branches" err
+'
+
+test_expect_success 'pull.rebase not set and --ff given (not-fast-forward)' '
+ git reset --hard c2 &&
+ git pull --ff . c1 2>err &&
+ test_grep ! "You have divergent branches" err
+'
+
+test_expect_success 'pull.rebase not set and --no-ff given (not-fast-forward)' '
+ git reset --hard c2 &&
+ git pull --no-ff . c1 2>err &&
+ test_grep ! "You have divergent branches" err
+'
+
+test_expect_success 'pull.rebase not set and --ff-only given (not-fast-forward)' '
+ git reset --hard c2 &&
+ test_must_fail git pull --ff-only . c1 2>err &&
+ test_grep ! "You have divergent branches" err
+'
+
+test_does_rebase () {
+ git reset --hard c2 &&
+ git "$@" . c1 &&
+ # Check that we actually did a rebase
+ git rev-list --count HEAD >actual &&
+ git rev-list --merges --count HEAD >>actual &&
+ test_write_lines 3 0 >expect &&
+ test_cmp expect actual &&
+ rm actual expect
+}
+
+# Prefers merge over fast-forward
+test_does_merge_when_ff_possible () {
+ git reset --hard c0 &&
+ git "$@" . c1 &&
+ # Check that we actually did a merge
+ git rev-list --count HEAD >actual &&
+ git rev-list --merges --count HEAD >>actual &&
+ test_write_lines 3 1 >expect &&
+ test_cmp expect actual &&
+ rm actual expect
+}
+
+# Prefers fast-forward over merge or rebase
+test_does_fast_forward () {
+ git reset --hard c0 &&
+ git "$@" . c1 &&
+
+ # Check that we did not get any merges
+ git rev-list --count HEAD >actual &&
+ git rev-list --merges --count HEAD >>actual &&
+ test_write_lines 2 0 >expect &&
+ test_cmp expect actual &&
+
+ # Check that we ended up at c1
+ git rev-parse HEAD >actual &&
+ git rev-parse c1^{commit} >expect &&
+ test_cmp actual expect &&
+
+ # Remove temporary files
+ rm actual expect
+}
+
+# Doesn't fail when fast-forward not possible; does a merge
+test_falls_back_to_full_merge () {
+ git reset --hard c2 &&
+ git "$@" . c1 &&
+ # Check that we actually did a merge
+ git rev-list --count HEAD >actual &&
+ git rev-list --merges --count HEAD >>actual &&
+ test_write_lines 4 1 >expect &&
+ test_cmp expect actual &&
+ rm actual expect
+}
+
+# Attempts fast forward, which is impossible, and bails
+test_attempts_fast_forward () {
+ git reset --hard c2 &&
+ test_must_fail git "$@" . c1 2>err &&
+ test_grep "Not possible to fast-forward, aborting" err
+}
+
+#
+# Group 1: Interaction of --ff-only with --[no-]rebase
+# (And related interaction of pull.ff=only with pull.rebase)
+#
+test_expect_success '--ff-only overrides --rebase' '
+ test_attempts_fast_forward pull --rebase --ff-only
+'
+
+test_expect_success '--ff-only overrides --rebase even if first' '
+ test_attempts_fast_forward pull --ff-only --rebase
+'
+
+test_expect_success '--ff-only overrides --no-rebase' '
+ test_attempts_fast_forward pull --ff-only --no-rebase
+'
+
+test_expect_success 'pull.ff=only overrides pull.rebase=true' '
+ test_attempts_fast_forward -c pull.ff=only -c pull.rebase=true pull
+'
+
+test_expect_success 'pull.ff=only overrides pull.rebase=false' '
+ test_attempts_fast_forward -c pull.ff=only -c pull.rebase=false pull
+'
+
+# Group 2: --rebase=[!false] overrides --no-ff and --ff
+# (And related interaction of pull.rebase=!false and pull.ff=!only)
+test_expect_success '--rebase overrides --no-ff' '
+ test_does_rebase pull --rebase --no-ff
+'
+
+test_expect_success '--rebase overrides --ff' '
+ test_does_rebase pull --rebase --ff
+'
+
+test_expect_success '--rebase fast-forwards when possible' '
+ test_does_fast_forward pull --rebase --ff
+'
+
+test_expect_success 'pull.rebase=true overrides pull.ff=false' '
+ test_does_rebase -c pull.rebase=true -c pull.ff=false pull
+'
+
+test_expect_success 'pull.rebase=true overrides pull.ff=true' '
+ test_does_rebase -c pull.rebase=true -c pull.ff=true pull
+'
+
+# Group 3: command line flags take precedence over config
+test_expect_success '--ff-only takes precedence over pull.rebase=true' '
+ test_attempts_fast_forward -c pull.rebase=true pull --ff-only
+'
+
+test_expect_success '--ff-only takes precedence over pull.rebase=false' '
+ test_attempts_fast_forward -c pull.rebase=false pull --ff-only
+'
+
+test_expect_success '--no-rebase takes precedence over pull.ff=only' '
+ test_falls_back_to_full_merge -c pull.ff=only pull --no-rebase
+'
+
+test_expect_success '--rebase takes precedence over pull.ff=only' '
+ test_does_rebase -c pull.ff=only pull --rebase
+'
+
+test_expect_success '--rebase overrides pull.ff=true' '
+ test_does_rebase -c pull.ff=true pull --rebase
+'
+
+test_expect_success '--rebase overrides pull.ff=false' '
+ test_does_rebase -c pull.ff=false pull --rebase
+'
+
+test_expect_success '--rebase overrides pull.ff unset' '
+ test_does_rebase pull --rebase
+'
+
+# Group 4: --no-rebase heeds pull.ff=!only or explict --ff or --no-ff
+
+test_expect_success '--no-rebase works with --no-ff' '
+ test_does_merge_when_ff_possible pull --no-rebase --no-ff
+'
+
+test_expect_success '--no-rebase works with --ff' '
+ test_does_fast_forward pull --no-rebase --ff
+'
+
+test_expect_success '--no-rebase does ff if pull.ff unset' '
+ test_does_fast_forward pull --no-rebase
+'
+
+test_expect_success '--no-rebase heeds pull.ff=true' '
+ test_does_fast_forward -c pull.ff=true pull --no-rebase
+'
+
+test_expect_success '--no-rebase heeds pull.ff=false' '
+ test_does_merge_when_ff_possible -c pull.ff=false pull --no-rebase
+'
+
+# Group 5: pull.rebase=!false in combination with --no-ff or --ff
+test_expect_success 'pull.rebase=true and --no-ff' '
+ test_does_rebase -c pull.rebase=true pull --no-ff
+'
+
+test_expect_success 'pull.rebase=true and --ff' '
+ test_does_rebase -c pull.rebase=true pull --ff
+'
+
+test_expect_success 'pull.rebase=false and --no-ff' '
+ test_does_merge_when_ff_possible -c pull.rebase=false pull --no-ff
+'
+
+test_expect_success 'pull.rebase=false and --ff, ff possible' '
+ test_does_fast_forward -c pull.rebase=false pull --ff
+'
+
+test_expect_success 'pull.rebase=false and --ff, ff not possible' '
+ test_falls_back_to_full_merge -c pull.rebase=false pull --ff
+'
+
+# End of groupings for conflicting merge vs. rebase flags/options
+
+test_expect_success 'Multiple heads warns about inability to fast forward' '
+ git reset --hard c1 &&
+ test_must_fail git pull . c2 c3 2>err &&
+ test_grep "You have divergent branches" err
+'
+
+test_expect_success 'Multiple can never be fast forwarded' '
+ git reset --hard c0 &&
+ test_must_fail git -c pull.ff=only pull . c1 c2 c3 2>err &&
+ test_grep ! "You have divergent branches" err &&
+ # In addition to calling out "cannot fast-forward", we very much
+ # want the "multiple branches" piece to be called out to users.
+ test_grep "Cannot fast-forward to multiple branches" err
+'
+
+test_expect_success 'Cannot rebase with multiple heads' '
+ git reset --hard c0 &&
+ test_must_fail git -c pull.rebase=true pull . c1 c2 c3 2>err &&
+ test_grep ! "You have divergent branches" err &&
+ test_grep "Cannot rebase onto multiple branches." err
+'
+
+test_expect_success 'merge c1 with c2' '
+ git reset --hard c1 &&
+ test_path_is_file c0.c &&
+ test_path_is_file c1.c &&
+ test_path_is_missing c2.c &&
+ test_path_is_missing c3.c &&
+ git merge c2 &&
+ test_path_is_file c1.c &&
+ test_path_is_file c2.c
+'
+
+test_expect_success 'fast-forward pull succeeds with "true" in pull.ff' '
+ git reset --hard c0 &&
+ test_config pull.ff true &&
+ git pull . c1 &&
+ test "$(git rev-parse HEAD)" = "$(git rev-parse c1)"
+'
+
+test_expect_success 'pull.ff=true overrides merge.ff=false' '
+ git reset --hard c0 &&
+ test_config merge.ff false &&
+ test_config pull.ff true &&
+ git pull . c1 &&
+ test "$(git rev-parse HEAD)" = "$(git rev-parse c1)"
+'
+
+test_expect_success 'fast-forward pull creates merge with "false" in pull.ff' '
+ git reset --hard c0 &&
+ test_config pull.ff false &&
+ git pull . c1 &&
+ test "$(git rev-parse HEAD^1)" = "$(git rev-parse c0)" &&
+ test "$(git rev-parse HEAD^2)" = "$(git rev-parse c1)"
+'
+
+test_expect_success 'pull prevents non-fast-forward with "only" in pull.ff' '
+ git reset --hard c1 &&
+ test_config pull.ff only &&
+ test_must_fail git pull . c3
+'
+
+test_expect_success 'already-up-to-date pull succeeds with unspecified pull.ff' '
+ git reset --hard c1 &&
+ git pull . c0 &&
+ test "$(git rev-parse HEAD)" = "$(git rev-parse c1)"
+'
+
+test_expect_success 'already-up-to-date pull succeeds with "only" in pull.ff' '
+ git reset --hard c1 &&
+ test_config pull.ff only &&
+ git pull . c0 &&
+ test "$(git rev-parse HEAD)" = "$(git rev-parse c1)"
+'
+
+test_expect_success 'already-up-to-date pull/rebase succeeds with "only" in pull.ff' '
+ git reset --hard c1 &&
+ test_config pull.ff only &&
+ git -c pull.rebase=true pull . c0 &&
+ test "$(git rev-parse HEAD)" = "$(git rev-parse c1)"
+'
+
+test_expect_success 'merge c1 with c2 (ours in pull.twohead)' '
+ git reset --hard c1 &&
+ git config pull.twohead ours &&
+ git merge c2 &&
+ test_path_is_file c1.c &&
+ test_path_is_missing c2.c
+'
+
+test_expect_success 'merge c1 with c2 and c3 (recursive in pull.octopus)' '
+ git reset --hard c1 &&
+ git config pull.octopus "recursive" &&
+ test_must_fail git merge c2 c3 &&
+ test "$(git rev-parse c1)" = "$(git rev-parse HEAD)"
+'
+
+test_expect_success 'merge c1 with c2 and c3 (recursive and octopus in pull.octopus)' '
+ git reset --hard c1 &&
+ git config pull.octopus "recursive octopus" &&
+ git merge c2 c3 &&
+ test "$(git rev-parse c1)" != "$(git rev-parse HEAD)" &&
+ test "$(git rev-parse c1)" = "$(git rev-parse HEAD^1)" &&
+ test "$(git rev-parse c2)" = "$(git rev-parse HEAD^2)" &&
+ test "$(git rev-parse c3)" = "$(git rev-parse HEAD^3)" &&
+ git diff --exit-code &&
+ test_path_is_file c0.c &&
+ test_path_is_file c1.c &&
+ test_path_is_file c2.c &&
+ test_path_is_file c3.c
+'
+
+conflict_count()
+{
+ {
+ git diff-files --name-only
+ git ls-files --unmerged
+ } | wc -l
+}
+
+# c4 - c5
+# \ c6
+#
+# There are two conflicts here:
+#
+# 1) Because foo.c is renamed to bar.c, recursive will handle this,
+# resolve won't.
+#
+# 2) One in conflict.c and that will always fail.
+
+test_expect_success 'setup conflicted merge' '
+ git reset --hard c0 &&
+ echo A >conflict.c &&
+ git add conflict.c &&
+ echo contents >foo.c &&
+ git add foo.c &&
+ git commit -m c4 &&
+ git tag c4 &&
+ echo B >conflict.c &&
+ git add conflict.c &&
+ git mv foo.c bar.c &&
+ git commit -m c5 &&
+ git tag c5 &&
+ git reset --hard c4 &&
+ echo C >conflict.c &&
+ git add conflict.c &&
+ echo secondline >> foo.c &&
+ git add foo.c &&
+ git commit -m c6 &&
+ git tag c6
+'
+
+# First do the merge with resolve and recursive then verify that
+# recursive is chosen.
+
+test_expect_success 'merge picks up the best result' '
+ git config --unset-all pull.twohead &&
+ git reset --hard c5 &&
+ test_must_fail git merge -s resolve c6 &&
+ resolve_count=$(conflict_count) &&
+ git reset --hard c5 &&
+ test_must_fail git merge -s recursive c6 &&
+ recursive_count=$(conflict_count) &&
+ git reset --hard c5 &&
+ test_must_fail git merge -s recursive -s resolve c6 &&
+ auto_count=$(conflict_count) &&
+ test $auto_count = $recursive_count &&
+ test $auto_count != $resolve_count
+'
+
+test_expect_success 'merge picks up the best result (from config)' '
+ git config pull.twohead "recursive resolve" &&
+ git reset --hard c5 &&
+ test_must_fail git merge -s resolve c6 &&
+ resolve_count=$(conflict_count) &&
+ git reset --hard c5 &&
+ test_must_fail git merge -s recursive c6 &&
+ recursive_count=$(conflict_count) &&
+ git reset --hard c5 &&
+ test_must_fail git merge c6 &&
+ auto_count=$(conflict_count) &&
+ test $auto_count = $recursive_count &&
+ test $auto_count != $resolve_count
+'
+
+test_expect_success 'merge errors out on invalid strategy' '
+ git config pull.twohead "foobar" &&
+ git reset --hard c5 &&
+ test_must_fail git merge c6
+'
+
+test_expect_success 'merge errors out on invalid strategy' '
+ git config --unset-all pull.twohead &&
+ git reset --hard c5 &&
+ test_must_fail git merge -s "resolve recursive" c6
+'
+
+test_done
diff --git a/t/t7602-merge-octopus-many.sh b/t/t7602-merge-octopus-many.sh
new file mode 100755
index 0000000..3669d33
--- /dev/null
+++ b/t/t7602-merge-octopus-many.sh
@@ -0,0 +1,108 @@
+#!/bin/sh
+
+test_description='git merge
+
+Testing octopus merge with more than 25 refs.'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ echo c0 > c0.c &&
+ git add c0.c &&
+ git commit -m c0 &&
+ git tag c0 &&
+ i=1 &&
+ while test $i -le 30
+ do
+ git reset --hard c0 &&
+ echo c$i > c$i.c &&
+ git add c$i.c &&
+ git commit -m c$i &&
+ git tag c$i &&
+ i=$(expr $i + 1) || return 1
+ done
+'
+
+test_expect_success 'merge c1 with c2, c3, c4, ... c29' '
+ git reset --hard c1 &&
+ i=2 &&
+ refs="" &&
+ while test $i -le 30
+ do
+ refs="$refs c$i" &&
+ i=$(expr $i + 1) || return 1
+ done &&
+ git merge $refs &&
+ test "$(git rev-parse c1)" != "$(git rev-parse HEAD)" &&
+ i=1 &&
+ while test $i -le 30
+ do
+ test "$(git rev-parse c$i)" = "$(git rev-parse HEAD^$i)" &&
+ i=$(expr $i + 1) || return 1
+ done &&
+ git diff --exit-code &&
+ i=1 &&
+ while test $i -le 30
+ do
+ test -f c$i.c &&
+ i=$(expr $i + 1) || return 1
+ done
+'
+
+cat >expected <<\EOF
+Trying simple merge with c2
+Trying simple merge with c3
+Trying simple merge with c4
+Merge made by the 'octopus' strategy.
+ c2.c | 1 +
+ c3.c | 1 +
+ c4.c | 1 +
+ 3 files changed, 3 insertions(+)
+ create mode 100644 c2.c
+ create mode 100644 c3.c
+ create mode 100644 c4.c
+EOF
+
+test_expect_success 'merge output uses pretty names' '
+ git reset --hard c1 &&
+ git merge c2 c3 c4 >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<\EOF
+Merge made by the 'recursive' strategy.
+ c5.c | 1 +
+ 1 file changed, 1 insertion(+)
+ create mode 100644 c5.c
+EOF
+
+test_expect_success 'merge reduces irrelevant remote heads' '
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ mv expected expected.tmp &&
+ sed s/recursive/ort/ expected.tmp >expected &&
+ rm expected.tmp
+ fi &&
+ GIT_MERGE_VERBOSITY=0 git merge c4 c5 >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<\EOF
+Fast-forwarding to: c1
+Trying simple merge with c2
+Merge made by the 'octopus' strategy.
+ c1.c | 1 +
+ c2.c | 1 +
+ 2 files changed, 2 insertions(+)
+ create mode 100644 c1.c
+ create mode 100644 c2.c
+EOF
+
+test_expect_success 'merge fast-forward output uses pretty names' '
+ git reset --hard c0 &&
+ git merge c1 c2 >actual &&
+ test_cmp expected actual
+'
+
+test_done
diff --git a/t/t7603-merge-reduce-heads.sh b/t/t7603-merge-reduce-heads.sh
new file mode 100755
index 0000000..0e85b21
--- /dev/null
+++ b/t/t7603-merge-reduce-heads.sh
@@ -0,0 +1,165 @@
+#!/bin/sh
+
+test_description='git merge
+
+Testing octopus merge when reducing parents to independent branches.'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# 0 - 1
+# \ 2
+# \ 3
+# \ 4 - 5
+#
+# So 1, 2, 3 and 5 should be kept, 4 should be avoided.
+
+test_expect_success 'setup' '
+ echo c0 > c0.c &&
+ git add c0.c &&
+ git commit -m c0 &&
+ git tag c0 &&
+ echo c1 > c1.c &&
+ git add c1.c &&
+ git commit -m c1 &&
+ git tag c1 &&
+ git reset --hard c0 &&
+ echo c2 > c2.c &&
+ git add c2.c &&
+ git commit -m c2 &&
+ git tag c2 &&
+ git reset --hard c0 &&
+ echo c3 > c3.c &&
+ git add c3.c &&
+ git commit -m c3 &&
+ git tag c3 &&
+ git reset --hard c0 &&
+ echo c4 > c4.c &&
+ git add c4.c &&
+ git commit -m c4 &&
+ git tag c4 &&
+ echo c5 > c5.c &&
+ git add c5.c &&
+ git commit -m c5 &&
+ git tag c5
+'
+
+test_expect_success 'merge c1 with c2, c3, c4, c5' '
+ git reset --hard c1 &&
+ git merge c2 c3 c4 c5 &&
+ test "$(git rev-parse c1)" != "$(git rev-parse HEAD)" &&
+ test "$(git rev-parse c1)" = "$(git rev-parse HEAD^1)" &&
+ test "$(git rev-parse c2)" = "$(git rev-parse HEAD^2)" &&
+ test "$(git rev-parse c3)" = "$(git rev-parse HEAD^3)" &&
+ test "$(git rev-parse c5)" = "$(git rev-parse HEAD^4)" &&
+ git diff --exit-code &&
+ test -f c0.c &&
+ test -f c1.c &&
+ test -f c2.c &&
+ test -f c3.c &&
+ test -f c4.c &&
+ test -f c5.c &&
+ git show --format=%s -s >actual &&
+ ! grep c1 actual &&
+ grep c2 actual &&
+ grep c3 actual &&
+ ! grep c4 actual &&
+ grep c5 actual
+'
+
+test_expect_success 'pull c2, c3, c4, c5 into c1' '
+ git reset --hard c1 &&
+ git pull --no-rebase . c2 c3 c4 c5 &&
+ test "$(git rev-parse c1)" != "$(git rev-parse HEAD)" &&
+ test "$(git rev-parse c1)" = "$(git rev-parse HEAD^1)" &&
+ test "$(git rev-parse c2)" = "$(git rev-parse HEAD^2)" &&
+ test "$(git rev-parse c3)" = "$(git rev-parse HEAD^3)" &&
+ test "$(git rev-parse c5)" = "$(git rev-parse HEAD^4)" &&
+ git diff --exit-code &&
+ test -f c0.c &&
+ test -f c1.c &&
+ test -f c2.c &&
+ test -f c3.c &&
+ test -f c4.c &&
+ test -f c5.c &&
+ git show --format=%s -s >actual &&
+ ! grep c1 actual &&
+ grep c2 actual &&
+ grep c3 actual &&
+ ! grep c4 actual &&
+ grep c5 actual
+'
+
+test_expect_success 'setup' '
+ for i in A B C D E
+ do
+ echo $i > $i.c &&
+ git add $i.c &&
+ git commit -m $i &&
+ git tag $i || return 1
+ done &&
+ git reset --hard A &&
+ for i in F G H I
+ do
+ echo $i > $i.c &&
+ git add $i.c &&
+ git commit -m $i &&
+ git tag $i || return 1
+ done
+'
+
+test_expect_success 'merge E and I' '
+ git reset --hard A &&
+ git merge E I
+'
+
+test_expect_success 'verify merge result' '
+ test $(git rev-parse HEAD^1) = $(git rev-parse E) &&
+ test $(git rev-parse HEAD^2) = $(git rev-parse I)
+'
+
+test_expect_success 'add conflicts' '
+ git reset --hard E &&
+ echo foo > file.c &&
+ git add file.c &&
+ git commit -m E2 &&
+ git tag E2 &&
+ git reset --hard I &&
+ echo bar >file.c &&
+ git add file.c &&
+ git commit -m I2 &&
+ git tag I2
+'
+
+test_expect_success 'merge E2 and I2, causing a conflict and resolve it' '
+ git reset --hard A &&
+ test_must_fail git merge E2 I2 &&
+ echo baz > file.c &&
+ git add file.c &&
+ git commit -m "resolve conflict"
+'
+
+test_expect_success 'verify merge result' '
+ test $(git rev-parse HEAD^1) = $(git rev-parse E2) &&
+ test $(git rev-parse HEAD^2) = $(git rev-parse I2)
+'
+
+test_expect_success 'fast-forward to redundant refs' '
+ git reset --hard c0 &&
+ git merge c4 c5
+'
+
+test_expect_success 'verify merge result' '
+ test $(git rev-parse HEAD) = $(git rev-parse c5)
+'
+
+test_expect_success 'merge up-to-date redundant refs' '
+ git reset --hard c5 &&
+ git merge c0 c4
+'
+
+test_expect_success 'verify merge result' '
+ test $(git rev-parse HEAD) = $(git rev-parse c5)
+'
+
+test_done
diff --git a/t/t7604-merge-custom-message.sh b/t/t7604-merge-custom-message.sh
new file mode 100755
index 0000000..eca7555
--- /dev/null
+++ b/t/t7604-merge-custom-message.sh
@@ -0,0 +1,116 @@
+#!/bin/sh
+
+test_description='git merge
+
+Testing merge when using a custom message for the merge commit.'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+create_merge_msgs() {
+ echo >exp.subject "custom message"
+
+ cp exp.subject exp.log &&
+ echo >>exp.log "" &&
+ echo >>exp.log "* tag 'c2':" &&
+ echo >>exp.log " c2"
+}
+
+test_expect_success 'setup' '
+ echo c0 >c0.c &&
+ git add c0.c &&
+ git commit -m c0 &&
+ git tag c0 &&
+ echo c1 >c1.c &&
+ git add c1.c &&
+ git commit -m c1 &&
+ git tag c1 &&
+ git reset --hard c0 &&
+ echo c2 >c2.c &&
+ git add c2.c &&
+ git commit -m c2 &&
+ git tag c2 &&
+ create_merge_msgs
+'
+
+
+test_expect_success 'merge c2 with a custom message' '
+ git reset --hard c1 &&
+ git merge -m "$(cat exp.subject)" c2 &&
+ git cat-file commit HEAD >raw &&
+ sed -e "1,/^$/d" raw >actual &&
+ test_cmp exp.subject actual
+'
+
+test_expect_success 'merge --log appends to custom message' '
+ git reset --hard c1 &&
+ git merge --log -m "$(cat exp.subject)" c2 &&
+ git cat-file commit HEAD >raw &&
+ sed -e "1,/^$/d" raw >actual &&
+ test_cmp exp.log actual
+'
+
+mesg_with_comment_and_newlines='
+# text
+
+'
+
+test_expect_success 'prepare file with comment line and trailing newlines' '
+ printf "%s" "$mesg_with_comment_and_newlines" >expect
+'
+
+test_expect_success 'cleanup commit messages (verbatim option)' '
+ git reset --hard c1 &&
+ git merge --cleanup=verbatim -F expect c2 &&
+ git cat-file commit HEAD >raw &&
+ sed -e "1,/^$/d" raw >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'cleanup commit messages (whitespace option)' '
+ git reset --hard c1 &&
+ test_write_lines "" "# text" "" >text &&
+ echo "# text" >expect &&
+ git merge --cleanup=whitespace -F text c2 &&
+ git cat-file commit HEAD >raw &&
+ sed -e "1,/^$/d" raw >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'cleanup merge messages (scissors option)' '
+ git reset --hard c1 &&
+ cat >text <<-\EOF &&
+
+ # to be kept
+
+ # ------------------------ >8 ------------------------
+ # to be kept, too
+ # ------------------------ >8 ------------------------
+ to be removed
+ # ------------------------ >8 ------------------------
+ to be removed, too
+ EOF
+
+ cat >expect <<-\EOF &&
+ # to be kept
+
+ # ------------------------ >8 ------------------------
+ # to be kept, too
+ EOF
+ git merge --cleanup=scissors -e -F text c2 &&
+ git cat-file commit HEAD >raw &&
+ sed -e "1,/^$/d" raw >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'cleanup commit messages (strip option)' '
+ git reset --hard c1 &&
+ test_write_lines "" "# text" "sample" "" >text &&
+ echo sample >expect &&
+ git merge --cleanup=strip -F text c2 &&
+ git cat-file commit HEAD >raw &&
+ sed -e "1,/^$/d" raw >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t7605-merge-resolve.sh b/t/t7605-merge-resolve.sh
new file mode 100755
index 0000000..62d935d
--- /dev/null
+++ b/t/t7605-merge-resolve.sh
@@ -0,0 +1,53 @@
+#!/bin/sh
+
+test_description='git merge
+
+Testing the resolve strategy.'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ echo c0 > c0.c &&
+ git add c0.c &&
+ git commit -m c0 &&
+ git tag c0 &&
+ echo c1 > c1.c &&
+ git add c1.c &&
+ git commit -m c1 &&
+ git tag c1 &&
+ git reset --hard c0 &&
+ echo c2 > c2.c &&
+ git add c2.c &&
+ git commit -m c2 &&
+ git tag c2 &&
+ git reset --hard c0 &&
+ echo c3 > c2.c &&
+ git add c2.c &&
+ git commit -m c3 &&
+ git tag c3
+'
+
+merge_c1_to_c2_cmds='
+ git reset --hard c1 &&
+ git merge -s resolve c2 &&
+ test "$(git rev-parse c1)" != "$(git rev-parse HEAD)" &&
+ test "$(git rev-parse c1)" = "$(git rev-parse HEAD^1)" &&
+ test "$(git rev-parse c2)" = "$(git rev-parse HEAD^2)" &&
+ git diff --exit-code &&
+ test -f c0.c &&
+ test -f c1.c &&
+ test -f c2.c &&
+ test 3 = $(git ls-tree -r HEAD | wc -l) &&
+ test 3 = $(git ls-files | wc -l)
+'
+
+test_expect_success 'merge c1 to c2' "$merge_c1_to_c2_cmds"
+
+test_expect_success 'merge c1 to c2, again' "$merge_c1_to_c2_cmds"
+
+test_expect_success 'merge c2 to c3 (fails)' '
+ git reset --hard c2 &&
+ test_must_fail git merge -s resolve c3
+'
+test_done
diff --git a/t/t7606-merge-custom.sh b/t/t7606-merge-custom.sh
new file mode 100755
index 0000000..81fb7c4
--- /dev/null
+++ b/t/t7606-merge-custom.sh
@@ -0,0 +1,93 @@
+#!/bin/sh
+
+test_description="git merge
+
+Testing a custom strategy.
+
+* (HEAD, main) Merge commit 'c3'
+|\
+| * (tag: c3) c3
+* | (tag: c1) c1
+|/
+| * tag: c2) c2
+|/
+* (tag: c0) c0
+"
+
+. ./test-lib.sh
+
+test_expect_success 'set up custom strategy' '
+ cat >git-merge-theirs <<-EOF &&
+ #!$SHELL_PATH
+ eval git read-tree --reset -u \\\$\$#
+ EOF
+
+ chmod +x git-merge-theirs &&
+ PATH=.:$PATH &&
+ export PATH
+'
+
+test_expect_success 'setup' '
+ test_commit c0 c0.c &&
+ test_commit c1 c1.c &&
+ git reset --keep c0 &&
+ echo c1c1 >c1.c &&
+ git add c1.c &&
+ test_commit c2 c2.c &&
+ git reset --keep c0 &&
+ test_commit c3 c3.c
+'
+
+test_expect_success 'merge c2 with a custom strategy' '
+ git reset --hard c1 &&
+
+ git rev-parse c1 >head.old &&
+ git rev-parse c2 >second-parent.expected &&
+ git rev-parse c2^{tree} >tree.expected &&
+ git merge -s theirs c2 &&
+
+ git rev-parse HEAD >head.new &&
+ git rev-parse HEAD^1 >first-parent &&
+ git rev-parse HEAD^2 >second-parent &&
+ git rev-parse HEAD^{tree} >tree &&
+ git update-index --refresh &&
+ git diff --exit-code &&
+ git diff --exit-code c2 HEAD &&
+ git diff --exit-code c2 &&
+
+ ! test_cmp head.old head.new &&
+ test_cmp head.old first-parent &&
+ test_cmp second-parent.expected second-parent &&
+ test_cmp tree.expected tree &&
+ test -f c0.c &&
+ grep c1c1 c1.c &&
+ test -f c2.c
+'
+
+test_expect_success 'trivial merge with custom strategy' '
+ git reset --hard c1 &&
+
+ git rev-parse c1 >head.old &&
+ git rev-parse c3 >second-parent.expected &&
+ git rev-parse c3^{tree} >tree.expected &&
+ git merge -s theirs c3 &&
+
+ git rev-parse HEAD >head.new &&
+ git rev-parse HEAD^1 >first-parent &&
+ git rev-parse HEAD^2 >second-parent &&
+ git rev-parse HEAD^{tree} >tree &&
+ git update-index --refresh &&
+ git diff --exit-code &&
+ git diff --exit-code c3 HEAD &&
+ git diff --exit-code c3 &&
+
+ ! test_cmp head.old head.new &&
+ test_cmp head.old first-parent &&
+ test_cmp second-parent.expected second-parent &&
+ test_cmp tree.expected tree &&
+ test -f c0.c &&
+ ! test -e c1.c &&
+ test -f c3.c
+'
+
+test_done
diff --git a/t/t7607-merge-state.sh b/t/t7607-merge-state.sh
new file mode 100755
index 0000000..9001674
--- /dev/null
+++ b/t/t7607-merge-state.sh
@@ -0,0 +1,33 @@
+#!/bin/sh
+
+test_description="Test that merge state is as expected after failed merge"
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'Ensure we restore original state if no merge strategy handles it' '
+ test_commit --no-tag "Initial" base base &&
+
+ for b in branch1 branch2 branch3
+ do
+ git checkout -b $b main &&
+ test_commit --no-tag "Change on $b" base $b || return 1
+ done &&
+
+ git checkout branch1 &&
+ # This is a merge that octopus cannot handle. Note, that it does not
+ # just hit conflicts, it completely fails and says that it cannot
+ # handle this type of merge.
+ test_expect_code 2 git merge branch2 branch3 >output 2>&1 &&
+ grep "fatal: merge program failed" output &&
+ grep "Should not be doing an octopus" output &&
+
+ # Make sure we did not leave stray changes around when no appropriate
+ # merge strategy was found
+ git diff --exit-code --name-status &&
+ test_path_is_missing .git/MERGE_HEAD
+'
+
+test_done
diff --git a/t/t7608-merge-messages.sh b/t/t7608-merge-messages.sh
new file mode 100755
index 0000000..2179938
--- /dev/null
+++ b/t/t7608-merge-messages.sh
@@ -0,0 +1,64 @@
+#!/bin/sh
+
+test_description='test auto-generated merge messages'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+check_oneline() {
+ echo "$1" | sed "s/Q/'/g" >expect &&
+ git log -1 --pretty=tformat:%s >actual &&
+ test_cmp expect actual
+}
+
+test_expect_success 'merge local branch' '
+ test_commit main-1 &&
+ git checkout -b local-branch &&
+ test_commit branch-1 &&
+ git checkout main &&
+ test_commit main-2 &&
+ git merge local-branch &&
+ check_oneline "Merge branch Qlocal-branchQ"
+'
+
+test_expect_success 'merge octopus branches' '
+ git checkout -b octopus-a main &&
+ test_commit octopus-1 &&
+ git checkout -b octopus-b main &&
+ test_commit octopus-2 &&
+ git checkout main &&
+ git merge octopus-a octopus-b &&
+ check_oneline "Merge branches Qoctopus-aQ and Qoctopus-bQ"
+'
+
+test_expect_success 'merge tag' '
+ git checkout -b tag-branch main &&
+ test_commit tag-1 &&
+ git checkout main &&
+ test_commit main-3 &&
+ git merge tag-1 &&
+ check_oneline "Merge tag Qtag-1Q"
+'
+
+test_expect_success 'ambiguous tag' '
+ git checkout -b ambiguous main &&
+ test_commit ambiguous &&
+ git checkout main &&
+ test_commit main-4 &&
+ git merge ambiguous &&
+ check_oneline "Merge tag QambiguousQ"
+'
+
+test_expect_success 'remote-tracking branch' '
+ git checkout -b remote main &&
+ test_commit remote-1 &&
+ git update-ref refs/remotes/origin/main remote &&
+ git checkout main &&
+ test_commit main-5 &&
+ git merge origin/main &&
+ check_oneline "Merge remote-tracking branch Qorigin/mainQ"
+'
+
+test_done
diff --git a/t/t7609-mergetool--lib.sh b/t/t7609-mergetool--lib.sh
new file mode 100755
index 0000000..8b1c3bd
--- /dev/null
+++ b/t/t7609-mergetool--lib.sh
@@ -0,0 +1,15 @@
+#!/bin/sh
+
+test_description='git mergetool
+
+Testing basic merge tools options'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'mergetool --tool=vimdiff creates the expected layout' '
+ . "$GIT_BUILD_DIR"/mergetools/vimdiff &&
+ run_unit_tests
+'
+
+test_done
diff --git a/t/t7610-mergetool.sh b/t/t7610-mergetool.sh
new file mode 100755
index 0000000..22b3a85
--- /dev/null
+++ b/t/t7610-mergetool.sh
@@ -0,0 +1,901 @@
+#!/bin/sh
+#
+# Copyright (c) 2008 Charles Bailey
+#
+
+test_description='git mergetool
+
+Testing basic merge tool invocation'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+# All the mergetool test work by checking out a temporary branch based
+# off 'branch1' and then merging in main and checking the results of
+# running mergetool
+
+test_expect_success 'setup' '
+ test_config rerere.enabled true &&
+ echo main >file1 &&
+ echo main spaced >"spaced name" &&
+ echo main file11 >file11 &&
+ echo main file12 >file12 &&
+ echo main file13 >file13 &&
+ echo main file14 >file14 &&
+ mkdir subdir &&
+ echo main sub >subdir/file3 &&
+ test_create_repo submod &&
+ (
+ cd submod &&
+ : >foo &&
+ git add foo &&
+ git commit -m "Add foo"
+ ) &&
+ git submodule add file:///dev/null submod &&
+ git add file1 "spaced name" file1[1-4] subdir/file3 .gitmodules submod &&
+ git commit -m "add initial versions" &&
+
+ git checkout -b branch1 main &&
+ git submodule update -N &&
+ echo branch1 change >file1 &&
+ echo branch1 newfile >file2 &&
+ echo branch1 spaced >"spaced name" &&
+ echo branch1 both added >both &&
+ echo branch1 change file11 >file11 &&
+ echo branch1 change file13 >file13 &&
+ echo branch1 sub >subdir/file3 &&
+ (
+ cd submod &&
+ echo branch1 submodule >bar &&
+ git add bar &&
+ git commit -m "Add bar on branch1" &&
+ git checkout -b submod-branch1
+ ) &&
+ git add file1 "spaced name" file11 file13 file2 subdir/file3 submod &&
+ git add both &&
+ git rm file12 &&
+ git commit -m "branch1 changes" &&
+
+ git checkout -b delete-base branch1 &&
+ mkdir -p a/a &&
+ test_write_lines one two 3 4 >a/a/file.txt &&
+ git add a/a/file.txt &&
+ git commit -m"base file" &&
+ git checkout -b move-to-b delete-base &&
+ mkdir -p b/b &&
+ git mv a/a/file.txt b/b/file.txt &&
+ test_write_lines one two 4 >b/b/file.txt &&
+ git commit -a -m"move to b" &&
+ git checkout -b move-to-c delete-base &&
+ mkdir -p c/c &&
+ git mv a/a/file.txt c/c/file.txt &&
+ test_write_lines one two 3 >c/c/file.txt &&
+ git commit -a -m"move to c" &&
+
+ git checkout -b stash1 main &&
+ echo stash1 change file11 >file11 &&
+ git add file11 &&
+ git commit -m "stash1 changes" &&
+
+ git checkout -b stash2 main &&
+ echo stash2 change file11 >file11 &&
+ git add file11 &&
+ git commit -m "stash2 changes" &&
+
+ git checkout main &&
+ git submodule update -N &&
+ echo main updated >file1 &&
+ echo main new >file2 &&
+ echo main updated spaced >"spaced name" &&
+ echo main both added >both &&
+ echo main updated file12 >file12 &&
+ echo main updated file14 >file14 &&
+ echo main new sub >subdir/file3 &&
+ (
+ cd submod &&
+ echo main submodule >bar &&
+ git add bar &&
+ git commit -m "Add bar on main" &&
+ git checkout -b submod-main
+ ) &&
+ git add file1 "spaced name" file12 file14 file2 subdir/file3 submod &&
+ git add both &&
+ git rm file11 &&
+ git commit -m "main updates" &&
+
+ git clean -fdx &&
+ git checkout -b order-file-start main &&
+ echo start >a &&
+ echo start >b &&
+ git add a b &&
+ git commit -m start &&
+ git checkout -b order-file-side1 order-file-start &&
+ echo side1 >a &&
+ echo side1 >b &&
+ git add a b &&
+ git commit -m side1 &&
+ git checkout -b order-file-side2 order-file-start &&
+ echo side2 >a &&
+ echo side2 >b &&
+ git add a b &&
+ git commit -m side2 &&
+
+ git config merge.tool mytool &&
+ git config mergetool.mytool.cmd "cat \"\$REMOTE\" >\"\$MERGED\"" &&
+ git config mergetool.mytool.trustExitCode true &&
+ git config mergetool.mybase.cmd "cat \"\$BASE\" >\"\$MERGED\"" &&
+ git config mergetool.mybase.trustExitCode true
+'
+
+test_expect_success 'custom mergetool' '
+ test_when_finished "git reset --hard" &&
+ git checkout -b test$test_count branch1 &&
+ git submodule update -N &&
+ test_must_fail git merge main &&
+ yes "" | git mergetool both &&
+ yes "" | git mergetool file1 file1 &&
+ yes "" | git mergetool file2 "spaced name" &&
+ yes "" | git mergetool subdir/file3 &&
+ yes "d" | git mergetool file11 &&
+ yes "d" | git mergetool file12 &&
+ yes "l" | git mergetool submod &&
+ echo "main updated" >expect &&
+ test_cmp expect file1 &&
+ echo "main new" >expect &&
+ test_cmp expect file2 &&
+ echo "main new sub" >expect &&
+ test_cmp expect subdir/file3 &&
+ echo "branch1 submodule" >expect &&
+ test_cmp expect submod/bar &&
+ git commit -m "branch1 resolved with mergetool"
+'
+
+test_expect_success 'gui mergetool' '
+ test_config merge.guitool myguitool &&
+ test_config mergetool.myguitool.cmd "(printf \"gui \" && cat \"\$REMOTE\") >\"\$MERGED\"" &&
+ test_config mergetool.myguitool.trustExitCode true &&
+ test_when_finished "git reset --hard" &&
+ git checkout -b test$test_count branch1 &&
+ git submodule update -N &&
+ test_must_fail git merge main &&
+ yes "" | git mergetool --gui both &&
+ yes "" | git mergetool -g file1 file1 &&
+ yes "" | git mergetool --gui file2 "spaced name" &&
+ yes "" | git mergetool --gui subdir/file3 &&
+ yes "d" | git mergetool --gui file11 &&
+ yes "d" | git mergetool --gui file12 &&
+ yes "l" | git mergetool --gui submod &&
+ echo "gui main updated" >expect &&
+ test_cmp expect file1 &&
+ echo "gui main new" >expect &&
+ test_cmp expect file2 &&
+ echo "gui main new sub" >expect &&
+ test_cmp expect subdir/file3 &&
+ echo "branch1 submodule" >expect &&
+ test_cmp expect submod/bar &&
+ git commit -m "branch1 resolved with mergetool"
+'
+
+test_expect_success 'gui mergetool without merge.guitool set falls back to merge.tool' '
+ test_when_finished "git reset --hard" &&
+ git checkout -b test$test_count branch1 &&
+ git submodule update -N &&
+ test_must_fail git merge main &&
+ yes "" | git mergetool --gui both &&
+ yes "" | git mergetool -g file1 file1 &&
+ yes "" | git mergetool --gui file2 "spaced name" &&
+ yes "" | git mergetool --gui subdir/file3 &&
+ yes "d" | git mergetool --gui file11 &&
+ yes "d" | git mergetool --gui file12 &&
+ yes "l" | git mergetool --gui submod &&
+ echo "main updated" >expect &&
+ test_cmp expect file1 &&
+ echo "main new" >expect &&
+ test_cmp expect file2 &&
+ echo "main new sub" >expect &&
+ test_cmp expect subdir/file3 &&
+ echo "branch1 submodule" >expect &&
+ test_cmp expect submod/bar &&
+ git commit -m "branch1 resolved with mergetool"
+'
+
+test_expect_success 'mergetool crlf' '
+ test_when_finished "git reset --hard" &&
+ # This test_config line must go after the above reset line so that
+ # core.autocrlf is unconfigured before reset runs. (The
+ # test_config command uses test_when_finished internally and
+ # test_when_finished is LIFO.)
+ test_config core.autocrlf true &&
+ git checkout -b test$test_count branch1 &&
+ test_must_fail git merge main &&
+ yes "" | git mergetool file1 &&
+ yes "" | git mergetool file2 &&
+ yes "" | git mergetool "spaced name" &&
+ yes "" | git mergetool both &&
+ yes "" | git mergetool subdir/file3 &&
+ yes "d" | git mergetool file11 &&
+ yes "d" | git mergetool file12 &&
+ yes "r" | git mergetool submod &&
+ test "$(printf x | cat file1 -)" = "$(printf "main updated\r\nx")" &&
+ test "$(printf x | cat file2 -)" = "$(printf "main new\r\nx")" &&
+ test "$(printf x | cat subdir/file3 -)" = "$(printf "main new sub\r\nx")" &&
+ git submodule update -N &&
+ echo "main submodule" >expect &&
+ test_cmp expect submod/bar &&
+ git commit -m "branch1 resolved with mergetool - autocrlf"
+'
+
+test_expect_success 'mergetool in subdir' '
+ test_when_finished "git reset --hard" &&
+ git checkout -b test$test_count branch1 &&
+ git submodule update -N &&
+ (
+ cd subdir &&
+ test_must_fail git merge main &&
+ yes "" | git mergetool file3 &&
+ echo "main new sub" >expect &&
+ test_cmp expect file3
+ )
+'
+
+test_expect_success 'mergetool on file in parent dir' '
+ test_when_finished "git reset --hard" &&
+ git checkout -b test$test_count branch1 &&
+ git submodule update -N &&
+ (
+ cd subdir &&
+ test_must_fail git merge main &&
+ yes "" | git mergetool file3 &&
+ yes "" | git mergetool ../file1 &&
+ yes "" | git mergetool ../file2 ../spaced\ name &&
+ yes "" | git mergetool ../both &&
+ yes "d" | git mergetool ../file11 &&
+ yes "d" | git mergetool ../file12 &&
+ yes "l" | git mergetool ../submod &&
+ echo "main updated" >expect &&
+ test_cmp expect ../file1 &&
+ echo "main new" >expect &&
+ test_cmp expect ../file2 &&
+ echo "branch1 submodule" >expect &&
+ test_cmp expect ../submod/bar &&
+ git commit -m "branch1 resolved with mergetool - subdir"
+ )
+'
+
+test_expect_success 'mergetool skips autoresolved' '
+ test_when_finished "git reset --hard" &&
+ git checkout -b test$test_count branch1 &&
+ git submodule update -N &&
+ test_must_fail git merge main &&
+ test -n "$(git ls-files -u)" &&
+ yes "d" | git mergetool file11 &&
+ yes "d" | git mergetool file12 &&
+ yes "l" | git mergetool submod &&
+ output="$(git mergetool --no-prompt)" &&
+ test "$output" = "No files need merging"
+'
+
+test_expect_success 'mergetool merges all from subdir (rerere disabled)' '
+ test_when_finished "git reset --hard" &&
+ git checkout -b test$test_count branch1 &&
+ test_config rerere.enabled false &&
+ (
+ cd subdir &&
+ test_must_fail git merge main &&
+ yes "r" | git mergetool ../submod &&
+ yes "d" "d" | git mergetool --no-prompt &&
+ echo "main updated" >expect &&
+ test_cmp expect ../file1 &&
+ echo "main new" >expect &&
+ test_cmp expect ../file2 &&
+ echo "main new sub" >expect &&
+ test_cmp expect file3 &&
+ ( cd .. && git submodule update -N ) &&
+ echo "main submodule" >expect &&
+ test_cmp expect ../submod/bar &&
+ git commit -m "branch2 resolved by mergetool from subdir"
+ )
+'
+
+test_expect_success 'mergetool merges all from subdir (rerere enabled)' '
+ test_when_finished "git reset --hard" &&
+ git checkout -b test$test_count branch1 &&
+ test_config rerere.enabled true &&
+ rm -rf .git/rr-cache &&
+ (
+ cd subdir &&
+ test_must_fail git merge main &&
+ yes "r" | git mergetool ../submod &&
+ yes "d" "d" | git mergetool --no-prompt &&
+ echo "main updated" >expect &&
+ test_cmp expect ../file1 &&
+ echo "main new" >expect &&
+ test_cmp expect ../file2 &&
+ echo "main new sub" >expect &&
+ test_cmp expect file3 &&
+ ( cd .. && git submodule update -N ) &&
+ echo "main submodule" >expect &&
+ test_cmp expect ../submod/bar &&
+ git commit -m "branch2 resolved by mergetool from subdir"
+ )
+'
+
+test_expect_success 'mergetool skips resolved paths when rerere is active' '
+ test_when_finished "git reset --hard" &&
+ test_config rerere.enabled true &&
+ rm -rf .git/rr-cache &&
+ git checkout -b test$test_count branch1 &&
+ git submodule update -N &&
+ test_must_fail git merge main &&
+ yes "l" | git mergetool --no-prompt submod &&
+ yes "d" "d" | git mergetool --no-prompt &&
+ git submodule update -N &&
+ output="$(yes "n" | git mergetool --no-prompt)" &&
+ test "$output" = "No files need merging"
+'
+
+test_expect_success 'conflicted stash sets up rerere' '
+ test_when_finished "git reset --hard" &&
+ test_config rerere.enabled true &&
+ git checkout stash1 &&
+ echo "Conflicting stash content" >file11 &&
+ git stash &&
+
+ git checkout --detach stash2 &&
+ test_must_fail git stash apply &&
+
+ test -n "$(git ls-files -u)" &&
+ conflicts="$(git rerere remaining)" &&
+ test "$conflicts" = "file11" &&
+ output="$(git mergetool --no-prompt)" &&
+ test "$output" != "No files need merging" &&
+
+ git commit -am "save the stash resolution" &&
+
+ git reset --hard stash2 &&
+ test_must_fail git stash apply &&
+
+ test -n "$(git ls-files -u)" &&
+ conflicts="$(git rerere remaining)" &&
+ test -z "$conflicts" &&
+ output="$(git mergetool --no-prompt)" &&
+ test "$output" = "No files need merging"
+'
+
+test_expect_success 'mergetool takes partial path' '
+ test_when_finished "git reset --hard" &&
+ test_config rerere.enabled false &&
+ git checkout -b test$test_count branch1 &&
+ git submodule update -N &&
+ test_must_fail git merge main &&
+
+ yes "" | git mergetool subdir &&
+
+ echo "main new sub" >expect &&
+ test_cmp expect subdir/file3
+'
+
+test_expect_success 'mergetool delete/delete conflict' '
+ test_when_finished "git reset --hard" &&
+ git checkout -b test$test_count move-to-c &&
+ test_must_fail git merge move-to-b &&
+ echo d | git mergetool a/a/file.txt &&
+ ! test -f a/a/file.txt &&
+ git reset --hard &&
+ test_must_fail git merge move-to-b &&
+ echo m | git mergetool a/a/file.txt &&
+ test -f b/b/file.txt &&
+ git reset --hard &&
+ test_must_fail git merge move-to-b &&
+ ! echo a | git mergetool a/a/file.txt &&
+ ! test -f a/a/file.txt
+'
+
+test_expect_success 'mergetool produces no errors when keepBackup is used' '
+ test_when_finished "git reset --hard" &&
+ git checkout -b test$test_count move-to-c &&
+ test_config mergetool.keepBackup true &&
+ test_must_fail git merge move-to-b &&
+ echo d | git mergetool a/a/file.txt 2>actual &&
+ test_must_be_empty actual &&
+ ! test -d a
+'
+
+test_expect_success 'mergetool honors tempfile config for deleted files' '
+ test_when_finished "git reset --hard" &&
+ git checkout -b test$test_count move-to-c &&
+ test_config mergetool.keepTemporaries false &&
+ test_must_fail git merge move-to-b &&
+ echo d | git mergetool a/a/file.txt &&
+ ! test -d a
+'
+
+test_expect_success 'mergetool keeps tempfiles when aborting delete/delete' '
+ test_when_finished "git reset --hard" &&
+ test_when_finished "git clean -fdx" &&
+ git checkout -b test$test_count move-to-c &&
+ test_config mergetool.keepTemporaries true &&
+ test_must_fail git merge move-to-b &&
+ ! test_write_lines a n | git mergetool a/a/file.txt &&
+ test -d a/a &&
+ cat >expect <<-\EOF &&
+ file_BASE_.txt
+ file_LOCAL_.txt
+ file_REMOTE_.txt
+ EOF
+ ls -1 a/a | sed -e "s/[0-9]*//g" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'deleted vs modified submodule' '
+ test_when_finished "git reset --hard" &&
+ git checkout -b test$test_count branch1 &&
+ git submodule update -N &&
+ mv submod submod-movedaside &&
+ git rm --cached submod &&
+ git commit -m "Submodule deleted from branch" &&
+ git checkout -b test$test_count.a test$test_count &&
+ test_must_fail git merge main &&
+ test -n "$(git ls-files -u)" &&
+ yes "" | git mergetool file1 file2 spaced\ name subdir/file3 &&
+ yes "" | git mergetool both &&
+ yes "d" | git mergetool file11 file12 &&
+ yes "r" | git mergetool submod &&
+ rmdir submod && mv submod-movedaside submod &&
+ echo "branch1 submodule" >expect &&
+ test_cmp expect submod/bar &&
+ git submodule update -N &&
+ echo "main submodule" >expect &&
+ test_cmp expect submod/bar &&
+ output="$(git mergetool --no-prompt)" &&
+ test "$output" = "No files need merging" &&
+ git commit -m "Merge resolved by keeping module" &&
+
+ mv submod submod-movedaside &&
+ git checkout -b test$test_count.b test$test_count &&
+ git submodule update -N &&
+ test_must_fail git merge main &&
+ test -n "$(git ls-files -u)" &&
+ yes "" | git mergetool file1 file2 spaced\ name subdir/file3 &&
+ yes "" | git mergetool both &&
+ yes "d" | git mergetool file11 file12 &&
+ yes "l" | git mergetool submod &&
+ test ! -e submod &&
+ output="$(git mergetool --no-prompt)" &&
+ test "$output" = "No files need merging" &&
+ git commit -m "Merge resolved by deleting module" &&
+
+ mv submod-movedaside submod &&
+ git checkout -b test$test_count.c main &&
+ git submodule update -N &&
+ test_must_fail git merge test$test_count &&
+ test -n "$(git ls-files -u)" &&
+ yes "" | git mergetool file1 file2 spaced\ name subdir/file3 &&
+ yes "" | git mergetool both &&
+ yes "d" | git mergetool file11 file12 &&
+ yes "r" | git mergetool submod &&
+ test ! -e submod &&
+ test -d submod.orig &&
+ git submodule update -N &&
+ output="$(git mergetool --no-prompt)" &&
+ test "$output" = "No files need merging" &&
+ git commit -m "Merge resolved by deleting module" &&
+ mv submod.orig submod &&
+
+ git checkout -b test$test_count.d main &&
+ git submodule update -N &&
+ test_must_fail git merge test$test_count &&
+ test -n "$(git ls-files -u)" &&
+ yes "" | git mergetool file1 file2 spaced\ name subdir/file3 &&
+ yes "" | git mergetool both &&
+ yes "d" | git mergetool file11 file12 &&
+ yes "l" | git mergetool submod &&
+ echo "main submodule" >expect &&
+ test_cmp expect submod/bar &&
+ git submodule update -N &&
+ echo "main submodule" >expect &&
+ test_cmp expect submod/bar &&
+ output="$(git mergetool --no-prompt)" &&
+ test "$output" = "No files need merging" &&
+ git commit -m "Merge resolved by keeping module"
+'
+
+test_expect_success 'file vs modified submodule' '
+ test_when_finished "git reset --hard" &&
+ git checkout -b test$test_count branch1 &&
+ git submodule update -N &&
+ mv submod submod-movedaside &&
+ git rm --cached submod &&
+ echo not a submodule >submod &&
+ git add submod &&
+ git commit -m "Submodule path becomes file" &&
+ git checkout -b test$test_count.a branch1 &&
+ test_must_fail git merge main &&
+ test -n "$(git ls-files -u)" &&
+ yes "" | git mergetool file1 file2 spaced\ name subdir/file3 &&
+ yes "" | git mergetool both &&
+ yes "d" | git mergetool file11 file12 &&
+ yes "r" | git mergetool submod &&
+ rmdir submod && mv submod-movedaside submod &&
+ echo "branch1 submodule" >expect &&
+ test_cmp expect submod/bar &&
+ git submodule update -N &&
+ echo "main submodule" >expect &&
+ test_cmp expect submod/bar &&
+ output="$(git mergetool --no-prompt)" &&
+ test "$output" = "No files need merging" &&
+ git commit -m "Merge resolved by keeping module" &&
+
+ mv submod submod-movedaside &&
+ git checkout -b test$test_count.b test$test_count &&
+ test_must_fail git merge main &&
+ test -n "$(git ls-files -u)" &&
+ yes "" | git mergetool file1 file2 spaced\ name subdir/file3 &&
+ yes "" | git mergetool both &&
+ yes "d" | git mergetool file11 file12 &&
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ yes "c" | git mergetool submod~HEAD &&
+ git rm submod &&
+ git mv submod~HEAD submod
+ else
+ yes "l" | git mergetool submod
+ fi &&
+ git submodule update -N &&
+ echo "not a submodule" >expect &&
+ test_cmp expect submod &&
+ output="$(git mergetool --no-prompt)" &&
+ test "$output" = "No files need merging" &&
+ git commit -m "Merge resolved by keeping file" &&
+
+ git checkout -b test$test_count.c main &&
+ rmdir submod && mv submod-movedaside submod &&
+ test ! -e submod.orig &&
+ git submodule update -N &&
+ test_must_fail git merge test$test_count &&
+ test -n "$(git ls-files -u)" &&
+ yes "" | git mergetool file1 file2 spaced\ name subdir/file3 &&
+ yes "" | git mergetool both &&
+ yes "d" | git mergetool file11 file12 &&
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ mv submod submod.orig &&
+ git rm --cached submod &&
+ yes "c" | git mergetool submod~test19 &&
+ git mv submod~test19 submod
+ else
+ yes "r" | git mergetool submod
+ fi &&
+ test -d submod.orig &&
+ git submodule update -N &&
+ echo "not a submodule" >expect &&
+ test_cmp expect submod &&
+ output="$(git mergetool --no-prompt)" &&
+ test "$output" = "No files need merging" &&
+ git commit -m "Merge resolved by keeping file" &&
+
+ git checkout -b test$test_count.d main &&
+ rmdir submod && mv submod.orig submod &&
+ git submodule update -N &&
+ test_must_fail git merge test$test_count &&
+ test -n "$(git ls-files -u)" &&
+ yes "" | git mergetool file1 file2 spaced\ name subdir/file3 &&
+ yes "" | git mergetool both &&
+ yes "d" | git mergetool file11 file12 &&
+ yes "l" | git mergetool submod &&
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ yes "d" | git mergetool submod~test19
+ fi &&
+ echo "main submodule" >expect &&
+ test_cmp expect submod/bar &&
+ git submodule update -N &&
+ echo "main submodule" >expect &&
+ test_cmp expect submod/bar &&
+ output="$(git mergetool --no-prompt)" &&
+ test "$output" = "No files need merging" &&
+ git commit -m "Merge resolved by keeping module"
+'
+
+test_expect_success 'submodule in subdirectory' '
+ test_when_finished "git reset --hard" &&
+ git checkout -b test$test_count branch1 &&
+ git submodule update -N &&
+ (
+ cd subdir &&
+ test_create_repo subdir_module &&
+ (
+ cd subdir_module &&
+ : >file15 &&
+ git add file15 &&
+ git commit -m "add initial versions"
+ )
+ ) &&
+ test_when_finished "rm -rf subdir/subdir_module" &&
+ git submodule add file:///dev/null subdir/subdir_module &&
+ git add subdir/subdir_module &&
+ git commit -m "add submodule in subdirectory" &&
+
+ git checkout -b test$test_count.a test$test_count &&
+ git submodule update -N &&
+ (
+ cd subdir/subdir_module &&
+ git checkout -b super10.a &&
+ echo test$test_count.a >file15 &&
+ git add file15 &&
+ git commit -m "on branch 10.a"
+ ) &&
+ git add subdir/subdir_module &&
+ git commit -m "change submodule in subdirectory on test$test_count.a" &&
+
+ git checkout -b test$test_count.b test$test_count &&
+ git submodule update -N &&
+ (
+ cd subdir/subdir_module &&
+ git checkout -b super10.b &&
+ echo test$test_count.b >file15 &&
+ git add file15 &&
+ git commit -m "on branch 10.b"
+ ) &&
+ git add subdir/subdir_module &&
+ git commit -m "change submodule in subdirectory on test$test_count.b" &&
+
+ test_must_fail git merge test$test_count.a &&
+ (
+ cd subdir &&
+ yes "l" | git mergetool subdir_module
+ ) &&
+ echo "test$test_count.b" >expect &&
+ test_cmp expect subdir/subdir_module/file15 &&
+ git submodule update -N &&
+ echo "test$test_count.b" >expect &&
+ test_cmp expect subdir/subdir_module/file15 &&
+ git reset --hard &&
+ git submodule update -N &&
+
+ test_must_fail git merge test$test_count.a &&
+ yes "r" | git mergetool subdir/subdir_module &&
+ echo "test$test_count.b" >expect &&
+ test_cmp expect subdir/subdir_module/file15 &&
+ git submodule update -N &&
+ echo "test$test_count.a" >expect &&
+ test_cmp expect subdir/subdir_module/file15 &&
+ git commit -m "branch1 resolved with mergetool"
+'
+
+test_expect_success 'directory vs modified submodule' '
+ test_when_finished "git reset --hard" &&
+ git checkout -b test$test_count branch1 &&
+ mv submod submod-movedaside &&
+ git rm --cached submod &&
+ mkdir submod &&
+ echo not a submodule >submod/file16 &&
+ git add submod/file16 &&
+ git commit -m "Submodule path becomes directory" &&
+
+ test_must_fail git merge main &&
+ test -n "$(git ls-files -u)" &&
+ yes "l" | git mergetool submod &&
+ echo "not a submodule" >expect &&
+ test_cmp expect submod/file16 &&
+ rm -rf submod.orig &&
+
+ git reset --hard &&
+ test_must_fail git merge main &&
+ test -n "$(git ls-files -u)" &&
+ test ! -e submod.orig &&
+ if test "$GIT_TEST_MERGE_ALGORITHM" = ort
+ then
+ yes "r" | git mergetool submod~main &&
+ git mv submod submod.orig &&
+ git mv submod~main submod
+ else
+ yes "r" | git mergetool submod
+ fi &&
+ test -d submod.orig &&
+ echo "not a submodule" >expect &&
+ test_cmp expect submod.orig/file16 &&
+ rm -r submod.orig &&
+ mv submod-movedaside/.git submod &&
+ ( cd submod && git clean -f && git reset --hard ) &&
+ git submodule update -N &&
+ echo "main submodule" >expect &&
+ test_cmp expect submod/bar &&
+ git reset --hard &&
+ rm -rf submod-movedaside &&
+
+ git checkout -b test$test_count.c main &&
+ git submodule update -N &&
+ test_must_fail git merge test$test_count &&
+ test -n "$(git ls-files -u)" &&
+ yes "l" | git mergetool submod &&
+ git submodule update -N &&
+ echo "main submodule" >expect &&
+ test_cmp expect submod/bar &&
+
+ git reset --hard &&
+ git submodule update -N &&
+ test_must_fail git merge test$test_count &&
+ test -n "$(git ls-files -u)" &&
+ test ! -e submod.orig &&
+ yes "r" | git mergetool submod &&
+ echo "not a submodule" >expect &&
+ test_cmp expect submod/file16 &&
+
+ git reset --hard main &&
+ ( cd submod && git clean -f && git reset --hard ) &&
+ git submodule update -N
+'
+
+test_expect_success 'file with no base' '
+ test_when_finished "git reset --hard" &&
+ git checkout -b test$test_count branch1 &&
+ test_must_fail git merge main &&
+ git mergetool --no-prompt --tool mybase -- both &&
+ test_must_be_empty both
+'
+
+test_expect_success 'custom commands override built-ins' '
+ test_when_finished "git reset --hard" &&
+ git checkout -b test$test_count branch1 &&
+ test_config mergetool.defaults.cmd "cat \"\$REMOTE\" >\"\$MERGED\"" &&
+ test_config mergetool.defaults.trustExitCode true &&
+ test_must_fail git merge main &&
+ git mergetool --no-prompt --tool defaults -- both &&
+ echo main both added >expected &&
+ test_cmp expected both
+'
+
+test_expect_success 'filenames seen by tools start with ./' '
+ test_when_finished "git reset --hard" &&
+ git checkout -b test$test_count branch1 &&
+ test_config mergetool.writeToTemp false &&
+ test_config mergetool.myecho.cmd "echo \"\$LOCAL\"" &&
+ test_config mergetool.myecho.trustExitCode true &&
+ test_must_fail git merge main &&
+ git mergetool --no-prompt --tool myecho -- both >actual &&
+ grep ^\./both_LOCAL_ actual
+'
+
+test_lazy_prereq MKTEMP '
+ tempdir=$(mktemp -d -t foo.XXXXXX) &&
+ test -d "$tempdir" &&
+ rmdir "$tempdir"
+'
+
+test_expect_success MKTEMP 'temporary filenames are used with mergetool.writeToTemp' '
+ test_when_finished "git reset --hard" &&
+ git checkout -b test$test_count branch1 &&
+ test_config mergetool.writeToTemp true &&
+ test_config mergetool.myecho.cmd "echo \"\$LOCAL\"" &&
+ test_config mergetool.myecho.trustExitCode true &&
+ test_must_fail git merge main &&
+ git mergetool --no-prompt --tool myecho -- both >actual &&
+ ! grep ^\./both_LOCAL_ actual &&
+ grep /both_LOCAL_ actual
+'
+
+test_expect_success 'diff.orderFile configuration is honored' '
+ test_when_finished "git reset --hard" &&
+ git checkout -b test$test_count order-file-side2 &&
+ test_config diff.orderFile order-file &&
+ test_config mergetool.myecho.cmd "echo \"\$LOCAL\"" &&
+ test_config mergetool.myecho.trustExitCode true &&
+ echo b >order-file &&
+ echo a >>order-file &&
+ test_must_fail git merge order-file-side1 &&
+ cat >expect <<-\EOF &&
+ Merging:
+ b
+ a
+ EOF
+
+ # make sure "order-file" that is ambiguous between
+ # rev and path is understood correctly.
+ git branch order-file HEAD &&
+
+ git mergetool --no-prompt --tool myecho >output &&
+ git grep --no-index -h -A2 Merging: output >actual &&
+ test_cmp expect actual
+'
+test_expect_success 'mergetool -Oorder-file is honored' '
+ test_when_finished "git reset --hard" &&
+ git checkout -b test$test_count order-file-side2 &&
+ test_config diff.orderFile order-file &&
+ test_config mergetool.myecho.cmd "echo \"\$LOCAL\"" &&
+ test_config mergetool.myecho.trustExitCode true &&
+ echo b >order-file &&
+ echo a >>order-file &&
+ test_must_fail git merge order-file-side1 &&
+ cat >expect <<-\EOF &&
+ Merging:
+ a
+ b
+ EOF
+ git mergetool -O/dev/null --no-prompt --tool myecho >output &&
+ git grep --no-index -h -A2 Merging: output >actual &&
+ test_cmp expect actual &&
+ git reset --hard &&
+
+ git config --unset diff.orderFile &&
+ test_must_fail git merge order-file-side1 &&
+ cat >expect <<-\EOF &&
+ Merging:
+ b
+ a
+ EOF
+ git mergetool -Oorder-file --no-prompt --tool myecho >output &&
+ git grep --no-index -h -A2 Merging: output >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'mergetool --tool-help shows recognized tools' '
+ # Check a few known tools are correctly shown
+ git mergetool --tool-help >mergetools &&
+ grep vimdiff mergetools &&
+ grep vimdiff3 mergetools &&
+ grep gvimdiff2 mergetools &&
+ grep araxis mergetools &&
+ grep xxdiff mergetools &&
+ grep meld mergetools
+'
+
+test_expect_success 'mergetool hideResolved' '
+ test_config mergetool.hideResolved true &&
+ test_when_finished "git reset --hard" &&
+ git checkout -b test${test_count}_b main &&
+ test_write_lines >file1 base "" a &&
+ git commit -a -m "base" &&
+ test_write_lines >file1 base "" c &&
+ git commit -a -m "remote update" &&
+ git checkout -b test${test_count}_a HEAD~ &&
+ test_write_lines >file1 local "" b &&
+ git commit -a -m "local update" &&
+ test_must_fail git merge test${test_count}_b &&
+ yes "" | git mergetool file1 &&
+ test_write_lines >expect local "" c &&
+ test_cmp expect file1 &&
+ git commit -m "test resolved with mergetool"
+'
+
+test_expect_success 'mergetool with guiDefault' '
+ test_config merge.guitool myguitool &&
+ test_config mergetool.myguitool.cmd "(printf \"gui \" && cat \"\$REMOTE\") >\"\$MERGED\"" &&
+ test_config mergetool.myguitool.trustExitCode true &&
+ test_when_finished "git reset --hard" &&
+ git checkout -b test$test_count branch1 &&
+ git submodule update -N &&
+ test_must_fail git merge main &&
+
+ test_config mergetool.guiDefault auto &&
+ DISPLAY=SOMETHING && export DISPLAY &&
+ yes "" | git mergetool both &&
+ yes "" | git mergetool file1 file1 &&
+
+ DISPLAY= && export DISPLAY &&
+ yes "" | git mergetool file2 "spaced name" &&
+
+ test_config mergetool.guiDefault true &&
+ yes "" | git mergetool subdir/file3 &&
+
+ yes "d" | git mergetool file11 &&
+ yes "d" | git mergetool file12 &&
+ yes "l" | git mergetool submod &&
+
+ echo "gui main updated" >expect &&
+ test_cmp expect file1 &&
+
+ echo "main new" >expect &&
+ test_cmp expect file2 &&
+
+ echo "gui main new sub" >expect &&
+ test_cmp expect subdir/file3 &&
+
+ echo "branch1 submodule" >expect &&
+ test_cmp expect submod/bar &&
+ git commit -m "branch1 resolved with mergetool"
+'
+
+test_done
diff --git a/t/t7611-merge-abort.sh b/t/t7611-merge-abort.sh
new file mode 100755
index 0000000..d6975ca
--- /dev/null
+++ b/t/t7611-merge-abort.sh
@@ -0,0 +1,204 @@
+#!/bin/sh
+
+test_description='test aborting in-progress merges
+
+Set up repo with conflicting and non-conflicting branches:
+
+There are three files foo/bar/baz, and the following graph illustrates the
+content of these files in each commit:
+
+# foo/bar/baz --- foo/bar/bazz <-- main
+# \
+# --- foo/barf/bazf <-- conflict_branch
+# \
+# --- foo/bart/baz <-- clean_branch
+
+Next, test git merge --abort with the following variables:
+- before/after successful merge (should fail when not in merge context)
+- with/without conflicts
+- clean/dirty index before merge
+- clean/dirty worktree before merge
+- dirty index before merge matches contents on remote branch
+- changed/unchanged worktree after merge
+- changed/unchanged index after merge
+'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ # Create the above repo
+ echo foo > foo &&
+ echo bar > bar &&
+ echo baz > baz &&
+ git add foo bar baz &&
+ git commit -m initial &&
+ echo bazz > baz &&
+ git commit -a -m "second" &&
+ git checkout -b conflict_branch HEAD^ &&
+ echo barf > bar &&
+ echo bazf > baz &&
+ git commit -a -m "conflict" &&
+ git checkout -b clean_branch HEAD^ &&
+ echo bart > bar &&
+ git commit -a -m "clean" &&
+ git checkout main
+'
+
+pre_merge_head="$(git rev-parse HEAD)"
+
+test_expect_success 'fails without MERGE_HEAD (unstarted merge)' '
+ test_must_fail git merge --abort 2>output &&
+ test_grep MERGE_HEAD output
+'
+
+test_expect_success 'fails without MERGE_HEAD (unstarted merge): .git/MERGE_HEAD sanity' '
+ test ! -f .git/MERGE_HEAD &&
+ test "$pre_merge_head" = "$(git rev-parse HEAD)"
+'
+
+test_expect_success 'fails without MERGE_HEAD (completed merge)' '
+ git merge clean_branch &&
+ test ! -f .git/MERGE_HEAD &&
+ # Merge successfully completed
+ post_merge_head="$(git rev-parse HEAD)" &&
+ test_must_fail git merge --abort 2>output &&
+ test_grep MERGE_HEAD output
+'
+
+test_expect_success 'fails without MERGE_HEAD (completed merge): .git/MERGE_HEAD sanity' '
+ test ! -f .git/MERGE_HEAD &&
+ test "$post_merge_head" = "$(git rev-parse HEAD)"
+'
+
+test_expect_success 'Forget previous merge' '
+ git reset --hard "$pre_merge_head"
+'
+
+test_expect_success 'Abort after --no-commit' '
+ # Redo merge, but stop before creating merge commit
+ git merge --no-commit clean_branch &&
+ test -f .git/MERGE_HEAD &&
+ # Abort non-conflicting merge
+ git merge --abort &&
+ test ! -f .git/MERGE_HEAD &&
+ test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
+ test -z "$(git diff)" &&
+ test -z "$(git diff --staged)"
+'
+
+test_expect_success 'Abort after conflicts' '
+ # Create conflicting merge
+ test_must_fail git merge conflict_branch &&
+ test -f .git/MERGE_HEAD &&
+ # Abort conflicting merge
+ git merge --abort &&
+ test ! -f .git/MERGE_HEAD &&
+ test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
+ test -z "$(git diff)" &&
+ test -z "$(git diff --staged)"
+'
+
+test_expect_success 'Clean merge with dirty index fails' '
+ echo xyzzy >> foo &&
+ git add foo &&
+ git diff --staged > expect &&
+ test_must_fail git merge clean_branch &&
+ test ! -f .git/MERGE_HEAD &&
+ test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
+ test -z "$(git diff)" &&
+ git diff --staged > actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Conflicting merge with dirty index fails' '
+ test_must_fail git merge conflict_branch &&
+ test ! -f .git/MERGE_HEAD &&
+ test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
+ test -z "$(git diff)" &&
+ git diff --staged > actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Reset index (but preserve worktree changes)' '
+ git reset "$pre_merge_head" &&
+ git diff > actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Abort clean merge with non-conflicting dirty worktree' '
+ git merge --no-commit clean_branch &&
+ test -f .git/MERGE_HEAD &&
+ # Abort merge
+ git merge --abort &&
+ test ! -f .git/MERGE_HEAD &&
+ test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
+ test -z "$(git diff --staged)" &&
+ git diff > actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Abort conflicting merge with non-conflicting dirty worktree' '
+ test_must_fail git merge conflict_branch &&
+ test -f .git/MERGE_HEAD &&
+ # Abort merge
+ git merge --abort &&
+ test ! -f .git/MERGE_HEAD &&
+ test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
+ test -z "$(git diff --staged)" &&
+ git diff > actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Reset worktree changes' '
+ git reset --hard "$pre_merge_head"
+'
+
+test_expect_success 'Fail clean merge with conflicting dirty worktree' '
+ echo xyzzy >> bar &&
+ git diff > expect &&
+ test_must_fail git merge --no-commit clean_branch &&
+ test ! -f .git/MERGE_HEAD &&
+ test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
+ test -z "$(git diff --staged)" &&
+ git diff > actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Fail conflicting merge with conflicting dirty worktree' '
+ test_must_fail git merge conflict_branch &&
+ test ! -f .git/MERGE_HEAD &&
+ test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
+ test -z "$(git diff --staged)" &&
+ git diff > actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Reset worktree changes' '
+ git reset --hard "$pre_merge_head"
+'
+
+test_expect_success 'Fail clean merge with matching dirty worktree' '
+ echo bart > bar &&
+ git diff > expect &&
+ test_must_fail git merge --no-commit clean_branch &&
+ test ! -f .git/MERGE_HEAD &&
+ test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
+ test -z "$(git diff --staged)" &&
+ git diff > actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Fail conflicting merge with matching dirty worktree' '
+ echo barf > bar &&
+ git diff > expect &&
+ test_must_fail git merge conflict_branch &&
+ test ! -f .git/MERGE_HEAD &&
+ test "$pre_merge_head" = "$(git rev-parse HEAD)" &&
+ test -z "$(git diff --staged)" &&
+ git diff > actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t7612-merge-verify-signatures.sh b/t/t7612-merge-verify-signatures.sh
new file mode 100755
index 0000000..84ddb56
--- /dev/null
+++ b/t/t7612-merge-verify-signatures.sh
@@ -0,0 +1,139 @@
+#!/bin/sh
+
+test_description='merge signature verification tests'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY/lib-gpg.sh"
+
+test_expect_success GPG 'create signed commits' '
+ echo 1 >file && git add file &&
+ test_tick && git commit -m initial &&
+ git tag initial &&
+
+ git checkout -b side-signed &&
+ echo 3 >elif && git add elif &&
+ test_tick && git commit -S -m "signed on side" &&
+ git checkout initial &&
+
+ git checkout -b side-unsigned &&
+ echo 3 >foo && git add foo &&
+ test_tick && git commit -m "unsigned on side" &&
+ git checkout initial &&
+
+ git checkout -b side-bad &&
+ echo 3 >bar && git add bar &&
+ test_tick && git commit -S -m "bad on side" &&
+ git cat-file commit side-bad >raw &&
+ sed -e "s/^bad/forged bad/" raw >forged &&
+ git hash-object -w -t commit forged >forged.commit &&
+ git checkout initial &&
+
+ git checkout -b side-untrusted &&
+ echo 3 >baz && git add baz &&
+ test_tick && git commit -SB7227189 -m "untrusted on side" &&
+
+ git checkout main
+'
+
+test_expect_success GPG 'merge unsigned commit with verification' '
+ test_when_finished "git reset --hard && git checkout initial" &&
+ test_must_fail git merge --ff-only --verify-signatures side-unsigned 2>mergeerror &&
+ test_grep "does not have a GPG signature" mergeerror
+'
+
+test_expect_success GPG 'merge unsigned commit with merge.verifySignatures=true' '
+ test_when_finished "git reset --hard && git checkout initial" &&
+ test_config merge.verifySignatures true &&
+ test_must_fail git merge --ff-only side-unsigned 2>mergeerror &&
+ test_grep "does not have a GPG signature" mergeerror
+'
+
+test_expect_success GPG 'merge commit with bad signature with verification' '
+ test_when_finished "git reset --hard && git checkout initial" &&
+ test_must_fail git merge --ff-only --verify-signatures $(cat forged.commit) 2>mergeerror &&
+ test_grep "has a bad GPG signature" mergeerror
+'
+
+test_expect_success GPG 'merge commit with bad signature with merge.verifySignatures=true' '
+ test_when_finished "git reset --hard && git checkout initial" &&
+ test_config merge.verifySignatures true &&
+ test_must_fail git merge --ff-only $(cat forged.commit) 2>mergeerror &&
+ test_grep "has a bad GPG signature" mergeerror
+'
+
+test_expect_success GPG 'merge commit with untrusted signature with verification' '
+ test_when_finished "git reset --hard && git checkout initial" &&
+ test_must_fail git merge --ff-only --verify-signatures side-untrusted 2>mergeerror &&
+ test_grep "has an untrusted GPG signature" mergeerror
+'
+
+test_expect_success GPG 'merge commit with untrusted signature with verification and high minTrustLevel' '
+ test_when_finished "git reset --hard && git checkout initial" &&
+ test_config gpg.minTrustLevel marginal &&
+ test_must_fail git merge --ff-only --verify-signatures side-untrusted 2>mergeerror &&
+ test_grep "has an untrusted GPG signature" mergeerror
+'
+
+test_expect_success GPG 'merge commit with untrusted signature with verification and low minTrustLevel' '
+ test_when_finished "git reset --hard && git checkout initial" &&
+ test_config gpg.minTrustLevel undefined &&
+ git merge --ff-only --verify-signatures side-untrusted >mergeoutput &&
+ test_grep "has a good GPG signature" mergeoutput
+'
+
+test_expect_success GPG 'merge commit with untrusted signature with merge.verifySignatures=true' '
+ test_when_finished "git reset --hard && git checkout initial" &&
+ test_config merge.verifySignatures true &&
+ test_must_fail git merge --ff-only side-untrusted 2>mergeerror &&
+ test_grep "has an untrusted GPG signature" mergeerror
+'
+
+test_expect_success GPG 'merge commit with untrusted signature with merge.verifySignatures=true and minTrustLevel' '
+ test_when_finished "git reset --hard && git checkout initial" &&
+ test_config merge.verifySignatures true &&
+ test_config gpg.minTrustLevel marginal &&
+ test_must_fail git merge --ff-only side-untrusted 2>mergeerror &&
+ test_grep "has an untrusted GPG signature" mergeerror
+'
+
+test_expect_success GPG 'merge signed commit with verification' '
+ test_when_finished "git reset --hard && git checkout initial" &&
+ git merge --verbose --ff-only --verify-signatures side-signed >mergeoutput &&
+ test_grep "has a good GPG signature" mergeoutput
+'
+
+test_expect_success GPG 'merge signed commit with merge.verifySignatures=true' '
+ test_when_finished "git reset --hard && git checkout initial" &&
+ test_config merge.verifySignatures true &&
+ git merge --verbose --ff-only side-signed >mergeoutput &&
+ test_grep "has a good GPG signature" mergeoutput
+'
+
+test_expect_success GPG 'merge commit with bad signature without verification' '
+ test_when_finished "git reset --hard && git checkout initial" &&
+ git merge $(cat forged.commit)
+'
+
+test_expect_success GPG 'merge commit with bad signature with merge.verifySignatures=false' '
+ test_when_finished "git reset --hard && git checkout initial" &&
+ test_config merge.verifySignatures false &&
+ git merge $(cat forged.commit)
+'
+
+test_expect_success GPG 'merge commit with bad signature with merge.verifySignatures=true and --no-verify-signatures' '
+ test_when_finished "git reset --hard && git checkout initial" &&
+ test_config merge.verifySignatures true &&
+ git merge --no-verify-signatures $(cat forged.commit)
+'
+
+test_expect_success GPG 'merge unsigned commit into unborn branch' '
+ test_when_finished "git checkout initial" &&
+ git checkout --orphan unborn &&
+ test_must_fail git merge --verify-signatures side-unsigned 2>mergeerror &&
+ test_grep "does not have a GPG signature" mergeerror
+'
+
+test_done
diff --git a/t/t7614-merge-signoff.sh b/t/t7614-merge-signoff.sh
new file mode 100755
index 0000000..cf96a35
--- /dev/null
+++ b/t/t7614-merge-signoff.sh
@@ -0,0 +1,73 @@
+#!/bin/sh
+
+test_description='git merge --signoff
+
+This test runs git merge --signoff and makes sure that it works.
+'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# Setup test files
+test_setup() {
+ # Expected commit message after merge --signoff
+ cat >expected-signed <<EOF &&
+Merge branch 'main' into other-branch
+
+Signed-off-by: $(git var GIT_COMMITTER_IDENT | sed -e "s/>.*/>/")
+EOF
+
+ # Expected commit message after merge without --signoff (or with --no-signoff)
+ cat >expected-unsigned <<EOF &&
+Merge branch 'main' into other-branch
+EOF
+
+ # Initial commit and feature branch to merge main into it.
+ git commit --allow-empty -m "Initial empty commit" &&
+ git checkout -b other-branch &&
+ test_commit other-branch file1 1
+}
+
+# Setup repository, files & feature branch
+# This step must be run if You want to test 2,3 or 4
+# Order of 2,3,4 is not important, but 1 must be run before
+# For example `-r 1,4` or `-r 1,4,2 -v` etc
+# But not `-r 2` or `-r 4,3,2,1`
+test_expect_success 'setup' '
+ test_setup
+'
+
+# Test with --signoff flag
+test_expect_success 'git merge --signoff adds a sign-off line' '
+ git checkout main &&
+ test_commit main-branch-2 file2 2 &&
+ git checkout other-branch &&
+ git merge main --signoff --no-edit &&
+ git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
+ test_cmp expected-signed actual
+'
+
+# Test without --signoff flag
+test_expect_success 'git merge does not add a sign-off line' '
+ git checkout main &&
+ test_commit main-branch-3 file3 3 &&
+ git checkout other-branch &&
+ git merge main --no-edit &&
+ git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
+ test_cmp expected-unsigned actual
+'
+
+# Test for --no-signoff flag
+test_expect_success 'git merge --no-signoff flag cancels --signoff flag' '
+ git checkout main &&
+ test_commit main-branch-4 file4 4 &&
+ git checkout other-branch &&
+ git merge main --no-edit --signoff --no-signoff &&
+ git cat-file commit HEAD | sed -e "1,/^\$/d" >actual &&
+ test_cmp expected-unsigned actual
+'
+
+test_done
diff --git a/t/t7700-repack.sh b/t/t7700-repack.sh
new file mode 100755
index 0000000..d2975e6
--- /dev/null
+++ b/t/t7700-repack.sh
@@ -0,0 +1,833 @@
+#!/bin/sh
+
+test_description='git repack works correctly'
+
+. ./test-lib.sh
+. "${TEST_DIRECTORY}/lib-bitmap.sh"
+. "${TEST_DIRECTORY}/lib-midx.sh"
+. "${TEST_DIRECTORY}/lib-terminal.sh"
+
+commit_and_pack () {
+ test_commit "$@" 1>&2 &&
+ incrpackid=$(git pack-objects --all --unpacked --incremental .git/objects/pack/pack </dev/null) &&
+ # Remove any loose object(s) created by test_commit, since they have
+ # already been packed. Leaving these around can create subtly different
+ # packs with `pack-objects`'s `--unpacked` option.
+ git prune-packed 1>&2 &&
+ echo pack-${incrpackid}.pack
+}
+
+test_no_missing_in_packs () {
+ myidx=$(ls -1 .git/objects/pack/*.idx) &&
+ test_path_is_file "$myidx" &&
+ git verify-pack -v alt_objects/pack/*.idx >orig.raw &&
+ sed -n -e "s/^\($OID_REGEX\).*/\1/p" orig.raw | sort >orig &&
+ git verify-pack -v $myidx >dest.raw &&
+ cut -d" " -f1 dest.raw | sort >dest &&
+ comm -23 orig dest >missing &&
+ test_must_be_empty missing
+}
+
+# we expect $packid and $oid to be defined
+test_has_duplicate_object () {
+ want_duplicate_object="$1"
+ found_duplicate_object=false
+ for p in .git/objects/pack/*.idx
+ do
+ idx=$(basename $p)
+ test "pack-$packid.idx" = "$idx" && continue
+ git verify-pack -v $p >packlist || return $?
+ if grep "^$oid" packlist
+ then
+ found_duplicate_object=true
+ echo "DUPLICATE OBJECT FOUND"
+ break
+ fi
+ done &&
+ test "$want_duplicate_object" = "$found_duplicate_object"
+}
+
+test_expect_success 'objects in packs marked .keep are not repacked' '
+ echo content1 >file1 &&
+ echo content2 >file2 &&
+ git add . &&
+ test_tick &&
+ git commit -m initial_commit &&
+ # Create two packs
+ # The first pack will contain all of the objects except one
+ git rev-list --objects --all >objs &&
+ grep -v file2 objs | git pack-objects pack &&
+ # The second pack will contain the excluded object
+ packid=$(grep file2 objs | git pack-objects pack) &&
+ >pack-$packid.keep &&
+ git verify-pack -v pack-$packid.idx >packlist &&
+ oid=$(head -n 1 packlist | sed -e "s/^\($OID_REGEX\).*/\1/") &&
+ mv pack-* .git/objects/pack/ &&
+ git repack -A -d -l &&
+ git prune-packed &&
+ test_has_duplicate_object false
+'
+
+test_expect_success 'writing bitmaps via command-line can duplicate .keep objects' '
+ # build on $oid, $packid, and .keep state from previous
+ GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 git repack -Adbl &&
+ test_has_duplicate_object true
+'
+
+test_expect_success 'writing bitmaps via config can duplicate .keep objects' '
+ # build on $oid, $packid, and .keep state from previous
+ GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
+ git -c repack.writebitmaps=true repack -Adl &&
+ test_has_duplicate_object true
+'
+
+test_expect_success 'loose objects in alternate ODB are not repacked' '
+ mkdir alt_objects &&
+ echo $(pwd)/alt_objects >.git/objects/info/alternates &&
+ echo content3 >file3 &&
+ oid=$(GIT_OBJECT_DIRECTORY=alt_objects git hash-object -w file3) &&
+ git add file3 &&
+ test_tick &&
+ git commit -m commit_file3 &&
+ git repack -a -d -l &&
+ git prune-packed &&
+ test_has_duplicate_object false
+'
+
+test_expect_success SYMLINKS '--local keeps packs when alternate is objectdir ' '
+ test_when_finished "rm -rf repo" &&
+ git init repo &&
+ test_commit -C repo A &&
+ (
+ cd repo &&
+ git repack -a &&
+ ls .git/objects/pack/*.pack >../expect &&
+ ln -s objects .git/alt_objects &&
+ echo "$(pwd)/.git/alt_objects" >.git/objects/info/alternates &&
+ git repack -a -d -l &&
+ ls .git/objects/pack/*.pack >../actual
+ ) &&
+ test_cmp expect actual
+'
+
+test_expect_success '--local disables writing bitmaps when connected to alternate ODB' '
+ test_when_finished "rm -rf shared member" &&
+
+ git init shared &&
+ git clone --shared shared member &&
+ (
+ cd member &&
+ test_commit "object" &&
+ GIT_TEST_MULTI_PACK_INDEX=0 git repack -Adl --write-bitmap-index 2>err &&
+ cat >expect <<-EOF &&
+ warning: disabling bitmap writing, as some objects are not being packed
+ EOF
+ test_cmp expect err &&
+ test_path_is_missing .git/objects/pack-*.bitmap
+ )
+'
+
+test_expect_success 'packed obs in alt ODB are repacked even when local repo is packless' '
+ mkdir alt_objects/pack &&
+ mv .git/objects/pack/* alt_objects/pack &&
+ git repack -a &&
+ test_no_missing_in_packs
+'
+
+test_expect_success 'packed obs in alt ODB are repacked when local repo has packs' '
+ rm -f .git/objects/pack/* &&
+ echo new_content >>file1 &&
+ git add file1 &&
+ test_tick &&
+ git commit -m more_content &&
+ git repack &&
+ git repack -a -d &&
+ test_no_missing_in_packs
+'
+
+test_expect_success 'packed obs in alternate ODB kept pack are repacked' '
+ # swap the .keep so the commit object is in the pack with .keep
+ for p in alt_objects/pack/*.pack
+ do
+ base_name=$(basename $p .pack) &&
+ if test_path_is_file alt_objects/pack/$base_name.keep
+ then
+ rm alt_objects/pack/$base_name.keep
+ else
+ touch alt_objects/pack/$base_name.keep
+ fi || return 1
+ done &&
+ git repack -a -d &&
+ test_no_missing_in_packs
+'
+
+test_expect_success 'packed unreachable obs in alternate ODB are not loosened' '
+ rm -f alt_objects/pack/*.keep &&
+ mv .git/objects/pack/* alt_objects/pack/ &&
+ coid=$(git rev-parse HEAD^{commit}) &&
+ git reset --hard HEAD^ &&
+ test_tick &&
+ git reflog expire --expire=$test_tick --expire-unreachable=$test_tick --all &&
+ # The pack-objects call on the next line is equivalent to
+ # git repack -A -d without the call to prune-packed
+ git pack-objects --honor-pack-keep --non-empty --all --reflog \
+ --unpack-unreachable </dev/null pack &&
+ rm -f .git/objects/pack/* &&
+ mv pack-* .git/objects/pack/ &&
+ git verify-pack -v -- .git/objects/pack/*.idx >packlist &&
+ ! grep "^$coid " packlist &&
+ echo >.git/objects/info/alternates &&
+ test_must_fail git show $coid
+'
+
+test_expect_success 'local packed unreachable obs that exist in alternate ODB are not loosened' '
+ echo $(pwd)/alt_objects >.git/objects/info/alternates &&
+ echo "$coid" | git pack-objects --non-empty --all --reflog pack &&
+ rm -f .git/objects/pack/* &&
+ mv pack-* .git/objects/pack/ &&
+ # The pack-objects call on the next line is equivalent to
+ # git repack -A -d without the call to prune-packed
+ git pack-objects --honor-pack-keep --non-empty --all --reflog \
+ --unpack-unreachable </dev/null pack &&
+ rm -f .git/objects/pack/* &&
+ mv pack-* .git/objects/pack/ &&
+ git verify-pack -v -- .git/objects/pack/*.idx >packlist &&
+ ! grep "^$coid " &&
+ echo >.git/objects/info/alternates &&
+ test_must_fail git show $coid
+'
+
+test_expect_success 'objects made unreachable by grafts only are kept' '
+ test_tick &&
+ git commit --allow-empty -m "commit 4" &&
+ H0=$(git rev-parse HEAD) &&
+ H1=$(git rev-parse HEAD^) &&
+ H2=$(git rev-parse HEAD^^) &&
+ echo "$H0 $H2" >.git/info/grafts &&
+ git reflog expire --expire=$test_tick --expire-unreachable=$test_tick --all &&
+ git repack -a -d &&
+ git cat-file -t $H1
+'
+
+test_expect_success 'repack --keep-pack' '
+ test_create_repo keep-pack &&
+ (
+ cd keep-pack &&
+ # avoid producing different packs due to delta/base choices
+ git config pack.window 0 &&
+ P1=$(commit_and_pack 1) &&
+ P2=$(commit_and_pack 2) &&
+ P3=$(commit_and_pack 3) &&
+ P4=$(commit_and_pack 4) &&
+ ls .git/objects/pack/*.pack >old-counts &&
+ test_line_count = 4 old-counts &&
+ git repack -a -d --keep-pack $P1 --keep-pack $P4 &&
+ ls .git/objects/pack/*.pack >new-counts &&
+ grep -q $P1 new-counts &&
+ grep -q $P4 new-counts &&
+ test_line_count = 3 new-counts &&
+ git fsck &&
+
+ P5=$(commit_and_pack --no-tag 5) &&
+ git reset --hard HEAD^ &&
+ git reflog expire --all --expire=all &&
+ rm -f ".git/objects/pack/${P5%.pack}.idx" &&
+ rm -f ".git/objects/info/commit-graph" &&
+ for from in $(find .git/objects/pack -type f -name "${P5%.pack}.*")
+ do
+ to="$(dirname "$from")/.tmp-1234-$(basename "$from")" &&
+ mv "$from" "$to" || return 1
+ done &&
+
+ # A .idx file without a .pack should not stop us from
+ # repacking what we can.
+ touch .git/objects/pack/pack-does-not-exist.idx &&
+
+ git repack --cruft -d --keep-pack $P1 --keep-pack $P4 &&
+
+ ls .git/objects/pack/*.pack >newer-counts &&
+ test_cmp new-counts newer-counts &&
+ git fsck
+ )
+'
+
+test_expect_success 'repacking fails when missing .pack actually means missing objects' '
+ test_create_repo idx-without-pack &&
+ (
+ cd idx-without-pack &&
+
+ # Avoid producing different packs due to delta/base choices
+ git config pack.window 0 &&
+ P1=$(commit_and_pack 1) &&
+ P2=$(commit_and_pack 2) &&
+ P3=$(commit_and_pack 3) &&
+ P4=$(commit_and_pack 4) &&
+ ls .git/objects/pack/*.pack >old-counts &&
+ test_line_count = 4 old-counts &&
+
+ # Remove one .pack file
+ rm .git/objects/pack/$P2 &&
+
+ ls .git/objects/pack/*.pack >before-pack-dir &&
+
+ test_must_fail git fsck &&
+ test_must_fail git repack --cruft -d 2>err &&
+ grep "bad object" err &&
+
+ # Before failing, the repack did not modify the
+ # pack directory.
+ ls .git/objects/pack/*.pack >after-pack-dir &&
+ test_cmp before-pack-dir after-pack-dir
+ )
+'
+
+test_expect_success 'bitmaps are created by default in bare repos' '
+ git clone --bare .git bare.git &&
+ rm -f bare.git/objects/pack/*.bitmap &&
+ GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
+ git -C bare.git repack -ad &&
+ bitmap=$(ls bare.git/objects/pack/*.bitmap) &&
+ test_path_is_file "$bitmap"
+'
+
+test_expect_success 'incremental repack does not complain' '
+ git -C bare.git repack -q 2>repack.err &&
+ test_must_be_empty repack.err
+'
+
+test_expect_success 'bitmaps can be disabled on bare repos' '
+ GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
+ git -c repack.writeBitmaps=false -C bare.git repack -ad &&
+ bitmap=$(ls bare.git/objects/pack/*.bitmap || :) &&
+ test -z "$bitmap"
+'
+
+test_expect_success 'no bitmaps created if .keep files present' '
+ pack=$(ls bare.git/objects/pack/*.pack) &&
+ test_path_is_file "$pack" &&
+ keep=${pack%.pack}.keep &&
+ test_when_finished "rm -f \"\$keep\"" &&
+ >"$keep" &&
+ GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
+ git -C bare.git repack -ad 2>stderr &&
+ test_must_be_empty stderr &&
+ find bare.git/objects/pack/ -type f -name "*.bitmap" >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'auto-bitmaps do not complain if unavailable' '
+ test_config -C bare.git pack.packSizeLimit 1M &&
+ blob=$(test-tool genrandom big $((1024*1024)) |
+ git -C bare.git hash-object -w --stdin) &&
+ git -C bare.git update-ref refs/tags/big $blob &&
+ GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
+ git -C bare.git repack -ad 2>stderr &&
+ test_must_be_empty stderr &&
+ find bare.git/objects/pack -type f -name "*.bitmap" >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'repacking with a filter works' '
+ git -C bare.git repack -a -d &&
+ test_stdout_line_count = 1 ls bare.git/objects/pack/*.pack &&
+ git -C bare.git -c repack.writebitmaps=false repack -a -d --filter=blob:none &&
+ test_stdout_line_count = 2 ls bare.git/objects/pack/*.pack &&
+ commit_pack=$(test-tool -C bare.git find-pack -c 1 HEAD) &&
+ blob_pack=$(test-tool -C bare.git find-pack -c 1 HEAD:file1) &&
+ test "$commit_pack" != "$blob_pack" &&
+ tree_pack=$(test-tool -C bare.git find-pack -c 1 HEAD^{tree}) &&
+ test "$tree_pack" = "$commit_pack" &&
+ blob_pack2=$(test-tool -C bare.git find-pack -c 1 HEAD:file2) &&
+ test "$blob_pack2" = "$blob_pack"
+'
+
+test_expect_success '--filter fails with --write-bitmap-index' '
+ test_must_fail \
+ env GIT_TEST_MULTI_PACK_INDEX_WRITE_BITMAP=0 \
+ git -C bare.git repack -a -d --write-bitmap-index --filter=blob:none
+'
+
+test_expect_success 'repacking with two filters works' '
+ git init two-filters &&
+ (
+ cd two-filters &&
+ mkdir subdir &&
+ test_commit foo &&
+ test_commit subdir_bar subdir/bar &&
+ test_commit subdir_baz subdir/baz
+ ) &&
+ git clone --no-local --bare two-filters two-filters.git &&
+ (
+ cd two-filters.git &&
+ test_stdout_line_count = 1 ls objects/pack/*.pack &&
+ git -c repack.writebitmaps=false repack -a -d \
+ --filter=blob:none --filter=tree:1 &&
+ test_stdout_line_count = 2 ls objects/pack/*.pack &&
+ commit_pack=$(test-tool find-pack -c 1 HEAD) &&
+ blob_pack=$(test-tool find-pack -c 1 HEAD:foo.t) &&
+ root_tree_pack=$(test-tool find-pack -c 1 HEAD^{tree}) &&
+ subdir_tree_hash=$(git ls-tree --object-only HEAD -- subdir) &&
+ subdir_tree_pack=$(test-tool find-pack -c 1 "$subdir_tree_hash") &&
+
+ # Root tree and subdir tree are not in the same packfiles
+ test "$commit_pack" != "$blob_pack" &&
+ test "$commit_pack" = "$root_tree_pack" &&
+ test "$blob_pack" = "$subdir_tree_pack"
+ )
+'
+
+prepare_for_keep_packs () {
+ git init keep-packs &&
+ (
+ cd keep-packs &&
+ test_commit foo &&
+ test_commit bar
+ ) &&
+ git clone --no-local --bare keep-packs keep-packs.git &&
+ (
+ cd keep-packs.git &&
+
+ # Create two packs
+ # The first pack will contain all of the objects except one blob
+ git rev-list --objects --all >objs &&
+ grep -v "bar.t" objs | git pack-objects pack &&
+ # The second pack will contain the excluded object and be kept
+ packid=$(grep "bar.t" objs | git pack-objects pack) &&
+ >pack-$packid.keep &&
+
+ # Replace the existing pack with the 2 new ones
+ rm -f objects/pack/pack* &&
+ mv pack-* objects/pack/
+ )
+}
+
+test_expect_success '--filter works with .keep packs' '
+ prepare_for_keep_packs &&
+ (
+ cd keep-packs.git &&
+
+ foo_pack=$(test-tool find-pack -c 1 HEAD:foo.t) &&
+ bar_pack=$(test-tool find-pack -c 1 HEAD:bar.t) &&
+ head_pack=$(test-tool find-pack -c 1 HEAD) &&
+
+ test "$foo_pack" != "$bar_pack" &&
+ test "$foo_pack" = "$head_pack" &&
+
+ git -c repack.writebitmaps=false repack -a -d --filter=blob:none &&
+
+ foo_pack_1=$(test-tool find-pack -c 1 HEAD:foo.t) &&
+ bar_pack_1=$(test-tool find-pack -c 1 HEAD:bar.t) &&
+ head_pack_1=$(test-tool find-pack -c 1 HEAD) &&
+
+ # Object bar is still only in the old .keep pack
+ test "$foo_pack_1" != "$foo_pack" &&
+ test "$bar_pack_1" = "$bar_pack" &&
+ test "$head_pack_1" != "$head_pack" &&
+
+ test "$foo_pack_1" != "$bar_pack_1" &&
+ test "$foo_pack_1" != "$head_pack_1" &&
+ test "$bar_pack_1" != "$head_pack_1"
+ )
+'
+
+test_expect_success '--filter works with --pack-kept-objects and .keep packs' '
+ rm -rf keep-packs keep-packs.git &&
+ prepare_for_keep_packs &&
+ (
+ cd keep-packs.git &&
+
+ foo_pack=$(test-tool find-pack -c 1 HEAD:foo.t) &&
+ bar_pack=$(test-tool find-pack -c 1 HEAD:bar.t) &&
+ head_pack=$(test-tool find-pack -c 1 HEAD) &&
+
+ test "$foo_pack" != "$bar_pack" &&
+ test "$foo_pack" = "$head_pack" &&
+
+ git -c repack.writebitmaps=false repack -a -d --filter=blob:none \
+ --pack-kept-objects &&
+
+ foo_pack_1=$(test-tool find-pack -c 1 HEAD:foo.t) &&
+ test-tool find-pack -c 2 HEAD:bar.t >bar_pack_1 &&
+ head_pack_1=$(test-tool find-pack -c 1 HEAD) &&
+
+ test "$foo_pack_1" != "$foo_pack" &&
+ test "$foo_pack_1" != "$bar_pack" &&
+ test "$head_pack_1" != "$head_pack" &&
+
+ # Object bar is in both the old .keep pack and the new
+ # pack that contained the filtered out objects
+ grep "$bar_pack" bar_pack_1 &&
+ grep "$foo_pack_1" bar_pack_1 &&
+ test "$foo_pack_1" != "$head_pack_1"
+ )
+'
+
+test_expect_success '--filter-to stores filtered out objects' '
+ git -C bare.git repack -a -d &&
+ test_stdout_line_count = 1 ls bare.git/objects/pack/*.pack &&
+
+ git init --bare filtered.git &&
+ git -C bare.git -c repack.writebitmaps=false repack -a -d \
+ --filter=blob:none \
+ --filter-to=../filtered.git/objects/pack/pack &&
+ test_stdout_line_count = 1 ls bare.git/objects/pack/pack-*.pack &&
+ test_stdout_line_count = 1 ls filtered.git/objects/pack/pack-*.pack &&
+
+ commit_pack=$(test-tool -C bare.git find-pack -c 1 HEAD) &&
+ blob_pack=$(test-tool -C bare.git find-pack -c 0 HEAD:file1) &&
+ blob_hash=$(git -C bare.git rev-parse HEAD:file1) &&
+ test -n "$blob_hash" &&
+ blob_pack=$(test-tool -C filtered.git find-pack -c 1 $blob_hash) &&
+
+ echo $(pwd)/filtered.git/objects >bare.git/objects/info/alternates &&
+ blob_pack=$(test-tool -C bare.git find-pack -c 1 HEAD:file1) &&
+ blob_content=$(git -C bare.git show $blob_hash) &&
+ test "$blob_content" = "content1"
+'
+
+test_expect_success '--filter works with --max-pack-size' '
+ rm -rf filtered.git &&
+ git init --bare filtered.git &&
+ git init max-pack-size &&
+ (
+ cd max-pack-size &&
+ test_commit base &&
+ # two blobs which exceed the maximum pack size
+ test-tool genrandom foo 1048576 >foo &&
+ git hash-object -w foo &&
+ test-tool genrandom bar 1048576 >bar &&
+ git hash-object -w bar &&
+ git add foo bar &&
+ git commit -m "adding foo and bar"
+ ) &&
+ git clone --no-local --bare max-pack-size max-pack-size.git &&
+ (
+ cd max-pack-size.git &&
+ git -c repack.writebitmaps=false repack -a -d --filter=blob:none \
+ --max-pack-size=1M \
+ --filter-to=../filtered.git/objects/pack/pack &&
+ echo $(cd .. && pwd)/filtered.git/objects >objects/info/alternates &&
+
+ # Check that the 3 blobs are in different packfiles in filtered.git
+ test_stdout_line_count = 3 ls ../filtered.git/objects/pack/pack-*.pack &&
+ test_stdout_line_count = 1 ls objects/pack/pack-*.pack &&
+ foo_pack=$(test-tool find-pack -c 1 HEAD:foo) &&
+ bar_pack=$(test-tool find-pack -c 1 HEAD:bar) &&
+ base_pack=$(test-tool find-pack -c 1 HEAD:base.t) &&
+ test "$foo_pack" != "$bar_pack" &&
+ test "$foo_pack" != "$base_pack" &&
+ test "$bar_pack" != "$base_pack" &&
+ for pack in "$foo_pack" "$bar_pack" "$base_pack"
+ do
+ case "$foo_pack" in */filtered.git/objects/pack/*) true ;; *) return 1 ;; esac
+ done
+ )
+'
+
+objdir=.git/objects
+midx=$objdir/pack/multi-pack-index
+
+test_expect_success 'setup for --write-midx tests' '
+ git init midx &&
+ (
+ cd midx &&
+ git config core.multiPackIndex true &&
+
+ test_commit base
+ )
+'
+
+test_expect_success '--write-midx unchanged' '
+ (
+ cd midx &&
+ GIT_TEST_MULTI_PACK_INDEX=0 git repack &&
+ test_path_is_missing $midx &&
+ test_path_is_missing $midx-*.bitmap &&
+
+ GIT_TEST_MULTI_PACK_INDEX=0 git repack --write-midx &&
+
+ test_path_is_file $midx &&
+ test_path_is_missing $midx-*.bitmap &&
+ test_midx_consistent $objdir
+ )
+'
+
+test_expect_success '--write-midx with a new pack' '
+ (
+ cd midx &&
+ test_commit loose &&
+
+ GIT_TEST_MULTI_PACK_INDEX=0 git repack --write-midx &&
+
+ test_path_is_file $midx &&
+ test_path_is_missing $midx-*.bitmap &&
+ test_midx_consistent $objdir
+ )
+'
+
+test_expect_success '--write-midx with -b' '
+ (
+ cd midx &&
+ GIT_TEST_MULTI_PACK_INDEX=0 git repack -mb &&
+
+ test_path_is_file $midx &&
+ test_path_is_file $midx-*.bitmap &&
+ test_midx_consistent $objdir
+ )
+'
+
+test_expect_success '--write-midx with -d' '
+ (
+ cd midx &&
+ test_commit repack &&
+
+ GIT_TEST_MULTI_PACK_INDEX=0 git repack -Ad --write-midx &&
+
+ test_path_is_file $midx &&
+ test_path_is_missing $midx-*.bitmap &&
+ test_midx_consistent $objdir
+ )
+'
+
+test_expect_success 'cleans up MIDX when appropriate' '
+ (
+ cd midx &&
+
+ test_commit repack-2 &&
+ GIT_TEST_MULTI_PACK_INDEX=0 git repack -Adb --write-midx &&
+
+ checksum=$(midx_checksum $objdir) &&
+ test_path_is_file $midx &&
+ test_path_is_file $midx-$checksum.bitmap &&
+
+ test_commit repack-3 &&
+ GIT_TEST_MULTI_PACK_INDEX=0 git repack -Adb --write-midx &&
+
+ test_path_is_file $midx &&
+ test_path_is_missing $midx-$checksum.bitmap &&
+ test_path_is_file $midx-$(midx_checksum $objdir).bitmap &&
+
+ test_commit repack-4 &&
+ GIT_TEST_MULTI_PACK_INDEX=0 git repack -Adb &&
+
+ find $objdir/pack -type f -name "multi-pack-index*" >files &&
+ test_must_be_empty files
+ )
+'
+
+test_expect_success '--write-midx with preferred bitmap tips' '
+ git init midx-preferred-tips &&
+ test_when_finished "rm -fr midx-preferred-tips" &&
+ (
+ cd midx-preferred-tips &&
+
+ test_commit_bulk --message="%s" 103 &&
+
+ git log --format="%H" >commits.raw &&
+ sort <commits.raw >commits &&
+
+ git log --format="create refs/tags/%s/%s %H" HEAD >refs &&
+ git update-ref --stdin <refs &&
+
+ git repack --write-midx --write-bitmap-index &&
+ test_path_is_file $midx &&
+ test_path_is_file $midx-$(midx_checksum $objdir).bitmap &&
+
+ test-tool bitmap list-commits | sort >bitmaps &&
+ comm -13 bitmaps commits >before &&
+ test_line_count = 1 before &&
+
+ rm -fr $midx-$(midx_checksum $objdir).bitmap &&
+ rm -fr $midx &&
+
+ # instead of constructing the snapshot ourselves (c.f., the test
+ # "write a bitmap with --refs-snapshot (preferred tips)" in
+ # t5326), mark the missing commit as preferred by adding it to
+ # the pack.preferBitmapTips configuration.
+ git for-each-ref --format="%(refname:rstrip=1)" \
+ --points-at="$(cat before)" >missing &&
+ git config pack.preferBitmapTips "$(cat missing)" &&
+ git repack --write-midx --write-bitmap-index &&
+
+ test-tool bitmap list-commits | sort >bitmaps &&
+ comm -13 bitmaps commits >after &&
+
+ ! test_cmp before after
+ )
+'
+
+# The first argument is expected to be a filename
+# and that file should contain the name of a .idx
+# file. Send the list of objects in that .idx file
+# into stdout.
+get_sorted_objects_from_pack () {
+ git show-index <$(cat "$1") >raw &&
+ cut -d" " -f2 raw
+}
+
+test_expect_success '--write-midx -b packs non-kept objects' '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+
+ # Create a kept pack-file
+ test_commit base &&
+ git repack -ad &&
+ find $objdir/pack -name "*.idx" >before &&
+ test_line_count = 1 before &&
+ before_name=$(cat before) &&
+ >${before_name%.idx}.keep &&
+
+ # Create a non-kept pack-file
+ test_commit other &&
+ git repack &&
+
+ # Create loose objects
+ test_commit loose &&
+
+ # Repack everything
+ git repack --write-midx -a -b -d &&
+
+ # There should be two pack-files now, the
+ # old, kept pack and the new, non-kept pack.
+ find $objdir/pack -name "*.idx" | sort >after &&
+ test_line_count = 2 after &&
+ find $objdir/pack -name "*.keep" >kept &&
+ kept_name=$(cat kept) &&
+ echo ${kept_name%.keep}.idx >kept-idx &&
+ test_cmp before kept-idx &&
+
+ # Get object list from the kept pack.
+ get_sorted_objects_from_pack before >old.objects &&
+
+ # Get object list from the one non-kept pack-file
+ comm -13 before after >new-pack &&
+ test_line_count = 1 new-pack &&
+ get_sorted_objects_from_pack new-pack >new.objects &&
+
+ # None of the objects in the new pack should
+ # exist within the kept pack.
+ comm -12 old.objects new.objects >shared.objects &&
+ test_must_be_empty shared.objects
+ )
+'
+
+test_expect_success '--write-midx removes stale pack-based bitmaps' '
+ rm -fr repo &&
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+ test_commit base &&
+ GIT_TEST_MULTI_PACK_INDEX=0 git repack -Ab &&
+
+ pack_bitmap=$(ls $objdir/pack/pack-*.bitmap) &&
+ test_path_is_file "$pack_bitmap" &&
+
+ test_commit tip &&
+ GIT_TEST_MULTI_PACK_INDEX=0 git repack -bm &&
+
+ test_path_is_file $midx &&
+ test_path_is_file $midx-$(midx_checksum $objdir).bitmap &&
+ test_path_is_missing $pack_bitmap
+ )
+'
+
+test_expect_success '--write-midx with --pack-kept-objects' '
+ git init repo &&
+ test_when_finished "rm -fr repo" &&
+ (
+ cd repo &&
+
+ test_commit one &&
+ test_commit two &&
+
+ one="$(echo "one" | git pack-objects --revs $objdir/pack/pack)" &&
+ two="$(echo "one..two" | git pack-objects --revs $objdir/pack/pack)" &&
+
+ keep="$objdir/pack/pack-$one.keep" &&
+ touch "$keep" &&
+
+ git repack --write-midx --write-bitmap-index --geometric=2 -d \
+ --pack-kept-objects &&
+
+ test_path_is_file $keep &&
+ test_path_is_file $midx &&
+ test_path_is_file $midx-$(midx_checksum $objdir).bitmap
+ )
+'
+
+test_expect_success TTY '--quiet disables progress' '
+ test_terminal env GIT_PROGRESS_DELAY=0 \
+ git -C midx repack -ad --quiet --write-midx 2>stderr &&
+ test_must_be_empty stderr
+'
+
+test_expect_success 'clean up .tmp-* packs on error' '
+ test_must_fail ok=sigpipe git \
+ -c repack.cruftwindow=bogus \
+ repack -ad --cruft &&
+ find $objdir/pack -name '.tmp-*' >tmpfiles &&
+ test_must_be_empty tmpfiles
+'
+
+test_expect_success 'repack -ad cleans up old .tmp-* packs' '
+ git rev-parse HEAD >input &&
+ git pack-objects $objdir/pack/.tmp-1234 <input &&
+ git repack -ad &&
+ find $objdir/pack -name '.tmp-*' >tmpfiles &&
+ test_must_be_empty tmpfiles
+'
+
+test_expect_success 'setup for update-server-info' '
+ git init update-server-info &&
+ test_commit -C update-server-info message
+'
+
+test_server_info_present () {
+ test_path_is_file update-server-info/.git/objects/info/packs &&
+ test_path_is_file update-server-info/.git/info/refs
+}
+
+test_server_info_missing () {
+ test_path_is_missing update-server-info/.git/objects/info/packs &&
+ test_path_is_missing update-server-info/.git/info/refs
+}
+
+test_server_info_cleanup () {
+ rm -f update-server-info/.git/objects/info/packs update-server-info/.git/info/refs &&
+ test_server_info_missing
+}
+
+test_expect_success 'updates server info by default' '
+ test_server_info_cleanup &&
+ git -C update-server-info repack &&
+ test_server_info_present
+'
+
+test_expect_success '-n skips updating server info' '
+ test_server_info_cleanup &&
+ git -C update-server-info repack -n &&
+ test_server_info_missing
+'
+
+test_expect_success 'repack.updateServerInfo=true updates server info' '
+ test_server_info_cleanup &&
+ git -C update-server-info -c repack.updateServerInfo=true repack &&
+ test_server_info_present
+'
+
+test_expect_success 'repack.updateServerInfo=false skips updating server info' '
+ test_server_info_cleanup &&
+ git -C update-server-info -c repack.updateServerInfo=false repack &&
+ test_server_info_missing
+'
+
+test_expect_success '-n overrides repack.updateServerInfo=true' '
+ test_server_info_cleanup &&
+ git -C update-server-info -c repack.updateServerInfo=true repack -n &&
+ test_server_info_missing
+'
+
+test_done
diff --git a/t/t7701-repack-unpack-unreachable.sh b/t/t7701-repack-unpack-unreachable.sh
new file mode 100755
index 0000000..fe6c3e7
--- /dev/null
+++ b/t/t7701-repack-unpack-unreachable.sh
@@ -0,0 +1,199 @@
+#!/bin/sh
+
+test_description='git repack works correctly'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+fsha1=
+csha1=
+tsha1=
+
+test_expect_success '-A with -d option leaves unreachable objects unpacked' '
+ echo content > file1 &&
+ git add . &&
+ test_tick &&
+ git commit -m initial_commit &&
+ # create a transient branch with unique content
+ git checkout -b transient_branch &&
+ echo more content >> file1 &&
+ # record the objects created in the database for file, commit, tree
+ fsha1=$(git hash-object file1) &&
+ test_tick &&
+ git commit -a -m more_content &&
+ csha1=$(git rev-parse HEAD^{commit}) &&
+ tsha1=$(git rev-parse HEAD^{tree}) &&
+ git checkout main &&
+ echo even more content >> file1 &&
+ test_tick &&
+ git commit -a -m even_more_content &&
+ # delete the transient branch
+ git branch -D transient_branch &&
+ # pack the repo
+ git repack -A -d -l &&
+ # verify objects are packed in repository
+ test 3 = $(git verify-pack -v -- .git/objects/pack/*.idx |
+ grep -E "^($fsha1|$csha1|$tsha1) " |
+ sort | uniq | wc -l) &&
+ git show $fsha1 &&
+ git show $csha1 &&
+ git show $tsha1 &&
+ # now expire the reflog, while keeping reachable ones but expiring
+ # unreachables immediately
+ test_tick &&
+ sometimeago=$(( $test_tick - 10000 )) &&
+ git reflog expire --expire=$sometimeago --expire-unreachable=$test_tick --all &&
+ # and repack
+ git repack -A -d -l &&
+ # verify objects are retained unpacked
+ test 0 = $(git verify-pack -v -- .git/objects/pack/*.idx |
+ grep -E "^($fsha1|$csha1|$tsha1) " |
+ sort | uniq | wc -l) &&
+ git show $fsha1 &&
+ git show $csha1 &&
+ git show $tsha1
+'
+
+compare_mtimes ()
+{
+ read tref &&
+ while read t; do
+ test "$tref" = "$t" || return 1
+ done
+}
+
+test_expect_success '-A without -d option leaves unreachable objects packed' '
+ fsha1path=$(echo "$fsha1" | sed -e "s|\(..\)|\1/|") &&
+ fsha1path=".git/objects/$fsha1path" &&
+ csha1path=$(echo "$csha1" | sed -e "s|\(..\)|\1/|") &&
+ csha1path=".git/objects/$csha1path" &&
+ tsha1path=$(echo "$tsha1" | sed -e "s|\(..\)|\1/|") &&
+ tsha1path=".git/objects/$tsha1path" &&
+ git branch transient_branch $csha1 &&
+ git repack -a -d -l &&
+ test ! -f "$fsha1path" &&
+ test ! -f "$csha1path" &&
+ test ! -f "$tsha1path" &&
+ test 1 = $(ls -1 .git/objects/pack/pack-*.pack | wc -l) &&
+ packfile=$(ls .git/objects/pack/pack-*.pack) &&
+ git branch -D transient_branch &&
+ test_tick &&
+ git repack -A -l &&
+ test ! -f "$fsha1path" &&
+ test ! -f "$csha1path" &&
+ test ! -f "$tsha1path" &&
+ git show $fsha1 &&
+ git show $csha1 &&
+ git show $tsha1
+'
+
+test_expect_success 'unpacked objects receive timestamp of pack file' '
+ tmppack=".git/objects/pack/tmp_pack" &&
+ ln "$packfile" "$tmppack" &&
+ git repack -A -l -d &&
+ test-tool chmtime --get "$tmppack" "$fsha1path" "$csha1path" "$tsha1path" \
+ > mtimes &&
+ compare_mtimes < mtimes
+'
+
+test_expect_success 'do not bother loosening old objects' '
+ obj1=$(echo one | git hash-object -w --stdin) &&
+ obj2=$(echo two | git hash-object -w --stdin) &&
+ pack1=$(echo $obj1 | git pack-objects .git/objects/pack/pack) &&
+ pack2=$(echo $obj2 | git pack-objects .git/objects/pack/pack) &&
+ git prune-packed &&
+ git cat-file -p $obj1 &&
+ git cat-file -p $obj2 &&
+ test-tool chmtime =-86400 .git/objects/pack/pack-$pack2.pack &&
+ git repack -A -d --unpack-unreachable=1.hour.ago &&
+ git cat-file -p $obj1 &&
+ test_must_fail git cat-file -p $obj2
+'
+
+test_expect_success 'gc.recentObjectsHook' '
+ obj1=$(echo one | git hash-object -w --stdin) &&
+ obj2=$(echo two | git hash-object -w --stdin) &&
+ obj3=$(echo three | git hash-object -w --stdin) &&
+ pack1=$(echo $obj1 | git pack-objects .git/objects/pack/pack) &&
+ pack2=$(echo $obj2 | git pack-objects .git/objects/pack/pack) &&
+ pack3=$(echo $obj3 | git pack-objects .git/objects/pack/pack) &&
+ git prune-packed &&
+
+ git cat-file -p $obj1 &&
+ git cat-file -p $obj2 &&
+ git cat-file -p $obj3 &&
+
+ # make an unreachable annotated tag object to ensure we rescue objects
+ # which are reachable from non-pruned unreachable objects
+ obj2_tag="$(git mktag <<-EOF
+ object $obj2
+ type blob
+ tag obj2-tag
+ tagger T A Gger <tagger@example.com> 1234567890 -0000
+ EOF
+ )" &&
+
+ obj2_tag_pack="$(echo $obj2_tag | git pack-objects .git/objects/pack/pack)" &&
+ git prune-packed &&
+
+ write_script precious-objects <<-EOF &&
+ echo $obj2_tag
+ EOF
+ git config gc.recentObjectsHook ./precious-objects &&
+
+ test-tool chmtime =-86400 .git/objects/pack/pack-$pack2.pack &&
+ test-tool chmtime =-86400 .git/objects/pack/pack-$pack3.pack &&
+ test-tool chmtime =-86400 .git/objects/pack/pack-$obj2_tag_pack.pack &&
+ git repack -A -d --unpack-unreachable=1.hour.ago &&
+
+ git cat-file -p $obj1 &&
+ git cat-file -p $obj2 &&
+ git cat-file -p $obj2_tag &&
+ test_must_fail git cat-file -p $obj3
+'
+
+test_expect_success 'keep packed objects found only in index' '
+ echo my-unique-content >file &&
+ git add file &&
+ git commit -m "make it reachable" &&
+ git gc &&
+ git reset HEAD^ &&
+ git reflog expire --expire=now --all &&
+ git add file &&
+ test-tool chmtime =-86400 .git/objects/pack/* &&
+ git gc --prune=1.hour.ago &&
+ git cat-file blob :file
+'
+
+test_expect_success 'repack -k keeps unreachable packed objects' '
+ # create packed-but-unreachable object
+ sha1=$(echo unreachable-packed | git hash-object -w --stdin) &&
+ pack=$(echo $sha1 | git pack-objects .git/objects/pack/pack) &&
+ git prune-packed &&
+
+ # -k should keep it
+ git repack -adk &&
+ git cat-file -p $sha1 &&
+
+ # and double check that without -k it would have been removed
+ git repack -ad &&
+ test_must_fail git cat-file -p $sha1
+'
+
+test_expect_success 'repack -k packs unreachable loose objects' '
+ # create loose unreachable object
+ sha1=$(echo would-be-deleted-loose | git hash-object -w --stdin) &&
+ objpath=.git/objects/$(echo $sha1 | sed "s,..,&/,") &&
+ test_path_is_file $objpath &&
+
+ # and confirm that the loose object goes away, but we can
+ # still access it (ergo, it is packed)
+ git repack -adk &&
+ test_path_is_missing $objpath &&
+ git cat-file -p $sha1
+'
+
+test_done
diff --git a/t/t7702-repack-cyclic-alternate.sh b/t/t7702-repack-cyclic-alternate.sh
new file mode 100755
index 0000000..f3cdb98
--- /dev/null
+++ b/t/t7702-repack-cyclic-alternate.sh
@@ -0,0 +1,26 @@
+#!/bin/sh
+#
+# Copyright (c) 2014 Ephrim Khong
+#
+
+test_description='repack involving cyclic alternate'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success setup '
+ GIT_OBJECT_DIRECTORY=.git//../.git/objects &&
+ export GIT_OBJECT_DIRECTORY &&
+ touch a &&
+ git add a &&
+ git commit -m 1 &&
+ git repack -adl &&
+ echo "$(pwd)"/.git/objects/../objects >.git/objects/info/alternates
+'
+
+test_expect_success 're-packing repository with itsself as alternate' '
+ git repack -adl &&
+ git fsck
+'
+
+test_done
diff --git a/t/t7703-repack-geometric.sh b/t/t7703-repack-geometric.sh
new file mode 100755
index 0000000..9fc1626
--- /dev/null
+++ b/t/t7703-repack-geometric.sh
@@ -0,0 +1,448 @@
+#!/bin/sh
+
+test_description='git repack --geometric works correctly'
+
+. ./test-lib.sh
+
+GIT_TEST_MULTI_PACK_INDEX=0
+
+objdir=.git/objects
+packdir=$objdir/pack
+midx=$objdir/pack/multi-pack-index
+
+packed_objects () {
+ git show-index <"$1" >tmp-object-list &&
+ cut -d' ' -f2 tmp-object-list | sort &&
+ rm tmp-object-list
+ }
+
+test_expect_success '--geometric with no packs' '
+ git init geometric &&
+ test_when_finished "rm -fr geometric" &&
+ (
+ cd geometric &&
+
+ git repack --write-midx --geometric 2 >out &&
+ test_grep "Nothing new to pack" out
+ )
+'
+
+test_expect_success '--geometric with one pack' '
+ git init geometric &&
+ test_when_finished "rm -fr geometric" &&
+ (
+ cd geometric &&
+
+ test_commit "base" &&
+ git repack -d &&
+
+ git repack --geometric 2 >out &&
+
+ test_grep "Nothing new to pack" out
+ )
+'
+
+test_expect_success '--geometric with an intact progression' '
+ git init geometric &&
+ test_when_finished "rm -fr geometric" &&
+ (
+ cd geometric &&
+
+ # These packs already form a geometric progression.
+ test_commit_bulk --start=1 1 && # 3 objects
+ test_commit_bulk --start=2 2 && # 6 objects
+ test_commit_bulk --start=4 4 && # 12 objects
+
+ find $objdir/pack -name "*.pack" | sort >expect &&
+ git repack --geometric 2 -d &&
+ find $objdir/pack -name "*.pack" | sort >actual &&
+
+ test_cmp expect actual
+ )
+'
+
+test_expect_success '--geometric with loose objects' '
+ git init geometric &&
+ test_when_finished "rm -fr geometric" &&
+ (
+ cd geometric &&
+
+ # These packs already form a geometric progression.
+ test_commit_bulk --start=1 1 && # 3 objects
+ test_commit_bulk --start=2 2 && # 6 objects
+ # The loose objects are packed together, breaking the
+ # progression.
+ test_commit loose && # 3 objects
+
+ find $objdir/pack -name "*.pack" | sort >before &&
+ git repack --geometric 2 -d &&
+ find $objdir/pack -name "*.pack" | sort >after &&
+
+ comm -13 before after >new &&
+ comm -23 before after >removed &&
+
+ test_line_count = 1 new &&
+ test_must_be_empty removed &&
+
+ git repack --geometric 2 -d &&
+ find $objdir/pack -name "*.pack" | sort >after &&
+
+ # The progression (3, 3, 6) is combined into one new pack.
+ test_line_count = 1 after
+ )
+'
+
+test_expect_success '--geometric with small-pack rollup' '
+ git init geometric &&
+ test_when_finished "rm -fr geometric" &&
+ (
+ cd geometric &&
+
+ test_commit_bulk --start=1 1 && # 3 objects
+ test_commit_bulk --start=2 1 && # 3 objects
+ find $objdir/pack -name "*.pack" | sort >small &&
+ test_commit_bulk --start=3 4 && # 12 objects
+ test_commit_bulk --start=7 8 && # 24 objects
+ find $objdir/pack -name "*.pack" | sort >before &&
+
+ git repack --geometric 2 -d &&
+
+ # Three packs in total; two of the existing large ones, and one
+ # new one.
+ find $objdir/pack -name "*.pack" | sort >after &&
+ test_line_count = 3 after &&
+ comm -3 small before | tr -d "\t" >large &&
+ grep -qFf large after
+ )
+'
+
+test_expect_success '--geometric with small- and large-pack rollup' '
+ git init geometric &&
+ test_when_finished "rm -fr geometric" &&
+ (
+ cd geometric &&
+
+ # size(small1) + size(small2) > size(medium) / 2
+ test_commit_bulk --start=1 1 && # 3 objects
+ test_commit_bulk --start=2 1 && # 3 objects
+ test_commit_bulk --start=2 3 && # 7 objects
+ test_commit_bulk --start=6 9 && # 27 objects &&
+
+ find $objdir/pack -name "*.pack" | sort >before &&
+
+ git repack --geometric 2 -d &&
+
+ find $objdir/pack -name "*.pack" | sort >after &&
+ comm -12 before after >untouched &&
+
+ # Two packs in total; the largest pack from before running "git
+ # repack", and one new one.
+ test_line_count = 1 untouched &&
+ test_line_count = 2 after
+ )
+'
+
+test_expect_success '--geometric ignores kept packs' '
+ git init geometric &&
+ test_when_finished "rm -fr geometric" &&
+ (
+ cd geometric &&
+
+ test_commit kept && # 3 objects
+ test_commit pack && # 3 objects
+
+ KEPT=$(git pack-objects --revs $objdir/pack/pack <<-EOF
+ refs/tags/kept
+ EOF
+ ) &&
+ PACK=$(git pack-objects --revs $objdir/pack/pack <<-EOF
+ refs/tags/pack
+ ^refs/tags/kept
+ EOF
+ ) &&
+
+ # neither pack contains more than twice the number of objects in
+ # the other, so they should be combined. but, marking one as
+ # .kept on disk will "freeze" it, so the pack structure should
+ # remain unchanged.
+ touch $objdir/pack/pack-$KEPT.keep &&
+
+ find $objdir/pack -name "*.pack" | sort >before &&
+ git repack --geometric 2 -d &&
+ find $objdir/pack -name "*.pack" | sort >after &&
+
+ # both packs should still exist
+ test_path_is_file $objdir/pack/pack-$KEPT.pack &&
+ test_path_is_file $objdir/pack/pack-$PACK.pack &&
+
+ # and no new packs should be created
+ test_cmp before after &&
+
+ # Passing --pack-kept-objects causes packs with a .keep file to
+ # be repacked, too.
+ git repack --geometric 2 -d --pack-kept-objects &&
+
+ # After repacking, two packs remain: one new one (containing the
+ # objects in both the .keep and non-kept pack), and the .keep
+ # pack (since `--pack-kept-objects -d` does not actually delete
+ # the kept pack).
+ find $objdir/pack -name "*.pack" >after &&
+ test_line_count = 2 after
+ )
+'
+
+test_expect_success '--geometric ignores --keep-pack packs' '
+ git init geometric &&
+ test_when_finished "rm -fr geometric" &&
+ (
+ cd geometric &&
+
+ # Create two equal-sized packs
+ test_commit kept && # 3 objects
+ git repack -d &&
+ test_commit pack && # 3 objects
+ git repack -d &&
+
+ find $objdir/pack -type f -name "*.pack" | sort >packs.before &&
+ git repack --geometric 2 -dm \
+ --keep-pack="$(basename "$(head -n 1 packs.before)")" >out &&
+ find $objdir/pack -type f -name "*.pack" | sort >packs.after &&
+
+ # Packs should not have changed (only one non-kept pack, no
+ # loose objects), but $midx should now exist.
+ grep "Nothing new to pack" out &&
+ test_path_is_file $midx &&
+
+ test_cmp packs.before packs.after &&
+
+ git fsck
+ )
+'
+
+test_expect_success '--geometric chooses largest MIDX preferred pack' '
+ git init geometric &&
+ test_when_finished "rm -fr geometric" &&
+ (
+ cd geometric &&
+
+ # These packs already form a geometric progression.
+ test_commit_bulk --start=1 1 && # 3 objects
+ test_commit_bulk --start=2 2 && # 6 objects
+ ls $objdir/pack/pack-*.idx >before &&
+ test_commit_bulk --start=4 4 && # 12 objects
+ ls $objdir/pack/pack-*.idx >after &&
+
+ git repack --geometric 2 -dbm &&
+
+ comm -3 before after | xargs -n 1 basename >expect &&
+ test-tool read-midx --preferred-pack $objdir >actual &&
+
+ test_cmp expect actual
+ )
+'
+
+test_expect_success '--geometric with pack.packSizeLimit' '
+ git init pack-rewrite &&
+ test_when_finished "rm -fr pack-rewrite" &&
+ (
+ cd pack-rewrite &&
+
+ test-tool genrandom foo 1048576 >foo &&
+ test-tool genrandom bar 1048576 >bar &&
+
+ git add foo bar &&
+ test_tick &&
+ git commit -m base &&
+
+ git rev-parse HEAD:foo HEAD:bar >p1.objects &&
+ git rev-parse HEAD HEAD^{tree} >p2.objects &&
+
+ # These two packs each contain two objects, so the following
+ # `--geometric` repack will try to combine them.
+ p1="$(git pack-objects $packdir/pack <p1.objects)" &&
+ p2="$(git pack-objects $packdir/pack <p2.objects)" &&
+
+ # Remove any loose objects in packs, since we do not want extra
+ # copies around (which would mask over potential object
+ # corruption issues).
+ git prune-packed &&
+
+ # Both p1 and p2 will be rolled up, but pack-objects will write
+ # three packs:
+ #
+ # - one containing object "foo",
+ # - another containing object "bar",
+ # - a final pack containing the commit and tree objects
+ # (identical to p2 above)
+ git repack --geometric 2 -d --max-pack-size=1048576 &&
+
+ # Ensure `repack` can detect that the third pack it wrote
+ # (containing just the tree and commit objects) was identical to
+ # one that was below the geometric split, so that we can save it
+ # from deletion.
+ #
+ # If `repack` fails to do that, we will incorrectly delete p2,
+ # causing object corruption.
+ git fsck
+ )
+'
+
+test_expect_success '--geometric --write-midx with packfiles in main and alternate ODB' '
+ test_when_finished "rm -fr shared member" &&
+
+ # Create a shared repository that will serve as the alternate object
+ # database for the member linked to it. It has got some objects on its
+ # own that are packed into a single packfile.
+ git init shared &&
+ test_commit -C shared common-object &&
+ git -C shared repack -ad &&
+
+ # We create member so that its alternates file points to the shared
+ # repository. We then create a commit in it so that git-repack(1) has
+ # something to repack.
+ # of the shared object database.
+ git clone --shared shared member &&
+ test_commit -C member unique-object &&
+ git -C member repack --geometric=2 --write-midx 2>err &&
+ test_must_be_empty err &&
+
+ # We should see that a new packfile was generated.
+ find shared/.git/objects/pack -type f -name "*.pack" >packs &&
+ test_line_count = 1 packs &&
+
+ # We should also see a multi-pack-index. This multi-pack-index should
+ # never refer to any packfiles in the alternate object database.
+ test_path_is_file member/.git/objects/pack/multi-pack-index &&
+ test-tool read-midx member/.git/objects >packs.midx &&
+ grep "^pack-.*\.idx$" packs.midx | sort >actual &&
+ basename member/.git/objects/pack/pack-*.idx >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success '--geometric --with-midx with no local objects' '
+ test_when_finished "rm -fr shared member" &&
+
+ # Create a repository with a single packfile that acts as alternate
+ # object database.
+ git init shared &&
+ test_commit -C shared "shared-objects" &&
+ git -C shared repack -ad &&
+
+ # Create a second repository linked to the first one and perform a
+ # geometric repack on it.
+ git clone --shared shared member &&
+ git -C member repack --geometric 2 --write-midx 2>err &&
+ test_must_be_empty err &&
+
+ # Assert that we wrote neither a new packfile nor a multi-pack-index.
+ # We should not have a packfile because the single packfile in the
+ # alternate object database does not invalidate the geometric sequence.
+ # And we should not have a multi-pack-index because these only index
+ # local packfiles, and there are none.
+ test_dir_is_empty member/$packdir
+'
+
+test_expect_success '--geometric with same pack in main and alternate ODB' '
+ test_when_finished "rm -fr shared member" &&
+
+ # Create a repository with a single packfile that acts as alternate
+ # object database.
+ git init shared &&
+ test_commit -C shared "shared-objects" &&
+ git -C shared repack -ad &&
+
+ # We create the member repository as an exact copy so that it has the
+ # same packfile.
+ cp -r shared member &&
+ test-tool path-utils real_path shared/.git/objects >member/.git/objects/info/alternates &&
+ find shared/.git/objects -type f >expected-files &&
+
+ # Verify that we can repack objects as expected without observing any
+ # error. Having the same packfile in both ODBs used to cause an error
+ # in git-pack-objects(1).
+ git -C member repack --geometric 2 2>err &&
+ test_must_be_empty err &&
+ # Nothing should have changed.
+ find shared/.git/objects -type f >actual-files &&
+ test_cmp expected-files actual-files
+'
+
+test_expect_success '--geometric -l with non-intact geometric sequence across ODBs' '
+ test_when_finished "rm -fr shared member" &&
+
+ git init shared &&
+ test_commit_bulk -C shared --start=1 1 &&
+
+ git clone --shared shared member &&
+ test_commit_bulk -C member --start=2 1 &&
+
+ # Verify that our assumptions actually hold: both generated packfiles
+ # should have three objects and should be non-equal.
+ packed_objects shared/.git/objects/pack/pack-*.idx >shared-objects &&
+ packed_objects member/.git/objects/pack/pack-*.idx >member-objects &&
+ test_line_count = 3 shared-objects &&
+ test_line_count = 3 member-objects &&
+ ! test_cmp shared-objects member-objects &&
+
+ # Perform the geometric repack. With `-l`, we should only see the local
+ # packfile and thus arrive at the conclusion that the geometric
+ # sequence is intact. We thus expect no changes.
+ #
+ # Note that we are tweaking mtimes of the packfiles so that we can
+ # verify they did not change. This is done in order to detect the case
+ # where we do repack objects, but the resulting packfile is the same.
+ test-tool chmtime --verbose =0 member/.git/objects/pack/* >expected-member-packs &&
+ git -C member repack --geometric=2 -l -d &&
+ test-tool chmtime --verbose member/.git/objects/pack/* >actual-member-packs &&
+ test_cmp expected-member-packs actual-member-packs &&
+
+ {
+ packed_objects shared/.git/objects/pack/pack-*.idx &&
+ packed_objects member/.git/objects/pack/pack-*.idx
+ } | sort >expected-objects &&
+
+ # On the other hand, when doing a non-local geometric repack we should
+ # see both packfiles and thus repack them. We expect that the shared
+ # object database was not changed.
+ test-tool chmtime --verbose =0 shared/.git/objects/pack/* >expected-shared-packs &&
+ git -C member repack --geometric=2 -d &&
+ test-tool chmtime --verbose shared/.git/objects/pack/* >actual-shared-packs &&
+ test_cmp expected-shared-packs actual-shared-packs &&
+
+ # Furthermore, we expect that the member repository now has a single
+ # packfile that contains the combined shared and non-shared objects.
+ ls member/.git/objects/pack/pack-*.idx >actual &&
+ test_line_count = 1 actual &&
+ packed_objects member/.git/objects/pack/pack-*.idx >actual-objects &&
+ test_line_count = 6 actual-objects &&
+ test_cmp expected-objects actual-objects
+'
+
+test_expect_success '--geometric -l disables writing bitmaps with non-local packfiles' '
+ test_when_finished "rm -fr shared member" &&
+
+ git init shared &&
+ test_commit_bulk -C shared --start=1 1 &&
+
+ git clone --shared shared member &&
+ test_commit_bulk -C member --start=2 1 &&
+
+ # When performing a geometric repack with `-l` while connected to an
+ # alternate object database that has a packfile we do not have full
+ # coverage of objects. As a result, we expect that writing the bitmap
+ # will be disabled.
+ git -C member repack -l --geometric=2 --write-midx --write-bitmap-index 2>err &&
+ cat >expect <<-EOF &&
+ warning: disabling bitmap writing, as some objects are not being packed
+ EOF
+ test_cmp expect err &&
+ test_path_is_missing member/.git/objects/pack/multi-pack-index-*.bitmap &&
+
+ # On the other hand, when we repack without `-l`, we should see that
+ # the bitmap gets created.
+ git -C member repack --geometric=2 --write-midx --write-bitmap-index 2>err &&
+ test_must_be_empty err &&
+ test_path_is_file member/.git/objects/pack/multi-pack-index-*.bitmap
+'
+
+test_done
diff --git a/t/t7704-repack-cruft.sh b/t/t7704-repack-cruft.sh
new file mode 100755
index 0000000..be3735d
--- /dev/null
+++ b/t/t7704-repack-cruft.sh
@@ -0,0 +1,414 @@
+#!/bin/sh
+
+test_description='git repack works correctly'
+
+. ./test-lib.sh
+
+objdir=.git/objects
+packdir=$objdir/pack
+
+test_expect_success '--expire-to stores pruned objects (now)' '
+ git init expire-to-now &&
+ (
+ cd expire-to-now &&
+
+ git branch -M main &&
+
+ test_commit base &&
+
+ git checkout -b cruft &&
+ test_commit --no-tag cruft &&
+
+ git rev-list --objects --no-object-names main..cruft >moved.raw &&
+ sort moved.raw >moved.want &&
+
+ git rev-list --all --objects --no-object-names >expect.raw &&
+ sort expect.raw >expect &&
+
+ git checkout main &&
+ git branch -D cruft &&
+ git reflog expire --all --expire=all &&
+
+ git init --bare expired.git &&
+ git repack -d \
+ --cruft --cruft-expiration="now" \
+ --expire-to="expired.git/objects/pack/pack" &&
+
+ expired="$(ls expired.git/objects/pack/pack-*.idx)" &&
+ test_path_is_file "${expired%.idx}.mtimes" &&
+
+ # Since the `--cruft-expiration` is "now", the effective
+ # behavior is to move _all_ unreachable objects out to
+ # the location in `--expire-to`.
+ git show-index <$expired >expired.raw &&
+ cut -d" " -f2 expired.raw | sort >expired.objects &&
+ git rev-list --all --objects --no-object-names \
+ >remaining.objects &&
+
+ # ...in other words, the combined contents of this
+ # repository and expired.git should be the same as the
+ # set of objects we started with.
+ cat expired.objects remaining.objects | sort >actual &&
+ test_cmp expect actual &&
+
+ # The "moved" objects (i.e., those in expired.git)
+ # should be the same as the cruft objects which were
+ # expired in the previous step.
+ test_cmp moved.want expired.objects
+ )
+'
+
+test_expect_success '--expire-to stores pruned objects (5.minutes.ago)' '
+ git init expire-to-5.minutes.ago &&
+ (
+ cd expire-to-5.minutes.ago &&
+
+ git branch -M main &&
+
+ test_commit base &&
+
+ # Create two classes of unreachable objects, one which
+ # is older than 5 minutes (stale), and another which is
+ # newer (recent).
+ for kind in stale recent
+ do
+ git checkout -b $kind main &&
+ test_commit --no-tag $kind || return 1
+ done &&
+
+ git rev-list --objects --no-object-names main..stale >in &&
+ stale="$(git pack-objects $objdir/pack/pack <in)" &&
+ mtime="$(test-tool chmtime --get =-600 $objdir/pack/pack-$stale.pack)" &&
+
+ # expect holds the set of objects we expect to find in
+ # this repository after repacking
+ git rev-list --objects --no-object-names recent >expect.raw &&
+ sort expect.raw >expect &&
+
+ # moved.want holds the set of objects we expect to find
+ # in expired.git
+ git rev-list --objects --no-object-names main..stale >out &&
+ sort out >moved.want &&
+
+ git checkout main &&
+ git branch -D stale recent &&
+ git reflog expire --all --expire=all &&
+ git prune-packed &&
+
+ git init --bare expired.git &&
+ git repack -d \
+ --cruft --cruft-expiration=5.minutes.ago \
+ --expire-to="expired.git/objects/pack/pack" &&
+
+ # Some of the remaining objects in this repository are
+ # unreachable, so use `cat-file --batch-all-objects`
+ # instead of `rev-list` to get their names
+ git cat-file --batch-all-objects --batch-check="%(objectname)" \
+ >remaining.objects &&
+ sort remaining.objects >actual &&
+ test_cmp expect actual &&
+
+ (
+ cd expired.git &&
+
+ expired="$(ls objects/pack/pack-*.mtimes)" &&
+ test-tool pack-mtimes $(basename $expired) >out &&
+ cut -d" " -f1 out | sort >../moved.got &&
+
+ # Ensure that there are as many objects with the
+ # expected mtime as were moved to expired.git.
+ #
+ # In other words, ensure that the recorded
+ # mtimes of any moved objects was written
+ # correctly.
+ grep " $mtime$" out >matching &&
+ test_line_count = $(wc -l <../moved.want) matching
+ ) &&
+ test_cmp moved.want moved.got
+ )
+'
+
+generate_random_blob() {
+ test-tool genrandom "$@" >blob &&
+ git hash-object -w -t blob blob &&
+ rm blob
+}
+
+pack_random_blob () {
+ generate_random_blob "$@" &&
+ git repack -d -q >/dev/null
+}
+
+generate_cruft_pack () {
+ pack_random_blob "$@" >/dev/null &&
+
+ ls $packdir/pack-*.pack | xargs -n 1 basename >in &&
+ pack="$(git pack-objects --cruft $packdir/pack <in)" &&
+ git prune-packed &&
+
+ echo "$packdir/pack-$pack.mtimes"
+}
+
+test_expect_success '--max-cruft-size creates new packs when above threshold' '
+ git init max-cruft-size-large &&
+ (
+ cd max-cruft-size-large &&
+ test_commit base &&
+
+ foo="$(pack_random_blob foo $((1*1024*1024)))" &&
+ git repack --cruft -d &&
+ cruft_foo="$(ls $packdir/pack-*.mtimes)" &&
+
+ bar="$(pack_random_blob bar $((1*1024*1024)))" &&
+ git repack --cruft -d --max-cruft-size=1M &&
+ cruft_bar="$(ls $packdir/pack-*.mtimes | grep -v $cruft_foo)" &&
+
+ test-tool pack-mtimes $(basename "$cruft_foo") >foo.objects &&
+ test-tool pack-mtimes $(basename "$cruft_bar") >bar.objects &&
+
+ grep "^$foo" foo.objects &&
+ test_line_count = 1 foo.objects &&
+ grep "^$bar" bar.objects &&
+ test_line_count = 1 bar.objects
+ )
+'
+
+test_expect_success '--max-cruft-size combines existing packs when below threshold' '
+ git init max-cruft-size-small &&
+ (
+ cd max-cruft-size-small &&
+ test_commit base &&
+
+ foo="$(pack_random_blob foo $((1*1024*1024)))" &&
+ git repack --cruft -d &&
+
+ bar="$(pack_random_blob bar $((1*1024*1024)))" &&
+ git repack --cruft -d --max-cruft-size=10M &&
+
+ cruft=$(ls $packdir/pack-*.mtimes) &&
+ test-tool pack-mtimes $(basename "$cruft") >cruft.objects &&
+
+ grep "^$foo" cruft.objects &&
+ grep "^$bar" cruft.objects &&
+ test_line_count = 2 cruft.objects
+ )
+'
+
+test_expect_success '--max-cruft-size combines smaller packs first' '
+ git init max-cruft-size-consume-small &&
+ (
+ cd max-cruft-size-consume-small &&
+
+ test_commit base &&
+ git repack -ad &&
+
+ cruft_foo="$(generate_cruft_pack foo 524288)" && # 0.5 MiB
+ cruft_bar="$(generate_cruft_pack bar 524288)" && # 0.5 MiB
+ cruft_baz="$(generate_cruft_pack baz 1048576)" && # 1.0 MiB
+ cruft_quux="$(generate_cruft_pack quux 1572864)" && # 1.5 MiB
+
+ test-tool pack-mtimes "$(basename $cruft_foo)" >expect.raw &&
+ test-tool pack-mtimes "$(basename $cruft_bar)" >>expect.raw &&
+ sort expect.raw >expect.objects &&
+
+ # repacking with `--max-cruft-size=2M` should combine
+ # both 0.5 MiB packs together, instead of, say, one of
+ # the 0.5 MiB packs with the 1.0 MiB pack
+ ls $packdir/pack-*.mtimes | sort >cruft.before &&
+ git repack -d --cruft --max-cruft-size=2M &&
+ ls $packdir/pack-*.mtimes | sort >cruft.after &&
+
+ comm -13 cruft.before cruft.after >cruft.new &&
+ comm -23 cruft.before cruft.after >cruft.removed &&
+
+ test_line_count = 1 cruft.new &&
+ test_line_count = 2 cruft.removed &&
+
+ # the two smaller packs should be rolled up first
+ printf "%s\n" $cruft_foo $cruft_bar | sort >expect.removed &&
+ test_cmp expect.removed cruft.removed &&
+
+ # ...and contain the set of objects rolled up
+ test-tool pack-mtimes "$(basename $(cat cruft.new))" >actual.raw &&
+ sort actual.raw >actual.objects &&
+
+ test_cmp expect.objects actual.objects
+ )
+'
+
+test_expect_success 'setup --max-cruft-size with freshened objects' '
+ git init max-cruft-size-freshen &&
+ (
+ cd max-cruft-size-freshen &&
+
+ test_commit base &&
+ git repack -ad &&
+
+ foo="$(generate_random_blob foo 64)" &&
+ test-tool chmtime --get -10000 \
+ "$objdir/$(test_oid_to_path "$foo")" >foo.mtime &&
+
+ git repack --cruft -d &&
+
+ cruft="$(ls $packdir/pack-*.mtimes)" &&
+ test-tool pack-mtimes "$(basename $cruft)" >actual &&
+ echo "$foo $(cat foo.mtime)" >expect &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success '--max-cruft-size with freshened objects (loose)' '
+ (
+ cd max-cruft-size-freshen &&
+
+ # regenerate the object, setting its mtime to be more recent
+ foo="$(generate_random_blob foo 64)" &&
+ test-tool chmtime --get -100 \
+ "$objdir/$(test_oid_to_path "$foo")" >foo.mtime &&
+
+ git repack --cruft -d &&
+
+ cruft="$(ls $packdir/pack-*.mtimes)" &&
+ test-tool pack-mtimes "$(basename $cruft)" >actual &&
+ echo "$foo $(cat foo.mtime)" >expect &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success '--max-cruft-size with freshened objects (packed)' '
+ (
+ cd max-cruft-size-freshen &&
+
+ # regenerate the object and store it in a packfile,
+ # setting its mtime to be more recent
+ #
+ # store it alongside another cruft object so that we
+ # do not create an identical copy of the existing
+ # cruft pack (which contains $foo).
+ foo="$(generate_random_blob foo 64)" &&
+ bar="$(generate_random_blob bar 64)" &&
+ foo_pack="$(printf "%s\n" $foo $bar | git pack-objects $packdir/pack)" &&
+ git prune-packed &&
+
+ test-tool chmtime --get -10 \
+ "$packdir/pack-$foo_pack.pack" >foo.mtime &&
+
+ git repack --cruft -d &&
+
+ cruft="$(ls $packdir/pack-*.mtimes)" &&
+ test-tool pack-mtimes "$(basename $cruft)" >actual &&
+ echo "$foo $(cat foo.mtime)" >expect.raw &&
+ echo "$bar $(cat foo.mtime)" >>expect.raw &&
+ sort expect.raw >expect &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success '--max-cruft-size with pruning' '
+ git init max-cruft-size-prune &&
+ (
+ cd max-cruft-size-prune &&
+
+ test_commit base &&
+ foo="$(generate_random_blob foo $((1024*1024)))" &&
+ bar="$(generate_random_blob bar $((1024*1024)))" &&
+ baz="$(generate_random_blob baz $((1024*1024)))" &&
+
+ test-tool chmtime -10000 "$objdir/$(test_oid_to_path "$foo")" &&
+
+ git repack -d --cruft --max-cruft-size=1M &&
+
+ # backdate the mtimes of all cruft packs to validate
+ # that they were rewritten as a result of pruning
+ ls $packdir/pack-*.mtimes | sort >cruft.before &&
+ for cruft in $(cat cruft.before)
+ do
+ mtime="$(test-tool chmtime --get -10000 "$cruft")" &&
+ echo $cruft $mtime >>mtimes || return 1
+ done &&
+
+ # repack (and prune) with a --max-cruft-size to ensure
+ # that we appropriately split the resulting set of packs
+ git repack -d --cruft --max-cruft-size=1M \
+ --cruft-expiration=10.seconds.ago &&
+ ls $packdir/pack-*.mtimes | sort >cruft.after &&
+
+ for cruft in $(cat cruft.after)
+ do
+ old_mtime="$(grep $cruft mtimes | cut -d" " -f2)" &&
+ new_mtime="$(test-tool chmtime --get $cruft)" &&
+ test $old_mtime -lt $new_mtime || return 1
+ done &&
+
+ test_line_count = 3 cruft.before &&
+ test_line_count = 2 cruft.after &&
+ test_must_fail git cat-file -e $foo &&
+ git cat-file -e $bar &&
+ git cat-file -e $baz
+ )
+'
+
+test_expect_success '--max-cruft-size ignores non-local packs' '
+ repo="max-cruft-size-non-local" &&
+ git init $repo &&
+ (
+ cd $repo &&
+ test_commit base &&
+ generate_random_blob foo 64 &&
+ git repack --cruft -d
+ ) &&
+
+ git clone --reference=$repo $repo $repo-alt &&
+ (
+ cd $repo-alt &&
+
+ test_commit other &&
+ generate_random_blob bar 64 &&
+
+ # ensure that we do not attempt to pick up packs from
+ # the non-alternated repository, which would result in a
+ # crash
+ git repack --cruft --max-cruft-size=1M -d
+ )
+'
+
+test_expect_success 'reachable packs are preferred over cruft ones' '
+ repo="cruft-preferred-packs" &&
+ git init "$repo" &&
+ (
+ cd "$repo" &&
+
+ # This test needs to exercise careful control over when a MIDX
+ # is and is not written. Unset the corresponding TEST variable
+ # accordingly.
+ sane_unset GIT_TEST_MULTI_PACK_INDEX &&
+
+ test_commit base &&
+ test_commit --no-tag cruft &&
+
+ non_cruft="$(echo base | git pack-objects --revs $packdir/pack)" &&
+ # Write a cruft pack which both (a) sorts ahead of the non-cruft
+ # pack in lexical order, and (b) has an older mtime to appease
+ # the MIDX preferred pack selection routine.
+ cruft="$(echo pack-$non_cruft.pack | git pack-objects --cruft $packdir/pack-A)" &&
+ test-tool chmtime -1000 $packdir/pack-A-$cruft.pack &&
+
+ test_commit other &&
+ git repack -d &&
+
+ git repack --geometric 2 -d --write-midx --write-bitmap-index &&
+
+ # After repacking, there are two packs left: one reachable one
+ # (which is the result of combining both of the existing two
+ # non-cruft packs), and one cruft pack.
+ find .git/objects/pack -type f -name "*.pack" >packs &&
+ test_line_count = 2 packs &&
+
+ # Make sure that the pack we just wrote is marked as preferred,
+ # not the cruft one.
+ pack="$(test-tool read-midx --preferred-pack $objdir)" &&
+ test_path_is_missing "$packdir/$(basename "$pack" ".idx").mtimes"
+ )
+'
+
+test_done
diff --git a/t/t7800-difftool.sh b/t/t7800-difftool.sh
new file mode 100755
index 0000000..6a36be1
--- /dev/null
+++ b/t/t7800-difftool.sh
@@ -0,0 +1,920 @@
+#!/bin/sh
+#
+# Copyright (c) 2009, 2010, 2012, 2013 David Aguilar
+#
+
+test_description='git-difftool
+
+Testing basic diff tool invocation
+'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+difftool_test_setup ()
+{
+ test_config diff.tool test-tool &&
+ test_config difftool.test-tool.cmd 'cat "$LOCAL"' &&
+ test_config difftool.bogus-tool.cmd false
+}
+
+prompt_given ()
+{
+ prompt="$1"
+ test "$prompt" = "Launch 'test-tool' [Y/n]? branch"
+}
+
+test_expect_success 'basic usage requires no repo' '
+ test_expect_code 129 git difftool -h >output &&
+ test_grep ^usage: output &&
+ # create a ceiling directory to prevent Git from finding a repo
+ mkdir -p not/repo &&
+ test_when_finished rm -r not &&
+ test_expect_code 129 \
+ env GIT_CEILING_DIRECTORIES="$(pwd)/not" \
+ git -C not/repo difftool -h >output &&
+ test_grep ^usage: output
+'
+
+# Create a file on main and change it on branch
+test_expect_success 'setup' '
+ echo main >file &&
+ git add file &&
+ git commit -m "added file" &&
+
+ git checkout -b branch main &&
+ echo branch >file &&
+ git commit -a -m "branch changed file" &&
+ git checkout main
+'
+
+# Configure a custom difftool.<tool>.cmd and use it
+test_expect_success 'custom commands' '
+ difftool_test_setup &&
+ test_config difftool.test-tool.cmd "cat \"\$REMOTE\"" &&
+ echo main >expect &&
+ git difftool --no-prompt branch >actual &&
+ test_cmp expect actual &&
+
+ test_config difftool.test-tool.cmd "cat \"\$LOCAL\"" &&
+ echo branch >expect &&
+ git difftool --no-prompt branch >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'custom tool commands override built-ins' '
+ test_config difftool.vimdiff.cmd "cat \"\$REMOTE\"" &&
+ echo main >expect &&
+ git difftool --tool vimdiff --no-prompt branch >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'difftool ignores bad --tool values' '
+ : >expect &&
+ test_must_fail \
+ git difftool --no-prompt --tool=bad-tool branch >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'difftool forwards arguments to diff' '
+ difftool_test_setup &&
+ >for-diff &&
+ git add for-diff &&
+ echo changes>for-diff &&
+ git add for-diff &&
+ : >expect &&
+ git difftool --cached --no-prompt -- for-diff >actual &&
+ test_cmp expect actual &&
+ git reset -- for-diff &&
+ rm for-diff
+'
+
+test_expect_success 'difftool ignores exit code' '
+ test_config difftool.error.cmd false &&
+ git difftool -y -t error branch
+'
+
+test_expect_success 'difftool forwards exit code with --trust-exit-code' '
+ test_config difftool.error.cmd false &&
+ test_must_fail git difftool -y --trust-exit-code -t error branch
+'
+
+test_expect_success 'difftool forwards exit code with --trust-exit-code for built-ins' '
+ test_config difftool.vimdiff.path false &&
+ test_must_fail git difftool -y --trust-exit-code -t vimdiff branch
+'
+
+test_expect_success 'difftool honors difftool.trustExitCode = true' '
+ test_config difftool.error.cmd false &&
+ test_config difftool.trustExitCode true &&
+ test_must_fail git difftool -y -t error branch
+'
+
+test_expect_success 'difftool honors difftool.trustExitCode = false' '
+ test_config difftool.error.cmd false &&
+ test_config difftool.trustExitCode false &&
+ git difftool -y -t error branch
+'
+
+test_expect_success 'difftool ignores exit code with --no-trust-exit-code' '
+ test_config difftool.error.cmd false &&
+ test_config difftool.trustExitCode true &&
+ git difftool -y --no-trust-exit-code -t error branch
+'
+
+test_expect_success 'difftool stops on error with --trust-exit-code' '
+ test_when_finished "rm -f for-diff .git/fail-right-file" &&
+ test_when_finished "git reset -- for-diff" &&
+ write_script .git/fail-right-file <<-\EOF &&
+ echo failed
+ exit 1
+ EOF
+ >for-diff &&
+ git add for-diff &&
+ test_must_fail git difftool -y --trust-exit-code \
+ --extcmd .git/fail-right-file branch >actual &&
+ test_line_count = 1 actual
+'
+
+test_expect_success 'difftool honors exit status if command not found' '
+ test_config difftool.nonexistent.cmd i-dont-exist &&
+ test_config difftool.trustExitCode false &&
+ test_must_fail git difftool -y -t nonexistent branch
+'
+
+test_expect_success 'difftool honors --gui' '
+ difftool_test_setup &&
+ test_config merge.tool bogus-tool &&
+ test_config diff.tool bogus-tool &&
+ test_config diff.guitool test-tool &&
+
+ echo branch >expect &&
+ git difftool --no-prompt --gui branch >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'difftool with guiDefault auto selects gui tool when there is DISPLAY' '
+ difftool_test_setup &&
+ test_config merge.tool bogus-tool &&
+ test_config diff.tool bogus-tool &&
+ test_config diff.guitool test-tool &&
+ test_config difftool.guiDefault auto &&
+ DISPLAY=SOMETHING && export DISPLAY &&
+
+ echo branch >expect &&
+ git difftool --no-prompt branch >actual &&
+ test_cmp expect actual
+'
+test_expect_success 'difftool with guiDefault auto selects regular tool when no DISPLAY' '
+ difftool_test_setup &&
+ test_config diff.guitool bogus-tool &&
+ test_config diff.tool test-tool &&
+ test_config difftool.guiDefault Auto &&
+ DISPLAY= && export DISPLAY &&
+
+ echo branch >expect &&
+ git difftool --no-prompt branch >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'difftool with guiDefault true selects gui tool' '
+ difftool_test_setup &&
+ test_config diff.tool bogus-tool &&
+ test_config diff.guitool test-tool &&
+ test_config difftool.guiDefault true &&
+
+ DISPLAY= && export DISPLAY &&
+ echo branch >expect &&
+ git difftool --no-prompt branch >actual &&
+ test_cmp expect actual &&
+
+ DISPLAY=Something && export DISPLAY &&
+ echo branch >expect &&
+ git difftool --no-prompt branch >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'difftool --no-gui trumps config guiDefault' '
+ difftool_test_setup &&
+ test_config diff.guitool bogus-tool &&
+ test_config diff.tool test-tool &&
+ test_config difftool.guiDefault true &&
+
+ echo branch >expect &&
+ git difftool --no-prompt --no-gui branch >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'difftool --gui last setting wins' '
+ difftool_test_setup &&
+ : >expect &&
+ git difftool --no-prompt --gui --no-gui >actual &&
+ test_cmp expect actual &&
+
+ test_config merge.tool bogus-tool &&
+ test_config diff.tool bogus-tool &&
+ test_config diff.guitool test-tool &&
+ echo branch >expect &&
+ git difftool --no-prompt --no-gui --gui branch >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'difftool --gui works without configured diff.guitool' '
+ difftool_test_setup &&
+ echo branch >expect &&
+ git difftool --no-prompt --gui branch >actual &&
+ test_cmp expect actual
+'
+
+# Specify the diff tool using $GIT_DIFF_TOOL
+test_expect_success 'GIT_DIFF_TOOL variable' '
+ difftool_test_setup &&
+ git config --unset diff.tool &&
+ echo branch >expect &&
+ GIT_DIFF_TOOL=test-tool git difftool --no-prompt branch >actual &&
+ test_cmp expect actual
+'
+
+# Test the $GIT_*_TOOL variables and ensure
+# that $GIT_DIFF_TOOL always wins unless --tool is specified
+test_expect_success 'GIT_DIFF_TOOL overrides' '
+ difftool_test_setup &&
+ test_config diff.tool bogus-tool &&
+ test_config merge.tool bogus-tool &&
+
+ echo branch >expect &&
+ GIT_DIFF_TOOL=test-tool git difftool --no-prompt branch >actual &&
+ test_cmp expect actual &&
+
+ test_config diff.tool bogus-tool &&
+ test_config merge.tool bogus-tool &&
+ GIT_DIFF_TOOL=bogus-tool \
+ git difftool --no-prompt --tool=test-tool branch >actual &&
+ test_cmp expect actual
+'
+
+# Test that we don't have to pass --no-prompt to difftool
+# when $GIT_DIFFTOOL_NO_PROMPT is true
+test_expect_success 'GIT_DIFFTOOL_NO_PROMPT variable' '
+ difftool_test_setup &&
+ echo branch >expect &&
+ GIT_DIFFTOOL_NO_PROMPT=true git difftool branch >actual &&
+ test_cmp expect actual
+'
+
+# git-difftool supports the difftool.prompt variable.
+# Test that GIT_DIFFTOOL_PROMPT can override difftool.prompt = false
+test_expect_success 'GIT_DIFFTOOL_PROMPT variable' '
+ difftool_test_setup &&
+ test_config difftool.prompt false &&
+ echo >input &&
+ GIT_DIFFTOOL_PROMPT=true git difftool branch <input >output &&
+ prompt=$(tail -1 <output) &&
+ prompt_given "$prompt"
+'
+
+# Test that we don't have to pass --no-prompt when difftool.prompt is false
+test_expect_success 'difftool.prompt config variable is false' '
+ difftool_test_setup &&
+ test_config difftool.prompt false &&
+ echo branch >expect &&
+ git difftool branch >actual &&
+ test_cmp expect actual
+'
+
+# Test that we don't have to pass --no-prompt when mergetool.prompt is false
+test_expect_success 'difftool merge.prompt = false' '
+ difftool_test_setup &&
+ test_might_fail git config --unset difftool.prompt &&
+ test_config mergetool.prompt false &&
+ echo branch >expect &&
+ git difftool branch >actual &&
+ test_cmp expect actual
+'
+
+# Test that the -y flag can override difftool.prompt = true
+test_expect_success 'difftool.prompt can overridden with -y' '
+ difftool_test_setup &&
+ test_config difftool.prompt true &&
+ echo branch >expect &&
+ git difftool -y branch >actual &&
+ test_cmp expect actual
+'
+
+# Test that the --prompt flag can override difftool.prompt = false
+test_expect_success 'difftool.prompt can overridden with --prompt' '
+ difftool_test_setup &&
+ test_config difftool.prompt false &&
+ echo >input &&
+ git difftool --prompt branch <input >output &&
+ prompt=$(tail -1 <output) &&
+ prompt_given "$prompt"
+'
+
+# Test that the last flag passed on the command-line wins
+test_expect_success 'difftool last flag wins' '
+ difftool_test_setup &&
+ echo branch >expect &&
+ git difftool --prompt --no-prompt branch >actual &&
+ test_cmp expect actual &&
+ echo >input &&
+ git difftool --no-prompt --prompt branch <input >output &&
+ prompt=$(tail -1 <output) &&
+ prompt_given "$prompt"
+'
+
+# git-difftool falls back to git-mergetool config variables
+# so test that behavior here
+test_expect_success 'difftool + mergetool config variables' '
+ test_config merge.tool test-tool &&
+ test_config mergetool.test-tool.cmd "cat \$LOCAL" &&
+ echo branch >expect &&
+ git difftool --no-prompt branch >actual &&
+ test_cmp expect actual &&
+ git difftool --gui --no-prompt branch >actual &&
+ test_cmp expect actual &&
+
+ # set merge.tool to something bogus, diff.tool to test-tool
+ test_config merge.tool bogus-tool &&
+ test_config diff.tool test-tool &&
+ git difftool --no-prompt branch >actual &&
+ test_cmp expect actual &&
+ git difftool --gui --no-prompt branch >actual &&
+ test_cmp expect actual &&
+
+ # set merge.tool, diff.tool to something bogus, merge.guitool to test-tool
+ test_config diff.tool bogus-tool &&
+ test_config merge.guitool test-tool &&
+ git difftool --gui --no-prompt branch >actual &&
+ test_cmp expect actual &&
+
+ # set merge.tool, diff.tool, merge.guitool to something bogus, diff.guitool to test-tool
+ test_config merge.guitool bogus-tool &&
+ test_config diff.guitool test-tool &&
+ git difftool --gui --no-prompt branch >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'difftool.<tool>.path' '
+ test_config difftool.tkdiff.path echo &&
+ git difftool --tool=tkdiff --no-prompt branch >output &&
+ grep file output >grep-output &&
+ test_line_count = 1 grep-output
+'
+
+test_expect_success 'difftool --extcmd=cat' '
+ echo branch >expect &&
+ echo main >>expect &&
+ git difftool --no-prompt --extcmd=cat branch >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'difftool --extcmd cat' '
+ echo branch >expect &&
+ echo main >>expect &&
+ git difftool --no-prompt --extcmd=cat branch >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'difftool -x cat' '
+ echo branch >expect &&
+ echo main >>expect &&
+ git difftool --no-prompt -x cat branch >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'difftool --extcmd echo arg1' '
+ echo file >expect &&
+ git difftool --no-prompt \
+ --extcmd sh\ -c\ \"echo\ \$1\" branch >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'difftool --extcmd cat arg1' '
+ echo main >expect &&
+ git difftool --no-prompt \
+ --extcmd sh\ -c\ \"cat\ \$1\" branch >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'difftool --extcmd cat arg2' '
+ echo branch >expect &&
+ git difftool --no-prompt \
+ --extcmd sh\ -c\ \"cat\ \\\"\$2\\\"\" branch >actual &&
+ test_cmp expect actual
+'
+
+# Create a second file on main and a different version on branch
+test_expect_success 'setup with 2 files different' '
+ echo m2 >file2 &&
+ git add file2 &&
+ git commit -m "added file2" &&
+
+ git checkout branch &&
+ echo br2 >file2 &&
+ git add file2 &&
+ git commit -a -m "branch changed file2" &&
+ git checkout main
+'
+
+test_expect_success 'say no to the first file' '
+ (echo n && echo) >input &&
+ git difftool -x cat branch <input >output &&
+ grep m2 output &&
+ grep br2 output &&
+ ! grep main output &&
+ ! grep branch output
+'
+
+test_expect_success 'say no to the second file' '
+ (echo && echo n) >input &&
+ git difftool -x cat branch <input >output &&
+ grep main output &&
+ grep branch output &&
+ ! grep m2 output &&
+ ! grep br2 output
+'
+
+test_expect_success 'ending prompt input with EOF' '
+ git difftool -x cat branch </dev/null >output &&
+ ! grep main output &&
+ ! grep branch output &&
+ ! grep m2 output &&
+ ! grep br2 output
+'
+
+test_expect_success 'difftool --tool-help' '
+ git difftool --tool-help >output &&
+ grep tool output
+'
+
+test_expect_success 'setup change in subdirectory' '
+ git checkout main &&
+ mkdir sub &&
+ echo main >sub/sub &&
+ git add sub/sub &&
+ git commit -m "added sub/sub" &&
+ git tag v1 &&
+ echo test >>file &&
+ echo test >>sub/sub &&
+ git add file sub/sub &&
+ git commit -m "modified both"
+'
+
+test_expect_success 'difftool -d with growing paths' '
+ a=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&
+ git init growing &&
+ (
+ cd growing &&
+ echo "test -f \"\$2/b\"" | write_script .git/test-for-b.sh &&
+ one=$(printf 1 | git hash-object -w --stdin) &&
+ two=$(printf 2 | git hash-object -w --stdin) &&
+ git update-index --add \
+ --cacheinfo 100644,$one,$a --cacheinfo 100644,$two,b &&
+ tree1=$(git write-tree) &&
+ git update-index --add \
+ --cacheinfo 100644,$two,$a --cacheinfo 100644,$one,b &&
+ tree2=$(git write-tree) &&
+ git checkout -- $a &&
+ git difftool -d --extcmd .git/test-for-b.sh $tree1 $tree2
+ )
+'
+
+run_dir_diff_test () {
+ test_expect_success "$1 --no-symlinks" "
+ symlinks=--no-symlinks &&
+ $2
+ "
+ test_expect_success SYMLINKS "$1 --symlinks" "
+ symlinks=--symlinks &&
+ $2
+ "
+}
+
+run_dir_diff_test 'difftool -d' '
+ git difftool -d $symlinks --extcmd ls branch >output &&
+ grep "^sub$" output &&
+ grep "^file$" output
+'
+
+run_dir_diff_test 'difftool --dir-diff' '
+ git difftool --dir-diff $symlinks --extcmd ls branch >output &&
+ grep "^sub$" output &&
+ grep "^file$" output
+'
+
+run_dir_diff_test 'difftool --dir-diff avoids repeated slashes in TMPDIR' '
+ TMPDIR="${TMPDIR:-/tmp}////" \
+ git difftool --dir-diff $symlinks --extcmd echo branch >output &&
+ grep -v // output >actual &&
+ test_line_count = 1 actual
+'
+
+run_dir_diff_test 'difftool --dir-diff ignores --prompt' '
+ git difftool --dir-diff $symlinks --prompt --extcmd ls branch >output &&
+ grep "^sub$" output &&
+ grep "^file$" output
+'
+
+run_dir_diff_test 'difftool --dir-diff branch from subdirectory' '
+ (
+ cd sub &&
+ git difftool --dir-diff $symlinks --extcmd ls branch >output &&
+ # "sub" must only exist in "right"
+ # "file" and "file2" must be listed in both "left" and "right"
+ grep "^sub$" output >sub-output &&
+ test_line_count = 1 sub-output &&
+ grep "^file$" output >file-output &&
+ test_line_count = 2 file-output &&
+ grep "^file2$" output >file2-output &&
+ test_line_count = 2 file2-output
+ )
+'
+
+run_dir_diff_test 'difftool --dir-diff v1 from subdirectory' '
+ (
+ cd sub &&
+ git difftool --dir-diff $symlinks --extcmd ls v1 >output &&
+ # "sub" and "file" exist in both v1 and HEAD.
+ # "file2" is unchanged.
+ grep "^sub$" output >sub-output &&
+ test_line_count = 2 sub-output &&
+ grep "^file$" output >file-output &&
+ test_line_count = 2 file-output &&
+ ! grep "^file2$" output
+ )
+'
+
+run_dir_diff_test 'difftool --dir-diff branch from subdirectory w/ pathspec' '
+ (
+ cd sub &&
+ git difftool --dir-diff $symlinks --extcmd ls branch -- .>output &&
+ # "sub" only exists in "right"
+ # "file" and "file2" must not be listed
+ grep "^sub$" output >sub-output &&
+ test_line_count = 1 sub-output &&
+ ! grep "^file$" output
+ )
+'
+
+run_dir_diff_test 'difftool --dir-diff v1 from subdirectory w/ pathspec' '
+ (
+ cd sub &&
+ git difftool --dir-diff $symlinks --extcmd ls v1 -- .>output &&
+ # "sub" exists in v1 and HEAD
+ # "file" is filtered out by the pathspec
+ grep "^sub$" output >sub-output &&
+ test_line_count = 2 sub-output &&
+ ! grep "^file$" output
+ )
+'
+
+run_dir_diff_test 'difftool --dir-diff from subdirectory with GIT_DIR set' '
+ (
+ GIT_DIR=$(pwd)/.git &&
+ export GIT_DIR &&
+ GIT_WORK_TREE=$(pwd) &&
+ export GIT_WORK_TREE &&
+ cd sub &&
+ git difftool --dir-diff $symlinks --extcmd ls \
+ branch -- sub >output &&
+ grep "^sub$" output &&
+ ! grep "^file$" output
+ )
+'
+
+run_dir_diff_test 'difftool --dir-diff when worktree file is missing' '
+ test_when_finished git reset --hard &&
+ rm file2 &&
+ git difftool --dir-diff $symlinks --extcmd ls branch main >output &&
+ grep "^file2$" output
+'
+
+run_dir_diff_test 'difftool --dir-diff with unmerged files' '
+ test_when_finished git reset --hard &&
+ test_config difftool.echo.cmd "echo ok" &&
+ git checkout -B conflict-a &&
+ git checkout -B conflict-b &&
+ git checkout conflict-a &&
+ echo a >>file &&
+ git add file &&
+ git commit -m conflict-a &&
+ git checkout conflict-b &&
+ echo b >>file &&
+ git add file &&
+ git commit -m conflict-b &&
+ git checkout main &&
+ git merge conflict-a &&
+ test_must_fail git merge conflict-b &&
+ cat >expect <<-EOF &&
+ ok
+ EOF
+ git difftool --dir-diff $symlinks -t echo >actual &&
+ test_cmp expect actual
+'
+
+write_script .git/CHECK_SYMLINKS <<\EOF
+for f in file file2 sub/sub
+do
+ echo "$f"
+ ls -ld "$2/$f" | sed -e 's/.* -> //'
+done >actual
+EOF
+
+test_expect_success SYMLINKS 'difftool --dir-diff --symlinks without unstaged changes' '
+ cat >expect <<-EOF &&
+ file
+ $PWD/file
+ file2
+ $PWD/file2
+ sub/sub
+ $PWD/sub/sub
+ EOF
+ git difftool --dir-diff --symlinks \
+ --extcmd "./.git/CHECK_SYMLINKS" branch HEAD &&
+ test_cmp expect actual
+'
+
+write_script modify-right-file <<\EOF
+echo "new content" >"$2/file"
+EOF
+
+run_dir_diff_test 'difftool --dir-diff syncs worktree with unstaged change' '
+ test_when_finished git reset --hard &&
+ echo "orig content" >file &&
+ git difftool -d $symlinks --extcmd "$PWD/modify-right-file" branch &&
+ echo "new content" >expect &&
+ test_cmp expect file
+'
+
+run_dir_diff_test 'difftool --dir-diff syncs worktree without unstaged change' '
+ test_when_finished git reset --hard &&
+ git difftool -d $symlinks --extcmd "$PWD/modify-right-file" branch &&
+ echo "new content" >expect &&
+ test_cmp expect file
+'
+
+write_script modify-file <<\EOF
+echo "new content" >file
+EOF
+
+test_expect_success 'difftool --no-symlinks does not overwrite working tree file ' '
+ echo "orig content" >file &&
+ git difftool --dir-diff --no-symlinks --extcmd "$PWD/modify-file" branch &&
+ echo "new content" >expect &&
+ test_cmp expect file
+'
+
+write_script modify-both-files <<\EOF
+echo "wt content" >file &&
+echo "tmp content" >"$2/file" &&
+echo "$2" >tmpdir
+EOF
+
+test_expect_success 'difftool --no-symlinks detects conflict ' '
+ (
+ TMPDIR=$TRASH_DIRECTORY &&
+ export TMPDIR &&
+ echo "orig content" >file &&
+ test_must_fail git difftool --dir-diff --no-symlinks --extcmd "$PWD/modify-both-files" branch &&
+ echo "wt content" >expect &&
+ test_cmp expect file &&
+ echo "tmp content" >expect &&
+ test_cmp expect "$(cat tmpdir)/file"
+ )
+'
+
+test_expect_success 'difftool properly honors gitlink and core.worktree' '
+ test_when_finished rm -rf submod/ule &&
+ test_config_global protocol.file.allow always &&
+ git submodule add ./. submod/ule &&
+ test_config -C submod/ule diff.tool checktrees &&
+ test_config -C submod/ule difftool.checktrees.cmd '\''
+ test -d "$LOCAL" && test -d "$REMOTE" && echo good
+ '\'' &&
+ (
+ cd submod/ule &&
+ echo good >expect &&
+ git difftool --tool=checktrees --dir-diff HEAD~ >actual &&
+ test_cmp expect actual &&
+ rm -f expect actual
+ )
+'
+
+test_expect_success SYMLINKS 'difftool --dir-diff symlinked directories' '
+ test_when_finished git reset --hard &&
+ git init dirlinks &&
+ (
+ cd dirlinks &&
+ git config diff.tool checktrees &&
+ git config difftool.checktrees.cmd "echo good" &&
+ mkdir foo &&
+ : >foo/bar &&
+ git add foo/bar &&
+ test_commit symlink-one &&
+ ln -s foo link &&
+ git add link &&
+ test_commit symlink-two &&
+ echo good >expect &&
+ git difftool --tool=checktrees --dir-diff HEAD~ >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success SYMLINKS 'difftool --dir-diff handles modified symlinks' '
+ test_when_finished git reset --hard &&
+ touch b &&
+ ln -s b c &&
+ git add b c &&
+ test_tick &&
+ git commit -m initial &&
+ touch d &&
+ rm c &&
+ ln -s d c &&
+ cat >expect <<-EOF &&
+ c
+
+ c
+ EOF
+ git difftool --symlinks --dir-diff --extcmd ls >output &&
+ grep -v ^/ output >actual &&
+ test_cmp expect actual &&
+
+ git difftool --no-symlinks --dir-diff --extcmd ls >output &&
+ grep -v ^/ output >actual &&
+ test_cmp expect actual &&
+
+ # The left side contains symlink "c" that points to "b"
+ test_config difftool.cat.cmd "cat \$LOCAL/c" &&
+ printf "%s\n" b >expect &&
+
+ git difftool --symlinks --dir-diff --tool cat >actual &&
+ test_cmp expect actual &&
+
+ git difftool --symlinks --no-symlinks --dir-diff --tool cat >actual &&
+ test_cmp expect actual &&
+
+ # The right side contains symlink "c" that points to "d"
+ test_config difftool.cat.cmd "cat \$REMOTE/c" &&
+ printf "%s\n" d >expect &&
+
+ git difftool --symlinks --dir-diff --tool cat >actual &&
+ test_cmp expect actual &&
+
+ git difftool --no-symlinks --dir-diff --tool cat >actual &&
+ test_cmp expect actual &&
+
+ # Deleted symlinks
+ rm -f c &&
+ cat >expect <<-EOF &&
+ c
+
+ EOF
+ git difftool --symlinks --dir-diff --extcmd ls >output &&
+ grep -v ^/ output >actual &&
+ test_cmp expect actual &&
+
+ git difftool --no-symlinks --dir-diff --extcmd ls >output &&
+ grep -v ^/ output >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success SYMLINKS 'difftool --dir-diff writes symlinks as raw text' '
+ # Start out on a branch called "branch-init".
+ git init -b branch-init symlink-files &&
+ (
+ cd symlink-files &&
+ # This test ensures that symlinks are written as raw text.
+ # The "cat" tools output link and file contents.
+ git config difftool.cat-left-link.cmd "cat \"\$LOCAL/link\"" &&
+ git config difftool.cat-left-a.cmd "cat \"\$LOCAL/file-a\"" &&
+ git config difftool.cat-right-link.cmd "cat \"\$REMOTE/link\"" &&
+ git config difftool.cat-right-b.cmd "cat \"\$REMOTE/file-b\"" &&
+
+ # Record the empty initial state so that we can come back here
+ # later and not have to consider the any cases where difftool
+ # will create symlinks back into the worktree.
+ test_tick &&
+ git commit --allow-empty -m init &&
+
+ # Create a file called "file-a" with a symlink pointing to it.
+ git switch -c branch-a &&
+ echo a >file-a &&
+ ln -s file-a link &&
+ git add file-a link &&
+ test_tick &&
+ git commit -m link-to-file-a &&
+
+ # Create a file called "file-b" and point the symlink to it.
+ git switch -c branch-b &&
+ echo b >file-b &&
+ rm link &&
+ ln -s file-b link &&
+ git add file-b link &&
+ git rm file-a &&
+ test_tick &&
+ git commit -m link-to-file-b &&
+
+ # Checkout the initial branch so that the --symlinks behavior is
+ # not activated. The two directories should be completely
+ # independent with no symlinks pointing back here.
+ git switch branch-init &&
+
+ # The left link must be "file-a" and "file-a" must contain "a".
+ echo file-a >expect &&
+ git difftool --symlinks --dir-diff --tool cat-left-link \
+ branch-a branch-b >actual &&
+ test_cmp expect actual &&
+
+ echo a >expect &&
+ git difftool --symlinks --dir-diff --tool cat-left-a \
+ branch-a branch-b >actual &&
+ test_cmp expect actual &&
+
+ # The right link must be "file-b" and "file-b" must contain "b".
+ echo file-b >expect &&
+ git difftool --symlinks --dir-diff --tool cat-right-link \
+ branch-a branch-b >actual &&
+ test_cmp expect actual &&
+
+ echo b >expect &&
+ git difftool --symlinks --dir-diff --tool cat-right-b \
+ branch-a branch-b >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'add -N and difftool -d' '
+ test_when_finished git reset --hard &&
+
+ test_write_lines A B C >intent-to-add &&
+ git add -N intent-to-add &&
+ git difftool --dir-diff --extcmd ls
+'
+
+test_expect_success 'difftool --cached with unmerged files' '
+ test_when_finished git reset --hard &&
+
+ test_commit conflicting &&
+ test_commit conflict-a conflict.t a &&
+ git reset --hard conflicting &&
+ test_commit conflict-b conflict.t b &&
+ test_must_fail git merge conflict-a &&
+
+ git difftool --cached --no-prompt >output &&
+ test_must_be_empty output
+'
+
+test_expect_success 'outside worktree' '
+ echo 1 >1 &&
+ echo 2 >2 &&
+ test_expect_code 1 nongit git \
+ -c diff.tool=echo -c difftool.echo.cmd="echo \$LOCAL \$REMOTE" \
+ difftool --no-prompt --no-index ../1 ../2 >actual &&
+ echo "../1 ../2" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'difftool --gui, --tool and --extcmd are mutually exclusive' '
+ difftool_test_setup &&
+ test_must_fail git difftool --gui --tool=test-tool &&
+ test_must_fail git difftool --gui --extcmd=cat &&
+ test_must_fail git difftool --tool=test-tool --extcmd=cat &&
+ test_must_fail git difftool --gui --tool=test-tool --extcmd=cat
+'
+
+test_expect_success 'difftool --rotate-to' '
+ difftool_test_setup &&
+ test_when_finished git reset --hard &&
+ echo 1 >1 &&
+ echo 2 >2 &&
+ echo 4 >4 &&
+ git add 1 2 4 &&
+ git commit -a -m "124" &&
+ git difftool --no-prompt --extcmd=cat --rotate-to="2" HEAD^ >output &&
+ cat >expect <<-\EOF &&
+ 2
+ 4
+ 1
+ EOF
+ test_cmp output expect
+'
+
+test_expect_success 'difftool --skip-to' '
+ difftool_test_setup &&
+ test_when_finished git reset --hard &&
+ git difftool --no-prompt --extcmd=cat --skip-to="2" HEAD^ >output &&
+ cat >expect <<-\EOF &&
+ 2
+ 4
+ EOF
+ test_cmp output expect
+'
+
+test_expect_success 'difftool --rotate/skip-to error condition' '
+ test_must_fail git difftool --no-prompt --extcmd=cat --rotate-to="3" HEAD^ &&
+ test_must_fail git difftool --no-prompt --extcmd=cat --skip-to="3" HEAD^
+'
+test_done
diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh
new file mode 100755
index 0000000..875dcfd
--- /dev/null
+++ b/t/t7810-grep.sh
@@ -0,0 +1,1922 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Junio C Hamano
+#
+
+test_description='git grep various.
+'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_invalid_grep_expression() {
+ params="$@" &&
+ test_expect_success "invalid expression: grep $params" '
+ test_must_fail git grep $params -- nonexisting
+ '
+}
+
+LC_ALL=en_US.UTF-8 test-tool regex '^.$' '¿' &&
+ test_set_prereq MB_REGEX
+
+cat >hello.c <<EOF
+#include <assert.h>
+#include <stdio.h>
+
+int main(int argc, const char **argv)
+{
+ printf("Hello world.\n");
+ return 0;
+ /* char ?? */
+}
+EOF
+
+test_expect_success setup '
+ cat >file <<-\EOF &&
+ foo mmap bar
+ foo_mmap bar
+ foo_mmap bar mmap
+ foo mmap bar_mmap
+ foo_mmap bar mmap baz
+ EOF
+ cat >hello_world <<-\EOF &&
+ Hello world
+ HeLLo world
+ Hello_world
+ HeLLo_world
+ EOF
+ cat >ab <<-\EOF &&
+ a+b*c
+ a+bc
+ abc
+ EOF
+ cat >d0 <<-\EOF &&
+ d
+ 0
+ EOF
+ echo vvv >v &&
+ echo ww w >w &&
+ echo x x xx x >x &&
+ echo y yy >y &&
+ echo zzz > z &&
+ mkdir t &&
+ echo test >t/t &&
+ echo vvv >t/v &&
+ mkdir t/a &&
+ echo vvv >t/a/v &&
+ qz_to_tab_space >space <<-\EOF &&
+ line without leading space1
+ Zline with leading space1
+ Zline with leading space2
+ Zline with leading space3
+ line without leading space2
+ EOF
+ cat >hello.ps1 <<-\EOF &&
+ # No-op.
+ function dummy() {}
+
+ # Say hello.
+ function hello() {
+ echo "Hello world."
+ echo "Hello again."
+ } # hello
+
+ # Still a no-op.
+ function dummy() {}
+ EOF
+ if test_have_prereq FUNNYNAMES
+ then
+ echo unusual >"\"unusual\" pathname" &&
+ echo unusual >"t/nested \"unusual\" pathname"
+ fi &&
+ if test_have_prereq MB_REGEX
+ then
+ echo "¿" >reverse-question-mark
+ fi &&
+ git add . &&
+ test_tick &&
+ git commit -m initial
+'
+
+test_expect_success 'grep should not segfault with a bad input' '
+ test_must_fail git grep "("
+'
+
+test_invalid_grep_expression --and -e A
+
+test_pattern_type () {
+ H=$1 &&
+ HC=$2 &&
+ L=$3 &&
+ type=$4 &&
+ shift 4 &&
+
+ expected_str= &&
+ case "$type" in
+ BRE)
+ expected_str="${HC}ab:a+bc"
+ ;;
+ ERE)
+ expected_str="${HC}ab:abc"
+ ;;
+ FIX)
+ expected_str="${HC}ab:a+b*c"
+ ;;
+ *)
+ BUG "unknown pattern type '$type'"
+ ;;
+ esac &&
+ config_str="$@" &&
+
+ test_expect_success "grep $L with '$config_str' interpreted as $type" '
+ echo $expected_str >expected &&
+ git $config_str grep "a+b*c" $H ab >actual &&
+ test_cmp expected actual
+ '
+}
+
+for H in HEAD ''
+do
+ case "$H" in
+ HEAD) HC='HEAD:' L='HEAD' ;;
+ '') HC= L='in working tree' ;;
+ esac
+
+ test_expect_success "grep -w $L" '
+ cat >expected <<-EOF &&
+ ${HC}file:1:foo mmap bar
+ ${HC}file:3:foo_mmap bar mmap
+ ${HC}file:4:foo mmap bar_mmap
+ ${HC}file:5:foo_mmap bar mmap baz
+ EOF
+ git -c grep.linenumber=false grep -n -w -e mmap $H >actual &&
+ test_cmp expected actual
+ '
+
+ test_expect_success "grep -w $L (with --column)" '
+ cat >expected <<-EOF &&
+ ${HC}file:5:foo mmap bar
+ ${HC}file:14:foo_mmap bar mmap
+ ${HC}file:5:foo mmap bar_mmap
+ ${HC}file:14:foo_mmap bar mmap baz
+ EOF
+ git grep --column -w -e mmap $H >actual &&
+ test_cmp expected actual
+ '
+
+ test_expect_success "grep -w $L (with --column, extended OR)" '
+ cat >expected <<-EOF &&
+ ${HC}file:14:foo_mmap bar mmap
+ ${HC}file:19:foo_mmap bar mmap baz
+ EOF
+ git grep --column -w -e mmap$ --or -e baz $H >actual &&
+ test_cmp expected actual
+ '
+
+ test_expect_success "grep -w $L (with --column, --invert-match)" '
+ cat >expected <<-EOF &&
+ ${HC}file:1:foo mmap bar
+ ${HC}file:1:foo_mmap bar
+ ${HC}file:1:foo_mmap bar mmap
+ ${HC}file:1:foo mmap bar_mmap
+ EOF
+ git grep --column --invert-match -w -e baz $H -- file >actual &&
+ test_cmp expected actual
+ '
+
+ test_expect_success "grep $L (with --column, --invert-match, extended OR)" '
+ cat >expected <<-EOF &&
+ ${HC}hello_world:6:HeLLo_world
+ EOF
+ git grep --column --invert-match -e ll --or --not -e _ $H -- hello_world \
+ >actual &&
+ test_cmp expected actual
+ '
+
+ test_expect_success "grep $L (with --column, --invert-match, extended AND)" '
+ cat >expected <<-EOF &&
+ ${HC}hello_world:3:Hello world
+ ${HC}hello_world:3:Hello_world
+ ${HC}hello_world:6:HeLLo_world
+ EOF
+ git grep --column --invert-match --not -e _ --and --not -e ll $H -- hello_world \
+ >actual &&
+ test_cmp expected actual
+ '
+
+ test_expect_success "grep $L (with --column, double-negation)" '
+ cat >expected <<-EOF &&
+ ${HC}file:1:foo_mmap bar mmap baz
+ EOF
+ git grep --column --not \( --not -e foo --or --not -e baz \) $H -- file \
+ >actual &&
+ test_cmp expected actual
+ '
+
+ test_expect_success "grep -w $L (with --column, -C)" '
+ cat >expected <<-EOF &&
+ ${HC}file:5:foo mmap bar
+ ${HC}file-foo_mmap bar
+ ${HC}file:14:foo_mmap bar mmap
+ ${HC}file:5:foo mmap bar_mmap
+ ${HC}file:14:foo_mmap bar mmap baz
+ EOF
+ git grep --column -w -C1 -e mmap $H >actual &&
+ test_cmp expected actual
+ '
+
+ test_expect_success "grep -w $L (with --line-number, --column)" '
+ cat >expected <<-EOF &&
+ ${HC}file:1:5:foo mmap bar
+ ${HC}file:3:14:foo_mmap bar mmap
+ ${HC}file:4:5:foo mmap bar_mmap
+ ${HC}file:5:14:foo_mmap bar mmap baz
+ EOF
+ git grep -n --column -w -e mmap $H >actual &&
+ test_cmp expected actual
+ '
+
+ test_expect_success "grep -w $L (with non-extended patterns, --column)" '
+ cat >expected <<-EOF &&
+ ${HC}file:5:foo mmap bar
+ ${HC}file:10:foo_mmap bar
+ ${HC}file:10:foo_mmap bar mmap
+ ${HC}file:5:foo mmap bar_mmap
+ ${HC}file:10:foo_mmap bar mmap baz
+ EOF
+ git grep --column -w -e bar -e mmap $H >actual &&
+ test_cmp expected actual
+ '
+
+ test_expect_success "grep -w $L" '
+ cat >expected <<-EOF &&
+ ${HC}file:1:foo mmap bar
+ ${HC}file:3:foo_mmap bar mmap
+ ${HC}file:4:foo mmap bar_mmap
+ ${HC}file:5:foo_mmap bar mmap baz
+ EOF
+ git -c grep.linenumber=true grep -w -e mmap $H >actual &&
+ test_cmp expected actual
+ '
+
+ test_expect_success "grep -w $L" '
+ cat >expected <<-EOF &&
+ ${HC}file:foo mmap bar
+ ${HC}file:foo_mmap bar mmap
+ ${HC}file:foo mmap bar_mmap
+ ${HC}file:foo_mmap bar mmap baz
+ EOF
+ git -c grep.linenumber=true grep --no-line-number -w -e mmap $H >actual &&
+ test_cmp expected actual
+ '
+
+ test_expect_success "grep -w $L (w)" '
+ test_must_fail git grep -n -w -e "^w" $H >actual &&
+ test_must_be_empty actual
+ '
+
+ test_expect_success "grep -w $L (x)" '
+ cat >expected <<-EOF &&
+ ${HC}x:1:x x xx x
+ EOF
+ git grep -n -w -e "x xx* x" $H >actual &&
+ test_cmp expected actual
+ '
+
+ test_expect_success "grep -w $L (y-1)" '
+ cat >expected <<-EOF &&
+ ${HC}y:1:y yy
+ EOF
+ git grep -n -w -e "^y" $H >actual &&
+ test_cmp expected actual
+ '
+
+ test_expect_success "grep -w $L (y-2)" '
+ if git grep -n -w -e "^y y" $H >actual
+ then
+ echo should not have matched
+ cat actual
+ false
+ else
+ test_must_be_empty actual
+ fi
+ '
+
+ test_expect_success "grep -w $L (z)" '
+ if git grep -n -w -e "^z" $H >actual
+ then
+ echo should not have matched
+ cat actual
+ false
+ else
+ test_must_be_empty actual
+ fi
+ '
+
+ test_expect_success "grep $L (with --column, --only-matching)" '
+ cat >expected <<-EOF &&
+ ${HC}file:1:5:mmap
+ ${HC}file:2:5:mmap
+ ${HC}file:3:5:mmap
+ ${HC}file:3:13:mmap
+ ${HC}file:4:5:mmap
+ ${HC}file:4:13:mmap
+ ${HC}file:5:5:mmap
+ ${HC}file:5:13:mmap
+ EOF
+ git grep --column -n -o -e mmap $H >actual &&
+ test_cmp expected actual
+ '
+
+ test_expect_success "grep $L (t-1)" '
+ echo "${HC}t/t:1:test" >expected &&
+ git grep -n -e test $H >actual &&
+ test_cmp expected actual
+ '
+
+ test_expect_success "grep $L (t-2)" '
+ echo "${HC}t:1:test" >expected &&
+ (
+ cd t &&
+ git grep -n -e test $H
+ ) >actual &&
+ test_cmp expected actual
+ '
+
+ test_expect_success "grep $L (t-3)" '
+ echo "${HC}t/t:1:test" >expected &&
+ (
+ cd t &&
+ git grep --full-name -n -e test $H
+ ) >actual &&
+ test_cmp expected actual
+ '
+
+ test_expect_success "grep -c $L (no /dev/null)" '
+ ! git grep -c test $H | grep /dev/null
+ '
+
+ test_expect_success "grep --max-depth -1 $L" '
+ cat >expected <<-EOF &&
+ ${HC}t/a/v:1:vvv
+ ${HC}t/v:1:vvv
+ ${HC}v:1:vvv
+ EOF
+ git grep --max-depth -1 -n -e vvv $H >actual &&
+ test_cmp expected actual &&
+ git grep --recursive -n -e vvv $H >actual &&
+ test_cmp expected actual
+ '
+
+ test_expect_success "grep --max-depth 0 $L" '
+ cat >expected <<-EOF &&
+ ${HC}v:1:vvv
+ EOF
+ git grep --max-depth 0 -n -e vvv $H >actual &&
+ test_cmp expected actual &&
+ git grep --no-recursive -n -e vvv $H >actual &&
+ test_cmp expected actual
+ '
+
+ test_expect_success "grep --max-depth 0 -- '*' $L" '
+ cat >expected <<-EOF &&
+ ${HC}t/a/v:1:vvv
+ ${HC}t/v:1:vvv
+ ${HC}v:1:vvv
+ EOF
+ git grep --max-depth 0 -n -e vvv $H -- "*" >actual &&
+ test_cmp expected actual &&
+ git grep --no-recursive -n -e vvv $H -- "*" >actual &&
+ test_cmp expected actual
+ '
+
+ test_expect_success "grep --max-depth 1 $L" '
+ cat >expected <<-EOF &&
+ ${HC}t/v:1:vvv
+ ${HC}v:1:vvv
+ EOF
+ git grep --max-depth 1 -n -e vvv $H >actual &&
+ test_cmp expected actual
+ '
+
+ test_expect_success "grep --max-depth 0 -- t $L" '
+ cat >expected <<-EOF &&
+ ${HC}t/v:1:vvv
+ EOF
+ git grep --max-depth 0 -n -e vvv $H -- t >actual &&
+ test_cmp expected actual &&
+ git grep --no-recursive -n -e vvv $H -- t >actual &&
+ test_cmp expected actual
+ '
+
+ test_expect_success "grep --max-depth 0 -- . t $L" '
+ cat >expected <<-EOF &&
+ ${HC}t/v:1:vvv
+ ${HC}v:1:vvv
+ EOF
+ git grep --max-depth 0 -n -e vvv $H -- . t >actual &&
+ test_cmp expected actual &&
+ git grep --no-recursive -n -e vvv $H -- . t >actual &&
+ test_cmp expected actual
+ '
+
+ test_expect_success "grep --max-depth 0 -- t . $L" '
+ cat >expected <<-EOF &&
+ ${HC}t/v:1:vvv
+ ${HC}v:1:vvv
+ EOF
+ git grep --max-depth 0 -n -e vvv $H -- t . >actual &&
+ test_cmp expected actual &&
+ git grep --no-recursive -n -e vvv $H -- t . >actual &&
+ test_cmp expected actual
+ '
+
+
+ test_pattern_type "$H" "$HC" "$L" BRE -c grep.extendedRegexp=false
+ test_pattern_type "$H" "$HC" "$L" ERE -c grep.extendedRegexp=true
+ test_pattern_type "$H" "$HC" "$L" BRE -c grep.patternType=basic
+ test_pattern_type "$H" "$HC" "$L" ERE -c grep.patternType=extended
+ test_pattern_type "$H" "$HC" "$L" FIX -c grep.patternType=fixed
+
+ test_expect_success PCRE "grep $L with grep.patterntype=perl" '
+ echo "${HC}ab:a+b*c" >expected &&
+ git -c grep.patterntype=perl grep "a\x{2b}b\x{2a}c" $H ab >actual &&
+ test_cmp expected actual
+ '
+
+ test_expect_success !FAIL_PREREQS,!PCRE "grep $L with grep.patterntype=perl errors without PCRE" '
+ test_must_fail git -c grep.patterntype=perl grep "foo.*bar"
+ '
+
+ test_pattern_type "$H" "$HC" "$L" ERE \
+ -c grep.patternType=default \
+ -c grep.extendedRegexp=true
+ test_pattern_type "$H" "$HC" "$L" ERE \
+ -c grep.extendedRegexp=true \
+ -c grep.patternType=default
+ test_pattern_type "$H" "$HC" "$L" ERE \
+ -c grep.patternType=extended \
+ -c grep.extendedRegexp=false
+ test_pattern_type "$H" "$HC" "$L" BRE \
+ -c grep.patternType=basic \
+ -c grep.extendedRegexp=true
+ test_pattern_type "$H" "$HC" "$L" ERE \
+ -c grep.extendedRegexp=false \
+ -c grep.patternType=extended
+ test_pattern_type "$H" "$HC" "$L" BRE \
+ -c grep.extendedRegexp=true \
+ -c grep.patternType=basic
+
+ # grep.extendedRegexp is last-one-wins
+ test_pattern_type "$H" "$HC" "$L" BRE \
+ -c grep.extendedRegexp=true \
+ -c grep.extendedRegexp=false
+
+ # grep.patternType=basic pays no attention to grep.extendedRegexp
+ test_pattern_type "$H" "$HC" "$L" BRE \
+ -c grep.extendedRegexp=true \
+ -c grep.patternType=basic \
+ -c grep.extendedRegexp=false
+
+ # grep.patternType=extended pays no attention to grep.extendedRegexp
+ test_pattern_type "$H" "$HC" "$L" ERE \
+ -c grep.extendedRegexp=true \
+ -c grep.patternType=extended \
+ -c grep.extendedRegexp=false
+
+ # grep.extendedRegexp is used with a last-one-wins grep.patternType=default
+ test_pattern_type "$H" "$HC" "$L" ERE \
+ -c grep.patternType=fixed \
+ -c grep.extendedRegexp=true \
+ -c grep.patternType=default
+
+ # grep.extendedRegexp is used with earlier grep.patternType=default
+ test_pattern_type "$H" "$HC" "$L" ERE \
+ -c grep.extendedRegexp=false \
+ -c grep.patternType=default \
+ -c grep.extendedRegexp=true
+
+ # grep.extendedRegexp is used with a last-one-loses grep.patternType=default
+ test_pattern_type "$H" "$HC" "$L" ERE \
+ -c grep.extendedRegexp=false \
+ -c grep.extendedRegexp=true \
+ -c grep.patternType=default
+
+ # grep.extendedRegexp and grep.patternType are both last-one-wins independently
+ test_pattern_type "$H" "$HC" "$L" BRE \
+ -c grep.patternType=default \
+ -c grep.extendedRegexp=true \
+ -c grep.patternType=basic
+
+ # grep.patternType=extended and grep.patternType=default
+ test_pattern_type "$H" "$HC" "$L" BRE \
+ -c grep.patternType=extended \
+ -c grep.patternType=default
+
+ # grep.patternType=[extended -> default -> fixed] (BRE)" '
+ test_pattern_type "$H" "$HC" "$L" FIX \
+ -c grep.patternType=extended \
+ -c grep.patternType=default \
+ -c grep.patternType=fixed
+
+ test_expect_success "grep --count $L" '
+ echo ${HC}ab:3 >expected &&
+ git grep --count -e b $H -- ab >actual &&
+ test_cmp expected actual
+ '
+
+ test_expect_success "grep --count -h $L" '
+ echo 3 >expected &&
+ git grep --count -h -e b $H -- ab >actual &&
+ test_cmp expected actual
+ '
+
+ test_expect_success FUNNYNAMES "grep $L should quote unusual pathnames" '
+ cat >expected <<-EOF &&
+ ${HC}"\"unusual\" pathname":unusual
+ ${HC}"t/nested \"unusual\" pathname":unusual
+ EOF
+ git grep unusual $H >actual &&
+ test_cmp expected actual
+ '
+
+ test_expect_success FUNNYNAMES "grep $L in subdir should quote unusual relative pathnames" '
+ cat >expected <<-EOF &&
+ ${HC}"nested \"unusual\" pathname":unusual
+ EOF
+ (
+ cd t &&
+ git grep unusual $H
+ ) >actual &&
+ test_cmp expected actual
+ '
+
+ test_expect_success FUNNYNAMES "grep -z $L with unusual pathnames" '
+ cat >expected <<-EOF &&
+ ${HC}"unusual" pathname:unusual
+ ${HC}t/nested "unusual" pathname:unusual
+ EOF
+ git grep -z unusual $H >actual &&
+ tr "\0" ":" <actual >actual-replace-null &&
+ test_cmp expected actual-replace-null
+ '
+
+ test_expect_success FUNNYNAMES "grep -z $L in subdir with unusual relative pathnames" '
+ cat >expected <<-EOF &&
+ ${HC}nested "unusual" pathname:unusual
+ EOF
+ (
+ cd t &&
+ git grep -z unusual $H
+ ) >actual &&
+ tr "\0" ":" <actual >actual-replace-null &&
+ test_cmp expected actual-replace-null
+ '
+done
+
+test_expect_success MB_REGEX 'grep exactly one char in single-char multibyte file' '
+ LC_ALL=en_US.UTF-8 git grep "^.$" reverse-question-mark
+'
+
+test_expect_success MB_REGEX 'grep two chars in single-char multibyte file' '
+ LC_ALL=en_US.UTF-8 test_expect_code 1 git grep ".." reverse-question-mark
+'
+
+cat >expected <<EOF
+file
+EOF
+test_expect_success 'grep -l -C' '
+ git grep -l -C1 foo >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<EOF
+file:5
+EOF
+test_expect_success 'grep -c -C' '
+ git grep -c -C1 foo >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'grep -L -C' '
+ git ls-files >expected &&
+ git grep -L -C1 nonexistent_string >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'grep --files-without-match --quiet' '
+ git grep --files-without-match --quiet nonexistent_string >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'grep --max-count 0 (must exit with non-zero)' '
+ test_must_fail git grep --max-count 0 foo >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'grep --max-count 3' '
+ cat >expected <<-EOF &&
+ file:foo mmap bar
+ file:foo_mmap bar
+ file:foo_mmap bar mmap
+ EOF
+ git grep --max-count 3 foo >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'grep --max-count -1 (no limit)' '
+ cat >expected <<-EOF &&
+ file:foo mmap bar
+ file:foo_mmap bar
+ file:foo_mmap bar mmap
+ file:foo mmap bar_mmap
+ file:foo_mmap bar mmap baz
+ EOF
+ git grep --max-count -1 foo >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'grep --max-count 1 --context 2' '
+ cat >expected <<-EOF &&
+ file-foo mmap bar
+ file:foo_mmap bar
+ file-foo_mmap bar mmap
+ EOF
+ git grep --max-count 1 --context 1 foo_mmap >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'grep --max-count 1 --show-function' '
+ cat >expected <<-EOF &&
+ hello.ps1=function hello() {
+ hello.ps1: echo "Hello world."
+ EOF
+ git grep --max-count 1 --show-function Hello hello.ps1 >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'grep --max-count 2 --show-function' '
+ cat >expected <<-EOF &&
+ hello.ps1=function hello() {
+ hello.ps1: echo "Hello world."
+ hello.ps1: echo "Hello again."
+ EOF
+ git grep --max-count 2 --show-function Hello hello.ps1 >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'grep --max-count 1 --count' '
+ cat >expected <<-EOF &&
+ hello.ps1:1
+ EOF
+ git grep --max-count 1 --count Hello hello.ps1 >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'grep --max-count 1 (multiple files)' '
+ cat >expected <<-EOF &&
+ hello.c:#include <stdio.h>
+ hello.ps1:# No-op.
+ EOF
+ git grep --max-count 1 -e o -- hello.\* >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'grep --max-count 1 --context 1 (multiple files)' '
+ cat >expected <<-EOF &&
+ hello.c-#include <assert.h>
+ hello.c:#include <stdio.h>
+ hello.c-
+ --
+ hello.ps1:# No-op.
+ hello.ps1-function dummy() {}
+ EOF
+ git grep --max-count 1 --context 1 -e o -- hello.\* >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<EOF
+file:foo mmap bar_mmap
+EOF
+
+test_expect_success 'grep -e A --and -e B' '
+ git grep -e "foo mmap" --and -e bar_mmap >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<EOF
+file:foo_mmap bar mmap
+file:foo_mmap bar mmap baz
+EOF
+
+
+test_expect_success 'grep ( -e A --or -e B ) --and -e B' '
+ git grep \( -e foo_ --or -e baz \) \
+ --and -e " mmap" >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<EOF
+file:foo mmap bar
+EOF
+
+test_expect_success 'grep -e A --and --not -e B' '
+ git grep -e "foo mmap" --and --not -e bar_mmap >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'grep should ignore GREP_OPTIONS' '
+ GREP_OPTIONS=-v git grep " mmap bar\$" >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'grep -f, non-existent file' '
+ test_must_fail git grep -f patterns
+'
+
+cat >expected <<EOF
+file:foo mmap bar
+file:foo_mmap bar
+file:foo_mmap bar mmap
+file:foo mmap bar_mmap
+file:foo_mmap bar mmap baz
+EOF
+
+cat >pattern <<EOF
+mmap
+EOF
+
+test_expect_success 'grep -f, one pattern' '
+ git grep -f pattern >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<EOF
+file:foo mmap bar
+file:foo_mmap bar
+file:foo_mmap bar mmap
+file:foo mmap bar_mmap
+file:foo_mmap bar mmap baz
+t/a/v:vvv
+t/v:vvv
+v:vvv
+EOF
+
+cat >patterns <<EOF
+mmap
+vvv
+EOF
+
+test_expect_success 'grep -f, multiple patterns' '
+ git grep -f patterns >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'grep, multiple patterns' '
+ git grep "$(cat patterns)" >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<EOF
+file:foo mmap bar
+file:foo_mmap bar
+file:foo_mmap bar mmap
+file:foo mmap bar_mmap
+file:foo_mmap bar mmap baz
+t/a/v:vvv
+t/v:vvv
+v:vvv
+EOF
+
+cat >patterns <<EOF
+
+mmap
+
+vvv
+
+EOF
+
+test_expect_success 'grep -f, ignore empty lines' '
+ git grep -f patterns >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'grep -f, ignore empty lines, read patterns from stdin' '
+ git grep -f - <patterns >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'grep -f, use cwd relative file' '
+ test_when_finished "git rm -f sub/dir/file" &&
+ mkdir -p sub/dir &&
+ echo hit >sub/dir/file &&
+ git add sub/dir/file &&
+ echo hit >sub/dir/pattern &&
+ echo miss >pattern &&
+ (
+ cd sub/dir && git grep -f pattern file
+ ) &&
+ git -C sub/dir grep -f pattern file
+'
+
+cat >expected <<EOF
+y:y yy
+--
+z:zzz
+EOF
+
+test_expect_success 'grep -q, silently report matches' '
+ git grep -q mmap >actual &&
+ test_must_be_empty actual &&
+ test_must_fail git grep -q qfwfq >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'grep -C1 hunk mark between files' '
+ git grep -C1 "^[yz]" >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'log grep setup' '
+ test_commit --append --author "With * Asterisk <xyzzy@frotz.com>" second file a &&
+ test_commit --append third file a &&
+ test_commit --append --author "Night Fall <nitfol@frobozz.com>" fourth file a
+'
+
+test_expect_success 'log grep (1)' '
+ git log --author=author --pretty=tformat:%s >actual &&
+ {
+ echo third && echo initial
+ } >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log grep (2)' '
+ git log --author=" * " -F --pretty=tformat:%s >actual &&
+ {
+ echo second
+ } >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log grep (3)' '
+ git log --author="^A U" --pretty=tformat:%s >actual &&
+ {
+ echo third && echo initial
+ } >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log grep (4)' '
+ git log --author="frotz\.com>$" --pretty=tformat:%s >actual &&
+ {
+ echo second
+ } >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log grep (5)' '
+ git log --author=Thor -F --pretty=tformat:%s >actual &&
+ {
+ echo third && echo initial
+ } >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log grep (6)' '
+ git log --author=-0700 --pretty=tformat:%s >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'log grep (7)' '
+ git log -g --grep-reflog="commit: third" --pretty=tformat:%s >actual &&
+ echo third >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log grep (8)' '
+ git log -g --grep-reflog="commit: third" --grep-reflog="commit: second" --pretty=tformat:%s >actual &&
+ {
+ echo third && echo second
+ } >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log grep (9)' '
+ git log -g --grep-reflog="commit: third" --author="Thor" --pretty=tformat:%s >actual &&
+ echo third >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log grep (9)' '
+ git log -g --grep-reflog="commit: third" --author="non-existent" --pretty=tformat:%s >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'log --grep-reflog can only be used under -g' '
+ test_must_fail git log --grep-reflog="commit: third"
+'
+
+test_expect_success 'log with multiple --grep uses union' '
+ git log --grep=i --grep=r --format=%s >actual &&
+ {
+ echo fourth && echo third && echo initial
+ } >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log --all-match with multiple --grep uses intersection' '
+ git log --all-match --grep=i --grep=r --format=%s >actual &&
+ {
+ echo third
+ } >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log with multiple --author uses union' '
+ git log --author="Thor" --author="Aster" --format=%s >actual &&
+ {
+ echo third && echo second && echo initial
+ } >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log --all-match with multiple --author still uses union' '
+ git log --all-match --author="Thor" --author="Aster" --format=%s >actual &&
+ {
+ echo third && echo second && echo initial
+ } >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log --grep --author uses intersection' '
+ # grep matches only third and fourth
+ # author matches only initial and third
+ git log --author="A U Thor" --grep=r --format=%s >actual &&
+ {
+ echo third
+ } >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log --grep --grep --author takes union of greps and intersects with author' '
+ # grep matches initial and second but not third
+ # author matches only initial and third
+ git log --author="A U Thor" --grep=s --grep=l --format=%s >actual &&
+ {
+ echo initial
+ } >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log ---all-match -grep --author --author still takes union of authors and intersects with grep' '
+ # grep matches only initial and third
+ # author matches all but second
+ git log --all-match --author="Thor" --author="Night" --grep=i --format=%s >actual &&
+ {
+ echo third && echo initial
+ } >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log --grep --author --author takes union of authors and intersects with grep' '
+ # grep matches only initial and third
+ # author matches all but second
+ git log --author="Thor" --author="Night" --grep=i --format=%s >actual &&
+ {
+ echo third && echo initial
+ } >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log --all-match --grep --grep --author takes intersection' '
+ # grep matches only third
+ # author matches only initial and third
+ git log --all-match --author="A U Thor" --grep=i --grep=r --format=%s >actual &&
+ {
+ echo third
+ } >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'log --author does not search in timestamp' '
+ git log --author="$GIT_AUTHOR_DATE" >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'log --committer does not search in timestamp' '
+ git log --committer="$GIT_COMMITTER_DATE" >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'grep with CE_VALID file' '
+ git update-index --assume-unchanged t/t &&
+ rm t/t &&
+ echo "t/t:test" >expect &&
+ git grep test >actual &&
+ test_cmp expect actual &&
+ git update-index --no-assume-unchanged t/t &&
+ git checkout t/t
+'
+
+cat >expected <<EOF
+hello.c=#include <stdio.h>
+hello.c: return 0;
+EOF
+
+test_expect_success 'grep -p with userdiff' '
+ git config diff.custom.funcname "^#" &&
+ echo "hello.c diff=custom" >.gitattributes &&
+ git grep -p return >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<EOF
+hello.c=int main(int argc, const char **argv)
+hello.c: return 0;
+EOF
+
+test_expect_success 'grep -p' '
+ rm -f .gitattributes &&
+ git grep -p return >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<EOF
+hello.c-#include <stdio.h>
+hello.c-
+hello.c=int main(int argc, const char **argv)
+hello.c-{
+hello.c- printf("Hello world.\n");
+hello.c: return 0;
+EOF
+
+test_expect_success 'grep -p -B5' '
+ git grep -p -B5 return >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<EOF
+hello.c=int main(int argc, const char **argv)
+hello.c-{
+hello.c- printf("Hello world.\n");
+hello.c: return 0;
+hello.c- /* char ?? */
+hello.c-}
+EOF
+
+test_expect_success 'grep -W' '
+ git grep -W return >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<EOF
+hello.c-#include <assert.h>
+hello.c:#include <stdio.h>
+EOF
+
+test_expect_success 'grep -W shows no trailing empty lines' '
+ git grep -W stdio >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'grep -W with userdiff' '
+ test_when_finished "rm -f .gitattributes" &&
+ git config diff.custom.xfuncname "^function .*$" &&
+ echo "hello.ps1 diff=custom" >.gitattributes &&
+ git grep -W echo >function-context-userdiff-actual
+'
+
+test_expect_success ' includes preceding comment' '
+ grep "# Say hello" function-context-userdiff-actual
+'
+
+test_expect_success ' includes function line' '
+ grep "=function hello" function-context-userdiff-actual
+'
+
+test_expect_success ' includes matching line' '
+ grep ": echo" function-context-userdiff-actual
+'
+
+test_expect_success ' includes last line of the function' '
+ grep "} # hello" function-context-userdiff-actual
+'
+
+for threads in $(test_seq 0 10)
+do
+ test_expect_success "grep --threads=$threads & -c grep.threads=$threads" "
+ git grep --threads=$threads . >actual.$threads &&
+ if test $threads -ge 1
+ then
+ test_cmp actual.\$(($threads - 1)) actual.$threads
+ fi &&
+ git -c grep.threads=$threads grep . >actual.$threads &&
+ if test $threads -ge 1
+ then
+ test_cmp actual.\$(($threads - 1)) actual.$threads
+ fi
+ "
+done
+
+test_expect_success !PTHREADS,!FAIL_PREREQS \
+ 'grep --threads=N or pack.threads=N warns when no pthreads' '
+ git grep --threads=2 Hello hello_world 2>err &&
+ grep ^warning: err >warnings &&
+ test_line_count = 1 warnings &&
+ grep -F "no threads support, ignoring --threads" err &&
+ git -c grep.threads=2 grep Hello hello_world 2>err &&
+ grep ^warning: err >warnings &&
+ test_line_count = 1 warnings &&
+ grep -F "no threads support, ignoring grep.threads" err &&
+ git -c grep.threads=2 grep --threads=4 Hello hello_world 2>err &&
+ grep ^warning: err >warnings &&
+ test_line_count = 2 warnings &&
+ grep -F "no threads support, ignoring --threads" err &&
+ grep -F "no threads support, ignoring grep.threads" err &&
+ git -c grep.threads=0 grep --threads=0 Hello hello_world 2>err &&
+ test_line_count = 0 err
+'
+
+test_expect_success 'grep from a subdirectory to search wider area (1)' '
+ mkdir -p s &&
+ (
+ cd s && git grep "x x x" ..
+ )
+'
+
+test_expect_success 'grep from a subdirectory to search wider area (2)' '
+ mkdir -p s &&
+ (
+ cd s &&
+ test_expect_code 1 git grep xxyyzz .. >out &&
+ test_must_be_empty out
+ )
+'
+
+cat >expected <<EOF
+hello.c:int main(int argc, const char **argv)
+EOF
+
+test_expect_success 'grep -Fi' '
+ git grep -Fi "CHAR *" >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'outside of git repository' '
+ rm -fr non &&
+ mkdir -p non/git/sub &&
+ echo hello >non/git/file1 &&
+ echo world >non/git/sub/file2 &&
+ {
+ echo file1:hello &&
+ echo sub/file2:world
+ } >non/expect.full &&
+ echo file2:world >non/expect.sub &&
+ (
+ GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
+ export GIT_CEILING_DIRECTORIES &&
+ cd non/git &&
+ test_must_fail git grep o &&
+ git grep --no-index o >../actual.full &&
+ test_cmp ../expect.full ../actual.full &&
+ cd sub &&
+ test_must_fail git grep o &&
+ git grep --no-index o >../../actual.sub &&
+ test_cmp ../../expect.sub ../../actual.sub
+ ) &&
+
+ echo ".*o*" >non/git/.gitignore &&
+ (
+ GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
+ export GIT_CEILING_DIRECTORIES &&
+ cd non/git &&
+ test_must_fail git grep o &&
+ git grep --no-index --exclude-standard o >../actual.full &&
+ test_cmp ../expect.full ../actual.full &&
+
+ {
+ echo ".gitignore:.*o*" &&
+ cat ../expect.full
+ } >../expect.with.ignored &&
+ git grep --no-index --no-exclude-standard o >../actual.full &&
+ test_cmp ../expect.with.ignored ../actual.full
+ )
+'
+
+test_expect_success 'outside of git repository with fallbackToNoIndex' '
+ rm -fr non &&
+ mkdir -p non/git/sub &&
+ echo hello >non/git/file1 &&
+ echo world >non/git/sub/file2 &&
+ cat <<-\EOF >non/expect.full &&
+ file1:hello
+ sub/file2:world
+ EOF
+ echo file2:world >non/expect.sub &&
+ (
+ GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
+ export GIT_CEILING_DIRECTORIES &&
+ cd non/git &&
+ test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
+ git -c grep.fallbackToNoIndex=true grep o >../actual.full &&
+ test_cmp ../expect.full ../actual.full &&
+ cd sub &&
+ test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
+ git -c grep.fallbackToNoIndex=true grep o >../../actual.sub &&
+ test_cmp ../../expect.sub ../../actual.sub
+ ) &&
+
+ echo ".*o*" >non/git/.gitignore &&
+ (
+ GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
+ export GIT_CEILING_DIRECTORIES &&
+ cd non/git &&
+ test_must_fail git -c grep.fallbackToNoIndex=false grep o &&
+ git -c grep.fallbackToNoIndex=true grep --exclude-standard o >../actual.full &&
+ test_cmp ../expect.full ../actual.full &&
+
+ {
+ echo ".gitignore:.*o*" &&
+ cat ../expect.full
+ } >../expect.with.ignored &&
+ git -c grep.fallbackToNoIndex grep --no-exclude-standard o >../actual.full &&
+ test_cmp ../expect.with.ignored ../actual.full
+ )
+'
+
+test_expect_success 'no repository with path outside $cwd' '
+ test_when_finished rm -fr non &&
+ rm -fr non &&
+ mkdir -p non/git/sub non/tig &&
+ (
+ GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
+ export GIT_CEILING_DIRECTORIES &&
+ cd non/git &&
+ test_expect_code 128 git grep --no-index search .. 2>error &&
+ grep "is outside the directory tree" error
+ ) &&
+ (
+ GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
+ export GIT_CEILING_DIRECTORIES &&
+ cd non/git &&
+ test_expect_code 128 git grep --no-index search ../tig 2>error &&
+ grep "is outside the directory tree" error
+ ) &&
+ (
+ GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
+ export GIT_CEILING_DIRECTORIES &&
+ cd non/git &&
+ test_expect_code 128 git grep --no-index search ../non 2>error &&
+ grep "no such path in the working tree" error
+ )
+'
+
+test_expect_success 'inside git repository but with --no-index' '
+ rm -fr is &&
+ mkdir -p is/git/sub &&
+ echo hello >is/git/file1 &&
+ echo world >is/git/sub/file2 &&
+ echo ".*o*" >is/git/.gitignore &&
+ {
+ echo file1:hello &&
+ echo sub/file2:world
+ } >is/expect.unignored &&
+ {
+ echo ".gitignore:.*o*" &&
+ cat is/expect.unignored
+ } >is/expect.full &&
+ echo file2:world >is/expect.sub &&
+ (
+ cd is/git &&
+ git init &&
+ test_must_fail git grep o >../actual.full &&
+ test_must_be_empty ../actual.full &&
+
+ git grep --untracked o >../actual.unignored &&
+ test_cmp ../expect.unignored ../actual.unignored &&
+
+ git grep --no-index o >../actual.full &&
+ test_cmp ../expect.full ../actual.full &&
+
+ git grep --no-index --exclude-standard o >../actual.unignored &&
+ test_cmp ../expect.unignored ../actual.unignored &&
+
+ cd sub &&
+ test_must_fail git grep o >../../actual.sub &&
+ test_must_be_empty ../../actual.sub &&
+
+ git grep --no-index o >../../actual.sub &&
+ test_cmp ../../expect.sub ../../actual.sub &&
+
+ git grep --untracked o >../../actual.sub &&
+ test_cmp ../../expect.sub ../../actual.sub
+ )
+'
+
+test_expect_success 'grep --no-index descends into repos, but not .git' '
+ rm -fr non &&
+ mkdir -p non/git &&
+ (
+ GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
+ export GIT_CEILING_DIRECTORIES &&
+ cd non/git &&
+
+ echo magic >file &&
+ git init repo &&
+ (
+ cd repo &&
+ echo magic >file &&
+ git add file &&
+ git commit -m foo &&
+ echo magic >.git/file
+ ) &&
+
+ cat >expect <<-\EOF &&
+ file
+ repo/file
+ EOF
+ git grep -l --no-index magic >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'setup double-dash tests' '
+cat >double-dash <<EOF &&
+--
+->
+other
+EOF
+git add double-dash
+'
+
+cat >expected <<EOF
+double-dash:->
+EOF
+test_expect_success 'grep -- pattern' '
+ git grep -- "->" >actual &&
+ test_cmp expected actual
+'
+test_expect_success 'grep -- pattern -- pathspec' '
+ git grep -- "->" -- double-dash >actual &&
+ test_cmp expected actual
+'
+test_expect_success 'grep -e pattern -- path' '
+ git grep -e "->" -- double-dash >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<EOF
+double-dash:--
+EOF
+test_expect_success 'grep -e -- -- path' '
+ git grep -e -- -- double-dash >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'dashdash disambiguates rev as rev' '
+ test_when_finished "rm -f main" &&
+ echo content >main &&
+ echo main:hello.c >expect &&
+ git grep -l o main -- hello.c >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'dashdash disambiguates pathspec as pathspec' '
+ test_when_finished "git rm -f main" &&
+ echo content >main &&
+ git add main &&
+ echo main:content >expect &&
+ git grep o -- main >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'report bogus arg without dashdash' '
+ test_must_fail git grep o does-not-exist
+'
+
+test_expect_success 'report bogus rev with dashdash' '
+ test_must_fail git grep o hello.c --
+'
+
+test_expect_success 'allow non-existent path with dashdash' '
+ # We need a real match so grep exits with success.
+ tree=$(git ls-tree HEAD |
+ sed s/hello.c/not-in-working-tree/ |
+ git mktree) &&
+ git grep o "$tree" -- not-in-working-tree
+'
+
+test_expect_success 'grep --no-index pattern -- path' '
+ rm -fr non &&
+ mkdir -p non/git &&
+ (
+ GIT_CEILING_DIRECTORIES="$(pwd)/non" &&
+ export GIT_CEILING_DIRECTORIES &&
+ cd non/git &&
+ echo hello >hello &&
+ echo goodbye >goodbye &&
+ echo hello:hello >expect &&
+ git grep --no-index o -- hello >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'grep --no-index complains of revs' '
+ test_must_fail git grep --no-index o main -- 2>err &&
+ test_grep "cannot be used with revs" err
+'
+
+test_expect_success 'grep --no-index prefers paths to revs' '
+ test_when_finished "rm -f main" &&
+ echo content >main &&
+ echo main:content >expect &&
+ git grep --no-index o main >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'grep --no-index does not "diagnose" revs' '
+ test_must_fail git grep --no-index o :1:hello.c 2>err &&
+ test_grep ! -i "did you mean" err
+'
+
+cat >expected <<EOF
+hello.c:int main(int argc, const char **argv)
+hello.c: printf("Hello world.\n");
+EOF
+
+test_expect_success PCRE 'grep --perl-regexp pattern' '
+ git grep --perl-regexp "\p{Ps}.*?\p{Pe}" hello.c >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success !FAIL_PREREQS,!PCRE 'grep --perl-regexp pattern errors without PCRE' '
+ test_must_fail git grep --perl-regexp "foo.*bar"
+'
+
+test_expect_success PCRE 'grep -P pattern' '
+ git grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success LIBPCRE2 "grep -P with (*NO_JIT) doesn't error out" '
+ git grep -P "(*NO_JIT)\p{Ps}.*?\p{Pe}" hello.c >actual &&
+ test_cmp expected actual
+
+'
+
+test_expect_success !FAIL_PREREQS,!PCRE 'grep -P pattern errors without PCRE' '
+ test_must_fail git grep -P "foo.*bar"
+'
+
+test_expect_success 'grep pattern with grep.extendedRegexp=true' '
+ test_must_fail git -c grep.extendedregexp=true \
+ grep "\p{Ps}.*?\p{Pe}" hello.c >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success PCRE 'grep -P pattern with grep.extendedRegexp=true' '
+ git -c grep.extendedregexp=true \
+ grep -P "\p{Ps}.*?\p{Pe}" hello.c >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success PCRE 'grep -P -v pattern' '
+ cat >expected <<-\EOF &&
+ ab:a+b*c
+ ab:a+bc
+ EOF
+ git grep -P -v "abc" ab >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success PCRE 'grep -P -i pattern' '
+ cat >expected <<-EOF &&
+ hello.c: printf("Hello world.\n");
+ EOF
+ git grep -P -i "PRINTF\([^\d]+\)" hello.c >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success PCRE 'grep -P -w pattern' '
+ cat >expected <<-\EOF &&
+ hello_world:Hello world
+ hello_world:HeLLo world
+ EOF
+ git grep -P -w "He((?i)ll)o" hello_world >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success PCRE 'grep -P backreferences work (the PCRE NO_AUTO_CAPTURE flag is not set)' '
+ git grep -P -h "(?P<one>.)(?P=one)" hello_world >actual &&
+ test_cmp hello_world actual &&
+ git grep -P -h "(.)\1" hello_world >actual &&
+ test_cmp hello_world actual
+'
+
+test_expect_success 'grep -G invalidpattern properly dies ' '
+ test_must_fail git grep -G "a["
+'
+
+test_expect_success 'grep invalidpattern properly dies with grep.patternType=basic' '
+ test_must_fail git -c grep.patterntype=basic grep "a["
+'
+
+test_expect_success 'grep -E invalidpattern properly dies ' '
+ test_must_fail git grep -E "a["
+'
+
+test_expect_success 'grep invalidpattern properly dies with grep.patternType=extended' '
+ test_must_fail git -c grep.patterntype=extended grep "a["
+'
+
+test_expect_success PCRE 'grep -P invalidpattern properly dies ' '
+ test_must_fail git grep -P "a["
+'
+
+test_expect_success PCRE 'grep invalidpattern properly dies with grep.patternType=perl' '
+ test_must_fail git -c grep.patterntype=perl grep "a["
+'
+
+test_expect_success 'grep -G -E -F pattern' '
+ echo "ab:a+b*c" >expected &&
+ git grep -G -E -F "a+b*c" ab >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'grep pattern with grep.patternType=basic, =extended, =fixed' '
+ echo "ab:a+b*c" >expected &&
+ git \
+ -c grep.patterntype=basic \
+ -c grep.patterntype=extended \
+ -c grep.patterntype=fixed \
+ grep "a+b*c" ab >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'grep -E -F -G pattern' '
+ echo "ab:a+bc" >expected &&
+ git grep -E -F -G "a+b*c" ab >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'grep pattern with grep.patternType=extended, =fixed, =basic' '
+ echo "ab:a+bc" >expected &&
+ git \
+ -c grep.patterntype=extended \
+ -c grep.patterntype=fixed \
+ -c grep.patterntype=basic \
+ grep "a+b*c" ab >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'grep -F -G -E pattern' '
+ echo "ab:abc" >expected &&
+ git grep -F -G -E "a+b*c" ab >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =extended' '
+ echo "ab:abc" >expected &&
+ git \
+ -c grep.patterntype=fixed \
+ -c grep.patterntype=basic \
+ -c grep.patterntype=extended \
+ grep "a+b*c" ab >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'grep -G -F -P -E pattern' '
+ echo "d0:d" >expected &&
+ git grep -G -F -P -E "[\d]" d0 >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'grep pattern with grep.patternType=fixed, =basic, =perl, =extended' '
+ echo "d0:d" >expected &&
+ git \
+ -c grep.patterntype=fixed \
+ -c grep.patterntype=basic \
+ -c grep.patterntype=perl \
+ -c grep.patterntype=extended \
+ grep "[\d]" d0 >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success PCRE 'grep -G -F -E -P pattern' '
+ echo "d0:0" >expected &&
+ git grep -G -F -E -P "[\d]" d0 >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success PCRE 'grep pattern with grep.patternType=fixed, =basic, =extended, =perl' '
+ echo "d0:0" >expected &&
+ git \
+ -c grep.patterntype=fixed \
+ -c grep.patterntype=basic \
+ -c grep.patterntype=extended \
+ -c grep.patterntype=perl \
+ grep "[\d]" d0 >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success PCRE 'grep -P pattern with grep.patternType=fixed' '
+ echo "ab:a+b*c" >expected &&
+ git \
+ -c grep.patterntype=fixed \
+ grep -P "a\x{2b}b\x{2a}c" ab >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'grep -F pattern with grep.patternType=basic' '
+ echo "ab:a+b*c" >expected &&
+ git \
+ -c grep.patterntype=basic \
+ grep -F "*c" ab >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'grep -G pattern with grep.patternType=fixed' '
+ cat >expected <<-\EOF &&
+ ab:a+b*c
+ ab:a+bc
+ EOF
+ git \
+ -c grep.patterntype=fixed \
+ grep -G "a+b" ab >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'grep -E pattern with grep.patternType=fixed' '
+ cat >expected <<-\EOF &&
+ ab:a+b*c
+ ab:a+bc
+ ab:abc
+ EOF
+ git \
+ -c grep.patterntype=fixed \
+ grep -E "a+" ab >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<EOF
+hello.c<RED>:<RESET>int main(int argc, const char **argv)
+hello.c<RED>-<RESET>{
+<RED>--<RESET>
+hello.c<RED>:<RESET> /* char ?? */
+hello.c<RED>-<RESET>}
+<RED>--<RESET>
+hello_world<RED>:<RESET>Hello_world
+hello_world<RED>-<RESET>HeLLo_world
+EOF
+
+test_expect_success 'grep --color, separator' '
+ test_config color.grep.context normal &&
+ test_config color.grep.filename normal &&
+ test_config color.grep.function normal &&
+ test_config color.grep.linenumber normal &&
+ test_config color.grep.match normal &&
+ test_config color.grep.selected normal &&
+ test_config color.grep.separator red &&
+
+ git grep --color=always -A1 -e char -e lo_w hello.c hello_world |
+ test_decode_color >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<EOF
+hello.c:int main(int argc, const char **argv)
+hello.c: /* char ?? */
+
+hello_world:Hello_world
+EOF
+
+test_expect_success 'grep --break' '
+ git grep --break -e char -e lo_w hello.c hello_world >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<EOF
+hello.c:int main(int argc, const char **argv)
+hello.c-{
+--
+hello.c: /* char ?? */
+hello.c-}
+
+hello_world:Hello_world
+hello_world-HeLLo_world
+EOF
+
+test_expect_success 'grep --break with context' '
+ git grep --break -A1 -e char -e lo_w hello.c hello_world >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<EOF
+hello.c
+int main(int argc, const char **argv)
+ /* char ?? */
+hello_world
+Hello_world
+EOF
+
+test_expect_success 'grep --heading' '
+ git grep --heading -e char -e lo_w hello.c hello_world >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<EOF
+<BOLD;GREEN>hello.c<RESET>
+4:int main(int argc, const <BLACK;BYELLOW>char<RESET> **argv)
+8: /* <BLACK;BYELLOW>char<RESET> ?? */
+
+<BOLD;GREEN>hello_world<RESET>
+3:Hel<BLACK;BYELLOW>lo_w<RESET>orld
+EOF
+
+test_expect_success 'mimic ack-grep --group' '
+ test_config color.grep.context normal &&
+ test_config color.grep.filename "bold green" &&
+ test_config color.grep.function normal &&
+ test_config color.grep.linenumber normal &&
+ test_config color.grep.match "black yellow" &&
+ test_config color.grep.selected normal &&
+ test_config color.grep.separator normal &&
+
+ git grep --break --heading -n --color \
+ -e char -e lo_w hello.c hello_world |
+ test_decode_color >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<EOF
+space: line with leading space1
+space: line with leading space2
+space: line with leading space3
+EOF
+
+test_expect_success PCRE 'grep -E "^ "' '
+ git grep -E "^ " space >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success PCRE 'grep -P "^ "' '
+ git grep -P "^ " space >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<EOF
+space-line without leading space1
+space: line <RED>with <RESET>leading space1
+space: line <RED>with <RESET>leading <RED>space2<RESET>
+space: line <RED>with <RESET>leading space3
+space:line without leading <RED>space2<RESET>
+EOF
+
+test_expect_success 'grep --color -e A -e B with context' '
+ test_config color.grep.context normal &&
+ test_config color.grep.filename normal &&
+ test_config color.grep.function normal &&
+ test_config color.grep.linenumber normal &&
+ test_config color.grep.matchContext normal &&
+ test_config color.grep.matchSelected red &&
+ test_config color.grep.selected normal &&
+ test_config color.grep.separator normal &&
+
+ git grep --color=always -C2 -e "with " -e space2 space |
+ test_decode_color >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<EOF
+space-line without leading space1
+space- line with leading space1
+space: line <RED>with <RESET>leading <RED>space2<RESET>
+space- line with leading space3
+space-line without leading space2
+EOF
+
+test_expect_success 'grep --color -e A --and -e B with context' '
+ test_config color.grep.context normal &&
+ test_config color.grep.filename normal &&
+ test_config color.grep.function normal &&
+ test_config color.grep.linenumber normal &&
+ test_config color.grep.matchContext normal &&
+ test_config color.grep.matchSelected red &&
+ test_config color.grep.selected normal &&
+ test_config color.grep.separator normal &&
+
+ git grep --color=always -C2 -e "with " --and -e space2 space |
+ test_decode_color >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<EOF
+space-line without leading space1
+space: line <RED>with <RESET>leading space1
+space- line with leading space2
+space: line <RED>with <RESET>leading space3
+space-line without leading space2
+EOF
+
+test_expect_success 'grep --color -e A --and --not -e B with context' '
+ test_config color.grep.context normal &&
+ test_config color.grep.filename normal &&
+ test_config color.grep.function normal &&
+ test_config color.grep.linenumber normal &&
+ test_config color.grep.matchContext normal &&
+ test_config color.grep.matchSelected red &&
+ test_config color.grep.selected normal &&
+ test_config color.grep.separator normal &&
+
+ git grep --color=always -C2 -e "with " --and --not -e space2 space |
+ test_decode_color >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<EOF
+hello.c-
+hello.c=int main(int argc, const char **argv)
+hello.c-{
+hello.c: pr<RED>int<RESET>f("<RED>Hello<RESET> world.\n");
+hello.c- return 0;
+hello.c- /* char ?? */
+hello.c-}
+EOF
+
+test_expect_success 'grep --color -e A --and -e B -p with context' '
+ test_config color.grep.context normal &&
+ test_config color.grep.filename normal &&
+ test_config color.grep.function normal &&
+ test_config color.grep.linenumber normal &&
+ test_config color.grep.matchContext normal &&
+ test_config color.grep.matchSelected red &&
+ test_config color.grep.selected normal &&
+ test_config color.grep.separator normal &&
+
+ git grep --color=always -p -C3 -e int --and -e Hello --no-index hello.c |
+ test_decode_color >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'grep can find things only in the work tree' '
+ : >work-tree-only &&
+ git add work-tree-only &&
+ test_when_finished "git rm -f work-tree-only" &&
+ echo "find in work tree" >work-tree-only &&
+ git grep --quiet "find in work tree" &&
+ test_must_fail git grep --quiet --cached "find in work tree" &&
+ test_must_fail git grep --quiet "find in work tree" HEAD
+'
+
+test_expect_success 'grep can find things only in the work tree (i-t-a)' '
+ echo "intend to add this" >intend-to-add &&
+ git add -N intend-to-add &&
+ test_when_finished "git rm -f intend-to-add" &&
+ git grep --quiet "intend to add this" &&
+ test_must_fail git grep --quiet --cached "intend to add this" &&
+ test_must_fail git grep --quiet "intend to add this" HEAD
+'
+
+test_expect_success 'grep does not search work tree with assume unchanged' '
+ echo "intend to add this" >intend-to-add &&
+ git add -N intend-to-add &&
+ git update-index --assume-unchanged intend-to-add &&
+ test_when_finished "git rm -f intend-to-add" &&
+ test_must_fail git grep --quiet "intend to add this" &&
+ test_must_fail git grep --quiet --cached "intend to add this" &&
+ test_must_fail git grep --quiet "intend to add this" HEAD
+'
+
+test_expect_success 'grep can find things only in the index' '
+ echo "only in the index" >cache-this &&
+ git add cache-this &&
+ rm cache-this &&
+ test_when_finished "git rm --cached cache-this" &&
+ test_must_fail git grep --quiet "only in the index" &&
+ git grep --quiet --cached "only in the index" &&
+ test_must_fail git grep --quiet "only in the index" HEAD
+'
+
+test_expect_success 'grep does not report i-t-a with -L --cached' '
+ echo "intend to add this" >intend-to-add &&
+ git add -N intend-to-add &&
+ test_when_finished "git rm -f intend-to-add" &&
+ git ls-files | grep -v "^intend-to-add\$" >expected &&
+ git grep -L --cached "nonexistent_string" >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'grep does not report i-t-a and assume unchanged with -L' '
+ echo "intend to add this" >intend-to-add-assume-unchanged &&
+ git add -N intend-to-add-assume-unchanged &&
+ test_when_finished "git rm -f intend-to-add-assume-unchanged" &&
+ git update-index --assume-unchanged intend-to-add-assume-unchanged &&
+ git ls-files | grep -v "^intend-to-add-assume-unchanged\$" >expected &&
+ git grep -L "nonexistent_string" >actual &&
+ test_cmp expected actual
+'
+
+test_done
diff --git a/t/t7811-grep-open.sh b/t/t7811-grep-open.sh
new file mode 100755
index 0000000..fe38d88
--- /dev/null
+++ b/t/t7811-grep-open.sh
@@ -0,0 +1,159 @@
+#!/bin/sh
+
+test_description='git grep --open-files-in-pager
+'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-pager.sh
+unset PAGER GIT_PAGER
+
+test_expect_success 'setup' '
+ test_commit initial grep.h "
+enum grep_pat_token {
+ GREP_PATTERN,
+ GREP_PATTERN_HEAD,
+ GREP_PATTERN_BODY,
+ GREP_AND,
+ GREP_OPEN_PAREN,
+ GREP_CLOSE_PAREN,
+ GREP_NOT,
+ GREP_OR,
+};" &&
+
+ test_commit add-user revision.c "
+ }
+ if (seen_dashdash)
+ read_pathspec_from_stdin(revs, &sb, prune);
+ strbuf_release(&sb);
+}
+
+static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
+{
+ append_grep_pattern(&revs->grep_filter, ptn, \"command line\", 0, what);
+" &&
+
+ mkdir subdir &&
+ test_commit subdir subdir/grep.c "enum grep_pat_token" &&
+
+ test_commit uninteresting unrelated "hello, world" &&
+
+ echo GREP_PATTERN >untracked
+'
+
+test_expect_success SIMPLEPAGER 'git grep -O' '
+ cat >$less <<-\EOF &&
+ #!/bin/sh
+ printf "%s\n" "$@" >pager-args
+ EOF
+ chmod +x $less &&
+ cat >expect.less <<-\EOF &&
+ +/*GREP_PATTERN
+ grep.h
+ EOF
+ echo grep.h >expect.notless &&
+
+ PATH=.:$PATH git grep -O GREP_PATTERN >out &&
+ {
+ test_cmp expect.less pager-args ||
+ test_cmp expect.notless pager-args
+ } &&
+ test_must_be_empty out
+'
+
+test_expect_success 'git grep -O --cached' '
+ test_must_fail git grep --cached -O GREP_PATTERN >out 2>msg &&
+ test_grep open-files-in-pager msg
+'
+
+test_expect_success 'git grep -O --no-index' '
+ rm -f expect.less pager-args out &&
+ cat >expect <<-\EOF &&
+ grep.h
+ untracked
+ EOF
+
+ (
+ GIT_PAGER='\''printf "%s\n" >pager-args'\'' &&
+ export GIT_PAGER &&
+ git grep --no-index -O GREP_PATTERN >out
+ ) &&
+ test_cmp expect pager-args &&
+ test_must_be_empty out
+'
+
+test_expect_success 'setup: fake "less"' '
+ cat >less <<-\EOF &&
+ #!/bin/sh
+ printf "%s\n" "$@" >actual
+ EOF
+ chmod +x less
+'
+
+test_expect_success 'git grep -O jumps to line in less' '
+ cat >expect <<-\EOF &&
+ +/*GREP_PATTERN
+ grep.h
+ EOF
+
+ GIT_PAGER=./less git grep -O GREP_PATTERN >out &&
+ test_cmp expect actual &&
+ test_must_be_empty out &&
+
+ git grep -O./less GREP_PATTERN >out2 &&
+ test_cmp expect actual &&
+ test_must_be_empty out2
+'
+
+test_expect_success 'modified file' '
+ rm -f actual &&
+ cat >expect <<-\EOF &&
+ +/*enum grep_pat_token
+ grep.h
+ revision.c
+ subdir/grep.c
+ unrelated
+ EOF
+
+ test_when_finished "git reset --hard" &&
+ echo "enum grep_pat_token" >unrelated &&
+ GIT_PAGER=./less git grep -F -O "enum grep_pat_token" >out &&
+ test_cmp expect actual &&
+ test_must_be_empty out
+'
+
+test_expect_success 'copes with color settings' '
+ rm -f actual &&
+ echo grep.h >expect &&
+ test_config color.grep always &&
+ test_config color.grep.filename yellow &&
+ test_config color.grep.separator green &&
+ git grep -O'\''printf "%s\n" >actual'\'' GREP_AND &&
+ test_cmp expect actual
+'
+
+test_expect_success 'run from subdir' '
+ rm -f actual &&
+ echo grep.c >expect &&
+
+ (
+ cd subdir &&
+ export GIT_PAGER &&
+ GIT_PAGER='\''printf "%s\n" >../args'\'' &&
+ git grep -O "enum grep_pat_token" >../out &&
+ git grep -O"pwd >../dir; :" "enum grep_pat_token" >../out2
+ ) &&
+ case $(cat dir) in
+ *subdir)
+ : good
+ ;;
+ *)
+ false
+ ;;
+ esac &&
+ test_cmp expect args &&
+ test_must_be_empty out &&
+ test_must_be_empty out2
+'
+
+test_done
diff --git a/t/t7812-grep-icase-non-ascii.sh b/t/t7812-grep-icase-non-ascii.sh
new file mode 100755
index 0000000..31c66b6
--- /dev/null
+++ b/t/t7812-grep-icase-non-ascii.sh
@@ -0,0 +1,153 @@
+#!/bin/sh
+
+test_description='grep icase on non-English locales'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./lib-gettext.sh
+
+doalarm () {
+ perl -e 'alarm shift; exec @ARGV' -- "$@"
+}
+
+test_expect_success GETTEXT_LOCALE 'setup' '
+ test_write_lines "TILRAUN: Halló Heimur!" >file &&
+ git add file &&
+ LC_ALL="$is_IS_locale" &&
+ export LC_ALL
+'
+
+test_expect_success GETTEXT_LOCALE 'setup REGEX_LOCALE prerequisite' '
+ # This "test-tool" invocation is identical...
+ if test-tool regex "HALLÓ" "Halló" ICASE
+ then
+ test_set_prereq REGEX_LOCALE
+ else
+
+ # ... to this one, but this way "test_must_fail" will
+ # tell a segfault or abort() from the regexec() test
+ # itself
+ test_must_fail test-tool regex "HALLÓ" "Halló" ICASE
+ fi
+'
+
+test_expect_success REGEX_LOCALE 'grep literal string, no -F' '
+ git grep -i "TILRAUN: Halló Heimur!" &&
+ git grep -i "TILRAUN: HALLÓ HEIMUR!"
+'
+
+test_expect_success GETTEXT_LOCALE,PCRE 'grep pcre utf-8 icase' '
+ git grep --perl-regexp "TILRAUN: H.lló Heimur!" &&
+ git grep --perl-regexp -i "TILRAUN: H.lló Heimur!" &&
+ git grep --perl-regexp -i "TILRAUN: H.LLÓ HEIMUR!"
+'
+
+test_expect_success GETTEXT_LOCALE,PCRE 'grep pcre utf-8 string with "+"' '
+ test_write_lines "TILRAUN: Hallóó Heimur!" >file2 &&
+ git add file2 &&
+ git grep -l --perl-regexp "TILRAUN: H.lló+ Heimur!" >actual &&
+ echo file >expected &&
+ echo file2 >>expected &&
+ test_cmp expected actual
+'
+
+test_expect_success REGEX_LOCALE 'grep literal string, with -F' '
+ git grep -i -F "TILRAUN: Halló Heimur!" &&
+ git grep -i -F "TILRAUN: HALLÓ HEIMUR!"
+'
+
+test_expect_success REGEX_LOCALE 'grep string with regex, with -F' '
+ test_write_lines "TILRAUN: Halló Heimur [abc]!" >file3 &&
+ git add file3 &&
+ git grep -i -F "TILRAUN: Halló Heimur [abc]!" file3
+'
+
+test_expect_success REGEX_LOCALE 'pickaxe -i on non-ascii' '
+ git commit -m first &&
+ git log --format=%f -i -S"TILRAUN: HALLÓ HEIMUR!" >actual &&
+ echo first >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success GETTEXT_LOCALE,LIBPCRE2 'PCRE v2: setup invalid UTF-8 data' '
+ printf "\\200\\n" >invalid-0x80 &&
+ echo "ævar" >expected &&
+ cat expected >>invalid-0x80 &&
+ git add invalid-0x80 &&
+
+ # Test for PCRE2_MATCH_INVALID_UTF bug
+ # https://bugs.exim.org/show_bug.cgi?id=2642
+ printf "\\345Aæ\\n" >invalid-0xe5 &&
+ git add invalid-0xe5
+'
+
+test_expect_success GETTEXT_LOCALE,LIBPCRE2 'PCRE v2: grep ASCII from invalid UTF-8 data' '
+ git grep -h "var" invalid-0x80 >actual &&
+ test_cmp expected actual &&
+ git grep -h "(*NO_JIT)var" invalid-0x80 >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success GETTEXT_LOCALE,LIBPCRE2 'PCRE v2: grep ASCII from invalid UTF-8 data (PCRE2 bug #2642)' '
+ git grep -h "Aæ" invalid-0xe5 >actual &&
+ test_cmp invalid-0xe5 actual &&
+ git grep -h "(*NO_JIT)Aæ" invalid-0xe5 >actual &&
+ test_cmp invalid-0xe5 actual
+'
+
+test_expect_success GETTEXT_LOCALE,LIBPCRE2 'PCRE v2: grep non-ASCII from invalid UTF-8 data' '
+ git grep -h "æ" invalid-0x80 >actual &&
+ test_cmp expected actual &&
+ git grep -h "(*NO_JIT)æ" invalid-0x80 >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success GETTEXT_LOCALE,LIBPCRE2 'PCRE v2: grep non-ASCII from invalid UTF-8 data (PCRE2 bug #2642)' '
+ git grep -h "Aæ" invalid-0xe5 >actual &&
+ test_cmp invalid-0xe5 actual &&
+ git grep -h "(*NO_JIT)Aæ" invalid-0xe5 >actual &&
+ test_cmp invalid-0xe5 actual
+'
+
+test_lazy_prereq PCRE2_MATCH_INVALID_UTF '
+ test-tool pcre2-config has-PCRE2_MATCH_INVALID_UTF
+'
+
+test_expect_success GETTEXT_LOCALE,LIBPCRE2 'PCRE v2: grep non-ASCII from invalid UTF-8 data with -i' '
+ test_might_fail git grep -hi "Æ" invalid-0x80 >actual &&
+ test_might_fail git grep -hi "(*NO_JIT)Æ" invalid-0x80 >actual
+'
+
+test_expect_success GETTEXT_LOCALE,LIBPCRE2,PCRE2_MATCH_INVALID_UTF 'PCRE v2: grep non-ASCII from invalid UTF-8 data with -i' '
+ git grep -hi "Æ" invalid-0x80 >actual &&
+ test_cmp expected actual &&
+ git grep -hi "(*NO_JIT)Æ" invalid-0x80 >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success GETTEXT_LOCALE,LIBPCRE2,PCRE2_MATCH_INVALID_UTF 'PCRE v2: grep non-ASCII from invalid UTF-8 data with -i (PCRE2 bug #2642)' '
+ git grep -hi "Æ" invalid-0xe5 >actual &&
+ test_cmp invalid-0xe5 actual &&
+ git grep -hi "(*NO_JIT)Æ" invalid-0xe5 >actual &&
+ test_cmp invalid-0xe5 actual &&
+
+ # Only the case of grepping the ASCII part in a way that
+ # relies on -i fails
+ git grep -hi "aÆ" invalid-0xe5 >actual &&
+ test_cmp invalid-0xe5 actual &&
+ git grep -hi "(*NO_JIT)aÆ" invalid-0xe5 >actual &&
+ test_cmp invalid-0xe5 actual
+'
+
+test_expect_success GETTEXT_LOCALE,LIBPCRE2 'PCRE v2: grep non-literal ASCII from UTF-8' '
+ git grep --perl-regexp -h -o -e ll. file >actual &&
+ echo "lló" >expected &&
+ test_cmp expected actual
+'
+
+test_expect_success GETTEXT_LOCALE,LIBPCRE2 'PCRE v2: grep avoid endless loop bug' '
+ echo " Halló" >leading-whitespace &&
+ git add leading-whitespace &&
+ doalarm 1 git grep --perl-regexp "^\s" leading-whitespace
+'
+
+test_done
diff --git a/t/t7813-grep-icase-iso.sh b/t/t7813-grep-icase-iso.sh
new file mode 100755
index 0000000..1227885
--- /dev/null
+++ b/t/t7813-grep-icase-iso.sh
@@ -0,0 +1,20 @@
+#!/bin/sh
+
+test_description='grep icase on non-English locales'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./lib-gettext.sh
+
+test_expect_success GETTEXT_ISO_LOCALE 'setup' '
+ printf "TILRAUN: Halló Heimur!" >file &&
+ git add file &&
+ LC_ALL="$is_IS_iso_locale" &&
+ export LC_ALL
+'
+
+test_expect_success GETTEXT_ISO_LOCALE,PCRE 'grep pcre string' '
+ git grep --perl-regexp -i "TILRAUN: H.lló Heimur!" &&
+ git grep --perl-regexp -i "TILRAUN: H.LLÓ HEIMUR!"
+'
+
+test_done
diff --git a/t/t7814-grep-recurse-submodules.sh b/t/t7814-grep-recurse-submodules.sh
new file mode 100755
index 0000000..167fe66
--- /dev/null
+++ b/t/t7814-grep-recurse-submodules.sh
@@ -0,0 +1,637 @@
+#!/bin/sh
+
+test_description='Test grep recurse-submodules feature
+
+This test verifies the recurse-submodules feature correctly greps across
+submodules.
+'
+
+TEST_CREATE_REPO_NO_TEMPLATE=1
+. ./test-lib.sh
+
+GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB=1
+export GIT_TEST_FATAL_REGISTER_SUBMODULE_ODB
+
+test_expect_success 'setup directory structure and submodule' '
+ echo "(1|2)d(3|4)" >a &&
+ mkdir b &&
+ echo "(3|4)" >b/b &&
+ git add a b &&
+ git commit -m "add a and b" &&
+ test_tick &&
+ git init submodule &&
+ echo "(1|2)d(3|4)" >submodule/a &&
+ git -C submodule add a &&
+ git -C submodule commit -m "add a" &&
+ git submodule add ./submodule &&
+ git commit -m "added submodule" &&
+ test_tick
+'
+
+test_expect_success 'grep correctly finds patterns in a submodule' '
+ cat >expect <<-\EOF &&
+ a:(1|2)d(3|4)
+ b/b:(3|4)
+ submodule/a:(1|2)d(3|4)
+ EOF
+
+ git grep -e "(3|4)" --recurse-submodules >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'grep finds patterns in a submodule via config' '
+ test_config submodule.recurse true &&
+ # expect from previous test
+ git grep -e "(3|4)" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'grep --no-recurse-submodules overrides config' '
+ test_config submodule.recurse true &&
+ cat >expect <<-\EOF &&
+ a:(1|2)d(3|4)
+ b/b:(3|4)
+ EOF
+
+ git grep -e "(3|4)" --no-recurse-submodules >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'grep and basic pathspecs' '
+ cat >expect <<-\EOF &&
+ submodule/a:(1|2)d(3|4)
+ EOF
+
+ git grep -e. --recurse-submodules -- submodule >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'grep and nested submodules' '
+ git init submodule/sub &&
+ echo "(1|2)d(3|4)" >submodule/sub/a &&
+ git -C submodule/sub add a &&
+ git -C submodule/sub commit -m "add a" &&
+ test_tick &&
+ git -C submodule submodule add ./sub &&
+ git -C submodule add sub &&
+ git -C submodule commit -m "added sub" &&
+ test_tick &&
+ git add submodule &&
+ git commit -m "updated submodule" &&
+ test_tick &&
+
+ cat >expect <<-\EOF &&
+ a:(1|2)d(3|4)
+ b/b:(3|4)
+ submodule/a:(1|2)d(3|4)
+ submodule/sub/a:(1|2)d(3|4)
+ EOF
+
+ git grep -e "(3|4)" --recurse-submodules >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'grep and multiple patterns' '
+ cat >expect <<-\EOF &&
+ a:(1|2)d(3|4)
+ submodule/a:(1|2)d(3|4)
+ submodule/sub/a:(1|2)d(3|4)
+ EOF
+
+ git grep -e "(3|4)" --and -e "(1|2)" --recurse-submodules >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'grep and multiple patterns' '
+ cat >expect <<-\EOF &&
+ b/b:(3|4)
+ EOF
+
+ git grep -e "(3|4)" --and --not -e "(1|2)" --recurse-submodules >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'basic grep tree' '
+ cat >expect <<-\EOF &&
+ HEAD:a:(1|2)d(3|4)
+ HEAD:b/b:(3|4)
+ HEAD:submodule/a:(1|2)d(3|4)
+ HEAD:submodule/sub/a:(1|2)d(3|4)
+ EOF
+
+ git grep -e "(3|4)" --recurse-submodules HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'grep tree HEAD^' '
+ cat >expect <<-\EOF &&
+ HEAD^:a:(1|2)d(3|4)
+ HEAD^:b/b:(3|4)
+ HEAD^:submodule/a:(1|2)d(3|4)
+ EOF
+
+ git grep -e "(3|4)" --recurse-submodules HEAD^ >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'grep tree HEAD^^' '
+ cat >expect <<-\EOF &&
+ HEAD^^:a:(1|2)d(3|4)
+ HEAD^^:b/b:(3|4)
+ EOF
+
+ git grep -e "(3|4)" --recurse-submodules HEAD^^ >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'grep tree and pathspecs' '
+ cat >expect <<-\EOF &&
+ HEAD:submodule/a:(1|2)d(3|4)
+ HEAD:submodule/sub/a:(1|2)d(3|4)
+ EOF
+
+ git grep -e "(3|4)" --recurse-submodules HEAD -- submodule >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'grep tree and pathspecs' '
+ cat >expect <<-\EOF &&
+ HEAD:submodule/a:(1|2)d(3|4)
+ HEAD:submodule/sub/a:(1|2)d(3|4)
+ EOF
+
+ git grep -e "(3|4)" --recurse-submodules HEAD -- "submodule*a" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'grep tree and more pathspecs' '
+ cat >expect <<-\EOF &&
+ HEAD:submodule/a:(1|2)d(3|4)
+ EOF
+
+ git grep -e "(3|4)" --recurse-submodules HEAD -- "submodul?/a" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'grep tree and more pathspecs' '
+ cat >expect <<-\EOF &&
+ HEAD:submodule/sub/a:(1|2)d(3|4)
+ EOF
+
+ git grep -e "(3|4)" --recurse-submodules HEAD -- "submodul*/sub/a" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success !MINGW 'grep recurse submodule colon in name' '
+ git init parent &&
+ test_when_finished "rm -rf parent" &&
+ echo "(1|2)d(3|4)" >"parent/fi:le" &&
+ git -C parent add "fi:le" &&
+ git -C parent commit -m "add fi:le" &&
+ test_tick &&
+
+ git init "su:b" &&
+ test_when_finished "rm -rf su:b" &&
+ echo "(1|2)d(3|4)" >"su:b/fi:le" &&
+ git -C "su:b" add "fi:le" &&
+ git -C "su:b" commit -m "add fi:le" &&
+ test_tick &&
+
+ test_config_global protocol.file.allow always &&
+ git -C parent submodule add "../su:b" "su:b" &&
+ git -C parent commit -m "add submodule" &&
+ test_tick &&
+
+ cat >expect <<-\EOF &&
+ fi:le:(1|2)d(3|4)
+ su:b/fi:le:(1|2)d(3|4)
+ EOF
+ git -C parent grep -e "(1|2)d(3|4)" --recurse-submodules >actual &&
+ test_cmp expect actual &&
+
+ cat >expect <<-\EOF &&
+ HEAD:fi:le:(1|2)d(3|4)
+ HEAD:su:b/fi:le:(1|2)d(3|4)
+ EOF
+ git -C parent grep -e "(1|2)d(3|4)" --recurse-submodules HEAD >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'grep history with moved submoules' '
+ git init parent &&
+ test_when_finished "rm -rf parent" &&
+ echo "(1|2)d(3|4)" >parent/file &&
+ git -C parent add file &&
+ git -C parent commit -m "add file" &&
+ test_tick &&
+
+ git init sub &&
+ test_when_finished "rm -rf sub" &&
+ echo "(1|2)d(3|4)" >sub/file &&
+ git -C sub add file &&
+ git -C sub commit -m "add file" &&
+ test_tick &&
+
+ test_config_global protocol.file.allow always &&
+ git -C parent submodule add ../sub dir/sub &&
+ git -C parent commit -m "add submodule" &&
+ test_tick &&
+
+ cat >expect <<-\EOF &&
+ dir/sub/file:(1|2)d(3|4)
+ file:(1|2)d(3|4)
+ EOF
+ git -C parent grep -e "(1|2)d(3|4)" --recurse-submodules >actual &&
+ test_cmp expect actual &&
+
+ git -C parent mv dir/sub sub-moved &&
+ git -C parent commit -m "moved submodule" &&
+ test_tick &&
+
+ cat >expect <<-\EOF &&
+ file:(1|2)d(3|4)
+ sub-moved/file:(1|2)d(3|4)
+ EOF
+ git -C parent grep -e "(1|2)d(3|4)" --recurse-submodules >actual &&
+ test_cmp expect actual &&
+
+ cat >expect <<-\EOF &&
+ HEAD^:dir/sub/file:(1|2)d(3|4)
+ HEAD^:file:(1|2)d(3|4)
+ EOF
+ git -C parent grep -e "(1|2)d(3|4)" --recurse-submodules HEAD^ >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'grep using relative path' '
+ test_when_finished "rm -rf parent sub" &&
+ git init sub &&
+ echo "(1|2)d(3|4)" >sub/file &&
+ git -C sub add file &&
+ git -C sub commit -m "add file" &&
+ test_tick &&
+
+ git init parent &&
+ echo "(1|2)d(3|4)" >parent/file &&
+ git -C parent add file &&
+ mkdir parent/src &&
+ echo "(1|2)d(3|4)" >parent/src/file2 &&
+ git -C parent add src/file2 &&
+ test_config_global protocol.file.allow always &&
+ git -C parent submodule add ../sub &&
+ git -C parent commit -m "add files and submodule" &&
+ test_tick &&
+
+ # From top works
+ cat >expect <<-\EOF &&
+ file:(1|2)d(3|4)
+ src/file2:(1|2)d(3|4)
+ sub/file:(1|2)d(3|4)
+ EOF
+ git -C parent grep --recurse-submodules -e "(1|2)d(3|4)" >actual &&
+ test_cmp expect actual &&
+
+ # Relative path to top
+ cat >expect <<-\EOF &&
+ ../file:(1|2)d(3|4)
+ file2:(1|2)d(3|4)
+ ../sub/file:(1|2)d(3|4)
+ EOF
+ git -C parent/src grep --recurse-submodules -e "(1|2)d(3|4)" -- .. >actual &&
+ test_cmp expect actual &&
+
+ # Relative path to submodule
+ cat >expect <<-\EOF &&
+ ../sub/file:(1|2)d(3|4)
+ EOF
+ git -C parent/src grep --recurse-submodules -e "(1|2)d(3|4)" -- ../sub >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'grep from a subdir' '
+ test_when_finished "rm -rf parent sub" &&
+ git init sub &&
+ echo "(1|2)d(3|4)" >sub/file &&
+ git -C sub add file &&
+ git -C sub commit -m "add file" &&
+ test_tick &&
+
+ git init parent &&
+ mkdir parent/src &&
+ echo "(1|2)d(3|4)" >parent/src/file &&
+ git -C parent add src/file &&
+ test_config_global protocol.file.allow always &&
+ git -C parent submodule add ../sub src/sub &&
+ git -C parent submodule add ../sub sub &&
+ git -C parent commit -m "add files and submodules" &&
+ test_tick &&
+
+ # Verify grep from root works
+ cat >expect <<-\EOF &&
+ src/file:(1|2)d(3|4)
+ src/sub/file:(1|2)d(3|4)
+ sub/file:(1|2)d(3|4)
+ EOF
+ git -C parent grep --recurse-submodules -e "(1|2)d(3|4)" >actual &&
+ test_cmp expect actual &&
+
+ # Verify grep from a subdir works
+ cat >expect <<-\EOF &&
+ file:(1|2)d(3|4)
+ sub/file:(1|2)d(3|4)
+ EOF
+ git -C parent/src grep --recurse-submodules -e "(1|2)d(3|4)" >actual &&
+ test_cmp expect actual
+'
+
+test_incompatible_with_recurse_submodules ()
+{
+ test_expect_success "--recurse-submodules and $1 are incompatible" "
+ test_must_fail git grep -e. --recurse-submodules $1 2>actual &&
+ test_grep 'not supported with --recurse-submodules' actual
+ "
+}
+
+test_incompatible_with_recurse_submodules --untracked
+
+test_expect_success 'grep --recurse-submodules --no-index ignores --recurse-submodules' '
+ git grep --recurse-submodules --no-index -e "^(.|.)[\d]" >actual &&
+ cat >expect <<-\EOF &&
+ a:(1|2)d(3|4)
+ submodule/a:(1|2)d(3|4)
+ submodule/sub/a:(1|2)d(3|4)
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'grep --recurse-submodules should pass the pattern type along' '
+ # Fixed
+ test_must_fail git grep -F --recurse-submodules -e "(.|.)[\d]" &&
+ test_must_fail git -c grep.patternType=fixed grep --recurse-submodules -e "(.|.)[\d]" &&
+
+ # Basic
+ git grep -G --recurse-submodules -e "(.|.)[\d]" >actual &&
+ cat >expect <<-\EOF &&
+ a:(1|2)d(3|4)
+ submodule/a:(1|2)d(3|4)
+ submodule/sub/a:(1|2)d(3|4)
+ EOF
+ test_cmp expect actual &&
+ git -c grep.patternType=basic grep --recurse-submodules -e "(.|.)[\d]" >actual &&
+ test_cmp expect actual &&
+
+ # Extended
+ git grep -E --recurse-submodules -e "(.|.)[\d]" >actual &&
+ cat >expect <<-\EOF &&
+ .gitmodules:[submodule "submodule"]
+ .gitmodules: path = submodule
+ .gitmodules: url = ./submodule
+ a:(1|2)d(3|4)
+ submodule/.gitmodules:[submodule "sub"]
+ submodule/a:(1|2)d(3|4)
+ submodule/sub/a:(1|2)d(3|4)
+ EOF
+ test_cmp expect actual &&
+ git -c grep.patternType=extended grep --recurse-submodules -e "(.|.)[\d]" >actual &&
+ test_cmp expect actual &&
+ git -c grep.extendedRegexp=true grep --recurse-submodules -e "(.|.)[\d]" >actual &&
+ test_cmp expect actual &&
+
+ # Perl
+ if test_have_prereq PCRE
+ then
+ git grep -P --recurse-submodules -e "(.|.)[\d]" >actual &&
+ cat >expect <<-\EOF &&
+ a:(1|2)d(3|4)
+ b/b:(3|4)
+ submodule/a:(1|2)d(3|4)
+ submodule/sub/a:(1|2)d(3|4)
+ EOF
+ test_cmp expect actual &&
+ git -c grep.patternType=perl grep --recurse-submodules -e "(.|.)[\d]" >actual &&
+ test_cmp expect actual
+ fi
+'
+
+test_expect_success 'grep --recurse-submodules with submodules without .gitmodules in the working tree' '
+ test_when_finished "git -C submodule checkout .gitmodules" &&
+ rm submodule/.gitmodules &&
+ git grep --recurse-submodules -e "(.|.)[\d]" >actual &&
+ cat >expect <<-\EOF &&
+ a:(1|2)d(3|4)
+ submodule/a:(1|2)d(3|4)
+ submodule/sub/a:(1|2)d(3|4)
+ EOF
+ test_cmp expect actual
+'
+
+reset_and_clean () {
+ git reset --hard &&
+ git clean -fd &&
+ git submodule foreach --recursive 'git reset --hard' &&
+ git submodule foreach --recursive 'git clean -fd'
+}
+
+test_expect_success 'grep --recurse-submodules without --cached considers worktree modifications' '
+ reset_and_clean &&
+ echo "A modified line in submodule" >>submodule/a &&
+ echo "submodule/a:A modified line in submodule" >expect &&
+ git grep --recurse-submodules "A modified line in submodule" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'grep --recurse-submodules with --cached ignores worktree modifications' '
+ reset_and_clean &&
+ echo "A modified line in submodule" >>submodule/a &&
+ test_must_fail git grep --recurse-submodules --cached "A modified line in submodule" >actual 2>&1 &&
+ test_must_be_empty actual
+'
+
+test_expect_failure 'grep --textconv: superproject .gitattributes does not affect submodules' '
+ reset_and_clean &&
+ test_config_global diff.d2x.textconv "sed -e \"s/d/x/\"" &&
+ echo "a diff=d2x" >.gitattributes &&
+
+ cat >expect <<-\EOF &&
+ a:(1|2)x(3|4)
+ EOF
+ git grep --textconv --recurse-submodules x >actual &&
+ test_cmp expect actual
+'
+
+test_expect_failure 'grep --textconv: superproject .gitattributes (from index) does not affect submodules' '
+ reset_and_clean &&
+ test_config_global diff.d2x.textconv "sed -e \"s/d/x/\"" &&
+ echo "a diff=d2x" >.gitattributes &&
+ git add .gitattributes &&
+ rm .gitattributes &&
+
+ cat >expect <<-\EOF &&
+ a:(1|2)x(3|4)
+ EOF
+ git grep --textconv --recurse-submodules x >actual &&
+ test_cmp expect actual
+'
+
+test_expect_failure 'grep --textconv: superproject .git/info/attributes does not affect submodules' '
+ reset_and_clean &&
+ test_config_global diff.d2x.textconv "sed -e \"s/d/x/\"" &&
+ super_info="$(git rev-parse --git-path info)" &&
+ super_attr="$super_info/attributes" &&
+ test_when_finished "rm -f \"$super_attr\"" &&
+ mkdir "$super_info" &&
+ echo "a diff=d2x" >"$super_attr" &&
+
+ cat >expect <<-\EOF &&
+ a:(1|2)x(3|4)
+ EOF
+ git grep --textconv --recurse-submodules x >actual &&
+ test_cmp expect actual
+'
+
+# Note: what currently prevents this test from passing is not that the
+# .gitattributes file from "./submodule" is being ignored, but that it is being
+# propagated to the nested "./submodule/sub" files.
+#
+test_expect_failure 'grep --textconv correctly reads submodule .gitattributes' '
+ reset_and_clean &&
+ test_config_global diff.d2x.textconv "sed -e \"s/d/x/\"" &&
+ echo "a diff=d2x" >submodule/.gitattributes &&
+
+ cat >expect <<-\EOF &&
+ submodule/a:(1|2)x(3|4)
+ EOF
+ git grep --textconv --recurse-submodules x >actual &&
+ test_cmp expect actual
+'
+
+test_expect_failure 'grep --textconv correctly reads submodule .gitattributes (from index)' '
+ reset_and_clean &&
+ test_config_global diff.d2x.textconv "sed -e \"s/d/x/\"" &&
+ echo "a diff=d2x" >submodule/.gitattributes &&
+ git -C submodule add .gitattributes &&
+ rm submodule/.gitattributes &&
+
+ cat >expect <<-\EOF &&
+ submodule/a:(1|2)x(3|4)
+ EOF
+ git grep --textconv --recurse-submodules x >actual &&
+ test_cmp expect actual
+'
+
+test_expect_failure 'grep --textconv correctly reads submodule .git/info/attributes' '
+ reset_and_clean &&
+ test_config_global diff.d2x.textconv "sed -e \"s/d/x/\"" &&
+
+ submodule_info="$(git -C submodule rev-parse --path-format=absolute --git-path info)" &&
+ submodule_attr="$submodule_info/attributes" &&
+ test_when_finished "rm -f \"$submodule_attr\"" &&
+ echo "a diff=d2x" >"$submodule_attr" &&
+
+ cat >expect <<-\EOF &&
+ submodule/a:(1|2)x(3|4)
+ EOF
+ git grep --textconv --recurse-submodules x >actual &&
+ test_cmp expect actual
+'
+
+test_expect_failure 'grep saves textconv cache in the appropriate repository' '
+ reset_and_clean &&
+ test_config_global diff.d2x_cached.textconv "sed -e \"s/d/x/\"" &&
+ test_config_global diff.d2x_cached.cachetextconv true &&
+ echo "a diff=d2x_cached" >submodule/.gitattributes &&
+
+ # We only read/write to the textconv cache when grepping from an OID,
+ # as the working tree file might have modifications.
+ git grep --textconv --cached --recurse-submodules x &&
+
+ super_textconv_cache="$(git rev-parse --git-path refs/notes/textconv/d2x_cached)" &&
+ sub_textconv_cache="$(git -C submodule rev-parse \
+ --path-format=absolute --git-path refs/notes/textconv/d2x_cached)" &&
+ test_path_is_missing "$super_textconv_cache" &&
+ test_path_is_file "$sub_textconv_cache"
+'
+
+test_expect_success 'grep partially-cloned submodule' '
+ # Set up clean superproject and submodule for partial cloning.
+ test_config_global protocol.file.allow always &&
+ git init super &&
+ git init super/sub &&
+ (
+ cd super &&
+ test_commit --no-tag "Add file in superproject" \
+ super-file "Some content for super-file" &&
+ test_commit -C sub --no-tag "Add file in submodule" \
+ sub-file "Some content for sub-file" &&
+ git submodule add ./sub &&
+ git commit -m "Add other as submodule sub" &&
+ test_tick &&
+ test_commit -C sub --no-tag --append "Update file in submodule" \
+ sub-file "Some more content for sub-file" &&
+ git add sub &&
+ git commit -m "Update submodule" &&
+ test_tick &&
+ git config --local uploadpack.allowfilter 1 &&
+ git config --local uploadpack.allowanysha1inwant 1 &&
+ git -C sub config --local uploadpack.allowfilter 1 &&
+ git -C sub config --local uploadpack.allowanysha1inwant 1
+ ) &&
+ # Clone the superproject & submodule, then make sure we can lazy-fetch submodule objects.
+ git clone --filter=blob:none --also-filter-submodules \
+ --recurse-submodules "file://$(pwd)/super" partial &&
+ (
+ cd partial &&
+ cat >expect <<-\EOF &&
+ HEAD^:sub/sub-file:Some content for sub-file
+ HEAD^:super-file:Some content for super-file
+ EOF
+
+ GIT_TRACE2_EVENT="$(pwd)/trace2.log" git grep -e content \
+ --recurse-submodules HEAD^ >actual &&
+ test_cmp expect actual &&
+ # Verify that we actually fetched data from the promisor remote:
+ grep \"category\":\"promisor\",\"key\":\"fetch_count\",\"value\":\"1\" trace2.log
+ )
+'
+
+test_expect_success 'check scope of core.useReplaceRefs' '
+ git init base &&
+ git init base/sub &&
+
+ echo A >base/a &&
+ echo B >base/b &&
+ echo C >base/sub/c &&
+ echo D >base/sub/d &&
+
+ git -C base/sub add c d &&
+ git -C base/sub commit -m "Add files" &&
+
+ git -C base submodule add ./sub &&
+ git -C base add a b sub &&
+ git -C base commit -m "Add files and submodule" &&
+
+ A=$(git -C base rev-parse HEAD:a) &&
+ B=$(git -C base rev-parse HEAD:b) &&
+ C=$(git -C base/sub rev-parse HEAD:c) &&
+ D=$(git -C base/sub rev-parse HEAD:d) &&
+
+ git -C base replace $A $B &&
+ git -C base/sub replace $C $D &&
+
+ test_must_fail git -C base grep --cached --recurse-submodules A &&
+ test_must_fail git -C base grep --cached --recurse-submodules C &&
+
+ git -C base config core.useReplaceRefs false &&
+ git -C base grep --recurse-submodules A &&
+ test_must_fail git -C base grep --cached --recurse-submodules C &&
+
+ git -C base/sub config core.useReplaceRefs false &&
+ git -C base grep --cached --recurse-submodules A &&
+ git -C base grep --cached --recurse-submodules C &&
+
+ git -C base config --unset core.useReplaceRefs &&
+ test_must_fail git -C base grep --cached --recurse-submodules A &&
+ git -C base grep --cached --recurse-submodules C
+'
+
+test_done
diff --git a/t/t7815-grep-binary.sh b/t/t7815-grep-binary.sh
new file mode 100755
index 0000000..ac87128
--- /dev/null
+++ b/t/t7815-grep-binary.sh
@@ -0,0 +1,149 @@
+#!/bin/sh
+
+test_description='git grep in binary files'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' "
+ echo 'binaryQfileQm[*]cQ*æQð' | q_to_nul >a &&
+ git add a &&
+ git commit -m.
+"
+
+test_expect_success 'git grep ina a' '
+ echo Binary file a matches >expect &&
+ git grep ina a >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git grep -ah ina a' '
+ git grep -ah ina a >actual &&
+ test_cmp a actual
+'
+
+test_expect_success 'git grep -I ina a' '
+ test_must_fail git grep -I ina a >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'git grep -c ina a' '
+ echo a:1 >expect &&
+ git grep -c ina a >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git grep -l ina a' '
+ echo a >expect &&
+ git grep -l ina a >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git grep -L bar a' '
+ echo a >expect &&
+ git grep -L bar a >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'git grep -q ina a' '
+ git grep -q ina a >actual &&
+ test_must_be_empty actual
+'
+
+test_expect_success 'git grep -F ile a' '
+ git grep -F ile a
+'
+
+test_expect_success 'git grep -Fi iLE a' '
+ git grep -Fi iLE a
+'
+
+# This test actually passes on platforms where regexec() supports the
+# flag REG_STARTEND.
+test_expect_success 'git grep ile a' '
+ git grep ile a
+'
+
+test_expect_failure 'git grep .fi a' '
+ git grep .fi a
+'
+
+test_expect_success 'grep respects binary diff attribute' '
+ echo text >t &&
+ git add t &&
+ echo t:text >expect &&
+ git grep text t >actual &&
+ test_cmp expect actual &&
+ echo "t -diff" >.gitattributes &&
+ echo "Binary file t matches" >expect &&
+ git grep text t >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'grep --cached respects binary diff attribute' '
+ git grep --cached text t >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'grep --cached respects binary diff attribute (2)' '
+ git add .gitattributes &&
+ rm .gitattributes &&
+ git grep --cached text t >actual &&
+ test_when_finished "git rm --cached .gitattributes" &&
+ test_when_finished "git checkout .gitattributes" &&
+ test_cmp expect actual
+'
+
+test_expect_success 'grep revision respects binary diff attribute' '
+ git commit -m new &&
+ echo "Binary file HEAD:t matches" >expect &&
+ git grep text HEAD -- t >actual &&
+ test_when_finished "git reset HEAD^" &&
+ test_cmp expect actual
+'
+
+test_expect_success 'grep respects not-binary diff attribute' '
+ echo binQary | q_to_nul >b &&
+ git add b &&
+ echo "Binary file b matches" >expect &&
+ git grep bin b >actual &&
+ test_cmp expect actual &&
+ echo "b diff" >.gitattributes &&
+ echo "b:binQary" >expect &&
+ git grep bin b >actual.raw &&
+ nul_to_q <actual.raw >actual &&
+ test_cmp expect actual
+'
+
+cat >nul_to_q_textconv <<'EOF'
+#!/bin/sh
+"$PERL_PATH" -pe 'y/\000/Q/' < "$1"
+EOF
+chmod +x nul_to_q_textconv
+
+test_expect_success 'setup textconv filters' '
+ echo a diff=foo >.gitattributes &&
+ git config diff.foo.textconv "\"$(pwd)\""/nul_to_q_textconv
+'
+
+test_expect_success 'grep does not honor textconv' '
+ test_must_fail git grep Qfile
+'
+
+test_expect_success 'grep --textconv honors textconv' '
+ echo "a:binaryQfileQm[*]cQ*æQð" >expect &&
+ git grep --textconv Qfile >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'grep --no-textconv does not honor textconv' '
+ test_must_fail git grep --no-textconv Qfile
+'
+
+test_expect_success 'grep --textconv blob honors textconv' '
+ echo "HEAD:a:binaryQfileQm[*]cQ*æQð" >expect &&
+ git grep --textconv Qfile HEAD:a >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t7816-grep-binary-pattern.sh b/t/t7816-grep-binary-pattern.sh
new file mode 100755
index 0000000..4353be5
--- /dev/null
+++ b/t/t7816-grep-binary-pattern.sh
@@ -0,0 +1,128 @@
+#!/bin/sh
+
+test_description='git grep with a binary pattern files'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./lib-gettext.sh
+
+nul_match_internal () {
+ matches=$1
+ prereqs=$2
+ lc_all=$3
+ extra_flags=$4
+ flags=$5
+ pattern=$6
+ pattern_human=$(echo "$pattern" | sed 's/Q/<NUL>/g')
+
+ if test "$matches" = 1
+ then
+ test_expect_success $prereqs "LC_ALL='$lc_all' git grep $extra_flags -f f $flags '$pattern_human' a" "
+ printf '$pattern' | q_to_nul >f &&
+ LC_ALL='$lc_all' git grep $extra_flags -f f $flags a
+ "
+ elif test "$matches" = 0
+ then
+ test_expect_success $prereqs "LC_ALL='$lc_all' git grep $extra_flags -f f $flags '$pattern_human' a" "
+ >stderr &&
+ printf '$pattern' | q_to_nul >f &&
+ test_must_fail env LC_ALL=\"$lc_all\" git grep $extra_flags -f f $flags a 2>stderr &&
+ test_grep ! 'This is only supported with -P under PCRE v2' stderr
+ "
+ elif test "$matches" = P
+ then
+ test_expect_success $prereqs "error, PCRE v2 only: LC_ALL='$lc_all' git grep -f f $flags '$pattern_human' a" "
+ >stderr &&
+ printf '$pattern' | q_to_nul >f &&
+ test_must_fail env LC_ALL=\"$lc_all\" git grep -f f $flags a 2>stderr &&
+ test_grep 'This is only supported with -P under PCRE v2' stderr
+ "
+ else
+ test_expect_success "PANIC: Test framework error. Unknown matches value $matches" 'false'
+ fi
+}
+
+nul_match () {
+ matches=$1
+ matches_pcre2=$2
+ matches_pcre2_locale=$3
+ flags=$4
+ pattern=$5
+ pattern_human=$(echo "$pattern" | sed 's/Q/<NUL>/g')
+
+ nul_match_internal "$matches" "" "C" "" "$flags" "$pattern"
+ nul_match_internal "$matches_pcre2" "LIBPCRE2" "C" "-P" "$flags" "$pattern"
+ nul_match_internal "$matches_pcre2_locale" "LIBPCRE2,GETTEXT_LOCALE" "$is_IS_locale" "-P" "$flags" "$pattern"
+}
+
+test_expect_success 'setup' "
+ echo 'binaryQfileQm[*]cQ*æQð' | q_to_nul >a &&
+ git add a &&
+ git commit -m.
+"
+
+# Simple fixed-string matching
+nul_match P P P '-F' 'yQf'
+nul_match P P P '-F' 'yQx'
+nul_match P P P '-Fi' 'YQf'
+nul_match P P P '-Fi' 'YQx'
+nul_match P P 1 '' 'yQf'
+nul_match P P 0 '' 'yQx'
+nul_match P P 1 '' 'æQð'
+nul_match P P P '-F' 'eQm[*]c'
+nul_match P P P '-Fi' 'EQM[*]C'
+
+# Regex patterns that would match but shouldn't with -F
+nul_match P P P '-F' 'yQ[f]'
+nul_match P P P '-F' '[y]Qf'
+nul_match P P P '-Fi' 'YQ[F]'
+nul_match P P P '-Fi' '[Y]QF'
+nul_match P P P '-F' 'æQ[ð]'
+nul_match P P P '-F' '[æ]Qð'
+
+# Matching pattern and subject case with -i
+nul_match P 1 1 '-i' '[æ]Qð'
+
+# ...PCRE v2 only matches non-ASCII with -i casefolding under UTF-8
+# semantics
+nul_match P P P '-Fi' 'ÆQ[Ã]'
+nul_match P 0 1 '-i' 'ÆQ[Ã]'
+nul_match P 0 1 '-i' '[Æ]QÃ'
+nul_match P 0 1 '-i' '[Æ]Qð'
+nul_match P 0 1 '-i' 'ÆQÃ'
+
+# \0 in regexes can only work with -P & PCRE v2
+nul_match P P 1 '' 'yQ[f]'
+nul_match P P 1 '' '[y]Qf'
+nul_match P P 1 '-i' 'YQ[F]'
+nul_match P P 1 '-i' '[Y]Qf'
+nul_match P P 1 '' 'æQ[ð]'
+nul_match P P 1 '' '[æ]Qð'
+nul_match P P 1 '-i' 'ÆQ[Ã]'
+nul_match P P 1 '' 'eQm.*cQ'
+nul_match P P 1 '-i' 'EQM.*cQ'
+nul_match P P 0 '' 'eQm[*]c'
+nul_match P P 0 '-i' 'EQM[*]C'
+
+# Assert that we're using REG_STARTEND and the pattern doesn't match
+# just because it's cut off at the first \0.
+nul_match P P 0 '-i' 'NOMATCHQð'
+nul_match P P 0 '-i' '[Æ]QNOMATCH'
+nul_match P P 0 '-i' '[æ]QNOMATCH'
+
+# Ensure that the matcher doesn't regress to something that stops at
+# \0
+nul_match P P P '-F' 'yQ[f]'
+nul_match P P P '-Fi' 'YQ[F]'
+nul_match P P 0 '' 'yQNOMATCH'
+nul_match P P 0 '' 'QNOMATCH'
+nul_match P P 0 '-i' 'YQNOMATCH'
+nul_match P P 0 '-i' 'QNOMATCH'
+nul_match P P P '-F' 'æQ[ð]'
+nul_match P P P '-Fi' 'ÆQ[Ã]'
+nul_match P P 1 '-i' 'ÆQ[Ã]'
+nul_match P P 0 '' 'yQNÓMATCH'
+nul_match P P 0 '' 'QNÓMATCH'
+nul_match P P 0 '-i' 'YQNÓMATCH'
+nul_match P P 0 '-i' 'QNÓMATCH'
+
+test_done
diff --git a/t/t7817-grep-sparse-checkout.sh b/t/t7817-grep-sparse-checkout.sh
new file mode 100755
index 0000000..eb59564
--- /dev/null
+++ b/t/t7817-grep-sparse-checkout.sh
@@ -0,0 +1,181 @@
+#!/bin/sh
+
+test_description='grep in sparse checkout
+
+This test creates a repo with the following structure:
+
+.
+|-- a
+|-- b
+|-- dir
+| `-- c
+|-- sub
+| |-- A
+| | `-- a
+| `-- B
+| `-- b
+`-- sub2
+ `-- a
+
+Where the outer repository has non-cone mode sparsity patterns, sub is a
+submodule with cone mode sparsity patterns and sub2 is a submodule that is
+excluded by the superproject sparsity patterns. The resulting sparse checkout
+should leave the following structure in the working tree:
+
+.
+|-- a
+|-- sub
+| `-- B
+| `-- b
+`-- sub2
+ `-- a
+
+But note that sub2 should have the SKIP_WORKTREE bit set.
+'
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ echo "text" >a &&
+ echo "text" >b &&
+ mkdir dir &&
+ echo "text" >dir/c &&
+
+ git init sub &&
+ (
+ cd sub &&
+ mkdir A B &&
+ echo "text" >A/a &&
+ echo "text" >B/b &&
+ git add A B &&
+ git commit -m sub &&
+ git sparse-checkout init --cone &&
+ git sparse-checkout set B
+ ) &&
+
+ git init sub2 &&
+ (
+ cd sub2 &&
+ echo "text" >a &&
+ git add a &&
+ git commit -m sub2
+ ) &&
+
+ git submodule add ./sub &&
+ git submodule add ./sub2 &&
+ git add a b dir &&
+ git commit -m super &&
+ git sparse-checkout init --no-cone &&
+ git sparse-checkout set "/*" "!b" "!/*/" "sub" &&
+
+ git tag -am tag-to-commit tag-to-commit HEAD &&
+ tree=$(git rev-parse HEAD^{tree}) &&
+ git tag -am tag-to-tree tag-to-tree $tree &&
+
+ test_path_is_missing b &&
+ test_path_is_missing dir &&
+ test_path_is_missing sub/A &&
+ test_path_is_file a &&
+ test_path_is_file sub/B/b &&
+ test_path_is_file sub2/a &&
+ git branch -m main
+'
+
+# The test below covers a special case: the sparsity patterns exclude '/b' and
+# sparse checkout is enabled, but the path exists in the working tree (e.g.
+# manually created after `git sparse-checkout init`). Although b is marked
+# as SKIP_WORKTREE, git grep should notice it IS present in the worktree and
+# report it.
+test_expect_success 'working tree grep honors sparse checkout' '
+ cat >expect <<-EOF &&
+ a:text
+ b:new-text
+ EOF
+ test_when_finished "rm -f b" &&
+ echo "new-text" >b &&
+ git grep "text" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'grep searches unmerged file despite not matching sparsity patterns' '
+ cat >expect <<-EOF &&
+ b:modified-b-in-branchX
+ b:modified-b-in-branchY
+ EOF
+ test_when_finished "test_might_fail git merge --abort && \
+ git checkout main && git sparse-checkout init" &&
+
+ git sparse-checkout disable &&
+ git checkout -b branchY main &&
+ test_commit modified-b-in-branchY b &&
+ git checkout -b branchX main &&
+ test_commit modified-b-in-branchX b &&
+
+ git sparse-checkout init &&
+ test_path_is_missing b &&
+ test_must_fail git merge branchY &&
+ git grep "modified-b" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'grep --cached searches entries with the SKIP_WORKTREE bit' '
+ cat >expect <<-EOF &&
+ a:text
+ b:text
+ dir/c:text
+ EOF
+ git grep --cached "text" >actual &&
+ test_cmp expect actual
+'
+
+# Note that sub2/ is present in the worktree but it is excluded by the sparsity
+# patterns. We also explicitly mark it as SKIP_WORKTREE in case it got cleared
+# by previous git commands. Thus sub2 starts as SKIP_WORKTREE but since it is
+# present in the working tree, grep should recurse into it.
+test_expect_success 'grep --recurse-submodules honors sparse checkout in submodule' '
+ cat >expect <<-EOF &&
+ a:text
+ sub/B/b:text
+ sub2/a:text
+ EOF
+ git update-index --skip-worktree sub2 &&
+ git grep --recurse-submodules "text" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'grep --recurse-submodules --cached searches entries with the SKIP_WORKTREE bit' '
+ cat >expect <<-EOF &&
+ a:text
+ b:text
+ dir/c:text
+ sub/A/a:text
+ sub/B/b:text
+ sub2/a:text
+ EOF
+ git grep --recurse-submodules --cached "text" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'working tree grep does not search the index with CE_VALID and SKIP_WORKTREE' '
+ cat >expect <<-EOF &&
+ a:text
+ EOF
+ test_when_finished "git update-index --no-assume-unchanged b" &&
+ git update-index --assume-unchanged b &&
+ git grep text >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'grep --cached searches index entries with both CE_VALID and SKIP_WORKTREE' '
+ cat >expect <<-EOF &&
+ a:text
+ b:text
+ dir/c:text
+ EOF
+ test_when_finished "git update-index --no-assume-unchanged b" &&
+ git update-index --assume-unchanged b &&
+ git grep --cached text >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh
new file mode 100755
index 0000000..935df6a
--- /dev/null
+++ b/t/t7900-maintenance.sh
@@ -0,0 +1,865 @@
+#!/bin/sh
+
+test_description='git maintenance builtin'
+
+. ./test-lib.sh
+
+GIT_TEST_COMMIT_GRAPH=0
+GIT_TEST_MULTI_PACK_INDEX=0
+
+test_lazy_prereq XMLLINT '
+ xmllint --version
+'
+
+test_xmllint () {
+ if test_have_prereq XMLLINT
+ then
+ xmllint --noout "$@"
+ else
+ true
+ fi
+}
+
+test_lazy_prereq SYSTEMD_ANALYZE '
+ systemd-analyze verify /lib/systemd/system/basic.target
+'
+
+test_systemd_analyze_verify () {
+ if test_have_prereq SYSTEMD_ANALYZE
+ then
+ systemd-analyze verify "$@"
+ fi
+}
+
+test_expect_success 'help text' '
+ test_expect_code 129 git maintenance -h >actual &&
+ test_grep "usage: git maintenance <subcommand>" actual &&
+ test_expect_code 129 git maintenance barf 2>err &&
+ test_grep "unknown subcommand: \`barf'\''" err &&
+ test_grep "usage: git maintenance" err &&
+ test_expect_code 129 git maintenance 2>err &&
+ test_grep "error: need a subcommand" err &&
+ test_grep "usage: git maintenance" err
+'
+
+test_expect_success 'run [--auto|--quiet]' '
+ GIT_TRACE2_EVENT="$(pwd)/run-no-auto.txt" \
+ git maintenance run 2>/dev/null &&
+ GIT_TRACE2_EVENT="$(pwd)/run-auto.txt" \
+ git maintenance run --auto 2>/dev/null &&
+ GIT_TRACE2_EVENT="$(pwd)/run-no-quiet.txt" \
+ git maintenance run --no-quiet 2>/dev/null &&
+ test_subcommand git gc --quiet <run-no-auto.txt &&
+ test_subcommand ! git gc --auto --quiet <run-auto.txt &&
+ test_subcommand git gc --no-quiet <run-no-quiet.txt
+'
+
+test_expect_success 'maintenance.auto config option' '
+ GIT_TRACE2_EVENT="$(pwd)/default" git commit --quiet --allow-empty -m 1 &&
+ test_subcommand git maintenance run --auto --quiet <default &&
+ GIT_TRACE2_EVENT="$(pwd)/true" \
+ git -c maintenance.auto=true \
+ commit --quiet --allow-empty -m 2 &&
+ test_subcommand git maintenance run --auto --quiet <true &&
+ GIT_TRACE2_EVENT="$(pwd)/false" \
+ git -c maintenance.auto=false \
+ commit --quiet --allow-empty -m 3 &&
+ test_subcommand ! git maintenance run --auto --quiet <false
+'
+
+test_expect_success 'maintenance.<task>.enabled' '
+ git config maintenance.gc.enabled false &&
+ git config maintenance.commit-graph.enabled true &&
+ GIT_TRACE2_EVENT="$(pwd)/run-config.txt" git maintenance run 2>err &&
+ test_subcommand ! git gc --quiet <run-config.txt &&
+ test_subcommand git commit-graph write --split --reachable --no-progress <run-config.txt
+'
+
+test_expect_success 'run --task=<task>' '
+ GIT_TRACE2_EVENT="$(pwd)/run-commit-graph.txt" \
+ git maintenance run --task=commit-graph 2>/dev/null &&
+ GIT_TRACE2_EVENT="$(pwd)/run-gc.txt" \
+ git maintenance run --task=gc 2>/dev/null &&
+ GIT_TRACE2_EVENT="$(pwd)/run-commit-graph.txt" \
+ git maintenance run --task=commit-graph 2>/dev/null &&
+ GIT_TRACE2_EVENT="$(pwd)/run-both.txt" \
+ git maintenance run --task=commit-graph --task=gc 2>/dev/null &&
+ test_subcommand ! git gc --quiet <run-commit-graph.txt &&
+ test_subcommand git gc --quiet <run-gc.txt &&
+ test_subcommand git gc --quiet <run-both.txt &&
+ test_subcommand git commit-graph write --split --reachable --no-progress <run-commit-graph.txt &&
+ test_subcommand ! git commit-graph write --split --reachable --no-progress <run-gc.txt &&
+ test_subcommand git commit-graph write --split --reachable --no-progress <run-both.txt
+'
+
+test_expect_success 'core.commitGraph=false prevents write process' '
+ GIT_TRACE2_EVENT="$(pwd)/no-commit-graph.txt" \
+ git -c core.commitGraph=false maintenance run \
+ --task=commit-graph 2>/dev/null &&
+ test_subcommand ! git commit-graph write --split --reachable --no-progress \
+ <no-commit-graph.txt
+'
+
+test_expect_success 'commit-graph auto condition' '
+ COMMAND="maintenance run --task=commit-graph --auto --quiet" &&
+
+ GIT_TRACE2_EVENT="$(pwd)/cg-no.txt" \
+ git -c maintenance.commit-graph.auto=1 $COMMAND &&
+ GIT_TRACE2_EVENT="$(pwd)/cg-negative-means-yes.txt" \
+ git -c maintenance.commit-graph.auto="-1" $COMMAND &&
+
+ test_commit first &&
+
+ GIT_TRACE2_EVENT="$(pwd)/cg-zero-means-no.txt" \
+ git -c maintenance.commit-graph.auto=0 $COMMAND &&
+ GIT_TRACE2_EVENT="$(pwd)/cg-one-satisfied.txt" \
+ git -c maintenance.commit-graph.auto=1 $COMMAND &&
+
+ git commit --allow-empty -m "second" &&
+ git commit --allow-empty -m "third" &&
+
+ GIT_TRACE2_EVENT="$(pwd)/cg-two-satisfied.txt" \
+ git -c maintenance.commit-graph.auto=2 $COMMAND &&
+
+ COMMIT_GRAPH_WRITE="git commit-graph write --split --reachable --no-progress" &&
+ test_subcommand ! $COMMIT_GRAPH_WRITE <cg-no.txt &&
+ test_subcommand $COMMIT_GRAPH_WRITE <cg-negative-means-yes.txt &&
+ test_subcommand ! $COMMIT_GRAPH_WRITE <cg-zero-means-no.txt &&
+ test_subcommand $COMMIT_GRAPH_WRITE <cg-one-satisfied.txt &&
+ test_subcommand $COMMIT_GRAPH_WRITE <cg-two-satisfied.txt
+'
+
+test_expect_success 'run --task=bogus' '
+ test_must_fail git maintenance run --task=bogus 2>err &&
+ test_grep "is not a valid task" err
+'
+
+test_expect_success 'run --task duplicate' '
+ test_must_fail git maintenance run --task=gc --task=gc 2>err &&
+ test_grep "cannot be selected multiple times" err
+'
+
+test_expect_success 'run --task=prefetch with no remotes' '
+ git maintenance run --task=prefetch 2>err &&
+ test_must_be_empty err
+'
+
+test_expect_success 'prefetch multiple remotes' '
+ git clone . clone1 &&
+ git clone . clone2 &&
+ git remote add remote1 "file://$(pwd)/clone1" &&
+ git remote add remote2 "file://$(pwd)/clone2" &&
+ git -C clone1 switch -c one &&
+ git -C clone2 switch -c two &&
+ test_commit -C clone1 one &&
+ test_commit -C clone2 two &&
+ GIT_TRACE2_EVENT="$(pwd)/run-prefetch.txt" git maintenance run --task=prefetch 2>/dev/null &&
+ fetchargs="--prefetch --prune --no-tags --no-write-fetch-head --recurse-submodules=no --quiet" &&
+ test_subcommand git fetch remote1 $fetchargs <run-prefetch.txt &&
+ test_subcommand git fetch remote2 $fetchargs <run-prefetch.txt &&
+ test_path_is_missing .git/refs/remotes &&
+ git log prefetch/remotes/remote1/one &&
+ git log prefetch/remotes/remote2/two &&
+ git fetch --all &&
+ test_cmp_rev refs/remotes/remote1/one refs/prefetch/remotes/remote1/one &&
+ test_cmp_rev refs/remotes/remote2/two refs/prefetch/remotes/remote2/two &&
+
+ git log --oneline --decorate --all >log &&
+ ! grep "prefetch" log &&
+
+ test_when_finished git config --unset remote.remote1.skipFetchAll &&
+ git config remote.remote1.skipFetchAll true &&
+ GIT_TRACE2_EVENT="$(pwd)/skip-remote1.txt" git maintenance run --task=prefetch 2>/dev/null &&
+ test_subcommand ! git fetch remote1 $fetchargs <skip-remote1.txt &&
+ test_subcommand git fetch remote2 $fetchargs <skip-remote1.txt
+'
+
+test_expect_success 'loose-objects task' '
+ # Repack everything so we know the state of the object dir
+ git repack -adk &&
+
+ # Hack to stop maintenance from running during "git commit"
+ echo in use >.git/objects/maintenance.lock &&
+
+ # Assuming that "git commit" creates at least one loose object
+ test_commit create-loose-object &&
+ rm .git/objects/maintenance.lock &&
+
+ ls .git/objects >obj-dir-before &&
+ test_file_not_empty obj-dir-before &&
+ ls .git/objects/pack/*.pack >packs-before &&
+ test_line_count = 1 packs-before &&
+
+ # The first run creates a pack-file
+ # but does not delete loose objects.
+ git maintenance run --task=loose-objects &&
+ ls .git/objects >obj-dir-between &&
+ test_cmp obj-dir-before obj-dir-between &&
+ ls .git/objects/pack/*.pack >packs-between &&
+ test_line_count = 2 packs-between &&
+ ls .git/objects/pack/loose-*.pack >loose-packs &&
+ test_line_count = 1 loose-packs &&
+
+ # The second run deletes loose objects
+ # but does not create a pack-file.
+ git maintenance run --task=loose-objects &&
+ ls .git/objects >obj-dir-after &&
+ cat >expect <<-\EOF &&
+ info
+ pack
+ EOF
+ test_cmp expect obj-dir-after &&
+ ls .git/objects/pack/*.pack >packs-after &&
+ test_cmp packs-between packs-after
+'
+
+test_expect_success 'maintenance.loose-objects.auto' '
+ git repack -adk &&
+ GIT_TRACE2_EVENT="$(pwd)/trace-lo1.txt" \
+ git -c maintenance.loose-objects.auto=1 maintenance \
+ run --auto --task=loose-objects 2>/dev/null &&
+ test_subcommand ! git prune-packed --quiet <trace-lo1.txt &&
+ printf data-A | git hash-object -t blob --stdin -w &&
+ GIT_TRACE2_EVENT="$(pwd)/trace-loA" \
+ git -c maintenance.loose-objects.auto=2 \
+ maintenance run --auto --task=loose-objects 2>/dev/null &&
+ test_subcommand ! git prune-packed --quiet <trace-loA &&
+ printf data-B | git hash-object -t blob --stdin -w &&
+ GIT_TRACE2_EVENT="$(pwd)/trace-loB" \
+ git -c maintenance.loose-objects.auto=2 \
+ maintenance run --auto --task=loose-objects 2>/dev/null &&
+ test_subcommand git prune-packed --quiet <trace-loB &&
+ GIT_TRACE2_EVENT="$(pwd)/trace-loC" \
+ git -c maintenance.loose-objects.auto=2 \
+ maintenance run --auto --task=loose-objects 2>/dev/null &&
+ test_subcommand git prune-packed --quiet <trace-loC
+'
+
+test_expect_success 'incremental-repack task' '
+ packDir=.git/objects/pack &&
+ for i in $(test_seq 1 5)
+ do
+ test_commit $i || return 1
+ done &&
+
+ # Create three disjoint pack-files with size BIG, small, small.
+ echo HEAD~2 | git pack-objects --revs $packDir/test-1 &&
+ test_tick &&
+ git pack-objects --revs $packDir/test-2 <<-\EOF &&
+ HEAD~1
+ ^HEAD~2
+ EOF
+ test_tick &&
+ git pack-objects --revs $packDir/test-3 <<-\EOF &&
+ HEAD
+ ^HEAD~1
+ EOF
+
+ # Delete refs that have not been repacked in these packs.
+ git for-each-ref --format="delete %(refname)" \
+ refs/prefetch refs/tags refs/remotes >refs &&
+ git update-ref --stdin <refs &&
+
+ # Replace the object directory with this pack layout.
+ rm -f $packDir/pack-* &&
+ rm -f $packDir/loose-* &&
+ ls $packDir/*.pack >packs-before &&
+ test_line_count = 3 packs-before &&
+
+ # make sure we do not have any broken refs that were
+ # missed in the deletion above
+ git for-each-ref &&
+
+ # the job repacks the two into a new pack, but does not
+ # delete the old ones.
+ git maintenance run --task=incremental-repack &&
+ ls $packDir/*.pack >packs-between &&
+ test_line_count = 4 packs-between &&
+
+ # the job deletes the two old packs, and does not write
+ # a new one because the batch size is not high enough to
+ # pack the largest pack-file.
+ git maintenance run --task=incremental-repack &&
+ ls .git/objects/pack/*.pack >packs-after &&
+ test_line_count = 2 packs-after
+'
+
+test_expect_success EXPENSIVE 'incremental-repack 2g limit' '
+ test_config core.compression 0 &&
+
+ for i in $(test_seq 1 5)
+ do
+ test-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||
+ return 1
+ done &&
+ git add big &&
+ git commit -qm "Add big file (1)" &&
+
+ # ensure any possible loose objects are in a pack-file
+ git maintenance run --task=loose-objects &&
+
+ rm big &&
+ for i in $(test_seq 6 10)
+ do
+ test-tool genrandom foo$i $((512 * 1024 * 1024 + 1)) >>big ||
+ return 1
+ done &&
+ git add big &&
+ git commit -qm "Add big file (2)" &&
+
+ # ensure any possible loose objects are in a pack-file
+ git maintenance run --task=loose-objects &&
+
+ # Now run the incremental-repack task and check the batch-size
+ GIT_TRACE2_EVENT="$(pwd)/run-2g.txt" git maintenance run \
+ --task=incremental-repack 2>/dev/null &&
+ test_subcommand git multi-pack-index repack \
+ --no-progress --batch-size=2147483647 <run-2g.txt
+'
+
+run_incremental_repack_and_verify () {
+ test_commit A &&
+ git repack -adk &&
+ git multi-pack-index write &&
+ GIT_TRACE2_EVENT="$(pwd)/midx-init.txt" git \
+ -c maintenance.incremental-repack.auto=1 \
+ maintenance run --auto --task=incremental-repack 2>/dev/null &&
+ test_subcommand ! git multi-pack-index write --no-progress <midx-init.txt &&
+ test_commit B &&
+ git pack-objects --revs .git/objects/pack/pack <<-\EOF &&
+ HEAD
+ ^HEAD~1
+ EOF
+ GIT_TRACE2_EVENT=$(pwd)/trace-A git \
+ -c maintenance.incremental-repack.auto=2 \
+ maintenance run --auto --task=incremental-repack 2>/dev/null &&
+ test_subcommand ! git multi-pack-index write --no-progress <trace-A &&
+ test_commit C &&
+ git pack-objects --revs .git/objects/pack/pack <<-\EOF &&
+ HEAD
+ ^HEAD~1
+ EOF
+ GIT_TRACE2_EVENT=$(pwd)/trace-B git \
+ -c maintenance.incremental-repack.auto=2 \
+ maintenance run --auto --task=incremental-repack 2>/dev/null &&
+ test_subcommand git multi-pack-index write --no-progress <trace-B
+}
+
+test_expect_success 'maintenance.incremental-repack.auto' '
+ rm -rf incremental-repack-true &&
+ git init incremental-repack-true &&
+ (
+ cd incremental-repack-true &&
+ git config core.multiPackIndex true &&
+ run_incremental_repack_and_verify
+ )
+'
+
+test_expect_success 'maintenance.incremental-repack.auto (when config is unset)' '
+ rm -rf incremental-repack-unset &&
+ git init incremental-repack-unset &&
+ (
+ cd incremental-repack-unset &&
+ test_unconfig core.multiPackIndex &&
+ run_incremental_repack_and_verify
+ )
+'
+
+test_expect_success 'pack-refs task' '
+ for n in $(test_seq 1 5)
+ do
+ git branch -f to-pack/$n HEAD || return 1
+ done &&
+ GIT_TRACE2_EVENT="$(pwd)/pack-refs.txt" \
+ git maintenance run --task=pack-refs &&
+ test_subcommand git pack-refs --all --prune <pack-refs.txt
+'
+
+test_expect_success '--auto and --schedule incompatible' '
+ test_must_fail git maintenance run --auto --schedule=daily 2>err &&
+ test_grep "at most one" err
+'
+
+test_expect_success 'invalid --schedule value' '
+ test_must_fail git maintenance run --schedule=annually 2>err &&
+ test_grep "unrecognized --schedule" err
+'
+
+test_expect_success '--schedule inheritance weekly -> daily -> hourly' '
+ git config maintenance.loose-objects.enabled true &&
+ git config maintenance.loose-objects.schedule hourly &&
+ git config maintenance.commit-graph.enabled true &&
+ git config maintenance.commit-graph.schedule daily &&
+ git config maintenance.incremental-repack.enabled true &&
+ git config maintenance.incremental-repack.schedule weekly &&
+
+ GIT_TRACE2_EVENT="$(pwd)/hourly.txt" \
+ git maintenance run --schedule=hourly 2>/dev/null &&
+ test_subcommand git prune-packed --quiet <hourly.txt &&
+ test_subcommand ! git commit-graph write --split --reachable \
+ --no-progress <hourly.txt &&
+ test_subcommand ! git multi-pack-index write --no-progress <hourly.txt &&
+
+ GIT_TRACE2_EVENT="$(pwd)/daily.txt" \
+ git maintenance run --schedule=daily 2>/dev/null &&
+ test_subcommand git prune-packed --quiet <daily.txt &&
+ test_subcommand git commit-graph write --split --reachable \
+ --no-progress <daily.txt &&
+ test_subcommand ! git multi-pack-index write --no-progress <daily.txt &&
+
+ GIT_TRACE2_EVENT="$(pwd)/weekly.txt" \
+ git maintenance run --schedule=weekly 2>/dev/null &&
+ test_subcommand git prune-packed --quiet <weekly.txt &&
+ test_subcommand git commit-graph write --split --reachable \
+ --no-progress <weekly.txt &&
+ test_subcommand git multi-pack-index write --no-progress <weekly.txt
+'
+
+test_expect_success 'maintenance.strategy inheritance' '
+ for task in commit-graph loose-objects incremental-repack
+ do
+ git config --unset maintenance.$task.schedule || return 1
+ done &&
+
+ test_when_finished git config --unset maintenance.strategy &&
+ git config maintenance.strategy incremental &&
+
+ GIT_TRACE2_EVENT="$(pwd)/incremental-hourly.txt" \
+ git maintenance run --schedule=hourly --quiet &&
+ GIT_TRACE2_EVENT="$(pwd)/incremental-daily.txt" \
+ git maintenance run --schedule=daily --quiet &&
+ GIT_TRACE2_EVENT="$(pwd)/incremental-weekly.txt" \
+ git maintenance run --schedule=weekly --quiet &&
+
+ test_subcommand git commit-graph write --split --reachable \
+ --no-progress <incremental-hourly.txt &&
+ test_subcommand ! git prune-packed --quiet <incremental-hourly.txt &&
+ test_subcommand ! git multi-pack-index write --no-progress \
+ <incremental-hourly.txt &&
+ test_subcommand ! git pack-refs --all --prune \
+ <incremental-hourly.txt &&
+
+ test_subcommand git commit-graph write --split --reachable \
+ --no-progress <incremental-daily.txt &&
+ test_subcommand git prune-packed --quiet <incremental-daily.txt &&
+ test_subcommand git multi-pack-index write --no-progress \
+ <incremental-daily.txt &&
+ test_subcommand ! git pack-refs --all --prune \
+ <incremental-daily.txt &&
+
+ test_subcommand git commit-graph write --split --reachable \
+ --no-progress <incremental-weekly.txt &&
+ test_subcommand git prune-packed --quiet <incremental-weekly.txt &&
+ test_subcommand git multi-pack-index write --no-progress \
+ <incremental-weekly.txt &&
+ test_subcommand git pack-refs --all --prune \
+ <incremental-weekly.txt &&
+
+ # Modify defaults
+ git config maintenance.commit-graph.schedule daily &&
+ git config maintenance.loose-objects.schedule hourly &&
+ git config maintenance.incremental-repack.enabled false &&
+
+ GIT_TRACE2_EVENT="$(pwd)/modified-hourly.txt" \
+ git maintenance run --schedule=hourly --quiet &&
+ GIT_TRACE2_EVENT="$(pwd)/modified-daily.txt" \
+ git maintenance run --schedule=daily --quiet &&
+
+ test_subcommand ! git commit-graph write --split --reachable \
+ --no-progress <modified-hourly.txt &&
+ test_subcommand git prune-packed --quiet <modified-hourly.txt &&
+ test_subcommand ! git multi-pack-index write --no-progress \
+ <modified-hourly.txt &&
+
+ test_subcommand git commit-graph write --split --reachable \
+ --no-progress <modified-daily.txt &&
+ test_subcommand git prune-packed --quiet <modified-daily.txt &&
+ test_subcommand ! git multi-pack-index write --no-progress \
+ <modified-daily.txt
+'
+
+test_expect_success 'register and unregister' '
+ test_when_finished git config --global --unset-all maintenance.repo &&
+
+ test_must_fail git maintenance unregister 2>err &&
+ grep "is not registered" err &&
+ git maintenance unregister --force &&
+
+ git config --global --add maintenance.repo /existing1 &&
+ git config --global --add maintenance.repo /existing2 &&
+ git config --global --get-all maintenance.repo >before &&
+
+ git maintenance register &&
+ test_cmp_config false maintenance.auto &&
+ git config --global --get-all maintenance.repo >between &&
+ cp before expect &&
+ pwd >>expect &&
+ test_cmp expect between &&
+
+ git maintenance unregister &&
+ git config --global --get-all maintenance.repo >actual &&
+ test_cmp before actual &&
+
+ git config --file ./other --add maintenance.repo /existing1 &&
+ git config --file ./other --add maintenance.repo /existing2 &&
+ git config --file ./other --get-all maintenance.repo >before &&
+
+ git maintenance register --config-file ./other &&
+ test_cmp_config false maintenance.auto &&
+ git config --file ./other --get-all maintenance.repo >between &&
+ cp before expect &&
+ pwd >>expect &&
+ test_cmp expect between &&
+
+ git maintenance unregister --config-file ./other &&
+ git config --file ./other --get-all maintenance.repo >actual &&
+ test_cmp before actual &&
+
+ test_must_fail git maintenance unregister 2>err &&
+ grep "is not registered" err &&
+ git maintenance unregister --force &&
+
+ test_must_fail git maintenance unregister --config-file ./other 2>err &&
+ grep "is not registered" err &&
+ git maintenance unregister --config-file ./other --force
+'
+
+test_expect_success 'register with no value for maintenance.repo' '
+ cp .git/config .git/config.orig &&
+ test_when_finished mv .git/config.orig .git/config &&
+
+ cat >>.git/config <<-\EOF &&
+ [maintenance]
+ repo
+ EOF
+ cat >expect <<-\EOF &&
+ error: missing value for '\''maintenance.repo'\''
+ EOF
+ git maintenance register 2>actual &&
+ test_cmp expect actual &&
+ git config maintenance.repo
+'
+
+test_expect_success 'unregister with no value for maintenance.repo' '
+ cp .git/config .git/config.orig &&
+ test_when_finished mv .git/config.orig .git/config &&
+
+ cat >>.git/config <<-\EOF &&
+ [maintenance]
+ repo
+ EOF
+ cat >expect <<-\EOF &&
+ error: missing value for '\''maintenance.repo'\''
+ EOF
+ test_expect_code 128 git maintenance unregister 2>actual.raw &&
+ grep ^error actual.raw >actual &&
+ test_cmp expect actual &&
+ git config maintenance.repo &&
+
+ git maintenance unregister --force 2>actual.raw &&
+ grep ^error actual.raw >actual &&
+ test_cmp expect actual &&
+ git config maintenance.repo
+'
+
+test_expect_success !MINGW 'register and unregister with regex metacharacters' '
+ META="a+b*c" &&
+ git init "$META" &&
+ git -C "$META" maintenance register &&
+ git config --get-all --show-origin maintenance.repo &&
+ git config --get-all --global --fixed-value \
+ maintenance.repo "$(pwd)/$META" &&
+ git -C "$META" maintenance unregister &&
+ test_must_fail git config --get-all --global --fixed-value \
+ maintenance.repo "$(pwd)/$META"
+'
+
+test_expect_success 'start --scheduler=<scheduler>' '
+ test_expect_code 129 git maintenance start --scheduler=foo 2>err &&
+ test_grep "unrecognized --scheduler argument" err &&
+
+ test_expect_code 129 git maintenance start --no-scheduler 2>err &&
+ test_grep "unknown option" err &&
+
+ test_expect_code 128 \
+ env GIT_TEST_MAINT_SCHEDULER="launchctl:true,schtasks:true" \
+ git maintenance start --scheduler=crontab 2>err &&
+ test_grep "fatal: crontab scheduler is not available" err
+'
+
+test_expect_success 'start from empty cron table' '
+ GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance start --scheduler=crontab &&
+
+ # start registers the repo
+ git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
+
+ grep "for-each-repo --config=maintenance.repo maintenance run --schedule=daily" cron.txt &&
+ grep "for-each-repo --config=maintenance.repo maintenance run --schedule=hourly" cron.txt &&
+ grep "for-each-repo --config=maintenance.repo maintenance run --schedule=weekly" cron.txt
+'
+
+test_expect_success 'stop from existing schedule' '
+ GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance stop &&
+
+ # stop does not unregister the repo
+ git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
+
+ # Operation is idempotent
+ GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance stop &&
+ test_must_be_empty cron.txt
+'
+
+test_expect_success 'start preserves existing schedule' '
+ echo "Important information!" >cron.txt &&
+ GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance start --scheduler=crontab &&
+ grep "Important information!" cron.txt
+'
+
+test_expect_success 'magic markers are correct' '
+ grep "GIT MAINTENANCE SCHEDULE" cron.txt >actual &&
+ cat >expect <<-\EOF &&
+ # BEGIN GIT MAINTENANCE SCHEDULE
+ # END GIT MAINTENANCE SCHEDULE
+ EOF
+ test_cmp actual expect
+'
+
+test_expect_success 'stop preserves surrounding schedule' '
+ echo "Crucial information!" >>cron.txt &&
+ GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt" git maintenance stop &&
+ grep "Important information!" cron.txt &&
+ grep "Crucial information!" cron.txt
+'
+
+test_expect_success 'start and stop macOS maintenance' '
+ # ensure $HOME can be compared against hook arguments on all platforms
+ pfx=$(cd "$HOME" && pwd) &&
+
+ write_script print-args <<-\EOF &&
+ echo $* | sed "s:gui/[0-9][0-9]*:gui/[UID]:" >>args
+ EOF
+
+ rm -f args &&
+ GIT_TEST_MAINT_SCHEDULER=launchctl:./print-args git maintenance start --scheduler=launchctl &&
+
+ # start registers the repo
+ git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
+
+ ls "$HOME/Library/LaunchAgents" >actual &&
+ cat >expect <<-\EOF &&
+ org.git-scm.git.daily.plist
+ org.git-scm.git.hourly.plist
+ org.git-scm.git.weekly.plist
+ EOF
+ test_cmp expect actual &&
+
+ rm -f expect &&
+ for frequency in hourly daily weekly
+ do
+ PLIST="$pfx/Library/LaunchAgents/org.git-scm.git.$frequency.plist" &&
+ test_xmllint "$PLIST" &&
+ grep schedule=$frequency "$PLIST" &&
+ echo "bootout gui/[UID] $PLIST" >>expect &&
+ echo "bootstrap gui/[UID] $PLIST" >>expect || return 1
+ done &&
+ test_cmp expect args &&
+
+ rm -f args &&
+ GIT_TEST_MAINT_SCHEDULER=launchctl:./print-args git maintenance stop &&
+
+ # stop does not unregister the repo
+ git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
+
+ printf "bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \
+ hourly daily weekly >expect &&
+ test_cmp expect args &&
+ ls "$HOME/Library/LaunchAgents" >actual &&
+ test_line_count = 0 actual
+'
+
+test_expect_success 'use launchctl list to prevent extra work' '
+ # ensure we are registered
+ GIT_TEST_MAINT_SCHEDULER=launchctl:./print-args git maintenance start --scheduler=launchctl &&
+
+ # do it again on a fresh args file
+ rm -f args &&
+ GIT_TEST_MAINT_SCHEDULER=launchctl:./print-args git maintenance start --scheduler=launchctl &&
+
+ ls "$HOME/Library/LaunchAgents" >actual &&
+ cat >expect <<-\EOF &&
+ list org.git-scm.git.hourly
+ list org.git-scm.git.daily
+ list org.git-scm.git.weekly
+ EOF
+ test_cmp expect args
+'
+
+test_expect_success 'start and stop Windows maintenance' '
+ write_script print-args <<-\EOF &&
+ echo $* >>args
+ while test $# -gt 0
+ do
+ case "$1" in
+ /xml) shift; xmlfile=$1; break ;;
+ *) shift ;;
+ esac
+ done
+ test -z "$xmlfile" || cp "$xmlfile" "$xmlfile.xml"
+ EOF
+
+ rm -f args &&
+ GIT_TEST_MAINT_SCHEDULER="schtasks:./print-args" git maintenance start --scheduler=schtasks &&
+
+ # start registers the repo
+ git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
+
+ for frequency in hourly daily weekly
+ do
+ grep "/create /tn Git Maintenance ($frequency) /f /xml" args &&
+ file=$(ls .git/schedule_${frequency}*.xml) &&
+ test_xmllint "$file" || return 1
+ done &&
+
+ rm -f args &&
+ GIT_TEST_MAINT_SCHEDULER="schtasks:./print-args" git maintenance stop &&
+
+ # stop does not unregister the repo
+ git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
+
+ printf "/delete /tn Git Maintenance (%s) /f\n" \
+ hourly daily weekly >expect &&
+ test_cmp expect args
+'
+
+test_expect_success 'start and stop Linux/systemd maintenance' '
+ write_script print-args <<-\EOF &&
+ printf "%s\n" "$*" >>args
+ EOF
+
+ XDG_CONFIG_HOME="$PWD" &&
+ export XDG_CONFIG_HOME &&
+ rm -f args &&
+ GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args" git maintenance start --scheduler=systemd-timer &&
+
+ # start registers the repo
+ git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
+
+ for schedule in hourly daily weekly
+ do
+ test_path_is_file "systemd/user/git-maintenance@$schedule.timer" || return 1
+ done &&
+ test_path_is_file "systemd/user/git-maintenance@.service" &&
+
+ test_systemd_analyze_verify "systemd/user/git-maintenance@hourly.service" &&
+ test_systemd_analyze_verify "systemd/user/git-maintenance@daily.service" &&
+ test_systemd_analyze_verify "systemd/user/git-maintenance@weekly.service" &&
+
+ printf -- "--user enable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
+ test_cmp expect args &&
+
+ rm -f args &&
+ GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args" git maintenance stop &&
+
+ # stop does not unregister the repo
+ git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
+
+ for schedule in hourly daily weekly
+ do
+ test_path_is_missing "systemd/user/git-maintenance@$schedule.timer" || return 1
+ done &&
+ test_path_is_missing "systemd/user/git-maintenance@.service" &&
+
+ printf -- "--user disable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
+ test_cmp expect args
+'
+
+test_expect_success 'start and stop when several schedulers are available' '
+ write_script print-args <<-\EOF &&
+ printf "%s\n" "$*" | sed "s:gui/[0-9][0-9]*:gui/[UID]:; s:\(schtasks /create .* /xml\).*:\1:;" >>args
+ EOF
+
+ rm -f args &&
+ GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args systemctl,launchctl:./print-args launchctl,schtasks:./print-args schtasks" git maintenance start --scheduler=systemd-timer &&
+ printf "launchctl bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \
+ hourly daily weekly >expect &&
+ printf "schtasks /delete /tn Git Maintenance (%s) /f\n" \
+ hourly daily weekly >>expect &&
+ printf -- "systemctl --user enable --now git-maintenance@%s.timer\n" hourly daily weekly >>expect &&
+ test_cmp expect args &&
+
+ rm -f args &&
+ GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args systemctl,launchctl:./print-args launchctl,schtasks:./print-args schtasks" git maintenance start --scheduler=launchctl &&
+ printf -- "systemctl --user disable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
+ printf "schtasks /delete /tn Git Maintenance (%s) /f\n" \
+ hourly daily weekly >>expect &&
+ for frequency in hourly daily weekly
+ do
+ PLIST="$pfx/Library/LaunchAgents/org.git-scm.git.$frequency.plist" &&
+ echo "launchctl bootout gui/[UID] $PLIST" >>expect &&
+ echo "launchctl bootstrap gui/[UID] $PLIST" >>expect || return 1
+ done &&
+ test_cmp expect args &&
+
+ rm -f args &&
+ GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args systemctl,launchctl:./print-args launchctl,schtasks:./print-args schtasks" git maintenance start --scheduler=schtasks &&
+ printf -- "systemctl --user disable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
+ printf "launchctl bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \
+ hourly daily weekly >>expect &&
+ printf "schtasks /create /tn Git Maintenance (%s) /f /xml\n" \
+ hourly daily weekly >>expect &&
+ test_cmp expect args &&
+
+ rm -f args &&
+ GIT_TEST_MAINT_SCHEDULER="systemctl:./print-args systemctl,launchctl:./print-args launchctl,schtasks:./print-args schtasks" git maintenance stop &&
+ printf -- "systemctl --user disable --now git-maintenance@%s.timer\n" hourly daily weekly >expect &&
+ printf "launchctl bootout gui/[UID] $pfx/Library/LaunchAgents/org.git-scm.git.%s.plist\n" \
+ hourly daily weekly >>expect &&
+ printf "schtasks /delete /tn Git Maintenance (%s) /f\n" \
+ hourly daily weekly >>expect &&
+ test_cmp expect args
+'
+
+test_expect_success 'register preserves existing strategy' '
+ git config maintenance.strategy none &&
+ git maintenance register &&
+ test_config maintenance.strategy none &&
+ git config --unset maintenance.strategy &&
+ git maintenance register &&
+ test_config maintenance.strategy incremental
+'
+
+test_expect_success 'fails when running outside of a repository' '
+ nongit test_must_fail git maintenance run &&
+ nongit test_must_fail git maintenance stop &&
+ nongit test_must_fail git maintenance start &&
+ nongit test_must_fail git maintenance register &&
+ nongit test_must_fail git maintenance unregister
+'
+
+test_expect_success 'register and unregister bare repo' '
+ test_when_finished "git config --global --unset-all maintenance.repo || :" &&
+ test_might_fail git config --global --unset-all maintenance.repo &&
+ git init --bare barerepo &&
+ (
+ cd barerepo &&
+ git maintenance register &&
+ git config --get --global --fixed-value maintenance.repo "$(pwd)" &&
+ git maintenance unregister &&
+ test_must_fail git config --global --get-all maintenance.repo
+ )
+'
+
+test_expect_success 'failed schedule prevents config change' '
+ git init --bare failcase &&
+
+ for scheduler in crontab launchctl schtasks systemctl
+ do
+ GIT_TEST_MAINT_SCHEDULER="$scheduler:false" &&
+ export GIT_TEST_MAINT_SCHEDULER &&
+ test_must_fail \
+ git -C failcase maintenance start &&
+ test_must_fail git -C failcase config maintenance.auto || return 1
+ done
+'
+
+test_done
diff --git a/t/t8001-annotate.sh b/t/t8001-annotate.sh
new file mode 100755
index 0000000..d7167f5
--- /dev/null
+++ b/t/t8001-annotate.sh
@@ -0,0 +1,20 @@
+#!/bin/sh
+
+test_description='git annotate'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_CREATE_REPO_NO_TEMPLATE=1
+. ./test-lib.sh
+
+PROG='git annotate'
+. "$TEST_DIRECTORY"/annotate-tests.sh
+
+test_expect_success 'annotate old revision' '
+ git annotate file main >actual &&
+ awk "{ print \$3; }" <actual >authors &&
+ test 2 = $(grep A <authors | wc -l) &&
+ test 2 = $(grep B <authors | wc -l)
+'
+
+test_done
diff --git a/t/t8002-blame.sh b/t/t8002-blame.sh
new file mode 100755
index 0000000..0147de3
--- /dev/null
+++ b/t/t8002-blame.sh
@@ -0,0 +1,144 @@
+#!/bin/sh
+
+test_description='git blame'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_CREATE_REPO_NO_TEMPLATE=1
+. ./test-lib.sh
+
+PROG='git blame -c'
+. "$TEST_DIRECTORY"/annotate-tests.sh
+
+test_expect_success 'setup' '
+ hexsz=$(test_oid hexsz)
+'
+
+test_expect_success 'blame untracked file in empty repo' '
+ >untracked &&
+ test_must_fail git blame untracked
+'
+
+PROG='git blame -c -e'
+test_expect_success 'blame --show-email' '
+ check_count \
+ "<A@test.git>" 1 \
+ "<B@test.git>" 1 \
+ "<B1@test.git>" 1 \
+ "<B2@test.git>" 1 \
+ "<author@example.com>" 1 \
+ "<C@test.git>" 1 \
+ "<D@test.git>" 1 \
+ "<E at test dot git>" 1
+'
+
+test_expect_success 'setup showEmail tests' '
+ echo "bin: test number 1" >one &&
+ git add one &&
+ GIT_AUTHOR_NAME=name1 \
+ GIT_AUTHOR_EMAIL=email1@test.git \
+ git commit -m First --date="2010-01-01 01:00:00" &&
+ cat >expected_n <<-\EOF &&
+ (name1 2010-01-01 01:00:00 +0000 1) bin: test number 1
+ EOF
+ cat >expected_e <<-\EOF
+ (<email1@test.git> 2010-01-01 01:00:00 +0000 1) bin: test number 1
+ EOF
+'
+
+find_blame () {
+ sed -e 's/^[^(]*//'
+}
+
+test_expect_success 'blame with no options and no config' '
+ git blame one >blame &&
+ find_blame <blame >result &&
+ test_cmp expected_n result
+'
+
+test_expect_success 'blame with showemail options' '
+ git blame --show-email one >blame1 &&
+ find_blame <blame1 >result &&
+ test_cmp expected_e result &&
+ git blame -e one >blame2 &&
+ find_blame <blame2 >result &&
+ test_cmp expected_e result &&
+ git blame --no-show-email one >blame3 &&
+ find_blame <blame3 >result &&
+ test_cmp expected_n result
+'
+
+test_expect_success 'blame with showEmail config false' '
+ git config blame.showEmail false &&
+ git blame one >blame1 &&
+ find_blame <blame1 >result &&
+ test_cmp expected_n result &&
+ git blame --show-email one >blame2 &&
+ find_blame <blame2 >result &&
+ test_cmp expected_e result &&
+ git blame -e one >blame3 &&
+ find_blame <blame3 >result &&
+ test_cmp expected_e result &&
+ git blame --no-show-email one >blame4 &&
+ find_blame <blame4 >result &&
+ test_cmp expected_n result
+'
+
+test_expect_success 'blame with showEmail config true' '
+ git config blame.showEmail true &&
+ git blame one >blame1 &&
+ find_blame <blame1 >result &&
+ test_cmp expected_e result &&
+ git blame --no-show-email one >blame2 &&
+ find_blame <blame2 >result &&
+ test_cmp expected_n result
+'
+
+test_expect_success 'set up abbrev tests' '
+ test_commit abbrev &&
+ sha1=$(git rev-parse --verify HEAD) &&
+ check_abbrev () {
+ expect=$1 && shift &&
+ echo $sha1 | cut -c 1-$expect >expect &&
+ git blame "$@" abbrev.t >actual &&
+ perl -lne "/[0-9a-f]+/ and print \$&" <actual >actual.sha &&
+ test_cmp expect actual.sha
+ }
+'
+
+test_expect_success 'blame --abbrev=<n> works' '
+ # non-boundary commits get +1 for alignment
+ check_abbrev 31 --abbrev=30 HEAD &&
+ check_abbrev 30 --abbrev=30 ^HEAD
+'
+
+test_expect_success 'blame -l aligns regular and boundary commits' '
+ check_abbrev $hexsz -l HEAD &&
+ check_abbrev $((hexsz - 1)) -l ^HEAD
+'
+
+test_expect_success 'blame --abbrev with full length behaves like -l' '
+ check_abbrev $hexsz --abbrev=$hexsz HEAD &&
+ check_abbrev $((hexsz - 1)) --abbrev=$hexsz ^HEAD
+'
+
+test_expect_success '--no-abbrev works like --abbrev with full length' '
+ check_abbrev $hexsz --no-abbrev
+'
+
+test_expect_success '--exclude-promisor-objects does not BUG-crash' '
+ test_must_fail git blame --exclude-promisor-objects one
+'
+
+test_expect_success 'blame with uncommitted edits in partial clone does not crash' '
+ git init server &&
+ echo foo >server/file.txt &&
+ git -C server add file.txt &&
+ git -C server commit -m file &&
+
+ git clone --filter=blob:none "file://$(pwd)/server" client &&
+ echo bar >>client/file.txt &&
+ git -C client blame file.txt
+'
+
+test_done
diff --git a/t/t8003-blame-corner-cases.sh b/t/t8003-blame-corner-cases.sh
new file mode 100755
index 0000000..7312655
--- /dev/null
+++ b/t/t8003-blame-corner-cases.sh
@@ -0,0 +1,319 @@
+#!/bin/sh
+
+test_description='git blame corner cases'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+pick_fc='s/^[0-9a-f^]* *\([^ ]*\) *(\([^ ]*\) .*/\1-\2/'
+
+test_expect_success setup '
+ echo A A A A A >one &&
+ echo B B B B B >two &&
+ echo C C C C C >tres &&
+ echo ABC >mouse &&
+ test_write_lines 1 2 3 4 5 6 7 8 9 >nine_lines &&
+ test_write_lines 1 2 3 4 5 6 7 8 9 a >ten_lines &&
+ git add one two tres mouse nine_lines ten_lines &&
+ test_tick &&
+ GIT_AUTHOR_NAME=Initial git commit -m Initial &&
+
+ cat one >uno &&
+ mv two dos &&
+ cat one >>tres &&
+ echo DEF >>mouse &&
+ git add uno dos tres mouse &&
+ test_tick &&
+ GIT_AUTHOR_NAME=Second git commit -a -m Second &&
+
+ echo GHIJK >>mouse &&
+ git add mouse &&
+ test_tick &&
+ GIT_AUTHOR_NAME=Third git commit -m Third &&
+
+ cat mouse >cow &&
+ git add cow &&
+ test_tick &&
+ GIT_AUTHOR_NAME=Fourth git commit -m Fourth &&
+
+ cat >cow <<-\EOF &&
+ ABC
+ DEF
+ XXXX
+ GHIJK
+ EOF
+ git add cow &&
+ test_tick &&
+ GIT_AUTHOR_NAME=Fifth git commit -m Fifth
+'
+
+test_expect_success 'straight copy without -C' '
+
+ git blame uno | grep Second
+
+'
+
+test_expect_success 'straight move without -C' '
+
+ git blame dos | grep Initial
+
+'
+
+test_expect_success 'straight copy with -C' '
+
+ git blame -C1 uno | grep Second
+
+'
+
+test_expect_success 'straight move with -C' '
+
+ git blame -C1 dos | grep Initial
+
+'
+
+test_expect_success 'straight copy with -C -C' '
+
+ git blame -C -C1 uno | grep Initial
+
+'
+
+test_expect_success 'straight move with -C -C' '
+
+ git blame -C -C1 dos | grep Initial
+
+'
+
+test_expect_success 'append without -C' '
+
+ git blame -L2 tres | grep Second
+
+'
+
+test_expect_success 'append with -C' '
+
+ git blame -L2 -C1 tres | grep Second
+
+'
+
+test_expect_success 'append with -C -C' '
+
+ git blame -L2 -C -C1 tres | grep Second
+
+'
+
+test_expect_success 'append with -C -C -C' '
+
+ git blame -L2 -C -C -C1 tres | grep Initial
+
+'
+
+test_expect_success 'blame wholesale copy' '
+
+ git blame -f -C -C1 HEAD^ -- cow | sed -e "$pick_fc" >current &&
+ cat >expected <<-\EOF &&
+ mouse-Initial
+ mouse-Second
+ mouse-Third
+ EOF
+ test_cmp expected current
+
+'
+
+test_expect_success 'blame wholesale copy and more' '
+
+ git blame -f -C -C1 HEAD -- cow | sed -e "$pick_fc" >current &&
+ cat >expected <<-\EOF &&
+ mouse-Initial
+ mouse-Second
+ cow-Fifth
+ mouse-Third
+ EOF
+ test_cmp expected current
+
+'
+
+test_expect_success 'blame wholesale copy and more in the index' '
+
+ cat >horse <<-\EOF &&
+ ABC
+ DEF
+ XXXX
+ YYYY
+ GHIJK
+ EOF
+ git add horse &&
+ test_when_finished "git rm -f horse" &&
+ git blame -f -C -C1 -- horse | sed -e "$pick_fc" >current &&
+ cat >expected <<-\EOF &&
+ mouse-Initial
+ mouse-Second
+ cow-Fifth
+ horse-Not
+ mouse-Third
+ EOF
+ test_cmp expected current
+
+'
+
+test_expect_success 'blame during cherry-pick with file rename conflict' '
+
+ test_when_finished "git reset --hard && git checkout main" &&
+ git checkout HEAD~3 &&
+ echo MOUSE >> mouse &&
+ git mv mouse rodent &&
+ git add rodent &&
+ GIT_AUTHOR_NAME=Rodent git commit -m "rodent" &&
+ git checkout --detach main &&
+ (git cherry-pick HEAD@{1} || test $? -eq 1) &&
+ git show HEAD@{1}:rodent > rodent &&
+ git add rodent &&
+ git blame -f -C -C1 rodent | sed -e "$pick_fc" >current &&
+ cat >expected <<-\EOF &&
+ mouse-Initial
+ mouse-Second
+ rodent-Not
+ EOF
+ test_cmp expected current
+'
+
+test_expect_success 'blame path that used to be a directory' '
+ mkdir path &&
+ echo A A A A A >path/file &&
+ echo B B B B B >path/elif &&
+ git add path &&
+ test_tick &&
+ git commit -m "path was a directory" &&
+ rm -fr path &&
+ echo A A A A A >path &&
+ git add path &&
+ test_tick &&
+ git commit -m "path is a regular file" &&
+ git blame HEAD^.. -- path
+'
+
+test_expect_success 'blame to a commit with no author name' '
+ TREE=$(git rev-parse HEAD:) &&
+ cat >badcommit <<EOF &&
+tree $TREE
+author <noname> 1234567890 +0000
+committer David Reiss <dreiss@facebook.com> 1234567890 +0000
+
+some message
+EOF
+ COMMIT=$(git hash-object --literally -t commit -w badcommit) &&
+ git --no-pager blame $COMMIT -- uno >/dev/null
+'
+
+test_expect_success 'blame -L with invalid start' '
+ test_must_fail git blame -L5 tres 2>errors &&
+ test_grep "has only 2 lines" errors
+'
+
+test_expect_success 'blame -L with invalid end' '
+ git blame -L1,5 tres >out &&
+ test_line_count = 2 out
+'
+
+test_expect_success 'blame parses <end> part of -L' '
+ git blame -L1,1 tres >out &&
+ test_line_count = 1 out
+'
+
+test_expect_success 'blame -Ln,-(n+1)' '
+ git blame -L3,-4 nine_lines >out &&
+ test_line_count = 3 out
+'
+
+test_expect_success 'indent of line numbers, nine lines' '
+ git blame nine_lines >actual &&
+ test $(grep -c " " actual) = 0
+'
+
+test_expect_success 'indent of line numbers, ten lines' '
+ git blame ten_lines >actual &&
+ test $(grep -c " " actual) = 9
+'
+
+test_expect_success 'setup file with CRLF newlines' '
+ git config core.autocrlf false &&
+ printf "testcase\n" >crlffile &&
+ git add crlffile &&
+ git commit -m testcase &&
+ printf "testcase\r\n" >crlffile
+'
+
+test_expect_success 'blame file with CRLF core.autocrlf true' '
+ git config core.autocrlf true &&
+ git blame crlffile >actual &&
+ grep "A U Thor" actual
+'
+
+test_expect_success 'blame file with CRLF attributes text' '
+ git config core.autocrlf false &&
+ echo "crlffile text" >.gitattributes &&
+ git blame crlffile >actual &&
+ grep "A U Thor" actual
+'
+
+test_expect_success 'blame file with CRLF core.autocrlf=true' '
+ git config core.autocrlf false &&
+ printf "testcase\r\n" >crlfinrepo &&
+ >.gitattributes &&
+ git add crlfinrepo &&
+ git commit -m "add crlfinrepo" &&
+ git config core.autocrlf true &&
+ mv crlfinrepo tmp &&
+ git checkout crlfinrepo &&
+ rm tmp &&
+ git blame crlfinrepo >actual &&
+ grep "A U Thor" actual
+'
+
+test_expect_success 'setup coalesce tests' '
+ cat >giraffe <<-\EOF &&
+ ABC
+ DEF
+ EOF
+ git add giraffe &&
+ git commit -m "original file" &&
+ orig=$(git rev-parse HEAD) &&
+
+ cat >giraffe <<-\EOF &&
+ ABC
+ SPLIT
+ DEF
+ EOF
+ git add giraffe &&
+ git commit -m "interior SPLIT line" &&
+ split=$(git rev-parse HEAD) &&
+
+ cat >giraffe <<-\EOF &&
+ ABC
+ DEF
+ EOF
+ git add giraffe &&
+ git commit -m "same contents as original" &&
+ final=$(git rev-parse HEAD)
+'
+
+test_expect_success 'blame coalesce' '
+ cat >expect <<-EOF &&
+ $orig 1 1 2
+ $orig 2 2
+ EOF
+ git blame --porcelain $final giraffe >actual.raw &&
+ grep "^$orig" actual.raw >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'blame does not coalesce non-adjacent result lines' '
+ cat >expect <<-EOF &&
+ $orig 1) ABC
+ $orig 3) DEF
+ EOF
+ git blame --no-abbrev -s -L1,1 -L3,3 $split giraffe >actual &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t8004-blame-with-conflicts.sh b/t/t8004-blame-with-conflicts.sh
new file mode 100755
index 0000000..35414a5
--- /dev/null
+++ b/t/t8004-blame-with-conflicts.sh
@@ -0,0 +1,76 @@
+#!/bin/sh
+
+# Based on a test case submitted by Björn Steinbrink.
+
+test_description='git blame on conflicted files'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success 'setup first case' '
+ # Create the old file
+ echo "Old line" > file1 &&
+ git add file1 &&
+ git commit --author "Old Line <ol@localhost>" -m file1.a &&
+
+ # Branch
+ git checkout -b foo &&
+
+ # Do an ugly move and change
+ git rm file1 &&
+ echo "New line ..." > file2 &&
+ echo "... and more" >> file2 &&
+ git add file2 &&
+ git commit --author "U Gly <ug@localhost>" -m ugly &&
+
+ # Back to main and change something
+ git checkout main &&
+ echo "
+
+bla" >> file1 &&
+ git commit --author "Old Line <ol@localhost>" -a -m file1.b &&
+
+ # Back to foo and merge main
+ git checkout foo &&
+ if git merge main; then
+ echo needed conflict here
+ exit 1
+ else
+ echo merge failed - resolving automatically
+ fi &&
+ echo "New line ...
+... and more
+
+bla
+Even more" > file2 &&
+ git rm file1 &&
+ git commit --author "M Result <mr@localhost>" -a -m merged &&
+
+ # Back to main and change file1 again
+ git checkout main &&
+ sed s/bla/foo/ <file1 >X &&
+ rm file1 &&
+ mv X file1 &&
+ git commit --author "No Bla <nb@localhost>" -a -m replace &&
+
+ # Try to merge into foo again
+ git checkout foo &&
+ if git merge main; then
+ echo needed conflict here
+ exit 1
+ else
+ echo merge failed - test is setup
+ fi
+'
+
+test_expect_success \
+ 'blame runs on unconflicted file while other file has conflicts' '
+ git blame file2
+'
+
+test_expect_success 'blame does not crash with conflicted file in stages 1,3' '
+ git blame file1
+'
+
+test_done
diff --git a/t/t8005-blame-i18n.sh b/t/t8005-blame-i18n.sh
new file mode 100755
index 0000000..75da219
--- /dev/null
+++ b/t/t8005-blame-i18n.sh
@@ -0,0 +1,96 @@
+#!/bin/sh
+
+test_description='git blame encoding conversion'
+. ./test-lib.sh
+
+. "$TEST_DIRECTORY"/t8005/utf8.txt
+. "$TEST_DIRECTORY"/t8005/euc-japan.txt
+. "$TEST_DIRECTORY"/t8005/sjis.txt
+
+test_expect_success 'setup the repository' '
+ # Create the file
+ echo "UTF-8 LINE" > file &&
+ git add file &&
+ git commit --author "$UTF8_NAME <utf8@localhost>" -m "$UTF8_MSG" &&
+
+ echo "EUC-JAPAN LINE" >> file &&
+ git add file &&
+ git config i18n.commitencoding eucJP &&
+ git commit --author "$EUC_JAPAN_NAME <euc-japan@localhost>" -m "$EUC_JAPAN_MSG" &&
+
+ echo "SJIS LINE" >> file &&
+ git add file &&
+ git config i18n.commitencoding SJIS &&
+ git commit --author "$SJIS_NAME <sjis@localhost>" -m "$SJIS_MSG"
+'
+
+cat >expected <<EOF
+author $SJIS_NAME
+summary $SJIS_MSG
+author $SJIS_NAME
+summary $SJIS_MSG
+author $SJIS_NAME
+summary $SJIS_MSG
+EOF
+
+filter_author_summary () {
+ sed -n -e '/^author /p' -e '/^summary /p' "$@"
+}
+
+test_expect_success !MINGW \
+ 'blame respects i18n.commitencoding' '
+ git blame --incremental file >output &&
+ filter_author_summary output >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<EOF
+author $EUC_JAPAN_NAME
+summary $EUC_JAPAN_MSG
+author $EUC_JAPAN_NAME
+summary $EUC_JAPAN_MSG
+author $EUC_JAPAN_NAME
+summary $EUC_JAPAN_MSG
+EOF
+
+test_expect_success !MINGW \
+ 'blame respects i18n.logoutputencoding' '
+ git config i18n.logoutputencoding eucJP &&
+ git blame --incremental file >output &&
+ filter_author_summary output >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<EOF
+author $UTF8_NAME
+summary $UTF8_MSG
+author $UTF8_NAME
+summary $UTF8_MSG
+author $UTF8_NAME
+summary $UTF8_MSG
+EOF
+
+test_expect_success !MINGW \
+ 'blame respects --encoding=UTF-8' '
+ git blame --incremental --encoding=UTF-8 file >output &&
+ filter_author_summary output >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<EOF
+author $SJIS_NAME
+summary $SJIS_MSG
+author $EUC_JAPAN_NAME
+summary $EUC_JAPAN_MSG
+author $UTF8_NAME
+summary $UTF8_MSG
+EOF
+
+test_expect_success !MINGW \
+ 'blame respects --encoding=none' '
+ git blame --incremental --encoding=none file >output &&
+ filter_author_summary output >actual &&
+ test_cmp expected actual
+'
+
+test_done
diff --git a/t/t8005/euc-japan.txt b/t/t8005/euc-japan.txt
new file mode 100644
index 0000000..288f040
--- /dev/null
+++ b/t/t8005/euc-japan.txt
@@ -0,0 +1,2 @@
+EUC_JAPAN_NAME="»³ÅÄ ÂÀϺ"
+EUC_JAPAN_MSG="¥Ö¥ì¡¼¥à¤Î¥Æ¥¹¥È¤Ç¤¹¡£"
diff --git a/t/t8005/sjis.txt b/t/t8005/sjis.txt
new file mode 100644
index 0000000..bbdefea
--- /dev/null
+++ b/t/t8005/sjis.txt
@@ -0,0 +1,2 @@
+SJIS_NAME="ŽR“c ‘¾˜Y"
+SJIS_MSG="ƒuƒŒ[ƒ€‚̃eƒXƒg‚Å‚·B"
diff --git a/t/t8005/utf8.txt b/t/t8005/utf8.txt
new file mode 100644
index 0000000..4d00dbe
--- /dev/null
+++ b/t/t8005/utf8.txt
@@ -0,0 +1,2 @@
+UTF8_NAME="山田 太郎"
+UTF8_MSG="ブレームã®ãƒ†ã‚¹ãƒˆã§ã™ã€‚"
diff --git a/t/t8006-blame-textconv.sh b/t/t8006-blame-textconv.sh
new file mode 100755
index 0000000..7683515
--- /dev/null
+++ b/t/t8006-blame-textconv.sh
@@ -0,0 +1,157 @@
+#!/bin/sh
+
+test_description='git blame textconv support'
+. ./test-lib.sh
+
+find_blame() {
+ sed -e 's/^[^(]*//'
+}
+
+cat >helper <<'EOF'
+#!/bin/sh
+grep -q '^bin: ' "$1" || { echo "E: $1 is not \"binary\" file" 1>&2; exit 1; }
+"$PERL_PATH" -p -e 's/^bin: /converted: /' "$1"
+EOF
+chmod +x helper
+
+test_expect_success 'setup ' '
+ echo "bin: test number 0" >zero.bin &&
+ echo "bin: test 1" >one.bin &&
+ echo "bin: test number 2" >two.bin &&
+ test_ln_s_add one.bin symlink.bin &&
+ git add . &&
+ GIT_AUTHOR_NAME=Number1 git commit -a -m First --date="2010-01-01 18:00:00" &&
+ echo "bin: test 1 version 2" >one.bin &&
+ echo "bin: test number 2 version 2" >>two.bin &&
+ rm -f symlink.bin &&
+ test_ln_s_add two.bin symlink.bin &&
+ GIT_AUTHOR_NAME=Number2 git commit -a -m Second --date="2010-01-01 20:00:00"
+'
+
+cat >expected <<EOF
+(Number2 2010-01-01 20:00:00 +0000 1) bin: test 1 version 2
+EOF
+
+test_expect_success 'no filter specified' '
+ git blame one.bin >blame &&
+ find_blame Number2 <blame >result &&
+ test_cmp expected result
+'
+
+test_expect_success 'setup textconv filters' '
+ echo "*.bin diff=test" >.gitattributes &&
+ echo "zero.bin eol=crlf" >>.gitattributes &&
+ git config diff.test.textconv ./helper &&
+ git config diff.test.cachetextconv false
+'
+
+test_expect_success 'blame with --no-textconv' '
+ git blame --no-textconv one.bin >blame &&
+ find_blame <blame> result &&
+ test_cmp expected result
+'
+
+cat >expected <<EOF
+(Number2 2010-01-01 20:00:00 +0000 1) converted: test 1 version 2
+EOF
+
+test_expect_success 'basic blame on last commit' '
+ git blame one.bin >blame &&
+ find_blame <blame >result &&
+ test_cmp expected result
+'
+
+cat >expected <<EOF
+(Number1 2010-01-01 18:00:00 +0000 1) converted: test number 2
+(Number2 2010-01-01 20:00:00 +0000 2) converted: test number 2 version 2
+EOF
+
+test_expect_success 'blame --textconv going through revisions' '
+ git blame --textconv two.bin >blame &&
+ find_blame <blame >result &&
+ test_cmp expected result
+'
+
+test_expect_success 'blame --textconv with local changes' '
+ test_when_finished "git checkout zero.bin" &&
+ printf "bin: updated number 0\015" >zero.bin &&
+ git blame --textconv zero.bin >blame &&
+ expect="(Not Committed Yet ....-..-.. ..:..:.. +0000 1)" &&
+ expect="$expect converted: updated number 0" &&
+ expr "$(find_blame <blame)" : "^$expect"
+'
+
+test_expect_success 'setup +cachetextconv' '
+ git config diff.test.cachetextconv true
+'
+
+cat >expected_one <<EOF
+(Number2 2010-01-01 20:00:00 +0000 1) converted: test 1 version 2
+EOF
+
+test_expect_success 'blame --textconv works with textconvcache' '
+ git blame --textconv two.bin >blame &&
+ find_blame <blame >result &&
+ test_cmp expected result &&
+ git blame --textconv one.bin >blame &&
+ find_blame <blame >result &&
+ test_cmp expected_one result
+'
+
+test_expect_success 'setup -cachetextconv' '
+ git config diff.test.cachetextconv false
+'
+
+test_expect_success 'make a new commit' '
+ echo "bin: test number 2 version 3" >>two.bin &&
+ GIT_AUTHOR_NAME=Number3 git commit -a -m Third --date="2010-01-01 22:00:00"
+'
+
+test_expect_success 'blame from previous revision' '
+ git blame HEAD^ two.bin >blame &&
+ find_blame <blame >result &&
+ test_cmp expected result
+'
+
+cat >expected <<EOF
+(Number2 2010-01-01 20:00:00 +0000 1) two.bin
+EOF
+
+test_expect_success SYMLINKS 'blame with --no-textconv (on symlink)' '
+ git blame --no-textconv symlink.bin >blame &&
+ find_blame <blame >result &&
+ test_cmp expected result
+'
+
+test_expect_success SYMLINKS 'blame --textconv (on symlink)' '
+ git blame --textconv symlink.bin >blame &&
+ find_blame <blame >result &&
+ test_cmp expected result
+'
+
+# cp two.bin three.bin and make small tweak
+# (this will direct blame -C -C three.bin to consider two.bin and symlink.bin)
+test_expect_success 'make another new commit' '
+ cat >three.bin <<\EOF &&
+bin: test number 2
+bin: test number 2 version 2
+bin: test number 2 version 3
+bin: test number 3
+EOF
+ git add three.bin &&
+ GIT_AUTHOR_NAME=Number4 git commit -a -m Fourth --date="2010-01-01 23:00:00"
+'
+
+test_expect_success 'blame on last commit (-C -C, symlink)' '
+ git blame -C -C three.bin >blame &&
+ find_blame <blame >result &&
+ cat >expected <<\EOF &&
+(Number1 2010-01-01 18:00:00 +0000 1) converted: test number 2
+(Number2 2010-01-01 20:00:00 +0000 2) converted: test number 2 version 2
+(Number3 2010-01-01 22:00:00 +0000 3) converted: test number 2 version 3
+(Number4 2010-01-01 23:00:00 +0000 4) converted: test number 3
+EOF
+ test_cmp expected result
+'
+
+test_done
diff --git a/t/t8007-cat-file-textconv.sh b/t/t8007-cat-file-textconv.sh
new file mode 100755
index 0000000..c8266f1
--- /dev/null
+++ b/t/t8007-cat-file-textconv.sh
@@ -0,0 +1,130 @@
+#!/bin/sh
+
+test_description='git cat-file textconv support'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+cat >helper <<'EOF'
+#!/bin/sh
+grep -q '^bin: ' "$1" || { echo "E: $1 is not \"binary\" file" 1>&2; exit 1; }
+sed 's/^bin: /converted: /' "$1"
+EOF
+chmod +x helper
+
+test_expect_success 'setup ' '
+ echo "bin: test" >one.bin &&
+ test_ln_s_add one.bin symlink.bin &&
+ git add . &&
+ GIT_AUTHOR_NAME=Number1 git commit -a -m First --date="2010-01-01 18:00:00" &&
+ echo "bin: test version 2" >one.bin &&
+ GIT_AUTHOR_NAME=Number2 git commit -a -m Second --date="2010-01-01 20:00:00"
+'
+
+test_expect_success 'usage: <bad rev>' '
+ cat >expect <<-\EOF &&
+ fatal: Not a valid object name HEAD2
+ EOF
+ test_must_fail git cat-file --textconv HEAD2 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'usage: <bad rev>:<bad path>' '
+ cat >expect <<-\EOF &&
+ fatal: invalid object name '\''HEAD2'\''.
+ EOF
+ test_must_fail git cat-file --textconv HEAD2:two.bin 2>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'usage: <rev>:<bad path>' '
+ cat >expect <<-\EOF &&
+ fatal: path '\''two.bin'\'' does not exist in '\''HEAD'\''
+ EOF
+ test_must_fail git cat-file --textconv HEAD:two.bin 2>actual &&
+ test_cmp expect actual
+'
+
+
+test_expect_success 'usage: <rev> with no <path>' '
+ cat >expect <<-\EOF &&
+ fatal: <object>:<path> required, only <object> '\''HEAD'\'' given
+ EOF
+ test_must_fail git cat-file --textconv HEAD 2>actual &&
+ test_cmp expect actual
+'
+
+
+test_expect_success 'usage: <bad rev>:<good (in HEAD) path>' '
+ cat >expect <<-\EOF &&
+ fatal: invalid object name '\''HEAD2'\''.
+ EOF
+ test_must_fail git cat-file --textconv HEAD2:one.bin 2>actual &&
+ test_cmp expect actual
+'
+
+cat >expected <<EOF
+bin: test version 2
+EOF
+
+test_expect_success 'no filter specified' '
+ git cat-file --textconv :one.bin >result &&
+ test_cmp expected result
+'
+
+test_expect_success 'setup textconv filters' '
+ echo "*.bin diff=test" >.gitattributes &&
+ git config diff.test.textconv ./helper &&
+ git config diff.test.cachetextconv false
+'
+
+test_expect_success 'cat-file without --textconv' '
+ git cat-file blob :one.bin >result &&
+ test_cmp expected result
+'
+
+cat >expected <<EOF
+bin: test
+EOF
+
+test_expect_success 'cat-file without --textconv on previous commit' '
+ git cat-file -p HEAD^:one.bin >result &&
+ test_cmp expected result
+'
+
+cat >expected <<EOF
+converted: test version 2
+EOF
+
+test_expect_success 'cat-file --textconv on last commit' '
+ git cat-file --textconv :one.bin >result &&
+ test_cmp expected result
+'
+
+cat >expected <<EOF
+converted: test
+EOF
+
+test_expect_success 'cat-file --textconv on previous commit' '
+ git cat-file --textconv HEAD^:one.bin >result &&
+ test_cmp expected result
+'
+
+test_expect_success 'cat-file without --textconv (symlink)' '
+ printf "%s" "one.bin" >expected &&
+ git cat-file blob :symlink.bin >result &&
+ test_cmp expected result
+'
+
+
+test_expect_success 'cat-file --textconv on index (symlink)' '
+ git cat-file --textconv :symlink.bin >result &&
+ test_cmp expected result
+'
+
+test_expect_success 'cat-file --textconv on HEAD (symlink)' '
+ git cat-file --textconv HEAD:symlink.bin >result &&
+ test_cmp expected result
+'
+
+test_done
diff --git a/t/t8008-blame-formats.sh b/t/t8008-blame-formats.sh
new file mode 100755
index 0000000..ae4b579
--- /dev/null
+++ b/t/t8008-blame-formats.sh
@@ -0,0 +1,109 @@
+#!/bin/sh
+
+test_description='blame output in various formats on a simple case'
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ echo a >file &&
+ git add file &&
+ test_tick &&
+ git commit -m one &&
+ echo b >>file &&
+ echo c >>file &&
+ echo d >>file &&
+ test_tick &&
+ git commit -a -m two &&
+ ID1=$(git rev-parse HEAD^) &&
+ shortID1="^$(git rev-parse HEAD^ |cut -c 1-17)" &&
+ ID2=$(git rev-parse HEAD) &&
+ shortID2="$(git rev-parse HEAD |cut -c 1-18)"
+'
+
+cat >expect <<EOF
+$shortID1 (A U Thor 2005-04-07 15:13:13 -0700 1) a
+$shortID2 (A U Thor 2005-04-07 15:14:13 -0700 2) b
+$shortID2 (A U Thor 2005-04-07 15:14:13 -0700 3) c
+$shortID2 (A U Thor 2005-04-07 15:14:13 -0700 4) d
+EOF
+test_expect_success 'normal blame output' '
+ git blame --abbrev=17 file >actual &&
+ test_cmp expect actual
+'
+
+COMMIT1="author A U Thor
+author-mail <author@example.com>
+author-time 1112911993
+author-tz -0700
+committer C O Mitter
+committer-mail <committer@example.com>
+committer-time 1112911993
+committer-tz -0700
+summary one
+boundary
+filename file"
+COMMIT2="author A U Thor
+author-mail <author@example.com>
+author-time 1112912053
+author-tz -0700
+committer C O Mitter
+committer-mail <committer@example.com>
+committer-time 1112912053
+committer-tz -0700
+summary two
+previous $ID1 file
+filename file"
+
+cat >expect <<EOF
+$ID1 1 1 1
+$COMMIT1
+ a
+$ID2 2 2 3
+$COMMIT2
+ b
+$ID2 3 3
+ c
+$ID2 4 4
+ d
+EOF
+test_expect_success 'blame --porcelain output' '
+ git blame --porcelain file >actual &&
+ test_cmp expect actual
+'
+
+cat >expect <<EOF
+$ID1 1 1 1
+$COMMIT1
+ a
+$ID2 2 2 3
+$COMMIT2
+ b
+$ID2 3 3
+$COMMIT2
+ c
+$ID2 4 4
+$COMMIT2
+ d
+EOF
+test_expect_success 'blame --line-porcelain output' '
+ git blame --line-porcelain file >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '--porcelain detects first non-blank line as subject' '
+ (
+ GIT_INDEX_FILE=.git/tmp-index &&
+ export GIT_INDEX_FILE &&
+ echo "This is it" >single-file &&
+ git add single-file &&
+ tree=$(git write-tree) &&
+ commit=$(printf "%s\n%s\n%s\n\n\n \noneline\n\nbody\n" \
+ "tree $tree" \
+ "author A <a@b.c> 123456789 +0000" \
+ "committer C <c@d.e> 123456789 +0000" |
+ git hash-object -w -t commit --stdin) &&
+ git blame --porcelain $commit -- single-file >output &&
+ grep "^summary oneline$" output
+ )
+'
+
+test_done
diff --git a/t/t8009-blame-vs-topicbranches.sh b/t/t8009-blame-vs-topicbranches.sh
new file mode 100755
index 0000000..72596e3
--- /dev/null
+++ b/t/t8009-blame-vs-topicbranches.sh
@@ -0,0 +1,36 @@
+#!/bin/sh
+
+test_description='blaming trough history with topic branches'
+. ./test-lib.sh
+
+# Creates the history shown below. '*'s mark the first parent in the merges.
+# The only line of file.t is changed in commit B2
+#
+# +---C1
+# / \
+# A0--A1--*A2--*A3
+# \ /
+# B1-B2
+#
+test_expect_success setup '
+ test_commit A0 file.t line0 &&
+ test_commit A1 &&
+ git reset --hard A0 &&
+ test_commit B1 &&
+ test_commit B2 file.t line0changed &&
+ git reset --hard A1 &&
+ test_merge A2 B2 &&
+ git reset --hard A1 &&
+ test_commit C1 &&
+ git reset --hard A2 &&
+ test_merge A3 C1
+ '
+
+test_expect_success 'blame --reverse --first-parent finds A1' '
+ git blame --porcelain --reverse --first-parent A0..A3 -- file.t >actual_full &&
+ head -n 1 <actual_full | sed -e "s/ .*//" >actual &&
+ git rev-parse A1 >expect &&
+ test_cmp expect actual
+ '
+
+test_done
diff --git a/t/t8010-cat-file-filters.sh b/t/t8010-cat-file-filters.sh
new file mode 100755
index 0000000..ca04242
--- /dev/null
+++ b/t/t8010-cat-file-filters.sh
@@ -0,0 +1,71 @@
+#!/bin/sh
+
+test_description='git cat-file filters support'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup ' '
+ echo "*.txt eol=crlf diff=txt" >.gitattributes &&
+ echo "hello" | append_cr >world.txt &&
+ git add .gitattributes world.txt &&
+ test_tick &&
+ git commit -m "Initial commit"
+'
+
+has_cr () {
+ tr '\015' Q <"$1" | grep Q >/dev/null
+}
+
+test_expect_success 'no filters with `git show`' '
+ git show HEAD:world.txt >actual &&
+ ! has_cr actual
+
+'
+
+test_expect_success 'no filters with cat-file' '
+ git cat-file blob HEAD:world.txt >actual &&
+ ! has_cr actual
+'
+
+test_expect_success 'cat-file --filters converts to worktree version' '
+ git cat-file --filters HEAD:world.txt >actual &&
+ has_cr actual
+'
+
+test_expect_success 'cat-file --filters --path=<path> works' '
+ sha1=$(git rev-parse -q --verify HEAD:world.txt) &&
+ git cat-file --filters --path=world.txt $sha1 >actual &&
+ has_cr actual
+'
+
+test_expect_success 'cat-file --textconv --path=<path> works' '
+ sha1=$(git rev-parse -q --verify HEAD:world.txt) &&
+ test_config diff.txt.textconv "tr A-Za-z N-ZA-Mn-za-m <" &&
+ git cat-file --textconv --path=hello.txt $sha1 >rot13 &&
+ test uryyb = "$(cat rot13 | remove_cr)"
+'
+
+test_expect_success '--path=<path> complains without --textconv/--filters' '
+ sha1=$(git rev-parse -q --verify HEAD:world.txt) &&
+ test_must_fail git cat-file --path=hello.txt blob $sha1 >actual 2>err &&
+ test_must_be_empty actual &&
+ grep "path.*needs.*filters" err
+'
+
+test_expect_success '--textconv/--filters complain without path' '
+ test_must_fail git cat-file --textconv HEAD &&
+ test_must_fail git cat-file --filters HEAD
+'
+
+test_expect_success 'cat-file --textconv --batch works' '
+ sha1=$(git rev-parse -q --verify HEAD:world.txt) &&
+ test_config diff.txt.textconv "tr A-Za-z N-ZA-Mn-za-m <" &&
+ printf "%s hello.txt\n%s hello\n" $sha1 $sha1 |
+ git cat-file --textconv --batch >actual &&
+ printf "%s blob 6\nuryyb\r\n\n%s blob 6\nhello\n\n" \
+ $sha1 $sha1 >expect &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t8011-blame-split-file.sh b/t/t8011-blame-split-file.sh
new file mode 100755
index 0000000..bdda0c0
--- /dev/null
+++ b/t/t8011-blame-split-file.sh
@@ -0,0 +1,117 @@
+#!/bin/sh
+
+test_description='
+The general idea is that we have a single file whose lines come from
+multiple other files, and those individual files were modified in the same
+commits. That means that we will see the same commit in multiple contexts,
+and each one should be attributed to the correct file.
+
+Note that we need to use "blame -C" to find the commit for all lines. We will
+not bother testing that the non-C case fails to find it. That is how blame
+behaves now, but it is not a property we want to make sure is retained.
+'
+. ./test-lib.sh
+
+# help avoid typing and reading long strings of similar lines
+# in the tests below
+generate_expect () {
+ while read nr data
+ do
+ i=0
+ while test $i -lt $nr
+ do
+ echo $data
+ i=$((i + 1))
+ done
+ done
+}
+
+test_expect_success 'setup split file case' '
+ # use lines long enough to trigger content detection
+ test_seq 1000 1010 >one &&
+ test_seq 2000 2010 >two &&
+ git add one two &&
+ test_commit base &&
+
+ sed "6s/^/modified /" <one >one.tmp &&
+ mv one.tmp one &&
+ sed "6s/^/modified /" <two >two.tmp &&
+ mv two.tmp two &&
+ git add -u &&
+ test_commit modified &&
+
+ cat one two >combined &&
+ git add combined &&
+ git rm one two &&
+ test_commit combined
+'
+
+test_expect_success 'setup simulated porcelain' '
+ # This just reads porcelain-ish output and tries
+ # to output the value of a given field for each line (either by
+ # reading the field that accompanies this line, or referencing
+ # the information found last time the commit was mentioned).
+ cat >read-porcelain.pl <<-\EOF
+ my $field = shift;
+ while (<>) {
+ if (/^[0-9a-f]{40,} /) {
+ flush();
+ $hash = $&;
+ } elsif (/^$field (.*)/) {
+ $cache{$hash} = $1;
+ }
+ }
+ flush();
+
+ sub flush {
+ return unless defined $hash;
+ if (defined $cache{$hash}) {
+ print "$cache{$hash}\n";
+ } else {
+ print "NONE\n";
+ }
+ }
+ EOF
+'
+
+for output in porcelain line-porcelain
+do
+ test_expect_success "generate --$output output" '
+ git blame --root -C --$output combined >output
+ '
+
+ test_expect_success "$output output finds correct commits" '
+ generate_expect >expect <<-\EOF &&
+ 5 base
+ 1 modified
+ 10 base
+ 1 modified
+ 5 base
+ EOF
+ perl read-porcelain.pl summary <output >actual &&
+ test_cmp expect actual
+ '
+
+ test_expect_success "$output output shows correct filenames" '
+ generate_expect >expect <<-\EOF &&
+ 11 one
+ 11 two
+ EOF
+ perl read-porcelain.pl filename <output >actual &&
+ test_cmp expect actual
+ '
+
+ test_expect_success "$output output shows correct previous pointer" '
+ generate_expect >expect <<-EOF &&
+ 5 NONE
+ 1 $(git rev-parse modified^) one
+ 10 NONE
+ 1 $(git rev-parse modified^) two
+ 5 NONE
+ EOF
+ perl read-porcelain.pl previous <output >actual &&
+ test_cmp expect actual
+ '
+done
+
+test_done
diff --git a/t/t8012-blame-colors.sh b/t/t8012-blame-colors.sh
new file mode 100755
index 0000000..c3a5f6d
--- /dev/null
+++ b/t/t8012-blame-colors.sh
@@ -0,0 +1,52 @@
+#!/bin/sh
+
+test_description='colored git blame'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_CREATE_REPO_NO_TEMPLATE=1
+. ./test-lib.sh
+
+PROG='git blame -c'
+. "$TEST_DIRECTORY"/annotate-tests.sh
+
+test_expect_success 'colored blame colors contiguous lines' '
+ git -c color.blame.repeatedLines=yellow blame --color-lines --abbrev=12 hello.c >actual.raw &&
+ git -c color.blame.repeatedLines=yellow -c blame.coloring=repeatedLines blame --abbrev=12 hello.c >actual.raw.2 &&
+ test_cmp actual.raw actual.raw.2 &&
+ test_decode_color <actual.raw >actual &&
+ grep "<YELLOW>" <actual >darkened &&
+ grep "(F" darkened > F.expect &&
+ grep "(H" darkened > H.expect &&
+ test_line_count = 2 F.expect &&
+ test_line_count = 3 H.expect
+'
+
+test_expect_success 'color by age consistently colors old code' '
+ git blame --color-by-age hello.c >actual.raw &&
+ git -c blame.coloring=highlightRecent blame hello.c >actual.raw.2 &&
+ test_cmp actual.raw actual.raw.2 &&
+ test_decode_color <actual.raw >actual &&
+ grep "<BLUE>" <actual >colored &&
+ test_line_count = 10 colored
+'
+
+test_expect_success 'blame color by age: new code is different' '
+ cat >>hello.c <<-EOF &&
+ void qfunc();
+ EOF
+ git add hello.c &&
+ GIT_AUTHOR_DATE="" git commit -m "new commit" &&
+
+ git -c color.blame.highlightRecent="yellow,1 month ago, cyan" blame --color-by-age hello.c >actual.raw &&
+ test_decode_color <actual.raw >actual &&
+
+ grep "<YELLOW>" <actual >colored &&
+ test_line_count = 10 colored &&
+
+ grep "<CYAN>" <actual >colored &&
+ test_line_count = 1 colored &&
+ grep qfunc colored
+'
+
+test_done
diff --git a/t/t8013-blame-ignore-revs.sh b/t/t8013-blame-ignore-revs.sh
new file mode 100755
index 0000000..9a03b0f
--- /dev/null
+++ b/t/t8013-blame-ignore-revs.sh
@@ -0,0 +1,291 @@
+#!/bin/sh
+
+test_description='ignore revisions when blaming'
+. ./test-lib.sh
+
+# Creates:
+# A--B--X
+# A added line 1 and B added line 2. X makes changes to those lines. Sanity
+# check that X is blamed for both lines.
+test_expect_success setup '
+ test_commit A file line1 &&
+
+ echo line2 >>file &&
+ git add file &&
+ test_tick &&
+ git commit -m B &&
+ git tag B &&
+
+ test_write_lines line-one line-two >file &&
+ git add file &&
+ test_tick &&
+ git commit -m X &&
+ git tag X &&
+ git tag -a -m "X (annotated)" XT &&
+
+ git blame --line-porcelain file >blame_raw &&
+
+ grep -E "^[0-9a-f]+ [0-9]+ 1" blame_raw | sed -e "s/ .*//" >actual &&
+ git rev-parse X >expect &&
+ test_cmp expect actual &&
+
+ grep -E "^[0-9a-f]+ [0-9]+ 2" blame_raw | sed -e "s/ .*//" >actual &&
+ git rev-parse X >expect &&
+ test_cmp expect actual
+'
+
+# Ensure bogus --ignore-rev requests are caught
+test_expect_success 'validate --ignore-rev' '
+ test_must_fail git blame --ignore-rev X^{tree} file
+'
+
+# Ensure bogus --ignore-revs-file requests are silently accepted
+test_expect_success 'validate --ignore-revs-file' '
+ git rev-parse X^{tree} >ignore_x &&
+ git blame --ignore-revs-file ignore_x file
+'
+
+for I in X XT
+do
+ # Ignore X (or XT), make sure A is blamed for line 1 and B for line 2.
+ # Giving X (i.e. commit) and XT (i.e. annotated tag to commit) should
+ # produce the same result.
+ test_expect_success "ignore_rev_changing_lines ($I)" '
+ git blame --line-porcelain --ignore-rev $I file >blame_raw &&
+
+ grep -E "^[0-9a-f]+ [0-9]+ 1" blame_raw | sed -e "s/ .*//" >actual &&
+ git rev-parse A >expect &&
+ test_cmp expect actual &&
+
+ grep -E "^[0-9a-f]+ [0-9]+ 2" blame_raw | sed -e "s/ .*//" >actual &&
+ git rev-parse B >expect &&
+ test_cmp expect actual
+ '
+done
+
+# For ignored revs that have added 'unblamable' lines, attribute those to the
+# ignored commit.
+# A--B--X--Y
+# Where Y changes lines 1 and 2, and adds lines 3 and 4. The added lines ought
+# to have nothing in common with "line-one" or "line-two", to keep any
+# heuristics from matching them with any lines in the parent.
+test_expect_success ignore_rev_adding_unblamable_lines '
+ test_write_lines line-one-change line-two-changed y3 y4 >file &&
+ git add file &&
+ test_tick &&
+ git commit -m Y &&
+ git tag Y &&
+
+ git rev-parse Y >expect &&
+ git blame --line-porcelain file --ignore-rev Y >blame_raw &&
+
+ grep -E "^[0-9a-f]+ [0-9]+ 3" blame_raw | sed -e "s/ .*//" >actual &&
+ test_cmp expect actual &&
+
+ grep -E "^[0-9a-f]+ [0-9]+ 4" blame_raw | sed -e "s/ .*//" >actual &&
+ test_cmp expect actual
+'
+
+# Ignore X and Y, both in separate files. Lines 1 == A, 2 == B.
+test_expect_success ignore_revs_from_files '
+ git rev-parse X >ignore_x &&
+ git rev-parse Y >ignore_y &&
+ git blame --line-porcelain file --ignore-revs-file ignore_x --ignore-revs-file ignore_y >blame_raw &&
+
+ grep -E "^[0-9a-f]+ [0-9]+ 1" blame_raw | sed -e "s/ .*//" >actual &&
+ git rev-parse A >expect &&
+ test_cmp expect actual &&
+
+ grep -E "^[0-9a-f]+ [0-9]+ 2" blame_raw | sed -e "s/ .*//" >actual &&
+ git rev-parse B >expect &&
+ test_cmp expect actual
+'
+
+# Ignore X from the config option, Y from a file.
+test_expect_success ignore_revs_from_configs_and_files '
+ git config --add blame.ignoreRevsFile ignore_x &&
+ git blame --line-porcelain file --ignore-revs-file ignore_y >blame_raw &&
+
+ grep -E "^[0-9a-f]+ [0-9]+ 1" blame_raw | sed -e "s/ .*//" >actual &&
+ git rev-parse A >expect &&
+ test_cmp expect actual &&
+
+ grep -E "^[0-9a-f]+ [0-9]+ 2" blame_raw | sed -e "s/ .*//" >actual &&
+ git rev-parse B >expect &&
+ test_cmp expect actual
+'
+
+# Override blame.ignoreRevsFile (ignore_x) with an empty string. X should be
+# blamed now for lines 1 and 2, since we are no longer ignoring X.
+test_expect_success override_ignore_revs_file '
+ git blame --line-porcelain file --ignore-revs-file "" --ignore-revs-file ignore_y >blame_raw &&
+ git rev-parse X >expect &&
+
+ grep -E "^[0-9a-f]+ [0-9]+ 1" blame_raw | sed -e "s/ .*//" >actual &&
+ test_cmp expect actual &&
+
+ grep -E "^[0-9a-f]+ [0-9]+ 2" blame_raw | sed -e "s/ .*//" >actual &&
+ test_cmp expect actual
+ '
+test_expect_success bad_files_and_revs '
+ test_must_fail git blame file --ignore-rev NOREV 2>err &&
+ test_grep "cannot find revision NOREV to ignore" err &&
+
+ test_must_fail git blame file --ignore-revs-file NOFILE 2>err &&
+ test_grep "could not open.*: NOFILE" err &&
+
+ echo NOREV >ignore_norev &&
+ test_must_fail git blame file --ignore-revs-file ignore_norev 2>err &&
+ test_grep "invalid object name: NOREV" err
+'
+
+# For ignored revs that have added 'unblamable' lines, mark those lines with a
+# '*'
+# A--B--X--Y
+# Lines 3 and 4 are from Y and unblamable. This was set up in
+# ignore_rev_adding_unblamable_lines.
+test_expect_success mark_unblamable_lines '
+ git config --add blame.markUnblamableLines true &&
+
+ git blame --ignore-rev Y file >blame_raw &&
+ echo "*" >expect &&
+
+ sed -n "3p" blame_raw | cut -c1 >actual &&
+ test_cmp expect actual &&
+
+ sed -n "4p" blame_raw | cut -c1 >actual &&
+ test_cmp expect actual
+'
+
+# Commit Z will touch the first two lines. Y touched all four.
+# A--B--X--Y--Z
+# The blame output when ignoring Z should be:
+# ?Y ... 1)
+# ?Y ... 2)
+# Y ... 3)
+# Y ... 4)
+# We're checking only the first character
+test_expect_success mark_ignored_lines '
+ git config --add blame.markIgnoredLines true &&
+
+ test_write_lines line-one-Z line-two-Z y3 y4 >file &&
+ git add file &&
+ test_tick &&
+ git commit -m Z &&
+ git tag Z &&
+
+ git blame --ignore-rev Z file >blame_raw &&
+ echo "?" >expect &&
+
+ sed -n "1p" blame_raw | cut -c1 >actual &&
+ test_cmp expect actual &&
+
+ sed -n "2p" blame_raw | cut -c1 >actual &&
+ test_cmp expect actual &&
+
+ sed -n "3p" blame_raw | cut -c1 >actual &&
+ ! test_cmp expect actual &&
+
+ sed -n "4p" blame_raw | cut -c1 >actual &&
+ ! test_cmp expect actual
+'
+
+# For ignored revs that added 'unblamable' lines and more recent commits changed
+# the blamable lines, mark the unblamable lines with a
+# '*'
+# A--B--X--Y--Z
+# Lines 3 and 4 are from Y and unblamable, as set up in
+# ignore_rev_adding_unblamable_lines. Z changed lines 1 and 2.
+test_expect_success mark_unblamable_lines_intermediate '
+ git config --add blame.markUnblamableLines true &&
+
+ git blame --ignore-rev Y file >blame_raw 2>stderr &&
+ echo "*" >expect &&
+
+ sed -n "3p" blame_raw | cut -c1 >actual &&
+ test_cmp expect actual &&
+
+ sed -n "4p" blame_raw | cut -c1 >actual &&
+ test_cmp expect actual
+'
+
+# The heuristic called by guess_line_blames() tries to find the size of a
+# blame_entry 'e' in the parent's address space. Those calculations need to
+# check for negative or zero values for when a blame entry is completely outside
+# the window of the parent's version of a file.
+#
+# This happens when one commit adds several lines (commit B below). A later
+# commit (C) changes one line in the middle of B's change. Commit C gets blamed
+# for its change, and that breaks up B's change into multiple blame entries.
+# When processing B, one of the blame_entries is outside A's window (which was
+# zero - it had no lines added on its side of the diff).
+#
+# A--B--C, ignore B to test the ignore heuristic's boundary checks.
+test_expect_success ignored_chunk_negative_parent_size '
+ rm -rf .git/ &&
+ git init &&
+
+ test_write_lines L1 L2 L7 L8 L9 >file &&
+ git add file &&
+ test_tick &&
+ git commit -m A &&
+ git tag A &&
+
+ test_write_lines L1 L2 L3 L4 L5 L6 L7 L8 L9 >file &&
+ git add file &&
+ test_tick &&
+ git commit -m B &&
+ git tag B &&
+
+ test_write_lines L1 L2 L3 L4 xxx L6 L7 L8 L9 >file &&
+ git add file &&
+ test_tick &&
+ git commit -m C &&
+ git tag C &&
+
+ git blame file --ignore-rev B >blame_raw
+'
+
+# Resetting the repo and creating:
+#
+# A--B--M
+# \ /
+# C-+
+#
+# 'A' creates a file. B changes line 1, and C changes line 9. M merges.
+test_expect_success ignore_merge '
+ rm -rf .git/ &&
+ git init &&
+
+ test_write_lines L1 L2 L3 L4 L5 L6 L7 L8 L9 >file &&
+ git add file &&
+ test_tick &&
+ git commit -m A &&
+ git tag A &&
+
+ test_write_lines BB L2 L3 L4 L5 L6 L7 L8 L9 >file &&
+ git add file &&
+ test_tick &&
+ git commit -m B &&
+ git tag B &&
+
+ git reset --hard A &&
+ test_write_lines L1 L2 L3 L4 L5 L6 L7 L8 CC >file &&
+ git add file &&
+ test_tick &&
+ git commit -m C &&
+ git tag C &&
+
+ test_merge M B &&
+ git blame --line-porcelain file --ignore-rev M >blame_raw &&
+
+ grep -E "^[0-9a-f]+ [0-9]+ 1" blame_raw | sed -e "s/ .*//" >actual &&
+ git rev-parse B >expect &&
+ test_cmp expect actual &&
+
+ grep -E "^[0-9a-f]+ [0-9]+ 9" blame_raw | sed -e "s/ .*//" >actual &&
+ git rev-parse C >expect &&
+ test_cmp expect actual
+'
+
+test_done
diff --git a/t/t8014-blame-ignore-fuzzy.sh b/t/t8014-blame-ignore-fuzzy.sh
new file mode 100755
index 0000000..0bd0341
--- /dev/null
+++ b/t/t8014-blame-ignore-fuzzy.sh
@@ -0,0 +1,437 @@
+#!/bin/sh
+
+test_description='git blame ignore fuzzy heuristic'
+. ./test-lib.sh
+
+pick_author='s/^[0-9a-f^]* *(\([^ ]*\) .*/\1/'
+
+# Each test is composed of 4 variables:
+# titleN - the test name
+# aN - the initial content
+# bN - the final content
+# expectedN - the line numbers from aN that we expect git blame
+# on bN to identify, or "Final" if bN itself should
+# be identified as the origin of that line.
+
+# We start at test 2 because setup will show as test 1
+title2="Regression test for partially overlapping search ranges"
+cat <<EOF >a2
+1
+2
+3
+abcdef
+5
+6
+7
+ijkl
+9
+10
+11
+pqrs
+13
+14
+15
+wxyz
+17
+18
+19
+EOF
+cat <<EOF >b2
+abcde
+ijk
+pqr
+wxy
+EOF
+cat <<EOF >expected2
+4
+8
+12
+16
+EOF
+
+title3="Combine 3 lines into 2"
+cat <<EOF >a3
+if ((maxgrow==0) ||
+ ( single_line_field && (field->dcols < maxgrow)) ||
+ (!single_line_field && (field->drows < maxgrow)))
+EOF
+cat <<EOF >b3
+if ((maxgrow == 0) || (single_line_field && (field->dcols < maxgrow)) ||
+ (!single_line_field && (field->drows < maxgrow))) {
+EOF
+cat <<EOF >expected3
+2
+3
+EOF
+
+title4="Add curly brackets"
+cat <<EOF >a4
+ if (rows) *rows = field->rows;
+ if (cols) *cols = field->cols;
+ if (frow) *frow = field->frow;
+ if (fcol) *fcol = field->fcol;
+EOF
+cat <<EOF >b4
+ if (rows) {
+ *rows = field->rows;
+ }
+ if (cols) {
+ *cols = field->cols;
+ }
+ if (frow) {
+ *frow = field->frow;
+ }
+ if (fcol) {
+ *fcol = field->fcol;
+ }
+EOF
+cat <<EOF >expected4
+1
+1
+Final
+2
+2
+Final
+3
+3
+Final
+4
+4
+Final
+EOF
+
+
+title5="Combine many lines and change case"
+cat <<EOF >a5
+for(row=0,pBuffer=field->buf;
+ row<height;
+ row++,pBuffer+=width )
+{
+ if ((len = (int)( After_End_Of_Data( pBuffer, width ) - pBuffer )) > 0)
+ {
+ wmove( win, row, 0 );
+ waddnstr( win, pBuffer, len );
+EOF
+cat <<EOF >b5
+for (Row = 0, PBuffer = field->buf; Row < Height; Row++, PBuffer += Width) {
+ if ((Len = (int)(afterEndOfData(PBuffer, Width) - PBuffer)) > 0) {
+ wmove(win, Row, 0);
+ waddnstr(win, PBuffer, Len);
+EOF
+cat <<EOF >expected5
+1
+5
+7
+8
+EOF
+
+title6="Rename and combine lines"
+cat <<EOF >a6
+bool need_visual_update = ((form != (FORM *)0) &&
+ (form->status & _POSTED) &&
+ (form->current==field));
+
+if (need_visual_update)
+ Synchronize_Buffer(form);
+
+if (single_line_field)
+{
+ growth = field->cols * amount;
+ if (field->maxgrow)
+ growth = Minimum(field->maxgrow - field->dcols,growth);
+ field->dcols += growth;
+ if (field->dcols == field->maxgrow)
+EOF
+cat <<EOF >b6
+bool NeedVisualUpdate = ((Form != (FORM *)0) && (Form->status & _POSTED) &&
+ (Form->current == field));
+
+if (NeedVisualUpdate) {
+ synchronizeBuffer(Form);
+}
+
+if (SingleLineField) {
+ Growth = field->cols * amount;
+ if (field->maxgrow) {
+ Growth = Minimum(field->maxgrow - field->dcols, Growth);
+ }
+ field->dcols += Growth;
+ if (field->dcols == field->maxgrow) {
+EOF
+cat <<EOF >expected6
+1
+3
+4
+5
+6
+Final
+7
+8
+10
+11
+12
+Final
+13
+14
+EOF
+
+# Both lines match identically so position must be used to tie-break.
+title7="Same line twice"
+cat <<EOF >a7
+abc
+abc
+EOF
+cat <<EOF >b7
+abcd
+abcd
+EOF
+cat <<EOF >expected7
+1
+2
+EOF
+
+title8="Enforce line order"
+cat <<EOF >a8
+abcdef
+ghijkl
+ab
+EOF
+cat <<EOF >b8
+ghijk
+abcd
+EOF
+cat <<EOF >expected8
+2
+3
+EOF
+
+title9="Expand lines and rename variables"
+cat <<EOF >a9
+int myFunction(int ArgumentOne, Thing *ArgTwo, Blah XuglyBug) {
+ Squiggle FabulousResult = squargle(ArgumentOne, *ArgTwo,
+ XuglyBug) + EwwwGlobalWithAReallyLongNameYepTooLong;
+ return FabulousResult * 42;
+}
+EOF
+cat <<EOF >b9
+int myFunction(int argument_one, Thing *arg_asdfgh,
+ Blah xugly_bug) {
+ Squiggle fabulous_result = squargle(argument_one,
+ *arg_asdfgh, xugly_bug)
+ + g_ewww_global_with_a_really_long_name_yep_too_long;
+ return fabulous_result * 42;
+}
+EOF
+cat <<EOF >expected9
+1
+1
+2
+3
+3
+4
+5
+EOF
+
+title10="Two close matches versus one less close match"
+cat <<EOF >a10
+abcdef
+abcdef
+ghijkl
+EOF
+cat <<EOF >b10
+gh
+abcdefx
+EOF
+cat <<EOF >expected10
+Final
+2
+EOF
+
+# The first line of b matches best with the last line of a, but the overall
+# match is better if we match it with the first line of a.
+title11="Piggy in the middle"
+cat <<EOF >a11
+abcdefg
+ijklmn
+abcdefgh
+EOF
+cat <<EOF >b11
+abcdefghx
+ijklm
+EOF
+cat <<EOF >expected11
+1
+2
+EOF
+
+title12="No trailing newline"
+printf "abc\ndef" >a12
+printf "abx\nstu" >b12
+cat <<EOF >expected12
+1
+Final
+EOF
+
+title13="Reorder includes"
+cat <<EOF >a13
+#include "c.h"
+#include "b.h"
+#include "a.h"
+#include "e.h"
+#include "d.h"
+EOF
+cat <<EOF >b13
+#include "a.h"
+#include "b.h"
+#include "c.h"
+#include "d.h"
+#include "e.h"
+EOF
+cat <<EOF >expected13
+3
+2
+1
+5
+4
+EOF
+
+last_test=13
+
+test_expect_success setup '
+ for i in $(test_seq 2 $last_test)
+ do
+ # Append each line in a separate commit to make it easy to
+ # check which original line the blame output relates to.
+
+ line_count=0 &&
+ while IFS= read line
+ do
+ line_count=$((line_count+1)) &&
+ echo "$line" >>"$i" &&
+ git add "$i" &&
+ test_tick &&
+ GIT_AUTHOR_NAME="$line_count" git commit -m "$line_count" || return 1
+ done <"a$i"
+ done &&
+
+ for i in $(test_seq 2 $last_test)
+ do
+ # Overwrite the files with the final content.
+ cp b$i $i &&
+ git add $i || return 1
+ done &&
+ test_tick &&
+
+ # Commit the final content all at once so it can all be
+ # referred to with the same commit ID.
+ GIT_AUTHOR_NAME=Final git commit -m Final &&
+
+ IGNOREME=$(git rev-parse HEAD)
+'
+
+for i in $(test_seq 2 $last_test); do
+ eval title="\$title$i"
+ test_expect_success "$title" \
+ "git blame -M9 --ignore-rev $IGNOREME $i >output &&
+ sed -e \"$pick_author\" output >actual &&
+ test_cmp expected$i actual"
+done
+
+# This invoked a null pointer dereference when the chunk callback was called
+# with a zero length parent chunk and there were no more suspects.
+test_expect_success 'Diff chunks with no suspects' '
+ test_write_lines xy1 A B C xy1 >file &&
+ git add file &&
+ test_tick &&
+ GIT_AUTHOR_NAME=1 git commit -m 1 &&
+
+ test_write_lines xy2 A B xy2 C xy2 >file &&
+ git add file &&
+ test_tick &&
+ GIT_AUTHOR_NAME=2 git commit -m 2 &&
+ REV_2=$(git rev-parse HEAD) &&
+
+ test_write_lines xy3 A >file &&
+ git add file &&
+ test_tick &&
+ GIT_AUTHOR_NAME=3 git commit -m 3 &&
+ REV_3=$(git rev-parse HEAD) &&
+
+ test_write_lines 1 1 >expected &&
+
+ git blame --ignore-rev $REV_2 --ignore-rev $REV_3 file >output &&
+ sed -e "$pick_author" output >actual &&
+
+ test_cmp expected actual
+ '
+
+test_expect_success 'position matching' '
+ test_write_lines abc def >file2 &&
+ git add file2 &&
+ test_tick &&
+ GIT_AUTHOR_NAME=1 git commit -m 1 &&
+
+ test_write_lines abc def abc def >file2 &&
+ git add file2 &&
+ test_tick &&
+ GIT_AUTHOR_NAME=2 git commit -m 2 &&
+
+ test_write_lines abcx defx abcx defx >file2 &&
+ git add file2 &&
+ test_tick &&
+ GIT_AUTHOR_NAME=3 git commit -m 3 &&
+ REV_3=$(git rev-parse HEAD) &&
+
+ test_write_lines abcy defy abcx defx >file2 &&
+ git add file2 &&
+ test_tick &&
+ GIT_AUTHOR_NAME=4 git commit -m 4 &&
+ REV_4=$(git rev-parse HEAD) &&
+
+ test_write_lines 1 1 2 2 >expected &&
+
+ git blame --ignore-rev $REV_3 --ignore-rev $REV_4 file2 >output &&
+ sed -e "$pick_author" output >actual &&
+
+ test_cmp expected actual
+ '
+
+# This fails if each blame entry is processed independently instead of
+# processing each diff change in full.
+test_expect_success 'preserve order' '
+ test_write_lines bcde >file3 &&
+ git add file3 &&
+ test_tick &&
+ GIT_AUTHOR_NAME=1 git commit -m 1 &&
+
+ test_write_lines bcde fghij >file3 &&
+ git add file3 &&
+ test_tick &&
+ GIT_AUTHOR_NAME=2 git commit -m 2 &&
+
+ test_write_lines bcde fghij abcd >file3 &&
+ git add file3 &&
+ test_tick &&
+ GIT_AUTHOR_NAME=3 git commit -m 3 &&
+
+ test_write_lines abcdx fghijx bcdex >file3 &&
+ git add file3 &&
+ test_tick &&
+ GIT_AUTHOR_NAME=4 git commit -m 4 &&
+ REV_4=$(git rev-parse HEAD) &&
+
+ test_write_lines abcdx fghijy bcdex >file3 &&
+ git add file3 &&
+ test_tick &&
+ GIT_AUTHOR_NAME=5 git commit -m 5 &&
+ REV_5=$(git rev-parse HEAD) &&
+
+ test_write_lines 1 2 3 >expected &&
+
+ git blame --ignore-rev $REV_4 --ignore-rev $REV_5 file3 >output &&
+ sed -e "$pick_author" output >actual &&
+
+ test_cmp expected actual
+ '
+
+test_done
diff --git a/t/t9001-send-email.sh b/t/t9001-send-email.sh
new file mode 100755
index 0000000..5a77100
--- /dev/null
+++ b/t/t9001-send-email.sh
@@ -0,0 +1,2568 @@
+#!/bin/sh
+
+test_description='git send-email'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+# May be altered later in the test
+PREREQ="PERL"
+
+replace_variable_fields () {
+ sed -e "s/^\(Date:\).*/\1 DATE-STRING/" \
+ -e "s/^\(Message-ID:\).*/\1 MESSAGE-ID-STRING/" \
+ -e "s/^\(X-Mailer:\).*/\1 X-MAILER-STRING/"
+}
+
+test_expect_success $PREREQ 'prepare reference tree' '
+ echo "1A quick brown fox jumps over the" >file &&
+ echo "lazy dog" >>file &&
+ git add file &&
+ GIT_AUTHOR_NAME="A" git commit -a -m "Initial."
+'
+
+test_expect_success $PREREQ 'Setup helper tool' '
+ write_script fake.sendmail <<-\EOF &&
+ shift
+ output=1
+ while test -f commandline$output
+ do
+ output=$(($output+1))
+ done
+ for a
+ do
+ echo "!$a!"
+ done >commandline$output
+ cat >"msgtxt$output"
+ EOF
+ git add fake.sendmail &&
+ GIT_AUTHOR_NAME="A" git commit -a -m "Second."
+'
+
+clean_fake_sendmail () {
+ rm -f commandline* msgtxt*
+}
+
+test_expect_success $PREREQ 'Extract patches' '
+ patches=$(git format-patch -s --cc="One <one@example.com>" --cc=two@example.com -n HEAD^1) &&
+ threaded_patches=$(git format-patch -o threaded --thread=shallow -s --in-reply-to="format" HEAD^1)
+'
+
+# Test no confirm early to ensure remaining tests will not hang
+test_no_confirm () {
+ rm -f no_confirm_okay
+ echo n | \
+ GIT_SEND_EMAIL_NOTTY=1 \
+ git send-email \
+ --from="Example <from@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ $@ \
+ $patches >stdout &&
+ ! grep "Send this email" stdout &&
+ >no_confirm_okay
+}
+
+# Exit immediately to prevent hang if a no-confirm test fails
+check_no_confirm () {
+ if ! test -f no_confirm_okay
+ then
+ say 'confirm test failed; skipping remaining tests to prevent hanging'
+ PREREQ="$PREREQ,CHECK_NO_CONFIRM"
+ fi
+ return 0
+}
+
+test_expect_success $PREREQ 'No confirm with --suppress-cc' '
+ test_no_confirm --suppress-cc=sob &&
+ check_no_confirm
+'
+
+
+test_expect_success $PREREQ 'No confirm with --confirm=never' '
+ test_no_confirm --confirm=never &&
+ check_no_confirm
+'
+
+# leave sendemail.confirm set to never after this so that none of the
+# remaining tests prompt unintentionally.
+test_expect_success $PREREQ 'No confirm with sendemail.confirm=never' '
+ git config sendemail.confirm never &&
+ test_no_confirm --compose --subject=foo &&
+ check_no_confirm
+'
+
+test_expect_success $PREREQ 'Send patches' '
+ git send-email --suppress-cc=sob --from="Example <nobody@example.com>" --to=nobody@example.com --smtp-server="$(pwd)/fake.sendmail" $patches 2>errors
+'
+
+test_expect_success $PREREQ 'setup expect' '
+ cat >expected <<-\EOF
+ !nobody@example.com!
+ !author@example.com!
+ !one@example.com!
+ !two@example.com!
+ EOF
+'
+
+test_expect_success $PREREQ 'Verify commandline' '
+ test_cmp expected commandline1
+'
+
+test_expect_success $PREREQ 'Send patches with --envelope-sender' '
+ clean_fake_sendmail &&
+ git send-email --envelope-sender="Patch Contributor <patch@example.com>" --suppress-cc=sob --from="Example <nobody@example.com>" --to=nobody@example.com --smtp-server="$(pwd)/fake.sendmail" $patches 2>errors
+'
+
+test_expect_success $PREREQ 'setup expect' '
+ cat >expected <<-\EOF
+ !patch@example.com!
+ !-i!
+ !nobody@example.com!
+ !author@example.com!
+ !one@example.com!
+ !two@example.com!
+ EOF
+'
+
+test_expect_success $PREREQ 'Verify commandline' '
+ test_cmp expected commandline1
+'
+
+test_expect_success $PREREQ 'Send patches with --envelope-sender=auto' '
+ clean_fake_sendmail &&
+ git send-email --envelope-sender=auto --suppress-cc=sob --from="Example <nobody@example.com>" --to=nobody@example.com --smtp-server="$(pwd)/fake.sendmail" $patches 2>errors
+'
+
+test_expect_success $PREREQ 'setup expect' '
+ cat >expected <<-\EOF
+ !nobody@example.com!
+ !-i!
+ !nobody@example.com!
+ !author@example.com!
+ !one@example.com!
+ !two@example.com!
+ EOF
+'
+
+test_expect_success $PREREQ 'Verify commandline' '
+ test_cmp expected commandline1
+'
+
+test_expect_success $PREREQ 'setup expect for cc trailer' "
+cat >expected-cc <<\EOF
+!recipient@example.com!
+!author@example.com!
+!one@example.com!
+!two@example.com!
+!three@example.com!
+!four@example.com!
+!five@example.com!
+!six@example.com!
+EOF
+"
+
+test_expect_success $PREREQ 'cc trailer with various syntax' '
+ test_commit cc-trailer &&
+ test_when_finished "git reset --hard HEAD^" &&
+ git commit --amend -F - <<-EOF &&
+ Test Cc: trailers.
+
+ Cc: one@example.com
+ Cc: <two@example.com> # trailing comments are ignored
+ Cc: <three@example.com>, <not.four@example.com> one address per line
+ Cc: "Some # Body" <four@example.com> [ <also.a.comment> ]
+ Cc: five@example.com # not.six@example.com
+ Cc: six@example.com, not.seven@example.com
+ EOF
+ clean_fake_sendmail &&
+ git send-email -1 --to=recipient@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" &&
+ test_cmp expected-cc commandline1
+'
+
+test_expect_success $PREREQ 'setup fake get_maintainer.pl script for cc trailer' "
+ write_script expected-cc-script.sh <<-EOF
+ echo 'One Person <one@example.com> (supporter:THIS (FOO/bar))'
+ echo 'Two Person <two@example.com> (maintainer:THIS THING)'
+ echo 'Third List <three@example.com> (moderated list:THIS THING (FOO/bar))'
+ echo '<four@example.com> (moderated list:FOR THING)'
+ echo 'five@example.com (open list:FOR THING (FOO/bar))'
+ echo 'six@example.com (open list)'
+ EOF
+"
+
+test_expect_success $PREREQ 'cc trailer with get_maintainer.pl output' '
+ clean_fake_sendmail &&
+ git send-email -1 --to=recipient@example.com \
+ --cc-cmd=./expected-cc-script.sh \
+ --smtp-server="$(pwd)/fake.sendmail" &&
+ test_cmp expected-cc commandline1
+'
+
+test_expect_success $PREREQ 'setup expect' "
+cat >expected-show-all-headers <<\EOF
+0001-Second.patch
+(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
+(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
+(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
+Dry-OK. Log says:
+Server: relay.example.com
+MAIL FROM:<from@example.com>
+RCPT TO:<to@example.com>
+RCPT TO:<cc@example.com>
+RCPT TO:<author@example.com>
+RCPT TO:<one@example.com>
+RCPT TO:<two@example.com>
+RCPT TO:<bcc@example.com>
+From: Example <from@example.com>
+To: to@example.com
+Cc: cc@example.com,
+ A <author@example.com>,
+ One <one@example.com>,
+ two@example.com
+Subject: [PATCH 1/1] Second.
+Date: DATE-STRING
+Message-ID: MESSAGE-ID-STRING
+X-Mailer: X-MAILER-STRING
+In-Reply-To: <unique-message-id@example.com>
+References: <unique-message-id@example.com>
+Reply-To: Reply <reply@example.com>
+MIME-Version: 1.0
+Content-Transfer-Encoding: 8bit
+
+Result: OK
+EOF
+"
+
+test_suppress_self () {
+ test_commit $3 &&
+ test_when_finished "git reset --hard HEAD^" &&
+
+ write_script cccmd-sed <<-EOF &&
+ sed -n -e s/^cccmd--//p "\$1"
+ EOF
+
+ git commit --amend --author="$1 <$2>" -F - &&
+ clean_fake_sendmail &&
+ git format-patch --stdout -1 >"suppress-self-$3.patch" &&
+
+ git send-email --from="$1 <$2>" \
+ --to=nobody@example.com \
+ --cc-cmd=./cccmd-sed \
+ --suppress-cc=self \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ suppress-self-$3.patch &&
+
+ mv msgtxt1 msgtxt1-$3 &&
+ sed -e '/^$/q' msgtxt1-$3 >"msghdr1-$3" &&
+
+ (grep '^Cc:' msghdr1-$3 >"actual-no-cc-$3";
+ test_must_be_empty actual-no-cc-$3)
+}
+
+test_suppress_self_unquoted () {
+ test_suppress_self "$1" "$2" "unquoted-$3" <<-EOF
+ test suppress-cc.self unquoted-$3 with name $1 email $2
+
+ unquoted-$3
+
+ cccmd--$1 <$2>
+
+ Cc: $1 <$2>
+ Signed-off-by: $1 <$2>
+ EOF
+}
+
+test_suppress_self_quoted () {
+ test_suppress_self "$1" "$2" "quoted-$3" <<-EOF
+ test suppress-cc.self quoted-$3 with name $1 email $2
+
+ quoted-$3
+
+ cccmd--"$1" <$2>
+
+ Cc: $1 <$2>
+ Cc: "$1" <$2>
+ Signed-off-by: $1 <$2>
+ Signed-off-by: "$1" <$2>
+ EOF
+}
+
+test_expect_success $PREREQ 'self name is suppressed' "
+ test_suppress_self_unquoted 'A U Thor' 'author@example.com' \
+ 'self_name_suppressed'
+"
+
+test_expect_success $PREREQ 'self name with dot is suppressed' "
+ test_suppress_self_quoted 'A U. Thor' 'author@example.com' \
+ 'self_name_dot_suppressed'
+"
+
+test_expect_success $PREREQ 'non-ascii self name is suppressed' "
+ test_suppress_self_quoted 'Füñný Nâmé' 'odd_?=mail@example.com' \
+ 'non_ascii_self_suppressed'
+"
+
+# This name is long enough to force format-patch to split it into multiple
+# encoded-words, assuming it uses UTF-8 with the "Q" encoding.
+test_expect_success $PREREQ 'long non-ascii self name is suppressed' "
+ test_suppress_self_quoted 'Ƒüñníęř €. Nâá¹Ã©' 'odd_?=mail@example.com' \
+ 'long_non_ascii_self_suppressed'
+"
+
+test_expect_success $PREREQ 'sanitized self name is suppressed' "
+ test_suppress_self_unquoted '\"A U. Thor\"' 'author@example.com' \
+ 'self_name_sanitized_suppressed'
+"
+
+test_expect_success $PREREQ 'Show all headers' '
+ git send-email \
+ --dry-run \
+ --suppress-cc=sob \
+ --from="Example <from@example.com>" \
+ --reply-to="Reply <reply@example.com>" \
+ --to=to@example.com \
+ --cc=cc@example.com \
+ --bcc=bcc@example.com \
+ --in-reply-to="<unique-message-id@example.com>" \
+ --smtp-server relay.example.com \
+ $patches | replace_variable_fields \
+ >actual-show-all-headers &&
+ test_cmp expected-show-all-headers actual-show-all-headers
+'
+
+test_expect_success $PREREQ 'Prompting works' '
+ clean_fake_sendmail &&
+ (echo "to@example.com" &&
+ echo "my-message-id@example.com"
+ ) | GIT_SEND_EMAIL_NOTTY=1 git send-email \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ $patches \
+ 2>errors &&
+ grep "^From: A U Thor <author@example.com>\$" msgtxt1 &&
+ grep "^To: to@example.com\$" msgtxt1 &&
+ grep "^In-Reply-To: <my-message-id@example.com>" msgtxt1
+'
+
+test_expect_success $PREREQ,AUTOIDENT 'implicit ident is allowed' '
+ clean_fake_sendmail &&
+ (sane_unset GIT_AUTHOR_NAME &&
+ sane_unset GIT_AUTHOR_EMAIL &&
+ sane_unset GIT_COMMITTER_NAME &&
+ sane_unset GIT_COMMITTER_EMAIL &&
+ GIT_SEND_EMAIL_NOTTY=1 git send-email \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ --to=to@example.com \
+ $patches </dev/null 2>errors
+ )
+'
+
+test_expect_success $PREREQ,!AUTOIDENT 'broken implicit ident aborts send-email' '
+ clean_fake_sendmail &&
+ (sane_unset GIT_AUTHOR_NAME &&
+ sane_unset GIT_AUTHOR_EMAIL &&
+ sane_unset GIT_COMMITTER_NAME &&
+ sane_unset GIT_COMMITTER_EMAIL &&
+ GIT_SEND_EMAIL_NOTTY=1 && export GIT_SEND_EMAIL_NOTTY &&
+ test_must_fail git send-email \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ --to=to@example.com \
+ $patches </dev/null 2>errors &&
+ test_grep "tell me who you are" errors
+ )
+'
+
+test_expect_success $PREREQ 'setup cmd scripts' '
+ write_script tocmd-sed <<-\EOF &&
+ sed -n -e "s/^tocmd--//p" "$1"
+ EOF
+ write_script cccmd-sed <<-\EOF &&
+ sed -n -e "s/^cccmd--//p" "$1"
+ EOF
+ write_script headercmd-sed <<-\EOF
+ sed -n -e "s/^headercmd--//p" "$1"
+ EOF
+'
+
+test_expect_success $PREREQ 'tocmd works' '
+ clean_fake_sendmail &&
+ cp $patches tocmd.patch &&
+ echo tocmd--tocmd@example.com >>tocmd.patch &&
+ git send-email \
+ --from="Example <nobody@example.com>" \
+ --to-cmd=./tocmd-sed \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ tocmd.patch \
+ &&
+ grep "^To: tocmd@example.com" msgtxt1
+'
+
+test_expect_success $PREREQ 'cccmd works' '
+ clean_fake_sendmail &&
+ cp $patches cccmd.patch &&
+ echo "cccmd-- cccmd@example.com" >>cccmd.patch &&
+ git send-email \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --cc-cmd=./cccmd-sed \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ cccmd.patch \
+ &&
+ grep "^ cccmd@example.com" msgtxt1
+'
+
+test_expect_success $PREREQ 'headercmd works' '
+ clean_fake_sendmail &&
+ cp $patches headercmd.patch &&
+ echo "headercmd--X-Debbugs-CC: dummy@example.com" >>headercmd.patch &&
+ git send-email \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --header-cmd=./headercmd-sed \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ headercmd.patch \
+ &&
+ grep "^X-Debbugs-CC: dummy@example.com" msgtxt1
+'
+
+test_expect_success $PREREQ '--no-header-cmd works' '
+ clean_fake_sendmail &&
+ cp $patches headercmd.patch &&
+ echo "headercmd--X-Debbugs-CC: dummy@example.com" >>headercmd.patch &&
+ git send-email \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --header-cmd=./headercmd-sed \
+ --no-header-cmd \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ headercmd.patch \
+ &&
+ ! grep "^X-Debbugs-CC: dummy@example.com" msgtxt1
+'
+
+test_expect_success $PREREQ 'multiline fields are correctly unfolded' '
+ clean_fake_sendmail &&
+ cp $patches headercmd.patch &&
+ write_script headercmd-multiline <<-\EOF &&
+ echo "X-Debbugs-CC: someone@example.com
+FoldedField: This is a tale
+ best told using
+ multiple lines."
+ EOF
+ git send-email \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --header-cmd=./headercmd-multiline \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ headercmd.patch &&
+ grep "^FoldedField: This is a tale best told using multiple lines.$" msgtxt1
+'
+
+# Blank lines in the middle of the output of a command are invalid.
+test_expect_success $PREREQ 'malform output reported on blank lines in command output' '
+ clean_fake_sendmail &&
+ cp $patches headercmd.patch &&
+ write_script headercmd-malformed-output <<-\EOF &&
+ echo "X-Debbugs-CC: someone@example.com
+
+SomeOtherField: someone-else@example.com"
+ EOF
+ ! git send-email \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --header-cmd=./headercmd-malformed-output \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ headercmd.patch
+'
+
+test_expect_success $PREREQ 'reject long lines' '
+ z8=zzzzzzzz &&
+ z64=$z8$z8$z8$z8$z8$z8$z8$z8 &&
+ z512=$z64$z64$z64$z64$z64$z64$z64$z64 &&
+ clean_fake_sendmail &&
+ cp $patches longline.patch &&
+ cat >>longline.patch <<-EOF &&
+ $z512$z512
+ not a long line
+ $z512$z512
+ EOF
+ test_must_fail git send-email \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ --transfer-encoding=8bit \
+ $patches longline.patch \
+ 2>actual &&
+ cat >expect <<-\EOF &&
+ fatal: longline.patch:35 is longer than 998 characters
+ warning: no patches were sent
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success $PREREQ 'no patch was sent' '
+ ! test -e commandline1
+'
+
+test_expect_success $PREREQ 'Author From: in message body' '
+ clean_fake_sendmail &&
+ git send-email \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ $patches &&
+ sed "1,/^\$/d" <msgtxt1 >msgbody1 &&
+ grep "From: A <author@example.com>" msgbody1
+'
+
+test_expect_success $PREREQ 'Author From: not in message body' '
+ clean_fake_sendmail &&
+ git send-email \
+ --from="A <author@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ $patches &&
+ sed "1,/^\$/d" <msgtxt1 >msgbody1 &&
+ ! grep "From: A <author@example.com>" msgbody1
+'
+
+test_expect_success $PREREQ 'allow long lines with --no-validate' '
+ git send-email \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ --no-validate \
+ $patches longline.patch \
+ 2>errors
+'
+
+test_expect_success $PREREQ 'short lines with auto encoding are 8bit' '
+ clean_fake_sendmail &&
+ git send-email \
+ --from="A <author@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ --transfer-encoding=auto \
+ $patches &&
+ grep "Content-Transfer-Encoding: 8bit" msgtxt1
+'
+
+test_expect_success $PREREQ 'long lines with auto encoding are quoted-printable' '
+ clean_fake_sendmail &&
+ git send-email \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ --transfer-encoding=auto \
+ --no-validate \
+ longline.patch &&
+ grep "Content-Transfer-Encoding: quoted-printable" msgtxt1
+'
+
+test_expect_success $PREREQ 'carriage returns with auto encoding are quoted-printable' '
+ clean_fake_sendmail &&
+ cp $patches cr.patch &&
+ printf "this is a line\r\n" >>cr.patch &&
+ git send-email \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ --transfer-encoding=auto \
+ --no-validate \
+ cr.patch &&
+ grep "Content-Transfer-Encoding: quoted-printable" msgtxt1
+'
+
+for enc in auto quoted-printable base64
+do
+ test_expect_success $PREREQ "--validate passes with encoding $enc" '
+ git send-email \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ --transfer-encoding=$enc \
+ --validate \
+ $patches longline.patch
+ '
+
+done
+
+test_expect_success $PREREQ "--validate respects relative core.hooksPath path" '
+ clean_fake_sendmail &&
+ mkdir my-hooks &&
+ test_when_finished "rm my-hooks.ran" &&
+ write_script my-hooks/sendemail-validate <<-\EOF &&
+ >my-hooks.ran
+ exit 1
+ EOF
+ test_config core.hooksPath "my-hooks" &&
+ test_must_fail git send-email \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ --validate \
+ longline.patch 2>actual &&
+ test_path_is_file my-hooks.ran &&
+ cat >expect <<-EOF &&
+ fatal: longline.patch: rejected by sendemail-validate hook
+ fatal: command '"'"'git hook run --ignore-missing sendemail-validate -- <patch> <header>'"'"' died with exit code 1
+ warning: no patches were sent
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success $PREREQ "--validate respects absolute core.hooksPath path" '
+ hooks_path="$(pwd)/my-hooks" &&
+ test_config core.hooksPath "$hooks_path" &&
+ test_when_finished "rm my-hooks.ran" &&
+ test_must_fail git send-email \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ --validate \
+ longline.patch 2>actual &&
+ test_path_is_file my-hooks.ran &&
+ cat >expect <<-EOF &&
+ fatal: longline.patch: rejected by sendemail-validate hook
+ fatal: command '"'"'git hook run --ignore-missing sendemail-validate -- <patch> <header>'"'"' died with exit code 1
+ warning: no patches were sent
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success $PREREQ "--validate hook supports multiple addresses in arguments" '
+ hooks_path="$(pwd)/my-hooks" &&
+ test_config core.hooksPath "$hooks_path" &&
+ test_when_finished "rm my-hooks.ran" &&
+ test_must_fail git send-email \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com,abc@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ --validate \
+ longline.patch 2>actual &&
+ test_path_is_file my-hooks.ran &&
+ cat >expect <<-EOF &&
+ fatal: longline.patch: rejected by sendemail-validate hook
+ fatal: command '"'"'git hook run --ignore-missing sendemail-validate -- <patch> <header>'"'"' died with exit code 1
+ warning: no patches were sent
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success $PREREQ "--validate hook supports header argument" '
+ write_script my-hooks/sendemail-validate <<-\EOF &&
+ if test "$#" -ge 2
+ then
+ grep "X-test-header: v1.0" "$2"
+ else
+ echo "No header arg passed"
+ exit 1
+ fi
+ EOF
+ test_config core.hooksPath "my-hooks" &&
+ rm -fr outdir &&
+ git format-patch \
+ --add-header="X-test-header: v1.0" \
+ -n HEAD^1 -o outdir &&
+ git send-email \
+ --dry-run \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ --validate \
+ outdir/000?-*.patch
+'
+
+test_expect_success $PREREQ 'clear message-id before parsing a new message' '
+ clean_fake_sendmail &&
+ echo true | write_script my-hooks/sendemail-validate &&
+ test_config core.hooksPath my-hooks &&
+ git send-email --validate --to=recipient@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ $patches $threaded_patches &&
+ id0=$(grep "^Message-ID: " $threaded_patches) &&
+ id1=$(grep "^Message-ID: " msgtxt1) &&
+ id2=$(grep "^Message-ID: " msgtxt2) &&
+ test "z$id0" = "z$id2" &&
+ test "z$id1" != "z$id2"
+'
+
+for enc in 7bit 8bit quoted-printable base64
+do
+ test_expect_success $PREREQ "--transfer-encoding=$enc produces correct header" '
+ clean_fake_sendmail &&
+ git send-email \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ --transfer-encoding=$enc \
+ $patches &&
+ grep "Content-Transfer-Encoding: $enc" msgtxt1
+ '
+done
+
+test_expect_success $PREREQ 'Invalid In-Reply-To' '
+ clean_fake_sendmail &&
+ git send-email \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --in-reply-to=" " \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ $patches \
+ 2>errors &&
+ ! grep "^In-Reply-To: < *>" msgtxt1
+'
+
+test_expect_success $PREREQ 'Valid In-Reply-To when prompting' '
+ clean_fake_sendmail &&
+ (echo "From Example <from@example.com>" &&
+ echo "To Example <to@example.com>" &&
+ echo ""
+ ) | GIT_SEND_EMAIL_NOTTY=1 git send-email \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ $patches 2>errors &&
+ ! grep "^In-Reply-To: < *>" msgtxt1
+'
+
+test_expect_success $PREREQ 'In-Reply-To without --chain-reply-to' '
+ clean_fake_sendmail &&
+ echo "<unique-message-id@example.com>" >expect &&
+ git send-email \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --no-chain-reply-to \
+ --in-reply-to="$(cat expect)" \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ $patches $patches $patches \
+ 2>errors &&
+ # The first message is a reply to --in-reply-to
+ sed -n -e "s/^In-Reply-To: *\(.*\)/\1/p" msgtxt1 >actual &&
+ test_cmp expect actual &&
+ # Second and subsequent messages are replies to the first one
+ sed -n -e "s/^Message-ID: *\(.*\)/\1/p" msgtxt1 >expect &&
+ sed -n -e "s/^In-Reply-To: *\(.*\)/\1/p" msgtxt2 >actual &&
+ test_cmp expect actual &&
+ sed -n -e "s/^In-Reply-To: *\(.*\)/\1/p" msgtxt3 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success $PREREQ 'In-Reply-To with --chain-reply-to' '
+ clean_fake_sendmail &&
+ echo "<unique-message-id@example.com>" >expect &&
+ git send-email \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --chain-reply-to \
+ --in-reply-to="$(cat expect)" \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ $patches $patches $patches \
+ 2>errors &&
+ sed -n -e "s/^In-Reply-To: *\(.*\)/\1/p" msgtxt1 >actual &&
+ test_cmp expect actual &&
+ sed -n -e "s/^Message-ID: *\(.*\)/\1/p" msgtxt1 >expect &&
+ sed -n -e "s/^In-Reply-To: *\(.*\)/\1/p" msgtxt2 >actual &&
+ test_cmp expect actual &&
+ sed -n -e "s/^Message-ID: *\(.*\)/\1/p" msgtxt2 >expect &&
+ sed -n -e "s/^In-Reply-To: *\(.*\)/\1/p" msgtxt3 >actual &&
+ test_cmp expect actual
+'
+
+test_set_editor "$(pwd)/fake-editor"
+
+test_expect_success $PREREQ 'setup erroring fake editor' '
+ write_script fake-editor <<-\EOF
+ echo >&2 "I am about to error"
+ exit 1
+ EOF
+'
+
+test_expect_success $PREREQ 'fake editor dies with error' '
+ clean_fake_sendmail &&
+ test_must_fail git send-email \
+ --compose --subject foo \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ $patches 2>err &&
+ grep "I am about to error" err &&
+ grep "the editor exited uncleanly, aborting everything" err
+'
+
+test_expect_success $PREREQ 'setup fake editor' '
+ write_script fake-editor <<-\EOF
+ echo fake edit >>"$1"
+ EOF
+'
+
+test_expect_success $PREREQ '--compose works' '
+ clean_fake_sendmail &&
+ git send-email \
+ --compose --subject foo \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ $patches \
+ 2>errors
+'
+
+test_expect_success $PREREQ 'first message is compose text' '
+ grep "^fake edit" msgtxt1
+'
+
+test_expect_success $PREREQ 'second message is patch' '
+ grep "Subject:.*Second" msgtxt2
+'
+
+test_expect_success $PREREQ 'setup expect' "
+cat >expected-suppress-sob <<\EOF
+0001-Second.patch
+(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
+(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
+(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
+Dry-OK. Log says:
+Server: relay.example.com
+MAIL FROM:<from@example.com>
+RCPT TO:<to@example.com>
+RCPT TO:<cc@example.com>
+RCPT TO:<author@example.com>
+RCPT TO:<one@example.com>
+RCPT TO:<two@example.com>
+From: Example <from@example.com>
+To: to@example.com
+Cc: cc@example.com,
+ A <author@example.com>,
+ One <one@example.com>,
+ two@example.com
+Subject: [PATCH 1/1] Second.
+Date: DATE-STRING
+Message-ID: MESSAGE-ID-STRING
+X-Mailer: X-MAILER-STRING
+MIME-Version: 1.0
+Content-Transfer-Encoding: 8bit
+
+Result: OK
+EOF
+"
+
+test_suppression () {
+ git send-email \
+ --dry-run \
+ --suppress-cc=$1 ${2+"--suppress-cc=$2"} \
+ --from="Example <from@example.com>" \
+ --to=to@example.com \
+ --smtp-server relay.example.com \
+ $patches | replace_variable_fields \
+ >actual-suppress-$1${2+"-$2"} &&
+ test_cmp expected-suppress-$1${2+"-$2"} actual-suppress-$1${2+"-$2"}
+}
+
+test_expect_success $PREREQ 'sendemail.cc set' '
+ git config sendemail.cc cc@example.com &&
+ test_suppression sob
+'
+
+test_expect_success $PREREQ 'setup expect' "
+cat >expected-suppress-sob <<\EOF
+0001-Second.patch
+(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
+(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
+(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
+Dry-OK. Log says:
+Server: relay.example.com
+MAIL FROM:<from@example.com>
+RCPT TO:<to@example.com>
+RCPT TO:<author@example.com>
+RCPT TO:<one@example.com>
+RCPT TO:<two@example.com>
+From: Example <from@example.com>
+To: to@example.com
+Cc: A <author@example.com>,
+ One <one@example.com>,
+ two@example.com
+Subject: [PATCH 1/1] Second.
+Date: DATE-STRING
+Message-ID: MESSAGE-ID-STRING
+X-Mailer: X-MAILER-STRING
+MIME-Version: 1.0
+Content-Transfer-Encoding: 8bit
+
+Result: OK
+EOF
+"
+
+test_expect_success $PREREQ 'sendemail.cc unset' '
+ git config --unset sendemail.cc &&
+ test_suppression sob
+'
+
+test_expect_success $PREREQ 'setup expect' "
+cat >expected-suppress-cccmd <<\EOF
+0001-Second.patch
+(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
+(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
+(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
+(body) Adding cc: C O Mitter <committer@example.com> from line 'Signed-off-by: C O Mitter <committer@example.com>'
+Dry-OK. Log says:
+Server: relay.example.com
+MAIL FROM:<from@example.com>
+RCPT TO:<to@example.com>
+RCPT TO:<author@example.com>
+RCPT TO:<one@example.com>
+RCPT TO:<two@example.com>
+RCPT TO:<committer@example.com>
+From: Example <from@example.com>
+To: to@example.com
+Cc: A <author@example.com>,
+ One <one@example.com>,
+ two@example.com,
+ C O Mitter <committer@example.com>
+Subject: [PATCH 1/1] Second.
+Date: DATE-STRING
+Message-ID: MESSAGE-ID-STRING
+X-Mailer: X-MAILER-STRING
+MIME-Version: 1.0
+Content-Transfer-Encoding: 8bit
+
+Result: OK
+EOF
+"
+
+test_expect_success $PREREQ 'sendemail.cccmd' '
+ write_script cccmd <<-\EOF &&
+ echo cc-cmd@example.com
+ EOF
+ git config sendemail.cccmd ./cccmd &&
+ test_suppression cccmd
+'
+
+test_expect_success $PREREQ 'setup expect' '
+cat >expected-suppress-all <<\EOF
+0001-Second.patch
+Dry-OK. Log says:
+Server: relay.example.com
+MAIL FROM:<from@example.com>
+RCPT TO:<to@example.com>
+From: Example <from@example.com>
+To: to@example.com
+Subject: [PATCH 1/1] Second.
+Date: DATE-STRING
+Message-ID: MESSAGE-ID-STRING
+X-Mailer: X-MAILER-STRING
+MIME-Version: 1.0
+Content-Transfer-Encoding: 8bit
+
+Result: OK
+EOF
+'
+
+test_expect_success $PREREQ '--suppress-cc=all' '
+ test_suppression all
+'
+
+test_expect_success $PREREQ 'setup expect' "
+cat >expected-suppress-body <<\EOF
+0001-Second.patch
+(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
+(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
+(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
+(cc-cmd) Adding cc: cc-cmd@example.com from: './cccmd'
+Dry-OK. Log says:
+Server: relay.example.com
+MAIL FROM:<from@example.com>
+RCPT TO:<to@example.com>
+RCPT TO:<author@example.com>
+RCPT TO:<one@example.com>
+RCPT TO:<two@example.com>
+RCPT TO:<cc-cmd@example.com>
+From: Example <from@example.com>
+To: to@example.com
+Cc: A <author@example.com>,
+ One <one@example.com>,
+ two@example.com,
+ cc-cmd@example.com
+Subject: [PATCH 1/1] Second.
+Date: DATE-STRING
+Message-ID: MESSAGE-ID-STRING
+X-Mailer: X-MAILER-STRING
+MIME-Version: 1.0
+Content-Transfer-Encoding: 8bit
+
+Result: OK
+EOF
+"
+
+test_expect_success $PREREQ '--suppress-cc=body' '
+ test_suppression body
+'
+
+test_expect_success $PREREQ 'setup expect' "
+cat >expected-suppress-body-cccmd <<\EOF
+0001-Second.patch
+(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
+(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
+(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
+Dry-OK. Log says:
+Server: relay.example.com
+MAIL FROM:<from@example.com>
+RCPT TO:<to@example.com>
+RCPT TO:<author@example.com>
+RCPT TO:<one@example.com>
+RCPT TO:<two@example.com>
+From: Example <from@example.com>
+To: to@example.com
+Cc: A <author@example.com>,
+ One <one@example.com>,
+ two@example.com
+Subject: [PATCH 1/1] Second.
+Date: DATE-STRING
+Message-ID: MESSAGE-ID-STRING
+X-Mailer: X-MAILER-STRING
+MIME-Version: 1.0
+Content-Transfer-Encoding: 8bit
+
+Result: OK
+EOF
+"
+
+test_expect_success $PREREQ '--suppress-cc=body --suppress-cc=cccmd' '
+ test_suppression body cccmd
+'
+
+test_expect_success $PREREQ 'setup expect' "
+cat >expected-suppress-sob <<\EOF
+0001-Second.patch
+(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
+(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
+(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
+Dry-OK. Log says:
+Server: relay.example.com
+MAIL FROM:<from@example.com>
+RCPT TO:<to@example.com>
+RCPT TO:<author@example.com>
+RCPT TO:<one@example.com>
+RCPT TO:<two@example.com>
+From: Example <from@example.com>
+To: to@example.com
+Cc: A <author@example.com>,
+ One <one@example.com>,
+ two@example.com
+Subject: [PATCH 1/1] Second.
+Date: DATE-STRING
+Message-ID: MESSAGE-ID-STRING
+X-Mailer: X-MAILER-STRING
+MIME-Version: 1.0
+Content-Transfer-Encoding: 8bit
+
+Result: OK
+EOF
+"
+
+test_expect_success $PREREQ '--suppress-cc=sob' '
+ test_might_fail git config --unset sendemail.cccmd &&
+ test_suppression sob
+'
+
+test_expect_success $PREREQ 'setup expect' "
+cat >expected-suppress-bodycc <<\EOF
+0001-Second.patch
+(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
+(mbox) Adding cc: One <one@example.com> from line 'Cc: One <one@example.com>, two@example.com'
+(mbox) Adding cc: two@example.com from line 'Cc: One <one@example.com>, two@example.com'
+(body) Adding cc: C O Mitter <committer@example.com> from line 'Signed-off-by: C O Mitter <committer@example.com>'
+Dry-OK. Log says:
+Server: relay.example.com
+MAIL FROM:<from@example.com>
+RCPT TO:<to@example.com>
+RCPT TO:<author@example.com>
+RCPT TO:<one@example.com>
+RCPT TO:<two@example.com>
+RCPT TO:<committer@example.com>
+From: Example <from@example.com>
+To: to@example.com
+Cc: A <author@example.com>,
+ One <one@example.com>,
+ two@example.com,
+ C O Mitter <committer@example.com>
+Subject: [PATCH 1/1] Second.
+Date: DATE-STRING
+Message-ID: MESSAGE-ID-STRING
+X-Mailer: X-MAILER-STRING
+MIME-Version: 1.0
+Content-Transfer-Encoding: 8bit
+
+Result: OK
+EOF
+"
+
+test_expect_success $PREREQ '--suppress-cc=bodycc' '
+ test_suppression bodycc
+'
+
+test_expect_success $PREREQ 'setup expect' "
+cat >expected-suppress-cc <<\EOF
+0001-Second.patch
+(mbox) Adding cc: A <author@example.com> from line 'From: A <author@example.com>'
+(body) Adding cc: C O Mitter <committer@example.com> from line 'Signed-off-by: C O Mitter <committer@example.com>'
+Dry-OK. Log says:
+Server: relay.example.com
+MAIL FROM:<from@example.com>
+RCPT TO:<to@example.com>
+RCPT TO:<author@example.com>
+RCPT TO:<committer@example.com>
+From: Example <from@example.com>
+To: to@example.com
+Cc: A <author@example.com>,
+ C O Mitter <committer@example.com>
+Subject: [PATCH 1/1] Second.
+Date: DATE-STRING
+Message-ID: MESSAGE-ID-STRING
+X-Mailer: X-MAILER-STRING
+MIME-Version: 1.0
+Content-Transfer-Encoding: 8bit
+
+Result: OK
+EOF
+"
+
+test_expect_success $PREREQ '--suppress-cc=cc' '
+ test_suppression cc
+'
+
+test_confirm () {
+ echo y | \
+ GIT_SEND_EMAIL_NOTTY=1 \
+ git send-email \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ $@ $patches >stdout &&
+ grep "Send this email" stdout
+}
+
+test_expect_success $PREREQ '--confirm=always' '
+ test_confirm --confirm=always --suppress-cc=all
+'
+
+test_expect_success $PREREQ '--confirm=auto' '
+ test_confirm --confirm=auto
+'
+
+test_expect_success $PREREQ '--confirm=cc' '
+ test_confirm --confirm=cc
+'
+
+test_expect_success $PREREQ '--confirm=compose' '
+ test_confirm --confirm=compose --compose
+'
+
+test_expect_success $PREREQ 'confirm by default (due to cc)' '
+ test_when_finished git config sendemail.confirm never &&
+ git config --unset sendemail.confirm &&
+ test_confirm
+'
+
+test_expect_success $PREREQ 'confirm by default (due to --compose)' '
+ test_when_finished git config sendemail.confirm never &&
+ git config --unset sendemail.confirm &&
+ test_confirm --suppress-cc=all --compose
+'
+
+test_expect_success $PREREQ 'confirm detects EOF (inform assumes y)' '
+ test_when_finished git config sendemail.confirm never &&
+ git config --unset sendemail.confirm &&
+ rm -fr outdir &&
+ git format-patch -2 -o outdir &&
+ GIT_SEND_EMAIL_NOTTY=1 \
+ git send-email \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ outdir/*.patch </dev/null
+'
+
+test_expect_success $PREREQ 'confirm detects EOF (auto causes failure)' '
+ test_when_finished git config sendemail.confirm never &&
+ git config sendemail.confirm auto &&
+ GIT_SEND_EMAIL_NOTTY=1 &&
+ export GIT_SEND_EMAIL_NOTTY &&
+ test_must_fail git send-email \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ $patches </dev/null
+'
+
+test_expect_success $PREREQ 'confirm does not loop forever' '
+ test_when_finished git config sendemail.confirm never &&
+ git config sendemail.confirm auto &&
+ GIT_SEND_EMAIL_NOTTY=1 &&
+ export GIT_SEND_EMAIL_NOTTY &&
+ yes "bogus" | test_must_fail git send-email \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ $patches
+'
+
+test_expect_success $PREREQ 'utf8 Cc is rfc2047 encoded' '
+ clean_fake_sendmail &&
+ rm -fr outdir &&
+ git format-patch -1 -o outdir --cc="àéìöú <utf8@example.com>" &&
+ git send-email \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ outdir/*.patch &&
+ grep "^ " msgtxt1 |
+ grep "=?UTF-8?q?=C3=A0=C3=A9=C3=AC=C3=B6=C3=BA?= <utf8@example.com>"
+'
+
+test_expect_success $PREREQ '--compose adds MIME for utf8 body' '
+ clean_fake_sendmail &&
+ write_script fake-editor-utf8 <<-\EOF &&
+ echo "utf8 body: àéìöú" >>"$1"
+ EOF
+ GIT_EDITOR="\"$(pwd)/fake-editor-utf8\"" \
+ git send-email \
+ --compose --subject foo \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ $patches &&
+ grep "^utf8 body" msgtxt1 &&
+ grep "^Content-Type: text/plain; charset=UTF-8" msgtxt1
+'
+
+test_expect_success $PREREQ '--compose respects user mime type' '
+ clean_fake_sendmail &&
+ write_script fake-editor-utf8-mime <<-\EOF &&
+ cat >"$1" <<-\EOM
+ MIME-Version: 1.0
+ Content-Type: text/plain; charset=iso-8859-1
+ Content-Transfer-Encoding: 8bit
+ Subject: foo
+
+ utf8 body: àéìöú
+ EOM
+ EOF
+ GIT_EDITOR="\"$(pwd)/fake-editor-utf8-mime\"" \
+ git send-email \
+ --compose --subject foo \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ $patches &&
+ grep "^utf8 body" msgtxt1 &&
+ grep "^Content-Type: text/plain; charset=iso-8859-1" msgtxt1 &&
+ ! grep "^Content-Type: text/plain; charset=UTF-8" msgtxt1
+'
+
+test_expect_success $PREREQ '--compose adds MIME for utf8 subject' '
+ clean_fake_sendmail &&
+ GIT_EDITOR="\"$(pwd)/fake-editor\"" \
+ git send-email \
+ --compose --subject utf8-sübjëct \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ $patches &&
+ grep "^fake edit" msgtxt1 &&
+ grep "^Subject: =?UTF-8?q?utf8-s=C3=BCbj=C3=ABct?=" msgtxt1
+'
+
+test_expect_success $PREREQ 'utf8 author is correctly passed on' '
+ clean_fake_sendmail &&
+ test_commit weird_author &&
+ test_when_finished "git reset --hard HEAD^" &&
+ git commit --amend --author "Füñný Nâmé <odd_?=mail@example.com>" &&
+ git format-patch --stdout -1 >funny_name.patch &&
+ git send-email --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ funny_name.patch &&
+ grep "^From: Füñný Nâmé <odd_?=mail@example.com>" msgtxt1
+'
+
+test_expect_success $PREREQ 'utf8 sender is not duplicated' '
+ clean_fake_sendmail &&
+ test_commit weird_sender &&
+ test_when_finished "git reset --hard HEAD^" &&
+ git commit --amend --author "Füñný Nâmé <odd_?=mail@example.com>" &&
+ git format-patch --stdout -1 >funny_name.patch &&
+ git send-email --from="Füñný Nâmé <odd_?=mail@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ funny_name.patch &&
+ grep "^From: " msgtxt1 >msgfrom &&
+ test_line_count = 1 msgfrom
+'
+
+test_expect_success $PREREQ 'sendemail.composeencoding works' '
+ clean_fake_sendmail &&
+ git config sendemail.composeencoding iso-8859-1 &&
+ write_script fake-editor-utf8 <<-\EOF &&
+ echo "utf8 body: àéìöú" >>"$1"
+ EOF
+ GIT_EDITOR="\"$(pwd)/fake-editor-utf8\"" \
+ git send-email \
+ --compose --subject foo \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ $patches &&
+ grep "^utf8 body" msgtxt1 &&
+ grep "^Content-Type: text/plain; charset=iso-8859-1" msgtxt1
+'
+
+test_expect_success $PREREQ '--compose-encoding works' '
+ clean_fake_sendmail &&
+ write_script fake-editor-utf8 <<-\EOF &&
+ echo "utf8 body: àéìöú" >>"$1"
+ EOF
+ GIT_EDITOR="\"$(pwd)/fake-editor-utf8\"" \
+ git send-email \
+ --compose-encoding iso-8859-1 \
+ --compose --subject foo \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ $patches &&
+ grep "^utf8 body" msgtxt1 &&
+ grep "^Content-Type: text/plain; charset=iso-8859-1" msgtxt1
+'
+
+test_expect_success $PREREQ '--compose-encoding overrides sendemail.composeencoding' '
+ clean_fake_sendmail &&
+ git config sendemail.composeencoding iso-8859-1 &&
+ write_script fake-editor-utf8 <<-\EOF &&
+ echo "utf8 body: àéìöú" >>"$1"
+ EOF
+ GIT_EDITOR="\"$(pwd)/fake-editor-utf8\"" \
+ git send-email \
+ --compose-encoding iso-8859-2 \
+ --compose --subject foo \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ $patches &&
+ grep "^utf8 body" msgtxt1 &&
+ grep "^Content-Type: text/plain; charset=iso-8859-2" msgtxt1
+'
+
+test_expect_success $PREREQ '--compose-encoding adds correct MIME for subject' '
+ clean_fake_sendmail &&
+ GIT_EDITOR="\"$(pwd)/fake-editor\"" \
+ git send-email \
+ --compose-encoding iso-8859-2 \
+ --compose --subject utf8-sübjëct \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ $patches &&
+ grep "^fake edit" msgtxt1 &&
+ grep "^Subject: =?iso-8859-2?q?utf8-s=C3=BCbj=C3=ABct?=" msgtxt1
+'
+
+test_expect_success $PREREQ 'detects ambiguous reference/file conflict' '
+ echo main >main &&
+ git add main &&
+ git commit -m"add main" &&
+ test_must_fail git send-email --dry-run main 2>errors &&
+ grep disambiguate errors
+'
+
+test_expect_success $PREREQ 'feed two files' '
+ rm -fr outdir &&
+ git format-patch -2 -o outdir &&
+ git send-email \
+ --dry-run \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ outdir/000?-*.patch 2>errors >out &&
+ grep "^Subject: " out >subjects &&
+ test "z$(sed -n -e 1p subjects)" = "zSubject: [PATCH 1/2] Second." &&
+ test "z$(sed -n -e 2p subjects)" = "zSubject: [PATCH 2/2] add main"
+'
+
+test_expect_success $PREREQ 'in-reply-to but no threading' '
+ git send-email \
+ --dry-run \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --in-reply-to="<in-reply-id@example.com>" \
+ --no-thread \
+ $patches >out &&
+ grep "In-Reply-To: <in-reply-id@example.com>" out
+'
+
+test_expect_success $PREREQ 'no in-reply-to and no threading' '
+ git send-email \
+ --dry-run \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --no-thread \
+ $patches >stdout &&
+ ! grep "In-Reply-To: " stdout
+'
+
+test_expect_success $PREREQ 'threading but no chain-reply-to' '
+ git send-email \
+ --dry-run \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --thread \
+ --no-chain-reply-to \
+ $patches $patches >stdout &&
+ grep "In-Reply-To: " stdout
+'
+
+test_expect_success $PREREQ 'override in-reply-to if no threading' '
+ git send-email \
+ --dry-run \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --no-thread \
+ --in-reply-to="override" \
+ $threaded_patches >stdout &&
+ grep "In-Reply-To: <override>" stdout
+'
+
+test_expect_success $PREREQ 'sendemail.to works' '
+ git config --replace-all sendemail.to "Somebody <somebody@ex.com>" &&
+ git send-email \
+ --dry-run \
+ --from="Example <nobody@example.com>" \
+ $patches >stdout &&
+ grep "To: Somebody <somebody@ex.com>" stdout
+'
+
+test_expect_success $PREREQ 'setup sendemail.identity' '
+ git config --replace-all sendemail.to "default@example.com" &&
+ git config --replace-all sendemail.isp.to "isp@example.com" &&
+ git config --replace-all sendemail.cloud.to "cloud@example.com"
+'
+
+test_expect_success $PREREQ 'sendemail.identity: reads the correct identity config' '
+ git -c sendemail.identity=cloud send-email \
+ --dry-run \
+ --from="nobody@example.com" \
+ $patches >stdout &&
+ grep "To: cloud@example.com" stdout
+'
+
+test_expect_success $PREREQ 'sendemail.identity: identity overrides sendemail.identity' '
+ git -c sendemail.identity=cloud send-email \
+ --identity=isp \
+ --dry-run \
+ --from="nobody@example.com" \
+ $patches >stdout &&
+ grep "To: isp@example.com" stdout
+'
+
+test_expect_success $PREREQ 'sendemail.identity: --no-identity clears previous identity' '
+ git -c sendemail.identity=cloud send-email \
+ --no-identity \
+ --dry-run \
+ --from="nobody@example.com" \
+ $patches >stdout &&
+ grep "To: default@example.com" stdout
+'
+
+test_expect_success $PREREQ 'sendemail.identity: bool identity variable existence overrides' '
+ git -c sendemail.identity=cloud \
+ -c sendemail.xmailer=true \
+ -c sendemail.cloud.xmailer=false \
+ send-email \
+ --dry-run \
+ --from="nobody@example.com" \
+ $patches >stdout &&
+ grep "To: cloud@example.com" stdout &&
+ ! grep "X-Mailer" stdout
+'
+
+test_expect_success $PREREQ 'sendemail.identity: bool variable fallback' '
+ git -c sendemail.identity=cloud \
+ -c sendemail.xmailer=false \
+ send-email \
+ --dry-run \
+ --from="nobody@example.com" \
+ $patches >stdout &&
+ grep "To: cloud@example.com" stdout &&
+ ! grep "X-Mailer" stdout
+'
+
+test_expect_success $PREREQ 'sendemail.identity: bool variable without a value' '
+ git -c sendemail.xmailer \
+ send-email \
+ --dry-run \
+ --from="nobody@example.com" \
+ $patches >stdout &&
+ grep "To: default@example.com" stdout &&
+ grep "X-Mailer" stdout
+'
+
+test_expect_success $PREREQ '--no-to overrides sendemail.to' '
+ git send-email \
+ --dry-run \
+ --from="Example <nobody@example.com>" \
+ --no-to \
+ --to=nobody@example.com \
+ $patches >stdout &&
+ grep "To: nobody@example.com" stdout &&
+ ! grep "To: Somebody <somebody@ex.com>" stdout
+'
+
+test_expect_success $PREREQ 'sendemail.cc works' '
+ git config --replace-all sendemail.cc "Somebody <somebody@ex.com>" &&
+ git send-email \
+ --dry-run \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ $patches >stdout &&
+ grep "Cc: Somebody <somebody@ex.com>" stdout
+'
+
+test_expect_success $PREREQ '--no-cc overrides sendemail.cc' '
+ git send-email \
+ --dry-run \
+ --from="Example <nobody@example.com>" \
+ --no-cc \
+ --cc=bodies@example.com \
+ --to=nobody@example.com \
+ $patches >stdout &&
+ grep "Cc: bodies@example.com" stdout &&
+ ! grep "Cc: Somebody <somebody@ex.com>" stdout
+'
+
+test_expect_success $PREREQ 'sendemail.bcc works' '
+ git config --replace-all sendemail.bcc "Other <other@ex.com>" &&
+ git send-email \
+ --dry-run \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server relay.example.com \
+ $patches >stdout &&
+ grep "RCPT TO:<other@ex.com>" stdout
+'
+
+test_expect_success $PREREQ '--no-bcc overrides sendemail.bcc' '
+ git send-email \
+ --dry-run \
+ --from="Example <nobody@example.com>" \
+ --no-bcc \
+ --bcc=bodies@example.com \
+ --to=nobody@example.com \
+ --smtp-server relay.example.com \
+ $patches >stdout &&
+ grep "RCPT TO:<bodies@example.com>" stdout &&
+ ! grep "RCPT TO:<other@ex.com>" stdout
+'
+
+test_expect_success $PREREQ 'patches To headers are used by default' '
+ patch=$(git format-patch -1 --to="bodies@example.com") &&
+ test_when_finished "rm $patch" &&
+ git send-email \
+ --dry-run \
+ --from="Example <nobody@example.com>" \
+ --smtp-server relay.example.com \
+ $patch >stdout &&
+ grep "RCPT TO:<bodies@example.com>" stdout
+'
+
+test_expect_success $PREREQ 'patches To headers are appended to' '
+ patch=$(git format-patch -1 --to="bodies@example.com") &&
+ test_when_finished "rm $patch" &&
+ git send-email \
+ --dry-run \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server relay.example.com \
+ $patch >stdout &&
+ grep "RCPT TO:<bodies@example.com>" stdout &&
+ grep "RCPT TO:<nobody@example.com>" stdout
+'
+
+test_expect_success $PREREQ 'To headers from files reset each patch' '
+ patch1=$(git format-patch -1 --to="bodies@example.com") &&
+ patch2=$(git format-patch -1 --to="other@example.com" HEAD~) &&
+ test_when_finished "rm $patch1 && rm $patch2" &&
+ git send-email \
+ --dry-run \
+ --from="Example <nobody@example.com>" \
+ --to="nobody@example.com" \
+ --smtp-server relay.example.com \
+ $patch1 $patch2 >stdout &&
+ test $(grep -c "RCPT TO:<bodies@example.com>" stdout) = 1 &&
+ test $(grep -c "RCPT TO:<nobody@example.com>" stdout) = 2 &&
+ test $(grep -c "RCPT TO:<other@example.com>" stdout) = 1
+'
+
+test_expect_success $PREREQ 'setup expect' '
+cat >email-using-8bit <<\EOF
+From fe6ecc66ece37198fe5db91fa2fc41d9f4fe5cc4 Mon Sep 17 00:00:00 2001
+Message-ID: <bogus-message-id@example.com>
+From: author@example.com
+Date: Sat, 12 Jun 2010 15:53:58 +0200
+Subject: subject goes here
+
+Dieser deutsche Text enthält einen Umlaut!
+EOF
+'
+
+test_expect_success $PREREQ 'setup expect' '
+ echo "Subject: subject goes here" >expected
+'
+
+test_expect_success $PREREQ 'ASCII subject is not RFC2047 quoted' '
+ clean_fake_sendmail &&
+ echo bogus |
+ git send-email --from=author@example.com --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ --8bit-encoding=UTF-8 \
+ email-using-8bit >stdout &&
+ grep "Subject" msgtxt1 >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success $PREREQ 'setup expect' '
+ cat >content-type-decl <<-\EOF
+ MIME-Version: 1.0
+ Content-Type: text/plain; charset=UTF-8
+ Content-Transfer-Encoding: 8bit
+ EOF
+'
+
+test_expect_success $PREREQ 'asks about and fixes 8bit encodings' '
+ clean_fake_sendmail &&
+ echo |
+ git send-email --from=author@example.com --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ email-using-8bit >stdout &&
+ grep "do not declare a Content-Transfer-Encoding" stdout &&
+ grep email-using-8bit stdout &&
+ grep "Which 8bit encoding" stdout &&
+ grep -E "Content|MIME" msgtxt1 >actual &&
+ test_cmp content-type-decl actual
+'
+
+test_expect_success $PREREQ 'sendemail.8bitEncoding works' '
+ clean_fake_sendmail &&
+ git config sendemail.assume8bitEncoding UTF-8 &&
+ echo bogus |
+ git send-email --from=author@example.com --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ email-using-8bit >stdout &&
+ grep -E "Content|MIME" msgtxt1 >actual &&
+ test_cmp content-type-decl actual
+'
+
+test_expect_success $PREREQ 'sendemail.8bitEncoding in .git/config overrides --global .gitconfig' '
+ clean_fake_sendmail &&
+ git config sendemail.assume8bitEncoding UTF-8 &&
+ test_when_finished "rm -rf home" &&
+ mkdir home &&
+ git config -f home/.gitconfig sendemail.assume8bitEncoding "bogus too" &&
+ echo bogus |
+ env HOME="$(pwd)/home" DEBUG=1 \
+ git send-email --from=author@example.com --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ email-using-8bit >stdout &&
+ grep -E "Content|MIME" msgtxt1 >actual &&
+ test_cmp content-type-decl actual
+'
+
+test_expect_success $PREREQ '--8bit-encoding overrides sendemail.8bitEncoding' '
+ clean_fake_sendmail &&
+ git config sendemail.assume8bitEncoding "bogus too" &&
+ echo bogus |
+ git send-email --from=author@example.com --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ --8bit-encoding=UTF-8 \
+ email-using-8bit >stdout &&
+ grep -E "Content|MIME" msgtxt1 >actual &&
+ test_cmp content-type-decl actual
+'
+
+test_expect_success $PREREQ 'setup expect' '
+ cat >email-using-8bit <<-\EOF
+ From fe6ecc66ece37198fe5db91fa2fc41d9f4fe5cc4 Mon Sep 17 00:00:00 2001
+ Message-ID: <bogus-message-id@example.com>
+ From: author@example.com
+ Date: Sat, 12 Jun 2010 15:53:58 +0200
+ Subject: Dieser Betreff enthält auch einen Umlaut!
+
+ Nothing to see here.
+ EOF
+'
+
+test_expect_success $PREREQ 'setup expect' '
+ cat >expected <<-\EOF
+ Subject: =?UTF-8?q?Dieser=20Betreff=20enth=C3=A4lt=20auch=20einen=20Umlaut!?=
+ EOF
+'
+
+test_expect_success $PREREQ '--8bit-encoding also treats subject' '
+ clean_fake_sendmail &&
+ echo bogus |
+ git send-email --from=author@example.com --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ --8bit-encoding=UTF-8 \
+ email-using-8bit >stdout &&
+ grep "Subject" msgtxt1 >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success $PREREQ 'setup expect' '
+ cat >email-using-8bit <<-\EOF
+ From fe6ecc66ece37198fe5db91fa2fc41d9f4fe5cc4 Mon Sep 17 00:00:00 2001
+ Message-ID: <bogus-message-id@example.com>
+ From: A U Thor <author@example.com>
+ Date: Sat, 12 Jun 2010 15:53:58 +0200
+ Content-Type: text/plain; charset=UTF-8
+ Subject: Nothing to see here.
+
+ Dieser Betreff enthält auch einen Umlaut!
+ EOF
+'
+
+test_expect_success $PREREQ '--transfer-encoding overrides sendemail.transferEncoding' '
+ clean_fake_sendmail &&
+ test_must_fail git -c sendemail.transferEncoding=8bit \
+ send-email \
+ --transfer-encoding=7bit \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ email-using-8bit \
+ 2>errors >out &&
+ grep "cannot send message as 7bit" errors &&
+ test -z "$(ls msgtxt*)"
+'
+
+test_expect_success $PREREQ 'sendemail.transferEncoding via config' '
+ clean_fake_sendmail &&
+ test_must_fail git -c sendemail.transferEncoding=7bit \
+ send-email \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ email-using-8bit \
+ 2>errors >out &&
+ grep "cannot send message as 7bit" errors &&
+ test -z "$(ls msgtxt*)"
+'
+
+test_expect_success $PREREQ 'sendemail.transferEncoding via cli' '
+ clean_fake_sendmail &&
+ test_must_fail git send-email \
+ --transfer-encoding=7bit \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ email-using-8bit \
+ 2>errors >out &&
+ grep "cannot send message as 7bit" errors &&
+ test -z "$(ls msgtxt*)"
+'
+
+test_expect_success $PREREQ 'setup expect' '
+ cat >expected <<-\EOF
+ Dieser Betreff enth=C3=A4lt auch einen Umlaut!
+ EOF
+'
+
+test_expect_success $PREREQ '8-bit and sendemail.transferencoding=quoted-printable' '
+ clean_fake_sendmail &&
+ git send-email \
+ --transfer-encoding=quoted-printable \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ email-using-8bit \
+ 2>errors >out &&
+ sed "1,/^$/d" msgtxt1 >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success $PREREQ 'setup expect' '
+ cat >expected <<-\EOF
+ RGllc2VyIEJldHJlZmYgZW50aMOkbHQgYXVjaCBlaW5lbiBVbWxhdXQhCg==
+ EOF
+'
+
+test_expect_success $PREREQ '8-bit and sendemail.transferencoding=base64' '
+ clean_fake_sendmail &&
+ git send-email \
+ --transfer-encoding=base64 \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ email-using-8bit \
+ 2>errors >out &&
+ sed "1,/^$/d" msgtxt1 >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success $PREREQ 'setup expect' '
+ cat >email-using-qp <<-\EOF
+ From fe6ecc66ece37198fe5db91fa2fc41d9f4fe5cc4 Mon Sep 17 00:00:00 2001
+ Message-ID: <bogus-message-id@example.com>
+ From: A U Thor <author@example.com>
+ Date: Sat, 12 Jun 2010 15:53:58 +0200
+ MIME-Version: 1.0
+ Content-Transfer-Encoding: quoted-printable
+ Content-Type: text/plain; charset=UTF-8
+ Subject: Nothing to see here.
+
+ Dieser Betreff enth=C3=A4lt auch einen Umlaut!
+ EOF
+'
+
+test_expect_success $PREREQ 'convert from quoted-printable to base64' '
+ clean_fake_sendmail &&
+ git send-email \
+ --transfer-encoding=base64 \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ email-using-qp \
+ 2>errors >out &&
+ sed "1,/^$/d" msgtxt1 >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success $PREREQ 'setup expect' "
+tr -d '\\015' | tr '%' '\\015' >email-using-crlf <<EOF
+From fe6ecc66ece37198fe5db91fa2fc41d9f4fe5cc4 Mon Sep 17 00:00:00 2001
+Message-ID: <bogus-message-id@example.com>
+From: A U Thor <author@example.com>
+Date: Sat, 12 Jun 2010 15:53:58 +0200
+Content-Type: text/plain; charset=UTF-8
+Subject: Nothing to see here.
+
+Look, I have a CRLF and an = sign!%
+EOF
+"
+
+test_expect_success $PREREQ 'setup expect' '
+ cat >expected <<-\EOF
+ Look, I have a CRLF and an =3D sign!=0D
+ EOF
+'
+
+test_expect_success $PREREQ 'CRLF and sendemail.transferencoding=quoted-printable' '
+ clean_fake_sendmail &&
+ git send-email \
+ --transfer-encoding=quoted-printable \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ email-using-crlf \
+ 2>errors >out &&
+ sed "1,/^$/d" msgtxt1 >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success $PREREQ 'setup expect' '
+ cat >expected <<-\EOF
+ TG9vaywgSSBoYXZlIGEgQ1JMRiBhbmQgYW4gPSBzaWduIQ0K
+ EOF
+'
+
+test_expect_success $PREREQ 'CRLF and sendemail.transferencoding=base64' '
+ clean_fake_sendmail &&
+ git send-email \
+ --transfer-encoding=base64 \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ email-using-crlf \
+ 2>errors >out &&
+ sed "1,/^$/d" msgtxt1 >actual &&
+ test_cmp expected actual
+'
+
+
+# Note that the patches in this test are deliberately out of order; we
+# want to make sure it works even if the cover-letter is not in the
+# first mail.
+test_expect_success $PREREQ 'refusing to send cover letter template' '
+ clean_fake_sendmail &&
+ rm -fr outdir &&
+ git format-patch --cover-letter -2 -o outdir &&
+ test_must_fail git send-email \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ outdir/0002-*.patch \
+ outdir/0000-*.patch \
+ outdir/0001-*.patch \
+ 2>errors >out &&
+ grep "SUBJECT HERE" errors &&
+ test -z "$(ls msgtxt*)"
+'
+
+test_expect_success $PREREQ '--force sends cover letter template anyway' '
+ clean_fake_sendmail &&
+ rm -fr outdir &&
+ git format-patch --cover-letter -2 -o outdir &&
+ git send-email \
+ --force \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ outdir/0002-*.patch \
+ outdir/0000-*.patch \
+ outdir/0001-*.patch \
+ 2>errors >out &&
+ ! grep "SUBJECT HERE" errors &&
+ test -n "$(ls msgtxt*)"
+'
+
+test_cover_addresses () {
+ header="$1"
+ shift
+ clean_fake_sendmail &&
+ rm -fr outdir &&
+ git format-patch --cover-letter -2 -o outdir &&
+ cover=$(echo outdir/0000-*.patch) &&
+ mv $cover cover-to-edit.patch &&
+ perl -pe "s/^From:/$header: extra\@address.com\nFrom:/" cover-to-edit.patch >"$cover" &&
+ git send-email \
+ --force \
+ --from="Example <nobody@example.com>" \
+ --no-to --no-cc \
+ "$@" \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ outdir/0000-*.patch \
+ outdir/0001-*.patch \
+ outdir/0002-*.patch \
+ 2>errors >out &&
+ grep "^$header: extra@address.com" msgtxt1 >to1 &&
+ grep "^$header: extra@address.com" msgtxt2 >to2 &&
+ grep "^$header: extra@address.com" msgtxt3 >to3 &&
+ test_line_count = 1 to1 &&
+ test_line_count = 1 to2 &&
+ test_line_count = 1 to3
+}
+
+test_expect_success $PREREQ 'to-cover adds To to all mail' '
+ test_cover_addresses "To" --to-cover
+'
+
+test_expect_success $PREREQ 'cc-cover adds Cc to all mail' '
+ test_cover_addresses "Cc" --cc-cover
+'
+
+test_expect_success $PREREQ 'tocover adds To to all mail' '
+ test_config sendemail.tocover true &&
+ test_cover_addresses "To"
+'
+
+test_expect_success $PREREQ 'cccover adds Cc to all mail' '
+ test_config sendemail.cccover true &&
+ test_cover_addresses "Cc"
+'
+
+test_expect_success $PREREQ 'escaped quotes in sendemail.aliasfiletype=mutt' '
+ clean_fake_sendmail &&
+ echo "alias sbd \\\"Dot U. Sir\\\" <somebody@example.org>" >.mutt &&
+ git config --replace-all sendemail.aliasesfile "$(pwd)/.mutt" &&
+ git config sendemail.aliasfiletype mutt &&
+ git send-email \
+ --from="Example <nobody@example.com>" \
+ --to=sbd \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ outdir/0001-*.patch \
+ 2>errors >out &&
+ grep "^!somebody@example\.org!$" commandline1 &&
+ grep -F "To: \"Dot U. Sir\" <somebody@example.org>" out
+'
+
+test_expect_success $PREREQ 'sendemail.aliasfiletype=mailrc' '
+ clean_fake_sendmail &&
+ echo "alias sbd somebody@example.org" >.mailrc &&
+ git config --replace-all sendemail.aliasesfile "$(pwd)/.mailrc" &&
+ git config sendemail.aliasfiletype mailrc &&
+ git send-email \
+ --from="Example <nobody@example.com>" \
+ --to=sbd \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ outdir/0001-*.patch \
+ 2>errors >out &&
+ grep "^!somebody@example\.org!$" commandline1
+'
+
+test_expect_success $PREREQ 'sendemail.aliasesfile=~/.mailrc' '
+ clean_fake_sendmail &&
+ echo "alias sbd someone@example.org" >"$HOME/.mailrc" &&
+ git config --replace-all sendemail.aliasesfile "~/.mailrc" &&
+ git config sendemail.aliasfiletype mailrc &&
+ git send-email \
+ --from="Example <nobody@example.com>" \
+ --to=sbd \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ outdir/0001-*.patch \
+ 2>errors >out &&
+ grep "^!someone@example\.org!$" commandline1
+'
+
+test_dump_aliases () {
+ msg="$1" && shift &&
+ filetype="$1" && shift &&
+ printf '%s\n' "$@" >expect &&
+ cat >.tmp-email-aliases &&
+
+ test_expect_success $PREREQ "$msg" '
+ clean_fake_sendmail && rm -fr outdir &&
+ git config --replace-all sendemail.aliasesfile \
+ "$(pwd)/.tmp-email-aliases" &&
+ git config sendemail.aliasfiletype "$filetype" &&
+ git send-email --dump-aliases 2>errors >actual &&
+ test_cmp expect actual
+ '
+}
+
+test_dump_aliases '--dump-aliases sendmail format' \
+ 'sendmail' \
+ 'abgroup' \
+ 'alice' \
+ 'bcgrp' \
+ 'bob' \
+ 'chloe' <<-\EOF
+ alice: Alice W Land <awol@example.com>
+ bob: Robert Bobbyton <bob@example.com>
+ chloe: chloe@example.com
+ abgroup: alice, bob
+ bcgrp: bob, chloe, Other <o@example.com>
+ EOF
+
+test_dump_aliases '--dump-aliases mutt format' \
+ 'mutt' \
+ 'alice' \
+ 'bob' \
+ 'chloe' \
+ 'donald' <<-\EOF
+ alias alice Alice W Land <awol@example.com>
+ alias donald Donald C Carlton <donc@example.com>
+ alias bob Robert Bobbyton <bob@example.com>
+ alias chloe chloe@example.com
+ EOF
+
+test_dump_aliases '--dump-aliases mailrc format' \
+ 'mailrc' \
+ 'alice' \
+ 'bob' \
+ 'chloe' \
+ 'eve' <<-\EOF
+ alias alice Alice W Land <awol@example.com>
+ alias eve Eve <eve@example.com>
+ alias bob Robert Bobbyton <bob@example.com>
+ alias chloe chloe@example.com
+ EOF
+
+test_dump_aliases '--dump-aliases pine format' \
+ 'pine' \
+ 'alice' \
+ 'bob' \
+ 'chloe' \
+ 'eve' <<-\EOF
+ alice Alice W Land <awol@example.com>
+ eve Eve <eve@example.com>
+ bob Robert Bobbyton <bob@example.com>
+ chloe chloe@example.com
+ EOF
+
+test_dump_aliases '--dump-aliases gnus format' \
+ 'gnus' \
+ 'alice' \
+ 'bob' \
+ 'chloe' \
+ 'eve' <<-\EOF
+ (define-mail-alias "alice" "awol@example.com")
+ (define-mail-alias "eve" "eve@example.com")
+ (define-mail-alias "bob" "bob@example.com")
+ (define-mail-alias "chloe" "chloe@example.com")
+ EOF
+
+test_expect_success '--dump-aliases must be used alone' '
+ test_must_fail git send-email --dump-aliases --to=janice@example.com -1 refs/heads/accounting
+'
+
+test_expect_success $PREREQ 'aliases and sendemail.identity' '
+ test_must_fail git \
+ -c sendemail.identity=cloud \
+ -c sendemail.aliasesfile=default-aliases \
+ -c sendemail.cloud.aliasesfile=cloud-aliases \
+ send-email -1 2>stderr &&
+ test_grep "cloud-aliases" stderr
+'
+
+test_sendmail_aliases () {
+ msg="$1" && shift &&
+ expect="$@" &&
+ cat >.tmp-email-aliases &&
+
+ test_expect_success $PREREQ "$msg" '
+ clean_fake_sendmail && rm -fr outdir &&
+ git format-patch -1 -o outdir &&
+ git config --replace-all sendemail.aliasesfile \
+ "$(pwd)/.tmp-email-aliases" &&
+ git config sendemail.aliasfiletype sendmail &&
+ git send-email \
+ --from="Example <nobody@example.com>" \
+ --to=alice --to=bcgrp \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ outdir/0001-*.patch \
+ 2>errors >out &&
+ for i in $expect
+ do
+ grep "^!$i!$" commandline1 || return 1
+ done
+ '
+}
+
+test_sendmail_aliases 'sendemail.aliasfiletype=sendmail' \
+ 'awol@example\.com' \
+ 'bob@example\.com' \
+ 'chloe@example\.com' \
+ 'o@example\.com' <<-\EOF
+ alice: Alice W Land <awol@example.com>
+ bob: Robert Bobbyton <bob@example.com>
+ # this is a comment
+ # this is also a comment
+ chloe: chloe@example.com
+ abgroup: alice, bob
+ bcgrp: bob, chloe, Other <o@example.com>
+ EOF
+
+test_sendmail_aliases 'sendmail aliases line folding' \
+ alice1 \
+ bob1 bob2 \
+ chuck1 chuck2 \
+ darla1 darla2 darla3 \
+ elton1 elton2 elton3 \
+ fred1 fred2 \
+ greg1 <<-\EOF
+ alice: alice1
+ bob: bob1,\
+ bob2
+ chuck: chuck1,
+ chuck2
+ darla: darla1,\
+ darla2,
+ darla3
+ elton: elton1,
+ elton2,\
+ elton3
+ fred: fred1,\
+ fred2
+ greg: greg1
+ bcgrp: bob, chuck, darla, elton, fred, greg
+ EOF
+
+test_sendmail_aliases 'sendmail aliases tolerate bogus line folding' \
+ alice1 bob1 <<-\EOF
+ alice: alice1
+ bcgrp: bob1\
+ EOF
+
+test_sendmail_aliases 'sendmail aliases empty' alice bcgrp <<-\EOF
+ EOF
+
+test_expect_success $PREREQ 'alias support in To header' '
+ clean_fake_sendmail &&
+ echo "alias sbd someone@example.org" >.mailrc &&
+ test_config sendemail.aliasesfile ".mailrc" &&
+ test_config sendemail.aliasfiletype mailrc &&
+ git format-patch --stdout -1 --to=sbd >aliased.patch &&
+ git send-email \
+ --from="Example <nobody@example.com>" \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ aliased.patch \
+ 2>errors >out &&
+ grep "^!someone@example\.org!$" commandline1
+'
+
+test_expect_success $PREREQ 'alias support in Cc header' '
+ clean_fake_sendmail &&
+ echo "alias sbd someone@example.org" >.mailrc &&
+ test_config sendemail.aliasesfile ".mailrc" &&
+ test_config sendemail.aliasfiletype mailrc &&
+ git format-patch --stdout -1 --cc=sbd >aliased.patch &&
+ git send-email \
+ --from="Example <nobody@example.com>" \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ aliased.patch \
+ 2>errors >out &&
+ grep "^!someone@example\.org!$" commandline1
+'
+
+test_expect_success $PREREQ 'tocmd works with aliases' '
+ clean_fake_sendmail &&
+ echo "alias sbd someone@example.org" >.mailrc &&
+ test_config sendemail.aliasesfile ".mailrc" &&
+ test_config sendemail.aliasfiletype mailrc &&
+ git format-patch --stdout -1 >tocmd.patch &&
+ echo tocmd--sbd >>tocmd.patch &&
+ git send-email \
+ --from="Example <nobody@example.com>" \
+ --to-cmd=./tocmd-sed \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ tocmd.patch \
+ 2>errors >out &&
+ grep "^!someone@example\.org!$" commandline1
+'
+
+test_expect_success $PREREQ 'cccmd works with aliases' '
+ clean_fake_sendmail &&
+ echo "alias sbd someone@example.org" >.mailrc &&
+ test_config sendemail.aliasesfile ".mailrc" &&
+ test_config sendemail.aliasfiletype mailrc &&
+ git format-patch --stdout -1 >cccmd.patch &&
+ echo cccmd--sbd >>cccmd.patch &&
+ git send-email \
+ --from="Example <nobody@example.com>" \
+ --cc-cmd=./cccmd-sed \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ cccmd.patch \
+ 2>errors >out &&
+ grep "^!someone@example\.org!$" commandline1
+'
+
+do_xmailer_test () {
+ expected=$1 params=$2 &&
+ git format-patch -1 &&
+ git send-email \
+ --from="Example <nobody@example.com>" \
+ --to=someone@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ $params \
+ 0001-*.patch \
+ 2>errors >out &&
+ { grep '^X-Mailer:' out || :; } >mailer &&
+ test_line_count = $expected mailer
+}
+
+test_expect_success $PREREQ '--[no-]xmailer without any configuration' '
+ do_xmailer_test 1 "--xmailer" &&
+ do_xmailer_test 0 "--no-xmailer"
+'
+
+test_expect_success $PREREQ '--[no-]xmailer with sendemail.xmailer=true' '
+ test_config sendemail.xmailer true &&
+ do_xmailer_test 1 "" &&
+ do_xmailer_test 0 "--no-xmailer" &&
+ do_xmailer_test 1 "--xmailer"
+'
+
+test_expect_success $PREREQ '--[no-]xmailer with sendemail.xmailer' '
+ test_when_finished "test_unconfig sendemail.xmailer" &&
+ cat >>.git/config <<-\EOF &&
+ [sendemail]
+ xmailer
+ EOF
+ test_config sendemail.xmailer true &&
+ do_xmailer_test 1 "" &&
+ do_xmailer_test 0 "--no-xmailer" &&
+ do_xmailer_test 1 "--xmailer"
+'
+
+test_expect_success $PREREQ '--[no-]xmailer with sendemail.xmailer=false' '
+ test_config sendemail.xmailer false &&
+ do_xmailer_test 0 "" &&
+ do_xmailer_test 0 "--no-xmailer" &&
+ do_xmailer_test 1 "--xmailer"
+'
+
+test_expect_success $PREREQ '--[no-]xmailer with sendemail.xmailer=' '
+ test_config sendemail.xmailer "" &&
+ do_xmailer_test 0 "" &&
+ do_xmailer_test 0 "--no-xmailer" &&
+ do_xmailer_test 1 "--xmailer"
+'
+
+test_expect_success $PREREQ 'setup expected-list' '
+ git send-email \
+ --dry-run \
+ --from="Example <from@example.com>" \
+ --to="To 1 <to1@example.com>" \
+ --to="to2@example.com" \
+ --to="to3@example.com" \
+ --cc="Cc 1 <cc1@example.com>" \
+ --cc="Cc2 <cc2@example.com>" \
+ --bcc="bcc1@example.com" \
+ --bcc="bcc2@example.com" \
+ 0001-add-main.patch | replace_variable_fields \
+ >expected-list
+'
+
+test_expect_success $PREREQ 'use email list in --cc --to and --bcc' '
+ git send-email \
+ --dry-run \
+ --from="Example <from@example.com>" \
+ --to="To 1 <to1@example.com>, to2@example.com" \
+ --to="to3@example.com" \
+ --cc="Cc 1 <cc1@example.com>, Cc2 <cc2@example.com>" \
+ --bcc="bcc1@example.com, bcc2@example.com" \
+ 0001-add-main.patch | replace_variable_fields \
+ >actual-list &&
+ test_cmp expected-list actual-list
+'
+
+test_expect_success $PREREQ 'aliases work with email list' '
+ echo "alias to2 to2@example.com" >.mutt &&
+ echo "alias cc1 Cc 1 <cc1@example.com>" >>.mutt &&
+ test_config sendemail.aliasesfile ".mutt" &&
+ test_config sendemail.aliasfiletype mutt &&
+ git send-email \
+ --dry-run \
+ --from="Example <from@example.com>" \
+ --to="To 1 <to1@example.com>, to2, to3@example.com" \
+ --cc="cc1, Cc2 <cc2@example.com>" \
+ --bcc="bcc1@example.com, bcc2@example.com" \
+ 0001-add-main.patch | replace_variable_fields \
+ >actual-list &&
+ test_cmp expected-list actual-list
+'
+
+test_expect_success $PREREQ 'leading and trailing whitespaces are removed' '
+ echo "alias to2 to2@example.com" >.mutt &&
+ echo "alias cc1 Cc 1 <cc1@example.com>" >>.mutt &&
+ test_config sendemail.aliasesfile ".mutt" &&
+ test_config sendemail.aliasfiletype mutt &&
+ TO1=$(echo "QTo 1 <to1@example.com>" | q_to_tab) &&
+ TO2=$(echo "QZto2" | qz_to_tab_space) &&
+ CC1=$(echo "cc1" | append_cr) &&
+ BCC1=$(echo " bcc1@example.com Q" | q_to_nul) &&
+ git send-email \
+ --dry-run \
+ --from=" Example <from@example.com>" \
+ --to="$TO1" \
+ --to="$TO2" \
+ --to=" to3@example.com " \
+ --cc="$CC1" \
+ --cc="Cc2 <cc2@example.com>" \
+ --bcc="$BCC1" \
+ --bcc="bcc2@example.com" \
+ 0001-add-main.patch | replace_variable_fields \
+ >actual-list &&
+ test_cmp expected-list actual-list
+'
+
+test_expect_success $PREREQ 'test using command name with --sendmail-cmd' '
+ clean_fake_sendmail &&
+ PATH="$PWD:$PATH" \
+ git send-email \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --sendmail-cmd="fake.sendmail" \
+ HEAD^ &&
+ test_path_is_file commandline1
+'
+
+test_expect_success $PREREQ 'test using arguments with --sendmail-cmd' '
+ clean_fake_sendmail &&
+ git send-email \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --sendmail-cmd='\''"$(pwd)/fake.sendmail" -f nobody@example.com'\'' \
+ HEAD^ &&
+ test_path_is_file commandline1
+'
+
+test_expect_success $PREREQ 'test shell expression with --sendmail-cmd' '
+ clean_fake_sendmail &&
+ git send-email \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --sendmail-cmd='\''f() { "$(pwd)/fake.sendmail" "$@"; };f'\'' \
+ HEAD^ &&
+ test_path_is_file commandline1
+'
+
+test_expect_success $PREREQ 'set up in-reply-to/references patches' '
+ cat >has-reply.patch <<-\EOF &&
+ From: A U Thor <author@example.com>
+ Subject: patch with in-reply-to
+ Message-ID: <patch.with.in.reply.to@example.com>
+ In-Reply-To: <replied.to@example.com>
+ References: <replied.to@example.com>
+
+ This is the body.
+ EOF
+ cat >no-reply.patch <<-\EOF
+ From: A U Thor <author@example.com>
+ Subject: patch without in-reply-to
+ Message-ID: <patch.without.in.reply.to@example.com>
+
+ This is the body.
+ EOF
+'
+
+test_expect_success $PREREQ 'patch reply headers correct with --no-thread' '
+ clean_fake_sendmail &&
+ git send-email \
+ --no-thread \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ has-reply.patch no-reply.patch &&
+ grep "In-Reply-To: <replied.to@example.com>" msgtxt1 &&
+ grep "References: <replied.to@example.com>" msgtxt1 &&
+ ! grep replied.to@example.com msgtxt2
+'
+
+test_expect_success $PREREQ 'cmdline in-reply-to used with --no-thread' '
+ clean_fake_sendmail &&
+ git send-email \
+ --no-thread \
+ --in-reply-to="<cmdline.reply@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ has-reply.patch no-reply.patch &&
+ grep "In-Reply-To: <cmdline.reply@example.com>" msgtxt1 &&
+ grep "References: <cmdline.reply@example.com>" msgtxt1 &&
+ grep "In-Reply-To: <cmdline.reply@example.com>" msgtxt2 &&
+ grep "References: <cmdline.reply@example.com>" msgtxt2
+'
+
+test_expect_success $PREREQ 'invoke hook' '
+ test_hook sendemail-validate <<-\EOF &&
+ # test that we have the correct environment variable, pwd, and
+ # argument
+ case "$GIT_DIR" in
+ *.git)
+ true
+ ;;
+ *)
+ false
+ ;;
+ esac &&
+ test -f 0001-add-main.patch &&
+ grep "add main" "$1"
+ EOF
+
+ mkdir subdir &&
+ (
+ # Test that it works even if we are not at the root of the
+ # working tree
+ cd subdir &&
+ git send-email \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/../fake.sendmail" \
+ ../0001-add-main.patch &&
+
+ # Verify error message when a patch is rejected by the hook
+ sed -e "s/add main/x/" ../0001-add-main.patch >../another.patch &&
+ test_must_fail git send-email \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/../fake.sendmail" \
+ ../another.patch 2>err &&
+ test_grep "rejected by sendemail-validate hook" err
+ )
+'
+
+expected_file_counter_output () {
+ total=$1
+ count=0
+ while test $count -ne $total
+ do
+ count=$((count + 1)) &&
+ echo "$count/$total" || return
+ done
+}
+
+test_expect_success $PREREQ '--validate hook allows counting of messages' '
+ test_when_finished "rm -rf my-hooks.log" &&
+ test_config core.hooksPath "my-hooks" &&
+ mkdir -p my-hooks &&
+
+ write_script my-hooks/sendemail-validate <<-\EOF &&
+ num=$GIT_SENDEMAIL_FILE_COUNTER &&
+ tot=$GIT_SENDEMAIL_FILE_TOTAL &&
+ echo "$num/$tot" >>my-hooks.log || exit 1
+ EOF
+
+ >my-hooks.log &&
+ expected_file_counter_output 4 >expect &&
+ git send-email \
+ --from="Example <from@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ --validate -3 --cover-letter --force &&
+ test_cmp expect my-hooks.log
+'
+
+test_expect_success $PREREQ 'test that send-email works outside a repo' '
+ nongit git send-email \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ "$(pwd)/0001-add-main.patch"
+'
+
+test_expect_success $PREREQ 'send-email relays -v 3 to format-patch' '
+ test_when_finished "rm -f out" &&
+ git send-email --dry-run -v 3 -1 >out &&
+ grep "PATCH v3" out
+'
+
+test_expect_success $PREREQ 'test that sendmail config is rejected' '
+ test_config sendmail.program sendmail &&
+ test_must_fail git send-email \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ HEAD^ 2>err &&
+ test_grep "found configuration options for '"'"sendmail"'"'" err
+'
+
+test_expect_success $PREREQ 'test that sendmail config rejection is specific' '
+ test_config resendmail.program sendmail &&
+ git send-email \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ HEAD^
+'
+
+test_expect_success $PREREQ 'test forbidSendmailVariables behavior override' '
+ test_config sendmail.program sendmail &&
+ test_config sendemail.forbidSendmailVariables false &&
+ git send-email \
+ --from="Example <nobody@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ HEAD^
+'
+
+test_expect_success $PREREQ '--compose handles lowercase headers' '
+ write_script fake-editor <<-\EOF &&
+ sed "s/^From:.*/from: edited-from@example.com/i" "$1" >"$1.tmp" &&
+ mv "$1.tmp" "$1"
+ EOF
+ clean_fake_sendmail &&
+ git send-email \
+ --compose \
+ --from="Example <from@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ HEAD^ &&
+ grep "From: edited-from@example.com" msgtxt1
+'
+
+test_expect_success $PREREQ '--compose handles to headers' '
+ write_script fake-editor <<-\EOF &&
+ sed "s/^To: .*/&, edited-to@example.com/" <"$1" >"$1.tmp" &&
+ echo this is the body >>"$1.tmp" &&
+ mv "$1.tmp" "$1"
+ EOF
+ clean_fake_sendmail &&
+ GIT_SEND_EMAIL_NOTTY=1 \
+ git send-email \
+ --compose \
+ --from="Example <from@example.com>" \
+ --to=nobody@example.com \
+ --smtp-server="$(pwd)/fake.sendmail" \
+ HEAD^ &&
+ # Check both that the cover letter used our modified "to" line,
+ # but also that it was picked up for the patch.
+ q_to_tab >expect <<-\EOF &&
+ To: nobody@example.com,
+ Qedited-to@example.com
+ EOF
+ grep -A1 "^To:" msgtxt1 >msgtxt1.to &&
+ test_cmp expect msgtxt1.to &&
+ grep -A1 "^To:" msgtxt2 >msgtxt2.to &&
+ test_cmp expect msgtxt2.to
+'
+
+test_done
diff --git a/t/t9002-column.sh b/t/t9002-column.sh
new file mode 100755
index 0000000..6d3dbde
--- /dev/null
+++ b/t/t9002-column.sh
@@ -0,0 +1,198 @@
+#!/bin/sh
+
+test_description='git column'
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ cat >lista <<\EOF
+one
+two
+three
+four
+five
+six
+seven
+eight
+nine
+ten
+eleven
+EOF
+'
+
+test_expect_success 'never' '
+ git column --indent=Z --mode=never <lista >actual &&
+ test_cmp lista actual
+'
+
+test_expect_success 'always' '
+ cat >expected <<\EOF &&
+Zone
+Ztwo
+Zthree
+Zfour
+Zfive
+Zsix
+Zseven
+Zeight
+Znine
+Zten
+Zeleven
+EOF
+ git column --indent=Z --mode=plain <lista >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success '--nl' '
+ cat >expected <<\EOF &&
+oneZ
+twoZ
+threeZ
+fourZ
+fiveZ
+sixZ
+sevenZ
+eightZ
+nineZ
+tenZ
+elevenZ
+EOF
+ git column --nl="Z$LF" --mode=plain <lista >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success '80 columns' '
+ cat >expected <<\EOF &&
+one two three four five six seven eight nine ten eleven
+EOF
+ COLUMNS=80 git column --mode=column <lista >actual &&
+ test_cmp expected actual
+'
+
+cat >expected <<\EOF
+one
+two
+three
+four
+five
+six
+seven
+eight
+nine
+ten
+eleven
+EOF
+
+test_expect_success COLUMNS_CAN_BE_1 'COLUMNS = 1' '
+ COLUMNS=1 git column --mode=column <lista >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'width = 1' '
+ git column --mode=column --width=1 <lista >actual &&
+ test_cmp expected actual
+'
+
+COLUMNS=20
+export COLUMNS
+
+test_expect_success '20 columns' '
+ cat >expected <<\EOF &&
+one seven
+two eight
+three nine
+four ten
+five eleven
+six
+EOF
+ git column --mode=column <lista >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success '20 columns, nodense' '
+ cat >expected <<\EOF &&
+one seven
+two eight
+three nine
+four ten
+five eleven
+six
+EOF
+ git column --mode=column,nodense < lista > actual &&
+ test_cmp expected actual
+'
+
+test_expect_success '20 columns, dense' '
+ cat >expected <<\EOF &&
+one five nine
+two six ten
+three seven eleven
+four eight
+EOF
+ git column --mode=column,dense < lista > actual &&
+ test_cmp expected actual
+'
+
+test_expect_success '20 columns, padding 2' '
+ cat >expected <<\EOF &&
+one seven
+two eight
+three nine
+four ten
+five eleven
+six
+EOF
+ git column --mode=column --padding 2 <lista >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success '20 columns, indented' '
+ cat >expected <<\EOF &&
+ one seven
+ two eight
+ three nine
+ four ten
+ five eleven
+ six
+EOF
+ git column --mode=column --indent=" " <lista >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success '20 columns, row first' '
+ cat >expected <<\EOF &&
+one two
+three four
+five six
+seven eight
+nine ten
+eleven
+EOF
+ git column --mode=row <lista >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success '20 columns, row first, nodense' '
+ cat >expected <<\EOF &&
+one two
+three four
+five six
+seven eight
+nine ten
+eleven
+EOF
+ git column --mode=row,nodense <lista >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success '20 columns, row first, dense' '
+ cat >expected <<\EOF &&
+one two three
+four five six
+seven eight nine
+ten eleven
+EOF
+ git column --mode=row,dense <lista >actual &&
+ test_cmp expected actual
+'
+
+test_done
diff --git a/t/t9003-help-autocorrect.sh b/t/t9003-help-autocorrect.sh
new file mode 100755
index 0000000..14a704d
--- /dev/null
+++ b/t/t9003-help-autocorrect.sh
@@ -0,0 +1,71 @@
+#!/bin/sh
+
+test_description='help.autocorrect finding a match'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+ # An alias
+ git config alias.lgf "log --format=%s --first-parent" &&
+
+ # A random user-defined command
+ write_script git-distimdistim <<-EOF &&
+ echo distimdistim was called
+ EOF
+
+ PATH="$PATH:." &&
+ export PATH &&
+
+ git commit --allow-empty -m "a single log entry" &&
+
+ # Sanity check
+ git lgf >actual &&
+ echo "a single log entry" >expect &&
+ test_cmp expect actual &&
+
+ git distimdistim >actual &&
+ echo "distimdistim was called" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'autocorrect showing candidates' '
+ git config help.autocorrect 0 &&
+
+ test_must_fail git lfg 2>actual &&
+ grep "^ lgf" actual &&
+
+ test_must_fail git distimdist 2>actual &&
+ grep "^ distimdistim" actual
+'
+
+for immediate in -1 immediate
+do
+ test_expect_success 'autocorrect running commands' '
+ git config help.autocorrect $immediate &&
+
+ git lfg >actual &&
+ echo "a single log entry" >expect &&
+ test_cmp expect actual &&
+
+ git distimdist >actual &&
+ echo "distimdistim was called" >expect &&
+ test_cmp expect actual
+ '
+done
+
+test_expect_success 'autocorrect can be declined altogether' '
+ git config help.autocorrect never &&
+
+ test_must_fail git lfg 2>actual &&
+ grep "is not a git command" actual &&
+ test_line_count = 1 actual
+'
+
+test_expect_success 'autocorrect works in work tree created from bare repo' '
+ git clone --bare . bare.git &&
+ git -C bare.git worktree add ../worktree &&
+ git -C worktree -c help.autocorrect=immediate stauts
+'
+
+test_done
diff --git a/t/t9004-example.sh b/t/t9004-example.sh
new file mode 100755
index 0000000..590aab0
--- /dev/null
+++ b/t/t9004-example.sh
@@ -0,0 +1,12 @@
+#!/bin/sh
+
+test_description='check that example code compiles and runs'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'decorate' '
+ test-tool example-decorate
+'
+
+test_done
diff --git a/t/t9100-git-svn-basic.sh b/t/t9100-git-svn-basic.sh
new file mode 100755
index 0000000..af28b01
--- /dev/null
+++ b/t/t9100-git-svn-basic.sh
@@ -0,0 +1,333 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Eric Wong
+#
+
+test_description='git svn basic tests'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./lib-git-svn.sh
+
+prepare_utf8_locale
+
+test_expect_success 'git svn --version works anywhere' '
+ nongit git svn --version
+'
+
+test_expect_success 'git svn help works anywhere' '
+ nongit git svn help
+'
+
+test_expect_success \
+ 'initialize git svn' '
+ mkdir import &&
+ (
+ cd import &&
+ echo foo >foo &&
+ ln -s foo foo.link &&
+ mkdir -p dir/a/b/c/d/e &&
+ echo "deep dir" >dir/a/b/c/d/e/file &&
+ mkdir bar &&
+ echo "zzz" >bar/zzz &&
+ echo "#!/bin/sh" >exec.sh &&
+ chmod +x exec.sh &&
+ svn_cmd import -m "import for git svn" . "$svnrepo" >/dev/null
+ ) &&
+ rm -rf import &&
+ git svn init "$svnrepo"'
+
+test_expect_success 'import an SVN revision into git' '
+ git svn fetch
+'
+
+test_expect_success "checkout from svn" 'svn co "$svnrepo" "$SVN_TREE"'
+
+name='try a deep --rmdir with a commit'
+test_expect_success "$name" '
+ git checkout -f -b mybranch remotes/git-svn &&
+ mv dir/a/b/c/d/e/file dir/file &&
+ cp dir/file file &&
+ git update-index --add --remove dir/a/b/c/d/e/file dir/file file &&
+ git commit -m "$name" &&
+ git svn set-tree --find-copies-harder --rmdir \
+ remotes/git-svn..mybranch &&
+ svn_cmd up "$SVN_TREE" &&
+ test -d "$SVN_TREE"/dir && test ! -d "$SVN_TREE"/dir/a'
+
+
+name='detect node change from file to directory #1'
+test_expect_success "$name" '
+ mkdir dir/new_file &&
+ mv dir/file dir/new_file/file &&
+ mv dir/new_file dir/file &&
+ git update-index --remove dir/file &&
+ git update-index --add dir/file/file &&
+ git commit -m "$name" &&
+ test_must_fail git svn set-tree --find-copies-harder --rmdir \
+ remotes/git-svn..mybranch
+'
+
+
+name='detect node change from directory to file #1'
+test_expect_success "$name" '
+ rm -rf dir "$GIT_DIR"/index &&
+ git checkout -f -b mybranch2 remotes/git-svn &&
+ mv bar/zzz zzz &&
+ rm -rf bar &&
+ mv zzz bar &&
+ git update-index --remove -- bar/zzz &&
+ git update-index --add -- bar &&
+ git commit -m "$name" &&
+ test_must_fail git svn set-tree --find-copies-harder --rmdir \
+ remotes/git-svn..mybranch2
+'
+
+
+name='detect node change from file to directory #2'
+test_expect_success "$name" '
+ rm -f "$GIT_DIR"/index &&
+ git checkout -f -b mybranch3 remotes/git-svn &&
+ rm bar/zzz &&
+ git update-index --remove bar/zzz &&
+ mkdir bar/zzz &&
+ echo yyy > bar/zzz/yyy &&
+ git update-index --add bar/zzz/yyy &&
+ git commit -m "$name" &&
+ git svn set-tree --find-copies-harder --rmdir \
+ remotes/git-svn..mybranch3 &&
+ svn_cmd up "$SVN_TREE" &&
+ test -d "$SVN_TREE"/bar/zzz &&
+ test -e "$SVN_TREE"/bar/zzz/yyy
+'
+
+name='detect node change from directory to file #2'
+test_expect_success "$name" '
+ rm -f "$GIT_DIR"/index &&
+ git checkout -f -b mybranch4 remotes/git-svn &&
+ rm -rf dir &&
+ git update-index --remove -- dir/file &&
+ touch dir &&
+ echo asdf > dir &&
+ git update-index --add -- dir &&
+ git commit -m "$name" &&
+ test_must_fail git svn set-tree --find-copies-harder --rmdir \
+ remotes/git-svn..mybranch4
+'
+
+
+name='remove executable bit from a file'
+test_expect_success POSIXPERM "$name" '
+ rm -f "$GIT_DIR"/index &&
+ git checkout -f -b mybranch5 remotes/git-svn &&
+ chmod -x exec.sh &&
+ git update-index exec.sh &&
+ git commit -m "$name" &&
+ git svn set-tree --find-copies-harder --rmdir \
+ remotes/git-svn..mybranch5 &&
+ svn_cmd up "$SVN_TREE" &&
+ test ! -x "$SVN_TREE"/exec.sh'
+
+
+name='add executable bit back file'
+test_expect_success POSIXPERM "$name" '
+ chmod +x exec.sh &&
+ git update-index exec.sh &&
+ git commit -m "$name" &&
+ git svn set-tree --find-copies-harder --rmdir \
+ remotes/git-svn..mybranch5 &&
+ svn_cmd up "$SVN_TREE" &&
+ test -x "$SVN_TREE"/exec.sh'
+
+
+name='executable file becomes a symlink to file'
+test_expect_success SYMLINKS "$name" '
+ rm exec.sh &&
+ ln -s file exec.sh &&
+ git update-index exec.sh &&
+ git commit -m "$name" &&
+ git svn set-tree --find-copies-harder --rmdir \
+ remotes/git-svn..mybranch5 &&
+ svn_cmd up "$SVN_TREE" &&
+ test -h "$SVN_TREE"/exec.sh'
+
+name='new symlink is added to a file that was also just made executable'
+
+test_expect_success POSIXPERM,SYMLINKS "$name" '
+ chmod +x file &&
+ ln -s file exec-2.sh &&
+ git update-index --add file exec-2.sh &&
+ git commit -m "$name" &&
+ git svn set-tree --find-copies-harder --rmdir \
+ remotes/git-svn..mybranch5 &&
+ svn_cmd up "$SVN_TREE" &&
+ test -x "$SVN_TREE"/file &&
+ test -h "$SVN_TREE"/exec-2.sh'
+
+name='modify a symlink to become a file'
+test_expect_success POSIXPERM,SYMLINKS "$name" '
+ echo git help >help &&
+ rm exec-2.sh &&
+ cp help exec-2.sh &&
+ git update-index exec-2.sh &&
+ git commit -m "$name" &&
+ git svn set-tree --find-copies-harder --rmdir \
+ remotes/git-svn..mybranch5 &&
+ svn_cmd up "$SVN_TREE" &&
+ test -f "$SVN_TREE"/exec-2.sh &&
+ test ! -h "$SVN_TREE"/exec-2.sh &&
+ test_cmp help "$SVN_TREE"/exec-2.sh'
+
+name="commit with UTF-8 message: locale: $GIT_TEST_UTF8_LOCALE"
+LC_ALL="$GIT_TEST_UTF8_LOCALE"
+export LC_ALL
+# This test relies on the previous test, hence requires POSIXPERM,SYMLINKS
+test_expect_success UTF8,POSIXPERM,SYMLINKS "$name" "
+ echo '# hello' >> exec-2.sh &&
+ git update-index exec-2.sh &&
+ git commit -m 'éïâˆ' &&
+ git svn set-tree HEAD"
+unset LC_ALL
+
+name='test fetch functionality (svn => git) with alternate GIT_SVN_ID'
+GIT_SVN_ID=alt
+export GIT_SVN_ID
+test_expect_success "$name" \
+ 'git svn init "$svnrepo" && git svn fetch &&
+ git log --format="tree %T %s" remotes/git-svn |
+ awk "!seen[\$0]++ { print \$1, \$2 }" >a &&
+ git log --format="tree %T" alt >b &&
+ test_cmp a b'
+
+name='check imported tree checksums expected tree checksums'
+rm -f expected
+if test_have_prereq UTF8
+then
+ echo tree dc68b14b733e4ec85b04ab6f712340edc5dc936e > expected.sha1
+ echo tree b95b55b29d771f5eb73aa9b9d52d02fe11a2538c2feb0829f754ce20a91d98eb > expected.sha256
+fi
+cat >> expected.sha1 <<\EOF
+tree c3322890dcf74901f32d216f05c5044f670ce632
+tree d3ccd5035feafd17b030c5732e7808cc49122853
+tree d03e1630363d4881e68929d532746b20b0986b83
+tree 149d63cd5878155c846e8c55d7d8487de283f89e
+tree 312b76e4f64ce14893aeac8591eb3960b065e247
+tree 149d63cd5878155c846e8c55d7d8487de283f89e
+tree d667270a1f7b109f5eb3aaea21ede14b56bfdd6e
+tree 8f51f74cf0163afc9ad68a4b1537288c4558b5a4
+EOF
+cat >> expected.sha256 <<\EOF
+tree 8d12756699d0b5b110514240a0ff141f6cbf8891fd69ab05e5594196fb437c9f
+tree 8187168d33f7d4ccb8c1cc6e99532810aaccb47658f35d19b3803072d1128d7a
+tree 74e535d85da8ee25eb23d7b506790c5ab3ccdb1ba0826bd57625ed44ef361650
+tree 6fd7dd963e3cdca0cbd6368ed3cfcc8037cc154d2e7719d9d369a0952364fd95
+tree 1fd6cec6aa95102d69266e20419bb62ec2a06372d614b9850ef23ff204103bb4
+tree 6fd7dd963e3cdca0cbd6368ed3cfcc8037cc154d2e7719d9d369a0952364fd95
+tree deb2b7ac79cd8ce6f52af6a5a0a08691e94ba74a2ed55966bb27dbec551730eb
+tree 59e2e936761188476a7752034e8aa0a822b34050c8504b0dfd946407f4bc9215
+EOF
+
+test_expect_success POSIXPERM,SYMLINKS "$name" '
+ test_cmp expected.$(test_oid algo) a
+'
+
+test_expect_success 'exit if remote refs are ambigious' '
+ git config --add svn-remote.svn.fetch \
+ bar:refs/remotes/git-svn &&
+ test_must_fail git svn migrate
+'
+
+test_expect_success 'exit if init-ing a would clobber a URL' '
+ svnadmin create "${PWD}/svnrepo2" &&
+ svn mkdir -m "mkdir bar" "${svnrepo}2/bar" &&
+ git config --unset svn-remote.svn.fetch \
+ "^bar:refs/remotes/git-svn$" &&
+ test_must_fail git svn init "${svnrepo}2/bar"
+ '
+
+test_expect_success 'init allows us to connect to another directory in the same repo' '
+ git svn init --minimize-url -i bar "$svnrepo/bar" &&
+ git config --get svn-remote.svn.fetch \
+ "^bar:refs/remotes/bar$" &&
+ git config --get svn-remote.svn.fetch \
+ "^:refs/remotes/git-svn$"
+'
+
+test_expect_success 'dcommit $rev does not clobber current branch' '
+ git svn fetch -i bar &&
+ git checkout -b my-bar refs/remotes/bar &&
+ echo 1 > foo &&
+ git add foo &&
+ git commit -m "change 1" &&
+ echo 2 > foo &&
+ git add foo &&
+ git commit -m "change 2" &&
+ old_head=$(git rev-parse HEAD) &&
+ git svn dcommit -i bar HEAD^ &&
+ test $old_head = $(git rev-parse HEAD) &&
+ test refs/heads/my-bar = $(git symbolic-ref HEAD) &&
+ git log refs/remotes/bar | grep "change 1" &&
+ ! git log refs/remotes/bar | grep "change 2" &&
+ git checkout main &&
+ git branch -D my-bar
+ '
+
+test_expect_success 'able to dcommit to a subdirectory' '
+ git svn fetch -i bar &&
+ git checkout -b my-bar refs/remotes/bar &&
+ echo abc > d &&
+ git update-index --add d &&
+ git commit -m "/bar/d should be in the log" &&
+ git svn dcommit -i bar &&
+ test -z "$(git diff refs/heads/my-bar refs/remotes/bar)" &&
+ mkdir newdir &&
+ echo new > newdir/dir &&
+ git update-index --add newdir/dir &&
+ git commit -m "add a new directory" &&
+ git svn dcommit -i bar &&
+ test -z "$(git diff refs/heads/my-bar refs/remotes/bar)" &&
+ echo foo >> newdir/dir &&
+ git update-index newdir/dir &&
+ git commit -m "modify a file in new directory" &&
+ git svn dcommit -i bar &&
+ test -z "$(git diff refs/heads/my-bar refs/remotes/bar)"
+'
+
+test_expect_success 'dcommit should not fail with a touched file' '
+ test_commit "commit-new-file-foo2" foo2 &&
+ test-tool chmtime =-60 foo &&
+ git svn dcommit
+'
+
+test_expect_success 'rebase should not fail with a touched file' '
+ test-tool chmtime =-60 foo &&
+ git svn rebase
+'
+
+test_expect_success 'able to set-tree to a subdirectory' '
+ echo cba > d &&
+ git update-index d &&
+ git commit -m "update /bar/d" &&
+ git svn set-tree -i bar HEAD &&
+ test -z "$(git diff refs/heads/my-bar refs/remotes/bar)"
+'
+
+test_expect_success 'git-svn works in a bare repository' '
+ mkdir bare-repo &&
+ ( cd bare-repo &&
+ git init --bare &&
+ GIT_DIR=. git svn init "$svnrepo" &&
+ git svn fetch ) &&
+ rm -rf bare-repo
+ '
+test_expect_success 'git-svn works in a repository with a gitdir: link' '
+ mkdir worktree gitdir &&
+ ( cd worktree &&
+ git svn init "$svnrepo" &&
+ git init --separate-git-dir ../gitdir &&
+ git svn fetch ) &&
+ rm -rf worktree gitdir
+ '
+
+test_done
diff --git a/t/t9101-git-svn-props.sh b/t/t9101-git-svn-props.sh
new file mode 100755
index 0000000..52046e6
--- /dev/null
+++ b/t/t9101-git-svn-props.sh
@@ -0,0 +1,236 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Eric Wong
+#
+
+test_description='git svn property tests'
+
+. ./lib-git-svn.sh
+
+mkdir import
+
+a_crlf=
+a_lf=
+a_cr=
+a_ne_crlf=
+a_ne_lf=
+a_ne_cr=
+a_empty=
+a_empty_lf=
+a_empty_cr=
+a_empty_crlf=
+
+cd import
+ cat >> kw.c <<\EOF
+/* Somebody prematurely put a keyword into this file */
+/* $Id$ */
+EOF
+
+ printf "Hello\r\nWorld\r\n" > crlf
+ a_crlf=$(git hash-object -w crlf)
+ printf "Hello\rWorld\r" > cr
+ a_cr=$(git hash-object -w cr)
+ printf "Hello\nWorld\n" > lf
+ a_lf=$(git hash-object -w lf)
+
+ printf "Hello\r\nWorld" > ne_crlf
+ a_ne_crlf=$(git hash-object -w ne_crlf)
+ printf "Hello\nWorld" > ne_lf
+ a_ne_lf=$(git hash-object -w ne_lf)
+ printf "Hello\rWorld" > ne_cr
+ a_ne_cr=$(git hash-object -w ne_cr)
+
+ touch empty
+ a_empty=$(git hash-object -w empty)
+ printf "\n" > empty_lf
+ a_empty_lf=$(git hash-object -w empty_lf)
+ printf "\r" > empty_cr
+ a_empty_cr=$(git hash-object -w empty_cr)
+ printf "\r\n" > empty_crlf
+ a_empty_crlf=$(git hash-object -w empty_crlf)
+
+ svn_cmd import --no-auto-props -m 'import for git svn' . "$svnrepo" >/dev/null
+cd ..
+
+rm -rf import
+test_expect_success 'checkout working copy from svn' 'svn co "$svnrepo" test_wc'
+test_expect_success 'setup some commits to svn' '
+ (
+ cd test_wc &&
+ echo Greetings >> kw.c &&
+ poke kw.c &&
+ svn_cmd commit -m "Not yet an Id" &&
+ echo Hello world >> kw.c &&
+ poke kw.c &&
+ svn_cmd commit -m "Modified file, but still not yet an Id" &&
+ svn_cmd propset svn:keywords Id kw.c &&
+ poke kw.c &&
+ svn_cmd commit -m "Propset Id"
+ )
+'
+
+test_expect_success 'initialize git svn' 'git svn init "$svnrepo"'
+test_expect_success 'fetch revisions from svn' 'git svn fetch'
+
+name='test svn:keywords ignoring'
+test_expect_success "$name" \
+ 'git checkout -b mybranch remotes/git-svn &&
+ echo Hi again >> kw.c &&
+ git commit -a -m "test keywords ignoring" &&
+ git svn set-tree remotes/git-svn..mybranch &&
+ git pull . remotes/git-svn'
+
+expect='/* $Id$ */'
+got="$(sed -ne 2p kw.c)"
+test_expect_success 'raw $Id$ found in kw.c' "test '$expect' = '$got'"
+
+test_expect_success "propset CR on crlf files" '
+ (
+ cd test_wc &&
+ svn_cmd propset svn:eol-style CR empty &&
+ svn_cmd propset svn:eol-style CR crlf &&
+ svn_cmd propset svn:eol-style CR ne_crlf &&
+ svn_cmd commit -m "propset CR on crlf files"
+ )
+'
+
+test_expect_success 'fetch and pull latest from svn and checkout a new wc' \
+ 'git svn fetch &&
+ git pull . remotes/git-svn &&
+ svn_cmd co "$svnrepo" new_wc'
+
+for i in crlf ne_crlf lf ne_lf cr ne_cr empty_cr empty_lf empty empty_crlf
+do
+ test_expect_success "Comparing $i" "cmp $i new_wc/$i"
+done
+
+
+cd test_wc
+ printf '$Id$\rHello\rWorld\r' > cr
+ printf '$Id$\rHello\rWorld' > ne_cr
+ a_cr=$(printf '$Id$\r\nHello\r\nWorld\r\n' | git hash-object --stdin)
+ a_ne_cr=$(printf '$Id$\r\nHello\r\nWorld' | git hash-object --stdin)
+ test_expect_success 'Set CRLF on cr files' \
+ 'svn_cmd propset svn:eol-style CRLF cr &&
+ svn_cmd propset svn:eol-style CRLF ne_cr &&
+ svn_cmd propset svn:keywords Id cr &&
+ svn_cmd propset svn:keywords Id ne_cr &&
+ svn_cmd commit -m "propset CRLF on cr files"'
+cd ..
+test_expect_success 'fetch and pull latest from svn' \
+ 'git svn fetch && git pull . remotes/git-svn'
+
+b_cr="$(git hash-object cr)"
+b_ne_cr="$(git hash-object ne_cr)"
+
+test_expect_success 'CRLF + $Id$' "test '$a_cr' = '$b_cr'"
+test_expect_success 'CRLF + $Id$ (no newline)' "test '$a_ne_cr' = '$b_ne_cr'"
+
+cat > show-ignore.expect <<\EOF
+
+# /
+/no-such-file*
+
+# /deeply/
+/deeply/no-such-file*
+
+# /deeply/nested/
+/deeply/nested/no-such-file*
+
+# /deeply/nested/directory/
+/deeply/nested/directory/no-such-file*
+EOF
+
+test_expect_success 'test show-ignore' "
+ (
+ cd test_wc &&
+ mkdir -p deeply/nested/directory &&
+ touch deeply/nested/directory/.keep &&
+ svn_cmd add deeply &&
+ svn_cmd up &&
+ svn_cmd propset -R svn:ignore '
+no-such-file*
+' . &&
+ svn_cmd commit -m 'propset svn:ignore'
+ ) &&
+ git svn show-ignore > show-ignore.got &&
+ cmp show-ignore.expect show-ignore.got
+"
+
+cat >create-ignore.expect <<\EOF
+/no-such-file*
+EOF
+
+expectoid=$(git hash-object create-ignore.expect)
+
+cat >create-ignore-index.expect <<EOF
+100644 $expectoid 0 .gitignore
+100644 $expectoid 0 deeply/.gitignore
+100644 $expectoid 0 deeply/nested/.gitignore
+100644 $expectoid 0 deeply/nested/directory/.gitignore
+EOF
+
+test_expect_success 'test create-ignore' "
+ git svn fetch && git pull . remotes/git-svn &&
+ git svn create-ignore &&
+ cmp ./.gitignore create-ignore.expect &&
+ cmp ./deeply/.gitignore create-ignore.expect &&
+ cmp ./deeply/nested/.gitignore create-ignore.expect &&
+ cmp ./deeply/nested/directory/.gitignore create-ignore.expect &&
+ git ls-files -s >ls_files_result &&
+ grep gitignore ls_files_result | cmp - create-ignore-index.expect
+ "
+
+cat >prop.expect <<\EOF
+
+no-such-file*
+
+EOF
+cat >prop2.expect <<\EOF
+8
+EOF
+
+# This test can be improved: since all the svn:ignore contain the same
+# pattern, it can pass even though the propget did not execute on the
+# right directory.
+test_expect_success 'test propget' '
+ test_propget () {
+ git svn propget $1 $2 >actual &&
+ cmp $3 actual
+ } &&
+ test_propget svn:ignore . prop.expect &&
+ cd deeply &&
+ test_propget svn:ignore . ../prop.expect &&
+ test_propget svn:entry:committed-rev nested/directory/.keep \
+ ../prop2.expect &&
+ test_propget svn:ignore .. ../prop.expect &&
+ test_propget svn:ignore nested/ ../prop.expect &&
+ test_propget svn:ignore ./nested ../prop.expect &&
+ test_propget svn:ignore .././deeply/nested ../prop.expect
+ '
+
+cat >prop.expect <<\EOF
+Properties on '.':
+ svn:entry:committed-date
+ svn:entry:committed-rev
+ svn:entry:last-author
+ svn:entry:uuid
+ svn:ignore
+EOF
+cat >prop2.expect <<\EOF
+Properties on 'nested/directory/.keep':
+ svn:entry:committed-date
+ svn:entry:committed-rev
+ svn:entry:last-author
+ svn:entry:uuid
+EOF
+
+test_expect_success 'test proplist' "
+ git svn proplist . >actual &&
+ cmp prop.expect actual &&
+
+ git svn proplist nested/directory/.keep >actual &&
+ cmp prop2.expect actual
+ "
+
+test_done
diff --git a/t/t9102-git-svn-deep-rmdir.sh b/t/t9102-git-svn-deep-rmdir.sh
new file mode 100755
index 0000000..946ef85
--- /dev/null
+++ b/t/t9102-git-svn-deep-rmdir.sh
@@ -0,0 +1,32 @@
+#!/bin/sh
+test_description='git svn rmdir'
+
+. ./lib-git-svn.sh
+
+test_expect_success 'initialize repo' '
+ mkdir import &&
+ (
+ cd import &&
+ mkdir -p deeply/nested/directory/number/1 &&
+ mkdir -p deeply/nested/directory/number/2 &&
+ echo foo >deeply/nested/directory/number/1/file &&
+ echo foo >deeply/nested/directory/number/2/another &&
+ svn_cmd import -m "import for git svn" . "$svnrepo"
+ )
+ '
+
+test_expect_success 'mirror via git svn' '
+ git svn init "$svnrepo" &&
+ git svn fetch &&
+ git checkout -f -b test-rmdir remotes/git-svn
+ '
+
+test_expect_success 'Try a commit on rmdir' '
+ git rm -f deeply/nested/directory/number/2/another &&
+ git commit -a -m "remove another" &&
+ git svn set-tree --rmdir HEAD &&
+ svn_cmd ls -R "$svnrepo" | grep ^deeply/nested/directory/number/1
+ '
+
+
+test_done
diff --git a/t/t9103-git-svn-tracked-directory-removed.sh b/t/t9103-git-svn-tracked-directory-removed.sh
new file mode 100755
index 0000000..b282713
--- /dev/null
+++ b/t/t9103-git-svn-tracked-directory-removed.sh
@@ -0,0 +1,41 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Eric Wong
+#
+
+test_description='git svn tracking removed top-level path'
+. ./lib-git-svn.sh
+
+test_expect_success 'make history for tracking' '
+ mkdir import &&
+ mkdir import/trunk &&
+ echo hello >> import/trunk/README &&
+ svn_cmd import -m initial import "$svnrepo" &&
+ rm -rf import &&
+ svn_cmd co "$svnrepo"/trunk trunk &&
+ echo bye bye >> trunk/README &&
+ svn_cmd rm -m "gone" "$svnrepo"/trunk &&
+ rm -rf trunk &&
+ mkdir trunk &&
+ echo "new" > trunk/FOLLOWME &&
+ svn_cmd import -m "new trunk" trunk "$svnrepo"/trunk
+'
+
+test_expect_success 'clone repo with git' '
+ git svn clone -s "$svnrepo" x &&
+ test_path_is_file x/FOLLOWME &&
+ test_path_is_missing x/README
+'
+
+test_expect_success 'make sure r2 still has old file' '
+ (
+ cd x &&
+ test -n "$(git svn find-rev r1)" &&
+ git reset --hard "$(git svn find-rev r1)" &&
+ test_path_is_file README &&
+ test_path_is_missing FOLLOWME &&
+ test -z "$(git svn find-rev r2)"
+ )
+'
+
+test_done
diff --git a/t/t9104-git-svn-follow-parent.sh b/t/t9104-git-svn-follow-parent.sh
new file mode 100755
index 0000000..b5845e2
--- /dev/null
+++ b/t/t9104-git-svn-follow-parent.sh
@@ -0,0 +1,230 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Eric Wong
+#
+
+test_description='git svn fetching'
+
+. ./lib-git-svn.sh
+
+test_expect_success 'initialize repo' '
+ mkdir import &&
+ (
+ cd import &&
+ mkdir -p trunk &&
+ echo hello >trunk/readme &&
+ svn_cmd import -m "initial" . "$svnrepo"
+ ) &&
+ svn_cmd co "$svnrepo" wc &&
+ (
+ cd wc &&
+ echo world >>trunk/readme &&
+ poke trunk/readme &&
+ svn_cmd commit -m "another commit" &&
+ svn_cmd up &&
+ svn_cmd mv trunk thunk &&
+ echo goodbye >>thunk/readme &&
+ poke thunk/readme &&
+ svn_cmd commit -m "bye now"
+ )
+ '
+
+test_expect_success 'init and fetch a moved directory' '
+ git svn init --minimize-url -i thunk "$svnrepo"/thunk &&
+ git svn fetch -i thunk &&
+ test "$(git rev-parse --verify refs/remotes/thunk@2)" \
+ = "$(git rev-parse --verify refs/remotes/thunk~1)" &&
+ git cat-file blob refs/remotes/thunk:readme >actual &&
+ test "$(sed -n -e "3p" actual)" = goodbye &&
+ test -z "$(git config --get svn-remote.svn.fetch \
+ "^trunk:refs/remotes/thunk@2$")"
+ '
+
+test_expect_success 'init and fetch from one svn-remote' '
+ git config svn-remote.svn.url "$svnrepo" &&
+ git config --add svn-remote.svn.fetch \
+ trunk:refs/remotes/svn/trunk &&
+ git config --add svn-remote.svn.fetch \
+ thunk:refs/remotes/svn/thunk &&
+ git svn fetch -i svn/thunk &&
+ test "$(git rev-parse --verify refs/remotes/svn/trunk)" \
+ = "$(git rev-parse --verify refs/remotes/svn/thunk~1)" &&
+ git cat-file blob refs/remotes/svn/thunk:readme >actual &&
+ test "$(sed -n -e "3p" actual)" = goodbye
+'
+
+test_expect_success 'follow deleted parent' '
+ (svn_cmd cp -m "resurrecting trunk as junk" \
+ "$svnrepo"/trunk@2 "$svnrepo"/junk ||
+ svn cp -m "resurrecting trunk as junk" \
+ -r2 "$svnrepo"/trunk "$svnrepo"/junk) &&
+ git config --add svn-remote.svn.fetch \
+ junk:refs/remotes/svn/junk &&
+ git svn fetch -i svn/thunk &&
+ git svn fetch -i svn/junk &&
+ test -z "$(git diff svn/junk svn/trunk)" &&
+ test "$(git merge-base svn/junk svn/trunk)" \
+ = "$(git rev-parse svn/trunk)"
+'
+
+test_expect_success 'follow larger parent' '
+ mkdir -p import/trunk/thunk/bump/thud &&
+ echo hi > import/trunk/thunk/bump/thud/file &&
+ svn import -m "import a larger parent" import "$svnrepo"/larger-parent &&
+ svn cp -m "hi" "$svnrepo"/larger-parent "$svnrepo"/another-larger &&
+ git svn init --minimize-url -i larger \
+ "$svnrepo"/larger-parent/trunk/thunk/bump/thud &&
+ git svn fetch -i larger &&
+ git svn init --minimize-url -i larger-parent \
+ "$svnrepo"/another-larger/trunk/thunk/bump/thud &&
+ git svn fetch -i larger-parent &&
+ git rev-parse --verify refs/remotes/larger &&
+ git rev-parse --verify \
+ refs/remotes/larger-parent &&
+ test "$(git merge-base \
+ refs/remotes/larger-parent \
+ refs/remotes/larger)" = \
+ "$(git rev-parse refs/remotes/larger)"
+'
+
+test_expect_success 'follow higher-level parent' '
+ svn mkdir -m "follow higher-level parent" "$svnrepo"/blob &&
+ svn co "$svnrepo"/blob blob &&
+ (
+ cd blob &&
+ echo hi > hi &&
+ svn add hi &&
+ svn commit -m "hihi"
+ ) &&
+ svn mkdir -m "new glob at top level" "$svnrepo"/glob &&
+ svn mv -m "move blob down a level" "$svnrepo"/blob "$svnrepo"/glob/blob &&
+ git svn init --minimize-url -i blob "$svnrepo"/glob/blob &&
+ git svn fetch -i blob
+ '
+
+test_expect_success 'follow deleted directory' '
+ svn_cmd mv -m "bye!" "$svnrepo"/glob/blob/hi "$svnrepo"/glob/blob/bye &&
+ svn_cmd rm -m "remove glob" "$svnrepo"/glob &&
+ git svn init --minimize-url -i glob "$svnrepo"/glob &&
+ git svn fetch -i glob &&
+ test "$(git cat-file blob refs/remotes/glob:blob/bye)" = hi &&
+ git ls-tree refs/remotes/glob >actual &&
+ test_line_count = 1 actual
+ '
+
+# ref: r9270 of the Subversion repository: (http://svn.collab.net/repos/svn)
+# in trunk/subversion/bindings/swig/perl
+test_expect_success 'follow-parent avoids deleting relevant info' '
+ mkdir -p import/trunk/subversion/bindings/swig/perl/t &&
+ for i in a b c ; do \
+ echo $i > import/trunk/subversion/bindings/swig/perl/$i.pm &&
+ echo _$i > import/trunk/subversion/bindings/swig/perl/t/$i.t || return 1
+ done &&
+ echo "bad delete test" > \
+ import/trunk/subversion/bindings/swig/perl/t/larger-parent &&
+ echo "bad delete test 2" > \
+ import/trunk/subversion/bindings/swig/perl/another-larger &&
+ (
+ cd import &&
+ svn import -m "r9270 test" . "$svnrepo"/r9270
+ ) &&
+ svn_cmd co "$svnrepo"/r9270/trunk/subversion/bindings/swig/perl r9270 &&
+ (
+ cd r9270 &&
+ svn mkdir native &&
+ svn mv t native/t &&
+ for i in a b c
+ do
+ svn mv $i.pm native/$i.pm || return 1
+ done &&
+ echo z >>native/t/c.t &&
+ poke native/t/c.t &&
+ svn commit -m "reorg test"
+ ) &&
+ git svn init --minimize-url -i r9270-t \
+ "$svnrepo"/r9270/trunk/subversion/bindings/swig/perl/native/t &&
+ git svn fetch -i r9270-t &&
+ test $(git rev-list r9270-t | wc -l) -eq 2 &&
+ test "$(git ls-tree --name-only r9270-t~1)" = \
+ "$(git ls-tree --name-only r9270-t)"
+ '
+
+test_expect_success "track initial change if it was only made to parent" '
+ svn_cmd cp -m "wheee!" "$svnrepo"/r9270/trunk "$svnrepo"/r9270/drunk &&
+ git svn init --minimize-url -i r9270-d \
+ "$svnrepo"/r9270/drunk/subversion/bindings/swig/perl/native/t &&
+ git svn fetch -i r9270-d &&
+ test $(git rev-list r9270-d | wc -l) -eq 3 &&
+ test "$(git ls-tree --name-only r9270-t)" = \
+ "$(git ls-tree --name-only r9270-d)" &&
+ test "$(git rev-parse r9270-t)" = \
+ "$(git rev-parse r9270-d~1)"
+ '
+
+test_expect_success "follow-parent is atomic" '
+ record_size=$(($(test_oid rawsz) + 4)) &&
+ (
+ cd wc &&
+ svn_cmd up &&
+ svn_cmd mkdir stunk &&
+ echo "trunk stunk" > stunk/readme &&
+ svn_cmd add stunk/readme &&
+ svn_cmd ci -m "trunk stunk" &&
+ echo "stunk like junk" >> stunk/readme &&
+ svn_cmd ci -m "really stunk" &&
+ echo "stink stank stunk" >> stunk/readme &&
+ svn_cmd ci -m "even the grinch agrees"
+ ) &&
+ svn_cmd copy -m "stunk flunked" "$svnrepo"/stunk "$svnrepo"/flunk &&
+ { svn cp -m "early stunk flunked too" \
+ "$svnrepo"/stunk@17 "$svnrepo"/flunked ||
+ svn_cmd cp -m "early stunk flunked too" \
+ -r17 "$svnrepo"/stunk "$svnrepo"/flunked; } &&
+ git svn init --minimize-url -i stunk "$svnrepo"/stunk &&
+ git svn fetch -i stunk &&
+ git update-ref refs/remotes/flunk@18 refs/remotes/stunk~2 &&
+ git update-ref -d refs/remotes/stunk &&
+ git config --unset svn-remote.svn.fetch stunk &&
+ mkdir -p "$GIT_DIR"/svn/refs/remotes/flunk@18 &&
+ rev_map=$(cd "$GIT_DIR"/svn/refs/remotes/stunk && ls .rev_map*) &&
+ dd if="$GIT_DIR"/svn/refs/remotes/stunk/$rev_map \
+ of="$GIT_DIR"/svn/refs/remotes/flunk@18/$rev_map bs=$record_size count=1 &&
+ rm -rf "$GIT_DIR"/svn/refs/remotes/stunk &&
+ git svn init --minimize-url -i flunk "$svnrepo"/flunk &&
+ git svn fetch -i flunk &&
+ git svn init --minimize-url -i stunk "$svnrepo"/stunk &&
+ git svn fetch -i stunk &&
+ git svn init --minimize-url -i flunked "$svnrepo"/flunked &&
+ git svn fetch -i flunked &&
+ test "$(git rev-parse --verify refs/remotes/flunk@18)" \
+ = "$(git rev-parse --verify refs/remotes/stunk)" &&
+ test "$(git rev-parse --verify refs/remotes/flunk~1)" \
+ = "$(git rev-parse --verify refs/remotes/stunk)" &&
+ test "$(git rev-parse --verify refs/remotes/flunked~1)" \
+ = "$(git rev-parse --verify refs/remotes/stunk~1)"
+ '
+
+test_expect_success "track multi-parent paths" '
+ svn_cmd cp -m "resurrect /glob" "$svnrepo"/r9270 "$svnrepo"/glob &&
+ git svn multi-fetch &&
+ git cat-file commit refs/remotes/glob >actual &&
+ grep "^parent " actual >actual2 &&
+ test_line_count = 2 actual2
+ '
+
+test_expect_success "multi-fetch continues to work" "
+ git svn multi-fetch
+ "
+
+test_expect_success "multi-fetch works off a 'clean' repository" '
+ rm -rf "$GIT_DIR/svn" &&
+ git for-each-ref --format="option no-deref%0adelete %(refname)" refs/remotes |
+ git update-ref --stdin &&
+ git reflog expire --all --expire=all &&
+ mkdir "$GIT_DIR/svn" &&
+ git svn multi-fetch
+ '
+
+test_debug 'gitk --all &'
+
+test_done
diff --git a/t/t9105-git-svn-commit-diff.sh b/t/t9105-git-svn-commit-diff.sh
new file mode 100755
index 0000000..6ed5f74
--- /dev/null
+++ b/t/t9105-git-svn-commit-diff.sh
@@ -0,0 +1,44 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Eric Wong
+test_description='git svn commit-diff'
+. ./lib-git-svn.sh
+
+test_expect_success 'initialize repo' '
+ mkdir import &&
+ (
+ cd import &&
+ echo hello >readme &&
+ svn_cmd import -m "initial" . "$svnrepo"
+ ) &&
+ echo hello > readme &&
+ git update-index --add readme &&
+ git commit -a -m "initial" &&
+ echo world >> readme &&
+ git commit -a -m "another"
+ '
+
+head=$(git rev-parse --verify HEAD^0)
+prev=$(git rev-parse --verify HEAD^1)
+
+# the internals of the commit-diff command are the same as the regular
+# commit, so only a basic test of functionality is needed since we've
+# already tested commit extensively elsewhere
+
+test_expect_success 'test the commit-diff command' '
+ test -n "$prev" && test -n "$head" &&
+ git svn commit-diff -r1 "$prev" "$head" "$svnrepo" &&
+ svn_cmd co "$svnrepo" wc &&
+ cmp readme wc/readme
+ '
+
+test_expect_success 'commit-diff to a sub-directory (with git svn config)' '
+ svn_cmd import -m "sub-directory" import "$svnrepo"/subdir &&
+ git svn init --minimize-url "$svnrepo"/subdir &&
+ git svn fetch &&
+ git svn commit-diff -r3 "$prev" "$head" &&
+ svn_cmd cat "$svnrepo"/subdir/readme > readme.2 &&
+ cmp readme readme.2
+ '
+
+test_done
diff --git a/t/t9106-git-svn-commit-diff-clobber.sh b/t/t9106-git-svn-commit-diff-clobber.sh
new file mode 100755
index 0000000..bca496c
--- /dev/null
+++ b/t/t9106-git-svn-commit-diff-clobber.sh
@@ -0,0 +1,107 @@
+#!/bin/sh
+#
+# Copyright (c) 2006 Eric Wong
+test_description='git svn commit-diff clobber'
+
+. ./lib-git-svn.sh
+
+test_expect_success 'initialize repo' '
+ mkdir import &&
+ (
+ cd import &&
+ echo initial >file &&
+ svn_cmd import -m "initial" . "$svnrepo"
+ ) &&
+ echo initial > file &&
+ git update-index --add file &&
+ git commit -a -m "initial"
+ '
+test_expect_success 'commit change from svn side' '
+ svn_cmd co "$svnrepo" t.svn &&
+ (
+ cd t.svn &&
+ echo second line from svn >>file &&
+ poke file &&
+ svn_cmd commit -m "second line from svn"
+ ) &&
+ rm -rf t.svn
+ '
+
+test_expect_success 'commit conflicting change from git' '
+ echo second line from git >> file &&
+ git commit -a -m "second line from git" &&
+ test_must_fail git svn commit-diff -r1 HEAD~1 HEAD "$svnrepo"
+'
+
+test_expect_success 'commit complementing change from git' '
+ git reset --hard HEAD~1 &&
+ echo second line from svn >> file &&
+ git commit -a -m "second line from svn" &&
+ echo third line from git >> file &&
+ git commit -a -m "third line from git" &&
+ git svn commit-diff -r2 HEAD~1 HEAD "$svnrepo"
+ '
+
+test_expect_success 'dcommit fails to commit because of conflict' '
+ git svn init "$svnrepo" &&
+ git svn fetch &&
+ git reset --hard refs/remotes/git-svn &&
+ svn_cmd co "$svnrepo" t.svn &&
+ (
+ cd t.svn &&
+ echo fourth line from svn >>file &&
+ poke file &&
+ svn_cmd commit -m "fourth line from svn"
+ ) &&
+ rm -rf t.svn &&
+ echo "fourth line from git" >> file &&
+ git commit -a -m "fourth line from git" &&
+ test_must_fail git svn dcommit
+ '
+
+test_expect_success 'dcommit does the svn equivalent of an index merge' "
+ git reset --hard refs/remotes/git-svn &&
+ echo 'index merge' > file2 &&
+ git update-index --add file2 &&
+ git commit -a -m 'index merge' &&
+ echo 'more changes' >> file2 &&
+ git update-index file2 &&
+ git commit -a -m 'more changes' &&
+ git svn dcommit
+ "
+
+test_expect_success 'commit another change from svn side' '
+ svn_cmd co "$svnrepo" t.svn &&
+ (
+ cd t.svn &&
+ echo third line from svn >>file &&
+ poke file &&
+ svn_cmd commit -m "third line from svn"
+ ) &&
+ rm -rf t.svn
+ '
+
+test_expect_success 'multiple dcommit from git svn will not clobber svn' "
+ git reset --hard refs/remotes/git-svn &&
+ echo new file >> new-file &&
+ git update-index --add new-file &&
+ git commit -a -m 'new file' &&
+ echo clobber > file &&
+ git commit -a -m 'clobber' &&
+ test_must_fail git svn dcommit
+ "
+
+
+test_expect_success 'check that rebase really failed' '
+ git status >output &&
+ grep currently.rebasing output
+'
+
+test_expect_success 'resolve, continue the rebase and dcommit' "
+ echo clobber and I really mean it > file &&
+ git update-index file &&
+ git rebase --continue &&
+ git svn dcommit
+ "
+
+test_done
diff --git a/t/t9107-git-svn-migrate.sh b/t/t9107-git-svn-migrate.sh
new file mode 100755
index 0000000..aa908bb
--- /dev/null
+++ b/t/t9107-git-svn-migrate.sh
@@ -0,0 +1,136 @@
+#!/bin/sh
+# Copyright (c) 2006 Eric Wong
+test_description='git svn metadata migrations from previous versions'
+. ./lib-git-svn.sh
+
+test_expect_success 'setup old-looking metadata' '
+ cp "$GIT_DIR"/config "$GIT_DIR"/config-old-git-svn &&
+ mkdir import &&
+ (
+ cd import &&
+ for i in trunk branches/a branches/b tags/0.1 tags/0.2 tags/0.3
+ do
+ mkdir -p $i &&
+ echo hello >>$i/README ||
+ exit 1
+ done &&
+ svn_cmd import -m test . "$svnrepo"
+ ) &&
+ git svn init "$svnrepo" &&
+ git svn fetch &&
+ rm -rf "$GIT_DIR"/svn &&
+ git update-ref refs/heads/git-svn-HEAD refs/remotes/git-svn &&
+ git update-ref refs/heads/svn-HEAD refs/remotes/git-svn &&
+ git update-ref -d refs/remotes/git-svn refs/remotes/git-svn
+ '
+
+test_expect_success 'git-svn-HEAD is a real HEAD' '
+ git rev-parse --verify refs/heads/git-svn-HEAD^0
+'
+
+svnrepo_escaped=$(echo $svnrepo | sed 's/ /%20/g')
+
+test_expect_success 'initialize old-style (v0) git svn layout' '
+ mkdir -p "$GIT_DIR"/git-svn/info "$GIT_DIR"/svn/info &&
+ echo "$svnrepo" > "$GIT_DIR"/git-svn/info/url &&
+ echo "$svnrepo" > "$GIT_DIR"/svn/info/url &&
+ git svn migrate &&
+ ! test -d "$GIT_DIR"/git-svn &&
+ git rev-parse --verify refs/remotes/git-svn^0 &&
+ git rev-parse --verify refs/remotes/svn^0 &&
+ test "$(git config --get svn-remote.svn.url)" = "$svnrepo_escaped" &&
+ test $(git config --get svn-remote.svn.fetch) = \
+ ":refs/remotes/git-svn"
+ '
+
+test_expect_success 'initialize a multi-repository repo' '
+ git svn init "$svnrepo" -T trunk -t tags -b branches &&
+ git config --get-all svn-remote.svn.fetch > fetch.out &&
+ grep "^trunk:refs/remotes/origin/trunk$" fetch.out &&
+ test -n "$(git config --get svn-remote.svn.branches \
+ "^branches/\*:refs/remotes/origin/\*$")" &&
+ test -n "$(git config --get svn-remote.svn.tags \
+ "^tags/\*:refs/remotes/origin/tags/\*$")" &&
+ git config --unset svn-remote.svn.branches \
+ "^branches/\*:refs/remotes/origin/\*$" &&
+ git config --unset svn-remote.svn.tags \
+ "^tags/\*:refs/remotes/origin/tags/\*$" &&
+ git config --add svn-remote.svn.fetch "branches/a:refs/remotes/origin/a" &&
+ git config --add svn-remote.svn.fetch "branches/b:refs/remotes/origin/b" &&
+ for i in tags/0.1 tags/0.2 tags/0.3
+ do
+ git config --add svn-remote.svn.fetch \
+ $i:refs/remotes/origin/$i || return 1
+ done &&
+ git config --get-all svn-remote.svn.fetch > fetch.out &&
+ grep "^trunk:refs/remotes/origin/trunk$" fetch.out &&
+ grep "^branches/a:refs/remotes/origin/a$" fetch.out &&
+ grep "^branches/b:refs/remotes/origin/b$" fetch.out &&
+ grep "^tags/0\.1:refs/remotes/origin/tags/0\.1$" fetch.out &&
+ grep "^tags/0\.2:refs/remotes/origin/tags/0\.2$" fetch.out &&
+ grep "^tags/0\.3:refs/remotes/origin/tags/0\.3$" fetch.out &&
+ grep "^:refs/remotes/git-svn" fetch.out
+ '
+
+# refs should all be different, but the trees should all be the same:
+test_expect_success 'multi-fetch works on partial urls + paths' '
+ refs="trunk a b tags/0.1 tags/0.2 tags/0.3" &&
+ git svn multi-fetch &&
+ for i in $refs
+ do
+ git rev-parse --verify refs/remotes/origin/$i^0 || return 1;
+ done >refs.out &&
+ test -z "$(sort <refs.out | uniq -d)" &&
+ for i in $refs
+ do
+ for j in $refs
+ do
+ git diff --exit-code refs/remotes/origin/$i \
+ refs/remotes/origin/$j ||
+ return 1
+ done
+ done
+'
+
+test_expect_success 'migrate --minimize on old inited layout' '
+ git config --unset-all svn-remote.svn.fetch &&
+ git config --unset-all svn-remote.svn.url &&
+ rm -rf "$GIT_DIR"/svn &&
+ for i in $(cat fetch.out)
+ do
+ path=${i%%:*} &&
+ ref=${i#*:} &&
+ if test "$ref" = "${ref#refs/remotes/}"; then continue; fi &&
+ if test -n "$path"; then path="/$path"; fi &&
+ mkdir -p "$GIT_DIR"/svn/$ref/info/ &&
+ echo "$svnrepo"$path >"$GIT_DIR"/svn/$ref/info/url ||
+ return 1
+ done &&
+ git svn migrate --minimize &&
+ test -z "$(git config -l | grep "^svn-remote\.git-svn\.")" &&
+ git config --get-all svn-remote.svn.fetch > fetch.out &&
+ grep "^trunk:refs/remotes/origin/trunk$" fetch.out &&
+ grep "^branches/a:refs/remotes/origin/a$" fetch.out &&
+ grep "^branches/b:refs/remotes/origin/b$" fetch.out &&
+ grep "^tags/0\.1:refs/remotes/origin/tags/0\.1$" fetch.out &&
+ grep "^tags/0\.2:refs/remotes/origin/tags/0\.2$" fetch.out &&
+ grep "^tags/0\.3:refs/remotes/origin/tags/0\.3$" fetch.out &&
+ grep "^:refs/remotes/git-svn" fetch.out
+ '
+
+test_expect_success ".rev_db auto-converted to .rev_map.UUID" '
+ git svn fetch -i trunk &&
+ test -z "$(ls "$GIT_DIR"/svn/refs/remotes/origin/trunk/.rev_db.* 2>/dev/null)" &&
+ expect="$(ls "$GIT_DIR"/svn/refs/remotes/origin/trunk/.rev_map.*)" &&
+ test -n "$expect" &&
+ rev_db="$(echo $expect | sed -e "s,_map,_db,")" &&
+ convert_to_rev_db "$expect" "$rev_db" &&
+ rm -f "$expect" &&
+ test -f "$rev_db" &&
+ git svn fetch -i trunk &&
+ test -z "$(ls "$GIT_DIR"/svn/refs/remotes/origin/trunk/.rev_db.* 2>/dev/null)" &&
+ test ! -e "$GIT_DIR"/svn/refs/remotes/origin/trunk/.rev_db &&
+ test -f "$expect"
+ '
+
+test_done
diff --git a/t/t9108-git-svn-glob.sh b/t/t9108-git-svn-glob.sh
new file mode 100755
index 0000000..d5939d4
--- /dev/null
+++ b/t/t9108-git-svn-glob.sh
@@ -0,0 +1,116 @@
+#!/bin/sh
+# Copyright (c) 2007 Eric Wong
+test_description='git svn globbing refspecs'
+. ./lib-git-svn.sh
+
+cat > expect.end <<EOF
+the end
+hi
+start a new branch
+initial
+EOF
+
+test_expect_success 'test refspec globbing' '
+ mkdir -p trunk/src/a trunk/src/b trunk/doc &&
+ echo "hello world" > trunk/src/a/readme &&
+ echo "goodbye world" > trunk/src/b/readme &&
+ svn_cmd import -m "initial" trunk "$svnrepo"/trunk &&
+ svn_cmd co "$svnrepo" tmp &&
+ (
+ cd tmp &&
+ mkdir branches tags &&
+ svn_cmd add branches tags &&
+ svn_cmd cp trunk branches/start &&
+ svn_cmd commit -m "start a new branch" &&
+ svn_cmd up &&
+ echo "hi" >> branches/start/src/b/readme &&
+ poke branches/start/src/b/readme &&
+ echo "hey" >> branches/start/src/a/readme &&
+ poke branches/start/src/a/readme &&
+ svn_cmd commit -m "hi" &&
+ svn_cmd up &&
+ svn_cmd cp branches/start tags/end &&
+ echo "bye" >> tags/end/src/b/readme &&
+ poke tags/end/src/b/readme &&
+ echo "aye" >> tags/end/src/a/readme &&
+ poke tags/end/src/a/readme &&
+ svn_cmd commit -m "the end" &&
+ echo "byebye" >> tags/end/src/b/readme &&
+ poke tags/end/src/b/readme &&
+ svn_cmd commit -m "nothing to see here"
+ ) &&
+ git config --add svn-remote.svn.url "$svnrepo" &&
+ git config --add svn-remote.svn.fetch \
+ "trunk/src/a:refs/remotes/trunk" &&
+ git config --add svn-remote.svn.branches \
+ "branches/*/src/a:refs/remotes/branches/*" &&
+ git config --add svn-remote.svn.tags\
+ "tags/*/src/a:refs/remotes/tags/*" &&
+ git svn multi-fetch &&
+ git log --pretty=oneline refs/remotes/tags/end >actual &&
+ cut -d" " -f2- actual >output.end &&
+ test_cmp expect.end output.end &&
+ test "$(git rev-parse refs/remotes/tags/end~1)" = \
+ "$(git rev-parse refs/remotes/branches/start)" &&
+ test "$(git rev-parse refs/remotes/branches/start~2)" = \
+ "$(git rev-parse refs/remotes/trunk)" &&
+ test_must_fail git rev-parse refs/remotes/tags/end@3
+ '
+
+echo try to try > expect.two
+echo nothing to see here >> expect.two
+cat expect.end >> expect.two
+
+test_expect_success 'test left-hand-side only globbing' '
+ git config --add svn-remote.two.url "$svnrepo" &&
+ git config --add svn-remote.two.fetch trunk:refs/remotes/two/trunk &&
+ git config --add svn-remote.two.branches \
+ "branches/*:refs/remotes/two/branches/*" &&
+ git config --add svn-remote.two.tags \
+ "tags/*:refs/remotes/two/tags/*" &&
+ (
+ cd tmp &&
+ echo "try try" >> tags/end/src/b/readme &&
+ poke tags/end/src/b/readme &&
+ svn_cmd commit -m "try to try"
+ ) &&
+ git svn fetch two &&
+ git rev-list refs/remotes/two/tags/end >actual &&
+ test_line_count = 6 actual &&
+ git rev-list refs/remotes/two/branches/start >actual &&
+ test_line_count = 3 actual &&
+ test $(git rev-parse refs/remotes/two/branches/start~2) = \
+ $(git rev-parse refs/remotes/two/trunk) &&
+ test $(git rev-parse refs/remotes/two/tags/end~3) = \
+ $(git rev-parse refs/remotes/two/branches/start) &&
+ git log --pretty=oneline refs/remotes/two/tags/end >actual &&
+ cut -d" " -f2- actual >output.two &&
+ test_cmp expect.two output.two
+ '
+
+test_expect_success 'prepare test disallow multi-globs' "
+cat >expect.three <<EOF
+Only one set of wildcards (e.g. '*' or '*/*/*') is supported: branches/*/t/*
+
+EOF
+ "
+
+test_expect_success 'test disallow multi-globs' '
+ git config --add svn-remote.three.url "$svnrepo" &&
+ git config --add svn-remote.three.fetch \
+ trunk:refs/remotes/three/trunk &&
+ git config --add svn-remote.three.branches \
+ "branches/*/t/*:refs/remotes/three/branches/*" &&
+ git config --add svn-remote.three.tags \
+ "tags/*/*:refs/remotes/three/tags/*" &&
+ (
+ cd tmp &&
+ echo "try try" >> tags/end/src/b/readme &&
+ poke tags/end/src/b/readme &&
+ svn_cmd commit -m "try to try"
+ ) &&
+ test_must_fail git svn fetch three 2> stderr.three &&
+ test_cmp expect.three stderr.three
+ '
+
+test_done
diff --git a/t/t9109-git-svn-multi-glob.sh b/t/t9109-git-svn-multi-glob.sh
new file mode 100755
index 0000000..648dcee
--- /dev/null
+++ b/t/t9109-git-svn-multi-glob.sh
@@ -0,0 +1,167 @@
+#!/bin/sh
+# Copyright (c) 2007 Eric Wong
+test_description='git svn globbing refspecs'
+. ./lib-git-svn.sh
+
+cat > expect.end <<EOF
+the end
+hi
+start a new branch
+initial
+EOF
+
+test_expect_success 'test refspec globbing' '
+ mkdir -p trunk/src/a trunk/src/b trunk/doc &&
+ echo "hello world" > trunk/src/a/readme &&
+ echo "goodbye world" > trunk/src/b/readme &&
+ svn_cmd import -m "initial" trunk "$svnrepo"/trunk &&
+ svn_cmd co "$svnrepo" tmp &&
+ (
+ cd tmp &&
+ mkdir branches branches/v1 tags &&
+ svn_cmd add branches tags &&
+ svn_cmd cp trunk branches/v1/start &&
+ svn_cmd commit -m "start a new branch" &&
+ svn_cmd up &&
+ echo "hi" >> branches/v1/start/src/b/readme &&
+ poke branches/v1/start/src/b/readme &&
+ echo "hey" >> branches/v1/start/src/a/readme &&
+ poke branches/v1/start/src/a/readme &&
+ svn_cmd commit -m "hi" &&
+ svn_cmd up &&
+ svn_cmd cp branches/v1/start tags/end &&
+ echo "bye" >> tags/end/src/b/readme &&
+ poke tags/end/src/b/readme &&
+ echo "aye" >> tags/end/src/a/readme &&
+ poke tags/end/src/a/readme &&
+ svn_cmd commit -m "the end" &&
+ echo "byebye" >> tags/end/src/b/readme &&
+ poke tags/end/src/b/readme &&
+ svn_cmd commit -m "nothing to see here"
+ ) &&
+ git config --add svn-remote.svn.url "$svnrepo" &&
+ git config --add svn-remote.svn.fetch \
+ "trunk/src/a:refs/remotes/trunk" &&
+ git config --add svn-remote.svn.branches \
+ "branches/*/*/src/a:refs/remotes/branches/*/*" &&
+ git config --add svn-remote.svn.tags\
+ "tags/*/src/a:refs/remotes/tags/*" &&
+ git svn multi-fetch &&
+ git log --pretty=oneline refs/remotes/tags/end >actual &&
+ cut -d" " -f2- actual >output.end &&
+ test_cmp expect.end output.end &&
+ test "$(git rev-parse refs/remotes/tags/end~1)" = \
+ "$(git rev-parse refs/remotes/branches/v1/start)" &&
+ test "$(git rev-parse refs/remotes/branches/v1/start~2)" = \
+ "$(git rev-parse refs/remotes/trunk)" &&
+ test_must_fail git rev-parse refs/remotes/tags/end@3
+ '
+
+echo try to try > expect.two
+echo nothing to see here >> expect.two
+cat expect.end >> expect.two
+
+test_expect_success 'test left-hand-side only globbing' '
+ git config --add svn-remote.two.url "$svnrepo" &&
+ git config --add svn-remote.two.fetch trunk:refs/remotes/two/trunk &&
+ git config --add svn-remote.two.branches \
+ "branches/*/*:refs/remotes/two/branches/*/*" &&
+ git config --add svn-remote.two.tags \
+ "tags/*:refs/remotes/two/tags/*" &&
+ (
+ cd tmp &&
+ echo "try try" >> tags/end/src/b/readme &&
+ poke tags/end/src/b/readme &&
+ svn_cmd commit -m "try to try"
+ ) &&
+ git svn fetch two &&
+ git rev-list refs/remotes/two/tags/end >actual &&
+ test_line_count = 6 actual &&
+ git rev-list refs/remotes/two/branches/v1/start >actual &&
+ test_line_count = 3 actual &&
+ test $(git rev-parse refs/remotes/two/branches/v1/start~2) = \
+ $(git rev-parse refs/remotes/two/trunk) &&
+ test $(git rev-parse refs/remotes/two/tags/end~3) = \
+ $(git rev-parse refs/remotes/two/branches/v1/start) &&
+ git log --pretty=oneline refs/remotes/two/tags/end >actual &&
+ cut -d" " -f2- actual >output.two &&
+ test_cmp expect.two output.two
+ '
+cat > expect.four <<EOF
+adios
+adding more
+Changed 2 in v2/start
+Another versioned branch
+initial
+EOF
+
+test_expect_success 'test another branch' '
+ (
+ cd tmp &&
+ mkdir branches/v2 &&
+ svn_cmd add branches/v2 &&
+ svn_cmd cp trunk branches/v2/start &&
+ svn_cmd commit -m "Another versioned branch" &&
+ svn_cmd up &&
+ echo "hello" >> branches/v2/start/src/b/readme &&
+ poke branches/v2/start/src/b/readme &&
+ echo "howdy" >> branches/v2/start/src/a/readme &&
+ poke branches/v2/start/src/a/readme &&
+ svn_cmd commit -m "Changed 2 in v2/start" &&
+ svn_cmd up &&
+ svn_cmd cp branches/v2/start tags/next &&
+ echo "bye" >> tags/next/src/b/readme &&
+ poke tags/next/src/b/readme &&
+ echo "aye" >> tags/next/src/a/readme &&
+ poke tags/next/src/a/readme &&
+ svn_cmd commit -m "adding more" &&
+ echo "byebye" >> tags/next/src/b/readme &&
+ poke tags/next/src/b/readme &&
+ svn_cmd commit -m "adios"
+ ) &&
+ git config --add svn-remote.four.url "$svnrepo" &&
+ git config --add svn-remote.four.fetch trunk:refs/remotes/four/trunk &&
+ git config --add svn-remote.four.branches \
+ "branches/*/*:refs/remotes/four/branches/*/*" &&
+ git config --add svn-remote.four.tags \
+ "tags/*:refs/remotes/four/tags/*" &&
+ git svn fetch four &&
+ git rev-list refs/remotes/four/tags/next >actual &&
+ test_line_count = 5 actual &&
+ git rev-list refs/remotes/four/branches/v2/start >actual &&
+ test_line_count = 3 actual &&
+ test $(git rev-parse refs/remotes/four/branches/v2/start~2) = \
+ $(git rev-parse refs/remotes/four/trunk) &&
+ test $(git rev-parse refs/remotes/four/tags/next~2) = \
+ $(git rev-parse refs/remotes/four/branches/v2/start) &&
+ git log --pretty=oneline refs/remotes/four/tags/next >actual &&
+ cut -d" " -f2- actual >output.four &&
+ test_cmp expect.four output.four
+ '
+
+test_expect_success 'prepare test disallow multiple globs' "
+cat >expect.three <<EOF
+Only one set of wildcards (e.g. '*' or '*/*/*') is supported: branches/*/t/*
+
+EOF
+ "
+
+test_expect_success 'test disallow multiple globs' '
+ git config --add svn-remote.three.url "$svnrepo" &&
+ git config --add svn-remote.three.fetch \
+ trunk:refs/remotes/three/trunk &&
+ git config --add svn-remote.three.branches \
+ "branches/*/t/*:refs/remotes/three/branches/*/*" &&
+ git config --add svn-remote.three.tags \
+ "tags/*:refs/remotes/three/tags/*" &&
+ (
+ cd tmp &&
+ echo "try try" >> tags/end/src/b/readme &&
+ poke tags/end/src/b/readme &&
+ svn_cmd commit -m "try to try"
+ ) &&
+ test_must_fail git svn fetch three 2> stderr.three &&
+ test_cmp expect.three stderr.three
+ '
+
+test_done
diff --git a/t/t9110-git-svn-use-svm-props.sh b/t/t9110-git-svn-use-svm-props.sh
new file mode 100755
index 0000000..ad37d98
--- /dev/null
+++ b/t/t9110-git-svn-use-svm-props.sh
@@ -0,0 +1,61 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Eric Wong
+#
+
+test_description='git svn useSvmProps test'
+
+. ./lib-git-svn.sh
+
+test_expect_success 'load svm repo' '
+ svnadmin load -q "$rawsvnrepo" < "$TEST_DIRECTORY"/t9110/svm.dump &&
+ git svn init --minimize-url -R arr -i bar "$svnrepo"/mirror/arr &&
+ git svn init --minimize-url -R argh -i dir "$svnrepo"/mirror/argh &&
+ git svn init --minimize-url -R argh -i e \
+ "$svnrepo"/mirror/argh/a/b/c/d/e &&
+ git config svn.useSvmProps true &&
+ git svn fetch --all
+ '
+
+uuid=161ce429-a9dd-4828-af4a-52023f968c89
+
+bar_url=http://mayonaise/svnrepo/bar
+test_expect_success 'verify metadata for /bar' "
+ git cat-file commit refs/remotes/bar >actual &&
+ grep '^git-svn-id: $bar_url@12 $uuid$' actual &&
+ git cat-file commit refs/remotes/bar~1 >actual &&
+ grep '^git-svn-id: $bar_url@11 $uuid$' actual &&
+ git cat-file commit refs/remotes/bar~2 >actual &&
+ grep '^git-svn-id: $bar_url@10 $uuid$' actual &&
+ git cat-file commit refs/remotes/bar~3 >actual &&
+ grep '^git-svn-id: $bar_url@9 $uuid$' actual &&
+ git cat-file commit refs/remotes/bar~4 >actual &&
+ grep '^git-svn-id: $bar_url@6 $uuid$' actual &&
+ git cat-file commit refs/remotes/bar~5 >actual &&
+ grep '^git-svn-id: $bar_url@1 $uuid$' actual
+ "
+
+e_url=http://mayonaise/svnrepo/dir/a/b/c/d/e
+test_expect_success 'verify metadata for /dir/a/b/c/d/e' "
+ git cat-file commit refs/remotes/e >actual &&
+ grep '^git-svn-id: $e_url@1 $uuid$' actual
+ "
+
+dir_url=http://mayonaise/svnrepo/dir
+test_expect_success 'verify metadata for /dir' "
+ git cat-file commit refs/remotes/dir >actual &&
+ grep '^git-svn-id: $dir_url@2 $uuid$' actual &&
+ git cat-file commit refs/remotes/dir~1 >actual &&
+ grep '^git-svn-id: $dir_url@1 $uuid$' actual
+ "
+
+test_expect_success 'find commit based on SVN revision number' "
+ git svn find-rev r12 >actual &&
+ grep $(git rev-parse HEAD) actual
+ "
+
+test_expect_success 'empty rebase' "
+ git svn rebase
+ "
+
+test_done
diff --git a/t/t9110/svm.dump b/t/t9110/svm.dump
new file mode 100644
index 0000000..cc799c2
--- /dev/null
+++ b/t/t9110/svm.dump
@@ -0,0 +1,511 @@
+SVN-fs-dump-format-version: 2
+
+UUID: de5973c6-545d-41da-aded-c265f9039e74
+
+Revision-number: 0
+Prop-content-length: 56
+Content-length: 56
+
+K 8
+svn:date
+V 27
+2007-02-17T06:54:59.793104Z
+PROPS-END
+
+Revision-number: 1
+Prop-content-length: 200
+Content-length: 200
+
+K 7
+svn:log
+V 40
+SVM: initializing mirror for /mirror/arr
+K 10
+svn:author
+V 3
+svm
+K 11
+svm:headrev
+V 39
+161ce429-a9dd-4828-af4a-52023f968c89:0
+
+K 8
+svn:date
+V 27
+2007-02-17T06:55:00.121647Z
+PROPS-END
+
+Node-path:
+Node-kind: dir
+Node-action: change
+Prop-content-length: 44
+Content-length: 44
+
+K 10
+svm:mirror
+V 12
+/mirror/arr
+
+PROPS-END
+
+
+Node-path: mirror
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Node-path: mirror/arr
+Node-kind: dir
+Node-action: add
+Prop-content-length: 116
+Content-length: 116
+
+K 10
+svm:source
+V 29
+http://mayonaise/svnrepo!/bar
+K 8
+svm:uuid
+V 36
+161ce429-a9dd-4828-af4a-52023f968c89
+PROPS-END
+
+
+Revision-number: 2
+Prop-content-length: 182
+Content-length: 182
+
+K 7
+svn:log
+V 18
+import for git-svn
+K 10
+svn:author
+V 7
+svnsync
+K 11
+svm:headrev
+V 39
+161ce429-a9dd-4828-af4a-52023f968c89:1
+
+K 8
+svn:date
+V 27
+2007-02-17T05:10:52.108847Z
+PROPS-END
+
+Node-path: mirror/arr
+Node-kind: dir
+Node-action: change
+Prop-content-length: 116
+Content-length: 116
+
+K 10
+svm:source
+V 29
+http://mayonaise/svnrepo!/bar
+K 8
+svm:uuid
+V 36
+161ce429-a9dd-4828-af4a-52023f968c89
+PROPS-END
+
+
+Node-path: mirror/arr/zzz
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 4
+Text-content-md5: 33b02bc15ce9557d2dd8484d58f95ac4
+Content-length: 14
+
+PROPS-END
+zzz
+
+
+Revision-number: 3
+Prop-content-length: 230
+Content-length: 230
+
+K 7
+svn:log
+V 66
+new symlink is added to a file that was also just made executable
+
+K 10
+svn:author
+V 7
+svnsync
+K 11
+svm:headrev
+V 39
+161ce429-a9dd-4828-af4a-52023f968c89:6
+
+K 8
+svn:date
+V 27
+2007-02-17T05:11:01.686891Z
+PROPS-END
+
+Node-path: mirror/arr/zzz
+Node-kind: file
+Node-action: change
+Prop-content-length: 36
+Text-content-length: 4
+Text-content-md5: 33b02bc15ce9557d2dd8484d58f95ac4
+Content-length: 40
+
+K 14
+svn:executable
+V 1
+*
+PROPS-END
+zzz
+
+
+Revision-number: 4
+Prop-content-length: 192
+Content-length: 192
+
+K 7
+svn:log
+V 28
+/bar/d should be in the log
+
+K 10
+svn:author
+V 7
+svnsync
+K 11
+svm:headrev
+V 39
+161ce429-a9dd-4828-af4a-52023f968c89:9
+
+K 8
+svn:date
+V 27
+2007-02-17T05:11:07.686552Z
+PROPS-END
+
+Node-path: mirror/arr/d
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 4
+Text-content-md5: 0bee89b07a248e27c83fc3d5951213c1
+Content-length: 14
+
+PROPS-END
+abc
+
+
+Revision-number: 5
+Prop-content-length: 185
+Content-length: 185
+
+K 7
+svn:log
+V 20
+add a new directory
+
+K 10
+svn:author
+V 7
+svnsync
+K 11
+svm:headrev
+V 40
+161ce429-a9dd-4828-af4a-52023f968c89:10
+
+K 8
+svn:date
+V 27
+2007-02-17T05:11:08.405953Z
+PROPS-END
+
+Node-path: mirror/arr/newdir
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Node-path: mirror/arr/newdir/dir
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 4
+Text-content-md5: 9cd599a3523898e6a12e13ec787da50a
+Content-length: 14
+
+PROPS-END
+new
+
+
+Revision-number: 6
+Prop-content-length: 196
+Content-length: 196
+
+K 7
+svn:log
+V 31
+modify a file in new directory
+
+K 10
+svn:author
+V 7
+svnsync
+K 11
+svm:headrev
+V 40
+161ce429-a9dd-4828-af4a-52023f968c89:11
+
+K 8
+svn:date
+V 27
+2007-02-17T05:11:09.126645Z
+PROPS-END
+
+Node-path: mirror/arr/newdir/dir
+Node-kind: file
+Node-action: change
+Text-content-length: 8
+Text-content-md5: a950e20332358e523a5e9d571e47fa64
+Content-length: 8
+
+new
+foo
+
+
+Revision-number: 7
+Prop-content-length: 179
+Content-length: 179
+
+K 7
+svn:log
+V 14
+update /bar/d
+
+K 10
+svn:author
+V 7
+svnsync
+K 11
+svm:headrev
+V 40
+161ce429-a9dd-4828-af4a-52023f968c89:12
+
+K 8
+svn:date
+V 27
+2007-02-17T05:11:09.846221Z
+PROPS-END
+
+Node-path: mirror/arr/d
+Node-kind: file
+Node-action: change
+Text-content-length: 4
+Text-content-md5: 7abb78de7f2756ca8b511cbc879fd5e7
+Content-length: 4
+
+cba
+
+
+Revision-number: 8
+Prop-content-length: 201
+Content-length: 201
+
+K 7
+svn:log
+V 41
+SVM: initializing mirror for /mirror/argh
+K 10
+svn:author
+V 3
+svm
+K 11
+svm:headrev
+V 39
+161ce429-a9dd-4828-af4a-52023f968c89:0
+
+K 8
+svn:date
+V 27
+2007-02-17T06:56:03.703677Z
+PROPS-END
+
+Node-path:
+Node-kind: dir
+Node-action: change
+Prop-content-length: 57
+Content-length: 57
+
+K 10
+svm:mirror
+V 25
+/mirror/argh
+/mirror/arr
+
+PROPS-END
+
+
+Node-path: mirror/argh
+Node-kind: dir
+Node-action: add
+Prop-content-length: 116
+Content-length: 116
+
+K 10
+svm:source
+V 29
+http://mayonaise/svnrepo!/dir
+K 8
+svm:uuid
+V 36
+161ce429-a9dd-4828-af4a-52023f968c89
+PROPS-END
+
+
+Revision-number: 9
+Prop-content-length: 182
+Content-length: 182
+
+K 7
+svn:log
+V 18
+import for git-svn
+K 10
+svn:author
+V 7
+svnsync
+K 11
+svm:headrev
+V 39
+161ce429-a9dd-4828-af4a-52023f968c89:1
+
+K 8
+svn:date
+V 27
+2007-02-17T05:10:52.108847Z
+PROPS-END
+
+Node-path: mirror/argh
+Node-kind: dir
+Node-action: change
+Prop-content-length: 116
+Content-length: 116
+
+K 10
+svm:source
+V 29
+http://mayonaise/svnrepo!/dir
+K 8
+svm:uuid
+V 36
+161ce429-a9dd-4828-af4a-52023f968c89
+PROPS-END
+
+
+Node-path: mirror/argh/a
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Node-path: mirror/argh/a/b
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Node-path: mirror/argh/a/b/c
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Node-path: mirror/argh/a/b/c/d
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Node-path: mirror/argh/a/b/c/d/e
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Node-path: mirror/argh/a/b/c/d/e/file
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 9
+Text-content-md5: 3fd46fe46fcdcf062c802ca60dc826d5
+Content-length: 19
+
+PROPS-END
+deep dir
+
+
+Revision-number: 10
+Prop-content-length: 197
+Content-length: 197
+
+K 7
+svn:log
+V 33
+try a deep --rmdir with a commit
+
+K 10
+svn:author
+V 7
+svnsync
+K 11
+svm:headrev
+V 39
+161ce429-a9dd-4828-af4a-52023f968c89:2
+
+K 8
+svn:date
+V 27
+2007-02-17T05:10:54.847015Z
+PROPS-END
+
+Node-path: mirror/argh/file
+Node-kind: file
+Node-action: add
+Node-copyfrom-rev: 9
+Node-copyfrom-path: mirror/argh/a/b/c/d/e/file
+Text-content-length: 9
+Text-content-md5: 3fd46fe46fcdcf062c802ca60dc826d5
+Content-length: 9
+
+deep dir
+
+
+Node-path: mirror/argh/a
+Node-action: delete
+
+
diff --git a/t/t9111-git-svn-use-svnsync-props.sh b/t/t9111-git-svn-use-svnsync-props.sh
new file mode 100755
index 0000000..6c93073
--- /dev/null
+++ b/t/t9111-git-svn-use-svnsync-props.sh
@@ -0,0 +1,51 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Eric Wong
+#
+
+test_description='git svn useSvnsyncProps test'
+
+. ./lib-git-svn.sh
+
+test_expect_success 'load svnsync repo' '
+ svnadmin load -q "$rawsvnrepo" < "$TEST_DIRECTORY"/t9111/svnsync.dump &&
+ git svn init --minimize-url -R arr -i bar "$svnrepo"/bar &&
+ git svn init --minimize-url -R argh -i dir "$svnrepo"/dir &&
+ git svn init --minimize-url -R argh -i e "$svnrepo"/dir/a/b/c/d/e &&
+ git config svn.useSvnsyncProps true &&
+ git svn fetch --all
+ '
+
+uuid=161ce429-a9dd-4828-af4a-52023f968c89
+
+bar_url=http://mayonaise/svnrepo/bar
+test_expect_success 'verify metadata for /bar' "
+ git cat-file commit refs/remotes/bar >actual &&
+ grep '^git-svn-id: $bar_url@12 $uuid$' actual &&
+ git cat-file commit refs/remotes/bar~1 >actual &&
+ grep '^git-svn-id: $bar_url@11 $uuid$' actual &&
+ git cat-file commit refs/remotes/bar~2 >actual &&
+ grep '^git-svn-id: $bar_url@10 $uuid$' actual &&
+ git cat-file commit refs/remotes/bar~3 >actual &&
+ grep '^git-svn-id: $bar_url@9 $uuid$' actual &&
+ git cat-file commit refs/remotes/bar~4 >actual &&
+ grep '^git-svn-id: $bar_url@6 $uuid$' actual &&
+ git cat-file commit refs/remotes/bar~5 >actual &&
+ grep '^git-svn-id: $bar_url@1 $uuid$' actual
+ "
+
+e_url=http://mayonaise/svnrepo/dir/a/b/c/d/e
+test_expect_success 'verify metadata for /dir/a/b/c/d/e' "
+ git cat-file commit refs/remotes/e >actual &&
+ grep '^git-svn-id: $e_url@1 $uuid$' actual
+ "
+
+dir_url=http://mayonaise/svnrepo/dir
+test_expect_success 'verify metadata for /dir' "
+ git cat-file commit refs/remotes/dir >actual &&
+ grep '^git-svn-id: $dir_url@2 $uuid$' actual &&
+ git cat-file commit refs/remotes/dir~1 >actual &&
+ grep '^git-svn-id: $dir_url@1 $uuid$' actual
+ "
+
+test_done
diff --git a/t/t9111/svnsync.dump b/t/t9111/svnsync.dump
new file mode 100644
index 0000000..499fa95
--- /dev/null
+++ b/t/t9111/svnsync.dump
@@ -0,0 +1,560 @@
+SVN-fs-dump-format-version: 2
+
+UUID: b4bfe35e-f256-4096-874c-08c5639ecad7
+
+Revision-number: 0
+Prop-content-length: 240
+Content-length: 240
+
+K 18
+svn:sync-from-uuid
+V 36
+161ce429-a9dd-4828-af4a-52023f968c89
+K 10
+svn:author
+V 7
+svnsync
+K 24
+svn:sync-last-merged-rev
+V 2
+12
+K 8
+svn:date
+V 27
+2007-02-17T05:10:52.017552Z
+K 17
+svn:sync-from-url
+V 24
+http://mayonaise/svnrepo
+PROPS-END
+
+Revision-number: 1
+Prop-content-length: 120
+Content-length: 120
+
+K 7
+svn:log
+V 18
+import for git-svn
+K 10
+svn:author
+V 7
+svnsync
+K 8
+svn:date
+V 27
+2007-02-17T05:10:52.108847Z
+PROPS-END
+
+Node-path: bar
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Node-path: bar/zzz
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 4
+Text-content-md5: 33b02bc15ce9557d2dd8484d58f95ac4
+Content-length: 14
+
+PROPS-END
+zzz
+
+
+Node-path: dir
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Node-path: dir/a
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Node-path: dir/a/b
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Node-path: dir/a/b/c
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Node-path: dir/a/b/c/d
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Node-path: dir/a/b/c/d/e
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Node-path: dir/a/b/c/d/e/file
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 9
+Text-content-md5: 3fd46fe46fcdcf062c802ca60dc826d5
+Content-length: 19
+
+PROPS-END
+deep dir
+
+
+Node-path: exec.sh
+Node-kind: file
+Node-action: add
+Prop-content-length: 35
+Text-content-length: 10
+Text-content-md5: 3e2b31c72181b87149ff995e7202c0e3
+Content-length: 45
+
+K 14
+svn:executable
+V 0
+
+PROPS-END
+#!/bin/sh
+
+
+Node-path: foo
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 4
+Text-content-md5: d3b07384d113edec49eaa6238ad5ff00
+Content-length: 14
+
+PROPS-END
+foo
+
+
+Node-path: foo.link
+Node-kind: file
+Node-action: add
+Prop-content-length: 33
+Text-content-length: 8
+Text-content-md5: 1043146e49ef02cab12eef865cb34ff3
+Content-length: 41
+
+K 11
+svn:special
+V 1
+*
+PROPS-END
+link foo
+
+Revision-number: 2
+Prop-content-length: 135
+Content-length: 135
+
+K 7
+svn:log
+V 33
+try a deep --rmdir with a commit
+
+K 10
+svn:author
+V 7
+svnsync
+K 8
+svn:date
+V 27
+2007-02-17T05:10:54.847015Z
+PROPS-END
+
+Node-path: dir/file
+Node-kind: file
+Node-action: add
+Node-copyfrom-rev: 1
+Node-copyfrom-path: dir/a/b/c/d/e/file
+Text-content-length: 9
+Text-content-md5: 3fd46fe46fcdcf062c802ca60dc826d5
+Content-length: 9
+
+deep dir
+
+
+Node-path: dir/a
+Node-action: delete
+
+
+Node-path: file
+Node-kind: file
+Node-action: add
+Node-copyfrom-rev: 1
+Node-copyfrom-path: dir/a/b/c/d/e/file
+Text-content-length: 9
+Text-content-md5: 3fd46fe46fcdcf062c802ca60dc826d5
+Content-length: 9
+
+deep dir
+
+
+Revision-number: 3
+Prop-content-length: 136
+Content-length: 136
+
+K 7
+svn:log
+V 34
+remove executable bit from a file
+
+K 10
+svn:author
+V 7
+svnsync
+K 8
+svn:date
+V 27
+2007-02-17T05:10:58.232691Z
+PROPS-END
+
+Node-path: exec.sh
+Node-kind: file
+Node-action: change
+Prop-content-length: 10
+Text-content-length: 10
+Text-content-md5: 3e2b31c72181b87149ff995e7202c0e3
+Content-length: 20
+
+PROPS-END
+#!/bin/sh
+
+
+Revision-number: 4
+Prop-content-length: 131
+Content-length: 131
+
+K 7
+svn:log
+V 29
+add executable bit back file
+
+K 10
+svn:author
+V 7
+svnsync
+K 8
+svn:date
+V 27
+2007-02-17T05:10:59.666560Z
+PROPS-END
+
+Node-path: exec.sh
+Node-kind: file
+Node-action: change
+Prop-content-length: 36
+Text-content-length: 10
+Text-content-md5: 3e2b31c72181b87149ff995e7202c0e3
+Content-length: 46
+
+K 14
+svn:executable
+V 1
+*
+PROPS-END
+#!/bin/sh
+
+
+Revision-number: 5
+Prop-content-length: 154
+Content-length: 154
+
+K 7
+svn:log
+V 52
+executable file becomes a symlink to bar/zzz (file)
+
+K 10
+svn:author
+V 7
+svnsync
+K 8
+svn:date
+V 27
+2007-02-17T05:11:00.676495Z
+PROPS-END
+
+Node-path: exec.sh
+Node-kind: file
+Node-action: change
+Prop-content-length: 33
+Text-content-length: 12
+Text-content-md5: f138693371665cc117742508761d684d
+Content-length: 45
+
+K 11
+svn:special
+V 1
+*
+PROPS-END
+link bar/zzz
+
+Revision-number: 6
+Prop-content-length: 168
+Content-length: 168
+
+K 7
+svn:log
+V 66
+new symlink is added to a file that was also just made executable
+
+K 10
+svn:author
+V 7
+svnsync
+K 8
+svn:date
+V 27
+2007-02-17T05:11:01.686891Z
+PROPS-END
+
+Node-path: bar/zzz
+Node-kind: file
+Node-action: change
+Prop-content-length: 36
+Text-content-length: 4
+Text-content-md5: 33b02bc15ce9557d2dd8484d58f95ac4
+Content-length: 40
+
+K 14
+svn:executable
+V 1
+*
+PROPS-END
+zzz
+
+
+Node-path: exec-2.sh
+Node-kind: file
+Node-action: add
+Node-copyfrom-rev: 5
+Node-copyfrom-path: exec.sh
+Text-content-length: 12
+Text-content-md5: f138693371665cc117742508761d684d
+Content-length: 12
+
+link bar/zzz
+
+Revision-number: 7
+Prop-content-length: 136
+Content-length: 136
+
+K 7
+svn:log
+V 34
+modify a symlink to become a file
+
+K 10
+svn:author
+V 7
+svnsync
+K 8
+svn:date
+V 27
+2007-02-17T05:11:02.677035Z
+PROPS-END
+
+Node-path: exec-2.sh
+Node-kind: file
+Node-action: change
+Prop-content-length: 10
+Text-content-length: 9
+Text-content-md5: 8e92eff9e911886cede27d420f89c735
+Content-length: 19
+
+PROPS-END
+git help
+
+
+Revision-number: 8
+Prop-content-length: 109
+Content-length: 109
+
+K 7
+svn:log
+V 8
+éïâˆ
+
+K 10
+svn:author
+V 7
+svnsync
+K 8
+svn:date
+V 27
+2007-02-17T05:11:03.676862Z
+PROPS-END
+
+Node-path: exec-2.sh
+Node-kind: file
+Node-action: change
+Text-content-length: 17
+Text-content-md5: 49881954063cf26ca48c212396a957ca
+Content-length: 17
+
+git help
+# hello
+
+
+Revision-number: 9
+Prop-content-length: 130
+Content-length: 130
+
+K 7
+svn:log
+V 28
+/bar/d should be in the log
+
+K 10
+svn:author
+V 7
+svnsync
+K 8
+svn:date
+V 27
+2007-02-17T05:11:07.686552Z
+PROPS-END
+
+Node-path: bar/d
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 4
+Text-content-md5: 0bee89b07a248e27c83fc3d5951213c1
+Content-length: 14
+
+PROPS-END
+abc
+
+
+Revision-number: 10
+Prop-content-length: 122
+Content-length: 122
+
+K 7
+svn:log
+V 20
+add a new directory
+
+K 10
+svn:author
+V 7
+svnsync
+K 8
+svn:date
+V 27
+2007-02-17T05:11:08.405953Z
+PROPS-END
+
+Node-path: bar/newdir
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Node-path: bar/newdir/dir
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 4
+Text-content-md5: 9cd599a3523898e6a12e13ec787da50a
+Content-length: 14
+
+PROPS-END
+new
+
+
+Revision-number: 11
+Prop-content-length: 133
+Content-length: 133
+
+K 7
+svn:log
+V 31
+modify a file in new directory
+
+K 10
+svn:author
+V 7
+svnsync
+K 8
+svn:date
+V 27
+2007-02-17T05:11:09.126645Z
+PROPS-END
+
+Node-path: bar/newdir/dir
+Node-kind: file
+Node-action: change
+Text-content-length: 8
+Text-content-md5: a950e20332358e523a5e9d571e47fa64
+Content-length: 8
+
+new
+foo
+
+
+Revision-number: 12
+Prop-content-length: 116
+Content-length: 116
+
+K 7
+svn:log
+V 14
+update /bar/d
+
+K 10
+svn:author
+V 7
+svnsync
+K 8
+svn:date
+V 27
+2007-02-17T05:11:09.846221Z
+PROPS-END
+
+Node-path: bar/d
+Node-kind: file
+Node-action: change
+Text-content-length: 4
+Text-content-md5: 7abb78de7f2756ca8b511cbc879fd5e7
+Content-length: 4
+
+cba
diff --git a/t/t9112-git-svn-md5less-file.sh b/t/t9112-git-svn-md5less-file.sh
new file mode 100755
index 0000000..9861c71
--- /dev/null
+++ b/t/t9112-git-svn-md5less-file.sh
@@ -0,0 +1,47 @@
+#!/bin/sh
+
+test_description='test that git handles an svn repository with missing md5sums'
+
+. ./lib-git-svn.sh
+
+# Loading a node from a svn dumpfile without a Text-Content-Length
+# field causes svn to neglect to store or report an md5sum. (it will
+# calculate one if you had put Text-Content-Length: 0). This showed
+# up in a repository created with cvs2svn.
+
+cat > dumpfile.svn <<EOF
+SVN-fs-dump-format-version: 1
+
+Revision-number: 1
+Prop-content-length: 98
+Content-length: 98
+
+K 7
+svn:log
+V 0
+
+K 10
+svn:author
+V 4
+test
+K 8
+svn:date
+V 27
+2007-05-06T12:37:01.153339Z
+PROPS-END
+
+Node-path: md5less-file
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+EOF
+
+test_expect_success 'load svn dumpfile' 'svnadmin load "$rawsvnrepo" < dumpfile.svn'
+
+test_expect_success 'initialize git svn' 'git svn init "$svnrepo"'
+test_expect_success 'fetch revisions from svn' 'git svn fetch'
+test_done
diff --git a/t/t9113-git-svn-dcommit-new-file.sh b/t/t9113-git-svn-dcommit-new-file.sh
new file mode 100755
index 0000000..e8479ce
--- /dev/null
+++ b/t/t9113-git-svn-dcommit-new-file.sh
@@ -0,0 +1,35 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Eric Wong
+#
+
+# Don't run this test by default unless the user really wants it
+# I don't like the idea of taking a port and possibly leaving a
+# daemon running on a users system if the test fails.
+# Not all git users will need to interact with SVN.
+
+test_description='git svn dcommit new files over svn:// test'
+
+. ./lib-git-svn.sh
+
+require_svnserve
+
+test_expect_success 'start tracking an empty repo' '
+ svn_cmd mkdir -m "empty dir" "$svnrepo"/empty-dir &&
+ echo "[general]" > "$rawsvnrepo"/conf/svnserve.conf &&
+ echo anon-access = write >> "$rawsvnrepo"/conf/svnserve.conf &&
+ start_svnserve &&
+ git svn init svn://127.0.0.1:$SVNSERVE_PORT &&
+ git svn fetch
+ '
+
+test_expect_success 'create files in new directory with dcommit' "
+ mkdir git-new-dir &&
+ echo hello > git-new-dir/world &&
+ git update-index --add git-new-dir/world &&
+ git commit -m hello &&
+ start_svnserve &&
+ git svn dcommit
+ "
+
+test_done
diff --git a/t/t9114-git-svn-dcommit-merge.sh b/t/t9114-git-svn-dcommit-merge.sh
new file mode 100755
index 0000000..32317d6
--- /dev/null
+++ b/t/t9114-git-svn-dcommit-merge.sh
@@ -0,0 +1,95 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Eric Wong
+# Based on a script by Joakim Tjernlund <joakim.tjernlund@transmode.se>
+
+test_description='git svn dcommit handles merges'
+
+. ./lib-git-svn.sh
+
+big_text_block () {
+cat << EOF
+#
+# (C) Copyright 2000 - 2005
+# Wolfgang Denk, DENX Software Engineering, wd@denx.de.
+#
+# See file CREDITS for list of people who contributed to this
+# project.
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation; either version 2 of
+# the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, see <http://www.gnu.org/licenses/>.
+#
+EOF
+}
+
+test_expect_success 'setup svn repository' '
+ svn_cmd co "$svnrepo" mysvnwork &&
+ mkdir -p mysvnwork/trunk &&
+ (
+ cd mysvnwork &&
+ big_text_block >>trunk/README &&
+ svn_cmd add trunk &&
+ svn_cmd ci -m "first commit" trunk
+ )
+ '
+
+test_expect_success 'setup git mirror and merge' '
+ git svn init "$svnrepo" -t tags -T trunk -b branches &&
+ git svn fetch &&
+ git checkout -b svn remotes/origin/trunk &&
+ git checkout -b merge &&
+ echo new file > new_file &&
+ git add new_file &&
+ git commit -a -m "New file" &&
+ echo hello >> README &&
+ git commit -a -m "hello" &&
+ echo add some stuff >> new_file &&
+ git commit -a -m "add some stuff" &&
+ git checkout svn &&
+ mv -f README tmp &&
+ echo friend > README &&
+ cat tmp >> README &&
+ git commit -a -m "friend" &&
+ git merge merge
+ '
+
+test_debug 'gitk --all & sleep 1'
+
+test_expect_success 'verify pre-merge ancestry' "
+ test x\$(git rev-parse --verify refs/heads/svn^2) = \
+ x\$(git rev-parse --verify refs/heads/merge) &&
+ git cat-file commit refs/heads/svn^ >actual &&
+ grep '^friend$' actual
+ "
+
+test_expect_success 'git svn dcommit merges' "
+ git svn dcommit
+ "
+
+test_debug 'gitk --all & sleep 1'
+
+test_expect_success 'verify post-merge ancestry' "
+ test x\$(git rev-parse --verify refs/heads/svn) = \
+ x\$(git rev-parse --verify refs/remotes/origin/trunk) &&
+ test x\$(git rev-parse --verify refs/heads/svn^2) = \
+ x\$(git rev-parse --verify refs/heads/merge) &&
+ git cat-file commit refs/heads/svn^ >actual &&
+ grep '^friend$' actual
+ "
+
+test_expect_success 'verify merge commit message' "
+ git rev-list --pretty=raw -1 refs/heads/svn >actual &&
+ grep \" Merge branch 'merge' into svn\" actual
+ "
+
+test_done
diff --git a/t/t9115-git-svn-dcommit-funky-renames.sh b/t/t9115-git-svn-dcommit-funky-renames.sh
new file mode 100755
index 0000000..743fbe1
--- /dev/null
+++ b/t/t9115-git-svn-dcommit-funky-renames.sh
@@ -0,0 +1,123 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Eric Wong
+
+
+test_description='git svn dcommit can commit renames of files with ugly names'
+
+. ./lib-git-svn.sh
+
+test_expect_success 'load repository with strange names' '
+ svnadmin load -q "$rawsvnrepo" <"$TEST_DIRECTORY"/t9115/funky-names.dump
+'
+
+maybe_start_httpd gtk+
+
+test_expect_success 'init and fetch repository' '
+ git svn init "$svnrepo" &&
+ git svn fetch &&
+ git reset --hard git-svn
+ '
+
+test_expect_success 'create file in existing ugly and empty dir' '
+ mkdir -p "#{bad_directory_name}" &&
+ echo hi > "#{bad_directory_name}/ foo" &&
+ git update-index --add "#{bad_directory_name}/ foo" &&
+ git commit -m "new file in ugly parent" &&
+ git svn dcommit
+ '
+
+test_expect_success 'rename ugly file' '
+ git mv "#{bad_directory_name}/ foo" "file name with feces" &&
+ git commit -m "rename ugly file" &&
+ git svn dcommit
+ '
+
+test_expect_success 'rename pretty file' '
+ echo :x > pretty &&
+ git update-index --add pretty &&
+ git commit -m "pretty :x" &&
+ git svn dcommit &&
+ mkdir -p regular_dir_name &&
+ git mv pretty regular_dir_name/pretty &&
+ git commit -m "moved pretty file" &&
+ git svn dcommit
+ '
+
+test_expect_success 'rename pretty file into ugly one' '
+ git mv regular_dir_name/pretty "#{bad_directory_name}/ booboo" &&
+ git commit -m booboo &&
+ git svn dcommit
+ '
+
+test_expect_success 'add a file with plus signs' '
+ echo .. > +_+ &&
+ git update-index --add +_+ &&
+ git commit -m plus &&
+ mkdir gtk+ &&
+ git mv +_+ gtk+/_+_ &&
+ git commit -m plus_dir &&
+ git svn dcommit
+ '
+
+test_expect_success 'clone the repository to test rebase' '
+ git svn clone "$svnrepo" test-rebase &&
+ (
+ cd test-rebase &&
+ echo test-rebase >test-rebase &&
+ git add test-rebase &&
+ git commit -m test-rebase
+ )
+ '
+
+test_expect_success 'make a commit to test rebase' '
+ echo test-rebase-main > test-rebase-main &&
+ git add test-rebase-main &&
+ git commit -m test-rebase-main &&
+ git svn dcommit
+ '
+
+test_expect_success 'git svn rebase works inside a fresh-cloned repository' '
+ (
+ cd test-rebase &&
+ git svn rebase &&
+ test -e test-rebase-main &&
+ test -e test-rebase
+ )'
+
+# Without this, LC_ALL=C as set in test-lib.sh, and Cygwin converts
+# non-ASCII characters in filenames unexpectedly, and causes errors.
+# https://cygwin.com/cygwin-ug-net/using-specialnames.html#pathnames-specialchars
+# > Some characters are disallowed in filenames on Windows filesystems. ...
+# ...
+# > ... All of the above characters, except for the backslash, are converted
+# > to special UNICODE characters in the range 0xf000 to 0xf0ff (the
+# > "Private use area") when creating or accessing files.
+prepare_utf8_locale
+test_expect_success UTF8,!MINGW,!UTF8_NFD_TO_NFC 'svn.pathnameencoding=cp932 new file on dcommit' '
+ LC_ALL=$GIT_TEST_UTF8_LOCALE &&
+ export LC_ALL &&
+ neq=$(printf "\201\202") &&
+ git config svn.pathnameencoding cp932 &&
+ echo neq >"$neq" &&
+ git add "$neq" &&
+ git commit -m "neq" &&
+ git svn dcommit
+'
+
+# See the comment on the above test for setting of LC_ALL.
+test_expect_success !MINGW,!UTF8_NFD_TO_NFC 'svn.pathnameencoding=cp932 rename on dcommit' '
+ LC_ALL=$GIT_TEST_UTF8_LOCALE &&
+ export LC_ALL &&
+ inf=$(printf "\201\207") &&
+ git config svn.pathnameencoding cp932 &&
+ echo inf >"$inf" &&
+ git add "$inf" &&
+ git commit -m "inf" &&
+ git svn dcommit &&
+ git mv "$inf" inf &&
+ git commit -m "inf rename" &&
+ git svn dcommit
+'
+
+test_done
diff --git a/t/t9115/funky-names.dump b/t/t9115/funky-names.dump
new file mode 100644
index 0000000..42422f7
--- /dev/null
+++ b/t/t9115/funky-names.dump
@@ -0,0 +1,103 @@
+SVN-fs-dump-format-version: 2
+
+UUID: 819c44fe-2bcc-4066-88e4-985e2bc0b418
+
+Revision-number: 0
+Prop-content-length: 56
+Content-length: 56
+
+K 8
+svn:date
+V 27
+2007-07-12T07:54:26.062914Z
+PROPS-END
+
+Revision-number: 1
+Prop-content-length: 152
+Content-length: 152
+
+K 7
+svn:log
+V 44
+what will those wacky people think of next?
+
+K 10
+svn:author
+V 12
+normalperson
+K 8
+svn:date
+V 27
+2007-07-12T08:00:05.011573Z
+PROPS-END
+
+Node-path: leading space
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Node-path: leading space file
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 5
+Text-content-md5: e4fa20c67542cdc21271e08d329397ab
+Content-length: 15
+
+PROPS-END
+ugly
+
+
+Node-path: #{bad_directory_name}
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Node-path: #{cool_name}
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 18
+Text-content-md5: 87dac40ca337dfa3dcc8911388c3ddda
+Content-length: 28
+
+PROPS-END
+strange name here
+
+
+Node-path: dir name with spaces
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Node-path: file name with spaces
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 7
+Text-content-md5: c1f10cfd640618484a2a475c11410fd3
+Content-length: 17
+
+PROPS-END
+spaces
+
+
+Node-path: regular_dir_name
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
diff --git a/t/t9116-git-svn-log.sh b/t/t9116-git-svn-log.sh
new file mode 100755
index 0000000..d74d7b2
--- /dev/null
+++ b/t/t9116-git-svn-log.sh
@@ -0,0 +1,150 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Eric Wong
+#
+
+test_description='git svn log tests'
+
+. ./lib-git-svn.sh
+
+test_expect_success 'setup repository and import' '
+ mkdir import &&
+ (
+ cd import &&
+ for i in trunk branches/a branches/b tags/0.1 tags/0.2 tags/0.3
+ do
+ mkdir -p $i &&
+ echo hello >>$i/README ||
+ exit 1
+ done &&
+ svn_cmd import -m test . "$svnrepo"
+ ) &&
+ git svn init "$svnrepo" -T trunk -b branches -t tags &&
+ git svn fetch &&
+ git reset --hard origin/trunk &&
+ echo bye >> README &&
+ git commit -a -m bye &&
+ git svn dcommit &&
+ git reset --hard origin/a &&
+ echo why >> FEEDME &&
+ git update-index --add FEEDME &&
+ git commit -m feedme &&
+ git svn dcommit &&
+ git reset --hard origin/trunk &&
+ echo aye >> README &&
+ git commit -a -m aye &&
+ git svn dcommit &&
+ git reset --hard origin/b &&
+ echo spy >> README &&
+ git commit -a -m spy &&
+ echo try >> README &&
+ git commit -a -m try &&
+ git svn dcommit
+ '
+
+test_expect_success 'run log' "
+ git reset --hard origin/a &&
+ git svn log -r2 origin/trunk >out &&
+ grep ^r2 out &&
+ git svn log -r4 origin/trunk >out &&
+ grep ^r4 out &&
+ git svn log -r3 >out &&
+ grep ^r3 out
+ "
+
+test_expect_success 'run log against a from trunk' "
+ git reset --hard origin/trunk &&
+ git svn log -r3 origin/a >out &&
+ grep ^r3 out
+ "
+
+printf 'r1 \nr2 \nr4 \n' > expected-range-r1-r2-r4
+
+test_expect_success 'test ascending revision range' "
+ git reset --hard origin/trunk &&
+ git svn log -r 1:4 | grep '^r[0-9]' | cut -d'|' -f1 | test_cmp expected-range-r1-r2-r4 -
+ "
+
+test_expect_success 'test ascending revision range with --show-commit' "
+ git reset --hard origin/trunk &&
+ git svn log --show-commit -r 1:4 | grep '^r[0-9]' | cut -d'|' -f1 | test_cmp expected-range-r1-r2-r4 -
+ "
+
+test_expect_success 'test ascending revision range with --show-commit (sha1)' "
+ git svn find-rev r1 >expected-range-r1-r2-r4-sha1 &&
+ git svn find-rev r2 >>expected-range-r1-r2-r4-sha1 &&
+ git svn find-rev r4 >>expected-range-r1-r2-r4-sha1 &&
+ git reset --hard origin/trunk &&
+ git svn log --show-commit -r 1:4 | grep '^r[0-9]' | cut -d'|' -f2 >out &&
+ git rev-parse \$(cat out) >actual &&
+ test_cmp expected-range-r1-r2-r4-sha1 actual
+ "
+
+printf 'r4 \nr2 \nr1 \n' > expected-range-r4-r2-r1
+
+test_expect_success 'test descending revision range' "
+ git reset --hard origin/trunk &&
+ git svn log -r 4:1 | grep '^r[0-9]' | cut -d'|' -f1 | test_cmp expected-range-r4-r2-r1 -
+ "
+
+printf 'r1 \nr2 \n' > expected-range-r1-r2
+
+test_expect_success 'test ascending revision range with unreachable revision' "
+ git reset --hard origin/trunk &&
+ git svn log -r 1:3 | grep '^r[0-9]' | cut -d'|' -f1 | test_cmp expected-range-r1-r2 -
+ "
+
+printf 'r2 \nr1 \n' > expected-range-r2-r1
+
+test_expect_success 'test descending revision range with unreachable revision' "
+ git reset --hard origin/trunk &&
+ git svn log -r 3:1 | grep '^r[0-9]' | cut -d'|' -f1 | test_cmp expected-range-r2-r1 -
+ "
+
+printf 'r2 \n' > expected-range-r2
+
+test_expect_success 'test ascending revision range with unreachable upper boundary revision and 1 commit' "
+ git reset --hard origin/trunk &&
+ git svn log -r 2:3 | grep '^r[0-9]' | cut -d'|' -f1 | test_cmp expected-range-r2 -
+ "
+
+test_expect_success 'test descending revision range with unreachable upper boundary revision and 1 commit' "
+ git reset --hard origin/trunk &&
+ git svn log -r 3:2 | grep '^r[0-9]' | cut -d'|' -f1 | test_cmp expected-range-r2 -
+ "
+
+printf 'r4 \n' > expected-range-r4
+
+test_expect_success 'test ascending revision range with unreachable lower boundary revision and 1 commit' "
+ git reset --hard origin/trunk &&
+ git svn log -r 3:4 | grep '^r[0-9]' | cut -d'|' -f1 | test_cmp expected-range-r4 -
+ "
+
+test_expect_success 'test descending revision range with unreachable lower boundary revision and 1 commit' "
+ git reset --hard origin/trunk &&
+ git svn log -r 4:3 | grep '^r[0-9]' | cut -d'|' -f1 | test_cmp expected-range-r4 -
+ "
+
+printf -- '------------------------------------------------------------------------\n' > expected-separator
+
+test_expect_success 'test ascending revision range with unreachable boundary revisions and no commits' "
+ git reset --hard origin/trunk &&
+ git svn log -r 5:6 | test_cmp expected-separator -
+ "
+
+test_expect_success 'test descending revision range with unreachable boundary revisions and no commits' "
+ git reset --hard origin/trunk &&
+ git svn log -r 6:5 | test_cmp expected-separator -
+ "
+
+test_expect_success 'test ascending revision range with unreachable boundary revisions and 1 commit' "
+ git reset --hard origin/trunk &&
+ git svn log -r 3:5 | grep '^r[0-9]' | cut -d'|' -f1 | test_cmp expected-range-r4 -
+ "
+
+test_expect_success 'test descending revision range with unreachable boundary revisions and 1 commit' "
+ git reset --hard origin/trunk &&
+ git svn log -r 5:3 | grep '^r[0-9]' | cut -d'|' -f1 | test_cmp expected-range-r4 -
+ "
+
+test_done
diff --git a/t/t9117-git-svn-init-clone.sh b/t/t9117-git-svn-init-clone.sh
new file mode 100755
index 0000000..62de819
--- /dev/null
+++ b/t/t9117-git-svn-init-clone.sh
@@ -0,0 +1,122 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Eric Wong
+#
+
+test_description='git svn init/clone tests'
+
+. ./lib-git-svn.sh
+
+test_expect_success 'setup svnrepo' '
+ mkdir project project/trunk project/branches project/tags &&
+ echo foo > project/trunk/foo &&
+ svn_cmd import -m "$test_description" project "$svnrepo"/project &&
+ rm -rf project
+ '
+
+test_expect_success 'basic clone' '
+ test ! -d trunk &&
+ git svn clone "$svnrepo"/project/trunk &&
+ test -d trunk/.git/svn &&
+ test -e trunk/foo &&
+ rm -rf trunk
+ '
+
+test_expect_success 'clone to target directory' '
+ test ! -d target &&
+ git svn clone "$svnrepo"/project/trunk target &&
+ test -d target/.git/svn &&
+ test -e target/foo &&
+ rm -rf target
+ '
+
+test_expect_success 'clone with --stdlayout' '
+ test ! -d project &&
+ git svn clone -s "$svnrepo"/project &&
+ test -d project/.git/svn &&
+ test -e project/foo &&
+ rm -rf project
+ '
+
+test_expect_success 'clone to target directory with --stdlayout' '
+ test ! -d target &&
+ git svn clone -s "$svnrepo"/project target &&
+ test -d target/.git/svn &&
+ test -e target/foo &&
+ rm -rf target
+ '
+
+test_expect_success 'init without -s/-T/-b/-t does not warn' '
+ test ! -d trunk &&
+ git svn init "$svnrepo"/project/trunk trunk 2>warning &&
+ ! grep -q prefix warning &&
+ rm -rf trunk &&
+ rm -f warning
+ '
+
+test_expect_success 'clone without -s/-T/-b/-t does not warn' '
+ test ! -d trunk &&
+ git svn clone "$svnrepo"/project/trunk 2>warning &&
+ ! grep -q prefix warning &&
+ rm -rf trunk &&
+ rm -f warning
+ '
+
+test_svn_configured_prefix () {
+ prefix=$1 &&
+ cat >expect <<EOF &&
+project/trunk:refs/remotes/${prefix}trunk
+project/branches/*:refs/remotes/${prefix}*
+project/tags/*:refs/remotes/${prefix}tags/*
+EOF
+ test ! -f actual &&
+ git --git-dir=project/.git config svn-remote.svn.fetch >>actual &&
+ git --git-dir=project/.git config svn-remote.svn.branches >>actual &&
+ git --git-dir=project/.git config svn-remote.svn.tags >>actual &&
+ test_cmp expect actual &&
+ rm -f expect actual
+}
+
+test_expect_success 'init with -s/-T/-b/-t assumes --prefix=origin/' '
+ test ! -d project &&
+ git svn init -s "$svnrepo"/project project 2>warning &&
+ ! grep -q prefix warning &&
+ test_svn_configured_prefix "origin/" &&
+ rm -rf project &&
+ rm -f warning
+ '
+
+test_expect_success 'clone with -s/-T/-b/-t assumes --prefix=origin/' '
+ test ! -d project &&
+ git svn clone -s "$svnrepo"/project 2>warning &&
+ ! grep -q prefix warning &&
+ test_svn_configured_prefix "origin/" &&
+ rm -rf project &&
+ rm -f warning
+ '
+
+test_expect_success 'init with -s/-T/-b/-t and --prefix "" still works' '
+ test ! -d project &&
+ git svn init -s "$svnrepo"/project project --prefix "" 2>warning &&
+ ! grep -q prefix warning &&
+ test_svn_configured_prefix "" &&
+ rm -rf project &&
+ rm -f warning
+ '
+
+test_expect_success 'clone with -s/-T/-b/-t and --prefix "" still works' '
+ test ! -d project &&
+ git svn clone -s "$svnrepo"/project --prefix "" 2>warning &&
+ ! grep -q prefix warning &&
+ test_svn_configured_prefix "" &&
+ rm -rf project &&
+ rm -f warning
+ '
+
+test_expect_success 'init with -T as a full url works' '
+ test ! -d project &&
+ git svn init -T "$svnrepo"/project/trunk project &&
+ rm -rf project
+ '
+
+test_done
diff --git a/t/t9118-git-svn-funky-branch-names.sh b/t/t9118-git-svn-funky-branch-names.sh
new file mode 100755
index 0000000..a159ff9
--- /dev/null
+++ b/t/t9118-git-svn-funky-branch-names.sh
@@ -0,0 +1,90 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Eric Wong
+#
+
+test_description='git svn funky branch names'
+. ./lib-git-svn.sh
+
+# Abo-Uebernahme (Bug #994)
+scary_uri='Abo-Uebernahme%20%28Bug%20%23994%29'
+scary_ref='Abo-Uebernahme%20(Bug%20#994)'
+
+test_expect_success 'setup svnrepo' '
+ mkdir project project/trunk project/branches project/tags &&
+ echo foo > project/trunk/foo &&
+ svn_cmd import -m "$test_description" project "$svnrepo/pr ject" &&
+ rm -rf project &&
+ svn_cmd cp -m "fun" "$svnrepo/pr ject/trunk" \
+ "$svnrepo/pr ject/branches/fun plugin" &&
+ svn_cmd cp -m "more fun!" "$svnrepo/pr ject/branches/fun plugin" \
+ "$svnrepo/pr ject/branches/more fun plugin!" &&
+ svn_cmd cp -m "scary" "$svnrepo/pr ject/branches/fun plugin" \
+ "$svnrepo/pr ject/branches/$scary_uri" &&
+ svn_cmd cp -m "leading dot" "$svnrepo/pr ject/trunk" \
+ "$svnrepo/pr ject/branches/.leading_dot" &&
+ if test_have_prereq !MINGW
+ then
+ svn_cmd cp -m "trailing dot" "$svnrepo/pr ject/trunk" \
+ "$svnrepo/pr ject/branches/trailing_dot."
+ fi &&
+ svn_cmd cp -m "trailing .lock" "$svnrepo/pr ject/trunk" \
+ "$svnrepo/pr ject/branches/trailing_dotlock.lock" &&
+ svn_cmd cp -m "reflog" "$svnrepo/pr ject/trunk" \
+ "$svnrepo/pr ject/branches/not-a@{0}reflog@" &&
+ maybe_start_httpd
+ '
+
+# SVN 1.7 will truncate "not-a%40{0]" to just "not-a".
+# Look at what SVN wound up naming the branch and use that.
+# Be sure to escape the @ if it shows up.
+non_reflog=$(svn_cmd ls "$svnrepo/pr ject/branches" | grep not-a | sed 's/\///' | sed 's/@/%40/')
+
+test_expect_success 'test clone with funky branch names' '
+ git svn clone -s "$svnrepo/pr ject" project &&
+ (
+ cd project &&
+ git rev-parse "refs/remotes/origin/fun%20plugin" &&
+ git rev-parse "refs/remotes/origin/more%20fun%20plugin!" &&
+ git rev-parse "refs/remotes/origin/$scary_ref" &&
+ git rev-parse "refs/remotes/origin/%2Eleading_dot" &&
+ if test_have_prereq !MINGW
+ then
+ git rev-parse "refs/remotes/origin/trailing_dot%2E"
+ fi &&
+ git rev-parse "refs/remotes/origin/trailing_dotlock%2Elock" &&
+ git rev-parse "refs/remotes/origin/$non_reflog"
+ )
+ '
+
+test_expect_success 'test dcommit to funky branch' "
+ (
+ cd project &&
+ git reset --hard 'refs/remotes/origin/more%20fun%20plugin!' &&
+ echo hello >> foo &&
+ git commit -m 'hello' -- foo &&
+ git svn dcommit
+ )
+ "
+
+test_expect_success 'test dcommit to scary branch' '
+ (
+ cd project &&
+ git reset --hard "refs/remotes/origin/$scary_ref" &&
+ echo urls are scary >> foo &&
+ git commit -m "eep" -- foo &&
+ git svn dcommit
+ )
+ '
+
+test_expect_success 'test dcommit to trailing_dotlock branch' '
+ (
+ cd project &&
+ git reset --hard "refs/remotes/origin/trailing_dotlock%2Elock" &&
+ echo who names branches like this anyway? >> foo &&
+ git commit -m "bar" -- foo &&
+ git svn dcommit
+ )
+ '
+
+test_done
diff --git a/t/t9119-git-svn-info.sh b/t/t9119-git-svn-info.sh
new file mode 100755
index 0000000..088d1c5
--- /dev/null
+++ b/t/t9119-git-svn-info.sh
@@ -0,0 +1,391 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 David D. Kilzer
+
+test_description='git svn info'
+
+. ./lib-git-svn.sh
+
+# Tested with: svn, version 1.4.4 (r25188)
+# Tested with: svn, version 1.6.[12345689]
+v=$(svn_cmd --version | sed -n -e 's/^svn, version \(1\.[0-9]*\.[0-9]*\).*$/\1/p')
+case $v in
+1.[456].*)
+ ;;
+*)
+ skip_all="skipping svn-info test (SVN version: $v not supported)"
+ test_done
+ ;;
+esac
+
+# On the "Text Last Updated" line, "git svn info" does not return the
+# same value as "svn info" (i.e. the commit timestamp that touched the
+# path most recently); do not expect that field to match.
+test_cmp_info () {
+ sed -e '/^Text Last Updated:/d' "$1" >tmp.expect &&
+ sed -e '/^Text Last Updated:/d' "$2" >tmp.actual &&
+ test_cmp tmp.expect tmp.actual &&
+ rm -f tmp.expect tmp.actual
+}
+
+quoted_svnrepo="$(echo $svnrepo | test_uri_escape)"
+
+test_expect_success 'setup repository and import' '
+ mkdir info &&
+ (
+ cd info &&
+ echo FIRST >A &&
+ echo one >file &&
+ ln -s file symlink-file &&
+ mkdir directory &&
+ touch directory/.placeholder &&
+ ln -s directory symlink-directory &&
+ svn_cmd import -m "initial" . "$svnrepo"
+ ) &&
+ svn_cmd co "$svnrepo" svnwc &&
+ (
+ cd svnwc &&
+ echo foo >foo &&
+ svn_cmd add foo &&
+ svn_cmd commit -m "change outside directory" &&
+ svn_cmd update
+ ) &&
+ mkdir gitwc &&
+ (
+ cd gitwc &&
+ git svn init "$svnrepo" &&
+ git svn fetch
+ )
+ '
+
+test_expect_success 'info' "
+ (cd svnwc && svn info) > expected.info &&
+ (cd gitwc && git svn info) > actual.info &&
+ test_cmp_info expected.info actual.info
+ "
+
+test_expect_success 'info --url' '
+ test "$(cd gitwc && git svn info --url)" = "$quoted_svnrepo"
+ '
+
+test_expect_success 'info .' "
+ (cd svnwc && svn info .) > expected.info-dot &&
+ (cd gitwc && git svn info .) > actual.info-dot &&
+ test_cmp_info expected.info-dot actual.info-dot
+ "
+
+test_expect_success 'info $(pwd)' '
+ (cd svnwc && svn info "$(pwd)") >expected.info-pwd &&
+ (cd gitwc && git svn info "$(pwd)") >actual.info-pwd &&
+ grep -v ^Path: <expected.info-pwd >expected.info-np &&
+ grep -v ^Path: <actual.info-pwd >actual.info-np &&
+ test_cmp_info expected.info-np actual.info-np &&
+ test "$(sed -ne \"/^Path:/ s!/svnwc!!\" <expected.info-pwd)" = \
+ "$(sed -ne \"/^Path:/ s!/gitwc!!\" <actual.info-pwd)"
+ '
+
+test_expect_success 'info $(pwd)/../___wc' '
+ (cd svnwc && svn info "$(pwd)/../svnwc") >expected.info-pwd &&
+ (cd gitwc && git svn info "$(pwd)/../gitwc") >actual.info-pwd &&
+ grep -v ^Path: <expected.info-pwd >expected.info-np &&
+ grep -v ^Path: <actual.info-pwd >actual.info-np &&
+ test_cmp_info expected.info-np actual.info-np &&
+ test "$(sed -ne \"/^Path:/ s!/svnwc!!\" <expected.info-pwd)" = \
+ "$(sed -ne \"/^Path:/ s!/gitwc!!\" <actual.info-pwd)"
+ '
+
+test_expect_success 'info $(pwd)/../___wc//file' '
+ (cd svnwc && svn info "$(pwd)/../svnwc//file") >expected.info-pwd &&
+ (cd gitwc && git svn info "$(pwd)/../gitwc//file") >actual.info-pwd &&
+ grep -v ^Path: <expected.info-pwd >expected.info-np &&
+ grep -v ^Path: <actual.info-pwd >actual.info-np &&
+ test_cmp_info expected.info-np actual.info-np &&
+ test "$(sed -ne \"/^Path:/ s!/svnwc!!\" <expected.info-pwd)" = \
+ "$(sed -ne \"/^Path:/ s!/gitwc!!\" <actual.info-pwd)"
+ '
+
+test_expect_success 'info --url .' '
+ test "$(cd gitwc && git svn info --url .)" = "$quoted_svnrepo"
+ '
+
+test_expect_success 'info file' "
+ (cd svnwc && svn info file) > expected.info-file &&
+ (cd gitwc && git svn info file) > actual.info-file &&
+ test_cmp_info expected.info-file actual.info-file
+ "
+
+test_expect_success 'info --url file' '
+ test "$(cd gitwc && git svn info --url file)" = "$quoted_svnrepo/file"
+ '
+
+test_expect_success 'info directory' "
+ (cd svnwc && svn info directory) > expected.info-directory &&
+ (cd gitwc && git svn info directory) > actual.info-directory &&
+ test_cmp_info expected.info-directory actual.info-directory
+ "
+
+test_expect_success 'info inside directory' "
+ (cd svnwc/directory && svn info) > expected.info-inside-directory &&
+ (cd gitwc/directory && git svn info) > actual.info-inside-directory &&
+ test_cmp_info expected.info-inside-directory actual.info-inside-directory
+ "
+
+test_expect_success 'info --url directory' '
+ test "$(cd gitwc && git svn info --url directory)" = "$quoted_svnrepo/directory"
+ '
+
+test_expect_success 'info symlink-file' "
+ (cd svnwc && svn info symlink-file) > expected.info-symlink-file &&
+ (cd gitwc && git svn info symlink-file) > actual.info-symlink-file &&
+ test_cmp_info expected.info-symlink-file actual.info-symlink-file
+ "
+
+test_expect_success 'info --url symlink-file' '
+ test "$(cd gitwc && git svn info --url symlink-file)" \
+ = "$quoted_svnrepo/symlink-file"
+ '
+
+test_expect_success 'info symlink-directory' "
+ (cd svnwc && svn info symlink-directory) \
+ > expected.info-symlink-directory &&
+ (cd gitwc && git svn info symlink-directory) \
+ > actual.info-symlink-directory &&
+ test_cmp_info expected.info-symlink-directory actual.info-symlink-directory
+ "
+
+test_expect_success 'info --url symlink-directory' '
+ test "$(cd gitwc && git svn info --url symlink-directory)" \
+ = "$quoted_svnrepo/symlink-directory"
+ '
+
+test_expect_success 'info added-file' "
+ echo two > gitwc/added-file &&
+ (
+ cd gitwc &&
+ git add added-file
+ ) &&
+ cp gitwc/added-file svnwc/added-file &&
+ (
+ cd svnwc &&
+ svn_cmd add added-file > /dev/null
+ ) &&
+ (cd svnwc && svn info added-file) > expected.info-added-file &&
+ (cd gitwc && git svn info added-file) > actual.info-added-file &&
+ test_cmp_info expected.info-added-file actual.info-added-file
+ "
+
+test_expect_success 'info --url added-file' '
+ test "$(cd gitwc && git svn info --url added-file)" \
+ = "$quoted_svnrepo/added-file"
+ '
+
+test_expect_success 'info added-directory' "
+ mkdir gitwc/added-directory svnwc/added-directory &&
+ touch gitwc/added-directory/.placeholder &&
+ (
+ cd svnwc &&
+ svn_cmd add added-directory > /dev/null
+ ) &&
+ (
+ cd gitwc &&
+ git add added-directory
+ ) &&
+ (cd svnwc && svn info added-directory) \
+ > expected.info-added-directory &&
+ (cd gitwc && git svn info added-directory) \
+ > actual.info-added-directory &&
+ test_cmp_info expected.info-added-directory actual.info-added-directory
+ "
+
+test_expect_success 'info --url added-directory' '
+ test "$(cd gitwc && git svn info --url added-directory)" \
+ = "$quoted_svnrepo/added-directory"
+ '
+
+test_expect_success 'info added-symlink-file' "
+ (
+ cd gitwc &&
+ ln -s added-file added-symlink-file &&
+ git add added-symlink-file
+ ) &&
+ (
+ cd svnwc &&
+ ln -s added-file added-symlink-file &&
+ svn_cmd add added-symlink-file > /dev/null
+ ) &&
+ (cd svnwc && svn info added-symlink-file) \
+ > expected.info-added-symlink-file &&
+ (cd gitwc && git svn info added-symlink-file) \
+ > actual.info-added-symlink-file &&
+ test_cmp_info expected.info-added-symlink-file \
+ actual.info-added-symlink-file
+ "
+
+test_expect_success 'info --url added-symlink-file' '
+ test "$(cd gitwc && git svn info --url added-symlink-file)" \
+ = "$quoted_svnrepo/added-symlink-file"
+ '
+
+test_expect_success 'info added-symlink-directory' "
+ (
+ cd gitwc &&
+ ln -s added-directory added-symlink-directory &&
+ git add added-symlink-directory
+ ) &&
+ (
+ cd svnwc &&
+ ln -s added-directory added-symlink-directory &&
+ svn_cmd add added-symlink-directory > /dev/null
+ ) &&
+ (cd svnwc && svn info added-symlink-directory) \
+ > expected.info-added-symlink-directory &&
+ (cd gitwc && git svn info added-symlink-directory) \
+ > actual.info-added-symlink-directory &&
+ test_cmp_info expected.info-added-symlink-directory \
+ actual.info-added-symlink-directory
+ "
+
+test_expect_success 'info --url added-symlink-directory' '
+ test "$(cd gitwc && git svn info --url added-symlink-directory)" \
+ = "$quoted_svnrepo/added-symlink-directory"
+ '
+
+test_expect_success 'info deleted-file' "
+ (
+ cd gitwc &&
+ git rm -f file > /dev/null
+ ) &&
+ (
+ cd svnwc &&
+ svn_cmd rm --force file > /dev/null
+ ) &&
+ (cd svnwc && svn info file) >expected.info-deleted-file &&
+ (cd gitwc && git svn info file) >actual.info-deleted-file &&
+ test_cmp_info expected.info-deleted-file actual.info-deleted-file
+ "
+
+test_expect_success 'info --url file (deleted)' '
+ test "$(cd gitwc && git svn info --url file)" \
+ = "$quoted_svnrepo/file"
+ '
+
+test_expect_success 'info deleted-directory' "
+ (
+ cd gitwc &&
+ git rm -r -f directory > /dev/null
+ ) &&
+ (
+ cd svnwc &&
+ svn_cmd rm --force directory > /dev/null
+ ) &&
+ (cd svnwc && svn info directory) >expected.info-deleted-directory &&
+ (cd gitwc && git svn info directory) >actual.info-deleted-directory &&
+ test_cmp_info expected.info-deleted-directory actual.info-deleted-directory
+ "
+
+test_expect_success 'info --url directory (deleted)' '
+ test "$(cd gitwc && git svn info --url directory)" \
+ = "$quoted_svnrepo/directory"
+ '
+
+test_expect_success 'info deleted-symlink-file' "
+ (
+ cd gitwc &&
+ git rm -f symlink-file > /dev/null
+ ) &&
+ (
+ cd svnwc &&
+ svn_cmd rm --force symlink-file > /dev/null
+ ) &&
+ (cd svnwc && svn info symlink-file) >expected.info-deleted-symlink-file &&
+ (cd gitwc && git svn info symlink-file) >actual.info-deleted-symlink-file &&
+ test_cmp_info expected.info-deleted-symlink-file actual.info-deleted-symlink-file
+ "
+
+test_expect_success 'info --url symlink-file (deleted)' '
+ test "$(cd gitwc && git svn info --url symlink-file)" \
+ = "$quoted_svnrepo/symlink-file"
+ '
+
+test_expect_success 'info deleted-symlink-directory' "
+ (
+ cd gitwc &&
+ git rm -f symlink-directory > /dev/null
+ ) &&
+ (
+ cd svnwc &&
+ svn_cmd rm --force symlink-directory > /dev/null
+ ) &&
+ (cd svnwc && svn info symlink-directory) >expected.info-deleted-symlink-directory &&
+ (cd gitwc && git svn info symlink-directory) >actual.info-deleted-symlink-directory &&
+ test_cmp_info expected.info-deleted-symlink-directory actual.info-deleted-symlink-directory
+ "
+
+test_expect_success 'info --url symlink-directory (deleted)' '
+ test "$(cd gitwc && git svn info --url symlink-directory)" \
+ = "$quoted_svnrepo/symlink-directory"
+ '
+
+# NOTE: git does not have the concept of replaced objects,
+# so we can't test for files in that state.
+
+test_expect_success 'info unknown-file' "
+ echo two > gitwc/unknown-file &&
+ (cd gitwc && test_must_fail git svn info unknown-file) \
+ 2> actual.info-unknown-file &&
+ grep unknown-file actual.info-unknown-file
+ "
+
+test_expect_success 'info --url unknown-file' '
+ echo two > gitwc/unknown-file &&
+ (cd gitwc && test_must_fail git svn info --url unknown-file) \
+ 2> actual.info-url-unknown-file &&
+ grep unknown-file actual.info-url-unknown-file
+ '
+
+test_expect_success 'info unknown-directory' "
+ mkdir gitwc/unknown-directory svnwc/unknown-directory &&
+ (cd gitwc && test_must_fail git svn info unknown-directory) \
+ 2> actual.info-unknown-directory &&
+ grep unknown-directory actual.info-unknown-directory
+ "
+
+test_expect_success 'info --url unknown-directory' '
+ (cd gitwc && test_must_fail git svn info --url unknown-directory) \
+ 2> actual.info-url-unknown-directory &&
+ grep unknown-directory actual.info-url-unknown-directory
+ '
+
+test_expect_success 'info unknown-symlink-file' "
+ (
+ cd gitwc &&
+ ln -s unknown-file unknown-symlink-file
+ ) &&
+ (cd gitwc && test_must_fail git svn info unknown-symlink-file) \
+ 2> actual.info-unknown-symlink-file &&
+ grep unknown-symlink-file actual.info-unknown-symlink-file
+ "
+
+test_expect_success 'info --url unknown-symlink-file' '
+ (cd gitwc && test_must_fail git svn info --url unknown-symlink-file) \
+ 2> actual.info-url-unknown-symlink-file &&
+ grep unknown-symlink-file actual.info-url-unknown-symlink-file
+ '
+
+test_expect_success 'info unknown-symlink-directory' "
+ (
+ cd gitwc &&
+ ln -s unknown-directory unknown-symlink-directory
+ ) &&
+ (cd gitwc && test_must_fail git svn info unknown-symlink-directory) \
+ 2> actual.info-unknown-symlink-directory &&
+ grep unknown-symlink-directory actual.info-unknown-symlink-directory
+ "
+
+test_expect_success 'info --url unknown-symlink-directory' '
+ (cd gitwc && test_must_fail git svn info --url unknown-symlink-directory) \
+ 2> actual.info-url-unknown-symlink-directory &&
+ grep unknown-symlink-directory actual.info-url-unknown-symlink-directory
+ '
+
+test_done
diff --git a/t/t9120-git-svn-clone-with-percent-escapes.sh b/t/t9120-git-svn-clone-with-percent-escapes.sh
new file mode 100755
index 0000000..40b714d
--- /dev/null
+++ b/t/t9120-git-svn-clone-with-percent-escapes.sh
@@ -0,0 +1,77 @@
+#!/bin/sh
+#
+# Copyright (c) 2008 Kevin Ballard
+#
+
+test_description='git svn clone with percent escapes'
+. ./lib-git-svn.sh
+
+test_expect_success 'setup svnrepo' '
+ mkdir project project/trunk project/branches project/tags &&
+ echo foo > project/trunk/foo &&
+ svn_cmd import -m "$test_description" project "$svnrepo/pr ject" &&
+ svn_cmd cp -m "branch" "$svnrepo/pr ject/trunk" \
+ "$svnrepo/pr ject/branches/b" &&
+ svn_cmd cp -m "tag" "$svnrepo/pr ject/trunk" \
+ "$svnrepo/pr ject/tags/v1" &&
+ rm -rf project &&
+ maybe_start_httpd
+'
+
+test_expect_success 'test clone with percent escapes' '
+ git svn clone "$svnrepo/pr%20ject" clone &&
+ (
+ cd clone &&
+ git rev-parse refs/remotes/git-svn
+ )
+'
+
+# SVN works either way, so should we...
+
+test_expect_success 'svn checkout with percent escapes' '
+ svn_cmd checkout "$svnrepo/pr%20ject" svn.percent &&
+ svn_cmd checkout "$svnrepo/pr%20ject/trunk" svn.percent.trunk
+'
+
+test_expect_success 'svn checkout with space' '
+ svn_cmd checkout "$svnrepo/pr ject" svn.space &&
+ svn_cmd checkout "$svnrepo/pr ject/trunk" svn.space.trunk
+'
+
+test_expect_success 'test clone trunk with percent escapes and minimize-url' '
+ git svn clone --minimize-url "$svnrepo/pr%20ject/trunk" minimize &&
+ (
+ cd minimize &&
+ git rev-parse refs/remotes/git-svn
+ )
+'
+
+test_expect_success 'test clone trunk with percent escapes' '
+ git svn clone "$svnrepo/pr%20ject/trunk" trunk &&
+ (
+ cd trunk &&
+ git rev-parse refs/remotes/git-svn
+ )
+'
+
+test_expect_success 'test clone --stdlayout with percent escapes' '
+ git svn clone --stdlayout "$svnrepo/pr%20ject" percent &&
+ (
+ cd percent &&
+ git rev-parse refs/remotes/origin/trunk^0 &&
+ git rev-parse refs/remotes/origin/b^0 &&
+ git rev-parse refs/remotes/origin/tags/v1^0
+ )
+'
+
+test_expect_success 'test clone -s with unescaped space' '
+ git svn clone -s "$svnrepo/pr ject" --prefix origin/ space &&
+ (
+ cd space &&
+ git rev-parse refs/remotes/origin/trunk^0 &&
+ git rev-parse refs/remotes/origin/b^0 &&
+ git rev-parse refs/remotes/origin/tags/v1^0
+ )
+'
+
+test_done
diff --git a/t/t9121-git-svn-fetch-renamed-dir.sh b/t/t9121-git-svn-fetch-renamed-dir.sh
new file mode 100755
index 0000000..000cad3
--- /dev/null
+++ b/t/t9121-git-svn-fetch-renamed-dir.sh
@@ -0,0 +1,20 @@
+#!/bin/sh
+#
+# Copyright (c) 2008 Santhosh Kumar Mani
+
+
+test_description='git svn can fetch renamed directories'
+
+. ./lib-git-svn.sh
+
+test_expect_success 'load repository with renamed directory' '
+ svnadmin load -q "$rawsvnrepo" < "$TEST_DIRECTORY"/t9121/renamed-dir.dump
+ '
+
+test_expect_success 'init and fetch repository' '
+ git svn init "$svnrepo/newname" &&
+ git svn fetch
+ '
+
+test_done
+
diff --git a/t/t9121/renamed-dir.dump b/t/t9121/renamed-dir.dump
new file mode 100644
index 0000000..5f9127b
--- /dev/null
+++ b/t/t9121/renamed-dir.dump
@@ -0,0 +1,90 @@
+SVN-fs-dump-format-version: 2
+
+UUID: 06b9b3ad-f546-4fbe-8328-fcb4e6ef5c3f
+
+Revision-number: 0
+Prop-content-length: 56
+Content-length: 56
+
+K 8
+svn:date
+V 27
+2008-04-02T09:11:59.778557Z
+PROPS-END
+
+Revision-number: 1
+Prop-content-length: 117
+Content-length: 117
+
+K 7
+svn:log
+V 14
+initial import
+K 10
+svn:author
+V 8
+santhosh
+K 8
+svn:date
+V 27
+2008-04-02T09:13:03.170863Z
+PROPS-END
+
+Node-path: name
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Node-path: name/a.txt
+Node-kind: file
+Node-action: add
+Prop-content-length: 71
+Text-content-length: 6
+Text-content-md5: b1946ac92492d2347c6235b4d2611184
+Content-length: 77
+
+K 13
+svn:mime-type
+V 10
+text/plain
+K 13
+svn:eol-style
+V 2
+LF
+PROPS-END
+hello
+
+
+Revision-number: 2
+Prop-content-length: 109
+Content-length: 109
+
+K 7
+svn:log
+V 7
+renamed
+K 10
+svn:author
+V 8
+santhosh
+K 8
+svn:date
+V 27
+2008-04-02T09:14:22.952186Z
+PROPS-END
+
+Node-path: newname
+Node-kind: dir
+Node-action: add
+Node-copyfrom-rev: 1
+Node-copyfrom-path: name
+
+
+Node-path: name
+Node-action: delete
+
+
diff --git a/t/t9122-git-svn-author.sh b/t/t9122-git-svn-author.sh
new file mode 100755
index 0000000..0fc289a
--- /dev/null
+++ b/t/t9122-git-svn-author.sh
@@ -0,0 +1,85 @@
+#!/bin/sh
+
+test_description='git svn authorship'
+
+. ./lib-git-svn.sh
+
+test_expect_success 'setup svn repository' '
+ svn_cmd checkout "$svnrepo" work.svn &&
+ (
+ cd work.svn &&
+ echo >file &&
+ svn_cmd add file &&
+ svn_cmd commit -m "first commit" file
+ )
+'
+
+test_expect_success 'interact with it via git svn' '
+ mkdir work.git &&
+ (
+ cd work.git &&
+ git svn init "$svnrepo" &&
+ git svn fetch &&
+
+ echo modification >file &&
+ test_tick &&
+ git commit -a -m second &&
+
+ test_tick &&
+ git svn dcommit &&
+
+ echo "further modification" >file &&
+ test_tick &&
+ git commit -a -m third &&
+
+ test_tick &&
+ git svn --add-author-from dcommit &&
+
+ echo "yet further modification" >file &&
+ test_tick &&
+ git commit -a -m fourth &&
+
+ test_tick &&
+ git svn --add-author-from --use-log-author dcommit &&
+
+ git log &&
+
+ git show -s HEAD^^ >../actual.2 &&
+ git show -s HEAD^ >../actual.3 &&
+ git show -s HEAD >../actual.4
+
+ ) &&
+
+ # Make sure that --add-author-from without --use-log-author
+ # did not affect the authorship information
+ myself=$(grep "^Author: " actual.2) &&
+ unaffected=$(grep "^Author: " actual.3) &&
+ test "z$myself" = "z$unaffected" &&
+
+ # Make sure lack of --add-author-from did not add cruft
+ ! grep "^ From: A U Thor " actual.2 &&
+
+ # Make sure --add-author-from added cruft
+ grep "^ From: A U Thor " actual.3 &&
+ grep "^ From: A U Thor " actual.4 &&
+
+ # Make sure --add-author-from with --use-log-author affected
+ # the authorship information
+ grep "^Author: A U Thor " actual.4 &&
+
+ # Make sure there are no commit messages with excess blank lines
+ test $(grep "^ " actual.2 | wc -l) = 3 &&
+ test $(grep "^ " actual.3 | wc -l) = 5 &&
+ test $(grep "^ " actual.4 | wc -l) = 5 &&
+
+ # Make sure there are no svn commit messages with excess blank lines
+ (
+ cd work.svn &&
+ svn_cmd up &&
+
+ test $(svn_cmd log -r2:2 | wc -l) = 5 &&
+ test $(svn_cmd log -r4:4 | wc -l) = 7
+ )
+'
+
+test_done
diff --git a/t/t9123-git-svn-rebuild-with-rewriteroot.sh b/t/t9123-git-svn-rebuild-with-rewriteroot.sh
new file mode 100755
index 0000000..ead4045
--- /dev/null
+++ b/t/t9123-git-svn-rebuild-with-rewriteroot.sh
@@ -0,0 +1,32 @@
+#!/bin/sh
+#
+# Copyright (c) 2008 Jan Krüger
+#
+
+test_description='git svn respects rewriteRoot during rebuild'
+
+. ./lib-git-svn.sh
+
+mkdir import
+(cd import
+ touch foo
+ svn_cmd import -m 'import for git svn' . "$svnrepo" >/dev/null
+)
+rm -rf import
+
+test_expect_success 'init, fetch and checkout repository' '
+ git svn init --rewrite-root=http://invalid.invalid/ "$svnrepo" &&
+ git svn fetch &&
+ git checkout -b mybranch remotes/git-svn
+ '
+
+test_expect_success 'remove rev_map' '
+ rm "$GIT_SVN_DIR"/.rev_map.*
+ '
+
+test_expect_success 'rebuild rev_map' '
+ git svn rebase >/dev/null
+ '
+
+test_done
+
diff --git a/t/t9124-git-svn-dcommit-auto-props.sh b/t/t9124-git-svn-dcommit-auto-props.sh
new file mode 100755
index 0000000..9f7231d
--- /dev/null
+++ b/t/t9124-git-svn-dcommit-auto-props.sh
@@ -0,0 +1,105 @@
+#!/bin/sh
+#
+# Copyright (c) 2008 Brad King
+
+test_description='git svn dcommit honors auto-props'
+
+. ./lib-git-svn.sh
+
+generate_auto_props() {
+cat << EOF
+[miscellany]
+enable-auto-props=$1
+[auto-props]
+*.sh = svn:mime-type=application/x-shellscript; svn:eol-style=LF
+*.txt = svn:mime-type=text/plain; svn:eol-style = native
+EOF
+}
+
+test_expect_success 'initialize git svn' '
+ mkdir import &&
+ (
+ cd import &&
+ echo foo >foo &&
+ svn_cmd import -m "import for git svn" . "$svnrepo"
+ ) &&
+ rm -rf import &&
+ git svn init "$svnrepo" &&
+ git svn fetch
+'
+
+test_expect_success 'enable auto-props config' '
+ mkdir user &&
+ generate_auto_props yes >user/config
+'
+
+test_expect_success 'add files matching auto-props' '
+ write_script exec1.sh </dev/null &&
+ echo "hello" >hello.txt &&
+ echo bar >bar &&
+ git add exec1.sh hello.txt bar &&
+ git commit -m "files for enabled auto-props" &&
+ git svn dcommit --config-dir=user
+'
+
+test_expect_success 'disable auto-props config' '
+ generate_auto_props no >user/config
+'
+
+test_expect_success 'add files matching disabled auto-props' '
+ write_script exec2.sh </dev/null &&
+ echo "world" >world.txt &&
+ echo zot >zot &&
+ git add exec2.sh world.txt zot &&
+ git commit -m "files for disabled auto-props" &&
+ git svn dcommit --config-dir=user
+'
+
+test_expect_success 'check resulting svn repository' '
+(
+ mkdir work &&
+ cd work &&
+ svn_cmd co "$svnrepo" &&
+ cd svnrepo &&
+
+ # Check properties from first commit.
+ if test_have_prereq POSIXPERM
+ then
+ test "x$(svn_cmd propget svn:executable exec1.sh)" = "x*"
+ fi &&
+ test "x$(svn_cmd propget svn:mime-type exec1.sh)" = \
+ "xapplication/x-shellscript" &&
+ test "x$(svn_cmd propget svn:mime-type hello.txt)" = "xtext/plain" &&
+ test "x$(svn_cmd propget svn:eol-style hello.txt)" = "xnative" &&
+ test "x$(svn_cmd propget svn:mime-type bar)" = "x" &&
+
+ # Check properties from second commit.
+ if test_have_prereq POSIXPERM
+ then
+ test "x$(svn_cmd propget svn:executable exec2.sh)" = "x*"
+ fi &&
+ test "x$(svn_cmd propget svn:mime-type exec2.sh)" = "x" &&
+ test "x$(svn_cmd propget svn:mime-type world.txt)" = "x" &&
+ test "x$(svn_cmd propget svn:eol-style world.txt)" = "x" &&
+ test "x$(svn_cmd propget svn:mime-type zot)" = "x"
+)
+'
+
+test_expect_success 'check renamed file' '
+ test -d user &&
+ generate_auto_props yes > user/config &&
+ git mv foo foo.sh &&
+ git commit -m "foo => foo.sh" &&
+ git svn dcommit --config-dir=user &&
+ (
+ cd work/svnrepo &&
+ svn_cmd up &&
+ test ! -e foo &&
+ test -e foo.sh &&
+ test "x$(svn_cmd propget svn:mime-type foo.sh)" = \
+ "xapplication/x-shellscript" &&
+ test "x$(svn_cmd propget svn:eol-style foo.sh)" = "xLF"
+ )
+'
+
+test_done
diff --git a/t/t9125-git-svn-multi-glob-branch-names.sh b/t/t9125-git-svn-multi-glob-branch-names.sh
new file mode 100755
index 0000000..0d53fc9
--- /dev/null
+++ b/t/t9125-git-svn-multi-glob-branch-names.sh
@@ -0,0 +1,37 @@
+#!/bin/sh
+# Copyright (c) 2008 Marcus Griep
+
+test_description='git svn multi-glob branch names'
+. ./lib-git-svn.sh
+
+test_expect_success 'setup svnrepo' '
+ mkdir project project/trunk project/branches \
+ project/branches/v14.1 project/tags &&
+ echo foo > project/trunk/foo &&
+ svn_cmd import -m "$test_description" project "$svnrepo/project" &&
+ rm -rf project &&
+ svn_cmd cp -m "fun" "$svnrepo/project/trunk" \
+ "$svnrepo/project/branches/v14.1/beta" &&
+ svn_cmd cp -m "more fun!" "$svnrepo/project/branches/v14.1/beta" \
+ "$svnrepo/project/branches/v14.1/gold"
+ '
+
+test_expect_success 'test clone with multi-glob in branch names' '
+ git svn clone -T trunk -b branches/*/* -t tags \
+ "$svnrepo/project" project &&
+ (cd project &&
+ git rev-parse "refs/remotes/origin/v14.1/beta" &&
+ git rev-parse "refs/remotes/origin/v14.1/gold"
+ )
+ '
+
+test_expect_success 'test dcommit to multi-globbed branch' "
+ (cd project &&
+ git reset --hard 'refs/remotes/origin/v14.1/gold' &&
+ echo hello >> foo &&
+ git commit -m 'hello' -- foo &&
+ git svn dcommit
+ )
+ "
+
+test_done
diff --git a/t/t9126-git-svn-follow-deleted-readded-directory.sh b/t/t9126-git-svn-follow-deleted-readded-directory.sh
new file mode 100755
index 0000000..edec640
--- /dev/null
+++ b/t/t9126-git-svn-follow-deleted-readded-directory.sh
@@ -0,0 +1,22 @@
+#!/bin/sh
+#
+# Copyright (c) 2008 Alec Berryman
+
+test_description='git svn fetch repository with deleted and readded directory'
+
+. ./lib-git-svn.sh
+
+# Don't run this by default; it opens up a port.
+require_svnserve
+
+test_expect_success 'load repository' '
+ svnadmin load -q "$rawsvnrepo" < "$TEST_DIRECTORY"/t9126/follow-deleted-readded.dump
+ '
+
+test_expect_success 'fetch repository' '
+ start_svnserve &&
+ git svn init svn://127.0.0.1:$SVNSERVE_PORT &&
+ git svn fetch
+ '
+
+test_done
diff --git a/t/t9126/follow-deleted-readded.dump b/t/t9126/follow-deleted-readded.dump
new file mode 100644
index 0000000..19da5d1
--- /dev/null
+++ b/t/t9126/follow-deleted-readded.dump
@@ -0,0 +1,201 @@
+SVN-fs-dump-format-version: 2
+
+UUID: 1807dc6f-c693-4cda-9710-00e1be8c1f21
+
+Revision-number: 0
+Prop-content-length: 56
+Content-length: 56
+
+K 8
+svn:date
+V 27
+2008-09-14T19:53:13.006748Z
+PROPS-END
+
+Revision-number: 1
+Prop-content-length: 111
+Content-length: 111
+
+K 7
+svn:log
+V 12
+Create trunk
+K 10
+svn:author
+V 4
+alec
+K 8
+svn:date
+V 27
+2008-09-14T19:53:13.239689Z
+PROPS-END
+
+Node-path: trunk
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Revision-number: 2
+Prop-content-length: 119
+Content-length: 119
+
+K 7
+svn:log
+V 20
+Create trunk/project
+K 10
+svn:author
+V 4
+alec
+K 8
+svn:date
+V 27
+2008-09-14T19:53:13.548860Z
+PROPS-END
+
+Node-path: trunk/project
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Revision-number: 3
+Prop-content-length: 111
+Content-length: 111
+
+K 7
+svn:log
+V 12
+add new file
+K 10
+svn:author
+V 4
+alec
+K 8
+svn:date
+V 27
+2008-09-14T19:53:15.433630Z
+PROPS-END
+
+Node-path: trunk/project/foo
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 4
+Text-content-md5: d3b07384d113edec49eaa6238ad5ff00
+Content-length: 14
+
+PROPS-END
+foo
+
+
+Revision-number: 4
+Prop-content-length: 116
+Content-length: 116
+
+K 7
+svn:log
+V 17
+change foo to bar
+K 10
+svn:author
+V 4
+alec
+K 8
+svn:date
+V 27
+2008-09-14T19:53:17.339884Z
+PROPS-END
+
+Node-path: trunk/project/foo
+Node-kind: file
+Node-action: change
+Text-content-length: 4
+Text-content-md5: c157a79031e1c40f85931829bc5fc552
+Content-length: 4
+
+bar
+
+
+Revision-number: 5
+Prop-content-length: 114
+Content-length: 114
+
+K 7
+svn:log
+V 15
+don't like that
+K 10
+svn:author
+V 4
+alec
+K 8
+svn:date
+V 27
+2008-09-14T19:53:19.335001Z
+PROPS-END
+
+Node-path: trunk/project
+Node-action: delete
+
+
+Revision-number: 6
+Prop-content-length: 110
+Content-length: 110
+
+K 7
+svn:log
+V 11
+reset trunk
+K 10
+svn:author
+V 4
+alec
+K 8
+svn:date
+V 27
+2008-09-14T19:53:19.845897Z
+PROPS-END
+
+Node-path: trunk/project
+Node-kind: dir
+Node-action: add
+Node-copyfrom-rev: 4
+Node-copyfrom-path: trunk/project
+
+
+Revision-number: 7
+Prop-content-length: 113
+Content-length: 113
+
+K 7
+svn:log
+V 14
+change to quux
+K 10
+svn:author
+V 4
+alec
+K 8
+svn:date
+V 27
+2008-09-14T19:53:21.367947Z
+PROPS-END
+
+Node-path: trunk/project/foo
+Node-kind: file
+Node-action: change
+Text-content-length: 5
+Text-content-md5: d3b07a382ec010c01889250fce66fb13
+Content-length: 5
+
+quux
+
+
diff --git a/t/t9127-git-svn-partial-rebuild.sh b/t/t9127-git-svn-partial-rebuild.sh
new file mode 100755
index 0000000..97f495b
--- /dev/null
+++ b/t/t9127-git-svn-partial-rebuild.sh
@@ -0,0 +1,60 @@
+#!/bin/sh
+#
+# Copyright (c) 2008 Deskin Miller
+#
+
+test_description='git svn partial-rebuild tests'
+
+. ./lib-git-svn.sh
+
+test_expect_success 'initialize svnrepo' '
+ mkdir import &&
+ (
+ (cd import &&
+ mkdir trunk branches tags &&
+ (cd trunk &&
+ echo foo > foo
+ ) &&
+ svn_cmd import -m "import for git-svn" . "$svnrepo" >/dev/null &&
+ svn_cmd copy "$svnrepo"/trunk "$svnrepo"/branches/a \
+ -m "created branch a"
+ ) &&
+ rm -rf import &&
+ svn_cmd co "$svnrepo"/trunk trunk &&
+ (cd trunk &&
+ echo bar >> foo &&
+ svn_cmd ci -m "updated trunk"
+ ) &&
+ svn_cmd co "$svnrepo"/branches/a a &&
+ (cd a &&
+ echo baz >> a &&
+ svn_cmd add a &&
+ svn_cmd ci -m "updated a"
+ ) &&
+ git svn init --stdlayout "$svnrepo"
+ )
+'
+
+test_expect_success 'import an early SVN revision into git' '
+ git svn fetch -r1:2
+'
+
+test_expect_success 'make full git mirror of SVN' '
+ mkdir mirror &&
+ (
+ (cd mirror &&
+ git init &&
+ git svn init --stdlayout "$svnrepo" &&
+ git svn fetch
+ )
+ )
+'
+
+test_expect_success 'fetch from git mirror and partial-rebuild' '
+ git config --add remote.origin.url "file://$PWD/mirror/.git" &&
+ git config --add remote.origin.fetch refs/remotes/*:refs/remotes/* &&
+ git fetch origin &&
+ git svn fetch
+'
+
+test_done
diff --git a/t/t9128-git-svn-cmd-branch.sh b/t/t9128-git-svn-cmd-branch.sh
new file mode 100755
index 0000000..783e3ba
--- /dev/null
+++ b/t/t9128-git-svn-cmd-branch.sh
@@ -0,0 +1,79 @@
+#!/bin/sh
+#
+# Copyright (c) 2008 Deskin Miller
+#
+
+test_description='git svn partial-rebuild tests'
+
+. ./lib-git-svn.sh
+
+test_expect_success 'initialize svnrepo' '
+ mkdir import &&
+ (
+ (cd import &&
+ mkdir trunk branches tags &&
+ (cd trunk &&
+ echo foo > foo
+ ) &&
+ svn_cmd import -m "import for git-svn" . "$svnrepo" >/dev/null
+ ) &&
+ rm -rf import &&
+ svn_cmd co "$svnrepo"/trunk trunk &&
+ (cd trunk &&
+ echo bar >> foo &&
+ svn_cmd ci -m "updated trunk"
+ ) &&
+ rm -rf trunk
+ )
+'
+
+test_expect_success 'import into git' '
+ git svn init --stdlayout "$svnrepo" &&
+ git svn fetch &&
+ git checkout remotes/origin/trunk
+'
+
+test_expect_success 'git svn branch tests' '
+ git svn branch a &&
+ base=$(git rev-parse HEAD:) &&
+ test $base = $(git rev-parse remotes/origin/a:) &&
+ git svn branch -m "created branch b blah" b &&
+ test $base = $(git rev-parse remotes/origin/b:) &&
+ test_must_fail git branch -m "no branchname" &&
+ git svn branch -n c &&
+ test_must_fail git rev-parse remotes/origin/c &&
+ test_must_fail git svn branch a &&
+ git svn branch -t tag1 &&
+ test $base = $(git rev-parse remotes/origin/tags/tag1:) &&
+ git svn branch --tag tag2 &&
+ test $base = $(git rev-parse remotes/origin/tags/tag2:) &&
+ git svn tag tag3 &&
+ test $base = $(git rev-parse remotes/origin/tags/tag3:) &&
+ git svn tag -m "created tag4 foo" tag4 &&
+ test $base = $(git rev-parse remotes/origin/tags/tag4:) &&
+ test_must_fail git svn tag -m "no tagname" &&
+ git svn tag -n tag5 &&
+ test_must_fail git rev-parse remotes/origin/tags/tag5 &&
+ test_must_fail git svn tag tag1
+'
+
+test_expect_success 'branch uses correct svn-remote' '
+ (svn_cmd co "$svnrepo" svn &&
+ cd svn &&
+ mkdir mirror &&
+ svn_cmd add mirror &&
+ svn_cmd copy trunk mirror/ &&
+ svn_cmd copy tags mirror/ &&
+ svn_cmd copy branches mirror/ &&
+ svn_cmd ci -m "made mirror" ) &&
+ rm -rf svn &&
+ git svn init -s -R mirror --prefix=mirror/ "$svnrepo"/mirror &&
+ git svn fetch -R mirror &&
+ git checkout mirror/trunk &&
+ base=$(git rev-parse HEAD:) &&
+ git svn branch -m "branch in mirror" d &&
+ test $base = $(git rev-parse remotes/mirror/d:) &&
+ test_must_fail git rev-parse remotes/d
+'
+
+test_done
diff --git a/t/t9129-git-svn-i18n-commitencoding.sh b/t/t9129-git-svn-i18n-commitencoding.sh
new file mode 100755
index 0000000..185248a
--- /dev/null
+++ b/t/t9129-git-svn-i18n-commitencoding.sh
@@ -0,0 +1,92 @@
+#!/bin/sh
+#
+# Copyright (c) 2008 Eric Wong
+
+test_description='git svn honors i18n.commitEncoding in config'
+
+TEST_FAILS_SANITIZE_LEAK=true
+. ./lib-git-svn.sh
+
+compare_git_head_with () {
+ nr=$(wc -l < "$1")
+ a=7
+ b=$(($a + $nr - 1))
+ git cat-file commit HEAD | sed -ne "$a,${b}p" >current &&
+ test_cmp current "$1"
+}
+
+prepare_utf8_locale
+
+compare_svn_head_with () {
+ # extract just the log message and strip out committer info.
+ # don't use --limit here since svn 1.1.x doesn't have it,
+ LC_ALL="$GIT_TEST_UTF8_LOCALE" svn log $(git svn info --url) | perl -w -e '
+ use bytes;
+ $/ = ("-"x72) . "\n";
+ my @x = <STDIN>;
+ @x = split(/\n/, $x[1]);
+ splice(@x, 0, 2);
+ $x[-1] = "";
+ print join("\n", @x);
+ ' > current &&
+ test_cmp current "$1"
+}
+
+for H in ISO8859-1 eucJP ISO-2022-JP
+do
+ test_expect_success "$H setup" '
+ mkdir $H &&
+ svn_cmd import -m "$H test" $H "$svnrepo"/$H &&
+ git svn clone "$svnrepo"/$H $H
+ '
+done
+
+for H in ISO8859-1 eucJP ISO-2022-JP
+do
+ test_expect_success "$H commit on git side" '
+ (
+ cd $H &&
+ git config i18n.commitencoding $H &&
+ git checkout -b t refs/remotes/git-svn &&
+ echo $H >F &&
+ git add F &&
+ git commit -a -F "$TEST_DIRECTORY"/t3900/$H.txt &&
+ E=$(git cat-file commit HEAD | sed -ne "s/^encoding //p") &&
+ test "z$E" = "z$H" &&
+ compare_git_head_with "$TEST_DIRECTORY"/t3900/$H.txt
+ )
+ '
+done
+
+for H in ISO8859-1 eucJP ISO-2022-JP
+do
+ test_expect_success "$H dcommit to svn" '
+ (
+ cd $H &&
+ git svn dcommit &&
+ git cat-file commit HEAD | grep git-svn-id: &&
+ E=$(git cat-file commit HEAD | sed -ne "s/^encoding //p") &&
+ test "z$E" = "z$H" &&
+ compare_git_head_with "$TEST_DIRECTORY"/t3900/$H.txt
+ )
+ '
+done
+
+test_expect_success UTF8 'ISO-8859-1 should match UTF-8 in svn' '
+ (
+ cd ISO8859-1 &&
+ compare_svn_head_with "$TEST_DIRECTORY"/t3900/1-UTF-8.txt
+ )
+'
+
+for H in eucJP ISO-2022-JP
+do
+ test_expect_success UTF8 "$H should match UTF-8 in svn" '
+ (
+ cd $H &&
+ compare_svn_head_with "$TEST_DIRECTORY"/t3900/2-UTF-8.txt
+ )
+ '
+done
+
+test_done
diff --git a/t/t9130-git-svn-authors-file.sh b/t/t9130-git-svn-authors-file.sh
new file mode 100755
index 0000000..90325db
--- /dev/null
+++ b/t/t9130-git-svn-authors-file.sh
@@ -0,0 +1,131 @@
+#!/bin/sh
+#
+# Copyright (c) 2008 Eric Wong
+#
+
+test_description='git svn authors file tests'
+
+. ./lib-git-svn.sh
+
+cat > svn-authors <<EOF
+aa = AAAAAAA AAAAAAA <aa@example.com>
+bb = BBBBBBB BBBBBBB <bb@example.com>
+EOF
+
+test_expect_success 'setup svnrepo' '
+ for i in aa bb cc dd
+ do
+ svn_cmd mkdir -m $i --username $i "$svnrepo"/$i || return 1
+ done
+ '
+
+test_expect_success 'start import with incomplete authors file' '
+ test_must_fail git svn clone --authors-file=svn-authors "$svnrepo" x
+ '
+
+test_expect_success 'imported 2 revisions successfully' '
+ (
+ cd x &&
+ git rev-list refs/remotes/git-svn >actual &&
+ test_line_count = 2 actual &&
+ git rev-list -1 --pretty=raw refs/remotes/git-svn >actual &&
+ grep "^author BBBBBBB BBBBBBB <bb@example\.com> " actual &&
+ git rev-list -1 --pretty=raw refs/remotes/git-svn~1 >actual &&
+ grep "^author AAAAAAA AAAAAAA <aa@example\.com> " actual
+ )
+ '
+
+cat >> svn-authors <<EOF
+cc = CCCCCCC CCCCCCC <cc@example.com>
+dd = DDDDDDD DDDDDDD <dd@example.com>
+EOF
+
+test_expect_success 'continues to import once authors have been added' '
+ (
+ cd x &&
+ git svn fetch --authors-file=../svn-authors &&
+ git rev-list refs/remotes/git-svn >actual &&
+ test_line_count = 4 actual &&
+ git rev-list -1 --pretty=raw refs/remotes/git-svn >actual &&
+ grep "^author DDDDDDD DDDDDDD <dd@example\.com> " actual &&
+ git rev-list -1 --pretty=raw refs/remotes/git-svn~1 >actual &&
+ grep "^author CCCCCCC CCCCCCC <cc@example\.com> " actual
+ )
+ '
+
+test_expect_success 'authors-file against globs' '
+ svn_cmd mkdir -m globs --username aa \
+ "$svnrepo"/aa/trunk "$svnrepo"/aa/branches "$svnrepo"/aa/tags &&
+ git svn clone --authors-file=svn-authors -s "$svnrepo"/aa aa-work &&
+ for i in bb ee cc
+ do
+ branch="aa/branches/$i" &&
+ svn_cmd mkdir -m "$branch" --username $i "$svnrepo/$branch" || return 1
+ done
+ '
+
+test_expect_success 'fetch fails on ee' '
+ ( cd aa-work && test_must_fail git svn fetch --authors-file=../svn-authors )
+ '
+
+tmp_config_get () {
+ git config --file=.git/svn/.metadata --get "$1"
+}
+
+test_expect_success 'failure happened without negative side effects' '
+ (
+ cd aa-work &&
+ test 6 -eq "$(tmp_config_get svn-remote.svn.branches-maxRev)" &&
+ test 6 -eq "$(tmp_config_get svn-remote.svn.tags-maxRev)"
+ )
+ '
+
+cat >> svn-authors <<EOF
+ee = EEEEEEE EEEEEEE <ee@example.com>
+EOF
+
+test_expect_success 'fetch continues after authors-file is fixed' '
+ (
+ cd aa-work &&
+ git svn fetch --authors-file=../svn-authors &&
+ test 8 -eq "$(tmp_config_get svn-remote.svn.branches-maxRev)" &&
+ test 8 -eq "$(tmp_config_get svn-remote.svn.tags-maxRev)"
+ )
+ '
+
+test_expect_success !MINGW 'fresh clone with svn.authors-file in config' '
+ (
+ rm -r "$GIT_DIR" &&
+ test x = x"$(git config svn.authorsfile)" &&
+ test_config="$HOME"/.gitconfig &&
+ sane_unset GIT_DIR &&
+ git config --global \
+ svn.authorsfile "$HOME"/svn-authors &&
+ test x"$HOME"/svn-authors = x"$(git config svn.authorsfile)" &&
+ git svn clone "$svnrepo" gitconfig.clone &&
+ cd gitconfig.clone &&
+ git log >actual &&
+ nr_ex=$(grep "^Author:.*example.com" actual | wc -l) &&
+ git rev-list HEAD >actual &&
+ nr_rev=$(wc -l <actual) &&
+ test $nr_rev -eq $nr_ex
+ )
+'
+
+cat >> svn-authors <<EOF
+ff = FFFFFFF FFFFFFF <>
+EOF
+
+test_expect_success 'authors-file imported user without email' '
+ svn_cmd mkdir -m aa/branches/ff --username ff "$svnrepo/aa/branches/ff" &&
+ (
+ cd aa-work &&
+ git svn fetch --authors-file=../svn-authors &&
+ git rev-list -1 --pretty=raw refs/remotes/origin/ff | \
+ grep "^author FFFFFFF FFFFFFF <> "
+ )
+ '
+
+test_debug 'GIT_DIR=gitconfig.clone/.git git log'
+
+test_done
diff --git a/t/t9131-git-svn-empty-symlink.sh b/t/t9131-git-svn-empty-symlink.sh
new file mode 100755
index 0000000..3bf4255
--- /dev/null
+++ b/t/t9131-git-svn-empty-symlink.sh
@@ -0,0 +1,110 @@
+#!/bin/sh
+
+test_description='test that git handles an svn repository with empty symlinks'
+
+. ./lib-git-svn.sh
+test_expect_success 'load svn dumpfile' '
+ svnadmin load "$rawsvnrepo" <<EOF
+SVN-fs-dump-format-version: 2
+
+UUID: 60780f9a-7df5-43b4-83ab-60e2c0673ef7
+
+Revision-number: 0
+Prop-content-length: 56
+Content-length: 56
+
+K 8
+svn:date
+V 27
+2008-11-26T07:17:27.590577Z
+PROPS-END
+
+Revision-number: 1
+Prop-content-length: 111
+Content-length: 111
+
+K 7
+svn:log
+V 4
+test
+K 10
+svn:author
+V 12
+normalperson
+K 8
+svn:date
+V 27
+2008-11-26T07:18:03.511836Z
+PROPS-END
+
+Node-path: bar
+Node-kind: file
+Node-action: add
+Prop-content-length: 33
+Text-content-length: 0
+Text-content-md5: d41d8cd98f00b204e9800998ecf8427e
+Content-length: 33
+
+K 11
+svn:special
+V 1
+*
+PROPS-END
+
+Revision-number: 2
+Prop-content-length: 121
+Content-length: 121
+
+K 7
+svn:log
+V 13
+bar => doink
+
+K 10
+svn:author
+V 12
+normalperson
+K 8
+svn:date
+V 27
+2008-11-27T03:55:31.601672Z
+PROPS-END
+
+Node-path: bar
+Node-kind: file
+Node-action: change
+Text-content-length: 10
+Text-content-md5: 92ca4fe7a9721f877f765c252dcd66c9
+Content-length: 10
+
+link doink
+
+EOF
+'
+
+test_expect_success 'clone using git svn' 'git svn clone -r1 "$svnrepo" x'
+test_expect_success 'enable broken symlink workaround' \
+ '(cd x && git config svn.brokenSymlinkWorkaround true)'
+test_expect_success '"bar" is an empty file' 'test_must_be_empty x/bar'
+test_expect_success 'get "bar" => symlink fix from svn' \
+ '(cd x && git svn rebase)'
+test_expect_success SYMLINKS '"bar" becomes a symlink' 'test -h x/bar'
+
+
+test_expect_success 'clone using git svn' 'git svn clone -r1 "$svnrepo" y'
+test_expect_success 'disable broken symlink workaround' \
+ '(cd y && git config svn.brokenSymlinkWorkaround false)'
+test_expect_success '"bar" is an empty file' 'test_must_be_empty y/bar'
+test_expect_success 'get "bar" => symlink fix from svn' \
+ '(cd y && git svn rebase)'
+test_expect_success '"bar" does not become a symlink' '! test -L y/bar'
+
+# svn.brokenSymlinkWorkaround is unset
+test_expect_success 'clone using git svn' 'git svn clone -r1 "$svnrepo" z'
+test_expect_success '"bar" is an empty file' 'test_must_be_empty z/bar'
+test_expect_success 'get "bar" => symlink fix from svn' \
+ '(cd z && git svn rebase)'
+test_expect_success '"bar" does not become a symlink' '! test -L z/bar'
+
+
+test_done
diff --git a/t/t9132-git-svn-broken-symlink.sh b/t/t9132-git-svn-broken-symlink.sh
new file mode 100755
index 0000000..aeceffa
--- /dev/null
+++ b/t/t9132-git-svn-broken-symlink.sh
@@ -0,0 +1,102 @@
+#!/bin/sh
+
+test_description='test that git handles an svn repository with empty symlinks'
+
+. ./lib-git-svn.sh
+test_expect_success 'load svn dumpfile' '
+ svnadmin load "$rawsvnrepo" <<EOF
+SVN-fs-dump-format-version: 2
+
+UUID: 60780f9a-7df5-43b4-83ab-60e2c0673ef7
+
+Revision-number: 0
+Prop-content-length: 56
+Content-length: 56
+
+K 8
+svn:date
+V 27
+2008-11-26T07:17:27.590577Z
+PROPS-END
+
+Revision-number: 1
+Prop-content-length: 111
+Content-length: 111
+
+K 7
+svn:log
+V 4
+test
+K 10
+svn:author
+V 12
+normalperson
+K 8
+svn:date
+V 27
+2008-11-26T07:18:03.511836Z
+PROPS-END
+
+Node-path: bar
+Node-kind: file
+Node-action: add
+Prop-content-length: 33
+Text-content-length: 4
+Text-content-md5: 912ec803b2ce49e4a541068d495ab570
+Content-length: 37
+
+K 11
+svn:special
+V 1
+*
+PROPS-END
+asdf
+
+Revision-number: 2
+Prop-content-length: 121
+Content-length: 121
+
+K 7
+svn:log
+V 13
+bar => doink
+
+K 10
+svn:author
+V 12
+normalperson
+K 8
+svn:date
+V 27
+2008-11-27T03:55:31.601672Z
+PROPS-END
+
+Node-path: bar
+Node-kind: file
+Node-action: change
+Text-content-length: 10
+Text-content-md5: 92ca4fe7a9721f877f765c252dcd66c9
+Content-length: 10
+
+link doink
+
+EOF
+'
+
+test_expect_success 'clone using git svn' 'git svn clone -r1 "$svnrepo" x'
+
+test_expect_success SYMLINKS '"bar" is a symlink that points to "asdf"' '
+ test -L x/bar &&
+ (cd x && test xasdf = x"$(git cat-file blob HEAD:bar)")
+'
+
+test_expect_success 'get "bar" => symlink fix from svn' '
+ (cd x && git svn rebase)
+'
+
+test_expect_success SYMLINKS '"bar" remains a proper symlink' '
+ test -L x/bar &&
+ (cd x && test xdoink = x"$(git cat-file blob HEAD:bar)")
+'
+
+test_done
diff --git a/t/t9133-git-svn-nested-git-repo.sh b/t/t9133-git-svn-nested-git-repo.sh
new file mode 100755
index 0000000..d8d5362
--- /dev/null
+++ b/t/t9133-git-svn-nested-git-repo.sh
@@ -0,0 +1,101 @@
+#!/bin/sh
+#
+# Copyright (c) 2009 Eric Wong
+#
+
+test_description='git svn property tests'
+. ./lib-git-svn.sh
+
+test_expect_success 'setup repo with a git repo inside it' '
+ svn_cmd co "$svnrepo" s &&
+ (
+ cd s &&
+ git init &&
+ test -f .git/HEAD &&
+ > .git/a &&
+ echo a > a &&
+ svn_cmd add .git a &&
+ svn_cmd commit -m "create a nested git repo" &&
+ svn_cmd up &&
+ echo hi >> .git/a &&
+ svn_cmd commit -m "modify .git/a" &&
+ svn_cmd up
+ )
+'
+
+test_expect_success 'clone an SVN repo containing a git repo' '
+ git svn clone "$svnrepo" g &&
+ echo a > expect &&
+ test_cmp expect g/a
+'
+
+test_expect_success 'SVN-side change outside of .git' '
+ (
+ cd s &&
+ echo b >> a &&
+ svn_cmd commit -m "SVN-side change outside of .git" &&
+ svn_cmd up &&
+ svn_cmd log -v | grep -F "SVN-side change outside of .git"
+ )
+'
+
+test_expect_success 'update git svn-cloned repo' '
+ (
+ cd g &&
+ git svn rebase &&
+ echo a > expect &&
+ echo b >> expect &&
+ test_cmp expect a &&
+ rm expect
+ )
+'
+
+test_expect_success 'SVN-side change inside of .git' '
+ (
+ cd s &&
+ git add a &&
+ git commit -m "add a inside an SVN repo" &&
+ git log &&
+ svn_cmd add --force .git &&
+ svn_cmd commit -m "SVN-side change inside of .git" &&
+ svn_cmd up &&
+ svn_cmd log -v | grep -F "SVN-side change inside of .git"
+ )
+'
+
+test_expect_success 'update git svn-cloned repo' '
+ (
+ cd g &&
+ git svn rebase &&
+ echo a > expect &&
+ echo b >> expect &&
+ test_cmp expect a &&
+ rm expect
+ )
+'
+
+test_expect_success 'SVN-side change in and out of .git' '
+ (
+ cd s &&
+ echo c >> a &&
+ git add a &&
+ git commit -m "add a inside an SVN repo" &&
+ svn_cmd commit -m "SVN-side change in and out of .git" &&
+ svn_cmd up &&
+ svn_cmd log -v | grep -F "SVN-side change in and out of .git"
+ )
+'
+
+test_expect_success 'update git svn-cloned repo again' '
+ (
+ cd g &&
+ git svn rebase &&
+ echo a > expect &&
+ echo b >> expect &&
+ echo c >> expect &&
+ test_cmp expect a &&
+ rm expect
+ )
+'
+
+test_done
diff --git a/t/t9134-git-svn-ignore-paths.sh b/t/t9134-git-svn-ignore-paths.sh
new file mode 100755
index 0000000..3188400
--- /dev/null
+++ b/t/t9134-git-svn-ignore-paths.sh
@@ -0,0 +1,147 @@
+#!/bin/sh
+#
+# Copyright (c) 2009 Vitaly Shukela
+# Copyright (c) 2009 Eric Wong
+#
+
+test_description='git svn property tests'
+. ./lib-git-svn.sh
+
+test_expect_success 'setup test repository' '
+ svn_cmd co "$svnrepo" s &&
+ (
+ cd s &&
+ mkdir qqq www &&
+ echo test_qqq > qqq/test_qqq.txt &&
+ echo test_www > www/test_www.txt &&
+ svn_cmd add qqq &&
+ svn_cmd add www &&
+ svn_cmd commit -m "create some files" &&
+ svn_cmd up &&
+ echo hi >> www/test_www.txt &&
+ svn_cmd commit -m "modify www/test_www.txt" &&
+ svn_cmd up
+ )
+'
+
+test_expect_success 'clone an SVN repository with ignored www directory' '
+ git svn clone --ignore-paths="^www" "$svnrepo" g &&
+ echo test_qqq > expect &&
+ for i in g/*/*.txt; do cat $i >> expect2 || return 1; done &&
+ test_cmp expect expect2
+'
+
+test_expect_success 'init+fetch an SVN repository with ignored www directory' '
+ git svn init "$svnrepo" c &&
+ ( cd c && git svn fetch --ignore-paths="^www" ) &&
+ rm expect2 &&
+ echo test_qqq > expect &&
+ for i in c/*/*.txt; do cat $i >> expect2 || return 1; done &&
+ test_cmp expect expect2
+'
+
+test_expect_success 'verify ignore-paths config saved by clone' '
+ (
+ cd g &&
+ git config --get svn-remote.svn.ignore-paths | grep www
+ )
+'
+
+test_expect_success 'SVN-side change outside of www' '
+ (
+ cd s &&
+ echo b >> qqq/test_qqq.txt &&
+ svn_cmd commit -m "SVN-side change outside of www" &&
+ svn_cmd up &&
+ svn_cmd log -v | grep "SVN-side change outside of www"
+ )
+'
+
+test_expect_success 'update git svn-cloned repo (config ignore)' '
+ (
+ cd g &&
+ git svn rebase &&
+ printf "test_qqq\nb\n" > expect &&
+ for i in */*.txt; do cat $i >> expect2 || exit 1; done &&
+ test_cmp expect2 expect &&
+ rm expect expect2
+ )
+'
+
+test_expect_success 'update git svn-cloned repo (option ignore)' '
+ (
+ cd c &&
+ git svn rebase --ignore-paths="^www" &&
+ printf "test_qqq\nb\n" > expect &&
+ for i in */*.txt; do cat $i >> expect2 || exit 1; done &&
+ test_cmp expect2 expect &&
+ rm expect expect2
+ )
+'
+
+test_expect_success 'SVN-side change inside of ignored www' '
+ (
+ cd s &&
+ echo zaq >> www/test_www.txt &&
+ svn_cmd commit -m "SVN-side change inside of www/test_www.txt" &&
+ svn_cmd up &&
+ svn_cmd log -v | grep -F "SVN-side change inside of www/test_www.txt"
+ )
+'
+
+test_expect_success 'update git svn-cloned repo (config ignore)' '
+ (
+ cd g &&
+ git svn rebase &&
+ printf "test_qqq\nb\n" > expect &&
+ for i in */*.txt; do cat $i >> expect2 || exit 1; done &&
+ test_cmp expect2 expect &&
+ rm expect expect2
+ )
+'
+
+test_expect_success 'update git svn-cloned repo (option ignore)' '
+ (
+ cd c &&
+ git svn rebase --ignore-paths="^www" &&
+ printf "test_qqq\nb\n" > expect &&
+ for i in */*.txt; do cat $i >> expect2 || exit 1; done &&
+ test_cmp expect2 expect &&
+ rm expect expect2
+ )
+'
+
+test_expect_success 'SVN-side change in and out of ignored www' '
+ (
+ cd s &&
+ echo cvf >> www/test_www.txt &&
+ echo ygg >> qqq/test_qqq.txt &&
+ svn_cmd commit -m "SVN-side change in and out of ignored www" &&
+ svn_cmd up &&
+ svn_cmd log -v | grep "SVN-side change in and out of ignored www"
+ )
+'
+
+test_expect_success 'update git svn-cloned repo again (config ignore)' '
+ (
+ cd g &&
+ git svn rebase &&
+ printf "test_qqq\nb\nygg\n" > expect &&
+ for i in */*.txt; do cat $i >> expect2 || exit 1; done &&
+ test_cmp expect2 expect &&
+ rm expect expect2
+ )
+'
+
+test_expect_success 'update git svn-cloned repo again (option ignore)' '
+ (
+ cd c &&
+ git svn rebase --ignore-paths="^www" &&
+ printf "test_qqq\nb\nygg\n" > expect &&
+ for i in */*.txt; do cat $i >> expect2 || exit 1; done &&
+ test_cmp expect2 expect &&
+ rm expect expect2
+ )
+'
+
+test_done
diff --git a/t/t9135-git-svn-moved-branch-empty-file.sh b/t/t9135-git-svn-moved-branch-empty-file.sh
new file mode 100755
index 0000000..2f80b21
--- /dev/null
+++ b/t/t9135-git-svn-moved-branch-empty-file.sh
@@ -0,0 +1,20 @@
+#!/bin/sh
+
+test_description='test moved svn branch with missing empty files'
+
+. ./lib-git-svn.sh
+test_expect_success 'load svn dumpfile' '
+ svnadmin load "$rawsvnrepo" < "${TEST_DIRECTORY}/t9135/svn.dump"
+ '
+
+test_expect_success 'clone using git svn' 'git svn clone -s "$svnrepo" x'
+
+test_expect_success 'test that b1 exists and is empty' '
+ (
+ cd x &&
+ git reset --hard origin/branch-c &&
+ test_must_be_empty b1
+ )
+ '
+
+test_done
diff --git a/t/t9135/svn.dump b/t/t9135/svn.dump
new file mode 100644
index 0000000..b51c0cc
--- /dev/null
+++ b/t/t9135/svn.dump
@@ -0,0 +1,192 @@
+SVN-fs-dump-format-version: 2
+
+UUID: 1f80e919-e9e3-4d80-a3ae-d9f21095e27b
+
+Revision-number: 0
+Prop-content-length: 56
+Content-length: 56
+
+K 8
+svn:date
+V 27
+2009-02-10T19:23:16.424027Z
+PROPS-END
+
+Revision-number: 1
+Prop-content-length: 123
+Content-length: 123
+
+K 7
+svn:log
+V 20
+init standard layout
+K 10
+svn:author
+V 8
+john.doe
+K 8
+svn:date
+V 27
+2009-02-10T19:23:17.195072Z
+PROPS-END
+
+Node-path: branches
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Node-path: trunk
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Revision-number: 2
+Prop-content-length: 121
+Content-length: 121
+
+K 7
+svn:log
+V 18
+branch-b off trunk
+K 10
+svn:author
+V 8
+john.doe
+K 8
+svn:date
+V 27
+2009-02-10T19:23:19.160095Z
+PROPS-END
+
+Node-path: branches/branch-b
+Node-kind: dir
+Node-action: add
+Node-copyfrom-rev: 1
+Node-copyfrom-path: trunk
+Prop-content-length: 34
+Content-length: 34
+
+K 13
+svn:mergeinfo
+V 0
+
+PROPS-END
+
+
+Revision-number: 3
+Prop-content-length: 120
+Content-length: 120
+
+K 7
+svn:log
+V 17
+add empty file b1
+K 10
+svn:author
+V 8
+john.doe
+K 8
+svn:date
+V 27
+2009-02-10T19:23:20.194568Z
+PROPS-END
+
+Node-path: branches/branch-b/b1
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 0
+Text-content-md5: d41d8cd98f00b204e9800998ecf8427e
+Content-length: 10
+
+PROPS-END
+
+
+Revision-number: 4
+Prop-content-length: 110
+Content-length: 110
+
+K 7
+svn:log
+V 8
+branch-c
+K 10
+svn:author
+V 8
+john.doe
+K 8
+svn:date
+V 27
+2009-02-10T19:23:21.169100Z
+PROPS-END
+
+Node-path: branches/branch-c
+Node-kind: dir
+Node-action: add
+Node-copyfrom-rev: 3
+Node-copyfrom-path: trunk
+
+
+Revision-number: 5
+Prop-content-length: 126
+Content-length: 126
+
+K 7
+svn:log
+V 23
+oops, wrong branchpoint
+K 10
+svn:author
+V 8
+john.doe
+K 8
+svn:date
+V 27
+2009-02-10T19:23:21.253557Z
+PROPS-END
+
+Node-path: branches/branch-c
+Node-action: delete
+
+
+Revision-number: 6
+Prop-content-length: 127
+Content-length: 127
+
+K 7
+svn:log
+V 24
+branch-c off of branch-b
+K 10
+svn:author
+V 8
+john.doe
+K 8
+svn:date
+V 27
+2009-02-10T19:23:21.314659Z
+PROPS-END
+
+Node-path: branches/branch-c
+Node-kind: dir
+Node-action: add
+Node-copyfrom-rev: 5
+Node-copyfrom-path: branches/branch-b
+Prop-content-length: 34
+Content-length: 34
+
+K 13
+svn:mergeinfo
+V 0
+
+PROPS-END
+
+
diff --git a/t/t9136-git-svn-recreated-branch-empty-file.sh b/t/t9136-git-svn-recreated-branch-empty-file.sh
new file mode 100755
index 0000000..733d16e
--- /dev/null
+++ b/t/t9136-git-svn-recreated-branch-empty-file.sh
@@ -0,0 +1,12 @@
+#!/bin/sh
+
+test_description='test recreated svn branch with empty files'
+
+. ./lib-git-svn.sh
+test_expect_success 'load svn dumpfile' '
+ svnadmin load "$rawsvnrepo" < "${TEST_DIRECTORY}/t9136/svn.dump"
+ '
+
+test_expect_success 'clone using git svn' 'git svn clone -s "$svnrepo" x'
+
+test_done
diff --git a/t/t9136/svn.dump b/t/t9136/svn.dump
new file mode 100644
index 0000000..6b1ce0b
--- /dev/null
+++ b/t/t9136/svn.dump
@@ -0,0 +1,192 @@
+SVN-fs-dump-format-version: 2
+
+UUID: eecae021-8f16-48da-969d-79beb8ae6ea5
+
+Revision-number: 0
+Prop-content-length: 56
+Content-length: 56
+
+K 8
+svn:date
+V 27
+2009-02-22T00:50:56.292890Z
+PROPS-END
+
+Revision-number: 1
+Prop-content-length: 106
+Content-length: 106
+
+K 7
+svn:log
+V 4
+init
+K 10
+svn:author
+V 8
+john.doe
+K 8
+svn:date
+V 27
+2009-02-22T00:50:57.192384Z
+PROPS-END
+
+Node-path: branches
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Node-path: tags
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Node-path: trunk
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Node-path: trunk/file
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 0
+Text-content-md5: d41d8cd98f00b204e9800998ecf8427e
+Content-length: 10
+
+PROPS-END
+
+
+Revision-number: 2
+Prop-content-length: 105
+Content-length: 105
+
+K 7
+svn:log
+V 3
+1.0
+K 10
+svn:author
+V 8
+john.doe
+K 8
+svn:date
+V 27
+2009-02-22T00:50:58.124724Z
+PROPS-END
+
+Node-path: tags/1.0
+Node-kind: dir
+Node-action: add
+Node-copyfrom-rev: 1
+Node-copyfrom-path: trunk
+
+
+Revision-number: 3
+Prop-content-length: 111
+Content-length: 111
+
+K 7
+svn:log
+V 9
+1.0.1-bad
+K 10
+svn:author
+V 8
+john.doe
+K 8
+svn:date
+V 27
+2009-02-22T00:50:58.151727Z
+PROPS-END
+
+Node-path: tags/1.0.1
+Node-kind: dir
+Node-action: add
+Node-copyfrom-rev: 2
+Node-copyfrom-path: tags/1.0
+
+
+Revision-number: 4
+Prop-content-length: 111
+Content-length: 111
+
+K 7
+svn:log
+V 9
+Wrong tag
+K 10
+svn:author
+V 8
+john.doe
+K 8
+svn:date
+V 27
+2009-02-22T00:50:58.167427Z
+PROPS-END
+
+Node-path: tags/1.0.1
+Node-action: delete
+
+
+Revision-number: 5
+Prop-content-length: 113
+Content-length: 113
+
+K 7
+svn:log
+V 10
+1.0-branch
+K 10
+svn:author
+V 8
+john.doe
+K 8
+svn:date
+V 27
+2009-02-22T00:50:58.184498Z
+PROPS-END
+
+Node-path: branches/1.0
+Node-kind: dir
+Node-action: add
+Node-copyfrom-rev: 4
+Node-copyfrom-path: tags/1.0
+
+
+Revision-number: 6
+Prop-content-length: 113
+Content-length: 113
+
+K 7
+svn:log
+V 10
+1.0.1-good
+K 10
+svn:author
+V 8
+john.doe
+K 8
+svn:date
+V 27
+2009-02-22T00:50:58.200695Z
+PROPS-END
+
+Node-path: tags/1.0.1
+Node-kind: dir
+Node-action: add
+Node-copyfrom-rev: 5
+Node-copyfrom-path: branches/1.0
+
+
diff --git a/t/t9137-git-svn-dcommit-clobber-series.sh b/t/t9137-git-svn-dcommit-clobber-series.sh
new file mode 100755
index 0000000..067b15b
--- /dev/null
+++ b/t/t9137-git-svn-dcommit-clobber-series.sh
@@ -0,0 +1,63 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Eric Wong
+test_description='git svn dcommit clobber series'
+. ./lib-git-svn.sh
+
+test_expect_success 'initialize repo' '
+ mkdir import &&
+ (cd import &&
+ awk "BEGIN { for (i = 1; i < 64; i++) { print i } }" > file &&
+ svn_cmd import -m "initial" . "$svnrepo"
+ ) &&
+ git svn init "$svnrepo" &&
+ git svn fetch &&
+ test -e file
+ '
+
+test_expect_success '(supposedly) non-conflicting change from SVN' '
+ test x"$(sed -n -e 58p < file)" = x58 &&
+ test x"$(sed -n -e 61p < file)" = x61 &&
+ svn_cmd co "$svnrepo" tmp &&
+ (cd tmp &&
+ perl -i.bak -p -e "s/^58$/5588/" file &&
+ perl -i.bak -p -e "s/^61$/6611/" file &&
+ poke file &&
+ test x"$(sed -n -e 58p < file)" = x5588 &&
+ test x"$(sed -n -e 61p < file)" = x6611 &&
+ svn_cmd commit -m "58 => 5588, 61 => 6611"
+ )
+ '
+
+test_expect_success 'some unrelated changes to git' "
+ echo hi > life &&
+ git update-index --add life &&
+ git commit -m hi-life &&
+ echo bye >> life &&
+ git commit -m bye-life life
+ "
+
+test_expect_success 'change file but in unrelated area' "
+ test x\"\$(sed -n -e 4p < file)\" = x4 &&
+ test x\"\$(sed -n -e 7p < file)\" = x7 &&
+ perl -i.bak -p -e 's/^4\$/4444/' file &&
+ perl -i.bak -p -e 's/^7\$/7777/' file &&
+ test x\"\$(sed -n -e 4p < file)\" = x4444 &&
+ test x\"\$(sed -n -e 7p < file)\" = x7777 &&
+ git commit -m '4 => 4444, 7 => 7777' file &&
+ git svn dcommit &&
+ svn_cmd up tmp &&
+ cd tmp &&
+ test x\"\$(sed -n -e 4p < file)\" = x4444 &&
+ test x\"\$(sed -n -e 7p < file)\" = x7777 &&
+ test x\"\$(sed -n -e 58p < file)\" = x5588 &&
+ test x\"\$(sed -n -e 61p < file)\" = x6611
+ "
+
+test_expect_success 'attempt to dcommit with a dirty index' '
+ echo foo >>file &&
+ git add file &&
+ test_must_fail git svn dcommit
+'
+
+test_done
diff --git a/t/t9138-git-svn-authors-prog.sh b/t/t9138-git-svn-authors-prog.sh
new file mode 100755
index 0000000..784ec7f
--- /dev/null
+++ b/t/t9138-git-svn-authors-prog.sh
@@ -0,0 +1,107 @@
+#!/bin/sh
+#
+# Copyright (c) 2009 Eric Wong, Mark Lodato
+#
+
+test_description='git svn authors prog tests'
+
+. ./lib-git-svn.sh
+
+write_script svn-authors-prog "$PERL_PATH" <<-\EOF
+ $_ = shift;
+ if (s/-hermit//) {
+ print "$_ <>\n";
+ } elsif (s/-sub$//) {
+ print "$_ <$_\@sub.example.com>\n";
+ } else {
+ print "$_ <$_\@example.com>\n";
+ }
+EOF
+
+test_expect_success 'svn-authors setup' '
+ cat >svn-authors <<-\EOF
+ ff = FFFFFFF FFFFFFF <fFf@other.example.com>
+ EOF
+'
+
+test_expect_success 'setup svnrepo' '
+ for i in aa bb cc-sub dd-sub ee-foo ff
+ do
+ svn mkdir -m $i --username $i "$svnrepo"/$i || return 1
+ done
+'
+
+test_expect_success 'import authors with prog and file' '
+ git svn clone --authors-prog=./svn-authors-prog \
+ --authors-file=svn-authors "$svnrepo" x
+'
+
+test_expect_success 'imported 6 revisions successfully' '
+ (
+ cd x &&
+ git rev-list refs/remotes/git-svn >actual &&
+ test_line_count = 6 actual
+ )
+'
+
+test_expect_success 'authors-prog ran correctly' '
+ (
+ cd x &&
+ git rev-list -1 --pretty=raw refs/remotes/git-svn~1 >actual &&
+ grep "^author ee-foo <ee-foo@example\.com> " actual &&
+ git rev-list -1 --pretty=raw refs/remotes/git-svn~2 >actual &&
+ grep "^author dd <dd@sub\.example\.com> " actual &&
+ git rev-list -1 --pretty=raw refs/remotes/git-svn~3 >actual &&
+ grep "^author cc <cc@sub\.example\.com> " actual &&
+ git rev-list -1 --pretty=raw refs/remotes/git-svn~4 >actual &&
+ grep "^author bb <bb@example\.com> " actual &&
+ git rev-list -1 --pretty=raw refs/remotes/git-svn~5 >actual &&
+ grep "^author aa <aa@example\.com> " actual
+ )
+'
+
+test_expect_success 'authors-file overrode authors-prog' '
+ (
+ cd x &&
+ git rev-list -1 --pretty=raw refs/remotes/git-svn >actual &&
+ grep "^author FFFFFFF FFFFFFF <fFf@other\.example\.com> " actual
+ )
+'
+
+git --git-dir=x/.git config --unset svn.authorsfile
+git --git-dir=x/.git config --unset svn.authorsprog
+
+test_expect_success 'authors-prog imported user without email' '
+ svn mkdir -m gg --username gg-hermit "$svnrepo"/gg &&
+ (
+ cd x &&
+ git svn fetch --authors-prog=../svn-authors-prog &&
+ git rev-list -1 --pretty=raw refs/remotes/git-svn | \
+ grep "^author gg <> "
+ )
+'
+
+test_expect_success 'imported without authors-prog and authors-file' '
+ svn mkdir -m hh --username hh "$svnrepo"/hh &&
+ (
+ uuid=$(svn info "$svnrepo" |
+ sed -n "s/^Repository UUID: //p") &&
+ cd x &&
+ git svn fetch &&
+ git rev-list -1 --pretty=raw refs/remotes/git-svn | \
+ grep "^author hh <hh@$uuid> "
+ )
+'
+
+test_expect_success 'authors-prog handled special characters in username' '
+ svn mkdir -m bad --username "xyz; touch evil" "$svnrepo"/bad &&
+ (
+ cd x &&
+ git svn --authors-prog=../svn-authors-prog fetch &&
+ git rev-list -1 --pretty=raw refs/remotes/git-svn >actual &&
+ grep "^author xyz; touch evil <xyz; touch evil@example\.com> " actual &&
+ ! test -f evil
+ )
+'
+
+test_done
diff --git a/t/t9139-git-svn-non-utf8-commitencoding.sh b/t/t9139-git-svn-non-utf8-commitencoding.sh
new file mode 100755
index 0000000..b7f756b
--- /dev/null
+++ b/t/t9139-git-svn-non-utf8-commitencoding.sh
@@ -0,0 +1,48 @@
+#!/bin/sh
+#
+# Copyright (c) 2009 Eric Wong
+
+test_description='git svn refuses to dcommit non-UTF8 messages'
+
+TEST_FAILS_SANITIZE_LEAK=true
+. ./lib-git-svn.sh
+
+# ISO-2022-JP can pass for valid UTF-8, so skipping that in this test
+
+for H in ISO8859-1 eucJP
+do
+ test_expect_success "$H setup" '
+ mkdir $H &&
+ svn_cmd import -m "$H test" $H "$svnrepo"/$H &&
+ git svn clone "$svnrepo"/$H $H
+ '
+done
+
+for H in ISO8859-1 eucJP
+do
+ test_expect_success "$H commit on git side" '
+ (
+ cd $H &&
+ git config i18n.commitencoding $H &&
+ git checkout -b t refs/remotes/git-svn &&
+ echo $H >F &&
+ git add F &&
+ git commit -a -F "$TEST_DIRECTORY"/t3900/$H.txt &&
+ E=$(git cat-file commit HEAD | sed -ne "s/^encoding //p") &&
+ test "z$E" = "z$H"
+ )
+ '
+done
+
+for H in ISO8859-1 eucJP
+do
+ test_expect_success "$H dcommit to svn" '
+ (
+ cd $H &&
+ git config --unset i18n.commitencoding &&
+ test_must_fail git svn dcommit
+ )
+ '
+done
+
+test_done
diff --git a/t/t9140-git-svn-reset.sh b/t/t9140-git-svn-reset.sh
new file mode 100755
index 0000000..a420b2a
--- /dev/null
+++ b/t/t9140-git-svn-reset.sh
@@ -0,0 +1,66 @@
+#!/bin/sh
+#
+# Copyright (c) 2009 Ben Jackson
+#
+
+test_description='git svn reset'
+. ./lib-git-svn.sh
+
+test_expect_success 'setup test repository' '
+ svn_cmd co "$svnrepo" s &&
+ (
+ cd s &&
+ mkdir vis &&
+ echo always visible > vis/vis.txt &&
+ svn_cmd add vis &&
+ svn_cmd commit -m "create visible files" &&
+ mkdir hid &&
+ echo initially hidden > hid/hid.txt &&
+ svn_cmd add hid &&
+ svn_cmd commit -m "create initially hidden files" &&
+ svn_cmd up &&
+ echo mod >> vis/vis.txt &&
+ svn_cmd commit -m "modify vis" &&
+ svn_cmd up
+ )
+'
+
+test_expect_success 'clone SVN repository with hidden directory' '
+ git svn init "$svnrepo" g &&
+ ( cd g && git svn fetch --ignore-paths="^hid" )
+'
+
+test_expect_success 'modify hidden file in SVN repo' '
+ ( cd s &&
+ echo mod hidden >> hid/hid.txt &&
+ svn_cmd commit -m "modify hid" &&
+ svn_cmd up
+ )
+'
+
+test_expect_success 'fetch fails on modified hidden file' '
+ ( cd g &&
+ git svn find-rev refs/remotes/git-svn > ../expect &&
+ test_must_fail git svn fetch 2> ../errors &&
+ git svn find-rev refs/remotes/git-svn > ../expect2 ) &&
+ grep "not found in commit" errors &&
+ test_cmp expect expect2
+'
+
+test_expect_success 'reset unwinds back to r1' '
+ ( cd g &&
+ git svn reset -r1 &&
+ git svn find-rev refs/remotes/git-svn > ../expect2 ) &&
+ echo 1 >expect &&
+ test_cmp expect expect2
+'
+
+test_expect_success 'refetch succeeds not ignoring any files' '
+ ( cd g &&
+ git svn fetch &&
+ git svn rebase &&
+ grep "mod hidden" hid/hid.txt
+ )
+'
+
+test_done
diff --git a/t/t9141-git-svn-multiple-branches.sh b/t/t9141-git-svn-multiple-branches.sh
new file mode 100755
index 0000000..bf168a3
--- /dev/null
+++ b/t/t9141-git-svn-multiple-branches.sh
@@ -0,0 +1,122 @@
+#!/bin/sh
+#
+# Copyright (c) 2009 Marc Branchaud
+#
+
+test_description='git svn multiple branch and tag paths in the svn repo'
+. ./lib-git-svn.sh
+
+test_expect_success 'setup svnrepo' '
+ mkdir project \
+ project/trunk \
+ project/b_one \
+ project/b_two \
+ project/tags_A \
+ project/tags_B &&
+ echo 1 > project/trunk/a.file &&
+ svn_cmd import -m "$test_description" project "$svnrepo/project" &&
+ rm -rf project &&
+ svn_cmd cp -m "Branch 1" "$svnrepo/project/trunk" \
+ "$svnrepo/project/b_one/first" &&
+ svn_cmd cp -m "Tag 1" "$svnrepo/project/trunk" \
+ "$svnrepo/project/tags_A/1.0" &&
+ svn_cmd co "$svnrepo/project" svn_project &&
+ ( cd svn_project &&
+ echo 2 > trunk/a.file &&
+ svn_cmd ci -m "Change 1" trunk/a.file &&
+ svn_cmd cp -m "Branch 2" "$svnrepo/project/trunk" \
+ "$svnrepo/project/b_one/second" &&
+ svn_cmd cp -m "Tag 2" "$svnrepo/project/trunk" \
+ "$svnrepo/project/tags_A/2.0" &&
+ echo 3 > trunk/a.file &&
+ svn_cmd ci -m "Change 2" trunk/a.file &&
+ svn_cmd cp -m "Branch 3" "$svnrepo/project/trunk" \
+ "$svnrepo/project/b_two/1" &&
+ svn_cmd cp -m "Tag 3" "$svnrepo/project/trunk" \
+ "$svnrepo/project/tags_A/3.0" &&
+ echo 4 > trunk/a.file &&
+ svn_cmd ci -m "Change 3" trunk/a.file &&
+ svn_cmd cp -m "Branch 4" "$svnrepo/project/trunk" \
+ "$svnrepo/project/b_two/2" &&
+ svn_cmd cp -m "Tag 4" "$svnrepo/project/trunk" \
+ "$svnrepo/project/tags_A/4.0" &&
+ svn_cmd up &&
+ echo 5 > b_one/first/a.file &&
+ svn_cmd ci -m "Change 4" b_one/first/a.file &&
+ svn_cmd cp -m "Tag 5" "$svnrepo/project/b_one/first" \
+ "$svnrepo/project/tags_B/v5" &&
+ echo 6 > b_one/second/a.file &&
+ svn_cmd ci -m "Change 5" b_one/second/a.file &&
+ svn_cmd cp -m "Tag 6" "$svnrepo/project/b_one/second" \
+ "$svnrepo/project/tags_B/v6" &&
+ echo 7 > b_two/1/a.file &&
+ svn_cmd ci -m "Change 6" b_two/1/a.file &&
+ svn_cmd cp -m "Tag 7" "$svnrepo/project/b_two/1" \
+ "$svnrepo/project/tags_B/v7" &&
+ echo 8 > b_two/2/a.file &&
+ svn_cmd ci -m "Change 7" b_two/2/a.file &&
+ svn_cmd cp -m "Tag 8" "$svnrepo/project/b_two/2" \
+ "$svnrepo/project/tags_B/v8"
+ )
+'
+
+test_expect_success 'clone multiple branch and tag paths' '
+ git svn clone -T trunk \
+ -b b_one/* --branches b_two/* \
+ -t tags_A/* --tags tags_B \
+ "$svnrepo/project" git_project &&
+ ( cd git_project &&
+ git rev-parse refs/remotes/origin/first &&
+ git rev-parse refs/remotes/origin/second &&
+ git rev-parse refs/remotes/origin/1 &&
+ git rev-parse refs/remotes/origin/2 &&
+ git rev-parse refs/remotes/origin/tags/1.0 &&
+ git rev-parse refs/remotes/origin/tags/2.0 &&
+ git rev-parse refs/remotes/origin/tags/3.0 &&
+ git rev-parse refs/remotes/origin/tags/4.0 &&
+ git rev-parse refs/remotes/origin/tags/v5 &&
+ git rev-parse refs/remotes/origin/tags/v6 &&
+ git rev-parse refs/remotes/origin/tags/v7 &&
+ git rev-parse refs/remotes/origin/tags/v8
+ )
+'
+
+test_expect_success 'Multiple branch or tag paths require -d' '
+ ( cd git_project &&
+ test_must_fail git svn branch -m "No new branch" Nope &&
+ test_must_fail git svn tag -m "No new tag" Tagless &&
+ test_must_fail git rev-parse refs/remotes/origin/Nope &&
+ test_must_fail git rev-parse refs/remotes/origin/tags/Tagless
+ ) &&
+ ( cd svn_project &&
+ svn_cmd up &&
+ test_path_is_missing b_one/Nope &&
+ test_path_is_missing b_two/Nope &&
+ test_path_is_missing tags_A/Tagless &&
+ test_path_is_missing tags_B/Tagless
+ )
+'
+
+test_expect_success 'create new branches and tags' '
+ ( cd git_project &&
+ git svn branch -m "New branch 1" -d b_one New1 ) &&
+ ( cd svn_project &&
+ svn_cmd up && test -e b_one/New1/a.file ) &&
+
+ ( cd git_project &&
+ git svn branch -m "New branch 2" -d b_two New2 ) &&
+ ( cd svn_project &&
+ svn_cmd up && test -e b_two/New2/a.file ) &&
+
+ ( cd git_project &&
+ git svn branch -t -m "New tag 1" -d tags_A Tag1 ) &&
+ ( cd svn_project &&
+ svn_cmd up && test -e tags_A/Tag1/a.file ) &&
+
+ ( cd git_project &&
+ git svn tag -m "New tag 2" -d tags_B Tag2 ) &&
+ ( cd svn_project &&
+ svn_cmd up && test -e tags_B/Tag2/a.file )
+'
+
+test_done
diff --git a/t/t9142-git-svn-shallow-clone.sh b/t/t9142-git-svn-shallow-clone.sh
new file mode 100755
index 0000000..a307305
--- /dev/null
+++ b/t/t9142-git-svn-shallow-clone.sh
@@ -0,0 +1,29 @@
+#!/bin/sh
+#
+# Copyright (c) 2009 Eric Wong
+#
+
+test_description='git svn shallow clone'
+. ./lib-git-svn.sh
+
+test_expect_success 'setup test repository' '
+ svn_cmd mkdir -m "create standard layout" \
+ "$svnrepo"/trunk "$svnrepo"/branches "$svnrepo"/tags &&
+ svn_cmd cp -m "branch off trunk" \
+ "$svnrepo"/trunk "$svnrepo"/branches/a &&
+ svn_cmd co "$svnrepo"/branches/a &&
+ (
+ cd a &&
+ > foo &&
+ svn_cmd add foo &&
+ svn_cmd commit -m "add foo"
+ ) &&
+ maybe_start_httpd
+'
+
+test_expect_success 'clone trunk with "-r HEAD"' '
+ git svn clone -r HEAD "$svnrepo/trunk" g &&
+ ( cd g && git rev-parse --symbolic --verify HEAD )
+'
+
+test_done
diff --git a/t/t9143-git-svn-gc.sh b/t/t9143-git-svn-gc.sh
new file mode 100755
index 0000000..4594e1a
--- /dev/null
+++ b/t/t9143-git-svn-gc.sh
@@ -0,0 +1,51 @@
+#!/bin/sh
+#
+# Copyright (c) 2009 Robert Allan Zeh
+
+test_description='git svn gc basic tests'
+
+. ./lib-git-svn.sh
+
+test_expect_success 'setup directories and test repo' '
+ mkdir import &&
+ mkdir tmp &&
+ echo "Sample text for Subversion repository." > import/test.txt &&
+ svn_cmd import -m "import for git svn" import "$svnrepo" > /dev/null
+ '
+
+test_expect_success 'checkout working copy from svn' \
+ 'svn_cmd co "$svnrepo" test_wc'
+
+test_expect_success 'set some properties to create an unhandled.log file' '
+ (
+ cd test_wc &&
+ svn_cmd propset foo bar test.txt &&
+ svn_cmd commit -m "property set"
+ )'
+
+test_expect_success 'Setup repo' 'git svn init "$svnrepo"'
+
+test_expect_success 'Fetch repo' 'git svn fetch'
+
+test_expect_success 'make backup copy of unhandled.log' '
+ cp .git/svn/refs/remotes/git-svn/unhandled.log tmp
+ '
+
+test_expect_success 'create leftover index' '> .git/svn/refs/remotes/git-svn/index'
+
+test_expect_success 'git svn gc runs' 'git svn gc'
+
+test_expect_success 'git svn index removed' '! test -f .git/svn/refs/remotes/git-svn/index'
+
+if test -r .git/svn/refs/remotes/git-svn/unhandled.log.gz
+then
+ test_expect_success 'git svn gc produces a valid gzip file' '
+ gunzip .git/svn/refs/remotes/git-svn/unhandled.log.gz
+ '
+fi
+
+test_expect_success 'git svn gc does not change unhandled.log files' '
+ test_cmp .git/svn/refs/remotes/git-svn/unhandled.log tmp/unhandled.log
+ '
+
+test_done
diff --git a/t/t9144-git-svn-old-rev_map.sh b/t/t9144-git-svn-old-rev_map.sh
new file mode 100755
index 0000000..7600a35
--- /dev/null
+++ b/t/t9144-git-svn-old-rev_map.sh
@@ -0,0 +1,31 @@
+#!/bin/sh
+#
+# Copyright (c) 2009 Eric Wong
+
+test_description='git svn old rev_map preservd'
+. ./lib-git-svn.sh
+
+test_expect_success 'setup test repository with old layout' '
+ mkdir i &&
+ (cd i && > a) &&
+ svn_cmd import -m- i "$svnrepo" &&
+ git svn init "$svnrepo" &&
+ git svn fetch &&
+ test -d .git/svn/refs/remotes/git-svn/ &&
+ ! test -e .git/svn/git-svn/ &&
+ mv .git/svn/refs/remotes/git-svn .git/svn/ &&
+ rm -r .git/svn/refs
+'
+
+test_expect_success 'old layout continues to work' '
+ svn_cmd import -m- i "$svnrepo/b" &&
+ git svn rebase &&
+ echo a >> b/a &&
+ git add b/a &&
+ git commit -m- -a &&
+ git svn dcommit &&
+ ! test -d .git/svn/refs/ &&
+ test -e .git/svn/git-svn/
+'
+
+test_done
diff --git a/t/t9145-git-svn-master-branch.sh b/t/t9145-git-svn-master-branch.sh
new file mode 100755
index 0000000..f863e43
--- /dev/null
+++ b/t/t9145-git-svn-master-branch.sh
@@ -0,0 +1,28 @@
+#!/bin/sh
+#
+# Copyright (c) 2009 Eric Wong
+#
+test_description='git svn initial main branch is "trunk" if possible'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./lib-git-svn.sh
+
+test_expect_success 'setup test repository' '
+ mkdir i &&
+ > i/a &&
+ svn_cmd import -m trunk i "$svnrepo/trunk" &&
+ svn_cmd import -m b/a i "$svnrepo/branches/a" &&
+ svn_cmd import -m b/b i "$svnrepo/branches/b"
+'
+
+test_expect_success 'git svn clone --stdlayout sets up trunk as main' '
+ git svn clone -s "$svnrepo" g &&
+ (
+ cd g &&
+ test x$(git rev-parse --verify refs/remotes/origin/trunk^0) = \
+ x$(git rev-parse --verify refs/heads/main^0)
+ )
+'
+
+test_done
diff --git a/t/t9146-git-svn-empty-dirs.sh b/t/t9146-git-svn-empty-dirs.sh
new file mode 100755
index 0000000..09606f1
--- /dev/null
+++ b/t/t9146-git-svn-empty-dirs.sh
@@ -0,0 +1,160 @@
+#!/bin/sh
+#
+# Copyright (c) 2009 Eric Wong
+
+test_description='git svn creates empty directories'
+
+. ./lib-git-svn.sh
+
+test_expect_success 'initialize repo' '
+ for i in a b c d d/e d/e/f "weird file name"
+ do
+ svn_cmd mkdir -m "mkdir $i" "$svnrepo"/"$i" || return 1
+ done
+'
+
+test_expect_success 'clone' 'git svn clone "$svnrepo" cloned'
+
+test_expect_success 'empty directories exist' '
+ (
+ cd cloned &&
+ for i in a b c d d/e d/e/f "weird file name"
+ do
+ if ! test -d "$i"
+ then
+ echo >&2 "$i does not exist" &&
+ exit 1
+ fi
+ done
+ )
+'
+
+test_expect_success 'option automkdirs set to false' '
+ (
+ git svn init "$svnrepo" cloned-no-mkdirs &&
+ cd cloned-no-mkdirs &&
+ git config svn-remote.svn.automkdirs false &&
+ git svn fetch &&
+ for i in a b c d d/e d/e/f "weird file name"
+ do
+ if test -d "$i"
+ then
+ echo >&2 "$i exists" &&
+ exit 1
+ fi
+ done
+ )
+'
+
+test_expect_success 'more emptiness' '
+ svn_cmd mkdir -m "bang bang" "$svnrepo"/"! !"
+'
+
+test_expect_success 'git svn rebase creates empty directory' '
+ ( cd cloned && git svn rebase ) &&
+ test -d cloned/"! !"
+'
+
+test_expect_success 'git svn mkdirs recreates empty directories' '
+ (
+ cd cloned &&
+ rm -r * &&
+ git svn mkdirs &&
+ for i in a b c d d/e d/e/f "weird file name" "! !"
+ do
+ if ! test -d "$i"
+ then
+ echo >&2 "$i does not exist" &&
+ exit 1
+ fi
+ done
+ )
+'
+
+test_expect_success 'git svn mkdirs -r works' '
+ (
+ cd cloned &&
+ rm -r * &&
+ git svn mkdirs -r7 &&
+ for i in a b c d d/e d/e/f "weird file name"
+ do
+ if ! test -d "$i"
+ then
+ echo >&2 "$i does not exist" &&
+ exit 1
+ fi
+ done &&
+
+ if test -d "! !"
+ then
+ echo >&2 "$i should not exist" &&
+ exit 1
+ fi &&
+
+ git svn mkdirs -r8 &&
+ if ! test -d "! !"
+ then
+ echo >&2 "$i not exist" &&
+ exit 1
+ fi
+ )
+'
+
+test_expect_success 'initialize trunk' '
+ for i in trunk trunk/a trunk/"weird file name"
+ do
+ svn_cmd mkdir -m "mkdir $i" "$svnrepo"/"$i" || return 1
+ done
+'
+
+test_expect_success 'clone trunk' 'git svn clone -s "$svnrepo" trunk'
+
+test_expect_success 'empty directories in trunk exist' '
+ (
+ cd trunk &&
+ for i in a "weird file name"
+ do
+ if ! test -d "$i"
+ then
+ echo >&2 "$i does not exist" &&
+ exit 1
+ fi
+ done
+ )
+'
+
+test_expect_success 'remove a top-level directory from svn' '
+ svn_cmd rm -m "remove d" "$svnrepo"/d
+'
+
+test_expect_success 'removed top-level directory does not exist' '
+ git svn clone "$svnrepo" removed &&
+ test ! -e removed/d
+
+'
+unhandled=.git/svn/refs/remotes/git-svn/unhandled.log
+test_expect_success 'git svn gc-ed files work' '
+ (
+ cd removed &&
+ git svn gc &&
+ : Compress::Zlib may not be available &&
+ if test -f "$unhandled".gz
+ then
+ svn_cmd mkdir -m gz "$svnrepo"/gz &&
+ git reset --hard $(git rev-list HEAD | tail -1) &&
+ git svn rebase &&
+ test -f "$unhandled".gz &&
+ test -f "$unhandled" &&
+ for i in a b c "weird file name" gz "! !"
+ do
+ if ! test -d "$i"
+ then
+ echo >&2 "$i does not exist" &&
+ exit 1
+ fi
+ done
+ fi
+ )
+'
+
+test_done
diff --git a/t/t9147-git-svn-include-paths.sh b/t/t9147-git-svn-include-paths.sh
new file mode 100755
index 0000000..63fa0b6
--- /dev/null
+++ b/t/t9147-git-svn-include-paths.sh
@@ -0,0 +1,149 @@
+#!/bin/sh
+#
+# Copyright (c) 2013 Paul Walmsley - based on t9134 by Vitaly Shukela
+#
+
+test_description='git svn property tests'
+. ./lib-git-svn.sh
+
+test_expect_success 'setup test repository' '
+ svn_cmd co "$svnrepo" s &&
+ (
+ cd s &&
+ mkdir qqq www xxx &&
+ echo test_qqq > qqq/test_qqq.txt &&
+ echo test_www > www/test_www.txt &&
+ echo test_xxx > xxx/test_xxx.txt &&
+ svn_cmd add qqq &&
+ svn_cmd add www &&
+ svn_cmd add xxx &&
+ svn_cmd commit -m "create some files" &&
+ svn_cmd up &&
+ echo hi >> www/test_www.txt &&
+ svn_cmd commit -m "modify www/test_www.txt" &&
+ svn_cmd up
+ )
+'
+
+test_expect_success 'clone an SVN repository with filter to include qqq directory' '
+ git svn clone --include-paths="qqq" "$svnrepo" g &&
+ echo test_qqq > expect &&
+ for i in g/*/*.txt; do cat $i >> expect2 || return 1; done &&
+ test_cmp expect expect2
+'
+
+
+test_expect_success 'init+fetch an SVN repository with included qqq directory' '
+ git svn init "$svnrepo" c &&
+ ( cd c && git svn fetch --include-paths="qqq" ) &&
+ rm expect2 &&
+ echo test_qqq > expect &&
+ for i in c/*/*.txt; do cat $i >> expect2 || return 1; done &&
+ test_cmp expect expect2
+'
+
+test_expect_success 'verify include-paths config saved by clone' '
+ (
+ cd g &&
+ git config --get svn-remote.svn.include-paths | grep qqq
+ )
+'
+
+test_expect_success 'SVN-side change outside of www' '
+ (
+ cd s &&
+ echo b >> qqq/test_qqq.txt &&
+ svn_cmd commit -m "SVN-side change outside of www" &&
+ svn_cmd up &&
+ svn_cmd log -v | grep "SVN-side change outside of www"
+ )
+'
+
+test_expect_success 'update git svn-cloned repo (config include)' '
+ (
+ cd g &&
+ git svn rebase &&
+ printf "test_qqq\nb\n" > expect &&
+ for i in */*.txt; do cat $i >> expect2 || exit 1; done &&
+ test_cmp expect2 expect &&
+ rm expect expect2
+ )
+'
+
+test_expect_success 'update git svn-cloned repo (option include)' '
+ (
+ cd c &&
+ git svn rebase --include-paths="qqq" &&
+ printf "test_qqq\nb\n" > expect &&
+ for i in */*.txt; do cat $i >> expect2 || exit 1; done &&
+ test_cmp expect2 expect &&
+ rm expect expect2
+ )
+'
+
+test_expect_success 'SVN-side change inside of ignored www' '
+ (
+ cd s &&
+ echo zaq >> www/test_www.txt &&
+ svn_cmd commit -m "SVN-side change inside of www/test_www.txt" &&
+ svn_cmd up &&
+ svn_cmd log -v | grep "SVN-side change inside of www/test_www.txt"
+ )
+'
+
+test_expect_success 'update git svn-cloned repo (config include)' '
+ (
+ cd g &&
+ git svn rebase &&
+ printf "test_qqq\nb\n" > expect &&
+ for i in */*.txt; do cat $i >> expect2 || exit 1; done &&
+ test_cmp expect2 expect &&
+ rm expect expect2
+ )
+'
+
+test_expect_success 'update git svn-cloned repo (option include)' '
+ (
+ cd c &&
+ git svn rebase --include-paths="qqq" &&
+ printf "test_qqq\nb\n" > expect &&
+ for i in */*.txt; do cat $i >> expect2 || exit 1; done &&
+ test_cmp expect2 expect &&
+ rm expect expect2
+ )
+'
+
+test_expect_success 'SVN-side change in and out of included qqq' '
+ (
+ cd s &&
+ echo cvf >> www/test_www.txt &&
+ echo ygg >> qqq/test_qqq.txt &&
+ svn_cmd commit -m "SVN-side change in and out of ignored www" &&
+ svn_cmd up &&
+ svn_cmd log -v | grep "SVN-side change in and out of ignored www"
+ )
+'
+
+test_expect_success 'update git svn-cloned repo again (config include)' '
+ (
+ cd g &&
+ git svn rebase &&
+ printf "test_qqq\nb\nygg\n" > expect &&
+ for i in */*.txt; do cat $i >> expect2 || exit 1; done &&
+ test_cmp expect2 expect &&
+ rm expect expect2
+ )
+'
+
+test_expect_success 'update git svn-cloned repo again (option include)' '
+ (
+ cd c &&
+ git svn rebase --include-paths="qqq" &&
+ printf "test_qqq\nb\nygg\n" > expect &&
+ for i in */*.txt; do cat $i >> expect2 || exit 1; done &&
+ test_cmp expect2 expect &&
+ rm expect expect2
+ )
+'
+
+test_done
diff --git a/t/t9148-git-svn-propset.sh b/t/t9148-git-svn-propset.sh
new file mode 100755
index 0000000..aebb289
--- /dev/null
+++ b/t/t9148-git-svn-propset.sh
@@ -0,0 +1,98 @@
+#!/bin/sh
+#
+# Copyright (c) 2014 Alfred Perlstein
+#
+
+test_description='git svn propset tests'
+
+. ./lib-git-svn.sh
+
+test_expect_success 'setup propset via import' '
+ test_when_finished "rm -rf import" &&
+
+ foo_subdir2="subdir/subdir2/foo_subdir2" &&
+ mkdir -p import/subdir/subdir2 &&
+ (
+ cd import &&
+ # for "add props top level"
+ >foo &&
+ # for "add props relative"
+ >subdir/foo_subdir &&
+ # for "add props subdir"
+ >"$foo_subdir2" &&
+ svn_cmd import -m "import for git svn" . "$svnrepo"
+ )
+'
+
+test_expect_success 'initialize git svn' '
+ git svn init "$svnrepo"
+ '
+
+test_expect_success 'fetch revisions from svn' '
+ git svn fetch
+ '
+
+set_props () {
+ subdir="$1"
+ file="$2"
+ shift;shift;
+ (cd "$subdir" &&
+ while [ $# -gt 0 ] ; do
+ git svn propset "$1" "$2" "$file" || exit 1
+ shift;shift;
+ done &&
+ echo hello >> "$file" &&
+ git commit -m "testing propset" "$file")
+}
+
+confirm_props () {
+ subdir="$1"
+ file="$2"
+ shift;shift;
+ (set -e ; cd "svn_project/$subdir" &&
+ while [ $# -gt 0 ] ; do
+ test "$(svn_cmd propget "$1" "$file")" = "$2" || exit 1
+ shift;shift;
+ done)
+}
+
+
+#The current implementation has a restriction:
+#svn propset will be taken as a delta for svn dcommit only
+#if the file content is also modified
+test_expect_success 'add props top level' '
+ set_props "." "foo" "svn:keywords" "FreeBSD=%H" &&
+ git svn dcommit &&
+ svn_cmd co "$svnrepo" svn_project &&
+ confirm_props "." "foo" "svn:keywords" "FreeBSD=%H" &&
+ rm -rf svn_project
+ '
+
+test_expect_success 'add multiple props' '
+ set_props "." "foo" \
+ "svn:keywords" "FreeBSD=%H" fbsd:nokeywords yes &&
+ git svn dcommit &&
+ svn_cmd co "$svnrepo" svn_project &&
+ confirm_props "." "foo" \
+ "svn:keywords" "FreeBSD=%H" fbsd:nokeywords yes &&
+ rm -rf svn_project
+ '
+
+test_expect_success 'add props subdir' '
+ set_props "." "$foo_subdir2" svn:keywords "FreeBSD=%H" &&
+ git svn dcommit &&
+ svn_cmd co "$svnrepo" svn_project &&
+ confirm_props "." "$foo_subdir2" "svn:keywords" "FreeBSD=%H" &&
+ rm -rf svn_project
+ '
+
+test_expect_success 'add props relative' '
+ set_props "subdir/subdir2" "../foo_subdir" \
+ svn:keywords "FreeBSD=%H" &&
+ git svn dcommit &&
+ svn_cmd co "$svnrepo" svn_project &&
+ confirm_props "subdir/subdir2" "../foo_subdir" \
+ svn:keywords "FreeBSD=%H" &&
+ rm -rf svn_project
+ '
+test_done
diff --git a/t/t9150-svk-mergetickets.sh b/t/t9150-svk-mergetickets.sh
new file mode 100755
index 0000000..1bb676b
--- /dev/null
+++ b/t/t9150-svk-mergetickets.sh
@@ -0,0 +1,25 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Sam Vilain
+#
+
+test_description='git-svn svk merge tickets'
+
+. ./lib-git-svn.sh
+
+test_expect_success 'load svk depot' "
+ svnadmin load -q '$rawsvnrepo' \
+ < '$TEST_DIRECTORY/t9150/svk-merge.dump' &&
+ git svn init --minimize-url -R svkmerge \
+ --rewrite-root=http://svn.example.org \
+ -T trunk -b branches '$svnrepo' &&
+ git svn fetch --all
+ "
+
+uuid=b48289b2-9c08-4d72-af37-0358a40b9c15
+
+test_expect_success 'svk merges were represented coming in' "
+ [ $(git cat-file commit HEAD | grep parent | wc -l) -eq 2 ]
+ "
+
+test_done
diff --git a/t/t9150/make-svk-dump b/t/t9150/make-svk-dump
new file mode 100755
index 0000000..2242f14
--- /dev/null
+++ b/t/t9150/make-svk-dump
@@ -0,0 +1,57 @@
+#!/bin/sh
+#
+# this script sets up a Subversion repository for Makefile in the
+# first ever git merge, as if it were done with svk.
+#
+
+set -e
+
+svk depotmap foo ~/.svk/foo
+svk co /foo/ foo
+cd foo
+mkdir trunk
+mkdir branches
+svk add trunk branches
+svk commit -m "Setup trunk and branches"
+cd trunk
+
+git cat-file blob 6683463e:Makefile > Makefile
+svk add Makefile
+
+svk commit -m "ancestor"
+cd ..
+svk cp trunk branches/left
+
+svk commit -m "make left branch"
+cd branches/left/
+
+git cat-file blob 5873b67e:Makefile > Makefile
+svk commit -m "left update 1"
+
+cd ../../trunk
+git cat-file blob 75118b13:Makefile > Makefile
+svk commit -m "trunk update"
+
+cd ../branches/left
+git cat-file blob b5039db6:Makefile > Makefile
+svk commit -m "left update 2"
+
+cd ../../trunk
+svk sm /foo/branches/left
+# in theory we could delete the "left" branch here, but it's not
+# required so don't do it, in case people start getting ideas ;)
+svk commit -m "merge branch 'left' into 'trunk'"
+
+git cat-file blob b51ad431:Makefile > Makefile
+
+svk diff Makefile && echo "Hey! No differences, magic"
+
+cd ../..
+
+svnadmin dump ~/.svk/foo > svk-merge.dump
+
+svk co -d foo
+rm -rf foo
+svk depotmap -d /foo/
+rm -rf ~/.svk/foo
+
diff --git a/t/t9150/svk-merge.dump b/t/t9150/svk-merge.dump
new file mode 100644
index 0000000..42f70db
--- /dev/null
+++ b/t/t9150/svk-merge.dump
@@ -0,0 +1,616 @@
+SVN-fs-dump-format-version: 2
+
+UUID: b48289b2-9c08-4d72-af37-0358a40b9c15
+
+Revision-number: 0
+Prop-content-length: 56
+Content-length: 56
+
+K 8
+svn:date
+V 27
+2009-10-19T23:44:03.722969Z
+PROPS-END
+
+Revision-number: 1
+Prop-content-length: 123
+Content-length: 123
+
+K 7
+svn:log
+V 24
+Setup trunk and branches
+K 10
+svn:author
+V 4
+samv
+K 8
+svn:date
+V 27
+2009-10-19T23:44:04.927533Z
+PROPS-END
+
+Node-path: branches
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Node-path: trunk
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Revision-number: 2
+Prop-content-length: 106
+Content-length: 106
+
+K 7
+svn:log
+V 8
+ancestor
+K 10
+svn:author
+V 4
+samv
+K 8
+svn:date
+V 27
+2009-10-19T23:44:05.835585Z
+PROPS-END
+
+Node-path: trunk/Makefile
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 2401
+Text-content-md5: bfd8ff778d1492dc6758567373176a89
+Content-length: 2411
+
+PROPS-END
+# -DCOLLISION_CHECK if you believe that SHA1's
+# 1461501637330902918203684832716283019655932542976 hashes do not give you
+# enough guarantees about no collisions between objects ever hapenning.
+#
+# -DNSEC if you want git to care about sub-second file mtimes and ctimes.
+# Note that you need some new glibc (at least >2.2.4) for this, and it will
+# BREAK YOUR LOCAL DIFFS! show-diff and anything using it will likely randomly
+# break unless your underlying filesystem supports those sub-second times
+# (my ext3 doesn't).
+CFLAGS=-g -O3 -Wall
+
+CC=gcc
+
+
+PROG= update-cache show-diff init-db write-tree read-tree commit-tree \
+ cat-file fsck-cache checkout-cache diff-tree rev-tree show-files \
+ check-files ls-tree merge-base
+
+all: $(PROG)
+
+install: $(PROG)
+ install $(PROG) $(HOME)/bin/
+
+LIBS= -lssl -lz
+
+init-db: init-db.o
+
+update-cache: update-cache.o read-cache.o
+ $(CC) $(CFLAGS) -o update-cache update-cache.o read-cache.o $(LIBS)
+
+show-diff: show-diff.o read-cache.o
+ $(CC) $(CFLAGS) -o show-diff show-diff.o read-cache.o $(LIBS)
+
+write-tree: write-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o write-tree write-tree.o read-cache.o $(LIBS)
+
+read-tree: read-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o read-tree read-tree.o read-cache.o $(LIBS)
+
+commit-tree: commit-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o commit-tree commit-tree.o read-cache.o $(LIBS)
+
+cat-file: cat-file.o read-cache.o
+ $(CC) $(CFLAGS) -o cat-file cat-file.o read-cache.o $(LIBS)
+
+fsck-cache: fsck-cache.o read-cache.o
+ $(CC) $(CFLAGS) -o fsck-cache fsck-cache.o read-cache.o $(LIBS)
+
+checkout-cache: checkout-cache.o read-cache.o
+ $(CC) $(CFLAGS) -o checkout-cache checkout-cache.o read-cache.o $(LIBS)
+
+diff-tree: diff-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o diff-tree diff-tree.o read-cache.o $(LIBS)
+
+rev-tree: rev-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o rev-tree rev-tree.o read-cache.o $(LIBS)
+
+show-files: show-files.o read-cache.o
+ $(CC) $(CFLAGS) -o show-files show-files.o read-cache.o $(LIBS)
+
+check-files: check-files.o read-cache.o
+ $(CC) $(CFLAGS) -o check-files check-files.o read-cache.o $(LIBS)
+
+ls-tree: ls-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o ls-tree ls-tree.o read-cache.o $(LIBS)
+
+merge-base: merge-base.o read-cache.o
+ $(CC) $(CFLAGS) -o merge-base merge-base.o read-cache.o $(LIBS)
+
+read-cache.o: cache.h
+show-diff.o: cache.h
+
+clean:
+ rm -f *.o $(PROG)
+
+backup: clean
+ cd .. ; tar czvf dircache.tar.gz dir-cache
+
+
+Revision-number: 3
+Prop-content-length: 115
+Content-length: 115
+
+K 7
+svn:log
+V 16
+make left branch
+K 10
+svn:author
+V 4
+samv
+K 8
+svn:date
+V 27
+2009-10-19T23:44:06.719737Z
+PROPS-END
+
+Node-path: branches/left
+Node-kind: dir
+Node-action: add
+Node-copyfrom-rev: 2
+Node-copyfrom-path: trunk
+
+
+Revision-number: 4
+Prop-content-length: 112
+Content-length: 112
+
+K 7
+svn:log
+V 13
+left update 1
+K 10
+svn:author
+V 4
+samv
+K 8
+svn:date
+V 27
+2009-10-19T23:44:07.167666Z
+PROPS-END
+
+Node-path: branches/left/Makefile
+Node-kind: file
+Node-action: change
+Text-content-length: 2465
+Text-content-md5: 16e38d9753b061731650561ce01b1195
+Content-length: 2465
+
+# -DCOLLISION_CHECK if you believe that SHA1's
+# 1461501637330902918203684832716283019655932542976 hashes do not give you
+# enough guarantees about no collisions between objects ever hapenning.
+#
+# -DNSEC if you want git to care about sub-second file mtimes and ctimes.
+# Note that you need some new glibc (at least >2.2.4) for this, and it will
+# BREAK YOUR LOCAL DIFFS! show-diff and anything using it will likely randomly
+# break unless your underlying filesystem supports those sub-second times
+# (my ext3 doesn't).
+CFLAGS=-g -O3 -Wall
+
+CC=gcc
+
+
+PROG= update-cache show-diff init-db write-tree read-tree commit-tree \
+ cat-file fsck-cache checkout-cache diff-tree rev-tree show-files \
+ check-files ls-tree merge-base
+
+all: $(PROG)
+
+install: $(PROG)
+ install $(PROG) $(HOME)/bin/
+
+LIBS= -lssl -lz
+
+init-db: init-db.o
+
+update-cache: update-cache.o read-cache.o
+ $(CC) $(CFLAGS) -o update-cache update-cache.o read-cache.o $(LIBS)
+
+show-diff: show-diff.o read-cache.o
+ $(CC) $(CFLAGS) -o show-diff show-diff.o read-cache.o $(LIBS)
+
+write-tree: write-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o write-tree write-tree.o read-cache.o $(LIBS)
+
+read-tree: read-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o read-tree read-tree.o read-cache.o $(LIBS)
+
+commit-tree: commit-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o commit-tree commit-tree.o read-cache.o $(LIBS)
+
+cat-file: cat-file.o read-cache.o
+ $(CC) $(CFLAGS) -o cat-file cat-file.o read-cache.o $(LIBS)
+
+fsck-cache: fsck-cache.o read-cache.o
+ $(CC) $(CFLAGS) -o fsck-cache fsck-cache.o read-cache.o $(LIBS)
+
+checkout-cache: checkout-cache.o read-cache.o
+ $(CC) $(CFLAGS) -o checkout-cache checkout-cache.o read-cache.o $(LIBS)
+
+diff-tree: diff-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o diff-tree diff-tree.o read-cache.o $(LIBS)
+
+rev-tree: rev-tree.o read-cache.o object.o commit.o tree.o blob.o
+ $(CC) $(CFLAGS) -o rev-tree rev-tree.o read-cache.o object.o commit.o tree.o blob.o $(LIBS)
+
+show-files: show-files.o read-cache.o
+ $(CC) $(CFLAGS) -o show-files show-files.o read-cache.o $(LIBS)
+
+check-files: check-files.o read-cache.o
+ $(CC) $(CFLAGS) -o check-files check-files.o read-cache.o $(LIBS)
+
+ls-tree: ls-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o ls-tree ls-tree.o read-cache.o $(LIBS)
+
+merge-base: merge-base.o read-cache.o
+ $(CC) $(CFLAGS) -o merge-base merge-base.o read-cache.o $(LIBS)
+
+read-cache.o: cache.h
+show-diff.o: cache.h
+
+clean:
+ rm -f *.o $(PROG)
+
+backup: clean
+ cd .. ; tar czvf dircache.tar.gz dir-cache
+
+
+Revision-number: 5
+Prop-content-length: 111
+Content-length: 111
+
+K 7
+svn:log
+V 12
+trunk update
+K 10
+svn:author
+V 4
+samv
+K 8
+svn:date
+V 27
+2009-10-19T23:44:07.619633Z
+PROPS-END
+
+Node-path: trunk/Makefile
+Node-kind: file
+Node-action: change
+Text-content-length: 2521
+Text-content-md5: 0668418a621333f4aa8b6632cd63e2a0
+Content-length: 2521
+
+# -DCOLLISION_CHECK if you believe that SHA1's
+# 1461501637330902918203684832716283019655932542976 hashes do not give you
+# enough guarantees about no collisions between objects ever hapenning.
+#
+# -DNSEC if you want git to care about sub-second file mtimes and ctimes.
+# Note that you need some new glibc (at least >2.2.4) for this, and it will
+# BREAK YOUR LOCAL DIFFS! show-diff and anything using it will likely randomly
+# break unless your underlying filesystem supports those sub-second times
+# (my ext3 doesn't).
+CFLAGS=-g -O3 -Wall
+
+CC=gcc
+
+
+PROG= update-cache show-diff init-db write-tree read-tree commit-tree \
+ cat-file fsck-cache checkout-cache diff-tree rev-tree show-files \
+ check-files ls-tree merge-base merge-cache
+
+all: $(PROG)
+
+install: $(PROG)
+ install $(PROG) $(HOME)/bin/
+
+LIBS= -lssl -lz
+
+init-db: init-db.o
+
+update-cache: update-cache.o read-cache.o
+ $(CC) $(CFLAGS) -o update-cache update-cache.o read-cache.o $(LIBS)
+
+show-diff: show-diff.o read-cache.o
+ $(CC) $(CFLAGS) -o show-diff show-diff.o read-cache.o $(LIBS)
+
+write-tree: write-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o write-tree write-tree.o read-cache.o $(LIBS)
+
+read-tree: read-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o read-tree read-tree.o read-cache.o $(LIBS)
+
+commit-tree: commit-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o commit-tree commit-tree.o read-cache.o $(LIBS)
+
+cat-file: cat-file.o read-cache.o
+ $(CC) $(CFLAGS) -o cat-file cat-file.o read-cache.o $(LIBS)
+
+fsck-cache: fsck-cache.o read-cache.o
+ $(CC) $(CFLAGS) -o fsck-cache fsck-cache.o read-cache.o $(LIBS)
+
+checkout-cache: checkout-cache.o read-cache.o
+ $(CC) $(CFLAGS) -o checkout-cache checkout-cache.o read-cache.o $(LIBS)
+
+diff-tree: diff-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o diff-tree diff-tree.o read-cache.o $(LIBS)
+
+rev-tree: rev-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o rev-tree rev-tree.o read-cache.o $(LIBS)
+
+show-files: show-files.o read-cache.o
+ $(CC) $(CFLAGS) -o show-files show-files.o read-cache.o $(LIBS)
+
+check-files: check-files.o read-cache.o
+ $(CC) $(CFLAGS) -o check-files check-files.o read-cache.o $(LIBS)
+
+ls-tree: ls-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o ls-tree ls-tree.o read-cache.o $(LIBS)
+
+merge-base: merge-base.o read-cache.o
+ $(CC) $(CFLAGS) -o merge-base merge-base.o read-cache.o $(LIBS)
+
+merge-cache: merge-cache.o read-cache.o
+ $(CC) $(CFLAGS) -o merge-cache merge-cache.o read-cache.o $(LIBS)
+
+read-cache.o: cache.h
+show-diff.o: cache.h
+
+clean:
+ rm -f *.o $(PROG)
+
+backup: clean
+ cd .. ; tar czvf dircache.tar.gz dir-cache
+
+
+Revision-number: 6
+Prop-content-length: 112
+Content-length: 112
+
+K 7
+svn:log
+V 13
+left update 2
+K 10
+svn:author
+V 4
+samv
+K 8
+svn:date
+V 27
+2009-10-19T23:44:08.067554Z
+PROPS-END
+
+Node-path: branches/left/Makefile
+Node-kind: file
+Node-action: change
+Text-content-length: 2593
+Text-content-md5: 5ccff689fb290e00b85fe18ee50c54ba
+Content-length: 2593
+
+# -DCOLLISION_CHECK if you believe that SHA1's
+# 1461501637330902918203684832716283019655932542976 hashes do not give you
+# enough guarantees about no collisions between objects ever hapenning.
+#
+# -DNSEC if you want git to care about sub-second file mtimes and ctimes.
+# Note that you need some new glibc (at least >2.2.4) for this, and it will
+# BREAK YOUR LOCAL DIFFS! show-diff and anything using it will likely randomly
+# break unless your underlying filesystem supports those sub-second times
+# (my ext3 doesn't).
+CFLAGS=-g -O3 -Wall
+
+CC=gcc
+
+
+PROG= update-cache show-diff init-db write-tree read-tree commit-tree \
+ cat-file fsck-cache checkout-cache diff-tree rev-tree show-files \
+ check-files ls-tree merge-base
+
+all: $(PROG)
+
+install: $(PROG)
+ install $(PROG) $(HOME)/bin/
+
+LIBS= -lssl -lz
+
+init-db: init-db.o
+
+update-cache: update-cache.o read-cache.o
+ $(CC) $(CFLAGS) -o update-cache update-cache.o read-cache.o $(LIBS)
+
+show-diff: show-diff.o read-cache.o
+ $(CC) $(CFLAGS) -o show-diff show-diff.o read-cache.o $(LIBS)
+
+write-tree: write-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o write-tree write-tree.o read-cache.o $(LIBS)
+
+read-tree: read-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o read-tree read-tree.o read-cache.o $(LIBS)
+
+commit-tree: commit-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o commit-tree commit-tree.o read-cache.o $(LIBS)
+
+cat-file: cat-file.o read-cache.o
+ $(CC) $(CFLAGS) -o cat-file cat-file.o read-cache.o $(LIBS)
+
+fsck-cache: fsck-cache.o read-cache.o object.o commit.o tree.o blob.o
+ $(CC) $(CFLAGS) -o fsck-cache fsck-cache.o read-cache.o object.o commit.o tree.o blob.o $(LIBS)
+
+checkout-cache: checkout-cache.o read-cache.o
+ $(CC) $(CFLAGS) -o checkout-cache checkout-cache.o read-cache.o $(LIBS)
+
+diff-tree: diff-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o diff-tree diff-tree.o read-cache.o $(LIBS)
+
+rev-tree: rev-tree.o read-cache.o object.o commit.o tree.o blob.o
+ $(CC) $(CFLAGS) -o rev-tree rev-tree.o read-cache.o object.o commit.o tree.o blob.o $(LIBS)
+
+show-files: show-files.o read-cache.o
+ $(CC) $(CFLAGS) -o show-files show-files.o read-cache.o $(LIBS)
+
+check-files: check-files.o read-cache.o
+ $(CC) $(CFLAGS) -o check-files check-files.o read-cache.o $(LIBS)
+
+ls-tree: ls-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o ls-tree ls-tree.o read-cache.o $(LIBS)
+
+merge-base: merge-base.o read-cache.o object.o commit.o tree.o blob.o
+ $(CC) $(CFLAGS) -o merge-base merge-base.o read-cache.o object.o commit.o tree.o blob.o $(LIBS)
+
+read-cache.o: cache.h
+show-diff.o: cache.h
+
+clean:
+ rm -f *.o $(PROG)
+
+backup: clean
+ cd .. ; tar czvf dircache.tar.gz dir-cache
+
+
+Revision-number: 7
+Prop-content-length: 131
+Content-length: 131
+
+K 7
+svn:log
+V 32
+merge branch 'left' into 'trunk'
+K 10
+svn:author
+V 4
+samv
+K 8
+svn:date
+V 27
+2009-10-19T23:44:08.971801Z
+PROPS-END
+
+Node-path: trunk
+Node-kind: dir
+Node-action: change
+Prop-content-length: 83
+Content-length: 83
+
+K 9
+svk:merge
+V 53
+b48289b2-9c08-4d72-af37-0358a40b9c15:/branches/left:6
+PROPS-END
+
+
+Node-path: trunk/Makefile
+Node-kind: file
+Node-action: change
+Text-content-length: 2713
+Text-content-md5: 0afbe34f244cd662b1f97d708c687f90
+Content-length: 2713
+
+# -DCOLLISION_CHECK if you believe that SHA1's
+# 1461501637330902918203684832716283019655932542976 hashes do not give you
+# enough guarantees about no collisions between objects ever hapenning.
+#
+# -DNSEC if you want git to care about sub-second file mtimes and ctimes.
+# Note that you need some new glibc (at least >2.2.4) for this, and it will
+# BREAK YOUR LOCAL DIFFS! show-diff and anything using it will likely randomly
+# break unless your underlying filesystem supports those sub-second times
+# (my ext3 doesn't).
+CFLAGS=-g -O3 -Wall
+
+CC=gcc
+
+
+PROG= update-cache show-diff init-db write-tree read-tree commit-tree \
+ cat-file fsck-cache checkout-cache diff-tree rev-tree show-files \
+ check-files ls-tree merge-base merge-cache
+
+all: $(PROG)
+
+install: $(PROG)
+ install $(PROG) $(HOME)/bin/
+
+LIBS= -lssl -lz
+
+init-db: init-db.o
+
+update-cache: update-cache.o read-cache.o
+ $(CC) $(CFLAGS) -o update-cache update-cache.o read-cache.o $(LIBS)
+
+show-diff: show-diff.o read-cache.o
+ $(CC) $(CFLAGS) -o show-diff show-diff.o read-cache.o $(LIBS)
+
+write-tree: write-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o write-tree write-tree.o read-cache.o $(LIBS)
+
+read-tree: read-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o read-tree read-tree.o read-cache.o $(LIBS)
+
+commit-tree: commit-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o commit-tree commit-tree.o read-cache.o $(LIBS)
+
+cat-file: cat-file.o read-cache.o
+ $(CC) $(CFLAGS) -o cat-file cat-file.o read-cache.o $(LIBS)
+
+fsck-cache: fsck-cache.o read-cache.o object.o commit.o tree.o blob.o
+ $(CC) $(CFLAGS) -o fsck-cache fsck-cache.o read-cache.o object.o commit.o tree.o blob.o $(LIBS)
+
+checkout-cache: checkout-cache.o read-cache.o
+ $(CC) $(CFLAGS) -o checkout-cache checkout-cache.o read-cache.o $(LIBS)
+
+diff-tree: diff-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o diff-tree diff-tree.o read-cache.o $(LIBS)
+
+rev-tree: rev-tree.o read-cache.o object.o commit.o tree.o blob.o
+ $(CC) $(CFLAGS) -o rev-tree rev-tree.o read-cache.o object.o commit.o tree.o blob.o $(LIBS)
+
+show-files: show-files.o read-cache.o
+ $(CC) $(CFLAGS) -o show-files show-files.o read-cache.o $(LIBS)
+
+check-files: check-files.o read-cache.o
+ $(CC) $(CFLAGS) -o check-files check-files.o read-cache.o $(LIBS)
+
+ls-tree: ls-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o ls-tree ls-tree.o read-cache.o $(LIBS)
+
+merge-base: merge-base.o read-cache.o object.o commit.o tree.o blob.o
+ $(CC) $(CFLAGS) -o merge-base merge-base.o read-cache.o object.o commit.o tree.o blob.o $(LIBS)
+
+merge-cache: merge-cache.o read-cache.o
+ $(CC) $(CFLAGS) -o merge-cache merge-cache.o read-cache.o $(LIBS)
+
+read-cache.o: cache.h
+show-diff.o: cache.h
+
+clean:
+ rm -f *.o $(PROG)
+
+backup: clean
+ cd .. ; tar czvf dircache.tar.gz dir-cache
+
+
diff --git a/t/t9151-svn-mergeinfo.sh b/t/t9151-svn-mergeinfo.sh
new file mode 100755
index 0000000..c93a5be
--- /dev/null
+++ b/t/t9151-svn-mergeinfo.sh
@@ -0,0 +1,54 @@
+#!/bin/sh
+#
+# Copyright (c) 2007, 2009 Sam Vilain
+#
+
+test_description='git-svn svn mergeinfo properties'
+
+. ./lib-git-svn.sh
+
+test_expect_success 'load svn dump' "
+ svnadmin load -q '$rawsvnrepo' \
+ <'$TEST_DIRECTORY/t9151/svn-mergeinfo.dump' &&
+ git svn init --minimize-url -R svnmerge \
+ --rewrite-root=http://svn.example.org \
+ -T trunk -b branches '$svnrepo' &&
+ git svn fetch --all
+"
+
+test_expect_success 'all svn merges became git merge commits' '
+ git rev-list --all --no-merges --grep=Merge >unmarked &&
+ test_must_be_empty unmarked
+'
+
+test_expect_success 'cherry picks did not become git merge commits' '
+ git rev-list --all --merges --grep=Cherry >bad-cherries &&
+ test_must_be_empty bad-cherries
+'
+
+test_expect_success 'svn non-merge merge commits did not become git merge commits' '
+ git rev-list --all --merges --grep=non-merge >bad-non-merges &&
+ test_must_be_empty bad-non-merges
+'
+
+test_expect_success 'commit made to merged branch is reachable from the merge' '
+ before_commit=$(git rev-list --all --grep="trunk commit before merging trunk to b2") &&
+ merge_commit=$(git rev-list --all --grep="Merge trunk to b2") &&
+ git rev-list -1 $before_commit --not $merge_commit >not-reachable &&
+ test_must_be_empty not-reachable
+'
+
+test_expect_success 'merging two branches in one commit is detected correctly' '
+ f1_commit=$(git rev-list --all --grep="make f1 branch from trunk") &&
+ f2_commit=$(git rev-list --all --grep="make f2 branch from trunk") &&
+ merge_commit=$(git rev-list --all --grep="Merge f1 and f2 to trunk") &&
+ git rev-list -1 $f1_commit $f2_commit --not $merge_commit >not-reachable &&
+ test_must_be_empty not-reachable
+'
+
+test_expect_failure 'everything got merged in the end' '
+ git rev-list --all --not main >unmerged &&
+ test_must_be_empty unmerged
+'
+
+test_done
diff --git a/t/t9151/.gitignore b/t/t9151/.gitignore
new file mode 100644
index 0000000..587c37d
--- /dev/null
+++ b/t/t9151/.gitignore
@@ -0,0 +1,2 @@
+foo
+foo.svn
diff --git a/t/t9151/make-svnmerge-dump b/t/t9151/make-svnmerge-dump
new file mode 100755
index 0000000..e1e138c
--- /dev/null
+++ b/t/t9151/make-svnmerge-dump
@@ -0,0 +1,305 @@
+#!/bin/sh
+#
+# this script sets up a Subversion repository for Makefile in the
+# first ever git merge, as if it were done with svnmerge (SVN 1.5+)
+#
+
+rm -rf foo.svn foo
+set -e
+
+mkdir foo.svn
+svnadmin create foo.svn
+svn co file://`pwd`/foo.svn foo
+
+commit() {
+ i=$(( $1 + 1 ))
+ shift;
+ svn commit -m "(r$i) $*" >/dev/null || exit 1
+ echo $i
+}
+
+say() {
+ echo " * $*"
+}
+
+i=0
+cd foo
+mkdir trunk
+mkdir branches
+mkdir tags
+svn add trunk branches tags
+i=$(commit $i "Setup trunk, branches, and tags")
+
+git cat-file blob 6683463e:Makefile > trunk/Makefile
+svn add trunk/Makefile
+
+say "Committing ANCESTOR"
+i=$(commit $i "ancestor")
+svn cp trunk branches/left
+
+say "Committing BRANCH POINT"
+i=$(commit $i "make left branch")
+svn cp trunk branches/right
+
+say "Committing other BRANCH POINT"
+i=$(commit $i "make right branch")
+
+say "Committing LEFT UPDATE"
+git cat-file blob 5873b67e:Makefile > branches/left/Makefile
+i=$(commit $i "left update 1")
+
+git cat-file blob 75118b13:Makefile > branches/right/Makefile
+say "Committing RIGHT UPDATE"
+pre_right_update_1=$i
+i=$(commit $i "right update 1")
+
+say "Making more commits on LEFT"
+git cat-file blob ff5ebe39:Makefile > branches/left/Makefile
+i=$(commit $i "left update 2")
+git cat-file blob b5039db6:Makefile > branches/left/Makefile
+i=$(commit $i "left update 3")
+
+say "Making a LEFT SUB-BRANCH"
+svn cp branches/left branches/left-sub
+sub_left_make=$i
+i=$(commit $i "make left sub-branch")
+
+say "Making a commit on LEFT SUB-BRANCH"
+echo "crunch" > branches/left-sub/README
+svn add branches/left-sub/README
+i=$(commit $i "left sub-branch update 1")
+
+say "Merging LEFT to TRUNK"
+svn update
+cd trunk
+svn merge ../branches/left --accept postpone
+git cat-file blob b5039db6:Makefile > Makefile
+svn resolved Makefile
+i=$(commit $i "Merge left to trunk 1")
+cd ..
+
+say "Making more commits on LEFT and RIGHT"
+echo "touche" > branches/left/zlonk
+svn add branches/left/zlonk
+i=$(commit $i "left update 4")
+echo "thwacke" > branches/right/bang
+svn add branches/right/bang
+i=$(commit $i "right update 2")
+
+say "Squash merge of RIGHT tip 2 commits onto TRUNK"
+svn update
+cd trunk
+svn merge -r$pre_right_update_1:$i ../branches/right
+i=$(commit $i "Cherry-pick right 2 commits to trunk")
+cd ..
+
+say "Merging RIGHT to TRUNK"
+svn update
+cd trunk
+svn merge ../branches/right --accept postpone
+git cat-file blob b51ad431:Makefile > Makefile
+svn resolved Makefile
+i=$(commit $i "Merge right to trunk 1")
+cd ..
+
+say "Making more commits on RIGHT and TRUNK"
+echo "whamm" > branches/right/urkkk
+svn add branches/right/urkkk
+i=$(commit $i "right update 3")
+echo "pow" > trunk/vronk
+svn add trunk/vronk
+i=$(commit $i "trunk update 1")
+
+say "Merging RIGHT to LEFT SUB-BRANCH"
+svn update
+cd branches/left-sub
+svn merge ../right --accept postpone
+git cat-file blob b51ad431:Makefile > Makefile
+svn resolved Makefile
+i=$(commit $i "Merge right to left sub-branch")
+cd ../..
+
+say "Making more commits on LEFT SUB-BRANCH and LEFT"
+echo "zowie" > branches/left-sub/wham_eth
+svn add branches/left-sub/wham_eth
+pre_sub_left_update_2=$i
+i=$(commit $i "left sub-branch update 2")
+sub_left_update_2=$i
+echo "eee_yow" > branches/left/glurpp
+svn add branches/left/glurpp
+i=$(commit $i "left update 5")
+
+say "Cherry pick LEFT SUB-BRANCH commit to LEFT"
+svn update
+cd branches/left
+svn merge -r$pre_sub_left_update_2:$sub_left_update_2 ../left-sub
+i=$(commit $i "Cherry-pick left sub-branch commit to left")
+cd ../..
+
+say "Merging LEFT SUB-BRANCH back to LEFT"
+svn update
+cd branches/left
+# it's only a merge because the previous merge cherry-picked the top commit
+svn merge -r$sub_left_make:$sub_left_update_2 ../left-sub --accept postpone
+i=$(commit $i "Merge left sub-branch to left")
+cd ../..
+
+say "Merging EVERYTHING to TRUNK"
+svn update
+cd trunk
+svn merge ../branches/left --accept postpone
+svn resolved bang
+i=$(commit $i "Merge left to trunk 2")
+# this merge, svn happily updates the mergeinfo, but there is actually
+# nothing to merge. git-svn will not make a meaningless merge commit.
+svn merge ../branches/right --accept postpone
+i=$(commit $i "non-merge right to trunk 2")
+cd ..
+
+say "Branching b1 from trunk"
+svn update
+svn cp trunk branches/b1
+i=$(commit $i "make b1 branch from trunk")
+
+say "Branching b2 from trunk"
+svn update
+svn cp trunk branches/b2
+i=$(commit $i "make b2 branch from trunk")
+
+say "Make a commit to b2"
+svn update
+cd branches/b2
+echo "b2" > b2file
+svn add b2file
+i=$(commit $i "b2 update 1")
+cd ../..
+
+say "Make a commit to b1"
+svn update
+cd branches/b1
+echo "b1" > b1file
+svn add b1file
+i=$(commit $i "b1 update 1")
+cd ../..
+
+say "Merge b1 to trunk"
+svn update
+cd trunk
+svn merge ../branches/b1/ --accept postpone
+i=$(commit $i "Merge b1 to trunk")
+cd ..
+
+say "Make a commit to trunk before merging trunk to b2"
+svn update
+cd trunk
+echo "trunk" > trunkfile
+svn add trunkfile
+i=$(commit $i "trunk commit before merging trunk to b2")
+cd ..
+
+say "Merge trunk to b2"
+svn update
+cd branches/b2
+svn merge ../../trunk/ --accept postpone
+i=$(commit $i "Merge trunk to b2")
+cd ../..
+
+say "Merge b2 to trunk"
+svn update
+cd trunk
+svn merge ../branches/b2/ --accept postpone
+svn resolved b1file
+svn resolved trunkfile
+i=$(commit $i "Merge b2 to trunk")
+cd ..
+
+say "Creating f1 from trunk with a new file"
+svn update
+svn cp trunk branches/f1
+cd branches/f1
+echo "f1" > f1file
+svn add f1file
+cd ../..
+i=$(commit $i "make f1 branch from trunk with a new file")
+
+say "Creating f2 from trunk with a new file"
+svn update
+svn cp trunk branches/f2
+cd branches/f2
+echo "f2" > f2file
+svn add f2file
+cd ../..
+i=$(commit $i "make f2 branch from trunk with a new file")
+
+say "Merge f1 and f2 to trunk in one go"
+svn update
+cd trunk
+svn merge ../branches/f1/ --accept postpone
+svn merge ../branches/f2/ --accept postpone
+i=$(commit $i "Merge f1 and f2 to trunk")
+cd ..
+
+say "Adding subdirectory to LEFT"
+svn update
+cd branches/left
+mkdir subdir
+echo "Yeehaw" > subdir/cowboy
+svn add subdir
+i=$(commit $i "add subdirectory to left branch")
+cd ../../
+
+say "Merging LEFT to TRUNK"
+svn update
+cd trunk
+svn merge ../branches/left --accept postpone
+i=$(commit $i "merge left to trunk")
+cd ..
+
+say "Make PARTIAL branch"
+svn update
+svn cp trunk/subdir branches/partial
+i=$(commit $i "make partial branch")
+
+say "Make a commit to PARTIAL"
+svn update
+cd branches/partial
+echo "racecar" > palindromes
+svn add palindromes
+i=$(commit $i "partial update")
+cd ../../
+
+say "Merge PARTIAL to TRUNK"
+svn update
+cd trunk/subdir
+svn merge ../../branches/partial --accept postpone
+i=$(commit $i "merge partial to trunk")
+cd ../../
+
+say "Tagging trunk"
+svn update
+svn cp trunk tags/v1.0
+i=$(commit $i "tagging v1.0")
+
+say "Branching BUGFIX from v1.0"
+svn update
+svn cp tags/v1.0 branches/bugfix
+i=$(commit $i "make bugfix branch from tag")
+
+say "Make a commit to BUGFIX"
+svn update
+cd branches/bugfix/
+echo "kayak" >> subdir/palindromes
+i=$(commit $i "commit to bugfix")
+cd ../../
+
+say "Merge BUGFIX to TRUNK"
+svn update
+cd trunk
+svn merge ../branches/bugfix/ --accept postpone
+i=$(commit $i "Merge BUGFIX to TRUNK")
+cd ..
+
+cd ..
+svnadmin dump foo.svn > svn-mergeinfo.dump
+
+rm -rf foo foo.svn
diff --git a/t/t9151/svn-mergeinfo.dump b/t/t9151/svn-mergeinfo.dump
new file mode 100644
index 0000000..47cafcf
--- /dev/null
+++ b/t/t9151/svn-mergeinfo.dump
@@ -0,0 +1,2388 @@
+SVN-fs-dump-format-version: 2
+
+UUID: d6191530-2693-4a8e-98e7-b194d4c3edd8
+
+Revision-number: 0
+Prop-content-length: 56
+Content-length: 56
+
+K 8
+svn:date
+V 27
+2010-01-19T04:14:02.832406Z
+PROPS-END
+
+Revision-number: 1
+Prop-content-length: 134
+Content-length: 134
+
+K 7
+svn:log
+V 36
+(r1) Setup trunk, branches, and tags
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-01-19T04:14:03.055172Z
+PROPS-END
+
+Node-path: branches
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Node-path: tags
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Node-path: trunk
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Revision-number: 2
+Prop-content-length: 111
+Content-length: 111
+
+K 7
+svn:log
+V 13
+(r2) ancestor
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-01-19T04:14:04.064506Z
+PROPS-END
+
+Node-path: trunk/Makefile
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 2401
+Text-content-md5: bfd8ff778d1492dc6758567373176a89
+Text-content-sha1: 103205ce331f7d64086dba497574734f78439590
+Content-length: 2411
+
+PROPS-END
+# -DCOLLISION_CHECK if you believe that SHA1's
+# 1461501637330902918203684832716283019655932542976 hashes do not give you
+# enough guarantees about no collisions between objects ever hapenning.
+#
+# -DNSEC if you want git to care about sub-second file mtimes and ctimes.
+# Note that you need some new glibc (at least >2.2.4) for this, and it will
+# BREAK YOUR LOCAL DIFFS! show-diff and anything using it will likely randomly
+# break unless your underlying filesystem supports those sub-second times
+# (my ext3 doesn't).
+CFLAGS=-g -O3 -Wall
+
+CC=gcc
+
+
+PROG= update-cache show-diff init-db write-tree read-tree commit-tree \
+ cat-file fsck-cache checkout-cache diff-tree rev-tree show-files \
+ check-files ls-tree merge-base
+
+all: $(PROG)
+
+install: $(PROG)
+ install $(PROG) $(HOME)/bin/
+
+LIBS= -lssl -lz
+
+init-db: init-db.o
+
+update-cache: update-cache.o read-cache.o
+ $(CC) $(CFLAGS) -o update-cache update-cache.o read-cache.o $(LIBS)
+
+show-diff: show-diff.o read-cache.o
+ $(CC) $(CFLAGS) -o show-diff show-diff.o read-cache.o $(LIBS)
+
+write-tree: write-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o write-tree write-tree.o read-cache.o $(LIBS)
+
+read-tree: read-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o read-tree read-tree.o read-cache.o $(LIBS)
+
+commit-tree: commit-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o commit-tree commit-tree.o read-cache.o $(LIBS)
+
+cat-file: cat-file.o read-cache.o
+ $(CC) $(CFLAGS) -o cat-file cat-file.o read-cache.o $(LIBS)
+
+fsck-cache: fsck-cache.o read-cache.o
+ $(CC) $(CFLAGS) -o fsck-cache fsck-cache.o read-cache.o $(LIBS)
+
+checkout-cache: checkout-cache.o read-cache.o
+ $(CC) $(CFLAGS) -o checkout-cache checkout-cache.o read-cache.o $(LIBS)
+
+diff-tree: diff-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o diff-tree diff-tree.o read-cache.o $(LIBS)
+
+rev-tree: rev-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o rev-tree rev-tree.o read-cache.o $(LIBS)
+
+show-files: show-files.o read-cache.o
+ $(CC) $(CFLAGS) -o show-files show-files.o read-cache.o $(LIBS)
+
+check-files: check-files.o read-cache.o
+ $(CC) $(CFLAGS) -o check-files check-files.o read-cache.o $(LIBS)
+
+ls-tree: ls-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o ls-tree ls-tree.o read-cache.o $(LIBS)
+
+merge-base: merge-base.o read-cache.o
+ $(CC) $(CFLAGS) -o merge-base merge-base.o read-cache.o $(LIBS)
+
+read-cache.o: cache.h
+show-diff.o: cache.h
+
+clean:
+ rm -f *.o $(PROG)
+
+backup: clean
+ cd .. ; tar czvf dircache.tar.gz dir-cache
+
+
+Revision-number: 3
+Prop-content-length: 119
+Content-length: 119
+
+K 7
+svn:log
+V 21
+(r3) make left branch
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-01-19T04:14:06.040389Z
+PROPS-END
+
+Node-path: branches/left
+Node-kind: dir
+Node-action: add
+Node-copyfrom-rev: 1
+Node-copyfrom-path: trunk
+
+
+Node-path: branches/left/Makefile
+Node-kind: file
+Node-action: add
+Node-copyfrom-rev: 2
+Node-copyfrom-path: trunk/Makefile
+Text-copy-source-md5: bfd8ff778d1492dc6758567373176a89
+Text-copy-source-sha1: 103205ce331f7d64086dba497574734f78439590
+
+
+Revision-number: 4
+Prop-content-length: 120
+Content-length: 120
+
+K 7
+svn:log
+V 22
+(r4) make right branch
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-01-19T04:14:08.040905Z
+PROPS-END
+
+Node-path: branches/right
+Node-kind: dir
+Node-action: add
+Node-copyfrom-rev: 1
+Node-copyfrom-path: trunk
+
+
+Node-path: branches/right/Makefile
+Node-kind: file
+Node-action: add
+Node-copyfrom-rev: 2
+Node-copyfrom-path: trunk/Makefile
+Text-copy-source-md5: bfd8ff778d1492dc6758567373176a89
+Text-copy-source-sha1: 103205ce331f7d64086dba497574734f78439590
+
+
+Revision-number: 5
+Prop-content-length: 116
+Content-length: 116
+
+K 7
+svn:log
+V 18
+(r5) left update 1
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-01-19T04:14:09.049169Z
+PROPS-END
+
+Node-path: branches/left/Makefile
+Node-kind: file
+Node-action: change
+Text-content-length: 2465
+Text-content-md5: 16e38d9753b061731650561ce01b1195
+Text-content-sha1: 36da4b84ea9b64218ab48171dfc5c48ae025f38b
+Content-length: 2465
+
+# -DCOLLISION_CHECK if you believe that SHA1's
+# 1461501637330902918203684832716283019655932542976 hashes do not give you
+# enough guarantees about no collisions between objects ever hapenning.
+#
+# -DNSEC if you want git to care about sub-second file mtimes and ctimes.
+# Note that you need some new glibc (at least >2.2.4) for this, and it will
+# BREAK YOUR LOCAL DIFFS! show-diff and anything using it will likely randomly
+# break unless your underlying filesystem supports those sub-second times
+# (my ext3 doesn't).
+CFLAGS=-g -O3 -Wall
+
+CC=gcc
+
+
+PROG= update-cache show-diff init-db write-tree read-tree commit-tree \
+ cat-file fsck-cache checkout-cache diff-tree rev-tree show-files \
+ check-files ls-tree merge-base
+
+all: $(PROG)
+
+install: $(PROG)
+ install $(PROG) $(HOME)/bin/
+
+LIBS= -lssl -lz
+
+init-db: init-db.o
+
+update-cache: update-cache.o read-cache.o
+ $(CC) $(CFLAGS) -o update-cache update-cache.o read-cache.o $(LIBS)
+
+show-diff: show-diff.o read-cache.o
+ $(CC) $(CFLAGS) -o show-diff show-diff.o read-cache.o $(LIBS)
+
+write-tree: write-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o write-tree write-tree.o read-cache.o $(LIBS)
+
+read-tree: read-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o read-tree read-tree.o read-cache.o $(LIBS)
+
+commit-tree: commit-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o commit-tree commit-tree.o read-cache.o $(LIBS)
+
+cat-file: cat-file.o read-cache.o
+ $(CC) $(CFLAGS) -o cat-file cat-file.o read-cache.o $(LIBS)
+
+fsck-cache: fsck-cache.o read-cache.o
+ $(CC) $(CFLAGS) -o fsck-cache fsck-cache.o read-cache.o $(LIBS)
+
+checkout-cache: checkout-cache.o read-cache.o
+ $(CC) $(CFLAGS) -o checkout-cache checkout-cache.o read-cache.o $(LIBS)
+
+diff-tree: diff-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o diff-tree diff-tree.o read-cache.o $(LIBS)
+
+rev-tree: rev-tree.o read-cache.o object.o commit.o tree.o blob.o
+ $(CC) $(CFLAGS) -o rev-tree rev-tree.o read-cache.o object.o commit.o tree.o blob.o $(LIBS)
+
+show-files: show-files.o read-cache.o
+ $(CC) $(CFLAGS) -o show-files show-files.o read-cache.o $(LIBS)
+
+check-files: check-files.o read-cache.o
+ $(CC) $(CFLAGS) -o check-files check-files.o read-cache.o $(LIBS)
+
+ls-tree: ls-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o ls-tree ls-tree.o read-cache.o $(LIBS)
+
+merge-base: merge-base.o read-cache.o
+ $(CC) $(CFLAGS) -o merge-base merge-base.o read-cache.o $(LIBS)
+
+read-cache.o: cache.h
+show-diff.o: cache.h
+
+clean:
+ rm -f *.o $(PROG)
+
+backup: clean
+ cd .. ; tar czvf dircache.tar.gz dir-cache
+
+
+Revision-number: 6
+Prop-content-length: 117
+Content-length: 117
+
+K 7
+svn:log
+V 19
+(r6) right update 1
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-01-19T04:14:10.049350Z
+PROPS-END
+
+Node-path: branches/right/Makefile
+Node-kind: file
+Node-action: change
+Text-content-length: 2521
+Text-content-md5: 0668418a621333f4aa8b6632cd63e2a0
+Text-content-sha1: 4f29afd038e52f45acb5ef8c41acfc70062a741a
+Content-length: 2521
+
+# -DCOLLISION_CHECK if you believe that SHA1's
+# 1461501637330902918203684832716283019655932542976 hashes do not give you
+# enough guarantees about no collisions between objects ever hapenning.
+#
+# -DNSEC if you want git to care about sub-second file mtimes and ctimes.
+# Note that you need some new glibc (at least >2.2.4) for this, and it will
+# BREAK YOUR LOCAL DIFFS! show-diff and anything using it will likely randomly
+# break unless your underlying filesystem supports those sub-second times
+# (my ext3 doesn't).
+CFLAGS=-g -O3 -Wall
+
+CC=gcc
+
+
+PROG= update-cache show-diff init-db write-tree read-tree commit-tree \
+ cat-file fsck-cache checkout-cache diff-tree rev-tree show-files \
+ check-files ls-tree merge-base merge-cache
+
+all: $(PROG)
+
+install: $(PROG)
+ install $(PROG) $(HOME)/bin/
+
+LIBS= -lssl -lz
+
+init-db: init-db.o
+
+update-cache: update-cache.o read-cache.o
+ $(CC) $(CFLAGS) -o update-cache update-cache.o read-cache.o $(LIBS)
+
+show-diff: show-diff.o read-cache.o
+ $(CC) $(CFLAGS) -o show-diff show-diff.o read-cache.o $(LIBS)
+
+write-tree: write-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o write-tree write-tree.o read-cache.o $(LIBS)
+
+read-tree: read-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o read-tree read-tree.o read-cache.o $(LIBS)
+
+commit-tree: commit-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o commit-tree commit-tree.o read-cache.o $(LIBS)
+
+cat-file: cat-file.o read-cache.o
+ $(CC) $(CFLAGS) -o cat-file cat-file.o read-cache.o $(LIBS)
+
+fsck-cache: fsck-cache.o read-cache.o
+ $(CC) $(CFLAGS) -o fsck-cache fsck-cache.o read-cache.o $(LIBS)
+
+checkout-cache: checkout-cache.o read-cache.o
+ $(CC) $(CFLAGS) -o checkout-cache checkout-cache.o read-cache.o $(LIBS)
+
+diff-tree: diff-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o diff-tree diff-tree.o read-cache.o $(LIBS)
+
+rev-tree: rev-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o rev-tree rev-tree.o read-cache.o $(LIBS)
+
+show-files: show-files.o read-cache.o
+ $(CC) $(CFLAGS) -o show-files show-files.o read-cache.o $(LIBS)
+
+check-files: check-files.o read-cache.o
+ $(CC) $(CFLAGS) -o check-files check-files.o read-cache.o $(LIBS)
+
+ls-tree: ls-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o ls-tree ls-tree.o read-cache.o $(LIBS)
+
+merge-base: merge-base.o read-cache.o
+ $(CC) $(CFLAGS) -o merge-base merge-base.o read-cache.o $(LIBS)
+
+merge-cache: merge-cache.o read-cache.o
+ $(CC) $(CFLAGS) -o merge-cache merge-cache.o read-cache.o $(LIBS)
+
+read-cache.o: cache.h
+show-diff.o: cache.h
+
+clean:
+ rm -f *.o $(PROG)
+
+backup: clean
+ cd .. ; tar czvf dircache.tar.gz dir-cache
+
+
+Revision-number: 7
+Prop-content-length: 116
+Content-length: 116
+
+K 7
+svn:log
+V 18
+(r7) left update 2
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-01-19T04:14:11.049209Z
+PROPS-END
+
+Node-path: branches/left/Makefile
+Node-kind: file
+Node-action: change
+Text-content-length: 2529
+Text-content-md5: f6b197cc3f2e89a83e545d4bb003de73
+Text-content-sha1: 2f656677cfec0bceec85e53036ffb63e25126f8e
+Content-length: 2529
+
+# -DCOLLISION_CHECK if you believe that SHA1's
+# 1461501637330902918203684832716283019655932542976 hashes do not give you
+# enough guarantees about no collisions between objects ever hapenning.
+#
+# -DNSEC if you want git to care about sub-second file mtimes and ctimes.
+# Note that you need some new glibc (at least >2.2.4) for this, and it will
+# BREAK YOUR LOCAL DIFFS! show-diff and anything using it will likely randomly
+# break unless your underlying filesystem supports those sub-second times
+# (my ext3 doesn't).
+CFLAGS=-g -O3 -Wall
+
+CC=gcc
+
+
+PROG= update-cache show-diff init-db write-tree read-tree commit-tree \
+ cat-file fsck-cache checkout-cache diff-tree rev-tree show-files \
+ check-files ls-tree merge-base
+
+all: $(PROG)
+
+install: $(PROG)
+ install $(PROG) $(HOME)/bin/
+
+LIBS= -lssl -lz
+
+init-db: init-db.o
+
+update-cache: update-cache.o read-cache.o
+ $(CC) $(CFLAGS) -o update-cache update-cache.o read-cache.o $(LIBS)
+
+show-diff: show-diff.o read-cache.o
+ $(CC) $(CFLAGS) -o show-diff show-diff.o read-cache.o $(LIBS)
+
+write-tree: write-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o write-tree write-tree.o read-cache.o $(LIBS)
+
+read-tree: read-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o read-tree read-tree.o read-cache.o $(LIBS)
+
+commit-tree: commit-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o commit-tree commit-tree.o read-cache.o $(LIBS)
+
+cat-file: cat-file.o read-cache.o
+ $(CC) $(CFLAGS) -o cat-file cat-file.o read-cache.o $(LIBS)
+
+fsck-cache: fsck-cache.o read-cache.o object.o commit.o tree.o blob.o
+ $(CC) $(CFLAGS) -o fsck-cache fsck-cache.o read-cache.o object.o commit.o tree.o blob.o $(LIBS)
+
+checkout-cache: checkout-cache.o read-cache.o
+ $(CC) $(CFLAGS) -o checkout-cache checkout-cache.o read-cache.o $(LIBS)
+
+diff-tree: diff-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o diff-tree diff-tree.o read-cache.o $(LIBS)
+
+rev-tree: rev-tree.o read-cache.o object.o commit.o tree.o blob.o
+ $(CC) $(CFLAGS) -o rev-tree rev-tree.o read-cache.o object.o commit.o tree.o blob.o $(LIBS)
+
+show-files: show-files.o read-cache.o
+ $(CC) $(CFLAGS) -o show-files show-files.o read-cache.o $(LIBS)
+
+check-files: check-files.o read-cache.o
+ $(CC) $(CFLAGS) -o check-files check-files.o read-cache.o $(LIBS)
+
+ls-tree: ls-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o ls-tree ls-tree.o read-cache.o $(LIBS)
+
+merge-base: merge-base.o read-cache.o
+ $(CC) $(CFLAGS) -o merge-base merge-base.o read-cache.o $(LIBS)
+
+read-cache.o: cache.h
+show-diff.o: cache.h
+
+clean:
+ rm -f *.o $(PROG)
+
+backup: clean
+ cd .. ; tar czvf dircache.tar.gz dir-cache
+
+
+Revision-number: 8
+Prop-content-length: 116
+Content-length: 116
+
+K 7
+svn:log
+V 18
+(r8) left update 3
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-01-19T04:14:12.049234Z
+PROPS-END
+
+Node-path: branches/left/Makefile
+Node-kind: file
+Node-action: change
+Text-content-length: 2593
+Text-content-md5: 5ccff689fb290e00b85fe18ee50c54ba
+Text-content-sha1: a13de8e23f1483efca3e57b2b64b0ae6f740ce10
+Content-length: 2593
+
+# -DCOLLISION_CHECK if you believe that SHA1's
+# 1461501637330902918203684832716283019655932542976 hashes do not give you
+# enough guarantees about no collisions between objects ever hapenning.
+#
+# -DNSEC if you want git to care about sub-second file mtimes and ctimes.
+# Note that you need some new glibc (at least >2.2.4) for this, and it will
+# BREAK YOUR LOCAL DIFFS! show-diff and anything using it will likely randomly
+# break unless your underlying filesystem supports those sub-second times
+# (my ext3 doesn't).
+CFLAGS=-g -O3 -Wall
+
+CC=gcc
+
+
+PROG= update-cache show-diff init-db write-tree read-tree commit-tree \
+ cat-file fsck-cache checkout-cache diff-tree rev-tree show-files \
+ check-files ls-tree merge-base
+
+all: $(PROG)
+
+install: $(PROG)
+ install $(PROG) $(HOME)/bin/
+
+LIBS= -lssl -lz
+
+init-db: init-db.o
+
+update-cache: update-cache.o read-cache.o
+ $(CC) $(CFLAGS) -o update-cache update-cache.o read-cache.o $(LIBS)
+
+show-diff: show-diff.o read-cache.o
+ $(CC) $(CFLAGS) -o show-diff show-diff.o read-cache.o $(LIBS)
+
+write-tree: write-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o write-tree write-tree.o read-cache.o $(LIBS)
+
+read-tree: read-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o read-tree read-tree.o read-cache.o $(LIBS)
+
+commit-tree: commit-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o commit-tree commit-tree.o read-cache.o $(LIBS)
+
+cat-file: cat-file.o read-cache.o
+ $(CC) $(CFLAGS) -o cat-file cat-file.o read-cache.o $(LIBS)
+
+fsck-cache: fsck-cache.o read-cache.o object.o commit.o tree.o blob.o
+ $(CC) $(CFLAGS) -o fsck-cache fsck-cache.o read-cache.o object.o commit.o tree.o blob.o $(LIBS)
+
+checkout-cache: checkout-cache.o read-cache.o
+ $(CC) $(CFLAGS) -o checkout-cache checkout-cache.o read-cache.o $(LIBS)
+
+diff-tree: diff-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o diff-tree diff-tree.o read-cache.o $(LIBS)
+
+rev-tree: rev-tree.o read-cache.o object.o commit.o tree.o blob.o
+ $(CC) $(CFLAGS) -o rev-tree rev-tree.o read-cache.o object.o commit.o tree.o blob.o $(LIBS)
+
+show-files: show-files.o read-cache.o
+ $(CC) $(CFLAGS) -o show-files show-files.o read-cache.o $(LIBS)
+
+check-files: check-files.o read-cache.o
+ $(CC) $(CFLAGS) -o check-files check-files.o read-cache.o $(LIBS)
+
+ls-tree: ls-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o ls-tree ls-tree.o read-cache.o $(LIBS)
+
+merge-base: merge-base.o read-cache.o object.o commit.o tree.o blob.o
+ $(CC) $(CFLAGS) -o merge-base merge-base.o read-cache.o object.o commit.o tree.o blob.o $(LIBS)
+
+read-cache.o: cache.h
+show-diff.o: cache.h
+
+clean:
+ rm -f *.o $(PROG)
+
+backup: clean
+ cd .. ; tar czvf dircache.tar.gz dir-cache
+
+
+Revision-number: 9
+Prop-content-length: 123
+Content-length: 123
+
+K 7
+svn:log
+V 25
+(r9) make left sub-branch
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-01-19T04:14:14.040894Z
+PROPS-END
+
+Node-path: branches/left-sub
+Node-kind: dir
+Node-action: add
+Node-copyfrom-rev: 3
+Node-copyfrom-path: branches/left
+
+
+Node-path: branches/left-sub/Makefile
+Node-kind: file
+Node-action: delete
+
+Node-path: branches/left-sub/Makefile
+Node-kind: file
+Node-action: add
+Node-copyfrom-rev: 8
+Node-copyfrom-path: branches/left/Makefile
+Text-copy-source-md5: 5ccff689fb290e00b85fe18ee50c54ba
+Text-copy-source-sha1: a13de8e23f1483efca3e57b2b64b0ae6f740ce10
+
+
+
+
+Revision-number: 10
+Prop-content-length: 128
+Content-length: 128
+
+K 7
+svn:log
+V 30
+(r10) left sub-branch update 1
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-01-19T04:14:15.049935Z
+PROPS-END
+
+Node-path: branches/left-sub/README
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 7
+Text-content-md5: fdbcfb6be9afe1121862143f226b51cf
+Text-content-sha1: 1d1f5ea4ceb584337ffe59b8980d92e3b78dfef4
+Content-length: 17
+
+PROPS-END
+crunch
+
+
+Revision-number: 11
+Prop-content-length: 125
+Content-length: 125
+
+K 7
+svn:log
+V 27
+(r11) Merge left to trunk 1
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-01-19T04:14:18.056594Z
+PROPS-END
+
+Node-path: trunk
+Node-kind: dir
+Node-action: change
+Prop-content-length: 54
+Content-length: 54
+
+K 13
+svn:mergeinfo
+V 19
+/branches/left:2-10
+PROPS-END
+
+
+Node-path: trunk/Makefile
+Node-kind: file
+Node-action: change
+Text-content-length: 2593
+Text-content-md5: 5ccff689fb290e00b85fe18ee50c54ba
+Text-content-sha1: a13de8e23f1483efca3e57b2b64b0ae6f740ce10
+Content-length: 2593
+
+# -DCOLLISION_CHECK if you believe that SHA1's
+# 1461501637330902918203684832716283019655932542976 hashes do not give you
+# enough guarantees about no collisions between objects ever hapenning.
+#
+# -DNSEC if you want git to care about sub-second file mtimes and ctimes.
+# Note that you need some new glibc (at least >2.2.4) for this, and it will
+# BREAK YOUR LOCAL DIFFS! show-diff and anything using it will likely randomly
+# break unless your underlying filesystem supports those sub-second times
+# (my ext3 doesn't).
+CFLAGS=-g -O3 -Wall
+
+CC=gcc
+
+
+PROG= update-cache show-diff init-db write-tree read-tree commit-tree \
+ cat-file fsck-cache checkout-cache diff-tree rev-tree show-files \
+ check-files ls-tree merge-base
+
+all: $(PROG)
+
+install: $(PROG)
+ install $(PROG) $(HOME)/bin/
+
+LIBS= -lssl -lz
+
+init-db: init-db.o
+
+update-cache: update-cache.o read-cache.o
+ $(CC) $(CFLAGS) -o update-cache update-cache.o read-cache.o $(LIBS)
+
+show-diff: show-diff.o read-cache.o
+ $(CC) $(CFLAGS) -o show-diff show-diff.o read-cache.o $(LIBS)
+
+write-tree: write-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o write-tree write-tree.o read-cache.o $(LIBS)
+
+read-tree: read-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o read-tree read-tree.o read-cache.o $(LIBS)
+
+commit-tree: commit-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o commit-tree commit-tree.o read-cache.o $(LIBS)
+
+cat-file: cat-file.o read-cache.o
+ $(CC) $(CFLAGS) -o cat-file cat-file.o read-cache.o $(LIBS)
+
+fsck-cache: fsck-cache.o read-cache.o object.o commit.o tree.o blob.o
+ $(CC) $(CFLAGS) -o fsck-cache fsck-cache.o read-cache.o object.o commit.o tree.o blob.o $(LIBS)
+
+checkout-cache: checkout-cache.o read-cache.o
+ $(CC) $(CFLAGS) -o checkout-cache checkout-cache.o read-cache.o $(LIBS)
+
+diff-tree: diff-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o diff-tree diff-tree.o read-cache.o $(LIBS)
+
+rev-tree: rev-tree.o read-cache.o object.o commit.o tree.o blob.o
+ $(CC) $(CFLAGS) -o rev-tree rev-tree.o read-cache.o object.o commit.o tree.o blob.o $(LIBS)
+
+show-files: show-files.o read-cache.o
+ $(CC) $(CFLAGS) -o show-files show-files.o read-cache.o $(LIBS)
+
+check-files: check-files.o read-cache.o
+ $(CC) $(CFLAGS) -o check-files check-files.o read-cache.o $(LIBS)
+
+ls-tree: ls-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o ls-tree ls-tree.o read-cache.o $(LIBS)
+
+merge-base: merge-base.o read-cache.o object.o commit.o tree.o blob.o
+ $(CC) $(CFLAGS) -o merge-base merge-base.o read-cache.o object.o commit.o tree.o blob.o $(LIBS)
+
+read-cache.o: cache.h
+show-diff.o: cache.h
+
+clean:
+ rm -f *.o $(PROG)
+
+backup: clean
+ cd .. ; tar czvf dircache.tar.gz dir-cache
+
+
+Revision-number: 12
+Prop-content-length: 117
+Content-length: 117
+
+K 7
+svn:log
+V 19
+(r12) left update 4
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-01-19T04:14:19.049620Z
+PROPS-END
+
+Node-path: branches/left/zlonk
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 7
+Text-content-md5: 8b9d8c7c2aaa6167e7d3407a773bbbba
+Text-content-sha1: 9716527ebd70a75c27625cacbeb2d897c6e86178
+Content-length: 17
+
+PROPS-END
+touche
+
+
+Revision-number: 13
+Prop-content-length: 118
+Content-length: 118
+
+K 7
+svn:log
+V 20
+(r13) right update 2
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-01-19T04:14:20.049659Z
+PROPS-END
+
+Node-path: branches/right/bang
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 8
+Text-content-md5: 34c28f1d2dc6a9adeccc4265bf7516cb
+Text-content-sha1: 0bc5bb345c0e71d28f784f12e0bd2d384c283062
+Content-length: 18
+
+PROPS-END
+thwacke
+
+
+Revision-number: 14
+Prop-content-length: 140
+Content-length: 140
+
+K 7
+svn:log
+V 42
+(r14) Cherry-pick right 2 commits to trunk
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-01-19T04:14:23.041991Z
+PROPS-END
+
+Node-path: trunk
+Node-kind: dir
+Node-action: change
+Prop-content-length: 75
+Content-length: 75
+
+K 13
+svn:mergeinfo
+V 40
+/branches/left:2-10
+/branches/right:6-13
+PROPS-END
+
+
+Node-path: trunk/Makefile
+Node-kind: file
+Node-action: change
+Text-content-length: 2713
+Text-content-md5: 0afbe34f244cd662b1f97d708c687f90
+Text-content-sha1: 46d9377d783e67a9b581da110352e799517c8a14
+Content-length: 2713
+
+# -DCOLLISION_CHECK if you believe that SHA1's
+# 1461501637330902918203684832716283019655932542976 hashes do not give you
+# enough guarantees about no collisions between objects ever hapenning.
+#
+# -DNSEC if you want git to care about sub-second file mtimes and ctimes.
+# Note that you need some new glibc (at least >2.2.4) for this, and it will
+# BREAK YOUR LOCAL DIFFS! show-diff and anything using it will likely randomly
+# break unless your underlying filesystem supports those sub-second times
+# (my ext3 doesn't).
+CFLAGS=-g -O3 -Wall
+
+CC=gcc
+
+
+PROG= update-cache show-diff init-db write-tree read-tree commit-tree \
+ cat-file fsck-cache checkout-cache diff-tree rev-tree show-files \
+ check-files ls-tree merge-base merge-cache
+
+all: $(PROG)
+
+install: $(PROG)
+ install $(PROG) $(HOME)/bin/
+
+LIBS= -lssl -lz
+
+init-db: init-db.o
+
+update-cache: update-cache.o read-cache.o
+ $(CC) $(CFLAGS) -o update-cache update-cache.o read-cache.o $(LIBS)
+
+show-diff: show-diff.o read-cache.o
+ $(CC) $(CFLAGS) -o show-diff show-diff.o read-cache.o $(LIBS)
+
+write-tree: write-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o write-tree write-tree.o read-cache.o $(LIBS)
+
+read-tree: read-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o read-tree read-tree.o read-cache.o $(LIBS)
+
+commit-tree: commit-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o commit-tree commit-tree.o read-cache.o $(LIBS)
+
+cat-file: cat-file.o read-cache.o
+ $(CC) $(CFLAGS) -o cat-file cat-file.o read-cache.o $(LIBS)
+
+fsck-cache: fsck-cache.o read-cache.o object.o commit.o tree.o blob.o
+ $(CC) $(CFLAGS) -o fsck-cache fsck-cache.o read-cache.o object.o commit.o tree.o blob.o $(LIBS)
+
+checkout-cache: checkout-cache.o read-cache.o
+ $(CC) $(CFLAGS) -o checkout-cache checkout-cache.o read-cache.o $(LIBS)
+
+diff-tree: diff-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o diff-tree diff-tree.o read-cache.o $(LIBS)
+
+rev-tree: rev-tree.o read-cache.o object.o commit.o tree.o blob.o
+ $(CC) $(CFLAGS) -o rev-tree rev-tree.o read-cache.o object.o commit.o tree.o blob.o $(LIBS)
+
+show-files: show-files.o read-cache.o
+ $(CC) $(CFLAGS) -o show-files show-files.o read-cache.o $(LIBS)
+
+check-files: check-files.o read-cache.o
+ $(CC) $(CFLAGS) -o check-files check-files.o read-cache.o $(LIBS)
+
+ls-tree: ls-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o ls-tree ls-tree.o read-cache.o $(LIBS)
+
+merge-base: merge-base.o read-cache.o object.o commit.o tree.o blob.o
+ $(CC) $(CFLAGS) -o merge-base merge-base.o read-cache.o object.o commit.o tree.o blob.o $(LIBS)
+
+merge-cache: merge-cache.o read-cache.o
+ $(CC) $(CFLAGS) -o merge-cache merge-cache.o read-cache.o $(LIBS)
+
+read-cache.o: cache.h
+show-diff.o: cache.h
+
+clean:
+ rm -f *.o $(PROG)
+
+backup: clean
+ cd .. ; tar czvf dircache.tar.gz dir-cache
+
+
+Node-path: trunk/bang
+Node-kind: file
+Node-action: add
+Node-copyfrom-rev: 13
+Node-copyfrom-path: branches/right/bang
+Text-copy-source-md5: 34c28f1d2dc6a9adeccc4265bf7516cb
+Text-copy-source-sha1: 0bc5bb345c0e71d28f784f12e0bd2d384c283062
+
+
+Revision-number: 15
+Prop-content-length: 126
+Content-length: 126
+
+K 7
+svn:log
+V 28
+(r15) Merge right to trunk 1
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-01-19T04:14:26.054456Z
+PROPS-END
+
+Node-path: trunk
+Node-kind: dir
+Node-action: change
+Prop-content-length: 75
+Content-length: 75
+
+K 13
+svn:mergeinfo
+V 40
+/branches/left:2-10
+/branches/right:2-14
+PROPS-END
+
+
+Revision-number: 16
+Prop-content-length: 118
+Content-length: 118
+
+K 7
+svn:log
+V 20
+(r16) right update 3
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-01-19T04:14:27.049955Z
+PROPS-END
+
+Node-path: branches/right/urkkk
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 6
+Text-content-md5: 5889c8392e16251b0c80927607a03036
+Text-content-sha1: 3934264d277a0cf886b6b1c7f2b9e56da2525302
+Content-length: 16
+
+PROPS-END
+whamm
+
+
+Revision-number: 17
+Prop-content-length: 118
+Content-length: 118
+
+K 7
+svn:log
+V 20
+(r17) trunk update 1
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-01-19T04:14:28.049615Z
+PROPS-END
+
+Node-path: trunk/vronk
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 4
+Text-content-md5: b2f80fa02a7f1364b9c29d3da44bf9f9
+Text-content-sha1: e994d980c0f2d7a3f76138bf96d57f36f9633828
+Content-length: 14
+
+PROPS-END
+pow
+
+
+Revision-number: 18
+Prop-content-length: 134
+Content-length: 134
+
+K 7
+svn:log
+V 36
+(r18) Merge right to left sub-branch
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-01-19T04:14:31.061460Z
+PROPS-END
+
+Node-path: branches/left-sub
+Node-kind: dir
+Node-action: change
+Prop-content-length: 55
+Content-length: 55
+
+K 13
+svn:mergeinfo
+V 20
+/branches/right:2-17
+PROPS-END
+
+
+Node-path: branches/left-sub/Makefile
+Node-kind: file
+Node-action: change
+Text-content-length: 2713
+Text-content-md5: 0afbe34f244cd662b1f97d708c687f90
+Text-content-sha1: 46d9377d783e67a9b581da110352e799517c8a14
+Content-length: 2713
+
+# -DCOLLISION_CHECK if you believe that SHA1's
+# 1461501637330902918203684832716283019655932542976 hashes do not give you
+# enough guarantees about no collisions between objects ever hapenning.
+#
+# -DNSEC if you want git to care about sub-second file mtimes and ctimes.
+# Note that you need some new glibc (at least >2.2.4) for this, and it will
+# BREAK YOUR LOCAL DIFFS! show-diff and anything using it will likely randomly
+# break unless your underlying filesystem supports those sub-second times
+# (my ext3 doesn't).
+CFLAGS=-g -O3 -Wall
+
+CC=gcc
+
+
+PROG= update-cache show-diff init-db write-tree read-tree commit-tree \
+ cat-file fsck-cache checkout-cache diff-tree rev-tree show-files \
+ check-files ls-tree merge-base merge-cache
+
+all: $(PROG)
+
+install: $(PROG)
+ install $(PROG) $(HOME)/bin/
+
+LIBS= -lssl -lz
+
+init-db: init-db.o
+
+update-cache: update-cache.o read-cache.o
+ $(CC) $(CFLAGS) -o update-cache update-cache.o read-cache.o $(LIBS)
+
+show-diff: show-diff.o read-cache.o
+ $(CC) $(CFLAGS) -o show-diff show-diff.o read-cache.o $(LIBS)
+
+write-tree: write-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o write-tree write-tree.o read-cache.o $(LIBS)
+
+read-tree: read-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o read-tree read-tree.o read-cache.o $(LIBS)
+
+commit-tree: commit-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o commit-tree commit-tree.o read-cache.o $(LIBS)
+
+cat-file: cat-file.o read-cache.o
+ $(CC) $(CFLAGS) -o cat-file cat-file.o read-cache.o $(LIBS)
+
+fsck-cache: fsck-cache.o read-cache.o object.o commit.o tree.o blob.o
+ $(CC) $(CFLAGS) -o fsck-cache fsck-cache.o read-cache.o object.o commit.o tree.o blob.o $(LIBS)
+
+checkout-cache: checkout-cache.o read-cache.o
+ $(CC) $(CFLAGS) -o checkout-cache checkout-cache.o read-cache.o $(LIBS)
+
+diff-tree: diff-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o diff-tree diff-tree.o read-cache.o $(LIBS)
+
+rev-tree: rev-tree.o read-cache.o object.o commit.o tree.o blob.o
+ $(CC) $(CFLAGS) -o rev-tree rev-tree.o read-cache.o object.o commit.o tree.o blob.o $(LIBS)
+
+show-files: show-files.o read-cache.o
+ $(CC) $(CFLAGS) -o show-files show-files.o read-cache.o $(LIBS)
+
+check-files: check-files.o read-cache.o
+ $(CC) $(CFLAGS) -o check-files check-files.o read-cache.o $(LIBS)
+
+ls-tree: ls-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o ls-tree ls-tree.o read-cache.o $(LIBS)
+
+merge-base: merge-base.o read-cache.o object.o commit.o tree.o blob.o
+ $(CC) $(CFLAGS) -o merge-base merge-base.o read-cache.o object.o commit.o tree.o blob.o $(LIBS)
+
+merge-cache: merge-cache.o read-cache.o
+ $(CC) $(CFLAGS) -o merge-cache merge-cache.o read-cache.o $(LIBS)
+
+read-cache.o: cache.h
+show-diff.o: cache.h
+
+clean:
+ rm -f *.o $(PROG)
+
+backup: clean
+ cd .. ; tar czvf dircache.tar.gz dir-cache
+
+
+Node-path: branches/left-sub/bang
+Node-kind: file
+Node-action: add
+Node-copyfrom-rev: 17
+Node-copyfrom-path: branches/right/bang
+Text-copy-source-md5: 34c28f1d2dc6a9adeccc4265bf7516cb
+Text-copy-source-sha1: 0bc5bb345c0e71d28f784f12e0bd2d384c283062
+
+
+Node-path: branches/left-sub/urkkk
+Node-kind: file
+Node-action: add
+Node-copyfrom-rev: 17
+Node-copyfrom-path: branches/right/urkkk
+Text-copy-source-md5: 5889c8392e16251b0c80927607a03036
+Text-copy-source-sha1: 3934264d277a0cf886b6b1c7f2b9e56da2525302
+
+
+Revision-number: 19
+Prop-content-length: 128
+Content-length: 128
+
+K 7
+svn:log
+V 30
+(r19) left sub-branch update 2
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-01-19T04:14:32.049244Z
+PROPS-END
+
+Node-path: branches/left-sub/wham_eth
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 6
+Text-content-md5: 757bcd5818572ef3f9580052617c1c8b
+Text-content-sha1: b165019b005c199237ba822c4404e771e93b654a
+Content-length: 16
+
+PROPS-END
+zowie
+
+
+Revision-number: 20
+Prop-content-length: 117
+Content-length: 117
+
+K 7
+svn:log
+V 19
+(r20) left update 5
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-01-19T04:14:33.049332Z
+PROPS-END
+
+Node-path: branches/left/glurpp
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 8
+Text-content-md5: 14a169f628e0bb59df9c2160649d0a30
+Text-content-sha1: ef7d929e52177767ecfcd28941f6b7f04b4131e3
+Content-length: 18
+
+PROPS-END
+eee_yow
+
+
+Revision-number: 21
+Prop-content-length: 146
+Content-length: 146
+
+K 7
+svn:log
+V 48
+(r21) Cherry-pick left sub-branch commit to left
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-01-19T04:14:36.041839Z
+PROPS-END
+
+Node-path: branches/left
+Node-kind: dir
+Node-action: change
+Prop-content-length: 56
+Content-length: 56
+
+K 13
+svn:mergeinfo
+V 21
+/branches/left-sub:19
+PROPS-END
+
+
+Node-path: branches/left/wham_eth
+Node-kind: file
+Node-action: add
+Node-copyfrom-rev: 19
+Node-copyfrom-path: branches/left-sub/wham_eth
+Text-copy-source-md5: 757bcd5818572ef3f9580052617c1c8b
+Text-copy-source-sha1: b165019b005c199237ba822c4404e771e93b654a
+
+
+Revision-number: 22
+Prop-content-length: 133
+Content-length: 133
+
+K 7
+svn:log
+V 35
+(r22) Merge left sub-branch to left
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-01-19T04:14:39.045014Z
+PROPS-END
+
+Node-path: branches/left
+Node-kind: dir
+Node-action: change
+Prop-content-length: 79
+Content-length: 79
+
+K 13
+svn:mergeinfo
+V 44
+/branches/left-sub:4-19
+/branches/right:2-17
+PROPS-END
+
+
+Node-path: branches/left/Makefile
+Node-kind: file
+Node-action: change
+Text-content-length: 2713
+Text-content-md5: 0afbe34f244cd662b1f97d708c687f90
+Text-content-sha1: 46d9377d783e67a9b581da110352e799517c8a14
+Content-length: 2713
+
+# -DCOLLISION_CHECK if you believe that SHA1's
+# 1461501637330902918203684832716283019655932542976 hashes do not give you
+# enough guarantees about no collisions between objects ever hapenning.
+#
+# -DNSEC if you want git to care about sub-second file mtimes and ctimes.
+# Note that you need some new glibc (at least >2.2.4) for this, and it will
+# BREAK YOUR LOCAL DIFFS! show-diff and anything using it will likely randomly
+# break unless your underlying filesystem supports those sub-second times
+# (my ext3 doesn't).
+CFLAGS=-g -O3 -Wall
+
+CC=gcc
+
+
+PROG= update-cache show-diff init-db write-tree read-tree commit-tree \
+ cat-file fsck-cache checkout-cache diff-tree rev-tree show-files \
+ check-files ls-tree merge-base merge-cache
+
+all: $(PROG)
+
+install: $(PROG)
+ install $(PROG) $(HOME)/bin/
+
+LIBS= -lssl -lz
+
+init-db: init-db.o
+
+update-cache: update-cache.o read-cache.o
+ $(CC) $(CFLAGS) -o update-cache update-cache.o read-cache.o $(LIBS)
+
+show-diff: show-diff.o read-cache.o
+ $(CC) $(CFLAGS) -o show-diff show-diff.o read-cache.o $(LIBS)
+
+write-tree: write-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o write-tree write-tree.o read-cache.o $(LIBS)
+
+read-tree: read-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o read-tree read-tree.o read-cache.o $(LIBS)
+
+commit-tree: commit-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o commit-tree commit-tree.o read-cache.o $(LIBS)
+
+cat-file: cat-file.o read-cache.o
+ $(CC) $(CFLAGS) -o cat-file cat-file.o read-cache.o $(LIBS)
+
+fsck-cache: fsck-cache.o read-cache.o object.o commit.o tree.o blob.o
+ $(CC) $(CFLAGS) -o fsck-cache fsck-cache.o read-cache.o object.o commit.o tree.o blob.o $(LIBS)
+
+checkout-cache: checkout-cache.o read-cache.o
+ $(CC) $(CFLAGS) -o checkout-cache checkout-cache.o read-cache.o $(LIBS)
+
+diff-tree: diff-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o diff-tree diff-tree.o read-cache.o $(LIBS)
+
+rev-tree: rev-tree.o read-cache.o object.o commit.o tree.o blob.o
+ $(CC) $(CFLAGS) -o rev-tree rev-tree.o read-cache.o object.o commit.o tree.o blob.o $(LIBS)
+
+show-files: show-files.o read-cache.o
+ $(CC) $(CFLAGS) -o show-files show-files.o read-cache.o $(LIBS)
+
+check-files: check-files.o read-cache.o
+ $(CC) $(CFLAGS) -o check-files check-files.o read-cache.o $(LIBS)
+
+ls-tree: ls-tree.o read-cache.o
+ $(CC) $(CFLAGS) -o ls-tree ls-tree.o read-cache.o $(LIBS)
+
+merge-base: merge-base.o read-cache.o object.o commit.o tree.o blob.o
+ $(CC) $(CFLAGS) -o merge-base merge-base.o read-cache.o object.o commit.o tree.o blob.o $(LIBS)
+
+merge-cache: merge-cache.o read-cache.o
+ $(CC) $(CFLAGS) -o merge-cache merge-cache.o read-cache.o $(LIBS)
+
+read-cache.o: cache.h
+show-diff.o: cache.h
+
+clean:
+ rm -f *.o $(PROG)
+
+backup: clean
+ cd .. ; tar czvf dircache.tar.gz dir-cache
+
+
+Node-path: branches/left/README
+Node-kind: file
+Node-action: add
+Node-copyfrom-rev: 18
+Node-copyfrom-path: branches/left-sub/README
+Text-copy-source-md5: fdbcfb6be9afe1121862143f226b51cf
+Text-copy-source-sha1: 1d1f5ea4ceb584337ffe59b8980d92e3b78dfef4
+
+
+Node-path: branches/left/bang
+Node-kind: file
+Node-action: add
+Node-copyfrom-rev: 18
+Node-copyfrom-path: branches/left-sub/bang
+Text-copy-source-md5: 34c28f1d2dc6a9adeccc4265bf7516cb
+Text-copy-source-sha1: 0bc5bb345c0e71d28f784f12e0bd2d384c283062
+
+
+Node-path: branches/left/urkkk
+Node-kind: file
+Node-action: add
+Node-copyfrom-rev: 18
+Node-copyfrom-path: branches/left-sub/urkkk
+Text-copy-source-md5: 5889c8392e16251b0c80927607a03036
+Text-copy-source-sha1: 3934264d277a0cf886b6b1c7f2b9e56da2525302
+
+
+Revision-number: 23
+Prop-content-length: 125
+Content-length: 125
+
+K 7
+svn:log
+V 27
+(r23) Merge left to trunk 2
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-01-19T04:14:42.052798Z
+PROPS-END
+
+Node-path: trunk
+Node-kind: dir
+Node-action: change
+Prop-content-length: 99
+Content-length: 99
+
+K 13
+svn:mergeinfo
+V 64
+/branches/left:2-22
+/branches/left-sub:4-19
+/branches/right:2-17
+PROPS-END
+
+
+Node-path: trunk/README
+Node-kind: file
+Node-action: add
+Node-copyfrom-rev: 22
+Node-copyfrom-path: branches/left/README
+Text-copy-source-md5: fdbcfb6be9afe1121862143f226b51cf
+Text-copy-source-sha1: 1d1f5ea4ceb584337ffe59b8980d92e3b78dfef4
+
+
+Node-path: trunk/glurpp
+Node-kind: file
+Node-action: add
+Node-copyfrom-rev: 22
+Node-copyfrom-path: branches/left/glurpp
+Text-copy-source-md5: 14a169f628e0bb59df9c2160649d0a30
+Text-copy-source-sha1: ef7d929e52177767ecfcd28941f6b7f04b4131e3
+
+
+Node-path: trunk/urkkk
+Node-kind: file
+Node-action: add
+Node-copyfrom-rev: 22
+Node-copyfrom-path: branches/left/urkkk
+Text-copy-source-md5: 5889c8392e16251b0c80927607a03036
+Text-copy-source-sha1: 3934264d277a0cf886b6b1c7f2b9e56da2525302
+
+
+Node-path: trunk/wham_eth
+Node-kind: file
+Node-action: add
+Node-copyfrom-rev: 22
+Node-copyfrom-path: branches/left/wham_eth
+Text-copy-source-md5: 757bcd5818572ef3f9580052617c1c8b
+Text-copy-source-sha1: b165019b005c199237ba822c4404e771e93b654a
+
+
+Node-path: trunk/zlonk
+Node-kind: file
+Node-action: add
+Node-copyfrom-rev: 22
+Node-copyfrom-path: branches/left/zlonk
+Text-copy-source-md5: 8b9d8c7c2aaa6167e7d3407a773bbbba
+Text-copy-source-sha1: 9716527ebd70a75c27625cacbeb2d897c6e86178
+
+
+Revision-number: 24
+Prop-content-length: 130
+Content-length: 130
+
+K 7
+svn:log
+V 32
+(r24) non-merge right to trunk 2
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-01-19T04:14:44.038434Z
+PROPS-END
+
+Node-path: trunk
+Node-kind: dir
+Node-action: change
+Prop-content-length: 99
+Content-length: 99
+
+K 13
+svn:mergeinfo
+V 64
+/branches/left:2-22
+/branches/left-sub:4-19
+/branches/right:2-22
+PROPS-END
+
+
+Revision-number: 25
+Prop-content-length: 129
+Content-length: 129
+
+K 7
+svn:log
+V 31
+(r25) make b1 branch from trunk
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-02-22T06:18:56.084589Z
+PROPS-END
+
+Node-path: branches/b1
+Node-kind: dir
+Node-action: add
+Node-copyfrom-rev: 24
+Node-copyfrom-path: trunk
+
+
+Revision-number: 26
+Prop-content-length: 129
+Content-length: 129
+
+K 7
+svn:log
+V 31
+(r26) make b2 branch from trunk
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-02-22T06:18:59.076940Z
+PROPS-END
+
+Node-path: branches/b2
+Node-kind: dir
+Node-action: add
+Node-copyfrom-rev: 25
+Node-copyfrom-path: trunk
+
+
+Revision-number: 27
+Prop-content-length: 115
+Content-length: 115
+
+K 7
+svn:log
+V 17
+(r27) b2 update 1
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-02-22T06:19:01.095762Z
+PROPS-END
+
+Node-path: branches/b2/b2file
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 3
+Text-content-md5: 5edbdd57cba621eb3c6e601bf563b4dc
+Text-content-sha1: 9d4b38049776bd0a2074d67cad23f8eaed35a3b3
+Content-length: 13
+
+PROPS-END
+b2
+
+
+Revision-number: 28
+Prop-content-length: 115
+Content-length: 115
+
+K 7
+svn:log
+V 17
+(r28) b1 update 1
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-02-22T06:19:03.097465Z
+PROPS-END
+
+Node-path: branches/b1/b1file
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 3
+Text-content-md5: 08778dfd9ac4f603231896aba7aad523
+Text-content-sha1: b551771aa4ad5b14123fc3bd98d89db2bc0edd4f
+Content-length: 13
+
+PROPS-END
+b1
+
+
+Revision-number: 29
+Prop-content-length: 121
+Content-length: 121
+
+K 7
+svn:log
+V 23
+(r29) Merge b1 to trunk
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-02-22T06:19:06.073175Z
+PROPS-END
+
+Node-path: trunk
+Node-kind: dir
+Node-action: change
+Prop-content-length: 118
+Content-length: 118
+
+K 13
+svn:mergeinfo
+V 83
+/branches/b1:25-28
+/branches/left:2-22
+/branches/left-sub:4-19
+/branches/right:2-22
+PROPS-END
+
+
+Node-path: trunk/b1file
+Node-kind: file
+Node-action: add
+Node-copyfrom-rev: 28
+Node-copyfrom-path: branches/b1/b1file
+Text-copy-source-md5: 08778dfd9ac4f603231896aba7aad523
+Text-copy-source-sha1: b551771aa4ad5b14123fc3bd98d89db2bc0edd4f
+
+
+Revision-number: 30
+Prop-content-length: 143
+Content-length: 143
+
+K 7
+svn:log
+V 45
+(r30) trunk commit before merging trunk to b2
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-02-22T06:19:08.096353Z
+PROPS-END
+
+Node-path: trunk/trunkfile
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 6
+Text-content-md5: edf45fe5c98c5367733b39bbb2bb20d9
+Text-content-sha1: 7361d1685e5c86dfc523620cfaf598f196f86239
+Content-length: 16
+
+PROPS-END
+trunk
+
+
+Revision-number: 31
+Prop-content-length: 121
+Content-length: 121
+
+K 7
+svn:log
+V 23
+(r31) Merge trunk to b2
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-02-22T06:19:11.081541Z
+PROPS-END
+
+Node-path: branches/b2
+Node-kind: dir
+Node-action: change
+Prop-content-length: 131
+Content-length: 131
+
+K 13
+svn:mergeinfo
+V 96
+/branches/b1:25-28
+/branches/left:2-22
+/branches/left-sub:4-19
+/branches/right:2-22
+/trunk:26-30
+PROPS-END
+
+
+Node-path: branches/b2/b1file
+Node-kind: file
+Node-action: add
+Node-copyfrom-rev: 30
+Node-copyfrom-path: trunk/b1file
+Text-copy-source-md5: 08778dfd9ac4f603231896aba7aad523
+Text-copy-source-sha1: b551771aa4ad5b14123fc3bd98d89db2bc0edd4f
+
+
+Node-path: branches/b2/trunkfile
+Node-kind: file
+Node-action: add
+Node-copyfrom-rev: 30
+Node-copyfrom-path: trunk/trunkfile
+Text-copy-source-md5: edf45fe5c98c5367733b39bbb2bb20d9
+Text-copy-source-sha1: 7361d1685e5c86dfc523620cfaf598f196f86239
+
+
+Revision-number: 32
+Prop-content-length: 121
+Content-length: 121
+
+K 7
+svn:log
+V 23
+(r32) Merge b2 to trunk
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-02-22T06:19:14.117939Z
+PROPS-END
+
+Node-path: trunk
+Node-kind: dir
+Node-action: change
+Prop-content-length: 138
+Content-length: 138
+
+K 13
+svn:mergeinfo
+V 102
+/branches/b1:25-28
+/branches/b2:26-31
+/branches/left:2-22
+/branches/left-sub:4-19
+/branches/right:2-22
+PROPS-END
+
+
+Node-path: trunk/b2file
+Node-kind: file
+Node-action: add
+Node-copyfrom-rev: 31
+Node-copyfrom-path: branches/b2/b2file
+Text-copy-source-md5: 5edbdd57cba621eb3c6e601bf563b4dc
+Text-copy-source-sha1: 9d4b38049776bd0a2074d67cad23f8eaed35a3b3
+
+
+Revision-number: 33
+Prop-content-length: 145
+Content-length: 145
+
+K 7
+svn:log
+V 47
+(r33) make f1 branch from trunk with a new file
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-02-22T06:19:17.105832Z
+PROPS-END
+
+Node-path: branches/f1
+Node-kind: dir
+Node-action: add
+Node-copyfrom-rev: 32
+Node-copyfrom-path: trunk
+
+
+Node-path: branches/f1/f1file
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 3
+Text-content-md5: 2b1abc6b6c5c0018851f9f8e6475563b
+Text-content-sha1: aece6dfba588900e00d95601d22b4408d49580af
+Content-length: 13
+
+PROPS-END
+f1
+
+
+Revision-number: 34
+Prop-content-length: 145
+Content-length: 145
+
+K 7
+svn:log
+V 47
+(r34) make f2 branch from trunk with a new file
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-02-22T06:19:20.110057Z
+PROPS-END
+
+Node-path: branches/f2
+Node-kind: dir
+Node-action: add
+Node-copyfrom-rev: 33
+Node-copyfrom-path: trunk
+
+
+Node-path: branches/f2/f2file
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 3
+Text-content-md5: 575c5638d60271457e54ab7d07309502
+Text-content-sha1: 1c49a440c352f3473efa9512255033b94dc7def0
+Content-length: 13
+
+PROPS-END
+f2
+
+
+Revision-number: 35
+Prop-content-length: 128
+Content-length: 128
+
+K 7
+svn:log
+V 30
+(r35) Merge f1 and f2 to trunk
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-02-22T06:19:24.081490Z
+PROPS-END
+
+Node-path: trunk
+Node-kind: dir
+Node-action: change
+Prop-content-length: 173
+Content-length: 173
+
+K 13
+svn:mergeinfo
+V 137
+/branches/b1:25-28
+/branches/b2:26-31
+/branches/f1:33-34
+/branches/f2:34
+/branches/left:2-22
+/branches/left-sub:4-19
+/branches/right:2-22
+PROPS-END
+
+
+Node-path: trunk/f1file
+Node-kind: file
+Node-action: add
+Node-copyfrom-rev: 34
+Node-copyfrom-path: branches/f1/f1file
+Text-copy-source-md5: 2b1abc6b6c5c0018851f9f8e6475563b
+Text-copy-source-sha1: aece6dfba588900e00d95601d22b4408d49580af
+
+
+Node-path: trunk/f2file
+Node-kind: file
+Node-action: add
+Node-copyfrom-rev: 34
+Node-copyfrom-path: branches/f2/f2file
+Text-copy-source-md5: 575c5638d60271457e54ab7d07309502
+Text-copy-source-sha1: 1c49a440c352f3473efa9512255033b94dc7def0
+
+
+Revision-number: 36
+Prop-content-length: 135
+Content-length: 135
+
+K 7
+svn:log
+V 37
+(r36) add subdirectory to left branch
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-02-22T06:19:26.113516Z
+PROPS-END
+
+Node-path: branches/left/subdir
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Node-path: branches/left/subdir/cowboy
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 7
+Text-content-md5: f1d6530278ad409e68cc675476ad995f
+Text-content-sha1: 732d9e3e5c391ffd767a98b45ddcc848de778cea
+Content-length: 17
+
+PROPS-END
+Yeehaw
+
+
+Revision-number: 37
+Prop-content-length: 123
+Content-length: 123
+
+K 7
+svn:log
+V 25
+(r37) merge left to trunk
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-02-22T06:19:29.073699Z
+PROPS-END
+
+Node-path: trunk
+Node-kind: dir
+Node-action: change
+Prop-content-length: 173
+Content-length: 173
+
+K 13
+svn:mergeinfo
+V 137
+/branches/b1:25-28
+/branches/b2:26-31
+/branches/f1:33-34
+/branches/f2:34
+/branches/left:2-36
+/branches/left-sub:4-19
+/branches/right:2-22
+PROPS-END
+
+
+Node-path: trunk/subdir
+Node-kind: dir
+Node-action: add
+Node-copyfrom-rev: 36
+Node-copyfrom-path: branches/left/subdir
+
+
+Revision-number: 38
+Prop-content-length: 123
+Content-length: 123
+
+K 7
+svn:log
+V 25
+(r38) make partial branch
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-02-22T06:19:32.072243Z
+PROPS-END
+
+Node-path: branches/partial
+Node-kind: dir
+Node-action: add
+Node-copyfrom-rev: 37
+Node-copyfrom-path: trunk/subdir
+
+
+Revision-number: 39
+Prop-content-length: 118
+Content-length: 118
+
+K 7
+svn:log
+V 20
+(r39) partial update
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-02-22T06:19:34.097961Z
+PROPS-END
+
+Node-path: branches/partial/palindromes
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 8
+Text-content-md5: 5d1c2024fb5efc4eef812856df1b080c
+Text-content-sha1: 5f8509ddd14c91a52864dd1447344e706f9bbc69
+Content-length: 18
+
+PROPS-END
+racecar
+
+
+Revision-number: 40
+Prop-content-length: 126
+Content-length: 126
+
+K 7
+svn:log
+V 28
+(r40) merge partial to trunk
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-02-22T06:19:37.080211Z
+PROPS-END
+
+Node-path: trunk/subdir
+Node-kind: dir
+Node-action: change
+Prop-content-length: 246
+Content-length: 246
+
+K 13
+svn:mergeinfo
+V 210
+/branches/b1/subdir:25-28
+/branches/b2/subdir:26-31
+/branches/f1/subdir:33-34
+/branches/f2/subdir:34
+/branches/left/subdir:2-36
+/branches/left-sub/subdir:4-19
+/branches/partial:38-39
+/branches/right/subdir:2-22
+PROPS-END
+
+
+Node-path: trunk/subdir/palindromes
+Node-kind: file
+Node-action: add
+Node-copyfrom-rev: 39
+Node-copyfrom-path: branches/partial/palindromes
+Text-copy-source-md5: 5d1c2024fb5efc4eef812856df1b080c
+Text-copy-source-sha1: 5f8509ddd14c91a52864dd1447344e706f9bbc69
+
+
+Revision-number: 41
+Prop-content-length: 116
+Content-length: 116
+
+K 7
+svn:log
+V 18
+(r41) tagging v1.0
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-02-22T06:19:40.083460Z
+PROPS-END
+
+Node-path: tags/v1.0
+Node-kind: dir
+Node-action: add
+Node-copyfrom-rev: 40
+Node-copyfrom-path: trunk
+
+
+Revision-number: 42
+Prop-content-length: 131
+Content-length: 131
+
+K 7
+svn:log
+V 33
+(r42) make bugfix branch from tag
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-02-22T06:19:43.118075Z
+PROPS-END
+
+Node-path: branches/bugfix
+Node-kind: dir
+Node-action: add
+Node-copyfrom-rev: 41
+Node-copyfrom-path: tags/v1.0
+
+
+Revision-number: 43
+Prop-content-length: 120
+Content-length: 120
+
+K 7
+svn:log
+V 22
+(r43) commit to bugfix
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-02-22T06:19:45.079536Z
+PROPS-END
+
+Node-path: branches/bugfix/subdir/palindromes
+Node-kind: file
+Node-action: change
+Text-content-length: 14
+Text-content-md5: 3b12d98578a3f4320ba97e66da54fe5f
+Text-content-sha1: 672931c9e8ac2c408209efab2f015638b6d64042
+Content-length: 14
+
+racecar
+kayak
+
+
+Revision-number: 44
+Prop-content-length: 125
+Content-length: 125
+
+K 7
+svn:log
+V 27
+(r44) Merge BUGFIX to TRUNK
+K 10
+svn:author
+V 3
+adm
+K 8
+svn:date
+V 27
+2010-02-22T06:19:48.078914Z
+PROPS-END
+
+Node-path: trunk
+Node-kind: dir
+Node-action: change
+Prop-content-length: 210
+Content-length: 210
+
+K 13
+svn:mergeinfo
+V 174
+/branches/b1:25-28
+/branches/b2:26-31
+/branches/bugfix:42-43
+/branches/f1:33-34
+/branches/f2:34
+/branches/left:2-36
+/branches/left-sub:4-19
+/branches/right:2-22
+/tags/v1.0:41
+PROPS-END
+
+
+Node-path: trunk/subdir
+Node-kind: dir
+Node-action: change
+Prop-content-length: 297
+Content-length: 297
+
+K 13
+svn:mergeinfo
+V 261
+/branches/b1/subdir:25-28
+/branches/b2/subdir:26-31
+/branches/bugfix/subdir:42-43
+/branches/f1/subdir:33-34
+/branches/f2/subdir:34
+/branches/left/subdir:2-36
+/branches/left-sub/subdir:4-19
+/branches/partial:38-39
+/branches/right/subdir:2-22
+/tags/v1.0/subdir:41
+PROPS-END
+
+
+Node-path: trunk/subdir/palindromes
+Node-kind: file
+Node-action: change
+Text-content-length: 14
+Text-content-md5: 3b12d98578a3f4320ba97e66da54fe5f
+Text-content-sha1: 672931c9e8ac2c408209efab2f015638b6d64042
+Content-length: 14
+
+racecar
+kayak
+
+
diff --git a/t/t9152-svn-empty-dirs-after-gc.sh b/t/t9152-svn-empty-dirs-after-gc.sh
new file mode 100755
index 0000000..a597c42
--- /dev/null
+++ b/t/t9152-svn-empty-dirs-after-gc.sh
@@ -0,0 +1,40 @@
+#!/bin/sh
+#
+# Copyright (c) 2009 Robert Zeh
+
+test_description='git svn creates empty directories, calls git gc, makes sure they are still empty'
+. ./lib-git-svn.sh
+
+test_expect_success 'initialize repo' '
+ for i in a b c d d/e d/e/f "weird file name"
+ do
+ svn_cmd mkdir -m "mkdir $i" "$svnrepo"/"$i" || return 1
+ done
+'
+
+test_expect_success 'clone' 'git svn clone "$svnrepo" cloned'
+
+test_expect_success 'git svn gc runs' '
+ (
+ cd cloned &&
+ git svn gc
+ )
+'
+
+test_expect_success 'git svn mkdirs recreates empty directories after git svn gc' '
+ (
+ cd cloned &&
+ rm -r * &&
+ git svn mkdirs &&
+ for i in a b c d d/e d/e/f "weird file name"
+ do
+ if ! test -d "$i"
+ then
+ echo >&2 "$i does not exist" &&
+ exit 1
+ fi
+ done
+ )
+'
+
+test_done
diff --git a/t/t9153-git-svn-rewrite-uuid.sh b/t/t9153-git-svn-rewrite-uuid.sh
new file mode 100755
index 0000000..8cb2b5c
--- /dev/null
+++ b/t/t9153-git-svn-rewrite-uuid.sh
@@ -0,0 +1,25 @@
+#!/bin/sh
+#
+# Copyright (c) 2010 Jay Soffian
+#
+
+test_description='git svn --rewrite-uuid test'
+
+. ./lib-git-svn.sh
+
+uuid=6cc8ada4-5932-4b4a-8242-3534ed8a3232
+
+test_expect_success 'load svn repo' "
+ svnadmin load -q '$rawsvnrepo' < '$TEST_DIRECTORY/t9153/svn.dump' &&
+ git svn init --minimize-url --rewrite-uuid='$uuid' '$svnrepo' &&
+ git svn fetch
+ "
+
+test_expect_success 'verify uuid' "
+ git cat-file commit refs/remotes/git-svn~0 >actual &&
+ grep '^git-svn-id: .*@2 $uuid$' actual &&
+ git cat-file commit refs/remotes/git-svn~1 >actual &&
+ grep '^git-svn-id: .*@1 $uuid$' actual
+ "
+
+test_done
diff --git a/t/t9153/svn.dump b/t/t9153/svn.dump
new file mode 100644
index 0000000..0ddfe70
--- /dev/null
+++ b/t/t9153/svn.dump
@@ -0,0 +1,75 @@
+SVN-fs-dump-format-version: 2
+
+UUID: b4885626-c94f-4a6c-b179-00c030fc68e8
+
+Revision-number: 0
+Prop-content-length: 56
+Content-length: 56
+
+K 8
+svn:date
+V 27
+2010-01-23T06:41:03.908576Z
+PROPS-END
+
+Revision-number: 1
+Prop-content-length: 109
+Content-length: 109
+
+K 7
+svn:log
+V 11
+initial foo
+K 10
+svn:author
+V 3
+jay
+K 8
+svn:date
+V 27
+2010-01-23T06:41:48.353776Z
+PROPS-END
+
+Node-path: foo
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 4
+Text-content-md5: d3b07384d113edec49eaa6238ad5ff00
+Text-content-sha1: f1d2d2f924e986ac86fdf7b36c94bcdf32beec15
+Content-length: 14
+
+PROPS-END
+foo
+
+
+Revision-number: 2
+Prop-content-length: 110
+Content-length: 110
+
+K 7
+svn:log
+V 12
+now with bar
+K 10
+svn:author
+V 3
+jay
+K 8
+svn:date
+V 27
+2010-01-23T06:42:14.214640Z
+PROPS-END
+
+Node-path: foo
+Node-kind: file
+Node-action: change
+Text-content-length: 8
+Text-content-md5: f47c75614087a8dd938ba4acff252494
+Text-content-sha1: 4e48e2c9a3d2ca8a708cb0cc545700544efb5021
+Content-length: 8
+
+foo
+bar
+
+
diff --git a/t/t9154-git-svn-fancy-glob.sh b/t/t9154-git-svn-fancy-glob.sh
new file mode 100755
index 0000000..a0150f0
--- /dev/null
+++ b/t/t9154-git-svn-fancy-glob.sh
@@ -0,0 +1,51 @@
+#!/bin/sh
+#
+# Copyright (c) 2010 Jay Soffian
+#
+
+test_description='git svn fancy glob test'
+
+. ./lib-git-svn.sh
+
+test_expect_success 'load svn repo' "
+ svnadmin load -q '$rawsvnrepo' < '$TEST_DIRECTORY/t9154/svn.dump' &&
+ git svn init --minimize-url -T trunk '$svnrepo' &&
+ git svn fetch
+ "
+
+test_expect_success 'add red branch' "
+ git config svn-remote.svn.branches 'branches/{red}:refs/remotes/*' &&
+ git svn fetch &&
+ git rev-parse refs/remotes/red &&
+ test_must_fail git rev-parse refs/remotes/green &&
+ test_must_fail git rev-parse refs/remotes/blue
+ "
+
+test_expect_success 'add gre branch' "
+ git config --file=.git/svn/.metadata --unset svn-remote.svn.branches-maxRev &&
+ git config svn-remote.svn.branches 'branches/{red,gre}:refs/remotes/*' &&
+ git svn fetch &&
+ git rev-parse refs/remotes/red &&
+ test_must_fail git rev-parse refs/remotes/green &&
+ test_must_fail git rev-parse refs/remotes/blue
+ "
+
+test_expect_success 'add green branch' "
+ git config --file=.git/svn/.metadata --unset svn-remote.svn.branches-maxRev &&
+ git config svn-remote.svn.branches 'branches/{red,green}:refs/remotes/*' &&
+ git svn fetch &&
+ git rev-parse refs/remotes/red &&
+ git rev-parse refs/remotes/green &&
+ test_must_fail git rev-parse refs/remotes/blue
+ "
+
+test_expect_success 'add all branches' "
+ git config --file=.git/svn/.metadata --unset svn-remote.svn.branches-maxRev &&
+ git config svn-remote.svn.branches 'branches/*:refs/remotes/*' &&
+ git svn fetch &&
+ git rev-parse refs/remotes/red &&
+ git rev-parse refs/remotes/green &&
+ git rev-parse refs/remotes/blue
+ "
+
+test_done
diff --git a/t/t9154/svn.dump b/t/t9154/svn.dump
new file mode 100644
index 0000000..3dfabb6
--- /dev/null
+++ b/t/t9154/svn.dump
@@ -0,0 +1,222 @@
+SVN-fs-dump-format-version: 2
+
+UUID: a18093a0-5f0b-44e0-8d88-8911ac7078db
+
+Revision-number: 0
+Prop-content-length: 56
+Content-length: 56
+
+K 8
+svn:date
+V 27
+2010-01-23T07:40:25.660053Z
+PROPS-END
+
+Revision-number: 1
+Prop-content-length: 104
+Content-length: 104
+
+K 7
+svn:log
+V 7
+initial
+K 10
+svn:author
+V 3
+jay
+K 8
+svn:date
+V 27
+2010-01-23T07:41:33.636365Z
+PROPS-END
+
+Node-path: trunk
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Node-path: trunk/foo
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 4
+Text-content-md5: d3b07384d113edec49eaa6238ad5ff00
+Text-content-sha1: f1d2d2f924e986ac86fdf7b36c94bcdf32beec15
+Content-length: 14
+
+PROPS-END
+foo
+
+
+Revision-number: 2
+Prop-content-length: 110
+Content-length: 110
+
+K 7
+svn:log
+V 12
+add branches
+K 10
+svn:author
+V 3
+jay
+K 8
+svn:date
+V 27
+2010-01-23T07:42:37.290694Z
+PROPS-END
+
+Node-path: branches
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Node-path: branches/blue
+Node-kind: dir
+Node-action: add
+Node-copyfrom-rev: 1
+Node-copyfrom-path: trunk
+
+
+Node-path: branches/green
+Node-kind: dir
+Node-action: add
+Node-copyfrom-rev: 1
+Node-copyfrom-path: trunk
+
+
+Node-path: branches/red
+Node-kind: dir
+Node-action: add
+Node-copyfrom-rev: 1
+Node-copyfrom-path: trunk
+
+
+Revision-number: 3
+Prop-content-length: 108
+Content-length: 108
+
+K 7
+svn:log
+V 10
+red change
+K 10
+svn:author
+V 3
+jay
+K 8
+svn:date
+V 27
+2010-01-23T07:43:02.208918Z
+PROPS-END
+
+Node-path: branches/red/foo
+Node-kind: file
+Node-action: change
+Text-content-length: 8
+Text-content-md5: 64c3c8cf7d0233ab7627623a68888bd1
+Text-content-sha1: 95a0492027876adfd3891ec71ee37b79ee44d640
+Content-length: 8
+
+foo
+red
+
+
+Revision-number: 4
+Prop-content-length: 110
+Content-length: 110
+
+K 7
+svn:log
+V 12
+green change
+K 10
+svn:author
+V 3
+jay
+K 8
+svn:date
+V 27
+2010-01-23T07:43:15.746586Z
+PROPS-END
+
+Node-path: branches/green/foo
+Node-kind: file
+Node-action: change
+Text-content-length: 10
+Text-content-md5: 0209b6450891abc033d5eaaa9d3a8023
+Text-content-sha1: 87fc3bef9faeec48c0cd61dfc9851db377fdccf7
+Content-length: 10
+
+foo
+green
+
+
+Revision-number: 5
+Prop-content-length: 109
+Content-length: 109
+
+K 7
+svn:log
+V 11
+blue change
+K 10
+svn:author
+V 3
+jay
+K 8
+svn:date
+V 27
+2010-01-23T07:43:29.364811Z
+PROPS-END
+
+Node-path: branches/blue/foo
+Node-kind: file
+Node-action: change
+Text-content-length: 9
+Text-content-md5: 9fbe4c13d0bae86386ae5209b2e6b275
+Text-content-sha1: cc4575083459a16f9aaef796c4a2456d64691ba0
+Content-length: 9
+
+foo
+blue
+
+
+Revision-number: 6
+Prop-content-length: 110
+Content-length: 110
+
+K 7
+svn:log
+V 12
+trunk change
+K 10
+svn:author
+V 3
+jay
+K 8
+svn:date
+V 27
+2010-01-23T07:44:01.313130Z
+PROPS-END
+
+Node-path: trunk/foo
+Node-kind: file
+Node-action: change
+Text-content-length: 10
+Text-content-md5: 1c4db977d7a57c3bae582aab87948516
+Text-content-sha1: 469c08df449e702cf2a1fe746244a9ef3f837fad
+Content-length: 10
+
+foo
+trunk
+
+
diff --git a/t/t9155-git-svn-fetch-deleted-tag.sh b/t/t9155-git-svn-fetch-deleted-tag.sh
new file mode 100755
index 0000000..3258374
--- /dev/null
+++ b/t/t9155-git-svn-fetch-deleted-tag.sh
@@ -0,0 +1,45 @@
+#!/bin/sh
+
+test_description='git svn fetch deleted tag'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./lib-git-svn.sh
+
+test_expect_success 'setup svn repo' '
+ mkdir -p import/trunk/subdir &&
+ mkdir -p import/branches &&
+ mkdir -p import/tags &&
+ echo "base" >import/trunk/subdir/file &&
+ svn_cmd import -m "import for git svn" import "$svnrepo" &&
+ rm -rf import &&
+
+ svn_cmd mkdir -m "create mybranch directory" "$svnrepo/branches/mybranch" &&
+ svn_cmd cp -m "create branch mybranch" "$svnrepo/trunk" "$svnrepo/branches/mybranch/trunk" &&
+
+ svn_cmd co "$svnrepo/trunk" svn_project &&
+ (cd svn_project &&
+ echo "trunk change" >>subdir/file &&
+ svn_cmd ci -m "trunk change" subdir/file &&
+
+ svn_cmd switch "$svnrepo/branches/mybranch/trunk" &&
+ echo "branch change" >>subdir/file &&
+ svn_cmd ci -m "branch change" subdir/file
+ ) &&
+
+ svn_cmd cp -m "create mytag attempt 1" -r5 "$svnrepo/trunk/subdir" "$svnrepo/tags/mytag" &&
+ svn_cmd rm -m "delete mytag attempt 1" "$svnrepo/tags/mytag" &&
+ svn_cmd cp -m "create mytag attempt 2" -r5 "$svnrepo/branches/mybranch/trunk/subdir" "$svnrepo/tags/mytag"
+'
+
+test_expect_success 'fetch deleted tags from same revision with checksum error' '
+ git svn init --stdlayout "$svnrepo" git_project &&
+ cd git_project &&
+ git svn fetch &&
+
+ git diff --exit-code origin/mybranch:trunk/subdir/file origin/tags/mytag:file &&
+ git diff --exit-code main:subdir/file origin/tags/mytag^:file
+'
+
+test_done
diff --git a/t/t9156-git-svn-fetch-deleted-tag-2.sh b/t/t9156-git-svn-fetch-deleted-tag-2.sh
new file mode 100755
index 0000000..2b75c40
--- /dev/null
+++ b/t/t9156-git-svn-fetch-deleted-tag-2.sh
@@ -0,0 +1,47 @@
+#!/bin/sh
+
+test_description='git svn fetch deleted tag 2'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./lib-git-svn.sh
+
+test_expect_success 'setup svn repo' '
+ mkdir -p import/branches &&
+ mkdir -p import/tags &&
+ mkdir -p import/trunk/subdir1 &&
+ mkdir -p import/trunk/subdir2 &&
+ mkdir -p import/trunk/subdir3 &&
+ echo "file1" >import/trunk/subdir1/file &&
+ echo "file2" >import/trunk/subdir2/file &&
+ echo "file3" >import/trunk/subdir3/file &&
+ svn_cmd import -m "import for git svn" import "$svnrepo" &&
+ rm -rf import &&
+
+ svn_cmd co "$svnrepo/trunk" svn_project &&
+ (cd svn_project &&
+ echo "change1" >>subdir1/file &&
+ echo "change2" >>subdir2/file &&
+ echo "change3" >>subdir3/file &&
+ svn_cmd ci -m "change" .
+ ) &&
+
+ svn_cmd cp -m "create mytag 1" -r2 "$svnrepo/trunk/subdir1" "$svnrepo/tags/mytag" &&
+ svn_cmd rm -m "delete mytag 1" "$svnrepo/tags/mytag" &&
+ svn_cmd cp -m "create mytag 2" -r2 "$svnrepo/trunk/subdir2" "$svnrepo/tags/mytag" &&
+ svn_cmd rm -m "delete mytag 2" "$svnrepo/tags/mytag" &&
+ svn_cmd cp -m "create mytag 3" -r2 "$svnrepo/trunk/subdir3" "$svnrepo/tags/mytag"
+'
+
+test_expect_success 'fetch deleted tags from same revision with no checksum error' '
+ git svn init --stdlayout "$svnrepo" git_project &&
+ cd git_project &&
+ git svn fetch &&
+
+ git diff --exit-code main:subdir3/file origin/tags/mytag:file &&
+ git diff --exit-code main:subdir2/file origin/tags/mytag^:file &&
+ git diff --exit-code main:subdir1/file origin/tags/mytag^^:file
+'
+
+test_done
diff --git a/t/t9157-git-svn-fetch-merge.sh b/t/t9157-git-svn-fetch-merge.sh
new file mode 100755
index 0000000..991d2aa
--- /dev/null
+++ b/t/t9157-git-svn-fetch-merge.sh
@@ -0,0 +1,58 @@
+#!/bin/sh
+#
+# Copyright (c) 2010 Steven Walter
+#
+
+test_description='git svn merge detection'
+. ./lib-git-svn.sh
+
+svn_ver="$(svn --version --quiet)"
+case $svn_ver in
+0.* | 1.[0-4].*)
+ skip_all="skipping git-svn test - SVN too old ($svn_ver)"
+ test_done
+ ;;
+esac
+
+test_expect_success 'initialize source svn repo' '
+ svn_cmd mkdir -m x "$svnrepo"/trunk &&
+ svn_cmd mkdir -m x "$svnrepo"/branches &&
+ svn_cmd co "$svnrepo"/trunk "$SVN_TREE" &&
+ (
+ cd "$SVN_TREE" &&
+ touch foo &&
+ svn add foo &&
+ svn commit -m "initial commit" &&
+ svn cp -m branch "$svnrepo"/trunk "$svnrepo"/branches/branch1 &&
+ touch bar &&
+ svn add bar &&
+ svn commit -m x &&
+ svn cp -m branch "$svnrepo"/trunk "$svnrepo"/branches/branch2 &&
+ svn switch "$svnrepo"/branches/branch1 &&
+ touch baz &&
+ svn add baz &&
+ svn commit -m x &&
+ svn switch "$svnrepo"/trunk &&
+ svn merge "$svnrepo"/branches/branch1 &&
+ svn commit -m "merge" &&
+ svn switch "$svnrepo"/branches/branch1 &&
+ svn commit -m x &&
+ svn switch "$svnrepo"/branches/branch2 &&
+ svn merge "$svnrepo"/branches/branch1 &&
+ svn commit -m "merge branch1" &&
+ svn switch "$svnrepo"/trunk &&
+ svn merge "$svnrepo"/branches/branch2 &&
+ svn resolved baz &&
+ svn commit -m "merge branch2"
+ ) &&
+ rm -rf "$SVN_TREE"
+'
+
+test_expect_success 'clone svn repo' '
+ git svn init -s "$svnrepo" &&
+ git svn fetch
+'
+
+test_expect_success 'verify merge commit' 'git rev-parse HEAD^2'
+
+test_done
diff --git a/t/t9158-git-svn-mergeinfo.sh b/t/t9158-git-svn-mergeinfo.sh
new file mode 100755
index 0000000..a875b45
--- /dev/null
+++ b/t/t9158-git-svn-mergeinfo.sh
@@ -0,0 +1,52 @@
+#!/bin/sh
+#
+# Copyright (c) 2010 Steven Walter
+#
+
+test_description='git svn mergeinfo propagation'
+
+. ./lib-git-svn.sh
+
+test_expect_success 'initialize source svn repo' '
+ svn_cmd mkdir -m x "$svnrepo"/trunk &&
+ svn_cmd co "$svnrepo"/trunk "$SVN_TREE" &&
+ (
+ cd "$SVN_TREE" &&
+ touch foo &&
+ svn_cmd add foo &&
+ svn_cmd commit -m "initial commit"
+ ) &&
+ rm -rf "$SVN_TREE"
+'
+
+test_expect_success 'clone svn repo' '
+ git svn init "$svnrepo"/trunk &&
+ git svn fetch
+'
+
+test_expect_success 'change svn:mergeinfo' '
+ touch bar &&
+ git add bar &&
+ git commit -m "bar" &&
+ git svn dcommit --mergeinfo="/branches/foo:1-10"
+'
+
+test_expect_success 'verify svn:mergeinfo' '
+ mergeinfo=$(svn_cmd propget svn:mergeinfo "$svnrepo"/trunk) &&
+ test "$mergeinfo" = "/branches/foo:1-10"
+'
+
+test_expect_success 'change svn:mergeinfo multiline' '
+ touch baz &&
+ git add baz &&
+ git commit -m "baz" &&
+ git svn dcommit --mergeinfo="/branches/bar:1-10 /branches/other:3-5,8,10-11"
+'
+
+test_expect_success 'verify svn:mergeinfo multiline' '
+ mergeinfo=$(svn_cmd propget svn:mergeinfo "$svnrepo"/trunk) &&
+ test "$mergeinfo" = "/branches/bar:1-10
+/branches/other:3-5,8,10-11"
+'
+
+test_done
diff --git a/t/t9159-git-svn-no-parent-mergeinfo.sh b/t/t9159-git-svn-no-parent-mergeinfo.sh
new file mode 100755
index 0000000..69e4815
--- /dev/null
+++ b/t/t9159-git-svn-no-parent-mergeinfo.sh
@@ -0,0 +1,41 @@
+#!/bin/sh
+test_description='git svn handling of root commits in merge ranges'
+. ./lib-git-svn.sh
+
+svn_ver="$(svn --version --quiet)"
+case $svn_ver in
+0.* | 1.[0-4].*)
+ skip_all="skipping git-svn test - SVN too old ($svn_ver)"
+ test_done
+ ;;
+esac
+
+test_expect_success 'test handling of root commits in merge ranges' '
+ mkdir -p init/trunk init/branches init/tags &&
+ echo "r1" > init/trunk/file.txt &&
+ svn_cmd import -m "initial import" init "$svnrepo" &&
+ svn_cmd co "$svnrepo" tmp &&
+ (
+ cd tmp &&
+ echo "r2" > trunk/file.txt &&
+ svn_cmd commit -m "Modify file.txt on trunk" &&
+ svn_cmd cp trunk@1 branches/a &&
+ svn_cmd commit -m "Create branch a from trunk r1" &&
+ svn_cmd propset svn:mergeinfo /trunk:1-2 branches/a &&
+ svn_cmd commit -m "Fake merge of trunk r2 into branch a" &&
+ mkdir branches/b &&
+ echo "r5" > branches/b/file2.txt &&
+ svn_cmd add branches/b &&
+ svn_cmd commit -m "Create branch b from thin air" &&
+ echo "r6" > branches/b/file2.txt &&
+ svn_cmd commit -m "Modify file2.txt on branch b" &&
+ svn_cmd cp branches/b@5 branches/c &&
+ svn_cmd commit -m "Create branch c from branch b r5" &&
+ svn_cmd propset svn:mergeinfo /branches/b:5-6 branches/c &&
+ svn_cmd commit -m "Fake merge of branch b r6 into branch c"
+ ) &&
+ git svn init -s "$svnrepo" &&
+ git svn fetch
+ '
+
+test_done
diff --git a/t/t9160-git-svn-preserve-empty-dirs.sh b/t/t9160-git-svn-preserve-empty-dirs.sh
new file mode 100755
index 0000000..36c6b1a
--- /dev/null
+++ b/t/t9160-git-svn-preserve-empty-dirs.sh
@@ -0,0 +1,152 @@
+#!/bin/sh
+#
+# Copyright (c) 2011 Ray Chen
+#
+
+test_description='git svn test (option --preserve-empty-dirs)
+
+This test uses git to clone a Subversion repository that contains empty
+directories, and checks that corresponding directories are created in the
+local Git repository with placeholder files.'
+
+. ./lib-git-svn.sh
+
+GIT_REPO=git-svn-repo
+
+test_expect_success 'initialize source svn repo containing empty dirs' '
+ svn_cmd mkdir -m x "$svnrepo"/trunk &&
+ svn_cmd co "$svnrepo"/trunk "$SVN_TREE" &&
+ (
+ cd "$SVN_TREE" &&
+ mkdir -p 1 2 3/a 3/b 4 5 6 &&
+ echo "First non-empty file" > 2/file1.txt &&
+ echo "Second non-empty file" > 2/file2.txt &&
+ echo "Third non-empty file" > 3/a/file1.txt &&
+ echo "Fourth non-empty file" > 3/b/file1.txt &&
+ svn_cmd add 1 2 3 4 5 6 &&
+ svn_cmd commit -m "initial commit" &&
+
+ mkdir 4/a &&
+ svn_cmd add 4/a &&
+ svn_cmd commit -m "nested empty directory" &&
+ mkdir 4/a/b &&
+ svn_cmd add 4/a/b &&
+ svn_cmd commit -m "deeply nested empty directory" &&
+ mkdir 4/a/b/c &&
+ svn_cmd add 4/a/b/c &&
+ svn_cmd commit -m "really deeply nested empty directory" &&
+ echo "Kill the placeholder file" > 4/a/b/c/foo &&
+ svn_cmd add 4/a/b/c/foo &&
+ svn_cmd commit -m "Regular file to remove placeholder" &&
+
+ svn_cmd del 2/file2.txt &&
+ svn_cmd del 3/b &&
+ svn_cmd commit -m "delete non-last entry in directory" &&
+
+ svn_cmd del 2/file1.txt &&
+ svn_cmd del 3/a &&
+ svn_cmd commit -m "delete last entry in directory" &&
+
+ echo "Conflict file" > 5/.placeholder &&
+ mkdir 6/.placeholder &&
+ svn_cmd add 5/.placeholder 6/.placeholder &&
+ svn_cmd commit -m "Placeholder Namespace conflict"
+ ) &&
+ rm -rf "$SVN_TREE"
+'
+
+test_expect_success 'clone svn repo with --preserve-empty-dirs' '
+ git svn clone "$svnrepo"/trunk --preserve-empty-dirs "$GIT_REPO"
+'
+
+# "$GIT_REPO"/1 should only contain the placeholder file.
+test_expect_success 'directory empty from inception' '
+ test -f "$GIT_REPO"/1/.gitignore &&
+ test $(find "$GIT_REPO"/1 -type f | wc -l) = "1"
+'
+
+# "$GIT_REPO"/2 and "$GIT_REPO"/3 should only contain the placeholder file.
+test_expect_success 'directory empty from subsequent svn commit' '
+ test -f "$GIT_REPO"/2/.gitignore &&
+ test $(find "$GIT_REPO"/2 -type f | wc -l) = "1" &&
+ test -f "$GIT_REPO"/3/.gitignore &&
+ test $(find "$GIT_REPO"/3 -type f | wc -l) = "1"
+'
+
+# No placeholder files should exist in "$GIT_REPO"/4, even though one was
+# generated for every sub-directory at some point in the repo's history.
+test_expect_success 'add entry to previously empty directory' '
+ test $(find "$GIT_REPO"/4 -type f | wc -l) = "1" &&
+ test -f "$GIT_REPO"/4/a/b/c/foo
+'
+
+# The HEAD~2 commit should not have introduced .gitignore placeholder files.
+test_expect_success 'remove non-last entry from directory' '
+ (
+ cd "$GIT_REPO" &&
+ git checkout HEAD~2
+ ) &&
+ test_path_is_missing "$GIT_REPO"/2/.gitignore &&
+ test_path_is_missing "$GIT_REPO"/3/.gitignore
+'
+
+# After re-cloning the repository with --placeholder-file specified, there
+# should be 5 files named ".placeholder" in the local Git repo.
+test_expect_success 'clone svn repo with --placeholder-file specified' '
+ rm -rf "$GIT_REPO" &&
+ git svn clone "$svnrepo"/trunk --preserve-empty-dirs \
+ --placeholder-file=.placeholder "$GIT_REPO" &&
+ find "$GIT_REPO" -type f -name ".placeholder" &&
+ test $(find "$GIT_REPO" -type f -name ".placeholder" | wc -l) = "5"
+'
+
+# "$GIT_REPO"/5/.placeholder should be a file, and non-empty.
+test_expect_success 'placeholder namespace conflict with file' '
+ test -s "$GIT_REPO"/5/.placeholder
+'
+
+# "$GIT_REPO"/6/.placeholder should be a directory, and the "$GIT_REPO"/6 tree
+# should only contain one file: the placeholder.
+test_expect_success 'placeholder namespace conflict with directory' '
+ test -d "$GIT_REPO"/6/.placeholder &&
+ test -f "$GIT_REPO"/6/.placeholder/.placeholder &&
+ test $(find "$GIT_REPO"/6 -type f | wc -l) = "1"
+'
+
+# Prepare a second set of svn commits to test persistence during rebase.
+test_expect_success 'second set of svn commits and rebase' '
+ svn_cmd co "$svnrepo"/trunk "$SVN_TREE" &&
+ (
+ cd "$SVN_TREE" &&
+ mkdir -p 7 &&
+ echo "This should remove placeholder" > 1/file1.txt &&
+ echo "This should not remove placeholder" > 5/file1.txt &&
+ svn_cmd add 7 1/file1.txt 5/file1.txt &&
+ svn_cmd commit -m "subsequent svn commit for persistence tests"
+ ) &&
+ rm -rf "$SVN_TREE" &&
+ (
+ cd "$GIT_REPO" &&
+ git svn rebase
+ )
+'
+
+# Check that --preserve-empty-dirs and --placeholder-file flag state
+# stays persistent over multiple invocations.
+test_expect_success 'flag persistence during subsqeuent rebase' '
+ test -f "$GIT_REPO"/7/.placeholder &&
+ test $(find "$GIT_REPO"/7 -type f | wc -l) = "1"
+'
+
+# Check that placeholder files are properly removed when unnecessary,
+# even across multiple invocations.
+test_expect_success 'placeholder list persistence during subsqeuent rebase' '
+ test -f "$GIT_REPO"/1/file1.txt &&
+ test $(find "$GIT_REPO"/1 -type f | wc -l) = "1" &&
+
+ test -f "$GIT_REPO"/5/file1.txt &&
+ test -f "$GIT_REPO"/5/.placeholder &&
+ test $(find "$GIT_REPO"/5 -type f | wc -l) = "2"
+'
+
+test_done
diff --git a/t/t9161-git-svn-mergeinfo-push.sh b/t/t9161-git-svn-mergeinfo-push.sh
new file mode 100755
index 0000000..f113aca
--- /dev/null
+++ b/t/t9161-git-svn-mergeinfo-push.sh
@@ -0,0 +1,103 @@
+#!/bin/sh
+#
+# Portions copyright (c) 2007, 2009 Sam Vilain
+# Portions copyright (c) 2011 Bryan Jacobs
+#
+
+test_description='git-svn svn mergeinfo propagation'
+
+. ./lib-git-svn.sh
+
+test_expect_success 'load svn dump' "
+ svnadmin load -q '$rawsvnrepo' \
+ < '$TEST_DIRECTORY/t9161/branches.dump' &&
+ git svn init --minimize-url -R svnmerge \
+ -T trunk -b branches '$svnrepo' &&
+ git svn fetch --all
+ "
+
+test_expect_success 'propagate merge information' '
+ git config svn.pushmergeinfo yes &&
+ git checkout origin/svnb1 &&
+ git merge --no-ff origin/svnb2 &&
+ git svn dcommit
+ '
+
+test_expect_success 'check svn:mergeinfo' '
+ mergeinfo=$(svn_cmd propget svn:mergeinfo "$svnrepo"/branches/svnb1) &&
+ test "$mergeinfo" = "/branches/svnb2:3,8"
+ '
+
+test_expect_success 'merge another branch' '
+ git merge --no-ff origin/svnb3 &&
+ git svn dcommit
+ '
+
+test_expect_success 'check primary parent mergeinfo respected' '
+ mergeinfo=$(svn_cmd propget svn:mergeinfo "$svnrepo"/branches/svnb1) &&
+ test "$mergeinfo" = "/branches/svnb2:3,8
+/branches/svnb3:4,9"
+ '
+
+test_expect_success 'merge existing merge' '
+ git merge --no-ff origin/svnb4 &&
+ git svn dcommit
+ '
+
+test_expect_success "check both parents' mergeinfo respected" '
+ mergeinfo=$(svn_cmd propget svn:mergeinfo "$svnrepo"/branches/svnb1) &&
+ test "$mergeinfo" = "/branches/svnb2:3,8
+/branches/svnb3:4,9
+/branches/svnb4:5-6,10-12
+/branches/svnb5:6,11"
+ '
+
+test_expect_success 'make further commits to branch' '
+ git checkout origin/svnb2 &&
+ touch newb2file &&
+ git add newb2file &&
+ git commit -m "later b2 commit" &&
+ touch newb2file-2 &&
+ git add newb2file-2 &&
+ git commit -m "later b2 commit 2" &&
+ git svn dcommit
+ '
+
+test_expect_success 'second forward merge' '
+ git checkout origin/svnb1 &&
+ git merge --no-ff origin/svnb2 &&
+ git svn dcommit
+ '
+
+test_expect_success 'check new mergeinfo added' '
+ mergeinfo=$(svn_cmd propget svn:mergeinfo "$svnrepo"/branches/svnb1) &&
+ test "$mergeinfo" = "/branches/svnb2:3,8,16-17
+/branches/svnb3:4,9
+/branches/svnb4:5-6,10-12
+/branches/svnb5:6,11"
+ '
+
+test_expect_success 'reintegration merge' '
+ git checkout origin/svnb4 &&
+ git merge --no-ff origin/svnb1 &&
+ git svn dcommit
+ '
+
+test_expect_success 'check reintegration mergeinfo' '
+ mergeinfo=$(svn_cmd propget svn:mergeinfo "$svnrepo"/branches/svnb4) &&
+ test "$mergeinfo" = "/branches/svnb1:2-4,7-9,13-18
+/branches/svnb2:3,8,16-17
+/branches/svnb3:4,9
+/branches/svnb5:6,11"
+ '
+
+test_expect_success 'dcommit a merge at the top of a stack' '
+ git checkout origin/svnb1 &&
+ touch anotherfile &&
+ git add anotherfile &&
+ git commit -m "a commit" &&
+ git merge origin/svnb4 &&
+ git svn dcommit
+ '
+
+test_done
diff --git a/t/t9161/branches.dump b/t/t9161/branches.dump
new file mode 100644
index 0000000..e61c3e7
--- /dev/null
+++ b/t/t9161/branches.dump
@@ -0,0 +1,374 @@
+SVN-fs-dump-format-version: 2
+
+UUID: 1ef08553-f2d1-45df-b38c-19af6b7c926d
+
+Revision-number: 0
+Prop-content-length: 56
+Content-length: 56
+
+K 8
+svn:date
+V 27
+2011-09-02T16:08:02.941384Z
+PROPS-END
+
+Revision-number: 1
+Prop-content-length: 114
+Content-length: 114
+
+K 7
+svn:log
+V 12
+Base commit
+
+K 10
+svn:author
+V 7
+bjacobs
+K 8
+svn:date
+V 27
+2011-09-02T16:08:27.205062Z
+PROPS-END
+
+Node-path: branches
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Node-path: trunk
+Node-kind: dir
+Node-action: add
+Prop-content-length: 10
+Content-length: 10
+
+PROPS-END
+
+
+Revision-number: 2
+Prop-content-length: 121
+Content-length: 121
+
+K 7
+svn:log
+V 19
+Create branch svnb1
+K 10
+svn:author
+V 7
+bjacobs
+K 8
+svn:date
+V 27
+2011-09-02T16:09:43.628137Z
+PROPS-END
+
+Node-path: branches/svnb1
+Node-kind: dir
+Node-action: add
+Node-copyfrom-rev: 1
+Node-copyfrom-path: trunk
+
+
+Revision-number: 3
+Prop-content-length: 121
+Content-length: 121
+
+K 7
+svn:log
+V 19
+Create branch svnb2
+K 10
+svn:author
+V 7
+bjacobs
+K 8
+svn:date
+V 27
+2011-09-02T16:09:46.339930Z
+PROPS-END
+
+Node-path: branches/svnb2
+Node-kind: dir
+Node-action: add
+Node-copyfrom-rev: 1
+Node-copyfrom-path: trunk
+
+
+Revision-number: 4
+Prop-content-length: 121
+Content-length: 121
+
+K 7
+svn:log
+V 19
+Create branch svnb3
+K 10
+svn:author
+V 7
+bjacobs
+K 8
+svn:date
+V 27
+2011-09-02T16:09:49.394515Z
+PROPS-END
+
+Node-path: branches/svnb3
+Node-kind: dir
+Node-action: add
+Node-copyfrom-rev: 1
+Node-copyfrom-path: trunk
+
+
+Revision-number: 5
+Prop-content-length: 121
+Content-length: 121
+
+K 7
+svn:log
+V 19
+Create branch svnb4
+K 10
+svn:author
+V 7
+bjacobs
+K 8
+svn:date
+V 27
+2011-09-02T16:09:54.114607Z
+PROPS-END
+
+Node-path: branches/svnb4
+Node-kind: dir
+Node-action: add
+Node-copyfrom-rev: 1
+Node-copyfrom-path: trunk
+
+
+Revision-number: 6
+Prop-content-length: 121
+Content-length: 121
+
+K 7
+svn:log
+V 19
+Create branch svnb5
+K 10
+svn:author
+V 7
+bjacobs
+K 8
+svn:date
+V 27
+2011-09-02T16:09:58.602623Z
+PROPS-END
+
+Node-path: branches/svnb5
+Node-kind: dir
+Node-action: add
+Node-copyfrom-rev: 1
+Node-copyfrom-path: trunk
+
+
+Revision-number: 7
+Prop-content-length: 110
+Content-length: 110
+
+K 7
+svn:log
+V 9
+b1 commit
+K 10
+svn:author
+V 7
+bjacobs
+K 8
+svn:date
+V 27
+2011-09-02T16:10:20.292369Z
+PROPS-END
+
+Node-path: branches/svnb1/b1file
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 0
+Text-content-md5: d41d8cd98f00b204e9800998ecf8427e
+Text-content-sha1: da39a3ee5e6b4b0d3255bfef95601890afd80709
+Content-length: 10
+
+PROPS-END
+
+
+Revision-number: 8
+Prop-content-length: 110
+Content-length: 110
+
+K 7
+svn:log
+V 9
+b2 commit
+K 10
+svn:author
+V 7
+bjacobs
+K 8
+svn:date
+V 27
+2011-09-02T16:10:38.429199Z
+PROPS-END
+
+Node-path: branches/svnb2/b2file
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 0
+Text-content-md5: d41d8cd98f00b204e9800998ecf8427e
+Text-content-sha1: da39a3ee5e6b4b0d3255bfef95601890afd80709
+Content-length: 10
+
+PROPS-END
+
+
+Revision-number: 9
+Prop-content-length: 110
+Content-length: 110
+
+K 7
+svn:log
+V 9
+b3 commit
+K 10
+svn:author
+V 7
+bjacobs
+K 8
+svn:date
+V 27
+2011-09-02T16:10:52.843023Z
+PROPS-END
+
+Node-path: branches/svnb3/b3file
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 0
+Text-content-md5: d41d8cd98f00b204e9800998ecf8427e
+Text-content-sha1: da39a3ee5e6b4b0d3255bfef95601890afd80709
+Content-length: 10
+
+PROPS-END
+
+
+Revision-number: 10
+Prop-content-length: 110
+Content-length: 110
+
+K 7
+svn:log
+V 9
+b4 commit
+K 10
+svn:author
+V 7
+bjacobs
+K 8
+svn:date
+V 27
+2011-09-02T16:11:17.489870Z
+PROPS-END
+
+Node-path: branches/svnb4/b4file
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 0
+Text-content-md5: d41d8cd98f00b204e9800998ecf8427e
+Text-content-sha1: da39a3ee5e6b4b0d3255bfef95601890afd80709
+Content-length: 10
+
+PROPS-END
+
+
+Revision-number: 11
+Prop-content-length: 110
+Content-length: 110
+
+K 7
+svn:log
+V 9
+b5 commit
+K 10
+svn:author
+V 7
+bjacobs
+K 8
+svn:date
+V 27
+2011-09-02T16:11:32.277404Z
+PROPS-END
+
+Node-path: branches/svnb5/b5file
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 0
+Text-content-md5: d41d8cd98f00b204e9800998ecf8427e
+Text-content-sha1: da39a3ee5e6b4b0d3255bfef95601890afd80709
+Content-length: 10
+
+PROPS-END
+
+
+Revision-number: 12
+Prop-content-length: 192
+Content-length: 192
+
+K 7
+svn:log
+V 90
+Merge remote-tracking branch 'svnb5' into HEAD
+
+* svnb5:
+ b5 commit
+ Create branch svnb5
+K 10
+svn:author
+V 7
+bjacobs
+K 8
+svn:date
+V 27
+2011-09-02T16:11:54.274722Z
+PROPS-END
+
+Node-path: branches/svnb4
+Node-kind: dir
+Node-action: change
+Prop-content-length: 56
+Content-length: 56
+
+K 13
+svn:mergeinfo
+V 21
+/branches/svnb5:6,11
+
+PROPS-END
+
+
+Node-path: branches/svnb4/b5file
+Node-kind: file
+Node-action: add
+Prop-content-length: 10
+Text-content-length: 0
+Text-content-md5: d41d8cd98f00b204e9800998ecf8427e
+Text-content-sha1: da39a3ee5e6b4b0d3255bfef95601890afd80709
+Content-length: 10
+
+PROPS-END
+
+
diff --git a/t/t9162-git-svn-dcommit-interactive.sh b/t/t9162-git-svn-dcommit-interactive.sh
new file mode 100755
index 0000000..b3ce033
--- /dev/null
+++ b/t/t9162-git-svn-dcommit-interactive.sh
@@ -0,0 +1,65 @@
+#!/bin/sh
+#
+# Copyright (c) 2011 Frédéric Heitzmann
+
+test_description='git svn dcommit --interactive series'
+
+. ./lib-git-svn.sh
+
+test_expect_success 'initialize repo' '
+ svn_cmd mkdir -m"mkdir test-interactive" "$svnrepo/test-interactive" &&
+ git svn clone "$svnrepo/test-interactive" test-interactive &&
+ cd test-interactive &&
+ touch foo && git add foo && git commit -m"foo: first commit" &&
+ git svn dcommit
+ '
+
+test_expect_success 'answers: y [\n] yes' '
+ (
+ echo "change #1" >> foo && git commit -a -m"change #1" &&
+ echo "change #2" >> foo && git commit -a -m"change #2" &&
+ echo "change #3" >> foo && git commit -a -m"change #3" &&
+ ( echo "y
+
+y" | GIT_SVN_NOTTY=1 git svn dcommit --interactive ) &&
+ test $(git rev-parse HEAD) = $(git rev-parse remotes/git-svn)
+ )
+ '
+
+test_expect_success 'answers: yes yes no' '
+ (
+ echo "change #1" >> foo && git commit -a -m"change #1" &&
+ echo "change #2" >> foo && git commit -a -m"change #2" &&
+ echo "change #3" >> foo && git commit -a -m"change #3" &&
+ ( echo "yes
+yes
+no" | GIT_SVN_NOTTY=1 git svn dcommit --interactive ) &&
+ test $(git rev-parse HEAD^^^) = $(git rev-parse remotes/git-svn) &&
+ git reset --hard remotes/git-svn
+ )
+ '
+
+test_expect_success 'answers: yes quit' '
+ (
+ echo "change #1" >> foo && git commit -a -m"change #1" &&
+ echo "change #2" >> foo && git commit -a -m"change #2" &&
+ echo "change #3" >> foo && git commit -a -m"change #3" &&
+ ( echo "yes
+quit" | GIT_SVN_NOTTY=1 git svn dcommit --interactive ) &&
+ test $(git rev-parse HEAD^^^) = $(git rev-parse remotes/git-svn) &&
+ git reset --hard remotes/git-svn
+ )
+ '
+
+test_expect_success 'answers: all' '
+ (
+ echo "change #1" >> foo && git commit -a -m"change #1" &&
+ echo "change #2" >> foo && git commit -a -m"change #2" &&
+ echo "change #3" >> foo && git commit -a -m"change #3" &&
+ ( echo "all" | GIT_SVN_NOTTY=1 git svn dcommit --interactive ) &&
+ test $(git rev-parse HEAD) = $(git rev-parse remotes/git-svn) &&
+ git reset --hard remotes/git-svn
+ )
+ '
+
+test_done
diff --git a/t/t9163-git-svn-reset-clears-caches.sh b/t/t9163-git-svn-reset-clears-caches.sh
new file mode 100755
index 0000000..978eb62
--- /dev/null
+++ b/t/t9163-git-svn-reset-clears-caches.sh
@@ -0,0 +1,81 @@
+#!/bin/sh
+#
+# Copyright (c) 2012 Peter Baumann
+#
+
+test_description='git svn reset clears memoized caches'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./lib-git-svn.sh
+
+svn_ver="$(svn --version --quiet)"
+case $svn_ver in
+0.* | 1.[0-4].*)
+ skip_all="skipping git-svn test - SVN too old ($svn_ver)"
+ test_done
+ ;;
+esac
+
+# ... a - b - m <- trunk
+# \ /
+# ... c <- branch1
+#
+# SVN Commits not interesting for this test are abbreviated with "..."
+#
+test_expect_success 'initialize source svn repo' '
+ svn_cmd mkdir -m "create trunk" "$svnrepo"/trunk &&
+ svn_cmd mkdir -m "create branches" "$svnrepo/branches" &&
+ svn_cmd co "$svnrepo"/trunk "$SVN_TREE" &&
+ (
+ cd "$SVN_TREE" &&
+ touch foo &&
+ svn_cmd add foo &&
+ svn_cmd commit -m "a" &&
+ svn_cmd cp -m branch "$svnrepo"/trunk "$svnrepo"/branches/branch1 &&
+ svn_cmd switch "$svnrepo"/branches/branch1 &&
+ touch bar &&
+ svn_cmd add bar &&
+ svn_cmd commit -m b &&
+ svn_cmd switch "$svnrepo"/trunk &&
+ touch baz &&
+ svn_cmd add baz &&
+ svn_cmd commit -m c &&
+ svn_cmd up &&
+ svn_cmd merge "$svnrepo"/branches/branch1 &&
+ svn_cmd commit -m "m"
+ ) &&
+ rm -rf "$SVN_TREE"
+'
+
+test_expect_success 'fetch to merge-base (a)' '
+ git svn init -s "$svnrepo" &&
+ git svn fetch --revision BASE:3
+'
+
+# git svn rebase looses the merge commit
+#
+# ... a - b - m <- trunk
+# \
+# ... c
+#
+test_expect_success 'rebase looses SVN merge (m)' '
+ git svn rebase &&
+ git svn fetch &&
+ test 1 = $(git cat-file -p main|grep parent|wc -l)
+'
+
+# git svn fetch creates correct history with merge commit
+#
+# ... a - b - m <- trunk
+# \ /
+# ... c <- branch1
+#
+test_expect_success 'reset and fetch gets the SVN merge (m) correctly' '
+ git svn reset -r 3 &&
+ git reset --hard origin/trunk &&
+ git svn fetch &&
+ test 2 = $(git cat-file -p origin/trunk|grep parent|wc -l)
+'
+
+test_done
diff --git a/t/t9164-git-svn-dcommit-concurrent.sh b/t/t9164-git-svn-dcommit-concurrent.sh
new file mode 100755
index 0000000..c8e6c07
--- /dev/null
+++ b/t/t9164-git-svn-dcommit-concurrent.sh
@@ -0,0 +1,217 @@
+#!/bin/sh
+#
+# Copyright (c) 2012 Robert Luberda
+#
+
+test_description='concurrent git svn dcommit'
+
+. ./lib-git-svn.sh
+
+
+
+test_expect_success 'setup svn repository' '
+ svn_cmd checkout "$svnrepo" work.svn &&
+ (
+ cd work.svn &&
+ echo >file && echo > auto_updated_file &&
+ svn_cmd add file auto_updated_file &&
+ svn_cmd commit -m "initial commit"
+ ) &&
+ svn_cmd checkout "$svnrepo" work-auto-commits.svn
+'
+N=0
+next_N()
+{
+ N=$(( $N + 1 ))
+}
+
+# Setup SVN repository hooks to emulate SVN failures or concurrent commits
+# The function adds
+# either pre-commit hook, which causes SVN commit given in second argument
+# to fail
+# or post-commit hook, which creates a new commit (a new line added to
+# auto_updated_file) after given SVN commit
+# The first argument contains a type of the hook
+# The second argument contains a number (not SVN revision) of commit
+# the hook should be applied for (each time the hook is run, the given
+# number is decreased by one until it gets 0, in which case the hook
+# will execute its real action)
+setup_hook()
+{
+ hook_type="$1" # "pre-commit" or "post-commit"
+ skip_revs="$2"
+ [ "$hook_type" = "pre-commit" ] ||
+ [ "$hook_type" = "post-commit" ] ||
+ { echo "ERROR: invalid argument ($hook_type)" \
+ "passed to setup_hook" >&2 ; return 1; }
+ echo "cnt=$skip_revs" > "$hook_type-counter"
+ rm -f "$rawsvnrepo/hooks/"*-commit # drop previous hooks
+ hook="$rawsvnrepo/hooks/$hook_type"
+ cat > "$hook" <<- 'EOF1'
+ #!/bin/sh
+ set -e
+ cd "$1/.." # "$1" is repository location
+ exec >> svn-hook.log 2>&1
+ hook="$(basename "$0")"
+ echo "*** Executing $hook $@"
+ set -x
+ . ./$hook-counter
+ cnt="$(($cnt - 1))"
+ echo "cnt=$cnt" > ./$hook-counter
+ [ "$cnt" = "0" ] || exit 0
+EOF1
+ if [ "$hook_type" = "pre-commit" ]; then
+ echo "echo 'commit disallowed' >&2; exit 1" >>"$hook"
+ else
+ echo "PATH=\"$PATH\"; export PATH" >>"$hook"
+ echo "svnconf=\"$svnconf\"" >>"$hook"
+ cat >>"$hook" <<- 'EOF2'
+ cd work-auto-commits.svn
+ svn up --config-dir "$svnconf"
+ echo "$$" >> auto_updated_file
+ svn commit --config-dir "$svnconf" \
+ -m "auto-committing concurrent change"
+ exit 0
+EOF2
+ fi
+ chmod 755 "$hook"
+}
+
+check_contents()
+{
+ gitdir="$1"
+ (cd ../work.svn && svn_cmd up) &&
+ test_cmp file ../work.svn/file &&
+ test_cmp auto_updated_file ../work.svn/auto_updated_file
+}
+
+test_expect_success 'check if post-commit hook creates a concurrent commit' '
+ setup_hook post-commit 1 &&
+ (
+ cd work.svn &&
+ cp auto_updated_file au_file_saved &&
+ echo 1 >> file &&
+ svn_cmd commit -m "changing file" &&
+ svn_cmd up &&
+ ! test_cmp auto_updated_file au_file_saved
+ )
+'
+
+test_expect_success 'check if pre-commit hook fails' '
+ setup_hook pre-commit 2 &&
+ (
+ cd work.svn &&
+ echo 2 >> file &&
+ svn_cmd commit -m "changing file once again" &&
+ echo 3 >> file &&
+ ! svn_cmd commit -m "this commit should fail" &&
+ svn_cmd revert file
+ )
+'
+
+test_expect_success 'dcommit error handling' '
+ setup_hook pre-commit 2 &&
+ next_N && git svn clone "$svnrepo" work$N.git &&
+ (
+ cd work$N.git &&
+ echo 1 >> file && git commit -am "commit change $N.1" &&
+ echo 2 >> file && git commit -am "commit change $N.2" &&
+ echo 3 >> file && git commit -am "commit change $N.3" &&
+ # should fail to dcommit 2nd and 3rd change
+ # but still should leave the repository in reasonable state
+ test_must_fail git svn dcommit &&
+ git update-index --refresh &&
+ git show HEAD~2 | grep -q git-svn-id &&
+ ! git show HEAD~1 | grep -q git-svn-id &&
+ ! git show HEAD | grep -q git-svn-id
+ )
+'
+
+test_expect_success 'dcommit concurrent change in non-changed file' '
+ setup_hook post-commit 2 &&
+ next_N && git svn clone "$svnrepo" work$N.git &&
+ (
+ cd work$N.git &&
+ echo 1 >> file && git commit -am "commit change $N.1" &&
+ echo 2 >> file && git commit -am "commit change $N.2" &&
+ echo 3 >> file && git commit -am "commit change $N.3" &&
+ # should rebase and leave the repository in reasonable state
+ git svn dcommit &&
+ git update-index --refresh &&
+ check_contents &&
+ git show HEAD~3 | grep -q git-svn-id &&
+ git show HEAD~2 | grep -q git-svn-id &&
+ git show HEAD~1 | grep -q auto-committing &&
+ git show HEAD | grep -q git-svn-id
+ )
+'
+
+# An utility function used in the following test
+delete_first_line()
+{
+ file="$1" &&
+ sed 1d < "$file" > "${file}.tmp" &&
+ rm "$file" &&
+ mv "${file}.tmp" "$file"
+}
+
+test_expect_success 'dcommit concurrent non-conflicting change' '
+ setup_hook post-commit 2 &&
+ next_N && git svn clone "$svnrepo" work$N.git &&
+ (
+ cd work$N.git &&
+ cat file >> auto_updated_file &&
+ git commit -am "commit change $N.1" &&
+ delete_first_line auto_updated_file &&
+ git commit -am "commit change $N.2" &&
+ delete_first_line auto_updated_file &&
+ git commit -am "commit change $N.3" &&
+ # should rebase and leave the repository in reasonable state
+ git svn dcommit &&
+ git update-index --refresh &&
+ check_contents &&
+ git show HEAD~3 | grep -q git-svn-id &&
+ git show HEAD~2 | grep -q git-svn-id &&
+ git show HEAD~1 | grep -q auto-committing &&
+ git show HEAD | grep -q git-svn-id
+ )
+'
+
+test_expect_success 'dcommit --no-rebase concurrent non-conflicting change' '
+ setup_hook post-commit 2 &&
+ next_N && git svn clone "$svnrepo" work$N.git &&
+ (
+ cd work$N.git &&
+ cat file >> auto_updated_file &&
+ git commit -am "commit change $N.1" &&
+ delete_first_line auto_updated_file &&
+ git commit -am "commit change $N.2" &&
+ delete_first_line auto_updated_file &&
+ git commit -am "commit change $N.3" &&
+ # should fail as rebase is needed
+ test_must_fail git svn dcommit --no-rebase &&
+ # but should leave HEAD unchanged
+ git update-index --refresh &&
+ ! git show HEAD~2 | grep -q git-svn-id &&
+ ! git show HEAD~1 | grep -q git-svn-id &&
+ ! git show HEAD | grep -q git-svn-id
+ )
+'
+
+test_expect_success 'dcommit fails on concurrent conflicting change' '
+ setup_hook post-commit 1 &&
+ next_N && git svn clone "$svnrepo" work$N.git &&
+ (
+ cd work$N.git &&
+ echo a >> file &&
+ git commit -am "commit change $N.1" &&
+ echo b >> auto_updated_file &&
+ git commit -am "commit change $N.2" &&
+ echo c >> auto_updated_file &&
+ git commit -am "commit change $N.3" &&
+ test_must_fail git svn dcommit && # rebase should fail
+ test_must_fail git update-index --refresh
+ )
+'
+
+test_done
diff --git a/t/t9165-git-svn-fetch-merge-branch-of-branch.sh b/t/t9165-git-svn-fetch-merge-branch-of-branch.sh
new file mode 100755
index 0000000..a4813c2
--- /dev/null
+++ b/t/t9165-git-svn-fetch-merge-branch-of-branch.sh
@@ -0,0 +1,60 @@
+#!/bin/sh
+#
+# Copyright (c) 2012 Steven Walter
+#
+
+test_description='git svn merge detection'
+. ./lib-git-svn.sh
+
+svn_ver="$(svn --version --quiet)"
+case $svn_ver in
+0.* | 1.[0-4].*)
+ skip_all="skipping git-svn test - SVN too old ($svn_ver)"
+ test_done
+ ;;
+esac
+
+test_expect_success 'initialize source svn repo' '
+ svn_cmd mkdir -m x "$svnrepo"/trunk &&
+ svn_cmd mkdir -m x "$svnrepo"/branches &&
+ svn_cmd co "$svnrepo"/trunk "$SVN_TREE" &&
+ (
+ cd "$SVN_TREE" &&
+ touch foo &&
+ svn_cmd add foo &&
+ svn_cmd commit -m "initial commit" &&
+ svn_cmd cp -m branch "$svnrepo"/trunk "$svnrepo"/branches/branch1 &&
+ svn_cmd switch "$svnrepo"/branches/branch1 &&
+ touch bar &&
+ svn_cmd add bar &&
+ svn_cmd commit -m branch1 &&
+ svn_cmd cp -m branch "$svnrepo"/branches/branch1 "$svnrepo"/branches/branch2 &&
+ svn_cmd switch "$svnrepo"/branches/branch2 &&
+ touch baz &&
+ svn_cmd add baz &&
+ svn_cmd commit -m branch2 &&
+ svn_cmd switch "$svnrepo"/trunk &&
+ touch bar2 &&
+ svn_cmd add bar2 &&
+ svn_cmd commit -m trunk &&
+ svn_cmd switch "$svnrepo"/branches/branch2 &&
+ svn_cmd merge "$svnrepo"/trunk &&
+ svn_cmd commit -m "merge trunk" &&
+ svn_cmd switch "$svnrepo"/trunk &&
+ svn_cmd merge --reintegrate "$svnrepo"/branches/branch2 &&
+ svn_cmd commit -m "merge branch2"
+ ) &&
+ rm -rf "$SVN_TREE"
+'
+
+test_expect_success 'clone svn repo' '
+ git svn init -s "$svnrepo" &&
+ git svn fetch
+'
+
+test_expect_success 'verify merge commit' 'x=$(git rev-parse HEAD^2) &&
+ y=$(git rev-parse origin/branch2) &&
+ test "x$x" = "x$y"
+'
+
+test_done
diff --git a/t/t9166-git-svn-fetch-merge-branch-of-branch2.sh b/t/t9166-git-svn-fetch-merge-branch-of-branch2.sh
new file mode 100755
index 0000000..52f2e46
--- /dev/null
+++ b/t/t9166-git-svn-fetch-merge-branch-of-branch2.sh
@@ -0,0 +1,53 @@
+#!/bin/sh
+#
+# Copyright (c) 2012 Steven Walter
+#
+
+test_description='git svn merge detection'
+. ./lib-git-svn.sh
+
+svn_ver="$(svn --version --quiet)"
+case $svn_ver in
+0.* | 1.[0-4].*)
+ skip_all="skipping git-svn test - SVN too old ($svn_ver)"
+ test_done
+ ;;
+esac
+
+test_expect_success 'initialize source svn repo' '
+ svn_cmd mkdir -m x "$svnrepo"/trunk &&
+ svn_cmd mkdir -m x "$svnrepo"/branches &&
+ svn_cmd co "$svnrepo"/trunk "$SVN_TREE" &&
+ (
+ cd "$SVN_TREE" &&
+ touch foo &&
+ svn_cmd add foo &&
+ svn_cmd commit -m "initial commit" &&
+ svn_cmd cp -m branch "$svnrepo"/trunk "$svnrepo"/branches/branch1 &&
+ svn_cmd switch "$svnrepo"/branches/branch1 &&
+ touch bar &&
+ svn_cmd add bar &&
+ svn_cmd commit -m branch1 &&
+ svn_cmd cp -m branch "$svnrepo"/branches/branch1 "$svnrepo"/branches/branch2 &&
+ svn_cmd switch "$svnrepo"/branches/branch2 &&
+ touch baz &&
+ svn_cmd add baz &&
+ svn_cmd commit -m branch2 &&
+ svn_cmd switch "$svnrepo"/trunk &&
+ svn_cmd merge --reintegrate "$svnrepo"/branches/branch2 &&
+ svn_cmd commit -m "merge branch2"
+ ) &&
+ rm -rf "$SVN_TREE"
+'
+
+test_expect_success 'clone svn repo' '
+ git svn init -s "$svnrepo" &&
+ git svn fetch
+'
+
+test_expect_success 'verify merge commit' 'x=$(git rev-parse HEAD^2) &&
+ y=$(git rev-parse origin/branch2) &&
+ test "x$x" = "x$y"
+'
+
+test_done
diff --git a/t/t9167-git-svn-cmd-branch-subproject.sh b/t/t9167-git-svn-cmd-branch-subproject.sh
new file mode 100755
index 0000000..d812843
--- /dev/null
+++ b/t/t9167-git-svn-cmd-branch-subproject.sh
@@ -0,0 +1,49 @@
+#!/bin/sh
+#
+# Copyright (c) 2013 Tobias Schulte
+#
+
+test_description='git svn branch for subproject clones'
+
+. ./lib-git-svn.sh
+
+test_expect_success 'initialize svnrepo' '
+ mkdir import &&
+ (
+ cd import &&
+ mkdir -p trunk/project branches tags &&
+ (
+ cd trunk/project &&
+ echo foo > foo
+ ) &&
+ svn_cmd import -m "import for git-svn" . "$svnrepo" >/dev/null
+ ) &&
+ rm -rf import &&
+ svn_cmd co "$svnrepo"/trunk/project trunk/project &&
+ (
+ cd trunk/project &&
+ echo bar >> foo &&
+ svn_cmd ci -m "updated trunk"
+ ) &&
+ rm -rf trunk
+'
+
+test_expect_success 'import into git' '
+ git svn init --trunk=trunk/project --branches=branches/*/project \
+ --tags=tags/*/project "$svnrepo" &&
+ git svn fetch &&
+ git checkout remotes/origin/trunk
+'
+
+test_expect_success 'git svn branch tests' '
+ test_must_fail git svn branch a &&
+ git svn branch --parents a &&
+ test_must_fail git svn branch -t tag1 &&
+ git svn branch --parents -t tag1 &&
+ test_must_fail git svn branch --tag tag2 &&
+ git svn branch --parents --tag tag2 &&
+ test_must_fail git svn tag tag3 &&
+ git svn tag --parents tag3
+'
+
+test_done
diff --git a/t/t9168-git-svn-partially-globbed-names.sh b/t/t9168-git-svn-partially-globbed-names.sh
new file mode 100755
index 0000000..854b341
--- /dev/null
+++ b/t/t9168-git-svn-partially-globbed-names.sh
@@ -0,0 +1,229 @@
+#!/bin/sh
+test_description='git svn globbing refspecs with prefixed globs'
+. ./lib-git-svn.sh
+
+test_expect_success 'prepare test refspec prefixed globbing' '
+ cat >expect.end <<EOF
+the end
+hi
+start a new branch
+initial
+EOF
+ '
+
+test_expect_success 'test refspec prefixed globbing' '
+ mkdir -p trunk/src/a trunk/src/b trunk/doc &&
+ echo "hello world" >trunk/src/a/readme &&
+ echo "goodbye world" >trunk/src/b/readme &&
+ svn_cmd import -m "initial" trunk "$svnrepo"/trunk &&
+ svn_cmd co "$svnrepo" tmp &&
+ (
+ cd tmp &&
+ mkdir branches tags &&
+ svn_cmd add branches tags &&
+ svn_cmd cp trunk branches/b_start &&
+ svn_cmd commit -m "start a new branch" &&
+ svn_cmd up &&
+ echo "hi" >>branches/b_start/src/b/readme &&
+ poke branches/b_start/src/b/readme &&
+ echo "hey" >>branches/b_start/src/a/readme &&
+ poke branches/b_start/src/a/readme &&
+ svn_cmd commit -m "hi" &&
+ svn_cmd up &&
+ svn_cmd cp branches/b_start tags/t_end &&
+ echo "bye" >>tags/t_end/src/b/readme &&
+ poke tags/t_end/src/b/readme &&
+ echo "aye" >>tags/t_end/src/a/readme &&
+ poke tags/t_end/src/a/readme &&
+ svn_cmd commit -m "the end" &&
+ echo "byebye" >>tags/t_end/src/b/readme &&
+ poke tags/t_end/src/b/readme &&
+ svn_cmd commit -m "nothing to see here"
+ ) &&
+ git config --add svn-remote.svn.url "$svnrepo" &&
+ git config --add svn-remote.svn.fetch \
+ "trunk/src/a:refs/remotes/trunk" &&
+ git config --add svn-remote.svn.branches \
+ "branches/b_*/src/a:refs/remotes/branches/b_*" &&
+ git config --add svn-remote.svn.tags\
+ "tags/t_*/src/a:refs/remotes/tags/t_*" &&
+ git svn multi-fetch &&
+ git log --pretty=oneline refs/remotes/tags/t_end >actual &&
+ cut -d" " -f2- actual >output.end &&
+ test_cmp expect.end output.end &&
+ test "$(git rev-parse refs/remotes/tags/t_end~1)" = \
+ "$(git rev-parse refs/remotes/branches/b_start)" &&
+ test "$(git rev-parse refs/remotes/branches/b_start~2)" = \
+ "$(git rev-parse refs/remotes/trunk)" &&
+ test_must_fail git rev-parse refs/remotes/tags/t_end@3
+ '
+
+test_expect_success 'prepare test left-hand-side only prefixed globbing' '
+ echo try to try >expect.two &&
+ echo nothing to see here >>expect.two &&
+ cat expect.end >>expect.two
+ '
+
+test_expect_success 'test left-hand-side only prefixed globbing' '
+ git config --add svn-remote.two.url "$svnrepo" &&
+ git config --add svn-remote.two.fetch trunk:refs/remotes/two/trunk &&
+ git config --add svn-remote.two.branches \
+ "branches/b_*:refs/remotes/two/branches/*" &&
+ git config --add svn-remote.two.tags \
+ "tags/t_*:refs/remotes/two/tags/*" &&
+ (
+ cd tmp &&
+ echo "try try" >>tags/t_end/src/b/readme &&
+ poke tags/t_end/src/b/readme &&
+ svn_cmd commit -m "try to try"
+ ) &&
+ git svn fetch two &&
+ git rev-list refs/remotes/two/tags/t_end >actual &&
+ test_line_count = 6 actual &&
+ git rev-list refs/remotes/two/branches/b_start >actual &&
+ test_line_count = 3 actual &&
+ test $(git rev-parse refs/remotes/two/branches/b_start~2) = \
+ $(git rev-parse refs/remotes/two/trunk) &&
+ test $(git rev-parse refs/remotes/two/tags/t_end~3) = \
+ $(git rev-parse refs/remotes/two/branches/b_start) &&
+ git log --pretty=oneline refs/remotes/two/tags/t_end >actual &&
+ cut -d" " -f2- actual >output.two &&
+ test_cmp expect.two output.two
+ '
+
+test_expect_success 'prepare test prefixed globs match just prefix' '
+ cat >expect.three <<EOF
+Tag commit to t_
+Branch commit to b_
+initial
+EOF
+ '
+
+test_expect_success 'test prefixed globs match just prefix' '
+ git config --add svn-remote.three.url "$svnrepo" &&
+ git config --add svn-remote.three.fetch \
+ trunk:refs/remotes/three/trunk &&
+ git config --add svn-remote.three.branches \
+ "branches/b_*:refs/remotes/three/branches/*" &&
+ git config --add svn-remote.three.tags \
+ "tags/t_*:refs/remotes/three/tags/*" &&
+ (
+ cd tmp &&
+ svn_cmd cp trunk branches/b_ &&
+ echo "Branch commit to b_" >>branches/b_/src/a/readme &&
+ poke branches/b_/src/a/readme &&
+ svn_cmd commit -m "Branch commit to b_" &&
+ svn_cmd up && svn_cmd cp branches/b_ tags/t_ &&
+ echo "Tag commit to t_" >>tags/t_/src/a/readme &&
+ poke tags/t_/src/a/readme &&
+ svn_cmd commit -m "Tag commit to t_" &&
+ svn_cmd up
+ ) &&
+ git svn fetch three &&
+ git rev-list refs/remotes/three/branches/b_ >actual &&
+ test_line_count = 2 actual &&
+ git rev-list refs/remotes/three/tags/t_ >actual &&
+ test_line_count = 3 actual &&
+ test $(git rev-parse refs/remotes/three/branches/b_~1) = \
+ $(git rev-parse refs/remotes/three/trunk) &&
+ test $(git rev-parse refs/remotes/three/tags/t_~1) = \
+ $(git rev-parse refs/remotes/three/branches/b_) &&
+ git log --pretty=oneline refs/remotes/three/tags/t_ >actual &&
+ cut -d" " -f2- actual >output.three &&
+ test_cmp expect.three output.three
+ '
+
+test_expect_success 'prepare test disallow prefixed multi-globs' "
+cat >expect.four <<EOF
+Only one set of wildcards (e.g. '*' or '*/*/*') is supported: branches/b_*/t/*
+
+EOF
+ "
+
+test_expect_success 'test disallow prefixed multi-globs' '
+ git config --add svn-remote.four.url "$svnrepo" &&
+ git config --add svn-remote.four.fetch \
+ trunk:refs/remotes/four/trunk &&
+ git config --add svn-remote.four.branches \
+ "branches/b_*/t/*:refs/remotes/four/branches/*" &&
+ git config --add svn-remote.four.tags \
+ "tags/t_*/*:refs/remotes/four/tags/*" &&
+ (
+ cd tmp &&
+ echo "try try" >>tags/t_end/src/b/readme &&
+ poke tags/t_end/src/b/readme &&
+ svn_cmd commit -m "try to try"
+ ) &&
+ test_must_fail git svn fetch four 2>stderr.four &&
+ test_cmp expect.four stderr.four &&
+ git config --unset svn-remote.four.branches &&
+ git config --unset svn-remote.four.tags
+ '
+
+test_expect_success 'prepare test globbing in the middle of the word' '
+ cat >expect.five <<EOF
+Tag commit to fghij
+Branch commit to abcde
+initial
+EOF
+ '
+
+test_expect_success 'test globbing in the middle of the word' '
+ git config --add svn-remote.five.url "$svnrepo" &&
+ git config --add svn-remote.five.fetch \
+ trunk:refs/remotes/five/trunk &&
+ git config --add svn-remote.five.branches \
+ "branches/a*e:refs/remotes/five/branches/*" &&
+ git config --add svn-remote.five.tags \
+ "tags/f*j:refs/remotes/five/tags/*" &&
+ (
+ cd tmp &&
+ svn_cmd cp trunk branches/abcde &&
+ echo "Branch commit to abcde" >>branches/abcde/src/a/readme &&
+ poke branches/b_/src/a/readme &&
+ svn_cmd commit -m "Branch commit to abcde" &&
+ svn_cmd up &&
+ svn_cmd cp branches/abcde tags/fghij &&
+ echo "Tag commit to fghij" >>tags/fghij/src/a/readme &&
+ poke tags/fghij/src/a/readme &&
+ svn_cmd commit -m "Tag commit to fghij" &&
+ svn_cmd up
+ ) &&
+ git svn fetch five &&
+ git rev-list refs/remotes/five/branches/abcde >actual &&
+ test_line_count = 2 actual &&
+ git rev-list refs/remotes/five/tags/fghij >actual &&
+ test_line_count = 3 actual &&
+ test $(git rev-parse refs/remotes/five/branches/abcde~1) = \
+ $(git rev-parse refs/remotes/five/trunk) &&
+ test $(git rev-parse refs/remotes/five/tags/fghij~1) = \
+ $(git rev-parse refs/remotes/five/branches/abcde) &&
+ git log --pretty=oneline refs/remotes/five/tags/fghij >actual &&
+ cut -d" " -f2- actual >output.five &&
+ test_cmp expect.five output.five
+ '
+
+test_expect_success 'prepare test disallow multiple asterisks in one word' "
+ echo \"Only one '*' is allowed in a pattern: 'a*c*e'\" >expect.six &&
+ echo \"\" >>expect.six
+ "
+
+test_expect_success 'test disallow multiple asterisks in one word' '
+ git config --add svn-remote.six.url "$svnrepo" &&
+ git config --add svn-remote.six.fetch \
+ trunk:refs/remotes/six/trunk &&
+ git config --add svn-remote.six.branches \
+ "branches/a*c*e:refs/remotes/six/branches/*" &&
+ git config --add svn-remote.six.tags \
+ "tags/f*h*j:refs/remotes/six/tags/*" &&
+ (
+ cd tmp &&
+ echo "try try" >>tags/fghij/src/b/readme &&
+ poke tags/fghij/src/b/readme &&
+ svn_cmd commit -m "try to try"
+ ) &&
+ test_must_fail git svn fetch six 2>stderr.six &&
+ test_cmp expect.six stderr.six
+ '
+
+test_done
diff --git a/t/t9169-git-svn-dcommit-crlf.sh b/t/t9169-git-svn-dcommit-crlf.sh
new file mode 100755
index 0000000..7080b5d
--- /dev/null
+++ b/t/t9169-git-svn-dcommit-crlf.sh
@@ -0,0 +1,30 @@
+#!/bin/sh
+
+test_description='git svn dcommit CRLF'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./lib-git-svn.sh
+
+test_expect_success 'setup commit repository' '
+ svn_cmd mkdir -m "$test_description" "$svnrepo/dir" &&
+ git svn clone "$svnrepo" work &&
+ (
+ cd work &&
+ echo foo >>foo &&
+ git update-index --add foo &&
+ printf "a\\r\\n\\r\\nb\\r\\nc\\r\\n" >cmt &&
+ p=$(git rev-parse HEAD) &&
+ t=$(git write-tree) &&
+ cmt=$(git commit-tree -p $p $t <cmt) &&
+ git update-ref refs/heads/main $cmt &&
+ git cat-file commit HEAD | tail -n4 >out &&
+ test_cmp cmt out &&
+ git svn dcommit &&
+ printf "a\\n\\nb\\nc\\n" >exp &&
+ git cat-file commit HEAD | sed -ne 6,9p >out &&
+ test_cmp exp out
+ )
+'
+
+test_done
diff --git a/t/t9200-git-cvsexportcommit.sh b/t/t9200-git-cvsexportcommit.sh
new file mode 100755
index 0000000..a44eabf
--- /dev/null
+++ b/t/t9200-git-cvsexportcommit.sh
@@ -0,0 +1,341 @@
+#!/bin/sh
+#
+# Copyright (c) Robin Rosenberg
+#
+test_description='Test export of commits to CVS'
+
+. ./test-lib.sh
+
+if ! test_have_prereq PERL; then
+ skip_all='skipping git cvsexportcommit tests, perl not available'
+ test_done
+fi
+
+cvs >/dev/null 2>&1
+if test $? -ne 1
+then
+ skip_all='skipping git cvsexportcommit tests, cvs not found'
+ test_done
+fi
+
+if ! test_have_prereq NOT_ROOT; then
+ skip_all='When cvs is compiled with CVS_BADROOT commits as root fail'
+ test_done
+fi
+
+CVSROOT=$PWD/tmpcvsroot
+CVSWORK=$PWD/cvswork
+GIT_DIR=$PWD/.git
+export CVSROOT CVSWORK GIT_DIR
+
+rm -rf "$CVSROOT" "$CVSWORK"
+
+cvs init &&
+test -d "$CVSROOT" &&
+cvs -Q co -d "$CVSWORK" . &&
+echo >empty &&
+git add empty &&
+git commit -q -a -m "Initial" 2>/dev/null ||
+exit 1
+
+check_entries () {
+ # $1 == directory, $2 == expected
+ sed -ne '/^\//p' "$1/CVS/Entries" | sort | cut -d/ -f2,3,5 >actual
+ if test -z "$2"
+ then
+ test_must_be_empty actual
+ else
+ printf '%s\n' "$2" | tr '|' '\012' >expected
+ test_cmp expected actual
+ fi
+}
+
+test_expect_success 'New file' '
+ mkdir A B C D E F &&
+ echo hello1 >A/newfile1.txt &&
+ echo hello2 >B/newfile2.txt &&
+ cp "$TEST_DIRECTORY"/test-binary-1.png C/newfile3.png &&
+ cp "$TEST_DIRECTORY"/test-binary-1.png D/newfile4.png &&
+ git add A/newfile1.txt &&
+ git add B/newfile2.txt &&
+ git add C/newfile3.png &&
+ git add D/newfile4.png &&
+ git commit -a -m "Test: New file" &&
+ id=$(git rev-list --max-count=1 HEAD) &&
+ (cd "$CVSWORK" &&
+ git cvsexportcommit -c $id &&
+ check_entries A "newfile1.txt/1.1/" &&
+ check_entries B "newfile2.txt/1.1/" &&
+ check_entries C "newfile3.png/1.1/-kb" &&
+ check_entries D "newfile4.png/1.1/-kb" &&
+ test_cmp A/newfile1.txt ../A/newfile1.txt &&
+ test_cmp B/newfile2.txt ../B/newfile2.txt &&
+ test_cmp C/newfile3.png ../C/newfile3.png &&
+ test_cmp D/newfile4.png ../D/newfile4.png
+ )
+'
+
+test_expect_success 'Remove two files, add two and update two' '
+ echo Hello1 >>A/newfile1.txt &&
+ rm -f B/newfile2.txt &&
+ rm -f C/newfile3.png &&
+ echo Hello5 >E/newfile5.txt &&
+ cp "$TEST_DIRECTORY"/test-binary-2.png D/newfile4.png &&
+ cp "$TEST_DIRECTORY"/test-binary-1.png F/newfile6.png &&
+ git add E/newfile5.txt &&
+ git add F/newfile6.png &&
+ git commit -a -m "Test: Remove, add and update" &&
+ id=$(git rev-list --max-count=1 HEAD) &&
+ (cd "$CVSWORK" &&
+ git cvsexportcommit -c $id &&
+ check_entries A "newfile1.txt/1.2/" &&
+ check_entries B "" &&
+ check_entries C "" &&
+ check_entries D "newfile4.png/1.2/-kb" &&
+ check_entries E "newfile5.txt/1.1/" &&
+ check_entries F "newfile6.png/1.1/-kb" &&
+ test_cmp A/newfile1.txt ../A/newfile1.txt &&
+ test_cmp D/newfile4.png ../D/newfile4.png &&
+ test_cmp E/newfile5.txt ../E/newfile5.txt &&
+ test_cmp F/newfile6.png ../F/newfile6.png
+ )
+'
+
+# Should fail (but only on the git cvsexportcommit stage)
+test_expect_success \
+ 'Fail to change binary more than one generation old' \
+ 'cat F/newfile6.png >>D/newfile4.png &&
+ git commit -a -m "generatiion 1" &&
+ cat F/newfile6.png >>D/newfile4.png &&
+ git commit -a -m "generation 2" &&
+ id=$(git rev-list --max-count=1 HEAD) &&
+ (cd "$CVSWORK" &&
+ test_must_fail git cvsexportcommit -c $id
+ )'
+
+#test_expect_success \
+# 'Fail to remove binary file more than one generation old' \
+# 'git reset --hard HEAD^ &&
+# cat F/newfile6.png >>D/newfile4.png &&
+# git commit -a -m "generation 2 (again)" &&
+# rm -f D/newfile4.png &&
+# git commit -a -m "generation 3" &&
+# id=$(git rev-list --max-count=1 HEAD) &&
+# (cd "$CVSWORK" &&
+# test_must_fail git cvsexportcommit -c $id
+# )'
+
+# We reuse the state from two tests back here
+
+# This test is here because a patch for only binary files will
+# fail with gnu patch, so cvsexportcommit must handle that.
+test_expect_success 'Remove only binary files' '
+ git reset --hard HEAD^^ &&
+ rm -f D/newfile4.png &&
+ git commit -a -m "test: remove only a binary file" &&
+ id=$(git rev-list --max-count=1 HEAD) &&
+ (cd "$CVSWORK" &&
+ git cvsexportcommit -c $id &&
+ check_entries A "newfile1.txt/1.2/" &&
+ check_entries B "" &&
+ check_entries C "" &&
+ check_entries D "" &&
+ check_entries E "newfile5.txt/1.1/" &&
+ check_entries F "newfile6.png/1.1/-kb" &&
+ test_cmp A/newfile1.txt ../A/newfile1.txt &&
+ test_cmp E/newfile5.txt ../E/newfile5.txt &&
+ test_cmp F/newfile6.png ../F/newfile6.png
+ )
+'
+
+test_expect_success 'Remove only a text file' '
+ rm -f A/newfile1.txt &&
+ git commit -a -m "test: remove only a binary file" &&
+ id=$(git rev-list --max-count=1 HEAD) &&
+ (cd "$CVSWORK" &&
+ git cvsexportcommit -c $id &&
+ check_entries A "" &&
+ check_entries B "" &&
+ check_entries C "" &&
+ check_entries D "" &&
+ check_entries E "newfile5.txt/1.1/" &&
+ check_entries F "newfile6.png/1.1/-kb" &&
+ test_cmp E/newfile5.txt ../E/newfile5.txt &&
+ test_cmp F/newfile6.png ../F/newfile6.png
+ )
+'
+
+test_expect_success 'New file with spaces in file name' '
+ mkdir "G g" &&
+ echo ok then >"G g/with spaces.txt" &&
+ git add "G g/with spaces.txt" && \
+ cp "$TEST_DIRECTORY"/test-binary-1.png "G g/with spaces.png" && \
+ git add "G g/with spaces.png" &&
+ git commit -a -m "With spaces" &&
+ id=$(git rev-list --max-count=1 HEAD) &&
+ (cd "$CVSWORK" &&
+ git cvsexportcommit -c $id &&
+ check_entries "G g" "with spaces.png/1.1/-kb|with spaces.txt/1.1/"
+ )
+'
+
+test_expect_success 'Update file with spaces in file name' '
+ echo Ok then >>"G g/with spaces.txt" &&
+ cat "$TEST_DIRECTORY"/test-binary-1.png >>"G g/with spaces.png" && \
+ git add "G g/with spaces.png" &&
+ git commit -a -m "Update with spaces" &&
+ id=$(git rev-list --max-count=1 HEAD) &&
+ (cd "$CVSWORK" &&
+ git cvsexportcommit -c $id &&
+ check_entries "G g" "with spaces.png/1.2/-kb|with spaces.txt/1.2/"
+ )
+'
+
+# Some filesystems mangle pathnames with UTF-8 characters --
+# check and skip
+if p="Å/goo/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z/å/ä/ö" &&
+ mkdir -p "tst/$p" &&
+ date >"tst/$p/day" &&
+ found=$(find tst -type f -print) &&
+ test "z$found" = "ztst/$p/day" &&
+ rm -fr tst
+then
+
+# This test contains UTF-8 characters
+test_expect_success !MINGW 'File with non-ascii file name' '
+ mkdir -p Å/goo/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z/å/ä/ö &&
+ echo Foo >Å/goo/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z/å/ä/ö/gårdetsågårdet.txt &&
+ git add Å/goo/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z/å/ä/ö/gårdetsågårdet.txt &&
+ cp "$TEST_DIRECTORY"/test-binary-1.png Å/goo/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z/å/ä/ö/gårdetsågårdet.png &&
+ git add Å/goo/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z/å/ä/ö/gårdetsågårdet.png &&
+ git commit -a -m "Går det så går det" && \
+ id=$(git rev-list --max-count=1 HEAD) &&
+ (cd "$CVSWORK" &&
+ git cvsexportcommit -v -c $id &&
+ check_entries \
+ "Å/goo/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z/å/ä/ö" \
+ "gårdetsågårdet.png/1.1/-kb|gårdetsågårdet.txt/1.1/"
+ )
+'
+
+fi
+
+rm -fr tst
+
+test_expect_success 'Mismatching patch should fail' '
+ date >>"E/newfile5.txt" &&
+ git add "E/newfile5.txt" &&
+ git commit -a -m "Update one" &&
+ date >>"E/newfile5.txt" &&
+ git add "E/newfile5.txt" &&
+ git commit -a -m "Update two" &&
+ id=$(git rev-list --max-count=1 HEAD) &&
+ (cd "$CVSWORK" &&
+ test_must_fail git cvsexportcommit -c $id
+ )
+'
+
+test_expect_success FILEMODE 'Retain execute bit' '
+ mkdir G &&
+ echo executeon >G/on &&
+ chmod +x G/on &&
+ echo executeoff >G/off &&
+ git add G/on &&
+ git add G/off &&
+ git commit -a -m "Execute test" &&
+ (cd "$CVSWORK" &&
+ git cvsexportcommit -c HEAD &&
+ test -x G/on &&
+ ! test -x G/off
+ )
+'
+
+test_expect_success '-w option should work with relative GIT_DIR' '
+ mkdir W &&
+ echo foobar >W/file1.txt &&
+ echo bazzle >W/file2.txt &&
+ git add W/file1.txt &&
+ git add W/file2.txt &&
+ git commit -m "More updates" &&
+ id=$(git rev-list --max-count=1 HEAD) &&
+ (cd "$GIT_DIR" &&
+ GIT_DIR=. git cvsexportcommit -w "$CVSWORK" -c $id &&
+ check_entries "$CVSWORK/W" "file1.txt/1.1/|file2.txt/1.1/" &&
+ test_cmp "$CVSWORK/W/file1.txt" ../W/file1.txt &&
+ test_cmp "$CVSWORK/W/file2.txt" ../W/file2.txt
+ )
+'
+
+test_expect_success 'check files before directories' '
+
+ echo Notes > release-notes &&
+ git add release-notes &&
+ git commit -m "Add release notes" release-notes &&
+ id=$(git rev-parse HEAD) &&
+ git cvsexportcommit -w "$CVSWORK" -c $id &&
+
+ echo new > DS &&
+ echo new > E/DS &&
+ echo modified > release-notes &&
+ git add DS E/DS release-notes &&
+ git commit -m "Add two files with the same basename" &&
+ id=$(git rev-parse HEAD) &&
+ git cvsexportcommit -w "$CVSWORK" -c $id &&
+ check_entries "$CVSWORK/E" "DS/1.1/|newfile5.txt/1.1/" &&
+ check_entries "$CVSWORK" "DS/1.1/|release-notes/1.2/" &&
+ test_cmp "$CVSWORK/DS" DS &&
+ test_cmp "$CVSWORK/E/DS" E/DS &&
+ test_cmp "$CVSWORK/release-notes" release-notes
+
+'
+
+test_expect_success 're-commit a removed filename which remains in CVS attic' '
+ (cd "$CVSWORK" &&
+ echo >attic_gremlin &&
+ cvs -Q add attic_gremlin &&
+ cvs -Q ci -m "added attic_gremlin" &&
+ rm attic_gremlin &&
+ cvs -Q rm attic_gremlin &&
+ cvs -Q ci -m "removed attic_gremlin") &&
+
+ echo > attic_gremlin &&
+ git add attic_gremlin &&
+ git commit -m "Added attic_gremlin" &&
+ git cvsexportcommit -w "$CVSWORK" -c HEAD &&
+ (cd "$CVSWORK" && cvs -Q update -d) &&
+ test -f "$CVSWORK/attic_gremlin"
+'
+
+# the state of the CVS sandbox may be indeterminate for ' space'
+# after this test on some platforms / with some versions of CVS
+# consider adding new tests above this point
+test_expect_success 'commit a file with leading spaces in the name' '
+
+ echo space > " space" &&
+ git add " space" &&
+ git commit -m "Add a file with a leading space" &&
+ id=$(git rev-parse HEAD) &&
+ git cvsexportcommit -w "$CVSWORK" -c $id &&
+ check_entries "$CVSWORK" " space/1.1/|DS/1.1/|attic_gremlin/1.3/|release-notes/1.2/" &&
+ test_cmp "$CVSWORK/ space" " space"
+
+'
+
+test_expect_success 'use the same checkout for Git and CVS' '
+
+ (mkdir shared &&
+ cd shared &&
+ sane_unset GIT_DIR &&
+ cvs co . &&
+ git init &&
+ git add " space" &&
+ git commit -m "fake initial commit" &&
+ echo Hello >> " space" &&
+ git commit -m "Another change" " space" &&
+ git cvsexportcommit -W -p -u -c HEAD &&
+ grep Hello " space" &&
+ git diff-files)
+
+'
+
+test_done
diff --git a/t/t9210-scalar.sh b/t/t9210-scalar.sh
new file mode 100755
index 0000000..4432a30
--- /dev/null
+++ b/t/t9210-scalar.sh
@@ -0,0 +1,234 @@
+#!/bin/sh
+
+test_description='test the `scalar` command'
+
+. ./test-lib.sh
+
+GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt,launchctl:true,schtasks:true"
+export GIT_TEST_MAINT_SCHEDULER
+
+test_expect_success 'scalar shows a usage' '
+ test_expect_code 129 scalar -h
+'
+
+test_expect_success 'scalar invoked on enlistment root' '
+ test_when_finished rm -rf test src deeper &&
+
+ for enlistment_root in test src deeper/test
+ do
+ git init ${enlistment_root}/src &&
+
+ # Register
+ scalar register ${enlistment_root} &&
+ scalar list >out &&
+ grep "$(pwd)/${enlistment_root}/src\$" out &&
+
+ # Delete (including enlistment root)
+ scalar delete $enlistment_root &&
+ test_path_is_missing $enlistment_root &&
+ scalar list >out &&
+ ! grep "^$(pwd)/${enlistment_root}/src\$" out || return 1
+ done
+'
+
+test_expect_success 'scalar invoked on enlistment src repo' '
+ test_when_finished rm -rf test src deeper &&
+
+ for enlistment_root in test src deeper/test
+ do
+ git init ${enlistment_root}/src &&
+
+ # Register
+ scalar register ${enlistment_root}/src &&
+ scalar list >out &&
+ grep "$(pwd)/${enlistment_root}/src\$" out &&
+
+ # Delete (will not include enlistment root)
+ scalar delete ${enlistment_root}/src &&
+ test_path_is_dir $enlistment_root &&
+ scalar list >out &&
+ ! grep "^$(pwd)/${enlistment_root}/src\$" out || return 1
+ done
+'
+
+test_expect_success 'scalar invoked when enlistment root and repo are the same' '
+ test_when_finished rm -rf test src deeper &&
+
+ for enlistment_root in test src deeper/test
+ do
+ git init ${enlistment_root} &&
+
+ # Register
+ scalar register ${enlistment_root} &&
+ scalar list >out &&
+ grep "$(pwd)/${enlistment_root}\$" out &&
+
+ # Delete (will not include enlistment root)
+ scalar delete ${enlistment_root} &&
+ test_path_is_missing $enlistment_root &&
+ scalar list >out &&
+ ! grep "^$(pwd)/${enlistment_root}\$" out &&
+
+ # Make sure we did not accidentally delete the trash dir
+ test_path_is_dir "$TRASH_DIRECTORY" || return 1
+ done
+'
+
+test_expect_success 'scalar repo search respects GIT_CEILING_DIRECTORIES' '
+ test_when_finished rm -rf test &&
+
+ git init test/src &&
+ mkdir -p test/src/deep &&
+ GIT_CEILING_DIRECTORIES="$(pwd)/test/src" &&
+ ! scalar register test/src/deep 2>err &&
+ grep "not a git repository" err
+'
+
+test_expect_success 'scalar enlistments need a worktree' '
+ test_when_finished rm -rf bare test &&
+
+ git init --bare bare/src &&
+ ! scalar register bare/src 2>err &&
+ grep "Scalar enlistments require a worktree" err &&
+
+ git init test/src &&
+ ! scalar register test/src/.git 2>err &&
+ grep "Scalar enlistments require a worktree" err
+'
+
+test_expect_success FSMONITOR_DAEMON 'scalar register starts fsmon daemon' '
+ git init test/src &&
+ test_must_fail git -C test/src fsmonitor--daemon status &&
+ scalar register test/src &&
+ git -C test/src fsmonitor--daemon status &&
+ test_cmp_config -C test/src true core.fsmonitor
+'
+
+test_expect_success 'scalar register warns when background maintenance fails' '
+ git init register-repo &&
+ GIT_TEST_MAINT_SCHEDULER="crontab:false,launchctl:false,schtasks:false" \
+ scalar register register-repo 2>err &&
+ grep "could not turn on maintenance" err
+'
+
+test_expect_success 'scalar unregister' '
+ git init vanish/src &&
+ scalar register vanish/src &&
+ git config --get --global --fixed-value \
+ maintenance.repo "$(pwd)/vanish/src" &&
+ scalar list >scalar.repos &&
+ grep -F "$(pwd)/vanish/src" scalar.repos &&
+ rm -rf vanish/src/.git &&
+ scalar unregister vanish &&
+ test_must_fail git config --get --global --fixed-value \
+ maintenance.repo "$(pwd)/vanish/src" &&
+ scalar list >scalar.repos &&
+ ! grep -F "$(pwd)/vanish/src" scalar.repos &&
+
+ # scalar unregister should be idempotent
+ scalar unregister vanish
+'
+
+test_expect_success 'set up repository to clone' '
+ test_commit first &&
+ test_commit second &&
+ test_commit third &&
+ git switch -c parallel first &&
+ mkdir -p 1/2 &&
+ test_commit 1/2/3 &&
+ git config uploadPack.allowFilter true &&
+ git config uploadPack.allowAnySHA1InWant true
+'
+
+test_expect_success 'scalar clone' '
+ second=$(git rev-parse --verify second:second.t) &&
+ scalar clone "file://$(pwd)" cloned --single-branch &&
+ (
+ cd cloned/src &&
+
+ git config --get --global --fixed-value maintenance.repo \
+ "$(pwd)" &&
+
+ git for-each-ref --format="%(refname)" refs/remotes/origin/ >actual &&
+ echo "refs/remotes/origin/parallel" >expect &&
+ test_cmp expect actual &&
+
+ test_path_is_missing 1/2 &&
+ test_must_fail git rev-list --missing=print $second &&
+ git rev-list $second &&
+ git cat-file blob $second >actual &&
+ echo "second" >expect &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'scalar reconfigure' '
+ git init one/src &&
+ scalar register one &&
+ git -C one/src config core.preloadIndex false &&
+ scalar reconfigure one &&
+ test true = "$(git -C one/src config core.preloadIndex)" &&
+ git -C one/src config core.preloadIndex false &&
+ scalar reconfigure -a &&
+ test true = "$(git -C one/src config core.preloadIndex)"
+'
+
+test_expect_success '`reconfigure -a` removes stale config entries' '
+ git init stale/src &&
+ scalar register stale &&
+ scalar list >scalar.repos &&
+ grep stale scalar.repos &&
+
+ grep -v stale scalar.repos >expect &&
+
+ rm -rf stale &&
+ scalar reconfigure -a &&
+ scalar list >scalar.repos &&
+ test_cmp expect scalar.repos
+'
+
+test_expect_success 'scalar delete without enlistment shows a usage' '
+ test_expect_code 129 scalar delete
+'
+
+test_expect_success 'scalar delete with enlistment' '
+ scalar delete cloned &&
+ test_path_is_missing cloned
+'
+
+test_expect_success 'scalar supports -c/-C' '
+ test_when_finished "scalar delete sub" &&
+ git init sub &&
+ scalar -C sub -c status.aheadBehind=bogus register &&
+ test -z "$(git -C sub config --local status.aheadBehind)" &&
+ test true = "$(git -C sub config core.preloadIndex)"
+'
+
+test_expect_success '`scalar [...] <dir>` errors out when dir is missing' '
+ ! scalar run config cloned 2>err &&
+ grep "cloned. does not exist" err
+'
+
+SQ="'"
+test_expect_success UNZIP 'scalar diagnose' '
+ scalar clone "file://$(pwd)" cloned --single-branch &&
+ git repack &&
+ echo "$(pwd)/.git/objects/" >>cloned/src/.git/objects/info/alternates &&
+ test_commit -C cloned/src loose &&
+ scalar diagnose cloned >out 2>err &&
+ grep "Available space" out &&
+ sed -n "s/.*$SQ\\(.*\\.zip\\)$SQ.*/\\1/p" <err >zip_path &&
+ zip_path=$(cat zip_path) &&
+ test -n "$zip_path" &&
+ "$GIT_UNZIP" -v "$zip_path" &&
+ folder=${zip_path%.zip} &&
+ test_path_is_missing "$folder" &&
+ "$GIT_UNZIP" -p "$zip_path" diagnostics.log >out &&
+ test_file_not_empty out &&
+ "$GIT_UNZIP" -p "$zip_path" packs-local.txt >out &&
+ grep "$(pwd)/.git/objects" out &&
+ "$GIT_UNZIP" -p "$zip_path" objects-local.txt >out &&
+ grep "^Total: [1-9]" out
+'
+
+test_done
diff --git a/t/t9211-scalar-clone.sh b/t/t9211-scalar-clone.sh
new file mode 100755
index 0000000..7869f45
--- /dev/null
+++ b/t/t9211-scalar-clone.sh
@@ -0,0 +1,195 @@
+#!/bin/sh
+
+test_description='test the `scalar clone` subcommand'
+
+. ./test-lib.sh
+. "${TEST_DIRECTORY}/lib-terminal.sh"
+
+GIT_TEST_MAINT_SCHEDULER="crontab:test-tool crontab cron.txt,launchctl:true,schtasks:true"
+export GIT_TEST_MAINT_SCHEDULER
+
+test_expect_success 'set up repository to clone' '
+ rm -rf .git &&
+ git init to-clone &&
+ (
+ cd to-clone &&
+ git branch -m base &&
+
+ test_commit first &&
+ test_commit second &&
+ test_commit third &&
+
+ git switch -c parallel first &&
+ mkdir -p 1/2 &&
+ test_commit 1/2/3 &&
+
+ git switch base &&
+
+ # By default, permit
+ git config uploadpack.allowfilter true &&
+ git config uploadpack.allowanysha1inwant true
+ )
+'
+
+cleanup_clone () {
+ rm -rf "$1"
+}
+
+test_expect_success 'creates content in enlistment root' '
+ enlistment=cloned &&
+
+ scalar clone "file://$(pwd)/to-clone" $enlistment &&
+ ls -A $enlistment >enlistment-root &&
+ test_line_count = 1 enlistment-root &&
+ test_path_is_dir $enlistment/src &&
+ test_path_is_dir $enlistment/src/.git &&
+
+ cleanup_clone $enlistment
+'
+
+test_expect_success 'with spaces' '
+ enlistment="cloned with space" &&
+
+ scalar clone "file://$(pwd)/to-clone" "$enlistment" &&
+ test_path_is_dir "$enlistment" &&
+ test_path_is_dir "$enlistment/src" &&
+ test_path_is_dir "$enlistment/src/.git" &&
+
+ cleanup_clone "$enlistment"
+'
+
+test_expect_success 'partial clone if supported by server' '
+ enlistment=partial-clone &&
+
+ scalar clone "file://$(pwd)/to-clone" $enlistment &&
+
+ (
+ cd $enlistment/src &&
+
+ # Two promisor packs: one for refs, the other for blobs
+ ls .git/objects/pack/pack-*.promisor >promisorlist &&
+ test_line_count = 2 promisorlist
+ ) &&
+
+ cleanup_clone $enlistment
+'
+
+test_expect_success 'fall back on full clone if partial unsupported' '
+ enlistment=no-partial-support &&
+
+ test_config -C to-clone uploadpack.allowfilter false &&
+ test_config -C to-clone uploadpack.allowanysha1inwant false &&
+
+ scalar clone "file://$(pwd)/to-clone" $enlistment 2>err &&
+ grep "filtering not recognized by server, ignoring" err &&
+
+ (
+ cd $enlistment/src &&
+
+ # Still get a refs promisor file, but none for blobs
+ ls .git/objects/pack/pack-*.promisor >promisorlist &&
+ test_line_count = 1 promisorlist
+ ) &&
+
+ cleanup_clone $enlistment
+'
+
+test_expect_success 'initializes sparse-checkout by default' '
+ enlistment=sparse &&
+
+ scalar clone "file://$(pwd)/to-clone" $enlistment &&
+ (
+ cd $enlistment/src &&
+ test_cmp_config true core.sparseCheckout &&
+ test_cmp_config true core.sparseCheckoutCone
+ ) &&
+
+ cleanup_clone $enlistment
+'
+
+test_expect_success '--full-clone does not create sparse-checkout' '
+ enlistment=full-clone &&
+
+ scalar clone --full-clone "file://$(pwd)/to-clone" $enlistment &&
+ (
+ cd $enlistment/src &&
+ test_cmp_config "" --default "" core.sparseCheckout &&
+ test_cmp_config "" --default "" core.sparseCheckoutCone
+ ) &&
+
+ cleanup_clone $enlistment
+'
+
+test_expect_success '--single-branch clones HEAD only' '
+ enlistment=single-branch &&
+
+ scalar clone --single-branch "file://$(pwd)/to-clone" $enlistment &&
+ (
+ cd $enlistment/src &&
+ git for-each-ref refs/remotes/origin >out &&
+ test_line_count = 1 out &&
+ grep "refs/remotes/origin/base" out
+ ) &&
+
+ cleanup_clone $enlistment
+'
+
+test_expect_success '--no-single-branch clones all branches' '
+ enlistment=no-single-branch &&
+
+ scalar clone --no-single-branch "file://$(pwd)/to-clone" $enlistment &&
+ (
+ cd $enlistment/src &&
+ git for-each-ref refs/remotes/origin >out &&
+ test_line_count = 2 out &&
+ grep "refs/remotes/origin/base" out &&
+ grep "refs/remotes/origin/parallel" out
+ ) &&
+
+ cleanup_clone $enlistment
+'
+
+test_expect_success TTY 'progress with tty' '
+ enlistment=progress1 &&
+
+ test_config -C to-clone uploadpack.allowfilter true &&
+ test_config -C to-clone uploadpack.allowanysha1inwant true &&
+
+ test_terminal env GIT_PROGRESS_DELAY=0 \
+ scalar clone "file://$(pwd)/to-clone" "$enlistment" 2>stderr &&
+ grep "Enumerating objects" stderr >actual &&
+ test_line_count = 2 actual &&
+ cleanup_clone $enlistment
+'
+
+test_expect_success 'progress without tty' '
+ enlistment=progress2 &&
+
+ test_config -C to-clone uploadpack.allowfilter true &&
+ test_config -C to-clone uploadpack.allowanysha1inwant true &&
+
+ GIT_PROGRESS_DELAY=0 scalar clone "file://$(pwd)/to-clone" "$enlistment" 2>stderr &&
+ ! grep "Enumerating objects" stderr &&
+ ! grep "Updating files" stderr &&
+ cleanup_clone $enlistment
+'
+
+test_expect_success 'scalar clone warns when background maintenance fails' '
+ GIT_TEST_MAINT_SCHEDULER="crontab:false,launchctl:false,schtasks:false" \
+ scalar clone "file://$(pwd)/to-clone" maint-fail 2>err &&
+ grep "could not turn on maintenance" err
+'
+
+test_expect_success '`scalar clone --no-src`' '
+ scalar clone --src "file://$(pwd)/to-clone" with-src &&
+ scalar clone --no-src "file://$(pwd)/to-clone" without-src &&
+
+ test_path_is_dir with-src/src &&
+ test_path_is_missing without-src/src &&
+
+ (cd with-src/src && ls ?*) >with &&
+ (cd without-src && ls ?*) >without &&
+ test_cmp with without
+'
+
+test_done
diff --git a/t/t9300-fast-import.sh b/t/t9300-fast-import.sh
new file mode 100755
index 0000000..dbb5042
--- /dev/null
+++ b/t/t9300-fast-import.sh
@@ -0,0 +1,3531 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Shawn Pearce
+#
+
+test_description='test git fast-import utility'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-diff.sh ;# test-lib chdir's into trash
+
+verify_packs () {
+ for p in .git/objects/pack/*.pack
+ do
+ git verify-pack "$@" "$p" || return
+ done
+}
+
+file2_data='file2
+second line of EOF'
+
+file3_data='EOF
+in 3rd file
+ END'
+
+file4_data=abcd
+file4_len=4
+
+file5_data='an inline file.
+ we should see it later.'
+
+file6_data='#!/bin/sh
+echo "$@"'
+
+###
+### series A
+###
+
+test_expect_success 'empty stream succeeds' '
+ git config fastimport.unpackLimit 0 &&
+ git fast-import </dev/null
+'
+
+test_expect_success 'truncated stream complains' '
+ echo "tag foo" | test_must_fail git fast-import
+'
+
+test_expect_success 'A: create pack from stdin' '
+ test_tick &&
+ cat >input <<-INPUT_END &&
+ blob
+ mark :2
+ data <<EOF
+ $file2_data
+ EOF
+
+ blob
+ mark :3
+ data <<END
+ $file3_data
+ END
+
+ blob
+ mark :4
+ data $file4_len
+ $file4_data
+ commit refs/heads/main
+ mark :5
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ initial
+ COMMIT
+
+ M 644 :2 file2
+ M 644 :3 file3
+ M 755 :4 file4
+
+ tag series-A
+ from :5
+ data <<EOF
+ An annotated tag without a tagger
+ EOF
+
+ tag series-A-blob
+ from :3
+ data <<EOF
+ An annotated tag that annotates a blob.
+ EOF
+
+ tag to-be-deleted
+ from :3
+ data <<EOF
+ Another annotated tag that annotates a blob.
+ EOF
+
+ reset refs/tags/to-be-deleted
+ from $ZERO_OID
+
+ tag nested
+ mark :6
+ from :4
+ data <<EOF
+ Tag of our lovely commit
+ EOF
+
+ reset refs/tags/nested
+ from $ZERO_OID
+
+ tag nested
+ mark :7
+ from :6
+ data <<EOF
+ Tag of tag of our lovely commit
+ EOF
+
+ alias
+ mark :8
+ to :5
+
+ INPUT_END
+ git fast-import --export-marks=marks.out <input &&
+ git whatchanged main
+'
+
+test_expect_success 'A: verify pack' '
+ verify_packs
+'
+
+test_expect_success 'A: verify commit' '
+ cat >expect <<-EOF &&
+ author $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+
+ initial
+ EOF
+ git cat-file commit main | sed 1d >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'A: verify tree' '
+ cat >expect <<-EOF &&
+ 100644 blob file2
+ 100644 blob file3
+ 100755 blob file4
+ EOF
+ git cat-file -p main^{tree} | sed "s/ [0-9a-f]* / /" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'A: verify file2' '
+ echo "$file2_data" >expect &&
+ git cat-file blob main:file2 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'A: verify file3' '
+ echo "$file3_data" >expect &&
+ git cat-file blob main:file3 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'A: verify file4' '
+ printf "$file4_data" >expect &&
+ git cat-file blob main:file4 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'A: verify tag/series-A' '
+ cat >expect <<-EOF &&
+ object $(git rev-parse refs/heads/main)
+ type commit
+ tag series-A
+
+ An annotated tag without a tagger
+ EOF
+ git cat-file tag tags/series-A >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'A: verify tag/series-A-blob' '
+ cat >expect <<-EOF &&
+ object $(git rev-parse refs/heads/main:file3)
+ type blob
+ tag series-A-blob
+
+ An annotated tag that annotates a blob.
+ EOF
+ git cat-file tag tags/series-A-blob >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'A: verify tag deletion is successful' '
+ test_must_fail git rev-parse --verify refs/tags/to-be-deleted
+'
+
+test_expect_success 'A: verify marks output' '
+ cat >expect <<-EOF &&
+ :2 $(git rev-parse --verify main:file2)
+ :3 $(git rev-parse --verify main:file3)
+ :4 $(git rev-parse --verify main:file4)
+ :5 $(git rev-parse --verify main^0)
+ :6 $(git cat-file tag nested | grep object | cut -d" " -f 2)
+ :7 $(git rev-parse --verify nested)
+ :8 $(git rev-parse --verify main^0)
+ EOF
+ test_cmp expect marks.out
+'
+
+test_expect_success 'A: verify marks import' '
+ git fast-import \
+ --import-marks=marks.out \
+ --export-marks=marks.new \
+ </dev/null &&
+ test_cmp expect marks.new
+'
+
+test_expect_success 'A: tag blob by sha1' '
+ test_tick &&
+ new_blob=$(echo testing | git hash-object --stdin) &&
+ cat >input <<-INPUT_END &&
+ tag series-A-blob-2
+ from $(git rev-parse refs/heads/main:file3)
+ data <<EOF
+ Tag blob by sha1.
+ EOF
+
+ blob
+ mark :6
+ data <<EOF
+ testing
+ EOF
+
+ commit refs/heads/new_blob
+ committer <> 0 +0000
+ data 0
+ M 644 :6 new_blob
+ #pretend we got sha1 from fast-import
+ ls "new_blob"
+
+ tag series-A-blob-3
+ from $new_blob
+ data <<EOF
+ Tag new_blob.
+ EOF
+ INPUT_END
+
+ cat >expect <<-EOF &&
+ object $(git rev-parse refs/heads/main:file3)
+ type blob
+ tag series-A-blob-2
+
+ Tag blob by sha1.
+ object $new_blob
+ type blob
+ tag series-A-blob-3
+
+ Tag new_blob.
+ EOF
+
+ git fast-import <input &&
+ git cat-file tag tags/series-A-blob-2 >actual &&
+ git cat-file tag tags/series-A-blob-3 >>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'A: verify marks import does not crash' '
+ test_tick &&
+ cat >input <<-INPUT_END &&
+ commit refs/heads/verify--import-marks
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ recreate from :5
+ COMMIT
+
+ from :5
+ M 755 :2 copy-of-file2
+
+ INPUT_END
+
+ git fast-import --import-marks=marks.out <input &&
+ git whatchanged verify--import-marks
+'
+
+test_expect_success 'A: verify pack' '
+ verify_packs
+'
+
+test_expect_success 'A: verify diff' '
+ copy=$(git rev-parse --verify main:file2) &&
+ cat >expect <<-EOF &&
+ :000000 100755 $ZERO_OID $copy A copy-of-file2
+ EOF
+ git diff-tree -M -r main verify--import-marks >actual &&
+ compare_diff_raw expect actual &&
+ test $(git rev-parse --verify main:file2) \
+ = $(git rev-parse --verify verify--import-marks:copy-of-file2)
+'
+
+test_expect_success 'A: export marks with large values' '
+ test_tick &&
+ mt=$(git hash-object --stdin < /dev/null) &&
+ >input.blob &&
+ >marks.exp &&
+ >tree.exp &&
+
+ cat >input.commit <<-EOF &&
+ commit refs/heads/verify--dump-marks
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ test the sparse array dumping routines with exponentially growing marks
+ COMMIT
+ EOF
+
+ i=0 l=4 m=6 n=7 &&
+ while test "$i" -lt 27
+ do
+ cat >>input.blob <<-EOF &&
+ blob
+ mark :$l
+ data 0
+ blob
+ mark :$m
+ data 0
+ blob
+ mark :$n
+ data 0
+ EOF
+ echo "M 100644 :$l l$i" >>input.commit &&
+ echo "M 100644 :$m m$i" >>input.commit &&
+ echo "M 100644 :$n n$i" >>input.commit &&
+
+ echo ":$l $mt" >>marks.exp &&
+ echo ":$m $mt" >>marks.exp &&
+ echo ":$n $mt" >>marks.exp &&
+
+ printf "100644 blob $mt\tl$i\n" >>tree.exp &&
+ printf "100644 blob $mt\tm$i\n" >>tree.exp &&
+ printf "100644 blob $mt\tn$i\n" >>tree.exp &&
+
+ l=$(($l + $l)) &&
+ m=$(($m + $m)) &&
+ n=$(($l + $n)) &&
+
+ i=$((1 + $i)) || return 1
+ done &&
+
+ sort tree.exp > tree.exp_s &&
+
+ cat input.blob input.commit | git fast-import --export-marks=marks.large &&
+ git ls-tree refs/heads/verify--dump-marks >tree.out &&
+ test_cmp tree.exp_s tree.out &&
+ test_cmp marks.exp marks.large
+'
+
+###
+### series B
+###
+
+test_expect_success 'B: fail on invalid blob sha1' '
+ test_tick &&
+ cat >input <<-INPUT_END &&
+ commit refs/heads/branch
+ mark :1
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ corrupt
+ COMMIT
+
+ from refs/heads/main
+ M 755 $(echo $ZERO_OID | sed -e "s/0$/1/") zero1
+
+ INPUT_END
+
+ test_when_finished "rm -f .git/objects/pack_* .git/objects/index_*" &&
+ test_must_fail git fast-import <input
+'
+
+test_expect_success 'B: accept branch name "TEMP_TAG"' '
+ cat >input <<-INPUT_END &&
+ commit TEMP_TAG
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ tag base
+ COMMIT
+
+ from refs/heads/main
+
+ INPUT_END
+
+ test_when_finished "rm -f .git/TEMP_TAG && git gc --prune=now" &&
+ git fast-import <input &&
+ test $(test-tool ref-store main resolve-ref TEMP_TAG 0 | cut -f1 -d " " ) != "$ZERO_OID" &&
+ test $(git rev-parse main) = $(git rev-parse TEMP_TAG^)
+'
+
+test_expect_success 'B: accept empty committer' '
+ cat >input <<-INPUT_END &&
+ commit refs/heads/empty-committer-1
+ committer <> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ empty commit
+ COMMIT
+ INPUT_END
+
+ test_when_finished "git update-ref -d refs/heads/empty-committer-1
+ git gc --prune=now" &&
+ git fast-import <input &&
+ out=$(git fsck) &&
+ echo "$out" &&
+ test -z "$out"
+'
+
+test_expect_success 'B: reject invalid timezone' '
+ cat >input <<-INPUT_END &&
+ commit refs/heads/invalid-timezone
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1234567890 +051800
+ data <<COMMIT
+ empty commit
+ COMMIT
+ INPUT_END
+
+ test_when_finished "git update-ref -d refs/heads/invalid-timezone" &&
+ test_must_fail git fast-import <input
+'
+
+test_expect_success 'B: accept invalid timezone with raw-permissive' '
+ cat >input <<-INPUT_END &&
+ commit refs/heads/invalid-timezone
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1234567890 +051800
+ data <<COMMIT
+ empty commit
+ COMMIT
+ INPUT_END
+
+ git init invalid-timezone &&
+ git -C invalid-timezone fast-import --date-format=raw-permissive <input &&
+ git -C invalid-timezone cat-file -p invalid-timezone >out &&
+ grep "1234567890 [+]051800" out
+'
+
+test_expect_success 'B: accept and fixup committer with no name' '
+ cat >input <<-INPUT_END &&
+ commit refs/heads/empty-committer-2
+ committer <a@b.com> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ empty commit
+ COMMIT
+ INPUT_END
+
+ test_when_finished "git update-ref -d refs/heads/empty-committer-2
+ git gc --prune=now" &&
+ git fast-import <input &&
+ out=$(git fsck) &&
+ echo "$out" &&
+ test -z "$out"
+'
+
+test_expect_success 'B: fail on invalid committer (1)' '
+ cat >input <<-INPUT_END &&
+ commit refs/heads/invalid-committer
+ committer Name email> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ empty commit
+ COMMIT
+ INPUT_END
+
+ test_when_finished "git update-ref -d refs/heads/invalid-committer" &&
+ test_must_fail git fast-import <input
+'
+
+test_expect_success 'B: fail on invalid committer (2)' '
+ cat >input <<-INPUT_END &&
+ commit refs/heads/invalid-committer
+ committer Name <e<mail> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ empty commit
+ COMMIT
+ INPUT_END
+
+ test_when_finished "git update-ref -d refs/heads/invalid-committer" &&
+ test_must_fail git fast-import <input
+'
+
+test_expect_success 'B: fail on invalid committer (3)' '
+ cat >input <<-INPUT_END &&
+ commit refs/heads/invalid-committer
+ committer Name <email>> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ empty commit
+ COMMIT
+ INPUT_END
+
+ test_when_finished "git update-ref -d refs/heads/invalid-committer" &&
+ test_must_fail git fast-import <input
+'
+
+test_expect_success 'B: fail on invalid committer (4)' '
+ cat >input <<-INPUT_END &&
+ commit refs/heads/invalid-committer
+ committer Name <email $GIT_COMMITTER_DATE
+ data <<COMMIT
+ empty commit
+ COMMIT
+ INPUT_END
+
+ test_when_finished "git update-ref -d refs/heads/invalid-committer" &&
+ test_must_fail git fast-import <input
+'
+
+test_expect_success 'B: fail on invalid committer (5)' '
+ cat >input <<-INPUT_END &&
+ commit refs/heads/invalid-committer
+ committer Name<email> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ empty commit
+ COMMIT
+ INPUT_END
+
+ test_when_finished "git update-ref -d refs/heads/invalid-committer" &&
+ test_must_fail git fast-import <input
+'
+
+###
+### series C
+###
+
+test_expect_success 'C: incremental import create pack from stdin' '
+ newf=$(echo hi newf | git hash-object -w --stdin) &&
+ oldf=$(git rev-parse --verify main:file2) &&
+ thrf=$(git rev-parse --verify main:file3) &&
+ test_tick &&
+ cat >input <<-INPUT_END &&
+ commit refs/heads/branch
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ second
+ COMMIT
+
+ from refs/heads/main
+ M 644 $oldf file2/oldf
+ M 755 $newf file2/newf
+ D file3
+
+ INPUT_END
+
+ git fast-import <input &&
+ git whatchanged branch
+'
+
+test_expect_success 'C: verify pack' '
+ verify_packs
+'
+
+test_expect_success 'C: validate reuse existing blob' '
+ test $newf = $(git rev-parse --verify branch:file2/newf) &&
+ test $oldf = $(git rev-parse --verify branch:file2/oldf)
+'
+
+test_expect_success 'C: verify commit' '
+ cat >expect <<-EOF &&
+ parent $(git rev-parse --verify main^0)
+ author $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+
+ second
+ EOF
+
+ git cat-file commit branch | sed 1d >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'C: validate rename result' '
+ zero=$ZERO_OID &&
+ cat >expect <<-EOF &&
+ :000000 100755 $zero $newf A file2/newf
+ :100644 100644 $oldf $oldf R100 file2 file2/oldf
+ :100644 000000 $thrf $zero D file3
+ EOF
+ git diff-tree -M -r main branch >actual &&
+ compare_diff_raw expect actual
+'
+
+###
+### series D
+###
+
+test_expect_success 'D: inline data in commit' '
+ test_tick &&
+ cat >input <<-INPUT_END &&
+ commit refs/heads/branch
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ third
+ COMMIT
+
+ from refs/heads/branch^0
+ M 644 inline newdir/interesting
+ data <<EOF
+ $file5_data
+ EOF
+
+ M 755 inline newdir/exec.sh
+ data <<EOF
+ $file6_data
+ EOF
+
+ INPUT_END
+
+ git fast-import <input &&
+ git whatchanged branch
+'
+
+test_expect_success 'D: verify pack' '
+ verify_packs
+'
+
+test_expect_success 'D: validate new files added' '
+ f5id=$(echo "$file5_data" | git hash-object --stdin) &&
+ f6id=$(echo "$file6_data" | git hash-object --stdin) &&
+ cat >expect <<-EOF &&
+ :000000 100755 $ZERO_OID $f6id A newdir/exec.sh
+ :000000 100644 $ZERO_OID $f5id A newdir/interesting
+ EOF
+ git diff-tree -M -r branch^ branch >actual &&
+ compare_diff_raw expect actual
+'
+
+test_expect_success 'D: verify file5' '
+ echo "$file5_data" >expect &&
+ git cat-file blob branch:newdir/interesting >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'D: verify file6' '
+ echo "$file6_data" >expect &&
+ git cat-file blob branch:newdir/exec.sh >actual &&
+ test_cmp expect actual
+'
+
+###
+### series E
+###
+
+test_expect_success 'E: rfc2822 date, --date-format=raw' '
+ cat >input <<-INPUT_END &&
+ commit refs/heads/branch
+ author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> Tue Feb 6 11:22:18 2007 -0500
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> Tue Feb 6 12:35:02 2007 -0500
+ data <<COMMIT
+ RFC 2822 type date
+ COMMIT
+
+ from refs/heads/branch^0
+
+ INPUT_END
+
+ test_must_fail git fast-import --date-format=raw <input
+'
+test_expect_success 'E: rfc2822 date, --date-format=rfc2822' '
+ git fast-import --date-format=rfc2822 <input
+'
+
+test_expect_success 'E: verify pack' '
+ verify_packs
+'
+
+test_expect_success 'E: verify commit' '
+ cat >expect <<-EOF &&
+ author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> 1170778938 -0500
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1170783302 -0500
+
+ RFC 2822 type date
+ EOF
+ git cat-file commit branch | sed 1,2d >actual &&
+ test_cmp expect actual
+'
+
+###
+### series F
+###
+
+test_expect_success 'F: non-fast-forward update skips' '
+ old_branch=$(git rev-parse --verify branch^0) &&
+ test_tick &&
+ cat >input <<-INPUT_END &&
+ commit refs/heads/branch
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ losing things already?
+ COMMIT
+
+ from refs/heads/branch~1
+
+ reset refs/heads/other
+ from refs/heads/branch
+
+ INPUT_END
+
+ test_must_fail git fast-import <input &&
+ # branch must remain unaffected
+ test $old_branch = $(git rev-parse --verify branch^0)
+'
+
+test_expect_success 'F: verify pack' '
+ verify_packs
+'
+
+test_expect_success 'F: verify other commit' '
+ cat >expect <<-EOF &&
+ tree $(git rev-parse branch~1^{tree})
+ parent $(git rev-parse branch~1)
+ author $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+
+ losing things already?
+ EOF
+ git cat-file commit other >actual &&
+ test_cmp expect actual
+'
+
+###
+### series G
+###
+
+test_expect_success 'G: non-fast-forward update forced' '
+ old_branch=$(git rev-parse --verify branch^0) &&
+ test_tick &&
+ cat >input <<-INPUT_END &&
+ commit refs/heads/branch
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ losing things already?
+ COMMIT
+
+ from refs/heads/branch~1
+
+ INPUT_END
+ git fast-import --force <input
+'
+
+test_expect_success 'G: verify pack' '
+ verify_packs
+'
+
+test_expect_success 'G: branch changed, but logged' '
+ test $old_branch != $(git rev-parse --verify branch^0) &&
+ test $old_branch = $(git rev-parse --verify branch@{1})
+'
+
+###
+### series H
+###
+
+test_expect_success 'H: deletall, add 1' '
+ test_tick &&
+ cat >input <<-INPUT_END &&
+ commit refs/heads/H
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ third
+ COMMIT
+
+ from refs/heads/branch^0
+ M 644 inline i-will-die
+ data <<EOF
+ this file will never exist.
+ EOF
+
+ deleteall
+ M 644 inline h/e/l/lo
+ data <<EOF
+ $file5_data
+ EOF
+
+ INPUT_END
+ git fast-import <input &&
+ git whatchanged H
+'
+
+test_expect_success 'H: verify pack' '
+ verify_packs
+'
+
+test_expect_success 'H: validate old files removed, new files added' '
+ f4id=$(git rev-parse HEAD:file4) &&
+ cat >expect <<-EOF &&
+ :100755 000000 $newf $zero D file2/newf
+ :100644 000000 $oldf $zero D file2/oldf
+ :100755 000000 $f4id $zero D file4
+ :100644 100644 $f5id $f5id R100 newdir/interesting h/e/l/lo
+ :100755 000000 $f6id $zero D newdir/exec.sh
+ EOF
+ git diff-tree -M -r H^ H >actual &&
+ compare_diff_raw expect actual
+'
+
+test_expect_success 'H: verify file' '
+ echo "$file5_data" >expect &&
+ git cat-file blob H:h/e/l/lo >actual &&
+ test_cmp expect actual
+'
+
+###
+### series I
+###
+
+test_expect_success 'I: export-pack-edges' '
+ cat >input <<-INPUT_END &&
+ commit refs/heads/export-boundary
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ we have a border. its only 40 characters wide.
+ COMMIT
+
+ from refs/heads/branch
+
+ INPUT_END
+ git fast-import --export-pack-edges=edges.list <input
+'
+
+test_expect_success 'I: verify edge list' '
+ cat >expect <<-EOF &&
+ .git/objects/pack/pack-.pack: $(git rev-parse --verify export-boundary)
+ EOF
+ sed -e s/pack-.*pack/pack-.pack/ edges.list >actual &&
+ test_cmp expect actual
+'
+
+###
+### series J
+###
+
+test_expect_success 'J: reset existing branch creates empty commit' '
+ cat >input <<-INPUT_END &&
+ commit refs/heads/J
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ create J
+ COMMIT
+
+ from refs/heads/branch
+
+ reset refs/heads/J
+
+ commit refs/heads/J
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ initialize J
+ COMMIT
+
+ INPUT_END
+ git fast-import <input
+'
+test_expect_success 'J: branch has 1 commit, empty tree' '
+ test 1 = $(git rev-list J | wc -l) &&
+ test 0 = $(git ls-tree J | wc -l)
+'
+
+test_expect_success 'J: tag must fail on empty branch' '
+ cat >input <<-INPUT_END &&
+ reset refs/heads/J2
+
+ tag wrong_tag
+ from refs/heads/J2
+ data <<EOF
+ Tag branch that was reset.
+ EOF
+ INPUT_END
+ test_must_fail git fast-import <input
+'
+
+###
+### series K
+###
+
+test_expect_success 'K: reinit branch with from' '
+ cat >input <<-INPUT_END &&
+ commit refs/heads/K
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ create K
+ COMMIT
+
+ from refs/heads/branch
+
+ commit refs/heads/K
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ redo K
+ COMMIT
+
+ from refs/heads/branch^1
+
+ INPUT_END
+ git fast-import <input
+'
+test_expect_success 'K: verify K^1 = branch^1' '
+ test $(git rev-parse --verify branch^1) \
+ = $(git rev-parse --verify K^1)
+'
+
+###
+### series L
+###
+
+test_expect_success 'L: verify internal tree sorting' '
+ cat >input <<-INPUT_END &&
+ blob
+ mark :1
+ data <<EOF
+ some data
+ EOF
+
+ blob
+ mark :2
+ data <<EOF
+ other data
+ EOF
+
+ commit refs/heads/L
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ create L
+ COMMIT
+
+ M 644 :1 b.
+ M 644 :1 b/other
+ M 644 :1 ba
+
+ commit refs/heads/L
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ update L
+ COMMIT
+
+ M 644 :2 b.
+ M 644 :2 b/other
+ M 644 :2 ba
+ INPUT_END
+
+ cat >expect <<-EXPECT_END &&
+ :100644 100644 M b.
+ :040000 040000 M b
+ :100644 100644 M ba
+ EXPECT_END
+
+ git fast-import <input &&
+ GIT_PRINT_SHA1_ELLIPSIS="yes" git diff-tree --abbrev --raw L^ L >output &&
+ cut -d" " -f1,2,5 output >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'L: nested tree copy does not corrupt deltas' '
+ cat >input <<-INPUT_END &&
+ blob
+ mark :1
+ data <<EOF
+ the data
+ EOF
+
+ commit refs/heads/L2
+ committer C O Mitter <committer@example.com> 1112912473 -0700
+ data <<COMMIT
+ init L2
+ COMMIT
+ M 644 :1 a/b/c
+ M 644 :1 a/b/d
+ M 644 :1 a/e/f
+
+ commit refs/heads/L2
+ committer C O Mitter <committer@example.com> 1112912473 -0700
+ data <<COMMIT
+ update L2
+ COMMIT
+ C a g
+ C a/e g/b
+ M 644 :1 g/b/h
+ INPUT_END
+
+ cat >expect <<-\EOF &&
+ g/b/f
+ g/b/h
+ EOF
+
+ test_when_finished "git update-ref -d refs/heads/L2" &&
+ git fast-import <input &&
+ git ls-tree L2 g/b/ >tmp &&
+ cat tmp | cut -f 2 >actual &&
+ test_cmp expect actual &&
+ git fsck $(git rev-parse L2)
+'
+
+###
+### series M
+###
+
+test_expect_success 'M: rename file in same subdirectory' '
+ test_tick &&
+ cat >input <<-INPUT_END &&
+ commit refs/heads/M1
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ file rename
+ COMMIT
+
+ from refs/heads/branch^0
+ R file2/newf file2/n.e.w.f
+
+ INPUT_END
+
+ cat >expect <<-EOF &&
+ :100755 100755 $newf $newf R100 file2/newf file2/n.e.w.f
+ EOF
+ git fast-import <input &&
+ git diff-tree -M -r M1^ M1 >actual &&
+ compare_diff_raw expect actual
+'
+
+test_expect_success 'M: rename file to new subdirectory' '
+ cat >input <<-INPUT_END &&
+ commit refs/heads/M2
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ file rename
+ COMMIT
+
+ from refs/heads/branch^0
+ R file2/newf i/am/new/to/you
+
+ INPUT_END
+
+ cat >expect <<-EOF &&
+ :100755 100755 $newf $newf R100 file2/newf i/am/new/to/you
+ EOF
+ git fast-import <input &&
+ git diff-tree -M -r M2^ M2 >actual &&
+ compare_diff_raw expect actual
+'
+
+test_expect_success 'M: rename subdirectory to new subdirectory' '
+ cat >input <<-INPUT_END &&
+ commit refs/heads/M3
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ file rename
+ COMMIT
+
+ from refs/heads/M2^0
+ R i other/sub
+
+ INPUT_END
+
+ cat >expect <<-EOF &&
+ :100755 100755 $newf $newf R100 i/am/new/to/you other/sub/am/new/to/you
+ EOF
+ git fast-import <input &&
+ git diff-tree -M -r M3^ M3 >actual &&
+ compare_diff_raw expect actual
+'
+
+test_expect_success 'M: rename root to subdirectory' '
+ cat >input <<-INPUT_END &&
+ commit refs/heads/M4
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ rename root
+ COMMIT
+
+ from refs/heads/M2^0
+ R "" sub
+
+ INPUT_END
+
+ cat >expect <<-EOF &&
+ :100644 100644 $oldf $oldf R100 file2/oldf sub/file2/oldf
+ :100755 100755 $f4id $f4id R100 file4 sub/file4
+ :100755 100755 $newf $newf R100 i/am/new/to/you sub/i/am/new/to/you
+ :100755 100755 $f6id $f6id R100 newdir/exec.sh sub/newdir/exec.sh
+ :100644 100644 $f5id $f5id R100 newdir/interesting sub/newdir/interesting
+ EOF
+ git fast-import <input &&
+ git diff-tree -M -r M4^ M4 >actual &&
+ compare_diff_raw expect actual
+'
+
+###
+### series N
+###
+
+test_expect_success 'N: copy file in same subdirectory' '
+ test_tick &&
+ cat >input <<-INPUT_END &&
+ commit refs/heads/N1
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ file copy
+ COMMIT
+
+ from refs/heads/branch^0
+ C file2/newf file2/n.e.w.f
+
+ INPUT_END
+
+ cat >expect <<-EOF &&
+ :100755 100755 $newf $newf C100 file2/newf file2/n.e.w.f
+ EOF
+ git fast-import <input &&
+ git diff-tree -C --find-copies-harder -r N1^ N1 >actual &&
+ compare_diff_raw expect actual
+'
+
+test_expect_success 'N: copy then modify subdirectory' '
+ cat >input <<-INPUT_END &&
+ commit refs/heads/N2
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ clean directory copy
+ COMMIT
+
+ from refs/heads/branch^0
+ C file2 file3
+
+ commit refs/heads/N2
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ modify directory copy
+ COMMIT
+
+ M 644 inline file3/file5
+ data <<EOF
+ $file5_data
+ EOF
+
+ INPUT_END
+
+ cat >expect <<-EOF &&
+ :100644 100644 $f5id $f5id C100 newdir/interesting file3/file5
+ :100755 100755 $newf $newf C100 file2/newf file3/newf
+ :100644 100644 $oldf $oldf C100 file2/oldf file3/oldf
+ EOF
+ git fast-import <input &&
+ git diff-tree -C --find-copies-harder -r N2^^ N2 >actual &&
+ compare_diff_raw expect actual
+'
+
+test_expect_success 'N: copy dirty subdirectory' '
+ cat >input <<-INPUT_END &&
+ commit refs/heads/N3
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ dirty directory copy
+ COMMIT
+
+ from refs/heads/branch^0
+ M 644 inline file2/file5
+ data <<EOF
+ $file5_data
+ EOF
+
+ C file2 file3
+ D file2/file5
+
+ INPUT_END
+
+ git fast-import <input &&
+ test $(git rev-parse N2^{tree}) = $(git rev-parse N3^{tree})
+'
+
+test_expect_success 'N: copy directory by id' '
+ cat >expect <<-EOF &&
+ :100755 100755 $newf $newf C100 file2/newf file3/newf
+ :100644 100644 $oldf $oldf C100 file2/oldf file3/oldf
+ EOF
+ subdir=$(git rev-parse refs/heads/branch^0:file2) &&
+ cat >input <<-INPUT_END &&
+ commit refs/heads/N4
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ copy by tree hash
+ COMMIT
+
+ from refs/heads/branch^0
+ M 040000 $subdir file3
+ INPUT_END
+ git fast-import <input &&
+ git diff-tree -C --find-copies-harder -r N4^ N4 >actual &&
+ compare_diff_raw expect actual
+'
+
+test_expect_success PIPE 'N: read and copy directory' '
+ cat >expect <<-EOF &&
+ :100755 100755 $newf $newf C100 file2/newf file3/newf
+ :100644 100644 $oldf $oldf C100 file2/oldf file3/oldf
+ EOF
+ git update-ref -d refs/heads/N4 &&
+ rm -f backflow &&
+ mkfifo backflow &&
+ (
+ exec <backflow &&
+ cat <<-EOF &&
+ commit refs/heads/N4
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ copy by tree hash, part 2
+ COMMIT
+
+ from refs/heads/branch^0
+ ls "file2"
+ EOF
+ read mode type tree filename &&
+ echo "M 040000 $tree file3"
+ ) |
+ git fast-import --cat-blob-fd=3 3>backflow &&
+ git diff-tree -C --find-copies-harder -r N4^ N4 >actual &&
+ compare_diff_raw expect actual
+'
+
+test_expect_success PIPE 'N: empty directory reads as missing' '
+ cat <<-\EOF >expect &&
+ OBJNAME
+ :000000 100644 OBJNAME OBJNAME A unrelated
+ EOF
+ echo "missing src" >expect.response &&
+ git update-ref -d refs/heads/read-empty &&
+ rm -f backflow &&
+ mkfifo backflow &&
+ (
+ exec <backflow &&
+ cat <<-EOF &&
+ commit refs/heads/read-empty
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ read "empty" (missing) directory
+ COMMIT
+
+ M 100644 inline src/greeting
+ data <<BLOB
+ hello
+ BLOB
+ C src/greeting dst1/non-greeting
+ C src/greeting unrelated
+ # leave behind "empty" src directory
+ D src/greeting
+ ls "src"
+ EOF
+ read -r line &&
+ printf "%s\n" "$line" >response &&
+ cat <<-\EOF
+ D dst1
+ D dst2
+ EOF
+ ) |
+ git fast-import --cat-blob-fd=3 3>backflow &&
+ test_cmp expect.response response &&
+ git rev-list read-empty |
+ git diff-tree -r --root --stdin |
+ sed "s/$OID_REGEX/OBJNAME/g" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'N: copy root directory by tree hash' '
+ cat >expect <<-EOF &&
+ :100755 000000 $newf $zero D file3/newf
+ :100644 000000 $oldf $zero D file3/oldf
+ EOF
+ root=$(git rev-parse refs/heads/branch^0^{tree}) &&
+ cat >input <<-INPUT_END &&
+ commit refs/heads/N6
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ copy root directory by tree hash
+ COMMIT
+
+ from refs/heads/branch^0
+ M 040000 $root ""
+ INPUT_END
+ git fast-import <input &&
+ git diff-tree -C --find-copies-harder -r N4 N6 >actual &&
+ compare_diff_raw expect actual
+'
+
+test_expect_success 'N: copy root by path' '
+ cat >expect <<-EOF &&
+ :100755 100755 $newf $newf C100 file2/newf oldroot/file2/newf
+ :100644 100644 $oldf $oldf C100 file2/oldf oldroot/file2/oldf
+ :100755 100755 $f4id $f4id C100 file4 oldroot/file4
+ :100755 100755 $f6id $f6id C100 newdir/exec.sh oldroot/newdir/exec.sh
+ :100644 100644 $f5id $f5id C100 newdir/interesting oldroot/newdir/interesting
+ EOF
+ cat >input <<-INPUT_END &&
+ commit refs/heads/N-copy-root-path
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ copy root directory by (empty) path
+ COMMIT
+
+ from refs/heads/branch^0
+ C "" oldroot
+ INPUT_END
+ git fast-import <input &&
+ git diff-tree -C --find-copies-harder -r branch N-copy-root-path >actual &&
+ compare_diff_raw expect actual
+'
+
+test_expect_success 'N: delete directory by copying' '
+ cat >expect <<-\EOF &&
+ OBJID
+ :100644 000000 OBJID OBJID D foo/bar/qux
+ OBJID
+ :000000 100644 OBJID OBJID A foo/bar/baz
+ :000000 100644 OBJID OBJID A foo/bar/qux
+ EOF
+ empty_tree=$(git mktree </dev/null) &&
+ cat >input <<-INPUT_END &&
+ commit refs/heads/N-delete
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ collect data to be deleted
+ COMMIT
+
+ deleteall
+ M 100644 inline foo/bar/baz
+ data <<DATA_END
+ hello
+ DATA_END
+ C "foo/bar/baz" "foo/bar/qux"
+ C "foo/bar/baz" "foo/bar/quux/1"
+ C "foo/bar/baz" "foo/bar/quuux"
+ M 040000 $empty_tree foo/bar/quux
+ M 040000 $empty_tree foo/bar/quuux
+
+ commit refs/heads/N-delete
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ delete subdirectory
+ COMMIT
+
+ M 040000 $empty_tree foo/bar/qux
+ INPUT_END
+ git fast-import <input &&
+ git rev-list N-delete |
+ git diff-tree -r --stdin --root --always |
+ sed -e "s/$OID_REGEX/OBJID/g" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'N: modify copied tree' '
+ cat >expect <<-EOF &&
+ :100644 100644 $f5id $f5id C100 newdir/interesting file3/file5
+ :100755 100755 $newf $newf C100 file2/newf file3/newf
+ :100644 100644 $oldf $oldf C100 file2/oldf file3/oldf
+ EOF
+ subdir=$(git rev-parse refs/heads/branch^0:file2) &&
+ cat >input <<-INPUT_END &&
+ commit refs/heads/N5
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ copy by tree hash
+ COMMIT
+
+ from refs/heads/branch^0
+ M 040000 $subdir file3
+
+ commit refs/heads/N5
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ modify directory copy
+ COMMIT
+
+ M 644 inline file3/file5
+ data <<EOF
+ $file5_data
+ EOF
+ INPUT_END
+ git fast-import <input &&
+ git diff-tree -C --find-copies-harder -r N5^^ N5 >actual &&
+ compare_diff_raw expect actual
+'
+
+test_expect_success 'N: reject foo/ syntax' '
+ subdir=$(git rev-parse refs/heads/branch^0:file2) &&
+ test_must_fail git fast-import <<-INPUT_END
+ commit refs/heads/N5B
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ copy with invalid syntax
+ COMMIT
+
+ from refs/heads/branch^0
+ M 040000 $subdir file3/
+ INPUT_END
+'
+
+test_expect_success 'N: reject foo/ syntax in copy source' '
+ test_must_fail git fast-import <<-INPUT_END
+ commit refs/heads/N5C
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ copy with invalid syntax
+ COMMIT
+
+ from refs/heads/branch^0
+ C file2/ file3
+ INPUT_END
+'
+
+test_expect_success 'N: reject foo/ syntax in rename source' '
+ test_must_fail git fast-import <<-INPUT_END
+ commit refs/heads/N5D
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ rename with invalid syntax
+ COMMIT
+
+ from refs/heads/branch^0
+ R file2/ file3
+ INPUT_END
+'
+
+test_expect_success 'N: reject foo/ syntax in ls argument' '
+ test_must_fail git fast-import <<-INPUT_END
+ commit refs/heads/N5E
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ copy with invalid syntax
+ COMMIT
+
+ from refs/heads/branch^0
+ ls "file2/"
+ INPUT_END
+'
+
+test_expect_success 'N: copy to root by id and modify' '
+ echo "hello, world" >expect.foo &&
+ echo hello >expect.bar &&
+ git fast-import <<-SETUP_END &&
+ commit refs/heads/N7
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ hello, tree
+ COMMIT
+
+ deleteall
+ M 644 inline foo/bar
+ data <<EOF
+ hello
+ EOF
+ SETUP_END
+
+ tree=$(git rev-parse --verify N7:) &&
+ git fast-import <<-INPUT_END &&
+ commit refs/heads/N8
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ copy to root by id and modify
+ COMMIT
+
+ M 040000 $tree ""
+ M 644 inline foo/foo
+ data <<EOF
+ hello, world
+ EOF
+ INPUT_END
+ git show N8:foo/foo >actual.foo &&
+ git show N8:foo/bar >actual.bar &&
+ test_cmp expect.foo actual.foo &&
+ test_cmp expect.bar actual.bar
+'
+
+test_expect_success 'N: extract subtree' '
+ branch=$(git rev-parse --verify refs/heads/branch^{tree}) &&
+ cat >input <<-INPUT_END &&
+ commit refs/heads/N9
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ extract subtree branch:newdir
+ COMMIT
+
+ M 040000 $branch ""
+ C "newdir" ""
+ INPUT_END
+ git fast-import <input &&
+ git diff --exit-code branch:newdir N9
+'
+
+test_expect_success 'N: modify subtree, extract it, and modify again' '
+ echo hello >expect.baz &&
+ echo hello, world >expect.qux &&
+ git fast-import <<-SETUP_END &&
+ commit refs/heads/N10
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ hello, tree
+ COMMIT
+
+ deleteall
+ M 644 inline foo/bar/baz
+ data <<EOF
+ hello
+ EOF
+ SETUP_END
+
+ tree=$(git rev-parse --verify N10:) &&
+ git fast-import <<-INPUT_END &&
+ commit refs/heads/N11
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ copy to root by id and modify
+ COMMIT
+
+ M 040000 $tree ""
+ M 100644 inline foo/bar/qux
+ data <<EOF
+ hello, world
+ EOF
+ R "foo" ""
+ C "bar/qux" "bar/quux"
+ INPUT_END
+ git show N11:bar/baz >actual.baz &&
+ git show N11:bar/qux >actual.qux &&
+ git show N11:bar/quux >actual.quux &&
+ test_cmp expect.baz actual.baz &&
+ test_cmp expect.qux actual.qux &&
+ test_cmp expect.qux actual.quux'
+
+###
+### series O
+###
+
+test_expect_success 'O: comments are all skipped' '
+ cat >input <<-INPUT_END &&
+ #we will
+ commit refs/heads/O1
+ # -- ignore all of this text
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ dirty directory copy
+ COMMIT
+
+ # do not forget the import blank line!
+ #
+ # yes, we started from our usual base of branch^0.
+ # i like branch^0.
+ from refs/heads/branch^0
+ # and we need to reuse file2/file5 from N3 above.
+ M 644 inline file2/file5
+ # otherwise the tree will be different
+ data <<EOF
+ $file5_data
+ EOF
+
+ # do not forget to copy file2 to file3
+ C file2 file3
+ #
+ # or to delete file5 from file2.
+ D file2/file5
+ # are we done yet?
+
+ INPUT_END
+
+ git fast-import <input &&
+ test $(git rev-parse N3) = $(git rev-parse O1)
+'
+
+test_expect_success 'O: blank lines not necessary after data commands' '
+ cat >input <<-INPUT_END &&
+ commit refs/heads/O2
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ dirty directory copy
+ COMMIT
+ from refs/heads/branch^0
+ M 644 inline file2/file5
+ data <<EOF
+ $file5_data
+ EOF
+ C file2 file3
+ D file2/file5
+
+ INPUT_END
+
+ git fast-import <input &&
+ test $(git rev-parse N3) = $(git rev-parse O2)
+'
+
+test_expect_success 'O: repack before next test' '
+ git repack -a -d
+'
+
+test_expect_success 'O: blank lines not necessary after other commands' '
+ cat >input <<-INPUT_END &&
+ commit refs/heads/O3
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ zstring
+ COMMIT
+ commit refs/heads/O3
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ zof
+ COMMIT
+ checkpoint
+ commit refs/heads/O3
+ mark :5
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ zempty
+ COMMIT
+ checkpoint
+ commit refs/heads/O3
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ zcommits
+ COMMIT
+ reset refs/tags/O3-2nd
+ from :5
+ reset refs/tags/O3-3rd
+ from :5
+ INPUT_END
+
+ cat >expect <<-INPUT_END &&
+ string
+ of
+ empty
+ commits
+ INPUT_END
+
+ git fast-import <input &&
+ ls -la .git/objects/pack/pack-*.pack >packlist &&
+ ls -la .git/objects/pack/pack-*.pack >idxlist &&
+ test_line_count = 4 idxlist &&
+ test_line_count = 4 packlist &&
+ test $(git rev-parse refs/tags/O3-2nd) = $(git rev-parse O3^) &&
+ git log --reverse --pretty=oneline O3 | sed s/^.*z// >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'O: progress outputs as requested by input' '
+ cat >input <<-INPUT_END &&
+ commit refs/heads/O4
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ zstring
+ COMMIT
+ commit refs/heads/O4
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ zof
+ COMMIT
+ progress Two commits down, 2 to go!
+ commit refs/heads/O4
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ zempty
+ COMMIT
+ progress Three commits down, 1 to go!
+ commit refs/heads/O4
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ zcommits
+ COMMIT
+ progress done!
+ INPUT_END
+ git fast-import <input >actual &&
+ grep "progress " <input >expect &&
+ test_cmp expect actual
+'
+
+###
+### series P (gitlinks)
+###
+
+test_expect_success 'P: superproject & submodule mix' '
+ cat >input <<-INPUT_END &&
+ blob
+ mark :1
+ data 10
+ test file
+
+ reset refs/heads/sub
+ commit refs/heads/sub
+ mark :2
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data 12
+ sub_initial
+ M 100644 :1 file
+
+ blob
+ mark :3
+ data <<DATAEND
+ [submodule "sub"]
+ path = sub
+ url = "$(pwd)/sub"
+ DATAEND
+
+ commit refs/heads/subuse1
+ mark :4
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data 8
+ initial
+ from refs/heads/main
+ M 100644 :3 .gitmodules
+ M 160000 :2 sub
+
+ blob
+ mark :5
+ data 20
+ test file
+ more data
+
+ commit refs/heads/sub
+ mark :6
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data 11
+ sub_second
+ from :2
+ M 100644 :5 file
+
+ commit refs/heads/subuse1
+ mark :7
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data 7
+ second
+ from :4
+ M 160000 :6 sub
+
+ INPUT_END
+
+ git fast-import <input &&
+ git checkout subuse1 &&
+ rm -rf sub &&
+ mkdir sub &&
+ (
+ cd sub &&
+ git init &&
+ git fetch --update-head-ok .. refs/heads/sub:refs/heads/main &&
+ git checkout main
+ ) &&
+ git submodule init &&
+ git submodule update
+'
+
+test_expect_success 'P: verbatim SHA gitlinks' '
+ SUBLAST=$(git rev-parse --verify sub) &&
+ SUBPREV=$(git rev-parse --verify sub^) &&
+
+ cat >input <<-INPUT_END &&
+ blob
+ mark :1
+ data <<DATAEND
+ [submodule "sub"]
+ path = sub
+ url = "$(pwd)/sub"
+ DATAEND
+
+ commit refs/heads/subuse2
+ mark :2
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data 8
+ initial
+ from refs/heads/main
+ M 100644 :1 .gitmodules
+ M 160000 $SUBPREV sub
+
+ commit refs/heads/subuse2
+ mark :3
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data 7
+ second
+ from :2
+ M 160000 $SUBLAST sub
+
+ INPUT_END
+
+ git branch -D sub &&
+ git gc --prune=now &&
+ git fast-import <input &&
+ test $(git rev-parse --verify subuse2) = $(git rev-parse --verify subuse1)
+'
+
+test_expect_success 'P: fail on inline gitlink' '
+ test_tick &&
+ cat >input <<-INPUT_END &&
+ commit refs/heads/subuse3
+ mark :1
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ corrupt
+ COMMIT
+
+ from refs/heads/subuse2
+ M 160000 inline sub
+ data <<DATA
+ $SUBPREV
+ DATA
+
+ INPUT_END
+
+ test_must_fail git fast-import <input
+'
+
+test_expect_success 'P: fail on blob mark in gitlink' '
+ test_tick &&
+ cat >input <<-INPUT_END &&
+ blob
+ mark :1
+ data <<DATA
+ $SUBPREV
+ DATA
+
+ commit refs/heads/subuse3
+ mark :2
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ corrupt
+ COMMIT
+
+ from refs/heads/subuse2
+ M 160000 :1 sub
+
+ INPUT_END
+
+ test_must_fail git fast-import <input
+'
+
+###
+### series Q (notes)
+###
+
+test_expect_success 'Q: commit notes' '
+ note1_data="The first note for the first commit" &&
+ note2_data="The first note for the second commit" &&
+ note3_data="The first note for the third commit" &&
+ note1b_data="The second note for the first commit" &&
+ note1c_data="The third note for the first commit" &&
+ note2b_data="The second note for the second commit" &&
+
+ test_tick &&
+ cat >input <<-INPUT_END &&
+ blob
+ mark :2
+ data <<EOF
+ $file2_data
+ EOF
+
+ commit refs/heads/notes-test
+ mark :3
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ first (:3)
+ COMMIT
+
+ M 644 :2 file2
+
+ blob
+ mark :4
+ data $file4_len
+ $file4_data
+ commit refs/heads/notes-test
+ mark :5
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ second (:5)
+ COMMIT
+
+ M 644 :4 file4
+
+ commit refs/heads/notes-test
+ mark :6
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ third (:6)
+ COMMIT
+
+ M 644 inline file5
+ data <<EOF
+ $file5_data
+ EOF
+
+ M 755 inline file6
+ data <<EOF
+ $file6_data
+ EOF
+
+ blob
+ mark :7
+ data <<EOF
+ $note1_data
+ EOF
+
+ blob
+ mark :8
+ data <<EOF
+ $note2_data
+ EOF
+
+ commit refs/notes/foobar
+ mark :9
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ notes (:9)
+ COMMIT
+
+ N :7 :3
+ N :8 :5
+ N inline :6
+ data <<EOF
+ $note3_data
+ EOF
+
+ commit refs/notes/foobar
+ mark :10
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ notes (:10)
+ COMMIT
+
+ N inline :3
+ data <<EOF
+ $note1b_data
+ EOF
+
+ commit refs/notes/foobar2
+ mark :11
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ notes (:11)
+ COMMIT
+
+ N inline :3
+ data <<EOF
+ $note1c_data
+ EOF
+
+ commit refs/notes/foobar
+ mark :12
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ notes (:12)
+ COMMIT
+
+ deleteall
+ N inline :5
+ data <<EOF
+ $note2b_data
+ EOF
+
+ INPUT_END
+
+ git fast-import <input &&
+ git whatchanged notes-test
+'
+
+test_expect_success 'Q: verify pack' '
+ verify_packs
+'
+
+test_expect_success 'Q: verify first commit' '
+ commit1=$(git rev-parse notes-test~2) &&
+ commit2=$(git rev-parse notes-test^) &&
+ commit3=$(git rev-parse notes-test) &&
+
+ cat >expect <<-EOF &&
+ author $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+
+ first (:3)
+ EOF
+ git cat-file commit notes-test~2 | sed 1d >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Q: verify second commit' '
+ cat >expect <<-EOF &&
+ parent $commit1
+ author $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+
+ second (:5)
+ EOF
+ git cat-file commit notes-test^ | sed 1d >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Q: verify third commit' '
+ cat >expect <<-EOF &&
+ parent $commit2
+ author $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+
+ third (:6)
+ EOF
+ git cat-file commit notes-test | sed 1d >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Q: verify first notes commit' '
+ cat >expect <<-EOF &&
+ author $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+
+ notes (:9)
+ EOF
+ git cat-file commit refs/notes/foobar~2 | sed 1d >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Q: verify first notes tree' '
+ cat >expect.unsorted <<-EOF &&
+ 100644 blob $commit1
+ 100644 blob $commit2
+ 100644 blob $commit3
+ EOF
+ cat expect.unsorted | sort >expect &&
+ git cat-file -p refs/notes/foobar~2^{tree} | sed "s/ [0-9a-f]* / /" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Q: verify first note for first commit' '
+ echo "$note1_data" >expect &&
+ git cat-file blob refs/notes/foobar~2:$commit1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Q: verify first note for second commit' '
+ echo "$note2_data" >expect &&
+ git cat-file blob refs/notes/foobar~2:$commit2 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Q: verify first note for third commit' '
+ echo "$note3_data" >expect &&
+ git cat-file blob refs/notes/foobar~2:$commit3 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Q: verify second notes commit' '
+ cat >expect <<-EOF &&
+ parent $(git rev-parse --verify refs/notes/foobar~2)
+ author $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+
+ notes (:10)
+ EOF
+ git cat-file commit refs/notes/foobar^ | sed 1d >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Q: verify second notes tree' '
+ cat >expect.unsorted <<-EOF &&
+ 100644 blob $commit1
+ 100644 blob $commit2
+ 100644 blob $commit3
+ EOF
+ cat expect.unsorted | sort >expect &&
+ git cat-file -p refs/notes/foobar^^{tree} | sed "s/ [0-9a-f]* / /" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Q: verify second note for first commit' '
+ echo "$note1b_data" >expect &&
+ git cat-file blob refs/notes/foobar^:$commit1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Q: verify first note for second commit' '
+ echo "$note2_data" >expect &&
+ git cat-file blob refs/notes/foobar^:$commit2 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Q: verify first note for third commit' '
+ echo "$note3_data" >expect &&
+ git cat-file blob refs/notes/foobar^:$commit3 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Q: verify third notes commit' '
+ cat >expect <<-EOF &&
+ author $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+
+ notes (:11)
+ EOF
+ git cat-file commit refs/notes/foobar2 | sed 1d >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Q: verify third notes tree' '
+ cat >expect.unsorted <<-EOF &&
+ 100644 blob $commit1
+ EOF
+ cat expect.unsorted | sort >expect &&
+ git cat-file -p refs/notes/foobar2^{tree} | sed "s/ [0-9a-f]* / /" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Q: verify third note for first commit' '
+ echo "$note1c_data" >expect &&
+ git cat-file blob refs/notes/foobar2:$commit1 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Q: verify fourth notes commit' '
+ cat >expect <<-EOF &&
+ parent $(git rev-parse --verify refs/notes/foobar^)
+ author $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+
+ notes (:12)
+ EOF
+ git cat-file commit refs/notes/foobar | sed 1d >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Q: verify fourth notes tree' '
+ cat >expect.unsorted <<-EOF &&
+ 100644 blob $commit2
+ EOF
+ cat expect.unsorted | sort >expect &&
+ git cat-file -p refs/notes/foobar^{tree} | sed "s/ [0-9a-f]* / /" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Q: verify second note for second commit' '
+ echo "$note2b_data" >expect &&
+ git cat-file blob refs/notes/foobar:$commit2 >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'Q: deny note on empty branch' '
+ cat >input <<-EOF &&
+ reset refs/heads/Q0
+
+ commit refs/heads/note-Q0
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ Note for an empty branch.
+ COMMIT
+
+ N inline refs/heads/Q0
+ data <<NOTE
+ some note
+ NOTE
+ EOF
+ test_must_fail git fast-import <input
+'
+###
+### series R (feature and option)
+###
+
+test_expect_success 'R: abort on unsupported feature' '
+ cat >input <<-EOF &&
+ feature no-such-feature-exists
+ EOF
+
+ test_must_fail git fast-import <input
+'
+
+test_expect_success 'R: supported feature is accepted' '
+ cat >input <<-EOF &&
+ feature date-format=now
+ EOF
+
+ git fast-import <input
+'
+
+test_expect_success 'R: abort on receiving feature after data command' '
+ cat >input <<-EOF &&
+ blob
+ data 3
+ hi
+ feature date-format=now
+ EOF
+
+ test_must_fail git fast-import <input
+'
+
+test_expect_success 'R: import-marks features forbidden by default' '
+ >git.marks &&
+ echo "feature import-marks=git.marks" >input &&
+ test_must_fail git fast-import <input &&
+ echo "feature import-marks-if-exists=git.marks" >input &&
+ test_must_fail git fast-import <input
+'
+
+test_expect_success 'R: only one import-marks feature allowed per stream' '
+ >git.marks &&
+ >git2.marks &&
+ cat >input <<-EOF &&
+ feature import-marks=git.marks
+ feature import-marks=git2.marks
+ EOF
+
+ test_must_fail git fast-import --allow-unsafe-features <input
+'
+
+test_expect_success 'R: export-marks feature forbidden by default' '
+ echo "feature export-marks=git.marks" >input &&
+ test_must_fail git fast-import <input
+'
+
+test_expect_success 'R: export-marks feature results in a marks file being created' '
+ cat >input <<-EOF &&
+ feature export-marks=git.marks
+ blob
+ mark :1
+ data 3
+ hi
+
+ EOF
+
+ git fast-import --allow-unsafe-features <input &&
+ grep :1 git.marks
+'
+
+test_expect_success 'R: export-marks options can be overridden by commandline options' '
+ cat >input <<-\EOF &&
+ feature export-marks=feature-sub/git.marks
+ blob
+ mark :1
+ data 3
+ hi
+
+ EOF
+ git fast-import --allow-unsafe-features \
+ --export-marks=cmdline-sub/other.marks <input &&
+ grep :1 cmdline-sub/other.marks &&
+ test_path_is_missing feature-sub
+'
+
+test_expect_success 'R: catch typo in marks file name' '
+ test_must_fail git fast-import --import-marks=nonexistent.marks </dev/null &&
+ echo "feature import-marks=nonexistent.marks" |
+ test_must_fail git fast-import --allow-unsafe-features
+'
+
+test_expect_success 'R: import and output marks can be the same file' '
+ rm -f io.marks &&
+ blob=$(echo hi | git hash-object --stdin) &&
+ cat >expect <<-EOF &&
+ :1 $blob
+ :2 $blob
+ EOF
+ git fast-import --export-marks=io.marks <<-\EOF &&
+ blob
+ mark :1
+ data 3
+ hi
+
+ EOF
+ git fast-import --import-marks=io.marks --export-marks=io.marks <<-\EOF &&
+ blob
+ mark :2
+ data 3
+ hi
+
+ EOF
+ test_cmp expect io.marks
+'
+
+test_expect_success 'R: --import-marks=foo --output-marks=foo to create foo fails' '
+ rm -f io.marks &&
+ test_must_fail git fast-import --import-marks=io.marks --export-marks=io.marks <<-\EOF
+ blob
+ mark :1
+ data 3
+ hi
+
+ EOF
+'
+
+test_expect_success 'R: --import-marks-if-exists' '
+ rm -f io.marks &&
+ blob=$(echo hi | git hash-object --stdin) &&
+ echo ":1 $blob" >expect &&
+ git fast-import --import-marks-if-exists=io.marks --export-marks=io.marks <<-\EOF &&
+ blob
+ mark :1
+ data 3
+ hi
+
+ EOF
+ test_cmp expect io.marks
+'
+
+test_expect_success 'R: feature import-marks-if-exists' '
+ rm -f io.marks &&
+
+ git fast-import --export-marks=io.marks \
+ --allow-unsafe-features <<-\EOF &&
+ feature import-marks-if-exists=not_io.marks
+ EOF
+ test_must_be_empty io.marks &&
+
+ blob=$(echo hi | git hash-object --stdin) &&
+
+ echo ":1 $blob" >io.marks &&
+ echo ":1 $blob" >expect &&
+ echo ":2 $blob" >>expect &&
+
+ git fast-import --export-marks=io.marks \
+ --allow-unsafe-features <<-\EOF &&
+ feature import-marks-if-exists=io.marks
+ blob
+ mark :2
+ data 3
+ hi
+
+ EOF
+ test_cmp expect io.marks &&
+
+ echo ":3 $blob" >>expect &&
+
+ git fast-import --import-marks=io.marks \
+ --export-marks=io.marks \
+ --allow-unsafe-features <<-\EOF &&
+ feature import-marks-if-exists=not_io.marks
+ blob
+ mark :3
+ data 3
+ hi
+
+ EOF
+ test_cmp expect io.marks &&
+
+ git fast-import --import-marks-if-exists=not_io.marks \
+ --export-marks=io.marks \
+ --allow-unsafe-features <<-\EOF &&
+ feature import-marks-if-exists=io.marks
+ EOF
+ test_must_be_empty io.marks
+'
+
+test_expect_success 'R: import to output marks works without any content' '
+ cat >input <<-EOF &&
+ feature import-marks=marks.out
+ feature export-marks=marks.new
+ EOF
+
+ git fast-import --allow-unsafe-features <input &&
+ test_cmp marks.out marks.new
+'
+
+test_expect_success 'R: import marks prefers commandline marks file over the stream' '
+ cat >input <<-EOF &&
+ feature import-marks=nonexistent.marks
+ feature export-marks=marks.new
+ EOF
+
+ git fast-import --import-marks=marks.out --allow-unsafe-features <input &&
+ test_cmp marks.out marks.new
+'
+
+
+test_expect_success 'R: multiple --import-marks= should be honoured' '
+ cat >input <<-EOF &&
+ feature import-marks=nonexistent.marks
+ feature export-marks=combined.marks
+ EOF
+
+ head -n2 marks.out > one.marks &&
+ tail -n +3 marks.out > two.marks &&
+ git fast-import --import-marks=one.marks --import-marks=two.marks \
+ --allow-unsafe-features <input &&
+ test_cmp marks.out combined.marks
+'
+
+test_expect_success 'R: feature relative-marks should be honoured' '
+ cat >input <<-EOF &&
+ feature relative-marks
+ feature import-marks=relative.in
+ feature export-marks=relative.out
+ EOF
+
+ mkdir -p .git/info/fast-import/ &&
+ cp marks.new .git/info/fast-import/relative.in &&
+ git fast-import --allow-unsafe-features <input &&
+ test_cmp marks.new .git/info/fast-import/relative.out
+'
+
+test_expect_success 'R: feature no-relative-marks should be honoured' '
+ cat >input <<-EOF &&
+ feature relative-marks
+ feature import-marks=relative.in
+ feature no-relative-marks
+ feature export-marks=non-relative.out
+ EOF
+
+ git fast-import --allow-unsafe-features <input &&
+ test_cmp marks.new non-relative.out
+'
+
+test_expect_success 'R: feature ls supported' '
+ echo "feature ls" |
+ git fast-import
+'
+
+test_expect_success 'R: feature cat-blob supported' '
+ echo "feature cat-blob" |
+ git fast-import
+'
+
+test_expect_success 'R: cat-blob-fd must be a nonnegative integer' '
+ test_must_fail git fast-import --cat-blob-fd=-1 </dev/null
+'
+
+test_expect_success !MINGW 'R: print old blob' '
+ blob=$(echo "yes it can" | git hash-object -w --stdin) &&
+ cat >expect <<-EOF &&
+ ${blob} blob 11
+ yes it can
+
+ EOF
+ echo "cat-blob $blob" |
+ git fast-import --cat-blob-fd=6 6>actual &&
+ test_cmp expect actual
+'
+
+test_expect_success !MINGW 'R: in-stream cat-blob-fd not respected' '
+ echo hello >greeting &&
+ blob=$(git hash-object -w greeting) &&
+ cat >expect <<-EOF &&
+ ${blob} blob 6
+ hello
+
+ EOF
+ git fast-import --cat-blob-fd=3 3>actual.3 >actual.1 <<-EOF &&
+ cat-blob $blob
+ EOF
+ test_cmp expect actual.3 &&
+ test_must_be_empty actual.1 &&
+ git fast-import 3>actual.3 >actual.1 <<-EOF &&
+ option cat-blob-fd=3
+ cat-blob $blob
+ EOF
+ test_must_be_empty actual.3 &&
+ test_cmp expect actual.1
+'
+
+test_expect_success !MINGW 'R: print mark for new blob' '
+ echo "effluentish" | git hash-object --stdin >expect &&
+ git fast-import --cat-blob-fd=6 6>actual <<-\EOF &&
+ blob
+ mark :1
+ data <<BLOB_END
+ effluentish
+ BLOB_END
+ get-mark :1
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success !MINGW 'R: print new blob' '
+ blob=$(echo "yep yep yep" | git hash-object --stdin) &&
+ cat >expect <<-EOF &&
+ ${blob} blob 12
+ yep yep yep
+
+ EOF
+ git fast-import --cat-blob-fd=6 6>actual <<-\EOF &&
+ blob
+ mark :1
+ data <<BLOB_END
+ yep yep yep
+ BLOB_END
+ cat-blob :1
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success !MINGW 'R: print new blob by sha1' '
+ blob=$(echo "a new blob named by sha1" | git hash-object --stdin) &&
+ cat >expect <<-EOF &&
+ ${blob} blob 25
+ a new blob named by sha1
+
+ EOF
+ git fast-import --cat-blob-fd=6 6>actual <<-EOF &&
+ blob
+ data <<BLOB_END
+ a new blob named by sha1
+ BLOB_END
+ cat-blob $blob
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'setup: big file' '
+ (
+ echo "the quick brown fox jumps over the lazy dog" >big &&
+ for i in 1 2 3
+ do
+ cat big big big big >bigger &&
+ cat bigger bigger bigger bigger >big ||
+ exit
+ done
+ )
+'
+
+test_expect_success 'R: print two blobs to stdout' '
+ blob1=$(git hash-object big) &&
+ blob1_len=$(wc -c <big) &&
+ blob2=$(echo hello | git hash-object --stdin) &&
+ {
+ echo ${blob1} blob $blob1_len &&
+ cat big &&
+ cat <<-EOF
+
+ ${blob2} blob 6
+ hello
+
+ EOF
+ } >expect &&
+ {
+ cat <<-\END_PART1 &&
+ blob
+ mark :1
+ data <<data_end
+ END_PART1
+ cat big &&
+ cat <<-\EOF
+ data_end
+ blob
+ mark :2
+ data <<data_end
+ hello
+ data_end
+ cat-blob :1
+ cat-blob :2
+ EOF
+ } |
+ git fast-import >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success PIPE 'R: copy using cat-file' '
+ expect_id=$(git hash-object big) &&
+ expect_len=$(wc -c <big) &&
+ echo $expect_id blob $expect_len >expect.response &&
+
+ rm -f blobs &&
+
+ mkfifo blobs &&
+ (
+ export GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL GIT_COMMITTER_DATE &&
+ cat <<-\EOF &&
+ feature cat-blob
+ blob
+ mark :1
+ data <<BLOB
+ EOF
+ cat big &&
+ cat <<-\EOF &&
+ BLOB
+ cat-blob :1
+ EOF
+
+ read blob_id type size <&3 &&
+ echo "$blob_id $type $size" >response &&
+ test_copy_bytes $size >blob <&3 &&
+ read newline <&3 &&
+
+ cat <<-EOF &&
+ commit refs/heads/copied
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ copy big file as file3
+ COMMIT
+ M 644 inline file3
+ data <<BLOB
+ EOF
+ cat blob &&
+ echo BLOB
+ ) 3<blobs |
+ git fast-import --cat-blob-fd=3 3>blobs &&
+ git show copied:file3 >actual &&
+ test_cmp expect.response response &&
+ test_cmp big actual
+'
+
+test_expect_success PIPE 'R: print blob mid-commit' '
+ rm -f blobs &&
+ echo "A blob from _before_ the commit." >expect &&
+ mkfifo blobs &&
+ (
+ exec 3<blobs &&
+ cat <<-EOF &&
+ feature cat-blob
+ blob
+ mark :1
+ data <<BLOB
+ A blob from _before_ the commit.
+ BLOB
+ commit refs/heads/temporary
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ Empty commit
+ COMMIT
+ cat-blob :1
+ EOF
+
+ read blob_id type size <&3 &&
+ test_copy_bytes $size >actual <&3 &&
+ read newline <&3 &&
+
+ echo
+ ) |
+ git fast-import --cat-blob-fd=3 3>blobs &&
+ test_cmp expect actual
+'
+
+test_expect_success PIPE 'R: print staged blob within commit' '
+ rm -f blobs &&
+ echo "A blob from _within_ the commit." >expect &&
+ mkfifo blobs &&
+ (
+ exec 3<blobs &&
+ cat <<-EOF &&
+ feature cat-blob
+ commit refs/heads/within
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ Empty commit
+ COMMIT
+ M 644 inline within
+ data <<BLOB
+ A blob from _within_ the commit.
+ BLOB
+ EOF
+
+ to_get=$(
+ echo "A blob from _within_ the commit." |
+ git hash-object --stdin
+ ) &&
+ echo "cat-blob $to_get" &&
+
+ read blob_id type size <&3 &&
+ test_copy_bytes $size >actual <&3 &&
+ read newline <&3 &&
+
+ echo deleteall
+ ) |
+ git fast-import --cat-blob-fd=3 3>blobs &&
+ test_cmp expect actual
+'
+
+test_expect_success 'R: quiet option results in no stats being output' '
+ cat >input <<-EOF &&
+ option git quiet
+ blob
+ data 3
+ hi
+
+ EOF
+
+ git fast-import 2>output <input &&
+ test_must_be_empty output
+'
+
+test_expect_success 'R: feature done means terminating "done" is mandatory' '
+ echo feature done | test_must_fail git fast-import &&
+ test_must_fail git fast-import --done </dev/null
+'
+
+test_expect_success 'R: terminating "done" with trailing gibberish is ok' '
+ git fast-import <<-\EOF &&
+ feature done
+ done
+ trailing gibberish
+ EOF
+ git fast-import <<-\EOF
+ done
+ more trailing gibberish
+ EOF
+'
+
+test_expect_success 'R: terminating "done" within commit' '
+ cat >expect <<-\EOF &&
+ OBJID
+ :000000 100644 OBJID OBJID A hello.c
+ :000000 100644 OBJID OBJID A hello2.c
+ EOF
+ git fast-import <<-EOF &&
+ commit refs/heads/done-ends
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<EOT
+ Commit terminated by "done" command
+ EOT
+ M 100644 inline hello.c
+ data <<EOT
+ Hello, world.
+ EOT
+ C hello.c hello2.c
+ done
+ EOF
+ git rev-list done-ends |
+ git diff-tree -r --stdin --root --always |
+ sed -e "s/$OID_REGEX/OBJID/g" >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'R: die on unknown option' '
+ cat >input <<-EOF &&
+ option git non-existing-option
+ EOF
+
+ test_must_fail git fast-import <input
+'
+
+test_expect_success 'R: unknown commandline options are rejected' '\
+ test_must_fail git fast-import --non-existing-option < /dev/null
+'
+
+test_expect_success 'R: die on invalid option argument' '
+ echo "option git active-branches=-5" |
+ test_must_fail git fast-import &&
+ echo "option git depth=" |
+ test_must_fail git fast-import &&
+ test_must_fail git fast-import --depth="5 elephants" </dev/null
+'
+
+test_expect_success 'R: ignore non-git options' '
+ cat >input <<-EOF &&
+ option non-existing-vcs non-existing-option
+ EOF
+
+ git fast-import <input
+'
+
+test_expect_success 'R: corrupt lines do not mess marks file' '
+ rm -f io.marks &&
+ blob=$(echo hi | git hash-object --stdin) &&
+ cat >expect <<-EOF &&
+ :3 $ZERO_OID
+ :1 $blob
+ :2 $blob
+ EOF
+ cp expect io.marks &&
+ test_must_fail git fast-import --import-marks=io.marks --export-marks=io.marks <<-\EOF &&
+
+ EOF
+ test_cmp expect io.marks
+'
+
+##
+## R: very large blobs
+##
+test_expect_success 'R: blob bigger than threshold' '
+ blobsize=$((2*1024*1024 + 53)) &&
+ test-tool genrandom bar $blobsize >expect &&
+ cat >input <<-INPUT_END &&
+ commit refs/heads/big-file
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ R - big file
+ COMMIT
+
+ M 644 inline big1
+ data $blobsize
+ INPUT_END
+ cat expect >>input &&
+ cat >>input <<-INPUT_END &&
+ M 644 inline big2
+ data $blobsize
+ INPUT_END
+ cat expect >>input &&
+ echo >>input &&
+
+ test_create_repo R &&
+ git --git-dir=R/.git config fastimport.unpackLimit 0 &&
+ git --git-dir=R/.git fast-import --big-file-threshold=1 <input
+'
+
+test_expect_success 'R: verify created pack' '
+ (
+ cd R &&
+ verify_packs -v > ../verify
+ )
+'
+
+test_expect_success 'R: verify written objects' '
+ git --git-dir=R/.git cat-file blob big-file:big1 >actual &&
+ test_cmp_bin expect actual &&
+ a=$(git --git-dir=R/.git rev-parse big-file:big1) &&
+ b=$(git --git-dir=R/.git rev-parse big-file:big2) &&
+ test $a = $b
+'
+
+test_expect_success 'R: blob appears only once' '
+ n=$(grep $a verify | wc -l) &&
+ test 1 = $n
+'
+
+###
+### series S
+###
+#
+# Make sure missing spaces and EOLs after mark references
+# cause errors.
+#
+# Setup:
+#
+# 1--2--4
+# \ /
+# -3-
+#
+# commit marks: 301, 302, 303, 304
+# blob marks: 403, 404, resp.
+# note mark: 202
+#
+# The error message when a space is missing not at the
+# end of the line is:
+#
+# Missing space after ..
+#
+# or when extra characters come after the mark at the end
+# of the line:
+#
+# Garbage after ..
+#
+# or when the dataref is neither "inline " or a known SHA1,
+#
+# Invalid dataref ..
+#
+test_expect_success 'S: initialize for S tests' '
+ test_tick &&
+
+ cat >input <<-INPUT_END &&
+ commit refs/heads/S
+ mark :301
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ commit 1
+ COMMIT
+ M 100644 inline hello.c
+ data <<BLOB
+ blob 1
+ BLOB
+
+ commit refs/heads/S
+ mark :302
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ commit 2
+ COMMIT
+ from :301
+ M 100644 inline hello.c
+ data <<BLOB
+ blob 2
+ BLOB
+
+ blob
+ mark :403
+ data <<BLOB
+ blob 3
+ BLOB
+
+ blob
+ mark :202
+ data <<BLOB
+ note 2
+ BLOB
+ INPUT_END
+
+ git fast-import --export-marks=marks <input
+'
+
+#
+# filemodify, three datarefs
+#
+test_expect_success 'S: filemodify with garbage after mark must fail' '
+ test_must_fail git fast-import --import-marks=marks <<-EOF 2>err &&
+ commit refs/heads/S
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ commit N
+ COMMIT
+ M 100644 :403x hello.c
+ EOF
+ test_grep "space after mark" err
+'
+
+# inline is misspelled; fast-import thinks it is some unknown dataref
+test_expect_success 'S: filemodify with garbage after inline must fail' '
+ test_must_fail git fast-import --import-marks=marks <<-EOF 2>err &&
+ commit refs/heads/S
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ commit N
+ COMMIT
+ M 100644 inlineX hello.c
+ data <<BLOB
+ inline
+ BLOB
+ EOF
+ test_grep "nvalid dataref" err
+'
+
+test_expect_success 'S: filemodify with garbage after sha1 must fail' '
+ sha1=$(grep :403 marks | cut -d\ -f2) &&
+ test_must_fail git fast-import --import-marks=marks <<-EOF 2>err &&
+ commit refs/heads/S
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ commit N
+ COMMIT
+ M 100644 ${sha1}x hello.c
+ EOF
+ test_grep "space after SHA1" err
+'
+
+#
+# notemodify, three ways to say dataref
+#
+test_expect_success 'S: notemodify with garbage after mark dataref must fail' '
+ test_must_fail git fast-import --import-marks=marks <<-EOF 2>err &&
+ commit refs/heads/S
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ commit S note dataref markref
+ COMMIT
+ N :202x :302
+ EOF
+ test_grep "space after mark" err
+'
+
+test_expect_success 'S: notemodify with garbage after inline dataref must fail' '
+ test_must_fail git fast-import --import-marks=marks <<-EOF 2>err &&
+ commit refs/heads/S
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ commit S note dataref inline
+ COMMIT
+ N inlineX :302
+ data <<BLOB
+ note blob
+ BLOB
+ EOF
+ test_grep "nvalid dataref" err
+'
+
+test_expect_success 'S: notemodify with garbage after sha1 dataref must fail' '
+ sha1=$(grep :202 marks | cut -d\ -f2) &&
+ test_must_fail git fast-import --import-marks=marks <<-EOF 2>err &&
+ commit refs/heads/S
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ commit S note dataref sha1
+ COMMIT
+ N ${sha1}x :302
+ EOF
+ test_grep "space after SHA1" err
+'
+
+#
+# notemodify, mark in commit-ish
+#
+test_expect_success 'S: notemodify with garbage after mark commit-ish must fail' '
+ test_must_fail git fast-import --import-marks=marks <<-EOF 2>err &&
+ commit refs/heads/Snotes
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ commit S note commit-ish
+ COMMIT
+ N :202 :302x
+ EOF
+ test_grep "after mark" err
+'
+
+#
+# from
+#
+test_expect_success 'S: from with garbage after mark must fail' '
+ test_must_fail \
+ git fast-import --import-marks=marks --export-marks=marks <<-EOF 2>err &&
+ commit refs/heads/S2
+ mark :303
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ commit 3
+ COMMIT
+ from :301x
+ M 100644 :403 hello.c
+ EOF
+
+
+ # go create the commit, need it for merge test
+ git fast-import --import-marks=marks --export-marks=marks <<-EOF &&
+ commit refs/heads/S2
+ mark :303
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ commit 3
+ COMMIT
+ from :301
+ M 100644 :403 hello.c
+ EOF
+
+ # now evaluate the error
+ test_grep "after mark" err
+'
+
+
+#
+# merge
+#
+test_expect_success 'S: merge with garbage after mark must fail' '
+ test_must_fail git fast-import --import-marks=marks <<-EOF 2>err &&
+ commit refs/heads/S
+ mark :304
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ merge 4
+ COMMIT
+ from :302
+ merge :303x
+ M 100644 :403 hello.c
+ EOF
+ test_grep "after mark" err
+'
+
+#
+# tag, from markref
+#
+test_expect_success 'S: tag with garbage after mark must fail' '
+ test_must_fail git fast-import --import-marks=marks <<-EOF 2>err &&
+ tag refs/tags/Stag
+ from :302x
+ tagger $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<TAG
+ tag S
+ TAG
+ EOF
+ test_grep "after mark" err
+'
+
+#
+# cat-blob markref
+#
+test_expect_success 'S: cat-blob with garbage after mark must fail' '
+ test_must_fail git fast-import --import-marks=marks <<-EOF 2>err &&
+ cat-blob :403x
+ EOF
+ test_grep "after mark" err
+'
+
+#
+# ls markref
+#
+test_expect_success 'S: ls with garbage after mark must fail' '
+ test_must_fail git fast-import --import-marks=marks <<-EOF 2>err &&
+ ls :302x hello.c
+ EOF
+ test_grep "space after mark" err
+'
+
+test_expect_success 'S: ls with garbage after sha1 must fail' '
+ sha1=$(grep :302 marks | cut -d\ -f2) &&
+ test_must_fail git fast-import --import-marks=marks <<-EOF 2>err &&
+ ls ${sha1}x hello.c
+ EOF
+ test_grep "space after tree-ish" err
+'
+
+###
+### series T (ls)
+###
+# Setup is carried over from series S.
+
+test_expect_success 'T: ls root tree' '
+ sed -e "s/Z\$//" >expect <<-EOF &&
+ 040000 tree $(git rev-parse S^{tree}) Z
+ EOF
+ sha1=$(git rev-parse --verify S) &&
+ git fast-import --import-marks=marks <<-EOF >actual &&
+ ls $sha1 ""
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'T: delete branch' '
+ git branch to-delete &&
+ git fast-import <<-EOF &&
+ reset refs/heads/to-delete
+ from $ZERO_OID
+ EOF
+ test_must_fail git rev-parse --verify refs/heads/to-delete
+'
+
+test_expect_success 'T: empty reset doesnt delete branch' '
+ git branch not-to-delete &&
+ git fast-import <<-EOF &&
+ reset refs/heads/not-to-delete
+ EOF
+ git show-ref &&
+ git rev-parse --verify refs/heads/not-to-delete
+'
+
+###
+### series U (filedelete)
+###
+
+test_expect_success 'U: initialize for U tests' '
+ cat >input <<-INPUT_END &&
+ commit refs/heads/U
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ test setup
+ COMMIT
+ M 100644 inline hello.c
+ data <<BLOB
+ blob 1
+ BLOB
+ M 100644 inline good/night.txt
+ data <<BLOB
+ sleep well
+ BLOB
+ M 100644 inline good/bye.txt
+ data <<BLOB
+ au revoir
+ BLOB
+
+ INPUT_END
+
+ f7id=$(echo "blob 1" | git hash-object --stdin) &&
+ f8id=$(echo "sleep well" | git hash-object --stdin) &&
+ f9id=$(echo "au revoir" | git hash-object --stdin) &&
+ git fast-import <input
+'
+
+test_expect_success 'U: filedelete file succeeds' '
+ cat >input <<-INPUT_END &&
+ commit refs/heads/U
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ delete good/night.txt
+ COMMIT
+ from refs/heads/U^0
+ D good/night.txt
+
+ INPUT_END
+
+ git fast-import <input
+'
+
+test_expect_success 'U: validate file delete result' '
+ cat >expect <<-EOF &&
+ :100644 000000 $f8id $ZERO_OID D good/night.txt
+ EOF
+
+ git diff-tree -M -r U^1 U >actual &&
+
+ compare_diff_raw expect actual
+'
+
+test_expect_success 'U: filedelete directory succeeds' '
+ cat >input <<-INPUT_END &&
+ commit refs/heads/U
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ delete good dir
+ COMMIT
+ from refs/heads/U^0
+ D good
+
+ INPUT_END
+
+ git fast-import <input
+'
+
+test_expect_success 'U: validate directory delete result' '
+ cat >expect <<-EOF &&
+ :100644 000000 $f9id $ZERO_OID D good/bye.txt
+ EOF
+
+ git diff-tree -M -r U^1 U >actual &&
+
+ compare_diff_raw expect actual
+'
+
+test_expect_success 'U: filedelete root succeeds' '
+ cat >input <<-INPUT_END &&
+ commit refs/heads/U
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ must succeed
+ COMMIT
+ from refs/heads/U^0
+ D ""
+
+ INPUT_END
+
+ git fast-import <input
+'
+
+test_expect_success 'U: validate root delete result' '
+ cat >expect <<-EOF &&
+ :100644 000000 $f7id $ZERO_OID D hello.c
+ EOF
+
+ git diff-tree -M -r U^1 U >actual &&
+
+ compare_diff_raw expect actual
+'
+
+###
+### series V (checkpoint)
+###
+
+# The commands in input_file should not produce any output on the file
+# descriptor set with --cat-blob-fd (or stdout if unspecified).
+#
+# To make sure you're observing the side effects of checkpoint *before*
+# fast-import terminates (and thus writes out its state), check that the
+# fast-import process is still running using background_import_still_running
+# *after* evaluating the test conditions.
+background_import_then_checkpoint () {
+ options=$1
+ input_file=$2
+
+ mkfifo V.input
+ exec 8<>V.input
+ rm V.input
+
+ mkfifo V.output
+ exec 9<>V.output
+ rm V.output
+
+ (
+ git fast-import $options <&8 >&9 &
+ echo $! >&9
+ wait $!
+ echo >&2 "background fast-import terminated too early with exit code $?"
+ # Un-block the read loop in the main shell process.
+ echo >&9 UNEXPECTED
+ ) &
+ sh_pid=$!
+ read fi_pid <&9
+ # We don't mind if fast-import has already died by the time the test
+ # ends.
+ test_when_finished "
+ exec 8>&-; exec 9>&-;
+ kill $sh_pid && wait $sh_pid
+ kill $fi_pid && wait $fi_pid
+ true"
+
+ # Start in the background to ensure we adhere strictly to (blocking)
+ # pipes writing sequence. We want to assume that the write below could
+ # block, e.g. if fast-import blocks writing its own output to &9
+ # because there is no reader on &9 yet.
+ (
+ cat "$input_file"
+ echo "checkpoint"
+ echo "progress checkpoint"
+ ) >&8 &
+
+ error=1 ;# assume the worst
+ while read output <&9
+ do
+ if test "$output" = "progress checkpoint"
+ then
+ error=0
+ break
+ elif test "$output" = "UNEXPECTED"
+ then
+ break
+ fi
+ # otherwise ignore cruft
+ echo >&2 "cruft: $output"
+ done
+
+ if test $error -eq 1
+ then
+ false
+ fi
+}
+
+background_import_still_running () {
+ if ! kill -0 "$fi_pid"
+ then
+ echo >&2 "background fast-import terminated too early"
+ false
+ fi
+}
+
+test_expect_success PIPE 'V: checkpoint helper does not get stuck with extra output' '
+ cat >input <<-INPUT_END &&
+ progress foo
+ progress bar
+
+ INPUT_END
+
+ background_import_then_checkpoint "" input &&
+ background_import_still_running
+'
+
+test_expect_success PIPE 'V: checkpoint updates refs after reset' '
+ cat >input <<-\INPUT_END &&
+ reset refs/heads/V
+ from refs/heads/U
+
+ INPUT_END
+
+ background_import_then_checkpoint "" input &&
+ test "$(git rev-parse --verify V)" = "$(git rev-parse --verify U)" &&
+ background_import_still_running
+'
+
+test_expect_success PIPE 'V: checkpoint updates refs and marks after commit' '
+ cat >input <<-INPUT_END &&
+ commit refs/heads/V
+ mark :1
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data 0
+ from refs/heads/U
+
+ INPUT_END
+
+ background_import_then_checkpoint "--export-marks=marks.actual" input &&
+
+ echo ":1 $(git rev-parse --verify V)" >marks.expected &&
+
+ test "$(git rev-parse --verify V^)" = "$(git rev-parse --verify U)" &&
+ test_cmp marks.expected marks.actual &&
+ background_import_still_running
+'
+
+# Re-create the exact same commit, but on a different branch: no new object is
+# created in the database, but the refs and marks still need to be updated.
+test_expect_success PIPE 'V: checkpoint updates refs and marks after commit (no new objects)' '
+ cat >input <<-INPUT_END &&
+ commit refs/heads/V2
+ mark :2
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data 0
+ from refs/heads/U
+
+ INPUT_END
+
+ background_import_then_checkpoint "--export-marks=marks.actual" input &&
+
+ echo ":2 $(git rev-parse --verify V2)" >marks.expected &&
+
+ test "$(git rev-parse --verify V2)" = "$(git rev-parse --verify V)" &&
+ test_cmp marks.expected marks.actual &&
+ background_import_still_running
+'
+
+test_expect_success PIPE 'V: checkpoint updates tags after tag' '
+ cat >input <<-INPUT_END &&
+ tag Vtag
+ from refs/heads/V
+ tagger $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data 0
+
+ INPUT_END
+
+ background_import_then_checkpoint "" input &&
+ git show-ref -d Vtag &&
+ background_import_still_running
+'
+
+###
+### series W (get-mark and empty orphan commits)
+###
+
+cat >>W-input <<-W_INPUT_END
+ commit refs/heads/W-branch
+ mark :1
+ author Full Name <user@company.tld> 1000000000 +0100
+ committer Full Name <user@company.tld> 1000000000 +0100
+ data 27
+ Intentionally empty commit
+ LFsget-mark :1
+ W_INPUT_END
+
+test_expect_success !MINGW 'W: get-mark & empty orphan commit with no newlines' '
+ sed -e s/LFs// W-input | tr L "\n" | git fast-import
+'
+
+test_expect_success !MINGW 'W: get-mark & empty orphan commit with one newline' '
+ sed -e s/LFs/L/ W-input | tr L "\n" | git fast-import
+'
+
+test_expect_success !MINGW 'W: get-mark & empty orphan commit with ugly second newline' '
+ # Technically, this should fail as it has too many linefeeds
+ # according to the grammar in fast-import.txt. But, for whatever
+ # reason, it works. Since using the correct number of newlines
+ # does not work with older (pre-2.22) versions of git, allow apps
+ # that used this second-newline workaround to keep working by
+ # checking it with this test...
+ sed -e s/LFs/LL/ W-input | tr L "\n" | git fast-import
+'
+
+test_expect_success !MINGW 'W: get-mark & empty orphan commit with erroneous third newline' '
+ # ...but do NOT allow more empty lines than that (see previous test).
+ sed -e s/LFs/LLL/ W-input | tr L "\n" | test_must_fail git fast-import
+'
+
+###
+### series X (other new features)
+###
+
+test_expect_success 'X: handling encoding' '
+ test_tick &&
+ cat >input <<-INPUT_END &&
+ commit refs/heads/encoding
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ encoding iso-8859-7
+ data <<COMMIT
+ INPUT_END
+
+ printf "Pi: \360\nCOMMIT\n" >>input &&
+
+ git fast-import <input &&
+ git cat-file -p encoding | grep $(printf "\360") &&
+ git log -1 --format=%B encoding | grep $(printf "\317\200")
+'
+
+###
+### series Y (submodules and hash algorithms)
+###
+
+cat >Y-sub-input <<\Y_INPUT_END
+blob
+mark :1
+data 4
+foo
+
+reset refs/heads/main
+commit refs/heads/main
+mark :2
+author Full Name <user@company.tld> 1000000000 +0100
+committer Full Name <user@company.tld> 1000000000 +0100
+data 24
+Test submodule commit 1
+M 100644 :1 file
+
+blob
+mark :3
+data 8
+foo
+bar
+
+commit refs/heads/main
+mark :4
+author Full Name <user@company.tld> 1000000001 +0100
+committer Full Name <user@company.tld> 1000000001 +0100
+data 24
+Test submodule commit 2
+from :2
+M 100644 :3 file
+Y_INPUT_END
+
+# Note that the submodule object IDs are intentionally not translated.
+cat >Y-main-input <<\Y_INPUT_END
+blob
+mark :1
+data 4
+foo
+
+reset refs/heads/main
+commit refs/heads/main
+mark :2
+author Full Name <user@company.tld> 2000000000 +0100
+committer Full Name <user@company.tld> 2000000000 +0100
+data 14
+Test commit 1
+M 100644 :1 file
+
+blob
+mark :3
+data 73
+[submodule "sub1"]
+ path = sub1
+ url = https://void.example.com/main.git
+
+commit refs/heads/main
+mark :4
+author Full Name <user@company.tld> 2000000001 +0100
+committer Full Name <user@company.tld> 2000000001 +0100
+data 14
+Test commit 2
+from :2
+M 100644 :3 .gitmodules
+M 160000 0712c5be7cf681388e355ef47525aaf23aee1a6d sub1
+
+blob
+mark :5
+data 8
+foo
+bar
+
+commit refs/heads/main
+mark :6
+author Full Name <user@company.tld> 2000000002 +0100
+committer Full Name <user@company.tld> 2000000002 +0100
+data 14
+Test commit 3
+from :4
+M 100644 :5 file
+M 160000 ff729f5e62f72c0c3978207d9a80e5f3a65f14d7 sub1
+Y_INPUT_END
+
+cat >Y-marks <<\Y_INPUT_END
+:2 0712c5be7cf681388e355ef47525aaf23aee1a6d
+:4 ff729f5e62f72c0c3978207d9a80e5f3a65f14d7
+Y_INPUT_END
+
+test_expect_success 'Y: setup' '
+ test_oid_cache <<-EOF
+ Ymain sha1:9afed2f9161ddf416c0a1863b8b0725b00070504
+ Ymain sha256:c0a1010da1df187b2e287654793df01b464bd6f8e3f17fc1481a7dadf84caee3
+ EOF
+'
+
+test_expect_success 'Y: rewrite submodules' '
+ git init main1 &&
+ (
+ cd main1 &&
+ git init sub2 &&
+ git -C sub2 fast-import --export-marks=../sub2-marks <../Y-sub-input &&
+ git fast-import --rewrite-submodules-from=sub:../Y-marks \
+ --rewrite-submodules-to=sub:sub2-marks <../Y-main-input &&
+ test "$(git rev-parse main)" = "$(test_oid Ymain)"
+ )
+'
+
+test_done
diff --git a/t/t9301-fast-import-notes.sh b/t/t9301-fast-import-notes.sh
new file mode 100755
index 0000000..5841322
--- /dev/null
+++ b/t/t9301-fast-import-notes.sh
@@ -0,0 +1,728 @@
+#!/bin/sh
+#
+# Copyright (c) 2009 Johan Herland
+#
+
+test_description='test git fast-import of notes objects'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+
+test_tick
+cat >input <<INPUT_END
+commit refs/heads/main
+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+data <<COMMIT
+first commit
+COMMIT
+
+M 644 inline foo
+data <<EOF
+file foo in first commit
+EOF
+
+M 755 inline bar
+data <<EOF
+file bar in first commit
+EOF
+
+M 644 inline baz/xyzzy
+data <<EOF
+file baz/xyzzy in first commit
+EOF
+
+commit refs/heads/main
+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+data <<COMMIT
+second commit
+COMMIT
+
+M 644 inline foo
+data <<EOF
+file foo in second commit
+EOF
+
+M 755 inline baz/xyzzy
+data <<EOF
+file baz/xyzzy in second commit
+EOF
+
+commit refs/heads/main
+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+data <<COMMIT
+third commit
+COMMIT
+
+M 644 inline foo
+data <<EOF
+file foo in third commit
+EOF
+
+commit refs/heads/main
+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+data <<COMMIT
+fourth commit
+COMMIT
+
+M 755 inline bar
+data <<EOF
+file bar in fourth commit
+EOF
+
+INPUT_END
+
+test_expect_success 'set up main branch' '
+
+ git fast-import <input &&
+ git whatchanged main
+'
+
+commit4=$(git rev-parse refs/heads/main)
+commit3=$(git rev-parse "$commit4^")
+commit2=$(git rev-parse "$commit4~2")
+commit1=$(git rev-parse "$commit4~3")
+
+test_tick
+cat >input <<INPUT_END
+commit refs/notes/test
+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+data <<COMMIT
+first notes commit
+COMMIT
+
+M 644 inline $commit1
+data <<EOF
+first note for first commit
+EOF
+
+M 755 inline $commit2
+data <<EOF
+first note for second commit
+EOF
+
+INPUT_END
+
+cat >expect <<EXPECT_END
+ fourth commit
+ third commit
+ second commit
+ first note for second commit
+ first commit
+ first note for first commit
+EXPECT_END
+
+test_expect_success 'add notes with simple M command' '
+
+ git fast-import <input &&
+ GIT_NOTES_REF=refs/notes/test git log | grep "^ " > actual &&
+ test_cmp expect actual
+
+'
+
+test_tick
+cat >input <<INPUT_END
+feature notes
+commit refs/notes/test
+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+data <<COMMIT
+second notes commit
+COMMIT
+
+from refs/notes/test^0
+N inline $commit3
+data <<EOF
+first note for third commit
+EOF
+
+N inline $commit4
+data <<EOF
+first note for fourth commit
+EOF
+
+INPUT_END
+
+cat >expect <<EXPECT_END
+ fourth commit
+ first note for fourth commit
+ third commit
+ first note for third commit
+ second commit
+ first note for second commit
+ first commit
+ first note for first commit
+EXPECT_END
+
+test_expect_success 'add notes with simple N command' '
+
+ git fast-import <input &&
+ GIT_NOTES_REF=refs/notes/test git log | grep "^ " > actual &&
+ test_cmp expect actual
+
+'
+
+test_tick
+cat >input <<INPUT_END
+commit refs/notes/test
+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+data <<COMMIT
+third notes commit
+COMMIT
+
+from refs/notes/test^0
+N inline $commit1
+data <<EOF
+second note for first commit
+EOF
+
+N inline $commit2
+data <<EOF
+second note for second commit
+EOF
+
+N inline $commit3
+data <<EOF
+second note for third commit
+EOF
+
+N inline $commit4
+data <<EOF
+second note for fourth commit
+EOF
+
+INPUT_END
+
+cat >expect <<EXPECT_END
+ fourth commit
+ second note for fourth commit
+ third commit
+ second note for third commit
+ second commit
+ second note for second commit
+ first commit
+ second note for first commit
+EXPECT_END
+
+test_expect_success 'update existing notes with N command' '
+
+ git fast-import <input &&
+ GIT_NOTES_REF=refs/notes/test git log | grep "^ " > actual &&
+ test_cmp expect actual
+
+'
+
+test_tick
+cat >input <<INPUT_END
+commit refs/notes/test
+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+data <<COMMIT
+fourth notes commit
+COMMIT
+
+from refs/notes/test^0
+M 644 inline $(echo "$commit3" | sed "s|^..|&/|")
+data <<EOF
+prefix of note for third commit
+EOF
+
+M 644 inline $(echo "$commit4" | sed "s|^..|&/|")
+data <<EOF
+prefix of note for fourth commit
+EOF
+
+M 644 inline $(echo "$commit4" | sed "s|^\(..\)\(..\)|\1/\2/|")
+data <<EOF
+pre-prefix of note for fourth commit
+EOF
+
+N inline $commit1
+data <<EOF
+third note for first commit
+EOF
+
+N inline $commit2
+data <<EOF
+third note for second commit
+EOF
+
+N inline $commit3
+data <<EOF
+third note for third commit
+EOF
+
+N inline $commit4
+data <<EOF
+third note for fourth commit
+EOF
+
+
+INPUT_END
+
+whitespace=" "
+
+cat >expect <<EXPECT_END
+ fourth commit
+ pre-prefix of note for fourth commit
+$whitespace
+ prefix of note for fourth commit
+$whitespace
+ third note for fourth commit
+ third commit
+ prefix of note for third commit
+$whitespace
+ third note for third commit
+ second commit
+ third note for second commit
+ first commit
+ third note for first commit
+EXPECT_END
+
+test_expect_success 'add concatenation notes with M command' '
+
+ git fast-import <input &&
+ GIT_NOTES_REF=refs/notes/test git log | grep "^ " > actual &&
+ test_cmp expect actual
+
+'
+
+test_tick
+cat >input <<INPUT_END
+commit refs/notes/test
+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+data <<COMMIT
+fifth notes commit
+COMMIT
+
+from refs/notes/test^0
+deleteall
+
+INPUT_END
+
+cat >expect <<EXPECT_END
+ fourth commit
+ third commit
+ second commit
+ first commit
+EXPECT_END
+
+test_expect_success 'verify that deleteall also removes notes' '
+
+ git fast-import <input &&
+ GIT_NOTES_REF=refs/notes/test git log | grep "^ " > actual &&
+ test_cmp expect actual
+
+'
+
+test_tick
+cat >input <<INPUT_END
+commit refs/notes/test
+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+data <<COMMIT
+sixth notes commit
+COMMIT
+
+from refs/notes/test^0
+M 644 inline $commit1
+data <<EOF
+third note for first commit
+EOF
+
+M 644 inline $commit3
+data <<EOF
+third note for third commit
+EOF
+
+N inline $commit1
+data <<EOF
+fourth note for first commit
+EOF
+
+N inline $commit3
+data <<EOF
+fourth note for third commit
+EOF
+
+INPUT_END
+
+cat >expect <<EXPECT_END
+ fourth commit
+ third commit
+ fourth note for third commit
+ second commit
+ first commit
+ fourth note for first commit
+EXPECT_END
+
+test_expect_success 'verify that later N commands override earlier M commands' '
+
+ git fast-import <input &&
+ GIT_NOTES_REF=refs/notes/test git log | grep "^ " > actual &&
+ test_cmp expect actual
+
+'
+
+# Write fast-import commands to create the given number of commits
+fast_import_commits () {
+ my_ref=$1
+ my_num_commits=$2
+ my_append_to_file=$3
+ my_i=0
+ while test $my_i -lt $my_num_commits
+ do
+ my_i=$(($my_i + 1))
+ test_tick
+ cat >>"$my_append_to_file" <<INPUT_END
+commit $my_ref
+mark :$my_i
+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+data <<COMMIT
+commit #$my_i
+COMMIT
+
+M 644 inline file
+data <<EOF
+file contents in commit #$my_i
+EOF
+
+INPUT_END
+ done
+}
+
+# Write fast-import commands to create the given number of notes annotating
+# the commits created by fast_import_commits()
+fast_import_notes () {
+ my_notes_ref=$1
+ my_num_commits=$2
+ my_append_to_file=$3
+ my_note_append=$4
+ test_tick
+ cat >>"$my_append_to_file" <<INPUT_END
+commit $my_notes_ref
+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+data <<COMMIT
+committing $my_num_commits notes
+COMMIT
+
+INPUT_END
+
+ my_i=0
+ while test $my_i -lt $my_num_commits
+ do
+ my_i=$(($my_i + 1))
+ cat >>"$my_append_to_file" <<INPUT_END
+N inline :$my_i
+data <<EOF
+note for commit #$my_i$my_note_append
+EOF
+
+INPUT_END
+ done
+}
+
+
+rm input expect
+num_commits=400
+# Create lots of commits
+fast_import_commits "refs/heads/many_commits" $num_commits input
+# Create one note per above commit
+fast_import_notes "refs/notes/many_notes" $num_commits input
+# Add a couple of non-notes as well
+test_tick
+cat >>input <<INPUT_END
+commit refs/notes/many_notes
+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+data <<COMMIT
+committing some non-notes to the notes tree
+COMMIT
+
+M 755 inline foobar/non-note.txt
+data <<EOF
+This is not a note, but rather a regular file residing in a notes tree
+EOF
+
+M 644 inline deadbeef
+data <<EOF
+Non-note file
+EOF
+
+M 644 inline de/adbeef
+data <<EOF
+Another non-note file
+EOF
+
+INPUT_END
+# Finally create the expected output from all these notes and commits
+i=$num_commits
+while test $i -gt 0
+do
+ cat >>expect <<EXPECT_END
+ commit #$i
+ note for commit #$i
+EXPECT_END
+ i=$(($i - 1))
+done
+
+test_expect_success 'add lots of commits and notes' '
+
+ git fast-import <input &&
+ GIT_NOTES_REF=refs/notes/many_notes git log refs/heads/many_commits |
+ grep "^ " > actual &&
+ test_cmp expect actual
+
+'
+
+test_expect_success 'verify that lots of notes trigger a fanout scheme' '
+ hexsz=$(test_oid hexsz) &&
+
+ # None of the entries in the top-level notes tree should be a full SHA1
+ git ls-tree --name-only refs/notes/many_notes |
+ while read path
+ do
+ if test $(expr length "$path") -ge $hexsz
+ then
+ return 1
+ fi
+ done
+
+'
+
+# Create another notes tree from the one above
+SP=" "
+cat >>input <<INPUT_END
+commit refs/heads/other_commits
+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+data <<COMMIT
+commit #$(($num_commit + 1))
+COMMIT
+
+from refs/heads/many_commits
+M 644 inline file
+data <<EOF
+file contents in commit #$(($num_commit + 1))
+EOF
+
+commit refs/notes/other_notes
+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+data <<COMMIT
+committing one more note on a tree imported from a previous notes tree
+COMMIT
+
+M 040000 $(git log --no-walk --format=%T refs/notes/many_notes)$SP
+N inline :$(($num_commit + 1))
+data <<EOF
+note for commit #$(($num_commit + 1))
+EOF
+INPUT_END
+
+test_expect_success 'verify that importing a notes tree respects the fanout scheme' '
+ git fast-import <input &&
+
+ # None of the entries in the top-level notes tree should be a full SHA1
+ git ls-tree --name-only refs/notes/other_notes |
+ while read path
+ do
+ if test $(expr length "$path") -ge $hexsz
+ then
+ return 1
+ fi
+ done
+'
+
+cat >>expect_non-note1 << EOF
+This is not a note, but rather a regular file residing in a notes tree
+EOF
+
+cat >>expect_non-note2 << EOF
+Non-note file
+EOF
+
+cat >>expect_non-note3 << EOF
+Another non-note file
+EOF
+
+test_expect_success 'verify that non-notes are untouched by a fanout change' '
+
+ git cat-file -p refs/notes/many_notes:foobar/non-note.txt > actual &&
+ test_cmp expect_non-note1 actual &&
+ git cat-file -p refs/notes/many_notes:deadbeef > actual &&
+ test_cmp expect_non-note2 actual &&
+ git cat-file -p refs/notes/many_notes:de/adbeef > actual &&
+ test_cmp expect_non-note3 actual
+
+'
+
+# Change the notes for the three top commits
+test_tick
+cat >input <<INPUT_END
+commit refs/notes/many_notes
+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+data <<COMMIT
+changing notes for the top three commits
+COMMIT
+from refs/notes/many_notes^0
+INPUT_END
+
+rm expect
+i=$num_commits
+j=0
+while test $j -lt 3
+do
+ cat >>input <<INPUT_END
+N inline refs/heads/many_commits~$j
+data <<EOF
+changed note for commit #$i
+EOF
+INPUT_END
+ cat >>expect <<EXPECT_END
+ commit #$i
+ changed note for commit #$i
+EXPECT_END
+ i=$(($i - 1))
+ j=$(($j + 1))
+done
+
+test_expect_success 'change a few existing notes' '
+
+ git fast-import <input &&
+ GIT_NOTES_REF=refs/notes/many_notes git log -n3 refs/heads/many_commits |
+ grep "^ " > actual &&
+ test_cmp expect actual
+
+'
+
+test_expect_success 'verify that changing notes respect existing fanout' '
+
+ # None of the entries in the top-level notes tree should be a full SHA1
+ git ls-tree --name-only refs/notes/many_notes |
+ while read path
+ do
+ if test $(expr length "$path") -ge $hexsz
+ then
+ return 1
+ fi
+ done
+
+'
+
+remaining_notes=10
+test_tick
+cat >input <<INPUT_END
+commit refs/notes/many_notes
+committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+data <<COMMIT
+removing all notes but $remaining_notes
+COMMIT
+from refs/notes/many_notes^0
+INPUT_END
+
+i=$(($num_commits - $remaining_notes))
+for sha1 in $(git rev-list -n $i refs/heads/many_commits)
+do
+ cat >>input <<INPUT_END
+N $ZERO_OID $sha1
+INPUT_END
+done
+
+i=$num_commits
+rm expect
+while test $i -gt 0
+do
+ cat >>expect <<EXPECT_END
+ commit #$i
+EXPECT_END
+ if test $i -le $remaining_notes
+ then
+ cat >>expect <<EXPECT_END
+ note for commit #$i
+EXPECT_END
+ fi
+ i=$(($i - 1))
+done
+
+test_expect_success 'remove lots of notes' '
+
+ git fast-import <input &&
+ GIT_NOTES_REF=refs/notes/many_notes git log refs/heads/many_commits |
+ grep "^ " > actual &&
+ test_cmp expect actual
+
+'
+
+test_expect_success 'verify that removing notes trigger fanout consolidation' '
+ # All entries in the top-level notes tree should be a full SHA1
+ git ls-tree --name-only -r refs/notes/many_notes |
+ while read path
+ do
+ # Explicitly ignore the non-note paths
+ test "$path" = "foobar/non-note.txt" && continue
+ test "$path" = "deadbeef" && continue
+ test "$path" = "de/adbeef" && continue
+
+ if test $(expr length "$path") -ne $hexsz
+ then
+ return 1
+ fi
+ done
+
+'
+
+test_expect_success 'verify that non-notes are untouched by a fanout change' '
+
+ git cat-file -p refs/notes/many_notes:foobar/non-note.txt > actual &&
+ test_cmp expect_non-note1 actual &&
+ git cat-file -p refs/notes/many_notes:deadbeef > actual &&
+ test_cmp expect_non-note2 actual &&
+ git cat-file -p refs/notes/many_notes:de/adbeef > actual &&
+ test_cmp expect_non-note3 actual
+
+'
+
+
+rm input expect
+num_notes_refs=10
+num_commits=16
+some_commits=8
+# Create commits
+fast_import_commits "refs/heads/more_commits" $num_commits input
+# Create one note per above commit per notes ref
+i=0
+while test $i -lt $num_notes_refs
+do
+ i=$(($i + 1))
+ fast_import_notes "refs/notes/more_notes_$i" $num_commits input
+done
+# Trigger branch reloading in git-fast-import by repeating the note creation
+i=0
+while test $i -lt $num_notes_refs
+do
+ i=$(($i + 1))
+ fast_import_notes "refs/notes/more_notes_$i" $some_commits input " (2)"
+done
+# Finally create the expected output from the notes in refs/notes/more_notes_1
+i=$num_commits
+while test $i -gt 0
+do
+ note_data="note for commit #$i"
+ if test $i -le $some_commits
+ then
+ note_data="$note_data (2)"
+ fi
+ cat >>expect <<EXPECT_END
+ commit #$i
+ $note_data
+EXPECT_END
+ i=$(($i - 1))
+done
+
+test_expect_success "add notes to $num_commits commits in each of $num_notes_refs refs" '
+
+ git fast-import --active-branches=5 <input &&
+ GIT_NOTES_REF=refs/notes/more_notes_1 git log refs/heads/more_commits |
+ grep "^ " > actual &&
+ test_cmp expect actual
+
+'
+
+test_done
diff --git a/t/t9302-fast-import-unpack-limit.sh b/t/t9302-fast-import-unpack-limit.sh
new file mode 100755
index 0000000..d8b1f94
--- /dev/null
+++ b/t/t9302-fast-import-unpack-limit.sh
@@ -0,0 +1,107 @@
+#!/bin/sh
+test_description='test git fast-import unpack limit'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_expect_success 'create loose objects on import' '
+ test_tick &&
+ cat >input <<-INPUT_END &&
+ commit refs/heads/main
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ initial
+ COMMIT
+
+ done
+ INPUT_END
+
+ git -c fastimport.unpackLimit=2 fast-import --done <input &&
+ git fsck --no-progress &&
+ test $(find .git/objects/?? -type f | wc -l) -eq 2 &&
+ test $(find .git/objects/pack -type f | wc -l) -eq 0
+'
+
+test_expect_success 'bigger packs are preserved' '
+ test_tick &&
+ cat >input <<-INPUT_END &&
+ commit refs/heads/main
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ incremental should create a pack
+ COMMIT
+ from refs/heads/main^0
+
+ commit refs/heads/branch
+ committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data <<COMMIT
+ branch
+ COMMIT
+
+ done
+ INPUT_END
+
+ git -c fastimport.unpackLimit=2 fast-import --done <input &&
+ git fsck --no-progress &&
+ test $(find .git/objects/?? -type f | wc -l) -eq 2 &&
+ test $(find .git/objects/pack -type f | wc -l) -eq 2
+'
+
+test_expect_success 'lookups after checkpoint works' '
+ hello_id=$(echo hello | git hash-object --stdin -t blob) &&
+ id="$GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE" &&
+ before=$(git rev-parse refs/heads/main^0) &&
+ (
+ cat <<-INPUT_END &&
+ blob
+ mark :1
+ data 6
+ hello
+
+ commit refs/heads/main
+ mark :2
+ committer $id
+ data <<COMMIT
+ checkpoint after this
+ COMMIT
+ from refs/heads/main^0
+ M 100644 :1 hello
+
+ # pre-checkpoint
+ cat-blob :1
+ cat-blob $hello_id
+ checkpoint
+ # post-checkpoint
+ cat-blob :1
+ cat-blob $hello_id
+ INPUT_END
+
+ n=0 &&
+ from=$before &&
+ while test x"$from" = x"$before"
+ do
+ if test $n -gt 30
+ then
+ echo >&2 "checkpoint did not update branch" &&
+ exit 1
+ else
+ n=$(($n + 1))
+ fi &&
+ sleep 1 &&
+ from=$(git rev-parse refs/heads/main^0)
+ done &&
+ cat <<-INPUT_END &&
+ commit refs/heads/main
+ committer $id
+ data <<COMMIT
+ make sure from "unpacked sha1 reference" works, too
+ COMMIT
+ from $from
+ INPUT_END
+ echo done
+ ) | git -c fastimport.unpackLimit=100 fast-import --done &&
+ test $(find .git/objects/?? -type f | wc -l) -eq 6 &&
+ test $(find .git/objects/pack -type f | wc -l) -eq 2
+'
+
+test_done
diff --git a/t/t9303-fast-import-compression.sh b/t/t9303-fast-import-compression.sh
new file mode 100755
index 0000000..4f5bf40
--- /dev/null
+++ b/t/t9303-fast-import-compression.sh
@@ -0,0 +1,63 @@
+#!/bin/sh
+
+test_description='compression setting of fast-import utility'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+import_large () {
+ (
+ echo blob
+ echo "data <<EOD"
+ printf "%2000000s\n" "$*"
+ echo EOD
+ ) | git "$@" fast-import
+}
+
+while read expect config
+do
+ test_expect_success "fast-import (packed) with $config" '
+ test_when_finished "rm -f .git/objects/pack/pack-*.*" &&
+ test_when_finished "rm -rf .git/objects/??" &&
+ import_large -c fastimport.unpacklimit=0 $config &&
+ sz=$(test_file_size .git/objects/pack/pack-*.pack) &&
+ case "$expect" in
+ small) test "$sz" -le 100000 ;;
+ large) test "$sz" -ge 100000 ;;
+ esac
+ '
+done <<\EOF
+large -c core.compression=0
+small -c core.compression=9
+large -c core.compression=0 -c pack.compression=0
+large -c core.compression=9 -c pack.compression=0
+small -c core.compression=0 -c pack.compression=9
+small -c core.compression=9 -c pack.compression=9
+large -c pack.compression=0
+small -c pack.compression=9
+EOF
+
+while read expect config
+do
+ test_expect_success "fast-import (loose) with $config" '
+ test_when_finished "rm -f .git/objects/pack/pack-*.*" &&
+ test_when_finished "rm -rf .git/objects/??" &&
+ import_large -c fastimport.unpacklimit=9 $config &&
+ sz=$(test_file_size .git/objects/??/????*) &&
+ case "$expect" in
+ small) test "$sz" -le 100000 ;;
+ large) test "$sz" -ge 100000 ;;
+ esac
+ '
+done <<\EOF
+large -c core.compression=0
+small -c core.compression=9
+large -c core.compression=0 -c core.loosecompression=0
+large -c core.compression=9 -c core.loosecompression=0
+small -c core.compression=0 -c core.loosecompression=9
+small -c core.compression=9 -c core.loosecompression=9
+large -c core.loosecompression=0
+small -c core.loosecompression=9
+EOF
+
+test_done
diff --git a/t/t9304-fast-import-marks.sh b/t/t9304-fast-import-marks.sh
new file mode 100755
index 0000000..410a871
--- /dev/null
+++ b/t/t9304-fast-import-marks.sh
@@ -0,0 +1,81 @@
+#!/bin/sh
+
+test_description='test exotic situations with marks'
+. ./test-lib.sh
+
+test_expect_success 'setup dump of basic history' '
+ test_commit one &&
+ git fast-export --export-marks=marks HEAD >dump
+'
+
+test_expect_success 'setup large marks file' '
+ # normally a marks file would have a lot of useful, unique
+ # marks. But for our purposes, just having a lot of nonsense
+ # ones is fine. Start at 1024 to avoid clashing with marks
+ # legitimately used in our tiny dump.
+ blob=$(git rev-parse HEAD:one.t) &&
+ for i in $(test_seq 1024 16384)
+ do
+ echo ":$i $blob" || return 1
+ done >>marks
+'
+
+test_expect_success 'import with large marks file' '
+ git fast-import --import-marks=marks <dump
+'
+
+test_expect_success 'setup dump with submodule' '
+ test_config_global protocol.file.allow always &&
+ git submodule add "$PWD" sub &&
+ git commit -m "add submodule" &&
+ git fast-export HEAD >dump
+'
+
+test_expect_success 'setup submodule mapping with large id' '
+ old=$(git rev-parse HEAD:sub) &&
+ new=$(echo $old | sed s/./a/g) &&
+ echo ":12345 $old" >from &&
+ echo ":12345 $new" >to
+'
+
+test_expect_success 'import with submodule mapping' '
+ git init dst &&
+ git -C dst fast-import \
+ --rewrite-submodules-from=sub:../from \
+ --rewrite-submodules-to=sub:../to \
+ <dump &&
+ git -C dst rev-parse HEAD:sub >actual &&
+ echo "$new" >expect &&
+ test_cmp expect actual
+'
+
+test_expect_success 'paths adjusted for relative subdir' '
+ git init deep-dst &&
+ mkdir deep-dst/subdir &&
+ >deep-dst/subdir/empty-marks &&
+ git -C deep-dst/subdir fast-import \
+ --rewrite-submodules-from=sub:../../from \
+ --rewrite-submodules-to=sub:../../to \
+ --import-marks=empty-marks \
+ --export-marks=exported-marks \
+ --export-pack-edges=exported-edges \
+ <dump &&
+ # we do not bother checking resulting repo; we just care that nothing
+ # complained about failing to open files for reading, and that files
+ # for writing were created in the expected spot
+ test_path_is_file deep-dst/subdir/exported-marks &&
+ test_path_is_file deep-dst/subdir/exported-edges
+'
+
+test_expect_success 'relative marks are not affected by subdir' '
+ git init deep-relative &&
+ mkdir deep-relative/subdir &&
+ git -C deep-relative/subdir fast-import \
+ --relative-marks \
+ --export-marks=exported-marks \
+ <dump &&
+ test_path_is_missing deep-relative/subdir/exported-marks &&
+ test_path_is_file deep-relative/.git/info/fast-import/exported-marks
+'
+
+test_done
diff --git a/t/t9350-fast-export.sh b/t/t9350-fast-export.sh
new file mode 100755
index 0000000..26c25c0
--- /dev/null
+++ b/t/t9350-fast-export.sh
@@ -0,0 +1,794 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Johannes E. Schindelin
+#
+
+test_description='git fast-export'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+
+ echo break it > file0 &&
+ git add file0 &&
+ test_tick &&
+ echo Wohlauf > file &&
+ git add file &&
+ test_tick &&
+ git commit -m initial &&
+ echo die Luft > file &&
+ echo geht frisch > file2 &&
+ git add file file2 &&
+ test_tick &&
+ git commit -m second &&
+ echo und > file2 &&
+ test_tick &&
+ git commit -m third file2 &&
+ test_tick &&
+ git tag rein &&
+ git checkout -b wer HEAD^ &&
+ echo lange > file2 &&
+ test_tick &&
+ git commit -m sitzt file2 &&
+ test_tick &&
+ git tag -a -m valentin muss &&
+ git merge -s ours main
+
+'
+
+test_expect_success 'fast-export | fast-import' '
+
+ MAIN=$(git rev-parse --verify main) &&
+ REIN=$(git rev-parse --verify rein) &&
+ WER=$(git rev-parse --verify wer) &&
+ MUSS=$(git rev-parse --verify muss) &&
+ mkdir new &&
+ git --git-dir=new/.git init &&
+ git fast-export --all >actual &&
+ (cd new &&
+ git fast-import &&
+ test $MAIN = $(git rev-parse --verify refs/heads/main) &&
+ test $REIN = $(git rev-parse --verify refs/tags/rein) &&
+ test $WER = $(git rev-parse --verify refs/heads/wer) &&
+ test $MUSS = $(git rev-parse --verify refs/tags/muss)) <actual
+
+'
+
+test_expect_success 'fast-export ^muss^{commit} muss' '
+ git fast-export --tag-of-filtered-object=rewrite ^muss^{commit} muss >actual &&
+ cat >expected <<-EOF &&
+ tag muss
+ from $(git rev-parse --verify muss^{commit})
+ $(git cat-file tag muss | grep tagger)
+ data 9
+ valentin
+
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'fast-export --mark-tags ^muss^{commit} muss' '
+ git fast-export --mark-tags --tag-of-filtered-object=rewrite ^muss^{commit} muss >actual &&
+ cat >expected <<-EOF &&
+ tag muss
+ mark :1
+ from $(git rev-parse --verify muss^{commit})
+ $(git cat-file tag muss | grep tagger)
+ data 9
+ valentin
+
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'fast-export main~2..main' '
+
+ git fast-export main~2..main >actual &&
+ sed "s/main/partial/" actual |
+ (cd new &&
+ git fast-import &&
+ test $MAIN != $(git rev-parse --verify refs/heads/partial) &&
+ git diff --exit-code main partial &&
+ git diff --exit-code main^ partial^ &&
+ test_must_fail git rev-parse partial~2)
+
+'
+
+test_expect_success 'fast-export --reference-excluded-parents main~2..main' '
+
+ git fast-export --reference-excluded-parents main~2..main >actual &&
+ grep commit.refs/heads/main actual >commit-count &&
+ test_line_count = 2 commit-count &&
+ sed "s/main/rewrite/" actual |
+ (cd new &&
+ git fast-import &&
+ test $MAIN = $(git rev-parse --verify refs/heads/rewrite))
+'
+
+test_expect_success 'fast-export --show-original-ids' '
+
+ git fast-export --show-original-ids main >output &&
+ grep ^original-oid output| sed -e s/^original-oid.// | sort >actual &&
+ git rev-list --objects main muss >objects-and-names &&
+ awk "{print \$1}" objects-and-names | sort >commits-trees-blobs &&
+ comm -23 actual commits-trees-blobs >unfound &&
+ test_must_be_empty unfound
+'
+
+test_expect_success 'fast-export --show-original-ids | git fast-import' '
+
+ git fast-export --show-original-ids main muss | git fast-import --quiet &&
+ test $MAIN = $(git rev-parse --verify refs/heads/main) &&
+ test $MUSS = $(git rev-parse --verify refs/tags/muss)
+'
+
+test_expect_success 'reencoding iso-8859-7' '
+
+ test_when_finished "git reset --hard HEAD~1" &&
+ test_config i18n.commitencoding iso-8859-7 &&
+ test_tick &&
+ echo rosten >file &&
+ git commit -s -F "$TEST_DIRECTORY/t9350/simple-iso-8859-7-commit-message.txt" file &&
+ git fast-export --reencode=yes wer^..wer >iso-8859-7.fi &&
+ sed "s/wer/i18n/" iso-8859-7.fi |
+ (cd new &&
+ git fast-import &&
+ # The commit object, if not re-encoded, would be 200 bytes plus hash.
+ # Removing the "encoding iso-8859-7\n" header drops 20 bytes.
+ # Re-encoding the Pi character from \xF0 (\360) in iso-8859-7
+ # to \xCF\x80 (\317\200) in UTF-8 adds a byte. Check for
+ # the expected size.
+ test $(($(test_oid hexsz) + 181)) -eq "$(git cat-file -s i18n)" &&
+ # ...and for the expected translation of bytes.
+ git cat-file commit i18n >actual &&
+ grep $(printf "\317\200") actual &&
+ # Also make sure the commit does not have the "encoding" header
+ ! grep ^encoding actual)
+'
+
+test_expect_success 'aborting on iso-8859-7' '
+
+ test_when_finished "git reset --hard HEAD~1" &&
+ test_config i18n.commitencoding iso-8859-7 &&
+ echo rosten >file &&
+ git commit -s -F "$TEST_DIRECTORY/t9350/simple-iso-8859-7-commit-message.txt" file &&
+ test_must_fail git fast-export --reencode=abort wer^..wer >iso-8859-7.fi
+'
+
+test_expect_success 'preserving iso-8859-7' '
+
+ test_when_finished "git reset --hard HEAD~1" &&
+ test_config i18n.commitencoding iso-8859-7 &&
+ echo rosten >file &&
+ git commit -s -F "$TEST_DIRECTORY/t9350/simple-iso-8859-7-commit-message.txt" file &&
+ git fast-export --reencode=no wer^..wer >iso-8859-7.fi &&
+ sed "s/wer/i18n-no-recoding/" iso-8859-7.fi |
+ (cd new &&
+ git fast-import &&
+ # The commit object, if not re-encoded, is 200 bytes plus hash.
+ # Removing the "encoding iso-8859-7\n" header would drops 20
+ # bytes. Re-encoding the Pi character from \xF0 (\360) in
+ # iso-8859-7 to \xCF\x80 (\317\200) in UTF-8 adds a byte.
+ # Check for the expected size...
+ test $(($(test_oid hexsz) + 200)) -eq "$(git cat-file -s i18n-no-recoding)" &&
+ # ...as well as the expected byte.
+ git cat-file commit i18n-no-recoding >actual &&
+ grep $(printf "\360") actual &&
+ # Also make sure the commit has the "encoding" header
+ grep ^encoding actual)
+'
+
+test_expect_success 'encoding preserved if reencoding fails' '
+
+ test_when_finished "git reset --hard HEAD~1" &&
+ test_config i18n.commitencoding iso-8859-7 &&
+ echo rosten >file &&
+ git commit -s -F "$TEST_DIRECTORY/t9350/broken-iso-8859-7-commit-message.txt" file &&
+ git fast-export --reencode=yes wer^..wer >iso-8859-7.fi &&
+ sed "s/wer/i18n-invalid/" iso-8859-7.fi |
+ (cd new &&
+ git fast-import &&
+ git cat-file commit i18n-invalid >actual &&
+ # Make sure the commit still has the encoding header
+ grep ^encoding actual &&
+ # Verify that the commit has the expected size; i.e.
+ # that no bytes were re-encoded to a different encoding.
+ test $(($(test_oid hexsz) + 212)) -eq "$(git cat-file -s i18n-invalid)" &&
+ # ...and check for the original special bytes
+ grep $(printf "\360") actual &&
+ grep $(printf "\377") actual)
+'
+
+test_expect_success 'import/export-marks' '
+
+ git checkout -b marks main &&
+ git fast-export --export-marks=tmp-marks HEAD &&
+ test -s tmp-marks &&
+ test_line_count = 3 tmp-marks &&
+ git fast-export --import-marks=tmp-marks \
+ --export-marks=tmp-marks HEAD >actual &&
+ test $(grep ^commit actual | wc -l) -eq 0 &&
+ echo change > file &&
+ git commit -m "last commit" file &&
+ git fast-export --import-marks=tmp-marks \
+ --export-marks=tmp-marks HEAD >actual &&
+ test $(grep ^commit\ actual | wc -l) -eq 1 &&
+ test_line_count = 4 tmp-marks
+
+'
+
+cat > signed-tag-import << EOF
+tag sign-your-name
+from $(git rev-parse HEAD)
+tagger C O Mitter <committer@example.com> 1112911993 -0700
+data 210
+A message for a sign
+-----BEGIN PGP SIGNATURE-----
+Version: GnuPG v1.4.5 (GNU/Linux)
+
+fakedsignaturefakedsignaturefakedsignaturefakedsignaturfakedsign
+aturefakedsignaturefake=
+=/59v
+-----END PGP SIGNATURE-----
+EOF
+
+test_expect_success 'set up faked signed tag' '
+
+ cat signed-tag-import | git fast-import
+
+'
+
+test_expect_success 'signed-tags=abort' '
+
+ test_must_fail git fast-export --signed-tags=abort sign-your-name
+
+'
+
+test_expect_success 'signed-tags=verbatim' '
+
+ git fast-export --signed-tags=verbatim sign-your-name > output &&
+ grep PGP output
+
+'
+
+test_expect_success 'signed-tags=strip' '
+
+ git fast-export --signed-tags=strip sign-your-name > output &&
+ ! grep PGP output
+
+'
+
+test_expect_success 'signed-tags=warn-strip' '
+ git fast-export --signed-tags=warn-strip sign-your-name >output 2>err &&
+ ! grep PGP output &&
+ test -s err
+'
+
+test_expect_success 'setup submodule' '
+
+ test_config_global protocol.file.allow always &&
+ git checkout -f main &&
+ mkdir sub &&
+ (
+ cd sub &&
+ git init &&
+ echo test file > file &&
+ git add file &&
+ git commit -m sub_initial
+ ) &&
+ git submodule add "$(pwd)/sub" sub &&
+ git commit -m initial &&
+ test_tick &&
+ (
+ cd sub &&
+ echo more data >> file &&
+ git add file &&
+ git commit -m sub_second
+ ) &&
+ git add sub &&
+ git commit -m second
+
+'
+
+test_expect_success 'submodule fast-export | fast-import' '
+
+ test_config_global protocol.file.allow always &&
+ SUBENT1=$(git ls-tree main^ sub) &&
+ SUBENT2=$(git ls-tree main sub) &&
+ rm -rf new &&
+ mkdir new &&
+ git --git-dir=new/.git init &&
+ git fast-export --signed-tags=strip --all >actual &&
+ (cd new &&
+ git fast-import &&
+ test "$SUBENT1" = "$(git ls-tree refs/heads/main^ sub)" &&
+ test "$SUBENT2" = "$(git ls-tree refs/heads/main sub)" &&
+ git checkout main &&
+ git submodule init &&
+ git submodule update &&
+ cmp sub/file ../sub/file) <actual
+
+'
+
+GIT_AUTHOR_NAME='A U Thor'; export GIT_AUTHOR_NAME
+GIT_COMMITTER_NAME='C O Mitter'; export GIT_COMMITTER_NAME
+
+test_expect_success 'setup copies' '
+
+ git checkout -b copy rein &&
+ git mv file file3 &&
+ git commit -m move1 &&
+ test_tick &&
+ cp file2 file4 &&
+ git add file4 &&
+ git mv file2 file5 &&
+ git commit -m copy1 &&
+ test_tick &&
+ cp file3 file6 &&
+ git add file6 &&
+ git commit -m copy2 &&
+ test_tick &&
+ echo more text >> file6 &&
+ echo even more text >> file6 &&
+ git add file6 &&
+ git commit -m modify &&
+ test_tick &&
+ cp file6 file7 &&
+ echo test >> file7 &&
+ git add file7 &&
+ git commit -m copy_modify
+
+'
+
+test_expect_success 'fast-export -C -C | fast-import' '
+
+ ENTRY=$(git rev-parse --verify copy) &&
+ rm -rf new &&
+ mkdir new &&
+ git --git-dir=new/.git init &&
+ git fast-export -C -C --signed-tags=strip --all > output &&
+ grep "^C file2 file4\$" output &&
+ cat output |
+ (cd new &&
+ git fast-import &&
+ test $ENTRY = $(git rev-parse --verify refs/heads/copy))
+
+'
+
+test_expect_success 'fast-export | fast-import when main is tagged' '
+
+ git tag -m msg last &&
+ git fast-export -C -C --signed-tags=strip --all > output &&
+ test $(grep -c "^tag " output) = 3
+
+'
+
+cat > tag-content << EOF
+object $(git rev-parse HEAD)
+type commit
+tag rosten
+EOF
+
+test_expect_success 'cope with tagger-less tags' '
+
+ TAG=$(git hash-object --literally -t tag -w tag-content) &&
+ git update-ref refs/tags/sonnenschein $TAG &&
+ git fast-export -C -C --signed-tags=strip --all > output &&
+ test $(grep -c "^tag " output) = 4 &&
+ ! grep "Unspecified Tagger" output &&
+ git fast-export -C -C --signed-tags=strip --all \
+ --fake-missing-tagger > output &&
+ test $(grep -c "^tag " output) = 4 &&
+ grep "Unspecified Tagger" output
+
+'
+
+test_expect_success 'setup for limiting exports by PATH' '
+ mkdir limit-by-paths &&
+ (
+ cd limit-by-paths &&
+ git init &&
+ echo hi > there &&
+ git add there &&
+ git commit -m "First file" &&
+ echo foo > bar &&
+ git add bar &&
+ git commit -m "Second file" &&
+ git tag -a -m msg mytag &&
+ echo morefoo >> bar &&
+ git add bar &&
+ git commit -m "Change to second file"
+ )
+'
+
+cat > limit-by-paths/expected << EOF
+blob
+mark :1
+data 3
+hi
+
+reset refs/tags/mytag
+commit refs/tags/mytag
+mark :2
+author A U Thor <author@example.com> 1112912713 -0700
+committer C O Mitter <committer@example.com> 1112912713 -0700
+data 11
+First file
+M 100644 :1 there
+
+EOF
+
+test_expect_success 'dropping tag of filtered out object' '
+(
+ cd limit-by-paths &&
+ git fast-export --tag-of-filtered-object=drop mytag -- there > output &&
+ test_cmp expected output
+)
+'
+
+cat >> limit-by-paths/expected << EOF
+tag mytag
+from :2
+tagger C O Mitter <committer@example.com> 1112912713 -0700
+data 4
+msg
+
+EOF
+
+test_expect_success 'rewriting tag of filtered out object' '
+(
+ cd limit-by-paths &&
+ git fast-export --tag-of-filtered-object=rewrite mytag -- there > output &&
+ test_cmp expected output
+)
+'
+
+test_expect_success 'rewrite tag predating pathspecs to nothing' '
+ test_create_repo rewrite_tag_predating_pathspecs &&
+ (
+ cd rewrite_tag_predating_pathspecs &&
+
+ test_commit initial &&
+
+ git tag -a -m "Some old tag" v0.0.0.0.0.0.1 &&
+
+ test_commit bar &&
+
+ git fast-export --tag-of-filtered-object=rewrite --all -- bar.t >output &&
+ grep from.$ZERO_OID output
+ )
+'
+
+cat > limit-by-paths/expected << EOF
+blob
+mark :1
+data 4
+foo
+
+blob
+mark :2
+data 3
+hi
+
+reset refs/heads/main
+commit refs/heads/main
+mark :3
+author A U Thor <author@example.com> 1112912713 -0700
+committer C O Mitter <committer@example.com> 1112912713 -0700
+data 12
+Second file
+M 100644 :1 bar
+M 100644 :2 there
+
+EOF
+
+test_expect_failure 'no exact-ref revisions included' '
+ (
+ cd limit-by-paths &&
+ git fast-export main~2..main~1 > output &&
+ test_cmp expected output
+ )
+'
+
+test_expect_success 'path limiting with import-marks does not lose unmodified files' '
+ git checkout -b simple marks~2 &&
+ git fast-export --export-marks=marks simple -- file > /dev/null &&
+ echo more content >> file &&
+ test_tick &&
+ git commit -mnext file &&
+ git fast-export --import-marks=marks simple -- file file0 >actual &&
+ grep file0 actual
+'
+
+test_expect_success 'path limiting works' '
+ git fast-export simple -- file >actual &&
+ sed -ne "s/^M .* //p" <actual | sort -u >actual.files &&
+ echo file >expect &&
+ test_cmp expect actual.files
+'
+
+test_expect_success 'avoid corrupt stream with non-existent mark' '
+ test_create_repo avoid_non_existent_mark &&
+ (
+ cd avoid_non_existent_mark &&
+
+ test_commit important-path &&
+
+ test_commit ignored &&
+
+ git branch A &&
+ git branch B &&
+
+ echo foo >>important-path.t &&
+ git add important-path.t &&
+ test_commit more changes &&
+
+ git fast-export --all -- important-path.t | git fast-import --force
+ )
+'
+
+test_expect_success 'full-tree re-shows unmodified files' '
+ git checkout -f simple &&
+ git fast-export --full-tree simple >actual &&
+ test $(grep -c file0 actual) -eq 3
+'
+
+test_expect_success 'set-up a few more tags for tag export tests' '
+ git checkout -f main &&
+ HEAD_TREE=$(git show -s --pretty=raw HEAD | grep tree | sed "s/tree //") &&
+ git tag tree_tag -m "tagging a tree" $HEAD_TREE &&
+ git tag -a tree_tag-obj -m "tagging a tree" $HEAD_TREE &&
+ git tag tag-obj_tag -m "tagging a tag" tree_tag-obj &&
+ git tag -a tag-obj_tag-obj -m "tagging a tag" tree_tag-obj
+'
+
+test_expect_success 'tree_tag' '
+ mkdir result &&
+ (cd result && git init) &&
+ git fast-export tree_tag > fe-stream &&
+ (cd result && git fast-import < ../fe-stream)
+'
+
+# NEEDSWORK: not just check return status, but validate the output
+# Note that these tests DO NOTHING other than print a warning that
+# they are omitting the one tag we asked them to export (because the
+# tags resolve to a tree). They exist just to make sure we do not
+# abort but instead just warn.
+test_expect_success 'tree_tag-obj' 'git fast-export tree_tag-obj'
+test_expect_success 'tag-obj_tag' 'git fast-export tag-obj_tag'
+test_expect_success 'tag-obj_tag-obj' 'git fast-export tag-obj_tag-obj'
+
+test_expect_success 'handling tags of blobs' '
+ git tag -a -m "Tag of a blob" blobtag $(git rev-parse main:file) &&
+ git fast-export blobtag >actual &&
+ cat >expect <<-EOF &&
+ blob
+ mark :1
+ data 9
+ die Luft
+
+ tag blobtag
+ from :1
+ tagger $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> $GIT_COMMITTER_DATE
+ data 14
+ Tag of a blob
+
+ EOF
+ test_cmp expect actual
+'
+
+test_expect_success 'handling nested tags' '
+ git tag -a -m "This is a nested tag" nested muss &&
+ git fast-export --mark-tags nested >output &&
+ grep "^from $ZERO_OID$" output &&
+ grep "^tag nested$" output >tag_lines &&
+ test_line_count = 2 tag_lines
+'
+
+test_expect_success 'directory becomes symlink' '
+ git init dirtosymlink &&
+ git init result &&
+ (
+ cd dirtosymlink &&
+ mkdir foo &&
+ mkdir bar &&
+ echo hello > foo/world &&
+ echo hello > bar/world &&
+ git add foo/world bar/world &&
+ git commit -q -mone &&
+ git rm -r foo &&
+ test_ln_s_add bar foo &&
+ git commit -q -mtwo
+ ) &&
+ (
+ cd dirtosymlink &&
+ git fast-export main -- foo |
+ (cd ../result && git fast-import --quiet)
+ ) &&
+ (cd result && git show main:foo)
+'
+
+test_expect_success 'fast-export quotes pathnames' '
+ git init crazy-paths &&
+ test_config -C crazy-paths core.protectNTFS false &&
+ (cd crazy-paths &&
+ blob=$(echo foo | git hash-object -w --stdin) &&
+ git -c core.protectNTFS=false update-index --add \
+ --cacheinfo 100644 $blob "$(printf "path with\\nnewline")" \
+ --cacheinfo 100644 $blob "path with \"quote\"" \
+ --cacheinfo 100644 $blob "path with \\backslash" \
+ --cacheinfo 100644 $blob "path with space" &&
+ git commit -m addition &&
+ git ls-files -z -s | perl -0pe "s{\\t}{$&subdir/}" >index &&
+ git read-tree --empty &&
+ git update-index -z --index-info <index &&
+ git commit -m rename &&
+ git read-tree --empty &&
+ git commit -m deletion &&
+ git fast-export -M HEAD >export.out &&
+ git rev-list HEAD >expect &&
+ git init result &&
+ cd result &&
+ git fast-import <../export.out &&
+ git rev-list HEAD >actual &&
+ test_cmp ../expect actual
+ )
+'
+
+test_expect_success 'test bidirectionality' '
+ git init marks-test &&
+ git fast-export --export-marks=marks-cur --import-marks-if-exists=marks-cur --branches | \
+ git --git-dir=marks-test/.git fast-import --export-marks=marks-new --import-marks-if-exists=marks-new &&
+ (cd marks-test &&
+ git reset --hard &&
+ echo Wohlauf > file &&
+ git commit -a -m "back in time") &&
+ git --git-dir=marks-test/.git fast-export --export-marks=marks-new --import-marks-if-exists=marks-new --branches | \
+ git fast-import --export-marks=marks-cur --import-marks-if-exists=marks-cur
+'
+
+cat > expected << EOF
+blob
+mark :13
+data 5
+bump
+
+commit refs/heads/main
+mark :14
+author A U Thor <author@example.com> 1112912773 -0700
+committer C O Mitter <committer@example.com> 1112912773 -0700
+data 5
+bump
+from :12
+M 100644 :13 file
+
+EOF
+
+test_expect_success 'avoid uninteresting refs' '
+ > tmp-marks &&
+ git fast-export --import-marks=tmp-marks \
+ --export-marks=tmp-marks main > /dev/null &&
+ git tag v1.0 &&
+ git branch uninteresting &&
+ echo bump > file &&
+ git commit -a -m bump &&
+ git fast-export --import-marks=tmp-marks \
+ --export-marks=tmp-marks ^uninteresting ^v1.0 main > actual &&
+ test_cmp expected actual
+'
+
+cat > expected << EOF
+reset refs/heads/main
+from :14
+
+EOF
+
+test_expect_success 'refs are updated even if no commits need to be exported' '
+ > tmp-marks &&
+ git fast-export --import-marks=tmp-marks \
+ --export-marks=tmp-marks main > /dev/null &&
+ git fast-export --import-marks=tmp-marks \
+ --export-marks=tmp-marks main > actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'use refspec' '
+ git fast-export --refspec refs/heads/main:refs/heads/foobar main >actual2 &&
+ grep "^commit " actual2 | sort | uniq >actual &&
+ echo "commit refs/heads/foobar" > expected &&
+ test_cmp expected actual
+'
+
+test_expect_success 'delete ref because entire history excluded' '
+ git branch to-delete &&
+ git fast-export to-delete ^to-delete >actual &&
+ cat >expected <<-EOF &&
+ reset refs/heads/to-delete
+ from $ZERO_OID
+
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'delete refspec' '
+ git fast-export --refspec :refs/heads/to-delete >actual &&
+ cat >expected <<-EOF &&
+ reset refs/heads/to-delete
+ from $ZERO_OID
+
+ EOF
+ test_cmp expected actual
+'
+
+test_expect_success 'when using -C, do not declare copy when source of copy is also modified' '
+ test_create_repo src &&
+ echo a_line >src/file.txt &&
+ git -C src add file.txt &&
+ git -C src commit -m 1st_commit &&
+
+ cp src/file.txt src/file2.txt &&
+ echo another_line >>src/file.txt &&
+ git -C src add file.txt file2.txt &&
+ git -C src commit -m 2nd_commit &&
+
+ test_create_repo dst &&
+ git -C src fast-export --all -C >actual &&
+ git -C dst fast-import <actual &&
+ git -C src show >expected &&
+ git -C dst show >actual &&
+ test_cmp expected actual
+'
+
+test_expect_success 'merge commit gets exported with --import-marks' '
+ test_create_repo merging &&
+ (
+ cd merging &&
+ test_commit initial &&
+ git checkout -b topic &&
+ test_commit on-topic &&
+ git checkout main &&
+ test_commit on-main &&
+ test_tick &&
+ git merge --no-ff -m Yeah topic &&
+
+ echo ":1 $(git rev-parse HEAD^^)" >marks &&
+ git fast-export --import-marks=marks main >out &&
+ grep Yeah out
+ )
+'
+
+
+test_expect_success 'fast-export --first-parent outputs all revisions output by revision walk' '
+ git init first-parent &&
+ (
+ cd first-parent &&
+ test_commit A &&
+ git checkout -b topic1 &&
+ test_commit B &&
+ git checkout main &&
+ git merge --no-ff topic1 &&
+
+ git checkout -b topic2 &&
+ test_commit C &&
+ git checkout main &&
+ git merge --no-ff topic2 &&
+
+ test_commit D &&
+
+ git fast-export main -- --first-parent >first-parent-export &&
+ git fast-export main -- --first-parent --reverse >first-parent-reverse-export &&
+ test_cmp first-parent-export first-parent-reverse-export &&
+
+ git init import &&
+ git -C import fast-import <first-parent-export &&
+
+ git log --format="%ad %s" --first-parent main >expected &&
+ git -C import log --format="%ad %s" --all >actual &&
+ test_cmp expected actual &&
+ test_line_count = 4 actual
+ )
+'
+
+test_done
diff --git a/t/t9350/broken-iso-8859-7-commit-message.txt b/t/t9350/broken-iso-8859-7-commit-message.txt
new file mode 100644
index 0000000..d06ad75
--- /dev/null
+++ b/t/t9350/broken-iso-8859-7-commit-message.txt
@@ -0,0 +1 @@
+Pi: ð; Invalid: ÿ \ No newline at end of file
diff --git a/t/t9350/simple-iso-8859-7-commit-message.txt b/t/t9350/simple-iso-8859-7-commit-message.txt
new file mode 100644
index 0000000..8b3f0c3
--- /dev/null
+++ b/t/t9350/simple-iso-8859-7-commit-message.txt
@@ -0,0 +1 @@
+Pi: ð \ No newline at end of file
diff --git a/t/t9351-fast-export-anonymize.sh b/t/t9351-fast-export-anonymize.sh
new file mode 100755
index 0000000..156a647
--- /dev/null
+++ b/t/t9351-fast-export-anonymize.sh
@@ -0,0 +1,141 @@
+#!/bin/sh
+
+test_description='basic tests for fast-export --anonymize'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+test_expect_success 'setup simple repo' '
+ test_commit base &&
+ test_commit foo &&
+ test_commit retain-me &&
+ git checkout -b other HEAD^ &&
+ mkdir subdir &&
+ test_commit subdir/bar &&
+ test_commit subdir/xyzzy &&
+ fake_commit=$(echo $ZERO_OID | sed s/0/a/) &&
+ git update-index --add --cacheinfo 160000,$fake_commit,link1 &&
+ git update-index --add --cacheinfo 160000,$fake_commit,link2 &&
+ git commit -m "add gitlink" &&
+ git tag -m "annotated tag" mytag &&
+ git tag -m "annotated tag with long message" longtag
+'
+
+test_expect_success 'export anonymized stream' '
+ git fast-export --anonymize --all \
+ --anonymize-map=retain-me \
+ --anonymize-map=xyzzy:should-not-appear \
+ --anonymize-map=xyzzy:custom-name \
+ --anonymize-map=other \
+ >stream
+'
+
+# this also covers commit messages
+test_expect_success 'stream omits path names' '
+ ! grep base stream &&
+ ! grep foo stream &&
+ ! grep subdir stream &&
+ ! grep bar stream &&
+ ! grep xyzzy stream
+'
+
+test_expect_success 'stream contains user-specified names' '
+ grep retain-me stream &&
+ ! grep should-not-appear stream &&
+ grep custom-name stream
+'
+
+test_expect_success 'stream omits gitlink oids' '
+ # avoid relying on the whole oid to remain hash-agnostic; this is
+ # plenty to be unique within our test case
+ ! grep a000000000000000000 stream
+'
+
+test_expect_success 'stream retains other as refname' '
+ grep other stream
+'
+
+test_expect_success 'stream omits other refnames' '
+ ! grep main stream &&
+ ! grep mytag stream &&
+ ! grep longtag stream
+'
+
+test_expect_success 'stream omits identities' '
+ ! grep "$GIT_COMMITTER_NAME" stream &&
+ ! grep "$GIT_COMMITTER_EMAIL" stream &&
+ ! grep "$GIT_AUTHOR_NAME" stream &&
+ ! grep "$GIT_AUTHOR_EMAIL" stream
+'
+
+test_expect_success 'stream omits tag message' '
+ ! grep "annotated tag" stream
+'
+
+# NOTE: we chdir to the new, anonymized repository
+# after this. All further tests should assume this.
+test_expect_success 'import stream to new repository' '
+ git init new &&
+ cd new &&
+ git fast-import <../stream
+'
+
+test_expect_success 'result has two branches' '
+ git for-each-ref --format="%(refname)" refs/heads >branches &&
+ test_line_count = 2 branches &&
+ other_branch=refs/heads/other &&
+ main_branch=$(grep -v $other_branch branches)
+'
+
+test_expect_success 'repo has original shape and timestamps' '
+ shape () {
+ git log --format="%m %ct" --left-right --boundary "$@"
+ } &&
+ (cd .. && shape main...other) >expect &&
+ shape $main_branch...$other_branch >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'root tree has original shape' '
+ # the output entries are not necessarily in the same
+ # order, but we should at least have the same set of
+ # object types.
+ git -C .. ls-tree HEAD >orig-root &&
+ cut -d" " -f2 <orig-root | sort >expect &&
+ git ls-tree $other_branch >root &&
+ cut -d" " -f2 <root | sort >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'paths in subdir ended up in one tree' '
+ git -C .. ls-tree other:subdir >orig-subdir &&
+ cut -d" " -f2 <orig-subdir | sort >expect &&
+ tree=$(grep tree root | cut -f2) &&
+ git ls-tree $other_branch:$tree >tree &&
+ cut -d" " -f2 <tree >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'identical gitlinks got identical oid' '
+ awk "/commit/ { print \$3 }" <root | sort -u >commits &&
+ test_line_count = 1 commits
+'
+
+test_expect_success 'all tags point to branch tip' '
+ git rev-parse $other_branch >expect &&
+ git for-each-ref --format="%(*objectname)" | grep . | uniq >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'idents are shared' '
+ git log --all --format="%an <%ae>" >authors &&
+ sort -u authors >unique &&
+ test_line_count = 1 unique &&
+ git log --all --format="%cn <%ce>" >committers &&
+ sort -u committers >unique &&
+ test_line_count = 1 unique &&
+ ! test_cmp authors committers
+'
+
+test_done
diff --git a/t/t9400-git-cvsserver-server.sh b/t/t9400-git-cvsserver-server.sh
new file mode 100755
index 0000000..003c0b6
--- /dev/null
+++ b/t/t9400-git-cvsserver-server.sh
@@ -0,0 +1,672 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Frank Lichtenheld
+#
+
+test_description='git-cvsserver access
+
+tests read access to a git repository with the
+cvs CLI client via git-cvsserver server'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+if ! test_have_prereq PERL; then
+ skip_all='skipping git cvsserver tests, perl not available'
+ test_done
+fi
+cvs >/dev/null 2>&1
+if test $? -ne 1
+then
+ skip_all='skipping git-cvsserver tests, cvs not found'
+ test_done
+fi
+perl -e 'use DBI; use DBD::SQLite' >/dev/null 2>&1 || {
+ skip_all='skipping git-cvsserver tests, Perl SQLite interface unavailable'
+ test_done
+}
+
+WORKDIR=$PWD
+SERVERDIR=$PWD/gitcvs.git
+git_config="$SERVERDIR/config"
+CVSROOT=":fork:$SERVERDIR"
+CVSWORK="$PWD/cvswork"
+CVS_SERVER=git-cvsserver
+export CVSROOT CVS_SERVER
+
+if perl -e 'exit(1) if not defined crypt("", "cv")'
+then
+ PWDHASH='lac2ItudM3.KM'
+else
+ PWDHASH='$2b$10$t8fGvE/a9eLmfOLzsZme2uOa2QtoMYwIxq9wZA6aBKtF1Yb7FJIzi'
+fi
+
+rm -rf "$CVSWORK" "$SERVERDIR"
+test_expect_success 'setup' '
+ git config push.default matching &&
+ echo >empty &&
+ git add empty &&
+ git commit -q -m "First Commit" &&
+ mkdir secondroot &&
+ ( cd secondroot &&
+ git init &&
+ touch secondrootfile &&
+ git add secondrootfile &&
+ git commit -m "second root") &&
+ git fetch secondroot main &&
+ git merge --allow-unrelated-histories FETCH_HEAD &&
+ git clone -q --bare "$WORKDIR/.git" "$SERVERDIR" >/dev/null 2>&1 &&
+ GIT_DIR="$SERVERDIR" git config --bool gitcvs.enabled true &&
+ GIT_DIR="$SERVERDIR" git config gitcvs.logfile "$SERVERDIR/gitcvs.log" &&
+ GIT_DIR="$SERVERDIR" git config gitcvs.authdb "$SERVERDIR/auth.db" &&
+ echo "cvsuser:$PWDHASH" >"$SERVERDIR/auth.db"
+'
+
+# note that cvs doesn't accept absolute pathnames
+# as argument to co -d
+test_expect_success 'basic checkout' '
+ GIT_CONFIG="$git_config" cvs -Q co -d cvswork main &&
+ test "$(echo $(grep -v ^D cvswork/CVS/Entries|cut -d/ -f2,3,5 | head -n 1))" = "empty/1.1/" &&
+ test "$(echo $(grep -v ^D cvswork/CVS/Entries|cut -d/ -f2,3,5 | sed -ne \$p))" = "secondrootfile/1.1/"
+'
+
+#------------------------
+# PSERVER AUTHENTICATION
+#------------------------
+
+cat >request-anonymous <<EOF
+BEGIN AUTH REQUEST
+$SERVERDIR
+anonymous
+
+END AUTH REQUEST
+EOF
+
+cat >request-git <<EOF
+BEGIN AUTH REQUEST
+$SERVERDIR
+git
+
+END AUTH REQUEST
+EOF
+
+cat >login-anonymous <<EOF
+BEGIN VERIFICATION REQUEST
+$SERVERDIR
+anonymous
+
+END VERIFICATION REQUEST
+EOF
+
+cat >login-git <<EOF
+BEGIN VERIFICATION REQUEST
+$SERVERDIR
+git
+
+END VERIFICATION REQUEST
+EOF
+
+cat >login-git-ok <<EOF
+BEGIN VERIFICATION REQUEST
+$SERVERDIR
+cvsuser
+Ah<Z:yZZ30 e
+END VERIFICATION REQUEST
+EOF
+
+test_expect_success 'pserver authentication' '
+ cat request-anonymous | git-cvsserver pserver >log 2>&1 &&
+ sed -ne \$p log | grep "^I LOVE YOU\$"
+'
+
+test_expect_success 'pserver authentication failure (non-anonymous user)' '
+ if cat request-git | git-cvsserver pserver >log 2>&1
+ then
+ false
+ else
+ true
+ fi &&
+ sed -ne \$p log | grep "^I HATE YOU\$"
+'
+
+test_expect_success 'pserver authentication success (non-anonymous user with password)' '
+ cat login-git-ok | git-cvsserver pserver >log 2>&1 &&
+ sed -ne \$p log | grep "^I LOVE YOU\$"
+'
+
+test_expect_success 'pserver authentication (login)' '
+ cat login-anonymous | git-cvsserver pserver >log 2>&1 &&
+ sed -ne \$p log | grep "^I LOVE YOU\$"
+'
+
+test_expect_success 'pserver authentication failure (login/non-anonymous user)' '
+ if cat login-git | git-cvsserver pserver >log 2>&1
+ then
+ false
+ else
+ true
+ fi &&
+ sed -ne \$p log | grep "^I HATE YOU\$"
+'
+
+
+# misuse pserver authentication for testing of req_Root
+
+cat >request-relative <<EOF
+BEGIN AUTH REQUEST
+gitcvs.git
+anonymous
+
+END AUTH REQUEST
+EOF
+
+cat >request-conflict <<EOF
+BEGIN AUTH REQUEST
+$SERVERDIR
+anonymous
+
+END AUTH REQUEST
+Root $WORKDIR
+EOF
+
+test_expect_success 'req_Root failure (relative pathname)' '
+ if cat request-relative | git-cvsserver pserver >log 2>&1
+ then
+ echo unexpected success
+ false
+ else
+ true
+ fi &&
+ tail log | grep "^error 1 Root must be an absolute pathname$"
+'
+
+test_expect_success 'req_Root failure (conflicting roots)' '
+ cat request-conflict | git-cvsserver pserver >log 2>&1 &&
+ tail log | grep "^error 1 Conflicting roots specified$"
+'
+
+test_expect_success 'req_Root (strict paths)' '
+ cat request-anonymous | git-cvsserver --strict-paths pserver "$SERVERDIR" >log 2>&1 &&
+ sed -ne \$p log | grep "^I LOVE YOU\$"
+'
+
+test_expect_success 'req_Root failure (strict-paths)' '
+ ! cat request-anonymous |
+ git-cvsserver --strict-paths pserver "$WORKDIR" >log 2>&1
+'
+
+test_expect_success 'req_Root (w/o strict-paths)' '
+ cat request-anonymous | git-cvsserver pserver "$WORKDIR/" >log 2>&1 &&
+ sed -ne \$p log | grep "^I LOVE YOU\$"
+'
+
+test_expect_success 'req_Root failure (w/o strict-paths)' '
+ ! cat request-anonymous |
+ git-cvsserver pserver "$WORKDIR/gitcvs" >log 2>&1
+'
+
+cat >request-base <<EOF
+BEGIN AUTH REQUEST
+/gitcvs.git
+anonymous
+
+END AUTH REQUEST
+Root /gitcvs.git
+EOF
+
+test_expect_success 'req_Root (base-path)' '
+ cat request-base | git-cvsserver --strict-paths --base-path "$WORKDIR/" pserver "$SERVERDIR" >log 2>&1 &&
+ sed -ne \$p log | grep "^I LOVE YOU\$"
+'
+
+test_expect_success 'req_Root failure (base-path)' '
+ ! cat request-anonymous |
+ git-cvsserver --strict-paths --base-path "$WORKDIR" pserver "$SERVERDIR" >log 2>&1
+'
+
+GIT_DIR="$SERVERDIR" git config --bool gitcvs.enabled false || exit 1
+
+test_expect_success 'req_Root (export-all)' '
+ cat request-anonymous | git-cvsserver --export-all pserver "$WORKDIR" >log 2>&1 &&
+ sed -ne \$p log | grep "^I LOVE YOU\$"
+'
+
+test_expect_success 'req_Root failure (export-all w/o directory list)' '
+ ! (cat request-anonymous | git-cvsserver --export-all pserver >log 2>&1 || false)'
+
+test_expect_success 'req_Root (everything together)' '
+ cat request-base | git-cvsserver --export-all --strict-paths --base-path "$WORKDIR/" pserver "$SERVERDIR" >log 2>&1 &&
+ sed -ne \$p log | grep "^I LOVE YOU\$"
+'
+
+GIT_DIR="$SERVERDIR" git config --bool gitcvs.enabled true || exit 1
+
+#--------------
+# CONFIG TESTS
+#--------------
+
+test_expect_success 'gitcvs.enabled = false' \
+ 'GIT_DIR="$SERVERDIR" git config --bool gitcvs.enabled false &&
+ if GIT_CONFIG="$git_config" cvs -Q co -d cvswork2 main >cvs.log 2>&1
+ then
+ echo unexpected cvs success
+ false
+ else
+ true
+ fi &&
+ grep "GITCVS emulation disabled" cvs.log &&
+ test ! -d cvswork2'
+
+rm -fr cvswork2
+test_expect_success 'gitcvs.ext.enabled = true' '
+ GIT_DIR="$SERVERDIR" git config --bool gitcvs.ext.enabled true &&
+ GIT_DIR="$SERVERDIR" git config --bool gitcvs.enabled false &&
+ GIT_CONFIG="$git_config" cvs -Q co -d cvswork2 main >cvs.log 2>&1 &&
+ test_cmp cvswork cvswork2
+'
+
+rm -fr cvswork2
+test_expect_success 'gitcvs.ext.enabled = false' '
+ GIT_DIR="$SERVERDIR" git config --bool gitcvs.ext.enabled false &&
+ GIT_DIR="$SERVERDIR" git config --bool gitcvs.enabled true &&
+ if GIT_CONFIG="$git_config" cvs -Q co -d cvswork2 main >cvs.log 2>&1
+ then
+ echo unexpected cvs success
+ false
+ else
+ true
+ fi &&
+ grep "GITCVS emulation disabled" cvs.log &&
+ test ! -d cvswork2
+'
+
+rm -fr cvswork2
+test_expect_success 'gitcvs.dbname' '
+ GIT_DIR="$SERVERDIR" git config --bool gitcvs.ext.enabled true &&
+ GIT_DIR="$SERVERDIR" git config gitcvs.dbname %Ggitcvs.%a.%m.sqlite &&
+ GIT_CONFIG="$git_config" cvs -Q co -d cvswork2 main >cvs.log 2>&1 &&
+ test_cmp cvswork cvswork2 &&
+ test -f "$SERVERDIR/gitcvs.ext.main.sqlite" &&
+ cmp "$SERVERDIR/gitcvs.main.sqlite" "$SERVERDIR/gitcvs.ext.main.sqlite"
+'
+
+rm -fr cvswork2
+test_expect_success 'gitcvs.ext.dbname' '
+ GIT_DIR="$SERVERDIR" git config --bool gitcvs.ext.enabled true &&
+ GIT_DIR="$SERVERDIR" git config gitcvs.ext.dbname %Ggitcvs1.%a.%m.sqlite &&
+ GIT_DIR="$SERVERDIR" git config gitcvs.dbname %Ggitcvs2.%a.%m.sqlite &&
+ GIT_CONFIG="$git_config" cvs -Q co -d cvswork2 main >cvs.log 2>&1 &&
+ test_cmp cvswork cvswork2 &&
+ test -f "$SERVERDIR/gitcvs1.ext.main.sqlite" &&
+ test ! -f "$SERVERDIR/gitcvs2.ext.main.sqlite" &&
+ cmp "$SERVERDIR/gitcvs.main.sqlite" "$SERVERDIR/gitcvs1.ext.main.sqlite"
+'
+
+
+#------------
+# CVS UPDATE
+#------------
+
+rm -fr "$SERVERDIR"
+cd "$WORKDIR" &&
+git clone -q --bare "$WORKDIR/.git" "$SERVERDIR" >/dev/null 2>&1 &&
+GIT_DIR="$SERVERDIR" git config --bool gitcvs.enabled true &&
+GIT_DIR="$SERVERDIR" git config gitcvs.logfile "$SERVERDIR/gitcvs.log" ||
+exit 1
+
+test_expect_success 'cvs update (create new file)' '
+ echo testfile1 >testfile1 &&
+ git add testfile1 &&
+ git commit -q -m "Add testfile1" &&
+ git push gitcvs.git >/dev/null &&
+ cd cvswork &&
+ GIT_CONFIG="$git_config" cvs -Q update &&
+ test "$(echo $(grep testfile1 CVS/Entries|cut -d/ -f2,3,5))" = "testfile1/1.1/" &&
+ test_cmp testfile1 ../testfile1
+'
+
+cd "$WORKDIR"
+test_expect_success 'cvs update (update existing file)' '
+ echo line 2 >>testfile1 &&
+ git add testfile1 &&
+ git commit -q -m "Append to testfile1" &&
+ git push gitcvs.git >/dev/null &&
+ cd cvswork &&
+ GIT_CONFIG="$git_config" cvs -Q update &&
+ test "$(echo $(grep testfile1 CVS/Entries|cut -d/ -f2,3,5))" = "testfile1/1.2/" &&
+ test_cmp testfile1 ../testfile1
+'
+
+cd "$WORKDIR"
+#TODO: cvsserver doesn't support update w/o -d
+test_expect_failure "cvs update w/o -d doesn't create subdir (TODO)" '
+ mkdir test &&
+ echo >test/empty &&
+ git add test &&
+ git commit -q -m "Single Subdirectory" &&
+ git push gitcvs.git >/dev/null &&
+ cd cvswork &&
+ GIT_CONFIG="$git_config" cvs -Q update &&
+ test ! -d test
+'
+
+cd "$WORKDIR"
+test_expect_success 'cvs update (subdirectories)' '
+ (for dir in A A/B A/B/C A/D E; do
+ mkdir $dir &&
+ echo "test file in $dir" >"$dir/file_in_$(echo $dir|sed -e "s#/# #g")" &&
+ git add $dir || exit 1
+ done) &&
+ git commit -q -m "deep sub directory structure" &&
+ git push gitcvs.git >/dev/null &&
+ cd cvswork &&
+ GIT_CONFIG="$git_config" cvs -Q update -d &&
+ (for dir in A A/B A/B/C A/D E; do
+ filename="file_in_$(echo $dir|sed -e "s#/# #g")" &&
+ if test "$(echo $(grep -v ^D $dir/CVS/Entries|cut -d/ -f2,3,5))" = "$filename/1.1/" &&
+ test_cmp "$dir/$filename" "../$dir/$filename"; then
+ :
+ else
+ exit 1
+ fi
+ done)
+'
+
+cd "$WORKDIR"
+test_expect_success 'cvs update (delete file)' '
+ git rm testfile1 &&
+ git commit -q -m "Remove testfile1" &&
+ git push gitcvs.git >/dev/null &&
+ cd cvswork &&
+ GIT_CONFIG="$git_config" cvs -Q update &&
+ test -z "$(grep testfile1 CVS/Entries)" &&
+ test ! -f testfile1
+'
+
+cd "$WORKDIR"
+test_expect_success 'cvs update (re-add deleted file)' '
+ echo readded testfile >testfile1 &&
+ git add testfile1 &&
+ git commit -q -m "Re-Add testfile1" &&
+ git push gitcvs.git >/dev/null &&
+ cd cvswork &&
+ GIT_CONFIG="$git_config" cvs -Q update &&
+ test "$(echo $(grep testfile1 CVS/Entries|cut -d/ -f2,3,5))" = "testfile1/1.4/" &&
+ test_cmp testfile1 ../testfile1
+'
+
+cd "$WORKDIR"
+test_expect_success 'cvs update (merge)' '
+ echo Line 0 >expected &&
+ for i in 1 2 3 4 5 6 7
+ do
+ echo Line $i >>merge &&
+ echo Line $i >>expected || return 1
+ done &&
+ echo Line 8 >>expected &&
+ git add merge &&
+ git commit -q -m "Merge test (pre-merge)" &&
+ git push gitcvs.git >/dev/null &&
+ cd cvswork &&
+ GIT_CONFIG="$git_config" cvs -Q update &&
+ test "$(echo $(grep merge CVS/Entries|cut -d/ -f2,3,5))" = "merge/1.1/" &&
+ test_cmp merge ../merge &&
+ ( echo Line 0 && cat merge ) >merge.tmp &&
+ mv merge.tmp merge &&
+ cd "$WORKDIR" &&
+ echo Line 8 >>merge &&
+ git add merge &&
+ git commit -q -m "Merge test (merge)" &&
+ git push gitcvs.git >/dev/null &&
+ cd cvswork &&
+ sleep 1 && touch merge &&
+ GIT_CONFIG="$git_config" cvs -Q update &&
+ test_cmp merge ../expected
+'
+
+cd "$WORKDIR"
+
+cat >expected.C <<EOF
+<<<<<<< merge.mine
+Line 0
+=======
+LINE 0
+>>>>>>> merge.1.3
+EOF
+
+for i in 1 2 3 4 5 6 7 8
+do
+ echo Line $i >>expected.C
+done
+
+test_expect_success 'cvs update (conflict merge)' '
+ ( echo LINE 0 && cat merge ) >merge.tmp &&
+ mv merge.tmp merge &&
+ git add merge &&
+ git commit -q -m "Merge test (conflict)" &&
+ git push gitcvs.git >/dev/null &&
+ cd cvswork &&
+ GIT_CONFIG="$git_config" cvs -Q update &&
+ test_cmp merge ../expected.C
+'
+
+cd "$WORKDIR"
+test_expect_success 'cvs update (-C)' '
+ cd cvswork &&
+ GIT_CONFIG="$git_config" cvs -Q update -C &&
+ test_cmp merge ../merge
+'
+
+cd "$WORKDIR"
+test_expect_success 'cvs update (merge no-op)' '
+ echo Line 9 >>merge &&
+ cp merge cvswork/merge &&
+ git add merge &&
+ git commit -q -m "Merge test (no-op)" &&
+ git push gitcvs.git >/dev/null &&
+ cd cvswork &&
+ sleep 1 && touch merge &&
+ GIT_CONFIG="$git_config" cvs -Q update &&
+ test_cmp merge ../merge
+'
+
+cd "$WORKDIR"
+test_expect_success 'cvs update (-p)' '
+ touch really-empty &&
+ echo Line 1 > no-lf &&
+ printf "Line 2" >> no-lf &&
+ git add really-empty no-lf &&
+ git commit -q -m "Update -p test" &&
+ git push gitcvs.git >/dev/null &&
+ cd cvswork &&
+ GIT_CONFIG="$git_config" cvs update &&
+ for i in merge no-lf empty really-empty; do
+ GIT_CONFIG="$git_config" cvs update -p "$i" >$i.out &&
+ test_cmp $i.out ../$i || return 1
+ done
+'
+
+cd "$WORKDIR"
+test_expect_success 'cvs update (module list supports packed refs)' '
+ GIT_DIR="$SERVERDIR" git pack-refs --all &&
+ GIT_CONFIG="$git_config" cvs -n up -d 2> out &&
+ grep "cvs update: New directory \`main'\''" < out
+'
+
+#------------
+# CVS STATUS
+#------------
+
+cd "$WORKDIR"
+test_expect_success 'cvs status' '
+ mkdir status.dir &&
+ echo Line > status.dir/status.file &&
+ echo Line > status.file &&
+ git add status.dir status.file &&
+ git commit -q -m "Status test" &&
+ git push gitcvs.git >/dev/null &&
+ cd cvswork &&
+ GIT_CONFIG="$git_config" cvs update &&
+ GIT_CONFIG="$git_config" cvs status | grep "^File: status.file" >../out &&
+ test_line_count = 2 ../out
+'
+
+cd "$WORKDIR"
+test_expect_success 'cvs status (nonrecursive)' '
+ cd cvswork &&
+ GIT_CONFIG="$git_config" cvs status -l | grep "^File: status.file" >../out &&
+ test_line_count = 1 ../out
+'
+
+cd "$WORKDIR"
+test_expect_success 'cvs status (no subdirs in header)' '
+ cd cvswork &&
+ GIT_CONFIG="$git_config" cvs status | grep ^File: >../out &&
+ ! grep / <../out
+'
+
+#------------
+# CVS CHECKOUT
+#------------
+
+cd "$WORKDIR"
+test_expect_success 'cvs co -c (shows module database)' '
+ GIT_CONFIG="$git_config" cvs co -c > out &&
+ grep "^main[ ][ ]*main$" <out &&
+ ! grep -v "^main[ ][ ]*main$" <out
+'
+
+#------------
+# CVS LOG
+#------------
+
+# Known issues with git-cvsserver current log output:
+# - Hard coded "lines: +2 -3" placeholder, instead of real numbers.
+# - CVS normally does not internally add a blank first line
+# or a last line with nothing but a space to log messages.
+# - The latest cvs 1.12.x server sends +0000 timezone (with some hidden "MT"
+# tagging in the protocol), and if cvs 1.12.x client sees the MT tags,
+# it converts to local time zone. git-cvsserver doesn't do the +0000
+# or the MT tags...
+# - The latest 1.12.x releases add a "commitid:" field on to the end of the
+# "date:" line (after "lines:"). Maybe we could stick git's commit id
+# in it? Or does CVS expect a certain number of bits (too few for
+# a full sha1)?
+#
+# Given the above, expect the following test to break if git-cvsserver's
+# log output is improved. The test is just to ensure it doesn't
+# accidentally get worse.
+
+sed -e 's/^x//' -e 's/SP$/ /' > "$WORKDIR/expect" <<EOF
+x
+xRCS file: $WORKDIR/gitcvs.git/main/merge,v
+xWorking file: merge
+xhead: 1.4
+xbranch:
+xlocks: strict
+xaccess list:
+xsymbolic names:
+xkeyword substitution: kv
+xtotal revisions: 4; selected revisions: 4
+xdescription:
+x----------------------------
+xrevision 1.4
+xdate: __DATE__; author: author; state: Exp; lines: +2 -3
+x
+xMerge test (no-op)
+xSP
+x----------------------------
+xrevision 1.3
+xdate: __DATE__; author: author; state: Exp; lines: +2 -3
+x
+xMerge test (conflict)
+xSP
+x----------------------------
+xrevision 1.2
+xdate: __DATE__; author: author; state: Exp; lines: +2 -3
+x
+xMerge test (merge)
+xSP
+x----------------------------
+xrevision 1.1
+xdate: __DATE__; author: author; state: Exp; lines: +2 -3
+x
+xMerge test (pre-merge)
+xSP
+x=============================================================================
+EOF
+expectStat="$?"
+
+cd "$WORKDIR"
+test_expect_success 'cvs log' '
+ cd cvswork &&
+ test x"$expectStat" = x"0" &&
+ GIT_CONFIG="$git_config" cvs log merge >../out &&
+sed -e "s%2[0-9][0-9][0-9]/[01][0-9]/[0-3][0-9] [0-2][0-9]:[0-5][0-9]:[0-5][0-9]%__DATE__%" ../out > ../actual &&
+ test_cmp ../expect ../actual
+'
+
+#------------
+# CVS ANNOTATE
+#------------
+
+cd "$WORKDIR"
+test_expect_success 'cvs annotate' '
+ cd cvswork &&
+ GIT_CONFIG="$git_config" cvs annotate merge >../out &&
+ sed -e "s/ .*//" ../out >../actual &&
+ printf "1.%d\n" 3 1 1 1 1 1 1 1 2 4 >../expect &&
+ test_cmp ../expect ../actual
+'
+
+#------------
+# running via git-shell
+#------------
+
+cd "$WORKDIR"
+
+test_expect_success 'create remote-cvs helper' '
+ write_script remote-cvs <<-\EOF
+ exec git shell -c "cvs server"
+ EOF
+'
+
+test_expect_success 'cvs server does not run with vanilla git-shell' '
+ (
+ cd cvswork &&
+ CVS_SERVER=$WORKDIR/remote-cvs &&
+ export CVS_SERVER &&
+ ! cvs log merge
+ )
+'
+
+test_expect_success 'configure git shell to run cvs server' '
+ mkdir "$HOME"/git-shell-commands &&
+
+ write_script "$HOME"/git-shell-commands/cvs <<-\EOF &&
+ if ! test $# = 1 && test "$1" = "server"
+ then
+ echo >&2 "git-cvsserver only handles \"server\""
+ exit 1
+ fi
+ exec git cvsserver server
+ EOF
+
+ # Should not be used, but part of the recommended setup
+ write_script "$HOME"/git-shell-commands/no-interactive-login <<-\EOF
+ echo Interactive login forbidden
+ EOF
+'
+
+test_expect_success 'cvs server can run with recommended config' '
+ (
+ cd cvswork &&
+ CVS_SERVER=$WORKDIR/remote-cvs &&
+ export CVS_SERVER &&
+ cvs log merge
+ )
+'
+
+test_done
diff --git a/t/t9401-git-cvsserver-crlf.sh b/t/t9401-git-cvsserver-crlf.sh
new file mode 100755
index 0000000..a34805a
--- /dev/null
+++ b/t/t9401-git-cvsserver-crlf.sh
@@ -0,0 +1,372 @@
+#!/bin/sh
+#
+# Copyright (c) 2008 Matthew Ogilvie
+# Parts adapted from other tests.
+#
+
+test_description='git-cvsserver -kb modes
+
+tests -kb mode for binary files when accessing a git
+repository using cvs CLI client via git-cvsserver server'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+marked_as () {
+ foundEntry="$(grep "^/$2/" "$1/CVS/Entries")"
+ if [ x"$foundEntry" = x"" ] ; then
+ echo "NOT FOUND: $1 $2 1 $3" >> "${WORKDIR}/marked.log"
+ return 1
+ fi
+ test x"$(grep "^/$2/" "$1/CVS/Entries" | cut -d/ -f5)" = x"$3"
+ stat=$?
+ echo "$1 $2 $stat '$3'" >> "${WORKDIR}/marked.log"
+ return $stat
+}
+
+not_present() {
+ foundEntry="$(grep "^/$2/" "$1/CVS/Entries")"
+ if [ -r "$1/$2" ] ; then
+ echo "Error: File still exists: $1 $2" >> "${WORKDIR}/marked.log"
+ return 1;
+ fi
+ if [ x"$foundEntry" != x"" ] ; then
+ echo "Error: should not have found: $1 $2" >> "${WORKDIR}/marked.log"
+ return 1;
+ else
+ echo "Correctly not found: $1 $2" >> "${WORKDIR}/marked.log"
+ return 0;
+ fi
+}
+
+check_status_options() {
+ (cd "$1" &&
+ GIT_CONFIG="$git_config" cvs -Q status "$2" > "${WORKDIR}/status.out" 2>&1
+ )
+ if [ x"$?" != x"0" ] ; then
+ echo "Error from cvs status: $1 $2" >> "${WORKDIR}/marked.log"
+ return 1;
+ fi
+ got="$(sed -n -e 's/^[ ]*Sticky Options:[ ]*//p' "${WORKDIR}/status.out")"
+ expect="$3"
+ if [ x"$expect" = x"" ] ; then
+ expect="(none)"
+ fi
+ test x"$got" = x"$expect"
+ stat=$?
+ echo "cvs status: $1 $2 $stat '$3' '$got'" >> "${WORKDIR}/marked.log"
+ return $stat
+}
+
+cvs >/dev/null 2>&1
+if test $? -ne 1
+then
+ skip_all='skipping git-cvsserver tests, cvs not found'
+ test_done
+fi
+if ! test_have_prereq PERL
+then
+ skip_all='skipping git-cvsserver tests, perl not available'
+ test_done
+fi
+perl -e 'use DBI; use DBD::SQLite' >/dev/null 2>&1 || {
+ skip_all='skipping git-cvsserver tests, Perl SQLite interface unavailable'
+ test_done
+}
+
+unset GIT_DIR GIT_CONFIG
+WORKDIR=$PWD
+SERVERDIR=$PWD/gitcvs.git
+git_config="$SERVERDIR/config"
+CVSROOT=":fork:$SERVERDIR"
+CVSWORK="$PWD/cvswork"
+CVS_SERVER=git-cvsserver
+export CVSROOT CVS_SERVER
+
+rm -rf "$CVSWORK" "$SERVERDIR"
+test_expect_success 'setup' '
+ git config push.default matching &&
+ echo "Simple text file" >textfile.c &&
+ echo "File with embedded NUL: Q <- there" | q_to_nul > binfile.bin &&
+ mkdir subdir &&
+ echo "Another text file" > subdir/file.h &&
+ echo "Another binary: Q (this time CR)" | q_to_cr > subdir/withCr.bin &&
+ echo "Mixed up NUL, but marked text: Q <- there" | q_to_nul > mixedUp.c &&
+ echo "Unspecified" > subdir/unspecified.other &&
+ echo "/*.bin -crlf" > .gitattributes &&
+ echo "/*.c crlf" >> .gitattributes &&
+ echo "subdir/*.bin -crlf" >> .gitattributes &&
+ echo "subdir/*.c crlf" >> .gitattributes &&
+ echo "subdir/file.h crlf" >> .gitattributes &&
+ git add .gitattributes textfile.c binfile.bin mixedUp.c subdir/* &&
+ git commit -q -m "First Commit" &&
+ git clone -q --bare "$WORKDIR/.git" "$SERVERDIR" >/dev/null 2>&1 &&
+ GIT_DIR="$SERVERDIR" git config --bool gitcvs.enabled true &&
+ GIT_DIR="$SERVERDIR" git config gitcvs.logfile "$SERVERDIR/gitcvs.log"
+'
+
+test_expect_success 'cvs co (default crlf)' '
+ GIT_CONFIG="$git_config" cvs -Q co -d cvswork main >cvs.log 2>&1 &&
+ test x"$(grep '/-k' cvswork/CVS/Entries cvswork/subdir/CVS/Entries)" = x""
+'
+
+rm -rf cvswork
+test_expect_success 'cvs co (allbinary)' '
+ GIT_DIR="$SERVERDIR" git config --bool gitcvs.allbinary true &&
+ GIT_CONFIG="$git_config" cvs -Q co -d cvswork main >cvs.log 2>&1 &&
+ marked_as cvswork textfile.c -kb &&
+ marked_as cvswork binfile.bin -kb &&
+ marked_as cvswork .gitattributes -kb &&
+ marked_as cvswork mixedUp.c -kb &&
+ marked_as cvswork/subdir withCr.bin -kb &&
+ marked_as cvswork/subdir file.h -kb &&
+ marked_as cvswork/subdir unspecified.other -kb
+'
+
+rm -rf cvswork cvs.log
+test_expect_success 'cvs co (use attributes/allbinary)' '
+ GIT_DIR="$SERVERDIR" git config --bool gitcvs.usecrlfattr true &&
+ GIT_CONFIG="$git_config" cvs -Q co -d cvswork main >cvs.log 2>&1 &&
+ marked_as cvswork textfile.c "" &&
+ marked_as cvswork binfile.bin -kb &&
+ marked_as cvswork .gitattributes -kb &&
+ marked_as cvswork mixedUp.c "" &&
+ marked_as cvswork/subdir withCr.bin -kb &&
+ marked_as cvswork/subdir file.h "" &&
+ marked_as cvswork/subdir unspecified.other -kb
+'
+
+rm -rf cvswork
+test_expect_success 'cvs co (use attributes)' '
+ GIT_DIR="$SERVERDIR" git config --bool gitcvs.allbinary false &&
+ GIT_CONFIG="$git_config" cvs -Q co -d cvswork main >cvs.log 2>&1 &&
+ marked_as cvswork textfile.c "" &&
+ marked_as cvswork binfile.bin -kb &&
+ marked_as cvswork .gitattributes "" &&
+ marked_as cvswork mixedUp.c "" &&
+ marked_as cvswork/subdir withCr.bin -kb &&
+ marked_as cvswork/subdir file.h "" &&
+ marked_as cvswork/subdir unspecified.other ""
+'
+
+test_expect_success 'adding files' '
+ (cd cvswork &&
+ (cd subdir &&
+ echo "more text" > src.c &&
+ GIT_CONFIG="$git_config" cvs -Q add src.c >cvs.log 2>&1 &&
+ marked_as . src.c "" &&
+ echo "pseudo-binary" > temp.bin
+ ) &&
+ GIT_CONFIG="$git_config" cvs -Q add subdir/temp.bin >cvs.log 2>&1 &&
+ marked_as subdir temp.bin "-kb" &&
+ cd subdir &&
+ GIT_CONFIG="$git_config" cvs -Q ci -m "adding files" >cvs.log 2>&1 &&
+ marked_as . temp.bin "-kb" &&
+ marked_as . src.c ""
+ )
+'
+
+test_expect_success 'updating' '
+ git pull gitcvs.git &&
+ echo "hi" >subdir/newfile.bin &&
+ echo "junk" >subdir/file.h &&
+ echo "hi" >subdir/newfile.c &&
+ echo "hello" >>binfile.bin &&
+ git add subdir/newfile.bin subdir/file.h subdir/newfile.c binfile.bin &&
+ git commit -q -m "Add and change some files" &&
+ git push gitcvs.git >/dev/null &&
+ (cd cvswork &&
+ GIT_CONFIG="$git_config" cvs -Q update
+ ) &&
+ marked_as cvswork textfile.c "" &&
+ marked_as cvswork binfile.bin -kb &&
+ marked_as cvswork .gitattributes "" &&
+ marked_as cvswork mixedUp.c "" &&
+ marked_as cvswork/subdir withCr.bin -kb &&
+ marked_as cvswork/subdir file.h "" &&
+ marked_as cvswork/subdir unspecified.other "" &&
+ marked_as cvswork/subdir newfile.bin -kb &&
+ marked_as cvswork/subdir newfile.c "" &&
+ echo "File with embedded NUL: Q <- there" | q_to_nul > tmpExpect1 &&
+ echo "hello" >> tmpExpect1 &&
+ cmp cvswork/binfile.bin tmpExpect1
+'
+
+rm -rf cvswork
+test_expect_success 'cvs co (use attributes/guess)' '
+ GIT_DIR="$SERVERDIR" git config gitcvs.allbinary guess &&
+ GIT_CONFIG="$git_config" cvs -Q co -d cvswork main >cvs.log 2>&1 &&
+ marked_as cvswork textfile.c "" &&
+ marked_as cvswork binfile.bin -kb &&
+ marked_as cvswork .gitattributes "" &&
+ marked_as cvswork mixedUp.c "" &&
+ marked_as cvswork/subdir withCr.bin -kb &&
+ marked_as cvswork/subdir file.h "" &&
+ marked_as cvswork/subdir unspecified.other "" &&
+ marked_as cvswork/subdir newfile.bin -kb &&
+ marked_as cvswork/subdir newfile.c ""
+'
+
+test_expect_success 'setup multi-line files' '
+ ( echo "line 1" &&
+ echo "line 2" &&
+ echo "line 3" &&
+ echo "line 4 with NUL: Q <-" ) | q_to_nul > multiline.c &&
+ git add multiline.c &&
+ ( echo "line 1" &&
+ echo "line 2" &&
+ echo "line 3" &&
+ echo "line 4" ) | q_to_nul > multilineTxt.c &&
+ git add multilineTxt.c &&
+ git commit -q -m "multiline files" &&
+ git push gitcvs.git >/dev/null
+'
+
+rm -rf cvswork
+test_expect_success 'cvs co (guess)' '
+ GIT_DIR="$SERVERDIR" git config --bool gitcvs.usecrlfattr false &&
+ GIT_CONFIG="$git_config" cvs -Q co -d cvswork main >cvs.log 2>&1 &&
+ marked_as cvswork textfile.c "" &&
+ marked_as cvswork binfile.bin -kb &&
+ marked_as cvswork .gitattributes "" &&
+ marked_as cvswork mixedUp.c -kb &&
+ marked_as cvswork multiline.c -kb &&
+ marked_as cvswork multilineTxt.c "" &&
+ marked_as cvswork/subdir withCr.bin -kb &&
+ marked_as cvswork/subdir file.h "" &&
+ marked_as cvswork/subdir unspecified.other "" &&
+ marked_as cvswork/subdir newfile.bin "" &&
+ marked_as cvswork/subdir newfile.c ""
+'
+
+test_expect_success 'cvs co another copy (guess)' '
+ GIT_CONFIG="$git_config" cvs -Q co -d cvswork2 main >cvs.log 2>&1 &&
+ marked_as cvswork2 textfile.c "" &&
+ marked_as cvswork2 binfile.bin -kb &&
+ marked_as cvswork2 .gitattributes "" &&
+ marked_as cvswork2 mixedUp.c -kb &&
+ marked_as cvswork2 multiline.c -kb &&
+ marked_as cvswork2 multilineTxt.c "" &&
+ marked_as cvswork2/subdir withCr.bin -kb &&
+ marked_as cvswork2/subdir file.h "" &&
+ marked_as cvswork2/subdir unspecified.other "" &&
+ marked_as cvswork2/subdir newfile.bin "" &&
+ marked_as cvswork2/subdir newfile.c ""
+'
+
+test_expect_success 'cvs status - sticky options' '
+ check_status_options cvswork2 textfile.c "" &&
+ check_status_options cvswork2 binfile.bin -kb &&
+ check_status_options cvswork2 .gitattributes "" &&
+ check_status_options cvswork2 mixedUp.c -kb &&
+ check_status_options cvswork2 multiline.c -kb &&
+ check_status_options cvswork2 multilineTxt.c "" &&
+ check_status_options cvswork2/subdir withCr.bin -kb &&
+ check_status_options cvswork2 subdir/withCr.bin -kb &&
+ check_status_options cvswork2/subdir file.h "" &&
+ check_status_options cvswork2 subdir/file.h "" &&
+ check_status_options cvswork2/subdir unspecified.other "" &&
+ check_status_options cvswork2/subdir newfile.bin "" &&
+ check_status_options cvswork2/subdir newfile.c ""
+'
+
+test_expect_success 'add text (guess)' '
+ (cd cvswork &&
+ echo "simpleText" > simpleText.c &&
+ GIT_CONFIG="$git_config" cvs -Q add simpleText.c
+ ) &&
+ marked_as cvswork simpleText.c ""
+'
+
+test_expect_success 'add bin (guess)' '
+ (cd cvswork &&
+ echo "simpleBin: NUL: Q <- there" | q_to_nul > simpleBin.bin &&
+ GIT_CONFIG="$git_config" cvs -Q add simpleBin.bin
+ ) &&
+ marked_as cvswork simpleBin.bin -kb
+'
+
+test_expect_success 'remove files (guess)' '
+ (cd cvswork &&
+ GIT_CONFIG="$git_config" cvs -Q rm -f subdir/file.h &&
+ (cd subdir &&
+ GIT_CONFIG="$git_config" cvs -Q rm -f withCr.bin
+ )) &&
+ marked_as cvswork/subdir withCr.bin -kb &&
+ marked_as cvswork/subdir file.h ""
+'
+
+test_expect_success 'cvs ci (guess)' '
+ (cd cvswork &&
+ GIT_CONFIG="$git_config" cvs -Q ci -m "add/rm files" >cvs.log 2>&1
+ ) &&
+ marked_as cvswork textfile.c "" &&
+ marked_as cvswork binfile.bin -kb &&
+ marked_as cvswork .gitattributes "" &&
+ marked_as cvswork mixedUp.c -kb &&
+ marked_as cvswork multiline.c -kb &&
+ marked_as cvswork multilineTxt.c "" &&
+ not_present cvswork/subdir withCr.bin &&
+ not_present cvswork/subdir file.h &&
+ marked_as cvswork/subdir unspecified.other "" &&
+ marked_as cvswork/subdir newfile.bin "" &&
+ marked_as cvswork/subdir newfile.c "" &&
+ marked_as cvswork simpleBin.bin -kb &&
+ marked_as cvswork simpleText.c ""
+'
+
+test_expect_success 'update subdir of other copy (guess)' '
+ (cd cvswork2/subdir &&
+ GIT_CONFIG="$git_config" cvs -Q update
+ ) &&
+ marked_as cvswork2 textfile.c "" &&
+ marked_as cvswork2 binfile.bin -kb &&
+ marked_as cvswork2 .gitattributes "" &&
+ marked_as cvswork2 mixedUp.c -kb &&
+ marked_as cvswork2 multiline.c -kb &&
+ marked_as cvswork2 multilineTxt.c "" &&
+ not_present cvswork2/subdir withCr.bin &&
+ not_present cvswork2/subdir file.h &&
+ marked_as cvswork2/subdir unspecified.other "" &&
+ marked_as cvswork2/subdir newfile.bin "" &&
+ marked_as cvswork2/subdir newfile.c "" &&
+ not_present cvswork2 simpleBin.bin &&
+ not_present cvswork2 simpleText.c
+'
+
+echo "starting update/merge" >> "${WORKDIR}/marked.log"
+test_expect_success 'update/merge full other copy (guess)' '
+ git pull gitcvs.git main &&
+ sed "s/3/replaced_3/" < multilineTxt.c > ml.temp &&
+ mv ml.temp multilineTxt.c &&
+ git add multilineTxt.c &&
+ git commit -q -m "modify multiline file" >> "${WORKDIR}/marked.log" &&
+ git push gitcvs.git >/dev/null &&
+ (cd cvswork2 &&
+ sed "s/1/replaced_1/" < multilineTxt.c > ml.temp &&
+ mv ml.temp multilineTxt.c &&
+ GIT_CONFIG="$git_config" cvs update > cvs.log 2>&1
+ ) &&
+ marked_as cvswork2 textfile.c "" &&
+ marked_as cvswork2 binfile.bin -kb &&
+ marked_as cvswork2 .gitattributes "" &&
+ marked_as cvswork2 mixedUp.c -kb &&
+ marked_as cvswork2 multiline.c -kb &&
+ marked_as cvswork2 multilineTxt.c "" &&
+ not_present cvswork2/subdir withCr.bin &&
+ not_present cvswork2/subdir file.h &&
+ marked_as cvswork2/subdir unspecified.other "" &&
+ marked_as cvswork2/subdir newfile.bin "" &&
+ marked_as cvswork2/subdir newfile.c "" &&
+ marked_as cvswork2 simpleBin.bin -kb &&
+ marked_as cvswork2 simpleText.c "" &&
+ echo "line replaced_1" > tmpExpect2 &&
+ echo "line 2" >> tmpExpect2 &&
+ echo "line replaced_3" >> tmpExpect2 &&
+ echo "line 4" | q_to_nul >> tmpExpect2 &&
+ cmp cvswork2/multilineTxt.c tmpExpect2
+'
+
+test_done
diff --git a/t/t9402-git-cvsserver-refs.sh b/t/t9402-git-cvsserver-refs.sh
new file mode 100755
index 0000000..2ee41f9
--- /dev/null
+++ b/t/t9402-git-cvsserver-refs.sh
@@ -0,0 +1,554 @@
+#!/bin/sh
+
+test_description='git-cvsserver and git refspecs
+
+tests ability for git-cvsserver to switch between and compare
+tags, branches and other git refspecs'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./test-lib.sh
+
+#########
+
+check_start_tree() {
+ rm -f "$WORKDIR/list.expected"
+ echo "start $1" >>"${WORKDIR}/check.log"
+}
+
+check_file() {
+ sandbox="$1"
+ file="$2"
+ ver="$3"
+ GIT_DIR=$SERVERDIR git show "${ver}:${file}" \
+ >"$WORKDIR/check.got" 2>"$WORKDIR/check.stderr"
+ test_cmp "$WORKDIR/check.got" "$sandbox/$file"
+ stat=$?
+ echo "check_file $sandbox $file $ver : $stat" >>"$WORKDIR/check.log"
+ echo "$file" >>"$WORKDIR/list.expected"
+ return $stat
+}
+
+check_end_tree() {
+ sandbox="$1" &&
+ find "$sandbox" -name CVS -prune -o -type f -print >"$WORKDIR/list.actual" &&
+ sort <"$WORKDIR/list.expected" >expected &&
+ sort <"$WORKDIR/list.actual" | sed -e "s%cvswork/%%" >actual &&
+ test_cmp expected actual &&
+ rm expected actual
+}
+
+check_end_full_tree() {
+ sandbox="$1" &&
+ sort <"$WORKDIR/list.expected" >expected &&
+ find "$sandbox" -name CVS -prune -o -type f -print |
+ sed -e "s%$sandbox/%%" | sort >act1 &&
+ test_cmp expected act1 &&
+ git ls-tree --name-only -r "$2" | sort >act2 &&
+ test_cmp expected act2 &&
+ rm expected act1 act2
+}
+
+#########
+
+check_diff() {
+ diffFile="$1"
+ vOld="$2"
+ vNew="$3"
+ rm -rf diffSandbox
+ git clone -q -n . diffSandbox &&
+ (
+ cd diffSandbox &&
+ git checkout "$vOld" &&
+ git apply -p0 --index <"../$diffFile" &&
+ git diff --exit-code "$vNew"
+ ) >check_diff_apply.out 2>&1
+}
+
+#########
+
+cvs >/dev/null 2>&1
+if test $? -ne 1
+then
+ skip_all='skipping git-cvsserver tests, cvs not found'
+ test_done
+fi
+if ! test_have_prereq PERL
+then
+ skip_all='skipping git-cvsserver tests, perl not available'
+ test_done
+fi
+perl -e 'use DBI; use DBD::SQLite' >/dev/null 2>&1 || {
+ skip_all='skipping git-cvsserver tests, Perl SQLite interface unavailable'
+ test_done
+}
+
+unset GIT_DIR GIT_CONFIG
+WORKDIR=$PWD
+SERVERDIR=$PWD/gitcvs.git
+git_config="$SERVERDIR/config"
+CVSROOT=":fork:$SERVERDIR"
+CVSWORK="$PWD/cvswork"
+CVS_SERVER=git-cvsserver
+export CVSROOT CVS_SERVER
+
+rm -rf "$CVSWORK" "$SERVERDIR"
+test_expect_success 'setup v1, b1' '
+ echo "Simple text file" >textfile.c &&
+ echo "t2" >t2 &&
+ mkdir adir &&
+ echo "adir/afile line1" >adir/afile &&
+ echo "adir/afile line2" >>adir/afile &&
+ echo "adir/afile line3" >>adir/afile &&
+ echo "adir/afile line4" >>adir/afile &&
+ echo "adir/a2file" >>adir/a2file &&
+ mkdir adir/bdir &&
+ echo "adir/bdir/bfile line 1" >adir/bdir/bfile &&
+ echo "adir/bdir/bfile line 2" >>adir/bdir/bfile &&
+ echo "adir/bdir/b2file" >adir/bdir/b2file &&
+ git add textfile.c t2 adir &&
+ git commit -q -m "First Commit (v1)" &&
+ git tag v1 &&
+ git branch b1 &&
+ git clone -q --bare "$WORKDIR/.git" "$SERVERDIR" >/dev/null 2>&1 &&
+ GIT_DIR="$SERVERDIR" git config --bool gitcvs.enabled true &&
+ GIT_DIR="$SERVERDIR" git config gitcvs.logfile "$SERVERDIR/gitcvs.log"
+'
+
+rm -rf cvswork
+test_expect_success 'cvs co v1' '
+ cvs -f -Q co -r v1 -d cvswork main >cvs.log 2>&1 &&
+ check_start_tree cvswork &&
+ check_file cvswork textfile.c v1 &&
+ check_file cvswork t2 v1 &&
+ check_file cvswork adir/afile v1 &&
+ check_file cvswork adir/a2file v1 &&
+ check_file cvswork adir/bdir/bfile v1 &&
+ check_file cvswork adir/bdir/b2file v1 &&
+ check_end_tree cvswork
+'
+
+rm -rf cvswork
+test_expect_success 'cvs co b1' '
+ cvs -f co -r b1 -d cvswork main >cvs.log 2>&1 &&
+ check_start_tree cvswork &&
+ check_file cvswork textfile.c v1 &&
+ check_file cvswork t2 v1 &&
+ check_file cvswork adir/afile v1 &&
+ check_file cvswork adir/a2file v1 &&
+ check_file cvswork adir/bdir/bfile v1 &&
+ check_file cvswork adir/bdir/b2file v1 &&
+ check_end_tree cvswork
+'
+
+test_expect_success 'cvs co b1 [cvswork3]' '
+ cvs -f co -r b1 -d cvswork3 main >cvs.log 2>&1 &&
+ check_start_tree cvswork3 &&
+ check_file cvswork3 textfile.c v1 &&
+ check_file cvswork3 t2 v1 &&
+ check_file cvswork3 adir/afile v1 &&
+ check_file cvswork3 adir/a2file v1 &&
+ check_file cvswork3 adir/bdir/bfile v1 &&
+ check_file cvswork3 adir/bdir/b2file v1 &&
+ check_end_full_tree cvswork3 v1
+'
+
+test_expect_success 'edit cvswork3 and save diff' '
+ (
+ cd cvswork3 &&
+ sed -e "s/line1/line1 - data/" adir/afile >adir/afileNEW &&
+ mv -f adir/afileNEW adir/afile &&
+ echo "afile5" >adir/afile5 &&
+ rm t2 &&
+ cvs -f add adir/afile5 &&
+ cvs -f rm t2 &&
+ ! cvs -f diff -N -u >"$WORKDIR/cvswork3edit.diff"
+ )
+'
+
+test_expect_success 'setup v1.2 on b1' '
+ git checkout b1 &&
+ echo "new v1.2" >t3 &&
+ rm t2 &&
+ sed -e "s/line3/line3 - more data/" adir/afile >adir/afileNEW &&
+ mv -f adir/afileNEW adir/afile &&
+ rm adir/a2file &&
+ echo "a3file" >>adir/a3file &&
+ echo "bfile line 3" >>adir/bdir/bfile &&
+ rm adir/bdir/b2file &&
+ echo "b3file" >adir/bdir/b3file &&
+ mkdir cdir &&
+ echo "cdir/cfile" >cdir/cfile &&
+ git add -A cdir adir t3 t2 &&
+ git commit -q -m "v1.2" &&
+ git tag v1.2 &&
+ git push --tags gitcvs.git b1:b1
+'
+
+test_expect_success 'cvs -f up (on b1 adir)' '
+ ( cd cvswork/adir && cvs -f up -d ) >cvs.log 2>&1 &&
+ check_start_tree cvswork &&
+ check_file cvswork textfile.c v1 &&
+ check_file cvswork t2 v1 &&
+ check_file cvswork adir/afile v1.2 &&
+ check_file cvswork adir/a3file v1.2 &&
+ check_file cvswork adir/bdir/bfile v1.2 &&
+ check_file cvswork adir/bdir/b3file v1.2 &&
+ check_end_tree cvswork
+'
+
+test_expect_success 'cvs up (on b1 /)' '
+ ( cd cvswork && cvs -f up -d ) >cvs.log 2>&1 &&
+ check_start_tree cvswork &&
+ check_file cvswork textfile.c v1.2 &&
+ check_file cvswork t3 v1.2 &&
+ check_file cvswork adir/afile v1.2 &&
+ check_file cvswork adir/a3file v1.2 &&
+ check_file cvswork adir/bdir/bfile v1.2 &&
+ check_file cvswork adir/bdir/b3file v1.2 &&
+ check_file cvswork cdir/cfile v1.2 &&
+ check_end_tree cvswork
+'
+
+# Make sure "CVS/Tag" files didn't get messed up:
+test_expect_success 'cvs up (on b1 /) (again; check CVS/Tag files)' '
+ ( cd cvswork && cvs -f up -d ) >cvs.log 2>&1 &&
+ check_start_tree cvswork &&
+ check_file cvswork textfile.c v1.2 &&
+ check_file cvswork t3 v1.2 &&
+ check_file cvswork adir/afile v1.2 &&
+ check_file cvswork adir/a3file v1.2 &&
+ check_file cvswork adir/bdir/bfile v1.2 &&
+ check_file cvswork adir/bdir/b3file v1.2 &&
+ check_file cvswork cdir/cfile v1.2 &&
+ check_end_tree cvswork
+'
+
+# update to another version:
+test_expect_success 'cvs up -r v1' '
+ ( cd cvswork && cvs -f up -r v1 ) >cvs.log 2>&1 &&
+ check_start_tree cvswork &&
+ check_file cvswork textfile.c v1 &&
+ check_file cvswork t2 v1 &&
+ check_file cvswork adir/afile v1 &&
+ check_file cvswork adir/a2file v1 &&
+ check_file cvswork adir/bdir/bfile v1 &&
+ check_file cvswork adir/bdir/b2file v1 &&
+ check_end_tree cvswork
+'
+
+test_expect_success 'cvs up' '
+ ( cd cvswork && cvs -f up ) >cvs.log 2>&1 &&
+ check_start_tree cvswork &&
+ check_file cvswork textfile.c v1 &&
+ check_file cvswork t2 v1 &&
+ check_file cvswork adir/afile v1 &&
+ check_file cvswork adir/a2file v1 &&
+ check_file cvswork adir/bdir/bfile v1 &&
+ check_file cvswork adir/bdir/b2file v1 &&
+ check_end_tree cvswork
+'
+
+test_expect_success 'cvs up (again; check CVS/Tag files)' '
+ ( cd cvswork && cvs -f up -d ) >cvs.log 2>&1 &&
+ check_start_tree cvswork &&
+ check_file cvswork textfile.c v1 &&
+ check_file cvswork t2 v1 &&
+ check_file cvswork adir/afile v1 &&
+ check_file cvswork adir/a2file v1 &&
+ check_file cvswork adir/bdir/bfile v1 &&
+ check_file cvswork adir/bdir/b2file v1 &&
+ check_end_tree cvswork
+'
+
+test_expect_success 'setup simple b2' '
+ git branch b2 v1 &&
+ git push --tags gitcvs.git b2:b2
+'
+
+test_expect_success 'cvs co b2 [into cvswork2]' '
+ cvs -f co -r b2 -d cvswork2 main >cvs.log 2>&1 &&
+ check_start_tree cvswork &&
+ check_file cvswork textfile.c v1 &&
+ check_file cvswork t2 v1 &&
+ check_file cvswork adir/afile v1 &&
+ check_file cvswork adir/a2file v1 &&
+ check_file cvswork adir/bdir/bfile v1 &&
+ check_file cvswork adir/bdir/b2file v1 &&
+ check_end_tree cvswork
+'
+
+test_expect_success 'root dir edit [cvswork2]' '
+ (
+ cd cvswork2 && echo "Line 2" >>textfile.c &&
+ ! cvs -f diff -u >"$WORKDIR/cvsEdit1.diff" &&
+ cvs -f commit -m "edit textfile.c" textfile.c
+ ) >cvsEdit1.log 2>&1
+'
+
+test_expect_success 'root dir rm file [cvswork2]' '
+ (
+ cd cvswork2 &&
+ cvs -f rm -f t2 &&
+ cvs -f diff -u >../cvsEdit2-empty.diff &&
+ ! cvs -f diff -N -u >"$WORKDIR/cvsEdit2-N.diff" &&
+ cvs -f commit -m "rm t2"
+ ) >cvsEdit2.log 2>&1
+'
+
+test_expect_success 'subdir edit/add/rm files [cvswork2]' '
+ (
+ cd cvswork2 &&
+ sed -e "s/line 1/line 1 (v2)/" adir/bdir/bfile >adir/bdir/bfileNEW &&
+ mv -f adir/bdir/bfileNEW adir/bdir/bfile &&
+ rm adir/bdir/b2file &&
+ cd adir &&
+ cvs -f rm bdir/b2file &&
+ echo "4th file" >bdir/b4file &&
+ cvs -f add bdir/b4file &&
+ ! cvs -f diff -N -u >"$WORKDIR/cvsEdit3.diff" &&
+ git fetch gitcvs.git b2:b2 &&
+ (
+ cd .. &&
+ ! cvs -f diff -u -N -r v1.2 >"$WORKDIR/cvsEdit3-v1.2.diff" &&
+ ! cvs -f diff -u -N -r v1.2 -r v1 >"$WORKDIR/cvsEdit3-v1.2-v1.diff"
+ ) &&
+ cvs -f commit -m "various add/rm/edit"
+ ) >cvs.log 2>&1
+'
+
+test_expect_success 'validate result of edits [cvswork2]' '
+ git fetch gitcvs.git b2:b2 &&
+ git tag v2 b2 &&
+ git push --tags gitcvs.git b2:b2 &&
+ check_start_tree cvswork2 &&
+ check_file cvswork2 textfile.c v2 &&
+ check_file cvswork2 adir/afile v2 &&
+ check_file cvswork2 adir/a2file v2 &&
+ check_file cvswork2 adir/bdir/bfile v2 &&
+ check_file cvswork2 adir/bdir/b4file v2 &&
+ check_end_full_tree cvswork2 v2
+'
+
+test_expect_success 'validate basic diffs saved during above cvswork2 edits' '
+ test $(grep Index: cvsEdit1.diff | wc -l) = 1 &&
+ test_must_be_empty cvsEdit2-empty.diff &&
+ test $(grep Index: cvsEdit2-N.diff | wc -l) = 1 &&
+ test $(grep Index: cvsEdit3.diff | wc -l) = 3 &&
+ rm -rf diffSandbox &&
+ git clone -q -n . diffSandbox &&
+ (
+ cd diffSandbox &&
+ git checkout v1 &&
+ git apply -p0 --index <"$WORKDIR/cvsEdit1.diff" &&
+ git apply -p0 --index <"$WORKDIR/cvsEdit2-N.diff" &&
+ git apply -p0 --directory=adir --index <"$WORKDIR/cvsEdit3.diff" &&
+ git diff --exit-code v2
+ ) >"check_diff_apply.out" 2>&1
+'
+
+test_expect_success 'validate v1.2 diff saved during last cvswork2 edit' '
+ test $(grep Index: cvsEdit3-v1.2.diff | wc -l) = 9 &&
+ check_diff cvsEdit3-v1.2.diff v1.2 v2
+'
+
+test_expect_success 'validate v1.2 v1 diff saved during last cvswork2 edit' '
+ test $(grep Index: cvsEdit3-v1.2-v1.diff | wc -l) = 9 &&
+ check_diff cvsEdit3-v1.2-v1.diff v1.2 v1
+'
+
+test_expect_success 'cvs up [cvswork2]' '
+ ( cd cvswork2 && cvs -f up ) >cvs.log 2>&1 &&
+ check_start_tree cvswork2 &&
+ check_file cvswork2 textfile.c v2 &&
+ check_file cvswork2 adir/afile v2 &&
+ check_file cvswork2 adir/a2file v2 &&
+ check_file cvswork2 adir/bdir/bfile v2 &&
+ check_file cvswork2 adir/bdir/b4file v2 &&
+ check_end_full_tree cvswork2 v2
+'
+
+test_expect_success 'cvs up -r b2 [back to cvswork]' '
+ ( cd cvswork && cvs -f up -r b2 ) >cvs.log 2>&1 &&
+ check_start_tree cvswork &&
+ check_file cvswork textfile.c v2 &&
+ check_file cvswork adir/afile v2 &&
+ check_file cvswork adir/a2file v2 &&
+ check_file cvswork adir/bdir/bfile v2 &&
+ check_file cvswork adir/bdir/b4file v2 &&
+ check_end_full_tree cvswork v2
+'
+
+test_expect_success 'cvs up -r b1' '
+ ( cd cvswork && cvs -f up -r b1 ) >cvs.log 2>&1 &&
+ check_start_tree cvswork &&
+ check_file cvswork textfile.c v1.2 &&
+ check_file cvswork t3 v1.2 &&
+ check_file cvswork adir/afile v1.2 &&
+ check_file cvswork adir/a3file v1.2 &&
+ check_file cvswork adir/bdir/bfile v1.2 &&
+ check_file cvswork adir/bdir/b3file v1.2 &&
+ check_file cvswork cdir/cfile v1.2 &&
+ check_end_full_tree cvswork v1.2
+'
+
+test_expect_success 'cvs up -A' '
+ ( cd cvswork && cvs -f up -A ) >cvs.log 2>&1 &&
+ check_start_tree cvswork &&
+ check_file cvswork textfile.c v1 &&
+ check_file cvswork t2 v1 &&
+ check_file cvswork adir/afile v1 &&
+ check_file cvswork adir/a2file v1 &&
+ check_file cvswork adir/bdir/bfile v1 &&
+ check_file cvswork adir/bdir/b2file v1 &&
+ check_end_full_tree cvswork v1
+'
+
+test_expect_success 'cvs up (check CVS/Tag files)' '
+ ( cd cvswork && cvs -f up ) >cvs.log 2>&1 &&
+ check_start_tree cvswork &&
+ check_file cvswork textfile.c v1 &&
+ check_file cvswork t2 v1 &&
+ check_file cvswork adir/afile v1 &&
+ check_file cvswork adir/a2file v1 &&
+ check_file cvswork adir/bdir/bfile v1 &&
+ check_file cvswork adir/bdir/b2file v1 &&
+ check_end_full_tree cvswork v1
+'
+
+# This is not really legal CVS, but it seems to work anyway:
+test_expect_success 'cvs up -r heads/b1' '
+ ( cd cvswork && cvs -f up -r heads/b1 ) >cvs.log 2>&1 &&
+ check_start_tree cvswork &&
+ check_file cvswork textfile.c v1.2 &&
+ check_file cvswork t3 v1.2 &&
+ check_file cvswork adir/afile v1.2 &&
+ check_file cvswork adir/a3file v1.2 &&
+ check_file cvswork adir/bdir/bfile v1.2 &&
+ check_file cvswork adir/bdir/b3file v1.2 &&
+ check_file cvswork cdir/cfile v1.2 &&
+ check_end_full_tree cvswork v1.2
+'
+
+# But this should work even if CVS client checks -r more carefully:
+test_expect_success 'cvs up -r heads_-s-b2 (cvsserver escape mechanism)' '
+ ( cd cvswork && cvs -f up -r heads_-s-b2 ) >cvs.log 2>&1 &&
+ check_start_tree cvswork &&
+ check_file cvswork textfile.c v2 &&
+ check_file cvswork adir/afile v2 &&
+ check_file cvswork adir/a2file v2 &&
+ check_file cvswork adir/bdir/bfile v2 &&
+ check_file cvswork adir/bdir/b4file v2 &&
+ check_end_full_tree cvswork v2
+'
+
+v1hash=$(git rev-parse v1)
+test_expect_success 'cvs up -r $(git rev-parse v1)' '
+ test -n "$v1hash" &&
+ ( cd cvswork && cvs -f up -r "$v1hash" ) >cvs.log 2>&1 &&
+ check_start_tree cvswork &&
+ check_file cvswork textfile.c v1 &&
+ check_file cvswork t2 v1 &&
+ check_file cvswork adir/afile v1 &&
+ check_file cvswork adir/a2file v1 &&
+ check_file cvswork adir/bdir/bfile v1 &&
+ check_file cvswork adir/bdir/b2file v1 &&
+ check_end_full_tree cvswork v1
+'
+
+test_expect_success 'cvs diff -r v1 -u' '
+ ( cd cvswork && cvs -f diff -r v1 -u >../cvsDiff.out 2>../cvs.log ) &&
+ test_must_be_empty cvsDiff.out &&
+ test_must_be_empty cvs.log
+'
+
+test_expect_success 'cvs diff -N -r v2 -u' '
+ ( cd cvswork && ! cvs -f diff -N -r v2 -u >../cvsDiff.out 2>../cvs.log ) &&
+ test_must_be_empty cvs.log &&
+ test -s cvsDiff.out &&
+ check_diff cvsDiff.out v2 v1 >check_diff.out 2>&1
+'
+
+test_expect_success 'cvs diff -N -r v2 -r v1.2' '
+ ( cd cvswork && ! cvs -f diff -N -r v2 -r v1.2 -u >../cvsDiff.out 2>../cvs.log ) &&
+ test_must_be_empty cvs.log &&
+ test -s cvsDiff.out &&
+ check_diff cvsDiff.out v2 v1.2 >check_diff.out 2>&1
+'
+
+test_expect_success 'apply early [cvswork3] diff to b3' '
+ git clone -q . gitwork3 &&
+ (
+ cd gitwork3 &&
+ git checkout -b b3 v1 &&
+ git apply -p0 --index <"$WORKDIR/cvswork3edit.diff" &&
+ git commit -m "cvswork3 edits applied"
+ ) &&
+ git fetch gitwork3 b3:b3 &&
+ git tag v3 b3
+'
+
+test_expect_success 'check [cvswork3] diff' '
+ ( cd cvswork3 && ! cvs -f diff -N -u >"$WORKDIR/cvsDiff.out" 2>../cvs.log ) &&
+ test_must_be_empty cvs.log &&
+ test -s cvsDiff.out &&
+ test $(grep Index: cvsDiff.out | wc -l) = 3 &&
+ test_cmp cvsDiff.out cvswork3edit.diff &&
+ check_diff cvsDiff.out v1 v3 >check_diff.out 2>&1
+'
+
+test_expect_success 'merge early [cvswork3] b3 with b1' '
+ ( cd gitwork3 && git merge -m "message" b1 ) &&
+ git fetch gitwork3 b3:b3 &&
+ git tag v3merged b3 &&
+ git push --tags gitcvs.git b3:b3
+'
+
+# This test would fail if cvsserver properly created a ".#afile"* file
+# for the merge.
+# TODO: Validate that the .# file was saved properly, and then
+# delete/ignore it when checking the tree.
+test_expect_success 'cvs up dirty [cvswork3]' '
+ (
+ cd cvswork3 &&
+ cvs -f up &&
+ ! cvs -f diff -N -u >"$WORKDIR/cvsDiff.out"
+ ) >cvs.log 2>&1 &&
+ test -s cvsDiff.out &&
+ test $(grep Index: cvsDiff.out | wc -l) = 2 &&
+ check_start_tree cvswork3 &&
+ check_file cvswork3 textfile.c v3merged &&
+ check_file cvswork3 t3 v3merged &&
+ check_file cvswork3 adir/afile v3merged &&
+ check_file cvswork3 adir/a3file v3merged &&
+ check_file cvswork3 adir/afile5 v3merged &&
+ check_file cvswork3 adir/bdir/bfile v3merged &&
+ check_file cvswork3 adir/bdir/b3file v3merged &&
+ check_file cvswork3 cdir/cfile v3merged &&
+ check_end_full_tree cvswork3 v3merged
+'
+
+# TODO: test cvs status
+
+test_expect_success 'cvs commit [cvswork3]' '
+ (
+ cd cvswork3 &&
+ cvs -f commit -m "dirty sandbox after auto-merge"
+ ) >cvs.log 2>&1 &&
+ check_start_tree cvswork3 &&
+ check_file cvswork3 textfile.c v3merged &&
+ check_file cvswork3 t3 v3merged &&
+ check_file cvswork3 adir/afile v3merged &&
+ check_file cvswork3 adir/a3file v3merged &&
+ check_file cvswork3 adir/afile5 v3merged &&
+ check_file cvswork3 adir/bdir/bfile v3merged &&
+ check_file cvswork3 adir/bdir/b3file v3merged &&
+ check_file cvswork3 cdir/cfile v3merged &&
+ check_end_full_tree cvswork3 v3merged &&
+ git fetch gitcvs.git b3:b4 &&
+ git tag v4.1 b4 &&
+ git diff --exit-code v4.1 v3merged >check_diff_apply.out 2>&1
+'
+
+test_done
diff --git a/t/t9500-gitweb-standalone-no-errors.sh b/t/t9500-gitweb-standalone-no-errors.sh
new file mode 100755
index 0000000..0333065
--- /dev/null
+++ b/t/t9500-gitweb-standalone-no-errors.sh
@@ -0,0 +1,810 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Jakub Narebski
+#
+
+test_description='gitweb as standalone script (basic tests).
+
+This test runs gitweb (git web interface) as CGI script from
+commandline, and checks that it would not write any errors
+or warnings to log.'
+
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./lib-gitweb.sh
+
+# ----------------------------------------------------------------------
+# no commits (empty, just initialized repository)
+
+test_expect_success \
+ 'no commits: projects_list (implicit)' \
+ 'gitweb_run'
+
+test_expect_success \
+ 'no commits: projects_index' \
+ 'gitweb_run "a=project_index"'
+
+test_expect_success \
+ 'no commits: .git summary (implicit)' \
+ 'gitweb_run "p=.git"'
+
+test_expect_success \
+ 'no commits: .git commit (implicit HEAD)' \
+ 'gitweb_run "p=.git;a=commit"'
+
+test_expect_success \
+ 'no commits: .git commitdiff (implicit HEAD)' \
+ 'gitweb_run "p=.git;a=commitdiff"'
+
+test_expect_success \
+ 'no commits: .git tree (implicit HEAD)' \
+ 'gitweb_run "p=.git;a=tree"'
+
+test_expect_success \
+ 'no commits: .git heads' \
+ 'gitweb_run "p=.git;a=heads"'
+
+test_expect_success \
+ 'no commits: .git tags' \
+ 'gitweb_run "p=.git;a=tags"'
+
+
+# ----------------------------------------------------------------------
+# initial commit
+
+test_expect_success \
+ 'Make initial commit' \
+ 'echo "Not an empty file." >file &&
+ git add file &&
+ git commit -a -m "Initial commit." &&
+ git branch b'
+
+test_expect_success \
+ 'projects_list (implicit)' \
+ 'gitweb_run'
+
+test_expect_success \
+ 'projects_index' \
+ 'gitweb_run "a=project_index"'
+
+test_expect_success \
+ '.git summary (implicit)' \
+ 'gitweb_run "p=.git"'
+
+test_expect_success \
+ '.git commit (implicit HEAD)' \
+ 'gitweb_run "p=.git;a=commit"'
+
+test_expect_success \
+ '.git commitdiff (implicit HEAD, root commit)' \
+ 'gitweb_run "p=.git;a=commitdiff"'
+
+test_expect_success \
+ '.git commitdiff_plain (implicit HEAD, root commit)' \
+ 'gitweb_run "p=.git;a=commitdiff_plain"'
+
+test_expect_success \
+ '.git commit (HEAD)' \
+ 'gitweb_run "p=.git;a=commit;h=HEAD"'
+
+test_expect_success \
+ '.git tree (implicit HEAD)' \
+ 'gitweb_run "p=.git;a=tree"'
+
+test_expect_success \
+ '.git blob (file)' \
+ 'gitweb_run "p=.git;a=blob;f=file"'
+
+test_expect_success \
+ '.git blob_plain (file)' \
+ 'gitweb_run "p=.git;a=blob_plain;f=file"'
+
+# ----------------------------------------------------------------------
+# nonexistent objects
+
+test_expect_success \
+ '.git commit (non-existent)' \
+ 'gitweb_run "p=.git;a=commit;h=non-existent"'
+
+test_expect_success \
+ '.git commitdiff (non-existent)' \
+ 'gitweb_run "p=.git;a=commitdiff;h=non-existent"'
+
+test_expect_success \
+ '.git commitdiff (non-existent vs HEAD)' \
+ 'gitweb_run "p=.git;a=commitdiff;hp=non-existent;h=HEAD"'
+
+test_expect_success \
+ '.git tree (0000000000000000000000000000000000000000)' \
+ 'gitweb_run "p=.git;a=tree;h=0000000000000000000000000000000000000000"'
+
+test_expect_success \
+ '.git tag (0000000000000000000000000000000000000000)' \
+ 'gitweb_run "p=.git;a=tag;h=0000000000000000000000000000000000000000"'
+
+test_expect_success \
+ '.git blob (non-existent)' \
+ 'gitweb_run "p=.git;a=blob;f=non-existent"'
+
+test_expect_success \
+ '.git blob_plain (non-existent)' \
+ 'gitweb_run "p=.git;a=blob_plain;f=non-existent"'
+
+
+# ----------------------------------------------------------------------
+# commitdiff testing (implicit, one implicit tree-ish)
+
+test_expect_success \
+ 'commitdiff(0): root' \
+ 'gitweb_run "p=.git;a=commitdiff"'
+
+test_expect_success \
+ 'commitdiff(0): file added' \
+ 'echo "New file" >new_file &&
+ git add new_file &&
+ git commit -a -m "File added." &&
+ gitweb_run "p=.git;a=commitdiff"'
+
+test_expect_success \
+ 'commitdiff(0): mode change' \
+ 'test_chmod +x new_file &&
+ git commit -a -m "Mode changed." &&
+ gitweb_run "p=.git;a=commitdiff"'
+
+test_expect_success \
+ 'commitdiff(0): file renamed' \
+ 'git mv new_file renamed_file &&
+ git commit -a -m "File renamed." &&
+ gitweb_run "p=.git;a=commitdiff"'
+
+test_expect_success \
+ 'commitdiff(0): file to symlink' \
+ 'rm renamed_file &&
+ test_ln_s_add file renamed_file &&
+ git commit -a -m "File to symlink." &&
+ gitweb_run "p=.git;a=commitdiff"'
+
+test_expect_success \
+ 'commitdiff(0): file deleted' \
+ 'git rm renamed_file &&
+ rm -f renamed_file &&
+ git commit -a -m "File removed." &&
+ gitweb_run "p=.git;a=commitdiff"'
+
+test_expect_success \
+ 'commitdiff(0): file copied / new file' \
+ 'cp file file2 &&
+ git add file2 &&
+ git commit -a -m "File copied." &&
+ gitweb_run "p=.git;a=commitdiff"'
+
+test_expect_success \
+ 'commitdiff(0): mode change and modified' \
+ 'echo "New line" >>file2 &&
+ test_chmod +x file2 &&
+ git commit -a -m "Mode change and modification." &&
+ gitweb_run "p=.git;a=commitdiff"'
+
+test_expect_success \
+ 'commitdiff(0): renamed and modified' \
+ 'cat >file2<<EOF &&
+Dominus regit me,
+et nihil mihi deerit.
+In loco pascuae ibi me collocavit,
+super aquam refectionis educavit me;
+animam meam convertit,
+deduxit me super semitas jusitiae,
+propter nomen suum.
+EOF
+ git commit -a -m "File added." &&
+ git mv file2 file3 &&
+ echo "Propter nomen suum." >>file3 &&
+ git commit -a -m "File rename and modification." &&
+ gitweb_run "p=.git;a=commitdiff"'
+
+test_expect_success \
+ 'commitdiff(0): renamed, mode change and modified' \
+ 'git mv file3 file2 &&
+ echo "Propter nomen suum." >>file2 &&
+ test_chmod +x file2 &&
+ git commit -a -m "File rename, mode change and modification." &&
+ gitweb_run "p=.git;a=commitdiff"'
+
+# ----------------------------------------------------------------------
+# commitdiff testing (taken from t4114-apply-typechange.sh)
+
+test_expect_success 'setup typechange commits' '
+ echo "hello world" >foo &&
+ echo "hi planet" >bar &&
+ git update-index --add foo bar &&
+ git commit -m initial &&
+ git branch initial &&
+ rm -f foo &&
+ test_ln_s_add bar foo &&
+ git commit -m "foo symlinked to bar" &&
+ git branch foo-symlinked-to-bar &&
+ rm -f foo &&
+ echo "how far is the sun?" >foo &&
+ git update-index foo &&
+ git commit -m "foo back to file" &&
+ git branch foo-back-to-file &&
+ rm -f foo &&
+ git update-index --remove foo &&
+ mkdir foo &&
+ echo "if only I knew" >foo/baz &&
+ git update-index --add foo/baz &&
+ git commit -m "foo becomes a directory" &&
+ git branch "foo-becomes-a-directory" &&
+ echo "hello world" >foo/baz &&
+ git update-index foo/baz &&
+ git commit -m "foo/baz is the original foo" &&
+ git branch foo-baz-renamed-from-foo
+ '
+
+test_expect_success \
+ 'commitdiff(2): file renamed from foo to foo/baz' \
+ 'gitweb_run "p=.git;a=commitdiff;hp=initial;h=foo-baz-renamed-from-foo"'
+
+test_expect_success \
+ 'commitdiff(2): file renamed from foo/baz to foo' \
+ 'gitweb_run "p=.git;a=commitdiff;hp=foo-baz-renamed-from-foo;h=initial"'
+
+test_expect_success \
+ 'commitdiff(2): directory becomes file' \
+ 'gitweb_run "p=.git;a=commitdiff;hp=foo-becomes-a-directory;h=initial"'
+
+test_expect_success \
+ 'commitdiff(2): file becomes directory' \
+ 'gitweb_run "p=.git;a=commitdiff;hp=initial;h=foo-becomes-a-directory"'
+
+test_expect_success \
+ 'commitdiff(2): file becomes symlink' \
+ 'gitweb_run "p=.git;a=commitdiff;hp=initial;h=foo-symlinked-to-bar"'
+
+test_expect_success \
+ 'commitdiff(2): symlink becomes file' \
+ 'gitweb_run "p=.git;a=commitdiff;hp=foo-symlinked-to-bar;h=foo-back-to-file"'
+
+test_expect_success \
+ 'commitdiff(2): symlink becomes directory' \
+ 'gitweb_run "p=.git;a=commitdiff;hp=foo-symlinked-to-bar;h=foo-becomes-a-directory"'
+
+test_expect_success \
+ 'commitdiff(2): directory becomes symlink' \
+ 'gitweb_run "p=.git;a=commitdiff;hp=foo-becomes-a-directory;h=foo-symlinked-to-bar"'
+
+# ----------------------------------------------------------------------
+# commitdiff testing (incomplete lines)
+
+test_expect_success 'setup incomplete lines' '
+ cat >file<<-\EOF &&
+ Dominus regit me,
+ et nihil mihi deerit.
+ In loco pascuae ibi me collocavit,
+ super aquam refectionis educavit me;
+ animam meam convertit,
+ deduxit me super semitas jusitiae,
+ propter nomen suum.
+ CHANGE_ME
+ EOF
+ git commit -a -m "Preparing for incomplete lines" &&
+ echo "incomplete" | tr -d "\\012" >>file &&
+ git commit -a -m "Add incomplete line" &&
+ git tag incomplete_lines_add &&
+ sed -e s/CHANGE_ME/change_me/ <file >file+ &&
+ mv -f file+ file &&
+ git commit -a -m "Incomplete context line" &&
+ git tag incomplete_lines_ctx &&
+ echo "Dominus regit me," >file &&
+ echo "incomplete line" | tr -d "\\012" >>file &&
+ git commit -a -m "Change incomplete line" &&
+ git tag incomplete_lines_chg &&
+ echo "Dominus regit me," >file &&
+ git commit -a -m "Remove incomplete line" &&
+ git tag incomplete_lines_rem
+'
+
+test_expect_success 'commitdiff(1): addition of incomplete line' '
+ gitweb_run "p=.git;a=commitdiff;h=incomplete_lines_add"
+'
+
+test_expect_success 'commitdiff(1): incomplete line as context line' '
+ gitweb_run "p=.git;a=commitdiff;h=incomplete_lines_ctx"
+'
+
+test_expect_success 'commitdiff(1): change incomplete line' '
+ gitweb_run "p=.git;a=commitdiff;h=incomplete_lines_chg"
+'
+
+test_expect_success 'commitdiff(1): removal of incomplete line' '
+ gitweb_run "p=.git;a=commitdiff;h=incomplete_lines_rem"
+'
+
+# ----------------------------------------------------------------------
+# commit, commitdiff: merge, large
+test_expect_success \
+ 'Create a merge' \
+ 'git checkout b &&
+ echo "Branch" >>b &&
+ git add b &&
+ git commit -a -m "On branch" &&
+ git checkout main &&
+ git merge b &&
+ git tag merge_commit'
+
+test_expect_success \
+ 'commit(0): merge commit' \
+ 'gitweb_run "p=.git;a=commit"'
+
+test_expect_success \
+ 'commitdiff(0): merge commit' \
+ 'gitweb_run "p=.git;a=commitdiff"'
+
+test_expect_success \
+ 'Prepare large commit' \
+ 'git checkout b &&
+ echo "To be changed" >01-change &&
+ echo "To be renamed" >02-pure-rename-from &&
+ echo "To be deleted" >03-delete &&
+ echo "To be renamed and changed" >04-rename-from &&
+ echo "To have mode changed" >05-mode-change &&
+ echo "File to symlink" >06-file-or-symlink &&
+ echo "To be changed and have mode changed" >07-change-mode-change &&
+ git add 0* &&
+ git commit -a -m "Prepare large commit" &&
+ echo "Changed" >01-change &&
+ git mv 02-pure-rename-from 02-pure-rename-to &&
+ git rm 03-delete && rm -f 03-delete &&
+ echo "A new file" >03-new &&
+ git add 03-new &&
+ git mv 04-rename-from 04-rename-to &&
+ echo "Changed" >>04-rename-to &&
+ test_chmod +x 05-mode-change &&
+ rm -f 06-file-or-symlink &&
+ test_ln_s_add 01-change 06-file-or-symlink &&
+ echo "Changed and have mode changed" >07-change-mode-change &&
+ test_chmod +x 07-change-mode-change &&
+ git commit -a -m "Large commit" &&
+ git checkout main'
+
+test_expect_success \
+ 'commit(1): large commit' \
+ 'gitweb_run "p=.git;a=commit;h=b"'
+
+test_expect_success \
+ 'commitdiff(1): large commit' \
+ 'gitweb_run "p=.git;a=commitdiff;h=b"'
+
+# ----------------------------------------------------------------------
+# side-by-side diff
+
+test_expect_success 'side-by-side: addition of incomplete line' '
+ gitweb_run "p=.git;a=commitdiff;h=incomplete_lines_add;ds=sidebyside"
+'
+
+test_expect_success 'side-by-side: incomplete line as context line' '
+ gitweb_run "p=.git;a=commitdiff;h=incomplete_lines_ctx;ds=sidebyside"
+'
+
+test_expect_success 'side-by-side: changed incomplete line' '
+ gitweb_run "p=.git;a=commitdiff;h=incomplete_lines_chg;ds=sidebyside"
+'
+
+test_expect_success 'side-by-side: removal of incomplete line' '
+ gitweb_run "p=.git;a=commitdiff;h=incomplete_lines_rem;ds=sidebyside"
+'
+
+test_expect_success 'side-by-side: merge commit' '
+ gitweb_run "p=.git;a=commitdiff;h=merge_commit;ds=sidebyside"
+'
+
+# ----------------------------------------------------------------------
+# tags testing
+
+test_expect_success \
+ 'tags: list of different types of tags' \
+ 'git checkout main &&
+ git tag -a -m "Tag commit object" tag-commit HEAD &&
+ git tag -a -m "" tag-commit-nomessage HEAD &&
+ git tag -a -m "Tag tag object" tag-tag tag-commit &&
+ git tag -a -m "Tag tree object" tag-tree HEAD^{tree} &&
+ git tag -a -m "Tag blob object" tag-blob HEAD:file &&
+ git tag lightweight/tag-commit HEAD &&
+ git tag lightweight/tag-tag tag-commit &&
+ git tag lightweight/tag-tree HEAD^{tree} &&
+ git tag lightweight/tag-blob HEAD:file &&
+ gitweb_run "p=.git;a=tags"'
+
+test_expect_success \
+ 'tag: Tag to commit object' \
+ 'gitweb_run "p=.git;a=tag;h=tag-commit"'
+
+test_expect_success \
+ 'tag: on lightweight tag (invalid)' \
+ 'gitweb_run "p=.git;a=tag;h=lightweight/tag-commit"'
+
+# ----------------------------------------------------------------------
+# logs
+
+test_expect_success \
+ 'logs: log (implicit HEAD)' \
+ 'gitweb_run "p=.git;a=log"'
+
+test_expect_success \
+ 'logs: shortlog (implicit HEAD)' \
+ 'gitweb_run "p=.git;a=shortlog"'
+
+test_expect_success \
+ 'logs: history (implicit HEAD, file)' \
+ 'gitweb_run "p=.git;a=history;f=file"'
+
+test_expect_success \
+ 'logs: history (implicit HEAD, non-existent file)' \
+ 'gitweb_run "p=.git;a=history;f=non-existent"'
+
+test_expect_success \
+ 'logs: history (implicit HEAD, deleted file)' \
+ 'git checkout main &&
+ echo "to be deleted" >deleted_file &&
+ git add deleted_file &&
+ git commit -m "Add file to be deleted" &&
+ git rm deleted_file &&
+ git commit -m "Delete file" &&
+ gitweb_run "p=.git;a=history;f=deleted_file"'
+
+# ----------------------------------------------------------------------
+# path_info links
+test_expect_success \
+ 'path_info: project' \
+ 'gitweb_run "" "/.git"'
+
+test_expect_success \
+ 'path_info: project/branch' \
+ 'gitweb_run "" "/.git/b"'
+
+test_expect_success \
+ 'path_info: project/branch:file' \
+ 'gitweb_run "" "/.git/main:file"'
+
+test_expect_success \
+ 'path_info: project/branch:dir/' \
+ 'gitweb_run "" "/.git/main:foo/"'
+
+test_expect_success \
+ 'path_info: project/branch (non-existent)' \
+ 'gitweb_run "" "/.git/non-existent"'
+
+test_expect_success \
+ 'path_info: project/branch:filename (non-existent branch)' \
+ 'gitweb_run "" "/.git/non-existent:non-existent"'
+
+test_expect_success \
+ 'path_info: project/branch:file (non-existent)' \
+ 'gitweb_run "" "/.git/main:non-existent"'
+
+test_expect_success \
+ 'path_info: project/branch:dir/ (non-existent)' \
+ 'gitweb_run "" "/.git/main:non-existent/"'
+
+
+test_expect_success \
+ 'path_info: project/branch:/file' \
+ 'gitweb_run "" "/.git/main:/file"'
+
+test_expect_success \
+ 'path_info: project/:/file (implicit HEAD)' \
+ 'gitweb_run "" "/.git/:/file"'
+
+test_expect_success \
+ 'path_info: project/:/ (implicit HEAD, top tree)' \
+ 'gitweb_run "" "/.git/:/"'
+
+
+# ----------------------------------------------------------------------
+# feed generation
+
+test_expect_success \
+ 'feeds: OPML' \
+ 'gitweb_run "a=opml"'
+
+test_expect_success \
+ 'feed: RSS' \
+ 'gitweb_run "p=.git;a=rss"'
+
+test_expect_success \
+ 'feed: Atom' \
+ 'gitweb_run "p=.git;a=atom"'
+
+# ----------------------------------------------------------------------
+# encoding/decoding
+
+test_expect_success \
+ 'encode(commit): utf8' \
+ '. "$TEST_DIRECTORY"/t3901/utf8.txt &&
+ test_when_finished "GIT_AUTHOR_NAME=\"A U Thor\"" &&
+ test_when_finished "GIT_COMMITTER_NAME=\"C O Mitter\"" &&
+ echo "UTF-8" >>file &&
+ git add file &&
+ git commit -F "$TEST_DIRECTORY"/t3900/1-UTF-8.txt &&
+ gitweb_run "p=.git;a=commit"'
+
+test_expect_success \
+ 'encode(commit): iso-8859-1' \
+ '. "$TEST_DIRECTORY"/t3901/8859-1.txt &&
+ test_when_finished "GIT_AUTHOR_NAME=\"A U Thor\"" &&
+ test_when_finished "GIT_COMMITTER_NAME=\"C O Mitter\"" &&
+ echo "ISO-8859-1" >>file &&
+ git add file &&
+ test_config i18n.commitencoding ISO-8859-1 &&
+ git commit -F "$TEST_DIRECTORY"/t3900/ISO8859-1.txt &&
+ gitweb_run "p=.git;a=commit"'
+
+test_expect_success \
+ 'encode(log): utf-8 and iso-8859-1' \
+ 'gitweb_run "p=.git;a=log"'
+
+# ----------------------------------------------------------------------
+# extra options
+
+test_expect_success \
+ 'opt: log --no-merges' \
+ 'gitweb_run "p=.git;a=log;opt=--no-merges"'
+
+test_expect_success \
+ 'opt: atom --no-merges' \
+ 'gitweb_run "p=.git;a=log;opt=--no-merges"'
+
+test_expect_success \
+ 'opt: "file" history --no-merges' \
+ 'gitweb_run "p=.git;a=history;f=file;opt=--no-merges"'
+
+test_expect_success \
+ 'opt: log --no-such-option (invalid option)' \
+ 'gitweb_run "p=.git;a=log;opt=--no-such-option"'
+
+test_expect_success \
+ 'opt: tree --no-merges (invalid option for action)' \
+ 'gitweb_run "p=.git;a=tree;opt=--no-merges"'
+
+# ----------------------------------------------------------------------
+# testing config_to_multi / cloneurl
+
+test_expect_success \
+ 'URL: no project URLs, no base URL' \
+ 'gitweb_run "p=.git;a=summary"'
+
+test_expect_success \
+ 'URL: project URLs via gitweb.url' \
+ 'git config --add gitweb.url git://example.com/git/trash.git &&
+ git config --add gitweb.url http://example.com/git/trash.git &&
+ gitweb_run "p=.git;a=summary"'
+
+cat >.git/cloneurl <<\EOF
+git://example.com/git/trash.git
+http://example.com/git/trash.git
+EOF
+
+test_expect_success \
+ 'URL: project URLs via cloneurl file' \
+ 'gitweb_run "p=.git;a=summary"'
+
+# ----------------------------------------------------------------------
+# gitweb config and repo config
+
+cat >>gitweb_config.perl <<\EOF
+
+# turn on override for each overridable feature
+foreach my $key (keys %feature) {
+ if ($feature{$key}{'sub'}) {
+ $feature{$key}{'override'} = 1;
+ }
+}
+EOF
+
+test_expect_success \
+ 'config override: projects list (implicit)' \
+ 'gitweb_run'
+
+test_expect_success \
+ 'config override: tree view, features not overridden in repo config' \
+ 'gitweb_run "p=.git;a=tree"'
+
+test_expect_success \
+ 'config override: tree view, features disabled in repo config' \
+ 'git config gitweb.blame no &&
+ git config gitweb.snapshot none &&
+ git config gitweb.avatar gravatar &&
+ gitweb_run "p=.git;a=tree"'
+
+test_expect_success \
+ 'config override: tree view, features enabled in repo config (1)' \
+ 'git config gitweb.blame yes &&
+ git config gitweb.snapshot "zip,tgz, tbz2" &&
+ gitweb_run "p=.git;a=tree"'
+
+test_expect_success 'setup' '
+ version=$(git config core.repositoryformatversion) &&
+ algo=$(test_might_fail git config extensions.objectformat) &&
+ cat >.git/config <<-\EOF &&
+ # testing noval and alternate separator
+ [gitweb]
+ blame
+ snapshot = zip tgz
+ EOF
+ git config core.repositoryformatversion "$version" &&
+ if test -n "$algo"
+ then
+ git config extensions.objectformat "$algo"
+ fi
+'
+
+test_expect_success \
+ 'config override: tree view, features enabled in repo config (2)' \
+ 'gitweb_run "p=.git;a=tree"'
+
+# ----------------------------------------------------------------------
+# searching
+
+cat >>gitweb_config.perl <<\EOF
+
+# enable search
+$feature{'search'}{'default'} = [1];
+$feature{'grep'}{'default'} = [1];
+$feature{'pickaxe'}{'default'} = [1];
+EOF
+
+test_expect_success \
+ 'search: preparation' \
+ 'echo "1st MATCH" >>file &&
+ echo "2nd MATCH" >>file &&
+ echo "MATCH" >>bar &&
+ git add file bar &&
+ git commit -m "Added MATCH word"'
+
+test_expect_success \
+ 'search: commit author' \
+ 'gitweb_run "p=.git;a=search;h=HEAD;st=author;s=A+U+Thor"'
+
+test_expect_success \
+ 'search: commit message' \
+ 'gitweb_run "p=.git;a=search;h=HEAD;st=commitr;s=MATCH"'
+
+test_expect_success \
+ 'search: grep' \
+ 'gitweb_run "p=.git;a=search;h=HEAD;st=grep;s=MATCH"'
+
+test_expect_success \
+ 'search: pickaxe' \
+ 'gitweb_run "p=.git;a=search;h=HEAD;st=pickaxe;s=MATCH"'
+
+test_expect_success \
+ 'search: projects' \
+ 'gitweb_run "a=project_list;s=.git"'
+
+# ----------------------------------------------------------------------
+# non-ASCII in README.html
+
+test_expect_success \
+ 'README.html with non-ASCII characters (utf-8)' \
+ 'echo "<b>UTF-8 example:</b><br />" >.git/README.html &&
+ cat "$TEST_DIRECTORY"/t3900/1-UTF-8.txt >>.git/README.html &&
+ gitweb_run "p=.git;a=summary"'
+
+# ----------------------------------------------------------------------
+# syntax highlighting
+
+
+highlight_version=$(highlight --version </dev/null 2>/dev/null)
+if [ $? -eq 127 ]; then
+ say "Skipping syntax highlighting tests: 'highlight' not found"
+elif test -z "$highlight_version"; then
+ say "Skipping syntax highlighting tests: incorrect 'highlight' found"
+else
+ test_set_prereq HIGHLIGHT
+ cat >>gitweb_config.perl <<-\EOF
+ our $highlight_bin = "highlight";
+ $feature{'highlight'}{'override'} = 1;
+ EOF
+fi
+
+test_expect_success HIGHLIGHT \
+ 'syntax highlighting (no highlight, unknown syntax)' \
+ 'git config gitweb.highlight yes &&
+ gitweb_run "p=.git;a=blob;f=file"'
+
+test_expect_success HIGHLIGHT \
+ 'syntax highlighting (highlighted, shell script)' \
+ 'git config gitweb.highlight yes &&
+ echo "#!/usr/bin/sh" >test.sh &&
+ git add test.sh &&
+ git commit -m "Add test.sh" &&
+ gitweb_run "p=.git;a=blob;f=test.sh"'
+
+test_expect_success HIGHLIGHT \
+ 'syntax highlighting (highlighter language autodetection)' \
+ 'git config gitweb.highlight yes &&
+ echo "#!/usr/bin/perl" >test &&
+ git add test &&
+ git commit -m "Add test" &&
+ gitweb_run "p=.git;a=blob;f=test"'
+
+# ----------------------------------------------------------------------
+# forks of projects
+
+cat >>gitweb_config.perl <<\EOF &&
+$feature{'forks'}{'default'} = [1];
+EOF
+
+test_expect_success \
+ 'forks: prepare' \
+ 'git init --bare foo.git &&
+ git --git-dir=foo.git --work-tree=. add file &&
+ git --git-dir=foo.git --work-tree=. commit -m "Initial commit" &&
+ echo "foo" >foo.git/description &&
+ mkdir -p foo &&
+ (cd foo &&
+ git clone --shared --bare ../foo.git foo-forked.git &&
+ echo "fork of foo" >foo-forked.git/description)'
+
+test_expect_success \
+ 'forks: projects list' \
+ 'gitweb_run'
+
+test_expect_success \
+ 'forks: forks action' \
+ 'gitweb_run "p=foo.git;a=forks"'
+
+# ----------------------------------------------------------------------
+# content tags (tag cloud)
+
+cat >>gitweb_config.perl <<-\EOF &&
+# we don't test _setting_ content tags, so any true value is good
+$feature{'ctags'}{'default'} = ['ctags_script.cgi'];
+EOF
+
+test_expect_success \
+ 'ctags: tag cloud in projects list' \
+ 'mkdir .git/ctags &&
+ echo "2" >.git/ctags/foo &&
+ echo "1" >.git/ctags/bar &&
+ gitweb_run'
+
+test_expect_success \
+ 'ctags: search projects by existing tag' \
+ 'gitweb_run "by_tag=foo"'
+
+test_expect_success \
+ 'ctags: search projects by non existent tag' \
+ 'gitweb_run "by_tag=non-existent"'
+
+test_expect_success \
+ 'ctags: malformed tag weights' \
+ 'mkdir -p .git/ctags &&
+ echo "not-a-number" >.git/ctags/nan &&
+ echo "not-a-number-2" >.git/ctags/nan2 &&
+ echo "0.1" >.git/ctags/floating-point &&
+ gitweb_run'
+
+# ----------------------------------------------------------------------
+# categories
+
+test_expect_success \
+ 'categories: projects list, only default category' \
+ 'echo "\$projects_list_group_categories = 1;" >>gitweb_config.perl &&
+ gitweb_run'
+
+# ----------------------------------------------------------------------
+# unborn branches
+
+test_expect_success \
+ 'unborn HEAD: "summary" page (with "heads" subview)' \
+ '{
+ git checkout orphan_branch ||
+ git checkout --orphan orphan_branch
+ } &&
+ test_when_finished "git checkout main" &&
+ gitweb_run "p=.git;a=summary"'
+
+test_done
diff --git a/t/t9501-gitweb-standalone-http-status.sh b/t/t9501-gitweb-standalone-http-status.sh
new file mode 100755
index 0000000..c900231
--- /dev/null
+++ b/t/t9501-gitweb-standalone-http-status.sh
@@ -0,0 +1,221 @@
+#!/bin/sh
+#
+# Copyright (c) 2009 Mark Rada
+#
+
+test_description='gitweb as standalone script (http status tests).
+
+This test runs gitweb (git web interface) as a CGI script from the
+commandline, and checks that it returns the expected HTTP status
+code and message.'
+
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./lib-gitweb.sh
+
+#
+# Gitweb only provides the functionality tested by the 'modification times'
+# tests if it can access a date parser from one of these modules:
+#
+perl -MHTTP::Date -e 0 >/dev/null 2>&1 && test_set_prereq DATE_PARSER
+perl -MTime::ParseDate -e 0 >/dev/null 2>&1 && test_set_prereq DATE_PARSER
+
+# ----------------------------------------------------------------------
+# snapshot settings
+
+test_expect_success 'setup' "
+ test_commit 'SnapshotTests' 'i can has snapshot'
+"
+
+
+cat >>gitweb_config.perl <<\EOF
+$feature{'snapshot'}{'override'} = 0;
+EOF
+
+test_expect_success \
+ 'snapshots: tgz only default format enabled' \
+ 'gitweb_run "p=.git;a=snapshot;h=HEAD;sf=tgz" &&
+ grep "Status: 200 OK" gitweb.output &&
+ gitweb_run "p=.git;a=snapshot;h=HEAD;sf=tbz2" &&
+ grep "403 - Unsupported snapshot format" gitweb.output &&
+ gitweb_run "p=.git;a=snapshot;h=HEAD;sf=txz" &&
+ grep "403 - Snapshot format not allowed" gitweb.output &&
+ gitweb_run "p=.git;a=snapshot;h=HEAD;sf=zip" &&
+ grep "403 - Unsupported snapshot format" gitweb.output'
+
+
+cat >>gitweb_config.perl <<\EOF
+$feature{'snapshot'}{'default'} = ['tgz','tbz2','txz','zip'];
+EOF
+
+test_expect_success \
+ 'snapshots: all enabled in default, use default disabled value' \
+ 'gitweb_run "p=.git;a=snapshot;h=HEAD;sf=tgz" &&
+ grep "Status: 200 OK" gitweb.output &&
+ gitweb_run "p=.git;a=snapshot;h=HEAD;sf=tbz2" &&
+ grep "Status: 200 OK" gitweb.output &&
+ gitweb_run "p=.git;a=snapshot;h=HEAD;sf=txz" &&
+ grep "403 - Snapshot format not allowed" gitweb.output &&
+ gitweb_run "p=.git;a=snapshot;h=HEAD;sf=zip" &&
+ grep "Status: 200 OK" gitweb.output'
+
+
+cat >>gitweb_config.perl <<\EOF
+$known_snapshot_formats{'zip'}{'disabled'} = 1;
+EOF
+
+test_expect_success \
+ 'snapshots: zip explicitly disabled' \
+ 'gitweb_run "p=.git;a=snapshot;h=HEAD;sf=zip" &&
+ grep "403 - Snapshot format not allowed" gitweb.output'
+test_debug 'cat gitweb.output'
+
+
+cat >>gitweb_config.perl <<\EOF
+$known_snapshot_formats{'tgz'}{'disabled'} = 0;
+EOF
+
+test_expect_success \
+ 'snapshots: tgz explicitly enabled' \
+ 'gitweb_run "p=.git;a=snapshot;h=HEAD;sf=tgz" &&
+ grep "Status: 200 OK" gitweb.output'
+test_debug 'cat gitweb.headers'
+
+
+# ----------------------------------------------------------------------
+# snapshot hash ids
+
+test_expect_success 'snapshots: good tree-ish id' '
+ gitweb_run "p=.git;a=snapshot;h=main;sf=tgz" &&
+ grep "Status: 200 OK" gitweb.output
+'
+test_debug 'cat gitweb.headers'
+
+test_expect_success 'snapshots: bad tree-ish id' '
+ gitweb_run "p=.git;a=snapshot;h=frizzumFrazzum;sf=tgz" &&
+ grep "404 - Object does not exist" gitweb.output
+'
+test_debug 'cat gitweb.output'
+
+test_expect_success 'snapshots: bad tree-ish id (tagged object)' '
+ echo object > tag-object &&
+ git add tag-object &&
+ test_tick && git commit -m "Object to be tagged" &&
+ git tag tagged-object $(git hash-object tag-object) &&
+ gitweb_run "p=.git;a=snapshot;h=tagged-object;sf=tgz" &&
+ grep "400 - Object is not a tree-ish" gitweb.output
+'
+test_debug 'cat gitweb.output'
+
+test_expect_success 'snapshots: good object id' '
+ ID=$(git rev-parse --verify HEAD) &&
+ gitweb_run "p=.git;a=snapshot;h=$ID;sf=tgz" &&
+ grep "Status: 200 OK" gitweb.output
+'
+test_debug 'cat gitweb.headers'
+
+test_expect_success 'snapshots: bad object id' '
+ gitweb_run "p=.git;a=snapshot;h=abcdef01234;sf=tgz" &&
+ grep "404 - Object does not exist" gitweb.output
+'
+test_debug 'cat gitweb.output'
+
+# ----------------------------------------------------------------------
+# modification times (Last-Modified and If-Modified-Since)
+
+test_expect_success DATE_PARSER 'modification: feed last-modified' '
+ gitweb_run "p=.git;a=atom;h=main" &&
+ grep "Status: 200 OK" gitweb.headers &&
+ grep "Last-modified: Thu, 7 Apr 2005 22:14:13 +0000" gitweb.headers
+'
+test_debug 'cat gitweb.headers'
+
+test_expect_success DATE_PARSER 'modification: feed if-modified-since (modified)' '
+ HTTP_IF_MODIFIED_SINCE="Wed, 6 Apr 2005 22:14:13 +0000" &&
+ export HTTP_IF_MODIFIED_SINCE &&
+ test_when_finished "unset HTTP_IF_MODIFIED_SINCE" &&
+ gitweb_run "p=.git;a=atom;h=main" &&
+ grep "Status: 200 OK" gitweb.headers
+'
+test_debug 'cat gitweb.headers'
+
+test_expect_success DATE_PARSER 'modification: feed if-modified-since (unmodified)' '
+ HTTP_IF_MODIFIED_SINCE="Thu, 7 Apr 2005 22:14:13 +0000" &&
+ export HTTP_IF_MODIFIED_SINCE &&
+ test_when_finished "unset HTTP_IF_MODIFIED_SINCE" &&
+ gitweb_run "p=.git;a=atom;h=main" &&
+ grep "Status: 304 Not Modified" gitweb.headers
+'
+test_debug 'cat gitweb.headers'
+
+test_expect_success DATE_PARSER 'modification: snapshot last-modified' '
+ gitweb_run "p=.git;a=snapshot;h=main;sf=tgz" &&
+ grep "Status: 200 OK" gitweb.headers &&
+ grep "Last-modified: Thu, 7 Apr 2005 22:14:13 +0000" gitweb.headers
+'
+test_debug 'cat gitweb.headers'
+
+test_expect_success DATE_PARSER 'modification: snapshot if-modified-since (modified)' '
+ HTTP_IF_MODIFIED_SINCE="Wed, 6 Apr 2005 22:14:13 +0000" &&
+ export HTTP_IF_MODIFIED_SINCE &&
+ test_when_finished "unset HTTP_IF_MODIFIED_SINCE" &&
+ gitweb_run "p=.git;a=snapshot;h=main;sf=tgz" &&
+ grep "Status: 200 OK" gitweb.headers
+'
+test_debug 'cat gitweb.headers'
+
+test_expect_success DATE_PARSER 'modification: snapshot if-modified-since (unmodified)' '
+ HTTP_IF_MODIFIED_SINCE="Thu, 7 Apr 2005 22:14:13 +0000" &&
+ export HTTP_IF_MODIFIED_SINCE &&
+ test_when_finished "unset HTTP_IF_MODIFIED_SINCE" &&
+ gitweb_run "p=.git;a=snapshot;h=main;sf=tgz" &&
+ grep "Status: 304 Not Modified" gitweb.headers
+'
+test_debug 'cat gitweb.headers'
+
+test_expect_success DATE_PARSER 'modification: tree snapshot' '
+ ID=$(git rev-parse --verify HEAD^{tree}) &&
+ HTTP_IF_MODIFIED_SINCE="Wed, 6 Apr 2005 22:14:13 +0000" &&
+ export HTTP_IF_MODIFIED_SINCE &&
+ test_when_finished "unset HTTP_IF_MODIFIED_SINCE" &&
+ gitweb_run "p=.git;a=snapshot;h=$ID;sf=tgz" &&
+ grep "Status: 200 OK" gitweb.headers &&
+ ! grep -i "last-modified" gitweb.headers
+'
+test_debug 'cat gitweb.headers'
+
+# ----------------------------------------------------------------------
+# load checking
+
+# always hit the load limit
+cat >>gitweb_config.perl <<\EOF
+our $maxload = -1;
+EOF
+
+test_expect_success 'load checking: load too high (default action)' '
+ gitweb_run "p=.git" &&
+ grep "Status: 503 Service Unavailable" gitweb.headers &&
+ grep "503 - The load average on the server is too high" gitweb.body
+'
+test_debug 'cat gitweb.headers'
+
+# turn off load checking
+cat >>gitweb_config.perl <<\EOF
+our $maxload = undef;
+EOF
+
+
+# ----------------------------------------------------------------------
+# invalid arguments
+
+test_expect_success 'invalid arguments: invalid regexp (in project search)' '
+ gitweb_run "a=project_list;s=*\.git;sr=1" &&
+ grep "Status: 400" gitweb.headers &&
+ grep "400 - Invalid.*regexp" gitweb.body
+'
+test_debug 'cat gitweb.headers'
+
+test_done
diff --git a/t/t9502-gitweb-standalone-parse-output.sh b/t/t9502-gitweb-standalone-parse-output.sh
new file mode 100755
index 0000000..81d5625
--- /dev/null
+++ b/t/t9502-gitweb-standalone-parse-output.sh
@@ -0,0 +1,237 @@
+#!/bin/sh
+#
+# Copyright (c) 2009 Mark Rada
+#
+
+test_description='gitweb as standalone script (parsing script output).
+
+This test runs gitweb (git web interface) as a CGI script from the
+commandline, and checks that it produces the correct output, either
+in the HTTP header or the actual script output.'
+
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./lib-gitweb.sh
+
+# ----------------------------------------------------------------------
+# snapshot file name and prefix
+
+cat >>gitweb_config.perl <<\EOF
+
+$known_snapshot_formats{'tar'} = {
+ 'display' => 'tar',
+ 'type' => 'application/x-tar',
+ 'suffix' => '.tar',
+ 'format' => 'tar',
+};
+
+$feature{'snapshot'}{'default'} = ['tar'];
+EOF
+
+# Call check_snapshot with the arguments "<basename> [<prefix>]"
+#
+# This will check that gitweb HTTP header contains proposed filename
+# as <basename> with '.tar' suffix added, and that generated tarfile
+# (gitweb message body) has <prefix> as prefix for all files in tarfile
+#
+# <prefix> default to <basename>
+check_snapshot () {
+ basename=$1
+ prefix=${2:-"$1"}
+ echo "basename=$basename"
+ grep "filename=.*$basename.tar" gitweb.headers >/dev/null 2>&1 &&
+ "$TAR" tf gitweb.body >file_list &&
+ ! grep -v -e "^$prefix$" -e "^$prefix/" -e "^pax_global_header$" file_list
+}
+
+test_expect_success setup '
+ test_commit first foo &&
+ git branch xx/test &&
+ FULL_ID=$(git rev-parse --verify HEAD) &&
+ SHORT_ID=$(git rev-parse --verify --short=7 HEAD)
+'
+test_debug '
+ echo "FULL_ID = $FULL_ID"
+ echo "SHORT_ID = $SHORT_ID"
+'
+
+test_expect_success 'snapshot: full sha1' '
+ gitweb_run "p=.git;a=snapshot;h=$FULL_ID;sf=tar" &&
+ check_snapshot ".git-$SHORT_ID"
+'
+test_debug 'cat gitweb.headers && cat file_list'
+
+test_expect_success 'snapshot: shortened sha1' '
+ gitweb_run "p=.git;a=snapshot;h=$SHORT_ID;sf=tar" &&
+ check_snapshot ".git-$SHORT_ID"
+'
+test_debug 'cat gitweb.headers && cat file_list'
+
+test_expect_success 'snapshot: almost full sha1' '
+ ID=$(git rev-parse --short=30 HEAD) &&
+ gitweb_run "p=.git;a=snapshot;h=$ID;sf=tar" &&
+ check_snapshot ".git-$SHORT_ID"
+'
+test_debug 'cat gitweb.headers && cat file_list'
+
+test_expect_success 'snapshot: HEAD' '
+ gitweb_run "p=.git;a=snapshot;h=HEAD;sf=tar" &&
+ check_snapshot ".git-HEAD-$SHORT_ID"
+'
+test_debug 'cat gitweb.headers && cat file_list'
+
+test_expect_success 'snapshot: short branch name (main)' '
+ gitweb_run "p=.git;a=snapshot;h=main;sf=tar" &&
+ ID=$(git rev-parse --verify --short=7 main) &&
+ check_snapshot ".git-main-$ID"
+'
+test_debug 'cat gitweb.headers && cat file_list'
+
+test_expect_success 'snapshot: short tag name (first)' '
+ gitweb_run "p=.git;a=snapshot;h=first;sf=tar" &&
+ ID=$(git rev-parse --verify --short=7 first) &&
+ check_snapshot ".git-first-$ID"
+'
+test_debug 'cat gitweb.headers && cat file_list'
+
+test_expect_success 'snapshot: full branch name (refs/heads/main)' '
+ gitweb_run "p=.git;a=snapshot;h=refs/heads/main;sf=tar" &&
+ ID=$(git rev-parse --verify --short=7 main) &&
+ check_snapshot ".git-main-$ID"
+'
+test_debug 'cat gitweb.headers && cat file_list'
+
+test_expect_success 'snapshot: full tag name (refs/tags/first)' '
+ gitweb_run "p=.git;a=snapshot;h=refs/tags/first;sf=tar" &&
+ check_snapshot ".git-first"
+'
+test_debug 'cat gitweb.headers && cat file_list'
+
+test_expect_success 'snapshot: hierarchical branch name (xx/test)' '
+ gitweb_run "p=.git;a=snapshot;h=xx/test;sf=tar" &&
+ ! grep "filename=.*/" gitweb.headers
+'
+test_debug 'cat gitweb.headers'
+
+# ----------------------------------------------------------------------
+# forks of projects
+
+test_expect_success 'forks: setup' '
+ git init --bare foo.git &&
+ echo file > file &&
+ git --git-dir=foo.git --work-tree=. add file &&
+ git --git-dir=foo.git --work-tree=. commit -m "Initial commit" &&
+ echo "foo" > foo.git/description &&
+ git clone --bare foo.git foo.bar.git &&
+ echo "foo.bar" > foo.bar.git/description &&
+ git clone --bare foo.git foo_baz.git &&
+ echo "foo_baz" > foo_baz.git/description &&
+ rm -fr foo &&
+ mkdir -p foo &&
+ (
+ cd foo &&
+ git clone --shared --bare ../foo.git foo-forked.git &&
+ echo "fork of foo" > foo-forked.git/description
+ )
+'
+
+test_expect_success 'forks: not skipped unless "forks" feature enabled' '
+ gitweb_run "a=project_list" &&
+ grep -q ">\\.git<" gitweb.body &&
+ grep -q ">foo\\.git<" gitweb.body &&
+ grep -q ">foo_baz\\.git<" gitweb.body &&
+ grep -q ">foo\\.bar\\.git<" gitweb.body &&
+ grep -q ">foo_baz\\.git<" gitweb.body &&
+ grep -q ">foo/foo-forked\\.git<" gitweb.body &&
+ grep -q ">fork of .*<" gitweb.body
+'
+
+test_expect_success 'enable forks feature' '
+ cat >>gitweb_config.perl <<-\EOF
+ $feature{"forks"}{"default"} = [1];
+ EOF
+'
+
+test_expect_success 'forks: forks skipped if "forks" feature enabled' '
+ gitweb_run "a=project_list" &&
+ grep -q ">\\.git<" gitweb.body &&
+ grep -q ">foo\\.git<" gitweb.body &&
+ grep -q ">foo_baz\\.git<" gitweb.body &&
+ grep -q ">foo\\.bar\\.git<" gitweb.body &&
+ grep -q ">foo_baz\\.git<" gitweb.body &&
+ grep -v ">foo/foo-forked\\.git<" gitweb.body &&
+ grep -v ">fork of .*<" gitweb.body
+'
+
+test_expect_success 'forks: "forks" action for forked repository' '
+ gitweb_run "p=foo.git;a=forks" &&
+ grep -q ">foo/foo-forked\\.git<" gitweb.body &&
+ grep -q ">fork of foo<" gitweb.body
+'
+
+test_expect_success 'forks: can access forked repository' '
+ gitweb_run "p=foo/foo-forked.git;a=summary" &&
+ grep -q "200 OK" gitweb.headers &&
+ grep -q ">fork of foo<" gitweb.body
+'
+
+test_expect_success 'forks: project_index lists all projects (incl. forks)' '
+ cat >expected <<-\EOF &&
+ .git
+ foo.bar.git
+ foo.git
+ foo/foo-forked.git
+ foo_baz.git
+ EOF
+ gitweb_run "a=project_index" &&
+ sed -e "s/ .*//" <gitweb.body | sort >actual &&
+ test_cmp expected actual
+'
+
+xss() {
+ echo >&2 "Checking $*..." &&
+ gitweb_run "$@" &&
+ if grep "$TAG" gitweb.body; then
+ echo >&2 "xss: $TAG should have been quoted in output"
+ return 1
+ fi
+ return 0
+}
+
+test_expect_success 'xss checks' '
+ TAG="<magic-xss-tag>" &&
+ xss "a=rss&p=$TAG" &&
+ xss "a=rss&p=foo.git&f=$TAG" &&
+ xss "" "$TAG+"
+'
+
+no_http_equiv_content_type() {
+ gitweb_run "$@" &&
+ ! grep -E "http-equiv=['\"]?content-type" gitweb.body
+}
+
+# See: <https://html.spec.whatwg.org/dev/semantics.html#attr-meta-http-equiv-content-type>
+test_expect_success 'no http-equiv="content-type" in XHTML' '
+ no_http_equiv_content_type &&
+ no_http_equiv_content_type "p=.git" &&
+ no_http_equiv_content_type "p=.git;a=log" &&
+ no_http_equiv_content_type "p=.git;a=tree"
+'
+
+proper_doctype() {
+ gitweb_run "$@" &&
+ grep -F "<!DOCTYPE html [" gitweb.body &&
+ grep "<!ENTITY nbsp" gitweb.body &&
+ grep "<!ENTITY sdot" gitweb.body
+}
+
+test_expect_success 'Proper DOCTYPE with entity declarations' '
+ proper_doctype &&
+ proper_doctype "p=.git" &&
+ proper_doctype "p=.git;a=log" &&
+ proper_doctype "p=.git;a=tree"
+'
+
+test_done
diff --git a/t/t9600-cvsimport.sh b/t/t9600-cvsimport.sh
new file mode 100755
index 0000000..5680849
--- /dev/null
+++ b/t/t9600-cvsimport.sh
@@ -0,0 +1,167 @@
+#!/bin/sh
+
+test_description='git cvsimport basic tests'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./lib-cvs.sh
+
+if ! test_have_prereq NOT_ROOT; then
+ skip_all='When cvs is compiled with CVS_BADROOT commits as root fail'
+ test_done
+fi
+
+test_expect_success PERL 'setup cvsroot environment' '
+ CVSROOT=$(pwd)/cvsroot &&
+ export CVSROOT
+'
+
+test_expect_success PERL 'setup cvsroot' '$CVS init'
+
+test_expect_success PERL 'setup a cvs module' '
+
+ mkdir "$CVSROOT/module" &&
+ $CVS co -d module-cvs module &&
+ (cd module-cvs &&
+ cat <<EOF >o_fortuna &&
+O Fortuna
+velut luna
+statu variabilis,
+
+semper crescis
+aut decrescis;
+vita detestabilis
+
+nunc obdurat
+et tunc curat
+ludo mentis aciem,
+
+egestatem,
+potestatem
+dissolvit ut glaciem.
+EOF
+ $CVS add o_fortuna &&
+ cat <<EOF >message &&
+add "O Fortuna" lyrics
+
+These public domain lyrics make an excellent sample text.
+EOF
+ $CVS commit -F message
+ )
+'
+
+test_expect_success PERL 'import a trivial module' '
+
+ git cvsimport -a -R -z 0 -C module-git module &&
+ test_cmp module-cvs/o_fortuna module-git/o_fortuna
+
+'
+
+test_expect_success PERL 'pack refs' '(cd module-git && git gc)'
+
+test_expect_success PERL 'initial import has correct .git/cvs-revisions' '
+
+ (cd module-git &&
+ git log --format="o_fortuna 1.1 %H" -1) > expected &&
+ test_cmp expected module-git/.git/cvs-revisions
+'
+
+test_expect_success PERL 'update cvs module' '
+ (cd module-cvs &&
+ cat <<EOF >o_fortuna &&
+O Fortune,
+like the moon
+you are changeable,
+
+ever waxing
+and waning;
+hateful life
+
+first oppresses
+and then soothes
+as fancy takes it;
+
+poverty
+and power
+it melts them like ice.
+EOF
+ cat <<EOF >message &&
+translate to English
+
+My Latin is terrible.
+EOF
+ $CVS commit -F message
+ )
+'
+
+test_expect_success PERL 'update git module' '
+
+ (cd module-git &&
+ git config cvsimport.trackRevisions true &&
+ git cvsimport -a -z 0 module &&
+ git merge origin
+ ) &&
+ test_cmp module-cvs/o_fortuna module-git/o_fortuna
+
+'
+
+test_expect_success PERL 'update has correct .git/cvs-revisions' '
+
+ (cd module-git &&
+ git log --format="o_fortuna 1.1 %H" -1 HEAD^ &&
+ git log --format="o_fortuna 1.2 %H" -1 HEAD) > expected &&
+ test_cmp expected module-git/.git/cvs-revisions
+'
+
+test_expect_success PERL 'update cvs module' '
+
+ (cd module-cvs &&
+ echo 1 >tick &&
+ $CVS add tick &&
+ $CVS commit -m 1
+ )
+'
+
+test_expect_success PERL 'cvsimport.module config works' '
+
+ (cd module-git &&
+ git config cvsimport.module module &&
+ git config cvsimport.trackRevisions true &&
+ git cvsimport -a -z0 &&
+ git merge origin
+ ) &&
+ test_cmp module-cvs/tick module-git/tick
+
+'
+
+test_expect_success PERL 'second update has correct .git/cvs-revisions' '
+
+ (cd module-git &&
+ git log --format="o_fortuna 1.1 %H" -1 HEAD^^ &&
+ git log --format="o_fortuna 1.2 %H" -1 HEAD^ &&
+ git log --format="tick 1.1 %H" -1 HEAD) > expected &&
+ test_cmp expected module-git/.git/cvs-revisions
+'
+
+test_expect_success PERL 'import from a CVS working tree' '
+
+ $CVS co -d import-from-wt module &&
+ (cd import-from-wt &&
+ git config cvsimport.trackRevisions false &&
+ git cvsimport -a -z0 &&
+ echo 1 >expect &&
+ git log -1 --pretty=format:%s%n >actual &&
+ test_cmp expect actual
+ )
+
+'
+
+test_expect_success PERL 'no .git/cvs-revisions created by default' '
+
+ ! test -e import-from-wt/.git/cvs-revisions
+
+'
+
+test_expect_success PERL 'test entire HEAD' 'test_cmp_branch_tree main'
+
+test_done
diff --git a/t/t9601-cvsimport-vendor-branch.sh b/t/t9601-cvsimport-vendor-branch.sh
new file mode 100755
index 0000000..116cddb
--- /dev/null
+++ b/t/t9601-cvsimport-vendor-branch.sh
@@ -0,0 +1,88 @@
+#!/bin/sh
+
+# Description of the files in the repository:
+#
+# imported-once.txt:
+#
+# Imported once. 1.1 and 1.1.1.1 should be identical.
+#
+# imported-twice.txt:
+#
+# Imported twice. HEAD should reflect the contents of the
+# second import (i.e., have the same contents as 1.1.1.2).
+#
+# imported-modified.txt:
+#
+# Imported, then modified on HEAD. HEAD should reflect the
+# modification.
+#
+# imported-modified-imported.txt:
+#
+# Imported, then modified on HEAD, then imported again.
+#
+# added-imported.txt,v:
+#
+# Added with 'cvs add' to create 1.1, then imported with
+# completely different contents to create 1.1.1.1, therefore the
+# vendor branch was never the default branch.
+#
+# imported-anonymously.txt:
+#
+# Like imported-twice.txt, but with a vendor branch whose branch
+# tag has been removed.
+
+test_description='git cvsimport handling of vendor branches'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./lib-cvs.sh
+
+setup_cvs_test_repository t9601
+
+test_expect_success PERL 'import a module with a vendor branch' '
+
+ git cvsimport -C module-git module
+
+'
+
+test_expect_success PERL 'check HEAD out of cvs repository' 'test_cvs_co main'
+
+test_expect_success PERL 'check main out of git repository' 'test_git_co main'
+
+test_expect_success PERL 'check a file that was imported once' '
+
+ test_cmp_branch_file main imported-once.txt
+
+'
+
+test_expect_failure PERL 'check a file that was imported twice' '
+
+ test_cmp_branch_file main imported-twice.txt
+
+'
+
+test_expect_success PERL 'check a file that was imported then modified on HEAD' '
+
+ test_cmp_branch_file main imported-modified.txt
+
+'
+
+test_expect_success PERL 'check a file that was imported, modified, then imported again' '
+
+ test_cmp_branch_file main imported-modified-imported.txt
+
+'
+
+test_expect_success PERL 'check a file that was added to HEAD then imported' '
+
+ test_cmp_branch_file main added-imported.txt
+
+'
+
+test_expect_success PERL 'a vendor branch whose tag has been removed' '
+
+ test_cmp_branch_file main imported-anonymously.txt
+
+'
+
+test_done
diff --git a/t/t9601/cvsroot/.gitattributes b/t/t9601/cvsroot/.gitattributes
new file mode 100644
index 0000000..562b12e
--- /dev/null
+++ b/t/t9601/cvsroot/.gitattributes
@@ -0,0 +1 @@
+* -whitespace
diff --git a/t/t9601/cvsroot/CVSROOT/.gitignore b/t/t9601/cvsroot/CVSROOT/.gitignore
new file mode 100644
index 0000000..3bb9b34
--- /dev/null
+++ b/t/t9601/cvsroot/CVSROOT/.gitignore
@@ -0,0 +1,2 @@
+history
+val-tags
diff --git a/t/t9601/cvsroot/module/added-imported.txt,v b/t/t9601/cvsroot/module/added-imported.txt,v
new file mode 100644
index 0000000..5f83072
--- /dev/null
+++ b/t/t9601/cvsroot/module/added-imported.txt,v
@@ -0,0 +1,44 @@
+head 1.1;
+access;
+symbols
+ vtag-4:1.1.1.1
+ vbranchA:1.1.1;
+locks; strict;
+comment @# @;
+
+
+1.1
+date 2004.02.09.15.43.15; author kfogel; state Exp;
+branches
+ 1.1.1.1;
+next ;
+
+1.1.1.1
+date 2004.02.09.15.43.16; author kfogel; state Exp;
+branches;
+next ;
+
+
+desc
+@@
+
+
+1.1
+log
+@Add a file to the working copy.
+@
+text
+@Adding this file, before importing it with different contents.
+@
+
+
+1.1.1.1
+log
+@Import (vbranchA, vtag-4).
+@
+text
+@d1 1
+a1 1
+This is vtag-4 (on vbranchA) of added-then-imported.txt.
+@
+
diff --git a/t/t9601/cvsroot/module/imported-anonymously.txt,v b/t/t9601/cvsroot/module/imported-anonymously.txt,v
new file mode 100644
index 0000000..55e1b0c
--- /dev/null
+++ b/t/t9601/cvsroot/module/imported-anonymously.txt,v
@@ -0,0 +1,42 @@
+head 1.1;
+branch 1.1.1;
+access;
+symbols
+ vtag-1:1.1.1.1;
+locks; strict;
+comment @# @;
+
+
+1.1
+date 2004.02.09.15.43.13; author kfogel; state Exp;
+branches
+ 1.1.1.1;
+next ;
+
+1.1.1.1
+date 2004.02.09.15.43.13; author kfogel; state Exp;
+branches;
+next ;
+
+
+desc
+@@
+
+
+1.1
+log
+@Initial revision
+@
+text
+@This is vtag-1 (on vbranchA) of imported-anonymously.txt.
+@
+
+
+1.1.1.1
+log
+@Import (vbranchA, vtag-1).
+@
+text
+@@
+
+
diff --git a/t/t9601/cvsroot/module/imported-modified-imported.txt,v b/t/t9601/cvsroot/module/imported-modified-imported.txt,v
new file mode 100644
index 0000000..e5830ae
--- /dev/null
+++ b/t/t9601/cvsroot/module/imported-modified-imported.txt,v
@@ -0,0 +1,76 @@
+head 1.2;
+access;
+symbols
+ vtag-2:1.1.1.2
+ vtag-1:1.1.1.1
+ vbranchA:1.1.1;
+locks; strict;
+comment @# @;
+
+
+1.2
+date 2004.02.09.15.43.14; author kfogel; state Exp;
+branches;
+next 1.1;
+
+1.1
+date 2004.02.09.15.43.13; author kfogel; state Exp;
+branches
+ 1.1.1.1;
+next ;
+
+1.1.1.1
+date 2004.02.09.15.43.13; author kfogel; state Exp;
+branches;
+next 1.1.1.2;
+
+1.1.1.2
+date 2004.02.09.15.43.13; author kfogel; state Exp;
+branches;
+next ;
+
+
+desc
+@@
+
+
+1.2
+log
+@First regular commit, to imported-modified-imported.txt, on HEAD.
+@
+text
+@This is a modification of imported-modified-imported.txt on HEAD.
+It should supersede the version from the vendor branch.
+@
+
+
+1.1
+log
+@Initial revision
+@
+text
+@d1 2
+a2 1
+This is vtag-1 (on vbranchA) of imported-modified-imported.txt.
+@
+
+
+1.1.1.1
+log
+@Import (vbranchA, vtag-1).
+@
+text
+@@
+
+
+1.1.1.2
+log
+@Import (vbranchA, vtag-2).
+@
+text
+@d1 1
+a1 1
+This is vtag-2 (on vbranchA) of imported-modified-imported.txt.
+@
+
+
diff --git a/t/t9601/cvsroot/module/imported-modified.txt,v b/t/t9601/cvsroot/module/imported-modified.txt,v
new file mode 100644
index 0000000..bbcfe44
--- /dev/null
+++ b/t/t9601/cvsroot/module/imported-modified.txt,v
@@ -0,0 +1,59 @@
+head 1.2;
+access;
+symbols
+ vtag-1:1.1.1.1
+ vbranchA:1.1.1;
+locks; strict;
+comment @# @;
+
+
+1.2
+date 2004.02.09.15.43.14; author kfogel; state Exp;
+branches;
+next 1.1;
+
+1.1
+date 2004.02.09.15.43.13; author kfogel; state Exp;
+branches
+ 1.1.1.1;
+next ;
+
+1.1.1.1
+date 2004.02.09.15.43.13; author kfogel; state Exp;
+branches;
+next ;
+
+
+desc
+@@
+
+
+1.2
+log
+@Commit on HEAD.
+@
+text
+@This is a modification of imported-modified.txt on HEAD.
+It should supersede the version from the vendor branch.
+@
+
+
+1.1
+log
+@Initial revision
+@
+text
+@d1 2
+a2 1
+This is vtag-1 (on vbranchA) of imported-modified.txt.
+@
+
+
+1.1.1.1
+log
+@Import (vbranchA, vtag-1).
+@
+text
+@@
+
+
diff --git a/t/t9601/cvsroot/module/imported-once.txt,v b/t/t9601/cvsroot/module/imported-once.txt,v
new file mode 100644
index 0000000..c5dd82b
--- /dev/null
+++ b/t/t9601/cvsroot/module/imported-once.txt,v
@@ -0,0 +1,43 @@
+head 1.1;
+branch 1.1.1;
+access;
+symbols
+ vtag-1:1.1.1.1
+ vbranchA:1.1.1;
+locks; strict;
+comment @# @;
+
+
+1.1
+date 2004.02.09.15.43.13; author kfogel; state Exp;
+branches
+ 1.1.1.1;
+next ;
+
+1.1.1.1
+date 2004.02.09.15.43.13; author kfogel; state Exp;
+branches;
+next ;
+
+
+desc
+@@
+
+
+1.1
+log
+@Initial revision
+@
+text
+@This is vtag-1 (on vbranchA) of imported-once.txt.
+@
+
+
+1.1.1.1
+log
+@Import (vbranchA, vtag-1).
+@
+text
+@@
+
+
diff --git a/t/t9601/cvsroot/module/imported-twice.txt,v b/t/t9601/cvsroot/module/imported-twice.txt,v
new file mode 100644
index 0000000..d1f3f1b
--- /dev/null
+++ b/t/t9601/cvsroot/module/imported-twice.txt,v
@@ -0,0 +1,60 @@
+head 1.1;
+branch 1.1.1;
+access;
+symbols
+ vtag-2:1.1.1.2
+ vtag-1:1.1.1.1
+ vbranchA:1.1.1;
+locks; strict;
+comment @# @;
+
+
+1.1
+date 2004.02.09.15.43.13; author kfogel; state Exp;
+branches
+ 1.1.1.1;
+next ;
+
+1.1.1.1
+date 2004.02.09.15.43.13; author kfogel; state Exp;
+branches;
+next 1.1.1.2;
+
+1.1.1.2
+date 2004.02.09.15.43.13; author kfogel; state Exp;
+branches;
+next ;
+
+
+desc
+@@
+
+
+1.1
+log
+@Initial revision
+@
+text
+@This is vtag-1 (on vbranchA) of imported-twice.txt.
+@
+
+
+1.1.1.1
+log
+@Import (vbranchA, vtag-1).
+@
+text
+@@
+
+
+1.1.1.2
+log
+@Import (vbranchA, vtag-2).
+@
+text
+@d1 1
+a1 1
+This is vtag-2 (on vbranchA) of imported-twice.txt.
+@
+
+
diff --git a/t/t9602-cvsimport-branches-tags.sh b/t/t9602-cvsimport-branches-tags.sh
new file mode 100755
index 0000000..e5266c9
--- /dev/null
+++ b/t/t9602-cvsimport-branches-tags.sh
@@ -0,0 +1,81 @@
+#!/bin/sh
+
+# A description of the repository used for this test can be found in
+# t9602/README.
+
+test_description='git cvsimport handling of branches and tags'
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./lib-cvs.sh
+
+setup_cvs_test_repository t9602
+
+test_expect_success PERL 'import module' '
+
+ git cvsimport -C module-git module
+
+'
+
+test_expect_success PERL 'test branch main' '
+
+ test_cmp_branch_tree main
+
+'
+
+test_expect_success PERL 'test branch vendorbranch' '
+
+ test_cmp_branch_tree vendorbranch
+
+'
+
+test_expect_failure PERL 'test branch B_FROM_INITIALS' '
+
+ test_cmp_branch_tree B_FROM_INITIALS
+
+'
+
+test_expect_failure PERL 'test branch B_FROM_INITIALS_BUT_ONE' '
+
+ test_cmp_branch_tree B_FROM_INITIALS_BUT_ONE
+
+'
+
+test_expect_failure PERL 'test branch B_MIXED' '
+
+ test_cmp_branch_tree B_MIXED
+
+'
+
+test_expect_success PERL 'test branch B_SPLIT' '
+
+ test_cmp_branch_tree B_SPLIT
+
+'
+
+test_expect_failure PERL 'test tag vendortag' '
+
+ test_cmp_branch_tree vendortag
+
+'
+
+test_expect_success PERL 'test tag T_ALL_INITIAL_FILES' '
+
+ test_cmp_branch_tree T_ALL_INITIAL_FILES
+
+'
+
+test_expect_failure PERL 'test tag T_ALL_INITIAL_FILES_BUT_ONE' '
+
+ test_cmp_branch_tree T_ALL_INITIAL_FILES_BUT_ONE
+
+'
+
+test_expect_failure PERL 'test tag T_MIXED' '
+
+ test_cmp_branch_tree T_MIXED
+
+'
+
+
+test_done
diff --git a/t/t9602/README b/t/t9602/README
new file mode 100644
index 0000000..c231e0f
--- /dev/null
+++ b/t/t9602/README
@@ -0,0 +1,62 @@
+This repository is for testing the ability to group revisions
+correctly along tags and branches. Here is its history:
+
+ 1. The initial import (revision 1.1 of everybody) created a
+ directory structure with a file named `default' in each dir:
+
+ ./
+ default
+ sub1/default
+ subsubA/default
+ subsubB/default
+ sub2/default
+ subsubA/default
+ sub3/default
+
+ 2. Then tagged everyone with T_ALL_INITIAL_FILES.
+
+ 3. Then tagged everyone except sub1/subsubB/default with
+ T_ALL_INITIAL_FILES_BUT_ONE.
+
+ 4. Then created branch B_FROM_INITIALS on everyone.
+
+ 5. Then created branch B_FROM_INITIALS_BUT_ONE on everyone except
+ /sub1/subsubB/default.
+
+ 6. Then committed modifications to two files: sub3/default, and
+ sub1/subsubA/default.
+
+ 7. Then committed a modification to all 7 files.
+
+ 8. Then backdated sub3/default to revision 1.2, and
+ sub2/subsubA/default to revision 1.1, and tagged with T_MIXED.
+
+ 9. Same as 8, but tagged with -b to create branch B_MIXED.
+
+ 10. Switched the working copy to B_MIXED, and added
+ sub2/branch_B_MIXED_only. (That's why the RCS file is in
+ sub2/Attic/ -- it never existed on trunk.)
+
+ 11. In one commit, modified default, sub1/default, and
+ sub2/subsubA/default, on branch B_MIXED.
+
+ 12. Did "cvs up -A" on sub2/default, then in one commit, made a
+ change to sub2/default and sub2/branch_B_MIXED_only. So this
+ commit should be spread between the branch and the trunk.
+
+ 13. Do "cvs up -A" to get everyone back to trunk, then make a new
+ branch B_SPLIT on everyone except sub1/subsubB/default,v.
+
+ 14. Switch to branch B_SPLIT (see sub1/subsubB/default disappear)
+ and commit a change that affects everyone except sub3/default.
+
+ 15. An hour or so later, "cvs up -A" to get sub1/subsubB/default
+ back, then commit a change on that file, on trunk. (It's
+ important that this change happened after the previous commits
+ on B_SPLIT.)
+
+ 16. Branch sub1/subsubB/default to B_SPLIT, then "cvs up -r B_SPLIT"
+ to switch the whole working copy to the branch.
+
+ 17. Commit a change on B_SPLIT, to sub1/subsubB/default and
+ sub3/default.
diff --git a/t/t9602/cvsroot/.gitattributes b/t/t9602/cvsroot/.gitattributes
new file mode 100644
index 0000000..562b12e
--- /dev/null
+++ b/t/t9602/cvsroot/.gitattributes
@@ -0,0 +1 @@
+* -whitespace
diff --git a/t/t9602/cvsroot/CVSROOT/.gitignore b/t/t9602/cvsroot/CVSROOT/.gitignore
new file mode 100644
index 0000000..3bb9b34
--- /dev/null
+++ b/t/t9602/cvsroot/CVSROOT/.gitignore
@@ -0,0 +1,2 @@
+history
+val-tags
diff --git a/t/t9602/cvsroot/module/default,v b/t/t9602/cvsroot/module/default,v
new file mode 100644
index 0000000..3b68382
--- /dev/null
+++ b/t/t9602/cvsroot/module/default,v
@@ -0,0 +1,102 @@
+head 1.2;
+access;
+symbols
+ B_SPLIT:1.2.0.4
+ B_MIXED:1.2.0.2
+ T_MIXED:1.2
+ B_FROM_INITIALS_BUT_ONE:1.1.1.1.0.4
+ B_FROM_INITIALS:1.1.1.1.0.2
+ T_ALL_INITIAL_FILES_BUT_ONE:1.1.1.1
+ T_ALL_INITIAL_FILES:1.1.1.1
+ vendortag:1.1.1.1
+ vendorbranch:1.1.1;
+locks; strict;
+comment @# @;
+
+
+1.2
+date 2003.05.23.00.17.53; author jrandom; state Exp;
+branches
+ 1.2.2.1
+ 1.2.4.1;
+next 1.1;
+
+1.1
+date 2003.05.22.23.20.19; author jrandom; state Exp;
+branches
+ 1.1.1.1;
+next ;
+
+1.1.1.1
+date 2003.05.22.23.20.19; author jrandom; state Exp;
+branches;
+next ;
+
+1.2.2.1
+date 2003.05.23.00.31.36; author jrandom; state Exp;
+branches;
+next ;
+
+1.2.4.1
+date 2003.06.03.03.20.31; author jrandom; state Exp;
+branches;
+next ;
+
+
+desc
+@@
+
+
+1.2
+log
+@Second commit to proj, affecting all 7 files.
+@
+text
+@This is the file `default' in the top level of the project.
+
+Every directory in the `proj' project has a file named `default'.
+
+This line was added in the second commit (affecting all 7 files).
+@
+
+
+1.2.4.1
+log
+@First change on branch B_SPLIT.
+
+This change excludes sub3/default, because it was not part of this
+commit, and sub1/subsubB/default, which is not even on the branch yet.
+@
+text
+@a5 2
+
+First change on branch B_SPLIT.
+@
+
+
+1.2.2.1
+log
+@Modify three files, on branch B_MIXED.
+@
+text
+@a5 2
+
+This line was added on branch B_MIXED only (affecting 3 files).
+@
+
+
+1.1
+log
+@Initial revision
+@
+text
+@d4 2
+@
+
+
+1.1.1.1
+log
+@Initial import.
+@
+text
+@@
diff --git a/t/t9602/cvsroot/module/sub1/default,v b/t/t9602/cvsroot/module/sub1/default,v
new file mode 100644
index 0000000..b7fdccd
--- /dev/null
+++ b/t/t9602/cvsroot/module/sub1/default,v
@@ -0,0 +1,102 @@
+head 1.2;
+access;
+symbols
+ B_SPLIT:1.2.0.4
+ B_MIXED:1.2.0.2
+ T_MIXED:1.2
+ B_FROM_INITIALS_BUT_ONE:1.1.1.1.0.4
+ B_FROM_INITIALS:1.1.1.1.0.2
+ T_ALL_INITIAL_FILES_BUT_ONE:1.1.1.1
+ T_ALL_INITIAL_FILES:1.1.1.1
+ vendortag:1.1.1.1
+ vendorbranch:1.1.1;
+locks; strict;
+comment @# @;
+
+
+1.2
+date 2003.05.23.00.17.53; author jrandom; state Exp;
+branches
+ 1.2.2.1
+ 1.2.4.1;
+next 1.1;
+
+1.1
+date 2003.05.22.23.20.19; author jrandom; state Exp;
+branches
+ 1.1.1.1;
+next ;
+
+1.1.1.1
+date 2003.05.22.23.20.19; author jrandom; state Exp;
+branches;
+next ;
+
+1.2.2.1
+date 2003.05.23.00.31.36; author jrandom; state Exp;
+branches;
+next ;
+
+1.2.4.1
+date 2003.06.03.03.20.31; author jrandom; state Exp;
+branches;
+next ;
+
+
+desc
+@@
+
+
+1.2
+log
+@Second commit to proj, affecting all 7 files.
+@
+text
+@This is sub1/default.
+
+Every directory in the `proj' project has a file named `default'.
+
+This line was added in the second commit (affecting all 7 files).
+@
+
+
+1.2.4.1
+log
+@First change on branch B_SPLIT.
+
+This change excludes sub3/default, because it was not part of this
+commit, and sub1/subsubB/default, which is not even on the branch yet.
+@
+text
+@a5 2
+
+First change on branch B_SPLIT.
+@
+
+
+1.2.2.1
+log
+@Modify three files, on branch B_MIXED.
+@
+text
+@a5 2
+
+This line was added on branch B_MIXED only (affecting 3 files).
+@
+
+
+1.1
+log
+@Initial revision
+@
+text
+@d4 2
+@
+
+
+1.1.1.1
+log
+@Initial import.
+@
+text
+@@
diff --git a/t/t9602/cvsroot/module/sub1/subsubA/default,v b/t/t9602/cvsroot/module/sub1/subsubA/default,v
new file mode 100644
index 0000000..472b7b2
--- /dev/null
+++ b/t/t9602/cvsroot/module/sub1/subsubA/default,v
@@ -0,0 +1,101 @@
+head 1.3;
+access;
+symbols
+ B_SPLIT:1.3.0.4
+ B_MIXED:1.3.0.2
+ T_MIXED:1.3
+ B_FROM_INITIALS_BUT_ONE:1.1.1.1.0.4
+ B_FROM_INITIALS:1.1.1.1.0.2
+ T_ALL_INITIAL_FILES_BUT_ONE:1.1.1.1
+ T_ALL_INITIAL_FILES:1.1.1.1
+ vendortag:1.1.1.1
+ vendorbranch:1.1.1;
+locks; strict;
+comment @# @;
+
+
+1.3
+date 2003.05.23.00.17.53; author jrandom; state Exp;
+branches
+ 1.3.4.1;
+next 1.2;
+
+1.2
+date 2003.05.23.00.15.26; author jrandom; state Exp;
+branches;
+next 1.1;
+
+1.1
+date 2003.05.22.23.20.19; author jrandom; state Exp;
+branches
+ 1.1.1.1;
+next ;
+
+1.1.1.1
+date 2003.05.22.23.20.19; author jrandom; state Exp;
+branches;
+next ;
+
+1.3.4.1
+date 2003.06.03.03.20.31; author jrandom; state Exp;
+branches;
+next ;
+
+
+desc
+@@
+
+
+1.3
+log
+@Second commit to proj, affecting all 7 files.
+@
+text
+@This is sub1/subsubA/default.
+
+Every directory in the `proj' project has a file named `default'.
+
+This line was added by the first commit (affecting two files).
+
+This line was added in the second commit (affecting all 7 files).
+@
+
+
+1.3.4.1
+log
+@First change on branch B_SPLIT.
+
+This change excludes sub3/default, because it was not part of this
+commit, and sub1/subsubB/default, which is not even on the branch yet.
+@
+text
+@a7 2
+
+First change on branch B_SPLIT.
+@
+
+
+1.2
+log
+@First commit to proj, affecting two files.
+@
+text
+@d6 2
+@
+
+
+1.1
+log
+@Initial revision
+@
+text
+@d4 2
+@
+
+
+1.1.1.1
+log
+@Initial import.
+@
+text
+@@
diff --git a/t/t9602/cvsroot/module/sub1/subsubB/default,v b/t/t9602/cvsroot/module/sub1/subsubB/default,v
new file mode 100644
index 0000000..fe6efa4
--- /dev/null
+++ b/t/t9602/cvsroot/module/sub1/subsubB/default,v
@@ -0,0 +1,107 @@
+head 1.3;
+access;
+symbols
+ B_SPLIT:1.3.0.2
+ B_MIXED:1.2.0.2
+ T_MIXED:1.2
+ B_FROM_INITIALS:1.1.1.1.0.2
+ T_ALL_INITIAL_FILES:1.1.1.1
+ vendortag:1.1.1.1
+ vendorbranch:1.1.1;
+locks; strict;
+comment @# @;
+
+
+1.3
+date 2003.06.03.04.29.14; author jrandom; state Exp;
+branches
+ 1.3.2.1;
+next 1.2;
+
+1.2
+date 2003.05.23.00.17.53; author jrandom; state Exp;
+branches;
+next 1.1;
+
+1.1
+date 2003.05.22.23.20.19; author jrandom; state Exp;
+branches
+ 1.1.1.1;
+next ;
+
+1.1.1.1
+date 2003.05.22.23.20.19; author jrandom; state Exp;
+branches;
+next ;
+
+1.3.2.1
+date 2003.06.03.04.33.13; author jrandom; state Exp;
+branches;
+next ;
+
+
+desc
+@@
+
+
+1.3
+log
+@A trunk change to sub1/subsubB/default. This was committed about an
+hour after an earlier change that affected most files on branch
+B_SPLIT. This file is not on that branch yet, but after this commit,
+we'll branch to B_SPLIT, albeit rooted in a revision that didn't exist
+at the time the rest of B_SPLIT was created.
+@
+text
+@This is sub1/subsubB/default.
+
+Every directory in the `proj' project has a file named `default'.
+
+This line was added in the second commit (affecting all 7 files).
+
+This bit was committed on trunk about an hour after an earlier change
+to everyone else on branch B_SPLIT. Afterwards, we'll finally branch
+this file to B_SPLIT, but rooted in a revision that didn't exist at
+the time the rest of B_SPLIT was created.
+@
+
+
+1.3.2.1
+log
+@This change affects sub3/default and sub1/subsubB/default, on branch
+B_SPLIT. Note that the latter file did not even exist on this branch
+until after some other files had had revisions committed on B_SPLIT.
+@
+text
+@a10 4
+
+This change affects sub3/default and sub1/subsubB/default, on branch
+B_SPLIT. Note that the latter file did not even exist on this branch
+until after some other files had had revisions committed on B_SPLIT.
+@
+
+
+1.2
+log
+@Second commit to proj, affecting all 7 files.
+@
+text
+@d6 5
+@
+
+
+1.1
+log
+@Initial revision
+@
+text
+@d4 2
+@
+
+
+1.1.1.1
+log
+@Initial import.
+@
+text
+@@
diff --git a/t/t9602/cvsroot/module/sub2/Attic/branch_B_MIXED_only,v b/t/t9602/cvsroot/module/sub2/Attic/branch_B_MIXED_only,v
new file mode 100644
index 0000000..34c9789
--- /dev/null
+++ b/t/t9602/cvsroot/module/sub2/Attic/branch_B_MIXED_only,v
@@ -0,0 +1,59 @@
+head 1.1;
+access;
+symbols
+ B_MIXED:1.1.0.2;
+locks; strict;
+comment @# @;
+
+
+1.1
+date 2003.05.23.00.25.26; author jrandom; state dead;
+branches
+ 1.1.2.1;
+next ;
+
+1.1.2.1
+date 2003.05.23.00.25.26; author jrandom; state Exp;
+branches;
+next 1.1.2.2;
+
+1.1.2.2
+date 2003.05.23.00.48.51; author jrandom; state Exp;
+branches;
+next ;
+
+
+desc
+@@
+
+
+1.1
+log
+@file branch_B_MIXED_only was initially added on branch B_MIXED.
+@
+text
+@@
+
+
+1.1.2.1
+log
+@Add a file on branch B_MIXED.
+@
+text
+@a0 1
+This file was added on branch B_MIXED. It never existed on trunk.
+@
+
+
+1.1.2.2
+log
+@A single commit affecting one file on branch B_MIXED and one on trunk.
+@
+text
+@a1 3
+
+The same commit added these two lines here on branch B_MIXED, and two
+similar lines to ./default on trunk.
+@
+
+
diff --git a/t/t9602/cvsroot/module/sub2/default,v b/t/t9602/cvsroot/module/sub2/default,v
new file mode 100644
index 0000000..018f7f8
--- /dev/null
+++ b/t/t9602/cvsroot/module/sub2/default,v
@@ -0,0 +1,102 @@
+head 1.3;
+access;
+symbols
+ B_SPLIT:1.3.0.2
+ B_MIXED:1.2.0.2
+ T_MIXED:1.2
+ B_FROM_INITIALS_BUT_ONE:1.1.1.1.0.4
+ B_FROM_INITIALS:1.1.1.1.0.2
+ T_ALL_INITIAL_FILES_BUT_ONE:1.1.1.1
+ T_ALL_INITIAL_FILES:1.1.1.1
+ vendortag:1.1.1.1
+ vendorbranch:1.1.1;
+locks; strict;
+comment @# @;
+
+
+1.3
+date 2003.05.23.00.48.51; author jrandom; state Exp;
+branches
+ 1.3.2.1;
+next 1.2;
+
+1.2
+date 2003.05.23.00.17.53; author jrandom; state Exp;
+branches;
+next 1.1;
+
+1.1
+date 2003.05.22.23.20.19; author jrandom; state Exp;
+branches
+ 1.1.1.1;
+next ;
+
+1.1.1.1
+date 2003.05.22.23.20.19; author jrandom; state Exp;
+branches;
+next ;
+
+1.3.2.1
+date 2003.06.03.03.20.31; author jrandom; state Exp;
+branches;
+next ;
+
+
+desc
+@@
+
+
+1.3
+log
+@A single commit affecting one file on branch B_MIXED and one on trunk.
+@
+text
+@This is sub2/default.
+
+Every directory in the `proj' project has a file named `default'.
+
+This line was added in the second commit (affecting all 7 files).
+
+The same commit added these two lines here on trunk, and two similar
+lines to ./branch_B_MIXED_only on branch B_MIXED.
+@
+
+
+1.3.2.1
+log
+@First change on branch B_SPLIT.
+
+This change excludes sub3/default, because it was not part of this
+commit, and sub1/subsubB/default, which is not even on the branch yet.
+@
+text
+@a8 2
+
+First change on branch B_SPLIT.
+@
+
+
+1.2
+log
+@Second commit to proj, affecting all 7 files.
+@
+text
+@d6 3
+@
+
+
+1.1
+log
+@Initial revision
+@
+text
+@d4 2
+@
+
+
+1.1.1.1
+log
+@Initial import.
+@
+text
+@@
diff --git a/t/t9602/cvsroot/module/sub2/subsubA/default,v b/t/t9602/cvsroot/module/sub2/subsubA/default,v
new file mode 100644
index 0000000..d13242c
--- /dev/null
+++ b/t/t9602/cvsroot/module/sub2/subsubA/default,v
@@ -0,0 +1,102 @@
+head 1.2;
+access;
+symbols
+ B_SPLIT:1.2.0.2
+ B_MIXED:1.1.0.2
+ T_MIXED:1.1
+ B_FROM_INITIALS_BUT_ONE:1.1.1.1.0.4
+ B_FROM_INITIALS:1.1.1.1.0.2
+ T_ALL_INITIAL_FILES_BUT_ONE:1.1.1.1
+ T_ALL_INITIAL_FILES:1.1.1.1
+ vendortag:1.1.1.1
+ vendorbranch:1.1.1;
+locks; strict;
+comment @# @;
+
+
+1.2
+date 2003.05.23.00.17.53; author jrandom; state Exp;
+branches
+ 1.2.2.1;
+next 1.1;
+
+1.1
+date 2003.05.22.23.20.19; author jrandom; state Exp;
+branches
+ 1.1.1.1
+ 1.1.2.1;
+next ;
+
+1.1.1.1
+date 2003.05.22.23.20.19; author jrandom; state Exp;
+branches;
+next ;
+
+1.1.2.1
+date 2003.05.23.00.31.36; author jrandom; state Exp;
+branches;
+next ;
+
+1.2.2.1
+date 2003.06.03.03.20.31; author jrandom; state Exp;
+branches;
+next ;
+
+
+desc
+@@
+
+
+1.2
+log
+@Second commit to proj, affecting all 7 files.
+@
+text
+@This is sub2/subsub2/default.
+
+Every directory in the `proj' project has a file named `default'.
+
+This line was added in the second commit (affecting all 7 files).
+@
+
+
+1.2.2.1
+log
+@First change on branch B_SPLIT.
+
+This change excludes sub3/default, because it was not part of this
+commit, and sub1/subsubB/default, which is not even on the branch yet.
+@
+text
+@a5 2
+
+First change on branch B_SPLIT.
+@
+
+
+1.1
+log
+@Initial revision
+@
+text
+@d4 2
+@
+
+
+1.1.2.1
+log
+@Modify three files, on branch B_MIXED.
+@
+text
+@a3 2
+
+This line was added on branch B_MIXED only (affecting 3 files).
+@
+
+
+1.1.1.1
+log
+@Initial import.
+@
+text
+@@
diff --git a/t/t9602/cvsroot/module/sub3/default,v b/t/t9602/cvsroot/module/sub3/default,v
new file mode 100644
index 0000000..88e4567
--- /dev/null
+++ b/t/t9602/cvsroot/module/sub3/default,v
@@ -0,0 +1,102 @@
+head 1.3;
+access;
+symbols
+ B_SPLIT:1.3.0.2
+ B_MIXED:1.2.0.2
+ T_MIXED:1.2
+ B_FROM_INITIALS_BUT_ONE:1.1.1.1.0.4
+ B_FROM_INITIALS:1.1.1.1.0.2
+ T_ALL_INITIAL_FILES_BUT_ONE:1.1.1.1
+ T_ALL_INITIAL_FILES:1.1.1.1
+ vendortag:1.1.1.1
+ vendorbranch:1.1.1;
+locks; strict;
+comment @# @;
+
+
+1.3
+date 2003.05.23.00.17.53; author jrandom; state Exp;
+branches
+ 1.3.2.1;
+next 1.2;
+
+1.2
+date 2003.05.23.00.15.26; author jrandom; state Exp;
+branches;
+next 1.1;
+
+1.1
+date 2003.05.22.23.20.19; author jrandom; state Exp;
+branches
+ 1.1.1.1;
+next ;
+
+1.1.1.1
+date 2003.05.22.23.20.19; author jrandom; state Exp;
+branches;
+next ;
+
+1.3.2.1
+date 2003.06.03.04.33.13; author jrandom; state Exp;
+branches;
+next ;
+
+
+desc
+@@
+
+
+1.3
+log
+@Second commit to proj, affecting all 7 files.
+@
+text
+@This is sub3/default.
+
+Every directory in the `proj' project has a file named `default'.
+
+This line was added by the first commit (affecting two files).
+
+This line was added in the second commit (affecting all 7 files).
+@
+
+
+1.3.2.1
+log
+@This change affects sub3/default and sub1/subsubB/default, on branch
+B_SPLIT. Note that the latter file did not even exist on this branch
+until after some other files had had revisions committed on B_SPLIT.
+@
+text
+@a7 4
+
+This change affects sub3/default and sub1/subsubB/default, on branch
+B_SPLIT. Note that the latter file did not even exist on this branch
+until after some other files had had revisions committed on B_SPLIT.
+@
+
+
+1.2
+log
+@First commit to proj, affecting two files.
+@
+text
+@d6 2
+@
+
+
+1.1
+log
+@Initial revision
+@
+text
+@d4 2
+@
+
+
+1.1.1.1
+log
+@Initial import.
+@
+text
+@@
diff --git a/t/t9603-cvsimport-patchsets.sh b/t/t9603-cvsimport-patchsets.sh
new file mode 100755
index 0000000..19f38f7
--- /dev/null
+++ b/t/t9603-cvsimport-patchsets.sh
@@ -0,0 +1,39 @@
+#!/bin/sh
+
+# Structure of the test cvs repository
+#
+# Message File:Content Commit Time
+# Rev 1 a: 1.1 2009-02-21 19:11:43 +0100
+# Rev 2 a: 1.2 b: 1.1 2009-02-21 19:11:14 +0100
+# Rev 3 b: 1.2 2009-02-21 19:11:43 +0100
+#
+# As you can see the commit of Rev 3 has the same time as
+# Rev 1 this leads to a broken import because of a cvsps
+# bug.
+
+test_description='git cvsimport testing for correct patchset estimation'
+. ./lib-cvs.sh
+
+setup_cvs_test_repository t9603
+
+test_expect_failure PERL 'import with criss cross times on revisions' '
+ git cvsimport -p"-x" -C module-git module &&
+ (
+ cd module-git &&
+ git log --pretty=format:%s > ../actual-main &&
+ git log A~2..A --pretty="format:%s %ad" -- > ../actual-A &&
+ echo "" >> ../actual-main &&
+ echo "" >> ../actual-A
+ ) &&
+ echo "Rev 4
+Rev 3
+Rev 2
+Rev 1" > expect-main &&
+ test_cmp expect-main actual-main &&
+
+ echo "Rev 5 Branch A Wed Mar 11 19:09:10 2009 +0000
+Rev 4 Branch A Wed Mar 11 19:03:52 2009 +0000" > expect-A &&
+ test_cmp expect-A actual-A
+'
+
+test_done
diff --git a/t/t9603/cvsroot/.gitattributes b/t/t9603/cvsroot/.gitattributes
new file mode 100644
index 0000000..562b12e
--- /dev/null
+++ b/t/t9603/cvsroot/.gitattributes
@@ -0,0 +1 @@
+* -whitespace
diff --git a/t/t9603/cvsroot/CVSROOT/.gitignore b/t/t9603/cvsroot/CVSROOT/.gitignore
new file mode 100644
index 0000000..3bb9b34
--- /dev/null
+++ b/t/t9603/cvsroot/CVSROOT/.gitignore
@@ -0,0 +1,2 @@
+history
+val-tags
diff --git a/t/t9603/cvsroot/module/a,v b/t/t9603/cvsroot/module/a,v
new file mode 100644
index 0000000..ba8fd5a
--- /dev/null
+++ b/t/t9603/cvsroot/module/a,v
@@ -0,0 +1,74 @@
+head 1.2;
+access;
+symbols
+ A:1.2.0.2;
+locks; strict;
+comment @# @;
+
+
+1.2
+date 2009.02.21.18.11.14; author tester; state Exp;
+branches
+ 1.2.2.1;
+next 1.1;
+
+1.1
+date 2009.02.21.18.11.43; author tester; state Exp;
+branches;
+next ;
+
+1.2.2.1
+date 2009.03.11.19.03.52; author tester; state Exp;
+branches;
+next 1.2.2.2;
+
+1.2.2.2
+date 2009.03.11.19.09.10; author tester; state Exp;
+branches;
+next ;
+
+
+desc
+@@
+
+
+1.2
+log
+@Rev 2
+@
+text
+@1.2
+@
+
+
+1.2.2.1
+log
+@Rev 4 Branch A
+@
+text
+@d1 1
+a1 1
+1.2.2.1
+@
+
+
+1.2.2.2
+log
+@Rev 5 Branch A
+@
+text
+@d1 1
+a1 1
+1.2.2.2
+@
+
+
+1.1
+log
+@Rev 1
+@
+text
+@d1 1
+a1 1
+1.1
+@
diff --git a/t/t9603/cvsroot/module/b,v b/t/t9603/cvsroot/module/b,v
new file mode 100644
index 0000000..d268855
--- /dev/null
+++ b/t/t9603/cvsroot/module/b,v
@@ -0,0 +1,90 @@
+head 1.3;
+access;
+symbols
+ A:1.2.0.2;
+locks; strict;
+comment @# @;
+
+
+1.3
+date 2009.03.11.19.05.08; author tester; state Exp;
+branches;
+next 1.2;
+
+1.2
+date 2009.02.21.18.11.43; author tester; state Exp;
+branches
+ 1.2.2.1;
+next 1.1;
+
+1.1
+date 2009.02.21.18.11.14; author tester; state Exp;
+branches;
+next ;
+
+1.2.2.1
+date 2009.03.11.19.03.52; author tester; state Exp;
+branches;
+next 1.2.2.2;
+
+1.2.2.2
+date 2009.03.11.19.09.10; author tester; state Exp;
+branches;
+next ;
+
+
+desc
+@@
+
+
+1.3
+log
+@Rev 4
+@
+text
+@1.3
+@
+
+
+1.2
+log
+@Rev 3
+@
+text
+@d1 1
+a1 1
+1.2
+@
+
+
+1.2.2.1
+log
+@Rev 4 Branch A
+@
+text
+@d1 1
+a1 1
+1.2.2.1
+@
+
+
+1.2.2.2
+log
+@Rev 5 Branch A
+@
+text
+@d1 1
+a1 1
+1.2
+@
+
+
+1.1
+log
+@Rev 2
+@
+text
+@d1 1
+a1 1
+1.1
+@
diff --git a/t/t9604-cvsimport-timestamps.sh b/t/t9604-cvsimport-timestamps.sh
new file mode 100755
index 0000000..2ff4aa9
--- /dev/null
+++ b/t/t9604-cvsimport-timestamps.sh
@@ -0,0 +1,71 @@
+#!/bin/sh
+
+test_description='git cvsimport timestamps'
+. ./lib-cvs.sh
+
+setup_cvs_test_repository t9604
+
+test_expect_success PERL 'check timestamps are UTC (TZ=CST6CDT)' '
+
+ TZ=CST6CDT git cvsimport -p"-x" -C module-1 module &&
+ git cvsimport -p"-x" -C module-1 module &&
+ (
+ cd module-1 &&
+ git log --format="%s %ai"
+ ) >actual-1 &&
+ cat >expect-1 <<-EOF &&
+ Rev 16 2006-10-29 07:00:01 +0000
+ Rev 15 2006-10-29 06:59:59 +0000
+ Rev 14 2006-04-02 08:00:01 +0000
+ Rev 13 2006-04-02 07:59:59 +0000
+ Rev 12 2005-12-01 00:00:00 +0000
+ Rev 11 2005-11-01 00:00:00 +0000
+ Rev 10 2005-10-01 00:00:00 +0000
+ Rev 9 2005-09-01 00:00:00 +0000
+ Rev 8 2005-08-01 00:00:00 +0000
+ Rev 7 2005-07-01 00:00:00 +0000
+ Rev 6 2005-06-01 00:00:00 +0000
+ Rev 5 2005-05-01 00:00:00 +0000
+ Rev 4 2005-04-01 00:00:00 +0000
+ Rev 3 2005-03-01 00:00:00 +0000
+ Rev 2 2005-02-01 00:00:00 +0000
+ Rev 1 2005-01-01 00:00:00 +0000
+ EOF
+ test_cmp expect-1 actual-1
+'
+
+test_expect_success PERL 'check timestamps with author-specific timezones' '
+
+ cat >cvs-authors <<-EOF &&
+ user1=User One <user1@domain.org>
+ user2=User Two <user2@domain.org> CST6CDT
+ user3=User Three <user3@domain.org> EST5EDT
+ user4=User Four <user4@domain.org> MST7MDT
+ EOF
+ git cvsimport -p"-x" -A cvs-authors -C module-2 module &&
+ (
+ cd module-2 &&
+ git log --format="%s %ai %an"
+ ) >actual-2 &&
+ cat >expect-2 <<-EOF &&
+ Rev 16 2006-10-29 01:00:01 -0600 User Two
+ Rev 15 2006-10-29 01:59:59 -0500 User Two
+ Rev 14 2006-04-02 03:00:01 -0500 User Two
+ Rev 13 2006-04-02 01:59:59 -0600 User Two
+ Rev 12 2005-11-30 17:00:00 -0700 User Four
+ Rev 11 2005-10-31 19:00:00 -0500 User Three
+ Rev 10 2005-09-30 19:00:00 -0500 User Two
+ Rev 9 2005-09-01 00:00:00 +0000 User One
+ Rev 8 2005-07-31 18:00:00 -0600 User Four
+ Rev 7 2005-06-30 20:00:00 -0400 User Three
+ Rev 6 2005-05-31 19:00:00 -0500 User Two
+ Rev 5 2005-05-01 00:00:00 +0000 User One
+ Rev 4 2005-03-31 17:00:00 -0700 User Four
+ Rev 3 2005-02-28 19:00:00 -0500 User Three
+ Rev 2 2005-01-31 18:00:00 -0600 User Two
+ Rev 1 2005-01-01 00:00:00 +0000 User One
+ EOF
+ test_cmp expect-2 actual-2
+'
+
+test_done
diff --git a/t/t9604/cvsroot/.gitattributes b/t/t9604/cvsroot/.gitattributes
new file mode 100644
index 0000000..562b12e
--- /dev/null
+++ b/t/t9604/cvsroot/.gitattributes
@@ -0,0 +1 @@
+* -whitespace
diff --git a/t/t9604/cvsroot/CVSROOT/.gitignore b/t/t9604/cvsroot/CVSROOT/.gitignore
new file mode 100644
index 0000000..3bb9b34
--- /dev/null
+++ b/t/t9604/cvsroot/CVSROOT/.gitignore
@@ -0,0 +1,2 @@
+history
+val-tags
diff --git a/t/t9604/cvsroot/module/a,v b/t/t9604/cvsroot/module/a,v
new file mode 100644
index 0000000..80658c3
--- /dev/null
+++ b/t/t9604/cvsroot/module/a,v
@@ -0,0 +1,264 @@
+head 1.16;
+access;
+symbols;
+locks; strict;
+comment @# @;
+
+
+1.16
+date 2006.10.29.07.00.01; author user2; state Exp;
+branches;
+next 1.15;
+
+1.15
+date 2006.10.29.06.59.59; author user2; state Exp;
+branches;
+next 1.14;
+
+1.14
+date 2006.04.02.08.00.01; author user2; state Exp;
+branches;
+next 1.13;
+
+1.13
+date 2006.04.02.07.59.59; author user2; state Exp;
+branches;
+next 1.12;
+
+1.12
+date 2005.12.01.00.00.00; author user4; state Exp;
+branches;
+next 1.11;
+
+1.11
+date 2005.11.01.00.00.00; author user3; state Exp;
+branches;
+next 1.10;
+
+1.10
+date 2005.10.01.00.00.00; author user2; state Exp;
+branches;
+next 1.9;
+
+1.9
+date 2005.09.01.00.00.00; author user1; state Exp;
+branches;
+next 1.8;
+
+1.8
+date 2005.08.01.00.00.00; author user4; state Exp;
+branches;
+next 1.7;
+
+1.7
+date 2005.07.01.00.00.00; author user3; state Exp;
+branches;
+next 1.6;
+
+1.6
+date 2005.06.01.00.00.00; author user2; state Exp;
+branches;
+next 1.5;
+
+1.5
+date 2005.05.01.00.00.00; author user1; state Exp;
+branches;
+next 1.4;
+
+1.4
+date 2005.04.01.00.00.00; author user4; state Exp;
+branches;
+next 1.3;
+
+1.3
+date 2005.03.01.00.00.00; author user3; state Exp;
+branches;
+next 1.2;
+
+1.2
+date 2005.02.01.00.00.00; author user2; state Exp;
+branches;
+next 1.1;
+
+1.1
+date 2005.01.01.00.00.00; author user1; state Exp;
+branches;
+next ;
+
+
+desc
+@@
+
+
+1.16
+log
+@Rev 16
+@
+text
+@Rev 16
+@
+
+
+1.15
+log
+@Rev 15
+@
+text
+@d1 1
+a1 1
+Rev 15
+@
+
+
+1.14
+log
+@Rev 14
+@
+text
+@d1 1
+a1 1
+Rev 14
+@
+
+
+1.13
+log
+@Rev 13
+@
+text
+@d1 1
+a1 1
+Rev 13
+@
+
+
+1.12
+log
+@Rev 12
+@
+text
+@d1 1
+a1 1
+Rev 12
+@
+
+
+1.11
+log
+@Rev 11
+@
+text
+@d1 1
+a1 1
+Rev 11
+@
+
+
+1.10
+log
+@Rev 10
+@
+text
+@d1 1
+a1 1
+Rev 10
+@
+
+
+1.9
+log
+@Rev 9
+@
+text
+@d1 1
+a1 1
+Rev 9
+@
+
+
+1.8
+log
+@Rev 8
+@
+text
+@d1 1
+a1 1
+Rev 8
+@
+
+
+1.7
+log
+@Rev 7
+@
+text
+@d1 1
+a1 1
+Rev 7
+@
+
+
+1.6
+log
+@Rev 6
+@
+text
+@d1 1
+a1 1
+Rev 6
+@
+
+
+1.5
+log
+@Rev 5
+@
+text
+@d1 1
+a1 1
+Rev 5
+@
+
+
+1.4
+log
+@Rev 4
+@
+text
+@d1 1
+a1 1
+Rev 4
+@
+
+
+1.3
+log
+@Rev 3
+@
+text
+@d1 1
+a1 1
+Rev 3
+@
+
+
+1.2
+log
+@Rev 2
+@
+text
+@d1 1
+a1 1
+Rev 2
+@
+
+
+1.1
+log
+@Rev 1
+@
+text
+@d1 1
+a1 1
+Rev 1
+@
diff --git a/t/t9700-perl-git.sh b/t/t9700-perl-git.sh
new file mode 100755
index 0000000..ccc8212
--- /dev/null
+++ b/t/t9700-perl-git.sh
@@ -0,0 +1,56 @@
+#!/bin/sh
+#
+# Copyright (c) 2008 Lea Wiemann
+#
+
+test_description='perl interface (Git.pm)'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-perl.sh
+
+skip_all_if_no_Test_More
+
+# set up test repository
+
+test_expect_success 'set up test repository' '
+ echo "test file 1" >file1 &&
+ echo "test file 2" >file2 &&
+ mkdir directory1 &&
+ echo "in directory1" >>directory1/file &&
+ mkdir directory2 &&
+ echo "in directory2" >>directory2/file &&
+ git add . &&
+ git commit -m "first commit" &&
+
+ echo "new file in subdir 2" >directory2/file2 &&
+ git add . &&
+ git commit -m "commit in directory2" &&
+
+ echo "changed file 1" >file1 &&
+ git commit -a -m "second commit" &&
+
+ git config --add color.test.slot1 green &&
+ git config --add test.string value &&
+ git config --add test.dupstring value1 &&
+ git config --add test.dupstring value2 &&
+ git config --add test.booltrue true &&
+ git config --add test.boolfalse no &&
+ git config --add test.boolother other &&
+ git config --add test.int 2k &&
+ git config --add test.path "~/foo" &&
+ git config --add test.pathexpanded "$HOME/foo" &&
+ git config --add test.pathmulti foo &&
+ git config --add test.pathmulti bar
+'
+
+test_expect_success 'set up bare repository' '
+ git init --bare bare.git
+'
+
+test_expect_success 'use t9700/test.pl to test Git.pm' '
+ "$PERL_PATH" "$TEST_DIRECTORY"/t9700/test.pl 2>stderr &&
+ test_must_be_empty stderr
+'
+
+test_done
diff --git a/t/t9700/test.pl b/t/t9700/test.pl
new file mode 100755
index 0000000..6d75370
--- /dev/null
+++ b/t/t9700/test.pl
@@ -0,0 +1,160 @@
+#!/usr/bin/perl
+use lib (split(/:/, $ENV{GITPERLLIB}));
+
+use 5.008;
+use warnings;
+use strict;
+
+use Test::More qw(no_plan);
+
+BEGIN {
+ # t9700-perl-git.sh kicks off our testing, so we have to go from
+ # there.
+ Test::More->builder->current_test(1);
+ Test::More->builder->no_ending(1);
+}
+
+use Cwd;
+use File::Basename;
+
+sub adjust_dirsep {
+ my $path = shift;
+ $path =~ s{\\}{/}g;
+ return $path;
+}
+
+my $oid_re = qr/^[0-9a-fA-F]{40}(?:[0-9a-fA-F]{24})?$/;
+
+BEGIN { use_ok('Git') }
+
+# set up
+our $abs_repo_dir = cwd();
+ok(our $r = Git->repository(Directory => "."), "open repository");
+{
+ local $ENV{GIT_TEST_ASSUME_DIFFERENT_OWNER} = 1;
+ my $failed;
+
+ $failed = eval { Git->repository(Directory => $abs_repo_dir) };
+ ok(!$failed, "reject unsafe non-bare repository");
+ like($@, qr/not a git repository/i, "unsafe error message");
+
+ $failed = eval { Git->repository(Directory => "$abs_repo_dir/bare.git") };
+ ok(!$failed, "reject unsafe bare repository");
+ like($@, qr/not a git repository/i, "unsafe error message");
+}
+
+# config
+is($r->config("test.string"), "value", "config scalar: string");
+is_deeply([$r->config("test.dupstring")], ["value1", "value2"],
+ "config array: string");
+is($r->config("test.nonexistent"), undef, "config scalar: nonexistent");
+is_deeply([$r->config("test.nonexistent")], [], "config array: nonexistent");
+is($r->config_int("test.int"), 2048, "config_int: integer");
+is($r->config_int("test.nonexistent"), undef, "config_int: nonexistent");
+ok($r->config_bool("test.booltrue"), "config_bool: true");
+ok(!$r->config_bool("test.boolfalse"), "config_bool: false");
+is(adjust_dirsep($r->config_path("test.path")), $r->config("test.pathexpanded"),
+ "config_path: ~/foo expansion");
+is_deeply([$r->config_path("test.pathmulti")], ["foo", "bar"],
+ "config_path: multiple values");
+our $ansi_green = "\x1b[32m";
+is($r->get_color("color.test.slot1", "red"), $ansi_green, "get_color");
+# Cannot test $r->get_colorbool("color.foo")) because we do not
+# control whether our STDOUT is a terminal.
+
+# Failure cases for config:
+# Save and restore STDERR; we will probably extract this into a
+# "dies_ok" method and possibly move the STDERR handling to Git.pm.
+open our $tmpstderr, ">&STDERR" or die "cannot save STDERR";
+open STDERR, ">", "/dev/null" or die "cannot redirect STDERR to /dev/null";
+is($r->config("test.dupstring"), "value2", "config: multivar");
+eval { $r->config_bool("test.boolother") };
+ok($@, "config_bool: non-boolean values fail");
+open STDERR, ">&", $tmpstderr or die "cannot restore STDERR";
+
+# ident
+like($r->ident("aUthor"), qr/^A U Thor <author\@example.com> [0-9]+ [+-]\d{4}$/,
+ "ident scalar: author (type)");
+like($r->ident("cOmmitter"), qr/^C O Mitter <committer\@example.com> [0-9]+ [+-]\d{4}$/,
+ "ident scalar: committer (type)");
+is($r->ident("invalid"), "invalid", "ident scalar: invalid ident string (no parsing)");
+my ($name, $email, $time_tz) = $r->ident('author');
+is_deeply([$name, $email], ["A U Thor", "author\@example.com"],
+ "ident array: author");
+like($time_tz, qr/[0-9]+ [+-]\d{4}/, "ident array: author");
+is_deeply([$r->ident("Name <email> 123 +0000")], ["Name", "email", "123 +0000"],
+ "ident array: ident string");
+is_deeply([$r->ident("invalid")], [], "ident array: invalid ident string");
+
+# ident_person
+is($r->ident_person("aUthor"), "A U Thor <author\@example.com>",
+ "ident_person: author (type)");
+is($r->ident_person("Name <email> 123 +0000"), "Name <email>",
+ "ident_person: ident string");
+is($r->ident_person("Name", "email", "123 +0000"), "Name <email>",
+ "ident_person: array");
+
+# objects and hashes
+ok(our $file1hash = $r->command_oneline('rev-parse', "HEAD:file1"), "(get file hash)");
+my $tmpfile = "file.tmp";
+open TEMPFILE, "+>$tmpfile" or die "Can't open $tmpfile: $!";
+is($r->cat_blob($file1hash, \*TEMPFILE), 15, "cat_blob: size");
+our $blobcontents;
+{ local $/; seek TEMPFILE, 0, 0; $blobcontents = <TEMPFILE>; }
+is($blobcontents, "changed file 1\n", "cat_blob: data");
+close TEMPFILE or die "Failed writing to $tmpfile: $!";
+is(Git::hash_object("blob", $tmpfile), $file1hash, "hash_object: roundtrip");
+open TEMPFILE, ">$tmpfile" or die "Can't open $tmpfile: $!";
+print TEMPFILE my $test_text = "test blob, to be inserted\n";
+close TEMPFILE or die "Failed writing to $tmpfile: $!";
+like(our $newhash = $r->hash_and_insert_object($tmpfile), $oid_re,
+ "hash_and_insert_object: returns hash");
+open TEMPFILE, "+>$tmpfile" or die "Can't open $tmpfile: $!";
+is($r->cat_blob($newhash, \*TEMPFILE), length $test_text, "cat_blob: roundtrip size");
+{ local $/; seek TEMPFILE, 0, 0; $blobcontents = <TEMPFILE>; }
+is($blobcontents, $test_text, "cat_blob: roundtrip data");
+close TEMPFILE;
+unlink $tmpfile;
+
+# paths
+is($r->repo_path, $abs_repo_dir . "/.git", "repo_path");
+is($r->wc_path, $abs_repo_dir . "/", "wc_path");
+is($r->wc_subdir, "", "wc_subdir initial");
+$r->wc_chdir("directory1");
+is($r->wc_subdir, "directory1", "wc_subdir after wc_chdir");
+is($r->config("test.string"), "value", "config after wc_chdir");
+
+# Object generation in sub directory
+chdir("directory2");
+my $r2 = Git->repository();
+is($r2->repo_path, $abs_repo_dir . "/.git", "repo_path (2)");
+is($r2->wc_path, $abs_repo_dir . "/", "wc_path (2)");
+is($r2->wc_subdir, "directory2/", "wc_subdir initial (2)");
+
+# commands in sub directory
+my $last_commit = $r2->command_oneline(qw(rev-parse --verify HEAD));
+like($last_commit, $oid_re, 'rev-parse returned hash');
+my $dir_commit = $r2->command_oneline('log', '-n1', '--pretty=format:%H', '.');
+isnt($last_commit, $dir_commit, 'log . does not show last commit');
+
+# commands outside working tree
+chdir($abs_repo_dir . '/..');
+my $r3 = Git->repository(Directory => $abs_repo_dir);
+my $tmpfile3 = "$abs_repo_dir/file3.tmp";
+open TEMPFILE3, "+>$tmpfile3" or die "Can't open $tmpfile3: $!";
+is($r3->cat_blob($file1hash, \*TEMPFILE3), 15, "cat_blob(outside): size");
+close TEMPFILE3;
+unlink $tmpfile3;
+chdir($abs_repo_dir);
+
+# unquoting paths
+is(Git::unquote_path('abc'), 'abc', 'unquote unquoted path');
+is(Git::unquote_path('"abc def"'), 'abc def', 'unquote simple quoted path');
+is(Git::unquote_path('"abc\"\\\\ \a\b\t\n\v\f\r\001\040"'),
+ "abc\"\\ \x07\x08\x09\x0a\x0b\x0c\x0d\x01 ",
+ 'unquote escape sequences');
+
+printf "1..%d\n", Test::More->builder->current_test;
+
+my $is_passing = eval { Test::More->is_passing };
+exit($is_passing ? 0 : 1) unless $@ =~ /Can't locate object method/;
diff --git a/t/t9800-git-p4-basic.sh b/t/t9800-git-p4-basic.sh
new file mode 100755
index 0000000..53af8e3
--- /dev/null
+++ b/t/t9800-git-p4-basic.sh
@@ -0,0 +1,421 @@
+#!/bin/sh
+
+test_description='git p4 tests'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./lib-git-p4.sh
+
+test_expect_success 'start p4d' '
+ start_p4d
+'
+
+test_expect_success 'add p4 files' '
+ (
+ cd "$cli" &&
+ echo file1 >file1 &&
+ p4 add file1 &&
+ p4 submit -d "file1" &&
+ echo file2 >file2 &&
+ p4 add file2 &&
+ p4 submit -d "file2"
+ )
+'
+
+test_expect_success 'basic git p4 clone' '
+ git p4 clone --dest="$git" //depot &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git log --oneline >lines &&
+ test_line_count = 1 lines
+ )
+'
+
+test_expect_success 'depot typo error' '
+ test_must_fail git p4 clone --dest="$git" /depot 2>errs &&
+ grep "Depot paths must start with" errs
+'
+
+test_expect_success 'git p4 clone @all' '
+ git p4 clone --dest="$git" //depot@all &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git log --oneline >lines &&
+ test_line_count = 2 lines
+ )
+'
+
+test_expect_success 'git p4 sync uninitialized repo' '
+ test_create_repo "$git" &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ test_must_fail git p4 sync 2>errs &&
+ test_grep "Perhaps you never did" errs
+ )
+'
+
+#
+# Create a git repo by hand. Add a commit so that HEAD is valid.
+# Test imports a new p4 repository into a new git branch.
+#
+test_expect_success 'git p4 sync new branch' '
+ test_create_repo "$git" &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ test_commit head &&
+ git p4 sync --branch=refs/remotes/p4/depot //depot@all &&
+ git log --oneline p4/depot >lines &&
+ test_line_count = 2 lines
+ )
+'
+
+#
+# Setup as before, and then explicitly sync imported branch, using a
+# different ref format.
+#
+test_expect_success 'git p4 sync existing branch without changes' '
+ test_create_repo "$git" &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ test_commit head &&
+ git p4 sync --branch=depot //depot@all &&
+ git p4 sync --branch=refs/remotes/p4/depot >out &&
+ test_grep "No changes to import!" out
+ )
+'
+
+#
+# Same as before, relative branch name.
+#
+test_expect_success 'git p4 sync existing branch with relative name' '
+ test_create_repo "$git" &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ test_commit head &&
+ git p4 sync --branch=branch1 //depot@all &&
+ git p4 sync --branch=p4/branch1 >out &&
+ test_grep "No changes to import!" out
+ )
+'
+
+#
+# Same as before, with a nested branch path, referenced different ways.
+#
+test_expect_success 'git p4 sync existing branch with nested path' '
+ test_create_repo "$git" &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ test_commit head &&
+ git p4 sync --branch=p4/some/path //depot@all &&
+ git p4 sync --branch=some/path >out &&
+ test_grep "No changes to import!" out
+ )
+'
+
+#
+# Same as before, with a full ref path outside the p4/* namespace.
+#
+test_expect_success 'git p4 sync branch explicit ref without p4 in path' '
+ test_create_repo "$git" &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ test_commit head &&
+ git p4 sync --branch=refs/remotes/someremote/depot //depot@all &&
+ git p4 sync --branch=refs/remotes/someremote/depot >out &&
+ test_grep "No changes to import!" out
+ )
+'
+
+test_expect_success 'git p4 sync nonexistent ref' '
+ test_create_repo "$git" &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ test_commit head &&
+ git p4 sync --branch=depot //depot@all &&
+ test_must_fail git p4 sync --branch=depot2 2>errs &&
+ test_grep "Perhaps you never did" errs
+ )
+'
+
+test_expect_success 'git p4 sync existing non-p4-imported ref' '
+ test_create_repo "$git" &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ test_commit head &&
+ git p4 sync --branch=depot //depot@all &&
+ test_must_fail git p4 sync --branch=refs/heads/master 2>errs &&
+ test_grep "Perhaps you never did" errs
+ )
+'
+
+test_expect_success 'clone two dirs' '
+ (
+ cd "$cli" &&
+ mkdir sub1 sub2 &&
+ echo sub1/f1 >sub1/f1 &&
+ echo sub2/f2 >sub2/f2 &&
+ p4 add sub1/f1 &&
+ p4 submit -d "sub1/f1" &&
+ p4 add sub2/f2 &&
+ p4 submit -d "sub2/f2"
+ ) &&
+ git p4 clone --dest="$git" //depot/sub1 //depot/sub2 &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git ls-files >lines &&
+ test_line_count = 2 lines &&
+ git log --oneline p4/master >lines &&
+ test_line_count = 1 lines
+ )
+'
+
+test_expect_success 'clone two dirs, @all' '
+ (
+ cd "$cli" &&
+ echo sub1/f3 >sub1/f3 &&
+ p4 add sub1/f3 &&
+ p4 submit -d "sub1/f3"
+ ) &&
+ git p4 clone --dest="$git" //depot/sub1@all //depot/sub2@all &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git ls-files >lines &&
+ test_line_count = 3 lines &&
+ git log --oneline p4/master >lines &&
+ test_line_count = 3 lines
+ )
+'
+
+test_expect_success 'clone two dirs, @all, conflicting files' '
+ (
+ cd "$cli" &&
+ echo sub2/f3 >sub2/f3 &&
+ p4 add sub2/f3 &&
+ p4 submit -d "sub2/f3"
+ ) &&
+ git p4 clone --dest="$git" //depot/sub1@all //depot/sub2@all &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git ls-files >lines &&
+ test_line_count = 3 lines &&
+ git log --oneline p4/master >lines &&
+ test_line_count = 4 lines &&
+ echo sub2/f3 >expected &&
+ test_cmp expected f3
+ )
+'
+
+test_expect_success 'clone two dirs, each edited by submit, single git commit' '
+ (
+ cd "$cli" &&
+ echo sub1/f4 >sub1/f4 &&
+ p4 add sub1/f4 &&
+ echo sub2/f4 >sub2/f4 &&
+ p4 add sub2/f4 &&
+ p4 submit -d "sub1/f4 and sub2/f4"
+ ) &&
+ git p4 clone --dest="$git" //depot/sub1@all //depot/sub2@all &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git ls-files >lines &&
+ test_line_count = 4 lines &&
+ git log --oneline p4/master >lines &&
+ test_line_count = 5 lines
+ )
+'
+
+revision_ranges="2000/01/01,#head \
+ 1,2080/01/01 \
+ 2000/01/01,2080/01/01 \
+ 2000/01/01,1000 \
+ 1,1000"
+
+test_expect_success 'clone using non-numeric revision ranges' '
+ test_when_finished cleanup_git &&
+ for r in $revision_ranges
+ do
+ rm -fr "$git" &&
+ test ! -d "$git" &&
+ git p4 clone --dest="$git" //depot@$r &&
+ (
+ cd "$git" &&
+ git ls-files >lines &&
+ test_line_count = 8 lines
+ ) || return 1
+ done
+'
+
+test_expect_success 'clone with date range, excluding some changes' '
+ test_when_finished cleanup_git &&
+ before=$(date +%Y/%m/%d:%H:%M:%S) &&
+ sleep 2 &&
+ (
+ cd "$cli" &&
+ :>date_range_test &&
+ p4 add date_range_test &&
+ p4 submit -d "Adding file"
+ ) &&
+ git p4 clone --dest="$git" //depot@1,$before &&
+ (
+ cd "$git" &&
+ test_path_is_missing date_range_test
+ )
+'
+
+test_expect_success 'exit when p4 fails to produce marshaled output' '
+ mkdir badp4dir &&
+ test_when_finished "rm badp4dir/p4 && rmdir badp4dir" &&
+ cat >badp4dir/p4 <<-EOF &&
+ #!$SHELL_PATH
+ exit 1
+ EOF
+ chmod 755 badp4dir/p4 &&
+ (
+ PATH="$TRASH_DIRECTORY/badp4dir:$PATH" &&
+ export PATH &&
+ test_expect_code 1 git p4 clone --dest="$git" //depot >errs 2>&1
+ ) &&
+ test_grep ! Traceback errs
+'
+
+# Hide a file from p4d, make sure we catch its complaint. This won't fail in
+# p4 changes, files, or describe; just in p4 print. If P4CLIENT is unset, the
+# message will include "Librarian checkout".
+test_expect_success 'exit gracefully for p4 server errors' '
+ test_when_finished "mv \"$db\"/depot/file1,v,hidden \"$db\"/depot/file1,v" &&
+ mv "$db"/depot/file1,v "$db"/depot/file1,v,hidden &&
+ test_when_finished cleanup_git &&
+ test_expect_code 1 git p4 clone --dest="$git" //depot@1 >out 2>err &&
+ test_grep "Error from p4 print" err
+'
+
+test_expect_success 'clone --bare should make a bare repository' '
+ rm -rf "$git" &&
+ git p4 clone --dest="$git" --bare //depot &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ test_path_is_missing .git &&
+ git config --get --bool core.bare true &&
+ git rev-parse --verify refs/remotes/p4/master &&
+ git rev-parse --verify refs/remotes/p4/HEAD &&
+ git rev-parse --verify refs/heads/main &&
+ git rev-parse --verify HEAD
+ )
+'
+
+# Sleep a bit so that the top-most p4 change did not happen "now". Then
+# import the repo and make sure that the initial import has the same time
+# as the top-most change.
+test_expect_success 'initial import time from top change time' '
+ p4change=$(p4 -G changes -m 1 //depot/... | marshal_dump change) &&
+ p4time=$(p4 -G changes -m 1 //depot/... | marshal_dump time) &&
+ sleep 3 &&
+ git p4 clone --dest="$git" //depot &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ gittime=$(git show -s --pretty=format:%at HEAD) &&
+ echo $p4time $gittime &&
+ test $p4time = $gittime
+ )
+'
+
+test_expect_success 'unresolvable host in P4PORT should display error' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ P4PORT=nosuchhost:65537 &&
+ export P4PORT &&
+ test_expect_code 1 git p4 sync >out 2>err &&
+ grep "connect to nosuchhost" err
+ )
+'
+
+# Test following scenarios:
+# - Without ".git/hooks/p4-pre-submit" , submit should continue
+# - With the hook returning 0, submit should continue
+# - With the hook returning 1, submit should abort
+test_expect_success 'run hook p4-pre-submit before submit' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ echo "hello world" >hello.txt &&
+ git add hello.txt &&
+ git commit -m "add hello.txt" &&
+ git config git-p4.skipSubmitEdit true &&
+ git p4 submit --dry-run >out &&
+ grep "Would apply" out
+ ) &&
+ test_hook -C "$git" p4-pre-submit <<-\EOF &&
+ exit 0
+ EOF
+ (
+ cd "$git" &&
+ git p4 submit --dry-run >out &&
+ grep "Would apply" out
+ ) &&
+ test_hook -C "$git" --clobber p4-pre-submit <<-\EOF &&
+ exit 1
+ EOF
+ (
+ cd "$git" &&
+ test_must_fail git p4 submit --dry-run >errs 2>&1 &&
+ ! grep "Would apply" errs
+ )
+'
+
+test_expect_success 'submit from detached head' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ git checkout p4/master &&
+ >detached_head_test &&
+ git add detached_head_test &&
+ git commit -m "add detached_head" &&
+ git config git-p4.skipSubmitEdit true &&
+ git p4 submit &&
+ git p4 rebase &&
+ git log p4/master | grep detached_head
+ )
+'
+
+test_expect_success 'submit from worktree' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ git worktree add ../worktree-test
+ ) &&
+ (
+ cd "$git/../worktree-test" &&
+ test_commit "worktree-commit" &&
+ git config git-p4.skipSubmitEdit true &&
+ git p4 submit
+ ) &&
+ (
+ cd "$cli" &&
+ p4 sync &&
+ test_path_is_file worktree-commit.t
+ )
+'
+
+test_done
diff --git a/t/t9801-git-p4-branch.sh b/t/t9801-git-p4-branch.sh
new file mode 100755
index 0000000..73cca0d
--- /dev/null
+++ b/t/t9801-git-p4-branch.sh
@@ -0,0 +1,760 @@
+#!/bin/sh
+
+test_description='git p4 tests for p4 branches'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./lib-git-p4.sh
+
+test_expect_success 'start p4d' '
+ start_p4d
+'
+
+#
+# 1: //depot/main/f1
+# 2: //depot/main/f2
+# 3: integrate //depot/main/... -> //depot/branch1/...
+# 4: //depot/main/f4
+# 5: //depot/branch1/f5
+# .: named branch branch2
+# 6: integrate -b branch2
+# 7: //depot/branch2/f7
+# 8: //depot/main/f8
+#
+test_expect_success 'basic p4 branches' '
+ (
+ cd "$cli" &&
+ mkdir -p main &&
+
+ echo f1 >main/f1 &&
+ p4 add main/f1 &&
+ p4 submit -d "main/f1" &&
+
+ echo f2 >main/f2 &&
+ p4 add main/f2 &&
+ p4 submit -d "main/f2" &&
+
+ p4 integrate //depot/main/... //depot/branch1/... &&
+ p4 submit -d "integrate main to branch1" &&
+
+ echo f4 >main/f4 &&
+ p4 add main/f4 &&
+ p4 submit -d "main/f4" &&
+
+ echo f5 >branch1/f5 &&
+ p4 add branch1/f5 &&
+ p4 submit -d "branch1/f5" &&
+
+ p4 branch -i <<-EOF &&
+ Branch: branch2
+ View: //depot/main/... //depot/branch2/...
+ EOF
+
+ p4 integrate -b branch2 &&
+ p4 submit -d "integrate main to branch2" &&
+
+ echo f7 >branch2/f7 &&
+ p4 add branch2/f7 &&
+ p4 submit -d "branch2/f7" &&
+
+ echo f8 >main/f8 &&
+ p4 add main/f8 &&
+ p4 submit -d "main/f8"
+ )
+'
+
+test_expect_success 'import main, no branch detection' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot/main@all &&
+ (
+ cd "$git" &&
+ git log --oneline --graph --decorate --all &&
+ git rev-list main -- >wc &&
+ test_line_count = 4 wc
+ )
+'
+
+test_expect_success 'import branch1, no branch detection' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot/branch1@all &&
+ (
+ cd "$git" &&
+ git log --oneline --graph --decorate --all &&
+ git rev-list main -- >wc &&
+ test_line_count = 2 wc
+ )
+'
+
+test_expect_success 'import branch2, no branch detection' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot/branch2@all &&
+ (
+ cd "$git" &&
+ git log --oneline --graph --decorate --all &&
+ git rev-list main -- >wc &&
+ test_line_count = 2 wc
+ )
+'
+
+test_expect_success 'import depot, no branch detection' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot@all &&
+ (
+ cd "$git" &&
+ git log --oneline --graph --decorate --all &&
+ git rev-list main -- >wc &&
+ test_line_count = 8 wc
+ )
+'
+
+test_expect_success 'import depot, branch detection' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" --detect-branches //depot@all &&
+ (
+ cd "$git" &&
+
+ git log --oneline --graph --decorate --all &&
+
+ # 4 main commits
+ git rev-list main -- >wc &&
+ test_line_count = 4 wc &&
+
+ # 3 main, 1 integrate, 1 on branch2
+ git rev-list p4/depot/branch2 >wc &&
+ test_line_count = 5 wc &&
+
+ # no branch1, since no p4 branch created for it
+ test_must_fail git show-ref p4/depot/branch1
+ )
+'
+
+test_expect_success 'sync specific detected branch' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" --detect-branches //depot@all &&
+ (
+ cd "$git" &&
+ git p4 sync --branch=depot/branch2 >out &&
+ test_grep "No changes to import!" out
+ )
+'
+
+test_expect_success 'import depot, branch detection, branchList branch definition' '
+ test_when_finished cleanup_git &&
+ test_create_repo "$git" &&
+ (
+ cd "$git" &&
+ git config git-p4.branchList main:branch1 &&
+ git p4 clone --dest=. --detect-branches //depot@all &&
+
+ git log --oneline --graph --decorate --all &&
+
+ # 4 main commits
+ git rev-list main -- >wc &&
+ test_line_count = 4 wc &&
+
+ # 3 main, 1 integrate, 1 on branch2
+ git rev-list p4/depot/branch2 >wc &&
+ test_line_count = 5 wc &&
+
+ # 2 main, 1 integrate, 1 on branch1
+ git rev-list p4/depot/branch1 >wc &&
+ test_line_count = 4 wc
+ )
+'
+
+test_expect_success 'restart p4d' '
+ stop_and_cleanup_p4d &&
+ start_p4d
+'
+
+#
+# 1: //depot/branch1/file1
+# //depot/branch1/file2
+# 2: integrate //depot/branch1/... -> //depot/branch2/...
+# 3: //depot/branch1/file3
+# 4: //depot/branch1/file2 (edit)
+# 5: integrate //depot/branch1/... -> //depot/branch3/...
+#
+## Create a simple branch structure in P4 depot.
+test_expect_success 'add simple p4 branches' '
+ (
+ cd "$cli" &&
+ mkdir branch1 &&
+ cd branch1 &&
+ echo file1 >file1 &&
+ echo file2 >file2 &&
+ p4 add file1 file2 &&
+ p4 submit -d "Create branch1" &&
+ p4 integrate //depot/branch1/... //depot/branch2/... &&
+ p4 submit -d "Integrate branch2 from branch1" &&
+ echo file3 >file3 &&
+ p4 add file3 &&
+ p4 submit -d "add file3 in branch1" &&
+ p4 open file2 &&
+ echo update >>file2 &&
+ p4 submit -d "update file2 in branch1" &&
+ p4 integrate //depot/branch1/... //depot/branch3/... &&
+ p4 submit -d "Integrate branch3 from branch1"
+ )
+'
+
+# Configure branches through git-config and clone them.
+# All files are tested to make sure branches were cloned correctly.
+# Finally, make an update to branch1 on P4 side to check if it is imported
+# correctly by git p4.
+test_expect_success 'git p4 clone simple branches' '
+ test_when_finished cleanup_git &&
+ test_create_repo "$git" &&
+ (
+ cd "$git" &&
+ git config git-p4.branchList branch1:branch2 &&
+ git config --add git-p4.branchList branch1:branch3 &&
+ git p4 clone --dest=. --detect-branches //depot@all &&
+ git log --all --graph --decorate --stat &&
+ git reset --hard p4/depot/branch1 &&
+ test_path_is_file file1 &&
+ test_path_is_file file2 &&
+ test_path_is_file file3 &&
+ grep update file2 &&
+ git reset --hard p4/depot/branch2 &&
+ test_path_is_file file1 &&
+ test_path_is_file file2 &&
+ test ! -f file3 &&
+ ! grep update file2 &&
+ git reset --hard p4/depot/branch3 &&
+ test_path_is_file file1 &&
+ test_path_is_file file2 &&
+ test_path_is_file file3 &&
+ grep update file2 &&
+ cd "$cli" &&
+ cd branch1 &&
+ p4 edit file2 &&
+ echo file2_ >>file2 &&
+ p4 submit -d "update file2 in branch1" &&
+ cd "$git" &&
+ git reset --hard p4/depot/branch1 &&
+ git p4 rebase &&
+ grep file2_ file2
+ )
+'
+
+# Create a complex branch structure in P4 depot to check if they are correctly
+# cloned. The branches are created from older changelists to check if git p4 is
+# able to correctly detect them.
+# The final expected structure is:
+# `branch1
+# | `- file1
+# | `- file2 (updated)
+# | `- file3
+# `branch2
+# | `- file1
+# | `- file2
+# `branch3
+# | `- file1
+# | `- file2 (updated)
+# | `- file3
+# `branch4
+# | `- file1
+# | `- file2
+# `branch5
+# `- file1
+# `- file2
+# `- file3
+test_expect_success 'git p4 add complex branches' '
+ (
+ cd "$cli" &&
+ changelist=$(p4 changes -m1 //depot/... | cut -d" " -f2) &&
+ changelist=$(($changelist - 5)) &&
+ p4 integrate //depot/branch1/...@$changelist //depot/branch4/... &&
+ p4 submit -d "Integrate branch4 from branch1@${changelist}" &&
+ changelist=$(($changelist + 2)) &&
+ p4 integrate //depot/branch1/...@$changelist //depot/branch5/... &&
+ p4 submit -d "Integrate branch5 from branch1@${changelist}"
+ )
+'
+
+# Configure branches through git-config and clone them. git p4 will only be able
+# to clone the original structure if it is able to detect the origin changelist
+# of each branch.
+test_expect_success 'git p4 clone complex branches' '
+ test_when_finished cleanup_git &&
+ test_create_repo "$git" &&
+ (
+ cd "$git" &&
+ git config git-p4.branchList branch1:branch2 &&
+ git config --add git-p4.branchList branch1:branch3 &&
+ git config --add git-p4.branchList branch1:branch4 &&
+ git config --add git-p4.branchList branch1:branch5 &&
+ git p4 clone --dest=. --detect-branches //depot@all &&
+ git log --all --graph --decorate --stat &&
+ git reset --hard p4/depot/branch1 &&
+ test_path_is_file file1 &&
+ test_path_is_file file2 &&
+ test_path_is_file file3 &&
+ grep update file2 &&
+ git reset --hard p4/depot/branch2 &&
+ test_path_is_file file1 &&
+ test_path_is_file file2 &&
+ test_path_is_missing file3 &&
+ ! grep update file2 &&
+ git reset --hard p4/depot/branch3 &&
+ test_path_is_file file1 &&
+ test_path_is_file file2 &&
+ test_path_is_file file3 &&
+ grep update file2 &&
+ git reset --hard p4/depot/branch4 &&
+ git diff-tree --quiet HEAD &&
+ test_path_is_file file1 &&
+ test_path_is_file file2 &&
+ test_path_is_missing file3 &&
+ ! grep update file2 &&
+ git reset --hard p4/depot/branch5 &&
+ git diff-tree --quiet HEAD &&
+ test_path_is_file file1 &&
+ test_path_is_file file2 &&
+ test_path_is_file file3 &&
+ ! grep update file2 &&
+ test_must_fail git show-ref --verify refs/git-p4-tmp
+ )
+'
+
+# Move branch3/file3 to branch4/file3 in a single changelist
+test_expect_success 'git p4 submit to two branches in a single changelist' '
+ (
+ cd "$cli" &&
+ p4 integrate //depot/branch3/file3 //depot/branch4/file3 &&
+ p4 delete //depot/branch3/file3 &&
+ p4 submit -d "Move branch3/file3 to branch4/file3"
+ )
+'
+
+# Confirm that changes to two branches done in a single changelist
+# are correctly imported by git p4
+test_expect_success 'git p4 sync changes to two branches in the same changelist' '
+ test_when_finished cleanup_git &&
+ test_create_repo "$git" &&
+ (
+ cd "$git" &&
+ git config git-p4.branchList branch1:branch2 &&
+ git config --add git-p4.branchList branch1:branch3 &&
+ git config --add git-p4.branchList branch1:branch4 &&
+ git config --add git-p4.branchList branch1:branch5 &&
+ git p4 clone --dest=. --detect-branches //depot@all &&
+ git log --all --graph --decorate --stat &&
+ git reset --hard p4/depot/branch1 &&
+ test_path_is_file file1 &&
+ test_path_is_file file2 &&
+ test_path_is_file file3 &&
+ grep update file2 &&
+ git reset --hard p4/depot/branch2 &&
+ test_path_is_file file1 &&
+ test_path_is_file file2 &&
+ test_path_is_missing file3 &&
+ ! grep update file2 &&
+ git reset --hard p4/depot/branch3 &&
+ test_path_is_file file1 &&
+ test_path_is_file file2 &&
+ test_path_is_missing file3 &&
+ grep update file2 &&
+ git reset --hard p4/depot/branch4 &&
+ test_path_is_file file1 &&
+ test_path_is_file file2 &&
+ test_path_is_file file3 &&
+ ! grep update file2 &&
+ git reset --hard p4/depot/branch5 &&
+ test_path_is_file file1 &&
+ test_path_is_file file2 &&
+ test_path_is_file file3 &&
+ ! grep update file2 &&
+ test_must_fail git show-ref --verify refs/git-p4-tmp
+ )
+'
+
+# Create a branch by integrating a single file
+test_expect_success 'git p4 file subset branch' '
+ (
+ cd "$cli" &&
+ p4 integrate //depot/branch1/file1 //depot/branch6/file1 &&
+ p4 submit -d "Integrate file1 alone from branch1 to branch6"
+ )
+'
+
+# Check if git p4 creates a new branch containing a single file,
+# instead of keeping the old files from the original branch
+test_expect_failure 'git p4 clone file subset branch' '
+ test_when_finished cleanup_git &&
+ test_create_repo "$git" &&
+ (
+ cd "$git" &&
+ git config git-p4.branchList branch1:branch2 &&
+ git config --add git-p4.branchList branch1:branch3 &&
+ git config --add git-p4.branchList branch1:branch4 &&
+ git config --add git-p4.branchList branch1:branch5 &&
+ git config --add git-p4.branchList branch1:branch6 &&
+ git p4 clone --dest=. --detect-branches //depot@all &&
+ git log --all --graph --decorate --stat &&
+ git reset --hard p4/depot/branch1 &&
+ test_path_is_file file1 &&
+ test_path_is_file file2 &&
+ test_path_is_file file3 &&
+ grep update file2 &&
+ git reset --hard p4/depot/branch2 &&
+ test_path_is_file file1 &&
+ test_path_is_file file2 &&
+ test_path_is_missing file3 &&
+ ! grep update file2 &&
+ git reset --hard p4/depot/branch3 &&
+ test_path_is_file file1 &&
+ test_path_is_file file2 &&
+ test_path_is_missing file3 &&
+ grep update file2 &&
+ git reset --hard p4/depot/branch4 &&
+ test_path_is_file file1 &&
+ test_path_is_file file2 &&
+ test_path_is_file file3 &&
+ ! grep update file2 &&
+ git reset --hard p4/depot/branch5 &&
+ test_path_is_file file1 &&
+ test_path_is_file file2 &&
+ test_path_is_file file3 &&
+ ! grep update file2 &&
+ git reset --hard p4/depot/branch6 &&
+ test_path_is_file file1 &&
+ test_path_is_missing file2 &&
+ test_path_is_missing file3
+ )
+'
+
+# Check that excluded files are omitted during import
+test_expect_success 'git p4 clone complex branches with excluded files' '
+ test_when_finished cleanup_git &&
+ test_create_repo "$git" &&
+ (
+ cd "$git" &&
+ git config git-p4.branchList branch1:branch2 &&
+ git config --add git-p4.branchList branch1:branch3 &&
+ git config --add git-p4.branchList branch1:branch4 &&
+ git config --add git-p4.branchList branch1:branch5 &&
+ git config --add git-p4.branchList branch1:branch6 &&
+ git p4 clone --dest=. --detect-branches -//depot/branch1/file2 -//depot/branch2/file2 -//depot/branch3/file2 -//depot/branch4/file2 -//depot/branch5/file2 -//depot/branch6/file2 //depot@all &&
+ git log --all --graph --decorate --stat &&
+ git reset --hard p4/depot/branch1 &&
+ test_path_is_file file1 &&
+ test_path_is_missing file2 &&
+ test_path_is_file file3 &&
+ git reset --hard p4/depot/branch2 &&
+ test_path_is_file file1 &&
+ test_path_is_missing file2 &&
+ test_path_is_missing file3 &&
+ git reset --hard p4/depot/branch3 &&
+ test_path_is_file file1 &&
+ test_path_is_missing file2 &&
+ test_path_is_missing file3 &&
+ git reset --hard p4/depot/branch4 &&
+ test_path_is_file file1 &&
+ test_path_is_missing file2 &&
+ test_path_is_file file3 &&
+ git reset --hard p4/depot/branch5 &&
+ test_path_is_file file1 &&
+ test_path_is_missing file2 &&
+ test_path_is_file file3 &&
+ git reset --hard p4/depot/branch6 &&
+ test_path_is_file file1 &&
+ test_path_is_missing file2 &&
+ test_path_is_missing file3
+ )
+'
+
+# From a report in http://stackoverflow.com/questions/11893688
+# where --use-client-spec caused branch prefixes not to be removed;
+# every file in git appeared into a subdirectory of the branch name.
+test_expect_success 'use-client-spec detect-branches setup' '
+ rm -rf "$cli" &&
+ mkdir "$cli" &&
+ (
+ cd "$cli" &&
+ client_view "//depot/usecs/... //client/..." &&
+ mkdir b1 &&
+ echo b1/b1-file1 >b1/b1-file1 &&
+ p4 add b1/b1-file1 &&
+ p4 submit -d "b1/b1-file1" &&
+
+ p4 integrate //depot/usecs/b1/... //depot/usecs/b2/... &&
+ p4 submit -d "b1 -> b2" &&
+ p4 branch -i <<-EOF &&
+ Branch: b2
+ View: //depot/usecs/b1/... //depot/usecs/b2/...
+ EOF
+
+ echo b2/b2-file2 >b2/b2-file2 &&
+ p4 add b2/b2-file2 &&
+ p4 submit -d "b2/b2-file2"
+ )
+'
+
+test_expect_success 'use-client-spec detect-branches files in top-level' '
+ test_when_finished cleanup_git &&
+ test_create_repo "$git" &&
+ (
+ cd "$git" &&
+ git p4 sync --detect-branches --use-client-spec //depot/usecs@all &&
+ git checkout -b main p4/usecs/b1 &&
+ test_path_is_file b1-file1 &&
+ test_path_is_missing b2-file2 &&
+ test_path_is_missing b1 &&
+ test_path_is_missing b2 &&
+
+ git checkout -b b2 p4/usecs/b2 &&
+ test_path_is_file b1-file1 &&
+ test_path_is_file b2-file2 &&
+ test_path_is_missing b1 &&
+ test_path_is_missing b2
+ )
+'
+
+test_expect_success 'use-client-spec detect-branches skips branches setup' '
+ (
+ cd "$cli" &&
+
+ p4 integrate //depot/usecs/b1/... //depot/usecs/b3/... &&
+ p4 submit -d "b1 -> b3" &&
+ p4 branch -i <<-EOF &&
+ Branch: b3
+ View: //depot/usecs/b1/... //depot/usecs/b3/...
+ EOF
+
+ echo b3/b3-file3_1 >b3/b3-file3_1 &&
+ echo b3/b3-file3_2 >b3/b3-file3_2 &&
+ p4 add b3/b3-file3_1 &&
+ p4 add b3/b3-file3_2 &&
+ p4 submit -d "b3/b3-file3_1 b3/b3-file3_2"
+ )
+'
+
+test_expect_success 'use-client-spec detect-branches skips branches' '
+ client_view "//depot/usecs/... //client/..." \
+ "-//depot/usecs/b3/... //client/b3/..." &&
+ test_when_finished cleanup_git &&
+ test_create_repo "$git" &&
+ (
+ cd "$git" &&
+ git p4 sync --detect-branches --use-client-spec //depot/usecs@all &&
+ test_must_fail git rev-parse refs/remotes/p4/usecs/b3
+ )
+'
+
+test_expect_success 'use-client-spec detect-branches skips files in branches' '
+ client_view "//depot/usecs/... //client/..." \
+ "-//depot/usecs/b3/b3-file3_1 //client/b3/b3-file3_1" &&
+ test_when_finished cleanup_git &&
+ test_create_repo "$git" &&
+ (
+ cd "$git" &&
+ git p4 sync --detect-branches --use-client-spec //depot/usecs@all &&
+ git checkout -b main p4/usecs/b3 &&
+ test_path_is_file b1-file1 &&
+ test_path_is_file b3-file3_2 &&
+ test_path_is_missing b3-file3_1
+ )
+'
+
+test_expect_success 'restart p4d' '
+ stop_and_cleanup_p4d &&
+ start_p4d
+'
+
+#
+# 1: //depot/branch1/base/file1
+# //depot/branch1/base/file2
+# //depot/branch1/base/dir/sub_file1
+# 2: integrate //depot/branch1/base/... -> //depot/branch2/base/...
+# 3: //depot/branch1/base/file3
+# 4: //depot/branch1/base/file2 (edit)
+# 5: integrate //depot/branch1/base/... -> //depot/branch3/base/...
+#
+# Note: the client view removes the "base" folder from the workspace
+# and moves sub_file1 one level up.
+test_expect_success 'add simple p4 branches with common base folder on each branch' '
+ (
+ cd "$cli" &&
+ client_view "//depot/branch1/base/... //client/branch1/..." \
+ "//depot/branch1/base/dir/sub_file1 //client/branch1/sub_file1" \
+ "//depot/branch2/base/... //client/branch2/..." \
+ "//depot/branch3/base/... //client/branch3/..." &&
+ mkdir -p branch1 &&
+ cd branch1 &&
+ echo file1 >file1 &&
+ echo file2 >file2 &&
+ mkdir dir &&
+ echo sub_file1 >sub_file1 &&
+ p4 add file1 file2 sub_file1 &&
+ p4 submit -d "Create branch1" &&
+ p4 integrate //depot/branch1/base/... //depot/branch2/base/... &&
+ p4 submit -d "Integrate branch2 from branch1" &&
+ echo file3 >file3 &&
+ p4 add file3 &&
+ p4 submit -d "add file3 in branch1" &&
+ p4 open file2 &&
+ echo update >>file2 &&
+ p4 submit -d "update file2 in branch1" &&
+ p4 integrate //depot/branch1/base/... //depot/branch3/base/... &&
+ p4 submit -d "Integrate branch3 from branch1"
+ )
+'
+
+# Configure branches through git-config and clone them.
+# All files are tested to make sure branches were cloned correctly.
+# Finally, make an update to branch1 on P4 side to check if it is imported
+# correctly by git p4.
+# git p4 is expected to use the client view to also not include the common
+# "base" folder in the imported directory structure.
+test_expect_success 'git p4 clone simple branches with base folder on server side' '
+ test_create_repo "$git" &&
+ (
+ cd "$git" &&
+ git config git-p4.branchList branch1:branch2 &&
+ git config --add git-p4.branchList branch1:branch3 &&
+ git p4 clone --dest=. --use-client-spec --detect-branches //depot@all &&
+ git log --all --graph --decorate --stat &&
+ git reset --hard p4/depot/branch1 &&
+ test_path_is_file file1 &&
+ test_path_is_file file2 &&
+ test_path_is_file file3 &&
+ test_path_is_file sub_file1 &&
+ grep update file2 &&
+ git reset --hard p4/depot/branch2 &&
+ test_path_is_file file1 &&
+ test_path_is_file file2 &&
+ test ! -f file3 &&
+ test_path_is_file sub_file1 &&
+ ! grep update file2 &&
+ git reset --hard p4/depot/branch3 &&
+ test_path_is_file file1 &&
+ test_path_is_file file2 &&
+ test_path_is_file file3 &&
+ test_path_is_file sub_file1 &&
+ grep update file2 &&
+ cd "$cli" &&
+ cd branch1 &&
+ p4 edit file2 &&
+ echo file2_ >>file2 &&
+ p4 submit -d "update file2 in branch1" &&
+ cd "$git" &&
+ git reset --hard p4/depot/branch1 &&
+ git p4 rebase &&
+ grep file2_ file2
+ )
+'
+
+# Now update a file in one of the branches in git and submit to P4
+test_expect_success 'Update a file in git side and submit to P4 using client view' '
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git reset --hard p4/depot/branch1 &&
+ echo "client spec" >> file1 &&
+ git add -u . &&
+ git commit -m "update file1 in branch1" &&
+ git config git-p4.skipSubmitEdit true &&
+ git p4 submit --verbose &&
+ cd "$cli" &&
+ p4 sync ... &&
+ cd branch1 &&
+ grep "client spec" file1
+ )
+'
+
+test_expect_success 'restart p4d (case folding enabled)' '
+ stop_and_cleanup_p4d &&
+ start_p4d -C1
+'
+
+#
+# 1: //depot/main/mf1
+# 2: integrate //depot/main/... -> //depot/branch1/...
+# 3: //depot/main/mf2
+# 4: //depot/BRANCH1/B1f3
+# 5: //depot/branch1/b1f4
+#
+test_expect_success !CASE_INSENSITIVE_FS 'basic p4 branches for case folding' '
+ (
+ cd "$cli" &&
+ mkdir -p main &&
+
+ echo mf1 >main/mf1 &&
+ p4 add main/mf1 &&
+ p4 submit -d "main/mf1" &&
+
+ p4 integrate //depot/main/... //depot/branch1/... &&
+ p4 submit -d "integrate main to branch1" &&
+
+ echo mf2 >main/mf2 &&
+ p4 add main/mf2 &&
+ p4 submit -d "main/mf2" &&
+
+ mkdir BRANCH1 &&
+ echo B1f3 >BRANCH1/B1f3 &&
+ p4 add BRANCH1/B1f3 &&
+ p4 submit -d "BRANCH1/B1f3" &&
+
+ echo b1f4 >branch1/b1f4 &&
+ p4 add branch1/b1f4 &&
+ p4 submit -d "branch1/b1f4"
+ )
+'
+
+# Check that files are properly split across branches when ignorecase is set
+test_expect_success !CASE_INSENSITIVE_FS 'git p4 clone, branchList branch definition, ignorecase' '
+ test_when_finished cleanup_git &&
+ test_create_repo "$git" &&
+ (
+ cd "$git" &&
+ git config git-p4.branchList main:branch1 &&
+ git config --type=bool core.ignoreCase true &&
+ git p4 clone --dest=. --detect-branches //depot@all &&
+
+ git log --all --graph --decorate --stat &&
+
+ git reset --hard p4/master &&
+ test_path_is_file mf1 &&
+ test_path_is_file mf2 &&
+ test_path_is_missing B1f3 &&
+ test_path_is_missing b1f4 &&
+
+ git reset --hard p4/depot/branch1 &&
+ test_path_is_file mf1 &&
+ test_path_is_missing mf2 &&
+ test_path_is_file B1f3 &&
+ test_path_is_file b1f4
+ )
+'
+
+# Check that files are properly split across branches when ignorecase is set, use-client-spec case
+test_expect_success !CASE_INSENSITIVE_FS 'git p4 clone with client-spec, branchList branch definition, ignorecase' '
+ client_view "//depot/... //client/..." &&
+ test_when_finished cleanup_git &&
+ test_create_repo "$git" &&
+ (
+ cd "$git" &&
+ git config git-p4.branchList main:branch1 &&
+ git config --type=bool core.ignoreCase true &&
+ git p4 clone --dest=. --use-client-spec --detect-branches //depot@all &&
+
+ git log --all --graph --decorate --stat &&
+
+ git reset --hard p4/master &&
+ test_path_is_file mf1 &&
+ test_path_is_file mf2 &&
+ test_path_is_missing B1f3 &&
+ test_path_is_missing b1f4 &&
+
+ git reset --hard p4/depot/branch1 &&
+ test_path_is_file mf1 &&
+ test_path_is_missing mf2 &&
+ test_path_is_file B1f3 &&
+ test_path_is_file b1f4
+ )
+'
+
+test_done
diff --git a/t/t9802-git-p4-filetype.sh b/t/t9802-git-p4-filetype.sh
new file mode 100755
index 0000000..2a6ee2a
--- /dev/null
+++ b/t/t9802-git-p4-filetype.sh
@@ -0,0 +1,370 @@
+#!/bin/sh
+
+test_description='git p4 filetype tests'
+
+. ./lib-git-p4.sh
+
+test_expect_success 'start p4d' '
+ start_p4d
+'
+
+#
+# This series of tests checks newline handling Both p4 and
+# git store newlines as \n, and have options to choose how
+# newlines appear in checked-out files.
+#
+test_expect_success 'p4 client newlines, unix' '
+ (
+ cd "$cli" &&
+ p4 client -o | sed "/LineEnd/s/:.*/:unix/" | p4 client -i &&
+ printf "unix\ncrlf\n" >f-unix &&
+ printf "unix\r\ncrlf\r\n" >f-unix-as-crlf &&
+ p4 add -t text f-unix &&
+ p4 submit -d f-unix &&
+
+ # LineEnd: unix; should be no change after sync
+ cp f-unix f-unix-orig &&
+ p4 sync -f &&
+ test_cmp f-unix-orig f-unix &&
+
+ # make sure stored in repo as unix newlines
+ # use sed to eat python-appended newline
+ p4 -G print //depot/f-unix | marshal_dump data 2 |\
+ sed \$d >f-unix-p4-print &&
+ test_cmp f-unix-orig f-unix-p4-print &&
+
+ # switch to win, make sure lf -> crlf
+ p4 client -o | sed "/LineEnd/s/:.*/:win/" | p4 client -i &&
+ p4 sync -f &&
+ test_cmp f-unix-as-crlf f-unix
+ )
+'
+
+test_expect_success 'p4 client newlines, win' '
+ (
+ cd "$cli" &&
+ p4 client -o | sed "/LineEnd/s/:.*/:win/" | p4 client -i &&
+ printf "win\r\ncrlf\r\n" >f-win &&
+ printf "win\ncrlf\n" >f-win-as-lf &&
+ p4 add -t text f-win &&
+ p4 submit -d f-win &&
+
+ # LineEnd: win; should be no change after sync
+ cp f-win f-win-orig &&
+ p4 sync -f &&
+ test_cmp f-win-orig f-win &&
+
+ # make sure stored in repo as unix newlines
+ # use sed to eat python-appened newline
+ p4 -G print //depot/f-win | marshal_dump data 2 |\
+ sed \$d >f-win-p4-print &&
+ test_cmp f-win-as-lf f-win-p4-print &&
+
+ # switch to unix, make sure lf -> crlf
+ p4 client -o | sed "/LineEnd/s/:.*/:unix/" | p4 client -i &&
+ p4 sync -f &&
+ test_cmp f-win-as-lf f-win
+ )
+'
+
+test_expect_success 'ensure blobs store only lf newlines' '
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git init &&
+ git p4 sync //depot@all &&
+
+ # verify the files in .git are stored only with newlines
+ o=$(git ls-tree p4/master -- f-unix | cut -f1 | cut -d\ -f3) &&
+ git cat-file blob $o >f-unix-blob &&
+ test_cmp "$cli"/f-unix-orig f-unix-blob &&
+
+ o=$(git ls-tree p4/master -- f-win | cut -f1 | cut -d\ -f3) &&
+ git cat-file blob $o >f-win-blob &&
+ test_cmp "$cli"/f-win-as-lf f-win-blob &&
+
+ rm f-unix-blob f-win-blob
+ )
+'
+
+test_expect_success 'gitattributes setting eol=lf produces lf newlines' '
+ test_when_finished cleanup_git &&
+ (
+ # checkout the files and make sure core.eol works as planned
+ cd "$git" &&
+ git init &&
+ echo "* eol=lf" >.gitattributes &&
+ git p4 sync //depot@all &&
+ git checkout -b master p4/master &&
+ test_cmp "$cli"/f-unix-orig f-unix &&
+ test_cmp "$cli"/f-win-as-lf f-win
+ )
+'
+
+test_expect_success 'gitattributes setting eol=crlf produces crlf newlines' '
+ test_when_finished cleanup_git &&
+ (
+ # checkout the files and make sure core.eol works as planned
+ cd "$git" &&
+ git init &&
+ echo "* eol=crlf" >.gitattributes &&
+ git p4 sync //depot@all &&
+ git checkout -b master p4/master &&
+ test_cmp "$cli"/f-unix-as-crlf f-unix &&
+ test_cmp "$cli"/f-win-orig f-win
+ )
+'
+
+test_expect_success 'crlf cleanup' '
+ (
+ cd "$cli" &&
+ rm f-unix-orig f-unix-as-crlf &&
+ rm f-win-orig f-win-as-lf &&
+ p4 client -o | sed "/LineEnd/s/:.*/:unix/" | p4 client -i &&
+ p4 sync -f
+ )
+'
+
+test_expect_success 'utf-16 file create' '
+ (
+ cd "$cli" &&
+
+ # p4 saves this verbatim
+ printf "three\nline\ntext\n" >f-ascii &&
+ p4 add -t text f-ascii &&
+
+ # p4 adds \377\376 header
+ cp f-ascii f-ascii-as-utf16 &&
+ p4 add -t utf16 f-ascii-as-utf16 &&
+
+ # p4 saves this exactly as iconv produced it
+ printf "three\nline\ntext\n" | iconv -f ascii -t utf-16 >f-utf16 &&
+ p4 add -t utf16 f-utf16 &&
+
+ # this also is unchanged
+ cp f-utf16 f-utf16-as-text &&
+ p4 add -t text f-utf16-as-text &&
+
+ p4 submit -d "f files" &&
+
+ # force update of client files
+ p4 sync -f
+ )
+'
+
+test_expect_success 'utf-16 file test' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot@all &&
+ (
+ cd "$git" &&
+
+ test_cmp "$cli/f-ascii" f-ascii &&
+ test_cmp "$cli/f-ascii-as-utf16" f-ascii-as-utf16 &&
+ test_cmp "$cli/f-utf16" f-utf16 &&
+ test_cmp "$cli/f-utf16-as-text" f-utf16-as-text
+ )
+'
+
+test_expect_success 'keyword file create' '
+ (
+ cd "$cli" &&
+
+ printf "id\n\$Id\$\n\$Author\$\ntext\n" >k-text-k &&
+ p4 add -t text+k k-text-k &&
+
+ cp k-text-k k-text-ko &&
+ p4 add -t text+ko k-text-ko &&
+
+ cat k-text-k | iconv -f ascii -t utf-16 >k-utf16-k &&
+ p4 add -t utf16+k k-utf16-k &&
+
+ cp k-utf16-k k-utf16-ko &&
+ p4 add -t utf16+ko k-utf16-ko &&
+
+ p4 submit -d "k files" &&
+ p4 sync -f
+ )
+'
+
+build_smush() {
+ cat >k_smush.py <<-\EOF &&
+ import re, sys
+ sys.stdout.write(re.sub(r'(?i)\$(Id|Header|Author|Date|DateTime|Change|File|Revision):[^$]*\$', r'$\1$', sys.stdin.read()))
+ EOF
+ cat >ko_smush.py <<-\EOF
+ import re, sys
+ sys.stdout.write(re.sub(r'(?i)\$(Id|Header):[^$]*\$', r'$\1$', sys.stdin.read()))
+ EOF
+}
+
+test_expect_success 'keyword file test' '
+ build_smush &&
+ test_when_finished rm -f k_smush.py ko_smush.py &&
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot@all &&
+ (
+ cd "$git" &&
+
+ # text, ensure unexpanded
+ "$PYTHON_PATH" "$TRASH_DIRECTORY/k_smush.py" <"$cli/k-text-k" >cli-k-text-k-smush &&
+ test_cmp cli-k-text-k-smush k-text-k &&
+ "$PYTHON_PATH" "$TRASH_DIRECTORY/ko_smush.py" <"$cli/k-text-ko" >cli-k-text-ko-smush &&
+ test_cmp cli-k-text-ko-smush k-text-ko &&
+
+ # utf16, even though p4 expands keywords, git p4 does not
+ # try to undo that
+ test_cmp "$cli/k-utf16-k" k-utf16-k &&
+ test_cmp "$cli/k-utf16-ko" k-utf16-ko
+ )
+'
+
+build_gendouble() {
+ cat >gendouble.py <<-\EOF
+ import sys
+ import struct
+
+ s = struct.pack(b">LL18s",
+ 0x00051607, # AppleDouble
+ 0x00020000, # version 2
+ b"" # pad to 26 bytes
+ )
+ getattr(sys.stdout, 'buffer', sys.stdout).write(s)
+ EOF
+}
+
+test_expect_success 'ignore apple' '
+ test_when_finished rm -f gendouble.py &&
+ build_gendouble &&
+ (
+ cd "$cli" &&
+ test-tool genrandom apple 1024 >double.png &&
+ "$PYTHON_PATH" "$TRASH_DIRECTORY/gendouble.py" >%double.png &&
+ p4 add -t apple double.png &&
+ p4 submit -d appledouble
+ ) &&
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot@all &&
+ (
+ cd "$git" &&
+ test ! -f double.png
+ )
+'
+
+test_expect_success SYMLINKS 'create p4 symlink' '
+ cd "$cli" &&
+ ln -s symlink-target symlink &&
+ p4 add symlink &&
+ p4 submit -d "add symlink"
+'
+
+test_expect_success SYMLINKS 'ensure p4 symlink parsed correctly' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot@all &&
+ (
+ cd "$git" &&
+ test -L symlink &&
+ test $(test_readlink symlink) = symlink-target
+ )
+'
+
+test_expect_success SYMLINKS 'empty symlink target' '
+ (
+ # first create the file as a file
+ cd "$cli" &&
+ >empty-symlink &&
+ p4 add empty-symlink &&
+ p4 submit -d "add empty-symlink as a file"
+ ) &&
+ (
+ # now change it to be a symlink to "target1"
+ cd "$cli" &&
+ p4 edit empty-symlink &&
+ p4 reopen -t symlink empty-symlink &&
+ rm empty-symlink &&
+ ln -s target1 empty-symlink &&
+ p4 add empty-symlink &&
+ p4 submit -d "make empty-symlink point to target1"
+ ) &&
+ (
+ # Hack the p4 depot to make the symlink point to nothing;
+ # this should not happen in reality, but shows up
+ # in p4 repos in the wild.
+ #
+ # The sed expression changes this:
+ # @@
+ # text
+ # @target1
+ # @
+ # to this:
+ # @@
+ # text
+ # @@
+ #
+ cd "$db/depot" &&
+ sed "/@target1/{; s/target1/@/; n; d; }" \
+ empty-symlink,v >empty-symlink,v.tmp &&
+ mv empty-symlink,v.tmp empty-symlink,v
+ ) &&
+ (
+ # Make sure symlink really is empty. Asking
+ # p4 to sync here will make it generate errors.
+ cd "$cli" &&
+ p4 print -q //depot/empty-symlink#2 >out &&
+ test_must_be_empty out
+ ) &&
+ test_when_finished cleanup_git &&
+
+ # make sure git p4 handles it without error
+ git p4 clone --dest="$git" //depot@all &&
+
+ # fix the symlink, make it point to "target2"
+ (
+ cd "$cli" &&
+ p4 open empty-symlink &&
+ rm empty-symlink &&
+ ln -s target2 empty-symlink &&
+ p4 submit -d "make empty-symlink point to target2"
+ ) &&
+ cleanup_git &&
+ git p4 clone --dest="$git" //depot@all &&
+ (
+ cd "$git" &&
+ test $(test_readlink empty-symlink) = target2
+ )
+'
+
+test_expect_success SYMLINKS 'utf-8 with and without BOM in text file' '
+ (
+ cd "$cli" &&
+
+ # some utf8 content
+ echo some tǣxt >utf8-nobom-test &&
+
+ # same utf8 content as before but with bom
+ echo some tǣxt | sed '\''s/^/\xef\xbb\xbf/'\'' >utf8-bom-test &&
+
+ # bom only
+ dd bs=1 count=3 if=utf8-bom-test of=utf8-bom-empty-test &&
+
+ p4 add utf8-nobom-test utf8-bom-test utf8-bom-empty-test &&
+ p4 submit -d "add utf8 test files"
+ ) &&
+ test_when_finished cleanup_git &&
+
+ git p4 clone --dest="$git" //depot@all &&
+ (
+ cd "$git" &&
+ git checkout refs/remotes/p4/master &&
+
+ echo some tǣxt >utf8-nobom-check &&
+ test_cmp utf8-nobom-check utf8-nobom-test &&
+
+ echo some tǣxt | sed '\''s/^/\xef\xbb\xbf/'\'' >utf8-bom-check &&
+ test_cmp utf8-bom-check utf8-bom-test &&
+
+ dd bs=1 count=3 if=utf8-bom-check of=utf8-bom-empty-check &&
+ test_cmp utf8-bom-empty-check utf8-bom-empty-test
+ )
+'
+
+test_done
diff --git a/t/t9803-git-p4-shell-metachars.sh b/t/t9803-git-p4-shell-metachars.sh
new file mode 100755
index 0000000..2913277
--- /dev/null
+++ b/t/t9803-git-p4-shell-metachars.sh
@@ -0,0 +1,108 @@
+#!/bin/sh
+
+test_description='git p4 transparency to shell metachars in filenames'
+
+. ./lib-git-p4.sh
+
+test_expect_success 'start p4d' '
+ start_p4d
+'
+
+test_expect_success 'init depot' '
+ (
+ cd "$cli" &&
+ echo file1 >file1 &&
+ p4 add file1 &&
+ p4 submit -d "file1"
+ )
+'
+
+test_expect_success 'shell metachars in filenames' '
+ git p4 clone --dest="$git" //depot &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git config git-p4.skipSubmitEditCheck true &&
+ echo f1 >foo\$bar &&
+ git add foo\$bar &&
+ echo f2 >"file with spaces" &&
+ git add "file with spaces" &&
+ git commit -m "add files" &&
+ P4EDITOR="test-tool chmtime +5" git p4 submit
+ ) &&
+ (
+ cd "$cli" &&
+ p4 sync ... &&
+ test -e "file with spaces" &&
+ test -e "foo\$bar"
+ )
+'
+
+test_expect_success 'deleting with shell metachars' '
+ git p4 clone --dest="$git" //depot &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git config git-p4.skipSubmitEditCheck true &&
+ git rm foo\$bar &&
+ git rm file\ with\ spaces &&
+ git commit -m "remove files" &&
+ P4EDITOR="test-tool chmtime +5" git p4 submit
+ ) &&
+ (
+ cd "$cli" &&
+ p4 sync ... &&
+ test ! -e "file with spaces" &&
+ test ! -e foo\$bar
+ )
+'
+
+# Create a branch with a shell metachar in its name
+#
+# 1. //depot/main
+# 2. //depot/branch$3
+
+test_expect_success 'branch with shell char' '
+ test_when_finished cleanup_git &&
+ test_create_repo "$git" &&
+ (
+ cd "$cli" &&
+
+ mkdir -p main &&
+
+ echo f1 >main/f1 &&
+ p4 add main/f1 &&
+ p4 submit -d "main/f1" &&
+
+ p4 integrate //depot/main/... //depot/branch\$3/... &&
+ p4 submit -d "integrate main to branch\$3" &&
+
+ echo f1 >branch\$3/shell_char_branch_file &&
+ p4 add branch\$3/shell_char_branch_file &&
+ p4 submit -d "branch\$3/shell_char_branch_file" &&
+
+ p4 branch -i <<-EOF &&
+ Branch: branch\$3
+ View: //depot/main/... //depot/branch\$3/...
+ EOF
+
+ p4 edit main/f1 &&
+ echo "a change" >> main/f1 &&
+ p4 submit -d "a change" main/f1 &&
+
+ p4 integrate -b branch\$3 &&
+ p4 resolve -am branch\$3/... &&
+ p4 submit -d "integrate main to branch\$3" &&
+
+ cd "$git" &&
+
+ git config git-p4.branchList main:branch\$3 &&
+ git p4 clone --dest=. --detect-branches //depot@all &&
+ git log --all --graph --decorate --stat &&
+ git reset --hard p4/depot/branch\$3 &&
+ test -f shell_char_branch_file &&
+ test -f f1
+ )
+'
+
+test_done
diff --git a/t/t9804-git-p4-label.sh b/t/t9804-git-p4-label.sh
new file mode 100755
index 0000000..3236457
--- /dev/null
+++ b/t/t9804-git-p4-label.sh
@@ -0,0 +1,111 @@
+#!/bin/sh
+
+test_description='git p4 label tests'
+
+. ./lib-git-p4.sh
+
+test_expect_success 'start p4d' '
+ start_p4d
+'
+
+# Basic p4 label tests.
+#
+# Note: can't have more than one label per commit - others
+# are silently discarded.
+#
+test_expect_success 'basic p4 labels' '
+ test_when_finished cleanup_git &&
+ (
+ cd "$cli" &&
+ mkdir -p main &&
+
+ echo f1 >main/f1 &&
+ p4 add main/f1 &&
+ p4 submit -d "main/f1" &&
+
+ echo f2 >main/f2 &&
+ p4 add main/f2 &&
+ p4 submit -d "main/f2" &&
+
+ echo f3 >main/file_with_\$metachar &&
+ p4 add main/file_with_\$metachar &&
+ p4 submit -d "file with metachar" &&
+
+ p4 tag -l tag_f1_only main/f1 &&
+ p4 tag -l tag_with\$_shell_char main/... &&
+
+ echo f4 >main/f4 &&
+ p4 add main/f4 &&
+ p4 submit -d "main/f4" &&
+
+ p4 label -i <<-EOF &&
+ Label: long_label
+ Description:
+ A Label first line
+ A Label second line
+ View: //depot/...
+ EOF
+
+ p4 tag -l long_label ... &&
+
+ p4 labels ... &&
+
+ git p4 clone --dest="$git" --detect-labels //depot@all &&
+ cd "$git" &&
+
+ git tag &&
+ git tag >taglist &&
+ test_line_count = 3 taglist &&
+
+ cd main &&
+ git checkout tag_tag_f1_only &&
+ ! test -f f2 &&
+ git checkout tag_tag_with\$_shell_char &&
+ test -f f1 && test -f f2 && test -f file_with_\$metachar &&
+
+ git show tag_long_label | grep -q "A Label second line"
+ )
+'
+
+# Test some label corner cases:
+#
+# - two tags on the same file; both should be available
+# - a tag that is only on one file; this kind of tag
+# cannot be imported (at least not easily).
+
+test_expect_failure 'two labels on the same changelist' '
+ test_when_finished cleanup_git &&
+ (
+ cd "$cli" &&
+ mkdir -p main &&
+
+ p4 edit main/f1 main/f2 &&
+ echo "hello world" >main/f1 &&
+ echo "not in the tag" >main/f2 &&
+ p4 submit -d "main/f[12]: testing two labels" &&
+
+ p4 tag -l tag_f1_1 main/... &&
+ p4 tag -l tag_f1_2 main/... &&
+
+ p4 labels ... &&
+
+ git p4 clone --dest="$git" --detect-labels //depot@all &&
+ cd "$git" &&
+
+ git tag | grep tag_f1 &&
+ git tag | grep -q tag_f1_1 &&
+ git tag | grep -q tag_f1_2 &&
+
+ cd main &&
+
+ git checkout tag_tag_f1_1 &&
+ ls &&
+ test -f f1 &&
+
+ git checkout tag_tag_f1_2 &&
+ ls &&
+ test -f f1
+ )
+'
+
+test_done
diff --git a/t/t9805-git-p4-skip-submit-edit.sh b/t/t9805-git-p4-skip-submit-edit.sh
new file mode 100755
index 0000000..90ef647
--- /dev/null
+++ b/t/t9805-git-p4-skip-submit-edit.sh
@@ -0,0 +1,101 @@
+#!/bin/sh
+
+test_description='git p4 skipSubmitEdit config variables'
+
+. ./lib-git-p4.sh
+
+test_expect_success 'start p4d' '
+ start_p4d
+'
+
+test_expect_success 'init depot' '
+ (
+ cd "$cli" &&
+ echo file1 >file1 &&
+ p4 add file1 &&
+ p4 submit -d "change 1"
+ )
+'
+
+# this works because P4EDITOR is set to true
+test_expect_success 'no config, unedited, say yes' '
+ git p4 clone --dest="$git" //depot &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ echo line >>file1 &&
+ git commit -a -m "change 2" &&
+ echo y | git p4 submit &&
+ p4 changes //depot/... >wc &&
+ test_line_count = 2 wc
+ )
+'
+
+test_expect_success 'no config, unedited, say no' '
+ git p4 clone --dest="$git" //depot &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ echo line >>file1 &&
+ git commit -a -m "change 3 (not really)" &&
+ printf "bad response\nn\n" | test_expect_code 1 git p4 submit &&
+ p4 changes //depot/... >wc &&
+ test_line_count = 2 wc
+ )
+'
+
+test_expect_success 'skipSubmitEdit' '
+ git p4 clone --dest="$git" //depot &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git config git-p4.skipSubmitEdit true &&
+ # will fail if editor is even invoked
+ git config core.editor /bin/false &&
+ echo line >>file1 &&
+ git commit -a -m "change 3" &&
+ git p4 submit &&
+ p4 changes //depot/... >wc &&
+ test_line_count = 3 wc
+ )
+'
+
+test_expect_success 'skipSubmitEditCheck' '
+ git p4 clone --dest="$git" //depot &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git config git-p4.skipSubmitEditCheck true &&
+ echo line >>file1 &&
+ git commit -a -m "change 4" &&
+ git p4 submit &&
+ p4 changes //depot/... >wc &&
+ test_line_count = 4 wc
+ )
+'
+
+# check the normal case, where the template really is edited
+test_expect_success 'no config, edited' '
+ git p4 clone --dest="$git" //depot &&
+ test_when_finished cleanup_git &&
+ test_when_finished "rm ed.sh" &&
+ cat >ed.sh <<-EOF &&
+ #!$SHELL_PATH
+ sleep 1
+ touch "\$1"
+ exit 0
+ EOF
+ chmod 755 ed.sh &&
+ (
+ cd "$git" &&
+ echo line >>file1 &&
+ git commit -a -m "change 5" &&
+ P4EDITOR="\"$TRASH_DIRECTORY/ed.sh\"" &&
+ export P4EDITOR &&
+ git p4 submit &&
+ p4 changes //depot/... >wc &&
+ test_line_count = 5 wc
+ )
+'
+
+test_done
diff --git a/t/t9806-git-p4-options.sh b/t/t9806-git-p4-options.sh
new file mode 100755
index 0000000..c26d297
--- /dev/null
+++ b/t/t9806-git-p4-options.sh
@@ -0,0 +1,306 @@
+#!/bin/sh
+
+test_description='git p4 options'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./lib-git-p4.sh
+
+test_expect_success 'start p4d' '
+ start_p4d
+'
+
+test_expect_success 'init depot' '
+ (
+ cd "$cli" &&
+ echo file1 >file1 &&
+ p4 add file1 &&
+ p4 submit -d "change 1" &&
+ echo file2 >file2 &&
+ p4 add file2 &&
+ p4 submit -d "change 2" &&
+ echo file3 >file3 &&
+ p4 add file3 &&
+ p4 submit -d "change 3"
+ )
+'
+
+test_expect_success 'clone no --git-dir' '
+ test_must_fail git p4 clone --git-dir=xx //depot
+'
+
+test_expect_success 'clone --branch should checkout main' '
+ git p4 clone --branch=refs/remotes/p4/sb --dest="$git" //depot &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git rev-parse refs/remotes/p4/sb >sb &&
+ git rev-parse refs/heads/main >main &&
+ test_cmp sb main &&
+ git rev-parse HEAD >head &&
+ test_cmp sb head
+ )
+'
+
+test_expect_success 'sync when no master branch prints a nice error' '
+ test_when_finished cleanup_git &&
+ git p4 clone --branch=refs/remotes/p4/sb --dest="$git" //depot@2 &&
+ (
+ cd "$git" &&
+ test_must_fail git p4 sync 2>err &&
+ grep "Error: no branch refs/remotes/p4/master" err
+ )
+'
+
+test_expect_success 'sync --branch builds the full ref name correctly' '
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git init &&
+
+ git p4 sync --branch=b1 //depot &&
+ git rev-parse --verify refs/remotes/p4/b1 &&
+ git p4 sync --branch=p4/b2 //depot &&
+ git rev-parse --verify refs/remotes/p4/b2 &&
+
+ git p4 sync --import-local --branch=h1 //depot &&
+ git rev-parse --verify refs/heads/p4/h1 &&
+ git p4 sync --import-local --branch=p4/h2 //depot &&
+ git rev-parse --verify refs/heads/p4/h2 &&
+
+ git p4 sync --branch=refs/stuff //depot &&
+ git rev-parse --verify refs/stuff
+ )
+'
+
+# engages --detect-branches code, which will do filename filtering so
+# no sync to either b1 or b2
+test_expect_success 'sync when two branches but no master should noop' '
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git init &&
+ git p4 sync --branch=refs/remotes/p4/b1 //depot@2 &&
+ git p4 sync --branch=refs/remotes/p4/b2 //depot@2 &&
+ git p4 sync &&
+ git show -s --format=%s refs/remotes/p4/b1 >show &&
+ grep "Initial import" show &&
+ git show -s --format=%s refs/remotes/p4/b2 >show &&
+ grep "Initial import" show
+ )
+'
+
+test_expect_success 'sync --branch updates specific branch, no detection' '
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git init &&
+ git p4 sync --branch=b1 //depot@2 &&
+ git p4 sync --branch=b2 //depot@2 &&
+ git p4 sync --branch=b2 &&
+ git show -s --format=%s refs/remotes/p4/b1 >show &&
+ grep "Initial import" show &&
+ git show -s --format=%s refs/remotes/p4/b2 >show &&
+ grep "change 3" show
+ )
+'
+
+# allows using the refname "p4" as a short name for p4/master
+test_expect_success 'clone creates HEAD symbolic reference' '
+ git p4 clone --dest="$git" //depot &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git rev-parse --verify refs/remotes/p4/master >master &&
+ git rev-parse --verify p4 >p4 &&
+ test_cmp master p4
+ )
+'
+
+test_expect_success 'clone --branch creates HEAD symbolic reference' '
+ git p4 clone --branch=refs/remotes/p4/sb --dest="$git" //depot &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git rev-parse --verify refs/remotes/p4/sb >sb &&
+ git rev-parse --verify p4 >p4 &&
+ test_cmp sb p4
+ )
+'
+
+test_expect_success 'clone --changesfile' '
+ test_when_finished "rm cf" &&
+ printf "1\n3\n" >cf &&
+ git p4 clone --changesfile="$TRASH_DIRECTORY/cf" --dest="$git" //depot &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git log --oneline p4/master >lines &&
+ test_line_count = 2 lines &&
+ test_path_is_file file1 &&
+ test_path_is_missing file2 &&
+ test_path_is_file file3
+ )
+'
+
+test_expect_success 'clone --changesfile, @all' '
+ test_when_finished "rm cf" &&
+ printf "1\n3\n" >cf &&
+ test_must_fail git p4 clone --changesfile="$TRASH_DIRECTORY/cf" --dest="$git" //depot@all
+'
+
+# imports both main and p4/master in refs/heads
+# requires --import-local on sync to find p4 refs/heads
+# does not update main on sync, just p4/master
+test_expect_success 'clone/sync --import-local' '
+ git p4 clone --import-local --dest="$git" //depot@1,2 &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git log --oneline refs/heads/main >lines &&
+ test_line_count = 2 lines &&
+ git log --oneline refs/heads/p4/master >lines &&
+ test_line_count = 2 lines &&
+ test_must_fail git p4 sync &&
+
+ git p4 sync --import-local &&
+ git log --oneline refs/heads/main >lines &&
+ test_line_count = 2 lines &&
+ git log --oneline refs/heads/p4/master >lines &&
+ test_line_count = 3 lines
+ )
+'
+
+test_expect_success 'clone --max-changes' '
+ git p4 clone --dest="$git" --max-changes 2 //depot@all &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git log --oneline refs/heads/main >lines &&
+ test_line_count = 2 lines
+ )
+'
+
+test_expect_success 'clone --keep-path' '
+ (
+ cd "$cli" &&
+ mkdir -p sub/dir &&
+ echo f4 >sub/dir/f4 &&
+ p4 add sub/dir/f4 &&
+ p4 submit -d "change 4"
+ ) &&
+ git p4 clone --dest="$git" --keep-path //depot/sub/dir@all &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ test_path_is_missing f4 &&
+ test_path_is_file sub/dir/f4
+ ) &&
+ cleanup_git &&
+ git p4 clone --dest="$git" //depot/sub/dir@all &&
+ (
+ cd "$git" &&
+ test_path_is_file f4 &&
+ test_path_is_missing sub/dir/f4
+ )
+'
+
+# clone --use-client-spec must still specify a depot path
+# if given, it should rearrange files according to client spec
+# when it has view lines that match the depot path
+# XXX: should clone/sync just use the client spec exactly, rather
+# than needing depot paths?
+test_expect_success 'clone --use-client-spec' '
+ (
+ # big usage message
+ exec >/dev/null &&
+ test_must_fail git p4 clone --dest="$git" --use-client-spec
+ ) &&
+ # build a different client
+ cli2="$TRASH_DIRECTORY/cli2" &&
+ mkdir -p "$cli2" &&
+ test_when_finished "rmdir \"$cli2\"" &&
+ test_when_finished cleanup_git &&
+ (
+ # group P4CLIENT and cli changes in a sub-shell
+ P4CLIENT=client2 &&
+ cli="$cli2" &&
+ client_view "//depot/sub/... //client2/bus/..." &&
+ git p4 clone --dest="$git" --use-client-spec //depot/... &&
+ (
+ cd "$git" &&
+ test_path_is_file bus/dir/f4 &&
+ test_path_is_missing file1
+ ) &&
+ cleanup_git &&
+ # same thing again, this time with variable instead of option
+ (
+ cd "$git" &&
+ git init &&
+ git config git-p4.useClientSpec true &&
+ git p4 sync //depot/... &&
+ git checkout -b main p4/master &&
+ test_path_is_file bus/dir/f4 &&
+ test_path_is_missing file1
+ )
+ )
+'
+
+test_expect_success 'submit works with no p4/master' '
+ test_when_finished cleanup_git &&
+ git p4 clone --branch=b1 //depot@1,2 --destination="$git" &&
+ (
+ cd "$git" &&
+ test_commit submit-1-branch &&
+ git config git-p4.skipSubmitEdit true &&
+ git p4 submit --branch=b1
+ )
+'
+
+# The sync/rebase part post-submit will engage detect-branches
+# machinery which will not do anything in this particular test.
+test_expect_success 'submit works with two branches' '
+ test_when_finished cleanup_git &&
+ git p4 clone --branch=b1 //depot@1,2 --destination="$git" &&
+ (
+ cd "$git" &&
+ git p4 sync --branch=b2 //depot@1,3 &&
+ test_commit submit-2-branches &&
+ git config git-p4.skipSubmitEdit true &&
+ git p4 submit
+ )
+'
+
+test_expect_success 'use --git-dir option and GIT_DIR' '
+ test_when_finished cleanup_git &&
+ git p4 clone //depot --destination="$git" &&
+ (
+ cd "$git" &&
+ git config git-p4.skipSubmitEdit true &&
+ test_commit first-change &&
+ git p4 submit --git-dir "$git"
+ ) &&
+ (
+ cd "$cli" &&
+ p4 sync &&
+ test_path_is_file first-change.t &&
+ echo "cli_file" >cli_file.t &&
+ p4 add cli_file.t &&
+ p4 submit -d "cli change"
+ ) &&
+ (git --git-dir "$git" p4 sync) &&
+ (cd "$git" && git checkout -q p4/master) &&
+ test_path_is_file "$git"/cli_file.t &&
+ (
+ cd "$cli" &&
+ echo "cli_file2" >cli_file2.t &&
+ p4 add cli_file2.t &&
+ p4 submit -d "cli change2"
+ ) &&
+ (GIT_DIR="$git" git p4 sync) &&
+ (cd "$git" && git checkout -q p4/master) &&
+ test_path_is_file "$git"/cli_file2.t
+'
+
+test_done
diff --git a/t/t9807-git-p4-submit.sh b/t/t9807-git-p4-submit.sh
new file mode 100755
index 0000000..af4b286
--- /dev/null
+++ b/t/t9807-git-p4-submit.sh
@@ -0,0 +1,599 @@
+#!/bin/sh
+
+test_description='git p4 submit'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./lib-git-p4.sh
+
+test_expect_success 'start p4d' '
+ start_p4d
+'
+
+test_expect_success 'init depot' '
+ (
+ cd "$cli" &&
+ echo file1 >file1 &&
+ p4 add file1 &&
+ p4 submit -d "change 1"
+ )
+'
+
+test_expect_success 'is_cli_file_writeable function' '
+ (
+ cd "$cli" &&
+ echo a >a &&
+ is_cli_file_writeable a &&
+ ! is_cli_file_writeable file1 &&
+ rm a
+ )
+'
+
+test_expect_success 'submit with no client dir' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ echo file2 >file2 &&
+ git add file2 &&
+ git commit -m "git commit 2" &&
+ rm -rf "$cli" &&
+ git config git-p4.skipSubmitEdit true &&
+ git p4 submit
+ ) &&
+ (
+ cd "$cli" &&
+ test_path_is_file file1 &&
+ test_path_is_file file2
+ )
+'
+
+# make two commits, but tell it to apply only from HEAD^
+test_expect_success 'submit --origin' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ test_commit "file3" &&
+ test_commit "file4" &&
+ git config git-p4.skipSubmitEdit true &&
+ git p4 submit --origin=HEAD^
+ ) &&
+ (
+ cd "$cli" &&
+ test_path_is_missing "file3.t" &&
+ test_path_is_file "file4.t"
+ )
+'
+
+test_expect_success 'submit --dry-run' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ test_commit "dry-run1" &&
+ test_commit "dry-run2" &&
+ git p4 submit --dry-run >out &&
+ test_grep "Would apply" out
+ ) &&
+ (
+ cd "$cli" &&
+ test_path_is_missing "dry-run1.t" &&
+ test_path_is_missing "dry-run2.t"
+ )
+'
+
+test_expect_success 'submit --dry-run --export-labels' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ echo dry-run1 >dry-run1 &&
+ git add dry-run1 &&
+ git commit -m "dry-run1" dry-run1 &&
+ git config git-p4.skipSubmitEdit true &&
+ git p4 submit &&
+ echo dry-run2 >dry-run2 &&
+ git add dry-run2 &&
+ git commit -m "dry-run2" dry-run2 &&
+ git tag -m "dry-run-tag1" dry-run-tag1 HEAD^ &&
+ git p4 submit --dry-run --export-labels >out &&
+ test_grep "Would create p4 label" out
+ ) &&
+ (
+ cd "$cli" &&
+ test_path_is_file "dry-run1" &&
+ test_path_is_missing "dry-run2"
+ )
+'
+
+test_expect_success 'submit with allowSubmit' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ test_commit "file5" &&
+ git config git-p4.skipSubmitEdit true &&
+ git config git-p4.allowSubmit "nobranch" &&
+ test_must_fail git p4 submit &&
+ git config git-p4.allowSubmit "nobranch,main" &&
+ git p4 submit
+ )
+'
+
+test_expect_success 'submit with master branch name from argv' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ test_commit "file6" &&
+ git config git-p4.skipSubmitEdit true &&
+ test_must_fail git p4 submit nobranch &&
+ git branch otherbranch &&
+ git reset --hard HEAD^ &&
+ test_commit "file7" &&
+ git p4 submit otherbranch
+ ) &&
+ (
+ cd "$cli" &&
+ test_path_is_file "file6.t" &&
+ test_path_is_missing "file7.t"
+ )
+'
+
+test_expect_success 'allow submit from branch with same revision but different name' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ test_commit "file8" &&
+ git checkout -b branch1 &&
+ git checkout -b branch2 &&
+ git config git-p4.skipSubmitEdit true &&
+ git config git-p4.allowSubmit "branch1" &&
+ test_must_fail git p4 submit &&
+ git checkout branch1 &&
+ git p4 submit
+ )
+'
+
+# make two commits, but tell it to apply only one
+
+test_expect_success 'submit --commit one' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ test_commit "file9" &&
+ test_commit "file10" &&
+ git config git-p4.skipSubmitEdit true &&
+ git p4 submit --commit HEAD
+ ) &&
+ (
+ cd "$cli" &&
+ test_path_is_missing "file9.t" &&
+ test_path_is_file "file10.t"
+ )
+'
+
+# make three commits, but tell it to apply only range
+
+test_expect_success 'submit --commit range' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ test_commit "file11" &&
+ test_commit "file12" &&
+ test_commit "file13" &&
+ git config git-p4.skipSubmitEdit true &&
+ git p4 submit --commit HEAD~2..HEAD
+ ) &&
+ (
+ cd "$cli" &&
+ test_path_is_missing "file11.t" &&
+ test_path_is_file "file12.t" &&
+ test_path_is_file "file13.t"
+ )
+'
+
+#
+# Basic submit tests, the five handled cases
+#
+
+test_expect_success 'submit modify' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ git config git-p4.skipSubmitEdit true &&
+ echo line >>file1 &&
+ git add file1 &&
+ git commit -m file1 &&
+ git p4 submit
+ ) &&
+ (
+ cd "$cli" &&
+ test_path_is_file file1 &&
+ test_line_count = 2 file1
+ )
+'
+
+test_expect_success 'submit add' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ git config git-p4.skipSubmitEdit true &&
+ echo file13 >file13 &&
+ git add file13 &&
+ git commit -m file13 &&
+ git p4 submit
+ ) &&
+ (
+ cd "$cli" &&
+ test_path_is_file file13
+ )
+'
+
+test_expect_success 'submit delete' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ git config git-p4.skipSubmitEdit true &&
+ git rm file4.t &&
+ git commit -m "delete file4.t" &&
+ git p4 submit
+ ) &&
+ (
+ cd "$cli" &&
+ test_path_is_missing file4.t
+ )
+'
+
+test_expect_success 'submit copy' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ git config git-p4.skipSubmitEdit true &&
+ git config git-p4.detectCopies true &&
+ git config git-p4.detectCopiesHarder true &&
+ cp file5.t file5.ta &&
+ git add file5.ta &&
+ git commit -m "copy to file5.ta" &&
+ git p4 submit
+ ) &&
+ (
+ cd "$cli" &&
+ test_path_is_file file5.ta &&
+ ! is_cli_file_writeable file5.ta
+ )
+'
+
+test_expect_success 'submit rename' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ git config git-p4.skipSubmitEdit true &&
+ git config git-p4.detectRenames true &&
+ git mv file6.t file6.ta &&
+ git commit -m "rename file6.t to file6.ta" &&
+ git p4 submit
+ ) &&
+ (
+ cd "$cli" &&
+ test_path_is_missing file6.t &&
+ test_path_is_file file6.ta &&
+ ! is_cli_file_writeable file6.ta
+ )
+'
+
+#
+# Converting git commit message to p4 change description, including
+# parsing out the optional Jobs: line.
+#
+test_expect_success 'simple one-line description' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ echo desc2 >desc2 &&
+ git add desc2 &&
+ cat >msg <<-EOF &&
+ One-line description line for desc2.
+ EOF
+ git commit -F - <msg &&
+ git config git-p4.skipSubmitEdit true &&
+ git p4 submit &&
+ change=$(p4 -G changes -m 1 //depot/... | \
+ marshal_dump change) &&
+ # marshal_dump always adds a newline
+ p4 -G describe $change | marshal_dump desc | sed \$d >pmsg &&
+ test_cmp msg pmsg
+ )
+'
+
+test_expect_success 'description with odd formatting' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ echo desc3 >desc3 &&
+ git add desc3 &&
+ (
+ printf "subject line\n\n\tExtra tab\nline.\n\n" &&
+ printf "Description:\n\tBogus description marker\n\n" &&
+ # git commit eats trailing newlines; only use one
+ printf "Files:\n\tBogus descs marker\n"
+ ) >msg &&
+ git commit -F - <msg &&
+ git config git-p4.skipSubmitEdit true &&
+ git p4 submit &&
+ change=$(p4 -G changes -m 1 //depot/... | \
+ marshal_dump change) &&
+ # marshal_dump always adds a newline
+ p4 -G describe $change | marshal_dump desc | sed \$d >pmsg &&
+ test_cmp msg pmsg
+ )
+'
+
+make_job() {
+ name="$1" &&
+ tab="$(printf \\t)" &&
+ p4 job -o | \
+ sed -e "/^Job:/s/.*/Job: $name/" \
+ -e "/^Description/{ n; s/.*/$tab job text/; }" | \
+ p4 job -i
+}
+
+test_expect_success 'description with Jobs section at end' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ echo desc4 >desc4 &&
+ git add desc4 &&
+ echo 6060842 >jobname &&
+ (
+ printf "subject line\n\n\tExtra tab\nline.\n\n" &&
+ printf "Files:\n\tBogus files marker\n" &&
+ printf "Junk: 3164175\n" &&
+ printf "Jobs: $(cat jobname)\n"
+ ) >msg &&
+ git commit -F - <msg &&
+ git config git-p4.skipSubmitEdit true &&
+ # build a job
+ make_job $(cat jobname) &&
+ git p4 submit &&
+ change=$(p4 -G changes -m 1 //depot/... | \
+ marshal_dump change) &&
+ # marshal_dump always adds a newline
+ p4 -G describe $change | marshal_dump desc | sed \$d >pmsg &&
+ # make sure Jobs line and all following is gone
+ sed "/^Jobs:/,\$d" msg >jmsg &&
+ test_cmp jmsg pmsg &&
+ # make sure p4 knows about job
+ p4 -G describe $change | marshal_dump job0 >job0 &&
+ test_cmp jobname job0
+ )
+'
+
+test_expect_success 'description with Jobs and values on separate lines' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ echo desc5 >desc5 &&
+ git add desc5 &&
+ echo PROJ-6060842 >jobname1 &&
+ echo PROJ-6060847 >jobname2 &&
+ (
+ printf "subject line\n\n\tExtra tab\nline.\n\n" &&
+ printf "Files:\n\tBogus files marker\n" &&
+ printf "Junk: 3164175\n" &&
+ printf "Jobs:\n" &&
+ printf "\t$(cat jobname1)\n" &&
+ printf "\t$(cat jobname2)\n"
+ ) >msg &&
+ git commit -F - <msg &&
+ git config git-p4.skipSubmitEdit true &&
+ # build two jobs
+ make_job $(cat jobname1) &&
+ make_job $(cat jobname2) &&
+ git p4 submit &&
+ change=$(p4 -G changes -m 1 //depot/... | \
+ marshal_dump change) &&
+ # marshal_dump always adds a newline
+ p4 -G describe $change | marshal_dump desc | sed \$d >pmsg &&
+ # make sure Jobs line and all following is gone
+ sed "/^Jobs:/,\$d" msg >jmsg &&
+ test_cmp jmsg pmsg &&
+ # make sure p4 knows about the two jobs
+ p4 -G describe $change >change &&
+ (
+ marshal_dump job0 <change &&
+ marshal_dump job1 <change
+ ) | sort >jobs &&
+ cat jobname1 jobname2 | sort >expected &&
+ test_cmp expected jobs
+ )
+'
+
+test_expect_success 'description with Jobs section and bogus following text' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ echo desc6 >desc6 &&
+ git add desc6 &&
+ echo 6060843 >jobname &&
+ (
+ printf "subject line\n\n\tExtra tab\nline.\n\n" &&
+ printf "Files:\n\tBogus files marker\n" &&
+ printf "Junk: 3164175\n" &&
+ printf "Jobs: $(cat jobname)\n" &&
+ printf "MoreJunk: 3711\n"
+ ) >msg &&
+ git commit -F - <msg &&
+ git config git-p4.skipSubmitEdit true &&
+ # build a job
+ make_job $(cat jobname) &&
+ test_must_fail git p4 submit 2>err &&
+ test_grep "Unknown field name" err
+ ) &&
+ (
+ cd "$cli" &&
+ p4 revert desc6 &&
+ rm -f desc6
+ )
+'
+
+test_expect_success 'submit --prepare-p4-only' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ echo prep-only-add >prep-only-add &&
+ git add prep-only-add &&
+ git commit -m "prep only add" &&
+ git p4 submit --prepare-p4-only >out &&
+ test_grep "prepared for submission" out &&
+ test_grep "must be deleted" out &&
+ test_grep ! "everything below this line is just the diff" out
+ ) &&
+ (
+ cd "$cli" &&
+ test_path_is_file prep-only-add &&
+ p4 fstat -T action prep-only-add | grep -w add
+ )
+'
+
+test_expect_success 'submit --shelve' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$cli" &&
+ p4 revert ... &&
+ cd "$git" &&
+ git config git-p4.skipSubmitEdit true &&
+ test_commit "shelveme1" &&
+ git p4 submit --origin=HEAD^ &&
+
+ echo 654321 >shelveme2.t &&
+ echo 123456 >>shelveme1.t &&
+ git add shelveme* &&
+ git commit -m"shelvetest" &&
+ git p4 submit --shelve --origin=HEAD^ &&
+
+ test_path_is_file shelveme1.t &&
+ test_path_is_file shelveme2.t
+ ) &&
+ (
+ cd "$cli" &&
+ change=$(p4 -G changes -s shelved -m 1 //depot/... | \
+ marshal_dump change) &&
+ p4 describe -S $change | grep shelveme2 &&
+ p4 describe -S $change | grep 123456 &&
+ test_path_is_file shelveme1.t &&
+ test_path_is_missing shelveme2.t
+ )
+'
+
+last_shelve () {
+ p4 -G changes -s shelved -m 1 //depot/... | marshal_dump change
+}
+
+make_shelved_cl() {
+ test_commit "$1" >/dev/null &&
+ git p4 submit --origin HEAD^ --shelve >/dev/null &&
+ p4 -G changes -s shelved -m 1 | marshal_dump change
+}
+
+# Update existing shelved changelists
+
+test_expect_success 'submit --update-shelve' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$cli" &&
+ p4 revert ... &&
+ cd "$git" &&
+ git config git-p4.skipSubmitEdit true &&
+ shelved_cl0=$(make_shelved_cl "shelved-change-0") &&
+ echo shelved_cl0=$shelved_cl0 &&
+ shelved_cl1=$(make_shelved_cl "shelved-change-1") &&
+
+ echo "updating shelved change lists $shelved_cl0 and $shelved_cl1" &&
+
+ echo "updated-line" >>shelf.t &&
+ echo added-file.t >added-file.t &&
+ git add shelf.t added-file.t &&
+ git rm -f shelved-change-1.t &&
+ git commit --amend -C HEAD &&
+ git show --stat HEAD &&
+ git p4 submit -v --origin HEAD~2 --update-shelve $shelved_cl0 --update-shelve $shelved_cl1 &&
+ echo "done git p4 submit"
+ ) &&
+ (
+ cd "$cli" &&
+ change=$(last_shelve) &&
+ p4 unshelve -c $change -s $change &&
+ grep -q updated-line shelf.t &&
+ p4 describe -S $change | grep added-file.t &&
+ test_path_is_missing shelved-change-1.t &&
+ p4 revert ...
+ )
+'
+
+test_expect_success 'update a shelve involving moved and copied files' '
+ test_when_finished cleanup_git &&
+ (
+ cd "$cli" &&
+ : >file_to_move &&
+ p4 add file_to_move &&
+ p4 submit -d "change1" &&
+ p4 edit file_to_move &&
+ echo change >>file_to_move &&
+ p4 submit -d "change2" &&
+ p4 opened
+ ) &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ git config git-p4.detectCopies true &&
+ git config git-p4.detectRenames true &&
+ git config git-p4.skipSubmitEdit true &&
+ mkdir moved &&
+ cp file_to_move copy_of_file &&
+ git add copy_of_file &&
+ git mv file_to_move moved/ &&
+ git commit -m "rename a file" &&
+ git p4 submit -M --shelve --origin HEAD^ &&
+ : >new_file &&
+ git add new_file &&
+ git commit --amend &&
+ git show --stat HEAD &&
+ change=$(last_shelve) &&
+ git p4 submit -M --update-shelve $change --commit HEAD
+ ) &&
+ (
+ cd "$cli" &&
+ change=$(last_shelve) &&
+ echo change=$change &&
+ p4 unshelve -s $change &&
+ p4 submit -d "Testing update-shelve" &&
+ test_path_is_file copy_of_file &&
+ test_path_is_file moved/file_to_move &&
+ test_path_is_missing file_to_move &&
+ test_path_is_file new_file &&
+ echo "unshelved and submitted change $change" &&
+ p4 changes moved/file_to_move | grep "Testing update-shelve" &&
+ p4 changes copy_of_file | grep "Testing update-shelve"
+ )
+'
+
+test_done
diff --git a/t/t9808-git-p4-chdir.sh b/t/t9808-git-p4-chdir.sh
new file mode 100755
index 0000000..58a9b3b
--- /dev/null
+++ b/t/t9808-git-p4-chdir.sh
@@ -0,0 +1,86 @@
+#!/bin/sh
+
+test_description='git p4 relative chdir'
+
+. ./lib-git-p4.sh
+
+test_expect_success 'start p4d' '
+ start_p4d
+'
+
+test_expect_success 'init depot' '
+ (
+ cd "$cli" &&
+ echo file1 >file1 &&
+ p4 add file1 &&
+ p4 submit -d "change 1"
+ )
+'
+
+# P4 reads from P4CONFIG file to find its server params, if the
+# environment variable is set
+test_expect_success 'P4CONFIG and absolute dir clone' '
+ printf "P4PORT=$P4PORT\nP4CLIENT=$P4CLIENT\n" >p4config &&
+ test_when_finished "rm p4config" &&
+ test_when_finished cleanup_git &&
+ (
+ P4CONFIG=p4config && export P4CONFIG &&
+ sane_unset P4PORT P4CLIENT &&
+ git p4 clone --verbose --dest="$git" //depot
+ )
+'
+
+# same thing, but with relative directory name, note missing $ on --dest
+test_expect_success 'P4CONFIG and relative dir clone' '
+ printf "P4PORT=$P4PORT\nP4CLIENT=$P4CLIENT\n" >p4config &&
+ test_when_finished "rm p4config" &&
+ test_when_finished cleanup_git &&
+ (
+ P4CONFIG=p4config && export P4CONFIG &&
+ sane_unset P4PORT P4CLIENT &&
+ git p4 clone --verbose --dest="git" //depot
+ )
+'
+
+# Common setup using .p4config to set P4CLIENT and P4PORT breaks
+# if clone destination is relative. Make sure that chdir() expands
+# the relative path in --dest to absolute.
+test_expect_success 'p4 client root would be relative due to clone --dest' '
+ test_when_finished cleanup_git &&
+ (
+ echo P4PORT=$P4PORT >git/.p4config &&
+ P4CONFIG=.p4config &&
+ export P4CONFIG &&
+ unset P4PORT &&
+ git p4 clone --dest="git" //depot
+ )
+'
+
+# When the p4 client Root is a symlink, make sure chdir() does not use
+# getcwd() to convert it to a physical path.
+test_expect_success SYMLINKS 'p4 client root symlink should stay symbolic' '
+ physical="$TRASH_DIRECTORY/physical" &&
+ symbolic="$TRASH_DIRECTORY/symbolic" &&
+ test_when_finished "rm -rf \"$physical\"" &&
+ test_when_finished "rm \"$symbolic\"" &&
+ mkdir -p "$physical" &&
+ ln -s "$physical" "$symbolic" &&
+ test_when_finished cleanup_git &&
+ (
+ P4CLIENT=client-sym &&
+ p4 client -i <<-EOF &&
+ Client: $P4CLIENT
+ Description: $P4CLIENT
+ Root: $symbolic
+ LineEnd: unix
+ View: //depot/... //$P4CLIENT/...
+ EOF
+ git p4 clone --dest="$git" //depot &&
+ cd "$git" &&
+ test_commit file2 &&
+ git config git-p4.skipSubmitEdit true &&
+ git p4 submit
+ )
+'
+
+test_done
diff --git a/t/t9809-git-p4-client-view.sh b/t/t9809-git-p4-client-view.sh
new file mode 100755
index 0000000..9c9710d
--- /dev/null
+++ b/t/t9809-git-p4-client-view.sh
@@ -0,0 +1,839 @@
+#!/bin/sh
+
+test_description='git p4 client view'
+
+. ./lib-git-p4.sh
+
+test_expect_success 'start p4d' '
+ start_p4d
+'
+
+#
+# Verify these files exist, exactly. Caller creates
+# a list of files in file "files".
+#
+check_files_exist() {
+ ok=0 &&
+ num=$# &&
+ for arg ; do
+ test_path_is_file "$arg" &&
+ ok=$(($ok + 1))
+ done &&
+ test $ok -eq $num &&
+ test_line_count = $num files
+}
+
+#
+# Sync up the p4 client, make sure the given files (and only
+# those) exist.
+#
+client_verify() {
+ (
+ cd "$cli" &&
+ p4 sync &&
+ find . -type f ! -name files >files &&
+ check_files_exist "$@"
+ )
+}
+
+#
+# Make sure the named files, exactly, exist.
+#
+git_verify() {
+ (
+ cd "$git" &&
+ git ls-files >files &&
+ check_files_exist "$@"
+ )
+}
+
+# //depot
+# - dir1
+# - file11
+# - file12
+# - dir2
+# - file21
+# - file22
+init_depot() {
+ for d in 1 2 ; do
+ mkdir -p dir$d &&
+ for f in 1 2 ; do
+ echo dir$d/file$d$f >dir$d/file$d$f &&
+ p4 add dir$d/file$d$f &&
+ p4 submit -d "dir$d/file$d$f"
+ done
+ done &&
+ find . -type f ! -name files >files &&
+ check_files_exist dir1/file11 dir1/file12 \
+ dir2/file21 dir2/file22
+}
+
+test_expect_success 'init depot' '
+ (
+ cd "$cli" &&
+ init_depot
+ )
+'
+
+# double % for printf
+test_expect_success 'view wildcard %%n' '
+ client_view "//depot/%%%%1/sub/... //client/sub/%%%%1/..." &&
+ test_when_finished cleanup_git &&
+ git p4 clone --use-client-spec --dest="$git" //depot
+'
+
+test_expect_success 'view wildcard *' '
+ client_view "//depot/*/bar/... //client/*/bar/..." &&
+ test_when_finished cleanup_git &&
+ git p4 clone --use-client-spec --dest="$git" //depot
+'
+
+test_expect_success 'wildcard ... in the middle' '
+ client_view "//depot/.../file11 //client/.../file11" &&
+ test_when_finished cleanup_git &&
+ git p4 clone --use-client-spec --dest="$git" //depot
+'
+
+test_expect_success 'wildcard ... in the middle and at the end' '
+ client_view "//depot/.../a/... //client/.../a/..." &&
+ test_when_finished cleanup_git &&
+ git p4 clone --use-client-spec --dest="$git" //depot
+'
+
+test_expect_success 'basic map' '
+ client_view "//depot/dir1/... //client/cli1/..." &&
+ files="cli1/file11 cli1/file12" &&
+ client_verify $files &&
+ test_when_finished cleanup_git &&
+ git p4 clone --use-client-spec --dest="$git" //depot &&
+ git_verify $files
+'
+
+test_expect_success 'client view with no mappings' '
+ client_view &&
+ client_verify &&
+ test_when_finished cleanup_git &&
+ git p4 clone --use-client-spec --dest="$git" //depot &&
+ git_verify
+'
+
+test_expect_success 'single file map' '
+ client_view "//depot/dir1/file11 //client/file11" &&
+ files="file11" &&
+ client_verify $files &&
+ test_when_finished cleanup_git &&
+ git p4 clone --use-client-spec --dest="$git" //depot &&
+ git_verify $files
+'
+
+test_expect_success 'later mapping takes precedence (entire repo)' '
+ client_view "//depot/dir1/... //client/cli1/..." \
+ "//depot/... //client/cli2/..." &&
+ files="cli2/dir1/file11 cli2/dir1/file12
+ cli2/dir2/file21 cli2/dir2/file22" &&
+ client_verify $files &&
+ test_when_finished cleanup_git &&
+ git p4 clone --use-client-spec --dest="$git" //depot &&
+ git_verify $files
+'
+
+test_expect_success 'later mapping takes precedence (partial repo)' '
+ client_view "//depot/dir1/... //client/..." \
+ "//depot/dir2/... //client/..." &&
+ files="file21 file22" &&
+ client_verify $files &&
+ test_when_finished cleanup_git &&
+ git p4 clone --use-client-spec --dest="$git" //depot &&
+ git_verify $files
+'
+
+# Reading the view backwards,
+# dir2 goes to cli12
+# dir1 cannot go to cli12 since it was filled by dir2
+# dir1 also does not go to cli3, since the second rule
+# noticed that it matched, but was already filled
+test_expect_success 'depot path matching rejected client path' '
+ client_view "//depot/dir1/... //client/cli3/..." \
+ "//depot/dir1/... //client/cli12/..." \
+ "//depot/dir2/... //client/cli12/..." &&
+ files="cli12/file21 cli12/file22" &&
+ client_verify $files &&
+ test_when_finished cleanup_git &&
+ git p4 clone --use-client-spec --dest="$git" //depot &&
+ git_verify $files
+'
+
+# since both have the same //client/..., the exclusion
+# rule keeps everything out
+test_expect_success 'exclusion wildcard, client rhs same (odd)' '
+ client_view "//depot/... //client/..." \
+ "-//depot/dir2/... //client/..." &&
+ client_verify &&
+ test_when_finished cleanup_git &&
+ git p4 clone --use-client-spec --dest="$git" //depot &&
+ git_verify
+'
+
+test_expect_success 'exclusion wildcard, client rhs different (normal)' '
+ client_view "//depot/... //client/..." \
+ "-//depot/dir2/... //client/dir2/..." &&
+ files="dir1/file11 dir1/file12" &&
+ client_verify $files &&
+ test_when_finished cleanup_git &&
+ git p4 clone --use-client-spec --dest="$git" //depot &&
+ git_verify $files
+'
+
+test_expect_success 'exclusion single file' '
+ client_view "//depot/... //client/..." \
+ "-//depot/dir2/file22 //client/file22" &&
+ files="dir1/file11 dir1/file12 dir2/file21" &&
+ client_verify $files &&
+ test_when_finished cleanup_git &&
+ git p4 clone --use-client-spec --dest="$git" //depot &&
+ git_verify $files
+'
+
+test_expect_success 'overlay wildcard' '
+ client_view "//depot/dir1/... //client/cli/..." \
+ "+//depot/dir2/... //client/cli/..." &&
+ files="cli/file11 cli/file12 cli/file21 cli/file22" &&
+ client_verify $files &&
+ test_when_finished cleanup_git &&
+ git p4 clone --use-client-spec --dest="$git" //depot &&
+ git_verify $files
+'
+
+test_expect_success 'overlay single file' '
+ client_view "//depot/dir1/... //client/cli/..." \
+ "+//depot/dir2/file21 //client/cli/file21" &&
+ files="cli/file11 cli/file12 cli/file21" &&
+ client_verify $files &&
+ test_when_finished cleanup_git &&
+ git p4 clone --use-client-spec --dest="$git" //depot &&
+ git_verify $files
+'
+
+test_expect_success 'exclusion with later inclusion' '
+ client_view "//depot/... //client/..." \
+ "-//depot/dir2/... //client/dir2/..." \
+ "//depot/dir2/... //client/dir2incl/..." &&
+ files="dir1/file11 dir1/file12 dir2incl/file21 dir2incl/file22" &&
+ client_verify $files &&
+ test_when_finished cleanup_git &&
+ git p4 clone --use-client-spec --dest="$git" //depot &&
+ git_verify $files
+'
+
+test_expect_success 'quotes on rhs only' '
+ client_view "//depot/dir1/... \"//client/cdir 1/...\"" &&
+ client_verify "cdir 1/file11" "cdir 1/file12" &&
+ test_when_finished cleanup_git &&
+ git p4 clone --use-client-spec --dest="$git" //depot &&
+ git_verify "cdir 1/file11" "cdir 1/file12"
+'
+
+#
+# Submit tests
+#
+
+# clone sets variable
+test_expect_success 'clone --use-client-spec sets useClientSpec' '
+ client_view "//depot/... //client/..." &&
+ test_when_finished cleanup_git &&
+ git p4 clone --use-client-spec --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ git config --bool git-p4.useClientSpec >actual &&
+ echo true >true &&
+ test_cmp actual true
+ )
+'
+
+# clone just a subdir of the client spec
+test_expect_success 'subdir clone' '
+ client_view "//depot/... //client/..." &&
+ files="dir1/file11 dir1/file12 dir2/file21 dir2/file22" &&
+ client_verify $files &&
+ test_when_finished cleanup_git &&
+ git p4 clone --use-client-spec --dest="$git" //depot/dir1 &&
+ git_verify dir1/file11 dir1/file12
+'
+
+#
+# submit back, see what happens: five cases
+#
+test_expect_success 'subdir clone, submit modify' '
+ client_view "//depot/... //client/..." &&
+ test_when_finished cleanup_git &&
+ git p4 clone --use-client-spec --dest="$git" //depot/dir1 &&
+ (
+ cd "$git" &&
+ git config git-p4.skipSubmitEdit true &&
+ echo line >>dir1/file12 &&
+ git add dir1/file12 &&
+ git commit -m dir1/file12 &&
+ git p4 submit
+ ) &&
+ (
+ cd "$cli" &&
+ test_path_is_file dir1/file12 &&
+ test_line_count = 2 dir1/file12
+ )
+'
+
+test_expect_success 'subdir clone, submit add' '
+ client_view "//depot/... //client/..." &&
+ test_when_finished cleanup_git &&
+ git p4 clone --use-client-spec --dest="$git" //depot/dir1 &&
+ (
+ cd "$git" &&
+ git config git-p4.skipSubmitEdit true &&
+ echo file13 >dir1/file13 &&
+ git add dir1/file13 &&
+ git commit -m dir1/file13 &&
+ git p4 submit
+ ) &&
+ (
+ cd "$cli" &&
+ test_path_is_file dir1/file13
+ )
+'
+
+test_expect_success 'subdir clone, submit delete' '
+ client_view "//depot/... //client/..." &&
+ test_when_finished cleanup_git &&
+ git p4 clone --use-client-spec --dest="$git" //depot/dir1 &&
+ (
+ cd "$git" &&
+ git config git-p4.skipSubmitEdit true &&
+ git rm dir1/file12 &&
+ git commit -m "delete dir1/file12" &&
+ git p4 submit
+ ) &&
+ (
+ cd "$cli" &&
+ test_path_is_missing dir1/file12
+ )
+'
+
+test_expect_success 'subdir clone, submit copy' '
+ client_view "//depot/... //client/..." &&
+ test_when_finished cleanup_git &&
+ git p4 clone --use-client-spec --dest="$git" //depot/dir1 &&
+ (
+ cd "$git" &&
+ git config git-p4.skipSubmitEdit true &&
+ git config git-p4.detectCopies true &&
+ cp dir1/file11 dir1/file11a &&
+ git add dir1/file11a &&
+ git commit -m "copy to dir1/file11a" &&
+ git p4 submit
+ ) &&
+ (
+ cd "$cli" &&
+ test_path_is_file dir1/file11a &&
+ ! is_cli_file_writeable dir1/file11a
+ )
+'
+
+test_expect_success 'subdir clone, submit rename' '
+ client_view "//depot/... //client/..." &&
+ test_when_finished cleanup_git &&
+ git p4 clone --use-client-spec --dest="$git" //depot/dir1 &&
+ (
+ cd "$git" &&
+ git config git-p4.skipSubmitEdit true &&
+ git config git-p4.detectRenames true &&
+ git mv dir1/file13 dir1/file13a &&
+ git commit -m "rename dir1/file13 to dir1/file13a" &&
+ git p4 submit
+ ) &&
+ (
+ cd "$cli" &&
+ test_path_is_missing dir1/file13 &&
+ test_path_is_file dir1/file13a &&
+ ! is_cli_file_writeable dir1/file13a
+ )
+'
+
+# see t9800 for the non-client-spec case, and the rest of the wildcard tests
+test_expect_success 'wildcard files submit back to p4, client-spec case' '
+ client_view "//depot/... //client/..." &&
+ test_when_finished cleanup_git &&
+ git p4 clone --use-client-spec --dest="$git" //depot/dir1 &&
+ (
+ cd "$git" &&
+ echo git-wild-hash >dir1/git-wild#hash &&
+ if test_have_prereq !MINGW,!CYGWIN
+ then
+ echo git-wild-star >dir1/git-wild\*star
+ fi &&
+ echo git-wild-at >dir1/git-wild@at &&
+ echo git-wild-percent >dir1/git-wild%percent &&
+ git add dir1/git-wild* &&
+ git commit -m "add some wildcard filenames" &&
+ git config git-p4.skipSubmitEditCheck true &&
+ git p4 submit
+ ) &&
+ (
+ cd "$cli" &&
+ test_path_is_file dir1/git-wild#hash &&
+ if test_have_prereq !MINGW,!CYGWIN
+ then
+ test_path_is_file dir1/git-wild\*star
+ fi &&
+ test_path_is_file dir1/git-wild@at &&
+ test_path_is_file dir1/git-wild%percent
+ ) &&
+ (
+ # delete these carefully, cannot just do "p4 delete"
+ # on files with wildcards; but git-p4 knows how
+ cd "$git" &&
+ git rm dir1/git-wild* &&
+ git commit -m "clean up the wildcards" &&
+ git p4 submit
+ )
+'
+
+test_expect_success 'reinit depot' '
+ (
+ cd "$cli" &&
+ rm files &&
+ p4 delete */* &&
+ p4 submit -d "delete all files" &&
+ init_depot
+ )
+'
+
+#
+# What happens when two files of the same name are overlaid together?
+# The last-listed file should take preference.
+#
+# //depot
+# - dir1
+# - file11
+# - file12
+# - filecollide
+# - dir2
+# - file21
+# - file22
+# - filecollide
+#
+test_expect_success 'overlay collision setup' '
+ client_view "//depot/... //client/..." &&
+ (
+ cd "$cli" &&
+ p4 sync &&
+ echo dir1/filecollide >dir1/filecollide &&
+ p4 add dir1/filecollide &&
+ p4 submit -d dir1/filecollide &&
+ echo dir2/filecollide >dir2/filecollide &&
+ p4 add dir2/filecollide &&
+ p4 submit -d dir2/filecollide
+ )
+'
+
+test_expect_success 'overlay collision 1 to 2' '
+ client_view "//depot/dir1/... //client/..." \
+ "+//depot/dir2/... //client/..." &&
+ files="file11 file12 file21 file22 filecollide" &&
+ echo dir2/filecollide >actual &&
+ client_verify $files &&
+ test_cmp actual "$cli"/filecollide &&
+ test_when_finished cleanup_git &&
+ git p4 clone --use-client-spec --dest="$git" //depot &&
+ git_verify $files &&
+ test_cmp actual "$git"/filecollide
+'
+
+test_expect_failure 'overlay collision 2 to 1' '
+ client_view "//depot/dir2/... //client/..." \
+ "+//depot/dir1/... //client/..." &&
+ files="file11 file12 file21 file22 filecollide" &&
+ echo dir1/filecollide >actual &&
+ client_verify $files &&
+ test_cmp actual "$cli"/filecollide &&
+ test_when_finished cleanup_git &&
+ git p4 clone --use-client-spec --dest="$git" //depot &&
+ git_verify $files &&
+ test_cmp actual "$git"/filecollide
+'
+
+test_expect_success 'overlay collision delete 2' '
+ client_view "//depot/... //client/..." &&
+ (
+ cd "$cli" &&
+ p4 sync &&
+ p4 delete dir2/filecollide &&
+ p4 submit -d "remove dir2/filecollide"
+ )
+'
+
+# no filecollide, got deleted with dir2
+test_expect_failure 'overlay collision 1 to 2, but 2 deleted' '
+ client_view "//depot/dir1/... //client/..." \
+ "+//depot/dir2/... //client/..." &&
+ files="file11 file12 file21 file22" &&
+ client_verify $files &&
+ test_when_finished cleanup_git &&
+ git p4 clone --use-client-spec --dest="$git" //depot &&
+ git_verify $files
+'
+
+test_expect_success 'overlay collision update 1' '
+ client_view "//depot/dir1/... //client/dir1/..." &&
+ (
+ cd "$cli" &&
+ p4 sync &&
+ p4 open dir1/filecollide &&
+ echo dir1/filecollide update >dir1/filecollide &&
+ p4 submit -d "update dir1/filecollide"
+ )
+'
+
+# still no filecollide, dir2 still wins with the deletion even though the
+# change to dir1 is more recent
+test_expect_failure 'overlay collision 1 to 2, but 2 deleted, then 1 updated' '
+ client_view "//depot/dir1/... //client/..." \
+ "+//depot/dir2/... //client/..." &&
+ files="file11 file12 file21 file22" &&
+ client_verify $files &&
+ test_when_finished cleanup_git &&
+ git p4 clone --use-client-spec --dest="$git" //depot &&
+ git_verify $files
+'
+
+test_expect_success 'overlay collision delete filecollides' '
+ client_view "//depot/... //client/..." &&
+ (
+ cd "$cli" &&
+ p4 sync &&
+ p4 delete dir1/filecollide dir2/filecollide &&
+ p4 submit -d "remove filecollides"
+ )
+'
+
+#
+# Overlays as part of sync, rather than initial checkout:
+# 1. add a file in dir1
+# 2. sync to include it
+# 3. add same file in dir2
+# 4. sync, make sure content switches as dir2 has priority
+# 5. add another file in dir1
+# 6. sync
+# 7. add/delete same file in dir2
+# 8. sync, make sure it disappears, again dir2 wins
+# 9. cleanup
+#
+# //depot
+# - dir1
+# - file11
+# - file12
+# - colA
+# - colB
+# - dir2
+# - file21
+# - file22
+# - colA
+# - colB
+#
+test_expect_success 'overlay sync: add colA in dir1' '
+ client_view "//depot/dir1/... //client/dir1/..." &&
+ (
+ cd "$cli" &&
+ p4 sync &&
+ echo dir1/colA >dir1/colA &&
+ p4 add dir1/colA &&
+ p4 submit -d dir1/colA
+ )
+'
+
+test_expect_success 'overlay sync: initial git checkout' '
+ client_view "//depot/dir1/... //client/..." \
+ "+//depot/dir2/... //client/..." &&
+ files="file11 file12 file21 file22 colA" &&
+ echo dir1/colA >actual &&
+ client_verify $files &&
+ test_cmp actual "$cli"/colA &&
+ git p4 clone --use-client-spec --dest="$git" //depot &&
+ git_verify $files &&
+ test_cmp actual "$git"/colA
+'
+
+test_expect_success 'overlay sync: add colA in dir2' '
+ client_view "//depot/dir2/... //client/dir2/..." &&
+ (
+ cd "$cli" &&
+ p4 sync &&
+ echo dir2/colA >dir2/colA &&
+ p4 add dir2/colA &&
+ p4 submit -d dir2/colA
+ )
+'
+
+test_expect_success 'overlay sync: colA content switch' '
+ client_view "//depot/dir1/... //client/..." \
+ "+//depot/dir2/... //client/..." &&
+ files="file11 file12 file21 file22 colA" &&
+ echo dir2/colA >actual &&
+ client_verify $files &&
+ test_cmp actual "$cli"/colA &&
+ (
+ cd "$git" &&
+ git p4 sync --use-client-spec &&
+ git merge --ff-only p4/master
+ ) &&
+ git_verify $files &&
+ test_cmp actual "$git"/colA
+'
+
+test_expect_success 'overlay sync: add colB in dir1' '
+ client_view "//depot/dir1/... //client/dir1/..." &&
+ (
+ cd "$cli" &&
+ p4 sync &&
+ echo dir1/colB >dir1/colB &&
+ p4 add dir1/colB &&
+ p4 submit -d dir1/colB
+ )
+'
+
+test_expect_success 'overlay sync: colB appears' '
+ client_view "//depot/dir1/... //client/..." \
+ "+//depot/dir2/... //client/..." &&
+ files="file11 file12 file21 file22 colA colB" &&
+ echo dir1/colB >actual &&
+ client_verify $files &&
+ test_cmp actual "$cli"/colB &&
+ (
+ cd "$git" &&
+ git p4 sync --use-client-spec &&
+ git merge --ff-only p4/master
+ ) &&
+ git_verify $files &&
+ test_cmp actual "$git"/colB
+'
+
+test_expect_success 'overlay sync: add/delete colB in dir2' '
+ client_view "//depot/dir2/... //client/dir2/..." &&
+ (
+ cd "$cli" &&
+ p4 sync &&
+ echo dir2/colB >dir2/colB &&
+ p4 add dir2/colB &&
+ p4 submit -d dir2/colB &&
+ p4 delete dir2/colB &&
+ p4 submit -d "delete dir2/colB"
+ )
+'
+
+test_expect_success 'overlay sync: colB disappears' '
+ client_view "//depot/dir1/... //client/..." \
+ "+//depot/dir2/... //client/..." &&
+ files="file11 file12 file21 file22 colA" &&
+ client_verify $files &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git p4 sync --use-client-spec &&
+ git merge --ff-only p4/master
+ ) &&
+ git_verify $files
+'
+
+test_expect_success 'overlay sync: cleanup' '
+ client_view "//depot/... //client/..." &&
+ (
+ cd "$cli" &&
+ p4 sync &&
+ p4 delete dir1/colA dir2/colA dir1/colB &&
+ p4 submit -d "remove overlay sync files"
+ )
+'
+
+#
+# Overlay tests again, but swapped so dir1 has priority.
+# 1. add a file in dir1
+# 2. sync to include it
+# 3. add same file in dir2
+# 4. sync, make sure content does not switch
+# 5. add another file in dir1
+# 6. sync
+# 7. add/delete same file in dir2
+# 8. sync, make sure it is still there
+# 9. cleanup
+#
+# //depot
+# - dir1
+# - file11
+# - file12
+# - colA
+# - colB
+# - dir2
+# - file21
+# - file22
+# - colA
+# - colB
+#
+test_expect_success 'overlay sync swap: add colA in dir1' '
+ client_view "//depot/dir1/... //client/dir1/..." &&
+ (
+ cd "$cli" &&
+ p4 sync &&
+ echo dir1/colA >dir1/colA &&
+ p4 add dir1/colA &&
+ p4 submit -d dir1/colA
+ )
+'
+
+test_expect_success 'overlay sync swap: initial git checkout' '
+ client_view "//depot/dir2/... //client/..." \
+ "+//depot/dir1/... //client/..." &&
+ files="file11 file12 file21 file22 colA" &&
+ echo dir1/colA >actual &&
+ client_verify $files &&
+ test_cmp actual "$cli"/colA &&
+ git p4 clone --use-client-spec --dest="$git" //depot &&
+ git_verify $files &&
+ test_cmp actual "$git"/colA
+'
+
+test_expect_success 'overlay sync swap: add colA in dir2' '
+ client_view "//depot/dir2/... //client/dir2/..." &&
+ (
+ cd "$cli" &&
+ p4 sync &&
+ echo dir2/colA >dir2/colA &&
+ p4 add dir2/colA &&
+ p4 submit -d dir2/colA
+ )
+'
+
+test_expect_failure 'overlay sync swap: colA no content switch' '
+ client_view "//depot/dir2/... //client/..." \
+ "+//depot/dir1/... //client/..." &&
+ files="file11 file12 file21 file22 colA" &&
+ echo dir1/colA >actual &&
+ client_verify $files &&
+ test_cmp actual "$cli"/colA &&
+ (
+ cd "$git" &&
+ git p4 sync --use-client-spec &&
+ git merge --ff-only p4/master
+ ) &&
+ git_verify $files &&
+ test_cmp actual "$git"/colA
+'
+
+test_expect_success 'overlay sync swap: add colB in dir1' '
+ client_view "//depot/dir1/... //client/dir1/..." &&
+ (
+ cd "$cli" &&
+ p4 sync &&
+ echo dir1/colB >dir1/colB &&
+ p4 add dir1/colB &&
+ p4 submit -d dir1/colB
+ )
+'
+
+test_expect_success 'overlay sync swap: colB appears' '
+ client_view "//depot/dir2/... //client/..." \
+ "+//depot/dir1/... //client/..." &&
+ files="file11 file12 file21 file22 colA colB" &&
+ echo dir1/colB >actual &&
+ client_verify $files &&
+ test_cmp actual "$cli"/colB &&
+ (
+ cd "$git" &&
+ git p4 sync --use-client-spec &&
+ git merge --ff-only p4/master
+ ) &&
+ git_verify $files &&
+ test_cmp actual "$git"/colB
+'
+
+test_expect_success 'overlay sync swap: add/delete colB in dir2' '
+ client_view "//depot/dir2/... //client/dir2/..." &&
+ (
+ cd "$cli" &&
+ p4 sync &&
+ echo dir2/colB >dir2/colB &&
+ p4 add dir2/colB &&
+ p4 submit -d dir2/colB &&
+ p4 delete dir2/colB &&
+ p4 submit -d "delete dir2/colB"
+ )
+'
+
+test_expect_failure 'overlay sync swap: colB no change' '
+ client_view "//depot/dir2/... //client/..." \
+ "+//depot/dir1/... //client/..." &&
+ files="file11 file12 file21 file22 colA colB" &&
+ echo dir1/colB >actual &&
+ client_verify $files &&
+ test_cmp actual "$cli"/colB &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git p4 sync --use-client-spec &&
+ git merge --ff-only p4/master
+ ) &&
+ git_verify $files &&
+ test_cmp actual "$cli"/colB
+'
+
+test_expect_success 'overlay sync swap: cleanup' '
+ client_view "//depot/... //client/..." &&
+ (
+ cd "$cli" &&
+ p4 sync &&
+ p4 delete dir1/colA dir2/colA dir1/colB &&
+ p4 submit -d "remove overlay sync files"
+ )
+'
+
+#
+# Rename directories to test quoting in depot-side mappings
+# //depot
+# - "dir 1"
+# - file11
+# - file12
+# - "dir 2"
+# - file21
+# - file22
+#
+test_expect_success 'rename files to introduce spaces' '
+ client_view "//depot/... //client/..." &&
+ client_verify dir1/file11 dir1/file12 \
+ dir2/file21 dir2/file22 &&
+ (
+ cd "$cli" &&
+ p4 open dir1/... &&
+ p4 move dir1/... "dir 1"/... &&
+ p4 open dir2/... &&
+ p4 move dir2/... "dir 2"/... &&
+ p4 submit -d "rename with spaces"
+ ) &&
+ client_verify "dir 1/file11" "dir 1/file12" \
+ "dir 2/file21" "dir 2/file22"
+'
+
+test_expect_success 'quotes on lhs only' '
+ client_view "\"//depot/dir 1/...\" //client/cdir1/..." &&
+ files="cdir1/file11 cdir1/file12" &&
+ client_verify $files &&
+ test_when_finished cleanup_git &&
+ git p4 clone --use-client-spec --dest="$git" //depot &&
+ client_verify $files
+'
+
+test_expect_success 'quotes on both sides' '
+ client_view "\"//depot/dir 1/...\" \"//client/cdir 1/...\"" &&
+ client_verify "cdir 1/file11" "cdir 1/file12" &&
+ test_when_finished cleanup_git &&
+ git p4 clone --use-client-spec --dest="$git" //depot &&
+ git_verify "cdir 1/file11" "cdir 1/file12"
+'
+
+test_done
diff --git a/t/t9810-git-p4-rcs.sh b/t/t9810-git-p4-rcs.sh
new file mode 100755
index 0000000..5fe8331
--- /dev/null
+++ b/t/t9810-git-p4-rcs.sh
@@ -0,0 +1,377 @@
+#!/bin/sh
+
+test_description='git p4 rcs keywords'
+
+. ./lib-git-p4.sh
+
+CP1252="\223\224"
+
+test_expect_success 'start p4d' '
+ start_p4d
+'
+
+#
+# Make one file with keyword lines at the top, and
+# enough plain text to be able to test modifications
+# far away from the keywords.
+#
+test_expect_success 'init depot' '
+ (
+ cd "$cli" &&
+ cat <<-\EOF >filek &&
+ $Id$
+ /* $Revision$ */
+ # $Change$
+ line4
+ line5
+ line6
+ line7
+ line8
+ EOF
+ sed "s/Revision/Revision: do not scrub me/" <filek >fileko &&
+ sed "s/Id/Id: do not scrub me/" <fileko >file_text &&
+ p4 add -t text+k filek &&
+ p4 submit -d "filek" &&
+ p4 add -t text+ko fileko &&
+ p4 submit -d "fileko" &&
+ printf "$CP1252" >fileko_cp1252 &&
+ p4 add -t text+ko fileko_cp1252 &&
+ p4 submit -d "fileko_cp1252" &&
+ p4 add -t text file_text &&
+ p4 submit -d "file_text"
+ )
+'
+
+#
+# Generate these in a function to make it easy to use single quote marks.
+#
+write_scrub_scripts () {
+ cat >"$TRASH_DIRECTORY/scrub_k.py" <<-\EOF &&
+ import re, sys
+ sys.stdout.write(re.sub(r'(?i)\$(Id|Header|Author|Date|DateTime|Change|File|Revision):[^$]*\$', r'$\1$', sys.stdin.read()))
+ EOF
+ cat >"$TRASH_DIRECTORY/scrub_ko.py" <<-\EOF
+ import re, sys
+ sys.stdout.write(re.sub(r'(?i)\$(Id|Header):[^$]*\$', r'$\1$', sys.stdin.read()))
+ EOF
+}
+
+test_expect_success 'scrub scripts' '
+ write_scrub_scripts
+'
+
+#
+# Compare $cli/file to its scrubbed version, should be different.
+# Compare scrubbed $cli/file to $git/file, should be same.
+#
+scrub_k_check () {
+ file="$1" &&
+ scrub="$TRASH_DIRECTORY/$file" &&
+ "$PYTHON_PATH" "$TRASH_DIRECTORY/scrub_k.py" <"$git/$file" >"$scrub" &&
+ ! test_cmp "$cli/$file" "$scrub" &&
+ test_cmp "$git/$file" "$scrub" &&
+ rm "$scrub"
+}
+scrub_ko_check () {
+ file="$1" &&
+ scrub="$TRASH_DIRECTORY/$file" &&
+ "$PYTHON_PATH" "$TRASH_DIRECTORY/scrub_ko.py" <"$git/$file" >"$scrub" &&
+ ! test_cmp "$cli/$file" "$scrub" &&
+ test_cmp "$git/$file" "$scrub" &&
+ rm "$scrub"
+}
+
+#
+# Modify far away from keywords. If no RCS lines show up
+# in the diff, there is no conflict.
+#
+test_expect_success 'edit far away from RCS lines' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ git config git-p4.skipSubmitEdit true &&
+ sed "s/^line7/line7 edit/" <filek >filek.tmp &&
+ mv -f filek.tmp filek &&
+ git commit -m "filek line7 edit" filek &&
+ git p4 submit &&
+ scrub_k_check filek
+ )
+'
+
+#
+# Modify near the keywords. This will require RCS scrubbing.
+#
+test_expect_success 'edit near RCS lines' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ git config git-p4.skipSubmitEdit true &&
+ git config git-p4.attemptRCSCleanup true &&
+ sed "s/^line4/line4 edit/" <filek >filek.tmp &&
+ mv -f filek.tmp filek &&
+ git commit -m "filek line4 edit" filek &&
+ git p4 submit &&
+ scrub_k_check filek
+ )
+'
+
+#
+# Modify the keywords themselves. This also will require RCS scrubbing.
+#
+test_expect_success 'edit keyword lines' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ git config git-p4.skipSubmitEdit true &&
+ git config git-p4.attemptRCSCleanup true &&
+ sed "/Revision/d" <filek >filek.tmp &&
+ mv -f filek.tmp filek &&
+ git commit -m "filek remove Revision line" filek &&
+ git p4 submit &&
+ scrub_k_check filek
+ )
+'
+
+#
+# Scrubbing text+ko files should not alter all keywords, just Id, Header.
+#
+test_expect_success 'scrub ko files differently' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ git config git-p4.skipSubmitEdit true &&
+ git config git-p4.attemptRCSCleanup true &&
+ sed "s/^line4/line4 edit/" <fileko >fileko.tmp &&
+ mv -f fileko.tmp fileko &&
+ git commit -m "fileko line4 edit" fileko &&
+ git p4 submit &&
+ scrub_ko_check fileko &&
+ ! scrub_k_check fileko
+ )
+'
+
+# hack; git p4 submit should do it on its own
+test_expect_success 'cleanup after failure' '
+ (
+ cd "$cli" &&
+ p4 revert ...
+ )
+'
+
+# perl $File:: bug check
+test_expect_success 'ktext expansion should not expand multi-line $File::' '
+ (
+ cd "$cli" &&
+ cat >lv.pm <<-\EOF &&
+ my $wanted = sub { my $f = $File::Find::name;
+ if ( -f && $f =~ /foo/ ) {
+ EOF
+ p4 add -t ktext lv.pm &&
+ p4 submit -d "lv.pm"
+ ) &&
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ test_cmp "$cli/lv.pm" lv.pm
+ )
+'
+
+#
+# Do not scrub anything but +k or +ko files. Sneak a change into
+# the cli file so that submit will get a conflict. Make sure that
+# scrubbing doesn't make a mess of things.
+#
+# This might happen only if the git repo is behind the p4 repo at
+# submit time, and there is a conflict.
+#
+test_expect_success 'do not scrub plain text' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ git config git-p4.skipSubmitEdit true &&
+ git config git-p4.attemptRCSCleanup true &&
+ sed "s/^line4/line4 edit/" <file_text >file_text.tmp &&
+ mv -f file_text.tmp file_text &&
+ git commit -m "file_text line4 edit" file_text &&
+ (
+ cd "$cli" &&
+ p4 open file_text &&
+ sed "s/^line5/line5 p4 edit/" <file_text >file_text.tmp &&
+ mv -f file_text.tmp file_text &&
+ p4 submit -d "file5 p4 edit"
+ ) &&
+ echo s | test_expect_code 1 git p4 submit &&
+ (
+ # make sure the file is not left open
+ cd "$cli" &&
+ ! p4 fstat -T action file_text
+ )
+ )
+'
+
+# hack; git p4 submit should do it on its own
+test_expect_success 'cleanup after failure 2' '
+ (
+ cd "$cli" &&
+ p4 revert ...
+ )
+'
+
+create_kw_file () {
+ cat <<\EOF >"$1"
+/* A file
+ Id: $Id$
+ Revision: $Revision$
+ File: $File$
+ */
+int main(int argc, const char **argv) {
+ return 0;
+}
+EOF
+}
+
+test_expect_success 'add kwfile' '
+ (
+ cd "$cli" &&
+ echo file1 >file1 &&
+ p4 add file1 &&
+ p4 submit -d "file 1" &&
+ create_kw_file kwfile1.c &&
+ p4 add kwfile1.c &&
+ p4 submit -d "Add rcw kw file" kwfile1.c
+ )
+'
+
+p4_append_to_file () {
+ f="$1" &&
+ p4 edit -t ktext "$f" &&
+ echo "/* $(date) */" >>"$f" &&
+ p4 submit -d "appending a line in p4"
+}
+
+# Create some files with RCS keywords. If they get modified
+# elsewhere then the version number gets bumped which then
+# results in a merge conflict if we touch the RCS kw lines,
+# even though the change itself would otherwise apply cleanly.
+test_expect_success 'cope with rcs keyword expansion damage' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ git config git-p4.skipSubmitEdit true &&
+ git config git-p4.attemptRCSCleanup true &&
+ (cd "$cli" && p4_append_to_file kwfile1.c) &&
+ old_lines=$(wc -l <kwfile1.c) &&
+ perl -n -i -e "print unless m/Revision:/" kwfile1.c &&
+ new_lines=$(wc -l <kwfile1.c) &&
+ test $new_lines = $(($old_lines - 1)) &&
+
+ git add kwfile1.c &&
+ git commit -m "Zap an RCS kw line" &&
+ git p4 submit &&
+ git p4 rebase &&
+ git diff p4/master &&
+ git p4 commit &&
+ echo "try modifying in both" &&
+ cd "$cli" &&
+ p4 edit kwfile1.c &&
+ echo "line from p4" >>kwfile1.c &&
+ p4 submit -d "add a line in p4" kwfile1.c &&
+ cd "$git" &&
+ echo "line from git at the top" | cat - kwfile1.c >kwfile1.c.new &&
+ mv kwfile1.c.new kwfile1.c &&
+ git commit -m "Add line in git at the top" kwfile1.c &&
+ git p4 rebase &&
+ git p4 submit
+ )
+'
+
+test_expect_success 'cope with rcs keyword file deletion' '
+ test_when_finished cleanup_git &&
+ (
+ cd "$cli" &&
+ echo "\$Revision\$" >kwdelfile.c &&
+ p4 add -t ktext kwdelfile.c &&
+ p4 submit -d "Add file to be deleted" &&
+ grep 1 kwdelfile.c
+ ) &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ grep Revision kwdelfile.c &&
+ git rm -f kwdelfile.c &&
+ git commit -m "Delete a file containing RCS keywords" &&
+ git config git-p4.skipSubmitEdit true &&
+ git config git-p4.attemptRCSCleanup true &&
+ git p4 submit
+ ) &&
+ (
+ cd "$cli" &&
+ p4 sync &&
+ ! test -f kwdelfile.c
+ )
+'
+
+# If you add keywords in git of the form $Header$ then everything should
+# work fine without any special handling.
+test_expect_success 'Add keywords in git which match the default p4 values' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ echo "NewKW: \$Revision\$" >>kwfile1.c &&
+ git add kwfile1.c &&
+ git commit -m "Adding RCS keywords in git" &&
+ git config git-p4.skipSubmitEdit true &&
+ git config git-p4.attemptRCSCleanup true &&
+ git p4 submit
+ ) &&
+ (
+ cd "$cli" &&
+ p4 sync &&
+ test -f kwfile1.c &&
+ grep "NewKW.*Revision.*[0-9]" kwfile1.c
+
+ )
+'
+
+# If you add keywords in git of the form $Header:#1$ then things will fail
+# unless git-p4 takes steps to scrub the *git* commit.
+#
+test_expect_failure 'Add keywords in git which do not match the default p4 values' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ echo "NewKW2: \$Revision:1\$" >>kwfile1.c &&
+ git add kwfile1.c &&
+ git commit -m "Adding RCS keywords in git" &&
+ git config git-p4.skipSubmitEdit true &&
+ git config git-p4.attemptRCSCleanup true &&
+ git p4 submit
+ ) &&
+ (
+ cd "$cli" &&
+ p4 sync &&
+ grep "NewKW2.*Revision.*[0-9]" kwfile1.c
+
+ )
+'
+
+test_expect_success 'check cp1252 smart quote are preserved through RCS keyword processing' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ printf "$CP1252" >expect &&
+ test_cmp_bin expect fileko_cp1252
+ )
+'
+
+test_done
diff --git a/t/t9811-git-p4-label-import.sh b/t/t9811-git-p4-label-import.sh
new file mode 100755
index 0000000..5ac5383
--- /dev/null
+++ b/t/t9811-git-p4-label-import.sh
@@ -0,0 +1,265 @@
+#!/bin/sh
+
+test_description='git p4 label tests'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./lib-git-p4.sh
+
+test_expect_success 'start p4d' '
+ start_p4d
+'
+
+# Basic p4 label import tests.
+#
+test_expect_success 'basic p4 labels' '
+ test_when_finished cleanup_git &&
+ (
+ cd "$cli" &&
+ mkdir -p main &&
+
+ echo f1 >main/f1 &&
+ p4 add main/f1 &&
+ p4 submit -d "main/f1" &&
+
+ echo f2 >main/f2 &&
+ p4 add main/f2 &&
+ p4 submit -d "main/f2" &&
+
+ echo f3 >main/file_with_\$metachar &&
+ p4 add main/file_with_\$metachar &&
+ p4 submit -d "file with metachar" &&
+
+ p4 tag -l TAG_F1_ONLY main/f1 &&
+ p4 tag -l TAG_WITH\$_SHELL_CHAR main/... &&
+ p4 tag -l this_tag_will_be\ skipped main/... &&
+
+ echo f4 >main/f4 &&
+ p4 add main/f4 &&
+ p4 submit -d "main/f4" &&
+
+ p4 label -i <<-EOF &&
+ Label: TAG_LONG_LABEL
+ Description:
+ A Label first line
+ A Label second line
+ View: //depot/...
+ EOF
+
+ p4 tag -l TAG_LONG_LABEL ... &&
+
+ p4 labels ... &&
+
+ git p4 clone --dest="$git" //depot@all &&
+ cd "$git" &&
+ git config git-p4.labelImportRegexp ".*TAG.*" &&
+ git p4 sync --import-labels --verbose &&
+
+ git tag &&
+ git tag >taglist &&
+ test_line_count = 3 taglist &&
+
+ cd main &&
+ git checkout TAG_F1_ONLY &&
+ ! test -f f2 &&
+ git checkout TAG_WITH\$_SHELL_CHAR &&
+ test -f f1 && test -f f2 && test -f file_with_\$metachar &&
+
+ git show TAG_LONG_LABEL | grep -q "A Label second line"
+ )
+'
+# Test some label corner cases:
+#
+# - two tags on the same file; both should be available
+# - a tag that is only on one file; this kind of tag
+# cannot be imported (at least not easily).
+
+test_expect_success 'two labels on the same changelist' '
+ test_when_finished cleanup_git &&
+ (
+ cd "$cli" &&
+ mkdir -p main &&
+
+ p4 edit main/f1 main/f2 &&
+ echo "hello world" >main/f1 &&
+ echo "not in the tag" >main/f2 &&
+ p4 submit -d "main/f[12]: testing two labels" &&
+
+ p4 tag -l TAG_F1_1 main/... &&
+ p4 tag -l TAG_F1_2 main/... &&
+
+ p4 labels ... &&
+
+ git p4 clone --dest="$git" //depot@all &&
+ cd "$git" &&
+ git p4 sync --import-labels &&
+
+ git tag | grep TAG_F1 &&
+ git tag | grep -q TAG_F1_1 &&
+ git tag | grep -q TAG_F1_2 &&
+
+ cd main &&
+
+ git checkout TAG_F1_1 &&
+ ls &&
+ test -f f1 &&
+
+ git checkout TAG_F1_2 &&
+ ls &&
+ test -f f1
+ )
+'
+
+# Export some git tags to p4
+test_expect_success 'export git tags to p4' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot@all &&
+ (
+ cd "$git" &&
+ git tag -m "A tag created in git:xyzzy" GIT_TAG_1 &&
+ echo "hello world" >main/f10 &&
+ git add main/f10 &&
+ git commit -m "Adding file for export test" &&
+ git config git-p4.skipSubmitEdit true &&
+ git p4 submit &&
+ git tag -m "Another git tag" GIT_TAG_2 &&
+ git tag LIGHTWEIGHT_TAG &&
+ git p4 rebase --import-labels --verbose &&
+ git p4 submit --export-labels --verbose
+ ) &&
+ (
+ cd "$cli" &&
+ p4 sync ... &&
+ p4 labels ... | grep GIT_TAG_1 &&
+ p4 labels ... | grep GIT_TAG_2 &&
+ p4 labels ... | grep LIGHTWEIGHT_TAG &&
+ p4 label -o GIT_TAG_1 | grep "tag created in git:xyzzy" &&
+ p4 sync ...@GIT_TAG_1 &&
+ ! test -f main/f10 &&
+ p4 sync ...@GIT_TAG_2 &&
+ test -f main/f10
+ )
+'
+
+# Export a tag from git where an affected file is deleted later on
+# Need to create git tags after rebase, since only then can the
+# git commits be mapped to p4 changelists.
+test_expect_success 'export git tags to p4 with deletion' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot@all &&
+ (
+ cd "$git" &&
+ git p4 sync --import-labels &&
+ echo "deleted file" >main/deleted_file &&
+ git add main/deleted_file &&
+ git commit -m "create deleted file" &&
+ git rm main/deleted_file &&
+ echo "new file" >main/f11 &&
+ git add main/f11 &&
+ git commit -m "delete the deleted file" &&
+ git config git-p4.skipSubmitEdit true &&
+ git p4 submit &&
+ git p4 rebase --import-labels --verbose &&
+ git tag -m "tag on deleted file" GIT_TAG_ON_DELETED HEAD~1 &&
+ git tag -m "tag after deletion" GIT_TAG_AFTER_DELETION HEAD &&
+ git p4 submit --export-labels --verbose
+ ) &&
+ (
+ cd "$cli" &&
+ p4 sync ... &&
+ p4 sync ...@GIT_TAG_ON_DELETED &&
+ test -f main/deleted_file &&
+ p4 sync ...@GIT_TAG_AFTER_DELETION &&
+ ! test -f main/deleted_file &&
+ echo "checking label contents" &&
+ p4 label -o GIT_TAG_ON_DELETED | grep "tag on deleted file"
+ )
+'
+
+# Create a tag in git that cannot be exported to p4
+test_expect_success 'tag that cannot be exported' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot@all &&
+ (
+ cd "$git" &&
+ git checkout -b a_branch &&
+ echo "hello" >main/f12 &&
+ git add main/f12 &&
+ git commit -m "adding f12" &&
+ git tag -m "tag on a_branch" GIT_TAG_ON_A_BRANCH &&
+ git checkout main &&
+ git p4 submit --export-labels
+ ) &&
+ (
+ cd "$cli" &&
+ p4 sync ... &&
+ ! p4 labels | grep GIT_TAG_ON_A_BRANCH
+ )
+'
+
+test_expect_success 'use git config to enable import/export of tags' '
+ git p4 clone --verbose --dest="$git" //depot@all &&
+ (
+ cd "$git" &&
+ git config git-p4.exportLabels true &&
+ git config git-p4.importLabels true &&
+ git tag CFG_A_GIT_TAG &&
+ git p4 rebase --verbose &&
+ git p4 submit --verbose &&
+ git tag &&
+ git tag | grep TAG_F1_1
+ ) &&
+ (
+ cd "$cli" &&
+ p4 labels &&
+ p4 labels | grep CFG_A_GIT_TAG
+ )
+'
+
+p4_head_revision() {
+ p4 changes -m 1 "$@" | awk '{print $2}'
+}
+
+# Importing a label that references a P4 commit that
+# has not been seen. The presence of a label on a commit
+# we haven't seen should not cause git-p4 to fail. It should
+# merely skip that label, and still import other labels.
+test_expect_success 'importing labels with missing revisions' '
+ test_when_finished cleanup_git &&
+ (
+ rm -fr "$cli" "$git" &&
+ mkdir "$cli" &&
+ P4CLIENT=missing-revision &&
+ client_view "//depot/missing-revision/... //missing-revision/..." &&
+ cd "$cli" &&
+ >f1 && p4 add f1 && p4 submit -d "start" &&
+
+ p4 tag -l TAG_S0 ... &&
+
+ >f2 && p4 add f2 && p4 submit -d "second" &&
+
+ startrev=$(p4_head_revision //depot/missing-revision/...) &&
+
+ >f3 && p4 add f3 && p4 submit -d "third" &&
+
+ p4 edit f2 && date >f2 && p4 submit -d "change" f2 &&
+
+ endrev=$(p4_head_revision //depot/missing-revision/...) &&
+
+ p4 tag -l TAG_S1 ... &&
+
+ # we should skip TAG_S0 since it is before our startpoint,
+ # but pick up TAG_S1.
+
+ git p4 clone --dest="$git" --import-labels -v \
+ //depot/missing-revision/...@$startrev,$endrev &&
+ (
+ cd "$git" &&
+ git rev-parse TAG_S1 &&
+ ! git rev-parse TAG_S0
+ )
+ )
+'
+
+test_done
diff --git a/t/t9812-git-p4-wildcards.sh b/t/t9812-git-p4-wildcards.sh
new file mode 100755
index 0000000..254a7c2
--- /dev/null
+++ b/t/t9812-git-p4-wildcards.sh
@@ -0,0 +1,214 @@
+#!/bin/sh
+
+test_description='git p4 wildcards'
+
+. ./lib-git-p4.sh
+
+test_expect_success 'start p4d' '
+ start_p4d
+'
+
+test_expect_success 'add p4 files with wildcards in the names' '
+ (
+ cd "$cli" &&
+ printf "file2\nhas\nsome\nrandom\ntext\n" >file2 &&
+ p4 add file2 &&
+ echo file-wild-hash >file-wild#hash &&
+ if test_have_prereq !MINGW,!CYGWIN
+ then
+ echo file-wild-star >file-wild\*star
+ fi &&
+ echo file-wild-at >file-wild@at &&
+ echo file-wild-percent >file-wild%percent &&
+ p4 add -f file-wild* &&
+ p4 submit -d "file wildcards"
+ )
+'
+
+test_expect_success 'wildcard files git p4 clone' '
+ git p4 clone --dest="$git" //depot &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ test -f file-wild#hash &&
+ if test_have_prereq !MINGW,!CYGWIN
+ then
+ test -f file-wild\*star
+ fi &&
+ test -f file-wild@at &&
+ test -f file-wild%percent
+ )
+'
+
+test_expect_success 'wildcard files submit back to p4, add' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ echo git-wild-hash >git-wild#hash &&
+ if test_have_prereq !MINGW,!CYGWIN
+ then
+ echo git-wild-star >git-wild\*star
+ fi &&
+ echo git-wild-at >git-wild@at &&
+ echo git-wild-percent >git-wild%percent &&
+ git add git-wild* &&
+ git commit -m "add some wildcard filenames" &&
+ git config git-p4.skipSubmitEdit true &&
+ git p4 submit
+ ) &&
+ (
+ cd "$cli" &&
+ test_path_is_file git-wild#hash &&
+ if test_have_prereq !MINGW,!CYGWIN
+ then
+ test_path_is_file git-wild\*star
+ fi &&
+ test_path_is_file git-wild@at &&
+ test_path_is_file git-wild%percent
+ )
+'
+
+test_expect_success 'wildcard files submit back to p4, modify' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ echo new-line >>git-wild#hash &&
+ if test_have_prereq !MINGW,!CYGWIN
+ then
+ echo new-line >>git-wild\*star
+ fi &&
+ echo new-line >>git-wild@at &&
+ echo new-line >>git-wild%percent &&
+ git add git-wild* &&
+ git commit -m "modify the wildcard files" &&
+ git config git-p4.skipSubmitEdit true &&
+ git p4 submit
+ ) &&
+ (
+ cd "$cli" &&
+ test_line_count = 2 git-wild#hash &&
+ if test_have_prereq !MINGW,!CYGWIN
+ then
+ test_line_count = 2 git-wild\*star
+ fi &&
+ test_line_count = 2 git-wild@at &&
+ test_line_count = 2 git-wild%percent
+ )
+'
+
+test_expect_success 'wildcard files submit back to p4, copy' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ cp file2 git-wild-cp#hash &&
+ git add git-wild-cp#hash &&
+ cp git-wild#hash file-wild-3 &&
+ git add file-wild-3 &&
+ git commit -m "wildcard copies" &&
+ git config git-p4.detectCopies true &&
+ git config git-p4.detectCopiesHarder true &&
+ git config git-p4.skipSubmitEdit true &&
+ git p4 submit
+ ) &&
+ (
+ cd "$cli" &&
+ test_path_is_file git-wild-cp#hash &&
+ test_path_is_file file-wild-3
+ )
+'
+
+test_expect_success 'wildcard files submit back to p4, rename' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ git mv git-wild@at file-wild-4 &&
+ git mv file-wild-3 git-wild-cp%percent &&
+ git commit -m "wildcard renames" &&
+ git config git-p4.detectRenames true &&
+ git config git-p4.skipSubmitEdit true &&
+ git p4 submit
+ ) &&
+ (
+ cd "$cli" &&
+ test_path_is_missing git-wild@at &&
+ test_path_is_file git-wild-cp%percent
+ )
+'
+
+test_expect_success 'wildcard files submit back to p4, delete' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ git rm git-wild* &&
+ git commit -m "delete the wildcard files" &&
+ git config git-p4.skipSubmitEdit true &&
+ git p4 submit
+ ) &&
+ (
+ cd "$cli" &&
+ test_path_is_missing git-wild#hash &&
+ if test_have_prereq !MINGW,!CYGWIN
+ then
+ test_path_is_missing git-wild\*star
+ fi &&
+ test_path_is_missing git-wild@at &&
+ test_path_is_missing git-wild%percent
+ )
+'
+
+test_expect_success 'p4 deleted a wildcard file' '
+ (
+ cd "$cli" &&
+ echo "wild delete test" >wild@delete &&
+ p4 add -f wild@delete &&
+ p4 submit -d "add wild@delete"
+ ) &&
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ test_path_is_file wild@delete
+ ) &&
+ (
+ cd "$cli" &&
+ # must use its encoded name
+ p4 delete wild%40delete &&
+ p4 submit -d "delete wild@delete"
+ ) &&
+ (
+ cd "$git" &&
+ git p4 sync &&
+ git merge --ff-only p4/master &&
+ test_path_is_missing wild@delete
+ )
+'
+
+test_expect_success 'wildcard files requiring keyword scrub' '
+ (
+ cd "$cli" &&
+ cat <<-\EOF >scrub@wild &&
+ $Id$
+ line2
+ EOF
+ p4 add -t text+k -f scrub@wild &&
+ p4 submit -d "scrub at wild"
+ ) &&
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ git config git-p4.skipSubmitEdit true &&
+ git config git-p4.attemptRCSCleanup true &&
+ sed "s/^line2/line2 edit/" <scrub@wild >scrub@wild.tmp &&
+ mv -f scrub@wild.tmp scrub@wild &&
+ git commit -m "scrub at wild line2 edit" scrub@wild &&
+ git p4 submit
+ )
+'
+
+test_done
diff --git a/t/t9813-git-p4-preserve-users.sh b/t/t9813-git-p4-preserve-users.sh
new file mode 100755
index 0000000..fd018c8
--- /dev/null
+++ b/t/t9813-git-p4-preserve-users.sh
@@ -0,0 +1,141 @@
+#!/bin/sh
+
+test_description='git p4 preserve users'
+
+. ./lib-git-p4.sh
+
+test_expect_success 'start p4d' '
+ start_p4d
+'
+
+test_expect_success 'create files' '
+ (
+ cd "$cli" &&
+ p4 client -o | sed "/LineEnd/s/:.*/:unix/" | p4 client -i &&
+ echo file1 >file1 &&
+ echo file2 >file2 &&
+ p4 add file1 file2 &&
+ p4 submit -d "add files"
+ )
+'
+
+p4_grant_admin() {
+ name=$1 &&
+ {
+ p4 protect -o &&
+ echo " admin user $name * //depot/..."
+ } | p4 protect -i
+}
+
+p4_check_commit_author() {
+ file=$1 user=$2 &&
+ p4 changes -m 1 //depot/$file | grep -q $user
+}
+
+make_change_by_user() {
+ file=$1 name=$2 email=$3 &&
+ echo "username: a change by $name" >>"$file" &&
+ git add "$file" &&
+ git commit --author "$name <$email>" -m "a change by $name"
+}
+
+# Test username support, submitting as user 'alice'
+test_expect_success 'preserve users' '
+ p4_add_user alice &&
+ p4_add_user bob &&
+ p4_grant_admin alice &&
+ git p4 clone --dest="$git" //depot &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ echo "username: a change by alice" >>file1 &&
+ echo "username: a change by bob" >>file2 &&
+ git commit --author "Alice <alice@example.com>" -m "a change by alice" file1 &&
+ git commit --author "Bob <bob@example.com>" -m "a change by bob" file2 &&
+ git config git-p4.skipSubmitEditCheck true &&
+ P4EDITOR="test-tool chmtime +5" P4USER=alice P4PASSWD=secret &&
+ export P4EDITOR P4USER P4PASSWD &&
+ git p4 commit --preserve-user &&
+ p4_check_commit_author file1 alice &&
+ p4_check_commit_author file2 bob
+ )
+'
+
+# Test username support, submitting as bob, who lacks admin rights. Should
+# not submit change to p4 (git diff should show deltas).
+test_expect_success 'refuse to preserve users without perms' '
+ git p4 clone --dest="$git" //depot &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git config git-p4.skipSubmitEditCheck true &&
+ echo "username-noperms: a change by alice" >>file1 &&
+ git commit --author "Alice <alice@example.com>" -m "perms: a change by alice" file1 &&
+ P4EDITOR="test-tool chmtime +5" P4USER=bob P4PASSWD=secret &&
+ export P4EDITOR P4USER P4PASSWD &&
+ test_must_fail git p4 commit --preserve-user &&
+ ! git diff --exit-code HEAD..p4/master
+ )
+'
+
+# What happens with unknown author? Without allowMissingP4Users it should fail.
+test_expect_success 'preserve user where author is unknown to p4' '
+ git p4 clone --dest="$git" //depot &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git config git-p4.skipSubmitEditCheck true &&
+ echo "username-bob: a change by bob" >>file1 &&
+ git commit --author "Bob <bob@example.com>" -m "preserve: a change by bob" file1 &&
+ echo "username-unknown: a change by charlie" >>file1 &&
+ git commit --author "Charlie <charlie@example.com>" -m "preserve: a change by charlie" file1 &&
+ P4EDITOR="test-tool chmtime +5" P4USER=alice P4PASSWD=secret &&
+ export P4EDITOR P4USER P4PASSWD &&
+ test_must_fail git p4 commit --preserve-user &&
+ ! git diff --exit-code HEAD..p4/master &&
+
+ echo "$0: repeat with allowMissingP4Users enabled" &&
+ git config git-p4.allowMissingP4Users true &&
+ git config git-p4.preserveUser true &&
+ git p4 commit &&
+ git diff --exit-code HEAD..p4/master &&
+ p4_check_commit_author file1 alice
+ )
+'
+
+# If we're *not* using --preserve-user, git-p4 should warn if we're submitting
+# changes that are not all ours.
+# Test: user in p4 and user unknown to p4.
+# Test: warning disabled and user is the same.
+test_expect_success 'not preserving user with mixed authorship' '
+ git p4 clone --dest="$git" //depot &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git config git-p4.skipSubmitEditCheck true &&
+ p4_add_user derek &&
+
+ make_change_by_user usernamefile3 Derek derek@example.com &&
+ P4EDITOR=cat P4USER=alice P4PASSWD=secret &&
+ export P4EDITOR P4USER P4PASSWD &&
+ git p4 commit >actual &&
+ grep "git author derek@example.com does not match" actual &&
+
+ make_change_by_user usernamefile3 Charlie charlie@example.com &&
+ git p4 commit >actual &&
+ grep "git author charlie@example.com does not match" actual &&
+
+ make_change_by_user usernamefile3 alice alice@example.com &&
+ git p4 commit >actual &&
+ ! grep "git author.*does not match" actual &&
+
+ git config git-p4.skipUserNameCheck true &&
+ make_change_by_user usernamefile3 Charlie charlie@example.com &&
+ git p4 commit >actual &&
+ ! grep "git author.*does not match" actual &&
+
+ p4_check_commit_author usernamefile3 alice
+ )
+'
+
+test_done
diff --git a/t/t9814-git-p4-rename.sh b/t/t9814-git-p4-rename.sh
new file mode 100755
index 0000000..2a9838f
--- /dev/null
+++ b/t/t9814-git-p4-rename.sh
@@ -0,0 +1,245 @@
+#!/bin/sh
+
+test_description='git p4 rename'
+
+. ./lib-git-p4.sh
+
+test_expect_success 'start p4d' '
+ start_p4d
+'
+
+# We rely on this behavior to detect for p4 move availability.
+test_expect_success '"p4 help unknown" errors out' '
+ (
+ cd "$cli" &&
+ p4 help client &&
+ ! p4 help nosuchcommand
+ )
+'
+
+test_expect_success 'create files' '
+ (
+ cd "$cli" &&
+ p4 client -o | sed "/LineEnd/s/:.*/:unix/" | p4 client -i &&
+ cat >file1 <<-EOF &&
+ A large block of text
+ in file1 that will generate
+ enough context so that rename
+ and copy detection will find
+ something interesting to do.
+ EOF
+ cat >file2 <<-EOF &&
+ /*
+ * This blob looks a bit
+ * different.
+ */
+ int main(int argc, char **argv)
+ {
+ char text[200];
+
+ strcpy(text, "copy/rename this");
+ printf("text is %s\n", text);
+ return 0;
+ }
+ EOF
+ p4 add file1 file2 &&
+ p4 submit -d "add files"
+ )
+'
+
+# Rename a file and confirm that rename is not detected in P4.
+# Rename the new file again with detectRenames option enabled and confirm that
+# this is detected in P4.
+# Rename the new file again adding an extra line, configure a big threshold in
+# detectRenames and confirm that rename is not detected in P4.
+# Repeat, this time with a smaller threshold and confirm that the rename is
+# detected in P4.
+test_expect_success 'detect renames' '
+ git p4 clone --dest="$git" //depot@all &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git config git-p4.skipSubmitEdit true &&
+
+ git mv file1 file4 &&
+ git commit -a -m "Rename file1 to file4" &&
+ git diff-tree -r -M HEAD &&
+ git p4 submit &&
+ p4 filelog //depot/file4 >filelog &&
+ ! grep " from //depot" filelog &&
+
+ git mv file4 file5 &&
+ git commit -a -m "Rename file4 to file5" &&
+ git diff-tree -r -M HEAD &&
+ git config git-p4.detectRenames true &&
+ git p4 submit &&
+ p4 filelog //depot/file5 >filelog &&
+ grep " from //depot/file4" filelog &&
+
+ git mv file5 file6 &&
+ echo update >>file6 &&
+ git add file6 &&
+ git commit -a -m "Rename file5 to file6 with changes" &&
+ git diff-tree -r -M HEAD &&
+ level=$(git diff-tree -r -M HEAD | sed 1d | cut -f1 | cut -d" " -f5 | sed "s/R0*//") &&
+ test -n "$level" && test "$level" -gt 0 && test "$level" -lt 98 &&
+ git config git-p4.detectRenames $(($level + 2)) &&
+ git p4 submit &&
+ p4 filelog //depot/file6 >filelog &&
+ ! grep " from //depot" filelog &&
+
+ git mv file6 file7 &&
+ echo update >>file7 &&
+ git add file7 &&
+ git commit -a -m "Rename file6 to file7 with changes" &&
+ git diff-tree -r -M HEAD &&
+ level=$(git diff-tree -r -M HEAD | sed 1d | cut -f1 | cut -d" " -f5 | sed "s/R0*//") &&
+ test -n "$level" && test "$level" -gt 2 && test "$level" -lt 100 &&
+ git config git-p4.detectRenames $(($level - 2)) &&
+ git p4 submit &&
+ p4 filelog //depot/file7 >filelog &&
+ grep " from //depot/file6" filelog
+ )
+'
+
+# Copy a file and confirm that copy is not detected in P4.
+# Copy a file with detectCopies option enabled and confirm that copy is not
+# detected in P4.
+# Modify and copy a file with detectCopies option enabled and confirm that copy
+# is detected in P4.
+# Copy a file with detectCopies and detectCopiesHarder options enabled and
+# confirm that copy is detected in P4.
+# Modify and copy a file, configure a bigger threshold in detectCopies and
+# confirm that copy is not detected in P4.
+# Modify and copy a file, configure a smaller threshold in detectCopies and
+# confirm that copy is detected in P4.
+test_expect_success 'detect copies' '
+ git p4 clone --dest="$git" //depot@all &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git config git-p4.skipSubmitEdit true &&
+
+ echo "file8" >>file2 &&
+ git commit -a -m "Differentiate file2" &&
+ git p4 submit &&
+ cp file2 file8 &&
+ git add file8 &&
+ git commit -a -m "Copy file2 to file8" &&
+ git diff-tree -r -C HEAD &&
+ git p4 submit &&
+ p4 filelog //depot/file8 &&
+ ! p4 filelog //depot/file8 | grep -q "branch from" &&
+
+ echo "file9" >>file2 &&
+ git commit -a -m "Differentiate file2" &&
+ git p4 submit &&
+
+ cp file2 file9 &&
+ git add file9 &&
+ git commit -a -m "Copy file2 to file9" &&
+ git diff-tree -r -C HEAD &&
+ git config git-p4.detectCopies true &&
+ git p4 submit &&
+ p4 filelog //depot/file9 &&
+ ! p4 filelog //depot/file9 | grep -q "branch from" &&
+
+ echo "file10" >>file2 &&
+ git commit -a -m "Differentiate file2" &&
+ git p4 submit &&
+
+ echo "file2" >>file2 &&
+ cp file2 file10 &&
+ git add file2 file10 &&
+ git commit -a -m "Modify and copy file2 to file10" &&
+ git diff-tree -r -C HEAD &&
+ src=$(git diff-tree -r -C HEAD | sed 1d | sed 2d | cut -f2) &&
+ test "$src" = file2 &&
+ git p4 submit &&
+ p4 filelog //depot/file10 &&
+ p4 filelog //depot/file10 | grep -q "branch from //depot/file2" &&
+
+ echo "file11" >>file2 &&
+ git commit -a -m "Differentiate file2" &&
+ git p4 submit &&
+
+ cp file2 file11 &&
+ git add file11 &&
+ git commit -a -m "Copy file2 to file11" &&
+ git diff-tree -r -C --find-copies-harder HEAD &&
+ src=$(git diff-tree -r -C --find-copies-harder HEAD | sed 1d | cut -f2) &&
+ test "$src" = file2 &&
+ git config git-p4.detectCopiesHarder true &&
+ git p4 submit &&
+ p4 filelog //depot/file11 &&
+ p4 filelog //depot/file11 | grep -q "branch from //depot/file2" &&
+
+ echo "file12" >>file2 &&
+ git commit -a -m "Differentiate file2" &&
+ git p4 submit &&
+
+ cp file2 file12 &&
+ echo "some text" >>file12 &&
+ git add file12 &&
+ git commit -a -m "Copy file2 to file12 with changes" &&
+ git diff-tree -r -C --find-copies-harder HEAD &&
+ level=$(git diff-tree -r -C --find-copies-harder HEAD | sed 1d | cut -f1 | cut -d" " -f5 | sed "s/C0*//") &&
+ test -n "$level" && test "$level" -gt 0 && test "$level" -lt 98 &&
+ src=$(git diff-tree -r -C --find-copies-harder HEAD | sed 1d | cut -f2) &&
+ test "$src" = file2 &&
+ git config git-p4.detectCopies $(($level + 2)) &&
+ git p4 submit &&
+ p4 filelog //depot/file12 &&
+ ! p4 filelog //depot/file12 | grep -q "branch from" &&
+
+ echo "file13" >>file2 &&
+ git commit -a -m "Differentiate file2" &&
+ git p4 submit &&
+
+ cp file2 file13 &&
+ echo "different text" >>file13 &&
+ git add file13 &&
+ git commit -a -m "Copy file2 to file13 with changes" &&
+ git diff-tree -r -C --find-copies-harder HEAD &&
+ level=$(git diff-tree -r -C --find-copies-harder HEAD | sed 1d | cut -f1 | cut -d" " -f5 | sed "s/C0*//") &&
+ test -n "$level" && test "$level" -gt 2 && test "$level" -lt 100 &&
+ src=$(git diff-tree -r -C --find-copies-harder HEAD | sed 1d | cut -f2) &&
+ test "$src" = file2 &&
+ git config git-p4.detectCopies $(($level - 2)) &&
+ git p4 submit &&
+ p4 filelog //depot/file13 &&
+ p4 filelog //depot/file13 | grep -q "branch from //depot/file2"
+ )
+'
+
+# See if configurables can be set, and in particular if the run.move.allow
+# variable exists, which allows admins to disable the "p4 move" command.
+test_lazy_prereq P4D_HAVE_CONFIGURABLE_RUN_MOVE_ALLOW '
+ p4 configure show run.move.allow >out &&
+ grep -E ^run.move.allow: out
+'
+
+# If move can be disabled, turn it off and test p4 move handling
+test_expect_success P4D_HAVE_CONFIGURABLE_RUN_MOVE_ALLOW \
+ 'do not use p4 move when administratively disabled' '
+ test_when_finished "p4 configure set run.move.allow=1" &&
+ p4 configure set run.move.allow=0 &&
+ (
+ cd "$cli" &&
+ echo move-disallow-file >move-disallow-file &&
+ p4 add move-disallow-file &&
+ p4 submit -d "add move-disallow-file"
+ ) &&
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ git config git-p4.skipSubmitEdit true &&
+ git config git-p4.detectRenames true &&
+ git mv move-disallow-file move-disallow-file-moved &&
+ git commit -m "move move-disallow-file" &&
+ git p4 submit
+ )
+'
+
+test_done
diff --git a/t/t9815-git-p4-submit-fail.sh b/t/t9815-git-p4-submit-fail.sh
new file mode 100755
index 0000000..c766fd1
--- /dev/null
+++ b/t/t9815-git-p4-submit-fail.sh
@@ -0,0 +1,425 @@
+#!/bin/sh
+
+test_description='git p4 submit failure handling'
+
+. ./lib-git-p4.sh
+
+test_expect_success 'start p4d' '
+ start_p4d
+'
+
+test_expect_success 'init depot' '
+ (
+ cd "$cli" &&
+ p4 client -o | sed "/LineEnd/s/:.*/:unix/" | p4 client -i &&
+ echo line1 >file1 &&
+ p4 add file1 &&
+ p4 submit -d "line1 in file1"
+ )
+'
+
+test_expect_success 'conflict on one commit' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$cli" &&
+ p4 open file1 &&
+ echo line2 >>file1 &&
+ p4 submit -d "line2 in file1"
+ ) &&
+ (
+ # now this commit should cause a conflict
+ cd "$git" &&
+ git config git-p4.skipSubmitEdit true &&
+ echo line3 >>file1 &&
+ git add file1 &&
+ git commit -m "line3 in file1 will conflict" &&
+ test_expect_code 1 git p4 submit >out &&
+ test_grep "No commits applied" out
+ )
+'
+
+test_expect_success 'conflict on second of two commits' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$cli" &&
+ p4 open file1 &&
+ echo line3 >>file1 &&
+ p4 submit -d "line3 in file1"
+ ) &&
+ (
+ cd "$git" &&
+ git config git-p4.skipSubmitEdit true &&
+ # this commit is okay
+ test_commit "first_commit_okay" &&
+ # now this submit should cause a conflict
+ echo line4 >>file1 &&
+ git add file1 &&
+ git commit -m "line4 in file1 will conflict" &&
+ test_expect_code 1 git p4 submit >out &&
+ test_grep "Applied only the commits" out
+ )
+'
+
+test_expect_success 'conflict on first of two commits, skip' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$cli" &&
+ p4 open file1 &&
+ echo line4 >>file1 &&
+ p4 submit -d "line4 in file1"
+ ) &&
+ (
+ cd "$git" &&
+ git config git-p4.skipSubmitEdit true &&
+ # this submit should cause a conflict
+ echo line5 >>file1 &&
+ git add file1 &&
+ git commit -m "line5 in file1 will conflict" &&
+ # but this commit is okay
+ test_commit "okay_commit_after_skip" &&
+ echo s | test_expect_code 1 git p4 submit >out &&
+ test_grep "Applied only the commits" out
+ )
+'
+
+test_expect_success 'conflict on first of two commits, quit' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$cli" &&
+ p4 open file1 &&
+ echo line7 >>file1 &&
+ p4 submit -d "line7 in file1"
+ ) &&
+ (
+ cd "$git" &&
+ git config git-p4.skipSubmitEdit true &&
+ # this submit should cause a conflict
+ echo line8 >>file1 &&
+ git add file1 &&
+ git commit -m "line8 in file1 will conflict" &&
+ # but this commit is okay
+ test_commit "okay_commit_after_quit" &&
+ echo q | test_expect_code 1 git p4 submit >out &&
+ test_grep "No commits applied" out
+ )
+'
+
+test_expect_success 'conflict cli and config options' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ git p4 submit --conflict=ask &&
+ git p4 submit --conflict=skip &&
+ git p4 submit --conflict=quit &&
+ test_expect_code 2 git p4 submit --conflict=foo &&
+ test_expect_code 2 git p4 submit --conflict &&
+ git config git-p4.conflict foo &&
+ test_expect_code 1 git p4 submit &&
+ git config --unset git-p4.conflict &&
+ git p4 submit
+ )
+'
+
+test_expect_success 'conflict on first of two commits, --conflict=skip' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$cli" &&
+ p4 open file1 &&
+ echo line9 >>file1 &&
+ p4 submit -d "line9 in file1"
+ ) &&
+ (
+ cd "$git" &&
+ git config git-p4.skipSubmitEdit true &&
+ # this submit should cause a conflict
+ echo line10 >>file1 &&
+ git add file1 &&
+ git commit -m "line10 in file1 will conflict" &&
+ # but this commit is okay
+ test_commit "okay_commit_after_auto_skip" &&
+ test_expect_code 1 git p4 submit --conflict=skip >out &&
+ test_grep "Applied only the commits" out
+ )
+'
+
+test_expect_success 'conflict on first of two commits, --conflict=quit' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$cli" &&
+ p4 open file1 &&
+ echo line11 >>file1 &&
+ p4 submit -d "line11 in file1"
+ ) &&
+ (
+ cd "$git" &&
+ git config git-p4.skipSubmitEdit true &&
+ # this submit should cause a conflict
+ echo line12 >>file1 &&
+ git add file1 &&
+ git commit -m "line12 in file1 will conflict" &&
+ # but this commit is okay
+ test_commit "okay_commit_after_auto_quit" &&
+ test_expect_code 1 git p4 submit --conflict=quit >out &&
+ test_grep "No commits applied" out
+ )
+'
+
+#
+# Cleanup after submit fail, all cases. Some modifications happen
+# before trying to apply the patch. Make sure these are unwound
+# properly. Put each one in a diff along with something that will
+# obviously conflict. Make sure it is back to normal after.
+#
+
+test_expect_success 'cleanup edit p4 populate' '
+ (
+ cd "$cli" &&
+ echo text file >text &&
+ p4 add text &&
+ echo text+x file >text+x &&
+ chmod 755 text+x &&
+ p4 add text+x &&
+ p4 submit -d "populate p4"
+ )
+'
+
+setup_conflict() {
+ # clone before modifying file1 to force it to conflict
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ # ticks outside subshells
+ test_tick &&
+ (
+ cd "$cli" &&
+ p4 open file1 &&
+ echo $test_tick >>file1 &&
+ p4 submit -d "$test_tick in file1"
+ ) &&
+ test_tick &&
+ (
+ cd "$git" &&
+ git config git-p4.skipSubmitEdit true &&
+ # easy conflict
+ echo $test_tick >>file1 &&
+ git add file1
+ # caller will add more and submit
+ )
+}
+
+test_expect_success 'cleanup edit after submit fail' '
+ setup_conflict &&
+ (
+ cd "$git" &&
+ echo another line >>text &&
+ git add text &&
+ git commit -m "conflict" &&
+ test_expect_code 1 git p4 submit
+ ) &&
+ (
+ cd "$cli" &&
+ # make sure it is not open
+ ! p4 fstat -T action text
+ )
+'
+
+test_expect_success 'cleanup add after submit fail' '
+ setup_conflict &&
+ (
+ cd "$git" &&
+ echo new file >textnew &&
+ git add textnew &&
+ git commit -m "conflict" &&
+ test_expect_code 1 git p4 submit
+ ) &&
+ (
+ cd "$cli" &&
+ # make sure it is not there
+ # and that p4 thinks it is not added
+ # P4 returns 0 both for "not there but added" and
+ # "not there", so grep.
+ test_path_is_missing textnew &&
+ p4 fstat -T action textnew 2>&1 | grep "no such file"
+ )
+'
+
+test_expect_success 'cleanup delete after submit fail' '
+ setup_conflict &&
+ (
+ cd "$git" &&
+ git rm text+x &&
+ git commit -m "conflict" &&
+ test_expect_code 1 git p4 submit
+ ) &&
+ (
+ cd "$cli" &&
+ # make sure it is there
+ test_path_is_file text+x &&
+ ! p4 fstat -T action text+x
+ )
+'
+
+test_expect_success 'cleanup copy after submit fail' '
+ setup_conflict &&
+ (
+ cd "$git" &&
+ cp text text2 &&
+ git add text2 &&
+ git commit -m "conflict" &&
+ git config git-p4.detectCopies true &&
+ git config git-p4.detectCopiesHarder true &&
+ # make sure setup is okay
+ git diff-tree -r -C --find-copies-harder HEAD | grep text2 | grep C100 &&
+ test_expect_code 1 git p4 submit
+ ) &&
+ (
+ cd "$cli" &&
+ test_path_is_missing text2 &&
+ p4 fstat -T action text2 2>&1 | grep "no such file"
+ )
+'
+
+test_expect_success 'cleanup rename after submit fail' '
+ setup_conflict &&
+ (
+ cd "$git" &&
+ git mv text text2 &&
+ git commit -m "conflict" &&
+ git config git-p4.detectRenames true &&
+ # make sure setup is okay
+ git diff-tree -r -M HEAD | grep text2 | grep R100 &&
+ test_expect_code 1 git p4 submit
+ ) &&
+ (
+ cd "$cli" &&
+ test_path_is_missing text2 &&
+ p4 fstat -T action text2 2>&1 | grep "no such file"
+ )
+'
+
+#
+# Cleanup after deciding not to submit during editTemplate. This
+# involves unwinding more work, because files have been added, deleted
+# and chmod-ed now. Same approach as above.
+#
+
+test_expect_success 'cleanup edit after submit cancel' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ echo line >>text &&
+ git add text &&
+ git commit -m text &&
+ echo n | test_expect_code 1 git p4 submit &&
+ git reset --hard HEAD^
+ ) &&
+ (
+ cd "$cli" &&
+ ! p4 fstat -T action text &&
+ test_cmp "$git"/text text
+ )
+'
+
+test_expect_success 'cleanup add after submit cancel' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ echo line >textnew &&
+ git add textnew &&
+ git commit -m textnew &&
+ echo n | test_expect_code 1 git p4 submit
+ ) &&
+ (
+ cd "$cli" &&
+ test_path_is_missing textnew &&
+ p4 fstat -T action textnew 2>&1 | grep "no such file"
+ )
+'
+
+test_expect_success 'cleanup delete after submit cancel' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ git rm text &&
+ git commit -m "rm text" &&
+ echo n | test_expect_code 1 git p4 submit
+ ) &&
+ (
+ cd "$cli" &&
+ test_path_is_file text &&
+ ! p4 fstat -T action text
+ )
+'
+
+test_expect_success 'cleanup copy after submit cancel' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ cp text text2 &&
+ git add text2 &&
+ git commit -m text2 &&
+ git config git-p4.detectCopies true &&
+ git config git-p4.detectCopiesHarder true &&
+ git diff-tree -r -C --find-copies-harder HEAD | grep text2 | grep C100 &&
+ echo n | test_expect_code 1 git p4 submit
+ ) &&
+ (
+ cd "$cli" &&
+ test_path_is_missing text2 &&
+ p4 fstat -T action text2 2>&1 | grep "no such file"
+ )
+'
+
+test_expect_success 'cleanup rename after submit cancel' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ git mv text text2 &&
+ git commit -m text2 &&
+ git config git-p4.detectRenames true &&
+ git diff-tree -r -M HEAD | grep text2 | grep R100 &&
+ echo n | test_expect_code 1 git p4 submit
+ ) &&
+ (
+ cd "$cli" &&
+ test_path_is_missing text2 &&
+ p4 fstat -T action text2 2>&1 | grep "no such file" &&
+ test_path_is_file text &&
+ ! p4 fstat -T action text
+ )
+'
+
+test_expect_success 'cleanup chmod after submit cancel' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ test_chmod +x text &&
+ test_chmod -x text+x &&
+ git add text text+x &&
+ git commit -m "chmod texts" &&
+ echo n | test_expect_code 1 git p4 submit
+ ) &&
+ (
+ cd "$cli" &&
+ test_path_is_file text &&
+ ! p4 fstat -T action text &&
+ test_path_is_file text+x &&
+ ! p4 fstat -T action text+x &&
+ ls -l text | grep -E ^-r-- &&
+ ls -l text+x | grep -E ^-r-x
+ )
+'
+
+test_done
diff --git a/t/t9816-git-p4-locked.sh b/t/t9816-git-p4-locked.sh
new file mode 100755
index 0000000..9328410
--- /dev/null
+++ b/t/t9816-git-p4-locked.sh
@@ -0,0 +1,141 @@
+#!/bin/sh
+
+test_description='git p4 locked file behavior'
+
+. ./lib-git-p4.sh
+
+test_expect_success 'start p4d' '
+ start_p4d
+'
+
+# See
+# http://www.perforce.com/perforce/doc.current/manuals/p4sag/03_superuser.html#1088563
+# for suggestions on how to configure "sitewide pessimistic locking"
+# where only one person can have a file open for edit at a time.
+test_expect_success 'init depot' '
+ (
+ cd "$cli" &&
+ echo "TypeMap: +l //depot/..." | p4 typemap -i &&
+ echo file1 >file1 &&
+ p4 add file1 &&
+ p4 submit -d "add file1"
+ )
+'
+
+test_expect_success 'edit with lock not taken' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ echo line2 >>file1 &&
+ git add file1 &&
+ git commit -m "line2 in file1" &&
+ git config git-p4.skipSubmitEdit true &&
+ git p4 submit
+ )
+'
+
+test_expect_success 'add with lock not taken' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ echo line1 >>add-lock-not-taken &&
+ git add add-lock-not-taken &&
+ git commit -m "add add-lock-not-taken" &&
+ git config git-p4.skipSubmitEdit true &&
+ git p4 submit --verbose
+ )
+'
+
+lock_in_another_client() {
+ # build a different client
+ cli2="$TRASH_DIRECTORY/cli2" &&
+ mkdir -p "$cli2" &&
+ test_when_finished "p4 client -f -d client2 && rm -rf \"$cli2\"" &&
+ (
+ cd "$cli2" &&
+ P4CLIENT=client2 &&
+ cli="$cli2" &&
+ client_view "//depot/... //client2/..." &&
+ p4 sync &&
+ p4 open file1
+ )
+}
+
+test_expect_failure 'edit with lock taken' '
+ lock_in_another_client &&
+ test_when_finished cleanup_git &&
+ test_when_finished "cd \"$cli\" && p4 sync -f file1" &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ echo line3 >>file1 &&
+ git add file1 &&
+ git commit -m "line3 in file1" &&
+ git config git-p4.skipSubmitEdit true &&
+ git p4 submit --verbose
+ )
+'
+
+test_expect_failure 'delete with lock taken' '
+ lock_in_another_client &&
+ test_when_finished cleanup_git &&
+ test_when_finished "cd \"$cli\" && p4 sync -f file1" &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ git rm file1 &&
+ git commit -m "delete file1" &&
+ git config git-p4.skipSubmitEdit true &&
+ git p4 submit --verbose
+ )
+'
+
+test_expect_failure 'chmod with lock taken' '
+ lock_in_another_client &&
+ test_when_finished cleanup_git &&
+ test_when_finished "cd \"$cli\" && p4 sync -f file1" &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ chmod +x file1 &&
+ git add file1 &&
+ git commit -m "chmod +x file1" &&
+ git config git-p4.skipSubmitEdit true &&
+ git p4 submit --verbose
+ )
+'
+
+test_expect_success 'copy with lock taken' '
+ lock_in_another_client &&
+ test_when_finished cleanup_git &&
+ test_when_finished "cd \"$cli\" && p4 revert file2 && rm -f file2" &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ cp file1 file2 &&
+ git add file2 &&
+ git commit -m "cp file1 to file2" &&
+ git config git-p4.skipSubmitEdit true &&
+ git config git-p4.detectCopies true &&
+ git p4 submit --verbose
+ )
+'
+
+test_expect_failure 'move with lock taken' '
+ lock_in_another_client &&
+ test_when_finished cleanup_git &&
+ test_when_finished "cd \"$cli\" && p4 sync file1 && rm -f file2" &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ git mv file1 file3 &&
+ git commit -m "mv file1 to file3" &&
+ git config git-p4.skipSubmitEdit true &&
+ git config git-p4.detectRenames true &&
+ git p4 submit --verbose
+ )
+'
+
+test_done
diff --git a/t/t9817-git-p4-exclude.sh b/t/t9817-git-p4-exclude.sh
new file mode 100755
index 0000000..ec3d937
--- /dev/null
+++ b/t/t9817-git-p4-exclude.sh
@@ -0,0 +1,108 @@
+#!/bin/sh
+
+test_description='git p4 tests for excluded paths during clone and sync'
+
+. ./lib-git-p4.sh
+
+test_expect_success 'start p4d' '
+ start_p4d
+'
+
+# Create a repo with the structure:
+#
+# //depot/wanted/foo
+# //depot/discard/foo
+#
+# Check that we can exclude a subdirectory with both
+# clone and sync operations.
+
+test_expect_success 'create exclude repo' '
+ (
+ cd "$cli" &&
+ mkdir -p wanted discard &&
+ echo wanted >wanted/foo &&
+ echo discard >discard/foo &&
+ echo discard_file >discard_file &&
+ echo discard_file_not >discard_file_not &&
+ p4 add wanted/foo discard/foo discard_file discard_file_not &&
+ p4 submit -d "initial revision"
+ )
+'
+
+test_expect_success 'check the repo was created correctly' '
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot/...@all &&
+ (
+ cd "$git" &&
+ test_path_is_file wanted/foo &&
+ test_path_is_file discard/foo &&
+ test_path_is_file discard_file &&
+ test_path_is_file discard_file_not
+ )
+'
+
+test_expect_success 'clone, excluding part of repo' '
+ test_when_finished cleanup_git &&
+ git p4 clone -//depot/discard/... --dest="$git" //depot/...@all &&
+ (
+ cd "$git" &&
+ test_path_is_file wanted/foo &&
+ test_path_is_missing discard/foo &&
+ test_path_is_file discard_file &&
+ test_path_is_file discard_file_not
+ )
+'
+
+test_expect_success 'clone, excluding single file, no trailing /' '
+ test_when_finished cleanup_git &&
+ git p4 clone -//depot/discard_file --dest="$git" //depot/...@all &&
+ (
+ cd "$git" &&
+ test_path_is_file wanted/foo &&
+ test_path_is_file discard/foo &&
+ test_path_is_missing discard_file &&
+ test_path_is_file discard_file_not
+ )
+'
+
+test_expect_success 'clone, then sync with exclude' '
+ test_when_finished cleanup_git &&
+ git p4 clone -//depot/discard/... --dest="$git" //depot/...@all &&
+ (
+ cd "$cli" &&
+ p4 edit wanted/foo discard/foo discard_file_not &&
+ date >>wanted/foo &&
+ date >>discard/foo &&
+ date >>discard_file_not &&
+ p4 submit -d "updating" &&
+
+ cd "$git" &&
+ git p4 sync -//depot/discard/... &&
+ test_path_is_file wanted/foo &&
+ test_path_is_missing discard/foo &&
+ test_path_is_file discard_file &&
+ test_path_is_file discard_file_not
+ )
+'
+
+test_expect_success 'clone, then sync with exclude, no trailing /' '
+ test_when_finished cleanup_git &&
+ git p4 clone -//depot/discard/... -//depot/discard_file --dest="$git" //depot/...@all &&
+ (
+ cd "$cli" &&
+ p4 edit wanted/foo discard/foo discard_file_not &&
+ date >>wanted/foo &&
+ date >>discard/foo &&
+ date >>discard_file_not &&
+ p4 submit -d "updating" &&
+
+ cd "$git" &&
+ git p4 sync -//depot/discard/... -//depot/discard_file &&
+ test_path_is_file wanted/foo &&
+ test_path_is_missing discard/foo &&
+ test_path_is_missing discard_file &&
+ test_path_is_file discard_file_not
+ )
+'
+
+test_done
diff --git a/t/t9818-git-p4-block.sh b/t/t9818-git-p4-block.sh
new file mode 100755
index 0000000..de591d8
--- /dev/null
+++ b/t/t9818-git-p4-block.sh
@@ -0,0 +1,149 @@
+#!/bin/sh
+
+test_description='git p4 fetching changes in multiple blocks'
+
+. ./lib-git-p4.sh
+
+test_expect_success 'start p4d' '
+ start_p4d
+'
+
+create_restricted_group() {
+ p4 group -i <<-EOF
+ Group: restricted
+ MaxResults: 7
+ MaxScanRows: 40
+ Users: author
+ EOF
+}
+
+test_expect_success 'Create group with limited maxrows' '
+ create_restricted_group
+'
+
+test_expect_success 'Create a repo with many changes' '
+ (
+ client_view "//depot/included/... //client/included/..." \
+ "//depot/excluded/... //client/excluded/..." &&
+ mkdir -p "$cli/included" "$cli/excluded" &&
+ cd "$cli/included" &&
+ >file.txt &&
+ p4 add file.txt &&
+ p4 submit -d "Add file.txt" &&
+ for i in $(test_seq 0 5)
+ do
+ >outer$i.txt &&
+ p4 add outer$i.txt &&
+ p4 submit -d "Adding outer$i.txt" &&
+ for j in $(test_seq 0 5)
+ do
+ p4 edit file.txt &&
+ echo $i$j >file.txt &&
+ p4 submit -d "Commit $i$j" || exit
+ done || exit
+ done
+ )
+'
+
+test_expect_success 'Default user cannot fetch changes' '
+ ! p4 changes -m 1 //depot/...
+'
+
+test_expect_success 'Clone the repo' '
+ git p4 clone --dest="$git" --changes-block-size=7 --verbose //depot/included@all
+'
+
+test_expect_success 'All files are present' '
+ echo file.txt >expected &&
+ test_write_lines outer0.txt outer1.txt outer2.txt outer3.txt outer4.txt >>expected &&
+ test_write_lines outer5.txt >>expected &&
+ ls "$git" >current &&
+ test_cmp expected current
+'
+
+test_expect_success 'file.txt is correct' '
+ echo 55 >expected &&
+ test_cmp expected "$git/file.txt"
+'
+
+test_expect_success 'Correct number of commits' '
+ (cd "$git" && git log --oneline) >log &&
+ wc -l log &&
+ test_line_count = 43 log
+'
+
+test_expect_success 'Previous version of file.txt is correct' '
+ (cd "$git" && git checkout HEAD^^) &&
+ echo 53 >expected &&
+ test_cmp expected "$git/file.txt"
+'
+
+# Test git-p4 sync, with some files outside the client specification.
+
+p4_add_file() {
+ (cd "$cli" &&
+ >$1 &&
+ p4 add $1 &&
+ p4 submit -d "Added file $1" $1
+ )
+}
+
+test_expect_success 'Add some more files' '
+ for i in $(test_seq 0 10)
+ do
+ p4_add_file "included/x$i" &&
+ p4_add_file "excluded/x$i" || return 1
+ done &&
+ for i in $(test_seq 0 10)
+ do
+ p4_add_file "excluded/y$i" || return 1
+ done
+'
+
+# This should pick up the 10 new files in "included", but not be confused
+# by the additional files in "excluded"
+test_expect_success 'Syncing files' '
+ (
+ cd "$git" &&
+ git p4 sync --changes-block-size=7 &&
+ git checkout p4/master &&
+ ls -l x* > log &&
+ test_line_count = 11 log
+ )
+'
+
+# Handling of multiple depot paths:
+# git p4 clone //depot/pathA //depot/pathB
+#
+test_expect_success 'Create a repo with multiple depot paths' '
+ client_view "//depot/pathA/... //client/pathA/..." \
+ "//depot/pathB/... //client/pathB/..." &&
+ mkdir -p "$cli/pathA" "$cli/pathB" &&
+ for p in pathA pathB
+ do
+ for i in $(test_seq 1 10)
+ do
+ p4_add_file "$p/file$p$i" || return 1
+ done
+ done
+'
+
+test_expect_success 'Clone repo with multiple depot paths' '
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git p4 clone --changes-block-size=4 //depot/pathA@all //depot/pathB@all \
+ --destination=dest &&
+ ls -1 dest >log &&
+ test_line_count = 20 log
+ )
+'
+
+test_expect_success 'Clone repo with self-sizing block size' '
+ test_when_finished cleanup_git &&
+ git p4 clone --changes-block-size=1000000 //depot@all --destination="$git" &&
+ git -C "$git" log --oneline >log &&
+ test_line_count \> 10 log
+'
+
+test_done
diff --git a/t/t9819-git-p4-case-folding.sh b/t/t9819-git-p4-case-folding.sh
new file mode 100755
index 0000000..b4d93f0
--- /dev/null
+++ b/t/t9819-git-p4-case-folding.sh
@@ -0,0 +1,56 @@
+#!/bin/sh
+
+test_description='interaction with P4 case-folding'
+
+. ./lib-git-p4.sh
+
+if test_have_prereq CASE_INSENSITIVE_FS
+then
+ skip_all='skipping P4 case-folding tests; case insensitive file system detected'
+ test_done
+fi
+
+test_expect_success 'start p4d with case folding enabled' '
+ start_p4d -C1
+'
+
+test_expect_success 'Create a repo, name is lowercase' '
+ (
+ client_view "//depot/... //client/..." &&
+ cd "$cli" &&
+ mkdir -p lc UC &&
+ >lc/file.txt && >UC/file.txt &&
+ p4 add lc/file.txt UC/file.txt &&
+ p4 submit -d "Add initial lc and UC repos"
+ )
+'
+
+test_expect_success 'Check p4 is in case-folding mode' '
+ (
+ cd "$cli" &&
+ >lc/FILE.TXT &&
+ p4 add lc/FILE.TXT &&
+ ! p4 submit -d "Cannot add file differing only in case" lc/FILE.TXT
+ )
+'
+
+# Check we created the repo properly
+test_expect_success 'Clone lc repo using lc name' '
+ git p4 clone //depot/lc/... &&
+ test_path_is_file lc/file.txt &&
+ git p4 clone //depot/UC/... &&
+ test_path_is_file UC/file.txt
+'
+
+# The clone should fail, since there is no repo called LC, but because
+# we have case-insensitive p4d enabled, it appears to go ahead and work,
+# but leaves an empty git repo in place.
+test_expect_failure 'Clone lc repo using uc name' '
+ test_must_fail git p4 clone //depot/LC/...
+'
+
+test_expect_failure 'Clone UC repo with lc name' '
+ test_must_fail git p4 clone //depot/uc/...
+'
+
+test_done
diff --git a/t/t9820-git-p4-editor-handling.sh b/t/t9820-git-p4-editor-handling.sh
new file mode 100755
index 0000000..fa1bba1
--- /dev/null
+++ b/t/t9820-git-p4-editor-handling.sh
@@ -0,0 +1,34 @@
+#!/bin/sh
+
+test_description='git p4 handling of EDITOR'
+
+. ./lib-git-p4.sh
+
+test_expect_success 'start p4d' '
+ start_p4d
+'
+
+test_expect_success 'init depot' '
+ (
+ cd "$cli" &&
+ echo file1 >file1 &&
+ p4 add file1 &&
+ p4 submit -d "file1"
+ )
+'
+
+# Check that the P4EDITOR argument can be given command-line
+# options, which git-p4 will then pass through to the shell.
+test_expect_success 'EDITOR with options' '
+ git p4 clone --dest="$git" //depot &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ echo change >file1 &&
+ git commit -m "change" file1 &&
+ P4EDITOR=": >\"$git/touched\" && test-tool chmtime +5" git p4 submit &&
+ test_path_is_file "$git/touched"
+ )
+'
+
+test_done
diff --git a/t/t9821-git-p4-path-variations.sh b/t/t9821-git-p4-path-variations.sh
new file mode 100755
index 0000000..ef80f16
--- /dev/null
+++ b/t/t9821-git-p4-path-variations.sh
@@ -0,0 +1,196 @@
+#!/bin/sh
+
+test_description='Clone repositories with path case variations'
+
+. ./lib-git-p4.sh
+
+test_expect_success 'start p4d with case folding enabled' '
+ start_p4d -C1
+'
+
+test_expect_success 'Create a repo with path case variations' '
+ client_view "//depot/... //client/..." &&
+ (
+ cd "$cli" &&
+
+ mkdir -p Path/to &&
+ >Path/to/File2.txt &&
+ p4 add Path/to/File2.txt &&
+ p4 submit -d "Add file2" &&
+ rm -rf Path &&
+
+ mkdir -p path/TO &&
+ >path/TO/file1.txt &&
+ p4 add path/TO/file1.txt &&
+ p4 submit -d "Add file1" &&
+ rm -rf path &&
+
+ mkdir -p path/to &&
+ >path/to/file3.txt &&
+ p4 add path/to/file3.txt &&
+ p4 submit -d "Add file3" &&
+ rm -rf path &&
+
+ mkdir -p x-outside-spec &&
+ >x-outside-spec/file4.txt &&
+ p4 add x-outside-spec/file4.txt &&
+ p4 submit -d "Add file4" &&
+ rm -rf x-outside-spec
+ )
+'
+
+test_expect_success 'Clone root' '
+ client_view "//depot/... //client/..." &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git init . &&
+ git config core.ignorecase false &&
+ git p4 clone --use-client-spec --destination="$git" //depot &&
+ # This method is used instead of "test -f" to ensure the case is
+ # checked even if the test is executed on case-insensitive file systems.
+ # All files are there as expected although the path cases look random.
+ cat >expect <<-\EOF &&
+ Path/to/File2.txt
+ path/TO/file1.txt
+ path/to/file3.txt
+ x-outside-spec/file4.txt
+ EOF
+ git ls-files >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'Clone root (ignorecase)' '
+ client_view "//depot/... //client/..." &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git init . &&
+ git config core.ignorecase true &&
+ git p4 clone --use-client-spec --destination="$git" //depot &&
+ # This method is used instead of "test -f" to ensure the case is
+ # checked even if the test is executed on case-insensitive file systems.
+ # All files are there as expected although the path cases look random.
+ cat >expect <<-\EOF &&
+ path/TO/File2.txt
+ path/TO/file1.txt
+ path/TO/file3.txt
+ x-outside-spec/file4.txt
+ EOF
+ git ls-files >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'Clone root and ignore one file' '
+ client_view \
+ "//depot/... //client/..." \
+ "-//depot/path/TO/file1.txt //client/path/TO/file1.txt" &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git init . &&
+ git config core.ignorecase false &&
+ git p4 clone --use-client-spec --destination="$git" //depot &&
+ # We ignore one file in the client spec and all path cases change from
+ # "TO" to "to"!
+ cat >expect <<-\EOF &&
+ Path/to/File2.txt
+ path/to/file3.txt
+ x-outside-spec/file4.txt
+ EOF
+ git ls-files >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'Clone root and ignore one file (ignorecase)' '
+ client_view \
+ "//depot/... //client/..." \
+ "-//depot/path/TO/file1.txt //client/path/TO/file1.txt" &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git init . &&
+ git config core.ignorecase true &&
+ git p4 clone --use-client-spec --destination="$git" //depot &&
+ # We ignore one file in the client spec and all path cases change from
+ # "TO" to "to"!
+ cat >expect <<-\EOF &&
+ Path/to/File2.txt
+ Path/to/file3.txt
+ x-outside-spec/file4.txt
+ EOF
+ git ls-files >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'Clone path' '
+ client_view "//depot/Path/... //client/..." &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git init . &&
+ git config core.ignorecase false &&
+ git p4 clone --use-client-spec --destination="$git" //depot &&
+ cat >expect <<-\EOF &&
+ to/File2.txt
+ EOF
+ git ls-files >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'Clone path (ignorecase)' '
+ client_view "//depot/Path/... //client/..." &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git init . &&
+ git config core.ignorecase true &&
+ git p4 clone --use-client-spec --destination="$git" //depot &&
+ cat >expect <<-\EOF &&
+ TO/File2.txt
+ TO/file1.txt
+ TO/file3.txt
+ EOF
+ git ls-files >actual &&
+ test_cmp expect actual
+ )
+'
+
+# It looks like P4 determines the path case based on the first file in
+# lexicographical order. Please note the lower case "to" directory for all
+# files triggered through the addition of "File0.txt".
+test_expect_success 'Add a new file and clone path with new file (ignorecase)' '
+ client_view "//depot/... //client/..." &&
+ (
+ cd "$cli" &&
+ mkdir -p Path/to &&
+ >Path/to/File0.txt &&
+ p4 add Path/to/File0.txt &&
+ p4 submit -d "Add file" &&
+ rm -rf Path
+ ) &&
+
+ client_view "//depot/Path/... //client/..." &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git init . &&
+ git config core.ignorecase true &&
+ git p4 clone --use-client-spec --destination="$git" //depot &&
+ cat >expect <<-\EOF &&
+ to/File0.txt
+ to/File2.txt
+ to/file1.txt
+ to/file3.txt
+ EOF
+ git ls-files >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_done
diff --git a/t/t9822-git-p4-path-encoding.sh b/t/t9822-git-p4-path-encoding.sh
new file mode 100755
index 0000000..572d395
--- /dev/null
+++ b/t/t9822-git-p4-path-encoding.sh
@@ -0,0 +1,77 @@
+#!/bin/sh
+
+test_description='Clone repositories with non ASCII paths'
+
+. ./lib-git-p4.sh
+
+UTF8_ESCAPED="a-\303\244_o-\303\266_u-\303\274.txt"
+ISO8859_ESCAPED="a-\344_o-\366_u-\374.txt"
+
+ISO8859="$(printf "$ISO8859_ESCAPED")" &&
+echo content123 >"$ISO8859" &&
+rm "$ISO8859" || {
+ skip_all="fs does not accept ISO-8859-1 filenames"
+ test_done
+}
+
+test_expect_success 'start p4d' '
+ start_p4d
+'
+
+test_expect_success 'Create a repo containing iso8859-1 encoded paths' '
+ (
+ cd "$cli" &&
+ ISO8859="$(printf "$ISO8859_ESCAPED")" &&
+ echo content123 >"$ISO8859" &&
+ p4 add "$ISO8859" &&
+ p4 submit -d "test commit"
+ )
+'
+
+test_expect_failure 'Clone auto-detects depot with iso8859-1 paths' '
+ git p4 clone --destination="$git" //depot &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ UTF8="$(printf "$UTF8_ESCAPED")" &&
+ echo "$UTF8" >expect &&
+ git -c core.quotepath=false ls-files >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'Clone repo containing iso8859-1 encoded paths with git-p4.pathEncoding' '
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git init . &&
+ git config git-p4.pathEncoding iso8859-1 &&
+ git p4 clone --use-client-spec --destination="$git" //depot &&
+ UTF8="$(printf "$UTF8_ESCAPED")" &&
+ echo "$UTF8" >expect &&
+ git -c core.quotepath=false ls-files >actual &&
+ test_cmp expect actual &&
+
+ echo content123 >expect &&
+ cat "$UTF8" >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'Delete iso8859-1 encoded paths and clone' '
+ (
+ cd "$cli" &&
+ ISO8859="$(printf "$ISO8859_ESCAPED")" &&
+ p4 delete "$ISO8859" &&
+ p4 submit -d "remove file"
+ ) &&
+ git p4 clone --destination="$git" //depot@all &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git -c core.quotepath=false ls-files >actual &&
+ test_must_be_empty actual
+ )
+'
+
+test_done
diff --git a/t/t9823-git-p4-mock-lfs.sh b/t/t9823-git-p4-mock-lfs.sh
new file mode 100755
index 0000000..88b76dc
--- /dev/null
+++ b/t/t9823-git-p4-mock-lfs.sh
@@ -0,0 +1,188 @@
+#!/bin/sh
+
+test_description='Clone repositories and store files in Mock LFS'
+
+. ./lib-git-p4.sh
+
+test_file_is_not_in_mock_lfs () {
+ FILE="$1" &&
+ CONTENT="$2" &&
+ echo "$CONTENT" >expect_content &&
+ test_path_is_file "$FILE" &&
+ test_cmp expect_content "$FILE"
+}
+
+test_file_is_in_mock_lfs () {
+ FILE="$1" &&
+ CONTENT="$2" &&
+ LOCAL_STORAGE=".git/mock-storage/local/$CONTENT" &&
+ SERVER_STORAGE=".git/mock-storage/remote/$CONTENT" &&
+ echo "pointer-$CONTENT" >expect_pointer &&
+ echo "$CONTENT" >expect_content &&
+ test_path_is_file "$FILE" &&
+ test_path_is_file "$LOCAL_STORAGE" &&
+ test_path_is_file "$SERVER_STORAGE" &&
+ test_cmp expect_pointer "$FILE" &&
+ test_cmp expect_content "$LOCAL_STORAGE" &&
+ test_cmp expect_content "$SERVER_STORAGE"
+}
+
+test_file_is_deleted_in_mock_lfs () {
+ FILE="$1" &&
+ CONTENT="$2" &&
+ LOCAL_STORAGE=".git/mock-storage/local/$CONTENT" &&
+ SERVER_STORAGE=".git/mock-storage/remote/$CONTENT" &&
+ echo "pointer-$CONTENT" >expect_pointer &&
+ echo "$CONTENT" >expect_content &&
+ test_path_is_missing "$FILE" &&
+ test_path_is_file "$LOCAL_STORAGE" &&
+ test_path_is_file "$SERVER_STORAGE" &&
+ test_cmp expect_content "$LOCAL_STORAGE" &&
+ test_cmp expect_content "$SERVER_STORAGE"
+}
+
+test_file_count_in_dir () {
+ DIR="$1" &&
+ EXPECTED_COUNT="$2" &&
+ find "$DIR" -type f >actual &&
+ test_line_count = $EXPECTED_COUNT actual
+}
+
+test_expect_success 'start p4d' '
+ start_p4d
+'
+
+test_expect_success 'Create repo with binary files' '
+ client_view "//depot/... //client/..." &&
+ (
+ cd "$cli" &&
+
+ echo "content 1 txt 23 bytes" >file1.txt &&
+ p4 add file1.txt &&
+ echo "content 2-3 bin 25 bytes" >file2.dat &&
+ p4 add file2.dat &&
+ p4 submit -d "Add text and binary file" &&
+
+ mkdir "path with spaces" &&
+ echo "content 2-3 bin 25 bytes" >"path with spaces/file3.bin" &&
+ p4 add "path with spaces/file3.bin" &&
+ p4 submit -d "Add another binary file with same content and spaces in path" &&
+
+ echo "content 4 bin 26 bytes XX" >file4.bin &&
+ p4 add file4.bin &&
+ p4 submit -d "Add another binary file with different content"
+ )
+'
+
+test_expect_success 'Store files in Mock LFS based on size (>24 bytes)' '
+ client_view "//depot/... //client/..." &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git init . &&
+ git config git-p4.useClientSpec true &&
+ git config git-p4.largeFileSystem MockLFS &&
+ git config git-p4.largeFileThreshold 24 &&
+ git config git-p4.largeFilePush True &&
+ git p4 clone --destination="$git" //depot@all &&
+
+ test_file_is_not_in_mock_lfs file1.txt "content 1 txt 23 bytes" &&
+ test_file_is_in_mock_lfs file2.dat "content 2-3 bin 25 bytes" &&
+ test_file_is_in_mock_lfs "path with spaces/file3.bin" "content 2-3 bin 25 bytes" &&
+ test_file_is_in_mock_lfs file4.bin "content 4 bin 26 bytes XX" &&
+
+ test_file_count_in_dir ".git/mock-storage/local" 2 &&
+ test_file_count_in_dir ".git/mock-storage/remote" 2
+ )
+'
+
+test_expect_success 'Store files in Mock LFS based on extension (dat)' '
+ client_view "//depot/... //client/..." &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git init . &&
+ git config git-p4.useClientSpec true &&
+ git config git-p4.largeFileSystem MockLFS &&
+ git config git-p4.largeFileExtensions dat &&
+ git config git-p4.largeFilePush True &&
+ git p4 clone --destination="$git" //depot@all &&
+
+ test_file_is_not_in_mock_lfs file1.txt "content 1 txt 23 bytes" &&
+ test_file_is_in_mock_lfs file2.dat "content 2-3 bin 25 bytes" &&
+ test_file_is_not_in_mock_lfs "path with spaces/file3.bin" "content 2-3 bin 25 bytes" &&
+ test_file_is_not_in_mock_lfs file4.bin "content 4 bin 26 bytes XX" &&
+
+ test_file_count_in_dir ".git/mock-storage/local" 1 &&
+ test_file_count_in_dir ".git/mock-storage/remote" 1
+ )
+'
+
+test_expect_success 'Store files in Mock LFS based on extension (dat) and use git p4 sync and no client spec' '
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git init &&
+ git config git-p4.useClientSpec true &&
+ git config git-p4.largeFileSystem MockLFS &&
+ git config git-p4.largeFileExtensions dat &&
+ git config git-p4.largeFilePush True &&
+ git p4 sync //depot &&
+ git checkout p4/master &&
+
+ test_file_is_not_in_mock_lfs file1.txt "content 1 txt 23 bytes" &&
+ test_file_is_in_mock_lfs file2.dat "content 2-3 bin 25 bytes" &&
+ test_file_is_not_in_mock_lfs "path with spaces/file3.bin" "content 2-3 bin 25 bytes" &&
+ test_file_is_not_in_mock_lfs file4.bin "content 4 bin 26 bytes XX" &&
+
+ test_file_count_in_dir ".git/mock-storage/local" 1 &&
+ test_file_count_in_dir ".git/mock-storage/remote" 1
+ )
+'
+
+test_expect_success 'Remove file from repo and store files in Mock LFS based on size (>24 bytes)' '
+ client_view "//depot/... //client/..." &&
+ (
+ cd "$cli" &&
+ p4 delete file4.bin &&
+ p4 submit -d "Remove file"
+ ) &&
+
+ client_view "//depot/... //client/..." &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git init . &&
+ git config git-p4.useClientSpec true &&
+ git config git-p4.largeFileSystem MockLFS &&
+ git config git-p4.largeFileThreshold 24 &&
+ git config git-p4.largeFilePush True &&
+ git p4 clone --destination="$git" //depot@all &&
+
+ test_file_is_not_in_mock_lfs file1.txt "content 1 txt 23 bytes" &&
+ test_file_is_in_mock_lfs file2.dat "content 2-3 bin 25 bytes" &&
+ test_file_is_in_mock_lfs "path with spaces/file3.bin" "content 2-3 bin 25 bytes" &&
+ test_file_is_deleted_in_mock_lfs file4.bin "content 4 bin 26 bytes XX" &&
+
+ test_file_count_in_dir ".git/mock-storage/local" 2 &&
+ test_file_count_in_dir ".git/mock-storage/remote" 2
+ )
+'
+
+test_expect_success 'Run git p4 submit in repo configured with large file system' '
+ client_view "//depot/... //client/..." &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git init . &&
+ git config git-p4.useClientSpec true &&
+ git config git-p4.largeFileSystem MockLFS &&
+ git config git-p4.largeFileThreshold 24 &&
+ git config git-p4.largeFilePush True &&
+ git p4 clone --destination="$git" //depot@all &&
+
+ test_must_fail git p4 submit
+ )
+'
+
+test_done
diff --git a/t/t9824-git-p4-git-lfs.sh b/t/t9824-git-p4-git-lfs.sh
new file mode 100755
index 0000000..a28dbbd
--- /dev/null
+++ b/t/t9824-git-p4-git-lfs.sh
@@ -0,0 +1,290 @@
+#!/bin/sh
+
+test_description='Clone repositories and store files in Git LFS'
+
+. ./lib-git-p4.sh
+
+git lfs help >/dev/null 2>&1 || {
+ skip_all='skipping git p4 Git LFS tests; Git LFS not found'
+ test_done
+}
+
+test_file_in_lfs () {
+ FILE="$1" &&
+ SIZE="$2" &&
+ EXPECTED_CONTENT="$3" &&
+ sed -n '1,1 p' "$FILE" | grep "^version " &&
+ sed -n '2,2 p' "$FILE" | grep "^oid " &&
+ sed -n '3,3 p' "$FILE" | grep "^size " &&
+ test_line_count = 3 "$FILE" &&
+ cat "$FILE" | grep "size $SIZE" &&
+ HASH=$(cat "$FILE" | grep "oid sha256:" | sed -e "s/oid sha256://g") &&
+ LFS_FILE=".git/lfs/objects/$(echo "$HASH" | cut -c1-2)/$(echo "$HASH" | cut -c3-4)/$HASH" &&
+ echo $EXPECTED_CONTENT >expect &&
+ test_path_is_file "$FILE" &&
+ test_path_is_file "$LFS_FILE" &&
+ test_cmp expect "$LFS_FILE"
+}
+
+test_file_count_in_dir () {
+ DIR="$1" &&
+ EXPECTED_COUNT="$2" &&
+ find "$DIR" -type f >actual &&
+ test_line_count = $EXPECTED_COUNT actual
+}
+
+test_expect_success 'start p4d' '
+ start_p4d
+'
+
+test_expect_success 'Create repo with binary files' '
+ client_view "//depot/... //client/..." &&
+ (
+ cd "$cli" &&
+
+ >file0.dat &&
+ p4 add file0.dat &&
+ echo "content 1 txt 23 bytes" >file1.txt &&
+ p4 add file1.txt &&
+ echo "content 2-3 bin 25 bytes" >file2.dat &&
+ p4 add file2.dat &&
+ p4 submit -d "Add text and binary file" &&
+
+ mkdir "path with spaces" &&
+ echo "content 2-3 bin 25 bytes" >"path with spaces/file3.bin" &&
+ p4 add "path with spaces/file3.bin" &&
+ p4 submit -d "Add another binary file with same content and spaces in path" &&
+
+ echo "content 4 bin 26 bytes XX" >file4.bin &&
+ p4 add file4.bin &&
+ p4 submit -d "Add another binary file with different content"
+ )
+'
+
+test_expect_success 'Store files in LFS based on size (>24 bytes)' '
+ client_view "//depot/... //client/..." &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git init . &&
+ git config git-p4.useClientSpec true &&
+ git config git-p4.largeFileSystem GitLFS &&
+ git config git-p4.largeFileThreshold 24 &&
+ git p4 clone --destination="$git" //depot@all &&
+
+ test_file_in_lfs file2.dat 25 "content 2-3 bin 25 bytes" &&
+ test_file_in_lfs "path with spaces/file3.bin" 25 "content 2-3 bin 25 bytes" &&
+ test_file_in_lfs file4.bin 26 "content 4 bin 26 bytes XX" &&
+
+ test_file_count_in_dir ".git/lfs/objects" 2 &&
+
+ cat >expect <<-\EOF &&
+
+ #
+ # Git LFS (see https://git-lfs.github.com/)
+ #
+ /file2.dat filter=lfs diff=lfs merge=lfs -text
+ /file4.bin filter=lfs diff=lfs merge=lfs -text
+ /path[[:space:]]with[[:space:]]spaces/file3.bin filter=lfs diff=lfs merge=lfs -text
+ EOF
+ test_path_is_file .gitattributes &&
+ test_cmp expect .gitattributes
+ )
+'
+
+test_expect_success 'Store files in LFS based on size (>25 bytes)' '
+ client_view "//depot/... //client/..." &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git init . &&
+ git config git-p4.useClientSpec true &&
+ git config git-p4.largeFileSystem GitLFS &&
+ git config git-p4.largeFileThreshold 25 &&
+ git p4 clone --destination="$git" //depot@all &&
+
+ test_file_in_lfs file4.bin 26 "content 4 bin 26 bytes XX" &&
+ test_file_count_in_dir ".git/lfs/objects" 1 &&
+
+ cat >expect <<-\EOF &&
+
+ #
+ # Git LFS (see https://git-lfs.github.com/)
+ #
+ /file4.bin filter=lfs diff=lfs merge=lfs -text
+ EOF
+ test_path_is_file .gitattributes &&
+ test_cmp expect .gitattributes
+ )
+'
+
+test_expect_success 'Store files in LFS based on extension (dat)' '
+ client_view "//depot/... //client/..." &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git init . &&
+ git config git-p4.useClientSpec true &&
+ git config git-p4.largeFileSystem GitLFS &&
+ git config git-p4.largeFileExtensions dat &&
+ git p4 clone --destination="$git" //depot@all &&
+
+ test_file_in_lfs file2.dat 25 "content 2-3 bin 25 bytes" &&
+ test_file_count_in_dir ".git/lfs/objects" 1 &&
+
+ cat >expect <<-\EOF &&
+
+ #
+ # Git LFS (see https://git-lfs.github.com/)
+ #
+ *.dat filter=lfs diff=lfs merge=lfs -text
+ EOF
+ test_path_is_file .gitattributes &&
+ test_cmp expect .gitattributes
+ )
+'
+
+test_expect_success 'Store files in LFS based on size (>25 bytes) and extension (dat)' '
+ client_view "//depot/... //client/..." &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git init . &&
+ git config git-p4.useClientSpec true &&
+ git config git-p4.largeFileSystem GitLFS &&
+ git config git-p4.largeFileExtensions dat &&
+ git config git-p4.largeFileThreshold 25 &&
+ git p4 clone --destination="$git" //depot@all &&
+
+ test_file_in_lfs file2.dat 25 "content 2-3 bin 25 bytes" &&
+ test_file_in_lfs file4.bin 26 "content 4 bin 26 bytes XX" &&
+ test_file_count_in_dir ".git/lfs/objects" 2 &&
+
+ cat >expect <<-\EOF &&
+
+ #
+ # Git LFS (see https://git-lfs.github.com/)
+ #
+ *.dat filter=lfs diff=lfs merge=lfs -text
+ /file4.bin filter=lfs diff=lfs merge=lfs -text
+ EOF
+ test_path_is_file .gitattributes &&
+ test_cmp expect .gitattributes
+ )
+'
+
+test_expect_success 'Remove file from repo and store files in LFS based on size (>24 bytes)' '
+ client_view "//depot/... //client/..." &&
+ (
+ cd "$cli" &&
+ p4 delete file4.bin &&
+ p4 submit -d "Remove file"
+ ) &&
+
+ client_view "//depot/... //client/..." &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git init . &&
+ git config git-p4.useClientSpec true &&
+ git config git-p4.largeFileSystem GitLFS &&
+ git config git-p4.largeFileThreshold 24 &&
+ git p4 clone --destination="$git" //depot@all &&
+
+ test_file_in_lfs file2.dat 25 "content 2-3 bin 25 bytes" &&
+ test_file_in_lfs "path with spaces/file3.bin" 25 "content 2-3 bin 25 bytes" &&
+ test_path_is_missing file4.bin &&
+ test_file_count_in_dir ".git/lfs/objects" 2 &&
+
+ cat >expect <<-\EOF &&
+
+ #
+ # Git LFS (see https://git-lfs.github.com/)
+ #
+ /file2.dat filter=lfs diff=lfs merge=lfs -text
+ /path[[:space:]]with[[:space:]]spaces/file3.bin filter=lfs diff=lfs merge=lfs -text
+ EOF
+ test_path_is_file .gitattributes &&
+ test_cmp expect .gitattributes
+ )
+'
+
+test_expect_success 'Add .gitattributes and store files in LFS based on size (>24 bytes)' '
+ client_view "//depot/... //client/..." &&
+ (
+ cd "$cli" &&
+ echo "*.txt text" >.gitattributes &&
+ p4 add .gitattributes &&
+ p4 submit -d "Add .gitattributes"
+ ) &&
+
+ client_view "//depot/... //client/..." &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git init . &&
+ git config git-p4.useClientSpec true &&
+ git config git-p4.largeFileSystem GitLFS &&
+ git config git-p4.largeFileThreshold 24 &&
+ git p4 clone --destination="$git" //depot@all &&
+
+ test_file_in_lfs file2.dat 25 "content 2-3 bin 25 bytes" &&
+ test_file_in_lfs "path with spaces/file3.bin" 25 "content 2-3 bin 25 bytes" &&
+ test_path_is_missing file4.bin &&
+ test_file_count_in_dir ".git/lfs/objects" 2 &&
+
+ cat >expect <<-\EOF &&
+ *.txt text
+
+ #
+ # Git LFS (see https://git-lfs.github.com/)
+ #
+ /file2.dat filter=lfs diff=lfs merge=lfs -text
+ /path[[:space:]]with[[:space:]]spaces/file3.bin filter=lfs diff=lfs merge=lfs -text
+ EOF
+ test_path_is_file .gitattributes &&
+ test_cmp expect .gitattributes
+ )
+'
+
+test_expect_success 'Add big files to repo and store files in LFS based on compressed size (>28 bytes)' '
+ client_view "//depot/... //client/..." &&
+ (
+ cd "$cli" &&
+ echo "content 5 bin 40 bytes XXXXXXXXXXXXXXXX" >file5.bin &&
+ p4 add file5.bin &&
+ p4 submit -d "Add file with small footprint after compression" &&
+
+ echo "content 6 bin 39 bytes XXXXXYYYYYZZZZZ" >file6.bin &&
+ p4 add file6.bin &&
+ p4 submit -d "Add file with large footprint after compression"
+ ) &&
+
+ client_view "//depot/... //client/..." &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git init . &&
+ git config git-p4.useClientSpec true &&
+ git config git-p4.largeFileSystem GitLFS &&
+ git config git-p4.largeFileCompressedThreshold 28 &&
+ # We only import HEAD here ("@all" is missing!)
+ git p4 clone --destination="$git" //depot &&
+
+ test_file_in_lfs file6.bin 39 "content 6 bin 39 bytes XXXXXYYYYYZZZZZ" &&
+ test_file_count_in_dir ".git/lfs/objects" 1 &&
+
+ cat >expect <<-\EOF &&
+ *.txt text
+
+ #
+ # Git LFS (see https://git-lfs.github.com/)
+ #
+ /file6.bin filter=lfs diff=lfs merge=lfs -text
+ EOF
+ test_path_is_file .gitattributes &&
+ test_cmp expect .gitattributes
+ )
+'
+
+test_done
diff --git a/t/t9825-git-p4-handle-utf16-without-bom.sh b/t/t9825-git-p4-handle-utf16-without-bom.sh
new file mode 100755
index 0000000..f049ff8
--- /dev/null
+++ b/t/t9825-git-p4-handle-utf16-without-bom.sh
@@ -0,0 +1,46 @@
+#!/bin/sh
+
+test_description='git p4 handling of UTF-16 files without BOM'
+
+. ./lib-git-p4.sh
+
+UTF16="\227\000\227\000"
+
+test_expect_success 'start p4d' '
+ start_p4d
+'
+
+test_expect_success 'init depot with UTF-16 encoded file and artificially remove BOM' '
+ (
+ cd "$cli" &&
+ printf "$UTF16" >file1 &&
+ p4 add -t utf16 file1 &&
+ p4 submit -d "file1"
+ ) &&
+
+ (
+ cd db &&
+ p4d -jc &&
+ # P4D automatically adds a BOM. Remove it here to make the file invalid.
+ sed -e "\$d" depot/file1,v >depot/file1,v.new &&
+ mv depot/file1,v.new depot/file1,v &&
+ printf "@$UTF16@" >>depot/file1,v &&
+ p4d -jrF checkpoint.1
+ )
+'
+
+test_expect_success 'clone depot with invalid UTF-16 file in verbose mode' '
+ git p4 clone --dest="$git" --verbose //depot &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ printf "$UTF16" >expect &&
+ test_cmp_bin expect file1
+ )
+'
+
+test_expect_failure 'clone depot with invalid UTF-16 file in non-verbose mode' '
+ git p4 clone --dest="$git" //depot
+'
+
+test_done
diff --git a/t/t9826-git-p4-keep-empty-commits.sh b/t/t9826-git-p4-keep-empty-commits.sh
new file mode 100755
index 0000000..fd64afe
--- /dev/null
+++ b/t/t9826-git-p4-keep-empty-commits.sh
@@ -0,0 +1,130 @@
+#!/bin/sh
+
+test_description='Clone repositories and keep empty commits'
+
+. ./lib-git-p4.sh
+
+test_expect_success 'start p4d' '
+ start_p4d
+'
+
+test_expect_success 'Create a repo' '
+ client_view "//depot/... //client/..." &&
+ (
+ cd "$cli" &&
+
+ mkdir -p subdir &&
+
+ >subdir/file1.txt &&
+ p4 add subdir/file1.txt &&
+ p4 submit -d "Add file 1" &&
+
+ >file2.txt &&
+ p4 add file2.txt &&
+ p4 submit -d "Add file 2" &&
+
+ >subdir/file3.txt &&
+ p4 add subdir/file3.txt &&
+ p4 submit -d "Add file 3" &&
+
+ >file4.txt &&
+ p4 add file4.txt &&
+ p4 submit -d "Add file 4" &&
+
+ p4 delete subdir/file3.txt &&
+ p4 submit -d "Remove file 3" &&
+
+ p4 delete file4.txt &&
+ p4 submit -d "Remove file 4"
+ )
+'
+
+test_expect_success 'Clone repo root path with all history' '
+ client_view "//depot/... //client/..." &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git init . &&
+ git p4 clone --use-client-spec --destination="$git" //depot@all &&
+ cat >expect <<-\EOF &&
+ Remove file 4
+ [git-p4: depot-paths = "//depot/": change = 6]
+
+ Remove file 3
+ [git-p4: depot-paths = "//depot/": change = 5]
+
+ Add file 4
+ [git-p4: depot-paths = "//depot/": change = 4]
+
+ Add file 3
+ [git-p4: depot-paths = "//depot/": change = 3]
+
+ Add file 2
+ [git-p4: depot-paths = "//depot/": change = 2]
+
+ Add file 1
+ [git-p4: depot-paths = "//depot/": change = 1]
+
+ EOF
+ git log --format=%B >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'Clone repo subdir with all history but keep empty commits' '
+ client_view "//depot/subdir/... //client/subdir/..." &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git init . &&
+ git config git-p4.keepEmptyCommits true &&
+ git p4 clone --use-client-spec --destination="$git" //depot@all &&
+ cat >expect <<-\EOF &&
+ Remove file 4
+ [git-p4: depot-paths = "//depot/": change = 6]
+
+ Remove file 3
+ [git-p4: depot-paths = "//depot/": change = 5]
+
+ Add file 4
+ [git-p4: depot-paths = "//depot/": change = 4]
+
+ Add file 3
+ [git-p4: depot-paths = "//depot/": change = 3]
+
+ Add file 2
+ [git-p4: depot-paths = "//depot/": change = 2]
+
+ Add file 1
+ [git-p4: depot-paths = "//depot/": change = 1]
+
+ EOF
+ git log --format=%B >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'Clone repo subdir with all history' '
+ client_view "//depot/subdir/... //client/subdir/..." &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git init . &&
+ git p4 clone --use-client-spec --destination="$git" --verbose //depot@all &&
+ cat >expect <<-\EOF &&
+ Remove file 3
+ [git-p4: depot-paths = "//depot/": change = 5]
+
+ Add file 3
+ [git-p4: depot-paths = "//depot/": change = 3]
+
+ Add file 1
+ [git-p4: depot-paths = "//depot/": change = 1]
+
+ EOF
+ git log --format=%B >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_done
diff --git a/t/t9827-git-p4-change-filetype.sh b/t/t9827-git-p4-change-filetype.sh
new file mode 100755
index 0000000..d3670bd
--- /dev/null
+++ b/t/t9827-git-p4-change-filetype.sh
@@ -0,0 +1,62 @@
+#!/bin/sh
+
+test_description='git p4 support for file type change'
+
+. ./lib-git-p4.sh
+
+test_expect_success 'start p4d' '
+ start_p4d
+'
+
+test_expect_success 'create files' '
+ (
+ cd "$cli" &&
+ p4 client -o | sed "/LineEnd/s/:.*/:unix/" | p4 client -i &&
+ cat >file1 <<-EOF &&
+ text without any funny substitution business
+ EOF
+ cat >file2 <<-EOF &&
+ second file whose type will change
+ EOF
+ p4 add file1 file2 &&
+ p4 submit -d "add files"
+ )
+'
+
+test_expect_success SYMLINKS 'change file to symbolic link' '
+ git p4 clone --dest="$git" //depot@all &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git config git-p4.skipSubmitEdit true &&
+
+ rm file2 &&
+ ln -s file1 file2 &&
+ git add file2 &&
+ git commit -m "symlink file1 to file2" &&
+ git p4 submit &&
+ p4 filelog -m 1 //depot/file2 >filelog &&
+ grep "(symlink)" filelog
+ )
+'
+
+test_expect_success SYMLINKS 'change symbolic link to file' '
+ git p4 clone --dest="$git" //depot@all &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git config git-p4.skipSubmitEdit true &&
+
+ rm file2 &&
+ cat >file2 <<-EOF &&
+ This is another content for the second file.
+ EOF
+ git add file2 &&
+ git commit -m "re-write file2" &&
+ git p4 submit &&
+ p4 filelog -m 1 //depot/file2 >filelog &&
+ grep "(text)" filelog
+ )
+'
+
+test_done
diff --git a/t/t9828-git-p4-map-user.sh b/t/t9828-git-p4-map-user.sh
new file mode 100755
index 0000000..ca6c294
--- /dev/null
+++ b/t/t9828-git-p4-map-user.sh
@@ -0,0 +1,57 @@
+#!/bin/sh
+
+test_description='Clone repositories and map users'
+
+. ./lib-git-p4.sh
+
+test_expect_success 'start p4d' '
+ start_p4d
+'
+
+test_expect_success 'Create a repo with different users' '
+ client_view "//depot/... //client/..." &&
+ (
+ cd "$cli" &&
+
+ >author.txt &&
+ p4 add author.txt &&
+ p4 submit -d "Add file author\\n" &&
+
+ P4USER=mmax &&
+ >max.txt &&
+ p4 add max.txt &&
+ p4 submit -d "Add file max" &&
+
+ P4USER=eri &&
+ >moritz.txt &&
+ p4 add moritz.txt &&
+ p4 submit -d "Add file moritz" &&
+
+ P4USER=no &&
+ >nobody.txt &&
+ p4 add nobody.txt &&
+ p4 submit -d "Add file nobody"
+ )
+'
+
+test_expect_success 'Clone repo root path with all history' '
+ client_view "//depot/... //client/..." &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git init . &&
+ git config --add git-p4.mapUser "mmax = Max Musterman <max@example.com> " &&
+ git config --add git-p4.mapUser " eri=Erika Musterman <erika@example.com>" &&
+ git p4 clone --use-client-spec --destination="$git" //depot@all &&
+ cat >expect <<-\EOF &&
+ no <no@client>
+ Erika Musterman <erika@example.com>
+ Max Musterman <max@example.com>
+ Dr. author <author@example.com>
+ EOF
+ git log --format="%an <%ae>" >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_done
diff --git a/t/t9829-git-p4-jobs.sh b/t/t9829-git-p4-jobs.sh
new file mode 100755
index 0000000..88cfb1f
--- /dev/null
+++ b/t/t9829-git-p4-jobs.sh
@@ -0,0 +1,95 @@
+#!/bin/sh
+
+test_description='git p4 retrieve job info'
+
+. ./lib-git-p4.sh
+
+test_expect_success 'start p4d' '
+ start_p4d
+'
+
+test_expect_success 'add p4 jobs' '
+ (
+ p4_add_job TESTJOB-A &&
+ p4_add_job TESTJOB-B
+ )
+'
+
+test_expect_success 'add p4 files' '
+ client_view "//depot/... //client/..." &&
+ (
+ cd "$cli" &&
+ >file1 &&
+ p4 add file1 &&
+ p4 submit -d "Add file 1"
+ )
+'
+
+test_expect_success 'check log message of changelist with no jobs' '
+ client_view "//depot/... //client/..." &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git init . &&
+ git p4 clone --use-client-spec --destination="$git" //depot@all &&
+ cat >expect <<-\EOF &&
+ Add file 1
+ [git-p4: depot-paths = "//depot/": change = 1]
+
+ EOF
+ git log --format=%B >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'add TESTJOB-A to change 1' '
+ (
+ cd "$cli" &&
+ p4 fix -c 1 TESTJOB-A
+ )
+'
+
+test_expect_success 'check log message of changelist with one job' '
+ client_view "//depot/... //client/..." &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git init . &&
+ git p4 clone --use-client-spec --destination="$git" //depot@all &&
+ cat >expect <<-\EOF &&
+ Add file 1
+ Jobs: TESTJOB-A
+ [git-p4: depot-paths = "//depot/": change = 1]
+
+ EOF
+ git log --format=%B >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_expect_success 'add TESTJOB-B to change 1' '
+ (
+ cd "$cli" &&
+ p4 fix -c 1 TESTJOB-B
+ )
+'
+
+test_expect_success 'check log message of changelist with more jobs' '
+ client_view "//depot/... //client/..." &&
+ test_when_finished cleanup_git &&
+ (
+ cd "$git" &&
+ git init . &&
+ git p4 clone --use-client-spec --destination="$git" //depot@all &&
+ cat >expect <<-\EOF &&
+ Add file 1
+ Jobs: TESTJOB-A TESTJOB-B
+ [git-p4: depot-paths = "//depot/": change = 1]
+
+ EOF
+ git log --format=%B >actual &&
+ test_cmp expect actual
+ )
+'
+
+test_done
diff --git a/t/t9830-git-p4-symlink-dir.sh b/t/t9830-git-p4-symlink-dir.sh
new file mode 100755
index 0000000..3fb6960
--- /dev/null
+++ b/t/t9830-git-p4-symlink-dir.sh
@@ -0,0 +1,39 @@
+#!/bin/sh
+
+test_description='git p4 symlinked directories'
+
+. ./lib-git-p4.sh
+
+test_expect_success 'start p4d' '
+ start_p4d
+'
+
+test_expect_success 'symlinked directory' '
+ (
+ cd "$cli" &&
+ : >first_file.t &&
+ p4 add first_file.t &&
+ p4 submit -d "first change"
+ ) &&
+ git p4 clone --dest "$git" //depot &&
+ (
+ cd "$git" &&
+ mkdir -p some/sub/directory &&
+ mkdir -p other/subdir2 &&
+ : > other/subdir2/file.t &&
+ (cd some/sub/directory && ln -s ../../../other/subdir2 .) &&
+ git add some other &&
+ git commit -m "symlinks" &&
+ git config git-p4.skipSubmitEdit true &&
+ git p4 submit -v
+ ) &&
+ (
+ cd "$cli" &&
+ p4 sync &&
+ test -L some/sub/directory/subdir2 &&
+ test_path_is_file some/sub/directory/subdir2/file.t
+ )
+
+'
+
+test_done
diff --git a/t/t9831-git-p4-triggers.sh b/t/t9831-git-p4-triggers.sh
new file mode 100755
index 0000000..ff6c035
--- /dev/null
+++ b/t/t9831-git-p4-triggers.sh
@@ -0,0 +1,99 @@
+#!/bin/sh
+
+test_description='git p4 with server triggers'
+
+. ./lib-git-p4.sh
+
+test_expect_success 'start p4d' '
+ start_p4d
+'
+
+test_expect_success 'init depot' '
+ (
+ cd "$cli" &&
+ echo file1 >file1 &&
+ p4 add file1 &&
+ p4 submit -d "change 1" &&
+ echo file2 >file2 &&
+ p4 add file2 &&
+ p4 submit -d "change 2"
+ )
+'
+
+test_expect_success 'clone with extra info lines from verbose p4 trigger' '
+ test_when_finished cleanup_git &&
+ (
+ p4 triggers -i <<-EOF
+ Triggers: p4triggertest-command command pre-user-change "echo verbose trigger"
+ EOF
+ ) &&
+ (
+ p4 change -o | grep -s "verbose trigger"
+ ) &&
+ git p4 clone --dest="$git" //depot/@all &&
+ (
+ p4 triggers -i <<-EOF
+ Triggers:
+ EOF
+ )
+'
+
+test_expect_success 'import with extra info lines from verbose p4 trigger' '
+ test_when_finished cleanup_git &&
+ (
+ cd "$cli" &&
+ echo file3 >file3 &&
+ p4 add file3 &&
+ p4 submit -d "change 3"
+ ) &&
+ (
+ p4 triggers -i <<-EOF
+ Triggers: p4triggertest-command command pre-user-describe "echo verbose trigger"
+ EOF
+ ) &&
+ (
+ p4 describe 1 | grep -s "verbose trigger"
+ ) &&
+ git p4 clone --dest="$git" //depot/@all &&
+ (
+ cd "$git" &&
+ git p4 sync
+ ) &&
+ (
+ p4 triggers -i <<-EOF
+ Triggers:
+ EOF
+ )
+'
+
+test_expect_success 'submit description with extra info lines from verbose p4 change trigger' '
+ test_when_finished cleanup_git &&
+ (
+ p4 triggers -i <<-EOF
+ Triggers: p4triggertest-command command pre-user-change "echo verbose trigger"
+ EOF
+ ) &&
+ (
+ p4 change -o | grep -s "verbose trigger"
+ ) &&
+ git p4 clone --dest="$git" //depot &&
+ (
+ cd "$git" &&
+ git config git-p4.skipSubmitEdit true &&
+ echo file4 >file4 &&
+ git add file4 &&
+ git commit -m file4 &&
+ git p4 submit
+ ) &&
+ (
+ p4 triggers -i <<-EOF
+ Triggers:
+ EOF
+ ) &&
+ (
+ cd "$cli" &&
+ test_path_is_file file4
+ )
+'
+
+test_done
diff --git a/t/t9832-unshelve.sh b/t/t9832-unshelve.sh
new file mode 100755
index 0000000..6b3cb04
--- /dev/null
+++ b/t/t9832-unshelve.sh
@@ -0,0 +1,188 @@
+#!/bin/sh
+
+last_shelved_change () {
+ p4 changes -s shelved -m1 | cut -d " " -f 2
+}
+
+test_description='git p4 unshelve'
+
+. ./lib-git-p4.sh
+
+test_expect_success 'start p4d' '
+ start_p4d
+'
+
+test_expect_success 'init depot' '
+ (
+ cd "$cli" &&
+ echo file1 >file1 &&
+ p4 add file1 &&
+ p4 submit -d "change 1" &&
+ : >file_to_delete &&
+ : >file_to_move &&
+ p4 add file_to_delete &&
+ p4 add file_to_move &&
+ p4 submit -d "add files to delete" &&
+ echo file_to_integrate >file_to_integrate &&
+ p4 add file_to_integrate &&
+ p4 submit -d "add file to integrate"
+ )
+'
+
+# Create an initial clone, with a commit unrelated to the P4 change
+# on HEAD
+test_expect_success 'initial clone' '
+ git p4 clone --dest="$git" //depot/@all &&
+ test_commit -C "$git" "unrelated"
+'
+
+test_expect_success 'create shelved changelist' '
+ (
+ cd "$cli" &&
+ p4 edit file1 &&
+ echo "a change" >>file1 &&
+ echo "new file" >file2 &&
+ p4 add file2 &&
+ p4 delete file_to_delete &&
+ p4 edit file_to_move &&
+ p4 move file_to_move moved_file &&
+ p4 integrate file_to_integrate integrated_file &&
+ p4 opened &&
+ p4 shelve -i <<EOF
+Change: new
+Description:
+ Test commit
+
+ Further description
+Files:
+ //depot/file1
+ //depot/file2
+ //depot/file_to_delete
+ //depot/file_to_move
+ //depot/moved_file
+ //depot/integrated_file
+EOF
+
+ ) &&
+ (
+ cd "$git" &&
+ change=$(last_shelved_change) &&
+ git p4 unshelve $change &&
+ git show refs/remotes/p4-unshelved/$change >actual &&
+ grep -q "Further description" actual &&
+ git cherry-pick refs/remotes/p4-unshelved/$change &&
+ test_path_is_file file2 &&
+ test_cmp file1 "$cli"/file1 &&
+ test_cmp file2 "$cli"/file2 &&
+ test_cmp file_to_integrate "$cli"/integrated_file &&
+ test_path_is_missing file_to_delete &&
+ test_path_is_missing file_to_move &&
+ test_path_is_file moved_file
+ )
+'
+
+test_expect_success 'update shelved changelist and re-unshelve' '
+ test_when_finished cleanup_git &&
+ (
+ cd "$cli" &&
+ change=$(last_shelved_change) &&
+ echo "file3" >file3 &&
+ p4 add -c $change file3 &&
+ p4 shelve -i -r <<EOF &&
+Change: $change
+Description:
+ Test commit
+
+ Further description
+Files:
+ //depot/file1
+ //depot/file2
+ //depot/file3
+ //depot/file_to_delete
+EOF
+ p4 describe $change
+ ) &&
+ (
+ cd "$git" &&
+ change=$(last_shelved_change) &&
+ git p4 unshelve $change &&
+ git diff refs/remotes/p4-unshelved/$change.0 refs/remotes/p4-unshelved/$change | grep -q file3
+ )
+'
+
+shelve_one_file () {
+ description="Change to be unshelved" &&
+ file="$1" &&
+ p4 shelve -i <<EOF
+Change: new
+Description:
+ $description
+Files:
+ $file
+EOF
+}
+
+# This is the tricky case where the shelved changelist base revision doesn't
+# match git-p4's idea of the base revision
+#
+# We will attempt to unshelve a change that is based on a change one commit
+# ahead of p4/master
+
+test_expect_success 'create shelved changelist based on p4 change ahead of p4/master' '
+ git p4 clone --dest="$git" //depot/@all &&
+ (
+ cd "$cli" &&
+ p4 revert ... &&
+ p4 edit file1 &&
+ echo "foo" >>file1 &&
+ p4 submit -d "change:foo" &&
+ p4 edit file1 &&
+ echo "bar" >>file1 &&
+ shelve_one_file //depot/file1 &&
+ change=$(last_shelved_change) &&
+ p4 describe -S $change >out.txt &&
+ grep -q "Change to be unshelved" out.txt
+ )
+'
+
+# Now try to unshelve it.
+test_expect_success 'try to unshelve the change' '
+ test_when_finished cleanup_git &&
+ (
+ change=$(last_shelved_change) &&
+ cd "$git" &&
+ git p4 unshelve $change >out.txt &&
+ grep -q "unshelved changelist $change" out.txt
+ )
+'
+
+# Specify the origin. Create 2 unrelated files, and check that
+# we only get the one in HEAD~, not the one in HEAD.
+
+test_expect_success 'unshelve specifying the origin' '
+ (
+ cd "$cli" &&
+ : >unrelated_file0 &&
+ p4 add unrelated_file0 &&
+ p4 submit -d "unrelated" &&
+ : >unrelated_file1 &&
+ p4 add unrelated_file1 &&
+ p4 submit -d "unrelated" &&
+ : >file_to_shelve &&
+ p4 add file_to_shelve &&
+ shelve_one_file //depot/file_to_shelve
+ ) &&
+ test_when_finished cleanup_git &&
+ git p4 clone --dest="$git" //depot/@all &&
+ (
+ cd "$git" &&
+ change=$(last_shelved_change) &&
+ git p4 unshelve --origin HEAD~ $change &&
+ git checkout refs/remotes/p4-unshelved/$change &&
+ test_path_is_file unrelated_file0 &&
+ test_path_is_missing unrelated_file1 &&
+ test_path_is_file file_to_shelve
+ )
+'
+
+test_done
diff --git a/t/t9833-errors.sh b/t/t9833-errors.sh
new file mode 100755
index 0000000..e22369c
--- /dev/null
+++ b/t/t9833-errors.sh
@@ -0,0 +1,48 @@
+#!/bin/sh
+
+test_description='git p4 errors'
+
+. ./lib-git-p4.sh
+
+test_expect_success 'start p4d' '
+ start_p4d
+'
+
+test_expect_success 'add p4 files' '
+ (
+ cd "$cli" &&
+ echo file1 >file1 &&
+ p4 add file1 &&
+ p4 submit -d "file1"
+ )
+'
+
+# after this test, the default user requires a password
+test_expect_success 'error handling' '
+ git p4 clone --dest="$git" //depot@all &&
+ (
+ cd "$git" &&
+ P4PORT=: test_must_fail git p4 submit 2>errmsg
+ ) &&
+ p4 passwd -P newpassword &&
+ (
+ P4PASSWD=badpassword &&
+ export P4PASSWD &&
+ test_must_fail git p4 clone //depot/foo 2>errmsg &&
+ grep -q "failure accessing depot.*P4PASSWD" errmsg
+ )
+'
+
+test_expect_success 'ticket logged out' '
+ P4TICKETS="$cli/tickets" &&
+ echo "newpassword" | p4 login &&
+ (
+ cd "$git" &&
+ test_commit "ticket-auth-check" &&
+ p4 logout &&
+ test_must_fail git p4 submit 2>errmsg &&
+ grep -q "failure accessing depot" errmsg
+ )
+'
+
+test_done
diff --git a/t/t9834-git-p4-file-dir-bug.sh b/t/t9834-git-p4-file-dir-bug.sh
new file mode 100755
index 0000000..dac67e8
--- /dev/null
+++ b/t/t9834-git-p4-file-dir-bug.sh
@@ -0,0 +1,70 @@
+#!/bin/sh
+
+test_description='git p4 directory/file bug handling
+
+This test creates files and directories with the same name in perforce and
+checks that git-p4 recovers from the error at the same time as the perforce
+repository.'
+
+. ./lib-git-p4.sh
+
+test_expect_success 'start p4d' '
+ start_p4d &&
+ { p4 configure set submit.collision.check=0 || :; }
+'
+
+test_expect_success 'init depot' '
+ (
+ cd "$cli" &&
+
+ touch add_file_add_dir_del_file add_file_add_dir_del_dir &&
+ p4 add add_file_add_dir_del_file add_file_add_dir_del_dir &&
+ mkdir add_dir_add_file_del_file add_dir_add_file_del_dir &&
+ touch add_dir_add_file_del_file/file add_dir_add_file_del_dir/file &&
+ p4 add add_dir_add_file_del_file/file add_dir_add_file_del_dir/file &&
+ p4 submit -d "add initial" &&
+
+ rm -f add_file_add_dir_del_file add_file_add_dir_del_dir &&
+ mkdir add_file_add_dir_del_file add_file_add_dir_del_dir &&
+ touch add_file_add_dir_del_file/file add_file_add_dir_del_dir/file &&
+ p4 add add_file_add_dir_del_file/file add_file_add_dir_del_dir/file &&
+ rm -rf add_dir_add_file_del_file add_dir_add_file_del_dir &&
+ touch add_dir_add_file_del_file add_dir_add_file_del_dir &&
+ p4 add add_dir_add_file_del_file add_dir_add_file_del_dir &&
+ p4 submit -d "add conflicting" &&
+
+ p4 delete -k add_file_add_dir_del_file &&
+ p4 delete -k add_file_add_dir_del_dir/file &&
+ p4 delete -k add_dir_add_file_del_file &&
+ p4 delete -k add_dir_add_file_del_dir/file &&
+ p4 submit -d "delete conflicting" &&
+
+ p4 delete -k "add_file_add_dir_del_file/file" &&
+ p4 delete -k "add_file_add_dir_del_dir" &&
+ p4 delete -k "add_dir_add_file_del_file/file" &&
+ p4 delete -k "add_dir_add_file_del_dir" &&
+ p4 submit -d "delete remaining"
+ )
+'
+
+test_expect_success 'clone with git-p4' '
+ git p4 clone --dest="$git" //depot/@1,3
+'
+
+test_expect_success 'check contents' '
+ test_path_is_dir "$git/add_file_add_dir_del_file" &&
+ test_path_is_file "$git/add_file_add_dir_del_dir" &&
+ test_path_is_dir "$git/add_dir_add_file_del_file" &&
+ test_path_is_file "$git/add_dir_add_file_del_dir"
+'
+
+test_expect_success 'rebase and check empty' '
+ git -C "$git" p4 rebase &&
+
+ test_path_is_missing "$git/add_file_add_dir_del_file" &&
+ test_path_is_missing "$git/add_file_add_dir_del_dir" &&
+ test_path_is_missing "$git/add_dir_add_file_del_file" &&
+ test_path_is_missing "$git/add_dir_add_file_del_dir"
+'
+
+test_done
diff --git a/t/t9835-git-p4-metadata-encoding-python2.sh b/t/t9835-git-p4-metadata-encoding-python2.sh
new file mode 100755
index 0000000..036bf79
--- /dev/null
+++ b/t/t9835-git-p4-metadata-encoding-python2.sh
@@ -0,0 +1,213 @@
+#!/bin/sh
+
+test_description='git p4 metadata encoding
+
+This test checks that the import process handles inconsistent text
+encoding in p4 metadata (author names, commit messages, etc) without
+failing, and produces maximally sane output in git.'
+
+. ./lib-git-p4.sh
+
+python_target_version='2'
+
+###############################
+## SECTION REPEATED IN t9836 ##
+###############################
+
+# Please note: this test calls "git-p4.py" rather than "git-p4", because the
+# latter references a specific path so we can't easily force it to run under
+# the python version we need to.
+
+python_major_version=$(python -V 2>&1 | cut -c 8)
+python_target_binary=$(which python$python_target_version)
+if ! test "$python_major_version" = "$python_target_version" && test "$python_target_binary"
+then
+ mkdir temp_python
+ PATH="$(pwd)/temp_python:$PATH" && export PATH
+ ln -s $python_target_binary temp_python/python
+fi
+
+python_major_version=$(python -V 2>&1 | cut -c 8)
+if ! test "$python_major_version" = "$python_target_version"
+then
+ skip_all="skipping python$python_target_version-specific git p4 tests; python$python_target_version not available"
+ test_done
+fi
+
+remove_user_cache () {
+ rm "$HOME/.gitp4-usercache.txt" || true
+}
+
+test_expect_success 'start p4d' '
+ start_p4d
+'
+
+test_expect_success 'init depot' '
+ (
+ cd "$cli" &&
+
+ p4_add_user "utf8_author" "Ç£uthor" &&
+ P4USER=utf8_author &&
+ touch file1 &&
+ p4 add file1 &&
+ p4 submit -d "first CL has some utf-8 tǣxt" &&
+
+ p4_add_user "latin1_author" "$(echo æuthor |
+ iconv -f utf8 -t latin1)" &&
+ P4USER=latin1_author &&
+ touch file2 &&
+ p4 add file2 &&
+ p4 submit -d "$(echo second CL has some latin-1 tæxt |
+ iconv -f utf8 -t latin1)" &&
+
+ p4_add_user "cp1252_author" "$(echo æuthœr |
+ iconv -f utf8 -t cp1252)" &&
+ P4USER=cp1252_author &&
+ touch file3 &&
+ p4 add file3 &&
+ p4 submit -d "$(echo third CL has sœme cp-1252 tæxt |
+ iconv -f utf8 -t cp1252)" &&
+
+ p4_add_user "cp850_author" "$(echo Ã…uthor |
+ iconv -f utf8 -t cp850)" &&
+ P4USER=cp850_author &&
+ touch file4 &&
+ p4 add file4 &&
+ p4 submit -d "$(echo fourth CL hÃ…s some cp850 text |
+ iconv -f utf8 -t cp850)"
+ )
+'
+
+test_expect_success 'clone non-utf8 repo with strict encoding' '
+ test_when_finished cleanup_git &&
+ test_when_finished remove_user_cache &&
+ test_must_fail git -c git-p4.metadataDecodingStrategy=strict p4.py clone --dest="$git" //depot@all 2>err &&
+ grep "Decoding perforce metadata failed!" err
+'
+
+test_expect_success 'check utf-8 contents with passthrough strategy' '
+ test_when_finished cleanup_git &&
+ test_when_finished remove_user_cache &&
+ git -c git-p4.metadataDecodingStrategy=passthrough p4.py clone --dest="$git" //depot@all &&
+ (
+ cd "$git" &&
+ git log >actual &&
+ grep "some utf-8 tǣxt" actual &&
+ grep "Ç£uthor" actual
+ )
+'
+
+test_expect_success 'check latin-1 contents corrupted in git with passthrough strategy' '
+ test_when_finished cleanup_git &&
+ test_when_finished remove_user_cache &&
+ git -c git-p4.metadataDecodingStrategy=passthrough p4.py clone --dest="$git" //depot@all &&
+ (
+ cd "$git" &&
+ git log >actual &&
+ badly_encoded_in_git=$(echo "some latin-1 tæxt" | iconv -f utf8 -t latin1) &&
+ grep "$badly_encoded_in_git" actual &&
+ bad_author_in_git="$(echo æuthor | iconv -f utf8 -t latin1)" &&
+ grep "$bad_author_in_git" actual
+ )
+'
+
+test_expect_success 'check utf-8 contents with fallback strategy' '
+ test_when_finished cleanup_git &&
+ test_when_finished remove_user_cache &&
+ git -c git-p4.metadataDecodingStrategy=fallback p4.py clone --dest="$git" //depot@all &&
+ (
+ cd "$git" &&
+ git log >actual &&
+ grep "some utf-8 tǣxt" actual &&
+ grep "Ç£uthor" actual
+ )
+'
+
+test_expect_success 'check latin-1 contents with fallback strategy' '
+ test_when_finished cleanup_git &&
+ test_when_finished remove_user_cache &&
+ git -c git-p4.metadataDecodingStrategy=fallback p4.py clone --dest="$git" //depot@all &&
+ (
+ cd "$git" &&
+ git log >actual &&
+ grep "some latin-1 tæxt" actual &&
+ grep "æuthor" actual
+ )
+'
+
+test_expect_success 'check cp-1252 contents with fallback strategy' '
+ test_when_finished cleanup_git &&
+ test_when_finished remove_user_cache &&
+ git -c git-p4.metadataDecodingStrategy=fallback p4.py clone --dest="$git" //depot@all &&
+ (
+ cd "$git" &&
+ git log >actual &&
+ grep "sœme cp-1252 tæxt" actual &&
+ grep "æuthœr" actual
+ )
+'
+
+test_expect_success 'check cp850 contents parsed with correct fallback' '
+ test_when_finished cleanup_git &&
+ test_when_finished remove_user_cache &&
+ git -c git-p4.metadataDecodingStrategy=fallback -c git-p4.metadataFallbackEncoding=cp850 p4.py clone --dest="$git" //depot@all &&
+ (
+ cd "$git" &&
+ git log >actual &&
+ grep "hÃ…s some cp850 text" actual &&
+ grep "Ã…uthor" actual
+ )
+'
+
+test_expect_success 'check cp850-only contents escaped when cp1252 is fallback' '
+ test_when_finished cleanup_git &&
+ test_when_finished remove_user_cache &&
+ git -c git-p4.metadataDecodingStrategy=fallback p4.py clone --dest="$git" //depot@all &&
+ (
+ cd "$git" &&
+ git log >actual &&
+ grep "h%8Fs some cp850 text" actual &&
+ grep "%8Futhor" actual
+ )
+'
+
+test_expect_success 'check cp-1252 contents on later sync after clone with fallback strategy' '
+ test_when_finished cleanup_git &&
+ test_when_finished remove_user_cache &&
+ git -c git-p4.metadataDecodingStrategy=fallback p4.py clone --dest="$git" //depot@all &&
+ (
+ cd "$cli" &&
+ P4USER=cp1252_author &&
+ touch file10 &&
+ p4 add file10 &&
+ p4 submit -d "$(echo later CL has sœme more cp-1252 tæxt |
+ iconv -f utf8 -t cp1252)"
+ ) &&
+ (
+ cd "$git" &&
+
+ git p4.py sync --branch=master &&
+
+ git log p4/master >actual &&
+ grep "sœme more cp-1252 tæxt" actual &&
+ grep "æuthœr" actual
+ )
+'
+
+############################
+## / END REPEATED SECTION ##
+############################
+
+test_expect_success 'passthrough (latin-1 contents corrupted in git) is the default with python2' '
+ test_when_finished cleanup_git &&
+ test_when_finished remove_user_cache &&
+ git -c git-p4.metadataDecodingStrategy=passthrough p4.py clone --dest="$git" //depot@all &&
+ (
+ cd "$git" &&
+ git log >actual &&
+ badly_encoded_in_git=$(echo "some latin-1 tæxt" | iconv -f utf8 -t latin1) &&
+ grep "$badly_encoded_in_git" actual
+ )
+'
+
+test_done
diff --git a/t/t9836-git-p4-metadata-encoding-python3.sh b/t/t9836-git-p4-metadata-encoding-python3.sh
new file mode 100755
index 0000000..63350dc
--- /dev/null
+++ b/t/t9836-git-p4-metadata-encoding-python3.sh
@@ -0,0 +1,214 @@
+#!/bin/sh
+
+test_description='git p4 metadata encoding
+
+This test checks that the import process handles inconsistent text
+encoding in p4 metadata (author names, commit messages, etc) without
+failing, and produces maximally sane output in git.'
+
+. ./lib-git-p4.sh
+
+python_target_version='3'
+
+###############################
+## SECTION REPEATED IN t9835 ##
+###############################
+
+# Please note: this test calls "git-p4.py" rather than "git-p4", because the
+# latter references a specific path so we can't easily force it to run under
+# the python version we need to.
+
+python_major_version=$(python -V 2>&1 | cut -c 8)
+python_target_binary=$(which python$python_target_version)
+if ! test "$python_major_version" = "$python_target_version" && test "$python_target_binary"
+then
+ mkdir temp_python
+ PATH="$(pwd)/temp_python:$PATH" && export PATH
+ ln -s $python_target_binary temp_python/python
+fi
+
+python_major_version=$(python -V 2>&1 | cut -c 8)
+if ! test "$python_major_version" = "$python_target_version"
+then
+ skip_all="skipping python$python_target_version-specific git p4 tests; python$python_target_version not available"
+ test_done
+fi
+
+remove_user_cache () {
+ rm "$HOME/.gitp4-usercache.txt" || true
+}
+
+test_expect_success 'start p4d' '
+ start_p4d
+'
+
+test_expect_success 'init depot' '
+ (
+ cd "$cli" &&
+
+ p4_add_user "utf8_author" "Ç£uthor" &&
+ P4USER=utf8_author &&
+ touch file1 &&
+ p4 add file1 &&
+ p4 submit -d "first CL has some utf-8 tǣxt" &&
+
+ p4_add_user "latin1_author" "$(echo æuthor |
+ iconv -f utf8 -t latin1)" &&
+ P4USER=latin1_author &&
+ touch file2 &&
+ p4 add file2 &&
+ p4 submit -d "$(echo second CL has some latin-1 tæxt |
+ iconv -f utf8 -t latin1)" &&
+
+ p4_add_user "cp1252_author" "$(echo æuthœr |
+ iconv -f utf8 -t cp1252)" &&
+ P4USER=cp1252_author &&
+ touch file3 &&
+ p4 add file3 &&
+ p4 submit -d "$(echo third CL has sœme cp-1252 tæxt |
+ iconv -f utf8 -t cp1252)" &&
+
+ p4_add_user "cp850_author" "$(echo Ã…uthor |
+ iconv -f utf8 -t cp850)" &&
+ P4USER=cp850_author &&
+ touch file4 &&
+ p4 add file4 &&
+ p4 submit -d "$(echo fourth CL hÃ…s some cp850 text |
+ iconv -f utf8 -t cp850)"
+ )
+'
+
+test_expect_success 'clone non-utf8 repo with strict encoding' '
+ test_when_finished cleanup_git &&
+ test_when_finished remove_user_cache &&
+ test_must_fail git -c git-p4.metadataDecodingStrategy=strict p4.py clone --dest="$git" //depot@all 2>err &&
+ grep "Decoding perforce metadata failed!" err
+'
+
+test_expect_success 'check utf-8 contents with passthrough strategy' '
+ test_when_finished cleanup_git &&
+ test_when_finished remove_user_cache &&
+ git -c git-p4.metadataDecodingStrategy=passthrough p4.py clone --dest="$git" //depot@all &&
+ (
+ cd "$git" &&
+ git log >actual &&
+ grep "some utf-8 tǣxt" actual &&
+ grep "Ç£uthor" actual
+ )
+'
+
+test_expect_success 'check latin-1 contents corrupted in git with passthrough strategy' '
+ test_when_finished cleanup_git &&
+ test_when_finished remove_user_cache &&
+ git -c git-p4.metadataDecodingStrategy=passthrough p4.py clone --dest="$git" //depot@all &&
+ (
+ cd "$git" &&
+ git log >actual &&
+ badly_encoded_in_git=$(echo "some latin-1 tæxt" | iconv -f utf8 -t latin1) &&
+ grep "$badly_encoded_in_git" actual &&
+ bad_author_in_git="$(echo æuthor | iconv -f utf8 -t latin1)" &&
+ grep "$bad_author_in_git" actual
+ )
+'
+
+test_expect_success 'check utf-8 contents with fallback strategy' '
+ test_when_finished cleanup_git &&
+ test_when_finished remove_user_cache &&
+ git -c git-p4.metadataDecodingStrategy=fallback p4.py clone --dest="$git" //depot@all &&
+ (
+ cd "$git" &&
+ git log >actual &&
+ grep "some utf-8 tǣxt" actual &&
+ grep "Ç£uthor" actual
+ )
+'
+
+test_expect_success 'check latin-1 contents with fallback strategy' '
+ test_when_finished cleanup_git &&
+ test_when_finished remove_user_cache &&
+ git -c git-p4.metadataDecodingStrategy=fallback p4.py clone --dest="$git" //depot@all &&
+ (
+ cd "$git" &&
+ git log >actual &&
+ grep "some latin-1 tæxt" actual &&
+ grep "æuthor" actual
+ )
+'
+
+test_expect_success 'check cp-1252 contents with fallback strategy' '
+ test_when_finished cleanup_git &&
+ test_when_finished remove_user_cache &&
+ git -c git-p4.metadataDecodingStrategy=fallback p4.py clone --dest="$git" //depot@all &&
+ (
+ cd "$git" &&
+ git log >actual &&
+ grep "sœme cp-1252 tæxt" actual &&
+ grep "æuthœr" actual
+ )
+'
+
+test_expect_success 'check cp850 contents parsed with correct fallback' '
+ test_when_finished cleanup_git &&
+ test_when_finished remove_user_cache &&
+ git -c git-p4.metadataDecodingStrategy=fallback -c git-p4.metadataFallbackEncoding=cp850 p4.py clone --dest="$git" //depot@all &&
+ (
+ cd "$git" &&
+ git log >actual &&
+ grep "hÃ…s some cp850 text" actual &&
+ grep "Ã…uthor" actual
+ )
+'
+
+test_expect_success 'check cp850-only contents escaped when cp1252 is fallback' '
+ test_when_finished cleanup_git &&
+ test_when_finished remove_user_cache &&
+ git -c git-p4.metadataDecodingStrategy=fallback p4.py clone --dest="$git" //depot@all &&
+ (
+ cd "$git" &&
+ git log >actual &&
+ grep "h%8Fs some cp850 text" actual &&
+ grep "%8Futhor" actual
+ )
+'
+
+test_expect_success 'check cp-1252 contents on later sync after clone with fallback strategy' '
+ test_when_finished cleanup_git &&
+ test_when_finished remove_user_cache &&
+ git -c git-p4.metadataDecodingStrategy=fallback p4.py clone --dest="$git" //depot@all &&
+ (
+ cd "$cli" &&
+ P4USER=cp1252_author &&
+ touch file10 &&
+ p4 add file10 &&
+ p4 submit -d "$(echo later CL has sœme more cp-1252 tæxt |
+ iconv -f utf8 -t cp1252)"
+ ) &&
+ (
+ cd "$git" &&
+
+ git p4.py sync --branch=master &&
+
+ git log p4/master >actual &&
+ grep "sœme more cp-1252 tæxt" actual &&
+ grep "æuthœr" actual
+ )
+'
+
+############################
+## / END REPEATED SECTION ##
+############################
+
+
+test_expect_success 'fallback (both utf-8 and cp-1252 contents handled) is the default with python3' '
+ test_when_finished cleanup_git &&
+ test_when_finished remove_user_cache &&
+ git p4.py clone --dest="$git" //depot@all &&
+ (
+ cd "$git" &&
+ git log >actual &&
+ grep "sœme cp-1252 tæxt" actual &&
+ grep "æuthœr" actual
+ )
+'
+
+test_done
diff --git a/t/t9850-shell.sh b/t/t9850-shell.sh
new file mode 100755
index 0000000..cfc71c3
--- /dev/null
+++ b/t/t9850-shell.sh
@@ -0,0 +1,37 @@
+#!/bin/sh
+
+test_description='git shell tests'
+. ./test-lib.sh
+
+test_expect_success 'shell allows upload-pack' '
+ printf 0000 >input &&
+ git upload-pack . <input >expect &&
+ git shell -c "git-upload-pack $SQ.$SQ" <input >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'shell forbids other commands' '
+ test_must_fail git shell -c "git config foo.bar baz"
+'
+
+test_expect_success 'shell forbids interactive use by default' '
+ test_must_fail git shell
+'
+
+test_expect_success 'shell allows interactive command' '
+ mkdir git-shell-commands &&
+ write_script git-shell-commands/ping <<-\EOF &&
+ echo pong
+ EOF
+ echo pong >expect &&
+ echo ping | git shell >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'shell complains of overlong commands' '
+ perl -e "print \"a\" x 2**12 for (0..2**19)" |
+ test_must_fail git shell 2>err &&
+ grep "too long" err
+'
+
+test_done
diff --git a/t/t9901-git-web--browse.sh b/t/t9901-git-web--browse.sh
new file mode 100755
index 0000000..19f56e5
--- /dev/null
+++ b/t/t9901-git-web--browse.sh
@@ -0,0 +1,64 @@
+#!/bin/sh
+#
+
+test_description='git web--browse basic tests
+
+This test checks that git web--browse can handle various valid URLs.'
+
+TEST_PASSES_SANITIZE_LEAK=true
+. ./test-lib.sh
+
+test_web_browse () {
+ # browser=$1 url=$2
+ git web--browse --browser="$1" "$2" >actual &&
+ tr -d '\015' <actual >text &&
+ test_cmp expect text
+}
+
+test_expect_success \
+ 'URL with an ampersand in it' '
+ echo http://example.com/foo\&bar >expect &&
+ git config browser.custom.cmd echo &&
+ test_web_browse custom http://example.com/foo\&bar
+'
+
+test_expect_success \
+ 'URL with a semi-colon in it' '
+ echo http://example.com/foo\;bar >expect &&
+ git config browser.custom.cmd echo &&
+ test_web_browse custom http://example.com/foo\;bar
+'
+
+test_expect_success \
+ 'URL with a hash in it' '
+ echo http://example.com/foo#bar >expect &&
+ git config browser.custom.cmd echo &&
+ test_web_browse custom http://example.com/foo#bar
+'
+
+test_expect_success \
+ 'browser paths are properly quoted' '
+ echo fake: http://example.com/foo >expect &&
+ cat >"fake browser" <<-\EOF &&
+ #!/bin/sh
+ echo fake: "$@"
+ EOF
+ chmod +x "fake browser" &&
+ git config browser.w3m.path "$(pwd)/fake browser" &&
+ test_web_browse w3m http://example.com/foo
+'
+
+test_expect_success \
+ 'browser command allows arbitrary shell code' '
+ echo "arg: http://example.com/foo" >expect &&
+ git config browser.custom.cmd "
+ f() {
+ for i in \"\$@\"; do
+ echo arg: \$i
+ done
+ }
+ f" &&
+ test_web_browse custom http://example.com/foo
+'
+
+test_done
diff --git a/t/t9902-completion.sh b/t/t9902-completion.sh
new file mode 100755
index 0000000..a7c3b4e
--- /dev/null
+++ b/t/t9902-completion.sh
@@ -0,0 +1,2718 @@
+#!/bin/sh
+#
+# Copyright (c) 2012-2020 Felipe Contreras
+#
+
+test_description='test bash completion'
+
+. ./lib-bash.sh
+
+complete ()
+{
+ # do nothing
+ return 0
+}
+
+# Be careful when updating these lists:
+#
+# (1) The build tree may have build artifact from different branch, or
+# the user's $PATH may have a random executable that may begin
+# with "git-check" that are not part of the subcommands this build
+# will ship, e.g. "check-ignore". The tests for completion for
+# subcommand names tests how "check" is expanded; we limit the
+# possible candidates to "checkout" and "check-attr" to make sure
+# "check-attr", which is known by the filter function as a
+# subcommand to be thrown out, while excluding other random files
+# that happen to begin with "check" to avoid letting them get in
+# the way.
+#
+# (2) A test makes sure that common subcommands are included in the
+# completion for "git <TAB>", and a plumbing is excluded. "add",
+# "rebase" and "ls-files" are listed for this.
+
+GIT_TESTING_ALL_COMMAND_LIST='add checkout check-attr rebase ls-files'
+GIT_TESTING_PORCELAIN_COMMAND_LIST='add checkout rebase'
+
+. "$GIT_BUILD_DIR/contrib/completion/git-completion.bash"
+
+# We don't need this function to actually join words or do anything special.
+# Also, it's cleaner to avoid touching bash's internal completion variables.
+# So let's override it with a minimal version for testing purposes.
+_get_comp_words_by_ref ()
+{
+ while [ $# -gt 0 ]; do
+ case "$1" in
+ cur)
+ cur=${_words[_cword]}
+ ;;
+ prev)
+ prev=${_words[_cword-1]}
+ ;;
+ words)
+ words=("${_words[@]}")
+ ;;
+ cword)
+ cword=$_cword
+ ;;
+ esac
+ shift
+ done
+}
+
+print_comp ()
+{
+ local IFS=$'\n'
+ echo "${COMPREPLY[*]}" > out
+}
+
+run_completion ()
+{
+ local -a COMPREPLY _words
+ local _cword
+ _words=( $1 )
+ test "${1: -1}" = ' ' && _words[${#_words[@]}+1]=''
+ (( _cword = ${#_words[@]} - 1 ))
+ __git_wrap__git_main && print_comp
+}
+
+# Test high-level completion
+# Arguments are:
+# 1: typed text so far (cur)
+# 2: expected completion
+test_completion ()
+{
+ if test $# -gt 1
+ then
+ printf '%s\n' "$2" >expected
+ else
+ sed -e 's/Z$//' |sort >expected
+ fi &&
+ run_completion "$1" &&
+ sort out >out_sorted &&
+ test_cmp expected out_sorted
+}
+
+# Test __gitcomp.
+# The first argument is the typed text so far (cur); the rest are
+# passed to __gitcomp. Expected output comes is read from the
+# standard input, like test_completion().
+test_gitcomp ()
+{
+ local -a COMPREPLY &&
+ sed -e 's/Z$//' >expected &&
+ local cur="$1" &&
+ shift &&
+ __gitcomp "$@" &&
+ print_comp &&
+ test_cmp expected out
+}
+
+# Test __gitcomp_nl
+# Arguments are:
+# 1: current word (cur)
+# -: the rest are passed to __gitcomp_nl
+test_gitcomp_nl ()
+{
+ local -a COMPREPLY &&
+ sed -e 's/Z$//' >expected &&
+ local cur="$1" &&
+ shift &&
+ __gitcomp_nl "$@" &&
+ print_comp &&
+ test_cmp expected out
+}
+
+invalid_variable_name='${foo.bar}'
+
+actual="$TRASH_DIRECTORY/actual"
+
+if test_have_prereq MINGW
+then
+ ROOT="$(pwd -W)"
+else
+ ROOT="$(pwd)"
+fi
+
+test_expect_success 'setup for __git_find_repo_path/__gitdir tests' '
+ mkdir -p subdir/subsubdir &&
+ mkdir -p non-repo &&
+ git init -b main otherrepo
+'
+
+test_expect_success '__git_find_repo_path - from command line (through $__git_dir)' '
+ echo "$ROOT/otherrepo/.git" >expected &&
+ (
+ __git_dir="$ROOT/otherrepo/.git" &&
+ __git_find_repo_path &&
+ echo "$__git_repo_path" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__git_find_repo_path - .git directory in cwd' '
+ echo ".git" >expected &&
+ (
+ __git_find_repo_path &&
+ echo "$__git_repo_path" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__git_find_repo_path - .git directory in parent' '
+ echo "$ROOT/.git" >expected &&
+ (
+ cd subdir/subsubdir &&
+ __git_find_repo_path &&
+ echo "$__git_repo_path" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__git_find_repo_path - cwd is a .git directory' '
+ echo "." >expected &&
+ (
+ cd .git &&
+ __git_find_repo_path &&
+ echo "$__git_repo_path" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__git_find_repo_path - parent is a .git directory' '
+ echo "$ROOT/.git" >expected &&
+ (
+ cd .git/objects &&
+ __git_find_repo_path &&
+ echo "$__git_repo_path" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__git_find_repo_path - $GIT_DIR set while .git directory in cwd' '
+ echo "$ROOT/otherrepo/.git" >expected &&
+ (
+ GIT_DIR="$ROOT/otherrepo/.git" &&
+ export GIT_DIR &&
+ __git_find_repo_path &&
+ echo "$__git_repo_path" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__git_find_repo_path - $GIT_DIR set while .git directory in parent' '
+ echo "$ROOT/otherrepo/.git" >expected &&
+ (
+ GIT_DIR="$ROOT/otherrepo/.git" &&
+ export GIT_DIR &&
+ cd subdir &&
+ __git_find_repo_path &&
+ echo "$__git_repo_path" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__git_find_repo_path - from command line while "git -C"' '
+ echo "$ROOT/.git" >expected &&
+ (
+ __git_dir="$ROOT/.git" &&
+ __git_C_args=(-C otherrepo) &&
+ __git_find_repo_path &&
+ echo "$__git_repo_path" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__git_find_repo_path - relative dir from command line and "git -C"' '
+ echo "$ROOT/otherrepo/.git" >expected &&
+ (
+ cd subdir &&
+ __git_dir="otherrepo/.git" &&
+ __git_C_args=(-C ..) &&
+ __git_find_repo_path &&
+ echo "$__git_repo_path" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__git_find_repo_path - $GIT_DIR set while "git -C"' '
+ echo "$ROOT/.git" >expected &&
+ (
+ GIT_DIR="$ROOT/.git" &&
+ export GIT_DIR &&
+ __git_C_args=(-C otherrepo) &&
+ __git_find_repo_path &&
+ echo "$__git_repo_path" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__git_find_repo_path - relative dir in $GIT_DIR and "git -C"' '
+ echo "$ROOT/otherrepo/.git" >expected &&
+ (
+ cd subdir &&
+ GIT_DIR="otherrepo/.git" &&
+ export GIT_DIR &&
+ __git_C_args=(-C ..) &&
+ __git_find_repo_path &&
+ echo "$__git_repo_path" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__git_find_repo_path - "git -C" while .git directory in cwd' '
+ echo "$ROOT/otherrepo/.git" >expected &&
+ (
+ __git_C_args=(-C otherrepo) &&
+ __git_find_repo_path &&
+ echo "$__git_repo_path" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__git_find_repo_path - "git -C" while cwd is a .git directory' '
+ echo "$ROOT/otherrepo/.git" >expected &&
+ (
+ cd .git &&
+ __git_C_args=(-C .. -C otherrepo) &&
+ __git_find_repo_path &&
+ echo "$__git_repo_path" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__git_find_repo_path - "git -C" while .git directory in parent' '
+ echo "$ROOT/otherrepo/.git" >expected &&
+ (
+ cd subdir &&
+ __git_C_args=(-C .. -C otherrepo) &&
+ __git_find_repo_path &&
+ echo "$__git_repo_path" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__git_find_repo_path - non-existing path in "git -C"' '
+ (
+ __git_C_args=(-C non-existing) &&
+ test_must_fail __git_find_repo_path &&
+ printf "$__git_repo_path" >"$actual"
+ ) &&
+ test_must_be_empty "$actual"
+'
+
+test_expect_success '__git_find_repo_path - non-existing path in $__git_dir' '
+ (
+ __git_dir="non-existing" &&
+ test_must_fail __git_find_repo_path &&
+ printf "$__git_repo_path" >"$actual"
+ ) &&
+ test_must_be_empty "$actual"
+'
+
+test_expect_success '__git_find_repo_path - non-existing $GIT_DIR' '
+ (
+ GIT_DIR="$ROOT/non-existing" &&
+ export GIT_DIR &&
+ test_must_fail __git_find_repo_path &&
+ printf "$__git_repo_path" >"$actual"
+ ) &&
+ test_must_be_empty "$actual"
+'
+
+test_expect_success '__git_find_repo_path - gitfile in cwd' '
+ echo "$ROOT/otherrepo/.git" >expected &&
+ echo "gitdir: $ROOT/otherrepo/.git" >subdir/.git &&
+ test_when_finished "rm -f subdir/.git" &&
+ (
+ cd subdir &&
+ __git_find_repo_path &&
+ echo "$__git_repo_path" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__git_find_repo_path - gitfile in parent' '
+ echo "$ROOT/otherrepo/.git" >expected &&
+ echo "gitdir: $ROOT/otherrepo/.git" >subdir/.git &&
+ test_when_finished "rm -f subdir/.git" &&
+ (
+ cd subdir/subsubdir &&
+ __git_find_repo_path &&
+ echo "$__git_repo_path" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success SYMLINKS '__git_find_repo_path - resulting path avoids symlinks' '
+ echo "$ROOT/otherrepo/.git" >expected &&
+ mkdir otherrepo/dir &&
+ test_when_finished "rm -rf otherrepo/dir" &&
+ ln -s otherrepo/dir link &&
+ test_when_finished "rm -f link" &&
+ (
+ cd link &&
+ __git_find_repo_path &&
+ echo "$__git_repo_path" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__git_find_repo_path - not a git repository' '
+ (
+ cd non-repo &&
+ GIT_CEILING_DIRECTORIES="$ROOT" &&
+ export GIT_CEILING_DIRECTORIES &&
+ test_must_fail __git_find_repo_path &&
+ printf "$__git_repo_path" >"$actual"
+ ) &&
+ test_must_be_empty "$actual"
+'
+
+test_expect_success '__gitdir - finds repo' '
+ echo "$ROOT/.git" >expected &&
+ (
+ cd subdir/subsubdir &&
+ __gitdir >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+
+test_expect_success '__gitdir - returns error when cannot find repo' '
+ (
+ __git_dir="non-existing" &&
+ test_must_fail __gitdir >"$actual"
+ ) &&
+ test_must_be_empty "$actual"
+'
+
+test_expect_success '__gitdir - repo as argument' '
+ echo "otherrepo/.git" >expected &&
+ (
+ __gitdir "otherrepo" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__gitdir - remote as argument' '
+ echo "remote" >expected &&
+ (
+ __gitdir "remote" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+
+test_expect_success '__git_dequote - plain unquoted word' '
+ __git_dequote unquoted-word &&
+ test unquoted-word = "$dequoted_word"
+'
+
+# input: b\a\c\k\'\\\"s\l\a\s\h\es
+# expected: back'\"slashes
+test_expect_success '__git_dequote - backslash escaped' '
+ __git_dequote "b\a\c\k\\'\''\\\\\\\"s\l\a\s\h\es" &&
+ test "back'\''\\\"slashes" = "$dequoted_word"
+'
+
+# input: sin'gle\' '"quo'ted
+# expected: single\ "quoted
+test_expect_success '__git_dequote - single quoted' '
+ __git_dequote "'"sin'gle\\\\' '\\\"quo'ted"'" &&
+ test '\''single\ "quoted'\'' = "$dequoted_word"
+'
+
+# input: dou"ble\\" "\"\quot"ed
+# expected: double\ "\quoted
+test_expect_success '__git_dequote - double quoted' '
+ __git_dequote '\''dou"ble\\" "\"\quot"ed'\'' &&
+ test '\''double\ "\quoted'\'' = "$dequoted_word"
+'
+
+# input: 'open single quote
+test_expect_success '__git_dequote - open single quote' '
+ __git_dequote "'\''open single quote" &&
+ test "open single quote" = "$dequoted_word"
+'
+
+# input: "open double quote
+test_expect_success '__git_dequote - open double quote' '
+ __git_dequote "\"open double quote" &&
+ test "open double quote" = "$dequoted_word"
+'
+
+
+test_expect_success '__gitcomp_direct - puts everything into COMPREPLY as-is' '
+ sed -e "s/Z$//g" >expected <<-EOF &&
+ with-trailing-space Z
+ without-trailing-spaceZ
+ --option Z
+ --option=Z
+ $invalid_variable_name Z
+ EOF
+ (
+ cur=should_be_ignored &&
+ __gitcomp_direct "$(cat expected)" &&
+ print_comp
+ ) &&
+ test_cmp expected out
+'
+
+test_expect_success '__gitcomp - trailing space - options' '
+ test_gitcomp "--re" "--dry-run --reuse-message= --reedit-message=
+ --reset-author" <<-EOF
+ --reuse-message=Z
+ --reedit-message=Z
+ --reset-author Z
+ EOF
+'
+
+test_expect_success '__gitcomp - trailing space - config keys' '
+ test_gitcomp "br" "branch. branch.autosetupmerge
+ branch.autosetuprebase browser." <<-\EOF
+ branch.Z
+ branch.autosetupmerge Z
+ branch.autosetuprebase Z
+ browser.Z
+ EOF
+'
+
+test_expect_success '__gitcomp - option parameter' '
+ test_gitcomp "--strategy=re" "octopus ours recursive resolve subtree" \
+ "" "re" <<-\EOF
+ recursive Z
+ resolve Z
+ EOF
+'
+
+test_expect_success '__gitcomp - prefix' '
+ test_gitcomp "branch.me" "remote merge mergeoptions rebase" \
+ "branch.maint." "me" <<-\EOF
+ branch.maint.merge Z
+ branch.maint.mergeoptions Z
+ EOF
+'
+
+test_expect_success '__gitcomp - suffix' '
+ test_gitcomp "branch.me" "master maint next seen" "branch." \
+ "ma" "." <<-\EOF
+ branch.master.Z
+ branch.maint.Z
+ EOF
+'
+
+test_expect_success '__gitcomp - ignore optional negative options' '
+ test_gitcomp "--" "--abc --def --no-one -- --no-two" <<-\EOF
+ --abc Z
+ --def Z
+ --no-one Z
+ --no-... Z
+ EOF
+'
+
+test_expect_success '__gitcomp - ignore/narrow optional negative options' '
+ test_gitcomp "--a" "--abc --abcdef --no-one -- --no-two" <<-\EOF
+ --abc Z
+ --abcdef Z
+ EOF
+'
+
+test_expect_success '__gitcomp - ignore/narrow optional negative options' '
+ test_gitcomp "--n" "--abc --def --no-one -- --no-two" <<-\EOF
+ --no-one Z
+ --no-... Z
+ EOF
+'
+
+test_expect_success '__gitcomp - expand all negative options' '
+ test_gitcomp "--no-" "--abc --def --no-one -- --no-two" <<-\EOF
+ --no-one Z
+ --no-two Z
+ EOF
+'
+
+test_expect_success '__gitcomp - expand/narrow all negative options' '
+ test_gitcomp "--no-o" "--abc --def --no-one -- --no-two" <<-\EOF
+ --no-one Z
+ EOF
+'
+
+test_expect_success '__gitcomp - equal skip' '
+ test_gitcomp "--option=" "--option=" <<-\EOF &&
+
+ EOF
+ test_gitcomp "option=" "option=" <<-\EOF
+
+ EOF
+'
+
+test_expect_success '__gitcomp - doesnt fail because of invalid variable name' '
+ __gitcomp "$invalid_variable_name"
+'
+
+read -r -d "" refs <<-\EOF
+main
+maint
+next
+seen
+EOF
+
+test_expect_success '__gitcomp_nl - trailing space' '
+ test_gitcomp_nl "m" "$refs" <<-EOF
+ main Z
+ maint Z
+ EOF
+'
+
+test_expect_success '__gitcomp_nl - prefix' '
+ test_gitcomp_nl "--fixup=m" "$refs" "--fixup=" "m" <<-EOF
+ --fixup=main Z
+ --fixup=maint Z
+ EOF
+'
+
+test_expect_success '__gitcomp_nl - suffix' '
+ test_gitcomp_nl "branch.ma" "$refs" "branch." "ma" "." <<-\EOF
+ branch.main.Z
+ branch.maint.Z
+ EOF
+'
+
+test_expect_success '__gitcomp_nl - no suffix' '
+ test_gitcomp_nl "ma" "$refs" "" "ma" "" <<-\EOF
+ mainZ
+ maintZ
+ EOF
+'
+
+test_expect_success '__gitcomp_nl - doesnt fail because of invalid variable name' '
+ __gitcomp_nl "$invalid_variable_name"
+'
+
+test_expect_success '__git_remotes - list remotes from $GIT_DIR/remotes and from config file' '
+ cat >expect <<-EOF &&
+ remote_from_file_1
+ remote_from_file_2
+ remote_in_config_1
+ remote_in_config_2
+ EOF
+ test_when_finished "rm -rf .git/remotes" &&
+ mkdir -p .git/remotes &&
+ >.git/remotes/remote_from_file_1 &&
+ >.git/remotes/remote_from_file_2 &&
+ test_when_finished "git remote remove remote_in_config_1" &&
+ git remote add remote_in_config_1 git://remote_1 &&
+ test_when_finished "git remote remove remote_in_config_2" &&
+ git remote add remote_in_config_2 git://remote_2 &&
+ (
+ __git_remotes >actual
+ ) &&
+ test_cmp expect actual
+'
+
+test_expect_success '__git_is_configured_remote' '
+ test_when_finished "git remote remove remote_1" &&
+ git remote add remote_1 git://remote_1 &&
+ test_when_finished "git remote remove remote_2" &&
+ git remote add remote_2 git://remote_2 &&
+ (
+ __git_is_configured_remote remote_2 &&
+ test_must_fail __git_is_configured_remote non-existent
+ )
+'
+
+test_expect_success 'setup for ref completion' '
+ git commit --allow-empty -m initial &&
+ git branch -M main &&
+ git branch matching-branch &&
+ git tag matching-tag &&
+ (
+ cd otherrepo &&
+ git commit --allow-empty -m initial &&
+ git branch -m main main-in-other &&
+ git branch branch-in-other &&
+ git tag tag-in-other
+ ) &&
+ git remote add other "$ROOT/otherrepo/.git" &&
+ git fetch --no-tags other &&
+ rm -f .git/FETCH_HEAD &&
+ git init thirdrepo
+'
+
+test_expect_success '__git_refs - simple' '
+ cat >expected <<-EOF &&
+ HEAD
+ main
+ matching-branch
+ other/branch-in-other
+ other/main-in-other
+ matching-tag
+ EOF
+ (
+ cur= &&
+ __git_refs >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__git_refs - full refs' '
+ cat >expected <<-EOF &&
+ refs/heads/main
+ refs/heads/matching-branch
+ refs/remotes/other/branch-in-other
+ refs/remotes/other/main-in-other
+ refs/tags/matching-tag
+ EOF
+ (
+ cur=refs/heads/ &&
+ __git_refs >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__git_refs - repo given on the command line' '
+ cat >expected <<-EOF &&
+ HEAD
+ branch-in-other
+ main-in-other
+ tag-in-other
+ EOF
+ (
+ __git_dir="$ROOT/otherrepo/.git" &&
+ cur= &&
+ __git_refs >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__git_refs - remote on local file system' '
+ cat >expected <<-EOF &&
+ HEAD
+ branch-in-other
+ main-in-other
+ tag-in-other
+ EOF
+ (
+ cur= &&
+ __git_refs otherrepo >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__git_refs - remote on local file system - full refs' '
+ cat >expected <<-EOF &&
+ refs/heads/branch-in-other
+ refs/heads/main-in-other
+ refs/tags/tag-in-other
+ EOF
+ (
+ cur=refs/ &&
+ __git_refs otherrepo >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__git_refs - configured remote' '
+ cat >expected <<-EOF &&
+ HEAD
+ branch-in-other
+ main-in-other
+ EOF
+ (
+ cur= &&
+ __git_refs other >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__git_refs - configured remote - full refs' '
+ cat >expected <<-EOF &&
+ HEAD
+ refs/heads/branch-in-other
+ refs/heads/main-in-other
+ refs/tags/tag-in-other
+ EOF
+ (
+ cur=refs/ &&
+ __git_refs other >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__git_refs - configured remote - repo given on the command line' '
+ cat >expected <<-EOF &&
+ HEAD
+ branch-in-other
+ main-in-other
+ EOF
+ (
+ cd thirdrepo &&
+ __git_dir="$ROOT/.git" &&
+ cur= &&
+ __git_refs other >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__git_refs - configured remote - full refs - repo given on the command line' '
+ cat >expected <<-EOF &&
+ HEAD
+ refs/heads/branch-in-other
+ refs/heads/main-in-other
+ refs/tags/tag-in-other
+ EOF
+ (
+ cd thirdrepo &&
+ __git_dir="$ROOT/.git" &&
+ cur=refs/ &&
+ __git_refs other >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__git_refs - configured remote - remote name matches a directory' '
+ cat >expected <<-EOF &&
+ HEAD
+ branch-in-other
+ main-in-other
+ EOF
+ mkdir other &&
+ test_when_finished "rm -rf other" &&
+ (
+ cur= &&
+ __git_refs other >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__git_refs - URL remote' '
+ cat >expected <<-EOF &&
+ HEAD
+ branch-in-other
+ main-in-other
+ tag-in-other
+ EOF
+ (
+ cur= &&
+ __git_refs "file://$ROOT/otherrepo/.git" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__git_refs - URL remote - full refs' '
+ cat >expected <<-EOF &&
+ HEAD
+ refs/heads/branch-in-other
+ refs/heads/main-in-other
+ refs/tags/tag-in-other
+ EOF
+ (
+ cur=refs/ &&
+ __git_refs "file://$ROOT/otherrepo/.git" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__git_refs - non-existing remote' '
+ (
+ cur= &&
+ __git_refs non-existing >"$actual"
+ ) &&
+ test_must_be_empty "$actual"
+'
+
+test_expect_success '__git_refs - non-existing remote - full refs' '
+ (
+ cur=refs/ &&
+ __git_refs non-existing >"$actual"
+ ) &&
+ test_must_be_empty "$actual"
+'
+
+test_expect_success '__git_refs - non-existing URL remote' '
+ (
+ cur= &&
+ __git_refs "file://$ROOT/non-existing" >"$actual"
+ ) &&
+ test_must_be_empty "$actual"
+'
+
+test_expect_success '__git_refs - non-existing URL remote - full refs' '
+ (
+ cur=refs/ &&
+ __git_refs "file://$ROOT/non-existing" >"$actual"
+ ) &&
+ test_must_be_empty "$actual"
+'
+
+test_expect_success '__git_refs - not in a git repository' '
+ (
+ GIT_CEILING_DIRECTORIES="$ROOT" &&
+ export GIT_CEILING_DIRECTORIES &&
+ cd subdir &&
+ cur= &&
+ __git_refs >"$actual"
+ ) &&
+ test_must_be_empty "$actual"
+'
+
+test_expect_success '__git_refs - unique remote branches for git checkout DWIMery' '
+ cat >expected <<-EOF &&
+ HEAD
+ main
+ matching-branch
+ other/ambiguous
+ other/branch-in-other
+ other/main-in-other
+ remote/ambiguous
+ remote/branch-in-remote
+ matching-tag
+ branch-in-other
+ branch-in-remote
+ main-in-other
+ EOF
+ for remote_ref in refs/remotes/other/ambiguous \
+ refs/remotes/remote/ambiguous \
+ refs/remotes/remote/branch-in-remote
+ do
+ git update-ref $remote_ref main &&
+ test_when_finished "git update-ref -d $remote_ref" || return 1
+ done &&
+ (
+ cur= &&
+ __git_refs "" 1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__git_refs - after --opt=' '
+ cat >expected <<-EOF &&
+ HEAD
+ main
+ matching-branch
+ other/branch-in-other
+ other/main-in-other
+ matching-tag
+ EOF
+ (
+ cur="--opt=" &&
+ __git_refs "" "" "" "" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__git_refs - after --opt= - full refs' '
+ cat >expected <<-EOF &&
+ refs/heads/main
+ refs/heads/matching-branch
+ refs/remotes/other/branch-in-other
+ refs/remotes/other/main-in-other
+ refs/tags/matching-tag
+ EOF
+ (
+ cur="--opt=refs/" &&
+ __git_refs "" "" "" refs/ >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__git refs - excluding refs' '
+ cat >expected <<-EOF &&
+ ^HEAD
+ ^main
+ ^matching-branch
+ ^other/branch-in-other
+ ^other/main-in-other
+ ^matching-tag
+ EOF
+ (
+ cur=^ &&
+ __git_refs >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__git refs - excluding full refs' '
+ cat >expected <<-EOF &&
+ ^refs/heads/main
+ ^refs/heads/matching-branch
+ ^refs/remotes/other/branch-in-other
+ ^refs/remotes/other/main-in-other
+ ^refs/tags/matching-tag
+ EOF
+ (
+ cur=^refs/ &&
+ __git_refs >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'setup for filtering matching refs' '
+ git branch matching/branch &&
+ git tag matching/tag &&
+ git -C otherrepo branch matching/branch-in-other &&
+ git fetch --no-tags other &&
+ rm -f .git/FETCH_HEAD
+'
+
+test_expect_success '__git_refs - do not filter refs unless told so' '
+ cat >expected <<-EOF &&
+ HEAD
+ main
+ matching-branch
+ matching/branch
+ other/branch-in-other
+ other/main-in-other
+ other/matching/branch-in-other
+ matching-tag
+ matching/tag
+ EOF
+ (
+ cur=main &&
+ __git_refs >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__git_refs - only matching refs' '
+ cat >expected <<-EOF &&
+ matching-branch
+ matching/branch
+ matching-tag
+ matching/tag
+ EOF
+ (
+ cur=mat &&
+ __git_refs "" "" "" "$cur" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__git_refs - only matching refs - full refs' '
+ cat >expected <<-EOF &&
+ refs/heads/matching-branch
+ refs/heads/matching/branch
+ EOF
+ (
+ cur=refs/heads/mat &&
+ __git_refs "" "" "" "$cur" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__git_refs - only matching refs - remote on local file system' '
+ cat >expected <<-EOF &&
+ main-in-other
+ matching/branch-in-other
+ EOF
+ (
+ cur=ma &&
+ __git_refs otherrepo "" "" "$cur" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__git_refs - only matching refs - configured remote' '
+ cat >expected <<-EOF &&
+ main-in-other
+ matching/branch-in-other
+ EOF
+ (
+ cur=ma &&
+ __git_refs other "" "" "$cur" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__git_refs - only matching refs - remote - full refs' '
+ cat >expected <<-EOF &&
+ refs/heads/main-in-other
+ refs/heads/matching/branch-in-other
+ EOF
+ (
+ cur=refs/heads/ma &&
+ __git_refs other "" "" "$cur" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__git_refs - only matching refs - checkout DWIMery' '
+ cat >expected <<-EOF &&
+ matching-branch
+ matching/branch
+ matching-tag
+ matching/tag
+ matching/branch-in-other
+ EOF
+ for remote_ref in refs/remotes/other/ambiguous \
+ refs/remotes/remote/ambiguous \
+ refs/remotes/remote/branch-in-remote
+ do
+ git update-ref $remote_ref main &&
+ test_when_finished "git update-ref -d $remote_ref" || return 1
+ done &&
+ (
+ cur=mat &&
+ __git_refs "" 1 "" "$cur" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'teardown after filtering matching refs' '
+ git branch -d matching/branch &&
+ git tag -d matching/tag &&
+ git update-ref -d refs/remotes/other/matching/branch-in-other &&
+ git -C otherrepo branch -D matching/branch-in-other
+'
+
+test_expect_success '__git_refs - for-each-ref format specifiers in prefix' '
+ cat >expected <<-EOF &&
+ evil-%%-%42-%(refname)..main
+ EOF
+ (
+ cur="evil-%%-%42-%(refname)..mai" &&
+ __git_refs "" "" "evil-%%-%42-%(refname).." mai >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success '__git_complete_refs - simple' '
+ sed -e "s/Z$//" >expected <<-EOF &&
+ HEAD Z
+ main Z
+ matching-branch Z
+ other/branch-in-other Z
+ other/main-in-other Z
+ matching-tag Z
+ EOF
+ (
+ cur= &&
+ __git_complete_refs &&
+ print_comp
+ ) &&
+ test_cmp expected out
+'
+
+test_expect_success '__git_complete_refs - matching' '
+ sed -e "s/Z$//" >expected <<-EOF &&
+ matching-branch Z
+ matching-tag Z
+ EOF
+ (
+ cur=mat &&
+ __git_complete_refs &&
+ print_comp
+ ) &&
+ test_cmp expected out
+'
+
+test_expect_success '__git_complete_refs - remote' '
+ sed -e "s/Z$//" >expected <<-EOF &&
+ HEAD Z
+ branch-in-other Z
+ main-in-other Z
+ EOF
+ (
+ cur= &&
+ __git_complete_refs --remote=other &&
+ print_comp
+ ) &&
+ test_cmp expected out
+'
+
+test_expect_success '__git_complete_refs - track' '
+ sed -e "s/Z$//" >expected <<-EOF &&
+ HEAD Z
+ main Z
+ matching-branch Z
+ other/branch-in-other Z
+ other/main-in-other Z
+ matching-tag Z
+ branch-in-other Z
+ main-in-other Z
+ EOF
+ (
+ cur= &&
+ __git_complete_refs --track &&
+ print_comp
+ ) &&
+ test_cmp expected out
+'
+
+test_expect_success '__git_complete_refs - current word' '
+ sed -e "s/Z$//" >expected <<-EOF &&
+ matching-branch Z
+ matching-tag Z
+ EOF
+ (
+ cur="--option=mat" &&
+ __git_complete_refs --cur="${cur#*=}" &&
+ print_comp
+ ) &&
+ test_cmp expected out
+'
+
+test_expect_success '__git_complete_refs - prefix' '
+ sed -e "s/Z$//" >expected <<-EOF &&
+ v1.0..matching-branch Z
+ v1.0..matching-tag Z
+ EOF
+ (
+ cur=v1.0..mat &&
+ __git_complete_refs --pfx=v1.0.. --cur=mat &&
+ print_comp
+ ) &&
+ test_cmp expected out
+'
+
+test_expect_success '__git_complete_refs - suffix' '
+ cat >expected <<-EOF &&
+ HEAD.
+ main.
+ matching-branch.
+ other/branch-in-other.
+ other/main-in-other.
+ matching-tag.
+ EOF
+ (
+ cur= &&
+ __git_complete_refs --sfx=. &&
+ print_comp
+ ) &&
+ test_cmp expected out
+'
+
+test_expect_success '__git_complete_fetch_refspecs - simple' '
+ sed -e "s/Z$//" >expected <<-EOF &&
+ HEAD:HEAD Z
+ branch-in-other:branch-in-other Z
+ main-in-other:main-in-other Z
+ EOF
+ (
+ cur= &&
+ __git_complete_fetch_refspecs other &&
+ print_comp
+ ) &&
+ test_cmp expected out
+'
+
+test_expect_success '__git_complete_fetch_refspecs - matching' '
+ sed -e "s/Z$//" >expected <<-EOF &&
+ branch-in-other:branch-in-other Z
+ EOF
+ (
+ cur=br &&
+ __git_complete_fetch_refspecs other "" br &&
+ print_comp
+ ) &&
+ test_cmp expected out
+'
+
+test_expect_success '__git_complete_fetch_refspecs - prefix' '
+ sed -e "s/Z$//" >expected <<-EOF &&
+ +HEAD:HEAD Z
+ +branch-in-other:branch-in-other Z
+ +main-in-other:main-in-other Z
+ EOF
+ (
+ cur="+" &&
+ __git_complete_fetch_refspecs other "+" "" &&
+ print_comp
+ ) &&
+ test_cmp expected out
+'
+
+test_expect_success '__git_complete_fetch_refspecs - fully qualified' '
+ sed -e "s/Z$//" >expected <<-EOF &&
+ refs/heads/branch-in-other:refs/heads/branch-in-other Z
+ refs/heads/main-in-other:refs/heads/main-in-other Z
+ refs/tags/tag-in-other:refs/tags/tag-in-other Z
+ EOF
+ (
+ cur=refs/ &&
+ __git_complete_fetch_refspecs other "" refs/ &&
+ print_comp
+ ) &&
+ test_cmp expected out
+'
+
+test_expect_success '__git_complete_fetch_refspecs - fully qualified & prefix' '
+ sed -e "s/Z$//" >expected <<-EOF &&
+ +refs/heads/branch-in-other:refs/heads/branch-in-other Z
+ +refs/heads/main-in-other:refs/heads/main-in-other Z
+ +refs/tags/tag-in-other:refs/tags/tag-in-other Z
+ EOF
+ (
+ cur=+refs/ &&
+ __git_complete_fetch_refspecs other + refs/ &&
+ print_comp
+ ) &&
+ test_cmp expected out
+'
+
+test_expect_success 'git switch - with no options, complete local branches and unique remote branch names for DWIM logic' '
+ test_completion "git switch " <<-\EOF
+ branch-in-other Z
+ main Z
+ main-in-other Z
+ matching-branch Z
+ EOF
+'
+
+test_expect_success 'git checkout - completes refs and unique remote branches for DWIM' '
+ test_completion "git checkout " <<-\EOF
+ HEAD Z
+ branch-in-other Z
+ main Z
+ main-in-other Z
+ matching-branch Z
+ matching-tag Z
+ other/branch-in-other Z
+ other/main-in-other Z
+ EOF
+'
+
+test_expect_success 'git switch - with --no-guess, complete only local branches' '
+ test_completion "git switch --no-guess " <<-\EOF
+ main Z
+ matching-branch Z
+ EOF
+'
+
+test_expect_success 'git switch - with GIT_COMPLETION_CHECKOUT_NO_GUESS=1, complete only local branches' '
+ GIT_COMPLETION_CHECKOUT_NO_GUESS=1 test_completion "git switch " <<-\EOF
+ main Z
+ matching-branch Z
+ EOF
+'
+
+test_expect_success 'git switch - --guess overrides GIT_COMPLETION_CHECKOUT_NO_GUESS=1, complete local branches and unique remote names for DWIM logic' '
+ GIT_COMPLETION_CHECKOUT_NO_GUESS=1 test_completion "git switch --guess " <<-\EOF
+ branch-in-other Z
+ main Z
+ main-in-other Z
+ matching-branch Z
+ EOF
+'
+
+test_expect_success 'git switch - a later --guess overrides previous --no-guess, complete local and remote unique branches for DWIM' '
+ test_completion "git switch --no-guess --guess " <<-\EOF
+ branch-in-other Z
+ main Z
+ main-in-other Z
+ matching-branch Z
+ EOF
+'
+
+test_expect_success 'git switch - a later --no-guess overrides previous --guess, complete only local branches' '
+ test_completion "git switch --guess --no-guess " <<-\EOF
+ main Z
+ matching-branch Z
+ EOF
+'
+
+test_expect_success 'git checkout - with GIT_COMPLETION_NO_GUESS=1 only completes refs' '
+ GIT_COMPLETION_CHECKOUT_NO_GUESS=1 test_completion "git checkout " <<-\EOF
+ HEAD Z
+ main Z
+ matching-branch Z
+ matching-tag Z
+ other/branch-in-other Z
+ other/main-in-other Z
+ EOF
+'
+
+test_expect_success 'git checkout - --guess overrides GIT_COMPLETION_NO_GUESS=1, complete refs and unique remote branches for DWIM' '
+ GIT_COMPLETION_CHECKOUT_NO_GUESS=1 test_completion "git checkout --guess " <<-\EOF
+ HEAD Z
+ branch-in-other Z
+ main Z
+ main-in-other Z
+ matching-branch Z
+ matching-tag Z
+ other/branch-in-other Z
+ other/main-in-other Z
+ EOF
+'
+
+test_expect_success 'git checkout - with --no-guess, only completes refs' '
+ test_completion "git checkout --no-guess " <<-\EOF
+ HEAD Z
+ main Z
+ matching-branch Z
+ matching-tag Z
+ other/branch-in-other Z
+ other/main-in-other Z
+ EOF
+'
+
+test_expect_success 'git checkout - a later --guess overrides previous --no-guess, complete refs and unique remote branches for DWIM' '
+ test_completion "git checkout --no-guess --guess " <<-\EOF
+ HEAD Z
+ branch-in-other Z
+ main Z
+ main-in-other Z
+ matching-branch Z
+ matching-tag Z
+ other/branch-in-other Z
+ other/main-in-other Z
+ EOF
+'
+
+test_expect_success 'git checkout - a later --no-guess overrides previous --guess, complete only refs' '
+ test_completion "git checkout --guess --no-guess " <<-\EOF
+ HEAD Z
+ main Z
+ matching-branch Z
+ matching-tag Z
+ other/branch-in-other Z
+ other/main-in-other Z
+ EOF
+'
+
+test_expect_success 'git checkout - with checkout.guess = false, only completes refs' '
+ test_config checkout.guess false &&
+ test_completion "git checkout " <<-\EOF
+ HEAD Z
+ main Z
+ matching-branch Z
+ matching-tag Z
+ other/branch-in-other Z
+ other/main-in-other Z
+ EOF
+'
+
+test_expect_success 'git checkout - with checkout.guess = true, completes refs and unique remote branches for DWIM' '
+ test_config checkout.guess true &&
+ test_completion "git checkout " <<-\EOF
+ HEAD Z
+ branch-in-other Z
+ main Z
+ main-in-other Z
+ matching-branch Z
+ matching-tag Z
+ other/branch-in-other Z
+ other/main-in-other Z
+ EOF
+'
+
+test_expect_success 'git checkout - a later --guess overrides previous checkout.guess = false, complete refs and unique remote branches for DWIM' '
+ test_config checkout.guess false &&
+ test_completion "git checkout --guess " <<-\EOF
+ HEAD Z
+ branch-in-other Z
+ main Z
+ main-in-other Z
+ matching-branch Z
+ matching-tag Z
+ other/branch-in-other Z
+ other/main-in-other Z
+ EOF
+'
+
+test_expect_success 'git checkout - a later --no-guess overrides previous checkout.guess = true, complete only refs' '
+ test_config checkout.guess true &&
+ test_completion "git checkout --no-guess " <<-\EOF
+ HEAD Z
+ main Z
+ matching-branch Z
+ matching-tag Z
+ other/branch-in-other Z
+ other/main-in-other Z
+ EOF
+'
+
+test_expect_success 'git switch - with --detach, complete all references' '
+ test_completion "git switch --detach " <<-\EOF
+ HEAD Z
+ main Z
+ matching-branch Z
+ matching-tag Z
+ other/branch-in-other Z
+ other/main-in-other Z
+ EOF
+'
+
+test_expect_success 'git checkout - with --detach, complete only references' '
+ test_completion "git checkout --detach " <<-\EOF
+ HEAD Z
+ main Z
+ matching-branch Z
+ matching-tag Z
+ other/branch-in-other Z
+ other/main-in-other Z
+ EOF
+'
+
+test_expect_success 'setup sparse-checkout tests' '
+ # set up sparse-checkout repo
+ git init sparse-checkout &&
+ (
+ cd sparse-checkout &&
+ mkdir -p folder1/0/1 folder2/0 folder3 &&
+ touch folder1/0/1/t.txt &&
+ touch folder2/0/t.txt &&
+ touch folder3/t.txt &&
+ git add . &&
+ git commit -am "Initial commit"
+ )
+'
+
+test_expect_success 'sparse-checkout completes subcommands' '
+ test_completion "git sparse-checkout " <<-\EOF
+ list Z
+ init Z
+ set Z
+ add Z
+ reapply Z
+ disable Z
+ EOF
+'
+
+test_expect_success 'cone mode sparse-checkout completes directory names' '
+ # initialize sparse-checkout definitions
+ git -C sparse-checkout sparse-checkout set --cone folder1/0 folder3 &&
+
+ # test tab completion
+ (
+ cd sparse-checkout &&
+ test_completion "git sparse-checkout set f" <<-\EOF
+ folder1/
+ folder2/
+ folder3/
+ EOF
+ ) &&
+
+ (
+ cd sparse-checkout &&
+ test_completion "git sparse-checkout set folder1/" <<-\EOF
+ folder1/0/
+ EOF
+ ) &&
+
+ (
+ cd sparse-checkout &&
+ test_completion "git sparse-checkout set folder1/0/" <<-\EOF
+ folder1/0/1/
+ EOF
+ ) &&
+
+ (
+ cd sparse-checkout/folder1 &&
+ test_completion "git sparse-checkout add 0" <<-\EOF
+ 0/
+ EOF
+ )
+'
+
+test_expect_success 'cone mode sparse-checkout completes directory names with spaces and accents' '
+ # reset sparse-checkout
+ git -C sparse-checkout sparse-checkout disable &&
+ (
+ cd sparse-checkout &&
+ mkdir "directory with spaces" &&
+ mkdir "directory-with-áccent" &&
+ >"directory with spaces/randomfile" &&
+ >"directory-with-áccent/randomfile" &&
+ git add . &&
+ git commit -m "Add directory with spaces and directory with accent" &&
+ git sparse-checkout set --cone "directory with spaces" \
+ "directory-with-áccent" &&
+ test_completion "git sparse-checkout add dir" <<-\EOF &&
+ directory with spaces/
+ directory-with-áccent/
+ EOF
+ rm -rf "directory with spaces" &&
+ rm -rf "directory-with-áccent" &&
+ git add . &&
+ git commit -m "Remove directory with spaces and directory with accent"
+ )
+'
+
+# use FUNNYNAMES to avoid running on Windows, which doesn't permit tabs in paths
+test_expect_success FUNNYNAMES 'cone mode sparse-checkout completes directory names with tabs' '
+ # reset sparse-checkout
+ git -C sparse-checkout sparse-checkout disable &&
+ (
+ cd sparse-checkout &&
+ mkdir "$(printf "directory\twith\ttabs")" &&
+ >"$(printf "directory\twith\ttabs")/randomfile" &&
+ git add . &&
+ git commit -m "Add directory with tabs" &&
+ git sparse-checkout set --cone \
+ "$(printf "directory\twith\ttabs")" &&
+ test_completion "git sparse-checkout add dir" <<-\EOF &&
+ directory with tabs/
+ EOF
+ rm -rf "$(printf "directory\twith\ttabs")" &&
+ git add . &&
+ git commit -m "Remove directory with tabs"
+ )
+'
+
+# use FUNNYNAMES to avoid running on Windows, and !CYGWIN for Cygwin, as neither permit backslashes in paths
+test_expect_success FUNNYNAMES,!CYGWIN 'cone mode sparse-checkout completes directory names with backslashes' '
+ # reset sparse-checkout
+ git -C sparse-checkout sparse-checkout disable &&
+ (
+ cd sparse-checkout &&
+ mkdir "directory\with\backslashes" &&
+ >"directory\with\backslashes/randomfile" &&
+ git add . &&
+ git commit -m "Add directory with backslashes" &&
+ git sparse-checkout set --cone \
+ "directory\with\backslashes" &&
+ test_completion "git sparse-checkout add dir" <<-\EOF &&
+ directory\with\backslashes/
+ EOF
+ rm -rf "directory\with\backslashes" &&
+ git add . &&
+ git commit -m "Remove directory with backslashes"
+ )
+'
+
+test_expect_success 'non-cone mode sparse-checkout uses bash completion' '
+ # reset sparse-checkout repo to non-cone mode
+ git -C sparse-checkout sparse-checkout disable &&
+ git -C sparse-checkout sparse-checkout set --no-cone &&
+
+ (
+ cd sparse-checkout &&
+ # expected to be empty since we have not configured
+ # custom completion for non-cone mode
+ test_completion "git sparse-checkout set f" <<-\EOF
+
+ EOF
+ )
+'
+
+test_expect_success 'git sparse-checkout set --cone completes directory names' '
+ git -C sparse-checkout sparse-checkout disable &&
+
+ (
+ cd sparse-checkout &&
+ test_completion "git sparse-checkout set --cone f" <<-\EOF
+ folder1/
+ folder2/
+ folder3/
+ EOF
+ )
+'
+
+test_expect_success 'git switch - with -d, complete all references' '
+ test_completion "git switch -d " <<-\EOF
+ HEAD Z
+ main Z
+ matching-branch Z
+ matching-tag Z
+ other/branch-in-other Z
+ other/main-in-other Z
+ EOF
+'
+
+test_expect_success 'git checkout - with -d, complete only references' '
+ test_completion "git checkout -d " <<-\EOF
+ HEAD Z
+ main Z
+ matching-branch Z
+ matching-tag Z
+ other/branch-in-other Z
+ other/main-in-other Z
+ EOF
+'
+
+test_expect_success 'git switch - with --track, complete only remote branches' '
+ test_completion "git switch --track " <<-\EOF &&
+ other/branch-in-other Z
+ other/main-in-other Z
+ EOF
+ test_completion "git switch -t " <<-\EOF
+ other/branch-in-other Z
+ other/main-in-other Z
+ EOF
+'
+
+test_expect_success 'git checkout - with --track, complete only remote branches' '
+ test_completion "git checkout --track " <<-\EOF &&
+ other/branch-in-other Z
+ other/main-in-other Z
+ EOF
+ test_completion "git checkout -t " <<-\EOF
+ other/branch-in-other Z
+ other/main-in-other Z
+ EOF
+'
+
+test_expect_success 'git switch - with --no-track, complete only local branch names' '
+ test_completion "git switch --no-track " <<-\EOF
+ main Z
+ matching-branch Z
+ EOF
+'
+
+test_expect_success 'git checkout - with --no-track, complete only local references' '
+ test_completion "git checkout --no-track " <<-\EOF
+ HEAD Z
+ main Z
+ matching-branch Z
+ matching-tag Z
+ other/branch-in-other Z
+ other/main-in-other Z
+ EOF
+'
+
+test_expect_success 'git switch - with -c, complete all references' '
+ test_completion "git switch -c new-branch " <<-\EOF
+ HEAD Z
+ main Z
+ matching-branch Z
+ matching-tag Z
+ other/branch-in-other Z
+ other/main-in-other Z
+ EOF
+'
+
+test_expect_success 'git switch - with -C, complete all references' '
+ test_completion "git switch -C new-branch " <<-\EOF
+ HEAD Z
+ main Z
+ matching-branch Z
+ matching-tag Z
+ other/branch-in-other Z
+ other/main-in-other Z
+ EOF
+'
+
+test_expect_success 'git switch - with -c and --track, complete all references' '
+ test_completion "git switch -c new-branch --track " <<-EOF
+ HEAD Z
+ main Z
+ matching-branch Z
+ matching-tag Z
+ other/branch-in-other Z
+ other/main-in-other Z
+ EOF
+'
+
+test_expect_success 'git switch - with -C and --track, complete all references' '
+ test_completion "git switch -C new-branch --track " <<-EOF
+ HEAD Z
+ main Z
+ matching-branch Z
+ matching-tag Z
+ other/branch-in-other Z
+ other/main-in-other Z
+ EOF
+'
+
+test_expect_success 'git switch - with -c and --no-track, complete all references' '
+ test_completion "git switch -c new-branch --no-track " <<-\EOF
+ HEAD Z
+ main Z
+ matching-branch Z
+ matching-tag Z
+ other/branch-in-other Z
+ other/main-in-other Z
+ EOF
+'
+
+test_expect_success 'git switch - with -C and --no-track, complete all references' '
+ test_completion "git switch -C new-branch --no-track " <<-\EOF
+ HEAD Z
+ main Z
+ matching-branch Z
+ matching-tag Z
+ other/branch-in-other Z
+ other/main-in-other Z
+ EOF
+'
+
+test_expect_success 'git checkout - with -b, complete all references' '
+ test_completion "git checkout -b new-branch " <<-\EOF
+ HEAD Z
+ main Z
+ matching-branch Z
+ matching-tag Z
+ other/branch-in-other Z
+ other/main-in-other Z
+ EOF
+'
+
+test_expect_success 'git checkout - with -B, complete all references' '
+ test_completion "git checkout -B new-branch " <<-\EOF
+ HEAD Z
+ main Z
+ matching-branch Z
+ matching-tag Z
+ other/branch-in-other Z
+ other/main-in-other Z
+ EOF
+'
+
+test_expect_success 'git checkout - with -b and --track, complete all references' '
+ test_completion "git checkout -b new-branch --track " <<-EOF
+ HEAD Z
+ main Z
+ matching-branch Z
+ matching-tag Z
+ other/branch-in-other Z
+ other/main-in-other Z
+ EOF
+'
+
+test_expect_success 'git checkout - with -B and --track, complete all references' '
+ test_completion "git checkout -B new-branch --track " <<-EOF
+ HEAD Z
+ main Z
+ matching-branch Z
+ matching-tag Z
+ other/branch-in-other Z
+ other/main-in-other Z
+ EOF
+'
+
+test_expect_success 'git checkout - with -b and --no-track, complete all references' '
+ test_completion "git checkout -b new-branch --no-track " <<-\EOF
+ HEAD Z
+ main Z
+ matching-branch Z
+ matching-tag Z
+ other/branch-in-other Z
+ other/main-in-other Z
+ EOF
+'
+
+test_expect_success 'git checkout - with -B and --no-track, complete all references' '
+ test_completion "git checkout -B new-branch --no-track " <<-\EOF
+ HEAD Z
+ main Z
+ matching-branch Z
+ matching-tag Z
+ other/branch-in-other Z
+ other/main-in-other Z
+ EOF
+'
+
+test_expect_success 'git switch - for -c, complete local branches and unique remote branches' '
+ test_completion "git switch -c " <<-\EOF
+ branch-in-other Z
+ main Z
+ main-in-other Z
+ matching-branch Z
+ EOF
+'
+
+test_expect_success 'git switch - for -C, complete local branches and unique remote branches' '
+ test_completion "git switch -C " <<-\EOF
+ branch-in-other Z
+ main Z
+ main-in-other Z
+ matching-branch Z
+ EOF
+'
+
+test_expect_success 'git switch - for -c with --no-guess, complete local branches only' '
+ test_completion "git switch --no-guess -c " <<-\EOF
+ main Z
+ matching-branch Z
+ EOF
+'
+
+test_expect_success 'git switch - for -C with --no-guess, complete local branches only' '
+ test_completion "git switch --no-guess -C " <<-\EOF
+ main Z
+ matching-branch Z
+ EOF
+'
+
+test_expect_success 'git switch - for -c with --no-track, complete local branches only' '
+ test_completion "git switch --no-track -c " <<-\EOF
+ main Z
+ matching-branch Z
+ EOF
+'
+
+test_expect_success 'git switch - for -C with --no-track, complete local branches only' '
+ test_completion "git switch --no-track -C " <<-\EOF
+ main Z
+ matching-branch Z
+ EOF
+'
+
+test_expect_success 'git checkout - for -b, complete local branches and unique remote branches' '
+ test_completion "git checkout -b " <<-\EOF
+ branch-in-other Z
+ main Z
+ main-in-other Z
+ matching-branch Z
+ EOF
+'
+
+test_expect_success 'git checkout - for -B, complete local branches and unique remote branches' '
+ test_completion "git checkout -B " <<-\EOF
+ branch-in-other Z
+ main Z
+ main-in-other Z
+ matching-branch Z
+ EOF
+'
+
+test_expect_success 'git checkout - for -b with --no-guess, complete local branches only' '
+ test_completion "git checkout --no-guess -b " <<-\EOF
+ main Z
+ matching-branch Z
+ EOF
+'
+
+test_expect_success 'git checkout - for -B with --no-guess, complete local branches only' '
+ test_completion "git checkout --no-guess -B " <<-\EOF
+ main Z
+ matching-branch Z
+ EOF
+'
+
+test_expect_success 'git checkout - for -b with --no-track, complete local branches only' '
+ test_completion "git checkout --no-track -b " <<-\EOF
+ main Z
+ matching-branch Z
+ EOF
+'
+
+test_expect_success 'git checkout - for -B with --no-track, complete local branches only' '
+ test_completion "git checkout --no-track -B " <<-\EOF
+ main Z
+ matching-branch Z
+ EOF
+'
+
+test_expect_success 'git switch - with --orphan completes local branch names and unique remote branch names' '
+ test_completion "git switch --orphan " <<-\EOF
+ branch-in-other Z
+ main Z
+ main-in-other Z
+ matching-branch Z
+ EOF
+'
+
+test_expect_success 'git switch - --orphan with branch already provided completes nothing else' '
+ test_completion "git switch --orphan main " <<-\EOF
+
+ EOF
+'
+
+test_expect_success 'git checkout - with --orphan completes local branch names and unique remote branch names' '
+ test_completion "git checkout --orphan " <<-\EOF
+ branch-in-other Z
+ main Z
+ main-in-other Z
+ matching-branch Z
+ EOF
+'
+
+test_expect_success 'git checkout - --orphan with branch already provided completes local refs for a start-point' '
+ test_completion "git checkout --orphan main " <<-\EOF
+ HEAD Z
+ main Z
+ matching-branch Z
+ matching-tag Z
+ other/branch-in-other Z
+ other/main-in-other Z
+ EOF
+'
+
+test_expect_success 'teardown after ref completion' '
+ git branch -d matching-branch &&
+ git tag -d matching-tag &&
+ git remote remove other
+'
+
+
+test_path_completion ()
+{
+ test $# = 2 || BUG "not 2 parameters to test_path_completion"
+
+ local cur="$1" expected="$2"
+ echo "$expected" >expected &&
+ (
+ # In the following tests calling this function we only
+ # care about how __git_complete_index_file() deals with
+ # unusual characters in path names. By requesting only
+ # untracked files we do not have to bother adding any
+ # paths to the index in those tests.
+ __git_complete_index_file --others &&
+ print_comp
+ ) &&
+ test_cmp expected out
+}
+
+test_expect_success 'setup for path completion tests' '
+ mkdir simple-dir \
+ "spaces in dir" \
+ árvíztűrő &&
+ touch simple-dir/simple-file \
+ "spaces in dir/spaces in file" \
+ "árvíztűrÅ‘/Сайн Ñваарай" &&
+ if test_have_prereq !MINGW &&
+ mkdir BS\\dir \
+ '$'separators\034in\035dir'' &&
+ touch BS\\dir/DQ\"file \
+ '$'separators\034in\035dir/sep\036in\037file''
+ then
+ test_set_prereq FUNNIERNAMES
+ else
+ rm -rf BS\\dir '$'separators\034in\035dir''
+ fi
+'
+
+test_expect_success '__git_complete_index_file - simple' '
+ test_path_completion simple simple-dir && # Bash is supposed to
+ # add the trailing /.
+ test_path_completion simple-dir/simple simple-dir/simple-file
+'
+
+test_expect_success \
+ '__git_complete_index_file - escaped characters on cmdline' '
+ test_path_completion spac "spaces in dir" && # Bash will turn this
+ # into "spaces\ in\ dir"
+ test_path_completion "spaces\\ i" \
+ "spaces in dir" &&
+ test_path_completion "spaces\\ in\\ dir/s" \
+ "spaces in dir/spaces in file" &&
+ test_path_completion "spaces\\ in\\ dir/spaces\\ i" \
+ "spaces in dir/spaces in file"
+'
+
+test_expect_success \
+ '__git_complete_index_file - quoted characters on cmdline' '
+ # Testing with an opening but without a corresponding closing
+ # double quote is important.
+ test_path_completion \"spac "spaces in dir" &&
+ test_path_completion "\"spaces i" \
+ "spaces in dir" &&
+ test_path_completion "\"spaces in dir/s" \
+ "spaces in dir/spaces in file" &&
+ test_path_completion "\"spaces in dir/spaces i" \
+ "spaces in dir/spaces in file"
+'
+
+test_expect_success '__git_complete_index_file - UTF-8 in ls-files output' '
+ test_path_completion á árvíztűrő &&
+ test_path_completion árvíztűrÅ‘/С "árvíztűrÅ‘/Сайн Ñваарай"
+'
+
+test_expect_success FUNNIERNAMES \
+ '__git_complete_index_file - C-style escapes in ls-files output' '
+ test_path_completion BS \
+ BS\\dir &&
+ test_path_completion BS\\\\d \
+ BS\\dir &&
+ test_path_completion BS\\\\dir/DQ \
+ BS\\dir/DQ\"file &&
+ test_path_completion BS\\\\dir/DQ\\\"f \
+ BS\\dir/DQ\"file
+'
+
+test_expect_success FUNNIERNAMES \
+ '__git_complete_index_file - \nnn-escaped characters in ls-files output' '
+ test_path_completion sep '$'separators\034in\035dir'' &&
+ test_path_completion '$'separators\034i'' \
+ '$'separators\034in\035dir'' &&
+ test_path_completion '$'separators\034in\035dir/sep'' \
+ '$'separators\034in\035dir/sep\036in\037file'' &&
+ test_path_completion '$'separators\034in\035dir/sep\036i'' \
+ '$'separators\034in\035dir/sep\036in\037file''
+'
+
+test_expect_success FUNNYNAMES \
+ '__git_complete_index_file - removing repeated quoted path components' '
+ test_when_finished rm -r repeated-quoted &&
+ mkdir repeated-quoted && # A directory whose name in itself
+ # would not be quoted ...
+ >repeated-quoted/0-file &&
+ >repeated-quoted/1\"file && # ... but here the file makes the
+ # dirname quoted ...
+ >repeated-quoted/2-file &&
+ >repeated-quoted/3\"file && # ... and here, too.
+
+ # Still, we shold only list the directory name only once.
+ test_path_completion repeated repeated-quoted
+'
+
+test_expect_success 'teardown after path completion tests' '
+ rm -rf simple-dir "spaces in dir" árvíztűrő \
+ BS\\dir '$'separators\034in\035dir''
+'
+
+test_expect_success '__git_find_on_cmdline - single match' '
+ echo list >expect &&
+ (
+ words=(git command --opt list) &&
+ cword=${#words[@]} &&
+ __git_cmd_idx=1 &&
+ __git_find_on_cmdline "add list remove" >actual
+ ) &&
+ test_cmp expect actual
+'
+
+test_expect_success '__git_find_on_cmdline - multiple matches' '
+ echo remove >expect &&
+ (
+ words=(git command -o --opt remove list add) &&
+ cword=${#words[@]} &&
+ __git_cmd_idx=1 &&
+ __git_find_on_cmdline "add list remove" >actual
+ ) &&
+ test_cmp expect actual
+'
+
+test_expect_success '__git_find_on_cmdline - no match' '
+ (
+ words=(git command --opt branch) &&
+ cword=${#words[@]} &&
+ __git_cmd_idx=1 &&
+ __git_find_on_cmdline "add list remove" >actual
+ ) &&
+ test_must_be_empty actual
+'
+
+test_expect_success '__git_find_on_cmdline - single match with index' '
+ echo "3 list" >expect &&
+ (
+ words=(git command --opt list) &&
+ cword=${#words[@]} &&
+ __git_cmd_idx=1 &&
+ __git_find_on_cmdline --show-idx "add list remove" >actual
+ ) &&
+ test_cmp expect actual
+'
+
+test_expect_success '__git_find_on_cmdline - multiple matches with index' '
+ echo "4 remove" >expect &&
+ (
+ words=(git command -o --opt remove list add) &&
+ cword=${#words[@]} &&
+ __git_cmd_idx=1 &&
+ __git_find_on_cmdline --show-idx "add list remove" >actual
+ ) &&
+ test_cmp expect actual
+'
+
+test_expect_success '__git_find_on_cmdline - no match with index' '
+ (
+ words=(git command --opt branch) &&
+ cword=${#words[@]} &&
+ __git_cmd_idx=1 &&
+ __git_find_on_cmdline --show-idx "add list remove" >actual
+ ) &&
+ test_must_be_empty actual
+'
+
+test_expect_success '__git_find_on_cmdline - ignores matches before command with index' '
+ echo "6 remove" >expect &&
+ (
+ words=(git -C remove command -o --opt remove list add) &&
+ cword=${#words[@]} &&
+ __git_cmd_idx=3 &&
+ __git_find_on_cmdline --show-idx "add list remove" >actual
+ ) &&
+ test_cmp expect actual
+'
+
+test_expect_success '__git_get_config_variables' '
+ cat >expect <<-EOF &&
+ name-1
+ name-2
+ EOF
+ test_config interesting.name-1 good &&
+ test_config interesting.name-2 good &&
+ test_config subsection.interesting.name-3 bad &&
+ __git_get_config_variables interesting >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success '__git_pretty_aliases' '
+ cat >expect <<-EOF &&
+ author
+ hash
+ EOF
+ test_config pretty.author "%an %ae" &&
+ test_config pretty.hash %H &&
+ __git_pretty_aliases >actual &&
+ test_cmp expect actual
+'
+
+test_expect_success 'basic' '
+ run_completion "git " &&
+ # built-in
+ grep -q "^add \$" out &&
+ # script
+ grep -q "^rebase \$" out &&
+ # plumbing
+ ! grep -q "^ls-files \$" out &&
+
+ run_completion "git r" &&
+ ! grep -q -v "^r" out
+'
+
+test_expect_success 'double dash "git" itself' '
+ test_completion "git --" <<-\EOF
+ --paginate Z
+ --no-pager Z
+ --git-dir=
+ --bare Z
+ --version Z
+ --exec-path Z
+ --exec-path=
+ --html-path Z
+ --man-path Z
+ --info-path Z
+ --work-tree=
+ --namespace=
+ --no-replace-objects Z
+ --help Z
+ EOF
+'
+
+test_expect_success 'double dash "git checkout"' '
+ test_completion "git checkout --" <<-\EOF
+ --quiet Z
+ --detach Z
+ --track Z
+ --orphan=Z
+ --ours Z
+ --theirs Z
+ --merge Z
+ --conflict=Z
+ --patch Z
+ --ignore-skip-worktree-bits Z
+ --ignore-other-worktrees Z
+ --recurse-submodules Z
+ --progress Z
+ --guess Z
+ --no-guess Z
+ --no-... Z
+ --overlay Z
+ --pathspec-file-nul Z
+ --pathspec-from-file=Z
+ EOF
+'
+
+test_expect_success 'general options' '
+ test_completion "git --ver" "--version " &&
+ test_completion "git --hel" "--help " &&
+ test_completion "git --exe" <<-\EOF &&
+ --exec-path Z
+ --exec-path=
+ EOF
+ test_completion "git --htm" "--html-path " &&
+ test_completion "git --pag" "--paginate " &&
+ test_completion "git --no-p" "--no-pager " &&
+ test_completion "git --git" "--git-dir=" &&
+ test_completion "git --wor" "--work-tree=" &&
+ test_completion "git --nam" "--namespace=" &&
+ test_completion "git --bar" "--bare " &&
+ test_completion "git --inf" "--info-path " &&
+ test_completion "git --no-r" "--no-replace-objects "
+'
+
+test_expect_success 'general options plus command' '
+ test_completion "git --version check" "checkout " &&
+ test_completion "git --paginate check" "checkout " &&
+ test_completion "git --git-dir=foo check" "checkout " &&
+ test_completion "git --bare check" "checkout " &&
+ test_completion "git --exec-path=foo check" "checkout " &&
+ test_completion "git --html-path check" "checkout " &&
+ test_completion "git --no-pager check" "checkout " &&
+ test_completion "git --work-tree=foo check" "checkout " &&
+ test_completion "git --namespace=foo check" "checkout " &&
+ test_completion "git --paginate check" "checkout " &&
+ test_completion "git --info-path check" "checkout " &&
+ test_completion "git --no-replace-objects check" "checkout " &&
+ test_completion "git --git-dir some/path check" "checkout " &&
+ test_completion "git -c conf.var=value check" "checkout " &&
+ test_completion "git -C some/path check" "checkout " &&
+ test_completion "git --work-tree some/path check" "checkout " &&
+ test_completion "git --namespace name/space check" "checkout "
+'
+
+test_expect_success 'git --help completion' '
+ test_completion "git --help ad" "add " &&
+ test_completion "git --help core" "core-tutorial "
+'
+
+test_expect_success 'completion.commands removes multiple commands' '
+ test_config completion.commands "-cherry -mergetool" &&
+ git --list-cmds=list-mainporcelain,list-complete,config >out &&
+ ! grep -E "^(cherry|mergetool)$" out
+'
+
+test_expect_success 'setup for integration tests' '
+ echo content >file1 &&
+ echo more >file2 &&
+ git add file1 file2 &&
+ git commit -m one &&
+ git branch mybranch &&
+ git tag mytag
+'
+
+test_expect_success 'checkout completes ref names' '
+ test_completion "git checkout m" <<-\EOF
+ main Z
+ mybranch Z
+ mytag Z
+ EOF
+'
+
+test_expect_success 'checkout does not match ref names of a different case' '
+ test_completion "git checkout M" ""
+'
+
+test_expect_success 'checkout matches case insensitively with GIT_COMPLETION_IGNORE_CASE' '
+ (
+ GIT_COMPLETION_IGNORE_CASE=1 &&
+ test_completion "git checkout M" <<-\EOF
+ main Z
+ mybranch Z
+ mytag Z
+ EOF
+ )
+'
+
+test_expect_success 'checkout completes pseudo refs' '
+ test_completion "git checkout H" <<-\EOF
+ HEAD Z
+ EOF
+'
+
+test_expect_success 'checkout completes pseudo refs case insensitively with GIT_COMPLETION_IGNORE_CASE' '
+ (
+ GIT_COMPLETION_IGNORE_CASE=1 &&
+ test_completion "git checkout h" <<-\EOF
+ HEAD Z
+ EOF
+ )
+'
+
+test_expect_success 'git -C <path> checkout uses the right repo' '
+ test_completion "git -C subdir -C subsubdir -C .. -C ../otherrepo checkout b" <<-\EOF
+ branch-in-other Z
+ EOF
+'
+
+test_expect_success 'show completes all refs' '
+ test_completion "git show m" <<-\EOF
+ main Z
+ mybranch Z
+ mytag Z
+ EOF
+'
+
+test_expect_success '<ref>: completes paths' '
+ test_completion "git show mytag:f" <<-\EOF
+ file1Z
+ file2Z
+ EOF
+'
+
+test_expect_success 'complete tree filename with spaces' '
+ echo content >"name with spaces" &&
+ git add "name with spaces" &&
+ git commit -m spaces &&
+ test_completion "git show HEAD:nam" <<-\EOF
+ name with spacesZ
+ EOF
+'
+
+test_expect_success 'complete tree filename with metacharacters' '
+ echo content >"name with \${meta}" &&
+ git add "name with \${meta}" &&
+ git commit -m meta &&
+ test_completion "git show HEAD:nam" <<-\EOF
+ name with ${meta}Z
+ name with spacesZ
+ EOF
+'
+
+test_expect_success PERL 'send-email' '
+ test_completion "git send-email --cov" <<-\EOF &&
+ --cover-from-description=Z
+ --cover-letter Z
+ EOF
+ test_completion "git send-email --val" <<-\EOF &&
+ --validate Z
+ EOF
+ test_completion "git send-email ma" "main "
+'
+
+test_expect_success 'complete files' '
+ git init tmp && cd tmp &&
+ test_when_finished "cd .. && rm -rf tmp" &&
+
+ echo "expected" > .gitignore &&
+ echo "out" >> .gitignore &&
+ echo "out_sorted" >> .gitignore &&
+
+ git add .gitignore &&
+ test_completion "git commit " ".gitignore" &&
+
+ git commit -m ignore &&
+
+ touch new &&
+ test_completion "git add " "new" &&
+
+ git add new &&
+ git commit -a -m new &&
+ test_completion "git add " "" &&
+
+ git mv new modified &&
+ echo modify > modified &&
+ test_completion "git add " "modified" &&
+
+ mkdir -p some/deep &&
+ touch some/deep/path &&
+ test_completion "git add some/" "some/deep" &&
+ git clean -f some &&
+
+ touch untracked &&
+
+ : TODO .gitignore should not be here &&
+ test_completion "git rm " <<-\EOF &&
+ .gitignore
+ modified
+ EOF
+
+ test_completion "git clean " "untracked" &&
+
+ : TODO .gitignore should not be here &&
+ test_completion "git mv " <<-\EOF &&
+ .gitignore
+ modified
+ EOF
+
+ mkdir dir &&
+ touch dir/file-in-dir &&
+ git add dir/file-in-dir &&
+ git commit -m dir &&
+
+ mkdir untracked-dir &&
+
+ : TODO .gitignore should not be here &&
+ test_completion "git mv modified " <<-\EOF &&
+ .gitignore
+ dir
+ modified
+ untracked
+ untracked-dir
+ EOF
+
+ test_completion "git commit " "modified" &&
+
+ : TODO .gitignore should not be here &&
+ test_completion "git ls-files " <<-\EOF &&
+ .gitignore
+ dir
+ modified
+ EOF
+
+ touch momified &&
+ test_completion "git add mom" "momified"
+'
+
+test_expect_success "simple alias" '
+ test_config alias.co checkout &&
+ test_completion "git co m" <<-\EOF
+ main Z
+ mybranch Z
+ mytag Z
+ EOF
+'
+
+test_expect_success "recursive alias" '
+ test_config alias.co checkout &&
+ test_config alias.cod "co --detached" &&
+ test_completion "git cod m" <<-\EOF
+ main Z
+ mybranch Z
+ mytag Z
+ EOF
+'
+
+test_expect_success "completion uses <cmd> completion for alias: !sh -c 'git <cmd> ...'" '
+ test_config alias.co "!sh -c '"'"'git checkout ...'"'"'" &&
+ test_completion "git co m" <<-\EOF
+ main Z
+ mybranch Z
+ mytag Z
+ EOF
+'
+
+test_expect_success 'completion uses <cmd> completion for alias: !f () { VAR=val git <cmd> ... }' '
+ test_config alias.co "!f () { VAR=val git checkout ... ; } f" &&
+ test_completion "git co m" <<-\EOF
+ main Z
+ mybranch Z
+ mytag Z
+ EOF
+'
+
+test_expect_success 'completion used <cmd> completion for alias: !f() { : git <cmd> ; ... }' '
+ test_config alias.co "!f() { : git checkout ; if ... } f" &&
+ test_completion "git co m" <<-\EOF
+ main Z
+ mybranch Z
+ mytag Z
+ EOF
+'
+
+test_expect_success 'completion used <cmd> completion for alias: !f() { : <cmd> ; ... }' '
+ test_config alias.co "!f() { : checkout ; if ... } f" &&
+ test_completion "git co m" <<-\EOF
+ main Z
+ mybranch Z
+ mytag Z
+ EOF
+'
+
+test_expect_success 'completion used <cmd> completion for alias: !f() { : <cmd>; ... }' '
+ test_config alias.co "!f() { : checkout; if ... } f" &&
+ test_completion "git co m" <<-\EOF
+ main Z
+ mybranch Z
+ mytag Z
+ EOF
+'
+
+test_expect_success 'completion without explicit _git_xxx function' '
+ test_completion "git version --" <<-\EOF
+ --build-options Z
+ --no-build-options Z
+ EOF
+'
+
+test_expect_failure 'complete with tilde expansion' '
+ git init tmp && cd tmp &&
+ test_when_finished "cd .. && rm -rf tmp" &&
+
+ touch ~/tmp/file &&
+
+ test_completion "git add ~/tmp/" "~/tmp/file"
+'
+
+test_expect_success 'setup other remote for remote reference completion' '
+ git remote add other otherrepo &&
+ git fetch other
+'
+
+for flag in -d --delete
+do
+ test_expect_success "__git_complete_remote_or_refspec - push $flag other" '
+ sed -e "s/Z$//" >expected <<-EOF &&
+ main-in-other Z
+ EOF
+ (
+ words=(git push '$flag' other ma) &&
+ cword=${#words[@]} cur=${words[cword-1]} &&
+ __git_cmd_idx=1 &&
+ __git_complete_remote_or_refspec &&
+ print_comp
+ ) &&
+ test_cmp expected out
+ '
+
+ test_expect_failure "__git_complete_remote_or_refspec - push other $flag" '
+ sed -e "s/Z$//" >expected <<-EOF &&
+ main-in-other Z
+ EOF
+ (
+ words=(git push other '$flag' ma) &&
+ cword=${#words[@]} cur=${words[cword-1]} &&
+ __git_cmd_idx=1 &&
+ __git_complete_remote_or_refspec &&
+ print_comp
+ ) &&
+ test_cmp expected out
+ '
+done
+
+test_expect_success 'git config - section' '
+ test_completion "git config br" <<-\EOF
+ branch.Z
+ browser.Z
+ EOF
+'
+
+test_expect_success 'git config - section include, includeIf' '
+ test_completion "git config inclu" <<-\EOF
+ include.Z
+ includeIf.Z
+ EOF
+'
+
+test_expect_success 'git config - variable name' '
+ test_completion "git config log.d" <<-\EOF
+ log.date Z
+ log.decorate Z
+ log.diffMerges Z
+ EOF
+'
+
+test_expect_success 'git config - variable name include' '
+ test_completion "git config include.p" <<-\EOF
+ include.path Z
+ EOF
+'
+
+test_expect_success 'git config - value' '
+ test_completion "git config color.pager " <<-\EOF
+ false Z
+ true Z
+ EOF
+'
+
+test_expect_success 'git -c - section' '
+ test_completion "git -c br" <<-\EOF
+ branch.Z
+ browser.Z
+ EOF
+'
+
+test_expect_success 'git -c - variable name' '
+ test_completion "git -c log.d" <<-\EOF
+ log.date=Z
+ log.decorate=Z
+ log.diffMerges=Z
+ EOF
+'
+
+test_expect_success 'git -c - value' '
+ test_completion "git -c color.pager=" <<-\EOF
+ false Z
+ true Z
+ EOF
+'
+
+test_expect_success 'git clone --config= - section' '
+ test_completion "git clone --config=br" <<-\EOF
+ branch.Z
+ browser.Z
+ EOF
+'
+
+test_expect_success 'git clone --config= - variable name' '
+ test_completion "git clone --config=log.d" <<-\EOF
+ log.date=Z
+ log.decorate=Z
+ log.diffMerges=Z
+ EOF
+'
+
+test_expect_success 'git clone --config= - value' '
+ test_completion "git clone --config=color.pager=" <<-\EOF
+ false Z
+ true Z
+ EOF
+'
+
+test_expect_success 'options with value' '
+ test_completion "git merge -X diff-algorithm=" <<-\EOF
+
+ EOF
+'
+
+test_expect_success 'sourcing the completion script clears cached commands' '
+ (
+ __git_compute_all_commands &&
+ test -n "$__git_all_commands" &&
+ . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
+ test -z "$__git_all_commands"
+ )
+'
+
+test_expect_success 'sourcing the completion script clears cached merge strategies' '
+ (
+ __git_compute_merge_strategies &&
+ test -n "$__git_merge_strategies" &&
+ . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
+ test -z "$__git_merge_strategies"
+ )
+'
+
+test_expect_success 'sourcing the completion script clears cached --options' '
+ (
+ __gitcomp_builtin checkout &&
+ test -n "$__gitcomp_builtin_checkout" &&
+ __gitcomp_builtin notes_edit &&
+ test -n "$__gitcomp_builtin_notes_edit" &&
+ . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
+ test -z "$__gitcomp_builtin_checkout" &&
+ test -z "$__gitcomp_builtin_notes_edit"
+ )
+'
+
+test_expect_success 'option aliases are not shown by default' '
+ test_completion "git clone --recurs" "--recurse-submodules "
+'
+
+test_expect_success 'option aliases are shown with GIT_COMPLETION_SHOW_ALL' '
+ (
+ . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
+ GIT_COMPLETION_SHOW_ALL=1 && export GIT_COMPLETION_SHOW_ALL &&
+ test_completion "git clone --recurs" <<-\EOF
+ --recurse-submodules Z
+ --recursive Z
+ EOF
+ )
+'
+
+test_expect_success 'plumbing commands are excluded without GIT_COMPLETION_SHOW_ALL_COMMANDS' '
+ (
+ . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
+ sane_unset GIT_TESTING_PORCELAIN_COMMAND_LIST &&
+
+ # Just mainporcelain, not plumbing commands
+ run_completion "git c" &&
+ grep checkout out &&
+ ! grep cat-file out
+ )
+'
+
+test_expect_success 'all commands are shown with GIT_COMPLETION_SHOW_ALL_COMMANDS (also main non-builtin)' '
+ (
+ . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
+ GIT_COMPLETION_SHOW_ALL_COMMANDS=1 &&
+ export GIT_COMPLETION_SHOW_ALL_COMMANDS &&
+ sane_unset GIT_TESTING_PORCELAIN_COMMAND_LIST &&
+
+ # Both mainporcelain and plumbing commands
+ run_completion "git c" &&
+ grep checkout out &&
+ grep cat-file out &&
+
+ # Check "gitk", a "main" command, but not a built-in + more plumbing
+ run_completion "git g" &&
+ grep gitk out &&
+ grep get-tar-commit-id out
+ )
+'
+
+test_expect_success '__git_complete' '
+ unset -f __git_wrap__git_main &&
+
+ __git_complete foo __git_main &&
+ __git_have_func __git_wrap__git_main &&
+ unset -f __git_wrap__git_main &&
+
+ __git_complete gf _git_fetch &&
+ __git_have_func __git_wrap_git_fetch &&
+
+ __git_complete foo git &&
+ __git_have_func __git_wrap__git_main &&
+ unset -f __git_wrap__git_main &&
+
+ __git_complete gd git_diff &&
+ __git_have_func __git_wrap_git_diff &&
+
+ test_must_fail __git_complete ga missing
+'
+
+test_done
diff --git a/t/t9903-bash-prompt.sh b/t/t9903-bash-prompt.sh
new file mode 100755
index 0000000..d667dda
--- /dev/null
+++ b/t/t9903-bash-prompt.sh
@@ -0,0 +1,778 @@
+#!/bin/sh
+#
+# Copyright (c) 2012 SZEDER Gábor
+#
+
+test_description='test git-specific bash prompt functions'
+
+GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+. ./lib-bash.sh
+
+. "$GIT_BUILD_DIR/contrib/completion/git-prompt.sh"
+
+actual="$TRASH_DIRECTORY/actual"
+c_red='\001\e[31m\002'
+c_green='\001\e[32m\002'
+c_lblue='\001\e[1;34m\002'
+c_clear='\001\e[0m\002'
+
+test_expect_success 'setup for prompt tests' '
+ git init otherrepo &&
+ echo 1 >file &&
+ git add file &&
+ test_tick &&
+ git commit -m initial &&
+ git tag -a -m msg1 t1 &&
+ git checkout -b b1 &&
+ echo 2 >file &&
+ git commit -m "second b1" file &&
+ echo 3 >file &&
+ git commit -m "third b1" file &&
+ git tag -a -m msg2 t2 &&
+ git checkout -b b2 main &&
+ echo 0 >file &&
+ git commit -m "second b2" file &&
+ echo 00 >file &&
+ git commit -m "another b2" file &&
+ echo 000 >file &&
+ git commit -m "yet another b2" file &&
+ mkdir ignored_dir &&
+ echo "ignored_dir/" >>.gitignore &&
+ git checkout main
+'
+
+test_expect_success 'prompt - branch name' '
+ printf " (main)" >expected &&
+ __git_ps1 >"$actual" &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success SYMLINKS 'prompt - branch name - symlink symref' '
+ printf " (main)" >expected &&
+ test_when_finished "git checkout main" &&
+ test_config core.preferSymlinkRefs true &&
+ git checkout main &&
+ __git_ps1 >"$actual" &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - unborn branch' '
+ printf " (unborn)" >expected &&
+ git checkout --orphan unborn &&
+ test_when_finished "git checkout main" &&
+ __git_ps1 >"$actual" &&
+ test_cmp expected "$actual"
+'
+
+if test_have_prereq !FUNNYNAMES; then
+ say 'Your filesystem does not allow newlines in filenames.'
+fi
+
+test_expect_success FUNNYNAMES 'prompt - with newline in path' '
+ repo_with_newline="repo
+with
+newline" &&
+ mkdir "$repo_with_newline" &&
+ printf " (main)" >expected &&
+ git init "$repo_with_newline" &&
+ test_when_finished "rm -rf \"$repo_with_newline\"" &&
+ mkdir "$repo_with_newline"/subdir &&
+ (
+ cd "$repo_with_newline/subdir" &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - detached head' '
+ printf " ((%s...))" $(git log -1 --format="%h" --abbrev=13 b1^) >expected &&
+ test_config core.abbrev 13 &&
+ git checkout b1^ &&
+ test_when_finished "git checkout main" &&
+ __git_ps1 >"$actual" &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - describe detached head - contains' '
+ printf " ((t2~1))" >expected &&
+ git checkout b1^ &&
+ test_when_finished "git checkout main" &&
+ (
+ GIT_PS1_DESCRIBE_STYLE=contains &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - describe detached head - branch' '
+ printf " ((tags/t2~1))" >expected &&
+ git checkout b1^ &&
+ test_when_finished "git checkout main" &&
+ (
+ GIT_PS1_DESCRIBE_STYLE=branch &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - describe detached head - describe' '
+ printf " ((t1-1-g%s))" $(git log -1 --format="%h" b1^) >expected &&
+ git checkout b1^ &&
+ test_when_finished "git checkout main" &&
+ (
+ GIT_PS1_DESCRIBE_STYLE=describe &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - describe detached head - default' '
+ printf " ((t2))" >expected &&
+ git checkout --detach b1 &&
+ test_when_finished "git checkout main" &&
+ __git_ps1 >"$actual" &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - inside .git directory' '
+ printf " (GIT_DIR!)" >expected &&
+ (
+ cd .git &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - deep inside .git directory' '
+ printf " (GIT_DIR!)" >expected &&
+ (
+ cd .git/objects &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - inside bare repository' '
+ printf " (BARE:main)" >expected &&
+ git init --bare bare.git &&
+ test_when_finished "rm -rf bare.git" &&
+ (
+ cd bare.git &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - interactive rebase' '
+ printf " (b1|REBASE 2/3)" >expected &&
+ write_script fake_editor.sh <<-\EOF &&
+ echo "exec echo" >"$1"
+ echo "edit $(git log -1 --format="%h")" >>"$1"
+ echo "exec echo" >>"$1"
+ EOF
+ test_when_finished "rm -f fake_editor.sh" &&
+ test_set_editor "$TRASH_DIRECTORY/fake_editor.sh" &&
+ git checkout b1 &&
+ test_when_finished "git checkout main" &&
+ git rebase -i HEAD^ &&
+ test_when_finished "git rebase --abort" &&
+ __git_ps1 >"$actual" &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - rebase merge' '
+ printf " (b2|REBASE 1/3)" >expected &&
+ git checkout b2 &&
+ test_when_finished "git checkout main" &&
+ test_must_fail git rebase --merge b1 b2 &&
+ test_when_finished "git rebase --abort" &&
+ __git_ps1 >"$actual" &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - rebase am' '
+ printf " (b2|REBASE 1/3)" >expected &&
+ git checkout b2 &&
+ test_when_finished "git checkout main" &&
+ test_must_fail git rebase --apply b1 b2 &&
+ test_when_finished "git rebase --abort" &&
+ __git_ps1 >"$actual" &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - merge' '
+ printf " (b1|MERGING)" >expected &&
+ git checkout b1 &&
+ test_when_finished "git checkout main" &&
+ test_must_fail git merge b2 &&
+ test_when_finished "git reset --hard" &&
+ __git_ps1 >"$actual" &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - cherry-pick' '
+ printf " (main|CHERRY-PICKING)" >expected &&
+ test_must_fail git cherry-pick b1 b1^ &&
+ test_when_finished "git cherry-pick --abort" &&
+ __git_ps1 >"$actual" &&
+ test_cmp expected "$actual" &&
+ git reset --merge &&
+ test_must_fail git rev-parse CHERRY_PICK_HEAD &&
+ __git_ps1 >"$actual" &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - revert' '
+ printf " (main|REVERTING)" >expected &&
+ test_must_fail git revert b1^ b1 &&
+ test_when_finished "git revert --abort" &&
+ __git_ps1 >"$actual" &&
+ test_cmp expected "$actual" &&
+ git reset --merge &&
+ test_must_fail git rev-parse REVERT_HEAD &&
+ __git_ps1 >"$actual" &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - bisect' '
+ printf " (main|BISECTING)" >expected &&
+ git bisect start &&
+ test_when_finished "git bisect reset" &&
+ __git_ps1 >"$actual" &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - dirty status indicator - clean' '
+ printf " (main)" >expected &&
+ (
+ GIT_PS1_SHOWDIRTYSTATE=y &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - dirty status indicator - dirty worktree' '
+ printf " (main *)" >expected &&
+ echo "dirty" >file &&
+ test_when_finished "git reset --hard" &&
+ (
+ GIT_PS1_SHOWDIRTYSTATE=y &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - dirty status indicator - dirty index' '
+ printf " (main +)" >expected &&
+ echo "dirty" >file &&
+ test_when_finished "git reset --hard" &&
+ git add -u &&
+ (
+ GIT_PS1_SHOWDIRTYSTATE=y &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - dirty status indicator - dirty index and worktree' '
+ printf " (main *+)" >expected &&
+ echo "dirty index" >file &&
+ test_when_finished "git reset --hard" &&
+ git add -u &&
+ echo "dirty worktree" >file &&
+ (
+ GIT_PS1_SHOWDIRTYSTATE=y &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - dirty status indicator - orphan branch - clean' '
+ printf " (orphan #)" >expected &&
+ test_when_finished "git checkout main" &&
+ git checkout --orphan orphan &&
+ git reset --hard &&
+ (
+ GIT_PS1_SHOWDIRTYSTATE=y &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - dirty status indicator - orphan branch - dirty index' '
+ printf " (orphan +)" >expected &&
+ test_when_finished "git checkout main" &&
+ git checkout --orphan orphan &&
+ (
+ GIT_PS1_SHOWDIRTYSTATE=y &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - dirty status indicator - orphan branch - dirty index and worktree' '
+ printf " (orphan *+)" >expected &&
+ test_when_finished "git checkout main" &&
+ git checkout --orphan orphan &&
+ >file &&
+ (
+ GIT_PS1_SHOWDIRTYSTATE=y &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - dirty status indicator - shell variable unset with config disabled' '
+ printf " (main)" >expected &&
+ echo "dirty" >file &&
+ test_when_finished "git reset --hard" &&
+ test_config bash.showDirtyState false &&
+ (
+ sane_unset GIT_PS1_SHOWDIRTYSTATE &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - dirty status indicator - shell variable unset with config enabled' '
+ printf " (main)" >expected &&
+ echo "dirty" >file &&
+ test_when_finished "git reset --hard" &&
+ test_config bash.showDirtyState true &&
+ (
+ sane_unset GIT_PS1_SHOWDIRTYSTATE &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - dirty status indicator - shell variable set with config disabled' '
+ printf " (main)" >expected &&
+ echo "dirty" >file &&
+ test_when_finished "git reset --hard" &&
+ test_config bash.showDirtyState false &&
+ (
+ GIT_PS1_SHOWDIRTYSTATE=y &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - dirty status indicator - shell variable set with config enabled' '
+ printf " (main *)" >expected &&
+ echo "dirty" >file &&
+ test_when_finished "git reset --hard" &&
+ test_config bash.showDirtyState true &&
+ (
+ GIT_PS1_SHOWDIRTYSTATE=y &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - dirty status indicator - not shown inside .git directory' '
+ printf " (GIT_DIR!)" >expected &&
+ echo "dirty" >file &&
+ test_when_finished "git reset --hard" &&
+ (
+ GIT_PS1_SHOWDIRTYSTATE=y &&
+ cd .git &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - stash status indicator - no stash' '
+ printf " (main)" >expected &&
+ (
+ GIT_PS1_SHOWSTASHSTATE=y &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - stash status indicator - stash' '
+ printf " (main $)" >expected &&
+ echo 2 >file &&
+ git stash &&
+ test_when_finished "git stash drop" &&
+ git pack-refs --all &&
+ (
+ GIT_PS1_SHOWSTASHSTATE=y &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - stash status indicator - not shown inside .git directory' '
+ printf " (GIT_DIR!)" >expected &&
+ echo 2 >file &&
+ git stash &&
+ test_when_finished "git stash drop" &&
+ (
+ GIT_PS1_SHOWSTASHSTATE=y &&
+ cd .git &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - untracked files status indicator - no untracked files' '
+ printf " (main)" >expected &&
+ (
+ GIT_PS1_SHOWUNTRACKEDFILES=y &&
+ cd otherrepo &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - untracked files status indicator - untracked files' '
+ printf " (main %%)" >expected &&
+ (
+ GIT_PS1_SHOWUNTRACKEDFILES=y &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - untracked files status indicator - empty untracked dir' '
+ printf " (main)" >expected &&
+ mkdir otherrepo/untracked-dir &&
+ test_when_finished "rm -rf otherrepo/untracked-dir" &&
+ (
+ GIT_PS1_SHOWUNTRACKEDFILES=y &&
+ cd otherrepo &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - untracked files status indicator - non-empty untracked dir' '
+ printf " (main %%)" >expected &&
+ mkdir otherrepo/untracked-dir &&
+ test_when_finished "rm -rf otherrepo/untracked-dir" &&
+ >otherrepo/untracked-dir/untracked-file &&
+ (
+ GIT_PS1_SHOWUNTRACKEDFILES=y &&
+ cd otherrepo &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - untracked files status indicator - untracked files outside cwd' '
+ printf " (main %%)" >expected &&
+ (
+ mkdir -p ignored_dir &&
+ cd ignored_dir &&
+ GIT_PS1_SHOWUNTRACKEDFILES=y &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - untracked files status indicator - shell variable unset with config disabled' '
+ printf " (main)" >expected &&
+ test_config bash.showUntrackedFiles false &&
+ (
+ sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - untracked files status indicator - shell variable unset with config enabled' '
+ printf " (main)" >expected &&
+ test_config bash.showUntrackedFiles true &&
+ (
+ sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - untracked files status indicator - shell variable set with config disabled' '
+ printf " (main)" >expected &&
+ test_config bash.showUntrackedFiles false &&
+ (
+ GIT_PS1_SHOWUNTRACKEDFILES=y &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - untracked files status indicator - shell variable set with config enabled' '
+ printf " (main %%)" >expected &&
+ test_config bash.showUntrackedFiles true &&
+ (
+ GIT_PS1_SHOWUNTRACKEDFILES=y &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - untracked files status indicator - not shown inside .git directory' '
+ printf " (GIT_DIR!)" >expected &&
+ (
+ GIT_PS1_SHOWUNTRACKEDFILES=y &&
+ cd .git &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - format string starting with dash' '
+ printf -- "-main" >expected &&
+ __git_ps1 "-%s" >"$actual" &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - pc mode' '
+ printf "BEFORE: (\${__git_ps1_branch_name}):AFTER\\nmain" >expected &&
+ (
+ __git_ps1 "BEFORE:" ":AFTER" >"$actual" &&
+ test_must_be_empty "$actual" &&
+ printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - bash color pc mode - branch name' '
+ printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear}):AFTER\\nmain" >expected &&
+ (
+ GIT_PS1_SHOWCOLORHINTS=y &&
+ __git_ps1 "BEFORE:" ":AFTER" >"$actual" &&
+ printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - bash color pc mode - detached head' '
+ printf "BEFORE: (${c_red}\${__git_ps1_branch_name}${c_clear}):AFTER\\n(%s...)" $(git log -1 --format="%h" b1^) >expected &&
+ git checkout b1^ &&
+ test_when_finished "git checkout main" &&
+ (
+ GIT_PS1_SHOWCOLORHINTS=y &&
+ __git_ps1 "BEFORE:" ":AFTER" &&
+ printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - bash color pc mode - dirty status indicator - dirty worktree' '
+ printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_red}*${c_clear}):AFTER\\nmain" >expected &&
+ echo "dirty" >file &&
+ test_when_finished "git reset --hard" &&
+ (
+ GIT_PS1_SHOWDIRTYSTATE=y &&
+ GIT_PS1_SHOWCOLORHINTS=y &&
+ __git_ps1 "BEFORE:" ":AFTER" &&
+ printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - bash color pc mode - dirty status indicator - dirty index' '
+ printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_green}+${c_clear}):AFTER\\nmain" >expected &&
+ echo "dirty" >file &&
+ test_when_finished "git reset --hard" &&
+ git add -u &&
+ (
+ GIT_PS1_SHOWDIRTYSTATE=y &&
+ GIT_PS1_SHOWCOLORHINTS=y &&
+ __git_ps1 "BEFORE:" ":AFTER" &&
+ printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - bash color pc mode - dirty status indicator - dirty index and worktree' '
+ printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_red}*${c_clear}${c_green}+${c_clear}):AFTER\\nmain" >expected &&
+ echo "dirty index" >file &&
+ test_when_finished "git reset --hard" &&
+ git add -u &&
+ echo "dirty worktree" >file &&
+ (
+ GIT_PS1_SHOWCOLORHINTS=y &&
+ GIT_PS1_SHOWDIRTYSTATE=y &&
+ __git_ps1 "BEFORE:" ":AFTER" &&
+ printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - bash color pc mode - dirty status indicator - before root commit' '
+ printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_green}#${c_clear}):AFTER\\nmain" >expected &&
+ (
+ GIT_PS1_SHOWDIRTYSTATE=y &&
+ GIT_PS1_SHOWCOLORHINTS=y &&
+ cd otherrepo &&
+ __git_ps1 "BEFORE:" ":AFTER" &&
+ printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - bash color pc mode - inside .git directory' '
+ printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear}):AFTER\\nGIT_DIR!" >expected &&
+ echo "dirty" >file &&
+ test_when_finished "git reset --hard" &&
+ (
+ GIT_PS1_SHOWDIRTYSTATE=y &&
+ GIT_PS1_SHOWCOLORHINTS=y &&
+ cd .git &&
+ __git_ps1 "BEFORE:" ":AFTER" &&
+ printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - bash color pc mode - stash status indicator' '
+ printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_lblue}\$${c_clear}):AFTER\\nmain" >expected &&
+ echo 2 >file &&
+ git stash &&
+ test_when_finished "git stash drop" &&
+ (
+ GIT_PS1_SHOWSTASHSTATE=y &&
+ GIT_PS1_SHOWCOLORHINTS=y &&
+ __git_ps1 "BEFORE:" ":AFTER" &&
+ printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - bash color pc mode - untracked files status indicator' '
+ printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_red}%%${c_clear}):AFTER\\nmain" >expected &&
+ (
+ GIT_PS1_SHOWUNTRACKEDFILES=y &&
+ GIT_PS1_SHOWCOLORHINTS=y &&
+ __git_ps1 "BEFORE:" ":AFTER" &&
+ printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - zsh color pc mode' '
+ printf "BEFORE: (%%F{green}main%%f):AFTER" >expected &&
+ (
+ ZSH_VERSION=5.0.0 &&
+ GIT_PS1_SHOWCOLORHINTS=y &&
+ __git_ps1 "BEFORE:" ":AFTER" &&
+ printf "%s" "$PS1" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - hide if pwd ignored - env var unset, config disabled' '
+ printf " (main)" >expected &&
+ test_config bash.hideIfPwdIgnored false &&
+ (
+ cd ignored_dir &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - hide if pwd ignored - env var unset, config disabled, pc mode' '
+ printf "BEFORE: (\${__git_ps1_branch_name}):AFTER" >expected &&
+ test_config bash.hideIfPwdIgnored false &&
+ (
+ cd ignored_dir &&
+ __git_ps1 "BEFORE:" ":AFTER" &&
+ printf "%s" "$PS1" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - hide if pwd ignored - env var unset, config unset' '
+ printf " (main)" >expected &&
+ (
+ cd ignored_dir &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - hide if pwd ignored - env var unset, config unset, pc mode' '
+ printf "BEFORE: (\${__git_ps1_branch_name}):AFTER" >expected &&
+ (
+ cd ignored_dir &&
+ __git_ps1 "BEFORE:" ":AFTER" &&
+ printf "%s" "$PS1" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - hide if pwd ignored - env var set, config disabled' '
+ printf " (main)" >expected &&
+ test_config bash.hideIfPwdIgnored false &&
+ (
+ cd ignored_dir &&
+ GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - hide if pwd ignored - env var set, config disabled, pc mode' '
+ printf "BEFORE: (\${__git_ps1_branch_name}):AFTER" >expected &&
+ test_config bash.hideIfPwdIgnored false &&
+ (
+ cd ignored_dir &&
+ GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
+ __git_ps1 "BEFORE:" ":AFTER" &&
+ printf "%s" "$PS1" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - hide if pwd ignored - env var set, config unset' '
+ (
+ cd ignored_dir &&
+ GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_must_be_empty "$actual"
+'
+
+test_expect_success 'prompt - hide if pwd ignored - env var set, config unset, pc mode' '
+ printf "BEFORE::AFTER" >expected &&
+ (
+ cd ignored_dir &&
+ GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
+ __git_ps1 "BEFORE:" ":AFTER" &&
+ printf "%s" "$PS1" >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - hide if pwd ignored - inside gitdir' '
+ printf " (GIT_DIR!)" >expected &&
+ (
+ GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
+ cd .git &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_expect_success 'prompt - conflict indicator' '
+ printf " (main|CONFLICT)" >expected &&
+ echo "stash" >file &&
+ git stash &&
+ test_when_finished "git stash drop" &&
+ echo "commit" >file &&
+ git commit -m "commit" file &&
+ test_when_finished "git reset --hard HEAD~" &&
+ test_must_fail git stash apply &&
+ (
+ GIT_PS1_SHOWCONFLICTSTATE="yes" &&
+ __git_ps1 >"$actual"
+ ) &&
+ test_cmp expected "$actual"
+'
+
+test_done
diff --git a/t/test-binary-1.png b/t/test-binary-1.png
new file mode 100644
index 0000000..7b181d1
--- /dev/null
+++ b/t/test-binary-1.png
Binary files differ
diff --git a/t/test-binary-2.png b/t/test-binary-2.png
new file mode 100644
index 0000000..ac22ccb
--- /dev/null
+++ b/t/test-binary-2.png
Binary files differ
diff --git a/t/test-lib-functions.sh b/t/test-lib-functions.sh
new file mode 100644
index 0000000..9c3cf12
--- /dev/null
+++ b/t/test-lib-functions.sh
@@ -0,0 +1,1941 @@
+# Library of functions shared by all tests scripts, included by
+# test-lib.sh.
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see http://www.gnu.org/licenses/ .
+
+# The semantics of the editor variables are that of invoking
+# sh -c "$EDITOR \"$@\"" files ...
+#
+# If our trash directory contains shell metacharacters, they will be
+# interpreted if we just set $EDITOR directly, so do a little dance with
+# environment variables to work around this.
+#
+# In particular, quoting isn't enough, as the path may contain the same quote
+# that we're using.
+test_set_editor () {
+ FAKE_EDITOR="$1"
+ export FAKE_EDITOR
+ EDITOR='"$FAKE_EDITOR"'
+ export EDITOR
+}
+
+# Like test_set_editor but sets GIT_SEQUENCE_EDITOR instead of EDITOR
+test_set_sequence_editor () {
+ FAKE_SEQUENCE_EDITOR="$1"
+ export FAKE_SEQUENCE_EDITOR
+ GIT_SEQUENCE_EDITOR='"$FAKE_SEQUENCE_EDITOR"'
+ export GIT_SEQUENCE_EDITOR
+}
+
+test_decode_color () {
+ awk '
+ function name(n) {
+ if (n == 0) return "RESET";
+ if (n == 1) return "BOLD";
+ if (n == 2) return "FAINT";
+ if (n == 3) return "ITALIC";
+ if (n == 7) return "REVERSE";
+ if (n == 30) return "BLACK";
+ if (n == 31) return "RED";
+ if (n == 32) return "GREEN";
+ if (n == 33) return "YELLOW";
+ if (n == 34) return "BLUE";
+ if (n == 35) return "MAGENTA";
+ if (n == 36) return "CYAN";
+ if (n == 37) return "WHITE";
+ if (n == 40) return "BLACK";
+ if (n == 41) return "BRED";
+ if (n == 42) return "BGREEN";
+ if (n == 43) return "BYELLOW";
+ if (n == 44) return "BBLUE";
+ if (n == 45) return "BMAGENTA";
+ if (n == 46) return "BCYAN";
+ if (n == 47) return "BWHITE";
+ }
+ {
+ while (match($0, /\033\[[0-9;]*m/) != 0) {
+ printf "%s<", substr($0, 1, RSTART-1);
+ codes = substr($0, RSTART+2, RLENGTH-3);
+ if (length(codes) == 0)
+ printf "%s", name(0)
+ else {
+ n = split(codes, ary, ";");
+ sep = "";
+ for (i = 1; i <= n; i++) {
+ printf "%s%s", sep, name(ary[i]);
+ sep = ";"
+ }
+ }
+ printf ">";
+ $0 = substr($0, RSTART + RLENGTH, length($0) - RSTART - RLENGTH + 1);
+ }
+ print
+ }
+ '
+}
+
+lf_to_nul () {
+ perl -pe 'y/\012/\000/'
+}
+
+nul_to_q () {
+ perl -pe 'y/\000/Q/'
+}
+
+q_to_nul () {
+ perl -pe 'y/Q/\000/'
+}
+
+q_to_cr () {
+ tr Q '\015'
+}
+
+q_to_tab () {
+ tr Q '\011'
+}
+
+qz_to_tab_space () {
+ tr QZ '\011\040'
+}
+
+append_cr () {
+ sed -e 's/$/Q/' | tr Q '\015'
+}
+
+remove_cr () {
+ tr '\015' Q | sed -e 's/Q$//'
+}
+
+# In some bourne shell implementations, the "unset" builtin returns
+# nonzero status when a variable to be unset was not set in the first
+# place.
+#
+# Use sane_unset when that should not be considered an error.
+
+sane_unset () {
+ unset "$@"
+ return 0
+}
+
+test_tick () {
+ if test -z "${test_tick+set}"
+ then
+ test_tick=1112911993
+ else
+ test_tick=$(($test_tick + 60))
+ fi
+ GIT_COMMITTER_DATE="$test_tick -0700"
+ GIT_AUTHOR_DATE="$test_tick -0700"
+ export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
+}
+
+# Stop execution and start a shell. This is useful for debugging tests.
+#
+# Be sure to remove all invocations of this command before submitting.
+# WARNING: the shell invoked by this helper does not have the same environment
+# as the one running the tests (shell variables and functions are not
+# available, and the options below further modify the environment). As such,
+# commands copied from a test script might behave differently than when
+# running the test.
+#
+# Usage: test_pause [options]
+# -t
+# Use your original TERM instead of test-lib.sh's "dumb".
+# This usually restores color output in the invoked shell.
+# -s
+# Invoke $SHELL instead of $TEST_SHELL_PATH.
+# -h
+# Use your original HOME instead of test-lib.sh's "$TRASH_DIRECTORY".
+# This allows you to use your regular shell environment and Git aliases.
+# CAUTION: running commands copied from a test script into the paused shell
+# might result in files in your HOME being overwritten.
+# -a
+# Shortcut for -t -s -h
+
+test_pause () {
+ PAUSE_TERM=$TERM &&
+ PAUSE_SHELL=$TEST_SHELL_PATH &&
+ PAUSE_HOME=$HOME &&
+ while test $# != 0
+ do
+ case "$1" in
+ -t)
+ PAUSE_TERM="$USER_TERM"
+ ;;
+ -s)
+ PAUSE_SHELL="$SHELL"
+ ;;
+ -h)
+ PAUSE_HOME="$USER_HOME"
+ ;;
+ -a)
+ PAUSE_TERM="$USER_TERM"
+ PAUSE_SHELL="$SHELL"
+ PAUSE_HOME="$USER_HOME"
+ ;;
+ *)
+ break
+ ;;
+ esac
+ shift
+ done &&
+ TERM="$PAUSE_TERM" HOME="$PAUSE_HOME" "$PAUSE_SHELL" <&6 >&5 2>&7
+}
+
+# Wrap git with a debugger. Adding this to a command can make it easier
+# to understand what is going on in a failing test.
+#
+# Usage: debug [options] <git command>
+# -d <debugger>
+# --debugger=<debugger>
+# Use <debugger> instead of GDB
+# -t
+# Use your original TERM instead of test-lib.sh's "dumb".
+# This usually restores color output in the debugger.
+# WARNING: the command being debugged might behave differently than when
+# running the test.
+#
+# Examples:
+# debug git checkout master
+# debug --debugger=nemiver git $ARGS
+# debug -d "valgrind --tool=memcheck --track-origins=yes" git $ARGS
+debug () {
+ GIT_DEBUGGER=1 &&
+ DEBUG_TERM=$TERM &&
+ while test $# != 0
+ do
+ case "$1" in
+ -t)
+ DEBUG_TERM="$USER_TERM"
+ ;;
+ -d)
+ GIT_DEBUGGER="$2" &&
+ shift
+ ;;
+ --debugger=*)
+ GIT_DEBUGGER="${1#*=}"
+ ;;
+ *)
+ break
+ ;;
+ esac
+ shift
+ done &&
+
+ dotfiles=".gdbinit .lldbinit"
+
+ for dotfile in $dotfiles
+ do
+ dotfile="$USER_HOME/$dotfile" &&
+ test -f "$dotfile" && cp "$dotfile" "$HOME" || :
+ done &&
+
+ TERM="$DEBUG_TERM" GIT_DEBUGGER="${GIT_DEBUGGER}" "$@" <&6 >&5 2>&7 &&
+
+ for dotfile in $dotfiles
+ do
+ rm -f "$HOME/$dotfile"
+ done
+}
+
+# Usage: test_ref_exists [options] <ref>
+#
+# -C <dir>:
+# Run all git commands in directory <dir>
+#
+# This helper function checks whether a reference exists. Symrefs or object IDs
+# will not be resolved. Can be used to check references with bad names.
+test_ref_exists () {
+ local indir=
+
+ while test $# != 0
+ do
+ case "$1" in
+ -C)
+ indir="$2"
+ shift
+ ;;
+ *)
+ break
+ ;;
+ esac
+ shift
+ done &&
+
+ indir=${indir:+"$indir"/} &&
+
+ if test "$#" != 1
+ then
+ BUG "expected exactly one reference"
+ fi &&
+
+ git ${indir:+ -C "$indir"} show-ref --exists "$1"
+}
+
+# Behaves the same as test_ref_exists, except that it checks for the absence of
+# a reference. This is preferable to `! test_ref_exists` as this function is
+# able to distinguish actually-missing references from other, generic errors.
+test_ref_missing () {
+ test_ref_exists "$@"
+ case "$?" in
+ 2)
+ # This is the good case.
+ return 0
+ ;;
+ 0)
+ echo >&4 "test_ref_missing: reference exists"
+ return 1
+ ;;
+ *)
+ echo >&4 "test_ref_missing: generic error"
+ return 1
+ ;;
+ esac
+}
+
+# Usage: test_commit [options] <message> [<file> [<contents> [<tag>]]]
+# -C <dir>:
+# Run all git commands in directory <dir>
+# --notick
+# Do not call test_tick before making a commit
+# --append
+# Use ">>" instead of ">" when writing "<contents>" to "<file>"
+# --printf
+# Use "printf" instead of "echo" when writing "<contents>" to
+# "<file>", use this to write escape sequences such as "\0", a
+# trailing "\n" won't be added automatically. This option
+# supports nothing but the FORMAT of printf(1), i.e. no custom
+# ARGUMENT(s).
+# --signoff
+# Invoke "git commit" with --signoff
+# --author <author>
+# Invoke "git commit" with --author <author>
+# --no-tag
+# Do not tag the resulting commit
+# --annotate
+# Create an annotated tag with "--annotate -m <message>". Calls
+# test_tick between making the commit and tag, unless --notick
+# is given.
+#
+# This will commit a file with the given contents and the given commit
+# message, and tag the resulting commit with the given tag name.
+#
+# <file>, <contents>, and <tag> all default to <message>.
+
+test_commit () {
+ local notick= &&
+ local echo=echo &&
+ local append= &&
+ local author= &&
+ local signoff= &&
+ local indir= &&
+ local tag=light &&
+ while test $# != 0
+ do
+ case "$1" in
+ --notick)
+ notick=yes
+ ;;
+ --printf)
+ echo=printf
+ ;;
+ --append)
+ append=yes
+ ;;
+ --author)
+ author="$2"
+ shift
+ ;;
+ --signoff)
+ signoff="$1"
+ ;;
+ --date)
+ notick=yes
+ GIT_COMMITTER_DATE="$2"
+ GIT_AUTHOR_DATE="$2"
+ shift
+ ;;
+ -C)
+ indir="$2"
+ shift
+ ;;
+ --no-tag)
+ tag=none
+ ;;
+ --annotate)
+ tag=annotate
+ ;;
+ *)
+ break
+ ;;
+ esac
+ shift
+ done &&
+ indir=${indir:+"$indir"/} &&
+ local file=${2:-"$1.t"} &&
+ if test -n "$append"
+ then
+ $echo "${3-$1}" >>"$indir$file"
+ else
+ $echo "${3-$1}" >"$indir$file"
+ fi &&
+ git ${indir:+ -C "$indir"} add -- "$file" &&
+ if test -z "$notick"
+ then
+ test_tick
+ fi &&
+ git ${indir:+ -C "$indir"} commit \
+ ${author:+ --author "$author"} \
+ $signoff -m "$1" &&
+ case "$tag" in
+ none)
+ ;;
+ light)
+ git ${indir:+ -C "$indir"} tag "${4:-$1}"
+ ;;
+ annotate)
+ if test -z "$notick"
+ then
+ test_tick
+ fi &&
+ git ${indir:+ -C "$indir"} tag -a -m "$1" "${4:-$1}"
+ ;;
+ esac
+}
+
+# Call test_merge with the arguments "<message> <commit>", where <commit>
+# can be a tag pointing to the commit-to-merge.
+
+test_merge () {
+ label="$1" &&
+ shift &&
+ test_tick &&
+ git merge -m "$label" "$@" &&
+ git tag "$label"
+}
+
+# Efficiently create <nr> commits, each with a unique number (from 1 to <nr>
+# by default) in the commit message.
+#
+# Usage: test_commit_bulk [options] <nr>
+# -C <dir>:
+# Run all git commands in directory <dir>
+# --ref=<n>:
+# ref on which to create commits (default: HEAD)
+# --start=<n>:
+# number commit messages from <n> (default: 1)
+# --message=<msg>:
+# use <msg> as the commit mesasge (default: "commit %s")
+# --filename=<fn>:
+# modify <fn> in each commit (default: %s.t)
+# --contents=<string>:
+# place <string> in each file (default: "content %s")
+# --id=<string>:
+# shorthand to use <string> and %s in message, filename, and contents
+#
+# The message, filename, and contents strings are evaluated by printf, with the
+# first "%s" replaced by the current commit number. So you can do:
+#
+# test_commit_bulk --filename=file --contents="modification %s"
+#
+# to have every commit touch the same file, but with unique content.
+#
+test_commit_bulk () {
+ tmpfile=.bulk-commit.input
+ indir=.
+ ref=HEAD
+ n=1
+ message='commit %s'
+ filename='%s.t'
+ contents='content %s'
+ while test $# -gt 0
+ do
+ case "$1" in
+ -C)
+ indir=$2
+ shift
+ ;;
+ --ref=*)
+ ref=${1#--*=}
+ ;;
+ --start=*)
+ n=${1#--*=}
+ ;;
+ --message=*)
+ message=${1#--*=}
+ ;;
+ --filename=*)
+ filename=${1#--*=}
+ ;;
+ --contents=*)
+ contents=${1#--*=}
+ ;;
+ --id=*)
+ message="${1#--*=} %s"
+ filename="${1#--*=}-%s.t"
+ contents="${1#--*=} %s"
+ ;;
+ -*)
+ BUG "invalid test_commit_bulk option: $1"
+ ;;
+ *)
+ break
+ ;;
+ esac
+ shift
+ done
+ total=$1
+
+ add_from=
+ if git -C "$indir" rev-parse --quiet --verify "$ref"
+ then
+ add_from=t
+ fi
+
+ while test "$total" -gt 0
+ do
+ test_tick &&
+ echo "commit $ref"
+ printf 'author %s <%s> %s\n' \
+ "$GIT_AUTHOR_NAME" \
+ "$GIT_AUTHOR_EMAIL" \
+ "$GIT_AUTHOR_DATE"
+ printf 'committer %s <%s> %s\n' \
+ "$GIT_COMMITTER_NAME" \
+ "$GIT_COMMITTER_EMAIL" \
+ "$GIT_COMMITTER_DATE"
+ echo "data <<EOF"
+ printf "$message\n" $n
+ echo "EOF"
+ if test -n "$add_from"
+ then
+ echo "from $ref^0"
+ add_from=
+ fi
+ printf "M 644 inline $filename\n" $n
+ echo "data <<EOF"
+ printf "$contents\n" $n
+ echo "EOF"
+ echo
+ n=$((n + 1))
+ total=$((total - 1))
+ done >"$tmpfile"
+
+ git -C "$indir" \
+ -c fastimport.unpacklimit=0 \
+ fast-import <"$tmpfile" || return 1
+
+ # This will be left in place on failure, which may aid debugging.
+ rm -f "$tmpfile"
+
+ # If we updated HEAD, then be nice and update the index and working
+ # tree, too.
+ if test "$ref" = "HEAD"
+ then
+ git -C "$indir" checkout -f HEAD || return 1
+ fi
+
+}
+
+# This function helps systems where core.filemode=false is set.
+# Use it instead of plain 'chmod +x' to set or unset the executable bit
+# of a file in the working directory and add it to the index.
+
+test_chmod () {
+ chmod "$@" &&
+ git update-index --add "--chmod=$@"
+}
+
+# Get the modebits from a file or directory, ignoring the setgid bit (g+s).
+# This bit is inherited by subdirectories at their creation. So we remove it
+# from the returning string to prevent callers from having to worry about the
+# state of the bit in the test directory.
+#
+test_modebits () {
+ ls -ld "$1" | sed -e 's|^\(..........\).*|\1|' \
+ -e 's|^\(......\)S|\1-|' -e 's|^\(......\)s|\1x|'
+}
+
+# Unset a configuration variable, but don't fail if it doesn't exist.
+test_unconfig () {
+ config_dir=
+ if test "$1" = -C
+ then
+ shift
+ config_dir=$1
+ shift
+ fi
+ git ${config_dir:+-C "$config_dir"} config --unset-all "$@"
+ config_status=$?
+ case "$config_status" in
+ 5) # ok, nothing to unset
+ config_status=0
+ ;;
+ esac
+ return $config_status
+}
+
+# Set git config, automatically unsetting it after the test is over.
+test_config () {
+ config_dir=
+ if test "$1" = -C
+ then
+ shift
+ config_dir=$1
+ shift
+ fi
+
+ # If --worktree is provided, use it to configure/unconfigure
+ is_worktree=
+ if test "$1" = --worktree
+ then
+ is_worktree=1
+ shift
+ fi
+
+ test_when_finished "test_unconfig ${config_dir:+-C '$config_dir'} ${is_worktree:+--worktree} '$1'" &&
+ git ${config_dir:+-C "$config_dir"} config ${is_worktree:+--worktree} "$@"
+}
+
+test_config_global () {
+ test_when_finished "test_unconfig --global '$1'" &&
+ git config --global "$@"
+}
+
+write_script () {
+ {
+ echo "#!${2-"$SHELL_PATH"}" &&
+ cat
+ } >"$1" &&
+ chmod +x "$1"
+}
+
+# Usage: test_hook [options] <hook-name> <<-\EOF
+#
+# -C <dir>:
+# Run all git commands in directory <dir>
+# --setup
+# Setup a hook for subsequent tests, i.e. don't remove it in a
+# "test_when_finished"
+# --clobber
+# Overwrite an existing <hook-name>, if it exists. Implies
+# --setup (i.e. the "test_when_finished" is assumed to have been
+# set up already).
+# --disable
+# Disable (chmod -x) an existing <hook-name>, which must exist.
+# --remove
+# Remove (rm -f) an existing <hook-name>, which must exist.
+test_hook () {
+ setup= &&
+ clobber= &&
+ disable= &&
+ remove= &&
+ indir= &&
+ while test $# != 0
+ do
+ case "$1" in
+ -C)
+ indir="$2" &&
+ shift
+ ;;
+ --setup)
+ setup=t
+ ;;
+ --clobber)
+ clobber=t
+ ;;
+ --disable)
+ disable=t
+ ;;
+ --remove)
+ remove=t
+ ;;
+ -*)
+ BUG "invalid argument: $1"
+ ;;
+ *)
+ break
+ ;;
+ esac &&
+ shift
+ done &&
+
+ git_dir=$(git -C "$indir" rev-parse --absolute-git-dir) &&
+ hook_dir="$git_dir/hooks" &&
+ hook_file="$hook_dir/$1" &&
+ if test -n "$disable$remove"
+ then
+ test_path_is_file "$hook_file" &&
+ if test -n "$disable"
+ then
+ chmod -x "$hook_file"
+ elif test -n "$remove"
+ then
+ rm -f "$hook_file"
+ fi &&
+ return 0
+ fi &&
+ if test -z "$clobber"
+ then
+ test_path_is_missing "$hook_file"
+ fi &&
+ if test -z "$setup$clobber"
+ then
+ test_when_finished "rm \"$hook_file\""
+ fi &&
+ write_script "$hook_file"
+}
+
+# Use test_set_prereq to tell that a particular prerequisite is available.
+# The prerequisite can later be checked for in two ways:
+#
+# - Explicitly using test_have_prereq.
+#
+# - Implicitly by specifying the prerequisite tag in the calls to
+# test_expect_{success,failure}
+#
+# The single parameter is the prerequisite tag (a simple word, in all
+# capital letters by convention).
+
+test_unset_prereq () {
+ ! test_have_prereq "$1" ||
+ satisfied_prereq="${satisfied_prereq% $1 *} ${satisfied_prereq#* $1 }"
+}
+
+test_set_prereq () {
+ if test -n "$GIT_TEST_FAIL_PREREQS_INTERNAL"
+ then
+ case "$1" in
+ # The "!" case is handled below with
+ # test_unset_prereq()
+ !*)
+ ;;
+ # List of things we can't easily pretend to not support
+ SYMLINKS)
+ ;;
+ # Inspecting whether GIT_TEST_FAIL_PREREQS is on
+ # should be unaffected.
+ FAIL_PREREQS)
+ ;;
+ *)
+ return
+ esac
+ fi
+
+ case "$1" in
+ !*)
+ test_unset_prereq "${1#!}"
+ ;;
+ *)
+ satisfied_prereq="$satisfied_prereq$1 "
+ ;;
+ esac
+}
+satisfied_prereq=" "
+lazily_testable_prereq= lazily_tested_prereq=
+
+# Usage: test_lazy_prereq PREREQ 'script'
+test_lazy_prereq () {
+ lazily_testable_prereq="$lazily_testable_prereq$1 "
+ eval test_prereq_lazily_$1=\$2
+}
+
+test_run_lazy_prereq_ () {
+ script='
+mkdir -p "$TRASH_DIRECTORY/prereq-test-dir-'"$1"'" &&
+(
+ cd "$TRASH_DIRECTORY/prereq-test-dir-'"$1"'" &&'"$2"'
+)'
+ say >&3 "checking prerequisite: $1"
+ say >&3 "$script"
+ test_eval_ "$script"
+ eval_ret=$?
+ rm -rf "$TRASH_DIRECTORY/prereq-test-dir-$1"
+ if test "$eval_ret" = 0; then
+ say >&3 "prerequisite $1 ok"
+ else
+ say >&3 "prerequisite $1 not satisfied"
+ fi
+ return $eval_ret
+}
+
+test_have_prereq () {
+ # prerequisites can be concatenated with ','
+ save_IFS=$IFS
+ IFS=,
+ set -- $*
+ IFS=$save_IFS
+
+ total_prereq=0
+ ok_prereq=0
+ missing_prereq=
+
+ for prerequisite
+ do
+ case "$prerequisite" in
+ !*)
+ negative_prereq=t
+ prerequisite=${prerequisite#!}
+ ;;
+ *)
+ negative_prereq=
+ esac
+
+ case " $lazily_tested_prereq " in
+ *" $prerequisite "*)
+ ;;
+ *)
+ case " $lazily_testable_prereq " in
+ *" $prerequisite "*)
+ eval "script=\$test_prereq_lazily_$prerequisite" &&
+ if test_run_lazy_prereq_ "$prerequisite" "$script"
+ then
+ test_set_prereq $prerequisite
+ fi
+ lazily_tested_prereq="$lazily_tested_prereq$prerequisite "
+ esac
+ ;;
+ esac
+
+ total_prereq=$(($total_prereq + 1))
+ case "$satisfied_prereq" in
+ *" $prerequisite "*)
+ satisfied_this_prereq=t
+ ;;
+ *)
+ satisfied_this_prereq=
+ esac
+
+ case "$satisfied_this_prereq,$negative_prereq" in
+ t,|,t)
+ ok_prereq=$(($ok_prereq + 1))
+ ;;
+ *)
+ # Keep a list of missing prerequisites; restore
+ # the negative marker if necessary.
+ prerequisite=${negative_prereq:+!}$prerequisite
+
+ # Abort if this prereq was marked as required
+ if test -n "$GIT_TEST_REQUIRE_PREREQ"
+ then
+ case " $GIT_TEST_REQUIRE_PREREQ " in
+ *" $prerequisite "*)
+ BAIL_OUT "required prereq $prerequisite failed"
+ ;;
+ esac
+ fi
+
+ if test -z "$missing_prereq"
+ then
+ missing_prereq=$prerequisite
+ else
+ missing_prereq="$prerequisite,$missing_prereq"
+ fi
+ esac
+ done
+
+ test $total_prereq = $ok_prereq
+}
+
+test_declared_prereq () {
+ case ",$test_prereq," in
+ *,$1,*)
+ return 0
+ ;;
+ esac
+ return 1
+}
+
+test_verify_prereq () {
+ test -z "$test_prereq" ||
+ expr >/dev/null "$test_prereq" : '[A-Z0-9_,!]*$' ||
+ BUG "'$test_prereq' does not look like a prereq"
+}
+
+test_expect_failure () {
+ test_start_ "$@"
+ test "$#" = 3 && { test_prereq=$1; shift; } || test_prereq=
+ test "$#" = 2 ||
+ BUG "not 2 or 3 parameters to test-expect-failure"
+ test_verify_prereq
+ export test_prereq
+ if ! test_skip "$@"
+ then
+ test -n "$test_skip_test_preamble" ||
+ say >&3 "checking known breakage of $TEST_NUMBER.$test_count '$1': $2"
+ if test_run_ "$2" expecting_failure
+ then
+ test_known_broken_ok_ "$1"
+ else
+ test_known_broken_failure_ "$1"
+ fi
+ fi
+ test_finish_
+}
+
+test_expect_success () {
+ test_start_ "$@"
+ test "$#" = 3 && { test_prereq=$1; shift; } || test_prereq=
+ test "$#" = 2 ||
+ BUG "not 2 or 3 parameters to test-expect-success"
+ test_verify_prereq
+ export test_prereq
+ if ! test_skip "$@"
+ then
+ test -n "$test_skip_test_preamble" ||
+ say >&3 "expecting success of $TEST_NUMBER.$test_count '$1': $2"
+ if test_run_ "$2"
+ then
+ test_ok_ "$1"
+ else
+ test_failure_ "$@"
+ fi
+ fi
+ test_finish_
+}
+
+# debugging-friendly alternatives to "test [-f|-d|-e]"
+# The commands test the existence or non-existence of $1
+test_path_is_file () {
+ test "$#" -ne 1 && BUG "1 param"
+ if ! test -f "$1"
+ then
+ echo "File $1 doesn't exist"
+ false
+ fi
+}
+
+test_path_is_file_not_symlink () {
+ test "$#" -ne 1 && BUG "1 param"
+ test_path_is_file "$1" &&
+ if test -h "$1"
+ then
+ echo "$1 shouldn't be a symbolic link"
+ false
+ fi
+}
+
+test_path_is_dir () {
+ test "$#" -ne 1 && BUG "1 param"
+ if ! test -d "$1"
+ then
+ echo "Directory $1 doesn't exist"
+ false
+ fi
+}
+
+test_path_is_dir_not_symlink () {
+ test "$#" -ne 1 && BUG "1 param"
+ test_path_is_dir "$1" &&
+ if test -h "$1"
+ then
+ echo "$1 shouldn't be a symbolic link"
+ false
+ fi
+}
+
+test_path_exists () {
+ test "$#" -ne 1 && BUG "1 param"
+ if ! test -e "$1"
+ then
+ echo "Path $1 doesn't exist"
+ false
+ fi
+}
+
+test_path_is_symlink () {
+ test "$#" -ne 1 && BUG "1 param"
+ if ! test -h "$1"
+ then
+ echo "Symbolic link $1 doesn't exist"
+ false
+ fi
+}
+
+test_path_is_executable () {
+ test "$#" -ne 1 && BUG "1 param"
+ if ! test -x "$1"
+ then
+ echo "$1 is not executable"
+ false
+ fi
+}
+
+# Check if the directory exists and is empty as expected, barf otherwise.
+test_dir_is_empty () {
+ test "$#" -ne 1 && BUG "1 param"
+ test_path_is_dir "$1" &&
+ if test -n "$(ls -a1 "$1" | grep -E -v '^\.\.?$')"
+ then
+ echo "Directory '$1' is not empty, it contains:"
+ ls -la "$1"
+ return 1
+ fi
+}
+
+# Check if the file exists and has a size greater than zero
+test_file_not_empty () {
+ test "$#" = 2 && BUG "2 param"
+ if ! test -s "$1"
+ then
+ echo "'$1' is not a non-empty file."
+ false
+ fi
+}
+
+test_path_is_missing () {
+ test "$#" -ne 1 && BUG "1 param"
+ if test -e "$1"
+ then
+ echo "Path exists:"
+ ls -ld "$1"
+ false
+ fi
+}
+
+# test_line_count checks that a file has the number of lines it
+# ought to. For example:
+#
+# test_expect_success 'produce exactly one line of output' '
+# do something >output &&
+# test_line_count = 1 output
+# '
+#
+# is like "test $(wc -l <output) = 1" except that it passes the
+# output through when the number of lines is wrong.
+
+test_line_count () {
+ if test $# != 3
+ then
+ BUG "not 3 parameters to test_line_count"
+ elif ! test $(wc -l <"$3") "$1" "$2"
+ then
+ echo "test_line_count: line count for $3 !$1 $2"
+ cat "$3"
+ return 1
+ fi
+}
+
+# SYNOPSIS:
+# test_stdout_line_count <bin-ops> <value> <cmd> [<args>...]
+#
+# test_stdout_line_count checks that the output of a command has the number
+# of lines it ought to. For example:
+#
+# test_stdout_line_count = 3 git ls-files -u
+# test_stdout_line_count -gt 10 ls
+test_stdout_line_count () {
+ local ops val trashdir &&
+ if test "$#" -le 3
+ then
+ BUG "expect 3 or more arguments"
+ fi &&
+ ops="$1" &&
+ val="$2" &&
+ shift 2 &&
+ if ! trashdir="$(git rev-parse --git-dir)/trash"; then
+ BUG "expect to be run inside a worktree"
+ fi &&
+ mkdir -p "$trashdir" &&
+ "$@" >"$trashdir/output" &&
+ test_line_count "$ops" "$val" "$trashdir/output"
+}
+
+
+test_file_size () {
+ test "$#" -ne 1 && BUG "1 param"
+ test-tool path-utils file-size "$1"
+}
+
+# Returns success if a comma separated string of keywords ($1) contains a
+# given keyword ($2).
+# Examples:
+# `list_contains "foo,bar" bar` returns 0
+# `list_contains "foo" bar` returns 1
+
+list_contains () {
+ case ",$1," in
+ *,$2,*)
+ return 0
+ ;;
+ esac
+ return 1
+}
+
+# Returns success if the arguments indicate that a command should be
+# accepted by test_must_fail(). If the command is run with env, the env
+# and its corresponding variable settings will be stripped before we
+# test the command being run.
+test_must_fail_acceptable () {
+ if test "$1" = "env"
+ then
+ shift
+ while test $# -gt 0
+ do
+ case "$1" in
+ *?=*)
+ shift
+ ;;
+ *)
+ break
+ ;;
+ esac
+ done
+ fi
+
+ case "$1" in
+ git|__git*|scalar|test-tool|test_terminal)
+ return 0
+ ;;
+ *)
+ return 1
+ ;;
+ esac
+}
+
+# This is not among top-level (test_expect_success | test_expect_failure)
+# but is a prefix that can be used in the test script, like:
+#
+# test_expect_success 'complain and die' '
+# do something &&
+# do something else &&
+# test_must_fail git checkout ../outerspace
+# '
+#
+# Writing this as "! git checkout ../outerspace" is wrong, because
+# the failure could be due to a segv. We want a controlled failure.
+#
+# Accepts the following options:
+#
+# ok=<signal-name>[,<...>]:
+# Don't treat an exit caused by the given signal as error.
+# Multiple signals can be specified as a comma separated list.
+# Currently recognized signal names are: sigpipe, success.
+# (Don't use 'success', use 'test_might_fail' instead.)
+#
+# Do not use this to run anything but "git" and other specific testable
+# commands (see test_must_fail_acceptable()). We are not in the
+# business of vetting system supplied commands -- in other words, this
+# is wrong:
+#
+# test_must_fail grep pattern output
+#
+# Instead use '!':
+#
+# ! grep pattern output
+
+test_must_fail () {
+ case "$1" in
+ ok=*)
+ _test_ok=${1#ok=}
+ shift
+ ;;
+ *)
+ _test_ok=
+ ;;
+ esac
+ if ! test_must_fail_acceptable "$@"
+ then
+ echo >&7 "test_must_fail: only 'git' is allowed: $*"
+ return 1
+ fi
+ "$@" 2>&7
+ exit_code=$?
+ if test $exit_code -eq 0 && ! list_contains "$_test_ok" success
+ then
+ echo >&4 "test_must_fail: command succeeded: $*"
+ return 1
+ elif test_match_signal 13 $exit_code && list_contains "$_test_ok" sigpipe
+ then
+ return 0
+ elif test $exit_code -gt 129 && test $exit_code -le 192
+ then
+ echo >&4 "test_must_fail: died by signal $(($exit_code - 128)): $*"
+ return 1
+ elif test $exit_code -eq 127
+ then
+ echo >&4 "test_must_fail: command not found: $*"
+ return 1
+ elif test $exit_code -eq 126
+ then
+ echo >&4 "test_must_fail: valgrind error: $*"
+ return 1
+ fi
+ return 0
+} 7>&2 2>&4
+
+# Similar to test_must_fail, but tolerates success, too. This is
+# meant to be used in contexts like:
+#
+# test_expect_success 'some command works without configuration' '
+# test_might_fail git config --unset all.configuration &&
+# do something
+# '
+#
+# Writing "git config --unset all.configuration || :" would be wrong,
+# because we want to notice if it fails due to segv.
+#
+# Accepts the same options as test_must_fail.
+
+test_might_fail () {
+ test_must_fail ok=success "$@" 2>&7
+} 7>&2 2>&4
+
+# Similar to test_must_fail and test_might_fail, but check that a
+# given command exited with a given exit code. Meant to be used as:
+#
+# test_expect_success 'Merge with d/f conflicts' '
+# test_expect_code 1 git merge "merge msg" B master
+# '
+
+test_expect_code () {
+ want_code=$1
+ shift
+ "$@" 2>&7
+ exit_code=$?
+ if test $exit_code = $want_code
+ then
+ return 0
+ fi
+
+ echo >&4 "test_expect_code: command exited with $exit_code, we wanted $want_code $*"
+ return 1
+} 7>&2 2>&4
+
+# test_cmp is a helper function to compare actual and expected output.
+# You can use it like:
+#
+# test_expect_success 'foo works' '
+# echo expected >expected &&
+# foo >actual &&
+# test_cmp expected actual
+# '
+#
+# This could be written as either "cmp" or "diff -u", but:
+# - cmp's output is not nearly as easy to read as diff -u
+# - not all diff versions understand "-u"
+
+test_cmp () {
+ test "$#" -ne 2 && BUG "2 param"
+ eval "$GIT_TEST_CMP" '"$@"'
+}
+
+# Check that the given config key has the expected value.
+#
+# test_cmp_config [-C <dir>] <expected-value>
+# [<git-config-options>...] <config-key>
+#
+# for example to check that the value of core.bar is foo
+#
+# test_cmp_config foo core.bar
+#
+test_cmp_config () {
+ local GD &&
+ if test "$1" = "-C"
+ then
+ shift &&
+ GD="-C $1" &&
+ shift
+ fi &&
+ printf "%s\n" "$1" >expect.config &&
+ shift &&
+ git $GD config "$@" >actual.config &&
+ test_cmp expect.config actual.config
+}
+
+# test_cmp_bin - helper to compare binary files
+
+test_cmp_bin () {
+ test "$#" -ne 2 && BUG "2 param"
+ cmp "$@"
+}
+
+# Deprecated - do not use this in new code
+test_i18ngrep () {
+ test_grep "$@"
+}
+
+test_grep () {
+ eval "last_arg=\${$#}"
+
+ test -f "$last_arg" ||
+ BUG "test_grep requires a file to read as the last parameter"
+
+ if test $# -lt 2 ||
+ { test "x!" = "x$1" && test $# -lt 3 ; }
+ then
+ BUG "too few parameters to test_i18ngrep"
+ fi
+
+ if test "x!" = "x$1"
+ then
+ shift
+ ! grep "$@" && return 0
+
+ echo >&4 "error: '! grep $@' did find a match in:"
+ else
+ grep "$@" && return 0
+
+ echo >&4 "error: 'grep $@' didn't find a match in:"
+ fi
+
+ if test -s "$last_arg"
+ then
+ cat >&4 "$last_arg"
+ else
+ echo >&4 "<File '$last_arg' is empty>"
+ fi
+
+ return 1
+}
+
+# Check if the file expected to be empty is indeed empty, and barfs
+# otherwise.
+
+test_must_be_empty () {
+ test "$#" -ne 1 && BUG "1 param"
+ test_path_is_file "$1" &&
+ if test -s "$1"
+ then
+ echo "'$1' is not empty, it contains:"
+ cat "$1"
+ return 1
+ fi
+}
+
+# Tests that its two parameters refer to the same revision, or if '!' is
+# provided first, that its other two parameters refer to different
+# revisions.
+test_cmp_rev () {
+ local op='=' wrong_result=different
+
+ if test $# -ge 1 && test "x$1" = 'x!'
+ then
+ op='!='
+ wrong_result='the same'
+ shift
+ fi
+ if test $# != 2
+ then
+ BUG "test_cmp_rev requires two revisions, but got $#"
+ else
+ local r1 r2
+ r1=$(git rev-parse --verify "$1") &&
+ r2=$(git rev-parse --verify "$2") || return 1
+
+ if ! test "$r1" "$op" "$r2"
+ then
+ cat >&4 <<-EOF
+ error: two revisions point to $wrong_result objects:
+ '$1': $r1
+ '$2': $r2
+ EOF
+ return 1
+ fi
+ fi
+}
+
+# Tests that a commit message matches the expected text
+#
+# Usage: test_commit_message <rev> [-m <msg> | <file>]
+#
+# When using "-m" <msg> will have a line feed appended. If the second
+# argument is omitted then the expected message is read from stdin.
+
+test_commit_message () {
+ local msg_file=expect.msg
+
+ case $# in
+ 3)
+ if test "$2" = "-m"
+ then
+ printf "%s\n" "$3" >"$msg_file"
+ else
+ BUG "Usage: test_commit_message <rev> [-m <message> | <file>]"
+ fi
+ ;;
+ 2)
+ msg_file="$2"
+ ;;
+ 1)
+ cat >"$msg_file"
+ ;;
+ *)
+ BUG "Usage: test_commit_message <rev> [-m <message> | <file>]"
+ ;;
+ esac
+ git show --no-patch --pretty=format:%B "$1" -- >actual.msg &&
+ test_cmp "$msg_file" actual.msg
+}
+
+# Compare paths respecting core.ignoreCase
+test_cmp_fspath () {
+ if test "x$1" = "x$2"
+ then
+ return 0
+ fi
+
+ if test true != "$(git config --get --type=bool core.ignorecase)"
+ then
+ return 1
+ fi
+
+ test "x$(echo "$1" | tr A-Z a-z)" = "x$(echo "$2" | tr A-Z a-z)"
+}
+
+# Print a sequence of integers in increasing order, either with
+# two arguments (start and end):
+#
+# test_seq 1 5 -- outputs 1 2 3 4 5 one line at a time
+#
+# or with one argument (end), in which case it starts counting
+# from 1.
+
+test_seq () {
+ case $# in
+ 1) set 1 "$@" ;;
+ 2) ;;
+ *) BUG "not 1 or 2 parameters to test_seq" ;;
+ esac
+ test_seq_counter__=$1
+ while test "$test_seq_counter__" -le "$2"
+ do
+ echo "$test_seq_counter__"
+ test_seq_counter__=$(( $test_seq_counter__ + 1 ))
+ done
+}
+
+# This function can be used to schedule some commands to be run
+# unconditionally at the end of the test to restore sanity:
+#
+# test_expect_success 'test core.capslock' '
+# git config core.capslock true &&
+# test_when_finished "git config --unset core.capslock" &&
+# hello world
+# '
+#
+# That would be roughly equivalent to
+#
+# test_expect_success 'test core.capslock' '
+# git config core.capslock true &&
+# hello world
+# git config --unset core.capslock
+# '
+#
+# except that the greeting and config --unset must both succeed for
+# the test to pass.
+#
+# Note that under --immediate mode, no clean-up is done to help diagnose
+# what went wrong.
+
+test_when_finished () {
+ # We cannot detect when we are in a subshell in general, but by
+ # doing so on Bash is better than nothing (the test will
+ # silently pass on other shells).
+ test "${BASH_SUBSHELL-0}" = 0 ||
+ BUG "test_when_finished does nothing in a subshell"
+ test_cleanup="{ $*
+ } && (exit \"\$eval_ret\"); eval_ret=\$?; $test_cleanup"
+}
+
+# This function can be used to schedule some commands to be run
+# unconditionally at the end of the test script, e.g. to stop a daemon:
+#
+# test_expect_success 'test git daemon' '
+# git daemon &
+# daemon_pid=$! &&
+# test_atexit 'kill $daemon_pid' &&
+# hello world
+# '
+#
+# The commands will be executed before the trash directory is removed,
+# i.e. the atexit commands will still be able to access any pidfiles or
+# socket files.
+#
+# Note that these commands will be run even when a test script run
+# with '--immediate' fails. Be careful with your atexit commands to
+# minimize any changes to the failed state.
+
+test_atexit () {
+ # We cannot detect when we are in a subshell in general, but by
+ # doing so on Bash is better than nothing (the test will
+ # silently pass on other shells).
+ test "${BASH_SUBSHELL-0}" = 0 ||
+ BUG "test_atexit does nothing in a subshell"
+ test_atexit_cleanup="{ $*
+ } && (exit \"\$eval_ret\"); eval_ret=\$?; $test_atexit_cleanup"
+}
+
+# Deprecated wrapper for "git init", use "git init" directly instead
+# Usage: test_create_repo <directory>
+test_create_repo () {
+ git init "$@"
+}
+
+# This function helps on symlink challenged file systems when it is not
+# important that the file system entry is a symbolic link.
+# Use test_ln_s_add instead of "ln -s x y && git add y" to add a
+# symbolic link entry y to the index.
+
+test_ln_s_add () {
+ if test_have_prereq SYMLINKS
+ then
+ ln -s "$1" "$2" &&
+ git update-index --add "$2"
+ else
+ printf '%s' "$1" >"$2" &&
+ ln_s_obj=$(git hash-object -w "$2") &&
+ git update-index --add --cacheinfo 120000 $ln_s_obj "$2" &&
+ # pick up stat info from the file
+ git update-index "$2"
+ fi
+}
+
+# This function writes out its parameters, one per line
+test_write_lines () {
+ printf "%s\n" "$@"
+}
+
+perl () {
+ command "$PERL_PATH" "$@" 2>&7
+} 7>&2 2>&4
+
+# Given the name of an environment variable with a bool value, normalize
+# its value to a 0 (true) or 1 (false or empty string) return code.
+#
+# test_bool_env GIT_TEST_HTTPD <default-value>
+#
+# Return with code corresponding to the given default value if the variable
+# is unset.
+# Abort the test script if either the value of the variable or the default
+# are not valid bool values.
+
+test_bool_env () {
+ if test $# != 2
+ then
+ BUG "test_bool_env requires two parameters (variable name and default value)"
+ fi
+
+ test-tool env-helper --type=bool --default="$2" --exit-code "$1"
+ ret=$?
+ case $ret in
+ 0|1) # unset or valid bool value
+ ;;
+ *) # invalid bool value or something unexpected
+ error >&7 "test_bool_env requires bool values both for \$$1 and for the default fallback"
+ ;;
+ esac
+ return $ret
+}
+
+# Exit the test suite, either by skipping all remaining tests or by
+# exiting with an error. If our prerequisite variable $1 falls back
+# on a default assume we were opportunistically trying to set up some
+# tests and we skip. If it is explicitly "true", then we report a failure.
+#
+# The error/skip message should be given by $2.
+#
+test_skip_or_die () {
+ if ! test_bool_env "$1" false
+ then
+ skip_all=$2
+ test_done
+ fi
+ error "$2"
+}
+
+# Like "env FOO=BAR some-program", but run inside a subshell, which means
+# it also works for shell functions (though those functions cannot impact
+# the environment outside of the test_env invocation).
+test_env () {
+ (
+ while test $# -gt 0
+ do
+ case "$1" in
+ *=*)
+ eval "${1%%=*}=\${1#*=}"
+ eval "export ${1%%=*}"
+ shift
+ ;;
+ *)
+ "$@" 2>&7
+ exit
+ ;;
+ esac
+ done
+ )
+} 7>&2 2>&4
+
+# Returns true if the numeric exit code in "$2" represents the expected signal
+# in "$1". Signals should be given numerically.
+test_match_signal () {
+ if test "$2" = "$((128 + $1))"
+ then
+ # POSIX
+ return 0
+ elif test "$2" = "$((256 + $1))"
+ then
+ # ksh
+ return 0
+ fi
+ return 1
+}
+
+# Read up to "$1" bytes (or to EOF) from stdin and write them to stdout.
+test_copy_bytes () {
+ perl -e '
+ my $len = $ARGV[1];
+ while ($len > 0) {
+ my $s;
+ my $nread = sysread(STDIN, $s, $len);
+ die "cannot read: $!" unless defined($nread);
+ last unless $nread;
+ print $s;
+ $len -= $nread;
+ }
+ ' - "$1"
+}
+
+# run "$@" inside a non-git directory
+nongit () {
+ test -d non-repo ||
+ mkdir non-repo ||
+ return 1
+
+ (
+ GIT_CEILING_DIRECTORIES=$(pwd) &&
+ export GIT_CEILING_DIRECTORIES &&
+ cd non-repo &&
+ "$@" 2>&7
+ )
+} 7>&2 2>&4
+
+# These functions are historical wrappers around "test-tool pkt-line"
+# for older tests. Use "test-tool pkt-line" itself in new tests.
+packetize () {
+ if test $# -gt 0
+ then
+ packet="$*"
+ printf '%04x%s' "$((4 + ${#packet}))" "$packet"
+ else
+ test-tool pkt-line pack
+ fi
+}
+
+packetize_raw () {
+ test-tool pkt-line pack-raw-stdin
+}
+
+depacketize () {
+ test-tool pkt-line unpack
+}
+
+# Converts base-16 data into base-8. The output is given as a sequence of
+# escaped octals, suitable for consumption by 'printf'.
+hex2oct () {
+ perl -ne 'printf "\\%03o", hex for /../g'
+}
+
+# Set the hash algorithm in use to $1. Only useful when testing the testsuite.
+test_set_hash () {
+ test_hash_algo="$1"
+}
+
+# Detect the hash algorithm in use.
+test_detect_hash () {
+ test_hash_algo="${GIT_TEST_DEFAULT_HASH:-sha1}"
+}
+
+# Load common hash metadata and common placeholder object IDs for use with
+# test_oid.
+test_oid_init () {
+ test -n "$test_hash_algo" || test_detect_hash &&
+ test_oid_cache <"$TEST_DIRECTORY/oid-info/hash-info" &&
+ test_oid_cache <"$TEST_DIRECTORY/oid-info/oid"
+}
+
+# Load key-value pairs from stdin suitable for use with test_oid. Blank lines
+# and lines starting with "#" are ignored. Keys must be shell identifier
+# characters.
+#
+# Examples:
+# rawsz sha1:20
+# rawsz sha256:32
+test_oid_cache () {
+ local tag rest k v &&
+
+ { test -n "$test_hash_algo" || test_detect_hash; } &&
+ while read tag rest
+ do
+ case $tag in
+ \#*)
+ continue;;
+ ?*)
+ # non-empty
+ ;;
+ *)
+ # blank line
+ continue;;
+ esac &&
+
+ k="${rest%:*}" &&
+ v="${rest#*:}" &&
+
+ if ! expr "$k" : '[a-z0-9][a-z0-9]*$' >/dev/null
+ then
+ BUG 'bad hash algorithm'
+ fi &&
+ eval "test_oid_${k}_$tag=\"\$v\""
+ done
+}
+
+# Look up a per-hash value based on a key ($1). The value must have been loaded
+# by test_oid_init or test_oid_cache.
+test_oid () {
+ local algo="${test_hash_algo}" &&
+
+ case "$1" in
+ --hash=*)
+ algo="${1#--hash=}" &&
+ shift;;
+ *)
+ ;;
+ esac &&
+
+ local var="test_oid_${algo}_$1" &&
+
+ # If the variable is unset, we must be missing an entry for this
+ # key-hash pair, so exit with an error.
+ if eval "test -z \"\${$var+set}\""
+ then
+ BUG "undefined key '$1'"
+ fi &&
+ eval "printf '%s\n' \"\${$var}\""
+}
+
+# Insert a slash into an object ID so it can be used to reference a location
+# under ".git/objects". For example, "deadbeef..." becomes "de/adbeef..".
+test_oid_to_path () {
+ local basename=${1#??}
+ echo "${1%$basename}/$basename"
+}
+
+# Parse oids from git ls-files --staged output
+test_parse_ls_files_stage_oids () {
+ awk '{print $2}' -
+}
+
+# Parse oids from git ls-tree output
+test_parse_ls_tree_oids () {
+ awk '{print $3}' -
+}
+
+# Choose a port number based on the test script's number and store it in
+# the given variable name, unless that variable already contains a number.
+test_set_port () {
+ local var=$1 port
+
+ if test $# -ne 1 || test -z "$var"
+ then
+ BUG "test_set_port requires a variable name"
+ fi
+
+ eval port=\$$var
+ case "$port" in
+ "")
+ # No port is set in the given env var, use the test
+ # number as port number instead.
+ # Remove not only the leading 't', but all leading zeros
+ # as well, so the arithmetic below won't (mis)interpret
+ # a test number like '0123' as an octal value.
+ port=${this_test#${this_test%%[1-9]*}}
+ if test "${port:-0}" -lt 1024
+ then
+ # root-only port, use a larger one instead.
+ port=$(($port + 10000))
+ fi
+ ;;
+ *[!0-9]*|0*)
+ error >&7 "invalid port number: $port"
+ ;;
+ *)
+ # The user has specified the port.
+ ;;
+ esac
+
+ # Make sure that parallel '--stress' test jobs get different
+ # ports.
+ port=$(($port + ${GIT_TEST_STRESS_JOB_NR:-0}))
+ eval $var=$port
+}
+
+# Tests for the hidden file attribute on Windows
+test_path_is_hidden () {
+ test_have_prereq MINGW ||
+ BUG "test_path_is_hidden can only be used on Windows"
+
+ # Use the output of `attrib`, ignore the absolute path
+ case "$("$SYSTEMROOT"/system32/attrib "$1")" in *H*?:*) return 0;; esac
+ return 1
+}
+
+# Poor man's URI escaping. Good enough for the test suite whose trash
+# directory has a space in it. See 93c3fcbe4d4 (git-svn: attempt to
+# mimic SVN 1.7 URL canonicalization, 2012-07-28) for prior art.
+test_uri_escape() {
+ sed 's/ /%20/g'
+}
+
+# Check that the given command was invoked as part of the
+# trace2-format trace on stdin.
+#
+# test_subcommand [!] <command> <args>... < <trace>
+#
+# For example, to look for an invocation of "git upload-pack
+# /path/to/repo"
+#
+# GIT_TRACE2_EVENT=event.log git fetch ... &&
+# test_subcommand git upload-pack "$PATH" <event.log
+#
+# If the first parameter passed is !, this instead checks that
+# the given command was not called.
+#
+test_subcommand () {
+ local negate=
+ if test "$1" = "!"
+ then
+ negate=t
+ shift
+ fi
+
+ local expr=$(printf '"%s",' "$@")
+ expr="${expr%,}"
+
+ if test -n "$negate"
+ then
+ ! grep "\[$expr\]"
+ else
+ grep "\[$expr\]"
+ fi
+}
+
+# Check that the given command was invoked as part of the
+# trace2-format trace on stdin.
+#
+# test_region [!] <category> <label> git <command> <args>...
+#
+# For example, to look for trace2_region_enter("index", "do_read_index", repo)
+# in an invocation of "git checkout HEAD~1", run
+#
+# GIT_TRACE2_EVENT="$(pwd)/trace.txt" GIT_TRACE2_EVENT_NESTING=10 \
+# git checkout HEAD~1 &&
+# test_region index do_read_index <trace.txt
+#
+# If the first parameter passed is !, this instead checks that
+# the given region was not entered.
+#
+test_region () {
+ local expect_exit=0
+ if test "$1" = "!"
+ then
+ expect_exit=1
+ shift
+ fi
+
+ grep -e '"region_enter".*"category":"'"$1"'","label":"'"$2"\" "$3"
+ exitcode=$?
+
+ if test $exitcode != $expect_exit
+ then
+ return 1
+ fi
+
+ grep -e '"region_leave".*"category":"'"$1"'","label":"'"$2"\" "$3"
+ exitcode=$?
+
+ if test $exitcode != $expect_exit
+ then
+ return 1
+ fi
+
+ return 0
+}
+
+# Given a GIT_TRACE2_EVENT log over stdin, writes to stdout a list of URLs
+# sent to git-remote-https child processes.
+test_remote_https_urls() {
+ grep -e '"event":"child_start".*"argv":\["git-remote-https",".*"\]' |
+ sed -e 's/{"event":"child_start".*"argv":\["git-remote-https","//g' \
+ -e 's/"\]}//g'
+}
+
+# Print the destination of symlink(s) provided as arguments. Basically
+# the same as the readlink command, but it's not available everywhere.
+test_readlink () {
+ perl -le 'print readlink($_) for @ARGV' "$@"
+}
+
+# Set mtime to a fixed "magic" timestamp in mid February 2009, before we
+# run an operation that may or may not touch the file. If the file was
+# touched, its timestamp will not accidentally have such an old timestamp,
+# as long as your filesystem clock is reasonably correct. To verify the
+# timestamp, follow up with test_is_magic_mtime.
+#
+# An optional increment to the magic timestamp may be specified as second
+# argument.
+test_set_magic_mtime () {
+ local inc=${2:-0} &&
+ local mtime=$((1234567890 + $inc)) &&
+ test-tool chmtime =$mtime "$1" &&
+ test_is_magic_mtime "$1" $inc
+}
+
+# Test whether the given file has the "magic" mtime set. This is meant to
+# be used in combination with test_set_magic_mtime.
+#
+# An optional increment to the magic timestamp may be specified as second
+# argument. Usually, this should be the same increment which was used for
+# the associated test_set_magic_mtime.
+test_is_magic_mtime () {
+ local inc=${2:-0} &&
+ local mtime=$((1234567890 + $inc)) &&
+ echo $mtime >.git/test-mtime-expect &&
+ test-tool chmtime --get "$1" >.git/test-mtime-actual &&
+ test_cmp .git/test-mtime-expect .git/test-mtime-actual
+ local ret=$?
+ rm -f .git/test-mtime-expect
+ rm -f .git/test-mtime-actual
+ return $ret
+}
+
+# Given two filenames, parse both using 'git config --list --file'
+# and compare the sorted output of those commands. Useful when
+# wanting to ignore whitespace differences and sorting concerns.
+test_cmp_config_output () {
+ git config --list --file="$1" >config-expect &&
+ git config --list --file="$2" >config-actual &&
+ sort config-expect >sorted-expect &&
+ sort config-actual >sorted-actual &&
+ test_cmp sorted-expect sorted-actual
+}
+
+# Given a filename, extract its trailing hash as a hex string
+test_trailing_hash () {
+ local file="$1" &&
+ tail -c $(test_oid rawsz) "$file" |
+ test-tool hexdump |
+ sed "s/ //g"
+}
diff --git a/t/test-lib-github-workflow-markup.sh b/t/test-lib-github-workflow-markup.sh
new file mode 100644
index 0000000..9c5339c
--- /dev/null
+++ b/t/test-lib-github-workflow-markup.sh
@@ -0,0 +1,56 @@
+# Library of functions to mark up test scripts' output suitable for
+# pretty-printing it in GitHub workflows.
+#
+# Copyright (c) 2022 Johannes Schindelin
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see http://www.gnu.org/licenses/ .
+#
+# The idea is for `test-lib.sh` to source this file when run in GitHub
+# workflows; these functions will then override (empty) functions
+# that are are called at the appropriate times during the test runs.
+
+test_skip_test_preamble=t
+
+start_test_output () {
+ test -n "$GIT_TEST_TEE_OUTPUT_FILE" ||
+ die "--github-workflow-markup requires --verbose-log"
+ github_markup_output="${GIT_TEST_TEE_OUTPUT_FILE%.out}.markup"
+ >$github_markup_output
+ GIT_TEST_TEE_OFFSET=0
+}
+
+# No need to override start_test_case_output
+
+finalize_test_case_output () {
+ test_case_result=$1
+ shift
+ case "$test_case_result" in
+ failure)
+ echo >>$github_markup_output "::error::failed: $this_test.$test_count $1"
+ ;;
+ fixed)
+ echo >>$github_markup_output "::notice::fixed: $this_test.$test_count $1"
+ ;;
+ ok)
+ # Exit without printing the "ok" tests
+ return
+ ;;
+ esac
+ echo >>$github_markup_output "::group::$test_case_result: $this_test.$test_count $*"
+ test-tool >>$github_markup_output path-utils skip-n-bytes \
+ "$GIT_TEST_TEE_OUTPUT_FILE" $GIT_TEST_TEE_OFFSET
+ echo >>$github_markup_output "::endgroup::"
+}
+
+# No need to override finalize_test_output
diff --git a/t/test-lib-junit.sh b/t/test-lib-junit.sh
new file mode 100644
index 0000000..79c31c7
--- /dev/null
+++ b/t/test-lib-junit.sh
@@ -0,0 +1,132 @@
+# Library of functions to format test scripts' output in JUnit XML
+# format, to support Git's test suite result to be presented in an
+# easily digestible way on Azure Pipelines.
+#
+# Copyright (c) 2022 Johannes Schindelin
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see http://www.gnu.org/licenses/ .
+#
+# The idea is for `test-lib.sh` to source this file when the user asks
+# for JUnit XML; these functions will then override (empty) functions
+# that are are called at the appropriate times during the test runs.
+
+start_test_output () {
+ junit_xml_dir="$TEST_OUTPUT_DIRECTORY/out"
+ mkdir -p "$junit_xml_dir"
+ junit_xml_base=${1##*/}
+ junit_xml_path="$junit_xml_dir/TEST-${junit_xml_base%.sh}.xml"
+ junit_attrs="name=\"${junit_xml_base%.sh}\""
+ junit_attrs="$junit_attrs timestamp=\"$(TZ=UTC \
+ date +%Y-%m-%dT%H:%M:%S)\""
+ write_junit_xml --truncate "<testsuites>" " <testsuite $junit_attrs>"
+ junit_suite_start=$(test-tool date getnanos)
+ if test -n "$GIT_TEST_TEE_OUTPUT_FILE"
+ then
+ GIT_TEST_TEE_OFFSET=0
+ fi
+}
+
+start_test_case_output () {
+ junit_start=$(test-tool date getnanos)
+}
+
+finalize_test_case_output () {
+ test_case_result=$1
+ shift
+ case "$test_case_result" in
+ ok)
+ set -- "$*"
+ ;;
+ failure)
+ junit_insert="<failure message=\"not ok $test_count -"
+ junit_insert="$junit_insert $(xml_attr_encode --no-lf "$1")\">"
+ junit_insert="$junit_insert $(xml_attr_encode \
+ "$(if test -n "$GIT_TEST_TEE_OUTPUT_FILE"
+ then
+ test-tool path-utils skip-n-bytes \
+ "$GIT_TEST_TEE_OUTPUT_FILE" $GIT_TEST_TEE_OFFSET
+ else
+ printf '%s\n' "$@" | sed 1d
+ fi)")"
+ junit_insert="$junit_insert</failure>"
+ if test -n "$GIT_TEST_TEE_OUTPUT_FILE"
+ then
+ junit_insert="$junit_insert<system-err>$(xml_attr_encode \
+ "$(cat "$GIT_TEST_TEE_OUTPUT_FILE")")</system-err>"
+ fi
+ set -- "$1" " $junit_insert"
+ ;;
+ fixed)
+ set -- "$* (breakage fixed)"
+ ;;
+ broken)
+ set -- "$* (known breakage)"
+ ;;
+ skip)
+ message="$(xml_attr_encode --no-lf "$skipped_reason")"
+ set -- "$1" " <skipped message=\"$message\" />"
+ ;;
+ esac
+
+ junit_attrs="name=\"$(xml_attr_encode --no-lf "$this_test.$test_count $1")\""
+ shift
+ junit_attrs="$junit_attrs classname=\"$this_test\""
+ junit_attrs="$junit_attrs time=\"$(test-tool \
+ date getnanos $junit_start)\""
+ write_junit_xml "$(printf '%s\n' \
+ " <testcase $junit_attrs>" "$@" " </testcase>")"
+ junit_have_testcase=t
+}
+
+finalize_test_output () {
+ if test -n "$junit_xml_path"
+ then
+ test -n "$junit_have_testcase" || {
+ junit_start=$(test-tool date getnanos)
+ write_junit_xml_testcase "all tests skipped"
+ }
+
+ # adjust the overall time
+ junit_time=$(test-tool date getnanos $junit_suite_start)
+ sed -e "s/\(<testsuite.*\) time=\"[^\"]*\"/\1/" \
+ -e "s/<testsuite [^>]*/& time=\"$junit_time\"/" \
+ -e '/^ *<\/testsuite/d' \
+ <"$junit_xml_path" >"$junit_xml_path.new"
+ mv "$junit_xml_path.new" "$junit_xml_path"
+
+ write_junit_xml " </testsuite>" "</testsuites>"
+ write_junit_xml=
+ fi
+}
+
+write_junit_xml () {
+ case "$1" in
+ --truncate)
+ >"$junit_xml_path"
+ junit_have_testcase=
+ shift
+ ;;
+ esac
+ printf '%s\n' "$@" >>"$junit_xml_path"
+}
+
+xml_attr_encode () {
+ if test "x$1" = "x--no-lf"
+ then
+ shift
+ printf '%s' "$*" | test-tool xml-encode
+ else
+ printf '%s\n' "$@" | test-tool xml-encode
+ fi
+}
diff --git a/t/test-lib.sh b/t/test-lib.sh
new file mode 100644
index 0000000..1656c9e
--- /dev/null
+++ b/t/test-lib.sh
@@ -0,0 +1,1951 @@
+# Test framework for git. See t/README for usage.
+#
+# Copyright (c) 2005 Junio C Hamano
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see http://www.gnu.org/licenses/ .
+
+# Test the binaries we have just built. The tests are kept in
+# t/ subdirectory and are run in 'trash directory' subdirectory.
+if test -z "$TEST_DIRECTORY"
+then
+ # ensure that TEST_DIRECTORY is an absolute path so that it
+ # is valid even if the current working directory is changed
+ TEST_DIRECTORY=$(pwd)
+else
+ # The TEST_DIRECTORY will always be the path to the "t"
+ # directory in the git.git checkout. This is overridden by
+ # e.g. t/lib-subtest.sh, but only because its $(pwd) is
+ # different. Those tests still set "$TEST_DIRECTORY" to the
+ # same path.
+ #
+ # See use of "$GIT_BUILD_DIR" and "$TEST_DIRECTORY" below for
+ # hard assumptions about "$GIT_BUILD_DIR/t" existing and being
+ # the "$TEST_DIRECTORY", and e.g. "$TEST_DIRECTORY/helper"
+ # needing to exist.
+ TEST_DIRECTORY=$(cd "$TEST_DIRECTORY" && pwd) || exit 1
+fi
+if test -z "$TEST_OUTPUT_DIRECTORY"
+then
+ # Similarly, override this to store the test-results subdir
+ # elsewhere
+ TEST_OUTPUT_DIRECTORY=$TEST_DIRECTORY
+fi
+GIT_BUILD_DIR="${TEST_DIRECTORY%/t}"
+if test "$TEST_DIRECTORY" = "$GIT_BUILD_DIR"
+then
+ echo "PANIC: Running in a $TEST_DIRECTORY that doesn't end in '/t'?" >&2
+ exit 1
+fi
+if test -f "$GIT_BUILD_DIR/GIT-BUILD-DIR"
+then
+ GIT_BUILD_DIR="$(cat "$GIT_BUILD_DIR/GIT-BUILD-DIR")" || exit 1
+ # On Windows, we must convert Windows paths lest they contain a colon
+ case "$(uname -s)" in
+ *MINGW*)
+ GIT_BUILD_DIR="$(cygpath -au "$GIT_BUILD_DIR")"
+ ;;
+ esac
+fi
+
+# Prepend a string to a VAR using an arbitrary ":" delimiter, not
+# adding the delimiter if VAR or VALUE is empty. I.e. a generalized:
+#
+# VAR=$1${VAR:+${1:+$2}$VAR}
+#
+# Usage (using ":" as the $2 delimiter):
+#
+# prepend_var VAR : VALUE
+prepend_var () {
+ eval "$1=\"$3\${$1:+${3:+$2}\$$1}\""
+}
+
+# If [AL]SAN is in effect we want to abort so that we notice
+# problems. The GIT_SAN_OPTIONS variable can be used to set common
+# defaults shared between [AL]SAN_OPTIONS.
+prepend_var GIT_SAN_OPTIONS : abort_on_error=1
+prepend_var GIT_SAN_OPTIONS : strip_path_prefix="$GIT_BUILD_DIR/"
+
+# If we were built with ASAN, it may complain about leaks
+# of program-lifetime variables. Disable it by default to lower
+# the noise level. This needs to happen at the start of the script,
+# before we even do our "did we build git yet" check (since we don't
+# want that one to complain to stderr).
+prepend_var ASAN_OPTIONS : $GIT_SAN_OPTIONS
+prepend_var ASAN_OPTIONS : detect_leaks=0
+export ASAN_OPTIONS
+
+prepend_var LSAN_OPTIONS : $GIT_SAN_OPTIONS
+prepend_var LSAN_OPTIONS : fast_unwind_on_malloc=0
+export LSAN_OPTIONS
+
+prepend_var UBSAN_OPTIONS : $GIT_SAN_OPTIONS
+export UBSAN_OPTIONS
+
+if test ! -f "$GIT_BUILD_DIR"/GIT-BUILD-OPTIONS
+then
+ echo >&2 'error: GIT-BUILD-OPTIONS missing (has Git been built?).'
+ exit 1
+fi
+. "$GIT_BUILD_DIR"/GIT-BUILD-OPTIONS
+export PERL_PATH SHELL_PATH
+
+# In t0000, we need to override test directories of nested testcases. In case
+# the developer has TEST_OUTPUT_DIRECTORY part of his build options, then we'd
+# reset this value to instead contain what the developer has specified. We thus
+# have this knob to allow overriding the directory.
+if test -n "${TEST_OUTPUT_DIRECTORY_OVERRIDE}"
+then
+ TEST_OUTPUT_DIRECTORY="${TEST_OUTPUT_DIRECTORY_OVERRIDE}"
+fi
+
+# Disallow the use of abbreviated options in the test suite by default
+if test -z "${GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS}"
+then
+ GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS=true
+ export GIT_TEST_DISALLOW_ABBREVIATED_OPTIONS
+fi
+
+# Explicitly set the default branch name for testing, to avoid the
+# transitory "git init" warning under --verbose.
+: ${GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME:=master}
+export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
+
+################################################################
+# It appears that people try to run tests without building...
+"${GIT_TEST_INSTALLED:-$GIT_BUILD_DIR}/git$X" >/dev/null
+if test $? != 1
+then
+ if test -n "$GIT_TEST_INSTALLED"
+ then
+ echo >&2 "error: there is no working Git at '$GIT_TEST_INSTALLED'"
+ else
+ echo >&2 'error: you do not seem to have built git yet.'
+ fi
+ exit 1
+fi
+
+store_arg_to=
+opt_required_arg=
+# $1: option string
+# $2: name of the var where the arg will be stored
+mark_option_requires_arg () {
+ if test -n "$opt_required_arg"
+ then
+ echo "error: options that require args cannot be bundled" \
+ "together: '$opt_required_arg' and '$1'" >&2
+ exit 1
+ fi
+ opt_required_arg=$1
+ store_arg_to=$2
+}
+
+# These functions can be overridden e.g. to output JUnit XML
+start_test_output () { :; }
+start_test_case_output () { :; }
+finalize_test_case_output () { :; }
+finalize_test_output () { :; }
+
+parse_option () {
+ local opt="$1"
+
+ case "$opt" in
+ -d|--d|--de|--deb|--debu|--debug)
+ debug=t ;;
+ -i|--i|--im|--imm|--imme|--immed|--immedi|--immedia|--immediat|--immediate)
+ immediate=t ;;
+ -l|--l|--lo|--lon|--long|--long-|--long-t|--long-te|--long-tes|--long-test|--long-tests)
+ GIT_TEST_LONG=t; export GIT_TEST_LONG ;;
+ -r)
+ mark_option_requires_arg "$opt" run_list
+ ;;
+ --run=*)
+ run_list=${opt#--*=} ;;
+ -h|--h|--he|--hel|--help)
+ help=t ;;
+ -v|--v|--ve|--ver|--verb|--verbo|--verbos|--verbose)
+ verbose=t ;;
+ --verbose-only=*)
+ verbose_only=${opt#--*=}
+ ;;
+ -q|--q|--qu|--qui|--quie|--quiet)
+ # Ignore --quiet under a TAP::Harness. Saying how many tests
+ # passed without the ok/not ok details is always an error.
+ test -z "$HARNESS_ACTIVE" && quiet=t ;;
+ --with-dashes)
+ with_dashes=t ;;
+ --no-bin-wrappers)
+ no_bin_wrappers=t ;;
+ --no-color)
+ color= ;;
+ --va|--val|--valg|--valgr|--valgri|--valgrin|--valgrind)
+ valgrind=memcheck
+ tee=t
+ ;;
+ --valgrind=*)
+ valgrind=${opt#--*=}
+ tee=t
+ ;;
+ --valgrind-only=*)
+ valgrind_only=${opt#--*=}
+ tee=t
+ ;;
+ --tee)
+ tee=t ;;
+ --root=*)
+ root=${opt#--*=} ;;
+ --chain-lint)
+ GIT_TEST_CHAIN_LINT=1 ;;
+ --no-chain-lint)
+ GIT_TEST_CHAIN_LINT=0 ;;
+ -x)
+ trace=t ;;
+ -V|--verbose-log)
+ verbose_log=t
+ tee=t
+ ;;
+ --write-junit-xml)
+ . "$TEST_DIRECTORY/test-lib-junit.sh"
+ ;;
+ --github-workflow-markup)
+ . "$TEST_DIRECTORY/test-lib-github-workflow-markup.sh"
+ ;;
+ --stress)
+ stress=t ;;
+ --stress=*)
+ echo "error: --stress does not accept an argument: '$opt'" >&2
+ echo "did you mean --stress-jobs=${opt#*=} or --stress-limit=${opt#*=}?" >&2
+ exit 1
+ ;;
+ --stress-jobs=*)
+ stress=t;
+ stress_jobs=${opt#--*=}
+ case "$stress_jobs" in
+ *[!0-9]*|0*|"")
+ echo "error: --stress-jobs=<N> requires the number of jobs to run" >&2
+ exit 1
+ ;;
+ *) # Good.
+ ;;
+ esac
+ ;;
+ --stress-limit=*)
+ stress=t;
+ stress_limit=${opt#--*=}
+ case "$stress_limit" in
+ *[!0-9]*|0*|"")
+ echo "error: --stress-limit=<N> requires the number of repetitions" >&2
+ exit 1
+ ;;
+ *) # Good.
+ ;;
+ esac
+ ;;
+ --invert-exit-code)
+ invert_exit_code=t
+ ;;
+ *)
+ echo "error: unknown test option '$opt'" >&2; exit 1 ;;
+ esac
+}
+
+# Parse options while taking care to leave $@ intact, so we will still
+# have all the original command line options when executing the test
+# script again for '--tee' and '--verbose-log' later.
+for opt
+do
+ if test -n "$store_arg_to"
+ then
+ eval $store_arg_to=\$opt
+ store_arg_to=
+ opt_required_arg=
+ continue
+ fi
+
+ case "$opt" in
+ --*|-?)
+ parse_option "$opt" ;;
+ -?*)
+ # bundled short options must be fed separately to parse_option
+ opt=${opt#-}
+ while test -n "$opt"
+ do
+ extra=${opt#?}
+ this=${opt%$extra}
+ opt=$extra
+ parse_option "-$this"
+ done
+ ;;
+ *)
+ echo "error: unknown test option '$opt'" >&2; exit 1 ;;
+ esac
+done
+if test -n "$store_arg_to"
+then
+ echo "error: $opt_required_arg requires an argument" >&2
+ exit 1
+fi
+
+if test -n "$valgrind_only"
+then
+ test -z "$valgrind" && valgrind=memcheck
+ test -z "$verbose" && verbose_only="$valgrind_only"
+elif test -n "$valgrind"
+then
+ test -z "$verbose_log" && verbose=t
+fi
+
+if test -n "$stress"
+then
+ verbose=t
+ trace=t
+ immediate=t
+fi
+
+TEST_STRESS_JOB_SFX="${GIT_TEST_STRESS_JOB_NR:+.stress-$GIT_TEST_STRESS_JOB_NR}"
+TEST_NAME="$(basename "$0" .sh)"
+TEST_NUMBER="${TEST_NAME%%-*}"
+TEST_NUMBER="${TEST_NUMBER#t}"
+TEST_RESULTS_DIR="$TEST_OUTPUT_DIRECTORY/test-results"
+TEST_RESULTS_BASE="$TEST_RESULTS_DIR/$TEST_NAME$TEST_STRESS_JOB_SFX"
+TEST_RESULTS_SAN_FILE_PFX=trace
+TEST_RESULTS_SAN_DIR_SFX=leak
+TEST_RESULTS_SAN_FILE=
+TEST_RESULTS_SAN_DIR="$TEST_RESULTS_DIR/$TEST_NAME.$TEST_RESULTS_SAN_DIR_SFX"
+TEST_RESULTS_SAN_DIR_NR_LEAKS_STARTUP=
+TRASH_DIRECTORY="trash directory.$TEST_NAME$TEST_STRESS_JOB_SFX"
+test -n "$root" && TRASH_DIRECTORY="$root/$TRASH_DIRECTORY"
+case "$TRASH_DIRECTORY" in
+/*) ;; # absolute path is good
+ *) TRASH_DIRECTORY="$TEST_OUTPUT_DIRECTORY/$TRASH_DIRECTORY" ;;
+esac
+
+# Utility functions using $TEST_RESULTS_* variables
+nr_san_dir_leaks_ () {
+ # stderr piped to /dev/null because the directory may have
+ # been "rmdir"'d already.
+ find "$TEST_RESULTS_SAN_DIR" \
+ -type f \
+ -name "$TEST_RESULTS_SAN_FILE_PFX.*" 2>/dev/null |
+ xargs grep -lv "Unable to get registers from thread" |
+ wc -l
+}
+
+# If --stress was passed, run this test repeatedly in several parallel loops.
+if test "$GIT_TEST_STRESS_STARTED" = "done"
+then
+ : # Don't stress test again.
+elif test -n "$stress"
+then
+ if test -n "$stress_jobs"
+ then
+ job_count=$stress_jobs
+ elif test -n "$GIT_TEST_STRESS_LOAD"
+ then
+ job_count="$GIT_TEST_STRESS_LOAD"
+ elif job_count=$(getconf _NPROCESSORS_ONLN 2>/dev/null) &&
+ test -n "$job_count"
+ then
+ job_count=$((2 * $job_count))
+ else
+ job_count=8
+ fi
+
+ mkdir -p "$TEST_RESULTS_DIR"
+ stressfail="$TEST_RESULTS_BASE.stress-failed"
+ rm -f "$stressfail"
+
+ stress_exit=0
+ trap '
+ kill $job_pids 2>/dev/null
+ wait
+ stress_exit=1
+ ' TERM INT HUP
+
+ job_pids=
+ job_nr=0
+ while test $job_nr -lt "$job_count"
+ do
+ (
+ GIT_TEST_STRESS_STARTED=done
+ GIT_TEST_STRESS_JOB_NR=$job_nr
+ export GIT_TEST_STRESS_STARTED GIT_TEST_STRESS_JOB_NR
+
+ trap '
+ kill $test_pid 2>/dev/null
+ wait
+ exit 1
+ ' TERM INT
+
+ cnt=1
+ while ! test -e "$stressfail" &&
+ { test -z "$stress_limit" ||
+ test $cnt -le $stress_limit ; }
+ do
+ $TEST_SHELL_PATH "$0" "$@" >"$TEST_RESULTS_BASE.stress-$job_nr.out" 2>&1 &
+ test_pid=$!
+
+ if wait $test_pid
+ then
+ printf "OK %2d.%d\n" $GIT_TEST_STRESS_JOB_NR $cnt
+ else
+ echo $GIT_TEST_STRESS_JOB_NR >>"$stressfail"
+ printf "FAIL %2d.%d\n" $GIT_TEST_STRESS_JOB_NR $cnt
+ fi
+ cnt=$(($cnt + 1))
+ done
+ ) &
+ job_pids="$job_pids $!"
+ job_nr=$(($job_nr + 1))
+ done
+
+ wait
+
+ if test -f "$stressfail"
+ then
+ stress_exit=1
+ echo "Log(s) of failed test run(s):"
+ for failed_job_nr in $(sort -n "$stressfail")
+ do
+ echo "Contents of '$TEST_RESULTS_BASE.stress-$failed_job_nr.out':"
+ cat "$TEST_RESULTS_BASE.stress-$failed_job_nr.out"
+ done
+ rm -rf "$TRASH_DIRECTORY.stress-failed"
+ # Move the last one.
+ mv "$TRASH_DIRECTORY.stress-$failed_job_nr" "$TRASH_DIRECTORY.stress-failed"
+ fi
+
+ exit $stress_exit
+fi
+
+# if --tee was passed, write the output not only to the terminal, but
+# additionally to the file test-results/$BASENAME.out, too.
+if test "$GIT_TEST_TEE_STARTED" = "done"
+then
+ : # do not redirect again
+elif test -n "$tee"
+then
+ mkdir -p "$TEST_RESULTS_DIR"
+
+ # Make this filename available to the sub-process in case it is using
+ # --verbose-log.
+ GIT_TEST_TEE_OUTPUT_FILE=$TEST_RESULTS_BASE.out
+ export GIT_TEST_TEE_OUTPUT_FILE
+
+ # Truncate before calling "tee -a" to get rid of the results
+ # from any previous runs.
+ >"$GIT_TEST_TEE_OUTPUT_FILE"
+
+ (GIT_TEST_TEE_STARTED=done ${TEST_SHELL_PATH} "$0" "$@" 2>&1;
+ echo $? >"$TEST_RESULTS_BASE.exit") | tee -a "$GIT_TEST_TEE_OUTPUT_FILE"
+ test "$(cat "$TEST_RESULTS_BASE.exit")" = 0
+ exit
+fi
+
+if test -n "$trace" && test -n "$test_untraceable"
+then
+ # '-x' tracing requested, but this test script can't be reliably
+ # traced, unless it is run with a Bash version supporting
+ # BASH_XTRACEFD (introduced in Bash v4.1).
+ #
+ # Perform this version check _after_ the test script was
+ # potentially re-executed with $TEST_SHELL_PATH for '--tee' or
+ # '--verbose-log', so the right shell is checked and the
+ # warning is issued only once.
+ if test -n "$BASH_VERSION" && eval '
+ test ${BASH_VERSINFO[0]} -gt 4 || {
+ test ${BASH_VERSINFO[0]} -eq 4 &&
+ test ${BASH_VERSINFO[1]} -ge 1
+ }
+ '
+ then
+ : Executed by a Bash version supporting BASH_XTRACEFD. Good.
+ else
+ echo >&2 "warning: ignoring -x; '$0' is untraceable without BASH_XTRACEFD"
+ trace=
+ fi
+fi
+if test -n "$trace" && test -z "$verbose_log"
+then
+ verbose=t
+fi
+
+# Since bash 5.0, checkwinsize is enabled by default which does
+# update the COLUMNS variable every time a non-builtin command
+# completes, even for non-interactive shells.
+# Disable that since we are aiming for repeatability.
+test -n "$BASH_VERSION" && shopt -u checkwinsize 2>/dev/null
+
+# For repeatability, reset the environment to known value.
+# TERM is sanitized below, after saving color control sequences.
+LANG=C
+LC_ALL=C
+PAGER=cat
+TZ=UTC
+COLUMNS=80
+export LANG LC_ALL PAGER TZ COLUMNS
+EDITOR=:
+
+# A call to "unset" with no arguments causes at least Solaris 10
+# /usr/xpg4/bin/sh and /bin/ksh to bail out. So keep the unsets
+# deriving from the command substitution clustered with the other
+# ones.
+unset VISUAL EMAIL LANGUAGE $("$PERL_PATH" -e '
+ my @env = keys %ENV;
+ my $ok = join("|", qw(
+ TRACE
+ DEBUG
+ TEST
+ .*_TEST
+ PROVE
+ VALGRIND
+ UNZIP
+ PERF_
+ CURL_VERBOSE
+ TRACE_CURL
+ ));
+ my @vars = grep(/^GIT_/ && !/^GIT_($ok)/o, @env);
+ print join("\n", @vars);
+')
+unset XDG_CACHE_HOME
+unset XDG_CONFIG_HOME
+unset GITPERLLIB
+unset GIT_TRACE2_PARENT_NAME
+unset GIT_TRACE2_PARENT_SID
+TEST_AUTHOR_LOCALNAME=author
+TEST_AUTHOR_DOMAIN=example.com
+GIT_AUTHOR_EMAIL=${TEST_AUTHOR_LOCALNAME}@${TEST_AUTHOR_DOMAIN}
+GIT_AUTHOR_NAME='A U Thor'
+GIT_AUTHOR_DATE='1112354055 +0200'
+TEST_COMMITTER_LOCALNAME=committer
+TEST_COMMITTER_DOMAIN=example.com
+GIT_COMMITTER_EMAIL=${TEST_COMMITTER_LOCALNAME}@${TEST_COMMITTER_DOMAIN}
+GIT_COMMITTER_NAME='C O Mitter'
+GIT_COMMITTER_DATE='1112354055 +0200'
+GIT_MERGE_VERBOSITY=5
+GIT_MERGE_AUTOEDIT=no
+export GIT_MERGE_VERBOSITY GIT_MERGE_AUTOEDIT
+export GIT_AUTHOR_EMAIL GIT_AUTHOR_NAME
+export GIT_COMMITTER_EMAIL GIT_COMMITTER_NAME
+export GIT_COMMITTER_DATE GIT_AUTHOR_DATE
+export EDITOR
+
+GIT_DEFAULT_HASH="${GIT_TEST_DEFAULT_HASH:-sha1}"
+export GIT_DEFAULT_HASH
+GIT_TEST_MERGE_ALGORITHM="${GIT_TEST_MERGE_ALGORITHM:-ort}"
+export GIT_TEST_MERGE_ALGORITHM
+
+# Tests using GIT_TRACE typically don't want <timestamp> <file>:<line> output
+GIT_TRACE_BARE=1
+export GIT_TRACE_BARE
+
+# Some tests scan the GIT_TRACE2_EVENT feed for events, but the
+# default depth is 2, which frequently causes issues when the
+# events are wrapped in new regions. Set it to a sufficiently
+# large depth to avoid custom changes in the test suite.
+GIT_TRACE2_EVENT_NESTING=100
+export GIT_TRACE2_EVENT_NESTING
+
+# Use specific version of the index file format
+if test -n "${GIT_TEST_INDEX_VERSION:+isset}"
+then
+ GIT_INDEX_VERSION="$GIT_TEST_INDEX_VERSION"
+ export GIT_INDEX_VERSION
+fi
+
+if test -n "$GIT_TEST_PERL_FATAL_WARNINGS"
+then
+ GIT_PERL_FATAL_WARNINGS=1
+ export GIT_PERL_FATAL_WARNINGS
+fi
+
+case $GIT_TEST_FSYNC in
+'')
+ GIT_TEST_FSYNC=0
+ export GIT_TEST_FSYNC
+ ;;
+esac
+
+# Add libc MALLOC and MALLOC_PERTURB test only if we are not executing
+# the test with valgrind and have not compiled with conflict SANITIZE
+# options.
+if test -n "$valgrind" ||
+ test -n "$SANITIZE_ADDRESS" ||
+ test -n "$SANITIZE_LEAK" ||
+ test -n "$TEST_NO_MALLOC_CHECK"
+then
+ setup_malloc_check () {
+ : nothing
+ }
+ teardown_malloc_check () {
+ : nothing
+ }
+else
+ _USE_GLIBC_TUNABLES=
+ if _GLIBC_VERSION=$(getconf GNU_LIBC_VERSION 2>/dev/null) &&
+ _GLIBC_VERSION=${_GLIBC_VERSION#"glibc "} &&
+ expr 2.34 \<= "$_GLIBC_VERSION" >/dev/null
+ then
+ _USE_GLIBC_TUNABLES=YesPlease
+ fi
+ setup_malloc_check () {
+ local g
+ local t
+ MALLOC_CHECK_=3 MALLOC_PERTURB_=165
+ export MALLOC_CHECK_ MALLOC_PERTURB_
+ if test -n "$_USE_GLIBC_TUNABLES"
+ then
+ g=
+ LD_PRELOAD="libc_malloc_debug.so.0"
+ for t in \
+ glibc.malloc.check=1 \
+ glibc.malloc.perturb=165
+ do
+ g="${g#:}:$t"
+ done
+ GLIBC_TUNABLES=$g
+ export LD_PRELOAD GLIBC_TUNABLES
+ fi
+ }
+ teardown_malloc_check () {
+ unset MALLOC_CHECK_ MALLOC_PERTURB_
+ unset LD_PRELOAD GLIBC_TUNABLES
+ }
+fi
+
+# Protect ourselves from common misconfiguration to export
+# CDPATH into the environment
+unset CDPATH
+
+unset GREP_OPTIONS
+unset UNZIP
+
+case $(echo $GIT_TRACE |tr "[A-Z]" "[a-z]") in
+1|2|true)
+ GIT_TRACE=4
+ ;;
+esac
+
+# Line feed
+LF='
+'
+
+# Single quote
+SQ=\'
+
+# UTF-8 ZERO WIDTH NON-JOINER, which HFS+ ignores
+# when case-folding filenames
+u200c=$(printf '\342\200\214')
+
+export _x05 _x35 LF u200c EMPTY_TREE EMPTY_BLOB ZERO_OID OID_REGEX
+
+test "x$TERM" != "xdumb" && (
+ test -t 1 &&
+ tput bold >/dev/null 2>&1 &&
+ tput setaf 1 >/dev/null 2>&1 &&
+ tput sgr0 >/dev/null 2>&1
+ ) &&
+ color=t
+
+if test -n "$color"
+then
+ # Save the color control sequences now rather than run tput
+ # each time say_color() is called. This is done for two
+ # reasons:
+ # * TERM will be changed to dumb
+ # * HOME will be changed to a temporary directory and tput
+ # might need to read ~/.terminfo from the original HOME
+ # directory to get the control sequences
+ # Note: This approach assumes the control sequences don't end
+ # in a newline for any terminal of interest (command
+ # substitutions strip trailing newlines). Given that most
+ # (all?) terminals in common use are related to ECMA-48, this
+ # shouldn't be a problem.
+ say_color_error=$(tput bold; tput setaf 1) # bold red
+ say_color_skip=$(tput setaf 4) # blue
+ say_color_warn=$(tput setaf 3) # brown/yellow
+ say_color_pass=$(tput setaf 2) # green
+ say_color_info=$(tput setaf 6) # cyan
+ say_color_reset=$(tput sgr0)
+ say_color_="" # no formatting for normal text
+ say_color () {
+ test -z "$1" && test -n "$quiet" && return
+ eval "say_color_color=\$say_color_$1"
+ shift
+ printf "%s\\n" "$say_color_color$*$say_color_reset"
+ }
+else
+ say_color() {
+ test -z "$1" && test -n "$quiet" && return
+ shift
+ printf "%s\n" "$*"
+ }
+fi
+
+USER_TERM="$TERM"
+TERM=dumb
+export TERM USER_TERM
+
+# What is written by tests to stdout and stderr is sent to different places
+# depending on the test mode (e.g. /dev/null in non-verbose mode, piped to tee
+# with --tee option, etc.). We save the original stdin to FD #6 and stdout and
+# stderr to #5 and #7, so that the test framework can use them (e.g. for
+# printing errors within the test framework) independently of the test mode.
+exec 5>&1
+exec 6<&0
+exec 7>&2
+
+_error_exit () {
+ finalize_test_output
+ GIT_EXIT_OK=t
+ exit 1
+}
+
+error () {
+ say_color error "error: $*"
+ _error_exit
+}
+
+BUG () {
+ error >&7 "bug in the test script: $*"
+}
+
+BAIL_OUT () {
+ test $# -ne 1 && BUG "1 param"
+
+ # Do not change "Bail out! " string. It's part of TAP syntax:
+ # https://testanything.org/tap-specification.html
+ local bail_out="Bail out! "
+ local message="$1"
+
+ say_color >&5 error $bail_out "$message"
+ _error_exit
+}
+
+say () {
+ say_color info "$*"
+}
+
+if test -n "$HARNESS_ACTIVE"
+then
+ if test "$verbose" = t || test -n "$verbose_only"
+ then
+ BAIL_OUT 'verbose mode forbidden under TAP harness; try --verbose-log'
+ fi
+fi
+
+test "${test_description}" != "" ||
+error "Test script did not set test_description."
+
+if test "$help" = "t"
+then
+ printf '%s\n' "$test_description"
+ exit 0
+fi
+
+if test "$verbose_log" = "t"
+then
+ exec 3>>"$GIT_TEST_TEE_OUTPUT_FILE" 4>&3
+elif test "$verbose" = "t"
+then
+ exec 4>&2 3>&1
+else
+ exec 4>/dev/null 3>/dev/null
+fi
+
+# Send any "-x" output directly to stderr to avoid polluting tests
+# which capture stderr. We can do this unconditionally since it
+# has no effect if tracing isn't turned on.
+#
+# Note that this sets up the trace fd as soon as we assign the variable, so it
+# must come after the creation of descriptor 4 above. Likewise, we must never
+# unset this, as it has the side effect of closing descriptor 4, which we
+# use to show verbose tests to the user.
+#
+# Note also that we don't need or want to export it. The tracing is local to
+# this shell, and we would not want to influence any shells we exec.
+BASH_XTRACEFD=4
+
+test_failure=0
+test_count=0
+test_fixed=0
+test_broken=0
+test_success=0
+
+test_missing_prereq=
+
+test_external_has_tap=0
+
+die () {
+ code=$?
+ # This is responsible for running the atexit commands even when a
+ # test script run with '--immediate' fails, or when the user hits
+ # ctrl-C, i.e. when 'test_done' is not invoked at all.
+ test_atexit_handler || code=$?
+ if test -n "$GIT_EXIT_OK"
+ then
+ exit $code
+ else
+ echo >&5 "FATAL: Unexpected exit with code $code"
+ exit 1
+ fi
+}
+
+GIT_EXIT_OK=
+trap 'die' EXIT
+# Disable '-x' tracing, because with some shells, notably dash, it
+# prevents running the cleanup commands when a test script run with
+# '--verbose-log -x' is interrupted.
+trap '{ code=$?; set +x; } 2>/dev/null; exit $code' INT TERM HUP
+
+# The user-facing functions are loaded from a separate file so that
+# test_perf subshells can have them too
+. "$TEST_DIRECTORY/test-lib-functions.sh"
+
+# You are not expected to call test_ok_ and test_failure_ directly, use
+# the test_expect_* functions instead.
+
+test_ok_ () {
+ test_success=$(($test_success + 1))
+ say_color "" "ok $test_count - $@"
+ finalize_test_case_output ok "$@"
+}
+
+_invert_exit_code_failure_end_blurb () {
+ say_color warn "# faked up failures as TODO & now exiting with 0 due to --invert-exit-code"
+}
+
+test_failure_ () {
+ failure_label=$1
+ test_failure=$(($test_failure + 1))
+ local pfx=""
+ if test -n "$invert_exit_code" # && test -n "$HARNESS_ACTIVE"
+ then
+ pfx="# TODO induced breakage (--invert-exit-code):"
+ fi
+ say_color error "not ok $test_count - ${pfx:+$pfx }$1"
+ shift
+ printf '%s\n' "$*" | sed -e 's/^/# /'
+ if test -n "$immediate"
+ then
+ say_color error "1..$test_count"
+ if test -n "$invert_exit_code"
+ then
+ finalize_test_output
+ _invert_exit_code_failure_end_blurb
+ GIT_EXIT_OK=t
+ exit 0
+ fi
+ _error_exit
+ fi
+ finalize_test_case_output failure "$failure_label" "$@"
+}
+
+test_known_broken_ok_ () {
+ test_fixed=$(($test_fixed+1))
+ say_color error "ok $test_count - $1 # TODO known breakage vanished"
+ finalize_test_case_output fixed "$1"
+}
+
+test_known_broken_failure_ () {
+ test_broken=$(($test_broken+1))
+ say_color warn "not ok $test_count - $1 # TODO known breakage"
+ finalize_test_case_output broken "$1"
+}
+
+test_debug () {
+ test "$debug" = "" || eval "$1"
+}
+
+match_pattern_list () {
+ arg="$1"
+ shift
+ test -z "$*" && return 1
+ # We need to use "$*" to get field-splitting, but we want to
+ # disable globbing, since we are matching against an arbitrary
+ # $arg, not what's in the filesystem. Using "set -f" accomplishes
+ # that, but we must do it in a subshell to avoid impacting the
+ # rest of the script. The exit value of the subshell becomes
+ # the function's return value.
+ (
+ set -f
+ for pattern_ in $*
+ do
+ case "$arg" in
+ $pattern_)
+ exit 0
+ ;;
+ esac
+ done
+ exit 1
+ )
+}
+
+match_test_selector_list () {
+ operation="$1"
+ shift
+ title="$1"
+ shift
+ arg="$1"
+ shift
+ test -z "$1" && return 0
+
+ # Commas are accepted as separators.
+ OLDIFS=$IFS
+ IFS=','
+ set -- $1
+ IFS=$OLDIFS
+
+ # If the first selector is negative we include by default.
+ include=
+ case "$1" in
+ !*) include=t ;;
+ esac
+
+ for selector
+ do
+ orig_selector=$selector
+
+ positive=t
+ case "$selector" in
+ !*)
+ positive=
+ selector=${selector##?}
+ ;;
+ esac
+
+ test -z "$selector" && continue
+
+ case "$selector" in
+ *-*)
+ if expr "z${selector%%-*}" : "z[0-9]*[^0-9]" >/dev/null
+ then
+ echo "error: $operation: invalid non-numeric in range" \
+ "start: '$orig_selector'" >&2
+ exit 1
+ fi
+ if expr "z${selector#*-}" : "z[0-9]*[^0-9]" >/dev/null
+ then
+ echo "error: $operation: invalid non-numeric in range" \
+ "end: '$orig_selector'" >&2
+ exit 1
+ fi
+ ;;
+ *)
+ if expr "z$selector" : "z[0-9]*[^0-9]" >/dev/null
+ then
+ case "$title" in *${selector}*)
+ include=$positive
+ ;;
+ esac
+ continue
+ fi
+ esac
+
+ # Short cut for "obvious" cases
+ test -z "$include" && test -z "$positive" && continue
+ test -n "$include" && test -n "$positive" && continue
+
+ case "$selector" in
+ -*)
+ if test $arg -le ${selector#-}
+ then
+ include=$positive
+ fi
+ ;;
+ *-)
+ if test $arg -ge ${selector%-}
+ then
+ include=$positive
+ fi
+ ;;
+ *-*)
+ if test ${selector%%-*} -le $arg \
+ && test $arg -le ${selector#*-}
+ then
+ include=$positive
+ fi
+ ;;
+ *)
+ if test $arg -eq $selector
+ then
+ include=$positive
+ fi
+ ;;
+ esac
+ done
+
+ test -n "$include"
+}
+
+maybe_teardown_verbose () {
+ test -z "$verbose_only" && return
+ exec 4>/dev/null 3>/dev/null
+ verbose=
+}
+
+last_verbose=t
+maybe_setup_verbose () {
+ test -z "$verbose_only" && return
+ if match_pattern_list $test_count "$verbose_only"
+ then
+ exec 4>&2 3>&1
+ # Emit a delimiting blank line when going from
+ # non-verbose to verbose. Within verbose mode the
+ # delimiter is printed by test_expect_*. The choice
+ # of the initial $last_verbose is such that before
+ # test 1, we do not print it.
+ test -z "$last_verbose" && echo >&3 ""
+ verbose=t
+ else
+ exec 4>/dev/null 3>/dev/null
+ verbose=
+ fi
+ last_verbose=$verbose
+}
+
+maybe_teardown_valgrind () {
+ test -z "$GIT_VALGRIND" && return
+ GIT_VALGRIND_ENABLED=
+}
+
+maybe_setup_valgrind () {
+ test -z "$GIT_VALGRIND" && return
+ if test -z "$valgrind_only"
+ then
+ GIT_VALGRIND_ENABLED=t
+ return
+ fi
+ GIT_VALGRIND_ENABLED=
+ if match_pattern_list $test_count "$valgrind_only"
+ then
+ GIT_VALGRIND_ENABLED=t
+ fi
+}
+
+trace_level_=0
+want_trace () {
+ test "$trace" = t && {
+ test "$verbose" = t || test "$verbose_log" = t
+ }
+}
+
+# This is a separate function because some tests use
+# "return" to end a test_expect_success block early
+# (and we want to make sure we run any cleanup like
+# "set +x").
+test_eval_inner_ () {
+ eval "$*"
+}
+
+test_eval_ () {
+ # If "-x" tracing is in effect, then we want to avoid polluting stderr
+ # with non-test commands. But once in "set -x" mode, we cannot prevent
+ # the shell from printing the "set +x" to turn it off (nor the saving
+ # of $? before that). But we can make sure that the output goes to
+ # /dev/null.
+ #
+ # There are a few subtleties here:
+ #
+ # - we have to redirect descriptor 4 in addition to 2, to cover
+ # BASH_XTRACEFD
+ #
+ # - the actual eval has to come before the redirection block (since
+ # it needs to see descriptor 4 to set up its stderr)
+ #
+ # - likewise, any error message we print must be outside the block to
+ # access descriptor 4
+ #
+ # - checking $? has to come immediately after the eval, but it must
+ # be _inside_ the block to avoid polluting the "set -x" output
+ #
+
+ # Do not add anything extra (including LF) after '$*'
+ test_eval_inner_ </dev/null >&3 2>&4 "
+ want_trace && trace_level_=$(($trace_level_+1)) && set -x
+ $*"
+ {
+ test_eval_ret_=$?
+ if want_trace
+ then
+ test 1 = $trace_level_ && set +x
+ trace_level_=$(($trace_level_-1))
+ fi
+ } 2>/dev/null 4>&2
+
+ if test "$test_eval_ret_" != 0 && want_trace
+ then
+ say_color error >&4 "error: last command exited with \$?=$test_eval_ret_"
+ fi
+ return $test_eval_ret_
+}
+
+fail_117 () {
+ return 117
+}
+
+test_run_ () {
+ test_cleanup=:
+ expecting_failure=$2
+
+ if test "${GIT_TEST_CHAIN_LINT:-1}" != 0; then
+ # 117 is magic because it is unlikely to match the exit
+ # code of other programs
+ test_eval_inner_ "fail_117 && $1" </dev/null >&3 2>&4
+ if test $? != 117
+ then
+ BUG "broken &&-chain: $1"
+ fi
+ fi
+
+ setup_malloc_check
+ test_eval_ "$1"
+ eval_ret=$?
+ teardown_malloc_check
+
+ if test -z "$immediate" || test $eval_ret = 0 ||
+ test -n "$expecting_failure" && test "$test_cleanup" != ":"
+ then
+ setup_malloc_check
+ test_eval_ "$test_cleanup"
+ teardown_malloc_check
+ fi
+ if test "$verbose" = "t" && test -n "$HARNESS_ACTIVE"
+ then
+ echo ""
+ fi
+ return "$eval_ret"
+}
+
+test_start_ () {
+ test_count=$(($test_count+1))
+ maybe_setup_verbose
+ maybe_setup_valgrind
+ start_test_case_output "$@"
+}
+
+test_finish_ () {
+ echo >&3 ""
+ maybe_teardown_valgrind
+ maybe_teardown_verbose
+ if test -n "$GIT_TEST_TEE_OFFSET"
+ then
+ GIT_TEST_TEE_OFFSET=$(test-tool path-utils file-size \
+ "$GIT_TEST_TEE_OUTPUT_FILE")
+ fi
+}
+
+test_skip () {
+ to_skip=
+ skipped_reason=
+ if match_pattern_list $this_test.$test_count "$GIT_SKIP_TESTS"
+ then
+ to_skip=t
+ skipped_reason="GIT_SKIP_TESTS"
+ fi
+ if test -z "$to_skip" && test -n "$run_list" &&
+ ! match_test_selector_list '--run' "$1" $test_count "$run_list"
+ then
+ to_skip=t
+ skipped_reason="--run"
+ fi
+ if test -z "$to_skip" && test -n "$test_prereq" &&
+ ! test_have_prereq "$test_prereq"
+ then
+ to_skip=t
+
+ of_prereq=
+ if test "$missing_prereq" != "$test_prereq"
+ then
+ of_prereq=" of $test_prereq"
+ fi
+ skipped_reason="missing $missing_prereq${of_prereq}"
+
+ # Keep a list of all the missing prereq for result aggregation
+ if test -z "$missing_prereq"
+ then
+ test_missing_prereq=$missing_prereq
+ else
+ test_missing_prereq="$test_missing_prereq,$missing_prereq"
+ fi
+ fi
+
+ case "$to_skip" in
+ t)
+
+ say_color skip "ok $test_count # skip $1 ($skipped_reason)"
+ : true
+ finalize_test_case_output skip "$@"
+ ;;
+ *)
+ false
+ ;;
+ esac
+}
+
+# stub; perf-lib overrides it
+test_at_end_hook_ () {
+ :
+}
+
+test_atexit_cleanup=:
+test_atexit_handler () {
+ # In a succeeding test script 'test_atexit_handler' is invoked
+ # twice: first from 'test_done', then from 'die' in the trap on
+ # EXIT.
+ # This condition and resetting 'test_atexit_cleanup' below makes
+ # sure that the registered cleanup commands are run only once.
+ test : != "$test_atexit_cleanup" || return 0
+
+ setup_malloc_check
+ test_eval_ "$test_atexit_cleanup"
+ test_atexit_cleanup=:
+ teardown_malloc_check
+}
+
+sanitize_leak_log_message_ () {
+ local new="$1" &&
+ local old="$2" &&
+ local file="$3" &&
+
+ printf "With SANITIZE=leak at exit we have %d leak logs, but started with %d
+
+This means that we have a blindspot where git is leaking but we're
+losing the exit code somewhere, or not propagating it appropriately
+upwards!
+
+See the logs at \"%s.*\";
+those logs are reproduced below." \
+ "$new" "$old" "$file"
+}
+
+check_test_results_san_file_ () {
+ if test -z "$TEST_RESULTS_SAN_FILE"
+ then
+ return
+ fi &&
+ local old="$TEST_RESULTS_SAN_DIR_NR_LEAKS_STARTUP" &&
+ local new="$(nr_san_dir_leaks_)" &&
+
+ if test $new -le $old
+ then
+ return
+ fi &&
+ local out="$(sanitize_leak_log_message_ "$new" "$old" "$TEST_RESULTS_SAN_FILE")" &&
+ say_color error "$out" &&
+ if test "$old" != 0
+ then
+ echo &&
+ say_color error "The logs include output from past runs to avoid" &&
+ say_color error "that remove 'test-results' between runs."
+ fi &&
+ say_color error "$(cat "$TEST_RESULTS_SAN_FILE".*)" &&
+
+ if test -n "$passes_sanitize_leak" && test "$test_failure" = 0
+ then
+ say "As TEST_PASSES_SANITIZE_LEAK=true and our logs show we're leaking, exit non-zero!" &&
+ invert_exit_code=t
+ elif test -n "$passes_sanitize_leak"
+ then
+ say "As TEST_PASSES_SANITIZE_LEAK=true and our logs show we're leaking, and we're failing for other reasons too..." &&
+ invert_exit_code=
+ elif test -n "$sanitize_leak_check" && test "$test_failure" = 0
+ then
+ say "As TEST_PASSES_SANITIZE_LEAK=true isn't set the above leak is 'ok' with GIT_TEST_PASSING_SANITIZE_LEAK=check" &&
+ invert_exit_code=
+ elif test -n "$sanitize_leak_check"
+ then
+ say "As TEST_PASSES_SANITIZE_LEAK=true isn't set the above leak is 'ok' with GIT_TEST_PASSING_SANITIZE_LEAK=check" &&
+ invert_exit_code=t
+ else
+ say "With GIT_TEST_SANITIZE_LEAK_LOG=true our logs revealed a memory leak, exit non-zero!" &&
+ invert_exit_code=t
+ fi
+}
+
+test_done () {
+ # Run the atexit commands _before_ the trash directory is
+ # removed, so the commands can access pidfiles and socket files.
+ test_atexit_handler
+
+ finalize_test_output
+
+ if test -z "$HARNESS_ACTIVE"
+ then
+ mkdir -p "$TEST_RESULTS_DIR"
+
+ cat >"$TEST_RESULTS_BASE.counts" <<-EOF
+ total $test_count
+ success $test_success
+ fixed $test_fixed
+ broken $test_broken
+ failed $test_failure
+ missing_prereq $test_missing_prereq
+
+ EOF
+ fi
+
+ if test "$test_fixed" != 0
+ then
+ say_color error "# $test_fixed known breakage(s) vanished; please update test(s)"
+ fi
+ if test "$test_broken" != 0
+ then
+ say_color warn "# still have $test_broken known breakage(s)"
+ fi
+ if test "$test_broken" != 0 || test "$test_fixed" != 0
+ then
+ test_remaining=$(( $test_count - $test_broken - $test_fixed ))
+ msg="remaining $test_remaining test(s)"
+ else
+ test_remaining=$test_count
+ msg="$test_count test(s)"
+ fi
+ case "$test_failure" in
+ 0)
+ if test $test_remaining -gt 0
+ then
+ say_color pass "# passed all $msg"
+ fi
+
+ # Maybe print SKIP message
+ test -z "$skip_all" || skip_all="# SKIP $skip_all"
+ case "$test_count" in
+ 0)
+ say "1..$test_count${skip_all:+ $skip_all}"
+ ;;
+ *)
+ test -z "$skip_all" ||
+ say_color warn "$skip_all"
+ say "1..$test_count"
+ ;;
+ esac
+
+ if test -n "$stress" && test -n "$invert_exit_code"
+ then
+ # We're about to move our "$TRASH_DIRECTORY"
+ # to "$TRASH_DIRECTORY.stress-failed" if
+ # --stress is combined with
+ # --invert-exit-code.
+ say "with --stress and --invert-exit-code we're not removing '$TRASH_DIRECTORY'"
+ elif test -z "$debug" && test -n "$remove_trash"
+ then
+ test -d "$TRASH_DIRECTORY" ||
+ error "Tests passed but trash directory already removed before test cleanup; aborting"
+
+ cd "$TRASH_DIRECTORY/.." &&
+ rm -fr "$TRASH_DIRECTORY" || {
+ # try again in a bit
+ sleep 5;
+ rm -fr "$TRASH_DIRECTORY"
+ } ||
+ error "Tests passed but test cleanup failed; aborting"
+ fi
+
+ check_test_results_san_file_ "$test_failure"
+
+ if test -z "$skip_all" && test -n "$invert_exit_code"
+ then
+ say_color warn "# faking up non-zero exit with --invert-exit-code"
+ GIT_EXIT_OK=t
+ exit 1
+ fi
+
+ test_at_end_hook_
+
+ GIT_EXIT_OK=t
+ exit 0 ;;
+
+ *)
+ say_color error "# failed $test_failure among $msg"
+ say "1..$test_count"
+
+ check_test_results_san_file_ "$test_failure"
+
+ if test -n "$invert_exit_code"
+ then
+ _invert_exit_code_failure_end_blurb
+ GIT_EXIT_OK=t
+ exit 0
+ fi
+
+ GIT_EXIT_OK=t
+ exit 1 ;;
+
+ esac
+}
+
+if test -n "$valgrind"
+then
+ make_symlink () {
+ test -h "$2" &&
+ test "$1" = "$(readlink "$2")" || {
+ # be super paranoid
+ if mkdir "$2".lock
+ then
+ rm -f "$2" &&
+ ln -s "$1" "$2" &&
+ rm -r "$2".lock
+ else
+ while test -d "$2".lock
+ do
+ say "Waiting for lock on $2."
+ sleep 1
+ done
+ fi
+ }
+ }
+
+ make_valgrind_symlink () {
+ # handle only executables, unless they are shell libraries that
+ # need to be in the exec-path.
+ test -x "$1" ||
+ test "# " = "$(test_copy_bytes 2 <"$1")" ||
+ return;
+
+ base=$(basename "$1")
+ case "$base" in
+ test-*)
+ symlink_target="$GIT_BUILD_DIR/t/helper/$base"
+ ;;
+ *)
+ symlink_target="$GIT_BUILD_DIR/$base"
+ ;;
+ esac
+ # do not override scripts
+ if test -x "$symlink_target" &&
+ test ! -d "$symlink_target" &&
+ test "#!" != "$(test_copy_bytes 2 <"$symlink_target")"
+ then
+ symlink_target=../valgrind.sh
+ fi
+ case "$base" in
+ *.sh|*.perl)
+ symlink_target=../unprocessed-script
+ esac
+ # create the link, or replace it if it is out of date
+ make_symlink "$symlink_target" "$GIT_VALGRIND/bin/$base" || exit
+ }
+
+ # override all git executables in TEST_DIRECTORY/..
+ GIT_VALGRIND=$TEST_DIRECTORY/valgrind
+ mkdir -p "$GIT_VALGRIND"/bin
+ for file in $GIT_BUILD_DIR/git* $GIT_BUILD_DIR/t/helper/test-*
+ do
+ make_valgrind_symlink $file
+ done
+ # special-case the mergetools loadables
+ make_symlink "$GIT_BUILD_DIR"/mergetools "$GIT_VALGRIND/bin/mergetools"
+ OLDIFS=$IFS
+ IFS=:
+ for path in $PATH
+ do
+ ls "$path"/git-* 2> /dev/null |
+ while read file
+ do
+ make_valgrind_symlink "$file"
+ done
+ done
+ IFS=$OLDIFS
+ PATH=$GIT_VALGRIND/bin:$PATH
+ GIT_EXEC_PATH=$GIT_VALGRIND/bin
+ export GIT_VALGRIND
+ GIT_VALGRIND_MODE="$valgrind"
+ export GIT_VALGRIND_MODE
+ GIT_VALGRIND_ENABLED=t
+ test -n "$valgrind_only" && GIT_VALGRIND_ENABLED=
+ export GIT_VALGRIND_ENABLED
+elif test -n "$GIT_TEST_INSTALLED"
+then
+ GIT_EXEC_PATH=$($GIT_TEST_INSTALLED/git --exec-path) ||
+ error "Cannot run git from $GIT_TEST_INSTALLED."
+ PATH=$GIT_TEST_INSTALLED:$GIT_BUILD_DIR/t/helper:$PATH
+ GIT_EXEC_PATH=${GIT_TEST_EXEC_PATH:-$GIT_EXEC_PATH}
+else # normal case, use ../bin-wrappers only unless $with_dashes:
+ if test -n "$no_bin_wrappers"
+ then
+ with_dashes=t
+ else
+ git_bin_dir="$GIT_BUILD_DIR/bin-wrappers"
+ if ! test -x "$git_bin_dir/git"
+ then
+ if test -z "$with_dashes"
+ then
+ say "$git_bin_dir/git is not executable; using GIT_EXEC_PATH"
+ fi
+ with_dashes=t
+ fi
+ PATH="$git_bin_dir:$PATH"
+ fi
+ GIT_EXEC_PATH=$GIT_BUILD_DIR
+ if test -n "$with_dashes"
+ then
+ PATH="$GIT_BUILD_DIR:$GIT_BUILD_DIR/t/helper:$PATH"
+ fi
+fi
+GIT_TEMPLATE_DIR="$GIT_BUILD_DIR"/templates/blt
+GIT_CONFIG_NOSYSTEM=1
+GIT_ATTR_NOSYSTEM=1
+GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY/.."
+export PATH GIT_EXEC_PATH GIT_TEMPLATE_DIR GIT_CONFIG_NOSYSTEM GIT_ATTR_NOSYSTEM GIT_CEILING_DIRECTORIES
+
+if test -z "$GIT_TEST_CMP"
+then
+ if test -n "$GIT_TEST_CMP_USE_COPIED_CONTEXT"
+ then
+ GIT_TEST_CMP="$DIFF -c"
+ else
+ GIT_TEST_CMP="$DIFF -u"
+ fi
+fi
+
+GITPERLLIB="$GIT_BUILD_DIR"/perl/build/lib
+export GITPERLLIB
+test -d "$GIT_BUILD_DIR"/templates/blt || {
+ BAIL_OUT "You haven't built things yet, have you?"
+}
+
+if ! test -x "$GIT_BUILD_DIR"/t/helper/test-tool$X
+then
+ BAIL_OUT 'You need to build test-tool; Run "make t/helper/test-tool" in the source (toplevel) directory'
+fi
+
+# Are we running this test at all?
+remove_trash=
+this_test=${0##*/}
+this_test=${this_test%%-*}
+if match_pattern_list "$this_test" "$GIT_SKIP_TESTS"
+then
+ say_color info >&3 "skipping test $this_test altogether"
+ skip_all="skip all tests in $this_test"
+ test_done
+fi
+
+BAIL_OUT_ENV_NEEDS_SANITIZE_LEAK () {
+ BAIL_OUT "$1 has no effect except when compiled with SANITIZE=leak"
+}
+
+if test -n "$SANITIZE_LEAK"
+then
+ # Normalize with test_bool_env
+ passes_sanitize_leak=
+
+ # We need to see TEST_PASSES_SANITIZE_LEAK in "test-tool
+ # env-helper" (via test_bool_env)
+ export TEST_PASSES_SANITIZE_LEAK
+ if test_bool_env TEST_PASSES_SANITIZE_LEAK false
+ then
+ passes_sanitize_leak=t
+ fi
+
+ if test "$GIT_TEST_PASSING_SANITIZE_LEAK" = "check"
+ then
+ sanitize_leak_check=t
+ if test -n "$invert_exit_code"
+ then
+ BAIL_OUT "cannot use --invert-exit-code under GIT_TEST_PASSING_SANITIZE_LEAK=check"
+ fi
+
+ if test -z "$passes_sanitize_leak"
+ then
+ say "in GIT_TEST_PASSING_SANITIZE_LEAK=check mode, setting --invert-exit-code for TEST_PASSES_SANITIZE_LEAK != true"
+ invert_exit_code=t
+ fi
+ elif test -z "$passes_sanitize_leak" &&
+ test_bool_env GIT_TEST_PASSING_SANITIZE_LEAK false
+ then
+ skip_all="skipping $this_test under GIT_TEST_PASSING_SANITIZE_LEAK=true"
+ test_done
+ fi
+
+ if test_bool_env GIT_TEST_SANITIZE_LEAK_LOG false
+ then
+ if ! mkdir -p "$TEST_RESULTS_SAN_DIR"
+ then
+ BAIL_OUT "cannot create $TEST_RESULTS_SAN_DIR"
+ fi &&
+ TEST_RESULTS_SAN_FILE="$TEST_RESULTS_SAN_DIR/$TEST_RESULTS_SAN_FILE_PFX"
+
+ # In case "test-results" is left over from a previous
+ # run: Only report if new leaks show up.
+ TEST_RESULTS_SAN_DIR_NR_LEAKS_STARTUP=$(nr_san_dir_leaks_)
+
+ # Don't litter *.leak dirs if there was nothing to report
+ test_atexit "rmdir \"$TEST_RESULTS_SAN_DIR\" 2>/dev/null || :"
+
+ prepend_var LSAN_OPTIONS : dedup_token_length=9999
+ prepend_var LSAN_OPTIONS : log_exe_name=1
+ prepend_var LSAN_OPTIONS : log_path=\"$TEST_RESULTS_SAN_FILE\"
+ export LSAN_OPTIONS
+ fi
+elif test "$GIT_TEST_PASSING_SANITIZE_LEAK" = "check" ||
+ test_bool_env GIT_TEST_PASSING_SANITIZE_LEAK false
+then
+ BAIL_OUT_ENV_NEEDS_SANITIZE_LEAK "GIT_TEST_PASSING_SANITIZE_LEAK=true"
+elif test_bool_env GIT_TEST_SANITIZE_LEAK_LOG false
+then
+ BAIL_OUT_ENV_NEEDS_SANITIZE_LEAK "GIT_TEST_SANITIZE_LEAK_LOG=true"
+fi
+
+if test "${GIT_TEST_CHAIN_LINT:-1}" != 0 &&
+ test "${GIT_TEST_EXT_CHAIN_LINT:-1}" != 0
+then
+ "$PERL_PATH" "$TEST_DIRECTORY/chainlint.pl" "$0" ||
+ BUG "lint error (see '?!...!? annotations above)"
+fi
+
+# Last-minute variable setup
+USER_HOME="$HOME"
+HOME="$TRASH_DIRECTORY"
+GNUPGHOME="$HOME/gnupg-home-not-used"
+export HOME GNUPGHOME USER_HOME
+
+# "rm -rf" existing trash directory, even if a previous run left it
+# with bad permissions.
+remove_trash_directory () {
+ dir="$1"
+ if ! rm -rf "$dir" 2>/dev/null
+ then
+ chmod -R u+rwx "$dir"
+ rm -rf "$dir"
+ fi
+ ! test -d "$dir"
+}
+
+# Test repository
+remove_trash_directory "$TRASH_DIRECTORY" || {
+ BAIL_OUT 'cannot prepare test area'
+}
+
+remove_trash=t
+if test -z "$TEST_NO_CREATE_REPO"
+then
+ git init \
+ ${TEST_CREATE_REPO_NO_TEMPLATE:+--template=} \
+ "$TRASH_DIRECTORY" >&3 2>&4 ||
+ error "cannot run git init"
+else
+ mkdir -p "$TRASH_DIRECTORY"
+fi
+
+# Use -P to resolve symlinks in our working directory so that the cwd
+# in subprocesses like git equals our $PWD (for pathname comparisons).
+cd -P "$TRASH_DIRECTORY" || BAIL_OUT "cannot cd -P to \"$TRASH_DIRECTORY\""
+
+start_test_output "$0"
+
+# Convenience
+# A regexp to match 5 and 35 hexdigits
+_x05='[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]'
+_x35="$_x05$_x05$_x05$_x05$_x05$_x05$_x05"
+
+test_oid_init
+
+ZERO_OID=$(test_oid zero)
+OID_REGEX=$(echo $ZERO_OID | sed -e 's/0/[0-9a-f]/g')
+OIDPATH_REGEX=$(test_oid_to_path $ZERO_OID | sed -e 's/0/[0-9a-f]/g')
+EMPTY_TREE=$(test_oid empty_tree)
+EMPTY_BLOB=$(test_oid empty_blob)
+
+# Provide an implementation of the 'yes' utility; the upper bound
+# limit is there to help Windows that cannot stop this loop from
+# wasting cycles when the downstream stops reading, so do not be
+# tempted to turn it into an infinite loop. cf. 6129c930 ("test-lib:
+# limit the output of the yes utility", 2016-02-02)
+yes () {
+ if test $# = 0
+ then
+ y=y
+ else
+ y="$*"
+ fi
+
+ i=0
+ while test $i -lt 99
+ do
+ echo "$y"
+ i=$(($i+1))
+ done
+}
+
+# The GIT_TEST_FAIL_PREREQS code hooks into test_set_prereq(), and
+# thus needs to be set up really early, and set an internal variable
+# for convenience so the hot test_set_prereq() codepath doesn't need
+# to call "test-tool env-helper" (via test_bool_env). Only do that work
+# if needed by seeing if GIT_TEST_FAIL_PREREQS is set at all.
+GIT_TEST_FAIL_PREREQS_INTERNAL=
+if test -n "$GIT_TEST_FAIL_PREREQS"
+then
+ if test_bool_env GIT_TEST_FAIL_PREREQS false
+ then
+ GIT_TEST_FAIL_PREREQS_INTERNAL=true
+ test_set_prereq FAIL_PREREQS
+ fi
+else
+ test_lazy_prereq FAIL_PREREQS '
+ test_bool_env GIT_TEST_FAIL_PREREQS false
+ '
+fi
+
+# Fix some commands on Windows, and other OS-specific things
+uname_s=$(uname -s)
+case $uname_s in
+*MINGW*)
+ # Windows has its own (incompatible) sort and find
+ sort () {
+ /usr/bin/sort "$@"
+ }
+ find () {
+ /usr/bin/find "$@"
+ }
+ # git sees Windows-style pwd
+ pwd () {
+ builtin pwd -W
+ }
+ # no POSIX permissions
+ # backslashes in pathspec are converted to '/'
+ # exec does not inherit the PID
+ test_set_prereq MINGW
+ test_set_prereq NATIVE_CRLF
+ test_set_prereq SED_STRIPS_CR
+ test_set_prereq GREP_STRIPS_CR
+ test_set_prereq WINDOWS
+ GIT_TEST_CMP="GIT_DIR=/dev/null git diff --no-index --ignore-cr-at-eol --"
+ ;;
+*CYGWIN*)
+ test_set_prereq POSIXPERM
+ test_set_prereq EXECKEEPSPID
+ test_set_prereq CYGWIN
+ test_set_prereq SED_STRIPS_CR
+ test_set_prereq GREP_STRIPS_CR
+ test_set_prereq WINDOWS
+ ;;
+*)
+ test_set_prereq POSIXPERM
+ test_set_prereq BSLASHPSPEC
+ test_set_prereq EXECKEEPSPID
+ ;;
+esac
+
+# Detect arches where a few things don't work
+uname_m=$(uname -m)
+case $uname_m in
+parisc* | hppa*)
+ test_set_prereq HPPA
+ ;;
+esac
+
+test_set_prereq REFFILES
+
+( COLUMNS=1 && test $COLUMNS = 1 ) && test_set_prereq COLUMNS_CAN_BE_1
+test -z "$NO_CURL" && test_set_prereq LIBCURL
+test -z "$NO_PERL" && test_set_prereq PERL
+test -z "$NO_PTHREADS" && test_set_prereq PTHREADS
+test -z "$NO_PYTHON" && test_set_prereq PYTHON
+test -n "$USE_LIBPCRE2" && test_set_prereq PCRE
+test -n "$USE_LIBPCRE2" && test_set_prereq LIBPCRE2
+test -z "$NO_GETTEXT" && test_set_prereq GETTEXT
+test -n "$SANITIZE_LEAK" && test_set_prereq SANITIZE_LEAK
+test -n "$GIT_VALGRIND_ENABLED" && test_set_prereq VALGRIND
+
+if test -z "$GIT_TEST_CHECK_CACHE_TREE"
+then
+ GIT_TEST_CHECK_CACHE_TREE=true
+ export GIT_TEST_CHECK_CACHE_TREE
+fi
+
+test_lazy_prereq PIPE '
+ # test whether the filesystem supports FIFOs
+ test_have_prereq !MINGW,!CYGWIN &&
+ rm -f testfifo && mkfifo testfifo
+'
+
+test_lazy_prereq SYMLINKS '
+ # test whether the filesystem supports symbolic links
+ ln -s x y && test -h y
+'
+
+test_lazy_prereq SYMLINKS_WINDOWS '
+ # test whether symbolic links are enabled on Windows
+ test_have_prereq MINGW &&
+ cmd //c "mklink y x" &> /dev/null && test -h y
+'
+
+test_lazy_prereq FILEMODE '
+ test "$(git config --bool core.filemode)" = true
+'
+
+test_lazy_prereq CASE_INSENSITIVE_FS '
+ echo good >CamelCase &&
+ echo bad >camelcase &&
+ test "$(cat CamelCase)" != good
+'
+
+test_lazy_prereq FUNNYNAMES '
+ test_have_prereq !MINGW &&
+ touch -- \
+ "FUNNYNAMES tab embedded" \
+ "FUNNYNAMES \"quote embedded\"" \
+ "FUNNYNAMES newline
+embedded" 2>/dev/null &&
+ rm -- \
+ "FUNNYNAMES tab embedded" \
+ "FUNNYNAMES \"quote embedded\"" \
+ "FUNNYNAMES newline
+embedded" 2>/dev/null
+'
+
+test_lazy_prereq UTF8_NFD_TO_NFC '
+ # check whether FS converts nfd unicode to nfc
+ auml=$(printf "\303\244")
+ aumlcdiar=$(printf "\141\314\210")
+ >"$auml" &&
+ test -f "$aumlcdiar"
+'
+
+test_lazy_prereq AUTOIDENT '
+ sane_unset GIT_AUTHOR_NAME &&
+ sane_unset GIT_AUTHOR_EMAIL &&
+ git var GIT_AUTHOR_IDENT
+'
+
+test_lazy_prereq EXPENSIVE '
+ test -n "$GIT_TEST_LONG"
+'
+
+test_lazy_prereq EXPENSIVE_ON_WINDOWS '
+ test_have_prereq EXPENSIVE || test_have_prereq !MINGW,!CYGWIN
+'
+
+test_lazy_prereq USR_BIN_TIME '
+ test -x /usr/bin/time
+'
+
+test_lazy_prereq NOT_ROOT '
+ uid=$(id -u) &&
+ test "$uid" != 0
+'
+
+test_lazy_prereq JGIT '
+ jgit --version
+'
+
+# SANITY is about "can you correctly predict what the filesystem would
+# do by only looking at the permission bits of the files and
+# directories?" A typical example of !SANITY is running the test
+# suite as root, where a test may expect "chmod -r file && cat file"
+# to fail because file is supposed to be unreadable after a successful
+# chmod. In an environment (i.e. combination of what filesystem is
+# being used and who is running the tests) that lacks SANITY, you may
+# be able to delete or create a file when the containing directory
+# doesn't have write permissions, or access a file even if the
+# containing directory doesn't have read or execute permissions.
+
+test_lazy_prereq SANITY '
+ mkdir SANETESTD.1 SANETESTD.2 &&
+
+ chmod +w SANETESTD.1 SANETESTD.2 &&
+ >SANETESTD.1/x 2>SANETESTD.2/x &&
+ chmod -w SANETESTD.1 &&
+ chmod -r SANETESTD.1/x &&
+ chmod -rx SANETESTD.2 ||
+ BUG "cannot prepare SANETESTD"
+
+ ! test -r SANETESTD.1/x &&
+ ! rm SANETESTD.1/x && ! test -f SANETESTD.2/x
+ status=$?
+
+ chmod +rwx SANETESTD.1 SANETESTD.2 &&
+ rm -rf SANETESTD.1 SANETESTD.2 ||
+ BUG "cannot clean SANETESTD"
+ return $status
+'
+
+test FreeBSD != $uname_s || GIT_UNZIP=${GIT_UNZIP:-/usr/local/bin/unzip}
+GIT_UNZIP=${GIT_UNZIP:-unzip}
+test_lazy_prereq UNZIP '
+ "$GIT_UNZIP" -v
+ test $? -ne 127
+'
+
+run_with_limited_cmdline () {
+ (ulimit -s 128 && "$@")
+}
+
+test_lazy_prereq CMDLINE_LIMIT '
+ test_have_prereq !HPPA,!MINGW,!CYGWIN &&
+ run_with_limited_cmdline true
+'
+
+run_with_limited_stack () {
+ (ulimit -s 128 && "$@")
+}
+
+test_lazy_prereq ULIMIT_STACK_SIZE '
+ test_have_prereq !HPPA,!MINGW,!CYGWIN &&
+ run_with_limited_stack true
+'
+
+run_with_limited_open_files () {
+ (ulimit -n 32 && "$@")
+}
+
+test_lazy_prereq ULIMIT_FILE_DESCRIPTORS '
+ test_have_prereq !MINGW,!CYGWIN &&
+ run_with_limited_open_files true
+'
+
+build_option () {
+ git version --build-options |
+ sed -ne "s/^$1: //p"
+}
+
+test_lazy_prereq SIZE_T_IS_64BIT '
+ test 8 -eq "$(build_option sizeof-size_t)"
+'
+
+test_lazy_prereq LONG_IS_64BIT '
+ test 8 -le "$(build_option sizeof-long)"
+'
+
+test_lazy_prereq TIME_IS_64BIT 'test-tool date is64bit'
+test_lazy_prereq TIME_T_IS_64BIT 'test-tool date time_t-is64bit'
+
+test_lazy_prereq CURL '
+ curl --version
+'
+
+# SHA1 is a test if the hash algorithm in use is SHA-1. This is both for tests
+# which will not work with other hash algorithms and tests that work but don't
+# test anything meaningful (e.g. special values which cause short collisions).
+test_lazy_prereq SHA1 '
+ case "$GIT_DEFAULT_HASH" in
+ sha1) true ;;
+ "") test $(git hash-object /dev/null) = e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 ;;
+ *) false ;;
+ esac
+'
+
+# Ensure that no test accidentally triggers a Git command
+# that runs the actual maintenance scheduler, affecting a user's
+# system permanently.
+# Tests that verify the scheduler integration must set this locally
+# to avoid errors.
+GIT_TEST_MAINT_SCHEDULER="none:exit 1"
+
+# Does this platform support `git fsmonitor--daemon`
+#
+test_lazy_prereq FSMONITOR_DAEMON '
+ git version --build-options >output &&
+ grep "feature: fsmonitor--daemon" output
+'
diff --git a/t/test-terminal.perl b/t/test-terminal.perl
new file mode 100755
index 0000000..1bcf01a
--- /dev/null
+++ b/t/test-terminal.perl
@@ -0,0 +1,105 @@
+#!/usr/bin/perl
+use 5.008;
+use strict;
+use warnings;
+use IO::Pty;
+use File::Copy;
+
+# Run @$argv in the background with stdio redirected to $in, $out and $err.
+sub start_child {
+ my ($argv, $in, $out, $err) = @_;
+ my $pid = fork;
+ if (not defined $pid) {
+ die "fork failed: $!"
+ } elsif ($pid == 0) {
+ open STDIN, "<&", $in;
+ open STDOUT, ">&", $out;
+ open STDERR, ">&", $err;
+ close $in;
+ close $out;
+ exec(@$argv) or die "cannot exec '$argv->[0]': $!"
+ }
+ return $pid;
+}
+
+# Wait for $pid to finish.
+sub finish_child {
+ # Simplified from wait_or_whine() in run-command.c.
+ my ($pid) = @_;
+
+ my $waiting = waitpid($pid, 0);
+ if ($waiting < 0) {
+ die "waitpid failed: $!";
+ } elsif ($? & 127) {
+ my $code = $? & 127;
+ warn "died of signal $code";
+ return $code + 128;
+ } else {
+ return $? >> 8;
+ }
+}
+
+sub xsendfile {
+ my ($out, $in) = @_;
+
+ # Note: the real sendfile() cannot read from a terminal.
+
+ # It is unspecified by POSIX whether reads
+ # from a disconnected terminal will return
+ # EIO (as in AIX 4.x, IRIX, and Linux) or
+ # end-of-file. Either is fine.
+ copy($in, $out, 4096) or $!{EIO} or die "cannot copy from child: $!";
+}
+
+sub copy_stdin {
+ my ($in) = @_;
+ my $pid = fork;
+ if (!$pid) {
+ xsendfile($in, \*STDIN);
+ exit 0;
+ }
+ close($in);
+ return $pid;
+}
+
+sub copy_stdio {
+ my ($out, $err) = @_;
+ my $pid = fork;
+ defined $pid or die "fork failed: $!";
+ if (!$pid) {
+ close($out);
+ xsendfile(\*STDERR, $err);
+ exit 0;
+ }
+ close($err);
+ xsendfile(\*STDOUT, $out);
+ finish_child($pid) == 0
+ or exit 1;
+}
+
+if ($#ARGV < 1) {
+ die "usage: test-terminal program args";
+}
+$ENV{TERM} = 'vt100';
+my $parent_in = new IO::Pty;
+my $parent_out = new IO::Pty;
+my $parent_err = new IO::Pty;
+$parent_in->set_raw();
+$parent_out->set_raw();
+$parent_err->set_raw();
+$parent_in->slave->set_raw();
+$parent_out->slave->set_raw();
+$parent_err->slave->set_raw();
+my $pid = start_child(\@ARGV, $parent_in->slave, $parent_out->slave, $parent_err->slave);
+close $parent_in->slave;
+close $parent_out->slave;
+close $parent_err->slave;
+my $in_pid = copy_stdin($parent_in);
+copy_stdio($parent_out, $parent_err);
+my $ret = finish_child($pid);
+# If the child process terminates before our copy_stdin() process is able to
+# write all of its data to $parent_in, the copy_stdin() process could stall.
+# Send SIGTERM to it to ensure it terminates.
+kill 'TERM', $in_pid;
+finish_child($in_pid);
+exit($ret);
diff --git a/t/valgrind/.gitignore b/t/valgrind/.gitignore
new file mode 100644
index 0000000..d4ae667
--- /dev/null
+++ b/t/valgrind/.gitignore
@@ -0,0 +1,2 @@
+/bin/
+/templates
diff --git a/t/valgrind/analyze.sh b/t/valgrind/analyze.sh
new file mode 100755
index 0000000..2ffc80f
--- /dev/null
+++ b/t/valgrind/analyze.sh
@@ -0,0 +1,127 @@
+#!/bin/sh
+
+# Get TEST_OUTPUT_DIRECTORY from GIT-BUILD-OPTIONS if it's there...
+. "$(dirname "$0")/../../GIT-BUILD-OPTIONS"
+# ... otherwise set it to the default value.
+: ${TEST_OUTPUT_DIRECTORY=$(dirname "$0")/..}
+
+output=
+count=0
+total_count=0
+missing_message=
+new_line='
+'
+
+# start outputting the current valgrind error in $out_prefix.++$count,
+# and the test case which failed in the corresponding .message file
+start_output () {
+ test -z "$output" || return
+
+ # progress
+ total_count=$(($total_count+1))
+ test -t 2 && printf "\rFound %d errors" $total_count >&2
+
+ count=$(($count+1))
+ output=$out_prefix.$count
+ : > $output
+
+ echo "*** $1 ***" > $output.message
+}
+
+finish_output () {
+ test ! -z "$output" || return
+ output=
+
+ # if a test case has more than one valgrind error, we need to
+ # copy the last .message file to the previous errors
+ test -z "$missing_message" || {
+ while test $missing_message -lt $count
+ do
+ cp $out_prefix.$count.message \
+ $out_prefix.$missing_message.message
+ missing_message=$(($missing_message+1))
+ done
+ missing_message=
+ }
+}
+
+# group the valgrind errors by backtrace
+output_all () {
+ last_line=
+ j=0
+ i=1
+ while test $i -le $count
+ do
+ # output <number> <backtrace-in-one-line>
+ echo "$i $(tr '\n' ' ' < $out_prefix.$i)"
+ i=$(($i+1))
+ done |
+ sort -t ' ' -k 2 | # order by <backtrace-in-one-line>
+ while read number line
+ do
+ # find duplicates, do not output backtrace twice
+ if test "$line" != "$last_line"
+ then
+ last_line=$line
+ j=$(($j+1))
+ printf "\nValgrind error $j:\n\n"
+ cat $out_prefix.$number
+ printf "\nfound in:\n"
+ fi
+ # print the test case where this came from
+ printf "\n"
+ cat $out_prefix.$number.message
+ done
+}
+
+handle_one () {
+ OLDIFS=$IFS
+ IFS="$new_line"
+ while read line
+ do
+ case "$line" in
+ # backtrace, possibly a new one
+ ==[0-9]*)
+
+ # Does the current valgrind error have a message yet?
+ case "$output" in
+ *.message)
+ test -z "$missing_message" &&
+ missing_message=$count
+ output=
+ esac
+
+ start_output $(basename $1)
+ echo "$line" |
+ sed 's/==[0-9]*==/==valgrind==/' >> $output
+ ;;
+ # end of backtrace
+ '}')
+ test -z "$output" || {
+ echo "$line" >> $output
+ test $output = ${output%.message} &&
+ output=$output.message
+ }
+ ;;
+ # end of test case
+ '')
+ finish_output
+ ;;
+ # normal line; if $output is set, print the line
+ *)
+ test -z "$output" || echo "$line" >> $output
+ ;;
+ esac
+ done < $1
+ IFS=$OLDIFS
+
+ # just to be safe
+ finish_output
+}
+
+for test_script in "$TEST_OUTPUT_DIRECTORY"/test-results/*.out
+do
+ handle_one $test_script
+done
+
+output_all
diff --git a/t/valgrind/default.supp b/t/valgrind/default.supp
new file mode 100644
index 0000000..0a6724f
--- /dev/null
+++ b/t/valgrind/default.supp
@@ -0,0 +1,51 @@
+{
+ ignore-zlib-errors-cond
+ Memcheck:Cond
+ obj:*libz.so*
+}
+
+{
+ ignore-zlib-errors-value8
+ Memcheck:Value8
+ obj:*libz.so*
+}
+
+{
+ ignore-zlib-errors-value4
+ Memcheck:Value4
+ obj:*libz.so*
+}
+
+{
+ ignore-ldso-cond
+ Memcheck:Cond
+ obj:*ld-*.so
+}
+
+{
+ ignore-ldso-addr8
+ Memcheck:Addr8
+ obj:*ld-*.so
+}
+
+{
+ ignore-ldso-addr4
+ Memcheck:Addr4
+ obj:*ld-*.so
+}
+
+{
+ writing-data-from-zlib-triggers-even-more-errors
+ Memcheck:Param
+ write(buf)
+ obj:/lib/ld-*.so
+ fun:write_in_full
+ fun:write_buffer
+ fun:write_loose_object
+}
+
+{
+ ignore-sse-strlen-invalid-read-size
+ Memcheck:Addr4
+ fun:copy_ref
+}
diff --git a/t/valgrind/valgrind.sh b/t/valgrind/valgrind.sh
new file mode 100755
index 0000000..669ebaf
--- /dev/null
+++ b/t/valgrind/valgrind.sh
@@ -0,0 +1,40 @@
+#!/bin/sh
+
+base=$(basename "$0")
+case "$base" in
+test-*)
+ program="$GIT_VALGRIND/../../t/helper/$base"
+ ;;
+*)
+ program="$GIT_VALGRIND/../../$base"
+ ;;
+esac
+
+TOOL_OPTIONS='--leak-check=no'
+
+test -z "$GIT_VALGRIND_ENABLED" &&
+exec "$program" "$@"
+
+case "$GIT_VALGRIND_MODE" in
+memcheck-fast)
+ ;;
+memcheck)
+ VALGRIND_VERSION=$(valgrind --version)
+ VALGRIND_MAJOR=$(expr "$VALGRIND_VERSION" : '[^0-9]*\([0-9]*\)')
+ VALGRIND_MINOR=$(expr "$VALGRIND_VERSION" : '[^0-9]*[0-9]*\.\([0-9]*\)')
+ test 3 -gt "$VALGRIND_MAJOR" ||
+ test 3 -eq "$VALGRIND_MAJOR" -a 4 -gt "$VALGRIND_MINOR" ||
+ TOOL_OPTIONS="$TOOL_OPTIONS --track-origins=yes"
+ ;;
+*)
+ TOOL_OPTIONS="--tool=$GIT_VALGRIND_MODE"
+esac
+
+exec valgrind -q --error-exitcode=126 \
+ --gen-suppressions=all \
+ --suppressions="$GIT_VALGRIND/default.supp" \
+ $TOOL_OPTIONS \
+ --log-fd=4 \
+ --input-fd=4 \
+ $GIT_VALGRIND_OPTIONS \
+ "$program" "$@"